Hex Artifact Content
Not logged in

Artifact eb3a6cb924e37c28bb50051322345ef5393bdb41:

File src/sqlite3.c part of check-in [dffe11c29c] - Update to the latest version of SQLite (3.6.20rc1) and fix some compiler warnings. by drh on 2009-11-01 19:25:47.

0000: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
0010: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0020: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0030: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0040: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
0050: 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 73 20  ** This file is 
0060: 61 6e 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20  an amalgamation 
0070: 6f 66 20 6d 61 6e 79 20 73 65 70 61 72 61 74 65  of many separate
0080: 20 43 20 73 6f 75 72 63 65 20 66 69 6c 65 73 20   C source files 
0090: 66 72 6f 6d 20 53 51 4c 69 74 65 0a 2a 2a 20 76  from SQLite.** v
00a0: 65 72 73 69 6f 6e 20 33 2e 36 2e 32 30 2e 20 20  ersion 3.6.20.  
00b0: 42 79 20 63 6f 6d 62 69 6e 69 6e 67 20 61 6c 6c  By combining all
00c0: 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20   the individual 
00d0: 43 20 63 6f 64 65 20 66 69 6c 65 73 20 69 6e 74  C code files int
00e0: 6f 20 74 68 69 73 20 0a 2a 2a 20 73 69 6e 67 6c  o this .** singl
00f0: 65 20 6c 61 72 67 65 20 66 69 6c 65 2c 20 74 68  e large file, th
0100: 65 20 65 6e 74 69 72 65 20 63 6f 64 65 20 63 61  e entire code ca
0110: 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 61 73  n be compiled as
0120: 20 61 20 6f 6e 65 20 74 72 61 6e 73 6c 61 74 69   a one translati
0130: 6f 6e 0a 2a 2a 20 75 6e 69 74 2e 20 20 54 68 69  on.** unit.  Thi
0140: 73 20 61 6c 6c 6f 77 73 20 6d 61 6e 79 20 63 6f  s allows many co
0150: 6d 70 69 6c 65 72 73 20 74 6f 20 64 6f 20 6f 70  mpilers to do op
0160: 74 69 6d 69 7a 61 74 69 6f 6e 73 20 74 68 61 74  timizations that
0170: 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a   would not be.**
0180: 20 70 6f 73 73 69 62 6c 65 20 69 66 20 74 68 65   possible if the
0190: 20 66 69 6c 65 73 20 77 65 72 65 20 63 6f 6d 70   files were comp
01a0: 69 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e  iled separately.
01b0: 20 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d    Performance im
01c0: 70 72 6f 76 65 6d 65 6e 74 73 0a 2a 2a 20 6f 66  provements.** of
01d0: 20 35 25 20 61 72 65 20 6d 6f 72 65 20 61 72 65   5% are more are
01e0: 20 63 6f 6d 6d 6f 6e 6c 79 20 73 65 65 6e 20 77   commonly seen w
01f0: 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 6f  hen SQLite is co
0200: 6d 70 69 6c 65 64 20 61 73 20 61 20 73 69 6e 67  mpiled as a sing
0210: 6c 65 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69 6f  le.** translatio
0220: 6e 20 75 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  n unit..**.** Th
0230: 69 73 20 66 69 6c 65 20 69 73 20 61 6c 6c 20 79  is file is all y
0240: 6f 75 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70 69  ou need to compi
0250: 6c 65 20 53 51 4c 69 74 65 2e 20 20 54 6f 20 75  le SQLite.  To u
0260: 73 65 20 53 51 4c 69 74 65 20 69 6e 20 6f 74 68  se SQLite in oth
0270: 65 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 2c 20  er.** programs, 
0280: 79 6f 75 20 6e 65 65 64 20 74 68 69 73 20 66 69  you need this fi
0290: 6c 65 20 61 6e 64 20 74 68 65 20 22 73 71 6c 69  le and the "sqli
02a0: 74 65 33 2e 68 22 20 68 65 61 64 65 72 20 66 69  te3.h" header fi
02b0: 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 0a  le that defines.
02c0: 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 6d 69  ** the programmi
02d0: 6e 67 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20  ng interface to 
02e0: 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
02f0: 72 79 2e 20 20 28 49 66 20 79 6f 75 20 64 6f 20  ry.  (If you do 
0300: 6e 6f 74 20 68 61 76 65 20 0a 2a 2a 20 74 68 65  not have .** the
0310: 20 22 73 71 6c 69 74 65 33 2e 68 22 20 68 65 61   "sqlite3.h" hea
0320: 64 65 72 20 66 69 6c 65 20 61 74 20 68 61 6e 64  der file at hand
0330: 2c 20 79 6f 75 20 77 69 6c 6c 20 66 69 6e 64 20  , you will find 
0340: 61 20 63 6f 70 79 20 65 6d 62 65 64 64 65 64 20  a copy embedded 
0350: 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20 74 65  within.** the te
0360: 78 74 20 6f 66 20 74 68 69 73 20 66 69 6c 65 2e  xt of this file.
0370: 20 20 53 65 61 72 63 68 20 66 6f 72 20 22 42 65    Search for "Be
0380: 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 33  gin file sqlite3
0390: 2e 68 22 20 74 6f 20 66 69 6e 64 20 74 68 65 20  .h" to find the 
03a0: 73 74 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20  start.** of the 
03b0: 65 6d 62 65 64 64 65 64 20 73 71 6c 69 74 65 33  embedded sqlite3
03c0: 2e 68 20 68 65 61 64 65 72 20 66 69 6c 65 2e 29  .h header file.)
03d0: 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 64 65   Additional code
03e0: 20 66 69 6c 65 73 20 6d 61 79 20 62 65 20 6e 65   files may be ne
03f0: 65 64 65 64 0a 2a 2a 20 69 66 20 79 6f 75 20 77  eded.** if you w
0400: 61 6e 74 20 61 20 77 72 61 70 70 65 72 20 74 6f  ant a wrapper to
0410: 20 69 6e 74 65 72 66 61 63 65 20 53 51 4c 69 74   interface SQLit
0420: 65 20 77 69 74 68 20 79 6f 75 72 20 63 68 6f 69  e with your choi
0430: 63 65 20 6f 66 20 70 72 6f 67 72 61 6d 6d 69 6e  ce of programmin
0440: 67 0a 2a 2a 20 6c 61 6e 67 75 61 67 65 2e 20 54  g.** language. T
0450: 68 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20  he code for the 
0460: 22 73 71 6c 69 74 65 33 22 20 63 6f 6d 6d 61 6e  "sqlite3" comman
0470: 64 2d 6c 69 6e 65 20 73 68 65 6c 6c 20 69 73 20  d-line shell is 
0480: 61 6c 73 6f 20 69 6e 20 61 0a 2a 2a 20 73 65 70  also in a.** sep
0490: 61 72 61 74 65 20 66 69 6c 65 2e 20 54 68 69 73  arate file. This
04a0: 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6f   file contains o
04b0: 6e 6c 79 20 63 6f 64 65 20 66 6f 72 20 74 68 65  nly code for the
04c0: 20 63 6f 72 65 20 53 51 4c 69 74 65 20 6c 69 62   core SQLite lib
04d0: 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  rary..**.** This
04e0: 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 77 61   amalgamation wa
04f0: 73 20 67 65 6e 65 72 61 74 65 64 20 6f 6e 20 32  s generated on 2
0500: 30 30 39 2d 31 31 2d 30 31 20 31 39 3a 32 32 3a  009-11-01 19:22:
0510: 30 33 20 55 54 43 2e 0a 2a 2f 0a 23 64 65 66 69  03 UTC..*/.#defi
0520: 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 31  ne SQLITE_CORE 1
0530: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
0540: 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 31 0a 23  AMALGAMATION 1.#
0550: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 52  ifndef SQLITE_PR
0560: 49 56 41 54 45 0a 23 20 64 65 66 69 6e 65 20 53  IVATE.# define S
0570: 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 74  QLITE_PRIVATE st
0580: 61 74 69 63 0a 23 65 6e 64 69 66 0a 23 69 66 6e  atic.#endif.#ifn
0590: 64 65 66 20 53 51 4c 49 54 45 5f 41 50 49 0a 23  def SQLITE_API.#
05a0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41   define SQLITE_A
05b0: 50 49 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a 2a 2a  PI.#endif./*****
05c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
05d0: 66 69 6c 65 20 73 71 6c 69 74 65 49 6e 74 2e 68  file sqliteInt.h
05e0: 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
05f0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0600: 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
0610: 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
0620: 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
0630: 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
0640: 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
0650: 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
0660: 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
0670: 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
0680: 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
0690: 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
06a0: 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
06b0: 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
06c0: 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
06d0: 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
06e0: 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
06f0: 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
0700: 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
0710: 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
0720: 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
0730: 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
0740: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0750: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0760: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0770: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0780: 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 69 6e 74  .** Internal int
0790: 65 72 66 61 63 65 20 64 65 66 69 6e 69 74 69 6f  erface definitio
07a0: 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a 2a  ns for SQLite..*
07b0: 2a 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51  *.*/.#ifndef _SQ
07c0: 4c 49 54 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69  LITEINT_H_.#defi
07d0: 6e 65 20 5f 53 51 4c 49 54 45 49 4e 54 5f 48 5f  ne _SQLITEINT_H_
07e0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64  ../*.** These #d
07f0: 65 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e  efines should en
0800: 61 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73  able >2GB file s
0810: 75 70 70 6f 72 74 20 6f 6e 20 50 4f 53 49 58 20  upport on POSIX 
0820: 69 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c  if the.** underl
0830: 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73  ying operating s
0840: 79 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69  ystem supports i
0850: 74 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61  t.  If the OS la
0860: 63 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c  cks.** large fil
0870: 65 20 73 75 70 70 6f 72 74 2c 20 6f 72 20 69 66  e support, or if
0880: 20 74 68 65 20 4f 53 20 69 73 20 77 69 6e 64 6f   the OS is windo
0890: 77 73 2c 20 74 68 65 73 65 20 73 68 6f 75 6c 64  ws, these should
08a0: 20 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a   be no-ops..**.*
08b0: 2a 20 54 69 63 6b 65 74 20 23 32 37 33 39 3a 20  * Ticket #2739: 
08c0: 20 54 68 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f   The _LARGEFILE_
08d0: 53 4f 55 52 43 45 20 6d 61 63 72 6f 20 6d 75 73  SOURCE macro mus
08e0: 74 20 61 70 70 65 61 72 20 62 65 66 6f 72 65 20  t appear before 
08f0: 61 6e 79 0a 2a 2a 20 73 79 73 74 65 6d 20 23 69  any.** system #i
0900: 6e 63 6c 75 64 65 73 2e 20 20 48 65 6e 63 65 2c  ncludes.  Hence,
0910: 20 74 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63   this block of c
0920: 6f 64 65 20 6d 75 73 74 20 62 65 20 74 68 65 20  ode must be the 
0930: 76 65 72 79 20 66 69 72 73 74 0a 2a 2a 20 63 6f  very first.** co
0940: 64 65 20 69 6e 20 61 6c 6c 20 73 6f 75 72 63 65  de in all source
0950: 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61   files..**.** La
0960: 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74  rge file support
0970: 20 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64   can be disabled
0980: 20 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c   using the -DSQL
0990: 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20  ITE_DISABLE_LFS 
09a0: 73 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65  switch.** on the
09b0: 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e   compiler comman
09c0: 64 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73  d line.  This is
09d0: 20 6e 65 63 65 73 73 61 72 79 20 69 66 20 79 6f   necessary if yo
09e0: 75 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a  u are compiling.
09f0: 2a 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d  ** on a recent m
0a00: 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 20  achine (ex: Red 
0a10: 48 61 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75  Hat 7.2) but you
0a20: 20 77 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20   want your code 
0a30: 74 6f 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e  to work.** on an
0a40: 20 6f 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28   older machine (
0a50: 65 78 3a 20 52 65 64 20 48 61 74 20 36 2e 30 29  ex: Red Hat 6.0)
0a60: 2e 20 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c  .  If you compil
0a70: 65 20 6f 6e 20 52 65 64 20 48 61 74 20 37 2e 32  e on Red Hat 7.2
0a80: 0a 2a 2a 20 77 69 74 68 6f 75 74 20 74 68 69 73  .** without this
0a90: 20 6f 70 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20   option, LFS is 
0aa0: 65 6e 61 62 6c 65 2e 20 20 42 75 74 20 4c 46 53  enable.  But LFS
0ab0: 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20   does not exist 
0ac0: 69 6e 20 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a  in the kernel.**
0ad0: 20 69 6e 20 52 65 64 20 48 61 74 20 36 2e 30 2c   in Red Hat 6.0,
0ae0: 20 73 6f 20 74 68 65 20 63 6f 64 65 20 77 6f 6e   so the code won
0af0: 27 74 20 77 6f 72 6b 2e 20 20 48 65 6e 63 65 2c  't work.  Hence,
0b00: 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 62 69 6e   for maximum bin
0b10: 61 72 79 0a 2a 2a 20 70 6f 72 74 61 62 69 6c 69  ary.** portabili
0b20: 74 79 20 79 6f 75 20 73 68 6f 75 6c 64 20 6f 6d  ty you should om
0b30: 69 74 20 4c 46 53 2e 0a 2a 2a 0a 2a 2a 20 53 69  it LFS..**.** Si
0b40: 6d 69 6c 61 72 20 69 73 20 74 72 75 65 20 66 6f  milar is true fo
0b50: 72 20 4d 61 63 20 4f 53 20 58 2e 20 20 4c 46 53  r Mac OS X.  LFS
0b60: 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72 74   is only support
0b70: 65 64 20 6f 6e 20 4d 61 63 20 4f 53 20 58 20 39  ed on Mac OS X 9
0b80: 20 61 6e 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 23   and later..*/.#
0b90: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49  ifndef SQLITE_DI
0ba0: 53 41 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69  SABLE_LFS.# defi
0bb0: 6e 65 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20  ne _LARGE_FILE  
0bc0: 20 20 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20       1.# ifndef 
0bd0: 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54  _FILE_OFFSET_BIT
0be0: 53 0a 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49  S.#   define _FI
0bf0: 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36  LE_OFFSET_BITS 6
0c00: 34 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69  4.# endif.# defi
0c10: 6e 65 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f  ne _LARGEFILE_SO
0c20: 55 52 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 2f  URCE 1.#endif../
0c30: 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 74 68 65  *.** Include the
0c40: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 68   configuration h
0c50: 65 61 64 65 72 20 6f 75 74 70 75 74 20 62 79 20  eader output by 
0c60: 27 63 6f 6e 66 69 67 75 72 65 27 20 69 66 20 77  'configure' if w
0c70: 65 27 72 65 20 75 73 69 6e 67 20 74 68 65 0a 2a  e're using the.*
0c80: 2a 20 61 75 74 6f 63 6f 6e 66 2d 62 61 73 65 64  * autoconf-based
0c90: 20 62 75 69 6c 64 0a 2a 2f 0a 23 69 66 64 65 66   build.*/.#ifdef
0ca0: 20 5f 48 41 56 45 5f 53 51 4c 49 54 45 5f 43 4f   _HAVE_SQLITE_CO
0cb0: 4e 46 49 47 5f 48 0a 23 69 6e 63 6c 75 64 65 20  NFIG_H.#include 
0cc0: 22 63 6f 6e 66 69 67 2e 68 22 0a 23 65 6e 64 69  "config.h".#endi
0cd0: 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
0ce0: 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74  ** Include sqlit
0cf0: 65 4c 69 6d 69 74 2e 68 20 69 6e 20 74 68 65 20  eLimit.h in the 
0d00: 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65  middle of sqlite
0d10: 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
0d20: 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
0d30: 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71  ** Begin file sq
0d40: 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a  liteLimit.h ****
0d50: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0d60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0d70: 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61  */./*.** 2007 Ma
0d80: 79 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  y 7.**.** The au
0d90: 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
0da0: 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
0db0: 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
0dc0: 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
0dd0: 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
0de0: 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
0df0: 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
0e00: 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
0e10: 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
0e20: 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
0e30: 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
0e40: 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
0e50: 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
0e60: 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
0e70: 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
0e80: 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
0e90: 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
0ea0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0eb0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0ec0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0ed0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0ee0: 2a 2a 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 66  **.** .** This f
0ef0: 69 6c 65 20 64 65 66 69 6e 65 73 20 76 61 72 69  ile defines vari
0f00: 6f 75 73 20 6c 69 6d 69 74 73 20 6f 66 20 77 68  ous limits of wh
0f10: 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20 70 72  at SQLite can pr
0f20: 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23  ocess..**.** @(#
0f30: 29 20 24 49 64 3a 20 73 71 6c 69 74 65 4c 69 6d  ) $Id: sqliteLim
0f40: 69 74 2e 68 2c 76 20 31 2e 31 30 20 32 30 30 39  it.h,v 1.10 2009
0f50: 2f 30 31 2f 31 30 20 31 36 3a 31 35 3a 30 39 20  /01/10 16:15:09 
0f60: 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
0f70: 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  $.*/../*.** The 
0f80: 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f  maximum length o
0f90: 66 20 61 20 54 45 58 54 20 6f 72 20 42 4c 4f 42  f a TEXT or BLOB
0fa0: 20 69 6e 20 62 79 74 65 73 2e 20 20 20 54 68 69   in bytes.   Thi
0fb0: 73 20 61 6c 73 6f 0a 2a 2a 20 6c 69 6d 69 74 73  s also.** limits
0fc0: 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 20 72   the size of a r
0fd0: 6f 77 20 69 6e 20 61 20 74 61 62 6c 65 20 6f 72  ow in a table or
0fe0: 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68   index..**.** Th
0ff0: 65 20 68 61 72 64 20 6c 69 6d 69 74 20 69 73 20  e hard limit is 
1000: 74 68 65 20 61 62 69 6c 69 74 79 20 6f 66 20 61  the ability of a
1010: 20 33 32 2d 62 69 74 20 73 69 67 6e 65 64 20 69   32-bit signed i
1020: 6e 74 65 67 65 72 0a 2a 2a 20 74 6f 20 63 6f 75  nteger.** to cou
1030: 6e 74 20 74 68 65 20 73 69 7a 65 3a 20 32 5e 33  nt the size: 2^3
1040: 31 2d 31 20 6f 72 20 32 31 34 37 34 38 33 36 34  1-1 or 214748364
1050: 37 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  7..*/.#ifndef SQ
1060: 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a  LITE_MAX_LENGTH.
1070: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
1080: 4d 41 58 5f 4c 45 4e 47 54 48 20 31 30 30 30 30  MAX_LENGTH 10000
1090: 30 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a  00000.#endif../*
10a0: 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
10b0: 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f  maximum number o
10c0: 66 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c  f.**.**    * Col
10d0: 75 6d 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 0a  umns in a table.
10e0: 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 20  **    * Columns 
10f0: 69 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 20  in an index.**  
1100: 20 20 2a 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61    * Columns in a
1110: 20 76 69 65 77 0a 2a 2a 20 20 20 20 2a 20 54 65   view.**    * Te
1120: 72 6d 73 20 69 6e 20 74 68 65 20 53 45 54 20 63  rms in the SET c
1130: 6c 61 75 73 65 20 6f 66 20 61 6e 20 55 50 44 41  lause of an UPDA
1140: 54 45 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20  TE statement.** 
1150: 20 20 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68     * Terms in th
1160: 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  e result set of 
1170: 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
1180: 6e 74 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73  nt.**    * Terms
1190: 20 69 6e 20 74 68 65 20 47 52 4f 55 50 20 42 59   in the GROUP BY
11a0: 20 6f 72 20 4f 52 44 45 52 20 42 59 20 63 6c 61   or ORDER BY cla
11b0: 75 73 65 73 20 6f 66 20 61 20 53 45 4c 45 43 54  uses of a SELECT
11c0: 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 20   statement..**  
11d0: 20 20 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65    * Terms in the
11e0: 20 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20 6f   VALUES clause o
11f0: 66 20 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74  f an INSERT stat
1200: 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ement.**.** The 
1210: 68 61 72 64 20 75 70 70 65 72 20 6c 69 6d 69 74  hard upper limit
1220: 20 68 65 72 65 20 69 73 20 33 32 36 37 36 2e 20   here is 32676. 
1230: 20 4d 6f 73 74 20 64 61 74 61 62 61 73 65 20 70   Most database p
1240: 65 6f 70 6c 65 20 77 69 6c 6c 0a 2a 2a 20 74 65  eople will.** te
1250: 6c 6c 20 79 6f 75 20 74 68 61 74 20 69 6e 20 61  ll you that in a
1260: 20 77 65 6c 6c 2d 6e 6f 72 6d 61 6c 69 7a 65 64   well-normalized
1270: 20 64 61 74 61 62 61 73 65 2c 20 79 6f 75 20 75   database, you u
1280: 73 75 61 6c 6c 79 20 73 68 6f 75 6c 64 0a 2a 2a  sually should.**
1290: 20 6e 6f 74 20 68 61 76 65 20 6d 6f 72 65 20 74   not have more t
12a0: 68 61 6e 20 61 20 64 6f 7a 65 6e 20 6f 72 20 73  han a dozen or s
12b0: 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 6e 79  o columns in any
12c0: 20 74 61 62 6c 65 2e 20 20 41 6e 64 20 69 66 0a   table.  And if.
12d0: 2a 2a 20 74 68 61 74 20 69 73 20 74 68 65 20 63  ** that is the c
12e0: 61 73 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f  ase, there is no
12f0: 20 70 6f 69 6e 74 20 69 6e 20 68 61 76 69 6e 67   point in having
1300: 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 66 65 77   more than a few
1310: 0a 2a 2a 20 64 6f 7a 65 6e 20 76 61 6c 75 65 73  .** dozen values
1320: 20 69 6e 20 61 6e 79 20 6f 66 20 74 68 65 20 6f   in any of the o
1330: 74 68 65 72 20 73 69 74 75 61 74 69 6f 6e 73 20  ther situations 
1340: 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65 2e  described above.
1350: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
1360: 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 23 20  TE_MAX_COLUMN.# 
1370: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
1380: 58 5f 43 4f 4c 55 4d 4e 20 32 30 30 30 0a 23 65  X_COLUMN 2000.#e
1390: 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
13a0: 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f  maximum length o
13b0: 66 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73  f a single SQL s
13c0: 74 61 74 65 6d 65 6e 74 20 69 6e 20 62 79 74 65  tatement in byte
13d0: 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 75 73 65 64  s..**.** It used
13e0: 20 74 6f 20 62 65 20 74 68 65 20 63 61 73 65 20   to be the case 
13f0: 74 68 61 74 20 73 65 74 74 69 6e 67 20 74 68 69  that setting thi
1400: 73 20 76 61 6c 75 65 20 74 6f 20 7a 65 72 6f 20  s value to zero 
1410: 77 6f 75 6c 64 0a 2a 2a 20 74 75 72 6e 20 74 68  would.** turn th
1420: 65 20 6c 69 6d 69 74 20 6f 66 66 2e 20 20 54 68  e limit off.  Th
1430: 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20  at is no longer 
1440: 74 72 75 65 2e 20 20 49 74 20 69 73 20 6e 6f 74  true.  It is not
1450: 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 74 6f 20   possible.** to 
1460: 74 75 72 6e 20 74 68 69 73 20 6c 69 6d 69 74 20  turn this limit 
1470: 6f 66 66 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  off..*/.#ifndef 
1480: 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c  SQLITE_MAX_SQL_L
1490: 45 4e 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53  ENGTH.# define S
14a0: 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45  QLITE_MAX_SQL_LE
14b0: 4e 47 54 48 20 31 30 30 30 30 30 30 30 30 30 0a  NGTH 1000000000.
14c0: 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
14d0: 65 20 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20  e maximum depth 
14e0: 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  of an expression
14f0: 20 74 72 65 65 2e 20 54 68 69 73 20 69 73 20 6c   tree. This is l
1500: 69 6d 69 74 65 64 20 74 6f 20 0a 2a 2a 20 73 6f  imited to .** so
1510: 6d 65 20 65 78 74 65 6e 74 20 62 79 20 53 51 4c  me extent by SQL
1520: 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47  ITE_MAX_SQL_LENG
1530: 54 48 2e 20 42 75 74 20 73 6f 6d 65 74 69 6d 65  TH. But sometime
1540: 20 79 6f 75 20 6d 69 67 68 74 20 0a 2a 2a 20 77   you might .** w
1550: 61 6e 74 20 74 6f 20 70 6c 61 63 65 20 6d 6f 72  ant to place mor
1560: 65 20 73 65 76 65 72 65 20 6c 69 6d 69 74 73 20  e severe limits 
1570: 6f 6e 20 74 68 65 20 63 6f 6d 70 6c 65 78 69 74  on the complexit
1580: 79 20 6f 66 20 61 6e 20 0a 2a 2a 20 65 78 70 72  y of an .** expr
1590: 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 20  ession..**.** A 
15a0: 76 61 6c 75 65 20 6f 66 20 30 20 75 73 65 64 20  value of 0 used 
15b0: 74 6f 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65  to mean that the
15c0: 20 6c 69 6d 69 74 20 77 61 73 20 6e 6f 74 20 65   limit was not e
15d0: 6e 66 6f 72 63 65 64 2e 0a 2a 2a 20 42 75 74 20  nforced..** But 
15e0: 74 68 61 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65  that is no longe
15f0: 72 20 74 72 75 65 2e 20 20 54 68 65 20 6c 69 6d  r true.  The lim
1600: 69 74 20 69 73 20 6e 6f 77 20 73 74 72 69 63 74  it is now strict
1610: 6c 79 20 65 6e 66 6f 72 63 65 64 0a 2a 2a 20 61  ly enforced.** a
1620: 74 20 61 6c 6c 20 74 69 6d 65 73 2e 0a 2a 2f 0a  t all times..*/.
1630: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
1640: 41 58 5f 45 58 50 52 5f 44 45 50 54 48 0a 23 20  AX_EXPR_DEPTH.# 
1650: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
1660: 58 5f 45 58 50 52 5f 44 45 50 54 48 20 31 30 30  X_EXPR_DEPTH 100
1670: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
1680: 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62  The maximum numb
1690: 65 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 61  er of terms in a
16a0: 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54   compound SELECT
16b0: 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 54   statement..** T
16c0: 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f  he code generato
16d0: 72 20 66 6f 72 20 63 6f 6d 70 6f 75 6e 64 20 53  r for compound S
16e0: 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73  ELECT statements
16f0: 20 64 6f 65 73 20 6f 6e 65 0a 2a 2a 20 6c 65 76   does one.** lev
1700: 65 6c 20 6f 66 20 72 65 63 75 72 73 69 6f 6e 20  el of recursion 
1710: 66 6f 72 20 65 61 63 68 20 74 65 72 6d 2e 20 20  for each term.  
1720: 41 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77  A stack overflow
1730: 20 63 61 6e 20 72 65 73 75 6c 74 0a 2a 2a 20 69   can result.** i
1740: 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  f the number of 
1750: 74 65 72 6d 73 20 69 73 20 74 6f 6f 20 6c 61 72  terms is too lar
1760: 67 65 2e 20 20 49 6e 20 70 72 61 63 74 69 63 65  ge.  In practice
1770: 2c 20 6d 6f 73 74 20 53 51 4c 0a 2a 2a 20 6e 65  , most SQL.** ne
1780: 76 65 72 20 68 61 73 20 6d 6f 72 65 20 74 68 61  ver has more tha
1790: 6e 20 33 20 6f 72 20 34 20 74 65 72 6d 73 2e 20  n 3 or 4 terms. 
17a0: 20 55 73 65 20 61 20 76 61 6c 75 65 20 6f 66 20   Use a value of 
17b0: 30 20 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20  0 to disable.** 
17c0: 61 6e 79 20 6c 69 6d 69 74 20 6f 6e 20 74 68 65  any limit on the
17d0: 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73   number of terms
17e0: 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 74 20 53   in a compount S
17f0: 45 4c 45 43 54 2e 0a 2a 2f 0a 23 69 66 6e 64 65  ELECT..*/.#ifnde
1800: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d  f SQLITE_MAX_COM
1810: 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a 23 20 64  POUND_SELECT.# d
1820: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58  efine SQLITE_MAX
1830: 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
1840: 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a   500.#endif../*.
1850: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e  ** The maximum n
1860: 75 6d 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 73  umber of opcodes
1870: 20 69 6e 20 61 20 56 44 42 45 20 70 72 6f 67 72   in a VDBE progr
1880: 61 6d 2e 0a 2a 2a 20 4e 6f 74 20 63 75 72 72 65  am..** Not curre
1890: 6e 74 6c 79 20 65 6e 66 6f 72 63 65 64 2e 0a 2a  ntly enforced..*
18a0: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
18b0: 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 0a 23 20 64  _MAX_VDBE_OP.# d
18c0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58  efine SQLITE_MAX
18d0: 5f 56 44 42 45 5f 4f 50 20 32 35 30 30 30 0a 23  _VDBE_OP 25000.#
18e0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
18f0: 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
1900: 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  of arguments to 
1910: 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e  an SQL function.
1920: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
1930: 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f  TE_MAX_FUNCTION_
1940: 41 52 47 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  ARG.# define SQL
1950: 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e  ITE_MAX_FUNCTION
1960: 5f 41 52 47 20 31 32 37 0a 23 65 6e 64 69 66 0a  _ARG 127.#endif.
1970: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d  ./*.** The maxim
1980: 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d  um number of in-
1990: 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 74 6f 20  memory pages to 
19a0: 75 73 65 20 66 6f 72 20 74 68 65 20 6d 61 69 6e  use for the main
19b0: 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74 61 62   database.** tab
19c0: 6c 65 20 61 6e 64 20 66 6f 72 20 74 65 6d 70 6f  le and for tempo
19d0: 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 54 68  rary tables.  Th
19e0: 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  e SQLITE_DEFAULT
19f0: 5f 43 41 43 48 45 5f 53 49 5a 45 0a 2a 2f 0a 23  _CACHE_SIZE.*/.#
1a00: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45  ifndef SQLITE_DE
1a10: 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45  FAULT_CACHE_SIZE
1a20: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
1a30: 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53  _DEFAULT_CACHE_S
1a40: 49 5a 45 20 20 32 30 30 30 0a 23 65 6e 64 69 66  IZE  2000.#endif
1a50: 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1a60: 44 45 46 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43  DEFAULT_TEMP_CAC
1a70: 48 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65  HE_SIZE.# define
1a80: 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
1a90: 54 45 4d 50 5f 43 41 43 48 45 5f 53 49 5a 45 20  TEMP_CACHE_SIZE 
1aa0: 20 35 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a   500.#endif../*.
1ab0: 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e  ** The maximum n
1ac0: 75 6d 62 65 72 20 6f 66 20 61 74 74 61 63 68 65  umber of attache
1ad0: 64 20 64 61 74 61 62 61 73 65 73 2e 20 20 54 68  d databases.  Th
1ae0: 69 73 20 6d 75 73 74 20 62 65 20 62 65 74 77 65  is must be betwe
1af0: 65 6e 20 30 0a 2a 2a 20 61 6e 64 20 33 30 2e 20  en 0.** and 30. 
1b00: 20 54 68 65 20 75 70 70 65 72 20 62 6f 75 6e 64   The upper bound
1b10: 20 6f 6e 20 33 30 20 69 73 20 62 65 63 61 75 73   on 30 is becaus
1b20: 65 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67  e a 32-bit integ
1b30: 65 72 20 62 69 74 6d 61 70 0a 2a 2a 20 69 73 20  er bitmap.** is 
1b40: 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20  used internally 
1b50: 74 6f 20 74 72 61 63 6b 20 61 74 74 61 63 68 65  to track attache
1b60: 64 20 64 61 74 61 62 61 73 65 73 2e 0a 2a 2f 0a  d databases..*/.
1b70: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
1b80: 41 58 5f 41 54 54 41 43 48 45 44 0a 23 20 64 65  AX_ATTACHED.# de
1b90: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
1ba0: 41 54 54 41 43 48 45 44 20 31 30 0a 23 65 6e 64  ATTACHED 10.#end
1bb0: 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d  if.../*.** The m
1bc0: 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66 20  aximum value of 
1bd0: 61 20 3f 6e 6e 6e 20 77 69 6c 64 63 61 72 64 20  a ?nnn wildcard 
1be0: 74 68 61 74 20 74 68 65 20 70 61 72 73 65 72 20  that the parser 
1bf0: 77 69 6c 6c 20 61 63 63 65 70 74 2e 0a 2a 2f 0a  will accept..*/.
1c00: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d  #ifndef SQLITE_M
1c10: 41 58 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42  AX_VARIABLE_NUMB
1c20: 45 52 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  ER.# define SQLI
1c30: 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f  TE_MAX_VARIABLE_
1c40: 4e 55 4d 42 45 52 20 39 39 39 0a 23 65 6e 64 69  NUMBER 999.#endi
1c50: 66 0a 0a 2f 2a 20 4d 61 78 69 6d 75 6d 20 70 61  f../* Maximum pa
1c60: 67 65 20 73 69 7a 65 2e 20 20 54 68 65 20 75 70  ge size.  The up
1c70: 70 65 72 20 62 6f 75 6e 64 20 6f 6e 20 74 68 69  per bound on thi
1c80: 73 20 76 61 6c 75 65 20 69 73 20 33 32 37 36 38  s value is 32768
1c90: 2e 20 20 54 68 69 73 20 61 20 6c 69 6d 69 74 0a  .  This a limit.
1ca0: 2a 2a 20 69 6d 70 6f 73 65 64 20 62 79 20 74 68  ** imposed by th
1cb0: 65 20 6e 65 63 65 73 73 69 74 79 20 6f 66 20 73  e necessity of s
1cc0: 74 6f 72 69 6e 67 20 74 68 65 20 76 61 6c 75 65  toring the value
1cd0: 20 69 6e 20 61 20 32 2d 62 79 74 65 20 75 6e 73   in a 2-byte uns
1ce0: 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a  igned integer.**
1cf0: 20 61 6e 64 20 74 68 65 20 66 61 63 74 20 74 68   and the fact th
1d00: 61 74 20 74 68 65 20 70 61 67 65 20 73 69 7a 65  at the page size
1d10: 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72   must be a power
1d20: 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   of 2..**.** If 
1d30: 74 68 69 73 20 6c 69 6d 69 74 20 69 73 20 63 68  this limit is ch
1d40: 61 6e 67 65 64 2c 20 74 68 65 6e 20 74 68 65 20  anged, then the 
1d50: 63 6f 6d 70 69 6c 65 64 20 6c 69 62 72 61 72 79  compiled library
1d60: 20 69 73 20 74 65 63 68 6e 69 63 61 6c 6c 79 0a   is technically.
1d70: 2a 2a 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20  ** incompatible 
1d80: 77 69 74 68 20 61 6e 20 53 51 4c 69 74 65 20 6c  with an SQLite l
1d90: 69 62 72 61 72 79 20 63 6f 6d 70 69 6c 65 64 20  ibrary compiled 
1da0: 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e 74  with a different
1db0: 20 6c 69 6d 69 74 2e 20 49 66 0a 2a 2a 20 61 20   limit. If.** a 
1dc0: 70 72 6f 63 65 73 73 20 6f 70 65 72 61 74 69 6e  process operatin
1dd0: 67 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20  g on a database 
1de0: 77 69 74 68 20 61 20 70 61 67 65 2d 73 69 7a 65  with a page-size
1df0: 20 6f 66 20 36 35 35 33 36 20 62 79 74 65 73 20   of 65536 bytes 
1e00: 0a 2a 2a 20 63 72 61 73 68 65 73 2c 20 74 68 65  .** crashes, the
1e10: 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  n an instance of
1e20: 20 53 51 4c 69 74 65 20 63 6f 6d 70 69 6c 65 64   SQLite compiled
1e30: 20 77 69 74 68 20 74 68 65 20 64 65 66 61 75 6c   with the defaul
1e40: 74 20 70 61 67 65 2d 73 69 7a 65 20 0a 2a 2a 20  t page-size .** 
1e50: 6c 69 6d 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62  limit will not b
1e60: 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61  e able to rollba
1e70: 63 6b 20 74 68 65 20 61 62 6f 72 74 65 64 20 74  ck the aborted t
1e80: 72 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73  ransaction. This
1e90: 20 63 6f 75 6c 64 0a 2a 2a 20 6c 65 61 64 20 74   could.** lead t
1ea0: 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75  o database corru
1eb0: 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65  ption..*/.#ifnde
1ec0: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47  f SQLITE_MAX_PAG
1ed0: 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20  E_SIZE.# define 
1ee0: 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f  SQLITE_MAX_PAGE_
1ef0: 53 49 5a 45 20 33 32 37 36 38 0a 23 65 6e 64 69  SIZE 32768.#endi
1f00: 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 64 65  f.../*.** The de
1f10: 66 61 75 6c 74 20 73 69 7a 65 20 6f 66 20 61 20  fault size of a 
1f20: 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 0a 2a  database page..*
1f30: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1f40: 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49  _DEFAULT_PAGE_SI
1f50: 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  ZE.# define SQLI
1f60: 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  TE_DEFAULT_PAGE_
1f70: 53 49 5a 45 20 31 30 32 34 0a 23 65 6e 64 69 66  SIZE 1024.#endif
1f80: 0a 23 69 66 20 53 51 4c 49 54 45 5f 44 45 46 41  .#if SQLITE_DEFA
1f90: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3e 53 51  ULT_PAGE_SIZE>SQ
1fa0: 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49  LITE_MAX_PAGE_SI
1fb0: 5a 45 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54  ZE.# undef SQLIT
1fc0: 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53  E_DEFAULT_PAGE_S
1fd0: 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  IZE.# define SQL
1fe0: 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47 45  ITE_DEFAULT_PAGE
1ff0: 5f 53 49 5a 45 20 53 51 4c 49 54 45 5f 4d 41 58  _SIZE SQLITE_MAX
2000: 5f 50 41 47 45 5f 53 49 5a 45 0a 23 65 6e 64 69  _PAGE_SIZE.#endi
2010: 66 0a 0a 2f 2a 0a 2a 2a 20 4f 72 64 69 6e 61 72  f../*.** Ordinar
2020: 69 6c 79 2c 20 69 66 20 6e 6f 20 76 61 6c 75 65  ily, if no value
2030: 20 69 73 20 65 78 70 6c 69 63 69 74 6c 79 20 70   is explicitly p
2040: 72 6f 76 69 64 65 64 2c 20 53 51 4c 69 74 65 20  rovided, SQLite 
2050: 63 72 65 61 74 65 73 20 64 61 74 61 62 61 73 65  creates database
2060: 73 0a 2a 2a 20 77 69 74 68 20 70 61 67 65 20 73  s.** with page s
2070: 69 7a 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55  ize SQLITE_DEFAU
2080: 4c 54 5f 50 41 47 45 5f 53 49 5a 45 2e 20 48 6f  LT_PAGE_SIZE. Ho
2090: 77 65 76 65 72 2c 20 62 61 73 65 64 20 6f 6e 20  wever, based on 
20a0: 63 65 72 74 61 69 6e 0a 2a 2a 20 64 65 76 69 63  certain.** devic
20b0: 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63  e characteristic
20c0: 73 20 28 73 65 63 74 6f 72 2d 73 69 7a 65 20 61  s (sector-size a
20d0: 6e 64 20 61 74 6f 6d 69 63 20 77 72 69 74 65 28  nd atomic write(
20e0: 29 20 73 75 70 70 6f 72 74 29 2c 0a 2a 2a 20 53  ) support),.** S
20f0: 51 4c 69 74 65 20 6d 61 79 20 63 68 6f 6f 73 65  QLite may choose
2100: 20 61 20 6c 61 72 67 65 72 20 76 61 6c 75 65 2e   a larger value.
2110: 20 54 68 69 73 20 63 6f 6e 73 74 61 6e 74 20 69   This constant i
2120: 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61  s the maximum va
2130: 6c 75 65 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69  lue.** SQLite wi
2140: 6c 6c 20 63 68 6f 6f 73 65 20 6f 6e 20 69 74 73  ll choose on its
2150: 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66   own..*/.#ifndef
2160: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41   SQLITE_MAX_DEFA
2170: 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20  ULT_PAGE_SIZE.# 
2180: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
2190: 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53  X_DEFAULT_PAGE_S
21a0: 49 5a 45 20 38 31 39 32 0a 23 65 6e 64 69 66 0a  IZE 8192.#endif.
21b0: 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44  #if SQLITE_MAX_D
21c0: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
21d0: 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45  >SQLITE_MAX_PAGE
21e0: 5f 53 49 5a 45 0a 23 20 75 6e 64 65 66 20 53 51  _SIZE.# undef SQ
21f0: 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54  LITE_MAX_DEFAULT
2200: 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66  _PAGE_SIZE.# def
2210: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44  ine SQLITE_MAX_D
2220: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
2230: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45   SQLITE_MAX_PAGE
2240: 5f 53 49 5a 45 0a 23 65 6e 64 69 66 0a 0a 0a 2f  _SIZE.#endif.../
2250: 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d  *.** Maximum num
2260: 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
2270: 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  one database fil
2280: 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73  e..**.** This is
2290: 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 74 68 65   really just the
22a0: 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66   default value f
22b0: 6f 72 20 74 68 65 20 6d 61 78 5f 70 61 67 65 5f  or the max_page_
22c0: 63 6f 75 6e 74 20 70 72 61 67 6d 61 2e 0a 2a 2a  count pragma..**
22d0: 20 54 68 69 73 20 76 61 6c 75 65 20 63 61 6e 20   This value can 
22e0: 62 65 20 6c 6f 77 65 72 65 64 20 28 6f 72 20 72  be lowered (or r
22f0: 61 69 73 65 64 29 20 61 74 20 72 75 6e 2d 74 69  aised) at run-ti
2300: 6d 65 20 75 73 69 6e 67 20 74 68 61 74 20 74 68  me using that th
2310: 65 0a 2a 2a 20 6d 61 78 5f 70 61 67 65 5f 63 6f  e.** max_page_co
2320: 75 6e 74 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69  unt macro..*/.#i
2330: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
2340: 5f 50 41 47 45 5f 43 4f 55 4e 54 0a 23 20 64 65  _PAGE_COUNT.# de
2350: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
2360: 50 41 47 45 5f 43 4f 55 4e 54 20 31 30 37 33 37  PAGE_COUNT 10737
2370: 34 31 38 32 33 0a 23 65 6e 64 69 66 0a 0a 2f 2a  41823.#endif../*
2380: 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67  .** Maximum leng
2390: 74 68 20 28 69 6e 20 62 79 74 65 73 29 20 6f 66  th (in bytes) of
23a0: 20 74 68 65 20 70 61 74 74 65 72 6e 20 69 6e 20   the pattern in 
23b0: 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 0a 2a  a LIKE or GLOB.*
23c0: 2a 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 23  * operator..*/.#
23d0: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41  ifndef SQLITE_MA
23e0: 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c  X_LIKE_PATTERN_L
23f0: 45 4e 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53  ENGTH.# define S
2400: 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50  QLITE_MAX_LIKE_P
2410: 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20 35 30  ATTERN_LENGTH 50
2420: 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  000.#endif../*.*
2430: 2a 20 4d 61 78 69 6d 75 6d 20 64 65 70 74 68 20  * Maximum depth 
2440: 6f 66 20 72 65 63 75 72 73 69 6f 6e 20 66 6f 72  of recursion for
2450: 20 74 72 69 67 67 65 72 73 2e 0a 2a 2a 0a 2a 2a   triggers..**.**
2460: 20 41 20 76 61 6c 75 65 20 6f 66 20 31 20 6d 65   A value of 1 me
2470: 61 6e 73 20 74 68 61 74 20 61 20 74 72 69 67 67  ans that a trigg
2480: 65 72 20 70 72 6f 67 72 61 6d 20 77 69 6c 6c 20  er program will 
2490: 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f 20 69  not be able to i
24a0: 74 73 65 6c 66 0a 2a 2a 20 66 69 72 65 20 61 6e  tself.** fire an
24b0: 79 20 74 72 69 67 67 65 72 73 2e 20 41 20 76 61  y triggers. A va
24c0: 6c 75 65 20 6f 66 20 30 20 6d 65 61 6e 73 20 74  lue of 0 means t
24d0: 68 61 74 20 6e 6f 20 74 72 69 67 67 65 72 20 70  hat no trigger p
24e0: 72 6f 67 72 61 6d 73 20 61 74 20 61 6c 6c 20 0a  rograms at all .
24f0: 2a 2a 20 6d 61 79 20 62 65 20 65 78 65 63 75 74  ** may be execut
2500: 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ed..*/.#ifndef S
2510: 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47 47 45  QLITE_MAX_TRIGGE
2520: 52 5f 44 45 50 54 48 0a 23 69 66 20 64 65 66 69  R_DEPTH.#if defi
2530: 6e 65 64 28 53 51 4c 49 54 45 5f 53 4d 41 4c 4c  ned(SQLITE_SMALL
2540: 5f 53 54 41 43 4b 29 0a 23 20 64 65 66 69 6e 65  _STACK).# define
2550: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54 52 49 47   SQLITE_MAX_TRIG
2560: 47 45 52 5f 44 45 50 54 48 20 31 30 0a 23 65 6c  GER_DEPTH 10.#el
2570: 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  se.# define SQLI
2580: 54 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44  TE_MAX_TRIGGER_D
2590: 45 50 54 48 20 31 30 30 30 0a 23 65 6e 64 69 66  EPTH 1000.#endif
25a0: 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
25b0: 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
25c0: 73 71 6c 69 74 65 4c 69 6d 69 74 2e 68 20 2a 2a  sqliteLimit.h **
25d0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25e0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25f0: 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
2600: 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
2610: 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
2620: 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49  t off in sqliteI
2630: 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
2640: 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 44 69 73  *******/../* Dis
2650: 61 62 6c 65 20 6e 75 69 73 61 6e 63 65 20 77 61  able nuisance wa
2660: 72 6e 69 6e 67 73 20 6f 6e 20 42 6f 72 6c 61 6e  rnings on Borlan
2670: 64 20 63 6f 6d 70 69 6c 65 72 73 20 2a 2f 0a 23  d compilers */.#
2680: 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 42 4f 52  if defined(__BOR
2690: 4c 41 4e 44 43 5f 5f 29 0a 23 70 72 61 67 6d 61  LANDC__).#pragma
26a0: 20 77 61 72 6e 20 2d 72 63 68 20 2f 2a 20 75 6e   warn -rch /* un
26b0: 72 65 61 63 68 61 62 6c 65 20 63 6f 64 65 20 2a  reachable code *
26c0: 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d  /.#pragma warn -
26d0: 63 63 63 20 2f 2a 20 43 6f 6e 64 69 74 69 6f 6e  ccc /* Condition
26e0: 20 69 73 20 61 6c 77 61 79 73 20 74 72 75 65 20   is always true 
26f0: 6f 72 20 66 61 6c 73 65 20 2a 2f 0a 23 70 72 61  or false */.#pra
2700: 67 6d 61 20 77 61 72 6e 20 2d 61 75 73 20 2f 2a  gma warn -aus /*
2710: 20 41 73 73 69 67 6e 65 64 20 76 61 6c 75 65 20   Assigned value 
2720: 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 2a 2f  is never used */
2730: 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 63  .#pragma warn -c
2740: 73 75 20 2f 2a 20 43 6f 6d 70 61 72 69 6e 67 20  su /* Comparing 
2750: 73 69 67 6e 65 64 20 61 6e 64 20 75 6e 73 69 67  signed and unsig
2760: 6e 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20 77  ned */.#pragma w
2770: 61 72 6e 20 2d 73 70 61 20 2f 2a 20 53 75 73 70  arn -spa /* Susp
2780: 69 63 69 6f 75 73 20 70 6f 69 6e 74 65 72 20 61  icious pointer a
2790: 72 69 74 68 6d 65 74 69 63 20 2a 2f 0a 23 65 6e  rithmetic */.#en
27a0: 64 69 66 0a 0a 2f 2a 20 4e 65 65 64 65 64 20 66  dif../* Needed f
27b0: 6f 72 20 76 61 72 69 6f 75 73 20 64 65 66 69 6e  or various defin
27c0: 69 74 69 6f 6e 73 2e 2e 2e 20 2a 2f 0a 23 69 66  itions... */.#if
27d0: 6e 64 65 66 20 5f 47 4e 55 5f 53 4f 55 52 43 45  ndef _GNU_SOURCE
27e0: 0a 23 20 64 65 66 69 6e 65 20 5f 47 4e 55 5f 53  .# define _GNU_S
27f0: 4f 55 52 43 45 0a 23 65 6e 64 69 66 0a 0a 2f 2a  OURCE.#endif../*
2800: 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 73 74 61 6e  .** Include stan
2810: 64 61 72 64 20 68 65 61 64 65 72 20 66 69 6c 65  dard header file
2820: 73 20 61 73 20 6e 65 63 65 73 73 61 72 79 0a 2a  s as necessary.*
2830: 2f 0a 23 69 66 64 65 66 20 48 41 56 45 5f 53 54  /.#ifdef HAVE_ST
2840: 44 49 4e 54 5f 48 0a 23 69 6e 63 6c 75 64 65 20  DINT_H.#include 
2850: 3c 73 74 64 69 6e 74 2e 68 3e 0a 23 65 6e 64 69  <stdint.h>.#endi
2860: 66 0a 23 69 66 64 65 66 20 48 41 56 45 5f 49 4e  f.#ifdef HAVE_IN
2870: 54 54 59 50 45 53 5f 48 0a 23 69 6e 63 6c 75 64  TTYPES_H.#includ
2880: 65 20 3c 69 6e 74 74 79 70 65 73 2e 68 3e 0a 23  e <inttypes.h>.#
2890: 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 53  endif..#define S
28a0: 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50  QLITE_INDEX_SAMP
28b0: 4c 45 53 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 54 68  LES 10../*.** Th
28c0: 69 73 20 6d 61 63 72 6f 20 69 73 20 75 73 65 64  is macro is used
28d0: 20 74 6f 20 22 68 69 64 65 22 20 73 6f 6d 65 20   to "hide" some 
28e0: 75 67 6c 69 6e 65 73 73 20 69 6e 20 63 61 73 74  ugliness in cast
28f0: 69 6e 67 20 61 6e 20 69 6e 74 0a 2a 2a 20 76 61  ing an int.** va
2900: 6c 75 65 20 74 6f 20 61 20 70 74 72 20 76 61 6c  lue to a ptr val
2910: 75 65 20 75 6e 64 65 72 20 74 68 65 20 4d 53 56  ue under the MSV
2920: 43 20 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c 65  C 64-bit compile
2930: 72 2e 20 20 20 43 61 73 74 69 6e 67 0a 2a 2a 20  r.   Casting.** 
2940: 6e 6f 6e 20 36 34 2d 62 69 74 20 76 61 6c 75 65  non 64-bit value
2950: 73 20 74 6f 20 70 74 72 20 74 79 70 65 73 20 72  s to ptr types r
2960: 65 73 75 6c 74 73 20 69 6e 20 61 20 22 68 61 72  esults in a "har
2970: 64 22 20 65 72 72 6f 72 20 77 69 74 68 20 0a 2a  d" error with .*
2980: 2a 20 74 68 65 20 4d 53 56 43 20 36 34 2d 62 69  * the MSVC 64-bi
2990: 74 20 63 6f 6d 70 69 6c 65 72 20 77 68 69 63 68  t compiler which
29a0: 20 74 68 69 73 20 61 74 74 65 6d 70 74 73 20 74   this attempts t
29b0: 6f 20 61 76 6f 69 64 2e 20 20 0a 2a 2a 0a 2a 2a  o avoid.  .**.**
29c0: 20 41 20 73 69 6d 70 6c 65 20 63 6f 6d 70 69 6c   A simple compil
29d0: 65 72 20 70 72 61 67 6d 61 20 6f 72 20 63 61 73  er pragma or cas
29e0: 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f  ting sequence co
29f0: 75 6c 64 20 6e 6f 74 20 62 65 20 66 6f 75 6e 64  uld not be found
2a00: 0a 2a 2a 20 74 6f 20 63 6f 72 72 65 63 74 20 74  .** to correct t
2a10: 68 69 73 20 69 6e 20 61 6c 6c 20 73 69 74 75 61  his in all situa
2a20: 74 69 6f 6e 73 2c 20 73 6f 20 74 68 69 73 20 6d  tions, so this m
2a30: 61 63 72 6f 20 77 61 73 20 69 6e 74 72 6f 64 75  acro was introdu
2a40: 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 63 6f  ced..**.** It co
2a50: 75 6c 64 20 62 65 20 61 72 67 75 65 64 20 74 68  uld be argued th
2a60: 61 74 20 74 68 65 20 69 6e 74 70 74 72 5f 74 20  at the intptr_t 
2a70: 74 79 70 65 20 63 6f 75 6c 64 20 62 65 20 75 73  type could be us
2a80: 65 64 20 69 6e 20 74 68 69 73 0a 2a 2a 20 63 61  ed in this.** ca
2a90: 73 65 2c 20 62 75 74 20 74 68 61 74 20 74 79 70  se, but that typ
2aa0: 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62  e is not availab
2ab0: 6c 65 20 6f 6e 20 61 6c 6c 20 63 6f 6d 70 69 6c  le on all compil
2ac0: 65 72 73 2c 20 6f 72 20 0a 2a 2a 20 72 65 71 75  ers, or .** requ
2ad0: 69 72 65 73 20 74 68 65 20 23 69 6e 63 6c 75 64  ires the #includ
2ae0: 65 20 6f 66 20 73 70 65 63 69 66 69 63 20 68 65  e of specific he
2af0: 61 64 65 72 73 20 77 68 69 63 68 20 64 69 66 66  aders which diff
2b00: 65 72 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 70  ers between.** p
2b10: 6c 61 74 66 6f 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20  latforms..**.** 
2b20: 54 69 63 6b 65 74 20 23 33 38 36 30 3a 20 20 54  Ticket #3860:  T
2b30: 68 65 20 6c 6c 76 6d 2d 67 63 63 2d 34 2e 32 20  he llvm-gcc-4.2 
2b40: 63 6f 6d 70 69 6c 65 72 20 66 72 6f 6d 20 41 70  compiler from Ap
2b50: 70 6c 65 20 63 68 6f 6b 65 73 20 6f 6e 0a 2a 2a  ple chokes on.**
2b60: 20 74 68 65 20 28 28 76 6f 69 64 2a 29 26 28 28   the ((void*)&((
2b70: 63 68 61 72 2a 29 30 29 5b 58 5d 29 20 63 6f 6e  char*)0)[X]) con
2b80: 73 74 72 75 63 74 2e 20 20 42 75 74 20 4d 53 56  struct.  But MSV
2b90: 43 20 63 68 6f 6b 65 73 20 6f 6e 20 28 28 76 6f  C chokes on ((vo
2ba0: 69 64 2a 29 28 58 29 29 2e 0a 2a 2a 20 53 6f 20  id*)(X))..** So 
2bb0: 77 65 20 68 61 76 65 20 74 6f 20 64 65 66 69 6e  we have to defin
2bc0: 65 20 74 68 65 20 6d 61 63 72 6f 73 20 69 6e 20  e the macros in 
2bd0: 64 69 66 66 65 72 65 6e 74 20 77 61 79 73 20 64  different ways d
2be0: 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 0a  epending on the.
2bf0: 2a 2a 20 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2f 0a  ** compiler..*/.
2c00: 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e  #if defined(__GN
2c10: 55 43 5f 5f 29 0a 23 20 69 66 20 64 65 66 69 6e  UC__).# if defin
2c20: 65 64 28 48 41 56 45 5f 53 54 44 49 4e 54 5f 48  ed(HAVE_STDINT_H
2c30: 29 0a 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c  ).#   define SQL
2c40: 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 58  ITE_INT_TO_PTR(X
2c50: 29 20 20 28 28 76 6f 69 64 2a 29 28 69 6e 74 70  )  ((void*)(intp
2c60: 74 72 5f 74 29 28 58 29 29 0a 23 20 20 20 64 65  tr_t)(X)).#   de
2c70: 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54 52 5f  fine SQLITE_PTR_
2c80: 54 4f 5f 49 4e 54 28 58 29 20 20 28 28 69 6e 74  TO_INT(X)  ((int
2c90: 29 28 69 6e 74 70 74 72 5f 74 29 28 58 29 29 0a  )(intptr_t)(X)).
2ca0: 23 20 65 6c 73 65 0a 23 20 20 20 64 65 66 69 6e  # else.#   defin
2cb0: 65 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f  e SQLITE_INT_TO_
2cc0: 50 54 52 28 58 29 20 20 28 28 76 6f 69 64 2a 29  PTR(X)  ((void*)
2cd0: 28 58 29 29 0a 23 20 20 20 64 65 66 69 6e 65 20  (X)).#   define 
2ce0: 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e  SQLITE_PTR_TO_IN
2cf0: 54 28 58 29 20 20 28 28 69 6e 74 29 28 58 29 29  T(X)  ((int)(X))
2d00: 0a 23 20 65 6e 64 69 66 0a 23 65 6c 73 65 0a 23  .# endif.#else.#
2d10: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49   define SQLITE_I
2d20: 4e 54 5f 54 4f 5f 50 54 52 28 58 29 20 20 20 28  NT_TO_PTR(X)   (
2d30: 28 76 6f 69 64 2a 29 26 28 28 63 68 61 72 2a 29  (void*)&((char*)
2d40: 30 29 5b 58 5d 29 0a 23 20 64 65 66 69 6e 65 20  0)[X]).# define 
2d50: 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e  SQLITE_PTR_TO_IN
2d60: 54 28 58 29 20 20 20 28 28 69 6e 74 29 28 28 28  T(X)   ((int)(((
2d70: 63 68 61 72 2a 29 58 29 2d 28 63 68 61 72 2a 29  char*)X)-(char*)
2d80: 30 29 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  0)).#endif.../*.
2d90: 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 54 48  ** The SQLITE_TH
2da0: 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 20 6d  READSAFE macro m
2db0: 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 61  ust be defined a
2dc0: 73 20 65 69 74 68 65 72 20 30 20 6f 72 20 31 2e  s either 0 or 1.
2dd0: 0a 2a 2a 20 4f 6c 64 65 72 20 76 65 72 73 69 6f  .** Older versio
2de0: 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 75 73 65  ns of SQLite use
2df0: 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 54 48  d an optional TH
2e00: 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 2e 0a  READSAFE macro..
2e10: 2a 2a 20 57 65 20 73 75 70 70 6f 72 74 20 74 68  ** We support th
2e20: 61 74 20 66 6f 72 20 6c 65 67 61 63 79 0a 2a 2f  at for legacy.*/
2e30: 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
2e40: 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 29  LITE_THREADSAFE)
2e50: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48 52  .#if defined(THR
2e60: 45 41 44 53 41 46 45 29 0a 23 20 64 65 66 69 6e  EADSAFE).# defin
2e70: 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  e SQLITE_THREADS
2e80: 41 46 45 20 54 48 52 45 41 44 53 41 46 45 0a 23  AFE THREADSAFE.#
2e90: 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51  else.# define SQ
2ea0: 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20  LITE_THREADSAFE 
2eb0: 31 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  1.#endif.#endif.
2ec0: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54  ./*.** The SQLIT
2ed0: 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41  E_DEFAULT_MEMSTA
2ee0: 54 55 53 20 6d 61 63 72 6f 20 6d 75 73 74 20 62  TUS macro must b
2ef0: 65 20 64 65 66 69 6e 65 64 20 61 73 20 65 69 74  e defined as eit
2f00: 68 65 72 20 30 20 6f 72 20 31 2e 0a 2a 2a 20 49  her 0 or 1..** I
2f10: 74 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65  t determines whe
2f20: 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
2f30: 66 65 61 74 75 72 65 73 20 72 65 6c 61 74 65 64  features related
2f40: 20 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 43   to .** SQLITE_C
2f50: 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20  ONFIG_MEMSTATUS 
2f60: 61 72 65 20 61 76 61 69 6c 61 62 6c 65 20 62 79  are available by
2f70: 20 64 65 66 61 75 6c 74 20 6f 72 20 6e 6f 74 2e   default or not.
2f80: 20 54 68 69 73 20 76 61 6c 75 65 20 63 61 6e 0a   This value can.
2f90: 2a 2a 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e  ** be overridden
2fa0: 20 61 74 20 72 75 6e 74 69 6d 65 20 75 73 69 6e   at runtime usin
2fb0: 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f  g the sqlite3_co
2fc0: 6e 66 69 67 28 29 20 41 50 49 2e 0a 2a 2f 0a 23  nfig() API..*/.#
2fd0: 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
2fe0: 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54  TE_DEFAULT_MEMST
2ff0: 41 54 55 53 29 0a 23 20 64 65 66 69 6e 65 20 53  ATUS).# define S
3000: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45  QLITE_DEFAULT_ME
3010: 4d 53 54 41 54 55 53 20 31 0a 23 65 6e 64 69 66  MSTATUS 1.#endif
3020: 0a 0a 2f 2a 0a 2a 2a 20 45 78 61 63 74 6c 79 20  ../*.** Exactly 
3030: 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  one of the follo
3040: 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 75 73 74  wing macros must
3050: 20 62 65 20 64 65 66 69 6e 65 64 20 69 6e 20 6f   be defined in o
3060: 72 64 65 72 20 74 6f 0a 2a 2a 20 73 70 65 63 69  rder to.** speci
3070: 66 79 20 77 68 69 63 68 20 6d 65 6d 6f 72 79 20  fy which memory 
3080: 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
3090: 73 74 65 6d 20 74 6f 20 75 73 65 2e 0a 2a 2a 0a  stem to use..**.
30a0: 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 53 59  **     SQLITE_SY
30b0: 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 20 20 20 20  STEM_MALLOC     
30c0: 20 20 20 20 20 2f 2f 20 55 73 65 20 6e 6f 72 6d       // Use norm
30d0: 61 6c 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63  al system malloc
30e0: 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  ().**     SQLITE
30f0: 5f 4d 45 4d 44 45 42 55 47 20 20 20 20 20 20 20  _MEMDEBUG       
3100: 20 20 20 20 20 20 20 20 2f 2f 20 44 65 62 75 67          // Debug
3110: 67 69 6e 67 20 76 65 72 73 69 6f 6e 20 6f 66 20  ging version of 
3120: 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 0a  system malloc().
3130: 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 45  **     SQLITE_ME
3140: 4d 4f 52 59 5f 53 49 5a 45 20 20 20 20 20 20 20  MORY_SIZE       
3150: 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c       // internal
3160: 20 61 6c 6c 6f 63 61 74 6f 72 20 23 31 0a 2a 2a   allocator #1.**
3170: 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 4d 41 50       SQLITE_MMAP
3180: 5f 48 45 41 50 5f 53 49 5a 45 20 20 20 20 20 20  _HEAP_SIZE      
3190: 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c 20 6d     // internal m
31a0: 6d 61 70 28 29 20 61 6c 6c 6f 63 61 74 6f 72 0a  map() allocator.
31b0: 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 50 4f  **     SQLITE_PO
31c0: 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 20  W2_MEMORY_SIZE  
31d0: 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c       // internal
31e0: 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61 6c   power-of-two al
31f0: 6c 6f 63 61 74 6f 72 0a 2a 2a 0a 2a 2a 20 49 66  locator.**.** If
3200: 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 61 62 6f   none of the abo
3210: 76 65 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20  ve are defined, 
3220: 74 68 65 6e 20 73 65 74 20 53 51 4c 49 54 45 5f  then set SQLITE_
3230: 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 61 73  SYSTEM_MALLOC as
3240: 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 2e  .** the default.
3250: 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
3260: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41  SQLITE_SYSTEM_MA
3270: 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51  LLOC)+defined(SQ
3280: 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c  LITE_MEMDEBUG)+\
3290: 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c  .    defined(SQL
32a0: 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29  ITE_MEMORY_SIZE)
32b0: 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  +defined(SQLITE_
32c0: 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b  MMAP_HEAP_SIZE)+
32d0: 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51  \.    defined(SQ
32e0: 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59  LITE_POW2_MEMORY
32f0: 5f 53 49 5a 45 29 3e 31 0a 23 20 65 72 72 6f 72  _SIZE)>1.# error
3300: 20 22 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 66   "At most one of
3310: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   the following c
3320: 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 63 6f 6e 66  ompile-time conf
3330: 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e  iguration option
3340: 73 5c 0a 20 69 73 20 61 6c 6c 6f 77 73 3a 20 53  s\. is allows: S
3350: 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c  QLITE_SYSTEM_MAL
3360: 4c 4f 43 2c 20 53 51 4c 49 54 45 5f 4d 45 4d 44  LOC, SQLITE_MEMD
3370: 45 42 55 47 2c 20 53 51 4c 49 54 45 5f 4d 45 4d  EBUG, SQLITE_MEM
3380: 4f 52 59 5f 53 49 5a 45 2c 5c 0a 20 53 51 4c 49  ORY_SIZE,\. SQLI
3390: 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a  TE_MMAP_HEAP_SIZ
33a0: 45 2c 20 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d  E, SQLITE_POW2_M
33b0: 45 4d 4f 52 59 5f 53 49 5a 45 22 0a 23 65 6e 64  EMORY_SIZE".#end
33c0: 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  if.#if defined(S
33d0: 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c  QLITE_SYSTEM_MAL
33e0: 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51 4c  LOC)+defined(SQL
33f0: 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c 0a  ITE_MEMDEBUG)+\.
3400: 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c 49      defined(SQLI
3410: 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 2b  TE_MEMORY_SIZE)+
3420: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d  defined(SQLITE_M
3430: 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b 5c  MAP_HEAP_SIZE)+\
3440: 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c  .    defined(SQL
3450: 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f  ITE_POW2_MEMORY_
3460: 53 49 5a 45 29 3d 3d 30 0a 23 20 64 65 66 69 6e  SIZE)==0.# defin
3470: 65 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f  e SQLITE_SYSTEM_
3480: 4d 41 4c 4c 4f 43 20 31 0a 23 65 6e 64 69 66 0a  MALLOC 1.#endif.
3490: 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45  ./*.** If SQLITE
34a0: 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d  _MALLOC_SOFT_LIM
34b0: 49 54 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20  IT is not zero, 
34c0: 74 68 65 6e 20 74 72 79 20 74 6f 20 6b 65 65 70  then try to keep
34d0: 20 74 68 65 0a 2a 2a 20 73 69 7a 65 73 20 6f 66   the.** sizes of
34e0: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
34f0: 6f 6e 73 20 62 65 6c 6f 77 20 74 68 69 73 20 76  ons below this v
3500: 61 6c 75 65 20 77 68 65 72 65 20 70 6f 73 73 69  alue where possi
3510: 62 6c 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66  ble..*/.#if !def
3520: 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 41 4c 4c  ined(SQLITE_MALL
3530: 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 29 0a 23  OC_SOFT_LIMIT).#
3540: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
3550: 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54  ALLOC_SOFT_LIMIT
3560: 20 31 30 32 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a   1024.#endif../*
3570: 0a 2a 2a 20 57 65 20 6e 65 65 64 20 74 6f 20 64  .** We need to d
3580: 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55  efine _XOPEN_SOU
3590: 52 43 45 20 61 73 20 66 6f 6c 6c 6f 77 73 20 69  RCE as follows i
35a0: 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e 61 62 6c  n order to enabl
35b0: 65 0a 2a 2a 20 72 65 63 75 72 73 69 76 65 20 6d  e.** recursive m
35c0: 75 74 65 78 65 73 20 6f 6e 20 6d 6f 73 74 20 55  utexes on most U
35d0: 6e 69 78 20 73 79 73 74 65 6d 73 2e 20 20 42 75  nix systems.  Bu
35e0: 74 20 4d 61 63 20 4f 53 20 58 20 69 73 20 64 69  t Mac OS X is di
35f0: 66 66 65 72 65 6e 74 2e 0a 2a 2a 20 54 68 65 20  fferent..** The 
3600: 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 64 65  _XOPEN_SOURCE de
3610: 66 69 6e 65 20 63 61 75 73 65 73 20 70 72 6f 62  fine causes prob
3620: 6c 65 6d 73 20 66 6f 72 20 4d 61 63 20 4f 53 20  lems for Mac OS 
3630: 58 20 77 65 20 61 72 65 20 74 6f 6c 64 2c 0a 2a  X we are told,.*
3640: 2a 20 73 6f 20 69 74 20 69 73 20 6f 6d 69 74 74  * so it is omitt
3650: 65 64 20 74 68 65 72 65 2e 20 20 53 65 65 20 74  ed there.  See t
3660: 69 63 6b 65 74 20 23 32 36 37 33 2e 0a 2a 2a 0a  icket #2673..**.
3670: 2a 2a 20 4c 61 74 65 72 20 77 65 20 6c 65 61 72  ** Later we lear
3680: 6e 20 74 68 61 74 20 5f 58 4f 50 45 4e 5f 53 4f  n that _XOPEN_SO
3690: 55 52 43 45 20 69 73 20 70 6f 6f 72 6c 79 20 6f  URCE is poorly o
36a0: 72 20 69 6e 63 6f 72 72 65 63 74 6c 79 0a 2a 2a  r incorrectly.**
36b0: 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 6f 6e 20   implemented on 
36c0: 73 6f 6d 65 20 73 79 73 74 65 6d 73 2e 20 20 53  some systems.  S
36d0: 6f 20 77 65 20 61 76 6f 69 64 20 64 65 66 69 6e  o we avoid defin
36e0: 69 6e 67 20 69 74 20 61 74 20 61 6c 6c 0a 2a 2a  ing it at all.**
36f0: 20 69 66 20 69 74 20 69 73 20 61 6c 72 65 61 64   if it is alread
3700: 79 20 64 65 66 69 6e 65 64 20 6f 72 20 69 66 20  y defined or if 
3710: 69 74 20 69 73 20 75 6e 6e 65 65 64 65 64 20 62  it is unneeded b
3720: 65 63 61 75 73 65 20 77 65 20 61 72 65 0a 2a 2a  ecause we are.**
3730: 20 6e 6f 74 20 64 6f 69 6e 67 20 61 20 74 68 72   not doing a thr
3740: 65 61 64 73 61 66 65 20 62 75 69 6c 64 2e 20 20  eadsafe build.  
3750: 54 69 63 6b 65 74 20 23 32 36 38 31 2e 0a 2a 2a  Ticket #2681..**
3760: 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 69 63  .** See also tic
3770: 6b 65 74 20 23 32 37 34 31 2e 0a 2a 2f 0a 23 69  ket #2741..*/.#i
3780: 66 20 21 64 65 66 69 6e 65 64 28 5f 58 4f 50 45  f !defined(_XOPE
3790: 4e 5f 53 4f 55 52 43 45 29 20 26 26 20 21 64 65  N_SOURCE) && !de
37a0: 66 69 6e 65 64 28 5f 5f 44 41 52 57 49 4e 5f 5f  fined(__DARWIN__
37b0: 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f 5f  ) && !defined(__
37c0: 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49  APPLE__) && SQLI
37d0: 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20  TE_THREADSAFE.# 
37e0: 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53   define _XOPEN_S
37f0: 4f 55 52 43 45 20 35 30 30 20 20 2f 2a 20 4e 65  OURCE 500  /* Ne
3800: 65 64 65 64 20 74 6f 20 65 6e 61 62 6c 65 20 70  eded to enable p
3810: 74 68 72 65 61 64 20 72 65 63 75 72 73 69 76 65  thread recursive
3820: 20 6d 75 74 65 78 65 73 20 2a 2f 0a 23 65 6e 64   mutexes */.#end
3830: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 43  if../*.** The TC
3840: 4c 20 68 65 61 64 65 72 73 20 61 72 65 20 6f 6e  L headers are on
3850: 6c 79 20 6e 65 65 64 65 64 20 77 68 65 6e 20 63  ly needed when c
3860: 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 54 43 4c  ompiling the TCL
3870: 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2f 0a 23 69   bindings..*/.#i
3880: 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
3890: 5f 54 43 4c 29 20 7c 7c 20 64 65 66 69 6e 65 64  _TCL) || defined
38a0: 28 54 43 4c 53 48 29 0a 23 20 69 6e 63 6c 75 64  (TCLSH).# includ
38b0: 65 20 3c 74 63 6c 2e 68 3e 0a 23 65 6e 64 69 66  e <tcl.h>.#endif
38c0: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e 79 20 70 65 6f  ../*.** Many peo
38d0: 70 6c 65 20 61 72 65 20 66 61 69 6c 69 6e 67 20  ple are failing 
38e0: 74 6f 20 73 65 74 20 2d 44 4e 44 45 42 55 47 3d  to set -DNDEBUG=
38f0: 31 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67  1 when compiling
3900: 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 53 65 74 74   SQLite..** Sett
3910: 69 6e 67 20 4e 44 45 42 55 47 20 6d 61 6b 65 73  ing NDEBUG makes
3920: 20 74 68 65 20 63 6f 64 65 20 73 6d 61 6c 6c 65   the code smalle
3930: 72 20 61 6e 64 20 72 75 6e 20 66 61 73 74 65 72  r and run faster
3940: 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c 6f 77  .  So the follow
3950: 69 6e 67 0a 2a 2a 20 6c 69 6e 65 73 20 61 72 65  ing.** lines are
3960: 20 61 64 64 65 64 20 74 6f 20 61 75 74 6f 6d 61   added to automa
3970: 74 69 63 61 6c 6c 79 20 73 65 74 20 4e 44 45 42  tically set NDEB
3980: 55 47 20 75 6e 6c 65 73 73 20 74 68 65 20 2d 44  UG unless the -D
3990: 53 51 4c 49 54 45 5f 44 45 42 55 47 3d 31 0a 2a  SQLITE_DEBUG=1.*
39a0: 2a 20 6f 70 74 69 6f 6e 20 69 73 20 73 65 74 2e  * option is set.
39b0: 20 20 54 68 75 73 20 4e 44 45 42 55 47 20 62 65    Thus NDEBUG be
39c0: 63 6f 6d 65 73 20 61 6e 20 6f 70 74 2d 69 6e 20  comes an opt-in 
39d0: 72 61 74 68 65 72 20 74 68 61 6e 20 61 6e 20 6f  rather than an o
39e0: 70 74 2d 6f 75 74 0a 2a 2a 20 66 65 61 74 75 72  pt-out.** featur
39f0: 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  e..*/.#if !defin
3a00: 65 64 28 4e 44 45 42 55 47 29 20 26 26 20 21 64  ed(NDEBUG) && !d
3a10: 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
3a20: 42 55 47 29 20 0a 23 20 64 65 66 69 6e 65 20 4e  BUG) .# define N
3a30: 44 45 42 55 47 20 31 0a 23 65 6e 64 69 66 0a 0a  DEBUG 1.#endif..
3a40: 2f 2a 0a 2a 2a 20 54 68 65 20 74 65 73 74 63 61  /*.** The testca
3a50: 73 65 28 29 20 6d 61 63 72 6f 20 69 73 20 75 73  se() macro is us
3a60: 65 64 20 74 6f 20 61 69 64 20 69 6e 20 63 6f 76  ed to aid in cov
3a70: 65 72 61 67 65 20 74 65 73 74 69 6e 67 2e 20 20  erage testing.  
3a80: 57 68 65 6e 20 0a 2a 2a 20 64 6f 69 6e 67 20 63  When .** doing c
3a90: 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 2c  overage testing,
3aa0: 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 69   the condition i
3ab0: 6e 73 69 64 65 20 74 68 65 20 61 72 67 75 6d 65  nside the argume
3ac0: 6e 74 20 74 6f 0a 2a 2a 20 74 65 73 74 63 61 73  nt to.** testcas
3ad0: 65 28 29 20 6d 75 73 74 20 62 65 20 65 76 61 6c  e() must be eval
3ae0: 75 61 74 65 64 20 62 6f 74 68 20 74 72 75 65 20  uated both true 
3af0: 61 6e 64 20 66 61 6c 73 65 20 69 6e 20 6f 72 64  and false in ord
3b00: 65 72 20 74 6f 0a 2a 2a 20 67 65 74 20 66 75 6c  er to.** get ful
3b10: 6c 20 62 72 61 6e 63 68 20 63 6f 76 65 72 61 67  l branch coverag
3b20: 65 2e 20 20 54 68 65 20 74 65 73 74 63 61 73 65  e.  The testcase
3b30: 28 29 20 6d 61 63 72 6f 20 69 73 20 69 6e 73 65  () macro is inse
3b40: 72 74 65 64 0a 2a 2a 20 74 6f 20 68 65 6c 70 20  rted.** to help 
3b50: 65 6e 73 75 72 65 20 61 64 65 71 75 61 74 65 20  ensure adequate 
3b60: 74 65 73 74 20 63 6f 76 65 72 61 67 65 20 69 6e  test coverage in
3b70: 20 70 6c 61 63 65 73 20 77 68 65 72 65 20 73 69   places where si
3b80: 6d 70 6c 65 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f  mple.** conditio
3b90: 6e 2f 64 65 63 69 73 69 6f 6e 20 63 6f 76 65 72  n/decision cover
3ba0: 61 67 65 20 69 73 20 69 6e 61 64 65 71 75 61 74  age is inadequat
3bb0: 65 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  e.  For example,
3bc0: 20 74 65 73 74 63 61 73 65 28 29 0a 2a 2a 20 63   testcase().** c
3bd0: 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6d 61  an be used to ma
3be0: 6b 65 20 73 75 72 65 20 62 6f 75 6e 64 61 72 79  ke sure boundary
3bf0: 20 76 61 6c 75 65 73 20 61 72 65 20 74 65 73 74   values are test
3c00: 65 64 2e 20 20 46 6f 72 0a 2a 2a 20 62 69 74 6d  ed.  For.** bitm
3c10: 61 73 6b 20 74 65 73 74 73 2c 20 74 65 73 74 63  ask tests, testc
3c20: 61 73 65 28 29 20 63 61 6e 20 62 65 20 75 73 65  ase() can be use
3c30: 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 65  d to make sure e
3c40: 61 63 68 20 62 69 74 0a 2a 2a 20 69 73 20 73 69  ach bit.** is si
3c50: 67 6e 69 66 69 63 61 6e 74 20 61 6e 64 20 75 73  gnificant and us
3c60: 65 64 20 61 74 20 6c 65 61 73 74 20 6f 6e 63 65  ed at least once
3c70: 2e 20 20 4f 6e 20 73 77 69 74 63 68 20 73 74 61  .  On switch sta
3c80: 74 65 6d 65 6e 74 73 0a 2a 2a 20 77 68 65 72 65  tements.** where
3c90: 20 6d 75 6c 74 69 70 6c 65 20 63 61 73 65 73 20   multiple cases 
3ca0: 67 6f 20 74 6f 20 74 68 65 20 73 61 6d 65 20 62  go to the same b
3cb0: 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 2c 20 74 65  lock of code, te
3cc0: 73 74 63 61 73 65 28 29 0a 2a 2a 20 63 61 6e 20  stcase().** can 
3cd0: 69 6e 73 75 72 65 20 74 68 61 74 20 61 6c 6c 20  insure that all 
3ce0: 63 61 73 65 73 20 61 72 65 20 65 76 61 6c 75 61  cases are evalua
3cf0: 74 65 64 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 64 65  ted..**.*/.#ifde
3d00: 66 20 53 51 4c 49 54 45 5f 43 4f 56 45 52 41 47  f SQLITE_COVERAG
3d10: 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52  E_TEST.SQLITE_PR
3d20: 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
3d30: 69 74 65 33 43 6f 76 65 72 61 67 65 28 69 6e 74  ite3Coverage(int
3d40: 29 3b 0a 23 20 64 65 66 69 6e 65 20 74 65 73 74  );.# define test
3d50: 63 61 73 65 28 58 29 20 20 69 66 28 20 58 20 29  case(X)  if( X )
3d60: 7b 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67  { sqlite3Coverag
3d70: 65 28 5f 5f 4c 49 4e 45 5f 5f 29 3b 20 7d 0a 23  e(__LINE__); }.#
3d80: 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 74 65  else.# define te
3d90: 73 74 63 61 73 65 28 58 29 0a 23 65 6e 64 69 66  stcase(X).#endif
3da0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 45 53 54  ../*.** The TEST
3db0: 4f 4e 4c 59 20 6d 61 63 72 6f 20 69 73 20 75 73  ONLY macro is us
3dc0: 65 64 20 74 6f 20 65 6e 63 6c 6f 73 65 20 76 61  ed to enclose va
3dd0: 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74 69  riable declarati
3de0: 6f 6e 73 20 6f 72 0a 2a 2a 20 6f 74 68 65 72 20  ons or.** other 
3df0: 62 69 74 73 20 6f 66 20 63 6f 64 65 20 74 68 61  bits of code tha
3e00: 74 20 61 72 65 20 6e 65 65 64 65 64 20 74 6f 20  t are needed to 
3e10: 73 75 70 70 6f 72 74 20 74 68 65 20 61 72 67 75  support the argu
3e20: 6d 65 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20  ments.** within 
3e30: 74 65 73 74 63 61 73 65 28 29 20 61 6e 64 20 61  testcase() and a
3e40: 73 73 65 72 74 28 29 20 6d 61 63 72 6f 73 2e 0a  ssert() macros..
3e50: 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  */.#if !defined(
3e60: 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69 6e  NDEBUG) || defin
3e70: 65 64 28 53 51 4c 49 54 45 5f 43 4f 56 45 52 41  ed(SQLITE_COVERA
3e80: 47 45 5f 54 45 53 54 29 0a 23 20 64 65 66 69 6e  GE_TEST).# defin
3e90: 65 20 54 45 53 54 4f 4e 4c 59 28 58 29 20 20 58  e TESTONLY(X)  X
3ea0: 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
3eb0: 54 45 53 54 4f 4e 4c 59 28 58 29 0a 23 65 6e 64  TESTONLY(X).#end
3ec0: 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74 69  if../*.** Someti
3ed0: 6d 65 73 20 77 65 20 6e 65 65 64 20 61 20 73 6d  mes we need a sm
3ee0: 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 63 6f  all amount of co
3ef0: 64 65 20 73 75 63 68 20 61 73 20 61 20 76 61 72  de such as a var
3f00: 69 61 62 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61  iable initializa
3f10: 74 69 6f 6e 0a 2a 2a 20 74 6f 20 73 65 74 75 70  tion.** to setup
3f20: 20 66 6f 72 20 61 20 6c 61 74 65 72 20 61 73 73   for a later ass
3f30: 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 2e  ert() statement.
3f40: 20 20 57 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74    We do not want
3f50: 20 74 68 69 73 20 63 6f 64 65 20 74 6f 0a 2a 2a   this code to.**
3f60: 20 61 70 70 65 61 72 20 77 68 65 6e 20 61 73 73   appear when ass
3f70: 65 72 74 28 29 20 69 73 20 64 69 73 61 62 6c 65  ert() is disable
3f80: 64 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  d.  The followin
3f90: 67 20 6d 61 63 72 6f 20 69 73 20 74 68 65 72 65  g macro is there
3fa0: 66 6f 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20  fore.** used to 
3fb0: 63 6f 6e 74 61 69 6e 20 74 68 61 74 20 73 65 74  contain that set
3fc0: 75 70 20 63 6f 64 65 2e 20 20 54 68 65 20 22 56  up code.  The "V
3fd0: 56 41 22 20 61 63 72 6f 6e 79 6d 20 73 74 61 6e  VA" acronym stan
3fe0: 64 73 20 66 6f 72 0a 2a 2a 20 22 56 65 72 69 66  ds for.** "Verif
3ff0: 69 63 61 74 69 6f 6e 2c 20 56 61 6c 69 64 61 74  ication, Validat
4000: 69 6f 6e 2c 20 61 6e 64 20 41 63 63 72 65 64 69  ion, and Accredi
4010: 74 61 74 69 6f 6e 22 2e 20 20 49 6e 20 6f 74 68  tation".  In oth
4020: 65 72 20 77 6f 72 64 73 2c 20 74 68 65 0a 2a 2a  er words, the.**
4030: 20 63 6f 64 65 20 77 69 74 68 69 6e 20 56 56 41   code within VVA
4040: 5f 4f 4e 4c 59 28 29 20 77 69 6c 6c 20 6f 6e 6c  _ONLY() will onl
4050: 79 20 72 75 6e 20 64 75 72 69 6e 67 20 76 65 72  y run during ver
4060: 69 66 69 63 61 74 69 6f 6e 20 70 72 6f 63 65 73  ification proces
4070: 73 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ses..*/.#ifndef 
4080: 4e 44 45 42 55 47 0a 23 20 64 65 66 69 6e 65 20  NDEBUG.# define 
4090: 56 56 41 5f 4f 4e 4c 59 28 58 29 20 20 58 0a 23  VVA_ONLY(X)  X.#
40a0: 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56 56  else.# define VV
40b0: 41 5f 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 69 66  A_ONLY(X).#endif
40c0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 41 4c 57 41  ../*.** The ALWA
40d0: 59 53 20 61 6e 64 20 4e 45 56 45 52 20 6d 61 63  YS and NEVER mac
40e0: 72 6f 73 20 73 75 72 72 6f 75 6e 64 20 62 6f 6f  ros surround boo
40f0: 6c 65 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 73  lean expressions
4100: 20 77 68 69 63 68 20 0a 2a 2a 20 61 72 65 20 69   which .** are i
4110: 6e 74 65 6e 64 65 64 20 74 6f 20 61 6c 77 61 79  ntended to alway
4120: 73 20 62 65 20 74 72 75 65 20 6f 72 20 66 61 6c  s be true or fal
4130: 73 65 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79  se, respectively
4140: 2e 20 20 53 75 63 68 0a 2a 2a 20 65 78 70 72 65  .  Such.** expre
4150: 73 73 69 6f 6e 73 20 63 6f 75 6c 64 20 62 65 20  ssions could be 
4160: 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65  omitted from the
4170: 20 63 6f 64 65 20 63 6f 6d 70 6c 65 74 65 6c 79   code completely
4180: 2e 20 20 42 75 74 20 74 68 65 79 0a 2a 2a 20 61  .  But they.** a
4190: 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 61  re included in a
41a0: 20 66 65 77 20 63 61 73 65 73 20 69 6e 20 6f 72   few cases in or
41b0: 64 65 72 20 74 6f 20 65 6e 68 61 6e 63 65 20 74  der to enhance t
41c0: 68 65 20 72 65 73 69 6c 69 65 6e 63 65 0a 2a 2a  he resilience.**
41d0: 20 6f 66 20 53 51 4c 69 74 65 20 74 6f 20 75 6e   of SQLite to un
41e0: 65 78 70 65 63 74 65 64 20 62 65 68 61 76 69 6f  expected behavio
41f0: 72 20 2d 20 74 6f 20 6d 61 6b 65 20 74 68 65 20  r - to make the 
4200: 63 6f 64 65 20 22 73 65 6c 66 2d 68 65 61 6c 69  code "self-heali
4210: 6e 67 22 0a 2a 2a 20 6f 72 20 22 64 75 63 74 69  ng".** or "ducti
4220: 6c 65 22 20 72 61 74 68 65 72 20 74 68 61 6e 20  le" rather than 
4230: 62 65 69 6e 67 20 22 62 72 69 74 74 6c 65 22 20  being "brittle" 
4240: 61 6e 64 20 63 72 61 73 68 69 6e 67 20 61 74 20  and crashing at 
4250: 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 68 69 6e  the first.** hin
4260: 74 20 6f 66 20 75 6e 70 6c 61 6e 6e 65 64 20 62  t of unplanned b
4270: 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49  ehavior..**.** I
4280: 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 41  n other words, A
4290: 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45 52 20  LWAYS and NEVER 
42a0: 61 72 65 20 61 64 64 65 64 20 66 6f 72 20 64 65  are added for de
42b0: 66 65 6e 73 69 76 65 20 63 6f 64 65 2e 0a 2a 2a  fensive code..**
42c0: 0a 2a 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20 63  .** When doing c
42d0: 6f 76 65 72 61 67 65 20 74 65 73 74 69 6e 67 20  overage testing 
42e0: 41 4c 57 41 59 53 20 61 6e 64 20 4e 45 56 45 52  ALWAYS and NEVER
42f0: 20 61 72 65 20 68 61 72 64 2d 63 6f 64 65 64 20   are hard-coded 
4300: 74 6f 0a 2a 2a 20 62 65 20 74 72 75 65 20 61 6e  to.** be true an
4310: 64 20 66 61 6c 73 65 20 73 6f 20 74 68 61 74 20  d false so that 
4320: 74 68 65 20 75 6e 72 65 61 63 68 61 62 6c 65 20  the unreachable 
4330: 63 6f 64 65 20 74 68 65 6e 20 73 70 65 63 69 66  code then specif
4340: 79 20 77 69 6c 6c 0a 2a 2a 20 6e 6f 74 20 62 65  y will.** not be
4350: 20 63 6f 75 6e 74 65 64 20 61 73 20 75 6e 74 65   counted as unte
4360: 73 74 65 64 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69  sted code..*/.#i
4370: 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
4380: 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 29 0a  _COVERAGE_TEST).
4390: 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28  # define ALWAYS(
43a0: 58 29 20 20 20 20 20 20 28 31 29 0a 23 20 64 65  X)      (1).# de
43b0: 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20  fine NEVER(X)   
43c0: 20 20 20 20 28 30 29 0a 23 65 6c 69 66 20 21 64      (0).#elif !d
43d0: 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 23  efined(NDEBUG).#
43e0: 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28 58   define ALWAYS(X
43f0: 29 20 20 20 20 20 20 28 28 58 29 3f 31 3a 28 61  )      ((X)?1:(a
4400: 73 73 65 72 74 28 30 29 2c 30 29 29 0a 23 20 64  ssert(0),0)).# d
4410: 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20  efine NEVER(X)  
4420: 20 20 20 20 20 28 28 58 29 3f 28 61 73 73 65 72       ((X)?(asser
4430: 74 28 30 29 2c 31 29 3a 30 29 0a 23 65 6c 73 65  t(0),1):0).#else
4440: 0a 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53  .# define ALWAYS
4450: 28 58 29 20 20 20 20 20 20 28 58 29 0a 23 20 64  (X)      (X).# d
4460: 65 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20  efine NEVER(X)  
4470: 20 20 20 20 20 28 58 29 0a 23 65 6e 64 69 66 0a       (X).#endif.
4480: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f  ./*.** The macro
4490: 20 75 6e 6c 69 6b 65 6c 79 28 29 20 69 73 20 61   unlikely() is a
44a0: 20 68 69 6e 74 20 74 68 61 74 20 73 75 72 72 6f   hint that surro
44b0: 75 6e 64 73 20 61 20 62 6f 6f 6c 65 61 6e 0a 2a  unds a boolean.*
44c0: 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  * expression tha
44d0: 74 20 69 73 20 75 73 75 61 6c 6c 79 20 66 61 6c  t is usually fal
44e0: 73 65 2e 20 20 4d 61 63 72 6f 20 6c 69 6b 65 6c  se.  Macro likel
44f0: 79 28 29 20 73 75 72 72 6f 75 6e 64 73 0a 2a 2a  y() surrounds.**
4500: 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65   a boolean expre
4510: 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 75 73  ssion that is us
4520: 75 61 6c 6c 79 20 74 72 75 65 2e 20 20 47 43 43  ually true.  GCC
4530: 20 69 73 20 61 62 6c 65 20 74 6f 0a 2a 2a 20 75   is able to.** u
4540: 73 65 20 74 68 65 73 65 20 68 69 6e 74 73 20 74  se these hints t
4550: 6f 20 67 65 6e 65 72 61 74 65 20 62 65 74 74 65  o generate bette
4560: 72 20 63 6f 64 65 2c 20 73 6f 6d 65 74 69 6d 65  r code, sometime
4570: 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65  s..*/.#if define
4580: 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 30  d(__GNUC__) && 0
4590: 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c 79  .# define likely
45a0: 28 58 29 20 20 20 20 5f 5f 62 75 69 6c 74 69 6e  (X)    __builtin
45b0: 5f 65 78 70 65 63 74 28 28 58 29 2c 31 29 0a 23  _expect((X),1).#
45c0: 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79   define unlikely
45d0: 28 58 29 20 20 5f 5f 62 75 69 6c 74 69 6e 5f 65  (X)  __builtin_e
45e0: 78 70 65 63 74 28 28 58 29 2c 30 29 0a 23 65 6c  xpect((X),0).#el
45f0: 73 65 0a 23 20 64 65 66 69 6e 65 20 6c 69 6b 65  se.# define like
4600: 6c 79 28 58 29 20 20 20 20 21 21 28 58 29 0a 23  ly(X)    !!(X).#
4610: 20 64 65 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79   define unlikely
4620: 28 58 29 20 20 21 21 28 58 29 0a 23 65 6e 64 69  (X)  !!(X).#endi
4630: 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
4640: 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74  ** Include sqlit
4650: 65 33 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  e3.h in the midd
4660: 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e  le of sqliteInt.
4670: 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
4680: 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
4690: 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71  ** Begin file sq
46a0: 6c 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  lite3.h ********
46b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46c0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46d0: 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
46e0: 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
46f0: 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
4700: 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
4710: 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
4720: 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
4730: 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
4740: 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
4750: 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
4760: 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
4770: 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
4780: 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
4790: 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
47a0: 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
47b0: 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
47c0: 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
47d0: 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
47e0: 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
47f0: 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
4800: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4810: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4820: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4830: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4840: 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
4850: 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65  s header file de
4860: 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66  fines the interf
4870: 61 63 65 20 74 68 61 74 20 74 68 65 20 53 51 4c  ace that the SQL
4880: 69 74 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 70  ite library.** p
4890: 72 65 73 65 6e 74 73 20 74 6f 20 63 6c 69 65 6e  resents to clien
48a0: 74 20 70 72 6f 67 72 61 6d 73 2e 20 20 49 66 20  t programs.  If 
48b0: 61 20 43 2d 66 75 6e 63 74 69 6f 6e 2c 20 73 74  a C-function, st
48c0: 72 75 63 74 75 72 65 2c 20 64 61 74 61 74 79 70  ructure, datatyp
48d0: 65 2c 0a 2a 2a 20 6f 72 20 63 6f 6e 73 74 61 6e  e,.** or constan
48e0: 74 20 64 65 66 69 6e 69 74 69 6f 6e 20 64 6f 65  t definition doe
48f0: 73 20 6e 6f 74 20 61 70 70 65 61 72 20 69 6e 20  s not appear in 
4900: 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 6e 20  this file, then 
4910: 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20 70  it is.** not a p
4920: 75 62 6c 69 73 68 65 64 20 41 50 49 20 6f 66 20  ublished API of 
4930: 53 51 4c 69 74 65 2c 20 69 73 20 73 75 62 6a 65  SQLite, is subje
4940: 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74  ct to change wit
4950: 68 6f 75 74 0a 2a 2a 20 6e 6f 74 69 63 65 2c 20  hout.** notice, 
4960: 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  and should not b
4970: 65 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 20  e referenced by 
4980: 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20 75 73  programs that us
4990: 65 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20  e SQLite..**.** 
49a0: 53 6f 6d 65 20 6f 66 20 74 68 65 20 64 65 66 69  Some of the defi
49b0: 6e 69 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65  nitions that are
49c0: 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 61 72   in this file ar
49d0: 65 20 6d 61 72 6b 65 64 20 61 73 0a 2a 2a 20 22  e marked as.** "
49e0: 65 78 70 65 72 69 6d 65 6e 74 61 6c 22 2e 20 20  experimental".  
49f0: 45 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74  Experimental int
4a00: 65 72 66 61 63 65 73 20 61 72 65 20 6e 6f 72 6d  erfaces are norm
4a10: 61 6c 6c 79 20 6e 65 77 0a 2a 2a 20 66 65 61 74  ally new.** feat
4a20: 75 72 65 73 20 72 65 63 65 6e 74 6c 79 20 61 64  ures recently ad
4a30: 64 65 64 20 74 6f 20 53 51 4c 69 74 65 2e 20 20  ded to SQLite.  
4a40: 57 65 20 64 6f 20 6e 6f 74 20 61 6e 74 69 63 69  We do not antici
4a50: 70 61 74 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20  pate changes.** 
4a60: 74 6f 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20  to experimental 
4a70: 69 6e 74 65 72 66 61 63 65 73 20 62 75 74 20 72  interfaces but r
4a80: 65 73 65 72 76 65 20 74 68 65 20 72 69 67 68 74  eserve the right
4a90: 20 74 6f 20 6d 61 6b 65 20 6d 69 6e 6f 72 20 63   to make minor c
4aa0: 68 61 6e 67 65 73 0a 2a 2a 20 69 66 20 65 78 70  hanges.** if exp
4ab0: 65 72 69 65 6e 63 65 20 66 72 6f 6d 20 75 73 65  erience from use
4ac0: 20 22 69 6e 20 74 68 65 20 77 69 6c 64 22 20 73   "in the wild" s
4ad0: 75 67 67 65 73 74 20 73 75 63 68 20 63 68 61 6e  uggest such chan
4ae0: 67 65 73 20 61 72 65 20 70 72 75 64 65 6e 74 2e  ges are prudent.
4af0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 66 66 69 63  .**.** The offic
4b00: 69 61 6c 20 43 2d 6c 61 6e 67 75 61 67 65 20 41  ial C-language A
4b10: 50 49 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  PI documentation
4b20: 20 66 6f 72 20 53 51 4c 69 74 65 20 69 73 20 64   for SQLite is d
4b30: 65 72 69 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 63  erived.** from c
4b40: 6f 6d 6d 65 6e 74 73 20 69 6e 20 74 68 69 73 20  omments in this 
4b50: 66 69 6c 65 2e 20 20 54 68 69 73 20 66 69 6c 65  file.  This file
4b60: 20 69 73 20 74 68 65 20 61 75 74 68 6f 72 69 74   is the authorit
4b70: 61 74 69 76 65 20 73 6f 75 72 63 65 0a 2a 2a 20  ative source.** 
4b80: 6f 6e 20 68 6f 77 20 53 51 4c 69 74 65 20 69 6e  on how SQLite in
4b90: 74 65 72 66 61 63 65 73 20 61 72 65 20 73 75 70  terfaces are sup
4ba0: 70 6f 73 65 20 74 6f 20 6f 70 65 72 61 74 65 2e  pose to operate.
4bb0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20  .**.** The name 
4bc0: 6f 66 20 74 68 69 73 20 66 69 6c 65 20 75 6e 64  of this file und
4bd0: 65 72 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  er configuration
4be0: 20 6d 61 6e 61 67 65 6d 65 6e 74 20 69 73 20 22   management is "
4bf0: 73 71 6c 69 74 65 2e 68 2e 69 6e 22 2e 0a 2a 2a  sqlite.h.in"..**
4c00: 20 54 68 65 20 6d 61 6b 65 66 69 6c 65 20 6d 61   The makefile ma
4c10: 6b 65 73 20 73 6f 6d 65 20 6d 69 6e 6f 72 20 63  kes some minor c
4c20: 68 61 6e 67 65 73 20 74 6f 20 74 68 69 73 20 66  hanges to this f
4c30: 69 6c 65 20 28 73 75 63 68 20 61 73 20 69 6e 73  ile (such as ins
4c40: 65 72 74 69 6e 67 0a 2a 2a 20 74 68 65 20 76 65  erting.** the ve
4c50: 72 73 69 6f 6e 20 6e 75 6d 62 65 72 29 20 61 6e  rsion number) an
4c60: 64 20 63 68 61 6e 67 65 73 20 69 74 73 20 6e 61  d changes its na
4c70: 6d 65 20 74 6f 20 22 73 71 6c 69 74 65 33 2e 68  me to "sqlite3.h
4c80: 22 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20  " as.** part of 
4c90: 74 68 65 20 62 75 69 6c 64 20 70 72 6f 63 65 73  the build proces
4ca0: 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53  s..*/.#ifndef _S
4cb0: 51 4c 49 54 45 33 5f 48 5f 0a 23 64 65 66 69 6e  QLITE3_H_.#defin
4cc0: 65 20 5f 53 51 4c 49 54 45 33 5f 48 5f 0a 23 69  e _SQLITE3_H_.#i
4cd0: 6e 63 6c 75 64 65 20 3c 73 74 64 61 72 67 2e 68  nclude <stdarg.h
4ce0: 3e 20 20 20 20 20 2f 2a 20 4e 65 65 64 65 64 20  >     /* Needed 
4cf0: 66 6f 72 20 74 68 65 20 64 65 66 69 6e 69 74 69  for the definiti
4d00: 6f 6e 20 6f 66 20 76 61 5f 6c 69 73 74 20 2a 2f  on of va_list */
4d10: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72  ../*.** Make sur
4d20: 65 20 77 65 20 63 61 6e 20 63 61 6c 6c 20 74 68  e we can call th
4d30: 69 73 20 73 74 75 66 66 20 66 72 6f 6d 20 43 2b  is stuff from C+
4d40: 2b 2e 0a 2a 2f 0a 23 69 66 20 30 0a 65 78 74 65  +..*/.#if 0.exte
4d50: 72 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 0a  rn "C" {.#endif.
4d60: 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20  ../*.** Add the 
4d70: 61 62 69 6c 69 74 79 20 74 6f 20 6f 76 65 72 72  ability to overr
4d80: 69 64 65 20 27 65 78 74 65 72 6e 27 0a 2a 2f 0a  ide 'extern'.*/.
4d90: 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 45  #ifndef SQLITE_E
4da0: 58 54 45 52 4e 0a 23 20 64 65 66 69 6e 65 20 53  XTERN.# define S
4db0: 51 4c 49 54 45 5f 45 58 54 45 52 4e 20 65 78 74  QLITE_EXTERN ext
4dc0: 65 72 6e 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  ern.#endif..#ifn
4dd0: 64 65 66 20 53 51 4c 49 54 45 5f 41 50 49 0a 23  def SQLITE_API.#
4de0: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41   define SQLITE_A
4df0: 50 49 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  PI.#endif.../*.*
4e00: 2a 20 54 68 65 73 65 20 6e 6f 2d 6f 70 20 6d 61  * These no-op ma
4e10: 63 72 6f 73 20 61 72 65 20 75 73 65 64 20 69 6e  cros are used in
4e20: 20 66 72 6f 6e 74 20 6f 66 20 69 6e 74 65 72 66   front of interf
4e30: 61 63 65 73 20 74 6f 20 6d 61 72 6b 20 74 68 6f  aces to mark tho
4e40: 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73  se.** interfaces
4e50: 20 61 73 20 65 69 74 68 65 72 20 64 65 70 72 65   as either depre
4e60: 63 61 74 65 64 20 6f 72 20 65 78 70 65 72 69 6d  cated or experim
4e70: 65 6e 74 61 6c 2e 20 20 4e 65 77 20 61 70 70 6c  ental.  New appl
4e80: 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75  ications.** shou
4e90: 6c 64 20 6e 6f 74 20 75 73 65 20 64 65 70 72 65  ld not use depre
4ea0: 63 61 74 65 64 20 69 6e 74 65 72 66 61 63 65 73  cated interfaces
4eb0: 20 2d 20 74 68 65 79 20 61 72 65 20 73 75 70 70   - they are supp
4ec0: 6f 72 74 20 66 6f 72 20 62 61 63 6b 77 61 72 64  ort for backward
4ed0: 73 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69  s.** compatibili
4ee0: 74 79 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63  ty only.  Applic
4ef0: 61 74 69 6f 6e 20 77 72 69 74 65 72 73 20 73 68  ation writers sh
4f00: 6f 75 6c 64 20 62 65 20 61 77 61 72 65 20 74 68  ould be aware th
4f10: 61 74 0a 2a 2a 20 65 78 70 65 72 69 6d 65 6e 74  at.** experiment
4f20: 61 6c 20 69 6e 74 65 72 66 61 63 65 73 20 61 72  al interfaces ar
4f30: 65 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61  e subject to cha
4f40: 6e 67 65 20 69 6e 20 70 6f 69 6e 74 20 72 65 6c  nge in point rel
4f50: 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  eases..**.** The
4f60: 73 65 20 6d 61 63 72 6f 73 20 75 73 65 64 20 74  se macros used t
4f70: 6f 20 72 65 73 6f 6c 76 65 20 74 6f 20 76 61 72  o resolve to var
4f80: 69 6f 75 73 20 6b 69 6e 64 73 20 6f 66 20 63 6f  ious kinds of co
4f90: 6d 70 69 6c 65 72 20 6d 61 67 69 63 20 74 68 61  mpiler magic tha
4fa0: 74 0a 2a 2a 20 77 6f 75 6c 64 20 67 65 6e 65 72  t.** would gener
4fb0: 61 74 65 20 77 61 72 6e 69 6e 67 20 6d 65 73 73  ate warning mess
4fc0: 61 67 65 73 20 77 68 65 6e 20 74 68 65 79 20 77  ages when they w
4fd0: 65 72 65 20 75 73 65 64 2e 20 20 42 75 74 20 74  ere used.  But t
4fe0: 68 61 74 0a 2a 2a 20 63 6f 6d 70 69 6c 65 72 20  hat.** compiler 
4ff0: 6d 61 67 69 63 20 65 6e 64 65 64 20 75 70 20 67  magic ended up g
5000: 65 6e 65 72 61 74 69 6e 67 20 73 75 63 68 20 61  enerating such a
5010: 20 66 6c 75 72 72 79 20 6f 66 20 62 75 67 20 72   flurry of bug r
5020: 65 70 6f 72 74 73 0a 2a 2a 20 74 68 61 74 20 77  eports.** that w
5030: 65 20 68 61 76 65 20 74 61 6b 65 6e 20 69 74 20  e have taken it 
5040: 61 6c 6c 20 6f 75 74 20 61 6e 64 20 67 6f 6e 65  all out and gone
5050: 20 62 61 63 6b 20 74 6f 20 75 73 69 6e 67 20 73   back to using s
5060: 69 6d 70 6c 65 0a 2a 2a 20 6e 6f 6f 70 20 6d 61  imple.** noop ma
5070: 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  cros..*/.#define
5080: 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54   SQLITE_DEPRECAT
5090: 45 44 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  ED.#define SQLIT
50a0: 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 0a  E_EXPERIMENTAL..
50b0: 2f 2a 0a 2a 2a 20 45 6e 73 75 72 65 20 74 68 65  /*.** Ensure the
50c0: 73 65 20 73 79 6d 62 6f 6c 73 20 77 65 72 65 20  se symbols were 
50d0: 6e 6f 74 20 64 65 66 69 6e 65 64 20 62 79 20 73  not defined by s
50e0: 6f 6d 65 20 70 72 65 76 69 6f 75 73 20 68 65 61  ome previous hea
50f0: 64 65 72 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66  der file..*/.#if
5100: 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49  def SQLITE_VERSI
5110: 4f 4e 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54  ON.# undef SQLIT
5120: 45 5f 56 45 52 53 49 4f 4e 0a 23 65 6e 64 69 66  E_VERSION.#endif
5130: 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 56  .#ifdef SQLITE_V
5140: 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 20  ERSION_NUMBER.# 
5150: 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 56 45 52  undef SQLITE_VER
5160: 53 49 4f 4e 5f 4e 55 4d 42 45 52 0a 23 65 6e 64  SION_NUMBER.#end
5170: 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  if../*.** CAPI3R
5180: 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65  EF: Compile-Time
5190: 20 4c 69 62 72 61 72 79 20 56 65 72 73 69 6f 6e   Library Version
51a0: 20 4e 75 6d 62 65 72 73 20 7b 48 31 30 30 31 30   Numbers {H10010
51b0: 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 2a  } <S60100>.**.**
51c0: 20 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52 53   The SQLITE_VERS
51d0: 49 4f 4e 20 61 6e 64 20 53 51 4c 49 54 45 5f 56  ION and SQLITE_V
51e0: 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 23 64  ERSION_NUMBER #d
51f0: 65 66 69 6e 65 73 20 69 6e 0a 2a 2a 20 74 68 65  efines in.** the
5200: 20 73 71 6c 69 74 65 33 2e 68 20 66 69 6c 65 20   sqlite3.h file 
5210: 73 70 65 63 69 66 79 20 74 68 65 20 76 65 72 73  specify the vers
5220: 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 20 77 69  ion of SQLite wi
5230: 74 68 20 77 68 69 63 68 0a 2a 2a 20 74 68 61 74  th which.** that
5240: 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 20   header file is 
5250: 61 73 73 6f 63 69 61 74 65 64 2e 0a 2a 2a 0a 2a  associated..**.*
5260: 2a 20 54 68 65 20 22 76 65 72 73 69 6f 6e 22 20  * The "version" 
5270: 6f 66 20 53 51 4c 69 74 65 20 69 73 20 61 20 73  of SQLite is a s
5280: 74 72 69 6e 67 20 6f 66 20 74 68 65 20 66 6f 72  tring of the for
5290: 6d 20 22 57 2e 58 2e 59 22 20 6f 72 20 22 57 2e  m "W.X.Y" or "W.
52a0: 58 2e 59 2e 5a 22 2e 0a 2a 2a 20 54 68 65 20 57  X.Y.Z"..** The W
52b0: 20 76 61 6c 75 65 20 69 73 20 6d 61 6a 6f 72 20   value is major 
52c0: 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 61  version number a
52d0: 6e 64 20 69 73 20 61 6c 77 61 79 73 20 33 20 69  nd is always 3 i
52e0: 6e 20 53 51 4c 69 74 65 33 2e 0a 2a 2a 20 54 68  n SQLite3..** Th
52f0: 65 20 57 20 76 61 6c 75 65 20 6f 6e 6c 79 20 63  e W value only c
5300: 68 61 6e 67 65 73 20 77 68 65 6e 20 62 61 63 6b  hanges when back
5310: 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69 6c  wards compatibil
5320: 69 74 79 20 69 73 0a 2a 2a 20 62 72 6f 6b 65 6e  ity is.** broken
5330: 20 61 6e 64 20 77 65 20 69 6e 74 65 6e 64 20 74   and we intend t
5340: 6f 20 6e 65 76 65 72 20 62 72 65 61 6b 20 62 61  o never break ba
5350: 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62  ckwards compatib
5360: 69 6c 69 74 79 2e 0a 2a 2a 20 54 68 65 20 58 20  ility..** The X 
5370: 76 61 6c 75 65 20 69 73 20 74 68 65 20 6d 69 6e  value is the min
5380: 6f 72 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65  or version numbe
5390: 72 20 61 6e 64 20 6f 6e 6c 79 20 63 68 61 6e 67  r and only chang
53a0: 65 73 20 77 68 65 6e 0a 2a 2a 20 74 68 65 72 65  es when.** there
53b0: 20 61 72 65 20 6d 61 6a 6f 72 20 66 65 61 74 75   are major featu
53c0: 72 65 20 65 6e 68 61 6e 63 65 6d 65 6e 74 73 20  re enhancements 
53d0: 74 68 61 74 20 61 72 65 20 66 6f 72 77 61 72 64  that are forward
53e0: 73 20 63 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a 20  s compatible.** 
53f0: 62 75 74 20 6e 6f 74 20 62 61 63 6b 77 61 72 64  but not backward
5400: 73 20 63 6f 6d 70 61 74 69 62 6c 65 2e 0a 2a 2a  s compatible..**
5410: 20 54 68 65 20 59 20 76 61 6c 75 65 20 69 73 20   The Y value is 
5420: 74 68 65 20 72 65 6c 65 61 73 65 20 6e 75 6d 62  the release numb
5430: 65 72 20 61 6e 64 20 69 73 20 69 6e 63 72 65 6d  er and is increm
5440: 65 6e 74 65 64 20 77 69 74 68 0a 2a 2a 20 65 61  ented with.** ea
5450: 63 68 20 72 65 6c 65 61 73 65 20 62 75 74 20 72  ch release but r
5460: 65 73 65 74 73 20 62 61 63 6b 20 74 6f 20 30 20  esets back to 0 
5470: 77 68 65 6e 65 76 65 72 20 58 20 69 73 20 69 6e  whenever X is in
5480: 63 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a 20 54 68  cremented..** Th
5490: 65 20 5a 20 76 61 6c 75 65 20 6f 6e 6c 79 20 61  e Z value only a
54a0: 70 70 65 61 72 73 20 6f 6e 20 62 72 61 6e 63 68  ppears on branch
54b0: 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a 2a   releases..**.**
54c0: 20 54 68 65 20 53 51 4c 49 54 45 5f 56 45 52 53   The SQLITE_VERS
54d0: 49 4f 4e 5f 4e 55 4d 42 45 52 20 69 73 20 61 6e  ION_NUMBER is an
54e0: 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 73   integer that is
54f0: 20 63 6f 6d 70 75 74 65 64 20 61 73 0a 2a 2a 20   computed as.** 
5500: 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c  follows:.**.** <
5510: 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e  blockquote><pre>
5520: 0a 2a 2a 20 53 51 4c 49 54 45 5f 56 45 52 53 49  .** SQLITE_VERSI
5530: 4f 4e 5f 4e 55 4d 42 45 52 20 3d 20 57 2a 31 30  ON_NUMBER = W*10
5540: 30 30 30 30 30 20 2b 20 58 2a 31 30 30 30 20 2b  00000 + X*1000 +
5550: 20 59 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c   Y.** </pre></bl
5560: 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20  ockquote>.**.** 
5570: 53 69 6e 63 65 20 76 65 72 73 69 6f 6e 20 33 2e  Since version 3.
5580: 36 2e 31 38 2c 20 53 51 4c 69 74 65 20 73 6f 75  6.18, SQLite sou
5590: 72 63 65 20 63 6f 64 65 20 68 61 73 20 62 65 65  rce code has bee
55a0: 6e 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a  n stored in the.
55b0: 2a 2a 20 3c 61 20 68 72 65 66 3d 22 68 74 74 70  ** <a href="http
55c0: 3a 2f 2f 77 77 77 2e 66 6f 73 73 69 6c 2d 73 63  ://www.fossil-sc
55d0: 6d 2e 6f 72 67 2f 22 3e 66 6f 73 73 69 6c 20 63  m.org/">fossil c
55e0: 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6d 61 6e  onfiguration man
55f0: 61 67 65 6d 65 6e 74 0a 2a 2a 20 73 79 73 74 65  agement.** syste
5600: 6d 3c 2f 61 3e 2e 20 20 54 68 65 20 53 51 4c 49  m</a>.  The SQLI
5610: 54 45 5f 53 4f 55 52 43 45 5f 49 44 0a 2a 2a 20  TE_SOURCE_ID.** 
5620: 6d 61 63 72 6f 20 69 73 20 61 20 73 74 72 69 6e  macro is a strin
5630: 67 20 77 68 69 63 68 20 69 64 65 6e 74 69 66 69  g which identifi
5640: 65 73 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  es a particular 
5650: 63 68 65 63 6b 2d 69 6e 20 6f 66 20 53 51 4c 69  check-in of SQLi
5660: 74 65 0a 2a 2a 20 77 69 74 68 69 6e 20 69 74 73  te.** within its
5670: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6d   configuration m
5680: 61 6e 61 67 65 6d 65 6e 74 20 73 79 73 74 65 6d  anagement system
5690: 2e 20 20 54 68 65 20 73 74 72 69 6e 67 20 63 6f  .  The string co
56a0: 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 64 61  ntains the.** da
56b0: 74 65 20 61 6e 64 20 74 69 6d 65 20 6f 66 20 74  te and time of t
56c0: 68 65 20 63 68 65 63 6b 2d 69 6e 20 28 55 54 43  he check-in (UTC
56d0: 29 20 61 6e 64 20 61 6e 20 53 48 41 31 20 68 61  ) and an SHA1 ha
56e0: 73 68 20 6f 66 20 74 68 65 20 65 6e 74 69 72 65  sh of the entire
56f0: 0a 2a 2a 20 73 6f 75 72 63 65 20 74 72 65 65 2e  .** source tree.
5700: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
5710: 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72   [sqlite3_libver
5720: 73 69 6f 6e 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c  sion()],.** [sql
5730: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f  ite3_libversion_
5740: 6e 75 6d 62 65 72 28 29 5d 2c 20 5b 73 71 6c 69  number()], [sqli
5750: 74 65 33 5f 73 6f 75 72 63 65 69 64 28 29 5d 2c  te3_sourceid()],
5760: 0a 2a 2a 20 5b 73 71 6c 69 74 65 5f 76 65 72 73  .** [sqlite_vers
5770: 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69  ion()] and [sqli
5780: 74 65 5f 73 6f 75 72 63 65 5f 69 64 28 29 5d 2e  te_source_id()].
5790: 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
57a0: 6e 74 73 3a 20 5b 48 31 30 30 31 31 5d 20 5b 48  nts: [H10011] [H
57b0: 31 30 30 31 34 5d 0a 2a 2f 0a 23 64 65 66 69 6e  10014].*/.#defin
57c0: 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e  e SQLITE_VERSION
57d0: 20 20 20 20 20 20 20 20 22 33 2e 36 2e 32 30 22          "3.6.20"
57e0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
57f0: 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 20 33  VERSION_NUMBER 3
5800: 30 30 36 30 32 30 0a 23 64 65 66 69 6e 65 20 53  006020.#define S
5810: 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49 44 20  QLITE_SOURCE_ID 
5820: 20 20 20 20 20 22 32 30 30 39 2d 31 30 2d 33 30       "2009-10-30
5830: 20 31 34 3a 32 37 3a 31 35 20 36 31 32 39 35 32   14:27:15 612952
5840: 37 34 33 64 61 32 38 65 36 35 31 35 31 32 35 34  743da28e65151254
5850: 37 66 63 30 64 33 39 32 35 66 34 63 36 39 38 65  7fc0d3925f4c698e
5860: 62 34 22 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  b4"../*.** CAPI3
5870: 52 45 46 3a 20 52 75 6e 2d 54 69 6d 65 20 4c 69  REF: Run-Time Li
5880: 62 72 61 72 79 20 56 65 72 73 69 6f 6e 20 4e 75  brary Version Nu
5890: 6d 62 65 72 73 20 7b 48 31 30 30 32 30 7d 20 3c  mbers {H10020} <
58a0: 53 36 30 31 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f  S60100>.** KEYWO
58b0: 52 44 53 3a 20 73 71 6c 69 74 65 33 5f 76 65 72  RDS: sqlite3_ver
58c0: 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  sion.**.** These
58d0: 20 69 6e 74 65 72 66 61 63 65 73 20 70 72 6f 76   interfaces prov
58e0: 69 64 65 20 74 68 65 20 73 61 6d 65 20 69 6e 66  ide the same inf
58f0: 6f 72 6d 61 74 69 6f 6e 20 61 73 20 74 68 65 20  ormation as the 
5900: 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5d  [SQLITE_VERSION]
5910: 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 56 45 52  ,.** [SQLITE_VER
5920: 53 49 4f 4e 5f 4e 55 4d 42 45 52 5d 2c 20 61 6e  SION_NUMBER], an
5930: 64 20 5b 53 51 4c 49 54 45 5f 53 4f 55 52 43 45  d [SQLITE_SOURCE
5940: 5f 49 44 5d 20 23 64 65 66 69 6e 65 73 20 69 6e  _ID] #defines in
5950: 20 74 68 65 20 68 65 61 64 65 72 2c 0a 2a 2a 20   the header,.** 
5960: 62 75 74 20 61 72 65 20 61 73 73 6f 63 69 61 74  but are associat
5970: 65 64 20 77 69 74 68 20 74 68 65 20 6c 69 62 72  ed with the libr
5980: 61 72 79 20 69 6e 73 74 65 61 64 20 6f 66 20 74  ary instead of t
5990: 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e 20  he header file. 
59a0: 20 43 61 75 74 69 6f 75 73 0a 2a 2a 20 70 72 6f   Cautious.** pro
59b0: 67 72 61 6d 6d 65 72 73 20 6d 69 67 68 74 20 69  grammers might i
59c0: 6e 63 6c 75 64 65 20 61 73 73 65 72 74 28 29 20  nclude assert() 
59d0: 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68  statements in th
59e0: 65 69 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  eir application 
59f0: 74 6f 0a 2a 2a 20 76 65 72 69 66 79 20 74 68 61  to.** verify tha
5a00: 74 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65  t values returne
5a10: 64 20 62 79 20 74 68 65 73 65 20 69 6e 74 65 72  d by these inter
5a20: 66 61 63 65 73 20 6d 61 74 63 68 20 74 68 65 20  faces match the 
5a30: 6d 61 63 72 6f 73 20 69 6e 0a 2a 2a 20 74 68 65  macros in.** the
5a40: 20 68 65 61 64 65 72 2c 20 61 6e 64 20 74 68 75   header, and thu
5a50: 73 20 69 6e 73 75 72 65 20 74 68 61 74 20 74 68  s insure that th
5a60: 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 69 73  e application is
5a70: 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 77 69 74  .** compiled wit
5a80: 68 20 6d 61 74 63 68 69 6e 67 20 6c 69 62 72 61  h matching libra
5a90: 72 79 20 61 6e 64 20 68 65 61 64 65 72 20 66 69  ry and header fi
5aa0: 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63  les..**.** <bloc
5ab0: 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20  kquote><pre>.** 
5ac0: 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
5ad0: 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65  libversion_numbe
5ae0: 72 28 29 3d 3d 53 51 4c 49 54 45 5f 56 45 52 53  r()==SQLITE_VERS
5af0: 49 4f 4e 5f 4e 55 4d 42 45 52 20 29 3b 0a 2a 2a  ION_NUMBER );.**
5b00: 20 61 73 73 65 72 74 28 20 73 74 72 63 6d 70 28   assert( strcmp(
5b10: 73 71 6c 69 74 65 33 5f 73 6f 75 72 63 65 69 64  sqlite3_sourceid
5b20: 28 29 2c 53 51 4c 49 54 45 5f 53 4f 55 52 43 45  (),SQLITE_SOURCE
5b30: 5f 49 44 29 3d 3d 30 20 29 3b 0a 2a 2a 20 61 73  _ID)==0 );.** as
5b40: 73 65 72 74 28 20 73 74 72 63 6d 70 28 73 71 6c  sert( strcmp(sql
5b50: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 2c  ite3_libversion,
5b60: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 29 3d  SQLITE_VERSION)=
5b70: 3d 30 20 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c  =0 );.** </pre><
5b80: 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a  /blockquote>.**.
5b90: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c  ** The sqlite3_l
5ba0: 69 62 76 65 72 73 69 6f 6e 28 29 20 66 75 6e 63  ibversion() func
5bb0: 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65  tion returns the
5bc0: 20 73 61 6d 65 20 69 6e 66 6f 72 6d 61 74 69 6f   same informatio
5bd0: 6e 20 61 73 20 69 73 0a 2a 2a 20 69 6e 20 74 68  n as is.** in th
5be0: 65 20 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f  e sqlite3_versio
5bf0: 6e 5b 5d 20 73 74 72 69 6e 67 20 63 6f 6e 73 74  n[] string const
5c00: 61 6e 74 2e 20 20 54 68 65 20 66 75 6e 63 74 69  ant.  The functi
5c10: 6f 6e 20 69 73 20 70 72 6f 76 69 64 65 64 0a 2a  on is provided.*
5c20: 2a 20 66 6f 72 20 75 73 65 20 69 6e 20 44 4c 4c  * for use in DLL
5c30: 73 20 73 69 6e 63 65 20 44 4c 4c 20 75 73 65 72  s since DLL user
5c40: 73 20 75 73 75 61 6c 6c 79 20 64 6f 20 6e 6f 74  s usually do not
5c50: 20 68 61 76 65 20 64 69 72 65 63 74 20 61 63 63   have direct acc
5c60: 65 73 73 20 74 6f 20 73 74 72 69 6e 67 0a 2a 2a  ess to string.**
5c70: 20 63 6f 6e 73 74 61 6e 74 73 20 77 69 74 68 69   constants withi
5c80: 6e 20 74 68 65 20 44 4c 4c 2e 20 20 53 69 6d 69  n the DLL.  Simi
5c90: 6c 61 72 6c 79 2c 20 74 68 65 20 73 71 6c 69 74  larly, the sqlit
5ca0: 65 33 5f 73 6f 75 72 63 65 69 64 28 29 20 66 75  e3_sourceid() fu
5cb0: 6e 63 74 69 6f 6e 0a 2a 2a 20 72 65 74 75 72 6e  nction.** return
5cc0: 73 20 74 68 65 20 73 61 6d 65 20 69 6e 66 6f 72  s the same infor
5cd0: 6d 61 74 69 6f 6e 20 61 73 20 69 73 20 69 6e 20  mation as is in 
5ce0: 74 68 65 20 5b 53 51 4c 49 54 45 5f 53 4f 55 52  the [SQLITE_SOUR
5cf0: 43 45 5f 49 44 5d 20 23 64 65 66 69 6e 65 20 6f  CE_ID] #define o
5d00: 66 0a 2a 2a 20 74 68 65 20 68 65 61 64 65 72 20  f.** the header 
5d10: 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  file..**.** See 
5d20: 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 5f 76 65  also: [sqlite_ve
5d30: 72 73 69 6f 6e 28 29 5d 20 61 6e 64 20 5b 73 71  rsion()] and [sq
5d40: 6c 69 74 65 5f 73 6f 75 72 63 65 5f 69 64 28 29  lite_source_id()
5d50: 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  ]..**.** Require
5d60: 6d 65 6e 74 73 3a 20 5b 48 31 30 30 32 31 5d 20  ments: [H10021] 
5d70: 5b 48 31 30 30 32 32 5d 20 5b 48 31 30 30 32 33  [H10022] [H10023
5d80: 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
5d90: 63 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74  const char sqlit
5da0: 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 3d 20 53  e3_version[] = S
5db0: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 3b 0a 53  QLITE_VERSION;.S
5dc0: 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
5dd0: 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c 69  char *sqlite3_li
5de0: 62 76 65 72 73 69 6f 6e 28 76 6f 69 64 29 3b 0a  bversion(void);.
5df0: 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
5e00: 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73   char *sqlite3_s
5e10: 6f 75 72 63 65 69 64 28 76 6f 69 64 29 3b 0a 53  ourceid(void);.S
5e20: 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
5e30: 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e  lite3_libversion
5e40: 5f 6e 75 6d 62 65 72 28 76 6f 69 64 29 3b 0a 0a  _number(void);..
5e50: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
5e60: 54 65 73 74 20 54 6f 20 53 65 65 20 49 66 20 54  Test To See If T
5e70: 68 65 20 4c 69 62 72 61 72 79 20 49 73 20 54 68  he Library Is Th
5e80: 72 65 61 64 73 61 66 65 20 7b 48 31 30 31 30 30  readsafe {H10100
5e90: 7d 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 2a  } <S60100>.**.**
5ea0: 20 53 51 4c 69 74 65 20 63 61 6e 20 62 65 20 63   SQLite can be c
5eb0: 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 6f 72 20  ompiled with or 
5ec0: 77 69 74 68 6f 75 74 20 6d 75 74 65 78 65 73 2e  without mutexes.
5ed0: 20 20 57 68 65 6e 0a 2a 2a 20 74 68 65 20 5b 53    When.** the [S
5ee0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
5ef0: 5d 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f 72  ] C preprocessor
5f00: 20 6d 61 63 72 6f 20 69 73 20 31 20 6f 72 20 32   macro is 1 or 2
5f10: 2c 20 6d 75 74 65 78 65 73 0a 2a 2a 20 61 72 65  , mutexes.** are
5f20: 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 53 51 4c   enabled and SQL
5f30: 69 74 65 20 69 73 20 74 68 72 65 61 64 73 61 66  ite is threadsaf
5f40: 65 2e 20 20 57 68 65 6e 20 74 68 65 0a 2a 2a 20  e.  When the.** 
5f50: 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41  [SQLITE_THREADSA
5f60: 46 45 5d 20 6d 61 63 72 6f 20 69 73 20 30 2c 20  FE] macro is 0, 
5f70: 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 65 73 20  .** the mutexes 
5f80: 61 72 65 20 6f 6d 69 74 74 65 64 2e 20 20 57 69  are omitted.  Wi
5f90: 74 68 6f 75 74 20 74 68 65 20 6d 75 74 65 78 65  thout the mutexe
5fa0: 73 2c 20 69 74 20 69 73 20 6e 6f 74 20 73 61 66  s, it is not saf
5fb0: 65 0a 2a 2a 20 74 6f 20 75 73 65 20 53 51 4c 69  e.** to use SQLi
5fc0: 74 65 20 63 6f 6e 63 75 72 72 65 6e 74 6c 79 20  te concurrently 
5fd0: 66 72 6f 6d 20 6d 6f 72 65 20 74 68 61 6e 20 6f  from more than o
5fe0: 6e 65 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a  ne thread..**.**
5ff0: 20 45 6e 61 62 6c 69 6e 67 20 6d 75 74 65 78 65   Enabling mutexe
6000: 73 20 69 6e 63 75 72 73 20 61 20 6d 65 61 73 75  s incurs a measu
6010: 72 61 62 6c 65 20 70 65 72 66 6f 72 6d 61 6e 63  rable performanc
6020: 65 20 70 65 6e 61 6c 74 79 2e 0a 2a 2a 20 53 6f  e penalty..** So
6030: 20 69 66 20 73 70 65 65 64 20 69 73 20 6f 66 20   if speed is of 
6040: 75 74 6d 6f 73 74 20 69 6d 70 6f 72 74 61 6e 63  utmost importanc
6050: 65 2c 20 69 74 20 6d 61 6b 65 73 20 73 65 6e 73  e, it makes sens
6060: 65 20 74 6f 20 64 69 73 61 62 6c 65 0a 2a 2a 20  e to disable.** 
6070: 74 68 65 20 6d 75 74 65 78 65 73 2e 20 20 42 75  the mutexes.  Bu
6080: 74 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 73 61  t for maximum sa
6090: 66 65 74 79 2c 20 6d 75 74 65 78 65 73 20 73 68  fety, mutexes sh
60a0: 6f 75 6c 64 20 62 65 20 65 6e 61 62 6c 65 64 2e  ould be enabled.
60b0: 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20  .** The default 
60c0: 62 65 68 61 76 69 6f 72 20 69 73 20 66 6f 72 20  behavior is for 
60d0: 6d 75 74 65 78 65 73 20 74 6f 20 62 65 20 65 6e  mutexes to be en
60e0: 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  abled..**.** Thi
60f0: 73 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20  s interface can 
6100: 62 65 20 75 73 65 64 20 62 79 20 61 6e 20 61 70  be used by an ap
6110: 70 6c 69 63 61 74 69 6f 6e 20 74 6f 20 6d 61 6b  plication to mak
6120: 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65 0a  e sure that the.
6130: 2a 2a 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51  ** version of SQ
6140: 4c 69 74 65 20 74 68 61 74 20 69 74 20 69 73 20  Lite that it is 
6150: 6c 69 6e 6b 69 6e 67 20 61 67 61 69 6e 73 74 20  linking against 
6160: 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74  was compiled wit
6170: 68 0a 2a 2a 20 74 68 65 20 64 65 73 69 72 65 64  h.** the desired
6180: 20 73 65 74 74 69 6e 67 20 6f 66 20 74 68 65 20   setting of the 
6190: 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41  [SQLITE_THREADSA
61a0: 46 45 5d 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a  FE] macro..**.**
61b0: 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   This interface 
61c0: 6f 6e 6c 79 20 72 65 70 6f 72 74 73 20 6f 6e 20  only reports on 
61d0: 74 68 65 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  the compile-time
61e0: 20 6d 75 74 65 78 20 73 65 74 74 69 6e 67 0a 2a   mutex setting.*
61f0: 2a 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45  * of the [SQLITE
6200: 5f 54 48 52 45 41 44 53 41 46 45 5d 20 66 6c 61  _THREADSAFE] fla
6210: 67 2e 20 20 49 66 20 53 51 4c 69 74 65 20 69 73  g.  If SQLite is
6220: 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a   compiled with.*
6230: 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  * SQLITE_THREADS
6240: 41 46 45 3d 31 20 74 68 65 6e 20 6d 75 74 65 78  AFE=1 then mutex
6250: 65 73 20 61 72 65 20 65 6e 61 62 6c 65 64 20 62  es are enabled b
6260: 79 20 64 65 66 61 75 6c 74 20 62 75 74 0a 2a 2a  y default but.**
6270: 20 63 61 6e 20 62 65 20 66 75 6c 6c 79 20 6f 72   can be fully or
6280: 20 70 61 72 74 69 61 6c 6c 79 20 64 69 73 61 62   partially disab
6290: 6c 65 64 20 75 73 69 6e 67 20 61 20 63 61 6c 6c  led using a call
62a0: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e   to [sqlite3_con
62b0: 66 69 67 28 29 5d 0a 2a 2a 20 77 69 74 68 20 74  fig()].** with t
62c0: 68 65 20 76 65 72 62 73 20 5b 53 51 4c 49 54 45  he verbs [SQLITE
62d0: 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48  _CONFIG_SINGLETH
62e0: 52 45 41 44 5d 2c 20 5b 53 51 4c 49 54 45 5f 43  READ], [SQLITE_C
62f0: 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41  ONFIG_MULTITHREA
6300: 44 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54  D],.** or [SQLIT
6310: 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 2e  E_CONFIG_MUTEX].
6320: 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c    The return val
6330: 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74  ue of this funct
6340: 69 6f 6e 20 73 68 6f 77 73 0a 2a 2a 20 6f 6e 6c  ion shows.** onl
6350: 79 20 74 68 65 20 64 65 66 61 75 6c 74 20 63 6f  y the default co
6360: 6d 70 69 6c 65 2d 74 69 6d 65 20 73 65 74 74 69  mpile-time setti
6370: 6e 67 2c 20 6e 6f 74 20 61 6e 79 20 72 75 6e 2d  ng, not any run-
6380: 74 69 6d 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20  time changes.** 
6390: 74 6f 20 74 68 61 74 20 73 65 74 74 69 6e 67 2e  to that setting.
63a0: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 74 68 65 20 5b  .**.** See the [
63b0: 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20  threading mode] 
63c0: 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f  documentation fo
63d0: 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
63e0: 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  ormation..**.** 
63f0: 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48  Requirements: [H
6400: 31 30 31 30 31 5d 20 5b 48 31 30 31 30 32 5d 0a  10101] [H10102].
6410: 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
6420: 74 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64  t sqlite3_thread
6430: 73 61 66 65 28 76 6f 69 64 29 3b 0a 0a 2f 2a 0a  safe(void);../*.
6440: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74  ** CAPI3REF: Dat
6450: 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e  abase Connection
6460: 20 48 61 6e 64 6c 65 20 7b 48 31 32 30 30 30 7d   Handle {H12000}
6470: 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 20 4b 45 59   <S40200>.** KEY
6480: 57 4f 52 44 53 3a 20 7b 64 61 74 61 62 61 73 65  WORDS: {database
6490: 20 63 6f 6e 6e 65 63 74 69 6f 6e 7d 20 7b 64 61   connection} {da
64a0: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
64b0: 6e 73 7d 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 6f  ns}.**.** Each o
64c0: 70 65 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62  pen SQLite datab
64d0: 61 73 65 20 69 73 20 72 65 70 72 65 73 65 6e 74  ase is represent
64e0: 65 64 20 62 79 20 61 20 70 6f 69 6e 74 65 72 20  ed by a pointer 
64f0: 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  to an instance o
6500: 66 0a 2a 2a 20 74 68 65 20 6f 70 61 71 75 65 20  f.** the opaque 
6510: 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20  structure named 
6520: 22 73 71 6c 69 74 65 33 22 2e 20 20 49 74 20 69  "sqlite3".  It i
6530: 73 20 75 73 65 66 75 6c 20 74 6f 20 74 68 69 6e  s useful to thin
6540: 6b 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 0a  k of an sqlite3.
6550: 2a 2a 20 70 6f 69 6e 74 65 72 20 61 73 20 61 6e  ** pointer as an
6560: 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 5b 73   object.  The [s
6570: 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20  qlite3_open()], 
6580: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28  [sqlite3_open16(
6590: 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69  )], and.** [sqli
65a0: 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69  te3_open_v2()] i
65b0: 6e 74 65 72 66 61 63 65 73 20 61 72 65 20 69 74  nterfaces are it
65c0: 73 20 63 6f 6e 73 74 72 75 63 74 6f 72 73 2c 20  s constructors, 
65d0: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f  and [sqlite3_clo
65e0: 73 65 28 29 5d 0a 2a 2a 20 69 73 20 69 74 73 20  se()].** is its 
65f0: 64 65 73 74 72 75 63 74 6f 72 2e 20 20 54 68 65  destructor.  The
6600: 72 65 20 61 72 65 20 6d 61 6e 79 20 6f 74 68 65  re are many othe
6610: 72 20 69 6e 74 65 72 66 61 63 65 73 20 28 73 75  r interfaces (su
6620: 63 68 20 61 73 0a 2a 2a 20 5b 73 71 6c 69 74 65  ch as.** [sqlite
6630: 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 2c  3_prepare_v2()],
6640: 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
6650: 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 2c 20 61 6e  _function()], an
6660: 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 75  d.** [sqlite3_bu
6670: 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d 20 74 6f  sy_timeout()] to
6680: 20 6e 61 6d 65 20 62 75 74 20 74 68 72 65 65 29   name but three)
6690: 20 74 68 61 74 20 61 72 65 20 6d 65 74 68 6f 64   that are method
66a0: 73 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74  s on an.** sqlit
66b0: 65 33 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79  e3 object..*/.ty
66c0: 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
66d0: 69 74 65 33 20 73 71 6c 69 74 65 33 3b 0a 0a 2f  ite3 sqlite3;../
66e0: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 36  *.** CAPI3REF: 6
66f0: 34 2d 42 69 74 20 49 6e 74 65 67 65 72 20 54 79  4-Bit Integer Ty
6700: 70 65 73 20 7b 48 31 30 32 30 30 7d 20 3c 53 31  pes {H10200} <S1
6710: 30 31 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44  0110>.** KEYWORD
6720: 53 3a 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  S: sqlite_int64 
6730: 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 0a 2a 2a  sqlite_uint64.**
6740: 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 68 65 72  .** Because ther
6750: 65 20 69 73 20 6e 6f 20 63 72 6f 73 73 2d 70 6c  e is no cross-pl
6760: 61 74 66 6f 72 6d 20 77 61 79 20 74 6f 20 73 70  atform way to sp
6770: 65 63 69 66 79 20 36 34 2d 62 69 74 20 69 6e 74  ecify 64-bit int
6780: 65 67 65 72 20 74 79 70 65 73 0a 2a 2a 20 53 51  eger types.** SQ
6790: 4c 69 74 65 20 69 6e 63 6c 75 64 65 73 20 74 79  Lite includes ty
67a0: 70 65 64 65 66 73 20 66 6f 72 20 36 34 2d 62 69  pedefs for 64-bi
67b0: 74 20 73 69 67 6e 65 64 20 61 6e 64 20 75 6e 73  t signed and uns
67c0: 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73 2e 0a  igned integers..
67d0: 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
67e0: 33 5f 69 6e 74 36 34 20 61 6e 64 20 73 71 6c 69  3_int64 and sqli
67f0: 74 65 33 5f 75 69 6e 74 36 34 20 61 72 65 20 74  te3_uint64 are t
6800: 68 65 20 70 72 65 66 65 72 72 65 64 20 74 79 70  he preferred typ
6810: 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 2a  e definitions..*
6820: 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 69 6e 74  * The sqlite_int
6830: 36 34 20 61 6e 64 20 73 71 6c 69 74 65 5f 75 69  64 and sqlite_ui
6840: 6e 74 36 34 20 74 79 70 65 73 20 61 72 65 20 73  nt64 types are s
6850: 75 70 70 6f 72 74 65 64 20 66 6f 72 20 62 61 63  upported for bac
6860: 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d 70 61 74  kwards.** compat
6870: 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 0a 2a 2a  ibility only..**
6880: 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
6890: 3a 20 5b 48 31 30 32 30 31 5d 20 5b 48 31 30 32  : [H10201] [H102
68a0: 30 32 5d 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  02].*/.#ifdef SQ
68b0: 4c 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 0a  LITE_INT64_TYPE.
68c0: 20 20 74 79 70 65 64 65 66 20 53 51 4c 49 54 45    typedef SQLITE
68d0: 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c 69  _INT64_TYPE sqli
68e0: 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65  te_int64;.  type
68f0: 64 65 66 20 75 6e 73 69 67 6e 65 64 20 53 51 4c  def unsigned SQL
6900: 49 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73  ITE_INT64_TYPE s
6910: 71 6c 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65  qlite_uint64;.#e
6920: 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43  lif defined(_MSC
6930: 5f 56 45 52 29 20 7c 7c 20 64 65 66 69 6e 65 64  _VER) || defined
6940: 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 20  (__BORLANDC__). 
6950: 20 74 79 70 65 64 65 66 20 5f 5f 69 6e 74 36 34   typedef __int64
6960: 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20   sqlite_int64;. 
6970: 20 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65   typedef unsigne
6980: 64 20 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  d __int64 sqlite
6990: 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 73 65 0a 20  _uint64;.#else. 
69a0: 20 74 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c 6f   typedef long lo
69b0: 6e 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 69 6e  ng int sqlite_in
69c0: 74 36 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75  t64;.  typedef u
69d0: 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
69e0: 67 20 69 6e 74 20 73 71 6c 69 74 65 5f 75 69 6e  g int sqlite_uin
69f0: 74 36 34 3b 0a 23 65 6e 64 69 66 0a 74 79 70 65  t64;.#endif.type
6a00: 64 65 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  def sqlite_int64
6a10: 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 3b 0a   sqlite3_int64;.
6a20: 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f 75  typedef sqlite_u
6a30: 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 75 69  int64 sqlite3_ui
6a40: 6e 74 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  nt64;../*.** If 
6a50: 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20  compiling for a 
6a60: 70 72 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c  processor that l
6a70: 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f  acks floating po
6a80: 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20  int support,.** 
6a90: 73 75 62 73 74 69 74 75 74 65 20 69 6e 74 65 67  substitute integ
6aa0: 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d  er for floating-
6ab0: 70 6f 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64 65 66  point..*/.#ifdef
6ac0: 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
6ad0: 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65  ATING_POINT.# de
6ae0: 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69  fine double sqli
6af0: 74 65 33 5f 69 6e 74 36 34 0a 23 65 6e 64 69 66  te3_int64.#endif
6b00: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
6b10: 3a 20 43 6c 6f 73 69 6e 67 20 41 20 44 61 74 61  : Closing A Data
6b20: 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20  base Connection 
6b30: 7b 48 31 32 30 31 30 7d 20 3c 53 33 30 31 30 30  {H12010} <S30100
6b40: 3e 3c 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20  ><S40200>.**.** 
6b50: 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
6b60: 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 66  the destructor f
6b70: 6f 72 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d  or the [sqlite3]
6b80: 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41   object..**.** A
6b90: 70 70 6c 69 63 61 74 69 6f 6e 73 20 6d 75 73 74  pplications must
6ba0: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
6bb0: 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65 5d 20 61  ze | finalize] a
6bc0: 6c 6c 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  ll [prepared sta
6bd0: 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20 61 6e 64 20  tements].** and 
6be0: 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c  [sqlite3_blob_cl
6bf0: 6f 73 65 20 7c 20 63 6c 6f 73 65 5d 20 61 6c 6c  ose | close] all
6c00: 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 5d 20   [BLOB handles] 
6c10: 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a  associated with.
6c20: 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d  ** the [sqlite3]
6c30: 20 6f 62 6a 65 63 74 20 70 72 69 6f 72 20 74 6f   object prior to
6c40: 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 63   attempting to c
6c50: 6c 6f 73 65 20 74 68 65 20 6f 62 6a 65 63 74 2e  lose the object.
6c60: 0a 2a 2a 0a 2a 2a 20 49 66 20 5b 73 71 6c 69 74  .**.** If [sqlit
6c70: 65 33 5f 63 6c 6f 73 65 28 29 5d 20 69 73 20 69  e3_close()] is i
6c80: 6e 76 6f 6b 65 64 20 77 68 69 6c 65 20 61 20 74  nvoked while a t
6c90: 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70  ransaction is op
6ca0: 65 6e 2c 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73  en,.** the trans
6cb0: 61 63 74 69 6f 6e 20 69 73 20 61 75 74 6f 6d 61  action is automa
6cc0: 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62  tically rolled b
6cd0: 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43  ack..**.** The C
6ce0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73   parameter to [s
6cf0: 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 43 29 5d  qlite3_close(C)]
6d00: 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
6d10: 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65  a NULL.** pointe
6d20: 72 20 6f 72 20 61 6e 20 5b 73 71 6c 69 74 65 33  r or an [sqlite3
6d30: 5d 20 6f 62 6a 65 63 74 20 70 6f 69 6e 74 65 72  ] object pointer
6d40: 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f   obtained.** fro
6d50: 6d 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  m [sqlite3_open(
6d60: 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65  )], [sqlite3_ope
6d70: 6e 31 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73  n16()], or.** [s
6d80: 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
6d90: 5d 2c 20 61 6e 64 20 6e 6f 74 20 70 72 65 76 69  ], and not previ
6da0: 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e 0a 2a 2a  ously closed..**
6db0: 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
6dc0: 3a 0a 2a 2a 20 5b 48 31 32 30 31 31 5d 20 5b 48  :.** [H12011] [H
6dd0: 31 32 30 31 32 5d 20 5b 48 31 32 30 31 33 5d 20  12012] [H12013] 
6de0: 5b 48 31 32 30 31 34 5d 20 5b 48 31 32 30 31 35  [H12014] [H12015
6df0: 5d 20 5b 48 31 32 30 31 39 5d 0a 2a 2f 0a 53 51  ] [H12019].*/.SQ
6e00: 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
6e10: 69 74 65 33 5f 63 6c 6f 73 65 28 73 71 6c 69 74  ite3_close(sqlit
6e20: 65 33 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  e3 *);../*.** Th
6e30: 65 20 74 79 70 65 20 66 6f 72 20 61 20 63 61 6c  e type for a cal
6e40: 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a  lback function..
6e50: 2a 2a 20 54 68 69 73 20 69 73 20 6c 65 67 61 63  ** This is legac
6e60: 79 20 61 6e 64 20 64 65 70 72 65 63 61 74 65 64  y and deprecated
6e70: 2e 20 20 49 74 20 69 73 20 69 6e 63 6c 75 64 65  .  It is include
6e80: 64 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c  d for historical
6e90: 0a 2a 2a 20 63 6f 6d 70 61 74 69 62 69 6c 69 74  .** compatibilit
6ea0: 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 64 6f 63  y and is not doc
6eb0: 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 74 79 70 65  umented..*/.type
6ec0: 64 65 66 20 69 6e 74 20 28 2a 73 71 6c 69 74 65  def int (*sqlite
6ed0: 33 5f 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64  3_callback)(void
6ee0: 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 20 63 68  *,int,char**, ch
6ef0: 61 72 2a 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  ar**);../*.** CA
6f00: 50 49 33 52 45 46 3a 20 4f 6e 65 2d 53 74 65 70  PI3REF: One-Step
6f10: 20 51 75 65 72 79 20 45 78 65 63 75 74 69 6f 6e   Query Execution
6f20: 20 49 6e 74 65 72 66 61 63 65 20 7b 48 31 32 31   Interface {H121
6f30: 30 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 0a  00} <S10000>.**.
6f40: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65  ** The sqlite3_e
6f50: 78 65 63 28 29 20 69 6e 74 65 72 66 61 63 65 20  xec() interface 
6f60: 69 73 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74 20  is a convenient 
6f70: 77 61 79 20 6f 66 20 72 75 6e 6e 69 6e 67 20 6f  way of running o
6f80: 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 53 51  ne or more.** SQ
6f90: 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 77 69 74  L statements wit
6fa0: 68 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 77  hout having to w
6fb0: 72 69 74 65 20 61 20 6c 6f 74 20 6f 66 20 43 20  rite a lot of C 
6fc0: 63 6f 64 65 2e 20 20 54 68 65 20 55 54 46 2d 38  code.  The UTF-8
6fd0: 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 53 51 4c 20   encoded.** SQL 
6fe0: 73 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 70  statements are p
6ff0: 61 73 73 65 64 20 69 6e 20 61 73 20 74 68 65 20  assed in as the 
7000: 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
7010: 20 74 6f 20 73 71 6c 69 74 65 33 5f 65 78 65 63   to sqlite3_exec
7020: 28 29 2e 0a 2a 2a 20 54 68 65 20 73 74 61 74 65  ()..** The state
7030: 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61  ments are evalua
7040: 74 65 64 20 6f 6e 65 20 62 79 20 6f 6e 65 20 75  ted one by one u
7050: 6e 74 69 6c 20 65 69 74 68 65 72 20 61 6e 20 65  ntil either an e
7060: 72 72 6f 72 20 6f 72 0a 2a 2a 20 61 6e 20 69 6e  rror or.** an in
7070: 74 65 72 72 75 70 74 20 69 73 20 65 6e 63 6f 75  terrupt is encou
7080: 6e 74 65 72 65 64 2c 20 6f 72 20 75 6e 74 69 6c  ntered, or until
7090: 20 74 68 65 79 20 61 72 65 20 61 6c 6c 20 64 6f   they are all do
70a0: 6e 65 2e 20 20 54 68 65 20 33 72 64 20 70 61 72  ne.  The 3rd par
70b0: 61 6d 65 74 65 72 0a 2a 2a 20 69 73 20 61 6e 20  ameter.** is an 
70c0: 6f 70 74 69 6f 6e 61 6c 20 63 61 6c 6c 62 61 63  optional callbac
70d0: 6b 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65  k that is invoke
70e0: 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20  d once for each 
70f0: 72 6f 77 20 6f 66 20 61 6e 79 20 71 75 65 72 79  row of any query
7100: 0a 2a 2a 20 72 65 73 75 6c 74 73 20 70 72 6f 64  .** results prod
7110: 75 63 65 64 20 62 79 20 74 68 65 20 53 51 4c 20  uced by the SQL 
7120: 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 65  statements.  The
7130: 20 35 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   5th parameter t
7140: 65 6c 6c 73 20 77 68 65 72 65 0a 2a 2a 20 74 6f  ells where.** to
7150: 20 77 72 69 74 65 20 61 6e 79 20 65 72 72 6f 72   write any error
7160: 20 6d 65 73 73 61 67 65 73 2e 0a 2a 2a 0a 2a 2a   messages..**.**
7170: 20 54 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   The error messa
7180: 67 65 20 70 61 73 73 65 64 20 62 61 63 6b 20 74  ge passed back t
7190: 68 72 6f 75 67 68 20 74 68 65 20 35 74 68 20 70  hrough the 5th p
71a0: 61 72 61 6d 65 74 65 72 20 69 73 20 68 65 6c 64  arameter is held
71b0: 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62  .** in memory ob
71c0: 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c  tained from [sql
71d0: 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20  ite3_malloc()]. 
71e0: 20 54 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d 6f   To avoid a memo
71f0: 72 79 20 6c 65 61 6b 2c 0a 2a 2a 20 74 68 65 20  ry leak,.** the 
7200: 63 61 6c 6c 69 6e 67 20 61 70 70 6c 69 63 61 74  calling applicat
7210: 69 6f 6e 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20  ion should call 
7220: 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d  [sqlite3_free()]
7230: 20 6f 6e 20 61 6e 79 20 65 72 72 6f 72 0a 2a 2a   on any error.**
7240: 20 6d 65 73 73 61 67 65 20 72 65 74 75 72 6e 65   message returne
7250: 64 20 74 68 72 6f 75 67 68 20 74 68 65 20 35 74  d through the 5t
7260: 68 20 70 61 72 61 6d 65 74 65 72 20 77 68 65 6e  h parameter when
7270: 20 69 74 20 68 61 73 20 66 69 6e 69 73 68 65 64   it has finished
7280: 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 65 72   using.** the er
7290: 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a  ror message..**.
72a0: 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 20 73 74  ** If the SQL st
72b0: 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 32  atement in the 2
72c0: 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  nd parameter is 
72d0: 4e 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70 74 79  NULL or an empty
72e0: 20 73 74 72 69 6e 67 0a 2a 2a 20 6f 72 20 61 20   string.** or a 
72f0: 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e  string containin
7300: 67 20 6f 6e 6c 79 20 77 68 69 74 65 73 70 61 63  g only whitespac
7310: 65 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 2c 20  e and comments, 
7320: 74 68 65 6e 20 6e 6f 20 53 51 4c 0a 2a 2a 20 73  then no SQL.** s
7330: 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 65 76  tatements are ev
7340: 61 6c 75 61 74 65 64 20 61 6e 64 20 74 68 65 20  aluated and the 
7350: 64 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20  database is not 
7360: 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  changed..**.** T
7370: 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  he sqlite3_exec(
7380: 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 69  ) interface is i
7390: 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74 65  mplemented in te
73a0: 72 6d 73 20 6f 66 0a 2a 2a 20 5b 73 71 6c 69 74  rms of.** [sqlit
73b0: 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
73c0: 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  , [sqlite3_step(
73d0: 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  )], and [sqlite3
73e0: 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a  _finalize()]..**
73f0: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78 65   The sqlite3_exe
7400: 63 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73  c() routine does
7410: 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 65 20   nothing to the 
7420: 64 61 74 61 62 61 73 65 20 74 68 61 74 20 63 61  database that ca
7430: 6e 6e 6f 74 20 62 65 20 64 6f 6e 65 0a 2a 2a 20  nnot be done.** 
7440: 62 79 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  by [sqlite3_prep
7450: 61 72 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c 69  are_v2()], [sqli
7460: 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e 64  te3_step()], and
7470: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
7480: 7a 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ze()]..**.** The
7490: 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
74a0: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65   to [sqlite3_exe
74b0: 63 28 29 5d 20 6d 75 73 74 20 62 65 20 61 6e 20  c()] must be an 
74c0: 76 61 6c 69 64 20 61 6e 64 20 6f 70 65 6e 0a 2a  valid and open.*
74d0: 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  * [database conn
74e0: 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 0a 2a 2a 20 54  ection]..**.** T
74f0: 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
7500: 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20  ection must not 
7510: 62 65 20 63 6c 6f 73 65 64 20 77 68 69 6c 65 0a  be closed while.
7520: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63  ** [sqlite3_exec
7530: 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a  ()] is running..
7540: 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e  **.** The callin
7550: 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c  g function shoul
7560: 64 20 75 73 65 20 5b 73 71 6c 69 74 65 33 5f 66  d use [sqlite3_f
7570: 72 65 65 28 29 5d 20 74 6f 20 66 72 65 65 0a 2a  ree()] to free.*
7580: 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 74 68 61  * the memory tha
7590: 74 20 2a 65 72 72 6d 73 67 20 69 73 20 6c 65 66  t *errmsg is lef
75a0: 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 6f 6e  t pointing at on
75b0: 63 65 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20  ce the error.** 
75c0: 6d 65 73 73 61 67 65 20 69 73 20 6e 6f 20 6c 6f  message is no lo
75d0: 6e 67 65 72 20 6e 65 65 64 65 64 2e 0a 2a 2a 0a  nger needed..**.
75e0: 2a 2a 20 54 68 65 20 53 51 4c 20 73 74 61 74 65  ** The SQL state
75f0: 6d 65 6e 74 20 74 65 78 74 20 69 6e 20 74 68 65  ment text in the
7600: 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74   2nd parameter t
7610: 6f 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28  o [sqlite3_exec(
7620: 29 5d 0a 2a 2a 20 6d 75 73 74 20 72 65 6d 61 69  )].** must remai
7630: 6e 20 75 6e 63 68 61 6e 67 65 64 20 77 68 69 6c  n unchanged whil
7640: 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28  e [sqlite3_exec(
7650: 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a  )] is running..*
7660: 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
7670: 73 3a 0a 2a 2a 20 5b 48 31 32 31 30 31 5d 20 5b  s:.** [H12101] [
7680: 48 31 32 31 30 32 5d 20 5b 48 31 32 31 30 34 5d  H12102] [H12104]
7690: 20 5b 48 31 32 31 30 35 5d 20 5b 48 31 32 31 30   [H12105] [H1210
76a0: 37 5d 20 5b 48 31 32 31 31 30 5d 20 5b 48 31 32  7] [H12110] [H12
76b0: 31 31 33 5d 20 5b 48 31 32 31 31 36 5d 0a 2a 2a  113] [H12116].**
76c0: 20 5b 48 31 32 31 31 39 5d 20 5b 48 31 32 31 32   [H12119] [H1212
76d0: 32 5d 20 5b 48 31 32 31 32 35 5d 20 5b 48 31 32  2] [H12125] [H12
76e0: 31 33 31 5d 20 5b 48 31 32 31 33 34 5d 20 5b 48  131] [H12134] [H
76f0: 31 32 31 33 37 5d 20 5b 48 31 32 31 33 38 5d 0a  12137] [H12138].
7700: 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
7710: 74 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 0a  t sqlite3_exec(.
7720: 20 20 73 71 6c 69 74 65 33 2a 2c 20 20 20 20 20    sqlite3*,     
7730: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7740: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
7750: 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65  An open database
7760: 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
7770: 20 2a 73 71 6c 2c 20 20 20 20 20 20 20 20 20 20   *sql,          
7780: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7790: 20 2f 2a 20 53 51 4c 20 74 6f 20 62 65 20 65 76   /* SQL to be ev
77a0: 61 6c 75 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74  aluated */.  int
77b0: 20 28 2a 63 61 6c 6c 62 61 63 6b 29 28 76 6f 69   (*callback)(voi
77c0: 64 2a 2c 69 6e 74 2c 63 68 61 72 2a 2a 2c 63 68  d*,int,char**,ch
77d0: 61 72 2a 2a 29 2c 20 20 2f 2a 20 43 61 6c 6c 62  ar**),  /* Callb
77e0: 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ack function */.
77f0: 20 20 76 6f 69 64 20 2a 2c 20 20 20 20 20 20 20    void *,       
7800: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7810: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
7820: 31 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  1st argument to 
7830: 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 63 68  callback */.  ch
7840: 61 72 20 2a 2a 65 72 72 6d 73 67 20 20 20 20 20  ar **errmsg     
7850: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7860: 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f           /* Erro
7870: 72 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68 65  r msg written he
7880: 72 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  re */.);../*.** 
7890: 43 41 50 49 33 52 45 46 3a 20 52 65 73 75 6c 74  CAPI3REF: Result
78a0: 20 43 6f 64 65 73 20 7b 48 31 30 32 31 30 7d 20   Codes {H10210} 
78b0: 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57  <S10700>.** KEYW
78c0: 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f 4f 4b 20  ORDS: SQLITE_OK 
78d0: 7b 65 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65 72  {error code} {er
78e0: 72 6f 72 20 63 6f 64 65 73 7d 0a 2a 2a 20 4b 45  ror codes}.** KE
78f0: 59 57 4f 52 44 53 3a 20 7b 72 65 73 75 6c 74 20  YWORDS: {result 
7900: 63 6f 64 65 7d 20 7b 72 65 73 75 6c 74 20 63 6f  code} {result co
7910: 64 65 73 7d 0a 2a 2a 0a 2a 2a 20 4d 61 6e 79 20  des}.**.** Many 
7920: 53 51 4c 69 74 65 20 66 75 6e 63 74 69 6f 6e 73  SQLite functions
7930: 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67   return an integ
7940: 65 72 20 72 65 73 75 6c 74 20 63 6f 64 65 20 66  er result code f
7950: 72 6f 6d 20 74 68 65 20 73 65 74 20 73 68 6f 77  rom the set show
7960: 6e 0a 2a 2a 20 68 65 72 65 20 69 6e 20 6f 72 64  n.** here in ord
7970: 65 72 20 74 6f 20 69 6e 64 69 63 61 74 65 73 20  er to indicates 
7980: 73 75 63 63 65 73 73 20 6f 72 20 66 61 69 6c 75  success or failu
7990: 72 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 65 72  re..**.** New er
79a0: 72 6f 72 20 63 6f 64 65 73 20 6d 61 79 20 62 65  ror codes may be
79b0: 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65   added in future
79c0: 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
79d0: 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  ite..**.** See a
79e0: 6c 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 49 4f 45  lso: [SQLITE_IOE
79f0: 52 52 5f 52 45 41 44 20 7c 20 65 78 74 65 6e 64  RR_READ | extend
7a00: 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d  ed result codes]
7a10: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
7a20: 54 45 5f 4f 4b 20 20 20 20 20 20 20 20 20 20 20  TE_OK           
7a30: 30 20 20 20 2f 2a 20 53 75 63 63 65 73 73 66 75  0   /* Successfu
7a40: 6c 20 72 65 73 75 6c 74 20 2a 2f 0a 2f 2a 20 62  l result */./* b
7a50: 65 67 69 6e 6e 69 6e 67 2d 6f 66 2d 65 72 72 6f  eginning-of-erro
7a60: 72 2d 63 6f 64 65 73 20 2a 2f 0a 23 64 65 66 69  r-codes */.#defi
7a70: 6e 65 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  ne SQLITE_ERROR 
7a80: 20 20 20 20 20 20 20 31 20 20 20 2f 2a 20 53 51         1   /* SQ
7a90: 4c 20 65 72 72 6f 72 20 6f 72 20 6d 69 73 73 69  L error or missi
7aa0: 6e 67 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 23  ng database */.#
7ab0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e  define SQLITE_IN
7ac0: 54 45 52 4e 41 4c 20 20 20 20 20 32 20 20 20 2f  TERNAL     2   /
7ad0: 2a 20 49 6e 74 65 72 6e 61 6c 20 6c 6f 67 69 63  * Internal logic
7ae0: 20 65 72 72 6f 72 20 69 6e 20 53 51 4c 69 74 65   error in SQLite
7af0: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
7b00: 54 45 5f 50 45 52 4d 20 20 20 20 20 20 20 20 20  TE_PERM         
7b10: 33 20 20 20 2f 2a 20 41 63 63 65 73 73 20 70 65  3   /* Access pe
7b20: 72 6d 69 73 73 69 6f 6e 20 64 65 6e 69 65 64 20  rmission denied 
7b30: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
7b40: 45 5f 41 42 4f 52 54 20 20 20 20 20 20 20 20 34  E_ABORT        4
7b50: 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 72     /* Callback r
7b60: 6f 75 74 69 6e 65 20 72 65 71 75 65 73 74 65 64  outine requested
7b70: 20 61 6e 20 61 62 6f 72 74 20 2a 2f 0a 23 64 65   an abort */.#de
7b80: 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 55 53 59  fine SQLITE_BUSY
7b90: 20 20 20 20 20 20 20 20 20 35 20 20 20 2f 2a 20           5   /* 
7ba0: 54 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  The database fil
7bb0: 65 20 69 73 20 6c 6f 63 6b 65 64 20 2a 2f 0a 23  e is locked */.#
7bc0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f  define SQLITE_LO
7bd0: 43 4b 45 44 20 20 20 20 20 20 20 36 20 20 20 2f  CKED       6   /
7be0: 2a 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68 65  * A table in the
7bf0: 20 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63   database is loc
7c00: 6b 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ked */.#define S
7c10: 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 20 20 20 20  QLITE_NOMEM     
7c20: 20 20 20 37 20 20 20 2f 2a 20 41 20 6d 61 6c 6c     7   /* A mall
7c30: 6f 63 28 29 20 66 61 69 6c 65 64 20 2a 2f 0a 23  oc() failed */.#
7c40: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 45  define SQLITE_RE
7c50: 41 44 4f 4e 4c 59 20 20 20 20 20 38 20 20 20 2f  ADONLY     8   /
7c60: 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 77 72 69  * Attempt to wri
7c70: 74 65 20 61 20 72 65 61 64 6f 6e 6c 79 20 64 61  te a readonly da
7c80: 74 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e  tabase */.#defin
7c90: 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55  e SQLITE_INTERRU
7ca0: 50 54 20 20 20 20 39 20 20 20 2f 2a 20 4f 70 65  PT    9   /* Ope
7cb0: 72 61 74 69 6f 6e 20 74 65 72 6d 69 6e 61 74 65  ration terminate
7cc0: 64 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 74  d by sqlite3_int
7cd0: 65 72 72 75 70 74 28 29 2a 2f 0a 23 64 65 66 69  errupt()*/.#defi
7ce0: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20  ne SQLITE_IOERR 
7cf0: 20 20 20 20 20 20 31 30 20 20 20 2f 2a 20 53 6f        10   /* So
7d00: 6d 65 20 6b 69 6e 64 20 6f 66 20 64 69 73 6b 20  me kind of disk 
7d10: 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 72  I/O error occurr
7d20: 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ed */.#define SQ
7d30: 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 20 20 20  LITE_CORRUPT    
7d40: 20 31 31 20 20 20 2f 2a 20 54 68 65 20 64 61 74   11   /* The dat
7d50: 61 62 61 73 65 20 64 69 73 6b 20 69 6d 61 67 65  abase disk image
7d60: 20 69 73 20 6d 61 6c 66 6f 72 6d 65 64 20 2a 2f   is malformed */
7d70: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
7d80: 4e 4f 54 46 4f 55 4e 44 20 20 20 20 31 32 20 20  NOTFOUND    12  
7d90: 20 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20 54 61   /* NOT USED. Ta
7da0: 62 6c 65 20 6f 72 20 72 65 63 6f 72 64 20 6e 6f  ble or record no
7db0: 74 20 66 6f 75 6e 64 20 2a 2f 0a 23 64 65 66 69  t found */.#defi
7dc0: 6e 65 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 20  ne SQLITE_FULL  
7dd0: 20 20 20 20 20 20 31 33 20 20 20 2f 2a 20 49 6e        13   /* In
7de0: 73 65 72 74 69 6f 6e 20 66 61 69 6c 65 64 20 62  sertion failed b
7df0: 65 63 61 75 73 65 20 64 61 74 61 62 61 73 65 20  ecause database 
7e00: 69 73 20 66 75 6c 6c 20 2a 2f 0a 23 64 65 66 69  is full */.#defi
7e10: 6e 65 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50  ne SQLITE_CANTOP
7e20: 45 4e 20 20 20 20 31 34 20 20 20 2f 2a 20 55 6e  EN    14   /* Un
7e30: 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 74 68 65  able to open the
7e40: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
7e50: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
7e60: 5f 50 52 4f 54 4f 43 4f 4c 20 20 20 20 31 35 20  _PROTOCOL    15 
7e70: 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 2e 20 44    /* NOT USED. D
7e80: 61 74 61 62 61 73 65 20 6c 6f 63 6b 20 70 72 6f  atabase lock pro
7e90: 74 6f 63 6f 6c 20 65 72 72 6f 72 20 2a 2f 0a 23  tocol error */.#
7ea0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 4d  define SQLITE_EM
7eb0: 50 54 59 20 20 20 20 20 20 20 31 36 20 20 20 2f  PTY       16   /
7ec0: 2a 20 44 61 74 61 62 61 73 65 20 69 73 20 65 6d  * Database is em
7ed0: 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  pty */.#define S
7ee0: 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 20 20 20  QLITE_SCHEMA    
7ef0: 20 20 31 37 20 20 20 2f 2a 20 54 68 65 20 64 61    17   /* The da
7f00: 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 63 68  tabase schema ch
7f10: 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  anged */.#define
7f20: 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 20   SQLITE_TOOBIG  
7f30: 20 20 20 20 31 38 20 20 20 2f 2a 20 53 74 72 69      18   /* Stri
7f40: 6e 67 20 6f 72 20 42 4c 4f 42 20 65 78 63 65 65  ng or BLOB excee
7f50: 64 73 20 73 69 7a 65 20 6c 69 6d 69 74 20 2a 2f  ds size limit */
7f60: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
7f70: 43 4f 4e 53 54 52 41 49 4e 54 20 20 31 39 20 20  CONSTRAINT  19  
7f80: 20 2f 2a 20 41 62 6f 72 74 20 64 75 65 20 74 6f   /* Abort due to
7f90: 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c   constraint viol
7fa0: 61 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65  ation */.#define
7fb0: 20 53 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48   SQLITE_MISMATCH
7fc0: 20 20 20 20 32 30 20 20 20 2f 2a 20 44 61 74 61      20   /* Data
7fd0: 20 74 79 70 65 20 6d 69 73 6d 61 74 63 68 20 2a   type mismatch *
7fe0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
7ff0: 5f 4d 49 53 55 53 45 20 20 20 20 20 20 32 31 20  _MISUSE      21 
8000: 20 20 2f 2a 20 4c 69 62 72 61 72 79 20 75 73 65    /* Library use
8010: 64 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 2a 2f  d incorrectly */
8020: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
8030: 4e 4f 4c 46 53 20 20 20 20 20 20 20 32 32 20 20  NOLFS       22  
8040: 20 2f 2a 20 55 73 65 73 20 4f 53 20 66 65 61 74   /* Uses OS feat
8050: 75 72 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74  ures not support
8060: 65 64 20 6f 6e 20 68 6f 73 74 20 2a 2f 0a 23 64  ed on host */.#d
8070: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 55 54  efine SQLITE_AUT
8080: 48 20 20 20 20 20 20 20 20 32 33 20 20 20 2f 2a  H        23   /*
8090: 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 64   Authorization d
80a0: 65 6e 69 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  enied */.#define
80b0: 20 53 51 4c 49 54 45 5f 46 4f 52 4d 41 54 20 20   SQLITE_FORMAT  
80c0: 20 20 20 20 32 34 20 20 20 2f 2a 20 41 75 78 69      24   /* Auxi
80d0: 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66  liary database f
80e0: 6f 72 6d 61 74 20 65 72 72 6f 72 20 2a 2f 0a 23  ormat error */.#
80f0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 41  define SQLITE_RA
8100: 4e 47 45 20 20 20 20 20 20 20 32 35 20 20 20 2f  NGE       25   /
8110: 2a 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 20  * 2nd parameter 
8120: 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 20  to sqlite3_bind 
8130: 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f 0a  out of range */.
8140: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e  #define SQLITE_N
8150: 4f 54 41 44 42 20 20 20 20 20 20 32 36 20 20 20  OTADB      26   
8160: 2f 2a 20 46 69 6c 65 20 6f 70 65 6e 65 64 20 74  /* File opened t
8170: 68 61 74 20 69 73 20 6e 6f 74 20 61 20 64 61 74  hat is not a dat
8180: 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 23 64  abase file */.#d
8190: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 4f 57  efine SQLITE_ROW
81a0: 20 20 20 20 20 20 20 20 20 31 30 30 20 20 2f 2a           100  /*
81b0: 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20   sqlite3_step() 
81c0: 68 61 73 20 61 6e 6f 74 68 65 72 20 72 6f 77 20  has another row 
81d0: 72 65 61 64 79 20 2a 2f 0a 23 64 65 66 69 6e 65  ready */.#define
81e0: 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 20 20 20   SQLITE_DONE    
81f0: 20 20 20 20 31 30 31 20 20 2f 2a 20 73 71 6c 69      101  /* sqli
8200: 74 65 33 5f 73 74 65 70 28 29 20 68 61 73 20 66  te3_step() has f
8210: 69 6e 69 73 68 65 64 20 65 78 65 63 75 74 69 6e  inished executin
8220: 67 20 2a 2f 0a 2f 2a 20 65 6e 64 2d 6f 66 2d 65  g */./* end-of-e
8230: 72 72 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 0a 2f  rror-codes */../
8240: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45  *.** CAPI3REF: E
8250: 78 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20 43  xtended Result C
8260: 6f 64 65 73 20 7b 48 31 30 32 32 30 7d 20 3c 53  odes {H10220} <S
8270: 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  10700>.** KEYWOR
8280: 44 53 3a 20 7b 65 78 74 65 6e 64 65 64 20 65 72  DS: {extended er
8290: 72 6f 72 20 63 6f 64 65 7d 20 7b 65 78 74 65 6e  ror code} {exten
82a0: 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 73 7d  ded error codes}
82b0: 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 65  .** KEYWORDS: {e
82c0: 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63  xtended result c
82d0: 6f 64 65 7d 20 7b 65 78 74 65 6e 64 65 64 20 72  ode} {extended r
82e0: 65 73 75 6c 74 20 63 6f 64 65 73 7d 0a 2a 2a 0a  esult codes}.**.
82f0: 2a 2a 20 49 6e 20 69 74 73 20 64 65 66 61 75 6c  ** In its defaul
8300: 74 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2c  t configuration,
8310: 20 53 51 4c 69 74 65 20 41 50 49 20 72 6f 75 74   SQLite API rout
8320: 69 6e 65 73 20 72 65 74 75 72 6e 20 6f 6e 65 20  ines return one 
8330: 6f 66 20 32 36 20 69 6e 74 65 67 65 72 0a 2a 2a  of 26 integer.**
8340: 20 5b 53 51 4c 49 54 45 5f 4f 4b 20 7c 20 72 65   [SQLITE_OK | re
8350: 73 75 6c 74 20 63 6f 64 65 73 5d 2e 20 20 48 6f  sult codes].  Ho
8360: 77 65 76 65 72 2c 20 65 78 70 65 72 69 65 6e 63  wever, experienc
8370: 65 20 68 61 73 20 73 68 6f 77 6e 20 74 68 61 74  e has shown that
8380: 20 6d 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 65 73   many of.** thes
8390: 65 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 61  e result codes a
83a0: 72 65 20 74 6f 6f 20 63 6f 61 72 73 65 2d 67 72  re too coarse-gr
83b0: 61 69 6e 65 64 2e 20 20 54 68 65 79 20 64 6f 20  ained.  They do 
83c0: 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 73 0a 2a  not provide as.*
83d0: 2a 20 6d 75 63 68 20 69 6e 66 6f 72 6d 61 74 69  * much informati
83e0: 6f 6e 20 61 62 6f 75 74 20 70 72 6f 62 6c 65 6d  on about problem
83f0: 73 20 61 73 20 70 72 6f 67 72 61 6d 6d 65 72 73  s as programmers
8400: 20 6d 69 67 68 74 20 6c 69 6b 65 2e 20 20 49 6e   might like.  In
8410: 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 0a 2a 2a   an effort to.**
8420: 20 61 64 64 72 65 73 73 20 74 68 69 73 2c 20 6e   address this, n
8430: 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20 6f 66  ewer versions of
8440: 20 53 51 4c 69 74 65 20 28 76 65 72 73 69 6f 6e   SQLite (version
8450: 20 33 2e 33 2e 38 20 61 6e 64 20 6c 61 74 65 72   3.3.8 and later
8460: 29 20 69 6e 63 6c 75 64 65 0a 2a 2a 20 73 75 70  ) include.** sup
8470: 70 6f 72 74 20 66 6f 72 20 61 64 64 69 74 69 6f  port for additio
8480: 6e 61 6c 20 72 65 73 75 6c 74 20 63 6f 64 65 73  nal result codes
8490: 20 74 68 61 74 20 70 72 6f 76 69 64 65 20 6d 6f   that provide mo
84a0: 72 65 20 64 65 74 61 69 6c 65 64 20 69 6e 66 6f  re detailed info
84b0: 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74  rmation.** about
84c0: 20 65 72 72 6f 72 73 2e 20 54 68 65 20 65 78 74   errors. The ext
84d0: 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64  ended result cod
84e0: 65 73 20 61 72 65 20 65 6e 61 62 6c 65 64 20 6f  es are enabled o
84f0: 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 6f 6e  r disabled.** on
8500: 20 61 20 70 65 72 20 64 61 74 61 62 61 73 65 20   a per database 
8510: 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69 73  connection basis
8520: 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 5b 73   using the.** [s
8530: 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f  qlite3_extended_
8540: 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 29 5d 20  result_codes()] 
8550: 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20  API..**.** Some 
8560: 6f 66 20 74 68 65 20 61 76 61 69 6c 61 62 6c 65  of the available
8570: 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74   extended result
8580: 20 63 6f 64 65 73 20 61 72 65 20 6c 69 73 74 65   codes are liste
8590: 64 20 68 65 72 65 2e 0a 2a 2a 20 4f 6e 65 20 6d  d here..** One m
85a0: 61 79 20 65 78 70 65 63 74 20 74 68 65 20 6e 75  ay expect the nu
85b0: 6d 62 65 72 20 6f 66 20 65 78 74 65 6e 64 65 64  mber of extended
85c0: 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 77 69   result codes wi
85d0: 6c 6c 20 62 65 20 65 78 70 61 6e 64 0a 2a 2a 20  ll be expand.** 
85e0: 6f 76 65 72 20 74 69 6d 65 2e 20 20 53 6f 66 74  over time.  Soft
85f0: 77 61 72 65 20 74 68 61 74 20 75 73 65 73 20 65  ware that uses e
8600: 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63  xtended result c
8610: 6f 64 65 73 20 73 68 6f 75 6c 64 20 65 78 70 65  odes should expe
8620: 63 74 0a 2a 2a 20 74 6f 20 73 65 65 20 6e 65 77  ct.** to see new
8630: 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 69 6e   result codes in
8640: 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73   future releases
8650: 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a   of SQLite..**.*
8660: 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4f 4b 20  * The SQLITE_OK 
8670: 72 65 73 75 6c 74 20 63 6f 64 65 20 77 69 6c 6c  result code will
8680: 20 6e 65 76 65 72 20 62 65 20 65 78 74 65 6e 64   never be extend
8690: 65 64 2e 20 20 49 74 20 77 69 6c 6c 20 61 6c 77  ed.  It will alw
86a0: 61 79 73 0a 2a 2a 20 62 65 20 65 78 61 63 74 6c  ays.** be exactl
86b0: 79 20 7a 65 72 6f 2e 0a 2a 2f 0a 23 64 65 66 69  y zero..*/.#defi
86c0: 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  ne SQLITE_IOERR_
86d0: 52 45 41 44 20 20 20 20 20 20 20 20 20 20 20 20  READ            
86e0: 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20    (SQLITE_IOERR 
86f0: 7c 20 28 31 3c 3c 38 29 29 0a 23 64 65 66 69 6e  | (1<<8)).#defin
8700: 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53  e SQLITE_IOERR_S
8710: 48 4f 52 54 5f 52 45 41 44 20 20 20 20 20 20 20  HORT_READ       
8720: 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c   (SQLITE_IOERR |
8730: 20 28 32 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65   (2<<8)).#define
8740: 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52   SQLITE_IOERR_WR
8750: 49 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ITE             
8760: 28 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20  (SQLITE_IOERR | 
8770: 28 33 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20  (3<<8)).#define 
8780: 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59  SQLITE_IOERR_FSY
8790: 4e 43 20 20 20 20 20 20 20 20 20 20 20 20 20 28  NC             (
87a0: 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28  SQLITE_IOERR | (
87b0: 34 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53  4<<8)).#define S
87c0: 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f  QLITE_IOERR_DIR_
87d0: 46 53 59 4e 43 20 20 20 20 20 20 20 20 20 28 53  FSYNC         (S
87e0: 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 35  QLITE_IOERR | (5
87f0: 3c 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51  <<8)).#define SQ
8800: 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43  LITE_IOERR_TRUNC
8810: 41 54 45 20 20 20 20 20 20 20 20 20 20 28 53 51  ATE          (SQ
8820: 4c 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 36 3c  LITE_IOERR | (6<
8830: 3c 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c  <8)).#define SQL
8840: 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 20  ITE_IOERR_FSTAT 
8850: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c              (SQL
8860: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 37 3c 3c  ITE_IOERR | (7<<
8870: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  8)).#define SQLI
8880: 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 20  TE_IOERR_UNLOCK 
8890: 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49             (SQLI
88a0: 54 45 5f 49 4f 45 52 52 20 7c 20 28 38 3c 3c 38  TE_IOERR | (8<<8
88b0: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
88c0: 45 5f 49 4f 45 52 52 5f 52 44 4c 4f 43 4b 20 20  E_IOERR_RDLOCK  
88d0: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
88e0: 45 5f 49 4f 45 52 52 20 7c 20 28 39 3c 3c 38 29  E_IOERR | (9<<8)
88f0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ).#define SQLITE
8900: 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 20 20 20  _IOERR_DELETE   
8910: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45           (SQLITE
8920: 5f 49 4f 45 52 52 20 7c 20 28 31 30 3c 3c 38 29  _IOERR | (10<<8)
8930: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ).#define SQLITE
8940: 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20 20  _IOERR_BLOCKED  
8950: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45           (SQLITE
8960: 5f 49 4f 45 52 52 20 7c 20 28 31 31 3c 3c 38 29  _IOERR | (11<<8)
8970: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ).#define SQLITE
8980: 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 20 20 20  _IOERR_NOMEM    
8990: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45           (SQLITE
89a0: 5f 49 4f 45 52 52 20 7c 20 28 31 32 3c 3c 38 29  _IOERR | (12<<8)
89b0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ).#define SQLITE
89c0: 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20 20 20  _IOERR_ACCESS   
89d0: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45           (SQLITE
89e0: 5f 49 4f 45 52 52 20 7c 20 28 31 33 3c 3c 38 29  _IOERR | (13<<8)
89f0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ).#define SQLITE
8a00: 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45  _IOERR_CHECKRESE
8a10: 52 56 45 44 4c 4f 43 4b 20 28 53 51 4c 49 54 45  RVEDLOCK (SQLITE
8a20: 5f 49 4f 45 52 52 20 7c 20 28 31 34 3c 3c 38 29  _IOERR | (14<<8)
8a30: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ).#define SQLITE
8a40: 5f 49 4f 45 52 52 5f 4c 4f 43 4b 20 20 20 20 20  _IOERR_LOCK     
8a50: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45           (SQLITE
8a60: 5f 49 4f 45 52 52 20 7c 20 28 31 35 3c 3c 38 29  _IOERR | (15<<8)
8a70: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ).#define SQLITE
8a80: 5f 49 4f 45 52 52 5f 43 4c 4f 53 45 20 20 20 20  _IOERR_CLOSE    
8a90: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45           (SQLITE
8aa0: 5f 49 4f 45 52 52 20 7c 20 28 31 36 3c 3c 38 29  _IOERR | (16<<8)
8ab0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ).#define SQLITE
8ac0: 5f 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45  _IOERR_DIR_CLOSE
8ad0: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45           (SQLITE
8ae0: 5f 49 4f 45 52 52 20 7c 20 28 31 37 3c 3c 38 29  _IOERR | (17<<8)
8af0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ).#define SQLITE
8b00: 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41  _LOCKED_SHAREDCA
8b10: 43 48 45 20 20 20 20 20 20 28 53 51 4c 49 54 45  CHE      (SQLITE
8b20: 5f 4c 4f 43 4b 45 44 20 7c 20 28 31 3c 3c 38 29  _LOCKED | (1<<8)
8b30: 20 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52   )../*.** CAPI3R
8b40: 45 46 3a 20 46 6c 61 67 73 20 46 6f 72 20 46 69  EF: Flags For Fi
8b50: 6c 65 20 4f 70 65 6e 20 4f 70 65 72 61 74 69 6f  le Open Operatio
8b60: 6e 73 20 7b 48 31 30 32 33 30 7d 20 3c 48 31 31  ns {H10230} <H11
8b70: 31 32 30 3e 20 3c 48 31 32 37 30 30 3e 0a 2a 2a  120> <H12700>.**
8b80: 0a 2a 2a 20 54 68 65 73 65 20 62 69 74 20 76 61  .** These bit va
8b90: 6c 75 65 73 20 61 72 65 20 69 6e 74 65 6e 64 65  lues are intende
8ba0: 64 20 66 6f 72 20 75 73 65 20 69 6e 20 74 68 65  d for use in the
8bb0: 0a 2a 2a 20 33 72 64 20 70 61 72 61 6d 65 74 65  .** 3rd paramete
8bc0: 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65  r to the [sqlite
8bd0: 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69 6e 74  3_open_v2()] int
8be0: 65 72 66 61 63 65 20 61 6e 64 0a 2a 2a 20 69 6e  erface and.** in
8bf0: 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74   the 4th paramet
8c00: 65 72 20 74 6f 20 74 68 65 20 78 4f 70 65 6e 20  er to the xOpen 
8c10: 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 0a 2a 2a  method of the.**
8c20: 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f   [sqlite3_vfs] o
8c30: 62 6a 65 63 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e  bject..*/.#defin
8c40: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  e SQLITE_OPEN_RE
8c50: 41 44 4f 4e 4c 59 20 20 20 20 20 20 20 20 20 30  ADONLY         0
8c60: 78 30 30 30 30 30 30 30 31 20 20 2f 2a 20 4f 6b  x00000001  /* Ok
8c70: 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65   for sqlite3_ope
8c80: 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e  n_v2() */.#defin
8c90: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  e SQLITE_OPEN_RE
8ca0: 41 44 57 52 49 54 45 20 20 20 20 20 20 20 20 30  ADWRITE        0
8cb0: 78 30 30 30 30 30 30 30 32 20 20 2f 2a 20 4f 6b  x00000002  /* Ok
8cc0: 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65   for sqlite3_ope
8cd0: 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e  n_v2() */.#defin
8ce0: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52  e SQLITE_OPEN_CR
8cf0: 45 41 54 45 20 20 20 20 20 20 20 20 20 20 20 30  EATE           0
8d00: 78 30 30 30 30 30 30 30 34 20 20 2f 2a 20 4f 6b  x00000004  /* Ok
8d10: 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65   for sqlite3_ope
8d20: 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e  n_v2() */.#defin
8d30: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45  e SQLITE_OPEN_DE
8d40: 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 20 20 20 30  LETEONCLOSE    0
8d50: 78 30 30 30 30 30 30 30 38 20 20 2f 2a 20 56 46  x00000008  /* VF
8d60: 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e  S only */.#defin
8d70: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58  e SQLITE_OPEN_EX
8d80: 43 4c 55 53 49 56 45 20 20 20 20 20 20 20 20 30  CLUSIVE        0
8d90: 78 30 30 30 30 30 30 31 30 20 20 2f 2a 20 56 46  x00000010  /* VF
8da0: 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e  S only */.#defin
8db0: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  e SQLITE_OPEN_MA
8dc0: 49 4e 5f 44 42 20 20 20 20 20 20 20 20 20 20 30  IN_DB          0
8dd0: 78 30 30 30 30 30 31 30 30 20 20 2f 2a 20 56 46  x00000100  /* VF
8de0: 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e  S only */.#defin
8df0: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45  e SQLITE_OPEN_TE
8e00: 4d 50 5f 44 42 20 20 20 20 20 20 20 20 20 20 30  MP_DB          0
8e10: 78 30 30 30 30 30 32 30 30 20 20 2f 2a 20 56 46  x00000200  /* VF
8e20: 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e  S only */.#defin
8e30: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 52  e SQLITE_OPEN_TR
8e40: 41 4e 53 49 45 4e 54 5f 44 42 20 20 20 20 20 30  ANSIENT_DB     0
8e50: 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20 56 46  x00000400  /* VF
8e60: 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e  S only */.#defin
8e70: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  e SQLITE_OPEN_MA
8e80: 49 4e 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20 30  IN_JOURNAL     0
8e90: 78 30 30 30 30 30 38 30 30 20 20 2f 2a 20 56 46  x00000800  /* VF
8ea0: 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e  S only */.#defin
8eb0: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45  e SQLITE_OPEN_TE
8ec0: 4d 50 5f 4a 4f 55 52 4e 41 4c 20 20 20 20 20 30  MP_JOURNAL     0
8ed0: 78 30 30 30 30 31 30 30 30 20 20 2f 2a 20 56 46  x00001000  /* VF
8ee0: 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e  S only */.#defin
8ef0: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55  e SQLITE_OPEN_SU
8f00: 42 4a 4f 55 52 4e 41 4c 20 20 20 20 20 20 20 30  BJOURNAL       0
8f10: 78 30 30 30 30 32 30 30 30 20 20 2f 2a 20 56 46  x00002000  /* VF
8f20: 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e  S only */.#defin
8f30: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  e SQLITE_OPEN_MA
8f40: 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 20 20 30  STER_JOURNAL   0
8f50: 78 30 30 30 30 34 30 30 30 20 20 2f 2a 20 56 46  x00004000  /* VF
8f60: 53 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69 6e  S only */.#defin
8f70: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f  e SQLITE_OPEN_NO
8f80: 4d 55 54 45 58 20 20 20 20 20 20 20 20 20 20 30  MUTEX          0
8f90: 78 30 30 30 30 38 30 30 30 20 20 2f 2a 20 4f 6b  x00008000  /* Ok
8fa0: 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65   for sqlite3_ope
8fb0: 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e  n_v2() */.#defin
8fc0: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55  e SQLITE_OPEN_FU
8fd0: 4c 4c 4d 55 54 45 58 20 20 20 20 20 20 20 20 30  LLMUTEX        0
8fe0: 78 30 30 30 31 30 30 30 30 20 20 2f 2a 20 4f 6b  x00010000  /* Ok
8ff0: 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65   for sqlite3_ope
9000: 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e  n_v2() */.#defin
9010: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48  e SQLITE_OPEN_SH
9020: 41 52 45 44 43 41 43 48 45 20 20 20 20 20 20 30  AREDCACHE      0
9030: 78 30 30 30 32 30 30 30 30 20 20 2f 2a 20 4f 6b  x00020000  /* Ok
9040: 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65   for sqlite3_ope
9050: 6e 5f 76 32 28 29 20 2a 2f 0a 23 64 65 66 69 6e  n_v2() */.#defin
9060: 65 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 50 52  e SQLITE_OPEN_PR
9070: 49 56 41 54 45 43 41 43 48 45 20 20 20 20 20 30  IVATECACHE     0
9080: 78 30 30 30 34 30 30 30 30 20 20 2f 2a 20 4f 6b  x00040000  /* Ok
9090: 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65   for sqlite3_ope
90a0: 6e 5f 76 32 28 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  n_v2() */../*.**
90b0: 20 43 41 50 49 33 52 45 46 3a 20 44 65 76 69 63   CAPI3REF: Devic
90c0: 65 20 43 68 61 72 61 63 74 65 72 69 73 74 69 63  e Characteristic
90d0: 73 20 7b 48 31 30 32 34 30 7d 20 3c 48 31 31 31  s {H10240} <H111
90e0: 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 44  20>.**.** The xD
90f0: 65 76 69 63 65 43 61 70 61 62 69 6c 69 74 69 65  eviceCapabilitie
9100: 73 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20  s method of the 
9110: 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68  [sqlite3_io_meth
9120: 6f 64 73 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 72  ods].** object r
9130: 65 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67 65  eturns an intege
9140: 72 20 77 68 69 63 68 20 69 73 20 61 20 76 65 63  r which is a vec
9150: 74 6f 72 20 6f 66 20 74 68 65 20 74 68 65 73 65  tor of the these
9160: 0a 2a 2a 20 62 69 74 20 76 61 6c 75 65 73 20 65  .** bit values e
9170: 78 70 72 65 73 73 69 6e 67 20 49 2f 4f 20 63 68  xpressing I/O ch
9180: 61 72 61 63 74 65 72 69 73 74 69 63 73 20 6f 66  aracteristics of
9190: 20 74 68 65 20 6d 61 73 73 20 73 74 6f 72 61 67   the mass storag
91a0: 65 0a 2a 2a 20 64 65 76 69 63 65 20 74 68 61 74  e.** device that
91b0: 20 68 6f 6c 64 73 20 74 68 65 20 66 69 6c 65 20   holds the file 
91c0: 74 68 61 74 20 74 68 65 20 5b 73 71 6c 69 74 65  that the [sqlite
91d0: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a  3_io_methods].**
91e0: 20 72 65 66 65 72 73 20 74 6f 2e 0a 2a 2a 0a 2a   refers to..**.*
91f0: 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43  * The SQLITE_IOC
9200: 41 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65 72  AP_ATOMIC proper
9210: 74 79 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6c  ty means that al
9220: 6c 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20 61  l writes of.** a
9230: 6e 79 20 73 69 7a 65 20 61 72 65 20 61 74 6f 6d  ny size are atom
9240: 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f  ic.  The SQLITE_
9250: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e 20  IOCAP_ATOMICnnn 
9260: 76 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20 74  values.** mean t
9270: 68 61 74 20 77 72 69 74 65 73 20 6f 66 20 62 6c  hat writes of bl
9280: 6f 63 6b 73 20 74 68 61 74 20 61 72 65 20 6e 6e  ocks that are nn
9290: 6e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20  n bytes in size 
92a0: 61 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67 6e  and.** are align
92b0: 65 64 20 74 6f 20 61 6e 20 61 64 64 72 65 73 73  ed to an address
92c0: 20 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e 74   which is an int
92d0: 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66  eger multiple of
92e0: 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f 6d  .** nnn are atom
92f0: 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f  ic.  The SQLITE_
9300: 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e  IOCAP_SAFE_APPEN
9310: 44 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a 2a  D value means.**
9320: 20 74 68 61 74 20 77 68 65 6e 20 64 61 74 61 20   that when data 
9330: 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 61  is appended to a
9340: 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61 20   file, the data 
9350: 69 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 66  is appended.** f
9360: 69 72 73 74 20 74 68 65 6e 20 74 68 65 20 73 69  irst then the si
9370: 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69  ze of the file i
9380: 73 20 65 78 74 65 6e 64 65 64 2c 20 6e 65 76 65  s extended, neve
9390: 72 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 77  r the other.** w
93a0: 61 79 20 61 72 6f 75 6e 64 2e 20 20 54 68 65 20  ay around.  The 
93b0: 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51  SQLITE_IOCAP_SEQ
93c0: 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74 79  UENTIAL property
93d0: 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 69   means that.** i
93e0: 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77 72  nformation is wr
93f0: 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 69 6e  itten to disk in
9400: 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72 20   the same order 
9410: 61 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 78  as calls.** to x
9420: 57 72 69 74 65 28 29 2e 0a 2a 2f 0a 23 64 65 66  Write()..*/.#def
9430: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  ine SQLITE_IOCAP
9440: 5f 41 54 4f 4d 49 43 20 20 20 20 20 20 20 20 20  _ATOMIC         
9450: 20 30 78 30 30 30 30 30 30 30 31 0a 23 64 65 66   0x00000001.#def
9460: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  ine SQLITE_IOCAP
9470: 5f 41 54 4f 4d 49 43 35 31 32 20 20 20 20 20 20  _ATOMIC512      
9480: 20 30 78 30 30 30 30 30 30 30 32 0a 23 64 65 66   0x00000002.#def
9490: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  ine SQLITE_IOCAP
94a0: 5f 41 54 4f 4d 49 43 31 4b 20 20 20 20 20 20 20  _ATOMIC1K       
94b0: 20 30 78 30 30 30 30 30 30 30 34 0a 23 64 65 66   0x00000004.#def
94c0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  ine SQLITE_IOCAP
94d0: 5f 41 54 4f 4d 49 43 32 4b 20 20 20 20 20 20 20  _ATOMIC2K       
94e0: 20 30 78 30 30 30 30 30 30 30 38 0a 23 64 65 66   0x00000008.#def
94f0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  ine SQLITE_IOCAP
9500: 5f 41 54 4f 4d 49 43 34 4b 20 20 20 20 20 20 20  _ATOMIC4K       
9510: 20 30 78 30 30 30 30 30 30 31 30 0a 23 64 65 66   0x00000010.#def
9520: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  ine SQLITE_IOCAP
9530: 5f 41 54 4f 4d 49 43 38 4b 20 20 20 20 20 20 20  _ATOMIC8K       
9540: 20 30 78 30 30 30 30 30 30 32 30 0a 23 64 65 66   0x00000020.#def
9550: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  ine SQLITE_IOCAP
9560: 5f 41 54 4f 4d 49 43 31 36 4b 20 20 20 20 20 20  _ATOMIC16K      
9570: 20 30 78 30 30 30 30 30 30 34 30 0a 23 64 65 66   0x00000040.#def
9580: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  ine SQLITE_IOCAP
9590: 5f 41 54 4f 4d 49 43 33 32 4b 20 20 20 20 20 20  _ATOMIC32K      
95a0: 20 30 78 30 30 30 30 30 30 38 30 0a 23 64 65 66   0x00000080.#def
95b0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  ine SQLITE_IOCAP
95c0: 5f 41 54 4f 4d 49 43 36 34 4b 20 20 20 20 20 20  _ATOMIC64K      
95d0: 20 30 78 30 30 30 30 30 31 30 30 0a 23 64 65 66   0x00000100.#def
95e0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  ine SQLITE_IOCAP
95f0: 5f 53 41 46 45 5f 41 50 50 45 4e 44 20 20 20 20  _SAFE_APPEND    
9600: 20 30 78 30 30 30 30 30 32 30 30 0a 23 64 65 66   0x00000200.#def
9610: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  ine SQLITE_IOCAP
9620: 5f 53 45 51 55 45 4e 54 49 41 4c 20 20 20 20 20  _SEQUENTIAL     
9630: 20 30 78 30 30 30 30 30 34 30 30 0a 0a 2f 2a 0a   0x00000400../*.
9640: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 6c  ** CAPI3REF: Fil
9650: 65 20 4c 6f 63 6b 69 6e 67 20 4c 65 76 65 6c 73  e Locking Levels
9660: 20 7b 48 31 30 32 35 30 7d 20 3c 48 31 31 31 32   {H10250} <H1112
9670: 30 3e 20 3c 48 31 31 33 31 30 3e 0a 2a 2a 0a 2a  0> <H11310>.**.*
9680: 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 6f 6e  * SQLite uses on
9690: 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67  e of these integ
96a0: 65 72 20 76 61 6c 75 65 73 20 61 73 20 74 68 65  er values as the
96b0: 20 73 65 63 6f 6e 64 0a 2a 2a 20 61 72 67 75 6d   second.** argum
96c0: 65 6e 74 20 74 6f 20 63 61 6c 6c 73 20 69 74 20  ent to calls it 
96d0: 6d 61 6b 65 73 20 74 6f 20 74 68 65 20 78 4c 6f  makes to the xLo
96e0: 63 6b 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63 6b  ck() and xUnlock
96f0: 28 29 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66  () methods.** of
9700: 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f   an [sqlite3_io_
9710: 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 2e  methods] object.
9720: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
9730: 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 20 20 20 20  TE_LOCK_NONE    
9740: 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20        0.#define 
9750: 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 53 48 41 52  SQLITE_LOCK_SHAR
9760: 45 44 20 20 20 20 20 20 20 20 31 0a 23 64 65 66  ED        1.#def
9770: 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f  ine SQLITE_LOCK_
9780: 52 45 53 45 52 56 45 44 20 20 20 20 20 20 32 0a  RESERVED      2.
9790: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
97a0: 4f 43 4b 5f 50 45 4e 44 49 4e 47 20 20 20 20 20  OCK_PENDING     
97b0: 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    3.#define SQLI
97c0: 54 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56  TE_LOCK_EXCLUSIV
97d0: 45 20 20 20 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 43  E     4../*.** C
97e0: 41 50 49 33 52 45 46 3a 20 53 79 6e 63 68 72 6f  API3REF: Synchro
97f0: 6e 69 7a 61 74 69 6f 6e 20 54 79 70 65 20 46 6c  nization Type Fl
9800: 61 67 73 20 7b 48 31 30 32 36 30 7d 20 3c 48 31  ags {H10260} <H1
9810: 31 31 32 30 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e  1120>.**.** When
9820: 20 53 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20   SQLite invokes 
9830: 74 68 65 20 78 53 79 6e 63 28 29 20 6d 65 74 68  the xSync() meth
9840: 6f 64 20 6f 66 20 61 6e 0a 2a 2a 20 5b 73 71 6c  od of an.** [sql
9850: 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d  ite3_io_methods]
9860: 20 6f 62 6a 65 63 74 20 69 74 20 75 73 65 73 20   object it uses 
9870: 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66  a combination of
9880: 0a 2a 2a 20 74 68 65 73 65 20 69 6e 74 65 67 65  .** these intege
9890: 72 20 76 61 6c 75 65 73 20 61 73 20 74 68 65 20  r values as the 
98a0: 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e  second argument.
98b0: 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20  .**.** When the 
98c0: 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41  SQLITE_SYNC_DATA
98d0: 4f 4e 4c 59 20 66 6c 61 67 20 69 73 20 75 73 65  ONLY flag is use
98e0: 64 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  d, it means that
98f0: 20 74 68 65 0a 2a 2a 20 73 79 6e 63 20 6f 70 65   the.** sync ope
9900: 72 61 74 69 6f 6e 20 6f 6e 6c 79 20 6e 65 65 64  ration only need
9910: 73 20 74 6f 20 66 6c 75 73 68 20 64 61 74 61 20  s to flush data 
9920: 74 6f 20 6d 61 73 73 20 73 74 6f 72 61 67 65 2e  to mass storage.
9930: 20 20 49 6e 6f 64 65 0a 2a 2a 20 69 6e 66 6f 72    Inode.** infor
9940: 6d 61 74 69 6f 6e 20 6e 65 65 64 20 6e 6f 74 20  mation need not 
9950: 62 65 20 66 6c 75 73 68 65 64 2e 20 49 66 20 74  be flushed. If t
9960: 68 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 69  he lower four bi
9970: 74 73 20 6f 66 20 74 68 65 20 66 6c 61 67 0a 2a  ts of the flag.*
9980: 2a 20 65 71 75 61 6c 20 53 51 4c 49 54 45 5f 53  * equal SQLITE_S
9990: 59 4e 43 5f 4e 4f 52 4d 41 4c 2c 20 74 68 61 74  YNC_NORMAL, that
99a0: 20 6d 65 61 6e 73 20 74 6f 20 75 73 65 20 6e 6f   means to use no
99b0: 72 6d 61 6c 20 66 73 79 6e 63 28 29 20 73 65 6d  rmal fsync() sem
99c0: 61 6e 74 69 63 73 2e 0a 2a 2a 20 49 66 20 74 68  antics..** If th
99d0: 65 20 6c 6f 77 65 72 20 66 6f 75 72 20 62 69 74  e lower four bit
99e0: 73 20 65 71 75 61 6c 20 53 51 4c 49 54 45 5f 53  s equal SQLITE_S
99f0: 59 4e 43 5f 46 55 4c 4c 2c 20 74 68 61 74 20 6d  YNC_FULL, that m
9a00: 65 61 6e 73 0a 2a 2a 20 74 6f 20 75 73 65 20 4d  eans.** to use M
9a10: 61 63 20 4f 53 20 58 20 73 74 79 6c 65 20 66 75  ac OS X style fu
9a20: 6c 6c 73 79 6e 63 20 69 6e 73 74 65 61 64 20 6f  llsync instead o
9a30: 66 20 66 73 79 6e 63 28 29 2e 0a 2a 2f 0a 23 64  f fsync()..*/.#d
9a40: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 4e  efine SQLITE_SYN
9a50: 43 5f 4e 4f 52 4d 41 4c 20 20 20 20 20 20 20 20  C_NORMAL        
9a60: 30 78 30 30 30 30 32 0a 23 64 65 66 69 6e 65 20  0x00002.#define 
9a70: 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c  SQLITE_SYNC_FULL
9a80: 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30            0x0000
9a90: 33 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  3.#define SQLITE
9aa0: 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 20  _SYNC_DATAONLY  
9ab0: 20 20 20 20 30 78 30 30 30 31 30 0a 0a 2f 2a 0a      0x00010../*.
9ac0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20  ** CAPI3REF: OS 
9ad0: 49 6e 74 65 72 66 61 63 65 20 4f 70 65 6e 20 46  Interface Open F
9ae0: 69 6c 65 20 48 61 6e 64 6c 65 20 7b 48 31 31 31  ile Handle {H111
9af0: 31 30 7d 20 3c 53 32 30 31 31 30 3e 0a 2a 2a 0a  10} <S20110>.**.
9b00: 2a 2a 20 41 6e 20 5b 73 71 6c 69 74 65 33 5f 66  ** An [sqlite3_f
9b10: 69 6c 65 5d 20 6f 62 6a 65 63 74 20 72 65 70 72  ile] object repr
9b20: 65 73 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20 66  esents an open f
9b30: 69 6c 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 5b  ile in the .** [
9b40: 73 71 6c 69 74 65 33 5f 76 66 73 20 7c 20 4f 53  sqlite3_vfs | OS
9b50: 20 69 6e 74 65 72 66 61 63 65 20 6c 61 79 65 72   interface layer
9b60: 5d 2e 20 20 49 6e 64 69 76 69 64 75 61 6c 20 4f  ].  Individual O
9b70: 53 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 69  S interface.** i
9b80: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77  mplementations w
9b90: 69 6c 6c 0a 2a 2a 20 77 61 6e 74 20 74 6f 20 73  ill.** want to s
9ba0: 75 62 63 6c 61 73 73 20 74 68 69 73 20 6f 62 6a  ubclass this obj
9bb0: 65 63 74 20 62 79 20 61 70 70 65 6e 64 69 6e 67  ect by appending
9bc0: 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c   additional fiel
9bd0: 64 73 0a 2a 2a 20 66 6f 72 20 74 68 65 69 72 20  ds.** for their 
9be0: 6f 77 6e 20 75 73 65 2e 20 20 54 68 65 20 70 4d  own use.  The pM
9bf0: 65 74 68 6f 64 73 20 65 6e 74 72 79 20 69 73 20  ethods entry is 
9c00: 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a  a pointer to an.
9c10: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  ** [sqlite3_io_m
9c20: 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 74  ethods] object t
9c30: 68 61 74 20 64 65 66 69 6e 65 73 20 6d 65 74 68  hat defines meth
9c40: 6f 64 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 69  ods for performi
9c50: 6e 67 0a 2a 2a 20 49 2f 4f 20 6f 70 65 72 61 74  ng.** I/O operat
9c60: 69 6f 6e 73 20 6f 6e 20 74 68 65 20 6f 70 65 6e  ions on the open
9c70: 20 66 69 6c 65 2e 0a 2a 2f 0a 74 79 70 65 64 65   file..*/.typede
9c80: 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
9c90: 5f 66 69 6c 65 20 73 71 6c 69 74 65 33 5f 66 69  _file sqlite3_fi
9ca0: 6c 65 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74  le;.struct sqlit
9cb0: 65 33 5f 66 69 6c 65 20 7b 0a 20 20 63 6f 6e 73  e3_file {.  cons
9cc0: 74 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  t struct sqlite3
9cd0: 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65  _io_methods *pMe
9ce0: 74 68 6f 64 73 3b 20 20 2f 2a 20 4d 65 74 68 6f  thods;  /* Metho
9cf0: 64 73 20 66 6f 72 20 61 6e 20 6f 70 65 6e 20 66  ds for an open f
9d00: 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ile */.};../*.**
9d10: 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20 49 6e   CAPI3REF: OS In
9d20: 74 65 72 66 61 63 65 20 46 69 6c 65 20 56 69 72  terface File Vir
9d30: 74 75 61 6c 20 4d 65 74 68 6f 64 73 20 4f 62 6a  tual Methods Obj
9d40: 65 63 74 20 7b 48 31 31 31 32 30 7d 20 3c 53 32  ect {H11120} <S2
9d50: 30 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 45 76 65 72  0110>.**.** Ever
9d60: 79 20 66 69 6c 65 20 6f 70 65 6e 65 64 20 62 79  y file opened by
9d70: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66   the [sqlite3_vf
9d80: 73 5d 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20  s] xOpen method 
9d90: 70 6f 70 75 6c 61 74 65 73 20 61 6e 0a 2a 2a 20  populates an.** 
9da0: 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f  [sqlite3_file] o
9db0: 62 6a 65 63 74 20 28 6f 72 2c 20 6d 6f 72 65 20  bject (or, more 
9dc0: 63 6f 6d 6d 6f 6e 6c 79 2c 20 61 20 73 75 62 63  commonly, a subc
9dd0: 6c 61 73 73 20 6f 66 20 74 68 65 0a 2a 2a 20 5b  lass of the.** [
9de0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62  sqlite3_file] ob
9df0: 6a 65 63 74 29 20 77 69 74 68 20 61 20 70 6f 69  ject) with a poi
9e00: 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61  nter to an insta
9e10: 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65  nce of this obje
9e20: 63 74 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65  ct..** This obje
9e30: 63 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 6d  ct defines the m
9e40: 65 74 68 6f 64 73 20 75 73 65 64 20 74 6f 20 70  ethods used to p
9e50: 65 72 66 6f 72 6d 20 76 61 72 69 6f 75 73 20 6f  erform various o
9e60: 70 65 72 61 74 69 6f 6e 73 0a 2a 2a 20 61 67 61  perations.** aga
9e70: 69 6e 73 74 20 74 68 65 20 6f 70 65 6e 20 66 69  inst the open fi
9e80: 6c 65 20 72 65 70 72 65 73 65 6e 74 65 64 20 62  le represented b
9e90: 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 66  y the [sqlite3_f
9ea0: 69 6c 65 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a  ile] object..**.
9eb0: 2a 2a 20 49 66 20 74 68 65 20 78 4f 70 65 6e 20  ** If the xOpen 
9ec0: 6d 65 74 68 6f 64 20 73 65 74 73 20 74 68 65 20  method sets the 
9ed0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70 4d 65  sqlite3_file.pMe
9ee0: 74 68 6f 64 73 20 65 6c 65 6d 65 6e 74 20 0a 2a  thods element .*
9ef0: 2a 20 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20  * to a non-NULL 
9f00: 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20 74 68  pointer, then th
9f10: 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  e sqlite3_io_met
9f20: 68 6f 64 73 2e 78 43 6c 6f 73 65 20 6d 65 74 68  hods.xClose meth
9f30: 6f 64 0a 2a 2a 20 6d 61 79 20 62 65 20 69 6e 76  od.** may be inv
9f40: 6f 6b 65 64 20 65 76 65 6e 20 69 66 20 74 68 65  oked even if the
9f50: 20 78 4f 70 65 6e 20 72 65 70 6f 72 74 65 64 20   xOpen reported 
9f60: 74 68 61 74 20 69 74 20 66 61 69 6c 65 64 2e 20  that it failed. 
9f70: 20 54 68 65 0a 2a 2a 20 6f 6e 6c 79 20 77 61 79   The.** only way
9f80: 20 74 6f 20 70 72 65 76 65 6e 74 20 61 20 63 61   to prevent a ca
9f90: 6c 6c 20 74 6f 20 78 43 6c 6f 73 65 20 66 6f 6c  ll to xClose fol
9fa0: 6c 6f 77 69 6e 67 20 61 20 66 61 69 6c 65 64 20  lowing a failed 
9fb0: 78 4f 70 65 6e 0a 2a 2a 20 69 73 20 66 6f 72 20  xOpen.** is for 
9fc0: 74 68 65 20 78 4f 70 65 6e 20 74 6f 20 73 65 74  the xOpen to set
9fd0: 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c   the sqlite3_fil
9fe0: 65 2e 70 4d 65 74 68 6f 64 73 20 65 6c 65 6d 65  e.pMethods eleme
9ff0: 6e 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  nt to NULL..**.*
a000: 2a 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75  * The flags argu
a010: 6d 65 6e 74 20 74 6f 20 78 53 79 6e 63 20 6d 61  ment to xSync ma
a020: 79 20 62 65 20 6f 6e 65 20 6f 66 20 5b 53 51 4c  y be one of [SQL
a030: 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 5d  ITE_SYNC_NORMAL]
a040: 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 53   or.** [SQLITE_S
a050: 59 4e 43 5f 46 55 4c 4c 5d 2e 20 20 54 68 65 20  YNC_FULL].  The 
a060: 66 69 72 73 74 20 63 68 6f 69 63 65 20 69 73 20  first choice is 
a070: 74 68 65 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63  the normal fsync
a080: 28 29 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e  ()..** The secon
a090: 64 20 63 68 6f 69 63 65 20 69 73 20 61 20 4d 61  d choice is a Ma
a0a0: 63 20 4f 53 20 58 20 73 74 79 6c 65 20 66 75 6c  c OS X style ful
a0b0: 6c 73 79 6e 63 2e 20 20 54 68 65 20 5b 53 51 4c  lsync.  The [SQL
a0c0: 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c  ITE_SYNC_DATAONL
a0d0: 59 5d 0a 2a 2a 20 66 6c 61 67 20 6d 61 79 20 62  Y].** flag may b
a0e0: 65 20 4f 52 65 64 20 69 6e 20 74 6f 20 69 6e 64  e ORed in to ind
a0f0: 69 63 61 74 65 20 74 68 61 74 20 6f 6e 6c 79 20  icate that only 
a100: 74 68 65 20 64 61 74 61 20 6f 66 20 74 68 65 20  the data of the 
a110: 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 6e 6f 74 20  file.** and not 
a120: 69 74 73 20 69 6e 6f 64 65 20 6e 65 65 64 73 20  its inode needs 
a130: 74 6f 20 62 65 20 73 79 6e 63 65 64 2e 0a 2a 2a  to be synced..**
a140: 0a 2a 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20  .** The integer 
a150: 76 61 6c 75 65 73 20 74 6f 20 78 4c 6f 63 6b 28  values to xLock(
a160: 29 20 61 6e 64 20 78 55 6e 6c 6f 63 6b 28 29 20  ) and xUnlock() 
a170: 61 72 65 20 6f 6e 65 20 6f 66 0a 2a 2a 20 3c 75  are one of.** <u
a180: 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49  l>.** <li> [SQLI
a190: 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d 2c 0a 2a  TE_LOCK_NONE],.*
a1a0: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c  * <li> [SQLITE_L
a1b0: 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a 20  OCK_SHARED],.** 
a1c0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43  <li> [SQLITE_LOC
a1d0: 4b 5f 52 45 53 45 52 56 45 44 5d 2c 0a 2a 2a 20  K_RESERVED],.** 
a1e0: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c 4f 43  <li> [SQLITE_LOC
a1f0: 4b 5f 50 45 4e 44 49 4e 47 5d 2c 20 6f 72 0a 2a  K_PENDING], or.*
a200: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4c  * <li> [SQLITE_L
a210: 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45 5d 2e 0a  OCK_EXCLUSIVE]..
a220: 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 20 78 4c 6f 63  ** </ul>.** xLoc
a230: 6b 28 29 20 69 6e 63 72 65 61 73 65 73 20 74 68  k() increases th
a240: 65 20 6c 6f 63 6b 2e 20 78 55 6e 6c 6f 63 6b 28  e lock. xUnlock(
a250: 29 20 64 65 63 72 65 61 73 65 73 20 74 68 65 20  ) decreases the 
a260: 6c 6f 63 6b 2e 0a 2a 2a 20 54 68 65 20 78 43 68  lock..** The xCh
a270: 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
a280: 29 20 6d 65 74 68 6f 64 20 63 68 65 63 6b 73 20  ) method checks 
a290: 77 68 65 74 68 65 72 20 61 6e 79 20 64 61 74 61  whether any data
a2a0: 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c  base connection,
a2b0: 0a 2a 2a 20 65 69 74 68 65 72 20 69 6e 20 74 68  .** either in th
a2c0: 69 73 20 70 72 6f 63 65 73 73 20 6f 72 20 69 6e  is process or in
a2d0: 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63   some other proc
a2e0: 65 73 73 2c 20 69 73 20 68 6f 6c 64 69 6e 67 20  ess, is holding 
a2f0: 61 20 52 45 53 45 52 56 45 44 2c 0a 2a 2a 20 50  a RESERVED,.** P
a300: 45 4e 44 49 4e 47 2c 20 6f 72 20 45 58 43 4c 55  ENDING, or EXCLU
a310: 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  SIVE lock on the
a320: 20 66 69 6c 65 2e 20 20 49 74 20 72 65 74 75 72   file.  It retur
a330: 6e 73 20 74 72 75 65 0a 2a 2a 20 69 66 20 73 75  ns true.** if su
a340: 63 68 20 61 20 6c 6f 63 6b 20 65 78 69 73 74 73  ch a lock exists
a350: 20 61 6e 64 20 66 61 6c 73 65 20 6f 74 68 65 72   and false other
a360: 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  wise..**.** The 
a370: 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 29 20 6d  xFileControl() m
a380: 65 74 68 6f 64 20 69 73 20 61 20 67 65 6e 65 72  ethod is a gener
a390: 69 63 20 69 6e 74 65 72 66 61 63 65 20 74 68 61  ic interface tha
a3a0: 74 20 61 6c 6c 6f 77 73 20 63 75 73 74 6f 6d 0a  t allows custom.
a3b0: 2a 2a 20 56 46 53 20 69 6d 70 6c 65 6d 65 6e 74  ** VFS implement
a3c0: 61 74 69 6f 6e 73 20 74 6f 20 64 69 72 65 63 74  ations to direct
a3d0: 6c 79 20 63 6f 6e 74 72 6f 6c 20 61 6e 20 6f 70  ly control an op
a3e0: 65 6e 20 66 69 6c 65 20 75 73 69 6e 67 20 74 68  en file using th
a3f0: 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69  e.** [sqlite3_fi
a400: 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69 6e  le_control()] in
a410: 74 65 72 66 61 63 65 2e 20 20 54 68 65 20 73 65  terface.  The se
a420: 63 6f 6e 64 20 22 6f 70 22 20 61 72 67 75 6d 65  cond "op" argume
a430: 6e 74 20 69 73 20 61 6e 0a 2a 2a 20 69 6e 74 65  nt is an.** inte
a440: 67 65 72 20 6f 70 63 6f 64 65 2e 20 20 54 68 65  ger opcode.  The
a450: 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20   third argument 
a460: 69 73 20 61 20 67 65 6e 65 72 69 63 20 70 6f 69  is a generic poi
a470: 6e 74 65 72 20 69 6e 74 65 6e 64 65 64 20 74 6f  nter intended to
a480: 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 61 20 73  .** point to a s
a490: 74 72 75 63 74 75 72 65 20 74 68 61 74 20 6d 61  tructure that ma
a4a0: 79 20 63 6f 6e 74 61 69 6e 20 61 72 67 75 6d 65  y contain argume
a4b0: 6e 74 73 20 6f 72 20 73 70 61 63 65 20 69 6e 20  nts or space in 
a4c0: 77 68 69 63 68 20 74 6f 0a 2a 2a 20 77 72 69 74  which to.** writ
a4d0: 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73 2e  e return values.
a4e0: 20 20 50 6f 74 65 6e 74 69 61 6c 20 75 73 65 73    Potential uses
a4f0: 20 66 6f 72 20 78 46 69 6c 65 43 6f 6e 74 72 6f   for xFileContro
a500: 6c 28 29 20 6d 69 67 68 74 20 62 65 0a 2a 2a 20  l() might be.** 
a510: 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20 65 6e 61  functions to ena
a520: 62 6c 65 20 62 6c 6f 63 6b 69 6e 67 20 6c 6f 63  ble blocking loc
a530: 6b 73 20 77 69 74 68 20 74 69 6d 65 6f 75 74 73  ks with timeouts
a540: 2c 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 0a  , to change the.
a550: 2a 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74  ** locking strat
a560: 65 67 79 20 28 66 6f 72 20 65 78 61 6d 70 6c 65  egy (for example
a570: 20 74 6f 20 75 73 65 20 64 6f 74 2d 66 69 6c 65   to use dot-file
a580: 20 6c 6f 63 6b 73 29 2c 20 74 6f 20 69 6e 71 75   locks), to inqu
a590: 69 72 65 0a 2a 2a 20 61 62 6f 75 74 20 74 68 65  ire.** about the
a5a0: 20 73 74 61 74 75 73 20 6f 66 20 61 20 6c 6f 63   status of a loc
a5b0: 6b 2c 20 6f 72 20 74 6f 20 62 72 65 61 6b 20 73  k, or to break s
a5c0: 74 61 6c 65 20 6c 6f 63 6b 73 2e 20 20 54 68 65  tale locks.  The
a5d0: 20 53 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65 20   SQLite.** core 
a5e0: 72 65 73 65 72 76 65 73 20 61 6c 6c 20 6f 70 63  reserves all opc
a5f0: 6f 64 65 73 20 6c 65 73 73 20 74 68 61 6e 20 31  odes less than 1
a600: 30 30 20 66 6f 72 20 69 74 73 20 6f 77 6e 20 75  00 for its own u
a610: 73 65 2e 0a 2a 2a 20 41 20 5b 53 51 4c 49 54 45  se..** A [SQLITE
a620: 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45  _FCNTL_LOCKSTATE
a630: 20 7c 20 6c 69 73 74 20 6f 66 20 6f 70 63 6f 64   | list of opcod
a640: 65 73 5d 20 6c 65 73 73 20 74 68 61 6e 20 31 30  es] less than 10
a650: 30 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a  0 is available..
a660: 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20  ** Applications 
a670: 74 68 61 74 20 64 65 66 69 6e 65 20 61 20 63 75  that define a cu
a680: 73 74 6f 6d 20 78 46 69 6c 65 43 6f 6e 74 72 6f  stom xFileContro
a690: 6c 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20  l method should 
a6a0: 75 73 65 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 67  use opcodes.** g
a6b0: 72 65 61 74 65 72 20 74 68 61 6e 20 31 30 30 20  reater than 100 
a6c0: 74 6f 20 61 76 6f 69 64 20 63 6f 6e 66 6c 69 63  to avoid conflic
a6d0: 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 53  ts..**.** The xS
a6e0: 65 63 74 6f 72 53 69 7a 65 28 29 20 6d 65 74 68  ectorSize() meth
a6f0: 6f 64 20 72 65 74 75 72 6e 73 20 74 68 65 20 73  od returns the s
a700: 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 74 68  ector size of th
a710: 65 0a 2a 2a 20 64 65 76 69 63 65 20 74 68 61 74  e.** device that
a720: 20 75 6e 64 65 72 6c 69 65 73 20 74 68 65 20 66   underlies the f
a730: 69 6c 65 2e 20 20 54 68 65 20 73 65 63 74 6f 72  ile.  The sector
a740: 20 73 69 7a 65 20 69 73 20 74 68 65 0a 2a 2a 20   size is the.** 
a750: 6d 69 6e 69 6d 75 6d 20 77 72 69 74 65 20 74 68  minimum write th
a760: 61 74 20 63 61 6e 20 62 65 20 70 65 72 66 6f 72  at can be perfor
a770: 6d 65 64 20 77 69 74 68 6f 75 74 20 64 69 73 74  med without dist
a780: 75 72 62 69 6e 67 0a 2a 2a 20 6f 74 68 65 72 20  urbing.** other 
a790: 62 79 74 65 73 20 69 6e 20 74 68 65 20 66 69 6c  bytes in the fil
a7a0: 65 2e 20 20 54 68 65 20 78 44 65 76 69 63 65 43  e.  The xDeviceC
a7b0: 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 29  haracteristics()
a7c0: 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 65 74 75 72  .** method retur
a7d0: 6e 73 20 61 20 62 69 74 20 76 65 63 74 6f 72 20  ns a bit vector 
a7e0: 64 65 73 63 72 69 62 69 6e 67 20 62 65 68 61 76  describing behav
a7f0: 69 6f 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 75  iors of the.** u
a800: 6e 64 65 72 6c 79 69 6e 67 20 64 65 76 69 63 65  nderlying device
a810: 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  :.**.** <ul>.** 
a820: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
a830: 41 50 5f 41 54 4f 4d 49 43 5d 0a 2a 2a 20 3c 6c  AP_ATOMIC].** <l
a840: 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41 50  i> [SQLITE_IOCAP
a850: 5f 41 54 4f 4d 49 43 35 31 32 5d 0a 2a 2a 20 3c  _ATOMIC512].** <
a860: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41  li> [SQLITE_IOCA
a870: 50 5f 41 54 4f 4d 49 43 31 4b 5d 0a 2a 2a 20 3c  P_ATOMIC1K].** <
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 32 4b 5d 0a 2a 2a 20 3c  P_ATOMIC2K].** <
a8a0: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41  li> [SQLITE_IOCA
a8b0: 50 5f 41 54 4f 4d 49 43 34 4b 5d 0a 2a 2a 20 3c  P_ATOMIC4K].** <
a8c0: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41  li> [SQLITE_IOCA
a8d0: 50 5f 41 54 4f 4d 49 43 38 4b 5d 0a 2a 2a 20 3c  P_ATOMIC8K].** <
a8e0: 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43 41  li> [SQLITE_IOCA
a8f0: 50 5f 41 54 4f 4d 49 43 31 36 4b 5d 0a 2a 2a 20  P_ATOMIC16K].** 
a900: 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f 43  <li> [SQLITE_IOC
a910: 41 50 5f 41 54 4f 4d 49 43 33 32 4b 5d 0a 2a 2a  AP_ATOMIC32K].**
a920: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f   <li> [SQLITE_IO
a930: 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b 5d 0a 2a  CAP_ATOMIC64K].*
a940: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49  * <li> [SQLITE_I
a950: 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44  OCAP_SAFE_APPEND
a960: 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54  ].** <li> [SQLIT
a970: 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49  E_IOCAP_SEQUENTI
a980: 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a  AL].** </ul>.**.
a990: 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 49 4f  ** The SQLITE_IO
a9a0: 43 41 50 5f 41 54 4f 4d 49 43 20 70 72 6f 70 65  CAP_ATOMIC prope
a9b0: 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74 20 61  rty means that a
a9c0: 6c 6c 20 77 72 69 74 65 73 20 6f 66 0a 2a 2a 20  ll writes of.** 
a9d0: 61 6e 79 20 73 69 7a 65 20 61 72 65 20 61 74 6f  any size are ato
a9e0: 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45  mic.  The SQLITE
a9f0: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 6e 6e 6e  _IOCAP_ATOMICnnn
aa00: 20 76 61 6c 75 65 73 0a 2a 2a 20 6d 65 61 6e 20   values.** mean 
aa10: 74 68 61 74 20 77 72 69 74 65 73 20 6f 66 20 62  that writes of b
aa20: 6c 6f 63 6b 73 20 74 68 61 74 20 61 72 65 20 6e  locks that are n
aa30: 6e 6e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  nn bytes in size
aa40: 20 61 6e 64 0a 2a 2a 20 61 72 65 20 61 6c 69 67   and.** are alig
aa50: 6e 65 64 20 74 6f 20 61 6e 20 61 64 64 72 65 73  ned to an addres
aa60: 73 20 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e  s which is an in
aa70: 74 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f  teger multiple o
aa80: 66 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20 61 74 6f  f.** nnn are ato
aa90: 6d 69 63 2e 20 20 54 68 65 20 53 51 4c 49 54 45  mic.  The SQLITE
aaa0: 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45  _IOCAP_SAFE_APPE
aab0: 4e 44 20 76 61 6c 75 65 20 6d 65 61 6e 73 0a 2a  ND value means.*
aac0: 2a 20 74 68 61 74 20 77 68 65 6e 20 64 61 74 61  * that when data
aad0: 20 69 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20   is appended to 
aae0: 61 20 66 69 6c 65 2c 20 74 68 65 20 64 61 74 61  a file, the data
aaf0: 20 69 73 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20   is appended.** 
ab00: 66 69 72 73 74 20 74 68 65 6e 20 74 68 65 20 73  first then the s
ab10: 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20  ize of the file 
ab20: 69 73 20 65 78 74 65 6e 64 65 64 2c 20 6e 65 76  is extended, nev
ab30: 65 72 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20  er the other.** 
ab40: 77 61 79 20 61 72 6f 75 6e 64 2e 20 20 54 68 65  way around.  The
ab50: 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45   SQLITE_IOCAP_SE
ab60: 51 55 45 4e 54 49 41 4c 20 70 72 6f 70 65 72 74  QUENTIAL propert
ab70: 79 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20  y means that.** 
ab80: 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 77  information is w
ab90: 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 69  ritten to disk i
aba0: 6e 20 74 68 65 20 73 61 6d 65 20 6f 72 64 65 72  n the same order
abb0: 20 61 73 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20   as calls.** to 
abc0: 78 57 72 69 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20  xWrite()..**.** 
abd0: 49 66 20 78 52 65 61 64 28 29 20 72 65 74 75 72  If xRead() retur
abe0: 6e 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  ns SQLITE_IOERR_
abf0: 53 48 4f 52 54 5f 52 45 41 44 20 69 74 20 6d 75  SHORT_READ it mu
ac00: 73 74 20 61 6c 73 6f 20 66 69 6c 6c 0a 2a 2a 20  st also fill.** 
ac10: 69 6e 20 74 68 65 20 75 6e 72 65 61 64 20 70 6f  in the unread po
ac20: 72 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 62 75  rtions of the bu
ac30: 66 66 65 72 20 77 69 74 68 20 7a 65 72 6f 73 2e  ffer with zeros.
ac40: 20 20 41 20 56 46 53 20 74 68 61 74 0a 2a 2a 20    A VFS that.** 
ac50: 66 61 69 6c 73 20 74 6f 20 7a 65 72 6f 2d 66 69  fails to zero-fi
ac60: 6c 6c 20 73 68 6f 72 74 20 72 65 61 64 73 20 6d  ll short reads m
ac70: 69 67 68 74 20 73 65 65 6d 20 74 6f 20 77 6f 72  ight seem to wor
ac80: 6b 2e 20 20 48 6f 77 65 76 65 72 2c 0a 2a 2a 20  k.  However,.** 
ac90: 66 61 69 6c 75 72 65 20 74 6f 20 7a 65 72 6f 2d  failure to zero-
aca0: 66 69 6c 6c 20 73 68 6f 72 74 20 72 65 61 64 73  fill short reads
acb0: 20 77 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79   will eventually
acc0: 20 6c 65 61 64 20 74 6f 0a 2a 2a 20 64 61 74 61   lead to.** data
acd0: 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e  base corruption.
ace0: 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
acf0: 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  ct sqlite3_io_me
ad00: 74 68 6f 64 73 20 73 71 6c 69 74 65 33 5f 69 6f  thods sqlite3_io
ad10: 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74  _methods;.struct
ad20: 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
ad30: 6f 64 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 72  ods {.  int iVer
ad40: 73 69 6f 6e 3b 0a 20 20 69 6e 74 20 28 2a 78 43  sion;.  int (*xC
ad50: 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 66 69  lose)(sqlite3_fi
ad60: 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 52  le*);.  int (*xR
ad70: 65 61 64 29 28 73 71 6c 69 74 65 33 5f 66 69 6c  ead)(sqlite3_fil
ad80: 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69  e*, void*, int i
ad90: 41 6d 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74  Amt, sqlite3_int
ada0: 36 34 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e 74  64 iOfst);.  int
adb0: 20 28 2a 78 57 72 69 74 65 29 28 73 71 6c 69 74   (*xWrite)(sqlit
adc0: 65 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e 73 74 20  e3_file*, const 
add0: 76 6f 69 64 2a 2c 20 69 6e 74 20 69 41 6d 74 2c  void*, int iAmt,
ade0: 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69   sqlite3_int64 i
adf0: 4f 66 73 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78  Ofst);.  int (*x
ae00: 54 72 75 6e 63 61 74 65 29 28 73 71 6c 69 74 65  Truncate)(sqlite
ae10: 33 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65 33  3_file*, sqlite3
ae20: 5f 69 6e 74 36 34 20 73 69 7a 65 29 3b 0a 20 20  _int64 size);.  
ae30: 69 6e 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c  int (*xSync)(sql
ae40: 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20  ite3_file*, int 
ae50: 66 6c 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a  flags);.  int (*
ae60: 78 46 69 6c 65 53 69 7a 65 29 28 73 71 6c 69 74  xFileSize)(sqlit
ae70: 65 33 5f 66 69 6c 65 2a 2c 20 73 71 6c 69 74 65  e3_file*, sqlite
ae80: 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 3b  3_int64 *pSize);
ae90: 0a 20 20 69 6e 74 20 28 2a 78 4c 6f 63 6b 29 28  .  int (*xLock)(
aea0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69  sqlite3_file*, i
aeb0: 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 55 6e  nt);.  int (*xUn
aec0: 6c 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66 69  lock)(sqlite3_fi
aed0: 6c 65 2a 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74  le*, int);.  int
aee0: 20 28 2a 78 43 68 65 63 6b 52 65 73 65 72 76 65   (*xCheckReserve
aef0: 64 4c 6f 63 6b 29 28 73 71 6c 69 74 65 33 5f 66  dLock)(sqlite3_f
af00: 69 6c 65 2a 2c 20 69 6e 74 20 2a 70 52 65 73 4f  ile*, int *pResO
af10: 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 46 69  ut);.  int (*xFi
af20: 6c 65 43 6f 6e 74 72 6f 6c 29 28 73 71 6c 69 74  leControl)(sqlit
af30: 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 6f 70  e3_file*, int op
af40: 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 3b 0a 20  , void *pArg);. 
af50: 20 69 6e 74 20 28 2a 78 53 65 63 74 6f 72 53 69   int (*xSectorSi
af60: 7a 65 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ze)(sqlite3_file
af70: 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44 65 76  *);.  int (*xDev
af80: 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
af90: 63 73 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  cs)(sqlite3_file
afa0: 2a 29 3b 0a 20 20 2f 2a 20 41 64 64 69 74 69 6f  *);.  /* Additio
afb0: 6e 61 6c 20 6d 65 74 68 6f 64 73 20 6d 61 79 20  nal methods may 
afc0: 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75  be added in futu
afd0: 72 65 20 72 65 6c 65 61 73 65 73 20 2a 2f 0a 7d  re releases */.}
afe0: 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
aff0: 46 3a 20 53 74 61 6e 64 61 72 64 20 46 69 6c 65  F: Standard File
b000: 20 43 6f 6e 74 72 6f 6c 20 4f 70 63 6f 64 65 73   Control Opcodes
b010: 20 7b 48 31 31 33 31 30 7d 20 3c 53 33 30 38 30   {H11310} <S3080
b020: 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69  0>.**.** These i
b030: 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73  nteger constants
b040: 20 61 72 65 20 6f 70 63 6f 64 65 73 20 66 6f 72   are opcodes for
b050: 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f   the xFileContro
b060: 6c 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74  l method.** of t
b070: 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  he [sqlite3_io_m
b080: 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 61  ethods] object a
b090: 6e 64 20 66 6f 72 20 74 68 65 20 5b 73 71 6c 69  nd for the [sqli
b0a0: 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c  te3_file_control
b0b0: 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65  ()].** interface
b0c0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 53 51 4c  ..**.** The [SQL
b0d0: 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54  ITE_FCNTL_LOCKST
b0e0: 41 54 45 5d 20 6f 70 63 6f 64 65 20 69 73 20 75  ATE] opcode is u
b0f0: 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e  sed for debuggin
b100: 67 2e 20 20 54 68 69 73 0a 2a 2a 20 6f 70 63 6f  g.  This.** opco
b110: 64 65 20 63 61 75 73 65 73 20 74 68 65 20 78 46  de causes the xF
b120: 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74 68 6f  ileControl metho
b130: 64 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 63  d to write the c
b140: 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 0a  urrent state of.
b150: 2a 2a 20 74 68 65 20 6c 6f 63 6b 20 28 6f 6e 65  ** the lock (one
b160: 20 6f 66 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b   of [SQLITE_LOCK
b170: 5f 4e 4f 4e 45 5d 2c 20 5b 53 51 4c 49 54 45 5f  _NONE], [SQLITE_
b180: 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a 2a 2a  LOCK_SHARED],.**
b190: 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 52 45   [SQLITE_LOCK_RE
b1a0: 53 45 52 56 45 44 5d 2c 20 5b 53 51 4c 49 54 45  SERVED], [SQLITE
b1b0: 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c 20  _LOCK_PENDING], 
b1c0: 6f 72 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f  or [SQLITE_LOCK_
b1d0: 45 58 43 4c 55 53 49 56 45 5d 29 0a 2a 2a 20 69  EXCLUSIVE]).** i
b1e0: 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 74  nto an integer t
b1f0: 68 61 74 20 74 68 65 20 70 41 72 67 20 61 72 67  hat the pArg arg
b200: 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20 74 6f 2e  ument points to.
b210: 20 54 68 69 73 20 63 61 70 61 62 69 6c 69 74 79   This capability
b220: 0a 2a 2a 20 69 73 20 75 73 65 64 20 64 75 72 69  .** is used duri
b230: 6e 67 20 74 65 73 74 69 6e 67 20 61 6e 64 20 6f  ng testing and o
b240: 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20 62 65 20  nly needs to be 
b250: 73 75 70 70 6f 72 74 65 64 20 77 68 65 6e 20 53  supported when S
b260: 51 4c 49 54 45 5f 54 45 53 54 0a 2a 2a 20 69 73  QLITE_TEST.** is
b270: 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 64 65   defined..*/.#de
b280: 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54  fine SQLITE_FCNT
b290: 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 20 20 20 20  L_LOCKSTATE     
b2a0: 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c     1.#define SQL
b2b0: 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58  ITE_GET_LOCKPROX
b2c0: 59 46 49 4c 45 20 20 20 20 20 20 32 0a 23 64 65  YFILE      2.#de
b2d0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 45 54 5f  fine SQLITE_SET_
b2e0: 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 20 20 20  LOCKPROXYFILE   
b2f0: 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51 4c     3.#define SQL
b300: 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 20 20  ITE_LAST_ERRNO  
b310: 20 20 20 20 20 20 20 20 20 20 20 34 0a 0a 2f 2a             4../*
b320: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75  .** CAPI3REF: Mu
b330: 74 65 78 20 48 61 6e 64 6c 65 20 7b 48 31 37 31  tex Handle {H171
b340: 31 30 7d 20 3c 53 32 30 31 33 30 3e 0a 2a 2a 0a  10} <S20130>.**.
b350: 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 6d 6f 64  ** The mutex mod
b360: 75 6c 65 20 77 69 74 68 69 6e 20 53 51 4c 69 74  ule within SQLit
b370: 65 20 64 65 66 69 6e 65 73 20 5b 73 71 6c 69 74  e defines [sqlit
b380: 65 33 5f 6d 75 74 65 78 5d 20 74 6f 20 62 65 20  e3_mutex] to be 
b390: 61 6e 0a 2a 2a 20 61 62 73 74 72 61 63 74 20 74  an.** abstract t
b3a0: 79 70 65 20 66 6f 72 20 61 20 6d 75 74 65 78 20  ype for a mutex 
b3b0: 6f 62 6a 65 63 74 2e 20 20 54 68 65 20 53 51 4c  object.  The SQL
b3c0: 69 74 65 20 63 6f 72 65 20 6e 65 76 65 72 20 6c  ite core never l
b3d0: 6f 6f 6b 73 0a 2a 2a 20 61 74 20 74 68 65 20 69  ooks.** at the i
b3e0: 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e  nternal represen
b3f0: 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 5b 73 71  tation of an [sq
b400: 6c 69 74 65 33 5f 6d 75 74 65 78 5d 2e 20 20 49  lite3_mutex].  I
b410: 74 20 6f 6e 6c 79 0a 2a 2a 20 64 65 61 6c 73 20  t only.** deals 
b420: 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f  with pointers to
b430: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75   the [sqlite3_mu
b440: 74 65 78 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a  tex] object..**.
b450: 2a 2a 20 4d 75 74 65 78 65 73 20 61 72 65 20 63  ** Mutexes are c
b460: 72 65 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71  reated using [sq
b470: 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
b480: 63 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  c()]..*/.typedef
b490: 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
b4a0: 6d 75 74 65 78 20 73 71 6c 69 74 65 33 5f 6d 75  mutex sqlite3_mu
b4b0: 74 65 78 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  tex;../*.** CAPI
b4c0: 33 52 45 46 3a 20 4f 53 20 49 6e 74 65 72 66 61  3REF: OS Interfa
b4d0: 63 65 20 4f 62 6a 65 63 74 20 7b 48 31 31 31 34  ce Object {H1114
b4e0: 30 7d 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a  0} <S20100>.**.*
b4f0: 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
b500: 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 66 73   the sqlite3_vfs
b510: 20 6f 62 6a 65 63 74 20 64 65 66 69 6e 65 73 20   object defines 
b520: 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 62 65  the interface be
b530: 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 53 51 4c  tween.** the SQL
b540: 69 74 65 20 63 6f 72 65 20 61 6e 64 20 74 68 65  ite core and the
b550: 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72   underlying oper
b560: 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20 54  ating system.  T
b570: 68 65 20 22 76 66 73 22 0a 2a 2a 20 69 6e 20 74  he "vfs".** in t
b580: 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 6f  he name of the o
b590: 62 6a 65 63 74 20 73 74 61 6e 64 73 20 66 6f 72  bject stands for
b5a0: 20 22 76 69 72 74 75 61 6c 20 66 69 6c 65 20 73   "virtual file s
b5b0: 79 73 74 65 6d 22 2e 0a 2a 2a 0a 2a 2a 20 54 68  ystem"..**.** Th
b5c0: 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69  e value of the i
b5d0: 56 65 72 73 69 6f 6e 20 66 69 65 6c 64 20 69 73  Version field is
b5e0: 20 69 6e 69 74 69 61 6c 6c 79 20 31 20 62 75 74   initially 1 but
b5f0: 20 6d 61 79 20 62 65 20 6c 61 72 67 65 72 20 69   may be larger i
b600: 6e 0a 2a 2a 20 66 75 74 75 72 65 20 76 65 72 73  n.** future vers
b610: 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 2e 20  ions of SQLite. 
b620: 20 41 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c   Additional fiel
b630: 64 73 20 6d 61 79 20 62 65 20 61 70 70 65 6e 64  ds may be append
b640: 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a 20 6f 62  ed to this.** ob
b650: 6a 65 63 74 20 77 68 65 6e 20 74 68 65 20 69 56  ject when the iV
b660: 65 72 73 69 6f 6e 20 76 61 6c 75 65 20 69 73 20  ersion value is 
b670: 69 6e 63 72 65 61 73 65 64 2e 20 20 4e 6f 74 65  increased.  Note
b680: 20 74 68 61 74 20 74 68 65 20 73 74 72 75 63 74   that the struct
b690: 75 72 65 0a 2a 2a 20 6f 66 20 74 68 65 20 73 71  ure.** of the sq
b6a0: 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74  lite3_vfs object
b6b0: 20 63 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20   changes in the 
b6c0: 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 74 77  transaction betw
b6d0: 65 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 76 65  een.** SQLite ve
b6e0: 72 73 69 6f 6e 20 33 2e 35 2e 39 20 61 6e 64 20  rsion 3.5.9 and 
b6f0: 33 2e 36 2e 30 20 61 6e 64 20 79 65 74 20 74 68  3.6.0 and yet th
b700: 65 20 69 56 65 72 73 69 6f 6e 20 66 69 65 6c 64  e iVersion field
b710: 20 77 61 73 20 6e 6f 74 0a 2a 2a 20 6d 6f 64 69   was not.** modi
b720: 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  fied..**.** The 
b730: 73 7a 4f 73 46 69 6c 65 20 66 69 65 6c 64 20 69  szOsFile field i
b740: 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  s the size of th
b750: 65 20 73 75 62 63 6c 61 73 73 65 64 20 5b 73 71  e subclassed [sq
b760: 6c 69 74 65 33 5f 66 69 6c 65 5d 0a 2a 2a 20 73  lite3_file].** s
b770: 74 72 75 63 74 75 72 65 20 75 73 65 64 20 62 79  tructure used by
b780: 20 74 68 69 73 20 56 46 53 2e 20 20 6d 78 50 61   this VFS.  mxPa
b790: 74 68 6e 61 6d 65 20 69 73 20 74 68 65 20 6d 61  thname is the ma
b7a0: 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 0a  ximum length of.
b7b0: 2a 2a 20 61 20 70 61 74 68 6e 61 6d 65 20 69 6e  ** a pathname in
b7c0: 20 74 68 69 73 20 56 46 53 2e 0a 2a 2a 0a 2a 2a   this VFS..**.**
b7d0: 20 52 65 67 69 73 74 65 72 65 64 20 73 71 6c 69   Registered sqli
b7e0: 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 73 20  te3_vfs objects 
b7f0: 61 72 65 20 6b 65 70 74 20 6f 6e 20 61 20 6c 69  are kept on a li
b800: 6e 6b 65 64 20 6c 69 73 74 20 66 6f 72 6d 65 64  nked list formed
b810: 20 62 79 0a 2a 2a 20 74 68 65 20 70 4e 65 78 74   by.** the pNext
b820: 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 65 20 5b   pointer.  The [
b830: 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69  sqlite3_vfs_regi
b840: 73 74 65 72 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b  ster()].** and [
b850: 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65  sqlite3_vfs_unre
b860: 67 69 73 74 65 72 28 29 5d 20 69 6e 74 65 72 66  gister()] interf
b870: 61 63 65 73 20 6d 61 6e 61 67 65 20 74 68 69 73  aces manage this
b880: 20 6c 69 73 74 0a 2a 2a 20 69 6e 20 61 20 74 68   list.** in a th
b890: 72 65 61 64 2d 73 61 66 65 20 77 61 79 2e 20 20  read-safe way.  
b8a0: 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73  The [sqlite3_vfs
b8b0: 5f 66 69 6e 64 28 29 5d 20 69 6e 74 65 72 66 61  _find()] interfa
b8c0: 63 65 0a 2a 2a 20 73 65 61 72 63 68 65 73 20 74  ce.** searches t
b8d0: 68 65 20 6c 69 73 74 2e 20 20 4e 65 69 74 68 65  he list.  Neithe
b8e0: 72 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  r the applicatio
b8f0: 6e 20 63 6f 64 65 20 6e 6f 72 20 74 68 65 20 56  n code nor the V
b900: 46 53 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  FS.** implementa
b910: 74 69 6f 6e 20 73 68 6f 75 6c 64 20 75 73 65 20  tion should use 
b920: 74 68 65 20 70 4e 65 78 74 20 70 6f 69 6e 74 65  the pNext pointe
b930: 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4e 65  r..**.** The pNe
b940: 78 74 20 66 69 65 6c 64 20 69 73 20 74 68 65 20  xt field is the 
b950: 6f 6e 6c 79 20 66 69 65 6c 64 20 69 6e 20 74 68  only field in th
b960: 65 20 73 71 6c 69 74 65 33 5f 76 66 73 0a 2a 2a  e sqlite3_vfs.**
b970: 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20   structure that 
b980: 53 51 4c 69 74 65 20 77 69 6c 6c 20 65 76 65 72  SQLite will ever
b990: 20 6d 6f 64 69 66 79 2e 20 20 53 51 4c 69 74 65   modify.  SQLite
b9a0: 20 77 69 6c 6c 20 6f 6e 6c 79 20 61 63 63 65 73   will only acces
b9b0: 73 0a 2a 2a 20 6f 72 20 6d 6f 64 69 66 79 20 74  s.** or modify t
b9c0: 68 69 73 20 66 69 65 6c 64 20 77 68 69 6c 65 20  his field while 
b9d0: 68 6f 6c 64 69 6e 67 20 61 20 70 61 72 74 69 63  holding a partic
b9e0: 75 6c 61 72 20 73 74 61 74 69 63 20 6d 75 74 65  ular static mute
b9f0: 78 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63  x..** The applic
ba00: 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 65 76  ation should nev
ba10: 65 72 20 6d 6f 64 69 66 79 20 61 6e 79 74 68 69  er modify anythi
ba20: 6e 67 20 77 69 74 68 69 6e 20 74 68 65 20 73 71  ng within the sq
ba30: 6c 69 74 65 33 5f 76 66 73 0a 2a 2a 20 6f 62 6a  lite3_vfs.** obj
ba40: 65 63 74 20 6f 6e 63 65 20 74 68 65 20 6f 62 6a  ect once the obj
ba50: 65 63 74 20 68 61 73 20 62 65 65 6e 20 72 65 67  ect has been reg
ba60: 69 73 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  istered..**.** T
ba70: 68 65 20 7a 4e 61 6d 65 20 66 69 65 6c 64 20 68  he zName field h
ba80: 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  olds the name of
ba90: 20 74 68 65 20 56 46 53 20 6d 6f 64 75 6c 65 2e   the VFS module.
baa0: 20 20 54 68 65 20 6e 61 6d 65 20 6d 75 73 74 0a    The name must.
bab0: 2a 2a 20 62 65 20 75 6e 69 71 75 65 20 61 63 72  ** be unique acr
bac0: 6f 73 73 20 61 6c 6c 20 56 46 53 20 6d 6f 64 75  oss all VFS modu
bad0: 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  les..**.** SQLit
bae0: 65 20 77 69 6c 6c 20 67 75 61 72 61 6e 74 65 65  e will guarantee
baf0: 20 74 68 61 74 20 74 68 65 20 7a 46 69 6c 65 6e   that the zFilen
bb00: 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ame parameter to
bb10: 20 78 4f 70 65 6e 0a 2a 2a 20 69 73 20 65 69 74   xOpen.** is eit
bb20: 68 65 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74  her a NULL point
bb30: 65 72 20 6f 72 20 73 74 72 69 6e 67 20 6f 62 74  er or string obt
bb40: 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 78 46  ained.** from xF
bb50: 75 6c 6c 50 61 74 68 6e 61 6d 65 28 29 2e 20 20  ullPathname().  
bb60: 53 51 4c 69 74 65 20 66 75 72 74 68 65 72 20 67  SQLite further g
bb70: 75 61 72 61 6e 74 65 65 73 20 74 68 61 74 0a 2a  uarantees that.*
bb80: 2a 20 74 68 65 20 73 74 72 69 6e 67 20 77 69 6c  * the string wil
bb90: 6c 20 62 65 20 76 61 6c 69 64 20 61 6e 64 20 75  l be valid and u
bba0: 6e 63 68 61 6e 67 65 64 20 75 6e 74 69 6c 20 78  nchanged until x
bbb0: 43 6c 6f 73 65 28 29 20 69 73 0a 2a 2a 20 63 61  Close() is.** ca
bbc0: 6c 6c 65 64 2e 20 42 65 63 61 75 73 65 20 6f 66  lled. Because of
bbd0: 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73 65   the previous se
bbe0: 6e 74 65 6e 63 65 2c 0a 2a 2a 20 74 68 65 20 5b  ntence,.** the [
bbf0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 63 61  sqlite3_file] ca
bc00: 6e 20 73 61 66 65 6c 79 20 73 74 6f 72 65 20 61  n safely store a
bc10: 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a   pointer to the.
bc20: 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 69 66 20 69  ** filename if i
bc30: 74 20 6e 65 65 64 73 20 74 6f 20 72 65 6d 65 6d  t needs to remem
bc40: 62 65 72 20 74 68 65 20 66 69 6c 65 6e 61 6d 65  ber the filename
bc50: 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73 6f 6e   for some reason
bc60: 2e 0a 2a 2a 20 49 66 20 74 68 65 20 7a 46 69 6c  ..** If the zFil
bc70: 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20  ename parameter 
bc80: 69 73 20 78 4f 70 65 6e 20 69 73 20 61 20 4e 55  is xOpen is a NU
bc90: 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e 20  LL pointer then 
bca0: 78 4f 70 65 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e  xOpen.** must in
bcb0: 76 65 6e 74 20 69 74 73 20 6f 77 6e 20 74 65 6d  vent its own tem
bcc0: 70 6f 72 61 72 79 20 6e 61 6d 65 20 66 6f 72 20  porary name for 
bcd0: 74 68 65 20 66 69 6c 65 2e 20 20 57 68 65 6e 65  the file.  Whene
bce0: 76 65 72 20 74 68 65 20 0a 2a 2a 20 78 46 69 6c  ver the .** xFil
bcf0: 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 65 72 20  ename parameter 
bd00: 69 73 20 4e 55 4c 4c 20 69 74 20 77 69 6c 6c 20  is NULL it will 
bd10: 61 6c 73 6f 20 62 65 20 74 68 65 20 63 61 73 65  also be the case
bd20: 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 66 6c 61   that the.** fla
bd30: 67 73 20 70 61 72 61 6d 65 74 65 72 20 77 69 6c  gs parameter wil
bd40: 6c 20 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54  l include [SQLIT
bd50: 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43  E_OPEN_DELETEONC
bd60: 4c 4f 53 45 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  LOSE]..**.** The
bd70: 20 66 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20   flags argument 
bd80: 74 6f 20 78 4f 70 65 6e 28 29 20 69 6e 63 6c 75  to xOpen() inclu
bd90: 64 65 73 20 61 6c 6c 20 62 69 74 73 20 73 65 74  des all bits set
bda0: 20 69 6e 0a 2a 2a 20 74 68 65 20 66 6c 61 67 73   in.** the flags
bdb0: 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71   argument to [sq
bdc0: 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d  lite3_open_v2()]
bdd0: 2e 20 20 4f 72 20 69 66 20 5b 73 71 6c 69 74 65  .  Or if [sqlite
bde0: 33 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 6f 72 20  3_open()].** or 
bdf0: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28  [sqlite3_open16(
be00: 29 5d 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e  )] is used, then
be10: 20 66 6c 61 67 73 20 69 6e 63 6c 75 64 65 73 20   flags includes 
be20: 61 74 20 6c 65 61 73 74 0a 2a 2a 20 5b 53 51 4c  at least.** [SQL
be30: 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
be40: 54 45 5d 20 7c 20 5b 53 51 4c 49 54 45 5f 4f 50  TE] | [SQLITE_OP
be50: 45 4e 5f 43 52 45 41 54 45 5d 2e 20 0a 2a 2a 20  EN_CREATE]. .** 
be60: 49 66 20 78 4f 70 65 6e 28 29 20 6f 70 65 6e 73  If xOpen() opens
be70: 20 61 20 66 69 6c 65 20 72 65 61 64 2d 6f 6e 6c   a file read-onl
be80: 79 20 74 68 65 6e 20 69 74 20 73 65 74 73 20 2a  y then it sets *
be90: 70 4f 75 74 46 6c 61 67 73 20 74 6f 0a 2a 2a 20  pOutFlags to.** 
bea0: 69 6e 63 6c 75 64 65 20 5b 53 51 4c 49 54 45 5f  include [SQLITE_
beb0: 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 2e 20  OPEN_READONLY]. 
bec0: 20 4f 74 68 65 72 20 62 69 74 73 20 69 6e 20 2a   Other bits in *
bed0: 70 4f 75 74 46 6c 61 67 73 20 6d 61 79 20 62 65  pOutFlags may be
bee0: 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69   set..**.** SQLi
bef0: 74 65 20 77 69 6c 6c 20 61 6c 73 6f 20 61 64 64  te will also add
bf00: 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   one of the foll
bf10: 6f 77 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74  owing flags to t
bf20: 68 65 20 78 4f 70 65 6e 28 29 0a 2a 2a 20 63 61  he xOpen().** ca
bf30: 6c 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  ll, depending on
bf40: 20 74 68 65 20 6f 62 6a 65 63 74 20 62 65 69 6e   the object bein
bf50: 67 20 6f 70 65 6e 65 64 3a 0a 2a 2a 0a 2a 2a 20  g opened:.**.** 
bf60: 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53  <ul>.** <li>  [S
bf70: 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
bf80: 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51  DB].** <li>  [SQ
bf90: 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a  LITE_OPEN_MAIN_J
bfa0: 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20  OURNAL].** <li> 
bfb0: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45   [SQLITE_OPEN_TE
bfc0: 4d 50 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20  MP_DB].** <li>  
bfd0: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d  [SQLITE_OPEN_TEM
bfe0: 50 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c  P_JOURNAL].** <l
bff0: 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  i>  [SQLITE_OPEN
c000: 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 5d 0a 2a  _TRANSIENT_DB].*
c010: 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f  * <li>  [SQLITE_
c020: 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 5d  OPEN_SUBJOURNAL]
c030: 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54  .** <li>  [SQLIT
c040: 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f  E_OPEN_MASTER_JO
c050: 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a  URNAL].** </ul>.
c060: 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 49  **.** The file I
c070: 2f 4f 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  /O implementatio
c080: 6e 20 63 61 6e 20 75 73 65 20 74 68 65 20 6f 62  n can use the ob
c090: 6a 65 63 74 20 74 79 70 65 20 66 6c 61 67 73 20  ject type flags 
c0a0: 74 6f 0a 2a 2a 20 63 68 61 6e 67 65 20 74 68 65  to.** change the
c0b0: 20 77 61 79 20 69 74 20 64 65 61 6c 73 20 77 69   way it deals wi
c0c0: 74 68 20 66 69 6c 65 73 2e 20 20 46 6f 72 20 65  th files.  For e
c0d0: 78 61 6d 70 6c 65 2c 20 61 6e 20 61 70 70 6c 69  xample, an appli
c0e0: 63 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 64  cation.** that d
c0f0: 6f 65 73 20 6e 6f 74 20 63 61 72 65 20 61 62 6f  oes not care abo
c100: 75 74 20 63 72 61 73 68 20 72 65 63 6f 76 65 72  ut crash recover
c110: 79 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6d 69  y or rollback mi
c120: 67 68 74 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20  ght make.** the 
c130: 6f 70 65 6e 20 6f 66 20 61 20 6a 6f 75 72 6e 61  open of a journa
c140: 6c 20 66 69 6c 65 20 61 20 6e 6f 2d 6f 70 2e 20  l file a no-op. 
c150: 20 57 72 69 74 65 73 20 74 6f 20 74 68 69 73 20   Writes to this 
c160: 6a 6f 75 72 6e 61 6c 20 77 6f 75 6c 64 0a 2a 2a  journal would.**
c170: 20 61 6c 73 6f 20 62 65 20 6e 6f 2d 6f 70 73 2c   also be no-ops,
c180: 20 61 6e 64 20 61 6e 79 20 61 74 74 65 6d 70 74   and any attempt
c190: 20 74 6f 20 72 65 61 64 20 74 68 65 20 6a 6f 75   to read the jou
c1a0: 72 6e 61 6c 20 77 6f 75 6c 64 20 72 65 74 75 72  rnal would retur
c1b0: 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 49 4f 45 52  n.** SQLITE_IOER
c1c0: 52 2e 20 20 4f 72 20 74 68 65 20 69 6d 70 6c 65  R.  Or the imple
c1d0: 6d 65 6e 74 61 74 69 6f 6e 20 6d 69 67 68 74 20  mentation might 
c1e0: 72 65 63 6f 67 6e 69 7a 65 20 74 68 61 74 20 61  recognize that a
c1f0: 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c   database.** fil
c200: 65 20 77 69 6c 6c 20 62 65 20 64 6f 69 6e 67 20  e will be doing 
c210: 70 61 67 65 2d 61 6c 69 67 6e 65 64 20 73 65 63  page-aligned sec
c220: 74 6f 72 20 72 65 61 64 73 20 61 6e 64 20 77 72  tor reads and wr
c230: 69 74 65 73 20 69 6e 20 61 20 72 61 6e 64 6f 6d  ites in a random
c240: 0a 2a 2a 20 6f 72 64 65 72 20 61 6e 64 20 73 65  .** order and se
c250: 74 20 75 70 20 69 74 73 20 49 2f 4f 20 73 75 62  t up its I/O sub
c260: 73 79 73 74 65 6d 20 61 63 63 6f 72 64 69 6e 67  system according
c270: 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  ly..**.** SQLite
c280: 20 6d 69 67 68 74 20 61 6c 73 6f 20 61 64 64 20   might also add 
c290: 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  one of the follo
c2a0: 77 69 6e 67 20 66 6c 61 67 73 20 74 6f 20 74 68  wing flags to th
c2b0: 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 3a 0a  e xOpen method:.
c2c0: 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c  **.** <ul>.** <l
c2d0: 69 3e 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  i> [SQLITE_OPEN_
c2e0: 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a  DELETEONCLOSE].*
c2f0: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 4f  * <li> [SQLITE_O
c300: 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 0a 2a  PEN_EXCLUSIVE].*
c310: 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68  * </ul>.**.** Th
c320: 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44  e [SQLITE_OPEN_D
c330: 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 20 66 6c  ELETEONCLOSE] fl
c340: 61 67 20 6d 65 61 6e 73 20 74 68 65 20 66 69 6c  ag means the fil
c350: 65 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 64  e should be.** d
c360: 65 6c 65 74 65 64 20 77 68 65 6e 20 69 74 20 69  eleted when it i
c370: 73 20 63 6c 6f 73 65 64 2e 20 20 54 68 65 20 5b  s closed.  The [
c380: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45  SQLITE_OPEN_DELE
c390: 54 45 4f 4e 43 4c 4f 53 45 5d 0a 2a 2a 20 77 69  TEONCLOSE].** wi
c3a0: 6c 6c 20 62 65 20 73 65 74 20 66 6f 72 20 54 45  ll be set for TE
c3b0: 4d 50 20 20 64 61 74 61 62 61 73 65 73 2c 20 6a  MP  databases, j
c3c0: 6f 75 72 6e 61 6c 73 20 61 6e 64 20 66 6f 72 20  ournals and for 
c3d0: 73 75 62 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a 0a  subjournals..**.
c3e0: 2a 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f  ** The [SQLITE_O
c3f0: 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 5d 20 66  PEN_EXCLUSIVE] f
c400: 6c 61 67 20 69 73 20 61 6c 77 61 79 73 20 75 73  lag is always us
c410: 65 64 20 69 6e 20 63 6f 6e 6a 75 6e 63 74 69 6f  ed in conjunctio
c420: 6e 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 53  n.** with the [S
c430: 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
c440: 45 5d 20 66 6c 61 67 2c 20 77 68 69 63 68 20 61  E] flag, which a
c450: 72 65 20 62 6f 74 68 20 64 69 72 65 63 74 6c 79  re both directly
c460: 0a 2a 2a 20 61 6e 61 6c 6f 67 6f 75 73 20 74 6f  .** analogous to
c470: 20 74 68 65 20 4f 5f 45 58 43 4c 20 61 6e 64 20   the O_EXCL and 
c480: 4f 5f 43 52 45 41 54 20 66 6c 61 67 73 20 6f 66  O_CREAT flags of
c490: 20 74 68 65 20 50 4f 53 49 58 20 6f 70 65 6e 28   the POSIX open(
c4a0: 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 53  ).** API.  The S
c4b0: 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55  QLITE_OPEN_EXCLU
c4c0: 53 49 56 45 20 66 6c 61 67 2c 20 77 68 65 6e 20  SIVE flag, when 
c4d0: 70 61 69 72 65 64 20 77 69 74 68 20 74 68 65 20  paired with the 
c4e0: 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  .** SQLITE_OPEN_
c4f0: 43 52 45 41 54 45 2c 20 69 73 20 75 73 65 64 20  CREATE, is used 
c500: 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61 74  to indicate that
c510: 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 61 6c 77   file should alw
c520: 61 79 73 0a 2a 2a 20 62 65 20 63 72 65 61 74 65  ays.** be create
c530: 64 2c 20 61 6e 64 20 74 68 61 74 20 69 74 20 69  d, and that it i
c540: 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 69 74  s an error if it
c550: 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e   already exists.
c560: 0a 2a 2a 20 49 74 20 69 73 20 3c 69 3e 6e 6f 74  .** It is <i>not
c570: 3c 2f 69 3e 20 75 73 65 64 20 74 6f 20 69 6e 64  </i> used to ind
c580: 69 63 61 74 65 20 74 68 65 20 66 69 6c 65 20 73  icate the file s
c590: 68 6f 75 6c 64 20 62 65 20 6f 70 65 6e 65 64 20  hould be opened 
c5a0: 0a 2a 2a 20 66 6f 72 20 65 78 63 6c 75 73 69 76  .** for exclusiv
c5b0: 65 20 61 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20  e access..**.** 
c5c0: 41 74 20 6c 65 61 73 74 20 73 7a 4f 73 46 69 6c  At least szOsFil
c5d0: 65 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72  e bytes of memor
c5e0: 79 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 20  y are allocated 
c5f0: 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20 74 6f 20  by SQLite.** to 
c600: 68 6f 6c 64 20 74 68 65 20 20 5b 73 71 6c 69 74  hold the  [sqlit
c610: 65 33 5f 66 69 6c 65 5d 20 73 74 72 75 63 74 75  e3_file] structu
c620: 72 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65  re passed as the
c630: 20 74 68 69 72 64 0a 2a 2a 20 61 72 67 75 6d 65   third.** argume
c640: 6e 74 20 74 6f 20 78 4f 70 65 6e 2e 20 20 54 68  nt to xOpen.  Th
c650: 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20 64  e xOpen method d
c660: 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 0a  oes not have to.
c670: 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 74 68 65 20  ** allocate the 
c680: 73 74 72 75 63 74 75 72 65 3b 20 69 74 20 73 68  structure; it sh
c690: 6f 75 6c 64 20 6a 75 73 74 20 66 69 6c 6c 20 69  ould just fill i
c6a0: 74 20 69 6e 2e 20 20 4e 6f 74 65 20 74 68 61 74  t in.  Note that
c6b0: 0a 2a 2a 20 74 68 65 20 78 4f 70 65 6e 20 6d 65  .** the xOpen me
c6c0: 74 68 6f 64 20 6d 75 73 74 20 73 65 74 20 74 68  thod must set th
c6d0: 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2e 70  e sqlite3_file.p
c6e0: 4d 65 74 68 6f 64 73 20 74 6f 20 65 69 74 68 65  Methods to eithe
c6f0: 72 0a 2a 2a 20 61 20 76 61 6c 69 64 20 5b 73 71  r.** a valid [sq
c700: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
c710: 5d 20 6f 62 6a 65 63 74 20 6f 72 20 74 6f 20 4e  ] object or to N
c720: 55 4c 4c 2e 20 20 78 4f 70 65 6e 20 6d 75 73 74  ULL.  xOpen must
c730: 20 64 6f 0a 2a 2a 20 74 68 69 73 20 65 76 65 6e   do.** this even
c740: 20 69 66 20 74 68 65 20 6f 70 65 6e 20 66 61 69   if the open fai
c750: 6c 73 2e 20 20 53 51 4c 69 74 65 20 65 78 70 65  ls.  SQLite expe
c760: 63 74 73 20 74 68 61 74 20 74 68 65 20 73 71 6c  cts that the sql
c770: 69 74 65 33 5f 66 69 6c 65 2e 70 4d 65 74 68 6f  ite3_file.pMetho
c780: 64 73 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 77 69  ds.** element wi
c790: 6c 6c 20 62 65 20 76 61 6c 69 64 20 61 66 74 65  ll be valid afte
c7a0: 72 20 78 4f 70 65 6e 20 72 65 74 75 72 6e 73 20  r xOpen returns 
c7b0: 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68  regardless of th
c7c0: 65 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20  e success.** or 
c7d0: 66 61 69 6c 75 72 65 20 6f 66 20 74 68 65 20 78  failure of the x
c7e0: 4f 70 65 6e 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a  Open call..**.**
c7f0: 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d   The flags argum
c800: 65 6e 74 20 74 6f 20 78 41 63 63 65 73 73 28 29  ent to xAccess()
c810: 20 6d 61 79 20 62 65 20 5b 53 51 4c 49 54 45 5f   may be [SQLITE_
c820: 41 43 43 45 53 53 5f 45 58 49 53 54 53 5d 0a 2a  ACCESS_EXISTS].*
c830: 2a 20 74 6f 20 74 65 73 74 20 66 6f 72 20 74 68  * to test for th
c840: 65 20 65 78 69 73 74 65 6e 63 65 20 6f 66 20 61  e existence of a
c850: 20 66 69 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54   file, or [SQLIT
c860: 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49  E_ACCESS_READWRI
c870: 54 45 5d 20 74 6f 0a 2a 2a 20 74 65 73 74 20 77  TE] to.** test w
c880: 68 65 74 68 65 72 20 61 20 66 69 6c 65 20 69 73  hether a file is
c890: 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72   readable and wr
c8a0: 69 74 61 62 6c 65 2c 20 6f 72 20 5b 53 51 4c 49  itable, or [SQLI
c8b0: 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 5d 0a  TE_ACCESS_READ].
c8c0: 2a 2a 20 74 6f 20 74 65 73 74 20 77 68 65 74 68  ** to test wheth
c8d0: 65 72 20 61 20 66 69 6c 65 20 69 73 20 61 74 20  er a file is at 
c8e0: 6c 65 61 73 74 20 72 65 61 64 61 62 6c 65 2e 20  least readable. 
c8f0: 20 20 54 68 65 20 66 69 6c 65 20 63 61 6e 20 62    The file can b
c900: 65 20 61 0a 2a 2a 20 64 69 72 65 63 74 6f 72 79  e a.** directory
c910: 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77  ..**.** SQLite w
c920: 69 6c 6c 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63  ill always alloc
c930: 61 74 65 20 61 74 20 6c 65 61 73 74 20 6d 78 50  ate at least mxP
c940: 61 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 73 20  athname+1 bytes 
c950: 66 6f 72 20 74 68 65 0a 2a 2a 20 6f 75 74 70 75  for the.** outpu
c960: 74 20 62 75 66 66 65 72 20 78 46 75 6c 6c 50 61  t buffer xFullPa
c970: 74 68 6e 61 6d 65 2e 20 20 54 68 65 20 65 78 61  thname.  The exa
c980: 63 74 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6f  ct size of the o
c990: 75 74 70 75 74 20 62 75 66 66 65 72 0a 2a 2a 20  utput buffer.** 
c9a0: 69 73 20 61 6c 73 6f 20 70 61 73 73 65 64 20 61  is also passed a
c9b0: 73 20 61 20 70 61 72 61 6d 65 74 65 72 20 74 6f  s a parameter to
c9c0: 20 62 6f 74 68 20 20 6d 65 74 68 6f 64 73 2e 20   both  methods. 
c9d0: 49 66 20 74 68 65 20 6f 75 74 70 75 74 20 62 75  If the output bu
c9e0: 66 66 65 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c  ffer.** is not l
c9f0: 61 72 67 65 20 65 6e 6f 75 67 68 2c 20 5b 53 51  arge enough, [SQ
ca00: 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 73  LITE_CANTOPEN] s
ca10: 68 6f 75 6c 64 20 62 65 20 72 65 74 75 72 6e 65  hould be returne
ca20: 64 2e 20 53 69 6e 63 65 20 74 68 69 73 20 69 73  d. Since this is
ca30: 0a 2a 2a 20 68 61 6e 64 6c 65 64 20 61 73 20 61  .** handled as a
ca40: 20 66 61 74 61 6c 20 65 72 72 6f 72 20 62 79 20   fatal error by 
ca50: 53 51 4c 69 74 65 2c 20 76 66 73 20 69 6d 70 6c  SQLite, vfs impl
ca60: 65 6d 65 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75  ementations shou
ca70: 6c 64 20 65 6e 64 65 61 76 6f 72 0a 2a 2a 20 74  ld endeavor.** t
ca80: 6f 20 70 72 65 76 65 6e 74 20 74 68 69 73 20 62  o prevent this b
ca90: 79 20 73 65 74 74 69 6e 67 20 6d 78 50 61 74 68  y setting mxPath
caa0: 6e 61 6d 65 20 74 6f 20 61 20 73 75 66 66 69 63  name to a suffic
cab0: 69 65 6e 74 6c 79 20 6c 61 72 67 65 20 76 61 6c  iently large val
cac0: 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 52  ue..**.** The xR
cad0: 61 6e 64 6f 6d 6e 65 73 73 28 29 2c 20 78 53 6c  andomness(), xSl
cae0: 65 65 70 28 29 2c 20 61 6e 64 20 78 43 75 72 72  eep(), and xCurr
caf0: 65 6e 74 54 69 6d 65 28 29 20 69 6e 74 65 72 66  entTime() interf
cb00: 61 63 65 73 0a 2a 2a 20 61 72 65 20 6e 6f 74 20  aces.** are not 
cb10: 73 74 72 69 63 74 6c 79 20 61 20 70 61 72 74 20  strictly a part 
cb20: 6f 66 20 74 68 65 20 66 69 6c 65 73 79 73 74 65  of the filesyste
cb30: 6d 2c 20 62 75 74 20 74 68 65 79 20 61 72 65 0a  m, but they are.
cb40: 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74  ** included in t
cb50: 68 65 20 56 46 53 20 73 74 72 75 63 74 75 72 65  he VFS structure
cb60: 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65 6e 65 73   for completenes
cb70: 73 2e 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f  s..** The xRando
cb80: 6d 6e 65 73 73 28 29 20 66 75 6e 63 74 69 6f 6e  mness() function
cb90: 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 74   attempts to ret
cba0: 75 72 6e 20 6e 42 79 74 65 73 20 62 79 74 65 73  urn nBytes bytes
cbb0: 0a 2a 2a 20 6f 66 20 67 6f 6f 64 2d 71 75 61 6c  .** of good-qual
cbc0: 69 74 79 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69  ity randomness i
cbd0: 6e 74 6f 20 7a 4f 75 74 2e 20 20 54 68 65 20 72  nto zOut.  The r
cbe0: 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a  eturn value is.*
cbf0: 2a 20 74 68 65 20 61 63 74 75 61 6c 20 6e 75 6d  * the actual num
cc00: 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
cc10: 72 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69  randomness obtai
cc20: 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 78 53 6c 65  ned..** The xSle
cc30: 65 70 28 29 20 6d 65 74 68 6f 64 20 63 61 75 73  ep() method caus
cc40: 65 73 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 74  es the calling t
cc50: 68 72 65 61 64 20 74 6f 20 73 6c 65 65 70 20 66  hread to sleep f
cc60: 6f 72 20 61 74 0a 2a 2a 20 6c 65 61 73 74 20 74  or at.** least t
cc70: 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63  he number of mic
cc80: 72 6f 73 65 63 6f 6e 64 73 20 67 69 76 65 6e 2e  roseconds given.
cc90: 20 20 54 68 65 20 78 43 75 72 72 65 6e 74 54 69    The xCurrentTi
cca0: 6d 65 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 72  me().** method r
ccb0: 65 74 75 72 6e 73 20 61 20 4a 75 6c 69 61 6e 20  eturns a Julian 
ccc0: 44 61 79 20 4e 75 6d 62 65 72 20 66 6f 72 20 74  Day Number for t
ccd0: 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 65 20  he current date 
cce0: 61 6e 64 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2f 0a  and time..**.*/.
ccf0: 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
cd00: 71 6c 69 74 65 33 5f 76 66 73 20 73 71 6c 69 74  qlite3_vfs sqlit
cd10: 65 33 5f 76 66 73 3b 0a 73 74 72 75 63 74 20 73  e3_vfs;.struct s
cd20: 71 6c 69 74 65 33 5f 76 66 73 20 7b 0a 20 20 69  qlite3_vfs {.  i
cd30: 6e 74 20 69 56 65 72 73 69 6f 6e 3b 20 20 20 20  nt iVersion;    
cd40: 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 75 63          /* Struc
cd50: 74 75 72 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d  ture version num
cd60: 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4f  ber */.  int szO
cd70: 73 46 69 6c 65 3b 20 20 20 20 20 20 20 20 20 20  sFile;          
cd80: 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 73 75 62    /* Size of sub
cd90: 63 6c 61 73 73 65 64 20 73 71 6c 69 74 65 33 5f  classed sqlite3_
cda0: 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78  file */.  int mx
cdb0: 50 61 74 68 6e 61 6d 65 3b 20 20 20 20 20 20 20  Pathname;       
cdc0: 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 66 69     /* Maximum fi
cdd0: 6c 65 20 70 61 74 68 6e 61 6d 65 20 6c 65 6e 67  le pathname leng
cde0: 74 68 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  th */.  sqlite3_
cdf0: 76 66 73 20 2a 70 4e 65 78 74 3b 20 20 20 20 20  vfs *pNext;     
ce00: 20 2f 2a 20 4e 65 78 74 20 72 65 67 69 73 74 65   /* Next registe
ce10: 72 65 64 20 56 46 53 20 2a 2f 0a 20 20 63 6f 6e  red VFS */.  con
ce20: 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  st char *zName; 
ce30: 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
ce40: 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 66 69   this virtual fi
ce50: 6c 65 20 73 79 73 74 65 6d 20 2a 2f 0a 20 20 76  le system */.  v
ce60: 6f 69 64 20 2a 70 41 70 70 44 61 74 61 3b 20 20  oid *pAppData;  
ce70: 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
ce80: 65 72 20 74 6f 20 61 70 70 6c 69 63 61 74 69 6f  er to applicatio
ce90: 6e 2d 73 70 65 63 69 66 69 63 20 64 61 74 61 20  n-specific data 
cea0: 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e  */.  int (*xOpen
ceb0: 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20  )(sqlite3_vfs*, 
cec0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
ced0: 65 2c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  e, sqlite3_file*
cee0: 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
cef0: 20 69 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74 20   int flags, int 
cf00: 2a 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 69  *pOutFlags);.  i
cf10: 6e 74 20 28 2a 78 44 65 6c 65 74 65 29 28 73 71  nt (*xDelete)(sq
cf20: 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73  lite3_vfs*, cons
cf30: 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69  t char *zName, i
cf40: 6e 74 20 73 79 6e 63 44 69 72 29 3b 0a 20 20 69  nt syncDir);.  i
cf50: 6e 74 20 28 2a 78 41 63 63 65 73 73 29 28 73 71  nt (*xAccess)(sq
cf60: 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73  lite3_vfs*, cons
cf70: 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69  t char *zName, i
cf80: 6e 74 20 66 6c 61 67 73 2c 20 69 6e 74 20 2a 70  nt flags, int *p
cf90: 52 65 73 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28  ResOut);.  int (
cfa0: 2a 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29 28  *xFullPathname)(
cfb0: 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 63 6f  sqlite3_vfs*, co
cfc0: 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
cfd0: 20 69 6e 74 20 6e 4f 75 74 2c 20 63 68 61 72 20   int nOut, char 
cfe0: 2a 7a 4f 75 74 29 3b 0a 20 20 76 6f 69 64 20 2a  *zOut);.  void *
cff0: 28 2a 78 44 6c 4f 70 65 6e 29 28 73 71 6c 69 74  (*xDlOpen)(sqlit
d000: 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63  e3_vfs*, const c
d010: 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 3b  har *zFilename);
d020: 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 45 72 72  .  void (*xDlErr
d030: 6f 72 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a  or)(sqlite3_vfs*
d040: 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61  , int nByte, cha
d050: 72 20 2a 7a 45 72 72 4d 73 67 29 3b 0a 20 20 76  r *zErrMsg);.  v
d060: 6f 69 64 20 28 2a 28 2a 78 44 6c 53 79 6d 29 28  oid (*(*xDlSym)(
d070: 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 76 6f 69  sqlite3_vfs*,voi
d080: 64 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  d*, const char *
d090: 7a 53 79 6d 62 6f 6c 29 29 28 76 6f 69 64 29 3b  zSymbol))(void);
d0a0: 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 43 6c 6f  .  void (*xDlClo
d0b0: 73 65 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a  se)(sqlite3_vfs*
d0c0: 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20  , void*);.  int 
d0d0: 28 2a 78 52 61 6e 64 6f 6d 6e 65 73 73 29 28 73  (*xRandomness)(s
d0e0: 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74  qlite3_vfs*, int
d0f0: 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 4f   nByte, char *zO
d100: 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53 6c  ut);.  int (*xSl
d110: 65 65 70 29 28 73 71 6c 69 74 65 33 5f 76 66 73  eep)(sqlite3_vfs
d120: 2a 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 6f  *, int microseco
d130: 6e 64 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43  nds);.  int (*xC
d140: 75 72 72 65 6e 74 54 69 6d 65 29 28 73 71 6c 69  urrentTime)(sqli
d150: 74 65 33 5f 76 66 73 2a 2c 20 64 6f 75 62 6c 65  te3_vfs*, double
d160: 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 47 65 74  *);.  int (*xGet
d170: 4c 61 73 74 45 72 72 6f 72 29 28 73 71 6c 69 74  LastError)(sqlit
d180: 65 33 5f 76 66 73 2a 2c 20 69 6e 74 2c 20 63 68  e3_vfs*, int, ch
d190: 61 72 20 2a 29 3b 0a 20 20 2f 2a 20 4e 65 77 20  ar *);.  /* New 
d1a0: 66 69 65 6c 64 73 20 6d 61 79 20 62 65 20 61 70  fields may be ap
d1b0: 70 65 6e 64 65 64 20 69 6e 20 66 69 67 75 72 65  pended in figure
d1c0: 20 76 65 72 73 69 6f 6e 73 2e 20 20 54 68 65 20   versions.  The 
d1d0: 69 56 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 76 61  iVersion.  ** va
d1e0: 6c 75 65 20 77 69 6c 6c 20 69 6e 63 72 65 6d 65  lue will increme
d1f0: 6e 74 20 77 68 65 6e 65 76 65 72 20 74 68 69 73  nt whenever this
d200: 20 68 61 70 70 65 6e 73 2e 20 2a 2f 0a 7d 3b 0a   happens. */.};.
d210: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
d220: 20 46 6c 61 67 73 20 66 6f 72 20 74 68 65 20 78   Flags for the x
d230: 41 63 63 65 73 73 20 56 46 53 20 6d 65 74 68 6f  Access VFS metho
d240: 64 20 7b 48 31 31 31 39 30 7d 20 3c 48 31 31 31  d {H11190} <H111
d250: 34 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  40>.**.** These 
d260: 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74  integer constant
d270: 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73  s can be used as
d280: 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d   the third param
d290: 65 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 78  eter to.** the x
d2a0: 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 6f 66  Access method of
d2b0: 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 76 66 73   an [sqlite3_vfs
d2c0: 5d 20 6f 62 6a 65 63 74 2e 20 7b 45 4e 44 7d 20  ] object. {END} 
d2d0: 20 54 68 65 79 20 64 65 74 65 72 6d 69 6e 65 0a   They determine.
d2e0: 2a 2a 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20  ** what kind of 
d2f0: 70 65 72 6d 69 73 73 69 6f 6e 73 20 74 68 65 20  permissions the 
d300: 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 69  xAccess method i
d310: 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 2e 0a 2a  s looking for..*
d320: 2a 20 57 69 74 68 20 53 51 4c 49 54 45 5f 41 43  * With SQLITE_AC
d330: 43 45 53 53 5f 45 58 49 53 54 53 2c 20 74 68 65  CESS_EXISTS, the
d340: 20 78 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a   xAccess method.
d350: 2a 2a 20 73 69 6d 70 6c 79 20 63 68 65 63 6b 73  ** simply checks
d360: 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69 6c   whether the fil
d370: 65 20 65 78 69 73 74 73 2e 0a 2a 2a 20 57 69 74  e exists..** Wit
d380: 68 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  h SQLITE_ACCESS_
d390: 52 45 41 44 57 52 49 54 45 2c 20 74 68 65 20 78  READWRITE, the x
d3a0: 41 63 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a  Access method.**
d3b0: 20 63 68 65 63 6b 73 20 77 68 65 74 68 65 72 20   checks whether 
d3c0: 74 68 65 20 66 69 6c 65 20 69 73 20 62 6f 74 68  the file is both
d3d0: 20 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72   readable and wr
d3e0: 69 74 61 62 6c 65 2e 0a 2a 2a 20 57 69 74 68 20  itable..** With 
d3f0: 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45  SQLITE_ACCESS_RE
d400: 41 44 2c 20 74 68 65 20 78 41 63 63 65 73 73 20  AD, the xAccess 
d410: 6d 65 74 68 6f 64 0a 2a 2a 20 63 68 65 63 6b 73  method.** checks
d420: 20 77 68 65 74 68 65 72 20 74 68 65 20 66 69 6c   whether the fil
d430: 65 20 69 73 20 72 65 61 64 61 62 6c 65 2e 0a 2a  e is readable..*
d440: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
d450: 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 20 20  _ACCESS_EXISTS  
d460: 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    0.#define SQLI
d470: 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52  TE_ACCESS_READWR
d480: 49 54 45 20 31 0a 23 64 65 66 69 6e 65 20 53 51  ITE 1.#define SQ
d490: 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
d4a0: 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 43        2../*.** C
d4b0: 41 50 49 33 52 45 46 3a 20 49 6e 69 74 69 61 6c  API3REF: Initial
d4c0: 69 7a 65 20 54 68 65 20 53 51 4c 69 74 65 20 4c  ize The SQLite L
d4d0: 69 62 72 61 72 79 20 7b 48 31 30 31 33 30 7d 20  ibrary {H10130} 
d4e0: 3c 53 32 30 30 30 30 3e 3c 53 33 30 31 30 30 3e  <S20000><S30100>
d4f0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
d500: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
d510: 72 6f 75 74 69 6e 65 20 69 6e 69 74 69 61 6c 69  routine initiali
d520: 7a 65 73 20 74 68 65 0a 2a 2a 20 53 51 4c 69 74  zes the.** SQLit
d530: 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20  e library.  The 
d540: 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
d550: 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 64 65  () routine.** de
d560: 61 6c 6c 6f 63 61 74 65 73 20 61 6e 79 20 72 65  allocates any re
d570: 73 6f 75 72 63 65 73 20 74 68 61 74 20 77 65 72  sources that wer
d580: 65 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73  e allocated by s
d590: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
d5a0: 65 28 29 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75  e()..** This rou
d5b0: 74 69 6e 65 73 20 61 72 65 20 64 65 73 69 67 6e  tines are design
d5c0: 65 64 20 74 6f 20 61 69 64 20 69 6e 20 70 72 6f  ed to aid in pro
d5d0: 63 65 73 73 20 69 6e 69 74 69 61 6c 69 7a 61 74  cess initializat
d5e0: 69 6f 6e 20 61 6e 64 0a 2a 2a 20 73 68 75 74 64  ion and.** shutd
d5f0: 6f 77 6e 20 6f 6e 20 65 6d 62 65 64 64 65 64 20  own on embedded 
d600: 73 79 73 74 65 6d 73 2e 20 20 57 6f 72 6b 73 74  systems.  Workst
d610: 61 74 69 6f 6e 20 61 70 70 6c 69 63 61 74 69 6f  ation applicatio
d620: 6e 73 20 75 73 69 6e 67 0a 2a 2a 20 53 51 4c 69  ns using.** SQLi
d630: 74 65 20 6e 6f 72 6d 61 6c 6c 79 20 64 6f 20 6e  te normally do n
d640: 6f 74 20 6e 65 65 64 20 74 6f 20 69 6e 76 6f 6b  ot need to invok
d650: 65 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73  e either of thes
d660: 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a  e routines..**.*
d670: 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  * A call to sqli
d680: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
d690: 20 69 73 20 61 6e 20 22 65 66 66 65 63 74 69 76   is an "effectiv
d6a0: 65 22 20 63 61 6c 6c 20 69 66 20 69 74 20 69 73  e" call if it is
d6b0: 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74 69  .** the first ti
d6c0: 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  me sqlite3_initi
d6d0: 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76 6f 6b  alize() is invok
d6e0: 65 64 20 64 75 72 69 6e 67 20 74 68 65 20 6c 69  ed during the li
d6f0: 66 65 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 68 65  fetime of.** the
d700: 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69 66 20   process, or if 
d710: 69 74 20 69 73 20 74 68 65 20 66 69 72 73 74 20  it is the first 
d720: 74 69 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e 69  time sqlite3_ini
d730: 74 69 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76  tialize() is inv
d740: 6f 6b 65 64 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e  oked.** followin
d750: 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  g a call to sqli
d760: 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e 20  te3_shutdown(). 
d770: 20 4f 6e 6c 79 20 61 6e 20 65 66 66 65 63 74 69   Only an effecti
d780: 76 65 20 63 61 6c 6c 0a 2a 2a 20 6f 66 20 73 71  ve call.** of sq
d790: 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
d7a0: 28 29 20 64 6f 65 73 20 61 6e 79 20 69 6e 69 74  () does any init
d7b0: 69 61 6c 69 7a 61 74 69 6f 6e 2e 20 20 41 6c 6c  ialization.  All
d7c0: 20 6f 74 68 65 72 20 63 61 6c 6c 73 0a 2a 2a 20   other calls.** 
d7d0: 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d  are harmless no-
d7e0: 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c  ops..**.** A cal
d7f0: 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75  l to sqlite3_shu
d800: 74 64 6f 77 6e 28 29 20 69 73 20 61 6e 20 22 65  tdown() is an "e
d810: 66 66 65 63 74 69 76 65 22 20 63 61 6c 6c 20 69  ffective" call i
d820: 66 20 69 74 20 69 73 20 74 68 65 20 66 69 72 73  f it is the firs
d830: 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c  t.** call to sql
d840: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20  ite3_shutdown() 
d850: 73 69 6e 63 65 20 74 68 65 20 6c 61 73 74 20 73  since the last s
d860: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
d870: 65 28 29 2e 20 20 4f 6e 6c 79 0a 2a 2a 20 61 6e  e().  Only.** an
d880: 20 65 66 66 65 63 74 69 76 65 20 63 61 6c 6c 20   effective call 
d890: 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64  to sqlite3_shutd
d8a0: 6f 77 6e 28 29 20 64 6f 65 73 20 61 6e 79 20 64  own() does any d
d8b0: 65 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e  einitialization.
d8c0: 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 76 61  .** All other va
d8d0: 6c 69 64 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c  lid calls to sql
d8e0: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 20  ite3_shutdown() 
d8f0: 61 72 65 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d  are harmless no-
d900: 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ops..**.** The s
d910: 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
d920: 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73  e() interface is
d930: 20 74 68 72 65 61 64 73 61 66 65 2c 20 62 75 74   threadsafe, but
d940: 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77   sqlite3_shutdow
d950: 6e 28 29 0a 2a 2a 20 69 73 20 6e 6f 74 2e 20 20  n().** is not.  
d960: 54 68 65 20 73 71 6c 69 74 65 33 5f 73 68 75 74  The sqlite3_shut
d970: 64 6f 77 6e 28 29 20 69 6e 74 65 72 66 61 63 65  down() interface
d980: 20 6d 75 73 74 20 6f 6e 6c 79 20 62 65 20 63 61   must only be ca
d990: 6c 6c 65 64 20 66 72 6f 6d 20 61 0a 2a 2a 20 73  lled from a.** s
d9a0: 69 6e 67 6c 65 20 74 68 72 65 61 64 2e 20 20 41  ingle thread.  A
d9b0: 6c 6c 20 6f 70 65 6e 20 5b 64 61 74 61 62 61 73  ll open [databas
d9c0: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20 6d  e connections] m
d9d0: 75 73 74 20 62 65 20 63 6c 6f 73 65 64 20 61 6e  ust be closed an
d9e0: 64 20 61 6c 6c 0a 2a 2a 20 6f 74 68 65 72 20 53  d all.** other S
d9f0: 51 4c 69 74 65 20 72 65 73 6f 75 72 63 65 73 20  QLite resources 
da00: 6d 75 73 74 20 62 65 20 64 65 61 6c 6c 6f 63 61  must be dealloca
da10: 74 65 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76  ted prior to inv
da20: 6f 6b 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33  oking.** sqlite3
da30: 5f 73 68 75 74 64 6f 77 6e 28 29 2e 0a 2a 2a 0a  _shutdown()..**.
da40: 2a 2a 20 41 6d 6f 6e 67 20 6f 74 68 65 72 20 74  ** Among other t
da50: 68 69 6e 67 73 2c 20 73 71 6c 69 74 65 33 5f 69  hings, sqlite3_i
da60: 6e 69 74 69 61 6c 69 7a 65 28 29 20 77 69 6c 6c  nitialize() will
da70: 20 69 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c 69 74   invoke.** sqlit
da80: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 20 53  e3_os_init().  S
da90: 69 6d 69 6c 61 72 6c 79 2c 20 73 71 6c 69 74 65  imilarly, sqlite
daa0: 33 5f 73 68 75 74 64 6f 77 6e 28 29 0a 2a 2a 20  3_shutdown().** 
dab0: 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 73 71 6c 69  will invoke sqli
dac0: 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 0a 2a 2a  te3_os_end()..**
dad0: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
dae0: 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 72 6f 75  initialize() rou
daf0: 74 69 6e 65 20 72 65 74 75 72 6e 73 20 5b 53 51  tine returns [SQ
db00: 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63  LITE_OK] on succ
db10: 65 73 73 2e 0a 2a 2a 20 49 66 20 66 6f 72 20 73  ess..** If for s
db20: 6f 6d 65 20 72 65 61 73 6f 6e 2c 20 73 71 6c 69  ome reason, sqli
db30: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
db40: 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 69 6e   is unable to in
db50: 69 74 69 61 6c 69 7a 65 0a 2a 2a 20 74 68 65 20  itialize.** the 
db60: 6c 69 62 72 61 72 79 20 28 70 65 72 68 61 70 73  library (perhaps
db70: 20 69 74 20 69 73 20 75 6e 61 62 6c 65 20 74 6f   it is unable to
db80: 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 65 64   allocate a need
db90: 65 64 20 72 65 73 6f 75 72 63 65 20 73 75 63 68  ed resource such
dba0: 0a 2a 2a 20 61 73 20 61 20 6d 75 74 65 78 29 20  .** as a mutex) 
dbb0: 69 74 20 72 65 74 75 72 6e 73 20 61 6e 20 5b 65  it returns an [e
dbc0: 72 72 6f 72 20 63 6f 64 65 5d 20 6f 74 68 65 72  rror code] other
dbd0: 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b   than [SQLITE_OK
dbe0: 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  ]..**.** The sql
dbf0: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
dc00: 29 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  ) routine is cal
dc10: 6c 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 62  led internally b
dc20: 79 20 6d 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20  y many other.** 
dc30: 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65  SQLite interface
dc40: 73 20 73 6f 20 74 68 61 74 20 61 6e 20 61 70 70  s so that an app
dc50: 6c 69 63 61 74 69 6f 6e 20 75 73 75 61 6c 6c 79  lication usually
dc60: 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
dc70: 6f 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 6c 69  o.** invoke sqli
dc80: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
dc90: 20 64 69 72 65 63 74 6c 79 2e 20 20 46 6f 72 20   directly.  For 
dca0: 65 78 61 6d 70 6c 65 2c 20 5b 73 71 6c 69 74 65  example, [sqlite
dcb0: 33 5f 6f 70 65 6e 28 29 5d 0a 2a 2a 20 63 61 6c  3_open()].** cal
dcc0: 6c 73 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  ls sqlite3_initi
dcd0: 61 6c 69 7a 65 28 29 20 73 6f 20 74 68 65 20 53  alize() so the S
dce0: 51 4c 69 74 65 20 6c 69 62 72 61 72 79 20 77 69  QLite library wi
dcf0: 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61  ll be automatica
dd00: 6c 6c 79 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a  lly.** initializ
dd10: 65 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33  ed when [sqlite3
dd20: 5f 6f 70 65 6e 28 29 5d 20 69 73 20 63 61 6c 6c  _open()] is call
dd30: 65 64 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74  ed if it has not
dd40: 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 0a   be initialized.
dd50: 2a 2a 20 61 6c 72 65 61 64 79 2e 20 20 48 6f 77  ** already.  How
dd60: 65 76 65 72 2c 20 69 66 20 53 51 4c 69 74 65 20  ever, if SQLite 
dd70: 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  is compiled with
dd80: 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 4f 4d 49   the [SQLITE_OMI
dd90: 54 5f 41 55 54 4f 49 4e 49 54 5d 0a 2a 2a 20 63  T_AUTOINIT].** c
dda0: 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69  ompile-time opti
ddb0: 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 61 75 74  on, then the aut
ddc0: 6f 6d 61 74 69 63 20 63 61 6c 6c 73 20 74 6f 20  omatic calls to 
ddd0: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
dde0: 7a 65 28 29 0a 2a 2a 20 61 72 65 20 6f 6d 69 74  ze().** are omit
ddf0: 74 65 64 20 61 6e 64 20 74 68 65 20 61 70 70 6c  ted and the appl
de00: 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 63 61 6c  ication must cal
de10: 6c 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  l sqlite3_initia
de20: 6c 69 7a 65 28 29 20 64 69 72 65 63 74 6c 79 0a  lize() directly.
de30: 2a 2a 20 70 72 69 6f 72 20 74 6f 20 75 73 69 6e  ** prior to usin
de40: 67 20 61 6e 79 20 6f 74 68 65 72 20 53 51 4c 69  g any other SQLi
de50: 74 65 20 69 6e 74 65 72 66 61 63 65 2e 20 20 46  te interface.  F
de60: 6f 72 20 6d 61 78 69 6d 75 6d 20 70 6f 72 74 61  or maximum porta
de70: 62 69 6c 69 74 79 2c 0a 2a 2a 20 69 74 20 69 73  bility,.** it is
de80: 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61   recommended tha
de90: 74 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 61  t applications a
dea0: 6c 77 61 79 73 20 69 6e 76 6f 6b 65 20 73 71 6c  lways invoke sql
deb0: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
dec0: 29 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 70 72  ).** directly pr
ded0: 69 6f 72 20 74 6f 20 75 73 69 6e 67 20 61 6e 79  ior to using any
dee0: 20 6f 74 68 65 72 20 53 51 4c 69 74 65 20 69 6e   other SQLite in
def0: 74 65 72 66 61 63 65 2e 20 20 46 75 74 75 72 65  terface.  Future
df00: 20 72 65 6c 65 61 73 65 73 0a 2a 2a 20 6f 66 20   releases.** of 
df10: 53 51 4c 69 74 65 20 6d 61 79 20 72 65 71 75 69  SQLite may requi
df20: 72 65 20 74 68 69 73 2e 20 20 49 6e 20 6f 74 68  re this.  In oth
df30: 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 62 65  er words, the be
df40: 68 61 76 69 6f 72 20 65 78 68 69 62 69 74 65 64  havior exhibited
df50: 0a 2a 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 20  .** when SQLite 
df60: 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  is compiled with
df70: 20 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55   [SQLITE_OMIT_AU
df80: 54 4f 49 4e 49 54 5d 20 6d 69 67 68 74 20 62 65  TOINIT] might be
df90: 63 6f 6d 65 20 74 68 65 0a 2a 2a 20 64 65 66 61  come the.** defa
dfa0: 75 6c 74 20 62 65 68 61 76 69 6f 72 20 69 6e 20  ult behavior in 
dfb0: 73 6f 6d 65 20 66 75 74 75 72 65 20 72 65 6c 65  some future rele
dfc0: 61 73 65 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a  ase of SQLite..*
dfd0: 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
dfe0: 5f 6f 73 5f 69 6e 69 74 28 29 20 72 6f 75 74 69  _os_init() routi
dff0: 6e 65 20 64 6f 65 73 20 6f 70 65 72 61 74 69 6e  ne does operatin
e000: 67 2d 73 79 73 74 65 6d 20 73 70 65 63 69 66 69  g-system specifi
e010: 63 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 61 74  c.** initializat
e020: 69 6f 6e 20 6f 66 20 74 68 65 20 53 51 4c 69 74  ion of the SQLit
e030: 65 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20  e library.  The 
e040: 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29  sqlite3_os_end()
e050: 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 75 6e 64 6f  .** routine undo
e060: 65 73 20 74 68 65 20 65 66 66 65 63 74 20 6f 66  es the effect of
e070: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74   sqlite3_os_init
e080: 28 29 2e 20 20 54 79 70 69 63 61 6c 20 74 61 73  ().  Typical tas
e090: 6b 73 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20  ks.** performed 
e0a0: 62 79 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  by these routine
e0b0: 73 20 69 6e 63 6c 75 64 65 20 61 6c 6c 6f 63 61  s include alloca
e0c0: 74 69 6f 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61  tion or dealloca
e0d0: 74 69 6f 6e 0a 2a 2a 20 6f 66 20 73 74 61 74 69  tion.** of stati
e0e0: 63 20 72 65 73 6f 75 72 63 65 73 2c 20 69 6e 69  c resources, ini
e0f0: 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 67  tialization of g
e100: 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 73 2c  lobal variables,
e110: 0a 2a 2a 20 73 65 74 74 69 6e 67 20 75 70 20 61  .** setting up a
e120: 20 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65   default [sqlite
e130: 33 5f 76 66 73 5d 20 6d 6f 64 75 6c 65 2c 20 6f  3_vfs] module, o
e140: 72 20 73 65 74 74 69 6e 67 20 75 70 0a 2a 2a 20  r setting up.** 
e150: 61 20 64 65 66 61 75 6c 74 20 63 6f 6e 66 69 67  a default config
e160: 75 72 61 74 69 6f 6e 20 75 73 69 6e 67 20 5b 73  uration using [s
e170: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d  qlite3_config()]
e180: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c  ..**.** The appl
e190: 69 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e  ication should n
e1a0: 65 76 65 72 20 69 6e 76 6f 6b 65 20 65 69 74 68  ever invoke eith
e1b0: 65 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  er sqlite3_os_in
e1c0: 69 74 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74  it().** or sqlit
e1d0: 65 33 5f 6f 73 5f 65 6e 64 28 29 20 64 69 72 65  e3_os_end() dire
e1e0: 63 74 6c 79 2e 20 20 54 68 65 20 61 70 70 6c 69  ctly.  The appli
e1f0: 63 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 6e  cation should on
e200: 6c 79 20 69 6e 76 6f 6b 65 0a 2a 2a 20 73 71 6c  ly invoke.** sql
e210: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
e220: 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 73 68  ) and sqlite3_sh
e230: 75 74 64 6f 77 6e 28 29 2e 20 20 54 68 65 20 73  utdown().  The s
e240: 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29  qlite3_os_init()
e250: 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73  .** interface is
e260: 20 63 61 6c 6c 65 64 20 61 75 74 6f 6d 61 74 69   called automati
e270: 63 61 6c 6c 79 20 62 79 20 73 71 6c 69 74 65 33  cally by sqlite3
e280: 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 61 6e  _initialize() an
e290: 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f  d.** sqlite3_os_
e2a0: 65 6e 64 28 29 20 69 73 20 63 61 6c 6c 65 64 20  end() is called 
e2b0: 62 79 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64  by sqlite3_shutd
e2c0: 6f 77 6e 28 29 2e 20 20 41 70 70 72 6f 70 72 69  own().  Appropri
e2d0: 61 74 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  ate.** implement
e2e0: 61 74 69 6f 6e 73 20 66 6f 72 20 73 71 6c 69 74  ations for sqlit
e2f0: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64  e3_os_init() and
e300: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28   sqlite3_os_end(
e310: 29 0a 2a 2a 20 61 72 65 20 62 75 69 6c 74 20 69  ).** are built i
e320: 6e 74 6f 20 53 51 4c 69 74 65 20 77 68 65 6e 20  nto SQLite when 
e330: 69 74 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 66  it is compiled f
e340: 6f 72 20 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73  or Unix, Windows
e350: 2c 20 6f 72 20 4f 53 2f 32 2e 0a 2a 2a 20 57 68  , or OS/2..** Wh
e360: 65 6e 20 5b 63 75 73 74 6f 6d 20 62 75 69 6c 64  en [custom build
e370: 73 20 7c 20 62 75 69 6c 74 20 66 6f 72 20 6f 74  s | built for ot
e380: 68 65 72 20 70 6c 61 74 66 6f 72 6d 73 5d 0a 2a  her platforms].*
e390: 2a 20 28 75 73 69 6e 67 20 74 68 65 20 5b 53 51  * (using the [SQ
e3a0: 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 3d 31 5d  LITE_OS_OTHER=1]
e3b0: 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a   compile-time.**
e3c0: 20 6f 70 74 69 6f 6e 29 20 74 68 65 20 61 70 70   option) the app
e3d0: 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 73 75  lication must su
e3e0: 70 70 6c 79 20 61 20 73 75 69 74 61 62 6c 65 20  pply a suitable 
e3f0: 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66  implementation f
e400: 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 73  or.** sqlite3_os
e410: 5f 69 6e 69 74 28 29 20 61 6e 64 20 73 71 6c 69  _init() and sqli
e420: 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e 20 20 41  te3_os_end().  A
e430: 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73 75  n application-su
e440: 70 70 6c 69 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d  pplied.** implem
e450: 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69  entation of sqli
e460: 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 6f 72  te3_os_init() or
e470: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28   sqlite3_os_end(
e480: 29 0a 2a 2a 20 6d 75 73 74 20 72 65 74 75 72 6e  ).** must return
e490: 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20   [SQLITE_OK] on 
e4a0: 73 75 63 63 65 73 73 20 61 6e 64 20 73 6f 6d 65  success and some
e4b0: 20 6f 74 68 65 72 20 5b 65 72 72 6f 72 20 63 6f   other [error co
e4c0: 64 65 5d 20 75 70 6f 6e 0a 2a 2a 20 66 61 69 6c  de] upon.** fail
e4d0: 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ure..*/.SQLITE_A
e4e0: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
e4f0: 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29 3b  nitialize(void);
e500: 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
e510: 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
e520: 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41  (void);.SQLITE_A
e530: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
e540: 73 5f 69 6e 69 74 28 76 6f 69 64 29 3b 0a 53 51  s_init(void);.SQ
e550: 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
e560: 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64  ite3_os_end(void
e570: 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
e580: 45 46 3a 20 43 6f 6e 66 69 67 75 72 69 6e 67 20  EF: Configuring 
e590: 54 68 65 20 53 51 4c 69 74 65 20 4c 69 62 72 61  The SQLite Libra
e5a0: 72 79 20 7b 48 31 34 31 30 30 7d 20 3c 53 32 30  ry {H14100} <S20
e5b0: 30 30 30 3e 3c 53 33 30 32 30 30 3e 0a 2a 2a 20  000><S30200>.** 
e5c0: 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
e5d0: 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63  ** The sqlite3_c
e5e0: 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63  onfig() interfac
e5f0: 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b  e is used to mak
e600: 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75  e global configu
e610: 72 61 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65  ration.** change
e620: 73 20 74 6f 20 53 51 4c 69 74 65 20 69 6e 20 6f  s to SQLite in o
e630: 72 64 65 72 20 74 6f 20 74 75 6e 65 20 53 51 4c  rder to tune SQL
e640: 69 74 65 20 74 6f 20 74 68 65 20 73 70 65 63 69  ite to the speci
e650: 66 69 63 20 6e 65 65 64 73 20 6f 66 0a 2a 2a 20  fic needs of.** 
e660: 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e  the application.
e670: 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f    The default co
e680: 6e 66 69 67 75 72 61 74 69 6f 6e 20 69 73 20 72  nfiguration is r
e690: 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 6d  ecommended for m
e6a0: 6f 73 74 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69  ost.** applicati
e6b0: 6f 6e 73 20 61 6e 64 20 73 6f 20 74 68 69 73 20  ons and so this 
e6c0: 72 6f 75 74 69 6e 65 20 69 73 20 75 73 75 61 6c  routine is usual
e6d0: 6c 79 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79  ly not necessary
e6e0: 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70 72 6f 76  .  It is.** prov
e6f0: 69 64 65 64 20 74 6f 20 73 75 70 70 6f 72 74 20  ided to support 
e700: 72 61 72 65 20 61 70 70 6c 69 63 61 74 69 6f 6e  rare application
e710: 73 20 77 69 74 68 20 75 6e 75 73 75 61 6c 20 6e  s with unusual n
e720: 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  eeds..**.** The 
e730: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
e740: 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6e 6f   interface is no
e750: 74 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 54  t threadsafe.  T
e760: 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a 2a  he application.*
e770: 2a 20 6d 75 73 74 20 69 6e 73 75 72 65 20 74 68  * must insure th
e780: 61 74 20 6e 6f 20 6f 74 68 65 72 20 53 51 4c 69  at no other SQLi
e790: 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 61 72  te interfaces ar
e7a0: 65 20 69 6e 76 6f 6b 65 64 20 62 79 20 6f 74 68  e invoked by oth
e7b0: 65 72 0a 2a 2a 20 74 68 72 65 61 64 73 20 77 68  er.** threads wh
e7c0: 69 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  ile sqlite3_conf
e7d0: 69 67 28 29 20 69 73 20 72 75 6e 6e 69 6e 67 2e  ig() is running.
e7e0: 20 20 46 75 72 74 68 65 72 6d 6f 72 65 2c 20 73    Furthermore, s
e7f0: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 0a  qlite3_config().
e800: 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 69  ** may only be i
e810: 6e 76 6f 6b 65 64 20 70 72 69 6f 72 20 74 6f 20  nvoked prior to 
e820: 6c 69 62 72 61 72 79 20 69 6e 69 74 69 61 6c 69  library initiali
e830: 7a 61 74 69 6f 6e 20 75 73 69 6e 67 0a 2a 2a 20  zation using.** 
e840: 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c  [sqlite3_initial
e850: 69 7a 65 28 29 5d 20 6f 72 20 61 66 74 65 72 20  ize()] or after 
e860: 73 68 75 74 64 6f 77 6e 20 62 79 20 5b 73 71 6c  shutdown by [sql
e870: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d  ite3_shutdown()]
e880: 2e 0a 2a 2a 20 4e 6f 74 65 2c 20 68 6f 77 65 76  ..** Note, howev
e890: 65 72 2c 20 74 68 61 74 20 73 71 6c 69 74 65 33  er, that sqlite3
e8a0: 5f 63 6f 6e 66 69 67 28 29 20 63 61 6e 20 62 65  _config() can be
e8b0: 20 63 61 6c 6c 65 64 20 61 73 20 70 61 72 74 20   called as part 
e8c0: 6f 66 20 74 68 65 0a 2a 2a 20 69 6d 70 6c 65 6d  of the.** implem
e8d0: 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 61  entation of an a
e8e0: 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
e8f0: 65 64 20 5b 73 71 6c 69 74 65 33 5f 6f 73 5f 69  ed [sqlite3_os_i
e900: 6e 69 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  nit()]..**.** Th
e910: 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
e920: 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66   to sqlite3_conf
e930: 69 67 28 29 20 69 73 20 61 6e 20 69 6e 74 65 67  ig() is an integ
e940: 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f  er.** [SQLITE_CO
e950: 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41  NFIG_SINGLETHREA
e960: 44 20 7c 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  D | configuratio
e970: 6e 20 6f 70 74 69 6f 6e 5d 20 74 68 61 74 20 64  n option] that d
e980: 65 74 65 72 6d 69 6e 65 73 0a 2a 2a 20 77 68 61  etermines.** wha
e990: 74 20 70 72 6f 70 65 72 74 79 20 6f 66 20 53 51  t property of SQ
e9a0: 4c 69 74 65 20 69 73 20 74 6f 20 62 65 20 63 6f  Lite is to be co
e9b0: 6e 66 69 67 75 72 65 64 2e 20 20 53 75 62 73 65  nfigured.  Subse
e9c0: 71 75 65 6e 74 20 61 72 67 75 6d 65 6e 74 73 0a  quent arguments.
e9d0: 2a 2a 20 76 61 72 79 20 64 65 70 65 6e 64 69 6e  ** vary dependin
e9e0: 67 20 6f 6e 20 74 68 65 20 5b 53 51 4c 49 54 45  g on the [SQLITE
e9f0: 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48  _CONFIG_SINGLETH
ea00: 52 45 41 44 20 7c 20 63 6f 6e 66 69 67 75 72 61  READ | configura
ea10: 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d 0a 2a 2a 20  tion option].** 
ea20: 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67  in the first arg
ea30: 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  ument..**.** Whe
ea40: 6e 20 61 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  n a configuratio
ea50: 6e 20 6f 70 74 69 6f 6e 20 69 73 20 73 65 74 2c  n option is set,
ea60: 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
ea70: 29 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54  ) returns [SQLIT
ea80: 45 5f 4f 4b 5d 2e 0a 2a 2a 20 49 66 20 74 68 65  E_OK]..** If the
ea90: 20 6f 70 74 69 6f 6e 20 69 73 20 75 6e 6b 6e 6f   option is unkno
eaa0: 77 6e 20 6f 72 20 53 51 4c 69 74 65 20 69 73 20  wn or SQLite is 
eab0: 75 6e 61 62 6c 65 20 74 6f 20 73 65 74 20 74 68  unable to set th
eac0: 65 20 6f 70 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e  e option.** then
ead0: 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
eae0: 74 75 72 6e 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f  turns a non-zero
eaf0: 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a   [error code]..*
eb00: 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
eb10: 73 3a 0a 2a 2a 20 5b 48 31 34 31 30 33 5d 20 5b  s:.** [H14103] [
eb20: 48 31 34 31 30 36 5d 20 5b 48 31 34 31 32 30 5d  H14106] [H14120]
eb30: 20 5b 48 31 34 31 32 33 5d 20 5b 48 31 34 31 32   [H14123] [H1412
eb40: 36 5d 20 5b 48 31 34 31 32 39 5d 20 5b 48 31 34  6] [H14129] [H14
eb50: 31 33 32 5d 20 5b 48 31 34 31 33 35 5d 0a 2a 2a  132] [H14135].**
eb60: 20 5b 48 31 34 31 33 38 5d 20 5b 48 31 34 31 34   [H14138] [H1414
eb70: 31 5d 20 5b 48 31 34 31 34 34 5d 20 5b 48 31 34  1] [H14144] [H14
eb80: 31 34 37 5d 20 5b 48 31 34 31 35 30 5d 20 5b 48  147] [H14150] [H
eb90: 31 34 31 35 33 5d 20 5b 48 31 34 31 35 36 5d 20  14153] [H14156] 
eba0: 5b 48 31 34 31 35 39 5d 0a 2a 2a 20 5b 48 31 34  [H14159].** [H14
ebb0: 31 36 32 5d 20 5b 48 31 34 31 36 35 5d 20 5b 48  162] [H14165] [H
ebc0: 31 34 31 36 38 5d 0a 2a 2f 0a 53 51 4c 49 54 45  14168].*/.SQLITE
ebd0: 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45  _API SQLITE_EXPE
ebe0: 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c  RIMENTAL int sql
ebf0: 69 74 65 33 5f 63 6f 6e 66 69 67 28 69 6e 74 2c  ite3_config(int,
ec00: 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   ...);../*.** CA
ec10: 50 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72  PI3REF: Configur
ec20: 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
ec30: 63 74 69 6f 6e 73 20 20 7b 48 31 34 32 30 30 7d  ctions  {H14200}
ec40: 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50   <S20000>.** EXP
ec50: 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20  ERIMENTAL.**.** 
ec60: 54 68 65 20 73 71 6c 69 74 65 33 5f 64 62 5f 63  The sqlite3_db_c
ec70: 6f 6e 66 69 67 28 29 20 69 6e 74 65 72 66 61 63  onfig() interfac
ec80: 65 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b  e is used to mak
ec90: 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 0a  e configuration.
eca0: 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 61 20  ** changes to a 
ecb0: 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
ecc0: 74 69 6f 6e 5d 2e 20 20 54 68 65 20 69 6e 74 65  tion].  The inte
ecd0: 72 66 61 63 65 20 69 73 20 73 69 6d 69 6c 61 72  rface is similar
ece0: 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   to.** [sqlite3_
ecf0: 63 6f 6e 66 69 67 28 29 5d 20 65 78 63 65 70 74  config()] except
ed00: 20 74 68 61 74 20 74 68 65 20 63 68 61 6e 67 65   that the change
ed10: 73 20 61 70 70 6c 79 20 74 6f 20 61 20 73 69 6e  s apply to a sin
ed20: 67 6c 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65  gle.** [database
ed30: 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 73 70   connection] (sp
ed40: 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 66  ecified in the f
ed50: 69 72 73 74 20 61 72 67 75 6d 65 6e 74 29 2e 20  irst argument). 
ed60: 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   The.** sqlite3_
ed70: 64 62 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 65  db_config() inte
ed80: 72 66 61 63 65 20 63 61 6e 20 6f 6e 6c 79 20 62  rface can only b
ed90: 65 20 75 73 65 64 20 69 6d 6d 65 64 69 61 74 65  e used immediate
eda0: 6c 79 20 61 66 74 65 72 0a 2a 2a 20 74 68 65 20  ly after.** the 
edb0: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
edc0: 69 6f 6e 20 69 73 20 63 72 65 61 74 65 64 20 75  ion is created u
edd0: 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6f 70  sing [sqlite3_op
ede0: 65 6e 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74  en()],.** [sqlit
edf0: 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20 6f 72  e3_open16()], or
ee00: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76   [sqlite3_open_v
ee10: 32 28 29 5d 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68  2()].  .**.** Th
ee20: 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
ee30: 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 64 62 5f  t to sqlite3_db_
ee40: 63 6f 6e 66 69 67 28 44 2c 56 2c 2e 2e 2e 29 20  config(D,V,...) 
ee50: 20 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6e 66 69   is the.** confi
ee60: 67 75 72 61 74 69 6f 6e 20 76 65 72 62 20 2d 20  guration verb - 
ee70: 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 65 20  an integer code 
ee80: 74 68 61 74 20 69 6e 64 69 63 61 74 65 73 20 77  that indicates w
ee90: 68 61 74 0a 2a 2a 20 61 73 70 65 63 74 20 6f 66  hat.** aspect of
eea0: 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
eeb0: 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20 62 65  onnection] is be
eec0: 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 2e 0a  ing configured..
eed0: 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 63 68 6f 69  ** The only choi
eee0: 63 65 20 66 6f 72 20 74 68 69 73 20 76 61 6c 75  ce for this valu
eef0: 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 43  e is [SQLITE_DBC
ef00: 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 5d  ONFIG_LOOKASIDE]
ef10: 2e 0a 2a 2a 20 4e 65 77 20 76 65 72 62 73 20 61  ..** New verbs a
ef20: 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20  re likely to be 
ef30: 61 64 64 65 64 20 69 6e 20 66 75 74 75 72 65 20  added in future 
ef40: 72 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69  releases of SQLi
ef50: 74 65 2e 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61  te..** Additiona
ef60: 6c 20 61 72 67 75 6d 65 6e 74 73 20 64 65 70 65  l arguments depe
ef70: 6e 64 20 6f 6e 20 74 68 65 20 76 65 72 62 2e 0a  nd on the verb..
ef80: 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
ef90: 74 73 3a 0a 2a 2a 20 5b 48 31 34 32 30 33 5d 20  ts:.** [H14203] 
efa0: 5b 48 31 34 32 30 36 5d 20 5b 48 31 34 32 30 39  [H14206] [H14209
efb0: 5d 20 5b 48 31 34 32 31 32 5d 20 5b 48 31 34 32  ] [H14212] [H142
efc0: 31 35 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  15].*/.SQLITE_AP
efd0: 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d  I SQLITE_EXPERIM
efe0: 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65  ENTAL int sqlite
eff0: 33 5f 64 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69  3_db_config(sqli
f000: 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e  te3*, int op, ..
f010: 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .);../*.** CAPI3
f020: 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f  REF: Memory Allo
f030: 63 61 74 69 6f 6e 20 52 6f 75 74 69 6e 65 73 20  cation Routines 
f040: 7b 48 31 30 31 35 35 7d 20 3c 53 32 30 31 32 30  {H10155} <S20120
f050: 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  >.** EXPERIMENTA
f060: 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  L.**.** An insta
f070: 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65  nce of this obje
f080: 63 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 69  ct defines the i
f090: 6e 74 65 72 66 61 63 65 20 62 65 74 77 65 65 6e  nterface between
f0a0: 20 53 51 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 6c   SQLite.** and l
f0b0: 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20  ow-level memory 
f0c0: 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69  allocation routi
f0d0: 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  nes..**.** This 
f0e0: 6f 62 6a 65 63 74 20 69 73 20 75 73 65 64 20 69  object is used i
f0f0: 6e 20 6f 6e 6c 79 20 6f 6e 65 20 70 6c 61 63 65  n only one place
f100: 20 69 6e 20 74 68 65 20 53 51 4c 69 74 65 20 69   in the SQLite i
f110: 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 20 41 20 70  nterface..** A p
f120: 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73  ointer to an ins
f130: 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62  tance of this ob
f140: 6a 65 63 74 20 69 73 20 74 68 65 20 61 72 67 75  ject is the argu
f150: 6d 65 6e 74 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69  ment to.** [sqli
f160: 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 77 68  te3_config()] wh
f170: 65 6e 20 74 68 65 20 63 6f 6e 66 69 67 75 72 61  en the configura
f180: 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 0a 2a  tion option is.*
f190: 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  * [SQLITE_CONFIG
f1a0: 5f 4d 41 4c 4c 4f 43 5d 20 6f 72 20 5b 53 51 4c  _MALLOC] or [SQL
f1b0: 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41  ITE_CONFIG_GETMA
f1c0: 4c 4c 4f 43 5d 2e 20 20 0a 2a 2a 20 42 79 20 63  LLOC].  .** By c
f1d0: 72 65 61 74 69 6e 67 20 61 6e 20 69 6e 73 74 61  reating an insta
f1e0: 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65  nce of this obje
f1f0: 63 74 0a 2a 2a 20 61 6e 64 20 70 61 73 73 69 6e  ct.** and passin
f200: 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33  g it to [sqlite3
f210: 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45  _config]([SQLITE
f220: 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 5d 29  _CONFIG_MALLOC])
f230: 0a 2a 2a 20 64 75 72 69 6e 67 20 63 6f 6e 66 69  .** during confi
f240: 67 75 72 61 74 69 6f 6e 2c 20 61 6e 20 61 70 70  guration, an app
f250: 6c 69 63 61 74 69 6f 6e 20 63 61 6e 20 73 70 65  lication can spe
f260: 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74  cify an alternat
f270: 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c  ive.** memory al
f280: 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
f290: 65 6d 20 66 6f 72 20 53 51 4c 69 74 65 20 74 6f  em for SQLite to
f2a0: 20 75 73 65 20 66 6f 72 20 61 6c 6c 20 6f 66 20   use for all of 
f2b0: 69 74 73 0a 2a 2a 20 64 79 6e 61 6d 69 63 20 6d  its.** dynamic m
f2c0: 65 6d 6f 72 79 20 6e 65 65 64 73 2e 0a 2a 2a 0a  emory needs..**.
f2d0: 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 53 51 4c  ** Note that SQL
f2e0: 69 74 65 20 63 6f 6d 65 73 20 77 69 74 68 20 73  ite comes with s
f2f0: 65 76 65 72 61 6c 20 5b 62 75 69 6c 74 2d 69 6e  everal [built-in
f300: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
f310: 72 73 5d 0a 2a 2a 20 74 68 61 74 20 61 72 65 20  rs].** that are 
f320: 70 65 72 66 65 63 74 6c 79 20 61 64 65 71 75 61  perfectly adequa
f330: 74 65 20 66 6f 72 20 74 68 65 20 6f 76 65 72 77  te for the overw
f340: 68 65 6c 6d 69 6e 67 20 6d 61 6a 6f 72 69 74 79  helming majority
f350: 20 6f 66 20 61 70 70 6c 69 63 61 74 69 6f 6e 73   of applications
f360: 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74 68 69  .** and that thi
f370: 73 20 6f 62 6a 65 63 74 20 69 73 20 6f 6e 6c 79  s object is only
f380: 20 75 73 65 66 75 6c 20 74 6f 20 61 20 74 69 6e   useful to a tin
f390: 79 20 6d 69 6e 6f 72 69 74 79 20 6f 66 20 61 70  y minority of ap
f3a0: 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 77 69  plications.** wi
f3b0: 74 68 20 73 70 65 63 69 61 6c 69 7a 65 64 20 6d  th specialized m
f3c0: 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
f3d0: 20 72 65 71 75 69 72 65 6d 65 6e 74 73 2e 20 20   requirements.  
f3e0: 54 68 69 73 20 6f 62 6a 65 63 74 20 69 73 0a 2a  This object is.*
f3f0: 2a 20 61 6c 73 6f 20 75 73 65 64 20 64 75 72 69  * also used duri
f400: 6e 67 20 74 65 73 74 69 6e 67 20 6f 66 20 53 51  ng testing of SQ
f410: 4c 69 74 65 20 69 6e 20 6f 72 64 65 72 20 74 6f  Lite in order to
f420: 20 73 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65   specify an alte
f430: 72 6e 61 74 69 76 65 0a 2a 2a 20 6d 65 6d 6f 72  rnative.** memor
f440: 79 20 61 6c 6c 6f 63 61 74 6f 72 20 74 68 61 74  y allocator that
f450: 20 73 69 6d 75 6c 61 74 65 73 20 6d 65 6d 6f 72   simulates memor
f460: 79 20 6f 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20  y out-of-memory 
f470: 63 6f 6e 64 69 74 69 6f 6e 73 20 69 6e 0a 2a 2a  conditions in.**
f480: 20 6f 72 64 65 72 20 74 6f 20 76 65 72 69 66 79   order to verify
f490: 20 74 68 61 74 20 53 51 4c 69 74 65 20 72 65 63   that SQLite rec
f4a0: 6f 76 65 72 73 20 67 72 61 63 65 66 75 6c 6c 79  overs gracefully
f4b0: 20 66 72 6f 6d 20 73 75 63 68 0a 2a 2a 20 63 6f   from such.** co
f4c0: 6e 64 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20  nditions..**.** 
f4d0: 54 68 65 20 78 4d 61 6c 6c 6f 63 20 61 6e 64 20  The xMalloc and 
f4e0: 78 46 72 65 65 20 6d 65 74 68 6f 64 73 20 6d 75  xFree methods mu
f4f0: 73 74 20 77 6f 72 6b 20 6c 69 6b 65 20 74 68 65  st work like the
f500: 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 61 6e 64  .** malloc() and
f510: 20 66 72 65 65 28 29 20 66 75 6e 63 74 69 6f 6e   free() function
f520: 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64  s from the stand
f530: 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a  ard C library..*
f540: 2a 20 54 68 65 20 78 52 65 61 6c 6c 6f 63 20 6d  * The xRealloc m
f550: 65 74 68 6f 64 20 6d 75 73 74 20 77 6f 72 6b 20  ethod must work 
f560: 6c 69 6b 65 20 72 65 61 6c 6c 6f 63 28 29 20 66  like realloc() f
f570: 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61 72 64  rom the standard
f580: 20 43 20 6c 69 62 72 61 72 79 0a 2a 2a 20 77 69   C library.** wi
f590: 74 68 20 74 68 65 20 65 78 63 65 70 74 69 6f 6e  th the exception
f5a0: 20 74 68 61 74 20 69 66 20 74 68 65 20 73 65 63   that if the sec
f5b0: 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ond argument to 
f5c0: 78 52 65 61 6c 6c 6f 63 20 69 73 20 7a 65 72 6f  xRealloc is zero
f5d0: 2c 0a 2a 2a 20 78 52 65 61 6c 6c 6f 63 20 6d 75  ,.** xRealloc mu
f5e0: 73 74 20 62 65 20 61 20 6e 6f 2d 6f 70 20 2d 20  st be a no-op - 
f5f0: 69 74 20 6d 75 73 74 20 6e 6f 74 20 70 65 72 66  it must not perf
f600: 6f 72 6d 20 61 6e 79 20 61 6c 6c 6f 63 61 74 69  orm any allocati
f610: 6f 6e 20 6f 72 0a 2a 2a 20 64 65 61 6c 6c 6f 63  on or.** dealloc
f620: 61 74 69 6f 6e 2e 20 20 53 51 4c 69 74 65 20 67  ation.  SQLite g
f630: 75 61 72 61 6e 74 65 65 64 73 20 74 68 61 74 20  uaranteeds that 
f640: 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
f650: 65 6e 74 20 74 6f 0a 2a 2a 20 78 52 65 61 6c 6c  ent to.** xReall
f660: 6f 63 20 69 73 20 61 6c 77 61 79 73 20 61 20 76  oc is always a v
f670: 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
f680: 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f   a prior call to
f690: 20 78 52 6f 75 6e 64 75 70 2e 0a 2a 2a 20 41 6e   xRoundup..** An
f6a0: 64 20 73 6f 20 69 6e 20 63 61 73 65 73 20 77 68  d so in cases wh
f6b0: 65 72 65 20 78 52 6f 75 6e 64 75 70 20 61 6c 77  ere xRoundup alw
f6c0: 61 79 73 20 72 65 74 75 72 6e 73 20 61 20 70 6f  ays returns a po
f6d0: 73 69 74 69 76 65 20 6e 75 6d 62 65 72 2c 0a 2a  sitive number,.*
f6e0: 2a 20 78 52 65 61 6c 6c 6f 63 20 63 61 6e 20 70  * xRealloc can p
f6f0: 65 72 66 6f 72 6d 20 65 78 61 63 74 6c 79 20 61  erform exactly a
f700: 73 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 6c  s the standard l
f710: 69 62 72 61 72 79 20 72 65 61 6c 6c 6f 63 28 29  ibrary realloc()
f720: 20 61 6e 64 0a 2a 2a 20 73 74 69 6c 6c 20 62 65   and.** still be
f730: 20 69 6e 20 63 6f 6d 70 6c 69 61 6e 63 65 20 77   in compliance w
f740: 69 74 68 20 74 68 69 73 20 73 70 65 63 69 66 69  ith this specifi
f750: 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 78 53  cation..**.** xS
f760: 69 7a 65 20 73 68 6f 75 6c 64 20 72 65 74 75 72  ize should retur
f770: 6e 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20  n the allocated 
f780: 73 69 7a 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79  size of a memory
f790: 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 70   allocation.** p
f7a0: 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e  reviously obtain
f7b0: 65 64 20 66 72 6f 6d 20 78 4d 61 6c 6c 6f 63 20  ed from xMalloc 
f7c0: 6f 72 20 78 52 65 61 6c 6c 6f 63 2e 20 20 54 68  or xRealloc.  Th
f7d0: 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65  e allocated size
f7e0: 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61 74  .** is always at
f7f0: 20 6c 65 61 73 74 20 61 73 20 62 69 67 20 61 73   least as big as
f800: 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 73   the requested s
f810: 69 7a 65 20 62 75 74 20 6d 61 79 20 62 65 20 6c  ize but may be l
f820: 61 72 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  arger..**.** The
f830: 20 78 52 6f 75 6e 64 75 70 20 6d 65 74 68 6f 64   xRoundup method
f840: 20 72 65 74 75 72 6e 73 20 77 68 61 74 20 77 6f   returns what wo
f850: 75 6c 64 20 62 65 20 74 68 65 20 61 6c 6c 6f 63  uld be the alloc
f860: 61 74 65 64 20 73 69 7a 65 20 6f 66 0a 2a 2a 20  ated size of.** 
f870: 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
f880: 69 6f 6e 20 67 69 76 65 6e 20 61 20 70 61 72 74  ion given a part
f890: 69 63 75 6c 61 72 20 72 65 71 75 65 73 74 65 64  icular requested
f8a0: 20 73 69 7a 65 2e 20 20 4d 6f 73 74 20 6d 65 6d   size.  Most mem
f8b0: 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 6f 72  ory.** allocator
f8c0: 73 20 72 6f 75 6e 64 20 75 70 20 6d 65 6d 6f 72  s round up memor
f8d0: 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 74  y allocations at
f8e0: 20 6c 65 61 73 74 20 74 6f 20 74 68 65 20 6e 65   least to the ne
f8f0: 78 74 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 6f  xt multiple.** o
f900: 66 20 38 2e 20 20 53 6f 6d 65 20 61 6c 6c 6f 63  f 8.  Some alloc
f910: 61 74 6f 72 73 20 72 6f 75 6e 64 20 75 70 20 74  ators round up t
f920: 6f 20 61 20 6c 61 72 67 65 72 20 6d 75 6c 74 69  o a larger multi
f930: 70 6c 65 20 6f 72 20 74 6f 20 61 20 70 6f 77 65  ple or to a powe
f940: 72 20 6f 66 20 32 2e 0a 2a 2a 20 45 76 65 72 79  r of 2..** Every
f950: 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
f960: 6f 6e 20 72 65 71 75 65 73 74 20 63 6f 6d 69 6e  on request comin
f970: 67 20 69 6e 20 74 68 72 6f 75 67 68 20 5b 73 71  g in through [sq
f980: 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 0a  lite3_malloc()].
f990: 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72  ** or [sqlite3_r
f9a0: 65 61 6c 6c 6f 63 28 29 5d 20 66 69 72 73 74 20  ealloc()] first 
f9b0: 63 61 6c 6c 73 20 78 52 6f 75 6e 64 75 70 2e 20  calls xRoundup. 
f9c0: 20 49 66 20 78 52 6f 75 6e 64 75 70 20 72 65 74   If xRoundup ret
f9d0: 75 72 6e 73 20 30 2c 20 0a 2a 2a 20 74 68 61 74  urns 0, .** that
f9e0: 20 63 61 75 73 65 73 20 74 68 65 20 63 6f 72 72   causes the corr
f9f0: 65 73 70 6f 6e 64 69 6e 67 20 6d 65 6d 6f 72 79  esponding memory
fa00: 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 66   allocation to f
fa10: 61 69 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  ail..**.** The x
fa20: 49 6e 69 74 20 6d 65 74 68 6f 64 20 69 6e 69 74  Init method init
fa30: 69 61 6c 69 7a 65 73 20 74 68 65 20 6d 65 6d 6f  ializes the memo
fa40: 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 20 20 28  ry allocator.  (
fa50: 46 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20  For example,.** 
fa60: 69 74 20 6d 69 67 68 74 20 61 6c 6c 6f 63 61 74  it might allocat
fa70: 65 20 61 6e 79 20 72 65 71 75 69 72 65 20 6d 75  e any require mu
fa80: 74 65 78 65 73 20 6f 72 20 69 6e 69 74 69 61 6c  texes or initial
fa90: 69 7a 65 20 69 6e 74 65 72 6e 61 6c 20 64 61 74  ize internal dat
faa0: 61 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 2e  a.** structures.
fab0: 20 20 54 68 65 20 78 53 68 75 74 64 6f 77 6e 20    The xShutdown 
fac0: 6d 65 74 68 6f 64 20 69 73 20 69 6e 76 6f 6b 65  method is invoke
fad0: 64 20 28 69 6e 64 69 72 65 63 74 6c 79 29 20 62  d (indirectly) b
fae0: 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 68  y.** [sqlite3_sh
faf0: 75 74 64 6f 77 6e 28 29 5d 20 61 6e 64 20 73 68  utdown()] and sh
fb00: 6f 75 6c 64 20 64 65 61 6c 6c 6f 63 61 74 65 20  ould deallocate 
fb10: 61 6e 79 20 72 65 73 6f 75 72 63 65 73 20 61 63  any resources ac
fb20: 71 75 69 72 65 64 0a 2a 2a 20 62 79 20 78 49 6e  quired.** by xIn
fb30: 69 74 2e 20 20 54 68 65 20 70 41 70 70 44 61 74  it.  The pAppDat
fb40: 61 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65  a pointer is use
fb50: 64 20 61 73 20 74 68 65 20 6f 6e 6c 79 20 70 61  d as the only pa
fb60: 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 78 49  rameter to.** xI
fb70: 6e 69 74 20 61 6e 64 20 78 53 68 75 74 64 6f 77  nit and xShutdow
fb80: 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  n..**.** SQLite 
fb90: 68 6f 6c 64 73 20 74 68 65 20 5b 53 51 4c 49 54  holds the [SQLIT
fba0: 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
fbb0: 41 53 54 45 52 5d 20 6d 75 74 65 78 20 77 68 65  ASTER] mutex whe
fbc0: 6e 20 69 74 20 69 6e 76 6f 6b 65 73 0a 2a 2a 20  n it invokes.** 
fbd0: 74 68 65 20 78 49 6e 69 74 20 6d 65 74 68 6f 64  the xInit method
fbe0: 2c 20 73 6f 20 74 68 65 20 78 49 6e 69 74 20 6d  , so the xInit m
fbf0: 65 74 68 6f 64 20 6e 65 65 64 20 6e 6f 74 20 62  ethod need not b
fc00: 65 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 54  e threadsafe.  T
fc10: 68 65 0a 2a 2a 20 78 53 68 75 74 64 6f 77 6e 20  he.** xShutdown 
fc20: 6d 65 74 68 6f 64 20 69 73 20 6f 6e 6c 79 20 63  method is only c
fc30: 61 6c 6c 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69  alled from [sqli
fc40: 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d 20  te3_shutdown()] 
fc50: 73 6f 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f  so it does.** no
fc60: 74 20 6e 65 65 64 20 74 6f 20 62 65 20 74 68 72  t need to be thr
fc70: 65 61 64 73 61 66 65 20 65 69 74 68 65 72 2e 20  eadsafe either. 
fc80: 20 46 6f 72 20 61 6c 6c 20 6f 74 68 65 72 20 6d   For all other m
fc90: 65 74 68 6f 64 73 2c 20 53 51 4c 69 74 65 0a 2a  ethods, SQLite.*
fca0: 2a 20 68 6f 6c 64 73 20 74 68 65 20 5b 53 51 4c  * holds the [SQL
fcb0: 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
fcc0: 5f 4d 45 4d 5d 20 6d 75 74 65 78 20 61 73 20 6c  _MEM] mutex as l
fcd0: 6f 6e 67 20 61 73 20 74 68 65 0a 2a 2a 20 5b 53  ong as the.** [S
fce0: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d  QLITE_CONFIG_MEM
fcf0: 53 54 41 54 55 53 5d 20 63 6f 6e 66 69 67 75 72  STATUS] configur
fd00: 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 20  ation option is 
fd10: 74 75 72 6e 65 64 20 6f 6e 20 28 77 68 69 63 68  turned on (which
fd20: 0a 2a 2a 20 69 74 20 69 73 20 62 79 20 64 65 66  .** it is by def
fd30: 61 75 6c 74 29 20 61 6e 64 20 73 6f 20 74 68 65  ault) and so the
fd40: 20 6d 65 74 68 6f 64 73 20 61 72 65 20 61 75 74   methods are aut
fd50: 6f 6d 61 74 69 63 61 6c 6c 79 20 73 65 72 69 61  omatically seria
fd60: 6c 69 7a 65 64 2e 0a 2a 2a 20 48 6f 77 65 76 65  lized..** Howeve
fd70: 72 2c 20 69 66 20 5b 53 51 4c 49 54 45 5f 43 4f  r, if [SQLITE_CO
fd80: 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 5d 20  NFIG_MEMSTATUS] 
fd90: 69 73 20 64 69 73 61 62 6c 65 64 2c 20 74 68 65  is disabled, the
fda0: 6e 20 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 6d  n the other.** m
fdb0: 65 74 68 6f 64 73 20 6d 75 73 74 20 62 65 20 74  ethods must be t
fdc0: 68 72 65 61 64 73 61 66 65 20 6f 72 20 65 6c 73  hreadsafe or els
fdd0: 65 20 6d 61 6b 65 20 74 68 65 69 72 20 6f 77 6e  e make their own
fde0: 20 61 72 72 61 6e 67 65 6d 65 6e 74 73 20 66 6f   arrangements fo
fdf0: 72 0a 2a 2a 20 73 65 72 69 61 6c 69 7a 61 74 69  r.** serializati
fe00: 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  on..**.** SQLite
fe10: 20 77 69 6c 6c 20 6e 65 76 65 72 20 69 6e 76 6f   will never invo
fe20: 6b 65 20 78 49 6e 69 74 28 29 20 6d 6f 72 65 20  ke xInit() more 
fe30: 74 68 61 6e 20 6f 6e 63 65 20 77 69 74 68 6f 75  than once withou
fe40: 74 20 61 6e 20 69 6e 74 65 72 76 65 6e 69 6e 67  t an intervening
fe50: 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 53 68 75  .** call to xShu
fe60: 74 64 6f 77 6e 28 29 2e 0a 2a 2f 0a 74 79 70 65  tdown()..*/.type
fe70: 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
fe80: 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 73  e3_mem_methods s
fe90: 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
fea0: 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74  ds;.struct sqlit
feb0: 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 7b  e3_mem_methods {
fec0: 0a 20 20 76 6f 69 64 20 2a 28 2a 78 4d 61 6c 6c  .  void *(*xMall
fed0: 6f 63 29 28 69 6e 74 29 3b 20 20 20 20 20 20 20  oc)(int);       
fee0: 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f    /* Memory allo
fef0: 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  cation function 
ff00: 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 72 65  */.  void (*xFre
ff10: 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20  e)(void*);      
ff20: 20 20 20 20 2f 2a 20 46 72 65 65 20 61 20 70 72      /* Free a pr
ff30: 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a  ior allocation *
ff40: 2f 0a 20 20 76 6f 69 64 20 2a 28 2a 78 52 65 61  /.  void *(*xRea
ff50: 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c 69 6e 74 29  lloc)(void*,int)
ff60: 3b 20 20 2f 2a 20 52 65 73 69 7a 65 20 61 6e 20  ;  /* Resize an 
ff70: 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20  allocation */.  
ff80: 69 6e 74 20 28 2a 78 53 69 7a 65 29 28 76 6f 69  int (*xSize)(voi
ff90: 64 2a 29 3b 20 20 20 20 20 20 20 20 20 20 20 2f  d*);           /
ffa0: 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a  * Return the siz
ffb0: 65 20 6f 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69  e of an allocati
ffc0: 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 52  on */.  int (*xR
ffd0: 6f 75 6e 64 75 70 29 28 69 6e 74 29 3b 20 20 20  oundup)(int);   
ffe0: 20 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e 64 20         /* Round 
fff0: 75 70 20 72 65 71 75 65 73 74 20 73 69 7a 65 20  up request size 
10000 74 6f 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69  to allocation si
10010 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 49  ze */.  int (*xI
10020 6e 69 74 29 28 76 6f 69 64 2a 29 3b 20 20 20 20  nit)(void*);    
10030 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61         /* Initia
10040 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  lize the memory 
10050 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 76  allocator */.  v
10060 6f 69 64 20 28 2a 78 53 68 75 74 64 6f 77 6e 29  oid (*xShutdown)
10070 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20 2f 2a  (void*);      /*
10080 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68   Deinitialize th
10090 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
100a0 6f 72 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  or */.  void *pA
100b0 70 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  ppData;         
100c0 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
100d0 6e 74 20 74 6f 20 78 49 6e 69 74 28 29 20 61 6e  nt to xInit() an
100e0 64 20 78 53 68 75 74 64 6f 77 6e 28 29 20 2a 2f  d xShutdown() */
100f0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .};../*.** CAPI3
10100 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 61 74 69  REF: Configurati
10110 6f 6e 20 4f 70 74 69 6f 6e 73 20 7b 48 31 30 31  on Options {H101
10120 36 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20  60} <S20000>.** 
10130 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
10140 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e  ** These constan
10150 74 73 20 61 72 65 20 74 68 65 20 61 76 61 69 6c  ts are the avail
10160 61 62 6c 65 20 69 6e 74 65 67 65 72 20 63 6f 6e  able integer con
10170 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f  figuration optio
10180 6e 73 20 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62  ns that.** can b
10190 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
101a0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
101b0 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63  o the [sqlite3_c
101c0 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66 61  onfig()] interfa
101d0 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f  ce..**.** New co
101e0 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69  nfiguration opti
101f0 6f 6e 73 20 6d 61 79 20 62 65 20 61 64 64 65 64  ons may be added
10200 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61   in future relea
10210 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a  ses of SQLite..*
10220 2a 20 45 78 69 73 74 69 6e 67 20 63 6f 6e 66 69  * Existing confi
10230 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73  guration options
10240 20 6d 69 67 68 74 20 62 65 20 64 69 73 63 6f 6e   might be discon
10250 74 69 6e 75 65 64 2e 20 20 41 70 70 6c 69 63 61  tinued.  Applica
10260 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20  tions.** should 
10270 63 68 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e  check the return
10280 20 63 6f 64 65 20 66 72 6f 6d 20 5b 73 71 6c 69   code from [sqli
10290 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 74 6f  te3_config()] to
102a0 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a   make sure that.
102b0 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f 72 6b  ** the call work
102c0 65 64 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65  ed.  The [sqlite
102d0 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65  3_config()] inte
102e0 72 66 61 63 65 20 77 69 6c 6c 20 72 65 74 75 72  rface will retur
102f0 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20  n a.** non-zero 
10300 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 66 20  [error code] if 
10310 61 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f  a discontinued o
10320 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20 63 6f  r unsupported co
10330 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69  nfiguration opti
10340 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64  on.** is invoked
10350 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20  ..**.** <dl>.** 
10360 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49  <dt>SQLITE_CONFI
10370 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 3c 2f  G_SINGLETHREAD</
10380 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 72 65  dt>.** <dd>There
10390 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65 6e 74   are no argument
103a0 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e  s to this option
103b0 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 64  .  This option d
103c0 69 73 61 62 6c 65 73 0a 2a 2a 20 61 6c 6c 20 6d  isables.** all m
103d0 75 74 65 78 69 6e 67 20 61 6e 64 20 70 75 74 73  utexing and puts
103e0 20 53 51 4c 69 74 65 20 69 6e 74 6f 20 61 20 6d   SQLite into a m
103f0 6f 64 65 20 77 68 65 72 65 20 69 74 20 63 61 6e  ode where it can
10400 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 0a 2a 2a   only be used.**
10410 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74 68 72   by a single thr
10420 65 61 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ead.</dd>.**.** 
10430 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49  <dt>SQLITE_CONFI
10440 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 3c 2f 64  G_MULTITHREAD</d
10450 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 72 65 20  t>.** <dd>There 
10460 61 72 65 20 6e 6f 20 61 72 67 75 6d 65 6e 74 73  are no arguments
10470 20 74 6f 20 74 68 69 73 20 6f 70 74 69 6f 6e 2e   to this option.
10480 20 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 64 69    This option di
10490 73 61 62 6c 65 73 0a 2a 2a 20 6d 75 74 65 78 69  sables.** mutexi
104a0 6e 67 20 6f 6e 20 5b 64 61 74 61 62 61 73 65 20  ng on [database 
104b0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64 20  connection] and 
104c0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
104d0 65 6e 74 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a  ent] objects..**
104e0 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   The application
104f0 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
10500 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e 67 20  for serializing 
10510 61 63 63 65 73 73 20 74 6f 0a 2a 2a 20 5b 64 61  access to.** [da
10520 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
10530 6e 73 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65  ns] and [prepare
10540 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 2e 20 20  d statements].  
10550 42 75 74 20 6f 74 68 65 72 20 6d 75 74 65 78 65  But other mutexe
10560 73 0a 2a 2a 20 61 72 65 20 65 6e 61 62 6c 65 64  s.** are enabled
10570 20 73 6f 20 74 68 61 74 20 53 51 4c 69 74 65 20   so that SQLite 
10580 77 69 6c 6c 20 62 65 20 73 61 66 65 20 74 6f 20  will be safe to 
10590 75 73 65 20 69 6e 20 61 20 6d 75 6c 74 69 2d 74  use in a multi-t
105a0 68 72 65 61 64 65 64 0a 2a 2a 20 65 6e 76 69 72  hreaded.** envir
105b0 6f 6e 6d 65 6e 74 20 61 73 20 6c 6f 6e 67 20 61  onment as long a
105c0 73 20 6e 6f 20 74 77 6f 20 74 68 72 65 61 64 73  s no two threads
105d0 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20   attempt to use 
105e0 74 68 65 20 73 61 6d 65 0a 2a 2a 20 5b 64 61 74  the same.** [dat
105f0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
10600 5d 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69  ] at the same ti
10610 6d 65 2e 20 20 53 65 65 20 74 68 65 20 5b 74 68  me.  See the [th
10620 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 0a 2a 2a  reading mode].**
10630 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66   documentation f
10640 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
10650 66 6f 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64 3e 0a  formation.</dd>.
10660 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
10670 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a  _CONFIG_SERIALIZ
10680 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  ED</dt>.** <dd>T
10690 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75  here are no argu
106a0 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70  ments to this op
106b0 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 69  tion.  This opti
106c0 6f 6e 20 65 6e 61 62 6c 65 73 0a 2a 2a 20 61 6c  on enables.** al
106d0 6c 20 6d 75 74 65 78 65 73 20 69 6e 63 6c 75 64  l mutexes includ
106e0 69 6e 67 20 74 68 65 20 72 65 63 75 72 73 69 76  ing the recursiv
106f0 65 0a 2a 2a 20 6d 75 74 65 78 65 73 20 6f 6e 20  e.** mutexes on 
10700 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
10710 74 69 6f 6e 5d 20 61 6e 64 20 5b 70 72 65 70 61  tion] and [prepa
10720 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 6f  red statement] o
10730 62 6a 65 63 74 73 2e 0a 2a 2a 20 49 6e 20 74 68  bjects..** In th
10740 69 73 20 6d 6f 64 65 20 28 77 68 69 63 68 20 69  is mode (which i
10750 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 77 68  s the default wh
10760 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d  en SQLite is com
10770 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20 5b 53  piled with.** [S
10780 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
10790 3d 31 5d 29 20 74 68 65 20 53 51 4c 69 74 65 20  =1]) the SQLite 
107a0 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 69 74 73  library will its
107b0 65 6c 66 20 73 65 72 69 61 6c 69 7a 65 20 61 63  elf serialize ac
107c0 63 65 73 73 0a 2a 2a 20 74 6f 20 5b 64 61 74 61  cess.** to [data
107d0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
107e0 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65 64 20  ] and [prepared 
107f0 73 74 61 74 65 6d 65 6e 74 73 5d 20 73 6f 20 74  statements] so t
10800 68 61 74 20 74 68 65 0a 2a 2a 20 61 70 70 6c 69  hat the.** appli
10810 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65 20 74  cation is free t
10820 6f 20 75 73 65 20 74 68 65 20 73 61 6d 65 20 5b  o use the same [
10830 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
10840 69 6f 6e 5d 20 6f 72 20 74 68 65 0a 2a 2a 20 73  ion] or the.** s
10850 61 6d 65 20 5b 70 72 65 70 61 72 65 64 20 73 74  ame [prepared st
10860 61 74 65 6d 65 6e 74 5d 20 69 6e 20 64 69 66 66  atement] in diff
10870 65 72 65 6e 74 20 74 68 72 65 61 64 73 20 61 74  erent threads at
10880 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e 0a   the same time..
10890 2a 2a 20 53 65 65 20 74 68 65 20 5b 74 68 72 65  ** See the [thre
108a0 61 64 69 6e 67 20 6d 6f 64 65 5d 20 64 6f 63 75  ading mode] docu
108b0 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64  mentation for ad
108c0 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
108d0 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  tion.</dd>.**.**
108e0 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46   <dt>SQLITE_CONF
108f0 49 47 5f 4d 41 4c 4c 4f 43 3c 2f 64 74 3e 0a 2a  IG_MALLOC</dt>.*
10900 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f  * <dd>This optio
10910 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65  n takes a single
10920 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20   argument which 
10930 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
10940 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f  an.** instance o
10950 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d  f the [sqlite3_m
10960 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75  em_methods] stru
10970 63 74 75 72 65 2e 20 20 54 68 65 20 61 72 67 75  cture.  The argu
10980 6d 65 6e 74 20 73 70 65 63 69 66 69 65 73 0a 2a  ment specifies.*
10990 2a 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6c 6f  * alternative lo
109a0 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61  w-level memory a
109b0 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e  llocation routin
109c0 65 73 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e  es to be used in
109d0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 74 68 65   place of.** the
109e0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
109f0 6f 6e 20 72 6f 75 74 69 6e 65 73 20 62 75 69 6c  on routines buil
10a00 74 20 69 6e 74 6f 20 53 51 4c 69 74 65 2e 3c 2f  t into SQLite.</
10a10 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
10a20 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d  LITE_CONFIG_GETM
10a30 41 4c 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  ALLOC</dt>.** <d
10a40 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61  d>This option ta
10a50 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67  kes a single arg
10a60 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61  ument which is a
10a70 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a   pointer to an.*
10a80 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  * instance of th
10a90 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d  e [sqlite3_mem_m
10aa0 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74 75 72  ethods] structur
10ab0 65 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65 33  e.  The [sqlite3
10ac0 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a  _mem_methods].**
10ad0 20 73 74 72 75 63 74 75 72 65 20 69 73 20 66 69   structure is fi
10ae0 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 75  lled with the cu
10af0 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64 20  rrently defined 
10b00 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
10b10 6e 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 54  n routines..** T
10b20 68 69 73 20 6f 70 74 69 6f 6e 20 63 61 6e 20 62  his option can b
10b30 65 20 75 73 65 64 20 74 6f 20 6f 76 65 72 6c 6f  e used to overlo
10b40 61 64 20 74 68 65 20 64 65 66 61 75 6c 74 20 6d  ad the default m
10b50 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
10b60 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77 69 74  .** routines wit
10b70 68 20 61 20 77 72 61 70 70 65 72 20 74 68 61 74  h a wrapper that
10b80 20 73 69 6d 75 6c 61 74 69 6f 6e 73 20 6d 65 6d   simulations mem
10b90 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  ory allocation f
10ba0 61 69 6c 75 72 65 20 6f 72 0a 2a 2a 20 74 72 61  ailure or.** tra
10bb0 63 6b 73 20 6d 65 6d 6f 72 79 20 75 73 61 67 65  cks memory usage
10bc0 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e 3c 2f  , for example.</
10bd0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
10be0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53  LITE_CONFIG_MEMS
10bf0 54 41 54 55 53 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  TATUS</dt>.** <d
10c00 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61  d>This option ta
10c10 6b 65 73 20 73 69 6e 67 6c 65 20 61 72 67 75 6d  kes single argum
10c20 65 6e 74 20 6f 66 20 74 79 70 65 20 69 6e 74 2c  ent of type int,
10c30 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20   interpreted as 
10c40 61 20 0a 2a 2a 20 62 6f 6f 6c 65 61 6e 2c 20 77  a .** boolean, w
10c50 68 69 63 68 20 65 6e 61 62 6c 65 73 20 6f 72 20  hich enables or 
10c60 64 69 73 61 62 6c 65 73 20 74 68 65 20 63 6f 6c  disables the col
10c70 6c 65 63 74 69 6f 6e 20 6f 66 20 6d 65 6d 6f 72  lection of memor
10c80 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 0a 2a 2a  y allocation .**
10c90 20 73 74 61 74 69 73 74 69 63 73 2e 20 57 68 65   statistics. Whe
10ca0 6e 20 64 69 73 61 62 6c 65 64 2c 20 74 68 65 20  n disabled, the 
10cb0 66 6f 6c 6c 6f 77 69 6e 67 20 53 51 4c 69 74 65  following SQLite
10cc0 20 69 6e 74 65 72 66 61 63 65 73 20 62 65 63 6f   interfaces beco
10cd0 6d 65 20 0a 2a 2a 20 6e 6f 6e 2d 6f 70 65 72 61  me .** non-opera
10ce0 74 69 6f 6e 61 6c 3a 0a 2a 2a 20 20 20 3c 75 6c  tional:.**   <ul
10cf0 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c  >.**   <li> [sql
10d00 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64  ite3_memory_used
10d10 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73  ()].**   <li> [s
10d20 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69  qlite3_memory_hi
10d30 67 68 77 61 74 65 72 28 29 5d 0a 2a 2a 20 20 20  ghwater()].**   
10d40 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f 73 6f  <li> [sqlite3_so
10d50 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 5d  ft_heap_limit()]
10d60 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71 6c 69  .**   <li> [sqli
10d70 74 65 33 5f 73 74 61 74 75 73 28 29 5d 0a 2a 2a  te3_status()].**
10d80 20 20 20 3c 2f 75 6c 3e 0a 2a 2a 20 3c 2f 64 64     </ul>.** </dd
10d90 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
10da0 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43  TE_CONFIG_SCRATC
10db0 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  H</dt>.** <dd>Th
10dc0 69 73 20 6f 70 74 69 6f 6e 20 73 70 65 63 69 66  is option specif
10dd0 69 65 73 20 61 20 73 74 61 74 69 63 20 6d 65 6d  ies a static mem
10de0 6f 72 79 20 62 75 66 66 65 72 20 74 68 61 74 20  ory buffer that 
10df0 53 51 4c 69 74 65 20 63 61 6e 20 75 73 65 20 66  SQLite can use f
10e00 6f 72 0a 2a 2a 20 73 63 72 61 74 63 68 20 6d 65  or.** scratch me
10e10 6d 6f 72 79 2e 20 20 54 68 65 72 65 20 61 72 65  mory.  There are
10e20 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73   three arguments
10e30 3a 20 20 41 20 70 6f 69 6e 74 65 72 20 61 6e 20  :  A pointer an 
10e40 38 2d 62 79 74 65 0a 2a 2a 20 61 6c 69 67 6e 65  8-byte.** aligne
10e50 64 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20  d memory buffer 
10e60 66 72 6f 6d 20 77 68 69 63 68 20 74 68 65 20 73  from which the s
10e70 63 72 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e  crach allocation
10e80 73 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 72 61  s will be.** dra
10e90 77 6e 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20  wn, the size of 
10ea0 65 61 63 68 20 73 63 72 61 74 63 68 20 61 6c 6c  each scratch all
10eb0 6f 63 61 74 69 6f 6e 20 28 73 7a 29 2c 0a 2a 2a  ocation (sz),.**
10ec0 20 61 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d   and the maximum
10ed0 20 6e 75 6d 62 65 72 20 6f 66 20 73 63 72 61 74   number of scrat
10ee0 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 28  ch allocations (
10ef0 4e 29 2e 20 20 54 68 65 20 73 7a 0a 2a 2a 20 61  N).  The sz.** a
10f00 72 67 75 6d 65 6e 74 20 6d 75 73 74 20 62 65 20  rgument must be 
10f10 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 31 36  a multiple of 16
10f20 2e 20 54 68 65 20 73 7a 20 70 61 72 61 6d 65 74  . The sz paramet
10f30 65 72 20 73 68 6f 75 6c 64 20 62 65 20 61 20 66  er should be a f
10f40 65 77 20 62 79 74 65 73 0a 2a 2a 20 6c 61 72 67  ew bytes.** larg
10f50 65 72 20 74 68 61 6e 20 74 68 65 20 61 63 74 75  er than the actu
10f60 61 6c 20 73 63 72 61 74 63 68 20 73 70 61 63 65  al scratch space
10f70 20 72 65 71 75 69 72 65 64 20 64 75 65 20 74 6f   required due to
10f80 20 69 6e 74 65 72 6e 61 6c 20 6f 76 65 72 68 65   internal overhe
10f90 61 64 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  ad..** The first
10fa0 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75 6c 64   argument should
10fb0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 38   pointer to an 8
10fc0 2d 62 79 74 65 20 61 6c 69 67 6e 65 64 20 62 75  -byte aligned bu
10fd0 66 66 65 72 0a 2a 2a 20 6f 66 20 61 74 20 6c 65  ffer.** of at le
10fe0 61 73 74 20 73 7a 2a 4e 20 62 79 74 65 73 20 6f  ast sz*N bytes o
10ff0 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 53 51 4c  f memory..** SQL
11000 69 74 65 20 77 69 6c 6c 20 75 73 65 20 6e 6f 20  ite will use no 
11010 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 73 63  more than one sc
11020 72 61 74 63 68 20 62 75 66 66 65 72 20 61 74 20  ratch buffer at 
11030 6f 6e 63 65 20 70 65 72 20 74 68 72 65 61 64 2c  once per thread,
11040 20 73 6f 0a 2a 2a 20 4e 20 73 68 6f 75 6c 64 20   so.** N should 
11050 62 65 20 73 65 74 20 74 6f 20 74 68 65 20 65 78  be set to the ex
11060 70 65 63 74 65 64 20 6d 61 78 69 6d 75 6d 20 6e  pected maximum n
11070 75 6d 62 65 72 20 6f 66 20 74 68 72 65 61 64 73  umber of threads
11080 2e 20 20 54 68 65 20 73 7a 0a 2a 2a 20 70 61 72  .  The sz.** par
11090 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65  ameter should be
110a0 20 36 20 74 69 6d 65 73 20 74 68 65 20 73 69 7a   6 times the siz
110b0 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65 73 74  e of the largest
110c0 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 73   database page s
110d0 69 7a 65 2e 0a 2a 2a 20 53 63 72 61 74 63 68 20  ize..** Scratch 
110e0 62 75 66 66 65 72 73 20 61 72 65 20 75 73 65 64  buffers are used
110f0 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   as part of the 
11100 62 74 72 65 65 20 62 61 6c 61 6e 63 65 20 6f 70  btree balance op
11110 65 72 61 74 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20  eration.  If.** 
11120 54 68 65 20 62 74 72 65 65 20 62 61 6c 61 6e 63  The btree balanc
11130 65 72 20 6e 65 65 64 73 20 61 64 64 69 74 69 6f  er needs additio
11140 6e 61 6c 20 6d 65 6d 6f 72 79 20 62 65 79 6f 6e  nal memory beyon
11150 64 20 77 68 61 74 20 69 73 20 70 72 6f 76 69 64  d what is provid
11160 65 64 20 62 79 0a 2a 2a 20 73 63 72 61 74 63 68  ed by.** scratch
11170 20 62 75 66 66 65 72 73 20 6f 72 20 69 66 20 6e   buffers or if n
11180 6f 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72  o scratch buffer
11190 20 73 70 61 63 65 20 69 73 20 73 70 65 63 69 66   space is specif
111a0 69 65 64 2c 20 74 68 65 6e 20 53 51 4c 69 74 65  ied, then SQLite
111b0 0a 2a 2a 20 67 6f 65 73 20 74 6f 20 5b 73 71 6c  .** goes to [sql
111c0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 74  ite3_malloc()] t
111d0 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d  o obtain the mem
111e0 6f 72 79 20 69 74 20 6e 65 65 64 73 2e 3c 2f 64  ory it needs.</d
111f0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
11200 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43  ITE_CONFIG_PAGEC
11210 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  ACHE</dt>.** <dd
11220 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 73 70 65  >This option spe
11230 63 69 66 69 65 73 20 61 20 73 74 61 74 69 63 20  cifies a static 
11240 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 68  memory buffer th
11250 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20 75 73  at SQLite can us
11260 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 64 61 74  e for.** the dat
11270 61 62 61 73 65 20 70 61 67 65 20 63 61 63 68 65  abase page cache
11280 20 77 69 74 68 20 74 68 65 20 64 65 66 61 75 6c   with the defaul
11290 74 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70  t page cache imp
112a0 6c 65 6d 65 6e 61 74 69 6f 6e 2e 20 20 0a 2a 2a  lemenation.  .**
112b0 20 54 68 69 73 20 63 6f 6e 66 69 67 75 72 61 74   This configurat
112c0 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  ion should not b
112d0 65 20 75 73 65 64 20 69 66 20 61 6e 20 61 70 70  e used if an app
112e0 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 20  lication-define 
112f0 70 61 67 65 0a 2a 2a 20 63 61 63 68 65 20 69 6d  page.** cache im
11300 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20  plementation is 
11310 6c 6f 61 64 65 64 20 75 73 69 6e 67 20 74 68 65  loaded using the
11320 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50   SQLITE_CONFIG_P
11330 43 41 43 48 45 20 6f 70 74 69 6f 6e 2e 0a 2a 2a  CACHE option..**
11340 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65   There are three
11350 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68   arguments to th
11360 69 73 20 6f 70 74 69 6f 6e 3a 20 41 20 70 6f 69  is option: A poi
11370 6e 74 65 72 20 74 6f 20 38 2d 62 79 74 65 20 61  nter to 8-byte a
11380 6c 69 67 6e 65 64 0a 2a 2a 20 6d 65 6d 6f 72 79  ligned.** memory
11390 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 65 61  , the size of ea
113a0 63 68 20 70 61 67 65 20 62 75 66 66 65 72 20 28  ch page buffer (
113b0 73 7a 29 2c 20 61 6e 64 20 74 68 65 20 6e 75 6d  sz), and the num
113c0 62 65 72 20 6f 66 20 70 61 67 65 73 20 28 4e 29  ber of pages (N)
113d0 2e 0a 2a 2a 20 54 68 65 20 73 7a 20 61 72 67 75  ..** The sz argu
113e0 6d 65 6e 74 20 73 68 6f 75 6c 64 20 62 65 20 74  ment should be t
113f0 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c  he size of the l
11400 61 72 67 65 73 74 20 64 61 74 61 62 61 73 65 20  argest database 
11410 70 61 67 65 0a 2a 2a 20 28 61 20 70 6f 77 65 72  page.** (a power
11420 20 6f 66 20 74 77 6f 20 62 65 74 77 65 65 6e 20   of two between 
11430 35 31 32 20 61 6e 64 20 33 32 37 36 38 29 20 70  512 and 32768) p
11440 6c 75 73 20 61 20 6c 69 74 74 6c 65 20 65 78 74  lus a little ext
11450 72 61 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 70  ra for each.** p
11460 61 67 65 20 68 65 61 64 65 72 2e 20 20 54 68 65  age header.  The
11470 20 70 61 67 65 20 68 65 61 64 65 72 20 73 69 7a   page header siz
11480 65 20 69 73 20 32 30 20 74 6f 20 34 30 20 62 79  e is 20 to 40 by
11490 74 65 73 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  tes depending on
114a0 0a 2a 2a 20 74 68 65 20 68 6f 73 74 20 61 72 63  .** the host arc
114b0 68 69 74 65 63 74 75 72 65 2e 20 20 49 74 20 69  hitecture.  It i
114c0 73 20 68 61 72 6d 6c 65 73 73 2c 20 61 70 61 72  s harmless, apar
114d0 74 20 66 72 6f 6d 20 74 68 65 20 77 61 73 74 65  t from the waste
114e0 64 20 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 74 6f 20  d memory,.** to 
114f0 6d 61 6b 65 20 73 7a 20 61 20 6c 69 74 74 6c 65  make sz a little
11500 20 74 6f 6f 20 6c 61 72 67 65 2e 20 20 54 68 65   too large.  The
11510 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65   first.** argume
11520 6e 74 20 73 68 6f 75 6c 64 20 70 6f 69 6e 74 20  nt should point 
11530 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  to an allocation
11540 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73 7a 2a   of at least sz*
11550 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72  N bytes of memor
11560 79 2e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c  y..** SQLite wil
11570 6c 20 75 73 65 20 74 68 65 20 6d 65 6d 6f 72 79  l use the memory
11580 20 70 72 6f 76 69 64 65 64 20 62 79 20 74 68 65   provided by the
11590 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
115a0 74 6f 20 73 61 74 69 73 66 79 20 69 74 73 0a 2a  to satisfy its.*
115b0 2a 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73 20 66  * memory needs f
115c0 6f 72 20 74 68 65 20 66 69 72 73 74 20 4e 20 70  or the first N p
115d0 61 67 65 73 20 74 68 61 74 20 69 74 20 61 64 64  ages that it add
115e0 73 20 74 6f 20 63 61 63 68 65 2e 20 20 49 66 20  s to cache.  If 
115f0 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 70 61  additional.** pa
11600 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20  ge cache memory 
11610 69 73 20 6e 65 65 64 65 64 20 62 65 79 6f 6e 64  is needed beyond
11620 20 77 68 61 74 20 69 73 20 70 72 6f 76 69 64 65   what is provide
11630 64 20 62 79 20 74 68 69 73 20 6f 70 74 69 6f 6e  d by this option
11640 2c 20 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65  , then.** SQLite
11650 20 67 6f 65 73 20 74 6f 20 5b 73 71 6c 69 74 65   goes to [sqlite
11660 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 66 6f 72 20  3_malloc()] for 
11670 74 68 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73  the additional s
11680 74 6f 72 61 67 65 20 73 70 61 63 65 2e 0a 2a 2a  torage space..**
11690 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   The implementat
116a0 69 6f 6e 20 6d 69 67 68 74 20 75 73 65 20 6f 6e  ion might use on
116b0 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65  e or more of the
116c0 20 4e 20 62 75 66 66 65 72 73 20 74 6f 20 68 6f   N buffers to ho
116d0 6c 64 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 63  ld .** memory ac
116e0 63 6f 75 6e 74 69 6e 67 20 69 6e 66 6f 72 6d 61  counting informa
116f0 74 69 6f 6e 2e 20 54 68 65 20 70 6f 69 6e 74 65  tion. The pointe
11700 72 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61  r in the first a
11710 72 67 75 6d 65 6e 74 20 6d 75 73 74 0a 2a 2a 20  rgument must.** 
11720 62 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 61 6e  be aligned to an
11730 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79   8-byte boundary
11740 20 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20 62   or subsequent b
11750 65 68 61 76 69 6f 72 20 6f 66 20 53 51 4c 69 74  ehavior of SQLit
11760 65 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 75 6e 64  e.** will be und
11770 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a  efined.</dd>.**.
11780 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f  ** <dt>SQLITE_CO
11790 4e 46 49 47 5f 48 45 41 50 3c 2f 64 74 3e 0a 2a  NFIG_HEAP</dt>.*
117a0 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f  * <dd>This optio
117b0 6e 20 73 70 65 63 69 66 69 65 73 20 61 20 73 74  n specifies a st
117c0 61 74 69 63 20 6d 65 6d 6f 72 79 20 62 75 66 66  atic memory buff
117d0 65 72 20 74 68 61 74 20 53 51 4c 69 74 65 20 77  er that SQLite w
117e0 69 6c 6c 20 75 73 65 0a 2a 2a 20 66 6f 72 20 61  ill use.** for a
117f0 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69  ll of its dynami
11800 63 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  c memory allocat
11810 69 6f 6e 20 6e 65 65 64 73 20 62 65 79 6f 6e 64  ion needs beyond
11820 20 74 68 6f 73 65 20 70 72 6f 76 69 64 65 64 0a   those provided.
11830 2a 2a 20 66 6f 72 20 62 79 20 5b 53 51 4c 49 54  ** for by [SQLIT
11840 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48  E_CONFIG_SCRATCH
11850 5d 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 43 4f  ] and [SQLITE_CO
11860 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2e  NFIG_PAGECACHE].
11870 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 68  .** There are th
11880 72 65 65 20 61 72 67 75 6d 65 6e 74 73 3a 20 41  ree arguments: A
11890 6e 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 65 64  n 8-byte aligned
118a0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
118b0 6d 65 6d 6f 72 79 2c 0a 2a 2a 20 74 68 65 20 6e  memory,.** the n
118c0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
118d0 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 62 75 66  n the memory buf
118e0 66 65 72 2c 20 61 6e 64 20 74 68 65 20 6d 69 6e  fer, and the min
118f0 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  imum allocation 
11900 73 69 7a 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20  size..** If the 
11910 66 69 72 73 74 20 70 6f 69 6e 74 65 72 20 28 74  first pointer (t
11920 68 65 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65  he memory pointe
11930 72 29 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e  r) is NULL, then
11940 20 53 51 4c 69 74 65 20 72 65 76 65 72 74 73 0a   SQLite reverts.
11950 2a 2a 20 74 6f 20 75 73 69 6e 67 20 69 74 73 20  ** to using its 
11960 64 65 66 61 75 6c 74 20 6d 65 6d 6f 72 79 20 61  default memory a
11970 6c 6c 6f 63 61 74 6f 72 20 28 74 68 65 20 73 79  llocator (the sy
11980 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 20 69 6d  stem malloc() im
11990 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 29 2c 0a 2a  plementation),.*
119a0 2a 20 75 6e 64 6f 69 6e 67 20 61 6e 79 20 70 72  * undoing any pr
119b0 69 6f 72 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  ior invocation o
119c0 66 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  f [SQLITE_CONFIG
119d0 5f 4d 41 4c 4c 4f 43 5d 2e 20 20 49 66 20 74 68  _MALLOC].  If th
119e0 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 70 6f 69 6e  e.** memory poin
119f0 74 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20  ter is not NULL 
11a00 61 6e 64 20 65 69 74 68 65 72 20 5b 53 51 4c 49  and either [SQLI
11a10 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
11a20 33 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45  3] or.** [SQLITE
11a30 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 5d  _ENABLE_MEMSYS5]
11a40 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68   are defined, th
11a50 65 6e 20 74 68 65 20 61 6c 74 65 72 6e 61 74 69  en the alternati
11a60 76 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c  ve memory.** all
11a70 6f 63 61 74 6f 72 20 69 73 20 65 6e 67 61 67 65  ocator is engage
11a80 64 20 74 6f 20 68 61 6e 64 6c 65 20 61 6c 6c 20  d to handle all 
11a90 6f 66 20 53 51 4c 69 74 65 73 20 6d 65 6d 6f 72  of SQLites memor
11aa0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65  y allocation nee
11ab0 64 73 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  ds..** The first
11ac0 20 70 6f 69 6e 74 65 72 20 28 74 68 65 20 6d 65   pointer (the me
11ad0 6d 6f 72 79 20 70 6f 69 6e 74 65 72 29 20 6d 75  mory pointer) mu
11ae0 73 74 20 62 65 20 61 6c 69 67 6e 65 64 20 74 6f  st be aligned to
11af0 20 61 6e 20 38 2d 62 79 74 65 0a 2a 2a 20 62 6f   an 8-byte.** bo
11b00 75 6e 64 61 72 79 20 6f 72 20 73 75 62 73 65 71  undary or subseq
11b10 75 65 6e 74 20 62 65 68 61 76 69 6f 72 20 6f 66  uent behavior of
11b20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20   SQLite will be 
11b30 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a  undefined.</dd>.
11b40 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
11b50 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 3c 2f 64  _CONFIG_MUTEX</d
11b60 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f  t>.** <dd>This o
11b70 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69  ption takes a si
11b80 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68  ngle argument wh
11b90 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ich is a pointer
11ba0 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e   to an.** instan
11bb0 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74  ce of the [sqlit
11bc0 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
11bd0 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  ] structure.  Th
11be0 65 20 61 72 67 75 6d 65 6e 74 20 73 70 65 63 69  e argument speci
11bf0 66 69 65 73 0a 2a 2a 20 61 6c 74 65 72 6e 61 74  fies.** alternat
11c00 69 76 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 75  ive low-level mu
11c10 74 65 78 20 72 6f 75 74 69 6e 65 73 20 74 6f 20  tex routines to 
11c20 62 65 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65  be used in place
11c30 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20 72 6f  .** the mutex ro
11c40 75 74 69 6e 65 73 20 62 75 69 6c 74 20 69 6e 74  utines built int
11c50 6f 20 53 51 4c 69 74 65 2e 3c 2f 64 64 3e 0a 2a  o SQLite.</dd>.*
11c60 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
11c70 43 4f 4e 46 49 47 5f 47 45 54 4d 55 54 45 58 3c  CONFIG_GETMUTEX<
11c80 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73  /dt>.** <dd>This
11c90 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20   option takes a 
11ca0 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20  single argument 
11cb0 77 68 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74  which is a point
11cc0 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74  er to an.** inst
11cd0 61 6e 63 65 20 6f 66 20 74 68 65 20 5b 73 71 6c  ance of the [sql
11ce0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
11cf0 64 73 5d 20 73 74 72 75 63 74 75 72 65 2e 20 20  ds] structure.  
11d00 54 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  The.** [sqlite3_
11d10 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 0a 2a  mutex_methods].*
11d20 2a 20 73 74 72 75 63 74 75 72 65 20 69 73 20 66  * structure is f
11d30 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63  illed with the c
11d40 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65 64  urrently defined
11d50 20 6d 75 74 65 78 20 72 6f 75 74 69 6e 65 73 2e   mutex routines.
11d60 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6f 6e 20  .** This option 
11d70 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 6f  can be used to o
11d80 76 65 72 6c 6f 61 64 20 74 68 65 20 64 65 66 61  verload the defa
11d90 75 6c 74 20 6d 75 74 65 78 20 61 6c 6c 6f 63 61  ult mutex alloca
11da0 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  tion.** routines
11db0 20 77 69 74 68 20 61 20 77 72 61 70 70 65 72 20   with a wrapper 
11dc0 75 73 65 64 20 74 6f 20 74 72 61 63 6b 20 6d 75  used to track mu
11dd0 74 65 78 20 75 73 61 67 65 20 66 6f 72 20 70 65  tex usage for pe
11de0 72 66 6f 72 6d 61 6e 63 65 0a 2a 2a 20 70 72 6f  rformance.** pro
11df0 66 69 6c 69 6e 67 20 6f 72 20 74 65 73 74 69 6e  filing or testin
11e00 67 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e 3c  g, for example.<
11e10 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
11e20 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f  QLITE_CONFIG_LOO
11e30 4b 41 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c  KASIDE</dt>.** <
11e40 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74  dd>This option t
11e50 61 6b 65 73 20 74 77 6f 20 61 72 67 75 6d 65 6e  akes two argumen
11e60 74 73 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e  ts that determin
11e70 65 20 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a  e the default.**
11e80 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
11e90 6f 6e 20 6c 6f 6f 6b 61 73 69 64 65 20 6f 70 74  on lookaside opt
11ea0 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 54 68 65 20  imization.  The 
11eb0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69  first argument i
11ec0 73 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66  s the.** size of
11ed0 20 65 61 63 68 20 6c 6f 6f 6b 61 73 69 64 65 20   each lookaside 
11ee0 62 75 66 66 65 72 20 73 6c 6f 74 20 61 6e 64 20  buffer slot and 
11ef0 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 74 68  the second is th
11f00 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73  e number of.** s
11f10 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 74  lots allocated t
11f20 6f 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20  o each database 
11f30 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 69  connection.  Thi
11f40 73 20 6f 70 74 69 6f 6e 20 73 65 74 73 20 74 68  s option sets th
11f50 65 0a 2a 2a 20 3c 69 3e 64 65 66 61 75 6c 74 3c  e.** <i>default<
11f60 2f 69 3e 20 6c 6f 6f 6b 61 73 69 64 65 20 73 69  /i> lookaside si
11f70 7a 65 2e 20 20 54 68 65 20 5b 53 51 4c 49 54 45  ze.  The [SQLITE
11f80 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53  _DBCONFIG_LOOKAS
11f90 49 44 45 5d 0a 2a 2a 20 76 65 72 62 20 74 6f 20  IDE].** verb to 
11fa0 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66  [sqlite3_db_conf
11fb0 69 67 28 29 5d 20 63 61 6e 20 62 65 20 75 73 65  ig()] can be use
11fc0 64 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20  d to change the 
11fd0 6c 6f 6f 6b 61 73 69 64 65 0a 2a 2a 20 63 6f 6e  lookaside.** con
11fe0 66 69 67 75 72 61 74 69 6f 6e 20 6f 6e 20 69 6e  figuration on in
11ff0 64 69 76 69 64 75 61 6c 20 63 6f 6e 6e 65 63 74  dividual connect
12000 69 6f 6e 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  ions.</dd>.**.**
12010 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46   <dt>SQLITE_CONF
12020 49 47 5f 50 43 41 43 48 45 3c 2f 64 74 3e 0a 2a  IG_PCACHE</dt>.*
12030 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f  * <dd>This optio
12040 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65  n takes a single
12050 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20   argument which 
12060 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a  is a pointer to.
12070 2a 2a 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 70  ** an [sqlite3_p
12080 63 61 63 68 65 5f 6d 65 74 68 6f 64 73 5d 20 6f  cache_methods] o
12090 62 6a 65 63 74 2e 20 20 54 68 69 73 20 6f 62 6a  bject.  This obj
120a0 65 63 74 20 73 70 65 63 69 66 69 65 73 20 74 68  ect specifies th
120b0 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74  e interface.** t
120c0 6f 20 61 20 63 75 73 74 6f 6d 20 70 61 67 65 20  o a custom page 
120d0 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  cache implementa
120e0 74 69 6f 6e 2e 20 20 53 51 4c 69 74 65 20 6d 61  tion.  SQLite ma
120f0 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68  kes a copy of th
12100 65 0a 2a 2a 20 6f 62 6a 65 63 74 20 61 6e 64 20  e.** object and 
12110 75 73 65 73 20 69 74 20 66 6f 72 20 70 61 67 65  uses it for page
12120 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c   cache memory al
12130 6c 6f 63 61 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0a  locations.</dd>.
12140 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
12150 5f 43 4f 4e 46 49 47 5f 47 45 54 50 43 41 43 48  _CONFIG_GETPCACH
12160 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  E</dt>.** <dd>Th
12170 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20  is option takes 
12180 61 20 73 69 6e 67 6c 65 20 61 72 67 75 6d 65 6e  a single argumen
12190 74 20 77 68 69 63 68 20 69 73 20 61 20 70 6f 69  t which is a poi
121a0 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73  nter to an.** [s
121b0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
121c0 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 2e 20 20  thods] object.  
121d0 53 51 4c 69 74 65 20 63 6f 70 69 65 73 20 6f 66  SQLite copies of
121e0 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20   the current.** 
121f0 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c 65  page cache imple
12200 6d 65 6e 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74  mentation into t
12210 68 61 74 20 6f 62 6a 65 63 74 2e 3c 2f 64 64 3e  hat object.</dd>
12220 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a  .**.** </dl>.*/.
12230 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
12240 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45  ONFIG_SINGLETHRE
12250 41 44 20 20 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f  AD  1  /* nil */
12260 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
12270 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52 45  CONFIG_MULTITHRE
12280 41 44 20 20 20 32 20 20 2f 2a 20 6e 69 6c 20 2a  AD   2  /* nil *
12290 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
122a0 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c 49 5a  _CONFIG_SERIALIZ
122b0 45 44 20 20 20 20 33 20 20 2f 2a 20 6e 69 6c 20  ED    3  /* nil 
122c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
122d0 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20  E_CONFIG_MALLOC 
122e0 20 20 20 20 20 20 20 34 20 20 2f 2a 20 73 71 6c         4  /* sql
122f0 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
12300 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  * */.#define SQL
12310 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41  ITE_CONFIG_GETMA
12320 4c 4c 4f 43 20 20 20 20 20 35 20 20 2f 2a 20 73  LLOC     5  /* s
12330 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
12340 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ds* */.#define S
12350 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52  QLITE_CONFIG_SCR
12360 41 54 43 48 20 20 20 20 20 20 20 36 20 20 2f 2a  ATCH       6  /*
12370 20 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20   void*, int sz, 
12380 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65  int N */.#define
12390 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50   SQLITE_CONFIG_P
123a0 41 47 45 43 41 43 48 45 20 20 20 20 20 37 20 20  AGECACHE     7  
123b0 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20 73 7a  /* void*, int sz
123c0 2c 20 69 6e 74 20 4e 20 2a 2f 0a 23 64 65 66 69  , int N */.#defi
123d0 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ne SQLITE_CONFIG
123e0 5f 48 45 41 50 20 20 20 20 20 20 20 20 20 20 38  _HEAP          8
123f0 20 20 2f 2a 20 76 6f 69 64 2a 2c 20 69 6e 74 20    /* void*, int 
12400 6e 42 79 74 65 2c 20 69 6e 74 20 6d 69 6e 20 2a  nByte, int min *
12410 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
12420 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55  _CONFIG_MEMSTATU
12430 53 20 20 20 20 20 39 20 20 2f 2a 20 62 6f 6f 6c  S     9  /* bool
12440 65 61 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ean */.#define S
12450 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54  QLITE_CONFIG_MUT
12460 45 58 20 20 20 20 20 20 20 20 31 30 20 20 2f 2a  EX        10  /*
12470 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
12480 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69  ethods* */.#defi
12490 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ne SQLITE_CONFIG
124a0 5f 47 45 54 4d 55 54 45 58 20 20 20 20 20 31 31  _GETMUTEX     11
124b0 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74    /* sqlite3_mut
124c0 65 78 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 2f  ex_methods* */./
124d0 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 53 51 4c  * previously SQL
124e0 49 54 45 5f 43 4f 4e 46 49 47 5f 43 48 55 4e 4b  ITE_CONFIG_CHUNK
124f0 41 4c 4c 4f 43 20 31 32 20 77 68 69 63 68 20 69  ALLOC 12 which i
12500 73 20 6e 6f 77 20 75 6e 75 73 65 64 2e 20 2a 2f  s now unused. */
12510 20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45   .#define SQLITE
12520 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44  _CONFIG_LOOKASID
12530 45 20 20 20 20 31 33 20 20 2f 2a 20 69 6e 74 20  E    13  /* int 
12540 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  int */.#define S
12550 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41  QLITE_CONFIG_PCA
12560 43 48 45 20 20 20 20 20 20 20 31 34 20 20 2f 2a  CHE       14  /*
12570 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f   sqlite3_pcache_
12580 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 23 64 65 66  methods* */.#def
12590 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ine SQLITE_CONFI
125a0 47 5f 47 45 54 50 43 41 43 48 45 20 20 20 20 31  G_GETPCACHE    1
125b0 35 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 70 63  5  /* sqlite3_pc
125c0 61 63 68 65 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f  ache_methods* */
125d0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
125e0 3a 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  : Configuration 
125f0 4f 70 74 69 6f 6e 73 20 7b 48 31 30 31 37 30 7d  Options {H10170}
12600 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 20 45 58 50   <S20000>.** EXP
12610 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20  ERIMENTAL.**.** 
12620 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20  These constants 
12630 61 72 65 20 74 68 65 20 61 76 61 69 6c 61 62 6c  are the availabl
12640 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 66 69 67  e integer config
12650 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20  uration options 
12660 74 68 61 74 0a 2a 2a 20 63 61 6e 20 62 65 20 70  that.** can be p
12670 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63  assed as the sec
12680 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ond argument to 
12690 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f  the [sqlite3_db_
126a0 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72 66  config()] interf
126b0 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63  ace..**.** New c
126c0 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74  onfiguration opt
126d0 69 6f 6e 73 20 6d 61 79 20 62 65 20 61 64 64 65  ions may be adde
126e0 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65  d in future rele
126f0 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a  ases of SQLite..
12700 2a 2a 20 45 78 69 73 74 69 6e 67 20 63 6f 6e 66  ** Existing conf
12710 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e  iguration option
12720 73 20 6d 69 67 68 74 20 62 65 20 64 69 73 63 6f  s might be disco
12730 6e 74 69 6e 75 65 64 2e 20 20 41 70 70 6c 69 63  ntinued.  Applic
12740 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64  ations.** should
12750 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 75 72   check the retur
12760 6e 20 63 6f 64 65 20 66 72 6f 6d 20 5b 73 71 6c  n code from [sql
12770 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29  ite3_db_config()
12780 5d 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  ] to make sure t
12790 68 61 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20  hat.** the call 
127a0 77 6f 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71  worked.  The [sq
127b0 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28  lite3_db_config(
127c0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c  )] interface wil
127d0 6c 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f  l return a.** no
127e0 6e 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f  n-zero [error co
127f0 64 65 5d 20 69 66 20 61 20 64 69 73 63 6f 6e 74  de] if a discont
12800 69 6e 75 65 64 20 6f 72 20 75 6e 73 75 70 70 6f  inued or unsuppo
12810 72 74 65 64 20 63 6f 6e 66 69 67 75 72 61 74 69  rted configurati
12820 6f 6e 20 6f 70 74 69 6f 6e 0a 2a 2a 20 69 73 20  on option.** is 
12830 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c  invoked..**.** <
12840 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54  dl>.** <dt>SQLIT
12850 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41  E_DBCONFIG_LOOKA
12860 53 49 44 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  SIDE</dt>.** <dd
12870 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b  >This option tak
12880 65 73 20 74 68 72 65 65 20 61 64 64 69 74 69 6f  es three additio
12890 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 20 74 68  nal arguments th
128a0 61 74 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65  at determine the
128b0 20 0a 2a 2a 20 5b 6c 6f 6f 6b 61 73 69 64 65 20   .** [lookaside 
128c0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
128d0 5d 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  ] configuration 
128e0 66 6f 72 20 74 68 65 20 5b 64 61 74 61 62 61 73  for the [databas
128f0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a  e connection]..*
12900 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75  * The first argu
12910 6d 65 6e 74 20 28 74 68 65 20 74 68 69 72 64 20  ment (the third 
12920 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71  parameter to [sq
12930 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28  lite3_db_config(
12940 29 5d 20 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74  )] is a.** point
12950 65 72 20 74 6f 20 61 6e 20 6d 65 6d 6f 72 79 20  er to an memory 
12960 62 75 66 66 65 72 20 74 6f 20 75 73 65 20 66 6f  buffer to use fo
12970 72 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f  r lookaside memo
12980 72 79 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  ry..** The first
12990 20 61 72 67 75 6d 65 6e 74 20 6d 61 79 20 62 65   argument may be
129a0 20 4e 55 4c 4c 20 69 6e 20 77 68 69 63 68 20 63   NULL in which c
129b0 61 73 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  ase SQLite will 
129c0 61 6c 6c 6f 63 61 74 65 20 74 68 65 0a 2a 2a 20  allocate the.** 
129d0 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72  lookaside buffer
129e0 20 69 74 73 65 6c 66 20 75 73 69 6e 67 20 5b 73   itself using [s
129f0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
12a00 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72  .  The second ar
12a10 67 75 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a  gument is the.**
12a20 20 73 69 7a 65 20 6f 66 20 65 61 63 68 20 6c 6f   size of each lo
12a30 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 73  okaside buffer s
12a40 6c 6f 74 20 61 6e 64 20 74 68 65 20 74 68 69 72  lot and the thir
12a50 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  d argument is th
12a60 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 73  e number of.** s
12a70 6c 6f 74 73 2e 20 20 54 68 65 20 73 69 7a 65 20  lots.  The size 
12a80 6f 66 20 74 68 65 20 62 75 66 66 65 72 20 69 6e  of the buffer in
12a90 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
12aa0 65 6e 74 20 6d 75 73 74 20 62 65 20 67 72 65 61  ent must be grea
12ab0 74 65 72 20 74 68 61 6e 0a 2a 2a 20 6f 72 20 65  ter than.** or e
12ac0 71 75 61 6c 20 74 6f 20 74 68 65 20 70 72 6f 64  qual to the prod
12ad0 75 63 74 20 6f 66 20 74 68 65 20 73 65 63 6f 6e  uct of the secon
12ae0 64 20 61 6e 64 20 74 68 69 72 64 20 61 72 67 75  d and third argu
12af0 6d 65 6e 74 73 2e 20 20 54 68 65 20 62 75 66 66  ments.  The buff
12b00 65 72 0a 2a 2a 20 6d 75 73 74 20 62 65 20 61 6c  er.** must be al
12b10 69 67 6e 65 64 20 74 6f 20 61 6e 20 38 2d 62 79  igned to an 8-by
12b20 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 20 49 66  te boundary.  If
12b30 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
12b40 6d 65 6e 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 61  ment is not.** a
12b50 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 38 2c 20   multiple of 8, 
12b60 69 74 20 69 73 20 69 6e 74 65 72 6e 61 6c 6c 79  it is internally
12b70 20 72 6f 75 6e 64 65 64 20 64 6f 77 6e 20 74 6f   rounded down to
12b80 20 74 68 65 20 6e 65 78 74 20 73 6d 61 6c 6c 65   the next smalle
12b90 72 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20 6f 66  r.** multiple of
12ba0 20 38 2e 20 20 53 65 65 20 61 6c 73 6f 3a 20 5b   8.  See also: [
12bb0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f  SQLITE_CONFIG_LO
12bc0 4f 4b 41 53 49 44 45 5d 3c 2f 64 64 3e 0a 2a 2a  OKASIDE]</dd>.**
12bd0 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65  .** </dl>.*/.#de
12be0 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 42 43 4f  fine SQLITE_DBCO
12bf0 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 20  NFIG_LOOKASIDE  
12c00 20 20 31 30 30 31 20 20 2f 2a 20 76 6f 69 64 2a    1001  /* void*
12c10 20 69 6e 74 20 69 6e 74 20 2a 2f 0a 0a 0a 2f 2a   int int */.../*
12c20 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e  .** CAPI3REF: En
12c30 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20  able Or Disable 
12c40 45 78 74 65 6e 64 65 64 20 52 65 73 75 6c 74 20  Extended Result 
12c50 43 6f 64 65 73 20 7b 48 31 32 32 30 30 7d 20 3c  Codes {H12200} <
12c60 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S10700>.**.** Th
12c70 65 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64  e sqlite3_extend
12c80 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28  ed_result_codes(
12c90 29 20 72 6f 75 74 69 6e 65 20 65 6e 61 62 6c 65  ) routine enable
12ca0 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20 74 68  s or disables th
12cb0 65 0a 2a 2a 20 5b 65 78 74 65 6e 64 65 64 20 72  e.** [extended r
12cc0 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 66 65 61  esult codes] fea
12cd0 74 75 72 65 20 6f 66 20 53 51 4c 69 74 65 2e 20  ture of SQLite. 
12ce0 54 68 65 20 65 78 74 65 6e 64 65 64 20 72 65 73  The extended res
12cf0 75 6c 74 0a 2a 2a 20 63 6f 64 65 73 20 61 72 65  ult.** codes are
12d00 20 64 69 73 61 62 6c 65 64 20 62 79 20 64 65 66   disabled by def
12d10 61 75 6c 74 20 66 6f 72 20 68 69 73 74 6f 72 69  ault for histori
12d20 63 61 6c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74  cal compatibilit
12d30 79 20 63 6f 6e 73 69 64 65 72 61 74 69 6f 6e 73  y considerations
12d40 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
12d50 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32 32 30 31  ents:.** [H12201
12d60 5d 20 5b 48 31 32 32 30 32 5d 0a 2a 2f 0a 53 51  ] [H12202].*/.SQ
12d70 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
12d80 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65  ite3_extended_re
12d90 73 75 6c 74 5f 63 6f 64 65 73 28 73 71 6c 69 74  sult_codes(sqlit
12da0 65 33 2a 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 3b  e3*, int onoff);
12db0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
12dc0 3a 20 4c 61 73 74 20 49 6e 73 65 72 74 20 52 6f  : Last Insert Ro
12dd0 77 69 64 20 7b 48 31 32 32 32 30 7d 20 3c 53 31  wid {H12220} <S1
12de0 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68  0700>.**.** Each
12df0 20 65 6e 74 72 79 20 69 6e 20 61 6e 20 53 51 4c   entry in an SQL
12e00 69 74 65 20 74 61 62 6c 65 20 68 61 73 20 61 20  ite table has a 
12e10 75 6e 69 71 75 65 20 36 34 2d 62 69 74 20 73 69  unique 64-bit si
12e20 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20  gned.** integer 
12e30 6b 65 79 20 63 61 6c 6c 65 64 20 74 68 65 20 5b  key called the [
12e40 52 4f 57 49 44 20 7c 20 22 72 6f 77 69 64 22 5d  ROWID | "rowid"]
12e50 2e 20 54 68 65 20 72 6f 77 69 64 20 69 73 20 61  . The rowid is a
12e60 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c 65 0a  lways available.
12e70 2a 2a 20 61 73 20 61 6e 20 75 6e 64 65 63 6c 61  ** as an undecla
12e80 72 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64  red column named
12e90 20 52 4f 57 49 44 2c 20 4f 49 44 2c 20 6f 72 20   ROWID, OID, or 
12ea0 5f 52 4f 57 49 44 5f 20 61 73 20 6c 6f 6e 67 20  _ROWID_ as long 
12eb0 61 73 20 74 68 6f 73 65 0a 2a 2a 20 6e 61 6d 65  as those.** name
12ec0 73 20 61 72 65 20 6e 6f 74 20 61 6c 73 6f 20 75  s are not also u
12ed0 73 65 64 20 62 79 20 65 78 70 6c 69 63 69 74 6c  sed by explicitl
12ee0 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d  y declared colum
12ef0 6e 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20 74 61  ns. If.** the ta
12f00 62 6c 65 20 68 61 73 20 61 20 63 6f 6c 75 6d 6e  ble has a column
12f10 20 6f 66 20 74 79 70 65 20 5b 49 4e 54 45 47 45   of type [INTEGE
12f20 52 20 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 74  R PRIMARY KEY] t
12f30 68 65 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e 0a  hen that column.
12f40 2a 2a 20 69 73 20 61 6e 6f 74 68 65 72 20 61 6c  ** is another al
12f50 69 61 73 20 66 6f 72 20 74 68 65 20 72 6f 77 69  ias for the rowi
12f60 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  d..**.** This ro
12f70 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68  utine returns th
12f80 65 20 5b 72 6f 77 69 64 5d 20 6f 66 20 74 68 65  e [rowid] of the
12f90 20 6d 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20   most recent.** 
12fa0 73 75 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45  successful [INSE
12fb0 52 54 5d 20 69 6e 74 6f 20 74 68 65 20 64 61 74  RT] into the dat
12fc0 61 62 61 73 65 20 66 72 6f 6d 20 74 68 65 20 5b  abase from the [
12fd0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
12fe0 69 6f 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66  ion].** in the f
12ff0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 20  irst argument.  
13000 49 66 20 6e 6f 20 73 75 63 63 65 73 73 66 75 6c  If no successful
13010 20 5b 49 4e 53 45 52 54 5d 73 0a 2a 2a 20 68 61   [INSERT]s.** ha
13020 76 65 20 65 76 65 72 20 6f 63 63 75 72 72 65 64  ve ever occurred
13030 20 6f 6e 20 74 68 61 74 20 64 61 74 61 62 61 73   on that databas
13040 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 7a 65  e connection, ze
13050 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  ro is returned..
13060 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 5b 49 4e 53  **.** If an [INS
13070 45 52 54 5d 20 6f 63 63 75 72 73 20 77 69 74 68  ERT] occurs with
13080 69 6e 20 61 20 74 72 69 67 67 65 72 2c 20 74 68  in a trigger, th
13090 65 6e 20 74 68 65 20 5b 72 6f 77 69 64 5d 20 6f  en the [rowid] o
130a0 66 20 74 68 65 20 69 6e 73 65 72 74 65 64 0a 2a  f the inserted.*
130b0 2a 20 72 6f 77 20 69 73 20 72 65 74 75 72 6e 65  * row is returne
130c0 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e  d by this routin
130d0 65 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65  e as long as the
130e0 20 74 72 69 67 67 65 72 20 69 73 20 72 75 6e 6e   trigger is runn
130f0 69 6e 67 2e 0a 2a 2a 20 42 75 74 20 6f 6e 63 65  ing..** But once
13100 20 74 68 65 20 74 72 69 67 67 65 72 20 74 65 72   the trigger ter
13110 6d 69 6e 61 74 65 73 2c 20 74 68 65 20 76 61 6c  minates, the val
13120 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  ue returned by t
13130 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72  his routine.** r
13140 65 76 65 72 74 73 20 74 6f 20 74 68 65 20 6c 61  everts to the la
13150 73 74 20 76 61 6c 75 65 20 69 6e 73 65 72 74 65  st value inserte
13160 64 20 62 65 66 6f 72 65 20 74 68 65 20 74 72 69  d before the tri
13170 67 67 65 72 20 66 69 72 65 64 2e 0a 2a 2a 0a 2a  gger fired..**.*
13180 2a 20 41 6e 20 5b 49 4e 53 45 52 54 5d 20 74 68  * An [INSERT] th
13190 61 74 20 66 61 69 6c 73 20 64 75 65 20 74 6f 20  at fails due to 
131a0 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f  a constraint vio
131b0 6c 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 61 0a  lation is not a.
131c0 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20 5b 49  ** successful [I
131d0 4e 53 45 52 54 5d 20 61 6e 64 20 64 6f 65 73 20  NSERT] and does 
131e0 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20 76  not change the v
131f0 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
13200 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65   this.** routine
13210 2e 20 20 54 68 75 73 20 49 4e 53 45 52 54 20 4f  .  Thus INSERT O
13220 52 20 46 41 49 4c 2c 20 49 4e 53 45 52 54 20 4f  R FAIL, INSERT O
13230 52 20 49 47 4e 4f 52 45 2c 20 49 4e 53 45 52 54  R IGNORE, INSERT
13240 20 4f 52 20 52 4f 4c 4c 42 41 43 4b 2c 0a 2a 2a   OR ROLLBACK,.**
13250 20 61 6e 64 20 49 4e 53 45 52 54 20 4f 52 20 41   and INSERT OR A
13260 42 4f 52 54 20 6d 61 6b 65 20 6e 6f 20 63 68 61  BORT make no cha
13270 6e 67 65 73 20 74 6f 20 74 68 65 20 72 65 74 75  nges to the retu
13280 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73  rn value of this
13290 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 77 68 65 6e  .** routine when
132a0 20 74 68 65 69 72 20 69 6e 73 65 72 74 69 6f 6e   their insertion
132b0 20 66 61 69 6c 73 2e 20 20 57 68 65 6e 20 49 4e   fails.  When IN
132c0 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43 45 0a  SERT OR REPLACE.
132d0 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 20  ** encounters a 
132e0 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61  constraint viola
132f0 74 69 6f 6e 2c 20 69 74 20 64 6f 65 73 20 6e 6f  tion, it does no
13300 74 20 66 61 69 6c 2e 20 20 54 68 65 0a 2a 2a 20  t fail.  The.** 
13310 49 4e 53 45 52 54 20 63 6f 6e 74 69 6e 75 65 73  INSERT continues
13320 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 61   to completion a
13330 66 74 65 72 20 64 65 6c 65 74 69 6e 67 20 72 6f  fter deleting ro
13340 77 73 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a  ws that caused.*
13350 2a 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74  * the constraint
13360 20 70 72 6f 62 6c 65 6d 20 73 6f 20 49 4e 53 45   problem so INSE
13370 52 54 20 4f 52 20 52 45 50 4c 41 43 45 20 77 69  RT OR REPLACE wi
13380 6c 6c 20 61 6c 77 61 79 73 20 63 68 61 6e 67 65  ll always change
13390 0a 2a 2a 20 74 68 65 20 72 65 74 75 72 6e 20 76  .** the return v
133a0 61 6c 75 65 20 6f 66 20 74 68 69 73 20 69 6e 74  alue of this int
133b0 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f  erface..**.** Fo
133c0 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f  r the purposes o
133d0 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20  f this routine, 
133e0 61 6e 20 5b 49 4e 53 45 52 54 5d 20 69 73 20 63  an [INSERT] is c
133f0 6f 6e 73 69 64 65 72 65 64 20 74 6f 0a 2a 2a 20  onsidered to.** 
13400 62 65 20 73 75 63 63 65 73 73 66 75 6c 20 65 76  be successful ev
13410 65 6e 20 69 66 20 69 74 20 69 73 20 73 75 62 73  en if it is subs
13420 65 71 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 20  equently rolled 
13430 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  back..**.** Requ
13440 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
13450 32 32 32 31 5d 20 5b 48 31 32 32 32 33 5d 0a 2a  2221] [H12223].*
13460 2a 0a 2a 2a 20 49 66 20 61 20 73 65 70 61 72 61  *.** If a separa
13470 74 65 20 74 68 72 65 61 64 20 70 65 72 66 6f 72  te thread perfor
13480 6d 73 20 61 20 6e 65 77 20 5b 49 4e 53 45 52 54  ms a new [INSERT
13490 5d 20 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a  ] on the same.**
134a0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
134b0 74 69 6f 6e 20 77 68 69 6c 65 20 74 68 65 20 5b  tion while the [
134c0 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73  sqlite3_last_ins
134d0 65 72 74 5f 72 6f 77 69 64 28 29 5d 0a 2a 2a 20  ert_rowid()].** 
134e0 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e  function is runn
134f0 69 6e 67 20 61 6e 64 20 74 68 75 73 20 63 68 61  ing and thus cha
13500 6e 67 65 73 20 74 68 65 20 6c 61 73 74 20 69 6e  nges the last in
13510 73 65 72 74 20 5b 72 6f 77 69 64 5d 2c 0a 2a 2a  sert [rowid],.**
13520 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20   then the value 
13530 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c  returned by [sql
13540 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74  ite3_last_insert
13550 5f 72 6f 77 69 64 28 29 5d 20 69 73 0a 2a 2a 20  _rowid()] is.** 
13560 75 6e 70 72 65 64 69 63 74 61 62 6c 65 20 61 6e  unpredictable an
13570 64 20 6d 69 67 68 74 20 6e 6f 74 20 65 71 75 61  d might not equa
13580 6c 20 65 69 74 68 65 72 20 74 68 65 20 6f 6c 64  l either the old
13590 20 6f 72 20 74 68 65 20 6e 65 77 0a 2a 2a 20 6c   or the new.** l
135a0 61 73 74 20 69 6e 73 65 72 74 20 5b 72 6f 77 69  ast insert [rowi
135b0 64 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  d]..*/.SQLITE_AP
135c0 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  I sqlite3_int64 
135d0 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73  sqlite3_last_ins
135e0 65 72 74 5f 72 6f 77 69 64 28 73 71 6c 69 74 65  ert_rowid(sqlite
135f0 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  3*);../*.** CAPI
13600 33 52 45 46 3a 20 43 6f 75 6e 74 20 54 68 65 20  3REF: Count The 
13610 4e 75 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d  Number Of Rows M
13620 6f 64 69 66 69 65 64 20 7b 48 31 32 32 34 30 7d  odified {H12240}
13630 20 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S10600>.**.** 
13640 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
13650 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
13660 20 6f 66 20 64 61 74 61 62 61 73 65 20 72 6f 77   of database row
13670 73 20 74 68 61 74 20 77 65 72 65 20 63 68 61 6e  s that were chan
13680 67 65 64 0a 2a 2a 20 6f 72 20 69 6e 73 65 72 74  ged.** or insert
13690 65 64 20 6f 72 20 64 65 6c 65 74 65 64 20 62 79  ed or deleted by
136a0 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
136b0 6c 79 20 63 6f 6d 70 6c 65 74 65 64 20 53 51 4c  ly completed SQL
136c0 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 6f 6e   statement.** on
136d0 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
136e0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63 69  onnection] speci
136f0 66 69 65 64 20 62 79 20 74 68 65 20 66 69 72 73  fied by the firs
13700 74 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20  t parameter..** 
13710 4f 6e 6c 79 20 63 68 61 6e 67 65 73 20 74 68 61  Only changes tha
13720 74 20 61 72 65 20 64 69 72 65 63 74 6c 79 20 73  t are directly s
13730 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20  pecified by the 
13740 5b 49 4e 53 45 52 54 5d 2c 20 5b 55 50 44 41 54  [INSERT], [UPDAT
13750 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 44 45 4c 45 54  E],.** or [DELET
13760 45 5d 20 73 74 61 74 65 6d 65 6e 74 20 61 72 65  E] statement are
13770 20 63 6f 75 6e 74 65 64 2e 20 20 41 75 78 69 6c   counted.  Auxil
13780 69 61 72 79 20 63 68 61 6e 67 65 73 20 63 61 75  iary changes cau
13790 73 65 64 20 62 79 0a 2a 2a 20 74 72 69 67 67 65  sed by.** trigge
137a0 72 73 20 6f 72 20 5b 66 6f 72 65 69 67 6e 20 6b  rs or [foreign k
137b0 65 79 20 61 63 74 69 6f 6e 73 5d 20 61 72 65 20  ey actions] are 
137c0 6e 6f 74 20 63 6f 75 6e 74 65 64 2e 20 55 73 65  not counted. Use
137d0 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   the.** [sqlite3
137e0 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29  _total_changes()
137f0 5d 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 66 69  ] function to fi
13800 6e 64 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d  nd the total num
13810 62 65 72 20 6f 66 20 63 68 61 6e 67 65 73 0a 2a  ber of changes.*
13820 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 68 61 6e  * including chan
13830 67 65 73 20 63 61 75 73 65 64 20 62 79 20 74 72  ges caused by tr
13840 69 67 67 65 72 73 20 61 6e 64 20 66 6f 72 65 69  iggers and forei
13850 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e 73 2e 0a  gn key actions..
13860 2a 2a 0a 2a 2a 20 43 68 61 6e 67 65 73 20 74 6f  **.** Changes to
13870 20 61 20 76 69 65 77 20 74 68 61 74 20 61 72 65   a view that are
13880 20 73 69 6d 75 6c 61 74 65 64 20 62 79 20 61 6e   simulated by an
13890 20 5b 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69   [INSTEAD OF tri
138a0 67 67 65 72 5d 0a 2a 2a 20 61 72 65 20 6e 6f 74  gger].** are not
138b0 20 63 6f 75 6e 74 65 64 2e 20 20 4f 6e 6c 79 20   counted.  Only 
138c0 72 65 61 6c 20 74 61 62 6c 65 20 63 68 61 6e 67  real table chang
138d0 65 73 20 61 72 65 20 63 6f 75 6e 74 65 64 2e 0a  es are counted..
138e0 2a 2a 0a 2a 2a 20 41 20 22 72 6f 77 20 63 68 61  **.** A "row cha
138f0 6e 67 65 22 20 69 73 20 61 20 63 68 61 6e 67 65  nge" is a change
13900 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 72 6f 77   to a single row
13910 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62   of a single tab
13920 6c 65 0a 2a 2a 20 63 61 75 73 65 64 20 62 79 20  le.** caused by 
13930 61 6e 20 49 4e 53 45 52 54 2c 20 44 45 4c 45 54  an INSERT, DELET
13940 45 2c 20 6f 72 20 55 50 44 41 54 45 20 73 74 61  E, or UPDATE sta
13950 74 65 6d 65 6e 74 2e 20 20 52 6f 77 73 20 74 68  tement.  Rows th
13960 61 74 0a 2a 2a 20 61 72 65 20 63 68 61 6e 67 65  at.** are change
13970 64 20 61 73 20 73 69 64 65 20 65 66 66 65 63 74  d as side effect
13980 73 20 6f 66 20 5b 52 45 50 4c 41 43 45 5d 20 63  s of [REPLACE] c
13990 6f 6e 73 74 72 61 69 6e 74 20 72 65 73 6f 6c 75  onstraint resolu
139a0 74 69 6f 6e 2c 0a 2a 2a 20 72 6f 6c 6c 62 61 63  tion,.** rollbac
139b0 6b 2c 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73  k, ABORT process
139c0 69 6e 67 2c 20 5b 44 52 4f 50 20 54 41 42 4c 45  ing, [DROP TABLE
139d0 5d 2c 20 6f 72 20 62 79 20 61 6e 79 20 6f 74 68  ], or by any oth
139e0 65 72 0a 2a 2a 20 6d 65 63 68 61 6e 69 73 6d 73  er.** mechanisms
139f0 20 64 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73   do not count as
13a00 20 64 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e   direct row chan
13a10 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 22 74 72  ges..**.** A "tr
13a20 69 67 67 65 72 20 63 6f 6e 74 65 78 74 22 20 69  igger context" i
13a30 73 20 61 20 73 63 6f 70 65 20 6f 66 20 65 78 65  s a scope of exe
13a40 63 75 74 69 6f 6e 20 74 68 61 74 20 62 65 67 69  cution that begi
13a50 6e 73 20 61 6e 64 0a 2a 2a 20 65 6e 64 73 20 77  ns and.** ends w
13a60 69 74 68 20 74 68 65 20 73 63 72 69 70 74 20 6f  ith the script o
13a70 66 20 61 20 5b 43 52 45 41 54 45 20 54 52 49 47  f a [CREATE TRIG
13a80 47 45 52 20 7c 20 74 72 69 67 67 65 72 5d 2e 20  GER | trigger]. 
13a90 0a 2a 2a 20 4d 6f 73 74 20 53 51 4c 20 73 74 61  .** Most SQL sta
13aa0 74 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 65  tements are.** e
13ab0 76 61 6c 75 61 74 65 64 20 6f 75 74 73 69 64 65  valuated outside
13ac0 20 6f 66 20 61 6e 79 20 74 72 69 67 67 65 72 2e   of any trigger.
13ad0 20 20 54 68 69 73 20 69 73 20 74 68 65 20 22 74    This is the "t
13ae0 6f 70 20 6c 65 76 65 6c 22 0a 2a 2a 20 74 72 69  op level".** tri
13af0 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 49  gger context.  I
13b00 66 20 61 20 74 72 69 67 67 65 72 20 66 69 72 65  f a trigger fire
13b10 73 20 66 72 6f 6d 20 74 68 65 20 74 6f 70 20 6c  s from the top l
13b20 65 76 65 6c 2c 20 61 0a 2a 2a 20 6e 65 77 20 74  evel, a.** new t
13b30 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 20 69  rigger context i
13b40 73 20 65 6e 74 65 72 65 64 20 66 6f 72 20 74 68  s entered for th
13b50 65 20 64 75 72 61 74 69 6f 6e 20 6f 66 20 74 68  e duration of th
13b60 61 74 20 6f 6e 65 0a 2a 2a 20 74 72 69 67 67 65  at one.** trigge
13b70 72 2e 20 20 53 75 62 74 72 69 67 67 65 72 73 20  r.  Subtriggers 
13b80 63 72 65 61 74 65 20 73 75 62 63 6f 6e 74 65 78  create subcontex
13b90 74 73 20 66 6f 72 20 74 68 65 69 72 20 64 75 72  ts for their dur
13ba0 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c  ation..**.** Cal
13bb0 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 65 78  ling [sqlite3_ex
13bc0 65 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65  ec()] or [sqlite
13bd0 33 5f 73 74 65 70 28 29 5d 20 72 65 63 75 72 73  3_step()] recurs
13be0 69 76 65 6c 79 20 64 6f 65 73 0a 2a 2a 20 6e 6f  ively does.** no
13bf0 74 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 74  t create a new t
13c00 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2e 0a  rigger context..
13c10 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
13c20 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20  ion returns the 
13c30 6e 75 6d 62 65 72 20 6f 66 20 64 69 72 65 63 74  number of direct
13c40 20 72 6f 77 20 63 68 61 6e 67 65 73 20 69 6e 20   row changes in 
13c50 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 72 65 63 65  the.** most rece
13c60 6e 74 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54  nt INSERT, UPDAT
13c70 45 2c 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61  E, or DELETE sta
13c80 74 65 6d 65 6e 74 20 77 69 74 68 69 6e 20 74 68  tement within th
13c90 65 20 73 61 6d 65 0a 2a 2a 20 74 72 69 67 67 65  e same.** trigge
13ca0 72 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a 2a 2a  r context..**.**
13cb0 20 54 68 75 73 2c 20 77 68 65 6e 20 63 61 6c 6c   Thus, when call
13cc0 65 64 20 66 72 6f 6d 20 74 68 65 20 74 6f 70 20  ed from the top 
13cd0 6c 65 76 65 6c 2c 20 74 68 69 73 20 66 75 6e 63  level, this func
13ce0 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65  tion returns the
13cf0 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  .** number of ch
13d00 61 6e 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73  anges in the mos
13d10 74 20 72 65 63 65 6e 74 20 49 4e 53 45 52 54 2c  t recent INSERT,
13d20 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45   UPDATE, or DELE
13d30 54 45 0a 2a 2a 20 74 68 61 74 20 61 6c 73 6f 20  TE.** that also 
13d40 6f 63 63 75 72 72 65 64 20 61 74 20 74 68 65 20  occurred at the 
13d50 74 6f 70 20 6c 65 76 65 6c 2e 20 20 57 69 74 68  top level.  With
13d60 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61  in the body of a
13d70 20 74 72 69 67 67 65 72 2c 0a 2a 2a 20 74 68 65   trigger,.** the
13d80 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73   sqlite3_changes
13d90 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e  () interface can
13da0 20 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 66 69   be called to fi
13db0 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  nd the number of
13dc0 0a 2a 2a 20 63 68 61 6e 67 65 73 20 69 6e 20 74  .** changes in t
13dd0 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  he most recently
13de0 20 63 6f 6d 70 6c 65 74 65 64 20 49 4e 53 45 52   completed INSER
13df0 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45  T, UPDATE, or DE
13e00 4c 45 54 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  LETE.** statemen
13e10 74 20 77 69 74 68 69 6e 20 74 68 65 20 62 6f 64  t within the bod
13e20 79 20 6f 66 20 74 68 65 20 73 61 6d 65 20 74 72  y of the same tr
13e30 69 67 67 65 72 2e 0a 2a 2a 20 48 6f 77 65 76 65  igger..** Howeve
13e40 72 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 72 65  r, the number re
13e50 74 75 72 6e 65 64 20 64 6f 65 73 20 6e 6f 74 20  turned does not 
13e60 69 6e 63 6c 75 64 65 20 63 68 61 6e 67 65 73 0a  include changes.
13e70 2a 2a 20 63 61 75 73 65 64 20 62 79 20 73 75 62  ** caused by sub
13e80 74 72 69 67 67 65 72 73 20 73 69 6e 63 65 20 74  triggers since t
13e90 68 6f 73 65 20 68 61 76 65 20 74 68 65 69 72 20  hose have their 
13ea0 6f 77 6e 20 63 6f 6e 74 65 78 74 2e 0a 2a 2a 0a  own context..**.
13eb0 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20  ** See also the 
13ec0 5b 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63  [sqlite3_total_c
13ed0 68 61 6e 67 65 73 28 29 5d 20 69 6e 74 65 72 66  hanges()] interf
13ee0 61 63 65 20 61 6e 64 20 74 68 65 0a 2a 2a 20 5b  ace and the.** [
13ef0 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 20 70 72  count_changes pr
13f00 61 67 6d 61 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  agma]..**.** Req
13f10 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
13f20 31 32 32 34 31 5d 20 5b 48 31 32 32 34 33 5d 0a  12241] [H12243].
13f30 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 65 70 61 72  **.** If a separ
13f40 61 74 65 20 74 68 72 65 61 64 20 6d 61 6b 65 73  ate thread makes
13f50 20 63 68 61 6e 67 65 73 20 6f 6e 20 74 68 65 20   changes on the 
13f60 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f  same database co
13f70 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77 68 69 6c  nnection.** whil
13f80 65 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67  e [sqlite3_chang
13f90 65 73 28 29 5d 20 69 73 20 72 75 6e 6e 69 6e 67  es()] is running
13fa0 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20   then the value 
13fb0 72 65 74 75 72 6e 65 64 0a 2a 2a 20 69 73 20 75  returned.** is u
13fc0 6e 70 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64  npredictable and
13fd0 20 6e 6f 74 20 6d 65 61 6e 69 6e 67 66 75 6c 2e   not meaningful.
13fe0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
13ff0 6e 74 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67  nt sqlite3_chang
14000 65 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f  es(sqlite3*);../
14010 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54  *.** CAPI3REF: T
14020 6f 74 61 6c 20 4e 75 6d 62 65 72 20 4f 66 20 52  otal Number Of R
14030 6f 77 73 20 4d 6f 64 69 66 69 65 64 20 7b 48 31  ows Modified {H1
14040 32 32 36 30 7d 20 3c 53 31 30 36 30 30 3e 0a 2a  2260} <S10600>.*
14050 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
14060 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  on returns the n
14070 75 6d 62 65 72 20 6f 66 20 72 6f 77 20 63 68 61  umber of row cha
14080 6e 67 65 73 20 63 61 75 73 65 64 20 62 79 20 5b  nges caused by [
14090 49 4e 53 45 52 54 5d 2c 0a 2a 2a 20 5b 55 50 44  INSERT],.** [UPD
140a0 41 54 45 5d 20 6f 72 20 5b 44 45 4c 45 54 45 5d  ATE] or [DELETE]
140b0 20 73 74 61 74 65 6d 65 6e 74 73 20 73 69 6e 63   statements sinc
140c0 65 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  e the [database 
140d0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 77 61 73 20  connection] was 
140e0 6f 70 65 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 63  opened..** The c
140f0 6f 75 6e 74 20 69 6e 63 6c 75 64 65 73 20 61 6c  ount includes al
14100 6c 20 63 68 61 6e 67 65 73 20 66 72 6f 6d 20 61  l changes from a
14110 6c 6c 20 5b 43 52 45 41 54 45 20 54 52 49 47 47  ll [CREATE TRIGG
14120 45 52 20 7c 20 74 72 69 67 67 65 72 5d 20 0a 2a  ER | trigger] .*
14130 2a 20 63 6f 6e 74 65 78 74 73 20 61 6e 64 20 63  * contexts and c
14140 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 5b  hanges made by [
14150 66 6f 72 65 69 67 6e 20 6b 65 79 20 61 63 74 69  foreign key acti
14160 6f 6e 73 5d 2e 20 48 6f 77 65 76 65 72 2c 0a 2a  ons]. However,.*
14170 2a 20 74 68 65 20 63 6f 75 6e 74 20 64 6f 65 73  * the count does
14180 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63 68 61   not include cha
14190 6e 67 65 73 20 75 73 65 64 20 74 6f 20 69 6d 70  nges used to imp
141a0 6c 65 6d 65 6e 74 20 5b 52 45 50 4c 41 43 45 5d  lement [REPLACE]
141b0 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 0a 2a 2a   constraints,.**
141c0 20 64 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 6f 72   do rollbacks or
141d0 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e   ABORT processin
141e0 67 2c 20 6f 72 20 5b 44 52 4f 50 20 54 41 42 4c  g, or [DROP TABL
141f0 45 5d 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 20  E] processing.  
14200 54 68 65 0a 2a 2a 20 63 6f 75 6e 74 20 64 6f 65  The.** count doe
14210 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 72 6f  s not include ro
14220 77 73 20 6f 66 20 76 69 65 77 73 20 74 68 61 74  ws of views that
14230 20 66 69 72 65 20 61 6e 20 5b 49 4e 53 54 45 41   fire an [INSTEA
14240 44 20 4f 46 20 74 72 69 67 67 65 72 5d 2c 0a 2a  D OF trigger],.*
14250 2a 20 74 68 6f 75 67 68 20 69 66 20 74 68 65 20  * though if the 
14260 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67 67  INSTEAD OF trigg
14270 65 72 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73  er makes changes
14280 20 6f 66 20 69 74 73 20 6f 77 6e 2c 20 74 68 6f   of its own, tho
14290 73 65 20 63 68 61 6e 67 65 73 20 0a 2a 2a 20 61  se changes .** a
142a0 72 65 20 63 6f 75 6e 74 65 64 2e 0a 2a 2a 20 54  re counted..** T
142b0 68 65 20 63 68 61 6e 67 65 73 20 61 72 65 20 63  he changes are c
142c0 6f 75 6e 74 65 64 20 61 73 20 73 6f 6f 6e 20 61  ounted as soon a
142d0 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  s the statement 
142e0 74 68 61 74 20 6d 61 6b 65 73 20 74 68 65 6d 20  that makes them 
142f0 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 64 20  is.** completed 
14300 28 77 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d  (when the statem
14310 65 6e 74 20 68 61 6e 64 6c 65 20 69 73 20 70 61  ent handle is pa
14320 73 73 65 64 20 74 6f 20 5b 73 71 6c 69 74 65 33  ssed to [sqlite3
14330 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a 2a 2a 20  _reset()] or.** 
14340 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a  [sqlite3_finaliz
14350 65 28 29 5d 29 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  e()])..**.** See
14360 20 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74   also the [sqlit
14370 65 33 5f 63 68 61 6e 67 65 73 28 29 5d 20 69 6e  e3_changes()] in
14380 74 65 72 66 61 63 65 20 61 6e 64 20 74 68 65 0a  terface and the.
14390 2a 2a 20 5b 63 6f 75 6e 74 5f 63 68 61 6e 67 65  ** [count_change
143a0 73 20 70 72 61 67 6d 61 5d 2e 0a 2a 2a 0a 2a 2a  s pragma]..**.**
143b0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
143c0 2a 20 5b 48 31 32 32 36 31 5d 20 5b 48 31 32 32  * [H12261] [H122
143d0 36 33 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73  63].**.** If a s
143e0 65 70 61 72 61 74 65 20 74 68 72 65 61 64 20 6d  eparate thread m
143f0 61 6b 65 73 20 63 68 61 6e 67 65 73 20 6f 6e 20  akes changes on 
14400 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
14410 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20  e connection.** 
14420 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 74  while [sqlite3_t
14430 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20  otal_changes()] 
14440 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65 6e 20  is running then 
14450 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 72 65 74  the value.** ret
14460 75 72 6e 65 64 20 69 73 20 75 6e 70 72 65 64 69  urned is unpredi
14470 63 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20 6d  ctable and not m
14480 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53 51  eaningful..*/.SQ
14490 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
144a0 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67  ite3_total_chang
144b0 65 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f  es(sqlite3*);../
144c0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49  *.** CAPI3REF: I
144d0 6e 74 65 72 72 75 70 74 20 41 20 4c 6f 6e 67 2d  nterrupt A Long-
144e0 52 75 6e 6e 69 6e 67 20 51 75 65 72 79 20 7b 48  Running Query {H
144f0 31 32 32 37 30 7d 20 3c 53 33 30 35 30 30 3e 0a  12270} <S30500>.
14500 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
14510 69 6f 6e 20 63 61 75 73 65 73 20 61 6e 79 20 70  ion causes any p
14520 65 6e 64 69 6e 67 20 64 61 74 61 62 61 73 65 20  ending database 
14530 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 61 62 6f  operation to abo
14540 72 74 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e  rt and.** return
14550 20 61 74 20 69 74 73 20 65 61 72 6c 69 65 73 74   at its earliest
14560 20 6f 70 70 6f 72 74 75 6e 69 74 79 2e 20 54 68   opportunity. Th
14570 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 79  is routine is ty
14580 70 69 63 61 6c 6c 79 0a 2a 2a 20 63 61 6c 6c 65  pically.** calle
14590 64 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f  d in response to
145a0 20 61 20 75 73 65 72 20 61 63 74 69 6f 6e 20 73   a user action s
145b0 75 63 68 20 61 73 20 70 72 65 73 73 69 6e 67 20  uch as pressing 
145c0 22 43 61 6e 63 65 6c 22 0a 2a 2a 20 6f 72 20 43  "Cancel".** or C
145d0 74 72 6c 2d 43 20 77 68 65 72 65 20 74 68 65 20  trl-C where the 
145e0 75 73 65 72 20 77 61 6e 74 73 20 61 20 6c 6f 6e  user wants a lon
145f0 67 20 71 75 65 72 79 20 6f 70 65 72 61 74 69 6f  g query operatio
14600 6e 20 74 6f 20 68 61 6c 74 0a 2a 2a 20 69 6d 6d  n to halt.** imm
14610 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20  ediately..**.** 
14620 49 74 20 69 73 20 73 61 66 65 20 74 6f 20 63 61  It is safe to ca
14630 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ll this routine 
14640 66 72 6f 6d 20 61 20 74 68 72 65 61 64 20 64 69  from a thread di
14650 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65  fferent from the
14660 0a 2a 2a 20 74 68 72 65 61 64 20 74 68 61 74 20  .** thread that 
14670 69 73 20 63 75 72 72 65 6e 74 6c 79 20 72 75 6e  is currently run
14680 6e 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73  ning the databas
14690 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 75  e operation.  Bu
146a0 74 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 20 73  t it.** is not s
146b0 61 66 65 20 74 6f 20 63 61 6c 6c 20 74 68 69 73  afe to call this
146c0 20 72 6f 75 74 69 6e 65 20 77 69 74 68 20 61 20   routine with a 
146d0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
146e0 74 69 6f 6e 5d 20 74 68 61 74 0a 2a 2a 20 69 73  tion] that.** is
146f0 20 63 6c 6f 73 65 64 20 6f 72 20 6d 69 67 68 74   closed or might
14700 20 63 6c 6f 73 65 20 62 65 66 6f 72 65 20 73 71   close before sq
14710 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28  lite3_interrupt(
14720 29 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a  ) returns..**.**
14730 20 49 66 20 61 6e 20 53 51 4c 20 6f 70 65 72 61   If an SQL opera
14740 74 69 6f 6e 20 69 73 20 76 65 72 79 20 6e 65 61  tion is very nea
14750 72 6c 79 20 66 69 6e 69 73 68 65 64 20 61 74 20  rly finished at 
14760 74 68 65 20 74 69 6d 65 20 77 68 65 6e 0a 2a 2a  the time when.**
14770 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
14780 70 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2c 20  pt() is called, 
14790 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20 6e 6f  then it might no
147a0 74 20 68 61 76 65 20 61 6e 20 6f 70 70 6f 72 74  t have an opport
147b0 75 6e 69 74 79 0a 2a 2a 20 74 6f 20 62 65 20 69  unity.** to be i
147c0 6e 74 65 72 72 75 70 74 65 64 20 61 6e 64 20 6d  nterrupted and m
147d0 69 67 68 74 20 63 6f 6e 74 69 6e 75 65 20 74 6f  ight continue to
147e0 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a   completion..**.
147f0 2a 2a 20 41 6e 20 53 51 4c 20 6f 70 65 72 61 74  ** An SQL operat
14800 69 6f 6e 20 74 68 61 74 20 69 73 20 69 6e 74 65  ion that is inte
14810 72 72 75 70 74 65 64 20 77 69 6c 6c 20 72 65 74  rrupted will ret
14820 75 72 6e 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45  urn [SQLITE_INTE
14830 52 52 55 50 54 5d 2e 0a 2a 2a 20 49 66 20 74 68  RRUPT]..** If th
14840 65 20 69 6e 74 65 72 72 75 70 74 65 64 20 53 51  e interrupted SQ
14850 4c 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61  L operation is a
14860 6e 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45  n INSERT, UPDATE
14870 2c 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 74  , or DELETE.** t
14880 68 61 74 20 69 73 20 69 6e 73 69 64 65 20 61 6e  hat is inside an
14890 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e 73 61   explicit transa
148a0 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20  ction, then the 
148b0 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74 69  entire transacti
148c0 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 72 6f  on.** will be ro
148d0 6c 6c 65 64 20 62 61 63 6b 20 61 75 74 6f 6d 61  lled back automa
148e0 74 69 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54  tically..**.** T
148f0 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  he sqlite3_inter
14900 72 75 70 74 28 44 29 20 63 61 6c 6c 20 69 73 20  rupt(D) call is 
14910 69 6e 20 65 66 66 65 63 74 20 75 6e 74 69 6c 20  in effect until 
14920 61 6c 6c 20 63 75 72 72 65 6e 74 6c 79 20 72 75  all currently ru
14930 6e 6e 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61  nning.** SQL sta
14940 74 65 6d 65 6e 74 73 20 6f 6e 20 5b 64 61 74 61  tements on [data
14950 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
14960 20 44 20 63 6f 6d 70 6c 65 74 65 2e 20 20 41 6e   D complete.  An
14970 79 20 6e 65 77 20 53 51 4c 20 73 74 61 74 65 6d  y new SQL statem
14980 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65  ents.** that are
14990 20 73 74 61 72 74 65 64 20 61 66 74 65 72 20 74   started after t
149a0 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  he sqlite3_inter
149b0 72 75 70 74 28 29 20 63 61 6c 6c 20 61 6e 64 20  rupt() call and 
149c0 62 65 66 6f 72 65 20 74 68 65 20 0a 2a 2a 20 72  before the .** r
149d0 75 6e 6e 69 6e 67 20 73 74 61 74 65 6d 65 6e 74  unning statement
149e0 73 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20 61  s reaches zero a
149f0 72 65 20 69 6e 74 65 72 72 75 70 74 65 64 20 61  re interrupted a
14a00 73 20 69 66 20 74 68 65 79 20 68 61 64 20 62 65  s if they had be
14a10 65 6e 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 70 72  en.** running pr
14a20 69 6f 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74  ior to the sqlit
14a30 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20 63  e3_interrupt() c
14a40 61 6c 6c 2e 20 20 4e 65 77 20 53 51 4c 20 73 74  all.  New SQL st
14a50 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74  atements.** that
14a60 20 61 72 65 20 73 74 61 72 74 65 64 20 61 66 74   are started aft
14a70 65 72 20 74 68 65 20 72 75 6e 6e 69 6e 67 20 73  er the running s
14a80 74 61 74 65 6d 65 6e 74 20 63 6f 75 6e 74 20 72  tatement count r
14a90 65 61 63 68 65 73 20 7a 65 72 6f 20 61 72 65 0a  eaches zero are.
14aa0 2a 2a 20 6e 6f 74 20 65 66 66 65 63 74 65 64 20  ** not effected 
14ab0 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  by the sqlite3_i
14ac0 6e 74 65 72 72 75 70 74 28 29 2e 0a 2a 2a 20 41  nterrupt()..** A
14ad0 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
14ae0 5f 69 6e 74 65 72 72 75 70 74 28 44 29 20 74 68  _interrupt(D) th
14af0 61 74 20 6f 63 63 75 72 73 20 77 68 65 6e 20 74  at occurs when t
14b00 68 65 72 65 20 61 72 65 20 6e 6f 20 72 75 6e 6e  here are no runn
14b10 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65  ing.** SQL state
14b20 6d 65 6e 74 73 20 69 73 20 61 20 6e 6f 2d 6f 70  ments is a no-op
14b30 20 61 6e 64 20 68 61 73 20 6e 6f 20 65 66 66 65   and has no effe
14b40 63 74 20 6f 6e 20 53 51 4c 20 73 74 61 74 65 6d  ct on SQL statem
14b50 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20 61 72 65  ents.** that are
14b60 20 73 74 61 72 74 65 64 20 61 66 74 65 72 20 74   started after t
14b70 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  he sqlite3_inter
14b80 72 75 70 74 28 29 20 63 61 6c 6c 20 72 65 74 75  rupt() call retu
14b90 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  rns..**.** Requi
14ba0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32  rements:.** [H12
14bb0 32 37 31 5d 20 5b 48 31 32 32 37 32 5d 0a 2a 2a  271] [H12272].**
14bc0 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62  .** If the datab
14bd0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63  ase connection c
14be0 6c 6f 73 65 73 20 77 68 69 6c 65 20 5b 73 71 6c  loses while [sql
14bf0 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29  ite3_interrupt()
14c00 5d 0a 2a 2a 20 69 73 20 72 75 6e 6e 69 6e 67 20  ].** is running 
14c10 74 68 65 6e 20 62 61 64 20 74 68 69 6e 67 73 20  then bad things 
14c20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 68 61 70 70  will likely happ
14c30 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  en..*/.SQLITE_AP
14c40 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 69  I void sqlite3_i
14c50 6e 74 65 72 72 75 70 74 28 73 71 6c 69 74 65 33  nterrupt(sqlite3
14c60 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
14c70 52 45 46 3a 20 44 65 74 65 72 6d 69 6e 65 20 49  REF: Determine I
14c80 66 20 41 6e 20 53 51 4c 20 53 74 61 74 65 6d 65  f An SQL Stateme
14c90 6e 74 20 49 73 20 43 6f 6d 70 6c 65 74 65 20 7b  nt Is Complete {
14ca0 48 31 30 35 31 30 7d 20 3c 53 37 30 32 30 30 3e  H10510} <S70200>
14cb0 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75  .**.** These rou
14cc0 74 69 6e 65 73 20 61 72 65 20 75 73 65 66 75 6c  tines are useful
14cd0 20 64 75 72 69 6e 67 20 63 6f 6d 6d 61 6e 64 2d   during command-
14ce0 6c 69 6e 65 20 69 6e 70 75 74 20 74 6f 20 64 65  line input to de
14cf0 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 0a 2a  termine if the.*
14d00 2a 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74 65  * currently ente
14d10 72 65 64 20 74 65 78 74 20 73 65 65 6d 73 20 74  red text seems t
14d20 6f 20 66 6f 72 6d 20 61 20 63 6f 6d 70 6c 65 74  o form a complet
14d30 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
14d40 6f 72 0a 2a 2a 20 69 66 20 61 64 64 69 74 69 6f  or.** if additio
14d50 6e 61 6c 20 69 6e 70 75 74 20 69 73 20 6e 65 65  nal input is nee
14d60 64 65 64 20 62 65 66 6f 72 65 20 73 65 6e 64 69  ded before sendi
14d70 6e 67 20 74 68 65 20 74 65 78 74 20 69 6e 74 6f  ng the text into
14d80 0a 2a 2a 20 53 51 4c 69 74 65 20 66 6f 72 20 70  .** SQLite for p
14d90 61 72 73 69 6e 67 2e 20 20 54 68 65 73 65 20 72  arsing.  These r
14da0 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 31  outines return 1
14db0 20 69 66 20 74 68 65 20 69 6e 70 75 74 20 73 74   if the input st
14dc0 72 69 6e 67 0a 2a 2a 20 61 70 70 65 61 72 73 20  ring.** appears 
14dd0 74 6f 20 62 65 20 61 20 63 6f 6d 70 6c 65 74 65  to be a complete
14de0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20   SQL statement. 
14df0 20 41 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20   A statement is 
14e00 6a 75 64 67 65 64 20 74 6f 20 62 65 0a 2a 2a 20  judged to be.** 
14e10 63 6f 6d 70 6c 65 74 65 20 69 66 20 69 74 20 65  complete if it e
14e20 6e 64 73 20 77 69 74 68 20 61 20 73 65 6d 69 63  nds with a semic
14e30 6f 6c 6f 6e 20 74 6f 6b 65 6e 20 61 6e 64 20 69  olon token and i
14e40 73 20 6e 6f 74 20 61 20 70 72 65 66 69 78 20 6f  s not a prefix o
14e50 66 20 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d  f a.** well-form
14e60 65 64 20 43 52 45 41 54 45 20 54 52 49 47 47 45  ed CREATE TRIGGE
14e70 52 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 53 65  R statement.  Se
14e80 6d 69 63 6f 6c 6f 6e 73 20 74 68 61 74 20 61 72  micolons that ar
14e90 65 20 65 6d 62 65 64 64 65 64 20 77 69 74 68 69  e embedded withi
14ea0 6e 0a 2a 2a 20 73 74 72 69 6e 67 20 6c 69 74 65  n.** string lite
14eb0 72 61 6c 73 20 6f 72 20 71 75 6f 74 65 64 20 69  rals or quoted i
14ec0 64 65 6e 74 69 66 69 65 72 20 6e 61 6d 65 73 20  dentifier names 
14ed0 6f 72 20 63 6f 6d 6d 65 6e 74 73 20 61 72 65 20  or comments are 
14ee0 6e 6f 74 0a 2a 2a 20 69 6e 64 65 70 65 6e 64 65  not.** independe
14ef0 6e 74 20 74 6f 6b 65 6e 73 20 28 74 68 65 79 20  nt tokens (they 
14f00 61 72 65 20 70 61 72 74 20 6f 66 20 74 68 65 20  are part of the 
14f10 74 6f 6b 65 6e 20 69 6e 20 77 68 69 63 68 20 74  token in which t
14f20 68 65 79 20 61 72 65 0a 2a 2a 20 65 6d 62 65 64  hey are.** embed
14f30 64 65 64 29 20 61 6e 64 20 74 68 75 73 20 64 6f  ded) and thus do
14f40 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20 61 20   not count as a 
14f50 73 74 61 74 65 6d 65 6e 74 20 74 65 72 6d 69 6e  statement termin
14f60 61 74 6f 72 2e 20 20 57 68 69 74 65 73 70 61 63  ator.  Whitespac
14f70 65 0a 2a 2a 20 61 6e 64 20 63 6f 6d 6d 65 6e 74  e.** and comment
14f80 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 74 68  s that follow th
14f90 65 20 66 69 6e 61 6c 20 73 65 6d 69 63 6f 6c 6f  e final semicolo
14fa0 6e 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a  n are ignored..*
14fb0 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69  *.** These routi
14fc0 6e 65 73 20 72 65 74 75 72 6e 20 30 20 69 66 20  nes return 0 if 
14fd0 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73  the statement is
14fe0 20 69 6e 63 6f 6d 70 6c 65 74 65 2e 20 20 49 66   incomplete.  If
14ff0 20 61 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c   a.** memory all
15000 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 74  ocation fails, t
15010 68 65 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  hen SQLITE_NOMEM
15020 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
15030 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
15040 65 73 20 64 6f 20 6e 6f 74 20 70 61 72 73 65 20  es do not parse 
15050 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
15060 74 73 20 74 68 75 73 0a 2a 2a 20 77 69 6c 6c 20  ts thus.** will 
15070 6e 6f 74 20 64 65 74 65 63 74 20 73 79 6e 74 61  not detect synta
15080 63 74 69 63 61 6c 6c 79 20 69 6e 63 6f 72 72 65  ctically incorre
15090 63 74 20 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66  ct SQL..**.** If
150a0 20 53 51 4c 69 74 65 20 68 61 73 20 6e 6f 74 20   SQLite has not 
150b0 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64  been initialized
150c0 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
150d0 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d 20 70 72  initialize()] pr
150e0 69 6f 72 20 0a 2a 2a 20 74 6f 20 69 6e 76 6f 6b  ior .** to invok
150f0 69 6e 67 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  ing sqlite3_comp
15100 6c 65 74 65 31 36 28 29 20 74 68 65 6e 20 73 71  lete16() then sq
15110 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
15120 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a  () is invoked.**
15130 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62   automatically b
15140 79 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65  y sqlite3_comple
15150 74 65 31 36 28 29 2e 20 20 49 66 20 74 68 61 74  te16().  If that
15160 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
15170 66 61 69 6c 73 2c 0a 2a 2a 20 74 68 65 6e 20 74  fails,.** then t
15180 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
15190 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f 6d  from sqlite3_com
151a0 70 6c 65 74 65 31 36 28 29 20 77 69 6c 6c 20 62  plete16() will b
151b0 65 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 72 65  e non-zero.** re
151c0 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74  gardless of whet
151d0 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 69  her or not the i
151e0 6e 70 75 74 20 53 51 4c 20 69 73 20 63 6f 6d 70  nput SQL is comp
151f0 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  lete..**.** Requ
15200 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 30 35 31  irements: [H1051
15210 31 5d 20 5b 48 31 30 35 31 32 5d 0a 2a 2a 0a 2a  1] [H10512].**.*
15220 2a 20 54 68 65 20 69 6e 70 75 74 20 74 6f 20 5b  * The input to [
15230 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65  sqlite3_complete
15240 28 29 5d 20 6d 75 73 74 20 62 65 20 61 20 7a 65  ()] must be a ze
15250 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a  ro-terminated.**
15260 20 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 0a 2a   UTF-8 string..*
15270 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 74  *.** The input t
15280 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c  o [sqlite3_compl
15290 65 74 65 31 36 28 29 5d 20 6d 75 73 74 20 62 65  ete16()] must be
152a0 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74   a zero-terminat
152b0 65 64 0a 2a 2a 20 55 54 46 2d 31 36 20 73 74 72  ed.** UTF-16 str
152c0 69 6e 67 20 69 6e 20 6e 61 74 69 76 65 20 62 79  ing in native by
152d0 74 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a 53 51 4c  te order..*/.SQL
152e0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
152f0 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 63 6f 6e  te3_complete(con
15300 73 74 20 63 68 61 72 20 2a 73 71 6c 29 3b 0a 53  st char *sql);.S
15310 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
15320 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36  lite3_complete16
15330 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c  (const void *sql
15340 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
15350 45 46 3a 20 52 65 67 69 73 74 65 72 20 41 20 43  EF: Register A C
15360 61 6c 6c 62 61 63 6b 20 54 6f 20 48 61 6e 64 6c  allback To Handl
15370 65 20 53 51 4c 49 54 45 5f 42 55 53 59 20 45 72  e SQLITE_BUSY Er
15380 72 6f 72 73 20 7b 48 31 32 33 31 30 7d 20 3c 53  rors {H12310} <S
15390 34 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  40400>.**.** Thi
153a0 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 61  s routine sets a
153b0 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
153c0 6f 6e 20 74 68 61 74 20 6d 69 67 68 74 20 62 65  on that might be
153d0 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65   invoked wheneve
153e0 72 0a 2a 2a 20 61 6e 20 61 74 74 65 6d 70 74 20  r.** an attempt 
153f0 69 73 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20  is made to open 
15400 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  a database table
15410 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68   that another th
15420 72 65 61 64 0a 2a 2a 20 6f 72 20 70 72 6f 63 65  read.** or proce
15430 73 73 20 68 61 73 20 6c 6f 63 6b 65 64 2e 0a 2a  ss has locked..*
15440 2a 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 73 79  *.** If the busy
15450 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55 4c   callback is NUL
15460 4c 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f  L, then [SQLITE_
15470 42 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45  BUSY] or [SQLITE
15480 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a  _IOERR_BLOCKED].
15490 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  ** is returned i
154a0 6d 6d 65 64 69 61 74 65 6c 79 20 75 70 6f 6e 20  mmediately upon 
154b0 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 74 68 65  encountering the
154c0 20 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 62 75   lock. If the bu
154d0 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69  sy callback.** i
154e0 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
154f0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 77 69   the callback wi
15500 6c 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 69  ll be invoked wi
15510 74 68 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73  th two arguments
15520 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  ..**.** The firs
15530 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  t argument to th
15540 65 20 68 61 6e 64 6c 65 72 20 69 73 20 61 20 63  e handler is a c
15550 6f 70 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a  opy of the void*
15560 20 70 6f 69 6e 74 65 72 20 77 68 69 63 68 0a 2a   pointer which.*
15570 2a 20 69 73 20 74 68 65 20 74 68 69 72 64 20 61  * is the third a
15580 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74  rgument to sqlit
15590 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28  e3_busy_handler(
155a0 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61  ).  The second a
155b0 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 68  rgument to.** th
155c0 65 20 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61  e handler callba
155d0 63 6b 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ck is the number
155e0 20 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 74   of times that t
155f0 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20  he busy handler 
15600 68 61 73 0a 2a 2a 20 62 65 65 6e 20 69 6e 76 6f  has.** been invo
15610 6b 65 64 20 66 6f 72 20 74 68 69 73 20 6c 6f 63  ked for this loc
15620 6b 69 6e 67 20 65 76 65 6e 74 2e 20 20 49 66 20  king event.  If 
15630 74 68 65 0a 2a 2a 20 62 75 73 79 20 63 61 6c 6c  the.** busy call
15640 62 61 63 6b 20 72 65 74 75 72 6e 73 20 30 2c 20  back returns 0, 
15650 74 68 65 6e 20 6e 6f 20 61 64 64 69 74 69 6f 6e  then no addition
15660 61 6c 20 61 74 74 65 6d 70 74 73 20 61 72 65 20  al attempts are 
15670 6d 61 64 65 20 74 6f 0a 2a 2a 20 61 63 63 65 73  made to.** acces
15680 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61  s the database a
15690 6e 64 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d  nd [SQLITE_BUSY]
156a0 20 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52   or [SQLITE_IOER
156b0 52 5f 42 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65  R_BLOCKED] is re
156c0 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 20 74 68  turned..** If th
156d0 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  e callback retur
156e0 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  ns non-zero, the
156f0 6e 20 61 6e 6f 74 68 65 72 20 61 74 74 65 6d 70  n another attemp
15700 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20  t.** is made to 
15710 6f 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73  open the databas
15720 65 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e  e for reading an
15730 64 20 74 68 65 20 63 79 63 6c 65 20 72 65 70 65  d the cycle repe
15740 61 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ats..**.** The p
15750 72 65 73 65 6e 63 65 20 6f 66 20 61 20 62 75 73  resence of a bus
15760 79 20 68 61 6e 64 6c 65 72 20 64 6f 65 73 20 6e  y handler does n
15770 6f 74 20 67 75 61 72 61 6e 74 65 65 20 74 68 61  ot guarantee tha
15780 74 20 69 74 20 77 69 6c 6c 20 62 65 20 69 6e 76  t it will be inv
15790 6f 6b 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65  oked.** when the
157a0 72 65 20 69 73 20 6c 6f 63 6b 20 63 6f 6e 74 65  re is lock conte
157b0 6e 74 69 6f 6e 2e 20 49 66 20 53 51 4c 69 74 65  ntion. If SQLite
157c0 20 64 65 74 65 72 6d 69 6e 65 73 20 74 68 61 74   determines that
157d0 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75   invoking the bu
157e0 73 79 0a 2a 2a 20 68 61 6e 64 6c 65 72 20 63 6f  sy.** handler co
157f0 75 6c 64 20 72 65 73 75 6c 74 20 69 6e 20 61 20  uld result in a 
15800 64 65 61 64 6c 6f 63 6b 2c 20 69 74 20 77 69 6c  deadlock, it wil
15810 6c 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 72  l go ahead and r
15820 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 42 55  eturn [SQLITE_BU
15830 53 59 5d 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54  SY].** or [SQLIT
15840 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d  E_IOERR_BLOCKED]
15850 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f   instead of invo
15860 6b 69 6e 67 20 74 68 65 20 62 75 73 79 20 68 61  king the busy ha
15870 6e 64 6c 65 72 2e 0a 2a 2a 20 43 6f 6e 73 69 64  ndler..** Consid
15880 65 72 20 61 20 73 63 65 6e 61 72 69 6f 20 77 68  er a scenario wh
15890 65 72 65 20 6f 6e 65 20 70 72 6f 63 65 73 73 20  ere one process 
158a0 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 61  is holding a rea
158b0 64 20 6c 6f 63 6b 20 74 68 61 74 0a 2a 2a 20 69  d lock that.** i
158c0 74 20 69 73 20 74 72 79 69 6e 67 20 74 6f 20 70  t is trying to p
158d0 72 6f 6d 6f 74 65 20 74 6f 20 61 20 72 65 73 65  romote to a rese
158e0 72 76 65 64 20 6c 6f 63 6b 20 61 6e 64 0a 2a 2a  rved lock and.**
158f0 20 61 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73   a second proces
15900 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72  s is holding a r
15910 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 74 68 61  eserved lock tha
15920 74 20 69 74 20 69 73 20 74 72 79 69 6e 67 0a 2a  t it is trying.*
15930 2a 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20  * to promote to 
15940 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
15950 6b 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 72  k.  The first pr
15960 6f 63 65 73 73 20 63 61 6e 6e 6f 74 20 70 72 6f  ocess cannot pro
15970 63 65 65 64 0a 2a 2a 20 62 65 63 61 75 73 65 20  ceed.** because 
15980 69 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79  it is blocked by
15990 20 74 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20   the second and 
159a0 74 68 65 20 73 65 63 6f 6e 64 20 70 72 6f 63 65  the second proce
159b0 73 73 20 63 61 6e 6e 6f 74 0a 2a 2a 20 70 72 6f  ss cannot.** pro
159c0 63 65 65 64 20 62 65 63 61 75 73 65 20 69 74 20  ceed because it 
159d0 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 74 68  is blocked by th
159e0 65 20 66 69 72 73 74 2e 20 20 49 66 20 62 6f 74  e first.  If bot
159f0 68 20 70 72 6f 63 65 73 73 65 73 0a 2a 2a 20 69  h processes.** i
15a00 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68  nvoke the busy h
15a10 61 6e 64 6c 65 72 73 2c 20 6e 65 69 74 68 65 72  andlers, neither
15a20 20 77 69 6c 6c 20 6d 61 6b 65 20 61 6e 79 20 70   will make any p
15a30 72 6f 67 72 65 73 73 2e 20 20 54 68 65 72 65 66  rogress.  Theref
15a40 6f 72 65 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 72  ore,.** SQLite r
15a50 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 42  eturns [SQLITE_B
15a60 55 53 59 5d 20 66 6f 72 20 74 68 65 20 66 69 72  USY] for the fir
15a70 73 74 20 70 72 6f 63 65 73 73 2c 20 68 6f 70 69  st process, hopi
15a80 6e 67 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20  ng that this.** 
15a90 77 69 6c 6c 20 69 6e 64 75 63 65 20 74 68 65 20  will induce the 
15aa0 66 69 72 73 74 20 70 72 6f 63 65 73 73 20 74 6f  first process to
15ab0 20 72 65 6c 65 61 73 65 20 69 74 73 20 72 65 61   release its rea
15ac0 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6c 6c 6f 77  d lock and allow
15ad0 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 70  .** the second p
15ae0 72 6f 63 65 73 73 20 74 6f 20 70 72 6f 63 65 65  rocess to procee
15af0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66  d..**.** The def
15b00 61 75 6c 74 20 62 75 73 79 20 63 61 6c 6c 62 61  ault busy callba
15b10 63 6b 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  ck is NULL..**.*
15b20 2a 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 42 55  * The [SQLITE_BU
15b30 53 59 5d 20 65 72 72 6f 72 20 69 73 20 63 6f 6e  SY] error is con
15b40 76 65 72 74 65 64 20 74 6f 20 5b 53 51 4c 49 54  verted to [SQLIT
15b50 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d  E_IOERR_BLOCKED]
15b60 0a 2a 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 20  .** when SQLite 
15b70 69 73 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  is in the middle
15b80 20 6f 66 20 61 20 6c 61 72 67 65 20 74 72 61 6e   of a large tran
15b90 73 61 63 74 69 6f 6e 20 77 68 65 72 65 20 61 6c  saction where al
15ba0 6c 20 74 68 65 0a 2a 2a 20 63 68 61 6e 67 65 73  l the.** changes
15bb0 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 69 6e   will not fit in
15bc0 74 6f 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  to the in-memory
15bd0 20 63 61 63 68 65 2e 20 20 53 51 4c 69 74 65 20   cache.  SQLite 
15be0 77 69 6c 6c 0a 2a 2a 20 61 6c 72 65 61 64 79 20  will.** already 
15bf0 68 6f 6c 64 20 61 20 52 45 53 45 52 56 45 44 20  hold a RESERVED 
15c00 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61  lock on the data
15c10 62 61 73 65 20 66 69 6c 65 2c 20 62 75 74 20 69  base file, but i
15c20 74 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 70 72  t needs.** to pr
15c30 6f 6d 6f 74 65 20 74 68 69 73 20 6c 6f 63 6b 20  omote this lock 
15c40 74 6f 20 45 58 43 4c 55 53 49 56 45 20 73 6f 20  to EXCLUSIVE so 
15c50 74 68 61 74 20 69 74 20 63 61 6e 20 73 70 69 6c  that it can spil
15c60 6c 20 63 61 63 68 65 0a 2a 2a 20 70 61 67 65 73  l cache.** pages
15c70 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61   into the databa
15c80 73 65 20 66 69 6c 65 20 77 69 74 68 6f 75 74 20  se file without 
15c90 68 61 72 6d 20 74 6f 20 63 6f 6e 63 75 72 72 65  harm to concurre
15ca0 6e 74 0a 2a 2a 20 72 65 61 64 65 72 73 2e 20 20  nt.** readers.  
15cb0 49 66 20 69 74 20 69 73 20 75 6e 61 62 6c 65 20  If it is unable 
15cc0 74 6f 20 70 72 6f 6d 6f 74 65 20 74 68 65 20 6c  to promote the l
15cd0 6f 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 69 6e  ock, then the in
15ce0 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 63 61 63 68 65  -memory.** cache
15cf0 20 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 69 6e   will be left in
15d00 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74   an inconsistent
15d10 20 73 74 61 74 65 20 61 6e 64 20 73 6f 20 74 68   state and so th
15d20 65 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20  e error.** code 
15d30 69 73 20 70 72 6f 6d 6f 74 65 64 20 66 72 6f 6d  is promoted from
15d40 20 74 68 65 20 72 65 6c 61 74 69 76 65 6c 79 20   the relatively 
15d50 62 65 6e 69 67 6e 20 5b 53 51 4c 49 54 45 5f 42  benign [SQLITE_B
15d60 55 53 59 5d 20 74 6f 0a 2a 2a 20 74 68 65 20 6d  USY] to.** the m
15d70 6f 72 65 20 73 65 76 65 72 65 20 5b 53 51 4c 49  ore severe [SQLI
15d80 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44  TE_IOERR_BLOCKED
15d90 5d 2e 20 20 54 68 69 73 20 65 72 72 6f 72 20 63  ].  This error c
15da0 6f 64 65 20 70 72 6f 6d 6f 74 69 6f 6e 0a 2a 2a  ode promotion.**
15db0 20 66 6f 72 63 65 73 20 61 6e 20 61 75 74 6f 6d   forces an autom
15dc0 61 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20 6f 66  atic rollback of
15dd0 20 74 68 65 20 63 68 61 6e 67 65 73 2e 20 20 53   the changes.  S
15de0 65 65 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65  ee the.** <a hre
15df0 66 3d 22 2f 63 76 73 74 72 61 63 2f 77 69 6b 69  f="/cvstrac/wiki
15e00 3f 70 3d 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c  ?p=CorruptionFol
15e10 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 6f 72 22  lowingBusyError"
15e20 3e 0a 2a 2a 20 43 6f 72 72 75 70 74 69 6f 6e 46  >.** CorruptionF
15e30 6f 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 6f  ollowingBusyErro
15e40 72 3c 2f 61 3e 20 77 69 6b 69 20 70 61 67 65 20  r</a> wiki page 
15e50 66 6f 72 20 61 20 64 69 73 63 75 73 73 69 6f 6e  for a discussion
15e60 20 6f 66 20 77 68 79 0a 2a 2a 20 74 68 69 73 20   of why.** this 
15e70 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 0a 2a 2a  is important..**
15e80 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e  .** There can on
15e90 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62  ly be a single b
15ea0 75 73 79 20 68 61 6e 64 6c 65 72 20 64 65 66 69  usy handler defi
15eb0 6e 65 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20  ned for each.** 
15ec0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
15ed0 74 69 6f 6e 5d 2e 20 20 53 65 74 74 69 6e 67 20  tion].  Setting 
15ee0 61 20 6e 65 77 20 62 75 73 79 20 68 61 6e 64 6c  a new busy handl
15ef0 65 72 20 63 6c 65 61 72 73 20 61 6e 79 0a 2a 2a  er clears any.**
15f00 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74 20   previously set 
15f10 68 61 6e 64 6c 65 72 2e 20 20 4e 6f 74 65 20 74  handler.  Note t
15f20 68 61 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c  hat calling [sql
15f30 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75  ite3_busy_timeou
15f40 74 28 29 5d 0a 2a 2a 20 77 69 6c 6c 20 61 6c 73  t()].** will als
15f50 6f 20 73 65 74 20 6f 72 20 63 6c 65 61 72 20 74  o set or clear t
15f60 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e  he busy handler.
15f70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 75 73 79 20  .**.** The busy 
15f80 63 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 20  callback should 
15f90 6e 6f 74 20 74 61 6b 65 20 61 6e 79 20 61 63 74  not take any act
15fa0 69 6f 6e 73 20 77 68 69 63 68 20 6d 6f 64 69 66  ions which modif
15fb0 79 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  y the.** databas
15fc0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61  e connection tha
15fd0 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75  t invoked the bu
15fe0 73 79 20 68 61 6e 64 6c 65 72 2e 20 20 41 6e 79  sy handler.  Any
15ff0 20 73 75 63 68 20 61 63 74 69 6f 6e 73 0a 2a 2a   such actions.**
16000 20 72 65 73 75 6c 74 20 69 6e 20 75 6e 64 65 66   result in undef
16010 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a  ined behavior..*
16020 2a 20 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  * .** Requiremen
16030 74 73 3a 0a 2a 2a 20 5b 48 31 32 33 31 31 5d 20  ts:.** [H12311] 
16040 5b 48 31 32 33 31 32 5d 20 5b 48 31 32 33 31 34  [H12312] [H12314
16050 5d 20 5b 48 31 32 33 31 36 5d 20 5b 48 31 32 33  ] [H12316] [H123
16060 31 38 5d 0a 2a 2a 0a 2a 2a 20 41 20 62 75 73 79  18].**.** A busy
16070 20 68 61 6e 64 6c 65 72 20 6d 75 73 74 20 6e 6f   handler must no
16080 74 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74 61  t close the data
16090 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  base connection.
160a0 2a 2a 20 6f 72 20 5b 70 72 65 70 61 72 65 64 20  ** or [prepared 
160b0 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20  statement] that 
160c0 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 73 79  invoked the busy
160d0 20 68 61 6e 64 6c 65 72 2e 0a 2a 2f 0a 53 51 4c   handler..*/.SQL
160e0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
160f0 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72  te3_busy_handler
16100 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a  (sqlite3*, int(*
16110 29 28 76 6f 69 64 2a 2c 69 6e 74 29 2c 20 76 6f  )(void*,int), vo
16120 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  id*);../*.** CAP
16130 49 33 52 45 46 3a 20 53 65 74 20 41 20 42 75 73  I3REF: Set A Bus
16140 79 20 54 69 6d 65 6f 75 74 20 7b 48 31 32 33 34  y Timeout {H1234
16150 30 7d 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a  0} <S40410>.**.*
16160 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
16170 65 74 73 20 61 20 5b 73 71 6c 69 74 65 33 5f 62  ets a [sqlite3_b
16180 75 73 79 5f 68 61 6e 64 6c 65 72 20 7c 20 62 75  usy_handler | bu
16190 73 79 20 68 61 6e 64 6c 65 72 5d 20 74 68 61 74  sy handler] that
161a0 20 73 6c 65 65 70 73 0a 2a 2a 20 66 6f 72 20 61   sleeps.** for a
161b0 20 73 70 65 63 69 66 69 65 64 20 61 6d 6f 75 6e   specified amoun
161c0 74 20 6f 66 20 74 69 6d 65 20 77 68 65 6e 20 61  t of time when a
161d0 20 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64   table is locked
161e0 2e 20 20 54 68 65 20 68 61 6e 64 6c 65 72 0a 2a  .  The handler.*
161f0 2a 20 77 69 6c 6c 20 73 6c 65 65 70 20 6d 75 6c  * will sleep mul
16200 74 69 70 6c 65 20 74 69 6d 65 73 20 75 6e 74 69  tiple times unti
16210 6c 20 61 74 20 6c 65 61 73 74 20 22 6d 73 22 20  l at least "ms" 
16220 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20  milliseconds of 
16230 73 6c 65 65 70 69 6e 67 0a 2a 2a 20 68 61 76 65  sleeping.** have
16240 20 61 63 63 75 6d 75 6c 61 74 65 64 2e 20 7b 48   accumulated. {H
16250 31 32 33 34 33 7d 20 41 66 74 65 72 20 22 6d 73  12343} After "ms
16260 22 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 6f  " milliseconds o
16270 66 20 73 6c 65 65 70 69 6e 67 2c 0a 2a 2a 20 74  f sleeping,.** t
16280 68 65 20 68 61 6e 64 6c 65 72 20 72 65 74 75 72  he handler retur
16290 6e 73 20 30 20 77 68 69 63 68 20 63 61 75 73 65  ns 0 which cause
162a0 73 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  s [sqlite3_step(
162b0 29 5d 20 74 6f 20 72 65 74 75 72 6e 0a 2a 2a 20  )] to return.** 
162c0 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f 72  [SQLITE_BUSY] or
162d0 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42   [SQLITE_IOERR_B
162e0 4c 4f 43 4b 45 44 5d 2e 0a 2a 2a 0a 2a 2a 20 43  LOCKED]..**.** C
162f0 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74  alling this rout
16300 69 6e 65 20 77 69 74 68 20 61 6e 20 61 72 67 75  ine with an argu
16310 6d 65 6e 74 20 6c 65 73 73 20 74 68 61 6e 20 6f  ment less than o
16320 72 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 0a  r equal to zero.
16330 2a 2a 20 74 75 72 6e 73 20 6f 66 66 20 61 6c 6c  ** turns off all
16340 20 62 75 73 79 20 68 61 6e 64 6c 65 72 73 2e 0a   busy handlers..
16350 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 63 61 6e 20  **.** There can 
16360 6f 6e 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65  only be a single
16370 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66 6f   busy handler fo
16380 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a  r a particular.*
16390 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  * [database conn
163a0 65 63 74 69 6f 6e 5d 20 61 6e 79 20 61 6e 79 20  ection] any any 
163b0 67 69 76 65 6e 20 6d 6f 6d 65 6e 74 2e 20 20 49  given moment.  I
163c0 66 20 61 6e 6f 74 68 65 72 20 62 75 73 79 20 68  f another busy h
163d0 61 6e 64 6c 65 72 0a 2a 2a 20 77 61 73 20 64 65  andler.** was de
163e0 66 69 6e 65 64 20 20 28 75 73 69 6e 67 20 5b 73  fined  (using [s
163f0 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64  qlite3_busy_hand
16400 6c 65 72 28 29 5d 29 20 70 72 69 6f 72 20 74 6f  ler()]) prior to
16410 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73   calling.** this
16420 20 72 6f 75 74 69 6e 65 2c 20 74 68 61 74 20 6f   routine, that o
16430 74 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c 65  ther busy handle
16440 72 20 69 73 20 63 6c 65 61 72 65 64 2e 0a 2a 2a  r is cleared..**
16450 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
16460 3a 0a 2a 2a 20 5b 48 31 32 33 34 31 5d 20 5b 48  :.** [H12341] [H
16470 31 32 33 34 33 5d 20 5b 48 31 32 33 34 34 5d 0a  12343] [H12344].
16480 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
16490 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74  t sqlite3_busy_t
164a0 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33 2a 2c  imeout(sqlite3*,
164b0 20 69 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a 2a 2a   int ms);../*.**
164c0 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e 76 65   CAPI3REF: Conve
164d0 6e 69 65 6e 63 65 20 52 6f 75 74 69 6e 65 73 20  nience Routines 
164e0 46 6f 72 20 52 75 6e 6e 69 6e 67 20 51 75 65 72  For Running Quer
164f0 69 65 73 20 7b 48 31 32 33 37 30 7d 20 3c 53 31  ies {H12370} <S1
16500 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 44 65 66 69  0000>.**.** Defi
16510 6e 69 74 69 6f 6e 3a 20 41 20 3c 62 3e 72 65 73  nition: A <b>res
16520 75 6c 74 20 74 61 62 6c 65 3c 2f 62 3e 20 69 73  ult table</b> is
16530 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 74 72   memory data str
16540 75 63 74 75 72 65 20 63 72 65 61 74 65 64 20 62  ucture created b
16550 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65  y the.** [sqlite
16560 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 20 69  3_get_table()] i
16570 6e 74 65 72 66 61 63 65 2e 20 20 41 20 72 65 73  nterface.  A res
16580 75 6c 74 20 74 61 62 6c 65 20 72 65 63 6f 72 64  ult table record
16590 73 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74  s the.** complet
165a0 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 20  e query results 
165b0 66 72 6f 6d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  from one or more
165c0 20 71 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a 2a 20   queries..**.** 
165d0 54 68 65 20 74 61 62 6c 65 20 63 6f 6e 63 65 70  The table concep
165e0 74 75 61 6c 6c 79 20 68 61 73 20 61 20 6e 75 6d  tually has a num
165f0 62 65 72 20 6f 66 20 72 6f 77 73 20 61 6e 64 20  ber of rows and 
16600 63 6f 6c 75 6d 6e 73 2e 20 20 42 75 74 0a 2a 2a  columns.  But.**
16610 20 74 68 65 73 65 20 6e 75 6d 62 65 72 73 20 61   these numbers a
16620 72 65 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74  re not part of t
16630 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20  he result table 
16640 69 74 73 65 6c 66 2e 20 20 54 68 65 73 65 0a 2a  itself.  These.*
16650 2a 20 6e 75 6d 62 65 72 73 20 61 72 65 20 6f 62  * numbers are ob
16660 74 61 69 6e 65 64 20 73 65 70 61 72 61 74 65 6c  tained separatel
16670 79 2e 20 20 4c 65 74 20 4e 20 62 65 20 74 68 65  y.  Let N be the
16680 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 0a   number of rows.
16690 2a 2a 20 61 6e 64 20 4d 20 62 65 20 74 68 65 20  ** and M be the 
166a0 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
166b0 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 75 6c  s..**.** A resul
166c0 74 20 74 61 62 6c 65 20 69 73 20 61 6e 20 61 72  t table is an ar
166d0 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20  ray of pointers 
166e0 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  to zero-terminat
166f0 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73  ed UTF-8 strings
16700 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 28  ..** There are (
16710 4e 2b 31 29 2a 4d 20 65 6c 65 6d 65 6e 74 73 20  N+1)*M elements 
16720 69 6e 20 74 68 65 20 61 72 72 61 79 2e 20 20 54  in the array.  T
16730 68 65 20 66 69 72 73 74 20 4d 20 70 6f 69 6e 74  he first M point
16740 65 72 73 20 70 6f 69 6e 74 0a 2a 2a 20 74 6f 20  ers point.** to 
16750 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20  zero-terminated 
16760 73 74 72 69 6e 67 73 20 74 68 61 74 20 20 63 6f  strings that  co
16770 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d 65 73 20  ntain the names 
16780 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 2e 0a  of the columns..
16790 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e 67  ** The remaining
167a0 20 65 6e 74 72 69 65 73 20 61 6c 6c 20 70 6f 69   entries all poi
167b0 6e 74 20 74 6f 20 71 75 65 72 79 20 72 65 73 75  nt to query resu
167c0 6c 74 73 2e 20 20 4e 55 4c 4c 20 76 61 6c 75 65  lts.  NULL value
167d0 73 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 4e  s result.** in N
167e0 55 4c 4c 20 70 6f 69 6e 74 65 72 73 2e 20 20 41  ULL pointers.  A
167f0 6c 6c 20 6f 74 68 65 72 20 76 61 6c 75 65 73 20  ll other values 
16800 61 72 65 20 69 6e 20 74 68 65 69 72 20 55 54 46  are in their UTF
16810 2d 38 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  -8 zero-terminat
16820 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 72 65 70  ed.** string rep
16830 72 65 73 65 6e 74 61 74 69 6f 6e 20 61 73 20 72  resentation as r
16840 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69  eturned by [sqli
16850 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28  te3_column_text(
16860 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73 75  )]..**.** A resu
16870 6c 74 20 74 61 62 6c 65 20 6d 69 67 68 74 20 63  lt table might c
16880 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20 6f 72  onsist of one or
16890 20 6d 6f 72 65 20 6d 65 6d 6f 72 79 20 61 6c 6c   more memory all
168a0 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2a 20 49 74 20  ocations..** It 
168b0 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20 70  is not safe to p
168c0 61 73 73 20 61 20 72 65 73 75 6c 74 20 74 61 62  ass a result tab
168d0 6c 65 20 64 69 72 65 63 74 6c 79 20 74 6f 20 5b  le directly to [
168e0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e  sqlite3_free()].
168f0 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74 61 62  .** A result tab
16900 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 64 65 61  le should be dea
16910 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 5b  llocated using [
16920 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62  sqlite3_free_tab
16930 6c 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 73 20  le()]..**.** As 
16940 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 74 68  an example of th
16950 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 66  e result table f
16960 6f 72 6d 61 74 2c 20 73 75 70 70 6f 73 65 20 61  ormat, suppose a
16970 20 71 75 65 72 79 20 72 65 73 75 6c 74 0a 2a 2a   query result.**
16980 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a   is as follows:.
16990 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74  **.** <blockquot
169a0 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20 20 20  e><pre>.**      
169b0 20 20 4e 61 6d 65 20 20 20 20 20 20 20 20 7c 20    Name        | 
169c0 41 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 2d 2d  Age.**        --
169d0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
169e0 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 20 20  -----.**        
169f0 41 6c 69 63 65 20 20 20 20 20 20 20 7c 20 34 33  Alice       | 43
16a00 0a 2a 2a 20 20 20 20 20 20 20 20 42 6f 62 20 20  .**        Bob  
16a10 20 20 20 20 20 20 20 7c 20 32 38 0a 2a 2a 20 20         | 28.**  
16a20 20 20 20 20 20 20 43 69 6e 64 79 20 20 20 20 20        Cindy     
16a30 20 20 7c 20 32 31 0a 2a 2a 20 3c 2f 70 72 65 3e    | 21.** </pre>
16a40 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a  </blockquote>.**
16a50 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 77  .** There are tw
16a60 6f 20 63 6f 6c 75 6d 6e 20 28 4d 3d 3d 32 29 20  o column (M==2) 
16a70 61 6e 64 20 74 68 72 65 65 20 72 6f 77 73 20 28  and three rows (
16a80 4e 3d 3d 33 29 2e 20 20 54 68 75 73 20 74 68 65  N==3).  Thus the
16a90 0a 2a 2a 20 72 65 73 75 6c 74 20 74 61 62 6c 65  .** result table
16aa0 20 68 61 73 20 38 20 65 6e 74 72 69 65 73 2e 20   has 8 entries. 
16ab0 20 53 75 70 70 6f 73 65 20 74 68 65 20 72 65 73   Suppose the res
16ac0 75 6c 74 20 74 61 62 6c 65 20 69 73 20 73 74 6f  ult table is sto
16ad0 72 65 64 0a 2a 2a 20 69 6e 20 61 6e 20 61 72 72  red.** in an arr
16ae0 61 79 20 6e 61 6d 65 73 20 61 7a 52 65 73 75 6c  ay names azResul
16af0 74 2e 20 20 54 68 65 6e 20 61 7a 52 65 73 75 6c  t.  Then azResul
16b00 74 20 68 6f 6c 64 73 20 74 68 69 73 20 63 6f 6e  t holds this con
16b10 74 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f  tent:.**.** <blo
16b20 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a  ckquote><pre>.**
16b30 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74          azResult
16b40 26 23 39 31 3b 30 5d 20 3d 20 22 4e 61 6d 65 22  &#91;0] = "Name"
16b50 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65  ;.**        azRe
16b60 73 75 6c 74 26 23 39 31 3b 31 5d 20 3d 20 22 41  sult&#91;1] = "A
16b70 67 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61  ge";.**        a
16b80 7a 52 65 73 75 6c 74 26 23 39 31 3b 32 5d 20 3d  zResult&#91;2] =
16b90 20 22 41 6c 69 63 65 22 3b 0a 2a 2a 20 20 20 20   "Alice";.**    
16ba0 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31      azResult&#91
16bb0 3b 33 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a 20 20  ;3] = "43";.**  
16bc0 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23        azResult&#
16bd0 39 31 3b 34 5d 20 3d 20 22 42 6f 62 22 3b 0a 2a  91;4] = "Bob";.*
16be0 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c  *        azResul
16bf0 74 26 23 39 31 3b 35 5d 20 3d 20 22 32 38 22 3b  t&#91;5] = "28";
16c00 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73  .**        azRes
16c10 75 6c 74 26 23 39 31 3b 36 5d 20 3d 20 22 43 69  ult&#91;6] = "Ci
16c20 6e 64 79 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20  ndy";.**        
16c30 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 37 5d 20  azResult&#91;7] 
16c40 3d 20 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70 72 65  = "21";.** </pre
16c50 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a  ></blockquote>.*
16c60 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
16c70 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 66 75 6e  _get_table() fun
16c80 63 74 69 6f 6e 20 65 76 61 6c 75 61 74 65 73 20  ction evaluates 
16c90 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 73  one or more.** s
16ca0 65 6d 69 63 6f 6c 6f 6e 2d 73 65 70 61 72 61 74  emicolon-separat
16cb0 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ed SQL statement
16cc0 73 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d 74 65  s in the zero-te
16cd0 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 0a 2a  rminated UTF-8.*
16ce0 2a 20 73 74 72 69 6e 67 20 6f 66 20 69 74 73 20  * string of its 
16cf0 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20  2nd parameter.  
16d00 49 74 20 72 65 74 75 72 6e 73 20 61 20 72 65 73  It returns a res
16d10 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 74 68 65  ult table to the
16d20 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 67 69 76 65  .** pointer give
16d30 6e 20 69 6e 20 69 74 73 20 33 72 64 20 70 61 72  n in its 3rd par
16d40 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 66  ameter..**.** Af
16d50 74 65 72 20 74 68 65 20 63 61 6c 6c 69 6e 67 20  ter the calling 
16d60 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 66 69 6e  function has fin
16d70 69 73 68 65 64 20 75 73 69 6e 67 20 74 68 65 20  ished using the 
16d80 72 65 73 75 6c 74 2c 20 69 74 20 73 68 6f 75 6c  result, it shoul
16d90 64 0a 2a 2a 20 70 61 73 73 20 74 68 65 20 70 6f  d.** pass the po
16da0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 65 73  inter to the res
16db0 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 73 71 6c  ult table to sql
16dc0 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28  ite3_free_table(
16dd0 29 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a  ) in order to.**
16de0 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d 65 6d   release the mem
16df0 6f 72 79 20 74 68 61 74 20 77 61 73 20 6d 61 6c  ory that was mal
16e00 6c 6f 63 65 64 2e 20 20 42 65 63 61 75 73 65 20  loced.  Because 
16e10 6f 66 20 74 68 65 20 77 61 79 20 74 68 65 0a 2a  of the way the.*
16e20 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  * [sqlite3_mallo
16e30 63 28 29 5d 20 68 61 70 70 65 6e 73 20 77 69 74  c()] happens wit
16e40 68 69 6e 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  hin sqlite3_get_
16e50 74 61 62 6c 65 28 29 2c 20 74 68 65 20 63 61 6c  table(), the cal
16e60 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  ling.** function
16e70 20 6d 75 73 74 20 6e 6f 74 20 74 72 79 20 74 6f   must not try to
16e80 20 63 61 6c 6c 20 5b 73 71 6c 69 74 65 33 5f 66   call [sqlite3_f
16e90 72 65 65 28 29 5d 20 64 69 72 65 63 74 6c 79 2e  ree()] directly.
16ea0 20 20 4f 6e 6c 79 0a 2a 2a 20 5b 73 71 6c 69 74    Only.** [sqlit
16eb0 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 5d  e3_free_table()]
16ec0 20 69 73 20 61 62 6c 65 20 74 6f 20 72 65 6c 65   is able to rele
16ed0 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 70  ase the memory p
16ee0 72 6f 70 65 72 6c 79 20 61 6e 64 20 73 61 66 65  roperly and safe
16ef0 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  ly..**.** The sq
16f00 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28  lite3_get_table(
16f10 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 69  ) interface is i
16f20 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 61 20  mplemented as a 
16f30 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64 0a 2a  wrapper around.*
16f40 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28  * [sqlite3_exec(
16f50 29 5d 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  )].  The sqlite3
16f60 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 72 6f 75  _get_table() rou
16f70 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 68 61  tine does not ha
16f80 76 65 20 61 63 63 65 73 73 0a 2a 2a 20 74 6f 20  ve access.** to 
16f90 61 6e 79 20 69 6e 74 65 72 6e 61 6c 20 64 61 74  any internal dat
16fa0 61 20 73 74 72 75 63 74 75 72 65 73 20 6f 66 20  a structures of 
16fb0 53 51 4c 69 74 65 2e 20 20 49 74 20 75 73 65 73  SQLite.  It uses
16fc0 20 6f 6e 6c 79 20 74 68 65 20 70 75 62 6c 69 63   only the public
16fd0 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 64 65  .** interface de
16fe0 66 69 6e 65 64 20 68 65 72 65 2e 20 20 41 73 20  fined here.  As 
16ff0 61 20 63 6f 6e 73 65 71 75 65 6e 63 65 2c 20 65  a consequence, e
17000 72 72 6f 72 73 20 74 68 61 74 20 6f 63 63 75 72  rrors that occur
17010 20 69 6e 20 74 68 65 0a 2a 2a 20 77 72 61 70 70   in the.** wrapp
17020 65 72 20 6c 61 79 65 72 20 6f 75 74 73 69 64 65  er layer outside
17030 20 6f 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c   of the internal
17040 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29   [sqlite3_exec()
17050 5d 20 63 61 6c 6c 20 61 72 65 20 6e 6f 74 0a 2a  ] call are not.*
17060 2a 20 72 65 66 6c 65 63 74 65 64 20 69 6e 20 73  * reflected in s
17070 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20  ubsequent calls 
17080 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63  to [sqlite3_errc
17090 6f 64 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74  ode()] or [sqlit
170a0 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a  e3_errmsg()]..**
170b0 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
170c0 3a 0a 2a 2a 20 5b 48 31 32 33 37 31 5d 20 5b 48  :.** [H12371] [H
170d0 31 32 33 37 33 5d 20 5b 48 31 32 33 37 34 5d 20  12373] [H12374] 
170e0 5b 48 31 32 33 37 36 5d 20 5b 48 31 32 33 37 39  [H12376] [H12379
170f0 5d 20 5b 48 31 32 33 38 32 5d 0a 2a 2f 0a 53 51  ] [H12382].*/.SQ
17100 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
17110 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 0a  ite3_get_table(.
17120 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
17130 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 70          /* An op
17140 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  en database */. 
17150 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71   const char *zSq
17160 6c 2c 20 20 20 20 20 2f 2a 20 53 51 4c 20 74 6f  l,     /* SQL to
17170 20 62 65 20 65 76 61 6c 75 61 74 65 64 20 2a 2f   be evaluated */
17180 0a 20 20 63 68 61 72 20 2a 2a 2a 70 61 7a 52 65  .  char ***pazRe
17190 73 75 6c 74 2c 20 20 20 20 2f 2a 20 52 65 73 75  sult,    /* Resu
171a0 6c 74 73 20 6f 66 20 74 68 65 20 71 75 65 72 79  lts of the query
171b0 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52 6f 77   */.  int *pnRow
171c0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ,           /* N
171d0 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74 20  umber of result 
171e0 72 6f 77 73 20 77 72 69 74 74 65 6e 20 68 65 72  rows written her
171f0 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43 6f  e */.  int *pnCo
17200 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20  lumn,        /* 
17210 4e 75 6d 62 65 72 20 6f 66 20 72 65 73 75 6c 74  Number of result
17220 20 63 6f 6c 75 6d 6e 73 20 77 72 69 74 74 65 6e   columns written
17230 20 68 65 72 65 20 2a 2f 0a 20 20 63 68 61 72 20   here */.  char 
17240 2a 2a 70 7a 45 72 72 6d 73 67 20 20 20 20 20 20  **pzErrmsg      
17250 20 2f 2a 20 45 72 72 6f 72 20 6d 73 67 20 77 72   /* Error msg wr
17260 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 3b  itten here */.);
17270 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
17280 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61   sqlite3_free_ta
17290 62 6c 65 28 63 68 61 72 20 2a 2a 72 65 73 75 6c  ble(char **resul
172a0 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  t);../*.** CAPI3
172b0 52 45 46 3a 20 46 6f 72 6d 61 74 74 65 64 20 53  REF: Formatted S
172c0 74 72 69 6e 67 20 50 72 69 6e 74 69 6e 67 20 46  tring Printing F
172d0 75 6e 63 74 69 6f 6e 73 20 7b 48 31 37 34 30 30  unctions {H17400
172e0 7d 20 3c 53 37 30 30 30 30 3e 3c 53 32 30 30 30  } <S70000><S2000
172f0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  0>.**.** These r
17300 6f 75 74 69 6e 65 73 20 61 72 65 20 77 6f 72 6b  outines are work
17310 2d 61 6c 69 6b 65 73 20 6f 66 20 74 68 65 20 22  -alikes of the "
17320 70 72 69 6e 74 66 28 29 22 20 66 61 6d 69 6c 79  printf()" family
17330 20 6f 66 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a   of functions.**
17340 20 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64 61   from the standa
17350 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a  rd C library..**
17360 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
17370 6d 70 72 69 6e 74 66 28 29 20 61 6e 64 20 73 71  mprintf() and sq
17380 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 29  lite3_vmprintf()
17390 20 72 6f 75 74 69 6e 65 73 20 77 72 69 74 65 20   routines write 
173a0 74 68 65 69 72 0a 2a 2a 20 72 65 73 75 6c 74 73  their.** results
173b0 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74   into memory obt
173c0 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69  ained from [sqli
173d0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a 2a  te3_malloc()]..*
173e0 2a 20 54 68 65 20 73 74 72 69 6e 67 73 20 72 65  * The strings re
173f0 74 75 72 6e 65 64 20 62 79 20 74 68 65 73 65 20  turned by these 
17400 74 77 6f 20 72 6f 75 74 69 6e 65 73 20 73 68 6f  two routines sho
17410 75 6c 64 20 62 65 0a 2a 2a 20 72 65 6c 65 61 73  uld be.** releas
17420 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 66  ed by [sqlite3_f
17430 72 65 65 28 29 5d 2e 20 20 42 6f 74 68 20 72 6f  ree()].  Both ro
17440 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 61 0a  utines return a.
17450 2a 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20  ** NULL pointer 
17460 69 66 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  if [sqlite3_mall
17470 6f 63 28 29 5d 20 69 73 20 75 6e 61 62 6c 65 20  oc()] is unable 
17480 74 6f 20 61 6c 6c 6f 63 61 74 65 20 65 6e 6f 75  to allocate enou
17490 67 68 0a 2a 2a 20 6d 65 6d 6f 72 79 20 74 6f 20  gh.** memory to 
174a0 68 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 69  hold the resulti
174b0 6e 67 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a  ng string..**.**
174c0 20 49 6e 20 73 71 6c 69 74 65 33 5f 73 6e 70 72   In sqlite3_snpr
174d0 69 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 20 69  intf() routine i
174e0 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 22 73 6e  s similar to "sn
174f0 70 72 69 6e 74 66 28 29 22 20 66 72 6f 6d 0a 2a  printf()" from.*
17500 2a 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43  * the standard C
17510 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65 20 72   library.  The r
17520 65 73 75 6c 74 20 69 73 20 77 72 69 74 74 65 6e  esult is written
17530 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 62 75 66   into the.** buf
17540 66 65 72 20 73 75 70 70 6c 69 65 64 20 61 73 20  fer supplied as 
17550 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  the second param
17560 65 74 65 72 20 77 68 6f 73 65 20 73 69 7a 65 20  eter whose size 
17570 69 73 20 67 69 76 65 6e 20 62 79 0a 2a 2a 20 74  is given by.** t
17580 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
17590 65 72 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68  er. Note that th
175a0 65 20 6f 72 64 65 72 20 6f 66 20 74 68 65 0a 2a  e order of the.*
175b0 2a 20 66 69 72 73 74 20 74 77 6f 20 70 61 72 61  * first two para
175c0 6d 65 74 65 72 73 20 69 73 20 72 65 76 65 72 73  meters is revers
175d0 65 64 20 66 72 6f 6d 20 73 6e 70 72 69 6e 74 66  ed from snprintf
175e0 28 29 2e 20 20 54 68 69 73 20 69 73 20 61 6e 0a  ().  This is an.
175f0 2a 2a 20 68 69 73 74 6f 72 69 63 61 6c 20 61 63  ** historical ac
17600 63 69 64 65 6e 74 20 74 68 61 74 20 63 61 6e 6e  cident that cann
17610 6f 74 20 62 65 20 66 69 78 65 64 20 77 69 74 68  ot be fixed with
17620 6f 75 74 20 62 72 65 61 6b 69 6e 67 0a 2a 2a 20  out breaking.** 
17630 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74  backwards compat
17640 69 62 69 6c 69 74 79 2e 20 20 4e 6f 74 65 20 61  ibility.  Note a
17650 6c 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33  lso that sqlite3
17660 5f 73 6e 70 72 69 6e 74 66 28 29 0a 2a 2a 20 72  _snprintf().** r
17670 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
17680 20 74 6f 20 69 74 73 20 62 75 66 66 65 72 20 69   to its buffer i
17690 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6e 75  nstead of the nu
176a0 6d 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 72 61  mber of.** chara
176b0 63 74 65 72 73 20 61 63 74 75 61 6c 6c 79 20 77  cters actually w
176c0 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20  ritten into the 
176d0 62 75 66 66 65 72 2e 20 20 57 65 20 61 64 6d 69  buffer.  We admi
176e0 74 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 6e 75  t that.** the nu
176f0 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65  mber of characte
17700 72 73 20 77 72 69 74 74 65 6e 20 77 6f 75 6c 64  rs written would
17710 20 62 65 20 61 20 6d 6f 72 65 20 75 73 65 66 75   be a more usefu
17720 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75  l return.** valu
17730 65 20 62 75 74 20 77 65 20 63 61 6e 6e 6f 74 20  e but we cannot 
17740 63 68 61 6e 67 65 20 74 68 65 20 69 6d 70 6c 65  change the imple
17750 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c  mentation of sql
17760 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 0a  ite3_snprintf().
17770 2a 2a 20 6e 6f 77 20 77 69 74 68 6f 75 74 20 62  ** now without b
17780 72 65 61 6b 69 6e 67 20 63 6f 6d 70 61 74 69 62  reaking compatib
17790 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 41 73 20  ility..**.** As 
177a0 6c 6f 6e 67 20 61 73 20 74 68 65 20 62 75 66 66  long as the buff
177b0 65 72 20 73 69 7a 65 20 69 73 20 67 72 65 61 74  er size is great
177c0 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c 20 73 71  er than zero, sq
177d0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29  lite3_snprintf()
177e0 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 73 20 74  .** guarantees t
177f0 68 61 74 20 74 68 65 20 62 75 66 66 65 72 20 69  hat the buffer i
17800 73 20 61 6c 77 61 79 73 20 7a 65 72 6f 2d 74 65  s always zero-te
17810 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 65 20 66  rminated.  The f
17820 69 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65  irst.** paramete
17830 72 20 22 6e 22 20 69 73 20 74 68 65 20 74 6f 74  r "n" is the tot
17840 61 6c 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62  al size of the b
17850 75 66 66 65 72 2c 20 69 6e 63 6c 75 64 69 6e 67  uffer, including
17860 20 73 70 61 63 65 20 66 6f 72 0a 2a 2a 20 74 68   space for.** th
17870 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f  e zero terminato
17880 72 2e 20 20 53 6f 20 74 68 65 20 6c 6f 6e 67 65  r.  So the longe
17890 73 74 20 73 74 72 69 6e 67 20 74 68 61 74 20 63  st string that c
178a0 61 6e 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79  an be completely
178b0 0a 2a 2a 20 77 72 69 74 74 65 6e 20 77 69 6c 6c  .** written will
178c0 20 62 65 20 6e 2d 31 20 63 68 61 72 61 63 74 65   be n-1 characte
178d0 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  rs..**.** These 
178e0 72 6f 75 74 69 6e 65 73 20 61 6c 6c 20 69 6d 70  routines all imp
178f0 6c 65 6d 65 6e 74 20 73 6f 6d 65 20 61 64 64 69  lement some addi
17900 74 69 6f 6e 61 6c 20 66 6f 72 6d 61 74 74 69 6e  tional formattin
17910 67 0a 2a 2a 20 6f 70 74 69 6f 6e 73 20 74 68 61  g.** options tha
17920 74 20 61 72 65 20 75 73 65 66 75 6c 20 66 6f 72  t are useful for
17930 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 53 51   constructing SQ
17940 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a  L statements..**
17950 20 41 6c 6c 20 6f 66 20 74 68 65 20 75 73 75 61   All of the usua
17960 6c 20 70 72 69 6e 74 66 28 29 20 66 6f 72 6d 61  l printf() forma
17970 74 74 69 6e 67 20 6f 70 74 69 6f 6e 73 20 61 70  tting options ap
17980 70 6c 79 2e 20 20 49 6e 20 61 64 64 69 74 69 6f  ply.  In additio
17990 6e 2c 20 74 68 65 72 65 0a 2a 2a 20 69 73 20 61  n, there.** is a
179a0 72 65 20 22 25 71 22 2c 20 22 25 51 22 2c 20 61  re "%q", "%Q", a
179b0 6e 64 20 22 25 7a 22 20 6f 70 74 69 6f 6e 73 2e  nd "%z" options.
179c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 25 71 20 6f 70  .**.** The %q op
179d0 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65 20  tion works like 
179e0 25 73 20 69 6e 20 74 68 61 74 20 69 74 20 73 75  %s in that it su
179f0 62 73 74 69 74 75 74 65 73 20 61 20 6e 75 6c 6c  bstitutes a null
17a00 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 73  -terminated.** s
17a10 74 72 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 61  tring from the a
17a20 72 67 75 6d 65 6e 74 20 6c 69 73 74 2e 20 20 42  rgument list.  B
17a30 75 74 20 25 71 20 61 6c 73 6f 20 64 6f 75 62 6c  ut %q also doubl
17a40 65 73 20 65 76 65 72 79 20 27 5c 27 27 20 63 68  es every '\'' ch
17a50 61 72 61 63 74 65 72 2e 0a 2a 2a 20 25 71 20 69  aracter..** %q i
17a60 73 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75  s designed for u
17a70 73 65 20 69 6e 73 69 64 65 20 61 20 73 74 72 69  se inside a stri
17a80 6e 67 20 6c 69 74 65 72 61 6c 2e 20 20 42 79 20  ng literal.  By 
17a90 64 6f 75 62 6c 69 6e 67 20 65 61 63 68 20 27 5c  doubling each '\
17aa0 27 27 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 20  ''.** character 
17ab0 69 74 20 65 73 63 61 70 65 73 20 74 68 61 74 20  it escapes that 
17ac0 63 68 61 72 61 63 74 65 72 20 61 6e 64 20 61 6c  character and al
17ad0 6c 6f 77 73 20 69 74 20 74 6f 20 62 65 20 69 6e  lows it to be in
17ae0 73 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20 74  serted into.** t
17af0 68 65 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a  he string..**.**
17b00 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 73   For example, as
17b10 73 75 6d 65 20 74 68 65 20 73 74 72 69 6e 67 20  sume the string 
17b20 76 61 72 69 61 62 6c 65 20 7a 54 65 78 74 20 63  variable zText c
17b30 6f 6e 74 61 69 6e 73 20 74 65 78 74 20 61 73 20  ontains text as 
17b40 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c  follows:.**.** <
17b50 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e  blockquote><pre>
17b60 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 54 65 78 74  .**  char *zText
17b70 20 3d 20 22 49 74 27 73 20 61 20 68 61 70 70 79   = "It's a happy
17b80 20 64 61 79 21 22 3b 0a 2a 2a 20 3c 2f 70 72 65   day!";.** </pre
17b90 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a  ></blockquote>.*
17ba0 2a 0a 2a 2a 20 4f 6e 65 20 63 61 6e 20 75 73 65  *.** One can use
17bb0 20 74 68 69 73 20 74 65 78 74 20 69 6e 20 61 6e   this text in an
17bc0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61   SQL statement a
17bd0 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
17be0 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72   <blockquote><pr
17bf0 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 53 51  e>.**  char *zSQ
17c00 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  L = sqlite3_mpri
17c10 6e 74 66 28 22 49 4e 53 45 52 54 20 49 4e 54 4f  ntf("INSERT INTO
17c20 20 74 61 62 6c 65 20 56 41 4c 55 45 53 28 27 25   table VALUES('%
17c30 71 27 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a 2a  q')", zText);.**
17c40 20 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64    sqlite3_exec(d
17c50 62 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20 30  b, zSQL, 0, 0, 0
17c60 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66  );.**  sqlite3_f
17c70 72 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c 2f  ree(zSQL);.** </
17c80 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65  pre></blockquote
17c90 3e 0a 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 65 20  >.**.** Because 
17ca0 74 68 65 20 25 71 20 66 6f 72 6d 61 74 20 73 74  the %q format st
17cb0 72 69 6e 67 20 69 73 20 75 73 65 64 2c 20 74 68  ring is used, th
17cc0 65 20 27 5c 27 27 20 63 68 61 72 61 63 74 65 72  e '\'' character
17cd0 20 69 6e 20 7a 54 65 78 74 0a 2a 2a 20 69 73 20   in zText.** is 
17ce0 65 73 63 61 70 65 64 20 61 6e 64 20 74 68 65 20  escaped and the 
17cf0 53 51 4c 20 67 65 6e 65 72 61 74 65 64 20 69 73  SQL generated is
17d00 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
17d10 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c  ** <blockquote><
17d20 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52 54 20  pre>.**  INSERT 
17d30 49 4e 54 4f 20 74 61 62 6c 65 31 20 56 41 4c 55  INTO table1 VALU
17d40 45 53 28 27 49 74 27 27 73 20 61 20 68 61 70 70  ES('It''s a happ
17d50 79 20 64 61 79 21 27 29 0a 2a 2a 20 3c 2f 70 72  y day!').** </pr
17d60 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a  e></blockquote>.
17d70 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 63 6f  **.** This is co
17d80 72 72 65 63 74 2e 20 20 48 61 64 20 77 65 20 75  rrect.  Had we u
17d90 73 65 64 20 25 73 20 69 6e 73 74 65 61 64 20 6f  sed %s instead o
17da0 66 20 25 71 2c 20 74 68 65 20 67 65 6e 65 72 61  f %q, the genera
17db0 74 65 64 20 53 51 4c 0a 2a 2a 20 77 6f 75 6c 64  ted SQL.** would
17dc0 20 68 61 76 65 20 6c 6f 6f 6b 65 64 20 6c 69 6b   have looked lik
17dd0 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62  e this:.**.** <b
17de0 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a  lockquote><pre>.
17df0 2a 2a 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20  **  INSERT INTO 
17e00 74 61 62 6c 65 31 20 56 41 4c 55 45 53 28 27 49  table1 VALUES('I
17e10 74 27 73 20 61 20 68 61 70 70 79 20 64 61 79 21  t's a happy day!
17e20 27 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62  ');.** </pre></b
17e30 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a  lockquote>.**.**
17e40 20 54 68 69 73 20 73 65 63 6f 6e 64 20 65 78 61   This second exa
17e50 6d 70 6c 65 20 69 73 20 61 6e 20 53 51 4c 20 73  mple is an SQL s
17e60 79 6e 74 61 78 20 65 72 72 6f 72 2e 20 20 41 73  yntax error.  As
17e70 20 61 20 67 65 6e 65 72 61 6c 20 72 75 6c 65 20   a general rule 
17e80 79 6f 75 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 6c  you should.** al
17e90 77 61 79 73 20 75 73 65 20 25 71 20 69 6e 73 74  ways use %q inst
17ea0 65 61 64 20 6f 66 20 25 73 20 77 68 65 6e 20 69  ead of %s when i
17eb0 6e 73 65 72 74 69 6e 67 20 74 65 78 74 20 69 6e  nserting text in
17ec0 74 6f 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65  to a string lite
17ed0 72 61 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 25  ral..**.** The %
17ee0 51 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 20 6c  Q option works l
17ef0 69 6b 65 20 25 71 20 65 78 63 65 70 74 20 69 74  ike %q except it
17f00 20 61 6c 73 6f 20 61 64 64 73 20 73 69 6e 67 6c   also adds singl
17f10 65 20 71 75 6f 74 65 73 20 61 72 6f 75 6e 64 0a  e quotes around.
17f20 2a 2a 20 74 68 65 20 6f 75 74 73 69 64 65 20 6f  ** the outside o
17f30 66 20 74 68 65 20 74 6f 74 61 6c 20 73 74 72 69  f the total stri
17f40 6e 67 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 6c  ng.  Additionall
17f50 79 2c 20 69 66 20 74 68 65 20 70 61 72 61 6d 65  y, if the parame
17f60 74 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 72  ter in the.** ar
17f70 67 75 6d 65 6e 74 20 6c 69 73 74 20 69 73 20 61  gument list is a
17f80 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 25   NULL pointer, %
17f90 51 20 73 75 62 73 74 69 74 75 74 65 73 20 74 68  Q substitutes th
17fa0 65 20 74 65 78 74 20 22 4e 55 4c 4c 22 20 28 77  e text "NULL" (w
17fb0 69 74 68 6f 75 74 0a 2a 2a 20 73 69 6e 67 6c 65  ithout.** single
17fc0 20 71 75 6f 74 65 73 29 20 69 6e 20 70 6c 61 63   quotes) in plac
17fd0 65 20 6f 66 20 74 68 65 20 25 51 20 6f 70 74 69  e of the %Q opti
17fe0 6f 6e 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 61  on.  So, for exa
17ff0 6d 70 6c 65 2c 20 6f 6e 65 20 63 6f 75 6c 64 20  mple, one could 
18000 73 61 79 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63  say:.**.** <bloc
18010 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20  kquote><pre>.** 
18020 20 63 68 61 72 20 2a 7a 53 51 4c 20 3d 20 73 71   char *zSQL = sq
18030 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 49  lite3_mprintf("I
18040 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c 65  NSERT INTO table
18050 20 56 41 4c 55 45 53 28 25 51 29 22 2c 20 7a 54   VALUES(%Q)", zT
18060 65 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65  ext);.**  sqlite
18070 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c 2c  3_exec(db, zSQL,
18080 20 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 73   0, 0, 0);.**  s
18090 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51 4c  qlite3_free(zSQL
180a0 29 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c  );.** </pre></bl
180b0 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20  ockquote>.**.** 
180c0 54 68 65 20 63 6f 64 65 20 61 62 6f 76 65 20 77  The code above w
180d0 69 6c 6c 20 72 65 6e 64 65 72 20 61 20 63 6f 72  ill render a cor
180e0 72 65 63 74 20 53 51 4c 20 73 74 61 74 65 6d 65  rect SQL stateme
180f0 6e 74 20 69 6e 20 74 68 65 20 7a 53 51 4c 0a 2a  nt in the zSQL.*
18100 2a 20 76 61 72 69 61 62 6c 65 20 65 76 65 6e 20  * variable even 
18110 69 66 20 74 68 65 20 7a 54 65 78 74 20 76 61 72  if the zText var
18120 69 61 62 6c 65 20 69 73 20 61 20 4e 55 4c 4c 20  iable is a NULL 
18130 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54  pointer..**.** T
18140 68 65 20 22 25 7a 22 20 66 6f 72 6d 61 74 74 69  he "%z" formatti
18150 6e 67 20 6f 70 74 69 6f 6e 20 77 6f 72 6b 73 20  ng option works 
18160 65 78 61 63 74 6c 79 20 6c 69 6b 65 20 22 25 73  exactly like "%s
18170 22 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 61 64  " with the.** ad
18180 64 69 74 69 6f 6e 20 74 68 61 74 20 61 66 74 65  dition that afte
18190 72 20 74 68 65 20 73 74 72 69 6e 67 20 68 61 73  r the string has
181a0 20 62 65 65 6e 20 72 65 61 64 20 61 6e 64 20 63   been read and c
181b0 6f 70 69 65 64 20 69 6e 74 6f 0a 2a 2a 20 74 68  opied into.** th
181c0 65 20 72 65 73 75 6c 74 2c 20 5b 73 71 6c 69 74  e result, [sqlit
181d0 65 33 5f 66 72 65 65 28 29 5d 20 69 73 20 63 61  e3_free()] is ca
181e0 6c 6c 65 64 20 6f 6e 20 74 68 65 20 69 6e 70 75  lled on the inpu
181f0 74 20 73 74 72 69 6e 67 2e 20 7b 45 4e 44 7d 0a  t string. {END}.
18200 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
18210 74 73 3a 0a 2a 2a 20 5b 48 31 37 34 30 33 5d 20  ts:.** [H17403] 
18220 5b 48 31 37 34 30 36 5d 20 5b 48 31 37 34 30 37  [H17406] [H17407
18230 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
18240 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 6d 70  char *sqlite3_mp
18250 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68 61 72  rintf(const char
18260 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f 41  *,...);.SQLITE_A
18270 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  PI char *sqlite3
18280 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20  _vmprintf(const 
18290 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b  char*, va_list);
182a0 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72  .SQLITE_API char
182b0 20 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e   *sqlite3_snprin
182c0 74 66 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f 6e  tf(int,char*,con
182d0 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a  st char*, ...);.
182e0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
182f0 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69   Memory Allocati
18300 6f 6e 20 53 75 62 73 79 73 74 65 6d 20 7b 48 31  on Subsystem {H1
18310 37 33 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a  7300} <S20000>.*
18320 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20  *.** The SQLite 
18330 63 6f 72 65 20 20 75 73 65 73 20 74 68 65 73 65  core  uses these
18340 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65 73 20   three routines 
18350 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73 20 6f  for all of its o
18360 77 6e 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c 20 6d  wn.** internal m
18370 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
18380 20 6e 65 65 64 73 2e 20 22 43 6f 72 65 22 20 69   needs. "Core" i
18390 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73  n the previous s
183a0 65 6e 74 65 6e 63 65 0a 2a 2a 20 64 6f 65 73 20  entence.** does 
183b0 6e 6f 74 20 69 6e 63 6c 75 64 65 20 6f 70 65 72  not include oper
183c0 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 73 70 65  ating-system spe
183d0 63 69 66 69 63 20 56 46 53 20 69 6d 70 6c 65 6d  cific VFS implem
183e0 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65 0a 2a  entation.  The.*
183f0 2a 20 57 69 6e 64 6f 77 73 20 56 46 53 20 75 73  * Windows VFS us
18400 65 73 20 6e 61 74 69 76 65 20 6d 61 6c 6c 6f 63  es native malloc
18410 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 66 6f  () and free() fo
18420 72 20 73 6f 6d 65 20 6f 70 65 72 61 74 69 6f 6e  r some operation
18430 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  s..**.** The sql
18440 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 6f  ite3_malloc() ro
18450 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20  utine returns a 
18460 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62 6c 6f  pointer to a blo
18470 63 6b 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 20  ck.** of memory 
18480 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74 65 73  at least N bytes
18490 20 69 6e 20 6c 65 6e 67 74 68 2c 20 77 68 65 72   in length, wher
184a0 65 20 4e 20 69 73 20 74 68 65 20 70 61 72 61 6d  e N is the param
184b0 65 74 65 72 2e 0a 2a 2a 20 49 66 20 73 71 6c 69  eter..** If sqli
184c0 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 69 73 20  te3_malloc() is 
184d0 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61 69 6e  unable to obtain
184e0 20 73 75 66 66 69 63 69 65 6e 74 20 66 72 65 65   sufficient free
184f0 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 69 74 20 72  .** memory, it r
18500 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70 6f  eturns a NULL po
18510 69 6e 74 65 72 2e 20 20 49 66 20 74 68 65 20 70  inter.  If the p
18520 61 72 61 6d 65 74 65 72 20 4e 20 74 6f 0a 2a 2a  arameter N to.**
18530 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
18540 29 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65 67  ) is zero or neg
18550 61 74 69 76 65 20 74 68 65 6e 20 73 71 6c 69 74  ative then sqlit
18560 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 65 74 75  e3_malloc() retu
18570 72 6e 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f  rns.** a NULL po
18580 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c  inter..**.** Cal
18590 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 65  ling sqlite3_fre
185a0 65 28 29 20 77 69 74 68 20 61 20 70 6f 69 6e 74  e() with a point
185b0 65 72 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65  er previously re
185c0 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73 71 6c  turned.** by sql
185d0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72  ite3_malloc() or
185e0 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
185f0 28 29 20 72 65 6c 65 61 73 65 73 20 74 68 61 74  () releases that
18600 20 6d 65 6d 6f 72 79 20 73 6f 0a 2a 2a 20 74 68   memory so.** th
18610 61 74 20 69 74 20 6d 69 67 68 74 20 62 65 20 72  at it might be r
18620 65 75 73 65 64 2e 20 20 54 68 65 20 73 71 6c 69  eused.  The sqli
18630 74 65 33 5f 66 72 65 65 28 29 20 72 6f 75 74 69  te3_free() routi
18640 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70  ne is.** a no-op
18650 20 69 66 20 69 73 20 63 61 6c 6c 65 64 20 77 69   if is called wi
18660 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  th a NULL pointe
18670 72 2e 20 20 50 61 73 73 69 6e 67 20 61 20 4e 55  r.  Passing a NU
18680 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f  LL pointer.** to
18690 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20   sqlite3_free() 
186a0 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20 41 66  is harmless.  Af
186b0 74 65 72 20 62 65 69 6e 67 20 66 72 65 65 64 2c  ter being freed,
186c0 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 68 6f 75 6c   memory.** shoul
186d0 64 20 6e 65 69 74 68 65 72 20 62 65 20 72 65 61  d neither be rea
186e0 64 20 6e 6f 72 20 77 72 69 74 74 65 6e 2e 20 20  d nor written.  
186f0 45 76 65 6e 20 72 65 61 64 69 6e 67 20 70 72 65  Even reading pre
18700 76 69 6f 75 73 6c 79 20 66 72 65 65 64 0a 2a 2a  viously freed.**
18710 20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20 72 65   memory might re
18720 73 75 6c 74 20 69 6e 20 61 20 73 65 67 6d 65 6e  sult in a segmen
18730 74 61 74 69 6f 6e 20 66 61 75 6c 74 20 6f 72 20  tation fault or 
18740 6f 74 68 65 72 20 73 65 76 65 72 65 20 65 72 72  other severe err
18750 6f 72 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63 6f  or..** Memory co
18760 72 72 75 70 74 69 6f 6e 2c 20 61 20 73 65 67 6d  rruption, a segm
18770 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 2c 20  entation fault, 
18780 6f 72 20 6f 74 68 65 72 20 73 65 76 65 72 65 20  or other severe 
18790 65 72 72 6f 72 0a 2a 2a 20 6d 69 67 68 74 20 72  error.** might r
187a0 65 73 75 6c 74 20 69 66 20 73 71 6c 69 74 65 33  esult if sqlite3
187b0 5f 66 72 65 65 28 29 20 69 73 20 63 61 6c 6c 65  _free() is calle
187c0 64 20 77 69 74 68 20 61 20 6e 6f 6e 2d 4e 55 4c  d with a non-NUL
187d0 4c 20 70 6f 69 6e 74 65 72 20 74 68 61 74 0a 2a  L pointer that.*
187e0 2a 20 77 61 73 20 6e 6f 74 20 6f 62 74 61 69 6e  * was not obtain
187f0 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ed from sqlite3_
18800 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69  malloc() or sqli
18810 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 2a  te3_realloc()..*
18820 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
18830 5f 72 65 61 6c 6c 6f 63 28 29 20 69 6e 74 65 72  _realloc() inter
18840 66 61 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f  face attempts to
18850 20 72 65 73 69 7a 65 20 61 0a 2a 2a 20 70 72 69   resize a.** pri
18860 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  or memory alloca
18870 74 69 6f 6e 20 74 6f 20 62 65 20 61 74 20 6c 65  tion to be at le
18880 61 73 74 20 4e 20 62 79 74 65 73 2c 20 77 68 65  ast N bytes, whe
18890 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20 73  re N is the.** s
188a0 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e  econd parameter.
188b0 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c    The memory all
188c0 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65 20 72 65  ocation to be re
188d0 73 69 7a 65 64 20 69 73 20 74 68 65 20 66 69 72  sized is the fir
188e0 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 2e  st.** parameter.
188f0 20 20 49 66 20 74 68 65 20 66 69 72 73 74 20 70    If the first p
18900 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69  arameter to sqli
18910 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a  te3_realloc().**
18920 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   is a NULL point
18930 65 72 20 74 68 65 6e 20 69 74 73 20 62 65 68 61  er then its beha
18940 76 69 6f 72 20 69 73 20 69 64 65 6e 74 69 63 61  vior is identica
18950 6c 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20  l to calling.** 
18960 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 4e  sqlite3_malloc(N
18970 29 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65  ) where N is the
18980 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
18990 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61  r to sqlite3_rea
189a0 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66 20 74 68  lloc()..** If th
189b0 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  e second paramet
189c0 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65  er to sqlite3_re
189d0 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72 6f 20  alloc() is zero 
189e0 6f 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 74  or.** negative t
189f0 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72  hen the behavior
18a00 20 69 73 20 65 78 61 63 74 6c 79 20 74 68 65 20   is exactly the 
18a10 73 61 6d 65 20 61 73 20 63 61 6c 6c 69 6e 67 0a  same as calling.
18a20 2a 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  ** sqlite3_free(
18a30 50 29 20 77 68 65 72 65 20 50 20 69 73 20 74 68  P) where P is th
18a40 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
18a50 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 61  r to sqlite3_rea
18a60 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 73 71 6c 69 74  lloc()..** sqlit
18a70 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72 65 74  e3_realloc() ret
18a80 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74  urns a pointer t
18a90 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  o a memory alloc
18aa0 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 61 74 20 6c  ation.** of at l
18ab0 65 61 73 74 20 4e 20 62 79 74 65 73 20 69 6e 20  east N bytes in 
18ac0 73 69 7a 65 20 6f 72 20 4e 55 4c 4c 20 69 66 20  size or NULL if 
18ad0 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f 72  sufficient memor
18ae0 79 20 69 73 20 75 6e 61 76 61 69 6c 61 62 6c 65  y is unavailable
18af0 2e 0a 2a 2a 20 49 66 20 4d 20 69 73 20 74 68 65  ..** If M is the
18b00 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70 72 69   size of the pri
18b10 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 74  or allocation, t
18b20 68 65 6e 20 6d 69 6e 28 4e 2c 4d 29 20 62 79 74  hen min(N,M) byt
18b30 65 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70 72 69  es.** of the pri
18b40 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 72  or allocation ar
18b50 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68  e copied into th
18b60 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 62  e beginning of b
18b70 75 66 66 65 72 20 72 65 74 75 72 6e 65 64 0a 2a  uffer returned.*
18b80 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 72 65 61  * by sqlite3_rea
18b90 6c 6c 6f 63 28 29 20 61 6e 64 20 74 68 65 20 70  lloc() and the p
18ba0 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  rior allocation 
18bb0 69 73 20 66 72 65 65 64 2e 0a 2a 2a 20 49 66 20  is freed..** If 
18bc0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
18bd0 29 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2c 20  ) returns NULL, 
18be0 74 68 65 6e 20 74 68 65 20 70 72 69 6f 72 20 61  then the prior a
18bf0 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 20  llocation.** is 
18c00 6e 6f 74 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a  not freed..**.**
18c10 20 54 68 65 20 6d 65 6d 6f 72 79 20 72 65 74 75   The memory retu
18c20 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  rned by sqlite3_
18c30 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 73 71 6c  malloc() and sql
18c40 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a 2a  ite3_realloc().*
18c50 2a 20 69 73 20 61 6c 77 61 79 73 20 61 6c 69 67  * is always alig
18c60 6e 65 64 20 74 6f 20 61 74 20 6c 65 61 73 74 20  ned to at least 
18c70 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e 64 61  an 8 byte bounda
18c80 72 79 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20  ry. {END}.**.** 
18c90 54 68 65 20 64 65 66 61 75 6c 74 20 69 6d 70 6c  The default impl
18ca0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
18cb0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
18cc0 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 75 73  ion subsystem us
18cd0 65 73 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63  es.** the malloc
18ce0 28 29 2c 20 72 65 61 6c 6c 6f 63 28 29 20 61 6e  (), realloc() an
18cf0 64 20 66 72 65 65 28 29 20 70 72 6f 76 69 64 65  d free() provide
18d00 64 20 62 79 20 74 68 65 20 73 74 61 6e 64 61 72  d by the standar
18d10 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 20  d C library..** 
18d20 7b 48 31 37 33 38 32 7d 20 48 6f 77 65 76 65 72  {H17382} However
18d30 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73 20 63  , if SQLite is c
18d40 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65  ompiled with the
18d50 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 4f 52  .** SQLITE_MEMOR
18d60 59 5f 53 49 5a 45 3d 3c 69 3e 4e 4e 4e 3c 2f 69  Y_SIZE=<i>NNN</i
18d70 3e 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f 72  > C preprocessor
18d80 20 6d 61 63 72 6f 20 28 77 68 65 72 65 20 3c 69   macro (where <i
18d90 3e 4e 4e 4e 3c 2f 69 3e 0a 2a 2a 20 69 73 20 61  >NNN</i>.** is a
18da0 6e 20 69 6e 74 65 67 65 72 29 2c 20 74 68 65 6e  n integer), then
18db0 20 53 51 4c 69 74 65 20 63 72 65 61 74 65 20 61   SQLite create a
18dc0 20 73 74 61 74 69 63 20 61 72 72 61 79 20 6f 66   static array of
18dd0 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 3c 69 3e   at least.** <i>
18de0 4e 4e 4e 3c 2f 69 3e 20 62 79 74 65 73 20 69 6e  NNN</i> bytes in
18df0 20 73 69 7a 65 20 61 6e 64 20 75 73 65 73 20 74   size and uses t
18e00 68 61 74 20 61 72 72 61 79 20 66 6f 72 20 61 6c  hat array for al
18e10 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 63  l of its dynamic
18e20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  .** memory alloc
18e30 61 74 69 6f 6e 20 6e 65 65 64 73 2e 20 7b 45 4e  ation needs. {EN
18e40 44 7d 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 6d  D}  Additional m
18e50 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
18e60 6f 70 74 69 6f 6e 73 0a 2a 2a 20 6d 61 79 20 62  options.** may b
18e70 65 20 61 64 64 65 64 20 69 6e 20 66 75 74 75 72  e added in futur
18e80 65 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a 0a 2a  e releases..**.*
18e90 2a 20 49 6e 20 53 51 4c 69 74 65 20 76 65 72 73  * In SQLite vers
18ea0 69 6f 6e 20 33 2e 35 2e 30 20 61 6e 64 20 33 2e  ion 3.5.0 and 3.
18eb0 35 2e 31 2c 20 69 74 20 77 61 73 20 70 6f 73 73  5.1, it was poss
18ec0 69 62 6c 65 20 74 6f 20 64 65 66 69 6e 65 0a 2a  ible to define.*
18ed0 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d 49  * the SQLITE_OMI
18ee0 54 5f 4d 45 4d 4f 52 59 5f 41 4c 4c 4f 43 41 54  T_MEMORY_ALLOCAT
18ef0 49 4f 4e 20 77 68 69 63 68 20 77 6f 75 6c 64 20  ION which would 
18f00 63 61 75 73 65 20 74 68 65 20 62 75 69 6c 74 2d  cause the built-
18f10 69 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  in.** implementa
18f20 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 72 6f  tion of these ro
18f30 75 74 69 6e 65 73 20 74 6f 20 62 65 20 6f 6d 69  utines to be omi
18f40 74 74 65 64 2e 20 20 54 68 61 74 20 63 61 70 61  tted.  That capa
18f50 62 69 6c 69 74 79 0a 2a 2a 20 69 73 20 6e 6f 20  bility.** is no 
18f60 6c 6f 6e 67 65 72 20 70 72 6f 76 69 64 65 64 2e  longer provided.
18f70 20 20 4f 6e 6c 79 20 62 75 69 6c 74 2d 69 6e 20    Only built-in 
18f80 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
18f90 73 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a 2a  s can be used..*
18fa0 2a 0a 2a 2a 20 54 68 65 20 57 69 6e 64 6f 77 73  *.** The Windows
18fb0 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 6c 61   OS interface la
18fc0 79 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 74 68 65  yer calls.** the
18fd0 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29   system malloc()
18fe0 20 61 6e 64 20 66 72 65 65 28 29 20 64 69 72 65   and free() dire
18ff0 63 74 6c 79 20 77 68 65 6e 20 63 6f 6e 76 65 72  ctly when conver
19000 74 69 6e 67 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65  ting.** filename
19010 73 20 62 65 74 77 65 65 6e 20 74 68 65 20 55 54  s between the UT
19020 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 75 73 65  F-8 encoding use
19030 64 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a 20 61  d by SQLite.** a
19040 6e 64 20 77 68 61 74 65 76 65 72 20 66 69 6c 65  nd whatever file
19050 6e 61 6d 65 20 65 6e 63 6f 64 69 6e 67 20 69 73  name encoding is
19060 20 75 73 65 64 20 62 79 20 74 68 65 20 70 61 72   used by the par
19070 74 69 63 75 6c 61 72 20 57 69 6e 64 6f 77 73 0a  ticular Windows.
19080 2a 2a 20 69 6e 73 74 61 6c 6c 61 74 69 6f 6e 2e  ** installation.
19090 20 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74    Memory allocat
190a0 69 6f 6e 20 65 72 72 6f 72 73 20 61 72 65 20 64  ion errors are d
190b0 65 74 65 63 74 65 64 2c 20 62 75 74 0a 2a 2a 20  etected, but.** 
190c0 74 68 65 79 20 61 72 65 20 72 65 70 6f 72 74 65  they are reporte
190d0 64 20 62 61 63 6b 20 61 73 20 5b 53 51 4c 49 54  d back as [SQLIT
190e0 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 6f 72 0a 2a  E_CANTOPEN] or.*
190f0 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5d  * [SQLITE_IOERR]
19100 20 72 61 74 68 65 72 20 74 68 61 6e 20 5b 53 51   rather than [SQ
19110 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a 2a 0a  LITE_NOMEM]..**.
19120 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
19130 0a 2a 2a 20 5b 48 31 37 33 30 33 5d 20 5b 48 31  .** [H17303] [H1
19140 37 33 30 34 5d 20 5b 48 31 37 33 30 35 5d 20 5b  7304] [H17305] [
19150 48 31 37 33 30 36 5d 20 5b 48 31 37 33 31 30 5d  H17306] [H17310]
19160 20 5b 48 31 37 33 31 32 5d 20 5b 48 31 37 33 31   [H17312] [H1731
19170 35 5d 20 5b 48 31 37 33 31 38 5d 0a 2a 2a 20 5b  5] [H17318].** [
19180 48 31 37 33 32 31 5d 20 5b 48 31 37 33 32 32 5d  H17321] [H17322]
19190 20 5b 48 31 37 33 32 33 5d 0a 2a 2a 0a 2a 2a 20   [H17323].**.** 
191a0 54 68 65 20 70 6f 69 6e 74 65 72 20 61 72 67 75  The pointer argu
191b0 6d 65 6e 74 73 20 74 6f 20 5b 73 71 6c 69 74 65  ments to [sqlite
191c0 33 5f 66 72 65 65 28 29 5d 20 61 6e 64 20 5b 73  3_free()] and [s
191d0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29  qlite3_realloc()
191e0 5d 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74  ].** must be eit
191f0 68 65 72 20 4e 55 4c 4c 20 6f 72 20 65 6c 73 65  her NULL or else
19200 20 70 6f 69 6e 74 65 72 73 20 6f 62 74 61 69 6e   pointers obtain
19210 65 64 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 0a  ed from a prior.
19220 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66  ** invocation of
19230 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
19240 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
19250 72 65 61 6c 6c 6f 63 28 29 5d 20 74 68 61 74 20  realloc()] that 
19260 68 61 76 65 0a 2a 2a 20 6e 6f 74 20 79 65 74 20  have.** not yet 
19270 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 0a 2a  been released..*
19280 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61  *.** The applica
19290 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 72 65  tion must not re
192a0 61 64 20 6f 72 20 77 72 69 74 65 20 61 6e 79 20  ad or write any 
192b0 70 61 72 74 20 6f 66 0a 2a 2a 20 61 20 62 6c 6f  part of.** a blo
192c0 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 61 66 74  ck of memory aft
192d0 65 72 20 69 74 20 68 61 73 20 62 65 65 6e 20 72  er it has been r
192e0 65 6c 65 61 73 65 64 20 75 73 69 6e 67 0a 2a 2a  eleased using.**
192f0 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29   [sqlite3_free()
19300 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65  ] or [sqlite3_re
19310 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2f 0a 53 51 4c  alloc()]..*/.SQL
19320 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71  ITE_API void *sq
19330 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74  lite3_malloc(int
19340 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  );.SQLITE_API vo
19350 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 65 61 6c  id *sqlite3_real
19360 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e 74 29 3b  loc(void*, int);
19370 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
19380 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76 6f   sqlite3_free(vo
19390 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  id*);../*.** CAP
193a0 49 33 52 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c  I3REF: Memory Al
193b0 6c 6f 63 61 74 6f 72 20 53 74 61 74 69 73 74 69  locator Statisti
193c0 63 73 20 7b 48 31 37 33 37 30 7d 20 3c 53 33 30  cs {H17370} <S30
193d0 32 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  210>.**.** SQLit
193e0 65 20 70 72 6f 76 69 64 65 73 20 74 68 65 73 65  e provides these
193f0 20 74 77 6f 20 69 6e 74 65 72 66 61 63 65 73 20   two interfaces 
19400 66 6f 72 20 72 65 70 6f 72 74 69 6e 67 20 6f 6e  for reporting on
19410 20 74 68 65 20 73 74 61 74 75 73 0a 2a 2a 20 6f   the status.** o
19420 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d  f the [sqlite3_m
19430 61 6c 6c 6f 63 28 29 5d 2c 20 5b 73 71 6c 69 74  alloc()], [sqlit
19440 65 33 5f 66 72 65 65 28 29 5d 2c 20 61 6e 64 20  e3_free()], and 
19450 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  [sqlite3_realloc
19460 28 29 5d 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2c  ()].** routines,
19470 20 77 68 69 63 68 20 66 6f 72 6d 20 74 68 65 20   which form the 
19480 62 75 69 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20  built-in memory 
19490 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
194a0 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  stem..**.** Requ
194b0 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
194c0 37 33 37 31 5d 20 5b 48 31 37 33 37 33 5d 20 5b  7371] [H17373] [
194d0 48 31 37 33 37 34 5d 20 5b 48 31 37 33 37 35 5d  H17374] [H17375]
194e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  .*/.SQLITE_API s
194f0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c  qlite3_int64 sql
19500 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64  ite3_memory_used
19510 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41  (void);.SQLITE_A
19520 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  PI sqlite3_int64
19530 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f   sqlite3_memory_
19540 68 69 67 68 77 61 74 65 72 28 69 6e 74 20 72 65  highwater(int re
19550 73 65 74 46 6c 61 67 29 3b 0a 0a 2f 2a 0a 2a 2a  setFlag);../*.**
19560 20 43 41 50 49 33 52 45 46 3a 20 50 73 65 75 64   CAPI3REF: Pseud
19570 6f 2d 52 61 6e 64 6f 6d 20 4e 75 6d 62 65 72 20  o-Random Number 
19580 47 65 6e 65 72 61 74 6f 72 20 7b 48 31 37 33 39  Generator {H1739
19590 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a  0} <S20000>.**.*
195a0 2a 20 53 51 4c 69 74 65 20 63 6f 6e 74 61 69 6e  * SQLite contain
195b0 73 20 61 20 68 69 67 68 2d 71 75 61 6c 69 74 79  s a high-quality
195c0 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e   pseudo-random n
195d0 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20  umber generator 
195e0 28 50 52 4e 47 29 20 75 73 65 64 20 74 6f 0a 2a  (PRNG) used to.*
195f0 2a 20 73 65 6c 65 63 74 20 72 61 6e 64 6f 6d 20  * select random 
19600 5b 52 4f 57 49 44 20 7c 20 52 4f 57 49 44 73 5d  [ROWID | ROWIDs]
19610 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20   when inserting 
19620 6e 65 77 20 72 65 63 6f 72 64 73 20 69 6e 74 6f  new records into
19630 20 61 20 74 61 62 6c 65 20 74 68 61 74 0a 2a 2a   a table that.**
19640 20 61 6c 72 65 61 64 79 20 75 73 65 73 20 74 68   already uses th
19650 65 20 6c 61 72 67 65 73 74 20 70 6f 73 73 69 62  e largest possib
19660 6c 65 20 5b 52 4f 57 49 44 5d 2e 20 20 54 68 65  le [ROWID].  The
19670 20 50 52 4e 47 20 69 73 20 61 6c 73 6f 20 75 73   PRNG is also us
19680 65 64 20 66 6f 72 0a 2a 2a 20 74 68 65 20 62 75  ed for.** the bu
19690 69 6c 64 2d 69 6e 20 72 61 6e 64 6f 6d 28 29 20  ild-in random() 
196a0 61 6e 64 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 29  and randomblob()
196b0 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 20   SQL functions. 
196c0 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   This interface 
196d0 61 6c 6c 6f 77 73 0a 2a 2a 20 61 70 70 6c 69 63  allows.** applic
196e0 61 74 69 6f 6e 73 20 74 6f 20 61 63 63 65 73 73  ations to access
196f0 20 74 68 65 20 73 61 6d 65 20 50 52 4e 47 20 66   the same PRNG f
19700 6f 72 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65  or other purpose
19710 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20  s..**.** A call 
19720 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  to this routine 
19730 73 74 6f 72 65 73 20 4e 20 62 79 74 65 73 20 6f  stores N bytes o
19740 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74  f randomness int
19750 6f 20 62 75 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a  o buffer P..**.*
19760 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65  * The first time
19770 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
19780 20 69 6e 76 6f 6b 65 64 20 28 65 69 74 68 65 72   invoked (either
19790 20 69 6e 74 65 72 6e 61 6c 6c 79 20 6f 72 20 62   internally or b
197a0 79 0a 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61  y.** the applica
197b0 74 69 6f 6e 29 20 74 68 65 20 50 52 4e 47 20 69  tion) the PRNG i
197c0 73 20 73 65 65 64 65 64 20 75 73 69 6e 67 20 72  s seeded using r
197d0 61 6e 64 6f 6d 6e 65 73 73 20 6f 62 74 61 69 6e  andomness obtain
197e0 65 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 78  ed.** from the x
197f0 52 61 6e 64 6f 6d 6e 65 73 73 20 6d 65 74 68 6f  Randomness metho
19800 64 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74  d of the default
19810 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f   [sqlite3_vfs] o
19820 62 6a 65 63 74 2e 0a 2a 2a 20 4f 6e 20 61 6c 6c  bject..** On all
19830 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76 6f   subsequent invo
19840 63 61 74 69 6f 6e 73 2c 20 74 68 65 20 70 73 65  cations, the pse
19850 75 64 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 69  udo-randomness i
19860 73 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 69  s generated.** i
19870 6e 74 65 72 6e 61 6c 6c 79 20 61 6e 64 20 77 69  nternally and wi
19880 74 68 6f 75 74 20 72 65 63 6f 75 72 73 65 20 74  thout recourse t
19890 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76  o the [sqlite3_v
198a0 66 73 5d 20 78 52 61 6e 64 6f 6d 6e 65 73 73 0a  fs] xRandomness.
198b0 2a 2a 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a  ** method..**.**
198c0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
198d0 2a 20 5b 48 31 37 33 39 32 5d 0a 2a 2f 0a 53 51  * [H17392].*/.SQ
198e0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
198f0 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73  lite3_randomness
19900 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 50 29  (int N, void *P)
19910 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
19920 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d 65 20  F: Compile-Time 
19930 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 43 61  Authorization Ca
19940 6c 6c 62 61 63 6b 73 20 7b 48 31 32 35 30 30 7d  llbacks {H12500}
19950 20 3c 53 37 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S70100>.**.** 
19960 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 67  This routine reg
19970 69 73 74 65 72 73 20 61 20 61 75 74 68 6f 72 69  isters a authori
19980 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 77 69 74  zer callback wit
19990 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a  h a particular.*
199a0 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  * [database conn
199b0 65 63 74 69 6f 6e 5d 2c 20 73 75 70 70 6c 69 65  ection], supplie
199c0 64 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 61  d in the first a
199d0 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20  rgument..** The 
199e0 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62  authorizer callb
199f0 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 61  ack is invoked a
19a00 73 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  s SQL statements
19a10 20 61 72 65 20 62 65 69 6e 67 20 63 6f 6d 70 69   are being compi
19a20 6c 65 64 0a 2a 2a 20 62 79 20 5b 73 71 6c 69 74  led.** by [sqlit
19a30 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f 72  e3_prepare()] or
19a40 20 69 74 73 20 76 61 72 69 61 6e 74 73 20 5b 73   its variants [s
19a50 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
19a60 32 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65  2()],.** [sqlite
19a70 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20 61  3_prepare16()] a
19a80 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  nd [sqlite3_prep
19a90 61 72 65 31 36 5f 76 32 28 29 5d 2e 20 20 41 74  are16_v2()].  At
19aa0 20 76 61 72 69 6f 75 73 0a 2a 2a 20 70 6f 69 6e   various.** poin
19ab0 74 73 20 64 75 72 69 6e 67 20 74 68 65 20 63 6f  ts during the co
19ac0 6d 70 69 6c 61 74 69 6f 6e 20 70 72 6f 63 65 73  mpilation proces
19ad0 73 2c 20 61 73 20 6c 6f 67 69 63 20 69 73 20 62  s, as logic is b
19ae0 65 69 6e 67 20 63 72 65 61 74 65 64 0a 2a 2a 20  eing created.** 
19af0 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72 69 6f  to perform vario
19b00 75 73 20 61 63 74 69 6f 6e 73 2c 20 74 68 65 20  us actions, the 
19b10 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62  authorizer callb
19b20 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 74  ack is invoked t
19b30 6f 0a 2a 2a 20 73 65 65 20 69 66 20 74 68 6f 73  o.** see if thos
19b40 65 20 61 63 74 69 6f 6e 73 20 61 72 65 20 61 6c  e actions are al
19b50 6c 6f 77 65 64 2e 20 20 54 68 65 20 61 75 74 68  lowed.  The auth
19b60 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20  orizer callback 
19b70 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75 72 6e  should.** return
19b80 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 74 6f 20   [SQLITE_OK] to 
19b90 61 6c 6c 6f 77 20 74 68 65 20 61 63 74 69 6f 6e  allow the action
19ba0 2c 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  , [SQLITE_IGNORE
19bb0 5d 20 74 6f 20 64 69 73 61 6c 6c 6f 77 20 74 68  ] to disallow th
19bc0 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 61 63  e.** specific ac
19bd0 74 69 6f 6e 20 62 75 74 20 61 6c 6c 6f 77 20 74  tion but allow t
19be0 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
19bf0 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20   to continue to 
19c00 62 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 2c 20  be.** compiled, 
19c10 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d  or [SQLITE_DENY]
19c20 20 74 6f 20 63 61 75 73 65 20 74 68 65 20 65 6e   to cause the en
19c30 74 69 72 65 20 53 51 4c 20 73 74 61 74 65 6d 65  tire SQL stateme
19c40 6e 74 20 74 6f 20 62 65 0a 2a 2a 20 72 65 6a 65  nt to be.** reje
19c50 63 74 65 64 20 77 69 74 68 20 61 6e 20 65 72 72  cted with an err
19c60 6f 72 2e 20 20 49 66 20 74 68 65 20 61 75 74 68  or.  If the auth
19c70 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20  orizer callback 
19c80 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 79 20 76  returns.** any v
19c90 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e 20  alue other than 
19ca0 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 2c  [SQLITE_IGNORE],
19cb0 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20 6f 72   [SQLITE_OK], or
19cc0 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d 0a 2a   [SQLITE_DENY].*
19cd0 2a 20 74 68 65 6e 20 74 68 65 20 5b 73 71 6c 69  * then the [sqli
19ce0 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
19cf0 5d 20 6f 72 20 65 71 75 69 76 61 6c 65 6e 74 20  ] or equivalent 
19d00 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67 67 65  call that trigge
19d10 72 65 64 0a 2a 2a 20 74 68 65 20 61 75 74 68 6f  red.** the autho
19d20 72 69 7a 65 72 20 77 69 6c 6c 20 66 61 69 6c 20  rizer will fail 
19d30 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65  with an error me
19d40 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  ssage..**.** Whe
19d50 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 72  n the callback r
19d60 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f  eturns [SQLITE_O
19d70 4b 5d 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74  K], that means t
19d80 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20  he operation.** 
19d90 72 65 71 75 65 73 74 65 64 20 69 73 20 6f 6b 2e  requested is ok.
19da0 20 20 57 68 65 6e 20 74 68 65 20 63 61 6c 6c 62    When the callb
19db0 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c  ack returns [SQL
19dc0 49 54 45 5f 44 45 4e 59 5d 2c 20 74 68 65 0a 2a  ITE_DENY], the.*
19dd0 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  * [sqlite3_prepa
19de0 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71 75 69  re_v2()] or equi
19df0 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68 61 74  valent call that
19e00 20 74 72 69 67 67 65 72 65 64 20 74 68 65 0a 2a   triggered the.*
19e10 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 77 69 6c  * authorizer wil
19e20 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 20 65  l fail with an e
19e30 72 72 6f 72 20 6d 65 73 73 61 67 65 20 65 78 70  rror message exp
19e40 6c 61 69 6e 69 6e 67 20 74 68 61 74 0a 2a 2a 20  laining that.** 
19e50 61 63 63 65 73 73 20 69 73 20 64 65 6e 69 65 64  access is denied
19e60 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  . .**.** The fir
19e70 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  st parameter to 
19e80 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63  the authorizer c
19e90 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 63 6f 70  allback is a cop
19ea0 79 20 6f 66 20 74 68 65 20 74 68 69 72 64 0a 2a  y of the third.*
19eb0 2a 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  * parameter to t
19ec0 68 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  he sqlite3_set_a
19ed0 75 74 68 6f 72 69 7a 65 72 28 29 20 69 6e 74 65  uthorizer() inte
19ee0 72 66 61 63 65 2e 20 54 68 65 20 73 65 63 6f 6e  rface. The secon
19ef0 64 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74  d parameter.** t
19f00 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69  o the callback i
19f10 73 20 61 6e 20 69 6e 74 65 67 65 72 20 5b 53 51  s an integer [SQ
19f20 4c 49 54 45 5f 43 4f 50 59 20 7c 20 61 63 74 69  LITE_COPY | acti
19f30 6f 6e 20 63 6f 64 65 5d 20 74 68 61 74 20 73 70  on code] that sp
19f40 65 63 69 66 69 65 73 0a 2a 2a 20 74 68 65 20 70  ecifies.** the p
19f50 61 72 74 69 63 75 6c 61 72 20 61 63 74 69 6f 6e  articular action
19f60 20 74 6f 20 62 65 20 61 75 74 68 6f 72 69 7a 65   to be authorize
19f70 64 2e 20 54 68 65 20 74 68 69 72 64 20 74 68 72  d. The third thr
19f80 6f 75 67 68 20 73 69 78 74 68 20 70 61 72 61 6d  ough sixth param
19f90 65 74 65 72 73 0a 2a 2a 20 74 6f 20 74 68 65 20  eters.** to the 
19fa0 63 61 6c 6c 62 61 63 6b 20 61 72 65 20 7a 65 72  callback are zer
19fb0 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72  o-terminated str
19fc0 69 6e 67 73 20 74 68 61 74 20 63 6f 6e 74 61 69  ings that contai
19fd0 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20  n additional.** 
19fe0 64 65 74 61 69 6c 73 20 61 62 6f 75 74 20 74 68  details about th
19ff0 65 20 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 61  e action to be a
1a000 75 74 68 6f 72 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a  uthorized..**.**
1a010 20 49 66 20 74 68 65 20 61 63 74 69 6f 6e 20 63   If the action c
1a020 6f 64 65 20 69 73 20 5b 53 51 4c 49 54 45 5f 52  ode is [SQLITE_R
1a030 45 41 44 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 20  EAD].** and the 
1a040 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73  callback returns
1a050 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d   [SQLITE_IGNORE]
1a060 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 5b 70 72   then the.** [pr
1a070 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
1a080 5d 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 63  ] statement is c
1a090 6f 6e 73 74 72 75 63 74 65 64 20 74 6f 20 73 75  onstructed to su
1a0a0 62 73 74 69 74 75 74 65 0a 2a 2a 20 61 20 4e 55  bstitute.** a NU
1a0b0 4c 4c 20 76 61 6c 75 65 20 69 6e 20 70 6c 61 63  LL value in plac
1a0c0 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63  e of the table c
1a0d0 6f 6c 75 6d 6e 20 74 68 61 74 20 77 6f 75 6c 64  olumn that would
1a0e0 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20 72 65   have.** been re
1a0f0 61 64 20 69 66 20 5b 53 51 4c 49 54 45 5f 4f 4b  ad if [SQLITE_OK
1a100 5d 20 68 61 64 20 62 65 65 6e 20 72 65 74 75 72  ] had been retur
1a110 6e 65 64 2e 20 20 54 68 65 20 5b 53 51 4c 49 54  ned.  The [SQLIT
1a120 45 5f 49 47 4e 4f 52 45 5d 0a 2a 2a 20 72 65 74  E_IGNORE].** ret
1a130 75 72 6e 20 63 61 6e 20 62 65 20 75 73 65 64 20  urn can be used 
1a140 74 6f 20 64 65 6e 79 20 61 6e 20 75 6e 74 72 75  to deny an untru
1a150 73 74 65 64 20 75 73 65 72 20 61 63 63 65 73 73  sted user access
1a160 20 74 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a   to individual.*
1a170 2a 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 61 20 74  * columns of a t
1a180 61 62 6c 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20  able..** If the 
1a190 61 63 74 69 6f 6e 20 63 6f 64 65 20 69 73 20 5b  action code is [
1a1a0 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 5d 20 61  SQLITE_DELETE] a
1a1b0 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  nd the callback 
1a1c0 72 65 74 75 72 6e 73 0a 2a 2a 20 5b 53 51 4c 49  returns.** [SQLI
1a1d0 54 45 5f 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20  TE_IGNORE] then 
1a1e0 74 68 65 20 5b 44 45 4c 45 54 45 5d 20 6f 70 65  the [DELETE] ope
1a1f0 72 61 74 69 6f 6e 20 70 72 6f 63 65 65 64 73 20  ration proceeds 
1a200 62 75 74 20 74 68 65 0a 2a 2a 20 5b 74 72 75 6e  but the.** [trun
1a210 63 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f  cate optimizatio
1a220 6e 5d 20 69 73 20 64 69 73 61 62 6c 65 64 20 61  n] is disabled a
1a230 6e 64 20 61 6c 6c 20 72 6f 77 73 20 61 72 65 20  nd all rows are 
1a240 64 65 6c 65 74 65 64 20 69 6e 64 69 76 69 64 75  deleted individu
1a250 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61  ally..**.** An a
1a260 75 74 68 6f 72 69 7a 65 72 20 69 73 20 75 73 65  uthorizer is use
1a270 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f  d when [sqlite3_
1a280 70 72 65 70 61 72 65 20 7c 20 70 72 65 70 61 72  prepare | prepar
1a290 69 6e 67 5d 0a 2a 2a 20 53 51 4c 20 73 74 61 74  ing].** SQL stat
1a2a0 65 6d 65 6e 74 73 20 66 72 6f 6d 20 61 6e 20 75  ements from an u
1a2b0 6e 74 72 75 73 74 65 64 20 73 6f 75 72 63 65 2c  ntrusted source,
1a2c0 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20   to ensure that 
1a2d0 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
1a2e0 74 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20 74 72 79  ts.** do not try
1a2f0 20 74 6f 20 61 63 63 65 73 73 20 64 61 74 61 20   to access data 
1a300 74 68 65 79 20 61 72 65 20 6e 6f 74 20 61 6c 6c  they are not all
1a310 6f 77 65 64 20 74 6f 20 73 65 65 2c 20 6f 72 20  owed to see, or 
1a320 74 68 61 74 20 74 68 65 79 20 64 6f 20 6e 6f 74  that they do not
1a330 0a 2a 2a 20 74 72 79 20 74 6f 20 65 78 65 63 75  .** try to execu
1a340 74 65 20 6d 61 6c 69 63 69 6f 75 73 20 73 74 61  te malicious sta
1a350 74 65 6d 65 6e 74 73 20 74 68 61 74 20 64 61 6d  tements that dam
1a360 61 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65  age the database
1a370 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c  .  For.** exampl
1a380 65 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f  e, an applicatio
1a390 6e 20 6d 61 79 20 61 6c 6c 6f 77 20 61 20 75 73  n may allow a us
1a3a0 65 72 20 74 6f 20 65 6e 74 65 72 20 61 72 62 69  er to enter arbi
1a3b0 74 72 61 72 79 0a 2a 2a 20 53 51 4c 20 71 75 65  trary.** SQL que
1a3c0 72 69 65 73 20 66 6f 72 20 65 76 61 6c 75 61 74  ries for evaluat
1a3d0 69 6f 6e 20 62 79 20 61 20 64 61 74 61 62 61 73  ion by a databas
1a3e0 65 2e 20 20 42 75 74 20 74 68 65 20 61 70 70 6c  e.  But the appl
1a3f0 69 63 61 74 69 6f 6e 20 64 6f 65 73 0a 2a 2a 20  ication does.** 
1a400 6e 6f 74 20 77 61 6e 74 20 74 68 65 20 75 73 65  not want the use
1a410 72 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20  r to be able to 
1a420 6d 61 6b 65 20 61 72 62 69 74 72 61 72 79 20 63  make arbitrary c
1a430 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a  hanges to the.**
1a440 20 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20 61   database.  An a
1a450 75 74 68 6f 72 69 7a 65 72 20 63 6f 75 6c 64 20  uthorizer could 
1a460 74 68 65 6e 20 62 65 20 70 75 74 20 69 6e 20 70  then be put in p
1a470 6c 61 63 65 20 77 68 69 6c 65 20 74 68 65 0a 2a  lace while the.*
1a480 2a 20 75 73 65 72 2d 65 6e 74 65 72 65 64 20 53  * user-entered S
1a490 51 4c 20 69 73 20 62 65 69 6e 67 20 5b 73 71 6c  QL is being [sql
1a4a0 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70  ite3_prepare | p
1a4b0 72 65 70 61 72 65 64 5d 20 74 68 61 74 0a 2a 2a  repared] that.**
1a4c0 20 64 69 73 61 6c 6c 6f 77 73 20 65 76 65 72 79   disallows every
1a4d0 74 68 69 6e 67 20 65 78 63 65 70 74 20 5b 53 45  thing except [SE
1a4e0 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 73  LECT] statements
1a4f0 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74  ..**.** Applicat
1a500 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74  ions that need t
1a510 6f 20 70 72 6f 63 65 73 73 20 53 51 4c 20 66 72  o process SQL fr
1a520 6f 6d 20 75 6e 74 72 75 73 74 65 64 20 73 6f 75  om untrusted sou
1a530 72 63 65 73 0a 2a 2a 20 6d 69 67 68 74 20 61 6c  rces.** might al
1a540 73 6f 20 63 6f 6e 73 69 64 65 72 20 6c 6f 77 65  so consider lowe
1a550 72 69 6e 67 20 72 65 73 6f 75 72 63 65 20 6c 69  ring resource li
1a560 6d 69 74 73 20 75 73 69 6e 67 20 5b 73 71 6c 69  mits using [sqli
1a570 74 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20  te3_limit()].** 
1a580 61 6e 64 20 6c 69 6d 69 74 69 6e 67 20 64 61 74  and limiting dat
1a590 61 62 61 73 65 20 73 69 7a 65 20 75 73 69 6e 67  abase size using
1a5a0 20 74 68 65 20 5b 6d 61 78 5f 70 61 67 65 5f 63   the [max_page_c
1a5b0 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 0a 2a  ount] [PRAGMA].*
1a5c0 2a 20 69 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f  * in addition to
1a5d0 20 75 73 69 6e 67 20 61 6e 20 61 75 74 68 6f 72   using an author
1a5e0 69 7a 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79  izer..**.** Only
1a5f0 20 61 20 73 69 6e 67 6c 65 20 61 75 74 68 6f 72   a single author
1a600 69 7a 65 72 20 63 61 6e 20 62 65 20 69 6e 20 70  izer can be in p
1a610 6c 61 63 65 20 6f 6e 20 61 20 64 61 74 61 62 61  lace on a databa
1a620 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  se connection.**
1a630 20 61 74 20 61 20 74 69 6d 65 2e 20 20 45 61 63   at a time.  Eac
1a640 68 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  h call to sqlite
1a650 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  3_set_authorizer
1a660 20 6f 76 65 72 72 69 64 65 73 20 74 68 65 0a 2a   overrides the.*
1a670 2a 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2e  * previous call.
1a680 20 20 44 69 73 61 62 6c 65 20 74 68 65 20 61 75    Disable the au
1a690 74 68 6f 72 69 7a 65 72 20 62 79 20 69 6e 73 74  thorizer by inst
1a6a0 61 6c 6c 69 6e 67 20 61 20 4e 55 4c 4c 20 63 61  alling a NULL ca
1a6b0 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 54 68 65 20 61  llback..** The a
1a6c0 75 74 68 6f 72 69 7a 65 72 20 69 73 20 64 69 73  uthorizer is dis
1a6d0 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74  abled by default
1a6e0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  ..**.** The auth
1a6f0 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20  orizer callback 
1a700 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74  must not do anyt
1a710 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d  hing that will m
1a720 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74  odify.** the dat
1a730 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1a740 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68   that invoked th
1a750 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c  e authorizer cal
1a760 6c 62 61 63 6b 2e 0a 2a 2a 20 4e 6f 74 65 20 74  lback..** Note t
1a770 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  hat [sqlite3_pre
1a780 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b  pare_v2()] and [
1a790 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
1a7a0 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69  both modify thei
1a7b0 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  r.** database co
1a7c0 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68  nnections for th
1a7d0 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f  e meaning of "mo
1a7e0 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 61  dify" in this pa
1a7f0 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 57  ragraph..**.** W
1a800 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  hen [sqlite3_pre
1a810 70 61 72 65 5f 76 32 28 29 5d 20 69 73 20 75 73  pare_v2()] is us
1a820 65 64 20 74 6f 20 70 72 65 70 61 72 65 20 61 20  ed to prepare a 
1a830 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 0a 2a  statement, the.*
1a840 2a 20 73 74 61 74 65 6d 65 6e 74 20 6d 69 67 68  * statement migh
1a850 74 20 62 65 20 72 65 2d 70 72 65 70 61 72 65 64  t be re-prepared
1a860 20 64 75 72 69 6e 67 20 5b 73 71 6c 69 74 65 33   during [sqlite3
1a870 5f 73 74 65 70 28 29 5d 20 64 75 65 20 74 6f 20  _step()] due to 
1a880 61 20 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61  a .** schema cha
1a890 6e 67 65 2e 20 20 48 65 6e 63 65 2c 20 74 68 65  nge.  Hence, the
1a8a0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f   application sho
1a8b0 75 6c 64 20 65 6e 73 75 72 65 20 74 68 61 74 20  uld ensure that 
1a8c0 74 68 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61  the.** correct a
1a8d0 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61  uthorizer callba
1a8e0 63 6b 20 72 65 6d 61 69 6e 73 20 69 6e 20 70 6c  ck remains in pl
1a8f0 61 63 65 20 64 75 72 69 6e 67 20 74 68 65 20 5b  ace during the [
1a900 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e  sqlite3_step()].
1a910 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
1a920 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20   the authorizer 
1a930 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f  callback is invo
1a940 6b 65 64 20 6f 6e 6c 79 20 64 75 72 69 6e 67 0a  ked only during.
1a950 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ** [sqlite3_prep
1a960 61 72 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61  are()] or its va
1a970 72 69 61 6e 74 73 2e 20 20 41 75 74 68 6f 72 69  riants.  Authori
1a980 7a 61 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a  zation is not.**
1a990 20 70 65 72 66 6f 72 6d 65 64 20 64 75 72 69 6e   performed durin
1a9a0 67 20 73 74 61 74 65 6d 65 6e 74 20 65 76 61 6c  g statement eval
1a9b0 75 61 74 69 6f 6e 20 69 6e 20 5b 73 71 6c 69 74  uation in [sqlit
1a9c0 65 33 5f 73 74 65 70 28 29 5d 2c 20 75 6e 6c 65  e3_step()], unle
1a9d0 73 73 0a 2a 2a 20 61 73 20 73 74 61 74 65 64 20  ss.** as stated 
1a9e0 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  in the previous 
1a9f0 70 61 72 61 67 72 61 70 68 2c 20 73 71 6c 69 74  paragraph, sqlit
1aa00 65 33 5f 73 74 65 70 28 29 20 69 6e 76 6f 6b 65  e3_step() invoke
1aa10 73 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72 65  s.** sqlite3_pre
1aa20 70 61 72 65 5f 76 32 28 29 20 74 6f 20 72 65 70  pare_v2() to rep
1aa30 72 65 70 61 72 65 20 61 20 73 74 61 74 65 6d 65  repare a stateme
1aa40 6e 74 20 61 66 74 65 72 20 61 20 73 63 68 65 6d  nt after a schem
1aa50 61 20 63 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20  a change..**.** 
1aa60 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
1aa70 20 5b 48 31 32 35 30 31 5d 20 5b 48 31 32 35 30   [H12501] [H1250
1aa80 32 5d 20 5b 48 31 32 35 30 33 5d 20 5b 48 31 32  2] [H12503] [H12
1aa90 35 30 34 5d 20 5b 48 31 32 35 30 35 5d 20 5b 48  504] [H12505] [H
1aaa0 31 32 35 30 36 5d 20 5b 48 31 32 35 30 37 5d 20  12506] [H12507] 
1aab0 5b 48 31 32 35 31 30 5d 0a 2a 2a 20 5b 48 31 32  [H12510].** [H12
1aac0 35 31 31 5d 20 5b 48 31 32 35 31 32 5d 20 5b 48  511] [H12512] [H
1aad0 31 32 35 32 30 5d 20 5b 48 31 32 35 32 31 5d 20  12520] [H12521] 
1aae0 5b 48 31 32 35 32 32 5d 0a 2a 2f 0a 53 51 4c 49  [H12522].*/.SQLI
1aaf0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1ab00 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65  e3_set_authorize
1ab10 72 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 0a 20  r(.  sqlite3*,. 
1ab20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f   int (*xAuth)(vo
1ab30 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68  id*,int,const ch
1ab40 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
1ab50 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
1ab60 74 20 63 68 61 72 2a 29 2c 0a 20 20 76 6f 69 64  t char*),.  void
1ab70 20 2a 70 55 73 65 72 44 61 74 61 0a 29 3b 0a 0a   *pUserData.);..
1ab80 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
1ab90 41 75 74 68 6f 72 69 7a 65 72 20 52 65 74 75 72  Authorizer Retur
1aba0 6e 20 43 6f 64 65 73 20 7b 48 31 32 35 39 30 7d  n Codes {H12590}
1abb0 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a 2a 20   <H12500>.**.** 
1abc0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 65 74  The [sqlite3_set
1abd0 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61 75  _authorizer | au
1abe0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1abf0 6b 20 66 75 6e 63 74 69 6f 6e 5d 20 6d 75 73 74  k function] must
1ac00 0a 2a 2a 20 72 65 74 75 72 6e 20 65 69 74 68 65  .** return eithe
1ac10 72 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 72  r [SQLITE_OK] or
1ac20 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 74 77   one of these tw
1ac30 6f 20 63 6f 6e 73 74 61 6e 74 73 20 69 6e 20 6f  o constants in o
1ac40 72 64 65 72 0a 2a 2a 20 74 6f 20 73 69 67 6e 61  rder.** to signa
1ac50 6c 20 53 51 4c 69 74 65 20 77 68 65 74 68 65 72  l SQLite whether
1ac60 20 6f 72 20 6e 6f 74 20 74 68 65 20 61 63 74 69   or not the acti
1ac70 6f 6e 20 69 73 20 70 65 72 6d 69 74 74 65 64 2e  on is permitted.
1ac80 20 20 53 65 65 20 74 68 65 0a 2a 2a 20 5b 73 71    See the.** [sq
1ac90 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72  lite3_set_author
1aca0 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65  izer | authorize
1acb0 72 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 5d  r documentation]
1acc0 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a   for additional.
1acd0 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  ** information..
1ace0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1acf0 45 5f 44 45 4e 59 20 20 20 31 20 20 20 2f 2a 20  E_DENY   1   /* 
1ad00 41 62 6f 72 74 20 74 68 65 20 53 51 4c 20 73 74  Abort the SQL st
1ad10 61 74 65 6d 65 6e 74 20 77 69 74 68 20 61 6e 20  atement with an 
1ad20 65 72 72 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65  error */.#define
1ad30 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 32   SQLITE_IGNORE 2
1ad40 20 20 20 2f 2a 20 44 6f 6e 27 74 20 61 6c 6c 6f     /* Don't allo
1ad50 77 20 61 63 63 65 73 73 2c 20 62 75 74 20 64 6f  w access, but do
1ad60 6e 27 74 20 67 65 6e 65 72 61 74 65 20 61 6e 20  n't generate an 
1ad70 65 72 72 6f 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  error */../*.** 
1ad80 43 41 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72  CAPI3REF: Author
1ad90 69 7a 65 72 20 41 63 74 69 6f 6e 20 43 6f 64 65  izer Action Code
1ada0 73 20 7b 48 31 32 35 35 30 7d 20 3c 48 31 32 35  s {H12550} <H125
1adb0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73  00>.**.** The [s
1adc0 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f  qlite3_set_autho
1add0 72 69 7a 65 72 28 29 5d 20 69 6e 74 65 72 66 61  rizer()] interfa
1ade0 63 65 20 72 65 67 69 73 74 65 72 73 20 61 20 63  ce registers a c
1adf0 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
1ae00 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 76 6f  .** that is invo
1ae10 6b 65 64 20 74 6f 20 61 75 74 68 6f 72 69 7a 65  ked to authorize
1ae20 20 63 65 72 74 61 69 6e 20 53 51 4c 20 73 74 61   certain SQL sta
1ae30 74 65 6d 65 6e 74 20 61 63 74 69 6f 6e 73 2e 20  tement actions. 
1ae40 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 70   The.** second p
1ae50 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20  arameter to the 
1ae60 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 69  callback is an i
1ae70 6e 74 65 67 65 72 20 63 6f 64 65 20 74 68 61 74  nteger code that
1ae80 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 77 68   specifies.** wh
1ae90 61 74 20 61 63 74 69 6f 6e 20 69 73 20 62 65 69  at action is bei
1aea0 6e 67 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 20  ng authorized.  
1aeb0 54 68 65 73 65 20 61 72 65 20 74 68 65 20 69 6e  These are the in
1aec0 74 65 67 65 72 20 61 63 74 69 6f 6e 20 63 6f 64  teger action cod
1aed0 65 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 61  es that.** the a
1aee0 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61  uthorizer callba
1aef0 63 6b 20 6d 61 79 20 62 65 20 70 61 73 73 65 64  ck may be passed
1af00 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61 63  ..**.** These ac
1af10 74 69 6f 6e 20 63 6f 64 65 20 76 61 6c 75 65 73  tion code values
1af20 20 73 69 67 6e 69 66 79 20 77 68 61 74 20 6b 69   signify what ki
1af30 6e 64 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 20  nd of operation 
1af40 69 73 20 74 6f 20 62 65 0a 2a 2a 20 61 75 74 68  is to be.** auth
1af50 6f 72 69 7a 65 64 2e 20 20 54 68 65 20 33 72 64  orized.  The 3rd
1af60 20 61 6e 64 20 34 74 68 20 70 61 72 61 6d 65 74   and 4th paramet
1af70 65 72 73 20 74 6f 20 74 68 65 20 61 75 74 68 6f  ers to the autho
1af80 72 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6c 6c  rization.** call
1af90 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 77 69  back function wi
1afa0 6c 6c 20 62 65 20 70 61 72 61 6d 65 74 65 72 73  ll be parameters
1afb0 20 6f 72 20 4e 55 4c 4c 20 64 65 70 65 6e 64 69   or NULL dependi
1afc0 6e 67 20 6f 6e 20 77 68 69 63 68 20 6f 66 20 74  ng on which of t
1afd0 68 65 73 65 0a 2a 2a 20 63 6f 64 65 73 20 69 73  hese.** codes is
1afe0 20 75 73 65 64 20 61 73 20 74 68 65 20 73 65 63   used as the sec
1aff0 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20  ond parameter.  
1b000 54 68 65 20 35 74 68 20 70 61 72 61 6d 65 74 65  The 5th paramete
1b010 72 20 74 6f 20 74 68 65 0a 2a 2a 20 61 75 74 68  r to the.** auth
1b020 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20  orizer callback 
1b030 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
1b040 68 65 20 64 61 74 61 62 61 73 65 20 28 22 6d 61  he database ("ma
1b050 69 6e 22 2c 20 22 74 65 6d 70 22 2c 0a 2a 2a 20  in", "temp",.** 
1b060 65 74 63 2e 29 20 69 66 20 61 70 70 6c 69 63 61  etc.) if applica
1b070 62 6c 65 2e 20 20 54 68 65 20 36 74 68 20 70 61  ble.  The 6th pa
1b080 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61  rameter to the a
1b090 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61  uthorizer callba
1b0a0 63 6b 0a 2a 2a 20 69 73 20 74 68 65 20 6e 61 6d  ck.** is the nam
1b0b0 65 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 2d 6d  e of the inner-m
1b0c0 6f 73 74 20 74 72 69 67 67 65 72 20 6f 72 20 76  ost trigger or v
1b0d0 69 65 77 20 74 68 61 74 20 69 73 20 72 65 73 70  iew that is resp
1b0e0 6f 6e 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 74  onsible for.** t
1b0f0 68 65 20 61 63 63 65 73 73 20 61 74 74 65 6d 70  he access attemp
1b100 74 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 69  t or NULL if thi
1b110 73 20 61 63 63 65 73 73 20 61 74 74 65 6d 70 74  s access attempt
1b120 20 69 73 20 64 69 72 65 63 74 6c 79 20 66 72 6f   is directly fro
1b130 6d 0a 2a 2a 20 74 6f 70 2d 6c 65 76 65 6c 20 53  m.** top-level S
1b140 51 4c 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 52  QL code..**.** R
1b150 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
1b160 5b 48 31 32 35 35 31 5d 20 5b 48 31 32 35 35 32  [H12551] [H12552
1b170 5d 20 5b 48 31 32 35 35 33 5d 20 5b 48 31 32 35  ] [H12553] [H125
1b180 35 34 5d 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  54].*/./********
1b190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b1b0 2a 2a 2a 20 33 72 64 20 2a 2a 2a 2a 2a 2a 2a 2a  *** 3rd ********
1b1c0 2a 2a 2a 2a 20 34 74 68 20 2a 2a 2a 2a 2a 2a 2a  **** 4th *******
1b1d0 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ****/.#define SQ
1b1e0 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45  LITE_CREATE_INDE
1b1f0 58 20 20 20 20 20 20 20 20 20 20 31 20 20 20 2f  X          1   /
1b200 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20  * Index Name    
1b210 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20    Table Name    
1b220 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1b230 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c 45  ITE_CREATE_TABLE
1b240 20 20 20 20 20 20 20 20 20 20 32 20 20 20 2f 2a            2   /*
1b250 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
1b260 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
1b270 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1b280 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49  TE_CREATE_TEMP_I
1b290 4e 44 45 58 20 20 20 20 20 33 20 20 20 2f 2a 20  NDEX     3   /* 
1b2a0 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20 20  Index Name      
1b2b0 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
1b2c0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1b2d0 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 41  E_CREATE_TEMP_TA
1b2e0 42 4c 45 20 20 20 20 20 34 20 20 20 2f 2a 20 54  BLE     4   /* T
1b2f0 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 4e  able Name      N
1b300 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
1b310 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1b320 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52 49  _CREATE_TEMP_TRI
1b330 47 47 45 52 20 20 20 35 20 20 20 2f 2a 20 54 72  GGER   5   /* Tr
1b340 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61  igger Name    Ta
1b350 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f  ble Name      */
1b360 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1b370 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57  CREATE_TEMP_VIEW
1b380 20 20 20 20 20 20 36 20 20 20 2f 2a 20 56 69 65        6   /* Vie
1b390 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c  w Name       NUL
1b3a0 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L            */.
1b3b0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
1b3c0 52 45 41 54 45 5f 54 52 49 47 47 45 52 20 20 20  REATE_TRIGGER   
1b3d0 20 20 20 20 20 37 20 20 20 2f 2a 20 54 72 69 67       7   /* Trig
1b3e0 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62 6c  ger Name    Tabl
1b3f0 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23  e Name      */.#
1b400 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 52  define SQLITE_CR
1b410 45 41 54 45 5f 56 49 45 57 20 20 20 20 20 20 20  EATE_VIEW       
1b420 20 20 20 20 38 20 20 20 2f 2a 20 56 69 65 77 20      8   /* View 
1b430 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c 20  Name       NULL 
1b440 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
1b450 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 4c  efine SQLITE_DEL
1b460 45 54 45 20 20 20 20 20 20 20 20 20 20 20 20 20  ETE             
1b470 20 20 20 39 20 20 20 2f 2a 20 54 61 62 6c 65 20     9   /* Table 
1b480 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20  Name      NULL  
1b490 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65            */.#de
1b4a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50  fine SQLITE_DROP
1b4b0 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20  _INDEX          
1b4c0 20 31 30 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e   10   /* Index N
1b4d0 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e  ame      Table N
1b4e0 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  ame      */.#def
1b4f0 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  ine SQLITE_DROP_
1b500 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20  TABLE           
1b510 31 31 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61  11   /* Table Na
1b520 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20  me      NULL    
1b530 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
1b540 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54  ne SQLITE_DROP_T
1b550 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 20 31  EMP_INDEX      1
1b560 32 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61 6d  2   /* Index Nam
1b570 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61 6d  e      Table Nam
1b580 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  e      */.#defin
1b590 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45  e SQLITE_DROP_TE
1b5a0 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 20 31 33  MP_TABLE      13
1b5b0 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65     /* Table Name
1b5c0 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20        NULL      
1b5d0 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
1b5e0 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d   SQLITE_DROP_TEM
1b5f0 50 5f 54 52 49 47 47 45 52 20 20 20 20 31 34 20  P_TRIGGER    14 
1b600 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d    /* Trigger Nam
1b610 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20  e    Table Name 
1b620 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
1b630 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
1b640 5f 56 49 45 57 20 20 20 20 20 20 20 31 35 20 20  _VIEW       15  
1b650 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20   /* View Name   
1b660 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20      NULL        
1b670 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
1b680 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47 47  QLITE_DROP_TRIGG
1b690 45 52 20 20 20 20 20 20 20 20 20 31 36 20 20 20  ER         16   
1b6a0 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65 20  /* Trigger Name 
1b6b0 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20     Table Name   
1b6c0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1b6d0 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 20 20  LITE_DROP_VIEW  
1b6e0 20 20 20 20 20 20 20 20 20 20 31 37 20 20 20 2f            17   /
1b6f0 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20 20  * View Name     
1b700 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20    NULL          
1b710 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1b720 49 54 45 5f 49 4e 53 45 52 54 20 20 20 20 20 20  ITE_INSERT      
1b730 20 20 20 20 20 20 20 20 20 31 38 20 20 20 2f 2a           18   /*
1b740 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
1b750 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20   NULL           
1b760 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1b770 54 45 5f 50 52 41 47 4d 41 20 20 20 20 20 20 20  TE_PRAGMA       
1b780 20 20 20 20 20 20 20 20 31 39 20 20 20 2f 2a 20          19   /* 
1b790 50 72 61 67 6d 61 20 4e 61 6d 65 20 20 20 20 20  Pragma Name     
1b7a0 31 73 74 20 61 72 67 20 6f 72 20 4e 55 4c 4c 20  1st arg or NULL 
1b7b0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1b7c0 45 5f 52 45 41 44 20 20 20 20 20 20 20 20 20 20  E_READ          
1b7d0 20 20 20 20 20 20 20 32 30 20 20 20 2f 2a 20 54         20   /* T
1b7e0 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 43  able Name      C
1b7f0 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a  olumn Name     *
1b800 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1b810 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 20 20  _SELECT         
1b820 20 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4e 55        21   /* NU
1b830 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 4e 55  LL            NU
1b840 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  LL            */
1b850 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1b860 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20 20  TRANSACTION     
1b870 20 20 20 20 20 32 32 20 20 20 2f 2a 20 4f 70 65       22   /* Ope
1b880 72 61 74 69 6f 6e 20 20 20 20 20 20 20 4e 55 4c  ration       NUL
1b890 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  L            */.
1b8a0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55  #define SQLITE_U
1b8b0 50 44 41 54 45 20 20 20 20 20 20 20 20 20 20 20  PDATE           
1b8c0 20 20 20 20 32 33 20 20 20 2f 2a 20 54 61 62 6c      23   /* Tabl
1b8d0 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c 75  e Name      Colu
1b8e0 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a 23  mn Name     */.#
1b8f0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 54  define SQLITE_AT
1b900 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20  TACH            
1b910 20 20 20 32 34 20 20 20 2f 2a 20 46 69 6c 65 6e     24   /* Filen
1b920 61 6d 65 20 20 20 20 20 20 20 20 4e 55 4c 4c 20  ame        NULL 
1b930 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
1b940 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 54  efine SQLITE_DET
1b950 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20 20  ACH             
1b960 20 20 32 35 20 20 20 2f 2a 20 44 61 74 61 62 61    25   /* Databa
1b970 73 65 20 4e 61 6d 65 20 20 20 4e 55 4c 4c 20 20  se Name   NULL  
1b980 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65            */.#de
1b990 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4c 54 45  fine SQLITE_ALTE
1b9a0 52 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20  R_TABLE         
1b9b0 20 32 36 20 20 20 2f 2a 20 44 61 74 61 62 61 73   26   /* Databas
1b9c0 65 20 4e 61 6d 65 20 20 20 54 61 62 6c 65 20 4e  e Name   Table N
1b9d0 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  ame      */.#def
1b9e0 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 49 4e 44  ine SQLITE_REIND
1b9f0 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20  EX              
1ba00 32 37 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61  27   /* Index Na
1ba10 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20  me      NULL    
1ba20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
1ba30 6e 65 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59 5a  ne SQLITE_ANALYZ
1ba40 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32  E              2
1ba50 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d  8   /* Table Nam
1ba60 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20  e      NULL     
1ba70 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
1ba80 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  e SQLITE_CREATE_
1ba90 56 54 41 42 4c 45 20 20 20 20 20 20 20 20 32 39  VTABLE        29
1baa0 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65     /* Table Name
1bab0 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d        Module Nam
1bac0 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  e     */.#define
1bad0 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54 41   SQLITE_DROP_VTA
1bae0 42 4c 45 20 20 20 20 20 20 20 20 20 20 33 30 20  BLE          30 
1baf0 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20    /* Table Name 
1bb00 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d 65       Module Name
1bb10 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
1bb20 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e 20  SQLITE_FUNCTION 
1bb30 20 20 20 20 20 20 20 20 20 20 20 20 33 31 20 20              31  
1bb40 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20 20   /* NULL        
1bb50 20 20 20 20 46 75 6e 63 74 69 6f 6e 20 4e 61 6d      Function Nam
1bb60 65 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  e   */.#define S
1bb70 51 4c 49 54 45 5f 53 41 56 45 50 4f 49 4e 54 20  QLITE_SAVEPOINT 
1bb80 20 20 20 20 20 20 20 20 20 20 20 33 32 20 20 20             32   
1bb90 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 20 20 20  /* Operation    
1bba0 20 20 20 53 61 76 65 70 6f 69 6e 74 20 4e 61 6d     Savepoint Nam
1bbb0 65 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  e  */.#define SQ
1bbc0 4c 49 54 45 5f 43 4f 50 59 20 20 20 20 20 20 20  LITE_COPY       
1bbd0 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20 2f             0   /
1bbe0 2a 20 4e 6f 20 6c 6f 6e 67 65 72 20 75 73 65 64  * No longer used
1bbf0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33   */../*.** CAPI3
1bc00 52 45 46 3a 20 54 72 61 63 69 6e 67 20 41 6e 64  REF: Tracing And
1bc10 20 50 72 6f 66 69 6c 69 6e 67 20 46 75 6e 63 74   Profiling Funct
1bc20 69 6f 6e 73 20 7b 48 31 32 32 38 30 7d 20 3c 53  ions {H12280} <S
1bc30 36 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49  60400>.** EXPERI
1bc40 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65  MENTAL.**.** The
1bc50 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 67 69  se routines regi
1bc60 73 74 65 72 20 63 61 6c 6c 62 61 63 6b 20 66 75  ster callback fu
1bc70 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 63 61 6e  nctions that can
1bc80 20 62 65 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20   be used for.** 
1bc90 74 72 61 63 69 6e 67 20 61 6e 64 20 70 72 6f 66  tracing and prof
1bca0 69 6c 69 6e 67 20 74 68 65 20 65 78 65 63 75 74  iling the execut
1bcb0 69 6f 6e 20 6f 66 20 53 51 4c 20 73 74 61 74 65  ion of SQL state
1bcc0 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ments..**.** The
1bcd0 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
1bce0 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 62 79  on registered by
1bcf0 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 29   sqlite3_trace()
1bd00 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 0a 2a   is invoked at.*
1bd10 2a 20 76 61 72 69 6f 75 73 20 74 69 6d 65 73 20  * various times 
1bd20 77 68 65 6e 20 61 6e 20 53 51 4c 20 73 74 61 74  when an SQL stat
1bd30 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67 20 72  ement is being r
1bd40 75 6e 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 73  un by [sqlite3_s
1bd50 74 65 70 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 63  tep()]..** The c
1bd60 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20  allback returns 
1bd70 61 20 55 54 46 2d 38 20 72 65 6e 64 65 72 69 6e  a UTF-8 renderin
1bd80 67 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61  g of the SQL sta
1bd90 74 65 6d 65 6e 74 20 74 65 78 74 0a 2a 2a 20 61  tement text.** a
1bda0 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  s the statement 
1bdb0 66 69 72 73 74 20 62 65 67 69 6e 73 20 65 78 65  first begins exe
1bdc0 63 75 74 69 6e 67 2e 20 20 41 64 64 69 74 69 6f  cuting.  Additio
1bdd0 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 6f 63  nal callbacks oc
1bde0 63 75 72 0a 2a 2a 20 61 73 20 65 61 63 68 20 74  cur.** as each t
1bdf0 72 69 67 67 65 72 65 64 20 73 75 62 70 72 6f 67  riggered subprog
1be00 72 61 6d 20 69 73 20 65 6e 74 65 72 65 64 2e 20  ram is entered. 
1be10 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 73 20 66   The callbacks f
1be20 6f 72 20 74 72 69 67 67 65 72 73 0a 2a 2a 20 63  or triggers.** c
1be30 6f 6e 74 61 69 6e 20 61 20 55 54 46 2d 38 20 53  ontain a UTF-8 S
1be40 51 4c 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 20  QL comment that 
1be50 69 64 65 6e 74 69 66 69 65 73 20 74 68 65 20 74  identifies the t
1be60 72 69 67 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  rigger..**.** Th
1be70 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74  e callback funct
1be80 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20 62  ion registered b
1be90 79 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c  y sqlite3_profil
1bea0 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a  e() is invoked.*
1beb0 2a 20 61 73 20 65 61 63 68 20 53 51 4c 20 73 74  * as each SQL st
1bec0 61 74 65 6d 65 6e 74 20 66 69 6e 69 73 68 65 73  atement finishes
1bed0 2e 20 20 54 68 65 20 70 72 6f 66 69 6c 65 20 63  .  The profile c
1bee0 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 73  allback contains
1bef0 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  .** the original
1bf00 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74 20   statement text 
1bf10 61 6e 64 20 61 6e 20 65 73 74 69 6d 61 74 65 20  and an estimate 
1bf20 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74 69  of wall-clock ti
1bf30 6d 65 0a 2a 2a 20 6f 66 20 68 6f 77 20 6c 6f 6e  me.** of how lon
1bf40 67 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74  g that statement
1bf50 20 74 6f 6f 6b 20 74 6f 20 72 75 6e 2e 0a 2a 2a   took to run..**
1bf60 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
1bf70 3a 0a 2a 2a 20 5b 48 31 32 32 38 31 5d 20 5b 48  :.** [H12281] [H
1bf80 31 32 32 38 32 5d 20 5b 48 31 32 32 38 33 5d 20  12282] [H12283] 
1bf90 5b 48 31 32 32 38 34 5d 20 5b 48 31 32 32 38 35  [H12284] [H12285
1bfa0 5d 20 5b 48 31 32 32 38 37 5d 20 5b 48 31 32 32  ] [H12287] [H122
1bfb0 38 38 5d 20 5b 48 31 32 32 38 39 5d 0a 2a 2a 20  88] [H12289].** 
1bfc0 5b 48 31 32 32 39 30 5d 0a 2a 2f 0a 53 51 4c 49  [H12290].*/.SQLI
1bfd0 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58  TE_API SQLITE_EX
1bfe0 50 45 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20  PERIMENTAL void 
1bff0 2a 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 73  *sqlite3_trace(s
1c000 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 78  qlite3*, void(*x
1c010 54 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e  Trace)(void*,con
1c020 73 74 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 2a  st char*), void*
1c030 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  );.SQLITE_API SQ
1c040 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41  LITE_EXPERIMENTA
1c050 4c 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  L void *sqlite3_
1c060 70 72 6f 66 69 6c 65 28 73 71 6c 69 74 65 33 2a  profile(sqlite3*
1c070 2c 0a 20 20 20 76 6f 69 64 28 2a 78 50 72 6f 66  ,.   void(*xProf
1c080 69 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74  ile)(void*,const
1c090 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 75   char*,sqlite3_u
1c0a0 69 6e 74 36 34 29 2c 20 76 6f 69 64 2a 29 3b 0a  int64), void*);.
1c0b0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
1c0c0 20 51 75 65 72 79 20 50 72 6f 67 72 65 73 73 20   Query Progress 
1c0d0 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39 31  Callbacks {H1291
1c0e0 30 7d 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a  0} <S60400>.**.*
1c0f0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
1c100 6f 6e 66 69 67 75 72 65 73 20 61 20 63 61 6c 6c  onfigures a call
1c110 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2d 20  back function - 
1c120 74 68 65 0a 2a 2a 20 70 72 6f 67 72 65 73 73 20  the.** progress 
1c130 63 61 6c 6c 62 61 63 6b 20 2d 20 74 68 61 74 20  callback - that 
1c140 69 73 20 69 6e 76 6f 6b 65 64 20 70 65 72 69 6f  is invoked perio
1c150 64 69 63 61 6c 6c 79 20 64 75 72 69 6e 67 20 6c  dically during l
1c160 6f 6e 67 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 63  ong.** running c
1c170 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33  alls to [sqlite3
1c180 5f 65 78 65 63 28 29 5d 2c 20 5b 73 71 6c 69 74  _exec()], [sqlit
1c190 65 33 5f 73 74 65 70 28 29 5d 20 61 6e 64 0a 2a  e3_step()] and.*
1c1a0 2a 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  * [sqlite3_get_t
1c1b0 61 62 6c 65 28 29 5d 2e 20 20 41 6e 20 65 78 61  able()].  An exa
1c1c0 6d 70 6c 65 20 75 73 65 20 66 6f 72 20 74 68 69  mple use for thi
1c1d0 73 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69  s.** interface i
1c1e0 73 20 74 6f 20 6b 65 65 70 20 61 20 47 55 49 20  s to keep a GUI 
1c1f0 75 70 64 61 74 65 64 20 64 75 72 69 6e 67 20 61  updated during a
1c200 20 6c 61 72 67 65 20 71 75 65 72 79 2e 0a 2a 2a   large query..**
1c210 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72  .** If the progr
1c220 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74  ess callback ret
1c230 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  urns non-zero, t
1c240 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a  he operation is.
1c250 2a 2a 20 69 6e 74 65 72 72 75 70 74 65 64 2e 20  ** interrupted. 
1c260 20 54 68 69 73 20 66 65 61 74 75 72 65 20 63 61   This feature ca
1c270 6e 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70  n be used to imp
1c280 6c 65 6d 65 6e 74 20 61 0a 2a 2a 20 22 43 61 6e  lement a.** "Can
1c290 63 65 6c 22 20 62 75 74 74 6f 6e 20 6f 6e 20 61  cel" button on a
1c2a0 20 47 55 49 20 70 72 6f 67 72 65 73 73 20 64 69   GUI progress di
1c2b0 61 6c 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a 2a 20  alog box..**.** 
1c2c0 54 68 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e  The progress han
1c2d0 64 6c 65 72 20 6d 75 73 74 20 6e 6f 74 20 64 6f  dler must not do
1c2e0 20 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77   anything that w
1c2f0 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68  ill modify.** th
1c300 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
1c310 63 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b  ction that invok
1c320 65 64 20 74 68 65 20 70 72 6f 67 72 65 73 73 20  ed the progress 
1c330 68 61 6e 64 6c 65 72 2e 0a 2a 2a 20 4e 6f 74 65  handler..** Note
1c340 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70   that [sqlite3_p
1c350 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64  repare_v2()] and
1c360 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
1c370 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68  ] both modify th
1c380 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  eir.** database 
1c390 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20  connections for 
1c3a0 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22  the meaning of "
1c3b0 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20  modify" in this 
1c3c0 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a  paragraph..**.**
1c3d0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
1c3e0 2a 20 5b 48 31 32 39 31 31 5d 20 5b 48 31 32 39  * [H12911] [H129
1c3f0 31 32 5d 20 5b 48 31 32 39 31 33 5d 20 5b 48 31  12] [H12913] [H1
1c400 32 39 31 34 5d 20 5b 48 31 32 39 31 35 5d 20 5b  2914] [H12915] [
1c410 48 31 32 39 31 36 5d 20 5b 48 31 32 39 31 37 5d  H12916] [H12917]
1c420 20 5b 48 31 32 39 31 38 5d 0a 2a 2a 0a 2a 2f 0a   [H12918].**.*/.
1c430 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
1c440 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73  sqlite3_progress
1c450 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65 33  _handler(sqlite3
1c460 2a 2c 20 69 6e 74 2c 20 69 6e 74 28 2a 29 28 76  *, int, int(*)(v
1c470 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a  oid*), void*);..
1c480 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
1c490 4f 70 65 6e 69 6e 67 20 41 20 4e 65 77 20 44 61  Opening A New Da
1c4a0 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f  tabase Connectio
1c4b0 6e 20 7b 48 31 32 37 30 30 7d 20 3c 53 34 30 32  n {H12700} <S402
1c4c0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  00>.**.** These 
1c4d0 72 6f 75 74 69 6e 65 73 20 6f 70 65 6e 20 61 6e  routines open an
1c4e0 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73 65   SQLite database
1c4f0 20 66 69 6c 65 20 77 68 6f 73 65 20 6e 61 6d 65   file whose name
1c500 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 65   is given by the
1c510 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 61 72 67  .** filename arg
1c520 75 6d 65 6e 74 2e 20 54 68 65 20 66 69 6c 65 6e  ument. The filen
1c530 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ame argument is 
1c540 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 55  interpreted as U
1c550 54 46 2d 38 20 66 6f 72 0a 2a 2a 20 73 71 6c 69  TF-8 for.** sqli
1c560 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73  te3_open() and s
1c570 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
1c580 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36 20 69   and as UTF-16 i
1c590 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79 74  n the native byt
1c5a0 65 0a 2a 2a 20 6f 72 64 65 72 20 66 6f 72 20 73  e.** order for s
1c5b0 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 2e  qlite3_open16().
1c5c0 20 41 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e   A [database con
1c5d0 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 20  nection] handle 
1c5e0 69 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 72 65  is usually.** re
1c5f0 74 75 72 6e 65 64 20 69 6e 20 2a 70 70 44 62 2c  turned in *ppDb,
1c600 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f   even if an erro
1c610 72 20 6f 63 63 75 72 73 2e 20 20 54 68 65 20 6f  r occurs.  The o
1c620 6e 6c 79 20 65 78 63 65 70 74 69 6f 6e 20 69 73  nly exception is
1c630 20 74 68 61 74 0a 2a 2a 20 69 66 20 53 51 4c 69   that.** if SQLi
1c640 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f 20  te is unable to 
1c650 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20  allocate memory 
1c660 74 6f 20 68 6f 6c 64 20 74 68 65 20 5b 73 71 6c  to hold the [sql
1c670 69 74 65 33 5d 20 6f 62 6a 65 63 74 2c 0a 2a 2a  ite3] object,.**
1c680 20 61 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 20   a NULL will be 
1c690 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 70  written into *pp
1c6a0 44 62 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20  Db instead of a 
1c6b0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b  pointer to the [
1c6c0 73 71 6c 69 74 65 33 5d 0a 2a 2a 20 6f 62 6a 65  sqlite3].** obje
1c6d0 63 74 2e 20 49 66 20 74 68 65 20 64 61 74 61 62  ct. If the datab
1c6e0 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 28 61  ase is opened (a
1c6f0 6e 64 2f 6f 72 20 63 72 65 61 74 65 64 29 20 73  nd/or created) s
1c700 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 74 68 65  uccessfully, the
1c710 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d  n.** [SQLITE_OK]
1c720 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 4f   is returned.  O
1c730 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72  therwise an [err
1c740 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75  or code] is retu
1c750 72 6e 65 64 2e 20 20 54 68 65 0a 2a 2a 20 5b 73  rned.  The.** [s
1c760 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d  qlite3_errmsg()]
1c770 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72   or [sqlite3_err
1c780 6d 73 67 31 36 28 29 5d 20 72 6f 75 74 69 6e 65  msg16()] routine
1c790 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  s can be used to
1c7a0 20 6f 62 74 61 69 6e 0a 2a 2a 20 61 6e 20 45 6e   obtain.** an En
1c7b0 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20 64  glish language d
1c7c0 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68  escription of th
1c7d0 65 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  e error..**.** T
1c7e0 68 65 20 64 65 66 61 75 6c 74 20 65 6e 63 6f 64  he default encod
1c7f0 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74 61  ing for the data
1c800 62 61 73 65 20 77 69 6c 6c 20 62 65 20 55 54 46  base will be UTF
1c810 2d 38 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33  -8 if.** sqlite3
1c820 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74  _open() or sqlit
1c830 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73 20  e3_open_v2() is 
1c840 63 61 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 55 54  called and.** UT
1c850 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74 69  F-16 in the nati
1c860 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 69 66  ve byte order if
1c870 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28   sqlite3_open16(
1c880 29 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a  ) is used..**.**
1c890 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   Whether or not 
1c8a0 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20  an error occurs 
1c8b0 77 68 65 6e 20 69 74 20 69 73 20 6f 70 65 6e 65  when it is opene
1c8c0 64 2c 20 72 65 73 6f 75 72 63 65 73 0a 2a 2a 20  d, resources.** 
1c8d0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
1c8e0 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  the [database co
1c8f0 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65  nnection] handle
1c900 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6c 65 61   should be relea
1c910 73 65 64 20 62 79 0a 2a 2a 20 70 61 73 73 69 6e  sed by.** passin
1c920 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65 33  g it to [sqlite3
1c930 5f 63 6c 6f 73 65 28 29 5d 20 77 68 65 6e 20 69  _close()] when i
1c940 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72  t is no longer r
1c950 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  equired..**.** T
1c960 68 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  he sqlite3_open_
1c970 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 20 77  v2() interface w
1c980 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74 65  orks like sqlite
1c990 33 5f 6f 70 65 6e 28 29 0a 2a 2a 20 65 78 63 65  3_open().** exce
1c9a0 70 74 20 74 68 61 74 20 69 74 20 61 63 63 65 70  pt that it accep
1c9b0 74 73 20 74 77 6f 20 61 64 64 69 74 69 6f 6e 61  ts two additiona
1c9c0 6c 20 70 61 72 61 6d 65 74 65 72 73 20 66 6f 72  l parameters for
1c9d0 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6e 74   additional cont
1c9e0 72 6f 6c 0a 2a 2a 20 6f 76 65 72 20 74 68 65 20  rol.** over the 
1c9f0 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e  new database con
1ca00 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 66 6c  nection.  The fl
1ca10 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 63 61  ags parameter ca
1ca20 6e 20 74 61 6b 65 20 6f 6e 65 20 6f 66 0a 2a 2a  n take one of.**
1ca30 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   the following t
1ca40 68 72 65 65 20 76 61 6c 75 65 73 2c 20 6f 70 74  hree values, opt
1ca50 69 6f 6e 61 6c 6c 79 20 63 6f 6d 62 69 6e 65 64  ionally combined
1ca60 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 5b 53   with the .** [S
1ca70 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54  QLITE_OPEN_NOMUT
1ca80 45 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 50 45  EX], [SQLITE_OPE
1ca90 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 2c 20 5b 53  N_FULLMUTEX], [S
1caa0 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45  QLITE_OPEN_SHARE
1cab0 44 43 41 43 48 45 5d 2c 0a 2a 2a 20 61 6e 64 2f  DCACHE],.** and/
1cac0 6f 72 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  or [SQLITE_OPEN_
1cad0 50 52 49 56 41 54 45 43 41 43 48 45 5d 20 66 6c  PRIVATECACHE] fl
1cae0 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a  ags:.**.** <dl>.
1caf0 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f  ** <dt>[SQLITE_O
1cb00 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 3c 2f 64  PEN_READONLY]</d
1cb10 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61  t>.** <dd>The da
1cb20 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64  tabase is opened
1cb30 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f   in read-only mo
1cb40 64 65 2e 20 20 49 66 20 74 68 65 20 64 61 74 61  de.  If the data
1cb50 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a  base does not.**
1cb60 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2c 20   already exist, 
1cb70 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75  an error is retu
1cb80 72 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  rned.</dd>.**.**
1cb90 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45   <dt>[SQLITE_OPE
1cba0 4e 5f 52 45 41 44 57 52 49 54 45 5d 3c 2f 64 74  N_READWRITE]</dt
1cbb0 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64 61 74  >.** <dd>The dat
1cbc0 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20  abase is opened 
1cbd0 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64 20  for reading and 
1cbe0 77 72 69 74 69 6e 67 20 69 66 20 70 6f 73 73 69  writing if possi
1cbf0 62 6c 65 2c 20 6f 72 20 72 65 61 64 69 6e 67 0a  ble, or reading.
1cc00 2a 2a 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 66  ** only if the f
1cc10 69 6c 65 20 69 73 20 77 72 69 74 65 20 70 72 6f  ile is write pro
1cc20 74 65 63 74 65 64 20 62 79 20 74 68 65 20 6f 70  tected by the op
1cc30 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20  erating system. 
1cc40 20 49 6e 20 65 69 74 68 65 72 0a 2a 2a 20 63 61   In either.** ca
1cc50 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  se the database 
1cc60 6d 75 73 74 20 61 6c 72 65 61 64 79 20 65 78 69  must already exi
1cc70 73 74 2c 20 6f 74 68 65 72 77 69 73 65 20 61 6e  st, otherwise an
1cc80 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e   error is return
1cc90 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  ed.</dd>.**.** <
1cca0 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  dt>[SQLITE_OPEN_
1ccb0 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b 53 51  READWRITE] | [SQ
1ccc0 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45  LITE_OPEN_CREATE
1ccd0 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  ]</dt>.** <dd>Th
1cce0 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70  e database is op
1ccf0 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67  ened for reading
1cd00 20 61 6e 64 20 77 72 69 74 69 6e 67 2c 20 61 6e   and writing, an
1cd10 64 20 69 73 20 63 72 65 61 74 65 73 20 69 74 20  d is creates it 
1cd20 69 66 0a 2a 2a 20 69 74 20 64 6f 65 73 20 6e 6f  if.** it does no
1cd30 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 2e  t already exist.
1cd40 20 54 68 69 73 20 69 73 20 74 68 65 20 62 65 68   This is the beh
1cd50 61 76 69 6f 72 20 74 68 61 74 20 69 73 20 61 6c  avior that is al
1cd60 77 61 79 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a  ways used for.**
1cd70 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20   sqlite3_open() 
1cd80 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  and sqlite3_open
1cd90 31 36 28 29 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f  16().</dd>.** </
1cda0 64 6c 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  dl>.**.** If the
1cdb0 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74   3rd parameter t
1cdc0 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76  o sqlite3_open_v
1cdd0 32 28 29 20 69 73 20 6e 6f 74 20 6f 6e 65 20 6f  2() is not one o
1cde0 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 62 69 6e 61  f the.** combina
1cdf0 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62 6f 76  tions shown abov
1ce00 65 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20  e or one of the 
1ce10 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73 68 6f  combinations sho
1ce20 77 6e 20 61 62 6f 76 65 20 63 6f 6d 62 69 6e 65  wn above combine
1ce30 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 5b 53  d.** with the [S
1ce40 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54  QLITE_OPEN_NOMUT
1ce50 45 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 50 45  EX], [SQLITE_OPE
1ce60 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 2c 0a 2a 2a  N_FULLMUTEX],.**
1ce70 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48   [SQLITE_OPEN_SH
1ce80 41 52 45 44 43 41 43 48 45 5d 20 61 6e 64 2f 6f  AREDCACHE] and/o
1ce90 72 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53  r [SQLITE_OPEN_S
1cea0 48 41 52 45 44 43 41 43 48 45 5d 20 66 6c 61 67  HAREDCACHE] flag
1ceb0 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 62  s,.** then the b
1cec0 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66  ehavior is undef
1ced0 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ined..**.** If t
1cee0 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  he [SQLITE_OPEN_
1cef0 4e 4f 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73  NOMUTEX] flag is
1cf00 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 64   set, then the d
1cf10 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1cf20 6f 6e 0a 2a 2a 20 6f 70 65 6e 73 20 69 6e 20 74  on.** opens in t
1cf30 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 20  he multi-thread 
1cf40 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d  [threading mode]
1cf50 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20   as long as the 
1cf60 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 0a 2a 2a  single-thread.**
1cf70 20 6d 6f 64 65 20 68 61 73 20 6e 6f 74 20 62 65   mode has not be
1cf80 65 6e 20 73 65 74 20 61 74 20 63 6f 6d 70 69 6c  en set at compil
1cf90 65 2d 74 69 6d 65 20 6f 72 20 73 74 61 72 74 2d  e-time or start-
1cfa0 74 69 6d 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a  time.  If the.**
1cfb0 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55   [SQLITE_OPEN_FU
1cfc0 4c 4c 4d 55 54 45 58 5d 20 66 6c 61 67 20 69 73  LLMUTEX] flag is
1cfd0 20 73 65 74 20 74 68 65 6e 20 74 68 65 20 64 61   set then the da
1cfe0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1cff0 6e 20 6f 70 65 6e 73 0a 2a 2a 20 69 6e 20 74 68  n opens.** in th
1d000 65 20 73 65 72 69 61 6c 69 7a 65 64 20 5b 74 68  e serialized [th
1d010 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 75 6e  reading mode] un
1d020 6c 65 73 73 20 73 69 6e 67 6c 65 2d 74 68 72 65  less single-thre
1d030 61 64 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f  ad was.** previo
1d040 75 73 6c 79 20 73 65 6c 65 63 74 65 64 20 61 74  usly selected at
1d050 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72   compile-time or
1d060 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 20   start-time..** 
1d070 54 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  The [SQLITE_OPEN
1d080 5f 53 48 41 52 45 44 43 41 43 48 45 5d 20 66 6c  _SHAREDCACHE] fl
1d090 61 67 20 63 61 75 73 65 73 20 74 68 65 20 64 61  ag causes the da
1d0a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1d0b0 6e 20 74 6f 20 62 65 0a 2a 2a 20 65 6c 69 67 69  n to be.** eligi
1d0c0 62 6c 65 20 74 6f 20 75 73 65 20 5b 73 68 61 72  ble to use [shar
1d0d0 65 64 20 63 61 63 68 65 20 6d 6f 64 65 5d 2c 20  ed cache mode], 
1d0e0 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68  regardless of wh
1d0f0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 73 68 61  ether or not sha
1d100 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 69 73 20  red.** cache is 
1d110 65 6e 61 62 6c 65 64 20 75 73 69 6e 67 20 5b 73  enabled using [s
1d120 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68  qlite3_enable_sh
1d130 61 72 65 64 5f 63 61 63 68 65 28 29 5d 2e 20 20  ared_cache()].  
1d140 54 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f  The.** [SQLITE_O
1d150 50 45 4e 5f 50 52 49 56 41 54 45 43 41 43 48 45  PEN_PRIVATECACHE
1d160 5d 20 66 6c 61 67 20 63 61 75 73 65 73 20 74 68  ] flag causes th
1d170 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
1d180 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 0a 2a 2a 20  ction to not.** 
1d190 70 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 5b  participate in [
1d1a0 73 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64  shared cache mod
1d1b0 65 5d 20 65 76 65 6e 20 69 66 20 69 74 20 69 73  e] even if it is
1d1c0 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20   enabled..**.** 
1d1d0 49 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20  If the filename 
1d1e0 69 73 20 22 3a 6d 65 6d 6f 72 79 3a 22 2c 20 74  is ":memory:", t
1d1f0 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c 20 74  hen a private, t
1d200 65 6d 70 6f 72 61 72 79 20 69 6e 2d 6d 65 6d 6f  emporary in-memo
1d210 72 79 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69  ry database.** i
1d220 73 20 63 72 65 61 74 65 64 20 66 6f 72 20 74 68  s created for th
1d230 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54  e connection.  T
1d240 68 69 73 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61  his in-memory da
1d250 74 61 62 61 73 65 20 77 69 6c 6c 20 76 61 6e 69  tabase will vani
1d260 73 68 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 64  sh when.** the d
1d270 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
1d280 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 46  on is closed.  F
1d290 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f  uture versions o
1d2a0 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74 0a 2a  f SQLite might.*
1d2b0 2a 20 6d 61 6b 65 20 75 73 65 20 6f 66 20 61 64  * make use of ad
1d2c0 64 69 74 69 6f 6e 61 6c 20 73 70 65 63 69 61 6c  ditional special
1d2d0 20 66 69 6c 65 6e 61 6d 65 73 20 74 68 61 74 20   filenames that 
1d2e0 62 65 67 69 6e 20 77 69 74 68 20 74 68 65 20 22  begin with the "
1d2f0 3a 22 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a  :" character..**
1d300 20 49 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64   It is recommend
1d310 65 64 20 74 68 61 74 20 77 68 65 6e 20 61 20 64  ed that when a d
1d320 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65  atabase filename
1d330 20 61 63 74 75 61 6c 6c 79 20 64 6f 65 73 20 62   actually does b
1d340 65 67 69 6e 20 77 69 74 68 0a 2a 2a 20 61 20 22  egin with.** a "
1d350 3a 22 20 63 68 61 72 61 63 74 65 72 20 79 6f 75  :" character you
1d360 20 73 68 6f 75 6c 64 20 70 72 65 66 69 78 20 74   should prefix t
1d370 68 65 20 66 69 6c 65 6e 61 6d 65 20 77 69 74 68  he filename with
1d380 20 61 20 70 61 74 68 6e 61 6d 65 20 73 75 63 68   a pathname such
1d390 20 61 73 0a 2a 2a 20 22 2e 2f 22 20 74 6f 20 61   as.** "./" to a
1d3a0 76 6f 69 64 20 61 6d 62 69 67 75 69 74 79 2e 0a  void ambiguity..
1d3b0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69 6c  **.** If the fil
1d3c0 65 6e 61 6d 65 20 69 73 20 61 6e 20 65 6d 70 74  ename is an empt
1d3d0 79 20 73 74 72 69 6e 67 2c 20 74 68 65 6e 20 61  y string, then a
1d3e0 20 70 72 69 76 61 74 65 2c 20 74 65 6d 70 6f 72   private, tempor
1d3f0 61 72 79 0a 2a 2a 20 6f 6e 2d 64 69 73 6b 20 64  ary.** on-disk d
1d400 61 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20  atabase will be 
1d410 63 72 65 61 74 65 64 2e 20 20 54 68 69 73 20 70  created.  This p
1d420 72 69 76 61 74 65 20 64 61 74 61 62 61 73 65 20  rivate database 
1d430 77 69 6c 6c 20 62 65 0a 2a 2a 20 61 75 74 6f 6d  will be.** autom
1d440 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64  atically deleted
1d450 20 61 73 20 73 6f 6f 6e 20 61 73 20 74 68 65 20   as soon as the 
1d460 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1d470 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a  ion is closed..*
1d480 2a 0a 2a 2a 20 54 68 65 20 66 6f 75 72 74 68 20  *.** The fourth 
1d490 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c  parameter to sql
1d4a0 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69  ite3_open_v2() i
1d4b0 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
1d4c0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 76 66  e.** [sqlite3_vf
1d4d0 73 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20 64  s] object that d
1d4e0 65 66 69 6e 65 73 20 74 68 65 20 6f 70 65 72 61  efines the opera
1d4f0 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65  ting system inte
1d500 72 66 61 63 65 20 74 68 61 74 0a 2a 2a 20 74 68  rface that.** th
1d510 65 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 63  e new database c
1d520 6f 6e 6e 65 63 74 69 6f 6e 20 73 68 6f 75 6c 64  onnection should
1d530 20 75 73 65 2e 20 20 49 66 20 74 68 65 20 66 6f   use.  If the fo
1d540 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 20 69  urth parameter i
1d550 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  s.** a NULL poin
1d560 74 65 72 20 74 68 65 6e 20 74 68 65 20 64 65 66  ter then the def
1d570 61 75 6c 74 20 5b 73 71 6c 69 74 65 33 5f 76 66  ault [sqlite3_vf
1d580 73 5d 20 6f 62 6a 65 63 74 20 69 73 20 75 73 65  s] object is use
1d590 64 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 4e 6f 74 65  d..**.** <b>Note
1d5a0 20 74 6f 20 57 69 6e 64 6f 77 73 20 75 73 65 72   to Windows user
1d5b0 73 3a 3c 2f 62 3e 20 20 54 68 65 20 65 6e 63 6f  s:</b>  The enco
1d5c0 64 69 6e 67 20 75 73 65 64 20 66 6f 72 20 74 68  ding used for th
1d5d0 65 20 66 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d  e filename argum
1d5e0 65 6e 74 0a 2a 2a 20 6f 66 20 73 71 6c 69 74 65  ent.** of sqlite
1d5f0 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20 73 71 6c  3_open() and sql
1d600 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 6d  ite3_open_v2() m
1d610 75 73 74 20 62 65 20 55 54 46 2d 38 2c 20 6e 6f  ust be UTF-8, no
1d620 74 20 77 68 61 74 65 76 65 72 0a 2a 2a 20 63 6f  t whatever.** co
1d630 64 65 70 61 67 65 20 69 73 20 63 75 72 72 65 6e  depage is curren
1d640 74 6c 79 20 64 65 66 69 6e 65 64 2e 20 20 46 69  tly defined.  Fi
1d650 6c 65 6e 61 6d 65 73 20 63 6f 6e 74 61 69 6e 69  lenames containi
1d660 6e 67 20 69 6e 74 65 72 6e 61 74 69 6f 6e 61 6c  ng international
1d670 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73 20 6d  .** characters m
1d680 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64  ust be converted
1d690 20 74 6f 20 55 54 46 2d 38 20 70 72 69 6f 72 20   to UTF-8 prior 
1d6a0 74 6f 20 70 61 73 73 69 6e 67 20 74 68 65 6d 20  to passing them 
1d6b0 69 6e 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  into.** sqlite3_
1d6c0 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69 74 65  open() or sqlite
1d6d0 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2a 0a  3_open_v2()..**.
1d6e0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
1d6f0 0a 2a 2a 20 5b 48 31 32 37 30 31 5d 20 5b 48 31  .** [H12701] [H1
1d700 32 37 30 32 5d 20 5b 48 31 32 37 30 33 5d 20 5b  2702] [H12703] [
1d710 48 31 32 37 30 34 5d 20 5b 48 31 32 37 30 36 5d  H12704] [H12706]
1d720 20 5b 48 31 32 37 30 37 5d 20 5b 48 31 32 37 30   [H12707] [H1270
1d730 39 5d 20 5b 48 31 32 37 31 31 5d 0a 2a 2a 20 5b  9] [H12711].** [
1d740 48 31 32 37 31 32 5d 20 5b 48 31 32 37 31 33 5d  H12712] [H12713]
1d750 20 5b 48 31 32 37 31 34 5d 20 5b 48 31 32 37 31   [H12714] [H1271
1d760 37 5d 20 5b 48 31 32 37 31 39 5d 20 5b 48 31 32  7] [H12719] [H12
1d770 37 32 31 5d 20 5b 48 31 32 37 32 33 5d 0a 2a 2f  721] [H12723].*/
1d780 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1d790 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 0a 20 20  sqlite3_open(.  
1d7a0 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65  const char *file
1d7b0 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62  name,   /* Datab
1d7c0 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54  ase filename (UT
1d7d0 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65  F-8) */.  sqlite
1d7e0 33 20 2a 2a 70 70 44 62 20 20 20 20 20 20 20 20  3 **ppDb        
1d7f0 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65    /* OUT: SQLite
1d800 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 29 3b   db handle */.);
1d810 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1d820 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 0a  sqlite3_open16(.
1d830 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 66 69    const void *fi
1d840 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74  lename,   /* Dat
1d850 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 28  abase filename (
1d860 55 54 46 2d 31 36 29 20 2a 2f 0a 20 20 73 71 6c  UTF-16) */.  sql
1d870 69 74 65 33 20 2a 2a 70 70 44 62 20 20 20 20 20  ite3 **ppDb     
1d880 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c       /* OUT: SQL
1d890 69 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f  ite db handle */
1d8a0 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .);.SQLITE_API i
1d8b0 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  nt sqlite3_open_
1d8c0 76 32 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  v2(.  const char
1d8d0 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a   *filename,   /*
1d8e0 20 44 61 74 61 62 61 73 65 20 66 69 6c 65 6e 61   Database filena
1d8f0 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f 0a 20 20  me (UTF-8) */.  
1d900 73 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 2c 20  sqlite3 **ppDb, 
1d910 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
1d920 53 51 4c 69 74 65 20 64 62 20 68 61 6e 64 6c 65  SQLite db handle
1d930 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c   */.  int flags,
1d940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1d950 20 46 6c 61 67 73 20 2a 2f 0a 20 20 63 6f 6e 73   Flags */.  cons
1d960 74 20 63 68 61 72 20 2a 7a 56 66 73 20 20 20 20  t char *zVfs    
1d970 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 56      /* Name of V
1d980 46 53 20 6d 6f 64 75 6c 65 20 74 6f 20 75 73 65  FS module to use
1d990 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   */.);../*.** CA
1d9a0 50 49 33 52 45 46 3a 20 45 72 72 6f 72 20 43 6f  PI3REF: Error Co
1d9b0 64 65 73 20 41 6e 64 20 4d 65 73 73 61 67 65 73  des And Messages
1d9c0 20 7b 48 31 32 38 30 30 7d 20 3c 53 36 30 32 30   {H12800} <S6020
1d9d0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  0>.**.** The sql
1d9e0 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 20 69  ite3_errcode() i
1d9f0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
1da00 20 74 68 65 20 6e 75 6d 65 72 69 63 20 5b 72 65   the numeric [re
1da10 73 75 6c 74 20 63 6f 64 65 5d 20 6f 72 0a 2a 2a  sult code] or.**
1da20 20 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c   [extended resul
1da30 74 20 63 6f 64 65 5d 20 66 6f 72 20 74 68 65 20  t code] for the 
1da40 6d 6f 73 74 20 72 65 63 65 6e 74 20 66 61 69 6c  most recent fail
1da50 65 64 20 73 71 6c 69 74 65 33 5f 2a 20 41 50 49  ed sqlite3_* API
1da60 20 63 61 6c 6c 0a 2a 2a 20 61 73 73 6f 63 69 61   call.** associa
1da70 74 65 64 20 77 69 74 68 20 61 20 5b 64 61 74 61  ted with a [data
1da80 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
1da90 2e 20 49 66 20 61 20 70 72 69 6f 72 20 41 50 49  . If a prior API
1daa0 20 63 61 6c 6c 20 66 61 69 6c 65 64 0a 2a 2a 20   call failed.** 
1dab0 62 75 74 20 74 68 65 20 6d 6f 73 74 20 72 65 63  but the most rec
1dac0 65 6e 74 20 41 50 49 20 63 61 6c 6c 20 73 75 63  ent API call suc
1dad0 63 65 65 64 65 64 2c 20 74 68 65 20 72 65 74 75  ceeded, the retu
1dae0 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d 0a 2a 2a  rn value from.**
1daf0 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65   sqlite3_errcode
1db00 28 29 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  () is undefined.
1db10 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 78    The sqlite3_ex
1db20 74 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 29  tended_errcode()
1db30 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73  .** interface is
1db40 20 74 68 65 20 73 61 6d 65 20 65 78 63 65 70 74   the same except
1db50 20 74 68 61 74 20 69 74 20 61 6c 77 61 79 73 20   that it always 
1db60 72 65 74 75 72 6e 73 20 74 68 65 20 0a 2a 2a 20  returns the .** 
1db70 5b 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74  [extended result
1db80 20 63 6f 64 65 5d 20 65 76 65 6e 20 77 68 65 6e   code] even when
1db90 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74   extended result
1dba0 20 63 6f 64 65 73 20 61 72 65 0a 2a 2a 20 64 69   codes are.** di
1dbb0 73 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  sabled..**.** Th
1dbc0 65 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  e sqlite3_errmsg
1dbd0 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 65  () and sqlite3_e
1dbe0 72 72 6d 73 67 31 36 28 29 20 72 65 74 75 72 6e  rrmsg16() return
1dbf0 20 45 6e 67 6c 69 73 68 2d 6c 61 6e 67 75 61 67   English-languag
1dc00 65 0a 2a 2a 20 74 65 78 74 20 74 68 61 74 20 64  e.** text that d
1dc10 65 73 63 72 69 62 65 73 20 74 68 65 20 65 72 72  escribes the err
1dc20 6f 72 2c 20 61 73 20 65 69 74 68 65 72 20 55 54  or, as either UT
1dc30 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 72 65  F-8 or UTF-16 re
1dc40 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 4d  spectively..** M
1dc50 65 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68  emory to hold th
1dc60 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
1dc70 73 74 72 69 6e 67 20 69 73 20 6d 61 6e 61 67 65  string is manage
1dc80 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a 2a  d internally..**
1dc90 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   The application
1dca0 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
1dcb0 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20 66 72  o worry about fr
1dcc0 65 65 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74  eeing the result
1dcd0 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68  ..** However, th
1dce0 65 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 6d  e error string m
1dcf0 69 67 68 74 20 62 65 20 6f 76 65 72 77 72 69 74  ight be overwrit
1dd00 74 65 6e 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74  ten or deallocat
1dd10 65 64 20 62 79 0a 2a 2a 20 73 75 62 73 65 71 75  ed by.** subsequ
1dd20 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 6f 74 68  ent calls to oth
1dd30 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66  er SQLite interf
1dd40 61 63 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a  ace functions..*
1dd50 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 73 65  *.** When the se
1dd60 72 69 61 6c 69 7a 65 64 20 5b 74 68 72 65 61 64  rialized [thread
1dd70 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 69 6e 20  ing mode] is in 
1dd80 75 73 65 2c 20 69 74 20 6d 69 67 68 74 20 62 65  use, it might be
1dd90 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 74 68 61   the.** case tha
1dda0 74 20 61 20 73 65 63 6f 6e 64 20 65 72 72 6f 72  t a second error
1ddb0 20 6f 63 63 75 72 73 20 6f 6e 20 61 20 73 65 70   occurs on a sep
1ddc0 61 72 61 74 65 20 74 68 72 65 61 64 20 69 6e 20  arate thread in 
1ddd0 62 65 74 77 65 65 6e 0a 2a 2a 20 74 68 65 20 74  between.** the t
1dde0 69 6d 65 20 6f 66 20 74 68 65 20 66 69 72 73 74  ime of the first
1ddf0 20 65 72 72 6f 72 20 61 6e 64 20 74 68 65 20 63   error and the c
1de00 61 6c 6c 20 74 6f 20 74 68 65 73 65 20 69 6e 74  all to these int
1de10 65 72 66 61 63 65 73 2e 0a 2a 2a 20 57 68 65 6e  erfaces..** When
1de20 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c 20 74   that happens, t
1de30 68 65 20 73 65 63 6f 6e 64 20 65 72 72 6f 72 20  he second error 
1de40 77 69 6c 6c 20 62 65 20 72 65 70 6f 72 74 65 64  will be reported
1de50 20 73 69 6e 63 65 20 74 68 65 73 65 0a 2a 2a 20   since these.** 
1de60 69 6e 74 65 72 66 61 63 65 73 20 61 6c 77 61 79  interfaces alway
1de70 73 20 72 65 70 6f 72 74 20 74 68 65 20 6d 6f 73  s report the mos
1de80 74 20 72 65 63 65 6e 74 20 72 65 73 75 6c 74 2e  t recent result.
1de90 20 20 54 6f 20 61 76 6f 69 64 0a 2a 2a 20 74 68    To avoid.** th
1dea0 69 73 2c 20 65 61 63 68 20 74 68 72 65 61 64 20  is, each thread 
1deb0 63 61 6e 20 6f 62 74 61 69 6e 20 65 78 63 6c 75  can obtain exclu
1dec0 73 69 76 65 20 75 73 65 20 6f 66 20 74 68 65 20  sive use of the 
1ded0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
1dee0 74 69 6f 6e 5d 20 44 0a 2a 2a 20 62 79 20 69 6e  tion] D.** by in
1def0 76 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f  voking [sqlite3_
1df00 6d 75 74 65 78 5f 65 6e 74 65 72 5d 28 5b 73 71  mutex_enter]([sq
1df10 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28  lite3_db_mutex](
1df20 44 29 29 20 62 65 66 6f 72 65 20 62 65 67 69 6e  D)) before begin
1df30 6e 69 6e 67 0a 2a 2a 20 74 6f 20 75 73 65 20 44  ning.** to use D
1df40 20 61 6e 64 20 69 6e 76 6f 6b 69 6e 67 20 5b 73   and invoking [s
1df50 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1df60 76 65 5d 28 5b 73 71 6c 69 74 65 33 5f 64 62 5f  ve]([sqlite3_db_
1df70 6d 75 74 65 78 5d 28 44 29 29 20 61 66 74 65 72  mutex](D)) after
1df80 0a 2a 2a 20 61 6c 6c 20 63 61 6c 6c 73 20 74 6f  .** all calls to
1df90 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 73 20   the interfaces 
1dfa0 6c 69 73 74 65 64 20 68 65 72 65 20 61 72 65 20  listed here are 
1dfb0 63 6f 6d 70 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a  completed..**.**
1dfc0 20 49 66 20 61 6e 20 69 6e 74 65 72 66 61 63 65   If an interface
1dfd0 20 66 61 69 6c 73 20 77 69 74 68 20 53 51 4c 49   fails with SQLI
1dfe0 54 45 5f 4d 49 53 55 53 45 2c 20 74 68 61 74 20  TE_MISUSE, that 
1dff0 6d 65 61 6e 73 20 74 68 65 20 69 6e 74 65 72 66  means the interf
1e000 61 63 65 0a 2a 2a 20 77 61 73 20 69 6e 76 6f 6b  ace.** was invok
1e010 65 64 20 69 6e 63 6f 72 72 65 63 74 6c 79 20 62  ed incorrectly b
1e020 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  y the applicatio
1e030 6e 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73 65  n.  In that case
1e040 2c 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 63  , the.** error c
1e050 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65 20  ode and message 
1e060 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62  may or may not b
1e070 65 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  e set..**.** Req
1e080 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
1e090 31 32 38 30 31 5d 20 5b 48 31 32 38 30 32 5d 20  12801] [H12802] 
1e0a0 5b 48 31 32 38 30 33 5d 20 5b 48 31 32 38 30 37  [H12803] [H12807
1e0b0 5d 20 5b 48 31 32 38 30 38 5d 20 5b 48 31 32 38  ] [H12808] [H128
1e0c0 30 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  09].*/.SQLITE_AP
1e0d0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 72  I int sqlite3_er
1e0e0 72 63 6f 64 65 28 73 71 6c 69 74 65 33 20 2a 64  rcode(sqlite3 *d
1e0f0 62 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  b);.SQLITE_API i
1e100 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e  nt sqlite3_exten
1e110 64 65 64 5f 65 72 72 63 6f 64 65 28 73 71 6c 69  ded_errcode(sqli
1e120 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45  te3 *db);.SQLITE
1e130 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20  _API const char 
1e140 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28  *sqlite3_errmsg(
1e150 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54  sqlite3*);.SQLIT
1e160 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
1e170 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67   *sqlite3_errmsg
1e180 31 36 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f  16(sqlite3*);../
1e190 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53  *.** CAPI3REF: S
1e1a0 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a  QL Statement Obj
1e1b0 65 63 74 20 7b 48 31 33 30 30 30 7d 20 3c 48 31  ect {H13000} <H1
1e1c0 33 30 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44  3010>.** KEYWORD
1e1d0 53 3a 20 7b 70 72 65 70 61 72 65 64 20 73 74 61  S: {prepared sta
1e1e0 74 65 6d 65 6e 74 7d 20 7b 70 72 65 70 61 72 65  tement} {prepare
1e1f0 64 20 73 74 61 74 65 6d 65 6e 74 73 7d 0a 2a 2a  d statements}.**
1e200 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
1e210 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72  of this object r
1e220 65 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67  epresents a sing
1e230 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  le SQL statement
1e240 2e 0a 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 74  ..** This object
1e250 20 69 73 20 76 61 72 69 6f 75 73 6c 79 20 6b 6e   is variously kn
1e260 6f 77 6e 20 61 73 20 61 20 22 70 72 65 70 61 72  own as a "prepar
1e270 65 64 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72  ed statement" or
1e280 20 61 0a 2a 2a 20 22 63 6f 6d 70 69 6c 65 64 20   a.** "compiled 
1e290 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 22 20 6f  SQL statement" o
1e2a0 72 20 73 69 6d 70 6c 79 20 61 73 20 61 20 22 73  r simply as a "s
1e2b0 74 61 74 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a 2a  tatement"..**.**
1e2c0 20 54 68 65 20 6c 69 66 65 20 6f 66 20 61 20 73   The life of a s
1e2d0 74 61 74 65 6d 65 6e 74 20 6f 62 6a 65 63 74 20  tatement object 
1e2e0 67 6f 65 73 20 73 6f 6d 65 74 68 69 6e 67 20 6c  goes something l
1e2f0 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
1e300 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72 65  <ol>.** <li> Cre
1e310 61 74 65 20 74 68 65 20 6f 62 6a 65 63 74 20 75  ate the object u
1e320 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72  sing [sqlite3_pr
1e330 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 61  epare_v2()] or a
1e340 20 72 65 6c 61 74 65 64 0a 2a 2a 20 20 20 20 20   related.**     
1e350 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c 6c   function..** <l
1e360 69 3e 20 42 69 6e 64 20 76 61 6c 75 65 73 20 74  i> Bind values t
1e370 6f 20 5b 68 6f 73 74 20 70 61 72 61 6d 65 74 65  o [host paramete
1e380 72 73 5d 20 75 73 69 6e 67 20 74 68 65 20 73 71  rs] using the sq
1e390 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29 0a 2a  lite3_bind_*().*
1e3a0 2a 20 20 20 20 20 20 69 6e 74 65 72 66 61 63 65  *      interface
1e3b0 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20 74  s..** <li> Run t
1e3c0 68 65 20 53 51 4c 20 62 79 20 63 61 6c 6c 69 6e  he SQL by callin
1e3d0 67 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  g [sqlite3_step(
1e3e0 29 5d 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74  )] one or more t
1e3f0 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 65  imes..** <li> Re
1e400 73 65 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e  set the statemen
1e410 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  t using [sqlite3
1e420 5f 72 65 73 65 74 28 29 5d 20 74 68 65 6e 20 67  _reset()] then g
1e430 6f 20 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 74  o back.**      t
1e440 6f 20 73 74 65 70 20 32 2e 20 20 44 6f 20 74 68  o step 2.  Do th
1e450 69 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20  is zero or more 
1e460 74 69 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 44  times..** <li> D
1e470 65 73 74 72 6f 79 20 74 68 65 20 6f 62 6a 65 63  estroy the objec
1e480 74 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  t using [sqlite3
1e490 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a  _finalize()]..**
1e4a0 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 66   </ol>.**.** Ref
1e4b0 65 72 20 74 6f 20 64 6f 63 75 6d 65 6e 74 61 74  er to documentat
1e4c0 69 6f 6e 20 6f 6e 20 69 6e 64 69 76 69 64 75 61  ion on individua
1e4d0 6c 20 6d 65 74 68 6f 64 73 20 61 62 6f 76 65 20  l methods above 
1e4e0 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a  for additional.*
1e4f0 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  * information..*
1e500 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
1e510 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 73 71   sqlite3_stmt sq
1e520 6c 69 74 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a 0a  lite3_stmt;../*.
1e530 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e  ** CAPI3REF: Run
1e540 2d 74 69 6d 65 20 4c 69 6d 69 74 73 20 7b 48 31  -time Limits {H1
1e550 32 37 36 30 7d 20 3c 53 32 30 36 30 30 3e 0a 2a  2760} <S20600>.*
1e560 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66  *.** This interf
1e570 61 63 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 73  ace allows the s
1e580 69 7a 65 20 6f 66 20 76 61 72 69 6f 75 73 20 63  ize of various c
1e590 6f 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65 20  onstructs to be 
1e5a0 6c 69 6d 69 74 65 64 0a 2a 2a 20 6f 6e 20 61 20  limited.** on a 
1e5b0 63 6f 6e 6e 65 63 74 69 6f 6e 20 62 79 20 63 6f  connection by co
1e5c0 6e 6e 65 63 74 69 6f 6e 20 62 61 73 69 73 2e 20  nnection basis. 
1e5d0 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d   The first param
1e5e0 65 74 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 5b  eter is the.** [
1e5f0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1e600 69 6f 6e 5d 20 77 68 6f 73 65 20 6c 69 6d 69 74  ion] whose limit
1e610 20 69 73 20 74 6f 20 62 65 20 73 65 74 20 6f 72   is to be set or
1e620 20 71 75 65 72 69 65 64 2e 20 20 54 68 65 0a 2a   queried.  The.*
1e630 2a 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  * second paramet
1e640 65 72 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65  er is one of the
1e650 20 5b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69   [limit categori
1e660 65 73 5d 20 74 68 61 74 20 64 65 66 69 6e 65 20  es] that define 
1e670 61 0a 2a 2a 20 63 6c 61 73 73 20 6f 66 20 63 6f  a.** class of co
1e680 6e 73 74 72 75 63 74 73 20 74 6f 20 62 65 20 73  nstructs to be s
1e690 69 7a 65 20 6c 69 6d 69 74 65 64 2e 20 20 54 68  ize limited.  Th
1e6a0 65 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65  e third paramete
1e6b0 72 20 69 73 20 74 68 65 0a 2a 2a 20 6e 65 77 20  r is the.** new 
1e6c0 6c 69 6d 69 74 20 66 6f 72 20 74 68 61 74 20 63  limit for that c
1e6d0 6f 6e 73 74 72 75 63 74 2e 20 20 54 68 65 20 66  onstruct.  The f
1e6e0 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
1e6f0 74 68 65 20 6f 6c 64 20 6c 69 6d 69 74 2e 0a 2a  the old limit..*
1e700 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 65 77 20  *.** If the new 
1e710 6c 69 6d 69 74 20 69 73 20 61 20 6e 65 67 61 74  limit is a negat
1e720 69 76 65 20 6e 75 6d 62 65 72 2c 20 74 68 65 20  ive number, the 
1e730 6c 69 6d 69 74 20 69 73 20 75 6e 63 68 61 6e 67  limit is unchang
1e740 65 64 2e 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6c  ed..** For the l
1e750 69 6d 69 74 20 63 61 74 65 67 6f 72 79 20 6f 66  imit category of
1e760 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 58 59   SQLITE_LIMIT_XY
1e770 5a 20 74 68 65 72 65 20 69 73 20 61 20 0a 2a 2a  Z there is a .**
1e780 20 5b 6c 69 6d 69 74 73 20 7c 20 68 61 72 64 20   [limits | hard 
1e790 75 70 70 65 72 20 62 6f 75 6e 64 5d 0a 2a 2a 20  upper bound].** 
1e7a0 73 65 74 20 62 79 20 61 20 63 6f 6d 70 69 6c 65  set by a compile
1e7b0 2d 74 69 6d 65 20 43 20 70 72 65 70 72 6f 63 65  -time C preproce
1e7c0 73 73 6f 72 20 6d 61 63 72 6f 20 6e 61 6d 65 64  ssor macro named
1e7d0 20 0a 2a 2a 20 5b 6c 69 6d 69 74 73 20 7c 20 53   .** [limits | S
1e7e0 51 4c 49 54 45 5f 4d 41 58 5f 58 59 5a 5d 2e 0a  QLITE_MAX_XYZ]..
1e7f0 2a 2a 20 28 54 68 65 20 22 5f 4c 49 4d 49 54 5f  ** (The "_LIMIT_
1e800 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 69 73  " in the name is
1e810 20 63 68 61 6e 67 65 64 20 74 6f 20 22 5f 4d 41   changed to "_MA
1e820 58 5f 22 2e 29 0a 2a 2a 20 41 74 74 65 6d 70 74  X_".).** Attempt
1e830 73 20 74 6f 20 69 6e 63 72 65 61 73 65 20 61 20  s to increase a 
1e840 6c 69 6d 69 74 20 61 62 6f 76 65 20 69 74 73 20  limit above its 
1e850 68 61 72 64 20 75 70 70 65 72 20 62 6f 75 6e 64  hard upper bound
1e860 20 61 72 65 0a 2a 2a 20 73 69 6c 65 6e 74 6c 79   are.** silently
1e870 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 74 68   truncated to th
1e880 65 20 68 61 72 64 20 75 70 70 65 72 20 6c 69 6d  e hard upper lim
1e890 69 74 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 74 69  it..**.** Run ti
1e8a0 6d 65 20 6c 69 6d 69 74 73 20 61 72 65 20 69 6e  me limits are in
1e8b0 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69  tended for use i
1e8c0 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74  n applications t
1e8d0 68 61 74 20 6d 61 6e 61 67 65 0a 2a 2a 20 62 6f  hat manage.** bo
1e8e0 74 68 20 74 68 65 69 72 20 6f 77 6e 20 69 6e 74  th their own int
1e8f0 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 20 61  ernal database a
1e900 6e 64 20 61 6c 73 6f 20 64 61 74 61 62 61 73 65  nd also database
1e910 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e 74 72  s that are contr
1e920 6f 6c 6c 65 64 0a 2a 2a 20 62 79 20 75 6e 74 72  olled.** by untr
1e930 75 73 74 65 64 20 65 78 74 65 72 6e 61 6c 20 73  usted external s
1e940 6f 75 72 63 65 73 2e 20 20 41 6e 20 65 78 61 6d  ources.  An exam
1e950 70 6c 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  ple application 
1e960 6d 69 67 68 74 20 62 65 20 61 0a 2a 2a 20 77 65  might be a.** we
1e970 62 20 62 72 6f 77 73 65 72 20 74 68 61 74 20 68  b browser that h
1e980 61 73 20 69 74 73 20 6f 77 6e 20 64 61 74 61 62  as its own datab
1e990 61 73 65 73 20 66 6f 72 20 73 74 6f 72 69 6e 67  ases for storing
1e9a0 20 68 69 73 74 6f 72 79 20 61 6e 64 0a 2a 2a 20   history and.** 
1e9b0 73 65 70 61 72 61 74 65 20 64 61 74 61 62 61 73  separate databas
1e9c0 65 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79  es controlled by
1e9d0 20 4a 61 76 61 53 63 72 69 70 74 20 61 70 70 6c   JavaScript appl
1e9e0 69 63 61 74 69 6f 6e 73 20 64 6f 77 6e 6c 6f 61  ications downloa
1e9f0 64 65 64 0a 2a 2a 20 6f 66 66 20 74 68 65 20 49  ded.** off the I
1ea00 6e 74 65 72 6e 65 74 2e 20 20 54 68 65 20 69 6e  nternet.  The in
1ea10 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 73  ternal databases
1ea20 20 63 61 6e 20 62 65 20 67 69 76 65 6e 20 74 68   can be given th
1ea30 65 0a 2a 2a 20 6c 61 72 67 65 2c 20 64 65 66 61  e.** large, defa
1ea40 75 6c 74 20 6c 69 6d 69 74 73 2e 20 20 44 61 74  ult limits.  Dat
1ea50 61 62 61 73 65 73 20 6d 61 6e 61 67 65 64 20 62  abases managed b
1ea60 79 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63  y external sourc
1ea70 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20 67 69 76  es can.** be giv
1ea80 65 6e 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72 20  en much smaller 
1ea90 6c 69 6d 69 74 73 20 64 65 73 69 67 6e 65 64 20  limits designed 
1eaa0 74 6f 20 70 72 65 76 65 6e 74 20 61 20 64 65 6e  to prevent a den
1eab0 69 61 6c 20 6f 66 20 73 65 72 76 69 63 65 0a 2a  ial of service.*
1eac0 2a 20 61 74 74 61 63 6b 2e 20 20 44 65 76 65 6c  * attack.  Devel
1ead0 6f 70 65 72 73 20 6d 69 67 68 74 20 61 6c 73 6f  opers might also
1eae0 20 77 61 6e 74 20 74 6f 20 75 73 65 20 74 68 65   want to use the
1eaf0 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75   [sqlite3_set_au
1eb00 74 68 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a 20 69  thorizer()].** i
1eb10 6e 74 65 72 66 61 63 65 20 74 6f 20 66 75 72 74  nterface to furt
1eb20 68 65 72 20 63 6f 6e 74 72 6f 6c 20 75 6e 74 72  her control untr
1eb30 75 73 74 65 64 20 53 51 4c 2e 20 20 54 68 65 20  usted SQL.  The 
1eb40 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61  size of the data
1eb50 62 61 73 65 0a 2a 2a 20 63 72 65 61 74 65 64 20  base.** created 
1eb60 62 79 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20  by an untrusted 
1eb70 73 63 72 69 70 74 20 63 61 6e 20 62 65 20 63 6f  script can be co
1eb80 6e 74 61 69 6e 65 64 20 75 73 69 6e 67 20 74 68  ntained using th
1eb90 65 0a 2a 2a 20 5b 6d 61 78 5f 70 61 67 65 5f 63  e.** [max_page_c
1eba0 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 2e 0a  ount] [PRAGMA]..
1ebb0 2a 2a 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d 74 69  **.** New run-ti
1ebc0 6d 65 20 6c 69 6d 69 74 20 63 61 74 65 67 6f 72  me limit categor
1ebd0 69 65 73 20 6d 61 79 20 62 65 20 61 64 64 65 64  ies may be added
1ebe0 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61   in future relea
1ebf0 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ses..**.** Requi
1ec00 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32  rements:.** [H12
1ec10 37 36 32 5d 20 5b 48 31 32 37 36 36 5d 20 5b 48  762] [H12766] [H
1ec20 31 32 37 36 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45  12769].*/.SQLITE
1ec30 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1ec40 5f 6c 69 6d 69 74 28 73 71 6c 69 74 65 33 2a 2c  _limit(sqlite3*,
1ec50 20 69 6e 74 20 69 64 2c 20 69 6e 74 20 6e 65 77   int id, int new
1ec60 56 61 6c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  Val);../*.** CAP
1ec70 49 33 52 45 46 3a 20 52 75 6e 2d 54 69 6d 65 20  I3REF: Run-Time 
1ec80 4c 69 6d 69 74 20 43 61 74 65 67 6f 72 69 65 73  Limit Categories
1ec90 20 7b 48 31 32 37 39 30 7d 20 3c 48 31 32 37 36   {H12790} <H1276
1eca0 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  0>.** KEYWORDS: 
1ecb0 7b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 79 7d  {limit category}
1ecc0 20 7b 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69   {limit categori
1ecd0 65 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  es}.**.** These 
1ece0 63 6f 6e 73 74 61 6e 74 73 20 64 65 66 69 6e 65  constants define
1ecf0 20 76 61 72 69 6f 75 73 20 70 65 72 66 6f 72 6d   various perform
1ed00 61 6e 63 65 20 6c 69 6d 69 74 73 0a 2a 2a 20 74  ance limits.** t
1ed10 68 61 74 20 63 61 6e 20 62 65 20 6c 6f 77 65 72  hat can be lower
1ed20 65 64 20 61 74 20 72 75 6e 2d 74 69 6d 65 20 75  ed at run-time u
1ed30 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6c 69  sing [sqlite3_li
1ed40 6d 69 74 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73  mit()]..** The s
1ed50 79 6e 6f 70 73 69 73 20 6f 66 20 74 68 65 20 6d  ynopsis of the m
1ed60 65 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 76  eanings of the v
1ed70 61 72 69 6f 75 73 20 6c 69 6d 69 74 73 20 69 73  arious limits is
1ed80 20 73 68 6f 77 6e 20 62 65 6c 6f 77 2e 0a 2a 2a   shown below..**
1ed90 20 41 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   Additional info
1eda0 72 6d 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c  rmation is avail
1edb0 61 62 6c 65 20 61 74 20 5b 6c 69 6d 69 74 73 20  able at [limits 
1edc0 7c 20 4c 69 6d 69 74 73 20 69 6e 20 53 51 4c 69  | Limits in SQLi
1edd0 74 65 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a  te]..**.** <dl>.
1ede0 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49  ** <dt>SQLITE_LI
1edf0 4d 49 54 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a  MIT_LENGTH</dt>.
1ee00 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d  ** <dd>The maxim
1ee10 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79 20 73  um size of any s
1ee20 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 6f 72  tring or BLOB or
1ee30 20 74 61 62 6c 65 20 72 6f 77 2e 3c 64 64 3e 0a   table row.<dd>.
1ee40 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
1ee50 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47 54  _LIMIT_SQL_LENGT
1ee60 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  H</dt>.** <dd>Th
1ee70 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68  e maximum length
1ee80 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 61 74 65   of an SQL state
1ee90 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  ment.</dd>.**.**
1eea0 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49   <dt>SQLITE_LIMI
1eeb0 54 5f 43 4f 4c 55 4d 4e 3c 2f 64 74 3e 0a 2a 2a  T_COLUMN</dt>.**
1eec0 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d   <dd>The maximum
1eed0 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
1eee0 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 20 64 65  ns in a table de
1eef0 66 69 6e 69 74 69 6f 6e 20 6f 72 20 69 6e 20 74  finition or in t
1ef00 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65 74  he.** result set
1ef10 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 6f   of a [SELECT] o
1ef20 72 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  r the maximum nu
1ef30 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
1ef40 69 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 6f  in an index.** o
1ef50 72 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 59  r in an ORDER BY
1ef60 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61   or GROUP BY cla
1ef70 75 73 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  use.</dd>.**.** 
1ef80 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54  <dt>SQLITE_LIMIT
1ef90 5f 45 58 50 52 5f 44 45 50 54 48 3c 2f 64 74 3e  _EXPR_DEPTH</dt>
1efa0 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69  .** <dd>The maxi
1efb0 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65  mum depth of the
1efc0 20 70 61 72 73 65 20 74 72 65 65 20 6f 6e 20 61   parse tree on a
1efd0 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 3c 2f  ny expression.</
1efe0 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
1eff0 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f  LITE_LIMIT_COMPO
1f000 55 4e 44 5f 53 45 4c 45 43 54 3c 2f 64 74 3e 0a  UND_SELECT</dt>.
1f010 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d  ** <dd>The maxim
1f020 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72  um number of ter
1f030 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64  ms in a compound
1f040 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1f050 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  t.</dd>.**.** <d
1f060 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56  t>SQLITE_LIMIT_V
1f070 44 42 45 5f 4f 50 3c 2f 64 74 3e 0a 2a 2a 20 3c  DBE_OP</dt>.** <
1f080 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e  dd>The maximum n
1f090 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 63  umber of instruc
1f0a0 74 69 6f 6e 73 20 69 6e 20 61 20 76 69 72 74 75  tions in a virtu
1f0b0 61 6c 20 6d 61 63 68 69 6e 65 20 70 72 6f 67 72  al machine progr
1f0c0 61 6d 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6d  am.** used to im
1f0d0 70 6c 65 6d 65 6e 74 20 61 6e 20 53 51 4c 20 73  plement an SQL s
1f0e0 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a  tatement.</dd>.*
1f0f0 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
1f100 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41  LIMIT_FUNCTION_A
1f110 52 47 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  RG</dt>.** <dd>T
1f120 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
1f130 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 6f  r of arguments o
1f140 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e 3c 2f 64  n a function.</d
1f150 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
1f160 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48  ITE_LIMIT_ATTACH
1f170 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  ED</dt>.** <dd>T
1f180 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
1f190 72 20 6f 66 20 5b 41 54 54 41 43 48 20 7c 20 61  r of [ATTACH | a
1f1a0 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
1f1b0 73 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c  s].</dd>.**.** <
1f1c0 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  dt>SQLITE_LIMIT_
1f1d0 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e  LIKE_PATTERN_LEN
1f1e0 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  GTH</dt>.** <dd>
1f1f0 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67  The maximum leng
1f200 74 68 20 6f 66 20 74 68 65 20 70 61 74 74 65 72  th of the patter
1f210 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  n argument to th
1f220 65 20 5b 4c 49 4b 45 5d 20 6f 72 0a 2a 2a 20 5b  e [LIKE] or.** [
1f230 47 4c 4f 42 5d 20 6f 70 65 72 61 74 6f 72 73 2e  GLOB] operators.
1f240 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
1f250 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52  SQLITE_LIMIT_VAR
1f260 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 2f 64 74  IABLE_NUMBER</dt
1f270 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78  >.** <dd>The max
1f280 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 76  imum number of v
1f290 61 72 69 61 62 6c 65 73 20 69 6e 20 61 6e 20 53  ariables in an S
1f2a0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  QL statement tha
1f2b0 74 20 63 61 6e 0a 2a 2a 20 62 65 20 62 6f 75 6e  t can.** be boun
1f2c0 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  d.</dd>.**.** <d
1f2d0 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 54  t>SQLITE_LIMIT_T
1f2e0 52 49 47 47 45 52 5f 44 45 50 54 48 3c 2f 64 74  RIGGER_DEPTH</dt
1f2f0 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78  >.** <dd>The max
1f300 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 72 65  imum depth of re
1f310 63 75 72 73 69 6f 6e 20 66 6f 72 20 74 72 69 67  cursion for trig
1f320 67 65 72 73 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f  gers.</dd>.** </
1f330 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  dl>.*/.#define S
1f340 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
1f350 54 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TH              
1f360 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20        0.#define 
1f370 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c  SQLITE_LIMIT_SQL
1f380 5f 4c 45 4e 47 54 48 20 20 20 20 20 20 20 20 20  _LENGTH         
1f390 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65         1.#define
1f3a0 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f   SQLITE_LIMIT_CO
1f3b0 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20  LUMN            
1f3c0 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e          2.#defin
1f3d0 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45  e SQLITE_LIMIT_E
1f3e0 58 50 52 5f 44 45 50 54 48 20 20 20 20 20 20 20  XPR_DEPTH       
1f3f0 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69           3.#defi
1f400 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  ne SQLITE_LIMIT_
1f410 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20  COMPOUND_SELECT 
1f420 20 20 20 20 20 20 20 20 20 20 34 0a 23 64 65 66            4.#def
1f430 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54  ine SQLITE_LIMIT
1f440 5f 56 44 42 45 5f 4f 50 20 20 20 20 20 20 20 20  _VDBE_OP        
1f450 20 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65             5.#de
1f460 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49  fine SQLITE_LIMI
1f470 54 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 20  T_FUNCTION_ARG  
1f480 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64              6.#d
1f490 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d  efine SQLITE_LIM
1f4a0 49 54 5f 41 54 54 41 43 48 45 44 20 20 20 20 20  IT_ATTACHED     
1f4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 23               7.#
1f4c0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49  define SQLITE_LI
1f4d0 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e  MIT_LIKE_PATTERN
1f4e0 5f 4c 45 4e 47 54 48 20 20 20 20 20 20 20 38 0a  _LENGTH       8.
1f4f0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
1f500 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55  IMIT_VARIABLE_NU
1f510 4d 42 45 52 20 20 20 20 20 20 20 20 20 20 20 39  MBER           9
1f520 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1f530 4c 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45  LIMIT_TRIGGER_DE
1f540 50 54 48 20 20 20 20 20 20 20 20 20 20 20 20 31  PTH            1
1f550 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  0../*.** CAPI3RE
1f560 46 3a 20 43 6f 6d 70 69 6c 69 6e 67 20 41 6e 20  F: Compiling An 
1f570 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 7b 48  SQL Statement {H
1f580 31 33 30 31 30 7d 20 3c 53 31 30 30 30 30 3e 0a  13010} <S10000>.
1f590 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51  ** KEYWORDS: {SQ
1f5a0 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70  L statement comp
1f5b0 69 6c 65 72 7d 0a 2a 2a 0a 2a 2a 20 54 6f 20 65  iler}.**.** To e
1f5c0 78 65 63 75 74 65 20 61 6e 20 53 51 4c 20 71 75  xecute an SQL qu
1f5d0 65 72 79 2c 20 69 74 20 6d 75 73 74 20 66 69 72  ery, it must fir
1f5e0 73 74 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 69  st be compiled i
1f5f0 6e 74 6f 20 61 20 62 79 74 65 2d 63 6f 64 65 0a  nto a byte-code.
1f600 2a 2a 20 70 72 6f 67 72 61 6d 20 75 73 69 6e 67  ** program using
1f610 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 72 6f   one of these ro
1f620 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  utines..**.** Th
1f630 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
1f640 2c 20 22 64 62 22 2c 20 69 73 20 61 20 5b 64 61  , "db", is a [da
1f650 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1f660 6e 5d 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  n] obtained from
1f670 20 61 0a 2a 2a 20 70 72 69 6f 72 20 73 75 63 63   a.** prior succ
1f680 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b  essful call to [
1f690 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c  sqlite3_open()],
1f6a0 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76   [sqlite3_open_v
1f6b0 32 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69  2()] or.** [sqli
1f6c0 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 20 20  te3_open16()].  
1f6d0 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  The database con
1f6e0 6e 65 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74  nection must not
1f6f0 20 68 61 76 65 20 62 65 65 6e 20 63 6c 6f 73 65   have been close
1f700 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63  d..**.** The sec
1f710 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 22 7a  ond argument, "z
1f720 53 71 6c 22 2c 20 69 73 20 74 68 65 20 73 74 61  Sql", is the sta
1f730 74 65 6d 65 6e 74 20 74 6f 20 62 65 20 63 6f 6d  tement to be com
1f740 70 69 6c 65 64 2c 20 65 6e 63 6f 64 65 64 0a 2a  piled, encoded.*
1f750 2a 20 61 73 20 65 69 74 68 65 72 20 55 54 46 2d  * as either UTF-
1f760 38 20 6f 72 20 55 54 46 2d 31 36 2e 20 20 54 68  8 or UTF-16.  Th
1f770 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  e sqlite3_prepar
1f780 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  e() and sqlite3_
1f790 70 72 65 70 61 72 65 5f 76 32 28 29 0a 2a 2a 20  prepare_v2().** 
1f7a0 69 6e 74 65 72 66 61 63 65 73 20 75 73 65 20 55  interfaces use U
1f7b0 54 46 2d 38 2c 20 61 6e 64 20 73 71 6c 69 74 65  TF-8, and sqlite
1f7c0 33 5f 70 72 65 70 61 72 65 31 36 28 29 20 61 6e  3_prepare16() an
1f7d0 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  d sqlite3_prepar
1f7e0 65 31 36 5f 76 32 28 29 0a 2a 2a 20 75 73 65 20  e16_v2().** use 
1f7f0 55 54 46 2d 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66  UTF-16..**.** If
1f800 20 74 68 65 20 6e 42 79 74 65 20 61 72 67 75 6d   the nByte argum
1f810 65 6e 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e  ent is less than
1f820 20 7a 65 72 6f 2c 20 74 68 65 6e 20 7a 53 71 6c   zero, then zSql
1f830 20 69 73 20 72 65 61 64 20 75 70 20 74 6f 20 74   is read up to t
1f840 68 65 0a 2a 2a 20 66 69 72 73 74 20 7a 65 72 6f  he.** first zero
1f850 20 74 65 72 6d 69 6e 61 74 6f 72 2e 20 49 66 20   terminator. If 
1f860 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d 6e 65 67  nByte is non-neg
1f870 61 74 69 76 65 2c 20 74 68 65 6e 20 69 74 20 69  ative, then it i
1f880 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a  s the maximum.**
1f890 20 6e 75 6d 62 65 72 20 6f 66 20 20 62 79 74 65   number of  byte
1f8a0 73 20 72 65 61 64 20 66 72 6f 6d 20 7a 53 71 6c  s read from zSql
1f8b0 2e 20 20 57 68 65 6e 20 6e 42 79 74 65 20 69 73  .  When nByte is
1f8c0 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74   non-negative, t
1f8d0 68 65 0a 2a 2a 20 7a 53 71 6c 20 73 74 72 69 6e  he.** zSql strin
1f8e0 67 20 65 6e 64 73 20 61 74 20 65 69 74 68 65 72  g ends at either
1f8f0 20 74 68 65 20 66 69 72 73 74 20 27 5c 30 30 30   the first '\000
1f900 27 20 6f 72 20 27 5c 75 30 30 30 30 27 20 63 68  ' or '\u0000' ch
1f910 61 72 61 63 74 65 72 20 6f 72 0a 2a 2a 20 74 68  aracter or.** th
1f920 65 20 6e 42 79 74 65 2d 74 68 20 62 79 74 65 2c  e nByte-th byte,
1f930 20 77 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73   whichever comes
1f940 20 66 69 72 73 74 2e 20 49 66 20 74 68 65 20 63   first. If the c
1f950 61 6c 6c 65 72 20 6b 6e 6f 77 73 0a 2a 2a 20 74  aller knows.** t
1f960 68 61 74 20 74 68 65 20 73 75 70 70 6c 69 65 64  hat the supplied
1f970 20 73 74 72 69 6e 67 20 69 73 20 6e 75 6c 2d 74   string is nul-t
1f980 65 72 6d 69 6e 61 74 65 64 2c 20 74 68 65 6e 20  erminated, then 
1f990 74 68 65 72 65 20 69 73 20 61 20 73 6d 61 6c 6c  there is a small
1f9a0 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  .** performance 
1f9b0 61 64 76 61 6e 74 61 67 65 20 74 6f 20 62 65 20  advantage to be 
1f9c0 67 61 69 6e 65 64 20 62 79 20 70 61 73 73 69 6e  gained by passin
1f9d0 67 20 61 6e 20 6e 42 79 74 65 20 70 61 72 61 6d  g an nByte param
1f9e0 65 74 65 72 20 74 68 61 74 0a 2a 2a 20 69 73 20  eter that.** is 
1f9f0 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6e 75 6d  equal to the num
1fa00 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
1fa10 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67  the input string
1fa20 20 3c 69 3e 69 6e 63 6c 75 64 69 6e 67 3c 2f 69   <i>including</i
1fa30 3e 0a 2a 2a 20 74 68 65 20 6e 75 6c 2d 74 65 72  >.** the nul-ter
1fa40 6d 69 6e 61 74 6f 72 20 62 79 74 65 73 2e 0a 2a  minator bytes..*
1fa50 2a 0a 2a 2a 20 49 66 20 70 7a 54 61 69 6c 20 69  *.** If pzTail i
1fa60 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20  s not NULL then 
1fa70 2a 70 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 20  *pzTail is made 
1fa80 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  to point to the 
1fa90 66 69 72 73 74 20 62 79 74 65 0a 2a 2a 20 70 61  first byte.** pa
1faa0 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  st the end of th
1fab0 65 20 66 69 72 73 74 20 53 51 4c 20 73 74 61 74  e first SQL stat
1fac0 65 6d 65 6e 74 20 69 6e 20 7a 53 71 6c 2e 20 20  ement in zSql.  
1fad0 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f  These routines o
1fae0 6e 6c 79 0a 2a 2a 20 63 6f 6d 70 69 6c 65 20 74  nly.** compile t
1faf0 68 65 20 66 69 72 73 74 20 73 74 61 74 65 6d 65  he first stateme
1fb00 6e 74 20 69 6e 20 7a 53 71 6c 2c 20 73 6f 20 2a  nt in zSql, so *
1fb10 70 7a 54 61 69 6c 20 69 73 20 6c 65 66 74 20 70  pzTail is left p
1fb20 6f 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a 20 77 68  ointing to.** wh
1fb30 61 74 20 72 65 6d 61 69 6e 73 20 75 6e 63 6f 6d  at remains uncom
1fb40 70 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70  piled..**.** *pp
1fb50 53 74 6d 74 20 69 73 20 6c 65 66 74 20 70 6f 69  Stmt is left poi
1fb60 6e 74 69 6e 67 20 74 6f 20 61 20 63 6f 6d 70 69  nting to a compi
1fb70 6c 65 64 20 5b 70 72 65 70 61 72 65 64 20 73 74  led [prepared st
1fb80 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 63 61  atement] that ca
1fb90 6e 20 62 65 0a 2a 2a 20 65 78 65 63 75 74 65 64  n be.** executed
1fba0 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
1fbb0 73 74 65 70 28 29 5d 2e 20 20 49 66 20 74 68 65  step()].  If the
1fbc0 72 65 20 69 73 20 61 6e 20 65 72 72 6f 72 2c 20  re is an error, 
1fbd0 2a 70 70 53 74 6d 74 20 69 73 20 73 65 74 0a 2a  *ppStmt is set.*
1fbe0 2a 20 74 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 74  * to NULL.  If t
1fbf0 68 65 20 69 6e 70 75 74 20 74 65 78 74 20 63 6f  he input text co
1fc00 6e 74 61 69 6e 73 20 6e 6f 20 53 51 4c 20 28 69  ntains no SQL (i
1fc10 66 20 74 68 65 20 69 6e 70 75 74 20 69 73 20 61  f the input is a
1fc20 6e 20 65 6d 70 74 79 0a 2a 2a 20 73 74 72 69 6e  n empty.** strin
1fc30 67 20 6f 72 20 61 20 63 6f 6d 6d 65 6e 74 29 20  g or a comment) 
1fc40 74 68 65 6e 20 2a 70 70 53 74 6d 74 20 69 73 20  then *ppStmt is 
1fc50 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20  set to NULL..** 
1fc60 54 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63  The calling proc
1fc70 65 64 75 72 65 20 69 73 20 72 65 73 70 6f 6e 73  edure is respons
1fc80 69 62 6c 65 20 66 6f 72 20 64 65 6c 65 74 69 6e  ible for deletin
1fc90 67 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 0a 2a  g the compiled.*
1fca0 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  * SQL statement 
1fcb0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66  using [sqlite3_f
1fcc0 69 6e 61 6c 69 7a 65 28 29 5d 20 61 66 74 65 72  inalize()] after
1fcd0 20 69 74 20 68 61 73 20 66 69 6e 69 73 68 65 64   it has finished
1fce0 20 77 69 74 68 20 69 74 2e 0a 2a 2a 20 70 70 53   with it..** ppS
1fcf0 74 6d 74 20 6d 61 79 20 6e 6f 74 20 62 65 20 4e  tmt may not be N
1fd00 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75  ULL..**.** On su
1fd10 63 63 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f  ccess, [SQLITE_O
1fd20 4b 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20  K] is returned, 
1fd30 6f 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72  otherwise an [er
1fd40 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74  ror code] is ret
1fd50 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  urned..**.** The
1fd60 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
1fd70 5f 76 32 28 29 20 61 6e 64 20 73 71 6c 69 74 65  _v2() and sqlite
1fd80 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29  3_prepare16_v2()
1fd90 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 0a   interfaces are.
1fda0 2a 2a 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66  ** recommended f
1fdb0 6f 72 20 61 6c 6c 20 6e 65 77 20 70 72 6f 67 72  or all new progr
1fdc0 61 6d 73 2e 20 54 68 65 20 74 77 6f 20 6f 6c 64  ams. The two old
1fdd0 65 72 20 69 6e 74 65 72 66 61 63 65 73 20 61 72  er interfaces ar
1fde0 65 20 72 65 74 61 69 6e 65 64 0a 2a 2a 20 66 6f  e retained.** fo
1fdf0 72 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70  r backwards comp
1fe00 61 74 69 62 69 6c 69 74 79 2c 20 62 75 74 20 74  atibility, but t
1fe10 68 65 69 72 20 75 73 65 20 69 73 20 64 69 73 63  heir use is disc
1fe20 6f 75 72 61 67 65 64 2e 0a 2a 2a 20 49 6e 20 74  ouraged..** In t
1fe30 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63  he "v2" interfac
1fe40 65 73 2c 20 74 68 65 20 70 72 65 70 61 72 65 64  es, the prepared
1fe50 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 68   statement.** th
1fe60 61 74 20 69 73 20 72 65 74 75 72 6e 65 64 20 28  at is returned (
1fe70 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d  the [sqlite3_stm
1fe80 74 5d 20 6f 62 6a 65 63 74 29 20 63 6f 6e 74 61  t] object) conta
1fe90 69 6e 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ins a copy of th
1fea0 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 53 51  e.** original SQ
1feb0 4c 20 74 65 78 74 2e 20 54 68 69 73 20 63 61 75  L text. This cau
1fec0 73 65 73 20 74 68 65 20 5b 73 71 6c 69 74 65 33  ses the [sqlite3
1fed0 5f 73 74 65 70 28 29 5d 20 69 6e 74 65 72 66 61  _step()] interfa
1fee0 63 65 20 74 6f 0a 2a 2a 20 62 65 68 61 76 65 20  ce to.** behave 
1fef0 61 20 64 69 66 66 65 72 65 6e 74 6c 79 20 69 6e  a differently in
1ff00 20 74 68 72 65 65 20 77 61 79 73 3a 0a 2a 2a 0a   three ways:.**.
1ff10 2a 2a 20 3c 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 0a  ** <ol>.** <li>.
1ff20 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61  ** If the databa
1ff30 73 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65  se schema change
1ff40 73 2c 20 69 6e 73 74 65 61 64 20 6f 66 20 72 65  s, instead of re
1ff50 74 75 72 6e 69 6e 67 20 5b 53 51 4c 49 54 45 5f  turning [SQLITE_
1ff60 53 43 48 45 4d 41 5d 20 61 73 20 69 74 0a 2a 2a  SCHEMA] as it.**
1ff70 20 61 6c 77 61 79 73 20 75 73 65 64 20 74 6f 20   always used to 
1ff80 64 6f 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  do, [sqlite3_ste
1ff90 70 28 29 5d 20 77 69 6c 6c 20 61 75 74 6f 6d 61  p()] will automa
1ffa0 74 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69 6c  tically recompil
1ffb0 65 20 74 68 65 20 53 51 4c 0a 2a 2a 20 73 74 61  e the SQL.** sta
1ffc0 74 65 6d 65 6e 74 20 61 6e 64 20 74 72 79 20 74  tement and try t
1ffd0 6f 20 72 75 6e 20 69 74 20 61 67 61 69 6e 2e 20  o run it again. 
1ffe0 20 49 66 20 74 68 65 20 73 63 68 65 6d 61 20 68   If the schema h
1fff0 61 73 20 63 68 61 6e 67 65 64 20 69 6e 0a 2a 2a  as changed in.**
20000 20 61 20 77 61 79 20 74 68 61 74 20 6d 61 6b 65   a way that make
20010 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  s the statement 
20020 6e 6f 20 6c 6f 6e 67 65 72 20 76 61 6c 69 64 2c  no longer valid,
20030 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
20040 5d 20 77 69 6c 6c 20 73 74 69 6c 6c 0a 2a 2a 20  ] will still.** 
20050 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 53  return [SQLITE_S
20060 43 48 45 4d 41 5d 2e 20 20 42 75 74 20 75 6e 6c  CHEMA].  But unl
20070 69 6b 65 20 74 68 65 20 6c 65 67 61 63 79 20 62  ike the legacy b
20080 65 68 61 76 69 6f 72 2c 20 5b 53 51 4c 49 54 45  ehavior, [SQLITE
20090 5f 53 43 48 45 4d 41 5d 20 69 73 0a 2a 2a 20 6e  _SCHEMA] is.** n
200a0 6f 77 20 61 20 66 61 74 61 6c 20 65 72 72 6f 72  ow a fatal error
200b0 2e 20 20 43 61 6c 6c 69 6e 67 20 5b 73 71 6c 69  .  Calling [sqli
200c0 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
200d0 5d 20 61 67 61 69 6e 20 77 69 6c 6c 20 6e 6f 74  ] again will not
200e0 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 65 72 72   make the.** err
200f0 6f 72 20 67 6f 20 61 77 61 79 2e 20 20 4e 6f 74  or go away.  Not
20100 65 3a 20 75 73 65 20 5b 73 71 6c 69 74 65 33 5f  e: use [sqlite3_
20110 65 72 72 6d 73 67 28 29 5d 20 74 6f 20 66 69 6e  errmsg()] to fin
20120 64 20 74 68 65 20 74 65 78 74 0a 2a 2a 20 6f 66  d the text.** of
20130 20 74 68 65 20 70 61 72 73 69 6e 67 20 65 72 72   the parsing err
20140 6f 72 20 74 68 61 74 20 72 65 73 75 6c 74 73 20  or that results 
20150 69 6e 20 61 6e 20 5b 53 51 4c 49 54 45 5f 53 43  in an [SQLITE_SC
20160 48 45 4d 41 5d 20 72 65 74 75 72 6e 2e 0a 2a 2a  HEMA] return..**
20170 20 3c 2f 6c 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69   </li>.**.** <li
20180 3e 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 65 72 72  >.** When an err
20190 6f 72 20 6f 63 63 75 72 73 2c 20 5b 73 71 6c 69  or occurs, [sqli
201a0 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69 6c 6c  te3_step()] will
201b0 20 72 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20 74   return one of t
201c0 68 65 20 64 65 74 61 69 6c 65 64 0a 2a 2a 20 5b  he detailed.** [
201d0 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 6f 72 20  error codes] or 
201e0 5b 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20  [extended error 
201f0 63 6f 64 65 73 5d 2e 20 20 54 68 65 20 6c 65 67  codes].  The leg
20200 61 63 79 20 62 65 68 61 76 69 6f 72 20 77 61 73  acy behavior was
20210 20 74 68 61 74 0a 2a 2a 20 5b 73 71 6c 69 74 65   that.** [sqlite
20220 33 5f 73 74 65 70 28 29 5d 20 77 6f 75 6c 64 20  3_step()] would 
20230 6f 6e 6c 79 20 72 65 74 75 72 6e 20 61 20 67 65  only return a ge
20240 6e 65 72 69 63 20 5b 53 51 4c 49 54 45 5f 45 52  neric [SQLITE_ER
20250 52 4f 52 5d 20 72 65 73 75 6c 74 20 63 6f 64 65  ROR] result code
20260 0a 2a 2a 20 61 6e 64 20 79 6f 75 20 77 6f 75 6c  .** and you woul
20270 64 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 61  d have to make a
20280 20 73 65 63 6f 6e 64 20 63 61 6c 6c 20 74 6f 20   second call to 
20290 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29  [sqlite3_reset()
202a0 5d 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f  ] in order.** to
202b0 20 66 69 6e 64 20 74 68 65 20 75 6e 64 65 72 6c   find the underl
202c0 79 69 6e 67 20 63 61 75 73 65 20 6f 66 20 74 68  ying cause of th
202d0 65 20 70 72 6f 62 6c 65 6d 2e 20 57 69 74 68 20  e problem. With 
202e0 74 68 65 20 22 76 32 22 20 70 72 65 70 61 72 65  the "v2" prepare
202f0 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 2c 20  .** interfaces, 
20300 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 72  the underlying r
20310 65 61 73 6f 6e 20 66 6f 72 20 74 68 65 20 65 72  eason for the er
20320 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64 20  ror is returned 
20330 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a 2a 2a 20  immediately..** 
20340 3c 2f 6c 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e  </li>.**.** <li>
20350 0a 2a 2a 20 5e 49 66 20 74 68 65 20 76 61 6c 75  .** ^If the valu
20360 65 20 6f 66 20 61 20 5b 70 61 72 61 6d 65 74 65  e of a [paramete
20370 72 20 7c 20 68 6f 73 74 20 70 61 72 61 6d 65 74  r | host paramet
20380 65 72 5d 20 69 6e 20 74 68 65 20 57 48 45 52 45  er] in the WHERE
20390 20 63 6c 61 75 73 65 20 6d 69 67 68 74 0a 2a 2a   clause might.**
203a0 20 63 68 61 6e 67 65 20 74 68 65 20 71 75 65 72   change the quer
203b0 79 20 70 6c 61 6e 20 66 6f 72 20 61 20 73 74 61  y plan for a sta
203c0 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 74 68 65  tement, then the
203d0 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 79 20 62   statement may b
203e0 65 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c  e.** automatical
203f0 6c 79 20 72 65 63 6f 6d 70 69 6c 65 64 20 28 61  ly recompiled (a
20400 73 20 69 66 20 74 68 65 72 65 20 68 61 64 20 62  s if there had b
20410 65 65 6e 20 61 20 73 63 68 65 6d 61 20 63 68 61  een a schema cha
20420 6e 67 65 29 20 6f 6e 20 74 68 65 20 66 69 72 73  nge) on the firs
20430 74 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73  t .** [sqlite3_s
20440 74 65 70 28 29 5d 20 63 61 6c 6c 20 66 6f 6c 6c  tep()] call foll
20450 6f 77 69 6e 67 20 61 6e 79 20 63 68 61 6e 67 65  owing any change
20460 20 74 6f 20 74 68 65 20 0a 2a 2a 20 5b 73 71 6c   to the .** [sql
20470 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 20 7c  ite3_bind_text |
20480 20 62 69 6e 64 69 6e 67 73 5d 20 6f 66 20 74 68   bindings] of th
20490 65 20 5b 70 61 72 61 6d 65 74 65 72 5d 2e 20 0a  e [parameter]. .
204a0 2a 2a 20 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 6f 6c  ** </li>.** </ol
204b0 3e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  >.**.** Requirem
204c0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 30 31 31  ents:.** [H13011
204d0 5d 20 5b 48 31 33 30 31 32 5d 20 5b 48 31 33 30  ] [H13012] [H130
204e0 31 33 5d 20 5b 48 31 33 30 31 34 5d 20 5b 48 31  13] [H13014] [H1
204f0 33 30 31 35 5d 20 5b 48 31 33 30 31 36 5d 20 5b  3015] [H13016] [
20500 48 31 33 30 31 39 5d 20 5b 48 31 33 30 32 31 5d  H13019] [H13021]
20510 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  .**.*/.SQLITE_AP
20520 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72  I int sqlite3_pr
20530 65 70 61 72 65 28 0a 20 20 73 71 6c 69 74 65 33  epare(.  sqlite3
20540 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
20550 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
20560 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  dle */.  const c
20570 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20  har *zSql,      
20580 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   /* SQL statemen
20590 74 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  t, UTF-8 encoded
205a0 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c   */.  int nByte,
205b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
205c0 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20   Maximum length 
205d0 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73  of zSql in bytes
205e0 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  . */.  sqlite3_s
205f0 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f  tmt **ppStmt,  /
20600 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74  * OUT: Statement
20610 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e   handle */.  con
20620 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c  st char **pzTail
20630 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69       /* OUT: Poi
20640 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70  nter to unused p
20650 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a  ortion of zSql *
20660 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  /.);.SQLITE_API 
20670 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70  int sqlite3_prep
20680 61 72 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65  are_v2(.  sqlite
20690 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
206a0 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
206b0 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ndle */.  const 
206c0 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20  char *zSql,     
206d0 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65    /* SQL stateme
206e0 6e 74 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65  nt, UTF-8 encode
206f0 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  d */.  int nByte
20700 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
20710 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68  * Maximum length
20720 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65   of zSql in byte
20730 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  s. */.  sqlite3_
20740 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20  stmt **ppStmt,  
20750 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e  /* OUT: Statemen
20760 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f  t handle */.  co
20770 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61 69  nst char **pzTai
20780 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f  l     /* OUT: Po
20790 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64 20  inter to unused 
207a0 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20  portion of zSql 
207b0 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  */.);.SQLITE_API
207c0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65   int sqlite3_pre
207d0 70 61 72 65 31 36 28 0a 20 20 73 71 6c 69 74 65  pare16(.  sqlite
207e0 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
207f0 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
20800 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ndle */.  const 
20810 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20  void *zSql,     
20820 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65    /* SQL stateme
20830 6e 74 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f 64  nt, UTF-16 encod
20840 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  ed */.  int nByt
20850 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
20860 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  /* Maximum lengt
20870 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74  h of zSql in byt
20880 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  es. */.  sqlite3
20890 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20  _stmt **ppStmt, 
208a0 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65   /* OUT: Stateme
208b0 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63  nt handle */.  c
208c0 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61  onst void **pzTa
208d0 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50  il     /* OUT: P
208e0 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64  ointer to unused
208f0 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c   portion of zSql
20900 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50   */.);.SQLITE_AP
20910 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72  I int sqlite3_pr
20920 65 70 61 72 65 31 36 5f 76 32 28 0a 20 20 73 71  epare16_v2(.  sq
20930 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
20940 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
20950 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f  e handle */.  co
20960 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20  nst void *zSql, 
20970 20 20 20 20 20 20 2f 2a 20 53 51 4c 20 73 74 61        /* SQL sta
20980 74 65 6d 65 6e 74 2c 20 55 54 46 2d 31 36 20 65  tement, UTF-16 e
20990 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20  ncoded */.  int 
209a0 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20 20 20  nByte,          
209b0 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c      /* Maximum l
209c0 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e  ength of zSql in
209d0 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c   bytes. */.  sql
209e0 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74  ite3_stmt **ppSt
209f0 6d 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 53 74 61  mt,  /* OUT: Sta
20a00 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 2a 2f  tement handle */
20a10 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a  .  const void **
20a20 70 7a 54 61 69 6c 20 20 20 20 20 2f 2a 20 4f 55  pzTail     /* OU
20a30 54 3a 20 50 6f 69 6e 74 65 72 20 74 6f 20 75 6e  T: Pointer to un
20a40 75 73 65 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20  used portion of 
20a50 7a 53 71 6c 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a  zSql */.);../*.*
20a60 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 74 72  * CAPI3REF: Retr
20a70 69 65 76 69 6e 67 20 53 74 61 74 65 6d 65 6e 74  ieving Statement
20a80 20 53 51 4c 20 7b 48 31 33 31 30 30 7d 20 3c 48   SQL {H13100} <H
20a90 31 33 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  13000>.**.** Thi
20aa0 73 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20  s interface can 
20ab0 62 65 20 75 73 65 64 20 74 6f 20 72 65 74 72 69  be used to retri
20ac0 65 76 65 20 61 20 73 61 76 65 64 20 63 6f 70 79  eve a saved copy
20ad0 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   of the original
20ae0 0a 2a 2a 20 53 51 4c 20 74 65 78 74 20 75 73 65  .** SQL text use
20af0 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 5b 70  d to create a [p
20b00 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
20b10 74 5d 20 69 66 20 74 68 61 74 20 73 74 61 74 65  t] if that state
20b20 6d 65 6e 74 20 77 61 73 0a 2a 2a 20 63 6f 6d 70  ment was.** comp
20b30 69 6c 65 64 20 75 73 69 6e 67 20 65 69 74 68 65  iled using eithe
20b40 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  r [sqlite3_prepa
20b50 72 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c  re_v2()] or [sql
20b60 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76  ite3_prepare16_v
20b70 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  2()]..**.** Requ
20b80 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
20b90 33 31 30 31 5d 20 5b 48 31 33 31 30 32 5d 20 5b  3101] [H13102] [
20ba0 48 31 33 31 30 33 5d 0a 2a 2f 0a 53 51 4c 49 54  H13103].*/.SQLIT
20bb0 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
20bc0 20 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28 73 71   *sqlite3_sql(sq
20bd0 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
20be0 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  t);../*.** CAPI3
20bf0 52 45 46 3a 20 44 79 6e 61 6d 69 63 61 6c 6c 79  REF: Dynamically
20c00 20 54 79 70 65 64 20 56 61 6c 75 65 20 4f 62 6a   Typed Value Obj
20c10 65 63 74 20 7b 48 31 35 30 30 30 7d 20 3c 53 32  ect {H15000} <S2
20c20 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44  0200>.** KEYWORD
20c30 53 3a 20 7b 70 72 6f 74 65 63 74 65 64 20 73 71  S: {protected sq
20c40 6c 69 74 65 33 5f 76 61 6c 75 65 7d 20 7b 75 6e  lite3_value} {un
20c50 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
20c60 33 5f 76 61 6c 75 65 7d 0a 2a 2a 0a 2a 2a 20 53  3_value}.**.** S
20c70 51 4c 69 74 65 20 75 73 65 73 20 74 68 65 20 73  QLite uses the s
20c80 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a  qlite3_value obj
20c90 65 63 74 20 74 6f 20 72 65 70 72 65 73 65 6e 74  ect to represent
20ca0 20 61 6c 6c 20 76 61 6c 75 65 73 0a 2a 2a 20 74   all values.** t
20cb0 68 61 74 20 63 61 6e 20 62 65 20 73 74 6f 72 65  hat can be store
20cc0 64 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 20  d in a database 
20cd0 74 61 62 6c 65 2e 20 53 51 4c 69 74 65 20 75 73  table. SQLite us
20ce0 65 73 20 64 79 6e 61 6d 69 63 20 74 79 70 69 6e  es dynamic typin
20cf0 67 0a 2a 2a 20 66 6f 72 20 74 68 65 20 76 61 6c  g.** for the val
20d00 75 65 73 20 69 74 20 73 74 6f 72 65 73 2e 20 56  ues it stores. V
20d10 61 6c 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20  alues stored in 
20d20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
20d30 6a 65 63 74 73 0a 2a 2a 20 63 61 6e 20 62 65 20  jects.** can be 
20d40 69 6e 74 65 67 65 72 73 2c 20 66 6c 6f 61 74 69  integers, floati
20d50 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 2c  ng point values,
20d60 20 73 74 72 69 6e 67 73 2c 20 42 4c 4f 42 73 2c   strings, BLOBs,
20d70 20 6f 72 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   or NULL..**.** 
20d80 41 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  An sqlite3_value
20d90 20 6f 62 6a 65 63 74 20 6d 61 79 20 62 65 20 65   object may be e
20da0 69 74 68 65 72 20 22 70 72 6f 74 65 63 74 65 64  ither "protected
20db0 22 20 6f 72 20 22 75 6e 70 72 6f 74 65 63 74 65  " or "unprotecte
20dc0 64 22 2e 0a 2a 2a 20 53 6f 6d 65 20 69 6e 74 65  d"..** Some inte
20dd0 72 66 61 63 65 73 20 72 65 71 75 69 72 65 20 61  rfaces require a
20de0 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74   protected sqlit
20df0 65 33 5f 76 61 6c 75 65 2e 20 20 4f 74 68 65 72  e3_value.  Other
20e00 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 77   interfaces.** w
20e10 69 6c 6c 20 61 63 63 65 70 74 20 65 69 74 68 65  ill accept eithe
20e20 72 20 61 20 70 72 6f 74 65 63 74 65 64 20 6f 72  r a protected or
20e30 20 61 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 20   an unprotected 
20e40 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 0a 2a  sqlite3_value..*
20e50 2a 20 45 76 65 72 79 20 69 6e 74 65 72 66 61 63  * Every interfac
20e60 65 20 74 68 61 74 20 61 63 63 65 70 74 73 20 73  e that accepts s
20e70 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 61 72 67  qlite3_value arg
20e80 75 6d 65 6e 74 73 20 73 70 65 63 69 66 69 65 73  uments specifies
20e90 0a 2a 2a 20 77 68 65 74 68 65 72 20 6f 72 20 6e  .** whether or n
20ea0 6f 74 20 69 74 20 72 65 71 75 69 72 65 73 20 61  ot it requires a
20eb0 20 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74   protected sqlit
20ec0 65 33 5f 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20  e3_value..**.** 
20ed0 54 68 65 20 74 65 72 6d 73 20 22 70 72 6f 74 65  The terms "prote
20ee0 63 74 65 64 22 20 61 6e 64 20 22 75 6e 70 72 6f  cted" and "unpro
20ef0 74 65 63 74 65 64 22 20 72 65 66 65 72 20 74 6f  tected" refer to
20f00 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a   whether or not.
20f10 2a 2a 20 61 20 6d 75 74 65 78 20 69 73 20 68 65  ** a mutex is he
20f20 6c 64 2e 20 20 41 20 69 6e 74 65 72 6e 61 6c 20  ld.  A internal 
20f30 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 66 6f  mutex is held fo
20f40 72 20 61 20 70 72 6f 74 65 63 74 65 64 0a 2a 2a  r a protected.**
20f50 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
20f60 62 6a 65 63 74 20 62 75 74 20 6e 6f 20 6d 75 74  bject but no mut
20f70 65 78 20 69 73 20 68 65 6c 64 20 66 6f 72 20 61  ex is held for a
20f80 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a  n unprotected.**
20f90 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
20fa0 62 6a 65 63 74 2e 20 20 49 66 20 53 51 4c 69 74  bject.  If SQLit
20fb0 65 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f  e is compiled to
20fc0 20 62 65 20 73 69 6e 67 6c 65 2d 74 68 72 65 61   be single-threa
20fd0 64 65 64 0a 2a 2a 20 28 77 69 74 68 20 5b 53 51  ded.** (with [SQ
20fe0 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d  LITE_THREADSAFE=
20ff0 30 5d 20 61 6e 64 20 77 69 74 68 20 5b 73 71 6c  0] and with [sql
21000 69 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 28  ite3_threadsafe(
21010 29 5d 20 72 65 74 75 72 6e 69 6e 67 20 30 29 0a  )] returning 0).
21020 2a 2a 20 6f 72 20 69 66 20 53 51 4c 69 74 65 20  ** or if SQLite 
21030 69 73 20 72 75 6e 20 69 6e 20 6f 6e 65 20 6f 66  is run in one of
21040 20 72 65 64 75 63 65 64 20 6d 75 74 65 78 20 6d   reduced mutex m
21050 6f 64 65 73 20 0a 2a 2a 20 5b 53 51 4c 49 54 45  odes .** [SQLITE
21060 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48  _CONFIG_SINGLETH
21070 52 45 41 44 5d 20 6f 72 20 5b 53 51 4c 49 54 45  READ] or [SQLITE
21080 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54 48 52  _CONFIG_MULTITHR
21090 45 41 44 5d 0a 2a 2a 20 74 68 65 6e 20 74 68 65  EAD].** then the
210a0 72 65 20 69 73 20 6e 6f 20 64 69 73 74 69 6e 63  re is no distinc
210b0 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 70 72 6f  tion between pro
210c0 74 65 63 74 65 64 20 61 6e 64 20 75 6e 70 72 6f  tected and unpro
210d0 74 65 63 74 65 64 0a 2a 2a 20 73 71 6c 69 74 65  tected.** sqlite
210e0 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20  3_value objects 
210f0 61 6e 64 20 74 68 65 79 20 63 61 6e 20 62 65 20  and they can be 
21100 75 73 65 64 20 69 6e 74 65 72 63 68 61 6e 67 65  used interchange
21110 61 62 6c 79 2e 20 20 48 6f 77 65 76 65 72 2c 0a  ably.  However,.
21120 2a 2a 20 66 6f 72 20 6d 61 78 69 6d 75 6d 20 63  ** for maximum c
21130 6f 64 65 20 70 6f 72 74 61 62 69 6c 69 74 79 20  ode portability 
21140 69 74 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65  it is recommende
21150 64 20 74 68 61 74 20 61 70 70 6c 69 63 61 74 69  d that applicati
21160 6f 6e 73 0a 2a 2a 20 73 74 69 6c 6c 20 6d 61 6b  ons.** still mak
21170 65 20 74 68 65 20 64 69 73 74 69 6e 63 74 69 6f  e the distinctio
21180 6e 20 62 65 74 77 65 65 6e 20 62 65 74 77 65 65  n between betwee
21190 6e 20 70 72 6f 74 65 63 74 65 64 20 61 6e 64 20  n protected and 
211a0 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73  unprotected.** s
211b0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a  qlite3_value obj
211c0 65 63 74 73 20 65 76 65 6e 20 77 68 65 6e 20 6e  ects even when n
211d0 6f 74 20 73 74 72 69 63 74 6c 79 20 72 65 71 75  ot strictly requ
211e0 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ired..**.** The 
211f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
21200 6a 65 63 74 73 20 74 68 61 74 20 61 72 65 20 70  jects that are p
21210 61 73 73 65 64 20 61 73 20 70 61 72 61 6d 65 74  assed as paramet
21220 65 72 73 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20  ers into the.** 
21230 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
21240 66 20 5b 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  f [application-d
21250 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 74  efined SQL funct
21260 69 6f 6e 73 5d 20 61 72 65 20 70 72 6f 74 65 63  ions] are protec
21270 74 65 64 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ted..** The sqli
21280 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74  te3_value object
21290 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20   returned by.** 
212a0 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  [sqlite3_column_
212b0 76 61 6c 75 65 28 29 5d 20 69 73 20 75 6e 70 72  value()] is unpr
212c0 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 55 6e 70 72  otected..** Unpr
212d0 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
212e0 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 20 6d 61  value objects ma
212f0 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 77  y only be used w
21300 69 74 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  ith.** [sqlite3_
21310 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 5d 20  result_value()] 
21320 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  and [sqlite3_bin
21330 64 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a 2a 20 54  d_value()]..** T
21340 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  he [sqlite3_valu
21350 65 5f 62 6c 6f 62 20 7c 20 73 71 6c 69 74 65 33  e_blob | sqlite3
21360 5f 76 61 6c 75 65 5f 74 79 70 65 28 29 5d 20 66  _value_type()] f
21370 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20 69 6e 74 65  amily of.** inte
21380 72 66 61 63 65 73 20 72 65 71 75 69 72 65 20 70  rfaces require p
21390 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33  rotected sqlite3
213a0 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 73 2e 0a  _value objects..
213b0 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
213c0 74 20 4d 65 6d 20 73 71 6c 69 74 65 33 5f 76 61  t Mem sqlite3_va
213d0 6c 75 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  lue;../*.** CAPI
213e0 33 52 45 46 3a 20 53 51 4c 20 46 75 6e 63 74 69  3REF: SQL Functi
213f0 6f 6e 20 43 6f 6e 74 65 78 74 20 4f 62 6a 65 63  on Context Objec
21400 74 20 7b 48 31 36 30 30 31 7d 20 3c 53 32 30 32  t {H16001} <S202
21410 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  00>.**.** The co
21420 6e 74 65 78 74 20 69 6e 20 77 68 69 63 68 20 61  ntext in which a
21430 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 65  n SQL function e
21440 78 65 63 75 74 65 73 20 69 73 20 73 74 6f 72 65  xecutes is store
21450 64 20 69 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74  d in an.** sqlit
21460 65 33 5f 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63  e3_context objec
21470 74 2e 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  t.  A pointer to
21480 20 61 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74   an sqlite3_cont
21490 65 78 74 20 6f 62 6a 65 63 74 0a 2a 2a 20 69 73  ext object.** is
214a0 20 61 6c 77 61 79 73 20 66 69 72 73 74 20 70 61   always first pa
214b0 72 61 6d 65 74 65 72 20 74 6f 20 5b 61 70 70 6c  rameter to [appl
214c0 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
214d0 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a  SQL functions]..
214e0 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69  ** The applicati
214f0 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66  on-defined SQL f
21500 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e  unction implemen
21510 74 61 74 69 6f 6e 20 77 69 6c 6c 20 70 61 73 73  tation will pass
21520 20 74 68 69 73 0a 2a 2a 20 70 6f 69 6e 74 65 72   this.** pointer
21530 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 63 61   through into ca
21540 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  lls to [sqlite3_
21550 72 65 73 75 6c 74 5f 69 6e 74 20 7c 20 73 71 6c  result_int | sql
21560 69 74 65 33 5f 72 65 73 75 6c 74 28 29 5d 2c 0a  ite3_result()],.
21570 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 61 67 67 72  ** [sqlite3_aggr
21580 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 5d  egate_context()]
21590 2c 20 5b 73 71 6c 69 74 65 33 5f 75 73 65 72 5f  , [sqlite3_user_
215a0 64 61 74 61 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c  data()],.** [sql
215b0 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
215c0 68 61 6e 64 6c 65 28 29 5d 2c 20 5b 73 71 6c 69  handle()], [sqli
215d0 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28  te3_get_auxdata(
215e0 29 5d 2c 0a 2a 2a 20 61 6e 64 2f 6f 72 20 5b 73  )],.** and/or [s
215f0 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61  qlite3_set_auxda
21600 74 61 28 29 5d 2e 0a 2a 2f 0a 74 79 70 65 64 65  ta()]..*/.typede
21610 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
21620 5f 63 6f 6e 74 65 78 74 20 73 71 6c 69 74 65 33  _context sqlite3
21630 5f 63 6f 6e 74 65 78 74 3b 0a 0a 2f 2a 0a 2a 2a  _context;../*.**
21640 20 43 41 50 49 33 52 45 46 3a 20 42 69 6e 64 69   CAPI3REF: Bindi
21650 6e 67 20 56 61 6c 75 65 73 20 54 6f 20 50 72 65  ng Values To Pre
21660 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 73  pared Statements
21670 20 7b 48 31 33 35 30 30 7d 20 3c 53 37 30 33 30   {H13500} <S7030
21680 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  0>.** KEYWORDS: 
21690 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 7d  {host parameter}
216a0 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72   {host parameter
216b0 73 7d 20 7b 68 6f 73 74 20 70 61 72 61 6d 65 74  s} {host paramet
216c0 65 72 20 6e 61 6d 65 7d 0a 2a 2a 20 4b 45 59 57  er name}.** KEYW
216d0 4f 52 44 53 3a 20 7b 53 51 4c 20 70 61 72 61 6d  ORDS: {SQL param
216e0 65 74 65 72 7d 20 7b 53 51 4c 20 70 61 72 61 6d  eter} {SQL param
216f0 65 74 65 72 73 7d 20 7b 70 61 72 61 6d 65 74 65  eters} {paramete
21700 72 20 62 69 6e 64 69 6e 67 7d 0a 2a 2a 0a 2a 2a  r binding}.**.**
21710 20 49 6e 20 74 68 65 20 53 51 4c 20 73 74 72 69   In the SQL stri
21720 6e 67 73 20 69 6e 70 75 74 20 74 6f 20 5b 73 71  ngs input to [sq
21730 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
21740 28 29 5d 20 61 6e 64 20 69 74 73 20 76 61 72 69  ()] and its vari
21750 61 6e 74 73 2c 0a 2a 2a 20 6c 69 74 65 72 61 6c  ants,.** literal
21760 73 20 6d 61 79 20 62 65 20 72 65 70 6c 61 63 65  s may be replace
21770 64 20 62 79 20 61 20 5b 70 61 72 61 6d 65 74 65  d by a [paramete
21780 72 5d 20 74 68 61 74 20 6d 61 74 63 68 65 73 20  r] that matches 
21790 6f 6e 65 20 6f 66 20 66 6f 6c 6c 6f 77 69 6e 67  one of following
217a0 0a 2a 2a 20 74 65 6d 70 6c 61 74 65 73 3a 0a 2a  .** templates:.*
217b0 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  *.** <ul>.** <li
217c0 3e 20 20 3f 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 4e  >  ?.** <li>  ?N
217d0 4e 4e 0a 2a 2a 20 3c 6c 69 3e 20 20 3a 56 56 56  NN.** <li>  :VVV
217e0 0a 2a 2a 20 3c 6c 69 3e 20 20 40 56 56 56 0a 2a  .** <li>  @VVV.*
217f0 2a 20 3c 6c 69 3e 20 20 24 56 56 56 0a 2a 2a 20  * <li>  $VVV.** 
21800 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74  </ul>.**.** In t
21810 68 65 20 74 65 6d 70 6c 61 74 65 73 20 61 62 6f  he templates abo
21820 76 65 2c 20 4e 4e 4e 20 72 65 70 72 65 73 65 6e  ve, NNN represen
21830 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6c 69  ts an integer li
21840 74 65 72 61 6c 2c 0a 2a 2a 20 61 6e 64 20 56 56  teral,.** and VV
21850 56 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20  V represents an 
21860 61 6c 70 68 61 6e 75 6d 65 72 69 63 20 69 64 65  alphanumeric ide
21870 6e 74 69 66 65 72 2e 20 20 54 68 65 20 76 61 6c  ntifer.  The val
21880 75 65 73 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20  ues of these.** 
21890 70 61 72 61 6d 65 74 65 72 73 20 28 61 6c 73 6f  parameters (also
218a0 20 63 61 6c 6c 65 64 20 22 68 6f 73 74 20 70 61   called "host pa
218b0 72 61 6d 65 74 65 72 20 6e 61 6d 65 73 22 20 6f  rameter names" o
218c0 72 20 22 53 51 4c 20 70 61 72 61 6d 65 74 65 72  r "SQL parameter
218d0 73 22 29 0a 2a 2a 20 63 61 6e 20 62 65 20 73 65  s").** can be se
218e0 74 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69  t using the sqli
218f0 74 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75  te3_bind_*() rou
21900 74 69 6e 65 73 20 64 65 66 69 6e 65 64 20 68 65  tines defined he
21910 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  re..**.** The fi
21920 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
21930 74 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  the sqlite3_bind
21940 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 69 73  _*() routines is
21950 20 61 6c 77 61 79 73 0a 2a 2a 20 61 20 70 6f 69   always.** a poi
21960 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 73 71 6c  nter to the [sql
21970 69 74 65 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 63  ite3_stmt] objec
21980 74 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 0a  t returned from.
21990 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ** [sqlite3_prep
219a0 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 69 74 73  are_v2()] or its
219b0 20 76 61 72 69 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a   variants..**.**
219c0 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   The second argu
219d0 6d 65 6e 74 20 69 73 20 74 68 65 20 69 6e 64 65  ment is the inde
219e0 78 20 6f 66 20 74 68 65 20 53 51 4c 20 70 61 72  x of the SQL par
219f0 61 6d 65 74 65 72 20 74 6f 20 62 65 20 73 65 74  ameter to be set
21a00 2e 0a 2a 2a 20 54 68 65 20 6c 65 66 74 6d 6f 73  ..** The leftmos
21a10 74 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20  t SQL parameter 
21a20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20  has an index of 
21a30 31 2e 20 20 57 68 65 6e 20 74 68 65 20 73 61 6d  1.  When the sam
21a40 65 20 6e 61 6d 65 64 0a 2a 2a 20 53 51 4c 20 70  e named.** SQL p
21a50 61 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 64  arameter is used
21a60 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c   more than once,
21a70 20 73 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73   second and subs
21a80 65 71 75 65 6e 74 0a 2a 2a 20 6f 63 63 75 72 72  equent.** occurr
21a90 65 6e 63 65 73 20 68 61 76 65 20 74 68 65 20 73  ences have the s
21aa0 61 6d 65 20 69 6e 64 65 78 20 61 73 20 74 68 65  ame index as the
21ab0 20 66 69 72 73 74 20 6f 63 63 75 72 72 65 6e 63   first occurrenc
21ac0 65 2e 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20  e..** The index 
21ad0 66 6f 72 20 6e 61 6d 65 64 20 70 61 72 61 6d 65  for named parame
21ae0 74 65 72 73 20 63 61 6e 20 62 65 20 6c 6f 6f 6b  ters can be look
21af0 65 64 20 75 70 20 75 73 69 6e 67 20 74 68 65 0a  ed up using the.
21b00 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  ** [sqlite3_bind
21b10 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78  _parameter_index
21b20 28 29 5d 20 41 50 49 20 69 66 20 64 65 73 69 72  ()] API if desir
21b30 65 64 2e 20 20 54 68 65 20 69 6e 64 65 78 0a 2a  ed.  The index.*
21b40 2a 20 66 6f 72 20 22 3f 4e 4e 4e 22 20 70 61 72  * for "?NNN" par
21b50 61 6d 65 74 65 72 73 20 69 73 20 74 68 65 20 76  ameters is the v
21b60 61 6c 75 65 20 6f 66 20 4e 4e 4e 2e 0a 2a 2a 20  alue of NNN..** 
21b70 54 68 65 20 4e 4e 4e 20 76 61 6c 75 65 20 6d 75  The NNN value mu
21b80 73 74 20 62 65 20 62 65 74 77 65 65 6e 20 31 20  st be between 1 
21b90 61 6e 64 20 74 68 65 20 5b 73 71 6c 69 74 65 33  and the [sqlite3
21ba0 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20 70 61 72  _limit()].** par
21bb0 61 6d 65 74 65 72 20 5b 53 51 4c 49 54 45 5f 4c  ameter [SQLITE_L
21bc0 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55  IMIT_VARIABLE_NU
21bd0 4d 42 45 52 5d 20 28 64 65 66 61 75 6c 74 20 76  MBER] (default v
21be0 61 6c 75 65 3a 20 39 39 39 29 2e 0a 2a 2a 0a 2a  alue: 999)..**.*
21bf0 2a 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75  * The third argu
21c00 6d 65 6e 74 20 69 73 20 74 68 65 20 76 61 6c 75  ment is the valu
21c10 65 20 74 6f 20 62 69 6e 64 20 74 6f 20 74 68 65  e to bind to the
21c20 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a   parameter..**.*
21c30 2a 20 49 6e 20 74 68 6f 73 65 20 72 6f 75 74 69  * In those routi
21c40 6e 65 73 20 74 68 61 74 20 68 61 76 65 20 61 20  nes that have a 
21c50 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 2c  fourth argument,
21c60 20 69 74 73 20 76 61 6c 75 65 20 69 73 20 74 68   its value is th
21c70 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62  e.** number of b
21c80 79 74 65 73 20 69 6e 20 74 68 65 20 70 61 72 61  ytes in the para
21c90 6d 65 74 65 72 2e 20 20 54 6f 20 62 65 20 63 6c  meter.  To be cl
21ca0 65 61 72 3a 20 74 68 65 20 76 61 6c 75 65 20 69  ear: the value i
21cb0 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20  s the.** number 
21cc0 6f 66 20 3c 75 3e 62 79 74 65 73 3c 2f 75 3e 20  of <u>bytes</u> 
21cd0 69 6e 20 74 68 65 20 76 61 6c 75 65 2c 20 6e 6f  in the value, no
21ce0 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
21cf0 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20 49  characters..** I
21d00 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72  f the fourth par
21d10 61 6d 65 74 65 72 20 69 73 20 6e 65 67 61 74 69  ameter is negati
21d20 76 65 2c 20 74 68 65 20 6c 65 6e 67 74 68 20 6f  ve, the length o
21d30 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 0a  f the string is.
21d40 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** the number of
21d50 20 62 79 74 65 73 20 75 70 20 74 6f 20 74 68 65   bytes up to the
21d60 20 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d   first zero term
21d70 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  inator..**.** Th
21d80 65 20 66 69 66 74 68 20 61 72 67 75 6d 65 6e 74  e fifth argument
21d90 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64   to sqlite3_bind
21da0 5f 62 6c 6f 62 28 29 2c 20 73 71 6c 69 74 65 33  _blob(), sqlite3
21db0 5f 62 69 6e 64 5f 74 65 78 74 28 29 2c 20 61 6e  _bind_text(), an
21dc0 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e  d.** sqlite3_bin
21dd0 64 5f 74 65 78 74 31 36 28 29 20 69 73 20 61 20  d_text16() is a 
21de0 64 65 73 74 72 75 63 74 6f 72 20 75 73 65 64 20  destructor used 
21df0 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68  to dispose of th
21e00 65 20 42 4c 4f 42 20 6f 72 0a 2a 2a 20 73 74 72  e BLOB or.** str
21e10 69 6e 67 20 61 66 74 65 72 20 53 51 4c 69 74 65  ing after SQLite
21e20 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 77 69   has finished wi
21e30 74 68 20 69 74 2e 20 49 66 20 74 68 65 20 66 69  th it. If the fi
21e40 66 74 68 20 61 72 67 75 6d 65 6e 74 20 69 73 0a  fth argument is.
21e50 2a 2a 20 74 68 65 20 73 70 65 63 69 61 6c 20 76  ** the special v
21e60 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 53 54 41  alue [SQLITE_STA
21e70 54 49 43 5d 2c 20 74 68 65 6e 20 53 51 4c 69 74  TIC], then SQLit
21e80 65 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 74  e assumes that t
21e90 68 65 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f  he.** informatio
21ea0 6e 20 69 73 20 69 6e 20 73 74 61 74 69 63 2c 20  n is in static, 
21eb0 75 6e 6d 61 6e 61 67 65 64 20 73 70 61 63 65 20  unmanaged space 
21ec0 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65  and does not nee
21ed0 64 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a  d to be freed..*
21ee0 2a 20 49 66 20 74 68 65 20 66 69 66 74 68 20 61  * If the fifth a
21ef0 72 67 75 6d 65 6e 74 20 68 61 73 20 74 68 65 20  rgument has the 
21f00 76 61 6c 75 65 20 5b 53 51 4c 49 54 45 5f 54 52  value [SQLITE_TR
21f10 41 4e 53 49 45 4e 54 5d 2c 20 74 68 65 6e 0a 2a  ANSIENT], then.*
21f20 2a 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 69  * SQLite makes i
21f30 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65 20 63  ts own private c
21f40 6f 70 79 20 6f 66 20 74 68 65 20 64 61 74 61 20  opy of the data 
21f50 69 6d 6d 65 64 69 61 74 65 6c 79 2c 20 62 65 66  immediately, bef
21f60 6f 72 65 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74  ore.** the sqlit
21f70 65 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74  e3_bind_*() rout
21f80 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a  ine returns..**.
21f90 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62  ** The sqlite3_b
21fa0 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 72  ind_zeroblob() r
21fb0 6f 75 74 69 6e 65 20 62 69 6e 64 73 20 61 20 42  outine binds a B
21fc0 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 20 4e 20  LOB of length N 
21fd0 74 68 61 74 0a 2a 2a 20 69 73 20 66 69 6c 6c 65  that.** is fille
21fe0 64 20 77 69 74 68 20 7a 65 72 6f 65 73 2e 20 20  d with zeroes.  
21ff0 41 20 7a 65 72 6f 62 6c 6f 62 20 75 73 65 73 20  A zeroblob uses 
22000 61 20 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f  a fixed amount o
22010 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 28 6a 75 73  f memory.** (jus
22020 74 20 61 6e 20 69 6e 74 65 67 65 72 20 74 6f 20  t an integer to 
22030 68 6f 6c 64 20 69 74 73 20 73 69 7a 65 29 20 77  hold its size) w
22040 68 69 6c 65 20 69 74 20 69 73 20 62 65 69 6e 67  hile it is being
22050 20 70 72 6f 63 65 73 73 65 64 2e 0a 2a 2a 20 5a   processed..** Z
22060 65 72 6f 62 6c 6f 62 73 20 61 72 65 20 69 6e 74  eroblobs are int
22070 65 6e 64 65 64 20 74 6f 20 73 65 72 76 65 20 61  ended to serve a
22080 73 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 20 66  s placeholders f
22090 6f 72 20 42 4c 4f 42 73 20 77 68 6f 73 65 0a 2a  or BLOBs whose.*
220a0 2a 20 63 6f 6e 74 65 6e 74 20 69 73 20 6c 61 74  * content is lat
220b0 65 72 20 77 72 69 74 74 65 6e 20 75 73 69 6e 67  er written using
220c0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  .** [sqlite3_blo
220d0 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65  b_open | increme
220e0 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 72  ntal BLOB I/O] r
220f0 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 41 20 6e 65  outines..** A ne
22100 67 61 74 69 76 65 20 76 61 6c 75 65 20 66 6f 72  gative value for
22110 20 74 68 65 20 7a 65 72 6f 62 6c 6f 62 20 72 65   the zeroblob re
22120 73 75 6c 74 73 20 69 6e 20 61 20 7a 65 72 6f 2d  sults in a zero-
22130 6c 65 6e 67 74 68 20 42 4c 4f 42 2e 0a 2a 2a 0a  length BLOB..**.
22140 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62  ** The sqlite3_b
22150 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73  ind_*() routines
22160 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20   must be called 
22170 61 66 74 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65  after.** [sqlite
22180 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20  3_prepare_v2()] 
22190 28 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74  (and its variant
221a0 73 29 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72  s) or [sqlite3_r
221b0 65 73 65 74 28 29 5d 20 61 6e 64 0a 2a 2a 20 62  eset()] and.** b
221c0 65 66 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f 73  efore [sqlite3_s
221d0 74 65 70 28 29 5d 2e 0a 2a 2a 20 42 69 6e 64 69  tep()]..** Bindi
221e0 6e 67 73 20 61 72 65 20 6e 6f 74 20 63 6c 65 61  ngs are not clea
221f0 72 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c 69  red by the [sqli
22200 74 65 33 5f 72 65 73 65 74 28 29 5d 20 72 6f 75  te3_reset()] rou
22210 74 69 6e 65 2e 0a 2a 2a 20 55 6e 62 6f 75 6e 64  tine..** Unbound
22220 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20   parameters are 
22230 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 4e  interpreted as N
22240 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  ULL..**.** These
22250 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e   routines return
22260 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20   [SQLITE_OK] on 
22270 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72  success or an er
22280 72 6f 72 20 63 6f 64 65 20 69 66 0a 2a 2a 20 61  ror code if.** a
22290 6e 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f  nything goes wro
222a0 6e 67 2e 20 20 5b 53 51 4c 49 54 45 5f 52 41 4e  ng.  [SQLITE_RAN
222b0 47 45 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20  GE] is returned 
222c0 69 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  if the parameter
222d0 0a 2a 2a 20 69 6e 64 65 78 20 69 73 20 6f 75 74  .** index is out
222e0 20 6f 66 20 72 61 6e 67 65 2e 20 20 5b 53 51 4c   of range.  [SQL
222f0 49 54 45 5f 4e 4f 4d 45 4d 5d 20 69 73 20 72 65  ITE_NOMEM] is re
22300 74 75 72 6e 65 64 20 69 66 20 6d 61 6c 6c 6f 63  turned if malloc
22310 28 29 20 66 61 69 6c 73 2e 0a 2a 2a 20 5b 53 51  () fails..** [SQ
22320 4c 49 54 45 5f 4d 49 53 55 53 45 5d 20 6d 69 67  LITE_MISUSE] mig
22330 68 74 20 62 65 20 72 65 74 75 72 6e 65 64 20 69  ht be returned i
22340 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  f these routines
22350 20 61 72 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61   are called on a
22360 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68  .** virtual mach
22370 69 6e 65 20 74 68 61 74 20 69 73 20 74 68 65 20  ine that is the 
22380 77 72 6f 6e 67 20 73 74 61 74 65 20 6f 72 20 77  wrong state or w
22390 68 69 63 68 20 68 61 73 20 61 6c 72 65 61 64 79  hich has already
223a0 20 62 65 65 6e 20 66 69 6e 61 6c 69 7a 65 64 2e   been finalized.
223b0 0a 2a 2a 20 44 65 74 65 63 74 69 6f 6e 20 6f 66  .** Detection of
223c0 20 6d 69 73 75 73 65 20 69 73 20 75 6e 72 65 6c   misuse is unrel
223d0 69 61 62 6c 65 2e 20 20 41 70 70 6c 69 63 61 74  iable.  Applicat
223e0 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20  ions should not 
223f0 64 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 53 51 4c  depend.** on SQL
22400 49 54 45 5f 4d 49 53 55 53 45 20 72 65 74 75 72  ITE_MISUSE retur
22410 6e 73 2e 20 20 53 51 4c 49 54 45 5f 4d 49 53 55  ns.  SQLITE_MISU
22420 53 45 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74  SE is intended t
22430 6f 20 69 6e 64 69 63 61 74 65 20 61 0a 2a 2a 20  o indicate a.** 
22440 61 20 6c 6f 67 69 63 20 65 72 72 6f 72 20 69 6e  a logic error in
22450 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
22460 2e 20 20 46 75 74 75 72 65 20 76 65 72 73 69 6f  .  Future versio
22470 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d 69 67  ns of SQLite mig
22480 68 74 0a 2a 2a 20 70 61 6e 69 63 20 72 61 74 68  ht.** panic rath
22490 65 72 20 74 68 61 6e 20 72 65 74 75 72 6e 20 53  er than return S
224a0 51 4c 49 54 45 5f 4d 49 53 55 53 45 2e 0a 2a 2a  QLITE_MISUSE..**
224b0 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73  .** See also: [s
224c0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
224d0 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 0a  meter_count()],.
224e0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  ** [sqlite3_bind
224f0 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28  _parameter_name(
22500 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  )], and [sqlite3
22510 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
22520 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  index()]..**.** 
22530 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a  Requirements:.**
22540 20 5b 48 31 33 35 30 36 5d 20 5b 48 31 33 35 30   [H13506] [H1350
22550 39 5d 20 5b 48 31 33 35 31 32 5d 20 5b 48 31 33  9] [H13512] [H13
22560 35 31 35 5d 20 5b 48 31 33 35 31 38 5d 20 5b 48  515] [H13518] [H
22570 31 33 35 32 31 5d 20 5b 48 31 33 35 32 34 5d 20  13521] [H13524] 
22580 5b 48 31 33 35 32 37 5d 0a 2a 2a 20 5b 48 31 33  [H13527].** [H13
22590 35 33 30 5d 20 5b 48 31 33 35 33 33 5d 20 5b 48  530] [H13533] [H
225a0 31 33 35 33 36 5d 20 5b 48 31 33 35 33 39 5d 20  13536] [H13539] 
225b0 5b 48 31 33 35 34 32 5d 20 5b 48 31 33 35 34 35  [H13542] [H13545
225c0 5d 20 5b 48 31 33 35 34 38 5d 20 5b 48 31 33 35  ] [H13548] [H135
225d0 35 31 5d 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45  51].**.*/.SQLITE
225e0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
225f0 5f 62 69 6e 64 5f 62 6c 6f 62 28 73 71 6c 69 74  _bind_blob(sqlit
22600 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63  e3_stmt*, int, c
22610 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20  onst void*, int 
22620 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  n, void(*)(void*
22630 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ));.SQLITE_API i
22640 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
22650 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73  double(sqlite3_s
22660 74 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f 75 62 6c  tmt*, int, doubl
22670 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  e);.SQLITE_API i
22680 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
22690 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  int(sqlite3_stmt
226a0 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  *, int, int);.SQ
226b0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
226c0 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
226d0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
226e0 6e 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  nt, sqlite3_int6
226f0 34 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  4);.SQLITE_API i
22700 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
22710 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d  null(sqlite3_stm
22720 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  t*, int);.SQLITE
22730 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
22740 5f 62 69 6e 64 5f 74 65 78 74 28 73 71 6c 69 74  _bind_text(sqlit
22750 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63  e3_stmt*, int, c
22760 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 20  onst char*, int 
22770 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  n, void(*)(void*
22780 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ));.SQLITE_API i
22790 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
227a0 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73  text16(sqlite3_s
227b0 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74  tmt*, int, const
227c0 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69   void*, int, voi
227d0 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51  d(*)(void*));.SQ
227e0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
227f0 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28  ite3_bind_value(
22800 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
22810 6e 74 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  nt, const sqlite
22820 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54  3_value*);.SQLIT
22830 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
22840 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28  3_bind_zeroblob(
22850 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
22860 6e 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a  nt, int n);../*.
22870 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d  ** CAPI3REF: Num
22880 62 65 72 20 4f 66 20 53 51 4c 20 50 61 72 61 6d  ber Of SQL Param
22890 65 74 65 72 73 20 7b 48 31 33 36 30 30 7d 20 3c  eters {H13600} <
228a0 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S70300>.**.** Th
228b0 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62  is routine can b
228c0 65 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20 74  e used to find t
228d0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 5b 53 51  he number of [SQ
228e0 4c 20 70 61 72 61 6d 65 74 65 72 73 5d 0a 2a 2a  L parameters].**
228f0 20 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20   in a [prepared 
22900 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 53 51 4c  statement].  SQL
22910 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20   parameters are 
22920 74 6f 6b 65 6e 73 20 6f 66 20 74 68 65 0a 2a 2a  tokens of the.**
22930 20 66 6f 72 6d 20 22 3f 22 2c 20 22 3f 4e 4e 4e   form "?", "?NNN
22940 22 2c 20 22 3a 41 41 41 22 2c 20 22 24 41 41 41  ", ":AAA", "$AAA
22950 22 2c 20 6f 72 20 22 40 41 41 41 22 20 74 68 61  ", or "@AAA" tha
22960 74 20 73 65 72 76 65 20 61 73 0a 2a 2a 20 70 6c  t serve as.** pl
22970 61 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 76  aceholders for v
22980 61 6c 75 65 73 20 74 68 61 74 20 61 72 65 20 5b  alues that are [
22990 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f  sqlite3_bind_blo
229a0 62 20 7c 20 62 6f 75 6e 64 5d 0a 2a 2a 20 74 6f  b | bound].** to
229b0 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20   the parameters 
229c0 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65 2e  at a later time.
229d0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
229e0 69 6e 65 20 61 63 74 75 61 6c 6c 79 20 72 65 74  ine actually ret
229f0 75 72 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f  urns the index o
22a00 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 28 72  f the largest (r
22a10 69 67 68 74 6d 6f 73 74 29 0a 2a 2a 20 70 61 72  ightmost).** par
22a20 61 6d 65 74 65 72 2e 20 46 6f 72 20 61 6c 6c 20  ameter. For all 
22a30 66 6f 72 6d 73 20 65 78 63 65 70 74 20 3f 4e 4e  forms except ?NN
22a40 4e 2c 20 74 68 69 73 20 77 69 6c 6c 20 63 6f 72  N, this will cor
22a50 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 0a 2a  respond to the.*
22a60 2a 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 71  * number of uniq
22a70 75 65 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20  ue parameters.  
22a80 49 66 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66  If parameters of
22a90 20 74 68 65 20 3f 4e 4e 4e 20 61 72 65 20 75 73   the ?NNN are us
22aa0 65 64 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 61 79  ed,.** there may
22ab0 20 62 65 20 67 61 70 73 20 69 6e 20 74 68 65 20   be gaps in the 
22ac0 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  list..**.** See 
22ad0 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62  also: [sqlite3_b
22ae0 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33  ind_blob|sqlite3
22af0 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71  _bind()],.** [sq
22b00 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
22b10 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e  eter_name()], an
22b20 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69  d.** [sqlite3_bi
22b30 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64  nd_parameter_ind
22b40 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  ex()]..**.** Req
22b50 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
22b60 31 33 36 30 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45  13601].*/.SQLITE
22b70 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
22b80 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
22b90 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74  count(sqlite3_st
22ba0 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  mt*);../*.** CAP
22bb0 49 33 52 45 46 3a 20 4e 61 6d 65 20 4f 66 20 41  I3REF: Name Of A
22bc0 20 48 6f 73 74 20 50 61 72 61 6d 65 74 65 72 20   Host Parameter 
22bd0 7b 48 31 33 36 32 30 7d 20 3c 53 37 30 33 30 30  {H13620} <S70300
22be0 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  >.**.** This rou
22bf0 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 70  tine returns a p
22c00 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 61  ointer to the na
22c10 6d 65 20 6f 66 20 74 68 65 20 6e 2d 74 68 0a 2a  me of the n-th.*
22c20 2a 20 5b 53 51 4c 20 70 61 72 61 6d 65 74 65 72  * [SQL parameter
22c30 5d 20 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64  ] in a [prepared
22c40 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20   statement]..** 
22c50 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 20 6f  SQL parameters o
22c60 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 4e 4e 4e  f the form "?NNN
22c70 22 20 6f 72 20 22 3a 41 41 41 22 20 6f 72 20 22  " or ":AAA" or "
22c80 40 41 41 41 22 20 6f 72 20 22 24 41 41 41 22 0a  @AAA" or "$AAA".
22c90 2a 2a 20 68 61 76 65 20 61 20 6e 61 6d 65 20 77  ** have a name w
22ca0 68 69 63 68 20 69 73 20 74 68 65 20 73 74 72 69  hich is the stri
22cb0 6e 67 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41  ng "?NNN" or ":A
22cc0 41 41 22 20 6f 72 20 22 40 41 41 41 22 20 6f 72  AA" or "@AAA" or
22cd0 20 22 24 41 41 41 22 0a 2a 2a 20 72 65 73 70 65   "$AAA".** respe
22ce0 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 49 6e 20 6f  ctively..** In o
22cf0 74 68 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20  ther words, the 
22d00 69 6e 69 74 69 61 6c 20 22 3a 22 20 6f 72 20 22  initial ":" or "
22d10 24 22 20 6f 72 20 22 40 22 20 6f 72 20 22 3f 22  $" or "@" or "?"
22d20 0a 2a 2a 20 69 73 20 69 6e 63 6c 75 64 65 64 20  .** is included 
22d30 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e  as part of the n
22d40 61 6d 65 2e 0a 2a 2a 20 50 61 72 61 6d 65 74 65  ame..** Paramete
22d50 72 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  rs of the form "
22d60 3f 22 20 77 69 74 68 6f 75 74 20 61 20 66 6f 6c  ?" without a fol
22d70 6c 6f 77 69 6e 67 20 69 6e 74 65 67 65 72 20 68  lowing integer h
22d80 61 76 65 20 6e 6f 20 6e 61 6d 65 0a 2a 2a 20 61  ave no name.** a
22d90 6e 64 20 61 72 65 20 61 6c 73 6f 20 72 65 66 65  nd are also refe
22da0 72 72 65 64 20 74 6f 20 61 73 20 22 61 6e 6f 6e  rred to as "anon
22db0 79 6d 6f 75 73 20 70 61 72 61 6d 65 74 65 72 73  ymous parameters
22dc0 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  "..**.** The fir
22dd0 73 74 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65  st host paramete
22de0 72 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f  r has an index o
22df0 66 20 31 2c 20 6e 6f 74 20 30 2e 0a 2a 2a 0a 2a  f 1, not 0..**.*
22e00 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 6e  * If the value n
22e10 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65   is out of range
22e20 20 6f 72 20 69 66 20 74 68 65 20 6e 2d 74 68 20   or if the n-th 
22e30 70 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20  parameter is.** 
22e40 6e 61 6d 65 6c 65 73 73 2c 20 74 68 65 6e 20 4e  nameless, then N
22e50 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ULL is returned.
22e60 20 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73    The returned s
22e70 74 72 69 6e 67 20 69 73 0a 2a 2a 20 61 6c 77 61  tring is.** alwa
22e80 79 73 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f  ys in UTF-8 enco
22e90 64 69 6e 67 20 65 76 65 6e 20 69 66 20 74 68 65  ding even if the
22ea0 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 65 72   named parameter
22eb0 20 77 61 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c   was.** original
22ec0 6c 79 20 73 70 65 63 69 66 69 65 64 20 61 73 20  ly specified as 
22ed0 55 54 46 2d 31 36 20 69 6e 20 5b 73 71 6c 69 74  UTF-16 in [sqlit
22ee0 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20  e3_prepare16()] 
22ef0 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70  or.** [sqlite3_p
22f00 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a  repare16_v2()]..
22f10 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
22f20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c  [sqlite3_bind_bl
22f30 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28  ob|sqlite3_bind(
22f40 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  )],.** [sqlite3_
22f50 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63  bind_parameter_c
22f60 6f 75 6e 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20  ount()], and.** 
22f70 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61  [sqlite3_bind_pa
22f80 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d  rameter_index()]
22f90 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
22fa0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36 32 31  ents:.** [H13621
22fb0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
22fc0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
22fd0 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
22fe0 65 72 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f  er_name(sqlite3_
22ff0 73 74 6d 74 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a  stmt*, int);../*
23000 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6e  .** CAPI3REF: In
23010 64 65 78 20 4f 66 20 41 20 50 61 72 61 6d 65 74  dex Of A Paramet
23020 65 72 20 57 69 74 68 20 41 20 47 69 76 65 6e 20  er With A Given 
23030 4e 61 6d 65 20 7b 48 31 33 36 34 30 7d 20 3c 53  Name {H13640} <S
23040 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 52 65 74  70300>.**.** Ret
23050 75 72 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66  urn the index of
23060 20 61 6e 20 53 51 4c 20 70 61 72 61 6d 65 74 65   an SQL paramete
23070 72 20 67 69 76 65 6e 20 69 74 73 20 6e 61 6d 65  r given its name
23080 2e 20 20 54 68 65 0a 2a 2a 20 69 6e 64 65 78 20  .  The.** index 
23090 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69  value returned i
230a0 73 20 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75  s suitable for u
230b0 73 65 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  se as the second
230c0 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 74 6f  .** parameter to
230d0 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62   [sqlite3_bind_b
230e0 6c 6f 62 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64  lob|sqlite3_bind
230f0 28 29 5d 2e 20 20 41 20 7a 65 72 6f 0a 2a 2a 20  ()].  A zero.** 
23100 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 6e  is returned if n
23110 6f 20 6d 61 74 63 68 69 6e 67 20 70 61 72 61 6d  o matching param
23120 65 74 65 72 20 69 73 20 66 6f 75 6e 64 2e 20 20  eter is found.  
23130 54 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a  The parameter.**
23140 20 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 67 69   name must be gi
23150 76 65 6e 20 69 6e 20 55 54 46 2d 38 20 65 76 65  ven in UTF-8 eve
23160 6e 20 69 66 20 74 68 65 20 6f 72 69 67 69 6e 61  n if the origina
23170 6c 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 77  l statement.** w
23180 61 73 20 70 72 65 70 61 72 65 64 20 66 72 6f 6d  as prepared from
23190 20 55 54 46 2d 31 36 20 74 65 78 74 20 75 73 69   UTF-16 text usi
231a0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ng [sqlite3_prep
231b0 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a 0a  are16_v2()]..**.
231c0 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71  ** See also: [sq
231d0 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c  lite3_bind_blob|
231e0 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2c  sqlite3_bind()],
231f0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e  .** [sqlite3_bin
23200 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e  d_parameter_coun
23210 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71  t()], and.** [sq
23220 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
23230 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a 2a  eter_index()]..*
23240 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
23250 73 3a 0a 2a 2a 20 5b 48 31 33 36 34 31 5d 0a 2a  s:.** [H13641].*
23260 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
23270 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61   sqlite3_bind_pa
23280 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 73 71  rameter_index(sq
23290 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 63 6f 6e  lite3_stmt*, con
232a0 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 3b  st char *zName);
232b0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
232c0 3a 20 52 65 73 65 74 20 41 6c 6c 20 42 69 6e 64  : Reset All Bind
232d0 69 6e 67 73 20 4f 6e 20 41 20 50 72 65 70 61 72  ings On A Prepar
232e0 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31  ed Statement {H1
232f0 33 36 36 30 7d 20 3c 53 37 30 33 30 30 3e 0a 2a  3660} <S70300>.*
23300 2a 0a 2a 2a 20 43 6f 6e 74 72 61 72 79 20 74 6f  *.** Contrary to
23310 20 74 68 65 20 69 6e 74 75 69 74 69 6f 6e 20 6f   the intuition o
23320 66 20 6d 61 6e 79 2c 20 5b 73 71 6c 69 74 65 33  f many, [sqlite3
23330 5f 72 65 73 65 74 28 29 5d 20 64 6f 65 73 20 6e  _reset()] does n
23340 6f 74 20 72 65 73 65 74 0a 2a 2a 20 74 68 65 20  ot reset.** the 
23350 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c  [sqlite3_bind_bl
23360 6f 62 20 7c 20 62 69 6e 64 69 6e 67 73 5d 20 6f  ob | bindings] o
23370 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74  n a [prepared st
23380 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 55 73 65  atement]..** Use
23390 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f   this routine to
233a0 20 72 65 73 65 74 20 61 6c 6c 20 68 6f 73 74 20   reset all host 
233b0 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 4e 55  parameters to NU
233c0 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  LL..**.** Requir
233d0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 36  ements:.** [H136
233e0 36 31 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  61].*/.SQLITE_AP
233f0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6c  I int sqlite3_cl
23400 65 61 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c  ear_bindings(sql
23410 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a 2f 2a  ite3_stmt*);../*
23420 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75  .** CAPI3REF: Nu
23430 6d 62 65 72 20 4f 66 20 43 6f 6c 75 6d 6e 73 20  mber Of Columns 
23440 49 6e 20 41 20 52 65 73 75 6c 74 20 53 65 74 20  In A Result Set 
23450 7b 48 31 33 37 31 30 7d 20 3c 53 31 30 37 30 30  {H13710} <S10700
23460 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  >.**.** Return t
23470 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
23480 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75  umns in the resu
23490 6c 74 20 73 65 74 20 72 65 74 75 72 6e 65 64 20  lt set returned 
234a0 62 79 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61  by the.** [prepa
234b0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20  red statement]. 
234c0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
234d0 75 72 6e 73 20 30 20 69 66 20 70 53 74 6d 74 20  urns 0 if pStmt 
234e0 69 73 20 61 6e 20 53 51 4c 0a 2a 2a 20 73 74 61  is an SQL.** sta
234f0 74 65 6d 65 6e 74 20 74 68 61 74 20 64 6f 65 73  tement that does
23500 20 6e 6f 74 20 72 65 74 75 72 6e 20 64 61 74 61   not return data
23510 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 61 6e   (for example an
23520 20 5b 55 50 44 41 54 45 5d 29 2e 0a 2a 2a 0a 2a   [UPDATE])..**.*
23530 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a  * Requirements:.
23540 2a 2a 20 5b 48 31 33 37 31 31 5d 0a 2a 2f 0a 53  ** [H13711].*/.S
23550 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
23560 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75  lite3_column_cou
23570 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  nt(sqlite3_stmt 
23580 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20  *pStmt);../*.** 
23590 43 41 50 49 33 52 45 46 3a 20 43 6f 6c 75 6d 6e  CAPI3REF: Column
235a0 20 4e 61 6d 65 73 20 49 6e 20 41 20 52 65 73 75   Names In A Resu
235b0 6c 74 20 53 65 74 20 7b 48 31 33 37 32 30 7d 20  lt Set {H13720} 
235c0 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S10700>.**.** T
235d0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65  hese routines re
235e0 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 61 73  turn the name as
235f0 73 69 67 6e 65 64 20 74 6f 20 61 20 70 61 72 74  signed to a part
23600 69 63 75 6c 61 72 20 63 6f 6c 75 6d 6e 0a 2a 2a  icular column.**
23610 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
23620 65 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d  et of a [SELECT]
23630 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65   statement.  The
23640 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
23650 6e 61 6d 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66  name().** interf
23660 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f  ace returns a po
23670 69 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d  inter to a zero-
23680 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38  terminated UTF-8
23690 20 73 74 72 69 6e 67 0a 2a 2a 20 61 6e 64 20 73   string.** and s
236a0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61  qlite3_column_na
236b0 6d 65 31 36 28 29 20 72 65 74 75 72 6e 73 20 61  me16() returns a
236c0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 7a 65   pointer to a ze
236d0 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a  ro-terminated.**
236e0 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 20   UTF-16 string. 
236f0 20 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d   The first param
23700 65 74 65 72 20 69 73 20 74 68 65 20 5b 70 72 65  eter is the [pre
23710 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
23720 0a 2a 2a 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  .** that impleme
23730 6e 74 73 20 74 68 65 20 5b 53 45 4c 45 43 54 5d  nts the [SELECT]
23740 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 20   statement. The 
23750 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
23760 20 69 73 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d   is the.** colum
23770 6e 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 6c  n number.  The l
23780 65 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69  eftmost column i
23790 73 20 6e 75 6d 62 65 72 20 30 2e 0a 2a 2a 0a 2a  s number 0..**.*
237a0 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73  * The returned s
237b0 74 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 69 73  tring pointer is
237c0 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 65 69 74   valid until eit
237d0 68 65 72 20 74 68 65 20 5b 70 72 65 70 61 72 65  her the [prepare
237e0 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20  d statement].** 
237f0 69 73 20 64 65 73 74 72 6f 79 65 64 20 62 79 20  is destroyed by 
23800 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a  [sqlite3_finaliz
23810 65 28 29 5d 20 6f 72 20 75 6e 74 69 6c 20 74 68  e()] or until th
23820 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 0a 2a  e next call to.*
23830 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  * sqlite3_column
23840 5f 6e 61 6d 65 28 29 20 6f 72 20 73 71 6c 69 74  _name() or sqlit
23850 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36  e3_column_name16
23860 28 29 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 63  () on the same c
23870 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  olumn..**.** If 
23880 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
23890 20 66 61 69 6c 73 20 64 75 72 69 6e 67 20 74 68   fails during th
238a0 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 20  e processing of 
238b0 65 69 74 68 65 72 20 72 6f 75 74 69 6e 65 0a 2a  either routine.*
238c0 2a 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 64  * (for example d
238d0 75 72 69 6e 67 20 61 20 63 6f 6e 76 65 72 73 69  uring a conversi
238e0 6f 6e 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f  on from UTF-8 to
238f0 20 55 54 46 2d 31 36 29 20 74 68 65 6e 20 61 0a   UTF-16) then a.
23900 2a 2a 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20  ** NULL pointer 
23910 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
23920 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61  ** The name of a
23930 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69   result column i
23940 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  s the value of t
23950 68 65 20 22 41 53 22 20 63 6c 61 75 73 65 20 66  he "AS" clause f
23960 6f 72 0a 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d  or.** that colum
23970 6e 2c 20 69 66 20 74 68 65 72 65 20 69 73 20 61  n, if there is a
23980 6e 20 41 53 20 63 6c 61 75 73 65 2e 20 20 49 66  n AS clause.  If
23990 20 74 68 65 72 65 20 69 73 20 6e 6f 20 41 53 20   there is no AS 
239a0 63 6c 61 75 73 65 0a 2a 2a 20 74 68 65 6e 20 74  clause.** then t
239b0 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 63  he name of the c
239c0 6f 6c 75 6d 6e 20 69 73 20 75 6e 73 70 65 63 69  olumn is unspeci
239d0 66 69 65 64 20 61 6e 64 20 6d 61 79 20 63 68 61  fied and may cha
239e0 6e 67 65 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20  nge from.** one 
239f0 72 65 6c 65 61 73 65 20 6f 66 20 53 51 4c 69 74  release of SQLit
23a00 65 20 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a  e to the next..*
23a10 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
23a20 73 3a 0a 2a 2a 20 5b 48 31 33 37 32 31 5d 20 5b  s:.** [H13721] [
23a30 48 31 33 37 32 33 5d 20 5b 48 31 33 37 32 34 5d  H13723] [H13724]
23a40 20 5b 48 31 33 37 32 35 5d 20 5b 48 31 33 37 32   [H13725] [H1372
23a50 36 5d 20 5b 48 31 33 37 32 37 5d 0a 2a 2f 0a 53  6] [H13727].*/.S
23a60 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
23a70 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f  char *sqlite3_co
23a80 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65  lumn_name(sqlite
23a90 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29 3b  3_stmt*, int N);
23aa0 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
23ab0 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
23ac0 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73 71  column_name16(sq
23ad0 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
23ae0 20 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49   N);../*.** CAPI
23af0 33 52 45 46 3a 20 53 6f 75 72 63 65 20 4f 66 20  3REF: Source Of 
23b00 44 61 74 61 20 49 6e 20 41 20 51 75 65 72 79 20  Data In A Query 
23b10 52 65 73 75 6c 74 20 7b 48 31 33 37 34 30 7d 20  Result {H13740} 
23b20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S10700>.**.** T
23b30 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 70 72  hese routines pr
23b40 6f 76 69 64 65 20 61 20 6d 65 61 6e 73 20 74 6f  ovide a means to
23b50 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20   determine what 
23b60 63 6f 6c 75 6d 6e 20 6f 66 20 77 68 61 74 0a 2a  column of what.*
23b70 2a 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63 68  * table in which
23b80 20 64 61 74 61 62 61 73 65 20 61 20 72 65 73 75   database a resu
23b90 6c 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d  lt of a [SELECT]
23ba0 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65 73   statement comes
23bb0 20 66 72 6f 6d 2e 0a 2a 2a 20 54 68 65 20 6e 61   from..** The na
23bc0 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
23bd0 73 65 20 6f 72 20 74 61 62 6c 65 20 6f 72 20 63  se or table or c
23be0 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 72 65 74  olumn can be ret
23bf0 75 72 6e 65 64 20 61 73 0a 2a 2a 20 65 69 74 68  urned as.** eith
23c00 65 72 20 61 20 55 54 46 2d 38 20 6f 72 20 55 54  er a UTF-8 or UT
23c10 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54 68  F-16 string.  Th
23c20 65 20 5f 64 61 74 61 62 61 73 65 5f 20 72 6f 75  e _database_ rou
23c30 74 69 6e 65 73 20 72 65 74 75 72 6e 0a 2a 2a 20  tines return.** 
23c40 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  the database nam
23c50 65 2c 20 74 68 65 20 5f 74 61 62 6c 65 5f 20 72  e, the _table_ r
23c60 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 74  outines return t
23c70 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2c 20 61  he table name, a
23c80 6e 64 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e  nd.** the origin
23c90 5f 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72  _ routines retur
23ca0 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  n the column nam
23cb0 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  e..** The return
23cc0 65 64 20 73 74 72 69 6e 67 20 69 73 20 76 61 6c  ed string is val
23cd0 69 64 20 75 6e 74 69 6c 20 74 68 65 20 5b 70 72  id until the [pr
23ce0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
23cf0 5d 20 69 73 20 64 65 73 74 72 6f 79 65 64 0a 2a  ] is destroyed.*
23d00 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  * using [sqlite3
23d10 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20  _finalize()] or 
23d20 75 6e 74 69 6c 20 74 68 65 20 73 61 6d 65 20 69  until the same i
23d30 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72 65  nformation is re
23d40 71 75 65 73 74 65 64 0a 2a 2a 20 61 67 61 69 6e  quested.** again
23d50 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20   in a different 
23d60 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  encoding..**.** 
23d70 54 68 65 20 6e 61 6d 65 73 20 72 65 74 75 72 6e  The names return
23d80 65 64 20 61 72 65 20 74 68 65 20 6f 72 69 67 69  ed are the origi
23d90 6e 61 6c 20 75 6e 2d 61 6c 69 61 73 65 64 20 6e  nal un-aliased n
23da0 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64  ames of the.** d
23db0 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 2c 20  atabase, table, 
23dc0 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a  and column..**.*
23dd0 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75  * The first argu
23de0 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c 6c  ment to the foll
23df0 6f 77 69 6e 67 20 63 61 6c 6c 73 20 69 73 20 61  owing calls is a
23e00 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
23e10 6d 65 6e 74 5d 2e 0a 2a 2a 20 54 68 65 73 65 20  ment]..** These 
23e20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72 6e  functions return
23e30 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
23e40 75 74 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d  ut the Nth colum
23e50 6e 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a  n returned by.**
23e60 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c 20   the statement, 
23e70 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 73  where N is the s
23e80 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20 61  econd function a
23e90 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49  rgument..**.** I
23ea0 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e  f the Nth column
23eb0 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
23ec0 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 6e   statement is an
23ed0 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 0a 2a   expression or.*
23ee0 2a 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 69  * subquery and i
23ef0 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 76  s not a column v
23f00 61 6c 75 65 2c 20 74 68 65 6e 20 61 6c 6c 20 6f  alue, then all o
23f10 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e  f these function
23f20 73 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c  s return.** NULL
23f30 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  .  These routine
23f40 20 6d 69 67 68 74 20 61 6c 73 6f 20 72 65 74 75   might also retu
23f50 72 6e 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65 6d  rn NULL if a mem
23f60 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65  ory allocation e
23f70 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2e 20  rror.** occurs. 
23f80 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 79   Otherwise, they
23f90 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65   return the name
23fa0 20 6f 66 20 74 68 65 20 61 74 74 61 63 68 65 64   of the attached
23fb0 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65   database, table
23fc0 0a 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 74  .** and column t
23fd0 68 61 74 20 71 75 65 72 79 20 72 65 73 75 6c 74  hat query result
23fe0 20 63 6f 6c 75 6d 6e 20 77 61 73 20 65 78 74 72   column was extr
23ff0 61 63 74 65 64 20 66 72 6f 6d 2e 0a 2a 2a 0a 2a  acted from..**.*
24000 2a 20 41 73 20 77 69 74 68 20 61 6c 6c 20 6f 74  * As with all ot
24010 68 65 72 20 53 51 4c 69 74 65 20 41 50 49 73 2c  her SQLite APIs,
24020 20 74 68 6f 73 65 20 70 6f 73 74 66 69 78 65 64   those postfixed
24030 20 77 69 74 68 20 22 31 36 22 20 72 65 74 75 72   with "16" retur
24040 6e 0a 2a 2a 20 55 54 46 2d 31 36 20 65 6e 63 6f  n.** UTF-16 enco
24050 64 65 64 20 73 74 72 69 6e 67 73 2c 20 74 68 65  ded strings, the
24060 20 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 73   other functions
24070 20 72 65 74 75 72 6e 20 55 54 46 2d 38 2e 20 7b   return UTF-8. {
24080 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  END}.**.** These
24090 20 41 50 49 73 20 61 72 65 20 6f 6e 6c 79 20 61   APIs are only a
240a0 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65 20  vailable if the 
240b0 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d 70  library was comp
240c0 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a 2a  iled with the.**
240d0 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f   [SQLITE_ENABLE_
240e0 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 5d  COLUMN_METADATA]
240f0 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72 20   C-preprocessor 
24100 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e 0a  symbol defined..
24110 2a 2a 0a 2a 2a 20 7b 41 31 33 37 35 31 7d 0a 2a  **.** {A13751}.*
24120 2a 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65  * If two or more
24130 20 74 68 72 65 61 64 73 20 63 61 6c 6c 20 6f 6e   threads call on
24140 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65  e or more of the
24150 73 65 20 72 6f 75 74 69 6e 65 73 20 61 67 61 69  se routines agai
24160 6e 73 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20  nst the same.** 
24170 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
24180 6e 74 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 61 74  nt and column at
24190 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 74   the same time t
241a0 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20  hen the results 
241b0 61 72 65 0a 2a 2a 20 75 6e 64 65 66 69 6e 65 64  are.** undefined
241c0 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
241d0 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37 34 31  ents:.** [H13741
241e0 5d 20 5b 48 31 33 37 34 32 5d 20 5b 48 31 33 37  ] [H13742] [H137
241f0 34 33 5d 20 5b 48 31 33 37 34 34 5d 20 5b 48 31  43] [H13744] [H1
24200 33 37 34 35 5d 20 5b 48 31 33 37 34 36 5d 20 5b  3745] [H13746] [
24210 48 31 33 37 34 38 5d 0a 2a 2a 0a 2a 2a 20 49 66  H13748].**.** If
24220 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 74 68 72   two or more thr
24230 65 61 64 73 20 63 61 6c 6c 20 6f 6e 65 20 6f 72  eads call one or
24240 20 6d 6f 72 65 0a 2a 2a 20 5b 73 71 6c 69 74 65   more.** [sqlite
24250 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73  3_column_databas
24260 65 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75 6d 6e 20  e_name | column 
24270 6d 65 74 61 64 61 74 61 20 69 6e 74 65 72 66 61  metadata interfa
24280 63 65 73 5d 0a 2a 2a 20 66 6f 72 20 74 68 65 20  ces].** for the 
24290 73 61 6d 65 20 5b 70 72 65 70 61 72 65 64 20 73  same [prepared s
242a0 74 61 74 65 6d 65 6e 74 5d 20 61 6e 64 20 72 65  tatement] and re
242b0 73 75 6c 74 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 61  sult column.** a
242c0 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20  t the same time 
242d0 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73  then the results
242e0 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a   are undefined..
242f0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
24300 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
24310 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73  3_column_databas
24320 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73  e_name(sqlite3_s
24330 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  tmt*,int);.SQLIT
24340 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
24350 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
24360 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36  _database_name16
24370 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
24380 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  nt);.SQLITE_API 
24390 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
243a0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65  te3_column_table
243b0 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74  _name(sqlite3_st
243c0 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  mt*,int);.SQLITE
243d0 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
243e0 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
243f0 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c  table_name16(sql
24400 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
24410 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
24420 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  t char *sqlite3_
24430 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61  column_origin_na
24440 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  me(sqlite3_stmt*
24450 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50  ,int);.SQLITE_AP
24460 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
24470 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69  lite3_column_ori
24480 67 69 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74  gin_name16(sqlit
24490 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a  e3_stmt*,int);..
244a0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
244b0 44 65 63 6c 61 72 65 64 20 44 61 74 61 74 79 70  Declared Datatyp
244c0 65 20 4f 66 20 41 20 51 75 65 72 79 20 52 65 73  e Of A Query Res
244d0 75 6c 74 20 7b 48 31 33 37 36 30 7d 20 3c 53 31  ult {H13760} <S1
244e0 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0700>.**.** The 
244f0 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20  first parameter 
24500 69 73 20 61 20 5b 70 72 65 70 61 72 65 64 20 73  is a [prepared s
24510 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 49 66  tatement]..** If
24520 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20   this statement 
24530 69 73 20 61 20 5b 53 45 4c 45 43 54 5d 20 73 74  is a [SELECT] st
24540 61 74 65 6d 65 6e 74 20 61 6e 64 20 74 68 65 20  atement and the 
24550 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  Nth column of th
24560 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 72 65  e.** returned re
24570 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 61 74  sult set of that
24580 20 5b 53 45 4c 45 43 54 5d 20 69 73 20 61 20 74   [SELECT] is a t
24590 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 28 6e 6f 74  able column (not
245a0 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f   an.** expressio
245b0 6e 20 6f 72 20 73 75 62 71 75 65 72 79 29 20 74  n or subquery) t
245c0 68 65 6e 20 74 68 65 20 64 65 63 6c 61 72 65 64  hen the declared
245d0 20 74 79 70 65 20 6f 66 20 74 68 65 20 74 61 62   type of the tab
245e0 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20  le.** column is 
245f0 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68  returned.  If th
24600 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20  e Nth column of 
24610 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 69  the result set i
24620 73 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69  s an.** expressi
24630 6f 6e 20 6f 72 20 73 75 62 71 75 65 72 79 2c 20  on or subquery, 
24640 74 68 65 6e 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  then a NULL poin
24650 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ter is returned.
24660 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64  .** The returned
24670 20 73 74 72 69 6e 67 20 69 73 20 61 6c 77 61 79   string is alway
24680 73 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 2e  s UTF-8 encoded.
24690 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 46 6f 72   {END}.**.** For
246a0 20 65 78 61 6d 70 6c 65 2c 20 67 69 76 65 6e 20   example, given 
246b0 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
246c0 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54  ema:.**.** CREAT
246d0 45 20 54 41 42 4c 45 20 74 31 28 63 31 20 56 41  E TABLE t1(c1 VA
246e0 52 49 41 4e 54 29 3b 0a 2a 2a 0a 2a 2a 20 61 6e  RIANT);.**.** an
246f0 64 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  d the following 
24700 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62 65 20  statement to be 
24710 63 6f 6d 70 69 6c 65 64 3a 0a 2a 2a 0a 2a 2a 20  compiled:.**.** 
24720 53 45 4c 45 43 54 20 63 31 20 2b 20 31 2c 20 63  SELECT c1 + 1, c
24730 31 20 46 52 4f 4d 20 74 31 3b 0a 2a 2a 0a 2a 2a  1 FROM t1;.**.**
24740 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 6f   this routine wo
24750 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 73  uld return the s
24760 74 72 69 6e 67 20 22 56 41 52 49 41 4e 54 22 20  tring "VARIANT" 
24770 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 72  for the second r
24780 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 20  esult.** column 
24790 28 69 3d 3d 31 29 2c 20 61 6e 64 20 61 20 4e 55  (i==1), and a NU
247a0 4c 4c 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 74  LL pointer for t
247b0 68 65 20 66 69 72 73 74 20 72 65 73 75 6c 74 20  he first result 
247c0 63 6f 6c 75 6d 6e 20 28 69 3d 3d 30 29 2e 0a 2a  column (i==0)..*
247d0 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73  *.** SQLite uses
247e0 20 64 79 6e 61 6d 69 63 20 72 75 6e 2d 74 69 6d   dynamic run-tim
247f0 65 20 74 79 70 69 6e 67 2e 20 20 53 6f 20 6a 75  e typing.  So ju
24800 73 74 20 62 65 63 61 75 73 65 20 61 20 63 6f 6c  st because a col
24810 75 6d 6e 0a 2a 2a 20 69 73 20 64 65 63 6c 61 72  umn.** is declar
24820 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20  ed to contain a 
24830 70 61 72 74 69 63 75 6c 61 72 20 74 79 70 65 20  particular type 
24840 64 6f 65 73 20 6e 6f 74 20 6d 65 61 6e 20 74 68  does not mean th
24850 61 74 20 74 68 65 0a 2a 2a 20 64 61 74 61 20 73  at the.** data s
24860 74 6f 72 65 64 20 69 6e 20 74 68 61 74 20 63 6f  tored in that co
24870 6c 75 6d 6e 20 69 73 20 6f 66 20 74 68 65 20 64  lumn is of the d
24880 65 63 6c 61 72 65 64 20 74 79 70 65 2e 20 20 53  eclared type.  S
24890 51 4c 69 74 65 20 69 73 0a 2a 2a 20 73 74 72 6f  QLite is.** stro
248a0 6e 67 6c 79 20 74 79 70 65 64 2c 20 62 75 74 20  ngly typed, but 
248b0 74 68 65 20 74 79 70 69 6e 67 20 69 73 20 64 79  the typing is dy
248c0 6e 61 6d 69 63 20 6e 6f 74 20 73 74 61 74 69 63  namic not static
248d0 2e 20 20 54 79 70 65 0a 2a 2a 20 69 73 20 61 73  .  Type.** is as
248e0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 69 6e  sociated with in
248f0 64 69 76 69 64 75 61 6c 20 76 61 6c 75 65 73 2c  dividual values,
24900 20 6e 6f 74 20 77 69 74 68 20 74 68 65 20 63 6f   not with the co
24910 6e 74 61 69 6e 65 72 73 0a 2a 2a 20 75 73 65 64  ntainers.** used
24920 20 74 6f 20 68 6f 6c 64 20 74 68 6f 73 65 20 76   to hold those v
24930 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  alues..**.** Req
24940 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
24950 31 33 37 36 31 5d 20 5b 48 31 33 37 36 32 5d 20  13761] [H13762] 
24960 5b 48 31 33 37 36 33 5d 0a 2a 2f 0a 53 51 4c 49  [H13763].*/.SQLI
24970 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61  TE_API const cha
24980 72 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  r *sqlite3_colum
24990 6e 5f 64 65 63 6c 74 79 70 65 28 73 71 6c 69 74  n_decltype(sqlit
249a0 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53  e3_stmt*,int);.S
249b0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
249c0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
249d0 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 28  lumn_decltype16(
249e0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
249f0 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  t);../*.** CAPI3
24a00 52 45 46 3a 20 45 76 61 6c 75 61 74 65 20 41 6e  REF: Evaluate An
24a10 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20 7b   SQL Statement {
24a20 48 31 33 32 30 30 7d 20 3c 53 31 30 30 30 30 3e  H13200} <S10000>
24a30 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 20 5b  .**.** After a [
24a40 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
24a50 6e 74 5d 20 68 61 73 20 62 65 65 6e 20 70 72 65  nt] has been pre
24a60 70 61 72 65 64 20 75 73 69 6e 67 20 65 69 74 68  pared using eith
24a70 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70  er.** [sqlite3_p
24a80 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20  repare_v2()] or 
24a90 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
24aa0 31 36 5f 76 32 28 29 5d 20 6f 72 20 6f 6e 65 20  16_v2()] or one 
24ab0 6f 66 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a  of the legacy.**
24ac0 20 69 6e 74 65 72 66 61 63 65 73 20 5b 73 71 6c   interfaces [sql
24ad0 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20  ite3_prepare()] 
24ae0 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  or [sqlite3_prep
24af0 61 72 65 31 36 28 29 5d 2c 20 74 68 69 73 20 66  are16()], this f
24b00 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20  unction.** must 
24b10 62 65 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72  be called one or
24b20 20 6d 6f 72 65 20 74 69 6d 65 73 20 74 6f 20 65   more times to e
24b30 76 61 6c 75 61 74 65 20 74 68 65 20 73 74 61 74  valuate the stat
24b40 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ement..**.** The
24b50 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20   details of the 
24b60 62 65 68 61 76 69 6f 72 20 6f 66 20 74 68 65 20  behavior of the 
24b70 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 69  sqlite3_step() i
24b80 6e 74 65 72 66 61 63 65 20 64 65 70 65 6e 64 0a  nterface depend.
24b90 2a 2a 20 6f 6e 20 77 68 65 74 68 65 72 20 74 68  ** on whether th
24ba0 65 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20  e statement was 
24bb0 70 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 74  prepared using t
24bc0 68 65 20 6e 65 77 65 72 20 22 76 32 22 20 69 6e  he newer "v2" in
24bd0 74 65 72 66 61 63 65 0a 2a 2a 20 5b 73 71 6c 69  terface.** [sqli
24be0 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
24bf0 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70  ] and [sqlite3_p
24c00 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20 6f  repare16_v2()] o
24c10 72 20 74 68 65 20 6f 6c 64 65 72 20 6c 65 67 61  r the older lega
24c20 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  cy.** interface 
24c30 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
24c40 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
24c50 5f 70 72 65 70 61 72 65 31 36 28 29 5d 2e 20 20  _prepare16()].  
24c60 54 68 65 20 75 73 65 20 6f 66 20 74 68 65 0a 2a  The use of the.*
24c70 2a 20 6e 65 77 20 22 76 32 22 20 69 6e 74 65 72  * new "v2" inter
24c80 66 61 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e  face is recommen
24c90 64 65 64 20 66 6f 72 20 6e 65 77 20 61 70 70 6c  ded for new appl
24ca0 69 63 61 74 69 6f 6e 73 20 62 75 74 20 74 68 65  ications but the
24cb0 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72   legacy.** inter
24cc0 66 61 63 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e  face will contin
24cd0 75 65 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74  ue to be support
24ce0 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65  ed..**.** In the
24cf0 20 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61 63   legacy interfac
24d00 65 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  e, the return va
24d10 6c 75 65 20 77 69 6c 6c 20 62 65 20 65 69 74 68  lue will be eith
24d20 65 72 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d  er [SQLITE_BUSY]
24d30 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e  ,.** [SQLITE_DON
24d40 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d  E], [SQLITE_ROW]
24d50 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d  , [SQLITE_ERROR]
24d60 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d 49 53  , or [SQLITE_MIS
24d70 55 53 45 5d 2e 0a 2a 2a 20 57 69 74 68 20 74 68  USE]..** With th
24d80 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65  e "v2" interface
24d90 2c 20 61 6e 79 20 6f 66 20 74 68 65 20 6f 74 68  , any of the oth
24da0 65 72 20 5b 72 65 73 75 6c 74 20 63 6f 64 65 73  er [result codes
24db0 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e 64 65  ] or.** [extende
24dc0 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20  d result codes] 
24dd0 6d 69 67 68 74 20 62 65 20 72 65 74 75 72 6e 65  might be returne
24de0 64 20 61 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a  d as well..**.**
24df0 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6d   [SQLITE_BUSY] m
24e00 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 64 61  eans that the da
24e10 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20 77 61  tabase engine wa
24e20 73 20 75 6e 61 62 6c 65 20 74 6f 20 61 63 71 75  s unable to acqu
24e30 69 72 65 20 74 68 65 0a 2a 2a 20 64 61 74 61 62  ire the.** datab
24e40 61 73 65 20 6c 6f 63 6b 73 20 69 74 20 6e 65 65  ase locks it nee
24e50 64 73 20 74 6f 20 64 6f 20 69 74 73 20 6a 6f 62  ds to do its job
24e60 2e 20 20 49 66 20 74 68 65 20 73 74 61 74 65 6d  .  If the statem
24e70 65 6e 74 20 69 73 20 61 20 5b 43 4f 4d 4d 49 54  ent is a [COMMIT
24e80 5d 0a 2a 2a 20 6f 72 20 6f 63 63 75 72 73 20 6f  ].** or occurs o
24e90 75 74 73 69 64 65 20 6f 66 20 61 6e 20 65 78 70  utside of an exp
24ea0 6c 69 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f  licit transactio
24eb0 6e 2c 20 74 68 65 6e 20 79 6f 75 20 63 61 6e 20  n, then you can 
24ec0 72 65 74 72 79 20 74 68 65 0a 2a 2a 20 73 74 61  retry the.** sta
24ed0 74 65 6d 65 6e 74 2e 20 20 49 66 20 74 68 65 20  tement.  If the 
24ee0 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f 74  statement is not
24ef0 20 61 20 5b 43 4f 4d 4d 49 54 5d 20 61 6e 64 20   a [COMMIT] and 
24f00 6f 63 63 75 72 73 20 77 69 74 68 69 6e 20 61 0a  occurs within a.
24f10 2a 2a 20 65 78 70 6c 69 63 69 74 20 74 72 61 6e  ** explicit tran
24f20 73 61 63 74 69 6f 6e 20 74 68 65 6e 20 79 6f 75  saction then you
24f30 20 73 68 6f 75 6c 64 20 72 6f 6c 6c 62 61 63 6b   should rollback
24f40 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
24f50 20 62 65 66 6f 72 65 0a 2a 2a 20 63 6f 6e 74 69   before.** conti
24f60 6e 75 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51  nuing..**.** [SQ
24f70 4c 49 54 45 5f 44 4f 4e 45 5d 20 6d 65 61 6e 73  LITE_DONE] means
24f80 20 74 68 61 74 20 74 68 65 20 73 74 61 74 65 6d   that the statem
24f90 65 6e 74 20 68 61 73 20 66 69 6e 69 73 68 65 64  ent has finished
24fa0 20 65 78 65 63 75 74 69 6e 67 0a 2a 2a 20 73 75   executing.** su
24fb0 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 73 71 6c  ccessfully.  sql
24fc0 69 74 65 33 5f 73 74 65 70 28 29 20 73 68 6f 75  ite3_step() shou
24fd0 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64  ld not be called
24fe0 20 61 67 61 69 6e 20 6f 6e 20 74 68 69 73 20 76   again on this v
24ff0 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e  irtual.** machin
25000 65 20 77 69 74 68 6f 75 74 20 66 69 72 73 74 20  e without first 
25010 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33  calling [sqlite3
25020 5f 72 65 73 65 74 28 29 5d 20 74 6f 20 72 65 73  _reset()] to res
25030 65 74 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a  et the virtual.*
25040 2a 20 6d 61 63 68 69 6e 65 20 62 61 63 6b 20 74  * machine back t
25050 6f 20 69 74 73 20 69 6e 69 74 69 61 6c 20 73 74  o its initial st
25060 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ate..**.** If th
25070 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
25080 62 65 69 6e 67 20 65 78 65 63 75 74 65 64 20 72  being executed r
25090 65 74 75 72 6e 73 20 61 6e 79 20 64 61 74 61 2c  eturns any data,
250a0 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 52 4f   then [SQLITE_RO
250b0 57 5d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65  W].** is returne
250c0 64 20 65 61 63 68 20 74 69 6d 65 20 61 20 6e 65  d each time a ne
250d0 77 20 72 6f 77 20 6f 66 20 64 61 74 61 20 69 73  w row of data is
250e0 20 72 65 61 64 79 20 66 6f 72 20 70 72 6f 63 65   ready for proce
250f0 73 73 69 6e 67 20 62 79 20 74 68 65 0a 2a 2a 20  ssing by the.** 
25100 63 61 6c 6c 65 72 2e 20 54 68 65 20 76 61 6c 75  caller. The valu
25110 65 73 20 6d 61 79 20 62 65 20 61 63 63 65 73 73  es may be access
25120 65 64 20 75 73 69 6e 67 20 74 68 65 20 5b 63 6f  ed using the [co
25130 6c 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e 63  lumn access func
25140 74 69 6f 6e 73 5d 2e 0a 2a 2a 20 73 71 6c 69 74  tions]..** sqlit
25150 65 33 5f 73 74 65 70 28 29 20 69 73 20 63 61 6c  e3_step() is cal
25160 6c 65 64 20 61 67 61 69 6e 20 74 6f 20 72 65 74  led again to ret
25170 72 69 65 76 65 20 74 68 65 20 6e 65 78 74 20 72  rieve the next r
25180 6f 77 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a  ow of data..**.*
25190 2a 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d  * [SQLITE_ERROR]
251a0 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 72 75   means that a ru
251b0 6e 2d 74 69 6d 65 20 65 72 72 6f 72 20 28 73 75  n-time error (su
251c0 63 68 20 61 73 20 61 20 63 6f 6e 73 74 72 61 69  ch as a constrai
251d0 6e 74 0a 2a 2a 20 76 69 6f 6c 61 74 69 6f 6e 29  nt.** violation)
251e0 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20 20   has occurred.  
251f0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 73  sqlite3_step() s
25200 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c  hould not be cal
25210 6c 65 64 20 61 67 61 69 6e 20 6f 6e 0a 2a 2a 20  led again on.** 
25220 74 68 65 20 56 4d 2e 20 4d 6f 72 65 20 69 6e 66  the VM. More inf
25230 6f 72 6d 61 74 69 6f 6e 20 6d 61 79 20 62 65 20  ormation may be 
25240 66 6f 75 6e 64 20 62 79 20 63 61 6c 6c 69 6e 67  found by calling
25250 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67   [sqlite3_errmsg
25260 28 29 5d 2e 0a 2a 2a 20 57 69 74 68 20 74 68 65  ()]..** With the
25270 20 6c 65 67 61 63 79 20 69 6e 74 65 72 66 61 63   legacy interfac
25280 65 2c 20 61 20 6d 6f 72 65 20 73 70 65 63 69 66  e, a more specif
25290 69 63 20 65 72 72 6f 72 20 63 6f 64 65 20 28 66  ic error code (f
252a0 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 5b  or example,.** [
252b0 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54  SQLITE_INTERRUPT
252c0 5d 2c 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d  ], [SQLITE_SCHEM
252d0 41 5d 2c 20 5b 53 51 4c 49 54 45 5f 43 4f 52 52  A], [SQLITE_CORR
252e0 55 50 54 5d 2c 20 61 6e 64 20 73 6f 20 66 6f 72  UPT], and so for
252f0 74 68 29 0a 2a 2a 20 63 61 6e 20 62 65 20 6f 62  th).** can be ob
25300 74 61 69 6e 65 64 20 62 79 20 63 61 6c 6c 69 6e  tained by callin
25310 67 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74  g [sqlite3_reset
25320 28 29 5d 20 6f 6e 20 74 68 65 0a 2a 2a 20 5b 70  ()] on the.** [p
25330 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
25340 74 5d 2e 20 20 49 6e 20 74 68 65 20 22 76 32 22  t].  In the "v2"
25350 20 69 6e 74 65 72 66 61 63 65 2c 0a 2a 2a 20 74   interface,.** t
25360 68 65 20 6d 6f 72 65 20 73 70 65 63 69 66 69 63  he more specific
25370 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
25380 65 74 75 72 6e 65 64 20 64 69 72 65 63 74 6c 79  eturned directly
25390 20 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65 70   by sqlite3_step
253a0 28 29 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54  ()..**.** [SQLIT
253b0 45 5f 4d 49 53 55 53 45 5d 20 6d 65 61 6e 73 20  E_MISUSE] means 
253c0 74 68 61 74 20 74 68 65 20 74 68 69 73 20 72 6f  that the this ro
253d0 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64  utine was called
253e0 20 69 6e 61 70 70 72 6f 70 72 69 61 74 65 6c 79   inappropriately
253f0 2e 0a 2a 2a 20 50 65 72 68 61 70 73 20 69 74 20  ..** Perhaps it 
25400 77 61 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20  was called on a 
25410 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
25420 65 6e 74 5d 20 74 68 61 74 20 68 61 73 0a 2a 2a  ent] that has.**
25430 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 5b 73   already been [s
25440 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20  qlite3_finalize 
25450 7c 20 66 69 6e 61 6c 69 7a 65 64 5d 20 6f 72 20  | finalized] or 
25460 6f 6e 20 6f 6e 65 20 74 68 61 74 20 68 61 64 0a  on one that had.
25470 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65  ** previously re
25480 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 45  turned [SQLITE_E
25490 52 52 4f 52 5d 20 6f 72 20 5b 53 51 4c 49 54 45  RROR] or [SQLITE
254a0 5f 44 4f 4e 45 5d 2e 20 20 4f 72 20 69 74 20 63  _DONE].  Or it c
254b0 6f 75 6c 64 0a 2a 2a 20 62 65 20 74 68 65 20 63  ould.** be the c
254c0 61 73 65 20 74 68 61 74 20 74 68 65 20 73 61 6d  ase that the sam
254d0 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
254e0 63 74 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 75  ction is being u
254f0 73 65 64 20 62 79 20 74 77 6f 20 6f 72 0a 2a 2a  sed by two or.**
25500 20 6d 6f 72 65 20 74 68 72 65 61 64 73 20 61 74   more threads at
25510 20 74 68 65 20 73 61 6d 65 20 6d 6f 6d 65 6e 74   the same moment
25520 20 69 6e 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20   in time..**.** 
25530 3c 62 3e 47 6f 6f 66 79 20 49 6e 74 65 72 66 61  <b>Goofy Interfa
25540 63 65 20 41 6c 65 72 74 3a 3c 2f 62 3e 20 49 6e  ce Alert:</b> In
25550 20 74 68 65 20 6c 65 67 61 63 79 20 69 6e 74 65   the legacy inte
25560 72 66 61 63 65 2c 20 74 68 65 20 73 71 6c 69 74  rface, the sqlit
25570 65 33 5f 73 74 65 70 28 29 0a 2a 2a 20 41 50 49  e3_step().** API
25580 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20   always returns 
25590 61 20 67 65 6e 65 72 69 63 20 65 72 72 6f 72 20  a generic error 
255a0 63 6f 64 65 2c 20 5b 53 51 4c 49 54 45 5f 45 52  code, [SQLITE_ER
255b0 52 4f 52 5d 2c 20 66 6f 6c 6c 6f 77 69 6e 67 20  ROR], following 
255c0 61 6e 79 0a 2a 2a 20 65 72 72 6f 72 20 6f 74 68  any.** error oth
255d0 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f  er than [SQLITE_
255e0 42 55 53 59 5d 20 61 6e 64 20 5b 53 51 4c 49 54  BUSY] and [SQLIT
255f0 45 5f 4d 49 53 55 53 45 5d 2e 20 20 59 6f 75 20  E_MISUSE].  You 
25600 6d 75 73 74 20 63 61 6c 6c 0a 2a 2a 20 5b 73 71  must call.** [sq
25610 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f  lite3_reset()] o
25620 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  r [sqlite3_final
25630 69 7a 65 28 29 5d 20 69 6e 20 6f 72 64 65 72 20  ize()] in order 
25640 74 6f 20 66 69 6e 64 20 6f 6e 65 20 6f 66 20 74  to find one of t
25650 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20 5b  he.** specific [
25660 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 74 68 61  error codes] tha
25670 74 20 62 65 74 74 65 72 20 64 65 73 63 72 69 62  t better describ
25680 65 73 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a 2a  es the error..**
25690 20 57 65 20 61 64 6d 69 74 20 74 68 61 74 20 74   We admit that t
256a0 68 69 73 20 69 73 20 61 20 67 6f 6f 66 79 20 64  his is a goofy d
256b0 65 73 69 67 6e 2e 20 20 54 68 65 20 70 72 6f 62  esign.  The prob
256c0 6c 65 6d 20 68 61 73 20 62 65 65 6e 20 66 69 78  lem has been fix
256d0 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 22  ed.** with the "
256e0 76 32 22 20 69 6e 74 65 72 66 61 63 65 2e 20 20  v2" interface.  
256f0 49 66 20 79 6f 75 20 70 72 65 70 61 72 65 20 61  If you prepare a
25700 6c 6c 20 6f 66 20 79 6f 75 72 20 53 51 4c 20 73  ll of your SQL s
25710 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 75 73 69  tatements.** usi
25720 6e 67 20 65 69 74 68 65 72 20 5b 73 71 6c 69 74  ng either [sqlit
25730 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
25740 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 70 72 65   or [sqlite3_pre
25750 70 61 72 65 31 36 5f 76 32 28 29 5d 20 69 6e 73  pare16_v2()] ins
25760 74 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65 20 6c  tead.** of the l
25770 65 67 61 63 79 20 5b 73 71 6c 69 74 65 33 5f 70  egacy [sqlite3_p
25780 72 65 70 61 72 65 28 29 5d 20 61 6e 64 20 5b 73  repare()] and [s
25790 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
257a0 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 2c 0a  ()] interfaces,.
257b0 2a 2a 20 74 68 65 6e 20 74 68 65 20 6d 6f 72 65  ** then the more
257c0 20 73 70 65 63 69 66 69 63 20 5b 65 72 72 6f 72   specific [error
257d0 20 63 6f 64 65 73 5d 20 61 72 65 20 72 65 74 75   codes] are retu
257e0 72 6e 65 64 20 64 69 72 65 63 74 6c 79 0a 2a 2a  rned directly.**
257f0 20 62 79 20 73 71 6c 69 74 65 33 5f 73 74 65 70   by sqlite3_step
25800 28 29 2e 20 20 54 68 65 20 75 73 65 20 6f 66 20  ().  The use of 
25810 74 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61  the "v2" interfa
25820 63 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e 64 65  ce is recommende
25830 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  d..**.** Require
25840 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 32 30  ments:.** [H1320
25850 32 5d 20 5b 48 31 35 33 30 34 5d 20 5b 48 31 35  2] [H15304] [H15
25860 33 30 36 5d 20 5b 48 31 35 33 30 38 5d 20 5b 48  306] [H15308] [H
25870 31 35 33 31 30 5d 0a 2a 2f 0a 53 51 4c 49 54 45  15310].*/.SQLITE
25880 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
25890 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74  _step(sqlite3_st
258a0 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  mt*);../*.** CAP
258b0 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 6f 66  I3REF: Number of
258c0 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 72 65   columns in a re
258d0 73 75 6c 74 20 73 65 74 20 7b 48 31 33 37 37 30  sult set {H13770
258e0 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a  } <S10700>.**.**
258f0 20 52 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   Returns the num
25900 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 69 6e  ber of values in
25910 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77   the current row
25920 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73   of the result s
25930 65 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  et..**.** Requir
25940 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 33 37  ements:.** [H137
25950 37 31 5d 20 5b 48 31 33 37 37 32 5d 0a 2a 2f 0a  71] [H13772].*/.
25960 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
25970 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75 6e  qlite3_data_coun
25980 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  t(sqlite3_stmt *
25990 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  pStmt);../*.** C
259a0 41 50 49 33 52 45 46 3a 20 46 75 6e 64 61 6d 65  API3REF: Fundame
259b0 6e 74 61 6c 20 44 61 74 61 74 79 70 65 73 20 7b  ntal Datatypes {
259c0 48 31 30 32 36 35 7d 20 3c 53 31 30 31 31 30 3e  H10265} <S10110>
259d0 3c 53 31 30 31 32 30 3e 0a 2a 2a 20 4b 45 59 57  <S10120>.** KEYW
259e0 4f 52 44 53 3a 20 53 51 4c 49 54 45 5f 54 45 58  ORDS: SQLITE_TEX
259f0 54 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 36 36 7d  T.**.** {H10266}
25a00 20 45 76 65 72 79 20 76 61 6c 75 65 20 69 6e 20   Every value in 
25a10 53 51 4c 69 74 65 20 68 61 73 20 6f 6e 65 20 6f  SQLite has one o
25a20 66 20 66 69 76 65 20 66 75 6e 64 61 6d 65 6e 74  f five fundament
25a30 61 6c 20 64 61 74 61 74 79 70 65 73 3a 0a 2a 2a  al datatypes:.**
25a40 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ul>.** <li>
25a50 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69   64-bit signed i
25a60 6e 74 65 67 65 72 0a 2a 2a 20 3c 6c 69 3e 20 36  nteger.** <li> 6
25a70 34 2d 62 69 74 20 49 45 45 45 20 66 6c 6f 61 74  4-bit IEEE float
25a80 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72  ing point number
25a90 0a 2a 2a 20 3c 6c 69 3e 20 73 74 72 69 6e 67 0a  .** <li> string.
25aa0 2a 2a 20 3c 6c 69 3e 20 42 4c 4f 42 0a 2a 2a 20  ** <li> BLOB.** 
25ab0 3c 6c 69 3e 20 4e 55 4c 4c 0a 2a 2a 20 3c 2f 75  <li> NULL.** </u
25ac0 6c 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54  l> {END}.**.** T
25ad0 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73 20 61  hese constants a
25ae0 72 65 20 63 6f 64 65 73 20 66 6f 72 20 65 61 63  re codes for eac
25af0 68 20 6f 66 20 74 68 6f 73 65 20 74 79 70 65 73  h of those types
25b00 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
25b10 74 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 58  t the SQLITE_TEX
25b20 54 20 63 6f 6e 73 74 61 6e 74 20 77 61 73 20 61  T constant was a
25b30 6c 73 6f 20 75 73 65 64 20 69 6e 20 53 51 4c 69  lso used in SQLi
25b40 74 65 20 76 65 72 73 69 6f 6e 20 32 0a 2a 2a 20  te version 2.** 
25b50 66 6f 72 20 61 20 63 6f 6d 70 6c 65 74 65 6c 79  for a completely
25b60 20 64 69 66 66 65 72 65 6e 74 20 6d 65 61 6e 69   different meani
25b70 6e 67 2e 20 20 53 6f 66 74 77 61 72 65 20 74 68  ng.  Software th
25b80 61 74 20 6c 69 6e 6b 73 20 61 67 61 69 6e 73 74  at links against
25b90 20 62 6f 74 68 0a 2a 2a 20 53 51 4c 69 74 65 20   both.** SQLite 
25ba0 76 65 72 73 69 6f 6e 20 32 20 61 6e 64 20 53 51  version 2 and SQ
25bb0 4c 69 74 65 20 76 65 72 73 69 6f 6e 20 33 20 73  Lite version 3 s
25bc0 68 6f 75 6c 64 20 75 73 65 20 53 51 4c 49 54 45  hould use SQLITE
25bd0 33 5f 54 45 58 54 2c 20 6e 6f 74 0a 2a 2a 20 53  3_TEXT, not.** S
25be0 51 4c 49 54 45 5f 54 45 58 54 2e 0a 2a 2f 0a 23  QLITE_TEXT..*/.#
25bf0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e  define SQLITE_IN
25c00 54 45 47 45 52 20 20 31 0a 23 64 65 66 69 6e 65  TEGER  1.#define
25c10 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 20 20   SQLITE_FLOAT   
25c20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   2.#define SQLIT
25c30 45 5f 42 4c 4f 42 20 20 20 20 20 34 0a 23 64 65  E_BLOB     4.#de
25c40 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  fine SQLITE_NULL
25c50 20 20 20 20 20 35 0a 23 69 66 64 65 66 20 53 51       5.#ifdef SQ
25c60 4c 49 54 45 5f 54 45 58 54 0a 23 20 75 6e 64 65  LITE_TEXT.# unde
25c70 66 20 53 51 4c 49 54 45 5f 54 45 58 54 0a 23 65  f SQLITE_TEXT.#e
25c80 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  lse.# define SQL
25c90 49 54 45 5f 54 45 58 54 20 20 20 20 20 33 0a 23  ITE_TEXT     3.#
25ca0 65 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 53 51  endif.#define SQ
25cb0 4c 49 54 45 33 5f 54 45 58 54 20 20 20 20 20 33  LITE3_TEXT     3
25cc0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
25cd0 3a 20 52 65 73 75 6c 74 20 56 61 6c 75 65 73 20  : Result Values 
25ce0 46 72 6f 6d 20 41 20 51 75 65 72 79 20 7b 48 31  From A Query {H1
25cf0 33 38 30 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a  3800} <S10700>.*
25d00 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 63 6f 6c  * KEYWORDS: {col
25d10 75 6d 6e 20 61 63 63 65 73 73 20 66 75 6e 63 74  umn access funct
25d20 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73  ions}.**.** Thes
25d30 65 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 6d 20  e routines form 
25d40 74 68 65 20 22 72 65 73 75 6c 74 20 73 65 74 20  the "result set 
25d50 71 75 65 72 79 22 20 69 6e 74 65 72 66 61 63 65  query" interface
25d60 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f  ..**.** These ro
25d70 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 69 6e  utines return in
25d80 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
25d90 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20  a single column 
25da0 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a  of the current.*
25db0 2a 20 72 65 73 75 6c 74 20 72 6f 77 20 6f 66 20  * result row of 
25dc0 61 20 71 75 65 72 79 2e 20 20 49 6e 20 65 76 65  a query.  In eve
25dd0 72 79 20 63 61 73 65 20 74 68 65 20 66 69 72 73  ry case the firs
25de0 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20  t argument is a 
25df0 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68  pointer.** to th
25e00 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  e [prepared stat
25e10 65 6d 65 6e 74 5d 20 74 68 61 74 20 69 73 20 62  ement] that is b
25e20 65 69 6e 67 20 65 76 61 6c 75 61 74 65 64 20 28  eing evaluated (
25e30 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d  the [sqlite3_stm
25e40 74 2a 5d 0a 2a 2a 20 74 68 61 74 20 77 61 73 20  t*].** that was 
25e50 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 5b 73  returned from [s
25e60 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
25e70 32 28 29 5d 20 6f 72 20 6f 6e 65 20 6f 66 20 69  2()] or one of i
25e80 74 73 20 76 61 72 69 61 6e 74 73 29 0a 2a 2a 20  ts variants).** 
25e90 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64 20 61  and the second a
25ea0 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 69  rgument is the i
25eb0 6e 64 65 78 20 6f 66 20 74 68 65 20 63 6f 6c 75  ndex of the colu
25ec0 6d 6e 20 66 6f 72 20 77 68 69 63 68 20 69 6e 66  mn for which inf
25ed0 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 68 6f 75  ormation.** shou
25ee0 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20  ld be returned. 
25ef0 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 20 63 6f   The leftmost co
25f00 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73 75  lumn of the resu
25f10 6c 74 20 73 65 74 20 68 61 73 20 74 68 65 20 69  lt set has the i
25f20 6e 64 65 78 20 30 2e 0a 2a 2a 20 54 68 65 20 6e  ndex 0..** The n
25f30 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
25f40 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 63   in the result c
25f50 61 6e 20 62 65 20 64 65 74 65 72 6d 69 6e 65 64  an be determined
25f60 20 75 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74   using.** [sqlit
25f70 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28  e3_column_count(
25f80 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  )]..**.** If the
25f90 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 64   SQL statement d
25fa0 6f 65 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c  oes not currentl
25fb0 79 20 70 6f 69 6e 74 20 74 6f 20 61 20 76 61 6c  y point to a val
25fc0 69 64 20 72 6f 77 2c 20 6f 72 20 69 66 20 74 68  id row, or if th
25fd0 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 64 65  e.** column inde
25fe0 78 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67  x is out of rang
25ff0 65 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  e, the result is
26000 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 54   undefined..** T
26010 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d 61  hese routines ma
26020 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64  y only be called
26030 20 77 68 65 6e 20 74 68 65 20 6d 6f 73 74 20 72   when the most r
26040 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a  ecent call to.**
26050 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   [sqlite3_step()
26060 5d 20 68 61 73 20 72 65 74 75 72 6e 65 64 20 5b  ] has returned [
26070 53 51 4c 49 54 45 5f 52 4f 57 5d 20 61 6e 64 20  SQLITE_ROW] and 
26080 6e 65 69 74 68 65 72 0a 2a 2a 20 5b 73 71 6c 69  neither.** [sqli
26090 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6e 6f 72  te3_reset()] nor
260a0 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
260b0 7a 65 28 29 5d 20 68 61 76 65 20 62 65 65 6e 20  ze()] have been 
260c0 63 61 6c 6c 65 64 20 73 75 62 73 65 71 75 65 6e  called subsequen
260d0 74 6c 79 2e 0a 2a 2a 20 49 66 20 61 6e 79 20 6f  tly..** If any o
260e0 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  f these routines
260f0 20 61 72 65 20 63 61 6c 6c 65 64 20 61 66 74 65   are called afte
26100 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74  r [sqlite3_reset
26110 28 29 5d 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74  ()] or.** [sqlit
26120 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f  e3_finalize()] o
26130 72 20 61 66 74 65 72 20 5b 73 71 6c 69 74 65 33  r after [sqlite3
26140 5f 73 74 65 70 28 29 5d 20 68 61 73 20 72 65 74  _step()] has ret
26150 75 72 6e 65 64 0a 2a 2a 20 73 6f 6d 65 74 68 69  urned.** somethi
26160 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53  ng other than [S
26170 51 4c 49 54 45 5f 52 4f 57 5d 2c 20 74 68 65 20  QLITE_ROW], the 
26180 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e 64 65  results are unde
26190 66 69 6e 65 64 2e 0a 2a 2a 20 49 66 20 5b 73 71  fined..** If [sq
261a0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72  lite3_step()] or
261b0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
261c0 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66  )] or [sqlite3_f
261d0 69 6e 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 61 72  inalize()].** ar
261e0 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 61 20  e called from a 
261f0 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64  different thread
26200 20 77 68 69 6c 65 20 61 6e 79 20 6f 66 20 74 68   while any of th
26210 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20  ese routines.** 
26220 61 72 65 20 70 65 6e 64 69 6e 67 2c 20 74 68 65  are pending, the
26230 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 72  n the results ar
26240 65 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a  e undefined..**.
26250 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63  ** The sqlite3_c
26260 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 72 6f 75  olumn_type() rou
26270 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65  tine returns the
26280 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45  .** [SQLITE_INTE
26290 47 45 52 20 7c 20 64 61 74 61 74 79 70 65 20 63  GER | datatype c
262a0 6f 64 65 5d 20 66 6f 72 20 74 68 65 20 69 6e 69  ode] for the ini
262b0 74 69 61 6c 20 64 61 74 61 20 74 79 70 65 0a 2a  tial data type.*
262c0 2a 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  * of the result 
262d0 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20 72 65 74  column.  The ret
262e0 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 6f  urned value is o
262f0 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e  ne of [SQLITE_IN
26300 54 45 47 45 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49  TEGER],.** [SQLI
26310 54 45 5f 46 4c 4f 41 54 5d 2c 20 5b 53 51 4c 49  TE_FLOAT], [SQLI
26320 54 45 5f 54 45 58 54 5d 2c 20 5b 53 51 4c 49 54  TE_TEXT], [SQLIT
26330 45 5f 42 4c 4f 42 5d 2c 20 6f 72 20 5b 53 51 4c  E_BLOB], or [SQL
26340 49 54 45 5f 4e 55 4c 4c 5d 2e 20 20 54 68 65 20  ITE_NULL].  The 
26350 76 61 6c 75 65 0a 2a 2a 20 72 65 74 75 72 6e 65  value.** returne
26360 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c  d by sqlite3_col
26370 75 6d 6e 5f 74 79 70 65 28 29 20 69 73 20 6f 6e  umn_type() is on
26380 6c 79 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66  ly meaningful if
26390 20 6e 6f 20 74 79 70 65 0a 2a 2a 20 63 6f 6e 76   no type.** conv
263a0 65 72 73 69 6f 6e 73 20 68 61 76 65 20 6f 63 63  ersions have occ
263b0 75 72 72 65 64 20 61 73 20 64 65 73 63 72 69 62  urred as describ
263c0 65 64 20 62 65 6c 6f 77 2e 20 20 41 66 74 65 72  ed below.  After
263d0 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73 69   a type conversi
263e0 6f 6e 2c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65  on,.** the value
263f0 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c   returned by sql
26400 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65  ite3_column_type
26410 28 29 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e  () is undefined.
26420 20 20 46 75 74 75 72 65 0a 2a 2a 20 76 65 72 73    Future.** vers
26430 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 6d  ions of SQLite m
26440 61 79 20 63 68 61 6e 67 65 20 74 68 65 20 62 65  ay change the be
26450 68 61 76 69 6f 72 20 6f 66 20 73 71 6c 69 74 65  havior of sqlite
26460 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 0a  3_column_type().
26470 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 74  ** following a t
26480 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a  ype conversion..
26490 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73  **.** If the res
264a0 75 6c 74 20 69 73 20 61 20 42 4c 4f 42 20 6f 72  ult is a BLOB or
264b0 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74 68   UTF-8 string th
264c0 65 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  en the sqlite3_c
264d0 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a  olumn_bytes().**
264e0 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
264f0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
26500 79 74 65 73 20 69 6e 20 74 68 61 74 20 42 4c 4f  ytes in that BLO
26510 42 20 6f 72 20 73 74 72 69 6e 67 2e 0a 2a 2a 20  B or string..** 
26520 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  If the result is
26530 20 61 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67   a UTF-16 string
26540 2c 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 63  , then sqlite3_c
26550 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20 63 6f  olumn_bytes() co
26560 6e 76 65 72 74 73 0a 2a 2a 20 74 68 65 20 73 74  nverts.** the st
26570 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 20 61 6e  ring to UTF-8 an
26580 64 20 74 68 65 6e 20 72 65 74 75 72 6e 73 20 74  d then returns t
26590 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
265a0 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 72 65  es..** If the re
265b0 73 75 6c 74 20 69 73 20 61 20 6e 75 6d 65 72 69  sult is a numeri
265c0 63 20 76 61 6c 75 65 20 74 68 65 6e 20 73 71 6c  c value then sql
265d0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
265e0 73 28 29 20 75 73 65 73 0a 2a 2a 20 5b 73 71 6c  s() uses.** [sql
265f0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 5d  ite3_snprintf()]
26600 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 61 74   to convert that
26610 20 76 61 6c 75 65 20 74 6f 20 61 20 55 54 46 2d   value to a UTF-
26620 38 20 73 74 72 69 6e 67 20 61 6e 64 20 72 65 74  8 string and ret
26630 75 72 6e 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62  urns.** the numb
26640 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
26650 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 54  hat string..** T
26660 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
26670 64 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75  d does not inclu
26680 64 65 20 74 68 65 20 7a 65 72 6f 20 74 65 72 6d  de the zero term
26690 69 6e 61 74 6f 72 20 61 74 20 74 68 65 20 65 6e  inator at the en
266a0 64 0a 2a 2a 20 6f 66 20 74 68 65 20 73 74 72 69  d.** of the stri
266b0 6e 67 2e 20 20 46 6f 72 20 63 6c 61 72 69 74 79  ng.  For clarity
266c0 3a 20 74 68 65 20 76 61 6c 75 65 20 72 65 74 75  : the value retu
266d0 72 6e 65 64 20 69 73 20 74 68 65 20 6e 75 6d 62  rned is the numb
266e0 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73 20 69  er of.** bytes i
266f0 6e 20 74 68 65 20 73 74 72 69 6e 67 2c 20 6e 6f  n the string, no
26700 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
26710 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a  characters..**.*
26720 2a 20 53 74 72 69 6e 67 73 20 72 65 74 75 72 6e  * Strings return
26730 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f  ed by sqlite3_co
26740 6c 75 6d 6e 5f 74 65 78 74 28 29 20 61 6e 64 20  lumn_text() and 
26750 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
26760 65 78 74 31 36 28 29 2c 0a 2a 2a 20 65 76 65 6e  ext16(),.** even
26770 20 65 6d 70 74 79 20 73 74 72 69 6e 67 73 2c 20   empty strings, 
26780 61 72 65 20 61 6c 77 61 79 73 20 7a 65 72 6f 20  are always zero 
26790 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 54 68 65  terminated.  The
267a0 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65   return.** value
267b0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 6f   from sqlite3_co
267c0 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 66 6f 72 20  lumn_blob() for 
267d0 61 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 20 42 4c  a zero-length BL
267e0 4f 42 20 69 73 20 61 6e 20 61 72 62 69 74 72 61  OB is an arbitra
267f0 72 79 0a 2a 2a 20 70 6f 69 6e 74 65 72 2c 20 70  ry.** pointer, p
26800 6f 73 73 69 62 6c 79 20 65 76 65 6e 20 61 20 4e  ossibly even a N
26810 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a  ULL pointer..**.
26820 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63  ** The sqlite3_c
26830 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 20  olumn_bytes16() 
26840 72 6f 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c  routine is simil
26850 61 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f  ar to sqlite3_co
26860 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20  lumn_bytes().** 
26870 62 75 74 20 6c 65 61 76 65 73 20 74 68 65 20 72  but leaves the r
26880 65 73 75 6c 74 20 69 6e 20 55 54 46 2d 31 36 20  esult in UTF-16 
26890 69 6e 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f  in native byte o
268a0 72 64 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20  rder instead of 
268b0 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 7a 65  UTF-8..** The ze
268c0 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 73  ro terminator is
268d0 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20 69 6e   not included in
268e0 20 74 68 69 73 20 63 6f 75 6e 74 2e 0a 2a 2a 0a   this count..**.
268f0 2a 2a 20 54 68 65 20 6f 62 6a 65 63 74 20 72 65  ** The object re
26900 74 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74  turned by [sqlit
26910 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28  e3_column_value(
26920 29 5d 20 69 73 20 61 6e 0a 2a 2a 20 5b 75 6e 70  )] is an.** [unp
26930 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33  rotected sqlite3
26940 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 2e 20  _value] object. 
26950 20 41 6e 20 75 6e 70 72 6f 74 65 63 74 65 64 20   An unprotected 
26960 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
26970 6a 65 63 74 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79  ject.** may only
26980 20 62 65 20 75 73 65 64 20 77 69 74 68 20 5b 73   be used with [s
26990 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75  qlite3_bind_valu
269a0 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  e()] and [sqlite
269b0 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29  3_result_value()
269c0 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 5b 75 6e  ]..** If the [un
269d0 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65  protected sqlite
269e0 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20  3_value] object 
269f0 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 5b  returned by.** [
26a00 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76  sqlite3_column_v
26a10 61 6c 75 65 28 29 5d 20 69 73 20 75 73 65 64 20  alue()] is used 
26a20 69 6e 20 61 6e 79 20 6f 74 68 65 72 20 77 61 79  in any other way
26a30 2c 20 69 6e 63 6c 75 64 69 6e 67 20 63 61 6c 6c  , including call
26a40 73 0a 2a 2a 20 74 6f 20 72 6f 75 74 69 6e 65 73  s.** to routines
26a50 20 6c 69 6b 65 20 5b 73 71 6c 69 74 65 33 5f 76   like [sqlite3_v
26a60 61 6c 75 65 5f 69 6e 74 28 29 5d 2c 20 5b 73 71  alue_int()], [sq
26a70 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
26a80 28 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69  ()],.** or [sqli
26a90 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
26aa0 29 5d 2c 20 74 68 65 6e 20 74 68 65 20 62 65 68  )], then the beh
26ab0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
26ac0 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ed..**.** These 
26ad0 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74  routines attempt
26ae0 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65 20   to convert the 
26af0 76 61 6c 75 65 20 77 68 65 72 65 20 61 70 70 72  value where appr
26b00 6f 70 72 69 61 74 65 2e 20 20 46 6f 72 0a 2a 2a  opriate.  For.**
26b10 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65   example, if the
26b20 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73   internal repres
26b30 65 6e 74 61 74 69 6f 6e 20 69 73 20 46 4c 4f 41  entation is FLOA
26b40 54 20 61 6e 64 20 61 20 74 65 78 74 20 72 65 73  T and a text res
26b50 75 6c 74 0a 2a 2a 20 69 73 20 72 65 71 75 65 73  ult.** is reques
26b60 74 65 64 2c 20 5b 73 71 6c 69 74 65 33 5f 73 6e  ted, [sqlite3_sn
26b70 70 72 69 6e 74 66 28 29 5d 20 69 73 20 75 73 65  printf()] is use
26b80 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 74 6f 20  d internally to 
26b90 70 65 72 66 6f 72 6d 20 74 68 65 0a 2a 2a 20 63  perform the.** c
26ba0 6f 6e 76 65 72 73 69 6f 6e 20 61 75 74 6f 6d 61  onversion automa
26bb0 74 69 63 61 6c 6c 79 2e 20 20 54 68 65 20 66 6f  tically.  The fo
26bc0 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 65  llowing table de
26bd0 74 61 69 6c 73 20 74 68 65 20 63 6f 6e 76 65 72  tails the conver
26be0 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 61 72  sions.** that ar
26bf0 65 20 61 70 70 6c 69 65 64 3a 0a 2a 2a 0a 2a 2a  e applied:.**.**
26c00 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a   <blockquote>.**
26c10 20 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 22   <table border="
26c20 31 22 3e 0a 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20  1">.** <tr><th> 
26c30 49 6e 74 65 72 6e 61 6c 3c 62 72 3e 54 79 70 65  Internal<br>Type
26c40 20 3c 74 68 3e 20 52 65 71 75 65 73 74 65 64 3c   <th> Requested<
26c50 62 72 3e 54 79 70 65 20 3c 74 68 3e 20 20 43 6f  br>Type <th>  Co
26c60 6e 76 65 72 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c  nversion.**.** <
26c70 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20 20  tr><td>  NULL   
26c80 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20   <td> INTEGER   
26c90 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20 30  <td> Result is 0
26ca0 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 4e 55  .** <tr><td>  NU
26cb0 4c 4c 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41  LL    <td>  FLOA
26cc0 54 20 20 20 20 3c 74 64 3e 20 52 65 73 75 6c 74  T    <td> Result
26cd0 20 69 73 20 30 2e 30 0a 2a 2a 20 3c 74 72 3e 3c   is 0.0.** <tr><
26ce0 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64  td>  NULL    <td
26cf0 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64 3e  >   TEXT    <td>
26d00 20 52 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20   Result is NULL 
26d10 70 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c  pointer.** <tr><
26d20 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64  td>  NULL    <td
26d30 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e  >   BLOB    <td>
26d40 20 52 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20   Result is NULL 
26d50 70 6f 69 6e 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c  pointer.** <tr><
26d60 74 64 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64  td> INTEGER  <td
26d70 3e 20 20 46 4c 4f 41 54 20 20 20 20 3c 74 64 3e  >  FLOAT    <td>
26d80 20 43 6f 6e 76 65 72 74 20 66 72 6f 6d 20 69 6e   Convert from in
26d90 74 65 67 65 72 20 74 6f 20 66 6c 6f 61 74 0a 2a  teger to float.*
26da0 2a 20 3c 74 72 3e 3c 74 64 3e 20 49 4e 54 45 47  * <tr><td> INTEG
26db0 45 52 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20  ER  <td>   TEXT 
26dc0 20 20 20 3c 74 64 3e 20 41 53 43 49 49 20 72 65     <td> ASCII re
26dd0 6e 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20 69  ndering of the i
26de0 6e 74 65 67 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74  nteger.** <tr><t
26df0 64 3e 20 49 4e 54 45 47 45 52 20 20 3c 74 64 3e  d> INTEGER  <td>
26e00 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20     BLOB    <td> 
26e10 53 61 6d 65 20 61 73 20 49 4e 54 45 47 45 52 2d  Same as INTEGER-
26e20 3e 54 45 58 54 0a 2a 2a 20 3c 74 72 3e 3c 74 64  >TEXT.** <tr><td
26e30 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20  >  FLOAT   <td> 
26e40 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20 43  INTEGER   <td> C
26e50 6f 6e 76 65 72 74 20 66 72 6f 6d 20 66 6c 6f 61  onvert from floa
26e60 74 20 74 6f 20 69 6e 74 65 67 65 72 0a 2a 2a 20  t to integer.** 
26e70 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41 54 20  <tr><td>  FLOAT 
26e80 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20    <td>   TEXT   
26e90 20 3c 74 64 3e 20 41 53 43 49 49 20 72 65 6e 64   <td> ASCII rend
26ea0 65 72 69 6e 67 20 6f 66 20 74 68 65 20 66 6c 6f  ering of the flo
26eb0 61 74 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20  at.** <tr><td>  
26ec0 46 4c 4f 41 54 20 20 20 3c 74 64 3e 20 20 20 42  FLOAT   <td>   B
26ed0 4c 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65  LOB    <td> Same
26ee0 20 61 73 20 46 4c 4f 41 54 2d 3e 54 45 58 54 0a   as FLOAT->TEXT.
26ef0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58  ** <tr><td>  TEX
26f00 54 20 20 20 20 3c 74 64 3e 20 49 4e 54 45 47 45  T    <td> INTEGE
26f10 52 20 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f  R   <td> Use ato
26f20 69 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  i().** <tr><td> 
26f30 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 20 46   TEXT    <td>  F
26f40 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 55 73 65  LOAT    <td> Use
26f50 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c   atof().** <tr><
26f60 74 64 3e 20 20 54 45 58 54 20 20 20 20 3c 74 64  td>  TEXT    <td
26f70 3e 20 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e  >   BLOB    <td>
26f80 20 4e 6f 20 63 68 61 6e 67 65 0a 2a 2a 20 3c 74   No change.** <t
26f90 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20  r><td>  BLOB    
26fa0 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20 3c  <td> INTEGER   <
26fb0 74 64 3e 20 43 6f 6e 76 65 72 74 20 74 6f 20 54  td> Convert to T
26fc0 45 58 54 20 74 68 65 6e 20 75 73 65 20 61 74 6f  EXT then use ato
26fd0 69 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  i().** <tr><td> 
26fe0 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 20 46   BLOB    <td>  F
26ff0 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e  LOAT    <td> Con
27000 76 65 72 74 20 74 6f 20 54 45 58 54 20 74 68 65  vert to TEXT the
27010 6e 20 75 73 65 20 61 74 6f 66 28 29 0a 2a 2a 20  n use atof().** 
27020 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f 42 20 20  <tr><td>  BLOB  
27030 20 20 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20    <td>   TEXT   
27040 20 3c 74 64 3e 20 41 64 64 20 61 20 7a 65 72 6f   <td> Add a zero
27050 20 74 65 72 6d 69 6e 61 74 6f 72 20 69 66 20 6e   terminator if n
27060 65 65 64 65 64 0a 2a 2a 20 3c 2f 74 61 62 6c 65  eeded.** </table
27070 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74  >.** </blockquot
27080 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62  e>.**.** The tab
27090 6c 65 20 61 62 6f 76 65 20 6d 61 6b 65 73 20 72  le above makes r
270a0 65 66 65 72 65 6e 63 65 20 74 6f 20 73 74 61 6e  eference to stan
270b0 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 20 66  dard C library f
270c0 75 6e 63 74 69 6f 6e 73 20 61 74 6f 69 28 29 0a  unctions atoi().
270d0 2a 2a 20 61 6e 64 20 61 74 6f 66 28 29 2e 20 20  ** and atof().  
270e0 53 51 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20  SQLite does not 
270f0 72 65 61 6c 6c 79 20 75 73 65 20 74 68 65 73 65  really use these
27100 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 49 74 20   functions.  It 
27110 68 61 73 20 69 74 73 0a 2a 2a 20 6f 77 6e 20 65  has its.** own e
27120 71 75 69 76 61 6c 65 6e 74 20 69 6e 74 65 72 6e  quivalent intern
27130 61 6c 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 68  al routines.  Th
27140 65 20 61 74 6f 69 28 29 20 61 6e 64 20 61 74 6f  e atoi() and ato
27150 66 28 29 20 6e 61 6d 65 73 20 61 72 65 0a 2a 2a  f() names are.**
27160 20 75 73 65 64 20 69 6e 20 74 68 65 20 74 61 62   used in the tab
27170 6c 65 20 66 6f 72 20 62 72 65 76 69 74 79 20 61  le for brevity a
27180 6e 64 20 62 65 63 61 75 73 65 20 74 68 65 79 20  nd because they 
27190 61 72 65 20 66 61 6d 69 6c 69 61 72 20 74 6f 20  are familiar to 
271a0 6d 6f 73 74 0a 2a 2a 20 43 20 70 72 6f 67 72 61  most.** C progra
271b0 6d 6d 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  mmers..**.** Not
271c0 65 20 74 68 61 74 20 77 68 65 6e 20 74 79 70 65  e that when type
271d0 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 6f 63 63   conversions occ
271e0 75 72 2c 20 70 6f 69 6e 74 65 72 73 20 72 65 74  ur, pointers ret
271f0 75 72 6e 65 64 20 62 79 20 70 72 69 6f 72 0a 2a  urned by prior.*
27200 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  * calls to sqlit
27210 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29  e3_column_blob()
27220 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  , sqlite3_column
27230 5f 74 65 78 74 28 29 2c 20 61 6e 64 2f 6f 72 0a  _text(), and/or.
27240 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ** sqlite3_colum
27250 6e 5f 74 65 78 74 31 36 28 29 20 6d 61 79 20 62  n_text16() may b
27260 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a  e invalidated..*
27270 2a 20 54 79 70 65 20 63 6f 6e 76 65 72 73 69 6f  * Type conversio
27280 6e 73 20 61 6e 64 20 70 6f 69 6e 74 65 72 20 69  ns and pointer i
27290 6e 76 61 6c 69 64 61 74 69 6f 6e 73 20 6d 69 67  nvalidations mig
272a0 68 74 20 6f 63 63 75 72 0a 2a 2a 20 69 6e 20 74  ht occur.** in t
272b0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 73  he following cas
272c0 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a  es:.**.** <ul>.*
272d0 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69  * <li> The initi
272e0 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 61 20  al content is a 
272f0 42 4c 4f 42 20 61 6e 64 20 73 71 6c 69 74 65 33  BLOB and sqlite3
27300 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20 6f  _column_text() o
27310 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74 65  r.**      sqlite
27320 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28  3_column_text16(
27330 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 41 20  ) is called.  A 
27340 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 20  zero-terminator 
27350 6d 69 67 68 74 0a 2a 2a 20 20 20 20 20 20 6e 65  might.**      ne
27360 65 64 20 74 6f 20 62 65 20 61 64 64 65 64 20 74  ed to be added t
27370 6f 20 74 68 65 20 73 74 72 69 6e 67 2e 3c 2f 6c  o the string.</l
27380 69 3e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 69  i>.** <li> The i
27390 6e 69 74 69 61 6c 20 63 6f 6e 74 65 6e 74 20 69  nitial content i
273a0 73 20 55 54 46 2d 38 20 74 65 78 74 20 61 6e 64  s UTF-8 text and
273b0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
273c0 62 79 74 65 73 31 36 28 29 20 6f 72 0a 2a 2a 20  bytes16() or.** 
273d0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c       sqlite3_col
273e0 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 69 73 20  umn_text16() is 
273f0 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 63 6f 6e  called.  The con
27400 74 65 6e 74 20 6d 75 73 74 20 62 65 20 63 6f 6e  tent must be con
27410 76 65 72 74 65 64 0a 2a 2a 20 20 20 20 20 20 74  verted.**      t
27420 6f 20 55 54 46 2d 31 36 2e 3c 2f 6c 69 3e 0a 2a  o UTF-16.</li>.*
27430 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69  * <li> The initi
27440 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 54  al content is UT
27450 46 2d 31 36 20 74 65 78 74 20 61 6e 64 20 73 71  F-16 text and sq
27460 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
27470 65 73 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20 20  es() or.**      
27480 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
27490 65 78 74 28 29 20 69 73 20 63 61 6c 6c 65 64 2e  ext() is called.
274a0 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6d 75    The content mu
274b0 73 74 20 62 65 20 63 6f 6e 76 65 72 74 65 64 0a  st be converted.
274c0 2a 2a 20 20 20 20 20 20 74 6f 20 55 54 46 2d 38  **      to UTF-8
274d0 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a  .</li>.** </ul>.
274e0 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 73 69 6f 6e  **.** Conversion
274f0 73 20 62 65 74 77 65 65 6e 20 55 54 46 2d 31 36  s between UTF-16
27500 62 65 20 61 6e 64 20 55 54 46 2d 31 36 6c 65 20  be and UTF-16le 
27510 61 72 65 20 61 6c 77 61 79 73 20 64 6f 6e 65 20  are always done 
27520 69 6e 20 70 6c 61 63 65 20 61 6e 64 20 64 6f 0a  in place and do.
27530 2a 2a 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74  ** not invalidat
27540 65 20 61 20 70 72 69 6f 72 20 70 6f 69 6e 74 65  e a prior pointe
27550 72 2c 20 74 68 6f 75 67 68 20 6f 66 20 63 6f 75  r, though of cou
27560 72 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  rse the content 
27570 6f 66 20 74 68 65 20 62 75 66 66 65 72 0a 2a 2a  of the buffer.**
27580 20 74 68 61 74 20 74 68 65 20 70 72 69 6f 72 20   that the prior 
27590 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20 74  pointer points t
275a0 6f 20 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e  o will have been
275b0 20 6d 6f 64 69 66 69 65 64 2e 20 20 4f 74 68 65   modified.  Othe
275c0 72 20 6b 69 6e 64 73 0a 2a 2a 20 6f 66 20 63 6f  r kinds.** of co
275d0 6e 76 65 72 73 69 6f 6e 20 61 72 65 20 64 6f 6e  nversion are don
275e0 65 20 69 6e 20 70 6c 61 63 65 20 77 68 65 6e 20  e in place when 
275f0 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 2c 20  it is possible, 
27600 62 75 74 20 73 6f 6d 65 74 69 6d 65 73 20 74 68  but sometimes th
27610 65 79 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 70 6f  ey.** are not po
27620 73 73 69 62 6c 65 20 61 6e 64 20 69 6e 20 74 68  ssible and in th
27630 6f 73 65 20 63 61 73 65 73 20 70 72 69 6f 72 20  ose cases prior 
27640 70 6f 69 6e 74 65 72 73 20 61 72 65 20 69 6e 76  pointers are inv
27650 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  alidated..**.** 
27660 54 68 65 20 73 61 66 65 73 74 20 61 6e 64 20 65  The safest and e
27670 61 73 69 65 73 74 20 74 6f 20 72 65 6d 65 6d 62  asiest to rememb
27680 65 72 20 70 6f 6c 69 63 79 20 69 73 20 74 6f 20  er policy is to 
27690 69 6e 76 6f 6b 65 20 74 68 65 73 65 20 72 6f 75  invoke these rou
276a0 74 69 6e 65 73 0a 2a 2a 20 69 6e 20 6f 6e 65 20  tines.** in one 
276b0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
276c0 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c   ways:.**.** <ul
276d0 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69 74 65  >.**  <li>sqlite
276e0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 20  3_column_text() 
276f0 66 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69  followed by sqli
27700 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  te3_column_bytes
27710 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e  ()</li>.**  <li>
27720 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
27730 6c 6f 62 28 29 20 66 6f 6c 6c 6f 77 65 64 20 62  lob() followed b
27740 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  y sqlite3_column
27750 5f 62 79 74 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a  _bytes()</li>.**
27760 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f    <li>sqlite3_co
27770 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 6f  lumn_text16() fo
27780 6c 6c 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65  llowed by sqlite
27790 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36  3_column_bytes16
277a0 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e  ()</li>.** </ul>
277b0 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20  .**.** In other 
277c0 77 6f 72 64 73 2c 20 79 6f 75 20 73 68 6f 75 6c  words, you shoul
277d0 64 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 63  d call sqlite3_c
277e0 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 2c 0a 2a 2a  olumn_text(),.**
277f0 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
27800 62 6c 6f 62 28 29 2c 20 6f 72 20 73 71 6c 69 74  blob(), or sqlit
27810 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36  e3_column_text16
27820 28 29 20 66 69 72 73 74 20 74 6f 20 66 6f 72 63  () first to forc
27830 65 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20  e the result.** 
27840 69 6e 74 6f 20 74 68 65 20 64 65 73 69 72 65 64  into the desired
27850 20 66 6f 72 6d 61 74 2c 20 74 68 65 6e 20 69 6e   format, then in
27860 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  voke sqlite3_col
27870 75 6d 6e 5f 62 79 74 65 73 28 29 20 6f 72 0a 2a  umn_bytes() or.*
27880 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  * sqlite3_column
27890 5f 62 79 74 65 73 31 36 28 29 20 74 6f 20 66 69  _bytes16() to fi
278a0 6e 64 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  nd the size of t
278b0 68 65 20 72 65 73 75 6c 74 2e 20 20 44 6f 20 6e  he result.  Do n
278c0 6f 74 20 6d 69 78 20 63 61 6c 6c 73 0a 2a 2a 20  ot mix calls.** 
278d0 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  to sqlite3_colum
278e0 6e 5f 74 65 78 74 28 29 20 6f 72 20 73 71 6c 69  n_text() or sqli
278f0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
27900 29 20 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 0a  ) with calls to.
27910 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ** sqlite3_colum
27920 6e 5f 62 79 74 65 73 31 36 28 29 2c 20 61 6e 64  n_bytes16(), and
27930 20 64 6f 20 6e 6f 74 20 6d 69 78 20 63 61 6c 6c   do not mix call
27940 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c  s to sqlite3_col
27950 75 6d 6e 5f 74 65 78 74 31 36 28 29 0a 2a 2a 20  umn_text16().** 
27960 77 69 74 68 20 63 61 6c 6c 73 20 74 6f 20 73 71  with calls to sq
27970 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
27980 65 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  es()..**.** The 
27990 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65  pointers returne
279a0 64 20 61 72 65 20 76 61 6c 69 64 20 75 6e 74 69  d are valid unti
279b0 6c 20 61 20 74 79 70 65 20 63 6f 6e 76 65 72 73  l a type convers
279c0 69 6f 6e 20 6f 63 63 75 72 73 20 61 73 0a 2a 2a  ion occurs as.**
279d0 20 64 65 73 63 72 69 62 65 64 20 61 62 6f 76 65   described above
279e0 2c 20 6f 72 20 75 6e 74 69 6c 20 5b 73 71 6c 69  , or until [sqli
279f0 74 65 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b  te3_step()] or [
27a00 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d  sqlite3_reset()]
27a10 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   or.** [sqlite3_
27a20 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69 73 20 63  finalize()] is c
27a30 61 6c 6c 65 64 2e 20 20 54 68 65 20 6d 65 6d 6f  alled.  The memo
27a40 72 79 20 73 70 61 63 65 20 75 73 65 64 20 74 6f  ry space used to
27a50 20 68 6f 6c 64 20 73 74 72 69 6e 67 73 0a 2a 2a   hold strings.**
27a60 20 61 6e 64 20 42 4c 4f 42 73 20 69 73 20 66 72   and BLOBs is fr
27a70 65 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  eed automaticall
27a80 79 2e 20 20 44 6f 20 3c 62 3e 6e 6f 74 3c 2f 62  y.  Do <b>not</b
27a90 3e 20 70 61 73 73 20 74 68 65 20 70 6f 69 6e 74  > pass the point
27aa0 65 72 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20  ers returned.** 
27ab0 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  [sqlite3_column_
27ac0 62 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65  blob()], [sqlite
27ad0 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 29 5d  3_column_text()]
27ae0 2c 20 65 74 63 2e 20 69 6e 74 6f 0a 2a 2a 20 5b  , etc. into.** [
27af0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e  sqlite3_free()].
27b00 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f  .**.** If a memo
27b10 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72  ry allocation er
27b20 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e  ror occurs durin
27b30 67 20 74 68 65 20 65 76 61 6c 75 61 74 69 6f 6e  g the evaluation
27b40 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68   of any.** of th
27b50 65 73 65 20 72 6f 75 74 69 6e 65 73 2c 20 61 20  ese routines, a 
27b60 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69 73  default value is
27b70 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68 65 20   returned.  The 
27b80 64 65 66 61 75 6c 74 20 76 61 6c 75 65 0a 2a 2a  default value.**
27b90 20 69 73 20 65 69 74 68 65 72 20 74 68 65 20 69   is either the i
27ba0 6e 74 65 67 65 72 20 30 2c 20 74 68 65 20 66 6c  nteger 0, the fl
27bb0 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d  oating point num
27bc0 62 65 72 20 30 2e 30 2c 20 6f 72 20 61 20 4e 55  ber 0.0, or a NU
27bd0 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 20  LL.** pointer.  
27be0 53 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73  Subsequent calls
27bf0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72   to [sqlite3_err
27c00 63 6f 64 65 28 29 5d 20 77 69 6c 6c 20 72 65 74  code()] will ret
27c10 75 72 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e  urn.** [SQLITE_N
27c20 4f 4d 45 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  OMEM]..**.** Req
27c30 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
27c40 31 33 38 30 33 5d 20 5b 48 31 33 38 30 36 5d 20  13803] [H13806] 
27c50 5b 48 31 33 38 30 39 5d 20 5b 48 31 33 38 31 32  [H13809] [H13812
27c60 5d 20 5b 48 31 33 38 31 35 5d 20 5b 48 31 33 38  ] [H13815] [H138
27c70 31 38 5d 20 5b 48 31 33 38 32 31 5d 20 5b 48 31  18] [H13821] [H1
27c80 33 38 32 34 5d 0a 2a 2a 20 5b 48 31 33 38 32 37  3824].** [H13827
27c90 5d 20 5b 48 31 33 38 33 30 5d 0a 2a 2f 0a 53 51  ] [H13830].*/.SQ
27ca0 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
27cb0 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
27cc0 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33  umn_blob(sqlite3
27cd0 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c  _stmt*, int iCol
27ce0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
27cf0 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t sqlite3_column
27d00 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 73  _bytes(sqlite3_s
27d10 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b  tmt*, int iCol);
27d20 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
27d30 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
27d40 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73  ytes16(sqlite3_s
27d50 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b  tmt*, int iCol);
27d60 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62  .SQLITE_API doub
27d70 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  le sqlite3_colum
27d80 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33  n_double(sqlite3
27d90 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c  _stmt*, int iCol
27da0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
27db0 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  t sqlite3_column
27dc0 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  _int(sqlite3_stm
27dd0 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53  t*, int iCol);.S
27de0 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
27df0 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  3_int64 sqlite3_
27e00 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c  column_int64(sql
27e10 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20  ite3_stmt*, int 
27e20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50  iCol);.SQLITE_AP
27e30 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  I const unsigned
27e40 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
27e50 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74  olumn_text(sqlit
27e60 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43  e3_stmt*, int iC
27e70 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  ol);.SQLITE_API 
27e80 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
27e90 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  te3_column_text1
27ea0 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  6(sqlite3_stmt*,
27eb0 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49   int iCol);.SQLI
27ec0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
27ed0 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73  e3_column_type(s
27ee0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
27ef0 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f  t iCol);.SQLITE_
27f00 41 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  API sqlite3_valu
27f10 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  e *sqlite3_colum
27f20 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f  n_value(sqlite3_
27f30 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29  stmt*, int iCol)
27f40 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
27f50 46 3a 20 44 65 73 74 72 6f 79 20 41 20 50 72 65  F: Destroy A Pre
27f60 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20  pared Statement 
27f70 4f 62 6a 65 63 74 20 7b 48 31 33 33 30 30 7d 20  Object {H13300} 
27f80 3c 53 37 30 33 30 30 3e 3c 53 33 30 31 30 30 3e  <S70300><S30100>
27f90 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
27fa0 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66 75  e3_finalize() fu
27fb0 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
27fc0 20 74 6f 20 64 65 6c 65 74 65 20 61 20 5b 70 72   to delete a [pr
27fd0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
27fe0 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 73 74 61  ]..** If the sta
27ff0 74 65 6d 65 6e 74 20 77 61 73 20 65 78 65 63 75  tement was execu
28000 74 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79  ted successfully
28010 20 6f 72 20 6e 6f 74 20 65 78 65 63 75 74 65 64   or not executed
28020 20 61 74 20 61 6c 6c 2c 20 74 68 65 6e 0a 2a 2a   at all, then.**
28030 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
28040 74 75 72 6e 65 64 2e 20 49 66 20 65 78 65 63 75  turned. If execu
28050 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74  tion of the stat
28060 65 6d 65 6e 74 20 66 61 69 6c 65 64 20 74 68 65  ement failed the
28070 6e 20 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20 63  n an.** [error c
28080 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65  ode] or [extende
28090 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73  d error code] is
280a0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
280b0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   This routine ca
280c0 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20 61  n be called at a
280d0 6e 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67 20  ny point during 
280e0 74 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f 66  the execution of
280f0 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65   the.** [prepare
28100 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49  d statement].  I
28110 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61  f the virtual ma
28120 63 68 69 6e 65 20 68 61 73 20 6e 6f 74 0a 2a 2a  chine has not.**
28130 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63 75   completed execu
28140 74 69 6f 6e 20 77 68 65 6e 20 74 68 69 73 20 72  tion when this r
28150 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
28160 2c 20 74 68 61 74 20 69 73 20 6c 69 6b 65 0a 2a  , that is like.*
28170 2a 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20 61  * encountering a
28180 6e 20 65 72 72 6f 72 20 6f 72 20 61 6e 20 5b 73  n error or an [s
28190 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
281a0 20 7c 20 69 6e 74 65 72 72 75 70 74 5d 2e 0a 2a   | interrupt]..*
281b0 2a 20 49 6e 63 6f 6d 70 6c 65 74 65 20 75 70 64  * Incomplete upd
281c0 61 74 65 73 20 6d 61 79 20 62 65 20 72 6f 6c 6c  ates may be roll
281d0 65 64 20 62 61 63 6b 20 61 6e 64 20 74 72 61 6e  ed back and tran
281e0 73 61 63 74 69 6f 6e 73 20 63 61 6e 63 65 6c 65  sactions cancele
281f0 64 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67 20  d,.** depending 
28200 6f 6e 20 74 68 65 20 63 69 72 63 75 6d 73 74 61  on the circumsta
28210 6e 63 65 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a  nces, and the.**
28220 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 72 65   [error code] re
28230 74 75 72 6e 65 64 20 77 69 6c 6c 20 62 65 20 5b  turned will be [
28240 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a  SQLITE_ABORT]..*
28250 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
28260 73 3a 0a 2a 2a 20 5b 48 31 31 33 30 32 5d 20 5b  s:.** [H11302] [
28270 48 31 31 33 30 34 5d 0a 2a 2f 0a 53 51 4c 49 54  H11304].*/.SQLIT
28280 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
28290 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74  3_finalize(sqlit
282a0 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b  e3_stmt *pStmt);
282b0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
282c0 3a 20 52 65 73 65 74 20 41 20 50 72 65 70 61 72  : Reset A Prepar
282d0 65 64 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a  ed Statement Obj
282e0 65 63 74 20 7b 48 31 33 33 33 30 7d 20 3c 53 37  ect {H13330} <S7
282f0 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0300>.**.** The 
28300 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20  sqlite3_reset() 
28310 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
28320 65 64 20 74 6f 20 72 65 73 65 74 20 61 20 5b 70  ed to reset a [p
28330 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
28340 74 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20 62 61 63  t].** object bac
28350 6b 20 74 6f 20 69 74 73 20 69 6e 69 74 69 61 6c  k to its initial
28360 20 73 74 61 74 65 2c 20 72 65 61 64 79 20 74 6f   state, ready to
28370 20 62 65 20 72 65 2d 65 78 65 63 75 74 65 64 2e   be re-executed.
28380 0a 2a 2a 20 41 6e 79 20 53 51 4c 20 73 74 61 74  .** Any SQL stat
28390 65 6d 65 6e 74 20 76 61 72 69 61 62 6c 65 73 20  ement variables 
283a0 74 68 61 74 20 68 61 64 20 76 61 6c 75 65 73 20  that had values 
283b0 62 6f 75 6e 64 20 74 6f 20 74 68 65 6d 20 75 73  bound to them us
283c0 69 6e 67 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69  ing.** the [sqli
283d0 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 7c 20  te3_bind_blob | 
283e0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a 28 29  sqlite3_bind_*()
283f0 20 41 50 49 5d 20 72 65 74 61 69 6e 20 74 68 65   API] retain the
28400 69 72 20 76 61 6c 75 65 73 2e 0a 2a 2a 20 55 73  ir values..** Us
28410 65 20 5b 73 71 6c 69 74 65 33 5f 63 6c 65 61 72  e [sqlite3_clear
28420 5f 62 69 6e 64 69 6e 67 73 28 29 5d 20 74 6f 20  _bindings()] to 
28430 72 65 73 65 74 20 74 68 65 20 62 69 6e 64 69 6e  reset the bindin
28440 67 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 33  gs..**.** {H1133
28450 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  2} The [sqlite3_
28460 72 65 73 65 74 28 53 29 5d 20 69 6e 74 65 72 66  reset(S)] interf
28470 61 63 65 20 72 65 73 65 74 73 20 74 68 65 20 5b  ace resets the [
28480 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
28490 6e 74 5d 20 53 0a 2a 2a 20 20 20 20 20 20 20 20  nt] S.**        
284a0 20 20 62 61 63 6b 20 74 6f 20 74 68 65 20 62 65    back to the be
284b0 67 69 6e 6e 69 6e 67 20 6f 66 20 69 74 73 20 70  ginning of its p
284c0 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 7b 48  rogram..**.** {H
284d0 31 31 33 33 34 7d 20 49 66 20 74 68 65 20 6d 6f  11334} If the mo
284e0 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74  st recent call t
284f0 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  o [sqlite3_step(
28500 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20  S)] for the.**  
28510 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65          [prepare
28520 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 72  d statement] S r
28530 65 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f  eturned [SQLITE_
28540 52 4f 57 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f  ROW] or [SQLITE_
28550 44 4f 4e 45 5d 2c 0a 2a 2a 20 20 20 20 20 20 20  DONE],.**       
28560 20 20 20 6f 72 20 69 66 20 5b 73 71 6c 69 74 65     or if [sqlite
28570 33 5f 73 74 65 70 28 53 29 5d 20 68 61 73 20 6e  3_step(S)] has n
28580 65 76 65 72 20 62 65 66 6f 72 65 20 62 65 65 6e  ever before been
28590 20 63 61 6c 6c 65 64 20 6f 6e 20 53 2c 0a 2a 2a   called on S,.**
285a0 20 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 5b            then [
285b0 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29  sqlite3_reset(S)
285c0 5d 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54  ] returns [SQLIT
285d0 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  E_OK]..**.** {H1
285e0 31 33 33 36 7d 20 49 66 20 74 68 65 20 6d 6f 73  1336} If the mos
285f0 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f  t recent call to
28600 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53   [sqlite3_step(S
28610 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20  )] for the.**   
28620 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64         [prepared
28630 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 69 6e   statement] S in
28640 64 69 63 61 74 65 64 20 61 6e 20 65 72 72 6f 72  dicated an error
28650 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20  , then.**       
28660 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65     [sqlite3_rese
28670 74 28 53 29 5d 20 72 65 74 75 72 6e 73 20 61 6e  t(S)] returns an
28680 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b 65 72   appropriate [er
28690 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a  ror code]..**.**
286a0 20 7b 48 31 31 33 33 38 7d 20 54 68 65 20 5b 73   {H11338} The [s
286b0 71 6c 69 74 65 33 5f 72 65 73 65 74 28 53 29 5d  qlite3_reset(S)]
286c0 20 69 6e 74 65 72 66 61 63 65 20 64 6f 65 73 20   interface does 
286d0 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20 76  not change the v
286e0 61 6c 75 65 73 0a 2a 2a 20 20 20 20 20 20 20 20  alues.**        
286f0 20 20 6f 66 20 61 6e 79 20 5b 73 71 6c 69 74 65    of any [sqlite
28700 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 62 69 6e 64  3_bind_blob|bind
28710 69 6e 67 73 5d 20 6f 6e 20 74 68 65 20 5b 70 72  ings] on the [pr
28720 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
28730 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ] S..*/.SQLITE_A
28740 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 72  PI int sqlite3_r
28750 65 73 65 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  eset(sqlite3_stm
28760 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a  t *pStmt);../*.*
28770 2a 20 43 41 50 49 33 52 45 46 3a 20 43 72 65 61  * CAPI3REF: Crea
28780 74 65 20 4f 72 20 52 65 64 65 66 69 6e 65 20 53  te Or Redefine S
28790 51 4c 20 46 75 6e 63 74 69 6f 6e 73 20 7b 48 31  QL Functions {H1
287a0 36 31 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a  6100} <S20200>.*
287b0 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 66 75 6e  * KEYWORDS: {fun
287c0 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72  ction creation r
287d0 6f 75 74 69 6e 65 73 7d 0a 2a 2a 20 4b 45 59 57  outines}.** KEYW
287e0 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69  ORDS: {applicati
287f0 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66  on-defined SQL f
28800 75 6e 63 74 69 6f 6e 7d 0a 2a 2a 20 4b 45 59 57  unction}.** KEYW
28810 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63 61 74 69  ORDS: {applicati
28820 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20 66  on-defined SQL f
28830 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20  unctions}.**.** 
28840 54 68 65 73 65 20 74 77 6f 20 66 75 6e 63 74 69  These two functi
28850 6f 6e 73 20 28 63 6f 6c 6c 65 63 74 69 76 65 6c  ons (collectivel
28860 79 20 6b 6e 6f 77 6e 20 61 73 20 22 66 75 6e 63  y known as "func
28870 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e 20 72 6f  tion creation ro
28880 75 74 69 6e 65 73 22 29 0a 2a 2a 20 61 72 65 20  utines").** are 
28890 75 73 65 64 20 74 6f 20 61 64 64 20 53 51 4c 20  used to add SQL 
288a0 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 61 67 67  functions or agg
288b0 72 65 67 61 74 65 73 20 6f 72 20 74 6f 20 72 65  regates or to re
288c0 64 65 66 69 6e 65 20 74 68 65 20 62 65 68 61 76  define the behav
288d0 69 6f 72 0a 2a 2a 20 6f 66 20 65 78 69 73 74 69  ior.** of existi
288e0 6e 67 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73  ng SQL functions
288f0 20 6f 72 20 61 67 67 72 65 67 61 74 65 73 2e 20   or aggregates. 
28900 20 54 68 65 20 6f 6e 6c 79 20 64 69 66 66 65 72   The only differ
28910 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68 65  ence between the
28920 0a 2a 2a 20 74 77 6f 20 69 73 20 74 68 61 74 20  .** two is that 
28930 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  the second param
28940 65 74 65 72 2c 20 74 68 65 20 6e 61 6d 65 20 6f  eter, the name o
28950 66 20 74 68 65 20 28 73 63 61 6c 61 72 29 20 66  f the (scalar) f
28960 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67  unction or.** ag
28970 67 72 65 67 61 74 65 2c 20 69 73 20 65 6e 63 6f  gregate, is enco
28980 64 65 64 20 69 6e 20 55 54 46 2d 38 20 66 6f 72  ded in UTF-8 for
28990 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
289a0 66 75 6e 63 74 69 6f 6e 28 29 20 61 6e 64 20 55  function() and U
289b0 54 46 2d 31 36 0a 2a 2a 20 66 6f 72 20 73 71 6c  TF-16.** for sql
289c0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
289d0 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a 2a 2a 20  tion16()..**.** 
289e0 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  The first parame
289f0 74 65 72 20 69 73 20 74 68 65 20 5b 64 61 74 61  ter is the [data
28a00 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
28a10 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 53 51   to which the SQ
28a20 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73  L.** function is
28a30 20 74 6f 20 62 65 20 61 64 64 65 64 2e 20 20 49   to be added.  I
28a40 66 20 61 20 73 69 6e 67 6c 65 20 70 72 6f 67 72  f a single progr
28a50 61 6d 20 75 73 65 73 20 6d 6f 72 65 20 74 68 61  am uses more tha
28a60 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 0a 2a  n one database.*
28a70 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 74  * connection int
28a80 65 72 6e 61 6c 6c 79 2c 20 74 68 65 6e 20 53 51  ernally, then SQ
28a90 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6d 75 73 74  L functions must
28aa0 20 62 65 20 61 64 64 65 64 20 69 6e 64 69 76 69   be added indivi
28ab0 64 75 61 6c 6c 79 20 74 6f 0a 2a 2a 20 65 61 63  dually to.** eac
28ac0 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  h database conne
28ad0 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ction..**.** The
28ae0 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
28af0 72 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  r is the name of
28b00 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
28b10 6e 20 74 6f 20 62 65 20 63 72 65 61 74 65 64 20  n to be created 
28b20 6f 72 0a 2a 2a 20 72 65 64 65 66 69 6e 65 64 2e  or.** redefined.
28b30 20 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20    The length of 
28b40 74 68 65 20 6e 61 6d 65 20 69 73 20 6c 69 6d 69  the name is limi
28b50 74 65 64 20 74 6f 20 32 35 35 20 62 79 74 65 73  ted to 255 bytes
28b60 2c 20 65 78 63 6c 75 73 69 76 65 20 6f 66 0a 2a  , exclusive of.*
28b70 2a 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69  * the zero-termi
28b80 6e 61 74 6f 72 2e 20 20 4e 6f 74 65 20 74 68 61  nator.  Note tha
28b90 74 20 74 68 65 20 6e 61 6d 65 20 6c 65 6e 67 74  t the name lengt
28ba0 68 20 6c 69 6d 69 74 20 69 73 20 69 6e 20 62 79  h limit is in by
28bb0 74 65 73 2c 20 6e 6f 74 0a 2a 2a 20 63 68 61 72  tes, not.** char
28bc0 61 63 74 65 72 73 2e 20 20 41 6e 79 20 61 74 74  acters.  Any att
28bd0 65 6d 70 74 20 74 6f 20 63 72 65 61 74 65 20 61  empt to create a
28be0 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 61   function with a
28bf0 20 6c 6f 6e 67 65 72 20 6e 61 6d 65 0a 2a 2a 20   longer name.** 
28c00 77 69 6c 6c 20 72 65 73 75 6c 74 20 69 6e 20 5b  will result in [
28c10 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 62 65  SQLITE_ERROR] be
28c20 69 6e 67 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a  ing returned..**
28c30 0a 2a 2a 20 54 68 65 20 74 68 69 72 64 20 70 61  .** The third pa
28c40 72 61 6d 65 74 65 72 20 28 6e 41 72 67 29 0a 2a  rameter (nArg).*
28c50 2a 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  * is the number 
28c60 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74 68 61  of arguments tha
28c70 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69  t the SQL functi
28c80 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61  on or.** aggrega
28c90 74 65 20 74 61 6b 65 73 2e 20 49 66 20 74 68 69  te takes. If thi
28ca0 73 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 2d  s parameter is -
28cb0 31 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c 20  1, then the SQL 
28cc0 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61  function or.** a
28cd0 67 67 72 65 67 61 74 65 20 6d 61 79 20 74 61 6b  ggregate may tak
28ce0 65 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20  e any number of 
28cf0 61 72 67 75 6d 65 6e 74 73 20 62 65 74 77 65 65  arguments betwee
28d00 6e 20 30 20 61 6e 64 20 74 68 65 20 6c 69 6d 69  n 0 and the limi
28d10 74 0a 2a 2a 20 73 65 74 20 62 79 20 5b 73 71 6c  t.** set by [sql
28d20 69 74 65 33 5f 6c 69 6d 69 74 5d 28 5b 53 51 4c  ite3_limit]([SQL
28d30 49 54 45 5f 4c 49 4d 49 54 5f 46 55 4e 43 54 49  ITE_LIMIT_FUNCTI
28d40 4f 4e 5f 41 52 47 5d 29 2e 20 20 49 66 20 74 68  ON_ARG]).  If th
28d50 65 20 74 68 69 72 64 0a 2a 2a 20 70 61 72 61 6d  e third.** param
28d60 65 74 65 72 20 69 73 20 6c 65 73 73 20 74 68 61  eter is less tha
28d70 6e 20 2d 31 20 6f 72 20 67 72 65 61 74 65 72 20  n -1 or greater 
28d80 74 68 61 6e 20 31 32 37 20 74 68 65 6e 20 74 68  than 127 then th
28d90 65 20 62 65 68 61 76 69 6f 72 20 69 73 0a 2a 2a  e behavior is.**
28da0 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a   undefined..**.*
28db0 2a 20 54 68 65 20 66 6f 75 72 74 68 20 70 61 72  * The fourth par
28dc0 61 6d 65 74 65 72 2c 20 65 54 65 78 74 52 65 70  ameter, eTextRep
28dd0 2c 20 73 70 65 63 69 66 69 65 73 20 77 68 61 74  , specifies what
28de0 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 55 54 46 38  .** [SQLITE_UTF8
28df0 20 7c 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67   | text encoding
28e00 5d 20 74 68 69 73 20 53 51 4c 20 66 75 6e 63 74  ] this SQL funct
28e10 69 6f 6e 20 70 72 65 66 65 72 73 20 66 6f 72 0a  ion prefers for.
28e20 2a 2a 20 69 74 73 20 70 61 72 61 6d 65 74 65 72  ** its parameter
28e30 73 2e 20 20 41 6e 79 20 53 51 4c 20 66 75 6e 63  s.  Any SQL func
28e40 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74  tion implementat
28e50 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 61 62  ion should be ab
28e60 6c 65 20 74 6f 20 77 6f 72 6b 0a 2a 2a 20 77 6f  le to work.** wo
28e70 72 6b 20 77 69 74 68 20 55 54 46 2d 38 2c 20 55  rk with UTF-8, U
28e80 54 46 2d 31 36 6c 65 2c 20 6f 72 20 55 54 46 2d  TF-16le, or UTF-
28e90 31 36 62 65 2e 20 20 42 75 74 20 73 6f 6d 65 20  16be.  But some 
28ea0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
28eb0 6d 61 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20 65  may be.** more e
28ec0 66 66 69 63 69 65 6e 74 20 77 69 74 68 20 6f 6e  fficient with on
28ed0 65 20 65 6e 63 6f 64 69 6e 67 20 74 68 61 6e 20  e encoding than 
28ee0 61 6e 6f 74 68 65 72 2e 20 20 41 6e 20 61 70 70  another.  An app
28ef0 6c 69 63 61 74 69 6f 6e 20 6d 61 79 0a 2a 2a 20  lication may.** 
28f00 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 63  invoke sqlite3_c
28f10 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29  reate_function()
28f20 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61   or sqlite3_crea
28f30 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 20  te_function16() 
28f40 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 74 69 6d 65  multiple.** time
28f50 73 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20  s with the same 
28f60 66 75 6e 63 74 69 6f 6e 20 62 75 74 20 77 69 74  function but wit
28f70 68 20 64 69 66 66 65 72 65 6e 74 20 76 61 6c 75  h different valu
28f80 65 73 20 6f 66 20 65 54 65 78 74 52 65 70 2e 0a  es of eTextRep..
28f90 2a 2a 20 57 68 65 6e 20 6d 75 6c 74 69 70 6c 65  ** When multiple
28fa0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
28fb0 20 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e   of the same fun
28fc0 63 74 69 6f 6e 20 61 72 65 20 61 76 61 69 6c 61  ction are availa
28fd0 62 6c 65 2c 20 53 51 4c 69 74 65 0a 2a 2a 20 77  ble, SQLite.** w
28fe0 69 6c 6c 20 70 69 63 6b 20 74 68 65 20 6f 6e 65  ill pick the one
28ff0 20 74 68 61 74 20 69 6e 76 6f 6c 76 65 73 20 74   that involves t
29000 68 65 20 6c 65 61 73 74 20 61 6d 6f 75 6e 74 20  he least amount 
29010 6f 66 20 64 61 74 61 20 63 6f 6e 76 65 72 73 69  of data conversi
29020 6f 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  on..** If there 
29030 69 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65  is only a single
29040 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
29050 77 68 69 63 68 20 64 6f 65 73 20 6e 6f 74 20 63  which does not c
29060 61 72 65 20 77 68 61 74 20 74 65 78 74 0a 2a 2a  are what text.**
29070 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 75 73 65   encoding is use
29080 64 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 75 72  d, then the four
29090 74 68 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75  th argument shou
290a0 6c 64 20 62 65 20 5b 53 51 4c 49 54 45 5f 41 4e  ld be [SQLITE_AN
290b0 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  Y]..**.** The fi
290c0 66 74 68 20 70 61 72 61 6d 65 74 65 72 20 69 73  fth parameter is
290d0 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 70 6f   an arbitrary po
290e0 69 6e 74 65 72 2e 20 20 54 68 65 20 69 6d 70 6c  inter.  The impl
290f0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
29100 65 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 63 61  e.** function ca
29110 6e 20 67 61 69 6e 20 61 63 63 65 73 73 20 74 6f  n gain access to
29120 20 74 68 69 73 20 70 6f 69 6e 74 65 72 20 75 73   this pointer us
29130 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 75 73 65  ing [sqlite3_use
29140 72 5f 64 61 74 61 28 29 5d 2e 0a 2a 2a 0a 2a 2a  r_data()]..**.**
29150 20 54 68 65 20 73 65 76 65 6e 74 68 2c 20 65 69   The seventh, ei
29160 67 68 74 68 20 61 6e 64 20 6e 69 6e 74 68 20 70  ghth and ninth p
29170 61 72 61 6d 65 74 65 72 73 2c 20 78 46 75 6e 63  arameters, xFunc
29180 2c 20 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e  , xStep and xFin
29190 61 6c 2c 20 61 72 65 0a 2a 2a 20 70 6f 69 6e 74  al, are.** point
291a0 65 72 73 20 74 6f 20 43 2d 6c 61 6e 67 75 61 67  ers to C-languag
291b0 65 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  e functions that
291c0 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 53   implement the S
291d0 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a  QL function or.*
291e0 2a 20 61 67 67 72 65 67 61 74 65 2e 20 41 20 73  * aggregate. A s
291f0 63 61 6c 61 72 20 53 51 4c 20 66 75 6e 63 74 69  calar SQL functi
29200 6f 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 69  on requires an i
29210 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
29220 20 74 68 65 20 78 46 75 6e 63 0a 2a 2a 20 63 61   the xFunc.** ca
29230 6c 6c 62 61 63 6b 20 6f 6e 6c 79 2c 20 4e 55 4c  llback only, NUL
29240 4c 20 70 6f 69 6e 74 65 72 73 20 73 68 6f 75 6c  L pointers shoul
29250 64 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74  d be passed as t
29260 68 65 20 78 53 74 65 70 20 61 6e 64 20 78 46 69  he xStep and xFi
29270 6e 61 6c 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  nal.** parameter
29280 73 2e 20 41 6e 20 61 67 67 72 65 67 61 74 65 20  s. An aggregate 
29290 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 72 65 71  SQL function req
292a0 75 69 72 65 73 20 61 6e 20 69 6d 70 6c 65 6d 65  uires an impleme
292b0 6e 74 61 74 69 6f 6e 20 6f 66 20 78 53 74 65 70  ntation of xStep
292c0 0a 2a 2a 20 61 6e 64 20 78 46 69 6e 61 6c 20 61  .** and xFinal a
292d0 6e 64 20 4e 55 4c 4c 20 73 68 6f 75 6c 64 20 62  nd NULL should b
292e0 65 20 70 61 73 73 65 64 20 66 6f 72 20 78 46 75  e passed for xFu
292f0 6e 63 2e 20 54 6f 20 64 65 6c 65 74 65 20 61 6e  nc. To delete an
29300 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 53 51 4c   existing.** SQL
29310 20 66 75 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67   function or agg
29320 72 65 67 61 74 65 2c 20 70 61 73 73 20 4e 55 4c  regate, pass NUL
29330 4c 20 66 6f 72 20 61 6c 6c 20 74 68 72 65 65 20  L for all three 
29340 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 62 61 63  function callbac
29350 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20  ks..**.** It is 
29360 70 65 72 6d 69 74 74 65 64 20 74 6f 20 72 65 67  permitted to reg
29370 69 73 74 65 72 20 6d 75 6c 74 69 70 6c 65 20 69  ister multiple i
29380 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f  mplementations o
29390 66 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 66 75  f the same.** fu
293a0 6e 63 74 69 6f 6e 73 20 77 69 74 68 20 74 68 65  nctions with the
293b0 20 73 61 6d 65 20 6e 61 6d 65 20 62 75 74 20 77   same name but w
293c0 69 74 68 20 65 69 74 68 65 72 20 64 69 66 66 65  ith either diffe
293d0 72 69 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 0a  ring numbers of.
293e0 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20  ** arguments or 
293f0 64 69 66 66 65 72 69 6e 67 20 70 72 65 66 65 72  differing prefer
29400 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  red text encodin
29410 67 73 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c  gs.  SQLite will
29420 20 75 73 65 0a 2a 2a 20 74 68 65 20 69 6d 70 6c   use.** the impl
29430 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74 20  ementation that 
29440 6d 6f 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61 74  most closely mat
29450 63 68 65 73 20 74 68 65 20 77 61 79 20 69 6e 20  ches the way in 
29460 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 53 51 4c  which the.** SQL
29470 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
29480 64 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20 69  d.  A function i
29490 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69  mplementation wi
294a0 74 68 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69 76  th a non-negativ
294b0 65 0a 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d 65  e.** nArg parame
294c0 74 65 72 20 69 73 20 61 20 62 65 74 74 65 72 20  ter is a better 
294d0 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e  match than a fun
294e0 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61  ction implementa
294f0 74 69 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20 6e  tion with.** a n
29500 65 67 61 74 69 76 65 20 6e 41 72 67 2e 20 20 41  egative nArg.  A
29510 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65 20   function where 
29520 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74 65  the preferred te
29530 78 74 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 6d  xt encoding.** m
29540 61 74 63 68 65 73 20 74 68 65 20 64 61 74 61 62  atches the datab
29550 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20  ase encoding is 
29560 61 20 62 65 74 74 65 72 0a 2a 2a 20 6d 61 74 63  a better.** matc
29570 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69 6f  h than a functio
29580 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63 6f  n where the enco
29590 64 69 6e 67 20 69 73 20 64 69 66 66 65 72 65 6e  ding is differen
295a0 74 2e 20 20 0a 2a 2a 20 41 20 66 75 6e 63 74 69  t.  .** A functi
295b0 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63  on where the enc
295c0 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63 65  oding difference
295d0 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46 31   is between UTF1
295e0 36 6c 65 20 61 6e 64 20 55 54 46 31 36 62 65 0a  6le and UTF16be.
295f0 2a 2a 20 69 73 20 61 20 63 6c 6f 73 65 72 20 6d  ** is a closer m
29600 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63  atch than a func
29610 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65  tion where the e
29620 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e  ncoding differen
29630 63 65 20 69 73 0a 2a 2a 20 62 65 74 77 65 65 6e  ce is.** between
29640 20 55 54 46 38 20 61 6e 64 20 55 54 46 31 36 2e   UTF8 and UTF16.
29650 0a 2a 2a 0a 2a 2a 20 42 75 69 6c 74 2d 69 6e 20  .**.** Built-in 
29660 66 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62 65  functions may be
29670 20 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20 6e   overloaded by n
29680 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  ew application-d
29690 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73  efined functions
296a0 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61  ..** The first a
296b0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
296c0 65 64 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68  ed function with
296d0 20 61 20 67 69 76 65 6e 20 6e 61 6d 65 20 6f 76   a given name ov
296e0 65 72 72 69 64 65 73 20 61 6c 6c 0a 2a 2a 20 62  errides all.** b
296f0 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e  uilt-in function
29700 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 5b 64  s in the same [d
29710 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
29720 6f 6e 5d 20 77 69 74 68 20 74 68 65 20 73 61 6d  on] with the sam
29730 65 20 6e 61 6d 65 2e 0a 2a 2a 20 53 75 62 73 65  e name..** Subse
29740 71 75 65 6e 74 20 61 70 70 6c 69 63 61 74 69 6f  quent applicatio
29750 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  n-defined functi
29760 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20  ons of the same 
29770 6e 61 6d 65 20 6f 6e 6c 79 20 6f 76 65 72 72 69  name only overri
29780 64 65 20 0a 2a 2a 20 70 72 69 6f 72 20 61 70 70  de .** prior app
29790 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
297a0 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20   functions that 
297b0 61 72 65 20 61 6e 20 65 78 61 63 74 20 6d 61 74  are an exact mat
297c0 63 68 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6e 75  ch for the.** nu
297d0 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74 65  mber of paramete
297e0 72 73 20 61 6e 64 20 70 72 65 66 65 72 72 65 64  rs and preferred
297f0 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a   encoding..**.**
29800 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d   An application-
29810 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
29820 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74 6f   is permitted to
29830 20 63 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20 53   call other.** S
29840 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73  QLite interfaces
29850 2e 20 20 48 6f 77 65 76 65 72 2c 20 73 75 63 68  .  However, such
29860 20 63 61 6c 6c 73 20 6d 75 73 74 20 6e 6f 74 0a   calls must not.
29870 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 64 61 74  ** close the dat
29880 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
29890 20 6e 6f 72 20 66 69 6e 61 6c 69 7a 65 20 6f 72   nor finalize or
298a0 20 72 65 73 65 74 20 74 68 65 20 70 72 65 70 61   reset the prepa
298b0 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  red.** statement
298c0 20 69 6e 20 77 68 69 63 68 20 74 68 65 20 66 75   in which the fu
298d0 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e  nction is runnin
298e0 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65  g..**.** Require
298f0 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 31 30  ments:.** [H1610
29900 33 5d 20 5b 48 31 36 31 30 36 5d 20 5b 48 31 36  3] [H16106] [H16
29910 31 30 39 5d 20 5b 48 31 36 31 31 32 5d 20 5b 48  109] [H16112] [H
29920 31 36 31 31 38 5d 20 5b 48 31 36 31 32 31 5d 20  16118] [H16121] 
29930 5b 48 31 36 31 32 37 5d 0a 2a 2a 20 5b 48 31 36  [H16127].** [H16
29940 31 33 30 5d 20 5b 48 31 36 31 33 33 5d 20 5b 48  130] [H16133] [H
29950 31 36 31 33 36 5d 20 5b 48 31 36 31 33 39 5d 20  16136] [H16139] 
29960 5b 48 31 36 31 34 32 5d 0a 2a 2f 0a 53 51 4c 49  [H16142].*/.SQLI
29970 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
29980 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
29990 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  on(.  sqlite3 *d
299a0 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  b,.  const char 
299b0 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a  *zFunctionName,.
299c0 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e    int nArg,.  in
299d0 74 20 65 54 65 78 74 52 65 70 2c 0a 20 20 76 6f  t eTextRep,.  vo
299e0 69 64 20 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64  id *pApp,.  void
299f0 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65   (*xFunc)(sqlite
29a00 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
29a10 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c  qlite3_value**),
29a20 0a 20 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29  .  void (*xStep)
29a30 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
29a40 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
29a50 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28  lue**),.  void (
29a60 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33  *xFinal)(sqlite3
29a70 5f 63 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a 53 51  _context*).);.SQ
29a80 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
29a90 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
29aa0 74 69 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74 65  tion16(.  sqlite
29ab0 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 76  3 *db,.  const v
29ac0 6f 69 64 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61  oid *zFunctionNa
29ad0 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a  me,.  int nArg,.
29ae0 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 0a    int eTextRep,.
29af0 20 20 76 6f 69 64 20 2a 70 41 70 70 2c 0a 20 20    void *pApp,.  
29b00 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71  void (*xFunc)(sq
29b10 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
29b20 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
29b30 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53  **),.  void (*xS
29b40 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  tep)(sqlite3_con
29b50 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
29b60 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f  3_value**),.  vo
29b70 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c  id (*xFinal)(sql
29b80 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29  ite3_context*).)
29b90 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
29ba0 46 3a 20 54 65 78 74 20 45 6e 63 6f 64 69 6e 67  F: Text Encoding
29bb0 73 20 7b 48 31 30 32 36 37 7d 20 3c 53 35 30 32  s {H10267} <S502
29bc0 30 30 3e 20 3c 48 31 36 31 30 30 3e 0a 2a 2a 0a  00> <H16100>.**.
29bd0 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e  ** These constan
29be0 74 20 64 65 66 69 6e 65 20 69 6e 74 65 67 65 72  t define integer
29bf0 20 63 6f 64 65 73 20 74 68 61 74 20 72 65 70 72   codes that repr
29c00 65 73 65 6e 74 20 74 68 65 20 76 61 72 69 6f 75  esent the variou
29c10 73 0a 2a 2a 20 74 65 78 74 20 65 6e 63 6f 64 69  s.** text encodi
29c20 6e 67 73 20 73 75 70 70 6f 72 74 65 64 20 62 79  ngs supported by
29c30 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 23 64 65 66   SQLite..*/.#def
29c40 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 38 20  ine SQLITE_UTF8 
29c50 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66            1.#def
29c60 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36  ine SQLITE_UTF16
29c70 4c 45 20 20 20 20 20 20 20 20 32 0a 23 64 65 66  LE        2.#def
29c80 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36  ine SQLITE_UTF16
29c90 42 45 20 20 20 20 20 20 20 20 33 0a 23 64 65 66  BE        3.#def
29ca0 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36  ine SQLITE_UTF16
29cb0 20 20 20 20 20 20 20 20 20 20 34 20 20 20 20 2f            4    /
29cc0 2a 20 55 73 65 20 6e 61 74 69 76 65 20 62 79 74  * Use native byt
29cd0 65 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69  e order */.#defi
29ce0 6e 65 20 53 51 4c 49 54 45 5f 41 4e 59 20 20 20  ne SQLITE_ANY   
29cf0 20 20 20 20 20 20 20 20 20 35 20 20 20 20 2f 2a           5    /*
29d00 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
29d10 66 75 6e 63 74 69 6f 6e 20 6f 6e 6c 79 20 2a 2f  function only */
29d20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
29d30 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 20 20 38  UTF16_ALIGNED  8
29d40 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 63      /* sqlite3_c
29d50 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 20  reate_collation 
29d60 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  only */../*.** C
29d70 41 50 49 33 52 45 46 3a 20 44 65 70 72 65 63 61  API3REF: Depreca
29d80 74 65 64 20 46 75 6e 63 74 69 6f 6e 73 0a 2a 2a  ted Functions.**
29d90 20 44 45 50 52 45 43 41 54 45 44 0a 2a 2a 0a 2a   DEPRECATED.**.*
29da0 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f 6e  * These function
29db0 73 20 61 72 65 20 5b 64 65 70 72 65 63 61 74 65  s are [deprecate
29dc0 64 5d 2e 20 20 49 6e 20 6f 72 64 65 72 20 74 6f  d].  In order to
29dd0 20 6d 61 69 6e 74 61 69 6e 0a 2a 2a 20 62 61 63   maintain.** bac
29de0 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62 69  kwards compatibi
29df0 6c 69 74 79 20 77 69 74 68 20 6f 6c 64 65 72 20  lity with older 
29e00 63 6f 64 65 2c 20 74 68 65 73 65 20 66 75 6e 63  code, these func
29e10 74 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 0a  tions continue .
29e20 2a 2a 20 74 6f 20 62 65 20 73 75 70 70 6f 72 74  ** to be support
29e30 65 64 2e 20 20 48 6f 77 65 76 65 72 2c 20 6e 65  ed.  However, ne
29e40 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 73  w applications s
29e50 68 6f 75 6c 64 20 61 76 6f 69 64 0a 2a 2a 20 74  hould avoid.** t
29e60 68 65 20 75 73 65 20 6f 66 20 74 68 65 73 65 20  he use of these 
29e70 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 6f 20 68  functions.  To h
29e80 65 6c 70 20 65 6e 63 6f 75 72 61 67 65 20 70 65  elp encourage pe
29e90 6f 70 6c 65 20 74 6f 20 61 76 6f 69 64 0a 2a 2a  ople to avoid.**
29ea0 20 75 73 69 6e 67 20 74 68 65 73 65 20 66 75 6e   using these fun
29eb0 63 74 69 6f 6e 73 2c 20 77 65 20 61 72 65 20 6e  ctions, we are n
29ec0 6f 74 20 67 6f 69 6e 67 20 74 6f 20 74 65 6c 6c  ot going to tell
29ed0 20 79 6f 75 20 77 68 61 74 20 74 68 65 79 20 64   you what they d
29ee0 6f 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  o..*/.#ifndef SQ
29ef0 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
29f00 41 54 45 44 0a 53 51 4c 49 54 45 5f 41 50 49 20  ATED.SQLITE_API 
29f10 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45  SQLITE_DEPRECATE
29f20 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 67  D int sqlite3_ag
29f30 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28 73 71  gregate_count(sq
29f40 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b  lite3_context*);
29f50 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49  .SQLITE_API SQLI
29f60 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e  TE_DEPRECATED in
29f70 74 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65  t sqlite3_expire
29f80 64 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  d(sqlite3_stmt*)
29f90 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c  ;.SQLITE_API SQL
29fa0 49 54 45 5f 44 45 50 52 45 43 41 54 45 44 20 69  ITE_DEPRECATED i
29fb0 6e 74 20 73 71 6c 69 74 65 33 5f 74 72 61 6e 73  nt sqlite3_trans
29fc0 66 65 72 5f 62 69 6e 64 69 6e 67 73 28 73 71 6c  fer_bindings(sql
29fd0 69 74 65 33 5f 73 74 6d 74 2a 2c 20 73 71 6c 69  ite3_stmt*, sqli
29fe0 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49  te3_stmt*);.SQLI
29ff0 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45  TE_API SQLITE_DE
2a000 50 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c  PRECATED int sql
2a010 69 74 65 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f  ite3_global_reco
2a020 76 65 72 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  ver(void);.SQLIT
2a030 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50  E_API SQLITE_DEP
2a040 52 45 43 41 54 45 44 20 76 6f 69 64 20 73 71 6c  RECATED void sql
2a050 69 74 65 33 5f 74 68 72 65 61 64 5f 63 6c 65 61  ite3_thread_clea
2a060 6e 75 70 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  nup(void);.SQLIT
2a070 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50  E_API SQLITE_DEP
2a080 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69  RECATED int sqli
2a090 74 65 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d  te3_memory_alarm
2a0a0 28 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c 73  (void(*)(void*,s
2a0b0 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74  qlite3_int64,int
2a0c0 29 2c 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f  ),void*,sqlite3_
2a0d0 69 6e 74 36 34 29 3b 0a 23 65 6e 64 69 66 0a 0a  int64);.#endif..
2a0e0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
2a0f0 4f 62 74 61 69 6e 69 6e 67 20 53 51 4c 20 46 75  Obtaining SQL Fu
2a100 6e 63 74 69 6f 6e 20 50 61 72 61 6d 65 74 65 72  nction Parameter
2a110 20 56 61 6c 75 65 73 20 7b 48 31 35 31 30 30 7d   Values {H15100}
2a120 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S20200>.**.** 
2a130 54 68 65 20 43 2d 6c 61 6e 67 75 61 67 65 20 69  The C-language i
2a140 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
2a150 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61   SQL functions a
2a160 6e 64 20 61 67 67 72 65 67 61 74 65 73 20 75 73  nd aggregates us
2a170 65 73 0a 2a 2a 20 74 68 69 73 20 73 65 74 20 6f  es.** this set o
2a180 66 20 69 6e 74 65 72 66 61 63 65 20 72 6f 75 74  f interface rout
2a190 69 6e 65 73 20 74 6f 20 61 63 63 65 73 73 20 74  ines to access t
2a1a0 68 65 20 70 61 72 61 6d 65 74 65 72 20 76 61 6c  he parameter val
2a1b0 75 65 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 66 75  ues on.** the fu
2a1c0 6e 63 74 69 6f 6e 20 6f 72 20 61 67 67 72 65 67  nction or aggreg
2a1d0 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  ate..**.** The x
2a1e0 46 75 6e 63 20 28 66 6f 72 20 73 63 61 6c 61 72  Func (for scalar
2a1f0 20 66 75 6e 63 74 69 6f 6e 73 29 20 6f 72 20 78   functions) or x
2a200 53 74 65 70 20 28 66 6f 72 20 61 67 67 72 65 67  Step (for aggreg
2a210 61 74 65 73 29 20 70 61 72 61 6d 65 74 65 72 73  ates) parameters
2a220 0a 2a 2a 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  .** to [sqlite3_
2a230 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
2a240 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  )] and [sqlite3_
2a250 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31  create_function1
2a260 36 28 29 5d 0a 2a 2a 20 64 65 66 69 6e 65 20 63  6()].** define c
2a270 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 69 6d  allbacks that im
2a280 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20  plement the SQL 
2a290 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67  functions and ag
2a2a0 67 72 65 67 61 74 65 73 2e 0a 2a 2a 20 54 68 65  gregates..** The
2a2b0 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74   4th parameter t
2a2c0 6f 20 74 68 65 73 65 20 63 61 6c 6c 62 61 63 6b  o these callback
2a2d0 73 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66  s is an array of
2a2e0 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20   pointers to.** 
2a2f0 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  [protected sqlit
2a300 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
2a310 73 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65  s.  There is one
2a320 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d   [sqlite3_value]
2a330 20 6f 62 6a 65 63 74 20 66 6f 72 0a 2a 2a 20 65   object for.** e
2a340 61 63 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f  ach parameter to
2a350 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
2a360 6e 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e  n.  These routin
2a370 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 0a 2a  es are used to.*
2a380 2a 20 65 78 74 72 61 63 74 20 76 61 6c 75 65 73  * extract values
2a390 20 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74   from the [sqlit
2a3a0 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
2a3b0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  s..**.** These r
2a3c0 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6f 6e 6c  outines work onl
2a3d0 79 20 77 69 74 68 20 5b 70 72 6f 74 65 63 74 65  y with [protecte
2a3e0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d  d sqlite3_value]
2a3f0 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 41 6e 79   objects..** Any
2a400 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73 65 20   attempt to use 
2a410 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f  these routines o
2a420 6e 20 61 6e 20 5b 75 6e 70 72 6f 74 65 63 74 65  n an [unprotecte
2a430 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d  d sqlite3_value]
2a440 0a 2a 2a 20 6f 62 6a 65 63 74 20 72 65 73 75 6c  .** object resul
2a450 74 73 20 69 6e 20 75 6e 64 65 66 69 6e 65 64 20  ts in undefined 
2a460 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20  behavior..**.** 
2a470 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 77  These routines w
2a480 6f 72 6b 20 6a 75 73 74 20 6c 69 6b 65 20 74 68  ork just like th
2a490 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
2a4a0 5b 63 6f 6c 75 6d 6e 20 61 63 63 65 73 73 20 66  [column access f
2a4b0 75 6e 63 74 69 6f 6e 73 5d 0a 2a 2a 20 65 78 63  unctions].** exc
2a4c0 65 70 74 20 74 68 61 74 20 20 74 68 65 73 65 20  ept that  these 
2a4d0 72 6f 75 74 69 6e 65 73 20 74 61 6b 65 20 61 20  routines take a 
2a4e0 73 69 6e 67 6c 65 20 5b 70 72 6f 74 65 63 74 65  single [protecte
2a4f0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d  d sqlite3_value]
2a500 20 6f 62 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e 74   object.** point
2a510 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 20  er instead of a 
2a520 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 5d 20  [sqlite3_stmt*] 
2a530 70 6f 69 6e 74 65 72 20 61 6e 64 20 61 6e 20 69  pointer and an i
2a540 6e 74 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e 75  nteger column nu
2a550 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  mber..**.** The 
2a560 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2a570 78 74 31 36 28 29 20 69 6e 74 65 72 66 61 63 65  xt16() interface
2a580 20 65 78 74 72 61 63 74 73 20 61 20 55 54 46 2d   extracts a UTF-
2a590 31 36 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e 20  16 string.** in 
2a5a0 74 68 65 20 6e 61 74 69 76 65 20 62 79 74 65 2d  the native byte-
2a5b0 6f 72 64 65 72 20 6f 66 20 74 68 65 20 68 6f 73  order of the hos
2a5c0 74 20 6d 61 63 68 69 6e 65 2e 20 20 54 68 65 0a  t machine.  The.
2a5d0 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ** sqlite3_value
2a5e0 5f 74 65 78 74 31 36 62 65 28 29 20 61 6e 64 20  _text16be() and 
2a5f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2a600 78 74 31 36 6c 65 28 29 20 69 6e 74 65 72 66 61  xt16le() interfa
2a610 63 65 73 0a 2a 2a 20 65 78 74 72 61 63 74 20 55  ces.** extract U
2a620 54 46 2d 31 36 20 73 74 72 69 6e 67 73 20 61 73  TF-16 strings as
2a630 20 62 69 67 2d 65 6e 64 69 61 6e 20 61 6e 64 20   big-endian and 
2a640 6c 69 74 74 6c 65 2d 65 6e 64 69 61 6e 20 72 65  little-endian re
2a650 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a  spectively..**.*
2a660 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61  * The sqlite3_va
2a670 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65  lue_numeric_type
2a680 28 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 74  () interface att
2a690 65 6d 70 74 73 20 74 6f 20 61 70 70 6c 79 0a 2a  empts to apply.*
2a6a0 2a 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69  * numeric affini
2a6b0 74 79 20 74 6f 20 74 68 65 20 76 61 6c 75 65 2e  ty to the value.
2a6c0 20 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61    This means tha
2a6d0 74 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 0a  t an attempt is.
2a6e0 2a 2a 20 6d 61 64 65 20 74 6f 20 63 6f 6e 76 65  ** made to conve
2a6f0 72 74 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20  rt the value to 
2a700 61 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 66 6c  an integer or fl
2a710 6f 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20 20 49  oating point.  I
2a720 66 0a 2a 2a 20 73 75 63 68 20 61 20 63 6f 6e 76  f.** such a conv
2a730 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62  ersion is possib
2a740 6c 65 20 77 69 74 68 6f 75 74 20 6c 6f 73 73 20  le without loss 
2a750 6f 66 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 28  of information (
2a760 69 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64  in other.** word
2a770 73 2c 20 69 66 20 74 68 65 20 76 61 6c 75 65 20  s, if the value 
2a780 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 61 74  is a string that
2a790 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 61 20 6e 75   looks like a nu
2a7a0 6d 62 65 72 29 0a 2a 2a 20 74 68 65 6e 20 74 68  mber).** then th
2a7b0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20  e conversion is 
2a7c0 70 65 72 66 6f 72 6d 65 64 2e 20 20 4f 74 68 65  performed.  Othe
2a7d0 72 77 69 73 65 20 6e 6f 20 63 6f 6e 76 65 72 73  rwise no convers
2a7e0 69 6f 6e 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 54  ion occurs..** T
2a7f0 68 65 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47  he [SQLITE_INTEG
2a800 45 52 20 7c 20 64 61 74 61 74 79 70 65 5d 20 61  ER | datatype] a
2a810 66 74 65 72 20 63 6f 6e 76 65 72 73 69 6f 6e 20  fter conversion 
2a820 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
2a830 2a 2a 20 50 6c 65 61 73 65 20 70 61 79 20 70 61  ** Please pay pa
2a840 72 74 69 63 75 6c 61 72 20 61 74 74 65 6e 74 69  rticular attenti
2a850 6f 6e 20 74 6f 20 74 68 65 20 66 61 63 74 20 74  on to the fact t
2a860 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20  hat the pointer 
2a870 72 65 74 75 72 6e 65 64 0a 2a 2a 20 66 72 6f 6d  returned.** from
2a880 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   [sqlite3_value_
2a890 62 6c 6f 62 28 29 5d 2c 20 5b 73 71 6c 69 74 65  blob()], [sqlite
2a8a0 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c  3_value_text()],
2a8b0 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   or.** [sqlite3_
2a8c0 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29 5d 20  value_text16()] 
2a8d0 63 61 6e 20 62 65 20 69 6e 76 61 6c 69 64 61 74  can be invalidat
2a8e0 65 64 20 62 79 20 61 20 73 75 62 73 65 71 75 65  ed by a subseque
2a8f0 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73  nt call to.** [s
2a900 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
2a910 65 73 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  es()], [sqlite3_
2a920 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 29 5d  value_bytes16()]
2a930 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  , [sqlite3_value
2a940 5f 74 65 78 74 28 29 5d 2c 0a 2a 2a 20 6f 72 20  _text()],.** or 
2a950 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  [sqlite3_value_t
2a960 65 78 74 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  ext16()]..**.** 
2a970 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6d  These routines m
2a980 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72  ust be called fr
2a990 6f 6d 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  om the same thre
2a9a0 61 64 20 61 73 0a 2a 2a 20 74 68 65 20 53 51 4c  ad as.** the SQL
2a9b0 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 73   function that s
2a9c0 75 70 70 6c 69 65 64 20 74 68 65 20 5b 73 71 6c  upplied the [sql
2a9d0 69 74 65 33 5f 76 61 6c 75 65 2a 5d 20 70 61 72  ite3_value*] par
2a9e0 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 52  ameters..**.** R
2a9f0 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
2aa00 5b 48 31 35 31 30 33 5d 20 5b 48 31 35 31 30 36  [H15103] [H15106
2aa10 5d 20 5b 48 31 35 31 30 39 5d 20 5b 48 31 35 31  ] [H15109] [H151
2aa20 31 32 5d 20 5b 48 31 35 31 31 35 5d 20 5b 48 31  12] [H15115] [H1
2aa30 35 31 31 38 5d 20 5b 48 31 35 31 32 31 5d 20 5b  5118] [H15121] [
2aa40 48 31 35 31 32 34 5d 0a 2a 2a 20 5b 48 31 35 31  H15124].** [H151
2aa50 32 37 5d 20 5b 48 31 35 31 33 30 5d 20 5b 48 31  27] [H15130] [H1
2aa60 35 31 33 33 5d 20 5b 48 31 35 31 33 36 5d 0a 2a  5133] [H15136].*
2aa70 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
2aa80 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
2aa90 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 73 71 6c 69  _value_blob(sqli
2aaa0 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c  te3_value*);.SQL
2aab0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2aac0 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
2aad0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
2aae0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2aaf0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
2ab00 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 76 61  tes16(sqlite3_va
2ab10 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  lue*);.SQLITE_AP
2ab20 49 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33  I double sqlite3
2ab30 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 73 71  _value_double(sq
2ab40 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53  lite3_value*);.S
2ab50 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2ab60 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28  lite3_value_int(
2ab70 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
2ab80 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
2ab90 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  te3_int64 sqlite
2aba0 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 73 71  3_value_int64(sq
2abb0 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53  lite3_value*);.S
2abc0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
2abd0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 73  unsigned char *s
2abe0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2abf0 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t(sqlite3_value*
2ac00 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  );.SQLITE_API co
2ac10 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
2ac20 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73  3_value_text16(s
2ac30 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
2ac40 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
2ac50 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76   void *sqlite3_v
2ac60 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 73 71  alue_text16le(sq
2ac70 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53  lite3_value*);.S
2ac80 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
2ac90 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 76 61  void *sqlite3_va
2aca0 6c 75 65 5f 74 65 78 74 31 36 62 65 28 73 71 6c  lue_text16be(sql
2acb0 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51  ite3_value*);.SQ
2acc0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2acd0 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
2ace0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
2acf0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2ad00 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75  sqlite3_value_nu
2ad10 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74  meric_type(sqlit
2ad20 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 0a 2f 2a 0a  e3_value*);../*.
2ad30 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 62 74  ** CAPI3REF: Obt
2ad40 61 69 6e 20 41 67 67 72 65 67 61 74 65 20 46 75  ain Aggregate Fu
2ad50 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20 7b  nction Context {
2ad60 48 31 36 32 31 30 7d 20 3c 53 32 30 32 30 30 3e  H16210} <S20200>
2ad70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65  .**.** The imple
2ad80 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 67 67  mentation of agg
2ad90 72 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74  regate SQL funct
2ada0 69 6f 6e 73 20 75 73 65 20 74 68 69 73 20 72 6f  ions use this ro
2adb0 75 74 69 6e 65 20 74 6f 20 61 6c 6c 6f 63 61 74  utine to allocat
2adc0 65 0a 2a 2a 20 61 20 73 74 72 75 63 74 75 72 65  e.** a structure
2add0 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65   for storing the
2ade0 69 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20  ir state..**.** 
2adf0 54 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74  The first time t
2ae00 68 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65  he sqlite3_aggre
2ae10 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 29 20 72  gate_context() r
2ae20 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
2ae30 20 66 6f 72 20 61 0a 2a 2a 20 70 61 72 74 69 63   for a.** partic
2ae40 75 6c 61 72 20 61 67 67 72 65 67 61 74 65 2c 20  ular aggregate, 
2ae50 53 51 4c 69 74 65 20 61 6c 6c 6f 63 61 74 65 73  SQLite allocates
2ae60 20 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72   nBytes of memor
2ae70 79 2c 20 7a 65 72 6f 65 73 20 6f 75 74 20 74 68  y, zeroes out th
2ae80 61 74 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 61 6e  at.** memory, an
2ae90 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  d returns a poin
2aea0 74 65 72 20 74 6f 20 69 74 2e 20 4f 6e 20 73 65  ter to it. On se
2aeb0 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75  cond and subsequ
2aec0 65 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20  ent calls to.** 
2aed0 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
2aee0 65 5f 63 6f 6e 74 65 78 74 28 29 20 66 6f 72 20  e_context() for 
2aef0 74 68 65 20 73 61 6d 65 20 61 67 67 72 65 67 61  the same aggrega
2af00 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6e 64 65  te function inde
2af10 78 2c 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 62  x,.** the same b
2af20 75 66 66 65 72 20 69 73 20 72 65 74 75 72 6e 65  uffer is returne
2af30 64 2e 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74  d. The implement
2af40 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 61 67 67  ation of the agg
2af50 72 65 67 61 74 65 20 63 61 6e 20 75 73 65 0a 2a  regate can use.*
2af60 2a 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 62  * the returned b
2af70 75 66 66 65 72 20 74 6f 20 61 63 63 75 6d 75 6c  uffer to accumul
2af80 61 74 65 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20  ate data..**.** 
2af90 53 51 4c 69 74 65 20 61 75 74 6f 6d 61 74 69 63  SQLite automatic
2afa0 61 6c 6c 79 20 66 72 65 65 73 20 74 68 65 20 61  ally frees the a
2afb0 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20  llocated buffer 
2afc0 77 68 65 6e 20 74 68 65 20 61 67 67 72 65 67 61  when the aggrega
2afd0 74 65 0a 2a 2a 20 71 75 65 72 79 20 63 6f 6e 63  te.** query conc
2afe0 6c 75 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ludes..**.** The
2aff0 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
2b000 20 73 68 6f 75 6c 64 20 62 65 20 61 20 63 6f 70   should be a cop
2b010 79 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c  y of the.** [sql
2b020 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 7c 20 53  ite3_context | S
2b030 51 4c 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e 74  QL function cont
2b040 65 78 74 5d 20 74 68 61 74 20 69 73 20 74 68 65  ext] that is the
2b050 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
2b060 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62  .** to the callb
2b070 61 63 6b 20 72 6f 75 74 69 6e 65 20 74 68 61 74  ack routine that
2b080 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
2b090 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
2b0a0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  on..**.** This r
2b0b0 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63  outine must be c
2b0c0 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 73  alled from the s
2b0d0 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20 77 68  ame thread in wh
2b0e0 69 63 68 0a 2a 2a 20 74 68 65 20 61 67 67 72 65  ich.** the aggre
2b0f0 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69 6f  gate SQL functio
2b100 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2a  n is running..**
2b110 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
2b120 3a 0a 2a 2a 20 5b 48 31 36 32 31 31 5d 20 5b 48  :.** [H16211] [H
2b130 31 36 32 31 33 5d 20 5b 48 31 36 32 31 35 5d 20  16213] [H16215] 
2b140 5b 48 31 36 32 31 37 5d 0a 2a 2f 0a 53 51 4c 49  [H16217].*/.SQLI
2b150 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
2b160 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
2b170 6f 6e 74 65 78 74 28 73 71 6c 69 74 65 33 5f 63  ontext(sqlite3_c
2b180 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 6e 42 79  ontext*, int nBy
2b190 74 65 73 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  tes);../*.** CAP
2b1a0 49 33 52 45 46 3a 20 55 73 65 72 20 44 61 74 61  I3REF: User Data
2b1b0 20 46 6f 72 20 46 75 6e 63 74 69 6f 6e 73 20 7b   For Functions {
2b1c0 48 31 36 32 34 30 7d 20 3c 53 32 30 32 30 30 3e  H16240} <S20200>
2b1d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
2b1e0 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 69  e3_user_data() i
2b1f0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
2b200 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68   a copy of.** th
2b210 65 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 77  e pointer that w
2b220 61 73 20 74 68 65 20 70 55 73 65 72 44 61 74 61  as the pUserData
2b230 20 70 61 72 61 6d 65 74 65 72 20 28 74 68 65 20   parameter (the 
2b240 35 74 68 20 70 61 72 61 6d 65 74 65 72 29 0a 2a  5th parameter).*
2b250 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65  * of the [sqlite
2b260 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
2b270 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c  n()].** and [sql
2b280 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
2b290 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74 69 6e  tion16()] routin
2b2a0 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e 61 6c  es that original
2b2b0 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64  ly.** registered
2b2c0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
2b2d0 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f   defined functio
2b2e0 6e 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54  n. {END}.**.** T
2b2f0 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74  his routine must
2b300 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20   be called from 
2b310 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20  the same thread 
2b320 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20  in which.** the 
2b330 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
2b340 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  ned function is 
2b350 72 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52  running..**.** R
2b360 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20  equirements:.** 
2b370 5b 48 31 36 32 34 33 5d 0a 2a 2f 0a 53 51 4c 49  [H16243].*/.SQLI
2b380 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
2b390 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73  ite3_user_data(s
2b3a0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
2b3b0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
2b3c0 46 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e  F: Database Conn
2b3d0 65 63 74 69 6f 6e 20 46 6f 72 20 46 75 6e 63 74  ection For Funct
2b3e0 69 6f 6e 73 20 7b 48 31 36 32 35 30 7d 20 3c 53  ions {H16250} <S
2b3f0 36 30 36 30 30 3e 3c 53 32 30 32 30 30 3e 0a 2a  60600><S20200>.*
2b400 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
2b410 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
2b420 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  le() interface r
2b430 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66  eturns a copy of
2b440 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20  .** the pointer 
2b450 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  to the [database
2b460 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 68   connection] (th
2b470 65 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 29  e 1st parameter)
2b480 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69  .** of the [sqli
2b490 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
2b4a0 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73  ion()].** and [s
2b4b0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
2b4c0 6e 63 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74  nction16()] rout
2b4d0 69 6e 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e  ines that origin
2b4e0 61 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72  ally.** register
2b4f0 65 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  ed the applicati
2b500 6f 6e 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74  on defined funct
2b510 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ion..**.** Requi
2b520 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36  rements:.** [H16
2b530 32 35 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  253].*/.SQLITE_A
2b540 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69  PI sqlite3 *sqli
2b550 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
2b560 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f  andle(sqlite3_co
2b570 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  ntext*);../*.** 
2b580 43 41 50 49 33 52 45 46 3a 20 46 75 6e 63 74 69  CAPI3REF: Functi
2b590 6f 6e 20 41 75 78 69 6c 69 61 72 79 20 44 61 74  on Auxiliary Dat
2b5a0 61 20 7b 48 31 36 32 37 30 7d 20 3c 53 32 30 32  a {H16270} <S202
2b5b0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  00>.**.** The fo
2b5c0 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e 63  llowing two func
2b5d0 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 75 73 65  tions may be use
2b5e0 64 20 62 79 20 73 63 61 6c 61 72 20 53 51 4c 20  d by scalar SQL 
2b5f0 66 75 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20  functions to.** 
2b600 61 73 73 6f 63 69 61 74 65 20 6d 65 74 61 64 61  associate metada
2b610 74 61 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74  ta with argument
2b620 20 76 61 6c 75 65 73 2e 20 49 66 20 74 68 65 20   values. If the 
2b630 73 61 6d 65 20 76 61 6c 75 65 20 69 73 20 70 61  same value is pa
2b640 73 73 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74 69  ssed to.** multi
2b650 70 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20  ple invocations 
2b660 6f 66 20 74 68 65 20 73 61 6d 65 20 53 51 4c 20  of the same SQL 
2b670 66 75 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67 20  function during 
2b680 71 75 65 72 79 20 65 78 65 63 75 74 69 6f 6e 2c  query execution,
2b690 20 75 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20 63   under.** some c
2b6a0 69 72 63 75 6d 73 74 61 6e 63 65 73 20 74 68 65  ircumstances the
2b6b0 20 61 73 73 6f 63 69 61 74 65 64 20 6d 65 74 61   associated meta
2b6c0 64 61 74 61 20 6d 61 79 20 62 65 20 70 72 65 73  data may be pres
2b6d0 65 72 76 65 64 2e 20 54 68 69 73 20 6d 61 79 0a  erved. This may.
2b6e0 2a 2a 20 62 65 20 75 73 65 64 2c 20 66 6f 72 20  ** be used, for 
2b6f0 65 78 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64 20  example, to add 
2b700 61 20 72 65 67 75 6c 61 72 2d 65 78 70 72 65 73  a regular-expres
2b710 73 69 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73 63  sion matching sc
2b720 61 6c 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  alar.** function
2b730 2e 20 54 68 65 20 63 6f 6d 70 69 6c 65 64 20 76  . The compiled v
2b740 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 72 65  ersion of the re
2b750 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e  gular expression
2b760 20 69 73 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a   is stored as.**
2b770 20 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63 69   metadata associ
2b780 61 74 65 64 20 77 69 74 68 20 74 68 65 20 53 51  ated with the SQ
2b790 4c 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61  L value passed a
2b7a0 73 20 74 68 65 20 72 65 67 75 6c 61 72 20 65 78  s the regular ex
2b7b0 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74 74  pression.** patt
2b7c0 65 72 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69 6c  ern.  The compil
2b7d0 65 64 20 72 65 67 75 6c 61 72 20 65 78 70 72 65  ed regular expre
2b7e0 73 73 69 6f 6e 20 63 61 6e 20 62 65 20 72 65 75  ssion can be reu
2b7f0 73 65 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 0a  sed on multiple.
2b800 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6f  ** invocations o
2b810 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74  f the same funct
2b820 69 6f 6e 20 73 6f 20 74 68 61 74 20 74 68 65 20  ion so that the 
2b830 6f 72 69 67 69 6e 61 6c 20 70 61 74 74 65 72 6e  original pattern
2b840 20 73 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73 20   string.** does 
2b850 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 72  not need to be r
2b860 65 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61 63  ecompiled on eac
2b870 68 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a 2a  h invocation..**
2b880 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2b890 67 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e  get_auxdata() in
2b8a0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
2b8b0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
2b8c0 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 61 73 73   metadata.** ass
2b8d0 6f 63 69 61 74 65 64 20 62 79 20 74 68 65 20 73  ociated by the s
2b8e0 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61  qlite3_set_auxda
2b8f0 74 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 77 69  ta() function wi
2b900 74 68 20 74 68 65 20 4e 74 68 20 61 72 67 75 6d  th the Nth argum
2b910 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f 20  ent.** value to 
2b920 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  the application-
2b930 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
2b940 2e 20 49 66 20 6e 6f 20 6d 65 74 61 64 61 74 61  . If no metadata
2b950 20 68 61 73 20 62 65 65 6e 20 65 76 65 72 0a 2a   has been ever.*
2b960 2a 20 62 65 65 6e 20 73 65 74 20 66 6f 72 20 74  * been set for t
2b970 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e 74 20  he Nth argument 
2b980 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2c  of the function,
2b990 20 6f 72 20 69 66 20 74 68 65 20 63 6f 72 72 65   or if the corre
2b9a0 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e 63  sponding.** func
2b9b0 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 68  tion parameter h
2b9c0 61 73 20 63 68 61 6e 67 65 64 20 73 69 6e 63 65  as changed since
2b9d0 20 74 68 65 20 6d 65 74 61 2d 64 61 74 61 20 77   the meta-data w
2b9e0 61 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20  as set,.** then 
2b9f0 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78 64  sqlite3_get_auxd
2ba00 61 74 61 28 29 20 72 65 74 75 72 6e 73 20 61 20  ata() returns a 
2ba10 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a  NULL pointer..**
2ba20 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2ba30 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 69 6e  set_auxdata() in
2ba40 74 65 72 66 61 63 65 20 73 61 76 65 73 20 74 68  terface saves th
2ba50 65 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70 6f  e metadata.** po
2ba60 69 6e 74 65 64 20 74 6f 20 62 79 20 69 74 73 20  inted to by its 
2ba70 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 61 73  3rd parameter as
2ba80 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 66 6f   the metadata fo
2ba90 72 20 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61 72  r the N-th.** ar
2baa0 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 61 70  gument of the ap
2bab0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
2bac0 64 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75 62  d function.  Sub
2bad0 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c 73  sequent.** calls
2bae0 20 74 6f 20 73 71 6c 69 74 65 33 5f 67 65 74 5f   to sqlite3_get_
2baf0 61 75 78 64 61 74 61 28 29 20 6d 69 67 68 74 20  auxdata() might 
2bb00 72 65 74 75 72 6e 20 74 68 69 73 20 64 61 74 61  return this data
2bb10 2c 20 69 66 20 69 74 20 68 61 73 0a 2a 2a 20 6e  , if it has.** n
2bb20 6f 74 20 62 65 65 6e 20 64 65 73 74 72 6f 79 65  ot been destroye
2bb30 64 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e  d..** If it is n
2bb40 6f 74 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 65 20  ot NULL, SQLite 
2bb50 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20  will invoke the 
2bb60 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 66 75  destructor.** fu
2bb70 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 62 79 20  nction given by 
2bb80 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65  the 4th paramete
2bb90 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 65 74  r to sqlite3_set
2bba0 5f 61 75 78 64 61 74 61 28 29 20 6f 6e 0a 2a 2a  _auxdata() on.**
2bbb0 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 77 68   the metadata wh
2bbc0 65 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  en the correspon
2bbd0 64 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 61  ding function pa
2bbe0 72 61 6d 65 74 65 72 20 63 68 61 6e 67 65 73 0a  rameter changes.
2bbf0 2a 2a 20 6f 72 20 77 68 65 6e 20 74 68 65 20 53  ** or when the S
2bc00 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d  QL statement com
2bc10 70 6c 65 74 65 73 2c 20 77 68 69 63 68 65 76 65  pletes, whicheve
2bc20 72 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 0a 2a  r comes first..*
2bc30 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73 20 66  *.** SQLite is f
2bc40 72 65 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20  ree to call the 
2bc50 64 65 73 74 72 75 63 74 6f 72 20 61 6e 64 20 64  destructor and d
2bc60 72 6f 70 20 6d 65 74 61 64 61 74 61 20 6f 6e 20  rop metadata on 
2bc70 61 6e 79 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  any.** parameter
2bc80 20 6f 66 20 61 6e 79 20 66 75 6e 63 74 69 6f 6e   of any function
2bc90 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 20 20 54   at any time.  T
2bca0 68 65 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74 65  he only guarante
2bcb0 65 20 69 73 20 74 68 61 74 0a 2a 2a 20 74 68 65  e is that.** the
2bcc0 20 64 65 73 74 72 75 63 74 6f 72 20 77 69 6c 6c   destructor will
2bcd0 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72   be called befor
2bce0 65 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 69  e the metadata i
2bcf0 73 20 64 72 6f 70 70 65 64 2e 0a 2a 2a 0a 2a 2a  s dropped..**.**
2bd00 20 49 6e 20 70 72 61 63 74 69 63 65 2c 20 6d 65   In practice, me
2bd10 74 61 64 61 74 61 20 69 73 20 70 72 65 73 65 72  tadata is preser
2bd20 76 65 64 20 62 65 74 77 65 65 6e 20 66 75 6e 63  ved between func
2bd30 74 69 6f 6e 20 63 61 6c 6c 73 20 66 6f 72 0a 2a  tion calls for.*
2bd40 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68  * expressions th
2bd50 61 74 20 61 72 65 20 63 6f 6e 73 74 61 6e 74 20  at are constant 
2bd60 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e  at compile time.
2bd70 20 54 68 69 73 20 69 6e 63 6c 75 64 65 73 20 6c   This includes l
2bd80 69 74 65 72 61 6c 0a 2a 2a 20 76 61 6c 75 65 73  iteral.** values
2bd90 20 61 6e 64 20 53 51 4c 20 76 61 72 69 61 62 6c   and SQL variabl
2bda0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  es..**.** These 
2bdb0 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65  routines must be
2bdc0 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65   called from the
2bdd0 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20   same thread in 
2bde0 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 53 51 4c  which.** the SQL
2bdf0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e   function is run
2be00 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75  ning..**.** Requ
2be10 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31  irements:.** [H1
2be20 36 32 37 32 5d 20 5b 48 31 36 32 37 34 5d 20 5b  6272] [H16274] [
2be30 48 31 36 32 37 36 5d 20 5b 48 31 36 32 37 37 5d  H16276] [H16277]
2be40 20 5b 48 31 36 32 37 38 5d 20 5b 48 31 36 32 37   [H16278] [H1627
2be50 39 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  9].*/.SQLITE_API
2be60 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 67   void *sqlite3_g
2be70 65 74 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74  et_auxdata(sqlit
2be80 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74  e3_context*, int
2be90 20 4e 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20   N);.SQLITE_API 
2bea0 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 73 65 74  void sqlite3_set
2beb0 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74 65 33  _auxdata(sqlite3
2bec0 5f 63 6f 6e 74 65 78 74 2a 2c 20 69 6e 74 20 4e  _context*, int N
2bed0 2c 20 76 6f 69 64 2a 2c 20 76 6f 69 64 20 28 2a  , void*, void (*
2bee0 29 28 76 6f 69 64 2a 29 29 3b 0a 0a 0a 2f 2a 0a  )(void*));.../*.
2bef0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e  ** CAPI3REF: Con
2bf00 73 74 61 6e 74 73 20 44 65 66 69 6e 69 6e 67 20  stants Defining 
2bf10 53 70 65 63 69 61 6c 20 44 65 73 74 72 75 63 74  Special Destruct
2bf20 6f 72 20 42 65 68 61 76 69 6f 72 20 7b 48 31 30  or Behavior {H10
2bf30 32 38 30 7d 20 3c 53 33 30 31 30 30 3e 0a 2a 2a  280} <S30100>.**
2bf40 0a 2a 2a 20 54 68 65 73 65 20 61 72 65 20 73 70  .** These are sp
2bf50 65 63 69 61 6c 20 76 61 6c 75 65 73 20 66 6f 72  ecial values for
2bf60 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20   the destructor 
2bf70 74 68 61 74 20 69 73 20 70 61 73 73 65 64 20 69  that is passed i
2bf80 6e 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 6e 61  n as the.** fina
2bf90 6c 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 72 6f  l argument to ro
2bfa0 75 74 69 6e 65 73 20 6c 69 6b 65 20 5b 73 71 6c  utines like [sql
2bfb0 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
2bfc0 28 29 5d 2e 20 20 49 66 20 74 68 65 20 64 65 73  ()].  If the des
2bfd0 74 72 75 63 74 6f 72 0a 2a 2a 20 61 72 67 75 6d  tructor.** argum
2bfe0 65 6e 74 20 69 73 20 53 51 4c 49 54 45 5f 53 54  ent is SQLITE_ST
2bff0 41 54 49 43 2c 20 69 74 20 6d 65 61 6e 73 20 74  ATIC, it means t
2c000 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  hat the content 
2c010 70 6f 69 6e 74 65 72 20 69 73 20 63 6f 6e 73 74  pointer is const
2c020 61 6e 74 0a 2a 2a 20 61 6e 64 20 77 69 6c 6c 20  ant.** and will 
2c030 6e 65 76 65 72 20 63 68 61 6e 67 65 2e 20 20 49  never change.  I
2c040 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  t does not need 
2c050 74 6f 20 62 65 20 64 65 73 74 72 6f 79 65 64 2e  to be destroyed.
2c060 20 20 54 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f    The.** SQLITE_
2c070 54 52 41 4e 53 49 45 4e 54 20 76 61 6c 75 65 20  TRANSIENT value 
2c080 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 63  means that the c
2c090 6f 6e 74 65 6e 74 20 77 69 6c 6c 20 6c 69 6b 65  ontent will like
2c0a0 6c 79 20 63 68 61 6e 67 65 20 69 6e 0a 2a 2a 20  ly change in.** 
2c0b0 74 68 65 20 6e 65 61 72 20 66 75 74 75 72 65 20  the near future 
2c0c0 61 6e 64 20 74 68 61 74 20 53 51 4c 69 74 65 20  and that SQLite 
2c0d0 73 68 6f 75 6c 64 20 6d 61 6b 65 20 69 74 73 20  should make its 
2c0e0 6f 77 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79  own private copy
2c0f0 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65   of.** the conte
2c100 6e 74 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  nt before return
2c110 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74  ing..**.** The t
2c120 79 70 65 64 65 66 20 69 73 20 6e 65 63 65 73 73  ypedef is necess
2c130 61 72 79 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75  ary to work arou
2c140 6e 64 20 70 72 6f 62 6c 65 6d 73 20 69 6e 20 63  nd problems in c
2c150 65 72 74 61 69 6e 0a 2a 2a 20 43 2b 2b 20 63 6f  ertain.** C++ co
2c160 6d 70 69 6c 65 72 73 2e 20 20 53 65 65 20 74 69  mpilers.  See ti
2c170 63 6b 65 74 20 23 32 31 39 31 2e 0a 2a 2f 0a 74  cket #2191..*/.t
2c180 79 70 65 64 65 66 20 76 6f 69 64 20 28 2a 73 71  ypedef void (*sq
2c190 6c 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72  lite3_destructor
2c1a0 5f 74 79 70 65 29 28 76 6f 69 64 2a 29 3b 0a 23  _type)(void*);.#
2c1b0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54  define SQLITE_ST
2c1c0 41 54 49 43 20 20 20 20 20 20 28 28 73 71 6c 69  ATIC      ((sqli
2c1d0 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72 5f 74  te3_destructor_t
2c1e0 79 70 65 29 30 29 0a 23 64 65 66 69 6e 65 20 53  ype)0).#define S
2c1f0 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20  QLITE_TRANSIENT 
2c200 20 20 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74    ((sqlite3_dest
2c210 72 75 63 74 6f 72 5f 74 79 70 65 29 2d 31 29 0a  ructor_type)-1).
2c220 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
2c230 20 53 65 74 74 69 6e 67 20 54 68 65 20 52 65 73   Setting The Res
2c240 75 6c 74 20 4f 66 20 41 6e 20 53 51 4c 20 46 75  ult Of An SQL Fu
2c250 6e 63 74 69 6f 6e 20 7b 48 31 36 34 30 30 7d 20  nction {H16400} 
2c260 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S20200>.**.** T
2c270 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72  hese routines ar
2c280 65 20 75 73 65 64 20 62 79 20 74 68 65 20 78 46  e used by the xF
2c290 75 6e 63 20 6f 72 20 78 46 69 6e 61 6c 20 63 61  unc or xFinal ca
2c2a0 6c 6c 62 61 63 6b 73 20 74 68 61 74 0a 2a 2a 20  llbacks that.** 
2c2b0 69 6d 70 6c 65 6d 65 6e 74 20 53 51 4c 20 66 75  implement SQL fu
2c2c0 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67 72  nctions and aggr
2c2d0 65 67 61 74 65 73 2e 20 20 53 65 65 0a 2a 2a 20  egates.  See.** 
2c2e0 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
2c2f0 66 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64 20  function()] and 
2c300 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
2c310 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a 2a  function16()].**
2c320 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
2c330 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a  information..**.
2c340 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69 6f  ** These functio
2c350 6e 73 20 77 6f 72 6b 20 76 65 72 79 20 6d 75 63  ns work very muc
2c360 68 20 6c 69 6b 65 20 74 68 65 20 5b 70 61 72 61  h like the [para
2c370 6d 65 74 65 72 20 62 69 6e 64 69 6e 67 5d 20 66  meter binding] f
2c380 61 6d 69 6c 79 20 6f 66 0a 2a 2a 20 66 75 6e 63  amily of.** func
2c390 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20 62 69  tions used to bi
2c3a0 6e 64 20 76 61 6c 75 65 73 20 74 6f 20 68 6f 73  nd values to hos
2c3b0 74 20 70 61 72 61 6d 65 74 65 72 73 20 69 6e 20  t parameters in 
2c3c0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
2c3d0 6e 74 73 2e 0a 2a 2a 20 52 65 66 65 72 20 74 6f  nts..** Refer to
2c3e0 20 74 68 65 20 5b 53 51 4c 20 70 61 72 61 6d 65   the [SQL parame
2c3f0 74 65 72 5d 20 64 6f 63 75 6d 65 6e 74 61 74 69  ter] documentati
2c400 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61  on for additiona
2c410 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  l information..*
2c420 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
2c430 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 29 20 69  _result_blob() i
2c440 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68  nterface sets th
2c450 65 20 72 65 73 75 6c 74 20 66 72 6f 6d 0a 2a 2a  e result from.**
2c460 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d   an application-
2c470 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
2c480 20 74 6f 20 62 65 20 74 68 65 20 42 4c 4f 42 20   to be the BLOB 
2c490 77 68 6f 73 65 20 63 6f 6e 74 65 6e 74 20 69 73  whose content is
2c4a0 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62   pointed.** to b
2c4b0 79 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72  y the second par
2c4c0 61 6d 65 74 65 72 20 61 6e 64 20 77 68 69 63 68  ameter and which
2c4d0 20 69 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e 67   is N bytes long
2c4e0 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a   where N is the.
2c4f0 2a 2a 20 74 68 69 72 64 20 70 61 72 61 6d 65 74  ** third paramet
2c500 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  er..**.** The sq
2c510 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72  lite3_result_zer
2c520 6f 62 6c 6f 62 28 29 20 69 6e 74 65 72 66 61 63  oblob() interfac
2c530 65 73 20 73 65 74 20 74 68 65 20 72 65 73 75 6c  es set the resul
2c540 74 20 6f 66 0a 2a 2a 20 74 68 65 20 61 70 70 6c  t of.** the appl
2c550 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
2c560 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 61  function to be a
2c570 20 42 4c 4f 42 20 63 6f 6e 74 61 69 6e 69 6e 67   BLOB containing
2c580 20 61 6c 6c 20 7a 65 72 6f 0a 2a 2a 20 62 79 74   all zero.** byt
2c590 65 73 20 61 6e 64 20 4e 20 62 79 74 65 73 20 69  es and N bytes i
2c5a0 6e 20 73 69 7a 65 2c 20 77 68 65 72 65 20 4e 20  n size, where N 
2c5b0 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  is the value of 
2c5c0 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65  the 2nd paramete
2c5d0 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  r..**.** The sql
2c5e0 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
2c5f0 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 73  le() interface s
2c600 65 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 66  ets the result f
2c610 72 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63  rom.** an applic
2c620 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75  ation-defined fu
2c630 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 61 20 66  nction to be a f
2c640 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61  loating point va
2c650 6c 75 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  lue specified.**
2c660 20 62 79 20 69 74 73 20 32 6e 64 20 61 72 67 75   by its 2nd argu
2c670 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ment..**.** The 
2c680 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2c690 72 72 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74  rror() and sqlit
2c6a0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31  e3_result_error1
2c6b0 36 28 29 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a  6() functions.**
2c6c0 20 63 61 75 73 65 20 74 68 65 20 69 6d 70 6c 65   cause the imple
2c6d0 6d 65 6e 74 65 64 20 53 51 4c 20 66 75 6e 63 74  mented SQL funct
2c6e0 69 6f 6e 20 74 6f 20 74 68 72 6f 77 20 61 6e 20  ion to throw an 
2c6f0 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 20 53 51  exception..** SQ
2c700 4c 69 74 65 20 75 73 65 73 20 74 68 65 20 73 74  Lite uses the st
2c710 72 69 6e 67 20 70 6f 69 6e 74 65 64 20 74 6f 20  ring pointed to 
2c720 62 79 20 74 68 65 0a 2a 2a 20 32 6e 64 20 70 61  by the.** 2nd pa
2c730 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c 69 74  rameter of sqlit
2c740 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
2c750 29 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73  ) or sqlite3_res
2c760 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a  ult_error16().**
2c770 20 61 73 20 74 68 65 20 74 65 78 74 20 6f 66 20   as the text of 
2c780 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
2c790 2e 20 20 53 51 4c 69 74 65 20 69 6e 74 65 72 70  .  SQLite interp
2c7a0 72 65 74 73 20 74 68 65 20 65 72 72 6f 72 0a 2a  rets the error.*
2c7b0 2a 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67  * message string
2c7c0 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 72 65   from sqlite3_re
2c7d0 73 75 6c 74 5f 65 72 72 6f 72 28 29 20 61 73 20  sult_error() as 
2c7e0 55 54 46 2d 38 2e 20 53 51 4c 69 74 65 0a 2a 2a  UTF-8. SQLite.**
2c7f0 20 69 6e 74 65 72 70 72 65 74 73 20 74 68 65 20   interprets the 
2c800 73 74 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69  string from sqli
2c810 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2c820 31 36 28 29 20 61 73 20 55 54 46 2d 31 36 20 69  16() as UTF-16 i
2c830 6e 20 6e 61 74 69 76 65 0a 2a 2a 20 62 79 74 65  n native.** byte
2c840 20 6f 72 64 65 72 2e 20 20 49 66 20 74 68 65 20   order.  If the 
2c850 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20  third parameter 
2c860 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  to sqlite3_resul
2c870 74 5f 65 72 72 6f 72 28 29 0a 2a 2a 20 6f 72 20  t_error().** or 
2c880 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
2c890 72 72 6f 72 31 36 28 29 20 69 73 20 6e 65 67 61  rror16() is nega
2c8a0 74 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65  tive then SQLite
2c8b0 20 74 61 6b 65 73 20 61 73 20 74 68 65 20 65 72   takes as the er
2c8c0 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 61  ror.** message a
2c8d0 6c 6c 20 74 65 78 74 20 75 70 20 74 68 72 6f 75  ll text up throu
2c8e0 67 68 20 74 68 65 20 66 69 72 73 74 20 7a 65 72  gh the first zer
2c8f0 6f 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20  o character..** 
2c900 49 66 20 74 68 65 20 74 68 69 72 64 20 70 61 72  If the third par
2c910 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65  ameter to sqlite
2c920 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29  3_result_error()
2c930 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 72   or.** sqlite3_r
2c940 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20  esult_error16() 
2c950 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20  is non-negative 
2c960 74 68 65 6e 20 53 51 4c 69 74 65 20 74 61 6b 65  then SQLite take
2c970 73 20 74 68 61 74 20 6d 61 6e 79 0a 2a 2a 20 62  s that many.** b
2c980 79 74 65 73 20 28 6e 6f 74 20 63 68 61 72 61 63  ytes (not charac
2c990 74 65 72 73 29 20 66 72 6f 6d 20 74 68 65 20 32  ters) from the 2
2c9a0 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 73 20  nd parameter as 
2c9b0 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  the error messag
2c9c0 65 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  e..** The sqlite
2c9d0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29  3_result_error()
2c9e0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 73   and sqlite3_res
2c9f0 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 0a 2a 2a  ult_error16().**
2ca00 20 72 6f 75 74 69 6e 65 73 20 6d 61 6b 65 20 61   routines make a
2ca10 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f 66   private copy of
2ca20 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
2ca30 67 65 20 74 65 78 74 20 62 65 66 6f 72 65 0a 2a  ge text before.*
2ca40 2a 20 74 68 65 79 20 72 65 74 75 72 6e 2e 20 20  * they return.  
2ca50 48 65 6e 63 65 2c 20 74 68 65 20 63 61 6c 6c 69  Hence, the calli
2ca60 6e 67 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 20  ng function can 
2ca70 64 65 61 6c 6c 6f 63 61 74 65 20 6f 72 0a 2a 2a  deallocate or.**
2ca80 20 6d 6f 64 69 66 79 20 74 68 65 20 74 65 78 74   modify the text
2ca90 20 61 66 74 65 72 20 74 68 65 79 20 72 65 74 75   after they retu
2caa0 72 6e 20 77 69 74 68 6f 75 74 20 68 61 72 6d 2e  rn without harm.
2cab0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2cac0 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64  result_error_cod
2cad0 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 68 61  e() function cha
2cae0 6e 67 65 73 20 74 68 65 20 65 72 72 6f 72 20 63  nges the error c
2caf0 6f 64 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20  ode.** returned 
2cb00 62 79 20 53 51 4c 69 74 65 20 61 73 20 61 20 72  by SQLite as a r
2cb10 65 73 75 6c 74 20 6f 66 20 61 6e 20 65 72 72 6f  esult of an erro
2cb20 72 20 69 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e  r in a function.
2cb30 20 20 42 79 20 64 65 66 61 75 6c 74 2c 0a 2a 2a    By default,.**
2cb40 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   the error code 
2cb50 69 73 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e  is SQLITE_ERROR.
2cb60 20 20 41 20 73 75 62 73 65 71 75 65 6e 74 20 63    A subsequent c
2cb70 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 72  all to sqlite3_r
2cb80 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a 2a  esult_error().**
2cb90 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75   or sqlite3_resu
2cba0 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 72 65 73  lt_error16() res
2cbb0 65 74 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f  ets the error co
2cbc0 64 65 20 74 6f 20 53 51 4c 49 54 45 5f 45 52 52  de to SQLITE_ERR
2cbd0 4f 52 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  OR..**.** The sq
2cbe0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 6f 6f  lite3_result_too
2cbf0 62 69 67 28 29 20 69 6e 74 65 72 66 61 63 65 20  big() interface 
2cc00 63 61 75 73 65 73 20 53 51 4c 69 74 65 20 74 6f  causes SQLite to
2cc10 20 74 68 72 6f 77 20 61 6e 20 65 72 72 6f 72 0a   throw an error.
2cc20 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68  ** indicating th
2cc30 61 74 20 61 20 73 74 72 69 6e 67 20 6f 72 20 42  at a string or B
2cc40 4c 4f 42 20 69 73 20 74 6f 20 6c 6f 6e 67 20 74  LOB is to long t
2cc50 6f 20 72 65 70 72 65 73 65 6e 74 2e 0a 2a 2a 0a  o represent..**.
2cc60 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72  ** The sqlite3_r
2cc70 65 73 75 6c 74 5f 6e 6f 6d 65 6d 28 29 20 69 6e  esult_nomem() in
2cc80 74 65 72 66 61 63 65 20 63 61 75 73 65 73 20 53  terface causes S
2cc90 51 4c 69 74 65 20 74 6f 20 74 68 72 6f 77 20 61  QLite to throw a
2cca0 6e 20 65 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 63  n error.** indic
2ccb0 61 74 69 6e 67 20 74 68 61 74 20 61 20 6d 65 6d  ating that a mem
2ccc0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  ory allocation f
2ccd0 61 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ailed..**.** The
2cce0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
2ccf0 69 6e 74 28 29 20 69 6e 74 65 72 66 61 63 65 20  int() interface 
2cd00 73 65 74 73 20 74 68 65 20 72 65 74 75 72 6e 20  sets the return 
2cd10 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20  value.** of the 
2cd20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
2cd30 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  ned function to 
2cd40 62 65 20 74 68 65 20 33 32 2d 62 69 74 20 73 69  be the 32-bit si
2cd50 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20  gned integer.** 
2cd60 76 61 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 74  value given in t
2cd70 68 65 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e  he 2nd argument.
2cd80 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
2cd90 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 29 20 69  result_int64() i
2cda0 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68  nterface sets th
2cdb0 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a  e return value.*
2cdc0 2a 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61  * of the applica
2cdd0 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e  tion-defined fun
2cde0 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20  ction to be the 
2cdf0 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  64-bit signed in
2ce00 74 65 67 65 72 0a 2a 2a 20 76 61 6c 75 65 20 67  teger.** value g
2ce10 69 76 65 6e 20 69 6e 20 74 68 65 20 32 6e 64 20  iven in the 2nd 
2ce20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  argument..**.** 
2ce30 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  The sqlite3_resu
2ce40 6c 74 5f 6e 75 6c 6c 28 29 20 69 6e 74 65 72 66  lt_null() interf
2ce50 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 74  ace sets the ret
2ce60 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  urn value.** of 
2ce70 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  the application-
2ce80 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
2ce90 20 74 6f 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a   to be NULL..**.
2cea0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72  ** The sqlite3_r
2ceb0 65 73 75 6c 74 5f 74 65 78 74 28 29 2c 20 73 71  esult_text(), sq
2cec0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2ced0 74 31 36 28 29 2c 0a 2a 2a 20 73 71 6c 69 74 65  t16(),.** sqlite
2cee0 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c  3_result_text16l
2cef0 65 28 29 2c 20 61 6e 64 20 73 71 6c 69 74 65 33  e(), and sqlite3
2cf00 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65  _result_text16be
2cf10 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a  () interfaces.**
2cf20 20 73 65 74 20 74 68 65 20 72 65 74 75 72 6e 20   set the return 
2cf30 76 61 6c 75 65 20 6f 66 20 74 68 65 20 61 70 70  value of the app
2cf40 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64  lication-defined
2cf50 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 0a   function to be.
2cf60 2a 2a 20 61 20 74 65 78 74 20 73 74 72 69 6e 67  ** a text string
2cf70 20 77 68 69 63 68 20 69 73 20 72 65 70 72 65 73   which is repres
2cf80 65 6e 74 65 64 20 61 73 20 55 54 46 2d 38 2c 20  ented as UTF-8, 
2cf90 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79  UTF-16 native by
2cfa0 74 65 20 6f 72 64 65 72 2c 0a 2a 2a 20 55 54 46  te order,.** UTF
2cfb0 2d 31 36 20 6c 69 74 74 6c 65 20 65 6e 64 69 61  -16 little endia
2cfc0 6e 2c 20 6f 72 20 55 54 46 2d 31 36 20 62 69 67  n, or UTF-16 big
2cfd0 20 65 6e 64 69 61 6e 2c 20 72 65 73 70 65 63 74   endian, respect
2cfe0 69 76 65 6c 79 2e 0a 2a 2a 20 53 51 4c 69 74 65  ively..** SQLite
2cff0 20 74 61 6b 65 73 20 74 68 65 20 74 65 78 74 20   takes the text 
2d000 72 65 73 75 6c 74 20 66 72 6f 6d 20 74 68 65 20  result from the 
2d010 61 70 70 6c 69 63 61 74 69 6f 6e 20 66 72 6f 6d  application from
2d020 0a 2a 2a 20 74 68 65 20 32 6e 64 20 70 61 72 61  .** the 2nd para
2d030 6d 65 74 65 72 20 6f 66 20 74 68 65 20 73 71 6c  meter of the sql
2d040 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
2d050 2a 20 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2a  * interfaces..**
2d060 20 49 66 20 74 68 65 20 33 72 64 20 70 61 72 61   If the 3rd para
2d070 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c  meter to the sql
2d080 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
2d090 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20  * interfaces.** 
2d0a0 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65  is negative, the
2d0b0 6e 20 53 51 4c 69 74 65 20 74 61 6b 65 73 20 72  n SQLite takes r
2d0c0 65 73 75 6c 74 20 74 65 78 74 20 66 72 6f 6d 20  esult text from 
2d0d0 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65  the 2nd paramete
2d0e0 72 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65  r.** through the
2d0f0 20 66 69 72 73 74 20 7a 65 72 6f 20 63 68 61 72   first zero char
2d100 61 63 74 65 72 2e 0a 2a 2a 20 49 66 20 74 68 65  acter..** If the
2d110 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74   3rd parameter t
2d120 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65  o the sqlite3_re
2d130 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72  sult_text* inter
2d140 66 61 63 65 73 0a 2a 2a 20 69 73 20 6e 6f 6e 2d  faces.** is non-
2d150 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 61  negative, then a
2d160 73 20 6d 61 6e 79 20 62 79 74 65 73 20 28 6e 6f  s many bytes (no
2d170 74 20 63 68 61 72 61 63 74 65 72 73 29 20 6f 66  t characters) of
2d180 20 74 68 65 20 74 65 78 74 0a 2a 2a 20 70 6f 69   the text.** poi
2d190 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65 20 32  nted to by the 2
2d1a0 6e 64 20 70 61 72 61 6d 65 74 65 72 20 61 72 65  nd parameter are
2d1b0 20 74 61 6b 65 6e 20 61 73 20 74 68 65 20 61 70   taken as the ap
2d1c0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
2d1d0 64 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65  d.** function re
2d1e0 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20  sult..** If the 
2d1f0 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f  4th parameter to
2d200 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   the sqlite3_res
2d210 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66  ult_text* interf
2d220 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c 69 74  aces.** or sqlit
2d230 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69  e3_result_blob i
2d240 73 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69  s a non-NULL poi
2d250 6e 74 65 72 2c 20 74 68 65 6e 20 53 51 4c 69 74  nter, then SQLit
2d260 65 20 63 61 6c 6c 73 20 74 68 61 74 0a 2a 2a 20  e calls that.** 
2d270 66 75 6e 63 74 69 6f 6e 20 61 73 20 74 68 65 20  function as the 
2d280 64 65 73 74 72 75 63 74 6f 72 20 6f 6e 20 74 68  destructor on th
2d290 65 20 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 72  e text or BLOB r
2d2a0 65 73 75 6c 74 20 77 68 65 6e 20 69 74 20 68 61  esult when it ha
2d2b0 73 0a 2a 2a 20 66 69 6e 69 73 68 65 64 20 75 73  s.** finished us
2d2c0 69 6e 67 20 74 68 61 74 20 72 65 73 75 6c 74 2e  ing that result.
2d2d0 0a 2a 2a 20 49 66 20 74 68 65 20 34 74 68 20 70  .** If the 4th p
2d2e0 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20  arameter to the 
2d2f0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
2d300 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65 73 20  ext* interfaces 
2d310 6f 72 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33  or to.** sqlite3
2d320 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69 73 20  _result_blob is 
2d330 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f 6e 73  the special cons
2d340 74 61 6e 74 20 53 51 4c 49 54 45 5f 53 54 41 54  tant SQLITE_STAT
2d350 49 43 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 0a  IC, then SQLite.
2d360 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  ** assumes that 
2d370 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c 4f 42  the text or BLOB
2d380 20 72 65 73 75 6c 74 20 69 73 20 69 6e 20 63 6f   result is in co
2d390 6e 73 74 61 6e 74 20 73 70 61 63 65 20 61 6e 64  nstant space and
2d3a0 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63 6f 70   does not.** cop
2d3b0 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  y the content of
2d3c0 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 6e   the parameter n
2d3d0 6f 72 20 63 61 6c 6c 20 61 20 64 65 73 74 72 75  or call a destru
2d3e0 63 74 6f 72 20 6f 6e 20 74 68 65 20 63 6f 6e 74  ctor on the cont
2d3f0 65 6e 74 0a 2a 2a 20 77 68 65 6e 20 69 74 20 68  ent.** when it h
2d400 61 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e  as finished usin
2d410 67 20 74 68 61 74 20 72 65 73 75 6c 74 2e 0a 2a  g that result..*
2d420 2a 20 49 66 20 74 68 65 20 34 74 68 20 70 61 72  * If the 4th par
2d430 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71  ameter to the sq
2d440 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2d450 74 2a 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a  t* interfaces.**
2d460 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73 75   or sqlite3_resu
2d470 6c 74 5f 62 6c 6f 62 20 69 73 20 74 68 65 20 73  lt_blob is the s
2d480 70 65 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20  pecial constant 
2d490 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
2d4a0 0a 2a 2a 20 74 68 65 6e 20 53 51 4c 69 74 65 20  .** then SQLite 
2d4b0 6d 61 6b 65 73 20 61 20 63 6f 70 79 20 6f 66 20  makes a copy of 
2d4c0 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74 6f 20  the result into 
2d4d0 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 20 66  space obtained f
2d4e0 72 6f 6d 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c  rom.** from [sql
2d4f0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 62  ite3_malloc()] b
2d500 65 66 6f 72 65 20 69 74 20 72 65 74 75 72 6e 73  efore it returns
2d510 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
2d520 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
2d530 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65 74  () interface set
2d540 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a  s the result of.
2d550 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  ** the applicati
2d560 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  on-defined funct
2d570 69 6f 6e 20 74 6f 20 62 65 20 61 20 63 6f 70 79  ion to be a copy
2d580 20 74 68 65 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65   the.** [unprote
2d590 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c  cted sqlite3_val
2d5a0 75 65 5d 20 6f 62 6a 65 63 74 20 73 70 65 63 69  ue] object speci
2d5b0 66 69 65 64 20 62 79 20 74 68 65 20 32 6e 64 20  fied by the 2nd 
2d5c0 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68 65 0a  parameter.  The.
2d5d0 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ** sqlite3_resul
2d5e0 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65 72 66  t_value() interf
2d5f0 61 63 65 20 6d 61 6b 65 73 20 61 20 63 6f 70 79  ace makes a copy
2d600 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   of the [sqlite3
2d610 5f 76 61 6c 75 65 5d 0a 2a 2a 20 73 6f 20 74 68  _value].** so th
2d620 61 74 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  at the [sqlite3_
2d630 76 61 6c 75 65 5d 20 73 70 65 63 69 66 69 65 64  value] specified
2d640 20 69 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65   in the paramete
2d650 72 20 6d 61 79 20 63 68 61 6e 67 65 20 6f 72 0a  r may change or.
2d660 2a 2a 20 62 65 20 64 65 61 6c 6c 6f 63 61 74 65  ** be deallocate
2d670 64 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f  d after sqlite3_
2d680 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 29 20 72  result_value() r
2d690 65 74 75 72 6e 73 20 77 69 74 68 6f 75 74 20 68  eturns without h
2d6a0 61 72 6d 2e 0a 2a 2a 20 41 20 5b 70 72 6f 74 65  arm..** A [prote
2d6b0 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c  cted sqlite3_val
2d6c0 75 65 5d 20 6f 62 6a 65 63 74 20 6d 61 79 20 61  ue] object may a
2d6d0 6c 77 61 79 73 20 62 65 20 75 73 65 64 20 77 68  lways be used wh
2d6e0 65 72 65 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f  ere an.** [unpro
2d6f0 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
2d700 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 69 73 20  alue] object is 
2d710 72 65 71 75 69 72 65 64 2c 20 73 6f 20 65 69 74  required, so eit
2d720 68 65 72 0a 2a 2a 20 6b 69 6e 64 20 6f 66 20 5b  her.** kind of [
2d730 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f  sqlite3_value] o
2d740 62 6a 65 63 74 20 63 61 6e 20 62 65 20 75 73 65  bject can be use
2d750 64 20 77 69 74 68 20 74 68 69 73 20 69 6e 74 65  d with this inte
2d760 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  rface..**.** If 
2d770 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  these routines a
2d780 72 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77  re called from w
2d790 69 74 68 69 6e 20 74 68 65 20 64 69 66 66 65 72  ithin the differ
2d7a0 65 6e 74 20 74 68 72 65 61 64 0a 2a 2a 20 74 68  ent thread.** th
2d7b0 61 6e 20 74 68 65 20 6f 6e 65 20 63 6f 6e 74 61  an the one conta
2d7c0 69 6e 69 6e 67 20 74 68 65 20 61 70 70 6c 69 63  ining the applic
2d7d0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75  ation-defined fu
2d7e0 6e 63 74 69 6f 6e 20 74 68 61 74 20 72 65 63 65  nction that rece
2d7f0 69 76 65 64 0a 2a 2a 20 74 68 65 20 5b 73 71 6c  ived.** the [sql
2d800 69 74 65 33 5f 63 6f 6e 74 65 78 74 5d 20 70 6f  ite3_context] po
2d810 69 6e 74 65 72 2c 20 74 68 65 20 72 65 73 75 6c  inter, the resul
2d820 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64  ts are undefined
2d830 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d  ..**.** Requirem
2d840 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 34 30 33  ents:.** [H16403
2d850 5d 20 5b 48 31 36 34 30 36 5d 20 5b 48 31 36 34  ] [H16406] [H164
2d860 30 39 5d 20 5b 48 31 36 34 31 32 5d 20 5b 48 31  09] [H16412] [H1
2d870 36 34 31 35 5d 20 5b 48 31 36 34 31 38 5d 20 5b  6415] [H16418] [
2d880 48 31 36 34 32 31 5d 20 5b 48 31 36 34 32 34 5d  H16421] [H16424]
2d890 0a 2a 2a 20 5b 48 31 36 34 32 37 5d 20 5b 48 31  .** [H16427] [H1
2d8a0 36 34 33 30 5d 20 5b 48 31 36 34 33 33 5d 20 5b  6430] [H16433] [
2d8b0 48 31 36 34 33 36 5d 20 5b 48 31 36 34 33 39 5d  H16436] [H16439]
2d8c0 20 5b 48 31 36 34 34 32 5d 20 5b 48 31 36 34 34   [H16442] [H1644
2d8d0 35 5d 20 5b 48 31 36 34 34 38 5d 0a 2a 2a 20 5b  5] [H16448].** [
2d8e0 48 31 36 34 35 31 5d 20 5b 48 31 36 34 35 34 5d  H16451] [H16454]
2d8f0 20 5b 48 31 36 34 35 37 5d 20 5b 48 31 36 34 36   [H16457] [H1646
2d900 30 5d 20 5b 48 31 36 34 36 33 5d 0a 2a 2f 0a 53  0] [H16463].*/.S
2d910 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
2d920 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
2d930 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ob(sqlite3_conte
2d940 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a  xt*, const void*
2d950 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76  , int, void(*)(v
2d960 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41  oid*));.SQLITE_A
2d970 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
2d980 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71  result_double(sq
2d990 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
2d9a0 64 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f  double);.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 65 72 72 6f 72 28 73 71  _result_error(sq
2d9d0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
2d9e0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74  const char*, int
2d9f0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  );.SQLITE_API vo
2da00 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
2da10 74 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65  t_error16(sqlite
2da20 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73  3_context*, cons
2da30 74 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53  t void*, int);.S
2da40 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
2da50 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
2da60 72 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74  ror_toobig(sqlit
2da70 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51  e3_context*);.SQ
2da80 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
2da90 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
2daa0 6f 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33  or_nomem(sqlite3
2dab0 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49  _context*);.SQLI
2dac0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
2dad0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2dae0 5f 63 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f  _code(sqlite3_co
2daf0 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51  ntext*, int);.SQ
2db00 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
2db10 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
2db20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2db30 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
2db40 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
2db50 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71  _result_int64(sq
2db60 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
2db70 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a  sqlite3_int64);.
2db80 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
2db90 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
2dba0 75 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ull(sqlite3_cont
2dbb0 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  ext*);.SQLITE_AP
2dbc0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
2dbd0 65 73 75 6c 74 5f 74 65 78 74 28 73 71 6c 69 74  esult_text(sqlit
2dbe0 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e  e3_context*, con
2dbf0 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76  st char*, int, v
2dc00 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
2dc10 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
2dc20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
2dc30 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 63 6f  ext16(sqlite3_co
2dc40 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f  ntext*, const vo
2dc50 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a  id*, int, void(*
2dc60 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54  )(void*));.SQLIT
2dc70 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
2dc80 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36  e3_result_text16
2dc90 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  le(sqlite3_conte
2dca0 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a  xt*, const void*
2dcb0 2c 20 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f  , int,void(*)(vo
2dcc0 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50  id*));.SQLITE_AP
2dcd0 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
2dce0 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 73  esult_text16be(s
2dcf0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
2dd00 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e   const void*, in
2dd10 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  t,void(*)(void*)
2dd20 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  );.SQLITE_API vo
2dd30 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
2dd40 74 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f  t_value(sqlite3_
2dd50 63 6f 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65  context*, sqlite
2dd60 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54  3_value*);.SQLIT
2dd70 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
2dd80 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c  e3_result_zerobl
2dd90 6f 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ob(sqlite3_conte
2dda0 78 74 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a  xt*, int n);../*
2ddb0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65  .** CAPI3REF: De
2ddc0 66 69 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69  fine New Collati
2ddd0 6e 67 20 53 65 71 75 65 6e 63 65 73 20 7b 48 31  ng Sequences {H1
2dde0 36 36 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a 2a  6600} <S20300>.*
2ddf0 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74  *.** These funct
2de00 69 6f 6e 73 20 61 72 65 20 75 73 65 64 20 74 6f  ions are used to
2de10 20 61 64 64 20 6e 65 77 20 63 6f 6c 6c 61 74 69   add new collati
2de20 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 74 6f 20  on sequences to 
2de30 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65  the.** [database
2de40 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65   connection] spe
2de50 63 69 66 69 65 64 20 61 73 20 74 68 65 20 66 69  cified as the fi
2de60 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a  rst argument..**
2de70 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20  .** The name of 
2de80 74 68 65 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f  the new collatio
2de90 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 73 70  n sequence is sp
2dea0 65 63 69 66 69 65 64 20 61 73 20 61 20 55 54 46  ecified as a UTF
2deb0 2d 38 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72  -8 string.** for
2dec0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2ded0 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 61 6e 64 20  collation() and 
2dee0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
2def0 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 0a 2a 2a  ollation_v2().**
2df00 20 61 6e 64 20 61 20 55 54 46 2d 31 36 20 73 74   and a UTF-16 st
2df10 72 69 6e 67 20 66 6f 72 20 73 71 6c 69 74 65 33  ring for sqlite3
2df20 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
2df30 6e 31 36 28 29 2e 20 49 6e 20 61 6c 6c 20 63 61  n16(). In all ca
2df40 73 65 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20  ses.** the name 
2df50 69 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65  is passed as the
2df60 20 73 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e   second function
2df70 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   argument..**.**
2df80 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d   The third argum
2df90 65 6e 74 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f  ent may be one o
2dfa0 66 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20  f the constants 
2dfb0 5b 53 51 4c 49 54 45 5f 55 54 46 38 5d 2c 0a 2a  [SQLITE_UTF8],.*
2dfc0 2a 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  * [SQLITE_UTF16L
2dfd0 45 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55  E], or [SQLITE_U
2dfe0 54 46 31 36 42 45 5d 2c 20 69 6e 64 69 63 61 74  TF16BE], indicat
2dff0 69 6e 67 20 74 68 61 74 20 74 68 65 20 75 73 65  ing that the use
2e000 72 2d 73 75 70 70 6c 69 65 64 0a 2a 2a 20 72 6f  r-supplied.** ro
2e010 75 74 69 6e 65 20 65 78 70 65 63 74 73 20 74 6f  utine expects to
2e020 20 62 65 20 70 61 73 73 65 64 20 70 6f 69 6e 74   be passed point
2e030 65 72 73 20 74 6f 20 73 74 72 69 6e 67 73 20 65  ers to strings e
2e040 6e 63 6f 64 65 64 20 75 73 69 6e 67 20 55 54 46  ncoded using UTF
2e050 2d 38 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69  -8,.** UTF-16 li
2e060 74 74 6c 65 2d 65 6e 64 69 61 6e 2c 20 6f 72 20  ttle-endian, or 
2e070 55 54 46 2d 31 36 20 62 69 67 2d 65 6e 64 69 61  UTF-16 big-endia
2e080 6e 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  n, respectively.
2e090 20 54 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72   The.** third ar
2e0a0 67 75 6d 65 6e 74 20 6d 69 67 68 74 20 61 6c 73  gument might als
2e0b0 6f 20 62 65 20 5b 53 51 4c 49 54 45 5f 55 54 46  o be [SQLITE_UTF
2e0c0 31 36 5d 20 74 6f 20 69 6e 64 69 63 61 74 65 20  16] to indicate 
2e0d0 74 68 61 74 20 74 68 65 20 72 6f 75 74 69 6e 65  that the routine
2e0e0 0a 2a 2a 20 65 78 70 65 63 74 73 20 70 6f 69 6e  .** expects poin
2e0f0 74 65 72 73 20 74 6f 20 62 65 20 55 54 46 2d 31  ters to be UTF-1
2e100 36 20 73 74 72 69 6e 67 73 20 69 6e 20 74 68 65  6 strings in the
2e110 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64   native byte ord
2e120 65 72 2c 20 6f 72 20 74 68 65 0a 2a 2a 20 61 72  er, or the.** ar
2e130 67 75 6d 65 6e 74 20 63 61 6e 20 62 65 20 5b 53  gument can be [S
2e140 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47  QLITE_UTF16_ALIG
2e150 4e 45 44 5d 20 69 66 20 74 68 65 0a 2a 2a 20 74  NED] if the.** t
2e160 68 65 20 72 6f 75 74 69 6e 65 20 65 78 70 65 63  he routine expec
2e170 74 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 31  ts pointers to 1
2e180 36 2d 62 69 74 20 77 6f 72 64 20 61 6c 69 67 6e  6-bit word align
2e190 65 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20 6f 66  ed strings.** of
2e1a0 20 55 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e   UTF-16 in the n
2e1b0 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72  ative byte order
2e1c0 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65  ..**.** A pointe
2e1d0 72 20 74 6f 20 74 68 65 20 75 73 65 72 20 73 75  r to the user su
2e1e0 70 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20 6d  pplied routine m
2e1f0 75 73 74 20 62 65 20 70 61 73 73 65 64 20 61 73  ust be passed as
2e200 20 74 68 65 20 66 69 66 74 68 0a 2a 2a 20 61 72   the fifth.** ar
2e210 67 75 6d 65 6e 74 2e 20 20 49 66 20 69 74 20 69  gument.  If it i
2e220 73 20 4e 55 4c 4c 2c 20 74 68 69 73 20 69 73 20  s NULL, this is 
2e230 74 68 65 20 73 61 6d 65 20 61 73 20 64 65 6c 65  the same as dele
2e240 74 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69  ting the collati
2e250 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 28  on.** sequence (
2e260 73 6f 20 74 68 61 74 20 53 51 4c 69 74 65 20 63  so that SQLite c
2e270 61 6e 6e 6f 74 20 63 61 6c 6c 20 69 74 20 61 6e  annot call it an
2e280 79 6d 6f 72 65 29 2e 0a 2a 2a 20 45 61 63 68 20  ymore)..** Each 
2e290 74 69 6d 65 20 74 68 65 20 61 70 70 6c 69 63 61  time the applica
2e2a0 74 69 6f 6e 20 73 75 70 70 6c 69 65 64 20 66 75  tion supplied fu
2e2b0 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65  nction is invoke
2e2c0 64 2c 20 69 74 20 69 73 20 70 61 73 73 65 64 0a  d, it is passed.
2e2d0 2a 2a 20 61 73 20 69 74 73 20 66 69 72 73 74 20  ** as its first 
2e2e0 70 61 72 61 6d 65 74 65 72 20 61 20 63 6f 70 79  parameter a copy
2e2f0 20 6f 66 20 74 68 65 20 76 6f 69 64 2a 20 70 61   of the void* pa
2e300 73 73 65 64 20 61 73 20 74 68 65 20 66 6f 75 72  ssed as the four
2e310 74 68 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74  th argument.** t
2e320 6f 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  o sqlite3_create
2e330 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 6f 72 20  _collation() or 
2e340 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
2e350 6f 6c 6c 61 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a  ollation16()..**
2e360 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69 6e  .** The remainin
2e370 67 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74  g arguments to t
2e380 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 73  he application-s
2e390 75 70 70 6c 69 65 64 20 72 6f 75 74 69 6e 65 20  upplied routine 
2e3a0 61 72 65 20 74 77 6f 20 73 74 72 69 6e 67 73 2c  are two strings,
2e3b0 0a 2a 2a 20 65 61 63 68 20 72 65 70 72 65 73 65  .** each represe
2e3c0 6e 74 65 64 20 62 79 20 61 20 28 6c 65 6e 67 74  nted by a (lengt
2e3d0 68 2c 20 64 61 74 61 29 20 70 61 69 72 20 61 6e  h, data) pair an
2e3e0 64 20 65 6e 63 6f 64 65 64 20 69 6e 20 74 68 65  d encoded in the
2e3f0 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 68 61   encoding.** tha
2e400 74 20 77 61 73 20 70 61 73 73 65 64 20 61 73 20  t was passed as 
2e410 74 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65  the third argume
2e420 6e 74 20 77 68 65 6e 20 74 68 65 20 63 6f 6c 6c  nt when the coll
2e430 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77  ation sequence w
2e440 61 73 0a 2a 2a 20 72 65 67 69 73 74 65 72 65 64  as.** registered
2e450 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 61 70 70  . {END}  The app
2e460 6c 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64  lication defined
2e470 20 63 6f 6c 6c 61 74 69 6f 6e 20 72 6f 75 74 69   collation routi
2e480 6e 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74  ne should.** ret
2e490 75 72 6e 20 6e 65 67 61 74 69 76 65 2c 20 7a 65  urn negative, ze
2e4a0 72 6f 20 6f 72 20 70 6f 73 69 74 69 76 65 20 69  ro or positive i
2e4b0 66 20 74 68 65 20 66 69 72 73 74 20 73 74 72 69  f the first stri
2e4c0 6e 67 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c  ng is less than,
2e4d0 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72  .** equal to, or
2e4e0 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
2e4f0 65 20 73 65 63 6f 6e 64 20 73 74 72 69 6e 67 2e  e second string.
2e500 20 69 2e 65 2e 20 28 53 54 52 49 4e 47 31 20 2d   i.e. (STRING1 -
2e510 20 53 54 52 49 4e 47 32 29 2e 0a 2a 2a 0a 2a 2a   STRING2)..**.**
2e520 20 54 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65   The sqlite3_cre
2e530 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
2e540 28 29 20 77 6f 72 6b 73 20 6c 69 6b 65 20 73 71  () works like sq
2e550 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
2e560 6c 61 74 69 6f 6e 28 29 0a 2a 2a 20 65 78 63 65  lation().** exce
2e570 70 74 20 74 68 61 74 20 69 74 20 74 61 6b 65 73  pt that it takes
2e580 20 61 6e 20 65 78 74 72 61 20 61 72 67 75 6d 65   an extra argume
2e590 6e 74 20 77 68 69 63 68 20 69 73 20 61 20 64 65  nt which is a de
2e5a0 73 74 72 75 63 74 6f 72 20 66 6f 72 0a 2a 2a 20  structor for.** 
2e5b0 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 2e 20 20  the collation.  
2e5c0 54 68 65 20 64 65 73 74 72 75 63 74 6f 72 20 69  The destructor i
2e5d0 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68  s called when th
2e5e0 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 73 0a 2a  e collation is.*
2e5f0 2a 20 64 65 73 74 72 6f 79 65 64 20 61 6e 64 20  * destroyed and 
2e600 69 73 20 70 61 73 73 65 64 20 61 20 63 6f 70 79  is passed a copy
2e610 20 6f 66 20 74 68 65 20 66 6f 75 72 74 68 20 70   of the fourth p
2e620 61 72 61 6d 65 74 65 72 20 76 6f 69 64 2a 20 70  arameter void* p
2e630 6f 69 6e 74 65 72 0a 2a 2a 20 6f 66 20 74 68 65  ointer.** of the
2e640 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2e650 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 2e 0a  collation_v2()..
2e660 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 73 20 61 72  ** Collations ar
2e670 65 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e  e destroyed when
2e680 20 74 68 65 79 20 61 72 65 20 6f 76 65 72 72 69   they are overri
2e690 64 64 65 6e 20 62 79 20 6c 61 74 65 72 20 63 61  dden by later ca
2e6a0 6c 6c 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f  lls to the.** co
2e6b0 6c 6c 61 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e  llation creation
2e6c0 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20 77 68   functions or wh
2e6d0 65 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  en the [database
2e6e0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 73 20   connection] is 
2e6f0 63 6c 6f 73 65 64 0a 2a 2a 20 75 73 69 6e 67 20  closed.** using 
2e700 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29  [sqlite3_close()
2e710 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  ]..**.** See als
2e720 6f 3a 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c  o:  [sqlite3_col
2e730 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 5d  lation_needed()]
2e740 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6f   and [sqlite3_co
2e750 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36  llation_needed16
2e760 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ()]..**.** Requi
2e770 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36  rements:.** [H16
2e780 36 30 33 5d 20 5b 48 31 36 36 30 34 5d 20 5b 48  603] [H16604] [H
2e790 31 36 36 30 36 5d 20 5b 48 31 36 36 30 39 5d 20  16606] [H16609] 
2e7a0 5b 48 31 36 36 31 32 5d 20 5b 48 31 36 36 31 35  [H16612] [H16615
2e7b0 5d 20 5b 48 31 36 36 31 38 5d 20 5b 48 31 36 36  ] [H16618] [H166
2e7c0 32 31 5d 0a 2a 2a 20 5b 48 31 36 36 32 34 5d 20  21].** [H16624] 
2e7d0 5b 48 31 36 36 32 37 5d 20 5b 48 31 36 36 33 30  [H16627] [H16630
2e7e0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
2e7f0 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
2e800 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20  te_collation(.  
2e810 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e  sqlite3*, .  con
2e820 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
2e830 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c  .  int eTextRep,
2e840 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74   .  void*,.  int
2e850 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64  (*xCompare)(void
2e860 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
2e870 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
2e880 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  *).);.SQLITE_API
2e890 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65   int sqlite3_cre
2e8a0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
2e8b0 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20  (.  sqlite3*, . 
2e8c0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
2e8d0 6d 65 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74  me, .  int eText
2e8e0 52 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20  Rep, .  void*,. 
2e8f0 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28   int(*xCompare)(
2e900 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
2e910 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
2e920 76 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a  void*),.  void(*
2e930 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29  xDestroy)(void*)
2e940 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .);.SQLITE_API i
2e950 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
2e960 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20  e_collation16(. 
2e970 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f   sqlite3*, .  co
2e980 6e 73 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c  nst void *zName,
2e990 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c  .  int eTextRep,
2e9a0 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74   .  void*,.  int
2e9b0 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64  (*xCompare)(void
2e9c0 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
2e9d0 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
2e9e0 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  *).);../*.** CAP
2e9f0 49 33 52 45 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e  I3REF: Collation
2ea00 20 4e 65 65 64 65 64 20 43 61 6c 6c 62 61 63 6b   Needed Callback
2ea10 73 20 7b 48 31 36 37 30 30 7d 20 3c 53 32 30 33  s {H16700} <S203
2ea20 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f  00>.**.** To avo
2ea30 69 64 20 68 61 76 69 6e 67 20 74 6f 20 72 65 67  id having to reg
2ea40 69 73 74 65 72 20 61 6c 6c 20 63 6f 6c 6c 61 74  ister all collat
2ea50 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 62 65  ion sequences be
2ea60 66 6f 72 65 20 61 20 64 61 74 61 62 61 73 65 0a  fore a database.
2ea70 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20  ** can be used, 
2ea80 61 20 73 69 6e 67 6c 65 20 63 61 6c 6c 62 61 63  a single callbac
2ea90 6b 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62  k function may b
2eaa0 65 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74  e registered wit
2eab0 68 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61  h the.** [databa
2eac0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74  se connection] t
2ead0 6f 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e  o be called when
2eae0 65 76 65 72 20 61 6e 20 75 6e 64 65 66 69 6e 65  ever an undefine
2eaf0 64 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73  d collation.** s
2eb00 65 71 75 65 6e 63 65 20 69 73 20 72 65 71 75 69  equence is requi
2eb10 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  red..**.** If th
2eb20 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65  e function is re
2eb30 67 69 73 74 65 72 65 64 20 75 73 69 6e 67 20 74  gistered using t
2eb40 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61  he sqlite3_colla
2eb50 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20 41 50  tion_needed() AP
2eb60 49 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73  I,.** then it is
2eb70 20 70 61 73 73 65 64 20 74 68 65 20 6e 61 6d 65   passed the name
2eb80 73 20 6f 66 20 75 6e 64 65 66 69 6e 65 64 20 63  s of undefined c
2eb90 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2eba0 65 73 20 61 73 20 73 74 72 69 6e 67 73 0a 2a 2a  es as strings.**
2ebb0 20 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d   encoded in UTF-
2ebc0 38 2e 20 7b 48 31 36 37 30 33 7d 20 49 66 20 73  8. {H16703} If s
2ebd0 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
2ebe0 5f 6e 65 65 64 65 64 31 36 28 29 20 69 73 20 75  _needed16() is u
2ebf0 73 65 64 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d 65  sed,.** the name
2ec00 73 20 61 72 65 20 70 61 73 73 65 64 20 61 73 20  s are passed as 
2ec10 55 54 46 2d 31 36 20 69 6e 20 6d 61 63 68 69 6e  UTF-16 in machin
2ec20 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72  e native byte or
2ec30 64 65 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74  der..** A call t
2ec40 6f 20 65 69 74 68 65 72 20 66 75 6e 63 74 69 6f  o either functio
2ec50 6e 20 72 65 70 6c 61 63 65 73 20 61 6e 79 20 65  n replaces any e
2ec60 78 69 73 74 69 6e 67 20 63 61 6c 6c 62 61 63 6b  xisting callback
2ec70 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65  ..**.** When the
2ec80 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76   callback is inv
2ec90 6f 6b 65 64 2c 20 74 68 65 20 66 69 72 73 74 20  oked, the first 
2eca0 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20  argument passed 
2ecb0 69 73 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66 20  is a copy.** of 
2ecc0 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
2ecd0 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63  ent to sqlite3_c
2ece0 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28  ollation_needed(
2ecf0 29 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ) or.** sqlite3_
2ed00 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
2ed10 31 36 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e  16().  The secon
2ed20 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  d argument is th
2ed30 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f  e database.** co
2ed40 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 74  nnection.  The t
2ed50 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73  hird argument is
2ed60 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f   one of [SQLITE_
2ed70 55 54 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f 55  UTF8], [SQLITE_U
2ed80 54 46 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20 5b  TF16BE],.** or [
2ed90 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d 2c  SQLITE_UTF16LE],
2eda0 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20   indicating the 
2edb0 6d 6f 73 74 20 64 65 73 69 72 61 62 6c 65 20 66  most desirable f
2edc0 6f 72 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61  orm of the colla
2edd0 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65  tion.** sequence
2ede0 20 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72   function requir
2edf0 65 64 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20  ed.  The fourth 
2ee00 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65  parameter is the
2ee10 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20   name of the.** 
2ee20 72 65 71 75 69 72 65 64 20 63 6f 6c 6c 61 74 69  required collati
2ee30 6f 6e 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a  on sequence..**.
2ee40 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20  ** The callback 
2ee50 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20  function should 
2ee60 72 65 67 69 73 74 65 72 20 74 68 65 20 64 65 73  register the des
2ee70 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 75  ired collation u
2ee80 73 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  sing.** [sqlite3
2ee90 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
2eea0 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63  n()], [sqlite3_c
2eeb0 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31  reate_collation1
2eec0 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c  6()], or.** [sql
2eed0 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
2eee0 61 74 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a  ation_v2()]..**.
2eef0 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
2ef00 0a 2a 2a 20 5b 48 31 36 37 30 32 5d 20 5b 48 31  .** [H16702] [H1
2ef10 36 37 30 34 5d 20 5b 48 31 36 37 30 36 5d 0a 2a  6704] [H16706].*
2ef20 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2ef30 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69   sqlite3_collati
2ef40 6f 6e 5f 6e 65 65 64 65 64 28 0a 20 20 73 71 6c  on_needed(.  sql
2ef50 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a 2c  ite3*, .  void*,
2ef60 20 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64   .  void(*)(void
2ef70 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65  *,sqlite3*,int e
2ef80 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68  TextRep,const ch
2ef90 61 72 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41  ar*).);.SQLITE_A
2efa0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
2efb0 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31  ollation_needed1
2efc0 36 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a  6(.  sqlite3*, .
2efd0 20 20 76 6f 69 64 2a 2c 0a 20 20 76 6f 69 64 28    void*,.  void(
2efe0 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33  *)(void*,sqlite3
2eff0 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63  *,int eTextRep,c
2f000 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29 3b 0a 0a  onst void*).);..
2f010 2f 2a 0a 2a 2a 20 53 70 65 63 69 66 79 20 74 68  /*.** Specify th
2f020 65 20 6b 65 79 20 66 6f 72 20 61 6e 20 65 6e 63  e key for an enc
2f030 72 79 70 74 65 64 20 64 61 74 61 62 61 73 65 2e  rypted database.
2f040 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73    This routine s
2f050 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 63 61 6c 6c  hould be.** call
2f060 65 64 20 72 69 67 68 74 20 61 66 74 65 72 20 73  ed right after s
2f070 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e 0a 2a  qlite3_open()..*
2f080 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f  *.** The code to
2f090 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20   implement this 
2f0a0 41 50 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c  API is not avail
2f0b0 61 62 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c  able in the publ
2f0c0 69 63 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66  ic release.** of
2f0d0 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49   SQLite..*/.SQLI
2f0e0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2f0f0 65 33 5f 6b 65 79 28 0a 20 20 73 71 6c 69 74 65  e3_key(.  sqlite
2f100 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
2f110 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
2f120 62 61 73 65 20 74 6f 20 62 65 20 72 65 6b 65 79  base to be rekey
2f130 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  ed */.  const vo
2f140 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b  id *pKey, int nK
2f150 65 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6b 65  ey     /* The ke
2f160 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  y */.);../*.** C
2f170 68 61 6e 67 65 20 74 68 65 20 6b 65 79 20 6f 6e  hange the key on
2f180 20 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73   an open databas
2f190 65 2e 20 20 49 66 20 74 68 65 20 63 75 72 72 65  e.  If the curre
2f1a0 6e 74 20 64 61 74 61 62 61 73 65 20 69 73 20 6e  nt database is n
2f1b0 6f 74 0a 2a 2a 20 65 6e 63 72 79 70 74 65 64 2c  ot.** encrypted,
2f1c0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   this routine wi
2f1d0 6c 6c 20 65 6e 63 72 79 70 74 20 69 74 2e 20 20  ll encrypt it.  
2f1e0 49 66 20 70 4e 65 77 3d 3d 30 20 6f 72 20 6e 4e  If pNew==0 or nN
2f1f0 65 77 3d 3d 30 2c 20 74 68 65 0a 2a 2a 20 64 61  ew==0, the.** da
2f200 74 61 62 61 73 65 20 69 73 20 64 65 63 72 79 70  tabase is decryp
2f210 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ted..**.** The c
2f220 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ode to implement
2f230 20 74 68 69 73 20 41 50 49 20 69 73 20 6e 6f 74   this API is not
2f240 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68   available in th
2f250 65 20 70 75 62 6c 69 63 20 72 65 6c 65 61 73 65  e public release
2f260 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a  .** of SQLite..*
2f270 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2f280 20 73 71 6c 69 74 65 33 5f 72 65 6b 65 79 28 0a   sqlite3_rekey(.
2f290 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
2f2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f2b0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20   /* Database to 
2f2c0 62 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20 20  be rekeyed */.  
2f2d0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
2f2e0 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20 2f  , int nKey     /
2f2f0 2a 20 54 68 65 20 6e 65 77 20 6b 65 79 20 2a 2f  * The new key */
2f300 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  .);../*.** CAPI3
2f310 52 45 46 3a 20 53 75 73 70 65 6e 64 20 45 78 65  REF: Suspend Exe
2f320 63 75 74 69 6f 6e 20 46 6f 72 20 41 20 53 68 6f  cution For A Sho
2f330 72 74 20 54 69 6d 65 20 7b 48 31 30 35 33 30 7d  rt Time {H10530}
2f340 20 3c 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20   <S40410>.**.** 
2f350 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6c 65 65  The sqlite3_slee
2f360 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 63 61 75  p() function cau
2f370 73 65 73 20 74 68 65 20 63 75 72 72 65 6e 74 20  ses the current 
2f380 74 68 72 65 61 64 20 74 6f 20 73 75 73 70 65 6e  thread to suspen
2f390 64 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 66  d execution.** f
2f3a0 6f 72 20 61 74 20 6c 65 61 73 74 20 61 20 6e 75  or at least a nu
2f3b0 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63  mber of millisec
2f3c0 6f 6e 64 73 20 73 70 65 63 69 66 69 65 64 20 69  onds specified i
2f3d0 6e 20 69 74 73 20 70 61 72 61 6d 65 74 65 72 2e  n its parameter.
2f3e0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f 70  .**.** If the op
2f3f0 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 64  erating system d
2f400 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 20  oes not support 
2f410 73 6c 65 65 70 20 72 65 71 75 65 73 74 73 20 77  sleep requests w
2f420 69 74 68 0a 2a 2a 20 6d 69 6c 6c 69 73 65 63 6f  ith.** milliseco
2f430 6e 64 20 74 69 6d 65 20 72 65 73 6f 6c 75 74 69  nd time resoluti
2f440 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 74 69 6d  on, then the tim
2f450 65 20 77 69 6c 6c 20 62 65 20 72 6f 75 6e 64 65  e will be rounde
2f460 64 20 75 70 20 74 6f 0a 2a 2a 20 74 68 65 20 6e  d up to.** the n
2f470 65 61 72 65 73 74 20 73 65 63 6f 6e 64 2e 20 54  earest second. T
2f480 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6c  he number of mil
2f490 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65  liseconds of sle
2f4a0 65 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72  ep actually.** r
2f4b0 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68  equested from th
2f4c0 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  e operating syst
2f4d0 65 6d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  em is returned..
2f4e0 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 6d 70  **.** SQLite imp
2f4f0 6c 65 6d 65 6e 74 73 20 74 68 69 73 20 69 6e 74  lements this int
2f500 65 72 66 61 63 65 20 62 79 20 63 61 6c 6c 69 6e  erface by callin
2f510 67 20 74 68 65 20 78 53 6c 65 65 70 28 29 0a 2a  g the xSleep().*
2f520 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20  * method of the 
2f530 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33  default [sqlite3
2f540 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a  _vfs] object..**
2f550 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
2f560 3a 20 5b 48 31 30 35 33 33 5d 20 5b 48 31 30 35  : [H10533] [H105
2f570 33 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  36].*/.SQLITE_AP
2f580 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6c  I int sqlite3_sl
2f590 65 65 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a  eep(int);../*.**
2f5a0 20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65 20   CAPI3REF: Name 
2f5b0 4f 66 20 54 68 65 20 46 6f 6c 64 65 72 20 48 6f  Of The Folder Ho
2f5c0 6c 64 69 6e 67 20 54 65 6d 70 6f 72 61 72 79 20  lding Temporary 
2f5d0 46 69 6c 65 73 20 7b 48 31 30 33 31 30 7d 20 3c  Files {H10310} <
2f5e0 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 49 66  S20000>.**.** If
2f5f0 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72   this global var
2f600 69 61 62 6c 65 20 69 73 20 6d 61 64 65 20 74 6f  iable is made to
2f610 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72 69   point to a stri
2f620 6e 67 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74  ng which is.** t
2f630 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 66 6f 6c  he name of a fol
2f640 64 65 72 20 28 61 2e 6b 2e 61 2e 20 64 69 72 65  der (a.k.a. dire
2f650 63 74 6f 72 79 29 2c 20 74 68 65 6e 20 61 6c 6c  ctory), then all
2f660 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73   temporary files
2f670 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 53  .** created by S
2f680 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 70 6c  QLite will be pl
2f690 61 63 65 64 20 69 6e 20 74 68 61 74 20 64 69 72  aced in that dir
2f6a0 65 63 74 6f 72 79 2e 20 20 49 66 20 74 68 69 73  ectory.  If this
2f6b0 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73 20   variable.** is 
2f6c0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20  a NULL pointer, 
2f6d0 74 68 65 6e 20 53 51 4c 69 74 65 20 70 65 72 66  then SQLite perf
2f6e0 6f 72 6d 73 20 61 20 73 65 61 72 63 68 20 66 6f  orms a search fo
2f6f0 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65  r an appropriate
2f700 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69  .** temporary fi
2f710 6c 65 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a  le directory..**
2f720 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73 61  .** It is not sa
2f730 66 65 20 74 6f 20 72 65 61 64 20 6f 72 20 6d 6f  fe to read or mo
2f740 64 69 66 79 20 74 68 69 73 20 76 61 72 69 61 62  dify this variab
2f750 6c 65 20 69 6e 20 6d 6f 72 65 20 74 68 61 6e 20  le in more than 
2f760 6f 6e 65 0a 2a 2a 20 74 68 72 65 61 64 20 61 74  one.** thread at
2f770 20 61 20 74 69 6d 65 2e 20 20 49 74 20 69 73 20   a time.  It is 
2f780 6e 6f 74 20 73 61 66 65 20 74 6f 20 72 65 61 64  not safe to read
2f790 20 6f 72 20 6d 6f 64 69 66 79 20 74 68 69 73 20   or modify this 
2f7a0 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 66 20 61  variable.** if a
2f7b0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
2f7c0 63 74 69 6f 6e 5d 20 69 73 20 62 65 69 6e 67 20  ction] is being 
2f7d0 75 73 65 64 20 61 74 20 74 68 65 20 73 61 6d 65  used at the same
2f7e0 20 74 69 6d 65 20 69 6e 20 61 20 73 65 70 61 72   time in a separ
2f7f0 61 74 65 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a  ate.** thread..*
2f800 2a 20 49 74 20 69 73 20 69 6e 74 65 6e 64 65 64  * It is intended
2f810 20 74 68 61 74 20 74 68 69 73 20 76 61 72 69 61   that this varia
2f820 62 6c 65 20 62 65 20 73 65 74 20 6f 6e 63 65 0a  ble be set once.
2f830 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20 70 72  ** as part of pr
2f840 6f 63 65 73 73 20 69 6e 69 74 69 61 6c 69 7a 61  ocess initializa
2f850 74 69 6f 6e 20 61 6e 64 20 62 65 66 6f 72 65 20  tion and before 
2f860 61 6e 79 20 53 51 4c 69 74 65 20 69 6e 74 65 72  any SQLite inter
2f870 66 61 63 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  face.** routines
2f880 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c 65   have been calle
2f890 64 20 61 6e 64 20 74 68 61 74 20 74 68 69 73 20  d and that this 
2f8a0 76 61 72 69 61 62 6c 65 20 72 65 6d 61 69 6e 20  variable remain 
2f8b0 75 6e 63 68 61 6e 67 65 64 0a 2a 2a 20 74 68 65  unchanged.** the
2f8c0 72 65 61 66 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54  reafter..**.** T
2f8d0 68 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64  he [temp_store_d
2f8e0 69 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d  irectory pragma]
2f8f0 20 6d 61 79 20 6d 6f 64 69 66 79 20 74 68 69 73   may modify this
2f900 20 76 61 72 69 61 62 6c 65 20 61 6e 64 20 63 61   variable and ca
2f910 75 73 65 0a 2a 2a 20 69 74 20 74 6f 20 70 6f 69  use.** it to poi
2f920 6e 74 20 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74  nt to memory obt
2f930 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69  ained from [sqli
2f940 74 65 33 5f 6d 61 6c 6c 6f 63 5d 2e 20 20 46 75  te3_malloc].  Fu
2f950 72 74 68 65 72 6d 6f 72 65 2c 0a 2a 2a 20 74 68  rthermore,.** th
2f960 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69  e [temp_store_di
2f970 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20  rectory pragma] 
2f980 61 6c 77 61 79 73 20 61 73 73 75 6d 65 73 20 74  always assumes t
2f990 68 61 74 20 61 6e 79 20 73 74 72 69 6e 67 0a 2a  hat any string.*
2f9a0 2a 20 74 68 61 74 20 74 68 69 73 20 76 61 72 69  * that this vari
2f9b0 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69  able points to i
2f9c0 73 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79  s held in memory
2f9d0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 0a   obtained from .
2f9e0 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  ** [sqlite3_mall
2f9f0 6f 63 5d 20 61 6e 64 20 74 68 65 20 70 72 61 67  oc] and the prag
2fa00 6d 61 20 6d 61 79 20 61 74 74 65 6d 70 74 20 74  ma may attempt t
2fa10 6f 20 66 72 65 65 20 74 68 61 74 20 6d 65 6d 6f  o free that memo
2fa20 72 79 0a 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c  ry.** using [sql
2fa30 69 74 65 33 5f 66 72 65 65 5d 2e 0a 2a 2a 20 48  ite3_free]..** H
2fa40 65 6e 63 65 2c 20 69 66 20 74 68 69 73 20 76 61  ence, if this va
2fa50 72 69 61 62 6c 65 20 69 73 20 6d 6f 64 69 66 69  riable is modifi
2fa60 65 64 20 64 69 72 65 63 74 6c 79 2c 20 65 69 74  ed directly, eit
2fa70 68 65 72 20 69 74 20 73 68 6f 75 6c 64 20 62 65  her it should be
2fa80 0a 2a 2a 20 6d 61 64 65 20 4e 55 4c 4c 20 6f 72  .** made NULL or
2fa90 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74 20 74   made to point t
2faa0 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  o memory obtaine
2fab0 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f  d from [sqlite3_
2fac0 6d 61 6c 6c 6f 63 5d 0a 2a 2a 20 6f 72 20 65 6c  malloc].** or el
2fad0 73 65 20 74 68 65 20 75 73 65 20 6f 66 20 74 68  se the use of th
2fae0 65 20 5b 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69  e [temp_store_di
2faf0 72 65 63 74 6f 72 79 20 70 72 61 67 6d 61 5d 20  rectory pragma] 
2fb00 73 68 6f 75 6c 64 20 62 65 20 61 76 6f 69 64 65  should be avoide
2fb10 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
2fb20 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 74   char *sqlite3_t
2fb30 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a 0a  emp_directory;..
2fb40 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
2fb50 54 65 73 74 20 46 6f 72 20 41 75 74 6f 2d 43 6f  Test For Auto-Co
2fb60 6d 6d 69 74 20 4d 6f 64 65 20 7b 48 31 32 39 33  mmit Mode {H1293
2fb70 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 4b  0} <S60200>.** K
2fb80 45 59 57 4f 52 44 53 3a 20 7b 61 75 74 6f 63 6f  EYWORDS: {autoco
2fb90 6d 6d 69 74 20 6d 6f 64 65 7d 0a 2a 2a 0a 2a 2a  mmit mode}.**.**
2fba0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74   The sqlite3_get
2fbb0 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 29 20 69 6e  _autocommit() in
2fbc0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
2fbd0 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a 20 7a  non-zero or.** z
2fbe0 65 72 6f 20 69 66 20 74 68 65 20 67 69 76 65 6e  ero if the given
2fbf0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
2fc00 74 69 6f 6e 20 69 73 20 6f 72 20 69 73 20 6e 6f  tion is or is no
2fc10 74 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20  t in autocommit 
2fc20 6d 6f 64 65 2c 0a 2a 2a 20 72 65 73 70 65 63 74  mode,.** respect
2fc30 69 76 65 6c 79 2e 20 20 41 75 74 6f 63 6f 6d 6d  ively.  Autocomm
2fc40 69 74 20 6d 6f 64 65 20 69 73 20 6f 6e 20 62 79  it mode is on by
2fc50 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 20 41 75 74   default..** Aut
2fc60 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73 20  ocommit mode is 
2fc70 64 69 73 61 62 6c 65 64 20 62 79 20 61 20 5b 42  disabled by a [B
2fc80 45 47 49 4e 5d 20 73 74 61 74 65 6d 65 6e 74 2e  EGIN] statement.
2fc90 0a 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d  .** Autocommit m
2fca0 6f 64 65 20 69 73 20 72 65 2d 65 6e 61 62 6c 65  ode is re-enable
2fcb0 64 20 62 79 20 61 20 5b 43 4f 4d 4d 49 54 5d 20  d by a [COMMIT] 
2fcc0 6f 72 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a  or [ROLLBACK]..*
2fcd0 2a 0a 2a 2a 20 49 66 20 63 65 72 74 61 69 6e 20  *.** If certain 
2fce0 6b 69 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 20  kinds of errors 
2fcf0 6f 63 63 75 72 20 6f 6e 20 61 20 73 74 61 74 65  occur on a state
2fd00 6d 65 6e 74 20 77 69 74 68 69 6e 20 61 20 6d 75  ment within a mu
2fd10 6c 74 69 2d 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  lti-statement.**
2fd20 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 28 65 72   transaction (er
2fd30 72 6f 72 73 20 69 6e 63 6c 75 64 69 6e 67 20 5b  rors including [
2fd40 53 51 4c 49 54 45 5f 46 55 4c 4c 5d 2c 20 5b 53  SQLITE_FULL], [S
2fd50 51 4c 49 54 45 5f 49 4f 45 52 52 5d 2c 0a 2a 2a  QLITE_IOERR],.**
2fd60 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2c   [SQLITE_NOMEM],
2fd70 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 2c 20   [SQLITE_BUSY], 
2fd80 61 6e 64 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45  and [SQLITE_INTE
2fd90 52 52 55 50 54 5d 29 20 74 68 65 6e 20 74 68 65  RRUPT]) then the
2fda0 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  .** transaction 
2fdb0 6d 69 67 68 74 20 62 65 20 72 6f 6c 6c 65 64 20  might be rolled 
2fdc0 62 61 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c  back automatical
2fdd0 6c 79 2e 20 20 54 68 65 20 6f 6e 6c 79 20 77 61  ly.  The only wa
2fde0 79 20 74 6f 0a 2a 2a 20 66 69 6e 64 20 6f 75 74  y to.** find out
2fdf0 20 77 68 65 74 68 65 72 20 53 51 4c 69 74 65 20   whether SQLite 
2fe00 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 6f  automatically ro
2fe10 6c 6c 65 64 20 62 61 63 6b 20 74 68 65 20 74 72  lled back the tr
2fe20 61 6e 73 61 63 74 69 6f 6e 20 61 66 74 65 72 0a  ansaction after.
2fe30 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20 74  ** an error is t
2fe40 6f 20 75 73 65 20 74 68 69 73 20 66 75 6e 63 74  o use this funct
2fe50 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ion..**.** If an
2fe60 6f 74 68 65 72 20 74 68 72 65 61 64 20 63 68 61  other thread cha
2fe70 6e 67 65 73 20 74 68 65 20 61 75 74 6f 63 6f 6d  nges the autocom
2fe80 6d 69 74 20 73 74 61 74 75 73 20 6f 66 20 74 68  mit status of th
2fe90 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f  e database.** co
2fea0 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74  nnection while t
2feb0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72  his routine is r
2fec0 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 20 74 68 65  unning, then the
2fed0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a   return value.**
2fee0 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a   is undefined..*
2fef0 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74  *.** Requirement
2ff00 73 3a 20 5b 48 31 32 39 33 31 5d 20 5b 48 31 32  s: [H12931] [H12
2ff10 39 33 32 5d 20 5b 48 31 32 39 33 33 5d 20 5b 48  932] [H12933] [H
2ff20 31 32 39 33 34 5d 0a 2a 2f 0a 53 51 4c 49 54 45  12934].*/.SQLITE
2ff30 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2ff40 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 28  _get_autocommit(
2ff50 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a  sqlite3*);../*.*
2ff60 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 6e 64  * CAPI3REF: Find
2ff70 20 54 68 65 20 44 61 74 61 62 61 73 65 20 48 61   The Database Ha
2ff80 6e 64 6c 65 20 4f 66 20 41 20 50 72 65 70 61 72  ndle Of A Prepar
2ff90 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31  ed Statement {H1
2ffa0 33 31 32 30 7d 20 3c 53 36 30 36 30 30 3e 0a 2a  3120} <S60600>.*
2ffb0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
2ffc0 5f 64 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65 72  _db_handle inter
2ffd0 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65  face returns the
2ffe0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
2fff0 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a  ction] handle.**
30000 20 74 6f 20 77 68 69 63 68 20 61 20 5b 70 72 65   to which a [pre
30010 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
30020 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 65 20 5b   belongs.  The [
30030 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
30040 69 6f 6e 5d 0a 2a 2a 20 72 65 74 75 72 6e 65 64  ion].** returned
30050 20 62 79 20 73 71 6c 69 74 65 33 5f 64 62 5f 68   by sqlite3_db_h
30060 61 6e 64 6c 65 20 69 73 20 74 68 65 20 73 61 6d  andle is the sam
30070 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
30080 65 63 74 69 6f 6e 5d 20 74 68 61 74 20 77 61 73  ection] that was
30090 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
300a0 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20 5b 73  ent.** to the [s
300b0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
300c0 32 28 29 5d 20 63 61 6c 6c 20 28 6f 72 20 69 74  2()] call (or it
300d0 73 20 76 61 72 69 61 6e 74 73 29 20 74 68 61 74  s variants) that
300e0 20 77 61 73 20 75 73 65 64 20 74 6f 0a 2a 2a 20   was used to.** 
300f0 63 72 65 61 74 65 20 74 68 65 20 73 74 61 74 65  create the state
30100 6d 65 6e 74 20 69 6e 20 74 68 65 20 66 69 72 73  ment in the firs
30110 74 20 70 6c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 52  t place..**.** R
30120 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31  equirements: [H1
30130 33 31 32 33 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  3123].*/.SQLITE_
30140 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c  API sqlite3 *sql
30150 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28 73  ite3_db_handle(s
30160 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 0a  qlite3_stmt*);..
30170 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
30180 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70 72  Find the next pr
30190 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
301a0 20 7b 48 31 33 31 34 30 7d 20 3c 53 36 30 36 30   {H13140} <S6060
301b0 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  0>.**.** This in
301c0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
301d0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
301e0 20 6e 65 78 74 20 5b 70 72 65 70 61 72 65 64 20   next [prepared 
301f0 73 74 61 74 65 6d 65 6e 74 5d 20 61 66 74 65 72  statement] after
30200 0a 2a 2a 20 70 53 74 6d 74 20 61 73 73 6f 63 69  .** pStmt associ
30210 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64  ated with the [d
30220 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
30230 6f 6e 5d 20 70 44 62 2e 20 20 49 66 20 70 53 74  on] pDb.  If pSt
30240 6d 74 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68  mt is NULL.** th
30250 65 6e 20 74 68 69 73 20 69 6e 74 65 72 66 61 63  en this interfac
30260 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  e returns a poin
30270 74 65 72 20 74 6f 20 74 68 65 20 66 69 72 73 74  ter to the first
30280 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
30290 65 6e 74 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65  ent.** associate
302a0 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62  d with the datab
302b0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70  ase connection p
302c0 44 62 2e 20 20 49 66 20 6e 6f 20 70 72 65 70 61  Db.  If no prepa
302d0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  red statement.**
302e0 20 73 61 74 69 73 66 69 65 73 20 74 68 65 20 63   satisfies the c
302f0 6f 6e 64 69 74 69 6f 6e 73 20 6f 66 20 74 68 69  onditions of thi
30300 73 20 72 6f 75 74 69 6e 65 2c 20 69 74 20 72 65  s routine, it re
30310 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  turns NULL..**.*
30320 2a 20 54 68 65 20 5b 64 61 74 61 62 61 73 65 20  * The [database 
30330 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 70 6f 69 6e  connection] poin
30340 74 65 72 20 44 20 69 6e 20 61 20 63 61 6c 6c 20  ter D in a call 
30350 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6e  to.** [sqlite3_n
30360 65 78 74 5f 73 74 6d 74 28 44 2c 53 29 5d 20 6d  ext_stmt(D,S)] m
30370 75 73 74 20 72 65 66 65 72 20 74 6f 20 61 6e 20  ust refer to an 
30380 6f 70 65 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a  open database.**
30390 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 6e 64 20   connection and 
303a0 69 6e 20 70 61 72 74 69 63 75 6c 61 72 20 6d 75  in particular mu
303b0 73 74 20 6e 6f 74 20 62 65 20 61 20 4e 55 4c 4c  st not be a NULL
303c0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   pointer..**.** 
303d0 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48  Requirements: [H
303e0 31 33 31 34 33 5d 20 5b 48 31 33 31 34 36 5d 20  13143] [H13146] 
303f0 5b 48 31 33 31 34 39 5d 20 5b 48 31 33 31 35 32  [H13149] [H13152
30400 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
30410 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 71  sqlite3_stmt *sq
30420 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28  lite3_next_stmt(
30430 73 71 6c 69 74 65 33 20 2a 70 44 62 2c 20 73 71  sqlite3 *pDb, sq
30440 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
30450 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  t);../*.** CAPI3
30460 52 45 46 3a 20 43 6f 6d 6d 69 74 20 41 6e 64 20  REF: Commit And 
30470 52 6f 6c 6c 62 61 63 6b 20 4e 6f 74 69 66 69 63  Rollback Notific
30480 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20  ation Callbacks 
30490 7b 48 31 32 39 35 30 7d 20 3c 53 36 30 34 30 30  {H12950} <S60400
304a0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  >.**.** The sqli
304b0 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28  te3_commit_hook(
304c0 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69  ) interface regi
304d0 73 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b  sters a callback
304e0 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  .** function to 
304f0 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65  be invoked whene
30500 76 65 72 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ver a transactio
30510 6e 20 69 73 20 5b 43 4f 4d 4d 49 54 20 7c 20 63  n is [COMMIT | c
30520 6f 6d 6d 69 74 74 65 64 5d 2e 0a 2a 2a 20 41 6e  ommitted]..** An
30530 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62  y callback set b
30540 79 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c  y a previous cal
30550 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d  l to sqlite3_com
30560 6d 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f  mit_hook().** fo
30570 72 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  r the same datab
30580 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  ase connection i
30590 73 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a  s overridden..**
305a0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 6f 6c   The sqlite3_rol
305b0 6c 62 61 63 6b 5f 68 6f 6f 6b 28 29 20 69 6e 74  lback_hook() int
305c0 65 72 66 61 63 65 20 72 65 67 69 73 74 65 72 73  erface registers
305d0 20 61 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66   a callback.** f
305e0 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e  unction to be in
305f0 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72 20 61  voked whenever a
30600 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
30610 5b 52 4f 4c 4c 42 41 43 4b 20 7c 20 72 6f 6c 6c  [ROLLBACK | roll
30620 65 64 20 62 61 63 6b 5d 2e 0a 2a 2a 20 41 6e 79  ed back]..** Any
30630 20 63 61 6c 6c 62 61 63 6b 20 73 65 74 20 62 79   callback set by
30640 20 61 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c   a previous call
30650 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6d 6d   to sqlite3_comm
30660 69 74 5f 68 6f 6f 6b 28 29 0a 2a 2a 20 66 6f 72  it_hook().** for
30670 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61   the same databa
30680 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  se connection is
30690 20 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 20   overridden..** 
306a0 54 68 65 20 70 41 72 67 20 61 72 67 75 6d 65 6e  The pArg argumen
306b0 74 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f  t is passed thro
306c0 75 67 68 20 74 6f 20 74 68 65 20 63 61 6c 6c 62  ugh to the callb
306d0 61 63 6b 2e 0a 2a 2a 20 49 66 20 74 68 65 20 63  ack..** If the c
306e0 61 6c 6c 62 61 63 6b 20 6f 6e 20 61 20 63 6f 6d  allback on a com
306f0 6d 69 74 20 68 6f 6f 6b 20 66 75 6e 63 74 69 6f  mit hook functio
30700 6e 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65  n returns non-ze
30710 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20  ro,.** then the 
30720 63 6f 6d 6d 69 74 20 69 73 20 63 6f 6e 76 65 72  commit is conver
30730 74 65 64 20 69 6e 74 6f 20 61 20 72 6f 6c 6c 62  ted into a rollb
30740 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ack..**.** If an
30750 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 20 77  other function w
30760 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65  as previously re
30770 67 69 73 74 65 72 65 64 2c 20 69 74 73 0a 2a 2a  gistered, its.**
30780 20 70 41 72 67 20 76 61 6c 75 65 20 69 73 20 72   pArg value is r
30790 65 74 75 72 6e 65 64 2e 20 20 4f 74 68 65 72 77  eturned.  Otherw
307a0 69 73 65 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  ise NULL is retu
307b0 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rned..**.** The 
307c0 63 61 6c 6c 62 61 63 6b 20 69 6d 70 6c 65 6d 65  callback impleme
307d0 6e 74 61 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74  ntation must not
307e0 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 74 68 61   do anything tha
307f0 74 20 77 69 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a  t will modify.**
30800 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
30810 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 69 6e  nnection that in
30820 76 6f 6b 65 64 20 74 68 65 20 63 61 6c 6c 62 61  voked the callba
30830 63 6b 2e 20 20 41 6e 79 20 61 63 74 69 6f 6e 73  ck.  Any actions
30840 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20 74 68  .** to modify th
30850 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
30860 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 64 65  ction must be de
30870 66 65 72 72 65 64 20 75 6e 74 69 6c 20 61 66 74  ferred until aft
30880 65 72 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c 65  er the.** comple
30890 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c  tion of the [sql
308a0 69 74 65 33 5f 73 74 65 70 28 29 5d 20 63 61 6c  ite3_step()] cal
308b0 6c 20 74 68 61 74 20 74 72 69 67 67 65 72 65 64  l that triggered
308c0 20 74 68 65 20 63 6f 6d 6d 69 74 0a 2a 2a 20 6f   the commit.** o
308d0 72 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f 6b 20  r rollback hook 
308e0 69 6e 20 74 68 65 20 66 69 72 73 74 20 70 6c 61  in the first pla
308f0 63 65 2e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  ce..** Note that
30900 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
30910 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 71 6c  e_v2()] and [sql
30920 69 74 65 33 5f 73 74 65 70 28 29 5d 20 62 6f 74  ite3_step()] bot
30930 68 20 6d 6f 64 69 66 79 20 74 68 65 69 72 0a 2a  h modify their.*
30940 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
30950 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20 6d  ctions for the m
30960 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f 64 69 66  eaning of "modif
30970 79 22 20 69 6e 20 74 68 69 73 20 70 61 72 61 67  y" in this parag
30980 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 69  raph..**.** Regi
30990 73 74 65 72 69 6e 67 20 61 20 4e 55 4c 4c 20 66  stering a NULL f
309a0 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73  unction disables
309b0 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a   the callback..*
309c0 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63 6f  *.** When the co
309d0 6d 6d 69 74 20 68 6f 6f 6b 20 63 61 6c 6c 62 61  mmit hook callba
309e0 63 6b 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  ck routine retur
309f0 6e 73 20 7a 65 72 6f 2c 20 74 68 65 20 5b 43 4f  ns zero, the [CO
30a00 4d 4d 49 54 5d 0a 2a 2a 20 6f 70 65 72 61 74 69  MMIT].** operati
30a10 6f 6e 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f  on is allowed to
30a20 20 63 6f 6e 74 69 6e 75 65 20 6e 6f 72 6d 61 6c   continue normal
30a30 6c 79 2e 20 20 49 66 20 74 68 65 20 63 6f 6d 6d  ly.  If the comm
30a40 69 74 20 68 6f 6f 6b 0a 2a 2a 20 72 65 74 75 72  it hook.** retur
30a50 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  ns non-zero, the
30a60 6e 20 74 68 65 20 5b 43 4f 4d 4d 49 54 5d 20 69  n the [COMMIT] i
30a70 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f  s converted into
30a80 20 61 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a   a [ROLLBACK]..*
30a90 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 68  * The rollback h
30aa0 6f 6f 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f  ook is invoked o
30ab0 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 20 74 68 61  n a rollback tha
30ac0 74 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 61  t results from a
30ad0 20 63 6f 6d 6d 69 74 0a 2a 2a 20 68 6f 6f 6b 20   commit.** hook 
30ae0 72 65 74 75 72 6e 69 6e 67 20 6e 6f 6e 2d 7a 65  returning non-ze
30af0 72 6f 2c 20 6a 75 73 74 20 61 73 20 69 74 20 77  ro, just as it w
30b00 6f 75 6c 64 20 62 65 20 77 69 74 68 20 61 6e 79  ould be with any
30b10 20 6f 74 68 65 72 20 72 6f 6c 6c 62 61 63 6b 2e   other rollback.
30b20 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70  .**.** For the p
30b30 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20  urposes of this 
30b40 41 50 49 2c 20 61 20 74 72 61 6e 73 61 63 74 69  API, a transacti
30b50 6f 6e 20 69 73 20 73 61 69 64 20 74 6f 20 68 61  on is said to ha
30b60 76 65 20 62 65 65 6e 0a 2a 2a 20 72 6f 6c 6c 65  ve been.** rolle
30b70 64 20 62 61 63 6b 20 69 66 20 61 6e 20 65 78 70  d back if an exp
30b80 6c 69 63 69 74 20 22 52 4f 4c 4c 42 41 43 4b 22  licit "ROLLBACK"
30b90 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 65 78   statement is ex
30ba0 65 63 75 74 65 64 2c 20 6f 72 0a 2a 2a 20 61 6e  ecuted, or.** an
30bb0 20 65 72 72 6f 72 20 6f 72 20 63 6f 6e 73 74 72   error or constr
30bc0 61 69 6e 74 20 63 61 75 73 65 73 20 61 6e 20 69  aint causes an i
30bd0 6d 70 6c 69 63 69 74 20 72 6f 6c 6c 62 61 63 6b  mplicit rollback
30be0 20 74 6f 20 6f 63 63 75 72 2e 0a 2a 2a 20 54 68   to occur..** Th
30bf0 65 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6c 6c 62  e rollback callb
30c00 61 63 6b 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b  ack is not invok
30c10 65 64 20 69 66 20 61 20 74 72 61 6e 73 61 63 74  ed if a transact
30c20 69 6f 6e 20 69 73 0a 2a 2a 20 61 75 74 6f 6d 61  ion is.** automa
30c30 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62  tically rolled b
30c40 61 63 6b 20 62 65 63 61 75 73 65 20 74 68 65 20  ack because the 
30c50 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
30c60 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a  ion is closed..*
30c70 2a 20 54 68 65 20 72 6f 6c 6c 62 61 63 6b 20 63  * The rollback c
30c80 61 6c 6c 62 61 63 6b 20 69 73 20 6e 6f 74 20 69  allback is not i
30c90 6e 76 6f 6b 65 64 20 69 66 20 61 20 74 72 61 6e  nvoked if a tran
30ca0 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 72 6f  saction is.** ro
30cb0 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61 75 73  lled back becaus
30cc0 65 20 61 20 63 6f 6d 6d 69 74 20 63 61 6c 6c 62  e a commit callb
30cd0 61 63 6b 20 72 65 74 75 72 6e 65 64 20 6e 6f 6e  ack returned non
30ce0 2d 7a 65 72 6f 2e 0a 2a 2a 20 3c 74 6f 64 6f 3e  -zero..** <todo>
30cf0 20 43 68 65 63 6b 20 6f 6e 20 74 68 69 73 20 3c   Check on this <
30d00 2f 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 53 65 65  /todo>.**.** See
30d10 20 61 6c 73 6f 20 74 68 65 20 5b 73 71 6c 69 74   also the [sqlit
30d20 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29  e3_update_hook()
30d30 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a  ] interface..**.
30d40 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a  ** Requirements:
30d50 0a 2a 2a 20 5b 48 31 32 39 35 31 5d 20 5b 48 31  .** [H12951] [H1
30d60 32 39 35 32 5d 20 5b 48 31 32 39 35 33 5d 20 5b  2952] [H12953] [
30d70 48 31 32 39 35 34 5d 20 5b 48 31 32 39 35 35 5d  H12954] [H12955]
30d80 0a 2a 2a 20 5b 48 31 32 39 36 31 5d 20 5b 48 31  .** [H12961] [H1
30d90 32 39 36 32 5d 20 5b 48 31 32 39 36 33 5d 20 5b  2962] [H12963] [
30da0 48 31 32 39 36 34 5d 0a 2a 2f 0a 53 51 4c 49 54  H12964].*/.SQLIT
30db0 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
30dc0 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28  te3_commit_hook(
30dd0 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29  sqlite3*, int(*)
30de0 28 76 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b  (void*), void*);
30df0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
30e00 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61   *sqlite3_rollba
30e10 63 6b 5f 68 6f 6f 6b 28 73 71 6c 69 74 65 33 2a  ck_hook(sqlite3*
30e20 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a  , void(*)(void *
30e30 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a 2a  ), void*);../*.*
30e40 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61  * CAPI3REF: Data
30e50 20 43 68 61 6e 67 65 20 4e 6f 74 69 66 69 63 61   Change Notifica
30e60 74 69 6f 6e 20 43 61 6c 6c 62 61 63 6b 73 20 7b  tion Callbacks {
30e70 48 31 32 39 37 30 7d 20 3c 53 36 30 34 30 30 3e  H12970} <S60400>
30e80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
30e90 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29  e3_update_hook()
30ea0 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73   interface regis
30eb0 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20  ters a callback 
30ec0 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 77 69 74 68  function.** with
30ed0 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
30ee0 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 64 65 6e 74  onnection] ident
30ef0 69 66 69 65 64 20 62 79 20 74 68 65 20 66 69 72  ified by the fir
30f00 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74  st argument.** t
30f10 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 68 65  o be invoked whe
30f20 6e 65 76 65 72 20 61 20 72 6f 77 20 69 73 20 75  never a row is u
30f30 70 64 61 74 65 64 2c 20 69 6e 73 65 72 74 65 64  pdated, inserted
30f40 20 6f 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20   or deleted..** 
30f50 41 6e 79 20 63 61 6c 6c 62 61 63 6b 20 73 65 74  Any callback set
30f60 20 62 79 20 61 20 70 72 65 76 69 6f 75 73 20 63   by a previous c
30f70 61 6c 6c 20 74 6f 20 74 68 69 73 20 66 75 6e 63  all to this func
30f80 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20  tion.** for the 
30f90 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f  same database co
30fa0 6e 6e 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72  nnection is over
30fb0 72 69 64 64 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  ridden..**.** Th
30fc0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
30fd0 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  t is a pointer t
30fe0 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 74  o the function t
30ff0 6f 20 69 6e 76 6f 6b 65 20 77 68 65 6e 20 61 0a  o invoke when a.
31000 2a 2a 20 72 6f 77 20 69 73 20 75 70 64 61 74 65  ** row is update
31010 64 2c 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64  d, inserted or d
31020 65 6c 65 74 65 64 2e 0a 2a 2a 20 54 68 65 20 66  eleted..** The f
31030 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
31040 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73   the callback is
31050 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 74   a copy of the t
31060 68 69 72 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  hird argument.**
31070 20 74 6f 20 73 71 6c 69 74 65 33 5f 75 70 64 61   to sqlite3_upda
31080 74 65 5f 68 6f 6f 6b 28 29 2e 0a 2a 2a 20 54 68  te_hook()..** Th
31090 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c 62 61 63  e second callbac
310a0 6b 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6f 6e  k argument is on
310b0 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 49 4e 53  e of [SQLITE_INS
310c0 45 52 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 44 45  ERT], [SQLITE_DE
310d0 4c 45 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53 51  LETE],.** or [SQ
310e0 4c 49 54 45 5f 55 50 44 41 54 45 5d 2c 20 64 65  LITE_UPDATE], de
310f0 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 6f  pending on the o
31100 70 65 72 61 74 69 6f 6e 20 74 68 61 74 20 63 61  peration that ca
31110 75 73 65 64 20 74 68 65 20 63 61 6c 6c 62 61 63  used the callbac
31120 6b 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b  k.** to be invok
31130 65 64 2e 0a 2a 2a 20 54 68 65 20 74 68 69 72 64  ed..** The third
31140 20 61 6e 64 20 66 6f 75 72 74 68 20 61 72 67 75   and fourth argu
31150 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 63 61 6c  ments to the cal
31160 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 20 70 6f  lback contain po
31170 69 6e 74 65 72 73 20 74 6f 20 74 68 65 0a 2a 2a  inters to the.**
31180 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 74 61   database and ta
31190 62 6c 65 20 6e 61 6d 65 20 63 6f 6e 74 61 69 6e  ble name contain
311a0 69 6e 67 20 74 68 65 20 61 66 66 65 63 74 65 64  ing the affected
311b0 20 72 6f 77 2e 0a 2a 2a 20 54 68 65 20 66 69 6e   row..** The fin
311c0 61 6c 20 63 61 6c 6c 62 61 63 6b 20 70 61 72 61  al callback para
311d0 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 72 6f  meter is the [ro
311e0 77 69 64 5d 20 6f 66 20 74 68 65 20 72 6f 77 2e  wid] of the row.
311f0 0a 2a 2a 20 49 6e 20 74 68 65 20 63 61 73 65 20  .** In the case 
31200 6f 66 20 61 6e 20 75 70 64 61 74 65 2c 20 74 68  of an update, th
31210 69 73 20 69 73 20 74 68 65 20 5b 72 6f 77 69 64  is is the [rowid
31220 5d 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61  ] after the upda
31230 74 65 20 74 61 6b 65 73 20 70 6c 61 63 65 2e 0a  te takes place..
31240 2a 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65  **.** The update
31250 20 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 69 6e 76   hook is not inv
31260 6f 6b 65 64 20 77 68 65 6e 20 69 6e 74 65 72 6e  oked when intern
31270 61 6c 20 73 79 73 74 65 6d 20 74 61 62 6c 65 73  al system tables
31280 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65 64   are.** modified
31290 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61   (i.e. sqlite_ma
312a0 73 74 65 72 20 61 6e 64 20 73 71 6c 69 74 65 5f  ster and sqlite_
312b0 73 65 71 75 65 6e 63 65 29 2e 0a 2a 2a 0a 2a 2a  sequence)..**.**
312c0 20 49 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20   In the current 
312d0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20  implementation, 
312e0 74 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 0a  the update hook.
312f0 2a 2a 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65  ** is not invoke
31300 64 20 77 68 65 6e 20 64 75 70 6c 69 63 61 74 69  d when duplicati
31310 6f 6e 20 72 6f 77 73 20 61 72 65 20 64 65 6c 65  on rows are dele
31320 74 65 64 20 62 65 63 61 75 73 65 20 6f 66 20 61  ted because of a
31330 6e 0a 2a 2a 20 5b 4f 4e 20 43 4f 4e 46 4c 49 43  n.** [ON CONFLIC
31340 54 20 7c 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20  T | ON CONFLICT 
31350 52 45 50 4c 41 43 45 5d 20 63 6c 61 75 73 65 2e  REPLACE] clause.
31360 20 20 4e 6f 72 20 69 73 20 74 68 65 20 75 70 64    Nor is the upd
31370 61 74 65 20 68 6f 6f 6b 0a 2a 2a 20 69 6e 76 6f  ate hook.** invo
31380 6b 65 64 20 77 68 65 6e 20 72 6f 77 73 20 61 72  ked when rows ar
31390 65 20 64 65 6c 65 74 65 64 20 75 73 69 6e 67 20  e deleted using 
313a0 74 68 65 20 5b 74 72 75 6e 63 61 74 65 20 6f 70  the [truncate op
313b0 74 69 6d 69 7a 61 74 69 6f 6e 5d 2e 0a 2a 2a 20  timization]..** 
313c0 54 68 65 20 65 78 63 65 70 74 69 6f 6e 73 20 64  The exceptions d
313d0 65 66 69 6e 65 64 20 69 6e 20 74 68 69 73 20 70  efined in this p
313e0 61 72 61 67 72 61 70 68 20 6d 69 67 68 74 20 63  aragraph might c
313f0 68 61 6e 67 65 20 69 6e 20 61 20 66 75 74 75 72  hange in a futur
31400 65 0a 2a 2a 20 72 65 6c 65 61 73 65 20 6f 66 20  e.** release of 
31410 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  SQLite..**.** Th
31420 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 69 6d  e update hook im
31430 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6d 75 73  plementation mus
31440 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74 68 69 6e  t not do anythin
31450 67 20 74 68 61 74 20 77 69 6c 6c 20 6d 6f 64 69  g that will modi
31460 66 79 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61  fy.** the databa
31470 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68  se connection th
31480 61 74 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 75  at invoked the u
31490 70 64 61 74 65 20 68 6f 6f 6b 2e 20 20 41 6e 79  pdate hook.  Any
314a0 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f 20 6d   actions.** to m
314b0 6f 64 69 66 79 20 74 68 65 20 64 61 74 61 62 61  odify the databa
314c0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6d 75  se connection mu
314d0 73 74 20 62 65 20 64 65 66 65 72 72 65 64 20 75  st be deferred u
314e0 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 0a 2a  ntil after the.*
314f0 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f 66 20  * completion of 
31500 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  the [sqlite3_ste
31510 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74 20 74  p()] call that t
31520 72 69 67 67 65 72 65 64 20 74 68 65 20 75 70 64  riggered the upd
31530 61 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 20 4e 6f 74  ate hook..** Not
31540 65 20 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f  e that [sqlite3_
31550 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e  prepare_v2()] an
31560 64 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  d [sqlite3_step(
31570 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74  )] both modify t
31580 68 65 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65  heir.** database
31590 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72   connections for
315a0 20 74 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20   the meaning of 
315b0 22 6d 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73  "modify" in this
315c0 20 70 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a   paragraph..**.*
315d0 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 66 75 6e  * If another fun
315e0 63 74 69 6f 6e 20 77 61 73 20 70 72 65 76 69 6f  ction was previo
315f0 75 73 6c 79 20 72 65 67 69 73 74 65 72 65 64 2c  usly registered,
31600 20 69 74 73 20 70 41 72 67 20 76 61 6c 75 65 0a   its pArg value.
31610 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  ** is returned. 
31620 20 4f 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 20   Otherwise NULL 
31630 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
31640 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65 20  ** See also the 
31650 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f  [sqlite3_commit_
31660 68 6f 6f 6b 28 29 5d 20 61 6e 64 20 5b 73 71 6c  hook()] and [sql
31670 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f  ite3_rollback_ho
31680 6f 6b 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66 61  ok()].** interfa
31690 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69  ces..**.** Requi
316a0 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 32  rements:.** [H12
316b0 39 37 31 5d 20 5b 48 31 32 39 37 33 5d 20 5b 48  971] [H12973] [H
316c0 31 32 39 37 35 5d 20 5b 48 31 32 39 37 37 5d 20  12975] [H12977] 
316d0 5b 48 31 32 39 37 39 5d 20 5b 48 31 32 39 38 31  [H12979] [H12981
316e0 5d 20 5b 48 31 32 39 38 33 5d 20 5b 48 31 32 39  ] [H12983] [H129
316f0 38 36 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  86].*/.SQLITE_AP
31700 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  I void *sqlite3_
31710 75 70 64 61 74 65 5f 68 6f 6f 6b 28 0a 20 20 73  update_hook(.  s
31720 71 6c 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64  qlite3*, .  void
31730 28 2a 29 28 76 6f 69 64 20 2a 2c 69 6e 74 20 2c  (*)(void *,int ,
31740 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 63 68 61  char const *,cha
31750 72 20 63 6f 6e 73 74 20 2a 2c 73 71 6c 69 74 65  r const *,sqlite
31760 33 5f 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69 64  3_int64),.  void
31770 2a 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  *.);../*.** CAPI
31780 33 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72 20  3REF: Enable Or 
31790 44 69 73 61 62 6c 65 20 53 68 61 72 65 64 20 50  Disable Shared P
317a0 61 67 65 72 20 43 61 63 68 65 20 7b 48 31 30 33  ager Cache {H103
317b0 33 30 7d 20 3c 53 33 30 39 30 30 3e 0a 2a 2a 20  30} <S30900>.** 
317c0 4b 45 59 57 4f 52 44 53 3a 20 7b 73 68 61 72 65  KEYWORDS: {share
317d0 64 20 63 61 63 68 65 7d 0a 2a 2a 0a 2a 2a 20 54  d cache}.**.** T
317e0 68 69 73 20 72 6f 75 74 69 6e 65 20 65 6e 61 62  his routine enab
317f0 6c 65 73 20 6f 72 20 64 69 73 61 62 6c 65 73 20  les or disables 
31800 74 68 65 20 73 68 61 72 69 6e 67 20 6f 66 20 74  the sharing of t
31810 68 65 20 64 61 74 61 62 61 73 65 20 63 61 63 68  he database cach
31820 65 0a 2a 2a 20 61 6e 64 20 73 63 68 65 6d 61 20  e.** and schema 
31830 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20  data structures 
31840 62 65 74 77 65 65 6e 20 5b 64 61 74 61 62 61 73  between [databas
31850 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7c 20 63  e connection | c
31860 6f 6e 6e 65 63 74 69 6f 6e 73 5d 0a 2a 2a 20 74  onnections].** t
31870 6f 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  o the same datab
31880 61 73 65 2e 20 53 68 61 72 69 6e 67 20 69 73 20  ase. Sharing is 
31890 65 6e 61 62 6c 65 64 20 69 66 20 74 68 65 20 61  enabled if the a
318a0 72 67 75 6d 65 6e 74 20 69 73 20 74 72 75 65 0a  rgument is true.
318b0 2a 2a 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20  ** and disabled 
318c0 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  if the argument 
318d0 69 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20  is false..**.** 
318e0 43 61 63 68 65 20 73 68 61 72 69 6e 67 20 69 73  Cache sharing is
318f0 20 65 6e 61 62 6c 65 64 20 61 6e 64 20 64 69 73   enabled and dis
31900 61 62 6c 65 64 20 66 6f 72 20 61 6e 20 65 6e 74  abled for an ent
31910 69 72 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 20  ire process..** 
31920 54 68 69 73 20 69 73 20 61 20 63 68 61 6e 67 65  This is a change
31930 20 61 73 20 6f 66 20 53 51 4c 69 74 65 20 76 65   as of SQLite ve
31940 72 73 69 6f 6e 20 33 2e 35 2e 30 2e 20 49 6e 20  rsion 3.5.0. In 
31950 70 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 20 6f  prior versions o
31960 66 20 53 51 4c 69 74 65 2c 0a 2a 2a 20 73 68 61  f SQLite,.** sha
31970 72 69 6e 67 20 77 61 73 20 65 6e 61 62 6c 65 64  ring was enabled
31980 20 6f 72 20 64 69 73 61 62 6c 65 64 20 66 6f 72   or disabled for
31990 20 65 61 63 68 20 74 68 72 65 61 64 20 73 65 70   each thread sep
319a0 61 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54  arately..**.** T
319b0 68 65 20 63 61 63 68 65 20 73 68 61 72 69 6e 67  he cache sharing
319c0 20 6d 6f 64 65 20 73 65 74 20 62 79 20 74 68 69   mode set by thi
319d0 73 20 69 6e 74 65 72 66 61 63 65 20 65 66 66 65  s interface effe
319e0 63 74 73 20 61 6c 6c 20 73 75 62 73 65 71 75 65  cts all subseque
319f0 6e 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 5b  nt.** calls to [
31a00 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c  sqlite3_open()],
31a10 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76   [sqlite3_open_v
31a20 32 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74  2()], and [sqlit
31a30 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a  e3_open16()]..**
31a40 20 45 78 69 73 74 69 6e 67 20 64 61 74 61 62 61   Existing databa
31a50 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63  se connections c
31a60 6f 6e 74 69 6e 75 65 20 75 73 65 20 74 68 65 20  ontinue use the 
31a70 73 68 61 72 69 6e 67 20 6d 6f 64 65 0a 2a 2a 20  sharing mode.** 
31a80 74 68 61 74 20 77 61 73 20 69 6e 20 65 66 66 65  that was in effe
31a90 63 74 20 61 74 20 74 68 65 20 74 69 6d 65 20 74  ct at the time t
31aa0 68 65 79 20 77 65 72 65 20 6f 70 65 6e 65 64 2e  hey were opened.
31ab0 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74  .**.** Virtual t
31ac0 61 62 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20  ables cannot be 
31ad0 75 73 65 64 20 77 69 74 68 20 61 20 73 68 61 72  used with a shar
31ae0 65 64 20 63 61 63 68 65 2e 20 20 57 68 65 6e 20  ed cache.  When 
31af0 73 68 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20  shared.** cache 
31b00 69 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 20  is enabled, the 
31b10 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
31b20 6d 6f 64 75 6c 65 28 29 5d 20 41 50 49 20 75 73  module()] API us
31b30 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a  ed to register.*
31b40 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  * virtual tables
31b50 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 74   will always ret
31b60 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a  urn an error..**
31b70 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
31b80 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
31b90 5f 4f 4b 5d 20 69 66 20 73 68 61 72 65 64 20 63  _OK] if shared c
31ba0 61 63 68 65 20 77 61 73 20 65 6e 61 62 6c 65 64  ache was enabled
31bb0 20 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20   or disabled.** 
31bc0 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 41  successfully.  A
31bd0 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69  n [error code] i
31be0 73 20 72 65 74 75 72 6e 65 64 20 6f 74 68 65 72  s returned other
31bf0 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72  wise..**.** Shar
31c00 65 64 20 63 61 63 68 65 20 69 73 20 64 69 73 61  ed cache is disa
31c10 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e  bled by default.
31c20 20 42 75 74 20 74 68 69 73 20 6d 69 67 68 74 20   But this might 
31c30 63 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 66 75 74  change in.** fut
31c40 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20  ure releases of 
31c50 53 51 4c 69 74 65 2e 20 20 41 70 70 6c 69 63 61  SQLite.  Applica
31c60 74 69 6f 6e 73 20 74 68 61 74 20 63 61 72 65 20  tions that care 
31c70 61 62 6f 75 74 20 73 68 61 72 65 64 0a 2a 2a 20  about shared.** 
31c80 63 61 63 68 65 20 73 65 74 74 69 6e 67 20 73 68  cache setting sh
31c90 6f 75 6c 64 20 73 65 74 20 69 74 20 65 78 70 6c  ould set it expl
31ca0 69 63 69 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 53 65  icitly..**.** Se
31cb0 65 20 41 6c 73 6f 3a 20 20 5b 53 51 4c 69 74 65  e Also:  [SQLite
31cc0 20 53 68 61 72 65 64 2d 43 61 63 68 65 20 4d 6f   Shared-Cache Mo
31cd0 64 65 5d 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  de].**.** Requir
31ce0 65 6d 65 6e 74 73 3a 20 5b 48 31 30 33 33 31 5d  ements: [H10331]
31cf0 20 5b 48 31 30 33 33 36 5d 20 5b 48 31 30 33 33   [H10336] [H1033
31d00 37 5d 20 5b 48 31 30 33 33 39 5d 0a 2a 2f 0a 53  7] [H10339].*/.S
31d10 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
31d20 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61  lite3_enable_sha
31d30 72 65 64 5f 63 61 63 68 65 28 69 6e 74 29 3b 0a  red_cache(int);.
31d40 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
31d50 20 41 74 74 65 6d 70 74 20 54 6f 20 46 72 65 65   Attempt To Free
31d60 20 48 65 61 70 20 4d 65 6d 6f 72 79 20 7b 48 31   Heap Memory {H1
31d70 37 33 34 30 7d 20 3c 53 33 30 32 32 30 3e 0a 2a  7340} <S30220>.*
31d80 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
31d90 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28  _release_memory(
31da0 29 20 69 6e 74 65 72 66 61 63 65 20 61 74 74 65  ) interface atte
31db0 6d 70 74 73 20 74 6f 20 66 72 65 65 20 4e 20 62  mpts to free N b
31dc0 79 74 65 73 0a 2a 2a 20 6f 66 20 68 65 61 70 20  ytes.** of heap 
31dd0 6d 65 6d 6f 72 79 20 62 79 20 64 65 61 6c 6c 6f  memory by deallo
31de0 63 61 74 69 6e 67 20 6e 6f 6e 2d 65 73 73 65 6e  cating non-essen
31df0 74 69 61 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  tial memory allo
31e00 63 61 74 69 6f 6e 73 0a 2a 2a 20 68 65 6c 64 20  cations.** held 
31e10 62 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20  by the database 
31e20 6c 69 62 72 61 72 79 2e 20 7b 45 4e 44 7d 20 20  library. {END}  
31e30 4d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 20 63  Memory used to c
31e40 61 63 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a  ache database.**
31e50 20 70 61 67 65 73 20 74 6f 20 69 6d 70 72 6f 76   pages to improv
31e60 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 73  e performance is
31e70 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 6e   an example of n
31e80 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d  on-essential mem
31e90 6f 72 79 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ory..** sqlite3_
31ea0 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29  release_memory()
31eb0 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
31ec0 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74  ber of bytes act
31ed0 75 61 6c 6c 79 20 66 72 65 65 64 2c 0a 2a 2a 20  ually freed,.** 
31ee0 77 68 69 63 68 20 6d 69 67 68 74 20 62 65 20 6d  which might be m
31ef0 6f 72 65 20 6f 72 20 6c 65 73 73 20 74 68 61 6e  ore or less than
31f00 20 74 68 65 20 61 6d 6f 75 6e 74 20 72 65 71 75   the amount requ
31f10 65 73 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  ested..**.** Req
31f20 75 69 72 65 6d 65 6e 74 73 3a 20 5b 48 31 37 33  uirements: [H173
31f30 34 31 5d 20 5b 48 31 37 33 34 32 5d 0a 2a 2f 0a  41] [H17342].*/.
31f40 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
31f50 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
31f60 65 6d 6f 72 79 28 69 6e 74 29 3b 0a 0a 2f 2a 0a  emory(int);../*.
31f70 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6d 70  ** CAPI3REF: Imp
31f80 6f 73 65 20 41 20 4c 69 6d 69 74 20 4f 6e 20 48  ose A Limit On H
31f90 65 61 70 20 53 69 7a 65 20 7b 48 31 37 33 35 30  eap Size {H17350
31fa0 7d 20 3c 53 33 30 32 32 30 3e 0a 2a 2a 0a 2a 2a  } <S30220>.**.**
31fb0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 6f 66   The sqlite3_sof
31fc0 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 29 20 69  t_heap_limit() i
31fd0 6e 74 65 72 66 61 63 65 20 70 6c 61 63 65 73 20  nterface places 
31fe0 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 74 0a 2a  a "soft" limit.*
31ff0 2a 20 6f 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20  * on the amount 
32000 6f 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 74  of heap memory t
32010 68 61 74 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63  hat may be alloc
32020 61 74 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a  ated by SQLite..
32030 2a 2a 20 49 66 20 61 6e 20 69 6e 74 65 72 6e 61  ** If an interna
32040 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20  l allocation is 
32050 72 65 71 75 65 73 74 65 64 20 74 68 61 74 20 77  requested that w
32060 6f 75 6c 64 20 65 78 63 65 65 64 20 74 68 65 0a  ould exceed the.
32070 2a 2a 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d  ** soft heap lim
32080 69 74 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c  it, [sqlite3_rel
32090 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 69  ease_memory()] i
320a0 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 65 20 6f 72  s invoked one or
320b0 0a 2a 2a 20 6d 6f 72 65 20 74 69 6d 65 73 20 74  .** more times t
320c0 6f 20 66 72 65 65 20 75 70 20 73 6f 6d 65 20 73  o free up some s
320d0 70 61 63 65 20 62 65 66 6f 72 65 20 74 68 65 20  pace before the 
320e0 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 70 65  allocation is pe
320f0 72 66 6f 72 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  rformed..**.** T
32100 68 65 20 6c 69 6d 69 74 20 69 73 20 63 61 6c 6c  he limit is call
32110 65 64 20 22 73 6f 66 74 22 2c 20 62 65 63 61 75  ed "soft", becau
32120 73 65 20 69 66 20 5b 73 71 6c 69 74 65 33 5f 72  se if [sqlite3_r
32130 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d  elease_memory()]
32140 0a 2a 2a 20 63 61 6e 6e 6f 74 20 66 72 65 65 20  .** cannot free 
32150 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d 6f 72  sufficient memor
32160 79 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 65  y to prevent the
32170 20 6c 69 6d 69 74 20 66 72 6f 6d 20 62 65 69 6e   limit from bein
32180 67 20 65 78 63 65 65 64 65 64 2c 0a 2a 2a 20 74  g exceeded,.** t
32190 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c  he memory is all
321a0 6f 63 61 74 65 64 20 61 6e 79 77 61 79 20 61 6e  ocated anyway an
321b0 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f 70  d the current op
321c0 65 72 61 74 69 6f 6e 20 70 72 6f 63 65 65 64 73  eration proceeds
321d0 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 67 61 74 69  ..**.** A negati
321e0 76 65 20 6f 72 20 7a 65 72 6f 20 76 61 6c 75 65  ve or zero value
321f0 20 66 6f 72 20 4e 20 6d 65 61 6e 73 20 74 68 61   for N means tha
32200 74 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 6f  t there is no so
32210 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20 61 6e  ft heap limit an
32220 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65  d.** [sqlite3_re
32230 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29 5d 20  lease_memory()] 
32240 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 20 63 61 6c  will only be cal
32250 6c 65 64 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20  led when memory 
32260 69 73 20 65 78 68 61 75 73 74 65 64 2e 0a 2a 2a  is exhausted..**
32270 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c   The default val
32280 75 65 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20  ue for the soft 
32290 68 65 61 70 20 6c 69 6d 69 74 20 69 73 20 7a 65  heap limit is ze
322a0 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  ro..**.** SQLite
322b0 20 6d 61 6b 65 73 20 61 20 62 65 73 74 20 65 66   makes a best ef
322c0 66 6f 72 74 20 74 6f 20 68 6f 6e 6f 72 20 74 68  fort to honor th
322d0 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69  e soft heap limi
322e0 74 2e 0a 2a 2a 20 42 75 74 20 69 66 20 74 68 65  t..** But if the
322f0 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74   soft heap limit
32300 20 63 61 6e 6e 6f 74 20 62 65 20 68 6f 6e 6f 72   cannot be honor
32310 65 64 2c 20 65 78 65 63 75 74 69 6f 6e 20 77 69  ed, execution wi
32320 6c 6c 0a 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 77  ll.** continue w
32330 69 74 68 6f 75 74 20 65 72 72 6f 72 20 6f 72 20  ithout error or 
32340 6e 6f 74 69 66 69 63 61 74 69 6f 6e 2e 20 20 54  notification.  T
32350 68 69 73 20 69 73 20 77 68 79 20 74 68 65 20 6c  his is why the l
32360 69 6d 69 74 20 69 73 0a 2a 2a 20 63 61 6c 6c 65  imit is.** calle
32370 64 20 61 20 22 73 6f 66 74 22 20 6c 69 6d 69 74  d a "soft" limit
32380 2e 20 20 49 74 20 69 73 20 61 64 76 69 73 6f 72  .  It is advisor
32390 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 72  y only..**.** Pr
323a0 69 6f 72 20 74 6f 20 53 51 4c 69 74 65 20 76 65  ior to SQLite ve
323b0 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 69  rsion 3.5.0, thi
323c0 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63  s routine only c
323d0 6f 6e 73 74 72 61 69 6e 65 64 20 74 68 65 20 6d  onstrained the m
323e0 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74  emory.** allocat
323f0 65 64 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74  ed by a single t
32400 68 72 65 61 64 20 2d 20 74 68 65 20 73 61 6d 65  hread - the same
32410 20 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 68   thread in which
32420 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
32430 20 72 75 6e 73 2e 20 20 42 65 67 69 6e 6e 69 6e   runs.  Beginnin
32440 67 20 77 69 74 68 20 53 51 4c 69 74 65 20 76 65  g with SQLite ve
32450 72 73 69 6f 6e 20 33 2e 35 2e 30 2c 20 74 68 65  rsion 3.5.0, the
32460 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74   soft heap limit
32470 20 69 73 0a 2a 2a 20 61 70 70 6c 69 65 64 20 74   is.** applied t
32480 6f 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20 54  o all threads. T
32490 68 65 20 76 61 6c 75 65 20 73 70 65 63 69 66 69  he value specifi
324a0 65 64 20 66 6f 72 20 74 68 65 20 73 6f 66 74 20  ed for the soft 
324b0 68 65 61 70 20 6c 69 6d 69 74 0a 2a 2a 20 69 73  heap limit.** is
324c0 20 61 6e 20 75 70 70 65 72 20 62 6f 75 6e 64 20   an upper bound 
324d0 6f 6e 20 74 68 65 20 74 6f 74 61 6c 20 6d 65 6d  on the total mem
324e0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  ory allocation f
324f0 6f 72 20 61 6c 6c 20 74 68 72 65 61 64 73 2e 20  or all threads. 
32500 49 6e 0a 2a 2a 20 76 65 72 73 69 6f 6e 20 33 2e  In.** version 3.
32510 35 2e 30 20 74 68 65 72 65 20 69 73 20 6e 6f 20  5.0 there is no 
32520 6d 65 63 68 61 6e 69 73 6d 20 66 6f 72 20 6c 69  mechanism for li
32530 6d 69 74 69 6e 67 20 74 68 65 20 68 65 61 70 20  miting the heap 
32540 75 73 61 67 65 20 66 6f 72 0a 2a 2a 20 69 6e 64  usage for.** ind
32550 69 76 69 64 75 61 6c 20 74 68 72 65 61 64 73 2e  ividual threads.
32560 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65  .**.** Requireme
32570 6e 74 73 3a 0a 2a 2a 20 5b 48 31 36 33 35 31 5d  nts:.** [H16351]
32580 20 5b 48 31 36 33 35 32 5d 20 5b 48 31 36 33 35   [H16352] [H1635
32590 33 5d 20 5b 48 31 36 33 35 34 5d 20 5b 48 31 36  3] [H16354] [H16
325a0 33 35 35 5d 20 5b 48 31 36 33 35 38 5d 0a 2a 2f  355] [H16358].*/
325b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
325c0 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65   sqlite3_soft_he
325d0 61 70 5f 6c 69 6d 69 74 28 69 6e 74 29 3b 0a 0a  ap_limit(int);..
325e0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
325f0 45 78 74 72 61 63 74 20 4d 65 74 61 64 61 74 61  Extract Metadata
32600 20 41 62 6f 75 74 20 41 20 43 6f 6c 75 6d 6e 20   About A Column 
32610 4f 66 20 41 20 54 61 62 6c 65 20 7b 48 31 32 38  Of A Table {H128
32620 35 30 7d 20 3c 53 36 30 33 30 30 3e 0a 2a 2a 0a  50} <S60300>.**.
32630 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
32640 72 65 74 75 72 6e 73 20 6d 65 74 61 64 61 74 61  returns metadata
32650 20 61 62 6f 75 74 20 61 20 73 70 65 63 69 66 69   about a specifi
32660 63 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 73 70  c column of a sp
32670 65 63 69 66 69 63 0a 2a 2a 20 64 61 74 61 62 61  ecific.** databa
32680 73 65 20 74 61 62 6c 65 20 61 63 63 65 73 73 69  se table accessi
32690 62 6c 65 20 75 73 69 6e 67 20 74 68 65 20 5b 64  ble using the [d
326a0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
326b0 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 70 61  on] handle.** pa
326c0 73 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73  ssed as the firs
326d0 74 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d  t function argum
326e0 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ent..**.** The c
326f0 6f 6c 75 6d 6e 20 69 73 20 69 64 65 6e 74 69 66  olumn is identif
32700 69 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e  ied by the secon
32710 64 2c 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75  d, third and fou
32720 72 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74  rth parameters t
32730 6f 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69  o.** this functi
32740 6f 6e 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70  on. The second p
32750 61 72 61 6d 65 74 65 72 20 69 73 20 65 69 74 68  arameter is eith
32760 65 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  er the name of t
32770 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 28  he database.** (
32780 69 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65  i.e. "main", "te
32790 6d 70 22 20 6f 72 20 61 6e 20 61 74 74 61 63 68  mp" or an attach
327a0 65 64 20 64 61 74 61 62 61 73 65 29 20 63 6f 6e  ed database) con
327b0 74 61 69 6e 69 6e 67 20 74 68 65 20 73 70 65 63  taining the spec
327c0 69 66 69 65 64 0a 2a 2a 20 74 61 62 6c 65 20 6f  ified.** table o
327d0 72 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73  r NULL. If it is
327e0 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20   NULL, then all 
327f0 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
32800 65 73 20 61 72 65 20 73 65 61 72 63 68 65 64 0a  es are searched.
32810 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  ** for the table
32820 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20   using the same 
32830 61 6c 67 6f 72 69 74 68 6d 20 75 73 65 64 20 62  algorithm used b
32840 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 65  y the database e
32850 6e 67 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 73 6f  ngine to.** reso
32860 6c 76 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20  lve unqualified 
32870 74 61 62 6c 65 20 72 65 66 65 72 65 6e 63 65 73  table references
32880 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72  ..**.** The thir
32890 64 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72  d and fourth par
328a0 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20  ameters to this 
328b0 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65  function are the
328c0 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d   table and colum
328d0 6e 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65  n.** name of the
328e0 20 64 65 73 69 72 65 64 20 63 6f 6c 75 6d 6e 2c   desired column,
328f0 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 4e   respectively. N
32900 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20  either of these 
32910 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6d 61  parameters.** ma
32920 79 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  y be NULL..**.**
32930 20 4d 65 74 61 64 61 74 61 20 69 73 20 72 65 74   Metadata is ret
32940 75 72 6e 65 64 20 62 79 20 77 72 69 74 69 6e 67  urned by writing
32950 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c   to the memory l
32960 6f 63 61 74 69 6f 6e 73 20 70 61 73 73 65 64 20  ocations passed 
32970 61 73 20 74 68 65 20 35 74 68 0a 2a 2a 20 61 6e  as the 5th.** an
32980 64 20 73 75 62 73 65 71 75 65 6e 74 20 70 61 72  d subsequent par
32990 61 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20  ameters to this 
329a0 66 75 6e 63 74 69 6f 6e 2e 20 41 6e 79 20 6f 66  function. Any of
329b0 20 74 68 65 73 65 20 61 72 67 75 6d 65 6e 74 73   these arguments
329c0 20 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2c   may be.** NULL,
329d0 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74   in which case t
329e0 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
329f0 20 65 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 74 61   element of meta
32a00 64 61 74 61 20 69 73 20 6f 6d 69 74 74 65 64 2e  data is omitted.
32a10 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f  .**.** <blockquo
32a20 74 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f  te>.** <table bo
32a30 72 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72  rder="1">.** <tr
32a40 3e 3c 74 68 3e 20 50 61 72 61 6d 65 74 65 72 20  ><th> Parameter 
32a50 3c 74 68 3e 20 4f 75 74 70 75 74 3c 62 72 3e 54  <th> Output<br>T
32a60 79 70 65 20 3c 74 68 3e 20 20 44 65 73 63 72 69  ype <th>  Descri
32a70 70 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e  ption.**.** <tr>
32a80 3c 74 64 3e 20 35 74 68 20 3c 74 64 3e 20 63 6f  <td> 5th <td> co
32a90 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 44  nst char* <td> D
32aa0 61 74 61 20 74 79 70 65 0a 2a 2a 20 3c 74 72 3e  ata type.** <tr>
32ab0 3c 74 64 3e 20 36 74 68 20 3c 74 64 3e 20 63 6f  <td> 6th <td> co
32ac0 6e 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 4e  nst char* <td> N
32ad0 61 6d 65 20 6f 66 20 64 65 66 61 75 6c 74 20 63  ame of default c
32ae0 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
32af0 65 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 37 74  e.** <tr><td> 7t
32b00 68 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20  h <td> int      
32b10 20 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20     <td> True if 
32b20 63 6f 6c 75 6d 6e 20 68 61 73 20 61 20 4e 4f 54  column has a NOT
32b30 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74   NULL constraint
32b40 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 38 74 68  .** <tr><td> 8th
32b50 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20   <td> int       
32b60 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63    <td> True if c
32b70 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66  olumn is part of
32b80 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59   the PRIMARY KEY
32b90 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 39 74 68  .** <tr><td> 9th
32ba0 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20   <td> int       
32bb0 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63    <td> True if c
32bc0 6f 6c 75 6d 6e 20 69 73 20 5b 41 55 54 4f 49 4e  olumn is [AUTOIN
32bd0 43 52 45 4d 45 4e 54 5d 0a 2a 2a 20 3c 2f 74 61  CREMENT].** </ta
32be0 62 6c 65 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71  ble>.** </blockq
32bf0 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  uote>.**.** The 
32c00 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74  memory pointed t
32c10 6f 20 62 79 20 74 68 65 20 63 68 61 72 61 63 74  o by the charact
32c20 65 72 20 70 6f 69 6e 74 65 72 73 20 72 65 74 75  er pointers retu
32c30 72 6e 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20  rned for the.** 
32c40 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65  declaration type
32c50 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   and collation s
32c60 65 71 75 65 6e 63 65 20 69 73 20 76 61 6c 69 64  equence is valid
32c70 20 6f 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 20   only until the 
32c80 6e 65 78 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20  next.** call to 
32c90 61 6e 79 20 53 51 4c 69 74 65 20 41 50 49 20 66  any SQLite API f
32ca0 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49  unction..**.** I
32cb0 66 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  f the specified 
32cc0 74 61 62 6c 65 20 69 73 20 61 63 74 75 61 6c 6c  table is actuall
32cd0 79 20 61 20 76 69 65 77 2c 20 61 6e 20 5b 65 72  y a view, an [er
32ce0 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74  ror code] is ret
32cf0 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  urned..**.** If 
32d00 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63 6f  the specified co
32d10 6c 75 6d 6e 20 69 73 20 22 72 6f 77 69 64 22 2c  lumn is "rowid",
32d20 20 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77 69   "oid" or "_rowi
32d30 64 5f 22 20 61 6e 64 20 61 6e 0a 2a 2a 20 5b 49  d_" and an.** [I
32d40 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
32d50 45 59 5d 20 63 6f 6c 75 6d 6e 20 68 61 73 20 62  EY] column has b
32d60 65 65 6e 20 65 78 70 6c 69 63 69 74 6c 79 20 64  een explicitly d
32d70 65 63 6c 61 72 65 64 2c 20 74 68 65 6e 20 74 68  eclared, then th
32d80 65 20 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 61  e output.** para
32d90 6d 65 74 65 72 73 20 61 72 65 20 73 65 74 20 66  meters are set f
32da0 6f 72 20 74 68 65 20 65 78 70 6c 69 63 69 74 6c  or the explicitl
32db0 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75 6d  y declared colum
32dc0 6e 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  n. If there is n
32dd0 6f 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79 20  o.** explicitly 
32de0 64 65 63 6c 61 72 65 64 20 5b 49 4e 54 45 47 45  declared [INTEGE
32df0 52 20 50 52 49 4d 41 52 59 20 4b 45 59 5d 20 63  R PRIMARY KEY] c
32e00 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 74 68 65 20  olumn, then the 
32e10 6f 75 74 70 75 74 0a 2a 2a 20 70 61 72 61 6d 65  output.** parame
32e20 74 65 72 73 20 61 72 65 20 73 65 74 20 61 73 20  ters are set as 
32e30 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c  follows:.**.** <
32e40 70 72 65 3e 0a 2a 2a 20 20 20 20 20 64 61 74 61  pre>.**     data
32e50 20 74 79 70 65 3a 20 22 49 4e 54 45 47 45 52 22   type: "INTEGER"
32e60 0a 2a 2a 20 20 20 20 20 63 6f 6c 6c 61 74 69 6f  .**     collatio
32e70 6e 20 73 65 71 75 65 6e 63 65 3a 20 22 42 49 4e  n sequence: "BIN
32e80 41 52 59 22 0a 2a 2a 20 20 20 20 20 6e 6f 74 20  ARY".**     not 
32e90 6e 75 6c 6c 3a 20 30 0a 2a 2a 20 20 20 20 20 70  null: 0.**     p
32ea0 72 69 6d 61 72 79 20 6b 65 79 3a 20 31 0a 2a 2a  rimary key: 1.**
32eb0 20 20 20 20 20 61 75 74 6f 20 69 6e 63 72 65 6d       auto increm
32ec0 65 6e 74 3a 20 30 0a 2a 2a 20 3c 2f 70 72 65 3e  ent: 0.** </pre>
32ed0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
32ee0 74 69 6f 6e 20 6d 61 79 20 6c 6f 61 64 20 6f 6e  tion may load on
32ef0 65 20 6f 72 20 6d 6f 72 65 20 73 63 68 65 6d 61  e or more schema
32f00 73 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20  s from database 
32f10 66 69 6c 65 73 2e 20 49 66 20 61 6e 0a 2a 2a 20  files. If an.** 
32f20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72  error occurs dur
32f30 69 6e 67 20 74 68 69 73 20 70 72 6f 63 65 73 73  ing this process
32f40 2c 20 6f 72 20 69 66 20 74 68 65 20 72 65 71 75  , or if the requ
32f50 65 73 74 65 64 20 74 61 62 6c 65 20 6f 72 20 63  ested table or c
32f60 6f 6c 75 6d 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20  olumn.** cannot 
32f70 62 65 20 66 6f 75 6e 64 2c 20 61 6e 20 5b 65 72  be found, an [er
32f80 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74  ror code] is ret
32f90 75 72 6e 65 64 20 61 6e 64 20 61 6e 20 65 72 72  urned and an err
32fa0 6f 72 20 6d 65 73 73 61 67 65 20 6c 65 66 74 0a  or message left.
32fb0 2a 2a 20 69 6e 20 74 68 65 20 5b 64 61 74 61 62  ** in the [datab
32fc0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
32fd0 28 74 6f 20 62 65 20 72 65 74 72 69 65 76 65 64  (to be retrieved
32fe0 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 65   using sqlite3_e
32ff0 72 72 6d 73 67 28 29 29 2e 0a 2a 2a 0a 2a 2a 20  rrmsg())..**.** 
33000 54 68 69 73 20 41 50 49 20 69 73 20 6f 6e 6c 79  This API is only
33010 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68   available if th
33020 65 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f  e library was co
33030 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a  mpiled with the.
33040 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ** [SQLITE_ENABL
33050 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54  E_COLUMN_METADAT
33060 41 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f  A] C-preprocesso
33070 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64  r symbol defined
33080 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
33090 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 61 62 6c  int sqlite3_tabl
330a0 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74  e_column_metadat
330b0 61 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  a(.  sqlite3 *db
330c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
330d0 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 68   /* Connection h
330e0 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  andle */.  const
330f0 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20   char *zDbName, 
33100 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
33110 73 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c 20  se name or NULL 
33120 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
33130 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20  *zTableName,    
33140 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a   /* Table name *
33150 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
33160 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20 20  zColumnName,    
33170 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a  /* Column name *
33180 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  /.  char const *
33190 2a 70 7a 44 61 74 61 54 79 70 65 2c 20 20 20 20  *pzDataType,    
331a0 2f 2a 20 4f 55 54 50 55 54 3a 20 44 65 63 6c 61  /* OUTPUT: Decla
331b0 72 65 64 20 64 61 74 61 20 74 79 70 65 20 2a 2f  red data type */
331c0 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a  .  char const **
331d0 70 7a 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20 2f  pzCollSeq,     /
331e0 2a 20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61 74  * OUTPUT: Collat
331f0 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d  ion sequence nam
33200 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f 74  e */.  int *pNot
33210 4e 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20  Null,           
33220 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72     /* OUTPUT: Tr
33230 75 65 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20 63  ue if NOT NULL c
33240 6f 6e 73 74 72 61 69 6e 74 20 65 78 69 73 74 73  onstraint exists
33250 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69 6d   */.  int *pPrim
33260 61 72 79 4b 65 79 2c 20 20 20 20 20 20 20 20 20  aryKey,         
33270 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75    /* OUTPUT: Tru
33280 65 20 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72 74  e if column part
33290 20 6f 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74 20   of PK */.  int 
332a0 2a 70 41 75 74 6f 69 6e 63 20 20 20 20 20 20 20  *pAutoinc       
332b0 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55          /* OUTPU
332c0 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d  T: True if colum
332d0 6e 20 69 73 20 61 75 74 6f 2d 69 6e 63 72 65 6d  n is auto-increm
332e0 65 6e 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  ent */.);../*.**
332f0 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 61 64 20   CAPI3REF: Load 
33300 41 6e 20 45 78 74 65 6e 73 69 6f 6e 20 7b 48 31  An Extension {H1
33310 32 36 30 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a  2600} <S20500>.*
33320 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66  *.** This interf
33330 61 63 65 20 6c 6f 61 64 73 20 61 6e 20 53 51 4c  ace loads an SQL
33340 69 74 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69  ite extension li
33350 62 72 61 72 79 20 66 72 6f 6d 20 74 68 65 20 6e  brary from the n
33360 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  amed file..**.**
33370 20 7b 48 31 32 36 30 31 7d 20 54 68 65 20 73 71   {H12601} The sq
33380 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e  lite3_load_exten
33390 73 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65  sion() interface
333a0 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 6f 61   attempts to loa
333b0 64 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  d an.**         
333c0 20 53 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f   SQLite extensio
333d0 6e 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69  n library contai
333e0 6e 65 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20  ned in the file 
333f0 7a 46 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  zFile..**.** {H1
33400 32 36 30 32 7d 20 54 68 65 20 65 6e 74 72 79 20  2602} The entry 
33410 70 6f 69 6e 74 20 69 73 20 7a 50 72 6f 63 2e 0a  point is zProc..
33420 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 33 7d 20 7a  **.** {H12603} z
33430 50 72 6f 63 20 6d 61 79 20 62 65 20 30 2c 20 69  Proc may be 0, i
33440 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65  n which case the
33450 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 65 6e 74   name of the ent
33460 72 79 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20 20  ry point.**     
33470 20 20 20 20 20 64 65 66 61 75 6c 74 73 20 74 6f       defaults to
33480 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73   "sqlite3_extens
33490 69 6f 6e 5f 69 6e 69 74 22 2e 0a 2a 2a 0a 2a 2a  ion_init"..**.**
334a0 20 7b 48 31 32 36 30 34 7d 20 54 68 65 20 73 71   {H12604} The sq
334b0 6c 69 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e  lite3_load_exten
334c0 73 69 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65  sion() interface
334d0 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a   shall return.**
334e0 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54            [SQLIT
334f0 45 5f 4f 4b 5d 20 6f 6e 20 73 75 63 63 65 73 73  E_OK] on success
33500 20 61 6e 64 20 5b 53 51 4c 49 54 45 5f 45 52 52   and [SQLITE_ERR
33510 4f 52 5d 20 69 66 20 73 6f 6d 65 74 68 69 6e 67  OR] if something
33520 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a   goes wrong..**.
33530 2a 2a 20 7b 48 31 32 36 30 35 7d 20 49 66 20 61  ** {H12605} If a
33540 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61  n error occurs a
33550 6e 64 20 70 7a 45 72 72 4d 73 67 20 69 73 20 6e  nd pzErrMsg is n
33560 6f 74 20 30 2c 20 74 68 65 6e 20 74 68 65 0a 2a  ot 0, then the.*
33570 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
33580 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69  te3_load_extensi
33590 6f 6e 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  on()] interface 
335a0 73 68 61 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f  shall attempt to
335b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 69 6c  .**          fil
335c0 6c 20 2a 70 7a 45 72 72 4d 73 67 20 77 69 74 68  l *pzErrMsg with
335d0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74   error message t
335e0 65 78 74 20 73 74 6f 72 65 64 20 69 6e 20 6d 65  ext stored in me
335f0 6d 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20  mory.**         
33600 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b   obtained from [
33610 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
33620 5d 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 63 61  ]. {END}  The ca
33630 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a  lling function.*
33640 2a 20 20 20 20 20 20 20 20 20 20 73 68 6f 75 6c  *          shoul
33650 64 20 66 72 65 65 20 74 68 69 73 20 6d 65 6d 6f  d free this memo
33660 72 79 20 62 79 20 63 61 6c 6c 69 6e 67 20 5b 73  ry by calling [s
33670 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a  qlite3_free()]..
33680 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 36 7d 20 45  **.** {H12606} E
33690 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67  xtension loading
336a0 20 6d 75 73 74 20 62 65 20 65 6e 61 62 6c 65 64   must be enabled
336b0 20 75 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20   using.**       
336c0 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62     [sqlite3_enab
336d0 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  le_load_extensio
336e0 6e 28 29 5d 20 70 72 69 6f 72 20 74 6f 20 63 61  n()] prior to ca
336f0 6c 6c 69 6e 67 20 74 68 69 73 20 41 50 49 2c 0a  lling this API,.
33700 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 74 68 65  **          othe
33710 72 77 69 73 65 20 61 6e 20 65 72 72 6f 72 20 77  rwise an error w
33720 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64 2e  ill be returned.
33730 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
33740 6e 74 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f  nt sqlite3_load_
33750 65 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c  extension(.  sql
33760 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
33770 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65     /* Load the e
33780 78 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68  xtension into th
33790 69 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  is database conn
337a0 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73  ection */.  cons
337b0 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20  t char *zFile,  
337c0 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
337d0 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20   shared library 
337e0 63 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74 65 6e  containing exten
337f0 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  sion */.  const 
33800 63 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20  char *zProc,    
33810 2f 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20  /* Entry point. 
33820 20 44 65 72 69 76 65 64 20 66 72 6f 6d 20 7a 46   Derived from zF
33830 69 6c 65 20 69 66 20 30 20 2a 2f 0a 20 20 63 68  ile if 0 */.  ch
33840 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20  ar **pzErrMsg   
33850 20 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72      /* Put error
33860 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 69 66   message here if
33870 20 6e 6f 74 20 30 20 2a 2f 0a 29 3b 0a 0a 2f 2a   not 0 */.);../*
33880 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45 6e  .** CAPI3REF: En
33890 61 62 6c 65 20 4f 72 20 44 69 73 61 62 6c 65 20  able Or Disable 
338a0 45 78 74 65 6e 73 69 6f 6e 20 4c 6f 61 64 69 6e  Extension Loadin
338b0 67 20 7b 48 31 32 36 32 30 7d 20 3c 53 32 30 35  g {H12620} <S205
338c0 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 6f 20 61 73 20  00>.**.** So as 
338d0 6e 6f 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 75  not to open secu
338e0 72 69 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c  rity holes in ol
338f0 64 65 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73  der applications
33900 20 74 68 61 74 20 61 72 65 0a 2a 2a 20 75 6e 70   that are.** unp
33910 72 65 70 61 72 65 64 20 74 6f 20 64 65 61 6c 20  repared to deal 
33920 77 69 74 68 20 65 78 74 65 6e 73 69 6f 6e 20 6c  with extension l
33930 6f 61 64 69 6e 67 2c 20 61 6e 64 20 61 73 20 61  oading, and as a
33940 20 6d 65 61 6e 73 20 6f 66 20 64 69 73 61 62 6c   means of disabl
33950 69 6e 67 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e  ing.** extension
33960 20 6c 6f 61 64 69 6e 67 20 77 68 69 6c 65 20 65   loading while e
33970 76 61 6c 75 61 74 69 6e 67 20 75 73 65 72 2d 65  valuating user-e
33980 6e 74 65 72 65 64 20 53 51 4c 2c 20 74 68 65 20  ntered SQL, the 
33990 66 6f 6c 6c 6f 77 69 6e 67 20 41 50 49 0a 2a 2a  following API.**
339a0 20 69 73 20 70 72 6f 76 69 64 65 64 20 74 6f 20   is provided to 
339b0 74 75 72 6e 20 74 68 65 20 5b 73 71 6c 69 74 65  turn the [sqlite
339c0 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  3_load_extension
339d0 28 29 5d 20 6d 65 63 68 61 6e 69 73 6d 20 6f 6e  ()] mechanism on
339e0 20 61 6e 64 20 6f 66 66 2e 0a 2a 2a 0a 2a 2a 20   and off..**.** 
339f0 45 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e  Extension loadin
33a00 67 20 69 73 20 6f 66 66 20 62 79 20 64 65 66 61  g is off by defa
33a10 75 6c 74 2e 20 53 65 65 20 74 69 63 6b 65 74 20  ult. See ticket 
33a20 23 31 38 36 33 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  #1863..**.** {H1
33a30 32 36 32 31 7d 20 43 61 6c 6c 20 74 68 65 20 73  2621} Call the s
33a40 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f  qlite3_enable_lo
33a50 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20 72  ad_extension() r
33a60 6f 75 74 69 6e 65 20 77 69 74 68 20 6f 6e 6f 66  outine with onof
33a70 66 3d 3d 31 0a 2a 2a 20 20 20 20 20 20 20 20 20  f==1.**         
33a80 20 74 6f 20 74 75 72 6e 20 65 78 74 65 6e 73 69   to turn extensi
33a90 6f 6e 20 6c 6f 61 64 69 6e 67 20 6f 6e 20 61 6e  on loading on an
33aa0 64 20 63 61 6c 6c 20 69 74 20 77 69 74 68 20 6f  d call it with o
33ab0 6e 6f 66 66 3d 3d 30 20 74 6f 20 74 75 72 6e 0a  noff==0 to turn.
33ac0 2a 2a 20 20 20 20 20 20 20 20 20 20 69 74 20 62  **          it b
33ad0 61 63 6b 20 6f 66 66 20 61 67 61 69 6e 2e 0a 2a  ack off again..*
33ae0 2a 0a 2a 2a 20 7b 48 31 32 36 32 32 7d 20 45 78  *.** {H12622} Ex
33af0 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20  tension loading 
33b00 69 73 20 6f 66 66 20 62 79 20 64 65 66 61 75 6c  is off by defaul
33b10 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
33b20 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61   int sqlite3_ena
33b30 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69  ble_load_extensi
33b40 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  on(sqlite3 *db, 
33b50 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a 0a  int onoff);../*.
33b60 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 41 75 74  ** CAPI3REF: Aut
33b70 6f 6d 61 74 69 63 61 6c 6c 79 20 4c 6f 61 64 20  omatically Load 
33b80 41 6e 20 45 78 74 65 6e 73 69 6f 6e 73 20 7b 48  An Extensions {H
33b90 31 32 36 34 30 7d 20 3c 53 32 30 35 30 30 3e 0a  12640} <S20500>.
33ba0 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20 63  **.** This API c
33bb0 61 6e 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 74  an be invoked at
33bc0 20 70 72 6f 67 72 61 6d 20 73 74 61 72 74 75 70   program startup
33bd0 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 72 65 67   in order to reg
33be0 69 73 74 65 72 0a 2a 2a 20 6f 6e 65 20 6f 72 20  ister.** one or 
33bf0 6d 6f 72 65 20 73 74 61 74 69 63 61 6c 6c 79 20  more statically 
33c00 6c 69 6e 6b 65 64 20 65 78 74 65 6e 73 69 6f 6e  linked extension
33c10 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 61  s that will be a
33c20 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 74 6f 20 61  vailable.** to a
33c30 6c 6c 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65  ll new [database
33c40 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 2e 20 7b   connections]. {
33c50 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  END}.**.** This 
33c60 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20 61  routine stores a
33c70 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
33c80 65 78 74 65 6e 73 69 6f 6e 20 69 6e 20 61 6e 20  extension in an 
33c90 61 72 72 61 79 20 74 68 61 74 20 69 73 0a 2a 2a  array that is.**
33ca0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b   obtained from [
33cb0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
33cc0 5d 2e 20 20 49 66 20 79 6f 75 20 72 75 6e 20 61  ].  If you run a
33cd0 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 20 63 68 65   memory leak che
33ce0 63 6b 65 72 0a 2a 2a 20 6f 6e 20 79 6f 75 72 20  cker.** on your 
33cf0 70 72 6f 67 72 61 6d 20 61 6e 64 20 69 74 20 72  program and it r
33d00 65 70 6f 72 74 73 20 61 20 6c 65 61 6b 20 62 65  eports a leak be
33d10 63 61 75 73 65 20 6f 66 20 74 68 69 73 20 61 72  cause of this ar
33d20 72 61 79 2c 20 69 6e 76 6f 6b 65 0a 2a 2a 20 5b  ray, invoke.** [
33d30 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75  sqlite3_reset_au
33d40 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20  to_extension()] 
33d50 70 72 69 6f 72 20 74 6f 20 73 68 75 74 64 6f 77  prior to shutdow
33d60 6e 20 74 6f 20 66 72 65 65 20 74 68 65 20 6d 65  n to free the me
33d70 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  mory..**.** {H12
33d80 36 34 31 7d 20 54 68 69 73 20 66 75 6e 63 74 69  641} This functi
33d90 6f 6e 20 72 65 67 69 73 74 65 72 73 20 61 6e 20  on registers an 
33da0 65 78 74 65 6e 73 69 6f 6e 20 65 6e 74 72 79 20  extension entry 
33db0 70 6f 69 6e 74 20 74 68 61 74 20 69 73 0a 2a 2a  point that is.**
33dc0 20 20 20 20 20 20 20 20 20 20 61 75 74 6f 6d 61            automa
33dd0 74 69 63 61 6c 6c 79 20 69 6e 76 6f 6b 65 64 20  tically invoked 
33de0 77 68 65 6e 65 76 65 72 20 61 20 6e 65 77 20 5b  whenever a new [
33df0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
33e00 69 6f 6e 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20  ion].**         
33e10 20 69 73 20 6f 70 65 6e 65 64 20 75 73 69 6e 67   is opened using
33e20 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29   [sqlite3_open()
33e30 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ], [sqlite3_open
33e40 31 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20  16()],.**       
33e50 20 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f     or [sqlite3_o
33e60 70 65 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a  pen_v2()]..**.**
33e70 20 7b 48 31 32 36 34 32 7d 20 44 75 70 6c 69 63   {H12642} Duplic
33e80 61 74 65 20 65 78 74 65 6e 73 69 6f 6e 73 20 61  ate extensions a
33e90 72 65 20 64 65 74 65 63 74 65 64 20 73 6f 20 63  re detected so c
33ea0 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74  alling this rout
33eb0 69 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ine.**          
33ec0 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 77  multiple times w
33ed0 69 74 68 20 74 68 65 20 73 61 6d 65 20 65 78 74  ith the same ext
33ee0 65 6e 73 69 6f 6e 20 69 73 20 68 61 72 6d 6c 65  ension is harmle
33ef0 73 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34  ss..**.** {H1264
33f00 33 7d 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  3} This routine 
33f10 73 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72  stores a pointer
33f20 20 74 6f 20 74 68 65 20 65 78 74 65 6e 73 69 6f   to the extensio
33f30 6e 20 69 6e 20 61 6e 20 61 72 72 61 79 0a 2a 2a  n in an array.**
33f40 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20 69            that i
33f50 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  s obtained from 
33f60 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28  [sqlite3_malloc(
33f70 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34  )]..**.** {H1264
33f80 34 7d 20 41 75 74 6f 6d 61 74 69 63 20 65 78 74  4} Automatic ext
33f90 65 6e 73 69 6f 6e 73 20 61 70 70 6c 79 20 61 63  ensions apply ac
33fa0 72 6f 73 73 20 61 6c 6c 20 74 68 72 65 61 64 73  ross all threads
33fb0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
33fc0 69 6e 74 20 73 71 6c 69 74 65 33 5f 61 75 74 6f  int sqlite3_auto
33fd0 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69 64 20  _extension(void 
33fe0 28 2a 78 45 6e 74 72 79 50 6f 69 6e 74 29 28 76  (*xEntryPoint)(v
33ff0 6f 69 64 29 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  oid));../*.** CA
34000 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 75  PI3REF: Reset Au
34010 74 6f 6d 61 74 69 63 20 45 78 74 65 6e 73 69 6f  tomatic Extensio
34020 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36 36  n Loading {H1266
34030 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a  0} <S20500>.**.*
34040 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
34050 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 70 72 65  disables all pre
34060 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72  viously register
34070 65 64 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a 20  ed automatic.** 
34080 65 78 74 65 6e 73 69 6f 6e 73 2e 20 7b 45 4e 44  extensions. {END
34090 7d 20 20 49 74 20 75 6e 64 6f 65 73 20 74 68 65  }  It undoes the
340a0 20 65 66 66 65 63 74 20 6f 66 20 61 6c 6c 20 70   effect of all p
340b0 72 69 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  rior.** [sqlite3
340c0 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28  _auto_extension(
340d0 29 5d 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20  )] calls..**.** 
340e0 7b 48 31 32 36 36 31 7d 20 54 68 69 73 20 66 75  {H12661} This fu
340f0 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73 20  nction disables 
34100 61 6c 6c 20 70 72 65 76 69 6f 75 73 6c 79 20 72  all previously r
34110 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 20 20  egistered.**    
34120 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 20        automatic 
34130 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a  extensions..**.*
34140 2a 20 7b 48 31 32 36 36 32 7d 20 54 68 69 73 20  * {H12662} This 
34150 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65  function disable
34160 73 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65  s automatic exte
34170 6e 73 69 6f 6e 73 20 69 6e 20 61 6c 6c 20 74 68  nsions in all th
34180 72 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  reads..*/.SQLITE
34190 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
341a0 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74  3_reset_auto_ext
341b0 65 6e 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 0a 2f  ension(void);../
341c0 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d  *.****** EXPERIM
341d0 45 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20  ENTAL - subject 
341e0 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75  to change withou
341f0 74 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a  t notice *******
34200 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
34210 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74  e interface to t
34220 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  he virtual-table
34230 20 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 63 75   mechanism is cu
34240 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72  rrently consider
34250 65 64 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70 65  ed.** to be expe
34260 72 69 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20 69  rimental.  The i
34270 6e 74 65 72 66 61 63 65 20 6d 69 67 68 74 20 63  nterface might c
34280 68 61 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61  hange in incompa
34290 74 69 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49  tible ways..** I
342a0 66 20 74 68 69 73 20 69 73 20 61 20 70 72 6f 62  f this is a prob
342b0 6c 65 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20  lem for you, do 
342c0 6e 6f 74 20 75 73 65 20 74 68 65 20 69 6e 74 65  not use the inte
342d0 72 66 61 63 65 20 61 74 20 74 68 69 73 20 74 69  rface at this ti
342e0 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74  me..**.** When t
342f0 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  he virtual-table
34300 20 6d 65 63 68 61 6e 69 73 6d 20 73 74 61 62 69   mechanism stabi
34310 6c 69 7a 65 73 2c 20 77 65 20 77 69 6c 6c 20 64  lizes, we will d
34320 65 63 6c 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e  eclare the.** in
34330 74 65 72 66 61 63 65 20 66 69 78 65 64 2c 20 73  terface fixed, s
34340 75 70 70 6f 72 74 20 69 74 20 69 6e 64 65 66 69  upport it indefi
34350 6e 69 74 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f  nitely, and remo
34360 76 65 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e  ve this comment.
34370 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75 63  .*/../*.** Struc
34380 74 75 72 65 73 20 75 73 65 64 20 62 79 20 74 68  tures used by th
34390 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
343a0 69 6e 74 65 72 66 61 63 65 0a 2a 2f 0a 74 79 70  interface.*/.typ
343b0 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
343c0 74 65 33 5f 76 74 61 62 20 73 71 6c 69 74 65 33  te3_vtab sqlite3
343d0 5f 76 74 61 62 3b 0a 74 79 70 65 64 65 66 20 73  _vtab;.typedef s
343e0 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
343f0 64 65 78 5f 69 6e 66 6f 20 73 71 6c 69 74 65 33  dex_info sqlite3
34400 5f 69 6e 64 65 78 5f 69 6e 66 6f 3b 0a 74 79 70  _index_info;.typ
34410 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
34420 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
34430 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
34440 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72  sor;.typedef str
34450 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  uct sqlite3_modu
34460 6c 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  le sqlite3_modul
34470 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  e;../*.** CAPI3R
34480 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c  EF: Virtual Tabl
34490 65 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 30 30  e Object {H18000
344a0 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45  } <S20400>.** KE
344b0 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f  YWORDS: sqlite3_
344c0 6d 6f 64 75 6c 65 20 7b 76 69 72 74 75 61 6c 20  module {virtual 
344d0 74 61 62 6c 65 20 6d 6f 64 75 6c 65 7d 0a 2a 2a  table module}.**
344e0 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
344f0 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75  .** This structu
34500 72 65 2c 20 73 6f 6d 65 74 69 6d 65 73 20 63 61  re, sometimes ca
34510 6c 6c 65 64 20 61 20 61 20 22 76 69 72 74 75 61  lled a a "virtua
34520 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 22 2c  l table module",
34530 20 0a 2a 2a 20 64 65 66 69 6e 65 73 20 74 68 65   .** defines the
34540 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
34550 6f 66 20 61 20 5b 76 69 72 74 75 61 6c 20 74 61  of a [virtual ta
34560 62 6c 65 73 5d 2e 20 20 0a 2a 2a 20 54 68 69 73  bles].  .** This
34570 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 73 69   structure consi
34580 73 74 73 20 6d 6f 73 74 6c 79 20 6f 66 20 6d 65  sts mostly of me
34590 74 68 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f  thods for the mo
345a0 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 69  dule..**.** A vi
345b0 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
345c0 6c 65 20 69 73 20 63 72 65 61 74 65 64 20 62 79  le is created by
345d0 20 66 69 6c 6c 69 6e 67 20 69 6e 20 61 20 70 65   filling in a pe
345e0 72 73 69 73 74 65 6e 74 0a 2a 2a 20 69 6e 73 74  rsistent.** inst
345f0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
34600 75 63 74 75 72 65 20 61 6e 64 20 70 61 73 73 69  ucture and passi
34610 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ng a pointer to 
34620 74 68 61 74 20 69 6e 73 74 61 6e 63 65 0a 2a 2a  that instance.**
34630 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 72 65   to [sqlite3_cre
34640 61 74 65 5f 6d 6f 64 75 6c 65 28 29 5d 20 6f 72  ate_module()] or
34650 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
34660 5f 6d 6f 64 75 6c 65 5f 76 32 28 29 5d 2e 0a 2a  _module_v2()]..*
34670 2a 20 54 68 65 20 72 65 67 69 73 74 72 61 74 69  * The registrati
34680 6f 6e 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64  on remains valid
34690 20 75 6e 74 69 6c 20 69 74 20 69 73 20 72 65 70   until it is rep
346a0 6c 61 63 65 64 20 62 79 20 61 20 64 69 66 66 65  laced by a diffe
346b0 72 65 6e 74 0a 2a 2a 20 6d 6f 64 75 6c 65 20 6f  rent.** module o
346c0 72 20 75 6e 74 69 6c 20 74 68 65 20 5b 64 61 74  r until the [dat
346d0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
346e0 5d 20 63 6c 6f 73 65 73 2e 20 20 54 68 65 20 63  ] closes.  The c
346f0 6f 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 69  ontent.** of thi
34700 73 20 73 74 72 75 63 74 75 72 65 20 6d 75 73 74  s structure must
34710 20 6e 6f 74 20 63 68 61 6e 67 65 20 77 68 69 6c   not change whil
34720 65 20 69 74 20 69 73 20 72 65 67 69 73 74 65 72  e it is register
34730 65 64 20 77 69 74 68 0a 2a 2a 20 61 6e 79 20 64  ed with.** any d
34740 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
34750 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71  on..*/.struct sq
34760 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20  lite3_module {. 
34770 20 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20   int iVersion;. 
34780 20 69 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28   int (*xCreate)(
34790 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a  sqlite3*, void *
347a0 70 41 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20  pAux,.          
347b0 20 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63       int argc, c
347c0 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74  onst char *const
347d0 2a 61 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20  *argv,.         
347e0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74        sqlite3_vt
347f0 61 62 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61  ab **ppVTab, cha
34800 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43  r**);.  int (*xC
34810 6f 6e 6e 65 63 74 29 28 73 71 6c 69 74 65 33 2a  onnect)(sqlite3*
34820 2c 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20  , void *pAux,.  
34830 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
34840 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61   argc, const cha
34850 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20  r *const*argv,. 
34860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
34870 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56  lite3_vtab **ppV
34880 54 61 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20  Tab, char**);.  
34890 69 6e 74 20 28 2a 78 42 65 73 74 49 6e 64 65 78  int (*xBestIndex
348a0 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  )(sqlite3_vtab *
348b0 70 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69  pVTab, sqlite3_i
348c0 6e 64 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69  ndex_info*);.  i
348d0 6e 74 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 74  nt (*xDisconnect
348e0 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  )(sqlite3_vtab *
348f0 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a  pVTab);.  int (*
34900 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65  xDestroy)(sqlite
34910 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a  3_vtab *pVTab);.
34920 20 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73    int (*xOpen)(s
34930 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54  qlite3_vtab *pVT
34940 61 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62  ab, sqlite3_vtab
34950 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73  _cursor **ppCurs
34960 6f 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c  or);.  int (*xCl
34970 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61  ose)(sqlite3_vta
34980 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e  b_cursor*);.  in
34990 74 20 28 2a 78 46 69 6c 74 65 72 29 28 73 71 6c  t (*xFilter)(sql
349a0 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
349b0 2a 2c 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63  *, int idxNum, c
349c0 6f 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53 74  onst char *idxSt
349d0 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
349e0 20 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c     int argc, sql
349f0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
34a00 76 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78  v);.  int (*xNex
34a10 74 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  t)(sqlite3_vtab_
34a20 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20  cursor*);.  int 
34a30 28 2a 78 45 6f 66 29 28 73 71 6c 69 74 65 33 5f  (*xEof)(sqlite3_
34a40 76 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20  vtab_cursor*);. 
34a50 20 69 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29 28   int (*xColumn)(
34a60 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
34a70 73 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63 6f  sor*, sqlite3_co
34a80 6e 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20 20  ntext*, int);.  
34a90 69 6e 74 20 28 2a 78 52 6f 77 69 64 29 28 73 71  int (*xRowid)(sq
34aa0 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
34ab0 72 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  r*, sqlite3_int6
34ac0 34 20 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69 6e  4 *pRowid);.  in
34ad0 74 20 28 2a 78 55 70 64 61 74 65 29 28 73 71 6c  t (*xUpdate)(sql
34ae0 69 74 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e 74  ite3_vtab *, int
34af0 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
34b00 2a 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  **, sqlite3_int6
34b10 34 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42  4 *);.  int (*xB
34b20 65 67 69 6e 29 28 73 71 6c 69 74 65 33 5f 76 74  egin)(sqlite3_vt
34b30 61 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e  ab *pVTab);.  in
34b40 74 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74  t (*xSync)(sqlit
34b50 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b  e3_vtab *pVTab);
34b60 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74  .  int (*xCommit
34b70 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  )(sqlite3_vtab *
34b80 70 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a  pVTab);.  int (*
34b90 78 52 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69 74  xRollback)(sqlit
34ba0 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b  e3_vtab *pVTab);
34bb0 0a 20 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 75  .  int (*xFindFu
34bc0 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 5f  nction)(sqlite3_
34bd0 76 74 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74  vtab *pVtab, int
34be0 20 6e 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 61   nArg, const cha
34bf0 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20  r *zName,.      
34c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34c10 20 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29   void (**pxFunc)
34c20 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
34c30 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
34c40 6c 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20  lue**),.        
34c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76                 v
34c60 6f 69 64 20 2a 2a 70 70 41 72 67 29 3b 0a 20 20  oid **ppArg);.  
34c70 69 6e 74 20 28 2a 78 52 65 6e 61 6d 65 29 28 73  int (*xRename)(s
34c80 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
34c90 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ab, const char *
34ca0 7a 4e 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  zNew);.};../*.**
34cb0 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75   CAPI3REF: Virtu
34cc0 61 6c 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e  al Table Indexin
34cd0 67 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 7b 48  g Information {H
34ce0 31 38 31 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a  18100} <S20400>.
34cf0 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c  ** KEYWORDS: sql
34d00 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a  ite3_index_info.
34d10 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
34d20 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
34d30 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72  3_index_info str
34d40 75 63 74 75 72 65 20 61 6e 64 20 69 74 73 20 73  ucture and its s
34d50 75 62 73 74 72 75 63 74 75 72 65 73 20 69 73 20  ubstructures is 
34d60 75 73 65 64 20 74 6f 0a 2a 2a 20 70 61 73 73 20  used to.** pass 
34d70 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f  information into
34d80 20 61 6e 64 20 72 65 63 65 69 76 65 20 74 68 65   and receive the
34d90 20 72 65 70 6c 79 20 66 72 6f 6d 20 74 68 65 20   reply from the 
34da0 5b 78 42 65 73 74 49 6e 64 65 78 5d 0a 2a 2a 20  [xBestIndex].** 
34db0 6d 65 74 68 6f 64 20 6f 66 20 61 20 5b 76 69 72  method of a [vir
34dc0 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
34dd0 65 5d 2e 20 20 54 68 65 20 66 69 65 6c 64 73 20  e].  The fields 
34de0 75 6e 64 65 72 20 2a 2a 49 6e 70 75 74 73 2a 2a  under **Inputs**
34df0 20 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 70 75   are the.** inpu
34e00 74 73 20 74 6f 20 78 42 65 73 74 49 6e 64 65 78  ts to xBestIndex
34e10 20 61 6e 64 20 61 72 65 20 72 65 61 64 2d 6f 6e   and are read-on
34e20 6c 79 2e 20 20 78 42 65 73 74 49 6e 64 65 78 20  ly.  xBestIndex 
34e30 69 6e 73 65 72 74 73 20 69 74 73 0a 2a 2a 20 72  inserts its.** r
34e40 65 73 75 6c 74 73 20 69 6e 74 6f 20 74 68 65 20  esults into the 
34e50 2a 2a 4f 75 74 70 75 74 73 2a 2a 20 66 69 65 6c  **Outputs** fiel
34e60 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 43  ds..**.** The aC
34e70 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61  onstraint[] arra
34e80 79 20 72 65 63 6f 72 64 73 20 57 48 45 52 45 20  y records WHERE 
34e90 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e  clause constrain
34ea0 74 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a  ts of the form:.
34eb0 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 63 6f 6c 75 6d  **.** <pre>colum
34ec0 6e 20 4f 50 20 65 78 70 72 3c 2f 70 72 65 3e 0a  n OP expr</pre>.
34ed0 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 4f 50 20 69  **.** where OP i
34ee0 73 20 3d 2c 20 26 6c 74 3b 2c 20 26 6c 74 3b 3d  s =, &lt;, &lt;=
34ef0 2c 20 26 67 74 3b 2c 20 6f 72 20 26 67 74 3b 3d  , &gt;, or &gt;=
34f00 2e 20 20 54 68 65 20 70 61 72 74 69 63 75 6c 61  .  The particula
34f10 72 20 6f 70 65 72 61 74 6f 72 20 69 73 0a 2a 2a  r operator is.**
34f20 20 73 74 6f 72 65 64 20 69 6e 20 61 43 6f 6e 73   stored in aCons
34f30 74 72 61 69 6e 74 5b 5d 2e 6f 70 2e 20 20 54 68  traint[].op.  Th
34f40 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 63  e index of the c
34f50 6f 6c 75 6d 6e 20 69 73 20 73 74 6f 72 65 64 20  olumn is stored 
34f60 69 6e 0a 2a 2a 20 61 43 6f 6e 73 74 72 61 69 6e  in.** aConstrain
34f70 74 5b 5d 2e 69 43 6f 6c 75 6d 6e 2e 20 20 61 43  t[].iColumn.  aC
34f80 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62  onstraint[].usab
34f90 6c 65 20 69 73 20 54 52 55 45 20 69 66 20 74 68  le is TRUE if th
34fa0 65 0a 2a 2a 20 65 78 70 72 20 6f 6e 20 74 68 65  e.** expr on the
34fb0 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65   right-hand side
34fc0 20 63 61 6e 20 62 65 20 65 76 61 6c 75 61 74 65   can be evaluate
34fd0 64 20 28 61 6e 64 20 74 68 75 73 20 74 68 65 20  d (and thus the 
34fe0 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 69 73  constraint.** is
34ff0 20 75 73 61 62 6c 65 29 20 61 6e 64 20 66 61 6c   usable) and fal
35000 73 65 20 69 66 20 69 74 20 63 61 6e 6e 6f 74 2e  se if it cannot.
35010 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 74 69 6d  .**.** The optim
35020 69 7a 65 72 20 61 75 74 6f 6d 61 74 69 63 61 6c  izer automatical
35030 6c 79 20 69 6e 76 65 72 74 73 20 74 65 72 6d 73  ly inverts terms
35040 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 65 78   of the form "ex
35050 70 72 20 4f 50 20 63 6f 6c 75 6d 6e 22 0a 2a 2a  pr OP column".**
35060 20 61 6e 64 20 6d 61 6b 65 73 20 6f 74 68 65 72   and makes other
35070 20 73 69 6d 70 6c 69 66 69 63 61 74 69 6f 6e 73   simplifications
35080 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 63 6c   to the WHERE cl
35090 61 75 73 65 20 69 6e 20 61 6e 20 61 74 74 65 6d  ause in an attem
350a0 70 74 20 74 6f 0a 2a 2a 20 67 65 74 20 61 73 20  pt to.** get as 
350b0 6d 61 6e 79 20 57 48 45 52 45 20 63 6c 61 75 73  many WHERE claus
350c0 65 20 74 65 72 6d 73 20 69 6e 74 6f 20 74 68 65  e terms into the
350d0 20 66 6f 72 6d 20 73 68 6f 77 6e 20 61 62 6f 76   form shown abov
350e0 65 20 61 73 20 70 6f 73 73 69 62 6c 65 2e 0a 2a  e as possible..*
350f0 2a 20 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e  * The aConstrain
35100 74 5b 5d 20 61 72 72 61 79 20 6f 6e 6c 79 20 72  t[] array only r
35110 65 70 6f 72 74 73 20 57 48 45 52 45 20 63 6c 61  eports WHERE cla
35120 75 73 65 20 74 65 72 6d 73 20 69 6e 20 74 68 65  use terms in the
35130 20 63 6f 72 72 65 63 74 0a 2a 2a 20 66 6f 72 6d   correct.** form
35140 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74   that refer to t
35150 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 76 69  he particular vi
35160 72 74 75 61 6c 20 74 61 62 6c 65 20 62 65 69 6e  rtual table bein
35170 67 20 71 75 65 72 69 65 64 2e 0a 2a 2a 0a 2a 2a  g queried..**.**
35180 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   Information abo
35190 75 74 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  ut the ORDER BY 
351a0 63 6c 61 75 73 65 20 69 73 20 73 74 6f 72 65 64  clause is stored
351b0 20 69 6e 20 61 4f 72 64 65 72 42 79 5b 5d 2e 0a   in aOrderBy[]..
351c0 2a 2a 20 45 61 63 68 20 74 65 72 6d 20 6f 66 20  ** Each term of 
351d0 61 4f 72 64 65 72 42 79 20 72 65 63 6f 72 64 73  aOrderBy records
351e0 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65   a column of the
351f0 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
35200 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 78 42 65  ..**.** The [xBe
35210 73 74 49 6e 64 65 78 5d 20 6d 65 74 68 6f 64 20  stIndex] method 
35220 6d 75 73 74 20 66 69 6c 6c 20 61 43 6f 6e 73 74  must fill aConst
35230 72 61 69 6e 74 55 73 61 67 65 5b 5d 20 77 69 74  raintUsage[] wit
35240 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a  h information.**
35250 20 61 62 6f 75 74 20 77 68 61 74 20 70 61 72 61   about what para
35260 6d 65 74 65 72 73 20 74 6f 20 70 61 73 73 20 74  meters to pass t
35270 6f 20 78 46 69 6c 74 65 72 2e 20 20 49 66 20 61  o xFilter.  If a
35280 72 67 76 49 6e 64 65 78 3e 30 20 74 68 65 6e 0a  rgvIndex>0 then.
35290 2a 2a 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e  ** the right-han
352a0 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 63 6f  d side of the co
352b0 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 43 6f 6e  rresponding aCon
352c0 73 74 72 61 69 6e 74 5b 5d 20 69 73 20 65 76 61  straint[] is eva
352d0 6c 75 61 74 65 64 0a 2a 2a 20 61 6e 64 20 62 65  luated.** and be
352e0 63 6f 6d 65 73 20 74 68 65 20 61 72 67 76 49 6e  comes the argvIn
352f0 64 65 78 2d 74 68 20 65 6e 74 72 79 20 69 6e 20  dex-th entry in 
35300 61 72 67 76 2e 20 20 49 66 20 61 43 6f 6e 73 74  argv.  If aConst
35310 72 61 69 6e 74 55 73 61 67 65 5b 5d 2e 6f 6d 69  raintUsage[].omi
35320 74 0a 2a 2a 20 69 73 20 74 72 75 65 2c 20 74 68  t.** is true, th
35330 65 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  en the constrain
35340 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20  t is assumed to 
35350 62 65 20 66 75 6c 6c 79 20 68 61 6e 64 6c 65 64  be fully handled
35360 20 62 79 20 74 68 65 0a 2a 2a 20 76 69 72 74 75   by the.** virtu
35370 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 69 73 20  al table and is 
35380 6e 6f 74 20 63 68 65 63 6b 65 64 20 61 67 61 69  not checked agai
35390 6e 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a  n by SQLite..**.
353a0 2a 2a 20 54 68 65 20 69 64 78 4e 75 6d 20 61 6e  ** The idxNum an
353b0 64 20 69 64 78 50 74 72 20 76 61 6c 75 65 73 20  d idxPtr values 
353c0 61 72 65 20 72 65 63 6f 72 64 65 64 20 61 6e 64  are recorded and
353d0 20 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65   passed into the
353e0 0a 2a 2a 20 5b 78 46 69 6c 74 65 72 5d 20 6d 65  .** [xFilter] me
353f0 74 68 6f 64 2e 0a 2a 2a 20 5b 73 71 6c 69 74 65  thod..** [sqlite
35400 33 5f 66 72 65 65 28 29 5d 20 69 73 20 75 73 65  3_free()] is use
35410 64 20 74 6f 20 66 72 65 65 20 69 64 78 50 74 72  d to free idxPtr
35420 20 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 66   if and only iff
35430 0a 2a 2a 20 6e 65 65 64 54 6f 46 72 65 65 49 64  .** needToFreeId
35440 78 50 74 72 20 69 73 20 74 72 75 65 2e 0a 2a 2a  xPtr is true..**
35450 0a 2a 2a 20 54 68 65 20 6f 72 64 65 72 42 79 43  .** The orderByC
35460 6f 6e 73 75 6d 65 64 20 6d 65 61 6e 73 20 74 68  onsumed means th
35470 61 74 20 6f 75 74 70 75 74 20 66 72 6f 6d 20 5b  at output from [
35480 78 46 69 6c 74 65 72 5d 2f 5b 78 4e 65 78 74 5d  xFilter]/[xNext]
35490 20 77 69 6c 6c 20 6f 63 63 75 72 20 69 6e 0a 2a   will occur in.*
354a0 2a 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 72  * the correct or
354b0 64 65 72 20 74 6f 20 73 61 74 69 73 66 79 20 74  der to satisfy t
354c0 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
354d0 73 65 20 73 6f 20 74 68 61 74 20 6e 6f 20 73 65  se so that no se
354e0 70 61 72 61 74 65 0a 2a 2a 20 73 6f 72 74 69 6e  parate.** sortin
354f0 67 20 73 74 65 70 20 69 73 20 72 65 71 75 69 72  g step is requir
35500 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 73  ed..**.** The es
35510 74 69 6d 61 74 65 64 43 6f 73 74 20 76 61 6c 75  timatedCost valu
35520 65 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74 65  e is an estimate
35530 20 6f 66 20 74 68 65 20 63 6f 73 74 20 6f 66 20   of the cost of 
35540 64 6f 69 6e 67 20 74 68 65 0a 2a 2a 20 70 61 72  doing the.** par
35550 74 69 63 75 6c 61 72 20 6c 6f 6f 6b 75 70 2e 20  ticular lookup. 
35560 20 41 20 66 75 6c 6c 20 73 63 61 6e 20 6f 66 20   A full scan of 
35570 61 20 74 61 62 6c 65 20 77 69 74 68 20 4e 20 65  a table with N e
35580 6e 74 72 69 65 73 20 73 68 6f 75 6c 64 20 68 61  ntries should ha
35590 76 65 0a 2a 2a 20 61 20 63 6f 73 74 20 6f 66 20  ve.** a cost of 
355a0 4e 2e 20 20 41 20 62 69 6e 61 72 79 20 73 65 61  N.  A binary sea
355b0 72 63 68 20 6f 66 20 61 20 74 61 62 6c 65 20 6f  rch of a table o
355c0 66 20 4e 20 65 6e 74 72 69 65 73 20 73 68 6f 75  f N entries shou
355d0 6c 64 20 68 61 76 65 20 61 0a 2a 2a 20 63 6f 73  ld have a.** cos
355e0 74 20 6f 66 20 61 70 70 72 6f 78 69 6d 61 74 65  t of approximate
355f0 6c 79 20 6c 6f 67 28 4e 29 2e 0a 2a 2f 0a 73 74  ly log(N)..*/.st
35600 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
35610 65 78 5f 69 6e 66 6f 20 7b 0a 20 20 2f 2a 20 49  ex_info {.  /* I
35620 6e 70 75 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e  nputs */.  int n
35630 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20  Constraint;     
35640 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
35650 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43  of entries in aC
35660 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 73  onstraint */.  s
35670 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
35680 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 7b  dex_constraint {
35690 0a 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d  .     int iColum
356a0 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
356b0 2f 2a 20 43 6f 6c 75 6d 6e 20 6f 6e 20 6c 65 66  /* Column on lef
356c0 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 63  t-hand side of c
356d0 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 20  onstraint */.   
356e0 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
356f0 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43  op;         /* C
35700 6f 6e 73 74 72 61 69 6e 74 20 6f 70 65 72 61 74  onstraint operat
35710 6f 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73 69 67  or */.     unsig
35720 6e 65 64 20 63 68 61 72 20 75 73 61 62 6c 65 3b  ned char usable;
35730 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
35740 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20  this constraint 
35750 69 73 20 75 73 61 62 6c 65 20 2a 2f 0a 20 20 20  is usable */.   
35760 20 20 69 6e 74 20 69 54 65 72 6d 4f 66 66 73 65    int iTermOffse
35770 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55  t;          /* U
35780 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79 20 2d  sed internally -
35790 20 78 42 65 73 74 49 6e 64 65 78 20 73 68 6f 75   xBestIndex shou
357a0 6c 64 20 69 67 6e 6f 72 65 20 2a 2f 0a 20 20 7d  ld ignore */.  }
357b0 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20   *aConstraint;  
357c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62            /* Tab
357d0 6c 65 20 6f 66 20 57 48 45 52 45 20 63 6c 61 75  le of WHERE clau
357e0 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a  se constraints *
357f0 2f 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79  /.  int nOrderBy
35800 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
35810 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d  * Number of term
35820 73 20 69 6e 20 74 68 65 20 4f 52 44 45 52 20 42  s in the ORDER B
35830 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74  Y clause */.  st
35840 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
35850 65 78 5f 6f 72 64 65 72 62 79 20 7b 0a 20 20 20  ex_orderby {.   
35860 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20    int iColumn;  
35870 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
35880 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a  olumn number */.
35890 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68       unsigned ch
358a0 61 72 20 64 65 73 63 3b 20 20 20 20 20 20 20 2f  ar desc;       /
358b0 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 2e  * True for DESC.
358c0 20 20 46 61 6c 73 65 20 66 6f 72 20 41 53 43 2e    False for ASC.
358d0 20 2a 2f 0a 20 20 7d 20 2a 61 4f 72 64 65 72 42   */.  } *aOrderB
358e0 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
358f0 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59   /* The ORDER BY
35900 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 2f 2a 20   clause */.  /* 
35910 4f 75 74 70 75 74 73 20 2a 2f 0a 20 20 73 74 72  Outputs */.  str
35920 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
35930 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61  x_constraint_usa
35940 67 65 20 7b 0a 20 20 20 20 69 6e 74 20 61 72 67  ge {.    int arg
35950 76 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 20  vIndex;         
35960 20 20 2f 2a 20 69 66 20 3e 30 2c 20 63 6f 6e 73    /* if >0, cons
35970 74 72 61 69 6e 74 20 69 73 20 70 61 72 74 20 6f  traint is part o
35980 66 20 61 72 67 76 20 74 6f 20 78 46 69 6c 74 65  f argv to xFilte
35990 72 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67 6e 65  r */.    unsigne
359a0 64 20 63 68 61 72 20 6f 6d 69 74 3b 20 20 20 20  d char omit;    
359b0 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 64 65    /* Do not code
359c0 20 61 20 74 65 73 74 20 66 6f 72 20 74 68 69 73   a test for this
359d0 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20   constraint */. 
359e0 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 55   } *aConstraintU
359f0 73 61 67 65 3b 0a 20 20 69 6e 74 20 69 64 78 4e  sage;.  int idxN
35a00 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  um;             
35a10 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 75 73 65     /* Number use
35a20 64 20 74 6f 20 69 64 65 6e 74 69 66 79 20 74 68  d to identify th
35a30 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63 68 61  e index */.  cha
35a40 72 20 2a 69 64 78 53 74 72 3b 20 20 20 20 20 20  r *idxStr;      
35a50 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e          /* Strin
35a60 67 2c 20 70 6f 73 73 69 62 6c 79 20 6f 62 74 61  g, possibly obta
35a70 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
35a80 33 5f 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 69 6e  3_malloc */.  in
35a90 74 20 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53  t needToFreeIdxS
35aa0 74 72 3b 20 20 20 20 20 20 2f 2a 20 46 72 65 65  tr;      /* Free
35ab0 20 69 64 78 53 74 72 20 75 73 69 6e 67 20 73 71   idxStr using sq
35ac0 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69 66 20  lite3_free() if 
35ad0 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 72  true */.  int or
35ae0 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 3b 20 20  derByConsumed;  
35af0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
35b00 6f 75 74 70 75 74 20 69 73 20 61 6c 72 65 61 64  output is alread
35b10 79 20 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20 64  y ordered */.  d
35b20 6f 75 62 6c 65 20 65 73 74 69 6d 61 74 65 64 43  ouble estimatedC
35b30 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 45 73 74  ost;      /* Est
35b40 69 6d 61 74 65 64 20 63 6f 73 74 20 6f 66 20 75  imated cost of u
35b50 73 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78 20  sing this index 
35b60 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 53 51  */.};.#define SQ
35b70 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
35b80 52 41 49 4e 54 5f 45 51 20 20 20 20 32 0a 23 64  RAINT_EQ    2.#d
35b90 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44  efine SQLITE_IND
35ba0 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 47 54  EX_CONSTRAINT_GT
35bb0 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51      4.#define SQ
35bc0 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
35bd0 52 41 49 4e 54 5f 4c 45 20 20 20 20 38 0a 23 64  RAINT_LE    8.#d
35be0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 44  efine SQLITE_IND
35bf0 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 54  EX_CONSTRAINT_LT
35c00 20 20 20 20 31 36 0a 23 64 65 66 69 6e 65 20 53      16.#define S
35c10 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
35c20 54 52 41 49 4e 54 5f 47 45 20 20 20 20 33 32 0a  TRAINT_GE    32.
35c30 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
35c40 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
35c50 4d 41 54 43 48 20 36 34 0a 0a 2f 2a 0a 2a 2a 20  MATCH 64../*.** 
35c60 43 41 50 49 33 52 45 46 3a 20 52 65 67 69 73 74  CAPI3REF: Regist
35c70 65 72 20 41 20 56 69 72 74 75 61 6c 20 54 61 62  er A Virtual Tab
35c80 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le Implementatio
35c90 6e 20 7b 48 31 38 32 30 30 7d 20 3c 53 32 30 34  n {H18200} <S204
35ca0 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  00>.** EXPERIMEN
35cb0 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  TAL.**.** This r
35cc0 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74  outine is used t
35cd0 6f 20 72 65 67 69 73 74 65 72 20 61 20 6e 65 77  o register a new
35ce0 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20   [virtual table 
35cf0 6d 6f 64 75 6c 65 5d 20 6e 61 6d 65 2e 0a 2a 2a  module] name..**
35d00 20 4d 6f 64 75 6c 65 20 6e 61 6d 65 73 20 6d 75   Module names mu
35d10 73 74 20 62 65 20 72 65 67 69 73 74 65 72 65 64  st be registered
35d20 20 62 65 66 6f 72 65 0a 2a 2a 20 63 72 65 61 74   before.** creat
35d30 69 6e 67 20 61 20 6e 65 77 20 5b 76 69 72 74 75  ing a new [virtu
35d40 61 6c 20 74 61 62 6c 65 5d 20 75 73 69 6e 67 20  al table] using 
35d50 74 68 65 20 6d 6f 64 75 6c 65 2c 20 6f 72 20 62  the module, or b
35d60 65 66 6f 72 65 20 75 73 69 6e 67 20 61 0a 2a 2a  efore using a.**
35d70 20 70 72 65 65 78 69 73 74 69 6e 67 20 5b 76 69   preexisting [vi
35d80 72 74 75 61 6c 20 74 61 62 6c 65 5d 20 66 6f 72  rtual table] for
35d90 20 74 68 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a   the module..**.
35da0 2a 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61  ** The module na
35db0 6d 65 20 69 73 20 72 65 67 69 73 74 65 72 65 64  me is registered
35dc0 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73   on the [databas
35dd0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70  e connection] sp
35de0 65 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68  ecified.** by th
35df0 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
35e00 72 2e 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20  r.  The name of 
35e10 74 68 65 20 6d 6f 64 75 6c 65 20 69 73 20 67 69  the module is gi
35e20 76 65 6e 20 62 79 20 74 68 65 20 0a 2a 2a 20 73  ven by the .** s
35e30 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2e  econd parameter.
35e40 20 20 54 68 65 20 74 68 69 72 64 20 70 61 72 61    The third para
35e50 6d 65 74 65 72 20 69 73 20 61 20 70 6f 69 6e 74  meter is a point
35e60 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20 69 6d 70  er to.** the imp
35e70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
35e80 68 65 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c  he [virtual tabl
35e90 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 20 54 68 65  e module].   The
35ea0 20 66 6f 75 72 74 68 0a 2a 2a 20 70 61 72 61 6d   fourth.** param
35eb0 65 74 65 72 20 69 73 20 61 6e 20 61 72 62 69 74  eter is an arbit
35ec0 72 61 72 79 20 63 6c 69 65 6e 74 20 64 61 74 61  rary client data
35ed0 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73   pointer that is
35ee0 20 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 0a   passed through.
35ef0 2a 2a 20 69 6e 74 6f 20 74 68 65 20 5b 78 43 72  ** into the [xCr
35f00 65 61 74 65 5d 20 61 6e 64 20 5b 78 43 6f 6e 6e  eate] and [xConn
35f10 65 63 74 5d 20 6d 65 74 68 6f 64 73 20 6f 66 20  ect] methods of 
35f20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
35f30 65 20 6d 6f 64 75 6c 65 0a 2a 2a 20 77 68 65 6e  e module.** when
35f40 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74   a new virtual t
35f50 61 62 6c 65 20 69 73 20 62 65 20 62 65 69 6e 67  able is be being
35f60 20 63 72 65 61 74 65 64 20 6f 72 20 72 65 69 6e   created or rein
35f70 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a  itialized..**.**
35f80 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   This interface 
35f90 68 61 73 20 65 78 61 63 74 6c 79 20 74 68 65 20  has exactly the 
35fa0 73 61 6d 65 20 65 66 66 65 63 74 20 61 73 20 63  same effect as c
35fb0 61 6c 6c 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74  alling.** [sqlit
35fc0 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
35fd0 5f 76 32 28 29 5d 20 77 69 74 68 20 61 20 4e 55  _v2()] with a NU
35fe0 4c 4c 20 63 6c 69 65 6e 74 20 64 61 74 61 20 64  LL client data d
35ff0 65 73 74 72 75 63 74 6f 72 2e 0a 2a 2f 0a 53 51  estructor..*/.SQ
36000 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f  LITE_API SQLITE_
36010 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74  EXPERIMENTAL int
36020 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
36030 6d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65  module(.  sqlite
36040 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
36050 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63       /* SQLite c
36060 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67  onnection to reg
36070 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74  ister module wit
36080 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  h */.  const cha
36090 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20  r *zName,       
360a0 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
360b0 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e   module */.  con
360c0 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
360d0 65 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f  e *p,   /* Metho
360e0 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c  ds for the modul
360f0 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c  e */.  void *pCl
36100 69 65 6e 74 44 61 74 61 20 20 20 20 20 20 20 20  ientData        
36110 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61    /* Client data
36120 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f   for xCreate/xCo
36130 6e 6e 65 63 74 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a  nnect */.);../*.
36140 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67  ** CAPI3REF: Reg
36150 69 73 74 65 72 20 41 20 56 69 72 74 75 61 6c 20  ister A Virtual 
36160 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61  Table Implementa
36170 74 69 6f 6e 20 7b 48 31 38 32 31 30 7d 20 3c 53  tion {H18210} <S
36180 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49  20400>.** EXPERI
36190 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69  MENTAL.**.** Thi
361a0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 64 65  s routine is ide
361b0 6e 74 69 63 61 6c 20 74 6f 20 74 68 65 20 5b 73  ntical to the [s
361c0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f  qlite3_create_mo
361d0 64 75 6c 65 28 29 5d 20 6d 65 74 68 6f 64 2c 0a  dule()] method,.
361e0 2a 2a 20 65 78 63 65 70 74 20 74 68 61 74 20 69  ** except that i
361f0 74 20 68 61 73 20 61 6e 20 65 78 74 72 61 20 70  t has an extra p
36200 61 72 61 6d 65 74 65 72 20 74 6f 20 73 70 65 63  arameter to spec
36210 69 66 79 20 0a 2a 2a 20 61 20 64 65 73 74 72 75  ify .** a destru
36220 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f  ctor function fo
36230 72 20 74 68 65 20 63 6c 69 65 6e 74 20 64 61 74  r the client dat
36240 61 20 70 6f 69 6e 74 65 72 2e 20 20 53 51 4c 69  a pointer.  SQLi
36250 74 65 20 77 69 6c 6c 0a 2a 2a 20 69 6e 76 6f 6b  te will.** invok
36260 65 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72  e the destructor
36270 20 66 75 6e 63 74 69 6f 6e 20 28 69 66 20 69 74   function (if it
36280 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 29 20 77 68   is not NULL) wh
36290 65 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 6e 6f 20  en SQLite.** no 
362a0 6c 6f 6e 67 65 72 20 6e 65 65 64 73 20 74 68 65  longer needs the
362b0 20 70 43 6c 69 65 6e 74 44 61 74 61 20 70 6f 69   pClientData poi
362c0 6e 74 65 72 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54  nter.  .*/.SQLIT
362d0 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50  E_API SQLITE_EXP
362e0 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71  ERIMENTAL int sq
362f0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
36300 75 6c 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65  ule_v2(.  sqlite
36310 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
36320 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65 20 63       /* SQLite c
36330 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72 65 67  onnection to reg
36340 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 77 69 74  ister module wit
36350 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  h */.  const cha
36360 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20  r *zName,       
36370 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
36380 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e   module */.  con
36390 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
363a0 65 20 2a 70 2c 20 20 20 2f 2a 20 4d 65 74 68 6f  e *p,   /* Metho
363b0 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64 75 6c  ds for the modul
363c0 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6c  e */.  void *pCl
363d0 69 65 6e 74 44 61 74 61 2c 20 20 20 20 20 20 20  ientData,       
363e0 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61 74 61    /* Client data
363f0 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f   for xCreate/xCo
36400 6e 6e 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 28  nnect */.  void(
36410 2a 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a  *xDestroy)(void*
36420 29 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20  )     /* Module 
36430 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74  destructor funct
36440 69 6f 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a  ion */.);../*.**
36450 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75   CAPI3REF: Virtu
36460 61 6c 20 54 61 62 6c 65 20 49 6e 73 74 61 6e 63  al Table Instanc
36470 65 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 31 30  e Object {H18010
36480 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b 45  } <S20400>.** KE
36490 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33 5f  YWORDS: sqlite3_
364a0 76 74 61 62 0a 2a 2a 20 45 58 50 45 52 49 4d 45  vtab.** EXPERIME
364b0 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79  NTAL.**.** Every
364c0 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20   [virtual table 
364d0 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c 65 6d 65 6e  module] implemen
364e0 74 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75  tation uses a su
364f0 62 63 6c 61 73 73 0a 2a 2a 20 6f 66 20 74 68 65  bclass.** of the
36500 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
36510 74 75 72 65 20 74 6f 20 64 65 73 63 72 69 62 65  ture to describe
36520 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e   a particular in
36530 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 65  stance.** of the
36540 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d   [virtual table]
36550 2e 20 20 45 61 63 68 20 73 75 62 63 6c 61 73 73  .  Each subclass
36560 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 74 61 69 6c   will.** be tail
36570 6f 72 65 64 20 74 6f 20 74 68 65 20 73 70 65 63  ored to the spec
36580 69 66 69 63 20 6e 65 65 64 73 20 6f 66 20 74 68  ific needs of th
36590 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65  e module impleme
365a0 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20  ntation..** The 
365b0 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73 20  purpose of this 
365c0 73 75 70 65 72 63 6c 61 73 73 20 69 73 20 74 6f  superclass is to
365d0 20 64 65 66 69 6e 65 20 63 65 72 74 61 69 6e 20   define certain 
365e0 66 69 65 6c 64 73 20 74 68 61 74 20 61 72 65 0a  fields that are.
365f0 2a 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c  ** common to all
36600 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
36610 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56  tations..**.** V
36620 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 65  irtual tables me
36630 74 68 6f 64 73 20 63 61 6e 20 73 65 74 20 61 6e  thods can set an
36640 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 62   error message b
36650 79 20 61 73 73 69 67 6e 69 6e 67 20 61 0a 2a 2a  y assigning a.**
36660 20 73 74 72 69 6e 67 20 6f 62 74 61 69 6e 65 64   string obtained
36670 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d   from [sqlite3_m
36680 70 72 69 6e 74 66 28 29 5d 20 74 6f 20 7a 45 72  printf()] to zEr
36690 72 4d 73 67 2e 20 20 54 68 65 20 6d 65 74 68 6f  rMsg.  The metho
366a0 64 20 73 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65  d should.** take
366b0 20 63 61 72 65 20 74 68 61 74 20 61 6e 79 20 70   care that any p
366c0 72 69 6f 72 20 73 74 72 69 6e 67 20 69 73 20 66  rior string is f
366d0 72 65 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74  reed by a call t
366e0 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28  o [sqlite3_free(
366f0 29 5d 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61  )].** prior to a
36700 73 73 69 67 6e 69 6e 67 20 61 20 6e 65 77 20 73  ssigning a new s
36710 74 72 69 6e 67 20 74 6f 20 7a 45 72 72 4d 73 67  tring to zErrMsg
36720 2e 20 20 41 66 74 65 72 20 74 68 65 20 65 72 72  .  After the err
36730 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73  or message.** is
36740 20 64 65 6c 69 76 65 72 65 64 20 75 70 20 74 6f   delivered up to
36750 20 74 68 65 20 63 6c 69 65 6e 74 20 61 70 70 6c   the client appl
36760 69 63 61 74 69 6f 6e 2c 20 74 68 65 20 73 74 72  ication, the str
36770 69 6e 67 20 77 69 6c 6c 20 62 65 20 61 75 74 6f  ing will be auto
36780 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 66 72 65  matically.** fre
36790 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 66 72  ed by sqlite3_fr
367a0 65 65 28 29 20 61 6e 64 20 74 68 65 20 7a 45 72  ee() and the zEr
367b0 72 4d 73 67 20 66 69 65 6c 64 20 77 69 6c 6c 20  rMsg field will 
367c0 62 65 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 73 74  be zeroed..*/.st
367d0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61  ruct sqlite3_vta
367e0 62 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  b {.  const sqli
367f0 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64  te3_module *pMod
36800 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f 64  ule;  /* The mod
36810 75 6c 65 20 66 6f 72 20 74 68 69 73 20 76 69 72  ule for this vir
36820 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
36830 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20  int nRef;       
36840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36850 2f 2a 20 4e 4f 20 4c 4f 4e 47 45 52 20 55 53 45  /* NO LONGER USE
36860 44 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72  D */.  char *zEr
36870 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20 20  rMsg;           
36880 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20         /* Error 
36890 6d 65 73 73 61 67 65 20 66 72 6f 6d 20 73 71 6c  message from sql
368a0 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a  ite3_mprintf() *
368b0 2f 0a 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74  /.  /* Virtual t
368c0 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  able implementat
368d0 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69 63 61  ions will typica
368e0 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e  lly add addition
368f0 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a  al fields */.};.
36900 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
36910 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 43   Virtual Table C
36920 75 72 73 6f 72 20 4f 62 6a 65 63 74 20 20 7b 48  ursor Object  {H
36930 31 38 30 32 30 7d 20 3c 53 32 30 34 30 30 3e 0a  18020} <S20400>.
36940 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c  ** KEYWORDS: sql
36950 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
36960 20 7b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20   {virtual table 
36970 63 75 72 73 6f 72 7d 0a 2a 2a 20 45 58 50 45 52  cursor}.** EXPER
36980 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76  IMENTAL.**.** Ev
36990 65 72 79 20 5b 76 69 72 74 75 61 6c 20 74 61 62  ery [virtual tab
369a0 6c 65 20 6d 6f 64 75 6c 65 5d 20 69 6d 70 6c 65  le module] imple
369b0 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 61  mentation uses a
369c0 20 73 75 62 63 6c 61 73 73 20 6f 66 20 74 68 65   subclass of the
369d0 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  .** following st
369e0 72 75 63 74 75 72 65 20 74 6f 20 64 65 73 63 72  ructure to descr
369f0 69 62 65 20 63 75 72 73 6f 72 73 20 74 68 61 74  ibe cursors that
36a00 20 70 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 0a   point into the.
36a10 2a 2a 20 5b 76 69 72 74 75 61 6c 20 74 61 62 6c  ** [virtual tabl
36a20 65 5d 20 61 6e 64 20 61 72 65 20 75 73 65 64 0a  e] and are used.
36a30 2a 2a 20 74 6f 20 6c 6f 6f 70 20 74 68 72 6f 75  ** to loop throu
36a40 67 68 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  gh the virtual t
36a50 61 62 6c 65 2e 20 20 43 75 72 73 6f 72 73 20 61  able.  Cursors a
36a60 72 65 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  re created using
36a70 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   the.** [sqlite3
36a80 5f 6d 6f 64 75 6c 65 2e 78 4f 70 65 6e 20 7c 20  _module.xOpen | 
36a90 78 4f 70 65 6e 5d 20 6d 65 74 68 6f 64 20 6f 66  xOpen] method of
36aa0 20 74 68 65 20 6d 6f 64 75 6c 65 20 61 6e 64 20   the module and 
36ab0 61 72 65 20 64 65 73 74 72 6f 79 65 64 0a 2a 2a  are destroyed.**
36ac0 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33   by the [sqlite3
36ad0 5f 6d 6f 64 75 6c 65 2e 78 43 6c 6f 73 65 20 7c  _module.xClose |
36ae0 20 78 43 6c 6f 73 65 5d 20 6d 65 74 68 6f 64 2e   xClose] method.
36af0 20 20 43 75 73 73 6f 72 73 20 61 72 65 20 75 73    Cussors are us
36b00 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 5b 78 46  ed.** by the [xF
36b10 69 6c 74 65 72 5d 2c 20 5b 78 4e 65 78 74 5d 2c  ilter], [xNext],
36b20 20 5b 78 45 6f 66 5d 2c 20 5b 78 43 6f 6c 75 6d   [xEof], [xColum
36b30 6e 5d 2c 20 61 6e 64 20 5b 78 52 6f 77 69 64 5d  n], and [xRowid]
36b40 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20 74   methods.** of t
36b50 68 65 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63 68  he module.  Each
36b60 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
36b70 74 61 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66 69  tation will defi
36b80 6e 65 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e  ne.** the conten
36b90 74 20 6f 66 20 61 20 63 75 72 73 6f 72 20 73 74  t of a cursor st
36ba0 72 75 63 74 75 72 65 20 74 6f 20 73 75 69 74 20  ructure to suit 
36bb0 69 74 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a  its own needs..*
36bc0 2a 0a 2a 2a 20 54 68 69 73 20 73 75 70 65 72 63  *.** This superc
36bd0 6c 61 73 73 20 65 78 69 73 74 73 20 69 6e 20 6f  lass exists in o
36be0 72 64 65 72 20 74 6f 20 64 65 66 69 6e 65 20 66  rder to define f
36bf0 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72  ields of the cur
36c00 73 6f 72 20 74 68 61 74 0a 2a 2a 20 61 72 65 20  sor that.** are 
36c10 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d  common to all im
36c20 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a  plementations..*
36c30 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  /.struct sqlite3
36c40 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 7b 0a 20  _vtab_cursor {. 
36c50 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
36c60 56 74 61 62 3b 20 20 20 20 20 20 2f 2a 20 56 69  Vtab;      /* Vi
36c70 72 74 75 61 6c 20 74 61 62 6c 65 20 6f 66 20 74  rtual table of t
36c80 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  his cursor */.  
36c90 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62 6c 65  /* Virtual table
36ca0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
36cb0 20 77 69 6c 6c 20 74 79 70 69 63 61 6c 6c 79 20   will typically 
36cc0 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 66  add additional f
36cd0 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ields */.};../*.
36ce0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 63  ** CAPI3REF: Dec
36cf0 6c 61 72 65 20 54 68 65 20 53 63 68 65 6d 61 20  lare The Schema 
36d00 4f 66 20 41 20 56 69 72 74 75 61 6c 20 54 61 62  Of A Virtual Tab
36d10 6c 65 20 7b 48 31 38 32 38 30 7d 20 3c 53 32 30  le {H18280} <S20
36d20 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45  400>.** EXPERIME
36d30 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b  NTAL.**.** The [
36d40 78 43 72 65 61 74 65 5d 20 61 6e 64 20 5b 78 43  xCreate] and [xC
36d50 6f 6e 6e 65 63 74 5d 20 6d 65 74 68 6f 64 73 20  onnect] methods 
36d60 6f 66 20 61 0a 2a 2a 20 5b 76 69 72 74 75 61 6c  of a.** [virtual
36d70 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 20 63   table module] c
36d80 61 6c 6c 20 74 68 69 73 20 69 6e 74 65 72 66 61  all this interfa
36d90 63 65 0a 2a 2a 20 74 6f 20 64 65 63 6c 61 72 65  ce.** to declare
36da0 20 74 68 65 20 66 6f 72 6d 61 74 20 28 74 68 65   the format (the
36db0 20 6e 61 6d 65 73 20 61 6e 64 20 64 61 74 61 74   names and datat
36dc0 79 70 65 73 20 6f 66 20 74 68 65 20 63 6f 6c 75  ypes of the colu
36dd0 6d 6e 73 29 20 6f 66 0a 2a 2a 20 74 68 65 20 76  mns) of.** the v
36de0 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 74 68  irtual tables th
36df0 65 79 20 69 6d 70 6c 65 6d 65 6e 74 2e 0a 2a 2f  ey implement..*/
36e00 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49  .SQLITE_API SQLI
36e10 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20  TE_EXPERIMENTAL 
36e20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 65 63 6c  int sqlite3_decl
36e30 61 72 65 5f 76 74 61 62 28 73 71 6c 69 74 65 33  are_vtab(sqlite3
36e40 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  *, const char *z
36e50 53 51 4c 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  SQL);../*.** CAP
36e60 49 33 52 45 46 3a 20 4f 76 65 72 6c 6f 61 64 20  I3REF: Overload 
36e70 41 20 46 75 6e 63 74 69 6f 6e 20 46 6f 72 20 41  A Function For A
36e80 20 56 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b   Virtual Table {
36e90 48 31 38 33 30 30 7d 20 3c 53 32 30 34 30 30 3e  H18300} <S20400>
36ea0 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
36eb0 0a 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74  .**.** Virtual t
36ec0 61 62 6c 65 73 20 63 61 6e 20 70 72 6f 76 69 64  ables can provid
36ed0 65 20 61 6c 74 65 72 6e 61 74 69 76 65 20 69 6d  e alternative im
36ee0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66  plementations of
36ef0 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 75 73   functions.** us
36f00 69 6e 67 20 74 68 65 20 5b 78 46 69 6e 64 46 75  ing the [xFindFu
36f10 6e 63 74 69 6f 6e 5d 20 6d 65 74 68 6f 64 20 6f  nction] method o
36f20 66 20 74 68 65 20 5b 76 69 72 74 75 61 6c 20 74  f the [virtual t
36f30 61 62 6c 65 20 6d 6f 64 75 6c 65 5d 2e 20 20 0a  able module].  .
36f40 2a 2a 20 42 75 74 20 67 6c 6f 62 61 6c 20 76 65  ** But global ve
36f50 72 73 69 6f 6e 73 20 6f 66 20 74 68 6f 73 65 20  rsions of those 
36f60 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 6d 75 73  functions.** mus
36f70 74 20 65 78 69 73 74 20 69 6e 20 6f 72 64 65 72  t exist in order
36f80 20 74 6f 20 62 65 20 6f 76 65 72 6c 6f 61 64 65   to be overloade
36f90 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50  d..**.** This AP
36fa0 49 20 6d 61 6b 65 73 20 73 75 72 65 20 61 20 67  I makes sure a g
36fb0 6c 6f 62 61 6c 20 76 65 72 73 69 6f 6e 20 6f 66  lobal version of
36fc0 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68   a function with
36fd0 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a   a particular.**
36fe0 20 6e 61 6d 65 20 61 6e 64 20 6e 75 6d 62 65 72   name and number
36ff0 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20 65   of parameters e
37000 78 69 73 74 73 2e 20 20 49 66 20 6e 6f 20 73 75  xists.  If no su
37010 63 68 20 66 75 6e 63 74 69 6f 6e 20 65 78 69 73  ch function exis
37020 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20 74 68 69  ts.** before thi
37030 73 20 41 50 49 20 69 73 20 63 61 6c 6c 65 64 2c  s API is called,
37040 20 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 20   a new function 
37050 69 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 65  is created.  The
37060 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a   implementation.
37070 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77 20 66 75  ** of the new fu
37080 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 63 61  nction always ca
37090 75 73 65 73 20 61 6e 20 65 78 63 65 70 74 69 6f  uses an exceptio
370a0 6e 20 74 6f 20 62 65 20 74 68 72 6f 77 6e 2e 20  n to be thrown. 
370b0 20 53 6f 0a 2a 2a 20 74 68 65 20 6e 65 77 20 66   So.** the new f
370c0 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67  unction is not g
370d0 6f 6f 64 20 66 6f 72 20 61 6e 79 74 68 69 6e 67  ood for anything
370e0 20 62 79 20 69 74 73 65 6c 66 2e 20 20 49 74 73   by itself.  Its
370f0 20 6f 6e 6c 79 0a 2a 2a 20 70 75 72 70 6f 73 65   only.** purpose
37100 20 69 73 20 74 6f 20 62 65 20 61 20 70 6c 61 63   is to be a plac
37110 65 68 6f 6c 64 65 72 20 66 75 6e 63 74 69 6f 6e  eholder function
37120 20 74 68 61 74 20 63 61 6e 20 62 65 20 6f 76 65   that can be ove
37130 72 6c 6f 61 64 65 64 0a 2a 2a 20 62 79 20 61 20  rloaded.** by a 
37140 5b 76 69 72 74 75 61 6c 20 74 61 62 6c 65 5d 2e  [virtual table].
37150 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  .*/.SQLITE_API S
37160 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54  QLITE_EXPERIMENT
37170 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  AL int sqlite3_o
37180 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e  verload_function
37190 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74  (sqlite3*, const
371a0 20 63 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d 65   char *zFuncName
371b0 2c 20 69 6e 74 20 6e 41 72 67 29 3b 0a 0a 2f 2a  , int nArg);../*
371c0 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63  .** The interfac
371d0 65 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c  e to the virtual
371e0 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d  -table mechanism
371f0 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65 20 28   defined above (
37200 62 61 63 6b 20 75 70 0a 2a 2a 20 74 6f 20 61 20  back up.** to a 
37210 63 6f 6d 6d 65 6e 74 20 72 65 6d 61 72 6b 61 62  comment remarkab
37220 6c 79 20 73 69 6d 69 6c 61 72 20 74 6f 20 74 68  ly similar to th
37230 69 73 20 6f 6e 65 29 20 69 73 20 63 75 72 72 65  is one) is curre
37240 6e 74 6c 79 20 63 6f 6e 73 69 64 65 72 65 64 0a  ntly considered.
37250 2a 2a 20 74 6f 20 62 65 20 65 78 70 65 72 69 6d  ** to be experim
37260 65 6e 74 61 6c 2e 20 20 54 68 65 20 69 6e 74 65  ental.  The inte
37270 72 66 61 63 65 20 6d 69 67 68 74 20 63 68 61 6e  rface might chan
37280 67 65 20 69 6e 20 69 6e 63 6f 6d 70 61 74 69 62  ge in incompatib
37290 6c 65 20 77 61 79 73 2e 0a 2a 2a 20 49 66 20 74  le ways..** If t
372a0 68 69 73 20 69 73 20 61 20 70 72 6f 62 6c 65 6d  his is a problem
372b0 20 66 6f 72 20 79 6f 75 2c 20 64 6f 20 6e 6f 74   for you, do not
372c0 20 75 73 65 20 74 68 65 20 69 6e 74 65 72 66 61   use the interfa
372d0 63 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e  ce at this time.
372e0 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20  .**.** When the 
372f0 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6d 65  virtual-table me
37300 63 68 61 6e 69 73 6d 20 73 74 61 62 69 6c 69 7a  chanism stabiliz
37310 65 73 2c 20 77 65 20 77 69 6c 6c 20 64 65 63 6c  es, we will decl
37320 61 72 65 20 74 68 65 0a 2a 2a 20 69 6e 74 65 72  are the.** inter
37330 66 61 63 65 20 66 69 78 65 64 2c 20 73 75 70 70  face fixed, supp
37340 6f 72 74 20 69 74 20 69 6e 64 65 66 69 6e 69 74  ort it indefinit
37350 65 6c 79 2c 20 61 6e 64 20 72 65 6d 6f 76 65 20  ely, and remove 
37360 74 68 69 73 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2a  this comment..**
37370 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49 4d 45  .****** EXPERIME
37380 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74 20 74  NTAL - subject t
37390 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74  o change without
373a0 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a 2a 2a   notice ********
373b0 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  ******.*/../*.**
373c0 20 43 41 50 49 33 52 45 46 3a 20 41 20 48 61 6e   CAPI3REF: A Han
373d0 64 6c 65 20 54 6f 20 41 6e 20 4f 70 65 6e 20 42  dle To An Open B
373e0 4c 4f 42 20 7b 48 31 37 38 30 30 7d 20 3c 53 33  LOB {H17800} <S3
373f0 30 32 33 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44  0230>.** KEYWORD
37400 53 3a 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 7d  S: {BLOB handle}
37410 20 7b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 7d 0a   {BLOB handles}.
37420 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63  **.** An instanc
37430 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74  e of this object
37440 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 6f   represents an o
37450 70 65 6e 20 42 4c 4f 42 20 6f 6e 20 77 68 69 63  pen BLOB on whic
37460 68 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c  h.** [sqlite3_bl
37470 6f 62 5f 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d  ob_open | increm
37480 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20  ental BLOB I/O] 
37490 63 61 6e 20 62 65 20 70 65 72 66 6f 72 6d 65 64  can be performed
374a0 2e 0a 2a 2a 20 4f 62 6a 65 63 74 73 20 6f 66 20  ..** Objects of 
374b0 74 68 69 73 20 74 79 70 65 20 61 72 65 20 63 72  this type are cr
374c0 65 61 74 65 64 20 62 79 20 5b 73 71 6c 69 74 65  eated by [sqlite
374d0 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 0a 2a  3_blob_open()].*
374e0 2a 20 61 6e 64 20 64 65 73 74 72 6f 79 65 64 20  * and destroyed 
374f0 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  by [sqlite3_blob
37500 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 20 54 68  _close()]..** Th
37510 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  e [sqlite3_blob_
37520 72 65 61 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c  read()] and [sql
37530 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28  ite3_blob_write(
37540 29 5d 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a  )] interfaces.**
37550 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
37560 72 65 61 64 20 6f 72 20 77 72 69 74 65 20 73 6d  read or write sm
37570 61 6c 6c 20 73 75 62 73 65 63 74 69 6f 6e 73 20  all subsections 
37580 6f 66 20 74 68 65 20 42 4c 4f 42 2e 0a 2a 2a 20  of the BLOB..** 
37590 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  The [sqlite3_blo
375a0 62 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72  b_bytes()] inter
375b0 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65  face returns the
375c0 20 73 69 7a 65 20 6f 66 20 74 68 65 20 42 4c 4f   size of the BLO
375d0 42 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f 0a 74  B in bytes..*/.t
375e0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
375f0 6c 69 74 65 33 5f 62 6c 6f 62 20 73 71 6c 69 74  lite3_blob sqlit
37600 65 33 5f 62 6c 6f 62 3b 0a 0a 2f 2a 0a 2a 2a 20  e3_blob;../*.** 
37610 43 41 50 49 33 52 45 46 3a 20 4f 70 65 6e 20 41  CAPI3REF: Open A
37620 20 42 4c 4f 42 20 46 6f 72 20 49 6e 63 72 65 6d   BLOB For Increm
37630 65 6e 74 61 6c 20 49 2f 4f 20 7b 48 31 37 38 31  ental I/O {H1781
37640 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a  0} <S30230>.**.*
37650 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65  * This interface
37660 73 20 6f 70 65 6e 73 20 61 20 5b 42 4c 4f 42 20  s opens a [BLOB 
37670 68 61 6e 64 6c 65 20 7c 20 68 61 6e 64 6c 65 5d  handle | handle]
37680 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 6c 6f 63   to the BLOB loc
37690 61 74 65 64 0a 2a 2a 20 69 6e 20 72 6f 77 20 69  ated.** in row i
376a0 52 6f 77 2c 20 63 6f 6c 75 6d 6e 20 7a 43 6f 6c  Row, column zCol
376b0 75 6d 6e 2c 20 74 61 62 6c 65 20 7a 54 61 62 6c  umn, table zTabl
376c0 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 7a 44  e in database zD
376d0 62 3b 0a 2a 2a 20 69 6e 20 6f 74 68 65 72 20 77  b;.** in other w
376e0 6f 72 64 73 2c 20 74 68 65 20 73 61 6d 65 20 42  ords, the same B
376f0 4c 4f 42 20 74 68 61 74 20 77 6f 75 6c 64 20 62  LOB that would b
37700 65 20 73 65 6c 65 63 74 65 64 20 62 79 3a 0a 2a  e selected by:.*
37710 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a 20 20 20  *.** <pre>.**   
37720 20 20 53 45 4c 45 43 54 20 7a 43 6f 6c 75 6d 6e    SELECT zColumn
37730 20 46 52 4f 4d 20 7a 44 62 2e 7a 54 61 62 6c 65   FROM zDb.zTable
37740 20 57 48 45 52 45 20 5b 72 6f 77 69 64 5d 20 3d   WHERE [rowid] =
37750 20 69 52 6f 77 3b 0a 2a 2a 20 3c 2f 70 72 65 3e   iRow;.** </pre>
37760 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 66 20   {END}.**.** If 
37770 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65  the flags parame
37780 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c  ter is non-zero,
37790 20 74 68 65 6e 20 74 68 65 20 42 4c 4f 42 20 69   then the BLOB i
377a0 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61  s opened for rea
377b0 64 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 61  d.** and write a
377c0 63 63 65 73 73 2e 20 49 66 20 69 74 20 69 73 20  ccess. If it is 
377d0 7a 65 72 6f 2c 20 74 68 65 20 42 4c 4f 42 20 69  zero, the BLOB i
377e0 73 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61  s opened for rea
377f0 64 20 61 63 63 65 73 73 2e 0a 2a 2a 20 49 74 20  d access..** It 
37800 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
37810 74 6f 20 6f 70 65 6e 20 61 20 63 6f 6c 75 6d 6e  to open a column
37820 20 74 68 61 74 20 69 73 20 70 61 72 74 20 6f 66   that is part of
37830 20 61 6e 20 69 6e 64 65 78 20 6f 72 20 70 72 69   an index or pri
37840 6d 61 72 79 20 0a 2a 2a 20 6b 65 79 20 66 6f 72  mary .** key for
37850 20 77 72 69 74 69 6e 67 2e 20 5e 49 66 20 5b 66   writing. ^If [f
37860 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
37870 72 61 69 6e 74 73 5d 20 61 72 65 20 65 6e 61 62  raints] are enab
37880 6c 65 64 2c 20 69 74 20 69 73 20 0a 2a 2a 20 6e  led, it is .** n
37890 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6f  ot possible to o
378a0 70 65 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61  pen a column tha
378b0 74 20 69 73 20 70 61 72 74 20 6f 66 20 61 20 5b  t is part of a [
378c0 63 68 69 6c 64 20 6b 65 79 5d 20 66 6f 72 20 77  child key] for w
378d0 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  riting..**.** No
378e0 74 65 20 74 68 61 74 20 74 68 65 20 64 61 74 61  te that the data
378f0 62 61 73 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74  base name is not
37900 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 74 68   the filename th
37910 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74  at contains.** t
37920 68 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20  he database but 
37930 72 61 74 68 65 72 20 74 68 65 20 73 79 6d 62 6f  rather the symbo
37940 6c 69 63 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  lic name of the 
37950 64 61 74 61 62 61 73 65 20 74 68 61 74 0a 2a 2a  database that.**
37960 20 69 73 20 61 73 73 69 67 6e 65 64 20 77 68 65   is assigned whe
37970 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  n the database i
37980 73 20 63 6f 6e 6e 65 63 74 65 64 20 75 73 69 6e  s connected usin
37990 67 20 5b 41 54 54 41 43 48 5d 2e 0a 2a 2a 20 46  g [ATTACH]..** F
379a0 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  or the main data
379b0 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 20 64  base file, the d
379c0 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73 20  atabase name is 
379d0 22 6d 61 69 6e 22 2e 0a 2a 2a 20 46 6f 72 20 54  "main"..** For T
379e0 45 4d 50 20 74 61 62 6c 65 73 2c 20 74 68 65 20  EMP tables, the 
379f0 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 69 73  database name is
37a00 20 22 74 65 6d 70 22 2e 0a 2a 2a 0a 2a 2a 20 4f   "temp"..**.** O
37a10 6e 20 73 75 63 63 65 73 73 2c 20 5b 53 51 4c 49  n success, [SQLI
37a20 54 45 5f 4f 4b 5d 20 69 73 20 72 65 74 75 72 6e  TE_OK] is return
37a30 65 64 20 61 6e 64 20 74 68 65 20 6e 65 77 20 5b  ed and the new [
37a40 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 73 20  BLOB handle] is 
37a50 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70  written.** to *p
37a60 70 42 6c 6f 62 2e 20 4f 74 68 65 72 77 69 73 65  pBlob. Otherwise
37a70 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d   an [error code]
37a80 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
37a90 20 2a 70 70 42 6c 6f 62 20 69 73 20 73 65 74 0a   *ppBlob is set.
37aa0 2a 2a 20 74 6f 20 62 65 20 61 20 6e 75 6c 6c 20  ** to be a null 
37ab0 70 6f 69 6e 74 65 72 2e 0a 2a 2a 20 54 68 69 73  pointer..** This
37ac0 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74   function sets t
37ad0 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
37ae0 6e 65 63 74 69 6f 6e 5d 20 65 72 72 6f 72 20 63  nection] error c
37af0 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67 65 0a  ode and message.
37b00 2a 2a 20 61 63 63 65 73 73 69 62 6c 65 20 76 69  ** accessible vi
37b10 61 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  a [sqlite3_errco
37b20 64 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  de()] and [sqlit
37b30 65 33 5f 65 72 72 6d 73 67 28 29 5d 20 61 6e 64  e3_errmsg()] and
37b40 20 72 65 6c 61 74 65 64 0a 2a 2a 20 66 75 6e 63   related.** func
37b50 74 69 6f 6e 73 2e 20 20 4e 6f 74 65 20 74 68 61  tions.  Note tha
37b60 74 20 74 68 65 20 2a 70 70 42 6c 6f 62 20 76 61  t the *ppBlob va
37b70 72 69 61 62 6c 65 20 69 73 20 61 6c 77 61 79 73  riable is always
37b80 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 6e 20   initialized in 
37b90 61 0a 2a 2a 20 77 61 79 20 74 68 61 74 20 6d 61  a.** way that ma
37ba0 6b 65 73 20 69 74 20 73 61 66 65 20 74 6f 20 69  kes it safe to i
37bb0 6e 76 6f 6b 65 20 5b 73 71 6c 69 74 65 33 5f 62  nvoke [sqlite3_b
37bc0 6c 6f 62 5f 63 6c 6f 73 65 28 29 5d 20 6f 6e 20  lob_close()] on 
37bd0 2a 70 70 42 6c 6f 62 0a 2a 2a 20 72 65 67 61 72  *ppBlob.** regar
37be0 64 6c 65 73 73 20 6f 66 20 74 68 65 20 73 75 63  dless of the suc
37bf0 63 65 73 73 20 6f 72 20 66 61 69 6c 75 72 65 20  cess or failure 
37c00 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  of this routine.
37c10 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 6f  .**.** If the ro
37c20 77 20 74 68 61 74 20 61 20 42 4c 4f 42 20 68 61  w that a BLOB ha
37c30 6e 64 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69  ndle points to i
37c40 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e  s modified by an
37c50 0a 2a 2a 20 5b 55 50 44 41 54 45 5d 2c 20 5b 44  .** [UPDATE], [D
37c60 45 4c 45 54 45 5d 2c 20 6f 72 20 62 79 20 5b 4f  ELETE], or by [O
37c70 4e 20 43 4f 4e 46 4c 49 43 54 5d 20 73 69 64 65  N CONFLICT] side
37c80 2d 65 66 66 65 63 74 73 0a 2a 2a 20 74 68 65 6e  -effects.** then
37c90 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65   the BLOB handle
37ca0 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22 65   is marked as "e
37cb0 78 70 69 72 65 64 22 2e 0a 2a 2a 20 54 68 69 73  xpired"..** This
37cc0 20 69 73 20 74 72 75 65 20 69 66 20 61 6e 79 20   is true if any 
37cd0 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 6f  column of the ro
37ce0 77 20 69 73 20 63 68 61 6e 67 65 64 2c 20 65 76  w is changed, ev
37cf0 65 6e 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f  en a column.** o
37d00 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e  ther than the on
37d10 65 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c  e the BLOB handl
37d20 65 20 69 73 20 6f 70 65 6e 20 6f 6e 2e 0a 2a 2a  e is open on..**
37d30 20 43 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74   Calls to [sqlit
37d40 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d 20  e3_blob_read()] 
37d50 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  and [sqlite3_blo
37d60 62 5f 77 72 69 74 65 28 29 5d 20 66 6f 72 0a 2a  b_write()] for.*
37d70 2a 20 61 20 65 78 70 69 72 65 64 20 42 4c 4f 42  * a expired BLOB
37d80 20 68 61 6e 64 6c 65 20 66 61 69 6c 20 77 69 74   handle fail wit
37d90 68 20 61 6e 20 72 65 74 75 72 6e 20 63 6f 64 65  h an return code
37da0 20 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52   of [SQLITE_ABOR
37db0 54 5d 2e 0a 2a 2a 20 43 68 61 6e 67 65 73 20 77  T]..** Changes w
37dc0 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 42 4c  ritten into a BL
37dd0 4f 42 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20  OB prior to the 
37de0 42 4c 4f 42 20 65 78 70 69 72 69 6e 67 20 61 72  BLOB expiring ar
37df0 65 20 6e 6f 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63  e not.** rollbac
37e00 6b 20 62 79 20 74 68 65 20 65 78 70 69 72 61 74  k by the expirat
37e10 69 6f 6e 20 6f 66 20 74 68 65 20 42 4c 4f 42 2e  ion of the BLOB.
37e20 20 20 53 75 63 68 20 63 68 61 6e 67 65 73 20 77    Such changes w
37e30 69 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a  ill eventually.*
37e40 2a 20 63 6f 6d 6d 69 74 20 69 66 20 74 68 65 20  * commit if the 
37e50 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6e 74  transaction cont
37e60 69 6e 75 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74  inues to complet
37e70 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74  ion..**.** Use t
37e80 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  he [sqlite3_blob
37e90 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66  _bytes()] interf
37ea0 61 63 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  ace to determine
37eb0 20 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20   the size of.** 
37ec0 74 68 65 20 6f 70 65 6e 65 64 20 62 6c 6f 62 2e  the opened blob.
37ed0 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20 61 20    The size of a 
37ee0 62 6c 6f 62 20 6d 61 79 20 6e 6f 74 20 62 65 20  blob may not be 
37ef0 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73 0a  changed by this.
37f00 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 20 20 55  ** interface.  U
37f10 73 65 20 74 68 65 20 5b 55 50 44 41 54 45 5d 20  se the [UPDATE] 
37f20 53 51 4c 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 63  SQL command to c
37f30 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f  hange the size o
37f40 66 20 61 0a 2a 2a 20 62 6c 6f 62 2e 0a 2a 2a 0a  f a.** blob..**.
37f50 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  ** The [sqlite3_
37f60 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d  bind_zeroblob()]
37f70 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 65   and [sqlite3_re
37f80 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 5d  sult_zeroblob()]
37f90 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 61   interfaces.** a
37fa0 6e 64 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  nd the built-in 
37fb0 5b 7a 65 72 6f 62 6c 6f 62 5d 20 53 51 4c 20 66  [zeroblob] SQL f
37fc0 75 6e 63 74 69 6f 6e 20 63 61 6e 20 62 65 20 75  unction can be u
37fd0 73 65 64 2c 20 69 66 20 64 65 73 69 72 65 64 2c  sed, if desired,
37fe0 0a 2a 2a 20 74 6f 20 63 72 65 61 74 65 20 61 6e  .** to create an
37ff0 20 65 6d 70 74 79 2c 20 7a 65 72 6f 2d 66 69 6c   empty, zero-fil
38000 6c 65 64 20 62 6c 6f 62 20 69 6e 20 77 68 69 63  led blob in whic
38010 68 20 74 6f 20 72 65 61 64 20 6f 72 20 77 72 69  h to read or wri
38020 74 65 20 75 73 69 6e 67 0a 2a 2a 20 74 68 69 73  te using.** this
38030 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a   interface..**.*
38040 2a 20 54 6f 20 61 76 6f 69 64 20 61 20 72 65 73  * To avoid a res
38050 6f 75 72 63 65 20 6c 65 61 6b 2c 20 65 76 65 72  ource leak, ever
38060 79 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61 6e  y open [BLOB han
38070 64 6c 65 5d 20 73 68 6f 75 6c 64 20 65 76 65 6e  dle] should even
38080 74 75 61 6c 6c 79 0a 2a 2a 20 62 65 20 72 65 6c  tually.** be rel
38090 65 61 73 65 64 20 62 79 20 61 20 63 61 6c 6c 20  eased by a call 
380a0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  to [sqlite3_blob
380b0 5f 63 6c 6f 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a  _close()]..**.**
380c0 20 52 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   Requirements:.*
380d0 2a 20 5b 48 31 37 38 31 33 5d 20 5b 48 31 37 38  * [H17813] [H178
380e0 31 34 5d 20 5b 48 31 37 38 31 36 5d 20 5b 48 31  14] [H17816] [H1
380f0 37 38 31 39 5d 20 5b 48 31 37 38 32 31 5d 20 5b  7819] [H17821] [
38100 48 31 37 38 32 34 5d 0a 2a 2f 0a 53 51 4c 49 54  H17824].*/.SQLIT
38110 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
38120 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20 20 73  3_blob_open(.  s
38130 71 6c 69 74 65 33 2a 2c 0a 20 20 63 6f 6e 73 74  qlite3*,.  const
38140 20 63 68 61 72 20 2a 7a 44 62 2c 0a 20 20 63 6f   char *zDb,.  co
38150 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65  nst char *zTable
38160 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
38170 7a 43 6f 6c 75 6d 6e 2c 0a 20 20 73 71 6c 69 74  zColumn,.  sqlit
38180 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 2c 0a 20  e3_int64 iRow,. 
38190 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 73 71   int flags,.  sq
381a0 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42  lite3_blob **ppB
381b0 6c 6f 62 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  lob.);../*.** CA
381c0 50 49 33 52 45 46 3a 20 43 6c 6f 73 65 20 41 20  PI3REF: Close A 
381d0 42 4c 4f 42 20 48 61 6e 64 6c 65 20 7b 48 31 37  BLOB Handle {H17
381e0 38 33 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a  830} <S30230>.**
381f0 0a 2a 2a 20 43 6c 6f 73 65 73 20 61 6e 20 6f 70  .** Closes an op
38200 65 6e 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d  en [BLOB handle]
38210 2e 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20  ..**.** Closing 
38220 61 20 42 4c 4f 42 20 73 68 61 6c 6c 20 63 61 75  a BLOB shall cau
38230 73 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  se the current t
38240 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 63 6f  ransaction to co
38250 6d 6d 69 74 0a 2a 2a 20 69 66 20 74 68 65 72 65  mmit.** if there
38260 20 61 72 65 20 6e 6f 20 6f 74 68 65 72 20 42 4c   are no other BL
38270 4f 42 73 2c 20 6e 6f 20 70 65 6e 64 69 6e 67 20  OBs, no pending 
38280 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
38290 6e 74 73 2c 20 61 6e 64 20 74 68 65 0a 2a 2a 20  nts, and the.** 
382a0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
382b0 69 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74 6f 63  ion is in [autoc
382c0 6f 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a 2a 20  ommit mode]..** 
382d0 49 66 20 61 6e 79 20 77 72 69 74 65 73 20 77 65  If any writes we
382e0 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 42  re made to the B
382f0 4c 4f 42 2c 20 74 68 65 79 20 6d 69 67 68 74 20  LOB, they might 
38300 62 65 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65  be held in cache
38310 0a 2a 2a 20 75 6e 74 69 6c 20 74 68 65 20 63 6c  .** until the cl
38320 6f 73 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 66  ose operation if
38330 20 74 68 65 79 20 77 69 6c 6c 20 66 69 74 2e 0a   they will fit..
38340 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 74 68  **.** Closing th
38350 65 20 42 4c 4f 42 20 6f 66 74 65 6e 20 66 6f 72  e BLOB often for
38360 63 65 73 20 74 68 65 20 63 68 61 6e 67 65 73 0a  ces the changes.
38370 2a 2a 20 6f 75 74 20 74 6f 20 64 69 73 6b 20 61  ** out to disk a
38380 6e 64 20 73 6f 20 69 66 20 61 6e 79 20 49 2f 4f  nd so if any I/O
38390 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 74   errors occur, t
383a0 68 65 79 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20  hey will likely 
383b0 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 20  occur.** at the 
383c0 74 69 6d 65 20 77 68 65 6e 20 74 68 65 20 42 4c  time when the BL
383d0 4f 42 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 41  OB is closed.  A
383e0 6e 79 20 65 72 72 6f 72 73 20 74 68 61 74 20 6f  ny errors that o
383f0 63 63 75 72 20 64 75 72 69 6e 67 0a 2a 2a 20 63  ccur during.** c
38400 6c 6f 73 69 6e 67 20 61 72 65 20 72 65 70 6f 72  losing are repor
38410 74 65 64 20 61 73 20 61 20 6e 6f 6e 2d 7a 65 72  ted as a non-zer
38420 6f 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2e 0a  o return value..
38430 2a 2a 0a 2a 2a 20 54 68 65 20 42 4c 4f 42 20 69  **.** The BLOB i
38440 73 20 63 6c 6f 73 65 64 20 75 6e 63 6f 6e 64 69  s closed uncondi
38450 74 69 6f 6e 61 6c 6c 79 2e 20 20 45 76 65 6e 20  tionally.  Even 
38460 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  if this routine 
38470 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 20 65 72  returns.** an er
38480 72 6f 72 20 63 6f 64 65 2c 20 74 68 65 20 42 4c  ror code, the BL
38490 4f 42 20 69 73 20 73 74 69 6c 6c 20 63 6c 6f 73  OB is still clos
384a0 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69 6e  ed..**.** Callin
384b0 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  g this routine w
384c0 69 74 68 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74  ith a null point
384d0 65 72 20 28 77 68 69 63 68 20 61 73 20 77 6f 75  er (which as wou
384e0 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 0a 2a  ld be returned.*
384f0 2a 20 62 79 20 66 61 69 6c 65 64 20 63 61 6c 6c  * by failed call
38500 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f   to [sqlite3_blo
38510 62 5f 6f 70 65 6e 28 29 5d 29 20 69 73 20 61 20  b_open()]) is a 
38520 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a  harmless no-op..
38530 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
38540 74 73 3a 0a 2a 2a 20 5b 48 31 37 38 33 33 5d 20  ts:.** [H17833] 
38550 5b 48 31 37 38 33 36 5d 20 5b 48 31 37 38 33 39  [H17836] [H17839
38560 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
38570 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
38580 5f 63 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 62  _close(sqlite3_b
38590 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  lob *);../*.** C
385a0 41 50 49 33 52 45 46 3a 20 52 65 74 75 72 6e 20  API3REF: Return 
385b0 54 68 65 20 53 69 7a 65 20 4f 66 20 41 6e 20 4f  The Size Of An O
385c0 70 65 6e 20 42 4c 4f 42 20 7b 48 31 37 38 34 30  pen BLOB {H17840
385d0 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a  } <S30230>.**.**
385e0 20 52 65 74 75 72 6e 73 20 74 68 65 20 73 69 7a   Returns the siz
385f0 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68  e in bytes of th
38600 65 20 42 4c 4f 42 20 61 63 63 65 73 73 69 62 6c  e BLOB accessibl
38610 65 20 76 69 61 20 74 68 65 20 0a 2a 2a 20 73 75  e via the .** su
38620 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65  ccessfully opene
38630 64 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20  d [BLOB handle] 
38640 69 6e 20 69 74 73 20 6f 6e 6c 79 20 61 72 67 75  in its only argu
38650 6d 65 6e 74 2e 20 20 54 68 65 0a 2a 2a 20 69 6e  ment.  The.** in
38660 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 49  cremental blob I
38670 2f 4f 20 72 6f 75 74 69 6e 65 73 20 63 61 6e 20  /O routines can 
38680 6f 6e 6c 79 20 72 65 61 64 20 6f 72 20 6f 76 65  only read or ove
38690 72 77 72 69 74 69 6e 67 20 65 78 69 73 74 69 6e  rwriting existin
386a0 67 0a 2a 2a 20 62 6c 6f 62 20 63 6f 6e 74 65 6e  g.** blob conten
386b0 74 3b 20 74 68 65 79 20 63 61 6e 6e 6f 74 20 63  t; they cannot c
386c0 68 61 6e 67 65 20 74 68 65 20 73 69 7a 65 20 6f  hange the size o
386d0 66 20 61 20 62 6c 6f 62 2e 0a 2a 2a 0a 2a 2a 20  f a blob..**.** 
386e0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c  This routine onl
386f0 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c  y works on a [BL
38700 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 68  OB handle] which
38710 20 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65   has been create
38720 64 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 20  d.** by a prior 
38730 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20  successful call 
38740 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  to [sqlite3_blob
38750 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 69  _open()] and whi
38760 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 65  ch has not.** be
38770 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71  en closed by [sq
38780 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65  lite3_blob_close
38790 28 29 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e  ()].  Passing an
387a0 79 20 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20  y other pointer 
387b0 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f  in.** to this ro
387c0 75 74 69 6e 65 20 72 65 73 75 6c 74 73 20 69 6e  utine results in
387d0 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 70   undefined and p
387e0 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 69 72 61  robably undesira
387f0 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a  ble behavior..**
38800 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e 74 73  .** Requirements
38810 3a 0a 2a 2a 20 5b 48 31 37 38 34 33 5d 0a 2a 2f  :.** [H17843].*/
38820 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
38830 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74  sqlite3_blob_byt
38840 65 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20  es(sqlite3_blob 
38850 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
38860 52 45 46 3a 20 52 65 61 64 20 44 61 74 61 20 46  REF: Read Data F
38870 72 6f 6d 20 41 20 42 4c 4f 42 20 49 6e 63 72 65  rom A BLOB Incre
38880 6d 65 6e 74 61 6c 6c 79 20 7b 48 31 37 38 35 30  mentally {H17850
38890 7d 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a  } <S30230>.**.**
388a0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
388b0 73 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 64  s used to read d
388c0 61 74 61 20 66 72 6f 6d 20 61 6e 20 6f 70 65 6e  ata from an open
388d0 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69   [BLOB handle] i
388e0 6e 74 6f 20 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d  nto a.** caller-
388f0 73 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 2e  supplied buffer.
38900 20 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74 61   N bytes of data
38910 20 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f   are copied into
38920 20 62 75 66 66 65 72 20 5a 0a 2a 2a 20 66 72 6f   buffer Z.** fro
38930 6d 20 74 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c  m the open BLOB,
38940 20 73 74 61 72 74 69 6e 67 20 61 74 20 6f 66 66   starting at off
38950 73 65 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a  set iOffset..**.
38960 2a 2a 20 49 66 20 6f 66 66 73 65 74 20 69 4f 66  ** If offset iOf
38970 66 73 65 74 20 69 73 20 6c 65 73 73 20 74 68 61  fset is less tha
38980 6e 20 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74  n N bytes from t
38990 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 42 4c  he end of the BL
389a0 4f 42 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45  OB,.** [SQLITE_E
389b0 52 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65  RROR] is returne
389c0 64 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73  d and no data is
389d0 20 72 65 61 64 2e 20 20 49 66 20 4e 20 6f 72 20   read.  If N or 
389e0 69 4f 66 66 73 65 74 20 69 73 0a 2a 2a 20 6c 65  iOffset is.** le
389f0 73 73 20 74 68 61 6e 20 7a 65 72 6f 2c 20 5b 53  ss than zero, [S
38a00 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20  QLITE_ERROR] is 
38a10 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20  returned and no 
38a20 64 61 74 61 20 69 73 20 72 65 61 64 2e 0a 2a 2a  data is read..**
38a30 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   The size of the
38a40 20 62 6c 6f 62 20 28 61 6e 64 20 68 65 6e 63 65   blob (and hence
38a50 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c   the maximum val
38a60 75 65 20 6f 66 20 4e 2b 69 4f 66 66 73 65 74 29  ue of N+iOffset)
38a70 0a 2a 2a 20 63 61 6e 20 62 65 20 64 65 74 65 72  .** can be deter
38a80 6d 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65 20  mined using the 
38a90 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79  [sqlite3_blob_by
38aa0 74 65 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65  tes()] interface
38ab0 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74 74 65 6d  ..**.** An attem
38ac0 70 74 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20  pt to read from 
38ad0 61 6e 20 65 78 70 69 72 65 64 20 5b 42 4c 4f 42  an expired [BLOB
38ae0 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73 20 77   handle] fails w
38af0 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20  ith an.** error 
38b00 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f  code of [SQLITE_
38b10 41 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  ABORT]..**.** On
38b20 20 73 75 63 63 65 73 73 2c 20 53 51 4c 49 54 45   success, SQLITE
38b30 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e  _OK is returned.
38b40 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61  .** Otherwise, a
38b50 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f  n [error code] o
38b60 72 20 61 6e 20 5b 65 78 74 65 6e 64 65 64 20 65  r an [extended e
38b70 72 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65  rror code] is re
38b80 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  turned..**.** Th
38b90 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20  is routine only 
38ba0 77 6f 72 6b 73 20 6f 6e 20 61 20 5b 42 4c 4f 42  works on a [BLOB
38bb0 20 68 61 6e 64 6c 65 5d 20 77 68 69 63 68 20 68   handle] which h
38bc0 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64 0a  as been created.
38bd0 2a 2a 20 62 79 20 61 20 70 72 69 6f 72 20 73 75  ** by a prior su
38be0 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f  ccessful call to
38bf0 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f   [sqlite3_blob_o
38c00 70 65 6e 28 29 5d 20 61 6e 64 20 77 68 69 63 68  pen()] and which
38c10 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 62 65 65 6e   has not.** been
38c20 20 63 6c 6f 73 65 64 20 62 79 20 5b 73 71 6c 69   closed by [sqli
38c30 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29  te3_blob_close()
38c40 5d 2e 20 20 50 61 73 73 69 6e 67 20 61 6e 79 20  ].  Passing any 
38c50 6f 74 68 65 72 20 70 6f 69 6e 74 65 72 20 69 6e  other pointer in
38c60 0a 2a 2a 20 74 6f 20 74 68 69 73 20 72 6f 75 74  .** to this rout
38c70 69 6e 65 20 72 65 73 75 6c 74 73 20 69 6e 20 75  ine results in u
38c80 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 70 72 6f  ndefined and pro
38c90 62 61 62 6c 79 20 75 6e 64 65 73 69 72 61 62 6c  bably undesirabl
38ca0 65 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a 0a 2a  e behavior..**.*
38cb0 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c  * See also: [sql
38cc0 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28  ite3_blob_write(
38cd0 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 69 72  )]..**.** Requir
38ce0 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48 31 37 38  ements:.** [H178
38cf0 35 33 5d 20 5b 48 31 37 38 35 36 5d 20 5b 48 31  53] [H17856] [H1
38d00 37 38 35 39 5d 20 5b 48 31 37 38 36 32 5d 20 5b  7859] [H17862] [
38d10 48 31 37 38 36 33 5d 20 5b 48 31 37 38 36 35 5d  H17863] [H17865]
38d20 20 5b 48 31 37 38 36 38 5d 0a 2a 2f 0a 53 51 4c   [H17868].*/.SQL
38d30 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
38d40 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71  te3_blob_read(sq
38d50 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 2c 20 76 6f  lite3_blob *, vo
38d60 69 64 20 2a 5a 2c 20 69 6e 74 20 4e 2c 20 69 6e  id *Z, int N, in
38d70 74 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a  t iOffset);../*.
38d80 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 57 72 69  ** CAPI3REF: Wri
38d90 74 65 20 44 61 74 61 20 49 6e 74 6f 20 41 20 42  te Data Into A B
38da0 4c 4f 42 20 49 6e 63 72 65 6d 65 6e 74 61 6c 6c  LOB Incrementall
38db0 79 20 7b 48 31 37 38 37 30 7d 20 3c 53 33 30 32  y {H17870} <S302
38dc0 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  30>.**.** This f
38dd0 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
38de0 74 6f 20 77 72 69 74 65 20 64 61 74 61 20 69 6e  to write data in
38df0 74 6f 20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42  to an open [BLOB
38e00 20 68 61 6e 64 6c 65 5d 20 66 72 6f 6d 20 61 0a   handle] from a.
38e10 2a 2a 20 63 61 6c 6c 65 72 2d 73 75 70 70 6c 69  ** caller-suppli
38e20 65 64 20 62 75 66 66 65 72 2e 20 4e 20 62 79 74  ed buffer. N byt
38e30 65 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 63  es of data are c
38e40 6f 70 69 65 64 20 66 72 6f 6d 20 74 68 65 20 62  opied from the b
38e50 75 66 66 65 72 20 5a 0a 2a 2a 20 69 6e 74 6f 20  uffer Z.** into 
38e60 74 68 65 20 6f 70 65 6e 20 42 4c 4f 42 2c 20 73  the open BLOB, s
38e70 74 61 72 74 69 6e 67 20 61 74 20 6f 66 66 73 65  tarting at offse
38e80 74 20 69 4f 66 66 73 65 74 2e 0a 2a 2a 0a 2a 2a  t iOffset..**.**
38e90 20 49 66 20 74 68 65 20 5b 42 4c 4f 42 20 68 61   If the [BLOB ha
38ea0 6e 64 6c 65 5d 20 70 61 73 73 65 64 20 61 73 20  ndle] passed as 
38eb0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
38ec0 6e 74 20 77 61 73 20 6e 6f 74 20 6f 70 65 6e 65  nt was not opene
38ed0 64 20 66 6f 72 0a 2a 2a 20 77 72 69 74 69 6e 67  d for.** writing
38ee0 20 28 74 68 65 20 66 6c 61 67 73 20 70 61 72 61   (the flags para
38ef0 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65  meter to [sqlite
38f00 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 77  3_blob_open()] w
38f10 61 73 20 7a 65 72 6f 29 2c 0a 2a 2a 20 74 68 69  as zero),.** thi
38f20 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
38f30 6e 73 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f  ns [SQLITE_READO
38f40 4e 4c 59 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  NLY]..**.** This
38f50 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 6f 6e   function may on
38f60 6c 79 20 6d 6f 64 69 66 79 20 74 68 65 20 63 6f  ly modify the co
38f70 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 42 4c  ntents of the BL
38f80 4f 42 3b 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 74  OB; it is.** not
38f90 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 69 6e 63   possible to inc
38fa0 72 65 61 73 65 20 74 68 65 20 73 69 7a 65 20 6f  rease the size o
38fb0 66 20 61 20 42 4c 4f 42 20 75 73 69 6e 67 20 74  f a BLOB using t
38fc0 68 69 73 20 41 50 49 2e 0a 2a 2a 20 49 66 20 6f  his API..** If o
38fd0 66 66 73 65 74 20 69 4f 66 66 73 65 74 20 69 73  ffset iOffset is
38fe0 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 79 74   less than N byt
38ff0 65 73 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20  es from the end 
39000 6f 66 20 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a 20  of the BLOB,.** 
39010 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69  [SQLITE_ERROR] i
39020 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e  s returned and n
39030 6f 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65  o data is writte
39040 6e 2e 20 20 49 66 20 4e 20 69 73 0a 2a 2a 20 6c  n.  If N is.** l
39050 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 20 5b 53  ess than zero [S
39060 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20  QLITE_ERROR] is 
39070 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20  returned and no 
39080 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 2e  data is written.
39090 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20  .** The size of 
390a0 74 68 65 20 42 4c 4f 42 20 28 61 6e 64 20 68 65  the BLOB (and he
390b0 6e 63 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  nce the maximum 
390c0 76 61 6c 75 65 20 6f 66 20 4e 2b 69 4f 66 66 73  value of N+iOffs
390d0 65 74 29 0a 2a 2a 20 63 61 6e 20 62 65 20 64 65  et).** can be de
390e0 74 65 72 6d 69 6e 65 64 20 75 73 69 6e 67 20 74  termined using t
390f0 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  he [sqlite3_blob
39100 5f 62 79 74 65 73 28 29 5d 20 69 6e 74 65 72 66  _bytes()] interf
39110 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74  ace..**.** An at
39120 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20 74  tempt to write t
39130 6f 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42 4c  o an expired [BL
39140 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c 73  OB handle] fails
39150 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72 6f   with an.** erro
39160 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49 54  r code of [SQLIT
39170 45 5f 41 42 4f 52 54 5d 2e 20 20 57 72 69 74 65  E_ABORT].  Write
39180 73 20 74 6f 20 74 68 65 20 42 4c 4f 42 20 74 68  s to the BLOB th
39190 61 74 20 6f 63 63 75 72 72 65 64 0a 2a 2a 20 62  at occurred.** b
391a0 65 66 6f 72 65 20 74 68 65 20 5b 42 4c 4f 42 20  efore the [BLOB 
391b0 68 61 6e 64 6c 65 5d 20 65 78 70 69 72 65 64 20  handle] expired 
391c0 61 72 65 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62  are not rolled b
391d0 61 63 6b 20 62 79 20 74 68 65 0a 2a 2a 20 65 78  ack by the.** ex
391e0 70 69 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  piration of the 
391f0 68 61 6e 64 6c 65 2c 20 74 68 6f 75 67 68 20 6f  handle, though o
39200 66 20 63 6f 75 72 73 65 20 74 68 6f 73 65 20 63  f course those c
39210 68 61 6e 67 65 73 20 6d 69 67 68 74 0a 2a 2a 20  hanges might.** 
39220 68 61 76 65 20 62 65 65 6e 20 6f 76 65 72 77 72  have been overwr
39230 69 74 74 65 6e 20 62 79 20 74 68 65 20 73 74 61  itten by the sta
39240 74 65 6d 65 6e 74 20 74 68 61 74 20 65 78 70 69  tement that expi
39250 72 65 64 20 74 68 65 20 42 4c 4f 42 20 68 61 6e  red the BLOB han
39260 64 6c 65 0a 2a 2a 20 6f 72 20 62 79 20 6f 74 68  dle.** or by oth
39270 65 72 20 69 6e 64 65 70 65 6e 64 65 6e 74 20 73  er independent s
39280 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  tatements..**.**
39290 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c   On success, SQL
392a0 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
392b0 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  ed..** Otherwise
392c0 2c 20 61 6e 20 20 5b 65 72 72 6f 72 20 63 6f 64  , an  [error cod
392d0 65 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e 64  e] or an [extend
392e0 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69  ed error code] i
392f0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
39300 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6f  * This routine o
39310 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 5b  nly works on a [
39320 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 77 68 69  BLOB handle] whi
39330 63 68 20 68 61 73 20 62 65 65 6e 20 63 72 65 61  ch has been crea
39340 74 65 64 0a 2a 2a 20 62 79 20 61 20 70 72 69 6f  ted.** by a prio
39350 72 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c  r successful cal
39360 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 6c  l to [sqlite3_bl
39370 6f 62 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 77  ob_open()] and w
39380 68 69 63 68 20 68 61 73 20 6e 6f 74 0a 2a 2a 20  hich has not.** 
39390 62 65 65 6e 20 63 6c 6f 73 65 64 20 62 79 20 5b  been closed by [
393a0 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f  sqlite3_blob_clo
393b0 73 65 28 29 5d 2e 20 20 50 61 73 73 69 6e 67 20  se()].  Passing 
393c0 61 6e 79 20 6f 74 68 65 72 20 70 6f 69 6e 74 65  any other pointe
393d0 72 20 69 6e 0a 2a 2a 20 74 6f 20 74 68 69 73 20  r in.** to this 
393e0 72 6f 75 74 69 6e 65 20 72 65 73 75 6c 74 73 20  routine results 
393f0 69 6e 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64  in undefined and
39400 20 70 72 6f 62 61 62 6c 79 20 75 6e 64 65 73 69   probably undesi
39410 72 61 62 6c 65 20 62 65 68 61 76 69 6f 72 2e 0a  rable behavior..
39420 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
39430 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65  [sqlite3_blob_re
39440 61 64 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 52 65 71  ad()]..**.** Req
39450 75 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 20 5b 48  uirements:.** [H
39460 31 37 38 37 33 5d 20 5b 48 31 37 38 37 34 5d 20  17873] [H17874] 
39470 5b 48 31 37 38 37 35 5d 20 5b 48 31 37 38 37 36  [H17875] [H17876
39480 5d 20 5b 48 31 37 38 37 37 5d 20 5b 48 31 37 38  ] [H17877] [H178
39490 37 39 5d 20 5b 48 31 37 38 38 32 5d 20 5b 48 31  79] [H17882] [H1
394a0 37 38 38 35 5d 0a 2a 2a 20 5b 48 31 37 38 38 38  7885].** [H17888
394b0 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ].*/.SQLITE_API 
394c0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
394d0 5f 77 72 69 74 65 28 73 71 6c 69 74 65 33 5f 62  _write(sqlite3_b
394e0 6c 6f 62 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69  lob *, const voi
394f0 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74  d *z, int n, int
39500 20 69 4f 66 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a   iOffset);../*.*
39510 2a 20 43 41 50 49 33 52 45 46 3a 20 56 69 72 74  * CAPI3REF: Virt
39520 75 61 6c 20 46 69 6c 65 20 53 79 73 74 65 6d 20  ual File System 
39530 4f 62 6a 65 63 74 73 20 7b 48 31 31 32 30 30 7d  Objects {H11200}
39540 20 3c 53 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S20100>.**.** 
39550 41 20 76 69 72 74 75 61 6c 20 66 69 6c 65 73 79  A virtual filesy
39560 73 74 65 6d 20 28 56 46 53 29 20 69 73 20 61 6e  stem (VFS) is an
39570 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f   [sqlite3_vfs] o
39580 62 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20 53 51  bject.** that SQ
39590 4c 69 74 65 20 75 73 65 73 20 74 6f 20 69 6e 74  Lite uses to int
395a0 65 72 61 63 74 0a 2a 2a 20 77 69 74 68 20 74 68  eract.** with th
395b0 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65  e underlying ope
395c0 72 61 74 69 6e 67 20 73 79 73 74 65 6d 2e 20 20  rating system.  
395d0 4d 6f 73 74 20 53 51 4c 69 74 65 20 62 75 69 6c  Most SQLite buil
395e0 64 73 20 63 6f 6d 65 20 77 69 74 68 20 61 0a 2a  ds come with a.*
395f0 2a 20 73 69 6e 67 6c 65 20 64 65 66 61 75 6c 74  * single default
39600 20 56 46 53 20 74 68 61 74 20 69 73 20 61 70 70   VFS that is app
39610 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 65  ropriate for the
39620 20 68 6f 73 74 20 63 6f 6d 70 75 74 65 72 2e 0a   host computer..
39630 2a 2a 20 4e 65 77 20 56 46 53 65 73 20 63 61 6e  ** New VFSes can
39640 20 62 65 20 72 65 67 69 73 74 65 72 65 64 20 61   be registered a
39650 6e 64 20 65 78 69 73 74 69 6e 67 20 56 46 53 65  nd existing VFSe
39660 73 20 63 61 6e 20 62 65 20 75 6e 72 65 67 69 73  s can be unregis
39670 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 6f  tered..** The fo
39680 6c 6c 6f 77 69 6e 67 20 69 6e 74 65 72 66 61 63  llowing interfac
39690 65 73 20 61 72 65 20 70 72 6f 76 69 64 65 64 2e  es are provided.
396a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
396b0 65 33 5f 76 66 73 5f 66 69 6e 64 28 29 20 69 6e  e3_vfs_find() in
396c0 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20  terface returns 
396d0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 56  a pointer to a V
396e0 46 53 20 67 69 76 65 6e 20 69 74 73 20 6e 61 6d  FS given its nam
396f0 65 2e 0a 2a 2a 20 4e 61 6d 65 73 20 61 72 65 20  e..** Names are 
39700 63 61 73 65 20 73 65 6e 73 69 74 69 76 65 2e 0a  case sensitive..
39710 2a 2a 20 4e 61 6d 65 73 20 61 72 65 20 7a 65 72  ** Names are zer
39720 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46  o-terminated UTF
39730 2d 38 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 20 49  -8 strings..** I
39740 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61  f there is no ma
39750 74 63 68 2c 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  tch, a NULL poin
39760 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ter is returned.
39770 0a 2a 2a 20 49 66 20 7a 56 66 73 4e 61 6d 65 20  .** If zVfsName 
39780 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65  is NULL then the
39790 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 73 20   default VFS is 
397a0 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
397b0 4e 65 77 20 56 46 53 65 73 20 61 72 65 20 72 65  New VFSes are re
397c0 67 69 73 74 65 72 65 64 20 77 69 74 68 20 73 71  gistered with sq
397d0 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74  lite3_vfs_regist
397e0 65 72 28 29 2e 0a 2a 2a 20 45 61 63 68 20 6e 65  er()..** Each ne
397f0 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20 74 68  w VFS becomes th
39800 65 20 64 65 66 61 75 6c 74 20 56 46 53 20 69 66  e default VFS if
39810 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20 66 6c   the makeDflt fl
39820 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20 54 68  ag is set..** Th
39830 65 20 73 61 6d 65 20 56 46 53 20 63 61 6e 20 62  e same VFS can b
39840 65 20 72 65 67 69 73 74 65 72 65 64 20 6d 75 6c  e registered mul
39850 74 69 70 6c 65 20 74 69 6d 65 73 20 77 69 74 68  tiple times with
39860 6f 75 74 20 69 6e 6a 75 72 79 2e 0a 2a 2a 20 54  out injury..** T
39870 6f 20 6d 61 6b 65 20 61 6e 20 65 78 69 73 74 69  o make an existi
39880 6e 67 20 56 46 53 20 69 6e 74 6f 20 74 68 65 20  ng VFS into the 
39890 64 65 66 61 75 6c 74 20 56 46 53 2c 20 72 65 67  default VFS, reg
398a0 69 73 74 65 72 20 69 74 20 61 67 61 69 6e 0a 2a  ister it again.*
398b0 2a 20 77 69 74 68 20 74 68 65 20 6d 61 6b 65 44  * with the makeD
398c0 66 6c 74 20 66 6c 61 67 20 73 65 74 2e 20 20 49  flt flag set.  I
398d0 66 20 74 77 6f 20 64 69 66 66 65 72 65 6e 74 20  f two different 
398e0 56 46 53 65 73 20 77 69 74 68 20 74 68 65 0a 2a  VFSes with the.*
398f0 2a 20 73 61 6d 65 20 6e 61 6d 65 20 61 72 65 20  * same name are 
39900 72 65 67 69 73 74 65 72 65 64 2c 20 74 68 65 20  registered, the 
39910 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65  behavior is unde
39920 66 69 6e 65 64 2e 20 20 49 66 20 61 0a 2a 2a 20  fined.  If a.** 
39930 56 46 53 20 69 73 20 72 65 67 69 73 74 65 72 65  VFS is registere
39940 64 20 77 69 74 68 20 61 20 6e 61 6d 65 20 74 68  d with a name th
39950 61 74 20 69 73 20 4e 55 4c 4c 20 6f 72 20 61 6e  at is NULL or an
39960 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 0a 2a   empty string,.*
39970 2a 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76  * then the behav
39980 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ior is undefined
39990 2e 0a 2a 2a 0a 2a 2a 20 55 6e 72 65 67 69 73 74  ..**.** Unregist
399a0 65 72 20 61 20 56 46 53 20 77 69 74 68 20 74 68  er a VFS with th
399b0 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e  e sqlite3_vfs_un
399c0 72 65 67 69 73 74 65 72 28 29 20 69 6e 74 65 72  register() inter
399d0 66 61 63 65 2e 0a 2a 2a 20 49 66 20 74 68 65 20  face..** If the 
399e0 64 65 66 61 75 6c 74 20 56 46 53 20 69 73 20 75  default VFS is u
399f0 6e 72 65 67 69 73 74 65 72 65 64 2c 20 61 6e 6f  nregistered, ano
39a00 74 68 65 72 20 56 46 53 20 69 73 20 63 68 6f 73  ther VFS is chos
39a10 65 6e 20 61 73 0a 2a 2a 20 74 68 65 20 64 65 66  en as.** the def
39a20 61 75 6c 74 2e 20 20 54 68 65 20 63 68 6f 69 63  ault.  The choic
39a30 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20 56 46  e for the new VF
39a40 53 20 69 73 20 61 72 62 69 74 72 61 72 79 2e 0a  S is arbitrary..
39a50 2a 2a 0a 2a 2a 20 52 65 71 75 69 72 65 6d 65 6e  **.** Requiremen
39a60 74 73 3a 0a 2a 2a 20 5b 48 31 31 32 30 33 5d 20  ts:.** [H11203] 
39a70 5b 48 31 31 32 30 36 5d 20 5b 48 31 31 32 30 39  [H11206] [H11209
39a80 5d 20 5b 48 31 31 32 31 32 5d 20 5b 48 31 31 32  ] [H11212] [H112
39a90 31 35 5d 20 5b 48 31 31 32 31 38 5d 0a 2a 2f 0a  15] [H11218].*/.
39aa0 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
39ab0 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 5f  e3_vfs *sqlite3_
39ac0 76 66 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63  vfs_find(const c
39ad0 68 61 72 20 2a 7a 56 66 73 4e 61 6d 65 29 3b 0a  har *zVfsName);.
39ae0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
39af0 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73  qlite3_vfs_regis
39b00 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a  ter(sqlite3_vfs*
39b10 2c 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29 3b  , int makeDflt);
39b20 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
39b30 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65  sqlite3_vfs_unre
39b40 67 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76  gister(sqlite3_v
39b50 66 73 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  fs*);../*.** CAP
39b60 49 33 52 45 46 3a 20 4d 75 74 65 78 65 73 20 7b  I3REF: Mutexes {
39b70 48 31 37 30 30 30 7d 20 3c 53 32 30 30 30 30 3e  H17000} <S20000>
39b80 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74  .**.** The SQLit
39b90 65 20 63 6f 72 65 20 75 73 65 73 20 74 68 65 73  e core uses thes
39ba0 65 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 74  e routines for t
39bb0 68 72 65 61 64 0a 2a 2a 20 73 79 6e 63 68 72 6f  hread.** synchro
39bc0 6e 69 7a 61 74 69 6f 6e 2e 20 54 68 6f 75 67 68  nization. Though
39bd0 20 74 68 65 79 20 61 72 65 20 69 6e 74 65 6e 64   they are intend
39be0 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a  ed for internal.
39bf0 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65  ** use by SQLite
39c00 2c 20 63 6f 64 65 20 74 68 61 74 20 6c 69 6e 6b  , code that link
39c10 73 20 61 67 61 69 6e 73 74 20 53 51 4c 69 74 65  s against SQLite
39c20 20 69 73 0a 2a 2a 20 70 65 72 6d 69 74 74 65 64   is.** permitted
39c30 20 74 6f 20 75 73 65 20 61 6e 79 20 6f 66 20 74   to use any of t
39c40 68 65 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a  hese routines..*
39c50 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20  *.** The SQLite 
39c60 73 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6e 74  source code cont
39c70 61 69 6e 73 20 6d 75 6c 74 69 70 6c 65 20 69 6d  ains multiple im
39c80 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a  plementations.**
39c90 20 6f 66 20 74 68 65 73 65 20 6d 75 74 65 78 20   of these mutex 
39ca0 72 6f 75 74 69 6e 65 73 2e 20 20 41 6e 20 61 70  routines.  An ap
39cb0 70 72 6f 70 72 69 61 74 65 20 69 6d 70 6c 65 6d  propriate implem
39cc0 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73  entation.** is s
39cd0 65 6c 65 63 74 65 64 20 61 75 74 6f 6d 61 74 69  elected automati
39ce0 63 61 6c 6c 79 20 61 74 20 63 6f 6d 70 69 6c 65  cally at compile
39cf0 2d 74 69 6d 65 2e 20 20 54 68 65 20 66 6f 6c 6c  -time.  The foll
39d00 6f 77 69 6e 67 0a 2a 2a 20 69 6d 70 6c 65 6d 65  owing.** impleme
39d10 6e 74 61 74 69 6f 6e 73 20 61 72 65 20 61 76 61  ntations are ava
39d20 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 53 51  ilable in the SQ
39d30 4c 69 74 65 20 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a  Lite core:.**.**
39d40 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 20   <ul>.** <li>   
39d50 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32  SQLITE_MUTEX_OS2
39d60 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54  .** <li>   SQLIT
39d70 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 0a  E_MUTEX_PTHREAD.
39d80 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45  ** <li>   SQLITE
39d90 5f 4d 55 54 45 58 5f 57 33 32 0a 2a 2a 20 3c 6c  _MUTEX_W32.** <l
39da0 69 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  i>   SQLITE_MUTE
39db0 58 5f 4e 4f 4f 50 0a 2a 2a 20 3c 2f 75 6c 3e 0a  X_NOOP.** </ul>.
39dc0 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45  **.** The SQLITE
39dd0 5f 4d 55 54 45 58 5f 4e 4f 4f 50 20 69 6d 70 6c  _MUTEX_NOOP impl
39de0 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 20  ementation is a 
39df0 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 0a  set of routines.
39e00 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 20  ** that does no 
39e10 72 65 61 6c 20 6c 6f 63 6b 69 6e 67 20 61 6e 64  real locking and
39e20 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20   is appropriate 
39e30 66 6f 72 20 75 73 65 20 69 6e 0a 2a 2a 20 61 20  for use in.** a 
39e40 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20  single-threaded 
39e50 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 54 68  application.  Th
39e60 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f  e SQLITE_MUTEX_O
39e70 53 32 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55  S2,.** SQLITE_MU
39e80 54 45 58 5f 50 54 48 52 45 41 44 2c 20 61 6e 64  TEX_PTHREAD, and
39e90 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33   SQLITE_MUTEX_W3
39ea0 32 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  2 implementation
39eb0 73 0a 2a 2a 20 61 72 65 20 61 70 70 72 6f 70 72  s.** are appropr
39ec0 69 61 74 65 20 66 6f 72 20 75 73 65 20 6f 6e 20  iate for use on 
39ed0 4f 53 2f 32 2c 20 55 6e 69 78 2c 20 61 6e 64 20  OS/2, Unix, and 
39ee0 57 69 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49  Windows..**.** I
39ef0 66 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70  f SQLite is comp
39f00 69 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 51  iled with the SQ
39f10 4c 49 54 45 5f 4d 55 54 45 58 5f 41 50 50 44 45  LITE_MUTEX_APPDE
39f20 46 20 70 72 65 70 72 6f 63 65 73 73 6f 72 0a 2a  F preprocessor.*
39f30 2a 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20  * macro defined 
39f40 28 77 69 74 68 20 22 2d 44 53 51 4c 49 54 45 5f  (with "-DSQLITE_
39f50 4d 55 54 45 58 5f 41 50 50 44 45 46 3d 31 22 29  MUTEX_APPDEF=1")
39f60 2c 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 0a  , then no mutex.
39f70 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
39f80 6e 20 69 73 20 69 6e 63 6c 75 64 65 64 20 77 69  n is included wi
39f90 74 68 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20  th the library. 
39fa0 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
39fb0 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  .** application 
39fc0 6d 75 73 74 20 73 75 70 70 6c 79 20 61 20 63 75  must supply a cu
39fd0 73 74 6f 6d 20 6d 75 74 65 78 20 69 6d 70 6c 65  stom mutex imple
39fe0 6d 65 6e 74 61 74 69 6f 6e 20 75 73 69 6e 67 20  mentation using 
39ff0 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43  the.** [SQLITE_C
3a000 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70 74  ONFIG_MUTEX] opt
3a010 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
3a020 65 33 5f 63 6f 6e 66 69 67 28 29 20 66 75 6e 63  e3_config() func
3a030 74 69 6f 6e 0a 2a 2a 20 62 65 66 6f 72 65 20 63  tion.** before c
3a040 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69  alling sqlite3_i
3a050 6e 69 74 69 61 6c 69 7a 65 28 29 20 6f 72 20 61  nitialize() or a
3a060 6e 79 20 6f 74 68 65 72 20 70 75 62 6c 69 63 20  ny other public 
3a070 73 71 6c 69 74 65 33 5f 0a 2a 2a 20 66 75 6e 63  sqlite3_.** func
3a080 74 69 6f 6e 20 74 68 61 74 20 63 61 6c 6c 73 20  tion that calls 
3a090 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
3a0a0 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  ze()..**.** {H17
3a0b0 30 31 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33  011} The sqlite3
3a0c0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72  _mutex_alloc() r
3a0d0 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73  outine allocates
3a0e0 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20   a new.** mutex 
3a0f0 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f  and returns a po
3a100 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 7b 48 31  inter to it. {H1
3a110 37 30 31 32 7d 20 49 66 20 69 74 20 72 65 74 75  7012} If it retu
3a120 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74  rns NULL.** that
3a130 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75   means that a mu
3a140 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65  tex could not be
3a150 20 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 48 31 37   allocated. {H17
3a160 30 31 33 7d 20 53 51 4c 69 74 65 0a 2a 2a 20 77  013} SQLite.** w
3a170 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73  ill unwind its s
3a180 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20  tack and return 
3a190 61 6e 20 65 72 72 6f 72 2e 20 7b 48 31 37 30 31  an error. {H1701
3a1a0 34 7d 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a  4} The argument.
3a1b0 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75  ** to sqlite3_mu
3a1c0 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f  tex_alloc() is o
3a1d0 6e 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65  ne of these inte
3a1e0 67 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a  ger constants:.*
3a1f0 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  *.** <ul>.** <li
3a200 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
3a210 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  FAST.** <li>  SQ
3a220 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
3a230 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51  SIVE.** <li>  SQ
3a240 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
3a250 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e  C_MASTER.** <li>
3a260 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
3a270 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69  TATIC_MEM.** <li
3a280 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  >  SQLITE_MUTEX_
3a290 53 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c  STATIC_MEM2.** <
3a2a0 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
3a2b0 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a  X_STATIC_PRNG.**
3a2c0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
3a2d0 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a  TEX_STATIC_LRU.*
3a2e0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
3a2f0 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32  UTEX_STATIC_LRU2
3a300 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20  .** </ul>.**.** 
3a310 7b 48 31 37 30 31 35 7d 20 54 68 65 20 66 69 72  {H17015} The fir
3a320 73 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73  st two constants
3a330 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d   cause sqlite3_m
3a340 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20  utex_alloc() to 
3a350 63 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20  create.** a new 
3a360 6d 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20  mutex.  The new 
3a370 6d 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69  mutex is recursi
3a380 76 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d  ve when SQLITE_M
3a390 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a  UTEX_RECURSIVE.*
3a3a0 2a 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f  * is used but no
3a3b0 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f  t necessarily so
3a3c0 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54   when SQLITE_MUT
3a3d0 45 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e  EX_FAST is used.
3a3e0 20 7b 45 4e 44 7d 0a 2a 2a 20 54 68 65 20 6d 75   {END}.** The mu
3a3f0 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  tex implementati
3a400 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  on does not need
3a410 20 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69   to make a disti
3a420 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65  nction.** betwee
3a430 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  n SQLITE_MUTEX_R
3a440 45 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c  ECURSIVE and SQL
3a450 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69  ITE_MUTEX_FAST i
3a460 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74  f it does.** not
3a470 20 77 61 6e 74 20 74 6f 2e 20 20 7b 48 31 37 30   want to.  {H170
3a480 31 36 7d 20 42 75 74 20 53 51 4c 69 74 65 20 77  16} But SQLite w
3a490 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74  ill only request
3a4a0 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74   a recursive mut
3a4b0 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77  ex in.** cases w
3a4c0 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e  here it really n
3a4d0 65 65 64 73 20 6f 6e 65 2e 20 20 7b 45 4e 44 7d  eeds one.  {END}
3a4e0 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e   If a faster non
3a4f0 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78  -recursive mutex
3a500 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
3a510 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20  on is available 
3a520 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74  on the host plat
3a530 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20  form, the mutex 
3a540 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67  subsystem.** mig
3a550 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61  ht return such a
3a560 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e   mutex in respon
3a570 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54  se to SQLITE_MUT
3a580 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 7b  EX_FAST..**.** {
3a590 48 31 37 30 31 37 7d 20 54 68 65 20 6f 74 68 65  H17017} The othe
3a5a0 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65  r allowed parame
3a5b0 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f  ters to sqlite3_
3a5c0 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61  mutex_alloc() ea
3a5d0 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70  ch return.** a p
3a5e0 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74  ointer to a stat
3a5f0 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d  ic preexisting m
3a600 75 74 65 78 2e 20 7b 45 4e 44 7d 20 20 53 69 78  utex. {END}  Six
3a610 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20   static mutexes 
3a620 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74  are.** used by t
3a630 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 69  he current versi
3a640 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46  on of SQLite.  F
3a650 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f  uture versions o
3a660 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20  f SQLite.** may 
3a670 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73  add additional s
3a680 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20  tatic mutexes.  
3a690 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61  Static mutexes a
3a6a0 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a  re for internal.
3a6b0 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65  ** use by SQLite
3a6c0 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74   only.  Applicat
3a6d0 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 51  ions that use SQ
3a6e0 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f  Lite mutexes sho
3a6f0 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20  uld.** use only 
3a700 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65  the dynamic mute
3a710 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20  xes returned by 
3a720 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
3a730 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d  T or.** SQLITE_M
3a740 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a  UTEX_RECURSIVE..
3a750 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 38 7d 20 4e  **.** {H17018} N
3a760 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20  ote that if one 
3a770 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d  of the dynamic m
3a780 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20  utex parameters 
3a790 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41  (SQLITE_MUTEX_FA
3a7a0 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f  ST.** or SQLITE_
3a7b0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29  MUTEX_RECURSIVE)
3a7c0 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71   is used then sq
3a7d0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
3a7e0 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61  c().** returns a
3a7f0 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78   different mutex
3a800 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20   on every call. 
3a810 20 7b 48 31 37 30 33 34 7d 20 42 75 74 20 66 6f   {H17034} But fo
3a820 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20  r the static.** 
3a830 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65  mutex types, the
3a840 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72   same mutex is r
3a850 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79  eturned on every
3a860 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a   call that has.*
3a870 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20  * the same type 
3a880 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48  number..**.** {H
3a890 31 37 30 31 39 7d 20 54 68 65 20 73 71 6c 69 74  17019} The sqlit
3a8a0 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 29 20  e3_mutex_free() 
3a8b0 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61  routine dealloca
3a8c0 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79  tes a previously
3a8d0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 64 79  .** allocated dy
3a8e0 6e 61 6d 69 63 20 6d 75 74 65 78 2e 20 7b 48 31  namic mutex. {H1
3a8f0 37 30 32 30 7d 20 53 51 4c 69 74 65 20 69 73 20  7020} SQLite is 
3a900 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c  careful to deall
3a910 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 64  ocate every.** d
3a920 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 74 68 61  ynamic mutex tha
3a930 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 20  t it allocates. 
3a940 7b 41 31 37 30 32 31 7d 20 54 68 65 20 64 79 6e  {A17021} The dyn
3a950 61 6d 69 63 20 6d 75 74 65 78 65 73 20 6d 75 73  amic mutexes mus
3a960 74 20 6e 6f 74 20 62 65 20 69 6e 0a 2a 2a 20 75  t not be in.** u
3a970 73 65 20 77 68 65 6e 20 74 68 65 79 20 61 72 65  se when they are
3a980 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 41   deallocated. {A
3a990 31 37 30 32 32 7d 20 41 74 74 65 6d 70 74 69 6e  17022} Attemptin
3a9a0 67 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20  g to deallocate 
3a9b0 61 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65  a static.** mute
3a9c0 78 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64  x results in und
3a9d0 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e  efined behavior.
3a9e0 20 7b 48 31 37 30 32 33 7d 20 53 51 4c 69 74 65   {H17023} SQLite
3a9f0 20 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63 61 74   never deallocat
3aa00 65 73 0a 2a 2a 20 61 20 73 74 61 74 69 63 20 6d  es.** a static m
3aa10 75 74 65 78 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a  utex. {END}.**.*
3aa20 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
3aa30 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20  tex_enter() and 
3aa40 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
3aa50 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74  y() routines att
3aa60 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72  empt.** to enter
3aa70 20 61 20 6d 75 74 65 78 2e 20 7b 48 31 37 30 32   a mutex. {H1702
3aa80 34 7d 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68  4} If another th
3aa90 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20  read is already 
3aaa0 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78  within the mutex
3aab0 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74  ,.** sqlite3_mut
3aac0 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20  ex_enter() will 
3aad0 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65  block and sqlite
3aae0 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69  3_mutex_try() wi
3aaf0 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  ll return.** SQL
3ab00 49 54 45 5f 42 55 53 59 2e 20 7b 48 31 37 30 32  ITE_BUSY. {H1702
3ab10 35 7d 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f  5}  The sqlite3_
3ab20 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65  mutex_try() inte
3ab30 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 5b 53  rface returns [S
3ab40 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 75 70 6f  QLITE_OK].** upo
3ab50 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74  n successful ent
3ab60 72 79 2e 20 20 7b 48 31 37 30 32 36 7d 20 4d 75  ry.  {H17026} Mu
3ab70 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73  texes created us
3ab80 69 6e 67 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55  ing.** SQLITE_MU
3ab90 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61  TEX_RECURSIVE ca
3aba0 6e 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c  n be entered mul
3abb0 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74  tiple times by t
3abc0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 0a  he same thread..
3abd0 2a 2a 20 7b 48 31 37 30 32 37 7d 20 49 6e 20 73  ** {H17027} In s
3abe0 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a  uch cases the,.*
3abf0 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  * mutex must be 
3ac00 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20  exited an equal 
3ac10 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  number of times 
3ac20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74  before another t
3ac30 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74  hread.** can ent
3ac40 65 72 2e 20 20 7b 41 31 37 30 32 38 7d 20 49 66  er.  {A17028} If
3ac50 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
3ac60 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20   tries to enter 
3ac70 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6b 69 6e  any other.** kin
3ac80 64 20 6f 66 20 6d 75 74 65 78 20 6d 6f 72 65 20  d of mutex more 
3ac90 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62  than once, the b
3aca0 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66  ehavior is undef
3acb0 69 6e 65 64 2e 0a 2a 2a 20 7b 48 31 37 30 32 39  ined..** {H17029
3acc0 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65  } SQLite will ne
3acd0 76 65 72 20 65 78 68 69 62 69 74 0a 2a 2a 20 73  ver exhibit.** s
3ace0 75 63 68 20 62 65 68 61 76 69 6f 72 20 69 6e 20  uch behavior in 
3acf0 69 74 73 20 6f 77 6e 20 75 73 65 20 6f 66 20 6d  its own use of m
3ad00 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 6f  utexes..**.** So
3ad10 6d 65 20 73 79 73 74 65 6d 73 20 28 66 6f 72 20  me systems (for 
3ad20 65 78 61 6d 70 6c 65 2c 20 57 69 6e 64 6f 77 73  example, Windows
3ad30 20 39 35 29 20 64 6f 20 6e 6f 74 20 73 75 70 70   95) do not supp
3ad40 6f 72 74 20 74 68 65 20 6f 70 65 72 61 74 69 6f  ort the operatio
3ad50 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64  n.** implemented
3ad60 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   by sqlite3_mute
3ad70 78 5f 74 72 79 28 29 2e 20 20 4f 6e 20 74 68 6f  x_try().  On tho
3ad80 73 65 20 73 79 73 74 65 6d 73 2c 20 73 71 6c 69  se systems, sqli
3ad90 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 0a  te3_mutex_try().
3ada0 2a 2a 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72  ** will always r
3adb0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
3adc0 59 2e 20 20 7b 48 31 37 30 33 30 7d 20 54 68 65  Y.  {H17030} The
3add0 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6f 6e 6c   SQLite core onl
3ade0 79 20 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 73  y ever uses.** s
3adf0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
3ae00 28 29 20 61 73 20 61 6e 20 6f 70 74 69 6d 69 7a  () as an optimiz
3ae10 61 74 69 6f 6e 20 73 6f 20 74 68 69 73 20 69 73  ation so this is
3ae20 20 61 63 63 65 70 74 61 62 6c 65 20 62 65 68 61   acceptable beha
3ae30 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  vior..**.** {H17
3ae40 30 33 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33  031} The sqlite3
3ae50 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72  _mutex_leave() r
3ae60 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d  outine exits a m
3ae70 75 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a  utex that was.**
3ae80 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65   previously ente
3ae90 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20  red by the same 
3aea0 74 68 72 65 61 64 2e 20 20 7b 41 31 37 30 33 32  thread.  {A17032
3aeb0 7d 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a  } The behavior.*
3aec0 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69  * is undefined i
3aed0 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e  f the mutex is n
3aee0 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74  ot currently ent
3aef0 65 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63  ered by the.** c
3af00 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 6f 72  alling thread or
3af10 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
3af20 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 7b 48  y allocated.  {H
3af30 31 37 30 33 33 7d 20 53 51 4c 69 74 65 20 77 69  17033} SQLite wi
3af40 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 64 6f 20 65  ll.** never do e
3af50 69 74 68 65 72 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a  ither. {END}.**.
3af60 2a 2a 20 49 66 20 74 68 65 20 61 72 67 75 6d 65  ** If the argume
3af70 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75  nt to sqlite3_mu
3af80 74 65 78 5f 65 6e 74 65 72 28 29 2c 20 73 71 6c  tex_enter(), sql
3af90 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
3afa0 2c 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  , or.** sqlite3_
3afb0 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 69 73  mutex_leave() is
3afc0 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c   a NULL pointer,
3afd0 20 74 68 65 6e 20 61 6c 6c 20 74 68 72 65 65 20   then all three 
3afe0 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 62 65 68 61  routines.** beha
3aff0 76 65 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a  ve as no-ops..**
3b000 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73  .** See also: [s
3b010 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
3b020 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  d()] and [sqlite
3b030 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
3b040 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  )]..*/.SQLITE_AP
3b050 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  I sqlite3_mutex 
3b060 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61  *sqlite3_mutex_a
3b070 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54  lloc(int);.SQLIT
3b080 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
3b090 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71  e3_mutex_free(sq
3b0a0 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53  lite3_mutex*);.S
3b0b0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
3b0c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
3b0d0 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  er(sqlite3_mutex
3b0e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  *);.SQLITE_API i
3b0f0 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  nt sqlite3_mutex
3b100 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74  _try(sqlite3_mut
3b110 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ex*);.SQLITE_API
3b120 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75   void sqlite3_mu
3b130 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65  tex_leave(sqlite
3b140 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a  3_mutex*);../*.*
3b150 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65  * CAPI3REF: Mute
3b160 78 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 74  x Methods Object
3b170 20 7b 48 31 37 31 32 30 7d 20 3c 53 32 30 31 33   {H17120} <S2013
3b180 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
3b190 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  AL.**.** An inst
3b1a0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
3b1b0 75 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74  ucture defines t
3b1c0 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75  he low-level rou
3b1d0 74 69 6e 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f  tines.** used to
3b1e0 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 75 73   allocate and us
3b1f0 65 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a  e mutexes..**.**
3b200 20 55 73 75 61 6c 6c 79 2c 20 74 68 65 20 64 65   Usually, the de
3b210 66 61 75 6c 74 20 6d 75 74 65 78 20 69 6d 70 6c  fault mutex impl
3b220 65 6d 65 6e 74 61 74 69 6f 6e 73 20 70 72 6f 76  ementations prov
3b230 69 64 65 64 20 62 79 20 53 51 4c 69 74 65 20 61  ided by SQLite a
3b240 72 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74  re.** sufficient
3b250 2c 20 68 6f 77 65 76 65 72 20 74 68 65 20 75 73  , however the us
3b260 65 72 20 68 61 73 20 74 68 65 20 6f 70 74 69 6f  er has the optio
3b270 6e 20 6f 66 20 73 75 62 73 74 69 74 75 74 69 6e  n of substitutin
3b280 67 20 61 20 63 75 73 74 6f 6d 0a 2a 2a 20 69 6d  g a custom.** im
3b290 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72  plementation for
3b2a0 20 73 70 65 63 69 61 6c 69 7a 65 64 20 64 65 70   specialized dep
3b2b0 6c 6f 79 6d 65 6e 74 73 20 6f 72 20 73 79 73 74  loyments or syst
3b2c0 65 6d 73 20 66 6f 72 20 77 68 69 63 68 20 53 51  ems for which SQ
3b2d0 4c 69 74 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74  Lite.** does not
3b2e0 20 70 72 6f 76 69 64 65 20 61 20 73 75 69 74 61   provide a suita
3b2f0 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ble implementati
3b300 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  on. In this case
3b310 2c 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 63 72  , the user.** cr
3b320 65 61 74 65 73 20 61 6e 64 20 70 6f 70 75 6c 61  eates and popula
3b330 74 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20  tes an instance 
3b340 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
3b350 65 20 74 6f 20 70 61 73 73 0a 2a 2a 20 74 6f 20  e to pass.** to 
3b360 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
3b370 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20   along with the 
3b380 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
3b390 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a  UTEX] option..**
3b3a0 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 61   Additionally, a
3b3b0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
3b3c0 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e  is structure can
3b3d0 20 62 65 20 75 73 65 64 20 61 73 20 61 6e 0a 2a   be used as an.*
3b3e0 2a 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c  * output variabl
3b3f0 65 20 77 68 65 6e 20 71 75 65 72 79 69 6e 67 20  e when querying 
3b400 74 68 65 20 73 79 73 74 65 6d 20 66 6f 72 20 74  the system for t
3b410 68 65 20 63 75 72 72 65 6e 74 20 6d 75 74 65 78  he current mutex
3b420 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
3b430 6f 6e 2c 20 75 73 69 6e 67 20 74 68 65 20 5b 53  on, using the [S
3b440 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54  QLITE_CONFIG_GET
3b450 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a  MUTEX] option..*
3b460 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 49  *.** The xMutexI
3b470 6e 69 74 20 6d 65 74 68 6f 64 20 64 65 66 69 6e  nit method defin
3b480 65 64 20 62 79 20 74 68 69 73 20 73 74 72 75 63  ed by this struc
3b490 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 20  ture is invoked 
3b4a0 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 79  as.** part of sy
3b4b0 73 74 65 6d 20 69 6e 69 74 69 61 6c 69 7a 61 74  stem initializat
3b4c0 69 6f 6e 20 62 79 20 74 68 65 20 73 71 6c 69 74  ion by the sqlit
3b4d0 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
3b4e0 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 7b 48 31  function..** {H1
3b4f0 37 30 30 31 7d 20 54 68 65 20 78 4d 75 74 65 78  7001} The xMutex
3b500 49 6e 69 74 20 72 6f 75 74 69 6e 65 20 73 68 61  Init routine sha
3b510 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20  ll be called by 
3b520 53 51 4c 69 74 65 20 6f 6e 63 65 20 66 6f 72 20  SQLite once for 
3b530 65 61 63 68 0a 2a 2a 20 65 66 66 65 63 74 69 76  each.** effectiv
3b540 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  e call to [sqlit
3b550 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d  e3_initialize()]
3b560 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74  ..**.** The xMut
3b570 65 78 45 6e 64 20 6d 65 74 68 6f 64 20 64 65 66  exEnd method def
3b580 69 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72  ined by this str
3b590 75 63 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65  ucture is invoke
3b5a0 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20  d as.** part of 
3b5b0 73 79 73 74 65 6d 20 73 68 75 74 64 6f 77 6e 20  system shutdown 
3b5c0 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73  by the sqlite3_s
3b5d0 68 75 74 64 6f 77 6e 28 29 20 66 75 6e 63 74 69  hutdown() functi
3b5e0 6f 6e 2e 20 54 68 65 0a 2a 2a 20 69 6d 70 6c 65  on. The.** imple
3b5f0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69  mentation of thi
3b600 73 20 6d 65 74 68 6f 64 20 69 73 20 65 78 70 65  s method is expe
3b610 63 74 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20  cted to release 
3b620 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 0a  all outstanding.
3b630 2a 2a 20 72 65 73 6f 75 72 63 65 73 20 6f 62 74  ** resources obt
3b640 61 69 6e 65 64 20 62 79 20 74 68 65 20 6d 75 74  ained by the mut
3b650 65 78 20 6d 65 74 68 6f 64 73 20 69 6d 70 6c 65  ex methods imple
3b660 6d 65 6e 74 61 74 69 6f 6e 2c 20 65 73 70 65 63  mentation, espec
3b670 69 61 6c 6c 79 0a 2a 2a 20 74 68 6f 73 65 20 6f  ially.** those o
3b680 62 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 78  btained by the x
3b690 4d 75 74 65 78 49 6e 69 74 20 6d 65 74 68 6f 64  MutexInit method
3b6a0 2e 20 7b 48 31 37 30 30 33 7d 20 54 68 65 20 78  . {H17003} The x
3b6b0 4d 75 74 65 78 45 6e 64 28 29 0a 2a 2a 20 69 6e  MutexEnd().** in
3b6c0 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62 65  terface shall be
3b6d0 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f   invoked once fo
3b6e0 72 20 65 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b  r each call to [
3b6f0 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
3b700 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  ()]..**.** The r
3b710 65 6d 61 69 6e 69 6e 67 20 73 65 76 65 6e 20 6d  emaining seven m
3b720 65 74 68 6f 64 73 20 64 65 66 69 6e 65 64 20 62  ethods defined b
3b730 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  y this structure
3b740 20 28 78 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 2a   (xMutexAlloc,.*
3b750 2a 20 78 4d 75 74 65 78 46 72 65 65 2c 20 78 4d  * xMutexFree, xM
3b760 75 74 65 78 45 6e 74 65 72 2c 20 78 4d 75 74 65  utexEnter, xMute
3b770 78 54 72 79 2c 20 78 4d 75 74 65 78 4c 65 61 76  xTry, xMutexLeav
3b780 65 2c 20 78 4d 75 74 65 78 48 65 6c 64 20 61 6e  e, xMutexHeld an
3b790 64 0a 2a 2a 20 78 4d 75 74 65 78 4e 6f 74 68 65  d.** xMutexNothe
3b7a0 6c 64 29 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68  ld) implement th
3b7b0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65  e following inte
3b7c0 72 66 61 63 65 73 20 28 72 65 73 70 65 63 74 69  rfaces (respecti
3b7d0 76 65 6c 79 29 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c  vely):.**.** <ul
3b7e0 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71  >.**   <li>  [sq
3b7f0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
3b800 63 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20  c()] </li>.**   
3b810 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d  <li>  [sqlite3_m
3b820 75 74 65 78 5f 66 72 65 65 28 29 5d 20 3c 2f 6c  utex_free()] </l
3b830 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73  i>.**   <li>  [s
3b840 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
3b850 65 72 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20  er()] </li>.**  
3b860 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f   <li>  [sqlite3_
3b870 6d 75 74 65 78 5f 74 72 79 28 29 5d 20 3c 2f 6c  mutex_try()] </l
3b880 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73  i>.**   <li>  [s
3b890 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
3b8a0 76 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20  ve()] </li>.**  
3b8b0 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f   <li>  [sqlite3_
3b8c0 6d 75 74 65 78 5f 68 65 6c 64 28 29 5d 20 3c 2f  mutex_held()] </
3b8d0 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b  li>.**   <li>  [
3b8e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f  sqlite3_mutex_no
3b8f0 74 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a  theld()] </li>.*
3b900 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68  * </ul>.**.** Th
3b910 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e 63  e only differenc
3b920 65 20 69 73 20 74 68 61 74 20 74 68 65 20 70 75  e is that the pu
3b930 62 6c 69 63 20 73 71 6c 69 74 65 33 5f 58 58 58  blic sqlite3_XXX
3b940 20 66 75 6e 63 74 69 6f 6e 73 20 65 6e 75 6d 65   functions enume
3b950 72 61 74 65 64 0a 2a 2a 20 61 62 6f 76 65 20 73  rated.** above s
3b960 69 6c 65 6e 74 6c 79 20 69 67 6e 6f 72 65 20 61  ilently ignore a
3b970 6e 79 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 74  ny invocations t
3b980 68 61 74 20 70 61 73 73 20 61 20 4e 55 4c 4c 20  hat pass a NULL 
3b990 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 0a  pointer instead.
3b9a0 2a 2a 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75  ** of a valid mu
3b9b0 74 65 78 20 68 61 6e 64 6c 65 2e 20 54 68 65 20  tex handle. The 
3b9c0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
3b9d0 6f 66 20 74 68 65 20 6d 65 74 68 6f 64 73 20 64  of the methods d
3b9e0 65 66 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 69  efined.** by thi
3b9f0 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20  s structure are 
3ba00 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20  not required to 
3ba10 68 61 6e 64 6c 65 20 74 68 69 73 20 63 61 73 65  handle this case
3ba20 2c 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a  , the results.**
3ba30 20 6f 66 20 70 61 73 73 69 6e 67 20 61 20 4e 55   of passing a NU
3ba40 4c 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65  LL pointer inste
3ba50 61 64 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75  ad of a valid mu
3ba60 74 65 78 20 68 61 6e 64 6c 65 20 61 72 65 20 75  tex handle are u
3ba70 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 28 69 2e 65  ndefined.** (i.e
3ba80 2e 20 69 74 20 69 73 20 61 63 63 65 70 74 61 62  . it is acceptab
3ba90 6c 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e  le to provide an
3baa0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3bab0 74 68 61 74 20 73 65 67 66 61 75 6c 74 73 20 69  that segfaults i
3bac0 66 0a 2a 2a 20 69 74 20 69 73 20 70 61 73 73 65  f.** it is passe
3bad0 64 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  d a NULL pointer
3bae0 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75  )..**.** The xMu
3baf0 74 65 78 49 6e 69 74 28 29 20 6d 65 74 68 6f 64  texInit() method
3bb00 20 6d 75 73 74 20 62 65 20 74 68 72 65 61 64 73   must be threads
3bb10 61 66 65 2e 20 20 49 74 20 6d 75 73 74 20 62 65  afe.  It must be
3bb20 20 68 61 72 6d 6c 65 73 73 20 74 6f 0a 2a 2a 20   harmless to.** 
3bb30 69 6e 76 6f 6b 65 20 78 4d 75 74 65 78 49 6e 69  invoke xMutexIni
3bb40 74 28 29 20 6d 75 74 69 70 6c 65 20 74 69 6d 65  t() mutiple time
3bb50 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d  s within the sam
3bb60 65 20 70 72 6f 63 65 73 73 20 61 6e 64 20 77 69  e process and wi
3bb70 74 68 6f 75 74 0a 2a 2a 20 69 6e 74 65 72 76 65  thout.** interve
3bb80 6e 69 6e 67 20 63 61 6c 6c 73 20 74 6f 20 78 4d  ning calls to xM
3bb90 75 74 65 78 45 6e 64 28 29 2e 20 20 53 65 63 6f  utexEnd().  Seco
3bba0 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e  nd and subsequen
3bbb0 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 78 4d  t calls to.** xM
3bbc0 75 74 65 78 49 6e 69 74 28 29 20 6d 75 73 74 20  utexInit() must 
3bbd0 62 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a  be no-ops..**.**
3bbe0 20 78 4d 75 74 65 78 49 6e 69 74 28 29 20 6d 75   xMutexInit() mu
3bbf0 73 74 20 6e 6f 74 20 75 73 65 20 53 51 4c 69 74  st not use SQLit
3bc00 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
3bc10 69 6f 6e 20 28 5b 73 71 6c 69 74 65 33 5f 6d 61  ion ([sqlite3_ma
3bc20 6c 6c 6f 63 28 29 5d 0a 2a 2a 20 61 6e 64 20 69  lloc()].** and i
3bc30 74 73 20 61 73 73 6f 63 69 61 74 65 73 29 2e 20  ts associates). 
3bc40 20 53 69 6d 69 6c 61 72 6c 79 2c 20 78 4d 75 74   Similarly, xMut
3bc50 65 78 41 6c 6c 6f 63 28 29 20 6d 75 73 74 20 6e  exAlloc() must n
3bc60 6f 74 20 75 73 65 20 53 51 4c 69 74 65 20 6d 65  ot use SQLite me
3bc70 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69  mory.** allocati
3bc80 6f 6e 20 66 6f 72 20 61 20 73 74 61 74 69 63 20  on for a static 
3bc90 6d 75 74 65 78 2e 20 20 48 6f 77 65 76 65 72 20  mutex.  However 
3bca0 78 4d 75 74 65 78 41 6c 6c 6f 63 28 29 20 6d 61  xMutexAlloc() ma
3bcb0 79 20 75 73 65 20 53 51 4c 69 74 65 0a 2a 2a 20  y use SQLite.** 
3bcc0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
3bcd0 6e 20 66 6f 72 20 61 20 66 61 73 74 20 6f 72 20  n for a fast or 
3bce0 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 2e  recursive mutex.
3bcf0 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69  .**.** SQLite wi
3bd00 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78 4d  ll invoke the xM
3bd10 75 74 65 78 45 6e 64 28 29 20 6d 65 74 68 6f 64  utexEnd() method
3bd20 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 73   when [sqlite3_s
3bd30 68 75 74 64 6f 77 6e 28 29 5d 20 69 73 0a 2a 2a  hutdown()] is.**
3bd40 20 63 61 6c 6c 65 64 2c 20 62 75 74 20 6f 6e 6c   called, but onl
3bd50 79 20 69 66 20 74 68 65 20 70 72 69 6f 72 20 63  y if the prior c
3bd60 61 6c 6c 20 74 6f 20 78 4d 75 74 65 78 49 6e 69  all to xMutexIni
3bd70 74 20 72 65 74 75 72 6e 65 64 20 53 51 4c 49 54  t returned SQLIT
3bd80 45 5f 4f 4b 2e 0a 2a 2a 20 49 66 20 78 4d 75 74  E_OK..** If xMut
3bd90 65 78 49 6e 69 74 20 66 61 69 6c 73 20 69 6e 20  exInit fails in 
3bda0 61 6e 79 20 77 61 79 2c 20 69 74 20 69 73 20 65  any way, it is e
3bdb0 78 70 65 63 74 65 64 20 74 6f 20 63 6c 65 61 6e  xpected to clean
3bdc0 20 75 70 20 61 66 74 65 72 20 69 74 73 65 6c 66   up after itself
3bdd0 0a 2a 2a 20 70 72 69 6f 72 20 74 6f 20 72 65 74  .** prior to ret
3bde0 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 74 79 70 65 64  urning..*/.typed
3bdf0 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
3be00 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20  3_mutex_methods 
3be10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
3be20 74 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71  thods;.struct sq
3be30 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
3be40 6f 64 73 20 7b 0a 20 20 69 6e 74 20 28 2a 78 4d  ods {.  int (*xM
3be50 75 74 65 78 49 6e 69 74 29 28 76 6f 69 64 29 3b  utexInit)(void);
3be60 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 45  .  int (*xMutexE
3be70 6e 64 29 28 76 6f 69 64 29 3b 0a 20 20 73 71 6c  nd)(void);.  sql
3be80 69 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a 78 4d  ite3_mutex *(*xM
3be90 75 74 65 78 41 6c 6c 6f 63 29 28 69 6e 74 29 3b  utexAlloc)(int);
3bea0 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78  .  void (*xMutex
3beb0 46 72 65 65 29 28 73 71 6c 69 74 65 33 5f 6d 75  Free)(sqlite3_mu
3bec0 74 65 78 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28  tex *);.  void (
3bed0 2a 78 4d 75 74 65 78 45 6e 74 65 72 29 28 73 71  *xMutexEnter)(sq
3bee0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a  lite3_mutex *);.
3bef0 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 54 72    int (*xMutexTr
3bf00 79 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  y)(sqlite3_mutex
3bf10 20 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d   *);.  void (*xM
3bf20 75 74 65 78 4c 65 61 76 65 29 28 73 71 6c 69 74  utexLeave)(sqlit
3bf30 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69  e3_mutex *);.  i
3bf40 6e 74 20 28 2a 78 4d 75 74 65 78 48 65 6c 64 29  nt (*xMutexHeld)
3bf50 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
3bf60 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65  );.  int (*xMute
3bf70 78 4e 6f 74 68 65 6c 64 29 28 73 71 6c 69 74 65  xNotheld)(sqlite
3bf80 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 7d 3b 0a 0a  3_mutex *);.};..
3bf90 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
3bfa0 4d 75 74 65 78 20 56 65 72 69 66 69 63 61 74 69  Mutex Verificati
3bfb0 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48 31 37  on Routines {H17
3bfc0 30 38 30 7d 20 3c 53 32 30 31 33 30 3e 20 3c 53  080} <S20130> <S
3bfd0 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  30800>.**.** The
3bfe0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
3bff0 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65  eld() and sqlite
3c000 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
3c010 29 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72  ) routines.** ar
3c020 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75  e intended for u
3c030 73 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74  se inside assert
3c040 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 7b  () statements. {
3c050 48 31 37 30 38 31 7d 20 54 68 65 20 53 51 4c 69  H17081} The SQLi
3c060 74 65 20 63 6f 72 65 0a 2a 2a 20 6e 65 76 65 72  te core.** never
3c070 20 75 73 65 73 20 74 68 65 73 65 20 72 6f 75 74   uses these rout
3c080 69 6e 65 73 20 65 78 63 65 70 74 20 69 6e 73 69  ines except insi
3c090 64 65 20 61 6e 20 61 73 73 65 72 74 28 29 20 61  de an assert() a
3c0a0 6e 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a  nd applications.
3c0b0 2a 2a 20 61 72 65 20 61 64 76 69 73 65 64 20 74  ** are advised t
3c0c0 6f 20 66 6f 6c 6c 6f 77 20 74 68 65 20 6c 65 61  o follow the lea
3c0d0 64 20 6f 66 20 74 68 65 20 63 6f 72 65 2e 20 20  d of the core.  
3c0e0 7b 48 31 37 30 38 32 7d 20 54 68 65 20 63 6f 72  {H17082} The cor
3c0f0 65 20 6f 6e 6c 79 0a 2a 2a 20 70 72 6f 76 69 64  e only.** provid
3c100 65 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  es implementatio
3c110 6e 73 20 66 6f 72 20 74 68 65 73 65 20 72 6f 75  ns for these rou
3c120 74 69 6e 65 73 20 77 68 65 6e 20 69 74 20 69 73  tines when it is
3c130 20 63 6f 6d 70 69 6c 65 64 0a 2a 2a 20 77 69 74   compiled.** wit
3c140 68 20 74 68 65 20 53 51 4c 49 54 45 5f 44 45 42  h the SQLITE_DEB
3c150 55 47 20 66 6c 61 67 2e 20 20 7b 41 31 37 30 38  UG flag.  {A1708
3c160 37 7d 20 45 78 74 65 72 6e 61 6c 20 6d 75 74 65  7} External mute
3c170 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x implementation
3c180 73 0a 2a 2a 20 61 72 65 20 6f 6e 6c 79 20 72 65  s.** are only re
3c190 71 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69 64  quired to provid
3c1a0 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  e these routines
3c1b0 20 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47   if SQLITE_DEBUG
3c1c0 20 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64 20 61   is.** defined a
3c1d0 6e 64 20 69 66 20 4e 44 45 42 55 47 20 69 73 20  nd if NDEBUG is 
3c1e0 6e 6f 74 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a  not defined..**.
3c1f0 2a 2a 20 7b 48 31 37 30 38 33 7d 20 54 68 65 73  ** {H17083} Thes
3c200 65 20 72 6f 75 74 69 6e 65 73 20 73 68 6f 75 6c  e routines shoul
3c210 64 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 66  d return true if
3c220 20 74 68 65 20 6d 75 74 65 78 20 69 6e 20 74 68   the mutex in th
3c230 65 69 72 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  eir argument.** 
3c240 69 73 20 68 65 6c 64 20 6f 72 20 6e 6f 74 20 68  is held or not h
3c250 65 6c 64 2c 20 72 65 73 70 65 63 74 69 76 65 6c  eld, respectivel
3c260 79 2c 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e  y, by the callin
3c270 67 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20  g thread..**.** 
3c280 7b 58 31 37 30 38 34 7d 20 54 68 65 20 69 6d 70  {X17084} The imp
3c290 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6e  lementation is n
3c2a0 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 70  ot required to p
3c2b0 72 6f 76 69 64 65 64 20 76 65 72 73 69 6f 6e 73  rovided versions
3c2c0 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 72 6f 75   of these.** rou
3c2d0 74 69 6e 65 73 20 74 68 61 74 20 61 63 74 75 61  tines that actua
3c2e0 6c 6c 79 20 77 6f 72 6b 2e 20 49 66 20 74 68 65  lly work. If the
3c2f0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3c300 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76 69 64 65  does not provide
3c310 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 76 65 72 73   working.** vers
3c320 69 6f 6e 73 20 6f 66 20 74 68 65 73 65 20 72 6f  ions of these ro
3c330 75 74 69 6e 65 73 2c 20 69 74 20 73 68 6f 75 6c  utines, it shoul
3c340 64 20 61 74 20 6c 65 61 73 74 20 70 72 6f 76 69  d at least provi
3c350 64 65 20 73 74 75 62 73 20 74 68 61 74 20 61 6c  de stubs that al
3c360 77 61 79 73 0a 2a 2a 20 72 65 74 75 72 6e 20 74  ways.** return t
3c370 72 75 65 20 73 6f 20 74 68 61 74 20 6f 6e 65 20  rue so that one 
3c380 64 6f 65 73 20 6e 6f 74 20 67 65 74 20 73 70 75  does not get spu
3c390 72 69 6f 75 73 20 61 73 73 65 72 74 69 6f 6e 20  rious assertion 
3c3a0 66 61 69 6c 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20  failures..**.** 
3c3b0 7b 48 31 37 30 38 35 7d 20 49 66 20 74 68 65 20  {H17085} If the 
3c3c0 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69  argument to sqli
3c3d0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29  te3_mutex_held()
3c3e0 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   is a NULL point
3c3f0 65 72 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 72  er then.** the r
3c400 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 72 65  outine should re
3c410 74 75 72 6e 20 31 2e 20 20 7b 45 4e 44 7d 20 54  turn 1.  {END} T
3c420 68 69 73 20 73 65 65 6d 73 20 63 6f 75 6e 74 65  his seems counte
3c430 72 2d 69 6e 74 75 69 74 69 76 65 20 73 69 6e 63  r-intuitive sinc
3c440 65 0a 2a 2a 20 63 6c 65 61 72 6c 79 20 74 68 65  e.** clearly the
3c450 20 6d 75 74 65 78 20 63 61 6e 6e 6f 74 20 62 65   mutex cannot be
3c460 20 68 65 6c 64 20 69 66 20 69 74 20 64 6f 65 73   held if it does
3c470 20 6e 6f 74 20 65 78 69 73 74 2e 20 20 42 75 74   not exist.  But
3c480 20 74 68 65 0a 2a 2a 20 74 68 65 20 72 65 61 73   the.** the reas
3c490 6f 6e 20 74 68 65 20 6d 75 74 65 78 20 64 6f 65  on the mutex doe
3c4a0 73 20 6e 6f 74 20 65 78 69 73 74 20 69 73 20 62  s not exist is b
3c4b0 65 63 61 75 73 65 20 74 68 65 20 62 75 69 6c 64  ecause the build
3c4c0 20 69 73 20 6e 6f 74 0a 2a 2a 20 75 73 69 6e 67   is not.** using
3c4d0 20 6d 75 74 65 78 65 73 2e 20 20 41 6e 64 20 77   mutexes.  And w
3c4e0 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68  e do not want th
3c4f0 65 20 61 73 73 65 72 74 28 29 20 63 6f 6e 74 61  e assert() conta
3c500 69 6e 69 6e 67 20 74 68 65 0a 2a 2a 20 63 61 6c  ining the.** cal
3c510 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74  l to sqlite3_mut
3c520 65 78 5f 68 65 6c 64 28 29 20 74 6f 20 66 61 69  ex_held() to fai
3c530 6c 2c 20 73 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f  l, so a non-zero
3c540 20 72 65 74 75 72 6e 20 69 73 0a 2a 2a 20 74 68   return is.** th
3c550 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 74 68  e appropriate th
3c560 69 6e 67 20 74 6f 20 64 6f 2e 20 20 7b 48 31 37  ing to do.  {H17
3c570 30 38 36 7d 20 54 68 65 20 73 71 6c 69 74 65 33  086} The sqlite3
3c580 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29  _mutex_notheld()
3c590 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 73 68  .** interface sh
3c5a0 6f 75 6c 64 20 61 6c 73 6f 20 72 65 74 75 72 6e  ould also return
3c5b0 20 31 20 77 68 65 6e 20 67 69 76 65 6e 20 61 20   1 when given a 
3c5c0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f  NULL pointer..*/
3c5d0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
3c5e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
3c5f0 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
3c600 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  *);.SQLITE_API i
3c610 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  nt sqlite3_mutex
3c620 5f 6e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33  _notheld(sqlite3
3c630 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  _mutex*);../*.**
3c640 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78   CAPI3REF: Mutex
3c650 20 54 79 70 65 73 20 7b 48 31 37 30 30 31 7d 20   Types {H17001} 
3c660 3c 48 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <H17000>.**.** T
3c670 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  he [sqlite3_mute
3c680 78 5f 61 6c 6c 6f 63 28 29 5d 20 69 6e 74 65 72  x_alloc()] inter
3c690 66 61 63 65 20 74 61 6b 65 73 20 61 20 73 69 6e  face takes a sin
3c6a0 67 6c 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  gle argument.** 
3c6b0 77 68 69 63 68 20 69 73 20 6f 6e 65 20 6f 66 20  which is one of 
3c6c0 74 68 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f  these integer co
3c6d0 6e 73 74 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  nstants..**.** T
3c6e0 68 65 20 73 65 74 20 6f 66 20 73 74 61 74 69 63  he set of static
3c6f0 20 6d 75 74 65 78 65 73 20 6d 61 79 20 63 68 61   mutexes may cha
3c700 6e 67 65 20 66 72 6f 6d 20 6f 6e 65 20 53 51 4c  nge from one SQL
3c710 69 74 65 20 72 65 6c 65 61 73 65 20 74 6f 20 74  ite release to t
3c720 68 65 0a 2a 2a 20 6e 65 78 74 2e 20 20 41 70 70  he.** next.  App
3c730 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6f  lications that o
3c740 76 65 72 72 69 64 65 20 74 68 65 20 62 75 69 6c  verride the buil
3c750 74 2d 69 6e 20 6d 75 74 65 78 20 6c 6f 67 69 63  t-in mutex logic
3c760 20 6d 75 73 74 20 62 65 0a 2a 2a 20 70 72 65 70   must be.** prep
3c770 61 72 65 64 20 74 6f 20 61 63 63 6f 6d 6d 6f 64  ared to accommod
3c780 61 74 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73  ate additional s
3c790 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 0a 2a  tatic mutexes..*
3c7a0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
3c7b0 5f 4d 55 54 45 58 5f 46 41 53 54 20 20 20 20 20  _MUTEX_FAST     
3c7c0 20 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e          0.#defin
3c7d0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  e SQLITE_MUTEX_R
3c7e0 45 43 55 52 53 49 56 45 20 20 20 20 20 20 20 20  ECURSIVE        
3c7f0 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  1.#define SQLITE
3c800 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
3c810 53 54 45 52 20 20 20 20 32 0a 23 64 65 66 69 6e  STER    2.#defin
3c820 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  e SQLITE_MUTEX_S
3c830 54 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20  TATIC_MEM       
3c840 33 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 6d 61  3  /* sqlite3_ma
3c850 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69 6e  lloc() */.#defin
3c860 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  e SQLITE_MUTEX_S
3c870 54 41 54 49 43 5f 4d 45 4d 32 20 20 20 20 20 20  TATIC_MEM2      
3c880 34 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44 20 2a  4  /* NOT USED *
3c890 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
3c8a0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4f 50  _MUTEX_STATIC_OP
3c8b0 45 4e 20 20 20 20 20 20 34 20 20 2f 2a 20 73 71  EN      4  /* sq
3c8c0 6c 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 29  lite3BtreeOpen()
3c8d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
3c8e0 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
3c8f0 50 52 4e 47 20 20 20 20 20 20 35 20 20 2f 2a 20  PRNG      5  /* 
3c900 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 28 29  sqlite3_random()
3c910 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
3c920 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
3c930 4c 52 55 20 20 20 20 20 20 20 36 20 20 2f 2a 20  LRU       6  /* 
3c940 6c 72 75 20 70 61 67 65 20 6c 69 73 74 20 2a 2f  lru page list */
3c950 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3c960 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55  MUTEX_STATIC_LRU
3c970 32 20 20 20 20 20 20 37 20 20 2f 2a 20 6c 72 75  2      7  /* lru
3c980 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 0a 2f   page list */../
3c990 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52  *.** CAPI3REF: R
3c9a0 65 74 72 69 65 76 65 20 74 68 65 20 6d 75 74 65  etrieve the mute
3c9b0 78 20 66 6f 72 20 61 20 64 61 74 61 62 61 73 65  x for a database
3c9c0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48 31 37   connection {H17
3c9d0 30 30 32 7d 20 3c 48 31 37 30 30 30 3e 0a 2a 2a  002} <H17000>.**
3c9e0 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61  .** This interfa
3c9f0 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  ce returns a poi
3ca00 6e 74 65 72 20 74 68 65 20 5b 73 71 6c 69 74 65  nter the [sqlite
3ca10 33 5f 6d 75 74 65 78 5d 20 6f 62 6a 65 63 74 20  3_mutex] object 
3ca20 74 68 61 74 20 0a 2a 2a 20 73 65 72 69 61 6c 69  that .** seriali
3ca30 7a 65 73 20 61 63 63 65 73 73 20 74 6f 20 74 68  zes access to th
3ca40 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
3ca50 65 63 74 69 6f 6e 5d 20 67 69 76 65 6e 20 69 6e  ection] given in
3ca60 20 74 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a   the argument.**
3ca70 20 77 68 65 6e 20 74 68 65 20 5b 74 68 72 65 61   when the [threa
3ca80 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73 20 53 65  ding mode] is Se
3ca90 72 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66 20  rialized..** If 
3caa0 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d  the [threading m
3cab0 6f 64 65 5d 20 69 73 20 53 69 6e 67 6c 65 2d 74  ode] is Single-t
3cac0 68 72 65 61 64 20 6f 72 20 4d 75 6c 74 69 2d 74  hread or Multi-t
3cad0 68 72 65 61 64 20 74 68 65 6e 20 74 68 69 73 0a  hread then this.
3cae0 2a 2a 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72  ** routine retur
3caf0 6e 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  ns a NULL pointe
3cb00 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r..*/.SQLITE_API
3cb10 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
3cb20 73 71 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78  sqlite3_db_mutex
3cb30 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a  (sqlite3*);../*.
3cb40 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 6f 77  ** CAPI3REF: Low
3cb50 2d 4c 65 76 65 6c 20 43 6f 6e 74 72 6f 6c 20 4f  -Level Control O
3cb60 66 20 44 61 74 61 62 61 73 65 20 46 69 6c 65 73  f Database Files
3cb70 20 7b 48 31 31 33 30 30 7d 20 3c 53 33 30 38 30   {H11300} <S3080
3cb80 30 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 31  0>.**.** {H11301
3cb90 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 66  } The [sqlite3_f
3cba0 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69  ile_control()] i
3cbb0 6e 74 65 72 66 61 63 65 20 6d 61 6b 65 73 20 61  nterface makes a
3cbc0 20 64 69 72 65 63 74 20 63 61 6c 6c 20 74 6f 20   direct call to 
3cbd0 74 68 65 0a 2a 2a 20 78 46 69 6c 65 43 6f 6e 74  the.** xFileCont
3cbe0 72 6f 6c 20 6d 65 74 68 6f 64 20 66 6f 72 20 74  rol method for t
3cbf0 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  he [sqlite3_io_m
3cc00 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 61  ethods] object a
3cc10 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74  ssociated.** wit
3cc20 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64  h a particular d
3cc30 61 74 61 62 61 73 65 20 69 64 65 6e 74 69 66 69  atabase identifi
3cc40 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64  ed by the second
3cc50 20 61 72 67 75 6d 65 6e 74 2e 20 7b 48 31 31 33   argument. {H113
3cc60 30 32 7d 20 54 68 65 0a 2a 2a 20 6e 61 6d 65 20  02} The.** name 
3cc70 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
3cc80 69 73 20 74 68 65 20 6e 61 6d 65 20 61 73 73 69  is the name assi
3cc90 67 6e 65 64 20 74 6f 20 74 68 65 20 64 61 74 61  gned to the data
3cca0 62 61 73 65 20 62 79 20 74 68 65 0a 2a 2a 20 3c  base by the.** <
3ccb0 61 20 68 72 65 66 3d 22 6c 61 6e 67 5f 61 74 74  a href="lang_att
3ccc0 61 63 68 2e 68 74 6d 6c 22 3e 41 54 54 41 43 48  ach.html">ATTACH
3ccd0 3c 2f 61 3e 20 53 51 4c 20 63 6f 6d 6d 61 6e 64  </a> SQL command
3cce0 20 74 68 61 74 20 6f 70 65 6e 65 64 20 74 68 65   that opened the
3ccf0 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 7b 48  .** database. {H
3cd00 31 31 33 30 33 7d 20 54 6f 20 63 6f 6e 74 72 6f  11303} To contro
3cd10 6c 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  l the main datab
3cd20 61 73 65 20 66 69 6c 65 2c 20 75 73 65 20 74 68  ase file, use th
3cd30 65 20 6e 61 6d 65 20 22 6d 61 69 6e 22 0a 2a 2a  e name "main".**
3cd40 20 6f 72 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   or a NULL point
3cd50 65 72 2e 20 7b 48 31 31 33 30 34 7d 20 54 68 65  er. {H11304} The
3cd60 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74   third and fourt
3cd70 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20  h parameters to 
3cd80 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
3cd90 61 72 65 20 70 61 73 73 65 64 20 64 69 72 65 63  are passed direc
3cda0 74 6c 79 20 74 68 72 6f 75 67 68 20 74 6f 20 74  tly through to t
3cdb0 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74 68  he second and th
3cdc0 69 72 64 20 70 61 72 61 6d 65 74 65 72 73 20 6f  ird parameters o
3cdd0 66 0a 2a 2a 20 74 68 65 20 78 46 69 6c 65 43 6f  f.** the xFileCo
3cde0 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 20 7b  ntrol method.  {
3cdf0 48 31 31 33 30 35 7d 20 54 68 65 20 72 65 74 75  H11305} The retu
3ce00 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  rn value of the 
3ce10 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 0a 2a 2a 20  xFileControl.** 
3ce20 6d 65 74 68 6f 64 20 62 65 63 6f 6d 65 73 20 74  method becomes t
3ce30 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
3ce40 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  of this routine.
3ce50 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 36 7d 20  .**.** {H11306} 
3ce60 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61  If the second pa
3ce70 72 61 6d 65 74 65 72 20 28 7a 44 62 4e 61 6d 65  rameter (zDbName
3ce80 29 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68  ) does not match
3ce90 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 79   the name of any
3cea0 0a 2a 2a 20 6f 70 65 6e 20 64 61 74 61 62 61 73  .** open databas
3ceb0 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 53 51 4c  e file, then SQL
3cec0 49 54 45 5f 45 52 52 4f 52 20 69 73 20 72 65 74  ITE_ERROR is ret
3ced0 75 72 6e 65 64 2e 20 7b 48 31 31 33 30 37 7d 20  urned. {H11307} 
3cee0 54 68 69 73 20 65 72 72 6f 72 0a 2a 2a 20 63 6f  This error.** co
3cef0 64 65 20 69 73 20 6e 6f 74 20 72 65 6d 65 6d 62  de is not rememb
3cf00 65 72 65 64 20 61 6e 64 20 77 69 6c 6c 20 6e 6f  ered and will no
3cf10 74 20 62 65 20 72 65 63 61 6c 6c 65 64 20 62 79  t be recalled by
3cf20 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64   [sqlite3_errcod
3cf30 65 28 29 5d 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69  e()].** or [sqli
3cf40 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 20 7b  te3_errmsg()]. {
3cf50 41 31 31 33 30 38 7d 20 54 68 65 20 75 6e 64 65  A11308} The unde
3cf60 72 6c 79 69 6e 67 20 78 46 69 6c 65 43 6f 6e 74  rlying xFileCont
3cf70 72 6f 6c 20 6d 65 74 68 6f 64 20 6d 69 67 68 74  rol method might
3cf80 0a 2a 2a 20 61 6c 73 6f 20 72 65 74 75 72 6e 20  .** also return 
3cf90 53 51 4c 49 54 45 5f 45 52 52 4f 52 2e 20 20 7b  SQLITE_ERROR.  {
3cfa0 41 31 31 33 30 39 7d 20 54 68 65 72 65 20 69 73  A11309} There is
3cfb0 20 6e 6f 20 77 61 79 20 74 6f 20 64 69 73 74 69   no way to disti
3cfc0 6e 67 75 69 73 68 20 62 65 74 77 65 65 6e 0a 2a  nguish between.*
3cfd0 2a 20 61 6e 20 69 6e 63 6f 72 72 65 63 74 20 7a  * an incorrect z
3cfe0 44 62 4e 61 6d 65 20 61 6e 64 20 61 6e 20 53 51  DbName and an SQ
3cff0 4c 49 54 45 5f 45 52 52 4f 52 20 72 65 74 75 72  LITE_ERROR retur
3d000 6e 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 72  n from the under
3d010 6c 79 69 6e 67 0a 2a 2a 20 78 46 69 6c 65 43 6f  lying.** xFileCo
3d020 6e 74 72 6f 6c 20 6d 65 74 68 6f 64 2e 20 7b 45  ntrol method. {E
3d030 4e 44 7d 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  ND}.**.** See al
3d040 73 6f 3a 20 5b 53 51 4c 49 54 45 5f 46 43 4e 54  so: [SQLITE_FCNT
3d050 4c 5f 4c 4f 43 4b 53 54 41 54 45 5d 0a 2a 2f 0a  L_LOCKSTATE].*/.
3d060 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3d070 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74  qlite3_file_cont
3d080 72 6f 6c 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f  rol(sqlite3*, co
3d090 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d  nst char *zDbNam
3d0a0 65 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 2a  e, int op, void*
3d0b0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
3d0c0 45 46 3a 20 54 65 73 74 69 6e 67 20 49 6e 74 65  EF: Testing Inte
3d0d0 72 66 61 63 65 20 7b 48 31 31 34 30 30 7d 20 3c  rface {H11400} <
3d0e0 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S30800>.**.** Th
3d0f0 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63  e sqlite3_test_c
3d100 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 61  ontrol() interfa
3d110 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72 65  ce is used to re
3d120 61 64 20 6f 75 74 20 69 6e 74 65 72 6e 61 6c 0a  ad out internal.
3d130 2a 2a 20 73 74 61 74 65 20 6f 66 20 53 51 4c 69  ** state of SQLi
3d140 74 65 20 61 6e 64 20 74 6f 20 69 6e 6a 65 63 74  te and to inject
3d150 20 66 61 75 6c 74 73 20 69 6e 74 6f 20 53 51 4c   faults into SQL
3d160 69 74 65 20 66 6f 72 20 74 65 73 74 69 6e 67 0a  ite for testing.
3d170 2a 2a 20 70 75 72 70 6f 73 65 73 2e 20 20 54 68  ** purposes.  Th
3d180 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65  e first paramete
3d190 72 20 69 73 20 61 6e 20 6f 70 65 72 61 74 69 6f  r is an operatio
3d1a0 6e 20 63 6f 64 65 20 74 68 61 74 20 64 65 74 65  n code that dete
3d1b0 72 6d 69 6e 65 73 0a 2a 2a 20 74 68 65 20 6e 75  rmines.** the nu
3d1c0 6d 62 65 72 2c 20 6d 65 61 6e 69 6e 67 2c 20 61  mber, meaning, a
3d1d0 6e 64 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20  nd operation of 
3d1e0 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20 70  all subsequent p
3d1f0 61 72 61 6d 65 74 65 72 73 2e 0a 2a 2a 0a 2a 2a  arameters..**.**
3d200 20 54 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   This interface 
3d210 69 73 20 6e 6f 74 20 66 6f 72 20 75 73 65 20 62  is not for use b
3d220 79 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 20  y applications. 
3d230 20 49 74 20 65 78 69 73 74 73 20 73 6f 6c 65 6c   It exists solel
3d240 79 0a 2a 2a 20 66 6f 72 20 76 65 72 69 66 79 69  y.** for verifyi
3d250 6e 67 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f  ng the correct o
3d260 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  peration of the 
3d270 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20  SQLite library. 
3d280 20 44 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f 6e   Depending.** on
3d290 20 68 6f 77 20 74 68 65 20 53 51 4c 69 74 65 20   how the SQLite 
3d2a0 6c 69 62 72 61 72 79 20 69 73 20 63 6f 6d 70 69  library is compi
3d2b0 6c 65 64 2c 20 74 68 69 73 20 69 6e 74 65 72 66  led, this interf
3d2c0 61 63 65 20 6d 69 67 68 74 20 6e 6f 74 20 65 78  ace might not ex
3d2d0 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  ist..**.** The d
3d2e0 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20 6f 70  etails of the op
3d2f0 65 72 61 74 69 6f 6e 20 63 6f 64 65 73 2c 20 74  eration codes, t
3d300 68 65 69 72 20 6d 65 61 6e 69 6e 67 73 2c 20 74  heir meanings, t
3d310 68 65 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a  he parameters.**
3d320 20 74 68 65 79 20 74 61 6b 65 2c 20 61 6e 64 20   they take, and 
3d330 77 68 61 74 20 74 68 65 79 20 64 6f 20 61 72 65  what they do are
3d340 20 61 6c 6c 20 73 75 62 6a 65 63 74 20 74 6f 20   all subject to 
3d350 63 68 61 6e 67 65 20 77 69 74 68 6f 75 74 20 6e  change without n
3d360 6f 74 69 63 65 2e 0a 2a 2a 20 55 6e 6c 69 6b 65  otice..** Unlike
3d370 20 6d 6f 73 74 20 6f 66 20 74 68 65 20 53 51 4c   most of the SQL
3d380 69 74 65 20 41 50 49 2c 20 74 68 69 73 20 66 75  ite API, this fu
3d390 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 67 75  nction is not gu
3d3a0 61 72 61 6e 74 65 65 64 20 74 6f 0a 2a 2a 20 6f  aranteed to.** o
3d3b0 70 65 72 61 74 65 20 63 6f 6e 73 69 73 74 65 6e  perate consisten
3d3c0 74 6c 79 20 66 72 6f 6d 20 6f 6e 65 20 72 65 6c  tly from one rel
3d3d0 65 61 73 65 20 74 6f 20 74 68 65 20 6e 65 78 74  ease to the next
3d3e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3d3f0 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 65 73 74  int sqlite3_test
3d400 5f 63 6f 6e 74 72 6f 6c 28 69 6e 74 20 6f 70 2c  _control(int op,
3d410 20 2e 2e 2e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   ...);../*.** CA
3d420 50 49 33 52 45 46 3a 20 54 65 73 74 69 6e 67 20  PI3REF: Testing 
3d430 49 6e 74 65 72 66 61 63 65 20 4f 70 65 72 61 74  Interface Operat
3d440 69 6f 6e 20 43 6f 64 65 73 20 7b 48 31 31 34 31  ion Codes {H1141
3d450 30 7d 20 3c 48 31 31 34 30 30 3e 0a 2a 2a 0a 2a  0} <H11400>.**.*
3d460 2a 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74  * These constant
3d470 73 20 61 72 65 20 74 68 65 20 76 61 6c 69 64 20  s are the valid 
3d480 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 20 70  operation code p
3d490 61 72 61 6d 65 74 65 72 73 20 75 73 65 64 0a 2a  arameters used.*
3d4a0 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61  * as the first a
3d4b0 72 67 75 6d 65 6e 74 20 74 6f 20 5b 73 71 6c 69  rgument to [sqli
3d4c0 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  te3_test_control
3d4d0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  ()]..**.** These
3d4e0 20 70 61 72 61 6d 65 74 65 72 73 20 61 6e 64 20   parameters and 
3d4f0 74 68 65 69 72 20 6d 65 61 6e 69 6e 67 73 20 61  their meanings a
3d500 72 65 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68  re subject to ch
3d510 61 6e 67 65 0a 2a 2a 20 77 69 74 68 6f 75 74 20  ange.** without 
3d520 6e 6f 74 69 63 65 2e 20 20 54 68 65 73 65 20 76  notice.  These v
3d530 61 6c 75 65 73 20 61 72 65 20 66 6f 72 20 74 65  alues are for te
3d540 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73 20 6f  sting purposes o
3d550 6e 6c 79 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 74  nly..** Applicat
3d560 69 6f 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20  ions should not 
3d570 75 73 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65  use any of these
3d580 20 70 61 72 61 6d 65 74 65 72 73 20 6f 72 20 74   parameters or t
3d590 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 74  he.** [sqlite3_t
3d5a0 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d 20 69  est_control()] i
3d5b0 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 64 65  nterface..*/.#de
3d5c0 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54  fine SQLITE_TEST
3d5d0 43 54 52 4c 5f 50 52 4e 47 5f 53 41 56 45 20 20  CTRL_PRNG_SAVE  
3d5e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a                5.
3d5f0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54  #define SQLITE_T
3d600 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45 53  ESTCTRL_PRNG_RES
3d610 54 4f 52 45 20 20 20 20 20 20 20 20 20 20 20 20  TORE            
3d620 20 36 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   6.#define SQLIT
3d630 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f  E_TESTCTRL_PRNG_
3d640 52 45 53 45 54 20 20 20 20 20 20 20 20 20 20 20  RESET           
3d650 20 20 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51      7.#define SQ
3d660 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 49  LITE_TESTCTRL_BI
3d670 54 56 45 43 5f 54 45 53 54 20 20 20 20 20 20 20  TVEC_TEST       
3d680 20 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65         8.#define
3d690 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c   SQLITE_TESTCTRL
3d6a0 5f 46 41 55 4c 54 5f 49 4e 53 54 41 4c 4c 20 20  _FAULT_INSTALL  
3d6b0 20 20 20 20 20 20 20 20 20 20 39 0a 23 64 65 66            9.#def
3d6c0 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ine SQLITE_TESTC
3d6d0 54 52 4c 5f 42 45 4e 49 47 4e 5f 4d 41 4c 4c 4f  TRL_BENIGN_MALLO
3d6e0 43 5f 48 4f 4f 4b 53 20 20 20 20 20 31 30 0a 23  C_HOOKS     10.#
3d6f0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
3d700 53 54 43 54 52 4c 5f 50 45 4e 44 49 4e 47 5f 42  STCTRL_PENDING_B
3d710 59 54 45 20 20 20 20 20 20 20 20 20 20 20 20 31  YTE            1
3d720 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  1.#define SQLITE
3d730 5f 54 45 53 54 43 54 52 4c 5f 41 53 53 45 52 54  _TESTCTRL_ASSERT
3d740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3d750 20 20 31 32 0a 23 64 65 66 69 6e 65 20 53 51 4c    12.#define SQL
3d760 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 4c 57  ITE_TESTCTRL_ALW
3d770 41 59 53 20 20 20 20 20 20 20 20 20 20 20 20 20  AYS             
3d780 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65 20       13.#define 
3d790 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f  SQLITE_TESTCTRL_
3d7a0 52 45 53 45 52 56 45 20 20 20 20 20 20 20 20 20  RESERVE         
3d7b0 20 20 20 20 20 20 20 20 31 34 0a 0a 2f 2a 0a 2a          14../*.*
3d7c0 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c 69  * CAPI3REF: SQLi
3d7d0 74 65 20 52 75 6e 74 69 6d 65 20 53 74 61 74 75  te Runtime Statu
3d7e0 73 20 7b 48 31 37 32 30 30 7d 20 3c 53 36 30 32  s {H17200} <S602
3d7f0 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  00>.** EXPERIMEN
3d800 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  TAL.**.** This i
3d810 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64  nterface is used
3d820 20 74 6f 20 72 65 74 72 69 65 76 65 20 72 75 6e   to retrieve run
3d830 74 69 6d 65 20 73 74 61 74 75 73 20 69 6e 66 6f  time status info
3d840 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74  rmation.** about
3d850 20 74 68 65 20 70 72 65 66 6f 72 6d 61 6e 63 65   the preformance
3d860 20 6f 66 20 53 51 4c 69 74 65 2c 20 61 6e 64 20   of SQLite, and 
3d870 6f 70 74 69 6f 6e 61 6c 6c 79 20 74 6f 20 72 65  optionally to re
3d880 73 65 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 68  set various.** h
3d890 69 67 68 77 61 74 65 72 20 6d 61 72 6b 73 2e 20  ighwater marks. 
3d8a0 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
3d8b0 65 6e 74 20 69 73 20 61 6e 20 69 6e 74 65 67 65  ent is an intege
3d8c0 72 20 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 74 68  r code for.** th
3d8d0 65 20 73 70 65 63 69 66 69 63 20 70 61 72 61 6d  e specific param
3d8e0 65 74 65 72 20 74 6f 20 6d 65 61 73 75 72 65 2e  eter to measure.
3d8f0 20 20 52 65 63 6f 67 6e 69 7a 65 64 20 69 6e 74    Recognized int
3d900 65 67 65 72 20 63 6f 64 65 73 0a 2a 2a 20 61 72  eger codes.** ar
3d910 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 5b 53  e of the form [S
3d920 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d  QLITE_STATUS_MEM
3d930 4f 52 59 5f 55 53 45 44 20 7c 20 53 51 4c 49 54  ORY_USED | SQLIT
3d940 45 5f 53 54 41 54 55 53 5f 2e 2e 2e 5d 2e 0a 2a  E_STATUS_...]..*
3d950 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 76 61  * The current va
3d960 6c 75 65 20 6f 66 20 74 68 65 20 70 61 72 61 6d  lue of the param
3d970 65 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64  eter is returned
3d980 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e   into *pCurrent.
3d990 0a 2a 2a 20 54 68 65 20 68 69 67 68 65 73 74 20  .** The highest 
3d9a0 72 65 63 6f 72 64 65 64 20 76 61 6c 75 65 20 69  recorded value i
3d9b0 73 20 72 65 74 75 72 6e 65 64 20 69 6e 20 2a 70  s returned in *p
3d9c0 48 69 67 68 77 61 74 65 72 2e 20 20 49 66 20 74  Highwater.  If t
3d9d0 68 65 0a 2a 2a 20 72 65 73 65 74 46 6c 61 67 20  he.** resetFlag 
3d9e0 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68  is true, then th
3d9f0 65 20 68 69 67 68 65 73 74 20 72 65 63 6f 72 64  e highest record
3da00 20 76 61 6c 75 65 20 69 73 20 72 65 73 65 74 20   value is reset 
3da10 61 66 74 65 72 0a 2a 2a 20 2a 70 48 69 67 68 77  after.** *pHighw
3da20 61 74 65 72 20 69 73 20 77 72 69 74 74 65 6e 2e  ater is written.
3da30 20 53 6f 6d 65 20 70 61 72 61 6d 65 74 65 72 73   Some parameters
3da40 20 64 6f 20 6e 6f 74 20 72 65 63 6f 72 64 20 74   do not record t
3da50 68 65 20 68 69 67 68 65 73 74 0a 2a 2a 20 76 61  he highest.** va
3da60 6c 75 65 2e 20 20 46 6f 72 20 74 68 6f 73 65 20  lue.  For those 
3da70 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6e 6f  parameters.** no
3da80 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e  thing is written
3da90 20 69 6e 74 6f 20 2a 70 48 69 67 68 77 61 74 65   into *pHighwate
3daa0 72 20 61 6e 64 20 74 68 65 20 72 65 73 65 74 46  r and the resetF
3dab0 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a  lag is ignored..
3dac0 2a 2a 20 4f 74 68 65 72 20 70 61 72 61 6d 65 74  ** Other paramet
3dad0 65 72 73 20 72 65 63 6f 72 64 20 6f 6e 6c 79 20  ers record only 
3dae0 74 68 65 20 68 69 67 68 77 61 74 65 72 20 6d 61  the highwater ma
3daf0 72 6b 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 63  rk and not the c
3db00 75 72 72 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e  urrent.** value.
3db10 20 20 46 6f 72 20 74 68 65 73 65 20 6c 61 74 74    For these latt
3db20 65 72 20 70 61 72 61 6d 65 74 65 72 73 20 6e 6f  er parameters no
3db30 74 68 69 6e 67 20 69 73 20 77 72 69 74 74 65 6e  thing is written
3db40 20 69 6e 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e   into *pCurrent.
3db50 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
3db60 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49  ine returns SQLI
3db70 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73  TE_OK on success
3db80 20 61 6e 64 20 61 20 6e 6f 6e 2d 7a 65 72 6f 0a   and a non-zero.
3db90 2a 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20  ** [error code] 
3dba0 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a  on failure..**.*
3dbb0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
3dbc0 73 20 74 68 72 65 61 64 73 61 66 65 20 62 75 74  s threadsafe but
3dbd0 20 69 73 20 6e 6f 74 20 61 74 6f 6d 69 63 2e 20   is not atomic. 
3dbe0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61   This routine ca
3dbf0 6e 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 77  n be.** called w
3dc00 68 69 6c 65 20 6f 74 68 65 72 20 74 68 72 65 61  hile other threa
3dc10 64 73 20 61 72 65 20 72 75 6e 6e 69 6e 67 20 74  ds are running t
3dc20 68 65 20 73 61 6d 65 20 6f 72 20 64 69 66 66 65  he same or diffe
3dc30 72 65 6e 74 20 53 51 4c 69 74 65 0a 2a 2a 20 69  rent SQLite.** i
3dc40 6e 74 65 72 66 61 63 65 73 2e 20 20 48 6f 77 65  nterfaces.  Howe
3dc50 76 65 72 20 74 68 65 20 76 61 6c 75 65 73 20 72  ver the values r
3dc60 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 43 75 72  eturned in *pCur
3dc70 72 65 6e 74 20 61 6e 64 0a 2a 2a 20 2a 70 48 69  rent and.** *pHi
3dc80 67 68 77 61 74 65 72 20 72 65 66 6c 65 63 74 20  ghwater reflect 
3dc90 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 53 51  the status of SQ
3dca0 4c 69 74 65 20 61 74 20 64 69 66 66 65 72 65 6e  Lite at differen
3dcb0 74 20 70 6f 69 6e 74 73 20 69 6e 20 74 69 6d 65  t points in time
3dcc0 0a 2a 2a 20 61 6e 64 20 69 74 20 69 73 20 70 6f  .** and it is po
3dcd0 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f 74  ssible that anot
3dce0 68 65 72 20 74 68 72 65 61 64 20 6d 69 67 68 74  her thread might
3dcf0 20 63 68 61 6e 67 65 20 74 68 65 20 70 61 72 61   change the para
3dd00 6d 65 74 65 72 0a 2a 2a 20 69 6e 20 62 65 74 77  meter.** in betw
3dd10 65 65 6e 20 74 68 65 20 74 69 6d 65 73 20 77 68  een the times wh
3dd20 65 6e 20 2a 70 43 75 72 72 65 6e 74 20 61 6e 64  en *pCurrent and
3dd30 20 2a 70 48 69 67 68 77 61 74 65 72 20 61 72 65   *pHighwater are
3dd40 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20   written..**.** 
3dd50 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74  See also: [sqlit
3dd60 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 0a  e3_db_status()].
3dd70 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51  */.SQLITE_API SQ
3dd80 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41  LITE_EXPERIMENTA
3dd90 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 74  L int sqlite3_st
3dda0 61 74 75 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74  atus(int op, int
3ddb0 20 2a 70 43 75 72 72 65 6e 74 2c 20 69 6e 74 20   *pCurrent, int 
3ddc0 2a 70 48 69 67 68 77 61 74 65 72 2c 20 69 6e 74  *pHighwater, int
3ddd0 20 72 65 73 65 74 46 6c 61 67 29 3b 0a 0a 0a 2f   resetFlag);.../
3dde0 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53  *.** CAPI3REF: S
3ddf0 74 61 74 75 73 20 50 61 72 61 6d 65 74 65 72 73  tatus Parameters
3de00 20 7b 48 31 37 32 35 30 7d 20 3c 48 31 37 32 30   {H17250} <H1720
3de10 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
3de20 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 69  AL.**.** These i
3de30 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73  nteger constants
3de40 20 64 65 73 69 67 6e 61 74 65 20 76 61 72 69 6f   designate vario
3de50 75 73 20 72 75 6e 2d 74 69 6d 65 20 73 74 61 74  us run-time stat
3de60 75 73 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a  us parameters.**
3de70 20 74 68 61 74 20 63 61 6e 20 62 65 20 72 65 74   that can be ret
3de80 75 72 6e 65 64 20 62 79 20 5b 73 71 6c 69 74 65  urned by [sqlite
3de90 33 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a  3_status()]..**.
3dea0 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53  ** <dl>.** <dt>S
3deb0 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d  QLITE_STATUS_MEM
3dec0 4f 52 59 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a  ORY_USED</dt>.**
3ded0 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65   <dd>This parame
3dee0 74 65 72 20 69 73 20 74 68 65 20 63 75 72 72 65  ter is the curre
3def0 6e 74 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d  nt amount of mem
3df00 6f 72 79 20 63 68 65 63 6b 65 64 20 6f 75 74 0a  ory checked out.
3df10 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65  ** using [sqlite
3df20 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 65 69 74  3_malloc()], eit
3df30 68 65 72 20 64 69 72 65 63 74 6c 79 20 6f 72 20  her directly or 
3df40 69 6e 64 69 72 65 63 74 6c 79 2e 20 20 54 68 65  indirectly.  The
3df50 0a 2a 2a 20 66 69 67 75 72 65 20 69 6e 63 6c 75  .** figure inclu
3df60 64 65 73 20 63 61 6c 6c 73 20 6d 61 64 65 20 74  des calls made t
3df70 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  o [sqlite3_mallo
3df80 63 28 29 5d 20 62 79 20 74 68 65 20 61 70 70 6c  c()] by the appl
3df90 69 63 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 69  ication.** and i
3dfa0 6e 74 65 72 6e 61 6c 20 6d 65 6d 6f 72 79 20 75  nternal memory u
3dfb0 73 61 67 65 20 62 79 20 74 68 65 20 53 51 4c 69  sage by the SQLi
3dfc0 74 65 20 6c 69 62 72 61 72 79 2e 20 20 53 63 72  te library.  Scr
3dfd0 61 74 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 63  atch memory.** c
3dfe0 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20 5b 53 51  ontrolled by [SQ
3dff0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41  LITE_CONFIG_SCRA
3e000 54 43 48 5d 20 61 6e 64 20 61 75 78 69 6c 69 61  TCH] and auxilia
3e010 72 79 20 70 61 67 65 2d 63 61 63 68 65 0a 2a 2a  ry page-cache.**
3e020 20 6d 65 6d 6f 72 79 20 63 6f 6e 74 72 6f 6c 6c   memory controll
3e030 65 64 20 62 79 20 5b 53 51 4c 49 54 45 5f 43 4f  ed by [SQLITE_CO
3e040 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 5d 20  NFIG_PAGECACHE] 
3e050 69 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 64 20  is not included 
3e060 69 6e 0a 2a 2a 20 74 68 69 73 20 70 61 72 61 6d  in.** this param
3e070 65 74 65 72 2e 20 20 54 68 65 20 61 6d 6f 75 6e  eter.  The amoun
3e080 74 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68  t returned is th
3e090 65 20 73 75 6d 20 6f 66 20 74 68 65 20 61 6c 6c  e sum of the all
3e0a0 6f 63 61 74 69 6f 6e 0a 2a 2a 20 73 69 7a 65 73  ocation.** sizes
3e0b0 20 61 73 20 72 65 70 6f 72 74 65 64 20 62 79 20   as reported by 
3e0c0 74 68 65 20 78 53 69 7a 65 20 6d 65 74 68 6f 64  the xSize method
3e0d0 20 69 6e 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d   in [sqlite3_mem
3e0e0 5f 6d 65 74 68 6f 64 73 5d 2e 3c 2f 64 64 3e 0a  _methods].</dd>.
3e0f0 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
3e100 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53  _STATUS_MALLOC_S
3e110 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  IZE</dt>.** <dd>
3e120 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72  This parameter r
3e130 65 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65  ecords the large
3e140 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  st memory alloca
3e150 74 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20  tion request.** 
3e160 68 61 6e 64 65 64 20 74 6f 20 5b 73 71 6c 69 74  handed to [sqlit
3e170 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20  e3_malloc()] or 
3e180 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  [sqlite3_realloc
3e190 28 29 5d 20 28 6f 72 20 74 68 65 69 72 0a 2a 2a  ()] (or their.**
3e1a0 20 69 6e 74 65 72 6e 61 6c 20 65 71 75 69 76 61   internal equiva
3e1b0 6c 65 6e 74 73 29 2e 20 20 4f 6e 6c 79 20 74 68  lents).  Only th
3e1c0 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
3e1d0 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67   in the.** *pHig
3e1e0 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 72  hwater parameter
3e1f0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 61   to [sqlite3_sta
3e200 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e 74  tus()] is of int
3e210 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20  erest.  .** The 
3e220 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e  value written in
3e230 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74  to the *pCurrent
3e240 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e   parameter is un
3e250 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a  defined.</dd>.**
3e260 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53  .** <dt>SQLITE_S
3e270 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f  TATUS_PAGECACHE_
3e280 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  USED</dt>.** <dd
3e290 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20  >This parameter 
3e2a0 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
3e2b0 65 72 20 6f 66 20 70 61 67 65 73 20 75 73 65 64  er of pages used
3e2c0 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 5b   out of the.** [
3e2d0 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 79  pagecache memory
3e2e0 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 74 68 61 74   allocator] that
3e2f0 20 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64 20   was configured 
3e300 75 73 69 6e 67 20 0a 2a 2a 20 5b 53 51 4c 49 54  using .** [SQLIT
3e310 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43  E_CONFIG_PAGECAC
3e320 48 45 5d 2e 20 20 54 68 65 0a 2a 2a 20 76 61 6c  HE].  The.** val
3e330 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 69  ue returned is i
3e340 6e 20 70 61 67 65 73 2c 20 6e 6f 74 20 69 6e 20  n pages, not in 
3e350 62 79 74 65 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  bytes.</dd>.**.*
3e360 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41  * <dt>SQLITE_STA
3e370 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f 56  TUS_PAGECACHE_OV
3e380 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c  ERFLOW</dt>.** <
3e390 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65  dd>This paramete
3e3a0 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  r returns the nu
3e3b0 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
3e3c0 20 70 61 67 65 20 63 61 63 68 65 0a 2a 2a 20 61   page cache.** a
3e3d0 6c 6c 6f 63 61 74 69 6f 6e 20 77 68 69 63 68 20  llocation which 
3e3e0 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73 74 61  could not be sta
3e3f0 74 69 73 66 69 65 64 20 62 79 20 74 68 65 20 5b  tisfied by the [
3e400 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41  SQLITE_CONFIG_PA
3e410 47 45 43 41 43 48 45 5d 0a 2a 2a 20 62 75 66 66  GECACHE].** buff
3e420 65 72 20 61 6e 64 20 77 68 65 72 65 20 66 6f 72  er and where for
3e430 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f 77 20  ced to overflow 
3e440 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  to [sqlite3_mall
3e450 6f 63 28 29 5d 2e 20 20 54 68 65 0a 2a 2a 20 72  oc()].  The.** r
3e460 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 6e  eturned value in
3e470 63 6c 75 64 65 73 20 61 6c 6c 6f 63 61 74 69 6f  cludes allocatio
3e480 6e 73 20 74 68 61 74 20 6f 76 65 72 66 6c 6f 77  ns that overflow
3e490 65 64 20 62 65 63 61 75 73 65 20 74 68 65 79 0a  ed because they.
3e4a0 2a 2a 20 77 68 65 72 65 20 74 6f 6f 20 6c 61 72  ** where too lar
3e4b0 67 65 20 28 74 68 65 79 20 77 65 72 65 20 6c 61  ge (they were la
3e4c0 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 22 73  rger than the "s
3e4d0 7a 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a  z" parameter to.
3e4e0 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ** [SQLITE_CONFI
3e4f0 47 5f 50 41 47 45 43 41 43 48 45 5d 29 20 61 6e  G_PAGECACHE]) an
3e500 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68  d allocations th
3e510 61 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 65  at overflowed be
3e520 63 61 75 73 65 0a 2a 2a 20 6e 6f 20 73 70 61 63  cause.** no spac
3e530 65 20 77 61 73 20 6c 65 66 74 20 69 6e 20 74 68  e was left in th
3e540 65 20 70 61 67 65 20 63 61 63 68 65 2e 3c 2f 64  e page cache.</d
3e550 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
3e560 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43  ITE_STATUS_PAGEC
3e570 41 43 48 45 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a  ACHE_SIZE</dt>.*
3e580 2a 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d  * <dd>This param
3e590 65 74 65 72 20 72 65 63 6f 72 64 73 20 74 68 65  eter records the
3e5a0 20 6c 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20   largest memory 
3e5b0 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65  allocation reque
3e5c0 73 74 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20  st.** handed to 
3e5d0 5b 70 61 67 65 63 61 63 68 65 20 6d 65 6d 6f 72  [pagecache memor
3e5e0 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f  y allocator].  O
3e5f0 6e 6c 79 20 74 68 65 20 76 61 6c 75 65 20 72 65  nly the value re
3e600 74 75 72 6e 65 64 20 69 6e 20 74 68 65 0a 2a 2a  turned in the.**
3e610 20 2a 70 48 69 67 68 77 61 74 65 72 20 70 61 72   *pHighwater par
3e620 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74  ameter to [sqlit
3e630 65 33 5f 73 74 61 74 75 73 28 29 5d 20 69 73 20  e3_status()] is 
3e640 6f 66 20 69 6e 74 65 72 65 73 74 2e 20 20 0a 2a  of interest.  .*
3e650 2a 20 54 68 65 20 76 61 6c 75 65 20 77 72 69 74  * The value writ
3e660 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 2a 70 43  ten into the *pC
3e670 75 72 72 65 6e 74 20 70 61 72 61 6d 65 74 65 72  urrent parameter
3e680 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 3c 2f   is undefined.</
3e690 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
3e6a0 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41  LITE_STATUS_SCRA
3e6b0 54 43 48 5f 55 53 45 44 3c 2f 64 74 3e 0a 2a 2a  TCH_USED</dt>.**
3e6c0 20 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65   <dd>This parame
3e6d0 74 65 72 20 72 65 74 75 72 6e 73 20 74 68 65 20  ter returns the 
3e6e0 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61  number of alloca
3e6f0 74 69 6f 6e 73 20 75 73 65 64 20 6f 75 74 20 6f  tions used out o
3e700 66 20 74 68 65 0a 2a 2a 20 5b 73 63 72 61 74 63  f the.** [scratc
3e710 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  h memory allocat
3e720 6f 72 5d 20 63 6f 6e 66 69 67 75 72 65 64 20 75  or] configured u
3e730 73 69 6e 67 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  sing.** [SQLITE_
3e740 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 2e  CONFIG_SCRATCH].
3e750 20 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75    The value retu
3e760 72 6e 65 64 20 69 73 20 69 6e 20 61 6c 6c 6f 63  rned is in alloc
3e770 61 74 69 6f 6e 73 2c 20 6e 6f 74 0a 2a 2a 20 69  ations, not.** i
3e780 6e 20 62 79 74 65 73 2e 20 20 53 69 6e 63 65 20  n bytes.  Since 
3e790 61 20 73 69 6e 67 6c 65 20 74 68 72 65 61 64 20  a single thread 
3e7a0 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20 6f 6e  may only have on
3e7b0 65 20 73 63 72 61 74 63 68 20 61 6c 6c 6f 63 61  e scratch alloca
3e7c0 74 69 6f 6e 0a 2a 2a 20 6f 75 74 73 74 61 6e 64  tion.** outstand
3e7d0 69 6e 67 20 61 74 20 74 69 6d 65 2c 20 74 68 69  ing at time, thi
3e7e0 73 20 70 61 72 61 6d 65 74 65 72 20 61 6c 73 6f  s parameter also
3e7f0 20 72 65 70 6f 72 74 73 20 74 68 65 20 6e 75 6d   reports the num
3e800 62 65 72 20 6f 66 20 74 68 72 65 61 64 73 0a 2a  ber of threads.*
3e810 2a 20 75 73 69 6e 67 20 73 63 72 61 74 63 68 20  * using scratch 
3e820 6d 65 6d 6f 72 79 20 61 74 20 74 68 65 20 73 61  memory at the sa
3e830 6d 65 20 74 69 6d 65 2e 3c 2f 64 64 3e 0a 2a 2a  me time.</dd>.**
3e840 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53  .** <dt>SQLITE_S
3e850 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56  TATUS_SCRATCH_OV
3e860 45 52 46 4c 4f 57 3c 2f 64 74 3e 0a 2a 2a 20 3c  ERFLOW</dt>.** <
3e870 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65  dd>This paramete
3e880 72 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  r returns the nu
3e890 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
3e8a0 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 0a   scratch memory.
3e8b0 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 68  ** allocation wh
3e8c0 69 63 68 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65  ich could not be
3e8d0 20 73 74 61 74 69 73 66 69 65 64 20 62 79 20 74   statisfied by t
3e8e0 68 65 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  he [SQLITE_CONFI
3e8f0 47 5f 53 43 52 41 54 43 48 5d 0a 2a 2a 20 62 75  G_SCRATCH].** bu
3e900 66 66 65 72 20 61 6e 64 20 77 68 65 72 65 20 66  ffer and where f
3e910 6f 72 63 65 64 20 74 6f 20 6f 76 65 72 66 6c 6f  orced to overflo
3e920 77 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61  w to [sqlite3_ma
3e930 6c 6c 6f 63 28 29 5d 2e 20 20 54 68 65 20 76 61  lloc()].  The va
3e940 6c 75 65 73 0a 2a 2a 20 72 65 74 75 72 6e 65 64  lues.** returned
3e950 20 69 6e 63 6c 75 64 65 20 6f 76 65 72 66 6c 6f   include overflo
3e960 77 73 20 62 65 63 61 75 73 65 20 74 68 65 20 72  ws because the r
3e970 65 71 75 65 73 74 65 64 20 61 6c 6c 6f 63 61 74  equested allocat
3e980 69 6f 6e 20 77 61 73 20 74 6f 6f 0a 2a 2a 20 6c  ion was too.** l
3e990 61 72 67 65 72 20 28 74 68 61 74 20 69 73 2c 20  arger (that is, 
3e9a0 62 65 63 61 75 73 65 20 74 68 65 20 72 65 71 75  because the requ
3e9b0 65 73 74 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e  ested allocation
3e9c0 20 77 61 73 20 6c 61 72 67 65 72 20 74 68 61 6e   was larger than
3e9d0 20 74 68 65 0a 2a 2a 20 22 73 7a 22 20 70 61 72   the.** "sz" par
3e9e0 61 6d 65 74 65 72 20 74 6f 20 5b 53 51 4c 49 54  ameter to [SQLIT
3e9f0 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48  E_CONFIG_SCRATCH
3ea00 5d 29 20 61 6e 64 20 62 65 63 61 75 73 65 20 6e  ]) and because n
3ea10 6f 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72  o scratch buffer
3ea20 0a 2a 2a 20 73 6c 6f 74 73 20 77 65 72 65 20 61  .** slots were a
3ea30 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 20 3c 2f 64  vailable..** </d
3ea40 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
3ea50 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54  ITE_STATUS_SCRAT
3ea60 43 48 5f 53 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20  CH_SIZE</dt>.** 
3ea70 3c 64 64 3e 54 68 69 73 20 70 61 72 61 6d 65 74  <dd>This paramet
3ea80 65 72 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c  er records the l
3ea90 61 72 67 65 73 74 20 6d 65 6d 6f 72 79 20 61 6c  argest memory al
3eaa0 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74  location request
3eab0 0a 2a 2a 20 68 61 6e 64 65 64 20 74 6f 20 5b 73  .** handed to [s
3eac0 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 61 6c  cratch memory al
3ead0 6c 6f 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20  locator].  Only 
3eae0 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
3eaf0 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48  ed in the.** *pH
3eb00 69 67 68 77 61 74 65 72 20 70 61 72 61 6d 65 74  ighwater paramet
3eb10 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73  er to [sqlite3_s
3eb20 74 61 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69  tatus()] is of i
3eb30 6e 74 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68  nterest.  .** Th
3eb40 65 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20  e value written 
3eb50 69 6e 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65  into the *pCurre
3eb60 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  nt parameter is 
3eb70 75 6e 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a  undefined.</dd>.
3eb80 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45  **.** <dt>SQLITE
3eb90 5f 53 54 41 54 55 53 5f 50 41 52 53 45 52 5f 53  _STATUS_PARSER_S
3eba0 54 41 43 4b 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  TACK</dt>.** <dd
3ebb0 3e 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20  >This parameter 
3ebc0 72 65 63 6f 72 64 73 20 74 68 65 20 64 65 65 70  records the deep
3ebd0 65 73 74 20 70 61 72 73 65 72 20 73 74 61 63 6b  est parser stack
3ebe0 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79 0a 2a 2a  .  It is only.**
3ebf0 20 6d 65 61 6e 69 6e 67 66 75 6c 20 69 66 20 53   meaningful if S
3ec00 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65  QLite is compile
3ec10 64 20 77 69 74 68 20 5b 59 59 54 52 41 43 4b 4d  d with [YYTRACKM
3ec20 41 58 53 54 41 43 4b 44 45 50 54 48 5d 2e 3c 2f  AXSTACKDEPTH].</
3ec30 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2a 0a  dd>.** </dl>.**.
3ec40 2a 2a 20 4e 65 77 20 73 74 61 74 75 73 20 70 61  ** New status pa
3ec50 72 61 6d 65 74 65 72 73 20 6d 61 79 20 62 65 20  rameters may be 
3ec60 61 64 64 65 64 20 66 72 6f 6d 20 74 69 6d 65 20  added from time 
3ec70 74 6f 20 74 69 6d 65 2e 0a 2a 2f 0a 23 64 65 66  to time..*/.#def
3ec80 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55  ine SQLITE_STATU
3ec90 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 20 20 20  S_MEMORY_USED   
3eca0 20 20 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65         0.#define
3ecb0 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50   SQLITE_STATUS_P
3ecc0 41 47 45 43 41 43 48 45 5f 55 53 45 44 20 20 20  AGECACHE_USED   
3ecd0 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51      1.#define SQ
3ece0 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47 45  LITE_STATUS_PAGE
3ecf0 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57 20 20  CACHE_OVERFLOW  
3ed00 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   2.#define SQLIT
3ed10 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48  E_STATUS_SCRATCH
3ed20 5f 55 53 45 44 20 20 20 20 20 20 20 20 20 33 0a  _USED         3.
3ed30 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
3ed40 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 4f 56  TATUS_SCRATCH_OV
3ed50 45 52 46 4c 4f 57 20 20 20 20 20 34 0a 23 64 65  ERFLOW     4.#de
3ed60 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54  fine SQLITE_STAT
3ed70 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 20 20  US_MALLOC_SIZE  
3ed80 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e          5.#defin
3ed90 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  e SQLITE_STATUS_
3eda0 50 41 52 53 45 52 5f 53 54 41 43 4b 20 20 20 20  PARSER_STACK    
3edb0 20 20 20 20 20 36 0a 23 64 65 66 69 6e 65 20 53       6.#define S
3edc0 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41 47  QLITE_STATUS_PAG
3edd0 45 43 41 43 48 45 5f 53 49 5a 45 20 20 20 20 20  ECACHE_SIZE     
3ede0 20 20 37 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    7.#define SQLI
3edf0 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43  TE_STATUS_SCRATC
3ee00 48 5f 53 49 5a 45 20 20 20 20 20 20 20 20 20 38  H_SIZE         8
3ee10 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
3ee20 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e 65  : Database Conne
3ee30 63 74 69 6f 6e 20 53 74 61 74 75 73 20 7b 48 31  ction Status {H1
3ee40 37 35 30 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a  7500} <S60200>.*
3ee50 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
3ee60 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66  *.** This interf
3ee70 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20 72  ace is used to r
3ee80 65 74 72 69 65 76 65 20 72 75 6e 74 69 6d 65 20  etrieve runtime 
3ee90 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69  status informati
3eea0 6f 6e 20 0a 2a 2a 20 61 62 6f 75 74 20 61 20 73  on .** about a s
3eeb0 69 6e 67 6c 65 20 5b 64 61 74 61 62 61 73 65 20  ingle [database 
3eec0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20 54 68  connection].  Th
3eed0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
3eee0 20 69 73 20 74 68 65 0a 2a 2a 20 64 61 74 61 62   is the.** datab
3eef0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f  ase connection o
3ef00 62 6a 65 63 74 20 74 6f 20 62 65 20 69 6e 74 65  bject to be inte
3ef10 72 72 6f 67 61 74 65 64 2e 20 20 54 68 65 20 73  rrogated.  The s
3ef20 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a  econd argument.*
3ef30 2a 20 69 73 20 74 68 65 20 70 61 72 61 6d 65 74  * is the paramet
3ef40 65 72 20 74 6f 20 69 6e 74 65 72 72 6f 67 61 74  er to interrogat
3ef50 65 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c 20 74  e.  Currently, t
3ef60 68 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77 65 64 20  he only allowed 
3ef70 76 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65  value.** for the
3ef80 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
3ef90 72 20 69 73 20 5b 53 51 4c 49 54 45 5f 44 42 53  r is [SQLITE_DBS
3efa0 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f  TATUS_LOOKASIDE_
3efb0 55 53 45 44 5d 2e 0a 2a 2a 20 41 64 64 69 74 69  USED]..** Additi
3efc0 6f 6e 61 6c 20 6f 70 74 69 6f 6e 73 20 77 69 6c  onal options wil
3efd0 6c 20 6c 69 6b 65 6c 79 20 61 70 70 65 61 72 20  l likely appear 
3efe0 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  in future releas
3eff0 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a  es of SQLite..**
3f000 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20  .** The current 
3f010 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 65 71  value of the req
3f020 75 65 73 74 65 64 20 70 61 72 61 6d 65 74 65 72  uested parameter
3f030 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f   is written into
3f040 20 2a 70 43 75 72 0a 2a 2a 20 61 6e 64 20 74 68   *pCur.** and th
3f050 65 20 68 69 67 68 65 73 74 20 69 6e 73 74 61 6e  e highest instan
3f060 74 61 6e 65 6f 75 73 20 76 61 6c 75 65 20 69 73  taneous value is
3f070 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70   written into *p
3f080 48 69 77 74 72 2e 20 20 49 66 0a 2a 2a 20 74 68  Hiwtr.  If.** th
3f090 65 20 72 65 73 65 74 46 6c 67 20 69 73 20 74 72  e resetFlg is tr
3f0a0 75 65 2c 20 74 68 65 6e 20 74 68 65 20 68 69 67  ue, then the hig
3f0b0 68 65 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 6f  hest instantaneo
3f0c0 75 73 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 72  us value is.** r
3f0d0 65 73 65 74 20 62 61 63 6b 20 64 6f 77 6e 20 74  eset back down t
3f0e0 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61  o the current va
3f0f0 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  lue..**.** See a
3f100 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74  lso: [sqlite3_st
3f110 61 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c  atus()] and [sql
3f120 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75 73  ite3_stmt_status
3f130 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ()]..*/.SQLITE_A
3f140 50 49 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49  PI SQLITE_EXPERI
3f150 4d 45 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74  MENTAL int sqlit
3f160 65 33 5f 64 62 5f 73 74 61 74 75 73 28 73 71 6c  e3_db_status(sql
3f170 69 74 65 33 2a 2c 20 69 6e 74 20 6f 70 2c 20 69  ite3*, int op, i
3f180 6e 74 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70  nt *pCur, int *p
3f190 48 69 77 74 72 2c 20 69 6e 74 20 72 65 73 65 74  Hiwtr, int reset
3f1a0 46 6c 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  Flg);../*.** CAP
3f1b0 49 33 52 45 46 3a 20 53 74 61 74 75 73 20 50 61  I3REF: Status Pa
3f1c0 72 61 6d 65 74 65 72 73 20 66 6f 72 20 64 61 74  rameters for dat
3f1d0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
3f1e0 73 20 7b 48 31 37 35 32 30 7d 20 3c 48 31 37 35  s {H17520} <H175
3f1f0 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  00>.** EXPERIMEN
3f200 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  TAL.**.** These 
3f210 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74 68  constants are th
3f220 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 65  e available inte
3f230 67 65 72 20 22 76 65 72 62 73 22 20 74 68 61 74  ger "verbs" that
3f240 20 63 61 6e 20 62 65 20 70 61 73 73 65 64 20 61   can be passed a
3f250 73 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20  s.** the second 
3f260 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
3f270 5b 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74  [sqlite3_db_stat
3f280 75 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e  us()] interface.
3f290 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 76 65 72 62 73  .**.** New verbs
3f2a0 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e   may be added in
3f2b0 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73   future releases
3f2c0 20 6f 66 20 53 51 4c 69 74 65 2e 20 45 78 69 73   of SQLite. Exis
3f2d0 74 69 6e 67 20 76 65 72 62 73 0a 2a 2a 20 6d 69  ting verbs.** mi
3f2e0 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69 6e  ght be discontin
3f2f0 75 65 64 2e 20 41 70 70 6c 69 63 61 74 69 6f 6e  ued. Application
3f300 73 20 73 68 6f 75 6c 64 20 63 68 65 63 6b 20 74  s should check t
3f310 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65 20 66  he return code f
3f320 72 6f 6d 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  rom.** [sqlite3_
3f330 64 62 5f 73 74 61 74 75 73 28 29 5d 20 74 6f 20  db_status()] to 
3f340 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74  make sure that t
3f350 68 65 20 63 61 6c 6c 20 77 6f 72 6b 65 64 2e 0a  he call worked..
3f360 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  ** The [sqlite3_
3f370 64 62 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74  db_status()] int
3f380 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65 74 75  erface will retu
3f390 72 6e 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 65 72  rn a non-zero er
3f3a0 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 66 20 61  ror code.** if a
3f3b0 20 64 69 73 63 6f 6e 74 69 6e 75 65 64 20 6f 72   discontinued or
3f3c0 20 75 6e 73 75 70 70 6f 72 74 65 64 20 76 65 72   unsupported ver
3f3d0 62 20 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a  b is invoked..**
3f3e0 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e  .** <dl>.** <dt>
3f3f0 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f  SQLITE_DBSTATUS_
3f400 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 3c 2f  LOOKASIDE_USED</
3f410 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20  dt>.** <dd>This 
3f420 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e  parameter return
3f430 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
3f440 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79  lookaside memory
3f450 20 73 6c 6f 74 73 20 63 75 72 72 65 6e 74 6c 79   slots currently
3f460 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f 75 74 2e  .** checked out.
3f470 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a  </dd>.** </dl>.*
3f480 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
3f490 5f 44 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53  _DBSTATUS_LOOKAS
3f4a0 49 44 45 5f 55 53 45 44 20 20 20 20 20 30 0a 0a  IDE_USED     0..
3f4b0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
3f4c0 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65 6d   Prepared Statem
3f4d0 65 6e 74 20 53 74 61 74 75 73 20 7b 48 31 37 35  ent Status {H175
3f4e0 35 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20  50} <S60200>.** 
3f4f0 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
3f500 2a 2a 20 45 61 63 68 20 70 72 65 70 61 72 65 64  ** Each prepared
3f510 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 69 6e 74   statement maint
3f520 61 69 6e 73 20 76 61 72 69 6f 75 73 0a 2a 2a 20  ains various.** 
3f530 5b 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54  [SQLITE_STMTSTAT
3f540 55 53 5f 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65  US_SORT | counte
3f550 72 73 5d 20 74 68 61 74 20 6d 65 61 73 75 72 65  rs] that measure
3f560 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f   the number.** o
3f570 66 20 74 69 6d 65 73 20 69 74 20 68 61 73 20 70  f times it has p
3f580 65 72 66 6f 72 6d 65 64 20 73 70 65 63 69 66 69  erformed specifi
3f590 63 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 54  c operations.  T
3f5a0 68 65 73 65 20 63 6f 75 6e 74 65 72 73 20 63 61  hese counters ca
3f5b0 6e 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20  n.** be used to 
3f5c0 6d 6f 6e 69 74 6f 72 20 74 68 65 20 70 65 72 66  monitor the perf
3f5d0 6f 72 6d 61 6e 63 65 20 63 68 61 72 61 63 74 65  ormance characte
3f5e0 72 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 70  ristics of the p
3f5f0 72 65 70 61 72 65 64 0a 2a 2a 20 73 74 61 74 65  repared.** state
3f600 6d 65 6e 74 73 2e 20 20 46 6f 72 20 65 78 61 6d  ments.  For exam
3f610 70 6c 65 2c 20 69 66 20 74 68 65 20 6e 75 6d 62  ple, if the numb
3f620 65 72 20 6f 66 20 74 61 62 6c 65 20 73 74 65 70  er of table step
3f630 73 20 67 72 65 61 74 6c 79 20 65 78 63 65 65 64  s greatly exceed
3f640 73 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20  s.** the number 
3f650 6f 66 20 74 61 62 6c 65 20 73 65 61 72 63 68 65  of table searche
3f660 73 20 6f 72 20 72 65 73 75 6c 74 20 72 6f 77 73  s or result rows
3f670 2c 20 74 68 61 74 20 77 6f 75 6c 64 20 74 65 6e  , that would ten
3f680 64 20 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a  d to indicate.**
3f690 20 74 68 61 74 20 74 68 65 20 70 72 65 70 61 72   that the prepar
3f6a0 65 64 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  ed statement is 
3f6b0 75 73 69 6e 67 20 61 20 66 75 6c 6c 20 74 61 62  using a full tab
3f6c0 6c 65 20 73 63 61 6e 20 72 61 74 68 65 72 20 74  le scan rather t
3f6d0 68 61 6e 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e  han.** an index.
3f6e0 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e    .**.** This in
3f6f0 74 65 72 66 61 63 65 20 69 73 20 75 73 65 64 20  terface is used 
3f700 74 6f 20 72 65 74 72 69 65 76 65 20 61 6e 64 20  to retrieve and 
3f710 72 65 73 65 74 20 63 6f 75 6e 74 65 72 20 76 61  reset counter va
3f720 6c 75 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 20 5b  lues from.** a [
3f730 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
3f740 6e 74 5d 2e 20 20 54 68 65 20 66 69 72 73 74 20  nt].  The first 
3f750 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
3f760 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
3f770 6e 74 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 6f 20  nt.** object to 
3f780 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65 64 2e  be interrogated.
3f790 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67    The second arg
3f7a0 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 61 6e 20 69  ument.** is an i
3f7b0 6e 74 65 67 65 72 20 63 6f 64 65 20 66 6f 72 20  nteger code for 
3f7c0 61 20 73 70 65 63 69 66 69 63 20 5b 53 51 4c 49  a specific [SQLI
3f7d0 54 45 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f  TE_STMTSTATUS_SO
3f7e0 52 54 20 7c 20 63 6f 75 6e 74 65 72 5d 0a 2a 2a  RT | counter].**
3f7f0 20 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61   to be interroga
3f800 74 65 64 2e 20 0a 2a 2a 20 54 68 65 20 63 75 72  ted. .** The cur
3f810 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68  rent value of th
3f820 65 20 72 65 71 75 65 73 74 65 64 20 63 6f 75 6e  e requested coun
3f830 74 65 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ter is returned.
3f840 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 73 65 74  .** If the reset
3f850 46 6c 67 20 69 73 20 74 72 75 65 2c 20 74 68 65  Flg is true, the
3f860 6e 20 74 68 65 20 63 6f 75 6e 74 65 72 20 69 73  n the counter is
3f870 20 72 65 73 65 74 20 74 6f 20 7a 65 72 6f 20 61   reset to zero a
3f880 66 74 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74  fter this.** int
3f890 65 72 66 61 63 65 20 63 61 6c 6c 20 72 65 74 75  erface call retu
3f8a0 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  rns..**.** See a
3f8b0 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74  lso: [sqlite3_st
3f8c0 61 74 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c  atus()] and [sql
3f8d0 69 74 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29  ite3_db_status()
3f8e0 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  ]..*/.SQLITE_API
3f8f0 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45   SQLITE_EXPERIME
3f900 4e 54 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33  NTAL int sqlite3
3f910 5f 73 74 6d 74 5f 73 74 61 74 75 73 28 73 71 6c  _stmt_status(sql
3f920 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20  ite3_stmt*, int 
3f930 6f 70 2c 69 6e 74 20 72 65 73 65 74 46 6c 67 29  op,int resetFlg)
3f940 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
3f950 46 3a 20 53 74 61 74 75 73 20 50 61 72 61 6d 65  F: Status Parame
3f960 74 65 72 73 20 66 6f 72 20 70 72 65 70 61 72 65  ters for prepare
3f970 64 20 73 74 61 74 65 6d 65 6e 74 73 20 7b 48 31  d statements {H1
3f980 37 35 37 30 7d 20 3c 48 31 37 35 35 30 3e 0a 2a  7570} <H17550>.*
3f990 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
3f9a0 2a 0a 2a 2a 20 54 68 65 73 65 20 70 72 65 70 72  *.** These prepr
3f9b0 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 73 20 64  ocessor macros d
3f9c0 65 66 69 6e 65 20 69 6e 74 65 67 65 72 20 63 6f  efine integer co
3f9d0 64 65 73 20 74 68 61 74 20 6e 61 6d 65 20 63 6f  des that name co
3f9e0 75 6e 74 65 72 0a 2a 2a 20 76 61 6c 75 65 73 20  unter.** values 
3f9f0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
3fa00 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d  the [sqlite3_stm
3fa10 74 5f 73 74 61 74 75 73 28 29 5d 20 69 6e 74 65  t_status()] inte
3fa20 72 66 61 63 65 2e 0a 2a 2a 20 54 68 65 20 6d 65  rface..** The me
3fa30 61 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 76 61  anings of the va
3fa40 72 69 6f 75 73 20 63 6f 75 6e 74 65 72 73 20 61  rious counters a
3fa50 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  re as follows:.*
3fa60 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74  *.** <dl>.** <dt
3fa70 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54  >SQLITE_STMTSTAT
3fa80 55 53 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50  US_FULLSCAN_STEP
3fa90 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
3faa0 73 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  s is the number 
3fab0 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 53 51  of times that SQ
3fac0 4c 69 74 65 20 68 61 73 20 73 74 65 70 70 65 64  Lite has stepped
3fad0 20 66 6f 72 77 61 72 64 20 69 6e 0a 2a 2a 20 61   forward in.** a
3fae0 20 74 61 62 6c 65 20 61 73 20 70 61 72 74 20 6f   table as part o
3faf0 66 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73  f a full table s
3fb00 63 61 6e 2e 20 20 4c 61 72 67 65 20 6e 75 6d 62  can.  Large numb
3fb10 65 72 73 20 66 6f 72 20 74 68 69 73 20 63 6f 75  ers for this cou
3fb20 6e 74 65 72 0a 2a 2a 20 6d 61 79 20 69 6e 64 69  nter.** may indi
3fb30 63 61 74 65 20 6f 70 70 6f 72 74 75 6e 69 74 69  cate opportuniti
3fb40 65 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e  es for performan
3fb50 63 65 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 74  ce improvement t
3fb60 68 72 6f 75 67 68 20 0a 2a 2a 20 63 61 72 65 66  hrough .** caref
3fb70 75 6c 20 75 73 65 20 6f 66 20 69 6e 64 69 63 65  ul use of indice
3fb80 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  s.</dd>.**.** <d
3fb90 74 3e 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41  t>SQLITE_STMTSTA
3fba0 54 55 53 5f 53 4f 52 54 3c 2f 64 74 3e 0a 2a 2a  TUS_SORT</dt>.**
3fbb0 20 3c 64 64 3e 54 68 69 73 20 69 73 20 74 68 65   <dd>This is the
3fbc0 20 6e 75 6d 62 65 72 20 6f 66 20 73 6f 72 74 20   number of sort 
3fbd0 6f 70 65 72 61 74 69 6f 6e 73 20 74 68 61 74 20  operations that 
3fbe0 68 61 76 65 20 6f 63 63 75 72 72 65 64 2e 0a 2a  have occurred..*
3fbf0 2a 20 41 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c  * A non-zero val
3fc00 75 65 20 69 6e 20 74 68 69 73 20 63 6f 75 6e 74  ue in this count
3fc10 65 72 20 6d 61 79 20 69 6e 64 69 63 61 74 65 20  er may indicate 
3fc20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74  an opportunity t
3fc30 6f 0a 2a 2a 20 69 6d 70 72 6f 76 65 6d 65 6e 74  o.** improvement
3fc40 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 68 72   performance thr
3fc50 6f 75 67 68 20 63 61 72 65 66 75 6c 20 75 73 65  ough careful use
3fc60 20 6f 66 20 69 6e 64 69 63 65 73 2e 3c 2f 64 64   of indices.</dd
3fc70 3e 0a 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f  >.**.** </dl>.*/
3fc80 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
3fc90 53 54 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c 53  STMTSTATUS_FULLS
3fca0 43 41 4e 5f 53 54 45 50 20 20 20 20 20 31 0a 23  CAN_STEP     1.#
3fcb0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54  define SQLITE_ST
3fcc0 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 20 20 20  MTSTATUS_SORT   
3fcd0 20 20 20 20 20 20 20 20 20 20 20 32 0a 0a 2f 2a             2../*
3fce0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 75  .** CAPI3REF: Cu
3fcf0 73 74 6f 6d 20 50 61 67 65 20 43 61 63 68 65 20  stom Page Cache 
3fd00 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52 49  Object.** EXPERI
3fd10 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65  MENTAL.**.** The
3fd20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20   sqlite3_pcache 
3fd30 74 79 70 65 20 69 73 20 6f 70 61 71 75 65 2e 20  type is opaque. 
3fd40 20 49 74 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74   It is implement
3fd50 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 6c 75  ed by.** the plu
3fd60 67 67 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 20 20  ggable module.  
3fd70 54 68 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20  The SQLite core 
3fd80 68 61 73 20 6e 6f 20 6b 6e 6f 77 6c 65 64 67 65  has no knowledge
3fd90 20 6f 66 0a 2a 2a 20 69 74 73 20 73 69 7a 65 20   of.** its size 
3fda0 6f 72 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 75  or internal stru
3fdb0 63 74 75 72 65 20 61 6e 64 20 6e 65 76 65 72 20  cture and never 
3fdc0 64 65 61 6c 73 20 77 69 74 68 20 74 68 65 0a 2a  deals with the.*
3fdd0 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  * sqlite3_pcache
3fde0 20 6f 62 6a 65 63 74 20 65 78 63 65 70 74 20 62   object except b
3fdf0 79 20 68 6f 6c 64 69 6e 67 20 61 6e 64 20 70 61  y holding and pa
3fe00 73 73 69 6e 67 20 70 6f 69 6e 74 65 72 73 0a 2a  ssing pointers.*
3fe10 2a 20 74 6f 20 74 68 65 20 6f 62 6a 65 63 74 2e  * to the object.
3fe20 0a 2a 2a 0a 2a 2a 20 53 65 65 20 5b 73 71 6c 69  .**.** See [sqli
3fe30 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f  te3_pcache_metho
3fe40 64 73 5d 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  ds] for addition
3fe50 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
3fe60 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
3fe70 74 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  t sqlite3_pcache
3fe80 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 3b   sqlite3_pcache;
3fe90 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
3fea0 3a 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 44 65  : Application De
3feb0 66 69 6e 65 64 20 50 61 67 65 20 43 61 63 68 65  fined Page Cache
3fec0 2e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  ..** KEYWORDS: {
3fed0 70 61 67 65 20 63 61 63 68 65 7d 0a 2a 2a 20 45  page cache}.** E
3fee0 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a  XPERIMENTAL.**.*
3fef0 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63  * The [sqlite3_c
3ff00 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43  onfig]([SQLITE_C
3ff10 4f 4e 46 49 47 5f 50 43 41 43 48 45 5d 2c 20 2e  ONFIG_PCACHE], .
3ff20 2e 2e 29 20 69 6e 74 65 72 66 61 63 65 20 63 61  ..) interface ca
3ff30 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 61 6e  n.** register an
3ff40 20 61 6c 74 65 72 6e 61 74 69 76 65 20 70 61 67   alternative pag
3ff50 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e  e cache implemen
3ff60 74 61 74 69 6f 6e 20 62 79 20 70 61 73 73 69 6e  tation by passin
3ff70 67 20 69 6e 20 61 6e 20 0a 2a 2a 20 69 6e 73 74  g in an .** inst
3ff80 61 6e 63 65 20 6f 66 20 74 68 65 20 73 71 6c 69  ance of the sqli
3ff90 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f  te3_pcache_metho
3ffa0 64 73 20 73 74 72 75 63 74 75 72 65 2e 20 54 68  ds structure. Th
3ffb0 65 20 6d 61 6a 6f 72 69 74 79 20 6f 66 20 74 68  e majority of th
3ffc0 65 20 0a 2a 2a 20 68 65 61 70 20 6d 65 6d 6f 72  e .** heap memor
3ffd0 79 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65  y used by SQLite
3ffe0 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20   is used by the 
3fff0 70 61 67 65 20 63 61 63 68 65 20 74 6f 20 63 61  page cache to ca
40000 63 68 65 20 64 61 74 61 20 72 65 61 64 20 0a 2a  che data read .*
40010 2a 20 66 72 6f 6d 2c 20 6f 72 20 72 65 61 64 79  * from, or ready
40020 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20 74   to be written t
40030 6f 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20  o, the database 
40040 66 69 6c 65 2e 20 42 79 20 69 6d 70 6c 65 6d 65  file. By impleme
40050 6e 74 69 6e 67 20 61 20 0a 2a 2a 20 63 75 73 74  nting a .** cust
40060 6f 6d 20 70 61 67 65 20 63 61 63 68 65 20 75 73  om page cache us
40070 69 6e 67 20 74 68 69 73 20 41 50 49 2c 20 61 6e  ing this API, an
40080 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e   application can
40090 20 63 6f 6e 74 72 6f 6c 20 6d 6f 72 65 20 0a 2a   control more .*
400a0 2a 20 70 72 65 63 69 73 65 6c 79 20 74 68 65 20  * precisely the 
400b0 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79  amount of memory
400c0 20 63 6f 6e 73 75 6d 65 64 20 62 79 20 53 51 4c   consumed by SQL
400d0 69 74 65 2c 20 74 68 65 20 77 61 79 20 69 6e 20  ite, the way in 
400e0 77 68 69 63 68 20 0a 2a 2a 20 74 68 61 74 20 6d  which .** that m
400f0 65 6d 6f 72 79 20 69 73 20 61 6c 6c 6f 63 61 74  emory is allocat
40100 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65 64 2c  ed and released,
40110 20 61 6e 64 20 74 68 65 20 70 6f 6c 69 63 69 65   and the policie
40120 73 20 75 73 65 64 20 74 6f 20 0a 2a 2a 20 64 65  s used to .** de
40130 74 65 72 6d 69 6e 65 20 65 78 61 63 74 6c 79 20  termine exactly 
40140 77 68 69 63 68 20 70 61 72 74 73 20 6f 66 20 61  which parts of a
40150 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
40160 72 65 20 63 61 63 68 65 64 20 61 6e 64 20 66 6f  re cached and fo
40170 72 20 0a 2a 2a 20 68 6f 77 20 6c 6f 6e 67 2e 0a  r .** how long..
40180 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e  **.** The conten
40190 74 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  ts of the sqlite
401a0 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73  3_pcache_methods
401b0 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 63   structure are c
401c0 6f 70 69 65 64 20 74 6f 20 61 6e 0a 2a 2a 20 69  opied to an.** i
401d0 6e 74 65 72 6e 61 6c 20 62 75 66 66 65 72 20 62  nternal buffer b
401e0 79 20 53 51 4c 69 74 65 20 77 69 74 68 69 6e 20  y SQLite within 
401f0 74 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c  the call to [sql
40200 69 74 65 33 5f 63 6f 6e 66 69 67 5d 2e 20 20 48  ite3_config].  H
40210 65 6e 63 65 0a 2a 2a 20 74 68 65 20 61 70 70 6c  ence.** the appl
40220 69 63 61 74 69 6f 6e 20 6d 61 79 20 64 69 73 63  ication may disc
40230 61 72 64 20 74 68 65 20 70 61 72 61 6d 65 74 65  ard the paramete
40240 72 20 61 66 74 65 72 20 74 68 65 20 63 61 6c 6c  r after the call
40250 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f   to.** [sqlite3_
40260 63 6f 6e 66 69 67 28 29 5d 20 72 65 74 75 72 6e  config()] return
40270 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 49 6e  s..**.** The xIn
40280 69 74 28 29 20 6d 65 74 68 6f 64 20 69 73 20 63  it() method is c
40290 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20 65  alled once for e
402a0 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c  ach call to [sql
402b0 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
402c0 29 5d 0a 2a 2a 20 28 75 73 75 61 6c 6c 79 20 6f  )].** (usually o
402d0 6e 6c 79 20 6f 6e 63 65 20 64 75 72 69 6e 67 20  nly once during 
402e0 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20  the lifetime of 
402f0 74 68 65 20 70 72 6f 63 65 73 73 29 2e 20 49 74  the process). It
40300 20 69 73 20 70 61 73 73 65 64 0a 2a 2a 20 61 20   is passed.** a 
40310 63 6f 70 79 20 6f 66 20 74 68 65 20 73 71 6c 69  copy of the sqli
40320 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f  te3_pcache_metho
40330 64 73 2e 70 41 72 67 20 76 61 6c 75 65 2e 20 49  ds.pArg value. I
40340 74 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  t can be used to
40350 20 73 65 74 0a 2a 2a 20 75 70 20 67 6c 6f 62 61   set.** up globa
40360 6c 20 73 74 72 75 63 74 75 72 65 73 20 61 6e 64  l structures and
40370 20 6d 75 74 65 78 65 73 20 72 65 71 75 69 72 65   mutexes require
40380 64 20 62 79 20 74 68 65 20 63 75 73 74 6f 6d 20  d by the custom 
40390 70 61 67 65 20 63 61 63 68 65 20 0a 2a 2a 20 69  page cache .** i
403a0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 0a  mplementation. .
403b0 2a 2a 0a 2a 2a 20 54 68 65 20 78 53 68 75 74 64  **.** The xShutd
403c0 6f 77 6e 28 29 20 6d 65 74 68 6f 64 20 69 73 20  own() method is 
403d0 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68  called from with
403e0 69 6e 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74  in [sqlite3_shut
403f0 64 6f 77 6e 28 29 5d 2c 20 0a 2a 2a 20 69 66 20  down()], .** if 
40400 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
40410 69 6e 76 6f 6b 65 73 20 74 68 69 73 20 41 50 49  invokes this API
40420 2e 20 49 74 20 63 61 6e 20 62 65 20 75 73 65 64  . It can be used
40430 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 0a 2a 2a   to clean up .**
40440 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67   any outstanding
40450 20 72 65 73 6f 75 72 63 65 73 20 62 65 66 6f 72   resources befor
40460 65 20 70 72 6f 63 65 73 73 20 73 68 75 74 64 6f  e process shutdo
40470 77 6e 2c 20 69 66 20 72 65 71 75 69 72 65 64 2e  wn, if required.
40480 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 68 6f  .**.** SQLite ho
40490 6c 64 73 20 61 20 5b 53 51 4c 49 54 45 5f 4d 55  lds a [SQLITE_MU
404a0 54 45 58 5f 52 45 43 55 52 53 49 56 45 5d 20 6d  TEX_RECURSIVE] m
404b0 75 74 65 78 20 77 68 65 6e 20 69 74 20 69 6e 76  utex when it inv
404c0 6f 6b 65 73 0a 2a 2a 20 74 68 65 20 78 49 6e 69  okes.** the xIni
404d0 74 20 6d 65 74 68 6f 64 2c 20 73 6f 20 74 68 65  t method, so the
404e0 20 78 49 6e 69 74 20 6d 65 74 68 6f 64 20 6e 65   xInit method ne
404f0 65 64 20 6e 6f 74 20 62 65 20 74 68 72 65 61 64  ed not be thread
40500 73 61 66 65 2e 20 20 54 68 65 0a 2a 2a 20 78 53  safe.  The.** xS
40510 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 20 69  hutdown method i
40520 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72  s only called fr
40530 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 73 68 75 74  om [sqlite3_shut
40540 64 6f 77 6e 28 29 5d 20 73 6f 20 69 74 20 64 6f  down()] so it do
40550 65 73 0a 2a 2a 20 6e 6f 74 20 6e 65 65 64 20 74  es.** not need t
40560 6f 20 62 65 20 74 68 72 65 61 64 73 61 66 65 20  o be threadsafe 
40570 65 69 74 68 65 72 2e 20 20 41 6c 6c 20 6f 74 68  either.  All oth
40580 65 72 20 6d 65 74 68 6f 64 73 20 6d 75 73 74 20  er methods must 
40590 62 65 20 74 68 72 65 61 64 73 61 66 65 0a 2a 2a  be threadsafe.**
405a0 20 69 6e 20 6d 75 6c 74 69 74 68 72 65 61 64 65   in multithreade
405b0 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 0a  d applications..
405c0 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c  **.** SQLite wil
405d0 6c 20 6e 65 76 65 72 20 69 6e 76 6f 6b 65 20 78  l never invoke x
405e0 49 6e 69 74 28 29 20 6d 6f 72 65 20 74 68 61 6e  Init() more than
405f0 20 6f 6e 63 65 20 77 69 74 68 6f 75 74 20 61 6e   once without an
40600 20 69 6e 74 65 72 76 65 6e 69 6e 67 0a 2a 2a 20   intervening.** 
40610 63 61 6c 6c 20 74 6f 20 78 53 68 75 74 64 6f 77  call to xShutdow
40620 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  n()..**.** The x
40630 43 72 65 61 74 65 28 29 20 6d 65 74 68 6f 64 20  Create() method 
40640 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 73 74  is used to const
40650 72 75 63 74 20 61 20 6e 65 77 20 63 61 63 68 65  ruct a new cache
40660 20 69 6e 73 74 61 6e 63 65 2e 20 20 53 51 4c 69   instance.  SQLi
40670 74 65 0a 2a 2a 20 77 69 6c 6c 20 74 79 70 69 63  te.** will typic
40680 61 6c 6c 79 20 63 72 65 61 74 65 20 6f 6e 65 20  ally create one 
40690 63 61 63 68 65 20 69 6e 73 74 61 6e 63 65 20 66  cache instance f
406a0 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 64 61 74  or each open dat
406b0 61 62 61 73 65 20 66 69 6c 65 2c 0a 2a 2a 20 74  abase file,.** t
406c0 68 6f 75 67 68 20 74 68 69 73 20 69 73 20 6e 6f  hough this is no
406d0 74 20 67 75 61 72 61 6e 74 65 65 64 2e 20 54 68  t guaranteed. Th
406e0 65 0a 2a 2a 20 66 69 72 73 74 20 70 61 72 61 6d  e.** first param
406f0 65 74 65 72 2c 20 73 7a 50 61 67 65 2c 20 69 73  eter, szPage, is
40700 20 74 68 65 20 73 69 7a 65 20 69 6e 20 62 79 74   the size in byt
40710 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 73 20  es of the pages 
40720 74 68 61 74 20 6d 75 73 74 0a 2a 2a 20 62 65 20  that must.** be 
40730 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65  allocated by the
40740 20 63 61 63 68 65 2e 20 20 73 7a 50 61 67 65 20   cache.  szPage 
40750 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61 20 70 6f  will not be a po
40760 77 65 72 20 6f 66 20 74 77 6f 2e 20 20 73 7a 50  wer of two.  szP
40770 61 67 65 0a 2a 2a 20 77 69 6c 6c 20 74 68 65 20  age.** will the 
40780 70 61 67 65 20 73 69 7a 65 20 6f 66 20 74 68 65  page size of the
40790 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74   database file t
407a0 68 61 74 20 69 73 20 74 6f 20 62 65 20 63 61 63  hat is to be cac
407b0 68 65 64 20 70 6c 75 73 20 61 6e 0a 2a 2a 20 69  hed plus an.** i
407c0 6e 63 72 65 6d 65 6e 74 20 28 68 65 72 65 20 63  ncrement (here c
407d0 61 6c 6c 65 64 20 22 52 22 29 20 6f 66 20 61 62  alled "R") of ab
407e0 6f 75 74 20 31 30 30 20 6f 72 20 32 30 30 2e 20  out 100 or 200. 
407f0 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 65   SQLite will use
40800 20 74 68 65 0a 2a 2a 20 65 78 74 72 61 20 52 20   the.** extra R 
40810 62 79 74 65 73 20 6f 6e 20 65 61 63 68 20 70 61  bytes on each pa
40820 67 65 20 74 6f 20 73 74 6f 72 65 20 6d 65 74 61  ge to store meta
40830 64 61 74 61 20 61 62 6f 75 74 20 74 68 65 20 75  data about the u
40840 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 64 61 74  nderlying.** dat
40850 61 62 61 73 65 20 70 61 67 65 20 6f 6e 20 64 69  abase page on di
40860 73 6b 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6f  sk.  The value o
40870 66 20 52 20 64 65 70 65 6e 64 73 0a 2a 2a 20 6f  f R depends.** o
40880 6e 20 74 68 65 20 53 51 4c 69 74 65 20 76 65 72  n the SQLite ver
40890 73 69 6f 6e 2c 20 74 68 65 20 74 61 72 67 65 74  sion, the target
408a0 20 70 6c 61 74 66 6f 72 6d 2c 20 61 6e 64 20 68   platform, and h
408b0 6f 77 20 53 51 4c 69 74 65 20 77 61 73 20 63 6f  ow SQLite was co
408c0 6d 70 69 6c 65 64 2e 0a 2a 2a 20 52 20 69 73 20  mpiled..** R is 
408d0 63 6f 6e 73 74 61 6e 74 20 66 6f 72 20 61 20 70  constant for a p
408e0 61 72 74 69 63 75 6c 61 72 20 62 75 69 6c 64 20  articular build 
408f0 6f 66 20 53 51 4c 69 74 65 2e 20 20 54 68 65 20  of SQLite.  The 
40900 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
40910 74 6f 0a 2a 2a 20 78 43 72 65 61 74 65 28 29 2c  to.** xCreate(),
40920 20 62 50 75 72 67 65 61 62 6c 65 2c 20 69 73 20   bPurgeable, is 
40930 74 72 75 65 20 69 66 20 74 68 65 20 63 61 63 68  true if the cach
40940 65 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 20  e being created 
40950 77 69 6c 6c 0a 2a 2a 20 62 65 20 75 73 65 64 20  will.** be used 
40960 74 6f 20 63 61 63 68 65 20 64 61 74 61 62 61 73  to cache databas
40970 65 20 70 61 67 65 73 20 6f 66 20 61 20 66 69 6c  e pages of a fil
40980 65 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b  e stored on disk
40990 2c 20 6f 72 0a 2a 2a 20 66 61 6c 73 65 20 69 66  , or.** false if
409a0 20 69 74 20 69 73 20 75 73 65 64 20 66 6f 72 20   it is used for 
409b0 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  an in-memory dat
409c0 61 62 61 73 65 2e 20 54 68 65 20 63 61 63 68 65  abase. The cache
409d0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a   implementation.
409e0 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65  ** does not have
409f0 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67 20   to do anything 
40a00 73 70 65 63 69 61 6c 20 62 61 73 65 64 20 77 69  special based wi
40a10 74 68 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  th the value of 
40a20 62 50 75 72 67 65 61 62 6c 65 3b 0a 2a 2a 20 69  bPurgeable;.** i
40a30 74 20 69 73 20 70 75 72 65 6c 79 20 61 64 76 69  t is purely advi
40a40 73 6f 72 79 2e 20 20 4f 6e 20 61 20 63 61 63 68  sory.  On a cach
40a50 65 20 77 68 65 72 65 20 62 50 75 72 67 65 61 62  e where bPurgeab
40a60 6c 65 20 69 73 20 66 61 6c 73 65 2c 20 53 51 4c  le is false, SQL
40a70 69 74 65 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76 65  ite will.** neve
40a80 72 20 69 6e 76 6f 6b 65 20 78 55 6e 70 69 6e 28  r invoke xUnpin(
40a90 29 20 65 78 63 65 70 74 20 74 6f 20 64 65 6c 69  ) except to deli
40aa0 62 65 72 61 74 65 6c 79 20 64 65 6c 65 74 65 20  berately delete 
40ab0 61 20 70 61 67 65 2e 0a 2a 2a 20 49 6e 20 6f 74  a page..** In ot
40ac0 68 65 72 20 77 6f 72 64 73 2c 20 61 20 63 61 63  her words, a cac
40ad0 68 65 20 63 72 65 61 74 65 64 20 77 69 74 68 20  he created with 
40ae0 62 50 75 72 67 65 61 62 6c 65 20 73 65 74 20 74  bPurgeable set t
40af0 6f 20 66 61 6c 73 65 20 77 69 6c 6c 0a 2a 2a 20  o false will.** 
40b00 6e 65 76 65 72 20 63 6f 6e 74 61 69 6e 20 61 6e  never contain an
40b10 79 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73  y unpinned pages
40b20 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 61 63  ..**.** The xCac
40b30 68 65 73 69 7a 65 28 29 20 6d 65 74 68 6f 64 20  hesize() method 
40b40 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 20 61 74  may be called at
40b50 20 61 6e 79 20 74 69 6d 65 20 62 79 20 53 51 4c   any time by SQL
40b60 69 74 65 20 74 6f 20 73 65 74 20 74 68 65 0a 2a  ite to set the.*
40b70 2a 20 73 75 67 67 65 73 74 65 64 20 6d 61 78 69  * suggested maxi
40b80 6d 75 6d 20 63 61 63 68 65 2d 73 69 7a 65 20 28  mum cache-size (
40b90 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
40ba0 73 74 6f 72 65 64 20 62 79 29 20 74 68 65 20 63  stored by) the c
40bb0 61 63 68 65 0a 2a 2a 20 69 6e 73 74 61 6e 63 65  ache.** instance
40bc0 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66   passed as the f
40bd0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 54  irst argument. T
40be0 68 69 73 20 69 73 20 74 68 65 20 76 61 6c 75 65  his is the value
40bf0 20 63 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e   configured usin
40c00 67 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20  g.** the SQLite 
40c10 22 5b 50 52 41 47 4d 41 20 63 61 63 68 65 5f 73  "[PRAGMA cache_s
40c20 69 7a 65 5d 22 20 63 6f 6d 6d 61 6e 64 2e 20 41  ize]" command. A
40c30 73 20 77 69 74 68 20 74 68 65 20 62 50 75 72 67  s with the bPurg
40c40 65 61 62 6c 65 20 70 61 72 61 6d 65 74 65 72 2c  eable parameter,
40c50 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  .** the implemen
40c60 74 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 65  tation is not re
40c70 71 75 69 72 65 64 20 74 6f 20 64 6f 20 61 6e 79  quired to do any
40c80 74 68 69 6e 67 20 77 69 74 68 20 74 68 69 73 0a  thing with this.
40c90 2a 2a 20 76 61 6c 75 65 3b 20 69 74 20 69 73 20  ** value; it is 
40ca0 61 64 76 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a  advisory only..*
40cb0 2a 0a 2a 2a 20 54 68 65 20 78 50 61 67 65 63 6f  *.** The xPageco
40cc0 75 6e 74 28 29 20 6d 65 74 68 6f 64 20 73 68 6f  unt() method sho
40cd0 75 6c 64 20 72 65 74 75 72 6e 20 74 68 65 20 6e  uld return the n
40ce0 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 63  umber of pages c
40cf0 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 73 74 6f 72  urrently.** stor
40d00 65 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e  ed in the cache.
40d10 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 78 46 65 74  .** .** The xFet
40d20 63 68 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75  ch() method is u
40d30 73 65 64 20 74 6f 20 66 65 74 63 68 20 61 20 70  sed to fetch a p
40d40 61 67 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61  age and return a
40d50 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20   pointer to it. 
40d60 0a 2a 2a 20 41 20 27 70 61 67 65 27 2c 20 69 6e  .** A 'page', in
40d70 20 74 68 69 73 20 63 6f 6e 74 65 78 74 2c 20 69   this context, i
40d80 73 20 61 20 62 75 66 66 65 72 20 6f 66 20 73 7a  s a buffer of sz
40d90 50 61 67 65 20 62 79 74 65 73 20 61 6c 69 67 6e  Page bytes align
40da0 65 64 20 61 74 20 61 6e 0a 2a 2a 20 38 2d 62 79  ed at an.** 8-by
40db0 74 65 20 62 6f 75 6e 64 61 72 79 2e 20 54 68 65  te boundary. The
40dc0 20 70 61 67 65 20 74 6f 20 62 65 20 66 65 74 63   page to be fetc
40dd0 68 65 64 20 69 73 20 64 65 74 65 72 6d 69 6e 65  hed is determine
40de0 64 20 62 79 20 74 68 65 20 6b 65 79 2e 20 54 68  d by the key. Th
40df0 65 0a 2a 2a 20 6d 69 6d 69 6d 75 6d 20 6b 65 79  e.** mimimum key
40e00 20 76 61 6c 75 65 20 69 73 20 31 2e 20 41 66 74   value is 1. Aft
40e10 65 72 20 69 74 20 68 61 73 20 62 65 65 6e 20 72  er it has been r
40e20 65 74 72 69 65 76 65 64 20 75 73 69 6e 67 20 78  etrieved using x
40e30 46 65 74 63 68 2c 20 74 68 65 20 70 61 67 65 20  Fetch, the page 
40e40 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65  .** is considere
40e50 64 20 74 6f 20 62 65 20 22 70 69 6e 6e 65 64 22  d to be "pinned"
40e60 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72  ..**.** If the r
40e70 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73  equested page is
40e80 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
40e90 70 61 67 65 20 63 61 63 68 65 2c 20 74 68 65 6e  page cache, then
40ea0 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 0a   the page cache.
40eb0 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
40ec0 6e 20 6d 75 73 74 20 72 65 74 75 72 6e 20 61 20  n must return a 
40ed0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
40ee0 61 67 65 20 62 75 66 66 65 72 20 77 69 74 68 20  age buffer with 
40ef0 69 74 73 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 69  its content.** i
40f00 6e 74 61 63 74 2e 20 20 49 66 20 74 68 65 20 72  ntact.  If the r
40f10 65 71 75 65 73 74 65 64 20 70 61 67 65 20 69 73  equested page is
40f20 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20   not already in 
40f30 74 68 65 20 63 61 63 68 65 2c 20 74 68 65 6e 20  the cache, then 
40f40 74 68 65 0a 2a 2a 20 62 65 68 61 76 69 6f 72 20  the.** behavior 
40f50 6f 66 20 74 68 65 20 63 61 63 68 65 20 69 6d 70  of the cache imp
40f60 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 64  lementation is d
40f70 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65  etermined by the
40f80 20 76 61 6c 75 65 20 6f 66 20 74 68 65 0a 2a 2a   value of the.**
40f90 20 63 72 65 61 74 65 46 6c 61 67 20 70 61 72 61   createFlag para
40fa0 6d 65 74 65 72 20 70 61 73 73 65 64 20 74 6f 20  meter passed to 
40fb0 78 46 65 74 63 68 2c 20 61 63 63 6f 72 64 69 6e  xFetch, accordin
40fc0 67 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69  g to the followi
40fd0 6e 67 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20  ng table:.**.** 
40fe0 3c 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 31 20  <table border=1 
40ff0 77 69 64 74 68 3d 38 35 25 20 61 6c 69 67 6e 3d  width=85% align=
41000 63 65 6e 74 65 72 3e 0a 2a 2a 20 3c 74 72 3e 3c  center>.** <tr><
41010 74 68 3e 20 63 72 65 61 74 65 46 6c 61 67 20 3c  th> createFlag <
41020 74 68 3e 20 42 65 68 61 76 69 6f 75 72 20 77 68  th> Behaviour wh
41030 65 6e 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61  en page is not a
41040 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68 65 0a  lready in cache.
41050 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 30 20 3c 74  ** <tr><td> 0 <t
41060 64 3e 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61  d> Do not alloca
41070 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 20  te a new page.  
41080 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 20  Return NULL..** 
41090 3c 74 72 3e 3c 74 64 3e 20 31 20 3c 74 64 3e 20  <tr><td> 1 <td> 
410a0 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70  Allocate a new p
410b0 61 67 65 20 69 66 20 69 74 20 65 61 73 79 20 61  age if it easy a
410c0 6e 64 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f  nd convenient to
410d0 20 64 6f 20 73 6f 2e 0a 2a 2a 20 20 20 20 20 20   do so..**      
410e0 20 20 20 20 20 20 20 20 20 20 20 4f 74 68 65 72             Other
410f0 77 69 73 65 20 72 65 74 75 72 6e 20 4e 55 4c 4c  wise return NULL
41100 2e 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 32 20  ..** <tr><td> 2 
41110 3c 74 64 3e 20 4d 61 6b 65 20 65 76 65 72 79 20  <td> Make every 
41120 65 66 66 6f 72 74 20 74 6f 20 61 6c 6c 6f 63 61  effort to alloca
41130 74 65 20 61 20 6e 65 77 20 70 61 67 65 2e 20 20  te a new page.  
41140 4f 6e 6c 79 20 72 65 74 75 72 6e 0a 2a 2a 20 20  Only return.**  
41150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e                 N
41160 55 4c 4c 20 69 66 20 61 6c 6c 6f 63 61 74 69 6e  ULL if allocatin
41170 67 20 61 20 6e 65 77 20 70 61 67 65 20 69 73 20  g a new page is 
41180 65 66 66 65 63 74 69 76 65 6c 79 20 69 6d 70 6f  effectively impo
41190 73 73 69 62 6c 65 2e 0a 2a 2a 20 3c 2f 74 61 62  ssible..** </tab
411a0 6c 65 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65  le>.**.** SQLite
411b0 20 77 69 6c 6c 20 6e 6f 72 6d 61 6c 6c 79 20 69   will normally i
411c0 6e 76 6f 6b 65 20 78 46 65 74 63 68 28 29 20 77  nvoke xFetch() w
411d0 69 74 68 20 61 20 63 72 65 61 74 65 46 6c 61 67  ith a createFlag
411e0 20 6f 66 20 30 20 6f 72 20 31 2e 20 20 49 66 0a   of 0 or 1.  If.
411f0 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20 78 46 65  ** a call to xFe
41200 74 63 68 28 29 20 77 69 74 68 20 63 72 65 61 74  tch() with creat
41210 65 46 6c 61 67 3d 3d 31 20 72 65 74 75 72 6e 73  eFlag==1 returns
41220 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 53 51 4c 69   NULL, then SQLi
41230 74 65 20 77 69 6c 6c 0a 2a 2a 20 61 74 74 65 6d  te will.** attem
41240 70 74 20 74 6f 20 75 6e 70 69 6e 20 6f 6e 65 20  pt to unpin one 
41250 6f 72 20 6d 6f 72 65 20 63 61 63 68 65 20 70 61  or more cache pa
41260 67 65 73 20 62 79 20 73 70 69 6c 6c 69 6e 67 20  ges by spilling 
41270 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a  the content of.*
41280 2a 20 70 69 6e 6e 65 64 20 70 61 67 65 73 20 74  * pinned pages t
41290 6f 20 64 69 73 6b 20 61 6e 64 20 73 79 6e 63 68  o disk and synch
412a0 69 6e 67 20 74 68 65 20 6f 70 65 72 61 74 69 6e  ing the operatin
412b0 67 20 73 79 73 74 65 6d 20 64 69 73 6b 20 63 61  g system disk ca
412c0 63 68 65 2e 20 41 66 74 65 72 0a 2a 2a 20 61 74  che. After.** at
412d0 74 65 6d 70 74 69 6e 67 20 74 6f 20 75 6e 70 69  tempting to unpi
412e0 6e 20 70 61 67 65 73 2c 20 74 68 65 20 78 46 65  n pages, the xFe
412f0 74 63 68 28 29 20 6d 65 74 68 6f 64 20 77 69 6c  tch() method wil
41300 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 61 67 61  l be invoked aga
41310 69 6e 20 77 69 74 68 0a 2a 2a 20 61 20 63 72 65  in with.** a cre
41320 61 74 65 46 6c 61 67 20 6f 66 20 32 2e 0a 2a 2a  ateFlag of 2..**
41330 0a 2a 2a 20 78 55 6e 70 69 6e 28 29 20 69 73 20  .** xUnpin() is 
41340 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 65  called by SQLite
41350 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20   with a pointer 
41360 74 6f 20 61 20 63 75 72 72 65 6e 74 6c 79 20 70  to a currently p
41370 69 6e 6e 65 64 20 70 61 67 65 0a 2a 2a 20 61 73  inned page.** as
41380 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75   its second argu
41390 6d 65 6e 74 2e 20 49 66 20 74 68 65 20 74 68 69  ment. If the thi
413a0 72 64 20 70 61 72 61 6d 65 74 65 72 2c 20 64 69  rd parameter, di
413b0 73 63 61 72 64 2c 20 69 73 20 6e 6f 6e 2d 7a 65  scard, is non-ze
413c0 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20  ro,.** then the 
413d0 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 65  page should be e
413e0 76 69 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20  victed from the 
413f0 63 61 63 68 65 2e 20 49 6e 20 74 68 69 73 20 63  cache. In this c
41400 61 73 65 20 53 51 4c 69 74 65 20 0a 2a 2a 20 61  ase SQLite .** a
41410 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20  ssumes that the 
41420 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20 70 61  next time the pa
41430 67 65 20 69 73 20 72 65 74 72 69 65 76 65 64 20  ge is retrieved 
41440 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 75  from the cache u
41450 73 69 6e 67 0a 2a 2a 20 74 68 65 20 78 46 65 74  sing.** the xFet
41460 63 68 28 29 20 6d 65 74 68 6f 64 2c 20 69 74 20  ch() method, it 
41470 77 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64 2e 20  will be zeroed. 
41480 49 66 20 74 68 65 20 64 69 73 63 61 72 64 20 70  If the discard p
41490 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 7a  arameter is.** z
414a0 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 61  ero, then the pa
414b0 67 65 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  ge is considered
414c0 20 74 6f 20 62 65 20 75 6e 70 69 6e 6e 65 64 2e   to be unpinned.
414d0 20 54 68 65 20 63 61 63 68 65 20 69 6d 70 6c 65   The cache imple
414e0 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6d 61 79  mentation.** may
414f0 20 63 68 6f 6f 73 65 20 74 6f 20 65 76 69 63 74   choose to evict
41500 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20   unpinned pages 
41510 61 74 20 61 6e 79 20 74 69 6d 65 2e 0a 2a 2a 0a  at any time..**.
41520 2a 2a 20 54 68 65 20 63 61 63 68 65 20 69 73 20  ** The cache is 
41530 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20  not required to 
41540 70 65 72 66 6f 72 6d 20 61 6e 79 20 72 65 66 65  perform any refe
41550 72 65 6e 63 65 20 63 6f 75 6e 74 69 6e 67 2e 20  rence counting. 
41560 41 20 73 69 6e 67 6c 65 20 0a 2a 2a 20 63 61 6c  A single .** cal
41570 6c 20 74 6f 20 78 55 6e 70 69 6e 28 29 20 75 6e  l to xUnpin() un
41580 70 69 6e 73 20 74 68 65 20 70 61 67 65 20 72 65  pins the page re
41590 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20  gardless of the 
415a0 6e 75 6d 62 65 72 20 6f 66 20 70 72 69 6f 72 20  number of prior 
415b0 63 61 6c 6c 73 20 0a 2a 2a 20 74 6f 20 78 46 65  calls .** to xFe
415c0 74 63 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tch()..**.** The
415d0 20 78 52 65 6b 65 79 28 29 20 6d 65 74 68 6f 64   xRekey() method
415e0 20 69 73 20 75 73 65 64 20 74 6f 20 63 68 61 6e   is used to chan
415f0 67 65 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65  ge the key value
41600 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
41610 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 70 61 73   the.** page pas
41620 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
41630 64 20 61 72 67 75 6d 65 6e 74 20 66 72 6f 6d 20  d argument from 
41640 6f 6c 64 4b 65 79 20 74 6f 20 6e 65 77 4b 65 79  oldKey to newKey
41650 2e 20 49 66 20 74 68 65 20 63 61 63 68 65 0a 2a  . If the cache.*
41660 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6f 6e  * previously con
41670 74 61 69 6e 73 20 61 6e 20 65 6e 74 72 79 20 61  tains an entry a
41680 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e  ssociated with n
41690 65 77 4b 65 79 2c 20 69 74 20 73 68 6f 75 6c 64  ewKey, it should
416a0 20 62 65 0a 2a 2a 20 64 69 73 63 61 72 64 65 64   be.** discarded
416b0 2e 20 41 6e 79 20 70 72 69 6f 72 20 63 61 63 68  . Any prior cach
416c0 65 20 65 6e 74 72 79 20 61 73 73 6f 63 69 61 74  e entry associat
416d0 65 64 20 77 69 74 68 20 6e 65 77 4b 65 79 20 69  ed with newKey i
416e0 73 20 67 75 61 72 61 6e 74 65 65 64 20 6e 6f 74  s guaranteed not
416f0 0a 2a 2a 20 74 6f 20 62 65 20 70 69 6e 6e 65 64  .** to be pinned
41700 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c  ..**.** When SQL
41710 69 74 65 20 63 61 6c 6c 73 20 74 68 65 20 78 54  ite calls the xT
41720 72 75 6e 63 61 74 65 28 29 20 6d 65 74 68 6f 64  runcate() method
41730 2c 20 74 68 65 20 63 61 63 68 65 20 6d 75 73 74  , the cache must
41740 20 64 69 73 63 61 72 64 20 61 6c 6c 0a 2a 2a 20   discard all.** 
41750 65 78 69 73 74 69 6e 67 20 63 61 63 68 65 20 65  existing cache e
41760 6e 74 72 69 65 73 20 77 69 74 68 20 70 61 67 65  ntries with page
41770 20 6e 75 6d 62 65 72 73 20 28 6b 65 79 73 29 20   numbers (keys) 
41780 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20  greater than or 
41790 65 71 75 61 6c 0a 2a 2a 20 74 6f 20 74 68 65 20  equal.** to the 
417a0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 4c 69  value of the iLi
417b0 6d 69 74 20 70 61 72 61 6d 65 74 65 72 20 70 61  mit parameter pa
417c0 73 73 65 64 20 74 6f 20 78 54 72 75 6e 63 61 74  ssed to xTruncat
417d0 65 28 29 2e 20 49 66 20 61 6e 79 0a 2a 2a 20 6f  e(). If any.** o
417e0 66 20 74 68 65 73 65 20 70 61 67 65 73 20 61 72  f these pages ar
417f0 65 20 70 69 6e 6e 65 64 2c 20 74 68 65 79 20 61  e pinned, they a
41800 72 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 75 6e  re implicitly un
41810 70 69 6e 6e 65 64 2c 20 6d 65 61 6e 69 6e 67 20  pinned, meaning 
41820 74 68 61 74 0a 2a 2a 20 74 68 65 79 20 63 61 6e  that.** they can
41830 20 62 65 20 73 61 66 65 6c 79 20 64 69 73 63 61   be safely disca
41840 72 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rded..**.** The 
41850 78 44 65 73 74 72 6f 79 28 29 20 6d 65 74 68 6f  xDestroy() metho
41860 64 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 6c  d is used to del
41870 65 74 65 20 61 20 63 61 63 68 65 20 61 6c 6c 6f  ete a cache allo
41880 63 61 74 65 64 20 62 79 20 78 43 72 65 61 74 65  cated by xCreate
41890 28 29 2e 0a 2a 2a 20 41 6c 6c 20 72 65 73 6f 75  ()..** All resou
418a0 72 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  rces associated 
418b0 77 69 74 68 20 74 68 65 20 73 70 65 63 69 66 69  with the specifi
418c0 65 64 20 63 61 63 68 65 20 73 68 6f 75 6c 64 20  ed cache should 
418d0 62 65 20 66 72 65 65 64 2e 20 41 66 74 65 72 0a  be freed. After.
418e0 2a 2a 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 78  ** calling the x
418f0 44 65 73 74 72 6f 79 28 29 20 6d 65 74 68 6f 64  Destroy() method
41900 2c 20 53 51 4c 69 74 65 20 63 6f 6e 73 69 64 65  , SQLite conside
41910 72 73 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  rs the [sqlite3_
41920 70 63 61 63 68 65 2a 5d 0a 2a 2a 20 68 61 6e 64  pcache*].** hand
41930 6c 65 20 69 6e 76 61 6c 69 64 2c 20 61 6e 64 20  le invalid, and 
41940 77 69 6c 6c 20 6e 6f 74 20 75 73 65 20 69 74 20  will not use it 
41950 77 69 74 68 20 61 6e 79 20 6f 74 68 65 72 20 73  with any other s
41960 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
41970 74 68 6f 64 73 0a 2a 2a 20 66 75 6e 63 74 69 6f  thods.** functio
41980 6e 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ns..*/.typedef s
41990 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 70 63  truct sqlite3_pc
419a0 61 63 68 65 5f 6d 65 74 68 6f 64 73 20 73 71 6c  ache_methods sql
419b0 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68  ite3_pcache_meth
419c0 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c 69  ods;.struct sqli
419d0 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f  te3_pcache_metho
419e0 64 73 20 7b 0a 20 20 76 6f 69 64 20 2a 70 41 72  ds {.  void *pAr
419f0 67 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69 74  g;.  int (*xInit
41a00 29 28 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64  )(void*);.  void
41a10 20 28 2a 78 53 68 75 74 64 6f 77 6e 29 28 76 6f   (*xShutdown)(vo
41a20 69 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  id*);.  sqlite3_
41a30 70 63 61 63 68 65 20 2a 28 2a 78 43 72 65 61 74  pcache *(*xCreat
41a40 65 29 28 69 6e 74 20 73 7a 50 61 67 65 2c 20 69  e)(int szPage, i
41a50 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29 3b 0a  nt bPurgeable);.
41a60 20 20 76 6f 69 64 20 28 2a 78 43 61 63 68 65 73    void (*xCaches
41a70 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 70 63 61  ize)(sqlite3_pca
41a80 63 68 65 2a 2c 20 69 6e 74 20 6e 43 61 63 68 65  che*, int nCache
41a90 73 69 7a 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78  size);.  int (*x
41aa0 50 61 67 65 63 6f 75 6e 74 29 28 73 71 6c 69 74  Pagecount)(sqlit
41ab0 65 33 5f 70 63 61 63 68 65 2a 29 3b 0a 20 20 76  e3_pcache*);.  v
41ac0 6f 69 64 20 2a 28 2a 78 46 65 74 63 68 29 28 73  oid *(*xFetch)(s
41ad0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c 20  qlite3_pcache*, 
41ae0 75 6e 73 69 67 6e 65 64 20 6b 65 79 2c 20 69 6e  unsigned key, in
41af0 74 20 63 72 65 61 74 65 46 6c 61 67 29 3b 0a 20  t createFlag);. 
41b00 20 76 6f 69 64 20 28 2a 78 55 6e 70 69 6e 29 28   void (*xUnpin)(
41b10 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 2c  sqlite3_pcache*,
41b20 20 76 6f 69 64 2a 2c 20 69 6e 74 20 64 69 73 63   void*, int disc
41b30 61 72 64 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78  ard);.  void (*x
41b40 52 65 6b 65 79 29 28 73 71 6c 69 74 65 33 5f 70  Rekey)(sqlite3_p
41b50 63 61 63 68 65 2a 2c 20 76 6f 69 64 2a 2c 20 75  cache*, void*, u
41b60 6e 73 69 67 6e 65 64 20 6f 6c 64 4b 65 79 2c 20  nsigned oldKey, 
41b70 75 6e 73 69 67 6e 65 64 20 6e 65 77 4b 65 79 29  unsigned newKey)
41b80 3b 0a 20 20 76 6f 69 64 20 28 2a 78 54 72 75 6e  ;.  void (*xTrun
41b90 63 61 74 65 29 28 73 71 6c 69 74 65 33 5f 70 63  cate)(sqlite3_pc
41ba0 61 63 68 65 2a 2c 20 75 6e 73 69 67 6e 65 64 20  ache*, unsigned 
41bb0 69 4c 69 6d 69 74 29 3b 0a 20 20 76 6f 69 64 20  iLimit);.  void 
41bc0 28 2a 78 44 65 73 74 72 6f 79 29 28 73 71 6c 69  (*xDestroy)(sqli
41bd0 74 65 33 5f 70 63 61 63 68 65 2a 29 3b 0a 7d 3b  te3_pcache*);.};
41be0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
41bf0 3a 20 4f 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20  : Online Backup 
41c00 4f 62 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52 49  Object.** EXPERI
41c10 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65  MENTAL.**.** The
41c20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20   sqlite3_backup 
41c30 6f 62 6a 65 63 74 20 72 65 63 6f 72 64 73 20 73  object records s
41c40 74 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  tate information
41c50 20 61 62 6f 75 74 20 61 6e 20 6f 6e 67 6f 69 6e   about an ongoin
41c60 67 0a 2a 2a 20 6f 6e 6c 69 6e 65 20 62 61 63 6b  g.** online back
41c70 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 54  up operation.  T
41c80 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  he sqlite3_backu
41c90 70 20 6f 62 6a 65 63 74 20 69 73 20 63 72 65 61  p object is crea
41ca0 74 65 64 20 62 79 0a 2a 2a 20 61 20 63 61 6c 6c  ted by.** a call
41cb0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 61 63   to [sqlite3_bac
41cc0 6b 75 70 5f 69 6e 69 74 28 29 5d 20 61 6e 64 20  kup_init()] and 
41cd0 69 73 20 64 65 73 74 72 6f 79 65 64 20 62 79 20  is destroyed by 
41ce0 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71  a call to.** [sq
41cf0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e  lite3_backup_fin
41d00 69 73 68 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65  ish()]..**.** Se
41d10 65 20 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74  e Also: [Using t
41d20 68 65 20 53 51 4c 69 74 65 20 4f 6e 6c 69 6e 65  he SQLite Online
41d30 20 42 61 63 6b 75 70 20 41 50 49 5d 0a 2a 2f 0a   Backup API].*/.
41d40 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
41d50 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 73 71  qlite3_backup sq
41d60 6c 69 74 65 33 5f 62 61 63 6b 75 70 3b 0a 0a 2f  lite3_backup;../
41d70 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f  *.** CAPI3REF: O
41d80 6e 6c 69 6e 65 20 42 61 63 6b 75 70 20 41 50 49  nline Backup API
41d90 2e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  ..** EXPERIMENTA
41da0 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49  L.**.** This API
41db0 20 69 73 20 75 73 65 64 20 74 6f 20 6f 76 65 72   is used to over
41dc0 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e  write the conten
41dd0 74 73 20 6f 66 20 6f 6e 65 20 64 61 74 61 62 61  ts of one databa
41de0 73 65 20 77 69 74 68 20 74 68 61 74 0a 2a 2a 20  se with that.** 
41df0 6f 66 20 61 6e 6f 74 68 65 72 2e 20 49 74 20 69  of another. It i
41e00 73 20 75 73 65 66 75 6c 20 65 69 74 68 65 72 20  s useful either 
41e10 66 6f 72 20 63 72 65 61 74 69 6e 67 20 62 61 63  for creating bac
41e20 6b 75 70 73 20 6f 66 20 64 61 74 61 62 61 73 65  kups of database
41e30 73 20 6f 72 0a 2a 2a 20 66 6f 72 20 63 6f 70 79  s or.** for copy
41e40 69 6e 67 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61  ing in-memory da
41e50 74 61 62 61 73 65 73 20 74 6f 20 6f 72 20 66 72  tabases to or fr
41e60 6f 6d 20 70 65 72 73 69 73 74 65 6e 74 20 66 69  om persistent fi
41e70 6c 65 73 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20  les. .**.** See 
41e80 41 6c 73 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65  Also: [Using the
41e90 20 53 51 4c 69 74 65 20 4f 6e 6c 69 6e 65 20 42   SQLite Online B
41ea0 61 63 6b 75 70 20 41 50 49 5d 0a 2a 2a 0a 2a 2a  ackup API].**.**
41eb0 20 45 78 63 6c 75 73 69 76 65 20 61 63 63 65 73   Exclusive acces
41ec0 73 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f  s is required to
41ed0 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
41ee0 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 74 68   database for th
41ef0 65 20 0a 2a 2a 20 64 75 72 61 74 69 6f 6e 20 6f  e .** duration o
41f00 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 2e  f the operation.
41f10 20 48 6f 77 65 76 65 72 20 74 68 65 20 73 6f 75   However the sou
41f20 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20  rce database is 
41f30 6f 6e 6c 79 0a 2a 2a 20 72 65 61 64 2d 6c 6f 63  only.** read-loc
41f40 6b 65 64 20 77 68 69 6c 65 20 69 74 20 69 73 20  ked while it is 
41f50 61 63 74 75 61 6c 6c 79 20 62 65 69 6e 67 20 72  actually being r
41f60 65 61 64 2c 20 69 74 20 69 73 20 6e 6f 74 20 6c  ead, it is not l
41f70 6f 63 6b 65 64 0a 2a 2a 20 63 6f 6e 74 69 6e 75  ocked.** continu
41f80 6f 75 73 6c 79 20 66 6f 72 20 74 68 65 20 65 6e  ously for the en
41f90 74 69 72 65 20 6f 70 65 72 61 74 69 6f 6e 2e 20  tire operation. 
41fa0 54 68 75 73 2c 20 74 68 65 20 62 61 63 6b 75 70  Thus, the backup
41fb0 20 6d 61 79 20 62 65 0a 2a 2a 20 70 65 72 66 6f   may be.** perfo
41fc0 72 6d 65 64 20 6f 6e 20 61 20 6c 69 76 65 20 64  rmed on a live d
41fd0 61 74 61 62 61 73 65 20 77 69 74 68 6f 75 74 20  atabase without 
41fe0 70 72 65 76 65 6e 74 69 6e 67 20 6f 74 68 65 72  preventing other
41ff0 20 75 73 65 72 73 20 66 72 6f 6d 0a 2a 2a 20 77   users from.** w
42000 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61  riting to the da
42010 74 61 62 61 73 65 20 66 6f 72 20 61 6e 20 65 78  tabase for an ex
42020 74 65 6e 64 65 64 20 70 65 72 69 6f 64 20 6f 66  tended period of
42030 20 74 69 6d 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 6f   time..** .** To
42040 20 70 65 72 66 6f 72 6d 20 61 20 62 61 63 6b 75   perform a backu
42050 70 20 6f 70 65 72 61 74 69 6f 6e 3a 20 0a 2a 2a  p operation: .**
42060 20 20 20 3c 6f 6c 3e 0a 2a 2a 20 20 20 20 20 3c     <ol>.**     <
42070 6c 69 3e 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61  li><b>sqlite3_ba
42080 63 6b 75 70 5f 69 6e 69 74 28 29 3c 2f 62 3e 20  ckup_init()</b> 
42090 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 74  is called once t
420a0 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  o initialize the
420b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 62 61 63 6b  .**         back
420c0 75 70 2c 20 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e  up, .**     <li>
420d0 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  <b>sqlite3_backu
420e0 70 5f 73 74 65 70 28 29 3c 2f 62 3e 20 69 73 20  p_step()</b> is 
420f0 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20 6d 6f  called one or mo
42100 72 65 20 74 69 6d 65 73 20 74 6f 20 74 72 61 6e  re times to tran
42110 73 66 65 72 20 0a 2a 2a 20 20 20 20 20 20 20 20  sfer .**        
42120 20 74 68 65 20 64 61 74 61 20 62 65 74 77 65 65   the data betwee
42130 6e 20 74 68 65 20 74 77 6f 20 64 61 74 61 62 61  n the two databa
42140 73 65 73 2c 20 61 6e 64 20 66 69 6e 61 6c 6c 79  ses, and finally
42150 0a 2a 2a 20 20 20 20 20 3c 6c 69 3e 3c 62 3e 73  .**     <li><b>s
42160 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69  qlite3_backup_fi
42170 6e 69 73 68 28 29 3c 2f 62 3e 20 69 73 20 63 61  nish()</b> is ca
42180 6c 6c 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20  lled to release 
42190 61 6c 6c 20 72 65 73 6f 75 72 63 65 73 20 0a 2a  all resources .*
421a0 2a 20 20 20 20 20 20 20 20 20 61 73 73 6f 63 69  *         associ
421b0 61 74 65 64 20 77 69 74 68 20 74 68 65 20 62 61  ated with the ba
421c0 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20  ckup operation. 
421d0 0a 2a 2a 20 20 20 3c 2f 6f 6c 3e 0a 2a 2a 20 54  .**   </ol>.** T
421e0 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20 65  here should be e
421f0 78 61 63 74 6c 79 20 6f 6e 65 20 63 61 6c 6c 20  xactly one call 
42200 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
42210 70 5f 66 69 6e 69 73 68 28 29 20 66 6f 72 20 65  p_finish() for e
42220 61 63 68 0a 2a 2a 20 73 75 63 63 65 73 73 66 75  ach.** successfu
42230 6c 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  l call to sqlite
42240 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2e  3_backup_init().
42250 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c 69 74 65  .**.** <b>sqlite
42260 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 3c  3_backup_init()<
42270 2f 62 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  /b>.**.** The fi
42280 72 73 74 20 74 77 6f 20 61 72 67 75 6d 65 6e 74  rst two argument
42290 73 20 70 61 73 73 65 64 20 74 6f 20 5b 73 71 6c  s passed to [sql
422a0 69 74 65 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74  ite3_backup_init
422b0 28 29 5d 20 61 72 65 20 74 68 65 20 64 61 74 61  ()] are the data
422c0 62 61 73 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 61  base.** handle a
422d0 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
422e0 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64  he destination d
422f0 61 74 61 62 61 73 65 20 61 6e 64 20 74 68 65 20  atabase and the 
42300 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 0a 2a  database name .*
42310 2a 20 75 73 65 64 20 74 6f 20 61 74 74 61 63 68  * used to attach
42320 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
42330 20 64 61 74 61 62 61 73 65 20 74 6f 20 74 68 65   database to the
42340 20 68 61 6e 64 6c 65 2e 20 54 68 65 20 64 61 74   handle. The dat
42350 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a 20 69 73  abase name.** is
42360 20 22 6d 61 69 6e 22 20 66 6f 72 20 74 68 65 20   "main" for the 
42370 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20 22  main database, "
42380 74 65 6d 70 22 20 66 6f 72 20 74 68 65 20 74 65  temp" for the te
42390 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65  mporary database
423a0 2c 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 61 6d 65  , or.** the name
423b0 20 73 70 65 63 69 66 69 65 64 20 61 73 20 70 61   specified as pa
423c0 72 74 20 6f 66 20 74 68 65 20 5b 41 54 54 41 43  rt of the [ATTAC
423d0 48 5d 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20  H] statement if 
423e0 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
423f0 69 73 0a 2a 2a 20 61 6e 20 61 74 74 61 63 68 65  is.** an attache
42400 64 20 64 61 74 61 62 61 73 65 2e 20 54 68 65 20  d database. The 
42410 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68  third and fourth
42420 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73 65   arguments passe
42430 64 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74 65 33  d to .** sqlite3
42440 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 20 69  _backup_init() i
42450 64 65 6e 74 69 66 79 20 74 68 65 20 5b 64 61 74  dentify the [dat
42460 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
42470 5d 0a 2a 2a 20 61 6e 64 20 64 61 74 61 62 61 73  ].** and databas
42480 65 20 6e 61 6d 65 20 75 73 65 64 0a 2a 2a 20 74  e name used.** t
42490 6f 20 61 63 63 65 73 73 20 74 68 65 20 73 6f 75  o access the sou
424a0 72 63 65 20 64 61 74 61 62 61 73 65 2e 20 54 68  rce database. Th
424b0 65 20 76 61 6c 75 65 73 20 70 61 73 73 65 64 20  e values passed 
424c0 66 6f 72 20 74 68 65 20 73 6f 75 72 63 65 20 61  for the source a
424d0 6e 64 20 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69  nd .** destinati
424e0 6f 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  on [database con
424f0 6e 65 63 74 69 6f 6e 5d 20 70 61 72 61 6d 65 74  nection] paramet
42500 65 72 73 20 6d 75 73 74 20 6e 6f 74 20 62 65 20  ers must not be 
42510 74 68 65 20 73 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20  the same..**.** 
42520 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
42530 72 73 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65  rs within sqlite
42540 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2c  3_backup_init(),
42550 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 20 72 65   then NULL is re
42560 74 75 72 6e 65 64 0a 2a 2a 20 61 6e 64 20 61 6e  turned.** and an
42570 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20   error code and 
42580 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72  error message wr
42590 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 5b  itten into the [
425a0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
425b0 69 6f 6e 5d 20 0a 2a 2a 20 70 61 73 73 65 64 20  ion] .** passed 
425c0 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67  as the first arg
425d0 75 6d 65 6e 74 2e 20 54 68 65 79 20 6d 61 79 20  ument. They may 
425e0 62 65 20 72 65 74 72 69 65 76 65 64 20 75 73 69  be retrieved usi
425f0 6e 67 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74  ng the.** [sqlit
42600 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 2c 20 5b  e3_errcode()], [
42610 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29  sqlite3_errmsg()
42620 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  ], and [sqlite3_
42630 65 72 72 6d 73 67 31 36 28 29 5d 20 66 75 6e 63  errmsg16()] func
42640 74 69 6f 6e 73 2e 0a 2a 2a 20 4f 74 68 65 72 77  tions..** Otherw
42650 69 73 65 2c 20 69 66 20 73 75 63 63 65 73 73 66  ise, if successf
42660 75 6c 2c 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ul, a pointer to
42670 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61 63   an [sqlite3_bac
42680 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 73 0a 2a  kup] object is.*
42690 2a 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69 73  * returned. This
426a0 20 70 6f 69 6e 74 65 72 20 6d 61 79 20 62 65 20   pointer may be 
426b0 75 73 65 64 20 77 69 74 68 20 74 68 65 20 73 71  used with the sq
426c0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
426d0 70 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74  p() and.** sqlit
426e0 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69 73 68  e3_backup_finish
426f0 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20  () functions to 
42700 70 65 72 66 6f 72 6d 20 74 68 65 20 73 70 65 63  perform the spec
42710 69 66 69 65 64 20 62 61 63 6b 75 70 20 0a 2a 2a  ified backup .**
42720 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a   operation..**.*
42730 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61 63  * <b>sqlite3_bac
42740 6b 75 70 5f 73 74 65 70 28 29 3c 2f 62 3e 0a 2a  kup_step()</b>.*
42750 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 5b 73  *.** Function [s
42760 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
42770 65 70 28 29 5d 20 69 73 20 75 73 65 64 20 74 6f  ep()] is used to
42780 20 63 6f 70 79 20 75 70 20 74 6f 20 6e 50 61 67   copy up to nPag
42790 65 20 70 61 67 65 73 20 62 65 74 77 65 65 6e 20  e pages between 
427a0 0a 2a 2a 20 74 68 65 20 73 6f 75 72 63 65 20 61  .** the source a
427b0 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64  nd destination d
427c0 61 74 61 62 61 73 65 73 2c 20 77 68 65 72 65 20  atabases, where 
427d0 6e 50 61 67 65 20 69 73 20 74 68 65 20 76 61 6c  nPage is the val
427e0 75 65 20 6f 66 20 74 68 65 20 0a 2a 2a 20 73 65  ue of the .** se
427f0 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 70  cond parameter p
42800 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65 33  assed to sqlite3
42810 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20  _backup_step(). 
42820 49 66 20 6e 50 61 67 65 20 69 73 20 61 20 6e 65  If nPage is a ne
42830 67 61 74 69 76 65 0a 2a 2a 20 76 61 6c 75 65 2c  gative.** value,
42840 20 61 6c 6c 20 72 65 6d 61 69 6e 69 6e 67 20 73   all remaining s
42850 6f 75 72 63 65 20 70 61 67 65 73 20 61 72 65 20  ource pages are 
42860 63 6f 70 69 65 64 2e 20 49 66 20 74 68 65 20 72  copied. If the r
42870 65 71 75 69 72 65 64 20 70 61 67 65 73 20 61 72  equired pages ar
42880 65 20 0a 2a 2a 20 73 75 63 63 65 73 66 75 6c 6c  e .** succesfull
42890 79 20 63 6f 70 69 65 64 2c 20 62 75 74 20 74 68  y copied, but th
428a0 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 6f  ere are still mo
428b0 72 65 20 70 61 67 65 73 20 74 6f 20 63 6f 70 79  re pages to copy
428c0 20 62 65 66 6f 72 65 20 74 68 65 20 0a 2a 2a 20   before the .** 
428d0 62 61 63 6b 75 70 20 69 73 20 63 6f 6d 70 6c 65  backup is comple
428e0 74 65 2c 20 69 74 20 72 65 74 75 72 6e 73 20 5b  te, it returns [
428f0 53 51 4c 49 54 45 5f 4f 4b 5d 2e 20 49 66 20 6e  SQLITE_OK]. If n
42900 6f 20 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20  o error occured 
42910 61 6e 64 20 74 68 65 72 65 20 0a 2a 2a 20 61 72  and there .** ar
42920 65 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73 20  e no more pages 
42930 74 6f 20 63 6f 70 79 2c 20 74 68 65 6e 20 5b 53  to copy, then [S
42940 51 4c 49 54 45 5f 44 4f 4e 45 5d 20 69 73 20 72  QLITE_DONE] is r
42950 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65  eturned. If an e
42960 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 2c  rror .** occurs,
42970 20 74 68 65 6e 20 61 6e 20 53 51 4c 69 74 65 20   then an SQLite 
42980 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
42990 74 75 72 6e 65 64 2e 20 41 73 20 77 65 6c 6c 20  turned. As well 
429a0 61 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 61  as [SQLITE_OK] a
429b0 6e 64 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f  nd.** [SQLITE_DO
429c0 4e 45 5d 2c 20 61 20 63 61 6c 6c 20 74 6f 20 73  NE], a call to s
429d0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
429e0 65 70 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20  ep() may return 
429f0 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59  [SQLITE_READONLY
42a00 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f  ],.** [SQLITE_NO
42a10 4d 45 4d 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 55  MEM], [SQLITE_BU
42a20 53 59 5d 2c 20 5b 53 51 4c 49 54 45 5f 4c 4f 43  SY], [SQLITE_LOC
42a30 4b 45 44 5d 2c 20 6f 72 20 61 6e 0a 2a 2a 20 5b  KED], or an.** [
42a40 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43 43  SQLITE_IOERR_ACC
42a50 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49 4f 45  ESS | SQLITE_IOE
42a60 52 52 5f 58 58 58 5d 20 65 78 74 65 6e 64 65 64  RR_XXX] extended
42a70 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 0a   error code..**.
42a80 2a 2a 20 41 73 20 77 65 6c 6c 20 61 73 20 74 68  ** As well as th
42a90 65 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65  e case where the
42aa0 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   destination dat
42ab0 61 62 61 73 65 20 66 69 6c 65 20 77 61 73 20 6f  abase file was o
42ac0 70 65 6e 65 64 20 66 6f 72 0a 2a 2a 20 72 65 61  pened for.** rea
42ad0 64 2d 6f 6e 6c 79 20 61 63 63 65 73 73 2c 20 73  d-only access, s
42ae0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
42af0 65 70 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20  ep() may return 
42b00 5b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59  [SQLITE_READONLY
42b10 5d 20 69 66 0a 2a 2a 20 74 68 65 20 64 65 73 74  ] if.** the dest
42b20 69 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e  ination is an in
42b30 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65  -memory database
42b40 20 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e   with a differen
42b50 74 20 70 61 67 65 20 73 69 7a 65 0a 2a 2a 20 66  t page size.** f
42b60 72 6f 6d 20 74 68 65 20 73 6f 75 72 63 65 20 64  rom the source d
42b70 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49  atabase..**.** I
42b80 66 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  f sqlite3_backup
42b90 5f 73 74 65 70 28 29 20 63 61 6e 6e 6f 74 20 6f  _step() cannot o
42ba0 62 74 61 69 6e 20 61 20 72 65 71 75 69 72 65 64  btain a required
42bb0 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 6c 6f 63   file-system loc
42bc0 6b 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 5b  k, then.** the [
42bd0 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e  sqlite3_busy_han
42be0 64 6c 65 72 20 7c 20 62 75 73 79 2d 68 61 6e 64  dler | busy-hand
42bf0 6c 65 72 20 66 75 6e 63 74 69 6f 6e 5d 0a 2a 2a  ler function].**
42c00 20 69 73 20 69 6e 76 6f 6b 65 64 20 28 69 66 20   is invoked (if 
42c10 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69 65 64  one is specified
42c20 29 2e 20 49 66 20 74 68 65 20 0a 2a 2a 20 62 75  ). If the .** bu
42c30 73 79 2d 68 61 6e 64 6c 65 72 20 72 65 74 75 72  sy-handler retur
42c40 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 20 62 65 66 6f  ns non-zero befo
42c50 72 65 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61  re the lock is a
42c60 76 61 69 6c 61 62 6c 65 2c 20 74 68 65 6e 20 0a  vailable, then .
42c70 2a 2a 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d  ** [SQLITE_BUSY]
42c80 20 69 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20   is returned to 
42c90 74 68 65 20 63 61 6c 6c 65 72 2e 20 49 6e 20 74  the caller. In t
42ca0 68 69 73 20 63 61 73 65 20 74 68 65 20 63 61 6c  his case the cal
42cb0 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  l to.** sqlite3_
42cc0 62 61 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61  backup_step() ca
42cd0 6e 20 62 65 20 72 65 74 72 69 65 64 20 6c 61 74  n be retried lat
42ce0 65 72 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63  er. If the sourc
42cf0 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63  e.** [database c
42d00 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 69 73  onnection].** is
42d10 20 62 65 69 6e 67 20 75 73 65 64 20 74 6f 20 77   being used to w
42d20 72 69 74 65 20 74 6f 20 74 68 65 20 73 6f 75 72  rite to the sour
42d30 63 65 20 64 61 74 61 62 61 73 65 20 77 68 65 6e  ce database when
42d40 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
42d50 73 74 65 70 28 29 0a 2a 2a 20 69 73 20 63 61 6c  step().** is cal
42d60 6c 65 64 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54  led, then [SQLIT
42d70 45 5f 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65 74  E_LOCKED] is ret
42d80 75 72 6e 65 64 20 69 6d 6d 65 64 69 61 74 65 6c  urned immediatel
42d90 79 2e 20 41 67 61 69 6e 2c 20 69 6e 20 74 68 69  y. Again, in thi
42da0 73 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 63 61  s.** case the ca
42db0 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61  ll to sqlite3_ba
42dc0 63 6b 75 70 5f 73 74 65 70 28 29 20 63 61 6e 20  ckup_step() can 
42dd0 62 65 20 72 65 74 72 69 65 64 20 6c 61 74 65 72  be retried later
42de0 20 6f 6e 2e 20 49 66 0a 2a 2a 20 5b 53 51 4c 49   on. If.** [SQLI
42df0 54 45 5f 49 4f 45 52 52 5f 41 43 43 45 53 53 20  TE_IOERR_ACCESS 
42e00 7c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58  | SQLITE_IOERR_X
42e10 58 58 5d 2c 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d  XX], [SQLITE_NOM
42e20 45 4d 5d 2c 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49  EM], or.** [SQLI
42e30 54 45 5f 52 45 41 44 4f 4e 4c 59 5d 20 69 73 20  TE_READONLY] is 
42e40 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 0a  returned, then .
42e50 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70  ** there is no p
42e60 6f 69 6e 74 20 69 6e 20 72 65 74 72 79 69 6e 67  oint in retrying
42e70 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c   the call to sql
42e80 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
42e90 28 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20 65 72  (). These .** er
42ea0 72 6f 72 73 20 61 72 65 20 63 6f 6e 73 69 64 65  rors are conside
42eb0 72 65 64 20 66 61 74 61 6c 2e 20 41 74 20 74 68  red fatal. At th
42ec0 69 73 20 70 6f 69 6e 74 20 74 68 65 20 61 70 70  is point the app
42ed0 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 61 63  lication must ac
42ee0 63 65 70 74 20 0a 2a 2a 20 74 68 61 74 20 74 68  cept .** that th
42ef0 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69  e backup operati
42f00 6f 6e 20 68 61 73 20 66 61 69 6c 65 64 20 61 6e  on has failed an
42f10 64 20 70 61 73 73 20 74 68 65 20 62 61 63 6b 75  d pass the backu
42f20 70 20 6f 70 65 72 61 74 69 6f 6e 20 68 61 6e 64  p operation hand
42f30 6c 65 20 0a 2a 2a 20 74 6f 20 74 68 65 20 73 71  le .** to the sq
42f40 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e  lite3_backup_fin
42f50 69 73 68 28 29 20 74 6f 20 72 65 6c 65 61 73 65  ish() to release
42f60 20 61 73 73 6f 63 69 61 74 65 64 20 72 65 73 6f   associated reso
42f70 75 72 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 6c  urces..**.** Fol
42f80 6c 6f 77 69 6e 67 20 74 68 65 20 66 69 72 73 74  lowing the first
42f90 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
42fa0 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2c 20  _backup_step(), 
42fb0 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
42fc0 6b 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 64  k is.** obtained
42fd0 20 6f 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74   on the destinat
42fe0 69 6f 6e 20 66 69 6c 65 2e 20 49 74 20 69 73 20  ion file. It is 
42ff0 6e 6f 74 20 72 65 6c 65 61 73 65 64 20 75 6e 74  not released unt
43000 69 6c 20 65 69 74 68 65 72 20 0a 2a 2a 20 73 71  il either .** sq
43010 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e  lite3_backup_fin
43020 69 73 68 28 29 20 69 73 20 63 61 6c 6c 65 64 20  ish() is called 
43030 6f 72 20 74 68 65 20 62 61 63 6b 75 70 20 6f 70  or the backup op
43040 65 72 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70 6c  eration is compl
43050 65 74 65 20 0a 2a 2a 20 61 6e 64 20 73 71 6c 69  ete .** and sqli
43060 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70 28  te3_backup_step(
43070 29 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54  ) returns [SQLIT
43080 45 5f 44 4f 4e 45 5d 2e 20 41 64 64 69 74 69 6f  E_DONE]. Additio
43090 6e 61 6c 6c 79 2c 20 65 61 63 68 20 74 69 6d 65  nally, each time
430a0 20 0a 2a 2a 20 61 20 63 61 6c 6c 20 74 6f 20 73   .** a call to s
430b0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
430c0 65 70 28 29 20 69 73 20 6d 61 64 65 20 61 20 5b  ep() is made a [
430d0 73 68 61 72 65 64 20 6c 6f 63 6b 5d 20 69 73 20  shared lock] is 
430e0 6f 62 74 61 69 6e 65 64 20 6f 6e 0a 2a 2a 20 74  obtained on.** t
430f0 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62 61  he source databa
43100 73 65 20 66 69 6c 65 2e 20 54 68 69 73 20 6c 6f  se file. This lo
43110 63 6b 20 69 73 20 72 65 6c 65 61 73 65 64 20 62  ck is released b
43120 65 66 6f 72 65 20 74 68 65 0a 2a 2a 20 73 71 6c  efore the.** sql
43130 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
43140 28 29 20 63 61 6c 6c 20 72 65 74 75 72 6e 73 2e  () call returns.
43150 20 42 65 63 61 75 73 65 20 74 68 65 20 73 6f 75   Because the sou
43160 72 63 65 20 64 61 74 61 62 61 73 65 20 69 73 20  rce database is 
43170 6e 6f 74 0a 2a 2a 20 6c 6f 63 6b 65 64 20 62 65  not.** locked be
43180 74 77 65 65 6e 20 63 61 6c 6c 73 20 74 6f 20 73  tween calls to s
43190 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
431a0 65 70 28 29 2c 20 69 74 20 6d 61 79 20 62 65 20  ep(), it may be 
431b0 6d 6f 64 69 66 69 65 64 20 6d 69 64 2d 77 61 79  modified mid-way
431c0 0a 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20  .** through the 
431d0 62 61 63 6b 75 70 20 70 72 6f 63 65 64 75 72 65  backup procedure
431e0 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20  . If the source 
431f0 64 61 74 61 62 61 73 65 20 69 73 20 6d 6f 64 69  database is modi
43200 66 69 65 64 20 62 79 20 61 6e 0a 2a 2a 20 65 78  fied by an.** ex
43210 74 65 72 6e 61 6c 20 70 72 6f 63 65 73 73 20 6f  ternal process o
43220 72 20 76 69 61 20 61 20 64 61 74 61 62 61 73 65  r via a database
43230 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 74 68 65   connection othe
43240 72 20 74 68 61 6e 20 74 68 65 20 6f 6e 65 20 62  r than the one b
43250 65 69 6e 67 0a 2a 2a 20 75 73 65 64 20 62 79 20  eing.** used by 
43260 74 68 65 20 62 61 63 6b 75 70 20 6f 70 65 72 61  the backup opera
43270 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 62  tion, then the b
43280 61 63 6b 75 70 20 77 69 6c 6c 20 62 65 20 74 72  ackup will be tr
43290 61 6e 73 70 61 72 65 6e 74 6c 79 0a 2a 2a 20 72  ansparently.** r
432a0 65 73 74 61 72 74 65 64 20 62 79 20 74 68 65 20  estarted by the 
432b0 6e 65 78 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c  next call to sql
432c0 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65 70  ite3_backup_step
432d0 28 29 2e 20 49 66 20 74 68 65 20 73 6f 75 72 63  (). If the sourc
432e0 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69  e .** database i
432f0 73 20 6d 6f 64 69 66 69 65 64 20 62 79 20 74 68  s modified by th
43300 65 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65  e using the same
43310 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
43320 74 69 6f 6e 20 61 73 20 69 73 20 75 73 65 64 0a  tion as is used.
43330 2a 2a 20 62 79 20 74 68 65 20 62 61 63 6b 75 70  ** by the backup
43340 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 6e   operation, then
43350 20 74 68 65 20 62 61 63 6b 75 70 20 64 61 74 61   the backup data
43360 62 61 73 65 20 69 73 20 74 72 61 6e 73 70 61 72  base is transpar
43370 65 6e 74 6c 79 20 0a 2a 2a 20 75 70 64 61 74 65  ently .** update
43380 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69  d at the same ti
43390 6d 65 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e 73 71 6c  me..**.** <b>sql
433a0 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
433b0 73 68 28 29 3c 2f 62 3e 0a 2a 2a 0a 2a 2a 20 4f  sh()</b>.**.** O
433c0 6e 63 65 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  nce sqlite3_back
433d0 75 70 5f 73 74 65 70 28 29 20 68 61 73 20 72 65  up_step() has re
433e0 74 75 72 6e 65 64 20 5b 53 51 4c 49 54 45 5f 44  turned [SQLITE_D
433f0 4f 4e 45 5d 2c 20 6f 72 20 77 68 65 6e 20 74 68  ONE], or when th
43400 65 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f  e .** applicatio
43410 6e 20 77 69 73 68 65 73 20 74 6f 20 61 62 61 6e  n wishes to aban
43420 64 6f 6e 20 74 68 65 20 62 61 63 6b 75 70 20 6f  don the backup o
43430 70 65 72 61 74 69 6f 6e 2c 20 74 68 65 20 5b 73  peration, the [s
43440 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d 0a 2a  qlite3_backup].*
43450 2a 20 6f 62 6a 65 63 74 20 73 68 6f 75 6c 64 20  * object should 
43460 62 65 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c  be passed to sql
43470 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
43480 73 68 28 29 2e 20 54 68 69 73 20 72 65 6c 65 61  sh(). This relea
43490 73 65 73 20 61 6c 6c 0a 2a 2a 20 72 65 73 6f 75  ses all.** resou
434a0 72 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20  rces associated 
434b0 77 69 74 68 20 74 68 65 20 62 61 63 6b 75 70 20  with the backup 
434c0 6f 70 65 72 61 74 69 6f 6e 2e 20 49 66 20 73 71  operation. If sq
434d0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
434e0 70 28 29 0a 2a 2a 20 68 61 73 20 6e 6f 74 20 79  p().** has not y
434f0 65 74 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c  et returned [SQL
43500 49 54 45 5f 44 4f 4e 45 5d 2c 20 74 68 65 6e 20  ITE_DONE], then 
43510 61 6e 79 20 61 63 74 69 76 65 20 77 72 69 74 65  any active write
43520 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e 20  -transaction on 
43530 74 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69  the.** destinati
43540 6f 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 72  on database is r
43550 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 54 68 65 20  olled back. The 
43560 5b 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5d  [sqlite3_backup]
43570 20 6f 62 6a 65 63 74 20 69 73 20 69 6e 76 61 6c   object is inval
43580 69 64 0a 2a 2a 20 61 6e 64 20 6d 61 79 20 6e 6f  id.** and may no
43590 74 20 62 65 20 75 73 65 64 20 66 6f 6c 6c 6f 77  t be used follow
435a0 69 6e 67 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  ing a call to sq
435b0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e  lite3_backup_fin
435c0 69 73 68 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ish()..**.** The
435d0 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
435e0 62 79 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  by sqlite3_backu
435f0 70 5f 66 69 6e 69 73 68 20 69 73 20 5b 53 51 4c  p_finish is [SQL
43600 49 54 45 5f 4f 4b 5d 20 69 66 20 6e 6f 20 65 72  ITE_OK] if no er
43610 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 72 65 64 2c  ror.** occurred,
43620 20 72 65 67 61 72 64 6c 65 73 73 20 6f 72 20 77   regardless or w
43630 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 73 71  hether or not sq
43640 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
43650 70 28 29 20 77 61 73 20 63 61 6c 6c 65 64 0a 2a  p() was called.*
43660 2a 20 61 20 73 75 66 66 69 63 69 65 6e 74 20 6e  * a sufficient n
43670 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74  umber of times t
43680 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 62  o complete the b
43690 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e  ackup operation.
436a0 20 4f 72 2c 20 69 66 0a 2a 2a 20 61 6e 20 6f 75   Or, if.** an ou
436b0 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64  t-of-memory cond
436c0 69 74 69 6f 6e 20 6f 72 20 49 4f 20 65 72 72 6f  ition or IO erro
436d0 72 20 6f 63 63 75 72 65 64 20 64 75 72 69 6e 67  r occured during
436e0 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71   a call to.** sq
436f0 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
43700 70 28 29 20 74 68 65 6e 20 5b 53 51 4c 49 54 45  p() then [SQLITE
43710 5f 4e 4f 4d 45 4d 5d 20 6f 72 20 61 6e 0a 2a 2a  _NOMEM] or an.**
43720 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41   [SQLITE_IOERR_A
43730 43 43 45 53 53 20 7c 20 53 51 4c 49 54 45 5f 49  CCESS | SQLITE_I
43740 4f 45 52 52 5f 58 58 58 5d 20 65 72 72 6f 72 20  OERR_XXX] error 
43750 63 6f 64 65 0a 2a 2a 20 69 73 20 72 65 74 75 72  code.** is retur
43760 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ned. In this cas
43770 65 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  e the error code
43780 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65   and an error me
43790 73 73 61 67 65 20 61 72 65 0a 2a 2a 20 77 72 69  ssage are.** wri
437a0 74 74 65 6e 20 74 6f 20 74 68 65 20 64 65 73 74  tten to the dest
437b0 69 6e 61 74 69 6f 6e 20 5b 64 61 74 61 62 61 73  ination [databas
437c0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a  e connection]..*
437d0 2a 0a 2a 2a 20 41 20 72 65 74 75 72 6e 20 6f 66  *.** A return of
437e0 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 6f   [SQLITE_BUSY] o
437f0 72 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  r [SQLITE_LOCKED
43800 5d 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 62  ] from sqlite3_b
43810 61 63 6b 75 70 5f 73 74 65 70 28 29 20 69 73 0a  ackup_step() is.
43820 2a 2a 20 6e 6f 74 20 61 20 70 65 72 6d 61 6e 65  ** not a permane
43830 6e 74 20 65 72 72 6f 72 20 61 6e 64 20 64 6f 65  nt error and doe
43840 73 20 6e 6f 74 20 61 66 66 65 63 74 20 74 68 65  s not affect the
43850 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66   return value of
43860 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  .** sqlite3_back
43870 75 70 5f 66 69 6e 69 73 68 28 29 2e 0a 2a 2a 0a  up_finish()..**.
43880 2a 2a 20 3c 62 3e 73 71 6c 69 74 65 33 5f 62 61  ** <b>sqlite3_ba
43890 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29  ckup_remaining()
438a0 2c 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  , sqlite3_backup
438b0 5f 70 61 67 65 63 6f 75 6e 74 28 29 3c 2f 62 3e  _pagecount()</b>
438c0 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 61 6c 6c  .**.** Each call
438d0 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   to sqlite3_back
438e0 75 70 5f 73 74 65 70 28 29 20 73 65 74 73 20 74  up_step() sets t
438f0 77 6f 20 76 61 6c 75 65 73 20 73 74 6f 72 65 64  wo values stored
43900 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 62   internally.** b
43910 79 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 62 61  y an [sqlite3_ba
43920 63 6b 75 70 5d 20 6f 62 6a 65 63 74 2e 20 54 68  ckup] object. Th
43930 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
43940 73 20 73 74 69 6c 6c 20 74 6f 20 62 65 20 62 61  s still to be ba
43950 63 6b 65 64 0a 2a 2a 20 75 70 2c 20 77 68 69 63  cked.** up, whic
43960 68 20 6d 61 79 20 62 65 20 71 75 65 72 69 65 64  h may be queried
43970 20 62 79 20 73 71 6c 69 74 65 33 5f 62 61 63 6b   by sqlite3_back
43980 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 2c 20  up_remaining(), 
43990 61 6e 64 20 74 68 65 20 74 6f 74 61 6c 0a 2a 2a  and the total.**
439a0 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
439b0 20 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 64   in the source d
439c0 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 77 68  atabase file, wh
439d0 69 63 68 20 6d 61 79 20 62 65 20 71 75 65 72 69  ich may be queri
439e0 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33  ed by.** sqlite3
439f0 5f 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e  _backup_pagecoun
43a00 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76  t()..**.** The v
43a10 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 62  alues returned b
43a20 79 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e  y these function
43a30 73 20 61 72 65 20 6f 6e 6c 79 20 75 70 64 61 74  s are only updat
43a40 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33  ed by.** sqlite3
43a50 5f 62 61 63 6b 75 70 5f 73 74 65 70 28 29 2e 20  _backup_step(). 
43a60 49 66 20 74 68 65 20 73 6f 75 72 63 65 20 64 61  If the source da
43a70 74 61 62 61 73 65 20 69 73 20 6d 6f 64 69 66 69  tabase is modifi
43a80 65 64 20 64 75 72 69 6e 67 20 61 20 62 61 63 6b  ed during a back
43a90 75 70 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2c  up.** operation,
43aa0 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 73   then the values
43ab0 20 61 72 65 20 6e 6f 74 20 75 70 64 61 74 65 64   are not updated
43ac0 20 74 6f 20 61 63 63 6f 75 6e 74 20 66 6f 72 20   to account for 
43ad0 61 6e 79 20 65 78 74 72 61 0a 2a 2a 20 70 61 67  any extra.** pag
43ae0 65 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20  es that need to 
43af0 62 65 20 75 70 64 61 74 65 64 20 6f 72 20 74 68  be updated or th
43b00 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 73 6f  e size of the so
43b10 75 72 63 65 20 64 61 74 61 62 61 73 65 20 66 69  urce database fi
43b20 6c 65 0a 2a 2a 20 63 68 61 6e 67 69 6e 67 2e 0a  le.** changing..
43b30 2a 2a 0a 2a 2a 20 3c 62 3e 43 6f 6e 63 75 72 72  **.** <b>Concurr
43b40 65 6e 74 20 55 73 61 67 65 20 6f 66 20 44 61 74  ent Usage of Dat
43b50 61 62 61 73 65 20 48 61 6e 64 6c 65 73 3c 2f 62  abase Handles</b
43b60 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 6f 75 72  >.**.** The sour
43b70 63 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  ce [database con
43b80 6e 65 63 74 69 6f 6e 5d 20 6d 61 79 20 62 65 20  nection] may be 
43b90 75 73 65 64 20 62 79 20 74 68 65 20 61 70 70 6c  used by the appl
43ba0 69 63 61 74 69 6f 6e 20 66 6f 72 20 6f 74 68 65  ication for othe
43bb0 72 0a 2a 2a 20 70 75 72 70 6f 73 65 73 20 77 68  r.** purposes wh
43bc0 69 6c 65 20 61 20 62 61 63 6b 75 70 20 6f 70 65  ile a backup ope
43bd0 72 61 74 69 6f 6e 20 69 73 20 75 6e 64 65 72 77  ration is underw
43be0 61 79 20 6f 72 20 62 65 69 6e 67 20 69 6e 69 74  ay or being init
43bf0 69 61 6c 69 7a 65 64 2e 0a 2a 2a 20 49 66 20 53  ialized..** If S
43c00 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65  QLite is compile
43c10 64 20 61 6e 64 20 63 6f 6e 66 69 67 75 72 65 64  d and configured
43c20 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68 72 65   to support thre
43c30 61 64 73 61 66 65 20 64 61 74 61 62 61 73 65 0a  adsafe database.
43c40 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 20  ** connections, 
43c50 74 68 65 6e 20 74 68 65 20 73 6f 75 72 63 65 20  then the source 
43c60 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
43c70 69 6f 6e 20 6d 61 79 20 62 65 20 75 73 65 64 20  ion may be used 
43c80 63 6f 6e 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20  concurrently.** 
43c90 66 72 6f 6d 20 77 69 74 68 69 6e 20 6f 74 68 65  from within othe
43ca0 72 20 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a  r threads..**.**
43cb0 20 48 6f 77 65 76 65 72 2c 20 74 68 65 20 61 70   However, the ap
43cc0 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74 20 67  plication must g
43cd0 75 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68  uarantee that th
43ce0 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61  e destination da
43cf0 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63  tabase.** connec
43d00 74 69 6f 6e 20 68 61 6e 64 6c 65 20 69 73 20 6e  tion handle is n
43d10 6f 74 20 70 61 73 73 65 64 20 74 6f 20 61 6e 79  ot passed to any
43d20 20 6f 74 68 65 72 20 41 50 49 20 28 62 79 20 61   other API (by a
43d30 6e 79 20 74 68 72 65 61 64 29 20 61 66 74 65 72  ny thread) after
43d40 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63   .** sqlite3_bac
43d50 6b 75 70 5f 69 6e 69 74 28 29 20 69 73 20 63 61  kup_init() is ca
43d60 6c 6c 65 64 20 61 6e 64 20 62 65 66 6f 72 65 20  lled and before 
43d70 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  the correspondin
43d80 67 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c  g call to.** sql
43d90 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
43da0 73 68 28 29 2e 20 55 6e 66 6f 72 74 75 6e 61 74  sh(). Unfortunat
43db0 65 6c 79 20 53 51 4c 69 74 65 20 64 6f 65 73 20  ely SQLite does 
43dc0 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 63 68  not currently ch
43dd0 65 63 6b 0a 2a 2a 20 66 6f 72 20 74 68 69 73 2c  eck.** for this,
43de0 20 69 66 20 74 68 65 20 61 70 70 6c 69 63 61 74   if the applicat
43df0 69 6f 6e 20 64 6f 65 73 20 75 73 65 20 74 68 65  ion does use the
43e00 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 5b 64 61   destination [da
43e10 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
43e20 6e 5d 0a 2a 2a 20 66 6f 72 20 73 6f 6d 65 20 6f  n].** for some o
43e30 74 68 65 72 20 70 75 72 70 6f 73 65 20 64 75 72  ther purpose dur
43e40 69 6e 67 20 61 20 62 61 63 6b 75 70 20 6f 70 65  ing a backup ope
43e50 72 61 74 69 6f 6e 2c 20 74 68 69 6e 67 73 20 6d  ration, things m
43e60 61 79 20 61 70 70 65 61 72 20 74 6f 0a 2a 2a 20  ay appear to.** 
43e70 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 20 62  work correctly b
43e80 75 74 20 69 6e 20 66 61 63 74 20 62 65 20 73 75  ut in fact be su
43e90 62 74 6c 79 20 6d 61 6c 66 75 6e 63 74 69 6f 6e  btly malfunction
43ea0 69 6e 67 2e 20 20 55 73 65 20 6f 66 20 74 68 65  ing.  Use of the
43eb0 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  .** destination 
43ec0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
43ed0 69 6f 6e 20 77 68 69 6c 65 20 61 20 62 61 63 6b  ion while a back
43ee0 75 70 20 69 73 20 69 6e 20 70 72 6f 67 72 65 73  up is in progres
43ef0 73 20 6d 69 67 68 74 0a 2a 2a 20 61 6c 73 6f 20  s might.** also 
43f00 63 61 75 73 65 20 61 20 6d 75 74 65 78 20 64 65  cause a mutex de
43f10 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46 75  adlock..**.** Fu
43f20 72 74 68 65 72 6d 6f 72 65 2c 20 69 66 20 72 75  rthermore, if ru
43f30 6e 6e 69 6e 67 20 69 6e 20 5b 73 68 61 72 65 64  nning in [shared
43f40 20 63 61 63 68 65 20 6d 6f 64 65 5d 2c 20 74 68   cache mode], th
43f50 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75  e application mu
43f60 73 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65 65 20  st.** guarantee 
43f70 74 68 61 74 20 74 68 65 20 73 68 61 72 65 64 20  that the shared 
43f80 63 61 63 68 65 20 75 73 65 64 20 62 79 20 74 68  cache used by th
43f90 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61  e destination da
43fa0 74 61 62 61 73 65 0a 2a 2a 20 69 73 20 6e 6f 74  tabase.** is not
43fb0 20 61 63 63 65 73 73 65 64 20 77 68 69 6c 65 20   accessed while 
43fc0 74 68 65 20 62 61 63 6b 75 70 20 69 73 20 72 75  the backup is ru
43fd0 6e 6e 69 6e 67 2e 20 49 6e 20 70 72 61 63 74 69  nning. In practi
43fe0 63 65 20 74 68 69 73 20 6d 65 61 6e 73 0a 2a 2a  ce this means.**
43ff0 20 74 68 61 74 20 74 68 65 20 61 70 70 6c 69 63   that the applic
44000 61 74 69 6f 6e 20 6d 75 73 74 20 67 75 61 72 61  ation must guara
44010 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 66 69  ntee that the fi
44020 6c 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 20 62  le-system file b
44030 65 69 6e 67 20 0a 2a 2a 20 62 61 63 6b 65 64 20  eing .** backed 
44040 75 70 20 74 6f 20 69 73 20 6e 6f 74 20 61 63 63  up to is not acc
44050 65 73 73 65 64 20 62 79 20 61 6e 79 20 63 6f 6e  essed by any con
44060 6e 65 63 74 69 6f 6e 20 77 69 74 68 69 6e 20 74  nection within t
44070 68 65 20 70 72 6f 63 65 73 73 2c 0a 2a 2a 20 6e  he process,.** n
44080 6f 74 20 6a 75 73 74 20 74 68 65 20 73 70 65 63  ot just the spec
44090 69 66 69 63 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  ific connection 
440a0 74 68 61 74 20 77 61 73 20 70 61 73 73 65 64 20  that was passed 
440b0 74 6f 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  to sqlite3_backu
440c0 70 5f 69 6e 69 74 28 29 2e 0a 2a 2a 0a 2a 2a 20  p_init()..**.** 
440d0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 61 63  The [sqlite3_bac
440e0 6b 75 70 5d 20 6f 62 6a 65 63 74 20 69 74 73 65  kup] object itse
440f0 6c 66 20 69 73 20 70 61 72 74 69 61 6c 6c 79 20  lf is partially 
44100 74 68 72 65 61 64 73 61 66 65 2e 20 4d 75 6c 74  threadsafe. Mult
44110 69 70 6c 65 20 0a 2a 2a 20 74 68 72 65 61 64 73  iple .** threads
44120 20 6d 61 79 20 73 61 66 65 6c 79 20 6d 61 6b 65   may safely make
44130 20 6d 75 6c 74 69 70 6c 65 20 63 6f 6e 63 75 72   multiple concur
44140 72 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71  rent calls to sq
44150 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
44160 70 28 29 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c  p()..** However,
44170 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61 63   the sqlite3_bac
44180 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20  kup_remaining() 
44190 61 6e 64 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  and sqlite3_back
441a0 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 29 0a 2a  up_pagecount().*
441b0 2a 20 41 50 49 73 20 61 72 65 20 6e 6f 74 20 73  * APIs are not s
441c0 74 72 69 63 74 6c 79 20 73 70 65 61 6b 69 6e 67  trictly speaking
441d0 20 74 68 72 65 61 64 73 61 66 65 2e 20 49 66 20   threadsafe. If 
441e0 74 68 65 79 20 61 72 65 20 69 6e 76 6f 6b 65 64  they are invoked
441f0 20 61 74 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20   at the.** same 
44200 74 69 6d 65 20 61 73 20 61 6e 6f 74 68 65 72 20  time as another 
44210 74 68 72 65 61 64 20 69 73 20 69 6e 76 6f 6b 69  thread is invoki
44220 6e 67 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ng sqlite3_backu
44230 70 5f 73 74 65 70 28 29 20 69 74 20 69 73 0a 2a  p_step() it is.*
44240 2a 20 70 6f 73 73 69 62 6c 65 20 74 68 61 74 20  * possible that 
44250 74 68 65 79 20 72 65 74 75 72 6e 20 69 6e 76 61  they return inva
44260 6c 69 64 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53  lid values..*/.S
44270 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
44280 33 5f 62 61 63 6b 75 70 20 2a 73 71 6c 69 74 65  3_backup *sqlite
44290 33 5f 62 61 63 6b 75 70 5f 69 6e 69 74 28 0a 20  3_backup_init(. 
442a0 20 73 71 6c 69 74 65 33 20 2a 70 44 65 73 74 2c   sqlite3 *pDest,
442b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
442c0 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69          /* Desti
442d0 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20  nation database 
442e0 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  handle */.  cons
442f0 74 20 63 68 61 72 20 2a 7a 44 65 73 74 4e 61 6d  t char *zDestNam
44300 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
44310 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f     /* Destinatio
44320 6e 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  n database name 
44330 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 53  */.  sqlite3 *pS
44340 6f 75 72 63 65 2c 20 20 20 20 20 20 20 20 20 20  ource,          
44350 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
44360 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 20 68  ource database h
44370 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  andle */.  const
44380 20 63 68 61 72 20 2a 7a 53 6f 75 72 63 65 4e 61   char *zSourceNa
44390 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  me              
443a0 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74 61    /* Source data
443b0 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 29 3b 0a  base name */.);.
443c0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
443d0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
443e0 65 70 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ep(sqlite3_backu
443f0 70 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67 65 29  p *p, int nPage)
44400 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
44410 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f   sqlite3_backup_
44420 66 69 6e 69 73 68 28 73 71 6c 69 74 65 33 5f 62  finish(sqlite3_b
44430 61 63 6b 75 70 20 2a 70 29 3b 0a 53 51 4c 49 54  ackup *p);.SQLIT
44440 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
44450 33 5f 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69  3_backup_remaini
44460 6e 67 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ng(sqlite3_backu
44470 70 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 41 50  p *p);.SQLITE_AP
44480 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61  I int sqlite3_ba
44490 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74 28 73  ckup_pagecount(s
444a0 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70  qlite3_backup *p
444b0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
444c0 45 46 3a 20 55 6e 6c 6f 63 6b 20 4e 6f 74 69 66  EF: Unlock Notif
444d0 69 63 61 74 69 6f 6e 0a 2a 2a 20 45 58 50 45 52  ication.** EXPER
444e0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 57 68  IMENTAL.**.** Wh
444f0 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 20 73 68  en running in sh
44500 61 72 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 2c  ared-cache mode,
44510 20 61 20 64 61 74 61 62 61 73 65 20 6f 70 65 72   a database oper
44520 61 74 69 6f 6e 20 6d 61 79 20 66 61 69 6c 20 77  ation may fail w
44530 69 74 68 0a 2a 2a 20 61 6e 20 5b 53 51 4c 49 54  ith.** an [SQLIT
44540 45 5f 4c 4f 43 4b 45 44 5d 20 65 72 72 6f 72 20  E_LOCKED] error 
44550 69 66 20 74 68 65 20 72 65 71 75 69 72 65 64 20  if the required 
44560 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 73 68 61  locks on the sha
44570 72 65 64 2d 63 61 63 68 65 20 6f 72 0a 2a 2a 20  red-cache or.** 
44580 69 6e 64 69 76 69 64 75 61 6c 20 74 61 62 6c 65  individual table
44590 73 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61  s within the sha
445a0 72 65 64 2d 63 61 63 68 65 20 63 61 6e 6e 6f 74  red-cache cannot
445b0 20 62 65 20 6f 62 74 61 69 6e 65 64 2e 20 53 65   be obtained. Se
445c0 65 0a 2a 2a 20 5b 53 51 4c 69 74 65 20 53 68 61  e.** [SQLite Sha
445d0 72 65 64 2d 43 61 63 68 65 20 4d 6f 64 65 5d 20  red-Cache Mode] 
445e0 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 69 6f  for a descriptio
445f0 6e 20 6f 66 20 73 68 61 72 65 64 2d 63 61 63 68  n of shared-cach
44600 65 20 6c 6f 63 6b 69 6e 67 2e 20 0a 2a 2a 20 54  e locking. .** T
44610 68 69 73 20 41 50 49 20 6d 61 79 20 62 65 20 75  his API may be u
44620 73 65 64 20 74 6f 20 72 65 67 69 73 74 65 72 20  sed to register 
44630 61 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20  a callback that 
44640 53 51 4c 69 74 65 20 77 69 6c 6c 20 69 6e 76 6f  SQLite will invo
44650 6b 65 20 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20  ke .** when the 
44660 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72 65  connection curre
44670 6e 74 6c 79 20 68 6f 6c 64 69 6e 67 20 74 68 65  ntly holding the
44680 20 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 20 72   required lock r
44690 65 6c 69 6e 71 75 69 73 68 65 73 20 69 74 2e 0a  elinquishes it..
446a0 2a 2a 20 54 68 69 73 20 41 50 49 20 69 73 20 6f  ** This API is o
446b0 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66  nly available if
446c0 20 74 68 65 20 6c 69 62 72 61 72 79 20 77 61 73   the library was
446d0 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74   compiled with t
446e0 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 4e  he.** [SQLITE_EN
446f0 41 42 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49  ABLE_UNLOCK_NOTI
44700 46 59 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73  FY] C-preprocess
44710 6f 72 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65  or symbol define
44720 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 41 6c 73  d..**.** See Als
44730 6f 3a 20 5b 55 73 69 6e 67 20 74 68 65 20 53 51  o: [Using the SQ
44740 4c 69 74 65 20 55 6e 6c 6f 63 6b 20 4e 6f 74 69  Lite Unlock Noti
44750 66 69 63 61 74 69 6f 6e 20 46 65 61 74 75 72 65  fication Feature
44760 5d 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65 64 2d  ]..**.** Shared-
44770 63 61 63 68 65 20 6c 6f 63 6b 73 20 61 72 65 20  cache locks are 
44780 72 65 6c 65 61 73 65 64 20 77 68 65 6e 20 61 20  released when a 
44790 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
447a0 69 6f 6e 20 63 6f 6e 63 6c 75 64 65 73 0a 2a 2a  ion concludes.**
447b0 20 69 74 73 20 63 75 72 72 65 6e 74 20 74 72 61   its current tra
447c0 6e 73 61 63 74 69 6f 6e 2c 20 65 69 74 68 65 72  nsaction, either
447d0 20 62 79 20 63 6f 6d 6d 69 74 74 69 6e 67 20 69   by committing i
447e0 74 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 69 74 20  t or rolling it 
447f0 62 61 63 6b 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65  back. .**.** Whe
44800 6e 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28  n a connection (
44810 6b 6e 6f 77 6e 20 61 73 20 74 68 65 20 62 6c 6f  known as the blo
44820 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 29  cked connection)
44830 20 66 61 69 6c 73 20 74 6f 20 6f 62 74 61 69 6e   fails to obtain
44840 20 61 0a 2a 2a 20 73 68 61 72 65 64 2d 63 61 63   a.** shared-cac
44850 68 65 20 6c 6f 63 6b 20 61 6e 64 20 53 51 4c 49  he lock and SQLI
44860 54 45 5f 4c 4f 43 4b 45 44 20 69 73 20 72 65 74  TE_LOCKED is ret
44870 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c  urned to the cal
44880 6c 65 72 2c 20 74 68 65 0a 2a 2a 20 69 64 65 6e  ler, the.** iden
44890 74 69 74 79 20 6f 66 20 74 68 65 20 64 61 74 61  tity of the data
448a0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
448b0 28 74 68 65 20 62 6c 6f 63 6b 69 6e 67 20 63 6f  (the blocking co
448c0 6e 6e 65 63 74 69 6f 6e 29 20 74 68 61 74 0a 2a  nnection) that.*
448d0 2a 20 68 61 73 20 6c 6f 63 6b 65 64 20 74 68 65  * has locked the
448e0 20 72 65 71 75 69 72 65 64 20 72 65 73 6f 75 72   required resour
448f0 63 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 74  ce is stored int
44900 65 72 6e 61 6c 6c 79 2e 20 41 66 74 65 72 20 61  ernally. After a
44910 6e 20 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f  n .** applicatio
44920 6e 20 72 65 63 65 69 76 65 73 20 61 6e 20 53 51  n receives an SQ
44930 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 65 72 72 6f  LITE_LOCKED erro
44940 72 2c 20 69 74 20 6d 61 79 20 63 61 6c 6c 20 74  r, it may call t
44950 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 75 6e  he.** sqlite3_un
44960 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20 6d 65  lock_notify() me
44970 74 68 6f 64 20 77 69 74 68 20 74 68 65 20 62 6c  thod with the bl
44980 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e  ocked connection
44990 20 68 61 6e 64 6c 65 20 61 73 20 0a 2a 2a 20 74   handle as .** t
449a0 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
449b0 74 20 74 6f 20 72 65 67 69 73 74 65 72 20 66 6f  t to register fo
449c0 72 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 68 61  r a callback tha
449d0 74 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b 65  t will be invoke
449e0 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 62 6c  d.** when the bl
449f0 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f  ocking connectio
44a00 6e 73 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73  ns current trans
44a10 61 63 74 69 6f 6e 20 69 73 20 63 6f 6e 63 6c 75  action is conclu
44a20 64 65 64 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c  ded. The.** call
44a30 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20  back is invoked 
44a40 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20  from within the 
44a50 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 5d 20 6f  [sqlite3_step] o
44a60 72 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  r [sqlite3_close
44a70 5d 0a 2a 2a 20 63 61 6c 6c 20 74 68 61 74 20 63  ].** call that c
44a80 6f 6e 63 6c 75 64 65 73 20 74 68 65 20 62 6c 6f  oncludes the blo
44a90 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e  cking connection
44aa0 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a  s transaction..*
44ab0 2a 0a 2a 2a 20 49 66 20 73 71 6c 69 74 65 33 5f  *.** If sqlite3_
44ac0 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20  unlock_notify() 
44ad0 69 73 20 63 61 6c 6c 65 64 20 69 6e 20 61 20 6d  is called in a m
44ae0 75 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70  ulti-threaded ap
44af0 70 6c 69 63 61 74 69 6f 6e 2c 0a 2a 2a 20 74 68  plication,.** th
44b00 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 65 20  ere is a chance 
44b10 74 68 61 74 20 74 68 65 20 62 6c 6f 63 6b 69 6e  that the blockin
44b20 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 6c  g connection wil
44b30 6c 20 68 61 76 65 20 61 6c 72 65 61 64 79 0a 2a  l have already.*
44b40 2a 20 63 6f 6e 63 6c 75 64 65 64 20 69 74 73 20  * concluded its 
44b50 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 79 20 74  transaction by t
44b60 68 65 20 74 69 6d 65 20 73 71 6c 69 74 65 33 5f  he time sqlite3_
44b70 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 29 20  unlock_notify() 
44b80 69 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 49  is invoked..** I
44b90 66 20 74 68 69 73 20 68 61 70 70 65 6e 73 2c 20  f this happens, 
44ba0 74 68 65 6e 20 74 68 65 20 73 70 65 63 69 66 69  then the specifi
44bb0 65 64 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 69  ed callback is i
44bc0 6e 76 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74 65  nvoked immediate
44bd0 6c 79 2c 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68  ly,.** from with
44be0 69 6e 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 73  in the call to s
44bf0 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f  qlite3_unlock_no
44c00 74 69 66 79 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66  tify()..**.** If
44c10 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e   the blocked con
44c20 6e 65 63 74 69 6f 6e 20 69 73 20 61 74 74 65 6d  nection is attem
44c30 70 74 69 6e 67 20 74 6f 20 6f 62 74 61 69 6e 20  pting to obtain 
44c40 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20  a write-lock on 
44c50 61 0a 2a 2a 20 73 68 61 72 65 64 2d 63 61 63 68  a.** shared-cach
44c60 65 20 74 61 62 6c 65 2c 20 61 6e 64 20 6d 6f 72  e table, and mor
44c70 65 20 74 68 61 6e 20 6f 6e 65 20 6f 74 68 65 72  e than one other
44c80 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72 72   connection curr
44c90 65 6e 74 6c 79 20 68 6f 6c 64 73 0a 2a 2a 20 61  ently holds.** a
44ca0 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68   read-lock on th
44cb0 65 20 73 61 6d 65 20 74 61 62 6c 65 2c 20 74 68  e same table, th
44cc0 65 6e 20 53 51 4c 69 74 65 20 61 72 62 69 74 72  en SQLite arbitr
44cd0 61 72 69 6c 79 20 73 65 6c 65 63 74 73 20 6f 6e  arily selects on
44ce0 65 20 6f 66 20 0a 2a 2a 20 74 68 65 20 6f 74 68  e of .** the oth
44cf0 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74  er connections t
44d00 6f 20 75 73 65 20 61 73 20 74 68 65 20 62 6c 6f  o use as the blo
44d10 63 6b 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e  cking connection
44d20 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 61  ..**.** There ma
44d30 79 20 62 65 20 61 74 20 6d 6f 73 74 20 6f 6e 65  y be at most one
44d40 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63   unlock-notify c
44d50 61 6c 6c 62 61 63 6b 20 72 65 67 69 73 74 65 72  allback register
44d60 65 64 20 62 79 20 61 20 0a 2a 2a 20 62 6c 6f 63  ed by a .** bloc
44d70 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  ked connection. 
44d80 49 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63  If sqlite3_unloc
44d90 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 20 63 61  k_notify() is ca
44da0 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a  lled when the.**
44db0 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63 74   blocked connect
44dc0 69 6f 6e 20 61 6c 72 65 61 64 79 20 68 61 73 20  ion already has 
44dd0 61 20 72 65 67 69 73 74 65 72 65 64 20 75 6e 6c  a registered unl
44de0 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
44df0 61 63 6b 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ack,.** then the
44e00 20 6e 65 77 20 63 61 6c 6c 62 61 63 6b 20 72 65   new callback re
44e10 70 6c 61 63 65 73 20 74 68 65 20 6f 6c 64 2e 20  places the old. 
44e20 49 66 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63  If sqlite3_unloc
44e30 6b 5f 6e 6f 74 69 66 79 28 29 20 69 73 0a 2a 2a  k_notify() is.**
44e40 20 63 61 6c 6c 65 64 20 77 69 74 68 20 61 20 4e   called with a N
44e50 55 4c 4c 20 70 6f 69 6e 74 65 72 20 61 73 20 69  ULL pointer as i
44e60 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  ts second argume
44e70 6e 74 2c 20 74 68 65 6e 20 61 6e 79 20 65 78 69  nt, then any exi
44e80 73 74 69 6e 67 0a 2a 2a 20 75 6e 6c 6f 63 6b 2d  sting.** unlock-
44e90 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20  notify callback 
44ea0 69 73 20 63 61 6e 63 65 6c 6c 65 64 2e 20 54 68  is cancelled. Th
44eb0 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e 65 63  e blocked connec
44ec0 74 69 6f 6e 73 20 0a 2a 2a 20 75 6e 6c 6f 63 6b  tions .** unlock
44ed0 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
44ee0 20 6d 61 79 20 61 6c 73 6f 20 62 65 20 63 61 6e   may also be can
44ef0 63 65 6c 65 64 20 62 79 20 63 6c 6f 73 69 6e 67  celed by closing
44f00 20 74 68 65 20 62 6c 6f 63 6b 65 64 0a 2a 2a 20   the blocked.** 
44f10 63 6f 6e 6e 65 63 74 69 6f 6e 20 75 73 69 6e 67  connection using
44f20 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28   [sqlite3_close(
44f30 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 75 6e  )]..**.** The un
44f40 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c  lock-notify call
44f50 62 61 63 6b 20 69 73 20 6e 6f 74 20 72 65 65 6e  back is not reen
44f60 74 72 61 6e 74 2e 20 49 66 20 61 6e 20 61 70 70  trant. If an app
44f70 6c 69 63 61 74 69 6f 6e 20 69 6e 76 6f 6b 65 73  lication invokes
44f80 0a 2a 2a 20 61 6e 79 20 73 71 6c 69 74 65 33 5f  .** any sqlite3_
44f90 78 78 78 20 41 50 49 20 66 75 6e 63 74 69 6f 6e  xxx API function
44fa0 73 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 6e  s from within an
44fb0 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63   unlock-notify c
44fc0 61 6c 6c 62 61 63 6b 2c 20 61 0a 2a 2a 20 63 72  allback, a.** cr
44fd0 61 73 68 20 6f 72 20 64 65 61 64 6c 6f 63 6b 20  ash or deadlock 
44fe0 6d 61 79 20 62 65 20 74 68 65 20 72 65 73 75 6c  may be the resul
44ff0 74 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20  t..**.** Unless 
45000 64 65 61 64 6c 6f 63 6b 20 69 73 20 64 65 74 65  deadlock is dete
45010 63 74 65 64 20 28 73 65 65 20 62 65 6c 6f 77 29  cted (see below)
45020 2c 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b  , sqlite3_unlock
45030 5f 6e 6f 74 69 66 79 28 29 20 61 6c 77 61 79 73  _notify() always
45040 0a 2a 2a 20 72 65 74 75 72 6e 73 20 53 51 4c 49  .** returns SQLI
45050 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 3c 62 3e  TE_OK..**.** <b>
45060 43 61 6c 6c 62 61 63 6b 20 49 6e 76 6f 63 61 74  Callback Invocat
45070 69 6f 6e 20 44 65 74 61 69 6c 73 3c 2f 62 3e 0a  ion Details</b>.
45080 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 6e 20 75 6e  **.** When an un
45090 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c  lock-notify call
450a0 62 61 63 6b 20 69 73 20 72 65 67 69 73 74 65 72  back is register
450b0 65 64 2c 20 74 68 65 20 61 70 70 6c 69 63 61 74  ed, the applicat
450c0 69 6f 6e 20 70 72 6f 76 69 64 65 73 20 61 20 0a  ion provides a .
450d0 2a 2a 20 73 69 6e 67 6c 65 20 76 6f 69 64 2a 20  ** single void* 
450e0 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 20  pointer that is 
450f0 70 61 73 73 65 64 20 74 6f 20 74 68 65 20 63 61  passed to the ca
45100 6c 6c 62 61 63 6b 20 77 68 65 6e 20 69 74 20 69  llback when it i
45110 73 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 48 6f  s invoked..** Ho
45120 77 65 76 65 72 2c 20 74 68 65 20 73 69 67 6e 61  wever, the signa
45130 74 75 72 65 20 6f 66 20 74 68 65 20 63 61 6c 6c  ture of the call
45140 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 61 6c  back function al
45150 6c 6f 77 73 20 53 51 4c 69 74 65 20 74 6f 20 70  lows SQLite to p
45160 61 73 73 0a 2a 2a 20 69 74 20 61 6e 20 61 72 72  ass.** it an arr
45170 61 79 20 6f 66 20 76 6f 69 64 2a 20 63 6f 6e 74  ay of void* cont
45180 65 78 74 20 70 6f 69 6e 74 65 72 73 2e 20 54 68  ext pointers. Th
45190 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
451a0 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 61 6e   passed to.** an
451b0 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63   unlock-notify c
451c0 61 6c 6c 62 61 63 6b 20 69 73 20 61 20 70 6f 69  allback is a poi
451d0 6e 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79  nter to an array
451e0 20 6f 66 20 76 6f 69 64 2a 20 70 6f 69 6e 74 65   of void* pointe
451f0 72 73 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73  rs,.** and the s
45200 65 63 6f 6e 64 20 69 73 20 74 68 65 20 6e 75 6d  econd is the num
45210 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
45220 6e 20 74 68 65 20 61 72 72 61 79 2e 0a 2a 2a 0a  n the array..**.
45230 2a 2a 20 57 68 65 6e 20 61 20 62 6c 6f 63 6b 69  ** When a blocki
45240 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74  ng connections t
45250 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f  ransaction is co
45260 6e 63 6c 75 64 65 64 2c 20 74 68 65 72 65 20 6d  ncluded, there m
45270 61 79 20 62 65 0a 2a 2a 20 6d 6f 72 65 20 74 68  ay be.** more th
45280 61 6e 20 6f 6e 65 20 62 6c 6f 63 6b 65 64 20 63  an one blocked c
45290 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 68  onnection that h
452a0 61 73 20 72 65 67 69 73 74 65 72 65 64 20 66 6f  as registered fo
452b0 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69  r an unlock-noti
452c0 66 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 2e 20  fy.** callback. 
452d0 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73  If two or more s
452e0 75 63 68 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e  uch blocked conn
452f0 65 63 74 69 6f 6e 73 20 68 61 76 65 20 73 70 65  ections have spe
45300 63 69 66 69 65 64 20 74 68 65 0a 2a 2a 20 73 61  cified the.** sa
45310 6d 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  me callback func
45320 74 69 6f 6e 2c 20 74 68 65 6e 20 69 6e 73 74 65  tion, then inste
45330 61 64 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 74  ad of invoking t
45340 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  he callback func
45350 74 69 6f 6e 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65  tion.** multiple
45360 20 74 69 6d 65 73 2c 20 69 74 20 69 73 20 69 6e   times, it is in
45370 76 6f 6b 65 64 20 6f 6e 63 65 20 77 69 74 68 20  voked once with 
45380 74 68 65 20 73 65 74 20 6f 66 20 76 6f 69 64 2a  the set of void*
45390 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72   context pointer
453a0 73 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62  s.** specified b
453b0 79 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f  y the blocked co
453c0 6e 6e 65 63 74 69 6f 6e 73 20 62 75 6e 64 6c 65  nnections bundle
453d0 64 20 74 6f 67 65 74 68 65 72 20 69 6e 74 6f 20  d together into 
453e0 61 6e 20 61 72 72 61 79 2e 0a 2a 2a 20 54 68 69  an array..** Thi
453f0 73 20 67 69 76 65 73 20 74 68 65 20 61 70 70 6c  s gives the appl
45400 69 63 61 74 69 6f 6e 20 61 6e 20 6f 70 70 6f 72  ication an oppor
45410 74 75 6e 69 74 79 20 74 6f 20 70 72 69 6f 72 69  tunity to priori
45420 74 69 7a 65 20 61 6e 79 20 61 63 74 69 6f 6e 73  tize any actions
45430 20 0a 2a 2a 20 72 65 6c 61 74 65 64 20 74 6f 20   .** related to 
45440 74 68 65 20 73 65 74 20 6f 66 20 75 6e 62 6c 6f  the set of unblo
45450 63 6b 65 64 20 64 61 74 61 62 61 73 65 20 63 6f  cked database co
45460 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  nnections..**.**
45470 20 3c 62 3e 44 65 61 64 6c 6f 63 6b 20 44 65 74   <b>Deadlock Det
45480 65 63 74 69 6f 6e 3c 2f 62 3e 0a 2a 2a 0a 2a 2a  ection</b>.**.**
45490 20 41 73 73 75 6d 69 6e 67 20 74 68 61 74 20 61   Assuming that a
454a0 66 74 65 72 20 72 65 67 69 73 74 65 72 69 6e 67  fter registering
454b0 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e   for an unlock-n
454c0 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 61  otify callback a
454d0 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 77 61   .** database wa
454e0 69 74 73 20 66 6f 72 20 74 68 65 20 63 61 6c 6c  its for the call
454f0 62 61 63 6b 20 74 6f 20 62 65 20 69 73 73 75 65  back to be issue
45500 64 20 62 65 66 6f 72 65 20 74 61 6b 69 6e 67 20  d before taking 
45510 61 6e 79 20 66 75 72 74 68 65 72 0a 2a 2a 20 61  any further.** a
45520 63 74 69 6f 6e 20 28 61 20 72 65 61 73 6f 6e 61  ction (a reasona
45530 62 6c 65 20 61 73 73 75 6d 70 74 69 6f 6e 29 2c  ble assumption),
45540 20 74 68 65 6e 20 75 73 69 6e 67 20 74 68 69 73   then using this
45550 20 41 50 49 20 6d 61 79 20 63 61 75 73 65 20 74   API may cause t
45560 68 65 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f  he.** applicatio
45570 6e 20 74 6f 20 64 65 61 64 6c 6f 63 6b 2e 20 46  n to deadlock. F
45580 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 63  or example, if c
45590 6f 6e 6e 65 63 74 69 6f 6e 20 58 20 69 73 20 77  onnection X is w
455a0 61 69 74 69 6e 67 20 66 6f 72 0a 2a 2a 20 63 6f  aiting for.** co
455b0 6e 6e 65 63 74 69 6f 6e 20 59 27 73 20 74 72 61  nnection Y's tra
455c0 6e 73 61 63 74 69 6f 6e 20 74 6f 20 62 65 20 63  nsaction to be c
455d0 6f 6e 63 6c 75 64 65 64 2c 20 61 6e 64 20 73 69  oncluded, and si
455e0 6d 69 6c 61 72 6c 79 20 63 6f 6e 6e 65 63 74 69  milarly connecti
455f0 6f 6e 0a 2a 2a 20 59 20 69 73 20 77 61 69 74 69  on.** Y is waiti
45600 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e  ng on connection
45610 20 58 27 73 20 74 72 61 6e 73 61 63 74 69 6f 6e   X's transaction
45620 2c 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20 63  , then neither c
45630 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 77 69 6c  onnection.** wil
45640 6c 20 70 72 6f 63 65 65 64 20 61 6e 64 20 74 68  l proceed and th
45650 65 20 73 79 73 74 65 6d 20 6d 61 79 20 72 65 6d  e system may rem
45660 61 69 6e 20 64 65 61 64 6c 6f 63 6b 65 64 20 69  ain deadlocked i
45670 6e 64 65 66 69 6e 69 74 65 6c 79 2e 0a 2a 2a 0a  ndefinitely..**.
45680 2a 2a 20 54 6f 20 61 76 6f 69 64 20 74 68 69 73  ** To avoid this
45690 20 73 63 65 6e 61 72 69 6f 2c 20 74 68 65 20 73   scenario, the s
456a0 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f  qlite3_unlock_no
456b0 74 69 66 79 28 29 20 70 65 72 66 6f 72 6d 73 20  tify() performs 
456c0 64 65 61 64 6c 6f 63 6b 0a 2a 2a 20 64 65 74 65  deadlock.** dete
456d0 63 74 69 6f 6e 2e 20 49 66 20 61 20 67 69 76 65  ction. If a give
456e0 6e 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  n call to sqlite
456f0 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28  3_unlock_notify(
45700 29 20 77 6f 75 6c 64 20 70 75 74 20 74 68 65 0a  ) would put the.
45710 2a 2a 20 73 79 73 74 65 6d 20 69 6e 20 61 20 64  ** system in a d
45720 65 61 64 6c 6f 63 6b 65 64 20 73 74 61 74 65 2c  eadlocked state,
45730 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 4c 4f 43   then SQLITE_LOC
45740 4b 45 44 20 69 73 20 72 65 74 75 72 6e 65 64 20  KED is returned 
45750 61 6e 64 20 6e 6f 0a 2a 2a 20 75 6e 6c 6f 63 6b  and no.** unlock
45760 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
45770 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2e 20   is registered. 
45780 54 68 65 20 73 79 73 74 65 6d 20 69 73 20 73 61  The system is sa
45790 69 64 20 74 6f 20 62 65 20 69 6e 0a 2a 2a 20 61  id to be in.** a
457a0 20 64 65 61 64 6c 6f 63 6b 65 64 20 73 74 61 74   deadlocked stat
457b0 65 20 69 66 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  e if connection 
457c0 41 20 68 61 73 20 72 65 67 69 73 74 65 72 65 64  A has registered
457d0 20 66 6f 72 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e   for an unlock-n
457e0 6f 74 69 66 79 0a 2a 2a 20 63 61 6c 6c 62 61 63  otify.** callbac
457f0 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63 6c 75 73  k on the conclus
45800 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63 74 69 6f  ion of connectio
45810 6e 20 42 27 73 20 74 72 61 6e 73 61 63 74 69 6f  n B's transactio
45820 6e 2c 20 61 6e 64 20 63 6f 6e 6e 65 63 74 69 6f  n, and connectio
45830 6e 0a 2a 2a 20 42 20 68 61 73 20 69 74 73 65 6c  n.** B has itsel
45840 66 20 72 65 67 69 73 74 65 72 65 64 20 66 6f 72  f registered for
45850 20 61 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66   an unlock-notif
45860 79 20 63 61 6c 6c 62 61 63 6b 20 77 68 65 6e 20  y callback when 
45870 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 41 27  connection.** A'
45880 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  s transaction is
45890 20 63 6f 6e 63 6c 75 64 65 64 2e 20 49 6e 64 69   concluded. Indi
458a0 72 65 63 74 20 64 65 61 64 6c 6f 63 6b 20 69 73  rect deadlock is
458b0 20 61 6c 73 6f 20 64 65 74 65 63 74 65 64 2c 20   also detected, 
458c0 73 6f 0a 2a 2a 20 74 68 65 20 73 79 73 74 65 6d  so.** the system
458d0 20 69 73 20 61 6c 73 6f 20 63 6f 6e 73 69 64 65   is also conside
458e0 72 65 64 20 74 6f 20 62 65 20 64 65 61 64 6c 6f  red to be deadlo
458f0 63 6b 65 64 20 69 66 20 63 6f 6e 6e 65 63 74 69  cked if connecti
45900 6f 6e 20 42 20 68 61 73 0a 2a 2a 20 72 65 67 69  on B has.** regi
45910 73 74 65 72 65 64 20 66 6f 72 20 61 6e 20 75 6e  stered for an un
45920 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c  lock-notify call
45930 62 61 63 6b 20 6f 6e 20 74 68 65 20 63 6f 6e 63  back on the conc
45940 6c 75 73 69 6f 6e 20 6f 66 20 63 6f 6e 6e 65 63  lusion of connec
45950 74 69 6f 6e 0a 2a 2a 20 43 27 73 20 74 72 61 6e  tion.** C's tran
45960 73 61 63 74 69 6f 6e 2c 20 77 68 65 72 65 20 63  saction, where c
45970 6f 6e 6e 65 63 74 69 6f 6e 20 43 20 69 73 20 77  onnection C is w
45980 61 69 74 69 6e 67 20 6f 6e 20 63 6f 6e 6e 65 63  aiting on connec
45990 74 69 6f 6e 20 41 2e 20 41 6e 79 0a 2a 2a 20 6e  tion A. Any.** n
459a0 75 6d 62 65 72 20 6f 66 20 6c 65 76 65 6c 73 20  umber of levels 
459b0 6f 66 20 69 6e 64 69 72 65 63 74 69 6f 6e 20 61  of indirection a
459c0 72 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a 2a 0a 2a  re allowed..**.*
459d0 2a 20 3c 62 3e 54 68 65 20 22 44 52 4f 50 20 54  * <b>The "DROP T
459e0 41 42 4c 45 22 20 45 78 63 65 70 74 69 6f 6e 3c  ABLE" Exception<
459f0 2f 62 3e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61  /b>.**.** When a
45a00 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
45a10 33 5f 73 74 65 70 28 29 5d 20 72 65 74 75 72 6e  3_step()] return
45a20 73 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2c  s SQLITE_LOCKED,
45a30 20 69 74 20 69 73 20 61 6c 6d 6f 73 74 20 0a 2a   it is almost .*
45a40 2a 20 61 6c 77 61 79 73 20 61 70 70 72 6f 70 72  * always appropr
45a50 69 61 74 65 20 74 6f 20 63 61 6c 6c 20 73 71 6c  iate to call sql
45a60 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69  ite3_unlock_noti
45a70 66 79 28 29 2e 20 54 68 65 72 65 20 69 73 20 68  fy(). There is h
45a80 6f 77 65 76 65 72 2c 0a 2a 2a 20 6f 6e 65 20 65  owever,.** one e
45a90 78 63 65 70 74 69 6f 6e 2e 20 57 68 65 6e 20 65  xception. When e
45aa0 78 65 63 75 74 69 6e 67 20 61 20 22 44 52 4f 50  xecuting a "DROP
45ab0 20 54 41 42 4c 45 22 20 6f 72 20 22 44 52 4f 50   TABLE" or "DROP
45ac0 20 49 4e 44 45 58 22 20 73 74 61 74 65 6d 65 6e   INDEX" statemen
45ad0 74 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 63 68 65  t,.** SQLite che
45ae0 63 6b 73 20 69 66 20 74 68 65 72 65 20 61 72 65  cks if there are
45af0 20 61 6e 79 20 63 75 72 72 65 6e 74 6c 79 20 65   any currently e
45b00 78 65 63 75 74 69 6e 67 20 53 45 4c 45 43 54 20  xecuting SELECT 
45b10 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68  statements.** th
45b20 61 74 20 62 65 6c 6f 6e 67 20 74 6f 20 74 68 65  at belong to the
45b30 20 73 61 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e   same connection
45b40 2e 20 49 66 20 74 68 65 72 65 20 61 72 65 2c 20  . If there are, 
45b50 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 73  SQLITE_LOCKED is
45b60 0a 2a 2a 20 72 65 74 75 72 6e 65 64 2e 20 49 6e  .** returned. In
45b70 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65   this case there
45b80 20 69 73 20 6e 6f 20 22 62 6c 6f 63 6b 69 6e 67   is no "blocking
45b90 20 63 6f 6e 6e 65 63 74 69 6f 6e 22 2c 20 73 6f   connection", so
45ba0 20 69 6e 76 6f 6b 69 6e 67 0a 2a 2a 20 73 71 6c   invoking.** sql
45bb0 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74 69  ite3_unlock_noti
45bc0 66 79 28 29 20 72 65 73 75 6c 74 73 20 69 6e 20  fy() results in 
45bd0 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66  the unlock-notif
45be0 79 20 63 61 6c 6c 62 61 63 6b 20 62 65 69 6e 67  y callback being
45bf0 0a 2a 2a 20 69 6e 76 6f 6b 65 64 20 69 6d 6d 65  .** invoked imme
45c00 64 69 61 74 65 6c 79 2e 20 49 66 20 74 68 65 20  diately. If the 
45c10 61 70 70 6c 69 63 61 74 69 6f 6e 20 74 68 65 6e  application then
45c20 20 72 65 2d 61 74 74 65 6d 70 74 73 20 74 68 65   re-attempts the
45c30 20 22 44 52 4f 50 20 54 41 42 4c 45 22 0a 2a 2a   "DROP TABLE".**
45c40 20 6f 72 20 22 44 52 4f 50 20 49 4e 44 45 58 22   or "DROP INDEX"
45c50 20 71 75 65 72 79 2c 20 61 6e 20 69 6e 66 69 6e   query, an infin
45c60 69 74 65 20 6c 6f 6f 70 20 6d 69 67 68 74 20 62  ite loop might b
45c70 65 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a  e the result..**
45c80 0a 2a 2a 20 4f 6e 65 20 77 61 79 20 61 72 6f 75  .** One way arou
45c90 6e 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20  nd this problem 
45ca0 69 73 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20  is to check the 
45cb0 65 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63  extended error c
45cc0 6f 64 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20  ode returned.** 
45cd0 62 79 20 61 6e 20 73 71 6c 69 74 65 33 5f 73 74  by an sqlite3_st
45ce0 65 70 28 29 20 63 61 6c 6c 2e 20 49 66 20 74 68  ep() call. If th
45cf0 65 72 65 20 69 73 20 61 20 62 6c 6f 63 6b 69 6e  ere is a blockin
45d00 67 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68  g connection, th
45d10 65 6e 20 74 68 65 0a 2a 2a 20 65 78 74 65 6e 64  en the.** extend
45d20 65 64 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  ed error code is
45d30 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4c   set to SQLITE_L
45d40 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48  OCKED_SHAREDCACH
45d50 45 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 6e  E. Otherwise, in
45d60 0a 2a 2a 20 74 68 65 20 73 70 65 63 69 61 6c 20  .** the special 
45d70 22 44 52 4f 50 20 54 41 42 4c 45 2f 49 4e 44 45  "DROP TABLE/INDE
45d80 58 22 20 63 61 73 65 2c 20 74 68 65 20 65 78 74  X" case, the ext
45d90 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65  ended error code
45da0 20 69 73 20 6a 75 73 74 20 0a 2a 2a 20 53 51 4c   is just .** SQL
45db0 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 53  ITE_LOCKED..*/.S
45dc0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
45dd0 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74  lite3_unlock_not
45de0 69 66 79 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ify(.  sqlite3 *
45df0 70 42 6c 6f 63 6b 65 64 2c 20 20 20 20 20 20 20  pBlocked,       
45e00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45e10 20 20 20 2f 2a 20 57 61 69 74 69 6e 67 20 63 6f     /* Waiting co
45e20 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f  nnection */.  vo
45e30 69 64 20 28 2a 78 4e 6f 74 69 66 79 29 28 76 6f  id (*xNotify)(vo
45e40 69 64 20 2a 2a 61 70 41 72 67 2c 20 69 6e 74 20  id **apArg, int 
45e50 6e 41 72 67 29 2c 20 20 20 20 2f 2a 20 43 61 6c  nArg),    /* Cal
45e60 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 74  lback function t
45e70 6f 20 69 6e 76 6f 6b 65 20 2a 2f 0a 20 20 76 6f  o invoke */.  vo
45e80 69 64 20 2a 70 4e 6f 74 69 66 79 41 72 67 20 20  id *pNotifyArg  
45e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
45ea0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67            /* Arg
45eb0 75 6d 65 6e 74 20 74 6f 20 70 61 73 73 20 74 6f  ument to pass to
45ec0 20 78 4e 6f 74 69 66 79 20 2a 2f 0a 29 3b 0a 0a   xNotify */.);..
45ed0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
45ee0 20 53 74 72 69 6e 67 20 43 6f 6d 70 61 72 69 73   String Comparis
45ef0 6f 6e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  on.** EXPERIMENT
45f00 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71  AL.**.** The [sq
45f10 6c 69 74 65 33 5f 73 74 72 6e 69 63 6d 70 28 29  lite3_strnicmp()
45f20 5d 20 41 50 49 20 61 6c 6c 6f 77 73 20 61 70 70  ] API allows app
45f30 6c 69 63 61 74 69 6f 6e 73 20 61 6e 64 20 65 78  lications and ex
45f40 74 65 6e 73 69 6f 6e 73 20 74 6f 0a 2a 2a 20 63  tensions to.** c
45f50 6f 6d 70 61 72 65 20 74 68 65 20 63 6f 6e 74 65  ompare the conte
45f60 6e 74 73 20 6f 66 20 74 77 6f 20 62 75 66 66 65  nts of two buffe
45f70 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 55 54  rs containing UT
45f80 46 2d 38 20 73 74 72 69 6e 67 73 20 69 6e 20 61  F-8 strings in a
45f90 0a 2a 2a 20 63 61 73 65 2d 69 6e 64 65 6e 64 65  .** case-indende
45fa0 6e 74 20 66 61 73 68 69 6f 6e 2c 20 75 73 69 6e  nt fashion, usin
45fb0 67 20 74 68 65 20 73 61 6d 65 20 64 65 66 69 6e  g the same defin
45fc0 69 74 69 6f 6e 20 6f 66 20 63 61 73 65 20 69 6e  ition of case in
45fd0 64 65 70 65 6e 64 65 6e 63 65 20 0a 2a 2a 20 74  dependence .** t
45fe0 68 61 74 20 53 51 4c 69 74 65 20 75 73 65 73 20  hat SQLite uses 
45ff0 69 6e 74 65 72 6e 61 6c 6c 79 20 77 68 65 6e 20  internally when 
46000 63 6f 6d 70 61 72 69 6e 67 20 69 64 65 6e 74 69  comparing identi
46010 66 69 65 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  fiers..*/.SQLITE
46020 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
46030 5f 73 74 72 6e 69 63 6d 70 28 63 6f 6e 73 74 20  _strnicmp(const 
46040 63 68 61 72 20 2a 2c 20 63 6f 6e 73 74 20 63 68  char *, const ch
46050 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a  ar *, int);../*.
46060 2a 2a 20 55 6e 64 6f 20 74 68 65 20 68 61 63 6b  ** Undo the hack
46070 20 74 68 61 74 20 63 6f 6e 76 65 72 74 73 20 66   that converts f
46080 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 74 79  loating point ty
46090 70 65 73 20 74 6f 20 69 6e 74 65 67 65 72 20 66  pes to integer f
460a0 6f 72 0a 2a 2a 20 62 75 69 6c 64 73 20 6f 6e 20  or.** builds on 
460b0 70 72 6f 63 65 73 73 6f 72 73 20 77 69 74 68 6f  processors witho
460c0 75 74 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  ut floating poin
460d0 74 20 73 75 70 70 6f 72 74 2e 0a 2a 2f 0a 23 69  t support..*/.#i
460e0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
460f0 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
46100 23 20 75 6e 64 65 66 20 64 6f 75 62 6c 65 0a 23  # undef double.#
46110 65 6e 64 69 66 0a 0a 23 69 66 20 30 0a 7d 20 20  endif..#if 0.}  
46120 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20 27 65  /* End of the 'e
46130 78 74 65 72 6e 20 22 43 22 27 20 62 6c 6f 63 6b  xtern "C"' block
46140 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69   */.#endif.#endi
46150 66 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f.../***********
46160 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74  *** End of sqlit
46170 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e3.h ***********
46180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
461a0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
461b0 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
461c0 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
461d0 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   in sqliteInt.h 
461e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
461f0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
46200 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 61 73 68  *** Include hash
46210 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
46220 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   of sqliteInt.h 
46230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46240 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
46250 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68  *** Begin file h
46260 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ash.h **********
46270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46290 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
462a0 65 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a  eptember 22.**.*
462b0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
462c0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
462d0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
462e0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
462f0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
46300 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
46310 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
46320 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
46330 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
46340 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
46350 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
46360 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
46370 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
46380 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
46390 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
463a0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
463b0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
463c0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
463d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
463e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
463f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
46400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
46410 69 73 20 69 73 20 74 68 65 20 68 65 61 64 65 72  is is the header
46420 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 67 65   file for the ge
46430 6e 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c 65  neric hash-table
46440 20 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 0a 2a   implemenation.*
46450 2a 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65  * used in SQLite
46460 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 61 73  ..**.** $Id: has
46470 68 2e 68 2c 76 20 31 2e 31 35 20 32 30 30 39 2f  h.h,v 1.15 2009/
46480 30 35 2f 30 32 20 31 33 3a 32 39 3a 33 38 20 64  05/02 13:29:38 d
46490 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e  rh Exp $.*/.#ifn
464a0 64 65 66 20 5f 53 51 4c 49 54 45 5f 48 41 53 48  def _SQLITE_HASH
464b0 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c  _H_.#define _SQL
464c0 49 54 45 5f 48 41 53 48 5f 48 5f 0a 0a 2f 2a 20  ITE_HASH_H_../* 
464d0 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74  Forward declarat
464e0 69 6f 6e 73 20 6f 66 20 73 74 72 75 63 74 75 72  ions of structur
464f0 65 73 2e 20 2a 2f 0a 74 79 70 65 64 65 66 20 73  es. */.typedef s
46500 74 72 75 63 74 20 48 61 73 68 20 48 61 73 68 3b  truct Hash Hash;
46510 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
46520 48 61 73 68 45 6c 65 6d 20 48 61 73 68 45 6c 65  HashElem HashEle
46530 6d 3b 0a 0a 2f 2a 20 41 20 63 6f 6d 70 6c 65 74  m;../* A complet
46540 65 20 68 61 73 68 20 74 61 62 6c 65 20 69 73 20  e hash table is 
46550 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
46560 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
46570 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20 69  ucture..** The i
46580 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74 68 69 73  nternals of this
46590 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 69   structure are i
465a0 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70  ntended to be op
465b0 61 71 75 65 20 2d 2d 20 63 6c 69 65 6e 74 0a 2a  aque -- client.*
465c0 2a 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e 6f  * code should no
465d0 74 20 61 74 74 65 6d 70 74 20 74 6f 20 61 63 63  t attempt to acc
465e0 65 73 73 20 6f 72 20 6d 6f 64 69 66 79 20 74 68  ess or modify th
465f0 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69 73  e fields of this
46600 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 64 69   structure.** di
46610 72 65 63 74 6c 79 2e 20 20 43 68 61 6e 67 65 20  rectly.  Change 
46620 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 6f  this structure o
46630 6e 6c 79 20 62 79 20 75 73 69 6e 67 20 74 68 65  nly by using the
46640 20 72 6f 75 74 69 6e 65 73 20 62 65 6c 6f 77 2e   routines below.
46650 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 73 6f 6d  .** However, som
46660 65 20 6f 66 20 74 68 65 20 22 70 72 6f 63 65 64  e of the "proced
46670 75 72 65 73 22 20 61 6e 64 20 22 66 75 6e 63 74  ures" and "funct
46680 69 6f 6e 73 22 20 66 6f 72 20 6d 6f 64 69 66 79  ions" for modify
46690 69 6e 67 20 61 6e 64 0a 2a 2a 20 61 63 63 65 73  ing and.** acces
466a0 73 69 6e 67 20 74 68 69 73 20 73 74 72 75 63 74  sing this struct
466b0 75 72 65 20 61 72 65 20 72 65 61 6c 6c 79 20 6d  ure are really m
466c0 61 63 72 6f 73 2c 20 73 6f 20 77 65 20 63 61 6e  acros, so we can
466d0 27 74 20 72 65 61 6c 6c 79 20 6d 61 6b 65 0a 2a  't really make.*
466e0 2a 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  * this structure
466f0 20 6f 70 61 71 75 65 2e 0a 2a 2a 0a 2a 2a 20 41   opaque..**.** A
46700 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74  ll elements of t
46710 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 61 72  he hash table ar
46720 65 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f  e on a single do
46730 75 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74  ubly-linked list
46740 2e 0a 2a 2a 20 48 61 73 68 2e 66 69 72 73 74 20  ..** Hash.first 
46750 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 68 65  points to the he
46760 61 64 20 6f 66 20 74 68 69 73 20 6c 69 73 74 2e  ad of this list.
46770 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  .**.** There are
46780 20 48 61 73 68 2e 68 74 73 69 7a 65 20 62 75 63   Hash.htsize buc
46790 6b 65 74 73 2e 20 20 45 61 63 68 20 62 75 63 6b  kets.  Each buck
467a0 65 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 73  et points to a s
467b0 70 6f 74 20 69 6e 0a 2a 2a 20 74 68 65 20 67 6c  pot in.** the gl
467c0 6f 62 61 6c 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b  obal doubly-link
467d0 65 64 20 6c 69 73 74 2e 20 20 54 68 65 20 63 6f  ed list.  The co
467e0 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 62 75  ntents of the bu
467f0 63 6b 65 74 20 61 72 65 20 74 68 65 0a 2a 2a 20  cket are the.** 
46800 65 6c 65 6d 65 6e 74 20 70 6f 69 6e 74 65 64 20  element pointed 
46810 74 6f 20 70 6c 75 73 20 74 68 65 20 6e 65 78 74  to plus the next
46820 20 5f 68 74 2e 63 6f 75 6e 74 2d 31 20 65 6c 65   _ht.count-1 ele
46830 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 6c 69 73  ments in the lis
46840 74 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68 2e 68 74  t..**.** Hash.ht
46850 73 69 7a 65 20 61 6e 64 20 48 61 73 68 2e 68 74  size and Hash.ht
46860 20 6d 61 79 20 62 65 20 7a 65 72 6f 2e 20 20 49   may be zero.  I
46870 6e 20 74 68 61 74 20 63 61 73 65 20 6c 6f 6f 6b  n that case look
46880 75 70 20 69 73 20 64 6f 6e 65 0a 2a 2a 20 62 79  up is done.** by
46890 20 61 20 6c 69 6e 65 61 72 20 73 65 61 72 63 68   a linear search
468a0 20 6f 66 20 74 68 65 20 67 6c 6f 62 61 6c 20 6c   of the global l
468b0 69 73 74 2e 20 20 46 6f 72 20 73 6d 61 6c 6c 20  ist.  For small 
468c0 74 61 62 6c 65 73 2c 20 74 68 65 20 0a 2a 2a 20  tables, the .** 
468d0 48 61 73 68 2e 68 74 20 74 61 62 6c 65 20 69 73  Hash.ht table is
468e0 20 6e 65 76 65 72 20 61 6c 6c 6f 63 61 74 65 64   never allocated
468f0 20 62 65 63 61 75 73 65 20 69 66 20 74 68 65 72   because if ther
46900 65 20 61 72 65 20 66 65 77 20 65 6c 65 6d 65 6e  e are few elemen
46910 74 73 0a 2a 2a 20 69 6e 20 74 68 65 20 74 61 62  ts.** in the tab
46920 6c 65 2c 20 69 74 20 69 73 20 66 61 73 74 65 72  le, it is faster
46930 20 74 6f 20 64 6f 20 61 20 6c 69 6e 65 61 72 20   to do a linear 
46940 73 65 61 72 63 68 20 74 68 61 6e 20 74 6f 20 6d  search than to m
46950 61 6e 61 67 65 0a 2a 2a 20 74 68 65 20 68 61 73  anage.** the has
46960 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75  h table..*/.stru
46970 63 74 20 48 61 73 68 20 7b 0a 20 20 75 6e 73 69  ct Hash {.  unsi
46980 67 6e 65 64 20 69 6e 74 20 68 74 73 69 7a 65 3b  gned int htsize;
46990 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
469a0 6f 66 20 62 75 63 6b 65 74 73 20 69 6e 20 74 68  of buckets in th
469b0 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a  e hash table */.
469c0 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63    unsigned int c
469d0 6f 75 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 4e  ount;       /* N
469e0 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
469f0 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a   in this table *
46a00 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 66 69  /.  HashElem *fi
46a10 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  rst;          /*
46a20 20 54 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65   The first eleme
46a30 6e 74 20 6f 66 20 74 68 65 20 61 72 72 61 79 20  nt of the array 
46a40 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20  */.  struct _ht 
46a50 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  {              /
46a60 2a 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  * the hash table
46a70 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 6f 75 6e   */.    int coun
46a80 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
46a90 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
46aa0 65 6e 74 72 69 65 73 20 77 69 74 68 20 74 68 69  entries with thi
46ab0 73 20 68 61 73 68 20 2a 2f 0a 20 20 20 20 48 61  s hash */.    Ha
46ac0 73 68 45 6c 65 6d 20 2a 63 68 61 69 6e 3b 20 20  shElem *chain;  
46ad0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
46ae0 74 65 72 20 74 6f 20 66 69 72 73 74 20 65 6e 74  ter to first ent
46af0 72 79 20 77 69 74 68 20 74 68 69 73 20 68 61 73  ry with this has
46b00 68 20 2a 2f 0a 20 20 7d 20 2a 68 74 3b 0a 7d 3b  h */.  } *ht;.};
46b10 0a 0a 2f 2a 20 45 61 63 68 20 65 6c 65 6d 65 6e  ../* Each elemen
46b20 74 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61  t in the hash ta
46b30 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  ble is an instan
46b40 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
46b50 69 6e 67 20 0a 2a 2a 20 73 74 72 75 63 74 75 72  ing .** structur
46b60 65 2e 20 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73  e.  All elements
46b70 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 61   are stored on a
46b80 20 73 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d 6c   single doubly-l
46b90 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a  inked list..**.*
46ba0 2a 20 41 67 61 69 6e 2c 20 74 68 69 73 20 73 74  * Again, this st
46bb0 72 75 63 74 75 72 65 20 69 73 20 69 6e 74 65 6e  ructure is inten
46bc0 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65  ded to be opaque
46bd0 2c 20 62 75 74 20 69 74 20 63 61 6e 27 74 20 72  , but it can't r
46be0 65 61 6c 6c 79 0a 2a 2a 20 62 65 20 6f 70 61 71  eally.** be opaq
46bf0 75 65 20 62 65 63 61 75 73 65 20 69 74 20 69 73  ue because it is
46c00 20 75 73 65 64 20 62 79 20 6d 61 63 72 6f 73 2e   used by macros.
46c10 0a 2a 2f 0a 73 74 72 75 63 74 20 48 61 73 68 45  .*/.struct HashE
46c20 6c 65 6d 20 7b 0a 20 20 48 61 73 68 45 6c 65 6d  lem {.  HashElem
46c30 20 2a 6e 65 78 74 2c 20 2a 70 72 65 76 3b 20 20   *next, *prev;  
46c40 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 61 6e 64       /* Next and
46c50 20 70 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e   previous elemen
46c60 74 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ts in the table 
46c70 2a 2f 0a 20 20 76 6f 69 64 20 2a 64 61 74 61 3b  */.  void *data;
46c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
46c90 20 20 2f 2a 20 44 61 74 61 20 61 73 73 6f 63 69    /* Data associ
46ca0 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 65  ated with this e
46cb0 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73  lement */.  cons
46cc0 74 20 63 68 61 72 20 2a 70 4b 65 79 3b 20 69 6e  t char *pKey; in
46cd0 74 20 6e 4b 65 79 3b 20 20 2f 2a 20 4b 65 79 20  t nKey;  /* Key 
46ce0 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
46cf0 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a  this element */.
46d00 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73  };../*.** Access
46d10 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 6f 20 64   routines.  To d
46d20 65 6c 65 74 65 2c 20 69 6e 73 65 72 74 20 61 20  elete, insert a 
46d30 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f  NULL pointer..*/
46d40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
46d50 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68  void sqlite3Hash
46d60 49 6e 69 74 28 48 61 73 68 2a 29 3b 0a 53 51 4c  Init(Hash*);.SQL
46d70 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
46d80 20 2a 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73   *sqlite3HashIns
46d90 65 72 74 28 48 61 73 68 2a 2c 20 63 6f 6e 73 74  ert(Hash*, const
46da0 20 63 68 61 72 20 2a 70 4b 65 79 2c 20 69 6e 74   char *pKey, int
46db0 20 6e 4b 65 79 2c 20 76 6f 69 64 20 2a 70 44 61   nKey, void *pDa
46dc0 74 61 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ta);.SQLITE_PRIV
46dd0 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
46de0 33 48 61 73 68 46 69 6e 64 28 63 6f 6e 73 74 20  3HashFind(const 
46df0 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 63 68 61  Hash*, const cha
46e00 72 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65  r *pKey, int nKe
46e10 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  y);.SQLITE_PRIVA
46e20 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48  TE void sqlite3H
46e30 61 73 68 43 6c 65 61 72 28 48 61 73 68 2a 29 3b  ashClear(Hash*);
46e40 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66  ../*.** Macros f
46e50 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
46e60 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20  all elements of 
46e70 61 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54  a hash table.  T
46e80 68 65 20 69 64 69 6f 6d 20 69 73 0a 2a 2a 20 6c  he idiom is.** l
46e90 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
46ea0 20 20 48 61 73 68 20 68 3b 0a 2a 2a 20 20 20 48    Hash h;.**   H
46eb0 61 73 68 45 6c 65 6d 20 2a 70 3b 0a 2a 2a 20 20  ashElem *p;.**  
46ec0 20 2e 2e 2e 0a 2a 2a 20 20 20 66 6f 72 28 70 3d   ....**   for(p=
46ed0 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28  sqliteHashFirst(
46ee0 26 68 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65  &h); p; p=sqlite
46ef0 48 61 73 68 4e 65 78 74 28 70 29 29 7b 0a 2a 2a  HashNext(p)){.**
46f00 20 20 20 20 20 53 6f 6d 65 53 74 72 75 63 74 75       SomeStructu
46f10 72 65 20 2a 70 44 61 74 61 20 3d 20 73 71 6c 69  re *pData = sqli
46f20 74 65 48 61 73 68 44 61 74 61 28 70 29 3b 0a 2a  teHashData(p);.*
46f30 2a 20 20 20 20 20 2f 2f 20 64 6f 20 73 6f 6d 65  *     // do some
46f40 74 68 69 6e 67 20 77 69 74 68 20 70 44 61 74 61  thing with pData
46f50 0a 2a 2a 20 20 20 7d 0a 2a 2f 0a 23 64 65 66 69  .**   }.*/.#defi
46f60 6e 65 20 73 71 6c 69 74 65 48 61 73 68 46 69 72  ne sqliteHashFir
46f70 73 74 28 48 29 20 20 28 28 48 29 2d 3e 66 69 72  st(H)  ((H)->fir
46f80 73 74 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  st).#define sqli
46f90 74 65 48 61 73 68 4e 65 78 74 28 45 29 20 20 20  teHashNext(E)   
46fa0 28 28 45 29 2d 3e 6e 65 78 74 29 0a 23 64 65 66  ((E)->next).#def
46fb0 69 6e 65 20 73 71 6c 69 74 65 48 61 73 68 44 61  ine sqliteHashDa
46fc0 74 61 28 45 29 20 20 20 28 28 45 29 2d 3e 64 61  ta(E)   ((E)->da
46fd0 74 61 29 0a 2f 2a 20 23 64 65 66 69 6e 65 20 73  ta)./* #define s
46fe0 71 6c 69 74 65 48 61 73 68 4b 65 79 28 45 29 20  qliteHashKey(E) 
46ff0 20 20 20 28 28 45 29 2d 3e 70 4b 65 79 29 20 2f     ((E)->pKey) /
47000 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f 0a 2f 2a  / NOT USED */./*
47010 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48   #define sqliteH
47020 61 73 68 4b 65 79 73 69 7a 65 28 45 29 20 28 28  ashKeysize(E) ((
47030 45 29 2d 3e 6e 4b 65 79 29 20 20 2f 2f 20 4e 4f  E)->nKey)  // NO
47040 54 20 55 53 45 44 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  T USED */../*.**
47050 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
47060 65 73 20 69 6e 20 61 20 68 61 73 68 20 74 61 62  es in a hash tab
47070 6c 65 0a 2a 2f 0a 2f 2a 20 23 64 65 66 69 6e 65  le.*/./* #define
47080 20 73 71 6c 69 74 65 48 61 73 68 43 6f 75 6e 74   sqliteHashCount
47090 28 48 29 20 20 28 28 48 29 2d 3e 63 6f 75 6e 74  (H)  ((H)->count
470a0 29 20 2f 2f 20 4e 4f 54 20 55 53 45 44 20 2a 2f  ) // NOT USED */
470b0 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c  ..#endif /* _SQL
470c0 49 54 45 5f 48 41 53 48 5f 48 5f 20 2a 2f 0a 0a  ITE_HASH_H_ */..
470d0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
470e0 45 6e 64 20 6f 66 20 68 61 73 68 2e 68 20 2a 2a  End of hash.h **
470f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
47120 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
47130 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
47140 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
47150 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
47160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
47170 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
47180 49 6e 63 6c 75 64 65 20 70 61 72 73 65 2e 68 20  Include parse.h 
47190 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
471a0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
471b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
471c0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
471d0 42 65 67 69 6e 20 66 69 6c 65 20 70 61 72 73 65  Begin file parse
471e0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
471f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
47200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
47210 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 4d 49 20  #define TK_SEMI 
47220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47230 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65             1.#de
47240 66 69 6e 65 20 54 4b 5f 45 58 50 4c 41 49 4e 20  fine TK_EXPLAIN 
47250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47260 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e          2.#defin
47270 65 20 54 4b 5f 51 55 45 52 59 20 20 20 20 20 20  e TK_QUERY      
47280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47290 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 54       3.#define T
472a0 4b 5f 50 4c 41 4e 20 20 20 20 20 20 20 20 20 20  K_PLAN          
472b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
472c0 20 20 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42    4.#define TK_B
472d0 45 47 49 4e 20 20 20 20 20 20 20 20 20 20 20 20  EGIN            
472e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
472f0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 52 41 4e  .#define TK_TRAN
47300 53 41 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20  SACTION         
47310 20 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64              6.#d
47320 65 66 69 6e 65 20 54 4b 5f 44 45 46 45 52 52 45  efine TK_DEFERRE
47330 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  D               
47340 20 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69           7.#defi
47350 6e 65 20 54 4b 5f 49 4d 4d 45 44 49 41 54 45 20  ne TK_IMMEDIATE 
47360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47370 20 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20        8.#define 
47380 54 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20  TK_EXCLUSIVE    
47390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
473a0 20 20 20 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f     9.#define TK_
473b0 43 4f 4d 4d 49 54 20 20 20 20 20 20 20 20 20 20  COMMIT          
473c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
473d0 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4e 44  0.#define TK_END
473e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
473f0 20 20 20 20 20 20 20 20 20 20 20 20 31 31 0a 23              11.#
47400 64 65 66 69 6e 65 20 54 4b 5f 52 4f 4c 4c 42 41  define TK_ROLLBA
47410 43 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CK              
47420 20 20 20 20 20 20 20 20 20 31 32 0a 23 64 65 66           12.#def
47430 69 6e 65 20 54 4b 5f 53 41 56 45 50 4f 49 4e 54  ine TK_SAVEPOINT
47440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47450 20 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65        13.#define
47460 20 54 4b 5f 52 45 4c 45 41 53 45 20 20 20 20 20   TK_RELEASE     
47470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47480 20 20 20 31 34 0a 23 64 65 66 69 6e 65 20 54 4b     14.#define TK
47490 5f 54 4f 20 20 20 20 20 20 20 20 20 20 20 20 20  _TO             
474a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
474b0 31 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 41  15.#define TK_TA
474c0 42 4c 45 20 20 20 20 20 20 20 20 20 20 20 20 20  BLE             
474d0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 36 0a               16.
474e0 23 64 65 66 69 6e 65 20 54 4b 5f 43 52 45 41 54  #define TK_CREAT
474f0 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
47500 20 20 20 20 20 20 20 20 20 20 31 37 0a 23 64 65            17.#de
47510 66 69 6e 65 20 54 4b 5f 49 46 20 20 20 20 20 20  fine TK_IF      
47520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47530 20 20 20 20 20 20 20 31 38 0a 23 64 65 66 69 6e         18.#defin
47540 65 20 54 4b 5f 4e 4f 54 20 20 20 20 20 20 20 20  e TK_NOT        
47550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47560 20 20 20 20 31 39 0a 23 64 65 66 69 6e 65 20 54      19.#define T
47570 4b 5f 45 58 49 53 54 53 20 20 20 20 20 20 20 20  K_EXISTS        
47580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47590 20 32 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54   20.#define TK_T
475a0 45 4d 50 20 20 20 20 20 20 20 20 20 20 20 20 20  EMP             
475b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 31                21
475c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c 50 20 20  .#define TK_LP  
475d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
475e0 20 20 20 20 20 20 20 20 20 20 20 32 32 0a 23 64             22.#d
475f0 65 66 69 6e 65 20 54 4b 5f 52 50 20 20 20 20 20  efine TK_RP     
47600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47610 20 20 20 20 20 20 20 20 32 33 0a 23 64 65 66 69          23.#defi
47620 6e 65 20 54 4b 5f 41 53 20 20 20 20 20 20 20 20  ne TK_AS        
47630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47640 20 20 20 20 20 32 34 0a 23 64 65 66 69 6e 65 20       24.#define 
47650 54 4b 5f 43 4f 4d 4d 41 20 20 20 20 20 20 20 20  TK_COMMA        
47660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47670 20 20 32 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f    25.#define TK_
47680 49 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ID              
47690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
476a0 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 44  6.#define TK_IND
476b0 45 58 45 44 20 20 20 20 20 20 20 20 20 20 20 20  EXED            
476c0 20 20 20 20 20 20 20 20 20 20 20 20 32 37 0a 23              27.#
476d0 64 65 66 69 6e 65 20 54 4b 5f 41 42 4f 52 54 20  define TK_ABORT 
476e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
476f0 20 20 20 20 20 20 20 20 20 32 38 0a 23 64 65 66           28.#def
47700 69 6e 65 20 54 4b 5f 41 43 54 49 4f 4e 20 20 20  ine TK_ACTION   
47710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47720 20 20 20 20 20 20 32 39 0a 23 64 65 66 69 6e 65        29.#define
47730 20 54 4b 5f 41 46 54 45 52 20 20 20 20 20 20 20   TK_AFTER       
47740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47750 20 20 20 33 30 0a 23 64 65 66 69 6e 65 20 54 4b     30.#define TK
47760 5f 41 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20  _ANALYZE        
47770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47780 33 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 53  31.#define TK_AS
47790 43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  C               
477a0 20 20 20 20 20 20 20 20 20 20 20 20 20 33 32 0a               32.
477b0 23 64 65 66 69 6e 65 20 54 4b 5f 41 54 54 41 43  #define TK_ATTAC
477c0 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  H               
477d0 20 20 20 20 20 20 20 20 20 20 33 33 0a 23 64 65            33.#de
477e0 66 69 6e 65 20 54 4b 5f 42 45 46 4f 52 45 20 20  fine TK_BEFORE  
477f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47800 20 20 20 20 20 20 20 33 34 0a 23 64 65 66 69 6e         34.#defin
47810 65 20 54 4b 5f 42 59 20 20 20 20 20 20 20 20 20  e TK_BY         
47820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47830 20 20 20 20 33 35 0a 23 64 65 66 69 6e 65 20 54      35.#define T
47840 4b 5f 43 41 53 43 41 44 45 20 20 20 20 20 20 20  K_CASCADE       
47850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47860 20 33 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43   36.#define TK_C
47870 41 53 54 20 20 20 20 20 20 20 20 20 20 20 20 20  AST             
47880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 37                37
47890 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 55  .#define TK_COLU
478a0 4d 4e 4b 57 20 20 20 20 20 20 20 20 20 20 20 20  MNKW            
478b0 20 20 20 20 20 20 20 20 20 20 20 33 38 0a 23 64             38.#d
478c0 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 46 4c 49 43  efine TK_CONFLIC
478d0 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
478e0 20 20 20 20 20 20 20 20 33 39 0a 23 64 65 66 69          39.#defi
478f0 6e 65 20 54 4b 5f 44 41 54 41 42 41 53 45 20 20  ne TK_DATABASE  
47900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47910 20 20 20 20 20 34 30 0a 23 64 65 66 69 6e 65 20       40.#define 
47920 54 4b 5f 44 45 53 43 20 20 20 20 20 20 20 20 20  TK_DESC         
47930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47940 20 20 34 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f    41.#define TK_
47950 44 45 54 41 43 48 20 20 20 20 20 20 20 20 20 20  DETACH          
47960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
47970 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 41 43  2.#define TK_EAC
47980 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  H               
47990 20 20 20 20 20 20 20 20 20 20 20 20 34 33 0a 23              43.#
479a0 64 65 66 69 6e 65 20 54 4b 5f 46 41 49 4c 20 20  define TK_FAIL  
479b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
479c0 20 20 20 20 20 20 20 20 20 34 34 0a 23 64 65 66           44.#def
479d0 69 6e 65 20 54 4b 5f 46 4f 52 20 20 20 20 20 20  ine TK_FOR      
479e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
479f0 20 20 20 20 20 20 34 35 0a 23 64 65 66 69 6e 65        45.#define
47a00 20 54 4b 5f 49 47 4e 4f 52 45 20 20 20 20 20 20   TK_IGNORE      
47a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a20 20 20 20 34 36 0a 23 64 65 66 69 6e 65 20 54 4b     46.#define TK
47a30 5f 49 4e 49 54 49 41 4c 4c 59 20 20 20 20 20 20  _INITIALLY      
47a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47a50 34 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e  47.#define TK_IN
47a60 53 54 45 41 44 20 20 20 20 20 20 20 20 20 20 20  STEAD           
47a70 20 20 20 20 20 20 20 20 20 20 20 20 20 34 38 0a               48.
47a80 23 64 65 66 69 6e 65 20 54 4b 5f 4c 49 4b 45 5f  #define TK_LIKE_
47a90 4b 57 20 20 20 20 20 20 20 20 20 20 20 20 20 20  KW              
47aa0 20 20 20 20 20 20 20 20 20 20 34 39 0a 23 64 65            49.#de
47ab0 66 69 6e 65 20 54 4b 5f 4d 41 54 43 48 20 20 20  fine TK_MATCH   
47ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ad0 20 20 20 20 20 20 20 35 30 0a 23 64 65 66 69 6e         50.#defin
47ae0 65 20 54 4b 5f 4e 4f 20 20 20 20 20 20 20 20 20  e TK_NO         
47af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b00 20 20 20 20 35 31 0a 23 64 65 66 69 6e 65 20 54      51.#define T
47b10 4b 5f 4b 45 59 20 20 20 20 20 20 20 20 20 20 20  K_KEY           
47b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47b30 20 35 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f   52.#define TK_O
47b40 46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  F               
47b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 33                53
47b60 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 46 46 53  .#define TK_OFFS
47b70 45 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ET              
47b80 20 20 20 20 20 20 20 20 20 20 20 35 34 0a 23 64             54.#d
47b90 65 66 69 6e 65 20 54 4b 5f 50 52 41 47 4d 41 20  efine TK_PRAGMA 
47ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47bb0 20 20 20 20 20 20 20 20 35 35 0a 23 64 65 66 69          55.#defi
47bc0 6e 65 20 54 4b 5f 52 41 49 53 45 20 20 20 20 20  ne TK_RAISE     
47bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47be0 20 20 20 20 20 35 36 0a 23 64 65 66 69 6e 65 20       56.#define 
47bf0 54 4b 5f 52 45 50 4c 41 43 45 20 20 20 20 20 20  TK_REPLACE      
47c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c10 20 20 35 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f    57.#define TK_
47c20 52 45 53 54 52 49 43 54 20 20 20 20 20 20 20 20  RESTRICT        
47c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
47c40 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 4f 57  8.#define TK_ROW
47c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47c60 20 20 20 20 20 20 20 20 20 20 20 20 35 39 0a 23              59.#
47c70 64 65 66 69 6e 65 20 54 4b 5f 54 52 49 47 47 45  define TK_TRIGGE
47c80 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  R               
47c90 20 20 20 20 20 20 20 20 20 36 30 0a 23 64 65 66           60.#def
47ca0 69 6e 65 20 54 4b 5f 56 41 43 55 55 4d 20 20 20  ine TK_VACUUM   
47cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47cc0 20 20 20 20 20 20 36 31 0a 23 64 65 66 69 6e 65        61.#define
47cd0 20 54 4b 5f 56 49 45 57 20 20 20 20 20 20 20 20   TK_VIEW        
47ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47cf0 20 20 20 36 32 0a 23 64 65 66 69 6e 65 20 54 4b     62.#define TK
47d00 5f 56 49 52 54 55 41 4c 20 20 20 20 20 20 20 20  _VIRTUAL        
47d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47d20 36 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45  63.#define TK_RE
47d30 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20  INDEX           
47d40 20 20 20 20 20 20 20 20 20 20 20 20 20 36 34 0a               64.
47d50 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 4e 41 4d  #define TK_RENAM
47d60 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
47d70 20 20 20 20 20 20 20 20 20 20 36 35 0a 23 64 65            65.#de
47d80 66 69 6e 65 20 54 4b 5f 43 54 49 4d 45 5f 4b 57  fine TK_CTIME_KW
47d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47da0 20 20 20 20 20 20 20 36 36 0a 23 64 65 66 69 6e         66.#defin
47db0 65 20 54 4b 5f 41 4e 59 20 20 20 20 20 20 20 20  e TK_ANY        
47dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47dd0 20 20 20 20 36 37 0a 23 64 65 66 69 6e 65 20 54      67.#define T
47de0 4b 5f 4f 52 20 20 20 20 20 20 20 20 20 20 20 20  K_OR            
47df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e00 20 36 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41   68.#define TK_A
47e10 4e 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ND              
47e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 39                69
47e30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 53 20 20  .#define TK_IS  
47e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e50 20 20 20 20 20 20 20 20 20 20 20 37 30 0a 23 64             70.#d
47e60 65 66 69 6e 65 20 54 4b 5f 42 45 54 57 45 45 4e  efine TK_BETWEEN
47e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47e80 20 20 20 20 20 20 20 20 37 31 0a 23 64 65 66 69          71.#defi
47e90 6e 65 20 54 4b 5f 49 4e 20 20 20 20 20 20 20 20  ne TK_IN        
47ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47eb0 20 20 20 20 20 37 32 0a 23 64 65 66 69 6e 65 20       72.#define 
47ec0 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20 20 20 20  TK_ISNULL       
47ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ee0 20 20 37 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f    73.#define TK_
47ef0 4e 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20 20 20  NOTNULL         
47f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
47f10 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 45 20  4.#define TK_NE 
47f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f30 20 20 20 20 20 20 20 20 20 20 20 20 37 35 0a 23              75.#
47f40 64 65 66 69 6e 65 20 54 4b 5f 45 51 20 20 20 20  define TK_EQ    
47f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f60 20 20 20 20 20 20 20 20 20 37 36 0a 23 64 65 66           76.#def
47f70 69 6e 65 20 54 4b 5f 47 54 20 20 20 20 20 20 20  ine TK_GT       
47f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47f90 20 20 20 20 20 20 37 37 0a 23 64 65 66 69 6e 65        77.#define
47fa0 20 54 4b 5f 4c 45 20 20 20 20 20 20 20 20 20 20   TK_LE          
47fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47fc0 20 20 20 37 38 0a 23 64 65 66 69 6e 65 20 54 4b     78.#define TK
47fd0 5f 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20  _LT             
47fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
47ff0 37 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 45  79.#define TK_GE
48000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48010 20 20 20 20 20 20 20 20 20 20 20 20 20 38 30 0a               80.
48020 23 64 65 66 69 6e 65 20 54 4b 5f 45 53 43 41 50  #define TK_ESCAP
48030 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
48040 20 20 20 20 20 20 20 20 20 20 38 31 0a 23 64 65            81.#de
48050 66 69 6e 65 20 54 4b 5f 42 49 54 41 4e 44 20 20  fine TK_BITAND  
48060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48070 20 20 20 20 20 20 20 38 32 0a 23 64 65 66 69 6e         82.#defin
48080 65 20 54 4b 5f 42 49 54 4f 52 20 20 20 20 20 20  e TK_BITOR      
48090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
480a0 20 20 20 20 38 33 0a 23 64 65 66 69 6e 65 20 54      83.#define T
480b0 4b 5f 4c 53 48 49 46 54 20 20 20 20 20 20 20 20  K_LSHIFT        
480c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
480d0 20 38 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52   84.#define TK_R
480e0 53 48 49 46 54 20 20 20 20 20 20 20 20 20 20 20  SHIFT           
480f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 35                85
48100 0a 23 64 65 66 69 6e 65 20 54 4b 5f 50 4c 55 53  .#define TK_PLUS
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 20 20 38 36 0a 23 64             86.#d
48130 65 66 69 6e 65 20 54 4b 5f 4d 49 4e 55 53 20 20  efine TK_MINUS  
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 20 38 37 0a 23 64 65 66 69          87.#defi
48160 6e 65 20 54 4b 5f 53 54 41 52 20 20 20 20 20 20  ne TK_STAR      
48170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48180 20 20 20 20 20 38 38 0a 23 64 65 66 69 6e 65 20       88.#define 
48190 54 4b 5f 53 4c 41 53 48 20 20 20 20 20 20 20 20  TK_SLASH        
481a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
481b0 20 20 38 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f    89.#define TK_
481c0 52 45 4d 20 20 20 20 20 20 20 20 20 20 20 20 20  REM             
481d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
481e0 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e  0.#define TK_CON
481f0 43 41 54 20 20 20 20 20 20 20 20 20 20 20 20 20  CAT             
48200 20 20 20 20 20 20 20 20 20 20 20 20 39 31 0a 23              91.#
48210 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 4c 41 54  define TK_COLLAT
48220 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
48230 20 20 20 20 20 20 20 20 20 39 32 0a 23 64 65 66           92.#def
48240 69 6e 65 20 54 4b 5f 42 49 54 4e 4f 54 20 20 20  ine TK_BITNOT   
48250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48260 20 20 20 20 20 20 39 33 0a 23 64 65 66 69 6e 65        93.#define
48270 20 54 4b 5f 53 54 52 49 4e 47 20 20 20 20 20 20   TK_STRING      
48280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48290 20 20 20 39 34 0a 23 64 65 66 69 6e 65 20 54 4b     94.#define TK
482a0 5f 4a 4f 49 4e 5f 4b 57 20 20 20 20 20 20 20 20  _JOIN_KW        
482b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
482c0 39 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f  95.#define TK_CO
482d0 4e 53 54 52 41 49 4e 54 20 20 20 20 20 20 20 20  NSTRAINT        
482e0 20 20 20 20 20 20 20 20 20 20 20 20 20 39 36 0a               96.
482f0 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 41 55  #define TK_DEFAU
48300 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  LT              
48310 20 20 20 20 20 20 20 20 20 20 39 37 0a 23 64 65            97.#de
48320 66 69 6e 65 20 54 4b 5f 4e 55 4c 4c 20 20 20 20  fine TK_NULL    
48330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48340 20 20 20 20 20 20 20 39 38 0a 23 64 65 66 69 6e         98.#defin
48350 65 20 54 4b 5f 50 52 49 4d 41 52 59 20 20 20 20  e TK_PRIMARY    
48360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48370 20 20 20 20 39 39 0a 23 64 65 66 69 6e 65 20 54      99.#define T
48380 4b 5f 55 4e 49 51 55 45 20 20 20 20 20 20 20 20  K_UNIQUE        
48390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
483a0 20 31 30 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f   100.#define TK_
483b0 43 48 45 43 4b 20 20 20 20 20 20 20 20 20 20 20  CHECK           
483c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
483d0 30 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45  01.#define TK_RE
483e0 46 45 52 45 4e 43 45 53 20 20 20 20 20 20 20 20  FERENCES        
483f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 32               102
48400 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 55 54 4f  .#define TK_AUTO
48410 49 4e 43 52 20 20 20 20 20 20 20 20 20 20 20 20  INCR            
48420 20 20 20 20 20 20 20 20 20 20 20 31 30 33 0a 23             103.#
48430 64 65 66 69 6e 65 20 54 4b 5f 4f 4e 20 20 20 20  define TK_ON    
48440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48450 20 20 20 20 20 20 20 20 20 31 30 34 0a 23 64 65           104.#de
48460 66 69 6e 65 20 54 4b 5f 44 45 4c 45 54 45 20 20  fine TK_DELETE  
48470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48480 20 20 20 20 20 20 20 31 30 35 0a 23 64 65 66 69         105.#defi
48490 6e 65 20 54 4b 5f 55 50 44 41 54 45 20 20 20 20  ne TK_UPDATE    
484a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
484b0 20 20 20 20 20 31 30 36 0a 23 64 65 66 69 6e 65       106.#define
484c0 20 54 4b 5f 53 45 54 20 20 20 20 20 20 20 20 20   TK_SET         
484d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
484e0 20 20 20 31 30 37 0a 23 64 65 66 69 6e 65 20 54     107.#define T
484f0 4b 5f 44 45 46 45 52 52 41 42 4c 45 20 20 20 20  K_DEFERRABLE    
48500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48510 20 31 30 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f   108.#define TK_
48520 46 4f 52 45 49 47 4e 20 20 20 20 20 20 20 20 20  FOREIGN         
48530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48540 30 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 52  09.#define TK_DR
48550 4f 50 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OP              
48560 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 30               110
48570 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e 49 4f  .#define TK_UNIO
48580 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  N               
48590 20 20 20 20 20 20 20 20 20 20 20 31 31 31 0a 23             111.#
485a0 64 65 66 69 6e 65 20 54 4b 5f 41 4c 4c 20 20 20  define TK_ALL   
485b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
485c0 20 20 20 20 20 20 20 20 20 31 31 32 0a 23 64 65           112.#de
485d0 66 69 6e 65 20 54 4b 5f 45 58 43 45 50 54 20 20  fine TK_EXCEPT  
485e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
485f0 20 20 20 20 20 20 20 31 31 33 0a 23 64 65 66 69         113.#defi
48600 6e 65 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 20  ne TK_INTERSECT 
48610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48620 20 20 20 20 20 31 31 34 0a 23 64 65 66 69 6e 65       114.#define
48630 20 54 4b 5f 53 45 4c 45 43 54 20 20 20 20 20 20   TK_SELECT      
48640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48650 20 20 20 31 31 35 0a 23 64 65 66 69 6e 65 20 54     115.#define T
48660 4b 5f 44 49 53 54 49 4e 43 54 20 20 20 20 20 20  K_DISTINCT      
48670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48680 20 31 31 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f   116.#define TK_
48690 44 4f 54 20 20 20 20 20 20 20 20 20 20 20 20 20  DOT             
486a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
486b0 31 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 52  17.#define TK_FR
486c0 4f 4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20  OM              
486d0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 38               118
486e0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4a 4f 49 4e  .#define TK_JOIN
486f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48700 20 20 20 20 20 20 20 20 20 20 20 31 31 39 0a 23             119.#
48710 64 65 66 69 6e 65 20 54 4b 5f 55 53 49 4e 47 20  define TK_USING 
48720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48730 20 20 20 20 20 20 20 20 20 31 32 30 0a 23 64 65           120.#de
48740 66 69 6e 65 20 54 4b 5f 4f 52 44 45 52 20 20 20  fine TK_ORDER   
48750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48760 20 20 20 20 20 20 20 31 32 31 0a 23 64 65 66 69         121.#defi
48770 6e 65 20 54 4b 5f 47 52 4f 55 50 20 20 20 20 20  ne TK_GROUP     
48780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48790 20 20 20 20 20 31 32 32 0a 23 64 65 66 69 6e 65       122.#define
487a0 20 54 4b 5f 48 41 56 49 4e 47 20 20 20 20 20 20   TK_HAVING      
487b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
487c0 20 20 20 31 32 33 0a 23 64 65 66 69 6e 65 20 54     123.#define T
487d0 4b 5f 4c 49 4d 49 54 20 20 20 20 20 20 20 20 20  K_LIMIT         
487e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
487f0 20 31 32 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f   124.#define TK_
48800 57 48 45 52 45 20 20 20 20 20 20 20 20 20 20 20  WHERE           
48810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48820 32 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e  25.#define TK_IN
48830 54 4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  TO              
48840 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 36               126
48850 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 41 4c 55  .#define TK_VALU
48860 45 53 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ES              
48870 20 20 20 20 20 20 20 20 20 20 20 31 32 37 0a 23             127.#
48880 64 65 66 69 6e 65 20 54 4b 5f 49 4e 53 45 52 54  define TK_INSERT
48890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
488a0 20 20 20 20 20 20 20 20 20 31 32 38 0a 23 64 65           128.#de
488b0 66 69 6e 65 20 54 4b 5f 49 4e 54 45 47 45 52 20  fine TK_INTEGER 
488c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
488d0 20 20 20 20 20 20 20 31 32 39 0a 23 64 65 66 69         129.#defi
488e0 6e 65 20 54 4b 5f 46 4c 4f 41 54 20 20 20 20 20  ne TK_FLOAT     
488f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48900 20 20 20 20 20 31 33 30 0a 23 64 65 66 69 6e 65       130.#define
48910 20 54 4b 5f 42 4c 4f 42 20 20 20 20 20 20 20 20   TK_BLOB        
48920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48930 20 20 20 31 33 31 0a 23 64 65 66 69 6e 65 20 54     131.#define T
48940 4b 5f 52 45 47 49 53 54 45 52 20 20 20 20 20 20  K_REGISTER      
48950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48960 20 31 33 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f   132.#define TK_
48970 56 41 52 49 41 42 4c 45 20 20 20 20 20 20 20 20  VARIABLE        
48980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48990 33 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 41  33.#define TK_CA
489a0 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  SE              
489b0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34               134
489c0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 57 48 45 4e  .#define TK_WHEN
489d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
489e0 20 20 20 20 20 20 20 20 20 20 20 31 33 35 0a 23             135.#
489f0 64 65 66 69 6e 65 20 54 4b 5f 54 48 45 4e 20 20  define TK_THEN  
48a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a10 20 20 20 20 20 20 20 20 20 31 33 36 0a 23 64 65           136.#de
48a20 66 69 6e 65 20 54 4b 5f 45 4c 53 45 20 20 20 20  fine TK_ELSE    
48a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a40 20 20 20 20 20 20 20 31 33 37 0a 23 64 65 66 69         137.#defi
48a50 6e 65 20 54 4b 5f 49 4e 44 45 58 20 20 20 20 20  ne TK_INDEX     
48a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48a70 20 20 20 20 20 31 33 38 0a 23 64 65 66 69 6e 65       138.#define
48a80 20 54 4b 5f 41 4c 54 45 52 20 20 20 20 20 20 20   TK_ALTER       
48a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48aa0 20 20 20 31 33 39 0a 23 64 65 66 69 6e 65 20 54     139.#define T
48ab0 4b 5f 41 44 44 20 20 20 20 20 20 20 20 20 20 20  K_ADD           
48ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48ad0 20 31 34 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f   140.#define TK_
48ae0 54 4f 5f 54 45 58 54 20 20 20 20 20 20 20 20 20  TO_TEXT         
48af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48b00 34 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f  41.#define TK_TO
48b10 5f 42 4c 4f 42 20 20 20 20 20 20 20 20 20 20 20  _BLOB           
48b20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34 32               142
48b30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 4e  .#define TK_TO_N
48b40 55 4d 45 52 49 43 20 20 20 20 20 20 20 20 20 20  UMERIC          
48b50 20 20 20 20 20 20 20 20 20 20 20 31 34 33 0a 23             143.#
48b60 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 49 4e 54  define TK_TO_INT
48b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48b80 20 20 20 20 20 20 20 20 20 31 34 34 0a 23 64 65           144.#de
48b90 66 69 6e 65 20 54 4b 5f 54 4f 5f 52 45 41 4c 20  fine TK_TO_REAL 
48ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48bb0 20 20 20 20 20 20 20 31 34 35 0a 23 64 65 66 69         145.#defi
48bc0 6e 65 20 54 4b 5f 49 53 4e 4f 54 20 20 20 20 20  ne TK_ISNOT     
48bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48be0 20 20 20 20 20 31 34 36 0a 23 64 65 66 69 6e 65       146.#define
48bf0 20 54 4b 5f 45 4e 44 5f 4f 46 5f 46 49 4c 45 20   TK_END_OF_FILE 
48c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48c10 20 20 20 31 34 37 0a 23 64 65 66 69 6e 65 20 54     147.#define T
48c20 4b 5f 49 4c 4c 45 47 41 4c 20 20 20 20 20 20 20  K_ILLEGAL       
48c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48c40 20 31 34 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f   148.#define TK_
48c50 53 50 41 43 45 20 20 20 20 20 20 20 20 20 20 20  SPACE           
48c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48c70 34 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4e  49.#define TK_UN
48c80 43 4c 4f 53 45 44 5f 53 54 52 49 4e 47 20 20 20  CLOSED_STRING   
48c90 20 20 20 20 20 20 20 20 20 20 20 20 20 31 35 30               150
48ca0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 55 4e 43  .#define TK_FUNC
48cb0 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20  TION            
48cc0 20 20 20 20 20 20 20 20 20 20 20 31 35 31 0a 23             151.#
48cd0 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4c 55 4d 4e  define TK_COLUMN
48ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48cf0 20 20 20 20 20 20 20 20 20 31 35 32 0a 23 64 65           152.#de
48d00 66 69 6e 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43  fine TK_AGG_FUNC
48d10 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20 20 20  TION            
48d20 20 20 20 20 20 20 20 31 35 33 0a 23 64 65 66 69         153.#defi
48d30 6e 65 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  ne TK_AGG_COLUMN
48d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48d50 20 20 20 20 20 31 35 34 0a 23 64 65 66 69 6e 65       154.#define
48d60 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 20   TK_CONST_FUNC  
48d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48d80 20 20 20 31 35 35 0a 23 64 65 66 69 6e 65 20 54     155.#define T
48d90 4b 5f 55 4d 49 4e 55 53 20 20 20 20 20 20 20 20  K_UMINUS        
48da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
48db0 20 31 35 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f   156.#define TK_
48dc0 55 50 4c 55 53 20 20 20 20 20 20 20 20 20 20 20  UPLUS           
48dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
48de0 35 37 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  57../***********
48df0 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 72 73 65  *** End of parse
48e00 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
48e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48e30 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
48e40 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
48e50 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
48e60 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   in sqliteInt.h 
48e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
48e80 2a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74  **/.#include <st
48e90 64 69 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20  dio.h>.#include 
48ea0 3c 73 74 64 6c 69 62 2e 68 3e 0a 23 69 6e 63 6c  <stdlib.h>.#incl
48eb0 75 64 65 20 3c 73 74 72 69 6e 67 2e 68 3e 0a 23  ude <string.h>.#
48ec0 69 6e 63 6c 75 64 65 20 3c 61 73 73 65 72 74 2e  include <assert.
48ed0 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64  h>.#include <std
48ee0 64 65 66 2e 68 3e 0a 0a 2f 2a 0a 2a 2a 20 49 66  def.h>../*.** If
48ef0 20 63 6f 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61   compiling for a
48f00 20 70 72 6f 63 65 73 73 6f 72 20 74 68 61 74 20   processor that 
48f10 6c 61 63 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70  lacks floating p
48f20 6f 69 6e 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a  oint support,.**
48f30 20 73 75 62 73 74 69 74 75 74 65 20 69 6e 74 65   substitute inte
48f40 67 65 72 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67  ger for floating
48f50 2d 70 6f 69 6e 74 0a 2a 2f 0a 23 69 66 64 65 66  -point.*/.#ifdef
48f60 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
48f70 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65  ATING_POINT.# de
48f80 66 69 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69  fine double sqli
48f90 74 65 5f 69 6e 74 36 34 0a 23 20 64 65 66 69 6e  te_int64.# defin
48fa0 65 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50  e LONGDOUBLE_TYP
48fb0 45 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 0a 23  E sqlite_int64.#
48fc0 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 42   ifndef SQLITE_B
48fd0 49 47 5f 44 42 4c 0a 23 20 20 20 64 65 66 69 6e  IG_DBL.#   defin
48fe0 65 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c  e SQLITE_BIG_DBL
48ff0 20 28 28 28 73 71 6c 69 74 65 33 5f 69 6e 74 36   (((sqlite3_int6
49000 34 29 31 29 3c 3c 35 30 29 0a 23 20 65 6e 64 69  4)1)<<50).# endi
49010 66 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  f.# define SQLIT
49020 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f  E_OMIT_DATETIME_
49030 46 55 4e 43 53 20 31 0a 23 20 64 65 66 69 6e 65  FUNCS 1.# define
49040 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41   SQLITE_OMIT_TRA
49050 43 45 20 31 0a 23 20 75 6e 64 65 66 20 53 51 4c  CE 1.# undef SQL
49060 49 54 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e  ITE_MIXED_ENDIAN
49070 5f 36 34 42 49 54 5f 46 4c 4f 41 54 0a 23 20 75  _64BIT_FLOAT.# u
49080 6e 64 65 66 20 53 51 4c 49 54 45 5f 48 41 56 45  ndef SQLITE_HAVE
49090 5f 49 53 4e 41 4e 0a 23 65 6e 64 69 66 0a 23 69  _ISNAN.#endif.#i
490a0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 42 49 47  fndef SQLITE_BIG
490b0 5f 44 42 4c 0a 23 20 64 65 66 69 6e 65 20 53 51  _DBL.# define SQ
490c0 4c 49 54 45 5f 42 49 47 5f 44 42 4c 20 28 31 65  LITE_BIG_DBL (1e
490d0 39 39 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  99).#endif../*.*
490e0 2a 20 4f 4d 49 54 5f 54 45 4d 50 44 42 20 69 73  * OMIT_TEMPDB is
490f0 20 73 65 74 20 74 6f 20 31 20 69 66 20 53 51 4c   set to 1 if SQL
49100 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 20  ITE_OMIT_TEMPDB 
49110 69 73 20 64 65 66 69 6e 65 64 2c 20 6f 72 20 30  is defined, or 0
49120 0a 2a 2a 20 61 66 74 65 72 77 61 72 64 2e 20 48  .** afterward. H
49130 61 76 69 6e 67 20 74 68 69 73 20 6d 61 63 72 6f  aving this macro
49140 20 61 6c 6c 6f 77 73 20 75 73 20 74 6f 20 63 61   allows us to ca
49150 75 73 65 20 74 68 65 20 43 20 63 6f 6d 70 69 6c  use the C compil
49160 65 72 20 0a 2a 2a 20 74 6f 20 6f 6d 69 74 20 63  er .** to omit c
49170 6f 64 65 20 75 73 65 64 20 62 79 20 54 45 4d 50  ode used by TEMP
49180 20 74 61 62 6c 65 73 20 77 69 74 68 6f 75 74 20   tables without 
49190 6d 65 73 73 79 20 23 69 66 6e 64 65 66 20 73 74  messy #ifndef st
491a0 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 69 66  atements..*/.#if
491b0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
491c0 54 45 4d 50 44 42 0a 23 64 65 66 69 6e 65 20 4f  TEMPDB.#define O
491d0 4d 49 54 5f 54 45 4d 50 44 42 20 31 0a 23 65 6c  MIT_TEMPDB 1.#el
491e0 73 65 0a 23 64 65 66 69 6e 65 20 4f 4d 49 54 5f  se.#define OMIT_
491f0 54 45 4d 50 44 42 20 30 0a 23 65 6e 64 69 66 0a  TEMPDB 0.#endif.
49200 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f  ./*.** If the fo
49210 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20 69 73  llowing macro is
49220 20 73 65 74 20 74 6f 20 31 2c 20 74 68 65 6e 20   set to 1, then 
49230 4e 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 20  NULL values are 
49240 63 6f 6e 73 69 64 65 72 65 64 0a 2a 2a 20 64 69  considered.** di
49250 73 74 69 6e 63 74 20 77 68 65 6e 20 64 65 74 65  stinct when dete
49260 72 6d 69 6e 69 6e 67 20 77 68 65 74 68 65 72 20  rmining whether 
49270 6f 72 20 6e 6f 74 20 74 77 6f 20 65 6e 74 72 69  or not two entri
49280 65 73 20 61 72 65 20 74 68 65 20 73 61 6d 65 0a  es are the same.
49290 2a 2a 20 69 6e 20 61 20 55 4e 49 51 55 45 20 69  ** in a UNIQUE i
492a0 6e 64 65 78 2e 20 20 54 68 69 73 20 69 73 20 74  ndex.  This is t
492b0 68 65 20 77 61 79 20 50 6f 73 74 67 72 65 53 51  he way PostgreSQ
492c0 4c 2c 20 4f 72 61 63 6c 65 2c 20 44 42 32 2c 20  L, Oracle, DB2, 
492d0 4d 79 53 51 4c 2c 0a 2a 2a 20 4f 43 45 4c 4f 54  MySQL,.** OCELOT
492e0 2c 20 61 6e 64 20 46 69 72 65 62 69 72 64 20 61  , and Firebird a
492f0 6c 6c 20 77 6f 72 6b 2e 20 20 54 68 65 20 53 51  ll work.  The SQ
49300 4c 39 32 20 73 70 65 63 20 65 78 70 6c 69 63 69  L92 spec explici
49310 74 6c 79 20 73 61 79 73 20 74 68 69 73 0a 2a 2a  tly says this.**
49320 20 69 73 20 74 68 65 20 77 61 79 20 74 68 69 6e   is the way thin
49330 67 73 20 61 72 65 20 73 75 70 70 6f 73 65 20 74  gs are suppose t
49340 6f 20 77 6f 72 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66  o work..**.** If
49350 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   the following m
49360 61 63 72 6f 20 69 73 20 73 65 74 20 74 6f 20 30  acro is set to 0
49370 2c 20 74 68 65 20 4e 55 4c 4c 73 20 61 72 65 20  , the NULLs are 
49380 69 6e 64 69 73 74 69 6e 63 74 20 66 6f 72 0a 2a  indistinct for.*
49390 2a 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78  * a UNIQUE index
493a0 2e 20 20 49 6e 20 74 68 69 73 20 6d 6f 64 65 2c  .  In this mode,
493b0 20 79 6f 75 20 63 61 6e 20 6f 6e 6c 79 20 68 61   you can only ha
493c0 76 65 20 61 20 73 69 6e 67 6c 65 20 4e 55 4c 4c  ve a single NULL
493d0 20 65 6e 74 72 79 0a 2a 2a 20 66 6f 72 20 61 20   entry.** for a 
493e0 63 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 65 64 20  column declared 
493f0 55 4e 49 51 55 45 2e 20 20 54 68 69 73 20 69 73  UNIQUE.  This is
49400 20 74 68 65 20 77 61 79 20 49 6e 66 6f 72 6d 69   the way Informi
49410 78 20 61 6e 64 20 53 51 4c 20 53 65 72 76 65 72  x and SQL Server
49420 0a 2a 2a 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 64 65  .** work..*/.#de
49430 66 69 6e 65 20 4e 55 4c 4c 5f 44 49 53 54 49 4e  fine NULL_DISTIN
49440 43 54 5f 46 4f 52 5f 55 4e 49 51 55 45 20 31 0a  CT_FOR_UNIQUE 1.
49450 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 66 69 6c 65  ./*.** The "file
49460 20 66 6f 72 6d 61 74 22 20 6e 75 6d 62 65 72 20   format" number 
49470 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 68  is an integer th
49480 61 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65  at is incremente
49490 64 20 77 68 65 6e 65 76 65 72 0a 2a 2a 20 74 68  d whenever.** th
494a0 65 20 56 44 42 45 2d 6c 65 76 65 6c 20 66 69 6c  e VDBE-level fil
494b0 65 20 66 6f 72 6d 61 74 20 63 68 61 6e 67 65 73  e format changes
494c0 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
494d0 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 74   macros define t
494e0 68 65 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c  he.** the defaul
494f0 74 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 66 6f  t file format fo
49500 72 20 6e 65 77 20 64 61 74 61 62 61 73 65 73 20  r new databases 
49510 61 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  and the maximum 
49520 66 69 6c 65 20 66 6f 72 6d 61 74 0a 2a 2a 20 74  file format.** t
49530 68 61 74 20 74 68 65 20 6c 69 62 72 61 72 79 20  hat the library 
49540 63 61 6e 20 72 65 61 64 2e 0a 2a 2f 0a 23 64 65  can read..*/.#de
49550 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f  fine SQLITE_MAX_
49560 46 49 4c 45 5f 46 4f 52 4d 41 54 20 34 0a 23 69  FILE_FORMAT 4.#i
49570 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46  fndef SQLITE_DEF
49580 41 55 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54  AULT_FILE_FORMAT
49590 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
495a0 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f 46 4f  _DEFAULT_FILE_FO
495b0 52 4d 41 54 20 31 0a 23 65 6e 64 69 66 0a 0a 23  RMAT 1.#endif..#
495c0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45  ifndef SQLITE_DE
495d0 46 41 55 4c 54 5f 52 45 43 55 52 53 49 56 45 5f  FAULT_RECURSIVE_
495e0 54 52 49 47 47 45 52 53 0a 23 20 64 65 66 69 6e  TRIGGERS.# defin
495f0 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  e SQLITE_DEFAULT
49600 5f 52 45 43 55 52 53 49 56 45 5f 54 52 49 47 47  _RECURSIVE_TRIGG
49610 45 52 53 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ERS 0.#endif../*
49620 0a 2a 2a 20 50 72 6f 76 69 64 65 20 61 20 64 65  .** Provide a de
49630 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20  fault value for 
49640 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52  SQLITE_TEMP_STOR
49650 45 20 69 6e 20 63 61 73 65 20 69 74 20 69 73 20  E in case it is 
49660 6e 6f 74 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  not specified.**
49670 20 6f 6e 20 74 68 65 20 63 6f 6d 6d 61 6e 64 2d   on the command-
49680 6c 69 6e 65 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  line.*/.#ifndef 
49690 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52  SQLITE_TEMP_STOR
496a0 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  E.# define SQLIT
496b0 45 5f 54 45 4d 50 5f 53 54 4f 52 45 20 31 0a 23  E_TEMP_STORE 1.#
496c0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 43 43  endif../*.** GCC
496d0 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e 65   does not define
496e0 20 74 68 65 20 6f 66 66 73 65 74 6f 66 28 29 20   the offsetof() 
496f0 6d 61 63 72 6f 20 73 6f 20 77 65 27 6c 6c 20 68  macro so we'll h
49700 61 76 65 20 74 6f 20 64 6f 20 69 74 0a 2a 2a 20  ave to do it.** 
49710 6f 75 72 73 65 6c 76 65 73 2e 0a 2a 2f 0a 23 69  ourselves..*/.#i
49720 66 6e 64 65 66 20 6f 66 66 73 65 74 6f 66 0a 23  fndef offsetof.#
49730 64 65 66 69 6e 65 20 6f 66 66 73 65 74 6f 66 28  define offsetof(
49740 53 54 52 55 43 54 55 52 45 2c 46 49 45 4c 44 29  STRUCTURE,FIELD)
49750 20 28 28 69 6e 74 29 28 28 63 68 61 72 2a 29 26   ((int)((char*)&
49760 28 28 53 54 52 55 43 54 55 52 45 2a 29 30 29 2d  ((STRUCTURE*)0)-
49770 3e 46 49 45 4c 44 29 29 0a 23 65 6e 64 69 66 0a  >FIELD)).#endif.
49780 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
49790 73 65 65 20 69 66 20 74 68 69 73 20 6d 61 63 68  see if this mach
497a0 69 6e 65 20 75 73 65 73 20 45 42 43 44 49 43 2e  ine uses EBCDIC.
497b0 20 20 28 59 65 73 2c 20 62 65 6c 69 65 76 65 20    (Yes, believe 
497c0 69 74 20 6f 72 0a 2a 2a 20 6e 6f 74 2c 20 74 68  it or.** not, th
497d0 65 72 65 20 61 72 65 20 73 74 69 6c 6c 20 6d 61  ere are still ma
497e0 63 68 69 6e 65 73 20 6f 75 74 20 74 68 65 72 65  chines out there
497f0 20 74 68 61 74 20 75 73 65 20 45 42 43 44 49 43   that use EBCDIC
49800 2e 29 0a 2a 2f 0a 23 69 66 20 27 41 27 20 3d 3d  .).*/.#if 'A' ==
49810 20 27 5c 33 30 31 27 0a 23 20 64 65 66 69 6e 65   '\301'.# define
49820 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 20 31   SQLITE_EBCDIC 1
49830 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
49840 53 51 4c 49 54 45 5f 41 53 43 49 49 20 31 0a 23  SQLITE_ASCII 1.#
49850 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74  endif../*.** Int
49860 65 67 65 72 73 20 6f 66 20 6b 6e 6f 77 6e 20 73  egers of known s
49870 69 7a 65 73 2e 20 20 54 68 65 73 65 20 74 79 70  izes.  These typ
49880 65 64 65 66 73 20 6d 69 67 68 74 20 63 68 61 6e  edefs might chan
49890 67 65 20 66 6f 72 20 61 72 63 68 69 74 65 63 74  ge for architect
498a0 75 72 65 73 0a 2a 2a 20 77 68 65 72 65 20 74 68  ures.** where th
498b0 65 20 73 69 7a 65 73 20 76 65 72 79 2e 20 20 50  e sizes very.  P
498c0 72 65 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72  reprocessor macr
498d0 6f 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65  os are available
498e0 20 73 6f 20 74 68 61 74 20 74 68 65 0a 2a 2a 20   so that the.** 
498f0 74 79 70 65 73 20 63 61 6e 20 62 65 20 63 6f 6e  types can be con
49900 76 65 6e 69 65 6e 74 6c 79 20 72 65 64 65 66 69  veniently redefi
49910 6e 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74  ned at compile-t
49920 79 70 65 2e 20 20 4c 69 6b 65 20 74 68 69 73 3a  ype.  Like this:
49930 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 63  .**.**         c
49940 63 20 27 2d 44 55 49 4e 54 50 54 52 5f 54 59 50  c '-DUINTPTR_TYP
49950 45 3d 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e 74 27  E=long long int'
49960 20 2e 2e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20   ....*/.#ifndef 
49970 55 49 4e 54 33 32 5f 54 59 50 45 0a 23 20 69 66  UINT32_TYPE.# if
49980 64 65 66 20 48 41 56 45 5f 55 49 4e 54 33 32 5f  def HAVE_UINT32_
49990 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54  T.#  define UINT
499a0 33 32 5f 54 59 50 45 20 75 69 6e 74 33 32 5f 74  32_TYPE uint32_t
499b0 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e  .# else.#  defin
499c0 65 20 55 49 4e 54 33 32 5f 54 59 50 45 20 75 6e  e UINT32_TYPE un
499d0 73 69 67 6e 65 64 20 69 6e 74 0a 23 20 65 6e 64  signed int.# end
499e0 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65  if.#endif.#ifnde
499f0 66 20 55 49 4e 54 31 36 5f 54 59 50 45 0a 23 20  f UINT16_TYPE.# 
49a00 69 66 64 65 66 20 48 41 56 45 5f 55 49 4e 54 31  ifdef HAVE_UINT1
49a10 36 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49  6_T.#  define UI
49a20 4e 54 31 36 5f 54 59 50 45 20 75 69 6e 74 31 36  NT16_TYPE uint16
49a30 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66  _t.# else.#  def
49a40 69 6e 65 20 55 49 4e 54 31 36 5f 54 59 50 45 20  ine UINT16_TYPE 
49a50 75 6e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 69  unsigned short i
49a60 6e 74 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69  nt.# endif.#endi
49a70 66 0a 23 69 66 6e 64 65 66 20 49 4e 54 31 36 5f  f.#ifndef INT16_
49a80 54 59 50 45 0a 23 20 69 66 64 65 66 20 48 41 56  TYPE.# ifdef HAV
49a90 45 5f 49 4e 54 31 36 5f 54 0a 23 20 20 64 65 66  E_INT16_T.#  def
49aa0 69 6e 65 20 49 4e 54 31 36 5f 54 59 50 45 20 69  ine INT16_TYPE i
49ab0 6e 74 31 36 5f 74 0a 23 20 65 6c 73 65 0a 23 20  nt16_t.# else.# 
49ac0 20 64 65 66 69 6e 65 20 49 4e 54 31 36 5f 54 59   define INT16_TY
49ad0 50 45 20 73 68 6f 72 74 20 69 6e 74 0a 23 20 65  PE short int.# e
49ae0 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e  ndif.#endif.#ifn
49af0 64 65 66 20 55 49 4e 54 38 5f 54 59 50 45 0a 23  def UINT8_TYPE.#
49b00 20 69 66 64 65 66 20 48 41 56 45 5f 55 49 4e 54   ifdef HAVE_UINT
49b10 38 5f 54 0a 23 20 20 64 65 66 69 6e 65 20 55 49  8_T.#  define UI
49b20 4e 54 38 5f 54 59 50 45 20 75 69 6e 74 38 5f 74  NT8_TYPE uint8_t
49b30 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e  .# else.#  defin
49b40 65 20 55 49 4e 54 38 5f 54 59 50 45 20 75 6e 73  e UINT8_TYPE uns
49b50 69 67 6e 65 64 20 63 68 61 72 0a 23 20 65 6e 64  igned char.# end
49b60 69 66 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65  if.#endif.#ifnde
49b70 66 20 49 4e 54 38 5f 54 59 50 45 0a 23 20 69 66  f INT8_TYPE.# if
49b80 64 65 66 20 48 41 56 45 5f 49 4e 54 38 5f 54 0a  def HAVE_INT8_T.
49b90 23 20 20 64 65 66 69 6e 65 20 49 4e 54 38 5f 54  #  define INT8_T
49ba0 59 50 45 20 69 6e 74 38 5f 74 0a 23 20 65 6c 73  YPE int8_t.# els
49bb0 65 0a 23 20 20 64 65 66 69 6e 65 20 49 4e 54 38  e.#  define INT8
49bc0 5f 54 59 50 45 20 73 69 67 6e 65 64 20 63 68 61  _TYPE signed cha
49bd0 72 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66  r.# endif.#endif
49be0 0a 23 69 66 6e 64 65 66 20 4c 4f 4e 47 44 4f 55  .#ifndef LONGDOU
49bf0 42 4c 45 5f 54 59 50 45 0a 23 20 64 65 66 69 6e  BLE_TYPE.# defin
49c00 65 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50  e LONGDOUBLE_TYP
49c10 45 20 6c 6f 6e 67 20 64 6f 75 62 6c 65 0a 23 65  E long double.#e
49c20 6e 64 69 66 0a 74 79 70 65 64 65 66 20 73 71 6c  ndif.typedef sql
49c30 69 74 65 5f 69 6e 74 36 34 20 69 36 34 3b 20 20  ite_int64 i64;  
49c40 20 20 20 20 20 20 20 20 2f 2a 20 38 2d 62 79 74          /* 8-byt
49c50 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  e signed integer
49c60 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 71 6c 69   */.typedef sqli
49c70 74 65 5f 75 69 6e 74 36 34 20 75 36 34 3b 20 20  te_uint64 u64;  
49c80 20 20 20 20 20 20 20 2f 2a 20 38 2d 62 79 74 65         /* 8-byte
49c90 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65   unsigned intege
49ca0 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49 4e  r */.typedef UIN
49cb0 54 33 32 5f 54 59 50 45 20 75 33 32 3b 20 20 20  T32_TYPE u32;   
49cc0 20 20 20 20 20 20 20 20 2f 2a 20 34 2d 62 79 74          /* 4-byt
49cd0 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67  e unsigned integ
49ce0 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49  er */.typedef UI
49cf0 4e 54 31 36 5f 54 59 50 45 20 75 31 36 3b 20 20  NT16_TYPE u16;  
49d00 20 20 20 20 20 20 20 20 20 2f 2a 20 32 2d 62 79           /* 2-by
49d10 74 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65  te unsigned inte
49d20 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 49  ger */.typedef I
49d30 4e 54 31 36 5f 54 59 50 45 20 69 31 36 3b 20 20  NT16_TYPE i16;  
49d40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 2d 62            /* 2-b
49d50 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67  yte signed integ
49d60 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 55 49  er */.typedef UI
49d70 4e 54 38 5f 54 59 50 45 20 75 38 3b 20 20 20 20  NT8_TYPE u8;    
49d80 20 20 20 20 20 20 20 20 20 2f 2a 20 31 2d 62 79           /* 1-by
49d90 74 65 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65  te unsigned inte
49da0 67 65 72 20 2a 2f 0a 74 79 70 65 64 65 66 20 49  ger */.typedef I
49db0 4e 54 38 5f 54 59 50 45 20 69 38 3b 20 20 20 20  NT8_TYPE i8;    
49dc0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 2d 62            /* 1-b
49dd0 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67  yte signed integ
49de0 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c  er */../*.** SQL
49df0 49 54 45 5f 4d 41 58 5f 55 33 32 20 69 73 20 61  ITE_MAX_U32 is a
49e00 20 75 36 34 20 63 6f 6e 73 74 61 6e 74 20 74 68   u64 constant th
49e10 61 74 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75  at is the maximu
49e20 6d 20 75 36 34 20 76 61 6c 75 65 0a 2a 2a 20 74  m u64 value.** t
49e30 68 61 74 20 63 61 6e 20 62 65 20 73 74 6f 72 65  hat can be store
49e40 64 20 69 6e 20 61 20 75 33 32 20 77 69 74 68 6f  d in a u32 witho
49e50 75 74 20 6c 6f 73 73 20 6f 66 20 64 61 74 61 2e  ut loss of data.
49e60 20 20 54 68 65 20 76 61 6c 75 65 0a 2a 2a 20 69    The value.** i
49e70 73 20 30 78 30 30 30 30 30 30 30 30 66 66 66 66  s 0x00000000ffff
49e80 66 66 66 66 2e 20 20 42 75 74 20 62 65 63 61 75  ffff.  But becau
49e90 73 65 20 6f 66 20 71 75 69 72 6b 73 20 6f 66 20  se of quirks of 
49ea0 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 2c 20  some compilers, 
49eb0 77 65 0a 2a 2a 20 68 61 76 65 20 74 6f 20 73 70  we.** have to sp
49ec0 65 63 69 66 79 20 74 68 65 20 76 61 6c 75 65 20  ecify the value 
49ed0 69 6e 20 74 68 65 20 6c 65 73 73 20 69 6e 74 75  in the less intu
49ee0 69 74 69 76 65 20 6d 61 6e 6e 65 72 20 73 68 6f  itive manner sho
49ef0 77 6e 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  wn:.*/.#define S
49f00 51 4c 49 54 45 5f 4d 41 58 5f 55 33 32 20 20 28  QLITE_MAX_U32  (
49f10 28 28 28 75 36 34 29 31 29 3c 3c 33 32 29 2d 31  (((u64)1)<<32)-1
49f20 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20  )../*.** Macros 
49f30 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65  to determine whe
49f40 74 68 65 72 20 74 68 65 20 6d 61 63 68 69 6e 65  ther the machine
49f50 20 69 73 20 62 69 67 20 6f 72 20 6c 69 74 74 6c   is big or littl
49f60 65 20 65 6e 64 69 61 6e 2c 0a 2a 2a 20 65 76 61  e endian,.** eva
49f70 6c 75 61 74 65 64 20 61 74 20 72 75 6e 74 69 6d  luated at runtim
49f80 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e..*/.#ifdef SQL
49f90 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e  ITE_AMALGAMATION
49fa0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
49fb0 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65  const int sqlite
49fc0 33 6f 6e 65 20 3d 20 31 3b 0a 23 65 6c 73 65 0a  3one = 1;.#else.
49fd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
49fe0 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65 33  onst int sqlite3
49ff0 6f 6e 65 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20  one;.#endif.#if 
4a000 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c  defined(i386) ||
4a010 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f   defined(__i386_
4a020 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d  _) || defined(_M
4a030 5f 49 58 38 36 29 5c 0a 20 20 20 20 20 20 20 20  _IX86)\.        
4a040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4a050 20 20 20 20 20 7c 7c 20 64 65 66 69 6e 65 64 28       || defined(
4a060 5f 5f 78 38 36 5f 36 34 29 20 7c 7c 20 64 65 66  __x86_64) || def
4a070 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29  ined(__x86_64__)
4a080 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
4a090 5f 42 49 47 45 4e 44 49 41 4e 20 20 20 20 30 0a  _BIGENDIAN    0.
4a0a0 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
4a0b0 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 31 0a 23  LITTLEENDIAN 1.#
4a0c0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55   define SQLITE_U
4a0d0 54 46 31 36 4e 41 54 49 56 45 20 20 53 51 4c 49  TF16NATIVE  SQLI
4a0e0 54 45 5f 55 54 46 31 36 4c 45 0a 23 65 6c 73 65  TE_UTF16LE.#else
4a0f0 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
4a100 5f 42 49 47 45 4e 44 49 41 4e 20 20 20 20 28 2a  _BIGENDIAN    (*
4a110 28 63 68 61 72 20 2a 29 28 26 73 71 6c 69 74 65  (char *)(&sqlite
4a120 33 6f 6e 65 29 3d 3d 30 29 0a 23 20 64 65 66 69  3one)==0).# defi
4a130 6e 65 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45  ne SQLITE_LITTLE
4a140 45 4e 44 49 41 4e 20 28 2a 28 63 68 61 72 20 2a  ENDIAN (*(char *
4a150 29 28 26 73 71 6c 69 74 65 33 6f 6e 65 29 3d 3d  )(&sqlite3one)==
4a160 31 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  1).# define SQLI
4a170 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20 28  TE_UTF16NATIVE (
4a180 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41 4e  SQLITE_BIGENDIAN
4a190 3f 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3a  ?SQLITE_UTF16BE:
4a1a0 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 29 0a  SQLITE_UTF16LE).
4a1b0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f  #endif../*.** Co
4a1c0 6e 73 74 61 6e 74 73 20 66 6f 72 20 74 68 65 20  nstants for the 
4a1d0 6c 61 72 67 65 73 74 20 61 6e 64 20 73 6d 61 6c  largest and smal
4a1e0 6c 65 73 74 20 70 6f 73 73 69 62 6c 65 20 36 34  lest possible 64
4a1f0 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65  -bit signed inte
4a200 67 65 72 73 2e 0a 2a 2a 20 54 68 65 73 65 20 6d  gers..** These m
4a210 61 63 72 6f 73 20 61 72 65 20 64 65 73 69 67 6e  acros are design
4a220 65 64 20 74 6f 20 77 6f 72 6b 20 63 6f 72 72 65  ed to work corre
4a230 63 74 6c 79 20 6f 6e 20 62 6f 74 68 20 33 32 2d  ctly on both 32-
4a240 62 69 74 20 61 6e 64 20 36 34 2d 62 69 74 0a 2a  bit and 64-bit.*
4a250 2a 20 63 6f 6d 70 69 6c 65 72 73 2e 0a 2a 2f 0a  * compilers..*/.
4a260 23 64 65 66 69 6e 65 20 4c 41 52 47 45 53 54 5f  #define LARGEST_
4a270 49 4e 54 36 34 20 20 28 30 78 66 66 66 66 66 66  INT64  (0xffffff
4a280 66 66 7c 28 28 28 69 36 34 29 30 78 37 66 66 66  ff|(((i64)0x7fff
4a290 66 66 66 66 29 3c 3c 33 32 29 29 0a 23 64 65 66  ffff)<<32)).#def
4a2a0 69 6e 65 20 53 4d 41 4c 4c 45 53 54 5f 49 4e 54  ine SMALLEST_INT
4a2b0 36 34 20 28 28 28 69 36 34 29 2d 31 29 20 2d 20  64 (((i64)-1) - 
4a2c0 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 29 0a 0a  LARGEST_INT64)..
4a2d0 2f 2a 20 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20  /* .** Round up 
4a2e0 61 20 6e 75 6d 62 65 72 20 74 6f 20 74 68 65 20  a number to the 
4a2f0 6e 65 78 74 20 6c 61 72 67 65 72 20 6d 75 6c 74  next larger mult
4a300 69 70 6c 65 20 6f 66 20 38 2e 20 20 54 68 69 73  iple of 8.  This
4a310 20 69 73 20 75 73 65 64 0a 2a 2a 20 74 6f 20 66   is used.** to f
4a320 6f 72 63 65 20 38 2d 62 79 74 65 20 61 6c 69 67  orce 8-byte alig
4a330 6e 6d 65 6e 74 20 6f 6e 20 36 34 2d 62 69 74 20  nment on 64-bit 
4a340 61 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a 2a  architectures..*
4a350 2f 0a 23 64 65 66 69 6e 65 20 52 4f 55 4e 44 38  /.#define ROUND8
4a360 28 78 29 20 20 20 20 20 28 28 28 78 29 2b 37 29  (x)     (((x)+7)
4a370 26 7e 37 29 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e  &~7)../*.** Roun
4a380 64 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 6e 65  d down to the ne
4a390 61 72 65 73 74 20 6d 75 6c 74 69 70 6c 65 20 6f  arest multiple o
4a3a0 66 20 38 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 52  f 8.*/.#define R
4a3b0 4f 55 4e 44 44 4f 57 4e 38 28 78 29 20 28 28 78  OUNDDOWN8(x) ((x
4a3c0 29 26 7e 37 29 0a 0a 2f 2a 0a 2a 2a 20 41 73 73  )&~7)../*.** Ass
4a3d0 65 72 74 20 74 68 61 74 20 74 68 65 20 70 6f 69  ert that the poi
4a3e0 6e 74 65 72 20 58 20 69 73 20 61 6c 69 67 6e 65  nter X is aligne
4a3f0 64 20 74 6f 20 61 6e 20 38 2d 62 79 74 65 20 62  d to an 8-byte b
4a400 6f 75 6e 64 61 72 79 2e 0a 2a 2f 0a 23 64 65 66  oundary..*/.#def
4a410 69 6e 65 20 45 49 47 48 54 5f 42 59 54 45 5f 41  ine EIGHT_BYTE_A
4a420 4c 49 47 4e 4d 45 4e 54 28 58 29 20 20 20 28 28  LIGNMENT(X)   ((
4a430 28 28 63 68 61 72 2a 29 28 58 29 20 2d 20 28 63  ((char*)(X) - (c
4a440 68 61 72 2a 29 30 29 26 37 29 3d 3d 30 29 0a 0a  har*)0)&7)==0)..
4a450 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
4a460 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
4a470 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73  ing structure is
4a480 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74   used to store t
4a490 68 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a  he busy-handler.
4a4a0 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20  ** callback for 
4a4b0 61 20 67 69 76 65 6e 20 73 71 6c 69 74 65 20 68  a given sqlite h
4a4c0 61 6e 64 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68  andle. .**.** Th
4a4d0 65 20 73 71 6c 69 74 65 2e 62 75 73 79 48 61 6e  e sqlite.busyHan
4a4e0 64 6c 65 72 20 6d 65 6d 62 65 72 20 6f 66 20 74  dler member of t
4a4f0 68 65 20 73 71 6c 69 74 65 20 73 74 72 75 63 74  he sqlite struct
4a500 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 62 75   contains the bu
4a510 73 79 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66  sy.** callback f
4a520 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20  or the database 
4a530 68 61 6e 64 6c 65 2e 20 45 61 63 68 20 70 61 67  handle. Each pag
4a540 65 72 20 6f 70 65 6e 65 64 20 76 69 61 20 74 68  er opened via th
4a550 65 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61 6e 64  e sqlite.** hand
4a560 6c 65 20 69 73 20 70 61 73 73 65 64 20 61 20 70  le is passed a p
4a570 6f 69 6e 74 65 72 20 74 6f 20 73 71 6c 69 74 65  ointer to sqlite
4a580 2e 62 75 73 79 48 61 6e 64 6c 65 72 2e 20 54 68  .busyHandler. Th
4a590 65 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 0a 2a  e busy-handler.*
4a5a0 2a 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 75  * callback is cu
4a5b0 72 72 65 6e 74 6c 79 20 69 6e 76 6f 6b 65 64 20  rrently invoked 
4a5c0 6f 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68 69 6e  only from within
4a5d0 20 70 61 67 65 72 2e 63 2e 0a 2a 2f 0a 74 79 70   pager.c..*/.typ
4a5e0 65 64 65 66 20 73 74 72 75 63 74 20 42 75 73 79  edef struct Busy
4a5f0 48 61 6e 64 6c 65 72 20 42 75 73 79 48 61 6e 64  Handler BusyHand
4a600 6c 65 72 3b 0a 73 74 72 75 63 74 20 42 75 73 79  ler;.struct Busy
4a610 48 61 6e 64 6c 65 72 20 7b 0a 20 20 69 6e 74 20  Handler {.  int 
4a620 28 2a 78 46 75 6e 63 29 28 76 6f 69 64 20 2a 2c  (*xFunc)(void *,
4a630 69 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 62 75  int);  /* The bu
4a640 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20  sy callback */. 
4a650 20 76 6f 69 64 20 2a 70 41 72 67 3b 20 20 20 20   void *pArg;    
4a660 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
4a670 69 72 73 74 20 61 72 67 20 74 6f 20 62 75 73 79  irst arg to busy
4a680 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69   callback */.  i
4a690 6e 74 20 6e 42 75 73 79 3b 20 20 20 20 20 20 20  nt nBusy;       
4a6a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 63            /* Inc
4a6b0 72 65 6d 65 6e 74 65 64 20 77 69 74 68 20 65 61  remented with ea
4a6c0 63 68 20 62 75 73 79 20 63 61 6c 6c 20 2a 2f 0a  ch busy call */.
4a6d0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f  };../*.** Name o
4a6e0 66 20 74 68 65 20 6d 61 73 74 65 72 20 64 61 74  f the master dat
4a6f0 61 62 61 73 65 20 74 61 62 6c 65 2e 20 20 54 68  abase table.  Th
4a700 65 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73  e master databas
4a710 65 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 61 20  e table.** is a 
4a720 73 70 65 63 69 61 6c 20 74 61 62 6c 65 20 74 68  special table th
4a730 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d  at holds the nam
4a740 65 73 20 61 6e 64 20 61 74 74 72 69 62 75 74 65  es and attribute
4a750 73 20 6f 66 20 61 6c 6c 0a 2a 2a 20 75 73 65 72  s of all.** user
4a760 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69   tables and indi
4a770 63 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ces..*/.#define 
4a780 4d 41 53 54 45 52 5f 4e 41 4d 45 20 20 20 20 20  MASTER_NAME     
4a790 20 20 22 73 71 6c 69 74 65 5f 6d 61 73 74 65 72    "sqlite_master
4a7a0 22 0a 23 64 65 66 69 6e 65 20 54 45 4d 50 5f 4d  ".#define TEMP_M
4a7b0 41 53 54 45 52 5f 4e 41 4d 45 20 20 22 73 71 6c  ASTER_NAME  "sql
4a7c0 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 22  ite_temp_master"
4a7d0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74  ../*.** The root
4a7e0 2d 70 61 67 65 20 6f 66 20 74 68 65 20 6d 61 73  -page of the mas
4a7f0 74 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62  ter database tab
4a800 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  le..*/.#define M
4a810 41 53 54 45 52 5f 52 4f 4f 54 20 20 20 20 20 20  ASTER_ROOT      
4a820 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61   1../*.** The na
4a830 6d 65 20 6f 66 20 74 68 65 20 73 63 68 65 6d 61  me of the schema
4a840 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69   table..*/.#defi
4a850 6e 65 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28  ne SCHEMA_TABLE(
4a860 78 29 20 20 28 28 21 4f 4d 49 54 5f 54 45 4d 50  x)  ((!OMIT_TEMP
4a870 44 42 29 26 26 28 78 3d 3d 31 29 3f 54 45 4d 50  DB)&&(x==1)?TEMP
4a880 5f 4d 41 53 54 45 52 5f 4e 41 4d 45 3a 4d 41 53  _MASTER_NAME:MAS
4a890 54 45 52 5f 4e 41 4d 45 29 0a 0a 2f 2a 0a 2a 2a  TER_NAME)../*.**
4a8a0 20 41 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 6d   A convenience m
4a8b0 61 63 72 6f 20 74 68 61 74 20 72 65 74 75 72 6e  acro that return
4a8c0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
4a8d0 65 6c 65 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 61  elements in.** a
4a8e0 6e 20 61 72 72 61 79 2e 0a 2a 2f 0a 23 64 65 66  n array..*/.#def
4a8f0 69 6e 65 20 41 72 72 61 79 53 69 7a 65 28 58 29  ine ArraySize(X)
4a900 20 20 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f      ((int)(sizeo
4a910 66 28 58 29 2f 73 69 7a 65 6f 66 28 58 5b 30 5d  f(X)/sizeof(X[0]
4a920 29 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  )))../*.** The f
4a930 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 61  ollowing value a
4a940 73 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 6d  s a destructor m
4a950 65 61 6e 73 20 74 6f 20 75 73 65 20 73 71 6c 69  eans to use sqli
4a960 74 65 33 44 62 46 72 65 65 28 29 2e 0a 2a 2a 20  te3DbFree()..** 
4a970 54 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65 72  This is an inter
4a980 6e 61 6c 20 65 78 74 65 6e 73 69 6f 6e 20 74 6f  nal extension to
4a990 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 20 61   SQLITE_STATIC a
4a9a0 6e 64 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  nd SQLITE_TRANSI
4a9b0 45 4e 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ENT..*/.#define 
4a9c0 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 20  SQLITE_DYNAMIC  
4a9d0 20 28 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72   ((sqlite3_destr
4a9e0 75 63 74 6f 72 5f 74 79 70 65 29 73 71 6c 69 74  uctor_type)sqlit
4a9f0 65 33 44 62 46 72 65 65 29 0a 0a 2f 2a 0a 2a 2a  e3DbFree)../*.**
4aa00 20 57 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4d 49   When SQLITE_OMI
4aa10 54 5f 57 53 44 20 69 73 20 64 65 66 69 6e 65 64  T_WSD is defined
4aa20 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20  , it means that 
4aa30 74 68 65 20 74 61 72 67 65 74 20 70 6c 61 74 66  the target platf
4aa40 6f 72 6d 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20  orm does.** not 
4aa50 73 75 70 70 6f 72 74 20 57 72 69 74 61 62 6c 65  support Writable
4aa60 20 53 74 61 74 69 63 20 44 61 74 61 20 28 57 53   Static Data (WS
4aa70 44 29 20 73 75 63 68 20 61 73 20 67 6c 6f 62 61  D) such as globa
4aa80 6c 20 61 6e 64 20 73 74 61 74 69 63 20 76 61 72  l and static var
4aa90 69 61 62 6c 65 73 2e 0a 2a 2a 20 41 6c 6c 20 76  iables..** All v
4aaa0 61 72 69 61 62 6c 65 73 20 6d 75 73 74 20 65 69  ariables must ei
4aab0 74 68 65 72 20 62 65 20 6f 6e 20 74 68 65 20 73  ther be on the s
4aac0 74 61 63 6b 20 6f 72 20 64 79 6e 61 6d 69 63 61  tack or dynamica
4aad0 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 66 72  lly allocated fr
4aae0 6f 6d 0a 2a 2a 20 74 68 65 20 68 65 61 70 2e 20  om.** the heap. 
4aaf0 20 57 68 65 6e 20 57 53 44 20 69 73 20 75 6e 73   When WSD is uns
4ab00 75 70 70 6f 72 74 65 64 2c 20 74 68 65 20 76 61  upported, the va
4ab10 72 69 61 62 6c 65 20 64 65 63 6c 61 72 61 74 69  riable declarati
4ab20 6f 6e 73 20 73 63 61 74 74 65 72 65 64 0a 2a 2a  ons scattered.**
4ab30 20 74 68 72 6f 75 67 68 6f 75 74 20 74 68 65 20   throughout the 
4ab40 53 51 4c 69 74 65 20 63 6f 64 65 20 6d 75 73 74  SQLite code must
4ab50 20 62 65 63 6f 6d 65 20 63 6f 6e 73 74 61 6e 74   become constant
4ab60 73 20 69 6e 73 74 65 61 64 2e 20 20 54 68 65 20  s instead.  The 
4ab70 53 51 4c 49 54 45 5f 57 53 44 0a 2a 2a 20 6d 61  SQLITE_WSD.** ma
4ab80 63 72 6f 20 69 73 20 75 73 65 64 20 66 6f 72 20  cro is used for 
4ab90 74 68 69 73 20 70 75 72 70 6f 73 65 2e 20 20 41  this purpose.  A
4aba0 6e 64 20 69 6e 73 74 65 61 64 20 6f 66 20 72 65  nd instead of re
4abb0 66 65 72 65 6e 63 69 6e 67 20 74 68 65 20 76 61  ferencing the va
4abc0 72 69 61 62 6c 65 0a 2a 2a 20 64 69 72 65 63 74  riable.** direct
4abd0 6c 79 2c 20 77 65 20 75 73 65 20 69 74 73 20 63  ly, we use its c
4abe0 6f 6e 73 74 61 6e 74 20 61 73 20 61 20 6b 65 79  onstant as a key
4abf0 20 74 6f 20 6c 6f 6f 6b 75 70 20 74 68 65 20 72   to lookup the r
4ac00 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63 61 74 65  un-time allocate
4ac10 64 0a 2a 2a 20 62 75 66 66 65 72 20 74 68 61 74  d.** buffer that
4ac20 20 68 6f 6c 64 73 20 72 65 61 6c 20 76 61 72 69   holds real vari
4ac30 61 62 6c 65 2e 20 20 54 68 65 20 63 6f 6e 73 74  able.  The const
4ac40 61 6e 74 20 69 73 20 61 6c 73 6f 20 74 68 65 20  ant is also the 
4ac50 69 6e 69 74 69 61 6c 69 7a 65 72 0a 2a 2a 20 66  initializer.** f
4ac60 6f 72 20 74 68 65 20 72 75 6e 2d 74 69 6d 65 20  or the run-time 
4ac70 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72  allocated buffer
4ac80 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 75  ..**.** In the u
4ac90 73 75 61 6c 20 63 61 73 65 20 77 68 65 72 65 20  sual case where 
4aca0 57 53 44 20 69 73 20 73 75 70 70 6f 72 74 65 64  WSD is supported
4acb0 2c 20 74 68 65 20 53 51 4c 49 54 45 5f 57 53 44  , the SQLITE_WSD
4acc0 20 61 6e 64 20 47 4c 4f 42 41 4c 0a 2a 2a 20 6d   and GLOBAL.** m
4acd0 61 63 72 6f 73 20 62 65 63 6f 6d 65 20 6e 6f 2d  acros become no-
4ace0 6f 70 73 20 61 6e 64 20 68 61 76 65 20 7a 65 72  ops and have zer
4acf0 6f 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 69 6d  o performance im
4ad00 70 61 63 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  pact..*/.#ifdef 
4ad10 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a  SQLITE_OMIT_WSD.
4ad20 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45    #define SQLITE
4ad30 5f 57 53 44 20 63 6f 6e 73 74 0a 20 20 23 64 65  _WSD const.  #de
4ad40 66 69 6e 65 20 47 4c 4f 42 41 4c 28 74 2c 76 29  fine GLOBAL(t,v)
4ad50 20 28 2a 28 74 2a 29 73 71 6c 69 74 65 33 5f 77   (*(t*)sqlite3_w
4ad60 73 64 5f 66 69 6e 64 28 28 76 6f 69 64 2a 29 26  sd_find((void*)&
4ad70 28 76 29 2c 20 73 69 7a 65 6f 66 28 76 29 29 29  (v), sizeof(v)))
4ad80 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74  .  #define sqlit
4ad90 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 47  e3GlobalConfig G
4ada0 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 53 71 6c  LOBAL(struct Sql
4adb0 69 74 65 33 43 6f 6e 66 69 67 2c 20 73 71 6c 69  ite3Config, sqli
4adc0 74 65 33 43 6f 6e 66 69 67 29 0a 53 51 4c 49 54  te3Config).SQLIT
4add0 45 5f 41 50 49 20 20 20 69 6e 74 20 73 71 6c 69  E_API   int sqli
4ade0 74 65 33 5f 77 73 64 5f 69 6e 69 74 28 69 6e 74  te3_wsd_init(int
4adf0 20 4e 2c 20 69 6e 74 20 4a 29 3b 0a 53 51 4c 49   N, int J);.SQLI
4ae00 54 45 5f 41 50 49 20 20 20 76 6f 69 64 20 2a 73  TE_API   void *s
4ae10 71 6c 69 74 65 33 5f 77 73 64 5f 66 69 6e 64 28  qlite3_wsd_find(
4ae20 76 6f 69 64 20 2a 4b 2c 20 69 6e 74 20 4c 29 3b  void *K, int L);
4ae30 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
4ae40 20 53 51 4c 49 54 45 5f 57 53 44 20 0a 20 20 23   SQLITE_WSD .  #
4ae50 64 65 66 69 6e 65 20 47 4c 4f 42 41 4c 28 74 2c  define GLOBAL(t,
4ae60 76 29 20 76 0a 20 20 23 64 65 66 69 6e 65 20 73  v) v.  #define s
4ae70 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
4ae80 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67  ig sqlite3Config
4ae90 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
4aea0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63  he following mac
4aeb0 72 6f 73 20 61 72 65 20 75 73 65 64 20 74 6f 20  ros are used to 
4aec0 73 75 70 70 72 65 73 73 20 63 6f 6d 70 69 6c 65  suppress compile
4aed0 72 20 77 61 72 6e 69 6e 67 73 20 61 6e 64 20 74  r warnings and t
4aee0 6f 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 63 6c 65  o.** make it cle
4aef0 61 72 20 74 6f 20 68 75 6d 61 6e 20 72 65 61 64  ar to human read
4af00 65 72 73 20 77 68 65 6e 20 61 20 66 75 6e 63 74  ers when a funct
4af10 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20 69 73  ion parameter is
4af20 20 64 65 6c 69 62 65 72 61 74 65 6c 79 20 0a 2a   deliberately .*
4af30 2a 20 6c 65 66 74 20 75 6e 75 73 65 64 20 77 69  * left unused wi
4af40 74 68 69 6e 20 74 68 65 20 62 6f 64 79 20 6f 66  thin the body of
4af50 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68 69   a function. Thi
4af60 73 20 75 73 75 61 6c 6c 79 20 68 61 70 70 65 6e  s usually happen
4af70 73 20 77 68 65 6e 0a 2a 2a 20 61 20 66 75 6e 63  s when.** a func
4af80 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 76  tion is called v
4af90 69 61 20 61 20 66 75 6e 63 74 69 6f 6e 20 70 6f  ia a function po
4afa0 69 6e 74 65 72 2e 20 46 6f 72 20 65 78 61 6d 70  inter. For examp
4afb0 6c 65 20 74 68 65 20 0a 2a 2a 20 69 6d 70 6c 65  le the .** imple
4afc0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20  mentation of an 
4afd0 53 51 4c 20 61 67 67 72 65 67 61 74 65 20 73 74  SQL aggregate st
4afe0 65 70 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20  ep callback may 
4aff0 6e 6f 74 20 75 73 65 20 74 68 65 0a 2a 2a 20 70  not use the.** p
4b000 61 72 61 6d 65 74 65 72 20 69 6e 64 69 63 61 74  arameter indicat
4b010 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ing the number o
4b020 66 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73  f arguments pass
4b030 65 64 20 74 6f 20 74 68 65 20 61 67 67 72 65 67  ed to the aggreg
4b040 61 74 65 2c 0a 2a 2a 20 69 66 20 69 74 20 6b 6e  ate,.** if it kn
4b050 6f 77 73 20 74 68 61 74 20 74 68 69 73 20 69 73  ows that this is
4b060 20 65 6e 66 6f 72 63 65 64 20 65 6c 73 65 77 68   enforced elsewh
4b070 65 72 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  ere..**.** When 
4b080 61 20 66 75 6e 63 74 69 6f 6e 20 70 61 72 61 6d  a function param
4b090 65 74 65 72 20 69 73 20 6e 6f 74 20 75 73 65 64  eter is not used
4b0a0 20 61 74 20 61 6c 6c 20 77 69 74 68 69 6e 20 74   at all within t
4b0b0 68 65 20 62 6f 64 79 20 6f 66 20 61 20 66 75 6e  he body of a fun
4b0c0 63 74 69 6f 6e 2c 0a 2a 2a 20 69 74 20 69 73 20  ction,.** it is 
4b0d0 67 65 6e 65 72 61 6c 6c 79 20 6e 61 6d 65 64 20  generally named 
4b0e0 22 4e 6f 74 55 73 65 64 22 20 6f 72 20 22 4e 6f  "NotUsed" or "No
4b0f0 74 55 73 65 64 32 22 20 74 6f 20 6d 61 6b 65 20  tUsed2" to make 
4b100 74 68 69 6e 67 73 20 65 76 65 6e 20 63 6c 65 61  things even clea
4b110 72 65 72 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c  rer..** However,
4b120 20 74 68 65 73 65 20 6d 61 63 72 6f 73 20 6d 61   these macros ma
4b130 79 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20 74  y also be used t
4b140 6f 20 73 75 70 70 72 65 73 73 20 77 61 72 6e 69  o suppress warni
4b150 6e 67 73 20 72 65 6c 61 74 65 64 20 74 6f 0a 2a  ngs related to.*
4b160 2a 20 70 61 72 61 6d 65 74 65 72 73 20 74 68 61  * parameters tha
4b170 74 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74  t may or may not
4b180 20 62 65 20 75 73 65 64 20 64 65 70 65 6e 64 69   be used dependi
4b190 6e 67 20 6f 6e 20 63 6f 6d 70 69 6c 61 74 69 6f  ng on compilatio
4b1a0 6e 20 6f 70 74 69 6f 6e 73 2e 0a 2a 2a 20 46 6f  n options..** Fo
4b1b0 72 20 65 78 61 6d 70 6c 65 20 74 68 6f 73 65 20  r example those 
4b1c0 70 61 72 61 6d 65 74 65 72 73 20 6f 6e 6c 79 20  parameters only 
4b1d0 75 73 65 64 20 69 6e 20 61 73 73 65 72 74 28 29  used in assert()
4b1e0 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 6e 20   statements. In 
4b1f0 74 68 65 73 65 0a 2a 2a 20 63 61 73 65 73 20 74  these.** cases t
4b200 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 61 72  he parameters ar
4b210 65 20 6e 61 6d 65 64 20 61 73 20 70 65 72 20 74  e named as per t
4b220 68 65 20 75 73 75 61 6c 20 63 6f 6e 76 65 6e 74  he usual convent
4b230 69 6f 6e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ions..*/.#define
4b240 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
4b250 52 28 78 29 20 28 76 6f 69 64 29 28 78 29 0a 23  R(x) (void)(x).#
4b260 64 65 66 69 6e 65 20 55 4e 55 53 45 44 5f 50 41  define UNUSED_PA
4b270 52 41 4d 45 54 45 52 32 28 78 2c 79 29 20 55 4e  RAMETER2(x,y) UN
4b280 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 78  USED_PARAMETER(x
4b290 29 2c 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54  ),UNUSED_PARAMET
4b2a0 45 52 28 79 29 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72  ER(y)../*.** For
4b2b0 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 73 20  ward references 
4b2c0 74 6f 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2f  to structures.*/
4b2d0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b2e0 41 67 67 49 6e 66 6f 20 41 67 67 49 6e 66 6f 3b  AggInfo AggInfo;
4b2f0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b300 41 75 74 68 43 6f 6e 74 65 78 74 20 41 75 74 68  AuthContext Auth
4b310 43 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64 65 66  Context;.typedef
4b320 20 73 74 72 75 63 74 20 41 75 74 6f 69 6e 63 49   struct AutoincI
4b330 6e 66 6f 20 41 75 74 6f 69 6e 63 49 6e 66 6f 3b  nfo AutoincInfo;
4b340 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b350 42 69 74 76 65 63 20 42 69 74 76 65 63 3b 0a 74  Bitvec Bitvec;.t
4b360 79 70 65 64 65 66 20 73 74 72 75 63 74 20 52 6f  ypedef struct Ro
4b370 77 53 65 74 20 52 6f 77 53 65 74 3b 0a 74 79 70  wSet RowSet;.typ
4b380 65 64 65 66 20 73 74 72 75 63 74 20 43 6f 6c 6c  edef struct Coll
4b390 53 65 71 20 43 6f 6c 6c 53 65 71 3b 0a 74 79 70  Seq CollSeq;.typ
4b3a0 65 64 65 66 20 73 74 72 75 63 74 20 43 6f 6c 75  edef struct Colu
4b3b0 6d 6e 20 43 6f 6c 75 6d 6e 3b 0a 74 79 70 65 64  mn Column;.typed
4b3c0 65 66 20 73 74 72 75 63 74 20 44 62 20 44 62 3b  ef struct Db Db;
4b3d0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b3e0 53 63 68 65 6d 61 20 53 63 68 65 6d 61 3b 0a 74  Schema Schema;.t
4b3f0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78  ypedef struct Ex
4b400 70 72 20 45 78 70 72 3b 0a 74 79 70 65 64 65 66  pr Expr;.typedef
4b410 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
4b420 20 45 78 70 72 4c 69 73 74 3b 0a 74 79 70 65 64   ExprList;.typed
4b430 65 66 20 73 74 72 75 63 74 20 45 78 70 72 53 70  ef struct ExprSp
4b440 61 6e 20 45 78 70 72 53 70 61 6e 3b 0a 74 79 70  an ExprSpan;.typ
4b450 65 64 65 66 20 73 74 72 75 63 74 20 46 4b 65 79  edef struct FKey
4b460 20 46 4b 65 79 3b 0a 74 79 70 65 64 65 66 20 73   FKey;.typedef s
4b470 74 72 75 63 74 20 46 75 6e 63 44 65 66 20 46 75  truct FuncDef Fu
4b480 6e 63 44 65 66 3b 0a 74 79 70 65 64 65 66 20 73  ncDef;.typedef s
4b490 74 72 75 63 74 20 46 75 6e 63 44 65 66 48 61 73  truct FuncDefHas
4b4a0 68 20 46 75 6e 63 44 65 66 48 61 73 68 3b 0a 74  h FuncDefHash;.t
4b4b0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 64  ypedef struct Id
4b4c0 4c 69 73 74 20 49 64 4c 69 73 74 3b 0a 74 79 70  List IdList;.typ
4b4d0 65 64 65 66 20 73 74 72 75 63 74 20 49 6e 64 65  edef struct Inde
4b4e0 78 20 49 6e 64 65 78 3b 0a 74 79 70 65 64 65 66  x Index;.typedef
4b4f0 20 73 74 72 75 63 74 20 49 6e 64 65 78 53 61 6d   struct IndexSam
4b500 70 6c 65 20 49 6e 64 65 78 53 61 6d 70 6c 65 3b  ple IndexSample;
4b510 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b520 4b 65 79 43 6c 61 73 73 20 4b 65 79 43 6c 61 73  KeyClass KeyClas
4b530 73 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  s;.typedef struc
4b540 74 20 4b 65 79 49 6e 66 6f 20 4b 65 79 49 6e 66  t KeyInfo KeyInf
4b550 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  o;.typedef struc
4b560 74 20 4c 6f 6f 6b 61 73 69 64 65 20 4c 6f 6f 6b  t Lookaside Look
4b570 61 73 69 64 65 3b 0a 74 79 70 65 64 65 66 20 73  aside;.typedef s
4b580 74 72 75 63 74 20 4c 6f 6f 6b 61 73 69 64 65 53  truct LookasideS
4b590 6c 6f 74 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f  lot LookasideSlo
4b5a0 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  t;.typedef struc
4b5b0 74 20 4d 6f 64 75 6c 65 20 4d 6f 64 75 6c 65 3b  t Module Module;
4b5c0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b5d0 4e 61 6d 65 43 6f 6e 74 65 78 74 20 4e 61 6d 65  NameContext Name
4b5e0 43 6f 6e 74 65 78 74 3b 0a 74 79 70 65 64 65 66  Context;.typedef
4b5f0 20 73 74 72 75 63 74 20 50 61 72 73 65 20 50 61   struct Parse Pa
4b600 72 73 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72  rse;.typedef str
4b610 75 63 74 20 53 61 76 65 70 6f 69 6e 74 20 53 61  uct Savepoint Sa
4b620 76 65 70 6f 69 6e 74 3b 0a 74 79 70 65 64 65 66  vepoint;.typedef
4b630 20 73 74 72 75 63 74 20 53 65 6c 65 63 74 20 53   struct Select S
4b640 65 6c 65 63 74 3b 0a 74 79 70 65 64 65 66 20 73  elect;.typedef s
4b650 74 72 75 63 74 20 53 72 63 4c 69 73 74 20 53 72  truct SrcList Sr
4b660 63 4c 69 73 74 3b 0a 74 79 70 65 64 65 66 20 73  cList;.typedef s
4b670 74 72 75 63 74 20 53 74 72 41 63 63 75 6d 20 53  truct StrAccum S
4b680 74 72 41 63 63 75 6d 3b 0a 74 79 70 65 64 65 66  trAccum;.typedef
4b690 20 73 74 72 75 63 74 20 54 61 62 6c 65 20 54 61   struct Table Ta
4b6a0 62 6c 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ble;.typedef str
4b6b0 75 63 74 20 54 61 62 6c 65 4c 6f 63 6b 20 54 61  uct TableLock Ta
4b6c0 62 6c 65 4c 6f 63 6b 3b 0a 74 79 70 65 64 65 66  bleLock;.typedef
4b6d0 20 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 54 6f   struct Token To
4b6e0 6b 65 6e 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ken;.typedef str
4b6f0 75 63 74 20 54 72 69 67 67 65 72 50 72 67 20 54  uct TriggerPrg T
4b700 72 69 67 67 65 72 50 72 67 3b 0a 74 79 70 65 64  riggerPrg;.typed
4b710 65 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65  ef struct Trigge
4b720 72 53 74 65 70 20 54 72 69 67 67 65 72 53 74 65  rStep TriggerSte
4b730 70 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  p;.typedef struc
4b740 74 20 54 72 69 67 67 65 72 20 54 72 69 67 67 65  t Trigger Trigge
4b750 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  r;.typedef struc
4b760 74 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  t UnpackedRecord
4b770 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 3b   UnpackedRecord;
4b780 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b790 56 54 61 62 6c 65 20 56 54 61 62 6c 65 3b 0a 74  VTable VTable;.t
4b7a0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57 61  ypedef struct Wa
4b7b0 6c 6b 65 72 20 57 61 6c 6b 65 72 3b 0a 74 79 70  lker Walker;.typ
4b7c0 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72  edef struct Wher
4b7d0 65 50 6c 61 6e 20 57 68 65 72 65 50 6c 61 6e 3b  ePlan WherePlan;
4b7e0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4b7f0 57 68 65 72 65 49 6e 66 6f 20 57 68 65 72 65 49  WhereInfo WhereI
4b800 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73 74 72  nfo;.typedef str
4b810 75 63 74 20 57 68 65 72 65 4c 65 76 65 6c 20 57  uct WhereLevel W
4b820 68 65 72 65 4c 65 76 65 6c 3b 0a 0a 2f 2a 0a 2a  hereLevel;../*.*
4b830 2a 20 44 65 66 65 72 20 73 6f 75 72 63 69 6e 67  * Defer sourcing
4b840 20 76 64 62 65 2e 68 20 61 6e 64 20 62 74 72 65   vdbe.h and btre
4b850 65 2e 68 20 75 6e 74 69 6c 20 61 66 74 65 72 20  e.h until after 
4b860 74 68 65 20 22 75 38 22 20 61 6e 64 20 0a 2a 2a  the "u8" and .**
4b870 20 22 42 75 73 79 48 61 6e 64 6c 65 72 22 20 74   "BusyHandler" t
4b880 79 70 65 64 65 66 73 2e 20 76 64 62 65 2e 68 20  ypedefs. vdbe.h 
4b890 61 6c 73 6f 20 72 65 71 75 69 72 65 73 20 61 20  also requires a 
4b8a0 66 65 77 20 6f 66 20 74 68 65 20 6f 70 61 71 75  few of the opaqu
4b8b0 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 79 70  e.** pointer typ
4b8c0 65 73 20 28 69 2e 65 2e 20 46 75 6e 63 44 65 66  es (i.e. FuncDef
4b8d0 29 20 64 65 66 69 6e 65 64 20 61 62 6f 76 65 2e  ) defined above.
4b8e0 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .*/./***********
4b8f0 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 62 74 72 65  *** Include btre
4b900 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  e.h in the middl
4b910 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68  e of sqliteInt.h
4b920 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
4b930 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
4b940 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 62  *** Begin file b
4b950 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  tree.h *********
4b960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4b980 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
4b990 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
4b9a0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
4b9b0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
4b9c0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
4b9d0 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
4b9e0 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
4b9f0 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
4ba00 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
4ba10 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
4ba20 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
4ba30 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
4ba40 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
4ba50 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
4ba60 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
4ba70 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
4ba80 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
4ba90 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
4baa0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
4bab0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
4bac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4bad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4bae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4baf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
4bb00 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64  is header file d
4bb10 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72  efines the inter
4bb20 66 61 63 65 20 74 68 61 74 20 74 68 65 20 73 71  face that the sq
4bb30 6c 69 74 65 20 42 2d 54 72 65 65 20 66 69 6c 65  lite B-Tree file
4bb40 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 20  .** subsystem.  
4bb50 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 69 6e 20  See comments in 
4bb60 74 68 65 20 73 6f 75 72 63 65 20 63 6f 64 65 20  the source code 
4bb70 66 6f 72 20 61 20 64 65 74 61 69 6c 65 64 20 64  for a detailed d
4bb80 65 73 63 72 69 70 74 69 6f 6e 0a 2a 2a 20 6f 66  escription.** of
4bb90 20 77 68 61 74 20 65 61 63 68 20 69 6e 74 65 72   what each inter
4bba0 66 61 63 65 20 72 6f 75 74 69 6e 65 20 64 6f 65  face routine doe
4bbb0 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49  s..**.** @(#) $I
4bbc0 64 3a 20 62 74 72 65 65 2e 68 2c 76 20 31 2e 31  d: btree.h,v 1.1
4bbd0 32 30 20 32 30 30 39 2f 30 37 2f 32 32 20 30 30  20 2009/07/22 00
4bbe0 3a 33 35 3a 32 34 20 64 72 68 20 45 78 70 20 24  :35:24 drh Exp $
4bbf0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 42 54 52  .*/.#ifndef _BTR
4bc00 45 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 42  EE_H_.#define _B
4bc10 54 52 45 45 5f 48 5f 0a 0a 2f 2a 20 54 4f 44 4f  TREE_H_../* TODO
4bc20 3a 20 54 68 69 73 20 64 65 66 69 6e 69 74 69 6f  : This definitio
4bc30 6e 20 69 73 20 6a 75 73 74 20 69 6e 63 6c 75 64  n is just includ
4bc40 65 64 20 73 6f 20 6f 74 68 65 72 20 6d 6f 64 75  ed so other modu
4bc50 6c 65 73 20 63 6f 6d 70 69 6c 65 2e 20 49 74 0a  les compile. It.
4bc60 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20 72  ** needs to be r
4bc70 65 76 69 73 69 74 65 64 2e 0a 2a 2f 0a 23 64 65  evisited..*/.#de
4bc80 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 42 54  fine SQLITE_N_BT
4bc90 52 45 45 5f 4d 45 54 41 20 31 30 0a 0a 2f 2a 0a  REE_META 10../*.
4bca0 2a 2a 20 49 66 20 64 65 66 69 6e 65 64 20 61 73  ** If defined as
4bcb0 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d   non-zero, auto-
4bcc0 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c 65  vacuum is enable
4bcd0 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20 4f 74  d by default. Ot
4bce0 68 65 72 77 69 73 65 0a 2a 2a 20 69 74 20 6d 75  herwise.** it mu
4bcf0 73 74 20 62 65 20 74 75 72 6e 65 64 20 6f 6e 20  st be turned on 
4bd00 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73  for each databas
4bd10 65 20 75 73 69 6e 67 20 22 50 52 41 47 4d 41 20  e using "PRAGMA 
4bd20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20 31 22  auto_vacuum = 1"
4bd30 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
4bd40 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f  ITE_DEFAULT_AUTO
4bd50 56 41 43 55 55 4d 0a 20 20 23 64 65 66 69 6e 65  VACUUM.  #define
4bd60 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
4bd70 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65 6e  AUTOVACUUM 0.#en
4bd80 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 42 54 52  dif..#define BTR
4bd90 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e 4f  EE_AUTOVACUUM_NO
4bda0 4e 45 20 30 20 20 20 20 20 20 20 20 2f 2a 20 44  NE 0        /* D
4bdb0 6f 20 6e 6f 74 20 64 6f 20 61 75 74 6f 2d 76 61  o not do auto-va
4bdc0 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e 65 20  cuum */.#define 
4bdd0 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d  BTREE_AUTOVACUUM
4bde0 5f 46 55 4c 4c 20 31 20 20 20 20 20 20 20 20 2f  _FULL 1        /
4bdf0 2a 20 44 6f 20 66 75 6c 6c 20 61 75 74 6f 2d 76  * Do full auto-v
4be00 61 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e 65  acuum */.#define
4be10 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55   BTREE_AUTOVACUU
4be20 4d 5f 49 4e 43 52 20 32 20 20 20 20 20 20 20 20  M_INCR 2        
4be30 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 76  /* Incremental v
4be40 61 63 75 75 6d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  acuum */../*.** 
4be50 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74  Forward declarat
4be60 69 6f 6e 73 20 6f 66 20 73 74 72 75 63 74 75 72  ions of structur
4be70 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  e.*/.typedef str
4be80 75 63 74 20 42 74 72 65 65 20 42 74 72 65 65 3b  uct Btree Btree;
4be90 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4bea0 42 74 43 75 72 73 6f 72 20 42 74 43 75 72 73 6f  BtCursor BtCurso
4beb0 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  r;.typedef struc
4bec0 74 20 42 74 53 68 61 72 65 64 20 42 74 53 68 61  t BtShared BtSha
4bed0 72 65 64 3b 0a 74 79 70 65 64 65 66 20 73 74 72  red;.typedef str
4bee0 75 63 74 20 42 74 72 65 65 4d 75 74 65 78 41 72  uct BtreeMutexAr
4bef0 72 61 79 20 42 74 72 65 65 4d 75 74 65 78 41 72  ray BtreeMutexAr
4bf00 72 61 79 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ray;../*.** This
4bf10 20 73 74 72 75 63 74 75 72 65 20 72 65 63 6f 72   structure recor
4bf20 64 73 20 61 6c 6c 20 6f 66 20 74 68 65 20 42 74  ds all of the Bt
4bf30 72 65 65 73 20 74 68 61 74 20 6e 65 65 64 20 74  rees that need t
4bf40 6f 20 68 6f 6c 64 0a 2a 2a 20 61 20 6d 75 74 65  o hold.** a mute
4bf50 78 20 62 65 66 6f 72 65 20 77 65 20 65 6e 74 65  x before we ente
4bf60 72 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65  r sqlite3VdbeExe
4bf70 63 28 29 2e 20 20 54 68 65 20 42 74 72 65 65 73  c().  The Btrees
4bf80 20 61 72 65 0a 2a 2a 20 61 72 65 20 70 6c 61 63   are.** are plac
4bf90 65 64 20 69 6e 20 61 42 74 72 65 65 5b 5d 20 69  ed in aBtree[] i
4bfa0 6e 20 6f 72 64 65 72 20 6f 66 20 61 42 74 72 65  n order of aBtre
4bfb0 65 5b 5d 2d 3e 70 42 74 2e 20 20 54 68 61 74 20  e[]->pBt.  That 
4bfc0 77 61 79 2c 0a 2a 2a 20 77 65 20 63 61 6e 20 61  way,.** we can a
4bfd0 6c 77 61 79 73 20 6c 6f 63 6b 20 61 6e 64 20 75  lways lock and u
4bfe0 6e 6c 6f 63 6b 20 74 68 65 6d 20 61 6c 6c 20 71  nlock them all q
4bff0 75 69 63 6b 6c 79 2e 0a 2a 2f 0a 73 74 72 75 63  uickly..*/.struc
4c000 74 20 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  t BtreeMutexArra
4c010 79 20 7b 0a 20 20 69 6e 74 20 6e 4d 75 74 65 78  y {.  int nMutex
4c020 3b 0a 20 20 42 74 72 65 65 20 2a 61 42 74 72 65  ;.  Btree *aBtre
4c030 65 5b 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54  e[SQLITE_MAX_ATT
4c040 41 43 48 45 44 2b 31 5d 3b 0a 7d 3b 0a 0a 0a 53  ACHED+1];.};...S
4c050 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4c060 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f 70  t sqlite3BtreeOp
4c070 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  en(.  const char
4c080 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20 2f   *zFilename,   /
4c090 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61  * Name of databa
4c0a0 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20  se file to open 
4c0b0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
4c0c0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
4c0d0 20 41 73 73 6f 63 69 61 74 65 64 20 64 61 74 61   Associated data
4c0e0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
4c0f0 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 42  */.  Btree **ppB
4c100 74 72 65 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  tree,         /*
4c110 20 52 65 74 75 72 6e 20 6f 70 65 6e 20 42 74 72   Return open Btr
4c120 65 65 2a 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e  ee* here */.  in
4c130 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  t flags,        
4c140 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
4c150 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67  */.  int vfsFlag
4c160 73 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  s             /*
4c170 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68   Flags passed th
4c180 72 6f 75 67 68 20 74 6f 20 56 46 53 20 6f 70 65  rough to VFS ope
4c190 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 54 68 65 20  n */.);../* The 
4c1a0 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20  flags parameter 
4c1b0 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f  to sqlite3BtreeO
4c1c0 70 65 6e 20 63 61 6e 20 62 65 20 74 68 65 20 62  pen can be the b
4c1d0 69 74 77 69 73 65 20 6f 72 20 6f 66 20 74 68 65  itwise or of the
4c1e0 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  .** following va
4c1f0 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54 45  lues..**.** NOTE
4c200 3a 20 20 54 68 65 73 65 20 76 61 6c 75 65 73 20  :  These values 
4c210 6d 75 73 74 20 6d 61 74 63 68 20 74 68 65 20 63  must match the c
4c220 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 50 41 47  orresponding PAG
4c230 45 52 5f 20 76 61 6c 75 65 73 20 69 6e 0a 2a 2a  ER_ values in.**
4c240 20 70 61 67 65 72 2e 68 2e 0a 2a 2f 0a 23 64 65   pager.h..*/.#de
4c250 66 69 6e 65 20 42 54 52 45 45 5f 4f 4d 49 54 5f  fine BTREE_OMIT_
4c260 4a 4f 55 52 4e 41 4c 20 20 31 20 20 2f 2a 20 44  JOURNAL  1  /* D
4c270 6f 20 6e 6f 74 20 75 73 65 20 6a 6f 75 72 6e 61  o not use journa
4c280 6c 2e 20 20 4e 6f 20 61 72 67 75 6d 65 6e 74 20  l.  No argument 
4c290 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45  */.#define BTREE
4c2a0 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20 32  _NO_READLOCK   2
4c2b0 20 20 2f 2a 20 4f 6d 69 74 20 72 65 61 64 6c 6f    /* Omit readlo
4c2c0 63 6b 73 20 6f 6e 20 72 65 61 64 6f 6e 6c 79 20  cks on readonly 
4c2d0 66 69 6c 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65  files */.#define
4c2e0 20 42 54 52 45 45 5f 4d 45 4d 4f 52 59 20 20 20   BTREE_MEMORY   
4c2f0 20 20 20 20 20 34 20 20 2f 2a 20 49 6e 2d 6d 65       4  /* In-me
4c300 6d 6f 72 79 20 44 42 2e 20 20 4e 6f 20 61 72 67  mory DB.  No arg
4c310 75 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65  ument */.#define
4c320 20 42 54 52 45 45 5f 52 45 41 44 4f 4e 4c 59 20   BTREE_READONLY 
4c330 20 20 20 20 20 38 20 20 2f 2a 20 4f 70 65 6e 20       8  /* Open 
4c340 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 20  the database in 
4c350 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 2a  read-only mode *
4c360 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f  /.#define BTREE_
4c370 52 45 41 44 57 52 49 54 45 20 20 20 20 31 36 20  READWRITE    16 
4c380 20 2f 2a 20 4f 70 65 6e 20 66 6f 72 20 62 6f 74   /* Open for bot
4c390 68 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72  h reading and wr
4c3a0 69 74 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65  iting */.#define
4c3b0 20 42 54 52 45 45 5f 43 52 45 41 54 45 20 20 20   BTREE_CREATE   
4c3c0 20 20 20 20 33 32 20 20 2f 2a 20 43 72 65 61 74      32  /* Creat
4c3d0 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  e the database i
4c3e0 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78  f it does not ex
4c3f0 69 73 74 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50  ist */..SQLITE_P
4c400 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4c410 65 33 42 74 72 65 65 43 6c 6f 73 65 28 42 74 72  e3BtreeClose(Btr
4c420 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ee*);.SQLITE_PRI
4c430 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4c440 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a  BtreeSetCacheSiz
4c450 65 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53  e(Btree*,int);.S
4c460 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4c470 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  t sqlite3BtreeSe
4c480 74 53 61 66 65 74 79 4c 65 76 65 6c 28 42 74 72  tSafetyLevel(Btr
4c490 65 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51  ee*,int,int);.SQ
4c4a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
4c4b0 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 79 6e   sqlite3BtreeSyn
4c4c0 63 44 69 73 61 62 6c 65 64 28 42 74 72 65 65 2a  cDisabled(Btree*
4c4d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4c4e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4c4f0 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 42 74  eeSetPageSize(Bt
4c500 72 65 65 20 2a 70 2c 20 69 6e 74 20 6e 50 61 67  ree *p, int nPag
4c510 65 73 69 7a 65 2c 20 69 6e 74 20 6e 52 65 73 65  esize, int nRese
4c520 72 76 65 2c 20 69 6e 74 20 65 46 69 78 29 3b 0a  rve, int eFix);.
4c530 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4c540 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  nt sqlite3BtreeG
4c550 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65  etPageSize(Btree
4c560 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
4c570 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
4c580 72 65 65 4d 61 78 50 61 67 65 43 6f 75 6e 74 28  reeMaxPageCount(
4c590 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c  Btree*,int);.SQL
4c5a0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4c5b0 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52  sqlite3BtreeGetR
4c5c0 65 73 65 72 76 65 28 42 74 72 65 65 2a 29 3b 0a  eserve(Btree*);.
4c5d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4c5e0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  nt sqlite3BtreeS
4c5f0 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72  etAutoVacuum(Btr
4c600 65 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ee *, int);.SQLI
4c610 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4c620 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 41 75  qlite3BtreeGetAu
4c630 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a  toVacuum(Btree *
4c640 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4c650 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4c660 65 65 42 65 67 69 6e 54 72 61 6e 73 28 42 74 72  eeBeginTrans(Btr
4c670 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  ee*,int);.SQLITE
4c680 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4c690 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50  ite3BtreeCommitP
4c6a0 68 61 73 65 4f 6e 65 28 42 74 72 65 65 2a 2c 20  haseOne(Btree*, 
4c6b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73  const char *zMas
4c6c0 74 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ter);.SQLITE_PRI
4c6d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
4c6e0 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
4c6f0 54 77 6f 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c  Two(Btree*);.SQL
4c700 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4c710 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
4c720 69 74 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49  it(Btree*);.SQLI
4c730 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4c740 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62  qlite3BtreeRollb
4c750 61 63 6b 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c  ack(Btree*);.SQL
4c760 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4c770 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69  sqlite3BtreeBegi
4c780 6e 53 74 6d 74 28 42 74 72 65 65 2a 2c 69 6e 74  nStmt(Btree*,int
4c790 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4c7a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4c7b0 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 42 74  eeCreateTable(Bt
4c7c0 72 65 65 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 20  ree*, int*, int 
4c7d0 66 6c 61 67 73 29 3b 0a 53 51 4c 49 54 45 5f 50  flags);.SQLITE_P
4c7e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4c7f0 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73  e3BtreeIsInTrans
4c800 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45  (Btree*);.SQLITE
4c810 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4c820 69 74 65 33 42 74 72 65 65 49 73 49 6e 52 65 61  ite3BtreeIsInRea
4c830 64 54 72 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a  dTrans(Btree*);.
4c840 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4c850 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  nt sqlite3BtreeI
4c860 73 49 6e 42 61 63 6b 75 70 28 42 74 72 65 65 2a  sInBackup(Btree*
4c870 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
4c880 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42  E void *sqlite3B
4c890 74 72 65 65 53 63 68 65 6d 61 28 42 74 72 65 65  treeSchema(Btree
4c8a0 20 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29   *, int, void(*)
4c8b0 28 76 6f 69 64 20 2a 29 29 3b 0a 53 51 4c 49 54  (void *));.SQLIT
4c8c0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4c8d0 6c 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61  lite3BtreeSchema
4c8e0 4c 6f 63 6b 65 64 28 42 74 72 65 65 20 2a 70 42  Locked(Btree *pB
4c8f0 74 72 65 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  tree);.SQLITE_PR
4c900 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4c910 33 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28  3BtreeLockTable(
4c920 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c 20 69  Btree *pBtree, i
4c930 6e 74 20 69 54 61 62 2c 20 75 38 20 69 73 57 72  nt iTab, u8 isWr
4c940 69 74 65 4c 6f 63 6b 29 3b 0a 53 51 4c 49 54 45  iteLock);.SQLITE
4c950 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4c960 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f 69  ite3BtreeSavepoi
4c970 6e 74 28 42 74 72 65 65 20 2a 2c 20 69 6e 74 2c  nt(Btree *, int,
4c980 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50   int);..SQLITE_P
4c990 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
4c9a0 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47  r *sqlite3BtreeG
4c9b0 65 74 46 69 6c 65 6e 61 6d 65 28 42 74 72 65 65  etFilename(Btree
4c9c0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
4c9d0 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
4c9e0 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4a  sqlite3BtreeGetJ
4c9f0 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72 65 65  ournalname(Btree
4ca00 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
4ca10 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
4ca20 74 72 65 65 43 6f 70 79 46 69 6c 65 28 42 74 72  treeCopyFile(Btr
4ca30 65 65 20 2a 2c 20 42 74 72 65 65 20 2a 29 3b 0a  ee *, Btree *);.
4ca40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4ca50 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4ca60 49 6e 63 72 56 61 63 75 75 6d 28 42 74 72 65 65  IncrVacuum(Btree
4ca70 20 2a 29 3b 0a 0a 2f 2a 20 54 68 65 20 66 6c 61   *);../* The fla
4ca80 67 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  gs parameter to 
4ca90 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61  sqlite3BtreeCrea
4caa0 74 65 54 61 62 6c 65 20 63 61 6e 20 62 65 20 74  teTable can be t
4cab0 68 65 20 62 69 74 77 69 73 65 20 4f 52 0a 2a 2a  he bitwise OR.**
4cac0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
4cad0 67 20 66 6c 61 67 73 3a 0a 2a 2f 0a 23 64 65 66  g flags:.*/.#def
4cae0 69 6e 65 20 42 54 52 45 45 5f 49 4e 54 4b 45 59  ine BTREE_INTKEY
4caf0 20 20 20 20 20 31 20 20 20 20 2f 2a 20 54 61 62       1    /* Tab
4cb00 6c 65 20 68 61 73 20 6f 6e 6c 79 20 36 34 2d 62  le has only 64-b
4cb10 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  it signed intege
4cb20 72 20 6b 65 79 73 20 2a 2f 0a 23 64 65 66 69 6e  r keys */.#defin
4cb30 65 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41  e BTREE_ZERODATA
4cb40 20 20 20 32 20 20 20 20 2f 2a 20 54 61 62 6c 65     2    /* Table
4cb50 20 68 61 73 20 6b 65 79 73 20 6f 6e 6c 79 20 2d   has keys only -
4cb60 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 23 64 65 66   no data */.#def
4cb70 69 6e 65 20 42 54 52 45 45 5f 4c 45 41 46 44 41  ine BTREE_LEAFDA
4cb80 54 41 20 20 20 34 20 20 20 20 2f 2a 20 44 61 74  TA   4    /* Dat
4cb90 61 20 73 74 6f 72 65 64 20 69 6e 20 6c 65 61 76  a stored in leav
4cba0 65 73 20 6f 6e 6c 79 2e 20 20 49 6d 70 6c 69 65  es only.  Implie
4cbb0 73 20 49 4e 54 4b 45 59 20 2a 2f 0a 0a 53 51 4c  s INTKEY */..SQL
4cbc0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
4cbd0 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70  sqlite3BtreeDrop
4cbe0 54 61 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e  Table(Btree*, in
4cbf0 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45  t, int*);.SQLITE
4cc00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4cc10 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61  ite3BtreeClearTa
4cc20 62 6c 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2c  ble(Btree*, int,
4cc30 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50   int*);.SQLITE_P
4cc40 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
4cc50 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43  te3BtreeTripAllC
4cc60 75 72 73 6f 72 73 28 42 74 72 65 65 2a 2c 20 69  ursors(Btree*, i
4cc70 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  nt);..SQLITE_PRI
4cc80 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
4cc90 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 42 74  3BtreeGetMeta(Bt
4cca0 72 65 65 20 2a 70 42 74 72 65 65 2c 20 69 6e 74  ree *pBtree, int
4ccb0 20 69 64 78 2c 20 75 33 32 20 2a 70 56 61 6c 75   idx, u32 *pValu
4ccc0 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  e);.SQLITE_PRIVA
4ccd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
4cce0 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 42 74  reeUpdateMeta(Bt
4ccf0 72 65 65 2a 2c 20 69 6e 74 20 69 64 78 2c 20 75  ree*, int idx, u
4cd00 33 32 20 76 61 6c 75 65 29 3b 0a 0a 2f 2a 0a 2a  32 value);../*.*
4cd10 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72  * The second par
4cd20 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65  ameter to sqlite
4cd30 33 42 74 72 65 65 47 65 74 4d 65 74 61 20 6f 72  3BtreeGetMeta or
4cd40 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64   sqlite3BtreeUpd
4cd50 61 74 65 4d 65 74 61 0a 2a 2a 20 73 68 6f 75 6c  ateMeta.** shoul
4cd60 64 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65 20  d be one of the 
4cd70 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73  following values
4cd80 2e 20 54 68 65 20 69 6e 74 65 67 65 72 20 76 61  . The integer va
4cd90 6c 75 65 73 20 61 72 65 20 61 73 73 69 67 6e 65  lues are assigne
4cda0 64 20 0a 2a 2a 20 74 6f 20 63 6f 6e 73 74 61 6e  d .** to constan
4cdb0 74 73 20 73 6f 20 74 68 61 74 20 74 68 65 20 6f  ts so that the o
4cdc0 66 66 73 65 74 20 6f 66 20 74 68 65 20 63 6f 72  ffset of the cor
4cdd0 72 65 73 70 6f 6e 64 69 6e 67 20 66 69 65 6c 64  responding field
4cde0 20 69 6e 20 61 6e 0a 2a 2a 20 53 51 4c 69 74 65   in an.** SQLite
4cdf0 20 64 61 74 61 62 61 73 65 20 68 65 61 64 65 72   database header
4ce00 20 6d 61 79 20 62 65 20 66 6f 75 6e 64 20 75 73   may be found us
4ce10 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ing the followin
4ce20 67 20 66 6f 72 6d 75 6c 61 3a 0a 2a 2a 0a 2a 2a  g formula:.**.**
4ce30 20 20 20 6f 66 66 73 65 74 20 3d 20 33 36 20 2b     offset = 36 +
4ce40 20 28 69 64 78 20 2a 20 34 29 0a 2a 2a 0a 2a 2a   (idx * 4).**.**
4ce50 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68   For example, th
4ce60 65 20 66 72 65 65 2d 70 61 67 65 2d 63 6f 75 6e  e free-page-coun
4ce70 74 20 66 69 65 6c 64 20 69 73 20 6c 6f 63 61 74  t field is locat
4ce80 65 64 20 61 74 20 62 79 74 65 20 6f 66 66 73 65  ed at byte offse
4ce90 74 20 33 36 20 6f 66 0a 2a 2a 20 74 68 65 20 64  t 36 of.** the d
4cea0 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 65 61  atabase file hea
4ceb0 64 65 72 2e 20 54 68 65 20 69 6e 63 72 2d 76 61  der. The incr-va
4cec0 63 75 75 6d 2d 66 6c 61 67 20 66 69 65 6c 64 20  cuum-flag field 
4ced0 69 73 20 6c 6f 63 61 74 65 64 20 61 74 0a 2a 2a  is located at.**
4cee0 20 62 79 74 65 20 6f 66 66 73 65 74 20 36 34 20   byte offset 64 
4cef0 28 3d 3d 20 33 36 2b 34 2a 37 29 2e 0a 2a 2f 0a  (== 36+4*7)..*/.
4cf00 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 46 52  #define BTREE_FR
4cf10 45 45 5f 50 41 47 45 5f 43 4f 55 4e 54 20 20 20  EE_PAGE_COUNT   
4cf20 20 20 30 0a 23 64 65 66 69 6e 65 20 42 54 52 45    0.#define BTRE
4cf30 45 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f 4e  E_SCHEMA_VERSION
4cf40 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20        1.#define 
4cf50 42 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41  BTREE_FILE_FORMA
4cf60 54 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66  T         2.#def
4cf70 69 6e 65 20 42 54 52 45 45 5f 44 45 46 41 55 4c  ine BTREE_DEFAUL
4cf80 54 5f 43 41 43 48 45 5f 53 49 5a 45 20 20 33 0a  T_CACHE_SIZE  3.
4cf90 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4c 41  #define BTREE_LA
4cfa0 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41 47 45 20  RGEST_ROOT_PAGE 
4cfb0 20 20 34 0a 23 64 65 66 69 6e 65 20 42 54 52 45    4.#define BTRE
4cfc0 45 5f 54 45 58 54 5f 45 4e 43 4f 44 49 4e 47 20  E_TEXT_ENCODING 
4cfd0 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20        5.#define 
4cfe0 42 54 52 45 45 5f 55 53 45 52 5f 56 45 52 53 49  BTREE_USER_VERSI
4cff0 4f 4e 20 20 20 20 20 20 20 20 36 0a 23 64 65 66  ON        6.#def
4d000 69 6e 65 20 42 54 52 45 45 5f 49 4e 43 52 5f 56  ine BTREE_INCR_V
4d010 41 43 55 55 4d 20 20 20 20 20 20 20 20 20 37 0a  ACUUM         7.
4d020 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4d030 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4d040 43 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 2a  Cursor(.  Btree*
4d050 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
4d060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
4d070 2a 20 42 54 72 65 65 20 63 6f 6e 74 61 69 6e 69  * BTree containi
4d080 6e 67 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e  ng table to open
4d090 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65   */.  int iTable
4d0a0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
4d0b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
4d0c0 64 65 78 20 6f 66 20 72 6f 6f 74 20 70 61 67 65  dex of root page
4d0d0 20 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67   */.  int wrFlag
4d0e0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
4d0f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20             /* 1 
4d100 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 20 30 20  for writing.  0 
4d110 66 6f 72 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f  for read-only */
4d120 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66  .  struct KeyInf
4d130 6f 2a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  o*,             
4d140 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
4d150 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 63 6f 6d   argument to com
4d160 70 61 72 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  pare function */
4d170 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75  .  BtCursor *pCu
4d180 72 73 6f 72 20 20 20 20 20 20 20 20 20 20 20 20  rsor            
4d190 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65          /* Space
4d1a0 20 74 6f 20 77 72 69 74 65 20 63 75 72 73 6f 72   to write cursor
4d1b0 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29 3b   structure */.);
4d1c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4d1d0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4d1e0 43 75 72 73 6f 72 53 69 7a 65 28 76 6f 69 64 29  CursorSize(void)
4d1f0 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ;..SQLITE_PRIVAT
4d200 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
4d210 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 42 74  eeCloseCursor(Bt
4d220 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45  Cursor*);.SQLITE
4d230 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
4d240 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55  ite3BtreeMovetoU
4d250 6e 70 61 63 6b 65 64 28 0a 20 20 42 74 43 75 72  npacked(.  BtCur
4d260 73 6f 72 2a 2c 0a 20 20 55 6e 70 61 63 6b 65 64  sor*,.  Unpacked
4d270 52 65 63 6f 72 64 20 2a 70 55 6e 4b 65 79 2c 0a  Record *pUnKey,.
4d280 20 20 69 36 34 20 69 6e 74 4b 65 79 2c 0a 20 20    i64 intKey,.  
4d290 69 6e 74 20 62 69 61 73 2c 0a 20 20 69 6e 74 20  int bias,.  int 
4d2a0 2a 70 52 65 73 0a 29 3b 0a 53 51 4c 49 54 45 5f  *pRes.);.SQLITE_
4d2b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4d2c0 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 48 61  te3BtreeCursorHa
4d2d0 73 4d 6f 76 65 64 28 42 74 43 75 72 73 6f 72 2a  sMoved(BtCursor*
4d2e0 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  , int*);.SQLITE_
4d2f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4d300 74 65 33 42 74 72 65 65 44 65 6c 65 74 65 28 42  te3BtreeDelete(B
4d310 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49 54  tCursor*);.SQLIT
4d320 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4d330 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74  lite3BtreeInsert
4d340 28 42 74 43 75 72 73 6f 72 2a 2c 20 63 6f 6e 73  (BtCursor*, cons
4d350 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36  t void *pKey, i6
4d360 34 20 6e 4b 65 79 2c 0a 20 20 20 20 20 20 20 20  4 nKey,.        
4d370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d380 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
4d390 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74  void *pData, int
4d3a0 20 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20   nData,.        
4d3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4d3c0 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 5a            int nZ
4d3d0 65 72 6f 2c 20 69 6e 74 20 62 69 61 73 2c 20 69  ero, int bias, i
4d3e0 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 29 3b 0a  nt seekResult);.
4d3f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4d400 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46  nt sqlite3BtreeF
4d410 69 72 73 74 28 42 74 43 75 72 73 6f 72 2a 2c 20  irst(BtCursor*, 
4d420 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49  int *pRes);.SQLI
4d430 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
4d440 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74 28  qlite3BtreeLast(
4d450 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a  BtCursor*, int *
4d460 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52  pRes);.SQLITE_PR
4d470 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
4d480 33 42 74 72 65 65 4e 65 78 74 28 42 74 43 75 72  3BtreeNext(BtCur
4d490 73 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29  sor*, int *pRes)
4d4a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4d4b0 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
4d4c0 65 45 6f 66 28 42 74 43 75 72 73 6f 72 2a 29 3b  eEof(BtCursor*);
4d4d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4d4e0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4d4f0 50 72 65 76 69 6f 75 73 28 42 74 43 75 72 73 6f  Previous(BtCurso
4d500 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b 0a  r*, int *pRes);.
4d510 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
4d520 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b  nt sqlite3BtreeK
4d530 65 79 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a  eySize(BtCursor*
4d540 2c 20 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53  , i64 *pSize);.S
4d550 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4d560 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65  t sqlite3BtreeKe
4d570 79 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32  y(BtCursor*, u32
4d580 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74   offset, u32 amt
4d590 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45  , void*);.SQLITE
4d5a0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76  _PRIVATE const v
4d5b0 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65  oid *sqlite3Btre
4d5c0 65 4b 65 79 46 65 74 63 68 28 42 74 43 75 72 73  eKeyFetch(BtCurs
4d5d0 6f 72 2a 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b  or*, int *pAmt);
4d5e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
4d5f0 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
4d600 74 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63  te3BtreeDataFetc
4d610 68 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74  h(BtCursor*, int
4d620 20 2a 70 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f   *pAmt);.SQLITE_
4d630 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4d640 74 65 33 42 74 72 65 65 44 61 74 61 53 69 7a 65  te3BtreeDataSize
4d650 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20  (BtCursor*, u32 
4d660 2a 70 53 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f  *pSize);.SQLITE_
4d670 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
4d680 74 65 33 42 74 72 65 65 44 61 74 61 28 42 74 43  te3BtreeData(BtC
4d690 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66 66 73  ursor*, u32 offs
4d6a0 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69  et, u32 amt, voi
4d6b0 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  d*);.SQLITE_PRIV
4d6c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
4d6d0 42 74 72 65 65 53 65 74 43 61 63 68 65 64 52 6f  BtreeSetCachedRo
4d6e0 77 69 64 28 42 74 43 75 72 73 6f 72 2a 2c 20 73  wid(BtCursor*, s
4d6f0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53  qlite3_int64);.S
4d700 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71  QLITE_PRIVATE sq
4d710 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69  lite3_int64 sqli
4d720 74 65 33 42 74 72 65 65 47 65 74 43 61 63 68 65  te3BtreeGetCache
4d730 64 52 6f 77 69 64 28 42 74 43 75 72 73 6f 72 2a  dRowid(BtCursor*
4d740 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
4d750 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
4d760 42 74 72 65 65 49 6e 74 65 67 72 69 74 79 43 68  BtreeIntegrityCh
4d770 65 63 6b 28 42 74 72 65 65 2a 2c 20 69 6e 74 20  eck(Btree*, int 
4d780 2a 61 52 6f 6f 74 2c 20 69 6e 74 20 6e 52 6f 6f  *aRoot, int nRoo
4d790 74 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53  t, int, int*);.S
4d7a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 74  QLITE_PRIVATE st
4d7b0 72 75 63 74 20 50 61 67 65 72 20 2a 73 71 6c 69  ruct Pager *sqli
4d7c0 74 65 33 42 74 72 65 65 50 61 67 65 72 28 42 74  te3BtreePager(Bt
4d7d0 72 65 65 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50  ree*);..SQLITE_P
4d7e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
4d7f0 65 33 42 74 72 65 65 50 75 74 44 61 74 61 28 42  e3BtreePutData(B
4d800 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 20 6f 66  tCursor*, u32 of
4d810 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76  fset, u32 amt, v
4d820 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  oid*);.SQLITE_PR
4d830 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
4d840 65 33 42 74 72 65 65 43 61 63 68 65 4f 76 65 72  e3BtreeCacheOver
4d850 66 6c 6f 77 28 42 74 43 75 72 73 6f 72 20 2a 29  flow(BtCursor *)
4d860 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4d870 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
4d880 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 42 74  eeClearCursor(Bt
4d890 43 75 72 73 6f 72 20 2a 29 3b 0a 0a 23 69 66 6e  Cursor *);..#ifn
4d8a0 64 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54  def NDEBUG.SQLIT
4d8b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4d8c0 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
4d8d0 49 73 56 61 6c 69 64 28 42 74 43 75 72 73 6f 72  IsValid(BtCursor
4d8e0 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  *);.#endif..#ifn
4d8f0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
4d900 42 54 52 45 45 43 4f 55 4e 54 0a 53 51 4c 49 54  BTREECOUNT.SQLIT
4d910 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
4d920 6c 69 74 65 33 42 74 72 65 65 43 6f 75 6e 74 28  lite3BtreeCount(
4d930 42 74 43 75 72 73 6f 72 20 2a 2c 20 69 36 34 20  BtCursor *, i64 
4d940 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  *);.#endif..#ifd
4d950 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
4d960 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
4d970 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  t sqlite3BtreeCu
4d980 72 73 6f 72 49 6e 66 6f 28 42 74 43 75 72 73 6f  rsorInfo(BtCurso
4d990 72 2a 2c 20 69 6e 74 2a 2c 20 69 6e 74 29 3b 0a  r*, int*, int);.
4d9a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
4d9b0 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
4d9c0 43 75 72 73 6f 72 4c 69 73 74 28 42 74 72 65 65  CursorList(Btree
4d9d0 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  *);.#endif../*.*
4d9e0 2a 20 49 66 20 77 65 20 61 72 65 20 6e 6f 74 20  * If we are not 
4d9f0 75 73 69 6e 67 20 73 68 61 72 65 64 20 63 61 63  using shared cac
4da00 68 65 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69  he, then there i
4da10 73 20 6e 6f 20 6e 65 65 64 20 74 6f 0a 2a 2a 20  s no need to.** 
4da20 75 73 65 20 6d 75 74 65 78 65 73 20 74 6f 20 61  use mutexes to a
4da30 63 63 65 73 73 20 74 68 65 20 42 74 53 68 61 72  ccess the BtShar
4da40 65 64 20 73 74 72 75 63 74 75 72 65 73 2e 20 20  ed structures.  
4da50 53 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 45  So make the.** E
4da60 6e 74 65 72 20 61 6e 64 20 4c 65 61 76 65 20 70  nter and Leave p
4da70 72 6f 63 65 64 75 72 65 73 20 6e 6f 2d 6f 70 73  rocedures no-ops
4da80 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
4da90 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
4daa0 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 49  CACHE.SQLITE_PRI
4dab0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
4dac0 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 42 74  te3BtreeEnter(Bt
4dad0 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ree*);.SQLITE_PR
4dae0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
4daf0 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c  ite3BtreeEnterAl
4db00 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c  l(sqlite3*);.#el
4db10 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  se.# define sqli
4db20 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 58 29  te3BtreeEnter(X)
4db30 20 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74   .# define sqlit
4db40 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28  e3BtreeEnterAll(
4db50 58 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21  X).#endif..#if !
4db60 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
4db70 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
4db80 29 20 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45  ) && SQLITE_THRE
4db90 41 44 53 41 46 45 0a 53 51 4c 49 54 45 5f 50 52  ADSAFE.SQLITE_PR
4dba0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
4dbb0 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 42  ite3BtreeLeave(B
4dbc0 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  tree*);.SQLITE_P
4dbd0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
4dbe0 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 43  lite3BtreeEnterC
4dbf0 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29  ursor(BtCursor*)
4dc00 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4dc10 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42     void sqlite3B
4dc20 74 72 65 65 4c 65 61 76 65 43 75 72 73 6f 72 28  treeLeaveCursor(
4dc30 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c 49  BtCursor*);.SQLI
4dc40 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
4dc50 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65  d sqlite3BtreeLe
4dc60 61 76 65 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29  aveAll(sqlite3*)
4dc70 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
4dc80 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42     void sqlite3B
4dc90 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e  treeMutexArrayEn
4dca0 74 65 72 28 42 74 72 65 65 4d 75 74 65 78 41 72  ter(BtreeMutexAr
4dcb0 72 61 79 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ray*);.SQLITE_PR
4dcc0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
4dcd0 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72  ite3BtreeMutexAr
4dce0 72 61 79 4c 65 61 76 65 28 42 74 72 65 65 4d 75  rayLeave(BtreeMu
4dcf0 74 65 78 41 72 72 61 79 2a 29 3b 0a 53 51 4c 49  texArray*);.SQLI
4dd00 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
4dd10 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75  d sqlite3BtreeMu
4dd20 74 65 78 41 72 72 61 79 49 6e 73 65 72 74 28 42  texArrayInsert(B
4dd30 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 2a 2c  treeMutexArray*,
4dd40 20 42 74 72 65 65 2a 29 3b 0a 23 69 66 6e 64 65   Btree*);.#ifnde
4dd50 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54 68  f NDEBUG.  /* Th
4dd60 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ese routines are
4dd70 20 75 73 65 64 20 69 6e 73 69 64 65 20 61 73 73   used inside ass
4dd80 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73  ert() statements
4dd90 20 6f 6e 6c 79 2e 20 2a 2f 0a 53 51 4c 49 54 45   only. */.SQLITE
4dda0 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
4ddb0 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
4ddc0 4d 75 74 65 78 28 42 74 72 65 65 2a 29 3b 0a 53  Mutex(Btree*);.S
4ddd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
4dde0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
4ddf0 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28  HoldsAllMutexes(
4de00 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6e 64 69  sqlite3*);.#endi
4de10 66 0a 23 65 6c 73 65 0a 0a 23 20 64 65 66 69 6e  f.#else..# defin
4de20 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65  e sqlite3BtreeLe
4de30 61 76 65 28 58 29 0a 23 20 64 65 66 69 6e 65 20  ave(X).# define 
4de40 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
4de50 72 43 75 72 73 6f 72 28 58 29 0a 23 20 64 65 66  rCursor(X).# def
4de60 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65  ine sqlite3Btree
4de70 4c 65 61 76 65 43 75 72 73 6f 72 28 58 29 0a 23  LeaveCursor(X).#
4de80 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
4de90 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 58 29 0a  treeLeaveAll(X).
4dea0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
4deb0 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45  BtreeMutexArrayE
4dec0 6e 74 65 72 28 58 29 0a 23 20 64 65 66 69 6e 65  nter(X).# define
4ded0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74   sqlite3BtreeMut
4dee0 65 78 41 72 72 61 79 4c 65 61 76 65 28 58 29 0a  exArrayLeave(X).
4def0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
4df00 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49  BtreeMutexArrayI
4df10 6e 73 65 72 74 28 58 2c 59 29 0a 0a 23 20 64 65  nsert(X,Y)..# de
4df20 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65  fine sqlite3Btre
4df30 65 48 6f 6c 64 73 4d 75 74 65 78 28 58 29 20 31  eHoldsMutex(X) 1
4df40 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
4df50 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75  3BtreeHoldsAllMu
4df60 74 65 78 65 73 28 58 29 20 31 0a 23 65 6e 64 69  texes(X) 1.#endi
4df70 66 0a 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 42  f...#endif /* _B
4df80 54 52 45 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a  TREE_H_ */../***
4df90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
4dfa0 6f 66 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a  of btree.h *****
4dfb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dfc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4dfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
4dfe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
4dff0 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
4e000 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69  left off in sqli
4e010 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
4e020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
4e030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
4e040 75 64 65 20 76 64 62 65 2e 68 20 69 6e 20 74 68  ude vdbe.h in th
4e050 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69  e middle of sqli
4e060 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
4e070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
4e080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
4e090 6e 20 66 69 6c 65 20 76 64 62 65 2e 68 20 2a 2a  n file vdbe.h **
4e0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
4e0d0 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
4e0e0 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
4e0f0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
4e100 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
4e110 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
4e120 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
4e130 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
4e140 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
4e150 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
4e160 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
4e170 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
4e180 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
4e190 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
4e1a0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
4e1b0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
4e1c0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
4e1d0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
4e1e0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
4e1f0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
4e200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4e240 2a 2a 0a 2a 2a 20 48 65 61 64 65 72 20 66 69 6c  **.** Header fil
4e250 65 20 66 6f 72 20 74 68 65 20 56 69 72 74 75 61  e for the Virtua
4e260 6c 20 44 61 74 61 42 61 73 65 20 45 6e 67 69 6e  l DataBase Engin
4e270 65 20 28 56 44 42 45 29 0a 2a 2a 0a 2a 2a 20 54  e (VDBE).**.** T
4e280 68 69 73 20 68 65 61 64 65 72 20 64 65 66 69 6e  his header defin
4e290 65 73 20 74 68 65 20 69 6e 74 65 72 66 61 63 65  es the interface
4e2a0 20 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20   to the virtual 
4e2b0 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 0a  database engine.
4e2c0 2a 2a 20 6f 72 20 56 44 42 45 2e 20 20 54 68 65  ** or VDBE.  The
4e2d0 20 56 44 42 45 20 69 6d 70 6c 65 6d 65 6e 74 73   VDBE implements
4e2e0 20 61 6e 20 61 62 73 74 72 61 63 74 20 6d 61 63   an abstract mac
4e2f0 68 69 6e 65 20 74 68 61 74 20 72 75 6e 73 20 61  hine that runs a
4e300 0a 2a 2a 20 73 69 6d 70 6c 65 20 70 72 6f 67 72  .** simple progr
4e310 61 6d 20 74 6f 20 61 63 63 65 73 73 20 61 6e 64  am to access and
4e320 20 6d 6f 64 69 66 79 20 74 68 65 20 75 6e 64 65   modify the unde
4e330 72 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 2e  rlying database.
4e340 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65  .**.** $Id: vdbe
4e350 2e 68 2c 76 20 31 2e 31 34 32 20 32 30 30 39 2f  .h,v 1.142 2009/
4e360 30 37 2f 32 34 20 31 37 3a 35 38 3a 35 33 20 64  07/24 17:58:53 d
4e370 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
4e380 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c  .*/.#ifndef _SQL
4e390 49 54 45 5f 56 44 42 45 5f 48 5f 0a 23 64 65 66  ITE_VDBE_H_.#def
4e3a0 69 6e 65 20 5f 53 51 4c 49 54 45 5f 56 44 42 45  ine _SQLITE_VDBE
4e3b0 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e  _H_../*.** A sin
4e3c0 67 6c 65 20 56 44 42 45 20 69 73 20 61 6e 20 6f  gle VDBE is an o
4e3d0 70 61 71 75 65 20 73 74 72 75 63 74 75 72 65 20  paque structure 
4e3e0 6e 61 6d 65 64 20 22 56 64 62 65 22 2e 20 20 4f  named "Vdbe".  O
4e3f0 6e 6c 79 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20  nly routines.** 
4e400 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 66 69  in the source fi
4e410 6c 65 20 73 71 6c 69 74 65 56 64 62 65 2e 63 20  le sqliteVdbe.c 
4e420 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 73  are allowed to s
4e430 65 65 20 74 68 65 20 69 6e 73 69 64 65 73 0a 2a  ee the insides.*
4e440 2a 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  * of this struct
4e450 75 72 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ure..*/.typedef 
4e460 73 74 72 75 63 74 20 56 64 62 65 20 56 64 62 65  struct Vdbe Vdbe
4e470 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d  ;../*.** The nam
4e480 65 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  es of the follow
4e490 69 6e 67 20 74 79 70 65 73 20 64 65 63 6c 61 72  ing types declar
4e4a0 65 64 20 69 6e 20 76 64 62 65 49 6e 74 2e 68 20  ed in vdbeInt.h 
4e4b0 61 72 65 20 72 65 71 75 69 72 65 64 0a 2a 2a 20  are required.** 
4e4c0 66 6f 72 20 74 68 65 20 56 64 62 65 4f 70 20 64  for the VdbeOp d
4e4d0 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 74 79  efinition..*/.ty
4e4e0 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62  pedef struct Vdb
4e4f0 65 46 75 6e 63 20 56 64 62 65 46 75 6e 63 3b 0a  eFunc VdbeFunc;.
4e500 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d  typedef struct M
4e510 65 6d 20 4d 65 6d 3b 0a 74 79 70 65 64 65 66 20  em Mem;.typedef 
4e520 73 74 72 75 63 74 20 53 75 62 50 72 6f 67 72 61  struct SubProgra
4e530 6d 20 53 75 62 50 72 6f 67 72 61 6d 3b 0a 0a 2f  m SubProgram;../
4e540 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e  *.** A single in
4e550 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 65  struction of the
4e560 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
4e570 20 68 61 73 20 61 6e 20 6f 70 63 6f 64 65 0a 2a   has an opcode.*
4e580 2a 20 61 6e 64 20 61 73 20 6d 61 6e 79 20 61 73  * and as many as
4e590 20 74 68 72 65 65 20 6f 70 65 72 61 6e 64 73 2e   three operands.
4e5a0 20 20 54 68 65 20 69 6e 73 74 72 75 63 74 69 6f    The instructio
4e5b0 6e 20 69 73 20 72 65 63 6f 72 64 65 64 0a 2a 2a  n is recorded.**
4e5c0 20 61 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   as an instance 
4e5d0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
4e5e0 20 73 74 72 75 63 74 75 72 65 3a 0a 2a 2f 0a 73   structure:.*/.s
4e5f0 74 72 75 63 74 20 56 64 62 65 4f 70 20 7b 0a 20  truct VdbeOp {. 
4e600 20 75 38 20 6f 70 63 6f 64 65 3b 20 20 20 20 20   u8 opcode;     
4e610 20 20 20 20 20 2f 2a 20 57 68 61 74 20 6f 70 65       /* What ope
4e620 72 61 74 69 6f 6e 20 74 6f 20 70 65 72 66 6f 72  ration to perfor
4e630 6d 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68  m */.  signed ch
4e640 61 72 20 70 34 74 79 70 65 3b 20 2f 2a 20 4f 6e  ar p4type; /* On
4e650 65 20 6f 66 20 74 68 65 20 50 34 5f 78 78 78 20  e of the P4_xxx 
4e660 63 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 70 34  constants for p4
4e670 20 2a 2f 0a 20 20 75 38 20 6f 70 66 6c 61 67 73   */.  u8 opflags
4e680 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74  ;         /* Not
4e690 20 63 75 72 72 65 6e 74 6c 79 20 75 73 65 64 20   currently used 
4e6a0 2a 2f 0a 20 20 75 38 20 70 35 3b 20 20 20 20 20  */.  u8 p5;     
4e6b0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 66 74           /* Fift
4e6c0 68 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61  h parameter is a
4e6d0 6e 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61  n unsigned chara
4e6e0 63 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 31  cter */.  int p1
4e6f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
4e700 20 46 69 72 73 74 20 6f 70 65 72 61 6e 64 20 2a   First operand *
4e710 2f 0a 20 20 69 6e 74 20 70 32 3b 20 20 20 20 20  /.  int p2;     
4e720 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e          /* Secon
4e730 64 20 70 61 72 61 6d 65 74 65 72 20 28 6f 66 74  d parameter (oft
4e740 65 6e 20 74 68 65 20 6a 75 6d 70 20 64 65 73 74  en the jump dest
4e750 69 6e 61 74 69 6f 6e 29 20 2a 2f 0a 20 20 69 6e  ination) */.  in
4e760 74 20 70 33 3b 20 20 20 20 20 20 20 20 20 20 20  t p3;           
4e770 20 20 2f 2a 20 54 68 65 20 74 68 69 72 64 20 70    /* The third p
4e780 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 75 6e  arameter */.  un
4e790 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20 20 20  ion {           
4e7a0 20 20 2f 2a 20 66 6f 75 72 74 68 20 70 61 72 61    /* fourth para
4e7b0 6d 65 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74  meter */.    int
4e7c0 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
4e7d0 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76      /* Integer v
4e7e0 61 6c 75 65 20 69 66 20 70 34 74 79 70 65 3d 3d  alue if p4type==
4e7f0 50 34 5f 49 4e 54 33 32 20 2a 2f 0a 20 20 20 20  P4_INT32 */.    
4e800 76 6f 69 64 20 2a 70 3b 20 20 20 20 20 20 20 20  void *p;        
4e810 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 69         /* Generi
4e820 63 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 20  c pointer */.   
4e830 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20   char *z;       
4e840 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
4e850 65 72 20 74 6f 20 64 61 74 61 20 66 6f 72 20 73  er to data for s
4e860 74 72 69 6e 67 20 28 63 68 61 72 20 61 72 72 61  tring (char arra
4e870 79 29 20 74 79 70 65 73 20 2a 2f 0a 20 20 20 20  y) types */.    
4e880 69 36 34 20 2a 70 49 36 34 3b 20 20 20 20 20 20  i64 *pI64;      
4e890 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77         /* Used w
4e8a0 68 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34  hen p4type is P4
4e8b0 5f 49 4e 54 36 34 20 2a 2f 0a 20 20 20 20 64 6f  _INT64 */.    do
4e8c0 75 62 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20  uble *pReal;    
4e8d0 20 20 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65       /* Used whe
4e8e0 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f 52  n p4type is P4_R
4e8f0 45 41 4c 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44  EAL */.    FuncD
4e900 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20  ef *pFunc;      
4e910 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70    /* Used when p
4e920 34 74 79 70 65 20 69 73 20 50 34 5f 46 55 4e 43  4type is P4_FUNC
4e930 44 45 46 20 2a 2f 0a 20 20 20 20 56 64 62 65 46  DEF */.    VdbeF
4e940 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 3b 20  unc *pVdbeFunc; 
4e950 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 70    /* Used when p
4e960 34 74 79 70 65 20 69 73 20 50 34 5f 56 44 42 45  4type is P4_VDBE
4e970 46 55 4e 43 20 2a 2f 0a 20 20 20 20 43 6f 6c 6c  FUNC */.    Coll
4e980 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20  Seq *pColl;     
4e990 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20     /* Used when 
4e9a0 70 34 74 79 70 65 20 69 73 20 50 34 5f 43 4f 4c  p4type is P4_COL
4e9b0 4c 53 45 51 20 2a 2f 0a 20 20 20 20 4d 65 6d 20  LSEQ */.    Mem 
4e9c0 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20  *pMem;          
4e9d0 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20     /* Used when 
4e9e0 70 34 74 79 70 65 20 69 73 20 50 34 5f 4d 45 4d  p4type is P4_MEM
4e9f0 20 2a 2f 0a 20 20 20 20 56 54 61 62 6c 65 20 2a   */.    VTable *
4ea00 70 56 74 61 62 3b 20 20 20 20 20 20 20 20 20 2f  pVtab;         /
4ea10 2a 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79  * Used when p4ty
4ea20 70 65 20 69 73 20 50 34 5f 56 54 41 42 20 2a 2f  pe is P4_VTAB */
4ea30 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  .    KeyInfo *pK
4ea40 65 79 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 55  eyInfo;     /* U
4ea50 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20  sed when p4type 
4ea60 69 73 20 50 34 5f 4b 45 59 49 4e 46 4f 20 2a 2f  is P4_KEYINFO */
4ea70 0a 20 20 20 20 69 6e 74 20 2a 61 69 3b 20 20 20  .    int *ai;   
4ea80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55              /* U
4ea90 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20  sed when p4type 
4eaa0 69 73 20 50 34 5f 49 4e 54 41 52 52 41 59 20 2a  is P4_INTARRAY *
4eab0 2f 0a 20 20 20 20 53 75 62 50 72 6f 67 72 61 6d  /.    SubProgram
4eac0 20 2a 70 50 72 6f 67 72 61 6d 3b 20 20 2f 2a 20   *pProgram;  /* 
4ead0 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65  Used when p4type
4eae0 20 69 73 20 50 34 5f 53 55 42 50 52 4f 47 52 41   is P4_SUBPROGRA
4eaf0 4d 20 2a 2f 0a 20 20 7d 20 70 34 3b 0a 23 69 66  M */.  } p4;.#if
4eb00 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
4eb10 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6d 6d 65 6e  .  char *zCommen
4eb20 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43  t;          /* C
4eb30 6f 6d 6d 65 6e 74 20 74 6f 20 69 6d 70 72 6f 76  omment to improv
4eb40 65 20 72 65 61 64 61 62 69 6c 69 74 79 20 2a 2f  e readability */
4eb50 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 56  .#endif.#ifdef V
4eb60 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 69 6e  DBE_PROFILE.  in
4eb70 74 20 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20  t cnt;          
4eb80 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
4eb90 20 6f 66 20 74 69 6d 65 73 20 74 68 69 73 20 69   of times this i
4eba0 6e 73 74 72 75 63 74 69 6f 6e 20 77 61 73 20 65  nstruction was e
4ebb0 78 65 63 75 74 65 64 20 2a 2f 0a 20 20 75 36 34  xecuted */.  u64
4ebc0 20 63 79 63 6c 65 73 3b 20 20 20 20 20 20 20 20   cycles;        
4ebd0 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 74        /* Total t
4ebe0 69 6d 65 20 73 70 65 6e 74 20 65 78 65 63 75 74  ime spent execut
4ebf0 69 6e 67 20 74 68 69 73 20 69 6e 73 74 72 75 63  ing this instruc
4ec00 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d  tion */.#endif.}
4ec10 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
4ec20 20 56 64 62 65 4f 70 20 56 64 62 65 4f 70 3b 0a   VdbeOp VdbeOp;.
4ec30 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 75 62 2d 72 6f  ../*.** A sub-ro
4ec40 75 74 69 6e 65 20 75 73 65 64 20 74 6f 20 69 6d  utine used to im
4ec50 70 6c 65 6d 65 6e 74 20 61 20 74 72 69 67 67 65  plement a trigge
4ec60 72 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 73 74  r program..*/.st
4ec70 72 75 63 74 20 53 75 62 50 72 6f 67 72 61 6d 20  ruct SubProgram 
4ec80 7b 0a 20 20 56 64 62 65 4f 70 20 2a 61 4f 70 3b  {.  VdbeOp *aOp;
4ec90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4eca0 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 6f 70    /* Array of op
4ecb0 63 6f 64 65 73 20 66 6f 72 20 73 75 62 2d 70 72  codes for sub-pr
4ecc0 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e  ogram */.  int n
4ecd0 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Op;             
4ece0 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6c 65 6d           /* Elem
4ecf0 65 6e 74 73 20 69 6e 20 61 4f 70 5b 5d 20 2a 2f  ents in aOp[] */
4ed00 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20  .  int nMem;    
4ed10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ed20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65   /* Number of me
4ed30 6d 6f 72 79 20 63 65 6c 6c 73 20 72 65 71 75 69  mory cells requi
4ed40 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 73  red */.  int nCs
4ed50 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
4ed60 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
4ed70 20 6f 66 20 63 75 72 73 6f 72 73 20 72 65 71 75   of cursors requ
4ed80 69 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  ired */.  int nR
4ed90 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ef;             
4eda0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
4edb0 72 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f  r of pointers to
4edc0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
4edd0 2a 2f 0a 20 20 76 6f 69 64 20 2a 74 6f 6b 65 6e  */.  void *token
4ede0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
4edf0 20 20 20 2f 2a 20 69 64 20 74 68 61 74 20 6d 61     /* id that ma
4ee00 79 20 62 65 20 75 73 65 64 20 74 6f 20 72 65 63  y be used to rec
4ee10 75 72 73 69 76 65 20 74 72 69 67 67 65 72 73 20  ursive triggers 
4ee20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73  */.};../*.** A s
4ee30 6d 61 6c 6c 65 72 20 76 65 72 73 69 6f 6e 20 6f  maller version o
4ee40 66 20 56 64 62 65 4f 70 20 75 73 65 64 20 66 6f  f VdbeOp used fo
4ee50 72 20 74 68 65 20 56 64 62 65 41 64 64 4f 70 4c  r the VdbeAddOpL
4ee60 69 73 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 62  ist() function b
4ee70 65 63 61 75 73 65 0a 2a 2a 20 69 74 20 74 61 6b  ecause.** it tak
4ee80 65 73 20 75 70 20 6c 65 73 73 20 73 70 61 63 65  es up less space
4ee90 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65  ..*/.struct Vdbe
4eea0 4f 70 4c 69 73 74 20 7b 0a 20 20 75 38 20 6f 70  OpList {.  u8 op
4eeb0 63 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f  code;          /
4eec0 2a 20 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e  * What operation
4eed0 20 74 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20   to perform */. 
4eee0 20 73 69 67 6e 65 64 20 63 68 61 72 20 70 31 3b   signed char p1;
4eef0 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f 70       /* First op
4ef00 65 72 61 6e 64 20 2a 2f 0a 20 20 73 69 67 6e 65  erand */.  signe
4ef10 64 20 63 68 61 72 20 70 32 3b 20 20 20 20 20 2f  d char p2;     /
4ef20 2a 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 74  * Second paramet
4ef30 65 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a 75  er (often the ju
4ef40 6d 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 20  mp destination) 
4ef50 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72  */.  signed char
4ef60 20 70 33 3b 20 20 20 20 20 2f 2a 20 54 68 69 72   p3;     /* Thir
4ef70 64 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 7d  d parameter */.}
4ef80 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
4ef90 20 56 64 62 65 4f 70 4c 69 73 74 20 56 64 62 65   VdbeOpList Vdbe
4efa0 4f 70 4c 69 73 74 3b 0a 0a 2f 2a 0a 2a 2a 20 41  OpList;../*.** A
4efb0 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66  llowed values of
4efc0 20 56 64 62 65 4f 70 2e 70 34 74 79 70 65 0a 2a   VdbeOp.p4type.*
4efd0 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 4e 4f 54  /.#define P4_NOT
4efe0 55 53 45 44 20 20 20 20 30 20 20 20 2f 2a 20 54  USED    0   /* T
4eff0 68 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20  he P4 parameter 
4f000 69 73 20 6e 6f 74 20 75 73 65 64 20 2a 2f 0a 23  is not used */.#
4f010 64 65 66 69 6e 65 20 50 34 5f 44 59 4e 41 4d 49  define P4_DYNAMI
4f020 43 20 20 28 2d 31 29 20 20 2f 2a 20 50 6f 69 6e  C  (-1)  /* Poin
4f030 74 65 72 20 74 6f 20 61 20 73 74 72 69 6e 67 20  ter to a string 
4f040 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
4f050 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a  liteMalloc() */.
4f060 23 64 65 66 69 6e 65 20 50 34 5f 53 54 41 54 49  #define P4_STATI
4f070 43 20 20 20 28 2d 32 29 20 20 2f 2a 20 50 6f 69  C   (-2)  /* Poi
4f080 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69 63  nter to a static
4f090 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69   string */.#defi
4f0a0 6e 65 20 50 34 5f 43 4f 4c 4c 53 45 51 20 20 28  ne P4_COLLSEQ  (
4f0b0 2d 34 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20  -4)  /* P4 is a 
4f0c0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 43 6f 6c  pointer to a Col
4f0d0 6c 53 65 71 20 73 74 72 75 63 74 75 72 65 20 2a  lSeq structure *
4f0e0 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 46 55 4e  /.#define P4_FUN
4f0f0 43 44 45 46 20 20 28 2d 35 29 20 20 2f 2a 20 50  CDEF  (-5)  /* P
4f100 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  4 is a pointer t
4f110 6f 20 61 20 46 75 6e 63 44 65 66 20 73 74 72 75  o a FuncDef stru
4f120 63 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65  cture */.#define
4f130 20 50 34 5f 4b 45 59 49 4e 46 4f 20 20 28 2d 36   P4_KEYINFO  (-6
4f140 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f  )  /* P4 is a po
4f150 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e  inter to a KeyIn
4f160 66 6f 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  fo structure */.
4f170 23 64 65 66 69 6e 65 20 50 34 5f 56 44 42 45 46  #define P4_VDBEF
4f180 55 4e 43 20 28 2d 37 29 20 20 2f 2a 20 50 34 20  UNC (-7)  /* P4 
4f190 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
4f1a0 61 20 56 64 62 65 46 75 6e 63 20 73 74 72 75 63  a VdbeFunc struc
4f1b0 74 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ture */.#define 
4f1c0 50 34 5f 4d 45 4d 20 20 20 20 20 20 28 2d 38 29  P4_MEM      (-8)
4f1d0 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69    /* P4 is a poi
4f1e0 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d 2a 20 20  nter to a Mem*  
4f1f0 20 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23    structure */.#
4f200 64 65 66 69 6e 65 20 50 34 5f 54 52 41 4e 53 49  define P4_TRANSI
4f210 45 4e 54 20 28 2d 39 29 20 2f 2a 20 50 34 20 69  ENT (-9) /* P4 i
4f220 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
4f230 20 74 72 61 6e 73 69 65 6e 74 20 73 74 72 69 6e   transient strin
4f240 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  g */.#define P4_
4f250 56 54 41 42 20 20 20 20 20 28 2d 31 30 29 20 2f  VTAB     (-10) /
4f260 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65  * P4 is a pointe
4f270 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f  r to an sqlite3_
4f280 76 74 61 62 20 73 74 72 75 63 74 75 72 65 20 2a  vtab structure *
4f290 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 4d 50 52  /.#define P4_MPR
4f2a0 49 4e 54 46 20 20 28 2d 31 31 29 20 2f 2a 20 50  INTF  (-11) /* P
4f2b0 34 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 62  4 is a string ob
4f2c0 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
4f2d0 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f  te3_mprintf() */
4f2e0 0a 23 64 65 66 69 6e 65 20 50 34 5f 52 45 41 4c  .#define P4_REAL
4f2f0 20 20 20 20 20 28 2d 31 32 29 20 2f 2a 20 50 34       (-12) /* P4
4f300 20 69 73 20 61 20 36 34 2d 62 69 74 20 66 6c 6f   is a 64-bit flo
4f310 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75  ating point valu
4f320 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f  e */.#define P4_
4f330 49 4e 54 36 34 20 20 20 20 28 2d 31 33 29 20 2f  INT64    (-13) /
4f340 2a 20 50 34 20 69 73 20 61 20 36 34 2d 62 69 74  * P4 is a 64-bit
4f350 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20   signed integer 
4f360 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 4e  */.#define P4_IN
4f370 54 33 32 20 20 20 20 28 2d 31 34 29 20 2f 2a 20  T32    (-14) /* 
4f380 50 34 20 69 73 20 61 20 33 32 2d 62 69 74 20 73  P4 is a 32-bit s
4f390 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f  igned integer */
4f3a0 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 4e 54 41  .#define P4_INTA
4f3b0 52 52 41 59 20 28 2d 31 35 29 20 2f 2a 20 50 34  RRAY (-15) /* P4
4f3c0 20 69 73 20 61 20 76 65 63 74 6f 72 20 6f 66 20   is a vector of 
4f3d0 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 73 20  32-bit integers 
4f3e0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 53 55  */.#define P4_SU
4f3f0 42 50 52 4f 47 52 41 4d 20 20 28 2d 31 38 29 20  BPROGRAM  (-18) 
4f400 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74  /* P4 is a point
4f410 65 72 20 74 6f 20 61 20 53 75 62 50 72 6f 67 72  er to a SubProgr
4f420 61 6d 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  am structure */.
4f430 0a 2f 2a 20 57 68 65 6e 20 61 64 64 69 6e 67 20  ./* When adding 
4f440 61 20 50 34 20 61 72 67 75 6d 65 6e 74 20 75 73  a P4 argument us
4f450 69 6e 67 20 50 34 5f 4b 45 59 49 4e 46 4f 2c 20  ing P4_KEYINFO, 
4f460 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 4b 65  a copy of the Ke
4f470 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a  yInfo structure.
4f480 2a 2a 20 69 73 20 6d 61 64 65 2e 20 20 54 68 61  ** is made.  Tha
4f490 74 20 63 6f 70 79 20 69 73 20 66 72 65 65 64 20  t copy is freed 
4f4a0 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 69 73  when the Vdbe is
4f4b0 20 66 69 6e 61 6c 69 7a 65 64 2e 20 20 42 75 74   finalized.  But
4f4c0 20 69 66 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d   if the.** argum
4f4d0 65 6e 74 20 69 73 20 50 34 5f 4b 45 59 49 4e 46  ent is P4_KEYINF
4f4e0 4f 5f 48 41 4e 44 4f 46 46 2c 20 74 68 65 20 70  O_HANDOFF, the p
4f4f0 61 73 73 65 64 20 69 6e 20 70 6f 69 6e 74 65 72  assed in pointer
4f500 20 69 73 20 75 73 65 64 2e 20 20 49 74 20 73 74   is used.  It st
4f510 69 6c 6c 0a 2a 2a 20 67 65 74 73 20 66 72 65 65  ill.** gets free
4f520 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20  d when the Vdbe 
4f530 69 73 20 66 69 6e 61 6c 69 7a 65 64 20 73 6f 20  is finalized so 
4f540 69 74 20 73 74 69 6c 6c 20 73 68 6f 75 6c 64 20  it still should 
4f550 62 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66  be obtained.** f
4f560 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 73 71 6c  rom a single sql
4f570 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 42 75  iteMalloc().  Bu
4f580 74 20 6e 6f 20 63 6f 70 79 20 69 73 20 6d 61 64  t no copy is mad
4f590 65 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 69 6e  e and the callin
4f5a0 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 73 68  g.** function sh
4f5b0 6f 75 6c 64 20 2a 6e 6f 74 2a 20 74 72 79 20 74  ould *not* try t
4f5c0 6f 20 66 72 65 65 20 74 68 65 20 4b 65 79 49 6e  o free the KeyIn
4f5d0 66 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50  fo..*/.#define P
4f5e0 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  4_KEYINFO_HANDOF
4f5f0 46 20 28 2d 31 36 29 0a 23 64 65 66 69 6e 65 20  F (-16).#define 
4f600 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 54 49  P4_KEYINFO_STATI
4f610 43 20 20 28 2d 31 37 29 0a 0a 2f 2a 0a 2a 2a 20  C  (-17)../*.** 
4f620 54 68 65 20 56 64 62 65 2e 61 43 6f 6c 4e 61 6d  The Vdbe.aColNam
4f630 65 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73  e array contains
4f640 20 35 6e 20 4d 65 6d 20 73 74 72 75 63 74 75 72   5n Mem structur
4f650 65 73 2c 20 77 68 65 72 65 20 6e 20 69 73 20 74  es, where n is t
4f660 68 65 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66  he .** number of
4f670 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 74 61   columns of data
4f680 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
4f690 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 23   statement..*/.#
4f6a0 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e  define COLNAME_N
4f6b0 41 4d 45 20 20 20 20 20 30 0a 23 64 65 66 69 6e  AME     0.#defin
4f6c0 65 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59  e COLNAME_DECLTY
4f6d0 50 45 20 31 0a 23 64 65 66 69 6e 65 20 43 4f 4c  PE 1.#define COL
4f6e0 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 20 32 0a  NAME_DATABASE 2.
4f6f0 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f  #define COLNAME_
4f700 54 41 42 4c 45 20 20 20 20 33 0a 23 64 65 66 69  TABLE    3.#defi
4f710 6e 65 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d  ne COLNAME_COLUM
4f720 4e 20 20 20 34 0a 23 69 66 64 65 66 20 53 51 4c  N   4.#ifdef SQL
4f730 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d  ITE_ENABLE_COLUM
4f740 4e 5f 4d 45 54 41 44 41 54 41 0a 23 20 64 65 66  N_METADATA.# def
4f750 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20  ine COLNAME_N   
4f760 20 20 20 20 20 35 20 20 20 20 20 20 2f 2a 20 4e       5      /* N
4f770 75 6d 62 65 72 20 6f 66 20 43 4f 4c 4e 41 4d 45  umber of COLNAME
4f780 5f 78 78 78 20 73 79 6d 62 6f 6c 73 20 2a 2f 0a  _xxx symbols */.
4f790 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 53 51  #else.# ifdef SQ
4f7a0 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59  LITE_OMIT_DECLTY
4f7b0 50 45 0a 23 20 20 20 64 65 66 69 6e 65 20 43 4f  PE.#   define CO
4f7c0 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 20 31 20 20  LNAME_N      1  
4f7d0 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 6f 6e 6c      /* Store onl
4f7e0 79 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 23 20  y the name */.# 
4f7f0 65 6c 73 65 0a 23 20 20 20 64 65 66 69 6e 65 20  else.#   define 
4f800 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 20 32  COLNAME_N      2
4f810 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74        /* Store t
4f820 68 65 20 6e 61 6d 65 20 61 6e 64 20 64 65 63 6c  he name and decl
4f830 74 79 70 65 20 2a 2f 0a 23 20 65 6e 64 69 66 0a  type */.# endif.
4f840 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
4f850 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72  e following macr
4f860 6f 20 63 6f 6e 76 65 72 74 73 20 61 20 72 65 6c  o converts a rel
4f870 61 74 69 76 65 20 61 64 64 72 65 73 73 20 69 6e  ative address in
4f880 20 74 68 65 20 70 32 20 66 69 65 6c 64 0a 2a 2a   the p2 field.**
4f890 20 6f 66 20 61 20 56 64 62 65 4f 70 20 73 74 72   of a VdbeOp str
4f8a0 75 63 74 75 72 65 20 69 6e 74 6f 20 61 20 6e 65  ucture into a ne
4f8b0 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20 73 6f  gative number so
4f8c0 20 74 68 61 74 20 0a 2a 2a 20 73 71 6c 69 74 65   that .** sqlite
4f8d0 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 29  3VdbeAddOpList()
4f8e0 20 6b 6e 6f 77 73 20 74 68 61 74 20 74 68 65 20   knows that the 
4f8f0 61 64 64 72 65 73 73 20 69 73 20 72 65 6c 61 74  address is relat
4f900 69 76 65 2e 20 20 43 61 6c 6c 69 6e 67 0a 2a 2a  ive.  Calling.**
4f910 20 74 68 65 20 6d 61 63 72 6f 20 61 67 61 69 6e   the macro again
4f920 20 72 65 73 74 6f 72 65 73 20 74 68 65 20 61 64   restores the ad
4f930 64 72 65 73 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  dress..*/.#defin
4f940 65 20 41 44 44 52 28 58 29 20 20 28 2d 31 2d 28  e ADDR(X)  (-1-(
4f950 58 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d  X))../*.** The m
4f960 61 6b 65 66 69 6c 65 20 73 63 61 6e 73 20 74 68  akefile scans th
4f970 65 20 76 64 62 65 2e 63 20 73 6f 75 72 63 65 20  e vdbe.c source 
4f980 66 69 6c 65 20 61 6e 64 20 63 72 65 61 74 65 73  file and creates
4f990 20 74 68 65 20 22 6f 70 63 6f 64 65 73 2e 68 22   the "opcodes.h"
4f9a0 0a 2a 2a 20 68 65 61 64 65 72 20 66 69 6c 65 20  .** header file 
4f9b0 74 68 61 74 20 64 65 66 69 6e 65 73 20 61 20 6e  that defines a n
4f9c0 75 6d 62 65 72 20 66 6f 72 20 65 61 63 68 20 6f  umber for each o
4f9d0 70 63 6f 64 65 20 75 73 65 64 20 62 79 20 74 68  pcode used by th
4f9e0 65 20 56 44 42 45 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a  e VDBE..*/./****
4f9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
4fa00 64 65 20 6f 70 63 6f 64 65 73 2e 68 20 69 6e 20  de opcodes.h in 
4fa10 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 76 64  the middle of vd
4fa20 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  be.h ***********
4fa30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
4fa40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
4fa50 20 66 69 6c 65 20 6f 70 63 6f 64 65 73 2e 68 20   file opcodes.h 
4fa60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4fa70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
4fa80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 41 75  *********/./* Au
4fa90 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65  tomatically gene
4faa0 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20 65  rated.  Do not e
4fab0 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 65 20 74 68  dit */./* See th
4fac0 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20  e mkopcodeh.awk 
4fad0 73 63 72 69 70 74 20 66 6f 72 20 64 65 74 61 69  script for detai
4fae0 6c 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  ls */.#define OP
4faf0 5f 52 65 61 64 43 6f 6f 6b 69 65 20 20 20 20 20  _ReadCookie     
4fb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fb10 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20        1.#define 
4fb20 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 20 20 20  OP_AutoCommit   
4fb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fb40 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e          2.#defin
4fb50 65 20 4f 50 5f 46 6f 75 6e 64 20 20 20 20 20 20  e OP_Found      
4fb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fb70 20 20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66            3.#def
4fb80 69 6e 65 20 4f 50 5f 4e 75 6c 6c 52 6f 77 20 20  ine OP_NullRow  
4fb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fba0 20 20 20 20 20 20 20 20 20 20 20 20 34 0a 23 64              4.#d
4fbb0 65 66 69 6e 65 20 4f 50 5f 4c 74 20 20 20 20 20  efine OP_Lt     
4fbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fbd0 20 20 20 20 20 20 20 20 20 20 20 20 20 37 39 20               79 
4fbe0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
4fbf0 4c 54 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66  LT       */.#def
4fc00 69 6e 65 20 4f 50 5f 52 6f 77 53 65 74 54 65 73  ine OP_RowSetTes
4fc10 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
4fc20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 23 64              5.#d
4fc30 65 66 69 6e 65 20 4f 50 5f 56 61 72 69 61 62 6c  efine OP_Variabl
4fc40 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
4fc50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36 0a                6.
4fc60 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 41  #define OP_RealA
4fc70 66 66 69 6e 69 74 79 20 20 20 20 20 20 20 20 20  ffinity         
4fc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fc90 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 6f 72  7.#define OP_Sor
4fca0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
4fcb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fcc0 20 20 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41    8.#define OP_A
4fcd0 66 66 69 6e 69 74 79 20 20 20 20 20 20 20 20 20  ffinity         
4fce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fcf0 20 20 20 20 39 0a 23 64 65 66 69 6e 65 20 4f 50      9.#define OP
4fd00 5f 49 66 4e 6f 74 20 20 20 20 20 20 20 20 20 20  _IfNot          
4fd10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fd20 20 20 20 20 20 31 30 0a 23 64 65 66 69 6e 65 20       10.#define 
4fd30 4f 50 5f 47 6f 73 75 62 20 20 20 20 20 20 20 20  OP_Gosub        
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 31 31 0a 23 64 65 66 69 6e         11.#defin
4fd60 65 20 4f 50 5f 41 64 64 20 20 20 20 20 20 20 20  e OP_Add        
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 38 36 20 20 20 2f 2a           86   /*
4fd90 20 73 61 6d 65 20 61 73 20 54 4b 5f 50 4c 55 53   same as TK_PLUS
4fda0 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
4fdb0 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 20 20 20 20  OP_NotFound     
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 31 32 0a 23 64 65 66 69 6e         12.#defin
4fde0 65 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 20 20  e OP_ResultRow  
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 31 33 0a 23 64 65 66           13.#def
4fe10 69 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c 20 20 20  ine OP_IsNull   
4fe20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4fe30 20 20 20 20 20 20 20 20 20 20 20 37 33 20 20 20             73   
4fe40 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 49 53  /* same as TK_IS
4fe50 4e 55 4c 4c 20 20 20 2a 2f 0a 23 64 65 66 69 6e  NULL   */.#defin
4fe60 65 20 4f 50 5f 53 65 65 6b 4c 65 20 20 20 20 20  e OP_SeekLe     
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 31 34 0a 23 64 65 66           14.#def
4fe90 69 6e 65 20 4f 50 5f 52 6f 77 69 64 20 20 20 20  ine OP_Rowid    
4fea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4feb0 20 20 20 20 20 20 20 20 20 20 20 31 35 0a 23 64             15.#d
4fec0 65 66 69 6e 65 20 4f 50 5f 43 72 65 61 74 65 49  efine OP_CreateI
4fed0 6e 64 65 78 20 20 20 20 20 20 20 20 20 20 20 20  ndex            
4fee0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 36 0a               16.
4fef0 23 64 65 66 69 6e 65 20 4f 50 5f 45 78 70 6c 61  #define OP_Expla
4ff00 69 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  in              
4ff10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
4ff20 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44 72 6f  7.#define OP_Dro
4ff30 70 49 6e 64 65 78 20 20 20 20 20 20 20 20 20 20  pIndex          
4ff40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff50 20 31 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e   18.#define OP_N
4ff60 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20  ull             
4ff70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ff80 20 20 20 32 30 0a 23 64 65 66 69 6e 65 20 4f 50     20.#define OP
4ff90 5f 50 72 6f 67 72 61 6d 20 20 20 20 20 20 20 20  _Program        
4ffa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ffb0 20 20 20 20 20 32 31 0a 23 64 65 66 69 6e 65 20       21.#define 
4ffc0 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20 20 20 20  OP_ToInt        
4ffd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
4ffe0 20 20 20 20 20 20 31 34 34 20 20 20 2f 2a 20 73        144   /* s
4fff0 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54  ame as TK_TO_INT
50000 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
50010 5f 49 6e 74 36 34 20 20 20 20 20 20 20 20 20 20  _Int64          
50020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50030 20 20 20 20 20 32 32 0a 23 64 65 66 69 6e 65 20       22.#define 
50040 4f 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 20  OP_LoadAnalysis 
50050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50060 20 20 20 20 20 20 20 32 33 0a 23 64 65 66 69 6e         23.#defin
50070 65 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 20 20  e OP_IdxInsert  
50080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50090 20 20 20 20 20 20 20 20 20 32 34 0a 23 64 65 66           24.#def
500a0 69 6e 65 20 4f 50 5f 56 55 70 64 61 74 65 20 20  ine OP_VUpdate  
500b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
500c0 20 20 20 20 20 20 20 20 20 20 20 32 35 0a 23 64             25.#d
500d0 65 66 69 6e 65 20 4f 50 5f 4e 65 78 74 20 20 20  efine OP_Next   
500e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
500f0 20 20 20 20 20 20 20 20 20 20 20 20 20 32 36 0a               26.
50100 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f 4e 75 6d  #define OP_ToNum
50110 65 72 69 63 20 20 20 20 20 20 20 20 20 20 20 20  eric            
50120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34                14
50130 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  3   /* same as T
50140 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2a 2f 0a 23  K_TO_NUMERIC*/.#
50150 64 65 66 69 6e 65 20 4f 50 5f 47 65 20 20 20 20  define OP_Ge    
50160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 30                80
50180 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
50190 5f 47 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65  _GE       */.#de
501a0 66 69 6e 65 20 4f 50 5f 42 69 74 4e 6f 74 20 20  fine OP_BitNot  
501b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
501c0 20 20 20 20 20 20 20 20 20 20 20 20 39 33 20 20              93  
501d0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42   /* same as TK_B
501e0 49 54 4e 4f 54 20 20 20 2a 2f 0a 23 64 65 66 69  ITNOT   */.#defi
501f0 6e 65 20 4f 50 5f 53 65 65 6b 4c 74 20 20 20 20  ne OP_SeekLt    
50200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50210 20 20 20 20 20 20 20 20 20 20 32 37 0a 23 64 65            27.#de
50220 66 69 6e 65 20 4f 50 5f 52 65 77 69 6e 64 20 20  fine OP_Rewind  
50230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50240 20 20 20 20 20 20 20 20 20 20 20 20 32 38 0a 23              28.#
50250 64 65 66 69 6e 65 20 4f 50 5f 4d 75 6c 74 69 70  define OP_Multip
50260 6c 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ly              
50270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 38                88
50280 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
50290 5f 53 54 41 52 20 20 20 20 20 2a 2f 0a 23 64 65  _STAR     */.#de
502a0 66 69 6e 65 20 4f 50 5f 54 6f 52 65 61 6c 20 20  fine OP_ToReal  
502b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
502c0 20 20 20 20 20 20 20 20 20 20 20 31 34 35 20 20             145  
502d0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54   /* same as TK_T
502e0 4f 5f 52 45 41 4c 20 20 2a 2f 0a 23 64 65 66 69  O_REAL  */.#defi
502f0 6e 65 20 4f 50 5f 47 74 20 20 20 20 20 20 20 20  ne OP_Gt        
50300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50310 20 20 20 20 20 20 20 20 20 20 37 37 20 20 20 2f            77   /
50320 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 20  * same as TK_GT 
50330 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
50340 20 4f 50 5f 52 6f 77 53 65 74 52 65 61 64 20 20   OP_RowSetRead  
50350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50360 20 20 20 20 20 20 20 20 32 39 0a 23 64 65 66 69          29.#defi
50370 6e 65 20 4f 50 5f 4c 61 73 74 20 20 20 20 20 20  ne OP_Last      
50380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50390 20 20 20 20 20 20 20 20 20 20 33 30 0a 23 64 65            30.#de
503a0 66 69 6e 65 20 4f 50 5f 4d 75 73 74 42 65 49 6e  fine OP_MustBeIn
503b0 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t               
503c0 20 20 20 20 20 20 20 20 20 20 20 20 33 31 0a 23              31.#
503d0 64 65 66 69 6e 65 20 4f 50 5f 4e 65 20 20 20 20  define OP_Ne    
503e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
503f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 35                75
50400 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
50410 5f 4e 45 20 20 20 20 20 20 20 2a 2f 0a 23 64 65  _NE       */.#de
50420 66 69 6e 65 20 4f 50 5f 49 6e 63 72 56 61 63 75  fine OP_IncrVacu
50430 75 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20  um              
50440 20 20 20 20 20 20 20 20 20 20 20 20 33 32 0a 23              32.#
50450 64 65 66 69 6e 65 20 4f 50 5f 53 74 72 69 6e 67  define OP_String
50460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 33                33
50480 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 46 69 6c  .#define OP_VFil
50490 74 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20  ter             
504a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
504b0 33 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f  34.#define OP_Co
504c0 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20  unt             
504d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
504e0 20 20 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f    35.#define OP_
504f0 43 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20 20  Close           
50500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50510 20 20 20 20 33 36 0a 23 64 65 66 69 6e 65 20 4f      36.#define O
50520 50 5f 41 67 67 46 69 6e 61 6c 20 20 20 20 20 20  P_AggFinal      
50530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50540 20 20 20 20 20 20 33 37 0a 23 64 65 66 69 6e 65        37.#define
50550 20 4f 50 5f 52 6f 77 44 61 74 61 20 20 20 20 20   OP_RowData     
50560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50570 20 20 20 20 20 20 20 20 33 38 0a 23 64 65 66 69          38.#defi
50580 6e 65 20 4f 50 5f 49 64 78 52 6f 77 69 64 20 20  ne OP_IdxRowid  
50590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
505a0 20 20 20 20 20 20 20 20 20 20 33 39 0a 23 64 65            39.#de
505b0 66 69 6e 65 20 4f 50 5f 50 61 72 61 6d 20 20 20  fine OP_Param   
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 20 20 20 20 34 30 0a 23              40.#
505e0 64 65 66 69 6e 65 20 4f 50 5f 50 61 67 65 63 6f  define OP_Pageco
505f0 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20  unt             
50600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 31                41
50610 0a 23 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 4f  .#define OP_BitO
50620 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
50630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50640 38 33 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  83   /* same as 
50650 54 4b 5f 42 49 54 4f 52 20 20 20 20 2a 2f 0a 23  TK_BITOR    */.#
50660 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 4e 75 6c  define OP_NotNul
50670 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  l               
50680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 34                74
50690 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
506a0 5f 4e 4f 54 4e 55 4c 4c 20 20 2a 2f 0a 23 64 65  _NOTNULL  */.#de
506b0 66 69 6e 65 20 4f 50 5f 53 65 65 6b 47 65 20 20  fine OP_SeekGe  
506c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
506d0 20 20 20 20 20 20 20 20 20 20 20 20 34 32 0a 23              42.#
506e0 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 20 20 20  define OP_Not   
506f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 39                19
50710 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
50720 5f 4e 4f 54 20 20 20 20 20 20 2a 2f 0a 23 64 65  _NOT      */.#de
50730 66 69 6e 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75  fine OP_OpenPseu
50740 64 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  do              
50750 20 20 20 20 20 20 20 20 20 20 20 20 34 33 0a 23              43.#
50760 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 74 20 20  define OP_Halt  
50770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 34                44
50790 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6d 70  .#define OP_Comp
507a0 61 72 65 20 20 20 20 20 20 20 20 20 20 20 20 20  are             
507b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
507c0 34 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65  45.#define OP_Ne
507d0 77 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20  wRowid          
507e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
507f0 20 20 34 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f    46.#define OP_
50800 52 65 61 6c 20 20 20 20 20 20 20 20 20 20 20 20  Real            
50810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50820 20 20 20 31 33 30 20 20 20 2f 2a 20 73 61 6d 65     130   /* same
50830 20 61 73 20 54 4b 5f 46 4c 4f 41 54 20 20 20 20   as TK_FLOAT    
50840 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 64  */.#define OP_Id
50850 78 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20  xLT             
50860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50870 20 20 34 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f    47.#define OP_
50880 53 65 65 6b 47 74 20 20 20 20 20 20 20 20 20 20  SeekGt          
50890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
508a0 20 20 20 20 34 38 0a 23 64 65 66 69 6e 65 20 4f      48.#define O
508b0 50 5f 4d 65 6d 4d 61 78 20 20 20 20 20 20 20 20  P_MemMax        
508c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
508d0 20 20 20 20 20 20 34 39 0a 23 64 65 66 69 6e 65        49.#define
508e0 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 20 20 20 20   OP_Function    
508f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50900 20 20 20 20 20 20 20 20 35 30 0a 23 64 65 66 69          50.#defi
50910 6e 65 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43  ne OP_IntegrityC
50920 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k               
50930 20 20 20 20 20 20 20 20 20 20 35 31 0a 23 64 65            51.#de
50940 66 69 6e 65 20 4f 50 5f 52 65 6d 61 69 6e 64 65  fine OP_Remainde
50950 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
50960 20 20 20 20 20 20 20 20 20 20 20 20 39 30 20 20              90  
50970 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52   /* same as TK_R
50980 45 4d 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  EM      */.#defi
50990 6e 65 20 4f 50 5f 46 6b 43 6f 75 6e 74 65 72 20  ne OP_FkCounter 
509a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
509b0 20 20 20 20 20 20 20 20 20 20 35 32 0a 23 64 65            52.#de
509c0 66 69 6e 65 20 4f 50 5f 53 43 6f 70 79 20 20 20  fine OP_SCopy   
509d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
509e0 20 20 20 20 20 20 20 20 20 20 20 20 35 33 0a 23              53.#
509f0 64 65 66 69 6e 65 20 4f 50 5f 53 68 69 66 74 4c  define OP_ShiftL
50a00 65 66 74 20 20 20 20 20 20 20 20 20 20 20 20 20  eft             
50a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 34                84
50a20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
50a30 5f 4c 53 48 49 46 54 20 20 20 2a 2f 0a 23 64 65  _LSHIFT   */.#de
50a40 66 69 6e 65 20 4f 50 5f 49 66 4e 65 67 20 20 20  fine OP_IfNeg   
50a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a60 20 20 20 20 20 20 20 20 20 20 20 20 35 34 0a 23              54.#
50a70 64 65 66 69 6e 65 20 4f 50 5f 42 69 74 41 6e 64  define OP_BitAnd
50a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 32                82
50aa0 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
50ab0 5f 42 49 54 41 4e 44 20 20 20 2a 2f 0a 23 64 65  _BITAND   */.#de
50ac0 66 69 6e 65 20 4f 50 5f 4f 72 20 20 20 20 20 20  fine OP_Or      
50ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ae0 20 20 20 20 20 20 20 20 20 20 20 20 36 38 20 20              68  
50af0 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4f   /* same as TK_O
50b00 52 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  R       */.#defi
50b10 6e 65 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20  ne OP_NotExists 
50b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b30 20 20 20 20 20 20 20 20 20 20 35 35 0a 23 64 65            55.#de
50b40 66 69 6e 65 20 4f 50 5f 56 44 65 73 74 72 6f 79  fine OP_VDestroy
50b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50b60 20 20 20 20 20 20 20 20 20 20 20 20 35 36 0a 23              56.#
50b70 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 44 65 6c  define OP_IdxDel
50b80 65 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ete             
50b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 37                57
50ba0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 61 63 75  .#define OP_Vacu
50bb0 75 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20  um              
50bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50bd0 35 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f  58.#define OP_Co
50be0 70 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20  py              
50bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c00 20 20 35 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f    59.#define OP_
50c10 49 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20  If              
50c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c30 20 20 20 20 36 30 0a 23 64 65 66 69 6e 65 20 4f      60.#define O
50c40 50 5f 4a 75 6d 70 20 20 20 20 20 20 20 20 20 20  P_Jump          
50c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c60 20 20 20 20 20 20 36 31 0a 23 64 65 66 69 6e 65        61.#define
50c70 20 4f 50 5f 44 65 73 74 72 6f 79 20 20 20 20 20   OP_Destroy     
50c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50c90 20 20 20 20 20 20 20 20 36 32 0a 23 64 65 66 69          62.#defi
50ca0 6e 65 20 4f 50 5f 41 67 67 53 74 65 70 20 20 20  ne OP_AggStep   
50cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50cc0 20 20 20 20 20 20 20 20 20 20 36 33 0a 23 64 65            63.#de
50cd0 66 69 6e 65 20 4f 50 5f 43 6c 65 61 72 20 20 20  fine OP_Clear   
50ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50cf0 20 20 20 20 20 20 20 20 20 20 20 20 36 34 0a 23              64.#
50d00 64 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65 72 74  define OP_Insert
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 20 20 20 20 36 35                65
50d30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 50 65 72 6d  .#define OP_Perm
50d40 75 74 61 74 69 6f 6e 20 20 20 20 20 20 20 20 20  utation         
50d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d60 36 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 42  66.#define OP_VB
50d70 65 67 69 6e 20 20 20 20 20 20 20 20 20 20 20 20  egin            
50d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50d90 20 20 36 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f    67.#define OP_
50da0 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 20 20  OpenEphemeral   
50db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50dc0 20 20 20 20 37 30 0a 23 64 65 66 69 6e 65 20 4f      70.#define O
50dd0 50 5f 49 64 78 47 45 20 20 20 20 20 20 20 20 20  P_IdxGE         
50de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50df0 20 20 20 20 20 20 37 31 0a 23 64 65 66 69 6e 65        71.#define
50e00 20 4f 50 5f 54 72 61 63 65 20 20 20 20 20 20 20   OP_Trace       
50e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50e20 20 20 20 20 20 20 20 20 37 32 0a 23 64 65 66 69          72.#defi
50e30 6e 65 20 4f 50 5f 44 69 76 69 64 65 20 20 20 20  ne OP_Divide    
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 38 39 20 20 20 2f            89   /
50e60 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c 41  * same as TK_SLA
50e70 53 48 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  SH    */.#define
50e80 20 4f 50 5f 53 74 72 69 6e 67 38 20 20 20 20 20   OP_String8     
50e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ea0 20 20 20 20 20 20 20 20 39 34 20 20 20 2f 2a 20          94   /* 
50eb0 73 61 6d 65 20 61 73 20 54 4b 5f 53 54 52 49 4e  same as TK_STRIN
50ec0 47 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  G   */.#define O
50ed0 50 5f 43 6f 6e 63 61 74 20 20 20 20 20 20 20 20  P_Concat        
50ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50ef0 20 20 20 20 20 20 39 31 20 20 20 2f 2a 20 73 61        91   /* sa
50f00 6d 65 20 61 73 20 54 4b 5f 43 4f 4e 43 41 54 20  me as TK_CONCAT 
50f10 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f    */.#define OP_
50f20 4d 61 6b 65 52 65 63 6f 72 64 20 20 20 20 20 20  MakeRecord      
50f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f40 20 20 20 20 38 31 0a 23 64 65 66 69 6e 65 20 4f      81.#define O
50f50 50 5f 59 69 65 6c 64 20 20 20 20 20 20 20 20 20  P_Yield         
50f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50f70 20 20 20 20 20 20 39 32 0a 23 64 65 66 69 6e 65        92.#define
50f80 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 20 20 20   OP_SetCookie   
50f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50fa0 20 20 20 20 20 20 20 20 39 35 0a 23 64 65 66 69          95.#defi
50fb0 6e 65 20 4f 50 5f 50 72 65 76 20 20 20 20 20 20  ne OP_Prev      
50fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
50fd0 20 20 20 20 20 20 20 20 20 20 39 36 0a 23 64 65            96.#de
50fe0 66 69 6e 65 20 4f 50 5f 44 72 6f 70 54 72 69 67  fine OP_DropTrig
50ff0 67 65 72 20 20 20 20 20 20 20 20 20 20 20 20 20  ger             
51000 20 20 20 20 20 20 20 20 20 20 20 20 39 37 0a 23              97.#
51010 64 65 66 69 6e 65 20 4f 50 5f 46 6b 49 66 5a 65  define OP_FkIfZe
51020 72 6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ro              
51030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 38                98
51040 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 6e 64 20  .#define OP_And 
51050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51070 36 39 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  69   /* same as 
51080 54 4b 5f 41 4e 44 20 20 20 20 20 20 2a 2f 0a 23  TK_AND      */.#
51090 64 65 66 69 6e 65 20 4f 50 5f 56 43 6f 6c 75 6d  define OP_VColum
510a0 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
510b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 39                99
510c0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65 74 75  .#define OP_Retu
510d0 72 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rn              
510e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
510f0 30 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70  00.#define OP_Op
51100 65 6e 57 72 69 74 65 20 20 20 20 20 20 20 20 20  enWrite         
51110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51120 20 31 30 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f   101.#define OP_
51130 49 6e 74 65 67 65 72 20 20 20 20 20 20 20 20 20  Integer         
51140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51150 20 20 20 31 30 32 0a 23 64 65 66 69 6e 65 20 4f     102.#define O
51160 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20 20 20  P_Transaction   
51170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51180 20 20 20 20 20 31 30 33 0a 23 64 65 66 69 6e 65       103.#define
51190 20 4f 50 5f 49 66 50 6f 73 20 20 20 20 20 20 20   OP_IfPos       
511a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
511b0 20 20 20 20 20 20 20 31 30 34 0a 23 64 65 66 69         104.#defi
511c0 6e 65 20 4f 50 5f 52 6f 77 53 65 74 41 64 64 20  ne OP_RowSetAdd 
511d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
511e0 20 20 20 20 20 20 20 20 20 31 30 35 0a 23 64 65           105.#de
511f0 66 69 6e 65 20 4f 50 5f 43 6f 6c 6c 53 65 71 20  fine OP_CollSeq 
51200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51210 20 20 20 20 20 20 20 20 20 20 20 31 30 36 0a 23             106.#
51220 64 65 66 69 6e 65 20 4f 50 5f 53 61 76 65 70 6f  define OP_Savepo
51230 69 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20  int             
51240 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30 37               107
51250 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 52 65 6e  .#define OP_VRen
51260 61 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ame             
51270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51280 30 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f  08.#define OP_To
51290 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20 20  Blob            
512a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
512b0 20 31 34 32 20 20 20 2f 2a 20 73 61 6d 65 20 61   142   /* same a
512c0 73 20 54 4b 5f 54 4f 5f 42 4c 4f 42 20 20 2a 2f  s TK_TO_BLOB  */
512d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 71 75  .#define OP_Sequ
512e0 65 6e 63 65 20 20 20 20 20 20 20 20 20 20 20 20  ence            
512f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51300 30 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 68  09.#define OP_Sh
51310 69 66 74 52 69 67 68 74 20 20 20 20 20 20 20 20  iftRight        
51320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51330 20 20 38 35 20 20 20 2f 2a 20 73 61 6d 65 20 61    85   /* same a
51340 73 20 54 4b 5f 52 53 48 49 46 54 20 20 20 2a 2f  s TK_RSHIFT   */
51350 0a 23 64 65 66 69 6e 65 20 4f 50 5f 48 61 6c 74  .#define OP_Halt
51360 49 66 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20  IfNull          
51370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51380 31 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 43  10.#define OP_VC
51390 72 65 61 74 65 20 20 20 20 20 20 20 20 20 20 20  reate           
513a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
513b0 20 31 31 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f   111.#define OP_
513c0 43 72 65 61 74 65 54 61 62 6c 65 20 20 20 20 20  CreateTable     
513d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
513e0 20 20 20 31 31 32 0a 23 64 65 66 69 6e 65 20 4f     112.#define O
513f0 50 5f 41 64 64 49 6d 6d 20 20 20 20 20 20 20 20  P_AddImm        
51400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51410 20 20 20 20 20 31 31 33 0a 23 64 65 66 69 6e 65       113.#define
51420 20 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 20 20   OP_ToText      
51430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51440 20 20 20 20 20 20 20 31 34 31 20 20 20 2f 2a 20         141   /* 
51450 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45  same as TK_TO_TE
51460 58 54 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  XT  */.#define O
51470 50 5f 44 72 6f 70 54 61 62 6c 65 20 20 20 20 20  P_DropTable     
51480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51490 20 20 20 20 20 31 31 34 0a 23 64 65 66 69 6e 65       114.#define
514a0 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 20 20 20   OP_IsUnique    
514b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
514c0 20 20 20 20 20 20 20 31 31 35 0a 23 64 65 66 69         115.#defi
514d0 6e 65 20 4f 50 5f 56 4f 70 65 6e 20 20 20 20 20  ne OP_VOpen     
514e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
514f0 20 20 20 20 20 20 20 20 20 31 31 36 0a 23 64 65           116.#de
51500 66 69 6e 65 20 4f 50 5f 49 66 5a 65 72 6f 20 20  fine OP_IfZero  
51510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51520 20 20 20 20 20 20 20 20 20 20 20 31 31 37 0a 23             117.#
51530 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 6f 70 20 20  define OP_Noop  
51540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51550 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31 38               118
51560 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 6e 73 65  .#define OP_Inse
51570 72 74 49 6e 74 20 20 20 20 20 20 20 20 20 20 20  rtInt           
51580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51590 31 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 6f  19.#define OP_Ro
515a0 77 4b 65 79 20 20 20 20 20 20 20 20 20 20 20 20  wKey            
515b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
515c0 20 31 32 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f   120.#define OP_
515d0 45 78 70 69 72 65 20 20 20 20 20 20 20 20 20 20  Expire          
515e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
515f0 20 20 20 31 32 31 0a 23 64 65 66 69 6e 65 20 4f     121.#define O
51600 50 5f 44 65 6c 65 74 65 20 20 20 20 20 20 20 20  P_Delete        
51610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51620 20 20 20 20 20 31 32 32 0a 23 64 65 66 69 6e 65       122.#define
51630 20 4f 50 5f 53 75 62 74 72 61 63 74 20 20 20 20   OP_Subtract    
51640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51650 20 20 20 20 20 20 20 20 38 37 20 20 20 2f 2a 20          87   /* 
51660 73 61 6d 65 20 61 73 20 54 4b 5f 4d 49 4e 55 53  same as TK_MINUS
51670 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f      */.#define O
51680 50 5f 42 6c 6f 62 20 20 20 20 20 20 20 20 20 20  P_Blob          
51690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
516a0 20 20 20 20 20 31 32 33 0a 23 64 65 66 69 6e 65       123.#define
516b0 20 4f 50 5f 4d 6f 76 65 20 20 20 20 20 20 20 20   OP_Move        
516c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
516d0 20 20 20 20 20 20 20 31 32 34 0a 23 64 65 66 69         124.#defi
516e0 6e 65 20 4f 50 5f 47 6f 74 6f 20 20 20 20 20 20  ne OP_Goto      
516f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51700 20 20 20 20 20 20 20 20 20 31 32 35 0a 23 64 65           125.#de
51710 66 69 6e 65 20 4f 50 5f 50 61 72 73 65 53 63 68  fine OP_ParseSch
51720 65 6d 61 20 20 20 20 20 20 20 20 20 20 20 20 20  ema             
51730 20 20 20 20 20 20 20 20 20 20 20 31 32 36 0a 23             126.#
51740 64 65 66 69 6e 65 20 4f 50 5f 45 71 20 20 20 20  define OP_Eq    
51750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 36                76
51770 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
51780 5f 45 51 20 20 20 20 20 20 20 2a 2f 0a 23 64 65  _EQ       */.#de
51790 66 69 6e 65 20 4f 50 5f 56 4e 65 78 74 20 20 20  fine OP_VNext   
517a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
517b0 20 20 20 20 20 20 20 20 20 20 20 31 32 37 0a 23             127.#
517c0 64 65 66 69 6e 65 20 4f 50 5f 53 65 65 6b 20 20  define OP_Seek  
517d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
517e0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 38               128
517f0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 65 20 20  .#define OP_Le  
51800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51820 37 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  78   /* same as 
51830 54 4b 5f 4c 45 20 20 20 20 20 20 20 2a 2f 0a 23  TK_LE       */.#
51840 64 65 66 69 6e 65 20 4f 50 5f 54 61 62 6c 65 4c  define OP_TableL
51850 6f 63 6b 20 20 20 20 20 20 20 20 20 20 20 20 20  ock             
51860 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 39               129
51870 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 65 72 69  .#define OP_Veri
51880 66 79 43 6f 6f 6b 69 65 20 20 20 20 20 20 20 20  fyCookie        
51890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
518a0 33 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f  31.#define OP_Co
518b0 6c 75 6d 6e 20 20 20 20 20 20 20 20 20 20 20 20  lumn            
518c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
518d0 20 31 33 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f   132.#define OP_
518e0 4f 70 65 6e 52 65 61 64 20 20 20 20 20 20 20 20  OpenRead        
518f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51900 20 20 20 31 33 33 0a 23 64 65 66 69 6e 65 20 4f     133.#define O
51910 50 5f 52 65 73 65 74 43 6f 75 6e 74 20 20 20 20  P_ResetCount    
51920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51930 20 20 20 20 20 31 33 34 0a 0a 2f 2a 20 54 68 65       134../* The
51940 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 63 6f 64   following opcod
51950 65 20 76 61 6c 75 65 73 20 61 72 65 20 6e 65 76  e values are nev
51960 65 72 20 75 73 65 64 20 2a 2f 0a 23 64 65 66 69  er used */.#defi
51970 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33  ne OP_NotUsed_13
51980 35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  5               
51990 20 20 20 20 20 20 20 20 20 31 33 35 0a 23 64 65           135.#de
519a0 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f  fine OP_NotUsed_
519b0 31 33 36 20 20 20 20 20 20 20 20 20 20 20 20 20  136             
519c0 20 20 20 20 20 20 20 20 20 20 20 31 33 36 0a 23             136.#
519d0 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65  define OP_NotUse
519e0 64 5f 31 33 37 20 20 20 20 20 20 20 20 20 20 20  d_137           
519f0 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 37               137
51a00 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55  .#define OP_NotU
51a10 73 65 64 5f 31 33 38 20 20 20 20 20 20 20 20 20  sed_138         
51a20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51a30 33 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f  38.#define OP_No
51a40 74 55 73 65 64 5f 31 33 39 20 20 20 20 20 20 20  tUsed_139       
51a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51a60 20 31 33 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f   139.#define OP_
51a70 4e 6f 74 55 73 65 64 5f 31 34 30 20 20 20 20 20  NotUsed_140     
51a80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51a90 20 20 20 31 34 30 0a 0a 0a 2f 2a 20 50 72 6f 70     140.../* Prop
51aa0 65 72 74 69 65 73 20 73 75 63 68 20 61 73 20 22  erties such as "
51ab0 6f 75 74 32 22 20 6f 72 20 22 6a 75 6d 70 22 20  out2" or "jump" 
51ac0 74 68 61 74 20 61 72 65 20 73 70 65 63 69 66 69  that are specifi
51ad0 65 64 20 69 6e 0a 2a 2a 20 63 6f 6d 6d 65 6e 74  ed in.** comment
51ae0 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20  s following the 
51af0 22 63 61 73 65 22 20 66 6f 72 20 65 61 63 68 20  "case" for each 
51b00 6f 70 63 6f 64 65 20 69 6e 20 74 68 65 20 76 64  opcode in the vd
51b10 62 65 2e 63 0a 2a 2a 20 61 72 65 20 65 6e 63 6f  be.c.** are enco
51b20 64 65 64 20 69 6e 74 6f 20 62 69 74 76 65 63 74  ded into bitvect
51b30 6f 72 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  ors as follows:.
51b40 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 47  */.#define OPFLG
51b50 5f 4a 55 4d 50 20 20 20 20 20 20 20 20 20 20 20  _JUMP           
51b60 20 30 78 30 30 30 31 20 20 2f 2a 20 6a 75 6d 70   0x0001  /* jump
51b70 3a 20 20 50 32 20 68 6f 6c 64 73 20 6a 6d 70 20  :  P2 holds jmp 
51b80 74 61 72 67 65 74 20 2a 2f 0a 23 64 65 66 69 6e  target */.#defin
51b90 65 20 4f 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45  e OPFLG_OUT2_PRE
51ba0 52 45 4c 45 41 53 45 20 30 78 30 30 30 32 20 20  RELEASE 0x0002  
51bb0 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61  /* out2-prerelea
51bc0 73 65 3a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  se: */.#define O
51bd0 50 46 4c 47 5f 49 4e 31 20 20 20 20 20 20 20 20  PFLG_IN1        
51be0 20 20 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20       0x0004  /* 
51bf0 69 6e 31 3a 20 20 20 50 31 20 69 73 20 61 6e 20  in1:   P1 is an 
51c00 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65  input */.#define
51c10 20 4f 50 46 4c 47 5f 49 4e 32 20 20 20 20 20 20   OPFLG_IN2      
51c20 20 20 20 20 20 20 20 30 78 30 30 30 38 20 20 2f         0x0008  /
51c30 2a 20 69 6e 32 3a 20 20 20 50 32 20 69 73 20 61  * in2:   P2 is a
51c40 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69  n input */.#defi
51c50 6e 65 20 4f 50 46 4c 47 5f 49 4e 33 20 20 20 20  ne OPFLG_IN3    
51c60 20 20 20 20 20 20 20 20 20 30 78 30 30 31 30 20           0x0010 
51c70 20 2f 2a 20 69 6e 33 3a 20 20 20 50 33 20 69 73   /* in3:   P3 is
51c80 20 61 6e 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65   an input */.#de
51c90 66 69 6e 65 20 4f 50 46 4c 47 5f 4f 55 54 33 20  fine OPFLG_OUT3 
51ca0 20 20 20 20 20 20 20 20 20 20 20 30 78 30 30 32             0x002
51cb0 30 20 20 2f 2a 20 6f 75 74 33 3a 20 20 50 33 20  0  /* out3:  P3 
51cc0 69 73 20 61 6e 20 6f 75 74 70 75 74 20 2a 2f 0a  is an output */.
51cd0 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e  #define OPFLG_IN
51ce0 49 54 49 41 4c 49 5a 45 52 20 7b 5c 0a 2f 2a 20  ITIALIZER {\./* 
51cf0 20 20 30 20 2a 2f 20 30 78 30 30 2c 20 30 78 30    0 */ 0x00, 0x0
51d00 32 2c 20 30 78 30 30 2c 20 30 78 31 31 2c 20 30  2, 0x00, 0x11, 0
51d10 78 30 30 2c 20 30 78 31 35 2c 20 30 78 30 30 2c  x00, 0x15, 0x00,
51d20 20 30 78 30 34 2c 5c 0a 2f 2a 20 20 20 38 20 2a   0x04,\./*   8 *
51d30 2f 20 30 78 30 31 2c 20 30 78 30 30 2c 20 30 78  / 0x01, 0x00, 0x
51d40 30 35 2c 20 30 78 30 31 2c 20 30 78 31 31 2c 20  05, 0x01, 0x11, 
51d50 30 78 30 30 2c 20 30 78 31 31 2c 20 30 78 30 32  0x00, 0x11, 0x02
51d60 2c 5c 0a 2f 2a 20 20 31 36 20 2a 2f 20 30 78 30  ,\./*  16 */ 0x0
51d70 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  2, 0x00, 0x00, 0
51d80 78 30 34 2c 20 30 78 30 32 2c 20 30 78 30 31 2c  x04, 0x02, 0x01,
51d90 20 30 78 30 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a   0x02, 0x00,\./*
51da0 20 20 32 34 20 2a 2f 20 30 78 30 38 2c 20 30 78    24 */ 0x08, 0x
51db0 30 30 2c 20 30 78 30 31 2c 20 30 78 31 31 2c 20  00, 0x01, 0x11, 
51dc0 30 78 30 31 2c 20 30 78 32 31 2c 20 30 78 30 31  0x01, 0x21, 0x01
51dd0 2c 20 30 78 30 35 2c 5c 0a 2f 2a 20 20 33 32 20  , 0x05,\./*  32 
51de0 2a 2f 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30  */ 0x01, 0x02, 0
51df0 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 30 2c  x01, 0x02, 0x00,
51e00 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
51e10 32 2c 5c 0a 2f 2a 20 20 34 30 20 2a 2f 20 30 78  2,\./*  40 */ 0x
51e20 30 32 2c 20 30 78 30 32 2c 20 30 78 31 31 2c 20  02, 0x02, 0x11, 
51e30 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
51e40 2c 20 30 78 30 32 2c 20 30 78 31 31 2c 5c 0a 2f  , 0x02, 0x11,\./
51e50 2a 20 20 34 38 20 2a 2f 20 30 78 31 31 2c 20 30  *  48 */ 0x11, 0
51e60 78 30 38 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x08, 0x00, 0x00,
51e70 20 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30   0x00, 0x04, 0x0
51e80 35 2c 20 30 78 31 31 2c 5c 0a 2f 2a 20 20 35 36  5, 0x11,\./*  56
51e90 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20   */ 0x00, 0x00, 
51ea0 30 78 30 30 2c 20 30 78 30 34 2c 20 30 78 30 35  0x00, 0x04, 0x05
51eb0 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78  , 0x01, 0x02, 0x
51ec0 30 30 2c 5c 0a 2f 2a 20 20 36 34 20 2a 2f 20 30  00,\./*  64 */ 0
51ed0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
51ee0 20 30 78 30 30 2c 20 30 78 32 63 2c 20 30 78 32   0x00, 0x2c, 0x2
51ef0 63 2c 20 30 78 30 30 2c 20 30 78 31 31 2c 5c 0a  c, 0x00, 0x11,\.
51f00 2f 2a 20 20 37 32 20 2a 2f 20 30 78 30 30 2c 20  /*  72 */ 0x00, 
51f10 30 78 30 35 2c 20 30 78 30 35 2c 20 30 78 31 35  0x05, 0x05, 0x15
51f20 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20 30 78  , 0x15, 0x15, 0x
51f30 31 35 2c 20 30 78 31 35 2c 5c 0a 2f 2a 20 20 38  15, 0x15,\./*  8
51f40 30 20 2a 2f 20 30 78 31 35 2c 20 30 78 30 30 2c  0 */ 0x15, 0x00,
51f50 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32   0x2c, 0x2c, 0x2
51f60 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30  c, 0x2c, 0x2c, 0
51f70 78 32 63 2c 5c 0a 2f 2a 20 20 38 38 20 2a 2f 20  x2c,\./*  88 */ 
51f80 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63  0x2c, 0x2c, 0x2c
51f90 2c 20 30 78 32 63 2c 20 30 78 30 34 2c 20 30 78  , 0x2c, 0x04, 0x
51fa0 30 34 2c 20 30 78 30 32 2c 20 30 78 31 30 2c 5c  04, 0x02, 0x10,\
51fb0 0a 2f 2a 20 20 39 36 20 2a 2f 20 30 78 30 31 2c  ./*  96 */ 0x01,
51fc0 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30   0x00, 0x01, 0x0
51fd0 30 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 20 30  0, 0x04, 0x00, 0
51fe0 78 30 32 2c 20 30 78 30 30 2c 5c 0a 2f 2a 20 31  x02, 0x00,\./* 1
51ff0 30 34 20 2a 2f 20 30 78 30 35 2c 20 30 78 30 38  04 */ 0x05, 0x08
52000 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
52010 30 30 2c 20 30 78 30 32 2c 20 30 78 31 30 2c 20  00, 0x02, 0x10, 
52020 30 78 30 30 2c 5c 0a 2f 2a 20 31 31 32 20 2a 2f  0x00,\./* 112 */
52030 20 30 78 30 32 2c 20 30 78 30 34 2c 20 30 78 30   0x02, 0x04, 0x0
52040 30 2c 20 30 78 31 31 2c 20 30 78 30 30 2c 20 30  0, 0x11, 0x00, 0
52050 78 30 35 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x05, 0x00, 0x00,
52060 5c 0a 2f 2a 20 31 32 30 20 2a 2f 20 30 78 30 30  \./* 120 */ 0x00
52070 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
52080 30 32 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20  02, 0x00, 0x01, 
52090 30 78 30 30 2c 20 30 78 30 31 2c 5c 0a 2f 2a 20  0x00, 0x01,\./* 
520a0 31 32 38 20 2a 2f 20 30 78 30 38 2c 20 30 78 30  128 */ 0x08, 0x0
520b0 30 2c 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30  0, 0x02, 0x00, 0
520c0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
520d0 20 30 78 30 30 2c 5c 0a 2f 2a 20 31 33 36 20 2a   0x00,\./* 136 *
520e0 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  / 0x00, 0x00, 0x
520f0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
52100 30 78 30 34 2c 20 30 78 30 34 2c 20 30 78 30 34  0x04, 0x04, 0x04
52110 2c 5c 0a 2f 2a 20 31 34 34 20 2a 2f 20 30 78 30  ,\./* 144 */ 0x0
52120 34 2c 20 30 78 30 34 2c 7d 0a 0a 2f 2a 2a 2a 2a  4, 0x04,}../****
52130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
52140 66 20 6f 70 63 6f 64 65 73 2e 68 20 2a 2a 2a 2a  f opcodes.h ****
52150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
52180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
52190 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
521a0 65 66 74 20 6f 66 66 20 69 6e 20 76 64 62 65 2e  eft off in vdbe.
521b0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
521c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a  *********/../*.*
521d0 2a 20 50 72 6f 74 6f 74 79 70 65 73 20 66 6f 72  * Prototypes for
521e0 20 74 68 65 20 56 44 42 45 20 69 6e 74 65 72 66   the VDBE interf
521f0 61 63 65 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e  ace.  See commen
52200 74 73 20 6f 6e 20 74 68 65 20 69 6d 70 6c 65 6d  ts on the implem
52210 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20  entation.** for 
52220 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66  a description of
52230 20 77 68 61 74 20 65 61 63 68 20 6f 66 20 74 68   what each of th
52240 65 73 65 20 72 6f 75 74 69 6e 65 73 20 64 6f 65  ese routines doe
52250 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
52260 56 41 54 45 20 56 64 62 65 20 2a 73 71 6c 69 74  VATE Vdbe *sqlit
52270 65 33 56 64 62 65 43 72 65 61 74 65 28 73 71 6c  e3VdbeCreate(sql
52280 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ite3*);.SQLITE_P
52290 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
522a0 65 33 56 64 62 65 41 64 64 4f 70 30 28 56 64 62  e3VdbeAddOp0(Vdb
522b0 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  e*,int);.SQLITE_
522c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
522d0 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 56 64  te3VdbeAddOp1(Vd
522e0 62 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51  be*,int,int);.SQ
522f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
52300 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
52310 70 32 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74  p2(Vdbe*,int,int
52320 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ,int);.SQLITE_PR
52330 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
52340 33 56 64 62 65 41 64 64 4f 70 33 28 56 64 62 65  3VdbeAddOp3(Vdbe
52350 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e  *,int,int,int,in
52360 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
52370 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
52380 62 65 41 64 64 4f 70 34 28 56 64 62 65 2a 2c 69  beAddOp4(Vdbe*,i
52390 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 63  nt,int,int,int,c
523a0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34 2c 69  onst char *zP4,i
523b0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
523c0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
523d0 64 62 65 41 64 64 4f 70 4c 69 73 74 28 56 64 62  dbeAddOpList(Vdb
523e0 65 2a 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62  e*, int nOp, Vdb
523f0 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61  eOpList const *a
52400 4f 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  Op);.SQLITE_PRIV
52410 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
52420 56 64 62 65 43 68 61 6e 67 65 50 31 28 56 64 62  VdbeChangeP1(Vdb
52430 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e  e*, int addr, in
52440 74 20 50 31 29 3b 0a 53 51 4c 49 54 45 5f 50 52  t P1);.SQLITE_PR
52450 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
52460 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 56  e3VdbeChangeP2(V
52470 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c 20  dbe*, int addr, 
52480 69 6e 74 20 50 32 29 3b 0a 53 51 4c 49 54 45 5f  int P2);.SQLITE_
52490 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
524a0 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 33  ite3VdbeChangeP3
524b0 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72  (Vdbe*, int addr
524c0 2c 20 69 6e 74 20 50 33 29 3b 0a 53 51 4c 49 54  , int P3);.SQLIT
524d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
524e0 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
524f0 50 35 28 56 64 62 65 2a 2c 20 75 38 20 50 35 29  P5(Vdbe*, u8 P5)
52500 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
52510 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
52520 65 4a 75 6d 70 48 65 72 65 28 56 64 62 65 2a 2c  eJumpHere(Vdbe*,
52530 20 69 6e 74 20 61 64 64 72 29 3b 0a 53 51 4c 49   int addr);.SQLI
52540 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
52550 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
52560 65 54 6f 4e 6f 6f 70 28 56 64 62 65 2a 2c 20 69  eToNoop(Vdbe*, i
52570 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 4e 29 3b  nt addr, int N);
52580 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
52590 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
525a0 43 68 61 6e 67 65 50 34 28 56 64 62 65 2a 2c 20  ChangeP4(Vdbe*, 
525b0 69 6e 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20  int addr, const 
525c0 63 68 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 4e  char *zP4, int N
525d0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
525e0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
525f0 62 65 55 73 65 73 42 74 72 65 65 28 56 64 62 65  beUsesBtree(Vdbe
52600 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
52610 50 52 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a  PRIVATE VdbeOp *
52620 73 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70  sqlite3VdbeGetOp
52630 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51  (Vdbe*, int);.SQ
52640 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
52650 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
52660 4c 61 62 65 6c 28 56 64 62 65 2a 29 3b 0a 53 51  Label(Vdbe*);.SQ
52670 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
52680 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c  d sqlite3VdbeDel
52690 65 74 65 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49  ete(Vdbe*);.SQLI
526a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
526b0 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52  sqlite3VdbeMakeR
526c0 65 61 64 79 28 56 64 62 65 2a 2c 69 6e 74 2c 69  eady(Vdbe*,int,i
526d0 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69  nt,int,int,int,i
526e0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
526f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
52700 64 62 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 65  dbeFinalize(Vdbe
52710 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
52720 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
52730 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
52740 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  Vdbe*, int);.SQL
52750 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
52760 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
52770 6e 74 41 64 64 72 28 56 64 62 65 2a 29 3b 0a 23  ntAddr(Vdbe*);.#
52780 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
52790 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
527a0 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56  E   int sqlite3V
527b0 64 62 65 41 73 73 65 72 74 4d 61 79 41 62 6f 72  dbeAssertMayAbor
527c0 74 28 56 64 62 65 20 2a 2c 20 69 6e 74 29 3b 0a  t(Vdbe *, int);.
527d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
527e0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
527f0 65 54 72 61 63 65 28 56 64 62 65 2a 2c 46 49 4c  eTrace(Vdbe*,FIL
52800 45 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49  E*);.#endif.SQLI
52810 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
52820 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74  sqlite3VdbeReset
52830 53 74 65 70 52 65 73 75 6c 74 28 56 64 62 65 2a  StepResult(Vdbe*
52840 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
52850 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
52860 65 52 65 73 65 74 28 56 64 62 65 2a 29 3b 0a 53  eReset(Vdbe*);.S
52870 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
52880 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  id sqlite3VdbeSe
52890 74 4e 75 6d 43 6f 6c 73 28 56 64 62 65 2a 2c 69  tNumCols(Vdbe*,i
528a0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
528b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
528c0 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 56 64  dbeSetColName(Vd
528d0 62 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 63  be*, int, int, c
528e0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 76 6f 69  onst char *, voi
528f0 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51  d(*)(void*));.SQ
52900 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
52910 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75  d sqlite3VdbeCou
52920 6e 74 43 68 61 6e 67 65 73 28 56 64 62 65 2a 29  ntChanges(Vdbe*)
52930 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
52940 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65   sqlite3 *sqlite
52950 33 56 64 62 65 44 62 28 56 64 62 65 2a 29 3b 0a  3VdbeDb(Vdbe*);.
52960 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
52970 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53  oid sqlite3VdbeS
52980 65 74 53 71 6c 28 56 64 62 65 2a 2c 20 63 6f 6e  etSql(Vdbe*, con
52990 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  st char *z, int 
529a0 6e 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  n, int);.SQLITE_
529b0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
529c0 69 74 65 33 56 64 62 65 53 77 61 70 28 56 64 62  ite3VdbeSwap(Vdb
529d0 65 2a 2c 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54  e*,Vdbe*);.SQLIT
529e0 45 5f 50 52 49 56 41 54 45 20 56 64 62 65 4f 70  E_PRIVATE VdbeOp
529f0 20 2a 73 71 6c 69 74 65 33 56 64 62 65 54 61 6b   *sqlite3VdbeTak
52a00 65 4f 70 41 72 72 61 79 28 56 64 62 65 2a 2c 20  eOpArray(Vdbe*, 
52a10 69 6e 74 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c  int*, int*);.SQL
52a20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
52a30 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 6f 67   sqlite3VdbeProg
52a40 72 61 6d 44 65 6c 65 74 65 28 73 71 6c 69 74 65  ramDelete(sqlite
52a50 33 20 2a 2c 20 53 75 62 50 72 6f 67 72 61 6d 20  3 *, SubProgram 
52a60 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
52a70 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f  PRIVATE sqlite3_
52a80 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 64  value *sqlite3Vd
52a90 62 65 47 65 74 56 61 6c 75 65 28 56 64 62 65 2a  beGetValue(Vdbe*
52aa0 2c 20 69 6e 74 2c 20 75 38 29 3b 0a 53 51 4c 49  , int, u8);.SQLI
52ab0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
52ac0 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 56 61  sqlite3VdbeSetVa
52ad0 72 6d 61 73 6b 28 56 64 62 65 2a 2c 20 69 6e 74  rmask(Vdbe*, int
52ae0 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
52af0 54 45 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  TE UnpackedRecor
52b00 64 20 2a 73 71 6c 69 74 65 33 56 64 62 65 52 65  d *sqlite3VdbeRe
52b10 63 6f 72 64 55 6e 70 61 63 6b 28 4b 65 79 49 6e  cordUnpack(KeyIn
52b20 66 6f 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  fo*,int,const vo
52b30 69 64 2a 2c 63 68 61 72 2a 2c 69 6e 74 29 3b 0a  id*,char*,int);.
52b40 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
52b50 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44  oid sqlite3VdbeD
52b60 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63  eleteUnpackedRec
52b70 6f 72 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f  ord(UnpackedReco
52b80 72 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  rd*);.SQLITE_PRI
52b90 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
52ba0 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72  VdbeRecordCompar
52bb0 65 28 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  e(int,const void
52bc0 2a 2c 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  *,UnpackedRecord
52bd0 2a 29 3b 0a 0a 0a 23 69 66 6e 64 65 66 20 4e 44  *);...#ifndef ND
52be0 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56  EBUG.SQLITE_PRIV
52bf0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
52c00 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56 64  e3VdbeComment(Vd
52c10 62 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  be*, const char*
52c20 2c 20 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65  , ...);.# define
52c30 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29 20   VdbeComment(X) 
52c40 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 6d 6d   sqlite3VdbeComm
52c50 65 6e 74 20 58 0a 53 51 4c 49 54 45 5f 50 52 49  ent X.SQLITE_PRI
52c60 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
52c70 74 65 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65  te3VdbeNoopComme
52c80 6e 74 28 56 64 62 65 2a 2c 20 63 6f 6e 73 74 20  nt(Vdbe*, const 
52c90 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 23 20 64  char*, ...);.# d
52ca0 65 66 69 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f  efine VdbeNoopCo
52cb0 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c 69 74 65  mment(X)  sqlite
52cc0 33 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74  3VdbeNoopComment
52cd0 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e   X.#else.# defin
52ce0 65 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 58 29  e VdbeComment(X)
52cf0 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 4e 6f  .# define VdbeNo
52d00 6f 70 43 6f 6d 6d 65 6e 74 28 58 29 0a 23 65 6e  opComment(X).#en
52d10 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  dif..#endif../**
52d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
52d30 20 6f 66 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a   of vdbe.h *****
52d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
52d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
52d80 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
52d90 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c   left off in sql
52da0 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
52db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
52dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
52dd0 6c 75 64 65 20 70 61 67 65 72 2e 68 20 69 6e 20  lude pager.h in 
52de0 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71  the middle of sq
52df0 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  liteInt.h ******
52e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
52e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
52e20 69 6e 20 66 69 6c 65 20 70 61 67 65 72 2e 68 20  in file pager.h 
52e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
52e60 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
52e70 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
52e80 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
52e90 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
52ea0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
52eb0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
52ec0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
52ed0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
52ee0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
52ef0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
52f00 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
52f10 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
52f20 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
52f30 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
52f40 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
52f50 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
52f60 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
52f70 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
52f80 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
52f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
52fd0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64  ***.** This head
52fe0 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20  er file defines 
52ff0 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 68  the interface th
53000 61 74 20 74 68 65 20 73 71 6c 69 74 65 20 70 61  at the sqlite pa
53010 67 65 20 63 61 63 68 65 0a 2a 2a 20 73 75 62 73  ge cache.** subs
53020 79 73 74 65 6d 2e 20 20 54 68 65 20 70 61 67 65  ystem.  The page
53030 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d   cache subsystem
53040 20 72 65 61 64 73 20 61 6e 64 20 77 72 69 74 65   reads and write
53050 73 20 61 20 66 69 6c 65 20 61 20 70 61 67 65 0a  s a file a page.
53060 2a 2a 20 61 74 20 61 20 74 69 6d 65 20 61 6e 64  ** at a time and
53070 20 70 72 6f 76 69 64 65 73 20 61 20 6a 6f 75 72   provides a jour
53080 6e 61 6c 20 66 6f 72 20 72 6f 6c 6c 62 61 63 6b  nal for rollback
53090 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64  ..**.** @(#) $Id
530a0 3a 20 70 61 67 65 72 2e 68 2c 76 20 31 2e 31 30  : pager.h,v 1.10
530b0 34 20 32 30 30 39 2f 30 37 2f 32 34 20 31 39 3a  4 2009/07/24 19:
530c0 30 31 3a 31 39 20 64 72 68 20 45 78 70 20 24 0a  01:19 drh Exp $.
530d0 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 41 47  */..#ifndef _PAG
530e0 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 50  ER_H_.#define _P
530f0 41 47 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 44  AGER_H_../*.** D
53100 65 66 61 75 6c 74 20 6d 61 78 69 6d 75 6d 20 73  efault maximum s
53110 69 7a 65 20 66 6f 72 20 70 65 72 73 69 73 74 65  ize for persiste
53120 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73  nt journal files
53130 2e 20 41 20 6e 65 67 61 74 69 76 65 20 0a 2a 2a  . A negative .**
53140 20 76 61 6c 75 65 20 6d 65 61 6e 73 20 6e 6f 20   value means no 
53150 6c 69 6d 69 74 2e 20 54 68 69 73 20 76 61 6c 75  limit. This valu
53160 65 20 6d 61 79 20 62 65 20 6f 76 65 72 72 69 64  e may be overrid
53170 64 65 6e 20 75 73 69 6e 67 20 74 68 65 20 0a 2a  den using the .*
53180 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f  * sqlite3PagerJo
53190 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 29  urnalSizeLimit()
531a0 20 41 50 49 2e 20 53 65 65 20 61 6c 73 6f 20 22   API. See also "
531b0 50 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 73  PRAGMA journal_s
531c0 69 7a 65 5f 6c 69 6d 69 74 22 2e 0a 2a 2f 0a 23  ize_limit"..*/.#
531d0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45  ifndef SQLITE_DE
531e0 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49  FAULT_JOURNAL_SI
531f0 5a 45 5f 4c 49 4d 49 54 0a 20 20 23 64 65 66 69  ZE_LIMIT.  #defi
53200 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  ne SQLITE_DEFAUL
53210 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c  T_JOURNAL_SIZE_L
53220 49 4d 49 54 20 2d 31 0a 23 65 6e 64 69 66 0a 0a  IMIT -1.#endif..
53230 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65 20 75  /*.** The type u
53240 73 65 64 20 74 6f 20 72 65 70 72 65 73 65 6e 74  sed to represent
53250 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20   a page number. 
53260 20 54 68 65 20 66 69 72 73 74 20 70 61 67 65 20   The first page 
53270 69 6e 20 61 20 66 69 6c 65 0a 2a 2a 20 69 73 20  in a file.** is 
53280 63 61 6c 6c 65 64 20 70 61 67 65 20 31 2e 20 20  called page 1.  
53290 30 20 69 73 20 75 73 65 64 20 74 6f 20 72 65 70  0 is used to rep
532a0 72 65 73 65 6e 74 20 22 6e 6f 74 20 61 20 70 61  resent "not a pa
532b0 67 65 22 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  ge"..*/.typedef 
532c0 75 33 32 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a 2a 2a  u32 Pgno;../*.**
532d0 20 45 61 63 68 20 6f 70 65 6e 20 66 69 6c 65 20   Each open file 
532e0 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20 61 20  is managed by a 
532f0 73 65 70 61 72 61 74 65 20 69 6e 73 74 61 6e 63  separate instanc
53300 65 20 6f 66 20 74 68 65 20 22 50 61 67 65 72 22  e of the "Pager"
53310 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74   structure..*/.t
53320 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 61  ypedef struct Pa
53330 67 65 72 20 50 61 67 65 72 3b 0a 0a 2f 2a 0a 2a  ger Pager;../*.*
53340 2a 20 48 61 6e 64 6c 65 20 74 79 70 65 20 66 6f  * Handle type fo
53350 72 20 70 61 67 65 73 2e 0a 2a 2f 0a 74 79 70 65  r pages..*/.type
53360 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72  def struct PgHdr
53370 20 44 62 50 61 67 65 3b 0a 0a 2f 2a 0a 2a 2a 20   DbPage;../*.** 
53380 50 61 67 65 20 6e 75 6d 62 65 72 20 50 41 47 45  Page number PAGE
53390 52 5f 4d 4a 5f 50 47 4e 4f 20 69 73 20 6e 65 76  R_MJ_PGNO is nev
533a0 65 72 20 75 73 65 64 20 69 6e 20 61 6e 20 53 51  er used in an SQ
533b0 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 28 69  Lite database (i
533c0 74 20 69 73 0a 2a 2a 20 72 65 73 65 72 76 65 64  t is.** reserved
533d0 20 66 6f 72 20 77 6f 72 6b 69 6e 67 20 61 72 6f   for working aro
533e0 75 6e 64 20 61 20 77 69 6e 64 6f 77 73 2f 70 6f  und a windows/po
533f0 73 69 78 20 69 6e 63 6f 6d 70 61 74 69 62 69 6c  six incompatibil
53400 69 74 79 29 2e 20 49 74 20 69 73 0a 2a 2a 20 75  ity). It is.** u
53410 73 65 64 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  sed in the journ
53420 61 6c 20 74 6f 20 73 69 67 6e 69 66 79 20 74 68  al to signify th
53430 61 74 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72  at the remainder
53440 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
53450 66 69 6c 65 20 0a 2a 2a 20 69 73 20 64 65 76 6f  file .** is devo
53460 74 65 64 20 74 6f 20 73 74 6f 72 69 6e 67 20 61  ted to storing a
53470 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
53480 6e 61 6d 65 20 2d 20 74 68 65 72 65 20 61 72 65  name - there are
53490 20 6e 6f 20 6d 6f 72 65 20 70 61 67 65 73 20 74   no more pages t
534a0 6f 0a 2a 2a 20 72 6f 6c 6c 20 62 61 63 6b 2e 20  o.** roll back. 
534b0 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72  See comments for
534c0 20 66 75 6e 63 74 69 6f 6e 20 77 72 69 74 65 4d   function writeM
534d0 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 69  asterJournal() i
534e0 6e 20 70 61 67 65 72 2e 63 20 0a 2a 2a 20 66 6f  n pager.c .** fo
534f0 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64  r details..*/.#d
53500 65 66 69 6e 65 20 50 41 47 45 52 5f 4d 4a 5f 50  efine PAGER_MJ_P
53510 47 4e 4f 28 78 29 20 28 28 50 67 6e 6f 29 28 28  GNO(x) ((Pgno)((
53520 50 45 4e 44 49 4e 47 5f 42 59 54 45 2f 28 28 78  PENDING_BYTE/((x
53530 29 2d 3e 70 61 67 65 53 69 7a 65 29 29 2b 31 29  )->pageSize))+1)
53540 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64  )../*.** Allowed
53550 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20   values for the 
53560 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20  flags parameter 
53570 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f  to sqlite3PagerO
53580 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54  pen()..**.** NOT
53590 45 3a 20 54 68 65 73 65 20 76 61 6c 75 65 73 20  E: These values 
535a0 6d 75 73 74 20 6d 61 74 63 68 20 74 68 65 20 63  must match the c
535b0 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 42 54 52  orresponding BTR
535c0 45 45 5f 20 76 61 6c 75 65 73 20 69 6e 20 62 74  EE_ values in bt
535d0 72 65 65 2e 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ree.h..*/.#defin
535e0 65 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55  e PAGER_OMIT_JOU
535f0 52 4e 41 4c 20 20 30 78 30 30 30 31 20 20 20 20  RNAL  0x0001    
53600 2f 2a 20 44 6f 20 6e 6f 74 20 75 73 65 20 61 20  /* Do not use a 
53610 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
53620 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45   */.#define PAGE
53630 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20  R_NO_READLOCK   
53640 30 78 30 30 30 32 20 20 20 20 2f 2a 20 4f 6d 69  0x0002    /* Omi
53650 74 20 72 65 61 64 6c 6f 63 6b 73 20 6f 6e 20 72  t readlocks on r
53660 65 61 64 6f 6e 6c 79 20 66 69 6c 65 73 20 2a 2f  eadonly files */
53670 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61  ../*.** Valid va
53680 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 65 63  lues for the sec
53690 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ond argument to 
536a0 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b  sqlite3PagerLock
536b0 69 6e 67 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64  ingMode()..*/.#d
536c0 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b  efine PAGER_LOCK
536d0 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 20 20 20  INGMODE_QUERY   
536e0 20 20 20 2d 31 0a 23 64 65 66 69 6e 65 20 50 41     -1.#define PA
536f0 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
53700 4e 4f 52 4d 41 4c 20 20 20 20 20 20 30 0a 23 64  NORMAL      0.#d
53710 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b  efine PAGER_LOCK
53720 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56  INGMODE_EXCLUSIV
53730 45 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c  E   1../*.** Val
53740 69 64 20 76 61 6c 75 65 73 20 66 6f 72 20 74 68  id values for th
53750 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
53760 74 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65  t to sqlite3Page
53770 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 29 2e 0a  rJournalMode()..
53780 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  */.#define PAGER
53790 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45  _JOURNALMODE_QUE
537a0 52 59 20 20 20 20 20 20 2d 31 0a 23 64 65 66 69  RY      -1.#defi
537b0 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  ne PAGER_JOURNAL
537c0 4d 4f 44 45 5f 44 45 4c 45 54 45 20 20 20 20 20  MODE_DELETE     
537d0 20 30 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62   0   /* Commit b
537e0 79 20 64 65 6c 65 74 69 6e 67 20 6a 6f 75 72 6e  y deleting journ
537f0 61 6c 20 66 69 6c 65 20 2a 2f 0a 23 64 65 66 69  al file */.#defi
53800 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  ne PAGER_JOURNAL
53810 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 20 20 20  MODE_PERSIST    
53820 20 31 20 20 20 2f 2a 20 43 6f 6d 6d 69 74 20 62   1   /* Commit b
53830 79 20 7a 65 72 6f 69 6e 67 20 6a 6f 75 72 6e 61  y zeroing journa
53840 6c 20 68 65 61 64 65 72 20 2a 2f 0a 23 64 65 66  l header */.#def
53850 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41  ine PAGER_JOURNA
53860 4c 4d 4f 44 45 5f 4f 46 46 20 20 20 20 20 20 20  LMODE_OFF       
53870 20 20 32 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c    2   /* Journal
53880 20 6f 6d 69 74 74 65 64 2e 20 20 2a 2f 0a 23 64   omitted.  */.#d
53890 65 66 69 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52  efine PAGER_JOUR
538a0 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45  NALMODE_TRUNCATE
538b0 20 20 20 20 33 20 20 20 2f 2a 20 43 6f 6d 6d 69      3   /* Commi
538c0 74 20 62 79 20 74 72 75 6e 63 61 74 69 6e 67 20  t by truncating 
538d0 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 65 66 69  journal */.#defi
538e0 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  ne PAGER_JOURNAL
538f0 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 20 20 20 20  MODE_MEMORY     
53900 20 34 20 20 20 2f 2a 20 49 6e 2d 6d 65 6d 6f 72   4   /* In-memor
53910 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a  y journal file *
53920 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 6d  /../*.** The rem
53930 61 69 6e 64 65 72 20 6f 66 20 74 68 69 73 20 66  ainder of this f
53940 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ile contains the
53950 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66   declarations of
53960 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a   the functions.*
53970 2a 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 74  * that make up t
53980 68 65 20 50 61 67 65 72 20 73 75 62 2d 73 79 73  he Pager sub-sys
53990 74 65 6d 20 41 50 49 2e 20 53 65 65 20 73 6f 75  tem API. See sou
539a0 72 63 65 20 63 6f 64 65 20 63 6f 6d 6d 65 6e 74  rce code comment
539b0 73 20 66 6f 72 20 0a 2a 2a 20 61 20 64 65 74 61  s for .** a deta
539c0 69 6c 65 64 20 64 65 73 63 72 69 70 74 69 6f 6e  iled description
539d0 20 6f 66 20 65 61 63 68 20 72 6f 75 74 69 6e 65   of each routine
539e0 2e 0a 2a 2f 0a 0a 2f 2a 20 4f 70 65 6e 20 61 6e  ..*/../* Open an
539f0 64 20 63 6c 6f 73 65 20 61 20 50 61 67 65 72 20  d close a Pager 
53a00 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 2a 2f 20 0a  connection. */ .
53a10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
53a20 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f  nt sqlite3PagerO
53a30 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  pen(.  sqlite3_v
53a40 66 73 2a 2c 0a 20 20 50 61 67 65 72 20 2a 2a 70  fs*,.  Pager **p
53a50 70 50 61 67 65 72 2c 0a 20 20 63 6f 6e 73 74 20  pPager,.  const 
53a60 63 68 61 72 2a 2c 0a 20 20 69 6e 74 2c 0a 20 20  char*,.  int,.  
53a70 69 6e 74 2c 0a 20 20 69 6e 74 2c 0a 20 20 76 6f  int,.  int,.  vo
53a80 69 64 28 2a 29 28 44 62 50 61 67 65 2a 29 0a 29  id(*)(DbPage*).)
53a90 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
53aa0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
53ab0 72 43 6c 6f 73 65 28 50 61 67 65 72 20 2a 70 50  rClose(Pager *pP
53ac0 61 67 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ager);.SQLITE_PR
53ad0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
53ae0 33 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65  3PagerReadFilehe
53af0 61 64 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74  ader(Pager*, int
53b00 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a  , unsigned char*
53b10 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f 6e 73  );../* Functions
53b20 20 75 73 65 64 20 74 6f 20 63 6f 6e 66 69 67 75   used to configu
53b30 72 65 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63  re a Pager objec
53b40 74 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t. */.SQLITE_PRI
53b50 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
53b60 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e  3PagerSetBusyhan
53b70 64 6c 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74  dler(Pager*, int
53b80 28 2a 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69  (*)(void *), voi
53b90 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  d *);.SQLITE_PRI
53ba0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
53bb0 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a 65  PagerSetPagesize
53bc0 28 50 61 67 65 72 2a 2c 20 75 31 36 2a 2c 20 69  (Pager*, u16*, i
53bd0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
53be0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
53bf0 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e 74  agerMaxPageCount
53c00 28 50 61 67 65 72 2a 2c 20 69 6e 74 29 3b 0a 53  (Pager*, int);.S
53c10 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
53c20 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  id sqlite3PagerS
53c30 65 74 43 61 63 68 65 73 69 7a 65 28 50 61 67 65  etCachesize(Page
53c40 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  r*, int);.SQLITE
53c50 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
53c60 6c 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66  lite3PagerSetSaf
53c70 65 74 79 4c 65 76 65 6c 28 50 61 67 65 72 2a 2c  etyLevel(Pager*,
53c80 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  int,int);.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 4c 6f 63 6b 69 6e 67  ite3PagerLocking
53cb0 4d 6f 64 65 28 50 61 67 65 72 20 2a 2c 20 69 6e  Mode(Pager *, in
53cc0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
53cd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
53ce0 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50  gerJournalMode(P
53cf0 61 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51  ager *, int);.SQ
53d00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34  LITE_PRIVATE i64
53d10 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75   sqlite3PagerJou
53d20 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61  rnalSizeLimit(Pa
53d30 67 65 72 20 2a 2c 20 69 36 34 29 3b 0a 53 51 4c  ger *, i64);.SQL
53d40 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69  ITE_PRIVATE sqli
53d50 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a 73 71 6c  te3_backup **sql
53d60 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 70 50  ite3PagerBackupP
53d70 74 72 28 50 61 67 65 72 2a 29 3b 0a 0a 2f 2a 20  tr(Pager*);../* 
53d80 46 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74  Functions used t
53d90 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 65 6c  o obtain and rel
53da0 65 61 73 65 20 70 61 67 65 20 72 65 66 65 72 65  ease page refere
53db0 6e 63 65 73 2e 20 2a 2f 20 0a 53 51 4c 49 54 45  nces. */ .SQLITE
53dc0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
53dd0 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72 65  ite3PagerAcquire
53de0 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
53df0 50 67 6e 6f 20 70 67 6e 6f 2c 20 44 62 50 61 67  Pgno pgno, DbPag
53e00 65 20 2a 2a 70 70 50 61 67 65 2c 20 69 6e 74 20  e **ppPage, int 
53e10 63 6c 72 46 6c 61 67 29 3b 0a 23 64 65 66 69 6e  clrFlag);.#defin
53e20 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  e sqlite3PagerGe
53e30 74 28 41 2c 42 2c 43 29 20 73 71 6c 69 74 65 33  t(A,B,C) sqlite3
53e40 50 61 67 65 72 41 63 71 75 69 72 65 28 41 2c 42  PagerAcquire(A,B
53e50 2c 43 2c 30 29 0a 53 51 4c 49 54 45 5f 50 52 49  ,C,0).SQLITE_PRI
53e60 56 41 54 45 20 44 62 50 61 67 65 20 2a 73 71 6c  VATE DbPage *sql
53e70 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28  ite3PagerLookup(
53e80 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50  Pager *pPager, P
53e90 67 6e 6f 20 70 67 6e 6f 29 3b 0a 53 51 4c 49 54  gno pgno);.SQLIT
53ea0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
53eb0 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 28 44  qlite3PagerRef(D
53ec0 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  bPage*);.SQLITE_
53ed0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
53ee0 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 44  ite3PagerUnref(D
53ef0 62 50 61 67 65 2a 29 3b 0a 0a 2f 2a 20 4f 70 65  bPage*);../* Ope
53f00 72 61 74 69 6f 6e 73 20 6f 6e 20 70 61 67 65 20  rations on page 
53f10 72 65 66 65 72 65 6e 63 65 73 2e 20 2a 2f 0a 53  references. */.S
53f20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
53f30 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  t sqlite3PagerWr
53f40 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51  ite(DbPage*);.SQ
53f50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
53f60 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f  d sqlite3PagerDo
53f70 6e 74 57 72 69 74 65 28 44 62 50 61 67 65 2a 29  ntWrite(DbPage*)
53f80 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
53f90 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
53fa0 72 4d 6f 76 65 70 61 67 65 28 50 61 67 65 72 2a  rMovepage(Pager*
53fb0 2c 44 62 50 61 67 65 2a 2c 50 67 6e 6f 2c 69 6e  ,DbPage*,Pgno,in
53fc0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
53fd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
53fe0 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28  gerPageRefcount(
53ff0 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45  DbPage*);.SQLITE
54000 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
54010 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61  qlite3PagerGetDa
54020 74 61 28 44 62 50 61 67 65 20 2a 29 3b 20 0a 53  ta(DbPage *); .S
54030 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
54040 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  id *sqlite3Pager
54050 47 65 74 45 78 74 72 61 28 44 62 50 61 67 65 20  GetExtra(DbPage 
54060 2a 29 3b 20 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f  *); ../* Functio
54070 6e 73 20 75 73 65 64 20 74 6f 20 6d 61 6e 61 67  ns used to manag
54080 65 20 70 61 67 65 72 20 74 72 61 6e 73 61 63 74  e pager transact
54090 69 6f 6e 73 20 61 6e 64 20 73 61 76 65 70 6f 69  ions and savepoi
540a0 6e 74 73 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  nts. */.SQLITE_P
540b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
540c0 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74  e3PagerPagecount
540d0 28 50 61 67 65 72 2a 2c 20 69 6e 74 2a 29 3b 0a  (Pager*, int*);.
540e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
540f0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 42  nt sqlite3PagerB
54100 65 67 69 6e 28 50 61 67 65 72 2a 2c 20 69 6e 74  egin(Pager*, int
54110 20 65 78 46 6c 61 67 2c 20 69 6e 74 29 3b 0a 53   exFlag, int);.S
54120 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
54130 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  t sqlite3PagerCo
54140 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 50 61 67  mmitPhaseOne(Pag
54150 65 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 20 2a  er*,const char *
54160 7a 4d 61 73 74 65 72 2c 20 69 6e 74 29 3b 0a 53  zMaster, int);.S
54170 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
54180 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79  t sqlite3PagerSy
54190 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  nc(Pager *pPager
541a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
541b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
541c0 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  erCommitPhaseTwo
541d0 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
541e0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
541f0 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63  ite3PagerRollbac
54200 6b 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54  k(Pager*);.SQLIT
54210 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
54220 6c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 53 61  lite3PagerOpenSa
54230 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70  vepoint(Pager *p
54240 50 61 67 65 72 2c 20 69 6e 74 20 6e 29 3b 0a 53  Pager, int n);.S
54250 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
54260 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 61  t sqlite3PagerSa
54270 76 65 70 6f 69 6e 74 28 50 61 67 65 72 20 2a 70  vepoint(Pager *p
54280 50 61 67 65 72 2c 20 69 6e 74 20 6f 70 2c 20 69  Pager, int op, i
54290 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a  nt iSavepoint);.
542a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
542b0 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  nt sqlite3PagerS
542c0 68 61 72 65 64 4c 6f 63 6b 28 50 61 67 65 72 20  haredLock(Pager 
542d0 2a 70 50 61 67 65 72 29 3b 0a 0a 2f 2a 20 46 75  *pPager);../* Fu
542e0 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20  nctions used to 
542f0 71 75 65 72 79 20 70 61 67 65 72 20 73 74 61 74  query pager stat
54300 65 20 61 6e 64 20 63 6f 6e 66 69 67 75 72 61 74  e and configurat
54310 69 6f 6e 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion. */.SQLITE_P
54320 52 49 56 41 54 45 20 75 38 20 73 71 6c 69 74 65  RIVATE u8 sqlite
54330 33 50 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79  3PagerIsreadonly
54340 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
54350 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
54360 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e  ite3PagerRefcoun
54370 74 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54  t(Pager*);.SQLIT
54380 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
54390 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50 61 67  char *sqlite3Pag
543a0 65 72 46 69 6c 65 6e 61 6d 65 28 50 61 67 65 72  erFilename(Pager
543b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
543c0 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  TE const sqlite3
543d0 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67  _vfs *sqlite3Pag
543e0 65 72 56 66 73 28 50 61 67 65 72 2a 29 3b 0a 53  erVfs(Pager*);.S
543f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71  QLITE_PRIVATE sq
54400 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71 6c 69  lite3_file *sqli
54410 74 65 33 50 61 67 65 72 46 69 6c 65 28 50 61 67  te3PagerFile(Pag
54420 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  er*);.SQLITE_PRI
54430 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
54440 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75  *sqlite3PagerJou
54450 72 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72 2a 29  rnalname(Pager*)
54460 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
54470 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
54480 72 4e 6f 73 79 6e 63 28 50 61 67 65 72 2a 29 3b  rNosync(Pager*);
54490 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
544a0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67  void *sqlite3Pag
544b0 65 72 54 65 6d 70 53 70 61 63 65 28 50 61 67 65  erTempSpace(Page
544c0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
544d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
544e0 61 67 65 72 49 73 4d 65 6d 64 62 28 50 61 67 65  agerIsMemdb(Page
544f0 72 2a 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69 6f  r*);../* Functio
54500 6e 73 20 75 73 65 64 20 74 6f 20 74 72 75 6e 63  ns used to trunc
54510 61 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ate the database
54520 20 66 69 6c 65 2e 20 2a 2f 0a 53 51 4c 49 54 45   file. */.SQLITE
54530 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
54540 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61  lite3PagerTrunca
54550 74 65 49 6d 61 67 65 28 50 61 67 65 72 2a 2c 50  teImage(Pager*,P
54560 67 6e 6f 29 3b 0a 0a 2f 2a 20 46 75 6e 63 74 69  gno);../* Functi
54570 6f 6e 73 20 74 6f 20 73 75 70 70 6f 72 74 20 74  ons to support t
54580 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67  esting and debug
54590 67 69 6e 67 2e 20 2a 2f 0a 23 69 66 20 21 64 65  ging. */.#if !de
545a0 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c  fined(NDEBUG) ||
545b0 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
545c0 54 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49  TEST).SQLITE_PRI
545d0 56 41 54 45 20 20 20 50 67 6e 6f 20 73 71 6c 69  VATE   Pgno sqli
545e0 74 65 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62  te3PagerPagenumb
545f0 65 72 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c  er(DbPage*);.SQL
54600 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
54610 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  t sqlite3PagerIs
54620 77 72 69 74 65 61 62 6c 65 28 44 62 50 61 67 65  writeable(DbPage
54630 2a 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  *);.#endif.#ifde
54640 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
54650 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
54660 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  nt *sqlite3Pager
54670 53 74 61 74 73 28 50 61 67 65 72 2a 29 3b 0a 53  Stats(Pager*);.S
54680 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
54690 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
546a0 72 52 65 66 64 75 6d 70 28 50 61 67 65 72 2a 29  rRefdump(Pager*)
546b0 3b 0a 20 20 76 6f 69 64 20 64 69 73 61 62 6c 65  ;.  void disable
546c0 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72  _simulated_io_er
546d0 72 6f 72 73 28 76 6f 69 64 29 3b 0a 20 20 76 6f  rors(void);.  vo
546e0 69 64 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61  id enable_simula
546f0 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f  ted_io_errors(vo
54700 69 64 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  id);.#else.# def
54710 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75  ine disable_simu
54720 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28  lated_io_errors(
54730 29 0a 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c  ).# define enabl
54740 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65  e_simulated_io_e
54750 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a  rrors().#endif..
54760 23 65 6e 64 69 66 20 2f 2a 20 5f 50 41 47 45 52  #endif /* _PAGER
54770 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  _H_ */../*******
54780 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70  ******* End of p
54790 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ager.h *********
547a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
547b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
547c0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
547d0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
547e0 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
547f0 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e   off in sqliteIn
54800 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
54810 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
54820 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
54830 70 63 61 63 68 65 2e 68 20 69 6e 20 74 68 65 20  pcache.h in the 
54840 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65  middle of sqlite
54850 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
54860 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
54870 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
54880 6c 65 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a  le pcache.h ****
54890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
548a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
548b0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
548c0 30 38 20 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a  08 August 05.**.
548d0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
548e0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
548f0 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
54900 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
54910 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
54920 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
54930 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
54940 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
54950 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
54960 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
54970 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
54980 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
54990 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
549a0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
549b0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
549c0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
549d0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
549e0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
549f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
54a30 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20  his header file 
54a40 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65  defines the inte
54a50 72 66 61 63 65 20 74 68 61 74 20 74 68 65 20 73  rface that the s
54a60 71 6c 69 74 65 20 70 61 67 65 20 63 61 63 68 65  qlite page cache
54a70 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 0a  .** subsystem. .
54a80 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20  **.** @(#) $Id: 
54a90 70 63 61 63 68 65 2e 68 2c 76 20 31 2e 32 30 20  pcache.h,v 1.20 
54aa0 32 30 30 39 2f 30 37 2f 32 35 20 31 31 3a 34 36  2009/07/25 11:46
54ab0 3a 34 39 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :49 danielk1977 
54ac0 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65  Exp $.*/..#ifnde
54ad0 66 20 5f 50 43 41 43 48 45 5f 48 5f 0a 0a 74 79  f _PCACHE_H_..ty
54ae0 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48  pedef struct PgH
54af0 64 72 20 50 67 48 64 72 3b 0a 74 79 70 65 64 65  dr PgHdr;.typede
54b00 66 20 73 74 72 75 63 74 20 50 43 61 63 68 65 20  f struct PCache 
54b10 50 43 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45  PCache;../*.** E
54b20 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65  very page in the
54b30 20 63 61 63 68 65 20 69 73 20 63 6f 6e 74 72 6f   cache is contro
54b40 6c 6c 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61  lled by an insta
54b50 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
54b60 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72  wing.** structur
54b70 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 50 67 48  e..*/.struct PgH
54b80 64 72 20 7b 0a 20 20 76 6f 69 64 20 2a 70 44 61  dr {.  void *pDa
54b90 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ta;             
54ba0 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74        /* Content
54bb0 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f   of this page */
54bc0 0a 20 20 76 6f 69 64 20 2a 70 45 78 74 72 61 3b  .  void *pExtra;
54bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54be0 20 20 2f 2a 20 45 78 74 72 61 20 63 6f 6e 74 65    /* Extra conte
54bf0 6e 74 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70  nt */.  PgHdr *p
54c00 44 69 72 74 79 3b 20 20 20 20 20 20 20 20 20 20  Dirty;          
54c10 20 20 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 69         /* Transi
54c20 65 6e 74 20 6c 69 73 74 20 6f 66 20 64 69 72 74  ent list of dirt
54c30 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 6e  y pages */.  Pgn
54c40 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20  o pgno;         
54c50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
54c60 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  age number for t
54c70 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 61  his page */.  Pa
54c80 67 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20  ger *pPager;    
54c90 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
54ca0 54 68 65 20 70 61 67 65 72 20 74 68 69 73 20 70  The pager this p
54cb0 61 67 65 20 69 73 20 70 61 72 74 20 6f 66 20 2a  age is part of *
54cc0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
54cd0 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 75 33  CHECK_PAGES.  u3
54ce0 32 20 70 61 67 65 48 61 73 68 3b 20 20 20 20 20  2 pageHash;     
54cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
54d00 48 61 73 68 20 6f 66 20 70 61 67 65 20 63 6f 6e  Hash of page con
54d10 74 65 6e 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  tent */.#endif. 
54d20 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20   u16 flags;     
54d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54d40 2f 2a 20 50 47 48 44 52 20 66 6c 61 67 73 20 64  /* PGHDR flags d
54d50 65 66 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a  efined below */.
54d60 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .  /************
54d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
54da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20  **********.  ** 
54db0 45 6c 65 6d 65 6e 74 73 20 61 62 6f 76 65 20 61  Elements above a
54dc0 72 65 20 70 75 62 6c 69 63 2e 20 20 41 6c 6c 20  re public.  All 
54dd0 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20  that follows is 
54de0 70 72 69 76 61 74 65 20 74 6f 20 70 63 61 63 68  private to pcach
54df0 65 2e 63 0a 20 20 2a 2a 20 61 6e 64 20 73 68 6f  e.c.  ** and sho
54e00 75 6c 64 20 6e 6f 74 20 62 65 20 61 63 63 65 73  uld not be acces
54e10 73 65 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64  sed by other mod
54e20 75 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 31 36  ules..  */.  i16
54e30 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20   nRef;          
54e40 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
54e50 75 6d 62 65 72 20 6f 66 20 75 73 65 72 73 20 6f  umber of users o
54e60 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20  f this page */. 
54e70 20 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 3b   PCache *pCache;
54e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54e90 2f 2a 20 43 61 63 68 65 20 74 68 61 74 20 6f 77  /* Cache that ow
54ea0 6e 73 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a  ns this page */.
54eb0 0a 20 20 50 67 48 64 72 20 2a 70 44 69 72 74 79  .  PgHdr *pDirty
54ec0 4e 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20  Next;           
54ed0 20 20 2f 2a 20 4e 65 78 74 20 65 6c 65 6d 65 6e    /* Next elemen
54ee0 74 20 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72  t in list of dir
54ef0 74 79 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67  ty pages */.  Pg
54f00 48 64 72 20 2a 70 44 69 72 74 79 50 72 65 76 3b  Hdr *pDirtyPrev;
54f10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
54f20 50 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74  Previous element
54f30 20 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74   in list of dirt
54f40 79 20 70 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f  y pages */.};../
54f50 2a 20 42 69 74 20 76 61 6c 75 65 73 20 66 6f 72  * Bit values for
54f60 20 50 67 48 64 72 2e 66 6c 61 67 73 20 2a 2f 0a   PgHdr.flags */.
54f70 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 44 49  #define PGHDR_DI
54f80 52 54 59 20 20 20 20 20 20 20 20 20 20 20 20 20  RTY             
54f90 30 78 30 30 32 20 20 2f 2a 20 50 61 67 65 20 68  0x002  /* Page h
54fa0 61 73 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64  as changed */.#d
54fb0 65 66 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44  efine PGHDR_NEED
54fc0 5f 53 59 4e 43 20 20 20 20 20 20 20 20 20 30 78  _SYNC         0x
54fd0 30 30 34 20 20 2f 2a 20 46 73 79 6e 63 20 74 68  004  /* Fsync th
54fe0 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  e rollback journ
54ff0 61 6c 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20  al before.      
55000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
55010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
55020 20 2a 2a 20 77 72 69 74 69 6e 67 20 74 68 69 73   ** writing this
55030 20 70 61 67 65 20 74 6f 20 74 68 65 20 64 61 74   page to the dat
55040 61 62 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65  abase */.#define
55050 20 50 47 48 44 52 5f 4e 45 45 44 5f 52 45 41 44   PGHDR_NEED_READ
55060 20 20 20 20 20 20 20 20 20 30 78 30 30 38 20 20           0x008  
55070 2f 2a 20 43 6f 6e 74 65 6e 74 20 69 73 20 75 6e  /* Content is un
55080 72 65 61 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  read */.#define 
55090 50 47 48 44 52 5f 52 45 55 53 45 5f 55 4e 4c 49  PGHDR_REUSE_UNLI
550a0 4b 45 4c 59 20 20 20 20 30 78 30 31 30 20 20 2f  KELY    0x010  /
550b0 2a 20 41 20 68 69 6e 74 20 74 68 61 74 20 72 65  * A hint that re
550c0 75 73 65 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20  use is unlikely 
550d0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52  */.#define PGHDR
550e0 5f 44 4f 4e 54 5f 57 52 49 54 45 20 20 20 20 20  _DONT_WRITE     
550f0 20 20 20 30 78 30 32 30 20 20 2f 2a 20 44 6f 20     0x020  /* Do 
55100 6e 6f 74 20 77 72 69 74 65 20 63 6f 6e 74 65 6e  not write conten
55110 74 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 0a 2f 2a  t to disk */../*
55120 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20   Initialize and 
55130 73 68 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67  shutdown the pag
55140 65 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65  e cache subsyste
55150 6d 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  m */.SQLITE_PRIV
55160 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
55170 63 61 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28  cacheInitialize(
55180 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52  void);.SQLITE_PR
55190 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
551a0 65 33 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e  e3PcacheShutdown
551b0 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20 50 61 67 65  (void);../* Page
551c0 20 63 61 63 68 65 20 62 75 66 66 65 72 20 6d 61   cache buffer ma
551d0 6e 61 67 65 6d 65 6e 74 3a 0a 2a 2a 20 54 68 65  nagement:.** The
551e0 73 65 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c  se routines impl
551f0 65 6d 65 6e 74 20 53 51 4c 49 54 45 5f 43 4f 4e  ement SQLITE_CON
55200 46 49 47 5f 50 41 47 45 43 41 43 48 45 2e 0a 2a  FIG_PAGECACHE..*
55210 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
55220 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61   void sqlite3PCa
55230 63 68 65 42 75 66 66 65 72 53 65 74 75 70 28 76  cheBufferSetup(v
55240 6f 69 64 20 2a 2c 20 69 6e 74 20 73 7a 2c 20 69  oid *, int sz, i
55250 6e 74 20 6e 29 3b 0a 0a 2f 2a 20 43 72 65 61 74  nt n);../* Creat
55260 65 20 61 20 6e 65 77 20 70 61 67 65 72 20 63 61  e a new pager ca
55270 63 68 65 2e 0a 2a 2a 20 55 6e 64 65 72 20 6d 65  che..** Under me
55280 6d 6f 72 79 20 73 74 72 65 73 73 2c 20 69 6e 76  mory stress, inv
55290 6f 6b 65 20 78 53 74 72 65 73 73 20 74 6f 20 74  oke xStress to t
552a0 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73  ry to make pages
552b0 20 63 6c 65 61 6e 2e 0a 2a 2a 20 4f 6e 6c 79 20   clean..** Only 
552c0 63 6c 65 61 6e 20 61 6e 64 20 75 6e 70 69 6e 6e  clean and unpinn
552d0 65 64 20 70 61 67 65 73 20 63 61 6e 20 62 65 20  ed pages can be 
552e0 72 65 63 6c 61 69 6d 65 64 2e 0a 2a 2f 0a 53 51  reclaimed..*/.SQ
552f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
55300 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f  d sqlite3PcacheO
55310 70 65 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61 67  pen(.  int szPag
55320 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
55330 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
55340 20 65 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20   every page */. 
55350 20 69 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20   int szExtra,   
55360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
55370 2f 2a 20 45 78 74 72 61 20 73 70 61 63 65 20 61  /* Extra space a
55380 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 65  ssociated with e
55390 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e  ach page */.  in
553a0 74 20 62 50 75 72 67 65 61 62 6c 65 2c 20 20 20  t bPurgeable,   
553b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
553c0 54 72 75 65 20 69 66 20 70 61 67 65 73 20 61 72  True if pages ar
553d0 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f  e on backing sto
553e0 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53  re */.  int (*xS
553f0 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 20 50 67  tress)(void*, Pg
55400 48 64 72 2a 29 2c 20 2f 2a 20 43 61 6c 6c 20 74  Hdr*), /* Call t
55410 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61  o try to make pa
55420 67 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76  ges clean */.  v
55430 6f 69 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20  oid *pStress,   
55440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
55450 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74   Argument to xSt
55460 72 65 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65  ress */.  PCache
55470 20 2a 70 54 6f 49 6e 69 74 20 20 20 20 20 20 20   *pToInit       
55480 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61           /* Prea
55490 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20 66  llocated space f
554a0 6f 72 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f  or the PCache */
554b0 0a 29 3b 0a 0a 2f 2a 20 4d 6f 64 69 66 79 20 74  .);../* Modify t
554c0 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 66 74  he page-size aft
554d0 65 72 20 74 68 65 20 63 61 63 68 65 20 68 61 73  er the cache has
554e0 20 62 65 65 6e 20 63 72 65 61 74 65 64 2e 20 2a   been created. *
554f0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
55500 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
55510 63 68 65 53 65 74 50 61 67 65 53 69 7a 65 28 50  cheSetPageSize(P
55520 43 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a  Cache *, int);..
55530 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69  /* Return the si
55540 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 61  ze in bytes of a
55550 20 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20   PCache object. 
55560 20 55 73 65 64 20 74 6f 20 70 72 65 61 6c 6c 6f   Used to preallo
55570 63 61 74 65 0a 2a 2a 20 73 74 6f 72 61 67 65 20  cate.** storage 
55580 73 70 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  space..*/.SQLITE
55590 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
555a0 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28 76  ite3PcacheSize(v
555b0 6f 69 64 29 3b 0a 0a 2f 2a 20 4f 6e 65 20 72 65  oid);../* One re
555c0 6c 65 61 73 65 20 70 65 72 20 73 75 63 63 65 73  lease per succes
555d0 73 66 75 6c 20 66 65 74 63 68 2e 20 20 50 61 67  sful fetch.  Pag
555e0 65 20 69 73 20 70 69 6e 6e 65 64 20 75 6e 74 69  e is pinned unti
555f0 6c 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 20 52  l released..** R
55600 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 65 64  eference counted
55610 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  . .*/.SQLITE_PRI
55620 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
55630 50 63 61 63 68 65 46 65 74 63 68 28 50 43 61 63  PcacheFetch(PCac
55640 68 65 2a 2c 20 50 67 6e 6f 2c 20 69 6e 74 20 63  he*, Pgno, int c
55650 72 65 61 74 65 46 6c 61 67 2c 20 50 67 48 64 72  reateFlag, PgHdr
55660 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
55670 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
55680 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 50 67  PcacheRelease(Pg
55690 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50  Hdr*);..SQLITE_P
556a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
556b0 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 50 67  te3PcacheDrop(Pg
556c0 48 64 72 2a 29 3b 20 20 20 20 20 20 20 20 20 2f  Hdr*);         /
556d0 2a 20 52 65 6d 6f 76 65 20 70 61 67 65 20 66 72  * Remove page fr
556e0 6f 6d 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49  om cache */.SQLI
556f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
55700 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b  sqlite3PcacheMak
55710 65 44 69 72 74 79 28 50 67 48 64 72 2a 29 3b 20  eDirty(PgHdr*); 
55720 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20     /* Make sure 
55730 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 64  page is marked d
55740 69 72 74 79 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  irty */.SQLITE_P
55750 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
55760 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65  te3PcacheMakeCle
55770 61 6e 28 50 67 48 64 72 2a 29 3b 20 20 20 20 2f  an(PgHdr*);    /
55780 2a 20 4d 61 72 6b 20 61 20 73 69 6e 67 6c 65 20  * Mark a single 
55790 70 61 67 65 20 61 73 20 63 6c 65 61 6e 20 2a 2f  page as clean */
557a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
557b0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
557c0 68 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68  heCleanAll(PCach
557d0 65 2a 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20  e*);    /* Mark 
557e0 61 6c 6c 20 64 69 72 74 79 20 6c 69 73 74 20 70  all dirty list p
557f0 61 67 65 73 20 61 73 20 63 6c 65 61 6e 20 2a 2f  ages as clean */
55800 0a 0a 2f 2a 20 43 68 61 6e 67 65 20 61 20 70 61  ../* Change a pa
55810 67 65 20 6e 75 6d 62 65 72 2e 20 20 55 73 65 64  ge number.  Used
55820 20 62 79 20 69 6e 63 72 2d 76 61 63 75 75 6d 2e   by incr-vacuum.
55830 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41   */.SQLITE_PRIVA
55840 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
55850 63 61 63 68 65 4d 6f 76 65 28 50 67 48 64 72 2a  cacheMove(PgHdr*
55860 2c 20 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 52 65 6d  , Pgno);../* Rem
55870 6f 76 65 20 61 6c 6c 20 70 61 67 65 73 20 77 69  ove all pages wi
55880 74 68 20 70 67 6e 6f 3e 78 2e 20 20 52 65 73 65  th pgno>x.  Rese
55890 74 20 74 68 65 20 63 61 63 68 65 20 69 66 20 78  t the cache if x
558a0 3d 3d 30 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ==0 */.SQLITE_PR
558b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
558c0 65 33 50 63 61 63 68 65 54 72 75 6e 63 61 74 65  e3PcacheTruncate
558d0 28 50 43 61 63 68 65 2a 2c 20 50 67 6e 6f 20 78  (PCache*, Pgno x
558e0 29 3b 0a 0a 2f 2a 20 47 65 74 20 61 20 6c 69 73  );../* Get a lis
558f0 74 20 6f 66 20 61 6c 6c 20 64 69 72 74 79 20 70  t of all dirty p
55900 61 67 65 73 20 69 6e 20 74 68 65 20 63 61 63 68  ages in the cach
55910 65 2c 20 73 6f 72 74 65 64 20 62 79 20 70 61 67  e, sorted by pag
55920 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 53 51 4c 49  e number */.SQLI
55930 54 45 5f 50 52 49 56 41 54 45 20 50 67 48 64 72  TE_PRIVATE PgHdr
55940 20 2a 73 71 6c 69 74 65 33 50 63 61 63 68 65 44   *sqlite3PcacheD
55950 69 72 74 79 4c 69 73 74 28 50 43 61 63 68 65 2a  irtyList(PCache*
55960 29 3b 0a 0a 2f 2a 20 52 65 73 65 74 20 61 6e 64  );../* Reset and
55970 20 63 6c 6f 73 65 20 74 68 65 20 63 61 63 68 65   close the cache
55980 20 6f 62 6a 65 63 74 20 2a 2f 0a 53 51 4c 49 54   object */.SQLIT
55990 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
559a0 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73  qlite3PcacheClos
559b0 65 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20  e(PCache*);../* 
559c0 43 6c 65 61 72 20 66 6c 61 67 73 20 66 72 6f 6d  Clear flags from
559d0 20 70 61 67 65 73 20 6f 66 20 74 68 65 20 70 61   pages of the pa
559e0 67 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49  ge cache */.SQLI
559f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
55a00 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65  sqlite3PcacheCle
55a10 61 72 53 79 6e 63 46 6c 61 67 73 28 50 43 61 63  arSyncFlags(PCac
55a20 68 65 20 2a 29 3b 0a 0a 2f 2a 20 44 69 73 63 61  he *);../* Disca
55a30 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  rd the contents 
55a40 6f 66 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a  of the cache */.
55a50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
55a60 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
55a70 65 43 6c 65 61 72 28 50 43 61 63 68 65 2a 29 3b  eClear(PCache*);
55a80 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20  ../* Return the 
55a90 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
55aa0 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65  outstanding page
55ab0 20 72 65 66 65 72 65 6e 63 65 73 20 2a 2f 0a 53   references */.S
55ac0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
55ad0 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52  t sqlite3PcacheR
55ae0 65 66 43 6f 75 6e 74 28 50 43 61 63 68 65 2a 29  efCount(PCache*)
55af0 3b 0a 0a 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20  ;../* Increment 
55b00 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f  the reference co
55b10 75 6e 74 20 6f 66 20 61 6e 20 65 78 69 73 74 69  unt of an existi
55b20 6e 67 20 70 61 67 65 20 2a 2f 0a 53 51 4c 49 54  ng page */.SQLIT
55b30 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
55b40 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 28  qlite3PcacheRef(
55b50 50 67 48 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45  PgHdr*);..SQLITE
55b60 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
55b70 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52 65  ite3PcachePageRe
55b80 66 63 6f 75 6e 74 28 50 67 48 64 72 2a 29 3b 0a  fcount(PgHdr*);.
55b90 0a 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74  ./* Return the t
55ba0 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70  otal number of p
55bb0 61 67 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74  ages stored in t
55bc0 68 65 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49  he cache */.SQLI
55bd0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
55be0 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65  qlite3PcachePage
55bf0 63 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a  count(PCache*);.
55c00 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
55c10 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 29  ITE_CHECK_PAGES)
55c20 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
55c30 54 45 5f 44 45 42 55 47 29 0a 2f 2a 20 49 74 65  TE_DEBUG)./* Ite
55c40 72 61 74 65 20 74 68 72 6f 75 67 68 20 61 6c 6c  rate through all
55c50 20 64 69 72 74 79 20 70 61 67 65 73 20 63 75 72   dirty pages cur
55c60 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e  rently stored in
55c70 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 69 73   the cache. This
55c80 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73  .** interface is
55c90 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20   only available 
55ca0 69 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f  if SQLITE_CHECK_
55cb0 50 41 47 45 53 20 69 73 20 64 65 66 69 6e 65 64  PAGES is defined
55cc0 20 77 68 65 6e 20 74 68 65 20 0a 2a 2a 20 6c 69   when the .** li
55cd0 62 72 61 72 79 20 69 73 20 62 75 69 6c 74 2e 0a  brary is built..
55ce0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
55cf0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
55d00 61 63 68 65 49 74 65 72 61 74 65 44 69 72 74 79  acheIterateDirty
55d10 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c  (PCache *pCache,
55d20 20 76 6f 69 64 20 28 2a 78 49 74 65 72 29 28 50   void (*xIter)(P
55d30 67 48 64 72 20 2a 29 29 3b 0a 23 65 6e 64 69 66  gHdr *));.#endif
55d40 0a 0a 2f 2a 20 53 65 74 20 61 6e 64 20 67 65 74  ../* Set and get
55d50 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20 63   the suggested c
55d60 61 63 68 65 2d 73 69 7a 65 20 66 6f 72 20 74 68  ache-size for th
55d70 65 20 73 70 65 63 69 66 69 65 64 20 70 61 67 65  e specified page
55d80 72 2d 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 49  r-cache..**.** I
55d90 66 20 6e 6f 20 67 6c 6f 62 61 6c 20 6d 61 78 69  f no global maxi
55da0 6d 75 6d 20 69 73 20 63 6f 6e 66 69 67 75 72 65  mum is configure
55db0 64 2c 20 74 68 65 6e 20 74 68 65 20 73 79 73 74  d, then the syst
55dc0 65 6d 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c  em attempts to l
55dd0 69 6d 69 74 0a 2a 2a 20 74 68 65 20 74 6f 74 61  imit.** the tota
55de0 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  l number of page
55df0 73 20 63 61 63 68 65 64 20 62 79 20 70 75 72 67  s cached by purg
55e00 65 61 62 6c 65 20 70 61 67 65 72 2d 63 61 63 68  eable pager-cach
55e10 65 73 20 74 6f 20 74 68 65 20 73 75 6d 0a 2a 2a  es to the sum.**
55e20 20 6f 66 20 74 68 65 20 73 75 67 67 65 73 74 65   of the suggeste
55e30 64 20 63 61 63 68 65 2d 73 69 7a 65 73 2e 0a 2a  d cache-sizes..*
55e40 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
55e50 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
55e60 63 68 65 53 65 74 43 61 63 68 65 73 69 7a 65 28  cheSetCachesize(
55e70 50 43 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a  PCache *, int);.
55e80 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
55e90 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ST.SQLITE_PRIVAT
55ea0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61  E int sqlite3Pca
55eb0 63 68 65 47 65 74 43 61 63 68 65 73 69 7a 65 28  cheGetCachesize(
55ec0 50 43 61 63 68 65 20 2a 29 3b 0a 23 65 6e 64 69  PCache *);.#endi
55ed0 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
55ee0 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d  _ENABLE_MEMORY_M
55ef0 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 20 54 72 79  ANAGEMENT./* Try
55f00 20 74 6f 20 72 65 74 75 72 6e 20 6d 65 6d 6f 72   to return memor
55f10 79 20 75 73 65 64 20 62 79 20 74 68 65 20 70 63  y used by the pc
55f20 61 63 68 65 20 6d 6f 64 75 6c 65 20 74 6f 20 74  ache module to t
55f30 68 65 20 6d 61 69 6e 20 6d 65 6d 6f 72 79 20 68  he main memory h
55f40 65 61 70 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  eap */.SQLITE_PR
55f50 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
55f60 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 4d 65  3PcacheReleaseMe
55f70 6d 6f 72 79 28 69 6e 74 29 3b 0a 23 65 6e 64 69  mory(int);.#endi
55f80 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  f..#ifdef SQLITE
55f90 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49  _TEST.SQLITE_PRI
55fa0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
55fb0 33 50 63 61 63 68 65 53 74 61 74 73 28 69 6e 74  3PcacheStats(int
55fc0 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a  *,int*,int*,int*
55fd0 29 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54  );.#endif..SQLIT
55fe0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
55ff0 71 6c 69 74 65 33 50 43 61 63 68 65 53 65 74 44  qlite3PCacheSetD
56000 65 66 61 75 6c 74 28 76 6f 69 64 29 3b 0a 0a 23  efault(void);..#
56010 65 6e 64 69 66 20 2f 2a 20 5f 50 43 41 43 48 45  endif /* _PCACHE
56020 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  _H_ */../*******
56030 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70  ******* End of p
56040 63 61 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  cache.h ********
56050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56070 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
56080 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
56090 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
560a0 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e   off in sqliteIn
560b0 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
560c0 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  ******/../******
560d0 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
560e0 20 6f 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64   os.h in the mid
560f0 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74  dle of sqliteInt
56100 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
56110 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
56120 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
56130 69 6c 65 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a  ile os.h *******
56140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
56160 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
56170 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 36  001 September 16
56180 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
56190 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
561a0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
561b0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
561c0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
561d0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
561e0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
561f0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
56200 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
56210 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
56220 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
56230 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
56240 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
56250 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
56260 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
56270 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
56280 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
56290 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
562a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
562b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
562c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
562d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
562e0 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
562f0 68 65 61 64 65 72 20 66 69 6c 65 20 28 74 6f 67  header file (tog
56300 65 74 68 65 72 20 77 69 74 68 20 69 73 20 63 6f  ether with is co
56310 6d 70 61 6e 69 6f 6e 20 43 20 73 6f 75 72 63 65  mpanion C source
56320 2d 63 6f 64 65 20 66 69 6c 65 0a 2a 2a 20 22 6f  -code file.** "o
56330 73 2e 63 22 29 20 61 74 74 65 6d 70 74 20 74 6f  s.c") attempt to
56340 20 61 62 73 74 72 61 63 74 20 74 68 65 20 75 6e   abstract the un
56350 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69  derlying operati
56360 6e 67 20 73 79 73 74 65 6d 20 73 6f 20 74 68 61  ng system so tha
56370 74 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20  t.** the SQLite 
56380 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 77 6f 72  library will wor
56390 6b 20 6f 6e 20 62 6f 74 68 20 50 4f 53 49 58 20  k on both POSIX 
563a0 61 6e 64 20 77 69 6e 64 6f 77 73 20 73 79 73 74  and windows syst
563b0 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ems..**.** This 
563c0 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 20 23  header file is #
563d0 69 6e 63 6c 75 64 65 2d 65 64 20 62 79 20 73 71  include-ed by sq
563e0 6c 69 74 65 49 6e 74 2e 68 20 61 6e 64 20 74 68  liteInt.h and th
563f0 75 73 20 65 6e 64 73 20 75 70 0a 2a 2a 20 62 65  us ends up.** be
56400 69 6e 67 20 69 6e 63 6c 75 64 65 64 20 62 79 20  ing included by 
56410 65 76 65 72 79 20 73 6f 75 72 63 65 20 66 69 6c  every source fil
56420 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73  e..**.** $Id: os
56430 2e 68 2c 76 20 31 2e 31 30 38 20 32 30 30 39 2f  .h,v 1.108 2009/
56440 30 32 2f 30 35 20 31 36 3a 33 31 3a 34 36 20 64  02/05 16:31:46 d
56450 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e  rh Exp $.*/.#ifn
56460 64 65 66 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 48  def _SQLITE_OS_H
56470 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54  _.#define _SQLIT
56480 45 5f 4f 53 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 46  E_OS_H_../*.** F
56490 69 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20  igure out if we 
564a0 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68  are dealing with
564b0 20 55 6e 69 78 2c 20 57 69 6e 64 6f 77 73 2c 20   Unix, Windows, 
564c0 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a  or some other.**
564d0 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65   operating syste
564e0 6d 2e 20 20 41 66 74 65 72 20 74 68 65 20 66 6f  m.  After the fo
564f0 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 6f 66  llowing block of
56500 20 70 72 65 70 72 6f 63 65 73 73 20 6d 61 63 72   preprocess macr
56510 6f 73 2c 0a 2a 2a 20 61 6c 6c 20 6f 66 20 53 51  os,.** all of SQ
56520 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 2c 20 53 51  LITE_OS_UNIX, SQ
56530 4c 49 54 45 5f 4f 53 5f 57 49 4e 2c 20 53 51 4c  LITE_OS_WIN, SQL
56540 49 54 45 5f 4f 53 5f 4f 53 32 2c 20 61 6e 64 20  ITE_OS_OS2, and 
56550 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 20  SQLITE_OS_OTHER 
56560 0a 2a 2a 20 77 69 6c 6c 20 64 65 66 69 6e 65 64  .** will defined
56570 20 74 6f 20 65 69 74 68 65 72 20 31 20 6f 72 20   to either 1 or 
56580 30 2e 20 20 4f 6e 65 20 6f 66 20 74 68 65 20 66  0.  One of the f
56590 6f 75 72 20 77 69 6c 6c 20 62 65 20 31 2e 20 20  our will be 1.  
565a0 54 68 65 20 6f 74 68 65 72 20 0a 2a 2a 20 74 68  The other .** th
565b0 72 65 65 20 77 69 6c 6c 20 62 65 20 30 2e 0a 2a  ree will be 0..*
565c0 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  /.#if defined(SQ
565d0 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23  LITE_OS_OTHER).#
565e0 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54   if SQLITE_OS_OT
565f0 48 45 52 3d 3d 31 0a 23 20 20 20 75 6e 64 65 66  HER==1.#   undef
56600 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a   SQLITE_OS_UNIX.
56610 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54  #   define SQLIT
56620 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20  E_OS_UNIX 0.#   
56630 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f  undef SQLITE_OS_
56640 57 49 4e 0a 23 20 20 20 64 65 66 69 6e 65 20 53  WIN.#   define S
56650 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23  QLITE_OS_WIN 0.#
56660 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f     undef SQLITE_
56670 4f 53 5f 4f 53 32 0a 23 20 20 20 64 65 66 69 6e  OS_OS2.#   defin
56680 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20  e SQLITE_OS_OS2 
56690 30 0a 23 20 65 6c 73 65 0a 23 20 20 20 75 6e 64  0.# else.#   und
566a0 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48  ef SQLITE_OS_OTH
566b0 45 52 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69  ER.# endif.#endi
566c0 66 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  f.#if !defined(S
566d0 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 29 20 26  QLITE_OS_UNIX) &
566e0 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
566f0 45 5f 4f 53 5f 4f 54 48 45 52 29 0a 23 20 64 65  E_OS_OTHER).# de
56700 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 4f  fine SQLITE_OS_O
56710 54 48 45 52 20 30 0a 23 20 69 66 6e 64 65 66 20  THER 0.# ifndef 
56720 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20  SQLITE_OS_WIN.# 
56730 20 20 69 66 20 64 65 66 69 6e 65 64 28 5f 57 49    if defined(_WI
56740 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  N32) || defined(
56750 57 49 4e 33 32 29 20 7c 7c 20 64 65 66 69 6e 65  WIN32) || define
56760 64 28 5f 5f 43 59 47 57 49 4e 5f 5f 29 20 7c 7c  d(__CYGWIN__) ||
56770 20 64 65 66 69 6e 65 64 28 5f 5f 4d 49 4e 47 57   defined(__MINGW
56780 33 32 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64  32__) || defined
56790 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 23  (__BORLANDC__).#
567a0 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49       define SQLI
567b0 54 45 5f 4f 53 5f 57 49 4e 20 31 0a 23 20 20 20  TE_OS_WIN 1.#   
567c0 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
567d0 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 20  OS_UNIX 0.#     
567e0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
567f0 5f 4f 53 32 20 30 0a 23 20 20 20 65 6c 69 66 20  _OS2 0.#   elif 
56800 64 65 66 69 6e 65 64 28 5f 5f 45 4d 58 5f 5f 29  defined(__EMX__)
56810 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53 32   || defined(_OS2
56820 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 4f 53 32  ) || defined(OS2
56830 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4f 53  ) || defined(_OS
56840 32 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  2_) || defined(_
56850 5f 4f 53 32 5f 5f 29 0a 23 20 20 20 20 20 64 65  _OS2__).#     de
56860 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57  fine SQLITE_OS_W
56870 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e  IN 0.#     defin
56880 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58  e SQLITE_OS_UNIX
56890 20 30 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20   0.#     define 
568a0 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 31 0a  SQLITE_OS_OS2 1.
568b0 23 20 20 20 65 6c 73 65 0a 23 20 20 20 20 20 64  #   else.#     d
568c0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
568d0 57 49 4e 20 30 0a 23 20 20 20 20 20 64 65 66 69  WIN 0.#     defi
568e0 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49  ne SQLITE_OS_UNI
568f0 58 20 31 0a 23 20 20 20 20 20 64 65 66 69 6e 65  X 1.#     define
56900 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 30   SQLITE_OS_OS2 0
56910 0a 23 20 20 65 6e 64 69 66 0a 23 20 65 6c 73 65  .#  endif.# else
56920 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54  .#  define SQLIT
56930 45 5f 4f 53 5f 55 4e 49 58 20 30 0a 23 20 20 64  E_OS_UNIX 0.#  d
56940 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
56950 4f 53 32 20 30 0a 23 20 65 6e 64 69 66 0a 23 65  OS2 0.# endif.#e
56960 6c 73 65 0a 23 20 69 66 6e 64 65 66 20 53 51 4c  lse.# ifndef SQL
56970 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 64 65  ITE_OS_WIN.#  de
56980 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57  fine SQLITE_OS_W
56990 49 4e 20 30 0a 23 20 65 6e 64 69 66 0a 23 65 6e  IN 0.# endif.#en
569a0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72  dif../*.** Deter
569b0 6d 69 6e 65 20 69 66 20 77 65 20 61 72 65 20 64  mine if we are d
569c0 65 61 6c 69 6e 67 20 77 69 74 68 20 57 69 6e 64  ealing with Wind
569d0 6f 77 73 43 45 20 2d 20 77 68 69 63 68 20 68 61  owsCE - which ha
569e0 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72 65 64 75  s a much.** redu
569f0 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23 69 66 20  ced API..*/.#if 
56a00 64 65 66 69 6e 65 64 28 5f 57 49 4e 33 32 5f 57  defined(_WIN32_W
56a10 43 45 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  CE).# define SQL
56a20 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 31 0a 23  ITE_OS_WINCE 1.#
56a30 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51  else.# define SQ
56a40 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 30 0a  LITE_OS_WINCE 0.
56a50 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44  #endif.../*.** D
56a60 65 66 69 6e 65 20 74 68 65 20 6d 61 78 69 6d 75  efine the maximu
56a70 6d 20 73 69 7a 65 20 6f 66 20 61 20 74 65 6d 70  m size of a temp
56a80 6f 72 61 72 79 20 66 69 6c 65 6e 61 6d 65 0a 2a  orary filename.*
56a90 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  /.#if SQLITE_OS_
56aa0 57 49 4e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 77  WIN.# include <w
56ab0 69 6e 64 6f 77 73 2e 68 3e 0a 23 20 64 65 66 69  indows.h>.# defi
56ac0 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41  ne SQLITE_TEMPNA
56ad0 4d 45 5f 53 49 5a 45 20 28 4d 41 58 5f 50 41 54  ME_SIZE (MAX_PAT
56ae0 48 2b 35 30 29 0a 23 65 6c 69 66 20 53 51 4c 49  H+50).#elif SQLI
56af0 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 69 66 20 28  TE_OS_OS2.# if (
56b00 5f 5f 47 4e 55 43 5f 5f 20 3e 20 33 20 7c 7c 20  __GNUC__ > 3 || 
56b10 5f 5f 47 4e 55 43 5f 5f 20 3d 3d 20 33 20 26 26  __GNUC__ == 3 &&
56b20 20 5f 5f 47 4e 55 43 5f 4d 49 4e 4f 52 5f 5f 20   __GNUC_MINOR__ 
56b30 3e 3d 20 33 29 20 26 26 20 64 65 66 69 6e 65 64  >= 3) && defined
56b40 28 4f 53 32 5f 48 49 47 48 5f 4d 45 4d 4f 52 59  (OS2_HIGH_MEMORY
56b50 29 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 6f 73  ).#  include <os
56b60 32 73 61 66 65 2e 68 3e 20 2f 2a 20 68 61 73 20  2safe.h> /* has 
56b70 74 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 62  to be included b
56b80 65 66 6f 72 65 20 6f 73 32 2e 68 20 66 6f 72 20  efore os2.h for 
56b90 6c 69 6e 6b 69 6e 67 20 74 6f 20 77 6f 72 6b 20  linking to work 
56ba0 2a 2f 0a 23 20 65 6e 64 69 66 0a 23 20 64 65 66  */.# endif.# def
56bb0 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 44 41 54 45  ine INCL_DOSDATE
56bc0 54 49 4d 45 0a 23 20 64 65 66 69 6e 65 20 49 4e  TIME.# define IN
56bd0 43 4c 5f 44 4f 53 46 49 4c 45 4d 47 52 0a 23 20  CL_DOSFILEMGR.# 
56be0 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 45  define INCL_DOSE
56bf0 52 52 4f 52 53 0a 23 20 64 65 66 69 6e 65 20 49  RRORS.# define I
56c00 4e 43 4c 5f 44 4f 53 4d 49 53 43 0a 23 20 64 65  NCL_DOSMISC.# de
56c10 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 50 52 4f  fine INCL_DOSPRO
56c20 43 45 53 53 0a 23 20 64 65 66 69 6e 65 20 49 4e  CESS.# define IN
56c30 43 4c 5f 44 4f 53 4d 4f 44 55 4c 45 4d 47 52 0a  CL_DOSMODULEMGR.
56c40 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44 4f  # define INCL_DO
56c50 53 53 45 4d 41 50 48 4f 52 45 53 0a 23 20 69 6e  SSEMAPHORES.# in
56c60 63 6c 75 64 65 20 3c 6f 73 32 2e 68 3e 0a 23 20  clude <os2.h>.# 
56c70 69 6e 63 6c 75 64 65 20 3c 75 63 6f 6e 76 2e 68  include <uconv.h
56c80 3e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  >.# define SQLIT
56c90 45 5f 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20  E_TEMPNAME_SIZE 
56ca0 28 43 43 48 4d 41 58 50 41 54 48 43 4f 4d 50 29  (CCHMAXPATHCOMP)
56cb0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
56cc0 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45 5f  SQLITE_TEMPNAME_
56cd0 53 49 5a 45 20 32 30 30 0a 23 65 6e 64 69 66 0a  SIZE 200.#endif.
56ce0 0a 2f 2a 20 49 66 20 74 68 65 20 53 45 54 5f 46  ./* If the SET_F
56cf0 55 4c 4c 53 59 4e 43 20 6d 61 63 72 6f 20 69 73  ULLSYNC macro is
56d00 20 6e 6f 74 20 64 65 66 69 6e 65 64 20 61 62 6f   not defined abo
56d10 76 65 2c 20 74 68 65 6e 20 6d 61 6b 65 20 69 74  ve, then make it
56d20 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 0a 2a 2f 0a 23  .** a no-op.*/.#
56d30 69 66 6e 64 65 66 20 53 45 54 5f 46 55 4c 4c 53  ifndef SET_FULLS
56d40 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 53 45 54  YNC.# define SET
56d50 5f 46 55 4c 4c 53 59 4e 43 28 78 2c 79 29 0a 23  _FULLSYNC(x,y).#
56d60 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
56d70 20 64 65 66 61 75 6c 74 20 73 69 7a 65 20 6f 66   default size of
56d80 20 61 20 64 69 73 6b 20 73 65 63 74 6f 72 0a 2a   a disk sector.*
56d90 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
56da0 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f  _DEFAULT_SECTOR_
56db0 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51  SIZE.# define SQ
56dc0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43  LITE_DEFAULT_SEC
56dd0 54 4f 52 5f 53 49 5a 45 20 35 31 32 0a 23 65 6e  TOR_SIZE 512.#en
56de0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 6d 70 6f  dif../*.** Tempo
56df0 72 61 72 79 20 66 69 6c 65 73 20 61 72 65 20 6e  rary files are n
56e00 61 6d 65 64 20 73 74 61 72 74 69 6e 67 20 77 69  amed starting wi
56e10 74 68 20 74 68 69 73 20 70 72 65 66 69 78 20 66  th this prefix f
56e20 6f 6c 6c 6f 77 65 64 20 62 79 20 31 36 20 72 61  ollowed by 16 ra
56e30 6e 64 6f 6d 0a 2a 2a 20 61 6c 70 68 61 6e 75 6d  ndom.** alphanum
56e40 65 72 69 63 20 63 68 61 72 61 63 74 65 72 73 2c  eric characters,
56e50 20 61 6e 64 20 6e 6f 20 66 69 6c 65 20 65 78 74   and no file ext
56e60 65 6e 73 69 6f 6e 2e 20 54 68 65 79 20 61 72 65  ension. They are
56e70 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 0a 2a   stored in the.*
56e80 2a 20 4f 53 27 73 20 73 74 61 6e 64 61 72 64 20  * OS's standard 
56e90 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 64  temporary file d
56ea0 69 72 65 63 74 6f 72 79 2c 20 61 6e 64 20 61 72  irectory, and ar
56eb0 65 20 64 65 6c 65 74 65 64 20 70 72 69 6f 72 20  e deleted prior 
56ec0 74 6f 20 65 78 69 74 2e 0a 2a 2a 20 49 66 20 73  to exit..** If s
56ed0 71 6c 69 74 65 20 69 73 20 62 65 69 6e 67 20 65  qlite is being e
56ee0 6d 62 65 64 64 65 64 20 69 6e 20 61 6e 6f 74 68  mbedded in anoth
56ef0 65 72 20 70 72 6f 67 72 61 6d 2c 20 79 6f 75 20  er program, you 
56f00 6d 61 79 20 77 69 73 68 20 74 6f 20 63 68 61 6e  may wish to chan
56f10 67 65 20 74 68 65 0a 2a 2a 20 70 72 65 66 69 78  ge the.** prefix
56f20 20 74 6f 20 72 65 66 6c 65 63 74 20 79 6f 75 72   to reflect your
56f30 20 70 72 6f 67 72 61 6d 27 73 20 6e 61 6d 65 2c   program's name,
56f40 20 73 6f 20 74 68 61 74 20 69 66 20 79 6f 75 72   so that if your
56f50 20 70 72 6f 67 72 61 6d 20 65 78 69 74 73 0a 2a   program exits.*
56f60 2a 20 70 72 65 6d 61 74 75 72 65 6c 79 2c 20 6f  * prematurely, o
56f70 6c 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c  ld temporary fil
56f80 65 73 20 63 61 6e 20 62 65 20 65 61 73 69 6c 79  es can be easily
56f90 20 69 64 65 6e 74 69 66 69 65 64 2e 20 54 68 69   identified. Thi
56fa0 73 20 63 61 6e 20 62 65 20 64 6f 6e 65 0a 2a 2a  s can be done.**
56fb0 20 75 73 69 6e 67 20 2d 44 53 51 4c 49 54 45 5f   using -DSQLITE_
56fc0 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58  TEMP_FILE_PREFIX
56fd0 3d 6d 79 70 72 65 66 69 78 5f 20 6f 6e 20 74 68  =myprefix_ on th
56fe0 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61  e compiler comma
56ff0 6e 64 20 6c 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 32  nd line..**.** 2
57000 30 30 36 2d 31 30 2d 33 31 3a 20 20 54 68 65 20  006-10-31:  The 
57010 64 65 66 61 75 6c 74 20 70 72 65 66 69 78 20 75  default prefix u
57020 73 65 64 20 74 6f 20 62 65 20 22 73 71 6c 69 74  sed to be "sqlit
57030 65 5f 22 2e 20 20 42 75 74 20 74 68 65 6e 0a 2a  e_".  But then.*
57040 2a 20 4d 63 61 66 65 65 20 73 74 61 72 74 65 64  * Mcafee started
57050 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e   using SQLite in
57060 20 74 68 65 69 72 20 61 6e 74 69 2d 76 69 72 75   their anti-viru
57070 73 20 70 72 6f 64 75 63 74 20 61 6e 64 20 69 74  s product and it
57080 0a 2a 2a 20 73 74 61 72 74 65 64 20 70 75 74 74  .** started putt
57090 69 6e 67 20 66 69 6c 65 73 20 77 69 74 68 20 74  ing files with t
570a0 68 65 20 22 73 71 6c 69 74 65 22 20 6e 61 6d 65  he "sqlite" name
570b0 20 69 6e 20 74 68 65 20 63 3a 2f 74 65 6d 70 20   in the c:/temp 
570c0 66 6f 6c 64 65 72 2e 0a 2a 2a 20 54 68 69 73 20  folder..** This 
570d0 61 6e 6e 6f 79 65 64 20 6d 61 6e 79 20 77 69 6e  annoyed many win
570e0 64 6f 77 73 20 75 73 65 72 73 2e 20 20 54 68 6f  dows users.  Tho
570f0 73 65 20 75 73 65 72 73 20 77 6f 75 6c 64 20 74  se users would t
57100 68 65 6e 20 64 6f 20 61 20 0a 2a 2a 20 47 6f 6f  hen do a .** Goo
57110 67 6c 65 20 73 65 61 72 63 68 20 66 6f 72 20 22  gle search for "
57120 73 71 6c 69 74 65 22 2c 20 66 69 6e 64 20 74 68  sqlite", find th
57130 65 20 74 65 6c 65 70 68 6f 6e 65 20 6e 75 6d 62  e telephone numb
57140 65 72 73 20 6f 66 20 74 68 65 0a 2a 2a 20 64 65  ers of the.** de
57150 76 65 6c 6f 70 65 72 73 20 61 6e 64 20 63 61 6c  velopers and cal
57160 6c 20 74 6f 20 77 61 6b 65 20 74 68 65 6d 20 75  l to wake them u
57170 70 20 61 74 20 6e 69 67 68 74 20 61 6e 64 20 63  p at night and c
57180 6f 6d 70 6c 61 69 6e 2e 0a 2a 2a 20 46 6f 72 20  omplain..** For 
57190 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 74 68 65  this reason, the
571a0 20 64 65 66 61 75 6c 74 20 6e 61 6d 65 20 70 72   default name pr
571b0 65 66 69 78 20 69 73 20 63 68 61 6e 67 65 64 20  efix is changed 
571c0 74 6f 20 62 65 20 22 73 71 6c 69 74 65 22 20 0a  to be "sqlite" .
571d0 2a 2a 20 73 70 65 6c 6c 65 64 20 62 61 63 6b 77  ** spelled backw
571e0 61 72 64 73 2e 20 20 53 6f 20 74 68 65 20 74 65  ards.  So the te
571f0 6d 70 20 66 69 6c 65 73 20 61 72 65 20 73 74 69  mp files are sti
57200 6c 6c 20 69 64 65 6e 74 69 66 69 65 64 2c 20 62  ll identified, b
57210 75 74 0a 2a 2a 20 61 6e 79 62 6f 64 79 20 73 6d  ut.** anybody sm
57220 61 72 74 20 65 6e 6f 75 67 68 20 74 6f 20 66 69  art enough to fi
57230 67 75 72 65 20 6f 75 74 20 74 68 65 20 63 6f 64  gure out the cod
57240 65 20 69 73 20 61 6c 73 6f 20 6c 69 6b 65 6c 79  e is also likely
57250 20 73 6d 61 72 74 0a 2a 2a 20 65 6e 6f 75 67 68   smart.** enough
57260 20 74 6f 20 6b 6e 6f 77 20 74 68 61 74 20 63 61   to know that ca
57270 6c 6c 69 6e 67 20 74 68 65 20 64 65 76 65 6c 6f  lling the develo
57280 70 65 72 20 77 69 6c 6c 20 6e 6f 74 20 68 65 6c  per will not hel
57290 70 20 67 65 74 20 72 69 64 0a 2a 2a 20 6f 66 20  p get rid.** of 
572a0 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 23 69 66  the file..*/.#if
572b0 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 4d 50  ndef SQLITE_TEMP
572c0 5f 46 49 4c 45 5f 50 52 45 46 49 58 0a 23 20 64  _FILE_PREFIX.# d
572d0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d  efine SQLITE_TEM
572e0 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 20 22 65  P_FILE_PREFIX "e
572f0 74 69 6c 71 73 5f 22 0a 23 65 6e 64 69 66 0a 0a  tilqs_".#endif..
57300 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
57310 69 6e 67 20 76 61 6c 75 65 73 20 6d 61 79 20 62  ing values may b
57320 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
57330 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
57340 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 4c  to.** sqlite3OsL
57350 6f 63 6b 28 29 2e 20 54 68 65 20 76 61 72 69 6f  ock(). The vario
57360 75 73 20 6c 6f 63 6b 73 20 65 78 68 69 62 69 74  us locks exhibit
57370 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
57380 65 6d 61 6e 74 69 63 73 3a 0a 2a 2a 0a 2a 2a 20  emantics:.**.** 
57390 53 48 41 52 45 44 3a 20 20 20 20 41 6e 79 20 6e  SHARED:    Any n
573a0 75 6d 62 65 72 20 6f 66 20 70 72 6f 63 65 73 73  umber of process
573b0 65 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 53 48  es may hold a SH
573c0 41 52 45 44 20 6c 6f 63 6b 20 73 69 6d 75 6c 74  ARED lock simult
573d0 61 6e 65 6f 75 73 6c 79 2e 0a 2a 2a 20 52 45 53  aneously..** RES
573e0 45 52 56 45 44 3a 20 20 41 20 73 69 6e 67 6c 65  ERVED:  A single
573f0 20 70 72 6f 63 65 73 73 20 6d 61 79 20 68 6f 6c   process may hol
57400 64 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  d a RESERVED loc
57410 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74 0a 2a  k on a file at.*
57420 2a 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 79  *            any
57430 20 74 69 6d 65 2e 20 4f 74 68 65 72 20 70 72 6f   time. Other pro
57440 63 65 73 73 65 73 20 6d 61 79 20 68 6f 6c 64 20  cesses may hold 
57450 61 6e 64 20 6f 62 74 61 69 6e 20 6e 65 77 20 53  and obtain new S
57460 48 41 52 45 44 20 6c 6f 63 6b 73 2e 0a 2a 2a 20  HARED locks..** 
57470 50 45 4e 44 49 4e 47 3a 20 20 20 41 20 73 69 6e  PENDING:   A sin
57480 67 6c 65 20 70 72 6f 63 65 73 73 20 6d 61 79 20  gle process may 
57490 68 6f 6c 64 20 61 20 50 45 4e 44 49 4e 47 20 6c  hold a PENDING l
574a0 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65 20 61 74  ock on a file at
574b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 61  .**            a
574c0 6e 79 20 6f 6e 65 20 74 69 6d 65 2e 20 45 78 69  ny one time. Exi
574d0 73 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63  sting SHARED loc
574e0 6b 73 20 6d 61 79 20 70 65 72 73 69 73 74 2c 20  ks may persist, 
574f0 62 75 74 20 6e 6f 20 6e 65 77 0a 2a 2a 20 20 20  but no new.**   
57500 20 20 20 20 20 20 20 20 20 53 48 41 52 45 44 20           SHARED 
57510 6c 6f 63 6b 73 20 6d 61 79 20 62 65 20 6f 62 74  locks may be obt
57520 61 69 6e 65 64 20 62 79 20 6f 74 68 65 72 20 70  ained by other p
57530 72 6f 63 65 73 73 65 73 2e 0a 2a 2a 20 45 58 43  rocesses..** EXC
57540 4c 55 53 49 56 45 3a 20 41 6e 20 45 58 43 4c 55  LUSIVE: An EXCLU
57550 53 49 56 45 20 6c 6f 63 6b 20 70 72 65 63 6c 75  SIVE lock preclu
57560 64 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 6c 6f  des all other lo
57570 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 50 45 4e 44 49  cks..**.** PENDI
57580 4e 47 5f 4c 4f 43 4b 20 6d 61 79 20 6e 6f 74 20  NG_LOCK may not 
57590 62 65 20 70 61 73 73 65 64 20 64 69 72 65 63 74  be passed direct
575a0 6c 79 20 74 6f 20 73 71 6c 69 74 65 33 4f 73 4c  ly to sqlite3OsL
575b0 6f 63 6b 28 29 2e 20 49 6e 73 74 65 61 64 2c 20  ock(). Instead, 
575c0 61 0a 2a 2a 20 70 72 6f 63 65 73 73 20 74 68 61  a.** process tha
575d0 74 20 72 65 71 75 65 73 74 73 20 61 6e 20 45 58  t requests an EX
575e0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6d 61 79  CLUSIVE lock may
575f0 20 61 63 74 75 61 6c 6c 79 20 6f 62 74 61 69 6e   actually obtain
57600 20 61 20 50 45 4e 44 49 4e 47 0a 2a 2a 20 6c 6f   a PENDING.** lo
57610 63 6b 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20  ck. This can be 
57620 75 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45  upgraded to an E
57630 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 62 79  XCLUSIVE lock by
57640 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 63 61   a subsequent ca
57650 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33  ll to.** sqlite3
57660 4f 73 4c 6f 63 6b 28 29 2e 0a 2a 2f 0a 23 64 65  OsLock()..*/.#de
57670 66 69 6e 65 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20  fine NO_LOCK    
57680 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53       0.#define S
57690 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20 20 20 31  HARED_LOCK     1
576a0 0a 23 64 65 66 69 6e 65 20 52 45 53 45 52 56 45  .#define RESERVE
576b0 44 5f 4c 4f 43 4b 20 20 20 32 0a 23 64 65 66 69  D_LOCK   2.#defi
576c0 6e 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20  ne PENDING_LOCK 
576d0 20 20 20 33 0a 23 64 65 66 69 6e 65 20 45 58 43     3.#define EXC
576e0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 20 34 0a 0a  LUSIVE_LOCK  4..
576f0 2f 2a 0a 2a 2a 20 46 69 6c 65 20 4c 6f 63 6b 69  /*.** File Locki
57700 6e 67 20 4e 6f 74 65 73 3a 20 20 28 4d 6f 73 74  ng Notes:  (Most
57710 6c 79 20 61 62 6f 75 74 20 77 69 6e 64 6f 77 73  ly about windows
57720 20 62 75 74 20 61 6c 73 6f 20 73 6f 6d 65 20 69   but also some i
57730 6e 66 6f 20 66 6f 72 20 55 6e 69 78 29 0a 2a 2a  nfo for Unix).**
57740 0a 2a 2a 20 57 65 20 63 61 6e 6e 6f 74 20 75 73  .** We cannot us
57750 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 6f  e LockFileEx() o
57760 72 20 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29  r UnlockFileEx()
57770 20 6f 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45 20   on Win95/98/ME 
57780 62 65 63 61 75 73 65 0a 2a 2a 20 74 68 6f 73 65  because.** those
57790 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e   functions are n
577a0 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e 20 20 53  ot available.  S
577b0 6f 20 77 65 20 75 73 65 20 6f 6e 6c 79 20 4c 6f  o we use only Lo
577c0 63 6b 46 69 6c 65 28 29 20 61 6e 64 0a 2a 2a 20  ckFile() and.** 
577d0 55 6e 6c 6f 63 6b 46 69 6c 65 28 29 2e 0a 2a 2a  UnlockFile()..**
577e0 0a 2a 2a 20 4c 6f 63 6b 46 69 6c 65 28 29 20 70  .** LockFile() p
577f0 72 65 76 65 6e 74 73 20 6e 6f 74 20 6a 75 73 74  revents not just
57800 20 77 72 69 74 69 6e 67 20 62 75 74 20 61 6c 73   writing but als
57810 6f 20 72 65 61 64 69 6e 67 20 62 79 20 6f 74 68  o reading by oth
57820 65 72 20 70 72 6f 63 65 73 73 65 73 2e 0a 2a 2a  er processes..**
57830 20 41 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 69   A SHARED_LOCK i
57840 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f  s obtained by lo
57850 63 6b 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 72  cking a single r
57860 61 6e 64 6f 6d 6c 79 2d 63 68 6f 73 65 6e 20 0a  andomly-chosen .
57870 2a 2a 20 62 79 74 65 20 6f 75 74 20 6f 66 20 61  ** byte out of a
57880 20 73 70 65 63 69 66 69 63 20 72 61 6e 67 65 20   specific range 
57890 6f 66 20 62 79 74 65 73 2e 20 54 68 65 20 6c 6f  of bytes. The lo
578a0 63 6b 20 62 79 74 65 20 69 73 20 6f 62 74 61 69  ck byte is obtai
578b0 6e 65 64 20 61 74 20 0a 2a 2a 20 72 61 6e 64 6f  ned at .** rando
578c0 6d 20 73 6f 20 74 77 6f 20 73 65 70 61 72 61 74  m so two separat
578d0 65 20 72 65 61 64 65 72 73 20 63 61 6e 20 70 72  e readers can pr
578e0 6f 62 61 62 6c 79 20 61 63 63 65 73 73 20 74 68  obably access th
578f0 65 20 66 69 6c 65 20 61 74 20 74 68 65 20 0a 2a  e file at the .*
57900 2a 20 73 61 6d 65 20 74 69 6d 65 2c 20 75 6e 6c  * same time, unl
57910 65 73 73 20 74 68 65 79 20 61 72 65 20 75 6e 6c  ess they are unl
57920 75 63 6b 79 20 61 6e 64 20 63 68 6f 6f 73 65 20  ucky and choose 
57930 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b 20 62 79  the same lock by
57940 74 65 2e 0a 2a 2a 20 41 6e 20 45 58 43 4c 55 53  te..** An EXCLUS
57950 49 56 45 5f 4c 4f 43 4b 20 69 73 20 6f 62 74 61  IVE_LOCK is obta
57960 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67 20  ined by locking 
57970 61 6c 6c 20 62 79 74 65 73 20 69 6e 20 74 68 65  all bytes in the
57980 20 72 61 6e 67 65 2e 0a 2a 2a 20 54 68 65 72 65   range..** There
57990 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 6f 6e 65   can only be one
579a0 20 77 72 69 74 65 72 2e 20 20 41 20 52 45 53 45   writer.  A RESE
579b0 52 56 45 44 5f 4c 4f 43 4b 20 69 73 20 6f 62 74  RVED_LOCK is obt
579c0 61 69 6e 65 64 20 62 79 20 6c 6f 63 6b 69 6e 67  ained by locking
579d0 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 62 79 74  .** a single byt
579e0 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 68  e of the file th
579f0 61 74 20 69 73 20 64 65 73 69 67 6e 61 74 65 64  at is designated
57a00 20 61 73 20 74 68 65 20 72 65 73 65 72 76 65 64   as the reserved
57a10 20 6c 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 41   lock byte..** A
57a20 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 69 73   PENDING_LOCK is
57a30 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f 63   obtained by loc
57a40 6b 69 6e 67 20 61 20 64 65 73 69 67 6e 61 74 65  king a designate
57a50 64 20 62 79 74 65 20 64 69 66 66 65 72 65 6e 74  d byte different
57a60 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 52 45 53   from.** the RES
57a70 45 52 56 45 44 5f 4c 4f 43 4b 20 62 79 74 65 2e  ERVED_LOCK byte.
57a80 0a 2a 2a 0a 2a 2a 20 4f 6e 20 57 69 6e 4e 54 2f  .**.** On WinNT/
57a90 32 4b 2f 58 50 20 73 79 73 74 65 6d 73 2c 20 4c  2K/XP systems, L
57aa0 6f 63 6b 46 69 6c 65 45 78 28 29 20 61 6e 64 20  ockFileEx() and 
57ab0 55 6e 6c 6f 63 6b 46 69 6c 65 45 78 28 29 20 61  UnlockFileEx() a
57ac0 72 65 20 61 76 61 69 6c 61 62 6c 65 2c 0a 2a 2a  re available,.**
57ad0 20 77 68 69 63 68 20 6d 65 61 6e 73 20 77 65 20   which means we 
57ae0 63 61 6e 20 75 73 65 20 72 65 61 64 65 72 2f 77  can use reader/w
57af0 72 69 74 65 72 20 6c 6f 63 6b 73 2e 20 20 57 68  riter locks.  Wh
57b00 65 6e 20 72 65 61 64 65 72 2f 77 72 69 74 65 72  en reader/writer
57b10 20 6c 6f 63 6b 73 0a 2a 2a 20 61 72 65 20 75 73   locks.** are us
57b20 65 64 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20  ed, the lock is 
57b30 70 6c 61 63 65 64 20 6f 6e 20 74 68 65 20 73 61  placed on the sa
57b40 6d 65 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65  me range of byte
57b50 73 20 74 68 61 74 20 69 73 20 75 73 65 64 0a 2a  s that is used.*
57b60 2a 20 66 6f 72 20 70 72 6f 62 61 62 69 6c 69 73  * for probabilis
57b70 74 69 63 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 57  tic locking in W
57b80 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 48 65 6e  in95/98/ME.  Hen
57b90 63 65 2c 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  ce, the locking 
57ba0 73 63 68 65 6d 65 0a 2a 2a 20 77 69 6c 6c 20 73  scheme.** will s
57bb0 75 70 70 6f 72 74 20 74 77 6f 20 6f 72 20 6d 6f  upport two or mo
57bc0 72 65 20 57 69 6e 39 35 20 72 65 61 64 65 72 73  re Win95 readers
57bd0 20 6f 72 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20   or two or more 
57be0 57 69 6e 4e 54 20 72 65 61 64 65 72 73 2e 0a 2a  WinNT readers..*
57bf0 2a 20 42 75 74 20 61 20 73 69 6e 67 6c 65 20 57  * But a single W
57c00 69 6e 39 35 20 72 65 61 64 65 72 20 77 69 6c 6c  in95 reader will
57c10 20 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c 20 57 69   lock out all Wi
57c20 6e 4e 54 20 72 65 61 64 65 72 73 20 61 6e 64 20  nNT readers and 
57c30 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 57 69 6e 4e  a single.** WinN
57c40 54 20 72 65 61 64 65 72 20 77 69 6c 6c 20 6c 6f  T reader will lo
57c50 63 6b 20 6f 75 74 20 61 6c 6c 20 6f 74 68 65 72  ck out all other
57c60 20 57 69 6e 39 35 20 72 65 61 64 65 72 73 2e 0a   Win95 readers..
57c70 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  **.** The follow
57c80 69 6e 67 20 23 64 65 66 69 6e 65 73 20 73 70 65  ing #defines spe
57c90 63 69 66 79 20 74 68 65 20 72 61 6e 67 65 20 6f  cify the range o
57ca0 66 20 62 79 74 65 73 20 75 73 65 64 20 66 6f 72  f bytes used for
57cb0 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 53 48 41   locking..** SHA
57cc0 52 45 44 5f 53 49 5a 45 20 69 73 20 74 68 65 20  RED_SIZE is the 
57cd0 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
57ce0 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65  available in the
57cf0 20 70 6f 6f 6c 20 66 72 6f 6d 20 77 68 69 63 68   pool from which
57d00 0a 2a 2a 20 61 20 72 61 6e 64 6f 6d 20 62 79 74  .** a random byt
57d10 65 20 69 73 20 73 65 6c 65 63 74 65 64 20 66 6f  e is selected fo
57d20 72 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 2e  r a shared lock.
57d30 20 20 54 68 65 20 70 6f 6f 6c 20 6f 66 20 62 79    The pool of by
57d40 74 65 73 20 66 6f 72 0a 2a 2a 20 73 68 61 72 65  tes for.** share
57d50 64 20 6c 6f 63 6b 73 20 62 65 67 69 6e 73 20 61  d locks begins a
57d60 74 20 53 48 41 52 45 44 5f 46 49 52 53 54 2e 20  t SHARED_FIRST. 
57d70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61 6d 65 20  .**.** The same 
57d80 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79  locking strategy
57d90 20 61 6e 64 0a 2a 2a 20 62 79 74 65 20 72 61 6e   and.** byte ran
57da0 67 65 73 20 61 72 65 20 75 73 65 64 20 66 6f 72  ges are used for
57db0 20 55 6e 69 78 2e 20 20 54 68 69 73 20 6c 65 61   Unix.  This lea
57dc0 76 65 73 20 6f 70 65 6e 20 74 68 65 20 70 6f 73  ves open the pos
57dd0 73 69 62 6c 69 74 79 20 6f 66 20 68 61 76 69 6e  siblity of havin
57de0 67 0a 2a 2a 20 63 6c 69 65 6e 74 73 20 6f 6e 20  g.** clients on 
57df0 77 69 6e 39 35 2c 20 77 69 6e 4e 54 2c 20 61 6e  win95, winNT, an
57e00 64 20 75 6e 69 78 20 61 6c 6c 20 74 61 6c 6b 69  d unix all talki
57e10 6e 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20 73  ng to the same s
57e20 68 61 72 65 64 20 66 69 6c 65 0a 2a 2a 20 61 6e  hared file.** an
57e30 64 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f  d all locking co
57e40 72 72 65 63 74 6c 79 2e 20 20 54 6f 20 64 6f 20  rrectly.  To do 
57e50 73 6f 20 77 6f 75 6c 64 20 72 65 71 75 69 72 65  so would require
57e60 20 74 68 61 74 20 73 61 6d 62 61 20 28 6f 72 20   that samba (or 
57e70 77 68 61 74 65 76 65 72 0a 2a 2a 20 74 6f 6f 6c  whatever.** tool
57e80 20 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 66   is being used f
57e90 6f 72 20 66 69 6c 65 20 73 68 61 72 69 6e 67 29  or file sharing)
57ea0 20 69 6d 70 6c 65 6d 65 6e 74 73 20 6c 6f 63 6b   implements lock
57eb0 73 20 63 6f 72 72 65 63 74 6c 79 20 62 65 74 77  s correctly betw
57ec0 65 65 6e 0a 2a 2a 20 77 69 6e 64 6f 77 73 20 61  een.** windows a
57ed0 6e 64 20 75 6e 69 78 2e 20 20 49 27 6d 20 67 75  nd unix.  I'm gu
57ee0 65 73 73 69 6e 67 20 74 68 61 74 20 69 73 6e 27  essing that isn'
57ef0 74 20 6c 69 6b 65 6c 79 20 74 6f 20 68 61 70 70  t likely to happ
57f00 65 6e 2c 20 62 75 74 20 62 79 0a 2a 2a 20 75 73  en, but by.** us
57f10 69 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f 63  ing the same loc
57f20 6b 69 6e 67 20 72 61 6e 67 65 20 77 65 20 61 72  king range we ar
57f30 65 20 61 74 20 6c 65 61 73 74 20 6f 70 65 6e 20  e at least open 
57f40 74 6f 20 74 68 65 20 70 6f 73 73 69 62 69 6c 69  to the possibili
57f50 74 79 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 69 6e  ty..**.** Lockin
57f60 67 20 69 6e 20 77 69 6e 64 6f 77 73 20 69 73 20  g in windows is 
57f70 6d 61 6e 64 69 74 6f 72 79 2e 20 20 46 6f 72 20  manditory.  For 
57f80 74 68 69 73 20 72 65 61 73 6f 6e 2c 20 77 65 20  this reason, we 
57f90 63 61 6e 6e 6f 74 20 73 74 6f 72 65 0a 2a 2a 20  cannot store.** 
57fa0 61 63 74 75 61 6c 20 64 61 74 61 20 69 6e 20 74  actual data in t
57fb0 68 65 20 62 79 74 65 73 20 75 73 65 64 20 66 6f  he bytes used fo
57fc0 72 20 6c 6f 63 6b 69 6e 67 2e 20 20 54 68 65 20  r locking.  The 
57fd0 70 61 67 65 72 20 6e 65 76 65 72 20 61 6c 6c 6f  pager never allo
57fe0 63 61 74 65 73 0a 2a 2a 20 74 68 65 20 70 61 67  cates.** the pag
57ff0 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 6c  es involved in l
58000 6f 63 6b 69 6e 67 20 74 68 65 72 65 66 6f 72 65  ocking therefore
58010 2e 20 20 53 48 41 52 45 44 5f 53 49 5a 45 20 69  .  SHARED_SIZE i
58020 73 20 73 65 6c 65 63 74 65 64 20 73 6f 0a 2a 2a  s selected so.**
58030 20 74 68 61 74 20 61 6c 6c 20 6c 6f 63 6b 73 20   that all locks 
58040 77 69 6c 6c 20 66 69 74 20 6f 6e 20 61 20 73 69  will fit on a si
58050 6e 67 6c 65 20 70 61 67 65 20 65 76 65 6e 20 61  ngle page even a
58060 74 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 70 61  t the minimum pa
58070 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 50 45 4e 44  ge size..** PEND
58080 49 4e 47 5f 42 59 54 45 20 64 65 66 69 6e 65 73  ING_BYTE defines
58090 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
580a0 66 20 74 68 65 20 6c 6f 63 6b 73 2e 20 20 42 79  f the locks.  By
580b0 20 64 65 66 61 75 6c 74 20 50 45 4e 44 49 4e 47   default PENDING
580c0 5f 42 59 54 45 0a 2a 2a 20 69 73 20 73 65 74 20  _BYTE.** is set 
580d0 68 69 67 68 20 73 6f 20 74 68 61 74 20 77 65 20  high so that we 
580e0 64 6f 6e 27 74 20 68 61 76 65 20 74 6f 20 61 6c  don't have to al
580f0 6c 6f 63 61 74 65 20 61 6e 20 75 6e 75 73 65 64  locate an unused
58100 20 70 61 67 65 20 65 78 63 65 70 74 0a 2a 2a 20   page except.** 
58110 66 6f 72 20 76 65 72 79 20 6c 61 72 67 65 20 64  for very large d
58120 61 74 61 62 61 73 65 73 2e 20 20 42 75 74 20 6f  atabases.  But o
58130 6e 65 20 73 68 6f 75 6c 64 20 74 65 73 74 20 74  ne should test t
58140 68 65 20 70 61 67 65 20 73 6b 69 70 70 69 6e 67  he page skipping
58150 20 6c 6f 67 69 63 20 0a 2a 2a 20 62 79 20 73 65   logic .** by se
58160 74 74 69 6e 67 20 50 45 4e 44 49 4e 47 5f 42 59  tting PENDING_BY
58170 54 45 20 6c 6f 77 20 61 6e 64 20 72 75 6e 6e 69  TE low and runni
58180 6e 67 20 74 68 65 20 65 6e 74 69 72 65 20 72 65  ng the entire re
58190 67 72 65 73 73 69 6f 6e 20 73 75 69 74 65 2e 0a  gression suite..
581a0 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74  **.** Changing t
581b0 68 65 20 76 61 6c 75 65 20 6f 66 20 50 45 4e 44  he value of PEND
581c0 49 4e 47 5f 42 59 54 45 20 72 65 73 75 6c 74 73  ING_BYTE results
581d0 20 69 6e 20 61 20 73 75 62 74 6c 79 20 69 6e 63   in a subtly inc
581e0 6f 6d 70 61 74 69 62 6c 65 0a 2a 2a 20 66 69 6c  ompatible.** fil
581f0 65 20 66 6f 72 6d 61 74 2e 20 20 44 65 70 65 6e  e format.  Depen
58200 64 69 6e 67 20 6f 6e 20 68 6f 77 20 69 74 20 69  ding on how it i
58210 73 20 63 68 61 6e 67 65 64 2c 20 79 6f 75 20 6d  s changed, you m
58220 69 67 68 74 20 6e 6f 74 20 6e 6f 74 69 63 65 0a  ight not notice.
58230 2a 2a 20 74 68 65 20 69 6e 63 6f 6d 70 61 74 69  ** the incompati
58240 62 69 6c 69 74 79 20 72 69 67 68 74 20 61 77 61  bility right awa
58250 79 2c 20 65 76 65 6e 20 72 75 6e 6e 69 6e 67 20  y, even running 
58260 61 20 66 75 6c 6c 20 72 65 67 72 65 73 73 69 6f  a full regressio
58270 6e 20 74 65 73 74 2e 0a 2a 2a 20 54 68 65 20 64  n test..** The d
58280 65 66 61 75 6c 74 20 6c 6f 63 61 74 69 6f 6e 20  efault location 
58290 6f 66 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20  of PENDING_BYTE 
582a0 69 73 20 74 68 65 20 66 69 72 73 74 20 62 79 74  is the first byt
582b0 65 20 70 61 73 74 20 74 68 65 0a 2a 2a 20 31 47  e past the.** 1G
582c0 42 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2a 0a 2a  B boundary..**.*
582d0 2f 0a 23 64 65 66 69 6e 65 20 50 45 4e 44 49 4e  /.#define PENDIN
582e0 47 5f 42 59 54 45 20 20 20 20 20 20 73 71 6c 69  G_BYTE      sqli
582f0 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 0a 23  te3PendingByte.#
58300 64 65 66 69 6e 65 20 52 45 53 45 52 56 45 44 5f  define RESERVED_
58310 42 59 54 45 20 20 20 20 20 28 50 45 4e 44 49 4e  BYTE     (PENDIN
58320 47 5f 42 59 54 45 2b 31 29 0a 23 64 65 66 69 6e  G_BYTE+1).#defin
58330 65 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 20  e SHARED_FIRST  
58340 20 20 20 20 28 50 45 4e 44 49 4e 47 5f 42 59 54      (PENDING_BYT
58350 45 2b 32 29 0a 23 64 65 66 69 6e 65 20 53 48 41  E+2).#define SHA
58360 52 45 44 5f 53 49 5a 45 20 20 20 20 20 20 20 35  RED_SIZE       5
58370 31 30 0a 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70 65  10../*.** Wrappe
58380 72 20 61 72 6f 75 6e 64 20 4f 53 20 73 70 65 63  r around OS spec
58390 69 66 69 63 20 73 71 6c 69 74 65 33 5f 6f 73 5f  ific sqlite3_os_
583a0 69 6e 69 74 28 29 20 66 75 6e 63 74 69 6f 6e 2e  init() function.
583b0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
583c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
583d0 49 6e 69 74 28 76 6f 69 64 29 3b 0a 0a 2f 2a 20  Init(void);../* 
583e0 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f  .** Functions fo
583f0 72 20 61 63 63 65 73 73 69 6e 67 20 73 71 6c 69  r accessing sqli
58400 74 65 33 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73  te3_file methods
58410 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
58420 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
58430 73 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66  sClose(sqlite3_f
58440 69 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ile*);.SQLITE_PR
58450 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
58460 33 4f 73 52 65 61 64 28 73 71 6c 69 74 65 33 5f  3OsRead(sqlite3_
58470 66 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e  file*, void*, in
58480 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65  t amt, i64 offse
58490 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
584a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
584b0 57 72 69 74 65 28 73 71 6c 69 74 65 33 5f 66 69  Write(sqlite3_fi
584c0 6c 65 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a  le*, const void*
584d0 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f  , int amt, i64 o
584e0 66 66 73 65 74 29 3b 0a 53 51 4c 49 54 45 5f 50  ffset);.SQLITE_P
584f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
58500 65 33 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c  e3OsTruncate(sql
58510 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36 34 20  ite3_file*, i64 
58520 73 69 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  size);.SQLITE_PR
58530 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
58540 33 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33 5f  3OsSync(sqlite3_
58550 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  file*, int);.SQL
58560 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
58570 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a  sqlite3OsFileSiz
58580 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c  e(sqlite3_file*,
58590 20 69 36 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51   i64 *pSize);.SQ
585a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
585b0 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73   sqlite3OsLock(s
585c0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e  qlite3_file*, in
585d0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
585e0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
585f0 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  Unlock(sqlite3_f
58600 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ile*, int);.SQLI
58610 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
58620 71 6c 69 74 65 33 4f 73 43 68 65 63 6b 52 65 73  qlite3OsCheckRes
58630 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65  ervedLock(sqlite
58640 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
58650 2a 70 52 65 73 4f 75 74 29 3b 0a 53 51 4c 49 54  *pResOut);.SQLIT
58660 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
58670 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72  lite3OsFileContr
58680 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  ol(sqlite3_file*
58690 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 64 65  ,int,void*);.#de
586a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 43 4e 54  fine SQLITE_FCNT
586b0 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44 20 30  L_DB_UNCHANGED 0
586c0 78 63 61 30 39 33 66 61 30 0a 53 51 4c 49 54 45  xca093fa0.SQLITE
586d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
586e0 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65  ite3OsSectorSize
586f0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
58700 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  d);.SQLITE_PRIVA
58710 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
58720 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
58730 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69  stics(sqlite3_fi
58740 6c 65 20 2a 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a  le *id);../* .**
58750 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61   Functions for a
58760 63 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33  ccessing sqlite3
58770 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 0a 2a 2f  _vfs methods .*/
58780 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
58790 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  int sqlite3OsOpe
587a0 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  n(sqlite3_vfs *,
587b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73   const char *, s
587c0 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e  qlite3_file*, in
587d0 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54  t, int *);.SQLIT
587e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
587f0 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71  lite3OsDelete(sq
58800 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e  lite3_vfs *, con
58810 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b  st char *, int);
58820 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
58830 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63  int sqlite3OsAcc
58840 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ess(sqlite3_vfs 
58850 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  *, const char *,
58860 20 69 6e 74 2c 20 69 6e 74 20 2a 70 52 65 73 4f   int, int *pResO
58870 75 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ut);.SQLITE_PRIV
58880 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
58890 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 73 71  sFullPathname(sq
588a0 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e  lite3_vfs *, con
588b0 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20  st char *, int, 
588c0 63 68 61 72 20 2a 29 3b 0a 23 69 66 6e 64 65 66  char *);.#ifndef
588d0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41   SQLITE_OMIT_LOA
588e0 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49  D_EXTENSION.SQLI
588f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
58900 2a 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e  *sqlite3OsDlOpen
58910 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20  (sqlite3_vfs *, 
58920 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53  const char *);.S
58930 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
58940 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72  id sqlite3OsDlEr
58950 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ror(sqlite3_vfs 
58960 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b  *, int, char *);
58970 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
58980 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33 4f 73  void (*sqlite3Os
58990 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66  DlSym(sqlite3_vf
589a0 73 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 63 6f 6e  s *, void *, con
589b0 73 74 20 63 68 61 72 20 2a 29 29 28 76 6f 69 64  st char *))(void
589c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
589d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 73  E void sqlite3Os
589e0 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  DlClose(sqlite3_
589f0 76 66 73 20 2a 2c 20 76 6f 69 64 20 2a 29 3b 0a  vfs *, void *);.
58a00 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
58a10 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e  _OMIT_LOAD_EXTEN
58a20 53 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  SION */.SQLITE_P
58a30 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
58a40 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73  e3OsRandomness(s
58a50 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e  qlite3_vfs *, in
58a60 74 2c 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49  t, char *);.SQLI
58a70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
58a80 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 73 71  qlite3OsSleep(sq
58a90 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 69 6e 74  lite3_vfs *, int
58aa0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
58ab0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43  E int sqlite3OsC
58ac0 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74  urrentTime(sqlit
58ad0 65 33 5f 76 66 73 20 2a 2c 20 64 6f 75 62 6c 65  e3_vfs *, double
58ae0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  *);../*.** Conve
58af0 6e 69 65 6e 63 65 20 66 75 6e 63 74 69 6f 6e 73  nience functions
58b00 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 6e 64   for opening and
58b10 20 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 75   closing files u
58b20 73 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 33  sing .** sqlite3
58b30 5f 6d 61 6c 6c 6f 63 28 29 20 74 6f 20 6f 62 74  _malloc() to obt
58b40 61 69 6e 20 73 70 61 63 65 20 66 6f 72 20 74 68  ain space for th
58b50 65 20 66 69 6c 65 2d 68 61 6e 64 6c 65 20 73 74  e file-handle st
58b60 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49  ructure..*/.SQLI
58b70 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
58b80 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c  qlite3OsOpenMall
58b90 6f 63 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  oc(sqlite3_vfs *
58ba0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  , const char *, 
58bb0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2a 2c  sqlite3_file **,
58bc0 20 69 6e 74 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49   int,int*);.SQLI
58bd0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
58be0 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65  qlite3OsCloseFre
58bf0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
58c00 29 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53  );..#endif /* _S
58c10 51 4c 49 54 45 5f 4f 53 5f 48 5f 20 2a 2f 0a 0a  QLITE_OS_H_ */..
58c20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
58c30 45 6e 64 20 6f 66 20 6f 73 2e 68 20 2a 2a 2a 2a  End of os.h ****
58c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
58c70 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
58c80 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
58c90 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
58ca0 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
58cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
58cc0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
58cd0 49 6e 63 6c 75 64 65 20 6d 75 74 65 78 2e 68 20  Include mutex.h 
58ce0 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
58cf0 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a   sqliteInt.h ***
58d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
58d10 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
58d20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65 78  Begin file mutex
58d30 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
58d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
58d60 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73  /*.** 2007 Augus
58d70 74 20 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  t 28.**.** The a
58d80 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
58d90 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
58da0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
58db0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
58dc0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
58dd0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
58de0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
58df0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
58e00 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
58e10 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
58e20 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
58e30 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
58e40 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
58e50 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
58e60 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
58e70 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
58e80 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
58e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58ed0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
58ee0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ile contains the
58ef0 20 63 6f 6d 6d 6f 6e 20 68 65 61 64 65 72 20 66   common header f
58f00 6f 72 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70  or all mutex imp
58f10 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a  lementations..**
58f20 20 54 68 65 20 73 71 6c 69 74 65 49 6e 74 2e 68   The sqliteInt.h
58f30 20 68 65 61 64 65 72 20 23 69 6e 63 6c 75 64 65   header #include
58f40 73 20 74 68 69 73 20 66 69 6c 65 20 73 6f 20 74  s this file so t
58f50 68 61 74 20 69 74 20 69 73 20 61 76 61 69 6c 61  hat it is availa
58f60 62 6c 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 73 6f  ble.** to all so
58f70 75 72 63 65 20 66 69 6c 65 73 2e 20 20 57 65 20  urce files.  We 
58f80 62 72 65 61 6b 20 69 74 20 6f 75 74 20 69 6e 20  break it out in 
58f90 61 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 65  an effort to kee
58fa0 70 20 74 68 65 20 63 6f 64 65 0a 2a 2a 20 62 65  p the code.** be
58fb0 74 74 65 72 20 6f 72 67 61 6e 69 7a 65 64 2e 0a  tter organized..
58fc0 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 20 73 6f 75  **.** NOTE:  sou
58fd0 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c 64  rce files should
58fe0 20 2a 6e 6f 74 2a 20 23 69 6e 63 6c 75 64 65 20   *not* #include 
58ff0 74 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65  this header file
59000 20 64 69 72 65 63 74 6c 79 2e 0a 2a 2a 20 53 6f   directly..** So
59010 75 72 63 65 20 66 69 6c 65 73 20 73 68 6f 75 6c  urce files shoul
59020 64 20 23 69 6e 63 6c 75 64 65 20 74 68 65 20 73  d #include the s
59030 71 6c 69 74 65 49 6e 74 2e 68 20 66 69 6c 65 20  qliteInt.h file 
59040 61 6e 64 20 6c 65 74 20 74 68 61 74 20 66 69 6c  and let that fil
59050 65 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 74 68 69  e.** include thi
59060 73 20 6f 6e 65 20 69 6e 64 69 72 65 63 74 6c 79  s one indirectly
59070 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74  ..**.** $Id: mut
59080 65 78 2e 68 2c 76 20 31 2e 39 20 32 30 30 38 2f  ex.h,v 1.9 2008/
59090 31 30 2f 30 37 20 31 35 3a 32 35 3a 34 38 20 64  10/07 15:25:48 d
590a0 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a  rh Exp $.*/.../*
590b0 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20 77  .** Figure out w
590c0 68 61 74 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  hat version of t
590d0 68 65 20 63 6f 64 65 20 74 6f 20 75 73 65 2e 20  he code to use. 
590e0 20 54 68 65 20 63 68 6f 69 63 65 73 20 61 72 65   The choices are
590f0 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f  .**.**   SQLITE_
59100 4d 55 54 45 58 5f 4f 4d 49 54 20 20 20 20 20 20  MUTEX_OMIT      
59110 20 20 20 4e 6f 20 6d 75 74 65 78 20 6c 6f 67 69     No mutex logi
59120 63 2e 20 20 4e 6f 74 20 65 76 65 6e 20 73 74 75  c.  Not even stu
59130 62 73 2e 20 20 54 68 65 0a 2a 2a 20 20 20 20 20  bs.  The.**     
59140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59150 20 20 20 20 20 20 20 20 6d 75 74 65 78 65 73 20          mutexes 
59160 69 6d 70 6c 65 6d 65 6e 74 69 6f 6e 20 63 61 6e  implemention can
59170 6e 6f 74 20 62 65 20 6f 76 65 72 72 69 64 64 65  not be overridde
59180 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  n.**            
59190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
591a0 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a   at start-time..
591b0 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d  **.**   SQLITE_M
591c0 55 54 45 58 5f 4e 4f 4f 50 20 20 20 20 20 20 20  UTEX_NOOP       
591d0 20 20 46 6f 72 20 73 69 6e 67 6c 65 2d 74 68 72    For single-thr
591e0 65 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f  eaded applicatio
591f0 6e 73 2e 20 20 4e 6f 0a 2a 2a 20 20 20 20 20 20  ns.  No.**      
59200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59210 20 20 20 20 20 20 20 6d 75 74 75 61 6c 20 65 78         mutual ex
59220 63 6c 75 73 69 6f 6e 20 69 73 20 70 72 6f 76 69  clusion is provi
59230 64 65 64 2e 20 20 42 75 74 20 74 68 69 73 0a 2a  ded.  But this.*
59240 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
59250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6d                im
59260 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e  plementation can
59270 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 61   be overridden a
59280 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
59290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
592a0 20 73 74 61 72 74 2d 74 69 6d 65 2e 0a 2a 2a 0a   start-time..**.
592b0 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  **   SQLITE_MUTE
592c0 58 5f 50 54 48 52 45 41 44 53 20 20 20 20 20 46  X_PTHREADS     F
592d0 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65  or multi-threade
592e0 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f  d applications o
592f0 6e 20 55 6e 69 78 2e 0a 2a 2a 0a 2a 2a 20 20 20  n Unix..**.**   
59300 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32  SQLITE_MUTEX_W32
59310 20 20 20 20 20 20 20 20 20 20 46 6f 72 20 6d 75            For mu
59320 6c 74 69 2d 74 68 72 65 61 64 65 64 20 61 70 70  lti-threaded app
59330 6c 69 63 61 74 69 6f 6e 73 20 6f 6e 20 57 69 6e  lications on Win
59340 33 32 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49  32..**.**   SQLI
59350 54 45 5f 4d 55 54 45 58 5f 4f 53 32 20 20 20 20  TE_MUTEX_OS2    
59360 20 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69 2d        For multi-
59370 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63 61  threaded applica
59380 74 69 6f 6e 73 20 6f 6e 20 4f 53 2f 32 2e 0a 2a  tions on OS/2..*
59390 2f 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 54 48  /.#if !SQLITE_TH
593a0 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e  READSAFE.# defin
593b0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f  e SQLITE_MUTEX_O
593c0 4d 49 54 0a 23 65 6e 64 69 66 0a 23 69 66 20 53  MIT.#endif.#if S
593d0 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
593e0 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
593f0 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 0a  ITE_MUTEX_NOOP).
59400 23 20 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  #  if SQLITE_OS_
59410 55 4e 49 58 0a 23 20 20 20 20 64 65 66 69 6e 65  UNIX.#    define
59420 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54   SQLITE_MUTEX_PT
59430 48 52 45 41 44 53 0a 23 20 20 65 6c 69 66 20 53  HREADS.#  elif S
59440 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20  QLITE_OS_WIN.#  
59450 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
59460 4d 55 54 45 58 5f 57 33 32 0a 23 20 20 65 6c 69  MUTEX_W32.#  eli
59470 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a  f SQLITE_OS_OS2.
59480 23 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49  #    define SQLI
59490 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a 23 20 20  TE_MUTEX_OS2.#  
594a0 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65  else.#    define
594b0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f   SQLITE_MUTEX_NO
594c0 4f 50 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e 64  OP.#  endif.#end
594d0 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
594e0 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f 2a 0a  E_MUTEX_OMIT./*.
594f0 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20  ** If this is a 
59500 6e 6f 2d 6f 70 20 69 6d 70 6c 65 6d 65 6e 74 61  no-op implementa
59510 74 69 6f 6e 2c 20 69 6d 70 6c 65 6d 65 6e 74 20  tion, implement 
59520 65 76 65 72 79 74 68 69 6e 67 20 61 73 20 6d 61  everything as ma
59530 63 72 6f 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  cros..*/.#define
59540 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
59550 6c 6c 6f 63 28 58 29 20 20 20 20 28 28 73 71 6c  lloc(X)    ((sql
59560 69 74 65 33 5f 6d 75 74 65 78 2a 29 38 29 0a 23  ite3_mutex*)8).#
59570 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d  define sqlite3_m
59580 75 74 65 78 5f 66 72 65 65 28 58 29 0a 23 64 65  utex_free(X).#de
59590 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74  fine sqlite3_mut
595a0 65 78 5f 65 6e 74 65 72 28 58 29 0a 23 64 65 66  ex_enter(X).#def
595b0 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ine sqlite3_mute
595c0 78 5f 74 72 79 28 58 29 20 20 20 20 20 20 53 51  x_try(X)      SQ
595d0 4c 49 54 45 5f 4f 4b 0a 23 64 65 66 69 6e 65 20  LITE_OK.#define 
595e0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
595f0 61 76 65 28 58 29 0a 23 64 65 66 69 6e 65 20 73  ave(X).#define s
59600 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
59610 64 28 58 29 20 20 20 20 20 31 0a 23 64 65 66 69  d(X)     1.#defi
59620 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
59630 5f 6e 6f 74 68 65 6c 64 28 58 29 20 20 31 0a 23  _notheld(X)  1.#
59640 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75  define sqlite3Mu
59650 74 65 78 41 6c 6c 6f 63 28 58 29 20 20 20 20 20  texAlloc(X)     
59660 20 28 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78   ((sqlite3_mutex
59670 2a 29 38 29 0a 23 64 65 66 69 6e 65 20 73 71 6c  *)8).#define sql
59680 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 29 20  ite3MutexInit() 
59690 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b         SQLITE_OK
596a0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
596b0 4d 75 74 65 78 45 6e 64 28 29 0a 23 65 6e 64 69  MutexEnd().#endi
596c0 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c  f /* defined(SQL
596d0 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 29 20  ITE_MUTEX_OMIT) 
596e0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
596f0 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65 78  *** End of mutex
59700 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
59710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59730 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
59740 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
59750 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
59760 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20   in sqliteInt.h 
59770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
59780 2a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68  **/.../*.** Each
59790 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74   database file t
597a0 6f 20 62 65 20 61 63 63 65 73 73 65 64 20 62 79  o be accessed by
597b0 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 20 61   the system is a
597c0 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66  n instance.** of
597d0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
597e0 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 72 65  tructure.  There
597f0 20 61 72 65 20 6e 6f 72 6d 61 6c 6c 79 20 74 77   are normally tw
59800 6f 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63  o of these struc
59810 74 75 72 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20  tures.** in the 
59820 73 71 6c 69 74 65 2e 61 44 62 5b 5d 20 61 72 72  sqlite.aDb[] arr
59830 61 79 2e 20 20 61 44 62 5b 30 5d 20 69 73 20 74  ay.  aDb[0] is t
59840 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
59850 20 66 69 6c 65 20 61 6e 64 0a 2a 2a 20 61 44 62   file and.** aDb
59860 5b 31 5d 20 69 73 20 74 68 65 20 64 61 74 61 62  [1] is the datab
59870 61 73 65 20 66 69 6c 65 20 75 73 65 64 20 74 6f  ase file used to
59880 20 68 6f 6c 64 20 74 65 6d 70 6f 72 61 72 79 20   hold temporary 
59890 74 61 62 6c 65 73 2e 20 20 41 64 64 69 74 69 6f  tables.  Additio
598a0 6e 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 73  nal.** databases
598b0 20 6d 61 79 20 62 65 20 61 74 74 61 63 68 65 64   may be attached
598c0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 44 62 20 7b  ..*/.struct Db {
598d0 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  .  char *zName; 
598e0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
598f0 6f 66 20 74 68 69 73 20 64 61 74 61 62 61 73 65  of this database
59900 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74   */.  Btree *pBt
59910 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ;          /* Th
59920 65 20 42 2a 54 72 65 65 20 73 74 72 75 63 74 75  e B*Tree structu
59930 72 65 20 66 6f 72 20 74 68 69 73 20 64 61 74 61  re for this data
59940 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75  base file */.  u
59950 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 20 20  8 inTrans;      
59960 20 20 20 20 2f 2a 20 30 3a 20 6e 6f 74 20 77 72      /* 0: not wr
59970 69 74 61 62 6c 65 2e 20 20 31 3a 20 54 72 61 6e  itable.  1: Tran
59980 73 61 63 74 69 6f 6e 2e 20 20 32 3a 20 43 68 65  saction.  2: Che
59990 63 6b 70 6f 69 6e 74 20 2a 2f 0a 20 20 75 38 20  ckpoint */.  u8 
599a0 73 61 66 65 74 79 5f 6c 65 76 65 6c 3b 20 20 20  safety_level;   
599b0 20 20 2f 2a 20 48 6f 77 20 61 67 67 72 65 73 73    /* How aggress
599c0 69 76 65 20 61 74 20 73 79 6e 63 69 6e 67 20 64  ive at syncing d
599d0 61 74 61 20 74 6f 20 64 69 73 6b 20 2a 2f 0a 20  ata to disk */. 
599e0 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61   Schema *pSchema
599f0 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72  ;     /* Pointer
59a00 20 74 6f 20 64 61 74 61 62 61 73 65 20 73 63 68   to database sch
59a10 65 6d 61 20 28 70 6f 73 73 69 62 6c 79 20 73 68  ema (possibly sh
59a20 61 72 65 64 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ared) */.};../*.
59a30 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
59a40 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
59a50 73 74 72 75 63 74 75 72 65 20 73 74 6f 72 65 73  structure stores
59a60 20 61 20 64 61 74 61 62 61 73 65 20 73 63 68 65   a database sche
59a70 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ma..**.** If the
59a80 72 65 20 61 72 65 20 6e 6f 20 76 69 72 74 75 61  re are no virtua
59a90 6c 20 74 61 62 6c 65 73 20 63 6f 6e 66 69 67 75  l tables configu
59aa0 72 65 64 20 69 6e 20 74 68 69 73 20 73 63 68 65  red in this sche
59ab0 6d 61 2c 20 74 68 65 0a 2a 2a 20 53 63 68 65 6d  ma, the.** Schem
59ac0 61 2e 64 62 20 76 61 72 69 61 62 6c 65 20 69 73  a.db variable is
59ad0 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 41 66   set to NULL. Af
59ae0 74 65 72 20 74 68 65 20 66 69 72 73 74 20 76 69  ter the first vi
59af0 72 74 75 61 6c 20 74 61 62 6c 65 0a 2a 2a 20 68  rtual table.** h
59b00 61 73 20 62 65 65 6e 20 61 64 64 65 64 2c 20 69  as been added, i
59b10 74 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e  t is set to poin
59b20 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  t to the databas
59b30 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a  e connection .**
59b40 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20   used to create 
59b50 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  the connection. 
59b60 4f 6e 63 65 20 61 20 76 69 72 74 75 61 6c 20 74  Once a virtual t
59b70 61 62 6c 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a  able has been.**
59b80 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 53 63   added to the Sc
59b90 68 65 6d 61 20 73 74 72 75 63 74 75 72 65 20 61  hema structure a
59ba0 6e 64 20 74 68 65 20 53 63 68 65 6d 61 2e 64 62  nd the Schema.db
59bb0 20 76 61 72 69 61 62 6c 65 20 70 6f 70 75 6c 61   variable popula
59bc0 74 65 64 2c 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68  ted, .** only th
59bd0 61 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  at database conn
59be0 65 63 74 69 6f 6e 20 6d 61 79 20 75 73 65 20 74  ection may use t
59bf0 68 65 20 53 63 68 65 6d 61 20 74 6f 20 70 72 65  he Schema to pre
59c00 70 61 72 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65  pare .** stateme
59c10 6e 74 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53  nts..*/.struct S
59c20 63 68 65 6d 61 20 7b 0a 20 20 69 6e 74 20 73 63  chema {.  int sc
59c30 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f  hema_cookie;   /
59c40 2a 20 44 61 74 61 62 61 73 65 20 73 63 68 65 6d  * Database schem
59c50 61 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72  a version number
59c60 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a   for this file *
59c70 2f 0a 20 20 48 61 73 68 20 74 62 6c 48 61 73 68  /.  Hash tblHash
59c80 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20  ;        /* All 
59c90 74 61 62 6c 65 73 20 69 6e 64 65 78 65 64 20 62  tables indexed b
59ca0 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68  y name */.  Hash
59cb0 20 69 64 78 48 61 73 68 3b 20 20 20 20 20 20 20   idxHash;       
59cc0 20 2f 2a 20 41 6c 6c 20 28 6e 61 6d 65 64 29 20   /* All (named) 
59cd0 69 6e 64 69 63 65 73 20 69 6e 64 65 78 65 64 20  indices indexed 
59ce0 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73  by name */.  Has
59cf0 68 20 74 72 69 67 48 61 73 68 3b 20 20 20 20 20  h trigHash;     
59d00 20 20 2f 2a 20 41 6c 6c 20 74 72 69 67 67 65 72    /* All trigger
59d10 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d  s indexed by nam
59d20 65 20 2a 2f 0a 20 20 48 61 73 68 20 66 6b 65 79  e */.  Hash fkey
59d30 48 61 73 68 3b 20 20 20 20 20 20 20 2f 2a 20 41  Hash;       /* A
59d40 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20  ll foreign keys 
59d50 62 79 20 72 65 66 65 72 65 6e 63 65 64 20 74 61  by referenced ta
59d60 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 54 61  ble name */.  Ta
59d70 62 6c 65 20 2a 70 53 65 71 54 61 62 3b 20 20 20  ble *pSeqTab;   
59d80 20 20 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65     /* The sqlite
59d90 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20  _sequence table 
59da0 75 73 65 64 20 62 79 20 41 55 54 4f 49 4e 43 52  used by AUTOINCR
59db0 45 4d 45 4e 54 20 2a 2f 0a 20 20 75 38 20 66 69  EMENT */.  u8 fi
59dc0 6c 65 5f 66 6f 72 6d 61 74 3b 20 20 20 20 20 20  le_format;      
59dd0 2f 2a 20 53 63 68 65 6d 61 20 66 6f 72 6d 61 74  /* Schema format
59de0 20 76 65 72 73 69 6f 6e 20 66 6f 72 20 74 68 69   version for thi
59df0 73 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 65  s file */.  u8 e
59e00 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nc;             
59e10 20 2f 2a 20 54 65 78 74 20 65 6e 63 6f 64 69 6e   /* Text encodin
59e20 67 20 75 73 65 64 20 62 79 20 74 68 69 73 20 64  g used by this d
59e30 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 31 36  atabase */.  u16
59e40 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
59e50 20 20 2f 2a 20 46 6c 61 67 73 20 61 73 73 6f 63    /* Flags assoc
59e60 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20  iated with this 
59e70 73 63 68 65 6d 61 20 2a 2f 0a 20 20 69 6e 74 20  schema */.  int 
59e80 63 61 63 68 65 5f 73 69 7a 65 3b 20 20 20 20 20  cache_size;     
59e90 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
59ea0 67 65 73 20 74 6f 20 75 73 65 20 69 6e 20 74 68  ges to use in th
59eb0 65 20 63 61 63 68 65 20 2a 2f 0a 23 69 66 6e 64  e cache */.#ifnd
59ec0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
59ed0 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 73 71  IRTUALTABLE.  sq
59ee0 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
59ef0 20 20 20 2f 2a 20 22 4f 77 6e 65 72 22 20 63 6f     /* "Owner" co
59f00 6e 6e 65 63 74 69 6f 6e 2e 20 53 65 65 20 63 6f  nnection. See co
59f10 6d 6d 65 6e 74 20 61 62 6f 76 65 20 2a 2f 0a 23  mment above */.#
59f20 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  endif.};../*.** 
59f30 54 68 65 73 65 20 6d 61 63 72 6f 73 20 63 61 6e  These macros can
59f40 20 62 65 20 75 73 65 64 20 74 6f 20 74 65 73 74   be used to test
59f50 2c 20 73 65 74 2c 20 6f 72 20 63 6c 65 61 72 20  , set, or clear 
59f60 62 69 74 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20  bits in the .** 
59f70 44 62 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a  Db.flags field..
59f80 2a 2f 0a 23 64 65 66 69 6e 65 20 44 62 48 61 73  */.#define DbHas
59f90 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20  Property(D,I,P) 
59fa0 20 20 20 20 28 28 28 44 29 2d 3e 61 44 62 5b 49      (((D)->aDb[I
59fb0 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73  ].pSchema->flags
59fc0 26 28 50 29 29 3d 3d 28 50 29 29 0a 23 64 65 66  &(P))==(P)).#def
59fd0 69 6e 65 20 44 62 48 61 73 41 6e 79 50 72 6f 70  ine DbHasAnyProp
59fe0 65 72 74 79 28 44 2c 49 2c 50 29 20 20 28 28 28  erty(D,I,P)  (((
59ff0 44 29 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65  D)->aDb[I].pSche
5a000 6d 61 2d 3e 66 6c 61 67 73 26 28 50 29 29 21 3d  ma->flags&(P))!=
5a010 30 29 0a 23 64 65 66 69 6e 65 20 44 62 53 65 74  0).#define DbSet
5a020 50 72 6f 70 65 72 74 79 28 44 2c 49 2c 50 29 20  Property(D,I,P) 
5a030 20 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d 2e      (D)->aDb[I].
5a040 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 7c 3d  pSchema->flags|=
5a050 28 50 29 0a 23 64 65 66 69 6e 65 20 44 62 43 6c  (P).#define DbCl
5a060 65 61 72 50 72 6f 70 65 72 74 79 28 44 2c 49 2c  earProperty(D,I,
5a070 50 29 20 20 20 28 44 29 2d 3e 61 44 62 5b 49 5d  P)   (D)->aDb[I]
5a080 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73 26  .pSchema->flags&
5a090 3d 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  =~(P)../*.** All
5a0a0 6f 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20  owed values for 
5a0b0 74 68 65 20 44 42 2e 66 6c 61 67 73 20 66 69 65  the DB.flags fie
5a0c0 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 44 42  ld..**.** The DB
5a0d0 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 20 66 6c  _SchemaLoaded fl
5a0e0 61 67 20 69 73 20 73 65 74 20 61 66 74 65 72 20  ag is set after 
5a0f0 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
5a100 65 6d 61 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20  ema has been.** 
5a110 72 65 61 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e  read into intern
5a120 61 6c 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a  al hash tables..
5a130 2a 2a 0a 2a 2a 20 44 42 5f 55 6e 72 65 73 65 74  **.** DB_Unreset
5a140 56 69 65 77 73 20 6d 65 61 6e 73 20 74 68 61 74  Views means that
5a150 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 76 69 65   one or more vie
5a160 77 73 20 68 61 76 65 20 63 6f 6c 75 6d 6e 20 6e  ws have column n
5a170 61 6d 65 73 20 74 68 61 74 0a 2a 2a 20 68 61 76  ames that.** hav
5a180 65 20 62 65 65 6e 20 66 69 6c 6c 65 64 20 6f 75  e been filled ou
5a190 74 2e 20 20 49 66 20 74 68 65 20 73 63 68 65 6d  t.  If the schem
5a1a0 61 20 63 68 61 6e 67 65 73 2c 20 74 68 65 73 65  a changes, these
5a1b0 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6d 69   column names mi
5a1c0 67 68 74 0a 2a 2a 20 63 68 61 6e 67 65 73 20 61  ght.** changes a
5a1d0 6e 64 20 73 6f 20 74 68 65 20 76 69 65 77 20 77  nd so the view w
5a1e0 69 6c 6c 20 6e 65 65 64 20 74 6f 20 62 65 20 72  ill need to be r
5a1f0 65 73 65 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  eset..*/.#define
5a200 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64   DB_SchemaLoaded
5a210 20 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 54      0x0001  /* T
5a220 68 65 20 73 63 68 65 6d 61 20 68 61 73 20 62 65  he schema has be
5a230 65 6e 20 6c 6f 61 64 65 64 20 2a 2f 0a 23 64 65  en loaded */.#de
5a240 66 69 6e 65 20 44 42 5f 55 6e 72 65 73 65 74 56  fine DB_UnresetV
5a250 69 65 77 73 20 20 20 20 30 78 30 30 30 32 20 20  iews    0x0002  
5a260 2f 2a 20 53 6f 6d 65 20 76 69 65 77 73 20 68 61  /* Some views ha
5a270 76 65 20 64 65 66 69 6e 65 64 20 63 6f 6c 75 6d  ve defined colum
5a280 6e 20 6e 61 6d 65 73 20 2a 2f 0a 23 64 65 66 69  n names */.#defi
5a290 6e 65 20 44 42 5f 45 6d 70 74 79 20 20 20 20 20  ne DB_Empty     
5a2a0 20 20 20 20 20 20 30 78 30 30 30 34 20 20 2f 2a        0x0004  /*
5a2b0 20 54 68 65 20 66 69 6c 65 20 69 73 20 65 6d 70   The file is emp
5a2c0 74 79 20 28 6c 65 6e 67 74 68 20 30 20 62 79 74  ty (length 0 byt
5a2d0 65 73 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  es) */../*.** Th
5a2e0 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66 66  e number of diff
5a2f0 65 72 65 6e 74 20 6b 69 6e 64 73 20 6f 66 20 74  erent kinds of t
5a300 68 69 6e 67 73 20 74 68 61 74 20 63 61 6e 20 62  hings that can b
5a310 65 20 6c 69 6d 69 74 65 64 0a 2a 2a 20 75 73 69  e limited.** usi
5a320 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6c  ng the sqlite3_l
5a330 69 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63 65  imit() interface
5a340 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
5a350 49 54 45 5f 4e 5f 4c 49 4d 49 54 20 28 53 51 4c  ITE_N_LIMIT (SQL
5a360 49 54 45 5f 4c 49 4d 49 54 5f 54 52 49 47 47 45  ITE_LIMIT_TRIGGE
5a370 52 5f 44 45 50 54 48 2b 31 29 0a 0a 2f 2a 0a 2a  R_DEPTH+1)../*.*
5a380 2a 20 4c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c  * Lookaside mall
5a390 6f 63 20 69 73 20 61 20 73 65 74 20 6f 66 20 66  oc is a set of f
5a3a0 69 78 65 64 2d 73 69 7a 65 20 62 75 66 66 65 72  ixed-size buffer
5a3b0 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73  s that can be us
5a3c0 65 64 0a 2a 2a 20 74 6f 20 73 61 74 69 73 66 79  ed.** to satisfy
5a3d0 20 73 6d 61 6c 6c 20 74 72 61 6e 73 69 65 6e 74   small transient
5a3e0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
5a3f0 6f 6e 20 72 65 71 75 65 73 74 73 20 66 6f 72 20  on requests for 
5a400 6f 62 6a 65 63 74 73 0a 2a 2a 20 61 73 73 6f 63  objects.** assoc
5a410 69 61 74 65 64 20 77 69 74 68 20 61 20 70 61 72  iated with a par
5a420 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65  ticular database
5a430 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68   connection.  Th
5a440 65 20 75 73 65 20 6f 66 0a 2a 2a 20 6c 6f 6f 6b  e use of.** look
5a450 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 70 72 6f  aside malloc pro
5a460 76 69 64 65 73 20 61 20 73 69 67 6e 69 66 69 63  vides a signific
5a470 61 6e 74 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  ant performance 
5a480 65 6e 68 61 6e 63 65 6d 65 6e 74 0a 2a 2a 20 28  enhancement.** (
5a490 61 70 70 72 6f 78 20 31 30 25 29 20 62 79 20 61  approx 10%) by a
5a4a0 76 6f 69 64 69 6e 67 20 6e 75 6d 65 72 6f 75 73  voiding numerous
5a4b0 20 6d 61 6c 6c 6f 63 2f 66 72 65 65 20 72 65 71   malloc/free req
5a4c0 75 65 73 74 73 20 77 68 69 6c 65 20 70 61 72 73  uests while pars
5a4d0 69 6e 67 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65  ing.** SQL state
5a4e0 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ments..**.** The
5a4f0 20 4c 6f 6f 6b 61 73 69 64 65 20 73 74 72 75 63   Lookaside struc
5a500 74 75 72 65 20 68 6f 6c 64 73 20 63 6f 6e 66 69  ture holds confi
5a510 67 75 72 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61  guration informa
5a520 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 0a 2a  tion about the.*
5a530 2a 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 61 6c 6c  * lookaside mall
5a540 6f 63 20 73 75 62 73 79 73 74 65 6d 2e 20 20 45  oc subsystem.  E
5a550 61 63 68 20 61 76 61 69 6c 61 62 6c 65 20 6d 65  ach available me
5a560 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
5a570 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f 6f 6b 61 73  in.** the lookas
5a580 69 64 65 20 73 75 62 73 79 73 74 65 6d 20 69 73  ide subsystem is
5a590 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 6c 69 6e   stored on a lin
5a5a0 6b 65 64 20 6c 69 73 74 20 6f 66 20 4c 6f 6f 6b  ked list of Look
5a5b0 61 73 69 64 65 53 6c 6f 74 0a 2a 2a 20 6f 62 6a  asideSlot.** obj
5a5c0 65 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 6f 6b  ects..**.** Look
5a5d0 61 73 69 64 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  aside allocation
5a5e0 73 20 61 72 65 20 6f 6e 6c 79 20 61 6c 6c 6f 77  s are only allow
5a5f0 65 64 20 66 6f 72 20 6f 62 6a 65 63 74 73 20 74  ed for objects t
5a600 68 61 74 20 61 72 65 20 61 73 73 6f 63 69 61 74  hat are associat
5a610 65 64 0a 2a 2a 20 77 69 74 68 20 61 20 70 61 72  ed.** with a par
5a620 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65  ticular database
5a630 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 48 65   connection.  He
5a640 6e 63 65 2c 20 73 63 68 65 6d 61 20 69 6e 66 6f  nce, schema info
5a650 72 6d 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 0a 2a  rmation cannot.*
5a660 2a 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 6c  * be stored in l
5a670 6f 6f 6b 61 73 69 64 65 20 62 65 63 61 75 73 65  ookaside because
5a680 20 69 6e 20 73 68 61 72 65 64 20 63 61 63 68 65   in shared cache
5a690 20 6d 6f 64 65 20 74 68 65 20 73 63 68 65 6d 61   mode the schema
5a6a0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20   information.** 
5a6b0 69 73 20 73 68 61 72 65 64 20 62 79 20 6d 75 6c  is shared by mul
5a6c0 74 69 70 6c 65 20 64 61 74 61 62 61 73 65 20 63  tiple database c
5a6d0 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 54 68 65  onnections.  The
5a6e0 72 65 66 6f 72 65 2c 20 77 68 69 6c 65 20 70 61  refore, while pa
5a6f0 72 73 69 6e 67 0a 2a 2a 20 73 63 68 65 6d 61 20  rsing.** schema 
5a700 69 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65  information, the
5a710 20 4c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62   Lookaside.bEnab
5a720 6c 65 64 20 66 6c 61 67 20 69 73 20 63 6c 65 61  led flag is clea
5a730 72 65 64 20 73 6f 20 74 68 61 74 0a 2a 2a 20 6c  red so that.** l
5a740 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f 63 61 74  ookaside allocat
5a750 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 75 73 65  ions are not use
5a760 64 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 74  d to construct t
5a770 68 65 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74  he schema object
5a780 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4c 6f 6f  s..*/.struct Loo
5a790 6b 61 73 69 64 65 20 7b 0a 20 20 75 31 36 20 73  kaside {.  u16 s
5a7a0 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  z;              
5a7b0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61     /* Size of ea
5a7c0 63 68 20 62 75 66 66 65 72 20 69 6e 20 62 79 74  ch buffer in byt
5a7d0 65 73 20 2a 2f 0a 20 20 75 38 20 62 45 6e 61 62  es */.  u8 bEnab
5a7e0 6c 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  led;            
5a7f0 2f 2a 20 46 61 6c 73 65 20 74 6f 20 64 69 73 61  /* False to disa
5a800 62 6c 65 20 6e 65 77 20 6c 6f 6f 6b 61 73 69 64  ble new lookasid
5a810 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f  e allocations */
5a820 0a 20 20 75 38 20 62 4d 61 6c 6c 6f 63 65 64 3b  .  u8 bMalloced;
5a830 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
5a840 75 65 20 69 66 20 70 53 74 61 72 74 20 6f 62 74  ue if pStart obt
5a850 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
5a860 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20  e3_malloc() */. 
5a870 20 69 6e 74 20 6e 4f 75 74 3b 20 20 20 20 20 20   int nOut;      
5a880 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
5a890 65 72 20 6f 66 20 62 75 66 66 65 72 73 20 63 75  er of buffers cu
5a8a0 72 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20  rrently checked 
5a8b0 6f 75 74 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 4f  out */.  int mxO
5a8c0 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ut;             
5a8d0 20 2f 2a 20 48 69 67 68 77 61 74 65 72 20 6d 61   /* Highwater ma
5a8e0 72 6b 20 66 6f 72 20 6e 4f 75 74 20 2a 2f 0a 20  rk for nOut */. 
5a8f0 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a   LookasideSlot *
5a900 70 46 72 65 65 3b 20 20 20 2f 2a 20 4c 69 73 74  pFree;   /* List
5a910 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 62 75   of available bu
5a920 66 66 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20  ffers */.  void 
5a930 2a 70 53 74 61 72 74 3b 20 20 20 20 20 20 20 20  *pStart;        
5a940 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65     /* First byte
5a950 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 6d 65   of available me
5a960 6d 6f 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20  mory space */.  
5a970 76 6f 69 64 20 2a 70 45 6e 64 3b 20 20 20 20 20  void *pEnd;     
5a980 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
5a990 20 62 79 74 65 20 70 61 73 74 20 65 6e 64 20 6f   byte past end o
5a9a0 66 20 61 76 61 69 6c 61 62 6c 65 20 73 70 61 63  f available spac
5a9b0 65 20 2a 2f 0a 7d 3b 0a 73 74 72 75 63 74 20 4c  e */.};.struct L
5a9c0 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 7b 0a 20  ookasideSlot {. 
5a9d0 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a   LookasideSlot *
5a9e0 70 4e 65 78 74 3b 20 20 20 20 2f 2a 20 4e 65 78  pNext;    /* Nex
5a9f0 74 20 62 75 66 66 65 72 20 69 6e 20 74 68 65 20  t buffer in the 
5aa00 6c 69 73 74 20 6f 66 20 66 72 65 65 20 62 75 66  list of free buf
5aa10 66 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  fers */.};../*.*
5aa20 2a 20 41 20 68 61 73 68 20 74 61 62 6c 65 20 66  * A hash table f
5aa30 6f 72 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69  or function defi
5aa40 6e 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 48  nitions..**.** H
5aa50 61 73 68 20 65 61 63 68 20 46 75 6e 63 44 65 66  ash each FuncDef
5aa60 20 73 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20   structure into 
5aa70 6f 6e 65 20 6f 66 20 74 68 65 20 46 75 6e 63 44  one of the FuncD
5aa80 65 66 48 61 73 68 2e 61 5b 5d 20 73 6c 6f 74 73  efHash.a[] slots
5aa90 2e 0a 2a 2a 20 43 6f 6c 6c 69 73 69 6f 6e 73 20  ..** Collisions 
5aaa0 61 72 65 20 6f 6e 20 74 68 65 20 46 75 6e 63 44  are on the FuncD
5aab0 65 66 2e 70 48 61 73 68 20 63 68 61 69 6e 2e 0a  ef.pHash chain..
5aac0 2a 2f 0a 73 74 72 75 63 74 20 46 75 6e 63 44 65  */.struct FuncDe
5aad0 66 48 61 73 68 20 7b 0a 20 20 46 75 6e 63 44 65  fHash {.  FuncDe
5aae0 66 20 2a 61 5b 32 33 5d 3b 20 20 20 20 20 20 20  f *a[23];       
5aaf0 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f  /* Hash table fo
5ab00 72 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d  r functions */.}
5ab10 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61  ;../*.** Each da
5ab20 74 61 62 61 73 65 20 69 73 20 61 6e 20 69 6e 73  tabase is an ins
5ab30 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
5ab40 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
5ab50 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
5ab60 74 65 2e 6c 61 73 74 52 6f 77 69 64 20 72 65 63  te.lastRowid rec
5ab70 6f 72 64 73 20 74 68 65 20 6c 61 73 74 20 69 6e  ords the last in
5ab80 73 65 72 74 20 72 6f 77 69 64 20 67 65 6e 65 72  sert rowid gener
5ab90 61 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e  ated by an.** in
5aba0 73 65 72 74 20 73 74 61 74 65 6d 65 6e 74 2e 20  sert statement. 
5abb0 20 49 6e 73 65 72 74 73 20 6f 6e 20 76 69 65 77   Inserts on view
5abc0 73 20 64 6f 20 6e 6f 74 20 61 66 66 65 63 74 20  s do not affect 
5abd0 69 74 73 20 76 61 6c 75 65 2e 20 20 45 61 63 68  its value.  Each
5abe0 0a 2a 2a 20 74 72 69 67 67 65 72 20 68 61 73 20  .** trigger has 
5abf0 69 74 73 20 6f 77 6e 20 63 6f 6e 74 65 78 74 2c  its own context,
5ac00 20 73 6f 20 74 68 61 74 20 6c 61 73 74 52 6f 77   so that lastRow
5ac10 69 64 20 63 61 6e 20 62 65 20 75 70 64 61 74 65  id can be update
5ac20 64 20 69 6e 73 69 64 65 0a 2a 2a 20 74 72 69 67  d inside.** trig
5ac30 67 65 72 73 20 61 73 20 75 73 75 61 6c 2e 20 20  gers as usual.  
5ac40 54 68 65 20 70 72 65 76 69 6f 75 73 20 76 61 6c  The previous val
5ac50 75 65 20 77 69 6c 6c 20 62 65 20 72 65 73 74 6f  ue will be resto
5ac60 72 65 64 20 6f 6e 63 65 20 74 68 65 20 74 72 69  red once the tri
5ac70 67 67 65 72 0a 2a 2a 20 65 78 69 74 73 2e 20 20  gger.** exits.  
5ac80 55 70 6f 6e 20 65 6e 74 65 72 69 6e 67 20 61 20  Upon entering a 
5ac90 62 65 66 6f 72 65 20 6f 72 20 69 6e 73 74 65 61  before or instea
5aca0 64 20 6f 66 20 74 72 69 67 67 65 72 2c 20 6c 61  d of trigger, la
5acb0 73 74 52 6f 77 69 64 20 69 73 20 6e 6f 0a 2a 2a  stRowid is no.**
5acc0 20 6c 6f 6e 67 65 72 20 28 73 69 6e 63 65 20 61   longer (since a
5acd0 66 74 65 72 20 76 65 72 73 69 6f 6e 20 32 2e 38  fter version 2.8
5ace0 2e 31 32 29 20 72 65 73 65 74 20 74 6f 20 2d 31  .12) reset to -1
5acf0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
5ad00 74 65 2e 6e 43 68 61 6e 67 65 20 64 6f 65 73 20  te.nChange does 
5ad10 6e 6f 74 20 63 6f 75 6e 74 20 63 68 61 6e 67 65  not count change
5ad20 73 20 77 69 74 68 69 6e 20 74 72 69 67 67 65 72  s within trigger
5ad30 73 20 61 6e 64 20 6b 65 65 70 73 20 6e 6f 0a 2a  s and keeps no.*
5ad40 2a 20 63 6f 6e 74 65 78 74 2e 20 20 49 74 20 69  * context.  It i
5ad50 73 20 72 65 73 65 74 20 61 74 20 73 74 61 72 74  s reset at start
5ad60 20 6f 66 20 73 71 6c 69 74 65 33 5f 65 78 65 63   of sqlite3_exec
5ad70 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e  ..** The sqlite.
5ad80 6c 73 43 68 61 6e 67 65 20 72 65 70 72 65 73 65  lsChange represe
5ad90 6e 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  nts the number o
5ada0 66 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 62  f changes made b
5adb0 79 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 69 6e  y the last.** in
5adc0 73 65 72 74 2c 20 75 70 64 61 74 65 2c 20 6f 72  sert, update, or
5add0 20 64 65 6c 65 74 65 20 73 74 61 74 65 6d 65 6e   delete statemen
5ade0 74 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 63  t.  It remains c
5adf0 6f 6e 73 74 61 6e 74 20 74 68 72 6f 75 67 68 6f  onstant througho
5ae00 75 74 20 74 68 65 0a 2a 2a 20 6c 65 6e 67 74 68  ut the.** length
5ae10 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20   of a statement 
5ae20 61 6e 64 20 69 73 20 74 68 65 6e 20 75 70 64 61  and is then upda
5ae30 74 65 64 20 62 79 20 4f 50 5f 53 65 74 43 6f 75  ted by OP_SetCou
5ae40 6e 74 73 2e 20 20 49 74 20 6b 65 65 70 73 20 61  nts.  It keeps a
5ae50 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 73 74 61 63  .** context stac
5ae60 6b 20 6a 75 73 74 20 6c 69 6b 65 20 6c 61 73 74  k just like last
5ae70 52 6f 77 69 64 20 73 6f 20 74 68 61 74 20 74 68  Rowid so that th
5ae80 65 20 63 6f 75 6e 74 20 6f 66 20 63 68 61 6e 67  e count of chang
5ae90 65 73 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 74  es.** within a t
5aea0 72 69 67 67 65 72 20 69 73 20 6e 6f 74 20 73 65  rigger is not se
5aeb0 65 6e 20 6f 75 74 73 69 64 65 20 74 68 65 20 74  en outside the t
5aec0 72 69 67 67 65 72 2e 20 20 43 68 61 6e 67 65 73  rigger.  Changes
5aed0 20 74 6f 20 76 69 65 77 73 20 64 6f 20 6e 6f 74   to views do not
5aee0 0a 2a 2a 20 61 66 66 65 63 74 20 74 68 65 20 76  .** affect the v
5aef0 61 6c 75 65 20 6f 66 20 6c 73 43 68 61 6e 67 65  alue of lsChange
5af00 2e 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e  ..** The sqlite.
5af10 63 73 43 68 61 6e 67 65 20 6b 65 65 70 73 20 74  csChange keeps t
5af20 72 61 63 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62  rack of the numb
5af30 65 72 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68  er of current ch
5af40 61 6e 67 65 73 20 28 73 69 6e 63 65 0a 2a 2a 20  anges (since.** 
5af50 74 68 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65  the last stateme
5af60 6e 74 29 20 61 6e 64 20 69 73 20 75 73 65 64 20  nt) and is used 
5af70 74 6f 20 75 70 64 61 74 65 20 73 71 6c 69 74 65  to update sqlite
5af80 5f 6c 73 43 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a  _lsChange..**.**
5af90 20 54 68 65 20 6d 65 6d 62 65 72 20 76 61 72 69   The member vari
5afa0 61 62 6c 65 73 20 73 71 6c 69 74 65 2e 65 72 72  ables sqlite.err
5afb0 43 6f 64 65 2c 20 73 71 6c 69 74 65 2e 7a 45 72  Code, sqlite.zEr
5afc0 72 4d 73 67 20 61 6e 64 20 73 71 6c 69 74 65 2e  rMsg and sqlite.
5afd0 7a 45 72 72 4d 73 67 31 36 0a 2a 2a 20 73 74 6f  zErrMsg16.** sto
5afe0 72 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  re the most rece
5aff0 6e 74 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e  nt error code an
5b000 64 2c 20 69 66 20 61 70 70 6c 69 63 61 62 6c 65  d, if applicable
5b010 2c 20 73 74 72 69 6e 67 2e 20 54 68 65 0a 2a 2a  , string. The.**
5b020 20 69 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74 69   internal functi
5b030 6f 6e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  on sqlite3Error(
5b040 29 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 74  ) is used to set
5b050 20 74 68 65 73 65 20 76 61 72 69 61 62 6c 65 73   these variables
5b060 0a 2a 2a 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79  .** consistently
5b070 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  ..*/.struct sqli
5b080 74 65 33 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  te3 {.  sqlite3_
5b090 76 66 73 20 2a 70 56 66 73 3b 20 20 20 20 20 20  vfs *pVfs;      
5b0a0 20 20 20 20 20 20 2f 2a 20 4f 53 20 49 6e 74 65        /* OS Inte
5b0b0 72 66 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  rface */.  int n
5b0c0 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
5b0d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
5b0e0 65 72 20 6f 66 20 62 61 63 6b 65 6e 64 73 20 63  er of backends c
5b0f0 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 20  urrently in use 
5b100 2a 2f 0a 20 20 44 62 20 2a 61 44 62 3b 20 20 20  */.  Db *aDb;   
5b110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b120 20 20 20 2f 2a 20 41 6c 6c 20 62 61 63 6b 65 6e     /* All backen
5b130 64 73 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67  ds */.  int flag
5b140 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
5b150 20 20 20 20 20 20 2f 2a 20 4d 69 73 63 65 6c 6c        /* Miscell
5b160 61 6e 65 6f 75 73 20 66 6c 61 67 73 2e 20 53 65  aneous flags. Se
5b170 65 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74  e below */.  int
5b180 20 6f 70 65 6e 46 6c 61 67 73 3b 20 20 20 20 20   openFlags;     
5b190 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c             /* Fl
5b1a0 61 67 73 20 70 61 73 73 65 64 20 74 6f 20 73 71  ags passed to sq
5b1b0 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28  lite3_vfs.xOpen(
5b1c0 29 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72 43 6f  ) */.  int errCo
5b1d0 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  de;             
5b1e0 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63       /* Most rec
5b1f0 65 6e 74 20 65 72 72 6f 72 20 63 6f 64 65 20 28  ent error code (
5b200 53 51 4c 49 54 45 5f 2a 29 20 2a 2f 0a 20 20 69  SQLITE_*) */.  i
5b210 6e 74 20 65 72 72 4d 61 73 6b 3b 20 20 20 20 20  nt errMask;     
5b220 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5b230 26 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20 77  & result codes w
5b240 69 74 68 20 74 68 69 73 20 62 65 66 6f 72 65 20  ith this before 
5b250 72 65 74 75 72 6e 69 6e 67 20 2a 2f 0a 20 20 75  returning */.  u
5b260 38 20 61 75 74 6f 43 6f 6d 6d 69 74 3b 20 20 20  8 autoCommit;   
5b270 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5b280 54 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20  The auto-commit 
5b290 66 6c 61 67 2e 20 2a 2f 0a 20 20 75 38 20 74 65  flag. */.  u8 te
5b2a0 6d 70 5f 73 74 6f 72 65 3b 20 20 20 20 20 20 20  mp_store;       
5b2b0 20 20 20 20 20 20 20 20 20 2f 2a 20 31 3a 20 66           /* 1: f
5b2c0 69 6c 65 20 32 3a 20 6d 65 6d 6f 72 79 20 30 3a  ile 2: memory 0:
5b2d0 20 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38   default */.  u8
5b2e0 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20   mallocFailed;  
5b2f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
5b300 72 75 65 20 69 66 20 77 65 20 68 61 76 65 20 73  rue if we have s
5b310 65 65 6e 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69  een a malloc fai
5b320 6c 75 72 65 20 2a 2f 0a 20 20 75 38 20 64 66 6c  lure */.  u8 dfl
5b330 74 4c 6f 63 6b 4d 6f 64 65 3b 20 20 20 20 20 20  tLockMode;      
5b340 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75          /* Defau
5b350 6c 74 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20  lt locking-mode 
5b360 66 6f 72 20 61 74 74 61 63 68 65 64 20 64 62 73  for attached dbs
5b370 20 2a 2f 0a 20 20 75 38 20 64 66 6c 74 4a 6f 75   */.  u8 dfltJou
5b380 72 6e 61 6c 4d 6f 64 65 3b 20 20 20 20 20 20 20  rnalMode;       
5b390 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6a      /* Default j
5b3a0 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 66 6f 72 20  ournal mode for 
5b3b0 61 74 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0a  attached dbs */.
5b3c0 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 6e 65    signed char ne
5b3d0 78 74 41 75 74 6f 76 61 63 3b 20 20 20 20 20 20  xtAutovac;      
5b3e0 2f 2a 20 41 75 74 6f 76 61 63 20 73 65 74 74 69  /* Autovac setti
5b3f0 6e 67 20 61 66 74 65 72 20 56 41 43 55 55 4d 20  ng after VACUUM 
5b400 69 66 20 3e 3d 30 20 2a 2f 0a 20 20 69 6e 74 20  if >=0 */.  int 
5b410 6e 65 78 74 50 61 67 65 73 69 7a 65 3b 20 20 20  nextPagesize;   
5b420 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
5b430 65 73 69 7a 65 20 61 66 74 65 72 20 56 41 43 55  esize after VACU
5b440 55 4d 20 69 66 20 3e 30 20 2a 2f 0a 20 20 69 6e  UM if >0 */.  in
5b450 74 20 6e 54 61 62 6c 65 3b 20 20 20 20 20 20 20  t nTable;       
5b460 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
5b470 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20  umber of tables 
5b480 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
5b490 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 44  */.  CollSeq *pD
5b4a0 66 6c 74 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20  fltColl;        
5b4b0 20 20 20 2f 2a 20 54 68 65 20 64 65 66 61 75 6c     /* The defaul
5b4c0 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  t collating sequ
5b4d0 65 6e 63 65 20 28 42 49 4e 41 52 59 29 20 2a 2f  ence (BINARY) */
5b4e0 0a 20 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64  .  i64 lastRowid
5b4f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5b500 20 2f 2a 20 52 4f 57 49 44 20 6f 66 20 6d 6f 73   /* ROWID of mos
5b510 74 20 72 65 63 65 6e 74 20 69 6e 73 65 72 74 20  t recent insert 
5b520 28 73 65 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20  (see above) */. 
5b530 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20 20 20   u32 magic;     
5b540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5b550 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65 72 20 66  * Magic number f
5b560 6f 72 20 64 65 74 65 63 74 20 6c 69 62 72 61 72  or detect librar
5b570 79 20 6d 69 73 75 73 65 20 2a 2f 0a 20 20 69 6e  y misuse */.  in
5b580 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20  t nChange;      
5b590 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
5b5a0 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
5b5b0 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73   sqlite3_changes
5b5c0 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 74  () */.  int nTot
5b5d0 61 6c 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20  alChange;       
5b5e0 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72        /* Value r
5b5f0 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74  eturned by sqlit
5b600 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
5b610 28 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  () */.  sqlite3_
5b620 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20  mutex *mutex;   
5b630 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74        /* Connect
5b640 69 6f 6e 20 6d 75 74 65 78 20 2a 2f 0a 20 20 69  ion mutex */.  i
5b650 6e 74 20 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  nt aLimit[SQLITE
5b660 5f 4e 5f 4c 49 4d 49 54 5d 3b 20 20 20 2f 2a 20  _N_LIMIT];   /* 
5b670 4c 69 6d 69 74 73 20 2a 2f 0a 20 20 73 74 72 75  Limits */.  stru
5b680 63 74 20 73 71 6c 69 74 65 33 49 6e 69 74 49 6e  ct sqlite3InitIn
5b690 66 6f 20 7b 20 20 20 20 20 20 2f 2a 20 49 6e 66  fo {      /* Inf
5b6a0 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 64 75  ormation used du
5b6b0 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74  ring initializat
5b6c0 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ion */.    int i
5b6d0 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
5b6e0 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 62         /* When b
5b6f0 61 63 6b 20 69 73 20 62 65 69 6e 67 20 69 6e 69  ack is being ini
5b700 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 20 20  tialized */.    
5b710 69 6e 74 20 6e 65 77 54 6e 75 6d 3b 20 20 20 20  int newTnum;    
5b720 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
5b730 6f 6f 74 70 61 67 65 20 6f 66 20 74 61 62 6c 65  ootpage of table
5b740 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a   being initializ
5b750 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 62 75 73  ed */.    u8 bus
5b760 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
5b770 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 69 66        /* TRUE if
5b780 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 69 74 69   currently initi
5b790 61 6c 69 7a 69 6e 67 20 2a 2f 0a 20 20 20 20 75  alizing */.    u
5b7a0 38 20 6f 72 70 68 61 6e 54 72 69 67 67 65 72 3b  8 orphanTrigger;
5b7b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61             /* La
5b7c0 73 74 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  st statement is 
5b7d0 6f 72 70 68 61 6e 65 64 20 54 45 4d 50 20 74 72  orphaned TEMP tr
5b7e0 69 67 67 65 72 20 2a 2f 0a 20 20 7d 20 69 6e 69  igger */.  } ini
5b7f0 74 3b 0a 20 20 69 6e 74 20 6e 45 78 74 65 6e 73  t;.  int nExtens
5b800 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  ion;            
5b810 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
5b820 6c 6f 61 64 65 64 20 65 78 74 65 6e 73 69 6f 6e  loaded extension
5b830 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2a 61 45  s */.  void **aE
5b840 78 74 65 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20  xtension;       
5b850 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66       /* Array of
5b860 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20   shared library 
5b870 68 61 6e 64 6c 65 73 20 2a 2f 0a 20 20 73 74 72  handles */.  str
5b880 75 63 74 20 56 64 62 65 20 2a 70 56 64 62 65 3b  uct Vdbe *pVdbe;
5b890 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69             /* Li
5b8a0 73 74 20 6f 66 20 61 63 74 69 76 65 20 76 69 72  st of active vir
5b8b0 74 75 61 6c 20 6d 61 63 68 69 6e 65 73 20 2a 2f  tual machines */
5b8c0 0a 20 20 69 6e 74 20 61 63 74 69 76 65 56 64 62  .  int activeVdb
5b8d0 65 43 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  eCnt;           
5b8e0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 56 44   /* Number of VD
5b8f0 42 45 73 20 63 75 72 72 65 6e 74 6c 79 20 65 78  BEs currently ex
5b900 65 63 75 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74  ecuting */.  int
5b910 20 77 72 69 74 65 56 64 62 65 43 6e 74 3b 20 20   writeVdbeCnt;  
5b920 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
5b930 6d 62 65 72 20 6f 66 20 61 63 74 69 76 65 20 56  mber of active V
5b940 44 42 45 73 20 74 68 61 74 20 61 72 65 20 77 72  DBEs that are wr
5b950 69 74 69 6e 67 20 2a 2f 0a 20 20 76 6f 69 64 20  iting */.  void 
5b960 28 2a 78 54 72 61 63 65 29 28 76 6f 69 64 2a 2c  (*xTrace)(void*,
5b970 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 20 20 20  const char*);   
5b980 20 20 20 20 20 2f 2a 20 54 72 61 63 65 20 66 75       /* Trace fu
5b990 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64  nction */.  void
5b9a0 20 2a 70 54 72 61 63 65 41 72 67 3b 20 20 20 20   *pTraceArg;    
5b9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b9c0 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
5b9d0 74 20 74 6f 20 74 68 65 20 74 72 61 63 65 20 66  t to the trace f
5b9e0 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69  unction */.  voi
5b9f0 64 20 28 2a 78 50 72 6f 66 69 6c 65 29 28 76 6f  d (*xProfile)(vo
5ba00 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  id*,const char*,
5ba10 75 36 34 29 3b 20 20 2f 2a 20 50 72 6f 66 69 6c  u64);  /* Profil
5ba20 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ing function */.
5ba30 20 20 76 6f 69 64 20 2a 70 50 72 6f 66 69 6c 65    void *pProfile
5ba40 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Arg;            
5ba50 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
5ba60 72 67 75 6d 65 6e 74 20 74 6f 20 70 72 6f 66 69  rgument to profi
5ba70 6c 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  le function */. 
5ba80 20 76 6f 69 64 20 2a 70 43 6f 6d 6d 69 74 41 72   void *pCommitAr
5ba90 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
5baa0 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74     /* Argument t
5bab0 6f 20 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63  o xCommitCallbac
5bac0 6b 28 29 20 2a 2f 20 20 20 0a 20 20 69 6e 74 20  k() */   .  int 
5bad0 28 2a 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63  (*xCommitCallbac
5bae0 6b 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 2f 2a  k)(void*);    /*
5baf0 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72   Invoked at ever
5bb00 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76  y commit. */.  v
5bb10 6f 69 64 20 2a 70 52 6f 6c 6c 62 61 63 6b 41 72  oid *pRollbackAr
5bb20 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
5bb30 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20   /* Argument to 
5bb40 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63  xRollbackCallbac
5bb50 6b 28 29 20 2a 2f 20 20 20 0a 20 20 76 6f 69 64  k() */   .  void
5bb60 20 28 2a 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c   (*xRollbackCall
5bb70 62 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20 2f 2a  back)(void*); /*
5bb80 20 49 6e 76 6f 6b 65 64 20 61 74 20 65 76 65 72   Invoked at ever
5bb90 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a 20 20 76  y commit. */.  v
5bba0 6f 69 64 20 2a 70 55 70 64 61 74 65 41 72 67 3b  oid *pUpdateArg;
5bbb0 0a 20 20 76 6f 69 64 20 28 2a 78 55 70 64 61 74  .  void (*xUpdat
5bbc0 65 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a  eCallback)(void*
5bbd0 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72  ,int, const char
5bbe0 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73 71  *,const char*,sq
5bbf0 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a 20 20 76  lite_int64);.  v
5bc00 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64  oid(*xCollNeeded
5bc10 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 2a  )(void*,sqlite3*
5bc20 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63 6f  ,int eTextRep,co
5bc30 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 76 6f  nst char*);.  vo
5bc40 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64 65 64 31  id(*xCollNeeded1
5bc50 36 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33  6)(void*,sqlite3
5bc60 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c 63  *,int eTextRep,c
5bc70 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a 20 20 76  onst void*);.  v
5bc80 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64 65 64  oid *pCollNeeded
5bc90 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76  Arg;.  sqlite3_v
5bca0 61 6c 75 65 20 2a 70 45 72 72 3b 20 20 20 20 20  alue *pErr;     
5bcb0 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63       /* Most rec
5bcc0 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67  ent error messag
5bcd0 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72  e */.  char *zEr
5bce0 72 4d 73 67 3b 20 20 20 20 20 20 20 20 20 20 20  rMsg;           
5bcf0 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63       /* Most rec
5bd00 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67  ent error messag
5bd10 65 20 28 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  e (UTF-8 encoded
5bd20 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72  ) */.  char *zEr
5bd30 72 4d 73 67 31 36 3b 20 20 20 20 20 20 20 20 20  rMsg16;         
5bd40 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20 72 65 63       /* Most rec
5bd50 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67  ent error messag
5bd60 65 20 28 55 54 46 2d 31 36 20 65 6e 63 6f 64 65  e (UTF-16 encode
5bd70 64 29 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a  d) */.  union {.
5bd80 20 20 20 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74      volatile int
5bd90 20 69 73 49 6e 74 65 72 72 75 70 74 65 64 3b 20   isInterrupted; 
5bda0 2f 2a 20 54 72 75 65 20 69 66 20 73 71 6c 69 74  /* True if sqlit
5bdb0 65 33 5f 69 6e 74 65 72 72 75 70 74 20 68 61 73  e3_interrupt has
5bdc0 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 2a 2f 0a   been called */.
5bdd0 20 20 20 20 64 6f 75 62 6c 65 20 6e 6f 74 55 73      double notUs
5bde0 65 64 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  ed1;            
5bdf0 2f 2a 20 53 70 61 63 65 72 20 2a 2f 0a 20 20 7d  /* Spacer */.  }
5be00 20 75 31 3b 0a 20 20 4c 6f 6f 6b 61 73 69 64 65   u1;.  Lookaside
5be10 20 6c 6f 6f 6b 61 73 69 64 65 3b 20 20 20 20 20   lookaside;     
5be20 20 20 20 20 20 2f 2a 20 4c 6f 6f 6b 61 73 69 64       /* Lookasid
5be30 65 20 6d 61 6c 6c 6f 63 20 63 6f 6e 66 69 67 75  e malloc configu
5be40 72 61 74 69 6f 6e 20 2a 2f 0a 23 69 66 6e 64 65  ration */.#ifnde
5be50 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
5be60 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 69 6e  THORIZATION.  in
5be70 74 20 28 2a 78 41 75 74 68 29 28 76 6f 69 64 2a  t (*xAuth)(void*
5be80 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ,int,const char*
5be90 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
5bea0 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
5beb0 68 61 72 2a 29 3b 0a 20 20 20 20 20 20 20 20 20  har*);.         
5bec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5bed0 20 20 20 20 20 20 20 2f 2a 20 41 63 63 65 73 73         /* Access
5bee0 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66   authorization f
5bef0 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69  unction */.  voi
5bf00 64 20 2a 70 41 75 74 68 41 72 67 3b 20 20 20 20  d *pAuthArg;    
5bf10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 73             /* 1s
5bf20 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  t argument to th
5bf30 65 20 61 63 63 65 73 73 20 61 75 74 68 20 66 75  e access auth fu
5bf40 6e 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66  nction */.#endif
5bf50 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
5bf60 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 41  OMIT_PROGRESS_CA
5bf70 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 20 28 2a 78  LLBACK.  int (*x
5bf80 50 72 6f 67 72 65 73 73 29 28 76 6f 69 64 20 2a  Progress)(void *
5bf90 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 70 72  );     /* The pr
5bfa0 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20  ogress callback 
5bfb0 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 50 72 6f 67  */.  void *pProg
5bfc0 72 65 73 73 41 72 67 3b 20 20 20 20 20 20 20 20  ressArg;        
5bfd0 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74     /* Argument t
5bfe0 6f 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 63  o the progress c
5bff0 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74  allback */.  int
5c000 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 3b 20 20   nProgressOps;  
5c010 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
5c020 6d 62 65 72 20 6f 66 20 6f 70 63 6f 64 65 73 20  mber of opcodes 
5c030 66 6f 72 20 70 72 6f 67 72 65 73 73 20 63 61 6c  for progress cal
5c040 6c 62 61 63 6b 20 2a 2f 0a 23 65 6e 64 69 66 0a  lback */.#endif.
5c050 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
5c060 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
5c070 0a 20 20 48 61 73 68 20 61 4d 6f 64 75 6c 65 3b  .  Hash aModule;
5c080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c090 20 2f 2a 20 70 6f 70 75 6c 61 74 65 64 20 62 79   /* populated by
5c0a0 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
5c0b0 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20 20 54 61  module() */.  Ta
5c0c0 62 6c 65 20 2a 70 56 54 61 62 3b 20 20 20 20 20  ble *pVTab;     
5c0d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 76              /* v
5c0e0 74 61 62 20 77 69 74 68 20 61 63 74 69 76 65 20  tab with active 
5c0f0 43 6f 6e 6e 65 63 74 2f 43 72 65 61 74 65 20 6d  Connect/Create m
5c100 65 74 68 6f 64 20 2a 2f 0a 20 20 56 54 61 62 6c  ethod */.  VTabl
5c110 65 20 2a 2a 61 56 54 72 61 6e 73 3b 20 20 20 20  e **aVTrans;    
5c120 20 20 20 20 20 20 20 20 20 2f 2a 20 56 69 72 74           /* Virt
5c130 75 61 6c 20 74 61 62 6c 65 73 20 77 69 74 68 20  ual tables with 
5c140 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e  open transaction
5c150 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 54 72 61  s */.  int nVTra
5c160 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ns;             
5c170 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65       /* Allocate
5c180 64 20 73 69 7a 65 20 6f 66 20 61 56 54 72 61 6e  d size of aVTran
5c190 73 20 2a 2f 0a 20 20 56 54 61 62 6c 65 20 2a 70  s */.  VTable *p
5c1a0 44 69 73 63 6f 6e 6e 65 63 74 3b 20 20 20 20 2f  Disconnect;    /
5c1b0 2a 20 44 69 73 63 6f 6e 6e 65 63 74 20 74 68 65  * Disconnect the
5c1c0 73 65 20 69 6e 20 6e 65 78 74 20 73 71 6c 69 74  se in next sqlit
5c1d0 65 33 5f 70 72 65 70 61 72 65 28 29 20 2a 2f 0a  e3_prepare() */.
5c1e0 23 65 6e 64 69 66 0a 20 20 46 75 6e 63 44 65 66  #endif.  FuncDef
5c1f0 48 61 73 68 20 61 46 75 6e 63 3b 20 20 20 20 20  Hash aFunc;     
5c200 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74         /* Hash t
5c210 61 62 6c 65 20 6f 66 20 63 6f 6e 6e 65 63 74 69  able of connecti
5c220 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a  on functions */.
5c230 20 20 48 61 73 68 20 61 43 6f 6c 6c 53 65 71 3b    Hash aCollSeq;
5c240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c250 2f 2a 20 41 6c 6c 20 63 6f 6c 6c 61 74 69 6e 67  /* All collating
5c260 20 73 65 71 75 65 6e 63 65 73 20 2a 2f 0a 20 20   sequences */.  
5c270 42 75 73 79 48 61 6e 64 6c 65 72 20 62 75 73 79  BusyHandler busy
5c280 48 61 6e 64 6c 65 72 3b 20 20 20 20 20 20 2f 2a  Handler;      /*
5c290 20 42 75 73 79 20 63 61 6c 6c 62 61 63 6b 20 2a   Busy callback *
5c2a0 2f 0a 20 20 69 6e 74 20 62 75 73 79 54 69 6d 65  /.  int busyTime
5c2b0 6f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  out;            
5c2c0 20 20 2f 2a 20 42 75 73 79 20 68 61 6e 64 6c 65    /* Busy handle
5c2d0 72 20 74 69 6d 65 6f 75 74 2c 20 69 6e 20 6d 73  r timeout, in ms
5c2e0 65 63 20 2a 2f 0a 20 20 44 62 20 61 44 62 53 74  ec */.  Db aDbSt
5c2f0 61 74 69 63 5b 32 5d 3b 20 20 20 20 20 20 20 20  atic[2];        
5c300 20 20 20 20 20 20 2f 2a 20 53 74 61 74 69 63 20        /* Static 
5c310 73 70 61 63 65 20 66 6f 72 20 74 68 65 20 32 20  space for the 2 
5c320 64 65 66 61 75 6c 74 20 62 61 63 6b 65 6e 64 73  default backends
5c330 20 2a 2f 0a 20 20 53 61 76 65 70 6f 69 6e 74 20   */.  Savepoint 
5c340 2a 70 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20  *pSavepoint;    
5c350 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61      /* List of a
5c360 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73  ctive savepoints
5c370 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 61 76 65 70   */.  int nSavep
5c380 6f 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  oint;           
5c390 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
5c3a0 20 6e 6f 6e 2d 74 72 61 6e 73 61 63 74 69 6f 6e   non-transaction
5c3b0 20 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20   savepoints */. 
5c3c0 20 69 6e 74 20 6e 53 74 61 74 65 6d 65 6e 74 3b   int nStatement;
5c3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5c3e0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74  * Number of nest
5c3f0 65 64 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61  ed statement-tra
5c400 6e 73 61 63 74 69 6f 6e 73 20 20 2a 2f 0a 20 20  nsactions  */.  
5c410 75 38 20 69 73 54 72 61 6e 73 61 63 74 69 6f 6e  u8 isTransaction
5c420 53 61 76 65 70 6f 69 6e 74 3b 20 20 20 20 2f 2a  Savepoint;    /*
5c430 20 54 72 75 65 20 69 66 20 74 68 65 20 6f 75 74   True if the out
5c440 65 72 6d 6f 73 74 20 73 61 76 65 70 6f 69 6e 74  ermost savepoint
5c450 20 69 73 20 61 20 54 53 20 2a 2f 0a 20 20 69 36   is a TS */.  i6
5c460 34 20 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3b  4 nDeferredCons;
5c470 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
5c480 65 74 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73  et deferred cons
5c490 74 72 61 69 6e 74 73 20 74 68 69 73 20 74 72 61  traints this tra
5c4a0 6e 73 61 63 74 69 6f 6e 2e 20 2a 2f 0a 0a 23 69  nsaction. */..#i
5c4b0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
5c4c0 4c 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59  LE_UNLOCK_NOTIFY
5c4d0 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  /* The follow
5c4e0 69 6e 67 20 76 61 72 69 61 62 6c 65 73 20 61 72  ing variables ar
5c4f0 65 20 61 6c 6c 20 70 72 6f 74 65 63 74 65 64 20  e all protected 
5c500 62 79 20 74 68 65 20 53 54 41 54 49 43 5f 4d 41  by the STATIC_MA
5c510 53 54 45 52 20 0a 20 20 2a 2a 20 6d 75 74 65 78  STER .  ** mutex
5c520 2c 20 6e 6f 74 20 62 79 20 73 71 6c 69 74 65 33  , not by sqlite3
5c530 2e 6d 75 74 65 78 2e 20 54 68 65 79 20 61 72 65  .mutex. They are
5c540 20 75 73 65 64 20 62 79 20 63 6f 64 65 20 69 6e   used by code in
5c550 20 6e 6f 74 69 66 79 2e 63 2e 20 0a 20 20 2a 2a   notify.c. .  **
5c560 0a 20 20 2a 2a 20 57 68 65 6e 20 58 2e 70 55 6e  .  ** When X.pUn
5c570 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d  lockConnection==
5c580 59 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  Y, that means th
5c590 61 74 20 58 20 69 73 20 77 61 69 74 69 6e 67 20  at X is waiting 
5c5a0 66 6f 72 20 59 20 74 6f 0a 20 20 2a 2a 20 75 6e  for Y to.  ** un
5c5b0 6c 6f 63 6b 20 73 6f 20 74 68 61 74 20 69 74 20  lock so that it 
5c5c0 63 61 6e 20 70 72 6f 63 65 65 64 2e 0a 20 20 2a  can proceed..  *
5c5d0 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 58 2e 70 42  *.  ** When X.pB
5c5e0 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f  lockingConnectio
5c5f0 6e 3d 3d 59 2c 20 74 68 61 74 20 6d 65 61 6e 73  n==Y, that means
5c600 20 74 68 61 74 20 73 6f 6d 65 74 68 69 6e 67 20   that something 
5c610 74 68 61 74 20 58 20 74 72 69 65 64 0a 20 20 2a  that X tried.  *
5c620 2a 20 74 72 69 65 64 20 74 6f 20 64 6f 20 72 65  * tried to do re
5c630 63 65 6e 74 6c 79 20 66 61 69 6c 65 64 20 77 69  cently failed wi
5c640 74 68 20 61 6e 20 53 51 4c 49 54 45 5f 4c 4f 43  th an SQLITE_LOC
5c650 4b 45 44 20 65 72 72 6f 72 20 64 75 65 20 74 6f  KED error due to
5c660 20 6c 6f 63 6b 73 0a 20 20 2a 2a 20 68 65 6c 64   locks.  ** held
5c670 20 62 79 20 59 2e 0a 20 20 2a 2f 0a 20 20 73 71   by Y..  */.  sq
5c680 6c 69 74 65 33 20 2a 70 42 6c 6f 63 6b 69 6e 67  lite3 *pBlocking
5c690 43 6f 6e 6e 65 63 74 69 6f 6e 3b 20 2f 2a 20 43  Connection; /* C
5c6a0 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 63  onnection that c
5c6b0 61 75 73 65 64 20 53 51 4c 49 54 45 5f 4c 4f 43  aused SQLITE_LOC
5c6c0 4b 45 44 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  KED */.  sqlite3
5c6d0 20 2a 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74   *pUnlockConnect
5c6e0 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ion;           /
5c6f0 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20  * Connection to 
5c700 77 61 74 63 68 20 66 6f 72 20 75 6e 6c 6f 63 6b  watch for unlock
5c710 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 55 6e 6c   */.  void *pUnl
5c720 6f 63 6b 41 72 67 3b 20 20 20 20 20 20 20 20 20  ockArg;         
5c730 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
5c740 72 67 75 6d 65 6e 74 20 74 6f 20 78 55 6e 6c 6f  rgument to xUnlo
5c750 63 6b 4e 6f 74 69 66 79 20 2a 2f 0a 20 20 76 6f  ckNotify */.  vo
5c760 69 64 20 28 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69  id (*xUnlockNoti
5c770 66 79 29 28 76 6f 69 64 20 2a 2a 2c 20 69 6e 74  fy)(void **, int
5c780 29 3b 20 20 2f 2a 20 55 6e 6c 6f 63 6b 20 6e 6f  );  /* Unlock no
5c790 74 69 66 79 20 63 61 6c 6c 62 61 63 6b 20 2a 2f  tify callback */
5c7a0 0a 20 20 73 71 6c 69 74 65 33 20 2a 70 4e 65 78  .  sqlite3 *pNex
5c7b0 74 42 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20  tBlocked;       
5c7c0 20 2f 2a 20 4e 65 78 74 20 69 6e 20 6c 69 73 74   /* Next in list
5c7d0 20 6f 66 20 61 6c 6c 20 62 6c 6f 63 6b 65 64 20   of all blocked 
5c7e0 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 23  connections */.#
5c7f0 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  endif.};../*.** 
5c800 41 20 6d 61 63 72 6f 20 74 6f 20 64 69 73 63 6f  A macro to disco
5c810 76 65 72 20 74 68 65 20 65 6e 63 6f 64 69 6e 67  ver the encoding
5c820 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 2e 0a   of a database..
5c830 2a 2f 0a 23 64 65 66 69 6e 65 20 45 4e 43 28 64  */.#define ENC(d
5c840 62 29 20 28 28 64 62 29 2d 3e 61 44 62 5b 30 5d  b) ((db)->aDb[0]
5c850 2e 70 53 63 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a  .pSchema->enc)..
5c860 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76  /*.** Possible v
5c870 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73 71  alues for the sq
5c880 6c 69 74 65 2e 66 6c 61 67 73 20 61 6e 64 20 6f  lite.flags and o
5c890 72 20 44 62 2e 66 6c 61 67 73 20 66 69 65 6c 64  r Db.flags field
5c8a0 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 71 6c 69  s..**.** On sqli
5c8b0 74 65 2e 66 6c 61 67 73 2c 20 74 68 65 20 53 51  te.flags, the SQ
5c8c0 4c 49 54 45 5f 49 6e 54 72 61 6e 73 20 76 61 6c  LITE_InTrans val
5c8d0 75 65 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65  ue means that we
5c8e0 20 68 61 76 65 0a 2a 2a 20 65 78 65 63 75 74 65   have.** execute
5c8f0 64 20 61 20 42 45 47 49 4e 2e 20 20 4f 6e 20 44  d a BEGIN.  On D
5c900 62 2e 66 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f  b.flags, SQLITE_
5c910 49 6e 54 72 61 6e 73 20 6d 65 61 6e 73 20 61 20  InTrans means a 
5c920 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61  statement.** tra
5c930 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69  nsaction is acti
5c940 76 65 20 6f 6e 20 74 68 61 74 20 70 61 72 74 69  ve on that parti
5c950 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 66  cular database f
5c960 69 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ile..*/.#define 
5c970 53 51 4c 49 54 45 5f 56 64 62 65 54 72 61 63 65  SQLITE_VdbeTrace
5c980 20 20 20 20 20 20 30 78 30 30 30 30 30 30 30 31        0x00000001
5c990 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61    /* True to tra
5c9a0 63 65 20 56 44 42 45 20 65 78 65 63 75 74 69 6f  ce VDBE executio
5c9b0 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  n */.#define SQL
5c9c0 49 54 45 5f 49 6e 54 72 61 6e 73 20 20 20 20 20  ITE_InTrans     
5c9d0 20 20 20 30 78 30 30 30 30 30 30 30 38 20 20 2f     0x00000008  /
5c9e0 2a 20 54 72 75 65 20 69 66 20 69 6e 20 61 20 74  * True if in a t
5c9f0 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64  ransaction */.#d
5ca00 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 6e 74  efine SQLITE_Int
5ca10 65 72 6e 43 68 61 6e 67 65 73 20 20 30 78 30 30  ernChanges  0x00
5ca20 30 30 30 30 31 30 20 20 2f 2a 20 55 6e 63 6f 6d  000010  /* Uncom
5ca30 6d 69 74 74 65 64 20 48 61 73 68 20 74 61 62 6c  mitted Hash tabl
5ca40 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65  e changes */.#de
5ca50 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c  fine SQLITE_Full
5ca60 43 6f 6c 4e 61 6d 65 73 20 20 20 30 78 30 30 30  ColNames   0x000
5ca70 30 30 30 32 30 20 20 2f 2a 20 53 68 6f 77 20 66  00020  /* Show f
5ca80 75 6c 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  ull column names
5ca90 20 6f 6e 20 53 45 4c 45 43 54 20 2a 2f 0a 23 64   on SELECT */.#d
5caa0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 68 6f  efine SQLITE_Sho
5cab0 72 74 43 6f 6c 4e 61 6d 65 73 20 20 30 78 30 30  rtColNames  0x00
5cac0 30 30 30 30 34 30 20 20 2f 2a 20 53 68 6f 77 20  000040  /* Show 
5cad0 73 68 6f 72 74 20 63 6f 6c 75 6d 6e 73 20 6e 61  short columns na
5cae0 6d 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  mes */.#define S
5caf0 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20  QLITE_CountRows 
5cb00 20 20 20 20 20 30 78 30 30 30 30 30 30 38 30 20       0x00000080 
5cb10 20 2f 2a 20 43 6f 75 6e 74 20 72 6f 77 73 20 63   /* Count rows c
5cb20 68 61 6e 67 65 64 20 62 79 20 49 4e 53 45 52 54  hanged by INSERT
5cb30 2c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  , */.           
5cb40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cb50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
5cb60 2a 20 20 20 44 45 4c 45 54 45 2c 20 6f 72 20 55  *   DELETE, or U
5cb70 50 44 41 54 45 20 61 6e 64 20 72 65 74 75 72 6e  PDATE and return
5cb80 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
5cb90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5cbb0 20 20 20 74 68 65 20 63 6f 75 6e 74 20 75 73 69     the count usi
5cbc0 6e 67 20 61 20 63 61 6c 6c 62 61 63 6b 2e 20 2a  ng a callback. *
5cbd0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5cbe0 5f 4e 75 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 20  _NullCallback   
5cbf0 30 78 30 30 30 30 30 31 30 30 20 20 2f 2a 20 49  0x00000100  /* I
5cc00 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61  nvoke the callba
5cc10 63 6b 20 6f 6e 63 65 20 69 66 20 74 68 65 20 2a  ck once if the *
5cc20 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /.              
5cc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cc40 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20              /*  
5cc50 20 72 65 73 75 6c 74 20 73 65 74 20 69 73 20 65   result set is e
5cc60 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  mpty */.#define 
5cc70 53 51 4c 49 54 45 5f 53 71 6c 54 72 61 63 65 20  SQLITE_SqlTrace 
5cc80 20 20 20 20 20 20 30 78 30 30 30 30 30 32 30 30        0x00000200
5cc90 20 20 2f 2a 20 44 65 62 75 67 20 70 72 69 6e 74    /* Debug print
5cca0 20 53 51 4c 20 61 73 20 69 74 20 65 78 65 63 75   SQL as it execu
5ccb0 74 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  tes */.#define S
5ccc0 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e  QLITE_VdbeListin
5ccd0 67 20 20 20 20 30 78 30 30 30 30 30 34 30 30 20  g    0x00000400 
5cce0 20 2f 2a 20 44 65 62 75 67 20 6c 69 73 74 69 6e   /* Debug listin
5ccf0 67 73 20 6f 66 20 56 44 42 45 20 70 72 6f 67 72  gs of VDBE progr
5cd00 61 6d 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ams */.#define S
5cd10 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d  QLITE_WriteSchem
5cd20 61 20 20 20 20 30 78 30 30 30 30 30 38 30 30 20  a    0x00000800 
5cd30 20 2f 2a 20 4f 4b 20 74 6f 20 75 70 64 61 74 65   /* OK to update
5cd40 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 2a   SQLITE_MASTER *
5cd50 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5cd60 5f 4e 6f 52 65 61 64 6c 6f 63 6b 20 20 20 20 20  _NoReadlock     
5cd70 30 78 30 30 30 30 31 30 30 30 20 20 2f 2a 20 52  0x00001000  /* R
5cd80 65 61 64 6c 6f 63 6b 73 20 61 72 65 20 6f 6d 69  eadlocks are omi
5cd90 74 74 65 64 20 77 68 65 6e 20 0a 20 20 20 20 20  tted when .     
5cda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cdb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5cdc0 20 20 20 20 20 2a 2a 20 61 63 63 65 73 73 69 6e       ** accessin
5cdd0 67 20 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61  g read-only data
5cde0 62 61 73 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65  bases */.#define
5cdf0 20 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68   SQLITE_IgnoreCh
5ce00 65 63 6b 73 20 20 20 30 78 30 30 30 30 32 30 30  ecks   0x0000200
5ce10 30 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 65 6e 66  0  /* Do not enf
5ce20 6f 72 63 65 20 63 68 65 63 6b 20 63 6f 6e 73 74  orce check const
5ce30 72 61 69 6e 74 73 20 2a 2f 0a 23 64 65 66 69 6e  raints */.#defin
5ce40 65 20 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63  e SQLITE_ReadUnc
5ce50 6f 6d 6d 69 74 74 65 64 20 30 78 30 30 30 30 34  ommitted 0x00004
5ce60 30 30 30 20 2f 2a 20 46 6f 72 20 73 68 61 72 65  000 /* For share
5ce70 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 2a 2f 0a  d-cache mode */.
5ce80 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c  #define SQLITE_L
5ce90 65 67 61 63 79 46 69 6c 65 46 6d 74 20 20 30 78  egacyFileFmt  0x
5cea0 30 30 30 30 38 30 30 30 20 20 2f 2a 20 43 72 65  00008000  /* Cre
5ceb0 61 74 65 20 6e 65 77 20 64 61 74 61 62 61 73 65  ate new database
5cec0 73 20 69 6e 20 66 6f 72 6d 61 74 20 31 20 2a 2f  s in format 1 */
5ced0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5cee0 46 75 6c 6c 46 53 79 6e 63 20 20 20 20 20 20 30  FullFSync      0
5cef0 78 30 30 30 31 30 30 30 30 20 20 2f 2a 20 55 73  x00010000  /* Us
5cf00 65 20 66 75 6c 6c 20 66 73 79 6e 63 20 6f 6e 20  e full fsync on 
5cf10 74 68 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 23  the backend */.#
5cf20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 6f  define SQLITE_Lo
5cf30 61 64 45 78 74 65 6e 73 69 6f 6e 20 20 30 78 30  adExtension  0x0
5cf40 30 30 32 30 30 30 30 20 20 2f 2a 20 45 6e 61 62  0020000  /* Enab
5cf50 6c 65 20 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f  le load_extensio
5cf60 6e 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 53 51  n */..#define SQ
5cf70 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64  LITE_RecoveryMod
5cf80 65 20 20 20 30 78 30 30 30 34 30 30 30 30 20 20  e   0x00040000  
5cf90 2f 2a 20 49 67 6e 6f 72 65 20 73 63 68 65 6d 61  /* Ignore schema
5cfa0 20 65 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66 69   errors */.#defi
5cfb0 6e 65 20 53 51 4c 49 54 45 5f 52 65 76 65 72 73  ne SQLITE_Revers
5cfc0 65 4f 72 64 65 72 20 20 20 30 78 30 30 31 30 30  eOrder   0x00100
5cfd0 30 30 30 20 20 2f 2a 20 52 65 76 65 72 73 65 20  000  /* Reverse 
5cfe0 75 6e 6f 72 64 65 72 65 64 20 53 45 4c 45 43 54  unordered SELECT
5cff0 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s */.#define SQL
5d000 49 54 45 5f 52 65 63 54 72 69 67 67 65 72 73 20  ITE_RecTriggers 
5d010 20 20 20 30 78 30 30 32 30 30 30 30 30 20 20 2f     0x00200000  /
5d020 2a 20 45 6e 61 62 6c 65 20 72 65 63 75 72 73 69  * Enable recursi
5d030 76 65 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 23  ve triggers */.#
5d040 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 6f  define SQLITE_Fo
5d050 72 65 69 67 6e 4b 65 79 73 20 20 20 20 30 78 30  reignKeys    0x0
5d060 30 34 30 30 30 30 30 20 20 2f 2a 20 45 6e 66 6f  0400000  /* Enfo
5d070 72 63 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  rce foreign key 
5d080 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 2a 2f 0a  constraints  */.
5d090 0a 2f 2a 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20  ./*.** Possible 
5d0a0 76 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 73  values for the s
5d0b0 71 6c 69 74 65 2e 6d 61 67 69 63 20 66 69 65 6c  qlite.magic fiel
5d0c0 64 2e 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72  d..** The number
5d0d0 73 20 61 72 65 20 6f 62 74 61 69 6e 65 64 20 61  s are obtained a
5d0e0 74 20 72 61 6e 64 6f 6d 20 61 6e 64 20 68 61 76  t random and hav
5d0f0 65 20 6e 6f 20 73 70 65 63 69 61 6c 20 6d 65 61  e no special mea
5d100 6e 69 6e 67 2c 20 6f 74 68 65 72 0a 2a 2a 20 74  ning, other.** t
5d110 68 61 6e 20 62 65 69 6e 67 20 64 69 73 74 69 6e  han being distin
5d120 63 74 20 66 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74  ct from one anot
5d130 68 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  her..*/.#define 
5d140 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45  SQLITE_MAGIC_OPE
5d150 4e 20 20 20 20 20 30 78 61 30 32 39 61 36 39 37  N     0xa029a697
5d160 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73    /* Database is
5d170 20 6f 70 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65   open */.#define
5d180 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 43 4c   SQLITE_MAGIC_CL
5d190 4f 53 45 44 20 20 20 30 78 39 66 33 63 32 64 33  OSED   0x9f3c2d3
5d1a0 33 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69  3  /* Database i
5d1b0 73 20 63 6c 6f 73 65 64 20 2a 2f 0a 23 64 65 66  s closed */.#def
5d1c0 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  ine SQLITE_MAGIC
5d1d0 5f 53 49 43 4b 20 20 20 20 20 30 78 34 62 37 37  _SICK     0x4b77
5d1e0 31 32 39 30 20 20 2f 2a 20 45 72 72 6f 72 20 61  1290  /* Error a
5d1f0 6e 64 20 61 77 61 69 74 69 6e 67 20 63 6c 6f 73  nd awaiting clos
5d200 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  e */.#define SQL
5d210 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 20  ITE_MAGIC_BUSY  
5d220 20 20 20 30 78 66 30 33 62 37 39 30 36 20 20 2f     0xf03b7906  /
5d230 2a 20 44 61 74 61 62 61 73 65 20 63 75 72 72 65  * Database curre
5d240 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a 23  ntly in use */.#
5d250 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41  define SQLITE_MA
5d260 47 49 43 5f 45 52 52 4f 52 20 20 20 20 30 78 62  GIC_ERROR    0xb
5d270 35 33 35 37 39 33 30 20 20 2f 2a 20 41 6e 20 53  5357930  /* An S
5d280 51 4c 49 54 45 5f 4d 49 53 55 53 45 20 65 72 72  QLITE_MISUSE err
5d290 6f 72 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 0a  or occurred */..
5d2a0 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c 20 66  /*.** Each SQL f
5d2b0 75 6e 63 74 69 6f 6e 20 69 73 20 64 65 66 69 6e  unction is defin
5d2c0 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63  ed by an instanc
5d2d0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
5d2e0 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e  ng.** structure.
5d2f0 20 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74    A pointer to t
5d300 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73  his structure is
5d310 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73   stored in the s
5d320 71 6c 69 74 65 2e 61 46 75 6e 63 0a 2a 2a 20 68  qlite.aFunc.** h
5d330 61 73 68 20 74 61 62 6c 65 2e 20 20 57 68 65 6e  ash table.  When
5d340 20 6d 75 6c 74 69 70 6c 65 20 66 75 6e 63 74 69   multiple functi
5d350 6f 6e 73 20 68 61 76 65 20 74 68 65 20 73 61 6d  ons have the sam
5d360 65 20 6e 61 6d 65 2c 20 74 68 65 20 68 61 73 68  e name, the hash
5d370 20 74 61 62 6c 65 0a 2a 2a 20 70 6f 69 6e 74 73   table.** points
5d380 20 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73   to a linked lis
5d390 74 20 6f 66 20 74 68 65 73 65 20 73 74 72 75 63  t of these struc
5d3a0 74 75 72 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74  tures..*/.struct
5d3b0 20 46 75 6e 63 44 65 66 20 7b 0a 20 20 69 31 36   FuncDef {.  i16
5d3c0 20 6e 41 72 67 3b 20 20 20 20 20 20 20 20 20 20   nArg;          
5d3d0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
5d3e0 72 67 75 6d 65 6e 74 73 2e 20 20 2d 31 20 6d 65  rguments.  -1 me
5d3f0 61 6e 73 20 75 6e 6c 69 6d 69 74 65 64 20 2a 2f  ans unlimited */
5d400 0a 20 20 75 38 20 69 50 72 65 66 45 6e 63 3b 20  .  u8 iPrefEnc; 
5d410 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 66 65          /* Prefe
5d420 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64 69  rred text encodi
5d430 6e 67 20 28 53 51 4c 49 54 45 5f 55 54 46 38 2c  ng (SQLITE_UTF8,
5d440 20 31 36 4c 45 2c 20 31 36 42 45 29 20 2a 2f 0a   16LE, 16BE) */.
5d450 20 20 75 38 20 66 6c 61 67 73 3b 20 20 20 20 20    u8 flags;     
5d460 20 20 20 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63         /* Some c
5d470 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 53 51  ombination of SQ
5d480 4c 49 54 45 5f 46 55 4e 43 5f 2a 20 2a 2f 0a 20  LITE_FUNC_* */. 
5d490 20 76 6f 69 64 20 2a 70 55 73 65 72 44 61 74 61   void *pUserData
5d4a0 3b 20 20 20 20 20 2f 2a 20 55 73 65 72 20 64 61  ;     /* User da
5d4b0 74 61 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a  ta parameter */.
5d4c0 20 20 46 75 6e 63 44 65 66 20 2a 70 4e 65 78 74    FuncDef *pNext
5d4d0 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 66  ;      /* Next f
5d4e0 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 73 61 6d  unction with sam
5d4f0 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 76 6f 69 64  e name */.  void
5d500 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65   (*xFunc)(sqlite
5d510 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73  3_context*,int,s
5d520 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b  qlite3_value**);
5d530 20 2f 2a 20 52 65 67 75 6c 61 72 20 66 75 6e 63   /* Regular func
5d540 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28  tion */.  void (
5d550 2a 78 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f  *xStep)(sqlite3_
5d560 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
5d570 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f  ite3_value**); /
5d580 2a 20 41 67 67 72 65 67 61 74 65 20 73 74 65 70  * Aggregate step
5d590 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46 69   */.  void (*xFi
5d5a0 6e 61 6c 69 7a 65 29 28 73 71 6c 69 74 65 33 5f  nalize)(sqlite3_
5d5b0 63 6f 6e 74 65 78 74 2a 29 3b 20 20 20 20 20 20  context*);      
5d5c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 67 67            /* Agg
5d5d0 72 65 67 61 74 65 20 66 69 6e 61 6c 69 7a 65 72  regate finalizer
5d5e0 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d   */.  char *zNam
5d5f0 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51  e;         /* SQ
5d600 4c 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75  L name of the fu
5d610 6e 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20 46 75 6e  nction. */.  Fun
5d620 63 44 65 66 20 2a 70 48 61 73 68 3b 20 20 20 20  cDef *pHash;    
5d630 20 20 2f 2a 20 4e 65 78 74 20 77 69 74 68 20 61    /* Next with a
5d640 20 64 69 66 66 65 72 65 6e 74 20 6e 61 6d 65 20   different name 
5d650 62 75 74 20 74 68 65 20 73 61 6d 65 20 68 61 73  but the same has
5d660 68 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50  h */.};../*.** P
5d670 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66  ossible values f
5d680 6f 72 20 46 75 6e 63 44 65 66 2e 66 6c 61 67 73  or FuncDef.flags
5d690 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
5d6a0 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 20 20 20  TE_FUNC_LIKE    
5d6b0 20 30 78 30 31 20 2f 2a 20 43 61 6e 64 69 64 61   0x01 /* Candida
5d6c0 74 65 20 66 6f 72 20 74 68 65 20 4c 49 4b 45 20  te for the LIKE 
5d6d0 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 2a 2f 0a  optimization */.
5d6e0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46  #define SQLITE_F
5d6f0 55 4e 43 5f 43 41 53 45 20 20 20 20 20 30 78 30  UNC_CASE     0x0
5d700 32 20 2f 2a 20 43 61 73 65 2d 73 65 6e 73 69 74  2 /* Case-sensit
5d710 69 76 65 20 4c 49 4b 45 2d 74 79 70 65 20 66 75  ive LIKE-type fu
5d720 6e 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e  nction */.#defin
5d730 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50  e SQLITE_FUNC_EP
5d740 48 45 4d 20 20 20 20 30 78 30 34 20 2f 2a 20 45  HEM    0x04 /* E
5d750 70 68 65 6d 65 72 61 6c 2e 20 20 44 65 6c 65 74  phemeral.  Delet
5d760 65 20 77 69 74 68 20 56 44 42 45 20 2a 2f 0a 23  e with VDBE */.#
5d770 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55  define SQLITE_FU
5d780 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 30 78 30 38  NC_NEEDCOLL 0x08
5d790 20 2f 2a 20 73 71 6c 69 74 65 33 47 65 74 46 75   /* sqlite3GetFu
5d7a0 6e 63 43 6f 6c 6c 53 65 71 28 29 20 6d 69 67 68  ncCollSeq() migh
5d7b0 74 20 62 65 20 63 61 6c 6c 65 64 20 2a 2f 0a 23  t be called */.#
5d7c0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55  define SQLITE_FU
5d7d0 4e 43 5f 50 52 49 56 41 54 45 20 20 30 78 31 30  NC_PRIVATE  0x10
5d7e0 20 2f 2a 20 41 6c 6c 6f 77 65 64 20 66 6f 72 20   /* Allowed for 
5d7f0 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c  internal use onl
5d800 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  y */.#define SQL
5d810 49 54 45 5f 46 55 4e 43 5f 43 4f 55 4e 54 20 20  ITE_FUNC_COUNT  
5d820 20 20 30 78 32 30 20 2f 2a 20 42 75 69 6c 74 2d    0x20 /* Built-
5d830 69 6e 20 63 6f 75 6e 74 28 2a 29 20 61 67 67 72  in count(*) aggr
5d840 65 67 61 74 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  egate */../*.** 
5d850 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  The following th
5d860 72 65 65 20 6d 61 63 72 6f 73 2c 20 46 55 4e 43  ree macros, FUNC
5d870 54 49 4f 4e 28 29 2c 20 4c 49 4b 45 46 55 4e 43  TION(), LIKEFUNC
5d880 28 29 20 61 6e 64 20 41 47 47 52 45 47 41 54 45  () and AGGREGATE
5d890 28 29 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 74  () are.** used t
5d8a0 6f 20 63 72 65 61 74 65 20 74 68 65 20 69 6e 69  o create the ini
5d8b0 74 69 61 6c 69 7a 65 72 73 20 66 6f 72 20 74 68  tializers for th
5d8c0 65 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74  e FuncDef struct
5d8d0 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 46 55  ures..**.**   FU
5d8e0 4e 43 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41  NCTION(zName, nA
5d8f0 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78  rg, iArg, bNC, x
5d900 46 75 6e 63 29 0a 2a 2a 20 20 20 20 20 55 73 65  Func).**     Use
5d910 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 73 63  d to create a sc
5d920 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 64 65  alar function de
5d930 66 69 6e 69 74 69 6f 6e 20 6f 66 20 61 20 66 75  finition of a fu
5d940 6e 63 74 69 6f 6e 20 7a 4e 61 6d 65 20 0a 2a 2a  nction zName .**
5d950 20 20 20 20 20 69 6d 70 6c 65 6d 65 6e 74 65 64       implemented
5d960 20 62 79 20 43 20 66 75 6e 63 74 69 6f 6e 20 78   by C function x
5d970 46 75 6e 63 20 74 68 61 74 20 61 63 63 65 70 74  Func that accept
5d980 73 20 6e 41 72 67 20 61 72 67 75 6d 65 6e 74 73  s nArg arguments
5d990 2e 20 54 68 65 0a 2a 2a 20 20 20 20 20 76 61 6c  . The.**     val
5d9a0 75 65 20 70 61 73 73 65 64 20 61 73 20 69 41 72  ue passed as iAr
5d9b0 67 20 69 73 20 63 61 73 74 20 74 6f 20 61 20 28  g is cast to a (
5d9c0 76 6f 69 64 2a 29 20 61 6e 64 20 6d 61 64 65 20  void*) and made 
5d9d0 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 20 20 20  available.**    
5d9e0 20 61 73 20 74 68 65 20 75 73 65 72 2d 64 61 74   as the user-dat
5d9f0 61 20 28 73 71 6c 69 74 65 33 5f 75 73 65 72 5f  a (sqlite3_user_
5da00 64 61 74 61 28 29 29 20 66 6f 72 20 74 68 65 20  data()) for the 
5da10 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 0a 2a 2a  function. If .**
5da20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 20 62 4e       argument bN
5da30 43 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  C is true, then 
5da40 74 68 65 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  the SQLITE_FUNC_
5da50 4e 45 45 44 43 4f 4c 4c 20 66 6c 61 67 20 69 73  NEEDCOLL flag is
5da60 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 41 47   set..**.**   AG
5da70 47 52 45 47 41 54 45 28 7a 4e 61 6d 65 2c 20 6e  GREGATE(zName, n
5da80 41 72 67 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20  Arg, iArg, bNC, 
5da90 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 0a 2a  xStep, xFinal).*
5daa0 2a 20 20 20 20 20 55 73 65 64 20 74 6f 20 63 72  *     Used to cr
5dab0 65 61 74 65 20 61 6e 20 61 67 67 72 65 67 61 74  eate an aggregat
5dac0 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e  e function defin
5dad0 69 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 65  ition implemente
5dae0 64 20 62 79 0a 2a 2a 20 20 20 20 20 74 68 65 20  d by.**     the 
5daf0 43 20 66 75 6e 63 74 69 6f 6e 73 20 78 53 74 65  C functions xSte
5db00 70 20 61 6e 64 20 78 46 69 6e 61 6c 2e 20 54 68  p and xFinal. Th
5db10 65 20 66 69 72 73 74 20 66 6f 75 72 20 70 61 72  e first four par
5db20 61 6d 65 74 65 72 73 0a 2a 2a 20 20 20 20 20 61  ameters.**     a
5db30 72 65 20 69 6e 74 65 72 70 72 65 74 65 64 20 69  re interpreted i
5db40 6e 20 74 68 65 20 73 61 6d 65 20 77 61 79 20 61  n the same way a
5db50 73 20 74 68 65 20 66 69 72 73 74 20 34 20 70 61  s the first 4 pa
5db60 72 61 6d 65 74 65 72 73 20 74 6f 0a 2a 2a 20 20  rameters to.**  
5db70 20 20 20 46 55 4e 43 54 49 4f 4e 28 29 2e 0a 2a     FUNCTION()..*
5db80 2a 0a 2a 2a 20 20 20 4c 49 4b 45 46 55 4e 43 28  *.**   LIKEFUNC(
5db90 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72  zName, nArg, pAr
5dba0 67 2c 20 66 6c 61 67 73 29 0a 2a 2a 20 20 20 20  g, flags).**    
5dbb0 20 55 73 65 64 20 74 6f 20 63 72 65 61 74 65 20   Used to create 
5dbc0 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  a scalar functio
5dbd0 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20  n definition of 
5dbe0 61 20 66 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d 65  a function zName
5dbf0 20 0a 2a 2a 20 20 20 20 20 74 68 61 74 20 61 63   .**     that ac
5dc00 63 65 70 74 73 20 6e 41 72 67 20 61 72 67 75 6d  cepts nArg argum
5dc10 65 6e 74 73 20 61 6e 64 20 69 73 20 69 6d 70 6c  ents and is impl
5dc20 65 6d 65 6e 74 65 64 20 62 79 20 61 20 63 61 6c  emented by a cal
5dc30 6c 20 74 6f 20 43 20 0a 2a 2a 20 20 20 20 20 66  l to C .**     f
5dc40 75 6e 63 74 69 6f 6e 20 6c 69 6b 65 46 75 6e 63  unction likeFunc
5dc50 2e 20 41 72 67 75 6d 65 6e 74 20 70 41 72 67 20  . Argument pArg 
5dc60 69 73 20 63 61 73 74 20 74 6f 20 61 20 28 76 6f  is cast to a (vo
5dc70 69 64 20 2a 29 20 61 6e 64 20 6d 61 64 65 0a 2a  id *) and made.*
5dc80 2a 20 20 20 20 20 61 76 61 69 6c 61 62 6c 65 20  *     available 
5dc90 61 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  as the function 
5dca0 75 73 65 72 2d 64 61 74 61 20 28 73 71 6c 69 74  user-data (sqlit
5dcb0 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 29 2e  e3_user_data()).
5dcc0 20 54 68 65 0a 2a 2a 20 20 20 20 20 46 75 6e 63   The.**     Func
5dcd0 44 65 66 2e 66 6c 61 67 73 20 76 61 72 69 61 62  Def.flags variab
5dce0 6c 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  le is set to the
5dcf0 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61 73   value passed as
5dd00 20 74 68 65 20 66 6c 61 67 73 0a 2a 2a 20 20 20   the flags.**   
5dd10 20 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a    parameter..*/.
5dd20 23 64 65 66 69 6e 65 20 46 55 4e 43 54 49 4f 4e  #define FUNCTION
5dd30 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 69 41  (zName, nArg, iA
5dd40 72 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20  rg, bNC, xFunc) 
5dd50 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54  \.  {nArg, SQLIT
5dd60 45 5f 55 54 46 38 2c 20 62 4e 43 2a 53 51 4c 49  E_UTF8, bNC*SQLI
5dd70 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c  TE_FUNC_NEEDCOLL
5dd80 2c 20 5c 0a 20 20 20 53 51 4c 49 54 45 5f 49 4e  , \.   SQLITE_IN
5dd90 54 5f 54 4f 5f 50 54 52 28 69 41 72 67 29 2c 20  T_TO_PTR(iArg), 
5dda0 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c 20  0, xFunc, 0, 0, 
5ddb0 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69  #zName, 0}.#defi
5ddc0 6e 65 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28  ne STR_FUNCTION(
5ddd0 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 70 41 72  zName, nArg, pAr
5dde0 67 2c 20 62 4e 43 2c 20 78 46 75 6e 63 29 20 5c  g, bNC, xFunc) \
5ddf0 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49 54 45  .  {nArg, SQLITE
5de00 5f 55 54 46 38 2c 20 62 4e 43 2a 53 51 4c 49 54  _UTF8, bNC*SQLIT
5de10 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 2c  E_FUNC_NEEDCOLL,
5de20 20 5c 0a 20 20 20 70 41 72 67 2c 20 30 2c 20 78   \.   pArg, 0, x
5de30 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61  Func, 0, 0, #zNa
5de40 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 4c  me, 0}.#define L
5de50 49 4b 45 46 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e  IKEFUNC(zName, n
5de60 41 72 67 2c 20 61 72 67 2c 20 66 6c 61 67 73 29  Arg, arg, flags)
5de70 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c 49   \.  {nArg, SQLI
5de80 54 45 5f 55 54 46 38 2c 20 66 6c 61 67 73 2c 20  TE_UTF8, flags, 
5de90 28 76 6f 69 64 20 2a 29 61 72 67 2c 20 30 2c 20  (void *)arg, 0, 
5dea0 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30 2c 20  likeFunc, 0, 0, 
5deb0 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69  #zName, 0}.#defi
5dec0 6e 65 20 41 47 47 52 45 47 41 54 45 28 7a 4e 61  ne AGGREGATE(zNa
5ded0 6d 65 2c 20 6e 41 72 67 2c 20 61 72 67 2c 20 6e  me, nArg, arg, n
5dee0 63 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c  c, xStep, xFinal
5def0 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53 51 4c  ) \.  {nArg, SQL
5df00 49 54 45 5f 55 54 46 38 2c 20 6e 63 2a 53 51 4c  ITE_UTF8, nc*SQL
5df10 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43 4f 4c  ITE_FUNC_NEEDCOL
5df20 4c 2c 20 5c 0a 20 20 20 53 51 4c 49 54 45 5f 49  L, \.   SQLITE_I
5df30 4e 54 5f 54 4f 5f 50 54 52 28 61 72 67 29 2c 20  NT_TO_PTR(arg), 
5df40 30 2c 20 30 2c 20 78 53 74 65 70 2c 78 46 69 6e  0, 0, xStep,xFin
5df50 61 6c 2c 23 7a 4e 61 6d 65 2c 30 7d 0a 0a 2f 2a  al,#zName,0}../*
5df60 0a 2a 2a 20 41 6c 6c 20 63 75 72 72 65 6e 74 20  .** All current 
5df70 73 61 76 65 70 6f 69 6e 74 73 20 61 72 65 20 73  savepoints are s
5df80 74 6f 72 65 64 20 69 6e 20 61 20 6c 69 6e 6b 65  tored in a linke
5df90 64 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20  d list starting 
5dfa0 61 74 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 70 53  at.** sqlite3.pS
5dfb0 61 76 65 70 6f 69 6e 74 2e 20 54 68 65 20 66 69  avepoint. The fi
5dfc0 72 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 74  rst element in t
5dfd0 68 65 20 6c 69 73 74 20 69 73 20 74 68 65 20 6d  he list is the m
5dfe0 6f 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20  ost recently.** 
5dff0 6f 70 65 6e 65 64 20 73 61 76 65 70 6f 69 6e 74  opened savepoint
5e000 2e 20 53 61 76 65 70 6f 69 6e 74 73 20 61 72 65  . Savepoints are
5e010 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 6c 69   added to the li
5e020 73 74 20 62 79 20 74 68 65 20 76 64 62 65 0a 2a  st by the vdbe.*
5e030 2a 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69  * OP_Savepoint i
5e040 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  nstruction..*/.s
5e050 74 72 75 63 74 20 53 61 76 65 70 6f 69 6e 74 20  truct Savepoint 
5e060 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  {.  char *zName;
5e070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e080 20 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65 70          /* Savep
5e090 6f 69 6e 74 20 6e 61 6d 65 20 28 6e 75 6c 2d 74  oint name (nul-t
5e0a0 65 72 6d 69 6e 61 74 65 64 29 20 2a 2f 0a 20 20  erminated) */.  
5e0b0 69 36 34 20 6e 44 65 66 65 72 72 65 64 43 6f 6e  i64 nDeferredCon
5e0c0 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
5e0d0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
5e0e0 20 64 65 66 65 72 72 65 64 20 66 6b 20 76 69 6f   deferred fk vio
5e0f0 6c 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 53 61 76  lations */.  Sav
5e100 65 70 6f 69 6e 74 20 2a 70 4e 65 78 74 3b 20 20  epoint *pNext;  
5e110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e120 20 2f 2a 20 50 61 72 65 6e 74 20 73 61 76 65 70   /* Parent savep
5e130 6f 69 6e 74 20 28 69 66 20 61 6e 79 29 20 2a 2f  oint (if any) */
5e140 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  .};../*.** The f
5e150 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 75 73 65  ollowing are use
5e160 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
5e170 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c  parameter to sql
5e180 69 74 65 33 53 61 76 65 70 6f 69 6e 74 28 29 2c  ite3Savepoint(),
5e190 0a 2a 2a 20 61 6e 64 20 61 73 20 74 68 65 20 50  .** and as the P
5e1a0 31 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  1 argument to th
5e1b0 65 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 20 69  e OP_Savepoint i
5e1c0 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 23  nstruction..*/.#
5e1d0 64 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54  define SAVEPOINT
5e1e0 5f 42 45 47 49 4e 20 20 20 20 20 20 30 0a 23 64  _BEGIN      0.#d
5e1f0 65 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f  efine SAVEPOINT_
5e200 52 45 4c 45 41 53 45 20 20 20 20 31 0a 23 64 65  RELEASE    1.#de
5e210 66 69 6e 65 20 53 41 56 45 50 4f 49 4e 54 5f 52  fine SAVEPOINT_R
5e220 4f 4c 4c 42 41 43 4b 20 20 20 32 0a 0a 0a 2f 2a  OLLBACK   2.../*
5e230 0a 2a 2a 20 45 61 63 68 20 53 51 4c 69 74 65 20  .** Each SQLite 
5e240 6d 6f 64 75 6c 65 20 28 76 69 72 74 75 61 6c 20  module (virtual 
5e250 74 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e  table definition
5e260 29 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20  ) is defined by 
5e270 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f  an.** instance o
5e280 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
5e290 73 74 72 75 63 74 75 72 65 2c 20 73 74 6f 72 65  structure, store
5e2a0 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33  d in the sqlite3
5e2b0 2e 61 4d 6f 64 75 6c 65 0a 2a 2a 20 68 61 73 68  .aModule.** hash
5e2c0 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63   table..*/.struc
5e2d0 74 20 4d 6f 64 75 6c 65 20 7b 0a 20 20 63 6f 6e  t Module {.  con
5e2e0 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
5e2f0 65 20 2a 70 4d 6f 64 75 6c 65 3b 20 20 20 20 20  e *pModule;     
5e300 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 70 6f    /* Callback po
5e310 69 6e 74 65 72 73 20 2a 2f 0a 20 20 63 6f 6e 73  inters */.  cons
5e320 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20  t char *zName;  
5e330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e340 20 2f 2a 20 4e 61 6d 65 20 70 61 73 73 65 64 20   /* Name passed 
5e350 74 6f 20 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  to create_module
5e360 28 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  () */.  void *pA
5e370 75 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ux;             
5e380 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
5e390 70 41 75 78 20 70 61 73 73 65 64 20 74 6f 20 63  pAux passed to c
5e3a0 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a  reate_module() *
5e3b0 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 73 74  /.  void (*xDest
5e3c0 72 6f 79 29 28 76 6f 69 64 20 2a 29 3b 20 20 20  roy)(void *);   
5e3d0 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75           /* Modu
5e3e0 6c 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75  le destructor fu
5e3f0 6e 63 74 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  nction */.};../*
5e400 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  .** information 
5e410 61 62 6f 75 74 20 65 61 63 68 20 63 6f 6c 75 6d  about each colum
5e420 6e 20 6f 66 20 61 6e 20 53 51 4c 20 74 61 62 6c  n of an SQL tabl
5e430 65 20 69 73 20 68 65 6c 64 20 69 6e 20 61 6e 20  e is held in an 
5e440 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74  instance.** of t
5e450 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  his structure..*
5e460 2f 0a 73 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20  /.struct Column 
5e470 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  {.  char *zName;
5e480 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
5e490 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20  this column */. 
5e4a0 20 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 20 20   Expr *pDflt;   
5e4b0 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 76 61 6c    /* Default val
5e4c0 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d  ue of this colum
5e4d0 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 44 66  n */.  char *zDf
5e4e0 6c 74 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67 69  lt;     /* Origi
5e4f0 6e 61 6c 20 74 65 78 74 20 6f 66 20 74 68 65 20  nal text of the 
5e500 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 2a 2f  default value */
5e510 0a 20 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20  .  char *zType; 
5e520 20 20 20 20 2f 2a 20 44 61 74 61 20 74 79 70 65      /* Data type
5e530 20 66 6f 72 20 74 68 69 73 20 63 6f 6c 75 6d 6e   for this column
5e540 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c   */.  char *zCol
5e550 6c 3b 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74  l;     /* Collat
5e560 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 20 20 49  ing sequence.  I
5e570 66 20 4e 55 4c 4c 2c 20 75 73 65 20 74 68 65 20  f NULL, use the 
5e580 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38 20  default */.  u8 
5e590 6e 6f 74 4e 75 6c 6c 3b 20 20 20 20 20 20 2f 2a  notNull;      /*
5e5a0 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20 69   True if there i
5e5b0 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e  s a NOT NULL con
5e5c0 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 75 38 20  straint */.  u8 
5e5d0 69 73 50 72 69 6d 4b 65 79 3b 20 20 20 20 2f 2a  isPrimKey;    /*
5e5e0 20 54 72 75 65 20 69 66 20 74 68 69 73 20 63 6f   True if this co
5e5f0 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 20  lumn is part of 
5e600 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20  the PRIMARY KEY 
5e610 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e 69  */.  char affini
5e620 74 79 3b 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20  ty;   /* One of 
5e630 74 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 2e  the SQLITE_AFF_.
5e640 2e 2e 20 76 61 6c 75 65 73 20 2a 2f 0a 23 69 66  .. values */.#if
5e650 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
5e660 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
5e670 75 38 20 69 73 48 69 64 64 65 6e 3b 20 20 20 20  u8 isHidden;    
5e680 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73   /* True if this
5e690 20 63 6f 6c 75 6d 6e 20 69 73 20 27 68 69 64 64   column is 'hidd
5e6a0 65 6e 27 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b  en' */.#endif.};
5e6b0 0a 0a 2f 2a 0a 2a 2a 20 41 20 22 43 6f 6c 6c 61  ../*.** A "Colla
5e6c0 74 69 6e 67 20 53 65 71 75 65 6e 63 65 22 20 69  ting Sequence" i
5e6d0 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e 20  s defined by an 
5e6e0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
5e6f0 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72  following.** str
5e700 75 63 74 75 72 65 2e 20 43 6f 6e 63 65 70 74 75  ucture. Conceptu
5e710 61 6c 6c 79 2c 20 61 20 63 6f 6c 6c 61 74 69 6e  ally, a collatin
5e720 67 20 73 65 71 75 65 6e 63 65 20 63 6f 6e 73 69  g sequence consi
5e730 73 74 73 20 6f 66 20 61 20 6e 61 6d 65 20 61 6e  sts of a name an
5e740 64 0a 2a 2a 20 61 20 63 6f 6d 70 61 72 69 73 6f  d.** a compariso
5e750 6e 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20 64  n routine that d
5e760 65 66 69 6e 65 73 20 74 68 65 20 6f 72 64 65 72  efines the order
5e770 20 6f 66 20 74 68 61 74 20 73 65 71 75 65 6e 63   of that sequenc
5e780 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d  e..**.** There m
5e790 61 79 20 74 77 6f 20 73 65 70 61 72 61 74 65 20  ay two separate 
5e7a0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
5e7b0 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  of the collation
5e7c0 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 6e 65 0a 2a   function, one.*
5e7d0 2a 20 74 68 61 74 20 70 72 6f 63 65 73 73 65 73  * that processes
5e7e0 20 74 65 78 74 20 69 6e 20 55 54 46 2d 38 20 65   text in UTF-8 e
5e7f0 6e 63 6f 64 69 6e 67 20 28 43 6f 6c 6c 53 65 71  ncoding (CollSeq
5e800 2e 78 43 6d 70 29 20 61 6e 64 20 61 6e 6f 74 68  .xCmp) and anoth
5e810 65 72 20 74 68 61 74 0a 2a 2a 20 70 72 6f 63 65  er that.** proce
5e820 73 73 65 73 20 74 65 78 74 20 65 6e 63 6f 64 65  sses text encode
5e830 64 20 69 6e 20 55 54 46 2d 31 36 20 28 43 6f 6c  d in UTF-16 (Col
5e840 6c 53 65 71 2e 78 43 6d 70 31 36 29 2c 20 75 73  lSeq.xCmp16), us
5e850 69 6e 67 20 74 68 65 20 6d 61 63 68 69 6e 65 0a  ing the machine.
5e860 2a 2a 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f  ** native byte o
5e870 72 64 65 72 2e 20 57 68 65 6e 20 61 20 63 6f 6c  rder. When a col
5e880 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
5e890 69 73 20 69 6e 76 6f 6b 65 64 2c 20 53 51 4c 69  is invoked, SQLi
5e8a0 74 65 20 73 65 6c 65 63 74 73 0a 2a 2a 20 74 68  te selects.** th
5e8b0 65 20 76 65 72 73 69 6f 6e 20 74 68 61 74 20 77  e version that w
5e8c0 69 6c 6c 20 72 65 71 75 69 72 65 20 74 68 65 20  ill require the 
5e8d0 6c 65 61 73 74 20 65 78 70 65 6e 73 69 76 65 20  least expensive 
5e8e0 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 72 61 6e  encoding.** tran
5e8f0 73 6c 61 74 69 6f 6e 73 2c 20 69 66 20 61 6e 79  slations, if any
5e900 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 6f 6c 6c  ..**.** The Coll
5e910 53 65 71 2e 70 55 73 65 72 20 6d 65 6d 62 65 72  Seq.pUser member
5e920 20 76 61 72 69 61 62 6c 65 20 69 73 20 61 6e 20   variable is an 
5e930 65 78 74 72 61 20 70 61 72 61 6d 65 74 65 72 20  extra parameter 
5e940 74 68 61 74 20 70 61 73 73 65 64 20 69 6e 0a 2a  that passed in.*
5e950 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61  * as the first a
5e960 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 55  rgument to the U
5e970 54 46 2d 38 20 63 6f 6d 70 61 72 69 73 6f 6e 20  TF-8 comparison 
5e980 66 75 6e 63 74 69 6f 6e 2c 20 78 43 6d 70 2e 0a  function, xCmp..
5e990 2a 2a 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65 72  ** CollSeq.pUser
5e9a0 31 36 20 69 73 20 74 68 65 20 65 71 75 69 76 61  16 is the equiva
5e9b0 6c 65 6e 74 20 66 6f 72 20 74 68 65 20 55 54 46  lent for the UTF
5e9c0 2d 31 36 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66  -16 comparison f
5e9d0 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 78 43 6d 70  unction,.** xCmp
5e9e0 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f 74  16..**.** If bot
5e9f0 68 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 20 61  h CollSeq.xCmp a
5ea00 6e 64 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 31  nd CollSeq.xCmp1
5ea10 36 20 61 72 65 20 4e 55 4c 4c 2c 20 69 74 20 6d  6 are NULL, it m
5ea20 65 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a  eans that the.**
5ea30 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
5ea40 6e 63 65 20 69 73 20 75 6e 64 65 66 69 6e 65 64  nce is undefined
5ea50 2e 20 20 49 6e 64 69 63 65 73 20 62 75 69 6c 74  .  Indices built
5ea60 20 6f 6e 20 61 6e 20 75 6e 64 65 66 69 6e 65 64   on an undefined
5ea70 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  .** collating se
5ea80 71 75 65 6e 63 65 20 6d 61 79 20 6e 6f 74 20 62  quence may not b
5ea90 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65  e read or writte
5eaa0 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f 6c  n..*/.struct Col
5eab0 6c 53 65 71 20 7b 0a 20 20 63 68 61 72 20 2a 7a  lSeq {.  char *z
5eac0 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 2f  Name;          /
5ead0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f  * Name of the co
5eae0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
5eaf0 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  , UTF-8 encoded 
5eb00 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20  */.  u8 enc;    
5eb10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65             /* Te
5eb20 78 74 20 65 6e 63 6f 64 69 6e 67 20 68 61 6e 64  xt encoding hand
5eb30 6c 65 64 20 62 79 20 78 43 6d 70 28 29 20 2a 2f  led by xCmp() */
5eb40 0a 20 20 75 38 20 74 79 70 65 3b 20 20 20 20 20  .  u8 type;     
5eb50 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
5eb60 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43 4f  of the SQLITE_CO
5eb70 4c 4c 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 62 65  LL_... values be
5eb80 6c 6f 77 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  low */.  void *p
5eb90 55 73 65 72 3b 20 20 20 20 20 20 20 20 20 20 2f  User;          /
5eba0 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74  * First argument
5ebb0 20 74 6f 20 78 43 6d 70 28 29 20 2a 2f 0a 20 20   to xCmp() */.  
5ebc0 69 6e 74 20 28 2a 78 43 6d 70 29 28 76 6f 69 64  int (*xCmp)(void
5ebd0 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69  *,int, const voi
5ebe0 64 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76  d*, int, const v
5ebf0 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 28 2a  oid*);.  void (*
5ec00 78 44 65 6c 29 28 76 6f 69 64 2a 29 3b 20 20 2f  xDel)(void*);  /
5ec10 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72  * Destructor for
5ec20 20 70 55 73 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   pUser */.};../*
5ec30 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75  .** Allowed valu
5ec40 65 73 20 6f 66 20 43 6f 6c 6c 53 65 71 2e 74 79  es of CollSeq.ty
5ec50 70 65 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  pe:.*/.#define S
5ec60 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52  QLITE_COLL_BINAR
5ec70 59 20 20 31 20 20 2f 2a 20 54 68 65 20 64 65 66  Y  1  /* The def
5ec80 61 75 6c 74 20 6d 65 6d 63 6d 70 28 29 20 63 6f  ault memcmp() co
5ec90 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
5eca0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
5ecb0 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 20 20  TE_COLL_NOCASE  
5ecc0 32 20 20 2f 2a 20 54 68 65 20 62 75 69 6c 74 2d  2  /* The built-
5ecd0 69 6e 20 4e 4f 43 41 53 45 20 63 6f 6c 6c 61 74  in NOCASE collat
5ece0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a  ing sequence */.
5ecf0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
5ed00 4f 4c 4c 5f 52 45 56 45 52 53 45 20 33 20 20 2f  OLL_REVERSE 3  /
5ed10 2a 20 54 68 65 20 62 75 69 6c 74 2d 69 6e 20 52  * The built-in R
5ed20 45 56 45 52 53 45 20 63 6f 6c 6c 61 74 69 6e 67  EVERSE collating
5ed30 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64 65   sequence */.#de
5ed40 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c 4c  fine SQLITE_COLL
5ed50 5f 55 53 45 52 20 20 20 20 30 20 20 2f 2a 20 41  _USER    0  /* A
5ed60 6e 79 20 6f 74 68 65 72 20 75 73 65 72 2d 64 65  ny other user-de
5ed70 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6e 67 20  fined collating 
5ed80 73 65 71 75 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a  sequence */../*.
5ed90 2a 2a 20 41 20 73 6f 72 74 20 6f 72 64 65 72 20  ** A sort order 
5eda0 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 41 53  can be either AS
5edb0 43 20 6f 72 20 44 45 53 43 2e 0a 2a 2f 0a 23 64  C or DESC..*/.#d
5edc0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f 5f  efine SQLITE_SO_
5edd0 41 53 43 20 20 20 20 20 20 20 30 20 20 2f 2a 20  ASC       0  /* 
5ede0 53 6f 72 74 20 69 6e 20 61 73 63 65 6e 64 69 6e  Sort in ascendin
5edf0 67 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69  g order */.#defi
5ee00 6e 65 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45 53  ne SQLITE_SO_DES
5ee10 43 20 20 20 20 20 20 31 20 20 2f 2a 20 53 6f 72  C      1  /* Sor
5ee20 74 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f  t in ascending o
5ee30 72 64 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43  rder */../*.** C
5ee40 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74  olumn affinity t
5ee50 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  ypes..**.** Thes
5ee60 65 20 75 73 65 64 20 74 6f 20 68 61 76 65 20 6d  e used to have m
5ee70 6e 65 6d 6f 6e 69 63 20 6e 61 6d 65 20 6c 69 6b  nemonic name lik
5ee80 65 20 27 69 27 20 66 6f 72 20 53 51 4c 49 54 45  e 'i' for SQLITE
5ee90 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 61 6e 64  _AFF_INTEGER and
5eea0 0a 2a 2a 20 27 74 27 20 66 6f 72 20 53 51 4c 49  .** 't' for SQLI
5eeb0 54 45 5f 41 46 46 5f 54 45 58 54 2e 20 20 42 75  TE_AFF_TEXT.  Bu
5eec0 74 20 77 65 20 63 61 6e 20 73 61 76 65 20 61 20  t we can save a 
5eed0 6c 69 74 74 6c 65 20 73 70 61 63 65 20 61 6e 64  little space and
5eee0 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 74 68 65 20   improve.** the 
5eef0 73 70 65 65 64 20 61 20 6c 69 74 74 6c 65 20 62  speed a little b
5ef00 79 20 6e 75 6d 62 65 72 69 6e 67 20 74 68 65 20  y numbering the 
5ef10 76 61 6c 75 65 73 20 63 6f 6e 73 65 63 75 74 69  values consecuti
5ef20 76 65 6c 79 2e 20 20 0a 2a 2a 0a 2a 2a 20 42 75  vely.  .**.** Bu
5ef30 74 20 72 61 74 68 65 72 20 74 68 61 6e 20 73 74  t rather than st
5ef40 61 72 74 20 77 69 74 68 20 30 20 6f 72 20 31 2c  art with 0 or 1,
5ef50 20 77 65 20 62 65 67 69 6e 20 77 69 74 68 20 27   we begin with '
5ef60 61 27 2e 20 20 54 68 61 74 20 77 61 79 2c 0a 2a  a'.  That way,.*
5ef70 2a 20 77 68 65 6e 20 6d 75 6c 74 69 70 6c 65 20  * when multiple 
5ef80 61 66 66 69 6e 69 74 79 20 74 79 70 65 73 20 61  affinity types a
5ef90 72 65 20 63 6f 6e 63 61 74 65 6e 61 74 65 64 20  re concatenated 
5efa0 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 61 6e  into a string an
5efb0 64 0a 2a 2a 20 75 73 65 64 20 61 73 20 74 68 65  d.** used as the
5efc0 20 50 34 20 6f 70 65 72 61 6e 64 2c 20 74 68 65   P4 operand, the
5efd0 79 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20 72  y will be more r
5efe0 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4e  eadable..**.** N
5eff0 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20 74 68  ote also that th
5f000 65 20 6e 75 6d 65 72 69 63 20 74 79 70 65 73 20  e numeric types 
5f010 61 72 65 20 67 72 6f 75 70 65 64 20 74 6f 67 65  are grouped toge
5f020 74 68 65 72 20 73 6f 20 74 68 61 74 20 74 65 73  ther so that tes
5f030 74 69 6e 67 0a 2a 2a 20 66 6f 72 20 61 20 6e 75  ting.** for a nu
5f040 6d 65 72 69 63 20 74 79 70 65 20 69 73 20 61 20  meric type is a 
5f050 73 69 6e 67 6c 65 20 63 6f 6d 70 61 72 69 73 6f  single compariso
5f060 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  n..*/.#define SQ
5f070 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 20 20  LITE_AFF_TEXT   
5f080 20 20 27 61 27 0a 23 64 65 66 69 6e 65 20 53 51    'a'.#define SQ
5f090 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20 20  LITE_AFF_NONE   
5f0a0 20 20 27 62 27 0a 23 64 65 66 69 6e 65 20 53 51    'b'.#define SQ
5f0b0 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
5f0c0 20 20 27 63 27 0a 23 64 65 66 69 6e 65 20 53 51    'c'.#define SQ
5f0d0 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
5f0e0 20 20 27 64 27 0a 23 64 65 66 69 6e 65 20 53 51    'd'.#define SQ
5f0f0 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 20 20  LITE_AFF_REAL   
5f100 20 20 27 65 27 0a 0a 23 64 65 66 69 6e 65 20 73    'e'..#define s
5f110 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63 41  qlite3IsNumericA
5f120 66 66 69 6e 69 74 79 28 58 29 20 20 28 28 58 29  ffinity(X)  ((X)
5f130 3e 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  >=SQLITE_AFF_NUM
5f140 45 52 49 43 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ERIC)../*.** The
5f150 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b   SQLITE_AFF_MASK
5f160 20 76 61 6c 75 65 73 20 6d 61 73 6b 73 20 6f 66   values masks of
5f170 66 20 74 68 65 20 73 69 67 6e 69 66 69 63 61 6e  f the significan
5f180 74 20 62 69 74 73 20 6f 66 20 61 6e 0a 2a 2a 20  t bits of an.** 
5f190 61 66 66 69 6e 69 74 79 20 76 61 6c 75 65 2e 20  affinity value. 
5f1a0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
5f1b0 54 45 5f 41 46 46 5f 4d 41 53 4b 20 20 20 20 20  TE_AFF_MASK     
5f1c0 30 78 36 37 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 69  0x67../*.** Addi
5f1d0 74 69 6f 6e 61 6c 20 62 69 74 20 76 61 6c 75 65  tional bit value
5f1e0 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 4f 52  s that can be OR
5f1f0 65 64 20 77 69 74 68 20 61 6e 20 61 66 66 69 6e  ed with an affin
5f200 69 74 79 20 77 69 74 68 6f 75 74 0a 2a 2a 20 63  ity without.** c
5f210 68 61 6e 67 69 6e 67 20 74 68 65 20 61 66 66 69  hanging the affi
5f220 6e 69 74 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  nity..*/.#define
5f230 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55   SQLITE_JUMPIFNU
5f240 4c 4c 20 20 20 30 78 30 38 20 20 2f 2a 20 6a 75  LL   0x08  /* ju
5f250 6d 70 73 20 69 66 20 65 69 74 68 65 72 20 6f 70  mps if either op
5f260 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a 2f  erand is NULL */
5f270 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
5f280 53 54 4f 52 45 50 32 20 20 20 20 20 20 30 78 31  STOREP2      0x1
5f290 30 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75  0  /* Store resu
5f2a0 6c 74 20 69 6e 20 72 65 67 5b 50 32 5d 20 72 61  lt in reg[P2] ra
5f2b0 74 68 65 72 20 74 68 61 6e 20 6a 75 6d 70 20 2a  ther than jump *
5f2c0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
5f2d0 5f 4e 55 4c 4c 45 51 20 20 20 20 20 20 20 30 78  _NULLEQ       0x
5f2e0 38 30 20 20 2f 2a 20 4e 55 4c 4c 3d 4e 55 4c 4c  80  /* NULL=NULL
5f2f0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f 62   */../*.** An ob
5f300 6a 65 63 74 20 6f 66 20 74 68 69 73 20 74 79 70  ject of this typ
5f310 65 20 69 73 20 63 72 65 61 74 65 64 20 66 6f 72  e is created for
5f320 20 65 61 63 68 20 76 69 72 74 75 61 6c 20 74 61   each virtual ta
5f330 62 6c 65 20 70 72 65 73 65 6e 74 20 69 6e 0a 2a  ble present in.*
5f340 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  * the database s
5f350 63 68 65 6d 61 2e 20 0a 2a 2a 0a 2a 2a 20 49 66  chema. .**.** If
5f360 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
5f370 68 65 6d 61 20 69 73 20 73 68 61 72 65 64 2c 20  hema is shared, 
5f380 74 68 65 6e 20 74 68 65 72 65 20 69 73 20 6f 6e  then there is on
5f390 65 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  e instance of th
5f3a0 69 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20  is.** structure 
5f3b0 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61 73  for each databas
5f3c0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 28 73 71  e connection (sq
5f3d0 6c 69 74 65 33 2a 29 20 74 68 61 74 20 75 73 65  lite3*) that use
5f3e0 73 20 74 68 65 20 73 68 61 72 65 64 0a 2a 2a 20  s the shared.** 
5f3f0 73 63 68 65 6d 61 2e 20 54 68 69 73 20 69 73 20  schema. This is 
5f400 62 65 63 61 75 73 65 20 65 61 63 68 20 64 61 74  because each dat
5f410 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
5f420 20 72 65 71 75 69 72 65 73 20 69 74 73 20 6f 77   requires its ow
5f430 6e 20 75 6e 69 71 75 65 0a 2a 2a 20 69 6e 73 74  n unique.** inst
5f440 61 6e 63 65 20 6f 66 20 74 68 65 20 73 71 6c 69  ance of the sqli
5f450 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c 65  te3_vtab* handle
5f460 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20   used to access 
5f470 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
5f480 65 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  e .** implementa
5f490 74 69 6f 6e 2e 20 73 71 6c 69 74 65 33 5f 76 74  tion. sqlite3_vt
5f4a0 61 62 2a 20 68 61 6e 64 6c 65 73 20 63 61 6e 20  ab* handles can 
5f4b0 6e 6f 74 20 62 65 20 73 68 61 72 65 64 20 62 65  not be shared be
5f4c0 74 77 65 65 6e 20 0a 2a 2a 20 64 61 74 61 62 61  tween .** databa
5f4d0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 20  se connections, 
5f4e0 65 76 65 6e 20 77 68 65 6e 20 74 68 65 20 72 65  even when the re
5f4f0 73 74 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d  st of the in-mem
5f500 6f 72 79 20 64 61 74 61 62 61 73 65 20 0a 2a 2a  ory database .**
5f510 20 73 63 68 65 6d 61 20 69 73 20 73 68 61 72 65   schema is share
5f520 64 2c 20 61 73 20 74 68 65 20 69 6d 70 6c 65 6d  d, as the implem
5f530 65 6e 74 61 74 69 6f 6e 20 6f 66 74 65 6e 20 73  entation often s
5f540 74 6f 72 65 73 20 74 68 65 20 64 61 74 61 62 61  tores the databa
5f550 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  se.** connection
5f560 20 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20 74   handle passed t
5f570 6f 20 69 74 20 76 69 61 20 74 68 65 20 78 43 6f  o it via the xCo
5f580 6e 6e 65 63 74 28 29 20 6f 72 20 78 43 72 65 61  nnect() or xCrea
5f590 74 65 28 29 20 6d 65 74 68 6f 64 0a 2a 2a 20 64  te() method.** d
5f5a0 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61  uring initializa
5f5b0 74 69 6f 6e 20 69 6e 74 65 72 6e 61 6c 6c 79 2e  tion internally.
5f5c0 20 54 68 69 73 20 64 61 74 61 62 61 73 65 20 63   This database c
5f5d0 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65  onnection handle
5f5e0 20 6d 61 79 0a 2a 2a 20 74 68 65 6e 20 75 73 65   may.** then use
5f5f0 64 20 62 79 20 74 68 65 20 76 69 72 74 75 61 6c  d by the virtual
5f600 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74   table implement
5f610 61 74 69 6f 6e 20 74 6f 20 61 63 63 65 73 73 20  ation to access 
5f620 72 65 61 6c 20 74 61 62 6c 65 73 20 0a 2a 2a 20  real tables .** 
5f630 77 69 74 68 69 6e 20 74 68 65 20 64 61 74 61 62  within the datab
5f640 61 73 65 2e 20 53 6f 20 74 68 61 74 20 74 68 65  ase. So that the
5f650 79 20 61 70 70 65 61 72 20 61 73 20 70 61 72 74  y appear as part
5f660 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 73 20   of the callers 
5f670 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  .** transaction,
5f680 20 74 68 65 73 65 20 61 63 63 65 73 73 65 73 20   these accesses 
5f690 6e 65 65 64 20 74 6f 20 62 65 20 6d 61 64 65 20  need to be made 
5f6a0 76 69 61 20 74 68 65 20 73 61 6d 65 20 64 61 74  via the same dat
5f6b0 61 62 61 73 65 20 0a 2a 2a 20 63 6f 6e 6e 65 63  abase .** connec
5f6c0 74 69 6f 6e 20 61 73 20 74 68 61 74 20 75 73 65  tion as that use
5f6d0 64 20 74 6f 20 65 78 65 63 75 74 65 20 53 51 4c  d to execute SQL
5f6e0 20 6f 70 65 72 61 74 69 6f 6e 73 20 6f 6e 20 74   operations on t
5f6f0 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
5f700 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 56 54 61 62  ..**.** All VTab
5f710 6c 65 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20  le objects that 
5f720 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20 61 20  correspond to a 
5f730 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 20  single table in 
5f740 61 20 73 68 61 72 65 64 0a 2a 2a 20 64 61 74 61  a shared.** data
5f750 62 61 73 65 20 73 63 68 65 6d 61 20 61 72 65 20  base schema are 
5f760 69 6e 69 74 69 61 6c 6c 79 20 73 74 6f 72 65 64  initially stored
5f770 20 69 6e 20 61 20 6c 69 6e 6b 65 64 2d 6c 69 73   in a linked-lis
5f780 74 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 0a  t pointed to by.
5f790 2a 2a 20 74 68 65 20 54 61 62 6c 65 2e 70 56 54  ** the Table.pVT
5f7a0 61 62 6c 65 20 6d 65 6d 62 65 72 20 76 61 72 69  able member vari
5f7b0 61 62 6c 65 20 6f 66 20 74 68 65 20 63 6f 72 72  able of the corr
5f7c0 65 73 70 6f 6e 64 69 6e 67 20 54 61 62 6c 65 20  esponding Table 
5f7d0 6f 62 6a 65 63 74 2e 0a 2a 2a 20 57 68 65 6e 20  object..** When 
5f7e0 61 6e 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  an sqlite3_prepa
5f7f0 72 65 28 29 20 6f 70 65 72 61 74 69 6f 6e 20 69  re() operation i
5f800 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 61 63  s required to ac
5f810 63 65 73 73 20 74 68 65 20 76 69 72 74 75 61 6c  cess the virtual
5f820 0a 2a 2a 20 74 61 62 6c 65 2c 20 69 74 20 73 65  .** table, it se
5f830 61 72 63 68 65 73 20 74 68 65 20 6c 69 73 74 20  arches the list 
5f840 66 6f 72 20 74 68 65 20 56 54 61 62 6c 65 20 74  for the VTable t
5f850 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  hat corresponds 
5f860 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  to the.** databa
5f870 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 6f  se connection do
5f880 69 6e 67 20 74 68 65 20 70 72 65 70 61 72 69 6e  ing the preparin
5f890 67 20 73 6f 20 61 73 20 74 6f 20 75 73 65 20 74  g so as to use t
5f8a0 68 65 20 63 6f 72 72 65 63 74 0a 2a 2a 20 73 71  he correct.** sq
5f8b0 6c 69 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64  lite3_vtab* hand
5f8c0 6c 65 20 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c  le in the compil
5f8d0 65 64 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20  ed query..**.** 
5f8e0 57 68 65 6e 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  When an in-memor
5f8f0 79 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 20 69  y Table object i
5f900 73 20 64 65 6c 65 74 65 64 20 28 66 6f 72 20 65  s deleted (for e
5f910 78 61 6d 70 6c 65 20 77 68 65 6e 20 74 68 65 0a  xample when the.
5f920 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 62 65 69  ** schema is bei
5f930 6e 67 20 72 65 6c 6f 61 64 65 64 20 66 6f 72 20  ng reloaded for 
5f940 73 6f 6d 65 20 72 65 61 73 6f 6e 29 2c 20 74 68  some reason), th
5f950 65 20 56 54 61 62 6c 65 20 6f 62 6a 65 63 74 73  e VTable objects
5f960 20 61 72 65 20 6e 6f 74 20 0a 2a 2a 20 64 65 6c   are not .** del
5f970 65 74 65 64 20 61 6e 64 20 74 68 65 20 73 71 6c  eted and the sql
5f980 69 74 65 33 5f 76 74 61 62 2a 20 68 61 6e 64 6c  ite3_vtab* handl
5f990 65 73 20 61 72 65 20 6e 6f 74 20 78 44 69 73 63  es are not xDisc
5f9a0 6f 6e 6e 65 63 74 28 29 65 64 20 0a 2a 2a 20 69  onnect()ed .** i
5f9b0 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 6e 73 74  mmediately. Inst
5f9c0 65 61 64 2c 20 74 68 65 79 20 61 72 65 20 6d 6f  ead, they are mo
5f9d0 76 65 64 20 66 72 6f 6d 20 74 68 65 20 54 61 62  ved from the Tab
5f9e0 6c 65 2e 70 56 54 61 62 6c 65 20 6c 69 73 74 20  le.pVTable list 
5f9f0 74 6f 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 6c 69  to.** another li
5fa00 6e 6b 65 64 20 6c 69 73 74 20 68 65 61 64 65 64  nked list headed
5fa10 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 2e   by the sqlite3.
5fa20 70 44 69 73 63 6f 6e 6e 65 63 74 20 6d 65 6d 62  pDisconnect memb
5fa30 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 72  er of the.** cor
5fa40 72 65 73 70 6f 6e 64 69 6e 67 20 73 71 6c 69 74  responding sqlit
5fa50 65 33 20 73 74 72 75 63 74 75 72 65 2e 20 54 68  e3 structure. Th
5fa60 65 79 20 61 72 65 20 74 68 65 6e 20 64 65 6c 65  ey are then dele
5fa70 74 65 64 2f 78 44 69 73 63 6f 6e 6e 65 63 74 65  ted/xDisconnecte
5fa80 64 20 0a 2a 2a 20 6e 65 78 74 20 74 69 6d 65 20  d .** next time 
5fa90 61 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 70  a statement is p
5faa0 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 73 61  repared using sa
5fab0 69 64 20 73 71 6c 69 74 65 33 2a 2e 20 54 68 69  id sqlite3*. Thi
5fac0 73 20 69 73 20 64 6f 6e 65 0a 2a 2a 20 74 6f 20  s is done.** to 
5fad0 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 20 69  avoid deadlock i
5fae0 73 73 75 65 73 20 69 6e 76 6f 6c 76 69 6e 67 20  ssues involving 
5faf0 6d 75 6c 74 69 70 6c 65 20 73 71 6c 69 74 65 33  multiple sqlite3
5fb00 2e 6d 75 74 65 78 20 6d 75 74 65 78 65 73 2e 0a  .mutex mutexes..
5fb10 2a 2a 20 52 65 66 65 72 20 74 6f 20 63 6f 6d 6d  ** Refer to comm
5fb20 65 6e 74 73 20 61 62 6f 76 65 20 66 75 6e 63 74  ents above funct
5fb30 69 6f 6e 20 73 71 6c 69 74 65 33 56 74 61 62 55  ion sqlite3VtabU
5fb40 6e 6c 6f 63 6b 4c 69 73 74 28 29 20 66 6f 72 20  nlockList() for 
5fb50 61 6e 0a 2a 2a 20 65 78 70 6c 61 6e 61 74 69 6f  an.** explanatio
5fb60 6e 20 61 73 20 74 6f 20 77 68 79 20 69 74 20 69  n as to why it i
5fb70 73 20 73 61 66 65 20 74 6f 20 61 64 64 20 61 6e  s safe to add an
5fb80 20 65 6e 74 72 79 20 74 6f 20 61 6e 20 73 71 6c   entry to an sql
5fb90 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74  ite3.pDisconnect
5fba0 0a 2a 2a 20 6c 69 73 74 20 77 69 74 68 6f 75 74  .** list without
5fbb0 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 63 6f 72   holding the cor
5fbc0 72 65 73 70 6f 6e 64 69 6e 67 20 73 71 6c 69 74  responding sqlit
5fbd0 65 33 2e 6d 75 74 65 78 20 6d 75 74 65 78 2e 0a  e3.mutex mutex..
5fbe0 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79  **.** The memory
5fbf0 20 66 6f 72 20 6f 62 6a 65 63 74 73 20 6f 66 20   for objects of 
5fc00 74 68 69 73 20 74 79 70 65 20 69 73 20 61 6c 77  this type is alw
5fc10 61 79 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79  ays allocated by
5fc20 20 0a 2a 2a 20 73 71 6c 69 74 65 33 44 62 4d 61   .** sqlite3DbMa
5fc30 6c 6c 6f 63 28 29 2c 20 75 73 69 6e 67 20 74 68  lloc(), using th
5fc40 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e  e connection han
5fc50 64 6c 65 20 73 74 6f 72 65 64 20 69 6e 20 56 54  dle stored in VT
5fc60 61 62 6c 65 2e 64 62 20 61 73 20 0a 2a 2a 20 74  able.db as .** t
5fc70 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
5fc80 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56 54 61  t..*/.struct VTa
5fc90 62 6c 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 20  ble {.  sqlite3 
5fca0 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  *db;            
5fcb0 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
5fcc0 6e 6e 65 63 74 69 6f 6e 20 61 73 73 6f 63 69 61  nnection associa
5fcd0 74 65 64 20 77 69 74 68 20 74 68 69 73 20 74 61  ted with this ta
5fce0 62 6c 65 20 2a 2f 0a 20 20 4d 6f 64 75 6c 65 20  ble */.  Module 
5fcf0 2a 70 4d 6f 64 3b 20 20 20 20 20 20 20 20 20 20  *pMod;          
5fd00 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
5fd10 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
5fd20 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 71 6c 69  tation */.  sqli
5fd30 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b  te3_vtab *pVtab;
5fd40 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
5fd50 20 74 6f 20 76 74 61 62 20 69 6e 73 74 61 6e 63   to vtab instanc
5fd60 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b  e */.  int nRef;
5fd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5fd80 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f   /* Number of po
5fd90 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73 20 73  inters to this s
5fda0 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 56 54  tructure */.  VT
5fdb0 61 62 6c 65 20 2a 70 4e 65 78 74 3b 20 20 20 20  able *pNext;    
5fdc0 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
5fdd0 69 6e 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28  in linked list (
5fde0 73 65 65 20 61 62 6f 76 65 29 20 2a 2f 0a 7d 3b  see above) */.};
5fdf0 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51 4c  ../*.** Each SQL
5fe00 20 74 61 62 6c 65 20 69 73 20 72 65 70 72 65 73   table is repres
5fe10 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20  ented in memory 
5fe20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  by an instance o
5fe30 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69  f the.** followi
5fe40 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ng structure..**
5fe50 0a 2a 2a 20 54 61 62 6c 65 2e 7a 4e 61 6d 65 20  .** Table.zName 
5fe60 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
5fe70 68 65 20 74 61 62 6c 65 2e 20 20 54 68 65 20 63  he table.  The c
5fe80 61 73 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69  ase of the origi
5fe90 6e 61 6c 0a 2a 2a 20 43 52 45 41 54 45 20 54 41  nal.** CREATE TA
5fea0 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73  BLE statement is
5feb0 20 73 74 6f 72 65 64 2c 20 62 75 74 20 63 61 73   stored, but cas
5fec0 65 20 69 73 20 6e 6f 74 20 73 69 67 6e 69 66 69  e is not signifi
5fed0 63 61 6e 74 20 66 6f 72 0a 2a 2a 20 63 6f 6d 70  cant for.** comp
5fee0 61 72 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54  arisons..**.** T
5fef0 61 62 6c 65 2e 6e 43 6f 6c 20 69 73 20 74 68 65  able.nCol is the
5ff00 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
5ff10 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65  ns in this table
5ff20 2e 20 20 54 61 62 6c 65 2e 61 43 6f 6c 20 69 73  .  Table.aCol is
5ff30 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f   a.** pointer to
5ff40 20 61 6e 20 61 72 72 61 79 20 6f 66 20 43 6f 6c   an array of Col
5ff50 75 6d 6e 20 73 74 72 75 63 74 75 72 65 73 2c 20  umn structures, 
5ff60 6f 6e 65 20 66 6f 72 20 65 61 63 68 20 63 6f 6c  one for each col
5ff70 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  umn..**.** If th
5ff80 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 49  e table has an I
5ff90 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
5ffa0 45 59 2c 20 74 68 65 6e 20 54 61 62 6c 65 2e 69  EY, then Table.i
5ffb0 50 4b 65 79 20 69 73 20 74 68 65 20 69 6e 64 65  PKey is the inde
5ffc0 78 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75  x of.** the colu
5ffd0 6d 6e 20 74 68 61 74 20 69 73 20 74 68 61 74 20  mn that is that 
5ffe0 6b 65 79 2e 20 20 20 4f 74 68 65 72 77 69 73 65  key.   Otherwise
5fff0 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69 73 20   Table.iPKey is 
60000 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 74 65 0a  negative.  Note.
60010 2a 2a 20 74 68 61 74 20 74 68 65 20 64 61 74 61  ** that the data
60020 74 79 70 65 20 6f 66 20 74 68 65 20 50 52 49 4d  type of the PRIM
60030 41 52 59 20 4b 45 59 20 6d 75 73 74 20 62 65 20  ARY KEY must be 
60040 49 4e 54 45 47 45 52 20 66 6f 72 20 74 68 69 73  INTEGER for this
60050 20 66 69 65 6c 64 20 74 6f 0a 2a 2a 20 62 65 20   field to.** be 
60060 73 65 74 2e 20 20 41 6e 20 49 4e 54 45 47 45 52  set.  An INTEGER
60070 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69 73 20   PRIMARY KEY is 
60080 75 73 65 64 20 61 73 20 74 68 65 20 72 6f 77 69  used as the rowi
60090 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f  d for each row o
600a0 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 2e 20  f.** the table. 
600b0 20 49 66 20 61 20 74 61 62 6c 65 20 68 61 73 20   If a table has 
600c0 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  no INTEGER PRIMA
600d0 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 61 20 72  RY KEY, then a r
600e0 61 6e 64 6f 6d 20 72 6f 77 69 64 0a 2a 2a 20 69  andom rowid.** i
600f0 73 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20  s generated for 
60100 65 61 63 68 20 72 6f 77 20 6f 66 20 74 68 65 20  each row of the 
60110 74 61 62 6c 65 2e 20 20 54 46 5f 48 61 73 50 72  table.  TF_HasPr
60120 69 6d 61 72 79 4b 65 79 20 69 73 20 73 65 74 20  imaryKey is set 
60130 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20  if.** the table 
60140 68 61 73 20 61 6e 79 20 50 52 49 4d 41 52 59 20  has any PRIMARY 
60150 4b 45 59 2c 20 49 4e 54 45 47 45 52 20 6f 72 20  KEY, INTEGER or 
60160 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a  otherwise..**.**
60170 20 54 61 62 6c 65 2e 74 6e 75 6d 20 69 73 20 74   Table.tnum is t
60180 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66  he page number f
60190 6f 72 20 74 68 65 20 72 6f 6f 74 20 42 54 72 65  or the root BTre
601a0 65 20 70 61 67 65 20 6f 66 20 74 68 65 20 74 61  e page of the ta
601b0 62 6c 65 20 69 6e 20 74 68 65 0a 2a 2a 20 64 61  ble in the.** da
601c0 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66  tabase file.  If
601d0 20 54 61 62 6c 65 2e 69 44 62 20 69 73 20 74 68   Table.iDb is th
601e0 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64  e index of the d
601f0 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 62 61  atabase table ba
60200 63 6b 65 6e 64 0a 2a 2a 20 69 6e 20 73 71 6c 69  ckend.** in sqli
60210 74 65 2e 61 44 62 5b 5d 2e 20 20 30 20 69 73 20  te.aDb[].  0 is 
60220 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74  for the main dat
60230 61 62 61 73 65 20 61 6e 64 20 31 20 69 73 20 66  abase and 1 is f
60240 6f 72 20 74 68 65 20 66 69 6c 65 20 74 68 61 74  or the file that
60250 0a 2a 2a 20 68 6f 6c 64 73 20 74 65 6d 70 6f 72  .** holds tempor
60260 61 72 79 20 74 61 62 6c 65 73 20 61 6e 64 20 69  ary tables and i
60270 6e 64 69 63 65 73 2e 20 20 49 66 20 54 46 5f 45  ndices.  If TF_E
60280 70 68 65 6d 65 72 61 6c 20 69 73 20 73 65 74 0a  phemeral is set.
60290 2a 2a 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c  ** then the tabl
602a0 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61  e is stored in a
602b0 20 66 69 6c 65 20 74 68 61 74 20 69 73 20 61 75   file that is au
602c0 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 6c 65  tomatically dele
602d0 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20  ted.** when the 
602e0 56 44 42 45 20 63 75 72 73 6f 72 20 74 6f 20 74  VDBE cursor to t
602f0 68 65 20 74 61 62 6c 65 20 69 73 20 63 6c 6f 73  he table is clos
60300 65 64 2e 20 20 49 6e 20 74 68 69 73 20 63 61 73  ed.  In this cas
60310 65 20 54 61 62 6c 65 2e 74 6e 75 6d 20 0a 2a 2a  e Table.tnum .**
60320 20 72 65 66 65 72 73 20 56 44 42 45 20 63 75 72   refers VDBE cur
60330 73 6f 72 20 6e 75 6d 62 65 72 20 74 68 61 74 20  sor number that 
60340 68 6f 6c 64 73 20 74 68 65 20 74 61 62 6c 65 20  holds the table 
60350 6f 70 65 6e 2c 20 6e 6f 74 20 74 6f 20 74 68 65  open, not to the
60360 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20 6e 75   root.** page nu
60370 6d 62 65 72 2e 20 20 54 72 61 6e 73 69 65 6e 74  mber.  Transient
60380 20 74 61 62 6c 65 73 20 61 72 65 20 75 73 65 64   tables are used
60390 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65 73   to hold the res
603a0 75 6c 74 73 20 6f 66 20 61 0a 2a 2a 20 73 75 62  ults of a.** sub
603b0 2d 71 75 65 72 79 20 74 68 61 74 20 61 70 70 65  -query that appe
603c0 61 72 73 20 69 6e 73 74 65 61 64 20 6f 66 20 61  ars instead of a
603d0 20 72 65 61 6c 20 74 61 62 6c 65 20 6e 61 6d 65   real table name
603e0 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
603f0 75 73 65 20 0a 2a 2a 20 6f 66 20 61 20 53 45 4c  use .** of a SEL
60400 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ECT statement..*
60410 2f 0a 73 74 72 75 63 74 20 54 61 62 6c 65 20 7b  /.struct Table {
60420 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 4d 65  .  sqlite3 *dbMe
60430 6d 3b 20 20 20 20 20 20 2f 2a 20 44 42 20 63 6f  m;      /* DB co
60440 6e 6e 65 63 74 69 6f 6e 20 75 73 65 64 20 66 6f  nnection used fo
60450 72 20 6c 6f 6f 6b 61 73 69 64 65 20 61 6c 6c 6f  r lookaside allo
60460 63 61 74 69 6f 6e 73 2e 20 2a 2f 0a 20 20 63 68  cations. */.  ch
60470 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20  ar *zName;      
60480 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
60490 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20  e table or view 
604a0 2a 2f 0a 20 20 69 6e 74 20 69 50 4b 65 79 3b 20  */.  int iPKey; 
604b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
604c0 6e 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 75 73  not negative, us
604d0 65 20 61 43 6f 6c 5b 69 50 4b 65 79 5d 20 61 73  e aCol[iPKey] as
604e0 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79   the primary key
604f0 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20   */.  int nCol; 
60500 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
60510 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
60520 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f  in this table */
60530 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 3b  .  Column *aCol;
60540 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72          /* Infor
60550 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63  mation about eac
60560 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49 6e  h column */.  In
60570 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 20  dex *pIndex;    
60580 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 53 51     /* List of SQ
60590 4c 20 69 6e 64 65 78 65 73 20 6f 6e 20 74 68 69  L indexes on thi
605a0 73 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 69 6e  s table. */.  in
605b0 74 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 20  t tnum;         
605c0 20 20 20 2f 2a 20 52 6f 6f 74 20 42 54 72 65 65     /* Root BTree
605d0 20 6e 6f 64 65 20 66 6f 72 20 74 68 69 73 20 74   node for this t
605e0 61 62 6c 65 20 28 73 65 65 20 6e 6f 74 65 20 61  able (see note a
605f0 62 6f 76 65 29 20 2a 2f 0a 20 20 53 65 6c 65 63  bove) */.  Selec
60600 74 20 2a 70 53 65 6c 65 63 74 3b 20 20 20 20 20  t *pSelect;     
60610 2f 2a 20 4e 55 4c 4c 20 66 6f 72 20 74 61 62 6c  /* NULL for tabl
60620 65 73 2e 20 20 50 6f 69 6e 74 73 20 74 6f 20 64  es.  Points to d
60630 65 66 69 6e 69 74 69 6f 6e 20 69 66 20 61 20 76  efinition if a v
60640 69 65 77 2e 20 2a 2f 0a 20 20 75 31 36 20 6e 52  iew. */.  u16 nR
60650 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ef;            /
60660 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e  * Number of poin
60670 74 65 72 73 20 74 6f 20 74 68 69 73 20 54 61 62  ters to this Tab
60680 6c 65 20 2a 2f 0a 20 20 75 38 20 74 61 62 46 6c  le */.  u8 tabFl
60690 61 67 73 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ags;         /* 
606a0 4d 61 73 6b 20 6f 66 20 54 46 5f 2a 20 76 61 6c  Mask of TF_* val
606b0 75 65 73 20 2a 2f 0a 20 20 75 38 20 6b 65 79 43  ues */.  u8 keyC
606c0 6f 6e 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  onf;          /*
606d0 20 57 68 61 74 20 74 6f 20 64 6f 20 69 6e 20 63   What to do in c
606e0 61 73 65 20 6f 66 20 75 6e 69 71 75 65 6e 65 73  ase of uniquenes
606f0 73 20 63 6f 6e 66 6c 69 63 74 20 6f 6e 20 69 50  s conflict on iP
60700 4b 65 79 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70  Key */.  FKey *p
60710 46 4b 65 79 3b 20 20 20 20 20 20 20 20 20 2f 2a  FKey;         /*
60720 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20   Linked list of 
60730 61 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65 79 73  all foreign keys
60740 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a   in this table *
60750 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66  /.  char *zColAf
60760 66 3b 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69  f;       /* Stri
60770 6e 67 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20  ng defining the 
60780 61 66 66 69 6e 69 74 79 20 6f 66 20 65 61 63 68  affinity of each
60790 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 69 66 6e 64   column */.#ifnd
607a0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
607b0 48 45 43 4b 0a 20 20 45 78 70 72 20 2a 70 43 68  HECK.  Expr *pCh
607c0 65 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  eck;        /* T
607d0 68 65 20 41 4e 44 20 6f 66 20 61 6c 6c 20 43 48  he AND of all CH
607e0 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  ECK constraints 
607f0 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65  */.#endif.#ifnde
60800 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c  f SQLITE_OMIT_AL
60810 54 45 52 54 41 42 4c 45 0a 20 20 69 6e 74 20 61  TERTABLE.  int a
60820 64 64 43 6f 6c 4f 66 66 73 65 74 3b 20 20 20 20  ddColOffset;    
60830 2f 2a 20 4f 66 66 73 65 74 20 69 6e 20 43 52 45  /* Offset in CRE
60840 41 54 45 20 54 41 42 4c 45 20 73 74 6d 74 20 74  ATE TABLE stmt t
60850 6f 20 61 64 64 20 61 20 6e 65 77 20 63 6f 6c 75  o add a new colu
60860 6d 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66  mn */.#endif.#if
60870 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
60880 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
60890 56 54 61 62 6c 65 20 2a 70 56 54 61 62 6c 65 3b  VTable *pVTable;
608a0 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
608b0 56 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 2e 20  VTable objects. 
608c0 2a 2f 0a 20 20 69 6e 74 20 6e 4d 6f 64 75 6c 65  */.  int nModule
608d0 41 72 67 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d  Arg;      /* Num
608e0 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ber of arguments
608f0 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a   to the module *
60900 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 4d 6f 64  /.  char **azMod
60910 75 6c 65 41 72 67 3b 20 20 2f 2a 20 54 65 78 74  uleArg;  /* Text
60920 20 6f 66 20 61 6c 6c 20 6d 6f 64 75 6c 65 20 61   of all module a
60930 72 67 73 2e 20 5b 30 5d 20 69 73 20 6d 6f 64 75  rgs. [0] is modu
60940 6c 65 20 6e 61 6d 65 20 2a 2f 0a 23 65 6e 64 69  le name */.#endi
60950 66 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72  f.  Trigger *pTr
60960 69 67 67 65 72 3b 20 20 20 2f 2a 20 4c 69 73 74  igger;   /* List
60970 20 6f 66 20 74 72 69 67 67 65 72 73 20 73 74 6f   of triggers sto
60980 72 65 64 20 69 6e 20 70 53 63 68 65 6d 61 20 2a  red in pSchema *
60990 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68  /.  Schema *pSch
609a0 65 6d 61 3b 20 20 20 20 20 2f 2a 20 53 63 68 65  ema;     /* Sche
609b0 6d 61 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ma that contains
609c0 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20   this table */. 
609d0 20 54 61 62 6c 65 20 2a 70 4e 65 78 74 5a 6f 6d   Table *pNextZom
609e0 62 69 65 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 6e  bie;  /* Next on
609f0 20 74 68 65 20 50 61 72 73 65 2e 70 5a 6f 6d 62   the Parse.pZomb
60a00 69 65 54 61 62 20 6c 69 73 74 20 2a 2f 0a 7d 3b  ieTab list */.};
60a10 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20  ../*.** Allowed 
60a20 76 61 6c 75 65 73 20 66 6f 72 20 54 61 62 65 2e  values for Tabe.
60a30 74 61 62 46 6c 61 67 73 2e 0a 2a 2f 0a 23 64 65  tabFlags..*/.#de
60a40 66 69 6e 65 20 54 46 5f 52 65 61 64 6f 6e 6c 79  fine TF_Readonly
60a50 20 20 20 20 20 20 20 20 30 78 30 31 20 20 20 20          0x01    
60a60 2f 2a 20 52 65 61 64 2d 6f 6e 6c 79 20 73 79 73  /* Read-only sys
60a70 74 65 6d 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65  tem table */.#de
60a80 66 69 6e 65 20 54 46 5f 45 70 68 65 6d 65 72 61  fine TF_Ephemera
60a90 6c 20 20 20 20 20 20 20 30 78 30 32 20 20 20 20  l       0x02    
60aa0 2f 2a 20 41 6e 20 65 70 68 65 6d 65 72 61 6c 20  /* An ephemeral 
60ab0 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65  table */.#define
60ac0 20 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65   TF_HasPrimaryKe
60ad0 79 20 20 20 30 78 30 34 20 20 20 20 2f 2a 20 54  y   0x04    /* T
60ae0 61 62 6c 65 20 68 61 73 20 61 20 70 72 69 6d 61  able has a prima
60af0 72 79 20 6b 65 79 20 2a 2f 0a 23 64 65 66 69 6e  ry key */.#defin
60b00 65 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65  e TF_Autoincreme
60b10 6e 74 20 20 20 30 78 30 38 20 20 20 20 2f 2a 20  nt   0x08    /* 
60b20 49 6e 74 65 67 65 72 20 70 72 69 6d 61 72 79 20  Integer primary 
60b30 6b 65 79 20 69 73 20 61 75 74 6f 69 6e 63 72 65  key is autoincre
60b40 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ment */.#define 
60b50 54 46 5f 56 69 72 74 75 61 6c 20 20 20 20 20 20  TF_Virtual      
60b60 20 20 20 30 78 31 30 20 20 20 20 2f 2a 20 49 73     0x10    /* Is
60b70 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
60b80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 46 5f 4e   */.#define TF_N
60b90 65 65 64 4d 65 74 61 64 61 74 61 20 20 20 20 30  eedMetadata    0
60ba0 78 32 30 20 20 20 20 2f 2a 20 61 43 6f 6c 5b 5d  x20    /* aCol[]
60bb0 2e 7a 54 79 70 65 20 61 6e 64 20 61 43 6f 6c 5b  .zType and aCol[
60bc0 5d 2e 70 43 6f 6c 6c 20 6d 69 73 73 69 6e 67 20  ].pColl missing 
60bd0 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74  */..../*.** Test
60be0 20 74 6f 20 73 65 65 20 77 68 65 74 68 65 72 20   to see whether 
60bf0 6f 72 20 6e 6f 74 20 61 20 74 61 62 6c 65 20 69  or not a table i
60c00 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  s a virtual tabl
60c10 65 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a 20 64  e.  This is.** d
60c20 6f 6e 65 20 61 73 20 61 20 6d 61 63 72 6f 20 73  one as a macro s
60c30 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20 62  o that it will b
60c40 65 20 6f 70 74 69 6d 69 7a 65 64 20 6f 75 74 20  e optimized out 
60c50 77 68 65 6e 20 76 69 72 74 75 61 6c 0a 2a 2a 20  when virtual.** 
60c60 74 61 62 6c 65 20 73 75 70 70 6f 72 74 20 69 73  table support is
60c70 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68   omitted from th
60c80 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66 6e  e build..*/.#ifn
60c90 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
60ca0 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 20  VIRTUALTABLE.#  
60cb0 64 65 66 69 6e 65 20 49 73 56 69 72 74 75 61 6c  define IsVirtual
60cc0 28 58 29 20 20 20 20 20 20 28 28 28 58 29 2d 3e  (X)      (((X)->
60cd0 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69  tabFlags & TF_Vi
60ce0 72 74 75 61 6c 29 21 3d 30 29 0a 23 20 20 64 65  rtual)!=0).#  de
60cf0 66 69 6e 65 20 49 73 48 69 64 64 65 6e 43 6f 6c  fine IsHiddenCol
60d00 75 6d 6e 28 58 29 20 28 28 58 29 2d 3e 69 73 48  umn(X) ((X)->isH
60d10 69 64 64 65 6e 29 0a 23 65 6c 73 65 0a 23 20 20  idden).#else.#  
60d20 64 65 66 69 6e 65 20 49 73 56 69 72 74 75 61 6c  define IsVirtual
60d30 28 58 29 20 20 20 20 20 20 30 0a 23 20 20 64 65  (X)      0.#  de
60d40 66 69 6e 65 20 49 73 48 69 64 64 65 6e 43 6f 6c  fine IsHiddenCol
60d50 75 6d 6e 28 58 29 20 30 0a 23 65 6e 64 69 66 0a  umn(X) 0.#endif.
60d60 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 66 6f 72 65  ./*.** Each fore
60d70 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
60d80 6e 74 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  nt is an instanc
60d90 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
60da0 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  ng structure..**
60db0 0a 2a 2a 20 41 20 66 6f 72 65 69 67 6e 20 6b 65  .** A foreign ke
60dc0 79 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20  y is associated 
60dd0 77 69 74 68 20 74 77 6f 20 74 61 62 6c 65 73 2e  with two tables.
60de0 20 20 54 68 65 20 22 66 72 6f 6d 22 20 74 61 62    The "from" tab
60df0 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20 74 61 62  le is.** the tab
60e00 6c 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  le that contains
60e10 20 74 68 65 20 52 45 46 45 52 45 4e 43 45 53 20   the REFERENCES 
60e20 63 6c 61 75 73 65 20 74 68 61 74 20 63 72 65 61  clause that crea
60e30 74 65 73 20 74 68 65 20 66 6f 72 65 69 67 6e 0a  tes the foreign.
60e40 2a 2a 20 6b 65 79 2e 20 20 54 68 65 20 22 74 6f  ** key.  The "to
60e50 22 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 74  " table is the t
60e60 61 62 6c 65 20 74 68 61 74 20 69 73 20 6e 61 6d  able that is nam
60e70 65 64 20 69 6e 20 74 68 65 20 52 45 46 45 52 45  ed in the REFERE
60e80 4e 43 45 53 20 63 6c 61 75 73 65 2e 0a 2a 2a 20  NCES clause..** 
60e90 43 6f 6e 73 69 64 65 72 20 74 68 69 73 20 65 78  Consider this ex
60ea0 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ample:.**.**    
60eb0 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 65 78   CREATE TABLE ex
60ec0 31 28 0a 2a 2a 20 20 20 20 20 20 20 61 20 49 4e  1(.**       a IN
60ed0 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
60ee0 59 2c 0a 2a 2a 20 20 20 20 20 20 20 62 20 49 4e  Y,.**       b IN
60ef0 54 45 47 45 52 20 43 4f 4e 53 54 52 41 49 4e 54  TEGER CONSTRAINT
60f00 20 66 6b 31 20 52 45 46 45 52 45 4e 43 45 53 20   fk1 REFERENCES 
60f10 65 78 32 28 78 29 0a 2a 2a 20 20 20 20 20 29 3b  ex2(x).**     );
60f20 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66 6f 72 65 69  .**.** For forei
60f30 67 6e 20 6b 65 79 20 22 66 6b 31 22 2c 20 74 68  gn key "fk1", th
60f40 65 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20  e from-table is 
60f50 22 65 78 31 22 20 61 6e 64 20 74 68 65 20 74 6f  "ex1" and the to
60f60 2d 74 61 62 6c 65 20 69 73 20 22 65 78 32 22 2e  -table is "ex2".
60f70 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 52 45 46 45  .**.** Each REFE
60f80 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 67 65  RENCES clause ge
60f90 6e 65 72 61 74 65 73 20 61 6e 20 69 6e 73 74 61  nerates an insta
60fa0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
60fb0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a  wing structure.*
60fc0 2a 20 77 68 69 63 68 20 69 73 20 61 74 74 61 63  * which is attac
60fd0 68 65 64 20 74 6f 20 74 68 65 20 66 72 6f 6d 2d  hed to the from-
60fe0 74 61 62 6c 65 2e 20 20 54 68 65 20 74 6f 2d 74  table.  The to-t
60ff0 61 62 6c 65 20 6e 65 65 64 20 6e 6f 74 20 65 78  able need not ex
61000 69 73 74 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20  ist when.** the 
61010 66 72 6f 6d 2d 74 61 62 6c 65 20 69 73 20 63 72  from-table is cr
61020 65 61 74 65 64 2e 20 20 54 68 65 20 65 78 69 73  eated.  The exis
61030 74 65 6e 63 65 20 6f 66 20 74 68 65 20 74 6f 2d  tence of the to-
61040 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 63 68 65  table is not che
61050 63 6b 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  cked..*/.struct 
61060 46 4b 65 79 20 7b 0a 20 20 54 61 62 6c 65 20 2a  FKey {.  Table *
61070 70 46 72 6f 6d 3b 20 20 20 20 20 2f 2a 20 54 61  pFrom;     /* Ta
61080 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ble containing t
61090 68 65 20 52 45 46 45 52 45 4e 43 45 53 20 63 6c  he REFERENCES cl
610a0 61 75 73 65 20 28 61 6b 61 3a 20 43 68 69 6c 64  ause (aka: Child
610b0 29 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65  ) */.  FKey *pNe
610c0 78 74 46 72 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74  xtFrom;  /* Next
610d0 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 20   foreign key in 
610e0 70 46 72 6f 6d 20 2a 2f 0a 20 20 63 68 61 72 20  pFrom */.  char 
610f0 2a 7a 54 6f 3b 20 20 20 20 20 20 20 20 2f 2a 20  *zTo;        /* 
61100 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 74 68  Name of table th
61110 61 74 20 74 68 65 20 6b 65 79 20 70 6f 69 6e 74  at the key point
61120 73 20 74 6f 20 28 61 6b 61 3a 20 50 61 72 65 6e  s to (aka: Paren
61130 74 29 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e  t) */.  FKey *pN
61140 65 78 74 54 6f 3b 20 20 20 20 2f 2a 20 4e 65 78  extTo;    /* Nex
61150 74 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e  t foreign key on
61160 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 6f   table named zTo
61170 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 50 72 65   */.  FKey *pPre
61180 76 54 6f 3b 20 20 20 20 2f 2a 20 50 72 65 76 69  vTo;    /* Previ
61190 6f 75 73 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  ous foreign key 
611a0 6f 6e 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a  on table named z
611b0 54 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c  To */.  int nCol
611c0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ;         /* Num
611d0 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
611e0 6e 20 74 68 69 73 20 6b 65 79 20 2a 2f 0a 20 20  n this key */.  
611f0 2f 2a 20 45 56 3a 20 52 2d 33 30 33 32 33 2d 32  /* EV: R-30323-2
61200 31 39 31 37 20 2a 2f 0a 20 20 75 38 20 69 73 44  1917 */.  u8 isD
61210 65 66 65 72 72 65 64 3b 20 20 20 20 2f 2a 20 54  eferred;    /* T
61220 72 75 65 20 69 66 20 63 6f 6e 73 74 72 61 69 6e  rue if constrain
61230 74 20 63 68 65 63 6b 69 6e 67 20 69 73 20 64 65  t checking is de
61240 66 65 72 72 65 64 20 74 69 6c 6c 20 43 4f 4d 4d  ferred till COMM
61250 49 54 20 2a 2f 0a 20 20 75 38 20 61 41 63 74 69  IT */.  u8 aActi
61260 6f 6e 5b 32 5d 3b 20 20 20 20 20 20 20 20 20 20  on[2];          
61270 2f 2a 20 4f 4e 20 44 45 4c 45 54 45 20 61 6e 64  /* ON DELETE and
61280 20 4f 4e 20 55 50 44 41 54 45 20 61 63 74 69 6f   ON UPDATE actio
61290 6e 73 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79  ns, respectively
612a0 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 61   */.  Trigger *a
612b0 70 54 72 69 67 67 65 72 5b 32 5d 3b 20 20 2f 2a  pTrigger[2];  /*
612c0 20 54 72 69 67 67 65 72 73 20 66 6f 72 20 61 41   Triggers for aA
612d0 63 74 69 6f 6e 5b 5d 20 61 63 74 69 6f 6e 73 20  ction[] actions 
612e0 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 43 6f 6c  */.  struct sCol
612f0 4d 61 70 20 7b 20 20 2f 2a 20 4d 61 70 70 69 6e  Map {  /* Mappin
61300 67 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  g of columns in 
61310 70 46 72 6f 6d 20 74 6f 20 63 6f 6c 75 6d 6e 73  pFrom to columns
61320 20 69 6e 20 7a 54 6f 20 2a 2f 0a 20 20 20 20 69   in zTo */.    i
61330 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20 20 20 20  nt iFrom;       
61340 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f    /* Index of co
61350 6c 75 6d 6e 20 69 6e 20 70 46 72 6f 6d 20 2a 2f  lumn in pFrom */
61360 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 3b  .    char *zCol;
61370 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
61380 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 7a 54 6f  of column in zTo
61390 2e 20 20 49 66 20 30 20 75 73 65 20 50 52 49 4d  .  If 0 use PRIM
613a0 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 7d 20 61  ARY KEY */.  } a
613b0 43 6f 6c 5b 31 5d 3b 20 20 20 20 20 20 20 20 2f  Col[1];        /
613c0 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20  * One entry for 
613d0 65 61 63 68 20 6f 66 20 6e 43 6f 6c 20 63 6f 6c  each of nCol col
613e0 75 6d 6e 20 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  umn s */.};../*.
613f0 2a 2a 20 53 51 4c 69 74 65 20 73 75 70 70 6f 72  ** SQLite suppor
61400 74 73 20 6d 61 6e 79 20 64 69 66 66 65 72 65 6e  ts many differen
61410 74 20 77 61 79 73 20 74 6f 20 72 65 73 6f 6c 76  t ways to resolv
61420 65 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a  e a constraint.*
61430 2a 20 65 72 72 6f 72 2e 20 20 52 4f 4c 4c 42 41  * error.  ROLLBA
61440 43 4b 20 70 72 6f 63 65 73 73 69 6e 67 20 6d 65  CK processing me
61450 61 6e 73 20 74 68 61 74 20 61 20 63 6f 6e 73 74  ans that a const
61460 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 0a  raint violation.
61470 2a 2a 20 63 61 75 73 65 73 20 74 68 65 20 6f 70  ** causes the op
61480 65 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65  eration in proce
61490 73 73 20 74 6f 20 66 61 69 6c 20 61 6e 64 20 66  ss to fail and f
614a0 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  or the current t
614b0 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f  ransaction.** to
614c0 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e   be rolled back.
614d0 20 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69    ABORT processi
614e0 6e 67 20 6d 65 61 6e 73 20 74 68 65 20 6f 70 65  ng means the ope
614f0 72 61 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73  ration in proces
61500 73 0a 2a 2a 20 66 61 69 6c 73 20 61 6e 64 20 61  s.** fails and a
61510 6e 79 20 70 72 69 6f 72 20 63 68 61 6e 67 65 73  ny prior changes
61520 20 66 72 6f 6d 20 74 68 61 74 20 6f 6e 65 20 6f   from that one o
61530 70 65 72 61 74 69 6f 6e 20 61 72 65 20 62 61 63  peration are bac
61540 6b 65 64 20 6f 75 74 2c 0a 2a 2a 20 62 75 74 20  ked out,.** but 
61550 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
61560 69 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61  is not rolled ba
61570 63 6b 2e 20 20 46 41 49 4c 20 70 72 6f 63 65 73  ck.  FAIL proces
61580 73 69 6e 67 20 6d 65 61 6e 73 20 74 68 61 74 0a  sing means that.
61590 2a 2a 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  ** the operation
615a0 20 69 6e 20 70 72 6f 67 72 65 73 73 20 73 74 6f   in progress sto
615b0 70 73 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61  ps and returns a
615c0 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20 20 42  n error code.  B
615d0 75 74 20 70 72 69 6f 72 0a 2a 2a 20 63 68 61 6e  ut prior.** chan
615e0 67 65 73 20 64 75 65 20 74 6f 20 74 68 65 20 73  ges due to the s
615f0 61 6d 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 72  ame operation ar
61600 65 20 6e 6f 74 20 62 61 63 6b 65 64 20 6f 75 74  e not backed out
61610 20 61 6e 64 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b   and no rollback
61620 0a 2a 2a 20 6f 63 63 75 72 73 2e 20 20 49 47 4e  .** occurs.  IGN
61630 4f 52 45 20 6d 65 61 6e 73 20 74 68 61 74 20 74  ORE means that t
61640 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 72 6f  he particular ro
61650 77 20 74 68 61 74 20 63 61 75 73 65 64 20 74 68  w that caused th
61660 65 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20  e constraint.** 
61670 65 72 72 6f 72 20 69 73 20 6e 6f 74 20 69 6e 73  error is not ins
61680 65 72 74 65 64 20 6f 72 20 75 70 64 61 74 65 64  erted or updated
61690 2e 20 20 50 72 6f 63 65 73 73 69 6e 67 20 63 6f  .  Processing co
616a0 6e 74 69 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65  ntinues and no e
616b0 72 72 6f 72 0a 2a 2a 20 69 73 20 72 65 74 75 72  rror.** is retur
616c0 6e 65 64 2e 20 20 52 45 50 4c 41 43 45 20 6d 65  ned.  REPLACE me
616d0 61 6e 73 20 74 68 61 74 20 70 72 65 65 78 69 73  ans that preexis
616e0 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 72 6f  ting database ro
616f0 77 73 20 74 68 61 74 20 63 61 75 73 65 64 0a 2a  ws that caused.*
61700 2a 20 61 20 55 4e 49 51 55 45 20 63 6f 6e 73 74  * a UNIQUE const
61710 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20  raint violation 
61720 61 72 65 20 72 65 6d 6f 76 65 64 20 73 6f 20 74  are removed so t
61730 68 61 74 20 74 68 65 20 6e 65 77 20 69 6e 73 65  hat the new inse
61740 72 74 20 6f 72 0a 2a 2a 20 75 70 64 61 74 65 20  rt or.** update 
61750 63 61 6e 20 70 72 6f 63 65 65 64 2e 20 20 50 72  can proceed.  Pr
61760 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75  ocessing continu
61770 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f 72 20  es and no error 
61780 69 73 20 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 0a  is reported..**.
61790 2a 2a 20 52 45 53 54 52 49 43 54 2c 20 53 45 54  ** RESTRICT, SET
617a0 4e 55 4c 4c 2c 20 61 6e 64 20 43 41 53 43 41 44  NULL, and CASCAD
617b0 45 20 61 63 74 69 6f 6e 73 20 61 70 70 6c 79 20  E actions apply 
617c0 6f 6e 6c 79 20 74 6f 20 66 6f 72 65 69 67 6e 20  only to foreign 
617d0 6b 65 79 73 2e 0a 2a 2a 20 52 45 53 54 52 49 43  keys..** RESTRIC
617e0 54 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  T is the same as
617f0 20 41 42 4f 52 54 20 66 6f 72 20 49 4d 4d 45 44   ABORT for IMMED
61800 49 41 54 45 20 66 6f 72 65 69 67 6e 20 6b 65 79  IATE foreign key
61810 73 20 61 6e 64 20 74 68 65 0a 2a 2a 20 73 61 6d  s and the.** sam
61820 65 20 61 73 20 52 4f 4c 4c 42 41 43 4b 20 66 6f  e as ROLLBACK fo
61830 72 20 44 45 46 45 52 52 45 44 20 6b 65 79 73 2e  r DEFERRED keys.
61840 20 20 53 45 54 4e 55 4c 4c 20 6d 65 61 6e 73 20    SETNULL means 
61850 74 68 61 74 20 74 68 65 20 66 6f 72 65 69 67 6e  that the foreign
61860 0a 2a 2a 20 6b 65 79 20 69 73 20 73 65 74 20 74  .** key is set t
61870 6f 20 4e 55 4c 4c 2e 20 20 43 41 53 43 41 44 45  o NULL.  CASCADE
61880 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20 44 45   means that a DE
61890 4c 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 6f  LETE or UPDATE o
618a0 66 20 74 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e  f the.** referen
618b0 63 65 64 20 74 61 62 6c 65 20 72 6f 77 20 69 73  ced table row is
618c0 20 70 72 6f 70 61 67 61 74 65 64 20 69 6e 74 6f   propagated into
618d0 20 74 68 65 20 72 6f 77 20 74 68 61 74 20 68 6f   the row that ho
618e0 6c 64 73 20 74 68 65 0a 2a 2a 20 66 6f 72 65 69  lds the.** forei
618f0 67 6e 20 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 54  gn key..** .** T
61900 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d  he following sym
61910 62 6f 6c 69 63 20 76 61 6c 75 65 73 20 61 72 65  bolic values are
61920 20 75 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20   used to record 
61930 77 68 69 63 68 20 74 79 70 65 0a 2a 2a 20 6f 66  which type.** of
61940 20 61 63 74 69 6f 6e 20 74 6f 20 74 61 6b 65 2e   action to take.
61950 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 4e  .*/.#define OE_N
61960 6f 6e 65 20 20 20 20 20 30 20 20 20 2f 2a 20 54  one     0   /* T
61970 68 65 72 65 20 69 73 20 6e 6f 20 63 6f 6e 73 74  here is no const
61980 72 61 69 6e 74 20 74 6f 20 63 68 65 63 6b 20 2a  raint to check *
61990 2f 0a 23 64 65 66 69 6e 65 20 4f 45 5f 52 6f 6c  /.#define OE_Rol
619a0 6c 62 61 63 6b 20 31 20 20 20 2f 2a 20 46 61 69  lback 1   /* Fai
619b0 6c 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20  l the operation 
619c0 61 6e 64 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65  and rollback the
619d0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a   transaction */.
619e0 23 64 65 66 69 6e 65 20 4f 45 5f 41 62 6f 72 74  #define OE_Abort
619f0 20 20 20 20 32 20 20 20 2f 2a 20 42 61 63 6b 20      2   /* Back 
61a00 6f 75 74 20 63 68 61 6e 67 65 73 20 62 75 74 20  out changes but 
61a10 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 74  do no rollback t
61a20 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64  ransaction */.#d
61a30 65 66 69 6e 65 20 4f 45 5f 46 61 69 6c 20 20 20  efine OE_Fail   
61a40 20 20 33 20 20 20 2f 2a 20 53 74 6f 70 20 74 68    3   /* Stop th
61a50 65 20 6f 70 65 72 61 74 69 6f 6e 20 62 75 74 20  e operation but 
61a60 6c 65 61 76 65 20 61 6c 6c 20 70 72 69 6f 72 20  leave all prior 
61a70 63 68 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69  changes */.#defi
61a80 6e 65 20 4f 45 5f 49 67 6e 6f 72 65 20 20 20 34  ne OE_Ignore   4
61a90 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65     /* Ignore the
61aa0 20 65 72 72 6f 72 2e 20 44 6f 20 6e 6f 74 20 64   error. Do not d
61ab0 6f 20 74 68 65 20 49 4e 53 45 52 54 20 6f 72 20  o the INSERT or 
61ac0 55 50 44 41 54 45 20 2a 2f 0a 23 64 65 66 69 6e  UPDATE */.#defin
61ad0 65 20 4f 45 5f 52 65 70 6c 61 63 65 20 20 35 20  e OE_Replace  5 
61ae0 20 20 2f 2a 20 44 65 6c 65 74 65 20 65 78 69 73    /* Delete exis
61af0 74 69 6e 67 20 72 65 63 6f 72 64 2c 20 74 68 65  ting record, the
61b00 6e 20 64 6f 20 49 4e 53 45 52 54 20 6f 72 20 55  n do INSERT or U
61b10 50 44 41 54 45 20 2a 2f 0a 0a 23 64 65 66 69 6e  PDATE */..#defin
61b20 65 20 4f 45 5f 52 65 73 74 72 69 63 74 20 36 20  e OE_Restrict 6 
61b30 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74 20 66 6f    /* OE_Abort fo
61b40 72 20 49 4d 4d 45 44 49 41 54 45 2c 20 4f 45 5f  r IMMEDIATE, OE_
61b50 52 6f 6c 6c 62 61 63 6b 20 66 6f 72 20 44 45 46  Rollback for DEF
61b60 45 52 52 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65  ERRED */.#define
61b70 20 4f 45 5f 53 65 74 4e 75 6c 6c 20 20 37 20 20   OE_SetNull  7  
61b80 20 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65   /* Set the fore
61b90 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f  ign key value to
61ba0 20 4e 55 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65   NULL */.#define
61bb0 20 4f 45 5f 53 65 74 44 66 6c 74 20 20 38 20 20   OE_SetDflt  8  
61bc0 20 2f 2a 20 53 65 74 20 74 68 65 20 66 6f 72 65   /* Set the fore
61bd0 69 67 6e 20 6b 65 79 20 76 61 6c 75 65 20 74 6f  ign key value to
61be0 20 69 74 73 20 64 65 66 61 75 6c 74 20 2a 2f 0a   its default */.
61bf0 23 64 65 66 69 6e 65 20 4f 45 5f 43 61 73 63 61  #define OE_Casca
61c00 64 65 20 20 39 20 20 20 2f 2a 20 43 61 73 63 61  de  9   /* Casca
61c10 64 65 20 74 68 65 20 63 68 61 6e 67 65 73 20 2a  de the changes *
61c20 2f 0a 0a 23 64 65 66 69 6e 65 20 4f 45 5f 44 65  /..#define OE_De
61c30 66 61 75 6c 74 20 20 39 39 20 20 2f 2a 20 44 6f  fault  99  /* Do
61c40 20 77 68 61 74 65 76 65 72 20 74 68 65 20 64 65   whatever the de
61c50 66 61 75 6c 74 20 61 63 74 69 6f 6e 20 69 73 20  fault action is 
61c60 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e  */.../*.** An in
61c70 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
61c80 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
61c90 65 20 69 73 20 70 61 73 73 65 64 20 61 73 20 74  e is passed as t
61ca0 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67 75  he first.** argu
61cb0 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 56  ment to sqlite3V
61cc0 64 62 65 4b 65 79 43 6f 6d 70 61 72 65 20 61 6e  dbeKeyCompare an
61cd0 64 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6e  d is used to con
61ce0 74 72 6f 6c 20 74 68 65 20 0a 2a 2a 20 63 6f 6d  trol the .** com
61cf0 70 61 72 69 73 6f 6e 20 6f 66 20 74 68 65 20 74  parison of the t
61d00 77 6f 20 69 6e 64 65 78 20 6b 65 79 73 2e 0a 2a  wo index keys..*
61d10 2f 0a 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f  /.struct KeyInfo
61d20 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62   {.  sqlite3 *db
61d30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
61d40 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
61d50 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b  ion */.  u8 enc;
61d60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
61d70 54 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2d 20  Text encoding - 
61d80 6f 6e 65 20 6f 66 20 74 68 65 20 54 45 58 54 5f  one of the TEXT_
61d90 55 74 66 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20  Utf* values */. 
61da0 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20 20 20   u16 nField;    
61db0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
61dc0 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43 6f  f entries in aCo
61dd0 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 53  ll[] */.  u8 *aS
61de0 6f 72 74 4f 72 64 65 72 3b 20 20 20 20 20 2f 2a  ortOrder;     /*
61df0 20 49 66 20 64 65 66 69 6e 65 64 20 61 6e 20 61   If defined an a
61e00 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 69 73 20  SortOrder[i] is 
61e10 74 72 75 65 2c 20 73 6f 72 74 20 44 45 53 43 20  true, sort DESC 
61e20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43  */.  CollSeq *aC
61e30 6f 6c 6c 5b 31 5d 3b 20 20 2f 2a 20 43 6f 6c 6c  oll[1];  /* Coll
61e40 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66  ating sequence f
61e50 6f 72 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20  or each term of 
61e60 74 68 65 20 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f  the key */.};../
61e70 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
61e80 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
61e90 67 20 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64  g structure hold
61ea0 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  s information ab
61eb0 6f 75 74 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20  out a.** single 
61ec0 69 6e 64 65 78 20 72 65 63 6f 72 64 20 74 68 61  index record tha
61ed0 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  t has already be
61ee0 65 6e 20 70 61 72 73 65 64 20 6f 75 74 20 69 6e  en parsed out in
61ef0 74 6f 20 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a  to individual.**
61f00 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 41   values..**.** A
61f10 20 72 65 63 6f 72 64 20 69 73 20 61 6e 20 6f 62   record is an ob
61f20 6a 65 63 74 20 74 68 61 74 20 63 6f 6e 74 61 69  ject that contai
61f30 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 66  ns one or more f
61f40 69 65 6c 64 73 20 6f 66 20 64 61 74 61 2e 0a 2a  ields of data..*
61f50 2a 20 52 65 63 6f 72 64 73 20 61 72 65 20 75 73  * Records are us
61f60 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  ed to store the 
61f70 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 74 61 62  content of a tab
61f80 6c 65 20 72 6f 77 20 61 6e 64 20 74 6f 20 73 74  le row and to st
61f90 6f 72 65 0a 2a 2a 20 74 68 65 20 6b 65 79 20 6f  ore.** the key o
61fa0 66 20 61 6e 20 69 6e 64 65 78 2e 20 20 41 20 62  f an index.  A b
61fb0 6c 6f 62 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20  lob encoding of 
61fc0 61 20 72 65 63 6f 72 64 20 69 73 20 63 72 65 61  a record is crea
61fd0 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 4f 50  ted by.** the OP
61fe0 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f  _MakeRecord opco
61ff0 64 65 20 6f 66 20 74 68 65 20 56 44 42 45 20 61  de of the VDBE a
62000 6e 64 20 69 73 20 64 69 73 61 73 73 65 6d 62 6c  nd is disassembl
62010 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 4f 50 5f  ed by the.** OP_
62020 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a  Column opcode..*
62030 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74  *.** This struct
62040 75 72 65 20 68 6f 6c 64 73 20 61 20 72 65 63 6f  ure holds a reco
62050 72 64 20 74 68 61 74 20 68 61 73 20 61 6c 72 65  rd that has alre
62060 61 64 79 20 62 65 65 6e 20 64 69 73 61 73 73 65  ady been disasse
62070 6d 62 6c 65 64 0a 2a 2a 20 69 6e 74 6f 20 69 74  mbled.** into it
62080 73 20 63 6f 6e 73 74 69 74 75 65 6e 74 20 66 69  s constituent fi
62090 65 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  elds..*/.struct 
620a0 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 7b  UnpackedRecord {
620b0 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79  .  KeyInfo *pKey
620c0 49 6e 66 6f 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74  Info;  /* Collat
620d0 69 6f 6e 20 61 6e 64 20 73 6f 72 74 2d 6f 72 64  ion and sort-ord
620e0 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a  er information *
620f0 2f 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20  /.  u16 nField; 
62100 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
62110 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
62120 61 70 4d 65 6d 5b 5d 20 2a 2f 0a 20 20 75 31 36  apMem[] */.  u16
62130 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
62140 20 2f 2a 20 42 6f 6f 6c 65 61 6e 20 73 65 74 74   /* Boolean sett
62150 69 6e 67 73 2e 20 20 55 4e 50 41 43 4b 45 44 5f  ings.  UNPACKED_
62160 2e 2e 2e 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69  ... below */.  i
62170 36 34 20 72 6f 77 69 64 3b 20 20 20 20 20 20 20  64 rowid;       
62180 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20 55 4e     /* Used by UN
62190 50 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45  PACKED_PREFIX_SE
621a0 41 52 43 48 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61  ARCH */.  Mem *a
621b0 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Mem;          /*
621c0 20 56 61 6c 75 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f   Values */.};../
621d0 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c  *.** Allowed val
621e0 75 65 73 20 6f 66 20 55 6e 70 61 63 6b 65 64 52  ues of UnpackedR
621f0 65 63 6f 72 64 2e 66 6c 61 67 73 0a 2a 2f 0a 23  ecord.flags.*/.#
62200 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f  define UNPACKED_
62210 4e 45 45 44 5f 46 52 45 45 20 20 20 20 20 30 78  NEED_FREE     0x
62220 30 30 30 31 20 20 2f 2a 20 4d 65 6d 6f 72 79 20  0001  /* Memory 
62230 69 73 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d  is from sqlite3M
62240 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64 65 66 69  alloc() */.#defi
62250 6e 65 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44  ne UNPACKED_NEED
62260 5f 44 45 53 54 52 4f 59 20 20 30 78 30 30 30 32  _DESTROY  0x0002
62270 20 20 2f 2a 20 61 70 4d 65 6d 5b 5d 73 20 73 68    /* apMem[]s sh
62280 6f 75 6c 64 20 61 6c 6c 20 62 65 20 64 65 73 74  ould all be dest
62290 72 6f 79 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  royed */.#define
622a0 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45   UNPACKED_IGNORE
622b0 5f 52 4f 57 49 44 20 20 30 78 30 30 30 34 20 20  _ROWID  0x0004  
622c0 2f 2a 20 49 67 6e 6f 72 65 20 74 72 61 69 6c 69  /* Ignore traili
622d0 6e 67 20 72 6f 77 69 64 20 6f 6e 20 6b 65 79 31  ng rowid on key1
622e0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41   */.#define UNPA
622f0 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 20 20 20  CKED_INCRKEY    
62300 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 4d 61     0x0008  /* Ma
62310 6b 65 20 74 68 69 73 20 6b 65 79 20 61 6e 20 65  ke this key an e
62320 70 73 69 6c 6f 6e 20 6c 61 72 67 65 72 20 2a 2f  psilon larger */
62330 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45  .#define UNPACKE
62340 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 20 20  D_PREFIX_MATCH  
62350 30 78 30 30 31 30 20 20 2f 2a 20 41 20 70 72 65  0x0010  /* A pre
62360 66 69 78 20 6d 61 74 63 68 20 69 73 20 63 6f 6e  fix match is con
62370 73 69 64 65 72 65 64 20 4f 4b 20 2a 2f 0a 23 64  sidered OK */.#d
62380 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 50  efine UNPACKED_P
62390 52 45 46 49 58 5f 53 45 41 52 43 48 20 30 78 30  REFIX_SEARCH 0x0
623a0 30 32 30 20 20 2f 2a 20 41 20 70 72 65 66 69 78  020  /* A prefix
623b0 20 6d 61 74 63 68 20 69 73 20 63 6f 6e 73 69 64   match is consid
623c0 65 72 65 64 20 4f 4b 20 2a 2f 0a 0a 2f 2a 0a 2a  ered OK */../*.*
623d0 2a 20 45 61 63 68 20 53 51 4c 20 69 6e 64 65 78  * Each SQL index
623e0 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64 20   is represented 
623f0 69 6e 20 6d 65 6d 6f 72 79 20 62 79 20 61 6e 0a  in memory by an.
62400 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  ** instance of t
62410 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
62420 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  ucture..**.** Th
62430 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65  e columns of the
62440 20 74 61 62 6c 65 20 74 68 61 74 20 61 72 65 20   table that are 
62450 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 61 72  to be indexed ar
62460 65 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 62  e described.** b
62470 79 20 74 68 65 20 61 69 43 6f 6c 75 6d 6e 5b 5d  y the aiColumn[]
62480 20 66 69 65 6c 64 20 6f 66 20 74 68 69 73 20 73   field of this s
62490 74 72 75 63 74 75 72 65 2e 20 20 46 6f 72 20 65  tructure.  For e
624a0 78 61 6d 70 6c 65 2c 20 73 75 70 70 6f 73 65 0a  xample, suppose.
624b0 2a 2a 20 77 65 20 68 61 76 65 20 74 68 65 20 66  ** we have the f
624c0 6f 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 61  ollowing table a
624d0 6e 64 20 69 6e 64 65 78 3a 0a 2a 2a 0a 2a 2a 20  nd index:.**.** 
624e0 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
624f0 20 45 78 31 28 63 31 20 69 6e 74 2c 20 63 32 20   Ex1(c1 int, c2 
62500 69 6e 74 2c 20 63 33 20 74 65 78 74 29 3b 0a 2a  int, c3 text);.*
62510 2a 20 20 20 20 20 43 52 45 41 54 45 20 49 4e 44  *     CREATE IND
62520 45 58 20 45 78 32 20 4f 4e 20 45 78 31 28 63 33  EX Ex2 ON Ex1(c3
62530 2c 63 31 29 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 74  ,c1);.**.** In t
62540 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
62550 72 65 20 64 65 73 63 72 69 62 69 6e 67 20 45 78  re describing Ex
62560 31 2c 20 6e 43 6f 6c 3d 3d 33 20 62 65 63 61 75  1, nCol==3 becau
62570 73 65 20 74 68 65 72 65 20 61 72 65 0a 2a 2a 20  se there are.** 
62580 74 68 72 65 65 20 63 6f 6c 75 6d 6e 73 20 69 6e  three columns in
62590 20 74 68 65 20 74 61 62 6c 65 2e 20 20 49 6e 20   the table.  In 
625a0 74 68 65 20 49 6e 64 65 78 20 73 74 72 75 63 74  the Index struct
625b0 75 72 65 20 64 65 73 63 72 69 62 69 6e 67 0a 2a  ure describing.*
625c0 2a 20 45 78 32 2c 20 6e 43 6f 6c 75 6d 6e 3d 3d  * Ex2, nColumn==
625d0 32 20 73 69 6e 63 65 20 32 20 6f 66 20 74 68 65  2 since 2 of the
625e0 20 33 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 45 78   3 columns of Ex
625f0 31 20 61 72 65 20 69 6e 64 65 78 65 64 2e 0a 2a  1 are indexed..*
62600 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 61  * The value of a
62610 69 43 6f 6c 75 6d 6e 20 69 73 20 7b 32 2c 20 30  iColumn is {2, 0
62620 7d 2e 20 20 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d  }.  aiColumn[0]=
62630 3d 32 20 62 65 63 61 75 73 65 20 74 68 65 20 0a  =2 because the .
62640 2a 2a 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20  ** first column 
62650 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 28 63  to be indexed (c
62660 33 29 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20  3) has an index 
62670 6f 66 20 32 20 69 6e 20 45 78 31 2e 61 43 6f 6c  of 2 in Ex1.aCol
62680 5b 5d 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e  []..** The secon
62690 64 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 69  d column to be i
626a0 6e 64 65 78 65 64 20 28 63 31 29 20 68 61 73 20  ndexed (c1) has 
626b0 61 6e 20 69 6e 64 65 78 20 6f 66 20 30 20 69 6e  an index of 0 in
626c0 0a 2a 2a 20 45 78 31 2e 61 43 6f 6c 5b 5d 2c 20  .** Ex1.aCol[], 
626d0 68 65 6e 63 65 20 45 78 32 2e 61 69 43 6f 6c 75  hence Ex2.aiColu
626e0 6d 6e 5b 31 5d 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20  mn[1]==0..**.** 
626f0 54 68 65 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f  The Index.onErro
62700 72 20 66 69 65 6c 64 20 64 65 74 65 72 6d 69 6e  r field determin
62710 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  es whether or no
62720 74 20 74 68 65 20 69 6e 64 65 78 65 64 20 63 6f  t the indexed co
62730 6c 75 6d 6e 73 0a 2a 2a 20 6d 75 73 74 20 62 65  lumns.** must be
62740 20 75 6e 69 71 75 65 20 61 6e 64 20 77 68 61 74   unique and what
62750 20 74 6f 20 64 6f 20 69 66 20 74 68 65 79 20 61   to do if they a
62760 72 65 20 6e 6f 74 2e 20 20 57 68 65 6e 20 49 6e  re not.  When In
62770 64 65 78 2e 6f 6e 45 72 72 6f 72 3d 4f 45 5f 4e  dex.onError=OE_N
62780 6f 6e 65 2c 0a 2a 2a 20 69 74 20 6d 65 61 6e 73  one,.** it means
62790 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 20 75   this is not a u
627a0 6e 69 71 75 65 20 69 6e 64 65 78 2e 20 20 4f 74  nique index.  Ot
627b0 68 65 72 77 69 73 65 20 69 74 20 69 73 20 61 20  herwise it is a 
627c0 75 6e 69 71 75 65 20 69 6e 64 65 78 0a 2a 2a 20  unique index.** 
627d0 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66  and the value of
627e0 20 49 6e 64 65 78 2e 6f 6e 45 72 72 6f 72 20 69   Index.onError i
627f0 6e 64 69 63 61 74 65 20 74 68 65 20 77 68 69 63  ndicate the whic
62800 68 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c  h conflict resol
62810 75 74 69 6f 6e 20 0a 2a 2a 20 61 6c 67 6f 72 69  ution .** algori
62820 74 68 6d 20 74 6f 20 65 6d 70 6c 6f 79 20 77 68  thm to employ wh
62830 65 6e 65 76 65 72 20 61 6e 20 61 74 74 65 6d 70  enever an attemp
62840 74 20 69 73 20 6d 61 64 65 20 74 6f 20 69 6e 73  t is made to ins
62850 65 72 74 20 61 20 6e 6f 6e 2d 75 6e 69 71 75 65  ert a non-unique
62860 0a 2a 2a 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a  .** element..*/.
62870 73 74 72 75 63 74 20 49 6e 64 65 78 20 7b 0a 20  struct Index {. 
62880 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
62890 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69    /* Name of thi
628a0 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74  s index */.  int
628b0 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 2f 2a   nColumn;     /*
628c0 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
628d0 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ns in the table 
628e0 75 73 65 64 20 62 79 20 74 68 69 73 20 69 6e 64  used by this ind
628f0 65 78 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 43  ex */.  int *aiC
62900 6f 6c 75 6d 6e 3b 20 20 20 2f 2a 20 57 68 69 63  olumn;   /* Whic
62910 68 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75 73  h columns are us
62920 65 64 20 62 79 20 74 68 69 73 20 69 6e 64 65 78  ed by this index
62930 2e 20 20 31 73 74 20 69 73 20 30 20 2a 2f 0a 20  .  1st is 0 */. 
62940 20 75 6e 73 69 67 6e 65 64 20 2a 61 69 52 6f 77   unsigned *aiRow
62950 45 73 74 3b 20 2f 2a 20 52 65 73 75 6c 74 20 6f  Est; /* Result o
62960 66 20 41 4e 41 4c 59 5a 45 3a 20 45 73 74 2e 20  f ANALYZE: Est. 
62970 72 6f 77 73 20 73 65 6c 65 63 74 65 64 20 62 79  rows selected by
62980 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a   each column */.
62990 20 20 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 3b    Table *pTable;
629a0 20 20 20 2f 2a 20 54 68 65 20 53 51 4c 20 74 61     /* The SQL ta
629b0 62 6c 65 20 62 65 69 6e 67 20 69 6e 64 65 78 65  ble being indexe
629c0 64 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b  d */.  int tnum;
629d0 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
629e0 63 6f 6e 74 61 69 6e 69 6e 67 20 72 6f 6f 74 20  containing root 
629f0 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20 69 6e  of this index in
62a00 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
62a10 2f 0a 20 20 75 38 20 6f 6e 45 72 72 6f 72 3b 20  /.  u8 onError; 
62a20 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72 74       /* OE_Abort
62a30 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45 5f  , OE_Ignore, OE_
62a40 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f 4e  Replace, or OE_N
62a50 6f 6e 65 20 2a 2f 0a 20 20 75 38 20 61 75 74 6f  one */.  u8 auto
62a60 49 6e 64 65 78 3b 20 20 20 20 2f 2a 20 54 72 75  Index;    /* Tru
62a70 65 20 69 66 20 69 73 20 61 75 74 6f 6d 61 74 69  e if is automati
62a80 63 61 6c 6c 79 20 63 72 65 61 74 65 64 20 28 65  cally created (e
62a90 78 3a 20 62 79 20 55 4e 49 51 55 45 29 20 2a 2f  x: by UNIQUE) */
62aa0 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41 66 66  .  char *zColAff
62ab0 3b 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 64 65  ;   /* String de
62ac0 66 69 6e 69 6e 67 20 74 68 65 20 61 66 66 69 6e  fining the affin
62ad0 69 74 79 20 6f 66 20 65 61 63 68 20 63 6f 6c 75  ity of each colu
62ae0 6d 6e 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70  mn */.  Index *p
62af0 4e 65 78 74 3b 20 20 20 20 2f 2a 20 54 68 65 20  Next;    /* The 
62b00 6e 65 78 74 20 69 6e 64 65 78 20 61 73 73 6f 63  next index assoc
62b10 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73  iated with the s
62b20 61 6d 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 53  ame table */.  S
62b30 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 3b 20  chema *pSchema; 
62b40 2f 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 69  /* Schema contai
62b50 6e 69 6e 67 20 74 68 69 73 20 69 6e 64 65 78 20  ning this index 
62b60 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72 74 4f 72  */.  u8 *aSortOr
62b70 64 65 72 3b 20 20 2f 2a 20 41 72 72 61 79 20 6f  der;  /* Array o
62b80 66 20 73 69 7a 65 20 49 6e 64 65 78 2e 6e 43 6f  f size Index.nCo
62b90 6c 75 6d 6e 2e 20 54 72 75 65 3d 3d 44 45 53 43  lumn. True==DESC
62ba0 2c 20 46 61 6c 73 65 3d 3d 41 53 43 20 2a 2f 0a  , False==ASC */.
62bb0 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 6c 3b    char **azColl;
62bc0 20 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 63     /* Array of c
62bd0 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
62be0 65 20 6e 61 6d 65 73 20 66 6f 72 20 69 6e 64 65  e names for inde
62bf0 78 20 2a 2f 0a 20 20 49 6e 64 65 78 53 61 6d 70  x */.  IndexSamp
62c00 6c 65 20 2a 61 53 61 6d 70 6c 65 3b 20 20 20 20  le *aSample;    
62c10 2f 2a 20 41 72 72 61 79 20 6f 66 20 53 51 4c 49  /* Array of SQLI
62c20 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53  TE_INDEX_SAMPLES
62c30 20 73 61 6d 70 6c 65 73 20 2a 2f 0a 7d 3b 0a 0a   samples */.};..
62c40 2f 2a 0a 2a 2a 20 45 61 63 68 20 73 61 6d 70 6c  /*.** Each sampl
62c50 65 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  e stored in the 
62c60 73 71 6c 69 74 65 5f 73 74 61 74 32 20 74 61 62  sqlite_stat2 tab
62c70 6c 65 20 69 73 20 72 65 70 72 65 73 65 6e 74 65  le is represente
62c80 64 20 69 6e 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20  d in memory .** 
62c90 75 73 69 6e 67 20 61 20 73 74 72 75 63 74 75 72  using a structur
62ca0 65 20 6f 66 20 74 68 69 73 20 74 79 70 65 2e 0a  e of this type..
62cb0 2a 2f 0a 73 74 72 75 63 74 20 49 6e 64 65 78 53  */.struct IndexS
62cc0 61 6d 70 6c 65 20 7b 0a 20 20 75 6e 69 6f 6e 20  ample {.  union 
62cd0 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b 20 20  {.    char *z;  
62ce0 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 69        /* Value i
62cf0 66 20 65 54 79 70 65 20 69 73 20 53 51 4c 49 54  f eType is SQLIT
62d00 45 5f 54 45 58 54 20 6f 72 20 53 51 4c 49 54 45  E_TEXT or SQLITE
62d10 5f 42 4c 4f 42 20 2a 2f 0a 20 20 20 20 64 6f 75  _BLOB */.    dou
62d20 62 6c 65 20 72 3b 20 20 20 20 20 20 20 2f 2a 20  ble r;       /* 
62d30 56 61 6c 75 65 20 69 66 20 65 54 79 70 65 20 69  Value if eType i
62d40 73 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54 20 6f  s SQLITE_FLOAT o
62d50 72 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  r SQLITE_INTEGER
62d60 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20 75 38 20   */.  } u;.  u8 
62d70 65 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 2f  eType;         /
62d80 2a 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 53  * SQLITE_NULL, S
62d90 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 2e 2e  QLITE_INTEGER ..
62da0 2e 20 65 74 63 2e 20 2a 2f 0a 20 20 75 38 20 6e  . etc. */.  u8 n
62db0 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 2f 2a  Byte;         /*
62dc0 20 53 69 7a 65 20 69 6e 20 62 79 74 65 20 6f 66   Size in byte of
62dd0 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2e 20 2a   text or blob. *
62de0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68  /.};../*.** Each
62df0 20 74 6f 6b 65 6e 20 63 6f 6d 69 6e 67 20 6f 75   token coming ou
62e00 74 20 6f 66 20 74 68 65 20 6c 65 78 65 72 20 69  t of the lexer i
62e10 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  s an instance of
62e20 0a 2a 2a 20 74 68 69 73 20 73 74 72 75 63 74 75  .** this structu
62e30 72 65 2e 20 20 54 6f 6b 65 6e 73 20 61 72 65 20  re.  Tokens are 
62e40 61 6c 73 6f 20 75 73 65 64 20 61 73 20 70 61 72  also used as par
62e50 74 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69  t of an expressi
62e60 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 69  on..**.** Note i
62e70 66 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 20 74 68 65  f Token.z==0 the
62e80 6e 20 54 6f 6b 65 6e 2e 64 79 6e 20 61 6e 64 20  n Token.dyn and 
62e90 54 6f 6b 65 6e 2e 6e 20 61 72 65 20 75 6e 64 65  Token.n are unde
62ea0 66 69 6e 65 64 20 61 6e 64 0a 2a 2a 20 6d 61 79  fined and.** may
62eb0 20 63 6f 6e 74 61 69 6e 20 72 61 6e 64 6f 6d 20   contain random 
62ec0 76 61 6c 75 65 73 2e 20 20 44 6f 20 6e 6f 74 20  values.  Do not 
62ed0 6d 61 6b 65 20 61 6e 79 20 61 73 73 75 6d 70 74  make any assumpt
62ee0 69 6f 6e 73 20 61 62 6f 75 74 20 54 6f 6b 65 6e  ions about Token
62ef0 2e 64 79 6e 0a 2a 2a 20 61 6e 64 20 54 6f 6b 65  .dyn.** and Toke
62f00 6e 2e 6e 20 77 68 65 6e 20 54 6f 6b 65 6e 2e 7a  n.n when Token.z
62f10 3d 3d 30 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 54  ==0..*/.struct T
62f20 6f 6b 65 6e 20 7b 0a 20 20 63 6f 6e 73 74 20 63  oken {.  const c
62f30 68 61 72 20 2a 7a 3b 20 20 20 20 20 2f 2a 20 54  har *z;     /* T
62f40 65 78 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  ext of the token
62f50 2e 20 20 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d  .  Not NULL-term
62f60 69 6e 61 74 65 64 21 20 2a 2f 0a 20 20 75 6e 73  inated! */.  uns
62f70 69 67 6e 65 64 20 69 6e 74 20 6e 3b 20 20 20 20  igned int n;    
62f80 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61  /* Number of cha
62f90 72 61 63 74 65 72 73 20 69 6e 20 74 68 69 73 20  racters in this 
62fa0 74 6f 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  token */.};../*.
62fb0 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
62fc0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
62fd0 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d   contains inform
62fe0 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20  ation needed to 
62ff0 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65  generate.** code
63000 20 66 6f 72 20 61 20 53 45 4c 45 43 54 20 74 68   for a SELECT th
63010 61 74 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72  at contains aggr
63020 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e  egate functions.
63030 0a 2a 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e 6f  .**.** If Expr.o
63040 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  p==TK_AGG_COLUMN
63050 20 6f 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54   or TK_AGG_FUNCT
63060 49 4f 4e 20 74 68 65 6e 20 45 78 70 72 2e 70 41  ION then Expr.pA
63070 67 67 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20 70  ggInfo is a.** p
63080 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73  ointer to this s
63090 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 45  tructure.  The E
630a0 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65 6c  xpr.iColumn fiel
630b0 64 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 69  d is the index i
630c0 6e 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43 6f  n.** AggInfo.aCo
630d0 6c 5b 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e 61  l[] or AggInfo.a
630e0 46 75 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d  Func[] of inform
630f0 61 74 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20  ation needed to 
63100 67 65 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65  generate.** code
63110 20 66 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e 0a   for that node..
63120 2a 2a 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70 47  **.** AggInfo.pG
63130 72 6f 75 70 42 79 20 61 6e 64 20 41 67 67 49 6e  roupBy and AggIn
63140 66 6f 2e 61 46 75 6e 63 2e 70 45 78 70 72 20 70  fo.aFunc.pExpr p
63150 6f 69 6e 74 20 74 6f 20 66 69 65 6c 64 73 20 77  oint to fields w
63160 69 74 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72 69  ithin the.** ori
63170 67 69 6e 61 6c 20 53 65 6c 65 63 74 20 73 74 72  ginal Select str
63180 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63  ucture that desc
63190 72 69 62 65 73 20 74 68 65 20 53 45 4c 45 43 54  ribes the SELECT
631a0 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65   statement.  The
631b0 73 65 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f 20  se.** fields do 
631c0 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 66  not need to be f
631d0 72 65 65 64 20 77 68 65 6e 20 64 65 61 6c 6c 6f  reed when deallo
631e0 63 61 74 69 6e 67 20 74 68 65 20 41 67 67 49 6e  cating the AggIn
631f0 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  fo structure..*/
63200 0a 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20  .struct AggInfo 
63210 7b 0a 20 20 75 38 20 64 69 72 65 63 74 4d 6f 64  {.  u8 directMod
63220 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  e;          /* D
63230 69 72 65 63 74 20 72 65 6e 64 65 72 69 6e 67 20  irect rendering 
63240 6d 6f 64 65 20 6d 65 61 6e 73 20 74 61 6b 65 20  mode means take 
63250 64 61 74 61 20 64 69 72 65 63 74 6c 79 0a 20 20  data directly.  
63260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63270 20 20 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20          ** from 
63280 73 6f 75 72 63 65 20 74 61 62 6c 65 73 20 72 61  source tables ra
63290 74 68 65 72 20 74 68 61 6e 20 66 72 6f 6d 20 61  ther than from a
632a0 63 63 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a 20  ccumulators */. 
632b0 20 75 38 20 75 73 65 53 6f 72 74 69 6e 67 49 64   u8 useSortingId
632c0 78 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 64  x;       /* In d
632d0 69 72 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 65  irect mode, refe
632e0 72 65 6e 63 65 20 74 68 65 20 73 6f 72 74 69 6e  rence the sortin
632f0 67 20 69 6e 64 65 78 20 72 61 74 68 65 72 0a 20  g index rather. 
63300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63310 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e           ** than
63320 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c   the source tabl
63330 65 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 69  e */.  int sorti
63340 6e 67 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f  ngIdx;         /
63350 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  * Cursor number 
63360 6f 66 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69  of the sorting i
63370 6e 64 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c 69  ndex */.  ExprLi
63380 73 74 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20  st *pGroupBy;   
63390 20 20 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 62    /* The group b
633a0 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e  y clause */.  in
633b0 74 20 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e  t nSortingColumn
633c0 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  ;     /* Number 
633d0 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
633e0 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20  e sorting index 
633f0 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 67 67 49  */.  struct AggI
63400 6e 66 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a 20  nfo_col {    /* 
63410 46 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20  For each column 
63420 75 73 65 64 20 69 6e 20 73 6f 75 72 63 65 20 74  used in source t
63430 61 62 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61 62  ables */.    Tab
63440 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20  le *pTab;       
63450 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20        /* Source 
63460 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74  table */.    int
63470 20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20   iTable;        
63480 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
63490 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f  number of the so
634a0 75 72 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20  urce table */.  
634b0 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20    int iColumn;  
634c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
634d0 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68  lumn number with
634e0 69 6e 20 74 68 65 20 73 6f 75 72 63 65 20 74 61  in the source ta
634f0 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ble */.    int i
63500 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20 20  SorterColumn;   
63510 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75      /* Column nu
63520 6d 62 65 72 20 69 6e 20 74 68 65 20 73 6f 72 74  mber in the sort
63530 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20  ing index */.   
63540 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20   int iMem;      
63550 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d            /* Mem
63560 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61  ory location tha
63570 74 20 61 63 74 73 20 61 73 20 61 63 63 75 6d 75  t acts as accumu
63580 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70  lator */.    Exp
63590 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20  r *pExpr;       
635a0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69        /* The ori
635b0 67 69 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e  ginal expression
635c0 20 2a 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20   */.  } *aCol;. 
635d0 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20   int nColumn;   
635e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
635f0 65 72 20 6f 66 20 75 73 65 64 20 65 6e 74 72 69  er of used entri
63600 65 73 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a  es in aCol[] */.
63610 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c    int nColumnAll
63620 6f 63 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  oc;       /* Num
63630 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c  ber of slots all
63640 6f 63 61 74 65 64 20 66 6f 72 20 61 43 6f 6c 5b  ocated for aCol[
63650 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75  ] */.  int nAccu
63660 6d 75 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f  mulator;       /
63670 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  * Number of colu
63680 6d 6e 73 20 74 68 61 74 20 73 68 6f 77 20 74 68  mns that show th
63690 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74  rough to the out
636a0 70 75 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20  put..           
636b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
636c0 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c  * Additional col
636d0 75 6d 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e  umns are used on
636e0 6c 79 20 61 73 20 70 61 72 61 6d 65 74 65 72 73  ly as parameters
636f0 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20   to.            
63700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
63710 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
63720 69 6f 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74  ions */.  struct
63730 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20   AggInfo_func { 
63740 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67    /* For each ag
63750 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
63760 20 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45   */.    Expr *pE
63770 78 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  xpr;            
63780 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65   /* Expression e
63790 6e 63 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63  ncoding the func
637a0 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63  tion */.    Func
637b0 44 65 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20  Def *pFunc;     
637c0 20 20 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72       /* The aggr
637d0 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69  egate function i
637e0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f  mplementation */
637f0 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20  .    int iMem;  
63800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
63810 20 4d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e   Memory location
63820 20 74 68 61 74 20 61 63 74 73 20 61 73 20 61 63   that acts as ac
63830 63 75 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20  cumulator */.   
63840 20 69 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20   int iDistinct; 
63850 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68            /* Eph
63860 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65  emeral table use
63870 64 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53  d to enforce DIS
63880 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46  TINCT */.  } *aF
63890 75 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63  unc;.  int nFunc
638a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
638b0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
638c0 69 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a  ies in aFunc[] *
638d0 2f 0a 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c 6c  /.  int nFuncAll
638e0 6f 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  oc;         /* N
638f0 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61  umber of slots a
63900 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 46 75  llocated for aFu
63910 6e 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  nc[] */.};../*.*
63920 2a 20 45 61 63 68 20 6e 6f 64 65 20 6f 66 20 61  * Each node of a
63930 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20  n expression in 
63940 74 68 65 20 70 61 72 73 65 20 74 72 65 65 20 69  the parse tree i
63950 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a  s an instance.**
63960 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
63970 72 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f  re..**.** Expr.o
63980 70 20 69 73 20 74 68 65 20 6f 70 63 6f 64 65 2e  p is the opcode.
63990 20 54 68 65 20 69 6e 74 65 67 65 72 20 70 61 72   The integer par
639a0 73 65 72 20 74 6f 6b 65 6e 20 63 6f 64 65 73 20  ser token codes 
639b0 61 72 65 20 72 65 75 73 65 64 0a 2a 2a 20 61 73  are reused.** as
639c0 20 6f 70 63 6f 64 65 73 20 68 65 72 65 2e 20 46   opcodes here. F
639d0 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20  or example, the 
639e0 70 61 72 73 65 72 20 64 65 66 69 6e 65 73 20 54  parser defines T
639f0 4b 5f 47 45 20 74 6f 20 62 65 20 61 6e 20 69 6e  K_GE to be an in
63a00 74 65 67 65 72 0a 2a 2a 20 63 6f 64 65 20 72 65  teger.** code re
63a10 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20 22  presenting the "
63a20 3e 3d 22 20 6f 70 65 72 61 74 6f 72 2e 20 54 68  >=" operator. Th
63a30 69 73 20 73 61 6d 65 20 69 6e 74 65 67 65 72 20  is same integer 
63a40 63 6f 64 65 20 69 73 20 72 65 75 73 65 64 0a 2a  code is reused.*
63a50 2a 20 74 6f 20 72 65 70 72 65 73 65 6e 74 20 74  * to represent t
63a60 68 65 20 67 72 65 61 74 65 72 2d 74 68 61 6e 2d  he greater-than-
63a70 6f 72 2d 65 71 75 61 6c 2d 74 6f 20 6f 70 65 72  or-equal-to oper
63a80 61 74 6f 72 20 69 6e 20 74 68 65 20 65 78 70 72  ator in the expr
63a90 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65 2e 0a  ession.** tree..
63aa0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70  **.** If the exp
63ab0 72 65 73 73 69 6f 6e 20 69 73 20 61 6e 20 53 51  ression is an SQ
63ac0 4c 20 6c 69 74 65 72 61 6c 20 28 54 4b 5f 49 4e  L literal (TK_IN
63ad0 54 45 47 45 52 2c 20 54 4b 5f 46 4c 4f 41 54 2c  TEGER, TK_FLOAT,
63ae0 20 54 4b 5f 42 4c 4f 42 2c 20 0a 2a 2a 20 6f 72   TK_BLOB, .** or
63af0 20 54 4b 5f 53 54 52 49 4e 47 29 2c 20 74 68 65   TK_STRING), the
63b00 6e 20 45 78 70 72 2e 74 6f 6b 65 6e 20 63 6f 6e  n Expr.token con
63b10 74 61 69 6e 73 20 74 68 65 20 74 65 78 74 20 6f  tains the text o
63b20 66 20 74 68 65 20 53 51 4c 20 6c 69 74 65 72 61  f the SQL litera
63b30 6c 2e 20 49 66 0a 2a 2a 20 74 68 65 20 65 78 70  l. If.** the exp
63b40 72 65 73 73 69 6f 6e 20 69 73 20 61 20 76 61 72  ression is a var
63b50 69 61 62 6c 65 20 28 54 4b 5f 56 41 52 49 41 42  iable (TK_VARIAB
63b60 4c 45 29 2c 20 74 68 65 6e 20 45 78 70 72 2e 74  LE), then Expr.t
63b70 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68  oken contains th
63b80 65 20 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 6e  e .** variable n
63b90 61 6d 65 2e 20 46 69 6e 61 6c 6c 79 2c 20 69 66  ame. Finally, if
63ba0 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
63bb0 69 73 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69  is an SQL functi
63bc0 6f 6e 20 28 54 4b 5f 46 55 4e 43 54 49 4f 4e 29  on (TK_FUNCTION)
63bd0 2c 0a 2a 2a 20 74 68 65 6e 20 45 78 70 72 2e 74  ,.** then Expr.t
63be0 6f 6b 65 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68  oken contains th
63bf0 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75  e name of the fu
63c00 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 45 78  nction..**.** Ex
63c10 70 72 2e 70 52 69 67 68 74 20 61 6e 64 20 45 78  pr.pRight and Ex
63c20 70 72 2e 70 4c 65 66 74 20 61 72 65 20 74 68 65  pr.pLeft are the
63c30 20 6c 65 66 74 20 61 6e 64 20 72 69 67 68 74 20   left and right 
63c40 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73 20 6f  subexpressions o
63c50 66 20 61 0a 2a 2a 20 62 69 6e 61 72 79 20 6f 70  f a.** binary op
63c60 65 72 61 74 6f 72 2e 20 45 69 74 68 65 72 20 6f  erator. Either o
63c70 72 20 62 6f 74 68 20 6d 61 79 20 62 65 20 4e 55  r both may be NU
63c80 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 78  LL..**.** Expr.x
63c90 2e 70 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74  .pList is a list
63ca0 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 69 66   of arguments if
63cb0 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
63cc0 69 73 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69  is an SQL functi
63cd0 6f 6e 2c 0a 2a 2a 20 61 20 43 41 53 45 20 65 78  on,.** a CASE ex
63ce0 70 72 65 73 73 69 6f 6e 20 6f 72 20 61 6e 20 49  pression or an I
63cf0 4e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 66 20  N expression of 
63d00 74 68 65 20 66 6f 72 6d 20 22 3c 6c 68 73 3e 20  the form "<lhs> 
63d10 49 4e 20 28 3c 79 3e 2c 20 3c 7a 3e 2e 2e 2e 29  IN (<y>, <z>...)
63d20 22 2e 0a 2a 2a 20 45 78 70 72 2e 78 2e 70 53 65  "..** Expr.x.pSe
63d30 6c 65 63 74 20 69 73 20 75 73 65 64 20 69 66 20  lect is used if 
63d40 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
63d50 73 20 61 20 73 75 62 2d 73 65 6c 65 63 74 20 6f  s a sub-select o
63d60 72 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  r an expression 
63d70 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 72 6d 20 22  of.** the form "
63d80 3c 6c 68 73 3e 20 49 4e 20 28 53 45 4c 45 43 54  <lhs> IN (SELECT
63d90 20 2e 2e 2e 29 22 2e 20 49 66 20 74 68 65 20 45   ...)". If the E
63da0 50 5f 78 49 73 53 65 6c 65 63 74 20 62 69 74 20  P_xIsSelect bit 
63db0 69 73 20 73 65 74 20 69 6e 20 74 68 65 0a 2a 2a  is set in the.**
63dc0 20 45 78 70 72 2e 66 6c 61 67 73 20 6d 61 73 6b   Expr.flags mask
63dd0 2c 20 74 68 65 6e 20 45 78 70 72 2e 78 2e 70 53  , then Expr.x.pS
63de0 65 6c 65 63 74 20 69 73 20 76 61 6c 69 64 2e 20  elect is valid. 
63df0 4f 74 68 65 72 77 69 73 65 2c 20 45 78 70 72 2e  Otherwise, Expr.
63e00 78 2e 70 4c 69 73 74 20 69 73 20 0a 2a 2a 20 76  x.pList is .** v
63e10 61 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65  alid..**.** An e
63e20 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65  xpression of the
63e30 20 66 6f 72 6d 20 49 44 20 6f 72 20 49 44 2e 49   form ID or ID.I
63e40 44 20 72 65 66 65 72 73 20 74 6f 20 61 20 63 6f  D refers to a co
63e50 6c 75 6d 6e 20 69 6e 20 61 20 74 61 62 6c 65 2e  lumn in a table.
63e60 0a 2a 2a 20 46 6f 72 20 73 75 63 68 20 65 78 70  .** For such exp
63e70 72 65 73 73 69 6f 6e 73 2c 20 45 78 70 72 2e 6f  ressions, Expr.o
63e80 70 20 69 73 20 73 65 74 20 74 6f 20 54 4b 5f 43  p is set to TK_C
63e90 4f 4c 55 4d 4e 20 61 6e 64 20 45 78 70 72 2e 69  OLUMN and Expr.i
63ea0 54 61 62 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20  Table is.** the 
63eb0 69 6e 74 65 67 65 72 20 63 75 72 73 6f 72 20 6e  integer cursor n
63ec0 75 6d 62 65 72 20 6f 66 20 61 20 56 44 42 45 20  umber of a VDBE 
63ed0 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20  cursor pointing 
63ee0 74 6f 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e  to that table an
63ef0 64 0a 2a 2a 20 45 78 70 72 2e 69 43 6f 6c 75 6d  d.** Expr.iColum
63f00 6e 20 69 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20  n is the column 
63f10 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 73  number for the s
63f20 70 65 63 69 66 69 63 20 63 6f 6c 75 6d 6e 2e 20  pecific column. 
63f30 20 49 66 20 74 68 65 0a 2a 2a 20 65 78 70 72 65   If the.** expre
63f40 73 73 69 6f 6e 20 69 73 20 75 73 65 64 20 61 73  ssion is used as
63f50 20 61 20 72 65 73 75 6c 74 20 69 6e 20 61 6e 20   a result in an 
63f60 61 67 67 72 65 67 61 74 65 20 53 45 4c 45 43 54  aggregate SELECT
63f70 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 76 61  , then the.** va
63f80 6c 75 65 20 69 73 20 61 6c 73 6f 20 73 74 6f 72  lue is also stor
63f90 65 64 20 69 6e 20 74 68 65 20 45 78 70 72 2e 69  ed in the Expr.i
63fa0 41 67 67 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68  Agg column in th
63fb0 65 20 61 67 67 72 65 67 61 74 65 20 73 6f 20 74  e aggregate so t
63fc0 68 61 74 0a 2a 2a 20 69 74 20 63 61 6e 20 62 65  hat.** it can be
63fd0 20 61 63 63 65 73 73 65 64 20 61 66 74 65 72 20   accessed after 
63fe0 61 6c 6c 20 61 67 67 72 65 67 61 74 65 73 20 61  all aggregates a
63ff0 72 65 20 63 6f 6d 70 75 74 65 64 2e 0a 2a 2a 0a  re computed..**.
64000 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73  ** If the expres
64010 73 69 6f 6e 20 69 73 20 61 6e 20 75 6e 62 6f 75  sion is an unbou
64020 6e 64 20 76 61 72 69 61 62 6c 65 20 6d 61 72 6b  nd variable mark
64030 65 72 20 28 61 20 71 75 65 73 74 69 6f 6e 20 6d  er (a question m
64040 61 72 6b 20 0a 2a 2a 20 63 68 61 72 61 63 74 65  ark .** characte
64050 72 20 27 3f 27 20 69 6e 20 74 68 65 20 6f 72 69  r '?' in the ori
64060 67 69 6e 61 6c 20 53 51 4c 29 20 74 68 65 6e 20  ginal SQL) then 
64070 74 68 65 20 45 78 70 72 2e 69 54 61 62 6c 65 20  the Expr.iTable 
64080 68 6f 6c 64 73 20 74 68 65 20 69 6e 64 65 78 20  holds the index 
64090 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  .** number for t
640a0 68 61 74 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a  hat variable..**
640b0 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65  .** If the expre
640c0 73 73 69 6f 6e 20 69 73 20 61 20 73 75 62 71 75  ssion is a subqu
640d0 65 72 79 20 74 68 65 6e 20 45 78 70 72 2e 69 43  ery then Expr.iC
640e0 6f 6c 75 6d 6e 20 68 6f 6c 64 73 20 61 6e 20 69  olumn holds an i
640f0 6e 74 65 67 65 72 0a 2a 2a 20 72 65 67 69 73 74  nteger.** regist
64100 65 72 20 6e 75 6d 62 65 72 20 63 6f 6e 74 61 69  er number contai
64110 6e 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 20  ning the result 
64120 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79 2e  of the subquery.
64130 20 20 49 66 20 74 68 65 0a 2a 2a 20 73 75 62 71    If the.** subq
64140 75 65 72 79 20 67 69 76 65 73 20 61 20 63 6f 6e  uery gives a con
64150 73 74 61 6e 74 20 72 65 73 75 6c 74 2c 20 74 68  stant result, th
64160 65 6e 20 69 54 61 62 6c 65 20 69 73 20 2d 31 2e  en iTable is -1.
64170 20 20 49 66 20 74 68 65 20 73 75 62 71 75 65 72    If the subquer
64180 79 0a 2a 2a 20 67 69 76 65 73 20 61 20 64 69 66  y.** gives a dif
64190 66 65 72 65 6e 74 20 61 6e 73 77 65 72 20 61 74  ferent answer at
641a0 20 64 69 66 66 65 72 65 6e 74 20 74 69 6d 65 73   different times
641b0 20 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e   during statemen
641c0 74 20 70 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20  t processing.** 
641d0 74 68 65 6e 20 69 54 61 62 6c 65 20 69 73 20 74  then iTable is t
641e0 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 61 20  he address of a 
641f0 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20  subroutine that 
64200 63 6f 6d 70 75 74 65 73 20 74 68 65 20 73 75 62  computes the sub
64210 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  query..**.** If 
64220 74 68 65 20 45 78 70 72 20 69 73 20 6f 66 20 74  the Expr is of t
64230 79 70 65 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 61  ype OP_Column, a
64240 6e 64 20 74 68 65 20 74 61 62 6c 65 20 69 74 20  nd the table it 
64250 69 73 20 73 65 6c 65 63 74 69 6e 67 20 66 72 6f  is selecting fro
64260 6d 0a 2a 2a 20 69 73 20 61 20 64 69 73 6b 20 74  m.** is a disk t
64270 61 62 6c 65 20 6f 72 20 74 68 65 20 22 6f 6c 64  able or the "old
64280 2e 2a 22 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  .*" pseudo-table
64290 2c 20 74 68 65 6e 20 70 54 61 62 20 70 6f 69 6e  , then pTab poin
642a0 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 72  ts to the.** cor
642b0 72 65 73 70 6f 6e 64 69 6e 67 20 74 61 62 6c 65  responding table
642c0 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a   definition..**.
642d0 2a 2a 20 41 4c 4c 4f 43 41 54 49 4f 4e 20 4e 4f  ** ALLOCATION NO
642e0 54 45 53 3a 0a 2a 2a 0a 2a 2a 20 45 78 70 72 20  TES:.**.** Expr 
642f0 6f 62 6a 65 63 74 73 20 63 61 6e 20 75 73 65 20  objects can use 
64300 61 20 6c 6f 74 20 6f 66 20 6d 65 6d 6f 72 79 20  a lot of memory 
64310 73 70 61 63 65 20 69 6e 20 64 61 74 61 62 61 73  space in databas
64320 65 20 73 63 68 65 6d 61 2e 20 20 54 6f 0a 2a 2a  e schema.  To.**
64330 20 68 65 6c 70 20 72 65 64 75 63 65 20 6d 65 6d   help reduce mem
64340 6f 72 79 20 72 65 71 75 69 72 65 6d 65 6e 74 73  ory requirements
64350 2c 20 73 6f 6d 65 74 69 6d 65 73 20 61 6e 20 45  , sometimes an E
64360 78 70 72 20 6f 62 6a 65 63 74 20 77 69 6c 6c 20  xpr object will 
64370 62 65 0a 2a 2a 20 74 72 75 6e 63 61 74 65 64 2e  be.** truncated.
64380 20 20 41 6e 64 20 74 6f 20 72 65 64 75 63 65 20    And to reduce 
64390 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 65  the number of me
643a0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  mory allocations
643b0 2c 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 74  , sometimes.** t
643c0 77 6f 20 6f 72 20 6d 6f 72 65 20 45 78 70 72 20  wo or more Expr 
643d0 6f 62 6a 65 63 74 73 20 77 69 6c 6c 20 62 65 20  objects will be 
643e0 73 74 6f 72 65 64 20 69 6e 20 61 20 73 69 6e 67  stored in a sing
643f0 6c 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  le memory alloca
64400 74 69 6f 6e 2c 0a 2a 2a 20 74 6f 67 65 74 68 65  tion,.** togethe
64410 72 20 77 69 74 68 20 45 78 70 72 2e 7a 54 6f 6b  r with Expr.zTok
64420 65 6e 20 73 74 72 69 6e 67 73 2e 0a 2a 2a 0a 2a  en strings..**.*
64430 2a 20 49 66 20 74 68 65 20 45 50 5f 52 65 64 75  * If the EP_Redu
64440 63 65 64 20 61 6e 64 20 45 50 5f 54 6f 6b 65 6e  ced and EP_Token
64450 4f 6e 6c 79 20 66 6c 61 67 73 20 61 72 65 20 73  Only flags are s
64460 65 74 20 77 68 65 6e 0a 2a 2a 20 61 6e 20 45 78  et when.** an Ex
64470 70 72 20 6f 62 6a 65 63 74 20 69 73 20 74 72 75  pr object is tru
64480 6e 63 61 74 65 64 2e 20 20 57 68 65 6e 20 45 50  ncated.  When EP
64490 5f 52 65 64 75 63 65 64 20 69 73 20 73 65 74 2c  _Reduced is set,
644a0 20 74 68 65 6e 20 61 6c 6c 0a 2a 2a 20 74 68 65   then all.** the
644b0 20 63 68 69 6c 64 20 45 78 70 72 20 6f 62 6a 65   child Expr obje
644c0 63 74 73 20 69 6e 20 74 68 65 20 45 78 70 72 2e  cts in the Expr.
644d0 70 4c 65 66 74 20 61 6e 64 20 45 78 70 72 2e 70  pLeft and Expr.p
644e0 52 69 67 68 74 20 73 75 62 74 72 65 65 73 0a 2a  Right subtrees.*
644f0 2a 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20  * are contained 
64500 77 69 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20  within the same 
64510 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
64520 6e 2e 20 20 4e 6f 74 65 2c 20 68 6f 77 65 76 65  n.  Note, howeve
64530 72 2c 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 73  r, that.** the s
64540 75 62 74 72 65 65 73 20 69 6e 20 45 78 70 72 2e  ubtrees in Expr.
64550 78 2e 70 4c 69 73 74 20 6f 72 20 45 78 70 72 2e  x.pList or Expr.
64560 78 2e 70 53 65 6c 65 63 74 20 61 72 65 20 61 6c  x.pSelect are al
64570 77 61 79 73 20 73 65 70 61 72 61 74 65 6c 79 0a  ways separately.
64580 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 72 65  ** allocated, re
64590 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74  gardless of whet
645a0 68 65 72 20 6f 72 20 6e 6f 74 20 45 50 5f 52 65  her or not EP_Re
645b0 64 75 63 65 64 20 69 73 20 73 65 74 2e 0a 2a 2f  duced is set..*/
645c0 0a 73 74 72 75 63 74 20 45 78 70 72 20 7b 0a 20  .struct Expr {. 
645d0 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20   u8 op;         
645e0 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 72 61          /* Opera
645f0 74 69 6f 6e 20 70 65 72 66 6f 72 6d 65 64 20 62  tion performed b
64600 79 20 74 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 20  y this node */. 
64610 20 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 20   char affinity; 
64620 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61          /* The a
64630 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20 63  ffinity of the c
64640 6f 6c 75 6d 6e 20 6f 72 20 30 20 69 66 20 6e 6f  olumn or 0 if no
64650 74 20 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  t a column */.  
64660 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20  u16 flags;      
64670 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69 6f 75         /* Variou
64680 73 20 66 6c 61 67 73 2e 20 20 45 50 5f 2a 20 53  s flags.  EP_* S
64690 65 65 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 75 6e  ee below */.  un
646a0 69 6f 6e 20 7b 0a 20 20 20 20 63 68 61 72 20 2a  ion {.    char *
646b0 7a 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20  zToken;         
646c0 20 2f 2a 20 54 6f 6b 65 6e 20 76 61 6c 75 65 2e   /* Token value.
646d0 20 5a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 64   Zero terminated
646e0 20 61 6e 64 20 64 65 71 75 6f 74 65 64 20 2a 2f   and dequoted */
646f0 0a 20 20 20 20 69 6e 74 20 69 56 61 6c 75 65 3b  .    int iValue;
64700 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
64710 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 66 20  nteger value if 
64720 45 50 5f 49 6e 74 56 61 6c 75 65 20 2a 2f 0a 20  EP_IntValue */. 
64730 20 7d 20 75 3b 0a 0a 20 20 2f 2a 20 49 66 20 74   } u;..  /* If t
64740 68 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20  he EP_TokenOnly 
64750 66 6c 61 67 20 69 73 20 73 65 74 20 69 6e 20 74  flag is set in t
64760 68 65 20 45 78 70 72 2e 66 6c 61 67 73 20 6d 61  he Expr.flags ma
64770 73 6b 2c 20 74 68 65 6e 20 6e 6f 0a 20 20 2a 2a  sk, then no.  **
64780 20 73 70 61 63 65 20 69 73 20 61 6c 6c 6f 63 61   space is alloca
64790 74 65 64 20 66 6f 72 20 74 68 65 20 66 69 65 6c  ted for the fiel
647a0 64 73 20 62 65 6c 6f 77 20 74 68 69 73 20 70 6f  ds below this po
647b0 69 6e 74 2e 20 41 6e 20 61 74 74 65 6d 70 74 20  int. An attempt 
647c0 74 6f 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 74  to.  ** access t
647d0 68 65 6d 20 77 69 6c 6c 20 72 65 73 75 6c 74 20  hem will result 
647e0 69 6e 20 61 20 73 65 67 66 61 75 6c 74 20 6f 72  in a segfault or
647f0 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 20 0a 20   malfunction. . 
64800 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
64810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64840 2a 2a 2a 2a 2a 2a 2f 0a 0a 20 20 45 78 70 72 20  ******/..  Expr 
64850 2a 70 4c 65 66 74 3b 20 20 20 20 20 20 20 20 20  *pLeft;         
64860 20 20 2f 2a 20 4c 65 66 74 20 73 75 62 6e 6f 64    /* Left subnod
64870 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 69  e */.  Expr *pRi
64880 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ght;          /*
64890 20 52 69 67 68 74 20 73 75 62 6e 6f 64 65 20 2a   Right subnode *
648a0 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20  /.  union {.    
648b0 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b  ExprList *pList;
648c0 20 20 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e       /* Function
648d0 20 61 72 67 75 6d 65 6e 74 73 20 6f 72 20 69 6e   arguments or in
648e0 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 3c 65 78   "<expr> IN (<ex
648f0 70 72 2d 6c 69 73 74 29 22 20 2a 2f 0a 20 20 20  pr-list)" */.   
64900 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
64910 3b 20 20 20 20 20 2f 2a 20 55 73 65 64 20 66 6f  ;     /* Used fo
64920 72 20 73 75 62 2d 73 65 6c 65 63 74 73 20 61 6e  r sub-selects an
64930 64 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 3c 73  d "<expr> IN (<s
64940 65 6c 65 63 74 3e 29 22 20 2a 2f 0a 20 20 7d 20  elect>)" */.  } 
64950 78 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43  x;.  CollSeq *pC
64960 6f 6c 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  oll;        /* T
64970 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70  he collation typ
64980 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20  e of the column 
64990 6f 72 20 30 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66  or 0 */..  /* If
649a0 20 74 68 65 20 45 50 5f 52 65 64 75 63 65 64 20   the EP_Reduced 
649b0 66 6c 61 67 20 69 73 20 73 65 74 20 69 6e 20 74  flag is set in t
649c0 68 65 20 45 78 70 72 2e 66 6c 61 67 73 20 6d 61  he Expr.flags ma
649d0 73 6b 2c 20 74 68 65 6e 20 6e 6f 0a 20 20 2a 2a  sk, then no.  **
649e0 20 73 70 61 63 65 20 69 73 20 61 6c 6c 6f 63 61   space is alloca
649f0 74 65 64 20 66 6f 72 20 74 68 65 20 66 69 65 6c  ted for the fiel
64a00 64 73 20 62 65 6c 6f 77 20 74 68 69 73 20 70 6f  ds below this po
64a10 69 6e 74 2e 20 41 6e 20 61 74 74 65 6d 70 74 20  int. An attempt 
64a20 74 6f 0a 20 20 2a 2a 20 61 63 63 65 73 73 20 74  to.  ** access t
64a30 68 65 6d 20 77 69 6c 6c 20 72 65 73 75 6c 74 20  hem will result 
64a40 69 6e 20 61 20 73 65 67 66 61 75 6c 74 20 6f 72  in a segfault or
64a50 20 6d 61 6c 66 75 6e 63 74 69 6f 6e 2e 0a 20 20   malfunction..  
64a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
64aa0 2a 2a 2a 2a 2a 2f 0a 0a 20 20 69 6e 74 20 69 54  *****/..  int iT
64ab0 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20  able;           
64ac0 20 2f 2a 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 63   /* TK_COLUMN: c
64ad0 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20  ursor number of 
64ae0 74 61 62 6c 65 20 68 6f 6c 64 69 6e 67 20 63 6f  table holding co
64af0 6c 75 6d 6e 0a 20 20 20 20 20 20 20 20 20 20 20  lumn.           
64b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
64b10 20 54 4b 5f 52 45 47 49 53 54 45 52 3a 20 72 65   TK_REGISTER: re
64b20 67 69 73 74 65 72 20 6e 75 6d 62 65 72 0a 20 20  gister number.  
64b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64b40 20 20 20 20 20 20 20 2a 2a 20 54 4b 5f 54 52 49         ** TK_TRI
64b50 47 47 45 52 3a 20 31 20 2d 3e 20 6e 65 77 2c 20  GGER: 1 -> new, 
64b60 30 20 2d 3e 20 6f 6c 64 20 2a 2f 0a 20 20 69 31  0 -> old */.  i1
64b70 36 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  6 iColumn;      
64b80 20 20 20 20 20 2f 2a 20 54 4b 5f 43 4f 4c 55 4d       /* TK_COLUM
64b90 4e 3a 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 2e  N: column index.
64ba0 20 20 2d 31 20 66 6f 72 20 72 6f 77 69 64 2e 0a    -1 for rowid..
64bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64bc0 20 20 20 20 20 20 20 20 20 2a 2a 20 54 4b 5f 56           ** TK_V
64bd0 41 52 49 41 42 4c 45 3a 20 76 61 72 69 61 62 6c  ARIABLE: variabl
64be0 65 20 6e 75 6d 62 65 72 20 28 61 6c 77 61 79 73  e number (always
64bf0 20 3e 3d 20 31 29 2e 20 2a 2f 0a 20 20 69 31 36   >= 1). */.  i16
64c00 20 69 41 67 67 3b 20 20 20 20 20 20 20 20 20 20   iAgg;          
64c10 20 20 20 20 2f 2a 20 57 68 69 63 68 20 65 6e 74      /* Which ent
64c20 72 79 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e  ry in pAggInfo->
64c30 61 43 6f 6c 5b 5d 20 6f 72 20 2d 3e 61 46 75 6e  aCol[] or ->aFun
64c40 63 5b 5d 20 2a 2f 0a 20 20 69 31 36 20 69 52 69  c[] */.  i16 iRi
64c50 67 68 74 4a 6f 69 6e 54 61 62 6c 65 3b 20 20 20  ghtJoinTable;   
64c60 2f 2a 20 49 66 20 45 50 5f 46 72 6f 6d 4a 6f 69  /* If EP_FromJoi
64c70 6e 2c 20 74 68 65 20 72 69 67 68 74 20 74 61 62  n, the right tab
64c80 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69 6e 20 2a  le of the join *
64c90 2f 0a 20 20 75 38 20 66 6c 61 67 73 32 3b 20 20  /.  u8 flags2;  
64ca0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65             /* Se
64cb0 63 6f 6e 64 20 73 65 74 20 6f 66 20 66 6c 61 67  cond set of flag
64cc0 73 2e 20 20 45 50 32 5f 2e 2e 2e 20 2a 2f 0a 20  s.  EP2_... */. 
64cd0 20 75 38 20 6f 70 32 3b 20 20 20 20 20 20 20 20   u8 op2;        
64ce0 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20          /* If a 
64cf0 54 4b 5f 52 45 47 49 53 54 45 52 2c 20 74 68 65  TK_REGISTER, the
64d00 20 6f 72 69 67 69 6e 61 6c 20 76 61 6c 75 65 20   original value 
64d10 6f 66 20 45 78 70 72 2e 6f 70 20 2a 2f 0a 20 20  of Expr.op */.  
64d20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66  AggInfo *pAggInf
64d30 6f 3b 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62  o;     /* Used b
64d40 79 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20  y TK_AGG_COLUMN 
64d50 61 6e 64 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54  and TK_AGG_FUNCT
64d60 49 4f 4e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ION */.  Table *
64d70 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  pTab;           
64d80 2f 2a 20 54 61 62 6c 65 20 66 6f 72 20 54 4b 5f  /* Table for TK_
64d90 43 4f 4c 55 4d 4e 20 65 78 70 72 65 73 73 69 6f  COLUMN expressio
64da0 6e 73 2e 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ns. */.#if SQLIT
64db0 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48  E_MAX_EXPR_DEPTH
64dc0 3e 30 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74  >0.  int nHeight
64dd0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48  ;           /* H
64de0 65 69 67 68 74 20 6f 66 20 74 68 65 20 74 72 65  eight of the tre
64df0 65 20 68 65 61 64 65 64 20 62 79 20 74 68 69 73  e headed by this
64e00 20 6e 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a   node */.#endif.
64e10 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  };../*.** The fo
64e20 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 68 65 20  llowing are the 
64e30 6d 65 61 6e 69 6e 67 73 20 6f 66 20 62 69 74 73  meanings of bits
64e40 20 69 6e 20 74 68 65 20 45 78 70 72 2e 66 6c 61   in the Expr.fla
64e50 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65  gs field..*/.#de
64e60 66 69 6e 65 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e  fine EP_FromJoin
64e70 20 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4f 72     0x0001  /* Or
64e80 69 67 69 6e 61 74 65 64 20 69 6e 20 4f 4e 20 6f  iginated in ON o
64e90 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f  r USING clause o
64ea0 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66  f a join */.#def
64eb0 69 6e 65 20 45 50 5f 41 67 67 20 20 20 20 20 20  ine EP_Agg      
64ec0 20 20 30 78 30 30 30 32 20 20 2f 2a 20 43 6f 6e    0x0002  /* Con
64ed0 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72  tains one or mor
64ee0 65 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  e aggregate func
64ef0 74 69 6f 6e 73 20 2a 2f 0a 23 64 65 66 69 6e 65  tions */.#define
64f00 20 45 50 5f 52 65 73 6f 6c 76 65 64 20 20 20 30   EP_Resolved   0
64f10 78 30 30 30 34 20 20 2f 2a 20 49 44 73 20 68 61  x0004  /* IDs ha
64f20 76 65 20 62 65 65 6e 20 72 65 73 6f 6c 76 65 64  ve been resolved
64f30 20 74 6f 20 43 4f 4c 55 4d 4e 73 20 2a 2f 0a 23   to COLUMNs */.#
64f40 64 65 66 69 6e 65 20 45 50 5f 45 72 72 6f 72 20  define EP_Error 
64f50 20 20 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20       0x0008  /* 
64f60 45 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e 74 61  Expression conta
64f70 69 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  ins one or more 
64f80 65 72 72 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e  errors */.#defin
64f90 65 20 45 50 5f 44 69 73 74 69 6e 63 74 20 20 20  e EP_Distinct   
64fa0 30 78 30 30 31 30 20 20 2f 2a 20 41 67 67 72 65  0x0010  /* Aggre
64fb0 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 77 69  gate function wi
64fc0 74 68 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77  th DISTINCT keyw
64fd0 6f 72 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45  ord */.#define E
64fe0 50 5f 56 61 72 53 65 6c 65 63 74 20 20 30 78 30  P_VarSelect  0x0
64ff0 30 32 30 20 20 2f 2a 20 70 53 65 6c 65 63 74 20  020  /* pSelect 
65000 69 73 20 63 6f 72 72 65 6c 61 74 65 64 2c 20 6e  is correlated, n
65010 6f 74 20 63 6f 6e 73 74 61 6e 74 20 2a 2f 0a 23  ot constant */.#
65020 64 65 66 69 6e 65 20 45 50 5f 44 62 6c 51 75 6f  define EP_DblQuo
65030 74 65 64 20 20 30 78 30 30 34 30 20 20 2f 2a 20  ted  0x0040  /* 
65040 74 6f 6b 65 6e 2e 7a 20 77 61 73 20 6f 72 69 67  token.z was orig
65050 69 6e 61 6c 6c 79 20 69 6e 20 22 2e 2e 2e 22 20  inally in "..." 
65060 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 49 6e  */.#define EP_In
65070 66 69 78 46 75 6e 63 20 20 30 78 30 30 38 30 20  fixFunc  0x0080 
65080 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 6e 20   /* True for an 
65090 69 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e 3a 20  infix function: 
650a0 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20 65 74 63 20  LIKE, GLOB, etc 
650b0 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 45 78  */.#define EP_Ex
650c0 70 43 6f 6c 6c 61 74 65 20 30 78 30 31 30 30 20  pCollate 0x0100 
650d0 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65   /* Collating se
650e0 71 75 65 6e 63 65 20 73 70 65 63 69 66 69 65 64  quence specified
650f0 20 65 78 70 6c 69 63 69 74 6c 79 20 2a 2f 0a 23   explicitly */.#
65100 64 65 66 69 6e 65 20 45 50 5f 41 6e 79 41 66 66  define EP_AnyAff
65110 20 20 20 20 20 30 78 30 32 30 30 20 20 2f 2a 20       0x0200  /* 
65120 43 61 6e 20 74 61 6b 65 20 61 20 63 61 63 68 65  Can take a cache
65130 64 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 6e 79 20  d column of any 
65140 61 66 66 69 6e 69 74 79 20 2a 2f 0a 23 64 65 66  affinity */.#def
65150 69 6e 65 20 45 50 5f 46 69 78 65 64 44 65 73 74  ine EP_FixedDest
65160 20 20 30 78 30 34 30 30 20 20 2f 2a 20 52 65 73    0x0400  /* Res
65170 75 6c 74 20 6e 65 65 64 65 64 20 69 6e 20 61 20  ult needed in a 
65180 73 70 65 63 69 66 69 63 20 72 65 67 69 73 74 65  specific registe
65190 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f  r */.#define EP_
651a0 49 6e 74 56 61 6c 75 65 20 20 20 30 78 30 38 30  IntValue   0x080
651b0 30 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61  0  /* Integer va
651c0 6c 75 65 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e  lue contained in
651d0 20 75 2e 69 56 61 6c 75 65 20 2a 2f 0a 23 64 65   u.iValue */.#de
651e0 66 69 6e 65 20 45 50 5f 78 49 73 53 65 6c 65 63  fine EP_xIsSelec
651f0 74 20 20 30 78 31 30 30 30 20 20 2f 2a 20 78 2e  t  0x1000  /* x.
65200 70 53 65 6c 65 63 74 20 69 73 20 76 61 6c 69 64  pSelect is valid
65210 20 28 6f 74 68 65 72 77 69 73 65 20 78 2e 70 4c   (otherwise x.pL
65220 69 73 74 20 69 73 29 20 2a 2f 0a 0a 23 64 65 66  ist is) */..#def
65230 69 6e 65 20 45 50 5f 52 65 64 75 63 65 64 20 20  ine EP_Reduced  
65240 20 20 30 78 32 30 30 30 20 20 2f 2a 20 45 78 70    0x2000  /* Exp
65250 72 20 73 74 72 75 63 74 20 69 73 20 45 58 50 52  r struct is EXPR
65260 5f 52 45 44 55 43 45 44 53 49 5a 45 20 62 79 74  _REDUCEDSIZE byt
65270 65 73 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  es only */.#defi
65280 6e 65 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 20  ne EP_TokenOnly 
65290 20 30 78 34 30 30 30 20 20 2f 2a 20 45 78 70 72   0x4000  /* Expr
652a0 20 73 74 72 75 63 74 20 69 73 20 45 58 50 52 5f   struct is EXPR_
652b0 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 20 62 79  TOKENONLYSIZE by
652c0 74 65 73 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66  tes only */.#def
652d0 69 6e 65 20 45 50 5f 53 74 61 74 69 63 20 20 20  ine EP_Static   
652e0 20 20 30 78 38 30 30 30 20 20 2f 2a 20 48 65 6c    0x8000  /* Hel
652f0 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6e 6f 74 20  d in memory not 
65300 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61  obtained from ma
65310 6c 6c 6f 63 28 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  lloc() */../*.**
65320 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   The following a
65330 72 65 20 74 68 65 20 6d 65 61 6e 69 6e 67 73 20  re the meanings 
65340 6f 66 20 62 69 74 73 20 69 6e 20 74 68 65 20 45  of bits in the E
65350 78 70 72 2e 66 6c 61 67 73 32 20 66 69 65 6c 64  xpr.flags2 field
65360 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 32  ..*/.#define EP2
65370 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e 20 20  _MallocedToken  
65380 30 78 30 30 30 31 20 20 2f 2a 20 4e 65 65 64 20  0x0001  /* Need 
65390 74 6f 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  to sqlite3DbFree
653a0 28 29 20 45 78 70 72 2e 7a 54 6f 6b 65 6e 20 2a  () Expr.zToken *
653b0 2f 0a 23 64 65 66 69 6e 65 20 45 50 32 5f 49 72  /.#define EP2_Ir
653c0 72 65 64 75 63 69 62 6c 65 20 20 20 20 30 78 30  reducible    0x0
653d0 30 30 32 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 45  002  /* Cannot E
653e0 58 50 52 44 55 50 5f 52 45 44 55 43 45 20 74 68  XPRDUP_REDUCE th
653f0 69 73 20 45 78 70 72 20 2a 2f 0a 0a 2f 2a 0a 2a  is Expr */../*.*
65400 2a 20 54 68 65 20 70 73 65 75 64 6f 2d 72 6f 75  * The pseudo-rou
65410 74 69 6e 65 20 73 71 6c 69 74 65 33 45 78 70 72  tine sqlite3Expr
65420 53 65 74 49 72 72 65 64 75 63 69 62 6c 65 20 73  SetIrreducible s
65430 65 74 73 20 74 68 65 20 45 50 32 5f 49 72 72 65  ets the EP2_Irre
65440 64 75 63 69 62 6c 65 0a 2a 2a 20 66 6c 61 67 20  ducible.** flag 
65450 6f 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  on an expression
65460 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 69   structure.  Thi
65470 73 20 66 6c 61 67 20 69 73 20 75 73 65 64 20 66  s flag is used f
65480 6f 72 20 56 56 26 41 20 6f 6e 6c 79 2e 20 20 54  or VV&A only.  T
65490 68 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73  he.** routine is
654a0 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20   implemented as 
654b0 61 20 6d 61 63 72 6f 20 74 68 61 74 20 6f 6e 6c  a macro that onl
654c0 79 20 77 6f 72 6b 73 20 77 68 65 6e 20 69 6e 20  y works when in 
654d0 64 65 62 75 67 67 69 6e 67 20 6d 6f 64 65 2c 0a  debugging mode,.
654e0 2a 2a 20 73 6f 20 61 73 20 6e 6f 74 20 74 6f 20  ** so as not to 
654f0 62 75 72 64 65 6e 20 70 72 6f 64 75 63 74 69 6f  burden productio
65500 6e 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69 66 64 65  n code..*/.#ifde
65510 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 23  f SQLITE_DEBUG.#
65520 20 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 49   define ExprSetI
65530 72 72 65 64 75 63 69 62 6c 65 28 58 29 20 20 28  rreducible(X)  (
65540 58 29 2d 3e 66 6c 61 67 73 32 20 7c 3d 20 45 50  X)->flags2 |= EP
65550 32 5f 49 72 72 65 64 75 63 69 62 6c 65 0a 23 65  2_Irreducible.#e
65560 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 45 78 70  lse.# define Exp
65570 72 53 65 74 49 72 72 65 64 75 63 69 62 6c 65 28  rSetIrreducible(
65580 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  X).#endif../*.**
65590 20 54 68 65 73 65 20 6d 61 63 72 6f 73 20 63 61   These macros ca
655a0 6e 20 62 65 20 75 73 65 64 20 74 6f 20 74 65 73  n be used to tes
655b0 74 2c 20 73 65 74 2c 20 6f 72 20 63 6c 65 61 72  t, set, or clear
655c0 20 62 69 74 73 20 69 6e 20 74 68 65 20 0a 2a 2a   bits in the .**
655d0 20 45 78 70 72 2e 66 6c 61 67 73 20 66 69 65 6c   Expr.flags fiel
655e0 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45 78  d..*/.#define Ex
655f0 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 45 2c  prHasProperty(E,
65600 50 29 20 20 20 20 20 28 28 28 45 29 2d 3e 66 6c  P)     (((E)->fl
65610 61 67 73 26 28 50 29 29 3d 3d 28 50 29 29 0a 23  ags&(P))==(P)).#
65620 64 65 66 69 6e 65 20 45 78 70 72 48 61 73 41 6e  define ExprHasAn
65630 79 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20  yProperty(E,P)  
65640 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50 29  (((E)->flags&(P)
65650 29 21 3d 30 29 0a 23 64 65 66 69 6e 65 20 45 78  )!=0).#define Ex
65660 70 72 53 65 74 50 72 6f 70 65 72 74 79 28 45 2c  prSetProperty(E,
65670 50 29 20 20 20 20 20 28 45 29 2d 3e 66 6c 61 67  P)     (E)->flag
65680 73 7c 3d 28 50 29 0a 23 64 65 66 69 6e 65 20 45  s|=(P).#define E
65690 78 70 72 43 6c 65 61 72 50 72 6f 70 65 72 74 79  xprClearProperty
656a0 28 45 2c 50 29 20 20 20 28 45 29 2d 3e 66 6c 61  (E,P)   (E)->fla
656b0 67 73 26 3d 7e 28 50 29 0a 0a 2f 2a 0a 2a 2a 20  gs&=~(P)../*.** 
656c0 4d 61 63 72 6f 73 20 74 6f 20 64 65 74 65 72 6d  Macros to determ
656d0 69 6e 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ine the number o
656e0 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64  f bytes required
656f0 20 62 79 20 61 20 6e 6f 72 6d 61 6c 20 45 78 70   by a normal Exp
65700 72 20 0a 2a 2a 20 73 74 72 75 63 74 2c 20 61 6e  r .** struct, an
65710 20 45 78 70 72 20 73 74 72 75 63 74 20 77 69 74   Expr struct wit
65720 68 20 74 68 65 20 45 50 5f 52 65 64 75 63 65 64  h the EP_Reduced
65730 20 66 6c 61 67 20 73 65 74 20 69 6e 20 45 78 70   flag set in Exp
65740 72 2e 66 6c 61 67 73 20 0a 2a 2a 20 61 6e 64 20  r.flags .** and 
65750 61 6e 20 45 78 70 72 20 73 74 72 75 63 74 20 77  an Expr struct w
65760 69 74 68 20 74 68 65 20 45 50 5f 54 6f 6b 65 6e  ith the EP_Token
65770 4f 6e 6c 79 20 66 6c 61 67 20 73 65 74 2e 0a 2a  Only flag set..*
65780 2f 0a 23 64 65 66 69 6e 65 20 45 58 50 52 5f 46  /.#define EXPR_F
65790 55 4c 4c 53 49 5a 45 20 20 20 20 20 20 20 20 20  ULLSIZE         
657a0 20 20 73 69 7a 65 6f 66 28 45 78 70 72 29 20 20    sizeof(Expr)  
657b0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 75 6c 6c           /* Full
657c0 20 73 69 7a 65 20 2a 2f 0a 23 64 65 66 69 6e 65   size */.#define
657d0 20 45 58 50 52 5f 52 45 44 55 43 45 44 53 49 5a   EXPR_REDUCEDSIZ
657e0 45 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 6f  E        offseto
657f0 66 28 45 78 70 72 2c 69 54 61 62 6c 65 29 20 20  f(Expr,iTable)  
65800 2f 2a 20 43 6f 6d 6d 6f 6e 20 66 65 61 74 75 72  /* Common featur
65810 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 58  es */.#define EX
65820 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45  PR_TOKENONLYSIZE
65830 20 20 20 20 20 20 6f 66 66 73 65 74 6f 66 28 45        offsetof(E
65840 78 70 72 2c 70 4c 65 66 74 29 20 20 20 2f 2a 20  xpr,pLeft)   /* 
65850 46 65 77 65 72 20 66 65 61 74 75 72 65 73 20 2a  Fewer features *
65860 2f 0a 0a 2f 2a 0a 2a 2a 20 46 6c 61 67 73 20 70  /../*.** Flags p
65870 61 73 73 65 64 20 74 6f 20 74 68 65 20 73 71 6c  assed to the sql
65880 69 74 65 33 45 78 70 72 44 75 70 28 29 20 66 75  ite3ExprDup() fu
65890 6e 63 74 69 6f 6e 2e 20 53 65 65 20 74 68 65 20  nction. See the 
658a0 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 0a  header comment .
658b0 2a 2a 20 61 62 6f 76 65 20 73 71 6c 69 74 65 33  ** above sqlite3
658c0 45 78 70 72 44 75 70 28 29 20 66 6f 72 20 64 65  ExprDup() for de
658d0 74 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  tails..*/.#defin
658e0 65 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45  e EXPRDUP_REDUCE
658f0 20 20 20 20 20 20 20 20 20 30 78 30 30 30 31 20           0x0001 
65900 20 2f 2a 20 55 73 65 64 20 72 65 64 75 63 65 64   /* Used reduced
65910 2d 73 69 7a 65 20 45 78 70 72 20 6e 6f 64 65 73  -size Expr nodes
65920 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73   */../*.** A lis
65930 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73  t of expressions
65940 2e 20 20 45 61 63 68 20 65 78 70 72 65 73 73 69  .  Each expressi
65950 6f 6e 20 6d 61 79 20 6f 70 74 69 6f 6e 61 6c 6c  on may optionall
65960 79 20 68 61 76 65 20 61 0a 2a 2a 20 6e 61 6d 65  y have a.** name
65970 2e 20 20 41 6e 20 65 78 70 72 2f 6e 61 6d 65 20  .  An expr/name 
65980 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 63 61 6e 20  combination can 
65990 62 65 20 75 73 65 64 20 69 6e 20 73 65 76 65 72  be used in sever
659a0 61 6c 20 77 61 79 73 2c 20 73 75 63 68 0a 2a 2a  al ways, such.**
659b0 20 61 73 20 74 68 65 20 6c 69 73 74 20 6f 66 20   as the list of 
659c0 22 65 78 70 72 20 41 53 20 49 44 22 20 66 69 65  "expr AS ID" fie
659d0 6c 64 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20  lds following a 
659e0 22 53 45 4c 45 43 54 22 20 6f 72 20 69 6e 20 74  "SELECT" or in t
659f0 68 65 0a 2a 2a 20 6c 69 73 74 20 6f 66 20 22 49  he.** list of "I
65a00 44 20 3d 20 65 78 70 72 22 20 69 74 65 6d 73 20  D = expr" items 
65a10 69 6e 20 61 6e 20 55 50 44 41 54 45 2e 20 20 41  in an UPDATE.  A
65a20 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73   list of express
65a30 69 6f 6e 73 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f  ions can.** also
65a40 20 62 65 20 75 73 65 64 20 61 73 20 74 68 65 20   be used as the 
65a50 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 20 66 75  argument to a fu
65a60 6e 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68  nction, in which
65a70 20 63 61 73 65 20 74 68 65 20 61 2e 7a 4e 61 6d   case the a.zNam
65a80 65 0a 2a 2a 20 66 69 65 6c 64 20 69 73 20 6e 6f  e.** field is no
65a90 74 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63  t used..*/.struc
65aa0 74 20 45 78 70 72 4c 69 73 74 20 7b 0a 20 20 69  t ExprList {.  i
65ab0 6e 74 20 6e 45 78 70 72 3b 20 20 20 20 20 20 20  nt nExpr;       
65ac0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
65ad0 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f  of expressions o
65ae0 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20  n the list */.  
65af0 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20  int nAlloc;     
65b00 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
65b10 20 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f   of entries allo
65b20 63 61 74 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 20  cated below */. 
65b30 20 69 6e 74 20 69 45 43 75 72 73 6f 72 3b 20 20   int iECursor;  
65b40 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20          /* VDBE 
65b50 43 75 72 73 6f 72 20 61 73 73 6f 63 69 61 74 65  Cursor associate
65b60 64 20 77 69 74 68 20 74 68 69 73 20 45 78 70 72  d with this Expr
65b70 4c 69 73 74 20 2a 2f 0a 20 20 73 74 72 75 63 74  List */.  struct
65b80 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 7b   ExprList_item {
65b90 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72  .    Expr *pExpr
65ba0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
65bb0 68 65 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65  he list of expre
65bc0 73 73 69 6f 6e 73 20 2a 2f 0a 20 20 20 20 63 68  ssions */.    ch
65bd0 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20  ar *zName;      
65be0 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 73       /* Token as
65bf0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
65c00 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  is expression */
65c10 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 70 61 6e  .    char *zSpan
65c20 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  ;           /* O
65c30 72 69 67 69 6e 61 6c 20 74 65 78 74 20 6f 66 20  riginal text of 
65c40 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  the expression *
65c50 2f 0a 20 20 20 20 75 38 20 73 6f 72 74 4f 72 64  /.    u8 sortOrd
65c60 65 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  er;          /* 
65c70 31 20 66 6f 72 20 44 45 53 43 20 6f 72 20 30 20  1 for DESC or 0 
65c80 66 6f 72 20 41 53 43 20 2a 2f 0a 20 20 20 20 75  for ASC */.    u
65c90 38 20 64 6f 6e 65 3b 20 20 20 20 20 20 20 20 20  8 done;         
65ca0 20 20 20 20 20 20 2f 2a 20 41 20 66 6c 61 67 20        /* A flag 
65cb0 74 6f 20 69 6e 64 69 63 61 74 65 20 77 68 65 6e  to indicate when
65cc0 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20 66   processing is f
65cd0 69 6e 69 73 68 65 64 20 2a 2f 0a 20 20 20 20 75  inished */.    u
65ce0 31 36 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20  16 iCol;        
65cf0 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 4f 52 44        /* For ORD
65d00 45 52 20 42 59 2c 20 63 6f 6c 75 6d 6e 20 6e 75  ER BY, column nu
65d10 6d 62 65 72 20 69 6e 20 72 65 73 75 6c 74 20 73  mber in result s
65d20 65 74 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 41  et */.    u16 iA
65d30 6c 69 61 73 3b 20 20 20 20 20 20 20 20 20 20 20  lias;           
65d40 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 50   /* Index into P
65d50 61 72 73 65 2e 61 41 6c 69 61 73 5b 5d 20 66 6f  arse.aAlias[] fo
65d60 72 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a  r zName */.  } *
65d70 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a;              
65d80 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79      /* One entry
65d90 20 66 6f 72 20 65 61 63 68 20 65 78 70 72 65 73   for each expres
65da0 73 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  sion */.};../*.*
65db0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
65dc0 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
65dd0 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70  is used by the p
65de0 61 72 73 65 72 20 74 6f 20 72 65 63 6f 72 64 20  arser to record 
65df0 62 6f 74 68 0a 2a 2a 20 74 68 65 20 70 61 72 73  both.** the pars
65e00 65 20 74 72 65 65 20 66 6f 72 20 61 6e 20 65 78  e tree for an ex
65e10 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 74 68 65  pression and the
65e20 20 73 70 61 6e 20 6f 66 20 69 6e 70 75 74 20 74   span of input t
65e30 65 78 74 20 66 6f 72 20 61 6e 0a 2a 2a 20 65 78  ext for an.** ex
65e40 70 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 72  pression..*/.str
65e50 75 63 74 20 45 78 70 72 53 70 61 6e 20 7b 0a 20  uct ExprSpan {. 
65e60 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20   Expr *pExpr;   
65e70 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78         /* The ex
65e80 70 72 65 73 73 69 6f 6e 20 70 61 72 73 65 20 74  pression parse t
65e90 72 65 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ree */.  const c
65ea0 68 61 72 20 2a 7a 53 74 61 72 74 3b 20 20 20 2f  har *zStart;   /
65eb0 2a 20 46 69 72 73 74 20 63 68 61 72 61 63 74 65  * First characte
65ec0 72 20 6f 66 20 69 6e 70 75 74 20 74 65 78 74 20  r of input text 
65ed0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
65ee0 2a 7a 45 6e 64 3b 20 20 20 20 20 2f 2a 20 4f 6e  *zEnd;     /* On
65ef0 65 20 63 68 61 72 61 63 74 65 72 20 70 61 73 74  e character past
65f00 20 74 68 65 20 65 6e 64 20 6f 66 20 69 6e 70 75   the end of inpu
65f10 74 20 74 65 78 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  t text */.};../*
65f20 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
65f30 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
65f40 65 20 63 61 6e 20 68 6f 6c 64 20 61 20 73 69 6d  e can hold a sim
65f50 70 6c 65 20 6c 69 73 74 20 6f 66 20 69 64 65 6e  ple list of iden
65f60 74 69 66 69 65 72 73 2c 0a 2a 2a 20 73 75 63 68  tifiers,.** such
65f70 20 61 73 20 74 68 65 20 6c 69 73 74 20 22 61 2c   as the list "a,
65f80 62 2c 63 22 20 69 6e 20 74 68 65 20 66 6f 6c 6c  b,c" in the foll
65f90 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 73  owing statements
65fa0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 49 4e 53  :.**.**      INS
65fb0 45 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63  ERT INTO t(a,b,c
65fc0 29 20 56 41 4c 55 45 53 20 2e 2e 2e 3b 0a 2a 2a  ) VALUES ...;.**
65fd0 20 20 20 20 20 20 43 52 45 41 54 45 20 49 4e 44        CREATE IND
65fe0 45 58 20 69 64 78 20 4f 4e 20 74 28 61 2c 62 2c  EX idx ON t(a,b,
65ff0 63 29 3b 0a 2a 2a 20 20 20 20 20 20 43 52 45 41  c);.**      CREA
66000 54 45 20 54 52 49 47 47 45 52 20 74 72 69 67 20  TE TRIGGER trig 
66010 42 45 46 4f 52 45 20 55 50 44 41 54 45 20 4f 4e  BEFORE UPDATE ON
66020 20 74 28 61 2c 62 2c 63 29 20 2e 2e 2e 3b 0a 2a   t(a,b,c) ...;.*
66030 2a 0a 2a 2a 20 54 68 65 20 49 64 4c 69 73 74 2e  *.** The IdList.
66040 61 2e 69 64 78 20 66 69 65 6c 64 20 69 73 20 75  a.idx field is u
66050 73 65 64 20 77 68 65 6e 20 74 68 65 20 49 64 4c  sed when the IdL
66060 69 73 74 20 72 65 70 72 65 73 65 6e 74 73 20 74  ist represents t
66070 68 65 20 6c 69 73 74 20 6f 66 0a 2a 2a 20 63 6f  he list of.** co
66080 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 66 74 65 72  lumn names after
66090 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e   a table name in
660a0 20 61 6e 20 49 4e 53 45 52 54 20 73 74 61 74 65   an INSERT state
660b0 6d 65 6e 74 2e 20 20 49 6e 20 74 68 65 20 73 74  ment.  In the st
660c0 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20 20 20  atement.**.**   
660d0 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 28    INSERT INTO t(
660e0 61 2c 62 2c 63 29 20 2e 2e 2e 0a 2a 2a 0a 2a 2a  a,b,c) ....**.**
660f0 20 49 66 20 22 61 22 20 69 73 20 74 68 65 20 6b   If "a" is the k
66100 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61  -th column of ta
66110 62 6c 65 20 22 74 22 2c 20 74 68 65 6e 20 49 64  ble "t", then Id
66120 4c 69 73 74 2e 61 5b 30 5d 2e 69 64 78 3d 3d 6b  List.a[0].idx==k
66130 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 64 4c 69  ..*/.struct IdLi
66140 73 74 20 7b 0a 20 20 73 74 72 75 63 74 20 49 64  st {.  struct Id
66150 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20 20  List_item {.    
66160 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20  char *zName;    
66170 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
66180 20 69 64 65 6e 74 69 66 69 65 72 20 2a 2f 0a 20   identifier */. 
66190 20 20 20 69 6e 74 20 69 64 78 3b 20 20 20 20 20     int idx;     
661a0 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
661b0 20 73 6f 6d 65 20 54 61 62 6c 65 2e 61 43 6f 6c   some Table.aCol
661c0 5b 5d 20 6f 66 20 61 20 63 6f 6c 75 6d 6e 20 6e  [] of a column n
661d0 61 6d 65 64 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20  amed zName */.  
661e0 7d 20 2a 61 3b 0a 20 20 69 6e 74 20 6e 49 64 3b  } *a;.  int nId;
661f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
66200 65 72 20 6f 66 20 69 64 65 6e 74 69 66 69 65 72  er of identifier
66210 73 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f  s on the list */
66220 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20  .  int nAlloc;  
66230 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
66240 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61 74   entries allocat
66250 65 64 20 66 6f 72 20 61 5b 5d 20 62 65 6c 6f 77  ed for a[] below
66260 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   */.};../*.** Th
66270 65 20 62 69 74 6d 61 73 6b 20 64 61 74 61 74 79  e bitmask dataty
66280 70 65 20 64 65 66 69 6e 65 64 20 62 65 6c 6f 77  pe defined below
66290 20 69 73 20 75 73 65 64 20 66 6f 72 20 76 61 72   is used for var
662a0 69 6f 75 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f  ious optimizatio
662b0 6e 73 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67 69  ns..**.** Changi
662c0 6e 67 20 74 68 69 73 20 66 72 6f 6d 20 61 20 36  ng this from a 6
662d0 34 2d 62 69 74 20 74 6f 20 61 20 33 32 2d 62 69  4-bit to a 32-bi
662e0 74 20 74 79 70 65 20 6c 69 6d 69 74 73 20 74 68  t type limits th
662f0 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 74  e number of.** t
66300 61 62 6c 65 73 20 69 6e 20 61 20 6a 6f 69 6e 20  ables in a join 
66310 74 6f 20 33 32 20 69 6e 73 74 65 61 64 20 6f 66  to 32 instead of
66320 20 36 34 2e 20 20 42 75 74 20 69 74 20 61 6c 73   64.  But it als
66330 6f 20 72 65 64 75 63 65 73 20 74 68 65 20 73 69  o reduces the si
66340 7a 65 0a 2a 2a 20 6f 66 20 74 68 65 20 6c 69 62  ze.** of the lib
66350 72 61 72 79 20 62 79 20 37 33 38 20 62 79 74 65  rary by 738 byte
66360 73 20 6f 6e 20 69 78 38 36 2e 0a 2a 2f 0a 74 79  s on ix86..*/.ty
66370 70 65 64 65 66 20 75 36 34 20 42 69 74 6d 61 73  pedef u64 Bitmas
66380 6b 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 75  k;../*.** The nu
66390 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e 20  mber of bits in 
663a0 61 20 42 69 74 6d 61 73 6b 2e 20 20 22 42 4d 53  a Bitmask.  "BMS
663b0 22 20 6d 65 61 6e 73 20 22 42 69 74 4d 61 73 6b  " means "BitMask
663c0 20 53 69 7a 65 22 2e 0a 2a 2f 0a 23 64 65 66 69   Size"..*/.#defi
663d0 6e 65 20 42 4d 53 20 20 28 28 69 6e 74 29 28 73  ne BMS  ((int)(s
663e0 69 7a 65 6f 66 28 42 69 74 6d 61 73 6b 29 2a 38  izeof(Bitmask)*8
663f0 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  ))../*.** The fo
66400 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
66410 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  e describes the 
66420 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61  FROM clause of a
66430 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
66440 74 2e 0a 2a 2a 20 45 61 63 68 20 74 61 62 6c 65  t..** Each table
66450 20 6f 72 20 73 75 62 71 75 65 72 79 20 69 6e 20   or subquery in 
66460 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
66470 69 73 20 61 20 73 65 70 61 72 61 74 65 20 65 6c  is a separate el
66480 65 6d 65 6e 74 20 6f 66 0a 2a 2a 20 74 68 65 20  ement of.** the 
66490 53 72 63 4c 69 73 74 2e 61 5b 5d 20 61 72 72 61  SrcList.a[] arra
664a0 79 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 68  y..**.** With th
664b0 65 20 61 64 64 69 74 69 6f 6e 20 6f 66 20 6d 75  e addition of mu
664c0 6c 74 69 70 6c 65 20 64 61 74 61 62 61 73 65 20  ltiple database 
664d0 73 75 70 70 6f 72 74 2c 20 74 68 65 20 66 6f 6c  support, the fol
664e0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
664f0 0a 2a 2a 20 63 61 6e 20 61 6c 73 6f 20 62 65 20  .** can also be 
66500 75 73 65 64 20 74 6f 20 64 65 73 63 72 69 62 65  used to describe
66510 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 61   a particular ta
66520 62 6c 65 20 73 75 63 68 20 61 73 20 74 68 65 20  ble such as the 
66530 74 61 62 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73  table that.** is
66540 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e 20   modified by an 
66550 49 4e 53 45 52 54 2c 20 44 45 4c 45 54 45 2c 20  INSERT, DELETE, 
66560 6f 72 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  or UPDATE statem
66570 65 6e 74 2e 20 20 49 6e 20 73 74 61 6e 64 61 72  ent.  In standar
66580 64 20 53 51 4c 2c 0a 2a 2a 20 73 75 63 68 20 61  d SQL,.** such a
66590 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 61   table must be a
665a0 20 73 69 6d 70 6c 65 20 6e 61 6d 65 3a 20 49 44   simple name: ID
665b0 2e 20 20 42 75 74 20 69 6e 20 53 51 4c 69 74 65  .  But in SQLite
665c0 2c 20 74 68 65 20 74 61 62 6c 65 20 63 61 6e 0a  , the table can.
665d0 2a 2a 20 6e 6f 77 20 62 65 20 69 64 65 6e 74 69  ** now be identi
665e0 66 69 65 64 20 62 79 20 61 20 64 61 74 61 62 61  fied by a databa
665f0 73 65 20 6e 61 6d 65 2c 20 61 20 64 6f 74 2c 20  se name, a dot, 
66600 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20 6e  then the table n
66610 61 6d 65 3a 20 49 44 2e 49 44 2e 0a 2a 2a 0a 2a  ame: ID.ID..**.*
66620 2a 20 54 68 65 20 6a 6f 69 6e 74 79 70 65 20 73  * The jointype s
66630 74 61 72 74 73 20 6f 75 74 20 73 68 6f 77 69 6e  tarts out showin
66640 67 20 74 68 65 20 6a 6f 69 6e 20 74 79 70 65 20  g the join type 
66650 62 65 74 77 65 65 6e 20 74 68 65 20 63 75 72 72  between the curr
66660 65 6e 74 20 74 61 62 6c 65 0a 2a 2a 20 61 6e 64  ent table.** and
66670 20 74 68 65 20 6e 65 78 74 20 74 61 62 6c 65 20   the next table 
66680 6f 6e 20 74 68 65 20 6c 69 73 74 2e 20 20 54 68  on the list.  Th
66690 65 20 70 61 72 73 65 72 20 62 75 69 6c 64 73 20  e parser builds 
666a0 74 68 65 20 6c 69 73 74 20 74 68 69 73 20 77 61  the list this wa
666b0 79 2e 0a 2a 2a 20 42 75 74 20 73 71 6c 69 74 65  y..** But sqlite
666c0 33 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69  3SrcListShiftJoi
666d0 6e 54 79 70 65 28 29 20 6c 61 74 65 72 20 73 68  nType() later sh
666e0 69 66 74 73 20 74 68 65 20 6a 6f 69 6e 74 79 70  ifts the jointyp
666f0 65 73 20 73 6f 20 74 68 61 74 20 65 61 63 68 0a  es so that each.
66700 2a 2a 20 6a 6f 69 6e 74 79 70 65 20 65 78 70 72  ** jointype expr
66710 65 73 73 65 73 20 74 68 65 20 6a 6f 69 6e 20 62  esses the join b
66720 65 74 77 65 65 6e 20 74 68 65 20 74 61 62 6c 65  etween the table
66730 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75   and the previou
66740 73 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75  s table..*/.stru
66750 63 74 20 53 72 63 4c 69 73 74 20 7b 0a 20 20 69  ct SrcList {.  i
66760 31 36 20 6e 53 72 63 3b 20 20 20 20 20 20 20 20  16 nSrc;        
66770 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 61 62  /* Number of tab
66780 6c 65 73 20 6f 72 20 73 75 62 71 75 65 72 69 65  les or subquerie
66790 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  s in the FROM cl
667a0 61 75 73 65 20 2a 2f 0a 20 20 69 31 36 20 6e 41  ause */.  i16 nA
667b0 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 4e 75  lloc;      /* Nu
667c0 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
667d0 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 61 5b 5d  allocated in a[]
667e0 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 73 74 72 75   below */.  stru
667f0 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
66800 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 44 61 74  {.    char *zDat
66810 61 62 61 73 65 3b 20 20 2f 2a 20 4e 61 6d 65 20  abase;  /* Name 
66820 6f 66 20 64 61 74 61 62 61 73 65 20 68 6f 6c 64  of database hold
66830 69 6e 67 20 74 68 69 73 20 74 61 62 6c 65 20 2a  ing this table *
66840 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d  /.    char *zNam
66850 65 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  e;      /* Name 
66860 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  of the table */.
66870 20 20 20 20 63 68 61 72 20 2a 7a 41 6c 69 61 73      char *zAlias
66880 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 22 42 22  ;     /* The "B"
66890 20 70 61 72 74 20 6f 66 20 61 20 22 41 20 41 53   part of a "A AS
668a0 20 42 22 20 70 68 72 61 73 65 2e 20 20 7a 4e 61   B" phrase.  zNa
668b0 6d 65 20 69 73 20 74 68 65 20 22 41 22 20 2a 2f  me is the "A" */
668c0 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
668d0 3b 20 20 20 20 20 20 2f 2a 20 41 6e 20 53 51 4c  ;      /* An SQL
668e0 20 74 61 62 6c 65 20 63 6f 72 72 65 73 70 6f 6e   table correspon
668f0 64 69 6e 67 20 74 6f 20 7a 4e 61 6d 65 20 2a 2f  ding to zName */
66900 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 65  .    Select *pSe
66910 6c 65 63 74 3b 20 20 2f 2a 20 41 20 53 45 4c 45  lect;  /* A SELE
66920 43 54 20 73 74 61 74 65 6d 65 6e 74 20 75 73 65  CT statement use
66930 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 20  d in place of a 
66940 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  table name */.  
66950 20 20 75 38 20 69 73 50 6f 70 75 6c 61 74 65 64    u8 isPopulated
66960 3b 20 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79  ;   /* Temporary
66970 20 74 61 62 6c 65 20 61 73 73 6f 63 69 61 74 65   table associate
66980 64 20 77 69 74 68 20 53 45 4c 45 43 54 20 69 73  d with SELECT is
66990 20 70 6f 70 75 6c 61 74 65 64 20 2a 2f 0a 20 20   populated */.  
669a0 20 20 75 38 20 6a 6f 69 6e 74 79 70 65 3b 20 20    u8 jointype;  
669b0 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 6a      /* Type of j
669c0 6f 69 6e 20 62 65 74 77 65 65 6e 20 74 68 69 73  oin between this
669d0 20 61 62 6c 65 20 61 6e 64 20 74 68 65 20 70 72   able and the pr
669e0 65 76 69 6f 75 73 20 2a 2f 0a 20 20 20 20 75 38  evious */.    u8
669f0 20 6e 6f 74 49 6e 64 65 78 65 64 3b 20 20 20 20   notIndexed;    
66a00 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 65  /* True if there
66a10 20 69 73 20 61 20 4e 4f 54 20 49 4e 44 45 58 45   is a NOT INDEXE
66a20 44 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20  D clause */.    
66a30 69 6e 74 20 69 43 75 72 73 6f 72 3b 20 20 20 20  int iCursor;    
66a40 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75    /* The VDBE cu
66a50 72 73 6f 72 20 6e 75 6d 62 65 72 20 75 73 65 64  rsor number used
66a60 20 74 6f 20 61 63 63 65 73 73 20 74 68 69 73 20   to access this 
66a70 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 45 78 70  table */.    Exp
66a80 72 20 2a 70 4f 6e 3b 20 20 20 20 20 20 20 20 2f  r *pOn;        /
66a90 2a 20 54 68 65 20 4f 4e 20 63 6c 61 75 73 65 20  * The ON clause 
66aa0 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20  of a join */.   
66ab0 20 49 64 4c 69 73 74 20 2a 70 55 73 69 6e 67 3b   IdList *pUsing;
66ac0 20 20 20 2f 2a 20 54 68 65 20 55 53 49 4e 47 20     /* The USING 
66ad0 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e  clause of a join
66ae0 20 2a 2f 0a 20 20 20 20 42 69 74 6d 61 73 6b 20   */.    Bitmask 
66af0 63 6f 6c 55 73 65 64 3b 20 20 2f 2a 20 42 69 74  colUsed;  /* Bit
66b00 20 4e 20 28 31 3c 3c 4e 29 20 73 65 74 20 69 66   N (1<<N) set if
66b10 20 63 6f 6c 75 6d 6e 20 4e 20 6f 66 20 70 54 61   column N of pTa
66b20 62 20 69 73 20 75 73 65 64 20 2a 2f 0a 20 20 20  b is used */.   
66b30 20 63 68 61 72 20 2a 7a 49 6e 64 65 78 3b 20 20   char *zIndex;  
66b40 20 20 20 2f 2a 20 49 64 65 6e 74 69 66 69 65 72     /* Identifier
66b50 20 66 72 6f 6d 20 22 49 4e 44 45 58 45 44 20 42   from "INDEXED B
66b60 59 20 3c 7a 49 6e 64 65 78 3e 22 20 63 6c 61 75  Y <zIndex>" clau
66b70 73 65 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20  se */.    Index 
66b80 2a 70 49 6e 64 65 78 3b 20 20 20 20 2f 2a 20 49  *pIndex;    /* I
66b90 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20 63  ndex structure c
66ba0 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
66bb0 7a 49 6e 64 65 78 2c 20 69 66 20 61 6e 79 20 2a  zIndex, if any *
66bc0 2f 0a 20 20 7d 20 61 5b 31 5d 3b 20 20 20 20 20  /.  } a[1];     
66bd0 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65          /* One e
66be0 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20 69 64  ntry for each id
66bf0 65 6e 74 69 66 69 65 72 20 6f 6e 20 74 68 65 20  entifier on the 
66c00 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  list */.};../*.*
66c10 2a 20 50 65 72 6d 69 74 74 65 64 20 76 61 6c 75  * Permitted valu
66c20 65 73 20 6f 66 20 74 68 65 20 53 72 63 4c 69 73  es of the SrcLis
66c30 74 2e 61 2e 6a 6f 69 6e 74 79 70 65 20 66 69 65  t.a.jointype fie
66c40 6c 64 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54  ld.*/.#define JT
66c50 5f 49 4e 4e 45 52 20 20 20 20 20 30 78 30 30 30  _INNER     0x000
66c60 31 20 20 20 20 2f 2a 20 41 6e 79 20 6b 69 6e 64  1    /* Any kind
66c70 20 6f 66 20 69 6e 6e 65 72 20 6f 72 20 63 72 6f   of inner or cro
66c80 73 73 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66 69  ss join */.#defi
66c90 6e 65 20 4a 54 5f 43 52 4f 53 53 20 20 20 20 20  ne JT_CROSS     
66ca0 30 78 30 30 30 32 20 20 20 20 2f 2a 20 45 78 70  0x0002    /* Exp
66cb0 6c 69 63 69 74 20 75 73 65 20 6f 66 20 74 68 65  licit use of the
66cc0 20 43 52 4f 53 53 20 6b 65 79 77 6f 72 64 20 2a   CROSS keyword *
66cd0 2f 0a 23 64 65 66 69 6e 65 20 4a 54 5f 4e 41 54  /.#define JT_NAT
66ce0 55 52 41 4c 20 20 20 30 78 30 30 30 34 20 20 20  URAL   0x0004   
66cf0 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20 22   /* True for a "
66d00 6e 61 74 75 72 61 6c 22 20 6a 6f 69 6e 20 2a 2f  natural" join */
66d10 0a 23 64 65 66 69 6e 65 20 4a 54 5f 4c 45 46 54  .#define JT_LEFT
66d20 20 20 20 20 20 20 30 78 30 30 30 38 20 20 20 20        0x0008    
66d30 2f 2a 20 4c 65 66 74 20 6f 75 74 65 72 20 6a 6f  /* Left outer jo
66d40 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54  in */.#define JT
66d50 5f 52 49 47 48 54 20 20 20 20 20 30 78 30 30 31  _RIGHT     0x001
66d60 30 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 75  0    /* Right ou
66d70 74 65 72 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66  ter join */.#def
66d80 69 6e 65 20 4a 54 5f 4f 55 54 45 52 20 20 20 20  ine JT_OUTER    
66d90 20 30 78 30 30 32 30 20 20 20 20 2f 2a 20 54 68   0x0020    /* Th
66da0 65 20 22 4f 55 54 45 52 22 20 6b 65 79 77 6f 72  e "OUTER" keywor
66db0 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a  d is present */.
66dc0 23 64 65 66 69 6e 65 20 4a 54 5f 45 52 52 4f 52  #define JT_ERROR
66dd0 20 20 20 20 20 30 78 30 30 34 30 20 20 20 20 2f       0x0040    /
66de0 2a 20 75 6e 6b 6e 6f 77 6e 20 6f 72 20 75 6e 73  * unknown or uns
66df0 75 70 70 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79  upported join ty
66e00 70 65 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20  pe */.../*.** A 
66e10 57 68 65 72 65 50 6c 61 6e 20 6f 62 6a 65 63 74  WherePlan object
66e20 20 68 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69   holds informati
66e30 6f 6e 20 74 68 61 74 20 64 65 73 63 72 69 62 65  on that describe
66e40 73 20 61 20 6c 6f 6f 6b 75 70 0a 2a 2a 20 73 74  s a lookup.** st
66e50 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  rategy..**.** Th
66e60 69 73 20 6f 62 6a 65 63 74 20 69 73 20 69 6e 74  is object is int
66e70 65 6e 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71  ended to be opaq
66e80 75 65 20 6f 75 74 73 69 64 65 20 6f 66 20 74 68  ue outside of th
66e90 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 75 6c 65  e where.c module
66ea0 2e 0a 2a 2a 20 49 74 20 69 73 20 69 6e 63 6c 75  ..** It is inclu
66eb0 64 65 64 20 68 65 72 65 20 6f 6e 6c 79 20 73 6f  ded here only so
66ec0 20 74 68 61 74 20 74 68 61 74 20 63 6f 6d 70 69   that that compi
66ed0 6c 65 72 20 77 69 6c 6c 20 6b 6e 6f 77 20 68 6f  ler will know ho
66ee0 77 20 62 69 67 20 69 74 0a 2a 2a 20 69 73 2e 20  w big it.** is. 
66ef0 20 4e 6f 6e 65 20 6f 66 20 74 68 65 20 66 69 65   None of the fie
66f00 6c 64 73 20 69 6e 20 74 68 69 73 20 6f 62 6a 65  lds in this obje
66f10 63 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65  ct should be use
66f20 64 20 6f 75 74 73 69 64 65 20 6f 66 0a 2a 2a 20  d outside of.** 
66f30 74 68 65 20 77 68 65 72 65 2e 63 20 6d 6f 64 75  the where.c modu
66f40 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69 6e  le..**.** Within
66f50 20 74 68 65 20 75 6e 69 6f 6e 2c 20 70 49 64 78   the union, pIdx
66f60 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 77 68   is only used wh
66f70 65 6e 20 77 73 46 6c 61 67 73 26 57 48 45 52 45  en wsFlags&WHERE
66f80 5f 49 4e 44 45 58 45 44 20 69 73 20 74 72 75 65  _INDEXED is true
66f90 2e 0a 2a 2a 20 70 54 65 72 6d 20 69 73 20 6f 6e  ..** pTerm is on
66fa0 6c 79 20 75 73 65 64 20 77 68 65 6e 20 77 73 46  ly used when wsF
66fb0 6c 61 67 73 26 57 48 45 52 45 5f 4d 55 4c 54 49  lags&WHERE_MULTI
66fc0 5f 4f 52 20 69 73 20 74 72 75 65 2e 20 20 41 6e  _OR is true.  An
66fd0 64 20 70 56 74 61 62 49 64 78 0a 2a 2a 20 69 73  d pVtabIdx.** is
66fe0 20 6f 6e 6c 79 20 75 73 65 64 20 77 68 65 6e 20   only used when 
66ff0 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 56 49  wsFlags&WHERE_VI
67000 52 54 55 41 4c 54 41 42 4c 45 20 69 73 20 74 72  RTUALTABLE is tr
67010 75 65 2e 20 20 49 74 20 69 73 20 6e 65 76 65 72  ue.  It is never
67020 20 74 68 65 0a 2a 2a 20 63 61 73 65 20 74 68 61   the.** case tha
67030 74 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20  t more than one 
67040 6f 66 20 74 68 65 73 65 20 63 6f 6e 64 69 74 69  of these conditi
67050 6f 6e 73 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a  ons is true..*/.
67060 73 74 72 75 63 74 20 57 68 65 72 65 50 6c 61 6e  struct WherePlan
67070 20 7b 0a 20 20 75 33 32 20 77 73 46 6c 61 67 73   {.  u32 wsFlags
67080 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
67090 20 20 20 20 2f 2a 20 57 48 45 52 45 5f 2a 20 66      /* WHERE_* f
670a0 6c 61 67 73 20 74 68 61 74 20 64 65 73 63 72 69  lags that descri
670b0 62 65 20 74 68 65 20 73 74 72 61 74 65 67 79 20  be the strategy 
670c0 2a 2f 0a 20 20 75 33 32 20 6e 45 71 3b 20 20 20  */.  u32 nEq;   
670d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
670e0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
670f0 20 3d 3d 20 63 6f 6e 73 74 72 61 69 6e 74 73 20   == constraints 
67100 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20  */.  union {.   
67110 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20   Index *pIdx;   
67120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67130 2f 2a 20 49 6e 64 65 78 20 77 68 65 6e 20 57 48  /* Index when WH
67140 45 52 45 5f 49 4e 44 45 58 45 44 20 69 73 20 74  ERE_INDEXED is t
67150 72 75 65 20 2a 2f 0a 20 20 20 20 73 74 72 75 63  rue */.    struc
67160 74 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65  t WhereTerm *pTe
67170 72 6d 3b 20 20 20 20 20 20 20 2f 2a 20 57 48 45  rm;       /* WHE
67180 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 20 66  RE clause term f
67190 6f 72 20 4f 52 2d 73 65 61 72 63 68 20 2a 2f 0a  or OR-search */.
671a0 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65      sqlite3_inde
671b0 78 5f 69 6e 66 6f 20 2a 70 56 74 61 62 49 64 78  x_info *pVtabIdx
671c0 3b 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61  ;  /* Virtual ta
671d0 62 6c 65 20 69 6e 64 65 78 20 74 6f 20 75 73 65  ble index to use
671e0 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f   */.  } u;.};../
671f0 2a 0a 2a 2a 20 46 6f 72 20 65 61 63 68 20 6e 65  *.** For each ne
67200 73 74 65 64 20 6c 6f 6f 70 20 69 6e 20 61 20 57  sted loop in a W
67210 48 45 52 45 20 63 6c 61 75 73 65 20 69 6d 70 6c  HERE clause impl
67220 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 74 68 65 20  ementation, the 
67230 57 68 65 72 65 49 6e 66 6f 0a 2a 2a 20 73 74 72  WhereInfo.** str
67240 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20  ucture contains 
67250 61 20 73 69 6e 67 6c 65 20 69 6e 73 74 61 6e 63  a single instanc
67260 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  e of this struct
67270 75 72 65 2e 20 20 54 68 69 73 20 73 74 72 75 63  ure.  This struc
67280 74 75 72 65 0a 2a 2a 20 69 73 20 69 6e 74 65 6e  ture.** is inten
67290 64 65 64 20 74 6f 20 62 65 20 70 72 69 76 61 74  ded to be privat
672a0 65 20 74 68 65 20 74 68 65 20 77 68 65 72 65 2e  e the the where.
672b0 63 20 6d 6f 64 75 6c 65 20 61 6e 64 20 73 68 6f  c module and sho
672c0 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 61 63  uld not be.** ac
672d0 63 65 73 73 20 6f 72 20 6d 6f 64 69 66 69 65 64  cess or modified
672e0 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 75 6c 65   by other module
672f0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 49 64  s..**.** The pId
67300 78 49 6e 66 6f 20 66 69 65 6c 64 20 69 73 20 75  xInfo field is u
67310 73 65 64 20 74 6f 20 68 65 6c 70 20 70 69 63 6b  sed to help pick
67320 20 74 68 65 20 62 65 73 74 20 69 6e 64 65 78 20   the best index 
67330 6f 6e 20 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20  on a.** virtual 
67340 74 61 62 6c 65 2e 20 20 54 68 65 20 70 49 64 78  table.  The pIdx
67350 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20 63 6f 6e  Info pointer con
67360 74 61 69 6e 73 20 69 6e 64 65 78 69 6e 67 0a 2a  tains indexing.*
67370 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  * information fo
67380 72 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c 65  r the i-th table
67390 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
673a0 75 73 65 20 62 65 66 6f 72 65 20 72 65 6f 72 64  use before reord
673b0 65 72 69 6e 67 2e 0a 2a 2a 20 41 6c 6c 20 74 68  ering..** All th
673c0 65 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74  e pIdxInfo point
673d0 65 72 73 20 61 72 65 20 66 72 65 65 64 20 62 79  ers are freed by
673e0 20 77 68 65 72 65 49 6e 66 6f 46 72 65 65 28 29   whereInfoFree()
673f0 20 69 6e 20 77 68 65 72 65 2e 63 2e 0a 2a 2a 20   in where.c..** 
67400 41 6c 6c 20 6f 74 68 65 72 20 69 6e 66 6f 72 6d  All other inform
67410 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 69 2d 74  ation in the i-t
67420 68 20 57 68 65 72 65 4c 65 76 65 6c 20 6f 62 6a  h WhereLevel obj
67430 65 63 74 20 66 6f 72 20 74 68 65 20 69 2d 74 68  ect for the i-th
67440 20 74 61 62 6c 65 0a 2a 2a 20 61 66 74 65 72 20   table.** after 
67450 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 72 64 65  FROM clause orde
67460 72 69 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ring..*/.struct 
67470 57 68 65 72 65 4c 65 76 65 6c 20 7b 0a 20 20 57  WhereLevel {.  W
67480 68 65 72 65 50 6c 61 6e 20 70 6c 61 6e 3b 20 20  herePlan plan;  
67490 20 20 20 20 20 2f 2a 20 71 75 65 72 79 20 70 6c       /* query pl
674a0 61 6e 20 66 6f 72 20 74 68 69 73 20 65 6c 65 6d  an for this elem
674b0 65 6e 74 20 6f 66 20 74 68 65 20 46 52 4f 4d 20  ent of the FROM 
674c0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20  clause */.  int 
674d0 69 4c 65 66 74 4a 6f 69 6e 3b 20 20 20 20 20 20  iLeftJoin;      
674e0 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c    /* Memory cell
674f0 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
67500 6e 74 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f  nt LEFT OUTER JO
67510 49 4e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62  IN */.  int iTab
67520 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Cur;          /*
67530 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72   The VDBE cursor
67540 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20   used to access 
67550 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  the table */.  i
67560 6e 74 20 69 49 64 78 43 75 72 3b 20 20 20 20 20  nt iIdxCur;     
67570 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45       /* The VDBE
67580 20 63 75 72 73 6f 72 20 75 73 65 64 20 74 6f 20   cursor used to 
67590 61 63 63 65 73 73 20 70 49 64 78 20 2a 2f 0a 20  access pIdx */. 
675a0 20 69 6e 74 20 61 64 64 72 42 72 6b 3b 20 20 20   int addrBrk;   
675b0 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68         /* Jump h
675c0 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74  ere to break out
675d0 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a   of the loop */.
675e0 20 20 69 6e 74 20 61 64 64 72 4e 78 74 3b 20 20    int addrNxt;  
675f0 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20          /* Jump 
67600 68 65 72 65 20 74 6f 20 73 74 61 72 74 20 74 68  here to start th
67610 65 20 6e 65 78 74 20 49 4e 20 63 6f 6d 62 69 6e  e next IN combin
67620 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 61  ation */.  int a
67630 64 64 72 43 6f 6e 74 3b 20 20 20 20 20 20 20 20  ddrCont;        
67640 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74 6f   /* Jump here to
67650 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 74   continue with t
67660 68 65 20 6e 65 78 74 20 6c 6f 6f 70 20 63 79 63  he next loop cyc
67670 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  le */.  int addr
67680 46 69 72 73 74 3b 20 20 20 20 20 20 20 20 2f 2a  First;        /*
67690 20 46 69 72 73 74 20 69 6e 73 74 72 75 63 74 69   First instructi
676a0 6f 6e 20 6f 66 20 69 6e 74 65 72 69 6f 72 20 6f  on of interior o
676b0 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20  f the loop */.  
676c0 75 38 20 69 46 72 6f 6d 3b 20 20 20 20 20 20 20  u8 iFrom;       
676d0 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68 20 65        /* Which e
676e0 6e 74 72 79 20 69 6e 20 74 68 65 20 46 52 4f 4d  ntry in the FROM
676f0 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 75 38 20   clause */.  u8 
67700 6f 70 2c 20 70 35 3b 20 20 20 20 20 20 20 20 20  op, p5;         
67710 20 20 20 2f 2a 20 4f 70 63 6f 64 65 20 61 6e 64     /* Opcode and
67720 20 50 35 20 6f 66 20 74 68 65 20 6f 70 63 6f 64   P5 of the opcod
67730 65 20 74 68 61 74 20 65 6e 64 73 20 74 68 65 20  e that ends the 
67740 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 70 31  loop */.  int p1
67750 2c 20 70 32 3b 20 20 20 20 20 20 20 20 20 20 20  , p2;           
67760 2f 2a 20 4f 70 65 72 61 6e 64 73 20 6f 66 20 74  /* Operands of t
67770 68 65 20 6f 70 63 6f 64 65 20 75 73 65 64 20 74  he opcode used t
67780 6f 20 65 6e 64 73 20 74 68 65 20 6c 6f 6f 70 20  o ends the loop 
67790 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20  */.  union {    
677a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
677b0 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 64  formation that d
677c0 65 70 65 6e 64 73 20 6f 6e 20 70 6c 61 6e 2e 77  epends on plan.w
677d0 73 46 6c 61 67 73 20 2a 2f 0a 20 20 20 20 73 74  sFlags */.    st
677e0 72 75 63 74 20 7b 0a 20 20 20 20 20 20 69 6e 74  ruct {.      int
677f0 20 6e 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20   nIn;           
67800 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
67810 65 6e 74 72 69 65 73 20 69 6e 20 61 49 6e 4c 6f  entries in aInLo
67820 6f 70 5b 5d 20 2a 2f 0a 20 20 20 20 20 20 73 74  op[] */.      st
67830 72 75 63 74 20 49 6e 4c 6f 6f 70 20 7b 0a 20 20  ruct InLoop {.  
67840 20 20 20 20 20 20 69 6e 74 20 69 43 75 72 3b 20        int iCur; 
67850 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
67860 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  The VDBE cursor 
67870 75 73 65 64 20 62 79 20 74 68 69 73 20 49 4e 20  used by this IN 
67880 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20  operator */.    
67890 20 20 20 20 69 6e 74 20 61 64 64 72 49 6e 54 6f      int addrInTo
678a0 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f  p;         /* To
678b0 70 20 6f 66 20 74 68 65 20 49 4e 20 6c 6f 6f 70  p of the IN loop
678c0 20 2a 2f 0a 20 20 20 20 20 20 7d 20 2a 61 49 6e   */.      } *aIn
678d0 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20  Loop;           
678e0 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  /* Information a
678f0 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74 65 64  bout each nested
67900 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a   IN operator */.
67910 20 20 20 20 7d 20 69 6e 3b 20 20 20 20 20 20 20      } in;       
67920 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
67930 64 20 77 68 65 6e 20 70 6c 61 6e 2e 77 73 46 6c  d when plan.wsFl
67940 61 67 73 26 57 48 45 52 45 5f 49 4e 5f 41 42 4c  ags&WHERE_IN_ABL
67950 45 20 2a 2f 0a 20 20 7d 20 75 3b 0a 0a 20 20 2f  E */.  } u;..  /
67960 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
67970 66 69 65 6c 64 20 69 73 20 72 65 61 6c 6c 79 20  field is really 
67980 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 20  not part of the 
67990 63 75 72 72 65 6e 74 20 6c 65 76 65 6c 2e 20 20  current level.  
679a0 42 75 74 0a 20 20 2a 2a 20 77 65 20 6e 65 65 64  But.  ** we need
679b0 20 61 20 70 6c 61 63 65 20 74 6f 20 63 61 63 68   a place to cach
679c0 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
679d0 69 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74 69 6f  index informatio
679e0 6e 20 66 6f 72 20 65 61 63 68 0a 20 20 2a 2a 20  n for each.  ** 
679f0 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e  virtual table in
67a00 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
67a10 20 61 6e 64 20 74 68 65 20 57 68 65 72 65 4c 65   and the WhereLe
67a20 76 65 6c 20 73 74 72 75 63 74 75 72 65 20 69 73  vel structure is
67a30 0a 20 20 2a 2a 20 61 20 63 6f 6e 76 65 6e 69 65  .  ** a convenie
67a40 6e 74 20 70 6c 61 63 65 20 73 69 6e 63 65 20 74  nt place since t
67a50 68 65 72 65 20 69 73 20 6f 6e 65 20 57 68 65 72  here is one Wher
67a60 65 4c 65 76 65 6c 20 66 6f 72 20 65 61 63 68 20  eLevel for each 
67a70 46 52 4f 4d 20 63 6c 61 75 73 65 0a 20 20 2a 2a  FROM clause.  **
67a80 20 65 6c 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20   element..  */. 
67a90 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
67aa0 6e 66 6f 20 2a 70 49 64 78 49 6e 66 6f 3b 20 20  nfo *pIdxInfo;  
67ab0 2f 2a 20 49 6e 64 65 78 20 69 6e 66 6f 20 66 6f  /* Index info fo
67ac0 72 20 6e 2d 74 68 20 73 6f 75 72 63 65 20 74 61  r n-th source ta
67ad0 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ble */.};../*.**
67ae0 20 46 6c 61 67 73 20 61 70 70 72 6f 70 72 69 61   Flags appropria
67af0 74 65 20 66 6f 72 20 74 68 65 20 77 63 74 72 6c  te for the wctrl
67b00 46 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20  Flags parameter 
67b10 6f 66 20 73 71 6c 69 74 65 33 57 68 65 72 65 42  of sqlite3WhereB
67b20 65 67 69 6e 28 29 0a 2a 2a 20 61 6e 64 20 74 68  egin().** and th
67b30 65 20 57 68 65 72 65 49 6e 66 6f 2e 77 63 74 72  e WhereInfo.wctr
67b40 6c 46 6c 61 67 73 20 6d 65 6d 62 65 72 2e 0a 2a  lFlags member..*
67b50 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
67b60 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 20 20  ORDERBY_NORMAL  
67b70 20 30 78 30 30 30 30 20 2f 2a 20 4e 6f 2d 6f 70   0x0000 /* No-op
67b80 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
67b90 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 20 20 20  E_ORDERBY_MIN   
67ba0 20 20 20 30 78 30 30 30 31 20 2f 2a 20 4f 52 44     0x0001 /* ORD
67bb0 45 52 20 42 59 20 70 72 6f 63 65 73 73 69 6e 67  ER BY processing
67bc0 20 66 6f 72 20 6d 69 6e 28 29 20 66 75 6e 63 20   for min() func 
67bd0 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
67be0 5f 4f 52 44 45 52 42 59 5f 4d 41 58 20 20 20 20  _ORDERBY_MAX    
67bf0 20 20 30 78 30 30 30 32 20 2f 2a 20 4f 52 44 45    0x0002 /* ORDE
67c00 52 20 42 59 20 70 72 6f 63 65 73 73 69 6e 67 20  R BY processing 
67c10 66 6f 72 20 6d 61 78 28 29 20 66 75 6e 63 20 2a  for max() func *
67c20 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
67c30 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44 20  ONEPASS_DESIRED 
67c40 20 30 78 30 30 30 34 20 2f 2a 20 57 61 6e 74 20   0x0004 /* Want 
67c50 74 6f 20 64 6f 20 6f 6e 65 2d 70 61 73 73 20 55  to do one-pass U
67c60 50 44 41 54 45 2f 44 45 4c 45 54 45 20 2a 2f 0a  PDATE/DELETE */.
67c70 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 44 55  #define WHERE_DU
67c80 50 4c 49 43 41 54 45 53 5f 4f 4b 20 20 20 20 30  PLICATES_OK    0
67c90 78 30 30 30 38 20 2f 2a 20 4f 6b 20 74 6f 20 72  x0008 /* Ok to r
67ca0 65 74 75 72 6e 20 61 20 72 6f 77 20 6d 6f 72 65  eturn a row more
67cb0 20 74 68 61 6e 20 6f 6e 63 65 20 2a 2f 0a 23 64   than once */.#d
67cc0 65 66 69 6e 65 20 57 48 45 52 45 5f 4f 4d 49 54  efine WHERE_OMIT
67cd0 5f 4f 50 45 4e 20 20 20 20 20 20 20 20 30 78 30  _OPEN        0x0
67ce0 30 31 30 20 2f 2a 20 54 61 62 6c 65 20 63 75 72  010 /* Table cur
67cf0 73 6f 72 20 61 72 65 20 61 6c 72 65 61 64 79 20  sor are already 
67d00 6f 70 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  open */.#define 
67d10 57 48 45 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45  WHERE_OMIT_CLOSE
67d20 20 20 20 20 20 20 20 30 78 30 30 32 30 20 2f 2a         0x0020 /*
67d30 20 4f 6d 69 74 20 63 6c 6f 73 65 20 6f 66 20 74   Omit close of t
67d40 61 62 6c 65 20 26 20 69 6e 64 65 78 20 63 75 72  able & index cur
67d50 73 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  sors */.#define 
67d60 57 48 45 52 45 5f 46 4f 52 43 45 5f 54 41 42 4c  WHERE_FORCE_TABL
67d70 45 20 20 20 20 20 20 30 78 30 30 34 30 20 2f 2a  E      0x0040 /*
67d80 20 44 6f 20 6e 6f 74 20 75 73 65 20 61 6e 20 69   Do not use an i
67d90 6e 64 65 78 2d 6f 6e 6c 79 20 73 65 61 72 63 68  ndex-only search
67da0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 57   */../*.** The W
67db0 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63  HERE clause proc
67dc0 65 73 73 69 6e 67 20 72 6f 75 74 69 6e 65 20 68  essing routine h
67dd0 61 73 20 74 77 6f 20 68 61 6c 76 65 73 2e 20 20  as two halves.  
67de0 54 68 65 0a 2a 2a 20 66 69 72 73 74 20 70 61 72  The.** first par
67df0 74 20 64 6f 65 73 20 74 68 65 20 73 74 61 72 74  t does the start
67e00 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c 6f   of the WHERE lo
67e10 6f 70 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e  op and the secon
67e20 64 0a 2a 2a 20 68 61 6c 66 20 64 6f 65 73 20 74  d.** half does t
67e30 68 65 20 74 61 69 6c 20 6f 66 20 74 68 65 20 57  he tail of the W
67e40 48 45 52 45 20 6c 6f 6f 70 2e 20 20 41 6e 20 69  HERE loop.  An i
67e50 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68  nstance of.** th
67e60 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
67e70 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20  returned by the 
67e80 66 69 72 73 74 20 68 61 6c 66 20 61 6e 64 20 70  first half and p
67e90 61 73 73 65 64 0a 2a 2a 20 69 6e 74 6f 20 74 68  assed.** into th
67ea0 65 20 73 65 63 6f 6e 64 20 68 61 6c 66 20 74 6f  e second half to
67eb0 20 67 69 76 65 20 73 6f 6d 65 20 63 6f 6e 74 69   give some conti
67ec0 6e 75 69 74 79 2e 0a 2a 2f 0a 73 74 72 75 63 74  nuity..*/.struct
67ed0 20 57 68 65 72 65 49 6e 66 6f 20 7b 0a 20 20 50   WhereInfo {.  P
67ee0 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20  arse *pParse;   
67ef0 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61      /* Parsing a
67f00 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69  nd code generati
67f10 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
67f20 75 31 36 20 77 63 74 72 6c 46 6c 61 67 73 3b 20  u16 wctrlFlags; 
67f30 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 6f 72       /* Flags or
67f40 69 67 69 6e 61 6c 6c 79 20 70 61 73 73 65 64 20  iginally passed 
67f50 74 6f 20 73 71 6c 69 74 65 33 57 68 65 72 65 42  to sqlite3WhereB
67f60 65 67 69 6e 28 29 20 2a 2f 0a 20 20 75 38 20 6f  egin() */.  u8 o
67f70 6b 4f 6e 65 50 61 73 73 3b 20 20 20 20 20 20 20  kOnePass;       
67f80 20 2f 2a 20 4f 6b 20 74 6f 20 75 73 65 20 6f 6e   /* Ok to use on
67f90 65 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d  e-pass algorithm
67fa0 20 66 6f 72 20 55 50 44 41 54 45 20 6f 72 20 44   for UPDATE or D
67fb0 45 4c 45 54 45 20 2a 2f 0a 20 20 53 72 63 4c 69  ELETE */.  SrcLi
67fc0 73 74 20 2a 70 54 61 62 4c 69 73 74 3b 20 20 20  st *pTabList;   
67fd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73            /* Lis
67fe0 74 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74  t of tables in t
67ff0 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 69 6e 74  he join */.  int
68000 20 69 54 6f 70 3b 20 20 20 20 20 20 20 20 20 20   iTop;          
68010 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
68020 68 65 20 76 65 72 79 20 62 65 67 69 6e 6e 69 6e  he very beginnin
68030 67 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 6c  g of the WHERE l
68040 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f  oop */.  int iCo
68050 6e 74 69 6e 75 65 3b 20 20 20 20 20 20 20 20 20  ntinue;         
68060 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20          /* Jump 
68070 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75 65  here to continue
68080 20 77 69 74 68 20 6e 65 78 74 20 72 65 63 6f 72   with next recor
68090 64 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65 61  d */.  int iBrea
680a0 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
680b0 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65        /* Jump he
680c0 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20  re to break out 
680d0 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  of the loop */. 
680e0 20 69 6e 74 20 6e 4c 65 76 65 6c 3b 20 20 20 20   int nLevel;    
680f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68100 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73  /* Number of nes
68110 74 65 64 20 6c 6f 6f 70 20 2a 2f 0a 20 20 73 74  ted loop */.  st
68120 72 75 63 74 20 57 68 65 72 65 43 6c 61 75 73 65  ruct WhereClause
68130 20 2a 70 57 43 3b 20 20 20 20 20 20 20 2f 2a 20   *pWC;       /* 
68140 44 65 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f 66  Decomposition of
68150 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
68160 65 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65  e */.  WhereLeve
68170 6c 20 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20  l a[1];         
68180 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61        /* Informa
68190 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20  tion about each 
681a0 6e 65 73 74 20 6c 6f 6f 70 20 69 6e 20 57 48 45  nest loop in WHE
681b0 52 45 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  RE */.};../*.** 
681c0 41 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 64 65  A NameContext de
681d0 66 69 6e 65 73 20 61 20 63 6f 6e 74 65 78 74 20  fines a context 
681e0 69 6e 20 77 68 69 63 68 20 74 6f 20 72 65 73 6f  in which to reso
681f0 6c 76 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f  lve table and co
68200 6c 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 73 2e 20 20  lumn.** names.  
68210 54 68 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e 73  The context cons
68220 69 73 74 73 20 6f 66 20 61 20 6c 69 73 74 20 6f  ists of a list o
68230 66 20 74 61 62 6c 65 73 20 28 74 68 65 20 70 53  f tables (the pS
68240 72 63 4c 69 73 74 29 20 66 69 65 6c 64 20 61 6e  rcList) field an
68250 64 0a 2a 2a 20 61 20 6c 69 73 74 20 6f 66 20 6e  d.** a list of n
68260 61 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20  amed expression 
68270 28 70 45 4c 69 73 74 29 2e 20 20 54 68 65 20 6e  (pEList).  The n
68280 61 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20  amed expression 
68290 6c 69 73 74 20 6d 61 79 0a 2a 2a 20 62 65 20 4e  list may.** be N
682a0 55 4c 4c 2e 20 20 54 68 65 20 70 53 72 63 20 63  ULL.  The pSrc c
682b0 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68  orresponds to th
682c0 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  e FROM clause of
682d0 20 61 20 53 45 4c 45 43 54 20 6f 72 0a 2a 2a 20   a SELECT or.** 
682e0 74 6f 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  to the table bei
682f0 6e 67 20 6f 70 65 72 61 74 65 64 20 6f 6e 20 62  ng operated on b
68300 79 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45  y INSERT, UPDATE
68310 2c 20 6f 72 20 44 45 4c 45 54 45 2e 20 20 54 68  , or DELETE.  Th
68320 65 0a 2a 2a 20 70 45 4c 69 73 74 20 63 6f 72 72  e.** pEList corr
68330 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 72  esponds to the r
68340 65 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53  esult set of a S
68350 45 4c 45 43 54 20 61 6e 64 20 69 73 20 4e 55 4c  ELECT and is NUL
68360 4c 20 66 6f 72 0a 2a 2a 20 6f 74 68 65 72 20 73  L for.** other s
68370 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  tatements..**.**
68380 20 4e 61 6d 65 43 6f 6e 74 65 78 74 73 20 63 61   NameContexts ca
68390 6e 20 62 65 20 6e 65 73 74 65 64 2e 20 20 57 68  n be nested.  Wh
683a0 65 6e 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d  en resolving nam
683b0 65 73 2c 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f  es, the inner-mo
683c0 73 74 20 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69  st .** context i
683d0 73 20 73 65 61 72 63 68 65 64 20 66 69 72 73 74  s searched first
683e0 2e 20 20 49 66 20 6e 6f 20 6d 61 74 63 68 20 69  .  If no match i
683f0 73 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e 65 78  s found, the nex
68400 74 20 6f 75 74 65 72 0a 2a 2a 20 63 6f 6e 74 65  t outer.** conte
68410 78 74 20 69 73 20 63 68 65 63 6b 65 64 2e 20 20  xt is checked.  
68420 49 66 20 74 68 65 72 65 20 69 73 20 73 74 69 6c  If there is stil
68430 6c 20 6e 6f 20 6d 61 74 63 68 2c 20 74 68 65 20  l no match, the 
68440 6e 65 78 74 20 63 6f 6e 74 65 78 74 0a 2a 2a 20  next context.** 
68450 69 73 20 63 68 65 63 6b 65 64 2e 20 20 54 68 69  is checked.  Thi
68460 73 20 70 72 6f 63 65 73 73 20 63 6f 6e 74 69 6e  s process contin
68470 75 65 73 20 75 6e 74 69 6c 20 65 69 74 68 65 72  ues until either
68480 20 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e   a match is foun
68490 64 0a 2a 2a 20 6f 72 20 61 6c 6c 20 63 6f 6e 74  d.** or all cont
684a0 65 78 74 73 20 61 72 65 20 63 68 65 63 6b 2e 20  exts are check. 
684b0 20 57 68 65 6e 20 61 20 6d 61 74 63 68 20 69 73   When a match is
684c0 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e 52 65 66   found, the nRef
684d0 20 6d 65 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 68   member of.** th
684e0 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e 74 61 69  e context contai
684f0 6e 69 6e 67 20 74 68 65 20 6d 61 74 63 68 20 69  ning the match i
68500 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 20 0a  s incremented. .
68510 2a 2a 0a 2a 2a 20 45 61 63 68 20 73 75 62 71 75  **.** Each subqu
68520 65 72 79 20 67 65 74 73 20 61 20 6e 65 77 20 4e  ery gets a new N
68530 61 6d 65 43 6f 6e 74 65 78 74 2e 20 20 54 68 65  ameContext.  The
68540 20 70 4e 65 78 74 20 66 69 65 6c 64 20 70 6f 69   pNext field poi
68550 6e 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 4e 61  nts to the.** Na
68560 6d 65 43 6f 6e 74 65 78 74 20 69 6e 20 74 68 65  meContext in the
68570 20 70 61 72 65 6e 74 20 71 75 65 72 79 2e 20 20   parent query.  
68580 54 68 75 73 20 74 68 65 20 70 72 6f 63 65 73 73  Thus the process
68590 20 6f 66 20 73 63 61 6e 6e 69 6e 67 20 74 68 65   of scanning the
685a0 0a 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20  .** NameContext 
685b0 6c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 73  list corresponds
685c0 20 74 6f 20 73 65 61 72 63 68 69 6e 67 20 74 68   to searching th
685d0 72 6f 75 67 68 20 73 75 63 63 65 73 73 69 76 65  rough successive
685e0 6c 79 20 6f 75 74 65 72 0a 2a 2a 20 73 75 62 71  ly outer.** subq
685f0 75 65 72 69 65 73 20 6c 6f 6f 6b 69 6e 67 20 66  ueries looking f
68600 6f 72 20 61 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73  or a match..*/.s
68610 74 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78  truct NameContex
68620 74 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61  t {.  Parse *pPa
68630 72 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 68  rse;       /* Th
68640 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 53 72  e parser */.  Sr
68650 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 3b  cList *pSrcList;
68660 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72     /* One or mor
68670 65 20 74 61 62 6c 65 73 20 75 73 65 64 20 74 6f  e tables used to
68680 20 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 2a   resolve names *
68690 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
686a0 4c 69 73 74 3b 20 20 20 20 2f 2a 20 4f 70 74 69  List;    /* Opti
686b0 6f 6e 61 6c 20 6c 69 73 74 20 6f 66 20 6e 61 6d  onal list of nam
686c0 65 64 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a  ed expressions *
686d0 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20  /.  int nRef;   
686e0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
686f0 65 72 20 6f 66 20 6e 61 6d 65 73 20 72 65 73 6f  er of names reso
68700 6c 76 65 64 20 62 79 20 74 68 69 73 20 63 6f 6e  lved by this con
68710 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 45  text */.  int nE
68720 72 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  rr;            /
68730 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f  * Number of erro
68740 72 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 77  rs encountered w
68750 68 69 6c 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e  hile resolving n
68760 61 6d 65 73 20 2a 2f 0a 20 20 75 38 20 61 6c 6c  ames */.  u8 all
68770 6f 77 41 67 67 3b 20 20 20 20 20 20 20 20 20 2f  owAgg;         /
68780 2a 20 41 67 67 72 65 67 61 74 65 20 66 75 6e 63  * Aggregate func
68790 74 69 6f 6e 73 20 61 6c 6c 6f 77 65 64 20 68 65  tions allowed he
687a0 72 65 20 2a 2f 0a 20 20 75 38 20 68 61 73 41 67  re */.  u8 hasAg
687b0 67 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  g;           /* 
687c0 54 72 75 65 20 69 66 20 61 67 67 72 65 67 61 74  True if aggregat
687d0 65 73 20 61 72 65 20 73 65 65 6e 20 2a 2f 0a 20  es are seen */. 
687e0 20 75 38 20 69 73 43 68 65 63 6b 3b 20 20 20 20   u8 isCheck;    
687f0 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
68800 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73   resolving names
68810 20 69 6e 20 61 20 43 48 45 43 4b 20 63 6f 6e 73   in a CHECK cons
68820 74 72 61 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  traint */.  int 
68830 6e 44 65 70 74 68 3b 20 20 20 20 20 20 20 20 20  nDepth;         
68840 20 2f 2a 20 44 65 70 74 68 20 6f 66 20 73 75 62   /* Depth of sub
68850 71 75 65 72 79 20 72 65 63 75 72 73 69 6f 6e 2e  query recursion.
68860 20 31 20 66 6f 72 20 6e 6f 20 72 65 63 75 72 73   1 for no recurs
68870 69 6f 6e 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f  ion */.  AggInfo
68880 20 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20 2f 2a   *pAggInfo;   /*
68890 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   Information abo
688a0 75 74 20 61 67 67 72 65 67 61 74 65 73 20 61 74  ut aggregates at
688b0 20 74 68 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20   this level */. 
688c0 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e   NameContext *pN
688d0 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 75  ext;  /* Next ou
688e0 74 65 72 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74  ter name context
688f0 2e 20 20 4e 55 4c 4c 20 66 6f 72 20 6f 75 74 65  .  NULL for oute
68900 72 6d 6f 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  rmost */.};../*.
68910 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
68920 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
68930 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
68940 6e 73 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69  ns all informati
68950 6f 6e 0a 2a 2a 20 6e 65 65 64 65 64 20 74 6f 20  on.** needed to 
68960 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  generate code fo
68970 72 20 61 20 73 69 6e 67 6c 65 20 53 45 4c 45 43  r a single SELEC
68980 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a  T statement..**.
68990 2a 2a 20 6e 4c 69 6d 69 74 20 69 73 20 73 65 74  ** nLimit is set
689a0 20 74 6f 20 2d 31 20 69 66 20 74 68 65 72 65 20   to -1 if there 
689b0 69 73 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75  is no LIMIT clau
689c0 73 65 2e 20 20 6e 4f 66 66 73 65 74 20 69 73 20  se.  nOffset is 
689d0 73 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 49 66 20  set to 0..** If 
689e0 74 68 65 72 65 20 69 73 20 61 20 4c 49 4d 49 54  there is a LIMIT
689f0 20 63 6c 61 75 73 65 2c 20 74 68 65 20 70 61 72   clause, the par
68a00 73 65 72 20 73 65 74 73 20 6e 4c 69 6d 69 74 20  ser sets nLimit 
68a10 74 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  to the value of 
68a20 74 68 65 0a 2a 2a 20 6c 69 6d 69 74 20 61 6e 64  the.** limit and
68a30 20 6e 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20   nOffset to the 
68a40 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 66 66  value of the off
68a50 73 65 74 20 28 6f 72 20 30 20 69 66 20 74 68 65  set (or 0 if the
68a60 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 6f 66 66  re is not.** off
68a70 73 65 74 29 2e 20 20 42 75 74 20 6c 61 74 65 72  set).  But later
68a80 20 6f 6e 2c 20 6e 4c 69 6d 69 74 20 61 6e 64 20   on, nLimit and 
68a90 6e 4f 66 66 73 65 74 20 62 65 63 6f 6d 65 20 74  nOffset become t
68aa0 68 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69  he memory locati
68ab0 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 56 44  ons.** in the VD
68ac0 42 45 20 74 68 61 74 20 72 65 63 6f 72 64 20 74  BE that record t
68ad0 68 65 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66  he limit and off
68ae0 73 65 74 20 63 6f 75 6e 74 65 72 73 2e 0a 2a 2a  set counters..**
68af0 0a 2a 2a 20 61 64 64 72 4f 70 65 6e 45 70 68 6d  .** addrOpenEphm
68b00 5b 5d 20 65 6e 74 72 69 65 73 20 63 6f 6e 74 61  [] entries conta
68b10 69 6e 20 74 68 65 20 61 64 64 72 65 73 73 20 6f  in the address o
68b20 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  f OP_OpenEphemer
68b30 61 6c 20 6f 70 63 6f 64 65 73 2e 0a 2a 2a 20 54  al opcodes..** T
68b40 68 65 73 65 20 61 64 64 72 65 73 73 65 73 20 6d  hese addresses m
68b50 75 73 74 20 62 65 20 73 74 6f 72 65 64 20 73 6f  ust be stored so
68b60 20 74 68 61 74 20 77 65 20 63 61 6e 20 67 6f 20   that we can go 
68b70 62 61 63 6b 20 61 6e 64 20 66 69 6c 6c 20 69 6e  back and fill in
68b80 0a 2a 2a 20 74 68 65 20 50 34 5f 4b 45 59 49 4e  .** the P4_KEYIN
68b90 46 4f 20 61 6e 64 20 50 32 20 70 61 72 61 6d 65  FO and P2 parame
68ba0 74 65 72 73 20 6c 61 74 65 72 2e 20 20 4e 65 69  ters later.  Nei
68bb0 74 68 65 72 20 74 68 65 20 4b 65 79 49 6e 66 6f  ther the KeyInfo
68bc0 20 6e 6f 72 0a 2a 2a 20 74 68 65 20 6e 75 6d 62   nor.** the numb
68bd0 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
68be0 20 50 32 20 63 61 6e 20 62 65 20 63 6f 6d 70 75   P2 can be compu
68bf0 74 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 20  ted at the same 
68c00 74 69 6d 65 0a 2a 2a 20 61 73 20 74 68 65 20 4f  time.** as the O
68c10 50 5f 4f 70 65 6e 45 70 68 6d 20 69 6e 73 74 72  P_OpenEphm instr
68c20 75 63 74 69 6f 6e 20 69 73 20 63 6f 64 65 64 20  uction is coded 
68c30 62 65 63 61 75 73 65 20 6e 6f 74 0a 2a 2a 20 65  because not.** e
68c40 6e 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f  nough informatio
68c50 6e 20 61 62 6f 75 74 20 74 68 65 20 63 6f 6d 70  n about the comp
68c60 6f 75 6e 64 20 71 75 65 72 79 20 69 73 20 6b 6e  ound query is kn
68c70 6f 77 6e 20 61 74 20 74 68 61 74 20 70 6f 69 6e  own at that poin
68c80 74 2e 0a 2a 2a 20 54 68 65 20 4b 65 79 49 6e 66  t..** The KeyInf
68c90 6f 20 66 6f 72 20 61 64 64 72 4f 70 65 6e 54 72  o for addrOpenTr
68ca0 61 6e 5b 30 5d 20 61 6e 64 20 5b 31 5d 20 63 6f  an[0] and [1] co
68cb0 6e 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67  ntains collating
68cc0 20 73 65 71 75 65 6e 63 65 73 0a 2a 2a 20 66 6f   sequences.** fo
68cd0 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  r the result set
68ce0 2e 20 20 54 68 65 20 4b 65 79 49 6e 66 6f 20 66  .  The KeyInfo f
68cf0 6f 72 20 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b  or addrOpenTran[
68d00 32 5d 20 63 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c  2] contains coll
68d10 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63  ating.** sequenc
68d20 65 73 20 66 6f 72 20 74 68 65 20 4f 52 44 45 52  es for the ORDER
68d30 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73   BY clause..*/.s
68d40 74 72 75 63 74 20 53 65 6c 65 63 74 20 7b 0a 20  truct Select {. 
68d50 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73   ExprList *pELis
68d60 74 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66  t;      /* The f
68d70 69 65 6c 64 73 20 6f 66 20 74 68 65 20 72 65 73  ields of the res
68d80 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20  ult */.  u8 op; 
68d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
68da0 2f 2a 20 4f 6e 65 20 6f 66 3a 20 54 4b 5f 55 4e  /* One of: TK_UN
68db0 49 4f 4e 20 54 4b 5f 41 4c 4c 20 54 4b 5f 49 4e  ION TK_ALL TK_IN
68dc0 54 45 52 53 45 43 54 20 54 4b 5f 45 58 43 45 50  TERSECT TK_EXCEP
68dd0 54 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69  T */.  char affi
68de0 6e 69 74 79 3b 20 20 20 20 20 20 20 20 20 2f 2a  nity;         /*
68df0 20 4d 61 6b 65 52 65 63 6f 72 64 20 77 69 74 68   MakeRecord with
68e00 20 74 68 69 73 20 61 66 66 69 6e 69 74 79 20 66   this affinity f
68e10 6f 72 20 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20  or SRT_Set */.  
68e20 75 31 36 20 73 65 6c 46 6c 61 67 73 3b 20 20 20  u16 selFlags;   
68e30 20 20 20 20 20 20 20 2f 2a 20 56 61 72 69 6f 75         /* Variou
68e40 73 20 53 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f  s SF_* values */
68e50 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63  .  SrcList *pSrc
68e60 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ;         /* The
68e70 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a   FROM clause */.
68e80 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20    Expr *pWhere; 
68e90 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
68ea0 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
68eb0 20 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f    ExprList *pGro
68ec0 75 70 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20  upBy;    /* The 
68ed0 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
68ee0 2a 2f 0a 20 20 45 78 70 72 20 2a 70 48 61 76 69  */.  Expr *pHavi
68ef0 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ng;         /* T
68f00 68 65 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65  he HAVING clause
68f10 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
68f20 70 4f 72 64 65 72 42 79 3b 20 20 20 20 2f 2a 20  pOrderBy;    /* 
68f30 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  The ORDER BY cla
68f40 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  use */.  Select 
68f50 2a 70 50 72 69 6f 72 3b 20 20 20 20 20 20 20 20  *pPrior;        
68f60 2f 2a 20 50 72 69 6f 72 20 73 65 6c 65 63 74 20  /* Prior select 
68f70 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65  in a compound se
68f80 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a  lect statement *
68f90 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 78  /.  Select *pNex
68fa0 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65  t;         /* Ne
68fb0 78 74 20 73 65 6c 65 63 74 20 74 6f 20 74 68 65  xt select to the
68fc0 20 6c 65 66 74 20 69 6e 20 61 20 63 6f 6d 70 6f   left in a compo
68fd0 75 6e 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  und */.  Select 
68fe0 2a 70 52 69 67 68 74 6d 6f 73 74 3b 20 20 20 20  *pRightmost;    
68ff0 2f 2a 20 52 69 67 68 74 2d 6d 6f 73 74 20 73 65  /* Right-most se
69000 6c 65 63 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75  lect in a compou
69010 6e 64 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d  nd select statem
69020 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ent */.  Expr *p
69030 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20  Limit;          
69040 2f 2a 20 4c 49 4d 49 54 20 65 78 70 72 65 73 73  /* LIMIT express
69050 69 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 20  ion. NULL means 
69060 6e 6f 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20 45  not used. */.  E
69070 78 70 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20  xpr *pOffset;   
69080 20 20 20 20 20 20 2f 2a 20 4f 46 46 53 45 54 20        /* OFFSET 
69090 65 78 70 72 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c  expression. NULL
690a0 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73 65 64 2e   means not used.
690b0 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74   */.  int iLimit
690c0 2c 20 69 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20  , iOffset;   /* 
690d0 4d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73  Memory registers
690e0 20 68 6f 6c 64 69 6e 67 20 4c 49 4d 49 54 20 26   holding LIMIT &
690f0 20 4f 46 46 53 45 54 20 63 6f 75 6e 74 65 72 73   OFFSET counters
69100 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f 70   */.  int addrOp
69110 65 6e 45 70 68 6d 5b 33 5d 3b 20 20 20 2f 2a 20  enEphm[3];   /* 
69120 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 20 6f 70 63  OP_OpenEphem opc
69130 6f 64 65 73 20 72 65 6c 61 74 65 64 20 74 6f 20  odes related to 
69140 74 68 69 73 20 73 65 6c 65 63 74 20 2a 2f 0a 7d  this select */.}
69150 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64  ;../*.** Allowed
69160 20 76 61 6c 75 65 73 20 66 6f 72 20 53 65 6c 65   values for Sele
69170 63 74 2e 73 65 6c 46 6c 61 67 73 2e 20 20 54 68  ct.selFlags.  Th
69180 65 20 22 53 46 22 20 70 72 65 66 69 78 20 73 74  e "SF" prefix st
69190 61 6e 64 73 20 66 6f 72 0a 2a 2a 20 22 53 65 6c  ands for.** "Sel
691a0 65 63 74 20 46 6c 61 67 22 2e 0a 2a 2f 0a 23 64  ect Flag"..*/.#d
691b0 65 66 69 6e 65 20 53 46 5f 44 69 73 74 69 6e 63  efine SF_Distinc
691c0 74 20 20 20 20 20 20 20 20 30 78 30 30 30 31 20  t        0x0001 
691d0 20 2f 2a 20 4f 75 74 70 75 74 20 73 68 6f 75 6c   /* Output shoul
691e0 64 20 62 65 20 44 49 53 54 49 4e 43 54 20 2a 2f  d be DISTINCT */
691f0 0a 23 64 65 66 69 6e 65 20 53 46 5f 52 65 73 6f  .#define SF_Reso
69200 6c 76 65 64 20 20 20 20 20 20 20 20 30 78 30 30  lved        0x00
69210 30 32 20 20 2f 2a 20 49 64 65 6e 74 69 66 69 65  02  /* Identifie
69220 72 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 73  rs have been res
69230 6f 6c 76 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  olved */.#define
69240 20 53 46 5f 41 67 67 72 65 67 61 74 65 20 20 20   SF_Aggregate   
69250 20 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 43      0x0004  /* C
69260 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 74  ontains aggregat
69270 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23  e functions */.#
69280 64 65 66 69 6e 65 20 53 46 5f 55 73 65 73 45 70  define SF_UsesEp
69290 68 65 6d 65 72 61 6c 20 20 20 30 78 30 30 30 38  hemeral   0x0008
692a0 20 20 2f 2a 20 55 73 65 73 20 74 68 65 20 4f 70    /* Uses the Op
692b0 65 6e 45 70 68 65 6d 65 72 61 6c 20 6f 70 63 6f  enEphemeral opco
692c0 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46  de */.#define SF
692d0 5f 45 78 70 61 6e 64 65 64 20 20 20 20 20 20 20  _Expanded       
692e0 20 30 78 30 30 31 30 20 20 2f 2a 20 73 71 6c 69   0x0010  /* sqli
692f0 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28  te3SelectExpand(
69300 29 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 69 73  ) called on this
69310 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 48   */.#define SF_H
69320 61 73 54 79 70 65 49 6e 66 6f 20 20 20 20 20 30  asTypeInfo     0
69330 78 30 30 32 30 20 20 2f 2a 20 46 52 4f 4d 20 73  x0020  /* FROM s
69340 75 62 71 75 65 72 69 65 73 20 68 61 76 65 20 54  ubqueries have T
69350 61 62 6c 65 20 6d 65 74 61 64 61 74 61 20 2a 2f  able metadata */
69360 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 73  .../*.** The res
69370 75 6c 74 73 20 6f 66 20 61 20 73 65 6c 65 63 74  ults of a select
69380 20 63 61 6e 20 62 65 20 64 69 73 74 72 69 62 75   can be distribu
69390 74 65 64 20 69 6e 20 73 65 76 65 72 61 6c 20 77  ted in several w
693a0 61 79 73 2e 20 20 54 68 65 0a 2a 2a 20 22 53 52  ays.  The.** "SR
693b0 54 22 20 70 72 65 66 69 78 20 6d 65 61 6e 73 20  T" prefix means 
693c0 22 53 45 4c 45 43 54 20 52 65 73 75 6c 74 20 54  "SELECT Result T
693d0 79 70 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  ype"..*/.#define
693e0 20 53 52 54 5f 55 6e 69 6f 6e 20 20 20 20 20 20   SRT_Union      
693f0 20 20 31 20 20 2f 2a 20 53 74 6f 72 65 20 72 65    1  /* Store re
69400 73 75 6c 74 20 61 73 20 6b 65 79 73 20 69 6e 20  sult as keys in 
69410 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66  an index */.#def
69420 69 6e 65 20 53 52 54 5f 45 78 63 65 70 74 20 20  ine SRT_Except  
69430 20 20 20 20 20 32 20 20 2f 2a 20 52 65 6d 6f 76       2  /* Remov
69440 65 20 72 65 73 75 6c 74 20 66 72 6f 6d 20 61 20  e result from a 
69450 55 4e 49 4f 4e 20 69 6e 64 65 78 20 2a 2f 0a 23  UNION index */.#
69460 64 65 66 69 6e 65 20 53 52 54 5f 45 78 69 73 74  define SRT_Exist
69470 73 20 20 20 20 20 20 20 33 20 20 2f 2a 20 53 74  s       3  /* St
69480 6f 72 65 20 31 20 69 66 20 74 68 65 20 72 65 73  ore 1 if the res
69490 75 6c 74 20 69 73 20 6e 6f 74 20 65 6d 70 74 79  ult is not empty
694a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f   */.#define SRT_
694b0 44 69 73 63 61 72 64 20 20 20 20 20 20 34 20 20  Discard      4  
694c0 2f 2a 20 44 6f 20 6e 6f 74 20 73 61 76 65 20 74  /* Do not save t
694d0 68 65 20 72 65 73 75 6c 74 73 20 61 6e 79 77 68  he results anywh
694e0 65 72 65 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 4f  ere */../* The O
694f0 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69  RDER BY clause i
69500 73 20 69 67 6e 6f 72 65 64 20 66 6f 72 20 61 6c  s ignored for al
69510 6c 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 2a  l of the above *
69520 2f 0a 23 64 65 66 69 6e 65 20 49 67 6e 6f 72 61  /.#define Ignora
69530 62 6c 65 4f 72 64 65 72 62 79 28 58 29 20 28 28  bleOrderby(X) ((
69540 58 2d 3e 65 44 65 73 74 29 3c 3d 53 52 54 5f 44  X->eDest)<=SRT_D
69550 69 73 63 61 72 64 29 0a 0a 23 64 65 66 69 6e 65  iscard)..#define
69560 20 53 52 54 5f 4f 75 74 70 75 74 20 20 20 20 20   SRT_Output     
69570 20 20 35 20 20 2f 2a 20 4f 75 74 70 75 74 20 65    5  /* Output e
69580 61 63 68 20 72 6f 77 20 6f 66 20 72 65 73 75 6c  ach row of resul
69590 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54  t */.#define SRT
695a0 5f 4d 65 6d 20 20 20 20 20 20 20 20 20 20 36 20  _Mem          6 
695b0 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74   /* Store result
695c0 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c   in a memory cel
695d0 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54  l */.#define SRT
695e0 5f 53 65 74 20 20 20 20 20 20 20 20 20 20 37 20  _Set          7 
695f0 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74   /* Store result
69600 73 20 61 73 20 6b 65 79 73 20 69 6e 20 61 6e 20  s as keys in an 
69610 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65  index */.#define
69620 20 53 52 54 5f 54 61 62 6c 65 20 20 20 20 20 20   SRT_Table      
69630 20 20 38 20 20 2f 2a 20 53 74 6f 72 65 20 72 65    8  /* Store re
69640 73 75 6c 74 20 61 73 20 64 61 74 61 20 77 69 74  sult as data wit
69650 68 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72  h an automatic r
69660 6f 77 69 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  owid */.#define 
69670 53 52 54 5f 45 70 68 65 6d 54 61 62 20 20 20 20  SRT_EphemTab    
69680 20 39 20 20 2f 2a 20 43 72 65 61 74 65 20 74 72   9  /* Create tr
69690 61 6e 73 69 65 6e 74 20 74 61 62 20 61 6e 64 20  ansient tab and 
696a0 73 74 6f 72 65 20 6c 69 6b 65 20 53 52 54 5f 54  store like SRT_T
696b0 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  able */.#define 
696c0 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 20 20  SRT_Coroutine   
696d0 31 30 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  10  /* Generate 
696e0 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20  a single row of 
696f0 72 65 73 75 6c 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  result */../*.**
69700 20 41 20 73 74 72 75 63 74 75 72 65 20 75 73 65   A structure use
69710 64 20 74 6f 20 63 75 73 74 6f 6d 69 7a 65 20 74  d to customize t
69720 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20 73  he behavior of s
69730 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 2e 20  qlite3Select(). 
69740 53 65 65 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20  See.** comments 
69750 61 62 6f 76 65 20 73 71 6c 69 74 65 33 53 65 6c  above sqlite3Sel
69760 65 63 74 28 29 20 66 6f 72 20 64 65 74 61 69 6c  ect() for detail
69770 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  s..*/.typedef st
69780 72 75 63 74 20 53 65 6c 65 63 74 44 65 73 74 20  ruct SelectDest 
69790 53 65 6c 65 63 74 44 65 73 74 3b 0a 73 74 72 75  SelectDest;.stru
697a0 63 74 20 53 65 6c 65 63 74 44 65 73 74 20 7b 0a  ct SelectDest {.
697b0 20 20 75 38 20 65 44 65 73 74 3b 20 20 20 20 20    u8 eDest;     
697c0 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69      /* How to di
697d0 73 70 6f 73 65 20 6f 66 20 74 68 65 20 72 65 73  spose of the res
697e0 75 6c 74 73 20 2a 2f 0a 20 20 75 38 20 61 66 66  ults */.  u8 aff
697f0 69 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 41  inity;      /* A
69800 66 66 69 6e 69 74 79 20 75 73 65 64 20 77 68 65  ffinity used whe
69810 6e 20 65 44 65 73 74 3d 3d 53 52 54 5f 53 65 74  n eDest==SRT_Set
69820 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 6d 3b   */.  int iParm;
69830 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70 61 72          /* A par
69840 61 6d 65 74 65 72 20 75 73 65 64 20 62 79 20 74  ameter used by t
69850 68 65 20 65 44 65 73 74 20 64 69 73 70 6f 73 61  he eDest disposa
69860 6c 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e  l method */.  in
69870 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20  t iMem;         
69880 2f 2a 20 42 61 73 65 20 72 65 67 69 73 74 65 72  /* Base register
69890 20 77 68 65 72 65 20 72 65 73 75 6c 74 73 20 61   where results a
698a0 72 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20  re written */.  
698b0 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20  int nMem;       
698c0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
698d0 65 67 69 73 74 65 72 73 20 61 6c 6c 6f 63 61 74  egisters allocat
698e0 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ed */.};../*.** 
698f0 44 75 72 69 6e 67 20 63 6f 64 65 20 67 65 6e 65  During code gene
69900 72 61 74 69 6f 6e 20 6f 66 20 73 74 61 74 65 6d  ration of statem
69910 65 6e 74 73 20 74 68 61 74 20 64 6f 20 69 6e 73  ents that do ins
69920 65 72 74 73 20 69 6e 74 6f 20 41 55 54 4f 49 4e  erts into AUTOIN
69930 43 52 45 4d 45 4e 54 20 0a 2a 2a 20 74 61 62 6c  CREMENT .** tabl
69940 65 73 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  es, the followin
69950 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73  g information is
69960 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 65   attached to the
69970 20 54 61 62 6c 65 2e 75 2e 61 75 74 6f 49 6e 63   Table.u.autoInc
69980 2e 70 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 6f 66  .p.** pointer of
69990 20 65 61 63 68 20 61 75 74 6f 69 6e 63 72 65 6d   each autoincrem
699a0 65 6e 74 20 74 61 62 6c 65 20 74 6f 20 72 65 63  ent table to rec
699b0 6f 72 64 20 73 6f 6d 65 20 73 69 64 65 20 69 6e  ord some side in
699c0 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 0a 2a  formation that.*
699d0 2a 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72  * the code gener
699e0 61 74 6f 72 20 6e 65 65 64 73 2e 20 20 57 65 20  ator needs.  We 
699f0 68 61 76 65 20 74 6f 20 6b 65 65 70 20 70 65 72  have to keep per
69a00 2d 74 61 62 6c 65 20 61 75 74 6f 69 6e 63 72 65  -table autoincre
69a10 6d 65 6e 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74  ment.** informat
69a20 69 6f 6e 20 69 6e 20 63 61 73 65 20 69 6e 73 65  ion in case inse
69a30 72 74 73 20 61 72 65 20 64 6f 77 6e 20 77 69 74  rts are down wit
69a40 68 69 6e 20 74 72 69 67 67 65 72 73 2e 20 20 54  hin triggers.  T
69a50 72 69 67 67 65 72 73 20 64 6f 20 6e 6f 74 0a 2a  riggers do not.*
69a60 2a 20 6e 6f 72 6d 61 6c 6c 79 20 63 6f 6f 72 64  * normally coord
69a70 69 6e 61 74 65 20 74 68 65 69 72 20 61 63 74 69  inate their acti
69a80 76 69 74 69 65 73 2c 20 62 75 74 20 77 65 20 64  vities, but we d
69a90 6f 20 6e 65 65 64 20 74 6f 20 63 6f 6f 72 64 69  o need to coordi
69aa0 6e 61 74 65 20 74 68 65 0a 2a 2a 20 6c 6f 61 64  nate the.** load
69ab0 69 6e 67 20 61 6e 64 20 73 61 76 69 6e 67 20 6f  ing and saving o
69ac0 66 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20  f autoincrement 
69ad0 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  information..*/.
69ae0 73 74 72 75 63 74 20 41 75 74 6f 69 6e 63 49 6e  struct AutoincIn
69af0 66 6f 20 7b 0a 20 20 41 75 74 6f 69 6e 63 49 6e  fo {.  AutoincIn
69b00 66 6f 20 2a 70 4e 65 78 74 3b 20 20 20 2f 2a 20  fo *pNext;   /* 
69b10 4e 65 78 74 20 69 6e 66 6f 20 62 6c 6f 63 6b 20  Next info block 
69b20 69 6e 20 61 20 6c 69 73 74 20 6f 66 20 74 68 65  in a list of the
69b30 6d 20 61 6c 6c 20 2a 2f 0a 20 20 54 61 62 6c 65  m all */.  Table
69b40 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20   *pTab;         
69b50 20 2f 2a 20 54 61 62 6c 65 20 74 68 69 73 20 69   /* Table this i
69b60 6e 66 6f 20 62 6c 6f 63 6b 20 72 65 66 65 72 73  nfo block refers
69b70 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62   to */.  int iDb
69b80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
69b90 2a 20 49 6e 64 65 78 20 69 6e 20 73 71 6c 69 74  * Index in sqlit
69ba0 65 33 2e 61 44 62 5b 5d 20 6f 66 20 64 61 74 61  e3.aDb[] of data
69bb0 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61  base holding pTa
69bc0 62 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 43 74  b */.  int regCt
69bd0 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r;           /* 
69be0 4d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 20  Memory register 
69bf0 68 6f 6c 64 69 6e 67 20 74 68 65 20 72 6f 77 69  holding the rowi
69c00 64 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 7d 3b 0a  d counter */.};.
69c10 0a 2f 2a 0a 2a 2a 20 53 69 7a 65 20 6f 66 20 74  ./*.** Size of t
69c20 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 0a  he column cache.
69c30 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
69c40 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 0a 23 20 64  E_N_COLCACHE.# d
69c50 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 43  efine SQLITE_N_C
69c60 4f 4c 43 41 43 48 45 20 31 30 0a 23 65 6e 64 69  OLCACHE 10.#endi
69c70 66 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73  f../*.** At leas
69c80 74 20 6f 6e 65 20 69 6e 73 74 61 6e 63 65 20 6f  t one instance o
69c90 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
69ca0 73 74 72 75 63 74 75 72 65 20 69 73 20 63 72 65  structure is cre
69cb0 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 0a 2a  ated for each .*
69cc0 2a 20 74 72 69 67 67 65 72 20 74 68 61 74 20 6d  * trigger that m
69cd0 61 79 20 62 65 20 66 69 72 65 64 20 77 68 69 6c  ay be fired whil
69ce0 65 20 70 61 72 73 69 6e 67 20 61 6e 20 49 4e 53  e parsing an INS
69cf0 45 52 54 2c 20 55 50 44 41 54 45 20 6f 72 20 44  ERT, UPDATE or D
69d00 45 4c 45 54 45 0a 2a 2a 20 73 74 61 74 65 6d 65  ELETE.** stateme
69d10 6e 74 2e 20 41 6c 6c 20 73 75 63 68 20 6f 62 6a  nt. All such obj
69d20 65 63 74 73 20 61 72 65 20 73 74 6f 72 65 64 20  ects are stored 
69d30 69 6e 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69  in the linked li
69d40 73 74 20 68 65 61 64 65 64 20 61 74 0a 2a 2a 20  st headed at.** 
69d50 50 61 72 73 65 2e 70 54 72 69 67 67 65 72 50 72  Parse.pTriggerPr
69d60 67 20 61 6e 64 20 64 65 6c 65 74 65 64 20 6f 6e  g and deleted on
69d70 63 65 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d  ce statement com
69d80 70 69 6c 61 74 69 6f 6e 20 68 61 73 20 62 65 65  pilation has bee
69d90 6e 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 64 2e 0a  n.** completed..
69da0 2a 2a 0a 2a 2a 20 41 20 56 64 62 65 20 73 75 62  **.** A Vdbe sub
69db0 2d 70 72 6f 67 72 61 6d 20 74 68 61 74 20 69 6d  -program that im
69dc0 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 62 6f 64  plements the bod
69dd0 79 20 61 6e 64 20 57 48 45 4e 20 63 6c 61 75 73  y and WHEN claus
69de0 65 20 6f 66 20 74 72 69 67 67 65 72 0a 2a 2a 20  e of trigger.** 
69df0 54 72 69 67 67 65 72 50 72 67 2e 70 54 72 69 67  TriggerPrg.pTrig
69e00 67 65 72 2c 20 61 73 73 75 6d 69 6e 67 20 61 20  ger, assuming a 
69e10 64 65 66 61 75 6c 74 20 4f 4e 20 43 4f 4e 46 4c  default ON CONFL
69e20 49 43 54 20 63 6c 61 75 73 65 20 6f 66 0a 2a 2a  ICT clause of.**
69e30 20 54 72 69 67 67 65 72 50 72 67 2e 6f 72 63 6f   TriggerPrg.orco
69e40 6e 66 2c 20 69 73 20 73 74 6f 72 65 64 20 69 6e  nf, is stored in
69e50 20 74 68 65 20 54 72 69 67 67 65 72 50 72 67 2e   the TriggerPrg.
69e60 70 50 72 6f 67 72 61 6d 20 76 61 72 69 61 62 6c  pProgram variabl
69e70 65 2e 0a 2a 2a 20 54 68 65 20 50 61 72 73 65 2e  e..** The Parse.
69e80 70 54 72 69 67 67 65 72 50 72 67 20 6c 69 73 74  pTriggerPrg list
69e90 20 6e 65 76 65 72 20 63 6f 6e 74 61 69 6e 73 20   never contains 
69ea0 74 77 6f 20 65 6e 74 72 69 65 73 20 77 69 74 68  two entries with
69eb0 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 76 61 6c   the same.** val
69ec0 75 65 73 20 66 6f 72 20 62 6f 74 68 20 70 54 72  ues for both pTr
69ed0 69 67 67 65 72 20 61 6e 64 20 6f 72 63 6f 6e 66  igger and orconf
69ee0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54 72 69 67  ..**.** The Trig
69ef0 67 65 72 50 72 67 2e 6f 6c 64 6d 61 73 6b 20 76  gerPrg.oldmask v
69f00 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74  ariable is set t
69f10 6f 20 61 20 6d 61 73 6b 20 6f 66 20 6f 6c 64 2e  o a mask of old.
69f20 2a 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 61 63 63  * columns.** acc
69f30 65 73 73 65 64 20 28 6f 72 20 73 65 74 20 74 6f  essed (or set to
69f40 20 30 20 66 6f 72 20 74 72 69 67 67 65 72 73 20   0 for triggers 
69f50 66 69 72 65 64 20 61 73 20 61 20 72 65 73 75 6c  fired as a resul
69f60 74 20 6f 66 20 49 4e 53 45 52 54 20 0a 2a 2a 20  t of INSERT .** 
69f70 73 74 61 74 65 6d 65 6e 74 73 29 2e 0a 2a 2f 0a  statements)..*/.
69f80 73 74 72 75 63 74 20 54 72 69 67 67 65 72 50 72  struct TriggerPr
69f90 67 20 7b 0a 20 20 54 72 69 67 67 65 72 20 2a 70  g {.  Trigger *p
69fa0 54 72 69 67 67 65 72 3b 20 20 20 20 20 20 2f 2a  Trigger;      /*
69fb0 20 54 72 69 67 67 65 72 20 74 68 69 73 20 70 72   Trigger this pr
69fc0 6f 67 72 61 6d 20 77 61 73 20 63 6f 64 65 64 20  ogram was coded 
69fd0 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 6f 72  from */.  int or
69fe0 63 6f 6e 66 3b 20 20 20 20 20 20 20 20 20 20 20  conf;           
69ff0 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e 20    /* Default ON 
6a000 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20  CONFLICT policy 
6a010 2a 2f 0a 20 20 53 75 62 50 72 6f 67 72 61 6d 20  */.  SubProgram 
6a020 2a 70 50 72 6f 67 72 61 6d 3b 20 20 20 2f 2a 20  *pProgram;   /* 
6a030 50 72 6f 67 72 61 6d 20 69 6d 70 6c 65 6d 65 6e  Program implemen
6a040 74 69 6e 67 20 70 54 72 69 67 67 65 72 2f 6f 72  ting pTrigger/or
6a050 63 6f 6e 66 20 2a 2f 0a 20 20 75 33 32 20 6f 6c  conf */.  u32 ol
6a060 64 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20  dmask;          
6a070 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 6f 6c 64    /* Mask of old
6a080 2e 2a 20 63 6f 6c 75 6d 6e 73 20 61 63 63 65 73  .* columns acces
6a090 73 65 64 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  sed */.  Trigger
6a0a0 50 72 67 20 2a 70 4e 65 78 74 3b 20 20 20 20 20  Prg *pNext;     
6a0b0 20 2f 2a 20 4e 65 78 74 20 65 6e 74 72 79 20 69   /* Next entry i
6a0c0 6e 20 50 61 72 73 65 2e 70 54 72 69 67 67 65 72  n Parse.pTrigger
6a0d0 50 72 67 20 6c 69 73 74 20 2a 2f 0a 7d 3b 0a 0a  Prg list */.};..
6a0e0 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20 70 61 72  /*.** An SQL par
6a0f0 73 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 41 20  ser context.  A 
6a100 63 6f 70 79 20 6f 66 20 74 68 69 73 20 73 74 72  copy of this str
6a110 75 63 74 75 72 65 20 69 73 20 70 61 73 73 65 64  ucture is passed
6a120 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68 65 20   through.** the 
6a130 70 61 72 73 65 72 20 61 6e 64 20 64 6f 77 6e 20  parser and down 
6a140 69 6e 74 6f 20 61 6c 6c 20 74 68 65 20 70 61 72  into all the par
6a150 73 65 72 20 61 63 74 69 6f 6e 20 72 6f 75 74 69  ser action routi
6a160 6e 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a  ne in order to.*
6a170 2a 20 63 61 72 72 79 20 61 72 6f 75 6e 64 20 69  * carry around i
6a180 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20  nformation that 
6a190 69 73 20 67 6c 6f 62 61 6c 20 74 6f 20 74 68 65  is global to the
6a1a0 20 65 6e 74 69 72 65 20 70 61 72 73 65 2e 0a 2a   entire parse..*
6a1b0 2a 0a 2a 2a 20 54 68 65 20 73 74 72 75 63 74 75  *.** The structu
6a1c0 72 65 20 69 73 20 64 69 76 69 64 65 64 20 69 6e  re is divided in
6a1d0 74 6f 20 74 77 6f 20 70 61 72 74 73 2e 20 20 57  to two parts.  W
6a1e0 68 65 6e 20 74 68 65 20 70 61 72 73 65 72 20 61  hen the parser a
6a1f0 6e 64 20 63 6f 64 65 0a 2a 2a 20 67 65 6e 65 72  nd code.** gener
6a200 61 74 65 20 63 61 6c 6c 20 74 68 65 6d 73 65 6c  ate call themsel
6a210 76 65 73 20 72 65 63 75 72 73 69 76 65 6c 79 2c  ves recursively,
6a220 20 74 68 65 20 66 69 72 73 74 20 70 61 72 74 20   the first part 
6a230 6f 66 20 74 68 65 20 73 74 72 75 63 74 75 72 65  of the structure
6a240 0a 2a 2a 20 69 73 20 63 6f 6e 73 74 61 6e 74 20  .** is constant 
6a250 62 75 74 20 74 68 65 20 73 65 63 6f 6e 64 20 70  but the second p
6a260 61 72 74 20 69 73 20 72 65 73 65 74 20 61 74 20  art is reset at 
6a270 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e  the beginning an
6a280 64 20 65 6e 64 20 6f 66 0a 2a 2a 20 65 61 63 68  d end of.** each
6a290 20 72 65 63 75 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a   recursion..**.*
6a2a0 2a 20 54 68 65 20 6e 54 61 62 6c 65 4c 6f 63 6b  * The nTableLock
6a2b0 20 61 6e 64 20 61 54 61 62 6c 65 4c 6f 63 6b 20   and aTableLock 
6a2c0 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 6f 6e  variables are on
6a2d0 6c 79 20 75 73 65 64 20 69 66 20 74 68 65 20 73  ly used if the s
6a2e0 68 61 72 65 64 2d 63 61 63 68 65 20 0a 2a 2a 20  hared-cache .** 
6a2f0 66 65 61 74 75 72 65 20 69 73 20 65 6e 61 62 6c  feature is enabl
6a300 65 64 20 28 69 66 20 73 71 6c 69 74 65 33 54 73  ed (if sqlite3Ts
6a310 64 28 29 2d 3e 75 73 65 53 68 61 72 65 64 44 61  d()->useSharedDa
6a320 74 61 20 69 73 20 74 72 75 65 29 2e 20 54 68 65  ta is true). The
6a330 79 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f  y are.** used to
6a340 20 73 74 6f 72 65 20 74 68 65 20 73 65 74 20 6f   store the set o
6a350 66 20 74 61 62 6c 65 2d 6c 6f 63 6b 73 20 72 65  f table-locks re
6a360 71 75 69 72 65 64 20 62 79 20 74 68 65 20 73 74  quired by the st
6a370 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 0a 2a 2a  atement being.**
6a380 20 63 6f 6d 70 69 6c 65 64 2e 20 46 75 6e 63 74   compiled. Funct
6a390 69 6f 6e 20 73 71 6c 69 74 65 33 54 61 62 6c 65  ion sqlite3Table
6a3a0 4c 6f 63 6b 28 29 20 69 73 20 75 73 65 64 20 74  Lock() is used t
6a3b0 6f 20 61 64 64 20 65 6e 74 72 69 65 73 20 74 6f  o add entries to
6a3c0 20 74 68 65 0a 2a 2a 20 6c 69 73 74 2e 0a 2a 2f   the.** list..*/
6a3d0 0a 73 74 72 75 63 74 20 50 61 72 73 65 20 7b 0a  .struct Parse {.
6a3e0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
6a3f0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61         /* The ma
6a400 69 6e 20 64 61 74 61 62 61 73 65 20 73 74 72 75  in database stru
6a410 63 74 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72  cture */.  int r
6a420 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
6a430 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66  /* Return code f
6a440 72 6f 6d 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f  rom execution */
6a450 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  .  char *zErrMsg
6a460 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 72  ;       /* An er
6a470 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20  ror message */. 
6a480 20 56 64 62 65 20 2a 70 56 64 62 65 3b 20 20 20   Vdbe *pVdbe;   
6a490 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 6e 67 69        /* An engi
6a4a0 6e 65 20 66 6f 72 20 65 78 65 63 75 74 69 6e 67  ne for executing
6a4b0 20 64 61 74 61 62 61 73 65 20 62 79 74 65 63 6f   database byteco
6a4c0 64 65 20 2a 2f 0a 20 20 75 38 20 63 6f 6c 4e 61  de */.  u8 colNa
6a4d0 6d 65 73 53 65 74 3b 20 20 20 20 20 20 2f 2a 20  mesSet;      /* 
6a4e0 54 52 55 45 20 61 66 74 65 72 20 4f 50 5f 43 6f  TRUE after OP_Co
6a4f0 6c 75 6d 6e 4e 61 6d 65 20 68 61 73 20 62 65 65  lumnName has bee
6a500 6e 20 69 73 73 75 65 64 20 74 6f 20 70 56 64 62  n issued to pVdb
6a510 65 20 2a 2f 0a 20 20 75 38 20 6e 61 6d 65 43 6c  e */.  u8 nameCl
6a520 61 73 68 3b 20 20 20 20 20 20 20 20 2f 2a 20 41  ash;        /* A
6a530 20 70 65 72 6d 61 6e 65 6e 74 20 74 61 62 6c 65   permanent table
6a540 20 6e 61 6d 65 20 63 6c 61 73 68 65 73 20 77 69   name clashes wi
6a550 74 68 20 74 65 6d 70 20 74 61 62 6c 65 20 6e 61  th temp table na
6a560 6d 65 20 2a 2f 0a 20 20 75 38 20 63 68 65 63 6b  me */.  u8 check
6a570 53 63 68 65 6d 61 3b 20 20 20 20 20 20 2f 2a 20  Schema;      /* 
6a580 43 61 75 73 65 73 20 73 63 68 65 6d 61 20 63 6f  Causes schema co
6a590 6f 6b 69 65 20 63 68 65 63 6b 20 61 66 74 65 72  okie check after
6a5a0 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 75   an error */.  u
6a5b0 38 20 6e 65 73 74 65 64 3b 20 20 20 20 20 20 20  8 nested;       
6a5c0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
6a5d0 20 6e 65 73 74 65 64 20 63 61 6c 6c 73 20 74 6f   nested calls to
6a5e0 20 74 68 65 20 70 61 72 73 65 72 2f 63 6f 64 65   the parser/code
6a5f0 20 67 65 6e 65 72 61 74 6f 72 20 2a 2f 0a 20 20   generator */.  
6a600 75 38 20 70 61 72 73 65 45 72 72 6f 72 3b 20 20  u8 parseError;  
6a610 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74       /* True aft
6a620 65 72 20 61 20 70 61 72 73 69 6e 67 20 65 72 72  er a parsing err
6a630 6f 72 2e 20 20 54 69 63 6b 65 74 20 23 31 37 39  or.  Ticket #179
6a640 34 20 2a 2f 0a 20 20 75 38 20 6e 54 65 6d 70 52  4 */.  u8 nTempR
6a650 65 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  eg;         /* N
6a660 75 6d 62 65 72 20 6f 66 20 74 65 6d 70 6f 72 61  umber of tempora
6a670 72 79 20 72 65 67 69 73 74 65 72 73 20 69 6e 20  ry registers in 
6a680 61 54 65 6d 70 52 65 67 5b 5d 20 2a 2f 0a 20 20  aTempReg[] */.  
6a690 75 38 20 6e 54 65 6d 70 49 6e 55 73 65 3b 20 20  u8 nTempInUse;  
6a6a0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
6a6b0 66 20 61 54 65 6d 70 52 65 67 5b 5d 20 63 75 72  f aTempReg[] cur
6a6c0 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f  rently checked o
6a6d0 75 74 20 2a 2f 0a 20 20 69 6e 74 20 61 54 65 6d  ut */.  int aTem
6a6e0 70 52 65 67 5b 38 5d 3b 20 20 20 20 20 2f 2a 20  pReg[8];     /* 
6a6f0 48 6f 6c 64 69 6e 67 20 61 72 65 61 20 66 6f 72  Holding area for
6a700 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73   temporary regis
6a710 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52  ters */.  int nR
6a720 61 6e 67 65 52 65 67 3b 20 20 20 20 20 20 20 2f  angeReg;       /
6a730 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 74 65  * Size of the te
6a740 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72  mporary register
6a750 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20   block */.  int 
6a760 69 52 61 6e 67 65 52 65 67 3b 20 20 20 20 20 20  iRangeReg;      
6a770 20 2f 2a 20 46 69 72 73 74 20 72 65 67 69 73 74   /* First regist
6a780 65 72 20 69 6e 20 74 65 6d 70 6f 72 61 72 79 20  er in temporary 
6a790 72 65 67 69 73 74 65 72 20 62 6c 6f 63 6b 20 2a  register block *
6a7a0 2f 0a 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20  /.  int nErr;   
6a7b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
6a7c0 65 72 20 6f 66 20 65 72 72 6f 72 73 20 73 65 65  er of errors see
6a7d0 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 61 62 3b  n */.  int nTab;
6a7e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
6a7f0 75 6d 62 65 72 20 6f 66 20 70 72 65 76 69 6f 75  umber of previou
6a800 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 56 44  sly allocated VD
6a810 42 45 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20  BE cursors */.  
6a820 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20  int nMem;       
6a830 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
6a840 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 75  f memory cells u
6a850 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20  sed so far */.  
6a860 69 6e 74 20 6e 53 65 74 3b 20 20 20 20 20 20 20  int nSet;       
6a870 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
6a880 66 20 73 65 74 73 20 75 73 65 64 20 73 6f 20 66  f sets used so f
6a890 61 72 20 2a 2f 0a 20 20 69 6e 74 20 63 6b 42 61  ar */.  int ckBa
6a8a0 73 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  se;          /* 
6a8b0 42 61 73 65 20 72 65 67 69 73 74 65 72 20 6f 66  Base register of
6a8c0 20 64 61 74 61 20 64 75 72 69 6e 67 20 63 68 65   data during che
6a8d0 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a  ck constraints *
6a8e0 2f 0a 20 20 69 6e 74 20 69 43 61 63 68 65 4c 65  /.  int iCacheLe
6a8f0 76 65 6c 3b 20 20 20 20 20 2f 2a 20 43 6f 6c 43  vel;     /* ColC
6a900 61 63 68 65 20 76 61 6c 69 64 20 77 68 65 6e 20  ache valid when 
6a910 61 43 6f 6c 43 61 63 68 65 5b 5d 2e 69 4c 65 76  aColCache[].iLev
6a920 65 6c 3c 3d 69 43 61 63 68 65 4c 65 76 65 6c 20  el<=iCacheLevel 
6a930 2a 2f 0a 20 20 69 6e 74 20 69 43 61 63 68 65 43  */.  int iCacheC
6a940 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 75  nt;       /* Cou
6a950 6e 74 65 72 20 75 73 65 64 20 74 6f 20 67 65 6e  nter used to gen
6a960 65 72 61 74 65 20 61 43 6f 6c 43 61 63 68 65 5b  erate aColCache[
6a970 5d 2e 6c 72 75 20 76 61 6c 75 65 73 20 2a 2f 0a  ].lru values */.
6a980 20 20 75 38 20 6e 43 6f 6c 43 61 63 68 65 3b 20    u8 nColCache; 
6a990 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6a9a0 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74   of entries in t
6a9b0 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20  he column cache 
6a9c0 2a 2f 0a 20 20 75 38 20 69 43 6f 6c 43 61 63 68  */.  u8 iColCach
6a9d0 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78  e;        /* Nex
6a9e0 74 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 63  t entry of the c
6a9f0 61 63 68 65 20 74 6f 20 72 65 70 6c 61 63 65 20  ache to replace 
6aa00 2a 2f 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c  */.  struct yCol
6aa10 43 61 63 68 65 20 7b 0a 20 20 20 20 69 6e 74 20  Cache {.    int 
6aa20 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20  iTable;         
6aa30 20 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73 6f    /* Table curso
6aa40 72 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20  r number */.    
6aa50 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20  int iColumn;    
6aa60 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63        /* Table c
6aa70 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a  olumn number */.
6aa80 20 20 20 20 75 38 20 61 66 66 43 68 61 6e 67 65      u8 affChange
6aa90 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ;         /* Tru
6aaa0 65 20 69 66 20 74 68 69 73 20 72 65 67 69 73 74  e if this regist
6aab0 65 72 20 68 61 73 20 68 61 64 20 61 6e 20 61 66  er has had an af
6aac0 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20 2a 2f  finity change */
6aad0 0a 20 20 20 20 75 38 20 74 65 6d 70 52 65 67 3b  .    u8 tempReg;
6aae0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 52             /* iR
6aaf0 65 67 20 69 73 20 61 20 74 65 6d 70 20 72 65 67  eg is a temp reg
6ab00 69 73 74 65 72 20 74 68 61 74 20 6e 65 65 64 73  ister that needs
6ab10 20 74 6f 20 62 65 20 66 72 65 65 64 20 2a 2f 0a   to be freed */.
6ab20 20 20 20 20 69 6e 74 20 69 4c 65 76 65 6c 3b 20      int iLevel; 
6ab30 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 73            /* Nes
6ab40 74 69 6e 67 20 6c 65 76 65 6c 20 2a 2f 0a 20 20  ting level */.  
6ab50 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20 20 20    int iReg;     
6ab60 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 20 77          /* Reg w
6ab70 69 74 68 20 76 61 6c 75 65 20 6f 66 20 74 68 69  ith value of thi
6ab80 73 20 63 6f 6c 75 6d 6e 2e 20 30 20 6d 65 61 6e  s column. 0 mean
6ab90 73 20 6e 6f 6e 65 2e 20 2a 2f 0a 20 20 20 20 69  s none. */.    i
6aba0 6e 74 20 6c 72 75 3b 20 20 20 20 20 20 20 20 20  nt lru;         
6abb0 20 20 20 20 20 2f 2a 20 4c 65 61 73 74 20 72 65       /* Least re
6abc0 63 65 6e 74 6c 79 20 75 73 65 64 20 65 6e 74 72  cently used entr
6abd0 79 20 68 61 73 20 74 68 65 20 73 6d 61 6c 6c 65  y has the smalle
6abe0 73 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20 7d 20  st value */.  } 
6abf0 61 43 6f 6c 43 61 63 68 65 5b 53 51 4c 49 54 45  aColCache[SQLITE
6ac00 5f 4e 5f 43 4f 4c 43 41 43 48 45 5d 3b 20 20 2f  _N_COLCACHE];  /
6ac10 2a 20 4f 6e 65 20 66 6f 72 20 65 61 63 68 20 63  * One for each c
6ac20 6f 6c 75 6d 6e 20 63 61 63 68 65 20 65 6e 74 72  olumn cache entr
6ac30 79 20 2a 2f 0a 20 20 75 33 32 20 77 72 69 74 65  y */.  u32 write
6ac40 4d 61 73 6b 3b 20 20 20 20 20 20 20 2f 2a 20 53  Mask;       /* S
6ac50 74 61 72 74 20 61 20 77 72 69 74 65 20 74 72 61  tart a write tra
6ac60 6e 73 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 73  nsaction on thes
6ac70 65 20 64 61 74 61 62 61 73 65 73 20 2a 2f 0a 20  e databases */. 
6ac80 20 75 33 32 20 63 6f 6f 6b 69 65 4d 61 73 6b 3b   u32 cookieMask;
6ac90 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b        /* Bitmask
6aca0 20 6f 66 20 73 63 68 65 6d 61 20 76 65 72 69 66   of schema verif
6acb0 69 65 64 20 64 61 74 61 62 61 73 65 73 20 2a 2f  ied databases */
6acc0 0a 20 20 75 38 20 69 73 4d 75 6c 74 69 57 72 69  .  u8 isMultiWri
6acd0 74 65 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20  te;     /* True 
6ace0 69 66 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 79  if statement may
6acf0 20 61 66 66 65 63 74 2f 69 6e 73 65 72 74 20 6d   affect/insert m
6ad00 75 6c 74 69 70 6c 65 20 72 6f 77 73 20 2a 2f 0a  ultiple rows */.
6ad10 20 20 75 38 20 6d 61 79 41 62 6f 72 74 3b 20 20    u8 mayAbort;  
6ad20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
6ad30 66 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 79 20  f statement may 
6ad40 74 68 72 6f 77 20 61 6e 20 41 42 4f 52 54 20 65  throw an ABORT e
6ad50 78 63 65 70 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  xception */.  in
6ad60 74 20 63 6f 6f 6b 69 65 47 6f 74 6f 3b 20 20 20  t cookieGoto;   
6ad70 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
6ad80 20 4f 50 5f 47 6f 74 6f 20 74 6f 20 63 6f 6f 6b   OP_Goto to cook
6ad90 69 65 20 76 65 72 69 66 69 65 72 20 73 75 62 72  ie verifier subr
6ada0 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  outine */.  int 
6adb0 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 53 51 4c 49  cookieValue[SQLI
6adc0 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 2b  TE_MAX_ATTACHED+
6add0 32 5d 3b 20 20 2f 2a 20 56 61 6c 75 65 73 20 6f  2];  /* Values o
6ade0 66 20 63 6f 6f 6b 69 65 73 20 74 6f 20 76 65 72  f cookies to ver
6adf0 69 66 79 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ify */.#ifndef S
6ae00 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
6ae10 44 5f 43 41 43 48 45 0a 20 20 69 6e 74 20 6e 54  D_CACHE.  int nT
6ae20 61 62 6c 65 4c 6f 63 6b 3b 20 20 20 20 20 20 20  ableLock;       
6ae30 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c 6f   /* Number of lo
6ae40 63 6b 73 20 69 6e 20 61 54 61 62 6c 65 4c 6f 63  cks in aTableLoc
6ae50 6b 20 2a 2f 0a 20 20 54 61 62 6c 65 4c 6f 63 6b  k */.  TableLock
6ae60 20 2a 61 54 61 62 6c 65 4c 6f 63 6b 3b 20 2f 2a   *aTableLock; /*
6ae70 20 52 65 71 75 69 72 65 64 20 74 61 62 6c 65 20   Required table 
6ae80 6c 6f 63 6b 73 20 66 6f 72 20 73 68 61 72 65 64  locks for shared
6ae90 2d 63 61 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23  -cache mode */.#
6aea0 65 6e 64 69 66 0a 20 20 69 6e 74 20 72 65 67 52  endif.  int regR
6aeb0 6f 77 69 64 3b 20 20 20 20 20 20 20 20 2f 2a 20  owid;        /* 
6aec0 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67  Register holding
6aed0 20 72 6f 77 69 64 20 6f 66 20 43 52 45 41 54 45   rowid of CREATE
6aee0 20 54 41 42 4c 45 20 65 6e 74 72 79 20 2a 2f 0a   TABLE entry */.
6aef0 20 20 69 6e 74 20 72 65 67 52 6f 6f 74 3b 20 20    int regRoot;  
6af00 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
6af10 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 6f 74 20  er holding root 
6af20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20  page number for 
6af30 6e 65 77 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20  new objects */. 
6af40 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 2a 70 41   AutoincInfo *pA
6af50 69 6e 63 3b 20 20 2f 2a 20 49 6e 66 6f 72 6d 61  inc;  /* Informa
6af60 74 69 6f 6e 20 61 62 6f 75 74 20 41 55 54 4f 49  tion about AUTOI
6af70 4e 43 52 45 4d 45 4e 54 20 63 6f 75 6e 74 65 72  NCREMENT counter
6af80 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 78 41  s */.  int nMaxA
6af90 72 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  rg;         /* M
6afa0 61 78 20 61 72 67 73 20 70 61 73 73 65 64 20 74  ax args passed t
6afb0 6f 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20  o user function 
6afc0 62 79 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a  by sub-program *
6afd0 2f 0a 0a 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74  /..  /* Informat
6afe0 69 6f 6e 20 75 73 65 64 20 77 68 69 6c 65 20 63  ion used while c
6aff0 6f 64 69 6e 67 20 74 72 69 67 67 65 72 20 70 72  oding trigger pr
6b000 6f 67 72 61 6d 73 2e 20 2a 2f 0a 20 20 50 61 72  ograms. */.  Par
6b010 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c 3b 20 20  se *pToplevel;  
6b020 20 20 2f 2a 20 50 61 72 73 65 20 73 74 72 75 63    /* Parse struc
6b030 74 75 72 65 20 66 6f 72 20 6d 61 69 6e 20 70 72  ture for main pr
6b040 6f 67 72 61 6d 20 28 6f 72 20 4e 55 4c 4c 29 20  ogram (or NULL) 
6b050 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 72 69  */.  Table *pTri
6b060 67 67 65 72 54 61 62 3b 20 20 2f 2a 20 54 61 62  ggerTab;  /* Tab
6b070 6c 65 20 74 72 69 67 67 65 72 73 20 61 72 65 20  le triggers are 
6b080 62 65 69 6e 67 20 63 6f 64 65 64 20 66 6f 72 20  being coded for 
6b090 2a 2f 0a 20 20 75 33 32 20 6f 6c 64 6d 61 73 6b  */.  u32 oldmask
6b0a0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73  ;         /* Mas
6b0b0 6b 20 6f 66 20 6f 6c 64 2e 2a 20 63 6f 6c 75 6d  k of old.* colum
6b0c0 6e 73 20 72 65 66 65 72 65 6e 63 65 64 20 2a 2f  ns referenced */
6b0d0 0a 20 20 75 38 20 65 54 72 69 67 67 65 72 4f 70  .  u8 eTriggerOp
6b0e0 3b 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f 55 50  ;       /* TK_UP
6b0f0 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 20  DATE, TK_INSERT 
6b100 6f 72 20 54 4b 5f 44 45 4c 45 54 45 20 2a 2f 0a  or TK_DELETE */.
6b110 20 20 75 38 20 65 4f 72 63 6f 6e 66 3b 20 20 20    u8 eOrconf;   
6b120 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c         /* Defaul
6b130 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f  t ON CONFLICT po
6b140 6c 69 63 79 20 66 6f 72 20 74 72 69 67 67 65 72  licy for trigger
6b150 20 73 74 65 70 73 20 2a 2f 0a 20 20 75 38 20 64   steps */.  u8 d
6b160 69 73 61 62 6c 65 54 72 69 67 67 65 72 73 3b 20  isableTriggers; 
6b170 20 2f 2a 20 54 72 75 65 20 74 6f 20 64 69 73 61   /* True to disa
6b180 62 6c 65 20 74 72 69 67 67 65 72 73 20 2a 2f 0a  ble triggers */.
6b190 0a 20 20 2f 2a 20 41 62 6f 76 65 20 69 73 20 63  .  /* Above is c
6b1a0 6f 6e 73 74 61 6e 74 20 62 65 74 77 65 65 6e 20  onstant between 
6b1b0 72 65 63 75 72 73 69 6f 6e 73 2e 20 20 42 65 6c  recursions.  Bel
6b1c0 6f 77 20 69 73 20 72 65 73 65 74 20 62 65 66 6f  ow is reset befo
6b1d0 72 65 20 61 6e 64 20 61 66 74 65 72 0a 20 20 2a  re and after.  *
6b1e0 2a 20 65 61 63 68 20 72 65 63 75 72 73 69 6f 6e  * each recursion
6b1f0 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 56 61 72 3b   */..  int nVar;
6b200 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
6b210 75 6d 62 65 72 20 6f 66 20 27 3f 27 20 76 61 72  umber of '?' var
6b220 69 61 62 6c 65 73 20 73 65 65 6e 20 69 6e 20 74  iables seen in t
6b230 68 65 20 53 51 4c 20 73 6f 20 66 61 72 20 2a 2f  he SQL so far */
6b240 0a 20 20 69 6e 74 20 6e 56 61 72 45 78 70 72 3b  .  int nVarExpr;
6b250 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
6b260 72 20 6f 66 20 75 73 65 64 20 73 6c 6f 74 73 20  r of used slots 
6b270 69 6e 20 61 70 56 61 72 45 78 70 72 5b 5d 20 2a  in apVarExpr[] *
6b280 2f 0a 20 20 69 6e 74 20 6e 56 61 72 45 78 70 72  /.  int nVarExpr
6b290 41 6c 6c 6f 63 3b 20 20 20 2f 2a 20 4e 75 6d 62  Alloc;   /* Numb
6b2a0 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20  er of allocated 
6b2b0 73 6c 6f 74 73 20 69 6e 20 61 70 56 61 72 45 78  slots in apVarEx
6b2c0 70 72 5b 5d 20 2a 2f 0a 20 20 45 78 70 72 20 2a  pr[] */.  Expr *
6b2d0 2a 61 70 56 61 72 45 78 70 72 3b 20 20 20 20 2f  *apVarExpr;    /
6b2e0 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 3a 61  * Pointers to :a
6b2f0 61 61 20 61 6e 64 20 24 61 61 61 61 20 77 69 6c  aa and $aaaa wil
6b300 64 63 61 72 64 20 65 78 70 72 65 73 73 69 6f 6e  dcard expression
6b310 73 20 2a 2f 0a 20 20 56 64 62 65 20 2a 70 52 65  s */.  Vdbe *pRe
6b320 70 72 65 70 61 72 65 3b 20 20 20 20 2f 2a 20 56  prepare;    /* V
6b330 4d 20 62 65 69 6e 67 20 72 65 70 72 65 70 61 72  M being reprepar
6b340 65 64 20 28 73 71 6c 69 74 65 33 52 65 70 72 65  ed (sqlite3Repre
6b350 70 61 72 65 28 29 29 20 2a 2f 0a 20 20 69 6e 74  pare()) */.  int
6b360 20 6e 41 6c 69 61 73 3b 20 20 20 20 20 20 20 20   nAlias;        
6b370 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
6b380 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20 73 65  liased result se
6b390 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69  t columns */.  i
6b3a0 6e 74 20 6e 41 6c 69 61 73 41 6c 6c 6f 63 3b 20  nt nAliasAlloc; 
6b3b0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
6b3c0 20 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73   allocated slots
6b3d0 20 66 6f 72 20 61 41 6c 69 61 73 5b 5d 20 2a 2f   for aAlias[] */
6b3e0 0a 20 20 69 6e 74 20 2a 61 41 6c 69 61 73 3b 20  .  int *aAlias; 
6b3f0 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
6b400 74 65 72 20 75 73 65 64 20 74 6f 20 68 6f 6c 64  ter used to hold
6b410 20 61 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20   aliased result 
6b420 2a 2f 0a 20 20 75 38 20 65 78 70 6c 61 69 6e 3b  */.  u8 explain;
6b430 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
6b440 65 20 69 66 20 74 68 65 20 45 58 50 4c 41 49 4e  e if the EXPLAIN
6b450 20 66 6c 61 67 20 69 73 20 66 6f 75 6e 64 20 6f   flag is found o
6b460 6e 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20  n the query */. 
6b470 20 54 6f 6b 65 6e 20 73 4e 61 6d 65 54 6f 6b 65   Token sNameToke
6b480 6e 3b 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 77  n;    /* Token w
6b490 69 74 68 20 75 6e 71 75 61 6c 69 66 69 65 64 20  ith unqualified 
6b4a0 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 20 6e 61  schema object na
6b4b0 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73 4c  me */.  Token sL
6b4c0 61 73 74 54 6f 6b 65 6e 3b 20 20 20 20 2f 2a 20  astToken;    /* 
6b4d0 54 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e 20 70  The last token p
6b4e0 61 72 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  arsed */.  const
6b4f0 20 63 68 61 72 20 2a 7a 54 61 69 6c 3b 20 20 20   char *zTail;   
6b500 2f 2a 20 41 6c 6c 20 53 51 4c 20 74 65 78 74 20  /* All SQL text 
6b510 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 73 65  past the last se
6b520 6d 69 63 6f 6c 6f 6e 20 70 61 72 73 65 64 20 2a  micolon parsed *
6b530 2f 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 54  /.  Table *pNewT
6b540 61 62 6c 65 3b 20 20 20 20 2f 2a 20 41 20 74 61  able;    /* A ta
6b550 62 6c 65 20 62 65 69 6e 67 20 63 6f 6e 73 74 72  ble being constr
6b560 75 63 74 65 64 20 62 79 20 43 52 45 41 54 45 20  ucted by CREATE 
6b570 54 41 42 4c 45 20 2a 2f 0a 20 20 54 72 69 67 67  TABLE */.  Trigg
6b580 65 72 20 2a 70 4e 65 77 54 72 69 67 67 65 72 3b  er *pNewTrigger;
6b590 20 20 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20       /* Trigger 
6b5a0 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 20  under construct 
6b5b0 62 79 20 61 20 43 52 45 41 54 45 20 54 52 49 47  by a CREATE TRIG
6b5c0 47 45 52 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  GER */.  const c
6b5d0 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74 65 78  har *zAuthContex
6b5e0 74 3b 20 2f 2a 20 54 68 65 20 36 74 68 20 70 61  t; /* The 6th pa
6b5f0 72 61 6d 65 74 65 72 20 74 6f 20 64 62 2d 3e 78  rameter to db->x
6b600 41 75 74 68 20 63 61 6c 6c 62 61 63 6b 73 20 2a  Auth callbacks *
6b610 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
6b620 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
6b630 4c 45 0a 20 20 54 6f 6b 65 6e 20 73 41 72 67 3b  LE.  Token sArg;
6b640 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6b650 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74  /* Complete text
6b660 20 6f 66 20 61 20 6d 6f 64 75 6c 65 20 61 72 67   of a module arg
6b670 75 6d 65 6e 74 20 2a 2f 0a 20 20 75 38 20 64 65  ument */.  u8 de
6b680 63 6c 61 72 65 56 74 61 62 3b 20 20 20 20 20 20  clareVtab;      
6b690 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
6b6a0 20 69 6e 73 69 64 65 20 73 71 6c 69 74 65 33 5f   inside sqlite3_
6b6b0 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20 2a  declare_vtab() *
6b6c0 2f 0a 20 20 69 6e 74 20 6e 56 74 61 62 4c 6f 63  /.  int nVtabLoc
6b6d0 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  k;             /
6b6e0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 76 69 72 74  * Number of virt
6b6f0 75 61 6c 20 74 61 62 6c 65 73 20 74 6f 20 6c 6f  ual tables to lo
6b700 63 6b 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 2a  ck */.  Table **
6b710 61 70 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20 20  apVtabLock;     
6b720 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
6b730 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   virtual tables 
6b740 6e 65 65 64 69 6e 67 20 6c 6f 63 6b 69 6e 67 20  needing locking 
6b750 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20  */.#endif.  int 
6b760 6e 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20  nHeight;        
6b770 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f      /* Expressio
6b780 6e 20 74 72 65 65 20 68 65 69 67 68 74 20 6f 66  n tree height of
6b790 20 63 75 72 72 65 6e 74 20 73 75 62 2d 73 65 6c   current sub-sel
6b7a0 65 63 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ect */.  Table *
6b7b0 70 5a 6f 6d 62 69 65 54 61 62 3b 20 20 20 20 20  pZombieTab;     
6b7c0 20 2f 2a 20 4c 69 73 74 20 6f 66 20 54 61 62 6c   /* List of Tabl
6b7d0 65 20 6f 62 6a 65 63 74 73 20 74 6f 20 64 65 6c  e objects to del
6b7e0 65 74 65 20 61 66 74 65 72 20 63 6f 64 65 20 67  ete after code g
6b7f0 65 6e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 50  en */.  TriggerP
6b800 72 67 20 2a 70 54 72 69 67 67 65 72 50 72 67 3b  rg *pTriggerPrg;
6b810 20 20 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69      /* Linked li
6b820 73 74 20 6f 66 20 63 6f 64 65 64 20 74 72 69 67  st of coded trig
6b830 67 65 72 73 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64  gers */.};..#ifd
6b840 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
6b850 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 23 64  IRTUALTABLE.  #d
6b860 65 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41 52 45  efine IN_DECLARE
6b870 5f 56 54 41 42 20 30 0a 23 65 6c 73 65 0a 20 20  _VTAB 0.#else.  
6b880 23 64 65 66 69 6e 65 20 49 4e 5f 44 45 43 4c 41  #define IN_DECLA
6b890 52 45 5f 56 54 41 42 20 28 70 50 61 72 73 65 2d  RE_VTAB (pParse-
6b8a0 3e 64 65 63 6c 61 72 65 56 74 61 62 29 0a 23 65  >declareVtab).#e
6b8b0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  ndif../*.** An i
6b8c0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
6b8d0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
6b8e0 72 65 20 63 61 6e 20 62 65 20 64 65 63 6c 61 72  re can be declar
6b8f0 65 64 20 6f 6e 20 61 20 73 74 61 63 6b 20 61 6e  ed on a stack an
6b900 64 20 75 73 65 64 0a 2a 2a 20 74 6f 20 73 61 76  d used.** to sav
6b910 65 20 74 68 65 20 50 61 72 73 65 2e 7a 41 75 74  e the Parse.zAut
6b920 68 43 6f 6e 74 65 78 74 20 76 61 6c 75 65 20 73  hContext value s
6b930 6f 20 74 68 61 74 20 69 74 20 63 61 6e 20 62 65  o that it can be
6b940 20 72 65 73 74 6f 72 65 64 20 6c 61 74 65 72 2e   restored later.
6b950 0a 2a 2f 0a 73 74 72 75 63 74 20 41 75 74 68 43  .*/.struct AuthC
6b960 6f 6e 74 65 78 74 20 7b 0a 20 20 63 6f 6e 73 74  ontext {.  const
6b970 20 63 68 61 72 20 2a 7a 41 75 74 68 43 6f 6e 74   char *zAuthCont
6b980 65 78 74 3b 20 20 20 2f 2a 20 50 75 74 20 73 61  ext;   /* Put sa
6b990 76 65 64 20 50 61 72 73 65 2e 7a 41 75 74 68 43  ved Parse.zAuthC
6b9a0 6f 6e 74 65 78 74 20 68 65 72 65 20 2a 2f 0a 20  ontext here */. 
6b9b0 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20   Parse *pParse; 
6b9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6b9d0 54 68 65 20 50 61 72 73 65 20 73 74 72 75 63 74  The Parse struct
6b9e0 75 72 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ure */.};../*.**
6b9f0 20 42 69 74 66 69 65 6c 64 20 66 6c 61 67 73 20   Bitfield flags 
6ba00 66 6f 72 20 50 35 20 76 61 6c 75 65 20 69 6e 20  for P5 value in 
6ba10 4f 50 5f 49 6e 73 65 72 74 20 61 6e 64 20 4f 50  OP_Insert and OP
6ba20 5f 44 65 6c 65 74 65 0a 2a 2f 0a 23 64 65 66 69  _Delete.*/.#defi
6ba30 6e 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47  ne OPFLAG_NCHANG
6ba40 45 20 20 20 20 20 20 20 30 78 30 31 20 20 20 20  E       0x01    
6ba50 2f 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 65  /* Set to update
6ba60 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f 0a   db->nChange */.
6ba70 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f 4c  #define OPFLAG_L
6ba80 41 53 54 52 4f 57 49 44 20 20 20 20 20 30 78 30  ASTROWID     0x0
6ba90 32 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20 75  2    /* Set to u
6baa0 70 64 61 74 65 20 64 62 2d 3e 6c 61 73 74 52 6f  pdate db->lastRo
6bab0 77 69 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  wid */.#define O
6bac0 50 46 4c 41 47 5f 49 53 55 50 44 41 54 45 20 20  PFLAG_ISUPDATE  
6bad0 20 20 20 20 30 78 30 34 20 20 20 20 2f 2a 20 54      0x04    /* T
6bae0 68 69 73 20 4f 50 5f 49 6e 73 65 72 74 20 69 73  his OP_Insert is
6baf0 20 61 6e 20 73 71 6c 20 55 50 44 41 54 45 20 2a   an sql UPDATE *
6bb00 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47  /.#define OPFLAG
6bb10 5f 41 50 50 45 4e 44 20 20 20 20 20 20 20 20 30  _APPEND        0
6bb20 78 30 38 20 20 20 20 2f 2a 20 54 68 69 73 20 69  x08    /* This i
6bb30 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61  s likely to be a
6bb40 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 23 64 65 66  n append */.#def
6bb50 69 6e 65 20 4f 50 46 4c 41 47 5f 55 53 45 53 45  ine OPFLAG_USESE
6bb60 45 4b 52 45 53 55 4c 54 20 30 78 31 30 20 20 20  EKRESULT 0x10   
6bb70 20 2f 2a 20 54 72 79 20 74 6f 20 61 76 6f 69 64   /* Try to avoid
6bb80 20 61 20 73 65 65 6b 20 69 6e 20 42 74 72 65 65   a seek in Btree
6bb90 49 6e 73 65 72 74 28 29 20 2a 2f 0a 23 64 65 66  Insert() */.#def
6bba0 69 6e 65 20 4f 50 46 4c 41 47 5f 43 4c 45 41 52  ine OPFLAG_CLEAR
6bbb0 43 41 43 48 45 20 20 20 20 30 78 32 30 20 20 20  CACHE    0x20   
6bbc0 20 2f 2a 20 43 6c 65 61 72 20 70 73 65 75 64 6f   /* Clear pseudo
6bbd0 2d 74 61 62 6c 65 20 63 61 63 68 65 20 69 6e 20  -table cache in 
6bbe0 4f 50 5f 43 6f 6c 75 6d 6e 20 2a 2f 0a 0a 2f 2a  OP_Column */../*
6bbf0 0a 20 2a 20 45 61 63 68 20 74 72 69 67 67 65 72  . * Each trigger
6bc00 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20   present in the 
6bc10 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
6bc20 69 73 20 73 74 6f 72 65 64 20 61 73 20 61 6e 20  is stored as an 
6bc30 69 6e 73 74 61 6e 63 65 20 6f 66 0a 20 2a 20 73  instance of. * s
6bc40 74 72 75 63 74 20 54 72 69 67 67 65 72 2e 20 0a  truct Trigger. .
6bc50 20 2a 0a 20 2a 20 50 6f 69 6e 74 65 72 73 20 74   *. * Pointers t
6bc60 6f 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20 73  o instances of s
6bc70 74 72 75 63 74 20 54 72 69 67 67 65 72 20 61 72  truct Trigger ar
6bc80 65 20 73 74 6f 72 65 64 20 69 6e 20 74 77 6f 20  e stored in two 
6bc90 77 61 79 73 2e 0a 20 2a 20 31 2e 20 49 6e 20 74  ways.. * 1. In t
6bca0 68 65 20 22 74 72 69 67 48 61 73 68 22 20 68 61  he "trigHash" ha
6bcb0 73 68 20 74 61 62 6c 65 20 28 70 61 72 74 20 6f  sh table (part o
6bcc0 66 20 74 68 65 20 73 71 6c 69 74 65 33 2a 20 74  f the sqlite3* t
6bcd0 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 74  hat represents t
6bce0 68 65 20 0a 20 2a 20 20 20 20 64 61 74 61 62 61  he . *    databa
6bcf0 73 65 29 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73  se). This allows
6bd00 20 54 72 69 67 67 65 72 20 73 74 72 75 63 74 75   Trigger structu
6bd10 72 65 73 20 74 6f 20 62 65 20 72 65 74 72 69 65  res to be retrie
6bd20 76 65 64 20 62 79 20 6e 61 6d 65 2e 0a 20 2a 20  ved by name.. * 
6bd30 32 2e 20 41 6c 6c 20 74 72 69 67 67 65 72 73 20  2. All triggers 
6bd40 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
6bd50 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 66  a single table f
6bd60 6f 72 6d 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73  orm a linked lis
6bd70 74 2c 20 75 73 69 6e 67 20 74 68 65 0a 20 2a 20  t, using the. * 
6bd80 20 20 20 70 4e 65 78 74 20 6d 65 6d 62 65 72 20     pNext member 
6bd90 6f 66 20 73 74 72 75 63 74 20 54 72 69 67 67 65  of struct Trigge
6bda0 72 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  r. A pointer to 
6bdb0 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e  the first elemen
6bdc0 74 20 6f 66 20 74 68 65 0a 20 2a 20 20 20 20 6c  t of the. *    l
6bdd0 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 73 74  inked list is st
6bde0 6f 72 65 64 20 61 73 20 74 68 65 20 22 70 54 72  ored as the "pTr
6bdf0 69 67 67 65 72 22 20 6d 65 6d 62 65 72 20 6f 66  igger" member of
6be00 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 0a   the associated.
6be10 20 2a 20 20 20 20 73 74 72 75 63 74 20 54 61 62   *    struct Tab
6be20 6c 65 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 22 73  le.. *. * The "s
6be30 74 65 70 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72  tep_list" member
6be40 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66   points to the f
6be50 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20  irst element of 
6be60 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 20 2a  a linked list. *
6be70 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
6be80 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 73  SQL statements s
6be90 70 65 63 69 66 69 65 64 20 61 73 20 74 68 65 20  pecified as the 
6bea0 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e  trigger program.
6beb0 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54 72 69 67  . */.struct Trig
6bec0 67 65 72 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e  ger {.  char *zN
6bed0 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ame;            
6bee0 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74  /* The name of t
6bef0 68 65 20 74 72 69 67 67 65 72 20 20 20 20 20 20  he trigger      
6bf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6bf10 20 20 2a 2f 0a 20 20 63 68 61 72 20 2a 74 61 62    */.  char *tab
6bf20 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  le;            /
6bf30 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 72 20 76  * The table or v
6bf40 69 65 77 20 74 6f 20 77 68 69 63 68 20 74 68 65  iew to which the
6bf50 20 74 72 69 67 67 65 72 20 61 70 70 6c 69 65 73   trigger applies
6bf60 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20 20 20   */.  u8 op;    
6bf70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6bf80 20 4f 6e 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54   One of TK_DELET
6bf90 45 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b  E, TK_UPDATE, TK
6bfa0 5f 49 4e 53 45 52 54 20 20 20 20 20 20 20 20 20  _INSERT         
6bfb0 2a 2f 0a 20 20 75 38 20 74 72 5f 74 6d 3b 20 20  */.  u8 tr_tm;  
6bfc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6bfd0 4f 6e 65 20 6f 66 20 54 52 49 47 47 45 52 5f 42  One of TRIGGER_B
6bfe0 45 46 4f 52 45 2c 20 54 52 49 47 47 45 52 5f 41  EFORE, TRIGGER_A
6bff0 46 54 45 52 20 2a 2f 0a 20 20 45 78 70 72 20 2a  FTER */.  Expr *
6c000 70 57 68 65 6e 3b 20 20 20 20 20 20 20 20 20 20  pWhen;          
6c010 20 20 2f 2a 20 54 68 65 20 57 48 45 4e 20 63 6c    /* The WHEN cl
6c020 61 75 73 65 20 6f 66 20 74 68 65 20 65 78 70 72  ause of the expr
6c030 65 73 73 69 6f 6e 20 28 6d 61 79 20 62 65 20 4e  ession (may be N
6c040 55 4c 4c 29 20 2a 2f 0a 20 20 49 64 4c 69 73 74  ULL) */.  IdList
6c050 20 2a 70 43 6f 6c 75 6d 6e 73 3b 20 20 20 20 20   *pColumns;     
6c060 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
6c070 61 6e 20 55 50 44 41 54 45 20 4f 46 20 3c 63 6f  an UPDATE OF <co
6c080 6c 75 6d 6e 2d 6c 69 73 74 3e 20 74 72 69 67 67  lumn-list> trigg
6c090 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  er,.            
6c0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c0b0 20 74 68 65 20 3c 63 6f 6c 75 6d 6e 2d 6c 69 73   the <column-lis
6c0c0 74 3e 20 69 73 20 73 74 6f 72 65 64 20 68 65 72  t> is stored her
6c0d0 65 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70  e */.  Schema *p
6c0e0 53 63 68 65 6d 61 3b 20 20 20 20 20 20 20 20 2f  Schema;        /
6c0f0 2a 20 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e  * Schema contain
6c100 69 6e 67 20 74 68 65 20 74 72 69 67 67 65 72 20  ing the trigger 
6c110 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 54 61  */.  Schema *pTa
6c120 62 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20  bSchema;     /* 
6c130 53 63 68 65 6d 61 20 63 6f 6e 74 61 69 6e 69 6e  Schema containin
6c140 67 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20  g the table */. 
6c150 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 74   TriggerStep *st
6c160 65 70 5f 6c 69 73 74 3b 20 2f 2a 20 4c 69 6e 6b  ep_list; /* Link
6c170 20 6c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72   list of trigger
6c180 20 70 72 6f 67 72 61 6d 20 73 74 65 70 73 20 20   program steps  
6c190 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20             */.  
6c1a0 54 72 69 67 67 65 72 20 2a 70 4e 65 78 74 3b 20  Trigger *pNext; 
6c1b0 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
6c1c0 74 72 69 67 67 65 72 20 61 73 73 6f 63 69 61 74  trigger associat
6c1d0 65 64 20 77 69 74 68 20 74 68 65 20 74 61 62 6c  ed with the tabl
6c1e0 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  e */.};../*.** A
6c1f0 20 74 72 69 67 67 65 72 20 69 73 20 65 69 74 68   trigger is eith
6c200 65 72 20 61 20 42 45 46 4f 52 45 20 6f 72 20 61  er a BEFORE or a
6c210 6e 20 41 46 54 45 52 20 74 72 69 67 67 65 72 2e  n AFTER trigger.
6c220 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
6c230 63 6f 6e 73 74 61 6e 74 73 0a 2a 2a 20 64 65 74  constants.** det
6c240 65 72 6d 69 6e 65 20 77 68 69 63 68 2e 20 0a 2a  ermine which. .*
6c250 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72  *.** If there ar
6c260 65 20 6d 75 6c 74 69 70 6c 65 20 74 72 69 67 67  e multiple trigg
6c270 65 72 73 2c 20 79 6f 75 20 6d 69 67 68 74 20 6f  ers, you might o
6c280 66 20 73 6f 6d 65 20 42 45 46 4f 52 45 20 61 6e  f some BEFORE an
6c290 64 20 73 6f 6d 65 20 41 46 54 45 52 2e 0a 2a 2a  d some AFTER..**
6c2a0 20 49 6e 20 74 68 61 74 20 63 61 73 65 73 2c 20   In that cases, 
6c2b0 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 62 65  the constants be
6c2c0 6c 6f 77 20 63 61 6e 20 62 65 20 4f 52 65 64 20  low can be ORed 
6c2d0 74 6f 67 65 74 68 65 72 2e 0a 2a 2f 0a 23 64 65  together..*/.#de
6c2e0 66 69 6e 65 20 54 52 49 47 47 45 52 5f 42 45 46  fine TRIGGER_BEF
6c2f0 4f 52 45 20 20 31 0a 23 64 65 66 69 6e 65 20 54  ORE  1.#define T
6c300 52 49 47 47 45 52 5f 41 46 54 45 52 20 20 20 32  RIGGER_AFTER   2
6c310 0a 0a 2f 2a 0a 20 2a 20 41 6e 20 69 6e 73 74 61  ../*. * An insta
6c320 6e 63 65 20 6f 66 20 73 74 72 75 63 74 20 54 72  nce of struct Tr
6c330 69 67 67 65 72 53 74 65 70 20 69 73 20 75 73 65  iggerStep is use
6c340 64 20 74 6f 20 73 74 6f 72 65 20 61 20 73 69 6e  d to store a sin
6c350 67 6c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  gle SQL statemen
6c360 74 0a 20 2a 20 74 68 61 74 20 69 73 20 61 20 70  t. * that is a p
6c370 61 72 74 20 6f 66 20 61 20 74 72 69 67 67 65 72  art of a trigger
6c380 2d 70 72 6f 67 72 61 6d 2e 20 0a 20 2a 0a 20 2a  -program. . *. *
6c390 20 49 6e 73 74 61 6e 63 65 73 20 6f 66 20 73 74   Instances of st
6c3a0 72 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70  ruct TriggerStep
6c3b0 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61   are stored in a
6c3c0 20 73 69 6e 67 6c 79 20 6c 69 6e 6b 65 64 20 6c   singly linked l
6c3d0 69 73 74 20 28 6c 69 6e 6b 65 64 0a 20 2a 20 75  ist (linked. * u
6c3e0 73 69 6e 67 20 74 68 65 20 22 70 4e 65 78 74 22  sing the "pNext"
6c3f0 20 6d 65 6d 62 65 72 29 20 72 65 66 65 72 65 6e   member) referen
6c400 63 65 64 20 62 79 20 74 68 65 20 22 73 74 65 70  ced by the "step
6c410 5f 6c 69 73 74 22 20 6d 65 6d 62 65 72 20 6f 66  _list" member of
6c420 20 74 68 65 20 0a 20 2a 20 61 73 73 6f 63 69 61   the . * associa
6c430 74 65 64 20 73 74 72 75 63 74 20 54 72 69 67 67  ted struct Trigg
6c440 65 72 20 69 6e 73 74 61 6e 63 65 2e 20 54 68 65  er instance. The
6c450 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f   first element o
6c460 66 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73  f the linked lis
6c470 74 20 69 73 0a 20 2a 20 74 68 65 20 66 69 72 73  t is. * the firs
6c480 74 20 73 74 65 70 20 6f 66 20 74 68 65 20 74 72  t step of the tr
6c490 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 2e 0a 20  igger-program.. 
6c4a0 2a 20 0a 20 2a 20 54 68 65 20 22 6f 70 22 20 6d  * . * The "op" m
6c4b0 65 6d 62 65 72 20 69 6e 64 69 63 61 74 65 73 20  ember indicates 
6c4c0 77 68 65 74 68 65 72 20 74 68 69 73 20 69 73 20  whether this is 
6c4d0 61 20 22 44 45 4c 45 54 45 22 2c 20 22 49 4e 53  a "DELETE", "INS
6c4e0 45 52 54 22 2c 20 22 55 50 44 41 54 45 22 20 6f  ERT", "UPDATE" o
6c4f0 72 0a 20 2a 20 22 53 45 4c 45 43 54 22 20 73 74  r. * "SELECT" st
6c500 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 6d 65 61  atement. The mea
6c510 6e 69 6e 67 73 20 6f 66 20 74 68 65 20 6f 74 68  nings of the oth
6c520 65 72 20 6d 65 6d 62 65 72 73 20 69 73 20 64 65  er members is de
6c530 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20  termined by the 
6c540 0a 20 2a 20 76 61 6c 75 65 20 6f 66 20 22 6f 70  . * value of "op
6c550 22 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 2a  " as follows:. *
6c560 0a 20 2a 20 28 6f 70 20 3d 3d 20 54 4b 5f 49 4e  . * (op == TK_IN
6c570 53 45 52 54 29 0a 20 2a 20 6f 72 63 6f 6e 66 20  SERT). * orconf 
6c580 20 20 20 2d 3e 20 73 74 6f 72 65 73 20 74 68 65     -> stores the
6c590 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67   ON CONFLICT alg
6c5a0 6f 72 69 74 68 6d 0a 20 2a 20 70 53 65 6c 65 63  orithm. * pSelec
6c5b0 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73 20 69  t   -> If this i
6c5c0 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54 4f  s an INSERT INTO
6c5d0 20 2e 2e 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20   ... SELECT ... 
6c5e0 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e 0a  statement, then.
6c5f0 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   *              
6c600 74 68 69 73 20 73 74 6f 72 65 73 20 61 20 70 6f  this stores a po
6c610 69 6e 74 65 72 20 74 6f 20 74 68 65 20 53 45 4c  inter to the SEL
6c620 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 4f  ECT statement. O
6c630 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20  therwise NULL.. 
6c640 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41  * target    -> A
6c650 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74   token holding t
6c660 68 65 20 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f  he quoted name o
6c670 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 69  f the table to i
6c680 6e 73 65 72 74 20 69 6e 74 6f 2e 0a 20 2a 20 70  nsert into.. * p
6c690 45 78 70 72 4c 69 73 74 20 2d 3e 20 49 66 20 74  ExprList -> If t
6c6a0 68 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54  his is an INSERT
6c6b0 20 49 4e 54 4f 20 2e 2e 2e 20 56 41 4c 55 45 53   INTO ... VALUES
6c6c0 20 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 2c 20   ... statement, 
6c6d0 74 68 65 6e 0a 20 2a 20 20 20 20 20 20 20 20 20  then. *         
6c6e0 20 20 20 20 20 74 68 69 73 20 73 74 6f 72 65 73       this stores
6c6f0 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 69 6e   values to be in
6c700 73 65 72 74 65 64 2e 20 4f 74 68 65 72 77 69 73  serted. Otherwis
6c710 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 49 64 4c 69  e NULL.. * pIdLi
6c720 73 74 20 20 20 2d 3e 20 49 66 20 74 68 69 73 20  st   -> If this 
6c730 69 73 20 61 6e 20 49 4e 53 45 52 54 20 49 4e 54  is an INSERT INT
6c740 4f 20 2e 2e 2e 20 28 3c 63 6f 6c 75 6d 6e 2d 6e  O ... (<column-n
6c750 61 6d 65 73 3e 29 20 56 41 4c 55 45 53 20 2e 2e  ames>) VALUES ..
6c760 2e 20 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20  . . *           
6c770 20 20 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68     statement, th
6c780 65 6e 20 74 68 69 73 20 73 74 6f 72 65 73 20 74  en this stores t
6c790 68 65 20 63 6f 6c 75 6d 6e 2d 6e 61 6d 65 73 20  he column-names 
6c7a0 74 6f 20 62 65 0a 20 2a 20 20 20 20 20 20 20 20  to be. *        
6c7b0 20 20 20 20 20 20 69 6e 73 65 72 74 65 64 20 69        inserted i
6c7c0 6e 74 6f 2e 0a 20 2a 0a 20 2a 20 28 6f 70 20 3d  nto.. *. * (op =
6c7d0 3d 20 54 4b 5f 44 45 4c 45 54 45 29 0a 20 2a 20  = TK_DELETE). * 
6c7e0 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74  target    -> A t
6c7f0 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65  oken holding the
6c800 20 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20   quoted name of 
6c810 74 68 65 20 74 61 62 6c 65 20 74 6f 20 64 65 6c  the table to del
6c820 65 74 65 20 66 72 6f 6d 2e 0a 20 2a 20 70 57 68  ete from.. * pWh
6c830 65 72 65 20 20 20 20 2d 3e 20 54 68 65 20 57 48  ere    -> The WH
6c840 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68  ERE clause of th
6c850 65 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65  e DELETE stateme
6c860 6e 74 20 69 66 20 6f 6e 65 20 69 73 20 73 70 65  nt if one is spe
6c870 63 69 66 69 65 64 2e 0a 20 2a 20 20 20 20 20 20  cified.. *      
6c880 20 20 20 20 20 20 20 20 4f 74 68 65 72 77 69 73          Otherwis
6c890 65 20 4e 55 4c 4c 2e 0a 20 2a 20 0a 20 2a 20 28  e NULL.. * . * (
6c8a0 6f 70 20 3d 3d 20 54 4b 5f 55 50 44 41 54 45 29  op == TK_UPDATE)
6c8b0 0a 20 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e  . * target    ->
6c8c0 20 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67   A token holding
6c8d0 20 74 68 65 20 71 75 6f 74 65 64 20 6e 61 6d 65   the quoted name
6c8e0 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
6c8f0 20 75 70 64 61 74 65 20 72 6f 77 73 20 6f 66 2e   update rows of.
6c900 0a 20 2a 20 70 57 68 65 72 65 20 20 20 20 2d 3e  . * pWhere    ->
6c910 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
6c920 65 20 6f 66 20 74 68 65 20 55 50 44 41 54 45 20  e of the UPDATE 
6c930 73 74 61 74 65 6d 65 6e 74 20 69 66 20 6f 6e 65  statement if one
6c940 20 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a 20   is specified.. 
6c950 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f  *              O
6c960 74 68 65 72 77 69 73 65 20 4e 55 4c 4c 2e 0a 20  therwise NULL.. 
6c970 2a 20 70 45 78 70 72 4c 69 73 74 20 2d 3e 20 41  * pExprList -> A
6c980 20 6c 69 73 74 20 6f 66 20 74 68 65 20 63 6f 6c   list of the col
6c990 75 6d 6e 73 20 74 6f 20 75 70 64 61 74 65 20 61  umns to update a
6c9a0 6e 64 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  nd the expressio
6c9b0 6e 73 20 74 6f 20 75 70 64 61 74 65 0a 20 2a 20  ns to update. * 
6c9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65               the
6c9d0 6d 20 74 6f 2e 20 53 65 65 20 73 71 6c 69 74 65  m to. See sqlite
6c9e0 33 55 70 64 61 74 65 28 29 20 64 6f 63 75 6d 65  3Update() docume
6c9f0 6e 74 61 74 69 6f 6e 20 6f 66 20 22 70 43 68 61  ntation of "pCha
6ca00 6e 67 65 73 22 0a 20 2a 20 20 20 20 20 20 20 20  nges". *        
6ca10 20 20 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 0a        argument..
6ca20 20 2a 20 0a 20 2a 2f 0a 73 74 72 75 63 74 20 54   * . */.struct T
6ca30 72 69 67 67 65 72 53 74 65 70 20 7b 0a 20 20 75  riggerStep {.  u
6ca40 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20  8 op;           
6ca50 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b      /* One of TK
6ca60 5f 44 45 4c 45 54 45 2c 20 54 4b 5f 55 50 44 41  _DELETE, TK_UPDA
6ca70 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54  TE, TK_INSERT, T
6ca80 4b 5f 53 45 4c 45 43 54 20 2a 2f 0a 20 20 75 38  K_SELECT */.  u8
6ca90 20 6f 72 63 6f 6e 66 3b 20 20 20 20 20 20 20 20   orconf;        
6caa0 20 20 20 2f 2a 20 4f 45 5f 52 6f 6c 6c 62 61 63     /* OE_Rollbac
6cab0 6b 20 65 74 63 2e 20 2a 2f 0a 20 20 54 72 69 67  k etc. */.  Trig
6cac0 67 65 72 20 2a 70 54 72 69 67 3b 20 20 20 20 20  ger *pTrig;     
6cad0 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 20   /* The trigger 
6cae0 74 68 61 74 20 74 68 69 73 20 73 74 65 70 20 69  that this step i
6caf0 73 20 61 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20  s a part of */. 
6cb00 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
6cb10 3b 20 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20  ;     /* SELECT 
6cb20 73 74 61 74 6d 65 6e 74 20 6f 72 20 52 48 53 20  statment or RHS 
6cb30 6f 66 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 2e  of INSERT INTO .
6cb40 2e 20 53 45 4c 45 43 54 20 2e 2e 2e 20 2a 2f 0a  . SELECT ... */.
6cb50 20 20 54 6f 6b 65 6e 20 74 61 72 67 65 74 3b 20    Token target; 
6cb60 20 20 20 20 20 20 20 2f 2a 20 54 61 72 67 65 74         /* Target
6cb70 20 74 61 62 6c 65 20 66 6f 72 20 44 45 4c 45 54   table for DELET
6cb80 45 2c 20 55 50 44 41 54 45 2c 20 49 4e 53 45 52  E, UPDATE, INSER
6cb90 54 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  T */.  Expr *pWh
6cba0 65 72 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  ere;        /* T
6cbb0 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
6cbc0 66 6f 72 20 44 45 4c 45 54 45 20 6f 72 20 55 50  for DELETE or UP
6cbd0 44 41 54 45 20 73 74 65 70 73 20 2a 2f 0a 20 20  DATE steps */.  
6cbe0 45 78 70 72 4c 69 73 74 20 2a 70 45 78 70 72 4c  ExprList *pExprL
6cbf0 69 73 74 3b 20 2f 2a 20 53 45 54 20 63 6c 61 75  ist; /* SET clau
6cc00 73 65 20 66 6f 72 20 55 50 44 41 54 45 2e 20 20  se for UPDATE.  
6cc10 56 41 4c 55 45 53 20 63 6c 61 75 73 65 20 66 6f  VALUES clause fo
6cc20 72 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 49 64  r INSERT */.  Id
6cc30 4c 69 73 74 20 2a 70 49 64 4c 69 73 74 3b 20 20  List *pIdList;  
6cc40 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d     /* Column nam
6cc50 65 73 20 66 6f 72 20 49 4e 53 45 52 54 20 2a 2f  es for INSERT */
6cc60 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  .  TriggerStep *
6cc70 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20  pNext;  /* Next 
6cc80 69 6e 20 74 68 65 20 6c 69 6e 6b 2d 6c 69 73 74  in the link-list
6cc90 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65   */.  TriggerSte
6cca0 70 20 2a 70 4c 61 73 74 3b 20 20 2f 2a 20 4c 61  p *pLast;  /* La
6ccb0 73 74 20 65 6c 65 6d 65 6e 74 20 69 6e 20 6c 69  st element in li
6ccc0 6e 6b 2d 6c 69 73 74 2e 20 56 61 6c 69 64 20 66  nk-list. Valid f
6ccd0 6f 72 20 31 73 74 20 65 6c 65 6d 20 6f 6e 6c 79  or 1st elem only
6cce0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   */.};../*.** Th
6ccf0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
6cd00 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 69  cture contains i
6cd10 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20  nformation used 
6cd20 62 79 20 74 68 65 20 73 71 6c 69 74 65 46 69 78  by the sqliteFix
6cd30 2e 2e 2e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20  ....** routines 
6cd40 61 73 20 74 68 65 79 20 77 61 6c 6b 20 74 68 65  as they walk the
6cd50 20 70 61 72 73 65 20 74 72 65 65 20 74 6f 20 6d   parse tree to m
6cd60 61 6b 65 20 64 61 74 61 62 61 73 65 20 72 65 66  ake database ref
6cd70 65 72 65 6e 63 65 73 0a 2a 2a 20 65 78 70 6c 69  erences.** expli
6cd80 63 69 74 2e 20 20 0a 2a 2f 0a 74 79 70 65 64 65  cit.  .*/.typede
6cd90 66 20 73 74 72 75 63 74 20 44 62 46 69 78 65 72  f struct DbFixer
6cda0 20 44 62 46 69 78 65 72 3b 0a 73 74 72 75 63 74   DbFixer;.struct
6cdb0 20 44 62 46 69 78 65 72 20 7b 0a 20 20 50 61 72   DbFixer {.  Par
6cdc0 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20  se *pParse;     
6cdd0 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
6cde0 63 6f 6e 74 65 78 74 2e 20 20 45 72 72 6f 72 20  context.  Error 
6cdf0 6d 65 73 73 61 67 65 73 20 77 72 69 74 74 65 6e  messages written
6ce00 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74   here */.  const
6ce10 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 2f   char *zDb;    /
6ce20 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20  * Make sure all 
6ce30 6f 62 6a 65 63 74 73 20 61 72 65 20 63 6f 6e 74  objects are cont
6ce40 61 69 6e 65 64 20 69 6e 20 74 68 69 73 20 64 61  ained in this da
6ce50 74 61 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73  tabase */.  cons
6ce60 74 20 63 68 61 72 20 2a 7a 54 79 70 65 3b 20 20  t char *zType;  
6ce70 2f 2a 20 54 79 70 65 20 6f 66 20 74 68 65 20 63  /* Type of the c
6ce80 6f 6e 74 61 69 6e 65 72 20 2d 20 75 73 65 64 20  ontainer - used 
6ce90 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67  for error messag
6cea0 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 6f  es */.  const To
6ceb0 6b 65 6e 20 2a 70 4e 61 6d 65 3b 20 2f 2a 20 4e  ken *pName; /* N
6cec0 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61  ame of the conta
6ced0 69 6e 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20  iner - used for 
6cee0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a  error messages *
6cef0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 6f  /.};../*.** An o
6cf00 62 6a 65 63 74 65 64 20 75 73 65 64 20 74 6f 20  bjected used to 
6cf10 61 63 63 75 6d 75 6c 61 74 65 20 74 68 65 20 74  accumulate the t
6cf20 65 78 74 20 6f 66 20 61 20 73 74 72 69 6e 67 20  ext of a string 
6cf30 77 68 65 72 65 20 77 65 0a 2a 2a 20 64 6f 20 6e  where we.** do n
6cf40 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 20 6b  ot necessarily k
6cf50 6e 6f 77 20 68 6f 77 20 62 69 67 20 74 68 65 20  now how big the 
6cf60 73 74 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 69  string will be i
6cf70 6e 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74  n the end..*/.st
6cf80 72 75 63 74 20 53 74 72 41 63 63 75 6d 20 7b 0a  ruct StrAccum {.
6cf90 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
6cfa0 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e         /* Option
6cfb0 61 6c 20 64 61 74 61 62 61 73 65 20 66 6f 72 20  al database for 
6cfc0 6c 6f 6f 6b 61 73 69 64 65 2e 20 20 43 61 6e 20  lookaside.  Can 
6cfd0 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68 61  be NULL */.  cha
6cfe0 72 20 2a 7a 42 61 73 65 3b 20 20 20 20 20 20 20  r *zBase;       
6cff0 20 20 2f 2a 20 41 20 62 61 73 65 20 61 6c 6c 6f    /* A base allo
6d000 63 61 74 69 6f 6e 2e 20 20 4e 6f 74 20 66 72 6f  cation.  Not fro
6d010 6d 20 6d 61 6c 6c 6f 63 2e 20 2a 2f 0a 20 20 63  m malloc. */.  c
6d020 68 61 72 20 2a 7a 54 65 78 74 3b 20 20 20 20 20  har *zText;     
6d030 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72 69 6e      /* The strin
6d040 67 20 63 6f 6c 6c 65 63 74 65 64 20 73 6f 20 66  g collected so f
6d050 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 43 68  ar */.  int  nCh
6d060 61 72 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ar;          /* 
6d070 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 74  Length of the st
6d080 72 69 6e 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20  ring so far */. 
6d090 20 69 6e 74 20 20 6e 41 6c 6c 6f 63 3b 20 20 20   int  nAlloc;   
6d0a0 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20        /* Amount 
6d0b0 6f 66 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74  of space allocat
6d0c0 65 64 20 69 6e 20 7a 54 65 78 74 20 2a 2f 0a 20  ed in zText */. 
6d0d0 20 69 6e 74 20 20 6d 78 41 6c 6c 6f 63 3b 20 20   int  mxAlloc;  
6d0e0 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d        /* Maximum
6d0f0 20 61 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20   allowed string 
6d100 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 75 38 20 20  length */.  u8  
6d110 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 20 20   mallocFailed;  
6d120 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75 65   /* Becomes true
6d130 20 69 66 20 61 6e 79 20 6d 65 6d 6f 72 79 20 61   if any memory a
6d140 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 20  llocation fails 
6d150 2a 2f 0a 20 20 75 38 20 20 20 75 73 65 4d 61 6c  */.  u8   useMal
6d160 6c 6f 63 3b 20 20 20 20 20 20 2f 2a 20 54 72 75  loc;      /* Tru
6d170 65 20 69 66 20 7a 54 65 78 74 20 69 73 20 65 6e  e if zText is en
6d180 6c 61 72 67 65 61 62 6c 65 20 75 73 69 6e 67 20  largeable using 
6d190 72 65 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 38 20  realloc */.  u8 
6d1a0 20 20 74 6f 6f 42 69 67 3b 20 20 20 20 20 20 20    tooBig;       
6d1b0 20 20 2f 2a 20 42 65 63 6f 6d 65 73 20 74 72 75    /* Becomes tru
6d1c0 65 20 69 66 20 73 74 72 69 6e 67 20 73 69 7a 65  e if string size
6d1d0 20 65 78 63 65 65 64 73 20 6c 69 6d 69 74 73 20   exceeds limits 
6d1e0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 70  */.};../*.** A p
6d1f0 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73  ointer to this s
6d200 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64  tructure is used
6d210 20 74 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20   to communicate 
6d220 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 66  information.** f
6d230 72 6f 6d 20 73 71 6c 69 74 65 33 49 6e 69 74 20  rom sqlite3Init 
6d240 61 6e 64 20 4f 50 5f 50 61 72 73 65 53 63 68 65  and OP_ParseSche
6d250 6d 61 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69  ma into the sqli
6d260 74 65 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 2e  te3InitCallback.
6d270 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
6d280 63 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  ct {.  sqlite3 *
6d290 64 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  db;        /* Th
6d2a0 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67  e database being
6d2b0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
6d2c0 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
6d2d0 20 20 20 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d        /* 0 for m
6d2e0 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20 20 31  ain database.  1
6d2f0 20 66 6f 72 20 54 45 4d 50 2c 20 32 2e 2e 20 66   for TEMP, 2.. f
6d300 6f 72 20 41 54 54 41 43 48 65 64 20 2a 2f 0a 20  or ATTACHed */. 
6d310 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
6d320 3b 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65  ;    /* Error me
6d330 73 73 61 67 65 20 73 74 6f 72 65 64 20 68 65 72  ssage stored her
6d340 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  e */.  int rc;  
6d350 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
6d360 73 75 6c 74 20 63 6f 64 65 20 73 74 6f 72 65 64  sult code stored
6d370 20 68 65 72 65 20 2a 2f 0a 7d 20 49 6e 69 74 44   here */.} InitD
6d380 61 74 61 3b 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75  ata;../*.** Stru
6d390 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67  cture containing
6d3a0 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72   global configur
6d3b0 61 74 69 6f 6e 20 64 61 74 61 20 66 6f 72 20 74  ation data for t
6d3c0 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72  he SQLite librar
6d3d0 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 74  y..**.** This st
6d3e0 72 75 63 74 75 72 65 20 61 6c 73 6f 20 63 6f 6e  ructure also con
6d3f0 74 61 69 6e 73 20 73 6f 6d 65 20 73 74 61 74 65  tains some state
6d400 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
6d410 0a 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43  .struct Sqlite3C
6d420 6f 6e 66 69 67 20 7b 0a 20 20 69 6e 74 20 62 4d  onfig {.  int bM
6d430 65 6d 73 74 61 74 3b 20 20 20 20 20 20 20 20 20  emstat;         
6d440 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
6d450 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 6d 65  rue to enable me
6d460 6d 6f 72 79 20 73 74 61 74 75 73 20 2a 2f 0a 20  mory status */. 
6d470 20 69 6e 74 20 62 43 6f 72 65 4d 75 74 65 78 3b   int bCoreMutex;
6d480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d490 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e     /* True to en
6d4a0 61 62 6c 65 20 63 6f 72 65 20 6d 75 74 65 78 69  able core mutexi
6d4b0 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 46 75 6c  ng */.  int bFul
6d4c0 6c 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20  lMutex;         
6d4d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
6d4e0 65 20 74 6f 20 65 6e 61 62 6c 65 20 66 75 6c 6c  e to enable full
6d4f0 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20 69   mutexing */.  i
6d500 6e 74 20 6d 78 53 74 72 6c 65 6e 3b 20 20 20 20  nt mxStrlen;    
6d510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d520 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 74 72 69   /* Maximum stri
6d530 6e 67 20 6c 65 6e 67 74 68 20 2a 2f 0a 20 20 69  ng length */.  i
6d540 6e 74 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 3b 20  nt szLookaside; 
6d550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d560 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 6f 6b   /* Default look
6d570 61 73 69 64 65 20 62 75 66 66 65 72 20 73 69 7a  aside buffer siz
6d580 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f 6f 6b  e */.  int nLook
6d590 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 20  aside;          
6d5a0 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61           /* Defa
6d5b0 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75  ult lookaside bu
6d5c0 66 66 65 72 20 63 6f 75 6e 74 20 2a 2f 0a 20 20  ffer count */.  
6d5d0 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
6d5e0 6f 64 73 20 6d 3b 20 20 20 20 20 20 20 20 20 20  ods m;          
6d5f0 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d    /* Low-level m
6d600 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
6d610 20 69 6e 74 65 72 66 61 63 65 20 2a 2f 0a 20 20   interface */.  
6d620 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
6d630 74 68 6f 64 73 20 6d 75 74 65 78 3b 20 20 20 20  thods mutex;    
6d640 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 6d    /* Low-level m
6d650 75 74 65 78 20 69 6e 74 65 72 66 61 63 65 20 2a  utex interface *
6d660 2f 0a 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63  /.  sqlite3_pcac
6d670 68 65 5f 6d 65 74 68 6f 64 73 20 70 63 61 63 68  he_methods pcach
6d680 65 3b 20 20 20 20 2f 2a 20 4c 6f 77 2d 6c 65 76  e;    /* Low-lev
6d690 65 6c 20 70 61 67 65 2d 63 61 63 68 65 20 69 6e  el page-cache in
6d6a0 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 76 6f 69  terface */.  voi
6d6b0 64 20 2a 70 48 65 61 70 3b 20 20 20 20 20 20 20  d *pHeap;       
6d6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6d6d0 2a 20 48 65 61 70 20 73 74 6f 72 61 67 65 20 73  * Heap storage s
6d6e0 70 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48  pace */.  int nH
6d6f0 65 61 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  eap;            
6d700 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
6d710 69 7a 65 20 6f 66 20 70 48 65 61 70 5b 5d 20 2a  ize of pHeap[] *
6d720 2f 0a 20 20 69 6e 74 20 6d 6e 52 65 71 2c 20 6d  /.  int mnReq, m
6d730 78 52 65 71 3b 20 20 20 20 20 20 20 20 20 20 20  xReq;           
6d740 20 20 20 20 20 20 2f 2a 20 4d 69 6e 20 61 6e 64        /* Min and
6d750 20 6d 61 78 20 68 65 61 70 20 72 65 71 75 65 73   max heap reques
6d760 74 73 20 73 69 7a 65 73 20 2a 2f 0a 20 20 76 6f  ts sizes */.  vo
6d770 69 64 20 2a 70 53 63 72 61 74 63 68 3b 20 20 20  id *pScratch;   
6d780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d790 2f 2a 20 53 63 72 61 74 63 68 20 6d 65 6d 6f 72  /* Scratch memor
6d7a0 79 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53 63 72  y */.  int szScr
6d7b0 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20 20  atch;           
6d7c0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
6d7d0 20 6f 66 20 65 61 63 68 20 73 63 72 61 74 63 68   of each scratch
6d7e0 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74   buffer */.  int
6d7f0 20 6e 53 63 72 61 74 63 68 3b 20 20 20 20 20 20   nScratch;      
6d800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6d810 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 63 72 61  * Number of scra
6d820 74 63 68 20 62 75 66 66 65 72 73 20 2a 2f 0a 20  tch buffers */. 
6d830 20 76 6f 69 64 20 2a 70 50 61 67 65 3b 20 20 20   void *pPage;   
6d840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d850 20 20 20 2f 2a 20 50 61 67 65 20 63 61 63 68 65     /* Page cache
6d860 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 69 6e 74   memory */.  int
6d870 20 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20   szPage;        
6d880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6d890 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 70  * Size of each p
6d8a0 61 67 65 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a  age in pPage[] *
6d8b0 2f 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20  /.  int nPage;  
6d8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d8d0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
6d8e0 6f 66 20 70 61 67 65 73 20 69 6e 20 70 50 61 67  of pages in pPag
6d8f0 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 50  e[] */.  int mxP
6d900 61 72 73 65 72 53 74 61 63 6b 3b 20 20 20 20 20  arserStack;     
6d910 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 61             /* ma
6d920 78 69 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74  ximum depth of t
6d930 68 65 20 70 61 72 73 65 72 20 73 74 61 63 6b 20  he parser stack 
6d940 2a 2f 0a 20 20 69 6e 74 20 73 68 61 72 65 64 43  */.  int sharedC
6d950 61 63 68 65 45 6e 61 62 6c 65 64 3b 20 20 20 20  acheEnabled;    
6d960 20 20 20 20 20 20 20 2f 2a 20 74 72 75 65 20 69         /* true i
6d970 66 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6d  f shared-cache m
6d980 6f 64 65 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20  ode enabled */. 
6d990 20 2f 2a 20 54 68 65 20 61 62 6f 76 65 20 6d 69   /* The above mi
6d9a0 67 68 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a  ght be initializ
6d9b0 65 64 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 2e 20  ed to non-zero. 
6d9c0 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e   The following n
6d9d0 65 65 64 20 74 6f 20 61 6c 77 61 79 73 0a 20 20  eed to always.  
6d9e0 2a 2a 20 69 6e 69 74 69 61 6c 6c 79 20 62 65 20  ** initially be 
6d9f0 7a 65 72 6f 2c 20 68 6f 77 65 76 65 72 2e 20 2a  zero, however. *
6da00 2f 0a 20 20 69 6e 74 20 69 73 49 6e 69 74 3b 20  /.  int isInit; 
6da10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6da20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66        /* True af
6da30 74 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ter initializati
6da40 6f 6e 20 68 61 73 20 66 69 6e 69 73 68 65 64 20  on has finished 
6da50 2a 2f 0a 20 20 69 6e 74 20 69 6e 50 72 6f 67 72  */.  int inProgr
6da60 65 73 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ess;            
6da70 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 77         /* True w
6da80 68 69 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61 74  hile initializat
6da90 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 20  ion in progress 
6daa0 2a 2f 0a 20 20 69 6e 74 20 69 73 4d 75 74 65 78  */.  int isMutex
6dab0 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20  Init;           
6dac0 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61         /* True a
6dad0 66 74 65 72 20 6d 75 74 65 78 65 73 20 61 72 65  fter mutexes are
6dae0 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
6daf0 20 20 69 6e 74 20 69 73 4d 61 6c 6c 6f 63 49 6e    int isMallocIn
6db00 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  it;             
6db10 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65      /* True afte
6db20 72 20 6d 61 6c 6c 6f 63 20 69 73 20 69 6e 69 74  r malloc is init
6db30 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74  ialized */.  int
6db40 20 69 73 50 43 61 63 68 65 49 6e 69 74 3b 20 20   isPCacheInit;  
6db50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6db60 2a 20 54 72 75 65 20 61 66 74 65 72 20 6d 61 6c  * True after mal
6db70 6c 6f 63 20 69 73 20 69 6e 69 74 69 61 6c 69 7a  loc is initializ
6db80 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ed */.  sqlite3_
6db90 6d 75 74 65 78 20 2a 70 49 6e 69 74 4d 75 74 65  mutex *pInitMute
6dba0 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 75 74  x;        /* Mut
6dbb0 65 78 20 75 73 65 64 20 62 79 20 73 71 6c 69 74  ex used by sqlit
6dbc0 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
6dbd0 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 49 6e 69  */.  int nRefIni
6dbe0 74 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20  tMutex;         
6dbf0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6dc00 20 6f 66 20 75 73 65 72 73 20 6f 66 20 70 49 6e   of users of pIn
6dc10 69 74 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 2f  itMutex */.};../
6dc20 2a 0a 2a 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69  *.** Context poi
6dc30 6e 74 65 72 20 70 61 73 73 65 64 20 64 6f 77 6e  nter passed down
6dc40 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 72 65   through the tre
6dc50 65 2d 77 61 6c 6b 2e 0a 2a 2f 0a 73 74 72 75 63  e-walk..*/.struc
6dc60 74 20 57 61 6c 6b 65 72 20 7b 0a 20 20 69 6e 74  t Walker {.  int
6dc70 20 28 2a 78 45 78 70 72 43 61 6c 6c 62 61 63 6b   (*xExprCallback
6dc80 29 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 2a  )(Walker*, Expr*
6dc90 29 3b 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61  );     /* Callba
6dca0 63 6b 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f  ck for expressio
6dcb0 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53  ns */.  int (*xS
6dcc0 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 29 28 57  electCallback)(W
6dcd0 61 6c 6b 65 72 2a 2c 53 65 6c 65 63 74 2a 29 3b  alker*,Select*);
6dce0 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f    /* Callback fo
6dcf0 72 20 53 45 4c 45 43 54 73 20 2a 2f 0a 20 20 50  r SELECTs */.  P
6dd00 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20  arse *pParse;   
6dd10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dd20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
6dd30 65 72 20 63 6f 6e 74 65 78 74 2e 20 20 2a 2f 0a  er context.  */.
6dd40 20 20 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20    union {       
6dd50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6dd60 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
6dd70 78 74 72 61 20 64 61 74 61 20 66 6f 72 20 63 61  xtra data for ca
6dd80 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 20 20 4e 61  llback */.    Na
6dd90 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 20  meContext *pNC; 
6dda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ddb0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 69           /* Nami
6ddc0 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
6ddd0 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
6dde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6ddf0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
6de00 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 2a 2f  Integer value */
6de10 0a 20 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46  .  } u;.};../* F
6de20 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
6de30 6f 6e 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ons */.SQLITE_PR
6de40 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6de50 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72  3WalkExpr(Walker
6de60 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54  *, Expr*);.SQLIT
6de70 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6de80 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73  lite3WalkExprLis
6de90 74 28 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 4c  t(Walker*, ExprL
6dea0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ist*);.SQLITE_PR
6deb0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6dec0 33 57 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b  3WalkSelect(Walk
6ded0 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53  er*, Select*);.S
6dee0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6def0 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c  t sqlite3WalkSel
6df00 65 63 74 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c  ectExpr(Walker*,
6df10 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54   Select*);.SQLIT
6df20 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6df30 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46  lite3WalkSelectF
6df40 72 6f 6d 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c  rom(Walker*, Sel
6df50 65 63 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65  ect*);../*.** Re
6df60 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74  turn code from t
6df70 68 65 20 70 61 72 73 65 2d 74 72 65 65 20 77 61  he parse-tree wa
6df80 6c 6b 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73  lking primitives
6df90 20 61 6e 64 20 74 68 65 69 72 0a 2a 2a 20 63 61   and their.** ca
6dfa0 6c 6c 62 61 63 6b 73 2e 0a 2a 2f 0a 23 64 65 66  llbacks..*/.#def
6dfb0 69 6e 65 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65  ine WRC_Continue
6dfc0 20 20 20 20 30 20 20 20 2f 2a 20 43 6f 6e 74 69      0   /* Conti
6dfd0 6e 75 65 20 64 6f 77 6e 20 69 6e 74 6f 20 63 68  nue down into ch
6dfe0 69 6c 64 72 65 6e 20 2a 2f 0a 23 64 65 66 69 6e  ildren */.#defin
6dff0 65 20 57 52 43 5f 50 72 75 6e 65 20 20 20 20 20  e WRC_Prune     
6e000 20 20 31 20 20 20 2f 2a 20 4f 6d 69 74 20 63 68    1   /* Omit ch
6e010 69 6c 64 72 65 6e 20 62 75 74 20 63 6f 6e 74 69  ildren but conti
6e020 6e 75 65 20 77 61 6c 6b 69 6e 67 20 73 69 62 6c  nue walking sibl
6e030 69 6e 67 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ings */.#define 
6e040 57 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20  WRC_Abort       
6e050 32 20 20 20 2f 2a 20 41 62 61 6e 64 6f 6e 20 74  2   /* Abandon t
6e060 68 65 20 74 72 65 65 20 77 61 6c 6b 20 2a 2f 0a  he tree walk */.
6e070 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e 67 20  ./*.** Assuming 
6e080 7a 49 6e 20 70 6f 69 6e 74 73 20 74 6f 20 74 68  zIn points to th
6e090 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20  e first byte of 
6e0a0 61 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65  a UTF-8 characte
6e0b0 72 2c 0a 2a 2a 20 61 64 76 61 6e 63 65 20 7a 49  r,.** advance zI
6e0c0 6e 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  n to point to th
6e0d0 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20  e first byte of 
6e0e0 74 68 65 20 6e 65 78 74 20 55 54 46 2d 38 20 63  the next UTF-8 c
6e0f0 68 61 72 61 63 74 65 72 2e 0a 2a 2f 0a 23 64 65  haracter..*/.#de
6e100 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4b 49 50  fine SQLITE_SKIP
6e110 5f 55 54 46 38 28 7a 49 6e 29 20 7b 20 20 20 20  _UTF8(zIn) {    
6e120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e130 20 20 20 20 5c 0a 20 20 69 66 28 20 28 2a 28 7a      \.  if( (*(z
6e140 49 6e 2b 2b 29 29 3e 3d 30 78 63 30 20 29 7b 20  In++))>=0xc0 ){ 
6e150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e160 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
6e170 20 20 20 77 68 69 6c 65 28 20 28 2a 7a 49 6e 20     while( (*zIn 
6e180 26 20 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b  & 0xc0)==0x80 ){
6e190 20 7a 49 6e 2b 2b 3b 20 7d 20 20 20 20 20 20 20   zIn++; }       
6e1a0 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20        \.  }     
6e1b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6e1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
6e1e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51  .}../*.** The SQ
6e1f0 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
6e200 54 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 65  T macro can be e
6e210 69 74 68 65 72 20 61 20 63 6f 6e 73 74 61 6e 74  ither a constant
6e220 20 28 66 6f 72 20 70 72 6f 64 75 63 74 69 6f 6e   (for production
6e230 0a 2a 2a 20 62 75 69 6c 64 73 29 20 6f 72 20 61  .** builds) or a
6e240 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 28   function call (
6e250 66 6f 72 20 64 65 62 75 67 67 69 6e 67 29 2e 20  for debugging). 
6e260 20 49 66 20 69 74 20 69 73 20 61 20 66 75 6e 63   If it is a func
6e270 74 69 6f 6e 20 63 61 6c 6c 2c 0a 2a 2a 20 69 74  tion call,.** it
6e280 20 61 6c 6c 6f 77 73 20 74 68 65 20 6f 70 65 72   allows the oper
6e290 61 74 6f 72 20 74 6f 20 73 65 74 20 61 20 62 72  ator to set a br
6e2a0 65 61 6b 70 6f 69 6e 74 20 61 74 20 74 68 65 20  eakpoint at the 
6e2b0 73 70 6f 74 20 77 68 65 72 65 20 64 61 74 61 62  spot where datab
6e2c0 61 73 65 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f  ase.** corruptio
6e2d0 6e 20 69 73 20 66 69 72 73 74 20 64 65 74 65 63  n is first detec
6e2e0 74 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ted..*/.#ifdef S
6e2f0 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49  QLITE_DEBUG.SQLI
6e300 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
6e310 20 73 71 6c 69 74 65 33 43 6f 72 72 75 70 74 28   sqlite3Corrupt(
6e320 76 6f 69 64 29 3b 0a 23 20 64 65 66 69 6e 65 20  void);.# define 
6e330 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
6e340 4b 50 54 20 73 71 6c 69 74 65 33 43 6f 72 72 75  KPT sqlite3Corru
6e350 70 74 28 29 0a 23 65 6c 73 65 0a 23 20 64 65 66  pt().#else.# def
6e360 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  ine SQLITE_CORRU
6e370 50 54 5f 42 4b 50 54 20 53 51 4c 49 54 45 5f 43  PT_BKPT SQLITE_C
6e380 4f 52 52 55 50 54 0a 23 65 6e 64 69 66 0a 0a 2f  ORRUPT.#endif../
6e390 2a 0a 2a 2a 20 54 68 65 20 63 74 79 70 65 2e 68  *.** The ctype.h
6e3a0 20 68 65 61 64 65 72 20 69 73 20 6e 65 65 64 65   header is neede
6e3b0 64 20 66 6f 72 20 6e 6f 6e 2d 41 53 43 49 49 20  d for non-ASCII 
6e3c0 73 79 73 74 65 6d 73 2e 20 20 49 74 20 69 73 20  systems.  It is 
6e3d0 61 6c 73 6f 0a 2a 2a 20 6e 65 65 64 65 64 20 62  also.** needed b
6e3e0 79 20 46 54 53 33 20 77 68 65 6e 20 46 54 53 33  y FTS3 when FTS3
6e3f0 20 69 73 20 69 6e 63 6c 75 64 65 64 20 69 6e 20   is included in 
6e400 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e  the amalgamation
6e410 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  ..*/.#if !define
6e420 64 28 53 51 4c 49 54 45 5f 41 53 43 49 49 29 20  d(SQLITE_ASCII) 
6e430 7c 7c 20 5c 0a 20 20 20 20 28 64 65 66 69 6e 65  || \.    (define
6e440 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
6e450 46 54 53 33 29 20 26 26 20 64 65 66 69 6e 65 64  FTS3) && defined
6e460 28 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41  (SQLITE_AMALGAMA
6e470 54 49 4f 4e 29 29 0a 23 20 69 6e 63 6c 75 64 65  TION)).# include
6e480 20 3c 63 74 79 70 65 2e 68 3e 0a 23 65 6e 64 69   <ctype.h>.#endi
6e490 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  f../*.** The fol
6e4a0 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 69  lowing macros mi
6e4b0 6d 69 63 20 74 68 65 20 73 74 61 6e 64 61 72 64  mic the standard
6e4c0 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74 69 6f   library functio
6e4d0 6e 73 20 74 6f 75 70 70 65 72 28 29 2c 0a 2a 2a  ns toupper(),.**
6e4e0 20 69 73 73 70 61 63 65 28 29 2c 20 69 73 61 6c   isspace(), isal
6e4f0 6e 75 6d 28 29 2c 20 69 73 64 69 67 69 74 28 29  num(), isdigit()
6e500 20 61 6e 64 20 69 73 78 64 69 67 69 74 28 29 2c   and isxdigit(),
6e510 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54   respectively. T
6e520 68 65 0a 2a 2a 20 73 71 6c 69 74 65 20 76 65 72  he.** sqlite ver
6e530 73 69 6f 6e 73 20 6f 6e 6c 79 20 77 6f 72 6b 20  sions only work 
6e540 66 6f 72 20 41 53 43 49 49 20 63 68 61 72 61 63  for ASCII charac
6e550 74 65 72 73 2c 20 72 65 67 61 72 64 6c 65 73 73  ters, regardless
6e560 20 6f 66 20 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 23   of locale..*/.#
6e570 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43  ifdef SQLITE_ASC
6e580 49 49 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  II.# define sqli
6e590 74 65 33 54 6f 75 70 70 65 72 28 78 29 20 20 28  te3Toupper(x)  (
6e5a0 28 78 29 26 7e 28 73 71 6c 69 74 65 33 43 74 79  (x)&~(sqlite3Cty
6e5b0 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64 20  peMap[(unsigned 
6e5c0 63 68 61 72 29 28 78 29 5d 26 30 78 32 30 29 29  char)(x)]&0x20))
6e5d0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6e5e0 33 49 73 73 70 61 63 65 28 78 29 20 20 20 28 73  3Isspace(x)   (s
6e5f0 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28  qlite3CtypeMap[(
6e600 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78  unsigned char)(x
6e610 29 5d 26 30 78 30 31 29 0a 23 20 64 65 66 69 6e  )]&0x01).# defin
6e620 65 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d  e sqlite3Isalnum
6e630 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 43 74  (x)   (sqlite3Ct
6e640 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64  ypeMap[(unsigned
6e650 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 36 29   char)(x)]&0x06)
6e660 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6e670 33 49 73 61 6c 70 68 61 28 78 29 20 20 20 28 73  3Isalpha(x)   (s
6e680 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28  qlite3CtypeMap[(
6e690 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78  unsigned char)(x
6e6a0 29 5d 26 30 78 30 32 29 0a 23 20 64 65 66 69 6e  )]&0x02).# defin
6e6b0 65 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74  e sqlite3Isdigit
6e6c0 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 43 74  (x)   (sqlite3Ct
6e6d0 79 70 65 4d 61 70 5b 28 75 6e 73 69 67 6e 65 64  ypeMap[(unsigned
6e6e0 20 63 68 61 72 29 28 78 29 5d 26 30 78 30 34 29   char)(x)]&0x04)
6e6f0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6e700 33 49 73 78 64 69 67 69 74 28 78 29 20 20 28 73  3Isxdigit(x)  (s
6e710 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b 28  qlite3CtypeMap[(
6e720 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78  unsigned char)(x
6e730 29 5d 26 30 78 30 38 29 0a 23 20 64 65 66 69 6e  )]&0x08).# defin
6e740 65 20 73 71 6c 69 74 65 33 54 6f 6c 6f 77 65 72  e sqlite3Tolower
6e750 28 78 29 20 20 20 28 73 71 6c 69 74 65 33 55 70  (x)   (sqlite3Up
6e760 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69  perToLower[(unsi
6e770 67 6e 65 64 20 63 68 61 72 29 28 78 29 5d 29 0a  gned char)(x)]).
6e780 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73  #else.# define s
6e790 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28 78 29  qlite3Toupper(x)
6e7a0 20 20 20 74 6f 75 70 70 65 72 28 28 75 6e 73 69     toupper((unsi
6e7b0 67 6e 65 64 20 63 68 61 72 29 28 78 29 29 0a 23  gned char)(x)).#
6e7c0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 49   define sqlite3I
6e7d0 73 73 70 61 63 65 28 78 29 20 20 20 69 73 73 70  sspace(x)   issp
6e7e0 61 63 65 28 28 75 6e 73 69 67 6e 65 64 20 63 68  ace((unsigned ch
6e7f0 61 72 29 28 78 29 29 0a 23 20 64 65 66 69 6e 65  ar)(x)).# define
6e800 20 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28   sqlite3Isalnum(
6e810 78 29 20 20 20 69 73 61 6c 6e 75 6d 28 28 75 6e  x)   isalnum((un
6e820 73 69 67 6e 65 64 20 63 68 61 72 29 28 78 29 29  signed char)(x))
6e830 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
6e840 33 49 73 61 6c 70 68 61 28 78 29 20 20 20 69 73  3Isalpha(x)   is
6e850 61 6c 70 68 61 28 28 75 6e 73 69 67 6e 65 64 20  alpha((unsigned 
6e860 63 68 61 72 29 28 78 29 29 0a 23 20 64 65 66 69  char)(x)).# defi
6e870 6e 65 20 73 71 6c 69 74 65 33 49 73 64 69 67 69  ne sqlite3Isdigi
6e880 74 28 78 29 20 20 20 69 73 64 69 67 69 74 28 28  t(x)   isdigit((
6e890 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 78  unsigned char)(x
6e8a0 29 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  )).# define sqli
6e8b0 74 65 33 49 73 78 64 69 67 69 74 28 78 29 20 20  te3Isxdigit(x)  
6e8c0 69 73 78 64 69 67 69 74 28 28 75 6e 73 69 67 6e  isxdigit((unsign
6e8d0 65 64 20 63 68 61 72 29 28 78 29 29 0a 23 20 64  ed char)(x)).# d
6e8e0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 6f 6c  efine sqlite3Tol
6e8f0 6f 77 65 72 28 78 29 20 20 20 74 6f 6c 6f 77 65  ower(x)   tolowe
6e900 72 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  r((unsigned char
6e910 29 28 78 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  )(x)).#endif../*
6e920 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e  .** Internal fun
6e930 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65 73  ction prototypes
6e940 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
6e950 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74  TE int sqlite3St
6e960 72 49 43 6d 70 28 63 6f 6e 73 74 20 63 68 61 72  rICmp(const char
6e970 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
6e980 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
6e990 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e  E int sqlite3IsN
6e9a0 75 6d 62 65 72 28 63 6f 6e 73 74 20 63 68 61 72  umber(const char
6e9b0 2a 2c 20 69 6e 74 2a 2c 20 75 38 29 3b 0a 53 51  *, int*, u8);.SQ
6e9c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6e9d0 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
6e9e0 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23  (const char*);.#
6e9f0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74  define sqlite3St
6ea00 72 4e 49 43 6d 70 20 73 71 6c 69 74 65 33 5f 73  rNICmp sqlite3_s
6ea10 74 72 6e 69 63 6d 70 0a 0a 53 51 4c 49 54 45 5f  trnicmp..SQLITE_
6ea20 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6ea30 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f  te3MallocInit(vo
6ea40 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  id);.SQLITE_PRIV
6ea50 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6ea60 4d 61 6c 6c 6f 63 45 6e 64 28 76 6f 69 64 29 3b  MallocEnd(void);
6ea70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6ea80 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c  void *sqlite3Mal
6ea90 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45  loc(int);.SQLITE
6eaa0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
6eab0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
6eac0 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  (int);.SQLITE_PR
6ead0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
6eae0 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
6eaf0 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a  sqlite3*, int);.
6eb00 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6eb10 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61  oid *sqlite3DbMa
6eb20 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 65 33 2a  llocRaw(sqlite3*
6eb30 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
6eb40 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
6eb50 69 74 65 33 44 62 53 74 72 44 75 70 28 73 71 6c  ite3DbStrDup(sql
6eb60 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
6eb70 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6eb80 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  TE char *sqlite3
6eb90 44 62 53 74 72 4e 44 75 70 28 73 71 6c 69 74 65  DbStrNDup(sqlite
6eba0 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  3*,const char*, 
6ebb0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6ebc0 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
6ebd0 65 33 52 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c  e3Realloc(void*,
6ebe0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
6ebf0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
6ec00 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72  te3DbReallocOrFr
6ec10 65 65 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f  ee(sqlite3 *, vo
6ec20 69 64 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  id *, int);.SQLI
6ec30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
6ec40 2a 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f  *sqlite3DbReallo
6ec50 63 28 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69  c(sqlite3 *, voi
6ec60 64 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  d *, int);.SQLIT
6ec70 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6ec80 71 6c 69 74 65 33 44 62 46 72 65 65 28 73 71 6c  qlite3DbFree(sql
6ec90 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53  ite3*, void*);.S
6eca0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6ecb0 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53  t sqlite3MallocS
6ecc0 69 7a 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49  ize(void*);.SQLI
6ecd0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
6ece0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69  qlite3DbMallocSi
6ecf0 7a 65 28 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69  ze(sqlite3*, voi
6ed00 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  d*);.SQLITE_PRIV
6ed10 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
6ed20 33 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 69  3ScratchMalloc(i
6ed30 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6ed40 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6ed50 53 63 72 61 74 63 68 46 72 65 65 28 76 6f 69 64  ScratchFree(void
6ed60 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6ed70 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
6ed80 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b  PageMalloc(int);
6ed90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6eda0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65  void sqlite3Page
6edb0 46 72 65 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c  Free(void*);.SQL
6edc0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6edd0 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65   sqlite3MemSetDe
6ede0 66 61 75 6c 74 28 76 6f 69 64 29 3b 0a 53 51 4c  fault(void);.SQL
6edf0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6ee00 20 73 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61   sqlite3BenignMa
6ee10 6c 6c 6f 63 48 6f 6f 6b 73 28 76 6f 69 64 20 28  llocHooks(void (
6ee20 2a 29 28 76 6f 69 64 29 2c 20 76 6f 69 64 20 28  *)(void), void (
6ee30 2a 29 28 76 6f 69 64 29 29 3b 0a 53 51 4c 49 54  *)(void));.SQLIT
6ee40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6ee50 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d  lite3MemoryAlarm
6ee60 28 76 6f 69 64 20 28 2a 29 28 76 6f 69 64 2a 2c  (void (*)(void*,
6ee70 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20   sqlite3_int64, 
6ee80 69 6e 74 29 2c 20 76 6f 69 64 2a 2c 20 73 71 6c  int), void*, sql
6ee90 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 0a 2f 2a  ite3_int64);../*
6eea0 0a 2a 2a 20 4f 6e 20 73 79 73 74 65 6d 73 20 77  .** On systems w
6eeb0 69 74 68 20 61 6d 70 6c 65 20 73 74 61 63 6b 20  ith ample stack 
6eec0 73 70 61 63 65 20 61 6e 64 20 74 68 61 74 20 73  space and that s
6eed0 75 70 70 6f 72 74 20 61 6c 6c 6f 63 61 28 29 2c  upport alloca(),
6eee0 20 6d 61 6b 65 0a 2a 2a 20 75 73 65 20 6f 66 20   make.** use of 
6eef0 61 6c 6c 6f 63 61 28 29 20 74 6f 20 6f 62 74 61  alloca() to obta
6ef00 69 6e 20 73 70 61 63 65 20 66 6f 72 20 6c 61 72  in space for lar
6ef10 67 65 20 61 75 74 6f 6d 61 74 69 63 20 6f 62 6a  ge automatic obj
6ef20 65 63 74 73 2e 20 20 42 79 20 64 65 66 61 75 6c  ects.  By defaul
6ef30 74 2c 0a 2a 2a 20 6f 62 74 61 69 6e 20 73 70 61  t,.** obtain spa
6ef40 63 65 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29  ce from malloc()
6ef50 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 6c 6c 6f  ..**.** The allo
6ef60 63 61 28 29 20 72 6f 75 74 69 6e 65 20 6e 65 76  ca() routine nev
6ef70 65 72 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e  er returns NULL.
6ef80 20 20 54 68 69 73 20 77 69 6c 6c 20 63 61 75 73    This will caus
6ef90 65 20 63 6f 64 65 20 70 61 74 68 73 0a 2a 2a 20  e code paths.** 
6efa0 74 68 61 74 20 64 65 61 6c 20 77 69 74 68 20 73  that deal with s
6efb0 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63  qlite3StackAlloc
6efc0 28 29 20 66 61 69 6c 75 72 65 73 20 74 6f 20 62  () failures to b
6efd0 65 20 75 6e 72 65 61 63 68 61 62 6c 65 2e 0a 2a  e unreachable..*
6efe0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
6eff0 55 53 45 5f 41 4c 4c 4f 43 41 0a 23 20 64 65 66  USE_ALLOCA.# def
6f000 69 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b  ine sqlite3Stack
6f010 41 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 20 20 20  AllocRaw(D,N)   
6f020 61 6c 6c 6f 63 61 28 4e 29 0a 23 20 64 65 66 69  alloca(N).# defi
6f030 6e 65 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41  ne sqlite3StackA
6f040 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 20 20 6d  llocZero(D,N)  m
6f050 65 6d 73 65 74 28 61 6c 6c 6f 63 61 28 4e 29 2c  emset(alloca(N),
6f060 20 30 2c 20 4e 29 0a 23 20 64 65 66 69 6e 65 20   0, N).# define 
6f070 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65  sqlite3StackFree
6f080 28 44 2c 50 29 20 20 20 20 20 20 20 0a 23 65 6c  (D,P)       .#el
6f090 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  se.# define sqli
6f0a0 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77  te3StackAllocRaw
6f0b0 28 44 2c 4e 29 20 20 20 73 71 6c 69 74 65 33 44  (D,N)   sqlite3D
6f0c0 62 4d 61 6c 6c 6f 63 52 61 77 28 44 2c 4e 29 0a  bMallocRaw(D,N).
6f0d0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
6f0e0 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f 28 44  StackAllocZero(D
6f0f0 2c 4e 29 20 20 73 71 6c 69 74 65 33 44 62 4d 61  ,N)  sqlite3DbMa
6f100 6c 6c 6f 63 5a 65 72 6f 28 44 2c 4e 29 0a 23 20  llocZero(D,N).# 
6f110 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 74  define sqlite3St
6f120 61 63 6b 46 72 65 65 28 44 2c 50 29 20 20 20 20  ackFree(D,P)    
6f130 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
6f140 28 44 2c 50 29 0a 23 65 6e 64 69 66 0a 0a 23 69  (D,P).#endif..#i
6f150 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
6f160 4c 45 5f 4d 45 4d 53 59 53 33 0a 53 51 4c 49 54  LE_MEMSYS3.SQLIT
6f170 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
6f180 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68  sqlite3_mem_meth
6f190 6f 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47  ods *sqlite3MemG
6f1a0 65 74 4d 65 6d 73 79 73 33 28 76 6f 69 64 29 3b  etMemsys3(void);
6f1b0 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
6f1c0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
6f1d0 53 59 53 35 0a 53 51 4c 49 54 45 5f 50 52 49 56  SYS5.SQLITE_PRIV
6f1e0 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ATE const sqlite
6f1f0 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73  3_mem_methods *s
6f200 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73  qlite3MemGetMems
6f210 79 73 35 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69  ys5(void);.#endi
6f220 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  f...#ifndef SQLI
6f230 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 53 51  TE_MUTEX_OMIT.SQ
6f240 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73  LITE_PRIVATE   s
6f250 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
6f260 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66  hods *sqlite3Def
6f270 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 3b  aultMutex(void);
6f280 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f290 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
6f2a0 2a 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c  *sqlite3MutexAll
6f2b0 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  oc(int);.SQLITE_
6f2c0 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
6f2d0 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 76  lite3MutexInit(v
6f2e0 6f 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  oid);.SQLITE_PRI
6f2f0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
6f300 65 33 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29  e3MutexEnd(void)
6f310 3b 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45  ;.#endif..SQLITE
6f320 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
6f330 69 74 65 33 53 74 61 74 75 73 56 61 6c 75 65 28  ite3StatusValue(
6f340 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6f350 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6f360 33 53 74 61 74 75 73 41 64 64 28 69 6e 74 2c 20  3StatusAdd(int, 
6f370 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
6f380 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6f390 33 53 74 61 74 75 73 53 65 74 28 69 6e 74 2c 20  3StatusSet(int, 
6f3a0 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  int);..SQLITE_PR
6f3b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6f3c0 33 49 73 4e 61 4e 28 64 6f 75 62 6c 65 29 3b 0a  3IsNaN(double);.
6f3d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f3e0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 58 50 72  void sqlite3VXPr
6f3f0 69 6e 74 66 28 53 74 72 41 63 63 75 6d 2a 2c 20  intf(StrAccum*, 
6f400 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  int, const char*
6f410 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49  , va_list);.SQLI
6f420 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
6f430 2a 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28  *sqlite3MPrintf(
6f440 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
6f450 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49  har*, ...);.SQLI
6f460 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20  TE_PRIVATE char 
6f470 2a 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66  *sqlite3VMPrintf
6f480 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
6f490 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b  char*, va_list);
6f4a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f4b0 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4d 41 70  char *sqlite3MAp
6f4c0 70 65 6e 64 66 28 73 71 6c 69 74 65 33 2a 2c 63  pendf(sqlite3*,c
6f4d0 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
6f4e0 2c 2e 2e 2e 29 3b 0a 23 69 66 20 64 65 66 69 6e  ,...);.#if defin
6f4f0 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20  ed(SQLITE_TEST) 
6f500 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
6f510 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 54 45 5f  E_DEBUG).SQLITE_
6f520 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
6f530 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
6f540 66 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e  f(const char*, .
6f550 2e 2e 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20  ..);.#endif.#if 
6f560 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54  defined(SQLITE_T
6f570 45 53 54 29 0a 53 51 4c 49 54 45 5f 50 52 49 56  EST).SQLITE_PRIV
6f580 41 54 45 20 20 20 76 6f 69 64 20 2a 73 71 6c 69  ATE   void *sqli
6f590 74 65 33 54 65 73 74 54 65 78 74 54 6f 50 74 72  te3TestTextToPtr
6f5a0 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23  (const char*);.#
6f5b0 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49  endif.SQLITE_PRI
6f5c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
6f5d0 33 53 65 74 53 74 72 69 6e 67 28 63 68 61 72 20  3SetString(char 
6f5e0 2a 2a 2c 20 73 71 6c 69 74 65 33 2a 2c 20 63 6f  **, sqlite3*, co
6f5f0 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b  nst char*, ...);
6f600 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6f610 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f  void sqlite3Erro
6f620 72 4d 73 67 28 50 61 72 73 65 2a 2c 20 63 6f 6e  rMsg(Parse*, con
6f630 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a  st char*, ...);.
6f640 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6f650 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72  oid sqlite3Error
6f660 43 6c 65 61 72 28 50 61 72 73 65 2a 29 3b 0a 53  Clear(Parse*);.S
6f670 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6f680 74 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65  t sqlite3Dequote
6f690 28 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  (char*);.SQLITE_
6f6a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
6f6b0 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 63  te3KeywordCode(c
6f6c0 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
6f6d0 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  ar*, int);.SQLIT
6f6e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
6f6f0 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 28 50  lite3RunParser(P
6f700 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61  arse*, const cha
6f710 72 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a 53 51  r*, char **);.SQ
6f720 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6f730 64 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43  d sqlite3FinishC
6f740 6f 64 69 6e 67 28 50 61 72 73 65 2a 29 3b 0a 53  oding(Parse*);.S
6f750 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
6f760 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  t sqlite3GetTemp
6f770 52 65 67 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c  Reg(Parse*);.SQL
6f780 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6f790 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
6f7a0 65 6d 70 52 65 67 28 50 61 72 73 65 2a 2c 69 6e  empReg(Parse*,in
6f7b0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6f7c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65  TE int sqlite3Ge
6f7d0 74 54 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65  tTempRange(Parse
6f7e0 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *,int);.SQLITE_P
6f7f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
6f800 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61  te3ReleaseTempRa
6f810 6e 67 65 28 50 61 72 73 65 2a 2c 69 6e 74 2c 69  nge(Parse*,int,i
6f820 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
6f830 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
6f840 33 45 78 70 72 41 6c 6c 6f 63 28 73 71 6c 69 74  3ExprAlloc(sqlit
6f850 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 54 6f  e3*,int,const To
6f860 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ken*,int);.SQLIT
6f870 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
6f880 73 71 6c 69 74 65 33 45 78 70 72 28 73 71 6c 69  sqlite3Expr(sqli
6f890 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  te3*,int,const c
6f8a0 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
6f8b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6f8c0 65 33 45 78 70 72 41 74 74 61 63 68 53 75 62 74  e3ExprAttachSubt
6f8d0 72 65 65 73 28 73 71 6c 69 74 65 33 2a 2c 45 78  rees(sqlite3*,Ex
6f8e0 70 72 2a 2c 45 78 70 72 2a 2c 45 78 70 72 2a 29  pr*,Expr*,Expr*)
6f8f0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
6f900 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 50 45   Expr *sqlite3PE
6f910 78 70 72 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c  xpr(Parse*, int,
6f920 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 2c 20 63   Expr*, Expr*, c
6f930 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  onst Token*);.SQ
6f940 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
6f950 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e  r *sqlite3ExprAn
6f960 64 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a  d(sqlite3*,Expr*
6f970 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
6f980 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73  _PRIVATE Expr *s
6f990 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 69  qlite3ExprFuncti
6f9a0 6f 6e 28 50 61 72 73 65 2a 2c 45 78 70 72 4c 69  on(Parse*,ExprLi
6f9b0 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  st*, Token*);.SQ
6f9c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6f9d0 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 73 73  d sqlite3ExprAss
6f9e0 69 67 6e 56 61 72 4e 75 6d 62 65 72 28 50 61 72  ignVarNumber(Par
6f9f0 73 65 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c  se*, Expr*);.SQL
6fa00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6fa10 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61   sqlite3ExprClea
6fa20 72 28 73 71 6c 69 74 65 33 2a 2c 20 45 78 70 72  r(sqlite3*, Expr
6fa30 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6fa40 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
6fa50 78 70 72 44 65 6c 65 74 65 28 73 71 6c 69 74 65  xprDelete(sqlite
6fa60 33 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49  3*, Expr*);.SQLI
6fa70 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c  TE_PRIVATE ExprL
6fa80 69 73 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72  ist *sqlite3Expr
6fa90 4c 69 73 74 41 70 70 65 6e 64 28 50 61 72 73 65  ListAppend(Parse
6faa0 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 45 78 70 72  *,ExprList*,Expr
6fab0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
6fac0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
6fad0 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 50  xprListSetName(P
6fae0 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c  arse*,ExprList*,
6faf0 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53 51 4c  Token*,int);.SQL
6fb00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6fb10 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
6fb20 53 65 74 53 70 61 6e 28 50 61 72 73 65 2a 2c 45  SetSpan(Parse*,E
6fb30 78 70 72 4c 69 73 74 2a 2c 45 78 70 72 53 70 61  xprList*,ExprSpa
6fb40 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
6fb50 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
6fb60 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73  ExprListDelete(s
6fb70 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 4c 69 73  qlite3*, ExprLis
6fb80 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
6fb90 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
6fba0 6e 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 63 68  nit(sqlite3*, ch
6fbb0 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ar**);.SQLITE_PR
6fbc0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
6fbd0 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f  3InitCallback(vo
6fbe0 69 64 2a 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a  id*, int, char**
6fbf0 2c 20 63 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54  , char**);.SQLIT
6fc00 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
6fc10 71 6c 69 74 65 33 50 72 61 67 6d 61 28 50 61 72  qlite3Pragma(Par
6fc20 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e  se*,Token*,Token
6fc30 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53  *,Token*,int);.S
6fc40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
6fc50 69 64 20 73 71 6c 69 74 65 33 52 65 73 65 74 49  id sqlite3ResetI
6fc60 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71  nternalSchema(sq
6fc70 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51  lite3*, int);.SQ
6fc80 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6fc90 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61  d sqlite3BeginPa
6fca0 72 73 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b  rse(Parse*,int);
6fcb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
6fcc0 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d  void sqlite3Comm
6fcd0 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65  itInternalChange
6fce0 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c  s(sqlite3*);.SQL
6fcf0 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c  ITE_PRIVATE Tabl
6fd00 65 20 2a 73 71 6c 69 74 65 33 52 65 73 75 6c 74  e *sqlite3Result
6fd10 53 65 74 4f 66 53 65 6c 65 63 74 28 50 61 72 73  SetOfSelect(Pars
6fd20 65 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c  e*,Select*);.SQL
6fd30 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6fd40 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74   sqlite3OpenMast
6fd50 65 72 54 61 62 6c 65 28 50 61 72 73 65 20 2a 2c  erTable(Parse *,
6fd60 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
6fd70 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6fd80 65 33 53 74 61 72 74 54 61 62 6c 65 28 50 61 72  e3StartTable(Par
6fd90 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e  se*,Token*,Token
6fda0 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e  *,int,int,int,in
6fdb0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
6fdc0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
6fdd0 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c  ddColumn(Parse*,
6fde0 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
6fdf0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6fe00 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50  ite3AddNotNull(P
6fe10 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  arse*, int);.SQL
6fe20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
6fe30 20 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61   sqlite3AddPrima
6fe40 72 79 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78  ryKey(Parse*, Ex
6fe50 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e  prList*, int, in
6fe60 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
6fe70 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6fe80 69 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73  ite3AddCheckCons
6fe90 74 72 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 45  traint(Parse*, E
6fea0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
6feb0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
6fec0 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28  e3AddColumnType(
6fed0 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a  Parse*,Token*);.
6fee0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
6fef0 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 44 65  oid sqlite3AddDe
6ff00 66 61 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65  faultValue(Parse
6ff10 2a 2c 45 78 70 72 53 70 61 6e 2a 29 3b 0a 53 51  *,ExprSpan*);.SQ
6ff20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
6ff30 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c  d sqlite3AddColl
6ff40 61 74 65 54 79 70 65 28 50 61 72 73 65 2a 2c 20  ateType(Parse*, 
6ff50 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
6ff60 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
6ff70 69 74 65 33 45 6e 64 54 61 62 6c 65 28 50 61 72  ite3EndTable(Par
6ff80 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e  se*,Token*,Token
6ff90 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 0a 53 51 4c  *,Select*);..SQL
6ffa0 49 54 45 5f 50 52 49 56 41 54 45 20 42 69 74 76  ITE_PRIVATE Bitv
6ffb0 65 63 20 2a 73 71 6c 69 74 65 33 42 69 74 76 65  ec *sqlite3Bitve
6ffc0 63 43 72 65 61 74 65 28 75 33 32 29 3b 0a 53 51  cCreate(u32);.SQ
6ffd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
6ffe0 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65   sqlite3BitvecTe
6fff0 73 74 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29  st(Bitvec*, u32)
70000 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70010 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76   int sqlite3Bitv
70020 65 63 53 65 74 28 42 69 74 76 65 63 2a 2c 20 75  ecSet(Bitvec*, u
70030 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  32);.SQLITE_PRIV
70040 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
70050 42 69 74 76 65 63 43 6c 65 61 72 28 42 69 74 76  BitvecClear(Bitv
70060 65 63 2a 2c 20 75 33 32 2c 20 76 6f 69 64 2a 29  ec*, u32, void*)
70070 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70080 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74   void sqlite3Bit
70090 76 65 63 44 65 73 74 72 6f 79 28 42 69 74 76 65  vecDestroy(Bitve
700a0 63 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  c*);.SQLITE_PRIV
700b0 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33 42  ATE u32 sqlite3B
700c0 69 74 76 65 63 53 69 7a 65 28 42 69 74 76 65 63  itvecSize(Bitvec
700d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
700e0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69  TE int sqlite3Bi
700f0 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73 74 28  tvecBuiltinTest(
70100 69 6e 74 2c 69 6e 74 2a 29 3b 0a 0a 53 51 4c 49  int,int*);..SQLI
70110 54 45 5f 50 52 49 56 41 54 45 20 52 6f 77 53 65  TE_PRIVATE RowSe
70120 74 20 2a 73 71 6c 69 74 65 33 52 6f 77 53 65 74  t *sqlite3RowSet
70130 49 6e 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 76  Init(sqlite3*, v
70140 6f 69 64 2a 2c 20 75 6e 73 69 67 6e 65 64 20 69  oid*, unsigned i
70150 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
70160 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
70170 52 6f 77 53 65 74 43 6c 65 61 72 28 52 6f 77 53  RowSetClear(RowS
70180 65 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  et*);.SQLITE_PRI
70190 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
701a0 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 52 6f  3RowSetInsert(Ro
701b0 77 53 65 74 2a 2c 20 69 36 34 29 3b 0a 53 51 4c  wSet*, i64);.SQL
701c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
701d0 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65 73  sqlite3RowSetTes
701e0 74 28 52 6f 77 53 65 74 2a 2c 20 75 38 20 69 42  t(RowSet*, u8 iB
701f0 61 74 63 68 2c 20 69 36 34 29 3b 0a 53 51 4c 49  atch, i64);.SQLI
70200 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
70210 71 6c 69 74 65 33 52 6f 77 53 65 74 4e 65 78 74  qlite3RowSetNext
70220 28 52 6f 77 53 65 74 2a 2c 20 69 36 34 2a 29 3b  (RowSet*, i64*);
70230 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
70240 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65   void sqlite3Cre
70250 61 74 65 56 69 65 77 28 50 61 72 73 65 2a 2c 54  ateView(Parse*,T
70260 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b  oken*,Token*,Tok
70270 65 6e 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e 74 2c  en*,Select*,int,
70280 69 6e 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 69  int);..#if !defi
70290 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
702a0 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65  VIEW) || !define
702b0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  d(SQLITE_OMIT_VI
702c0 52 54 55 41 4c 54 41 42 4c 45 29 0a 53 51 4c 49  RTUALTABLE).SQLI
702d0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
702e0 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43   sqlite3ViewGetC
702f0 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65  olumnNames(Parse
70300 2a 2c 54 61 62 6c 65 2a 29 3b 0a 23 65 6c 73 65  *,Table*);.#else
70310 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
70320 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61  3ViewGetColumnNa
70330 6d 65 73 28 41 2c 42 29 20 30 0a 23 65 6e 64 69  mes(A,B) 0.#endi
70340 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  f..SQLITE_PRIVAT
70350 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72  E void sqlite3Dr
70360 6f 70 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 20  opTable(Parse*, 
70370 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69  SrcList*, int, i
70380 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
70390 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
703a0 44 65 6c 65 74 65 54 61 62 6c 65 28 54 61 62 6c  DeleteTable(Tabl
703b0 65 2a 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  e*);.#ifndef SQL
703c0 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
703d0 52 45 4d 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52  REMENT.SQLITE_PR
703e0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
703f0 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e  ite3Autoincremen
70400 74 42 65 67 69 6e 28 50 61 72 73 65 20 2a 70 50  tBegin(Parse *pP
70410 61 72 73 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  arse);.SQLITE_PR
70420 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
70430 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e  ite3Autoincremen
70440 74 45 6e 64 28 50 61 72 73 65 20 2a 70 50 61 72  tEnd(Parse *pPar
70450 73 65 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  se);.#else.# def
70460 69 6e 65 20 73 71 6c 69 74 65 33 41 75 74 6f 69  ine sqlite3Autoi
70470 6e 63 72 65 6d 65 6e 74 42 65 67 69 6e 28 58 29  ncrementBegin(X)
70480 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
70490 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 45 6e  3AutoincrementEn
704a0 64 28 58 29 0a 23 65 6e 64 69 66 0a 53 51 4c 49  d(X).#endif.SQLI
704b0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
704c0 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 50 61  sqlite3Insert(Pa
704d0 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20  rse*, SrcList*, 
704e0 45 78 70 72 4c 69 73 74 2a 2c 20 53 65 6c 65 63  ExprList*, Selec
704f0 74 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 69 6e 74  t*, IdList*, int
70500 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
70510 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 41  E void *sqlite3A
70520 72 72 61 79 41 6c 6c 6f 63 61 74 65 28 73 71 6c  rrayAllocate(sql
70530 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c  ite3*,void*,int,
70540 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e  int,int*,int*,in
70550 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
70560 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69  ATE IdList *sqli
70570 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28  te3IdListAppend(
70580 73 71 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73 74  sqlite3*, IdList
70590 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
705a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
705b0 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65  qlite3IdListInde
705c0 78 28 49 64 4c 69 73 74 2a 2c 63 6f 6e 73 74 20  x(IdList*,const 
705d0 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  char*);.SQLITE_P
705e0 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a  RIVATE SrcList *
705f0 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e  sqlite3SrcListEn
70600 6c 61 72 67 65 28 73 71 6c 69 74 65 33 2a 2c 20  large(sqlite3*, 
70610 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69  SrcList*, int, i
70620 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
70630 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c  ATE SrcList *sql
70640 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
70650 64 28 73 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c  d(sqlite3*, SrcL
70660 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f  ist*, Token*, To
70670 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
70680 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73  IVATE SrcList *s
70690 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
706a0 65 6e 64 46 72 6f 6d 54 65 72 6d 28 50 61 72 73  endFromTerm(Pars
706b0 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54 6f  e*, SrcList*, To
706c0 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 0a 20 20  ken*, Token*,.  
706d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
706e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
706f0 20 20 20 20 54 6f 6b 65 6e 2a 2c 20 53 65 6c 65      Token*, Sele
70700 63 74 2a 2c 20 45 78 70 72 2a 2c 20 49 64 4c 69  ct*, Expr*, IdLi
70710 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
70720 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
70730 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65 64 42  3SrcListIndexedB
70740 79 28 50 61 72 73 65 20 2a 2c 20 53 72 63 4c 69  y(Parse *, SrcLi
70750 73 74 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a  st *, Token *);.
70760 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
70770 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65 78 65  nt sqlite3Indexe
70780 64 42 79 4c 6f 6f 6b 75 70 28 50 61 72 73 65 20  dByLookup(Parse 
70790 2a 2c 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  *, struct SrcLis
707a0 74 5f 69 74 65 6d 20 2a 29 3b 0a 53 51 4c 49 54  t_item *);.SQLIT
707b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
707c0 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69  qlite3SrcListShi
707d0 66 74 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c 69  ftJoinType(SrcLi
707e0 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
707f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
70800 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75  3SrcListAssignCu
70810 72 73 6f 72 73 28 50 61 72 73 65 2a 2c 20 53 72  rsors(Parse*, Sr
70820 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  cList*);.SQLITE_
70830 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
70840 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65  ite3IdListDelete
70850 28 73 71 6c 69 74 65 33 2a 2c 20 49 64 4c 69 73  (sqlite3*, IdLis
70860 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
70870 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
70880 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 73 71  SrcListDelete(sq
70890 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a  lite3*, SrcList*
708a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
708b0 45 20 49 6e 64 65 78 20 2a 73 71 6c 69 74 65 33  E Index *sqlite3
708c0 43 72 65 61 74 65 49 6e 64 65 78 28 50 61 72 73  CreateIndex(Pars
708d0 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a  e*,Token*,Token*
708e0 2c 53 72 63 4c 69 73 74 2a 2c 45 78 70 72 4c 69  ,SrcList*,ExprLi
708f0 73 74 2a 2c 69 6e 74 2c 54 6f 6b 65 6e 2a 2c 0a  st*,int,Token*,.
70900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70910 20 20 20 20 20 20 20 20 54 6f 6b 65 6e 2a 2c 20          Token*, 
70920 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  int, int);.SQLIT
70930 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
70940 71 6c 69 74 65 33 44 72 6f 70 49 6e 64 65 78 28  qlite3DropIndex(
70950 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a  Parse*, SrcList*
70960 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
70970 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
70980 65 33 53 65 6c 65 63 74 28 50 61 72 73 65 2a 2c  e3Select(Parse*,
70990 20 53 65 6c 65 63 74 2a 2c 20 53 65 6c 65 63 74   Select*, Select
709a0 44 65 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Dest*);.SQLITE_P
709b0 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73  RIVATE Select *s
709c0 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28  qlite3SelectNew(
709d0 50 61 72 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a  Parse*,ExprList*
709e0 2c 53 72 63 4c 69 73 74 2a 2c 45 78 70 72 2a 2c  ,SrcList*,Expr*,
709f0 45 78 70 72 4c 69 73 74 2a 2c 0a 20 20 20 20 20  ExprList*,.     
70a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
70a10 20 20 20 20 45 78 70 72 2a 2c 45 78 70 72 4c 69      Expr*,ExprLi
70a20 73 74 2a 2c 69 6e 74 2c 45 78 70 72 2a 2c 45 78  st*,int,Expr*,Ex
70a30 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
70a40 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
70a50 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 73 71  3SelectDelete(sq
70a60 6c 69 74 65 33 2a 2c 20 53 65 6c 65 63 74 2a 29  lite3*, Select*)
70a70 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70a80 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 53   Table *sqlite3S
70a90 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 50 61 72  rcListLookup(Par
70aa0 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 29 3b 0a  se*, SrcList*);.
70ab0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
70ac0 6e 74 20 73 71 6c 69 74 65 33 49 73 52 65 61 64  nt sqlite3IsRead
70ad0 4f 6e 6c 79 28 50 61 72 73 65 2a 2c 20 54 61 62  Only(Parse*, Tab
70ae0 6c 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  le*, int);.SQLIT
70af0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
70b00 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28  qlite3OpenTable(
70b10 50 61 72 73 65 2a 2c 20 69 6e 74 20 69 43 75 72  Parse*, int iCur
70b20 2c 20 69 6e 74 20 69 44 62 2c 20 54 61 62 6c 65  , int iDb, Table
70b30 2a 2c 20 69 6e 74 29 3b 0a 23 69 66 20 64 65 66  *, int);.#if def
70b40 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
70b50 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54 45  LE_UPDATE_DELETE
70b60 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66 69  _LIMIT) && !defi
70b70 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
70b80 53 55 42 51 55 45 52 59 29 0a 53 51 4c 49 54 45  SUBQUERY).SQLITE
70b90 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73  _PRIVATE Expr *s
70ba0 71 6c 69 74 65 33 4c 69 6d 69 74 57 68 65 72 65  qlite3LimitWhere
70bb0 28 50 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73  (Parse *, SrcLis
70bc0 74 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70  t *, Expr *, Exp
70bd0 72 4c 69 73 74 20 2a 2c 20 45 78 70 72 20 2a 2c  rList *, Expr *,
70be0 20 45 78 70 72 20 2a 2c 20 63 68 61 72 20 2a 29   Expr *, char *)
70bf0 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f  ;.#endif.SQLITE_
70c00 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
70c10 69 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 50  ite3DeleteFrom(P
70c20 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c  arse*, SrcList*,
70c30 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
70c40 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
70c50 69 74 65 33 55 70 64 61 74 65 28 50 61 72 73 65  ite3Update(Parse
70c60 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 45 78 70  *, SrcList*, Exp
70c70 72 4c 69 73 74 2a 2c 20 45 78 70 72 2a 2c 20 69  rList*, Expr*, i
70c80 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
70c90 41 54 45 20 57 68 65 72 65 49 6e 66 6f 20 2a 73  ATE WhereInfo *s
70ca0 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e  qlite3WhereBegin
70cb0 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74  (Parse*, SrcList
70cc0 2a 2c 20 45 78 70 72 2a 2c 20 45 78 70 72 4c 69  *, Expr*, ExprLi
70cd0 73 74 2a 2a 2c 20 75 31 36 29 3b 0a 53 51 4c 49  st**, u16);.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 57 68 65 72 65 45 6e 64 28  sqlite3WhereEnd(
70d00 57 68 65 72 65 49 6e 66 6f 2a 29 3b 0a 53 51 4c  WhereInfo*);.SQL
70d10 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
70d20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47  sqlite3ExprCodeG
70d30 65 74 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c  etColumn(Parse*,
70d40 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e   Table*, int, in
70d50 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  t, int, int);.SQ
70d60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
70d70 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  d sqlite3ExprCod
70d80 65 4d 6f 76 65 28 50 61 72 73 65 2a 2c 20 69 6e  eMove(Parse*, in
70d90 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  t, int, int);.SQ
70da0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
70db0 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  d sqlite3ExprCod
70dc0 65 43 6f 70 79 28 50 61 72 73 65 2a 2c 20 69 6e  eCopy(Parse*, in
70dd0 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  t, int, int);.SQ
70de0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
70df0 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63  d sqlite3ExprCac
70e00 68 65 53 74 6f 72 65 28 50 61 72 73 65 2a 2c 20  heStore(Parse*, 
70e10 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  int, int, int);.
70e20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
70e30 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
70e40 61 63 68 65 50 75 73 68 28 50 61 72 73 65 2a 29  achePush(Parse*)
70e50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
70e60 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
70e70 72 43 61 63 68 65 50 6f 70 28 50 61 72 73 65 2a  rCachePop(Parse*
70e80 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
70e90 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
70ea0 74 65 33 45 78 70 72 43 61 63 68 65 52 65 6d 6f  te3ExprCacheRemo
70eb0 76 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b  ve(Parse*, int);
70ec0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
70ed0 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
70ee0 43 61 63 68 65 43 6c 65 61 72 28 50 61 72 73 65  CacheClear(Parse
70ef0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
70f00 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
70f10 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79  xprCacheAffinity
70f20 43 68 61 6e 67 65 28 50 61 72 73 65 2a 2c 20 69  Change(Parse*, i
70f30 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
70f40 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
70f50 6c 69 74 65 33 45 78 70 72 48 61 72 64 43 6f 70  lite3ExprHardCop
70f60 79 28 50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74  y(Parse*,int,int
70f70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
70f80 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
70f90 72 43 6f 64 65 28 50 61 72 73 65 2a 2c 20 45 78  rCode(Parse*, Ex
70fa0 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  pr*, int);.SQLIT
70fb0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
70fc0 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
70fd0 70 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c  p(Parse*, Expr*,
70fe0 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50   int*);.SQLITE_P
70ff0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
71000 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74  e3ExprCodeTarget
71010 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20  (Parse*, Expr*, 
71020 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
71030 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
71040 45 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68 65  ExprCodeAndCache
71050 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20  (Parse*, Expr*, 
71060 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
71070 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
71080 33 45 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e  3ExprCodeConstan
71090 74 73 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a  ts(Parse*, Expr*
710a0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
710b0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
710c0 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28 50 61  rCodeExprList(Pa
710d0 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c  rse*, ExprList*,
710e0 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
710f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
71100 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75  sqlite3ExprIfTru
71110 65 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c  e(Parse*, Expr*,
71120 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
71130 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
71140 73 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c  sqlite3ExprIfFal
71150 73 65 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a  se(Parse*, Expr*
71160 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c  , int, int);.SQL
71170 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c  ITE_PRIVATE Tabl
71180 65 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 54 61  e *sqlite3FindTa
71190 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  ble(sqlite3*,con
711a0 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20  st char*, const 
711b0 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  char*);.SQLITE_P
711c0 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71  RIVATE Table *sq
711d0 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
711e0 28 50 61 72 73 65 2a 2c 69 6e 74 20 69 73 56 69  (Parse*,int isVi
711f0 65 77 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  ew,const char*, 
71200 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51  const char*);.SQ
71210 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64  LITE_PRIVATE Ind
71220 65 78 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 49  ex *sqlite3FindI
71230 6e 64 65 78 28 73 71 6c 69 74 65 33 2a 2c 63 6f  ndex(sqlite3*,co
71240 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74  nst char*, const
71250 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   char*);.SQLITE_
71260 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
71270 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c  ite3UnlinkAndDel
71280 65 74 65 54 61 62 6c 65 28 73 71 6c 69 74 65 33  eteTable(sqlite3
71290 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72  *,int,const char
712a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
712b0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55  TE void sqlite3U
712c0 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e  nlinkAndDeleteIn
712d0 64 65 78 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74  dex(sqlite3*,int
712e0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53  ,const char*);.S
712f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
71300 69 64 20 73 71 6c 69 74 65 33 56 61 63 75 75 6d  id sqlite3Vacuum
71310 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45  (Parse*);.SQLITE
71320 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
71330 69 74 65 33 52 75 6e 56 61 63 75 75 6d 28 63 68  ite3RunVacuum(ch
71340 61 72 2a 2a 2c 20 73 71 6c 69 74 65 33 2a 29 3b  ar**, sqlite3*);
71350 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71360 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4e 61 6d  char *sqlite3Nam
71370 65 46 72 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69 74  eFromToken(sqlit
71380 65 33 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  e3*, Token*);.SQ
71390 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
713a0 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70   sqlite3ExprComp
713b0 61 72 65 28 45 78 70 72 2a 2c 20 45 78 70 72 2a  are(Expr*, Expr*
713c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
713d0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
713e0 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61  prAnalyzeAggrega
713f0 74 65 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a  tes(NameContext*
71400 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
71410 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
71420 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65  lite3ExprAnalyze
71430 41 67 67 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74  AggList(NameCont
71440 65 78 74 2a 2c 45 78 70 72 4c 69 73 74 2a 29 3b  ext*,ExprList*);
71450 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71460 56 64 62 65 20 2a 73 71 6c 69 74 65 33 47 65 74  Vdbe *sqlite3Get
71470 56 64 62 65 28 50 61 72 73 65 2a 29 3b 0a 53 51  Vdbe(Parse*);.SQ
71480 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
71490 64 20 73 71 6c 69 74 65 33 50 72 6e 67 53 61 76  d sqlite3PrngSav
714a0 65 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51  eState(void);.SQ
714b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
714c0 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73  d sqlite3PrngRes
714d0 74 6f 72 65 53 74 61 74 65 28 76 6f 69 64 29 3b  toreState(void);
714e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
714f0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67  void sqlite3Prng
71500 52 65 73 65 74 53 74 61 74 65 28 76 6f 69 64 29  ResetState(void)
71510 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
71520 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6c   void sqlite3Rol
71530 6c 62 61 63 6b 41 6c 6c 28 73 71 6c 69 74 65 33  lbackAll(sqlite3
71540 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
71550 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
71560 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28  odeVerifySchema(
71570 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51  Parse*, int);.SQ
71580 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
71590 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72  d sqlite3BeginTr
715a0 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a  ansaction(Parse*
715b0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
715c0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
715d0 74 65 33 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63  te3CommitTransac
715e0 74 69 6f 6e 28 50 61 72 73 65 2a 29 3b 0a 53 51  tion(Parse*);.SQ
715f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
71600 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63  d sqlite3Rollbac
71610 6b 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72  kTransaction(Par
71620 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  se*);.SQLITE_PRI
71630 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
71640 33 53 61 76 65 70 6f 69 6e 74 28 50 61 72 73 65  3Savepoint(Parse
71650 2a 2c 20 69 6e 74 2c 20 54 6f 6b 65 6e 2a 29 3b  *, int, Token*);
71660 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71670 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73  void sqlite3Clos
71680 65 53 61 76 65 70 6f 69 6e 74 73 28 73 71 6c 69  eSavepoints(sqli
71690 74 65 33 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  te3 *);.SQLITE_P
716a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
716b0 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74  e3ExprIsConstant
716c0 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Expr*);.SQLITE_
716d0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
716e0 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e  te3ExprIsConstan
716f0 74 4e 6f 74 4a 6f 69 6e 28 45 78 70 72 2a 29 3b  tNotJoin(Expr*);
71700 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71710 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 49  int sqlite3ExprI
71720 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63 74  sConstantOrFunct
71730 69 6f 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c 49  ion(Expr*);.SQLI
71740 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
71750 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65  qlite3ExprIsInte
71760 67 65 72 28 45 78 70 72 2a 2c 20 69 6e 74 2a 29  ger(Expr*, int*)
71770 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
71780 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 52 6f   int sqlite3IsRo
71790 77 69 64 28 63 6f 6e 73 74 20 63 68 61 72 2a 29  wid(const char*)
717a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
717b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65 6e   void sqlite3Gen
717c0 65 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28 50  erateRowDelete(P
717d0 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69  arse*, Table*, i
717e0 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 54 72  nt, int, int, Tr
717f0 69 67 67 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53  igger *, int);.S
71800 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
71810 69 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61  id sqlite3Genera
71820 74 65 52 6f 77 49 6e 64 65 78 44 65 6c 65 74 65  teRowIndexDelete
71830 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c  (Parse*, Table*,
71840 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c   int, int*);.SQL
71850 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
71860 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49  sqlite3GenerateI
71870 6e 64 65 78 4b 65 79 28 50 61 72 73 65 2a 2c 20  ndexKey(Parse*, 
71880 49 6e 64 65 78 2a 2c 20 69 6e 74 2c 20 69 6e 74  Index*, int, int
71890 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
718a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
718b0 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74  te3GenerateConst
718c0 72 61 69 6e 74 43 68 65 63 6b 73 28 50 61 72 73  raintChecks(Pars
718d0 65 2a 2c 54 61 62 6c 65 2a 2c 69 6e 74 2c 69 6e  e*,Table*,int,in
718e0 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
718f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71900 20 20 20 20 20 20 20 20 69 6e 74 2a 2c 69 6e 74          int*,int
71910 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  ,int,int,int,int
71920 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
71930 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
71940 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e  ompleteInsertion
71950 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c  (Parse*, Table*,
71960 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2a 2c   int, int, int*,
71970 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29 3b   int, int, int);
71980 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71990 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  int sqlite3OpenT
719a0 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28 50  ableAndIndices(P
719b0 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69  arse*, Table*, i
719c0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
719d0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
719e0 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
719f0 70 65 72 61 74 69 6f 6e 28 50 61 72 73 65 2a 2c  peration(Parse*,
71a00 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   int, int);.SQLI
71a10 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
71a20 73 71 6c 69 74 65 33 4d 75 6c 74 69 57 72 69 74  sqlite3MultiWrit
71a30 65 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54  e(Parse*);.SQLIT
71a40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
71a50 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 50  qlite3MayAbort(P
71a60 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  arse*);.SQLITE_P
71a70 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
71a80 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e  te3HaltConstrain
71a90 74 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 63  t(Parse*, int, c
71aa0 68 61 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  har*, int);.SQLI
71ab0 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
71ac0 2a 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28  *sqlite3ExprDup(
71ad0 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 69  sqlite3*,Expr*,i
71ae0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
71af0 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73 71  ATE ExprList *sq
71b00 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70  lite3ExprListDup
71b10 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 4c 69  (sqlite3*,ExprLi
71b20 73 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  st*,int);.SQLITE
71b30 5f 50 52 49 56 41 54 45 20 53 72 63 4c 69 73 74  _PRIVATE SrcList
71b40 20 2a 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74   *sqlite3SrcList
71b50 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 53 72 63  Dup(sqlite3*,Src
71b60 4c 69 73 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49  List*,int);.SQLI
71b70 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73  TE_PRIVATE IdLis
71b80 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74  t *sqlite3IdList
71b90 44 75 70 28 73 71 6c 69 74 65 33 2a 2c 49 64 4c  Dup(sqlite3*,IdL
71ba0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ist*);.SQLITE_PR
71bb0 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71  IVATE Select *sq
71bc0 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 73  lite3SelectDup(s
71bd0 71 6c 69 74 65 33 2a 2c 53 65 6c 65 63 74 2a 2c  qlite3*,Select*,
71be0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
71bf0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
71c00 33 46 75 6e 63 44 65 66 49 6e 73 65 72 74 28 46  3FuncDefInsert(F
71c10 75 6e 63 44 65 66 48 61 73 68 2a 2c 20 46 75 6e  uncDefHash*, Fun
71c20 63 44 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  cDef*);.SQLITE_P
71c30 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a  RIVATE FuncDef *
71c40 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74  sqlite3FindFunct
71c50 69 6f 6e 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  ion(sqlite3*,con
71c60 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 69 6e 74  st char*,int,int
71c70 2c 75 38 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  ,u8,int);.SQLITE
71c80 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
71c90 6c 69 74 65 33 52 65 67 69 73 74 65 72 42 75 69  lite3RegisterBui
71ca0 6c 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28 73 71  ltinFunctions(sq
71cb0 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f  lite3*);.SQLITE_
71cc0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
71cd0 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74 65  ite3RegisterDate
71ce0 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 76 6f  TimeFunctions(vo
71cf0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  id);.SQLITE_PRIV
71d00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
71d10 52 65 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 75  RegisterGlobalFu
71d20 6e 63 74 69 6f 6e 73 28 76 6f 69 64 29 3b 0a 23  nctions(void);.#
71d30 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
71d40 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
71d50 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53  E   int sqlite3S
71d60 61 66 65 74 79 4f 6e 28 73 71 6c 69 74 65 33 2a  afetyOn(sqlite3*
71d70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71d80 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 53  E   int sqlite3S
71d90 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74 65 33  afetyOff(sqlite3
71da0 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  *);.#else.# defi
71db0 6e 65 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  ne sqlite3Safety
71dc0 4f 6e 28 41 29 20 30 0a 23 20 64 65 66 69 6e 65  On(A) 0.# define
71dd0 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
71de0 66 28 41 29 20 30 0a 23 65 6e 64 69 66 0a 53 51  f(A) 0.#endif.SQ
71df0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
71e00 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68   sqlite3SafetyCh
71e10 65 63 6b 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b  eckOk(sqlite3*);
71e20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71e30 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65 74  int sqlite3Safet
71e40 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73  yCheckSickOrOk(s
71e50 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45  qlite3*);.SQLITE
71e60 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
71e70 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69  lite3ChangeCooki
71e80 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a  e(Parse*, int);.
71e90 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
71ea0 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
71eb0 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
71ec0 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29  TE_OMIT_TRIGGER)
71ed0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
71ee0 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 74 65  void sqlite3Mate
71ef0 72 69 61 6c 69 7a 65 56 69 65 77 28 50 61 72 73  rializeView(Pars
71f00 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78 70 72  e*, Table*, Expr
71f10 2a 2c 20 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a  *, int);.#endif.
71f20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
71f30 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 53 51 4c  OMIT_TRIGGER.SQL
71f40 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
71f50 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54  id sqlite3BeginT
71f60 72 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 54  rigger(Parse*, T
71f70 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74  oken*,Token*,int
71f80 2c 69 6e 74 2c 49 64 4c 69 73 74 2a 2c 53 72 63  ,int,IdList*,Src
71f90 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20  List*,.         
71fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
71fb0 20 20 45 78 70 72 2a 2c 69 6e 74 2c 20 69 6e 74    Expr*,int, int
71fc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
71fd0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
71fe0 46 69 6e 69 73 68 54 72 69 67 67 65 72 28 50 61  FinishTrigger(Pa
71ff0 72 73 65 2a 2c 20 54 72 69 67 67 65 72 53 74 65  rse*, TriggerSte
72000 70 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c  p*, Token*);.SQL
72010 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
72020 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72  id sqlite3DropTr
72030 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 53 72  igger(Parse*, Sr
72040 63 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51  cList*, int);.SQ
72050 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
72060 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54  oid sqlite3DropT
72070 72 69 67 67 65 72 50 74 72 28 50 61 72 73 65 2a  riggerPtr(Parse*
72080 2c 20 54 72 69 67 67 65 72 2a 29 3b 0a 53 51 4c  , Trigger*);.SQL
72090 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72  ITE_PRIVATE   Tr
720a0 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33 54 72  igger *sqlite3Tr
720b0 69 67 67 65 72 73 45 78 69 73 74 28 50 61 72 73  iggersExist(Pars
720c0 65 20 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74  e *, Table*, int
720d0 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e 74  , ExprList*, int
720e0 20 2a 70 4d 61 73 6b 29 3b 0a 53 51 4c 49 54 45   *pMask);.SQLITE
720f0 5f 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67  _PRIVATE   Trigg
72100 65 72 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67  er *sqlite3Trigg
72110 65 72 4c 69 73 74 28 50 61 72 73 65 20 2a 2c 20  erList(Parse *, 
72120 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45  Table *);.SQLITE
72130 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
72140 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72  sqlite3CodeRowTr
72150 69 67 67 65 72 28 50 61 72 73 65 2a 2c 20 54 72  igger(Parse*, Tr
72160 69 67 67 65 72 20 2a 2c 20 69 6e 74 2c 20 45 78  igger *, int, Ex
72170 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 54 61  prList*, int, Ta
72180 62 6c 65 20 2a 2c 0a 20 20 20 20 20 20 20 20 20  ble *,.         
72190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
721a0 20 20 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74     int, int, int
721b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
721c0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
721d0 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 44 69  CodeRowTriggerDi
721e0 72 65 63 74 28 50 61 72 73 65 20 2a 2c 20 54 72  rect(Parse *, Tr
721f0 69 67 67 65 72 20 2a 2c 20 54 61 62 6c 65 20 2a  igger *, Table *
72200 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 29  , int, int, int)
72210 3b 0a 20 20 76 6f 69 64 20 73 71 6c 69 74 65 56  ;.  void sqliteV
72220 69 65 77 54 72 69 67 67 65 72 73 28 50 61 72 73  iewTriggers(Pars
72230 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78 70 72  e*, Table*, Expr
72240 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74  *, int, ExprList
72250 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
72260 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
72270 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 53 74  3DeleteTriggerSt
72280 65 70 28 73 71 6c 69 74 65 33 2a 2c 20 54 72 69  ep(sqlite3*, Tri
72290 67 67 65 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49  ggerStep*);.SQLI
722a0 54 45 5f 50 52 49 56 41 54 45 20 20 20 54 72 69  TE_PRIVATE   Tri
722b0 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65  ggerStep *sqlite
722c0 33 54 72 69 67 67 65 72 53 65 6c 65 63 74 53 74  3TriggerSelectSt
722d0 65 70 28 73 71 6c 69 74 65 33 2a 2c 53 65 6c 65  ep(sqlite3*,Sele
722e0 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ct*);.SQLITE_PRI
722f0 56 41 54 45 20 20 20 54 72 69 67 67 65 72 53 74  VATE   TriggerSt
72300 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67  ep *sqlite3Trigg
72310 65 72 49 6e 73 65 72 74 53 74 65 70 28 73 71 6c  erInsertStep(sql
72320 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 49 64  ite3*,Token*, Id
72330 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20 20 20  List*,.         
72340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45                 E
72360 78 70 72 4c 69 73 74 2a 2c 53 65 6c 65 63 74 2a  xprList*,Select*
72370 2c 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ,u8);.SQLITE_PRI
72380 56 41 54 45 20 20 20 54 72 69 67 67 65 72 53 74  VATE   TriggerSt
72390 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67  ep *sqlite3Trigg
723a0 65 72 55 70 64 61 74 65 53 74 65 70 28 73 71 6c  erUpdateStep(sql
723b0 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 45 78 70  ite3*,Token*,Exp
723c0 72 4c 69 73 74 2a 2c 20 45 78 70 72 2a 2c 20 75  rList*, Expr*, u
723d0 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  8);.SQLITE_PRIVA
723e0 54 45 20 20 20 54 72 69 67 67 65 72 53 74 65 70  TE   TriggerStep
723f0 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65 72   *sqlite3Trigger
72400 44 65 6c 65 74 65 53 74 65 70 28 73 71 6c 69 74  DeleteStep(sqlit
72410 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 45 78 70 72  e3*,Token*, Expr
72420 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
72430 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
72440 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 28 73  3DeleteTrigger(s
72450 71 6c 69 74 65 33 2a 2c 20 54 72 69 67 67 65 72  qlite3*, Trigger
72460 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
72470 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
72480 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65  3UnlinkAndDelete
72490 54 72 69 67 67 65 72 28 73 71 6c 69 74 65 33 2a  Trigger(sqlite3*
724a0 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ,int,const char*
724b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
724c0 45 20 20 20 75 33 32 20 73 71 6c 69 74 65 33 54  E   u32 sqlite3T
724d0 72 69 67 67 65 72 4f 6c 64 6d 61 73 6b 28 50 61  riggerOldmask(Pa
724e0 72 73 65 2a 2c 54 72 69 67 67 65 72 2a 2c 45 78  rse*,Trigger*,Ex
724f0 70 72 4c 69 73 74 2a 2c 54 61 62 6c 65 2a 2c 69  prList*,Table*,i
72500 6e 74 29 3b 0a 23 20 64 65 66 69 6e 65 20 73 71  nt);.# define sq
72510 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76  lite3ParseToplev
72520 65 6c 28 70 29 20 28 28 70 29 2d 3e 70 54 6f 70  el(p) ((p)->pTop
72530 6c 65 76 65 6c 20 3f 20 28 70 29 2d 3e 70 54 6f  level ? (p)->pTo
72540 70 6c 65 76 65 6c 20 3a 20 28 70 29 29 0a 23 65  plevel : (p)).#e
72550 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  lse.# define sql
72560 69 74 65 33 54 72 69 67 67 65 72 73 45 78 69 73  ite3TriggersExis
72570 74 28 42 2c 43 2c 44 2c 45 2c 46 29 20 30 0a 23  t(B,C,D,E,F) 0.#
72580 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44   define sqlite3D
72590 65 6c 65 74 65 54 72 69 67 67 65 72 28 41 2c 42  eleteTrigger(A,B
725a0 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  ).# define sqlit
725b0 65 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72  e3DropTriggerPtr
725c0 28 41 2c 42 29 0a 23 20 64 65 66 69 6e 65 20 73  (A,B).# define s
725d0 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44  qlite3UnlinkAndD
725e0 65 6c 65 74 65 54 72 69 67 67 65 72 28 41 2c 42  eleteTrigger(A,B
725f0 2c 43 29 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ,C).# define sql
72600 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67  ite3CodeRowTrigg
72610 65 72 28 41 2c 42 2c 43 2c 44 2c 45 2c 46 2c 47  er(A,B,C,D,E,F,G
72620 2c 48 2c 49 29 0a 23 20 64 65 66 69 6e 65 20 73  ,H,I).# define s
72630 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69  qlite3CodeRowTri
72640 67 67 65 72 44 69 72 65 63 74 28 41 2c 42 2c 43  ggerDirect(A,B,C
72650 2c 44 2c 45 2c 46 29 0a 23 20 64 65 66 69 6e 65  ,D,E,F).# define
72660 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c   sqlite3TriggerL
72670 69 73 74 28 58 2c 20 59 29 20 30 0a 23 20 64 65  ist(X, Y) 0.# de
72680 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73  fine sqlite3Pars
72690 65 54 6f 70 6c 65 76 65 6c 28 70 29 20 70 0a 23  eToplevel(p) p.#
726a0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54   define sqlite3T
726b0 72 69 67 67 65 72 4f 6c 64 6d 61 73 6b 28 41 2c  riggerOldmask(A,
726c0 42 2c 43 2c 44 2c 45 29 20 30 0a 23 65 6e 64 69  B,C,D,E) 0.#endi
726d0 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  f..SQLITE_PRIVAT
726e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 69  E int sqlite3Joi
726f0 6e 54 79 70 65 28 50 61 72 73 65 2a 2c 20 54 6f  nType(Parse*, To
72700 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54 6f  ken*, Token*, To
72710 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
72720 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
72730 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b  e3CreateForeignK
72740 65 79 28 50 61 72 73 65 2a 2c 20 45 78 70 72 4c  ey(Parse*, ExprL
72750 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 45 78  ist*, Token*, Ex
72760 70 72 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53  prList*, int);.S
72770 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
72780 69 64 20 73 71 6c 69 74 65 33 44 65 66 65 72 46  id sqlite3DeferF
72790 6f 72 65 69 67 6e 4b 65 79 28 50 61 72 73 65 2a  oreignKey(Parse*
727a0 2c 20 69 6e 74 29 3b 0a 23 69 66 6e 64 65 66 20  , int);.#ifndef 
727b0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
727c0 4f 52 49 5a 41 54 49 4f 4e 0a 53 51 4c 49 54 45  ORIZATION.SQLITE
727d0 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
727e0 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 28  sqlite3AuthRead(
727f0 50 61 72 73 65 2a 2c 45 78 70 72 2a 2c 53 63 68  Parse*,Expr*,Sch
72800 65 6d 61 2a 2c 53 72 63 4c 69 73 74 2a 29 3b 0a  ema*,SrcList*);.
72810 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
72820 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74 68   int sqlite3Auth
72830 43 68 65 63 6b 28 50 61 72 73 65 2a 2c 69 6e 74  Check(Parse*,int
72840 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63  , const char*, c
72850 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73  onst char*, cons
72860 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45  t char*);.SQLITE
72870 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20  _PRIVATE   void 
72880 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65  sqlite3AuthConte
72890 78 74 50 75 73 68 28 50 61 72 73 65 2a 2c 20 41  xtPush(Parse*, A
728a0 75 74 68 43 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e  uthContext*, con
728b0 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54  st char*);.SQLIT
728c0 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
728d0 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74   sqlite3AuthCont
728e0 65 78 74 50 6f 70 28 41 75 74 68 43 6f 6e 74 65  extPop(AuthConte
728f0 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  xt*);.SQLITE_PRI
72900 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
72910 65 33 41 75 74 68 52 65 61 64 43 6f 6c 28 50 61  e3AuthReadCol(Pa
72920 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  rse*, const char
72930 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a   *, const char *
72940 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 20  , int);.#else.# 
72950 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 41 75  define sqlite3Au
72960 74 68 52 65 61 64 28 61 2c 62 2c 63 2c 64 29 0a  thRead(a,b,c,d).
72970 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
72980 41 75 74 68 43 68 65 63 6b 28 61 2c 62 2c 63 2c  AuthCheck(a,b,c,
72990 64 2c 65 29 20 20 20 20 53 51 4c 49 54 45 5f 4f  d,e)    SQLITE_O
729a0 4b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  K.# define sqlit
729b0 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73  e3AuthContextPus
729c0 68 28 61 2c 62 2c 63 29 0a 23 20 64 65 66 69 6e  h(a,b,c).# defin
729d0 65 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e  e sqlite3AuthCon
729e0 74 65 78 74 50 6f 70 28 61 29 20 20 28 28 76 6f  textPop(a)  ((vo
729f0 69 64 29 28 61 29 29 0a 23 65 6e 64 69 66 0a 53  id)(a)).#endif.S
72a00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
72a10 69 64 20 73 71 6c 69 74 65 33 41 74 74 61 63 68  id sqlite3Attach
72a20 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20  (Parse*, Expr*, 
72a30 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53  Expr*, Expr*);.S
72a40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
72a50 69 64 20 73 71 6c 69 74 65 33 44 65 74 61 63 68  id sqlite3Detach
72a60 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b  (Parse*, Expr*);
72a70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
72a80 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
72a90 46 61 63 74 6f 72 79 28 63 6f 6e 73 74 20 73 71  Factory(const sq
72aa0 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
72ab0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
72ac0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
72ad0 20 20 20 20 20 20 20 20 20 69 6e 74 20 6f 6d 69           int omi
72ae0 74 4a 6f 75 72 6e 61 6c 2c 20 69 6e 74 20 6e 43  tJournal, int nC
72af0 61 63 68 65 2c 20 69 6e 74 20 66 6c 61 67 73 2c  ache, int flags,
72b00 20 42 74 72 65 65 20 2a 2a 70 70 42 74 72 65 65   Btree **ppBtree
72b10 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
72b20 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78  E int sqlite3Fix
72b30 49 6e 69 74 28 44 62 46 69 78 65 72 2a 2c 20 50  Init(DbFixer*, P
72b40 61 72 73 65 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73  arse*, int, cons
72b50 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 54  t char*, const T
72b60 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
72b70 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
72b80 65 33 46 69 78 53 72 63 4c 69 73 74 28 44 62 46  e3FixSrcList(DbF
72b90 69 78 65 72 2a 2c 20 53 72 63 4c 69 73 74 2a 29  ixer*, SrcList*)
72ba0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
72bb0 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53   int sqlite3FixS
72bc0 65 6c 65 63 74 28 44 62 46 69 78 65 72 2a 2c 20  elect(DbFixer*, 
72bd0 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45  Select*);.SQLITE
72be0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
72bf0 69 74 65 33 46 69 78 45 78 70 72 28 44 62 46 69  ite3FixExpr(DbFi
72c00 78 65 72 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  xer*, Expr*);.SQ
72c10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
72c20 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 4c   sqlite3FixExprL
72c30 69 73 74 28 44 62 46 69 78 65 72 2a 2c 20 45 78  ist(DbFixer*, Ex
72c40 70 72 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  prList*);.SQLITE
72c50 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
72c60 69 74 65 33 46 69 78 54 72 69 67 67 65 72 53 74  ite3FixTriggerSt
72c70 65 70 28 44 62 46 69 78 65 72 2a 2c 20 54 72 69  ep(DbFixer*, Tri
72c80 67 67 65 72 53 74 65 70 2a 29 3b 0a 53 51 4c 49  ggerStep*);.SQLI
72c90 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
72ca0 71 6c 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74  qlite3AtoF(const
72cb0 20 63 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65   char *z, double
72cc0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
72cd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65  TE int sqlite3Ge
72ce0 74 49 6e 74 33 32 28 63 6f 6e 73 74 20 63 68 61  tInt32(const cha
72cf0 72 20 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49  r *, int*);.SQLI
72d00 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
72d10 71 6c 69 74 65 33 46 69 74 73 49 6e 36 34 42 69  qlite3FitsIn64Bi
72d20 74 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  ts(const char *,
72d30 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
72d40 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
72d50 33 55 74 66 31 36 42 79 74 65 4c 65 6e 28 63 6f  3Utf16ByteLen(co
72d60 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c  nst void *pData,
72d70 20 69 6e 74 20 6e 43 68 61 72 29 3b 0a 53 51 4c   int nChar);.SQL
72d80 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
72d90 73 71 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c  sqlite3Utf8CharL
72da0 65 6e 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  en(const char *p
72db0 44 61 74 61 2c 20 69 6e 74 20 6e 42 79 74 65 29  Data, int nByte)
72dc0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
72dd0 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38   int sqlite3Utf8
72de0 52 65 61 64 28 63 6f 6e 73 74 20 75 38 2a 2c 20  Read(const u8*, 
72df0 63 6f 6e 73 74 20 75 38 2a 2a 29 3b 0a 0a 2f 2a  const u8**);../*
72e00 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20  .** Routines to 
72e10 72 65 61 64 20 61 6e 64 20 77 72 69 74 65 20 76  read and write v
72e20 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69  ariable-length i
72e30 6e 74 65 67 65 72 73 2e 20 20 54 68 65 73 65 20  ntegers.  These 
72e40 75 73 65 64 20 74 6f 0a 2a 2a 20 62 65 20 64 65  used to.** be de
72e50 66 69 6e 65 64 20 6c 6f 63 61 6c 6c 79 2c 20 62  fined locally, b
72e60 75 74 20 6e 6f 77 20 77 65 20 75 73 65 20 74 68  ut now we use th
72e70 65 20 76 61 72 69 6e 74 20 72 6f 75 74 69 6e 65  e varint routine
72e80 73 20 69 6e 20 74 68 65 20 75 74 69 6c 2e 63 0a  s in the util.c.
72e90 2a 2a 20 66 69 6c 65 2e 20 20 43 6f 64 65 20 73  ** file.  Code s
72ea0 68 6f 75 6c 64 20 75 73 65 20 74 68 65 20 4d 41  hould use the MA
72eb0 43 52 4f 20 66 6f 72 6d 73 20 62 65 6c 6f 77 2c  CRO forms below,
72ec0 20 61 73 20 74 68 65 20 56 61 72 69 6e 74 33 32   as the Varint32
72ed0 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 61 72 65   versions.** are
72ee0 20 63 6f 64 65 64 20 74 6f 20 61 73 73 75 6d 65   coded to assume
72ef0 20 74 68 65 20 73 69 6e 67 6c 65 20 62 79 74 65   the single byte
72f00 20 63 61 73 65 20 69 73 20 61 6c 72 65 61 64 79   case is already
72f10 20 68 61 6e 64 6c 65 64 20 28 77 68 69 63 68 20   handled (which 
72f20 0a 2a 2a 20 74 68 65 20 4d 41 43 52 4f 20 66 6f  .** the MACRO fo
72f30 72 6d 20 64 6f 65 73 29 2e 0a 2a 2f 0a 53 51 4c  rm does)..*/.SQL
72f40 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
72f50 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74  sqlite3PutVarint
72f60 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c  (unsigned char*,
72f70 20 75 36 34 29 3b 0a 53 51 4c 49 54 45 5f 50 52   u64);.SQLITE_PR
72f80 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
72f90 33 50 75 74 56 61 72 69 6e 74 33 32 28 75 6e 73  3PutVarint32(uns
72fa0 69 67 6e 65 64 20 63 68 61 72 2a 2c 20 75 33 32  igned char*, u32
72fb0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
72fc0 45 20 75 38 20 73 71 6c 69 74 65 33 47 65 74 56  E u8 sqlite3GetV
72fd0 61 72 69 6e 74 28 63 6f 6e 73 74 20 75 6e 73 69  arint(const unsi
72fe0 67 6e 65 64 20 63 68 61 72 20 2a 2c 20 75 36 34  gned char *, u64
72ff0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
73000 41 54 45 20 75 38 20 73 71 6c 69 74 65 33 47 65  ATE u8 sqlite3Ge
73010 74 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74 20  tVarint32(const 
73020 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c  unsigned char *,
73030 20 75 33 32 20 2a 29 3b 0a 53 51 4c 49 54 45 5f   u32 *);.SQLITE_
73040 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
73050 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 75 36 34  te3VarintLen(u64
73060 20 76 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20   v);../*.** The 
73070 68 65 61 64 65 72 20 6f 66 20 61 20 72 65 63 6f  header of a reco
73080 72 64 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61  rd consists of a
73090 20 73 65 71 75 65 6e 63 65 20 76 61 72 69 61 62   sequence variab
730a0 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65  le-length intege
730b0 72 73 2e 0a 2a 2a 20 54 68 65 73 65 20 69 6e 74  rs..** These int
730c0 65 67 65 72 73 20 61 72 65 20 61 6c 6d 6f 73 74  egers are almost
730d0 20 61 6c 77 61 79 73 20 73 6d 61 6c 6c 20 61 6e   always small an
730e0 64 20 61 72 65 20 65 6e 63 6f 64 65 64 20 61 73  d are encoded as
730f0 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 2e 0a   a single byte..
73100 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
73110 20 6d 61 63 72 6f 73 20 74 61 6b 65 20 61 64 76   macros take adv
73120 61 6e 74 61 67 65 20 74 68 69 73 20 66 61 63 74  antage this fact
73130 20 74 6f 20 70 72 6f 76 69 64 65 20 61 20 66 61   to provide a fa
73140 73 74 20 65 6e 63 6f 64 65 0a 2a 2a 20 61 6e 64  st encode.** and
73150 20 64 65 63 6f 64 65 20 6f 66 20 74 68 65 20 69   decode of the i
73160 6e 74 65 67 65 72 73 20 69 6e 20 61 20 72 65 63  ntegers in a rec
73170 6f 72 64 20 68 65 61 64 65 72 2e 20 20 49 74 20  ord header.  It 
73180 69 73 20 66 61 73 74 65 72 20 66 6f 72 20 74 68  is faster for th
73190 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65  e common.** case
731a0 20 77 68 65 72 65 20 74 68 65 20 69 6e 74 65 67   where the integ
731b0 65 72 20 69 73 20 61 20 73 69 6e 67 6c 65 20 62  er is a single b
731c0 79 74 65 2e 20 20 49 74 20 69 73 20 61 20 6c 69  yte.  It is a li
731d0 74 74 6c 65 20 73 6c 6f 77 65 72 20 77 68 65 6e  ttle slower when
731e0 20 74 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20   the.** integer 
731f0 69 73 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62  is two or more b
73200 79 74 65 73 2e 20 20 42 75 74 20 6f 76 65 72 61  ytes.  But overa
73210 6c 6c 20 69 74 20 69 73 20 66 61 73 74 65 72 2e  ll it is faster.
73220 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  .**.** The follo
73230 77 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 73  wing expressions
73240 20 61 72 65 20 65 71 75 69 76 61 6c 65 6e 74 3a   are equivalent:
73250 0a 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73  .**.**     x = s
73260 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33  qlite3GetVarint3
73270 32 28 20 41 2c 20 26 42 20 29 3b 0a 2a 2a 20 20  2( A, &B );.**  
73280 20 20 20 78 20 3d 20 73 71 6c 69 74 65 33 50 75     x = sqlite3Pu
73290 74 56 61 72 69 6e 74 33 32 28 20 41 2c 20 42 20  tVarint32( A, B 
732a0 29 3b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 78 20 3d  );.**.**     x =
732b0 20 67 65 74 56 61 72 69 6e 74 33 32 28 20 41 2c   getVarint32( A,
732c0 20 42 20 29 3b 0a 2a 2a 20 20 20 20 20 78 20 3d   B );.**     x =
732d0 20 70 75 74 56 61 72 69 6e 74 33 32 28 20 41 2c   putVarint32( A,
732e0 20 42 20 29 3b 0a 2a 2a 0a 2a 2f 0a 23 64 65 66   B );.**.*/.#def
732f0 69 6e 65 20 67 65 74 56 61 72 69 6e 74 33 32 28  ine getVarint32(
73300 41 2c 42 29 20 20 28 75 38 29 28 28 2a 28 41 29  A,B)  (u8)((*(A)
73310 3c 28 75 38 29 30 78 38 30 29 20 3f 20 28 28 42  <(u8)0x80) ? ((B
73320 29 20 3d 20 28 75 33 32 29 2a 28 41 29 29 2c 31  ) = (u32)*(A)),1
73330 20 3a 20 73 71 6c 69 74 65 33 47 65 74 56 61 72   : sqlite3GetVar
73340 69 6e 74 33 32 28 28 41 29 2c 20 28 75 33 32 20  int32((A), (u32 
73350 2a 29 26 28 42 29 29 29 0a 23 64 65 66 69 6e 65  *)&(B))).#define
73360 20 70 75 74 56 61 72 69 6e 74 33 32 28 41 2c 42   putVarint32(A,B
73370 29 20 20 28 75 38 29 28 28 28 75 33 32 29 28 42  )  (u8)(((u32)(B
73380 29 3c 28 75 33 32 29 30 78 38 30 29 20 3f 20 28  )<(u32)0x80) ? (
73390 2a 28 41 29 20 3d 20 28 75 6e 73 69 67 6e 65 64  *(A) = (unsigned
733a0 20 63 68 61 72 29 28 42 29 29 2c 31 20 3a 20 73   char)(B)),1 : s
733b0 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 33  qlite3PutVarint3
733c0 32 28 28 41 29 2c 20 28 42 29 29 29 0a 23 64 65  2((A), (B))).#de
733d0 66 69 6e 65 20 67 65 74 56 61 72 69 6e 74 20 20  fine getVarint  
733e0 20 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69    sqlite3GetVari
733f0 6e 74 0a 23 64 65 66 69 6e 65 20 70 75 74 56 61  nt.#define putVa
73400 72 69 6e 74 20 20 20 20 73 71 6c 69 74 65 33 50  rint    sqlite3P
73410 75 74 56 61 72 69 6e 74 0a 0a 0a 53 51 4c 49 54  utVarint...SQLIT
73420 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
73430 63 68 61 72 20 2a 73 71 6c 69 74 65 33 49 6e 64  char *sqlite3Ind
73440 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 56 64  exAffinityStr(Vd
73450 62 65 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b 0a  be *, Index *);.
73460 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
73470 6f 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65  oid sqlite3Table
73480 41 66 66 69 6e 69 74 79 53 74 72 28 56 64 62 65  AffinityStr(Vdbe
73490 20 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a 53 51   *, Table *);.SQ
734a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
734b0 72 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65  r sqlite3Compare
734c0 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70  Affinity(Expr *p
734d0 45 78 70 72 2c 20 63 68 61 72 20 61 66 66 32 29  Expr, char aff2)
734e0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
734f0 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64 65   int sqlite3Inde
73500 78 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70 72  xAffinityOk(Expr
73510 20 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69 64   *pExpr, char id
73520 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a 53 51 4c  x_affinity);.SQL
73530 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
73540 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69   sqlite3ExprAffi
73550 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70 72  nity(Expr *pExpr
73560 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
73570 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f  E int sqlite3Ato
73580 69 36 34 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  i64(const char*,
73590 20 69 36 34 2a 29 3b 0a 53 51 4c 49 54 45 5f 50   i64*);.SQLITE_P
735a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
735b0 74 65 33 45 72 72 6f 72 28 73 71 6c 69 74 65 33  te3Error(sqlite3
735c0 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68  *, int, const ch
735d0 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45  ar*,...);.SQLITE
735e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
735f0 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28  qlite3HexToBlob(
73600 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20  sqlite3*, const 
73610 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 3b  char *z, int n);
73620 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73630 69 6e 74 20 73 71 6c 69 74 65 33 54 77 6f 50 61  int sqlite3TwoPa
73640 72 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c 20  rtName(Parse *, 
73650 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20 2a  Token *, Token *
73660 2c 20 54 6f 6b 65 6e 20 2a 2a 29 3b 0a 53 51 4c  , Token **);.SQL
73670 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
73680 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 45  t char *sqlite3E
73690 72 72 53 74 72 28 69 6e 74 29 3b 0a 53 51 4c 49  rrStr(int);.SQLI
736a0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
736b0 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
736c0 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 3b  (Parse *pParse);
736d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
736e0 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33  CollSeq *sqlite3
736f0 46 69 6e 64 43 6f 6c 6c 53 65 71 28 73 71 6c 69  FindCollSeq(sqli
73700 74 65 33 2a 2c 75 38 20 65 6e 63 2c 20 63 6f 6e  te3*,u8 enc, con
73710 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53  st char*,int);.S
73720 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f  QLITE_PRIVATE Co
73730 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 4c 6f  llSeq *sqlite3Lo
73740 63 61 74 65 43 6f 6c 6c 53 65 71 28 50 61 72 73  cateCollSeq(Pars
73750 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74  e *pParse, const
73760 20 63 68 61 72 2a 7a 4e 61 6d 65 29 3b 0a 53 51   char*zName);.SQ
73770 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c  LITE_PRIVATE Col
73780 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 45 78 70  lSeq *sqlite3Exp
73790 72 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a  rCollSeq(Parse *
737a0 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45  pParse, Expr *pE
737b0 78 70 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  xpr);.SQLITE_PRI
737c0 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74  VATE Expr *sqlit
737d0 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 50 61  e3ExprSetColl(Pa
737e0 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70  rse *pParse, Exp
737f0 72 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a 53  r *, Token *);.S
73800 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
73810 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b 43 6f  t sqlite3CheckCo
73820 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 2c 20 43  llSeq(Parse *, C
73830 6f 6c 6c 53 65 71 20 2a 29 3b 0a 53 51 4c 49 54  ollSeq *);.SQLIT
73840 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
73850 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74  lite3CheckObject
73860 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c 20 63 6f  Name(Parse *, co
73870 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c  nst char *);.SQL
73880 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
73890 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
738a0 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a  hanges(sqlite3 *
738b0 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f  , int);..SQLITE_
738c0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 76 6f  PRIVATE const vo
738d0 69 64 20 2a 73 71 6c 69 74 65 33 56 61 6c 75 65  id *sqlite3Value
738e0 54 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c  Text(sqlite3_val
738f0 75 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49 54 45  ue*, u8);.SQLITE
73900 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
73910 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 73  ite3ValueBytes(s
73920 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 75  qlite3_value*, u
73930 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  8);.SQLITE_PRIVA
73940 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
73950 61 6c 75 65 53 65 74 53 74 72 28 73 71 6c 69 74  alueSetStr(sqlit
73960 65 33 5f 76 61 6c 75 65 2a 2c 20 69 6e 74 2c 20  e3_value*, int, 
73970 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2c 75 38 2c  const void *,u8,
73980 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
73990 20 20 20 20 20 20 20 20 20 20 76 6f 69 64 28 2a            void(*
739a0 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54  )(void*));.SQLIT
739b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
739c0 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
739d0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
739e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
739f0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 73  sqlite3_value *s
73a00 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 73  qlite3ValueNew(s
73a10 71 6c 69 74 65 33 20 2a 29 3b 0a 53 51 4c 49 54  qlite3 *);.SQLIT
73a20 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
73a30 73 71 6c 69 74 65 33 55 74 66 31 36 74 6f 38 28  sqlite3Utf16to8(
73a40 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e 73 74  sqlite3 *, const
73a50 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 23 69   void*, int);.#i
73a60 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
73a70 4c 45 5f 53 54 41 54 32 0a 53 51 4c 49 54 45 5f  LE_STAT2.SQLITE_
73a80 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71  PRIVATE char *sq
73a90 6c 69 74 65 33 55 74 66 38 74 6f 31 36 28 73 71  lite3Utf8to16(sq
73aa0 6c 69 74 65 33 20 2a 2c 20 75 38 2c 20 63 68 61  lite3 *, u8, cha
73ab0 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 20 2a 29  r *, int, int *)
73ac0 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f  ;.#endif.SQLITE_
73ad0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
73ae0 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72  te3ValueFromExpr
73af0 28 73 71 6c 69 74 65 33 20 2a 2c 20 45 78 70 72  (sqlite3 *, Expr
73b00 20 2a 2c 20 75 38 2c 20 75 38 2c 20 73 71 6c 69   *, u8, u8, sqli
73b10 74 65 33 5f 76 61 6c 75 65 20 2a 2a 29 3b 0a 53  te3_value **);.S
73b20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
73b30 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41  id sqlite3ValueA
73b40 70 70 6c 79 41 66 66 69 6e 69 74 79 28 73 71 6c  pplyAffinity(sql
73b50 69 74 65 33 5f 76 61 6c 75 65 20 2a 2c 20 75 38  ite3_value *, u8
73b60 2c 20 75 38 29 3b 0a 23 69 66 6e 64 65 66 20 53  , u8);.#ifndef S
73b70 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49  QLITE_AMALGAMATI
73b80 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ON.SQLITE_PRIVAT
73b90 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  E const unsigned
73ba0 20 63 68 61 72 20 73 71 6c 69 74 65 33 55 70 70   char sqlite3Upp
73bb0 65 72 54 6f 4c 6f 77 65 72 5b 5d 3b 0a 53 51 4c  erToLower[];.SQL
73bc0 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
73bd0 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
73be0 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70 5b  sqlite3CtypeMap[
73bf0 5d 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ];.SQLITE_PRIVAT
73c00 45 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72  E SQLITE_WSD str
73c10 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66 69  uct Sqlite3Confi
73c20 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67 3b  g sqlite3Config;
73c30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73c40 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 44  SQLITE_WSD FuncD
73c50 65 66 48 61 73 68 20 73 71 6c 69 74 65 33 47 6c  efHash sqlite3Gl
73c60 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 3b 0a 53  obalFunctions;.S
73c70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
73c80 74 20 73 71 6c 69 74 65 33 50 65 6e 64 69 6e 67  t sqlite3Pending
73c90 42 79 74 65 3b 0a 23 65 6e 64 69 66 0a 53 51 4c  Byte;.#endif.SQL
73ca0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
73cb0 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61 67 65   sqlite3RootPage
73cc0 4d 6f 76 65 64 28 44 62 2a 2c 20 69 6e 74 2c 20  Moved(Db*, int, 
73cd0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
73ce0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
73cf0 33 52 65 69 6e 64 65 78 28 50 61 72 73 65 2a 2c  3Reindex(Parse*,
73d00 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29   Token*, Token*)
73d10 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73d20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74   void sqlite3Alt
73d30 65 72 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69  erFunctions(sqli
73d40 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  te3*);.SQLITE_PR
73d50 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
73d60 65 33 41 6c 74 65 72 52 65 6e 61 6d 65 54 61 62  e3AlterRenameTab
73d70 6c 65 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69  le(Parse*, SrcLi
73d80 73 74 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  st*, Token*);.SQ
73d90 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
73da0 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e   sqlite3GetToken
73db0 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20  (const unsigned 
73dc0 63 68 61 72 20 2a 2c 20 69 6e 74 20 2a 29 3b 0a  char *, int *);.
73dd0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
73de0 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73 74 65  oid sqlite3Neste
73df0 64 50 61 72 73 65 28 50 61 72 73 65 2a 2c 20 63  dParse(Parse*, c
73e00 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29  onst char*, ...)
73e10 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73e20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
73e30 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74 65  irePreparedState
73e40 6d 65 6e 74 73 28 73 71 6c 69 74 65 33 2a 29 3b  ments(sqlite3*);
73e50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
73e60 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 64 65 53  int sqlite3CodeS
73e70 75 62 73 65 6c 65 63 74 28 50 61 72 73 65 20 2a  ubselect(Parse *
73e80 2c 20 45 78 70 72 20 2a 2c 20 69 6e 74 2c 20 69  , Expr *, int, i
73e90 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
73ea0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
73eb0 53 65 6c 65 63 74 50 72 65 70 28 50 61 72 73 65  SelectPrep(Parse
73ec0 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 4e 61 6d 65  *, Select*, Name
73ed0 43 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54  Context*);.SQLIT
73ee0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
73ef0 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
73f00 4e 61 6d 65 73 28 4e 61 6d 65 43 6f 6e 74 65 78  Names(NameContex
73f10 74 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49  t*, Expr*);.SQLI
73f20 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
73f30 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65  sqlite3ResolveSe
73f40 6c 65 63 74 4e 61 6d 65 73 28 50 61 72 73 65 2a  lectNames(Parse*
73f50 2c 20 53 65 6c 65 63 74 2a 2c 20 4e 61 6d 65 43  , Select*, NameC
73f60 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54 45  ontext*);.SQLITE
73f70 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
73f80 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65 72  ite3ResolveOrder
73f90 47 72 6f 75 70 42 79 28 50 61 72 73 65 2a 2c 20  GroupBy(Parse*, 
73fa0 53 65 6c 65 63 74 2a 2c 20 45 78 70 72 4c 69 73  Select*, ExprLis
73fb0 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29  t*, const char*)
73fc0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
73fd0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6c   void sqlite3Col
73fe0 75 6d 6e 44 65 66 61 75 6c 74 28 56 64 62 65 20  umnDefault(Vdbe 
73ff0 2a 2c 20 54 61 62 6c 65 20 2a 2c 20 69 6e 74 2c  *, Table *, int,
74000 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
74010 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
74020 65 33 41 6c 74 65 72 46 69 6e 69 73 68 41 64 64  e3AlterFinishAdd
74030 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a 2c 20  Column(Parse *, 
74040 54 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45  Token *);.SQLITE
74050 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
74060 6c 69 74 65 33 41 6c 74 65 72 42 65 67 69 6e 41  lite3AlterBeginA
74070 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a  ddColumn(Parse *
74080 2c 20 53 72 63 4c 69 73 74 20 2a 29 3b 0a 53 51  , SrcList *);.SQ
74090 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f 6c  LITE_PRIVATE Col
740a0 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 47 65 74  lSeq *sqlite3Get
740b0 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 2a  CollSeq(sqlite3*
740c0 2c 20 75 38 2c 20 43 6f 6c 6c 53 65 71 20 2a 2c  , u8, CollSeq *,
740d0 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53   const char*);.S
740e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
740f0 61 72 20 73 71 6c 69 74 65 33 41 66 66 69 6e 69  ar sqlite3Affini
74100 74 79 54 79 70 65 28 63 6f 6e 73 74 20 63 68 61  tyType(const cha
74110 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
74120 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
74130 41 6e 61 6c 79 7a 65 28 50 61 72 73 65 2a 2c 20  Analyze(Parse*, 
74140 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b  Token*, Token*);
74150 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
74160 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b  int sqlite3Invok
74170 65 42 75 73 79 48 61 6e 64 6c 65 72 28 42 75 73  eBusyHandler(Bus
74180 79 48 61 6e 64 6c 65 72 2a 29 3b 0a 53 51 4c 49  yHandler*);.SQLI
74190 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
741a0 71 6c 69 74 65 33 46 69 6e 64 44 62 28 73 71 6c  qlite3FindDb(sql
741b0 69 74 65 33 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  ite3*, Token*);.
741c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
741d0 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62  nt sqlite3FindDb
741e0 4e 61 6d 65 28 73 71 6c 69 74 65 33 20 2a 2c 20  Name(sqlite3 *, 
741f0 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53  const char *);.S
74200 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
74210 74 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73 69  t sqlite3Analysi
74220 73 4c 6f 61 64 28 73 71 6c 69 74 65 33 2a 2c 69  sLoad(sqlite3*,i
74230 6e 74 20 69 44 42 29 3b 0a 53 51 4c 49 54 45 5f  nt iDB);.SQLITE_
74240 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
74250 69 74 65 33 44 65 6c 65 74 65 49 6e 64 65 78 53  ite3DeleteIndexS
74260 61 6d 70 6c 65 73 28 49 6e 64 65 78 2a 29 3b 0a  amples(Index*);.
74270 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
74280 6f 69 64 20 73 71 6c 69 74 65 33 44 65 66 61 75  oid sqlite3Defau
74290 6c 74 52 6f 77 45 73 74 28 49 6e 64 65 78 2a 29  ltRowEst(Index*)
742a0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
742b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67   void sqlite3Reg
742c0 69 73 74 65 72 4c 69 6b 65 46 75 6e 63 74 69 6f  isterLikeFunctio
742d0 6e 73 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e 74  ns(sqlite3*, int
742e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
742f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4c  E int sqlite3IsL
74300 69 6b 65 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69  ikeFunction(sqli
74310 74 65 33 2a 2c 45 78 70 72 2a 2c 69 6e 74 2a 2c  te3*,Expr*,int*,
74320 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  char*);.SQLITE_P
74330 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
74340 74 65 33 4d 69 6e 69 6d 75 6d 46 69 6c 65 46 6f  te3MinimumFileFo
74350 72 6d 61 74 28 50 61 72 73 65 2a 2c 20 69 6e 74  rmat(Parse*, int
74360 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
74370 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
74380 74 65 33 53 63 68 65 6d 61 46 72 65 65 28 76 6f  te3SchemaFree(vo
74390 69 64 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  id *);.SQLITE_PR
743a0 49 56 41 54 45 20 53 63 68 65 6d 61 20 2a 73 71  IVATE Schema *sq
743b0 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74 28 73  lite3SchemaGet(s
743c0 71 6c 69 74 65 33 20 2a 2c 20 42 74 72 65 65 20  qlite3 *, Btree 
743d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
743e0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 63  TE int sqlite3Sc
743f0 68 65 6d 61 54 6f 49 6e 64 65 78 28 73 71 6c 69  hemaToIndex(sqli
74400 74 65 33 20 2a 64 62 2c 20 53 63 68 65 6d 61 20  te3 *db, Schema 
74410 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
74420 54 45 20 4b 65 79 49 6e 66 6f 20 2a 73 71 6c 69  TE KeyInfo *sqli
74430 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28  te3IndexKeyinfo(
74440 50 61 72 73 65 20 2a 2c 20 49 6e 64 65 78 20 2a  Parse *, Index *
74450 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
74460 45 20 69 6e 74 20 73 71 6c 69 74 65 33 43 72 65  E int sqlite3Cre
74470 61 74 65 46 75 6e 63 28 73 71 6c 69 74 65 33 20  ateFunc(sqlite3 
74480 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  *, const char *,
74490 20 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 64 20   int, int, void 
744a0 2a 2c 20 0a 20 20 76 6f 69 64 20 28 2a 29 28 73  *, .  void (*)(s
744b0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
744c0 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
744d0 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a  e **),.  void (*
744e0 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
744f0 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
74500 61 6c 75 65 20 2a 2a 29 2c 20 76 6f 69 64 20 28  alue **), void (
74510 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  *)(sqlite3_conte
74520 78 74 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xt*));.SQLITE_PR
74530 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
74540 33 41 70 69 45 78 69 74 28 73 71 6c 69 74 65 33  3ApiExit(sqlite3
74550 20 2a 64 62 2c 20 69 6e 74 29 3b 0a 53 51 4c 49   *db, int);.SQLI
74560 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
74570 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61  qlite3OpenTempDa
74580 74 61 62 61 73 65 28 50 61 72 73 65 20 2a 29 3b  tabase(Parse *);
74590 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
745a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72   void sqlite3Str
745b0 41 63 63 75 6d 49 6e 69 74 28 53 74 72 41 63 63  AccumInit(StrAcc
745c0 75 6d 2a 2c 20 63 68 61 72 2a 2c 20 69 6e 74 2c  um*, char*, int,
745d0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
745e0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
745f0 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64  e3StrAccumAppend
74600 28 53 74 72 41 63 63 75 6d 2a 2c 63 6f 6e 73 74  (StrAccum*,const
74610 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a 53 51 4c   char*,int);.SQL
74620 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
74630 20 2a 73 71 6c 69 74 65 33 53 74 72 41 63 63 75   *sqlite3StrAccu
74640 6d 46 69 6e 69 73 68 28 53 74 72 41 63 63 75 6d  mFinish(StrAccum
74650 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
74660 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
74670 74 72 41 63 63 75 6d 52 65 73 65 74 28 53 74 72  trAccumReset(Str
74680 41 63 63 75 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f  Accum*);.SQLITE_
74690 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
746a0 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e  ite3SelectDestIn
746b0 69 74 28 53 65 6c 65 63 74 44 65 73 74 2a 2c 69  it(SelectDest*,i
746c0 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  nt,int);.SQLITE_
746d0 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
746e0 6c 69 74 65 33 43 72 65 61 74 65 43 6f 6c 75 6d  lite3CreateColum
746f0 6e 45 78 70 72 28 73 71 6c 69 74 65 33 20 2a 2c  nExpr(sqlite3 *,
74700 20 53 72 63 4c 69 73 74 20 2a 2c 20 69 6e 74 2c   SrcList *, int,
74710 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50   int);..SQLITE_P
74720 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
74730 74 65 33 42 61 63 6b 75 70 52 65 73 74 61 72 74  te3BackupRestart
74740 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
74750 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
74760 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
74770 61 63 6b 75 70 55 70 64 61 74 65 28 73 71 6c 69  ackupUpdate(sqli
74780 74 65 33 5f 62 61 63 6b 75 70 20 2a 2c 20 50 67  te3_backup *, Pg
74790 6e 6f 2c 20 63 6f 6e 73 74 20 75 38 20 2a 29 3b  no, const u8 *);
747a0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65  ../*.** The inte
747b0 72 66 61 63 65 20 74 6f 20 74 68 65 20 4c 45 4d  rface to the LEM
747c0 4f 4e 2d 67 65 6e 65 72 61 74 65 64 20 70 61 72  ON-generated par
747d0 73 65 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ser.*/.SQLITE_PR
747e0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
747f0 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 28 76  te3ParserAlloc(v
74800 6f 69 64 2a 28 2a 29 28 73 69 7a 65 5f 74 29 29  oid*(*)(size_t))
74810 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74820 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72   void sqlite3Par
74830 73 65 72 46 72 65 65 28 76 6f 69 64 2a 2c 20 76  serFree(void*, v
74840 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  oid(*)(void*));.
74850 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
74860 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65  oid sqlite3Parse
74870 72 28 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 54 6f  r(void*, int, To
74880 6b 65 6e 2c 20 50 61 72 73 65 2a 29 3b 0a 23 69  ken, Parse*);.#i
74890 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53  fdef YYTRACKMAXS
748a0 54 41 43 4b 44 45 50 54 48 0a 53 51 4c 49 54 45  TACKDEPTH.SQLITE
748b0 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
748c0 71 6c 69 74 65 33 50 61 72 73 65 72 53 74 61 63  qlite3ParserStac
748d0 6b 50 65 61 6b 28 76 6f 69 64 2a 29 3b 0a 23 65  kPeak(void*);.#e
748e0 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49  ndif..SQLITE_PRI
748f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
74900 33 41 75 74 6f 4c 6f 61 64 45 78 74 65 6e 73 69  3AutoLoadExtensi
74910 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23  ons(sqlite3*);.#
74920 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
74930 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f  IT_LOAD_EXTENSIO
74940 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  N.SQLITE_PRIVATE
74950 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43     void sqlite3C
74960 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28 73  loseExtensions(s
74970 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a  qlite3*);.#else.
74980 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
74990 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f 6e 73 28  CloseExtensions(
749a0 58 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  X).#endif..#ifnd
749b0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
749c0 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49  HARED_CACHE.SQLI
749d0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
749e0 64 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f  d sqlite3TableLo
749f0 63 6b 28 50 61 72 73 65 20 2a 2c 20 69 6e 74 2c  ck(Parse *, int,
74a00 20 69 6e 74 2c 20 75 38 2c 20 63 6f 6e 73 74 20   int, u8, const 
74a10 63 68 61 72 20 2a 29 3b 0a 23 65 6c 73 65 0a 20  char *);.#else. 
74a20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
74a30 54 61 62 6c 65 4c 6f 63 6b 28 76 2c 77 2c 78 2c  TableLock(v,w,x,
74a40 79 2c 7a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66  y,z).#endif..#if
74a50 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
74a60 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
74a70 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38   int sqlite3Utf8
74a80 54 6f 38 28 75 6e 73 69 67 6e 65 64 20 63 68 61  To8(unsigned cha
74a90 72 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66  r*);.#endif..#if
74aa0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
74ab0 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 23 20 20  VIRTUALTABLE.#  
74ac0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74  define sqlite3Vt
74ad0 61 62 43 6c 65 61 72 28 59 29 0a 23 20 20 64 65  abClear(Y).#  de
74ae0 66 69 6e 65 20 73 71 6c 69 74 65 33 56 74 61 62  fine sqlite3Vtab
74af0 53 79 6e 63 28 58 2c 59 29 20 53 51 4c 49 54 45  Sync(X,Y) SQLITE
74b00 5f 4f 4b 0a 23 20 20 64 65 66 69 6e 65 20 73 71  _OK.#  define sq
74b10 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61 63  lite3VtabRollbac
74b20 6b 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73  k(X).#  define s
74b30 71 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74  qlite3VtabCommit
74b40 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71  (X).#  define sq
74b50 6c 69 74 65 33 56 74 61 62 49 6e 53 79 6e 63 28  lite3VtabInSync(
74b60 64 62 29 20 30 0a 23 20 20 64 65 66 69 6e 65 20  db) 0.#  define 
74b70 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28  sqlite3VtabLock(
74b80 58 29 20 0a 23 20 20 64 65 66 69 6e 65 20 73 71  X) .#  define sq
74b90 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28  lite3VtabUnlock(
74ba0 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c  X).#  define sql
74bb0 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69  ite3VtabUnlockLi
74bc0 73 74 28 58 29 0a 23 65 6c 73 65 0a 53 51 4c 49  st(X).#else.SQLI
74bd0 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 76 6f  TE_PRIVATE    vo
74be0 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c  id sqlite3VtabCl
74bf0 65 61 72 28 54 61 62 6c 65 2a 29 3b 0a 53 51 4c  ear(Table*);.SQL
74c00 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69  ITE_PRIVATE    i
74c10 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 53 79  nt sqlite3VtabSy
74c20 6e 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  nc(sqlite3 *db, 
74c30 63 68 61 72 20 2a 2a 29 3b 0a 53 51 4c 49 54 45  char **);.SQLITE
74c40 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e 74 20  _PRIVATE    int 
74c50 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62  sqlite3VtabRollb
74c60 61 63 6b 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ack(sqlite3 *db)
74c70 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74c80 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 56      int sqlite3V
74c90 74 61 62 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65  tabCommit(sqlite
74ca0 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 50  3 *db);.SQLITE_P
74cb0 52 49 56 41 54 45 20 20 20 20 76 6f 69 64 20 73  RIVATE    void s
74cc0 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 56  qlite3VtabLock(V
74cd0 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45  Table *);.SQLITE
74ce0 5f 50 52 49 56 41 54 45 20 20 20 20 76 6f 69 64  _PRIVATE    void
74cf0 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f   sqlite3VtabUnlo
74d00 63 6b 28 56 54 61 62 6c 65 20 2a 29 3b 0a 53 51  ck(VTable *);.SQ
74d10 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 20  LITE_PRIVATE    
74d20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
74d30 55 6e 6c 6f 63 6b 4c 69 73 74 28 73 71 6c 69 74  UnlockList(sqlit
74d40 65 33 2a 29 3b 0a 23 20 20 64 65 66 69 6e 65 20  e3*);.#  define 
74d50 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53 79 6e  sqlite3VtabInSyn
74d60 63 28 64 62 29 20 28 28 64 62 29 2d 3e 6e 56 54  c(db) ((db)->nVT
74d70 72 61 6e 73 3e 30 20 26 26 20 28 64 62 29 2d 3e  rans>0 && (db)->
74d80 61 56 54 72 61 6e 73 3d 3d 30 29 0a 23 65 6e 64  aVTrans==0).#end
74d90 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  if.SQLITE_PRIVAT
74da0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74  E void sqlite3Vt
74db0 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 50  abMakeWritable(P
74dc0 61 72 73 65 2a 2c 54 61 62 6c 65 2a 29 3b 0a 53  arse*,Table*);.S
74dd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
74de0 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 42 65  id sqlite3VtabBe
74df0 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 2a 2c  ginParse(Parse*,
74e00 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c   Token*, Token*,
74e10 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45   Token*);.SQLITE
74e20 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
74e30 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73 68 50  lite3VtabFinishP
74e40 61 72 73 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b  arse(Parse*, Tok
74e50 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
74e60 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
74e70 33 56 74 61 62 41 72 67 49 6e 69 74 28 50 61 72  3VtabArgInit(Par
74e80 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  se*);.SQLITE_PRI
74e90 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
74ea0 33 56 74 61 62 41 72 67 45 78 74 65 6e 64 28 50  3VtabArgExtend(P
74eb0 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  arse*, Token*);.
74ec0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
74ed0 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61  nt sqlite3VtabCa
74ee0 6c 6c 43 72 65 61 74 65 28 73 71 6c 69 74 65 33  llCreate(sqlite3
74ef0 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68  *, int, const ch
74f00 61 72 20 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a  ar *, char **);.
74f10 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
74f20 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61  nt sqlite3VtabCa
74f30 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72 73 65 2a  llConnect(Parse*
74f40 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54  , Table*);.SQLIT
74f50 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
74f60 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44 65 73  lite3VtabCallDes
74f70 74 72 6f 79 28 73 71 6c 69 74 65 33 2a 2c 20 69  troy(sqlite3*, i
74f80 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  nt, const char *
74f90 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
74fa0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61  E int sqlite3Vta
74fb0 62 42 65 67 69 6e 28 73 71 6c 69 74 65 33 20 2a  bBegin(sqlite3 *
74fc0 2c 20 56 54 61 62 6c 65 20 2a 29 3b 0a 53 51 4c  , VTable *);.SQL
74fd0 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63  ITE_PRIVATE Func
74fe0 44 65 66 20 2a 73 71 6c 69 74 65 33 56 74 61 62  Def *sqlite3Vtab
74ff0 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e  OverloadFunction
75000 28 73 71 6c 69 74 65 33 20 2a 2c 46 75 6e 63 44  (sqlite3 *,FuncD
75010 65 66 2a 2c 20 69 6e 74 20 6e 41 72 67 2c 20 45  ef*, int nArg, E
75020 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
75030 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
75040 65 33 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f  e3InvalidFunctio
75050 6e 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  n(sqlite3_contex
75060 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
75070 61 6c 75 65 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f  alue**);.SQLITE_
75080 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
75090 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69  te3TransferBindi
750a0 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ngs(sqlite3_stmt
750b0 20 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74   *, sqlite3_stmt
750c0 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
750d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
750e0 65 70 72 65 70 61 72 65 28 56 64 62 65 2a 29 3b  eprepare(Vdbe*);
750f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
75100 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
75110 4c 69 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28  ListCheckLength(
75120 50 61 72 73 65 2a 2c 20 45 78 70 72 4c 69 73 74  Parse*, ExprList
75130 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  *, const char*);
75140 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
75150 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33  CollSeq *sqlite3
75160 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c  BinaryCompareCol
75170 6c 53 65 71 28 50 61 72 73 65 20 2a 2c 20 45 78  lSeq(Parse *, Ex
75180 70 72 20 2a 2c 20 45 78 70 72 20 2a 29 3b 0a 53  pr *, Expr *);.S
75190 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
751a0 74 20 73 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d  t sqlite3TempInM
751b0 65 6d 6f 72 79 28 63 6f 6e 73 74 20 73 71 6c 69  emory(const sqli
751c0 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  te3*);.SQLITE_PR
751d0 49 56 41 54 45 20 56 54 61 62 6c 65 20 2a 73 71  IVATE VTable *sq
751e0 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 73  lite3GetVTable(s
751f0 71 6c 69 74 65 33 2a 2c 20 54 61 62 6c 65 2a 29  qlite3*, Table*)
75200 3b 0a 0a 2f 2a 20 44 65 63 6c 61 72 61 74 69 6f  ;../* Declaratio
75210 6e 73 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 73  ns for functions
75220 20 69 6e 20 66 6b 65 79 2e 63 2e 20 41 6c 6c 20   in fkey.c. All 
75230 6f 66 20 74 68 65 73 65 20 61 72 65 20 72 65 70  of these are rep
75240 6c 61 63 65 64 20 62 79 0a 2a 2a 20 6e 6f 2d 6f  laced by.** no-o
75250 70 20 6d 61 63 72 6f 73 20 69 66 20 4f 4d 49 54  p macros if OMIT
75260 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 20 69 73 20  _FOREIGN_KEY is 
75270 64 65 66 69 6e 65 64 2e 20 49 6e 20 74 68 69 73  defined. In this
75280 20 63 61 73 65 20 6e 6f 20 66 6f 72 65 69 67 6e   case no foreign
75290 0a 2a 2a 20 6b 65 79 20 66 75 6e 63 74 69 6f 6e  .** key function
752a0 61 6c 69 74 79 20 69 73 20 61 76 61 69 6c 61 62  ality is availab
752b0 6c 65 2e 20 49 66 20 4f 4d 49 54 5f 54 52 49 47  le. If OMIT_TRIG
752c0 47 45 52 20 69 73 20 64 65 66 69 6e 65 64 20 62  GER is defined b
752d0 75 74 0a 2a 2a 20 4f 4d 49 54 5f 46 4f 52 45 49  ut.** OMIT_FOREI
752e0 47 4e 5f 4b 45 59 20 69 73 20 6e 6f 74 2c 20 6f  GN_KEY is not, o
752f0 6e 6c 79 20 73 6f 6d 65 20 6f 66 20 74 68 65 20  nly some of the 
75300 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f  functions are no
75310 2d 6f 70 65 64 2e 20 49 6e 0a 2a 2a 20 74 68 69  -oped. In.** thi
75320 73 20 63 61 73 65 20 66 6f 72 65 69 67 6e 20 6b  s case foreign k
75330 65 79 73 20 61 72 65 20 70 61 72 73 65 64 2c 20  eys are parsed, 
75340 62 75 74 20 6e 6f 20 6f 74 68 65 72 20 66 75 6e  but no other fun
75350 63 74 69 6f 6e 61 6c 69 74 79 20 69 73 20 0a 2a  ctionality is .*
75360 2a 20 70 72 6f 76 69 64 65 64 20 28 65 6e 66 6f  * provided (enfo
75370 72 63 65 6d 65 6e 74 20 6f 66 20 46 4b 20 63 6f  rcement of FK co
75380 6e 73 74 72 61 69 6e 74 73 20 72 65 71 75 69 72  nstraints requir
75390 65 73 20 74 68 65 20 74 72 69 67 67 65 72 73 20  es the triggers 
753a0 73 75 62 2d 73 79 73 74 65 6d 29 2e 0a 2a 2f 0a  sub-system)..*/.
753b0 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
753c0 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e  ITE_OMIT_FOREIGN
753d0 5f 4b 45 59 29 20 26 26 20 21 64 65 66 69 6e 65  _KEY) && !define
753e0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
753f0 49 47 47 45 52 29 0a 53 51 4c 49 54 45 5f 50 52  IGGER).SQLITE_PR
75400 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
75410 69 74 65 33 46 6b 43 68 65 63 6b 28 50 61 72 73  ite3FkCheck(Pars
75420 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c  e*, Table*, int,
75430 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
75440 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
75450 69 74 65 33 46 6b 44 72 6f 70 54 61 62 6c 65 28  ite3FkDropTable(
75460 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 20  Parse*, SrcList 
75470 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49  *, Table*);.SQLI
75480 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
75490 64 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69 6f  d sqlite3FkActio
754a0 6e 73 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65  ns(Parse*, Table
754b0 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69 6e  *, ExprList*, in
754c0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
754d0 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33  TE   int sqlite3
754e0 46 6b 52 65 71 75 69 72 65 64 28 50 61 72 73 65  FkRequired(Parse
754f0 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2a 2c  *, Table*, int*,
75500 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
75510 49 56 41 54 45 20 20 20 75 33 32 20 73 71 6c 69  IVATE   u32 sqli
75520 74 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 50 61 72  te3FkOldmask(Par
75530 73 65 2a 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51  se*, Table*);.SQ
75540 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 46  LITE_PRIVATE   F
75550 4b 65 79 20 2a 73 71 6c 69 74 65 33 46 6b 52 65  Key *sqlite3FkRe
75560 66 65 72 65 6e 63 65 73 28 54 61 62 6c 65 20 2a  ferences(Table *
75570 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69  );.#else.  #defi
75580 6e 65 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69  ne sqlite3FkActi
75590 6f 6e 73 28 61 2c 62 2c 63 2c 64 29 0a 20 20 23  ons(a,b,c,d).  #
755a0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 6b  define sqlite3Fk
755b0 43 68 65 63 6b 28 61 2c 62 2c 63 2c 64 29 0a 20  Check(a,b,c,d). 
755c0 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
755d0 46 6b 44 72 6f 70 54 61 62 6c 65 28 61 2c 62 2c  FkDropTable(a,b,
755e0 63 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71 6c  c).  #define sql
755f0 69 74 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 61 2c  ite3FkOldmask(a,
75600 62 29 20 20 20 20 20 20 30 0a 20 20 23 64 65 66  b)      0.  #def
75610 69 6e 65 20 73 71 6c 69 74 65 33 46 6b 52 65 71  ine sqlite3FkReq
75620 75 69 72 65 64 28 61 2c 62 2c 63 2c 64 29 20 30  uired(a,b,c,d) 0
75630 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
75640 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45  SQLITE_OMIT_FORE
75650 49 47 4e 5f 4b 45 59 0a 53 51 4c 49 54 45 5f 50  IGN_KEY.SQLITE_P
75660 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
75670 6c 69 74 65 33 46 6b 44 65 6c 65 74 65 28 54 61  lite3FkDelete(Ta
75680 62 6c 65 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23  ble*);.#else.  #
75690 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 46 6b  define sqlite3Fk
756a0 44 65 6c 65 74 65 28 61 29 0a 23 65 6e 64 69 66  Delete(a).#endif
756b0 0a 0a 0a 2f 2a 0a 2a 2a 20 41 76 61 69 6c 61 62  .../*.** Availab
756c0 6c 65 20 66 61 75 6c 74 20 69 6e 6a 65 63 74 6f  le fault injecto
756d0 72 73 2e 20 20 53 68 6f 75 6c 64 20 62 65 20 6e  rs.  Should be n
756e0 75 6d 62 65 72 65 64 20 62 65 67 69 6e 6e 69 6e  umbered beginnin
756f0 67 20 77 69 74 68 20 30 2e 0a 2a 2f 0a 23 64 65  g with 0..*/.#de
75700 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 41 55 4c  fine SQLITE_FAUL
75710 54 49 4e 4a 45 43 54 4f 52 5f 4d 41 4c 4c 4f 43  TINJECTOR_MALLOC
75720 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53       0.#define S
75730 51 4c 49 54 45 5f 46 41 55 4c 54 49 4e 4a 45 43  QLITE_FAULTINJEC
75740 54 4f 52 5f 43 4f 55 4e 54 20 20 20 20 20 20 31  TOR_COUNT      1
75750 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65  ../*.** The inte
75760 72 66 61 63 65 20 74 6f 20 74 68 65 20 63 6f 64  rface to the cod
75770 65 20 69 6e 20 66 61 75 6c 74 2e 63 20 75 73 65  e in fault.c use
75780 64 20 66 6f 72 20 69 64 65 6e 74 69 66 79 69 6e  d for identifyin
75790 67 20 22 62 65 6e 69 67 6e 22 0a 2a 2a 20 6d 61  g "benign".** ma
757a0 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 2e 20 54  lloc failures. T
757b0 68 69 73 20 69 73 20 6f 6e 6c 79 20 70 72 65 73  his is only pres
757c0 65 6e 74 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d  ent if SQLITE_OM
757d0 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a  IT_BUILTIN_TEST.
757e0 2a 2a 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65  ** is not define
757f0 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  d..*/.#ifndef SQ
75800 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49  LITE_OMIT_BUILTI
75810 4e 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52  N_TEST.SQLITE_PR
75820 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
75830 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
75840 61 6c 6c 6f 63 28 76 6f 69 64 29 3b 0a 53 51 4c  alloc(void);.SQL
75850 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
75860 69 64 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e  id sqlite3EndBen
75870 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 3b  ignMalloc(void);
75880 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65  .#else.  #define
75890 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e   sqlite3BeginBen
758a0 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 20 20 23 64  ignMalloc().  #d
758b0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 45 6e 64  efine sqlite3End
758c0 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a 23  BenignMalloc().#
758d0 65 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 49  endif..#define I
758e0 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 20 20  N_INDEX_ROWID   
758f0 20 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e          1.#defin
75900 65 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20 20  e IN_INDEX_EPH  
75910 20 20 20 20 20 20 20 20 20 20 20 32 0a 23 64 65             2.#de
75920 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 49 4e  fine IN_INDEX_IN
75930 44 45 58 20 20 20 20 20 20 20 20 20 20 20 33 0a  DEX           3.
75940 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
75950 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e  nt sqlite3FindIn
75960 49 6e 64 65 78 28 50 61 72 73 65 20 2a 2c 20 45  Index(Parse *, E
75970 78 70 72 20 2a 2c 20 69 6e 74 2a 29 3b 0a 0a 23  xpr *, int*);..#
75980 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
75990 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45  BLE_ATOMIC_WRITE
759a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
759b0 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75    int sqlite3Jou
759c0 72 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33  rnalOpen(sqlite3
759d0 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20 63 68  _vfs *, const ch
759e0 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f 66 69  ar *, sqlite3_fi
759f0 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  le *, int, int);
75a00 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
75a10 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75    int sqlite3Jou
75a20 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74 65 33  rnalSize(sqlite3
75a30 5f 76 66 73 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  _vfs *);.SQLITE_
75a40 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
75a50 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61  lite3JournalCrea
75a60 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  te(sqlite3_file 
75a70 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66  *);.#else.  #def
75a80 69 6e 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e  ine sqlite3Journ
75a90 61 6c 53 69 7a 65 28 70 56 66 73 29 20 28 28 70  alSize(pVfs) ((p
75aa0 56 66 73 29 2d 3e 73 7a 4f 73 46 69 6c 65 29 0a  Vfs)->szOsFile).
75ab0 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50  #endif..SQLITE_P
75ac0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
75ad0 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65  te3MemJournalOpe
75ae0 6e 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  n(sqlite3_file *
75af0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
75b00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d  E int sqlite3Mem
75b10 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 76 6f 69 64  JournalSize(void
75b20 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
75b30 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4d  E int sqlite3IsM
75b40 65 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65  emJournal(sqlite
75b50 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 69 66 20  3_file *);..#if 
75b60 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f  SQLITE_MAX_EXPR_
75b70 44 45 50 54 48 3e 30 0a 53 51 4c 49 54 45 5f 50  DEPTH>0.SQLITE_P
75b80 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
75b90 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67  lite3ExprSetHeig
75ba0 68 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ht(Parse *pParse
75bb0 2c 20 45 78 70 72 20 2a 70 29 3b 0a 53 51 4c 49  , Expr *p);.SQLI
75bc0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
75bd0 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78   sqlite3SelectEx
75be0 70 72 48 65 69 67 68 74 28 53 65 6c 65 63 74 20  prHeight(Select 
75bf0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
75c00 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33  TE   int sqlite3
75c10 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28  ExprCheckHeight(
75c20 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 65  Parse*, int);.#e
75c30 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71  lse.  #define sq
75c40 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67  lite3ExprSetHeig
75c50 68 74 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e  ht(x,y).  #defin
75c60 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45  e sqlite3SelectE
75c70 78 70 72 48 65 69 67 68 74 28 78 29 20 30 0a 20  xprHeight(x) 0. 
75c80 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
75c90 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28  ExprCheckHeight(
75ca0 78 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c  x,y).#endif..SQL
75cb0 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20  ITE_PRIVATE u32 
75cc0 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28  sqlite3Get4byte(
75cd0 63 6f 6e 73 74 20 75 38 2a 29 3b 0a 53 51 4c 49  const u8*);.SQLI
75ce0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
75cf0 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28  sqlite3Put4byte(
75d00 75 38 2a 2c 20 75 33 32 29 3b 0a 0a 23 69 66 64  u8*, u32);..#ifd
75d10 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
75d20 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a 53  _UNLOCK_NOTIFY.S
75d30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
75d40 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e  void sqlite3Conn
75d50 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 73 71  ectionBlocked(sq
75d60 6c 69 74 65 33 20 2a 2c 20 73 71 6c 69 74 65 33  lite3 *, sqlite3
75d70 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
75d80 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
75d90 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f  e3ConnectionUnlo
75da0 63 6b 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62  cked(sqlite3 *db
75db0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
75dc0 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
75dd0 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64  ConnectionClosed
75de0 28 73 71 6c 69 74 65 33 20 2a 64 62 29 3b 0a 23  (sqlite3 *db);.#
75df0 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73  else.  #define s
75e00 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e  qlite3Connection
75e10 42 6c 6f 63 6b 65 64 28 78 2c 79 29 0a 20 20 23  Blocked(x,y).  #
75e20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f  define sqlite3Co
75e30 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64  nnectionUnlocked
75e40 28 78 29 0a 20 20 23 64 65 66 69 6e 65 20 73 71  (x).  #define sq
75e50 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43  lite3ConnectionC
75e60 6c 6f 73 65 64 28 78 29 0a 23 65 6e 64 69 66 0a  losed(x).#endif.
75e70 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
75e80 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56  EBUG.SQLITE_PRIV
75e90 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
75ea0 65 33 50 61 72 73 65 72 54 72 61 63 65 28 46 49  e3ParserTrace(FI
75eb0 4c 45 2a 2c 20 63 68 61 72 20 2a 29 3b 0a 23 65  LE*, char *);.#e
75ec0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  ndif../*.** If t
75ed0 68 65 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  he SQLITE_ENABLE
75ee0 20 49 4f 54 52 41 43 45 20 65 78 69 73 74 73 20   IOTRACE exists 
75ef0 74 68 65 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20  then the global 
75f00 76 61 72 69 61 62 6c 65 0a 2a 2a 20 73 71 6c 69  variable.** sqli
75f10 74 65 33 49 6f 54 72 61 63 65 20 69 73 20 61 20  te3IoTrace is a 
75f20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 72 69  pointer to a pri
75f30 6e 74 66 2d 6c 69 6b 65 20 72 6f 75 74 69 6e 65  ntf-like routine
75f40 20 75 73 65 64 20 74 6f 0a 2a 2a 20 70 72 69 6e   used to.** prin
75f50 74 20 49 2f 4f 20 74 72 61 63 69 6e 67 20 6d 65  t I/O tracing me
75f60 73 73 61 67 65 73 2e 20 0a 2a 2f 0a 23 69 66 64  ssages. .*/.#ifd
75f70 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
75f80 5f 49 4f 54 52 41 43 45 0a 23 20 64 65 66 69 6e  _IOTRACE.# defin
75f90 65 20 49 4f 54 52 41 43 45 28 41 29 20 20 69 66  e IOTRACE(A)  if
75fa0 28 20 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65  ( sqlite3IoTrace
75fb0 20 29 7b 20 73 71 6c 69 74 65 33 49 6f 54 72 61   ){ sqlite3IoTra
75fc0 63 65 20 41 3b 20 7d 0a 53 51 4c 49 54 45 5f 50  ce A; }.SQLITE_P
75fd0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
75fe0 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61 63 65  lite3VdbeIOTrace
75ff0 53 71 6c 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49  Sql(Vdbe*);.SQLI
76000 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
76010 28 2a 73 71 6c 69 74 65 33 49 6f 54 72 61 63 65  (*sqlite3IoTrace
76020 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e  )(const char*,..
76030 2e 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  .);.#else.# defi
76040 6e 65 20 49 4f 54 52 41 43 45 28 41 29 0a 23 20  ne IOTRACE(A).# 
76050 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56 64  define sqlite3Vd
76060 62 65 49 4f 54 72 61 63 65 53 71 6c 28 58 29 0a  beIOTraceSql(X).
76070 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 0a 0a  #endif..#endif..
76080 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
76090 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 49 6e 74  End of sqliteInt
760a0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
760b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
760c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
760d0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
760e0 42 65 67 69 6e 20 66 69 6c 65 20 67 6c 6f 62 61  Begin file globa
760f0 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  l.c ************
76100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
76120 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20  /*.** 2008 June 
76130 31 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  13.**.** The aut
76140 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
76150 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
76160 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
76170 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
76180 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
76190 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
761a0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
761b0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
761c0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
761d0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
761e0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
761f0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
76200 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
76210 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
76220 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
76230 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
76240 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
76250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
76290 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
762a0 65 20 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e  e contains defin
762b0 69 74 69 6f 6e 73 20 6f 66 20 67 6c 6f 62 61 6c  itions of global
762c0 20 76 61 72 69 61 62 6c 65 73 20 61 6e 64 20 63   variables and c
762d0 6f 6e 74 61 6e 74 73 2e 0a 2a 2f 0a 0a 0a 2f 2a  ontants..*/.../*
762e0 20 41 6e 20 61 72 72 61 79 20 74 6f 20 6d 61 70   An array to map
762f0 20 61 6c 6c 20 75 70 70 65 72 2d 63 61 73 65 20   all upper-case 
76300 63 68 61 72 61 63 74 65 72 73 20 69 6e 74 6f 20  characters into 
76310 74 68 65 69 72 20 63 6f 72 72 65 73 70 6f 6e 64  their correspond
76320 69 6e 67 0a 2a 2a 20 6c 6f 77 65 72 2d 63 61 73  ing.** lower-cas
76330 65 20 63 68 61 72 61 63 74 65 72 2e 20 0a 2a 2a  e character. .**
76340 0a 2a 2a 20 53 51 4c 69 74 65 20 6f 6e 6c 79 20  .** SQLite only 
76350 63 6f 6e 73 69 64 65 72 73 20 55 53 2d 41 53 43  considers US-ASC
76360 49 49 20 28 6f 72 20 45 42 43 44 49 43 29 20 63  II (or EBCDIC) c
76370 68 61 72 61 63 74 65 72 73 2e 20 20 57 65 20 64  haracters.  We d
76380 6f 20 6e 6f 74 0a 2a 2a 20 68 61 6e 64 6c 65 20  o not.** handle 
76390 63 61 73 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73  case conversions
763a0 20 66 6f 72 20 74 68 65 20 55 54 46 20 63 68 61   for the UTF cha
763b0 72 61 63 74 65 72 20 73 65 74 20 73 69 6e 63 65  racter set since
763c0 20 74 68 65 20 74 61 62 6c 65 73 0a 2a 2a 20 69   the tables.** i
763d0 6e 76 6f 6c 76 65 64 20 61 72 65 20 6e 65 61 72  nvolved are near
763e0 6c 79 20 61 73 20 62 69 67 20 6f 72 20 62 69 67  ly as big or big
763f0 67 65 72 20 74 68 61 6e 20 53 51 4c 69 74 65 20  ger than SQLite 
76400 69 74 73 65 6c 66 2e 0a 2a 2f 0a 53 51 4c 49 54  itself..*/.SQLIT
76410 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
76420 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71  unsigned char sq
76430 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
76440 72 5b 5d 20 3d 20 7b 0a 23 69 66 64 65 66 20 53  r[] = {.#ifdef S
76450 51 4c 49 54 45 5f 41 53 43 49 49 0a 20 20 20 20  QLITE_ASCII.    
76460 20 20 30 2c 20 20 31 2c 20 20 32 2c 20 20 33 2c    0,  1,  2,  3,
76470 20 20 34 2c 20 20 35 2c 20 20 36 2c 20 20 37 2c    4,  5,  6,  7,
76480 20 20 38 2c 20 20 39 2c 20 31 30 2c 20 31 31 2c    8,  9, 10, 11,
76490 20 31 32 2c 20 31 33 2c 20 31 34 2c 20 31 35 2c   12, 13, 14, 15,
764a0 20 31 36 2c 20 31 37 2c 0a 20 20 20 20 20 31 38   16, 17,.     18
764b0 2c 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 32  , 19, 20, 21, 22
764c0 2c 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 36  , 23, 24, 25, 26
764d0 2c 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 30  , 27, 28, 29, 30
764e0 2c 20 33 31 2c 20 33 32 2c 20 33 33 2c 20 33 34  , 31, 32, 33, 34
764f0 2c 20 33 35 2c 0a 20 20 20 20 20 33 36 2c 20 33  , 35,.     36, 3
76500 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 2c 20 34  7, 38, 39, 40, 4
76510 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 2c 20 34  1, 42, 43, 44, 4
76520 35 2c 20 34 36 2c 20 34 37 2c 20 34 38 2c 20 34  5, 46, 47, 48, 4
76530 39 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 20 35  9, 50, 51, 52, 5
76540 33 2c 0a 20 20 20 20 20 35 34 2c 20 35 35 2c 20  3,.     54, 55, 
76550 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c 20  56, 57, 58, 59, 
76560 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c 20  60, 61, 62, 63, 
76570 36 34 2c 20 39 37 2c 20 39 38 2c 20 39 39 2c 31  64, 97, 98, 99,1
76580 30 30 2c 31 30 31 2c 31 30 32 2c 31 30 33 2c 0a  00,101,102,103,.
76590 20 20 20 20 31 30 34 2c 31 30 35 2c 31 30 36 2c      104,105,106,
765a0 31 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c  107,108,109,110,
765b0 31 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c  111,112,113,114,
765c0 31 31 35 2c 31 31 36 2c 31 31 37 2c 31 31 38 2c  115,116,117,118,
765d0 31 31 39 2c 31 32 30 2c 31 32 31 2c 0a 20 20 20  119,120,121,.   
765e0 20 31 32 32 2c 20 39 31 2c 20 39 32 2c 20 39 33   122, 91, 92, 93
765f0 2c 20 39 34 2c 20 39 35 2c 20 39 36 2c 20 39 37  , 94, 95, 96, 97
76600 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31  , 98, 99,100,101
76610 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30 35  ,102,103,104,105
76620 2c 31 30 36 2c 31 30 37 2c 0a 20 20 20 20 31 30  ,106,107,.    10
76630 38 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31  8,109,110,111,11
76640 32 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c 31 31  2,113,114,115,11
76650 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32  6,117,118,119,12
76660 30 2c 31 32 31 2c 31 32 32 2c 31 32 33 2c 31 32  0,121,122,123,12
76670 34 2c 31 32 35 2c 0a 20 20 20 20 31 32 36 2c 31  4,125,.    126,1
76680 32 37 2c 31 32 38 2c 31 32 39 2c 31 33 30 2c 31  27,128,129,130,1
76690 33 31 2c 31 33 32 2c 31 33 33 2c 31 33 34 2c 31  31,132,133,134,1
766a0 33 35 2c 31 33 36 2c 31 33 37 2c 31 33 38 2c 31  35,136,137,138,1
766b0 33 39 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31  39,140,141,142,1
766c0 34 33 2c 0a 20 20 20 20 31 34 34 2c 31 34 35 2c  43,.    144,145,
766d0 31 34 36 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c  146,147,148,149,
766e0 31 35 30 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c  150,151,152,153,
766f0 31 35 34 2c 31 35 35 2c 31 35 36 2c 31 35 37 2c  154,155,156,157,
76700 31 35 38 2c 31 35 39 2c 31 36 30 2c 31 36 31 2c  158,159,160,161,
76710 0a 20 20 20 20 31 36 32 2c 31 36 33 2c 31 36 34  .    162,163,164
76720 2c 31 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38  ,165,166,167,168
76730 2c 31 36 39 2c 31 37 30 2c 31 37 31 2c 31 37 32  ,169,170,171,172
76740 2c 31 37 33 2c 31 37 34 2c 31 37 35 2c 31 37 36  ,173,174,175,176
76750 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c 0a 20 20  ,177,178,179,.  
76760 20 20 31 38 30 2c 31 38 31 2c 31 38 32 2c 31 38    180,181,182,18
76770 33 2c 31 38 34 2c 31 38 35 2c 31 38 36 2c 31 38  3,184,185,186,18
76780 37 2c 31 38 38 2c 31 38 39 2c 31 39 30 2c 31 39  7,188,189,190,19
76790 31 2c 31 39 32 2c 31 39 33 2c 31 39 34 2c 31 39  1,192,193,194,19
767a0 35 2c 31 39 36 2c 31 39 37 2c 0a 20 20 20 20 31  5,196,197,.    1
767b0 39 38 2c 31 39 39 2c 32 30 30 2c 32 30 31 2c 32  98,199,200,201,2
767c0 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32  02,203,204,205,2
767d0 30 36 2c 32 30 37 2c 32 30 38 2c 32 30 39 2c 32  06,207,208,209,2
767e0 31 30 2c 32 31 31 2c 32 31 32 2c 32 31 33 2c 32  10,211,212,213,2
767f0 31 34 2c 32 31 35 2c 0a 20 20 20 20 32 31 36 2c  14,215,.    216,
76800 32 31 37 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c  217,218,219,220,
76810 32 32 31 2c 32 32 32 2c 32 32 33 2c 32 32 34 2c  221,222,223,224,
76820 32 32 35 2c 32 32 36 2c 32 32 37 2c 32 32 38 2c  225,226,227,228,
76830 32 32 39 2c 32 33 30 2c 32 33 31 2c 32 33 32 2c  229,230,231,232,
76840 32 33 33 2c 0a 20 20 20 20 32 33 34 2c 32 33 35  233,.    234,235
76850 2c 32 33 36 2c 32 33 37 2c 32 33 38 2c 32 33 39  ,236,237,238,239
76860 2c 32 34 30 2c 32 34 31 2c 32 34 32 2c 32 34 33  ,240,241,242,243
76870 2c 32 34 34 2c 32 34 35 2c 32 34 36 2c 32 34 37  ,244,245,246,247
76880 2c 32 34 38 2c 32 34 39 2c 32 35 30 2c 32 35 31  ,248,249,250,251
76890 2c 0a 20 20 20 20 32 35 32 2c 32 35 33 2c 32 35  ,.    252,253,25
768a0 34 2c 32 35 35 0a 23 65 6e 64 69 66 0a 23 69 66  4,255.#endif.#if
768b0 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49  def SQLITE_EBCDI
768c0 43 0a 20 20 20 20 20 20 30 2c 20 20 31 2c 20 20  C.      0,  1,  
768d0 32 2c 20 20 33 2c 20 20 34 2c 20 20 35 2c 20 20  2,  3,  4,  5,  
768e0 36 2c 20 20 37 2c 20 20 38 2c 20 20 39 2c 20 31  6,  7,  8,  9, 1
768f0 30 2c 20 31 31 2c 20 31 32 2c 20 31 33 2c 20 31  0, 11, 12, 13, 1
76900 34 2c 20 31 35 2c 20 2f 2a 20 30 78 20 2a 2f 0a  4, 15, /* 0x */.
76910 20 20 20 20 20 31 36 2c 20 31 37 2c 20 31 38 2c       16, 17, 18,
76920 20 31 39 2c 20 32 30 2c 20 32 31 2c 20 32 32 2c   19, 20, 21, 22,
76930 20 32 33 2c 20 32 34 2c 20 32 35 2c 20 32 36 2c   23, 24, 25, 26,
76940 20 32 37 2c 20 32 38 2c 20 32 39 2c 20 33 30 2c   27, 28, 29, 30,
76950 20 33 31 2c 20 2f 2a 20 31 78 20 2a 2f 0a 20 20   31, /* 1x */.  
76960 20 20 20 33 32 2c 20 33 33 2c 20 33 34 2c 20 33     32, 33, 34, 3
76970 35 2c 20 33 36 2c 20 33 37 2c 20 33 38 2c 20 33  5, 36, 37, 38, 3
76980 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 2c 20 34  9, 40, 41, 42, 4
76990 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 2c 20 34  3, 44, 45, 46, 4
769a0 37 2c 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20 20  7, /* 2x */.    
769b0 20 34 38 2c 20 34 39 2c 20 35 30 2c 20 35 31 2c   48, 49, 50, 51,
769c0 20 35 32 2c 20 35 33 2c 20 35 34 2c 20 35 35 2c   52, 53, 54, 55,
769d0 20 35 36 2c 20 35 37 2c 20 35 38 2c 20 35 39 2c   56, 57, 58, 59,
769e0 20 36 30 2c 20 36 31 2c 20 36 32 2c 20 36 33 2c   60, 61, 62, 63,
769f0 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 20 20 36   /* 3x */.     6
76a00 34 2c 20 36 35 2c 20 36 36 2c 20 36 37 2c 20 36  4, 65, 66, 67, 6
76a10 38 2c 20 36 39 2c 20 37 30 2c 20 37 31 2c 20 37  8, 69, 70, 71, 7
76a20 32 2c 20 37 33 2c 20 37 34 2c 20 37 35 2c 20 37  2, 73, 74, 75, 7
76a30 36 2c 20 37 37 2c 20 37 38 2c 20 37 39 2c 20 2f  6, 77, 78, 79, /
76a40 2a 20 34 78 20 2a 2f 0a 20 20 20 20 20 38 30 2c  * 4x */.     80,
76a50 20 38 31 2c 20 38 32 2c 20 38 33 2c 20 38 34 2c   81, 82, 83, 84,
76a60 20 38 35 2c 20 38 36 2c 20 38 37 2c 20 38 38 2c   85, 86, 87, 88,
76a70 20 38 39 2c 20 39 30 2c 20 39 31 2c 20 39 32 2c   89, 90, 91, 92,
76a80 20 39 33 2c 20 39 34 2c 20 39 35 2c 20 2f 2a 20   93, 94, 95, /* 
76a90 35 78 20 2a 2f 0a 20 20 20 20 20 39 36 2c 20 39  5x */.     96, 9
76aa0 37 2c 20 36 36 2c 20 36 37 2c 20 36 38 2c 20 36  7, 66, 67, 68, 6
76ab0 39 2c 20 37 30 2c 20 37 31 2c 20 37 32 2c 20 37  9, 70, 71, 72, 7
76ac0 33 2c 31 30 36 2c 31 30 37 2c 31 30 38 2c 31 30  3,106,107,108,10
76ad0 39 2c 31 31 30 2c 31 31 31 2c 20 2f 2a 20 36 78  9,110,111, /* 6x
76ae0 20 2a 2f 0a 20 20 20 20 31 31 32 2c 20 38 31 2c   */.    112, 81,
76af0 20 38 32 2c 20 38 33 2c 20 38 34 2c 20 38 35 2c   82, 83, 84, 85,
76b00 20 38 36 2c 20 38 37 2c 20 38 38 2c 20 38 39 2c   86, 87, 88, 89,
76b10 31 32 32 2c 31 32 33 2c 31 32 34 2c 31 32 35 2c  122,123,124,125,
76b20 31 32 36 2c 31 32 37 2c 20 2f 2a 20 37 78 20 2a  126,127, /* 7x *
76b30 2f 0a 20 20 20 20 31 32 38 2c 31 32 39 2c 31 33  /.    128,129,13
76b40 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 31 33  0,131,132,133,13
76b50 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 31 33  4,135,136,137,13
76b60 38 2c 31 33 39 2c 31 34 30 2c 31 34 31 2c 31 34  8,139,140,141,14
76b70 32 2c 31 34 33 2c 20 2f 2a 20 38 78 20 2a 2f 0a  2,143, /* 8x */.
76b80 20 20 20 20 31 34 34 2c 31 34 35 2c 31 34 36 2c      144,145,146,
76b90 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c  147,148,149,150,
76ba0 31 35 31 2c 31 35 32 2c 31 35 33 2c 31 35 34 2c  151,152,153,154,
76bb0 31 35 35 2c 31 35 36 2c 31 35 37 2c 31 35 36 2c  155,156,157,156,
76bc0 31 35 39 2c 20 2f 2a 20 39 78 20 2a 2f 0a 20 20  159, /* 9x */.  
76bd0 20 20 31 36 30 2c 31 36 31 2c 31 36 32 2c 31 36    160,161,162,16
76be0 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 31 36  3,164,165,166,16
76bf0 37 2c 31 36 38 2c 31 36 39 2c 31 37 30 2c 31 37  7,168,169,170,17
76c00 31 2c 31 34 30 2c 31 34 31 2c 31 34 32 2c 31 37  1,140,141,142,17
76c10 35 2c 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20 20  5, /* Ax */.    
76c20 31 37 36 2c 31 37 37 2c 31 37 38 2c 31 37 39 2c  176,177,178,179,
76c30 31 38 30 2c 31 38 31 2c 31 38 32 2c 31 38 33 2c  180,181,182,183,
76c40 31 38 34 2c 31 38 35 2c 31 38 36 2c 31 38 37 2c  184,185,186,187,
76c50 31 38 38 2c 31 38 39 2c 31 39 30 2c 31 39 31 2c  188,189,190,191,
76c60 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 20 31 39   /* Bx */.    19
76c70 32 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33  2,129,130,131,13
76c80 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33  2,133,134,135,13
76c90 36 2c 31 33 37 2c 32 30 32 2c 32 30 33 2c 32 30  6,137,202,203,20
76ca0 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c 20 2f  4,205,206,207, /
76cb0 2a 20 43 78 20 2a 2f 0a 20 20 20 20 32 30 38 2c  * Cx */.    208,
76cc0 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38 2c  145,146,147,148,
76cd0 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32 2c  149,150,151,152,
76ce0 31 35 33 2c 32 31 38 2c 32 31 39 2c 32 32 30 2c  153,218,219,220,
76cf0 32 32 31 2c 32 32 32 2c 32 32 33 2c 20 2f 2a 20  221,222,223, /* 
76d00 44 78 20 2a 2f 0a 20 20 20 20 32 32 34 2c 32 32  Dx */.    224,22
76d10 35 2c 31 36 32 2c 31 36 33 2c 31 36 34 2c 31 36  5,162,163,164,16
76d20 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 31 36  5,166,167,168,16
76d30 39 2c 32 33 32 2c 32 30 33 2c 32 30 34 2c 32 30  9,232,203,204,20
76d40 35 2c 32 30 36 2c 32 30 37 2c 20 2f 2a 20 45 78  5,206,207, /* Ex
76d50 20 2a 2f 0a 20 20 20 20 32 33 39 2c 32 34 30 2c   */.    239,240,
76d60 32 34 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c  241,242,243,244,
76d70 32 34 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c  245,246,247,248,
76d80 32 34 39 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c  249,219,220,221,
76d90 32 32 32 2c 32 35 35 2c 20 2f 2a 20 46 78 20 2a  222,255, /* Fx *
76da0 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a  /.#endif.};../*.
76db0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
76dc0 20 32 35 36 20 62 79 74 65 20 6c 6f 6f 6b 75 70   256 byte lookup
76dd0 20 74 61 62 6c 65 20 69 73 20 75 73 65 64 20 74   table is used t
76de0 6f 20 73 75 70 70 6f 72 74 20 53 51 4c 69 74 65  o support SQLite
76df0 73 20 62 75 69 6c 74 2d 69 6e 0a 2a 2a 20 65 71  s built-in.** eq
76e00 75 69 76 61 6c 65 6e 74 73 20 74 6f 20 74 68 65  uivalents to the
76e10 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 6e 64   following stand
76e20 61 72 64 20 6c 69 62 72 61 72 79 20 66 75 6e 63  ard library func
76e30 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 69  tions:.**.**   i
76e40 73 73 70 61 63 65 28 29 20 20 20 20 20 20 20 20  sspace()        
76e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76e60 30 78 30 31 0a 2a 2a 20 20 20 69 73 61 6c 70 68  0x01.**   isalph
76e70 61 28 29 20 20 20 20 20 20 20 20 20 20 20 20 20  a()             
76e80 20 20 20 20 20 20 20 20 20 20 20 30 78 30 32 0a             0x02.
76e90 2a 2a 20 20 20 69 73 64 69 67 69 74 28 29 20 20  **   isdigit()  
76ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76eb0 20 20 20 20 20 20 30 78 30 34 0a 2a 2a 20 20 20        0x04.**   
76ec0 69 73 61 6c 6e 75 6d 28 29 20 20 20 20 20 20 20  isalnum()       
76ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76ee0 20 30 78 30 36 0a 2a 2a 20 20 20 69 73 78 64 69   0x06.**   isxdi
76ef0 67 69 74 28 29 20 20 20 20 20 20 20 20 20 20 20  git()           
76f00 20 20 20 20 20 20 20 20 20 20 20 20 30 78 30 38              0x08
76f10 0a 2a 2a 20 20 20 74 6f 75 70 70 65 72 28 29 20  .**   toupper() 
76f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76f30 20 20 20 20 20 20 20 30 78 32 30 0a 2a 2a 0a 2a         0x20.**.*
76f40 2a 20 42 69 74 20 30 78 32 30 20 69 73 20 73 65  * Bit 0x20 is se
76f50 74 20 69 66 20 74 68 65 20 6d 61 70 70 65 64 20  t if the mapped 
76f60 63 68 61 72 61 63 74 65 72 20 72 65 71 75 69 72  character requir
76f70 65 73 20 74 72 61 6e 73 6c 61 74 69 6f 6e 20 74  es translation t
76f80 6f 20 75 70 70 65 72 0a 2a 2a 20 63 61 73 65 2e  o upper.** case.
76f90 20 69 2e 65 2e 20 69 66 20 74 68 65 20 63 68 61   i.e. if the cha
76fa0 72 61 63 74 65 72 20 69 73 20 61 20 6c 6f 77 65  racter is a lowe
76fb0 72 2d 63 61 73 65 20 41 53 43 49 49 20 63 68 61  r-case ASCII cha
76fc0 72 61 63 74 65 72 2e 0a 2a 2a 20 49 66 20 78 20  racter..** If x 
76fd0 69 73 20 61 20 6c 6f 77 65 72 2d 63 61 73 65 20  is a lower-case 
76fe0 41 53 43 49 49 20 63 68 61 72 61 63 74 65 72 2c  ASCII character,
76ff0 20 74 68 65 6e 20 69 74 73 20 75 70 70 65 72 2d   then its upper-
77000 63 61 73 65 20 65 71 75 69 76 61 6c 65 6e 74 0a  case equivalent.
77010 2a 2a 20 69 73 20 28 78 20 2d 20 30 78 32 30 29  ** is (x - 0x20)
77020 2e 20 54 68 65 72 65 66 6f 72 65 20 74 6f 75 70  . Therefore toup
77030 70 65 72 28 29 20 63 61 6e 20 62 65 20 69 6d 70  per() can be imp
77040 6c 65 6d 65 6e 74 65 64 20 61 73 3a 0a 2a 2a 0a  lemented as:.**.
77050 2a 2a 20 20 20 28 78 20 26 20 7e 28 6d 61 70 5b  **   (x & ~(map[
77060 78 5d 26 30 78 32 30 29 29 0a 2a 2a 0a 2a 2a 20  x]&0x20)).**.** 
77070 53 74 61 6e 64 61 72 64 20 66 75 6e 63 74 69 6f  Standard functio
77080 6e 20 74 6f 6c 6f 77 65 72 28 29 20 69 73 20 69  n tolower() is i
77090 6d 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67  mplemented using
770a0 20 74 68 65 20 73 71 6c 69 74 65 33 55 70 70 65   the sqlite3Uppe
770b0 72 54 6f 4c 6f 77 65 72 5b 5d 0a 2a 2a 20 61 72  rToLower[].** ar
770c0 72 61 79 2e 20 74 6f 6c 6f 77 65 72 28 29 20 69  ray. tolower() i
770d0 73 20 75 73 65 64 20 6d 6f 72 65 20 6f 66 74 65  s used more ofte
770e0 6e 20 74 68 61 6e 20 74 6f 75 70 70 65 72 28 29  n than toupper()
770f0 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a   by SQLite..**.*
77100 2a 20 53 51 4c 69 74 65 27 73 20 76 65 72 73 69  * SQLite's versi
77110 6f 6e 73 20 61 72 65 20 69 64 65 6e 74 69 63 61  ons are identica
77120 6c 20 74 6f 20 74 68 65 20 73 74 61 6e 64 61 72  l to the standar
77130 64 20 76 65 72 73 69 6f 6e 73 20 61 73 73 75 6d  d versions assum
77140 69 6e 67 20 61 0a 2a 2a 20 6c 6f 63 61 6c 65 20  ing a.** locale 
77150 6f 66 20 22 43 22 2e 20 54 68 65 79 20 61 72 65  of "C". They are
77160 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20   implemented as 
77170 6d 61 63 72 6f 73 20 69 6e 20 73 71 6c 69 74 65  macros in sqlite
77180 49 6e 74 2e 68 2e 0a 2a 2f 0a 23 69 66 64 65 66  Int.h..*/.#ifdef
77190 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 53 51   SQLITE_ASCII.SQ
771a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
771b0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
771c0 20 73 71 6c 69 74 65 33 43 74 79 70 65 4d 61 70   sqlite3CtypeMap
771d0 5b 32 35 36 5d 20 3d 20 7b 0a 20 20 30 78 30 30  [256] = {.  0x00
771e0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
771f0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77200 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20  0x00, 0x00,  /* 
77210 30 30 2e 2e 30 37 20 20 20 20 2e 2e 2e 2e 2e 2e  00..07    ......
77220 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78  .. */.  0x00, 0x
77230 30 31 2c 20 30 78 30 31 2c 20 30 78 30 31 2c 20  01, 0x01, 0x01, 
77240 30 78 30 31 2c 20 30 78 30 31 2c 20 30 78 30 30  0x01, 0x01, 0x00
77250 2c 20 30 78 30 30 2c 20 20 2f 2a 20 30 38 2e 2e  , 0x00,  /* 08..
77260 30 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a  0f    ........ *
77270 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20  /.  0x00, 0x00, 
77280 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
77290 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
772a0 30 30 2c 20 20 2f 2a 20 31 30 2e 2e 31 37 20 20  00,  /* 10..17  
772b0 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20    ........ */.  
772c0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
772d0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
772e0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
772f0 20 2f 2a 20 31 38 2e 2e 31 66 20 20 20 20 2e 2e   /* 18..1f    ..
77300 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 31  ...... */.  0x01
77310 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77320 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77330 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20  0x00, 0x00,  /* 
77340 32 30 2e 2e 32 37 20 20 20 20 20 21 22 23 24 25  20..27     !"#$%
77350 26 27 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78  &' */.  0x00, 0x
77360 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77370 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
77380 2c 20 30 78 30 30 2c 20 20 2f 2a 20 32 38 2e 2e  , 0x00,  /* 28..
77390 32 66 20 20 20 20 28 29 2a 2b 2c 2d 2e 2f 20 2a  2f    ()*+,-./ *
773a0 2f 0a 20 20 30 78 30 63 2c 20 30 78 30 63 2c 20  /.  0x0c, 0x0c, 
773b0 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 63  0x0c, 0x0c, 0x0c
773c0 2c 20 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78  , 0x0c, 0x0c, 0x
773d0 30 63 2c 20 20 2f 2a 20 33 30 2e 2e 33 37 20 20  0c,  /* 30..37  
773e0 20 20 30 31 32 33 34 35 36 37 20 2a 2f 0a 20 20    01234567 */.  
773f0 30 78 30 63 2c 20 30 78 30 63 2c 20 30 78 30 30  0x0c, 0x0c, 0x00
77400 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77410 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77420 20 2f 2a 20 33 38 2e 2e 33 66 20 20 20 20 38 39   /* 38..3f    89
77430 3a 3b 3c 3d 3e 3f 20 2a 2f 0a 0a 20 20 30 78 30  :;<=>? */..  0x0
77440 30 2c 20 30 78 30 61 2c 20 30 78 30 61 2c 20 30  0, 0x0a, 0x0a, 0
77450 78 30 61 2c 20 30 78 30 61 2c 20 30 78 30 61 2c  x0a, 0x0a, 0x0a,
77460 20 30 78 30 61 2c 20 30 78 30 32 2c 20 20 2f 2a   0x0a, 0x02,  /*
77470 20 34 30 2e 2e 34 37 20 20 20 20 40 41 42 43 44   40..47    @ABCD
77480 45 46 47 20 2a 2f 0a 20 20 30 78 30 32 2c 20 30  EFG */.  0x02, 0
77490 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c  x02, 0x02, 0x02,
774a0 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30   0x02, 0x02, 0x0
774b0 32 2c 20 30 78 30 32 2c 20 20 2f 2a 20 34 38 2e  2, 0x02,  /* 48.
774c0 2e 34 66 20 20 20 20 48 49 4a 4b 4c 4d 4e 4f 20  .4f    HIJKLMNO 
774d0 2a 2f 0a 20 20 30 78 30 32 2c 20 30 78 30 32 2c  */.  0x02, 0x02,
774e0 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30   0x02, 0x02, 0x0
774f0 32 2c 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30  2, 0x02, 0x02, 0
77500 78 30 32 2c 20 20 2f 2a 20 35 30 2e 2e 35 37 20  x02,  /* 50..57 
77510 20 20 20 50 51 52 53 54 55 56 57 20 2a 2f 0a 20     PQRSTUVW */. 
77520 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30   0x02, 0x02, 0x0
77530 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  2, 0x00, 0x00, 0
77540 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77550 20 20 2f 2a 20 35 38 2e 2e 35 66 20 20 20 20 58    /* 58..5f    X
77560 59 5a 5b 5c 5d 5e 5f 20 2a 2f 0a 20 20 30 78 30  YZ[\]^_ */.  0x0
77570 30 2c 20 30 78 32 61 2c 20 30 78 32 61 2c 20 30  0, 0x2a, 0x2a, 0
77580 78 32 61 2c 20 30 78 32 61 2c 20 30 78 32 61 2c  x2a, 0x2a, 0x2a,
77590 20 30 78 32 61 2c 20 30 78 32 32 2c 20 20 2f 2a   0x2a, 0x22,  /*
775a0 20 36 30 2e 2e 36 37 20 20 20 20 60 61 62 63 64   60..67    `abcd
775b0 65 66 67 20 2a 2f 0a 20 20 30 78 32 32 2c 20 30  efg */.  0x22, 0
775c0 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c  x22, 0x22, 0x22,
775d0 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32   0x22, 0x22, 0x2
775e0 32 2c 20 30 78 32 32 2c 20 20 2f 2a 20 36 38 2e  2, 0x22,  /* 68.
775f0 2e 36 66 20 20 20 20 68 69 6a 6b 6c 6d 6e 6f 20  .6f    hijklmno 
77600 2a 2f 0a 20 20 30 78 32 32 2c 20 30 78 32 32 2c  */.  0x22, 0x22,
77610 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32   0x22, 0x22, 0x2
77620 32 2c 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30  2, 0x22, 0x22, 0
77630 78 32 32 2c 20 20 2f 2a 20 37 30 2e 2e 37 37 20  x22,  /* 70..77 
77640 20 20 20 70 71 72 73 74 75 76 77 20 2a 2f 0a 20     pqrstuvw */. 
77650 20 30 78 32 32 2c 20 30 78 32 32 2c 20 30 78 32   0x22, 0x22, 0x2
77660 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  2, 0x00, 0x00, 0
77670 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77680 20 20 2f 2a 20 37 38 2e 2e 37 66 20 20 20 20 78    /* 78..7f    x
77690 79 7a 7b 7c 7d 7e 2e 20 2a 2f 0a 0a 20 20 30 78  yz{|}~. */..  0x
776a0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
776b0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
776c0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f  , 0x00, 0x00,  /
776d0 2a 20 38 30 2e 2e 38 37 20 20 20 20 2e 2e 2e 2e  * 80..87    ....
776e0 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20  .... */.  0x00, 
776f0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
77700 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77710 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 38 38  00, 0x00,  /* 88
77720 2e 2e 38 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e  ..8f    ........
77730 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30   */.  0x00, 0x00
77740 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77750 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77760 30 78 30 30 2c 20 20 2f 2a 20 39 30 2e 2e 39 37  0x00,  /* 90..97
77770 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a      ........ */.
77780 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78    0x00, 0x00, 0x
77790 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
777a0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
777b0 2c 20 20 2f 2a 20 39 38 2e 2e 39 66 20 20 20 20  ,  /* 98..9f    
777c0 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78  ........ */.  0x
777d0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
777e0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
777f0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20 2f  , 0x00, 0x00,  /
77800 2a 20 61 30 2e 2e 61 37 20 20 20 20 2e 2e 2e 2e  * a0..a7    ....
77810 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20  .... */.  0x00, 
77820 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
77830 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77840 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 61 38  00, 0x00,  /* a8
77850 2e 2e 61 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e  ..af    ........
77860 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30 30   */.  0x00, 0x00
77870 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
77880 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
77890 30 78 30 30 2c 20 20 2f 2a 20 62 30 2e 2e 62 37  0x00,  /* b0..b7
778a0 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a      ........ */.
778b0 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78    0x00, 0x00, 0x
778c0 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
778d0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
778e0 2c 20 20 2f 2a 20 62 38 2e 2e 62 66 20 20 20 20  ,  /* b8..bf    
778f0 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 0a 20 20 30  ........ */..  0
77900 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77910 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77920 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20  0, 0x00, 0x00,  
77930 2f 2a 20 63 30 2e 2e 63 37 20 20 20 20 2e 2e 2e  /* c0..c7    ...
77940 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c  ..... */.  0x00,
77950 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77960 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77970 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 63  x00, 0x00,  /* c
77980 38 2e 2e 63 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e  8..cf    .......
77990 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30  . */.  0x00, 0x0
779a0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
779b0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
779c0 20 30 78 30 30 2c 20 20 2f 2a 20 64 30 2e 2e 64   0x00,  /* d0..d
779d0 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f  7    ........ */
779e0 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  .  0x00, 0x00, 0
779f0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77a00 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77a10 30 2c 20 20 2f 2a 20 64 38 2e 2e 64 66 20 20 20  0,  /* d8..df   
77a20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30   ........ */.  0
77a30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77a40 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77a50 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 20  0, 0x00, 0x00,  
77a60 2f 2a 20 65 30 2e 2e 65 37 20 20 20 20 2e 2e 2e  /* e0..e7    ...
77a70 2e 2e 2e 2e 2e 20 2a 2f 0a 20 20 30 78 30 30 2c  ..... */.  0x00,
77a80 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77a90 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77aa0 78 30 30 2c 20 30 78 30 30 2c 20 20 2f 2a 20 65  x00, 0x00,  /* e
77ab0 38 2e 2e 65 66 20 20 20 20 2e 2e 2e 2e 2e 2e 2e  8..ef    .......
77ac0 2e 20 2a 2f 0a 20 20 30 78 30 30 2c 20 30 78 30  . */.  0x00, 0x0
77ad0 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  0, 0x00, 0x00, 0
77ae0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77af0 20 30 78 30 30 2c 20 20 2f 2a 20 66 30 2e 2e 66   0x00,  /* f0..f
77b00 37 20 20 20 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f  7    ........ */
77b10 0a 20 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  .  0x00, 0x00, 0
77b20 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
77b30 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30   0x00, 0x00, 0x0
77b40 30 20 20 20 2f 2a 20 66 38 2e 2e 66 66 20 20 20  0   /* f8..ff   
77b50 20 2e 2e 2e 2e 2e 2e 2e 2e 20 2a 2f 0a 7d 3b 0a   ........ */.};.
77b60 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a 2a 2a 20  #endif..../*.** 
77b70 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 69  The following si
77b80 6e 67 6c 65 74 6f 6e 20 63 6f 6e 74 61 69 6e 73  ngleton contains
77b90 20 74 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66   the global conf
77ba0 69 67 75 72 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a  iguration for.**
77bb0 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
77bc0 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ary..*/.SQLITE_P
77bd0 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f 57 53  RIVATE SQLITE_WS
77be0 44 20 73 74 72 75 63 74 20 53 71 6c 69 74 65 33  D struct Sqlite3
77bf0 43 6f 6e 66 69 67 20 73 71 6c 69 74 65 33 43 6f  Config sqlite3Co
77c00 6e 66 69 67 20 3d 20 7b 0a 20 20 20 53 51 4c 49  nfig = {.   SQLI
77c10 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54  TE_DEFAULT_MEMST
77c20 41 54 55 53 2c 20 20 2f 2a 20 62 4d 65 6d 73 74  ATUS,  /* bMemst
77c30 61 74 20 2a 2f 0a 20 20 20 31 2c 20 20 20 20 20  at */.   1,     
77c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77c50 20 20 20 20 2f 2a 20 62 43 6f 72 65 4d 75 74 65      /* bCoreMute
77c60 78 20 2a 2f 0a 20 20 20 53 51 4c 49 54 45 5f 54  x */.   SQLITE_T
77c70 48 52 45 41 44 53 41 46 45 3d 3d 31 2c 20 20 20  HREADSAFE==1,   
77c80 20 20 20 2f 2a 20 62 46 75 6c 6c 4d 75 74 65 78     /* bFullMutex
77c90 20 2a 2f 0a 20 20 20 30 78 37 66 66 66 66 66 66   */.   0x7ffffff
77ca0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
77cb0 20 20 2f 2a 20 6d 78 53 74 72 6c 65 6e 20 2a 2f    /* mxStrlen */
77cc0 0a 20 20 20 31 30 30 2c 20 20 20 20 20 20 20 20  .   100,        
77cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
77ce0 2a 20 73 7a 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f  * szLookaside */
77cf0 0a 20 20 20 35 30 30 2c 20 20 20 20 20 20 20 20  .   500,        
77d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
77d10 2a 20 6e 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a  * nLookaside */.
77d20 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c     {0,0,0,0,0,0,
77d30 30 2c 30 7d 2c 20 20 20 20 20 20 20 20 20 2f 2a  0,0},         /*
77d40 20 6d 20 2a 2f 0a 20 20 20 7b 30 2c 30 2c 30 2c   m */.   {0,0,0,
77d50 30 2c 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20  0,0,0,0,0,0},   
77d60 20 20 20 20 2f 2a 20 6d 75 74 65 78 20 2a 2f 0a      /* mutex */.
77d70 20 20 20 7b 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c     {0,0,0,0,0,0,
77d80 30 2c 30 2c 30 2c 30 2c 30 7d 2c 20 20 20 2f 2a  0,0,0,0,0},   /*
77d90 20 70 63 61 63 68 65 20 2a 2f 0a 20 20 20 28 76   pcache */.   (v
77da0 6f 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20  oid*)0,         
77db0 20 20 20 20 20 20 20 20 20 2f 2a 20 70 48 65 61           /* pHea
77dc0 70 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20  p */.   0,      
77dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77de0 20 20 20 2f 2a 20 6e 48 65 61 70 20 2a 2f 0a 20     /* nHeap */. 
77df0 20 20 30 2c 20 30 2c 20 20 20 20 20 20 20 20 20    0, 0,         
77e00 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
77e10 6d 6e 48 65 61 70 2c 20 6d 78 48 65 61 70 20 2a  mnHeap, mxHeap *
77e20 2f 0a 20 20 20 28 76 6f 69 64 2a 29 30 2c 20 20  /.   (void*)0,  
77e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77e40 2f 2a 20 70 53 63 72 61 74 63 68 20 2a 2f 0a 20  /* pScratch */. 
77e50 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
77e60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
77e70 73 7a 53 63 72 61 74 63 68 20 2a 2f 0a 20 20 20  szScratch */.   
77e80 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
77e90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 53             /* nS
77ea0 63 72 61 74 63 68 20 2a 2f 0a 20 20 20 28 76 6f  cratch */.   (vo
77eb0 69 64 2a 29 30 2c 20 20 20 20 20 20 20 20 20 20  id*)0,          
77ec0 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65          /* pPage
77ed0 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20 20 20 20   */.   0,       
77ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77ef0 20 20 2f 2a 20 73 7a 50 61 67 65 20 2a 2f 0a 20    /* szPage */. 
77f00 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
77f10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
77f20 6e 50 61 67 65 20 2a 2f 0a 20 20 20 30 2c 20 20  nPage */.   0,  
77f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77f40 20 20 20 20 20 20 20 2f 2a 20 6d 78 50 61 72 73         /* mxPars
77f50 65 72 53 74 61 63 6b 20 2a 2f 0a 20 20 20 30 2c  erStack */.   0,
77f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77f70 20 20 20 20 20 20 20 20 20 2f 2a 20 73 68 61 72           /* shar
77f80 65 64 43 61 63 68 65 45 6e 61 62 6c 65 64 20 2a  edCacheEnabled *
77f90 2f 0a 20 20 20 2f 2a 20 41 6c 6c 20 74 68 65 20  /.   /* All the 
77fa0 72 65 73 74 20 73 68 6f 75 6c 64 20 61 6c 77 61  rest should alwa
77fb0 79 73 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65  ys be initialize
77fc0 64 20 74 6f 20 7a 65 72 6f 20 2a 2f 0a 20 20 20  d to zero */.   
77fd0 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
77fe0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 73             /* is
77ff0 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20  Init */.   0,   
78000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78010 20 20 20 20 20 20 2f 2a 20 69 6e 50 72 6f 67 72        /* inProgr
78020 65 73 73 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20  ess */.   0,    
78030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78040 20 20 20 20 20 2f 2a 20 69 73 4d 75 74 65 78 49       /* isMutexI
78050 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20 20  nit */.   0,    
78060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78070 20 20 20 20 20 2f 2a 20 69 73 4d 61 6c 6c 6f 63       /* isMalloc
78080 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20 20  Init */.   0,   
78090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
780a0 20 20 20 20 20 20 2f 2a 20 69 73 50 43 61 63 68        /* isPCach
780b0 65 49 6e 69 74 20 2a 2f 0a 20 20 20 30 2c 20 20  eInit */.   0,  
780c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
780d0 20 20 20 20 20 20 20 2f 2a 20 70 49 6e 69 74 4d         /* pInitM
780e0 75 74 65 78 20 2a 2f 0a 20 20 20 30 2c 20 20 20  utex */.   0,   
780f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78100 20 20 20 20 20 20 2f 2a 20 6e 52 65 66 49 6e 69        /* nRefIni
78110 74 4d 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 0a 2f  tMutex */.};.../
78120 2a 0a 2a 2a 20 48 61 73 68 20 74 61 62 6c 65 20  *.** Hash table 
78130 66 6f 72 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74  for global funct
78140 69 6f 6e 73 20 2d 20 66 75 6e 63 74 69 6f 6e 73  ions - functions
78150 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a   common to all.*
78160 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * database conne
78170 63 74 69 6f 6e 73 2e 20 20 41 66 74 65 72 20 69  ctions.  After i
78180 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2c 20 74  nitialization, t
78190 68 69 73 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20  his table is.** 
781a0 72 65 61 64 2d 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51  read-only..*/.SQ
781b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 51 4c  LITE_PRIVATE SQL
781c0 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 48  ITE_WSD FuncDefH
781d0 61 73 68 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  ash sqlite3Globa
781e0 6c 46 75 6e 63 74 69 6f 6e 73 3b 0a 0a 2f 2a 0a  lFunctions;../*.
781f0 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20  ** The value of 
78200 74 68 65 20 22 70 65 6e 64 69 6e 67 22 20 62 79  the "pending" by
78210 74 65 20 6d 75 73 74 20 62 65 20 30 78 34 30 30  te must be 0x400
78220 30 30 30 30 30 20 28 31 20 62 79 74 65 20 70 61  00000 (1 byte pa
78230 73 74 20 74 68 65 0a 2a 2a 20 31 2d 67 69 62 61  st the.** 1-giba
78240 62 79 74 65 20 62 6f 75 6e 64 61 72 79 29 20 69  byte boundary) i
78250 6e 20 61 20 63 6f 6d 70 61 74 69 62 6c 65 20 64  n a compatible d
78260 61 74 61 62 61 73 65 2e 20 20 53 51 4c 69 74 65  atabase.  SQLite
78270 20 6e 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 74   never uses.** t
78280 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  he database page
78290 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
782a0 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 2e  he pending byte.
782b0 20 20 49 74 20 6e 65 76 65 72 20 61 74 74 65 6d    It never attem
782c0 70 74 73 0a 2a 2a 20 74 6f 20 72 65 61 64 20 6f  pts.** to read o
782d0 72 20 77 72 69 74 65 20 74 68 61 74 20 70 61 67  r write that pag
782e0 65 2e 20 20 54 68 65 20 70 65 6e 64 69 6e 67 20  e.  The pending 
782f0 62 79 74 65 20 70 61 67 65 20 69 73 20 73 65 74  byte page is set
78300 20 61 73 73 69 67 6e 0a 2a 2a 20 66 6f 72 20 75   assign.** for u
78310 73 65 20 62 79 20 74 68 65 20 56 46 53 20 6c 61  se by the VFS la
78320 79 65 72 73 20 61 73 20 73 70 61 63 65 20 66 6f  yers as space fo
78330 72 20 6d 61 6e 61 67 69 6e 67 20 66 69 6c 65 20  r managing file 
78340 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 44 75 72  locks..**.** Dur
78350 69 6e 67 20 74 65 73 74 69 6e 67 2c 20 69 74 20  ing testing, it 
78360 69 73 20 6f 66 74 65 6e 20 64 65 73 69 72 61 62  is often desirab
78370 6c 65 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 70  le to move the p
78380 65 6e 64 69 6e 67 20 62 79 74 65 20 74 6f 0a 2a  ending byte to.*
78390 2a 20 61 20 64 69 66 66 65 72 65 6e 74 20 70 6f  * a different po
783a0 73 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 66 69  sition in the fi
783b0 6c 65 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77 73  le.  This allows
783c0 20 63 6f 64 65 20 74 68 61 74 20 68 61 73 20 74   code that has t
783d0 6f 0a 2a 2a 20 64 65 61 6c 20 77 69 74 68 20 74  o.** deal with t
783e0 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20  he pending byte 
783f0 74 6f 20 72 75 6e 20 6f 6e 20 66 69 6c 65 73 20  to run on files 
78400 74 68 61 74 20 61 72 65 20 6d 75 63 68 20 73 6d  that are much sm
78410 61 6c 6c 65 72 0a 2a 2a 20 74 68 61 6e 20 31 20  aller.** than 1 
78420 47 69 42 2e 20 20 54 68 65 20 73 71 6c 69 74 65  GiB.  The sqlite
78430 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 29  3_test_control()
78440 20 69 6e 74 65 72 66 61 63 65 20 63 61 6e 20 62   interface can b
78450 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 6d 6f 76  e used to.** mov
78460 65 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62 79  e the pending by
78470 74 65 2e 0a 2a 2a 0a 2a 2a 20 49 4d 50 4f 52 54  te..**.** IMPORT
78480 41 4e 54 3a 20 20 43 68 61 6e 67 69 6e 67 20 74  ANT:  Changing t
78490 68 65 20 70 65 6e 64 69 6e 67 20 62 79 74 65 20  he pending byte 
784a0 74 6f 20 61 6e 79 20 76 61 6c 75 65 20 6f 74 68  to any value oth
784b0 65 72 20 74 68 61 6e 0a 2a 2a 20 30 78 34 30 30  er than.** 0x400
784c0 30 30 30 30 30 20 72 65 73 75 6c 74 73 20 69 6e  00000 results in
784d0 20 61 6e 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65   an incompatible
784e0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66   database file f
784f0 6f 72 6d 61 74 21 0a 2a 2a 20 43 68 61 6e 67 69  ormat!.** Changi
78500 6e 67 20 74 68 65 20 70 65 6e 64 69 6e 67 20 62  ng the pending b
78510 79 74 65 20 64 75 72 69 6e 67 20 6f 70 65 72 61  yte during opera
78520 74 69 6e 67 20 72 65 73 75 6c 74 73 20 69 6e 20  ting results in 
78530 75 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 61 6e 64  undefined.** and
78540 20 64 69 6c 65 74 65 72 69 6f 75 73 20 62 65 68   dileterious beh
78550 61 76 69 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  avior..*/.SQLITE
78560 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
78570 69 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 20  ite3PendingByte 
78580 3d 20 30 78 34 30 30 30 30 30 30 30 3b 0a 0a 2f  = 0x40000000;../
78590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
785a0 6e 64 20 6f 66 20 67 6c 6f 62 61 6c 2e 63 20 2a  nd of global.c *
785b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
785c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
785d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
785e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
785f0 65 67 69 6e 20 66 69 6c 65 20 73 74 61 74 75 73  egin file status
78600 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
78610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
78630 2a 0a 2a 2a 20 32 30 30 38 20 4a 75 6e 65 20 31  *.** 2008 June 1
78640 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  8.**.** The auth
78650 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
78660 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
78670 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
78680 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
78690 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
786a0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
786b0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
786c0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
786d0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
786e0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
786f0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
78700 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
78710 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
78720 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
78730 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
78740 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
78750 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
78760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
78790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
787a0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64 75  .**.** This modu
787b0 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  le implements th
787c0 65 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73  e sqlite3_status
787d0 28 29 20 69 6e 74 65 72 66 61 63 65 20 61 6e 64  () interface and
787e0 20 72 65 6c 61 74 65 64 0a 2a 2a 20 66 75 6e 63   related.** func
787f0 74 69 6f 6e 61 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a  tionality..**.**
78800 20 24 49 64 3a 20 73 74 61 74 75 73 2e 63 2c 76   $Id: status.c,v
78810 20 31 2e 39 20 32 30 30 38 2f 30 39 2f 30 32 20   1.9 2008/09/02 
78820 30 30 3a 35 32 3a 35 32 20 64 72 68 20 45 78 70  00:52:52 drh Exp
78830 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 61 72   $.*/../*.** Var
78840 69 61 62 6c 65 73 20 69 6e 20 77 68 69 63 68 20  iables in which 
78850 74 6f 20 72 65 63 6f 72 64 20 73 74 61 74 75 73  to record status
78860 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
78870 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
78880 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 20  sqlite3StatType 
78890 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65 3b  sqlite3StatType;
788a0 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57  .static SQLITE_W
788b0 53 44 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  SD struct sqlite
788c0 33 53 74 61 74 54 79 70 65 20 7b 0a 20 20 69 6e  3StatType {.  in
788d0 74 20 6e 6f 77 56 61 6c 75 65 5b 39 5d 3b 20 20  t nowValue[9];  
788e0 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
788f0 74 20 76 61 6c 75 65 20 2a 2f 0a 20 20 69 6e 74  t value */.  int
78900 20 6d 78 56 61 6c 75 65 5b 39 5d 3b 20 20 20 20   mxValue[9];    
78910 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d        /* Maximum
78920 20 76 61 6c 75 65 20 2a 2f 0a 7d 20 73 71 6c 69   value */.} sqli
78930 74 65 33 53 74 61 74 20 3d 20 7b 20 7b 30 2c 7d  te3Stat = { {0,}
78940 2c 20 7b 30 2c 7d 20 7d 3b 0a 0a 0a 2f 2a 20 54  , {0,} };.../* T
78950 68 65 20 22 77 73 64 53 74 61 74 22 20 6d 61 63  he "wsdStat" mac
78960 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20  ro will resolve 
78970 74 6f 20 74 68 65 20 73 74 61 74 75 73 20 69 6e  to the status in
78980 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 73 74 61  formation.** sta
78990 74 65 20 76 65 63 74 6f 72 2e 20 20 49 66 20 77  te vector.  If w
789a0 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64  ritable static d
789b0 61 74 61 20 69 73 20 75 6e 73 75 70 70 6f 72 74  ata is unsupport
789c0 65 64 20 6f 6e 20 74 68 65 20 74 61 72 67 65 74  ed on the target
789d0 2c 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20  ,.** we have to 
789e0 6c 6f 63 61 74 65 20 74 68 65 20 73 74 61 74 65  locate the state
789f0 20 76 65 63 74 6f 72 20 61 74 20 72 75 6e 2d 74   vector at run-t
78a00 69 6d 65 2e 20 20 49 6e 20 74 68 65 20 6d 6f 72  ime.  In the mor
78a10 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a 20 63 61 73 65  e common.** case
78a20 20 77 68 65 72 65 20 77 72 69 74 61 62 6c 65 20   where writable 
78a30 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 73  static data is s
78a40 75 70 70 6f 72 74 65 64 2c 20 77 73 64 53 74 61  upported, wsdSta
78a50 74 20 63 61 6e 20 72 65 66 65 72 20 64 69 72 65  t can refer dire
78a60 63 74 6c 79 0a 2a 2a 20 74 6f 20 74 68 65 20 22  ctly.** to the "
78a70 73 71 6c 69 74 65 33 53 74 61 74 22 20 73 74 61  sqlite3Stat" sta
78a80 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72  te vector declar
78a90 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66  ed above..*/.#if
78aa0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
78ab0 57 53 44 0a 23 20 64 65 66 69 6e 65 20 77 73 64  WSD.# define wsd
78ac0 53 74 61 74 49 6e 69 74 20 20 73 71 6c 69 74 65  StatInit  sqlite
78ad0 33 53 74 61 74 54 79 70 65 20 2a 78 20 3d 20 26  3StatType *x = &
78ae0 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33 53 74  GLOBAL(sqlite3St
78af0 61 74 54 79 70 65 2c 73 71 6c 69 74 65 33 53 74  atType,sqlite3St
78b00 61 74 29 0a 23 20 64 65 66 69 6e 65 20 77 73 64  at).# define wsd
78b10 53 74 61 74 20 78 5b 30 5d 0a 23 65 6c 73 65 0a  Stat x[0].#else.
78b20 23 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74  # define wsdStat
78b30 49 6e 69 74 0a 23 20 64 65 66 69 6e 65 20 77 73  Init.# define ws
78b40 64 53 74 61 74 20 73 71 6c 69 74 65 33 53 74 61  dStat sqlite3Sta
78b50 74 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  t.#endif../*.** 
78b60 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65  Return the curre
78b70 6e 74 20 76 61 6c 75 65 20 6f 66 20 61 20 73 74  nt value of a st
78b80 61 74 75 73 20 70 61 72 61 6d 65 74 65 72 2e 0a  atus parameter..
78b90 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
78ba0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 74 61  E int sqlite3Sta
78bb0 74 75 73 56 61 6c 75 65 28 69 6e 74 20 6f 70 29  tusValue(int op)
78bc0 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74 3b  {.  wsdStatInit;
78bd0 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d 30  .  assert( op>=0
78be0 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 65   && op<ArraySize
78bf0 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75  (wsdStat.nowValu
78c00 65 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 77  e) );.  return w
78c10 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b  sdStat.nowValue[
78c20 6f 70 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  op];.}../*.** Ad
78c30 64 20 4e 20 74 6f 20 74 68 65 20 76 61 6c 75 65  d N to the value
78c40 20 6f 66 20 61 20 73 74 61 74 75 73 20 72 65 63   of a status rec
78c50 6f 72 64 2e 20 20 49 74 20 69 73 20 61 73 73 75  ord.  It is assu
78c60 6d 65 64 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  med that the.** 
78c70 63 61 6c 6c 65 72 20 68 6f 6c 64 73 20 61 70 70  caller holds app
78c80 72 6f 70 72 69 61 74 65 20 6c 6f 63 6b 73 2e 0a  ropriate locks..
78c90 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
78ca0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74  E void sqlite3St
78cb0 61 74 75 73 41 64 64 28 69 6e 74 20 6f 70 2c 20  atusAdd(int op, 
78cc0 69 6e 74 20 4e 29 7b 0a 20 20 77 73 64 53 74 61  int N){.  wsdSta
78cd0 74 49 6e 69 74 3b 0a 20 20 61 73 73 65 72 74 28  tInit;.  assert(
78ce0 20 6f 70 3e 3d 30 20 26 26 20 6f 70 3c 41 72 72   op>=0 && op<Arr
78cf0 61 79 53 69 7a 65 28 77 73 64 53 74 61 74 2e 6e  aySize(wsdStat.n
78d00 6f 77 56 61 6c 75 65 29 20 29 3b 0a 20 20 77 73  owValue) );.  ws
78d10 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f  dStat.nowValue[o
78d20 70 5d 20 2b 3d 20 4e 3b 0a 20 20 69 66 28 20 77  p] += N;.  if( w
78d30 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b  sdStat.nowValue[
78d40 6f 70 5d 3e 77 73 64 53 74 61 74 2e 6d 78 56 61  op]>wsdStat.mxVa
78d50 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 20 20 20 77  lue[op] ){.    w
78d60 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f  sdStat.mxValue[o
78d70 70 5d 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f 77  p] = wsdStat.now
78d80 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a 7d  Value[op];.  }.}
78d90 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
78da0 76 61 6c 75 65 20 6f 66 20 61 20 73 74 61 74 75  value of a statu
78db0 73 20 74 6f 20 58 2e 0a 2a 2f 0a 53 51 4c 49 54  s to X..*/.SQLIT
78dc0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
78dd0 71 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28  qlite3StatusSet(
78de0 69 6e 74 20 6f 70 2c 20 69 6e 74 20 58 29 7b 0a  int op, int X){.
78df0 20 20 77 73 64 53 74 61 74 49 6e 69 74 3b 0a 20    wsdStatInit;. 
78e00 20 61 73 73 65 72 74 28 20 6f 70 3e 3d 30 20 26   assert( op>=0 &
78e10 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 65 28 77  & op<ArraySize(w
78e20 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29  sdStat.nowValue)
78e30 20 29 3b 0a 20 20 77 73 64 53 74 61 74 2e 6e 6f   );.  wsdStat.no
78e40 77 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 58 3b 0a  wValue[op] = X;.
78e50 20 20 69 66 28 20 77 73 64 53 74 61 74 2e 6e 6f    if( wsdStat.no
78e60 77 56 61 6c 75 65 5b 6f 70 5d 3e 77 73 64 53 74  wValue[op]>wsdSt
78e70 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 29  at.mxValue[op] )
78e80 7b 0a 20 20 20 20 77 73 64 53 74 61 74 2e 6d 78  {.    wsdStat.mx
78e90 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73 64 53  Value[op] = wsdS
78ea0 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d  tat.nowValue[op]
78eb0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51  ;.  }.}../*.** Q
78ec0 75 65 72 79 20 73 74 61 74 75 73 20 69 6e 66 6f  uery status info
78ed0 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  rmation..**.** T
78ee0 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  his implementati
78ef0 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  on assumes that 
78f00 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69  reading or writi
78f10 6e 67 20 61 6e 20 61 6c 69 67 6e 65 64 0a 2a 2a  ng an aligned.**
78f20 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72 20   32-bit integer 
78f30 69 73 20 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65  is an atomic ope
78f40 72 61 74 69 6f 6e 2e 20 20 49 66 20 74 68 61 74  ration.  If that
78f50 20 61 73 73 75 6d 70 74 69 6f 6e 20 69 73 20 6e   assumption is n
78f60 6f 74 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e  ot true,.** then
78f70 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
78f80 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e   not threadsafe.
78f90 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
78fa0 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75  nt sqlite3_statu
78fb0 73 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70  s(int op, int *p
78fc0 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48  Current, int *pH
78fd0 69 67 68 77 61 74 65 72 2c 20 69 6e 74 20 72 65  ighwater, int re
78fe0 73 65 74 46 6c 61 67 29 7b 0a 20 20 77 73 64 53  setFlag){.  wsdS
78ff0 74 61 74 49 6e 69 74 3b 0a 20 20 69 66 28 20 6f  tatInit;.  if( o
79000 70 3c 30 20 7c 7c 20 6f 70 3e 3d 41 72 72 61 79  p<0 || op>=Array
79010 53 69 7a 65 28 77 73 64 53 74 61 74 2e 6e 6f 77  Size(wsdStat.now
79020 56 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 72 65  Value) ){.    re
79030 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  turn SQLITE_MISU
79040 53 45 3b 0a 20 20 7d 0a 20 20 2a 70 43 75 72 72  SE;.  }.  *pCurr
79050 65 6e 74 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f  ent = wsdStat.no
79060 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 2a 70  wValue[op];.  *p
79070 48 69 67 68 77 61 74 65 72 20 3d 20 77 73 64 53  Highwater = wsdS
79080 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 3b  tat.mxValue[op];
79090 0a 20 20 69 66 28 20 72 65 73 65 74 46 6c 61 67  .  if( resetFlag
790a0 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61 74 2e   ){.    wsdStat.
790b0 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77 73  mxValue[op] = ws
790c0 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b 6f  dStat.nowValue[o
790d0 70 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  p];.  }.  return
790e0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
790f0 2a 0a 2a 2a 20 51 75 65 72 79 20 73 74 61 74 75  *.** Query statu
79100 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  s information fo
79110 72 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62  r a single datab
79120 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a  ase connection.*
79130 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
79140 20 73 71 6c 69 74 65 33 5f 64 62 5f 73 74 61 74   sqlite3_db_stat
79150 75 73 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  us(.  sqlite3 *d
79160 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  b,          /* T
79170 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
79180 65 63 74 69 6f 6e 20 77 68 6f 73 65 20 73 74 61  ection whose sta
79190 74 75 73 20 69 73 20 64 65 73 69 72 65 64 20 2a  tus is desired *
791a0 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20  /.  int op,     
791b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61            /* Sta
791c0 74 75 73 20 76 65 72 62 20 2a 2f 0a 20 20 69 6e  tus verb */.  in
791d0 74 20 2a 70 43 75 72 72 65 6e 74 2c 20 20 20 20  t *pCurrent,    
791e0 20 20 20 20 2f 2a 20 57 72 69 74 65 20 63 75 72      /* Write cur
791f0 72 65 6e 74 20 76 61 6c 75 65 20 68 65 72 65 20  rent value here 
79200 2a 2f 0a 20 20 69 6e 74 20 2a 70 48 69 67 68 77  */.  int *pHighw
79210 61 74 65 72 2c 20 20 20 20 20 20 2f 2a 20 57 72  ater,      /* Wr
79220 69 74 65 20 68 69 67 68 2d 77 61 74 65 72 20 6d  ite high-water m
79230 61 72 6b 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e  ark here */.  in
79240 74 20 72 65 73 65 74 46 6c 61 67 20 20 20 20 20  t resetFlag     
79250 20 20 20 20 2f 2a 20 52 65 73 65 74 20 68 69 67      /* Reset hig
79260 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 69 66 20  h-water mark if 
79270 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 73 77 69  true */.){.  swi
79280 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63  tch( op ){.    c
79290 61 73 65 20 53 51 4c 49 54 45 5f 44 42 53 54 41  ase SQLITE_DBSTA
792a0 54 55 53 5f 4c 4f 4f 4b 41 53 49 44 45 5f 55 53  TUS_LOOKASIDE_US
792b0 45 44 3a 20 7b 0a 20 20 20 20 20 20 2a 70 43 75  ED: {.      *pCu
792c0 72 72 65 6e 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b  rrent = db->look
792d0 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20 20 20  aside.nOut;.    
792e0 20 20 2a 70 48 69 67 68 77 61 74 65 72 20 3d 20    *pHighwater = 
792f0 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78  db->lookaside.mx
79300 4f 75 74 3b 0a 20 20 20 20 20 20 69 66 28 20 72  Out;.      if( r
79310 65 73 65 74 46 6c 61 67 20 29 7b 0a 20 20 20 20  esetFlag ){.    
79320 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
79330 65 2e 6d 78 4f 75 74 20 3d 20 64 62 2d 3e 6c 6f  e.mxOut = db->lo
79340 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3b 0a 20 20  okaside.nOut;.  
79350 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
79360 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
79370 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 65  ault: {.      re
79380 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
79390 52 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  R;.    }.  }.  r
793a0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
793b0 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
793c0 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 74 61 74 75  *** End of statu
793d0 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s.c ************
793e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
793f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79400 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
79410 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 64  *** Begin file d
79420 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ate.c **********
79430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79450 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 4f  **/./*.** 2003 O
79460 63 74 6f 62 65 72 20 33 31 0a 2a 2a 0a 2a 2a 20  ctober 31.**.** 
79470 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
79480 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
79490 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
794a0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
794b0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
794c0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
794d0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
794e0 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
794f0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
79500 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
79510 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
79520 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
79530 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
79540 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
79550 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
79560 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
79570 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
79580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
79590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
795a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
795b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
795c0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
795d0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74   file contains t
795e0 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74  he C functions t
795f0 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 64 61  hat implement da
79600 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 66  te and time.** f
79610 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 53 51 4c  unctions for SQL
79620 69 74 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65  ite.  .**.** The
79630 72 65 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65  re is only one e
79640 78 70 6f 72 74 65 64 20 73 79 6d 62 6f 6c 20 69  xported symbol i
79650 6e 20 74 68 69 73 20 66 69 6c 65 20 2d 20 74 68  n this file - th
79660 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71  e function.** sq
79670 6c 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74  lite3RegisterDat
79680 65 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 29  eTimeFunctions()
79690 20 66 6f 75 6e 64 20 61 74 20 74 68 65 20 62 6f   found at the bo
796a0 74 74 6f 6d 20 6f 66 20 74 68 65 20 66 69 6c 65  ttom of the file
796b0 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72 20 63  ..** All other c
796c0 6f 64 65 20 68 61 73 20 66 69 6c 65 20 73 63 6f  ode has file sco
796d0 70 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 64  pe..**.** $Id: d
796e0 61 74 65 2e 63 2c 76 20 31 2e 31 30 37 20 32 30  ate.c,v 1.107 20
796f0 30 39 2f 30 35 2f 30 33 20 32 30 3a 32 33 3a 35  09/05/03 20:23:5
79700 33 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a  3 drh Exp $.**.*
79710 2a 20 53 51 4c 69 74 65 20 70 72 6f 63 65 73 73  * SQLite process
79720 65 73 20 61 6c 6c 20 74 69 6d 65 73 20 61 6e 64  es all times and
79730 20 64 61 74 65 73 20 61 73 20 4a 75 6c 69 61 6e   dates as Julian
79740 20 44 61 79 20 6e 75 6d 62 65 72 73 2e 20 20 54   Day numbers.  T
79750 68 65 0a 2a 2a 20 64 61 74 65 73 20 61 6e 64 20  he.** dates and 
79760 74 69 6d 65 73 20 61 72 65 20 73 74 6f 72 65 64  times are stored
79770 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   as the number o
79780 66 20 64 61 79 73 20 73 69 6e 63 65 20 6e 6f 6f  f days since noo
79790 6e 0a 2a 2a 20 69 6e 20 47 72 65 65 6e 77 69 63  n.** in Greenwic
797a0 68 20 6f 6e 20 4e 6f 76 65 6d 62 65 72 20 32 34  h on November 24
797b0 2c 20 34 37 31 34 20 42 2e 43 2e 20 61 63 63 6f  , 4714 B.C. acco
797c0 72 64 69 6e 67 20 74 6f 20 74 68 65 20 47 72 65  rding to the Gre
797d0 67 6f 72 69 61 6e 0a 2a 2a 20 63 61 6c 65 6e 64  gorian.** calend
797e0 61 72 20 73 79 73 74 65 6d 2e 20 0a 2a 2a 0a 2a  ar system. .**.*
797f0 2a 20 31 39 37 30 2d 30 31 2d 30 31 20 30 30 3a  * 1970-01-01 00:
79800 30 30 3a 30 30 20 69 73 20 4a 44 20 32 34 34 30  00:00 is JD 2440
79810 35 38 37 2e 35 0a 2a 2a 20 32 30 30 30 2d 30 31  587.5.** 2000-01
79820 2d 30 31 20 30 30 3a 30 30 3a 30 30 20 69 73 20  -01 00:00:00 is 
79830 4a 44 20 32 34 35 31 35 34 34 2e 35 0a 2a 2a 0a  JD 2451544.5.**.
79840 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e  ** This implemen
79850 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20 79 65  tion requires ye
79860 61 72 73 20 74 6f 20 62 65 20 65 78 70 72 65 73  ars to be expres
79870 73 65 64 20 61 73 20 61 20 34 2d 64 69 67 69 74  sed as a 4-digit
79880 20 6e 75 6d 62 65 72 0a 2a 2a 20 77 68 69 63 68   number.** which
79890 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 6c 79   means that only
798a0 20 64 61 74 65 73 20 62 65 74 77 65 65 6e 20 30   dates between 0
798b0 30 30 30 2d 30 31 2d 30 31 20 61 6e 64 20 39 39  000-01-01 and 99
798c0 39 39 2d 31 32 2d 33 31 20 63 61 6e 0a 2a 2a 20  99-12-31 can.** 
798d0 62 65 20 72 65 70 72 65 73 65 6e 74 65 64 2c 20  be represented, 
798e0 65 76 65 6e 20 74 68 6f 75 67 68 20 6a 75 6c 69  even though juli
798f0 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 73 20 61  an day numbers a
79900 6c 6c 6f 77 20 61 20 6d 75 63 68 20 77 69 64 65  llow a much wide
79910 72 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64 61  r.** range of da
79920 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 47  tes..**.** The G
79930 72 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64 61  regorian calenda
79940 72 20 73 79 73 74 65 6d 20 69 73 20 75 73 65 64  r system is used
79950 20 66 6f 72 20 61 6c 6c 20 64 61 74 65 73 20 61   for all dates a
79960 6e 64 20 74 69 6d 65 73 2c 0a 2a 2a 20 65 76 65  nd times,.** eve
79970 6e 20 74 68 6f 73 65 20 74 68 61 74 20 70 72 65  n those that pre
79980 64 61 74 65 20 74 68 65 20 47 72 65 67 6f 72 69  date the Gregori
79990 61 6e 20 63 61 6c 65 6e 64 61 72 2e 20 20 48 69  an calendar.  Hi
799a0 73 74 6f 72 69 61 6e 73 20 75 73 75 61 6c 6c 79  storians usually
799b0 0a 2a 2a 20 75 73 65 20 74 68 65 20 4a 75 6c 69  .** use the Juli
799c0 61 6e 20 63 61 6c 65 6e 64 61 72 20 66 6f 72 20  an calendar for 
799d0 64 61 74 65 73 20 70 72 69 6f 72 20 74 6f 20 31  dates prior to 1
799e0 35 38 32 2d 31 30 2d 31 35 20 61 6e 64 20 66 6f  582-10-15 and fo
799f0 72 20 73 6f 6d 65 0a 2a 2a 20 64 61 74 65 73 20  r some.** dates 
79a00 61 66 74 65 72 77 61 72 64 73 2c 20 64 65 70 65  afterwards, depe
79a10 6e 64 69 6e 67 20 6f 6e 20 6c 6f 63 61 6c 65 2e  nding on locale.
79a20 20 20 42 65 77 61 72 65 20 6f 66 20 74 68 69 73    Beware of this
79a30 20 64 69 66 66 65 72 65 6e 63 65 2e 0a 2a 2a 0a   difference..**.
79a40 2a 2a 20 54 68 65 20 63 6f 6e 76 65 72 73 69 6f  ** The conversio
79a50 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72 65  n algorithms are
79a60 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 61 73   implemented bas
79a70 65 64 20 6f 6e 20 64 65 73 63 72 69 70 74 69 6f  ed on descriptio
79a80 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 66 6f 6c  ns.** in the fol
79a90 6c 6f 77 69 6e 67 20 74 65 78 74 3a 0a 2a 2a 0a  lowing text:.**.
79aa0 2a 2a 20 20 20 20 20 20 4a 65 61 6e 20 4d 65 65  **      Jean Mee
79ab0 75 73 0a 2a 2a 20 20 20 20 20 20 41 73 74 72 6f  us.**      Astro
79ac0 6e 6f 6d 69 63 61 6c 20 41 6c 67 6f 72 69 74 68  nomical Algorith
79ad0 6d 73 2c 20 32 6e 64 20 45 64 69 74 69 6f 6e 2c  ms, 2nd Edition,
79ae0 20 31 39 39 38 0a 2a 2a 20 20 20 20 20 20 49 53   1998.**      IS
79af0 42 4d 20 30 2d 39 34 33 33 39 36 2d 36 31 2d 31  BM 0-943396-61-1
79b00 0a 2a 2a 20 20 20 20 20 20 57 69 6c 6c 6d 61 6e  .**      Willman
79b10 6e 2d 42 65 6c 6c 2c 20 49 6e 63 0a 2a 2a 20 20  n-Bell, Inc.**  
79b20 20 20 20 20 52 69 63 68 6d 6f 6e 64 2c 20 56 69      Richmond, Vi
79b30 72 67 69 6e 69 61 20 28 55 53 41 29 0a 2a 2f 0a  rginia (USA).*/.
79b40 23 69 6e 63 6c 75 64 65 20 3c 74 69 6d 65 2e 68  #include <time.h
79b50 3e 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  >..#ifndef SQLIT
79b60 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f  E_OMIT_DATETIME_
79b70 46 55 4e 43 53 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20  FUNCS../*.** On 
79b80 72 65 63 65 6e 74 20 57 69 6e 64 6f 77 73 20 70  recent Windows p
79b90 6c 61 74 66 6f 72 6d 73 2c 20 74 68 65 20 6c 6f  latforms, the lo
79ba0 63 61 6c 74 69 6d 65 5f 73 28 29 20 66 75 6e 63  caltime_s() func
79bb0 74 69 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c  tion is availabl
79bc0 65 0a 2a 2a 20 61 73 20 70 61 72 74 20 6f 66 20  e.** as part of 
79bd0 74 68 65 20 22 53 65 63 75 72 65 20 43 52 54 22  the "Secure CRT"
79be0 2e 20 49 74 20 69 73 20 65 73 73 65 6e 74 69 61  . It is essentia
79bf0 6c 6c 79 20 65 71 75 69 76 61 6c 65 6e 74 20 74  lly equivalent t
79c00 6f 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65 5f  o .** localtime_
79c10 72 28 29 20 61 76 61 69 6c 61 62 6c 65 20 75 6e  r() available un
79c20 64 65 72 20 6d 6f 73 74 20 50 4f 53 49 58 20 70  der most POSIX p
79c30 6c 61 74 66 6f 72 6d 73 2c 20 65 78 63 65 70 74  latforms, except
79c40 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 6f 72   that the .** or
79c50 64 65 72 20 6f 66 20 74 68 65 20 70 61 72 61 6d  der of the param
79c60 65 74 65 72 73 20 69 73 20 72 65 76 65 72 73 65  eters is reverse
79c70 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 68 74 74  d..**.** See htt
79c80 70 3a 2f 2f 6d 73 64 6e 2e 6d 69 63 72 6f 73 6f  p://msdn.microso
79c90 66 74 2e 63 6f 6d 2f 65 6e 2d 75 73 2f 6c 69 62  ft.com/en-us/lib
79ca0 72 61 72 79 2f 61 34 34 32 78 33 79 65 28 56 53  rary/a442x3ye(VS
79cb0 2e 38 30 29 2e 61 73 70 78 2e 0a 2a 2a 0a 2a 2a  .80).aspx..**.**
79cc0 20 49 66 20 74 68 65 20 75 73 65 72 20 68 61 73   If the user has
79cd0 20 6e 6f 74 20 69 6e 64 69 63 61 74 65 64 20 74   not indicated t
79ce0 6f 20 75 73 65 20 6c 6f 63 61 6c 74 69 6d 65 5f  o use localtime_
79cf0 72 28 29 20 6f 72 20 6c 6f 63 61 6c 74 69 6d 65  r() or localtime
79d00 5f 73 28 29 0a 2a 2a 20 61 6c 72 65 61 64 79 2c  _s().** already,
79d10 20 63 68 65 63 6b 20 66 6f 72 20 61 6e 20 4d 53   check for an MS
79d20 56 43 20 62 75 69 6c 64 20 65 6e 76 69 72 6f 6e  VC build environ
79d30 6d 65 6e 74 20 74 68 61 74 20 70 72 6f 76 69 64  ment that provid
79d40 65 73 20 0a 2a 2a 20 6c 6f 63 61 6c 74 69 6d 65  es .** localtime
79d50 5f 73 28 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65  _s()..*/.#if !de
79d60 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c  fined(HAVE_LOCAL
79d70 54 49 4d 45 5f 52 29 20 26 26 20 21 64 65 66 69  TIME_R) && !defi
79d80 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54 49  ned(HAVE_LOCALTI
79d90 4d 45 5f 53 29 20 26 26 20 5c 0a 20 20 20 20 20  ME_S) && \.     
79da0 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52  defined(_MSC_VER
79db0 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 43 52  ) && defined(_CR
79dc0 54 5f 49 4e 53 45 43 55 52 45 5f 44 45 50 52 45  T_INSECURE_DEPRE
79dd0 43 41 54 45 29 0a 23 64 65 66 69 6e 65 20 48 41  CATE).#define HA
79de0 56 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 20 31  VE_LOCALTIME_S 1
79df0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41  .#endif../*.** A
79e00 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 68   structure for h
79e10 6f 6c 64 69 6e 67 20 61 20 73 69 6e 67 6c 65 20  olding a single 
79e20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 2e 0a 2a  date and time..*
79e30 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
79e40 20 44 61 74 65 54 69 6d 65 20 44 61 74 65 54 69   DateTime DateTi
79e50 6d 65 3b 0a 73 74 72 75 63 74 20 44 61 74 65 54  me;.struct DateT
79e60 69 6d 65 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  ime {.  sqlite3_
79e70 69 6e 74 36 34 20 69 4a 44 3b 20 2f 2a 20 54 68  int64 iJD; /* Th
79e80 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d  e julian day num
79e90 62 65 72 20 74 69 6d 65 73 20 38 36 34 30 30 30  ber times 864000
79ea0 30 30 20 2a 2f 0a 20 20 69 6e 74 20 59 2c 20 4d  00 */.  int Y, M
79eb0 2c 20 44 3b 20 20 20 20 20 20 20 2f 2a 20 59 65  , D;       /* Ye
79ec0 61 72 2c 20 6d 6f 6e 74 68 2c 20 61 6e 64 20 64  ar, month, and d
79ed0 61 79 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 6d  ay */.  int h, m
79ee0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f  ;          /* Ho
79ef0 75 72 20 61 6e 64 20 6d 69 6e 75 74 65 73 20 2a  ur and minutes *
79f00 2f 0a 20 20 69 6e 74 20 74 7a 3b 20 20 20 20 20  /.  int tz;     
79f10 20 20 20 20 20 20 20 2f 2a 20 54 69 6d 65 7a 6f         /* Timezo
79f20 6e 65 20 6f 66 66 73 65 74 20 69 6e 20 6d 69 6e  ne offset in min
79f30 75 74 65 73 20 2a 2f 0a 20 20 64 6f 75 62 6c 65  utes */.  double
79f40 20 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20   s;          /* 
79f50 53 65 63 6f 6e 64 73 20 2a 2f 0a 20 20 63 68 61  Seconds */.  cha
79f60 72 20 76 61 6c 69 64 59 4d 44 3b 20 20 20 20 20  r validYMD;     
79f70 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66 20 59  /* True (1) if Y
79f80 2c 4d 2c 44 20 61 72 65 20 76 61 6c 69 64 20 2a  ,M,D are valid *
79f90 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 48 4d  /.  char validHM
79fa0 53 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 28  S;     /* True (
79fb0 31 29 20 69 66 20 68 2c 6d 2c 73 20 61 72 65 20  1) if h,m,s are 
79fc0 76 61 6c 69 64 20 2a 2f 0a 20 20 63 68 61 72 20  valid */.  char 
79fd0 76 61 6c 69 64 4a 44 3b 20 20 20 20 20 20 2f 2a  validJD;      /*
79fe0 20 54 72 75 65 20 28 31 29 20 69 66 20 69 4a 44   True (1) if iJD
79ff0 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 63   is valid */.  c
7a000 68 61 72 20 76 61 6c 69 64 54 5a 3b 20 20 20 20  har validTZ;    
7a010 20 20 2f 2a 20 54 72 75 65 20 28 31 29 20 69 66    /* True (1) if
7a020 20 74 7a 20 69 73 20 76 61 6c 69 64 20 2a 2f 0a   tz is valid */.
7a030 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  };.../*.** Conve
7a040 72 74 20 7a 44 61 74 65 20 69 6e 74 6f 20 6f 6e  rt zDate into on
7a050 65 20 6f 72 20 6d 6f 72 65 20 69 6e 74 65 67 65  e or more intege
7a060 72 73 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c 20  rs.  Additional 
7a070 61 72 67 75 6d 65 6e 74 73 0a 2a 2a 20 63 6f 6d  arguments.** com
7a080 65 20 69 6e 20 67 72 6f 75 70 73 20 6f 66 20 35  e in groups of 5
7a090 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
7a0a0 2a 2a 20 20 20 20 20 20 20 4e 20 20 20 20 20 20  **       N      
7a0b0 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 67 69 74   number of digit
7a0c0 73 20 69 6e 20 74 68 65 20 69 6e 74 65 67 65 72  s in the integer
7a0d0 0a 2a 2a 20 20 20 20 20 20 20 6d 69 6e 20 20 20  .**       min   
7a0e0 20 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 77 65    minimum allowe
7a0f0 64 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69  d value of the i
7a100 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20  nteger.**       
7a110 6d 61 78 20 20 20 20 20 6d 61 78 69 6d 75 6d 20  max     maximum 
7a120 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66  allowed value of
7a130 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20   the integer.** 
7a140 20 20 20 20 20 20 6e 65 78 74 43 20 20 20 66 69        nextC   fi
7a150 72 73 74 20 63 68 61 72 61 63 74 65 72 20 61 66  rst character af
7a160 74 65 72 20 74 68 65 20 69 6e 74 65 67 65 72 0a  ter the integer.
7a170 2a 2a 20 20 20 20 20 20 20 70 56 61 6c 20 20 20  **       pVal   
7a180 20 77 68 65 72 65 20 74 6f 20 77 72 69 74 65 20   where to write 
7a190 74 68 65 20 69 6e 74 65 67 65 72 73 20 76 61 6c  the integers val
7a1a0 75 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72  ue..**.** Conver
7a1b0 73 69 6f 6e 73 20 63 6f 6e 74 69 6e 75 65 20 75  sions continue u
7a1c0 6e 74 69 6c 20 6f 6e 65 20 77 69 74 68 20 6e 65  ntil one with ne
7a1d0 78 74 43 3d 3d 30 20 69 73 20 65 6e 63 6f 75 6e  xtC==0 is encoun
7a1e0 74 65 72 65 64 2e 0a 2a 2a 20 54 68 65 20 66 75  tered..** The fu
7a1f0 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
7a200 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 75 63  he number of suc
7a210 63 65 73 73 66 75 6c 20 63 6f 6e 76 65 72 73 69  cessful conversi
7a220 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ons..*/.static i
7a230 6e 74 20 67 65 74 44 69 67 69 74 73 28 63 6f 6e  nt getDigits(con
7a240 73 74 20 63 68 61 72 20 2a 7a 44 61 74 65 2c 20  st char *zDate, 
7a250 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
7a260 61 70 3b 0a 20 20 69 6e 74 20 76 61 6c 3b 0a 20  ap;.  int val;. 
7a270 20 69 6e 74 20 4e 3b 0a 20 20 69 6e 74 20 6d 69   int N;.  int mi
7a280 6e 3b 0a 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20  n;.  int max;.  
7a290 69 6e 74 20 6e 65 78 74 43 3b 0a 20 20 69 6e 74  int nextC;.  int
7a2a0 20 2a 70 56 61 6c 3b 0a 20 20 69 6e 74 20 63 6e   *pVal;.  int cn
7a2b0 74 20 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 72  t = 0;.  va_star
7a2c0 74 28 61 70 2c 20 7a 44 61 74 65 29 3b 0a 20 20  t(ap, zDate);.  
7a2d0 64 6f 7b 0a 20 20 20 20 4e 20 3d 20 76 61 5f 61  do{.    N = va_a
7a2e0 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20  rg(ap, int);.   
7a2f0 20 6d 69 6e 20 3d 20 76 61 5f 61 72 67 28 61 70   min = va_arg(ap
7a300 2c 20 69 6e 74 29 3b 0a 20 20 20 20 6d 61 78 20  , int);.    max 
7a310 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74  = va_arg(ap, int
7a320 29 3b 0a 20 20 20 20 6e 65 78 74 43 20 3d 20 76  );.    nextC = v
7a330 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a  a_arg(ap, int);.
7a340 20 20 20 20 70 56 61 6c 20 3d 20 76 61 5f 61 72      pVal = va_ar
7a350 67 28 61 70 2c 20 69 6e 74 2a 29 3b 0a 20 20 20  g(ap, int*);.   
7a360 20 76 61 6c 20 3d 20 30 3b 0a 20 20 20 20 77 68   val = 0;.    wh
7a370 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20  ile( N-- ){.    
7a380 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73    if( !sqlite3Is
7a390 64 69 67 69 74 28 2a 7a 44 61 74 65 29 20 29 7b  digit(*zDate) ){
7a3a0 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e  .        goto en
7a3b0 64 5f 67 65 74 44 69 67 69 74 73 3b 0a 20 20 20  d_getDigits;.   
7a3c0 20 20 20 7d 0a 20 20 20 20 20 20 76 61 6c 20 3d     }.      val =
7a3d0 20 76 61 6c 2a 31 30 20 2b 20 2a 7a 44 61 74 65   val*10 + *zDate
7a3e0 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 7a 44   - '0';.      zD
7a3f0 61 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20  ate++;.    }.   
7a400 20 69 66 28 20 76 61 6c 3c 6d 69 6e 20 7c 7c 20   if( val<min || 
7a410 76 61 6c 3e 6d 61 78 20 7c 7c 20 28 6e 65 78 74  val>max || (next
7a420 43 21 3d 30 20 26 26 20 6e 65 78 74 43 21 3d 2a  C!=0 && nextC!=*
7a430 7a 44 61 74 65 29 20 29 7b 0a 20 20 20 20 20 20  zDate) ){.      
7a440 67 6f 74 6f 20 65 6e 64 5f 67 65 74 44 69 67 69  goto end_getDigi
7a450 74 73 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70  ts;.    }.    *p
7a460 56 61 6c 20 3d 20 76 61 6c 3b 0a 20 20 20 20 7a  Val = val;.    z
7a470 44 61 74 65 2b 2b 3b 0a 20 20 20 20 63 6e 74 2b  Date++;.    cnt+
7a480 2b 3b 0a 20 20 7d 77 68 69 6c 65 28 20 6e 65 78  +;.  }while( nex
7a490 74 43 20 29 3b 0a 65 6e 64 5f 67 65 74 44 69 67  tC );.end_getDig
7a4a0 69 74 73 3a 0a 20 20 76 61 5f 65 6e 64 28 61 70  its:.  va_end(ap
7a4b0 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 6e 74 3b  );.  return cnt;
7a4c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74  .}../*.** Read t
7a4d0 65 78 74 20 66 72 6f 6d 20 7a 5b 5d 20 61 6e 64  ext from z[] and
7a4e0 20 63 6f 6e 76 65 72 74 20 69 6e 74 6f 20 61 20   convert into a 
7a4f0 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e  floating point n
7a500 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a  umber.  Return.*
7a510 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
7a520 64 69 67 69 74 73 20 63 6f 6e 76 65 72 74 65 64  digits converted
7a530 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67 65 74  ..*/.#define get
7a540 56 61 6c 75 65 20 73 71 6c 69 74 65 33 41 74 6f  Value sqlite3Ato
7a550 46 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 61  F../*.** Parse a
7a560 20 74 69 6d 65 7a 6f 6e 65 20 65 78 74 65 6e 73   timezone extens
7a570 69 6f 6e 20 6f 6e 20 74 68 65 20 65 6e 64 20 6f  ion on the end o
7a580 66 20 61 20 64 61 74 65 2d 74 69 6d 65 2e 0a 2a  f a date-time..*
7a590 2a 20 54 68 65 20 65 78 74 65 6e 73 69 6f 6e 20  * The extension 
7a5a0 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a  is of the form:.
7a5b0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 28 2b 2f  **.**        (+/
7a5c0 2d 29 48 48 3a 4d 4d 0a 2a 2a 0a 2a 2a 20 4f 72  -)HH:MM.**.** Or
7a5d0 20 74 68 65 20 22 7a 75 6c 75 22 20 6e 6f 74 61   the "zulu" nota
7a5e0 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  tion:.**.**     
7a5f0 20 20 20 5a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68     Z.**.** If th
7a600 65 20 70 61 72 73 65 20 69 73 20 73 75 63 63 65  e parse is succe
7a610 73 73 66 75 6c 2c 20 77 72 69 74 65 20 74 68 65  ssful, write the
7a620 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6e 75 74   number of minut
7a630 65 73 0a 2a 2a 20 6f 66 20 63 68 61 6e 67 65 20  es.** of change 
7a640 69 6e 20 70 2d 3e 74 7a 20 61 6e 64 20 72 65 74  in p->tz and ret
7a650 75 72 6e 20 30 2e 20 20 49 66 20 61 20 70 61 72  urn 0.  If a par
7a660 73 65 72 20 65 72 72 6f 72 20 6f 63 63 75 72 73  ser error occurs
7a670 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f 6e 2d  ,.** return non-
7a680 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 69  zero..**.** A mi
7a690 73 73 69 6e 67 20 73 70 65 63 69 66 69 65 72 20  ssing specifier 
7a6a0 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65 72 65  is not considere
7a6b0 64 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73  d an error..*/.s
7a6c0 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 54  tatic int parseT
7a6d0 69 6d 65 7a 6f 6e 65 28 63 6f 6e 73 74 20 63 68  imezone(const ch
7a6e0 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54  ar *zDate, DateT
7a6f0 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 73  ime *p){.  int s
7a700 67 6e 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 48  gn = 0;.  int nH
7a710 72 2c 20 6e 4d 6e 3b 0a 20 20 69 6e 74 20 63 3b  r, nMn;.  int c;
7a720 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65  .  while( sqlite
7a730 33 49 73 73 70 61 63 65 28 2a 7a 44 61 74 65 29  3Isspace(*zDate)
7a740 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20   ){ zDate++; }. 
7a750 20 70 2d 3e 74 7a 20 3d 20 30 3b 0a 20 20 63 20   p->tz = 0;.  c 
7a760 3d 20 2a 7a 44 61 74 65 3b 0a 20 20 69 66 28 20  = *zDate;.  if( 
7a770 63 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 73 67  c=='-' ){.    sg
7a780 6e 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 20  n = -1;.  }else 
7a790 69 66 28 20 63 3d 3d 27 2b 27 20 29 7b 0a 20 20  if( c=='+' ){.  
7a7a0 20 20 73 67 6e 20 3d 20 2b 31 3b 0a 20 20 7d 65    sgn = +1;.  }e
7a7b0 6c 73 65 20 69 66 28 20 63 3d 3d 27 5a 27 20 7c  lse if( c=='Z' |
7a7c0 7c 20 63 3d 3d 27 7a 27 20 29 7b 0a 20 20 20 20  | c=='z' ){.    
7a7d0 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 67 6f 74  zDate++;.    got
7a7e0 6f 20 7a 75 6c 75 5f 74 69 6d 65 3b 0a 20 20 7d  o zulu_time;.  }
7a7f0 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
7a800 20 63 21 3d 30 3b 0a 20 20 7d 0a 20 20 7a 44 61   c!=0;.  }.  zDa
7a810 74 65 2b 2b 3b 0a 20 20 69 66 28 20 67 65 74 44  te++;.  if( getD
7a820 69 67 69 74 73 28 7a 44 61 74 65 2c 20 32 2c 20  igits(zDate, 2, 
7a830 30 2c 20 31 34 2c 20 27 3a 27 2c 20 26 6e 48 72  0, 14, ':', &nHr
7a840 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26  , 2, 0, 59, 0, &
7a850 6e 4d 6e 29 21 3d 32 20 29 7b 0a 20 20 20 20 72  nMn)!=2 ){.    r
7a860 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a  eturn 1;.  }.  z
7a870 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20 70 2d 3e  Date += 5;.  p->
7a880 74 7a 20 3d 20 73 67 6e 2a 28 6e 4d 6e 20 2b 20  tz = sgn*(nMn + 
7a890 6e 48 72 2a 36 30 29 3b 0a 7a 75 6c 75 5f 74 69  nHr*60);.zulu_ti
7a8a0 6d 65 3a 0a 20 20 77 68 69 6c 65 28 20 73 71 6c  me:.  while( sql
7a8b0 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a 44 61  ite3Isspace(*zDa
7a8c0 74 65 29 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20  te) ){ zDate++; 
7a8d0 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a 44 61 74  }.  return *zDat
7a8e0 65 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  e!=0;.}../*.** P
7a8f0 61 72 73 65 20 74 69 6d 65 73 20 6f 66 20 74 68  arse times of th
7a900 65 20 66 6f 72 6d 20 48 48 3a 4d 4d 20 6f 72 20  e form HH:MM or 
7a910 48 48 3a 4d 4d 3a 53 53 20 6f 72 20 48 48 3a 4d  HH:MM:SS or HH:M
7a920 4d 3a 53 53 2e 46 46 46 46 2e 0a 2a 2a 20 54 68  M:SS.FFFF..** Th
7a930 65 20 48 48 2c 20 4d 4d 2c 20 61 6e 64 20 53 53  e HH, MM, and SS
7a940 20 6d 75 73 74 20 65 61 63 68 20 62 65 20 65 78   must each be ex
7a950 61 63 74 6c 79 20 32 20 64 69 67 69 74 73 2e 20  actly 2 digits. 
7a960 20 54 68 65 0a 2a 2a 20 66 72 61 63 74 69 6f 6e   The.** fraction
7a970 61 6c 20 73 65 63 6f 6e 64 73 20 46 46 46 46 20  al seconds FFFF 
7a980 63 61 6e 20 62 65 20 6f 6e 65 20 6f 72 20 6d 6f  can be one or mo
7a990 72 65 20 64 69 67 69 74 73 2e 0a 2a 2a 0a 2a 2a  re digits..**.**
7a9a0 20 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65   Return 1 if the
7a9b0 72 65 20 69 73 20 61 20 70 61 72 73 69 6e 67 20  re is a parsing 
7a9c0 65 72 72 6f 72 20 61 6e 64 20 30 20 6f 6e 20 73  error and 0 on s
7a9d0 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 74 69  uccess..*/.stati
7a9e0 63 20 69 6e 74 20 70 61 72 73 65 48 68 4d 6d 53  c int parseHhMmS
7a9f0 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  s(const char *zD
7aa00 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70  ate, DateTime *p
7aa10 29 7b 0a 20 20 69 6e 74 20 68 2c 20 6d 2c 20 73  ){.  int h, m, s
7aa20 3b 0a 20 20 64 6f 75 62 6c 65 20 6d 73 20 3d 20  ;.  double ms = 
7aa30 30 2e 30 3b 0a 20 20 69 66 28 20 67 65 74 44 69  0.0;.  if( getDi
7aa40 67 69 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30  gits(zDate, 2, 0
7aa50 2c 20 32 34 2c 20 27 3a 27 2c 20 26 68 2c 20 32  , 24, ':', &h, 2
7aa60 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 6d 29 21  , 0, 59, 0, &m)!
7aa70 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =2 ){.    return
7aa80 20 31 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 20   1;.  }.  zDate 
7aa90 2b 3d 20 35 3b 0a 20 20 69 66 28 20 2a 7a 44 61  += 5;.  if( *zDa
7aaa0 74 65 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 7a  te==':' ){.    z
7aab0 44 61 74 65 2b 2b 3b 0a 20 20 20 20 69 66 28 20  Date++;.    if( 
7aac0 67 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c  getDigits(zDate,
7aad0 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 73   2, 0, 59, 0, &s
7aae0 29 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 72 65  )!=1 ){.      re
7aaf0 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
7ab00 20 20 7a 44 61 74 65 20 2b 3d 20 32 3b 0a 20 20    zDate += 2;.  
7ab10 20 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 27 2e    if( *zDate=='.
7ab20 27 20 26 26 20 73 71 6c 69 74 65 33 49 73 64 69  ' && sqlite3Isdi
7ab30 67 69 74 28 7a 44 61 74 65 5b 31 5d 29 20 29 7b  git(zDate[1]) ){
7ab40 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 53  .      double rS
7ab50 63 61 6c 65 20 3d 20 31 2e 30 3b 0a 20 20 20 20  cale = 1.0;.    
7ab60 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 20    zDate++;.     
7ab70 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49   while( sqlite3I
7ab80 73 64 69 67 69 74 28 2a 7a 44 61 74 65 29 20 29  sdigit(*zDate) )
7ab90 7b 0a 20 20 20 20 20 20 20 20 6d 73 20 3d 20 6d  {.        ms = m
7aba0 73 2a 31 30 2e 30 20 2b 20 2a 7a 44 61 74 65 20  s*10.0 + *zDate 
7abb0 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 72  - '0';.        r
7abc0 53 63 61 6c 65 20 2a 3d 20 31 30 2e 30 3b 0a 20  Scale *= 10.0;. 
7abd0 20 20 20 20 20 20 20 7a 44 61 74 65 2b 2b 3b 0a         zDate++;.
7abe0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 73        }.      ms
7abf0 20 2f 3d 20 72 53 63 61 6c 65 3b 0a 20 20 20 20   /= rScale;.    
7ac00 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  }.  }else{.    s
7ac10 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76   = 0;.  }.  p->v
7ac20 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 70 2d  alidJD = 0;.  p-
7ac30 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20  >validHMS = 1;. 
7ac40 20 70 2d 3e 68 20 3d 20 68 3b 0a 20 20 70 2d 3e   p->h = h;.  p->
7ac50 6d 20 3d 20 6d 3b 0a 20 20 70 2d 3e 73 20 3d 20  m = m;.  p->s = 
7ac60 73 20 2b 20 6d 73 3b 0a 20 20 69 66 28 20 70 61  s + ms;.  if( pa
7ac70 72 73 65 54 69 6d 65 7a 6f 6e 65 28 7a 44 61 74  rseTimezone(zDat
7ac80 65 2c 20 70 29 20 29 20 72 65 74 75 72 6e 20 31  e, p) ) return 1
7ac90 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d  ;.  p->validTZ =
7aca0 20 28 70 2d 3e 74 7a 21 3d 30 29 3f 31 3a 30 3b   (p->tz!=0)?1:0;
7acb0 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
7acc0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 66 72  /*.** Convert fr
7acd0 6f 6d 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48  om YYYY-MM-DD HH
7ace0 3a 4d 4d 3a 53 53 20 74 6f 20 6a 75 6c 69 61 6e  :MM:SS to julian
7acf0 20 64 61 79 2e 20 20 57 65 20 61 6c 77 61 79 73   day.  We always
7ad00 20 61 73 73 75 6d 65 0a 2a 2a 20 74 68 61 74 20   assume.** that 
7ad10 74 68 65 20 59 59 59 59 2d 4d 4d 2d 44 44 20 69  the YYYY-MM-DD i
7ad20 73 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74  s according to t
7ad30 68 65 20 47 72 65 67 6f 72 69 61 6e 20 63 61 6c  he Gregorian cal
7ad40 65 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 66  endar..**.** Ref
7ad50 65 72 65 6e 63 65 3a 20 20 4d 65 65 75 73 20 70  erence:  Meeus p
7ad60 61 67 65 20 36 31 0a 2a 2f 0a 73 74 61 74 69 63  age 61.*/.static
7ad70 20 76 6f 69 64 20 63 6f 6d 70 75 74 65 4a 44 28   void computeJD(
7ad80 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20  DateTime *p){.  
7ad90 69 6e 74 20 59 2c 20 4d 2c 20 44 2c 20 41 2c 20  int Y, M, D, A, 
7ada0 42 2c 20 58 31 2c 20 58 32 3b 0a 0a 20 20 69 66  B, X1, X2;..  if
7adb0 28 20 70 2d 3e 76 61 6c 69 64 4a 44 20 29 20 72  ( p->validJD ) r
7adc0 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e  eturn;.  if( p->
7add0 76 61 6c 69 64 59 4d 44 20 29 7b 0a 20 20 20 20  validYMD ){.    
7ade0 59 20 3d 20 70 2d 3e 59 3b 0a 20 20 20 20 4d 20  Y = p->Y;.    M 
7adf0 3d 20 70 2d 3e 4d 3b 0a 20 20 20 20 44 20 3d 20  = p->M;.    D = 
7ae00 70 2d 3e 44 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  p->D;.  }else{. 
7ae10 20 20 20 59 20 3d 20 32 30 30 30 3b 20 20 2f 2a     Y = 2000;  /*
7ae20 20 49 66 20 6e 6f 20 59 4d 44 20 73 70 65 63 69   If no YMD speci
7ae30 66 69 65 64 2c 20 61 73 73 75 6d 65 20 32 30 30  fied, assume 200
7ae40 30 2d 4a 61 6e 2d 30 31 20 2a 2f 0a 20 20 20 20  0-Jan-01 */.    
7ae50 4d 20 3d 20 31 3b 0a 20 20 20 20 44 20 3d 20 31  M = 1;.    D = 1
7ae60 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4d 3c 3d 32  ;.  }.  if( M<=2
7ae70 20 29 7b 0a 20 20 20 20 59 2d 2d 3b 0a 20 20 20   ){.    Y--;.   
7ae80 20 4d 20 2b 3d 20 31 32 3b 0a 20 20 7d 0a 20 20   M += 12;.  }.  
7ae90 41 20 3d 20 59 2f 31 30 30 3b 0a 20 20 42 20 3d  A = Y/100;.  B =
7aea0 20 32 20 2d 20 41 20 2b 20 28 41 2f 34 29 3b 0a   2 - A + (A/4);.
7aeb0 20 20 58 31 20 3d 20 33 36 35 32 35 2a 28 59 2b    X1 = 36525*(Y+
7aec0 34 37 31 36 29 2f 31 30 30 3b 0a 20 20 58 32 20  4716)/100;.  X2 
7aed0 3d 20 33 30 36 30 30 31 2a 28 4d 2b 31 29 2f 31  = 306001*(M+1)/1
7aee0 30 30 30 30 3b 0a 20 20 70 2d 3e 69 4a 44 20 3d  0000;.  p->iJD =
7aef0 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29   (sqlite3_int64)
7af00 28 28 58 31 20 2b 20 58 32 20 2b 20 44 20 2b 20  ((X1 + X2 + D + 
7af10 42 20 2d 20 31 35 32 34 2e 35 20 29 20 2a 20 38  B - 1524.5 ) * 8
7af20 36 34 30 30 30 30 30 29 3b 0a 20 20 70 2d 3e 76  6400000);.  p->v
7af30 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20 20 69 66  alidJD = 1;.  if
7af40 28 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20 29 7b  ( p->validHMS ){
7af50 0a 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 70  .    p->iJD += p
7af60 2d 3e 68 2a 33 36 30 30 30 30 30 20 2b 20 70 2d  ->h*3600000 + p-
7af70 3e 6d 2a 36 30 30 30 30 20 2b 20 28 73 71 6c 69  >m*60000 + (sqli
7af80 74 65 33 5f 69 6e 74 36 34 29 28 70 2d 3e 73 2a  te3_int64)(p->s*
7af90 31 30 30 30 29 3b 0a 20 20 20 20 69 66 28 20 70  1000);.    if( p
7afa0 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20  ->validTZ ){.   
7afb0 20 20 20 70 2d 3e 69 4a 44 20 2d 3d 20 70 2d 3e     p->iJD -= p->
7afc0 74 7a 2a 36 30 30 30 30 3b 0a 20 20 20 20 20 20  tz*60000;.      
7afd0 70 2d 3e 76 61 6c 69 64 59 4d 44 20 3d 20 30 3b  p->validYMD = 0;
7afe0 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 48  .      p->validH
7aff0 4d 53 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d  MS = 0;.      p-
7b000 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20  >validTZ = 0;.  
7b010 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
7b020 20 50 61 72 73 65 20 64 61 74 65 73 20 6f 66 20   Parse dates of 
7b030 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20  the form.**.**  
7b040 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48     YYYY-MM-DD HH
7b050 3a 4d 4d 3a 53 53 2e 46 46 46 0a 2a 2a 20 20 20  :MM:SS.FFF.**   
7b060 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a    YYYY-MM-DD HH:
7b070 4d 4d 3a 53 53 0a 2a 2a 20 20 20 20 20 59 59 59  MM:SS.**     YYY
7b080 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 0a 2a 2a  Y-MM-DD HH:MM.**
7b090 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 0a       YYYY-MM-DD.
7b0a0 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20  **.** Write the 
7b0b0 72 65 73 75 6c 74 20 69 6e 74 6f 20 74 68 65 20  result into the 
7b0c0 44 61 74 65 54 69 6d 65 20 73 74 72 75 63 74 75  DateTime structu
7b0d0 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20 30 0a  re and return 0.
7b0e0 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e  ** on success an
7b0f0 64 20 31 20 69 66 20 74 68 65 20 69 6e 70 75 74  d 1 if the input
7b100 20 73 74 72 69 6e 67 20 69 73 20 6e 6f 74 20 61   string is not a
7b110 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20   well-formed.** 
7b120 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  date..*/.static 
7b130 69 6e 74 20 70 61 72 73 65 59 79 79 79 4d 6d 44  int parseYyyyMmD
7b140 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  d(const char *zD
7b150 61 74 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70  ate, DateTime *p
7b160 29 7b 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44  ){.  int Y, M, D
7b170 2c 20 6e 65 67 3b 0a 0a 20 20 69 66 28 20 7a 44  , neg;..  if( zD
7b180 61 74 65 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20  ate[0]=='-' ){. 
7b190 20 20 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20     zDate++;.    
7b1a0 6e 65 67 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  neg = 1;.  }else
7b1b0 7b 0a 20 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20  {.    neg = 0;. 
7b1c0 20 7d 0a 20 20 69 66 28 20 67 65 74 44 69 67 69   }.  if( getDigi
7b1d0 74 73 28 7a 44 61 74 65 2c 34 2c 30 2c 39 39 39  ts(zDate,4,0,999
7b1e0 39 2c 27 2d 27 2c 26 59 2c 32 2c 31 2c 31 32 2c  9,'-',&Y,2,1,12,
7b1f0 27 2d 27 2c 26 4d 2c 32 2c 31 2c 33 31 2c 30 2c  '-',&M,2,1,31,0,
7b200 26 44 29 21 3d 33 20 29 7b 0a 20 20 20 20 72 65  &D)!=3 ){.    re
7b210 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a 44  turn 1;.  }.  zD
7b220 61 74 65 20 2b 3d 20 31 30 3b 0a 20 20 77 68 69  ate += 10;.  whi
7b230 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73 70 61  le( sqlite3Isspa
7b240 63 65 28 2a 7a 44 61 74 65 29 20 7c 7c 20 27 54  ce(*zDate) || 'T
7b250 27 3d 3d 2a 28 75 38 2a 29 7a 44 61 74 65 20 29  '==*(u8*)zDate )
7b260 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20 20 69  { zDate++; }.  i
7b270 66 28 20 70 61 72 73 65 48 68 4d 6d 53 73 28 7a  f( parseHhMmSs(z
7b280 44 61 74 65 2c 20 70 29 3d 3d 30 20 29 7b 0a 20  Date, p)==0 ){. 
7b290 20 20 20 2f 2a 20 57 65 20 67 6f 74 20 74 68 65     /* We got the
7b2a0 20 74 69 6d 65 20 2a 2f 0a 20 20 7d 65 6c 73 65   time */.  }else
7b2b0 20 69 66 28 20 2a 7a 44 61 74 65 3d 3d 30 20 29   if( *zDate==0 )
7b2c0 7b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d  {.    p->validHM
7b2d0 53 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  S = 0;.  }else{.
7b2e0 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
7b2f0 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d  }.  p->validJD =
7b300 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d   0;.  p->validYM
7b310 44 20 3d 20 31 3b 0a 20 20 70 2d 3e 59 20 3d 20  D = 1;.  p->Y = 
7b320 6e 65 67 20 3f 20 2d 59 20 3a 20 59 3b 0a 20 20  neg ? -Y : Y;.  
7b330 70 2d 3e 4d 20 3d 20 4d 3b 0a 20 20 70 2d 3e 44  p->M = M;.  p->D
7b340 20 3d 20 44 3b 0a 20 20 69 66 28 20 70 2d 3e 76   = D;.  if( p->v
7b350 61 6c 69 64 54 5a 20 29 7b 0a 20 20 20 20 63 6f  alidTZ ){.    co
7b360 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 7d 0a  mputeJD(p);.  }.
7b370 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
7b380 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 74 69 6d  *.** Set the tim
7b390 65 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74  e to the current
7b3a0 20 74 69 6d 65 20 72 65 70 6f 72 74 65 64 20 62   time reported b
7b3b0 79 20 74 68 65 20 56 46 53 0a 2a 2f 0a 73 74 61  y the VFS.*/.sta
7b3c0 74 69 63 20 76 6f 69 64 20 73 65 74 44 61 74 65  tic void setDate
7b3d0 54 69 6d 65 54 6f 43 75 72 72 65 6e 74 28 73 71  TimeToCurrent(sq
7b3e0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
7b3f0 6f 6e 74 65 78 74 2c 20 44 61 74 65 54 69 6d 65  ontext, DateTime
7b400 20 2a 70 29 7b 0a 20 20 64 6f 75 62 6c 65 20 72   *p){.  double r
7b410 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
7b420 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  = sqlite3_contex
7b430 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
7b440 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f  ext);.  sqlite3O
7b450 73 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d  sCurrentTime(db-
7b460 3e 70 56 66 73 2c 20 26 72 29 3b 0a 20 20 70 2d  >pVfs, &r);.  p-
7b470 3e 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f  >iJD = (sqlite3_
7b480 69 6e 74 36 34 29 28 72 2a 38 36 34 30 30 30 30  int64)(r*8640000
7b490 30 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 70 2d  0.0 + 0.5);.  p-
7b4a0 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 7d 0a  >validJD = 1;.}.
7b4b0 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74  ./*.** Attempt t
7b4c0 6f 20 70 61 72 73 65 20 74 68 65 20 67 69 76 65  o parse the give
7b4d0 6e 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 20  n string into a 
7b4e0 4a 75 6c 69 61 6e 20 44 61 79 20 4e 75 6d 62 65  Julian Day Numbe
7b4f0 72 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68  r.  Return.** th
7b500 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f  e number of erro
7b510 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  rs..**.** The fo
7b520 6c 6c 6f 77 69 6e 67 20 61 72 65 20 61 63 63 65  llowing are acce
7b530 70 74 61 62 6c 65 20 66 6f 72 6d 73 20 66 6f 72  ptable forms for
7b540 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e   the input strin
7b550 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 59  g:.**.**      YY
7b560 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d 4d 3a 53  YY-MM-DD HH:MM:S
7b570 53 2e 46 46 46 20 20 2b 2f 2d 48 48 3a 4d 4d 0a  S.FFF  +/-HH:MM.
7b580 2a 2a 20 20 20 20 20 20 44 44 44 44 2e 44 44 20  **      DDDD.DD 
7b590 0a 2a 2a 20 20 20 20 20 20 6e 6f 77 0a 2a 2a 0a  .**      now.**.
7b5a0 2a 2a 20 49 6e 20 74 68 65 20 66 69 72 73 74 20  ** In the first 
7b5b0 66 6f 72 6d 2c 20 74 68 65 20 2b 2f 2d 48 48 3a  form, the +/-HH:
7b5c0 4d 4d 20 69 73 20 61 6c 77 61 79 73 20 6f 70 74  MM is always opt
7b5d0 69 6f 6e 61 6c 2e 20 20 54 68 65 20 66 72 61 63  ional.  The frac
7b5e0 74 69 6f 6e 61 6c 0a 2a 2a 20 73 65 63 6f 6e 64  tional.** second
7b5f0 73 20 65 78 74 65 6e 73 69 6f 6e 20 28 74 68 65  s extension (the
7b600 20 22 2e 46 46 46 22 29 20 69 73 20 6f 70 74 69   ".FFF") is opti
7b610 6f 6e 61 6c 2e 20 20 54 68 65 20 73 65 63 6f 6e  onal.  The secon
7b620 64 73 20 70 6f 72 74 69 6f 6e 0a 2a 2a 20 28 22  ds portion.** ("
7b630 3a 53 53 2e 46 46 46 22 29 20 69 73 20 6f 70 74  :SS.FFF") is opt
7b640 69 6f 6e 2e 20 20 54 68 65 20 79 65 61 72 20 61  ion.  The year a
7b650 6e 64 20 64 61 74 65 20 63 61 6e 20 62 65 20 6f  nd date can be o
7b660 6d 69 74 74 65 64 20 61 73 20 6c 6f 6e 67 0a 2a  mitted as long.*
7b670 2a 20 61 73 20 74 68 65 72 65 20 69 73 20 61 20  * as there is a 
7b680 74 69 6d 65 20 73 74 72 69 6e 67 2e 20 20 54 68  time string.  Th
7b690 65 20 74 69 6d 65 20 73 74 72 69 6e 67 20 63 61  e time string ca
7b6a0 6e 20 62 65 20 6f 6d 69 74 74 65 64 20 61 73 20  n be omitted as 
7b6b0 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72 65  long.** as there
7b6c0 20 69 73 20 61 20 79 65 61 72 20 61 6e 64 20 64   is a year and d
7b6d0 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ate..*/.static i
7b6e0 6e 74 20 70 61 72 73 65 44 61 74 65 4f 72 54 69  nt parseDateOrTi
7b6f0 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  me(.  sqlite3_co
7b700 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20  ntext *context, 
7b710 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
7b720 44 61 74 65 2c 20 0a 20 20 44 61 74 65 54 69 6d  Date, .  DateTim
7b730 65 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 20 69 73  e *p.){.  int is
7b740 52 65 61 6c 4e 75 6d 3b 20 20 20 20 2f 2a 20 52  RealNum;    /* R
7b750 65 74 75 72 6e 20 66 72 6f 6d 20 73 71 6c 69 74  eturn from sqlit
7b760 65 33 49 73 4e 75 6d 62 65 72 28 29 2e 20 20 4e  e3IsNumber().  N
7b770 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20 69 66 28  ot used */.  if(
7b780 20 70 61 72 73 65 59 79 79 79 4d 6d 44 64 28 7a   parseYyyyMmDd(z
7b790 44 61 74 65 2c 70 29 3d 3d 30 20 29 7b 0a 20 20  Date,p)==0 ){.  
7b7a0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65    return 0;.  }e
7b7b0 6c 73 65 20 69 66 28 20 70 61 72 73 65 48 68 4d  lse if( parseHhM
7b7c0 6d 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30  mSs(zDate, p)==0
7b7d0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
7b7e0 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71  ;.  }else if( sq
7b7f0 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44 61  lite3StrICmp(zDa
7b800 74 65 2c 22 6e 6f 77 22 29 3d 3d 30 29 7b 0a 20  te,"now")==0){. 
7b810 20 20 20 73 65 74 44 61 74 65 54 69 6d 65 54 6f     setDateTimeTo
7b820 43 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74 2c  Current(context,
7b830 20 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20   p);.    return 
7b840 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  0;.  }else if( s
7b850 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 7a  qlite3IsNumber(z
7b860 44 61 74 65 2c 20 26 69 73 52 65 61 6c 4e 75 6d  Date, &isRealNum
7b870 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 20 29  , SQLITE_UTF8) )
7b880 7b 0a 20 20 20 20 64 6f 75 62 6c 65 20 72 3b 0a  {.    double r;.
7b890 20 20 20 20 67 65 74 56 61 6c 75 65 28 7a 44 61      getValue(zDa
7b8a0 74 65 2c 20 26 72 29 3b 0a 20 20 20 20 70 2d 3e  te, &r);.    p->
7b8b0 69 4a 44 20 3d 20 28 73 71 6c 69 74 65 33 5f 69  iJD = (sqlite3_i
7b8c0 6e 74 36 34 29 28 72 2a 38 36 34 30 30 30 30 30  nt64)(r*86400000
7b8d0 2e 30 20 2b 20 30 2e 35 29 3b 0a 20 20 20 20 70  .0 + 0.5);.    p
7b8e0 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31 3b 0a 20  ->validJD = 1;. 
7b8f0 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
7b900 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
7b910 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68  /*.** Compute th
7b920 65 20 59 65 61 72 2c 20 4d 6f 6e 74 68 2c 20 61  e Year, Month, a
7b930 6e 64 20 44 61 79 20 66 72 6f 6d 20 74 68 65 20  nd Day from the 
7b940 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65  julian day numbe
7b950 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  r..*/.static voi
7b960 64 20 63 6f 6d 70 75 74 65 59 4d 44 28 44 61 74  d computeYMD(Dat
7b970 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74  eTime *p){.  int
7b980 20 5a 2c 20 41 2c 20 42 2c 20 43 2c 20 44 2c 20   Z, A, B, C, D, 
7b990 45 2c 20 58 31 3b 0a 20 20 69 66 28 20 70 2d 3e  E, X1;.  if( p->
7b9a0 76 61 6c 69 64 59 4d 44 20 29 20 72 65 74 75 72  validYMD ) retur
7b9b0 6e 3b 0a 20 20 69 66 28 20 21 70 2d 3e 76 61 6c  n;.  if( !p->val
7b9c0 69 64 4a 44 20 29 7b 0a 20 20 20 20 70 2d 3e 59  idJD ){.    p->Y
7b9d0 20 3d 20 32 30 30 30 3b 0a 20 20 20 20 70 2d 3e   = 2000;.    p->
7b9e0 4d 20 3d 20 31 3b 0a 20 20 20 20 70 2d 3e 44 20  M = 1;.    p->D 
7b9f0 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 1;.  }else{.  
7ba00 20 20 5a 20 3d 20 28 69 6e 74 29 28 28 70 2d 3e    Z = (int)((p->
7ba10 69 4a 44 20 2b 20 34 33 32 30 30 30 30 30 29 2f  iJD + 43200000)/
7ba20 38 36 34 30 30 30 30 30 29 3b 0a 20 20 20 20 41  86400000);.    A
7ba30 20 3d 20 28 69 6e 74 29 28 28 5a 20 2d 20 31 38   = (int)((Z - 18
7ba40 36 37 32 31 36 2e 32 35 29 2f 33 36 35 32 34 2e  67216.25)/36524.
7ba50 32 35 29 3b 0a 20 20 20 20 41 20 3d 20 5a 20 2b  25);.    A = Z +
7ba60 20 31 20 2b 20 41 20 2d 20 28 41 2f 34 29 3b 0a   1 + A - (A/4);.
7ba70 20 20 20 20 42 20 3d 20 41 20 2b 20 31 35 32 34      B = A + 1524
7ba80 3b 0a 20 20 20 20 43 20 3d 20 28 69 6e 74 29 28  ;.    C = (int)(
7ba90 28 42 20 2d 20 31 32 32 2e 31 29 2f 33 36 35 2e  (B - 122.1)/365.
7baa0 32 35 29 3b 0a 20 20 20 20 44 20 3d 20 28 33 36  25);.    D = (36
7bab0 35 32 35 2a 43 29 2f 31 30 30 3b 0a 20 20 20 20  525*C)/100;.    
7bac0 45 20 3d 20 28 69 6e 74 29 28 28 42 2d 44 29 2f  E = (int)((B-D)/
7bad0 33 30 2e 36 30 30 31 29 3b 0a 20 20 20 20 58 31  30.6001);.    X1
7bae0 20 3d 20 28 69 6e 74 29 28 33 30 2e 36 30 30 31   = (int)(30.6001
7baf0 2a 45 29 3b 0a 20 20 20 20 70 2d 3e 44 20 3d 20  *E);.    p->D = 
7bb00 42 20 2d 20 44 20 2d 20 58 31 3b 0a 20 20 20 20  B - D - X1;.    
7bb10 70 2d 3e 4d 20 3d 20 45 3c 31 34 20 3f 20 45 2d  p->M = E<14 ? E-
7bb20 31 20 3a 20 45 2d 31 33 3b 0a 20 20 20 20 70 2d  1 : E-13;.    p-
7bb30 3e 59 20 3d 20 70 2d 3e 4d 3e 32 20 3f 20 43 20  >Y = p->M>2 ? C 
7bb40 2d 20 34 37 31 36 20 3a 20 43 20 2d 20 34 37 31  - 4716 : C - 471
7bb50 35 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69  5;.  }.  p->vali
7bb60 64 59 4d 44 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a  dYMD = 1;.}../*.
7bb70 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 48  ** Compute the H
7bb80 6f 75 72 2c 20 4d 69 6e 75 74 65 2c 20 61 6e 64  our, Minute, and
7bb90 20 53 65 63 6f 6e 64 73 20 66 72 6f 6d 20 74 68   Seconds from th
7bba0 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d  e julian day num
7bbb0 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ber..*/.static v
7bbc0 6f 69 64 20 63 6f 6d 70 75 74 65 48 4d 53 28 44  oid computeHMS(D
7bbd0 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69  ateTime *p){.  i
7bbe0 6e 74 20 73 3b 0a 20 20 69 66 28 20 70 2d 3e 76  nt s;.  if( p->v
7bbf0 61 6c 69 64 48 4d 53 20 29 20 72 65 74 75 72 6e  alidHMS ) return
7bc00 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29  ;.  computeJD(p)
7bc10 3b 0a 20 20 73 20 3d 20 28 69 6e 74 29 28 28 70  ;.  s = (int)((p
7bc20 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 30 30 30  ->iJD + 43200000
7bc30 29 20 25 20 38 36 34 30 30 30 30 30 29 3b 0a 20  ) % 86400000);. 
7bc40 20 70 2d 3e 73 20 3d 20 73 2f 31 30 30 30 2e 30   p->s = s/1000.0
7bc50 3b 0a 20 20 73 20 3d 20 28 69 6e 74 29 70 2d 3e  ;.  s = (int)p->
7bc60 73 3b 0a 20 20 70 2d 3e 73 20 2d 3d 20 73 3b 0a  s;.  p->s -= s;.
7bc70 20 20 70 2d 3e 68 20 3d 20 73 2f 33 36 30 30 3b    p->h = s/3600;
7bc80 0a 20 20 73 20 2d 3d 20 70 2d 3e 68 2a 33 36 30  .  s -= p->h*360
7bc90 30 3b 0a 20 20 70 2d 3e 6d 20 3d 20 73 2f 36 30  0;.  p->m = s/60
7bca0 3b 0a 20 20 70 2d 3e 73 20 2b 3d 20 73 20 2d 20  ;.  p->s += s - 
7bcb0 70 2d 3e 6d 2a 36 30 3b 0a 20 20 70 2d 3e 76 61  p->m*60;.  p->va
7bcc0 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 7d 0a 0a 2f  lidHMS = 1;.}../
7bcd0 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 62 6f 74  *.** Compute bot
7bce0 68 20 59 4d 44 20 61 6e 64 20 48 4d 53 0a 2a 2f  h YMD and HMS.*/
7bcf0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 6d  .static void com
7bd00 70 75 74 65 59 4d 44 5f 48 4d 53 28 44 61 74 65  puteYMD_HMS(Date
7bd10 54 69 6d 65 20 2a 70 29 7b 0a 20 20 63 6f 6d 70  Time *p){.  comp
7bd20 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 63 6f 6d  uteYMD(p);.  com
7bd30 70 75 74 65 48 4d 53 28 70 29 3b 0a 7d 0a 0a 2f  puteHMS(p);.}../
7bd40 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 59  *.** Clear the Y
7bd50 4d 44 20 61 6e 64 20 48 4d 53 20 61 6e 64 20 74  MD and HMS and t
7bd60 68 65 20 54 5a 0a 2a 2f 0a 73 74 61 74 69 63 20  he TZ.*/.static 
7bd70 76 6f 69 64 20 63 6c 65 61 72 59 4d 44 5f 48 4d  void clearYMD_HM
7bd80 53 5f 54 5a 28 44 61 74 65 54 69 6d 65 20 2a 70  S_TZ(DateTime *p
7bd90 29 7b 0a 20 20 70 2d 3e 76 61 6c 69 64 59 4d 44  ){.  p->validYMD
7bda0 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64   = 0;.  p->valid
7bdb0 48 4d 53 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61  HMS = 0;.  p->va
7bdc0 6c 69 64 54 5a 20 3d 20 30 3b 0a 7d 0a 0a 23 69  lidTZ = 0;.}..#i
7bdd0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
7bde0 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 2f 2a 0a 2a  T_LOCALTIME./*.*
7bdf0 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 64 69  * Compute the di
7be00 66 66 65 72 65 6e 63 65 20 28 69 6e 20 6d 69 6c  fference (in mil
7be10 6c 69 73 65 63 6f 6e 64 73 29 0a 2a 2a 20 62 65  liseconds).** be
7be20 74 77 65 65 6e 20 6c 6f 63 61 6c 74 69 6d 65 20  tween localtime 
7be30 61 6e 64 20 55 54 43 20 28 61 2e 6b 2e 61 2e 20  and UTC (a.k.a. 
7be40 47 4d 54 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20  GMT).** for the 
7be50 74 69 6d 65 20 76 61 6c 75 65 20 70 20 77 68 65  time value p whe
7be60 72 65 20 70 20 69 73 20 69 6e 20 55 54 43 2e 0a  re p is in UTC..
7be70 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65  */.static sqlite
7be80 33 5f 69 6e 74 36 34 20 6c 6f 63 61 6c 74 69 6d  3_int64 localtim
7be90 65 4f 66 66 73 65 74 28 44 61 74 65 54 69 6d 65  eOffset(DateTime
7bea0 20 2a 70 29 7b 0a 20 20 44 61 74 65 54 69 6d 65   *p){.  DateTime
7beb0 20 78 2c 20 79 3b 0a 20 20 74 69 6d 65 5f 74 20   x, y;.  time_t 
7bec0 74 3b 0a 20 20 78 20 3d 20 2a 70 3b 0a 20 20 63  t;.  x = *p;.  c
7bed0 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 26 78  omputeYMD_HMS(&x
7bee0 29 3b 0a 20 20 69 66 28 20 78 2e 59 3c 31 39 37  );.  if( x.Y<197
7bef0 31 20 7c 7c 20 78 2e 59 3e 3d 32 30 33 38 20 29  1 || x.Y>=2038 )
7bf00 7b 0a 20 20 20 20 78 2e 59 20 3d 20 32 30 30 30  {.    x.Y = 2000
7bf10 3b 0a 20 20 20 20 78 2e 4d 20 3d 20 31 3b 0a 20  ;.    x.M = 1;. 
7bf20 20 20 20 78 2e 44 20 3d 20 31 3b 0a 20 20 20 20     x.D = 1;.    
7bf30 78 2e 68 20 3d 20 30 3b 0a 20 20 20 20 78 2e 6d  x.h = 0;.    x.m
7bf40 20 3d 20 30 3b 0a 20 20 20 20 78 2e 73 20 3d 20   = 0;.    x.s = 
7bf50 30 2e 30 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a  0.0;.  } else {.
7bf60 20 20 20 20 69 6e 74 20 73 20 3d 20 28 69 6e 74      int s = (int
7bf70 29 28 78 2e 73 20 2b 20 30 2e 35 29 3b 0a 20 20  )(x.s + 0.5);.  
7bf80 20 20 78 2e 73 20 3d 20 73 3b 0a 20 20 7d 0a 20    x.s = s;.  }. 
7bf90 20 78 2e 74 7a 20 3d 20 30 3b 0a 20 20 78 2e 76   x.tz = 0;.  x.v
7bfa0 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 63 6f  alidJD = 0;.  co
7bfb0 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20 74  mputeJD(&x);.  t
7bfc0 20 3d 20 28 74 69 6d 65 5f 74 29 28 78 2e 69 4a   = (time_t)(x.iJ
7bfd0 44 2f 31 30 30 30 20 2d 20 32 31 30 38 36 36 37  D/1000 - 2108667
7bfe0 36 2a 28 69 36 34 29 31 30 30 30 30 29 3b 0a 23  6*(i64)10000);.#
7bff0 69 66 64 65 66 20 48 41 56 45 5f 4c 4f 43 41 4c  ifdef HAVE_LOCAL
7c000 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73  TIME_R.  {.    s
7c010 74 72 75 63 74 20 74 6d 20 73 4c 6f 63 61 6c 3b  truct tm sLocal;
7c020 0a 20 20 20 20 6c 6f 63 61 6c 74 69 6d 65 5f 72  .    localtime_r
7c030 28 26 74 2c 20 26 73 4c 6f 63 61 6c 29 3b 0a 20  (&t, &sLocal);. 
7c040 20 20 20 79 2e 59 20 3d 20 73 4c 6f 63 61 6c 2e     y.Y = sLocal.
7c050 74 6d 5f 79 65 61 72 20 2b 20 31 39 30 30 3b 0a  tm_year + 1900;.
7c060 20 20 20 20 79 2e 4d 20 3d 20 73 4c 6f 63 61 6c      y.M = sLocal
7c070 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b 0a 20 20 20  .tm_mon + 1;.   
7c080 20 79 2e 44 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d   y.D = sLocal.tm
7c090 5f 6d 64 61 79 3b 0a 20 20 20 20 79 2e 68 20 3d  _mday;.    y.h =
7c0a0 20 73 4c 6f 63 61 6c 2e 74 6d 5f 68 6f 75 72 3b   sLocal.tm_hour;
7c0b0 0a 20 20 20 20 79 2e 6d 20 3d 20 73 4c 6f 63 61  .    y.m = sLoca
7c0c0 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79 2e  l.tm_min;.    y.
7c0d0 73 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 73 65  s = sLocal.tm_se
7c0e0 63 3b 0a 20 20 7d 0a 23 65 6c 69 66 20 64 65 66  c;.  }.#elif def
7c0f0 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43 41 4c 54  ined(HAVE_LOCALT
7c100 49 4d 45 5f 53 29 20 26 26 20 48 41 56 45 5f 4c  IME_S) && HAVE_L
7c110 4f 43 41 4c 54 49 4d 45 5f 53 0a 20 20 7b 0a 20  OCALTIME_S.  {. 
7c120 20 20 20 73 74 72 75 63 74 20 74 6d 20 73 4c 6f     struct tm sLo
7c130 63 61 6c 3b 0a 20 20 20 20 6c 6f 63 61 6c 74 69  cal;.    localti
7c140 6d 65 5f 73 28 26 73 4c 6f 63 61 6c 2c 20 26 74  me_s(&sLocal, &t
7c150 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20 73 4c 6f  );.    y.Y = sLo
7c160 63 61 6c 2e 74 6d 5f 79 65 61 72 20 2b 20 31 39  cal.tm_year + 19
7c170 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d 20 73 4c  00;.    y.M = sL
7c180 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b 20 31 3b  ocal.tm_mon + 1;
7c190 0a 20 20 20 20 79 2e 44 20 3d 20 73 4c 6f 63 61  .    y.D = sLoca
7c1a0 6c 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20 79  l.tm_mday;.    y
7c1b0 2e 68 20 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 68  .h = sLocal.tm_h
7c1c0 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 73  our;.    y.m = s
7c1d0 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b 0a 20 20  Local.tm_min;.  
7c1e0 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61 6c 2e 74    y.s = sLocal.t
7c1f0 6d 5f 73 65 63 3b 0a 20 20 7d 0a 23 65 6c 73 65  m_sec;.  }.#else
7c200 0a 20 20 7b 0a 20 20 20 20 73 74 72 75 63 74 20  .  {.    struct 
7c210 74 6d 20 2a 70 54 6d 3b 0a 20 20 20 20 73 71 6c  tm *pTm;.    sql
7c220 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
7c230 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c  (sqlite3MutexAll
7c240 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
7c250 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b  STATIC_MASTER));
7c260 0a 20 20 20 20 70 54 6d 20 3d 20 6c 6f 63 61 6c  .    pTm = local
7c270 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 79 2e  time(&t);.    y.
7c280 59 20 3d 20 70 54 6d 2d 3e 74 6d 5f 79 65 61 72  Y = pTm->tm_year
7c290 20 2b 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d   + 1900;.    y.M
7c2a0 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 6f 6e 20 2b   = pTm->tm_mon +
7c2b0 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20 70 54   1;.    y.D = pT
7c2c0 6d 2d 3e 74 6d 5f 6d 64 61 79 3b 0a 20 20 20 20  m->tm_mday;.    
7c2d0 79 2e 68 20 3d 20 70 54 6d 2d 3e 74 6d 5f 68 6f  y.h = pTm->tm_ho
7c2e0 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d 20 70 54  ur;.    y.m = pT
7c2f0 6d 2d 3e 74 6d 5f 6d 69 6e 3b 0a 20 20 20 20 79  m->tm_min;.    y
7c300 2e 73 20 3d 20 70 54 6d 2d 3e 74 6d 5f 73 65 63  .s = pTm->tm_sec
7c310 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
7c320 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65  tex_leave(sqlite
7c330 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49  3MutexAlloc(SQLI
7c340 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
7c350 4d 41 53 54 45 52 29 29 3b 0a 20 20 7d 0a 23 65  MASTER));.  }.#e
7c360 6e 64 69 66 0a 20 20 79 2e 76 61 6c 69 64 59 4d  ndif.  y.validYM
7c370 44 20 3d 20 31 3b 0a 20 20 79 2e 76 61 6c 69 64  D = 1;.  y.valid
7c380 48 4d 53 20 3d 20 31 3b 0a 20 20 79 2e 76 61 6c  HMS = 1;.  y.val
7c390 69 64 4a 44 20 3d 20 30 3b 0a 20 20 79 2e 76 61  idJD = 0;.  y.va
7c3a0 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 63 6f 6d  lidTZ = 0;.  com
7c3b0 70 75 74 65 4a 44 28 26 79 29 3b 0a 20 20 72 65  puteJD(&y);.  re
7c3c0 74 75 72 6e 20 79 2e 69 4a 44 20 2d 20 78 2e 69  turn y.iJD - x.i
7c3d0 4a 44 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  JD;.}.#endif /* 
7c3e0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41  SQLITE_OMIT_LOCA
7c3f0 4c 54 49 4d 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  LTIME */../*.** 
7c400 50 72 6f 63 65 73 73 20 61 20 6d 6f 64 69 66 69  Process a modifi
7c410 65 72 20 74 6f 20 61 20 64 61 74 65 2d 74 69 6d  er to a date-tim
7c420 65 20 73 74 61 6d 70 2e 20 20 54 68 65 20 6d 6f  e stamp.  The mo
7c430 64 69 66 69 65 72 73 20 61 72 65 0a 2a 2a 20 61  difiers are.** a
7c440 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
7c450 20 20 20 20 20 4e 4e 4e 20 64 61 79 73 0a 2a 2a       NNN days.**
7c460 20 20 20 20 20 4e 4e 4e 20 68 6f 75 72 73 0a 2a       NNN hours.*
7c470 2a 20 20 20 20 20 4e 4e 4e 20 6d 69 6e 75 74 65  *     NNN minute
7c480 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 2e 4e 4e 4e  s.**     NNN.NNN
7c490 4e 20 73 65 63 6f 6e 64 73 0a 2a 2a 20 20 20 20  N seconds.**    
7c4a0 20 4e 4e 4e 20 6d 6f 6e 74 68 73 0a 2a 2a 20 20   NNN months.**  
7c4b0 20 20 20 4e 4e 4e 20 79 65 61 72 73 0a 2a 2a 20     NNN years.** 
7c4c0 20 20 20 20 73 74 61 72 74 20 6f 66 20 6d 6f 6e      start of mon
7c4d0 74 68 0a 2a 2a 20 20 20 20 20 73 74 61 72 74 20  th.**     start 
7c4e0 6f 66 20 79 65 61 72 0a 2a 2a 20 20 20 20 20 73  of year.**     s
7c4f0 74 61 72 74 20 6f 66 20 77 65 65 6b 0a 2a 2a 20  tart of week.** 
7c500 20 20 20 20 73 74 61 72 74 20 6f 66 20 64 61 79      start of day
7c510 0a 2a 2a 20 20 20 20 20 77 65 65 6b 64 61 79 20  .**     weekday 
7c520 4e 0a 2a 2a 20 20 20 20 20 75 6e 69 78 65 70 6f  N.**     unixepo
7c530 63 68 0a 2a 2a 20 20 20 20 20 6c 6f 63 61 6c 74  ch.**     localt
7c540 69 6d 65 0a 2a 2a 20 20 20 20 20 75 74 63 0a 2a  ime.**     utc.*
7c550 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 30 20 6f 6e  *.** Return 0 on
7c560 20 73 75 63 63 65 73 73 20 61 6e 64 20 31 20 69   success and 1 i
7c570 66 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 6b  f there is any k
7c580 69 6e 64 20 6f 66 20 65 72 72 6f 72 2e 0a 2a 2f  ind of error..*/
7c590 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73  .static int pars
7c5a0 65 4d 6f 64 69 66 69 65 72 28 63 6f 6e 73 74 20  eModifier(const 
7c5b0 63 68 61 72 20 2a 7a 4d 6f 64 2c 20 44 61 74 65  char *zMod, Date
7c5c0 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Time *p){.  int 
7c5d0 72 63 20 3d 20 31 3b 0a 20 20 69 6e 74 20 6e 3b  rc = 1;.  int n;
7c5e0 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a 20 20 63  .  double r;.  c
7c5f0 68 61 72 20 2a 7a 2c 20 7a 42 75 66 5b 33 30 5d  har *z, zBuf[30]
7c600 3b 0a 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20  ;.  z = zBuf;.  
7c610 66 6f 72 28 6e 3d 30 3b 20 6e 3c 41 72 72 61 79  for(n=0; n<Array
7c620 53 69 7a 65 28 7a 42 75 66 29 2d 31 20 26 26 20  Size(zBuf)-1 && 
7c630 7a 4d 6f 64 5b 6e 5d 3b 20 6e 2b 2b 29 7b 0a 20  zMod[n]; n++){. 
7c640 20 20 20 7a 5b 6e 5d 20 3d 20 28 63 68 61 72 29     z[n] = (char)
7c650 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f  sqlite3UpperToLo
7c660 77 65 72 5b 28 75 38 29 7a 4d 6f 64 5b 6e 5d 5d  wer[(u8)zMod[n]]
7c670 3b 0a 20 20 7d 0a 20 20 7a 5b 6e 5d 20 3d 20 30  ;.  }.  z[n] = 0
7c680 3b 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 30 5d  ;.  switch( z[0]
7c690 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   ){.#ifndef SQLI
7c6a0 54 45 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d  TE_OMIT_LOCALTIM
7c6b0 45 0a 20 20 20 20 63 61 73 65 20 27 6c 27 3a 20  E.    case 'l': 
7c6c0 7b 0a 20 20 20 20 20 20 2f 2a 20 20 20 20 6c 6f  {.      /*    lo
7c6d0 63 61 6c 74 69 6d 65 0a 20 20 20 20 20 20 2a 2a  caltime.      **
7c6e0 0a 20 20 20 20 20 20 2a 2a 20 41 73 73 75 6d 69  .      ** Assumi
7c6f0 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  ng the current t
7c700 69 6d 65 20 76 61 6c 75 65 20 69 73 20 55 54 43  ime value is UTC
7c710 20 28 61 2e 6b 2e 61 2e 20 47 4d 54 29 2c 20 73   (a.k.a. GMT), s
7c720 68 69 66 74 20 69 74 20 74 6f 0a 20 20 20 20 20  hift it to.     
7c730 20 2a 2a 20 73 68 6f 77 20 6c 6f 63 61 6c 20 74   ** show local t
7c740 69 6d 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ime..      */.  
7c750 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a      if( strcmp(z
7c760 2c 20 22 6c 6f 63 61 6c 74 69 6d 65 22 29 3d 3d  , "localtime")==
7c770 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6d  0 ){.        com
7c780 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20  puteJD(p);.     
7c790 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 6c 6f 63     p->iJD += loc
7c7a0 61 6c 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b  altimeOffset(p);
7c7b0 0a 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d  .        clearYM
7c7c0 44 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20  D_HMS_TZ(p);.   
7c7d0 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20       rc = 0;.   
7c7e0 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
7c7f0 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
7c800 20 20 20 63 61 73 65 20 27 75 27 3a 20 7b 0a 20     case 'u': {. 
7c810 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a       /*.      **
7c820 20 20 20 20 75 6e 69 78 65 70 6f 63 68 0a 20 20      unixepoch.  
7c830 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
7c840 54 72 65 61 74 20 74 68 65 20 63 75 72 72 65 6e  Treat the curren
7c850 74 20 76 61 6c 75 65 20 6f 66 20 70 2d 3e 69 4a  t value of p->iJ
7c860 44 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20  D as the number 
7c870 6f 66 0a 20 20 20 20 20 20 2a 2a 20 73 65 63 6f  of.      ** seco
7c880 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 2e 20  nds since 1970. 
7c890 20 43 6f 6e 76 65 72 74 20 74 6f 20 61 20 72 65   Convert to a re
7c8a0 61 6c 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75  al julian day nu
7c8b0 6d 62 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  mber..      */. 
7c8c0 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28       if( strcmp(
7c8d0 7a 2c 20 22 75 6e 69 78 65 70 6f 63 68 22 29 3d  z, "unixepoch")=
7c8e0 3d 30 20 26 26 20 70 2d 3e 76 61 6c 69 64 4a 44  =0 && p->validJD
7c8f0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69   ){.        p->i
7c900 4a 44 20 3d 20 28 70 2d 3e 69 4a 44 20 2b 20 34  JD = (p->iJD + 4
7c910 33 32 30 30 29 2f 38 36 34 30 30 20 2b 20 32 31  3200)/86400 + 21
7c920 30 38 36 36 37 36 2a 28 69 36 34 29 31 30 30 30  086676*(i64)1000
7c930 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20 63 6c  0000;.        cl
7c940 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29  earYMD_HMS_TZ(p)
7c950 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30  ;.        rc = 0
7c960 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65  ;.      }.#ifnde
7c970 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
7c980 43 41 4c 54 49 4d 45 0a 20 20 20 20 20 20 65 6c  CALTIME.      el
7c990 73 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c  se if( strcmp(z,
7c9a0 20 22 75 74 63 22 29 3d 3d 30 20 29 7b 0a 20 20   "utc")==0 ){.  
7c9b0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e        sqlite3_in
7c9c0 74 36 34 20 63 31 3b 0a 20 20 20 20 20 20 20 20  t64 c1;.        
7c9d0 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20  computeJD(p);.  
7c9e0 20 20 20 20 20 20 63 31 20 3d 20 6c 6f 63 61 6c        c1 = local
7c9f0 74 69 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a 20  timeOffset(p);. 
7ca00 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20 2d 3d         p->iJD -=
7ca10 20 63 31 3b 0a 20 20 20 20 20 20 20 20 63 6c 65   c1;.        cle
7ca20 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28 70 29 3b  arYMD_HMS_TZ(p);
7ca30 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20  .        p->iJD 
7ca40 2b 3d 20 63 31 20 2d 20 6c 6f 63 61 6c 74 69 6d  += c1 - localtim
7ca50 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 20 20 20  eOffset(p);.    
7ca60 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20      rc = 0;.    
7ca70 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
7ca80 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
7ca90 20 20 63 61 73 65 20 27 77 27 3a 20 7b 0a 20 20    case 'w': {.  
7caa0 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a 20      /*.      ** 
7cab0 20 20 20 77 65 65 6b 64 61 79 20 4e 0a 20 20 20     weekday N.   
7cac0 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4d     **.      ** M
7cad0 6f 76 65 20 74 68 65 20 64 61 74 65 20 74 6f 20  ove the date to 
7cae0 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20 6f 6e  the same time on
7caf0 20 74 68 65 20 6e 65 78 74 20 6f 63 63 75 72 72   the next occurr
7cb00 65 6e 63 65 20 6f 66 0a 20 20 20 20 20 20 2a 2a  ence of.      **
7cb10 20 77 65 65 6b 64 61 79 20 4e 20 77 68 65 72 65   weekday N where
7cb20 20 30 3d 3d 53 75 6e 64 61 79 2c 20 31 3d 3d 4d   0==Sunday, 1==M
7cb30 6f 6e 64 61 79 2c 20 61 6e 64 20 73 6f 20 66 6f  onday, and so fo
7cb40 72 74 68 2e 20 20 49 66 20 74 68 65 0a 20 20 20  rth.  If the.   
7cb50 20 20 20 2a 2a 20 64 61 74 65 20 69 73 20 61 6c     ** date is al
7cb60 72 65 61 64 79 20 6f 6e 20 74 68 65 20 61 70 70  ready on the app
7cb70 72 6f 70 72 69 61 74 65 20 77 65 65 6b 64 61 79  ropriate weekday
7cb80 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f  , this is a no-o
7cb90 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  p..      */.    
7cba0 20 20 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c    if( strncmp(z,
7cbb0 20 22 77 65 65 6b 64 61 79 20 22 2c 20 38 29 3d   "weekday ", 8)=
7cbc0 3d 30 20 26 26 20 67 65 74 56 61 6c 75 65 28 26  =0 && getValue(&
7cbd0 7a 5b 38 5d 2c 26 72 29 3e 30 0a 20 20 20 20 20  z[8],&r)>0.     
7cbe0 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28              && (
7cbf0 6e 3d 28 69 6e 74 29 72 29 3d 3d 72 20 26 26 20  n=(int)r)==r && 
7cc00 6e 3e 3d 30 20 26 26 20 72 3c 37 20 29 7b 0a 20  n>=0 && r<7 ){. 
7cc10 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69         sqlite3_i
7cc20 6e 74 36 34 20 5a 3b 0a 20 20 20 20 20 20 20 20  nt64 Z;.        
7cc30 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70  computeYMD_HMS(p
7cc40 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61  );.        p->va
7cc50 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20 20 20 20  lidTZ = 0;.     
7cc60 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20     p->validJD = 
7cc70 30 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75  0;.        compu
7cc80 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20  teJD(p);.       
7cc90 20 5a 20 3d 20 28 28 70 2d 3e 69 4a 44 20 2b 20   Z = ((p->iJD + 
7cca0 31 32 39 36 30 30 30 30 30 29 2f 38 36 34 30 30  129600000)/86400
7ccb0 30 30 30 29 20 25 20 37 3b 0a 20 20 20 20 20 20  000) % 7;.      
7ccc0 20 20 69 66 28 20 5a 3e 6e 20 29 20 5a 20 2d 3d    if( Z>n ) Z -=
7ccd0 20 37 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69   7;.        p->i
7cce0 4a 44 20 2b 3d 20 28 6e 20 2d 20 5a 29 2a 38 36  JD += (n - Z)*86
7ccf0 34 30 30 30 30 30 3b 0a 20 20 20 20 20 20 20 20  400000;.        
7cd00 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28  clearYMD_HMS_TZ(
7cd10 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  p);.        rc =
7cd20 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
7cd30 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
7cd40 20 20 20 63 61 73 65 20 27 73 27 3a 20 7b 0a 20     case 's': {. 
7cd50 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a       /*.      **
7cd60 20 20 20 20 73 74 61 72 74 20 6f 66 20 54 54 54      start of TTT
7cd70 54 54 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  TT.      **.    
7cd80 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65 20 64 61    ** Move the da
7cd90 74 65 20 62 61 63 6b 77 61 72 64 73 20 74 6f 20  te backwards to 
7cda0 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
7cdb0 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 79   the current day
7cdc0 2c 0a 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d 6f  ,.      ** or mo
7cdd0 6e 74 68 20 6f 72 20 79 65 61 72 2e 0a 20 20 20  nth or year..   
7cde0 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
7cdf0 73 74 72 6e 63 6d 70 28 7a 2c 20 22 73 74 61 72  strncmp(z, "star
7ce00 74 20 6f 66 20 22 2c 20 39 29 21 3d 30 20 29 20  t of ", 9)!=0 ) 
7ce10 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7a 20 2b  break;.      z +
7ce20 3d 20 39 3b 0a 20 20 20 20 20 20 63 6f 6d 70 75  = 9;.      compu
7ce30 74 65 59 4d 44 28 70 29 3b 0a 20 20 20 20 20 20  teYMD(p);.      
7ce40 70 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b  p->validHMS = 1;
7ce50 0a 20 20 20 20 20 20 70 2d 3e 68 20 3d 20 70 2d  .      p->h = p-
7ce60 3e 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d  >m = 0;.      p-
7ce70 3e 73 20 3d 20 30 2e 30 3b 0a 20 20 20 20 20 20  >s = 0.0;.      
7ce80 70 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a  p->validTZ = 0;.
7ce90 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44        p->validJD
7cea0 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20   = 0;.      if( 
7ceb0 73 74 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22  strcmp(z,"month"
7cec0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
7ced0 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 20 20 20  p->D = 1;.      
7cee0 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20    rc = 0;.      
7cef0 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70  }else if( strcmp
7cf00 28 7a 2c 22 79 65 61 72 22 29 3d 3d 30 20 29 7b  (z,"year")==0 ){
7cf10 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65  .        compute
7cf20 59 4d 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20  YMD(p);.        
7cf30 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20  p->M = 1;.      
7cf40 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 20 20    p->D = 1;.    
7cf50 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20      rc = 0;.    
7cf60 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63    }else if( strc
7cf70 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d 30 20 29  mp(z,"day")==0 )
7cf80 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30  {.        rc = 0
7cf90 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
7cfa0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
7cfb0 20 63 61 73 65 20 27 2b 27 3a 0a 20 20 20 20 63   case '+':.    c
7cfc0 61 73 65 20 27 2d 27 3a 0a 20 20 20 20 63 61 73  ase '-':.    cas
7cfd0 65 20 27 30 27 3a 0a 20 20 20 20 63 61 73 65 20  e '0':.    case 
7cfe0 27 31 27 3a 0a 20 20 20 20 63 61 73 65 20 27 32  '1':.    case '2
7cff0 27 3a 0a 20 20 20 20 63 61 73 65 20 27 33 27 3a  ':.    case '3':
7d000 0a 20 20 20 20 63 61 73 65 20 27 34 27 3a 0a 20  .    case '4':. 
7d010 20 20 20 63 61 73 65 20 27 35 27 3a 0a 20 20 20     case '5':.   
7d020 20 63 61 73 65 20 27 36 27 3a 0a 20 20 20 20 63   case '6':.    c
7d030 61 73 65 20 27 37 27 3a 0a 20 20 20 20 63 61 73  ase '7':.    cas
7d040 65 20 27 38 27 3a 0a 20 20 20 20 63 61 73 65 20  e '8':.    case 
7d050 27 39 27 3a 20 7b 0a 20 20 20 20 20 20 64 6f 75  '9': {.      dou
7d060 62 6c 65 20 72 52 6f 75 6e 64 65 72 3b 0a 20 20  ble rRounder;.  
7d070 20 20 20 20 6e 20 3d 20 67 65 74 56 61 6c 75 65      n = getValue
7d080 28 7a 2c 20 26 72 29 3b 0a 20 20 20 20 20 20 61  (z, &r);.      a
7d090 73 73 65 72 74 28 20 6e 3e 3d 31 20 29 3b 0a 20  ssert( n>=1 );. 
7d0a0 20 20 20 20 20 69 66 28 20 7a 5b 6e 5d 3d 3d 27       if( z[n]=='
7d0b0 3a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  :' ){.        /*
7d0c0 20 41 20 6d 6f 64 69 66 69 65 72 20 6f 66 20 74   A modifier of t
7d0d0 68 65 20 66 6f 72 6d 20 28 2b 7c 2d 29 48 48 3a  he form (+|-)HH:
7d0e0 4d 4d 3a 53 53 2e 46 46 46 20 61 64 64 73 20 28  MM:SS.FFF adds (
7d0f0 6f 72 20 73 75 62 74 72 61 63 74 73 29 20 74 68  or subtracts) th
7d100 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 70 65  e.        ** spe
7d110 63 69 66 69 65 64 20 6e 75 6d 62 65 72 20 6f 66  cified number of
7d120 20 68 6f 75 72 73 2c 20 6d 69 6e 75 74 65 73 2c   hours, minutes,
7d130 20 73 65 63 6f 6e 64 73 2c 20 61 6e 64 20 66 72   seconds, and fr
7d140 61 63 74 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73  actional seconds
7d150 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 74  .        ** to t
7d160 68 65 20 74 69 6d 65 2e 20 20 54 68 65 20 22 2e  he time.  The ".
7d170 46 46 46 22 20 6d 61 79 20 62 65 20 6f 6d 69 74  FFF" may be omit
7d180 74 65 64 2e 20 20 54 68 65 20 22 3a 53 53 2e 46  ted.  The ":SS.F
7d190 46 46 22 20 6d 61 79 20 62 65 0a 20 20 20 20 20  FF" may be.     
7d1a0 20 20 20 2a 2a 20 6f 6d 69 74 74 65 64 2e 0a 20     ** omitted.. 
7d1b0 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
7d1c0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32    const char *z2
7d1d0 20 3d 20 7a 3b 0a 20 20 20 20 20 20 20 20 44 61   = z;.        Da
7d1e0 74 65 54 69 6d 65 20 74 78 3b 0a 20 20 20 20 20  teTime tx;.     
7d1f0 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34     sqlite3_int64
7d200 20 64 61 79 3b 0a 20 20 20 20 20 20 20 20 69 66   day;.        if
7d210 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67 69  ( !sqlite3Isdigi
7d220 74 28 2a 7a 32 29 20 29 20 7a 32 2b 2b 3b 0a 20  t(*z2) ) z2++;. 
7d230 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 74         memset(&t
7d240 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 74 78 29  x, 0, sizeof(tx)
7d250 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
7d260 61 72 73 65 48 68 4d 6d 53 73 28 7a 32 2c 20 26  arseHhMmSs(z2, &
7d270 74 78 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  tx) ) break;.   
7d280 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26       computeJD(&
7d290 74 78 29 3b 0a 20 20 20 20 20 20 20 20 74 78 2e  tx);.        tx.
7d2a0 69 4a 44 20 2d 3d 20 34 33 32 30 30 30 30 30 3b  iJD -= 43200000;
7d2b0 0a 20 20 20 20 20 20 20 20 64 61 79 20 3d 20 74  .        day = t
7d2c0 78 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 3b 0a  x.iJD/86400000;.
7d2d0 20 20 20 20 20 20 20 20 74 78 2e 69 4a 44 20 2d          tx.iJD -
7d2e0 3d 20 64 61 79 2a 38 36 34 30 30 30 30 30 3b 0a  = day*86400000;.
7d2f0 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b 30 5d          if( z[0]
7d300 3d 3d 27 2d 27 20 29 20 74 78 2e 69 4a 44 20 3d  =='-' ) tx.iJD =
7d310 20 2d 74 78 2e 69 4a 44 3b 0a 20 20 20 20 20 20   -tx.iJD;.      
7d320 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a    computeJD(p);.
7d330 20 20 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44          clearYMD
7d340 5f 48 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20  _HMS_TZ(p);.    
7d350 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 74 78      p->iJD += tx
7d360 2e 69 4a 44 3b 0a 20 20 20 20 20 20 20 20 72 63  .iJD;.        rc
7d370 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72   = 0;.        br
7d380 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
7d390 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20 20 20 20     z += n;.     
7d3a0 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49   while( sqlite3I
7d3b0 73 73 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b  sspace(*z) ) z++
7d3c0 3b 0a 20 20 20 20 20 20 6e 20 3d 20 73 71 6c 69  ;.      n = sqli
7d3d0 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29 3b 0a  te3Strlen30(z);.
7d3e0 20 20 20 20 20 20 69 66 28 20 6e 3e 31 30 20 7c        if( n>10 |
7d3f0 7c 20 6e 3c 33 20 29 20 62 72 65 61 6b 3b 0a 20  | n<3 ) break;. 
7d400 20 20 20 20 20 69 66 28 20 7a 5b 6e 2d 31 5d 3d       if( z[n-1]=
7d410 3d 27 73 27 20 29 7b 20 7a 5b 6e 2d 31 5d 20 3d  ='s' ){ z[n-1] =
7d420 20 30 3b 20 6e 2d 2d 3b 20 7d 0a 20 20 20 20 20   0; n--; }.     
7d430 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20   computeJD(p);. 
7d440 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20       rc = 0;.   
7d450 20 20 20 72 52 6f 75 6e 64 65 72 20 3d 20 72 3c     rRounder = r<
7d460 30 20 3f 20 2d 30 2e 35 20 3a 20 2b 30 2e 35 3b  0 ? -0.5 : +0.5;
7d470 0a 20 20 20 20 20 20 69 66 28 20 6e 3d 3d 33 20  .      if( n==3 
7d480 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 64 61 79  && strcmp(z,"day
7d490 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ")==0 ){.       
7d4a0 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69   p->iJD += (sqli
7d4b0 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34  te3_int64)(r*864
7d4c0 30 30 30 30 30 2e 30 20 2b 20 72 52 6f 75 6e 64  00000.0 + rRound
7d4d0 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  er);.      }else
7d4e0 20 69 66 28 20 6e 3d 3d 34 20 26 26 20 73 74 72   if( n==4 && str
7d4f0 63 6d 70 28 7a 2c 22 68 6f 75 72 22 29 3d 3d 30  cmp(z,"hour")==0
7d500 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69   ){.        p->i
7d510 4a 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69  JD += (sqlite3_i
7d520 6e 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 30  nt64)(r*(8640000
7d530 30 2e 30 2f 32 34 2e 30 29 20 2b 20 72 52 6f 75  0.0/24.0) + rRou
7d540 6e 64 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c  nder);.      }el
7d550 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 26 20 73  se if( n==6 && s
7d560 74 72 63 6d 70 28 7a 2c 22 6d 69 6e 75 74 65 22  trcmp(z,"minute"
7d570 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
7d580 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69 74  p->iJD += (sqlit
7d590 65 33 5f 69 6e 74 36 34 29 28 72 2a 28 38 36 34  e3_int64)(r*(864
7d5a0 30 30 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36 30  00000.0/(24.0*60
7d5b0 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72 29  .0)) + rRounder)
7d5c0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
7d5d0 28 20 6e 3d 3d 36 20 26 26 20 73 74 72 63 6d 70  ( n==6 && strcmp
7d5e0 28 7a 2c 22 73 65 63 6f 6e 64 22 29 3d 3d 30 20  (z,"second")==0 
7d5f0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a  ){.        p->iJ
7d600 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e  D += (sqlite3_in
7d610 74 36 34 29 28 72 2a 28 38 36 34 30 30 30 30 30  t64)(r*(86400000
7d620 2e 30 2f 28 32 34 2e 30 2a 36 30 2e 30 2a 36 30  .0/(24.0*60.0*60
7d630 2e 30 29 29 20 2b 20 72 52 6f 75 6e 64 65 72 29  .0)) + rRounder)
7d640 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
7d650 28 20 6e 3d 3d 35 20 26 26 20 73 74 72 63 6d 70  ( n==5 && strcmp
7d660 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d 3d 30 20 29  (z,"month")==0 )
7d670 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78 2c  {.        int x,
7d680 20 79 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70   y;.        comp
7d690 75 74 65 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20  uteYMD_HMS(p);. 
7d6a0 20 20 20 20 20 20 20 70 2d 3e 4d 20 2b 3d 20 28         p->M += (
7d6b0 69 6e 74 29 72 3b 0a 20 20 20 20 20 20 20 20 78  int)r;.        x
7d6c0 20 3d 20 70 2d 3e 4d 3e 30 20 3f 20 28 70 2d 3e   = p->M>0 ? (p->
7d6d0 4d 2d 31 29 2f 31 32 20 3a 20 28 70 2d 3e 4d 2d  M-1)/12 : (p->M-
7d6e0 31 32 29 2f 31 32 3b 0a 20 20 20 20 20 20 20 20  12)/12;.        
7d6f0 70 2d 3e 59 20 2b 3d 20 78 3b 0a 20 20 20 20 20  p->Y += x;.     
7d700 20 20 20 70 2d 3e 4d 20 2d 3d 20 78 2a 31 32 3b     p->M -= x*12;
7d710 0a 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69  .        p->vali
7d720 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  dJD = 0;.       
7d730 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20   computeJD(p);. 
7d740 20 20 20 20 20 20 20 79 20 3d 20 28 69 6e 74 29         y = (int)
7d750 72 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 79  r;.        if( y
7d760 21 3d 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20  !=r ){.         
7d770 20 70 2d 3e 69 4a 44 20 2b 3d 20 28 73 71 6c 69   p->iJD += (sqli
7d780 74 65 33 5f 69 6e 74 36 34 29 28 28 72 20 2d 20  te3_int64)((r - 
7d790 79 29 2a 33 30 2e 30 2a 38 36 34 30 30 30 30 30  y)*30.0*86400000
7d7a0 2e 30 20 2b 20 72 52 6f 75 6e 64 65 72 29 3b 0a  .0 + rRounder);.
7d7b0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
7d7c0 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20 26  }else if( n==4 &
7d7d0 26 20 73 74 72 63 6d 70 28 7a 2c 22 79 65 61 72  & strcmp(z,"year
7d7e0 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ")==0 ){.       
7d7f0 20 69 6e 74 20 79 20 3d 20 28 69 6e 74 29 72 3b   int y = (int)r;
7d800 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65  .        compute
7d810 59 4d 44 5f 48 4d 53 28 70 29 3b 0a 20 20 20 20  YMD_HMS(p);.    
7d820 20 20 20 20 70 2d 3e 59 20 2b 3d 20 79 3b 0a 20      p->Y += y;. 
7d830 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a         p->validJ
7d840 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 63  D = 0;.        c
7d850 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20  omputeJD(p);.   
7d860 20 20 20 20 20 69 66 28 20 79 21 3d 72 20 29 7b       if( y!=r ){
7d870 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 4a  .          p->iJ
7d880 44 20 2b 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e  D += (sqlite3_in
7d890 74 36 34 29 28 28 72 20 2d 20 79 29 2a 33 36 35  t64)((r - y)*365
7d8a0 2e 30 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20  .0*86400000.0 + 
7d8b0 72 52 6f 75 6e 64 65 72 29 3b 0a 20 20 20 20 20  rRounder);.     
7d8c0 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
7d8d0 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 31  {.        rc = 1
7d8e0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
7d8f0 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a 28  clearYMD_HMS_TZ(
7d900 70 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  p);.      break;
7d910 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
7d920 6c 74 3a 20 7b 0a 20 20 20 20 20 20 62 72 65 61  lt: {.      brea
7d930 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  k;.    }.  }.  r
7d940 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
7d950 2a 2a 20 50 72 6f 63 65 73 73 20 74 69 6d 65 20  ** Process time 
7d960 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e  function argumen
7d970 74 73 2e 20 20 61 72 67 76 5b 30 5d 20 69 73 20  ts.  argv[0] is 
7d980 61 20 64 61 74 65 2d 74 69 6d 65 20 73 74 61 6d  a date-time stam
7d990 70 2e 0a 2a 2a 20 61 72 67 76 5b 31 5d 20 61 6e  p..** argv[1] an
7d9a0 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20  d following are 
7d9b0 6d 6f 64 69 66 69 65 72 73 2e 20 20 50 61 72 73  modifiers.  Pars
7d9c0 65 20 74 68 65 6d 20 61 6c 6c 20 61 6e 64 20 77  e them all and w
7d9d0 72 69 74 65 0a 2a 2a 20 74 68 65 20 72 65 73 75  rite.** the resu
7d9e0 6c 74 69 6e 67 20 74 69 6d 65 20 69 6e 74 6f 20  lting time into 
7d9f0 74 68 65 20 44 61 74 65 54 69 6d 65 20 73 74 72  the DateTime str
7da00 75 63 74 75 72 65 20 70 2e 20 20 52 65 74 75 72  ucture p.  Retur
7da10 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73  n 0.** on succes
7da20 73 20 61 6e 64 20 31 20 69 66 20 74 68 65 72 65  s and 1 if there
7da30 20 61 72 65 20 61 6e 79 20 65 72 72 6f 72 73 2e   are any errors.
7da40 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  .**.** If there 
7da50 61 72 65 20 7a 65 72 6f 20 70 61 72 61 6d 65 74  are zero paramet
7da60 65 72 73 20 28 69 66 20 65 76 65 6e 20 61 72 67  ers (if even arg
7da70 76 5b 30 5d 20 69 73 20 75 6e 64 65 66 69 6e 65  v[0] is undefine
7da80 64 29 0a 2a 2a 20 74 68 65 6e 20 61 73 73 75 6d  d).** then assum
7da90 65 20 61 20 64 65 66 61 75 6c 74 20 76 61 6c 75  e a default valu
7daa0 65 20 6f 66 20 22 6e 6f 77 22 20 66 6f 72 20 61  e of "now" for a
7dab0 72 67 76 5b 30 5d 2e 0a 2a 2f 0a 73 74 61 74 69  rgv[0]..*/.stati
7dac0 63 20 69 6e 74 20 69 73 44 61 74 65 28 0a 20 20  c int isDate(.  
7dad0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
7dae0 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20 69 6e 74  *context, .  int
7daf0 20 61 72 67 63 2c 20 0a 20 20 73 71 6c 69 74 65   argc, .  sqlite
7db00 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 2c 20  3_value **argv, 
7db10 0a 20 20 44 61 74 65 54 69 6d 65 20 2a 70 0a 29  .  DateTime *p.)
7db20 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f 6e  {.  int i;.  con
7db30 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
7db40 20 2a 7a 3b 0a 20 20 69 6e 74 20 65 54 79 70 65   *z;.  int eType
7db50 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c  ;.  memset(p, 0,
7db60 20 73 69 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20   sizeof(*p));.  
7db70 69 66 28 20 61 72 67 63 3d 3d 30 20 29 7b 0a 20  if( argc==0 ){. 
7db80 20 20 20 73 65 74 44 61 74 65 54 69 6d 65 54 6f     setDateTimeTo
7db90 43 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74 2c  Current(context,
7dba0 20 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28   p);.  }else if(
7dbb0 20 28 65 54 79 70 65 20 3d 20 73 71 6c 69 74 65   (eType = sqlite
7dbc0 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
7dbd0 76 5b 30 5d 29 29 3d 3d 53 51 4c 49 54 45 5f 46  v[0]))==SQLITE_F
7dbe0 4c 4f 41 54 0a 20 20 20 20 20 20 20 20 20 20 20  LOAT.           
7dbf0 20 20 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65          || eType
7dc00 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  ==SQLITE_INTEGER
7dc10 20 29 7b 0a 20 20 20 20 70 2d 3e 69 4a 44 20 3d   ){.    p->iJD =
7dc20 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29   (sqlite3_int64)
7dc30 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64  (sqlite3_value_d
7dc40 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 2a 38  ouble(argv[0])*8
7dc50 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29  6400000.0 + 0.5)
7dc60 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44  ;.    p->validJD
7dc70 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 1;.  }else{. 
7dc80 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76     z = sqlite3_v
7dc90 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
7dca0 5d 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 20 7c  ]);.    if( !z |
7dcb0 7c 20 70 61 72 73 65 44 61 74 65 4f 72 54 69 6d  | parseDateOrTim
7dcc0 65 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72  e(context, (char
7dcd0 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20 20 20 20  *)z, p) ){.     
7dce0 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
7dcf0 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 3b 20  .  }.  for(i=1; 
7dd00 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20  i<argc; i++){.  
7dd10 20 20 69 66 28 20 28 7a 20 3d 20 73 71 6c 69 74    if( (z = sqlit
7dd20 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
7dd30 67 76 5b 69 5d 29 29 3d 3d 30 20 7c 7c 20 70 61  gv[i]))==0 || pa
7dd40 72 73 65 4d 6f 64 69 66 69 65 72 28 28 63 68 61  rseModifier((cha
7dd50 72 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20 20 20  r*)z, p) ){.    
7dd60 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
7dd70 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  }.  }.  return 0
7dd80 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ;.}.../*.** The 
7dd90 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e  following routin
7dda0 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  es implement the
7ddb0 20 76 61 72 69 6f 75 73 20 64 61 74 65 20 61 6e   various date an
7ddc0 64 20 74 69 6d 65 20 66 75 6e 63 74 69 6f 6e 73  d time functions
7ddd0 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a  .** of SQLite..*
7dde0 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 6a 75 6c 69  /../*.**    juli
7ddf0 61 6e 64 61 79 28 20 54 49 4d 45 53 54 52 49 4e  anday( TIMESTRIN
7de00 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e  G, MOD, MOD, ...
7de10 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ).**.** Return t
7de20 68 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75  he julian day nu
7de30 6d 62 65 72 20 6f 66 20 74 68 65 20 64 61 74 65  mber of the date
7de40 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68   specified in th
7de50 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2f 0a 73  e arguments.*/.s
7de60 74 61 74 69 63 20 76 6f 69 64 20 6a 75 6c 69 61  tatic void julia
7de70 6e 64 61 79 46 75 6e 63 28 0a 20 20 73 71 6c 69  ndayFunc(.  sqli
7de80 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
7de90 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
7dea0 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
7deb0 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 44 61  e **argv.){.  Da
7dec0 74 65 54 69 6d 65 20 78 3b 0a 20 20 69 66 28 20  teTime x;.  if( 
7ded0 69 73 44 61 74 65 28 63 6f 6e 74 65 78 74 2c 20  isDate(context, 
7dee0 61 72 67 63 2c 20 61 72 67 76 2c 20 26 78 29 3d  argc, argv, &x)=
7def0 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6d 70 75 74  =0 ){.    comput
7df00 65 4a 44 28 26 78 29 3b 0a 20 20 20 20 73 71 6c  eJD(&x);.    sql
7df10 69 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62  ite3_result_doub
7df20 6c 65 28 63 6f 6e 74 65 78 74 2c 20 78 2e 69 4a  le(context, x.iJ
7df30 44 2f 38 36 34 30 30 30 30 30 2e 30 29 3b 0a 20  D/86400000.0);. 
7df40 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 64   }.}../*.**    d
7df50 61 74 65 74 69 6d 65 28 20 54 49 4d 45 53 54 52  atetime( TIMESTR
7df60 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e  ING, MOD, MOD, .
7df70 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ..).**.** Return
7df80 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a 4d   YYYY-MM-DD HH:M
7df90 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76  M:SS.*/.static v
7dfa0 6f 69 64 20 64 61 74 65 74 69 6d 65 46 75 6e 63  oid datetimeFunc
7dfb0 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
7dfc0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
7dfd0 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
7dfe0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
7dff0 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78  .){.  DateTime x
7e000 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63  ;.  if( isDate(c
7e010 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72  ontext, argc, ar
7e020 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20  gv, &x)==0 ){.  
7e030 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d    char zBuf[100]
7e040 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44  ;.    computeYMD
7e050 5f 48 4d 53 28 26 78 29 3b 0a 20 20 20 20 73 71  _HMS(&x);.    sq
7e060 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
7e070 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75  izeof(zBuf), zBu
7e080 66 2c 20 22 25 30 34 64 2d 25 30 32 64 2d 25 30  f, "%04d-%02d-%0
7e090 32 64 20 25 30 32 64 3a 25 30 32 64 3a 25 30 32  2d %02d:%02d:%02
7e0a0 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  d",.            
7e0b0 20 20 20 20 20 20 20 20 20 78 2e 59 2c 20 78 2e           x.Y, x.
7e0c0 4d 2c 20 78 2e 44 2c 20 78 2e 68 2c 20 78 2e 6d  M, x.D, x.h, x.m
7e0d0 2c 20 28 69 6e 74 29 28 78 2e 73 29 29 3b 0a 20  , (int)(x.s));. 
7e0e0 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
7e0f0 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
7e100 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45  zBuf, -1, SQLITE
7e110 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d  _TRANSIENT);.  }
7e120 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 74 69 6d  .}../*.**    tim
7e130 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d  e( TIMESTRING, M
7e140 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a  OD, MOD, ...).**
7e150 0a 2a 2a 20 52 65 74 75 72 6e 20 48 48 3a 4d 4d  .** Return HH:MM
7e160 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  :SS.*/.static vo
7e170 69 64 20 74 69 6d 65 46 75 6e 63 28 0a 20 20 73  id timeFunc(.  s
7e180 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
7e190 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
7e1a0 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
7e1b0 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
7e1c0 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69   DateTime x;.  i
7e1d0 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78  f( isDate(contex
7e1e0 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26  t, argc, argv, &
7e1f0 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61  x)==0 ){.    cha
7e200 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20  r zBuf[100];.   
7e210 20 63 6f 6d 70 75 74 65 48 4d 53 28 26 78 29 3b   computeHMS(&x);
7e220 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
7e230 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 42 75  rintf(sizeof(zBu
7e240 66 29 2c 20 7a 42 75 66 2c 20 22 25 30 32 64 3a  f), zBuf, "%02d:
7e250 25 30 32 64 3a 25 30 32 64 22 2c 20 78 2e 68 2c  %02d:%02d", x.h,
7e260 20 78 2e 6d 2c 20 28 69 6e 74 29 78 2e 73 29 3b   x.m, (int)x.s);
7e270 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
7e280 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
7e290 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49  , zBuf, -1, SQLI
7e2a0 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
7e2b0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 64   }.}../*.**    d
7e2c0 61 74 65 28 20 54 49 4d 45 53 54 52 49 4e 47 2c  ate( TIMESTRING,
7e2d0 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a   MOD, MOD, ...).
7e2e0 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 59 59 59  **.** Return YYY
7e2f0 59 2d 4d 4d 2d 44 44 0a 2a 2f 0a 73 74 61 74 69  Y-MM-DD.*/.stati
7e300 63 20 76 6f 69 64 20 64 61 74 65 46 75 6e 63 28  c void dateFunc(
7e310 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
7e320 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
7e330 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74  nt argc,.  sqlit
7e340 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
7e350 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b  ){.  DateTime x;
7e360 0a 20 20 69 66 28 20 69 73 44 61 74 65 28 63 6f  .  if( isDate(co
7e370 6e 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67  ntext, argc, arg
7e380 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20  v, &x)==0 ){.   
7e390 20 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b   char zBuf[100];
7e3a0 0a 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 28  .    computeYMD(
7e3b0 26 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  &x);.    sqlite3
7e3c0 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66  _snprintf(sizeof
7e3d0 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25  (zBuf), zBuf, "%
7e3e0 30 34 64 2d 25 30 32 64 2d 25 30 32 64 22 2c 20  04d-%02d-%02d", 
7e3f0 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44 29 3b 0a  x.Y, x.M, x.D);.
7e400 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
7e410 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
7e420 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54   zBuf, -1, SQLIT
7e430 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
7e440 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 73 74  }.}../*.**    st
7e450 72 66 74 69 6d 65 28 20 46 4f 52 4d 41 54 2c 20  rftime( FORMAT, 
7e460 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d 4f 44 2c  TIMESTRING, MOD,
7e470 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a   MOD, ...).**.**
7e480 20 52 65 74 75 72 6e 20 61 20 73 74 72 69 6e 67   Return a string
7e490 20 64 65 73 63 72 69 62 65 64 20 62 79 20 46 4f   described by FO
7e4a0 52 4d 41 54 2e 20 20 43 6f 6e 76 65 72 73 69 6f  RMAT.  Conversio
7e4b0 6e 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  ns as follows:.*
7e4c0 2a 0a 2a 2a 20 20 20 25 64 20 20 64 61 79 20 6f  *.**   %d  day o
7e4d0 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20 25 66 20  f month.**   %f 
7e4e0 20 2a 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20 73   ** fractional s
7e4f0 65 63 6f 6e 64 73 20 20 53 53 2e 53 53 53 0a 2a  econds  SS.SSS.*
7e500 2a 20 20 20 25 48 20 20 68 6f 75 72 20 30 30 2d  *   %H  hour 00-
7e510 32 34 0a 2a 2a 20 20 20 25 6a 20 20 64 61 79 20  24.**   %j  day 
7e520 6f 66 20 79 65 61 72 20 30 30 30 2d 33 36 36 0a  of year 000-366.
7e530 2a 2a 20 20 20 25 4a 20 20 2a 2a 20 4a 75 6c 69  **   %J  ** Juli
7e540 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 0a 2a 2a  an day number.**
7e550 20 20 20 25 6d 20 20 6d 6f 6e 74 68 20 30 31 2d     %m  month 01-
7e560 31 32 0a 2a 2a 20 20 20 25 4d 20 20 6d 69 6e 75  12.**   %M  minu
7e570 74 65 20 30 30 2d 35 39 0a 2a 2a 20 20 20 25 73  te 00-59.**   %s
7e580 20 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20    seconds since 
7e590 31 39 37 30 2d 30 31 2d 30 31 0a 2a 2a 20 20 20  1970-01-01.**   
7e5a0 25 53 20 20 73 65 63 6f 6e 64 73 20 30 30 2d 35  %S  seconds 00-5
7e5b0 39 0a 2a 2a 20 20 20 25 77 20 20 64 61 79 20 6f  9.**   %w  day o
7e5c0 66 20 77 65 65 6b 20 30 2d 36 20 20 73 75 6e 64  f week 0-6  sund
7e5d0 61 79 3d 3d 30 0a 2a 2a 20 20 20 25 57 20 20 77  ay==0.**   %W  w
7e5e0 65 65 6b 20 6f 66 20 79 65 61 72 20 30 30 2d 35  eek of year 00-5
7e5f0 33 0a 2a 2a 20 20 20 25 59 20 20 79 65 61 72 20  3.**   %Y  year 
7e600 30 30 30 30 2d 39 39 39 39 0a 2a 2a 20 20 20 25  0000-9999.**   %
7e610 25 20 20 25 0a 2a 2f 0a 73 74 61 74 69 63 20 76  %  %.*/.static v
7e620 6f 69 64 20 73 74 72 66 74 69 6d 65 46 75 6e 63  oid strftimeFunc
7e630 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
7e640 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
7e650 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
7e660 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
7e670 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78  .){.  DateTime x
7e680 3b 0a 20 20 75 36 34 20 6e 3b 0a 20 20 73 69 7a  ;.  u64 n;.  siz
7e690 65 5f 74 20 69 2c 6a 3b 0a 20 20 63 68 61 72 20  e_t i,j;.  char 
7e6a0 2a 7a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  *z;.  sqlite3 *d
7e6b0 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  b;.  const char 
7e6c0 2a 7a 46 6d 74 20 3d 20 28 63 6f 6e 73 74 20 63  *zFmt = (const c
7e6d0 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  har*)sqlite3_val
7e6e0 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
7e6f0 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30  ;.  char zBuf[10
7e700 30 5d 3b 0a 20 20 69 66 28 20 7a 46 6d 74 3d 3d  0];.  if( zFmt==
7e710 30 20 7c 7c 20 69 73 44 61 74 65 28 63 6f 6e 74  0 || isDate(cont
7e720 65 78 74 2c 20 61 72 67 63 2d 31 2c 20 61 72 67  ext, argc-1, arg
7e730 76 2b 31 2c 20 26 78 29 20 29 20 72 65 74 75 72  v+1, &x) ) retur
7e740 6e 3b 0a 20 20 64 62 20 3d 20 73 71 6c 69 74 65  n;.  db = sqlite
7e750 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
7e760 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  dle(context);.  
7e770 66 6f 72 28 69 3d 30 2c 20 6e 3d 31 3b 20 7a 46  for(i=0, n=1; zF
7e780 6d 74 5b 69 5d 3b 20 69 2b 2b 2c 20 6e 2b 2b 29  mt[i]; i++, n++)
7e790 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69  {.    if( zFmt[i
7e7a0 5d 3d 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20  ]=='%' ){.      
7e7b0 73 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 2b 31  switch( zFmt[i+1
7e7c0 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73  ] ){.        cas
7e7d0 65 20 27 64 27 3a 0a 20 20 20 20 20 20 20 20 63  e 'd':.        c
7e7e0 61 73 65 20 27 48 27 3a 0a 20 20 20 20 20 20 20  ase 'H':.       
7e7f0 20 63 61 73 65 20 27 6d 27 3a 0a 20 20 20 20 20   case 'm':.     
7e800 20 20 20 63 61 73 65 20 27 4d 27 3a 0a 20 20 20     case 'M':.   
7e810 20 20 20 20 20 63 61 73 65 20 27 53 27 3a 0a 20       case 'S':. 
7e820 20 20 20 20 20 20 20 63 61 73 65 20 27 57 27 3a         case 'W':
7e830 0a 20 20 20 20 20 20 20 20 20 20 6e 2b 2b 3b 0a  .          n++;.
7e840 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 61 6c            /* fal
7e850 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20 20 20 20  l thru */.      
7e860 20 20 63 61 73 65 20 27 77 27 3a 0a 20 20 20 20    case 'w':.    
7e870 20 20 20 20 63 61 73 65 20 27 25 27 3a 0a 20 20      case '%':.  
7e880 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
7e890 20 20 20 20 20 20 20 63 61 73 65 20 27 66 27 3a         case 'f':
7e8a0 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20  .          n += 
7e8b0 38 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  8;.          bre
7e8c0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
7e8d0 20 27 6a 27 3a 0a 20 20 20 20 20 20 20 20 20 20   'j':.          
7e8e0 6e 20 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20 20  n += 3;.        
7e8f0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
7e900 20 63 61 73 65 20 27 59 27 3a 0a 20 20 20 20 20   case 'Y':.     
7e910 20 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20 20       n += 8;.   
7e920 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
7e930 20 20 20 20 20 20 63 61 73 65 20 27 73 27 3a 0a        case 's':.
7e940 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4a 27          case 'J'
7e950 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d  :.          n +=
7e960 20 35 30 3b 0a 20 20 20 20 20 20 20 20 20 20 62   50;.          b
7e970 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 65  reak;.        de
7e980 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 20  fault:.         
7e990 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 45 52 52   return;  /* ERR
7e9a0 4f 52 2e 20 20 72 65 74 75 72 6e 20 61 20 4e 55  OR.  return a NU
7e9b0 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20  LL */.      }.  
7e9c0 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 7d 0a 20      i++;.    }. 
7e9d0 20 7d 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e   }.  testcase( n
7e9e0 3d 3d 73 69 7a 65 6f 66 28 7a 42 75 66 29 2d 31  ==sizeof(zBuf)-1
7e9f0 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
7ea00 6e 3d 3d 73 69 7a 65 6f 66 28 7a 42 75 66 29 20  n==sizeof(zBuf) 
7ea10 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e  );.  testcase( n
7ea20 3d 3d 28 75 36 34 29 64 62 2d 3e 61 4c 69 6d 69  ==(u64)db->aLimi
7ea30 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
7ea40 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20 20 74 65  ENGTH]+1 );.  te
7ea50 73 74 63 61 73 65 28 20 6e 3d 3d 28 75 36 34 29  stcase( n==(u64)
7ea60 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
7ea70 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20  E_LIMIT_LENGTH] 
7ea80 29 3b 0a 20 20 69 66 28 20 6e 3c 73 69 7a 65 6f  );.  if( n<sizeo
7ea90 66 28 7a 42 75 66 29 20 29 7b 0a 20 20 20 20 7a  f(zBuf) ){.    z
7eaa0 20 3d 20 7a 42 75 66 3b 0a 20 20 7d 65 6c 73 65   = zBuf;.  }else
7eab0 20 69 66 28 20 6e 3e 28 75 36 34 29 64 62 2d 3e   if( n>(u64)db->
7eac0 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
7ead0 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20  MIT_LENGTH] ){. 
7eae0 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
7eaf0 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63  t_error_toobig(c
7eb00 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 72 65 74  ontext);.    ret
7eb10 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  urn;.  }else{.  
7eb20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 44 62 4d    z = sqlite3DbM
7eb30 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 28 69 6e  allocRaw(db, (in
7eb40 74 29 6e 29 3b 0a 20 20 20 20 69 66 28 20 7a 3d  t)n);.    if( z=
7eb50 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
7eb60 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
7eb70 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b  _nomem(context);
7eb80 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
7eb90 20 20 20 7d 0a 20 20 7d 0a 20 20 63 6f 6d 70 75     }.  }.  compu
7eba0 74 65 4a 44 28 26 78 29 3b 0a 20 20 63 6f 6d 70  teJD(&x);.  comp
7ebb0 75 74 65 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a  uteYMD_HMS(&x);.
7ebc0 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 7a 46 6d    for(i=j=0; zFm
7ebd0 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  t[i]; i++){.    
7ebe0 69 66 28 20 7a 46 6d 74 5b 69 5d 21 3d 27 25 27  if( zFmt[i]!='%'
7ebf0 20 29 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d   ){.      z[j++]
7ec00 20 3d 20 7a 46 6d 74 5b 69 5d 3b 0a 20 20 20 20   = zFmt[i];.    
7ec10 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 2b 2b  }else{.      i++
7ec20 3b 0a 20 20 20 20 20 20 73 77 69 74 63 68 28 20  ;.      switch( 
7ec30 7a 46 6d 74 5b 69 5d 20 29 7b 0a 20 20 20 20 20  zFmt[i] ){.     
7ec40 20 20 20 63 61 73 65 20 27 64 27 3a 20 20 73 71     case 'd':  sq
7ec50 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33  lite3_snprintf(3
7ec60 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78  , &z[j],"%02d",x
7ec70 2e 44 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b  .D); j+=2; break
7ec80 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  ;.        case '
7ec90 66 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20  f': {.          
7eca0 64 6f 75 62 6c 65 20 73 20 3d 20 78 2e 73 3b 0a  double s = x.s;.
7ecb0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 3e            if( s>
7ecc0 35 39 2e 39 39 39 20 29 20 73 20 3d 20 35 39 2e  59.999 ) s = 59.
7ecd0 39 39 39 3b 0a 20 20 20 20 20 20 20 20 20 20 73  999;.          s
7ece0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
7ecf0 37 2c 20 26 7a 5b 6a 5d 2c 22 25 30 36 2e 33 66  7, &z[j],"%06.3f
7ed00 22 2c 20 73 29 3b 0a 20 20 20 20 20 20 20 20 20  ", s);.         
7ed10 20 6a 20 2b 3d 20 73 71 6c 69 74 65 33 53 74 72   j += sqlite3Str
7ed20 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20  len30(&z[j]);.  
7ed30 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
7ed40 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
7ed50 20 63 61 73 65 20 27 48 27 3a 20 20 73 71 6c 69   case 'H':  sqli
7ed60 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20  te3_snprintf(3, 
7ed70 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 68  &z[j],"%02d",x.h
7ed80 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a  ); j+=2; break;.
7ed90 20 20 20 20 20 20 20 20 63 61 73 65 20 27 57 27          case 'W'
7eda0 3a 20 2f 2a 20 46 61 6c 6c 20 74 68 72 75 20 2a  : /* Fall thru *
7edb0 2f 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  /.        case '
7edc0 6a 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20  j': {.          
7edd0 69 6e 74 20 6e 44 61 79 3b 20 20 20 20 20 20 20  int nDay;       
7ede0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
7edf0 6f 66 20 64 61 79 73 20 73 69 6e 63 65 20 31 73  of days since 1s
7ee00 74 20 64 61 79 20 6f 66 20 79 65 61 72 20 2a 2f  t day of year */
7ee10 0a 20 20 20 20 20 20 20 20 20 20 44 61 74 65 54  .          DateT
7ee20 69 6d 65 20 79 20 3d 20 78 3b 0a 20 20 20 20 20  ime y = x;.     
7ee30 20 20 20 20 20 79 2e 76 61 6c 69 64 4a 44 20 3d       y.validJD =
7ee40 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 79 2e   0;.          y.
7ee50 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  M = 1;.         
7ee60 20 79 2e 44 20 3d 20 31 3b 0a 20 20 20 20 20 20   y.D = 1;.      
7ee70 20 20 20 20 63 6f 6d 70 75 74 65 4a 44 28 26 79      computeJD(&y
7ee80 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 44 61  );.          nDa
7ee90 79 20 3d 20 28 69 6e 74 29 28 28 78 2e 69 4a 44  y = (int)((x.iJD
7eea0 2d 79 2e 69 4a 44 2b 34 33 32 30 30 30 30 30 29  -y.iJD+43200000)
7eeb0 2f 38 36 34 30 30 30 30 30 29 3b 0a 20 20 20 20  /86400000);.    
7eec0 20 20 20 20 20 20 69 66 28 20 7a 46 6d 74 5b 69        if( zFmt[i
7eed0 5d 3d 3d 27 57 27 20 29 7b 0a 20 20 20 20 20 20  ]=='W' ){.      
7eee0 20 20 20 20 20 20 69 6e 74 20 77 64 3b 20 20 20        int wd;   
7eef0 2f 2a 20 30 3d 4d 6f 6e 64 61 79 2c 20 31 3d 54  /* 0=Monday, 1=T
7ef00 75 65 73 64 61 79 2c 20 2e 2e 2e 20 36 3d 53 75  uesday, ... 6=Su
7ef10 6e 64 61 79 20 2a 2f 0a 20 20 20 20 20 20 20 20  nday */.        
7ef20 20 20 20 20 77 64 20 3d 20 28 69 6e 74 29 28 28      wd = (int)((
7ef30 28 78 2e 69 4a 44 2b 34 33 32 30 30 30 30 30 29  (x.iJD+43200000)
7ef40 2f 38 36 34 30 30 30 30 30 29 25 37 29 3b 0a 20  /86400000)%7);. 
7ef50 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
7ef60 65 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26  e3_snprintf(3, &
7ef70 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 28 6e 44 61  z[j],"%02d",(nDa
7ef80 79 2b 37 2d 77 64 29 2f 37 29 3b 0a 20 20 20 20  y+7-wd)/7);.    
7ef90 20 20 20 20 20 20 20 20 6a 20 2b 3d 20 32 3b 0a          j += 2;.
7efa0 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
7efb0 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
7efc0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 34 2c  ite3_snprintf(4,
7efd0 20 26 7a 5b 6a 5d 2c 22 25 30 33 64 22 2c 6e 44   &z[j],"%03d",nD
7efe0 61 79 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20  ay+1);.         
7eff0 20 20 20 6a 20 2b 3d 20 33 3b 0a 20 20 20 20 20     j += 3;.     
7f000 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
7f010 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
7f020 7d 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  }.        case '
7f030 4a 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20  J': {.          
7f040 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
7f050 28 32 30 2c 20 26 7a 5b 6a 5d 2c 22 25 2e 31 36  (20, &z[j],"%.16
7f060 67 22 2c 78 2e 69 4a 44 2f 38 36 34 30 30 30 30  g",x.iJD/8640000
7f070 30 2e 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  0.0);.          
7f080 6a 2b 3d 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  j+=sqlite3Strlen
7f090 33 30 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20  30(&z[j]);.     
7f0a0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
7f0b0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61      }.        ca
7f0c0 73 65 20 27 6d 27 3a 20 20 73 71 6c 69 74 65 33  se 'm':  sqlite3
7f0d0 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b  _snprintf(3, &z[
7f0e0 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 4d 29 3b 20  j],"%02d",x.M); 
7f0f0 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20  j+=2; break;.   
7f100 20 20 20 20 20 63 61 73 65 20 27 4d 27 3a 20 20       case 'M':  
7f110 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
7f120 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22  (3, &z[j],"%02d"
7f130 2c 78 2e 6d 29 3b 20 6a 2b 3d 32 3b 20 62 72 65  ,x.m); j+=2; bre
7f140 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
7f150 20 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 20 20   's': {.        
7f160 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
7f170 74 66 28 33 30 2c 26 7a 5b 6a 5d 2c 22 25 6c 6c  tf(30,&z[j],"%ll
7f180 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  d",.            
7f190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
7f1a0 69 36 34 29 28 78 2e 69 4a 44 2f 31 30 30 30 20  i64)(x.iJD/1000 
7f1b0 2d 20 32 31 30 38 36 36 37 36 2a 28 69 36 34 29  - 21086676*(i64)
7f1c0 31 30 30 30 30 29 29 3b 0a 20 20 20 20 20 20 20  10000));.       
7f1d0 20 20 20 6a 20 2b 3d 20 73 71 6c 69 74 65 33 53     j += sqlite3S
7f1e0 74 72 6c 65 6e 33 30 28 26 7a 5b 6a 5d 29 3b 0a  trlen30(&z[j]);.
7f1f0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
7f200 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
7f210 20 20 20 63 61 73 65 20 27 53 27 3a 20 20 73 71     case 'S':  sq
7f220 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 33  lite3_snprintf(3
7f230 2c 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 28 69  ,&z[j],"%02d",(i
7f240 6e 74 29 78 2e 73 29 3b 20 6a 2b 3d 32 3b 20 62  nt)x.s); j+=2; b
7f250 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61  reak;.        ca
7f260 73 65 20 27 77 27 3a 20 7b 0a 20 20 20 20 20 20  se 'w': {.      
7f270 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 28 63 68      z[j++] = (ch
7f280 61 72 29 28 28 28 78 2e 69 4a 44 2b 31 32 39 36  ar)(((x.iJD+1296
7f290 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29  00000)/86400000)
7f2a0 20 25 20 37 29 20 2b 20 27 30 27 3b 0a 20 20 20   % 7) + '0';.   
7f2b0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
7f2c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
7f2d0 63 61 73 65 20 27 59 27 3a 20 7b 0a 20 20 20 20  case 'Y': {.    
7f2e0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
7f2f0 70 72 69 6e 74 66 28 35 2c 26 7a 5b 6a 5d 2c 22  printf(5,&z[j],"
7f300 25 30 34 64 22 2c 78 2e 59 29 3b 20 6a 2b 3d 73  %04d",x.Y); j+=s
7f310 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 26  qlite3Strlen30(&
7f320 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 20 20 20  z[j]);.         
7f330 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
7f340 7d 0a 20 20 20 20 20 20 20 20 64 65 66 61 75 6c  }.        defaul
7f350 74 3a 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 25  t:   z[j++] = '%
7f360 27 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  '; break;.      
7f370 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 7a 5b  }.    }.  }.  z[
7f380 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  j] = 0;.  sqlite
7f390 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
7f3a0 6e 74 65 78 74 2c 20 7a 2c 20 2d 31 2c 0a 20 20  ntext, z, -1,.  
7f3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7f3c0 20 20 20 20 7a 3d 3d 7a 42 75 66 20 3f 20 53 51      z==zBuf ? SQ
7f3d0 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 3a  LITE_TRANSIENT :
7f3e0 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29   SQLITE_DYNAMIC)
7f3f0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65  ;.}../*.** curre
7f400 6e 74 5f 74 69 6d 65 28 29 0a 2a 2a 0a 2a 2a 20  nt_time().**.** 
7f410 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
7f420 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65 20 76  turns the same v
7f430 61 6c 75 65 20 61 73 20 74 69 6d 65 28 27 6e 6f  alue as time('no
7f440 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  w')..*/.static v
7f450 6f 69 64 20 63 74 69 6d 65 46 75 6e 63 28 0a 20  oid ctimeFunc(. 
7f460 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
7f470 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
7f480 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69   NotUsed,.  sqli
7f490 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55  te3_value **NotU
7f4a0 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44  sed2.){.  UNUSED
7f4b0 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
7f4c0 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a  sed, NotUsed2);.
7f4d0 20 20 74 69 6d 65 46 75 6e 63 28 63 6f 6e 74 65    timeFunc(conte
7f4e0 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a  xt, 0, 0);.}../*
7f4f0 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 64 61 74 65  .** current_date
7f500 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  ().**.** This fu
7f510 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74  nction returns t
7f520 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73  he same value as
7f530 20 64 61 74 65 28 27 6e 6f 77 27 29 2e 0a 2a 2f   date('now')..*/
7f540 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 64 61  .static void cda
7f550 74 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  teFunc(.  sqlite
7f560 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
7f570 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65  xt,.  int NotUse
7f580 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  d,.  sqlite3_val
7f590 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b  ue **NotUsed2.){
7f5a0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
7f5b0 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
7f5c0 74 55 73 65 64 32 29 3b 0a 20 20 64 61 74 65 46  tUsed2);.  dateF
7f5d0 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20  unc(context, 0, 
7f5e0 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75 72  0);.}../*.** cur
7f5f0 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d 70 28 29  rent_timestamp()
7f600 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
7f610 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68 65  tion returns the
7f620 20 73 61 6d 65 20 76 61 6c 75 65 20 61 73 20 64   same value as d
7f630 61 74 65 74 69 6d 65 28 27 6e 6f 77 27 29 2e 0a  atetime('now')..
7f640 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
7f650 74 69 6d 65 73 74 61 6d 70 46 75 6e 63 28 0a 20  timestampFunc(. 
7f660 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
7f670 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
7f680 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69   NotUsed,.  sqli
7f690 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55  te3_value **NotU
7f6a0 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44  sed2.){.  UNUSED
7f6b0 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
7f6c0 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a  sed, NotUsed2);.
7f6d0 20 20 64 61 74 65 74 69 6d 65 46 75 6e 63 28 63    datetimeFunc(c
7f6e0 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a 7d  ontext, 0, 0);.}
7f6f0 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
7f700 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
7f710 44 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 29 20  DATETIME_FUNCS) 
7f720 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
7f730 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49 4d 45 5f  E_OMIT_DATETIME_
7f740 46 55 4e 43 53 0a 2f 2a 0a 2a 2a 20 49 66 20 74  FUNCS./*.** If t
7f750 68 65 20 6c 69 62 72 61 72 79 20 69 73 20 63 6f  he library is co
7f760 6d 70 69 6c 65 64 20 74 6f 20 6f 6d 69 74 20 74  mpiled to omit t
7f770 68 65 20 66 75 6c 6c 2d 73 63 61 6c 65 20 64 61  he full-scale da
7f780 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a 2a 20 68  te and time.** h
7f790 61 6e 64 6c 69 6e 67 20 28 74 6f 20 67 65 74 20  andling (to get 
7f7a0 61 20 73 6d 61 6c 6c 65 72 20 62 69 6e 61 72 79  a smaller binary
7f7b0 29 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ), the following
7f7c0 20 6d 69 6e 69 6d 61 6c 20 76 65 72 73 69 6f 6e   minimal version
7f7d0 0a 2a 2a 20 6f 66 20 74 68 65 20 66 75 6e 63 74  .** of the funct
7f7e0 69 6f 6e 73 20 63 75 72 72 65 6e 74 5f 74 69 6d  ions current_tim
7f7f0 65 28 29 2c 20 63 75 72 72 65 6e 74 5f 64 61 74  e(), current_dat
7f800 65 28 29 20 61 6e 64 20 63 75 72 72 65 6e 74 5f  e() and current_
7f810 74 69 6d 65 73 74 61 6d 70 28 29 0a 2a 2a 20 61  timestamp().** a
7f820 72 65 20 69 6e 63 6c 75 64 65 64 20 69 6e 73 74  re included inst
7f830 65 61 64 2e 20 54 68 69 73 20 69 73 20 74 6f 20  ead. This is to 
7f840 73 75 70 70 6f 72 74 20 63 6f 6c 75 6d 6e 20 64  support column d
7f850 65 63 6c 61 72 61 74 69 6f 6e 73 20 74 68 61 74  eclarations that
7f860 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 22 44 45 46  .** include "DEF
7f870 41 55 4c 54 20 43 55 52 52 45 4e 54 5f 54 49 4d  AULT CURRENT_TIM
7f880 45 22 20 65 74 63 2e 0a 2a 2a 0a 2a 2a 20 54 68  E" etc..**.** Th
7f890 69 73 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 73  is function uses
7f8a0 20 74 68 65 20 43 2d 6c 69 62 72 61 72 79 20 66   the C-library f
7f8b0 75 6e 63 74 69 6f 6e 73 20 74 69 6d 65 28 29 2c  unctions time(),
7f8c0 20 67 6d 74 69 6d 65 28 29 0a 2a 2a 20 61 6e 64   gmtime().** and
7f8d0 20 73 74 72 66 74 69 6d 65 28 29 2e 20 54 68 65   strftime(). The
7f8e0 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 74   format string t
7f8f0 6f 20 70 61 73 73 20 74 6f 20 73 74 72 66 74 69  o pass to strfti
7f900 6d 65 28 29 20 69 73 20 73 75 70 70 6c 69 65 64  me() is supplied
7f910 0a 2a 2a 20 61 73 20 74 68 65 20 75 73 65 72 2d  .** as the user-
7f920 64 61 74 61 20 66 6f 72 20 74 68 65 20 66 75 6e  data for the fun
7f930 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ction..*/.static
7f940 20 76 6f 69 64 20 63 75 72 72 65 6e 74 54 69 6d   void currentTim
7f950 65 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  eFunc(.  sqlite3
7f960 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
7f970 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
7f980 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
7f990 2a 61 72 67 76 0a 29 7b 0a 20 20 74 69 6d 65 5f  *argv.){.  time_
7f9a0 74 20 74 3b 0a 20 20 63 68 61 72 20 2a 7a 46 6f  t t;.  char *zFo
7f9b0 72 6d 61 74 20 3d 20 28 63 68 61 72 20 2a 29 73  rmat = (char *)s
7f9c0 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
7f9d0 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c  (context);.  sql
7f9e0 69 74 65 33 20 2a 64 62 3b 0a 20 20 64 6f 75 62  ite3 *db;.  doub
7f9f0 6c 65 20 72 54 3b 0a 20 20 63 68 61 72 20 7a 42  le rT;.  char zB
7fa00 75 66 5b 32 30 5d 3b 0a 0a 20 20 55 4e 55 53 45  uf[20];..  UNUSE
7fa10 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63  D_PARAMETER(argc
7fa20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
7fa30 4d 45 54 45 52 28 61 72 67 76 29 3b 0a 0a 20 20  METER(argv);..  
7fa40 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  db = sqlite3_con
7fa50 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
7fa60 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74  ontext);.  sqlit
7fa70 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28  e3OsCurrentTime(
7fa80 64 62 2d 3e 70 56 66 73 2c 20 26 72 54 29 3b 0a  db->pVfs, &rT);.
7fa90 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
7faa0 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49  MIT_FLOATING_POI
7fab0 4e 54 0a 20 20 74 20 3d 20 38 36 34 30 30 2e 30  NT.  t = 86400.0
7fac0 2a 28 72 54 20 2d 20 32 34 34 30 35 38 37 2e 35  *(rT - 2440587.5
7fad0 29 20 2b 20 30 2e 35 3b 0a 23 65 6c 73 65 0a 20  ) + 0.5;.#else. 
7fae0 20 2f 2a 20 77 69 74 68 6f 75 74 20 66 6c 6f 61   /* without floa
7faf0 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70 6f  ting point suppo
7fb00 72 74 2c 20 72 54 20 77 69 6c 6c 20 68 61 76 65  rt, rT will have
7fb10 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 6c 6f  .  ** already lo
7fb20 73 74 20 66 72 61 63 74 69 6f 6e 61 6c 20 64 61  st fractional da
7fb30 79 20 70 72 65 63 69 73 69 6f 6e 2e 0a 20 20 2a  y precision..  *
7fb40 2f 0a 20 20 74 20 3d 20 38 36 34 30 30 20 2a 20  /.  t = 86400 * 
7fb50 28 72 54 20 2d 20 32 34 34 30 35 38 37 29 20 2d  (rT - 2440587) -
7fb60 20 34 33 32 30 30 3b 0a 23 65 6e 64 69 66 0a 23   43200;.#endif.#
7fb70 69 66 64 65 66 20 48 41 56 45 5f 47 4d 54 49 4d  ifdef HAVE_GMTIM
7fb80 45 5f 52 0a 20 20 7b 0a 20 20 20 20 73 74 72 75  E_R.  {.    stru
7fb90 63 74 20 74 6d 20 73 4e 6f 77 3b 0a 20 20 20 20  ct tm sNow;.    
7fba0 67 6d 74 69 6d 65 5f 72 28 26 74 2c 20 26 73 4e  gmtime_r(&t, &sN
7fbb0 6f 77 29 3b 0a 20 20 20 20 73 74 72 66 74 69 6d  ow);.    strftim
7fbc0 65 28 7a 42 75 66 2c 20 32 30 2c 20 7a 46 6f 72  e(zBuf, 20, zFor
7fbd0 6d 61 74 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 7d  mat, &sNow);.  }
7fbe0 0a 23 65 6c 73 65 0a 20 20 7b 0a 20 20 20 20 73  .#else.  {.    s
7fbf0 74 72 75 63 74 20 74 6d 20 2a 70 54 6d 3b 0a 20  truct tm *pTm;. 
7fc00 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
7fc10 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d 75  _enter(sqlite3Mu
7fc20 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
7fc30 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
7fc40 54 45 52 29 29 3b 0a 20 20 20 20 70 54 6d 20 3d  TER));.    pTm =
7fc50 20 67 6d 74 69 6d 65 28 26 74 29 3b 0a 20 20 20   gmtime(&t);.   
7fc60 20 73 74 72 66 74 69 6d 65 28 7a 42 75 66 2c 20   strftime(zBuf, 
7fc70 32 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 70 54 6d  20, zFormat, pTm
7fc80 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
7fc90 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74  utex_leave(sqlit
7fca0 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
7fcb0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
7fcc0 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 7d 0a 23  _MASTER));.  }.#
7fcd0 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33  endif..  sqlite3
7fce0 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
7fcf0 74 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20  text, zBuf, -1, 
7fd00 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
7fd10 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  );.}.#endif../*.
7fd20 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
7fd30 20 72 65 67 69 73 74 65 72 65 64 20 61 6c 6c 20   registered all 
7fd40 6f 66 20 74 68 65 20 61 62 6f 76 65 20 43 20 66  of the above C f
7fd50 75 6e 63 74 69 6f 6e 73 20 61 73 20 53 51 4c 0a  unctions as SQL.
7fd60 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54  ** functions.  T
7fd70 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 74 68  his should be th
7fd80 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69  e only routine i
7fd90 6e 20 74 68 69 73 20 66 69 6c 65 20 77 69 74 68  n this file with
7fda0 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e  .** external lin
7fdb0 6b 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  kage..*/.SQLITE_
7fdc0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
7fdd0 69 74 65 33 52 65 67 69 73 74 65 72 44 61 74 65  ite3RegisterDate
7fde0 54 69 6d 65 46 75 6e 63 74 69 6f 6e 73 28 76 6f  TimeFunctions(vo
7fdf0 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 53 51  id){.  static SQ
7fe00 4c 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66  LITE_WSD FuncDef
7fe10 20 61 44 61 74 65 54 69 6d 65 46 75 6e 63 73 5b   aDateTimeFuncs[
7fe20 5d 20 3d 20 7b 0a 23 69 66 6e 64 65 66 20 53 51  ] = {.#ifndef SQ
7fe30 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49  LITE_OMIT_DATETI
7fe40 4d 45 5f 46 55 4e 43 53 0a 20 20 20 20 46 55 4e  ME_FUNCS.    FUN
7fe50 43 54 49 4f 4e 28 6a 75 6c 69 61 6e 64 61 79 2c  CTION(julianday,
7fe60 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30          -1, 0, 0
7fe70 2c 20 6a 75 6c 69 61 6e 64 61 79 46 75 6e 63 20  , juliandayFunc 
7fe80 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
7fe90 64 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  date,           
7fea0 20 20 2d 31 2c 20 30 2c 20 30 2c 20 64 61 74 65    -1, 0, 0, date
7feb0 46 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20  Func      ),.   
7fec0 20 46 55 4e 43 54 49 4f 4e 28 74 69 6d 65 2c 20   FUNCTION(time, 
7fed0 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20              -1, 
7fee0 30 2c 20 30 2c 20 74 69 6d 65 46 75 6e 63 20 20  0, 0, timeFunc  
7fef0 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
7ff00 49 4f 4e 28 64 61 74 65 74 69 6d 65 2c 20 20 20  ION(datetime,   
7ff10 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20        -1, 0, 0, 
7ff20 64 61 74 65 74 69 6d 65 46 75 6e 63 20 20 29 2c  datetimeFunc  ),
7ff30 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 74  .    FUNCTION(st
7ff40 72 66 74 69 6d 65 2c 20 20 20 20 20 20 20 20 20  rftime,         
7ff50 2d 31 2c 20 30 2c 20 30 2c 20 73 74 72 66 74 69  -1, 0, 0, strfti
7ff60 6d 65 46 75 6e 63 20 20 29 2c 0a 20 20 20 20 46  meFunc  ),.    F
7ff70 55 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f  UNCTION(current_
7ff80 74 69 6d 65 2c 20 20 20 20 20 20 30 2c 20 30 2c  time,      0, 0,
7ff90 20 30 2c 20 63 74 69 6d 65 46 75 6e 63 20 20 20   0, ctimeFunc   
7ffa0 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
7ffb0 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74  N(current_timest
7ffc0 61 6d 70 2c 20 30 2c 20 30 2c 20 30 2c 20 63 74  amp, 0, 0, 0, ct
7ffd0 69 6d 65 73 74 61 6d 70 46 75 6e 63 29 2c 0a 20  imestampFunc),. 
7ffe0 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72 72     FUNCTION(curr
7fff0 65 6e 74 5f 64 61 74 65 2c 20 20 20 20 20 20 30  ent_date,      0
80000 2c 20 30 2c 20 30 2c 20 63 64 61 74 65 46 75 6e  , 0, 0, cdateFun
80010 63 20 20 20 20 20 29 2c 0a 23 65 6c 73 65 0a 20  c     ),.#else. 
80020 20 20 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e 28     STR_FUNCTION(
80030 63 75 72 72 65 6e 74 5f 74 69 6d 65 2c 20 20 20  current_time,   
80040 20 20 20 30 2c 20 22 25 48 3a 25 4d 3a 25 53 22     0, "%H:%M:%S"
80050 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20 63 75  ,          0, cu
80060 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 29 2c 0a  rrentTimeFunc),.
80070 20 20 20 20 53 54 52 5f 46 55 4e 43 54 49 4f 4e      STR_FUNCTION
80080 28 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61  (current_timesta
80090 6d 70 2c 20 30 2c 20 22 25 59 2d 25 6d 2d 25 64  mp, 0, "%Y-%m-%d
800a0 22 2c 20 20 20 20 20 20 20 20 20 20 30 2c 20 63  ",          0, c
800b0 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 29 2c  urrentTimeFunc),
800c0 0a 20 20 20 20 53 54 52 5f 46 55 4e 43 54 49 4f  .    STR_FUNCTIO
800d0 4e 28 63 75 72 72 65 6e 74 5f 64 61 74 65 2c 20  N(current_date, 
800e0 20 20 20 20 20 30 2c 20 22 25 59 2d 25 6d 2d 25       0, "%Y-%m-%
800f0 64 20 25 48 3a 25 4d 3a 25 53 22 2c 20 30 2c 20  d %H:%M:%S", 0, 
80100 63 75 72 72 65 6e 74 54 69 6d 65 46 75 6e 63 29  currentTimeFunc)
80110 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 20 20  ,.#endif.  };.  
80120 69 6e 74 20 69 3b 0a 20 20 46 75 6e 63 44 65 66  int i;.  FuncDef
80130 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26 47  Hash *pHash = &G
80140 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 48 61 73  LOBAL(FuncDefHas
80150 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  h, sqlite3Global
80160 46 75 6e 63 74 69 6f 6e 73 29 3b 0a 20 20 46 75  Functions);.  Fu
80170 6e 63 44 65 66 20 2a 61 46 75 6e 63 20 3d 20 28  ncDef *aFunc = (
80180 46 75 6e 63 44 65 66 2a 29 26 47 4c 4f 42 41 4c  FuncDef*)&GLOBAL
80190 28 46 75 6e 63 44 65 66 2c 20 61 44 61 74 65 54  (FuncDef, aDateT
801a0 69 6d 65 46 75 6e 63 73 29 3b 0a 0a 20 20 66 6f  imeFuncs);..  fo
801b0 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69  r(i=0; i<ArraySi
801c0 7a 65 28 61 44 61 74 65 54 69 6d 65 46 75 6e 63  ze(aDateTimeFunc
801d0 73 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71  s); i++){.    sq
801e0 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e 73 65  lite3FuncDefInse
801f0 72 74 28 70 48 61 73 68 2c 20 26 61 46 75 6e 63  rt(pHash, &aFunc
80200 5b 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a  [i]);.  }.}../**
80210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
80220 20 6f 66 20 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a   of date.c *****
80230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
80260 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
80270 69 6e 20 66 69 6c 65 20 6f 73 2e 63 20 2a 2a 2a  in file os.c ***
80280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
802a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
802b0 2a 2a 20 32 30 30 35 20 4e 6f 76 65 6d 62 65 72  ** 2005 November
802c0 20 32 39 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   29.**.** The au
802d0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
802e0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
802f0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
80300 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
80310 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
80320 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
80330 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
80340 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
80350 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
80360 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
80370 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
80380 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
80390 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
803a0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
803b0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
803c0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
803d0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
803e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
803f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
80420 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
80430 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
80440 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20 63 6f   OS interface co
80450 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f  de that is commo
80460 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20 61 72 63 68  n to all.** arch
80470 69 74 65 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a  itectures..**.**
80480 20 24 49 64 3a 20 6f 73 2e 63 2c 76 20 31 2e 31   $Id: os.c,v 1.1
80490 32 37 20 32 30 30 39 2f 30 37 2f 32 37 20 31 31  27 2009/07/27 11
804a0 3a 34 31 3a 32 31 20 64 61 6e 69 65 6c 6b 31 39  :41:21 danielk19
804b0 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 64 65 66  77 Exp $.*/.#def
804c0 69 6e 65 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 43  ine _SQLITE_OS_C
804d0 5f 20 31 0a 23 75 6e 64 65 66 20 5f 53 51 4c 49  _ 1.#undef _SQLI
804e0 54 45 5f 4f 53 5f 43 5f 0a 0a 2f 2a 0a 2a 2a 20  TE_OS_C_../*.** 
804f0 54 68 65 20 64 65 66 61 75 6c 74 20 53 51 4c 69  The default SQLi
80500 74 65 20 73 71 6c 69 74 65 33 5f 76 66 73 20 69  te sqlite3_vfs i
80510 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 64  mplementations d
80520 6f 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 0a 2a  o not allocate.*
80530 2a 20 6d 65 6d 6f 72 79 20 28 61 63 74 75 61 6c  * memory (actual
80540 6c 79 2c 20 6f 73 5f 75 6e 69 78 2e 63 20 61 6c  ly, os_unix.c al
80550 6c 6f 63 61 74 65 73 20 61 20 73 6d 61 6c 6c 20  locates a small 
80560 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79  amount of memory
80570 0a 2a 2a 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  .** from within 
80580 4f 73 4f 70 65 6e 28 29 29 2c 20 62 75 74 20 73  OsOpen()), but s
80590 6f 6d 65 20 74 68 69 72 64 2d 70 61 72 74 79 20  ome third-party 
805a0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
805b0 6d 61 79 2e 0a 2a 2a 20 53 6f 20 77 65 20 74 65  may..** So we te
805c0 73 74 20 74 68 65 20 65 66 66 65 63 74 73 20 6f  st the effects o
805d0 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  f a malloc() fai
805e0 6c 69 6e 67 20 61 6e 64 20 74 68 65 20 73 71 6c  ling and the sql
805f0 69 74 65 33 4f 73 58 58 58 28 29 0a 2a 2a 20 66  ite3OsXXX().** f
80600 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 69 6e  unction returnin
80610 67 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e  g SQLITE_IOERR_N
80620 4f 4d 45 4d 20 75 73 69 6e 67 20 74 68 65 20 44  OMEM using the D
80630 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
80640 20 6d 61 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68   macro..**.** Th
80650 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63  e following func
80660 74 69 6f 6e 73 20 61 72 65 20 69 6e 73 74 72 75  tions are instru
80670 6d 65 6e 74 65 64 20 66 6f 72 20 6d 61 6c 6c 6f  mented for mallo
80680 63 28 29 20 66 61 69 6c 75 72 65 20 0a 2a 2a 20  c() failure .** 
80690 74 65 73 74 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  testing:.**.**  
806a0 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e     sqlite3OsOpen
806b0 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  ().**     sqlite
806c0 33 4f 73 52 65 61 64 28 29 0a 2a 2a 20 20 20 20  3OsRead().**    
806d0 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
806e0 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  ).**     sqlite3
806f0 4f 73 53 79 6e 63 28 29 0a 2a 2a 20 20 20 20 20  OsSync().**     
80700 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 0a  sqlite3OsLock().
80710 2a 2a 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65  **.*/.#if define
80720 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26  d(SQLITE_TEST) &
80730 26 20 28 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  & (SQLITE_OS_WIN
80740 3d 3d 30 29 0a 20 20 23 64 65 66 69 6e 65 20 44  ==0).  #define D
80750 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54  O_OS_MALLOC_TEST
80760 28 78 29 20 69 66 20 28 21 78 20 7c 7c 20 21 73  (x) if (!x || !s
80770 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e  qlite3IsMemJourn
80780 61 6c 28 78 29 29 20 7b 20 20 20 20 20 5c 0a 20  al(x)) {     \. 
80790 20 20 20 76 6f 69 64 20 2a 70 54 73 74 41 6c 6c     void *pTstAll
807a0 6f 63 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  oc = sqlite3Mall
807b0 6f 63 28 31 30 29 3b 20 20 20 20 20 20 20 20 20  oc(10);         
807c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
807d0 20 20 20 20 5c 0a 20 20 20 20 69 66 20 28 21 70      \.    if (!p
807e0 54 73 74 41 6c 6c 6f 63 29 20 72 65 74 75 72 6e  TstAlloc) return
807f0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f   SQLITE_IOERR_NO
80800 4d 45 4d 3b 20 20 20 20 20 20 20 20 20 20 20 20  MEM;            
80810 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
80820 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54   sqlite3_free(pT
80830 73 74 41 6c 6c 6f 63 29 3b 20 20 20 20 20 20 20  stAlloc);       
80840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
80860 20 20 5c 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20    \.  }.#else.  
80870 23 64 65 66 69 6e 65 20 44 4f 5f 4f 53 5f 4d 41  #define DO_OS_MA
80880 4c 4c 4f 43 5f 54 45 53 54 28 78 29 0a 23 65 6e  LLOC_TEST(x).#en
80890 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  dif../*.** The f
808a0 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65  ollowing routine
808b0 73 20 61 72 65 20 63 6f 6e 76 65 6e 69 65 6e 63  s are convenienc
808c0 65 20 77 72 61 70 70 65 72 73 20 61 72 6f 75 6e  e wrappers aroun
808d0 64 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f 66 20  d methods.** of 
808e0 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  the sqlite3_file
808f0 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73 20 69   object.  This i
80900 73 20 6d 6f 73 74 6c 79 20 6a 75 73 74 20 73 79  s mostly just sy
80910 6e 74 61 63 74 69 63 20 73 75 67 61 72 2e 20 41  ntactic sugar. A
80920 6c 6c 0a 2a 2a 20 6f 66 20 74 68 69 73 20 77 6f  ll.** of this wo
80930 75 6c 64 20 62 65 20 63 6f 6d 70 6c 65 74 65 6c  uld be completel
80940 79 20 61 75 74 6f 6d 61 74 69 63 20 69 66 20 53  y automatic if S
80950 51 4c 69 74 65 20 77 65 72 65 20 63 6f 64 65 64  QLite were coded
80960 20 75 73 69 6e 67 0a 2a 2a 20 43 2b 2b 20 69 6e   using.** C++ in
80970 73 74 65 61 64 20 6f 66 20 70 6c 61 69 6e 20 6f  stead of plain o
80980 6c 64 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ld C..*/.SQLITE_
80990 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
809a0 74 65 33 4f 73 43 6c 6f 73 65 28 73 71 6c 69 74  te3OsClose(sqlit
809b0 65 33 5f 66 69 6c 65 20 2a 70 49 64 29 7b 0a 20  e3_file *pId){. 
809c0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
809d0 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 49 64 2d 3e  _OK;.  if( pId->
809e0 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20  pMethods ){.    
809f0 72 63 20 3d 20 70 49 64 2d 3e 70 4d 65 74 68 6f  rc = pId->pMetho
80a00 64 73 2d 3e 78 43 6c 6f 73 65 28 70 49 64 29 3b  ds->xClose(pId);
80a10 0a 20 20 20 20 70 49 64 2d 3e 70 4d 65 74 68 6f  .    pId->pMetho
80a20 64 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65  ds = 0;.  }.  re
80a30 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54  turn rc;.}.SQLIT
80a40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
80a50 6c 69 74 65 33 4f 73 52 65 61 64 28 73 71 6c 69  lite3OsRead(sqli
80a60 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 76 6f  te3_file *id, vo
80a70 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 61 6d  id *pBuf, int am
80a80 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 7b 0a  t, i64 offset){.
80a90 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54    DO_OS_MALLOC_T
80aa0 45 53 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72  EST(id);.  retur
80ab0 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e  n id->pMethods->
80ac0 78 52 65 61 64 28 69 64 2c 20 70 42 75 66 2c 20  xRead(id, pBuf, 
80ad0 61 6d 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a  amt, offset);.}.
80ae0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
80af0 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74  nt sqlite3OsWrit
80b00 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
80b10 69 64 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  id, const void *
80b20 70 42 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20 69  pBuf, int amt, i
80b30 36 34 20 6f 66 66 73 65 74 29 7b 0a 20 20 44 4f  64 offset){.  DO
80b40 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28  _OS_MALLOC_TEST(
80b50 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 64  id);.  return id
80b60 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 57 72 69  ->pMethods->xWri
80b70 74 65 28 69 64 2c 20 70 42 75 66 2c 20 61 6d 74  te(id, pBuf, amt
80b80 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51 4c  , offset);.}.SQL
80b90 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
80ba0 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74  sqlite3OsTruncat
80bb0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
80bc0 69 64 2c 20 69 36 34 20 73 69 7a 65 29 7b 0a 20  id, i64 size){. 
80bd0 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74   return id->pMet
80be0 68 6f 64 73 2d 3e 78 54 72 75 6e 63 61 74 65 28  hods->xTruncate(
80bf0 69 64 2c 20 73 69 7a 65 29 3b 0a 7d 0a 53 51 4c  id, size);.}.SQL
80c00 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
80c10 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 73 71  sqlite3OsSync(sq
80c20 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
80c30 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 44 4f  int flags){.  DO
80c40 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28  _OS_MALLOC_TEST(
80c50 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 64  id);.  return id
80c60 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 79 6e  ->pMethods->xSyn
80c70 63 28 69 64 2c 20 66 6c 61 67 73 29 3b 0a 7d 0a  c(id, flags);.}.
80c80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
80c90 6e 74 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65  nt sqlite3OsFile
80ca0 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  Size(sqlite3_fil
80cb0 65 20 2a 69 64 2c 20 69 36 34 20 2a 70 53 69 7a  e *id, i64 *pSiz
80cc0 65 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c  e){.  DO_OS_MALL
80cd0 4f 43 5f 54 45 53 54 28 69 64 29 3b 0a 20 20 72  OC_TEST(id);.  r
80ce0 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f  eturn id->pMetho
80cf0 64 73 2d 3e 78 46 69 6c 65 53 69 7a 65 28 69 64  ds->xFileSize(id
80d00 2c 20 70 53 69 7a 65 29 3b 0a 7d 0a 53 51 4c 49  , pSize);.}.SQLI
80d10 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
80d20 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71 6c  qlite3OsLock(sql
80d30 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
80d40 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a 20 20  nt lockType){.  
80d50 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53  DO_OS_MALLOC_TES
80d60 54 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  T(id);.  return 
80d70 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 4c  id->pMethods->xL
80d80 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 79 70 65  ock(id, lockType
80d90 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
80da0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
80db0 73 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  sUnlock(sqlite3_
80dc0 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f  file *id, int lo
80dd0 63 6b 54 79 70 65 29 7b 0a 20 20 72 65 74 75 72  ckType){.  retur
80de0 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e  n id->pMethods->
80df0 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b  xUnlock(id, lock
80e00 54 79 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  Type);.}.SQLITE_
80e10 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
80e20 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76  te3OsCheckReserv
80e30 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  edLock(sqlite3_f
80e40 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52  ile *id, int *pR
80e50 65 73 4f 75 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f  esOut){.  DO_OS_
80e60 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 69 64 29 3b  MALLOC_TEST(id);
80e70 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d  .  return id->pM
80e80 65 74 68 6f 64 73 2d 3e 78 43 68 65 63 6b 52 65  ethods->xCheckRe
80e90 73 65 72 76 65 64 4c 6f 63 6b 28 69 64 2c 20 70  servedLock(id, p
80ea0 52 65 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54  ResOut);.}.SQLIT
80eb0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
80ec0 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72  lite3OsFileContr
80ed0 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ol(sqlite3_file 
80ee0 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69  *id, int op, voi
80ef0 64 20 2a 70 41 72 67 29 7b 0a 20 20 72 65 74 75  d *pArg){.  retu
80f00 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d  rn id->pMethods-
80f10 3e 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 69 64  >xFileControl(id
80f20 2c 20 6f 70 2c 20 70 41 72 67 29 3b 0a 7d 0a 53  , op, pArg);.}.S
80f30 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
80f40 74 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f  t sqlite3OsSecto
80f50 72 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69  rSize(sqlite3_fi
80f60 6c 65 20 2a 69 64 29 7b 0a 20 20 69 6e 74 20 28  le *id){.  int (
80f70 2a 78 53 65 63 74 6f 72 53 69 7a 65 29 28 73 71  *xSectorSize)(sq
80f80 6c 69 74 65 33 5f 66 69 6c 65 2a 29 20 3d 20 69  lite3_file*) = i
80f90 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 53 65  d->pMethods->xSe
80fa0 63 74 6f 72 53 69 7a 65 3b 0a 20 20 72 65 74 75  ctorSize;.  retu
80fb0 72 6e 20 28 78 53 65 63 74 6f 72 53 69 7a 65 20  rn (xSectorSize 
80fc0 3f 20 78 53 65 63 74 6f 72 53 69 7a 65 28 69 64  ? xSectorSize(id
80fd0 29 20 3a 20 53 51 4c 49 54 45 5f 44 45 46 41 55  ) : SQLITE_DEFAU
80fe0 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 29 3b  LT_SECTOR_SIZE);
80ff0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
81000 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 44  E int sqlite3OsD
81010 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
81020 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c  tics(sqlite3_fil
81030 65 20 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e  e *id){.  return
81040 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78   id->pMethods->x
81050 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
81060 73 74 69 63 73 28 69 64 29 3b 0a 7d 0a 0a 2f 2a  stics(id);.}../*
81070 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 67 72 6f  .** The next gro
81080 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 61  up of routines a
81090 72 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77  re convenience w
810a0 72 61 70 70 65 72 73 20 61 72 6f 75 6e 64 20 74  rappers around t
810b0 68 65 0a 2a 2a 20 56 46 53 20 6d 65 74 68 6f 64  he.** VFS method
810c0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
810d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
810e0 4f 73 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65  OsOpen(.  sqlite
810f0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20  3_vfs *pVfs, .  
81100 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74  const char *zPat
81110 68 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 66 69  h, .  sqlite3_fi
81120 6c 65 20 2a 70 46 69 6c 65 2c 20 0a 20 20 69 6e  le *pFile, .  in
81130 74 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20  t flags, .  int 
81140 2a 70 46 6c 61 67 73 4f 75 74 0a 29 7b 0a 20 20  *pFlagsOut.){.  
81150 69 6e 74 20 72 63 3b 0a 20 20 44 4f 5f 4f 53 5f  int rc;.  DO_OS_
81160 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 30 29 3b 0a  MALLOC_TEST(0);.
81170 20 20 2f 2a 20 30 78 37 66 31 66 20 69 73 20 61    /* 0x7f1f is a
81180 20 6d 61 73 6b 20 6f 66 20 53 51 4c 49 54 45 5f   mask of SQLITE_
81190 4f 50 45 4e 5f 20 66 6c 61 67 73 20 74 68 61 74  OPEN_ flags that
811a0 20 61 72 65 20 76 61 6c 69 64 20 74 6f 20 62 65   are valid to be
811b0 20 70 61 73 73 65 64 0a 20 20 2a 2a 20 64 6f 77   passed.  ** dow
811c0 6e 20 69 6e 74 6f 20 74 68 65 20 56 46 53 20 6c  n into the VFS l
811d0 61 79 65 72 2e 20 20 53 6f 6d 65 20 53 51 4c 49  ayer.  Some SQLI
811e0 54 45 5f 4f 50 45 4e 5f 20 66 6c 61 67 73 20 28  TE_OPEN_ flags (
811f0 66 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 20 20 2a  for example,.  *
81200 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46 55  * SQLITE_OPEN_FU
81210 4c 4c 4d 55 54 45 58 20 6f 72 20 53 51 4c 49 54  LLMUTEX or SQLIT
81220 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43 41 43  E_OPEN_SHAREDCAC
81230 48 45 29 20 61 72 65 20 62 6c 6f 63 6b 65 64 20  HE) are blocked 
81240 62 65 66 6f 72 65 0a 20 20 2a 2a 20 72 65 61 63  before.  ** reac
81250 68 69 6e 67 20 74 68 65 20 56 46 53 2e 20 2a 2f  hing the VFS. */
81260 0a 20 20 72 63 20 3d 20 70 56 66 73 2d 3e 78 4f  .  rc = pVfs->xO
81270 70 65 6e 28 70 56 66 73 2c 20 7a 50 61 74 68 2c  pen(pVfs, zPath,
81280 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 20 26 20   pFile, flags & 
81290 30 78 37 66 31 66 2c 20 70 46 6c 61 67 73 4f 75  0x7f1f, pFlagsOu
812a0 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  t);.  assert( rc
812b0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70  ==SQLITE_OK || p
812c0 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d  File->pMethods==
812d0 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  0 );.  return rc
812e0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
812f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
81300 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 5f 76  Delete(sqlite3_v
81310 66 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20  fs *pVfs, const 
81320 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 69 6e 74  char *zPath, int
81330 20 64 69 72 53 79 6e 63 29 7b 0a 20 20 72 65 74   dirSync){.  ret
81340 75 72 6e 20 70 56 66 73 2d 3e 78 44 65 6c 65 74  urn pVfs->xDelet
81350 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 64  e(pVfs, zPath, d
81360 69 72 53 79 6e 63 29 3b 0a 7d 0a 53 51 4c 49 54  irSync);.}.SQLIT
81370 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
81380 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 0a 20  lite3OsAccess(. 
81390 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
813a0 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  fs, .  const cha
813b0 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20 69 6e 74  r *zPath, .  int
813c0 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e 74 20 2a   flags, .  int *
813d0 70 52 65 73 4f 75 74 0a 29 7b 0a 20 20 44 4f 5f  pResOut.){.  DO_
813e0 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 28 30  OS_MALLOC_TEST(0
813f0 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73  );.  return pVfs
81400 2d 3e 78 41 63 63 65 73 73 28 70 56 66 73 2c 20  ->xAccess(pVfs, 
81410 7a 50 61 74 68 2c 20 66 6c 61 67 73 2c 20 70 52  zPath, flags, pR
81420 65 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45  esOut);.}.SQLITE
81430 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
81440 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61  ite3OsFullPathna
81450 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  me(.  sqlite3_vf
81460 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73  s *pVfs, .  cons
81470 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a  t char *zPath, .
81480 20 20 69 6e 74 20 6e 50 61 74 68 4f 75 74 2c 20    int nPathOut, 
81490 0a 20 20 63 68 61 72 20 2a 7a 50 61 74 68 4f 75  .  char *zPathOu
814a0 74 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56  t.){.  return pV
814b0 66 73 2d 3e 78 46 75 6c 6c 50 61 74 68 6e 61 6d  fs->xFullPathnam
814c0 65 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 6e  e(pVfs, zPath, n
814d0 50 61 74 68 4f 75 74 2c 20 7a 50 61 74 68 4f 75  PathOut, zPathOu
814e0 74 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51  t);.}.#ifndef SQ
814f0 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45  LITE_OMIT_LOAD_E
81500 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49 54 45 5f  XTENSION.SQLITE_
81510 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
81520 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 73 71  lite3OsDlOpen(sq
81530 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
81540 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61   const char *zPa
81550 74 68 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 56  th){.  return pV
81560 66 73 2d 3e 78 44 6c 4f 70 65 6e 28 70 56 66 73  fs->xDlOpen(pVfs
81570 2c 20 7a 50 61 74 68 29 3b 0a 7d 0a 53 51 4c 49  , zPath);.}.SQLI
81580 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
81590 73 71 6c 69 74 65 33 4f 73 44 6c 45 72 72 6f 72  sqlite3OsDlError
815a0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
815b0 66 73 2c 20 69 6e 74 20 6e 42 79 74 65 2c 20 63  fs, int nByte, c
815c0 68 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20  har *zBufOut){. 
815d0 20 70 56 66 73 2d 3e 78 44 6c 45 72 72 6f 72 28   pVfs->xDlError(
815e0 70 56 66 73 2c 20 6e 42 79 74 65 2c 20 7a 42 75  pVfs, nByte, zBu
815f0 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  fOut);.}.SQLITE_
81600 50 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73  PRIVATE void (*s
81610 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d 28 73 71  qlite3OsDlSym(sq
81620 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
81630 20 76 6f 69 64 20 2a 70 48 64 6c 65 2c 20 63 6f   void *pHdle, co
81640 6e 73 74 20 63 68 61 72 20 2a 7a 53 79 6d 29 29  nst char *zSym))
81650 28 76 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e  (void){.  return
81660 20 70 56 66 73 2d 3e 78 44 6c 53 79 6d 28 70 56   pVfs->xDlSym(pV
81670 66 73 2c 20 70 48 64 6c 65 2c 20 7a 53 79 6d 29  fs, pHdle, zSym)
81680 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
81690 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f  TE void sqlite3O
816a0 73 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  sDlClose(sqlite3
816b0 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64  _vfs *pVfs, void
816c0 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 70 56   *pHandle){.  pV
816d0 66 73 2d 3e 78 44 6c 43 6c 6f 73 65 28 70 56 66  fs->xDlClose(pVf
816e0 73 2c 20 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23  s, pHandle);.}.#
816f0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
81700 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53  OMIT_LOAD_EXTENS
81710 49 4f 4e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ION */.SQLITE_PR
81720 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
81730 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71  3OsRandomness(sq
81740 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
81750 20 69 6e 74 20 6e 42 79 74 65 2c 20 63 68 61 72   int nByte, char
81760 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 20 20 72 65   *zBufOut){.  re
81770 74 75 72 6e 20 70 56 66 73 2d 3e 78 52 61 6e 64  turn pVfs->xRand
81780 6f 6d 6e 65 73 73 28 70 56 66 73 2c 20 6e 42 79  omness(pVfs, nBy
81790 74 65 2c 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a  te, zBufOut);.}.
817a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
817b0 6e 74 20 73 71 6c 69 74 65 33 4f 73 53 6c 65 65  nt sqlite3OsSlee
817c0 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  p(sqlite3_vfs *p
817d0 56 66 73 2c 20 69 6e 74 20 6e 4d 69 63 72 6f 29  Vfs, int nMicro)
817e0 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d  {.  return pVfs-
817f0 3e 78 53 6c 65 65 70 28 70 56 66 73 2c 20 6e 4d  >xSleep(pVfs, nM
81800 69 63 72 6f 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  icro);.}.SQLITE_
81810 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
81820 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65  te3OsCurrentTime
81830 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
81840 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70 54 69 6d  fs, double *pTim
81850 65 4f 75 74 29 7b 0a 20 20 72 65 74 75 72 6e 20  eOut){.  return 
81860 70 56 66 73 2d 3e 78 43 75 72 72 65 6e 74 54 69  pVfs->xCurrentTi
81870 6d 65 28 70 56 66 73 2c 20 70 54 69 6d 65 4f 75  me(pVfs, pTimeOu
81880 74 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52  t);.}..SQLITE_PR
81890 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
818a0 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 0a 20  3OsOpenMalloc(. 
818b0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
818c0 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  fs, .  const cha
818d0 72 20 2a 7a 46 69 6c 65 2c 20 0a 20 20 73 71 6c  r *zFile, .  sql
818e0 69 74 65 33 5f 66 69 6c 65 20 2a 2a 70 70 46 69  ite3_file **ppFi
818f0 6c 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67 73  le, .  int flags
81900 2c 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61  ,.  int *pOutFla
81910 67 73 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  gs.){.  int rc =
81920 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
81930 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
81940 46 69 6c 65 3b 0a 20 20 70 46 69 6c 65 20 3d 20  File;.  pFile = 
81950 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29  (sqlite3_file *)
81960 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 70 56  sqlite3Malloc(pV
81970 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 29 3b 0a 20  fs->szOsFile);. 
81980 20 69 66 28 20 70 46 69 6c 65 20 29 7b 0a 20 20   if( pFile ){.  
81990 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
819a0 4f 70 65 6e 28 70 56 66 73 2c 20 7a 46 69 6c 65  Open(pVfs, zFile
819b0 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20  , pFile, flags, 
819c0 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20  pOutFlags);.    
819d0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
819e0 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  K ){.      sqlit
819f0 65 33 5f 66 72 65 65 28 70 46 69 6c 65 29 3b 0a  e3_free(pFile);.
81a00 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
81a10 20 2a 70 70 46 69 6c 65 20 3d 20 70 46 69 6c 65   *ppFile = pFile
81a20 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
81a30 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54  turn rc;.}.SQLIT
81a40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
81a50 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65  lite3OsCloseFree
81a60 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
81a70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 20  File){.  int rc 
81a80 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61  = SQLITE_OK;.  a
81a90 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a  ssert( pFile );.
81aa0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
81ab0 43 6c 6f 73 65 28 70 46 69 6c 65 29 3b 0a 20 20  Close(pFile);.  
81ac0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69  sqlite3_free(pFi
81ad0 6c 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  le);.  return rc
81ae0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
81af0 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 77 72  function is a wr
81b00 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68 65  apper around the
81b10 20 4f 53 20 73 70 65 63 69 66 69 63 20 69 6d 70   OS specific imp
81b20 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 0a 2a  lementation of.*
81b30 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69  * sqlite3_os_ini
81b40 74 28 29 2e 20 54 68 65 20 70 75 72 70 6f 73 65  t(). The purpose
81b50 20 6f 66 20 74 68 65 20 77 72 61 70 70 65 72 20   of the wrapper 
81b60 69 73 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68  is to provide th
81b70 65 0a 2a 2a 20 61 62 69 6c 69 74 79 20 74 6f 20  e.** ability to 
81b80 73 69 6d 75 6c 61 74 65 20 61 20 6d 61 6c 6c 6f  simulate a mallo
81b90 63 20 66 61 69 6c 75 72 65 2c 20 73 6f 20 74 68  c failure, so th
81ba0 61 74 20 74 68 65 20 68 61 6e 64 6c 69 6e 67 20  at the handling 
81bb0 6f 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 69  of an.** error i
81bc0 6e 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69  n sqlite3_os_ini
81bd0 74 28 29 20 62 79 20 74 68 65 20 75 70 70 65 72  t() by the upper
81be0 20 6c 61 79 65 72 73 20 63 61 6e 20 62 65 20 74   layers can be t
81bf0 65 73 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ested..*/.SQLITE
81c00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
81c10 69 74 65 33 4f 73 49 6e 69 74 28 76 6f 69 64 29  ite3OsInit(void)
81c20 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73 71  {.  void *p = sq
81c30 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 31 30 29  lite3_malloc(10)
81c40 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
81c50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
81c60 45 4d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  EM;.  sqlite3_fr
81c70 65 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ee(p);.  return 
81c80 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28  sqlite3_os_init(
81c90 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  );.}../*.** The 
81ca0 6c 69 73 74 20 6f 66 20 61 6c 6c 20 72 65 67 69  list of all regi
81cb0 73 74 65 72 65 64 20 56 46 53 20 69 6d 70 6c 65  stered VFS imple
81cc0 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73  mentations..*/.s
81cd0 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66  tatic sqlite3_vf
81ce0 73 20 2a 20 53 51 4c 49 54 45 5f 57 53 44 20 76  s * SQLITE_WSD v
81cf0 66 73 4c 69 73 74 20 3d 20 30 3b 0a 23 64 65 66  fsList = 0;.#def
81d00 69 6e 65 20 76 66 73 4c 69 73 74 20 47 4c 4f 42  ine vfsList GLOB
81d10 41 4c 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  AL(sqlite3_vfs *
81d20 2c 20 76 66 73 4c 69 73 74 29 0a 0a 2f 2a 0a 2a  , vfsList)../*.*
81d30 2a 20 4c 6f 63 61 74 65 20 61 20 56 46 53 20 62  * Locate a VFS b
81d40 79 20 6e 61 6d 65 2e 20 20 49 66 20 6e 6f 20 6e  y name.  If no n
81d50 61 6d 65 20 69 73 20 67 69 76 65 6e 2c 20 73 69  ame is given, si
81d60 6d 70 6c 79 20 72 65 74 75 72 6e 20 74 68 65 0a  mply return the.
81d70 2a 2a 20 66 69 72 73 74 20 56 46 53 20 6f 6e 20  ** first VFS on 
81d80 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c  the list..*/.SQL
81d90 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
81da0 76 66 73 20 2a 73 71 6c 69 74 65 33 5f 76 66 73  vfs *sqlite3_vfs
81db0 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63 68 61 72  _find(const char
81dc0 20 2a 7a 56 66 73 29 7b 0a 20 20 73 71 6c 69 74   *zVfs){.  sqlit
81dd0 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 30  e3_vfs *pVfs = 0
81de0 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  ;.#if SQLITE_THR
81df0 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65  EADSAFE.  sqlite
81e00 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 0a  3_mutex *mutex;.
81e10 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53  #endif.#ifndef S
81e20 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
81e30 4e 49 54 0a 20 20 69 6e 74 20 72 63 20 3d 20 73  NIT.  int rc = s
81e40 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
81e50 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  e();.  if( rc ) 
81e60 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66  return 0;.#endif
81e70 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
81e80 41 44 53 41 46 45 0a 20 20 6d 75 74 65 78 20 3d  ADSAFE.  mutex =
81e90 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
81ea0 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
81eb0 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a  STATIC_MASTER);.
81ec0 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33  #endif.  sqlite3
81ed0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74  _mutex_enter(mut
81ee0 65 78 29 3b 0a 20 20 66 6f 72 28 70 56 66 73 20  ex);.  for(pVfs 
81ef0 3d 20 76 66 73 4c 69 73 74 3b 20 70 56 66 73 3b  = vfsList; pVfs;
81f00 20 70 56 66 73 3d 70 56 66 73 2d 3e 70 4e 65 78   pVfs=pVfs->pNex
81f10 74 29 7b 0a 20 20 20 20 69 66 28 20 7a 56 66 73  t){.    if( zVfs
81f20 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ==0 ) break;.   
81f30 20 69 66 28 20 73 74 72 63 6d 70 28 7a 56 66 73   if( strcmp(zVfs
81f40 2c 20 70 56 66 73 2d 3e 7a 4e 61 6d 65 29 3d 3d  , pVfs->zName)==
81f50 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20  0 ) break;.  }. 
81f60 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
81f70 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72  eave(mutex);.  r
81f80 65 74 75 72 6e 20 70 56 66 73 3b 0a 7d 0a 0a 2f  eturn pVfs;.}../
81f90 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 61 20 56 46  *.** Unlink a VF
81fa0 53 20 66 72 6f 6d 20 74 68 65 20 6c 69 6e 6b 65  S from the linke
81fb0 64 20 6c 69 73 74 0a 2a 2f 0a 73 74 61 74 69 63  d list.*/.static
81fc0 20 76 6f 69 64 20 76 66 73 55 6e 6c 69 6e 6b 28   void vfsUnlink(
81fd0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
81fe0 73 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  s){.  assert( sq
81ff0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
82000 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c  (sqlite3MutexAll
82010 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
82020 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 20  STATIC_MASTER)) 
82030 29 3b 0a 20 20 69 66 28 20 70 56 66 73 3d 3d 30  );.  if( pVfs==0
82040 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f 2d 6f 70   ){.    /* No-op
82050 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   */.  }else if( 
82060 76 66 73 4c 69 73 74 3d 3d 70 56 66 73 20 29 7b  vfsList==pVfs ){
82070 0a 20 20 20 20 76 66 73 4c 69 73 74 20 3d 20 70  .    vfsList = p
82080 56 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65  Vfs->pNext;.  }e
82090 6c 73 65 20 69 66 28 20 76 66 73 4c 69 73 74 20  lse if( vfsList 
820a0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  ){.    sqlite3_v
820b0 66 73 20 2a 70 20 3d 20 76 66 73 4c 69 73 74 3b  fs *p = vfsList;
820c0 0a 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 70  .    while( p->p
820d0 4e 65 78 74 20 26 26 20 70 2d 3e 70 4e 65 78 74  Next && p->pNext
820e0 21 3d 70 56 66 73 20 29 7b 0a 20 20 20 20 20 20  !=pVfs ){.      
820f0 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20  p = p->pNext;.  
82100 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 70    }.    if( p->p
82110 4e 65 78 74 3d 3d 70 56 66 73 20 29 7b 0a 20 20  Next==pVfs ){.  
82120 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70      p->pNext = p
82130 56 66 73 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  Vfs->pNext;.    
82140 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  }.  }.}../*.** R
82150 65 67 69 73 74 65 72 20 61 20 56 46 53 20 77 69  egister a VFS wi
82160 74 68 20 74 68 65 20 73 79 73 74 65 6d 2e 20 20  th the system.  
82170 49 74 20 69 73 20 68 61 72 6d 6c 65 73 73 20 74  It is harmless t
82180 6f 20 72 65 67 69 73 74 65 72 20 74 68 65 20 73  o register the s
82190 61 6d 65 0a 2a 2a 20 56 46 53 20 6d 75 6c 74 69  ame.** VFS multi
821a0 70 6c 65 20 74 69 6d 65 73 2e 20 20 54 68 65 20  ple times.  The 
821b0 6e 65 77 20 56 46 53 20 62 65 63 6f 6d 65 73 20  new VFS becomes 
821c0 74 68 65 20 64 65 66 61 75 6c 74 20 69 66 20 6d  the default if m
821d0 61 6b 65 44 66 6c 74 20 69 73 0a 2a 2a 20 74 72  akeDflt is.** tr
821e0 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ue..*/.SQLITE_AP
821f0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 66  I int sqlite3_vf
82200 73 5f 72 65 67 69 73 74 65 72 28 73 71 6c 69 74  s_register(sqlit
82210 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e  e3_vfs *pVfs, in
82220 74 20 6d 61 6b 65 44 66 6c 74 29 7b 0a 20 20 73  t makeDflt){.  s
82230 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
82240 74 65 78 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66  tex = 0;.#ifndef
82250 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
82260 4f 49 4e 49 54 0a 20 20 69 6e 74 20 72 63 20 3d  OINIT.  int rc =
82270 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
82280 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20  ize();.  if( rc 
82290 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e  ) return rc;.#en
822a0 64 69 66 0a 20 20 6d 75 74 65 78 20 3d 20 73 71  dif.  mutex = sq
822b0 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
822c0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
822d0 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73  TIC_MASTER);.  s
822e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
822f0 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 76 66 73  er(mutex);.  vfs
82300 55 6e 6c 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20  Unlink(pVfs);.  
82310 69 66 28 20 6d 61 6b 65 44 66 6c 74 20 7c 7c 20  if( makeDflt || 
82320 76 66 73 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20  vfsList==0 ){.  
82330 20 20 70 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20    pVfs->pNext = 
82340 76 66 73 4c 69 73 74 3b 0a 20 20 20 20 76 66 73  vfsList;.    vfs
82350 4c 69 73 74 20 3d 20 70 56 66 73 3b 0a 20 20 7d  List = pVfs;.  }
82360 65 6c 73 65 7b 0a 20 20 20 20 70 56 66 73 2d 3e  else{.    pVfs->
82370 70 4e 65 78 74 20 3d 20 76 66 73 4c 69 73 74 2d  pNext = vfsList-
82380 3e 70 4e 65 78 74 3b 0a 20 20 20 20 76 66 73 4c  >pNext;.    vfsL
82390 69 73 74 2d 3e 70 4e 65 78 74 20 3d 20 70 56 66  ist->pNext = pVf
823a0 73 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  s;.  }.  assert(
823b0 76 66 73 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69  vfsList);.  sqli
823c0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
823d0 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
823e0 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
823f0 2a 0a 2a 2a 20 55 6e 72 65 67 69 73 74 65 72 20  *.** Unregister 
82400 61 20 56 46 53 20 73 6f 20 74 68 61 74 20 69 74  a VFS so that it
82410 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 61 63   is no longer ac
82420 63 65 73 73 69 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  cessible..*/.SQL
82430 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
82440 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74  te3_vfs_unregist
82450 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  er(sqlite3_vfs *
82460 70 56 66 73 29 7b 0a 23 69 66 20 53 51 4c 49 54  pVfs){.#if SQLIT
82470 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73  E_THREADSAFE.  s
82480 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
82490 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  tex = sqlite3Mut
824a0 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
824b0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
824c0 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71  ER);.#endif.  sq
824d0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
824e0 72 28 6d 75 74 65 78 29 3b 0a 20 20 76 66 73 55  r(mutex);.  vfsU
824f0 6e 6c 69 6e 6b 28 70 56 66 73 29 3b 0a 20 20 73  nlink(pVfs);.  s
82500 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
82510 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  ve(mutex);.  ret
82520 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
82530 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
82540 2a 20 45 6e 64 20 6f 66 20 6f 73 2e 63 20 2a 2a  * End of os.c **
82550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82580 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
82590 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 61 75  * Begin file fau
825a0 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  lt.c ***********
825b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
825c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
825d0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4a 61 6e  /./*.** 2008 Jan
825e0 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   22.**.** The au
825f0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
82600 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
82610 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
82620 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
82630 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
82640 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
82650 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
82660 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
82670 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
82680 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
82690 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
826a0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
826b0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
826c0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
826d0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
826e0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
826f0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
82700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
82740 2a 2a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 66 61  **.**.** $Id: fa
82750 75 6c 74 2e 63 2c 76 20 31 2e 31 31 20 32 30 30  ult.c,v 1.11 200
82760 38 2f 30 39 2f 30 32 20 30 30 3a 35 32 3a 35 32  8/09/02 00:52:52
82770 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f   drh Exp $.*/../
82780 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
82790 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 6f 20  ontains code to 
827a0 73 75 70 70 6f 72 74 20 74 68 65 20 63 6f 6e 63  support the conc
827b0 65 70 74 20 6f 66 20 22 62 65 6e 69 67 6e 22 20  ept of "benign" 
827c0 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75  .** malloc failu
827d0 72 65 73 20 28 77 68 65 6e 20 74 68 65 20 78 4d  res (when the xM
827e0 61 6c 6c 6f 63 28 29 20 6f 72 20 78 52 65 61 6c  alloc() or xReal
827f0 6c 6f 63 28 29 20 6d 65 74 68 6f 64 20 6f 66 20  loc() method of 
82800 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d  the.** sqlite3_m
82810 65 6d 5f 6d 65 74 68 6f 64 73 20 73 74 72 75 63  em_methods struc
82820 74 75 72 65 20 66 61 69 6c 73 20 74 6f 20 61 6c  ture fails to al
82830 6c 6f 63 61 74 65 20 61 20 62 6c 6f 63 6b 20 6f  locate a block o
82840 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6e 64 20  f memory.** and 
82850 72 65 74 75 72 6e 73 20 30 29 2e 20 0a 2a 2a 0a  returns 0). .**.
82860 2a 2a 20 4d 6f 73 74 20 6d 61 6c 6c 6f 63 20 66  ** Most malloc f
82870 61 69 6c 75 72 65 73 20 61 72 65 20 6e 6f 6e 2d  ailures are non-
82880 62 65 6e 69 67 6e 2e 20 41 66 74 65 72 20 74 68  benign. After th
82890 65 79 20 6f 63 63 75 72 2c 20 53 51 4c 69 74 65  ey occur, SQLite
828a0 0a 2a 2a 20 61 62 61 6e 64 6f 6e 73 20 74 68 65  .** abandons the
828b0 20 63 75 72 72 65 6e 74 20 6f 70 65 72 61 74 69   current operati
828c0 6f 6e 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61  on and returns a
828d0 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 28 75 73  n error code (us
828e0 75 61 6c 6c 79 0a 2a 2a 20 53 51 4c 49 54 45 5f  ually.** SQLITE_
828f0 4e 4f 4d 45 4d 29 20 74 6f 20 74 68 65 20 75 73  NOMEM) to the us
82900 65 72 2e 20 48 6f 77 65 76 65 72 2c 20 73 6f 6d  er. However, som
82910 65 74 69 6d 65 73 20 61 20 66 61 75 6c 74 20 69  etimes a fault i
82920 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c  s not necessaril
82930 79 0a 2a 2a 20 66 61 74 61 6c 2e 20 46 6f 72 20  y.** fatal. For 
82940 65 78 61 6d 70 6c 65 2c 20 69 66 20 61 20 6d 61  example, if a ma
82950 6c 6c 6f 63 20 66 61 69 6c 73 20 77 68 69 6c 65  lloc fails while
82960 20 72 65 73 69 7a 69 6e 67 20 61 20 68 61 73 68   resizing a hash
82970 20 74 61 62 6c 65 2c 20 74 68 69 73 20 0a 2a 2a   table, this .**
82980 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 72   is completely r
82990 65 63 6f 76 65 72 61 62 6c 65 20 73 69 6d 70 6c  ecoverable simpl
829a0 79 20 62 79 20 6e 6f 74 20 63 61 72 72 79 69 6e  y by not carryin
829b0 67 20 6f 75 74 20 74 68 65 20 72 65 73 69 7a 65  g out the resize
829c0 2e 20 54 68 65 20 0a 2a 2a 20 68 61 73 68 20 74  . The .** hash t
829d0 61 62 6c 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e  able will contin
829e0 75 65 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 6e  ue to function n
829f0 6f 72 6d 61 6c 6c 79 2e 20 20 53 6f 20 61 20 6d  ormally.  So a m
82a00 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 0a 2a  alloc failure .*
82a10 2a 20 64 75 72 69 6e 67 20 61 20 68 61 73 68 20  * during a hash 
82a20 74 61 62 6c 65 20 72 65 73 69 7a 65 20 69 73 20  table resize is 
82a30 61 20 62 65 6e 69 67 6e 20 66 61 75 6c 74 2e 0a  a benign fault..
82a40 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  */...#ifndef SQL
82a50 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e  ITE_OMIT_BUILTIN
82a60 5f 54 45 53 54 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f  _TEST../*.** Glo
82a70 62 61 6c 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a  bal variables..*
82a80 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
82a90 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f   BenignMallocHoo
82aa0 6b 73 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48  ks BenignMallocH
82ab0 6f 6f 6b 73 3b 0a 73 74 61 74 69 63 20 53 51 4c  ooks;.static SQL
82ac0 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 42  ITE_WSD struct B
82ad0 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73  enignMallocHooks
82ae0 20 7b 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e   {.  void (*xBen
82af0 69 67 6e 42 65 67 69 6e 29 28 76 6f 69 64 29 3b  ignBegin)(void);
82b00 0a 20 20 76 6f 69 64 20 28 2a 78 42 65 6e 69 67  .  void (*xBenig
82b10 6e 45 6e 64 29 28 76 6f 69 64 29 3b 0a 7d 20 73  nEnd)(void);.} s
82b20 71 6c 69 74 65 33 48 6f 6f 6b 73 20 3d 20 7b 20  qlite3Hooks = { 
82b30 30 2c 20 30 20 7d 3b 0a 0a 2f 2a 20 54 68 65 20  0, 0 };../* The 
82b40 22 77 73 64 48 6f 6f 6b 73 22 20 6d 61 63 72 6f  "wsdHooks" macro
82b50 20 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f   will resolve to
82b60 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
82b70 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 48 6f 6f   BenignMallocHoo
82b80 6b 73 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e  ks.** structure.
82b90 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 73 74    If writable st
82ba0 61 74 69 63 20 64 61 74 61 20 69 73 20 75 6e 73  atic data is uns
82bb0 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 20  upported on the 
82bc0 74 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20 68 61  target,.** we ha
82bd0 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65  ve to locate the
82be0 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 61 74   state vector at
82bf0 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74   run-time.  In t
82c00 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a  he more common.*
82c10 2a 20 63 61 73 65 20 77 68 65 72 65 20 77 72 69  * case where wri
82c20 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74  table static dat
82c30 61 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c 20  a is supported, 
82c40 77 73 64 48 6f 6f 6b 73 20 63 61 6e 20 72 65 66  wsdHooks can ref
82c50 65 72 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 74  er directly.** t
82c60 6f 20 74 68 65 20 22 73 71 6c 69 74 65 33 48 6f  o the "sqlite3Ho
82c70 6f 6b 73 22 20 73 74 61 74 65 20 76 65 63 74 6f  oks" state vecto
82c80 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f 76 65  r declared above
82c90 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
82ca0 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20 64 65  TE_OMIT_WSD.# de
82cb0 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 49 6e 69  fine wsdHooksIni
82cc0 74 20 5c 0a 20 20 42 65 6e 69 67 6e 4d 61 6c 6c  t \.  BenignMall
82cd0 6f 63 48 6f 6f 6b 73 20 2a 78 20 3d 20 26 47 4c  ocHooks *x = &GL
82ce0 4f 42 41 4c 28 42 65 6e 69 67 6e 4d 61 6c 6c 6f  OBAL(BenignMallo
82cf0 63 48 6f 6f 6b 73 2c 73 71 6c 69 74 65 33 48 6f  cHooks,sqlite3Ho
82d00 6f 6b 73 29 0a 23 20 64 65 66 69 6e 65 20 77 73  oks).# define ws
82d10 64 48 6f 6f 6b 73 20 78 5b 30 5d 0a 23 65 6c 73  dHooks x[0].#els
82d20 65 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f  e.# define wsdHo
82d30 6f 6b 73 49 6e 69 74 0a 23 20 64 65 66 69 6e 65  oksInit.# define
82d40 20 77 73 64 48 6f 6f 6b 73 20 73 71 6c 69 74 65   wsdHooks sqlite
82d50 33 48 6f 6f 6b 73 0a 23 65 6e 64 69 66 0a 0a 0a  3Hooks.#endif...
82d60 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 68  /*.** Register h
82d70 6f 6f 6b 73 20 74 6f 20 63 61 6c 6c 20 77 68 65  ooks to call whe
82d80 6e 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65  n sqlite3BeginBe
82d90 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 6e 64  nignMalloc() and
82da0 0a 2a 2a 20 73 71 6c 69 74 65 33 45 6e 64 42 65  .** sqlite3EndBe
82db0 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 20 61 72 65  nignMalloc() are
82dc0 20 63 61 6c 6c 65 64 2c 20 72 65 73 70 65 63 74   called, respect
82dd0 69 76 65 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ively..*/.SQLITE
82de0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
82df0 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f  lite3BenignMallo
82e00 63 48 6f 6f 6b 73 28 0a 20 20 76 6f 69 64 20 28  cHooks(.  void (
82e10 2a 78 42 65 6e 69 67 6e 42 65 67 69 6e 29 28 76  *xBenignBegin)(v
82e20 6f 69 64 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78  oid),.  void (*x
82e30 42 65 6e 69 67 6e 45 6e 64 29 28 76 6f 69 64 29  BenignEnd)(void)
82e40 0a 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e  .){.  wsdHooksIn
82e50 69 74 3b 0a 20 20 77 73 64 48 6f 6f 6b 73 2e 78  it;.  wsdHooks.x
82e60 42 65 6e 69 67 6e 42 65 67 69 6e 20 3d 20 78 42  BenignBegin = xB
82e70 65 6e 69 67 6e 42 65 67 69 6e 3b 0a 20 20 77 73  enignBegin;.  ws
82e80 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e  dHooks.xBenignEn
82e90 64 20 3d 20 78 42 65 6e 69 67 6e 45 6e 64 3b 0a  d = xBenignEnd;.
82ea0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 28 73  }../*.** This (s
82eb0 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d  qlite3EndBenignM
82ec0 61 6c 6c 6f 63 28 29 29 20 69 73 20 63 61 6c 6c  alloc()) is call
82ed0 65 64 20 62 79 20 53 51 4c 69 74 65 20 63 6f 64  ed by SQLite cod
82ee0 65 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68  e to indicate th
82ef0 61 74 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74  at.** subsequent
82f00 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73   malloc failures
82f10 20 61 72 65 20 62 65 6e 69 67 6e 2e 20 41 20 63   are benign. A c
82f20 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 45 6e  all to sqlite3En
82f30 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a  dBenignMalloc().
82f40 2a 2a 20 69 6e 64 69 63 61 74 65 73 20 74 68 61  ** indicates tha
82f50 74 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c  t subsequent mal
82f60 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61 72 65  loc failures are
82f70 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e 0a 2a 2f 0a   non-benign..*/.
82f80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
82f90 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e  oid sqlite3Begin
82fa0 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69  BenignMalloc(voi
82fb0 64 29 7b 0a 20 20 77 73 64 48 6f 6f 6b 73 49 6e  d){.  wsdHooksIn
82fc0 69 74 3b 0a 20 20 69 66 28 20 77 73 64 48 6f 6f  it;.  if( wsdHoo
82fd0 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e 20  ks.xBenignBegin 
82fe0 29 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b 73 2e  ){.    wsdHooks.
82ff0 78 42 65 6e 69 67 6e 42 65 67 69 6e 28 29 3b 0a  xBenignBegin();.
83000 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49    }.}.SQLITE_PRI
83010 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
83020 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  3EndBenignMalloc
83030 28 76 6f 69 64 29 7b 0a 20 20 77 73 64 48 6f 6f  (void){.  wsdHoo
83040 6b 73 49 6e 69 74 3b 0a 20 20 69 66 28 20 77 73  ksInit;.  if( ws
83050 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e  dHooks.xBenignEn
83060 64 20 29 7b 0a 20 20 20 20 77 73 64 48 6f 6f 6b  d ){.    wsdHook
83070 73 2e 78 42 65 6e 69 67 6e 45 6e 64 28 29 3b 0a  s.xBenignEnd();.
83080 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 20 20    }.}..#endif   
83090 2f 2a 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /* #ifndef SQLIT
830a0 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54  E_OMIT_BUILTIN_T
830b0 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  EST */../*******
830c0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66  ******* End of f
830d0 61 75 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ault.c *********
830e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
830f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83100 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
83110 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
83120 6c 65 20 6d 65 6d 30 2e 63 20 2a 2a 2a 2a 2a 2a  le mem0.c ******
83130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83150 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
83160 30 38 20 4f 63 74 6f 62 65 72 20 32 38 0a 2a 2a  08 October 28.**
83170 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
83180 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
83190 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
831a0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
831b0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
831c0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
831d0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
831e0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
831f0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
83200 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
83210 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
83220 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
83230 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
83240 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
83250 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
83260 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
83270 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
83280 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
83290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
832a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
832b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
832c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
832d0 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
832e0 74 61 69 6e 73 20 61 20 6e 6f 2d 6f 70 20 6d 65  tains a no-op me
832f0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
83300 64 72 69 76 65 72 73 20 66 6f 72 20 75 73 65 20  drivers for use 
83310 77 68 65 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 5a  when.** SQLITE_Z
83320 45 52 4f 5f 4d 41 4c 4c 4f 43 20 69 73 20 64 65  ERO_MALLOC is de
83330 66 69 6e 65 64 2e 20 20 54 68 65 20 61 6c 6c 6f  fined.  The allo
83340 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73 20 69  cation drivers i
83350 6d 70 6c 65 6d 65 6e 74 65 64 0a 2a 2a 20 68 65  mplemented.** he
83360 72 65 20 61 6c 77 61 79 73 20 66 61 69 6c 2e 20  re always fail. 
83370 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74   SQLite will not
83380 20 6f 70 65 72 61 74 65 20 77 69 74 68 20 74 68   operate with th
83390 65 73 65 20 64 72 69 76 65 72 73 2e 20 20 54 68  ese drivers.  Th
833a0 65 73 65 0a 2a 2a 20 61 72 65 20 6d 65 72 65 6c  ese.** are merel
833b0 79 20 70 6c 61 63 65 68 6f 6c 64 65 72 73 2e 20  y placeholders. 
833c0 20 52 65 61 6c 20 64 72 69 76 65 72 73 20 6d 75   Real drivers mu
833d0 73 74 20 62 65 20 73 75 62 73 74 69 74 75 74 65  st be substitute
833e0 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74  d using.** sqlit
833f0 65 33 5f 63 6f 6e 66 69 67 28 29 20 62 65 66 6f  e3_config() befo
83400 72 65 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f  re SQLite will o
83410 70 65 72 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  perate..**.** $I
83420 64 3a 20 6d 65 6d 30 2e 63 2c 76 20 31 2e 31 20  d: mem0.c,v 1.1 
83430 32 30 30 38 2f 31 30 2f 32 38 20 31 38 3a 35 38  2008/10/28 18:58
83440 3a 32 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :20 drh Exp $.*/
83450 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72  ../*.** This ver
83460 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f  sion of the memo
83470 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20  ry allocator is 
83480 74 68 65 20 64 65 66 61 75 6c 74 2e 20 20 49 74  the default.  It
83490 20 69 73 0a 2a 2a 20 75 73 65 64 20 77 68 65 6e   is.** used when
834a0 20 6e 6f 20 6f 74 68 65 72 20 6d 65 6d 6f 72 79   no other memory
834b0 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 73 70   allocator is sp
834c0 65 63 69 66 69 65 64 20 75 73 69 6e 67 20 63 6f  ecified using co
834d0 6d 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6d 61  mpile-time.** ma
834e0 63 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  cros..*/.#ifdef 
834f0 53 51 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c  SQLITE_ZERO_MALL
83500 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4e 6f 2d 6f 70 20  OC../*.** No-op 
83510 76 65 72 73 69 6f 6e 73 20 6f 66 20 61 6c 6c 20  versions of all 
83520 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
83530 6e 20 72 6f 75 74 69 6e 65 73 0a 2a 2f 0a 73 74  n routines.*/.st
83540 61 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74  atic void *sqlit
83550 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20  e3MemMalloc(int 
83560 6e 42 79 74 65 29 7b 20 72 65 74 75 72 6e 20 30  nByte){ return 0
83570 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ; }.static void 
83580 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 28 76  sqlite3MemFree(v
83590 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 20 72 65  oid *pPrior){ re
835a0 74 75 72 6e 3b 20 7d 0a 73 74 61 74 69 63 20 76  turn; }.static v
835b0 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52  oid *sqlite3MemR
835c0 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72  ealloc(void *pPr
835d0 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b  ior, int nByte){
835e0 20 72 65 74 75 72 6e 20 30 3b 20 7d 0a 73 74 61   return 0; }.sta
835f0 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  tic int sqlite3M
83600 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 50 72  emSize(void *pPr
83610 69 6f 72 29 7b 20 72 65 74 75 72 6e 20 30 3b 20  ior){ return 0; 
83620 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  }.static int sql
83630 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69  ite3MemRoundup(i
83640 6e 74 20 6e 29 7b 20 72 65 74 75 72 6e 20 6e 3b  nt n){ return n;
83650 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71   }.static int sq
83660 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28 76 6f 69  lite3MemInit(voi
83670 64 20 2a 4e 6f 74 55 73 65 64 29 7b 20 72 65 74  d *NotUsed){ ret
83680 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d  urn SQLITE_OK; }
83690 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
836a0 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28  ite3MemShutdown(
836b0 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 20  void *NotUsed){ 
836c0 72 65 74 75 72 6e 3b 20 7d 0a 0a 2f 2a 0a 2a 2a  return; }../*.**
836d0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
836e0 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e   the only routin
836f0 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 77  e in this file w
83700 69 74 68 20 65 78 74 65 72 6e 61 6c 20 6c 69 6e  ith external lin
83710 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75  kage..**.** Popu
83720 6c 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76  late the low-lev
83730 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  el memory alloca
83740 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f  tion function po
83750 69 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c  inters in.** sql
83760 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
83770 2e 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73  .m with pointers
83780 20 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73   to the routines
83790 20 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a   in this file..*
837a0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
837b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
837c0 53 65 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29  SetDefault(void)
837d0 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  {.  static const
837e0 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
837f0 68 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68  hods defaultMeth
83800 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c  ods = {.     sql
83810 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20  ite3MemMalloc,. 
83820 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72      sqlite3MemFr
83830 65 65 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  ee,.     sqlite3
83840 4d 65 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20  MemRealloc,.    
83850 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c   sqlite3MemSize,
83860 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  .     sqlite3Mem
83870 52 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71  Roundup,.     sq
83880 6c 69 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20  lite3MemInit,.  
83890 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75     sqlite3MemShu
838a0 74 64 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20  tdown,.     0.  
838b0 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  };.  sqlite3_con
838c0 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49  fig(SQLITE_CONFI
838d0 47 5f 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75  G_MALLOC, &defau
838e0 6c 74 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 23  ltMethods);.}..#
838f0 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
83900 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 20 2a 2f 0a 0a  ZERO_MALLOC */..
83910 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
83920 45 6e 64 20 6f 66 20 6d 65 6d 30 2e 63 20 2a 2a  End of mem0.c **
83930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
83960 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
83970 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d 31 2e  Begin file mem1.
83980 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
83990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
839a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
839b0 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75 73  /*.** 2007 Augus
839c0 74 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  t 14.**.** The a
839d0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
839e0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
839f0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
83a00 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
83a10 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
83a20 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
83a30 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
83a40 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
83a50 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
83a60 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
83a70 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
83a80 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
83a90 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
83aa0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
83ab0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
83ac0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
83ad0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
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 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83b20 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ***.**.** This f
83b30 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6c 6f 77  ile contains low
83b40 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c  -level memory al
83b50 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76 65 72 73  location drivers
83b60 20 66 6f 72 20 77 68 65 6e 0a 2a 2a 20 53 51 4c   for when.** SQL
83b70 69 74 65 20 77 69 6c 6c 20 75 73 65 20 74 68 65  ite will use the
83b80 20 73 74 61 6e 64 61 72 64 20 43 2d 6c 69 62 72   standard C-libr
83b90 61 72 79 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c  ary malloc/reall
83ba0 6f 63 2f 66 72 65 65 20 69 6e 74 65 72 66 61 63  oc/free interfac
83bb0 65 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 74  e.** to obtain t
83bc0 68 65 20 6d 65 6d 6f 72 79 20 69 74 20 6e 65 65  he memory it nee
83bd0 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ds..**.** This f
83be0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70  ile contains imp
83bf0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20  lementations of 
83c00 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65  the low-level me
83c10 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a  mory allocation.
83c20 2a 2a 20 72 6f 75 74 69 6e 65 73 20 73 70 65 63  ** routines spec
83c30 69 66 69 65 64 20 69 6e 20 74 68 65 20 73 71 6c  ified in the sql
83c40 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
83c50 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 24   object..**.** $
83c60 49 64 3a 20 6d 65 6d 31 2e 63 2c 76 20 31 2e 33  Id: mem1.c,v 1.3
83c70 30 20 32 30 30 39 2f 30 33 2f 32 33 20 30 34 3a  0 2009/03/23 04:
83c80 33 33 3a 33 33 20 64 61 6e 69 65 6c 6b 31 39 37  33:33 danielk197
83c90 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  7 Exp $.*/../*.*
83ca0 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  * This version o
83cb0 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  f the memory all
83cc0 6f 63 61 74 6f 72 20 69 73 20 74 68 65 20 64 65  ocator is the de
83cd0 66 61 75 6c 74 2e 20 20 49 74 20 69 73 0a 2a 2a  fault.  It is.**
83ce0 20 75 73 65 64 20 77 68 65 6e 20 6e 6f 20 6f 74   used when no ot
83cf0 68 65 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  her memory alloc
83d00 61 74 6f 72 20 69 73 20 73 70 65 63 69 66 69 65  ator is specifie
83d10 64 20 75 73 69 6e 67 20 63 6f 6d 70 69 6c 65 2d  d using compile-
83d20 74 69 6d 65 0a 2a 2a 20 6d 61 63 72 6f 73 2e 0a  time.** macros..
83d30 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
83d40 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 0a 0a  _SYSTEM_MALLOC..
83d50 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 6d 61 6c 6c 6f  /*.** Like mallo
83d60 63 28 29 2c 20 62 75 74 20 72 65 6d 65 6d 62 65  c(), but remembe
83d70 72 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  r the size of th
83d80 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20  e allocation.** 
83d90 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 66  so that we can f
83da0 69 6e 64 20 69 74 20 6c 61 74 65 72 20 75 73 69  ind it later usi
83db0 6e 67 20 73 71 6c 69 74 65 33 4d 65 6d 53 69 7a  ng sqlite3MemSiz
83dc0 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74  e()..**.** For t
83dd0 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f  his low-level ro
83de0 75 74 69 6e 65 2c 20 77 65 20 61 72 65 20 67 75  utine, we are gu
83df0 61 72 61 6e 74 65 65 64 20 74 68 61 74 20 6e 42  aranteed that nB
83e00 79 74 65 3e 30 20 62 65 63 61 75 73 65 0a 2a 2a  yte>0 because.**
83e10 20 63 61 73 65 73 20 6f 66 20 6e 42 79 74 65 3c   cases of nByte<
83e20 3d 30 20 77 69 6c 6c 20 62 65 20 69 6e 74 65 72  =0 will be inter
83e30 63 65 70 74 65 64 20 61 6e 64 20 64 65 61 6c 74  cepted and dealt
83e40 20 77 69 74 68 20 62 79 20 68 69 67 68 65 72 20   with by higher 
83e50 6c 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65  level.** routine
83e60 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
83e70 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c  d *sqlite3MemMal
83e80 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a  loc(int nByte){.
83e90 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
83ea0 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 42  *p;.  assert( nB
83eb0 79 74 65 3e 30 20 29 3b 0a 20 20 6e 42 79 74 65  yte>0 );.  nByte
83ec0 20 3d 20 52 4f 55 4e 44 38 28 6e 42 79 74 65 29   = ROUND8(nByte)
83ed0 3b 0a 20 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 20  ;.  p = malloc( 
83ee0 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66 28  nByte+8 );.  if(
83ef0 20 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d   p ){.    p[0] =
83f00 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b   nByte;.    p++;
83f10 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76  .  }.  return (v
83f20 6f 69 64 20 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  oid *)p;.}../*.*
83f30 2a 20 4c 69 6b 65 20 66 72 65 65 28 29 20 62 75  * Like free() bu
83f40 74 20 77 6f 72 6b 73 20 66 6f 72 20 61 6c 6c 6f  t works for allo
83f50 63 61 74 69 6f 6e 73 20 6f 62 74 61 69 6e 65 64  cations obtained
83f60 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 65 6d   from sqlite3Mem
83f70 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f 72 20 73  Malloc().** or s
83f80 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63  qlite3MemRealloc
83f90 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68  ()..**.** For th
83fa0 69 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75  is low-level rou
83fb0 74 69 6e 65 2c 20 77 65 20 61 6c 72 65 61 64 79  tine, we already
83fc0 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 72 69 6f   know that pPrio
83fd0 72 21 3d 30 20 73 69 6e 63 65 0a 2a 2a 20 63 61  r!=0 since.** ca
83fe0 73 65 73 20 77 68 65 72 65 20 70 50 72 69 6f 72  ses where pPrior
83ff0 3d 3d 30 20 77 69 6c 6c 20 68 61 76 65 20 62 65  ==0 will have be
84000 65 6e 20 69 6e 74 65 63 65 70 74 65 64 20 61 6e  en intecepted an
84010 64 20 64 65 61 6c 74 20 77 69 74 68 0a 2a 2a 20  d dealt with.** 
84020 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20  by higher-level 
84030 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61  routines..*/.sta
84040 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
84050 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50  MemFree(void *pP
84060 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69 74 65 33  rior){.  sqlite3
84070 5f 69 6e 74 36 34 20 2a 70 20 3d 20 28 73 71 6c  _int64 *p = (sql
84080 69 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 69  ite3_int64*)pPri
84090 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  or;.  assert( pP
840a0 72 69 6f 72 21 3d 30 20 29 3b 0a 20 20 70 2d 2d  rior!=0 );.  p--
840b0 3b 0a 20 20 66 72 65 65 28 70 29 3b 0a 7d 0a 0a  ;.  free(p);.}..
840c0 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 72 65 61 6c 6c  /*.** Like reall
840d0 6f 63 28 29 2e 20 20 52 65 73 69 7a 65 20 61 6e  oc().  Resize an
840e0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76   allocation prev
840f0 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20  iously obtained 
84100 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d  from.** sqlite3M
84110 65 6d 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a  emMalloc()..**.*
84120 2a 20 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c  * For this low-l
84130 65 76 65 6c 20 69 6e 74 65 72 66 61 63 65 2c 20  evel interface, 
84140 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 70 50 72  we know that pPr
84150 69 6f 72 21 3d 30 2e 20 20 43 61 73 65 73 20 77  ior!=0.  Cases w
84160 68 65 72 65 0a 2a 2a 20 70 50 72 69 6f 72 3d 3d  here.** pPrior==
84170 30 20 77 68 69 6c 65 20 68 61 76 65 20 62 65 65  0 while have bee
84180 6e 20 69 6e 74 65 72 63 65 70 74 65 64 20 62 79  n intercepted by
84190 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20 72 6f   higher-level ro
841a0 75 74 69 6e 65 20 61 6e 64 0a 2a 2a 20 72 65 64  utine and.** red
841b0 69 72 65 63 74 65 64 20 74 6f 20 78 4d 61 6c 6c  irected to xMall
841c0 6f 63 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c 20  oc.  Similarly, 
841d0 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 6e 42 79  we know that nBy
841e0 74 65 3e 30 20 62 65 63 61 75 73 65 73 0a 2a 2a  te>0 becauses.**
841f0 20 63 61 73 65 73 20 77 68 65 72 65 20 6e 42 79   cases where nBy
84200 74 65 3c 3d 30 20 77 69 6c 6c 20 68 61 76 65 20  te<=0 will have 
84210 62 65 65 6e 20 69 6e 74 65 72 63 65 70 74 65 64  been intercepted
84220 20 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65 6c   by higher-level
84230 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 61 6e 64  .** routines and
84240 20 72 65 64 69 72 65 63 74 65 64 20 74 6f 20 78   redirected to x
84250 46 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  Free..*/.static 
84260 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d  void *sqlite3Mem
84270 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50  Realloc(void *pP
84280 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29  rior, int nByte)
84290 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  {.  sqlite3_int6
842a0 34 20 2a 70 20 3d 20 28 73 71 6c 69 74 65 33 5f  4 *p = (sqlite3_
842b0 69 6e 74 36 34 2a 29 70 50 72 69 6f 72 3b 0a 20  int64*)pPrior;. 
842c0 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 21   assert( pPrior!
842d0 3d 30 20 26 26 20 6e 42 79 74 65 3e 30 20 29 3b  =0 && nByte>0 );
842e0 0a 20 20 6e 42 79 74 65 20 3d 20 52 4f 55 4e 44  .  nByte = ROUND
842f0 38 28 6e 42 79 74 65 29 3b 0a 20 20 70 20 3d 20  8(nByte);.  p = 
84300 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29  (sqlite3_int64*)
84310 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20  pPrior;.  p--;. 
84320 20 70 20 3d 20 72 65 61 6c 6c 6f 63 28 70 2c 20   p = realloc(p, 
84330 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20 69 66 28  nByte+8 );.  if(
84340 20 70 20 29 7b 0a 20 20 20 20 70 5b 30 5d 20 3d   p ){.    p[0] =
84350 20 6e 42 79 74 65 3b 0a 20 20 20 20 70 2b 2b 3b   nByte;.    p++;
84360 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76  .  }.  return (v
84370 6f 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  oid*)p;.}../*.**
84380 20 52 65 70 6f 72 74 20 74 68 65 20 61 6c 6c 6f   Report the allo
84390 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 20  cated size of a 
843a0 70 72 69 6f 72 20 72 65 74 75 72 6e 20 66 72 6f  prior return fro
843b0 6d 20 78 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20 6f  m xMalloc().** o
843c0 72 20 78 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2f  r xRealloc()..*/
843d0 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
843e0 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20  te3MemSize(void 
843f0 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 71 6c 69  *pPrior){.  sqli
84400 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a 20 20  te3_int64 *p;.  
84410 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 20  if( pPrior==0 ) 
84420 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20  return 0;.  p = 
84430 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29  (sqlite3_int64*)
84440 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d 3b 0a 20  pPrior;.  p--;. 
84450 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70 5b 30   return (int)p[0
84460 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e  ];.}../*.** Roun
84470 64 20 75 70 20 61 20 72 65 71 75 65 73 74 20 73  d up a request s
84480 69 7a 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ize to the next 
84490 76 61 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e  valid allocation
844a0 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   size..*/.static
844b0 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52   int sqlite3MemR
844c0 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20  oundup(int n){. 
844d0 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 38 28 6e   return ROUND8(n
844e0 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74  );.}../*.** Init
844f0 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75  ialize this modu
84500 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
84510 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74  t sqlite3MemInit
84520 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b  (void *NotUsed){
84530 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
84540 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
84550 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
84560 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69  ;.}../*.** Deini
84570 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64  tialize this mod
84580 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ule..*/.static v
84590 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68  oid sqlite3MemSh
845a0 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74  utdown(void *Not
845b0 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f  Used){.  UNUSED_
845c0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
845d0 64 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a  d);.  return;.}.
845e0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
845f0 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20  ine is the only 
84600 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20  routine in this 
84610 66 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e  file with extern
84620 61 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a  al linkage..**.*
84630 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c  * Populate the l
84640 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20  ow-level memory 
84650 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74  allocation funct
84660 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a  ion pointers in.
84670 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ** sqlite3Global
84680 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f  Config.m with po
84690 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f  inters to the ro
846a0 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66  utines in this f
846b0 69 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ile..*/.SQLITE_P
846c0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
846d0 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74  te3MemSetDefault
846e0 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63  (void){.  static
846f0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
84700 65 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75  em_methods defau
84710 6c 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20  ltMethods = {.  
84720 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c     sqlite3MemMal
84730 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65  loc,.     sqlite
84740 33 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73  3MemFree,.     s
84750 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63  qlite3MemRealloc
84760 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65  ,.     sqlite3Me
84770 6d 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69  mSize,.     sqli
84780 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20  te3MemRoundup,. 
84790 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e      sqlite3MemIn
847a0 69 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  it,.     sqlite3
847b0 4d 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20  MemShutdown,.   
847c0 20 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74    0.  };.  sqlit
847d0 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45  e3_config(SQLITE
847e0 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20  _CONFIG_MALLOC, 
847f0 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29  &defaultMethods)
84800 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53  ;.}..#endif /* S
84810 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c  QLITE_SYSTEM_MAL
84820 4c 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  LOC */../*******
84830 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d  ******* End of m
84840 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  em1.c **********
84850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84870 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
84880 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
84890 6c 65 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a  le mem2.c ******
848a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
848b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
848c0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
848d0 30 37 20 41 75 67 75 73 74 20 31 35 0a 2a 2a 0a  07 August 15.**.
848e0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
848f0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
84900 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
84910 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
84920 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
84930 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
84940 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
84950 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
84960 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
84970 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
84980 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
84990 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
849a0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
849b0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
849c0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
849d0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
849e0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
849f0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
84a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
84a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
84a40 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
84a50 61 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d  ains low-level m
84a60 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
84a70 20 64 72 69 76 65 72 73 20 66 6f 72 20 77 68 65   drivers for whe
84a80 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c  n.** SQLite will
84a90 20 75 73 65 20 74 68 65 20 73 74 61 6e 64 61 72   use the standar
84aa0 64 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c  d C-library mall
84ab0 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20  oc/realloc/free 
84ac0 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20  interface.** to 
84ad0 6f 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72  obtain the memor
84ae0 79 20 69 74 20 6e 65 65 64 73 20 77 68 69 6c 65  y it needs while
84af0 20 61 64 64 69 6e 67 20 6c 6f 74 73 20 6f 66 20   adding lots of 
84b00 61 64 64 69 74 69 6f 6e 61 6c 20 64 65 62 75 67  additional debug
84b10 67 69 6e 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74  ging.** informat
84b20 69 6f 6e 20 74 6f 20 65 61 63 68 20 61 6c 6c 6f  ion to each allo
84b30 63 61 74 69 6f 6e 20 69 6e 20 6f 72 64 65 72 20  cation in order 
84b40 74 6f 20 68 65 6c 70 20 64 65 74 65 63 74 20 61  to help detect a
84b50 6e 64 20 66 69 78 20 6d 65 6d 6f 72 79 0a 2a 2a  nd fix memory.**
84b60 20 6c 65 61 6b 73 20 61 6e 64 20 6d 65 6d 6f 72   leaks and memor
84b70 79 20 75 73 61 67 65 20 65 72 72 6f 72 73 2e 0a  y usage errors..
84b80 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
84b90 63 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65  contains impleme
84ba0 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20  ntations of the 
84bb0 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79  low-level memory
84bc0 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72   allocation.** r
84bd0 6f 75 74 69 6e 65 73 20 73 70 65 63 69 66 69 65  outines specifie
84be0 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33  d in the sqlite3
84bf0 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a  _mem_methods obj
84c00 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  ect..**.** $Id: 
84c10 6d 65 6d 32 2e 63 2c 76 20 31 2e 34 35 20 32 30  mem2.c,v 1.45 20
84c20 30 39 2f 30 33 2f 32 33 20 30 34 3a 33 33 3a 33  09/03/23 04:33:3
84c30 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  3 danielk1977 Ex
84c40 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  p $.*/../*.** Th
84c50 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68  is version of th
84c60 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
84c70 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20  or is used only 
84c80 69 66 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45  if the.** SQLITE
84c90 5f 4d 45 4d 44 45 42 55 47 20 6d 61 63 72 6f 20  _MEMDEBUG macro 
84ca0 69 73 20 64 65 66 69 6e 65 64 0a 2a 2f 0a 23 69  is defined.*/.#i
84cb0 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 45 4d 44  fdef SQLITE_MEMD
84cc0 45 42 55 47 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  EBUG../*.** The 
84cd0 62 61 63 6b 74 72 61 63 65 20 66 75 6e 63 74 69  backtrace functi
84ce0 6f 6e 61 6c 69 74 79 20 69 73 20 6f 6e 6c 79 20  onality is only 
84cf0 61 76 61 69 6c 61 62 6c 65 20 77 69 74 68 20 47  available with G
84d00 4c 49 42 43 0a 2a 2f 0a 23 69 66 64 65 66 20 5f  LIBC.*/.#ifdef _
84d10 5f 47 4c 49 42 43 5f 5f 0a 20 20 65 78 74 65 72  _GLIBC__.  exter
84d20 6e 20 69 6e 74 20 62 61 63 6b 74 72 61 63 65 28  n int backtrace(
84d30 76 6f 69 64 2a 2a 2c 69 6e 74 29 3b 0a 20 20 65  void**,int);.  e
84d40 78 74 65 72 6e 20 76 6f 69 64 20 62 61 63 6b 74  xtern void backt
84d50 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66 64 28  race_symbols_fd(
84d60 76 6f 69 64 2a 63 6f 6e 73 74 2a 2c 69 6e 74 2c  void*const*,int,
84d70 69 6e 74 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65  int);.#else.# de
84d80 66 69 6e 65 20 62 61 63 6b 74 72 61 63 65 28 41  fine backtrace(A
84d90 2c 42 29 20 31 0a 23 20 64 65 66 69 6e 65 20 62  ,B) 1.# define b
84da0 61 63 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73  acktrace_symbols
84db0 5f 66 64 28 41 2c 42 2c 43 29 0a 23 65 6e 64 69  _fd(A,B,C).#endi
84dc0 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 6d 65  f../*.** Each me
84dd0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
84de0 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a  looks like this:
84df0 0a 2a 2a 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d  .**.**  --------
84e00 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84e10 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84e20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84e30 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84e40 0a 2a 2a 20 20 7c 20 54 69 74 6c 65 20 7c 20 20  .**  | Title |  
84e50 62 61 63 6b 74 72 61 63 65 20 70 6f 69 6e 74 65  backtrace pointe
84e60 72 73 20 7c 20 20 4d 65 6d 42 6c 6f 63 6b 48 64  rs |  MemBlockHd
84e70 72 20 7c 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  r |  allocation 
84e80 7c 20 20 45 6e 64 47 75 61 72 64 20 7c 0a 2a 2a  |  EndGuard |.**
84e90 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
84ea0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84eb0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84ec0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
84ed0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a  ----------.**.**
84ee0 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   The application
84ef0 20 63 6f 64 65 20 73 65 65 73 20 6f 6e 6c 79 20   code sees only 
84f00 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
84f10 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 57 65   allocation.  We
84f20 20 68 61 76 65 0a 2a 2a 20 74 6f 20 62 61 63 6b   have.** to back
84f30 20 75 70 20 66 72 6f 6d 20 74 68 65 20 61 6c 6c   up from the all
84f40 6f 63 61 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20  ocation pointer 
84f50 74 6f 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42  to find the MemB
84f60 6c 6f 63 6b 48 64 72 2e 20 20 54 68 65 0a 2a 2a  lockHdr.  The.**
84f70 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 74 65 6c   MemBlockHdr tel
84f80 6c 73 20 75 73 20 74 68 65 20 73 69 7a 65 20 6f  ls us the size o
84f90 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  f the allocation
84fa0 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20   and the number 
84fb0 6f 66 0a 2a 2a 20 62 61 63 6b 74 72 61 63 65 20  of.** backtrace 
84fc0 70 6f 69 6e 74 65 72 73 2e 20 20 54 68 65 72 65  pointers.  There
84fd0 20 69 73 20 61 6c 73 6f 20 61 20 67 75 61 72 64   is also a guard
84fe0 20 77 6f 72 64 20 61 74 20 74 68 65 20 65 6e 64   word at the end
84ff0 20 6f 66 20 74 68 65 0a 2a 2a 20 4d 65 6d 42 6c   of the.** MemBl
85000 6f 63 6b 48 64 72 2e 0a 2a 2f 0a 73 74 72 75 63  ockHdr..*/.struc
85010 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 7b 0a  t MemBlockHdr {.
85020 20 20 69 36 34 20 69 53 69 7a 65 3b 20 20 20 20    i64 iSize;    
85030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
85040 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
85050 20 74 68 69 73 20 61 6c 6c 6f 63 61 74 69 6f 6e   this allocation
85060 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4d 65 6d   */.  struct Mem
85070 42 6c 6f 63 6b 48 64 72 20 2a 70 4e 65 78 74 2c  BlockHdr *pNext,
85080 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 4c 69 6e   *pPrev;  /* Lin
85090 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  ked list of all 
850a0 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 2a  unfreed memory *
850b0 2f 0a 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72  /.  char nBacktr
850c0 61 63 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ace;            
850d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
850e0 72 20 6f 66 20 62 61 63 6b 74 72 61 63 65 73 20  r of backtraces 
850f0 6f 6e 20 74 68 69 73 20 61 6c 6c 6f 63 20 2a 2f  on this alloc */
85100 0a 20 20 63 68 61 72 20 6e 42 61 63 6b 74 72 61  .  char nBacktra
85110 63 65 53 6c 6f 74 73 3b 20 20 20 20 20 20 20 20  ceSlots;        
85120 20 20 20 20 20 20 20 2f 2a 20 41 76 61 69 6c 61         /* Availa
85130 62 6c 65 20 62 61 63 6b 74 72 61 63 65 20 73 6c  ble backtrace sl
85140 6f 74 73 20 2a 2f 0a 20 20 73 68 6f 72 74 20 6e  ots */.  short n
85150 54 69 74 6c 65 3b 20 20 20 20 20 20 20 20 20 20  Title;          
85160 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
85170 42 79 74 65 73 20 6f 66 20 74 69 74 6c 65 3b 20  Bytes of title; 
85180 69 6e 63 6c 75 64 65 73 20 27 5c 30 27 20 2a 2f  includes '\0' */
85190 0a 20 20 69 6e 74 20 69 46 6f 72 65 47 75 61 72  .  int iForeGuar
851a0 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d;              
851b0 20 20 20 20 20 20 20 2f 2a 20 47 75 61 72 64 20         /* Guard 
851c0 77 6f 72 64 20 66 6f 72 20 73 61 6e 69 74 79 20  word for sanity 
851d0 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 75 61  */.};../*.** Gua
851e0 72 64 20 77 6f 72 64 73 0a 2a 2f 0a 23 64 65 66  rd words.*/.#def
851f0 69 6e 65 20 46 4f 52 45 47 55 41 52 44 20 30 78  ine FOREGUARD 0x
85200 38 30 46 35 45 31 35 33 0a 23 64 65 66 69 6e 65  80F5E153.#define
85210 20 52 45 41 52 47 55 41 52 44 20 30 78 45 34 36   REARGUARD 0xE46
85220 37 36 42 35 33 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d  76B53../*.** Num
85230 62 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 73 69  ber of malloc si
85240 7a 65 20 69 6e 63 72 65 6d 65 6e 74 73 20 74 6f  ze increments to
85250 20 74 72 61 63 6b 2e 0a 2a 2f 0a 23 64 65 66 69   track..*/.#defi
85260 6e 65 20 4e 43 53 49 5a 45 20 20 31 30 30 30 0a  ne NCSIZE  1000.
85270 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68  ./*.** All of th
85280 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c  e static variabl
85290 65 73 20 75 73 65 64 20 62 79 20 74 68 69 73 20  es used by this 
852a0 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f 6c 6c 65  module are colle
852b0 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20 61 20 73  cted.** into a s
852c0 69 6e 67 6c 65 20 73 74 72 75 63 74 75 72 65 20  ingle structure 
852d0 6e 61 6d 65 64 20 22 6d 65 6d 22 2e 20 20 54 68  named "mem".  Th
852e0 69 73 20 69 73 20 74 6f 20 6b 65 65 70 20 74 68  is is to keep th
852f0 65 0a 2a 2a 20 73 74 61 74 69 63 20 76 61 72 69  e.** static vari
85300 61 62 6c 65 73 20 6f 72 67 61 6e 69 7a 65 64 20  ables organized 
85310 61 6e 64 20 74 6f 20 72 65 64 75 63 65 20 6e 61  and to reduce na
85320 6d 65 73 70 61 63 65 20 70 6f 6c 6c 75 74 69 6f  mespace pollutio
85330 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 6d  n.** when this m
85340 6f 64 75 6c 65 20 69 73 20 63 6f 6d 62 69 6e 65  odule is combine
85350 64 20 77 69 74 68 20 6f 74 68 65 72 20 69 6e 20  d with other in 
85360 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e  the amalgamation
85370 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75  ..*/.static stru
85380 63 74 20 7b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a  ct {.  .  /*.  *
85390 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74 72  * Mutex to contr
853a0 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68 65  ol access to the
853b0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
853c0 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20  on subsystem..  
853d0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  */.  sqlite3_mut
853e0 65 78 20 2a 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a  ex *mutex;..  /*
853f0 0a 20 20 2a 2a 20 48 65 61 64 20 61 6e 64 20 74  .  ** Head and t
85400 61 69 6c 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20  ail of a linked 
85410 6c 69 73 74 20 6f 66 20 61 6c 6c 20 6f 75 74 73  list of all outs
85420 74 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69  tanding allocati
85430 6f 6e 73 0a 20 20 2a 2f 0a 20 20 73 74 72 75 63  ons.  */.  struc
85440 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70  t MemBlockHdr *p
85450 46 69 72 73 74 3b 0a 20 20 73 74 72 75 63 74 20  First;.  struct 
85460 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 4c 61  MemBlockHdr *pLa
85470 73 74 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a  st;.  .  /*.  **
85480 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c   The number of l
85490 65 76 65 6c 73 20 6f 66 20 62 61 63 6b 74 72 61  evels of backtra
854a0 63 65 20 74 6f 20 73 61 76 65 20 69 6e 20 6e 65  ce to save in ne
854b0 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20  w allocations.. 
854c0 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 61 63 6b 74   */.  int nBackt
854d0 72 61 63 65 3b 0a 20 20 76 6f 69 64 20 28 2a 78  race;.  void (*x
854e0 42 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20  Backtrace)(int, 
854f0 69 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 3b 0a 0a  int, void **);..
85500 20 20 2f 2a 0a 20 20 2a 2a 20 54 69 74 6c 65 20    /*.  ** Title 
85510 74 65 78 74 20 74 6f 20 69 6e 73 65 72 74 20 69  text to insert i
85520 6e 20 66 72 6f 6e 74 20 6f 66 20 65 61 63 68 20  n front of each 
85530 62 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 6e 74  block.  */.  int
85540 20 6e 54 69 74 6c 65 3b 20 20 20 20 20 20 20 20   nTitle;        
85550 2f 2a 20 42 79 74 65 73 20 6f 66 20 7a 54 69 74  /* Bytes of zTit
85560 6c 65 20 74 6f 20 73 61 76 65 2e 20 20 49 6e 63  le to save.  Inc
85570 6c 75 64 65 73 20 27 5c 30 27 20 61 6e 64 20 70  ludes '\0' and p
85580 61 64 64 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72  adding */.  char
85590 20 7a 54 69 74 6c 65 5b 31 30 30 5d 3b 20 20 2f   zTitle[100];  /
855a0 2a 20 54 68 65 20 74 69 74 6c 65 20 74 65 78 74  * The title text
855b0 20 2a 2f 0a 0a 20 20 2f 2a 20 0a 20 20 2a 2a 20   */..  /* .  ** 
855c0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 44 69 73  sqlite3MallocDis
855d0 61 6c 6c 6f 77 28 29 20 69 6e 63 72 65 6d 65 6e  allow() incremen
855e0 74 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ts the following
855f0 20 63 6f 75 6e 74 65 72 2e 0a 20 20 2a 2a 20 73   counter..  ** s
85600 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 6c 6f  qlite3MallocAllo
85610 77 28 29 20 64 65 63 72 65 6d 65 6e 74 73 20 69  w() decrements i
85620 74 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 64 69  t..  */.  int di
85630 73 61 6c 6c 6f 77 3b 20 2f 2a 20 44 6f 20 6e 6f  sallow; /* Do no
85640 74 20 61 6c 6c 6f 77 20 6d 65 6d 6f 72 79 20 61  t allow memory a
85650 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a 0a 20 20  llocation */..  
85660 2f 2a 0a 20 20 2a 2a 20 47 61 74 68 65 72 20 73  /*.  ** Gather s
85670 74 61 74 69 73 74 69 63 73 20 6f 6e 20 74 68 65  tatistics on the
85680 20 73 69 7a 65 73 20 6f 66 20 6d 65 6d 6f 72 79   sizes of memory
85690 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 20 20   allocations..  
856a0 2a 2a 20 6e 41 6c 6c 6f 63 5b 69 5d 20 69 73 20  ** nAlloc[i] is 
856b0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 6c  the number of al
856c0 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74  location attempt
856d0 73 20 6f 66 20 69 2a 38 0a 20 20 2a 2a 20 62 79  s of i*8.  ** by
856e0 74 65 73 2e 20 20 69 3d 3d 4e 43 53 49 5a 45 20  tes.  i==NCSIZE 
856f0 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
85700 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61 74 74 65   allocation atte
85710 6d 70 74 73 20 66 6f 72 0a 20 20 2a 2a 20 73 69  mpts for.  ** si
85720 7a 65 73 20 6d 6f 72 65 20 74 68 61 6e 20 4e 43  zes more than NC
85730 53 49 5a 45 2a 38 20 62 79 74 65 73 2e 0a 20 20  SIZE*8 bytes..  
85740 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 5b  */.  int nAlloc[
85750 4e 43 53 49 5a 45 5d 3b 20 20 20 20 20 20 2f 2a  NCSIZE];      /*
85760 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   Total number of
85770 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a   allocations */.
85780 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 5b 4e    int nCurrent[N
85790 43 53 49 5a 45 5d 3b 20 20 20 20 2f 2a 20 43 75  CSIZE];    /* Cu
857a0 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20  rrent number of 
857b0 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20  allocations */. 
857c0 20 69 6e 74 20 6d 78 43 75 72 72 65 6e 74 5b 4e   int mxCurrent[N
857d0 43 53 49 5a 45 5d 3b 20 20 20 2f 2a 20 48 69 67  CSIZE];   /* Hig
857e0 68 77 61 74 65 72 20 6d 61 72 6b 20 66 6f 72 20  hwater mark for 
857f0 6e 43 75 72 72 65 6e 74 20 2a 2f 0a 0a 7d 20 6d  nCurrent */..} m
85800 65 6d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a 75  em;.../*.** Adju
85810 73 74 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20  st memory usage 
85820 73 74 61 74 69 73 74 69 63 73 0a 2a 2f 0a 73 74  statistics.*/.st
85830 61 74 69 63 20 76 6f 69 64 20 61 64 6a 75 73 74  atic void adjust
85840 53 74 61 74 73 28 69 6e 74 20 69 53 69 7a 65 2c  Stats(int iSize,
85850 20 69 6e 74 20 69 6e 63 72 65 6d 65 6e 74 29 7b   int increment){
85860 0a 20 20 69 6e 74 20 69 20 3d 20 52 4f 55 4e 44  .  int i = ROUND
85870 38 28 69 53 69 7a 65 29 2f 38 3b 0a 20 20 69 66  8(iSize)/8;.  if
85880 28 20 69 3e 4e 43 53 49 5a 45 2d 31 20 29 7b 0a  ( i>NCSIZE-1 ){.
85890 20 20 20 20 69 20 3d 20 4e 43 53 49 5a 45 20 2d      i = NCSIZE -
858a0 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 6e   1;.  }.  if( in
858b0 63 72 65 6d 65 6e 74 3e 30 20 29 7b 0a 20 20 20  crement>0 ){.   
858c0 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2b 2b   mem.nAlloc[i]++
858d0 3b 0a 20 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65  ;.    mem.nCurre
858e0 6e 74 5b 69 5d 2b 2b 3b 0a 20 20 20 20 69 66 28  nt[i]++;.    if(
858f0 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d   mem.nCurrent[i]
85900 3e 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69  >mem.mxCurrent[i
85910 5d 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 2e 6d  ] ){.      mem.m
85920 78 43 75 72 72 65 6e 74 5b 69 5d 20 3d 20 6d 65  xCurrent[i] = me
85930 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 3b 0a 20  m.nCurrent[i];. 
85940 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
85950 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69    mem.nCurrent[i
85960 5d 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ]--;.    assert(
85970 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d   mem.nCurrent[i]
85980 3e 3d 30 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  >=0 );.  }.}../*
85990 0a 2a 2a 20 47 69 76 65 6e 20 61 6e 20 61 6c 6c  .** Given an all
859a0 6f 63 61 74 69 6f 6e 2c 20 66 69 6e 64 20 74 68  ocation, find th
859b0 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 66 6f  e MemBlockHdr fo
859c0 72 20 74 68 61 74 20 61 6c 6c 6f 63 61 74 69 6f  r that allocatio
859d0 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  n..**.** This ro
859e0 75 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68 65  utine checks the
859f0 20 67 75 61 72 64 73 20 61 74 20 65 69 74 68 65   guards at eithe
85a00 72 20 65 6e 64 20 6f 66 20 74 68 65 20 61 6c 6c  r end of the all
85a10 6f 63 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 69  ocation and.** i
85a20 66 20 74 68 65 79 20 61 72 65 20 69 6e 63 6f 72  f they are incor
85a30 72 65 63 74 20 69 74 20 61 73 73 65 72 74 73 2e  rect it asserts.
85a40 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63  .*/.static struc
85a50 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 73  t MemBlockHdr *s
85a60 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48  qlite3MemsysGetH
85a70 65 61 64 65 72 28 76 6f 69 64 20 2a 70 41 6c 6c  eader(void *pAll
85a80 6f 63 61 74 69 6f 6e 29 7b 0a 20 20 73 74 72 75  ocation){.  stru
85a90 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
85aa0 70 3b 0a 20 20 69 6e 74 20 2a 70 49 6e 74 3b 0a  p;.  int *pInt;.
85ab0 20 20 75 38 20 2a 70 55 38 3b 0a 20 20 69 6e 74    u8 *pU8;.  int
85ac0 20 6e 52 65 73 65 72 76 65 3b 0a 0a 20 20 70 20   nReserve;..  p 
85ad0 3d 20 28 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  = (struct MemBlo
85ae0 63 6b 48 64 72 2a 29 70 41 6c 6c 6f 63 61 74 69  ckHdr*)pAllocati
85af0 6f 6e 3b 0a 20 20 70 2d 2d 3b 0a 20 20 61 73 73  on;.  p--;.  ass
85b00 65 72 74 28 20 70 2d 3e 69 46 6f 72 65 47 75 61  ert( p->iForeGua
85b10 72 64 3d 3d 28 69 6e 74 29 46 4f 52 45 47 55 41  rd==(int)FOREGUA
85b20 52 44 20 29 3b 0a 20 20 6e 52 65 73 65 72 76 65  RD );.  nReserve
85b30 20 3d 20 52 4f 55 4e 44 38 28 70 2d 3e 69 53 69   = ROUND8(p->iSi
85b40 7a 65 29 3b 0a 20 20 70 49 6e 74 20 3d 20 28 69  ze);.  pInt = (i
85b50 6e 74 2a 29 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b  nt*)pAllocation;
85b60 0a 20 20 70 55 38 20 3d 20 28 75 38 2a 29 70 41  .  pU8 = (u8*)pA
85b70 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 61 73 73  llocation;.  ass
85b80 65 72 74 28 20 70 49 6e 74 5b 6e 52 65 73 65 72  ert( pInt[nReser
85b90 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 3d  ve/sizeof(int)]=
85ba0 3d 28 69 6e 74 29 52 45 41 52 47 55 41 52 44 20  =(int)REARGUARD 
85bb0 29 3b 0a 20 20 2f 2a 20 54 68 69 73 20 63 68 65  );.  /* This che
85bc0 63 6b 73 20 61 6e 79 20 6f 66 20 74 68 65 20 22  cks any of the "
85bd0 65 78 74 72 61 22 20 62 79 74 65 73 20 61 6c 6c  extra" bytes all
85be0 6f 63 61 74 65 64 20 64 75 65 0a 20 20 2a 2a 20  ocated due.  ** 
85bf0 74 6f 20 72 6f 75 6e 64 69 6e 67 20 75 70 20 74  to rounding up t
85c00 6f 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e  o an 8 byte boun
85c10 64 61 72 79 20 74 6f 20 65 6e 73 75 72 65 20 0a  dary to ensure .
85c20 20 20 2a 2a 20 74 68 65 79 20 68 61 76 65 6e 27    ** they haven'
85c30 74 20 62 65 65 6e 20 6f 76 65 72 77 72 69 74 74  t been overwritt
85c40 65 6e 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65  en..  */.  while
85c50 28 20 6e 52 65 73 65 72 76 65 2d 2d 20 3e 20 70  ( nReserve-- > p
85c60 2d 3e 69 53 69 7a 65 20 29 20 61 73 73 65 72 74  ->iSize ) assert
85c70 28 20 70 55 38 5b 6e 52 65 73 65 72 76 65 5d 3d  ( pU8[nReserve]=
85c80 3d 30 78 36 35 20 29 3b 0a 20 20 72 65 74 75 72  =0x65 );.  retur
85c90 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  n p;.}../*.** Re
85ca0 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
85cb0 6f 66 20 62 79 74 65 73 20 63 75 72 72 65 6e 74  of bytes current
85cc0 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20  ly allocated at 
85cd0 61 64 64 72 65 73 73 20 70 2e 0a 2a 2f 0a 73 74  address p..*/.st
85ce0 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
85cf0 4d 65 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 29  MemSize(void *p)
85d00 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c  {.  struct MemBl
85d10 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20  ockHdr *pHdr;.  
85d20 69 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65  if( !p ){.    re
85d30 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 48  turn 0;.  }.  pH
85d40 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73  dr = sqlite3Mems
85d50 79 73 47 65 74 48 65 61 64 65 72 28 70 29 3b 0a  ysGetHeader(p);.
85d60 20 20 72 65 74 75 72 6e 20 70 48 64 72 2d 3e 69    return pHdr->i
85d70 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  Size;.}../*.** I
85d80 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65  nitialize the me
85d90 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
85da0 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74  subsystem..*/.st
85db0 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
85dc0 4d 65 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f  MemInit(void *No
85dd0 74 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44  tUsed){.  UNUSED
85de0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
85df0 65 64 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  ed);.  assert( (
85e00 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 4d 65  sizeof(struct Me
85e10 6d 42 6c 6f 63 6b 48 64 72 29 26 37 29 20 3d 3d  mBlockHdr)&7) ==
85e20 20 30 20 29 3b 0a 20 20 69 66 28 20 21 73 71 6c   0 );.  if( !sql
85e30 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
85e40 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20  .bMemstat ){.   
85e50 20 2f 2a 20 49 66 20 6d 65 6d 6f 72 79 20 73 74   /* If memory st
85e60 61 74 75 73 20 69 73 20 65 6e 61 62 6c 65 64 2c  atus is enabled,
85e70 20 74 68 65 6e 20 74 68 65 20 6d 61 6c 6c 6f 63   then the malloc
85e80 2e 63 20 77 72 61 70 70 65 72 20 77 69 6c 6c 20  .c wrapper will 
85e90 61 6c 72 65 61 64 79 0a 20 20 20 20 2a 2a 20 68  already.    ** h
85ea0 6f 6c 64 20 74 68 65 20 53 54 41 54 49 43 5f 4d  old the STATIC_M
85eb0 45 4d 20 6d 75 74 65 78 20 77 68 65 6e 20 74 68  EM mutex when th
85ec0 65 20 72 6f 75 74 69 6e 65 73 20 68 65 72 65 20  e routines here 
85ed0 61 72 65 20 69 6e 76 6f 6b 65 64 2e 20 2a 2f 0a  are invoked. */.
85ee0 20 20 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20      mem.mutex = 
85ef0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
85f00 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
85f10 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a  TATIC_MEM);.  }.
85f20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
85f30 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69  OK;.}../*.** Dei
85f40 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65  nitialize the me
85f50 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
85f60 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74  subsystem..*/.st
85f70 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65  atic void sqlite
85f80 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69  3MemShutdown(voi
85f90 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55  d *NotUsed){.  U
85fa0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
85fb0 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 2e  NotUsed);.  mem.
85fc0 6d 75 74 65 78 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  mutex = 0;.}../*
85fd0 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72  .** Round up a r
85fe0 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74  equest size to t
85ff0 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c  he next valid al
86000 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a  location size..*
86010 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
86020 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75 70 28 69  ite3MemRoundup(i
86030 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 72 6e 20  nt n){.  return 
86040 52 4f 55 4e 44 38 28 6e 29 3b 0a 7d 0a 0a 2f 2a  ROUND8(n);.}../*
86050 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79  .** Allocate nBy
86060 74 65 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f  te bytes of memo
86070 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ry..*/.static vo
86080 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 4d 61  id *sqlite3MemMa
86090 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b  lloc(int nByte){
860a0 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  .  struct MemBlo
860b0 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 76  ckHdr *pHdr;.  v
860c0 6f 69 64 20 2a 2a 70 42 74 3b 0a 20 20 63 68 61  oid **pBt;.  cha
860d0 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 2a 70 49 6e  r *z;.  int *pIn
860e0 74 3b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 30  t;.  void *p = 0
860f0 3b 0a 20 20 69 6e 74 20 74 6f 74 61 6c 53 69 7a  ;.  int totalSiz
86100 65 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 76  e;.  int nReserv
86110 65 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  e;.  sqlite3_mut
86120 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d 75 74  ex_enter(mem.mut
86130 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d  ex);.  assert( m
86140 65 6d 2e 64 69 73 61 6c 6c 6f 77 3d 3d 30 20 29  em.disallow==0 )
86150 3b 0a 20 20 6e 52 65 73 65 72 76 65 20 3d 20 52  ;.  nReserve = R
86160 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 20  OUND8(nByte);.  
86170 74 6f 74 61 6c 53 69 7a 65 20 3d 20 6e 52 65 73  totalSize = nRes
86180 65 72 76 65 20 2b 20 73 69 7a 65 6f 66 28 2a 70  erve + sizeof(*p
86190 48 64 72 29 20 2b 20 73 69 7a 65 6f 66 28 69 6e  Hdr) + sizeof(in
861a0 74 29 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20  t) +.           
861b0 20 20 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61      mem.nBacktra
861c0 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29  ce*sizeof(void*)
861d0 20 2b 20 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20   + mem.nTitle;. 
861e0 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 74 6f 74 61   p = malloc(tota
861f0 6c 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 70 20  lSize);.  if( p 
86200 29 7b 0a 20 20 20 20 7a 20 3d 20 70 3b 0a 20 20  ){.    z = p;.  
86210 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29    pBt = (void**)
86220 26 7a 5b 6d 65 6d 2e 6e 54 69 74 6c 65 5d 3b 0a  &z[mem.nTitle];.
86230 20 20 20 20 70 48 64 72 20 3d 20 28 73 74 72 75      pHdr = (stru
86240 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29  ct MemBlockHdr*)
86250 26 70 42 74 5b 6d 65 6d 2e 6e 42 61 63 6b 74 72  &pBt[mem.nBacktr
86260 61 63 65 5d 3b 0a 20 20 20 20 70 48 64 72 2d 3e  ace];.    pHdr->
86270 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70  pNext = 0;.    p
86280 48 64 72 2d 3e 70 50 72 65 76 20 3d 20 6d 65 6d  Hdr->pPrev = mem
86290 2e 70 4c 61 73 74 3b 0a 20 20 20 20 69 66 28 20  .pLast;.    if( 
862a0 6d 65 6d 2e 70 4c 61 73 74 20 29 7b 0a 20 20 20  mem.pLast ){.   
862b0 20 20 20 6d 65 6d 2e 70 4c 61 73 74 2d 3e 70 4e     mem.pLast->pN
862c0 65 78 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20  ext = pHdr;.    
862d0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d  }else{.      mem
862e0 2e 70 46 69 72 73 74 20 3d 20 70 48 64 72 3b 0a  .pFirst = pHdr;.
862f0 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 2e 70 4c      }.    mem.pL
86300 61 73 74 20 3d 20 70 48 64 72 3b 0a 20 20 20 20  ast = pHdr;.    
86310 70 48 64 72 2d 3e 69 46 6f 72 65 47 75 61 72 64  pHdr->iForeGuard
86320 20 3d 20 46 4f 52 45 47 55 41 52 44 3b 0a 20 20   = FOREGUARD;.  
86330 20 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61    pHdr->nBacktra
86340 63 65 53 6c 6f 74 73 20 3d 20 6d 65 6d 2e 6e 42  ceSlots = mem.nB
86350 61 63 6b 74 72 61 63 65 3b 0a 20 20 20 20 70 48  acktrace;.    pH
86360 64 72 2d 3e 6e 54 69 74 6c 65 20 3d 20 6d 65 6d  dr->nTitle = mem
86370 2e 6e 54 69 74 6c 65 3b 0a 20 20 20 20 69 66 28  .nTitle;.    if(
86380 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61 63 65 20   mem.nBacktrace 
86390 29 7b 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 61  ){.      void *a
863a0 41 64 64 72 5b 34 30 5d 3b 0a 20 20 20 20 20 20  Addr[40];.      
863b0 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
863c0 20 3d 20 62 61 63 6b 74 72 61 63 65 28 61 41 64   = backtrace(aAd
863d0 64 72 2c 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61  dr, mem.nBacktra
863e0 63 65 2b 31 29 2d 31 3b 0a 20 20 20 20 20 20 6d  ce+1)-1;.      m
863f0 65 6d 63 70 79 28 70 42 74 2c 20 26 61 41 64 64  emcpy(pBt, &aAdd
86400 72 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e 42 61 63  r[1], pHdr->nBac
86410 6b 74 72 61 63 65 2a 73 69 7a 65 6f 66 28 76 6f  ktrace*sizeof(vo
86420 69 64 2a 29 29 3b 0a 20 20 20 20 20 20 61 73 73  id*));.      ass
86430 65 72 74 28 70 42 74 5b 30 5d 29 3b 0a 20 20 20  ert(pBt[0]);.   
86440 20 20 20 69 66 28 20 6d 65 6d 2e 78 42 61 63 6b     if( mem.xBack
86450 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20  trace ){.       
86460 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 28   mem.xBacktrace(
86470 6e 42 79 74 65 2c 20 70 48 64 72 2d 3e 6e 42 61  nByte, pHdr->nBa
86480 63 6b 74 72 61 63 65 2d 31 2c 20 26 61 41 64 64  cktrace-1, &aAdd
86490 72 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20  r[1]);.      }. 
864a0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
864b0 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
864c0 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
864d0 69 66 28 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29  if( mem.nTitle )
864e0 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a  {.      memcpy(z
864f0 2c 20 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d 65  , mem.zTitle, me
86500 6d 2e 6e 54 69 74 6c 65 29 3b 0a 20 20 20 20 7d  m.nTitle);.    }
86510 0a 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65  .    pHdr->iSize
86520 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 64   = nByte;.    ad
86530 6a 75 73 74 53 74 61 74 73 28 6e 42 79 74 65 2c  justStats(nByte,
86540 20 2b 31 29 3b 0a 20 20 20 20 70 49 6e 74 20 3d   +1);.    pInt =
86550 20 28 69 6e 74 2a 29 26 70 48 64 72 5b 31 5d 3b   (int*)&pHdr[1];
86560 0a 20 20 20 20 70 49 6e 74 5b 6e 52 65 73 65 72  .    pInt[nReser
86570 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29 5d 20  ve/sizeof(int)] 
86580 3d 20 52 45 41 52 47 55 41 52 44 3b 0a 20 20 20  = REARGUARD;.   
86590 20 6d 65 6d 73 65 74 28 70 49 6e 74 2c 20 30 78   memset(pInt, 0x
865a0 36 35 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a 20  65, nReserve);. 
865b0 20 20 20 70 20 3d 20 28 76 6f 69 64 2a 29 70 49     p = (void*)pI
865c0 6e 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  nt;.  }.  sqlite
865d0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
865e0 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  m.mutex);.  retu
865f0 72 6e 20 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn p; .}../*.** 
86600 46 72 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a  Free memory..*/.
86610 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
86620 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64 20  te3MemFree(void 
86630 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 74 72 75  *pPrior){.  stru
86640 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a  ct MemBlockHdr *
86650 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70  pHdr;.  void **p
86660 42 74 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20  Bt;.  char *z;. 
86670 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
86680 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65  GlobalConfig.bMe
86690 6d 73 74 61 74 20 7c 7c 20 6d 65 6d 2e 6d 75 74  mstat || mem.mut
866a0 65 78 21 3d 30 20 29 3b 0a 20 20 70 48 64 72 20  ex!=0 );.  pHdr 
866b0 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73 47  = sqlite3MemsysG
866c0 65 74 48 65 61 64 65 72 28 70 50 72 69 6f 72 29  etHeader(pPrior)
866d0 3b 0a 20 20 70 42 74 20 3d 20 28 76 6f 69 64 2a  ;.  pBt = (void*
866e0 2a 29 70 48 64 72 3b 0a 20 20 70 42 74 20 2d 3d  *)pHdr;.  pBt -=
866f0 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63   pHdr->nBacktrac
86700 65 53 6c 6f 74 73 3b 0a 20 20 73 71 6c 69 74 65  eSlots;.  sqlite
86710 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65  3_mutex_enter(me
86720 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20  m.mutex);.  if( 
86730 70 48 64 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20  pHdr->pPrev ){. 
86740 20 20 20 61 73 73 65 72 74 28 20 70 48 64 72 2d     assert( pHdr-
86750 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70  >pPrev->pNext==p
86760 48 64 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d  Hdr );.    pHdr-
86770 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20  >pPrev->pNext = 
86780 70 48 64 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d  pHdr->pNext;.  }
86790 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
867a0 28 20 6d 65 6d 2e 70 46 69 72 73 74 3d 3d 70 48  ( mem.pFirst==pH
867b0 64 72 20 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 46  dr );.    mem.pF
867c0 69 72 73 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65  irst = pHdr->pNe
867d0 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 48  xt;.  }.  if( pH
867e0 64 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20  dr->pNext ){.   
867f0 20 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e 70   assert( pHdr->p
86800 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70 48 64  Next->pPrev==pHd
86810 72 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70  r );.    pHdr->p
86820 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 48  Next->pPrev = pH
86830 64 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d 65 6c  dr->pPrev;.  }el
86840 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
86850 6d 65 6d 2e 70 4c 61 73 74 3d 3d 70 48 64 72 20  mem.pLast==pHdr 
86860 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74  );.    mem.pLast
86870 20 3d 20 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a   = pHdr->pPrev;.
86880 20 20 7d 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a    }.  z = (char*
86890 29 70 42 74 3b 0a 20 20 7a 20 2d 3d 20 70 48 64  )pBt;.  z -= pHd
868a0 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 61 64 6a  r->nTitle;.  adj
868b0 75 73 74 53 74 61 74 73 28 70 48 64 72 2d 3e 69  ustStats(pHdr->i
868c0 53 69 7a 65 2c 20 2d 31 29 3b 0a 20 20 6d 65 6d  Size, -1);.  mem
868d0 73 65 74 28 7a 2c 20 30 78 32 62 2c 20 73 69 7a  set(z, 0x2b, siz
868e0 65 6f 66 28 76 6f 69 64 2a 29 2a 70 48 64 72 2d  eof(void*)*pHdr-
868f0 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73  >nBacktraceSlots
86900 20 2b 20 73 69 7a 65 6f 66 28 2a 70 48 64 72 29   + sizeof(*pHdr)
86910 20 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   +.             
86920 20 20 20 20 20 70 48 64 72 2d 3e 69 53 69 7a 65       pHdr->iSize
86930 20 2b 20 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b   + sizeof(int) +
86940 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 29 3b 0a   pHdr->nTitle);.
86950 20 20 66 72 65 65 28 7a 29 3b 0a 20 20 73 71 6c    free(z);.  sql
86960 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
86970 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 20 20 0a 7d  (mem.mutex);  .}
86980 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
86990 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78  he size of an ex
869a0 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c  isting memory al
869b0 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  location..**.** 
869c0 46 6f 72 20 74 68 69 73 20 64 65 62 75 67 67 69  For this debuggi
869d0 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ng implementatio
869e0 6e 2c 20 77 65 20 2a 61 6c 77 61 79 73 2a 20 6d  n, we *always* m
869f0 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ake a copy of th
86a00 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  e.** allocation 
86a10 69 6e 74 6f 20 61 20 6e 65 77 20 70 6c 61 63 65  into a new place
86a20 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 49 6e 20   in memory.  In 
86a30 74 68 69 73 20 77 61 79 2c 20 69 66 20 74 68 65  this way, if the
86a40 20 0a 2a 2a 20 68 69 67 68 65 72 20 6c 65 76 65   .** higher leve
86a50 6c 20 63 6f 64 65 20 69 73 20 75 73 69 6e 67 20  l code is using 
86a60 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f  pointer to the o
86a70 6c 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69  ld allocation, i
86a80 74 20 69 73 20 0a 2a 2a 20 6d 75 63 68 20 6d 6f  t is .** much mo
86a90 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 62 72 65  re likely to bre
86aa0 61 6b 20 61 6e 64 20 77 65 20 61 72 65 20 6d 75  ak and we are mu
86ab0 63 68 20 6d 6f 72 65 20 6c 69 6b 69 6e 67 20 74  ch more liking t
86ac0 6f 20 66 69 6e 64 0a 2a 2a 20 74 68 65 20 65 72  o find.** the er
86ad0 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ror..*/.static v
86ae0 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52  oid *sqlite3MemR
86af0 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72  ealloc(void *pPr
86b00 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b  ior, int nByte){
86b10 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  .  struct MemBlo
86b20 63 6b 48 64 72 20 2a 70 4f 6c 64 48 64 72 3b 0a  ckHdr *pOldHdr;.
86b30 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20    void *pNew;.  
86b40 61 73 73 65 72 74 28 20 6d 65 6d 2e 64 69 73 61  assert( mem.disa
86b50 6c 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20 70 4f 6c  llow==0 );.  pOl
86b60 64 48 64 72 20 3d 20 73 71 6c 69 74 65 33 4d 65  dHdr = sqlite3Me
86b70 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 70 50  msysGetHeader(pP
86b80 72 69 6f 72 29 3b 0a 20 20 70 4e 65 77 20 3d 20  rior);.  pNew = 
86b90 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63  sqlite3MemMalloc
86ba0 28 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 70  (nByte);.  if( p
86bb0 4e 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70  New ){.    memcp
86bc0 79 28 70 4e 65 77 2c 20 70 50 72 69 6f 72 2c 20  y(pNew, pPrior, 
86bd0 6e 42 79 74 65 3c 70 4f 6c 64 48 64 72 2d 3e 69  nByte<pOldHdr->i
86be0 53 69 7a 65 20 3f 20 6e 42 79 74 65 20 3a 20 70  Size ? nByte : p
86bf0 4f 6c 64 48 64 72 2d 3e 69 53 69 7a 65 29 3b 0a  OldHdr->iSize);.
86c00 20 20 20 20 69 66 28 20 6e 42 79 74 65 3e 70 4f      if( nByte>pO
86c10 6c 64 48 64 72 2d 3e 69 53 69 7a 65 20 29 7b 0a  ldHdr->iSize ){.
86c20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28        memset(&((
86c30 63 68 61 72 2a 29 70 4e 65 77 29 5b 70 4f 6c 64  char*)pNew)[pOld
86c40 48 64 72 2d 3e 69 53 69 7a 65 5d 2c 20 30 78 32  Hdr->iSize], 0x2
86c50 62 2c 20 6e 42 79 74 65 20 2d 20 70 4f 6c 64 48  b, nByte - pOldH
86c60 64 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20  dr->iSize);.    
86c70 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  }.    sqlite3Mem
86c80 46 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20  Free(pPrior);.  
86c90 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b  }.  return pNew;
86ca0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6f 70 75 6c 61  .}../*.** Popula
86cb0 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c  te the low-level
86cc0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
86cd0 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e  on function poin
86ce0 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74  ters in.** sqlit
86cf0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
86d00 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20 74   with pointers t
86d10 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 69  o the routines i
86d20 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a  n this file..*/.
86d30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
86d40 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 65  oid sqlite3MemSe
86d50 74 44 65 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a  tDefault(void){.
86d60 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
86d70 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
86d80 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68 6f 64  ds defaultMethod
86d90 73 20 3d 20 7b 0a 20 20 20 20 20 73 71 6c 69 74  s = {.     sqlit
86da0 65 33 4d 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20  e3MemMalloc,.   
86db0 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65 65    sqlite3MemFree
86dc0 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65  ,.     sqlite3Me
86dd0 6d 52 65 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73  mRealloc,.     s
86de0 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20  qlite3MemSize,. 
86df0 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f      sqlite3MemRo
86e00 75 6e 64 75 70 2c 0a 20 20 20 20 20 73 71 6c 69  undup,.     sqli
86e10 74 65 33 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20  te3MemInit,.    
86e20 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74 64   sqlite3MemShutd
86e30 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b  own,.     0.  };
86e40 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  .  sqlite3_confi
86e50 67 28 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  g(SQLITE_CONFIG_
86e60 4d 41 4c 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74  MALLOC, &default
86e70 4d 65 74 68 6f 64 73 29 3b 0a 7d 0a 0a 2f 2a 0a  Methods);.}../*.
86e80 2a 2a 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65  ** Set the numbe
86e90 72 20 6f 66 20 62 61 63 6b 74 72 61 63 65 20 6c  r of backtrace l
86ea0 65 76 65 6c 73 20 6b 65 70 74 20 66 6f 72 20 65  evels kept for e
86eb0 61 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a  ach allocation..
86ec0 2a 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 7a 65  ** A value of ze
86ed0 72 6f 20 74 75 72 6e 73 20 6f 66 66 20 62 61 63  ro turns off bac
86ee0 6b 74 72 61 63 69 6e 67 2e 20 20 54 68 65 20 6e  ktracing.  The n
86ef0 75 6d 62 65 72 20 69 73 20 61 6c 77 61 79 73 20  umber is always 
86f00 72 6f 75 6e 64 65 64 0a 2a 2a 20 75 70 20 74 6f  rounded.** up to
86f10 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32   a multiple of 2
86f20 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
86f30 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
86f40 4d 65 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63  MemdebugBacktrac
86f50 65 28 69 6e 74 20 64 65 70 74 68 29 7b 0a 20 20  e(int depth){.  
86f60 69 66 28 20 64 65 70 74 68 3c 30 20 29 7b 20 64  if( depth<0 ){ d
86f70 65 70 74 68 20 3d 20 30 3b 20 7d 0a 20 20 69 66  epth = 0; }.  if
86f80 28 20 64 65 70 74 68 3e 32 30 20 29 7b 20 64 65  ( depth>20 ){ de
86f90 70 74 68 20 3d 20 32 30 3b 20 7d 0a 20 20 64 65  pth = 20; }.  de
86fa0 70 74 68 20 3d 20 28 64 65 70 74 68 2b 31 29 26  pth = (depth+1)&
86fb0 30 78 66 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61 63  0xfe;.  mem.nBac
86fc0 6b 74 72 61 63 65 20 3d 20 64 65 70 74 68 3b 0a  ktrace = depth;.
86fd0 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  }..SQLITE_PRIVAT
86fe0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65  E void sqlite3Me
86ff0 6d 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 43  mdebugBacktraceC
87000 61 6c 6c 62 61 63 6b 28 76 6f 69 64 20 28 2a 78  allback(void (*x
87010 42 61 63 6b 74 72 61 63 65 29 28 69 6e 74 2c 20  Backtrace)(int, 
87020 69 6e 74 2c 20 76 6f 69 64 20 2a 2a 29 29 7b 0a  int, void **)){.
87030 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65    mem.xBacktrace
87040 20 3d 20 78 42 61 63 6b 74 72 61 63 65 3b 0a 7d   = xBacktrace;.}
87050 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
87060 74 69 74 6c 65 20 73 74 72 69 6e 67 20 66 6f 72  title string for
87070 20 73 75 62 73 65 71 75 65 6e 74 20 61 6c 6c 6f   subsequent allo
87080 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49  cations..*/.SQLI
87090 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
870a0 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53  sqlite3MemdebugS
870b0 65 74 74 69 74 6c 65 28 63 6f 6e 73 74 20 63 68  ettitle(const ch
870c0 61 72 20 2a 7a 54 69 74 6c 65 29 7b 0a 20 20 75  ar *zTitle){.  u
870d0 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 20 3d 20  nsigned int n = 
870e0 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
870f0 7a 54 69 74 6c 65 29 20 2b 20 31 3b 0a 20 20 73  zTitle) + 1;.  s
87100 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
87110 65 72 28 6d 65 6d 2e 6d 75 74 65 78 29 3b 0a 20  er(mem.mutex);. 
87120 20 69 66 28 20 6e 3e 3d 73 69 7a 65 6f 66 28 6d   if( n>=sizeof(m
87130 65 6d 2e 7a 54 69 74 6c 65 29 20 29 20 6e 20 3d  em.zTitle) ) n =
87140 20 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54 69 74   sizeof(mem.zTit
87150 6c 65 29 2d 31 3b 0a 20 20 6d 65 6d 63 70 79 28  le)-1;.  memcpy(
87160 6d 65 6d 2e 7a 54 69 74 6c 65 2c 20 7a 54 69 74  mem.zTitle, zTit
87170 6c 65 2c 20 6e 29 3b 0a 20 20 6d 65 6d 2e 7a 54  le, n);.  mem.zT
87180 69 74 6c 65 5b 6e 5d 20 3d 20 30 3b 0a 20 20 6d  itle[n] = 0;.  m
87190 65 6d 2e 6e 54 69 74 6c 65 20 3d 20 52 4f 55 4e  em.nTitle = ROUN
871a0 44 38 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33  D8(n);.  sqlite3
871b0 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
871c0 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 53 51 4c 49  .mutex);.}..SQLI
871d0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
871e0 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 53  sqlite3MemdebugS
871f0 79 6e 63 28 29 7b 0a 20 20 73 74 72 75 63 74 20  ync(){.  struct 
87200 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64  MemBlockHdr *pHd
87210 72 3b 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d 65  r;.  for(pHdr=me
87220 6d 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b 20  m.pFirst; pHdr; 
87230 70 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78 74  pHdr=pHdr->pNext
87240 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 2a 70 42  ){.    void **pB
87250 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64 72  t = (void**)pHdr
87260 3b 0a 20 20 20 20 70 42 74 20 2d 3d 20 70 48 64  ;.    pBt -= pHd
87270 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f  r->nBacktraceSlo
87280 74 73 3b 0a 20 20 20 20 6d 65 6d 2e 78 42 61 63  ts;.    mem.xBac
87290 6b 74 72 61 63 65 28 70 48 64 72 2d 3e 69 53 69  ktrace(pHdr->iSi
872a0 7a 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74  ze, pHdr->nBackt
872b0 72 61 63 65 2d 31 2c 20 26 70 42 74 5b 31 5d 29  race-1, &pBt[1])
872c0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f  ;.  }.}../*.** O
872d0 70 65 6e 20 74 68 65 20 66 69 6c 65 20 69 6e 64  pen the file ind
872e0 69 63 61 74 65 64 20 61 6e 64 20 77 72 69 74 65  icated and write
872f0 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20 75 6e   a log of all un
87300 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a 2a 2a  freed memory .**
87310 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69 6e 74   allocations int
87320 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f 0a 53  o that log..*/.S
87330 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
87340 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65 62  id sqlite3Memdeb
87350 75 67 44 75 6d 70 28 63 6f 6e 73 74 20 63 68 61  ugDump(const cha
87360 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20  r *zFilename){. 
87370 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 20 73 74   FILE *out;.  st
87380 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72  ruct MemBlockHdr
87390 20 2a 70 48 64 72 3b 0a 20 20 76 6f 69 64 20 2a   *pHdr;.  void *
873a0 2a 70 42 74 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  *pBt;.  int i;. 
873b0 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69   out = fopen(zFi
873c0 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20  lename, "w");.  
873d0 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20  if( out==0 ){.  
873e0 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72    fprintf(stderr
873f0 2c 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20  , "** Unable to 
87400 6f 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65  output memory de
87410 62 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20  bug output log: 
87420 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20  %s **\n",.      
87430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 46                zF
87440 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 72 65  ilename);.    re
87450 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  turn;.  }.  for(
87460 70 48 64 72 3d 6d 65 6d 2e 70 46 69 72 73 74 3b  pHdr=mem.pFirst;
87470 20 70 48 64 72 3b 20 70 48 64 72 3d 70 48 64 72   pHdr; pHdr=pHdr
87480 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 63 68  ->pNext){.    ch
87490 61 72 20 2a 7a 20 3d 20 28 63 68 61 72 2a 29 70  ar *z = (char*)p
874a0 48 64 72 3b 0a 20 20 20 20 7a 20 2d 3d 20 70 48  Hdr;.    z -= pH
874b0 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c  dr->nBacktraceSl
874c0 6f 74 73 2a 73 69 7a 65 6f 66 28 76 6f 69 64 2a  ots*sizeof(void*
874d0 29 20 2b 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65  ) + pHdr->nTitle
874e0 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  ;.    fprintf(ou
874f0 74 2c 20 22 2a 2a 2a 2a 20 25 6c 6c 64 20 62 79  t, "**** %lld by
87500 74 65 73 20 61 74 20 25 70 20 66 72 6f 6d 20 25  tes at %p from %
87510 73 20 2a 2a 2a 2a 5c 6e 22 2c 20 0a 20 20 20 20  s ****\n", .    
87520 20 20 20 20 20 20 20 20 70 48 64 72 2d 3e 69 53          pHdr->iS
87530 69 7a 65 2c 20 26 70 48 64 72 5b 31 5d 2c 20 70  ize, &pHdr[1], p
87540 48 64 72 2d 3e 6e 54 69 74 6c 65 20 3f 20 7a 20  Hdr->nTitle ? z 
87550 3a 20 22 3f 3f 3f 22 29 3b 0a 20 20 20 20 69 66  : "???");.    if
87560 28 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61  ( pHdr->nBacktra
87570 63 65 20 29 7b 0a 20 20 20 20 20 20 66 66 6c 75  ce ){.      fflu
87580 73 68 28 6f 75 74 29 3b 0a 20 20 20 20 20 20 70  sh(out);.      p
87590 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70 48 64  Bt = (void**)pHd
875a0 72 3b 0a 20 20 20 20 20 20 70 42 74 20 2d 3d 20  r;.      pBt -= 
875b0 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65  pHdr->nBacktrace
875c0 53 6c 6f 74 73 3b 0a 20 20 20 20 20 20 62 61 63  Slots;.      bac
875d0 6b 74 72 61 63 65 5f 73 79 6d 62 6f 6c 73 5f 66  ktrace_symbols_f
875e0 64 28 70 42 74 2c 20 70 48 64 72 2d 3e 6e 42 61  d(pBt, pHdr->nBa
875f0 63 6b 74 72 61 63 65 2c 20 66 69 6c 65 6e 6f 28  cktrace, fileno(
87600 6f 75 74 29 29 3b 0a 20 20 20 20 20 20 66 70 72  out));.      fpr
87610 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22 29 3b  intf(out, "\n");
87620 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 70 72  .    }.  }.  fpr
87630 69 6e 74 66 28 6f 75 74 2c 20 22 43 4f 55 4e 54  intf(out, "COUNT
87640 53 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d  S:\n");.  for(i=
87650 30 3b 20 69 3c 4e 43 53 49 5a 45 2d 31 3b 20 69  0; i<NCSIZE-1; i
87660 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d  ++){.    if( mem
87670 2e 6e 41 6c 6c 6f 63 5b 69 5d 20 29 7b 0a 20 20  .nAlloc[i] ){.  
87680 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
87690 20 22 20 20 20 25 35 64 3a 20 25 31 30 64 20 25   "   %5d: %10d %
876a0 31 30 64 20 25 31 30 64 5c 6e 22 2c 20 0a 20 20  10d %10d\n", .  
876b0 20 20 20 20 20 20 20 20 20 20 69 2a 38 2c 20 6d            i*8, m
876c0 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 2c 20 6d 65  em.nAlloc[i], me
876d0 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2c 20 6d  m.nCurrent[i], m
876e0 65 6d 2e 6d 78 43 75 72 72 65 6e 74 5b 69 5d 29  em.mxCurrent[i])
876f0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
87700 28 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53  ( mem.nAlloc[NCS
87710 49 5a 45 2d 31 5d 20 29 7b 0a 20 20 20 20 66 70  IZE-1] ){.    fp
87720 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25  rintf(out, "   %
87730 35 64 3a 20 25 31 30 64 20 25 31 30 64 20 25 31  5d: %10d %10d %1
87740 30 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  0d\n",.         
87750 20 20 20 20 4e 43 53 49 5a 45 2a 38 2d 38 2c 20      NCSIZE*8-8, 
87760 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a  mem.nAlloc[NCSIZ
87770 45 2d 31 5d 2c 0a 20 20 20 20 20 20 20 20 20 20  E-1],.          
87780 20 20 20 6d 65 6d 2e 6e 43 75 72 72 65 6e 74 5b     mem.nCurrent[
87790 4e 43 53 49 5a 45 2d 31 5d 2c 20 6d 65 6d 2e 6d  NCSIZE-1], mem.m
877a0 78 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45 2d  xCurrent[NCSIZE-
877b0 31 5d 29 3b 0a 20 20 7d 0a 20 20 66 63 6c 6f 73  1]);.  }.  fclos
877c0 65 28 6f 75 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  e(out);.}../*.**
877d0 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
877e0 65 72 20 6f 66 20 74 69 6d 65 73 20 73 71 6c 69  er of times sqli
877f0 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 20 68  te3MemMalloc() h
87800 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2e 0a  as been called..
87810 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
87820 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d  E int sqlite3Mem
87830 64 65 62 75 67 4d 61 6c 6c 6f 63 43 6f 75 6e 74  debugMallocCount
87840 28 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  (){.  int i;.  i
87850 6e 74 20 6e 54 6f 74 61 6c 20 3d 20 30 3b 0a 20  nt nTotal = 0;. 
87860 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 53 49   for(i=0; i<NCSI
87870 5a 45 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 54  ZE; i++){.    nT
87880 6f 74 61 6c 20 2b 3d 20 6d 65 6d 2e 6e 41 6c 6c  otal += mem.nAll
87890 6f 63 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65 74  oc[i];.  }.  ret
878a0 75 72 6e 20 6e 54 6f 74 61 6c 3b 0a 7d 0a 0a 0a  urn nTotal;.}...
878b0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
878c0 5f 4d 45 4d 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a  _MEMDEBUG */../*
878d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
878e0 64 20 6f 66 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a  d of mem2.c ****
878f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
87920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
87930 67 69 6e 20 66 69 6c 65 20 6d 65 6d 33 2e 63 20  gin file mem3.c 
87940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
87970 0a 2a 2a 20 32 30 30 37 20 4f 63 74 6f 62 65 72  .** 2007 October
87980 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   14.**.** The au
87990 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
879a0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
879b0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
879c0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
879d0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
879e0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
879f0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
87a00 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
87a10 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
87a20 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
87a30 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
87a40 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
87a50 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
87a60 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
87a70 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
87a80 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
87a90 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
87aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87ae0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
87af0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66  contains the C f
87b00 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d  unctions that im
87b10 70 6c 65 6d 65 6e 74 20 61 20 6d 65 6d 6f 72 79  plement a memory
87b20 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  .** allocation s
87b30 75 62 73 79 73 74 65 6d 20 66 6f 72 20 75 73 65  ubsystem for use
87b40 20 62 79 20 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a   by SQLite. .**.
87b50 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20  ** This version 
87b60 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  of the memory al
87b70 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
87b80 65 6d 20 6f 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20  em omits all.** 
87b90 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e  use of malloc().
87ba0 20 54 68 65 20 53 51 4c 69 74 65 20 75 73 65 72   The SQLite user
87bb0 20 73 75 70 70 6c 69 65 73 20 61 20 62 6c 6f 63   supplies a bloc
87bc0 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62  k of memory.** b
87bd0 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 73 71  efore calling sq
87be0 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65  lite3_initialize
87bf0 28 29 20 66 72 6f 6d 20 77 68 69 63 68 20 61 6c  () from which al
87c00 6c 6f 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72 65  locations.** are
87c10 20 6d 61 64 65 20 61 6e 64 20 72 65 74 75 72 6e   made and return
87c20 65 64 20 62 79 20 74 68 65 20 78 4d 61 6c 6c 6f  ed by the xMallo
87c30 63 28 29 20 61 6e 64 20 78 52 65 61 6c 6c 6f 63  c() and xRealloc
87c40 28 29 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  () .** implement
87c50 61 74 69 6f 6e 73 2e 20 4f 6e 63 65 20 73 71 6c  ations. Once sql
87c60 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
87c70 29 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65  ) has been calle
87c80 64 2c 0a 2a 2a 20 74 68 65 20 61 6d 6f 75 6e 74  d,.** the amount
87c90 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61 69 6c   of memory avail
87ca0 61 62 6c 65 20 74 6f 20 53 51 4c 69 74 65 20 69  able to SQLite i
87cb0 73 20 66 69 78 65 64 20 61 6e 64 20 63 61 6e 6e  s fixed and cann
87cc0 6f 74 0a 2a 2a 20 62 65 20 63 68 61 6e 67 65 64  ot.** be changed
87cd0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72  ..**.** This ver
87ce0 73 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f  sion of the memo
87cf0 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75  ry allocation su
87d00 62 73 79 73 74 65 6d 20 69 73 20 69 6e 63 6c 75  bsystem is inclu
87d10 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 62 75  ded.** in the bu
87d20 69 6c 64 20 6f 6e 6c 79 20 69 66 20 53 51 4c 49  ild only if SQLI
87d30 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
87d40 33 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a  3 is defined..**
87d50 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 33 2e 63 2c  .** $Id: mem3.c,
87d60 76 20 31 2e 32 35 20 32 30 30 38 2f 31 31 2f 31  v 1.25 2008/11/1
87d70 39 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69 65  9 16:52:44 danie
87d80 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
87d90 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73  ./*.** This vers
87da0 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  ion of the memor
87db0 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 6f  y allocator is o
87dc0 6e 6c 79 20 62 75 69 6c 74 20 69 6e 74 6f 20 74  nly built into t
87dd0 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 53 51  he library.** SQ
87de0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53  LITE_ENABLE_MEMS
87df0 59 53 33 20 69 73 20 64 65 66 69 6e 65 64 2e 20  YS3 is defined. 
87e00 44 65 66 69 6e 69 6e 67 20 74 68 69 73 20 73 79  Defining this sy
87e10 6d 62 6f 6c 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a  mbol does not.**
87e20 20 6d 65 61 6e 20 74 68 61 74 20 74 68 65 20 6c   mean that the l
87e30 69 62 72 61 72 79 20 77 69 6c 6c 20 75 73 65 20  ibrary will use 
87e40 61 20 6d 65 6d 6f 72 79 2d 70 6f 6f 6c 20 62 79  a memory-pool by
87e50 20 64 65 66 61 75 6c 74 2c 20 6a 75 73 74 20 74   default, just t
87e60 68 61 74 0a 2a 2a 20 69 74 20 69 73 20 61 76 61  hat.** it is ava
87e70 69 6c 61 62 6c 65 2e 20 54 68 65 20 6d 65 6d 70  ilable. The memp
87e80 6f 6f 6c 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73  ool allocator is
87e90 20 61 63 74 69 76 61 74 65 64 20 62 79 20 63 61   activated by ca
87ea0 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33  lling.** sqlite3
87eb0 5f 63 6f 6e 66 69 67 28 29 2e 0a 2a 2f 0a 23 69  _config()..*/.#i
87ec0 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
87ed0 4c 45 5f 4d 45 4d 53 59 53 33 0a 0a 2f 2a 0a 2a  LE_MEMSYS3../*.*
87ee0 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 28  * Maximum size (
87ef0 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 6f  in Mem3Blocks) o
87f00 66 20 61 20 22 73 6d 61 6c 6c 22 20 63 68 75 6e  f a "small" chun
87f10 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58  k..*/.#define MX
87f20 5f 53 4d 41 4c 4c 20 31 30 0a 0a 0a 2f 2a 0a 2a  _SMALL 10.../*.*
87f30 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65  * Number of free
87f40 6c 69 73 74 20 68 61 73 68 20 73 6c 6f 74 73 0a  list hash slots.
87f50 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 5f 48 41 53  */.#define N_HAS
87f60 48 20 20 36 31 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d  H  61../*.** A m
87f70 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
87f80 20 28 61 6c 73 6f 20 63 61 6c 6c 65 64 20 61 20   (also called a 
87f90 22 63 68 75 6e 6b 22 29 20 63 6f 6e 73 69 73 74  "chunk") consist
87fa0 73 20 6f 66 20 74 77 6f 20 6f 72 20 0a 2a 2a 20  s of two or .** 
87fb0 6d 6f 72 65 20 62 6c 6f 63 6b 73 20 77 68 65 72  more blocks wher
87fc0 65 20 65 61 63 68 20 62 6c 6f 63 6b 20 69 73 20  e each block is 
87fd0 38 20 62 79 74 65 73 2e 20 20 54 68 65 20 66 69  8 bytes.  The fi
87fe0 72 73 74 20 38 20 62 79 74 65 73 20 61 72 65 20  rst 8 bytes are 
87ff0 0a 2a 2a 20 61 20 68 65 61 64 65 72 20 74 68 61  .** a header tha
88000 74 20 69 73 20 6e 6f 74 20 72 65 74 75 72 6e 65  t is not returne
88010 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 0a 2a  d to the user..*
88020 2a 0a 2a 2a 20 41 20 63 68 75 6e 6b 20 69 73 20  *.** A chunk is 
88030 74 77 6f 20 6f 72 20 6d 6f 72 65 20 62 6c 6f 63  two or more bloc
88040 6b 73 20 74 68 61 74 20 69 73 20 65 69 74 68 65  ks that is eithe
88050 72 20 63 68 65 63 6b 65 64 20 6f 75 74 20 6f 72  r checked out or
88060 0a 2a 2a 20 66 72 65 65 2e 20 20 54 68 65 20 66  .** free.  The f
88070 69 72 73 74 20 62 6c 6f 63 6b 20 68 61 73 20 66  irst block has f
88080 6f 72 6d 61 74 20 75 2e 68 64 72 2e 20 20 75 2e  ormat u.hdr.  u.
88090 68 64 72 2e 73 69 7a 65 34 78 20 69 73 20 34 20  hdr.size4x is 4 
880a0 74 69 6d 65 73 20 74 68 65 0a 2a 2a 20 73 69 7a  times the.** siz
880b0 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74  e of the allocat
880c0 69 6f 6e 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66  ion in blocks if
880d0 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
880e0 69 73 20 66 72 65 65 2e 0a 2a 2a 20 54 68 65 20  is free..** The 
880f0 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 31 20 62  u.hdr.size4x&1 b
88100 69 74 20 69 73 20 74 72 75 65 20 69 66 20 74 68  it is true if th
88110 65 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b  e chunk is check
88120 65 64 20 6f 75 74 20 61 6e 64 0a 2a 2a 20 66 61  ed out and.** fa
88130 6c 73 65 20 69 66 20 74 68 65 20 63 68 75 6e 6b  lse if the chunk
88140 20 69 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c   is on the freel
88150 69 73 74 2e 20 20 54 68 65 20 75 2e 68 64 72 2e  ist.  The u.hdr.
88160 73 69 7a 65 34 78 26 32 20 62 69 74 0a 2a 2a 20  size4x&2 bit.** 
88170 69 73 20 74 72 75 65 20 69 66 20 74 68 65 20 70  is true if the p
88180 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69 73  revious chunk is
88190 20 63 68 65 63 6b 65 64 20 6f 75 74 20 61 6e 64   checked out and
881a0 20 66 61 6c 73 65 20 69 66 20 74 68 65 0a 2a 2a   false if the.**
881b0 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20   previous chunk 
881c0 69 73 20 66 72 65 65 2e 20 20 54 68 65 20 75 2e  is free.  The u.
881d0 68 64 72 2e 70 72 65 76 53 69 7a 65 20 66 69 65  hdr.prevSize fie
881e0 6c 64 20 69 73 20 74 68 65 20 73 69 7a 65 20 6f  ld is the size o
881f0 66 0a 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75  f.** the previou
88200 73 20 63 68 75 6e 6b 20 69 6e 20 62 6c 6f 63 6b  s chunk in block
88210 73 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75  s if the previou
88220 73 20 63 68 75 6e 6b 20 69 73 20 6f 6e 20 74 68  s chunk is on th
88230 65 0a 2a 2a 20 66 72 65 65 6c 69 73 74 2e 20 49  e.** freelist. I
88240 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63  f the previous c
88250 68 75 6e 6b 20 69 73 20 63 68 65 63 6b 65 64 20  hunk is checked 
88260 6f 75 74 2c 20 74 68 65 6e 0a 2a 2a 20 75 2e 68  out, then.** u.h
88270 64 72 2e 70 72 65 76 53 69 7a 65 20 63 61 6e 20  dr.prevSize can 
88280 62 65 20 70 61 72 74 20 6f 66 20 74 68 65 20 64  be part of the d
88290 61 74 61 20 66 6f 72 20 74 68 61 74 20 63 68 75  ata for that chu
882a0 6e 6b 20 61 6e 64 20 73 68 6f 75 6c 64 0a 2a 2a  nk and should.**
882b0 20 6e 6f 74 20 62 65 20 72 65 61 64 20 6f 72 20   not be read or 
882c0 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 57  written..**.** W
882d0 65 20 6f 66 74 65 6e 20 69 64 65 6e 74 69 66 79  e often identify
882e0 20 61 20 63 68 75 6e 6b 20 62 79 20 69 74 73 20   a chunk by its 
882f0 69 6e 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50  index in mem3.aP
88300 6f 6f 6c 5b 5d 2e 20 20 57 68 65 6e 0a 2a 2a 20  ool[].  When.** 
88310 74 68 69 73 20 69 73 20 64 6f 6e 65 2c 20 74 68  this is done, th
88320 65 20 63 68 75 6e 6b 20 69 6e 64 65 78 20 72 65  e chunk index re
88330 66 65 72 73 20 74 6f 20 74 68 65 20 73 65 63 6f  fers to the seco
88340 6e 64 20 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 74  nd block of.** t
88350 68 65 20 63 68 75 6e 6b 2e 20 20 49 6e 20 74 68  he chunk.  In th
88360 69 73 20 77 61 79 2c 20 74 68 65 20 66 69 72 73  is way, the firs
88370 74 20 63 68 75 6e 6b 20 68 61 73 20 61 6e 20 69  t chunk has an i
88380 6e 64 65 78 20 6f 66 20 31 2e 0a 2a 2a 20 41 20  ndex of 1..** A 
88390 63 68 75 6e 6b 20 69 6e 64 65 78 20 6f 66 20 30  chunk index of 0
883a0 20 6d 65 61 6e 73 20 22 6e 6f 20 73 75 63 68 20   means "no such 
883b0 63 68 75 6e 6b 22 20 61 6e 64 20 69 73 20 74 68  chunk" and is th
883c0 65 20 65 71 75 69 76 61 6c 65 6e 74 0a 2a 2a 20  e equivalent.** 
883d0 6f 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  of a NULL pointe
883e0 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63  r..**.** The sec
883f0 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20 66 72 65  ond block of fre
88400 65 20 63 68 75 6e 6b 73 20 69 73 20 6f 66 20 74  e chunks is of t
88410 68 65 20 66 6f 72 6d 20 75 2e 6c 69 73 74 2e 20  he form u.list. 
88420 20 54 68 65 0a 2a 2a 20 74 77 6f 20 66 69 65 6c   The.** two fiel
88430 64 73 20 66 6f 72 6d 20 61 20 64 6f 75 62 6c 65  ds form a double
88440 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20  -linked list of 
88450 63 68 75 6e 6b 73 20 6f 66 20 72 65 6c 61 74 65  chunks of relate
88460 64 20 73 69 7a 65 73 2e 0a 2a 2a 20 50 6f 69 6e  d sizes..** Poin
88470 74 65 72 73 20 74 6f 20 74 68 65 20 68 65 61 64  ters to the head
88480 20 6f 66 20 74 68 65 20 6c 69 73 74 20 61 72 65   of the list are
88490 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 33 2e   stored in mem3.
884a0 61 69 53 6d 61 6c 6c 5b 5d 20 0a 2a 2a 20 66 6f  aiSmall[] .** fo
884b0 72 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e 6b 73  r smaller chunks
884c0 20 61 6e 64 20 6d 65 6d 33 2e 61 69 48 61 73 68   and mem3.aiHash
884d0 5b 5d 20 66 6f 72 20 6c 61 72 67 65 72 20 63 68  [] for larger ch
884e0 75 6e 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  unks..**.** The 
884f0 73 65 63 6f 6e 64 20 62 6c 6f 63 6b 20 6f 66 20  second block of 
88500 61 20 63 68 75 6e 6b 20 69 73 20 75 73 65 72 20  a chunk is user 
88510 64 61 74 61 20 69 66 20 74 68 65 20 63 68 75 6e  data if the chun
88520 6b 20 69 73 20 63 68 65 63 6b 65 64 20 0a 2a 2a  k is checked .**
88530 20 6f 75 74 2e 20 20 49 66 20 61 20 63 68 75 6e   out.  If a chun
88540 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74  k is checked out
88550 2c 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20  , the user data 
88560 6d 61 79 20 65 78 74 65 6e 64 20 69 6e 74 6f 0a  may extend into.
88570 2a 2a 20 74 68 65 20 75 2e 68 64 72 2e 70 72 65  ** the u.hdr.pre
88580 76 53 69 7a 65 20 76 61 6c 75 65 20 6f 66 20 74  vSize value of t
88590 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 75  he following chu
885a0 6e 6b 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  nk..*/.typedef s
885b0 74 72 75 63 74 20 4d 65 6d 33 42 6c 6f 63 6b 20  truct Mem3Block 
885c0 4d 65 6d 33 42 6c 6f 63 6b 3b 0a 73 74 72 75 63  Mem3Block;.struc
885d0 74 20 4d 65 6d 33 42 6c 6f 63 6b 20 7b 0a 20 20  t Mem3Block {.  
885e0 75 6e 69 6f 6e 20 7b 0a 20 20 20 20 73 74 72 75  union {.    stru
885f0 63 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 70  ct {.      u32 p
88600 72 65 76 53 69 7a 65 3b 20 20 20 2f 2a 20 53 69  revSize;   /* Si
88610 7a 65 20 6f 66 20 70 72 65 76 69 6f 75 73 20 63  ze of previous c
88620 68 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63  hunk in Mem3Bloc
88630 6b 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20  k elements */.  
88640 20 20 20 20 75 33 32 20 73 69 7a 65 34 78 3b 20      u32 size4x; 
88650 20 20 20 20 2f 2a 20 34 78 20 74 68 65 20 73 69      /* 4x the si
88660 7a 65 20 6f 66 20 63 75 72 72 65 6e 74 20 63 68  ze of current ch
88670 75 6e 6b 20 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b  unk in Mem3Block
88680 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20   elements */.   
88690 20 7d 20 68 64 72 3b 0a 20 20 20 20 73 74 72 75   } hdr;.    stru
886a0 63 74 20 7b 0a 20 20 20 20 20 20 75 33 32 20 6e  ct {.      u32 n
886b0 65 78 74 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e  ext;       /* In
886c0 64 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f  dex in mem3.aPoo
886d0 6c 5b 5d 20 6f 66 20 6e 65 78 74 20 66 72 65 65  l[] of next free
886e0 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 20 20 20 20   chunk */.      
886f0 75 33 32 20 70 72 65 76 3b 20 20 20 20 20 20 20  u32 prev;       
88700 2f 2a 20 49 6e 64 65 78 20 69 6e 20 6d 65 6d 33  /* Index in mem3
88710 2e 61 50 6f 6f 6c 5b 5d 20 6f 66 20 70 72 65 76  .aPool[] of prev
88720 69 6f 75 73 20 66 72 65 65 20 63 68 75 6e 6b 20  ious free chunk 
88730 2a 2f 0a 20 20 20 20 7d 20 6c 69 73 74 3b 0a 20  */.    } list;. 
88740 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20   } u;.};../*.** 
88750 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69  All of the stati
88760 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64  c variables used
88770 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20   by this module 
88780 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a  are collected.**
88790 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73   into a single s
887a0 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22  tructure named "
887b0 6d 65 6d 33 22 2e 20 20 54 68 69 73 20 69 73 20  mem3".  This is 
887c0 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73  to keep the.** s
887d0 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20  tatic variables 
887e0 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f  organized and to
887f0 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63   reduce namespac
88800 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77  e pollution.** w
88810 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20  hen this module 
88820 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68  is combined with
88830 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d   other in the am
88840 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73  algamation..*/.s
88850 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44  tatic SQLITE_WSD
88860 20 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62   struct Mem3Glob
88870 61 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d  al {.  /*.  ** M
88880 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20  emory available 
88890 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20  for allocation. 
888a0 6e 50 6f 6f 6c 20 69 73 20 74 68 65 20 73 69 7a  nPool is the siz
888b0 65 20 6f 66 20 74 68 65 20 61 72 72 61 79 0a 20  e of the array. 
888c0 20 2a 2a 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63   ** (in Mem3Bloc
888d0 6b 73 29 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  ks) pointed to b
888e0 79 20 61 50 6f 6f 6c 20 6c 65 73 73 20 32 2e 0a  y aPool less 2..
888f0 20 20 2a 2f 0a 20 20 75 33 32 20 6e 50 6f 6f 6c    */.  u32 nPool
88900 3b 0a 20 20 4d 65 6d 33 42 6c 6f 63 6b 20 2a 61  ;.  Mem3Block *a
88910 50 6f 6f 6c 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  Pool;..  /*.  **
88920 20 54 72 75 65 20 69 66 20 77 65 20 61 72 65 20   True if we are 
88930 65 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 6f 75  evaluating an ou
88940 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 63 61 6c 6c  t-of-memory call
88950 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74  back..  */.  int
88960 20 61 6c 61 72 6d 42 75 73 79 3b 0a 20 20 0a 20   alarmBusy;.  . 
88970 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74   /*.  ** Mutex t
88980 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73  o control access
88990 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   to the memory a
889a0 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
889b0 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  tem..  */.  sqli
889c0 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
889d0 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54  ;.  .  /*.  ** T
889e0 68 65 20 6d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e  he minimum amoun
889f0 74 20 6f 66 20 66 72 65 65 20 73 70 61 63 65 20  t of free space 
88a00 74 68 61 74 20 77 65 20 68 61 76 65 20 73 65 65  that we have see
88a10 6e 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 6d 6e  n..  */.  u32 mn
88a20 4d 61 73 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20  Master;..  /*.  
88a30 2a 2a 20 69 4d 61 73 74 65 72 20 69 73 20 74 68  ** iMaster is th
88a40 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d  e index of the m
88a50 61 73 74 65 72 20 63 68 75 6e 6b 2e 20 20 4d 6f  aster chunk.  Mo
88a60 73 74 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f  st new allocatio
88a70 6e 73 0a 20 20 2a 2a 20 6f 63 63 75 72 20 6f 66  ns.  ** occur of
88a80 66 20 6f 66 20 74 68 69 73 20 63 68 75 6e 6b 2e  f of this chunk.
88a90 20 20 73 7a 4d 61 73 74 65 72 20 69 73 20 74 68    szMaster is th
88aa0 65 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d 33 42  e size (in Mem3B
88ab0 6c 6f 63 6b 73 29 0a 20 20 2a 2a 20 6f 66 20 74  locks).  ** of t
88ac0 68 65 20 63 75 72 72 65 6e 74 20 6d 61 73 74 65  he current maste
88ad0 72 2e 20 20 69 4d 61 73 74 65 72 20 69 73 20 30  r.  iMaster is 0
88ae0 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74   if there is not
88af0 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 0a 20   master chunk.. 
88b00 20 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 63   ** The master c
88b10 68 75 6e 6b 20 69 73 20 6e 6f 74 20 69 6e 20 65  hunk is not in e
88b20 69 74 68 65 72 20 74 68 65 20 61 69 48 61 73 68  ither the aiHash
88b30 5b 5d 20 6f 72 20 61 69 53 6d 61 6c 6c 5b 5d 2e  [] or aiSmall[].
88b40 0a 20 20 2a 2f 0a 20 20 75 33 32 20 69 4d 61 73  .  */.  u32 iMas
88b50 74 65 72 3b 0a 20 20 75 33 32 20 73 7a 4d 61 73  ter;.  u32 szMas
88b60 74 65 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  ter;..  /*.  ** 
88b70 41 72 72 61 79 20 6f 66 20 6c 69 73 74 73 20 6f  Array of lists o
88b80 66 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 61 63  f free blocks ac
88b90 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 62  cording to the b
88ba0 6c 6f 63 6b 20 73 69 7a 65 20 0a 20 20 2a 2a 20  lock size .  ** 
88bb0 66 6f 72 20 73 6d 61 6c 6c 65 72 20 63 68 75 6e  for smaller chun
88bc0 6b 73 2c 20 6f 72 20 61 20 68 61 73 68 20 6f 6e  ks, or a hash on
88bd0 20 74 68 65 20 62 6c 6f 63 6b 20 73 69 7a 65 20   the block size 
88be0 66 6f 72 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20  for larger.  ** 
88bf0 63 68 75 6e 6b 73 2e 0a 20 20 2a 2f 0a 20 20 75  chunks..  */.  u
88c00 33 32 20 61 69 53 6d 61 6c 6c 5b 4d 58 5f 53 4d  32 aiSmall[MX_SM
88c10 41 4c 4c 2d 31 5d 3b 20 20 20 2f 2a 20 46 6f 72  ALL-1];   /* For
88c20 20 73 69 7a 65 73 20 32 20 74 68 72 6f 75 67 68   sizes 2 through
88c30 20 4d 58 5f 53 4d 41 4c 4c 2c 20 69 6e 63 6c 75   MX_SMALL, inclu
88c40 73 69 76 65 20 2a 2f 0a 20 20 75 33 32 20 61 69  sive */.  u32 ai
88c50 48 61 73 68 5b 4e 5f 48 41 53 48 5d 3b 20 20 20  Hash[N_HASH];   
88c60 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65       /* For size
88c70 73 20 4d 58 5f 53 4d 41 4c 4c 2b 31 20 61 6e 64  s MX_SMALL+1 and
88c80 20 6c 61 72 67 65 72 20 2a 2f 0a 7d 20 6d 65 6d   larger */.} mem
88c90 33 20 3d 20 7b 20 39 37 35 33 35 35 37 35 20 7d  3 = { 97535575 }
88ca0 3b 0a 0a 23 64 65 66 69 6e 65 20 6d 65 6d 33 20  ;..#define mem3 
88cb0 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 4d 65  GLOBAL(struct Me
88cc0 6d 33 47 6c 6f 62 61 6c 2c 20 6d 65 6d 33 29 0a  m3Global, mem3).
88cd0 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68  ./*.** Unlink th
88ce0 65 20 63 68 75 6e 6b 20 61 74 20 6d 65 6d 33 2e  e chunk at mem3.
88cf0 61 50 6f 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69  aPool[i] from li
88d00 73 74 20 69 74 20 69 73 20 63 75 72 72 65 6e 74  st it is current
88d10 6c 79 0a 2a 2a 20 6f 6e 2e 20 20 2a 70 52 6f 6f  ly.** on.  *pRoo
88d20 74 20 69 73 20 74 68 65 20 6c 69 73 74 20 74 68  t is the list th
88d30 61 74 20 69 20 69 73 20 61 20 6d 65 6d 62 65 72  at i is a member
88d40 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76   of..*/.static v
88d50 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e  oid memsys3Unlin
88d60 6b 46 72 6f 6d 4c 69 73 74 28 75 33 32 20 69 2c  kFromList(u32 i,
88d70 20 75 33 32 20 2a 70 52 6f 6f 74 29 7b 0a 20 20   u32 *pRoot){.  
88d80 75 33 32 20 6e 65 78 74 20 3d 20 6d 65 6d 33 2e  u32 next = mem3.
88d90 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e  aPool[i].u.list.
88da0 6e 65 78 74 3b 0a 20 20 75 33 32 20 70 72 65 76  next;.  u32 prev
88db0 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d   = mem3.aPool[i]
88dc0 2e 75 2e 6c 69 73 74 2e 70 72 65 76 3b 0a 20 20  .u.list.prev;.  
88dd0 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
88de0 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e  mutex_held(mem3.
88df0 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
88e00 70 72 65 76 3d 3d 30 20 29 7b 0a 20 20 20 20 2a  prev==0 ){.    *
88e10 70 52 6f 6f 74 20 3d 20 6e 65 78 74 3b 0a 20 20  pRoot = next;.  
88e20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 33 2e  }else{.    mem3.
88e30 61 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69  aPool[prev].u.li
88e40 73 74 2e 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a  st.next = next;.
88e50 20 20 7d 0a 20 20 69 66 28 20 6e 65 78 74 20 29    }.  if( next )
88e60 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c  {.    mem3.aPool
88e70 5b 6e 65 78 74 5d 2e 75 2e 6c 69 73 74 2e 70 72  [next].u.list.pr
88e80 65 76 20 3d 20 70 72 65 76 3b 0a 20 20 7d 0a 20  ev = prev;.  }. 
88e90 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75   mem3.aPool[i].u
88ea0 2e 6c 69 73 74 2e 6e 65 78 74 20 3d 20 30 3b 0a  .list.next = 0;.
88eb0 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e    mem3.aPool[i].
88ec0 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 30 3b  u.list.prev = 0;
88ed0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b  .}../*.** Unlink
88ee0 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 69 6e   the chunk at in
88ef0 64 65 78 20 69 20 66 72 6f 6d 20 0a 2a 2a 20 77  dex i from .** w
88f00 68 61 74 65 76 65 72 20 6c 69 73 74 20 69 73 20  hatever list is 
88f10 63 75 72 72 65 6e 74 6c 79 20 61 20 6d 65 6d 62  currently a memb
88f20 65 72 20 6f 66 2e 0a 2a 2f 0a 73 74 61 74 69 63  er of..*/.static
88f30 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 55 6e 6c   void memsys3Unl
88f40 69 6e 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 33  ink(u32 i){.  u3
88f50 32 20 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20  2 size, hash;.  
88f60 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
88f70 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e  mutex_held(mem3.
88f80 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
88f90 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  rt( (mem3.aPool[
88fa0 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  i-1].u.hdr.size4
88fb0 78 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 61  x & 1)==0 );.  a
88fc0 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20  ssert( i>=1 );. 
88fd0 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f   size = mem3.aPo
88fe0 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ol[i-1].u.hdr.si
88ff0 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74  ze4x/4;.  assert
89000 28 20 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f  ( size==mem3.aPo
89010 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68  ol[i+size-1].u.h
89020 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20  dr.prevSize );. 
89030 20 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 32   assert( size>=2
89040 20 29 3b 0a 20 20 69 66 28 20 73 69 7a 65 20 3c   );.  if( size <
89050 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20  = MX_SMALL ){.  
89060 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46    memsys3UnlinkF
89070 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33  romList(i, &mem3
89080 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d  .aiSmall[size-2]
89090 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
890a0 68 61 73 68 20 3d 20 73 69 7a 65 20 25 20 4e 5f  hash = size % N_
890b0 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73 79 73  HASH;.    memsys
890c0 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28  3UnlinkFromList(
890d0 69 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b  i, &mem3.aiHash[
890e0 68 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  hash]);.  }.}../
890f0 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65 20 63 68  *.** Link the ch
89100 75 6e 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f  unk at mem3.aPoo
89110 6c 5b 69 5d 20 73 6f 20 74 68 61 74 20 69 73 20  l[i] so that is 
89120 6f 6e 20 74 68 65 20 6c 69 73 74 20 72 6f 6f 74  on the list root
89130 65 64 0a 2a 2a 20 61 74 20 2a 70 52 6f 6f 74 2e  ed.** at *pRoot.
89140 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
89150 6d 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c  memsys3LinkIntoL
89160 69 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a  ist(u32 i, u32 *
89170 70 52 6f 6f 74 29 7b 0a 20 20 61 73 73 65 72 74  pRoot){.  assert
89180 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
89190 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29  held(mem3.mutex)
891a0 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c   );.  mem3.aPool
891b0 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 20  [i].u.list.next 
891c0 3d 20 2a 70 52 6f 6f 74 3b 0a 20 20 6d 65 6d 33  = *pRoot;.  mem3
891d0 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74  .aPool[i].u.list
891e0 2e 70 72 65 76 20 3d 20 30 3b 0a 20 20 69 66 28  .prev = 0;.  if(
891f0 20 2a 70 52 6f 6f 74 20 29 7b 0a 20 20 20 20 6d   *pRoot ){.    m
89200 65 6d 33 2e 61 50 6f 6f 6c 5b 2a 70 52 6f 6f 74  em3.aPool[*pRoot
89210 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20  ].u.list.prev = 
89220 69 3b 0a 20 20 7d 0a 20 20 2a 70 52 6f 6f 74 20  i;.  }.  *pRoot 
89230 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69  = i;.}../*.** Li
89240 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20  nk the chunk at 
89250 69 6e 64 65 78 20 69 20 69 6e 74 6f 20 65 69 74  index i into eit
89260 68 65 72 20 74 68 65 20 61 70 70 72 6f 70 72 69  her the appropri
89270 61 74 65 0a 2a 2a 20 73 6d 61 6c 6c 20 63 68 75  ate.** small chu
89280 6e 6b 20 6c 69 73 74 2c 20 6f 72 20 69 6e 74 6f  nk list, or into
89290 20 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b   the large chunk
892a0 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a   hash table..*/.
892b0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73  static void mems
892c0 79 73 33 4c 69 6e 6b 28 75 33 32 20 69 29 7b 0a  ys3Link(u32 i){.
892d0 20 20 75 33 32 20 73 69 7a 65 2c 20 68 61 73 68    u32 size, hash
892e0 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
892f0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d  te3_mutex_held(m
89300 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20  em3.mutex) );.  
89310 61 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a  assert( i>=1 );.
89320 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e    assert( (mem3.
89330 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72  aPool[i-1].u.hdr
89340 2e 73 69 7a 65 34 78 20 26 20 31 29 3d 3d 30 20  .size4x & 1)==0 
89350 29 3b 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33  );.  size = mem3
89360 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64  .aPool[i-1].u.hd
89370 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73  r.size4x/4;.  as
89380 73 65 72 74 28 20 73 69 7a 65 3d 3d 6d 65 6d 33  sert( size==mem3
89390 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d  .aPool[i+size-1]
893a0 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20  .u.hdr.prevSize 
893b0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a  );.  assert( siz
893c0 65 3e 3d 32 20 29 3b 0a 20 20 69 66 28 20 73 69  e>=2 );.  if( si
893d0 7a 65 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20 29  ze <= MX_SMALL )
893e0 7b 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e  {.    memsys3Lin
893f0 6b 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65  kIntoList(i, &me
89400 6d 33 2e 61 69 53 6d 61 6c 6c 5b 73 69 7a 65 2d  m3.aiSmall[size-
89410 32 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  2]);.  }else{.  
89420 20 20 68 61 73 68 20 3d 20 73 69 7a 65 20 25 20    hash = size % 
89430 4e 5f 48 41 53 48 3b 0a 20 20 20 20 6d 65 6d 73  N_HASH;.    mems
89440 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28  ys3LinkIntoList(
89450 69 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b  i, &mem3.aiHash[
89460 68 61 73 68 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  hash]);.  }.}../
89470 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41 54  *.** If the STAT
89480 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73 20  IC_MEM mutex is 
89490 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c 64  not already held
894a0 2c 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77 2e  , obtain it now.
894b0 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77 69   The mutex.** wi
894c0 6c 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68 65  ll already be he
894d0 6c 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79 20  ld (obtained by 
894e0 63 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e 63  code in malloc.c
894f0 29 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33 47  ) if.** sqlite3G
89500 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d  lobalConfig.bMem
89510 53 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a 2f  Stat is true..*/
89520 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
89530 73 79 73 33 45 6e 74 65 72 28 76 6f 69 64 29 7b  sys3Enter(void){
89540 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
89550 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73  obalConfig.bMems
89560 74 61 74 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 6d  tat==0 && mem3.m
89570 75 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 6d  utex==0 ){.    m
89580 65 6d 33 2e 6d 75 74 65 78 20 3d 20 73 71 6c 69  em3.mutex = sqli
89590 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
895a0 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
895b0 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 73 71  C_MEM);.  }.  sq
895c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
895d0 72 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d  r(mem3.mutex);.}
895e0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
895f0 73 79 73 33 4c 65 61 76 65 28 76 6f 69 64 29 7b  sys3Leave(void){
89600 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
89610 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d 75 74 65  _leave(mem3.mute
89620 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c  x);.}../*.** Cal
89630 6c 65 64 20 77 68 65 6e 20 77 65 20 61 72 65 20  led when we are 
89640 75 6e 61 62 6c 65 20 74 6f 20 73 61 74 69 73 66  unable to satisf
89650 79 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  y an allocation 
89660 6f 66 20 6e 42 79 74 65 73 2e 0a 2a 2f 0a 73 74  of nBytes..*/.st
89670 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73  atic void memsys
89680 33 4f 75 74 4f 66 4d 65 6d 6f 72 79 28 69 6e 74  3OutOfMemory(int
89690 20 6e 42 79 74 65 29 7b 0a 20 20 69 66 28 20 21   nByte){.  if( !
896a0 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 29  mem3.alarmBusy )
896b0 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d  {.    mem3.alarm
896c0 42 75 73 79 20 3d 20 31 3b 0a 20 20 20 20 61 73  Busy = 1;.    as
896d0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
896e0 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75  tex_held(mem3.mu
896f0 74 65 78 29 20 29 3b 0a 20 20 20 20 73 71 6c 69  tex) );.    sqli
89700 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
89710 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20 20 20  mem3.mutex);.   
89720 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65   sqlite3_release
89730 5f 6d 65 6d 6f 72 79 28 6e 42 79 74 65 29 3b 0a  _memory(nByte);.
89740 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
89750 78 5f 65 6e 74 65 72 28 6d 65 6d 33 2e 6d 75 74  x_enter(mem3.mut
89760 65 78 29 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 6c  ex);.    mem3.al
89770 61 72 6d 42 75 73 79 20 3d 20 30 3b 0a 20 20 7d  armBusy = 0;.  }
89780 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 75 6e 6b  .}.../*.** Chunk
89790 20 69 20 69 73 20 61 20 66 72 65 65 20 63 68 75   i is a free chu
897a0 6e 6b 20 74 68 61 74 20 68 61 73 20 62 65 65 6e  nk that has been
897b0 20 75 6e 6c 69 6e 6b 65 64 2e 20 20 41 64 6a 75   unlinked.  Adju
897c0 73 74 20 69 74 73 20 0a 2a 2a 20 73 69 7a 65 20  st its .** size 
897d0 70 61 72 61 6d 65 74 65 72 73 20 66 6f 72 20 63  parameters for c
897e0 68 65 63 6b 2d 6f 75 74 20 61 6e 64 20 72 65 74  heck-out and ret
897f0 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
89800 20 74 68 65 20 0a 2a 2a 20 75 73 65 72 20 70 6f   the .** user po
89810 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 68 75  rtion of the chu
89820 6e 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  nk..*/.static vo
89830 69 64 20 2a 6d 65 6d 73 79 73 33 43 68 65 63 6b  id *memsys3Check
89840 6f 75 74 28 75 33 32 20 69 2c 20 75 33 32 20 6e  out(u32 i, u32 n
89850 42 6c 6f 63 6b 29 7b 0a 20 20 75 33 32 20 78 3b  Block){.  u32 x;
89860 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
89870 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65  e3_mutex_held(me
89880 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  m3.mutex) );.  a
89890 73 73 65 72 74 28 20 69 3e 3d 31 20 29 3b 0a 20  ssert( i>=1 );. 
898a0 20 61 73 73 65 72 74 28 20 6d 65 6d 33 2e 61 50   assert( mem3.aP
898b0 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73  ool[i-1].u.hdr.s
898c0 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20  ize4x/4==nBlock 
898d0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d  );.  assert( mem
898e0 33 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b  3.aPool[i+nBlock
898f0 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69  -1].u.hdr.prevSi
89900 7a 65 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20  ze==nBlock );.  
89910 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  x = mem3.aPool[i
89920 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
89930 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  ;.  mem3.aPool[i
89940 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
89950 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20 7c 20 31 20   = nBlock*4 | 1 
89960 7c 20 28 78 26 32 29 3b 0a 20 20 6d 65 6d 33 2e  | (x&2);.  mem3.
89970 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31  aPool[i+nBlock-1
89980 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65  ].u.hdr.prevSize
89990 20 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20 6d 65 6d   = nBlock;.  mem
899a0 33 2e 61 50 6f 6f 6c 5b 69 2b 6e 42 6c 6f 63 6b  3.aPool[i+nBlock
899b0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
899c0 20 7c 3d 20 32 3b 0a 20 20 72 65 74 75 72 6e 20   |= 2;.  return 
899d0 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 3b 0a  &mem3.aPool[i];.
899e0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 72 76 65 20 61  }../*.** Carve a
899f0 20 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74 68   piece off of th
89a00 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 65 6d  e end of the mem
89a10 33 2e 69 4d 61 73 74 65 72 20 66 72 65 65 20 63  3.iMaster free c
89a20 68 75 6e 6b 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  hunk..** Return 
89a30 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
89a40 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e   new allocation.
89a50 20 20 4f 72 2c 20 69 66 20 74 68 65 20 6d 61 73    Or, if the mas
89a60 74 65 72 20 63 68 75 6e 6b 0a 2a 2a 20 69 73 20  ter chunk.** is 
89a70 6e 6f 74 20 6c 61 72 67 65 20 65 6e 6f 75 67 68  not large enough
89a80 2c 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 73  , return 0..*/.s
89a90 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73  tatic void *mems
89aa0 79 73 33 46 72 6f 6d 4d 61 73 74 65 72 28 75 33  ys3FromMaster(u3
89ab0 32 20 6e 42 6c 6f 63 6b 29 7b 0a 20 20 61 73 73  2 nBlock){.  ass
89ac0 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
89ad0 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74  ex_held(mem3.mut
89ae0 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
89af0 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 3e 3d   mem3.szMaster>=
89b00 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 69 66 28 20  nBlock );.  if( 
89b10 6e 42 6c 6f 63 6b 3e 3d 6d 65 6d 33 2e 73 7a 4d  nBlock>=mem3.szM
89b20 61 73 74 65 72 2d 31 20 29 7b 0a 20 20 20 20 2f  aster-1 ){.    /
89b30 2a 20 55 73 65 20 74 68 65 20 65 6e 74 69 72 65  * Use the entire
89b40 20 6d 61 73 74 65 72 20 2a 2f 0a 20 20 20 20 76   master */.    v
89b50 6f 69 64 20 2a 70 20 3d 20 6d 65 6d 73 79 73 33  oid *p = memsys3
89b60 43 68 65 63 6b 6f 75 74 28 6d 65 6d 33 2e 69 4d  Checkout(mem3.iM
89b70 61 73 74 65 72 2c 20 6d 65 6d 33 2e 73 7a 4d 61  aster, mem3.szMa
89b80 73 74 65 72 29 3b 0a 20 20 20 20 6d 65 6d 33 2e  ster);.    mem3.
89b90 69 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20  iMaster = 0;.   
89ba0 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 3d   mem3.szMaster =
89bb0 20 30 3b 0a 20 20 20 20 6d 65 6d 33 2e 6d 6e 4d   0;.    mem3.mnM
89bc0 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 72  aster = 0;.    r
89bd0 65 74 75 72 6e 20 70 3b 0a 20 20 7d 65 6c 73 65  eturn p;.  }else
89be0 7b 0a 20 20 20 20 2f 2a 20 53 70 6c 69 74 20 74  {.    /* Split t
89bf0 68 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b 2e  he master block.
89c00 20 20 52 65 74 75 72 6e 20 74 68 65 20 74 61 69    Return the tai
89c10 6c 2e 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 65  l. */.    u32 ne
89c20 77 69 2c 20 78 3b 0a 20 20 20 20 6e 65 77 69 20  wi, x;.    newi 
89c30 3d 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 2b  = mem3.iMaster +
89c40 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 2d   mem3.szMaster -
89c50 20 6e 42 6c 6f 63 6b 3b 0a 20 20 20 20 61 73 73   nBlock;.    ass
89c60 65 72 74 28 20 6e 65 77 69 20 3e 20 6d 65 6d 33  ert( newi > mem3
89c70 2e 69 4d 61 73 74 65 72 2b 31 20 29 3b 0a 20 20  .iMaster+1 );.  
89c80 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d    mem3.aPool[mem
89c90 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73  3.iMaster+mem3.s
89ca0 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72  zMaster-1].u.hdr
89cb0 2e 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c 6f  .prevSize = nBlo
89cc0 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f  ck;.    mem3.aPo
89cd0 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b  ol[mem3.iMaster+
89ce0 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d  mem3.szMaster-1]
89cf0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d  .u.hdr.size4x |=
89d00 20 32 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f   2;.    mem3.aPo
89d10 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64 72  ol[newi-1].u.hdr
89d20 2e 73 69 7a 65 34 78 20 3d 20 6e 42 6c 6f 63 6b  .size4x = nBlock
89d30 2a 34 20 2b 20 31 3b 0a 20 20 20 20 6d 65 6d 33  *4 + 1;.    mem3
89d40 2e 73 7a 4d 61 73 74 65 72 20 2d 3d 20 6e 42 6c  .szMaster -= nBl
89d50 6f 63 6b 3b 0a 20 20 20 20 6d 65 6d 33 2e 61 50  ock;.    mem3.aP
89d60 6f 6f 6c 5b 6e 65 77 69 2d 31 5d 2e 75 2e 68 64  ool[newi-1].u.hd
89d70 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65 6d  r.prevSize = mem
89d80 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20  3.szMaster;.    
89d90 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d  x = mem3.aPool[m
89da0 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75  em3.iMaster-1].u
89db0 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32 3b  .hdr.size4x & 2;
89dc0 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  .    mem3.aPool[
89dd0 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e  mem3.iMaster-1].
89de0 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d  u.hdr.size4x = m
89df0 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c  em3.szMaster*4 |
89e00 20 78 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33   x;.    if( mem3
89e10 2e 73 7a 4d 61 73 74 65 72 20 3c 20 6d 65 6d 33  .szMaster < mem3
89e20 2e 6d 6e 4d 61 73 74 65 72 20 29 7b 0a 20 20 20  .mnMaster ){.   
89e30 20 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72     mem3.mnMaster
89e40 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72   = mem3.szMaster
89e50 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
89e60 72 6e 20 28 76 6f 69 64 2a 29 26 6d 65 6d 33 2e  rn (void*)&mem3.
89e70 61 50 6f 6f 6c 5b 6e 65 77 69 5d 3b 0a 20 20 7d  aPool[newi];.  }
89e80 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 2a 70 52 6f 6f 74  .}../*.** *pRoot
89e90 20 69 73 20 74 68 65 20 68 65 61 64 20 6f 66 20   is the head of 
89ea0 61 20 6c 69 73 74 20 6f 66 20 66 72 65 65 20 63  a list of free c
89eb0 68 75 6e 6b 73 20 6f 66 20 74 68 65 20 73 61 6d  hunks of the sam
89ec0 65 20 73 69 7a 65 0a 2a 2a 20 6f 72 20 73 61 6d  e size.** or sam
89ed0 65 20 73 69 7a 65 20 68 61 73 68 2e 20 20 49 6e  e size hash.  In
89ee0 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 2a 70   other words, *p
89ef0 52 6f 6f 74 20 69 73 20 61 6e 20 65 6e 74 72 79  Root is an entry
89f00 20 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 6d 65   in either.** me
89f10 6d 33 2e 61 69 53 6d 61 6c 6c 5b 5d 20 6f 72 20  m3.aiSmall[] or 
89f20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 5d 2e 20 20  mem3.aiHash[].  
89f30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
89f40 69 6e 65 20 65 78 61 6d 69 6e 65 73 20 61 6c 6c  ine examines all
89f50 20 65 6e 74 72 69 65 73 20 6f 6e 20 74 68 65 20   entries on the 
89f60 67 69 76 65 6e 20 6c 69 73 74 20 61 6e 64 20 74  given list and t
89f70 72 69 65 73 0a 2a 2a 20 74 6f 20 63 6f 61 6c 65  ries.** to coale
89f80 73 63 65 20 65 61 63 68 20 65 6e 74 72 69 65 73  sce each entries
89f90 20 77 69 74 68 20 61 64 6a 61 63 65 6e 74 20 66   with adjacent f
89fa0 72 65 65 20 63 68 75 6e 6b 73 2e 20 20 0a 2a 2a  ree chunks.  .**
89fb0 0a 2a 2a 20 49 66 20 69 74 20 73 65 65 73 20 61  .** If it sees a
89fc0 20 63 68 75 6e 6b 20 74 68 61 74 20 69 73 20 6c   chunk that is l
89fd0 61 72 67 65 72 20 74 68 61 6e 20 6d 65 6d 33 2e  arger than mem3.
89fe0 69 4d 61 73 74 65 72 2c 20 69 74 20 72 65 70 6c  iMaster, it repl
89ff0 61 63 65 73 20 0a 2a 2a 20 74 68 65 20 63 75 72  aces .** the cur
8a000 72 65 6e 74 20 6d 65 6d 33 2e 69 4d 61 73 74 65  rent mem3.iMaste
8a010 72 20 77 69 74 68 20 74 68 65 20 6e 65 77 20 6c  r with the new l
8a020 61 72 67 65 72 20 63 68 75 6e 6b 2e 20 20 49 6e  arger chunk.  In
8a030 20 6f 72 64 65 72 20 66 6f 72 0a 2a 2a 20 74 68   order for.** th
8a040 69 73 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20  is mem3.iMaster 
8a050 72 65 70 6c 61 63 65 6d 65 6e 74 20 74 6f 20 77  replacement to w
8a060 6f 72 6b 2c 20 74 68 65 20 6d 61 73 74 65 72 20  ork, the master 
8a070 63 68 75 6e 6b 20 6d 75 73 74 20 62 65 0a 2a 2a  chunk must be.**
8a080 20 6c 69 6e 6b 65 64 20 69 6e 74 6f 20 74 68 65   linked into the
8a090 20 68 61 73 68 20 74 61 62 6c 65 73 2e 20 20 54   hash tables.  T
8a0a0 68 61 74 20 69 73 20 6e 6f 74 20 74 68 65 20 6e  hat is not the n
8a0b0 6f 72 6d 61 6c 20 73 74 61 74 65 20 6f 66 0a 2a  ormal state of.*
8a0c0 2a 20 61 66 66 61 69 72 73 2c 20 6f 66 20 63 6f  * affairs, of co
8a0d0 75 72 73 65 2e 20 20 54 68 65 20 63 61 6c 6c 69  urse.  The calli
8a0e0 6e 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20  ng routine must 
8a0f0 6c 69 6e 6b 20 74 68 65 20 6d 61 73 74 65 72 0a  link the master.
8a100 2a 2a 20 63 68 75 6e 6b 20 62 65 66 6f 72 65 20  ** chunk before 
8a110 69 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f  invoking this ro
8a120 75 74 69 6e 65 2c 20 74 68 65 6e 20 6d 75 73 74  utine, then must
8a130 20 75 6e 6c 69 6e 6b 20 74 68 65 20 28 70 6f 73   unlink the (pos
8a140 73 69 62 6c 79 0a 2a 2a 20 63 68 61 6e 67 65 64  sibly.** changed
8a150 29 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 6f  ) master chunk o
8a160 6e 63 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65  nce this routine
8a170 20 68 61 73 20 66 69 6e 69 73 68 65 64 2e 0a 2a   has finished..*
8a180 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65  /.static void me
8a190 6d 73 79 73 33 4d 65 72 67 65 28 75 33 32 20 2a  msys3Merge(u32 *
8a1a0 70 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 69 4e  pRoot){.  u32 iN
8a1b0 65 78 74 2c 20 70 72 65 76 2c 20 73 69 7a 65 2c  ext, prev, size,
8a1c0 20 69 2c 20 78 3b 0a 0a 20 20 61 73 73 65 72 74   i, x;..  assert
8a1d0 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
8a1e0 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29  held(mem3.mutex)
8a1f0 20 29 3b 0a 20 20 66 6f 72 28 69 3d 2a 70 52 6f   );.  for(i=*pRo
8a200 6f 74 3b 20 69 3e 30 3b 20 69 3d 69 4e 65 78 74  ot; i>0; i=iNext
8a210 29 7b 0a 20 20 20 20 69 4e 65 78 74 20 3d 20 6d  ){.    iNext = m
8a220 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c  em3.aPool[i].u.l
8a230 69 73 74 2e 6e 65 78 74 3b 0a 20 20 20 20 73 69  ist.next;.    si
8a240 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  ze = mem3.aPool[
8a250 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  i-1].u.hdr.size4
8a260 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  x;.    assert( (
8a270 73 69 7a 65 26 31 29 3d 3d 30 20 29 3b 0a 20 20  size&1)==0 );.  
8a280 20 20 69 66 28 20 28 73 69 7a 65 26 32 29 3d 3d    if( (size&2)==
8a290 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79  0 ){.      memsy
8a2a0 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74  s3UnlinkFromList
8a2b0 28 69 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20  (i, pRoot);.    
8a2c0 20 20 61 73 73 65 72 74 28 20 69 20 3e 20 6d 65    assert( i > me
8a2d0 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e  m3.aPool[i-1].u.
8a2e0 68 64 72 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a  hdr.prevSize );.
8a2f0 20 20 20 20 20 20 70 72 65 76 20 3d 20 69 20 2d        prev = i -
8a300 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d   mem3.aPool[i-1]
8a310 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b  .u.hdr.prevSize;
8a320 0a 20 20 20 20 20 20 69 66 28 20 70 72 65 76 3d  .      if( prev=
8a330 3d 69 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20  =iNext ){.      
8a340 20 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61    iNext = mem3.a
8a350 50 6f 6f 6c 5b 70 72 65 76 5d 2e 75 2e 6c 69 73  Pool[prev].u.lis
8a360 74 2e 6e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a  t.next;.      }.
8a370 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c        memsys3Unl
8a380 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20 20  ink(prev);.     
8a390 20 73 69 7a 65 20 3d 20 69 20 2b 20 73 69 7a 65   size = i + size
8a3a0 2f 34 20 2d 20 70 72 65 76 3b 0a 20 20 20 20 20  /4 - prev;.     
8a3b0 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b   x = mem3.aPool[
8a3c0 70 72 65 76 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  prev-1].u.hdr.si
8a3d0 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20  ze4x & 2;.      
8a3e0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d  mem3.aPool[prev-
8a3f0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20  1].u.hdr.size4x 
8a400 3d 20 73 69 7a 65 2a 34 20 7c 20 78 3b 0a 20 20  = size*4 | x;.  
8a410 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70      mem3.aPool[p
8a420 72 65 76 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64  rev+size-1].u.hd
8a430 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 73 69 7a  r.prevSize = siz
8a440 65 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33  e;.      memsys3
8a450 4c 69 6e 6b 28 70 72 65 76 29 3b 0a 20 20 20 20  Link(prev);.    
8a460 20 20 69 20 3d 20 70 72 65 76 3b 0a 20 20 20 20    i = prev;.    
8a470 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 69 7a  }else{.      siz
8a480 65 20 2f 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 20  e /= 4;.    }.  
8a490 20 20 69 66 28 20 73 69 7a 65 3e 6d 65 6d 33 2e    if( size>mem3.
8a4a0 73 7a 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20  szMaster ){.    
8a4b0 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d    mem3.iMaster =
8a4c0 20 69 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73   i;.      mem3.s
8a4d0 7a 4d 61 73 74 65 72 20 3d 20 73 69 7a 65 3b 0a  zMaster = size;.
8a4e0 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
8a4f0 2a 2a 20 52 65 74 75 72 6e 20 61 20 62 6c 6f 63  ** Return a bloc
8a500 6b 20 6f 66 20 6d 65 6d 6f 72 79 20 6f 66 20 61  k of memory of a
8a510 74 20 6c 65 61 73 74 20 6e 42 79 74 65 73 20 69  t least nBytes i
8a520 6e 20 73 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72  n size..** Retur
8a530 6e 20 4e 55 4c 4c 20 69 66 20 75 6e 61 62 6c 65  n NULL if unable
8a540 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
8a550 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68  ction assumes th
8a560 61 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79  at the necessary
8a570 20 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79   mutexes, if any
8a580 2c 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79  , are.** already
8a590 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c   held by the cal
8a5a0 6c 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61  ler. Hence "Unsa
8a5b0 66 65 22 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  fe"..*/.static v
8a5c0 6f 69 64 20 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c  oid *memsys3Mall
8a5d0 6f 63 55 6e 73 61 66 65 28 69 6e 74 20 6e 42 79  ocUnsafe(int nBy
8a5e0 74 65 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20 20  te){.  u32 i;.  
8a5f0 75 33 32 20 6e 42 6c 6f 63 6b 3b 0a 20 20 75 33  u32 nBlock;.  u3
8a600 32 20 74 6f 46 72 65 65 3b 0a 0a 20 20 61 73 73  2 toFree;..  ass
8a610 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
8a620 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74  ex_held(mem3.mut
8a630 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
8a640 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f 63   sizeof(Mem3Bloc
8a650 6b 29 3d 3d 38 20 29 3b 0a 20 20 69 66 28 20 6e  k)==8 );.  if( n
8a660 42 79 74 65 3c 3d 31 32 20 29 7b 0a 20 20 20 20  Byte<=12 ){.    
8a670 6e 42 6c 6f 63 6b 20 3d 20 32 3b 0a 20 20 7d 65  nBlock = 2;.  }e
8a680 6c 73 65 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b 20  lse{.    nBlock 
8a690 3d 20 28 6e 42 79 74 65 20 2b 20 31 31 29 2f 38  = (nByte + 11)/8
8a6a0 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
8a6b0 6e 42 6c 6f 63 6b 3e 3d 32 20 29 3b 0a 0a 20 20  nBlock>=2 );..  
8a6c0 2f 2a 20 53 54 45 50 20 31 3a 0a 20 20 2a 2a 20  /* STEP 1:.  ** 
8a6d0 4c 6f 6f 6b 20 66 6f 72 20 61 6e 20 65 6e 74 72  Look for an entr
8a6e0 79 20 6f 66 20 74 68 65 20 63 6f 72 72 65 63 74  y of the correct
8a6f0 20 73 69 7a 65 20 69 6e 20 65 69 74 68 65 72 20   size in either 
8a700 74 68 65 20 73 6d 61 6c 6c 0a 20 20 2a 2a 20 63  the small.  ** c
8a710 68 75 6e 6b 20 74 61 62 6c 65 20 6f 72 20 69 6e  hunk table or in
8a720 20 74 68 65 20 6c 61 72 67 65 20 63 68 75 6e 6b   the large chunk
8a730 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68   hash table.  Th
8a740 69 73 20 69 73 0a 20 20 2a 2a 20 73 75 63 63 65  is is.  ** succe
8a750 73 73 66 75 6c 20 6d 6f 73 74 20 6f 66 20 74 68  ssful most of th
8a760 65 20 74 69 6d 65 20 28 61 62 6f 75 74 20 39 20  e time (about 9 
8a770 74 69 6d 65 73 20 6f 75 74 20 6f 66 20 31 30 29  times out of 10)
8a780 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42 6c  ..  */.  if( nBl
8a790 6f 63 6b 20 3c 3d 20 4d 58 5f 53 4d 41 4c 4c 20  ock <= MX_SMALL 
8a7a0 29 7b 0a 20 20 20 20 69 20 3d 20 6d 65 6d 33 2e  ){.    i = mem3.
8a7b0 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d 32  aiSmall[nBlock-2
8a7c0 5d 3b 0a 20 20 20 20 69 66 28 20 69 3e 30 20 29  ];.    if( i>0 )
8a7d0 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55  {.      memsys3U
8a7e0 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c  nlinkFromList(i,
8a7f0 20 26 6d 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 6e   &mem3.aiSmall[n
8a800 42 6c 6f 63 6b 2d 32 5d 29 3b 0a 20 20 20 20 20  Block-2]);.     
8a810 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 43   return memsys3C
8a820 68 65 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63  heckout(i, nBloc
8a830 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  k);.    }.  }els
8a840 65 7b 0a 20 20 20 20 69 6e 74 20 68 61 73 68 20  e{.    int hash 
8a850 3d 20 6e 42 6c 6f 63 6b 20 25 20 4e 5f 48 41 53  = nBlock % N_HAS
8a860 48 3b 0a 20 20 20 20 66 6f 72 28 69 3d 6d 65 6d  H;.    for(i=mem
8a870 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 3b 20  3.aiHash[hash]; 
8a880 69 3e 30 3b 20 69 3d 6d 65 6d 33 2e 61 50 6f 6f  i>0; i=mem3.aPoo
8a890 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74  l[i].u.list.next
8a8a0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d  ){.      if( mem
8a8b0 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68  3.aPool[i-1].u.h
8a8c0 64 72 2e 73 69 7a 65 34 78 2f 34 3d 3d 6e 42 6c  dr.size4x/4==nBl
8a8d0 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  ock ){.        m
8a8e0 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d  emsys3UnlinkFrom
8a8f0 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69  List(i, &mem3.ai
8a900 48 61 73 68 5b 68 61 73 68 5d 29 3b 0a 20 20 20  Hash[hash]);.   
8a910 20 20 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73       return mems
8a920 79 73 33 43 68 65 63 6b 6f 75 74 28 69 2c 20 6e  ys3Checkout(i, n
8a930 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a  Block);.      }.
8a940 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
8a950 53 54 45 50 20 32 3a 0a 20 20 2a 2a 20 54 72 79  STEP 2:.  ** Try
8a960 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 20   to satisfy the 
8a970 61 6c 6c 6f 63 61 74 69 6f 6e 20 62 79 20 63 61  allocation by ca
8a980 72 76 69 6e 67 20 61 20 70 69 65 63 65 20 6f 66  rving a piece of
8a990 66 20 6f 66 20 74 68 65 20 65 6e 64 0a 20 20 2a  f of the end.  *
8a9a0 2a 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20  * of the master 
8a9b0 63 68 75 6e 6b 2e 20 20 54 68 69 73 20 73 74 65  chunk.  This ste
8a9c0 70 20 75 73 75 61 6c 6c 79 20 77 6f 72 6b 73 20  p usually works 
8a9d0 69 66 20 73 74 65 70 20 31 20 66 61 69 6c 73 2e  if step 1 fails.
8a9e0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33  .  */.  if( mem3
8a9f0 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63  .szMaster>=nBloc
8aa00 6b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  k ){.    return 
8aa10 6d 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65  memsys3FromMaste
8aa20 72 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a  r(nBlock);.  }..
8aa30 0a 20 20 2f 2a 20 53 54 45 50 20 33 3a 20 20 0a  .  /* STEP 3:  .
8aa40 20 20 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67    ** Loop throug
8aa50 68 20 74 68 65 20 65 6e 74 69 72 65 20 6d 65 6d  h the entire mem
8aa60 6f 72 79 20 70 6f 6f 6c 2e 20 20 43 6f 61 6c 65  ory pool.  Coale
8aa70 73 63 65 20 61 64 6a 61 63 65 6e 74 20 66 72 65  sce adjacent fre
8aa80 65 0a 20 20 2a 2a 20 63 68 75 6e 6b 73 2e 20 20  e.  ** chunks.  
8aa90 52 65 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61  Recompute the ma
8aaa0 73 74 65 72 20 63 68 75 6e 6b 20 61 73 20 74 68  ster chunk as th
8aab0 65 20 6c 61 72 67 65 73 74 20 66 72 65 65 20 63  e largest free c
8aac0 68 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68 65 6e 20  hunk..  ** Then 
8aad0 74 72 79 20 61 67 61 69 6e 20 74 6f 20 73 61 74  try again to sat
8aae0 69 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61 74  isfy the allocat
8aaf0 69 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20 61  ion by carving a
8ab00 20 70 69 65 63 65 20 6f 66 66 0a 20 20 2a 2a 20   piece off.  ** 
8ab10 6f 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  of the end of th
8ab20 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e 20  e master chunk. 
8ab30 20 54 68 69 73 20 73 74 65 70 20 68 61 70 70 65   This step happe
8ab40 6e 73 20 76 65 72 79 0a 20 20 2a 2a 20 72 61 72  ns very.  ** rar
8ab50 65 6c 79 20 28 77 65 20 68 6f 70 65 21 29 0a 20  ely (we hope!). 
8ab60 20 2a 2f 0a 20 20 66 6f 72 28 74 6f 46 72 65 65   */.  for(toFree
8ab70 3d 6e 42 6c 6f 63 6b 2a 31 36 3b 20 74 6f 46 72  =nBlock*16; toFr
8ab80 65 65 3c 28 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 31  ee<(mem3.nPool*1
8ab90 36 29 3b 20 74 6f 46 72 65 65 20 2a 3d 20 32 29  6); toFree *= 2)
8aba0 7b 0a 20 20 20 20 6d 65 6d 73 79 73 33 4f 75 74  {.    memsys3Out
8abb0 4f 66 4d 65 6d 6f 72 79 28 74 6f 46 72 65 65 29  OfMemory(toFree)
8abc0 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 69  ;.    if( mem3.i
8abd0 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20  Master ){.      
8abe0 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 6d 65 6d 33  memsys3Link(mem3
8abf0 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20  .iMaster);.     
8ac00 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3d 20   mem3.iMaster = 
8ac10 30 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a  0;.      mem3.sz
8ac20 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20  Master = 0;.    
8ac30 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  }.    for(i=0; i
8ac40 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b 29 7b 0a 20  <N_HASH; i++){. 
8ac50 20 20 20 20 20 6d 65 6d 73 79 73 33 4d 65 72 67       memsys3Merg
8ac60 65 28 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69  e(&mem3.aiHash[i
8ac70 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f  ]);.    }.    fo
8ac80 72 28 69 3d 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c  r(i=0; i<MX_SMAL
8ac90 4c 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  L-1; i++){.     
8aca0 20 6d 65 6d 73 79 73 33 4d 65 72 67 65 28 26 6d   memsys3Merge(&m
8acb0 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 29 3b  em3.aiSmall[i]);
8acc0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d  .    }.    if( m
8acd0 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20 29 7b 0a  em3.szMaster ){.
8ace0 20 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c        memsys3Unl
8acf0 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ink(mem3.iMaster
8ad00 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d  );.      if( mem
8ad10 33 2e 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f  3.szMaster>=nBlo
8ad20 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  ck ){.        re
8ad30 74 75 72 6e 20 6d 65 6d 73 79 73 33 46 72 6f 6d  turn memsys3From
8ad40 4d 61 73 74 65 72 28 6e 42 6c 6f 63 6b 29 3b 0a  Master(nBlock);.
8ad50 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
8ad60 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 6e 65 20  }..  /* If none 
8ad70 6f 66 20 74 68 65 20 61 62 6f 76 65 20 77 6f 72  of the above wor
8ad80 6b 65 64 2c 20 74 68 65 6e 20 77 65 20 66 61 69  ked, then we fai
8ad90 6c 2e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30  l. */.  return 0
8ada0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  ;.}../*.** Free 
8adb0 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6d  an outstanding m
8adc0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
8add0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
8ade0 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68  ction assumes th
8adf0 61 74 20 74 68 65 20 6e 65 63 65 73 73 61 72 79  at the necessary
8ae00 20 6d 75 74 65 78 65 73 2c 20 69 66 20 61 6e 79   mutexes, if any
8ae10 2c 20 61 72 65 0a 2a 2a 20 61 6c 72 65 61 64 79  , are.** already
8ae20 20 68 65 6c 64 20 62 79 20 74 68 65 20 63 61 6c   held by the cal
8ae30 6c 65 72 2e 20 48 65 6e 63 65 20 22 55 6e 73 61  ler. Hence "Unsa
8ae40 66 65 22 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d  fe"..*/.void mem
8ae50 73 79 73 33 46 72 65 65 55 6e 73 61 66 65 28 76  sys3FreeUnsafe(v
8ae60 6f 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 4d 65  oid *pOld){.  Me
8ae70 6d 33 42 6c 6f 63 6b 20 2a 70 20 3d 20 28 4d 65  m3Block *p = (Me
8ae80 6d 33 42 6c 6f 63 6b 2a 29 70 4f 6c 64 3b 0a 20  m3Block*)pOld;. 
8ae90 20 69 6e 74 20 69 3b 0a 20 20 75 33 32 20 73 69   int i;.  u32 si
8aea0 7a 65 2c 20 78 3b 0a 20 20 61 73 73 65 72 74 28  ze, x;.  assert(
8aeb0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
8aec0 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20  eld(mem3.mutex) 
8aed0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 3e 6d  );.  assert( p>m
8aee0 65 6d 33 2e 61 50 6f 6f 6c 20 26 26 20 70 3c 26  em3.aPool && p<&
8aef0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e  mem3.aPool[mem3.
8af00 6e 50 6f 6f 6c 5d 20 29 3b 0a 20 20 69 20 3d 20  nPool] );.  i = 
8af10 70 20 2d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 3b 0a  p - mem3.aPool;.
8af20 20 20 61 73 73 65 72 74 28 20 28 6d 65 6d 33 2e    assert( (mem3.
8af30 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72  aPool[i-1].u.hdr
8af40 2e 73 69 7a 65 34 78 26 31 29 3d 3d 31 20 29 3b  .size4x&1)==1 );
8af50 0a 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61  .  size = mem3.a
8af60 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e  Pool[i-1].u.hdr.
8af70 73 69 7a 65 34 78 2f 34 3b 0a 20 20 61 73 73 65  size4x/4;.  asse
8af80 72 74 28 20 69 2b 73 69 7a 65 3c 3d 6d 65 6d 33  rt( i+size<=mem3
8af90 2e 6e 50 6f 6f 6c 2b 31 20 29 3b 0a 20 20 6d 65  .nPool+1 );.  me
8afa0 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e  m3.aPool[i-1].u.
8afb0 68 64 72 2e 73 69 7a 65 34 78 20 26 3d 20 7e 31  hdr.size4x &= ~1
8afc0 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  ;.  mem3.aPool[i
8afd0 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70  +size-1].u.hdr.p
8afe0 72 65 76 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a  revSize = size;.
8aff0 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73    mem3.aPool[i+s
8b000 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  ize-1].u.hdr.siz
8b010 65 34 78 20 26 3d 20 7e 32 3b 0a 20 20 6d 65 6d  e4x &= ~2;.  mem
8b020 73 79 73 33 4c 69 6e 6b 28 69 29 3b 0a 0a 20 20  sys3Link(i);..  
8b030 2f 2a 20 54 72 79 20 74 6f 20 65 78 70 61 6e 64  /* Try to expand
8b040 20 74 68 65 20 6d 61 73 74 65 72 20 75 73 69 6e   the master usin
8b050 67 20 74 68 65 20 6e 65 77 6c 79 20 66 72 65 65  g the newly free
8b060 64 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 69 66 28  d chunk */.  if(
8b070 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 29 7b   mem3.iMaster ){
8b080 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65 6d  .    while( (mem
8b090 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61  3.aPool[mem3.iMa
8b0a0 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ster-1].u.hdr.si
8b0b0 7a 65 34 78 26 32 29 3d 3d 30 20 29 7b 0a 20 20  ze4x&2)==0 ){.  
8b0c0 20 20 20 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e      size = mem3.
8b0d0 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74  aPool[mem3.iMast
8b0e0 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76  er-1].u.hdr.prev
8b0f0 53 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65 6d 33  Size;.      mem3
8b100 2e 69 4d 61 73 74 65 72 20 2d 3d 20 73 69 7a 65  .iMaster -= size
8b110 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73 7a 4d  ;.      mem3.szM
8b120 61 73 74 65 72 20 2b 3d 20 73 69 7a 65 3b 0a 20  aster += size;. 
8b130 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69       memsys3Unli
8b140 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 29  nk(mem3.iMaster)
8b150 3b 0a 20 20 20 20 20 20 78 20 3d 20 6d 65 6d 33  ;.      x = mem3
8b160 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73  .aPool[mem3.iMas
8b170 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  ter-1].u.hdr.siz
8b180 65 34 78 20 26 20 32 3b 0a 20 20 20 20 20 20 6d  e4x & 2;.      m
8b190 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69  em3.aPool[mem3.i
8b1a0 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e  Master-1].u.hdr.
8b1b0 73 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a  size4x = mem3.sz
8b1c0 4d 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20  Master*4 | x;.  
8b1d0 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d      mem3.aPool[m
8b1e0 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33  em3.iMaster+mem3
8b1f0 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68  .szMaster-1].u.h
8b200 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d 65  dr.prevSize = me
8b210 6d 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20  m3.szMaster;.   
8b220 20 7d 0a 20 20 20 20 78 20 3d 20 6d 65 6d 33 2e   }.    x = mem3.
8b230 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74  aPool[mem3.iMast
8b240 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  er-1].u.hdr.size
8b250 34 78 20 26 20 32 3b 0a 20 20 20 20 77 68 69 6c  4x & 2;.    whil
8b260 65 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d  e( (mem3.aPool[m
8b270 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33  em3.iMaster+mem3
8b280 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68  .szMaster-1].u.h
8b290 64 72 2e 73 69 7a 65 34 78 26 31 29 3d 3d 30 20  dr.size4x&1)==0 
8b2a0 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 33  ){.      memsys3
8b2b0 55 6e 6c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73  Unlink(mem3.iMas
8b2c0 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  ter+mem3.szMaste
8b2d0 72 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 73  r);.      mem3.s
8b2e0 7a 4d 61 73 74 65 72 20 2b 3d 20 6d 65 6d 33 2e  zMaster += mem3.
8b2f0 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74  aPool[mem3.iMast
8b300 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72  er+mem3.szMaster
8b310 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
8b320 2f 34 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61  /4;.      mem3.a
8b330 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65  Pool[mem3.iMaste
8b340 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  r-1].u.hdr.size4
8b350 78 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  x = mem3.szMaste
8b360 72 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d  r*4 | x;.      m
8b370 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69  em3.aPool[mem3.i
8b380 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61  Master+mem3.szMa
8b390 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72  ster-1].u.hdr.pr
8b3a0 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a  evSize = mem3.sz
8b3b0 4d 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20  Master;.    }.  
8b3c0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  }.}../*.** Retur
8b3d0 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61 6e  n the size of an
8b3e0 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 6c 6c   outstanding all
8b3f0 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 62 79 74 65  ocation, in byte
8b400 73 2e 20 20 54 68 65 0a 2a 2a 20 73 69 7a 65 20  s.  The.** size 
8b410 72 65 74 75 72 6e 65 64 20 6f 6d 69 74 73 20 74  returned omits t
8b420 68 65 20 38 2d 62 79 74 65 20 68 65 61 64 65 72  he 8-byte header
8b430 20 6f 76 65 72 68 65 61 64 2e 20 20 54 68 69 73   overhead.  This
8b440 20 6f 6e 6c 79 0a 2a 2a 20 77 6f 72 6b 73 20 66   only.** works f
8b450 6f 72 20 63 68 75 6e 6b 73 20 74 68 61 74 20 61  or chunks that a
8b460 72 65 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65  re currently che
8b470 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 73 74 61  cked out..*/.sta
8b480 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 53  tic int memsys3S
8b490 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20  ize(void *p){.  
8b4a0 4d 65 6d 33 42 6c 6f 63 6b 20 2a 70 42 6c 6f 63  Mem3Block *pBloc
8b4b0 6b 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  k;.  if( p==0 ) 
8b4c0 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 42 6c 6f  return 0;.  pBlo
8b4d0 63 6b 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 2a  ck = (Mem3Block*
8b4e0 29 70 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70  )p;.  assert( (p
8b4f0 42 6c 6f 63 6b 5b 2d 31 5d 2e 75 2e 68 64 72 2e  Block[-1].u.hdr.
8b500 73 69 7a 65 34 78 26 31 29 21 3d 30 20 29 3b 0a  size4x&1)!=0 );.
8b510 20 20 72 65 74 75 72 6e 20 28 70 42 6c 6f 63 6b    return (pBlock
8b520 5b 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  [-1].u.hdr.size4
8b530 78 26 7e 33 29 2a 32 20 2d 20 34 3b 0a 7d 0a 0a  x&~3)*2 - 4;.}..
8b540 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61  /*.** Round up a
8b550 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74 6f   request size to
8b560 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64 20   the next valid 
8b570 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e  allocation size.
8b580 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
8b590 65 6d 73 79 73 33 52 6f 75 6e 64 75 70 28 69 6e  emsys3Roundup(in
8b5a0 74 20 6e 29 7b 0a 20 20 69 66 28 20 6e 3c 3d 31  t n){.  if( n<=1
8b5b0 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  2 ){.    return 
8b5c0 31 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  12;.  }else{.   
8b5d0 20 72 65 74 75 72 6e 20 28 28 6e 2b 31 31 29 26   return ((n+11)&
8b5e0 7e 37 29 20 2d 20 34 3b 0a 20 20 7d 0a 7d 0a 0a  ~7) - 4;.  }.}..
8b5f0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6e  /*.** Allocate n
8b600 42 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e  Bytes of memory.
8b610 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
8b620 2a 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 28 69  *memsys3Malloc(i
8b630 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 73 71  nt nBytes){.  sq
8b640 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b 0a  lite3_int64 *p;.
8b650 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 73    assert( nBytes
8b660 3e 30 20 29 3b 20 20 20 20 20 20 20 20 20 20 2f  >0 );          /
8b670 2a 20 6d 61 6c 6c 6f 63 2e 63 20 66 69 6c 74 65  * malloc.c filte
8b680 72 73 20 6f 75 74 20 30 20 62 79 74 65 20 72 65  rs out 0 byte re
8b690 71 75 65 73 74 73 20 2a 2f 0a 20 20 6d 65 6d 73  quests */.  mems
8b6a0 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20  ys3Enter();.  p 
8b6b0 3d 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55  = memsys3MallocU
8b6c0 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20  nsafe(nBytes);. 
8b6d0 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28 29 3b   memsys3Leave();
8b6e0 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a  .  return (void*
8b6f0 29 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72  )p; .}../*.** Fr
8b700 65 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 76 6f  ee memory..*/.vo
8b710 69 64 20 6d 65 6d 73 79 73 33 46 72 65 65 28 76  id memsys3Free(v
8b720 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20  oid *pPrior){.  
8b730 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 20 29  assert( pPrior )
8b740 3b 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 72  ;.  memsys3Enter
8b750 28 29 3b 0a 20 20 6d 65 6d 73 79 73 33 46 72 65  ();.  memsys3Fre
8b760 65 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b  eUnsafe(pPrior);
8b770 0a 20 20 6d 65 6d 73 79 73 33 4c 65 61 76 65 28  .  memsys3Leave(
8b780 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  );.}../*.** Chan
8b790 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61  ge the size of a
8b7a0 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72  n existing memor
8b7b0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a  y allocation.*/.
8b7c0 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 52 65 61  void *memsys3Rea
8b7d0 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f  lloc(void *pPrio
8b7e0 72 2c 20 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a  r, int nBytes){.
8b7f0 20 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20 20 76 6f    int nOld;.  vo
8b800 69 64 20 2a 70 3b 0a 20 20 69 66 28 20 70 50 72  id *p;.  if( pPr
8b810 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ior==0 ){.    re
8b820 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 6d 61 6c  turn sqlite3_mal
8b830 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d  loc(nBytes);.  }
8b840 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30  .  if( nBytes<=0
8b850 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
8b860 66 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20 20  free(pPrior);.  
8b870 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
8b880 20 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73 33    nOld = memsys3
8b890 53 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20 20  Size(pPrior);.  
8b8a0 69 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c 64  if( nBytes<=nOld
8b8b0 20 26 26 20 6e 42 79 74 65 73 3e 3d 6e 4f 6c 64   && nBytes>=nOld
8b8c0 2d 31 32 38 20 29 7b 0a 20 20 20 20 72 65 74 75  -128 ){.    retu
8b8d0 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20  rn pPrior;.  }. 
8b8e0 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29 3b   memsys3Enter();
8b8f0 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 33 4d 61  .  p = memsys3Ma
8b900 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 65  llocUnsafe(nByte
8b910 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  s);.  if( p ){. 
8b920 20 20 20 69 66 28 20 6e 4f 6c 64 3c 6e 42 79 74     if( nOld<nByt
8b930 65 73 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63  es ){.      memc
8b940 70 79 28 70 2c 20 70 50 72 69 6f 72 2c 20 6e 4f  py(p, pPrior, nO
8b950 6c 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ld);.    }else{.
8b960 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20        memcpy(p, 
8b970 70 50 72 69 6f 72 2c 20 6e 42 79 74 65 73 29 3b  pPrior, nBytes);
8b980 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 79  .    }.    memsy
8b990 73 33 46 72 65 65 55 6e 73 61 66 65 28 70 50 72  s3FreeUnsafe(pPr
8b9a0 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  ior);.  }.  mems
8b9b0 79 73 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65  ys3Leave();.  re
8b9c0 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn p;.}../*.**
8b9d0 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73   Initialize this
8b9e0 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74   module..*/.stat
8b9f0 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33 49 6e  ic int memsys3In
8ba00 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64  it(void *NotUsed
8ba10 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
8ba20 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
8ba30 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c    if( !sqlite3Gl
8ba40 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70  obalConfig.pHeap
8ba50 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
8ba60 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
8ba70 0a 0a 20 20 2f 2a 20 53 74 6f 72 65 20 61 20 70  ..  /* Store a p
8ba80 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65  ointer to the me
8ba90 6d 6f 72 79 20 62 6c 6f 63 6b 20 69 6e 20 67 6c  mory block in gl
8baa0 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 20 6d  obal structure m
8bab0 65 6d 33 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74  em3. */.  assert
8bac0 28 20 73 69 7a 65 6f 66 28 4d 65 6d 33 42 6c 6f  ( sizeof(Mem3Blo
8bad0 63 6b 29 3d 3d 38 20 29 3b 0a 20 20 6d 65 6d 33  ck)==8 );.  mem3
8bae0 2e 61 50 6f 6f 6c 20 3d 20 28 4d 65 6d 33 42 6c  .aPool = (Mem3Bl
8baf0 6f 63 6b 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f  ock *)sqlite3Glo
8bb00 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b  balConfig.pHeap;
8bb10 0a 20 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 20 3d 20  .  mem3.nPool = 
8bb20 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  (sqlite3GlobalCo
8bb30 6e 66 69 67 2e 6e 48 65 61 70 20 2f 20 73 69 7a  nfig.nHeap / siz
8bb40 65 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 29 20  eof(Mem3Block)) 
8bb50 2d 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69  - 2;..  /* Initi
8bb60 61 6c 69 7a 65 20 74 68 65 20 6d 61 73 74 65 72  alize the master
8bb70 20 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6d 65 6d   block. */.  mem
8bb80 33 2e 73 7a 4d 61 73 74 65 72 20 3d 20 6d 65 6d  3.szMaster = mem
8bb90 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e  3.nPool;.  mem3.
8bba0 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d 33 2e  mnMaster = mem3.
8bbb0 73 7a 4d 61 73 74 65 72 3b 0a 20 20 6d 65 6d 33  szMaster;.  mem3
8bbc0 2e 69 4d 61 73 74 65 72 20 3d 20 31 3b 0a 20 20  .iMaster = 1;.  
8bbd0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 30 5d 2e 75 2e  mem3.aPool[0].u.
8bbe0 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 28 6d 65  hdr.size4x = (me
8bbf0 6d 33 2e 73 7a 4d 61 73 74 65 72 3c 3c 32 29 20  m3.szMaster<<2) 
8bc00 2b 20 32 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f  + 2;.  mem3.aPoo
8bc10 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e  l[mem3.nPool].u.
8bc20 68 64 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6d  hdr.prevSize = m
8bc30 65 6d 33 2e 6e 50 6f 6f 6c 3b 0a 20 20 6d 65 6d  em3.nPool;.  mem
8bc40 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f  3.aPool[mem3.nPo
8bc50 6f 6c 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  ol].u.hdr.size4x
8bc60 20 3d 20 31 3b 0a 0a 20 20 72 65 74 75 72 6e 20   = 1;..  return 
8bc70 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
8bc80 0a 2a 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65  .** Deinitialize
8bc90 20 74 68 69 73 20 6d 6f 64 75 6c 65 2e 0a 2a 2f   this module..*/
8bca0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d  .static void mem
8bcb0 73 79 73 33 53 68 75 74 64 6f 77 6e 28 76 6f 69  sys3Shutdown(voi
8bcc0 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55  d *NotUsed){.  U
8bcd0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
8bce0 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65 6d 33  NotUsed);.  mem3
8bcf0 2e 6d 75 74 65 78 20 3d 20 30 3b 0a 20 20 72 65  .mutex = 0;.  re
8bd00 74 75 72 6e 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a  turn;.}..../*.**
8bd10 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 69   Open the file i
8bd20 6e 64 69 63 61 74 65 64 20 61 6e 64 20 77 72 69  ndicated and wri
8bd30 74 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c 20  te a log of all 
8bd40 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20 0a  unfreed memory .
8bd50 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 69  ** allocations i
8bd60 6e 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a 2f  nto that log..*/
8bd70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
8bd80 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 73  void sqlite3Mems
8bd90 79 73 33 44 75 6d 70 28 63 6f 6e 73 74 20 63 68  ys3Dump(const ch
8bda0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a  ar *zFilename){.
8bdb0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
8bdc0 42 55 47 0a 20 20 46 49 4c 45 20 2a 6f 75 74 3b  BUG.  FILE *out;
8bdd0 0a 20 20 75 33 32 20 69 2c 20 6a 3b 0a 20 20 75  .  u32 i, j;.  u
8bde0 33 32 20 73 69 7a 65 3b 0a 20 20 69 66 28 20 7a  32 size;.  if( z
8bdf0 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c 7c 20 7a  Filename==0 || z
8be00 46 69 6c 65 6e 61 6d 65 5b 30 5d 3d 3d 30 20 29  Filename[0]==0 )
8be10 7b 0a 20 20 20 20 6f 75 74 20 3d 20 73 74 64 6f  {.    out = stdo
8be20 75 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ut;.  }else{.   
8be30 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 46 69   out = fopen(zFi
8be40 6c 65 6e 61 6d 65 2c 20 22 77 22 29 3b 0a 20 20  lename, "w");.  
8be50 20 20 69 66 28 20 6f 75 74 3d 3d 30 20 29 7b 0a    if( out==0 ){.
8be60 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74        fprintf(st
8be70 64 65 72 72 2c 20 22 2a 2a 20 55 6e 61 62 6c 65  derr, "** Unable
8be80 20 74 6f 20 6f 75 74 70 75 74 20 6d 65 6d 6f 72   to output memor
8be90 79 20 64 65 62 75 67 20 6f 75 74 70 75 74 20 6c  y debug output l
8bea0 6f 67 3a 20 25 73 20 2a 2a 5c 6e 22 2c 0a 20 20  og: %s **\n",.  
8beb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
8bec0 20 20 20 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a      zFilename);.
8bed0 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
8bee0 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 79 73    }.  }.  memsys
8bef0 33 45 6e 74 65 72 28 29 3b 0a 20 20 66 70 72 69  3Enter();.  fpri
8bf00 6e 74 66 28 6f 75 74 2c 20 22 43 48 55 4e 4b 53  ntf(out, "CHUNKS
8bf10 3a 5c 6e 22 29 3b 0a 20 20 66 6f 72 28 69 3d 31  :\n");.  for(i=1
8bf20 3b 20 69 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c 3b  ; i<=mem3.nPool;
8bf30 20 69 2b 3d 73 69 7a 65 2f 34 29 7b 0a 20 20 20   i+=size/4){.   
8bf40 20 73 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f   size = mem3.aPo
8bf50 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ol[i-1].u.hdr.si
8bf60 7a 65 34 78 3b 0a 20 20 20 20 69 66 28 20 73 69  ze4x;.    if( si
8bf70 7a 65 2f 34 3c 3d 31 20 29 7b 0a 20 20 20 20 20  ze/4<=1 ){.     
8bf80 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25   fprintf(out, "%
8bf90 70 20 73 69 7a 65 20 65 72 72 6f 72 5c 6e 22 2c  p size error\n",
8bfa0 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29   &mem3.aPool[i])
8bfb0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
8bfc0 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  0 );.      break
8bfd0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
8bfe0 28 73 69 7a 65 26 31 29 3d 3d 30 20 26 26 20 6d  (size&1)==0 && m
8bff0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65  em3.aPool[i+size
8c000 2f 34 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76  /4-1].u.hdr.prev
8c010 53 69 7a 65 21 3d 73 69 7a 65 2f 34 20 29 7b 0a  Size!=size/4 ){.
8c020 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75        fprintf(ou
8c030 74 2c 20 22 25 70 20 74 61 69 6c 20 73 69 7a 65  t, "%p tail size
8c040 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 5c   does not match\
8c050 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  n", &mem3.aPool[
8c060 69 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  i]);.      asser
8c070 74 28 20 30 20 29 3b 0a 20 20 20 20 20 20 62 72  t( 0 );.      br
8c080 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  eak;.    }.    i
8c090 66 28 20 28 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  f( ((mem3.aPool[
8c0a0 69 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e 68 64  i+size/4-1].u.hd
8c0b0 72 2e 73 69 7a 65 34 78 26 32 29 3e 3e 31 29 21  r.size4x&2)>>1)!
8c0c0 3d 28 73 69 7a 65 26 31 29 20 29 7b 0a 20 20 20  =(size&1) ){.   
8c0d0 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20     fprintf(out, 
8c0e0 22 25 70 20 74 61 69 6c 20 63 68 65 63 6b 6f 75  "%p tail checkou
8c0f0 74 20 62 69 74 20 69 73 20 69 6e 63 6f 72 72 65  t bit is incorre
8c100 63 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f  ct\n", &mem3.aPo
8c110 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20 61 73  ol[i]);.      as
8c120 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20 20 20  sert( 0 );.     
8c130 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
8c140 20 20 69 66 28 20 73 69 7a 65 26 31 20 29 7b 0a    if( size&1 ){.
8c150 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75        fprintf(ou
8c160 74 2c 20 22 25 70 20 25 36 64 20 62 79 74 65 73  t, "%p %6d bytes
8c170 20 63 68 65 63 6b 65 64 20 6f 75 74 5c 6e 22 2c   checked out\n",
8c180 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c   &mem3.aPool[i],
8c190 20 28 73 69 7a 65 2f 34 29 2a 38 2d 38 29 3b 0a   (size/4)*8-8);.
8c1a0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
8c1b0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25   fprintf(out, "%
8c1c0 70 20 25 36 64 20 62 79 74 65 73 20 66 72 65 65  p %6d bytes free
8c1d0 25 73 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61 50 6f  %s\n", &mem3.aPo
8c1e0 6f 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f 34 29 2a  ol[i], (size/4)*
8c1f0 38 2d 38 2c 0a 20 20 20 20 20 20 20 20 20 20 20  8-8,.           
8c200 20 20 20 20 20 20 20 69 3d 3d 6d 65 6d 33 2e 69         i==mem3.i
8c210 4d 61 73 74 65 72 20 3f 20 22 20 2a 2a 6d 61 73  Master ? " **mas
8c220 74 65 72 2a 2a 22 20 3a 20 22 22 29 3b 0a 20 20  ter**" : "");.  
8c230 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d    }.  }.  for(i=
8c240 30 3b 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b  0; i<MX_SMALL-1;
8c250 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 6d   i++){.    if( m
8c260 65 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3d 3d  em3.aiSmall[i]==
8c270 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
8c280 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
8c290 73 6d 61 6c 6c 28 25 32 64 29 3a 22 2c 20 69 29  small(%2d):", i)
8c2a0 3b 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20 6d 65  ;.    for(j = me
8c2b0 6d 33 2e 61 69 53 6d 61 6c 6c 5b 69 5d 3b 20 6a  m3.aiSmall[i]; j
8c2c0 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f 6c  >0; j=mem3.aPool
8c2d0 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 29  [j].u.list.next)
8c2e0 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  {.      fprintf(
8c2f0 6f 75 74 2c 20 22 20 25 70 28 25 64 29 22 2c 20  out, " %p(%d)", 
8c300 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c 0a  &mem3.aPool[j],.
8c310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6d                (m
8c320 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e 75  em3.aPool[j-1].u
8c330 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 29 2a 38  .hdr.size4x/4)*8
8c340 2d 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  -8);.    }.    f
8c350 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22  printf(out, "\n"
8c360 29 3b 20 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  ); .  }.  for(i=
8c370 30 3b 20 69 3c 4e 5f 48 41 53 48 3b 20 69 2b 2b  0; i<N_HASH; i++
8c380 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e  ){.    if( mem3.
8c390 61 69 48 61 73 68 5b 69 5d 3d 3d 30 20 29 20 63  aiHash[i]==0 ) c
8c3a0 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 66 70 72  ontinue;.    fpr
8c3b0 69 6e 74 66 28 6f 75 74 2c 20 22 68 61 73 68 28  intf(out, "hash(
8c3c0 25 32 64 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20  %2d):", i);.    
8c3d0 66 6f 72 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 48  for(j = mem3.aiH
8c3e0 61 73 68 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d  ash[i]; j>0; j=m
8c3f0 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c  em3.aPool[j].u.l
8c400 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20  ist.next){.     
8c410 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20   fprintf(out, " 
8c420 25 70 28 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61  %p(%d)", &mem3.a
8c430 50 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20  Pool[j],.       
8c440 20 20 20 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f         (mem3.aPo
8c450 6f 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ol[j-1].u.hdr.si
8c460 7a 65 34 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20  ze4x/4)*8-8);.  
8c470 20 20 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28    }.    fprintf(
8c480 6f 75 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d  out, "\n"); .  }
8c490 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20  .  fprintf(out, 
8c4a0 22 6d 61 73 74 65 72 3d 25 64 5c 6e 22 2c 20 6d  "master=%d\n", m
8c4b0 65 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20  em3.iMaster);.  
8c4c0 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6e 6f  fprintf(out, "no
8c4d0 77 55 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d  wUsed=%d\n", mem
8c4e0 33 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33  3.nPool*8 - mem3
8c4f0 2e 73 7a 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20  .szMaster*8);.  
8c500 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 78  fprintf(out, "mx
8c510 55 73 65 64 3d 25 64 5c 6e 22 2c 20 6d 65 6d 33  Used=%d\n", mem3
8c520 2e 6e 50 6f 6f 6c 2a 38 20 2d 20 6d 65 6d 33 2e  .nPool*8 - mem3.
8c530 6d 6e 4d 61 73 74 65 72 2a 38 29 3b 0a 20 20 73  mnMaster*8);.  s
8c540 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
8c550 76 65 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a  ve(mem3.mutex);.
8c560 20 20 69 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75    if( out==stdou
8c570 74 20 29 7b 0a 20 20 20 20 66 66 6c 75 73 68 28  t ){.    fflush(
8c580 73 74 64 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65  stdout);.  }else
8c590 7b 0a 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 74  {.    fclose(out
8c5a0 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55  );.  }.#else.  U
8c5b0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
8c5c0 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64  zFilename);.#end
8c5d0 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  if.}../*.** This
8c5e0 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20   routine is the 
8c5f0 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20  only routine in 
8c600 74 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65  this file with e
8c610 78 74 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b  xternal .** link
8c620 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 6f 70 75 6c  age..**.** Popul
8c630 61 74 65 20 74 68 65 20 6c 6f 77 2d 6c 65 76 65  ate the low-leve
8c640 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  l memory allocat
8c650 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69  ion function poi
8c660 6e 74 65 72 73 20 69 6e 0a 2a 2a 20 73 71 6c 69  nters in.** sqli
8c670 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
8c680 6d 20 77 69 74 68 20 70 6f 69 6e 74 65 72 73 20  m with pointers 
8c690 74 6f 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20  to the routines 
8c6a0 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 20 54 68  in this file. Th
8c6b0 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20 73  e.** arguments s
8c6c0 70 65 63 69 66 79 20 74 68 65 20 62 6c 6f 63 6b  pecify the block
8c6d0 20 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 6d 61   of memory to ma
8c6e0 6e 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  nage..**.** This
8c6f0 20 72 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79   routine is only
8c700 20 63 61 6c 6c 65 64 20 62 79 20 73 71 6c 69 74   called by sqlit
8c710 65 33 5f 63 6f 6e 66 69 67 28 29 2c 20 61 6e 64  e3_config(), and
8c720 20 74 68 65 72 65 66 6f 72 65 0a 2a 2a 20 69 73   therefore.** is
8c730 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f   not required to
8c740 20 62 65 20 74 68 72 65 61 64 73 61 66 65 20 28   be threadsafe (
8c750 69 74 20 69 73 20 6e 6f 74 29 2e 0a 2a 2f 0a 53  it is not)..*/.S
8c760 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
8c770 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  nst sqlite3_mem_
8c780 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33  methods *sqlite3
8c790 4d 65 6d 47 65 74 4d 65 6d 73 79 73 33 28 76 6f  MemGetMemsys3(vo
8c7a0 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f  id){.  static co
8c7b0 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f  nst sqlite3_mem_
8c7c0 6d 65 74 68 6f 64 73 20 6d 65 6d 70 6f 6f 6c 4d  methods mempoolM
8c7d0 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20 20 20  ethods = {.     
8c7e0 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 2c 0a 20  memsys3Malloc,. 
8c7f0 20 20 20 20 6d 65 6d 73 79 73 33 46 72 65 65 2c      memsys3Free,
8c800 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 52 65 61  .     memsys3Rea
8c810 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73 79  lloc,.     memsy
8c820 73 33 53 69 7a 65 2c 0a 20 20 20 20 20 6d 65 6d  s3Size,.     mem
8c830 73 79 73 33 52 6f 75 6e 64 75 70 2c 0a 20 20 20  sys3Roundup,.   
8c840 20 20 6d 65 6d 73 79 73 33 49 6e 69 74 2c 0a 20    memsys3Init,. 
8c850 20 20 20 20 6d 65 6d 73 79 73 33 53 68 75 74 64      memsys3Shutd
8c860 6f 77 6e 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b  own,.     0.  };
8c870 0a 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 70 6f  .  return &mempo
8c880 6f 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 23 65  olMethods;.}..#e
8c890 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45  ndif /* SQLITE_E
8c8a0 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 2a 2f  NABLE_MEMSYS3 */
8c8b0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
8c8c0 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 33 2e 63 20  * End of mem3.c 
8c8d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c8e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c8f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c900 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
8c910 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 65 6d  * Begin file mem
8c920 35 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  5.c ************
8c930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8c950 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4f 63 74  /./*.** 2007 Oct
8c960 6f 62 65 72 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68  ober 14.**.** Th
8c970 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
8c980 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
8c990 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
8c9a0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
8c9b0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
8c9c0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
8c9d0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
8c9e0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
8c9f0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
8ca00 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
8ca10 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
8ca20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
8ca30 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
8ca40 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
8ca50 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
8ca60 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
8ca70 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
8ca80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ca90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8caa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8cac0 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
8cad0 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ile contains the
8cae0 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61   C functions tha
8caf0 74 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 6d 65  t implement a me
8cb00 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69  mory.** allocati
8cb10 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 66 6f 72  on subsystem for
8cb20 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 2e 20   use by SQLite. 
8cb30 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73  .**.** This vers
8cb40 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  ion of the memor
8cb50 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
8cb60 73 79 73 74 65 6d 20 6f 6d 69 74 73 20 61 6c 6c  system omits all
8cb70 0a 2a 2a 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f  .** use of mallo
8cb80 63 28 29 2e 20 54 68 65 20 61 70 70 6c 69 63 61  c(). The applica
8cb90 74 69 6f 6e 20 67 69 76 65 73 20 53 51 4c 69 74  tion gives SQLit
8cba0 65 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d  e a block of mem
8cbb0 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61  ory.** before ca
8cbc0 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e  lling sqlite3_in
8cbd0 69 74 69 61 6c 69 7a 65 28 29 20 66 72 6f 6d 20  itialize() from 
8cbe0 77 68 69 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e  which allocation
8cbf0 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 61 6e  s.** are made an
8cc00 64 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  d returned by th
8cc10 65 20 78 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20  e xMalloc() and 
8cc20 78 52 65 61 6c 6c 6f 63 28 29 20 0a 2a 2a 20 69  xRealloc() .** i
8cc30 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 20  mplementations. 
8cc40 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f 69 6e 69  Once sqlite3_ini
8cc50 74 69 61 6c 69 7a 65 28 29 20 68 61 73 20 62 65  tialize() has be
8cc60 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 74 68  en called,.** th
8cc70 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f  e amount of memo
8cc80 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20  ry available to 
8cc90 53 51 4c 69 74 65 20 69 73 20 66 69 78 65 64 20  SQLite is fixed 
8cca0 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a 20 62 65  and cannot.** be
8ccb0 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20   changed..**.** 
8ccc0 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20  This version of 
8ccd0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
8cce0 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20  ation subsystem 
8ccf0 69 73 20 69 6e 63 6c 75 64 65 64 0a 2a 2a 20 69  is included.** i
8cd00 6e 20 74 68 65 20 62 75 69 6c 64 20 6f 6e 6c 79  n the build only
8cd10 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   if SQLITE_ENABL
8cd20 45 5f 4d 45 4d 53 59 53 35 20 69 73 20 64 65 66  E_MEMSYS5 is def
8cd30 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ined..**.** This
8cd40 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
8cd50 72 20 75 73 65 73 20 74 68 65 20 66 6f 6c 6c 6f  r uses the follo
8cd60 77 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 3a 0a  wing algorithm:.
8cd70 2a 2a 0a 2a 2a 20 20 20 31 2e 20 20 41 6c 6c 20  **.**   1.  All 
8cd80 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
8cd90 6e 73 20 73 69 7a 65 73 20 61 72 65 20 72 6f 75  ns sizes are rou
8cda0 6e 64 65 64 20 75 70 20 74 6f 20 61 20 70 6f 77  nded up to a pow
8cdb0 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20 20  er of 2..**.**  
8cdc0 20 32 2e 20 20 49 66 20 74 77 6f 20 61 64 6a 61   2.  If two adja
8cdd0 63 65 6e 74 20 66 72 65 65 20 62 6c 6f 63 6b 73  cent free blocks
8cde0 20 61 72 65 20 74 68 65 20 68 61 6c 76 65 73 20   are the halves 
8cdf0 6f 66 20 61 20 6c 61 72 67 65 72 20 62 6c 6f 63  of a larger bloc
8ce00 6b 2c 0a 2a 2a 20 20 20 20 20 20 20 74 68 65 6e  k,.**       then
8ce10 20 74 68 65 20 74 77 6f 20 62 6c 6f 63 6b 73 20   the two blocks 
8ce20 61 72 65 20 63 6f 61 6c 65 73 65 64 20 69 6e 74  are coalesed int
8ce30 6f 20 74 68 65 20 73 69 6e 67 6c 65 20 6c 61 72  o the single lar
8ce40 67 65 72 20 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a  ger block..**.**
8ce50 20 20 20 33 2e 20 20 4e 65 77 20 6d 65 6d 6f 72     3.  New memor
8ce60 79 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  y is allocated f
8ce70 72 6f 6d 20 74 68 65 20 66 69 72 73 74 20 61 76  rom the first av
8ce80 61 69 6c 61 62 6c 65 20 66 72 65 65 20 62 6c 6f  ailable free blo
8ce90 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 61  ck..**.** This a
8cea0 6c 67 6f 72 69 74 68 6d 20 69 73 20 64 65 73 63  lgorithm is desc
8ceb0 72 69 62 65 64 20 69 6e 3a 20 4a 2e 20 4d 2e 20  ribed in: J. M. 
8cec0 52 6f 62 73 6f 6e 2e 20 22 42 6f 75 6e 64 73 20  Robson. "Bounds 
8ced0 66 6f 72 20 53 6f 6d 65 20 46 75 6e 63 74 69 6f  for Some Functio
8cee0 6e 73 0a 2a 2a 20 43 6f 6e 63 65 72 6e 69 6e 67  ns.** Concerning
8cef0 20 44 79 6e 61 6d 69 63 20 53 74 6f 72 61 67 65   Dynamic Storage
8cf00 20 41 6c 6c 6f 63 61 74 69 6f 6e 22 2e 20 4a 6f   Allocation". Jo
8cf10 75 72 6e 61 6c 20 6f 66 20 74 68 65 20 41 73 73  urnal of the Ass
8cf20 6f 63 69 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20  ociation for.** 
8cf30 43 6f 6d 70 75 74 69 6e 67 20 4d 61 63 68 69 6e  Computing Machin
8cf40 65 72 79 2c 20 56 6f 6c 75 6d 65 20 32 31 2c 20  ery, Volume 21, 
8cf50 4e 75 6d 62 65 72 20 38 2c 20 4a 75 6c 79 20 31  Number 8, July 1
8cf60 39 37 34 2c 20 70 61 67 65 73 20 34 39 31 2d 34  974, pages 491-4
8cf70 39 39 2e 0a 2a 2a 20 0a 2a 2a 20 4c 65 74 20 6e  99..** .** Let n
8cf80 20 62 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20   be the size of 
8cf90 74 68 65 20 6c 61 72 67 65 73 74 20 61 6c 6c 6f  the largest allo
8cfa0 63 61 74 69 6f 6e 20 64 69 76 69 64 65 64 20 62  cation divided b
8cfb0 79 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a 2a 2a  y the minimum.**
8cfc0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65   allocation size
8cfd0 20 28 61 66 74 65 72 20 72 6f 75 6e 64 69 6e 67   (after rounding
8cfe0 20 61 6c 6c 20 73 69 7a 65 73 20 75 70 20 74 6f   all sizes up to
8cff0 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 29 20   a power of 2.) 
8d000 20 4c 65 74 20 4d 0a 2a 2a 20 62 65 20 74 68 65   Let M.** be the
8d010 20 6d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20   maximum amount 
8d020 6f 66 20 6d 65 6d 6f 72 79 20 65 76 65 72 20 6f  of memory ever o
8d030 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e  utstanding at on
8d040 65 20 74 69 6d 65 2e 20 20 4c 65 74 0a 2a 2a 20  e time.  Let.** 
8d050 4e 20 62 65 20 74 68 65 20 74 6f 74 61 6c 20 61  N be the total a
8d060 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20  mount of memory 
8d070 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c  available for al
8d080 6c 6f 63 61 74 69 6f 6e 2e 20 20 52 6f 62 73 6f  location.  Robso
8d090 6e 0a 2a 2a 20 70 72 6f 76 65 64 20 74 68 61 74  n.** proved that
8d0a0 20 74 68 69 73 20 6d 65 6d 6f 72 79 20 61 6c 6c   this memory all
8d0b0 6f 63 61 74 6f 72 20 77 69 6c 6c 20 6e 65 76 65  ocator will neve
8d0c0 72 20 62 72 65 61 6b 64 6f 77 6e 20 64 75 65 20  r breakdown due 
8d0d0 74 6f 20 0a 2a 2a 20 66 72 61 67 6d 65 6e 74 61  to .** fragmenta
8d0e0 74 69 6f 6e 20 61 73 20 6c 6f 6e 67 20 61 73 20  tion as long as 
8d0f0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  the following co
8d100 6e 73 74 72 61 69 6e 74 20 68 6f 6c 64 73 3a 0a  nstraint holds:.
8d110 2a 2a 0a 2a 2a 20 20 20 20 20 20 4e 20 3e 3d 20  **.**      N >= 
8d120 20 4d 2a 28 31 20 2b 20 6c 6f 67 32 28 6e 29 2f   M*(1 + log2(n)/
8d130 32 29 20 2d 20 6e 20 2b 20 31 0a 2a 2a 0a 2a 2a  2) - n + 1.**.**
8d140 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 74 61   The sqlite3_sta
8d150 74 75 73 28 29 20 6c 6f 67 69 63 20 74 72 61 63  tus() logic trac
8d160 6b 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 76  ks the maximum v
8d170 61 6c 75 65 73 20 6f 66 20 6e 20 61 6e 64 20 4d  alues of n and M
8d180 20 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6e 20 61   so.** that an a
8d190 70 70 6c 69 63 61 74 69 6f 6e 20 63 61 6e 2c 20  pplication can, 
8d1a0 61 74 20 61 6e 79 20 74 69 6d 65 2c 20 76 65 72  at any time, ver
8d1b0 69 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72 61  ify this constra
8d1c0 69 6e 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  int..*/../*.** T
8d1d0 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  his version of t
8d1e0 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
8d1f0 74 6f 72 20 69 73 20 75 73 65 64 20 6f 6e 6c 79  tor is used only
8d200 20 77 68 65 6e 20 0a 2a 2a 20 53 51 4c 49 54 45   when .** SQLITE
8d210 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20  _ENABLE_MEMSYS5 
8d220 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23  is defined..*/.#
8d230 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
8d240 42 4c 45 5f 4d 45 4d 53 59 53 35 0a 0a 2f 2a 0a  BLE_MEMSYS5../*.
8d250 2a 2a 20 41 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c  ** A minimum all
8d260 6f 63 61 74 69 6f 6e 20 69 73 20 61 6e 20 69 6e  ocation is an in
8d270 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
8d280 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
8d290 65 2e 0a 2a 2a 20 4c 61 72 67 65 72 20 61 6c 6c  e..** Larger all
8d2a0 6f 63 61 74 69 6f 6e 73 20 61 72 65 20 61 6e 20  ocations are an 
8d2b0 61 72 72 61 79 20 6f 66 20 74 68 65 73 65 20 73  array of these s
8d2c0 74 72 75 63 74 75 72 65 73 20 77 68 65 72 65 20  tructures where 
8d2d0 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74  the.** size of t
8d2e0 68 65 20 61 72 72 61 79 20 69 73 20 61 20 70 6f  he array is a po
8d2f0 77 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a 2a 2a 20  wer of 2..**.** 
8d300 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73  The size of this
8d310 20 6f 62 6a 65 63 74 20 6d 75 73 74 20 62 65 20   object must be 
8d320 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e 20  a power of two. 
8d330 20 54 68 61 74 20 66 61 63 74 20 69 73 0a 2a 2a   That fact is.**
8d340 20 76 65 72 69 66 69 65 64 20 69 6e 20 6d 65 6d   verified in mem
8d350 73 79 73 35 49 6e 69 74 28 29 2e 0a 2a 2f 0a 74  sys5Init()..*/.t
8d360 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65  ypedef struct Me
8d370 6d 35 4c 69 6e 6b 20 4d 65 6d 35 4c 69 6e 6b 3b  m5Link Mem5Link;
8d380 0a 73 74 72 75 63 74 20 4d 65 6d 35 4c 69 6e 6b  .struct Mem5Link
8d390 20 7b 0a 20 20 69 6e 74 20 6e 65 78 74 3b 20 20   {.  int next;  
8d3a0 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
8d3b0 20 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e 6b   next free chunk
8d3c0 20 2a 2f 0a 20 20 69 6e 74 20 70 72 65 76 3b 20   */.  int prev; 
8d3d0 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
8d3e0 66 20 70 72 65 76 69 6f 75 73 20 66 72 65 65 20  f previous free 
8d3f0 63 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  chunk */.};../*.
8d400 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20  ** Maximum size 
8d410 6f 66 20 61 6e 79 20 61 6c 6c 6f 63 61 74 69 6f  of any allocatio
8d420 6e 20 69 73 20 28 28 31 3c 3c 4c 4f 47 4d 41 58  n is ((1<<LOGMAX
8d430 29 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d 29 2e 20  )*mem5.szAtom). 
8d440 53 69 6e 63 65 0a 2a 2a 20 6d 65 6d 35 2e 73 7a  Since.** mem5.sz
8d450 41 74 6f 6d 20 69 73 20 61 6c 77 61 79 73 20 61  Atom is always a
8d460 74 20 6c 65 61 73 74 20 38 20 61 6e 64 20 33 32  t least 8 and 32
8d470 2d 62 69 74 20 69 6e 74 65 67 65 72 73 20 61 72  -bit integers ar
8d480 65 20 75 73 65 64 2c 0a 2a 2a 20 69 74 20 69 73  e used,.** it is
8d490 20 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 70 6f   not actually po
8d4a0 73 73 69 62 6c 65 20 74 6f 20 72 65 61 63 68 20  ssible to reach 
8d4b0 74 68 69 73 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23  this limit..*/.#
8d4c0 64 65 66 69 6e 65 20 4c 4f 47 4d 41 58 20 33 30  define LOGMAX 30
8d4d0 0a 0a 2f 2a 0a 2a 2a 20 4d 61 73 6b 73 20 75 73  ../*.** Masks us
8d4e0 65 64 20 66 6f 72 20 6d 65 6d 35 2e 61 43 74 72  ed for mem5.aCtr
8d4f0 6c 5b 5d 20 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f  l[] elements..*/
8d500 0a 23 64 65 66 69 6e 65 20 43 54 52 4c 5f 4c 4f  .#define CTRL_LO
8d510 47 53 49 5a 45 20 20 30 78 31 66 20 20 20 20 2f  GSIZE  0x1f    /
8d520 2a 20 4c 6f 67 32 20 53 69 7a 65 20 6f 66 20 74  * Log2 Size of t
8d530 68 69 73 20 62 6c 6f 63 6b 20 2a 2f 0a 23 64 65  his block */.#de
8d540 66 69 6e 65 20 43 54 52 4c 5f 46 52 45 45 20 20  fine CTRL_FREE  
8d550 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 54 72     0x20    /* Tr
8d560 75 65 20 69 66 20 6e 6f 74 20 63 68 65 63 6b 65  ue if not checke
8d570 64 20 6f 75 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  d out */../*.** 
8d580 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69  All of the stati
8d590 63 20 76 61 72 69 61 62 6c 65 73 20 75 73 65 64  c variables used
8d5a0 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20   by this module 
8d5b0 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a  are collected.**
8d5c0 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73   into a single s
8d5d0 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22  tructure named "
8d5e0 6d 65 6d 35 22 2e 20 20 54 68 69 73 20 69 73 20  mem5".  This is 
8d5f0 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73  to keep the.** s
8d600 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20  tatic variables 
8d610 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f  organized and to
8d620 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70 61 63   reduce namespac
8d630 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77  e pollution.** w
8d640 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c 65 20  hen this module 
8d650 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69 74 68  is combined with
8d660 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20 61 6d   other in the am
8d670 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73  algamation..*/.s
8d680 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44  tatic SQLITE_WSD
8d690 20 73 74 72 75 63 74 20 4d 65 6d 35 47 6c 6f 62   struct Mem5Glob
8d6a0 61 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d  al {.  /*.  ** M
8d6b0 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20  emory available 
8d6c0 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 20  for allocation. 
8d6d0 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 41 74 6f 6d   */.  int szAtom
8d6e0 3b 20 20 20 20 20 20 2f 2a 20 53 6d 61 6c 6c 65  ;      /* Smalle
8d6f0 73 74 20 70 6f 73 73 69 62 6c 65 20 61 6c 6c 6f  st possible allo
8d700 63 61 74 69 6f 6e 20 69 6e 20 62 79 74 65 73 20  cation in bytes 
8d710 2a 2f 0a 20 20 69 6e 74 20 6e 42 6c 6f 63 6b 3b  */.  int nBlock;
8d720 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
8d730 6f 66 20 73 7a 41 74 6f 6d 20 73 69 7a 65 64 20  of szAtom sized 
8d740 62 6c 6f 63 6b 73 20 69 6e 20 7a 50 6f 6f 6c 20  blocks in zPool 
8d750 2a 2f 0a 20 20 75 38 20 2a 7a 50 6f 6f 6c 3b 20  */.  u8 *zPool; 
8d760 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20        /* Memory 
8d770 61 76 61 69 6c 61 62 6c 65 20 74 6f 20 62 65 20  available to be 
8d780 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 0a  allocated */.  .
8d790 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20    /*.  ** Mutex 
8d7a0 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73  to control acces
8d7b0 73 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20  s to the memory 
8d7c0 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
8d7d0 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  stem..  */.  sql
8d7e0 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
8d7f0 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 65  x;..  /*.  ** Pe
8d800 72 66 6f 72 6d 61 6e 63 65 20 73 74 61 74 69 73  rformance statis
8d810 74 69 63 73 0a 20 20 2a 2f 0a 20 20 75 36 34 20  tics.  */.  u64 
8d820 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20  nAlloc;         
8d830 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20  /* Total number 
8d840 6f 66 20 63 61 6c 6c 73 20 74 6f 20 6d 61 6c 6c  of calls to mall
8d850 6f 63 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61  oc */.  u64 tota
8d860 6c 41 6c 6c 6f 63 3b 20 20 20 20 20 2f 2a 20 54  lAlloc;     /* T
8d870 6f 74 61 6c 20 6f 66 20 61 6c 6c 20 6d 61 6c 6c  otal of all mall
8d880 6f 63 20 63 61 6c 6c 73 20 2d 20 69 6e 63 6c 75  oc calls - inclu
8d890 64 65 73 20 69 6e 74 65 72 6e 61 6c 20 66 72 61  des internal fra
8d8a0 67 20 2a 2f 0a 20 20 75 36 34 20 74 6f 74 61 6c  g */.  u64 total
8d8b0 45 78 63 65 73 73 3b 20 20 20 20 2f 2a 20 54 6f  Excess;    /* To
8d8c0 74 61 6c 20 69 6e 74 65 72 6e 61 6c 20 66 72 61  tal internal fra
8d8d0 67 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20  gmentation */.  
8d8e0 75 33 32 20 63 75 72 72 65 6e 74 4f 75 74 3b 20  u32 currentOut; 
8d8f0 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 63      /* Current c
8d900 68 65 63 6b 6f 75 74 2c 20 69 6e 63 6c 75 64 69  heckout, includi
8d910 6e 67 20 69 6e 74 65 72 6e 61 6c 20 66 72 61 67  ng internal frag
8d920 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 75  mentation */.  u
8d930 33 32 20 63 75 72 72 65 6e 74 43 6f 75 6e 74 3b  32 currentCount;
8d940 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75     /* Current nu
8d950 6d 62 65 72 20 6f 66 20 64 69 73 74 69 6e 63 74  mber of distinct
8d960 20 63 68 65 63 6b 6f 75 74 73 20 2a 2f 0a 20 20   checkouts */.  
8d970 75 33 32 20 6d 61 78 4f 75 74 3b 20 20 20 20 20  u32 maxOut;     
8d980 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 69      /* Maximum i
8d990 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 63 75 72  nstantaneous cur
8d9a0 72 65 6e 74 4f 75 74 20 2a 2f 0a 20 20 75 33 32  rentOut */.  u32
8d9b0 20 6d 61 78 43 6f 75 6e 74 3b 20 20 20 20 20 20   maxCount;      
8d9c0 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 69 6e 73 74   /* Maximum inst
8d9d0 61 6e 74 61 6e 65 6f 75 73 20 63 75 72 72 65 6e  antaneous curren
8d9e0 74 43 6f 75 6e 74 20 2a 2f 0a 20 20 75 33 32 20  tCount */.  u32 
8d9f0 6d 61 78 52 65 71 75 65 73 74 3b 20 20 20 20 20  maxRequest;     
8da00 2f 2a 20 4c 61 72 67 65 73 74 20 61 6c 6c 6f 63  /* Largest alloc
8da10 61 74 69 6f 6e 20 28 65 78 63 6c 75 73 69 76 65  ation (exclusive
8da20 20 6f 66 20 69 6e 74 65 72 6e 61 6c 20 66 72 61   of internal fra
8da30 67 29 20 2a 2f 0a 20 20 0a 20 20 2f 2a 0a 20 20  g) */.  .  /*.  
8da40 2a 2a 20 4c 69 73 74 73 20 6f 66 20 66 72 65 65  ** Lists of free
8da50 20 62 6c 6f 63 6b 73 2e 20 20 61 69 46 72 65 65   blocks.  aiFree
8da60 6c 69 73 74 5b 30 5d 20 69 73 20 61 20 6c 69 73  list[0] is a lis
8da70 74 20 6f 66 20 66 72 65 65 20 62 6c 6f 63 6b 73  t of free blocks
8da80 20 6f 66 0a 20 20 2a 2a 20 73 69 7a 65 20 6d 65   of.  ** size me
8da90 6d 35 2e 73 7a 41 74 6f 6d 2e 20 20 61 69 46 72  m5.szAtom.  aiFr
8daa0 65 65 6c 69 73 74 5b 31 5d 20 68 6f 6c 64 73 20  eelist[1] holds 
8dab0 62 6c 6f 63 6b 73 20 6f 66 20 73 69 7a 65 20 73  blocks of size s
8dac0 7a 41 74 6f 6d 2a 32 2e 0a 20 20 2a 2a 20 61 6e  zAtom*2..  ** an
8dad0 64 20 73 6f 20 66 6f 72 74 68 2e 0a 20 20 2a 2f  d so forth..  */
8dae0 0a 20 20 69 6e 74 20 61 69 46 72 65 65 6c 69 73  .  int aiFreelis
8daf0 74 5b 4c 4f 47 4d 41 58 2b 31 5d 3b 0a 0a 20 20  t[LOGMAX+1];..  
8db00 2f 2a 0a 20 20 2a 2a 20 53 70 61 63 65 20 66 6f  /*.  ** Space fo
8db10 72 20 74 72 61 63 6b 69 6e 67 20 77 68 69 63 68  r tracking which
8db20 20 62 6c 6f 63 6b 73 20 61 72 65 20 63 68 65 63   blocks are chec
8db30 6b 65 64 20 6f 75 74 20 61 6e 64 20 74 68 65 20  ked out and the 
8db40 73 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 65 61 63  size.  ** of eac
8db50 68 20 62 6c 6f 63 6b 2e 20 20 4f 6e 65 20 62 79  h block.  One by
8db60 74 65 20 70 65 72 20 62 6c 6f 63 6b 2e 0a 20 20  te per block..  
8db70 2a 2f 0a 20 20 75 38 20 2a 61 43 74 72 6c 3b 0a  */.  u8 *aCtrl;.
8db80 0a 7d 20 6d 65 6d 35 20 3d 20 7b 20 30 20 7d 3b  .} mem5 = { 0 };
8db90 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73 20 74  ../*.** Access t
8dba0 68 65 20 73 74 61 74 69 63 20 76 61 72 69 61 62  he static variab
8dbb0 6c 65 20 74 68 72 6f 75 67 68 20 61 20 6d 61 63  le through a mac
8dbc0 72 6f 20 66 6f 72 20 53 51 4c 49 54 45 5f 4f 4d  ro for SQLITE_OM
8dbd0 49 54 5f 57 53 44 0a 2a 2f 0a 23 64 65 66 69 6e  IT_WSD.*/.#defin
8dbe0 65 20 6d 65 6d 35 20 47 4c 4f 42 41 4c 28 73 74  e mem5 GLOBAL(st
8dbf0 72 75 63 74 20 4d 65 6d 35 47 6c 6f 62 61 6c 2c  ruct Mem5Global,
8dc00 20 6d 65 6d 35 29 0a 0a 2f 2a 0a 2a 2a 20 41 73   mem5)../*.** As
8dc10 73 75 6d 69 6e 67 20 6d 65 6d 35 2e 7a 50 6f 6f  suming mem5.zPoo
8dc20 6c 20 69 73 20 64 69 76 69 64 65 64 20 75 70 20  l is divided up 
8dc30 69 6e 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66  into an array of
8dc40 20 4d 65 6d 35 4c 69 6e 6b 0a 2a 2a 20 73 74 72   Mem5Link.** str
8dc50 75 63 74 75 72 65 73 2c 20 72 65 74 75 72 6e 20  uctures, return 
8dc60 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
8dc70 20 69 64 78 2d 74 68 20 73 75 63 68 20 6c 69 6b   idx-th such lik
8dc80 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  ..*/.#define MEM
8dc90 35 4c 49 4e 4b 28 69 64 78 29 20 28 28 4d 65 6d  5LINK(idx) ((Mem
8dca0 35 4c 69 6e 6b 20 2a 29 28 26 6d 65 6d 35 2e 7a  5Link *)(&mem5.z
8dcb0 50 6f 6f 6c 5b 28 69 64 78 29 2a 6d 65 6d 35 2e  Pool[(idx)*mem5.
8dcc0 73 7a 41 74 6f 6d 5d 29 29 0a 0a 2f 2a 0a 2a 2a  szAtom]))../*.**
8dcd0 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e   Unlink the chun
8dce0 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b  k at mem5.aPool[
8dcf0 69 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74 20  i] from list it 
8dd00 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20  is currently.** 
8dd10 6f 6e 2e 20 20 49 74 20 73 68 6f 75 6c 64 20 62  on.  It should b
8dd20 65 20 66 6f 75 6e 64 20 6f 6e 20 6d 65 6d 35 2e  e found on mem5.
8dd30 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67 73  aiFreelist[iLogs
8dd40 69 7a 65 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ize]..*/.static 
8dd50 76 6f 69 64 20 6d 65 6d 73 79 73 35 55 6e 6c 69  void memsys5Unli
8dd60 6e 6b 28 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c  nk(int i, int iL
8dd70 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 6e  ogsize){.  int n
8dd80 65 78 74 2c 20 70 72 65 76 3b 0a 20 20 61 73 73  ext, prev;.  ass
8dd90 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 6d  ert( i>=0 && i<m
8dda0 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20  em5.nBlock );.  
8ddb0 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a 65  assert( iLogsize
8ddc0 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65 3c  >=0 && iLogsize<
8ddd0 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73 73  =LOGMAX );.  ass
8dde0 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c  ert( (mem5.aCtrl
8ddf0 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53 49  [i] & CTRL_LOGSI
8de00 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29 3b  ZE)==iLogsize );
8de10 0a 0a 20 20 6e 65 78 74 20 3d 20 4d 45 4d 35 4c  ..  next = MEM5L
8de20 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20  INK(i)->next;.  
8de30 70 72 65 76 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28  prev = MEM5LINK(
8de40 69 29 2d 3e 70 72 65 76 3b 0a 20 20 69 66 28 20  i)->prev;.  if( 
8de50 70 72 65 76 3c 30 20 29 7b 0a 20 20 20 20 6d 65  prev<0 ){.    me
8de60 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c  m5.aiFreelist[iL
8de70 6f 67 73 69 7a 65 5d 20 3d 20 6e 65 78 74 3b 0a  ogsize] = next;.
8de80 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 45 4d    }else{.    MEM
8de90 35 4c 49 4e 4b 28 70 72 65 76 29 2d 3e 6e 65 78  5LINK(prev)->nex
8dea0 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20 20  t = next;.  }.  
8deb0 69 66 28 20 6e 65 78 74 3e 3d 30 20 29 7b 0a 20  if( next>=0 ){. 
8dec0 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 6e 65 78 74     MEM5LINK(next
8ded0 29 2d 3e 70 72 65 76 20 3d 20 70 72 65 76 3b 0a  )->prev = prev;.
8dee0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e    }.}../*.** Lin
8def0 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74 20 6d  k the chunk at m
8df00 65 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20 73 6f 20  em5.aPool[i] so 
8df10 74 68 61 74 20 69 73 20 6f 6e 20 74 68 65 20 69  that is on the i
8df20 4c 6f 67 73 69 7a 65 0a 2a 2a 20 66 72 65 65 20  Logsize.** free 
8df30 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  list..*/.static 
8df40 76 6f 69 64 20 6d 65 6d 73 79 73 35 4c 69 6e 6b  void memsys5Link
8df50 28 69 6e 74 20 69 2c 20 69 6e 74 20 69 4c 6f 67  (int i, int iLog
8df60 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 78 3b 0a  size){.  int x;.
8df70 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
8df80 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d  3_mutex_held(mem
8df90 35 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  5.mutex) );.  as
8dfa0 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
8dfb0 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20  mem5.nBlock );. 
8dfc0 20 61 73 73 65 72 74 28 20 69 4c 6f 67 73 69 7a   assert( iLogsiz
8dfd0 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69 7a 65  e>=0 && iLogsize
8dfe0 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20 61 73  <=LOGMAX );.  as
8dff0 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43 74 72  sert( (mem5.aCtr
8e000 6c 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53  l[i] & CTRL_LOGS
8e010 49 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65 20 29  IZE)==iLogsize )
8e020 3b 0a 0a 20 20 78 20 3d 20 4d 45 4d 35 4c 49 4e  ;..  x = MEM5LIN
8e030 4b 28 69 29 2d 3e 6e 65 78 74 20 3d 20 6d 65 6d  K(i)->next = mem
8e040 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f  5.aiFreelist[iLo
8e050 67 73 69 7a 65 5d 3b 0a 20 20 4d 45 4d 35 4c 49  gsize];.  MEM5LI
8e060 4e 4b 28 69 29 2d 3e 70 72 65 76 20 3d 20 2d 31  NK(i)->prev = -1
8e070 3b 0a 20 20 69 66 28 20 78 3e 3d 30 20 29 7b 0a  ;.  if( x>=0 ){.
8e080 20 20 20 20 61 73 73 65 72 74 28 20 78 3c 6d 65      assert( x<me
8e090 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 20  m5.nBlock );.   
8e0a0 20 4d 45 4d 35 4c 49 4e 4b 28 78 29 2d 3e 70 72   MEM5LINK(x)->pr
8e0b0 65 76 20 3d 20 69 3b 0a 20 20 7d 0a 20 20 6d 65  ev = i;.  }.  me
8e0c0 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c  m5.aiFreelist[iL
8e0d0 6f 67 73 69 7a 65 5d 20 3d 20 69 3b 0a 7d 0a 0a  ogsize] = i;.}..
8e0e0 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 54 41  /*.** If the STA
8e0f0 54 49 43 5f 4d 45 4d 20 6d 75 74 65 78 20 69 73  TIC_MEM mutex is
8e100 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 68 65 6c   not already hel
8e110 64 2c 20 6f 62 74 61 69 6e 20 69 74 20 6e 6f 77  d, obtain it now
8e120 2e 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 77  . The mutex.** w
8e130 69 6c 6c 20 61 6c 72 65 61 64 79 20 62 65 20 68  ill already be h
8e140 65 6c 64 20 28 6f 62 74 61 69 6e 65 64 20 62 79  eld (obtained by
8e150 20 63 6f 64 65 20 69 6e 20 6d 61 6c 6c 6f 63 2e   code in malloc.
8e160 63 29 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65 33  c) if.** sqlite3
8e170 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65  GlobalConfig.bMe
8e180 6d 53 74 61 74 20 69 73 20 74 72 75 65 2e 0a 2a  mStat is true..*
8e190 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65  /.static void me
8e1a0 6d 73 79 73 35 45 6e 74 65 72 28 76 6f 69 64 29  msys5Enter(void)
8e1b0 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
8e1c0 78 5f 65 6e 74 65 72 28 6d 65 6d 35 2e 6d 75 74  x_enter(mem5.mut
8e1d0 65 78 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  ex);.}.static vo
8e1e0 69 64 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28  id memsys5Leave(
8e1f0 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33  void){.  sqlite3
8e200 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
8e210 35 2e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a  5.mutex);.}../*.
8e220 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73 69  ** Return the si
8e230 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e  ze of an outstan
8e240 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c  ding allocation,
8e250 20 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65 0a   in bytes.  The.
8e260 2a 2a 20 73 69 7a 65 20 72 65 74 75 72 6e 65 64  ** size returned
8e270 20 6f 6d 69 74 73 20 74 68 65 20 38 2d 62 79 74   omits the 8-byt
8e280 65 20 68 65 61 64 65 72 20 6f 76 65 72 68 65 61  e header overhea
8e290 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a  d.  This only.**
8e2a0 20 77 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e 6b   works for chunk
8e2b0 73 20 74 68 61 74 20 61 72 65 20 63 75 72 72 65  s that are curre
8e2c0 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74  ntly checked out
8e2d0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
8e2e0 6d 65 6d 73 79 73 35 53 69 7a 65 28 76 6f 69 64  memsys5Size(void
8e2f0 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 53 69 7a   *p){.  int iSiz
8e300 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29  e = 0;.  if( p )
8e310 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 28 28  {.    int i = ((
8e320 75 38 20 2a 29 70 2d 6d 65 6d 35 2e 7a 50 6f 6f  u8 *)p-mem5.zPoo
8e330 6c 29 2f 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3b 0a  l)/mem5.szAtom;.
8e340 20 20 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30      assert( i>=0
8e350 20 26 26 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63   && i<mem5.nBloc
8e360 6b 20 29 3b 0a 20 20 20 20 69 53 69 7a 65 20 3d  k );.    iSize =
8e370 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 2a 20 28   mem5.szAtom * (
8e380 31 20 3c 3c 20 28 6d 65 6d 35 2e 61 43 74 72 6c  1 << (mem5.aCtrl
8e390 5b 69 5d 26 43 54 52 4c 5f 4c 4f 47 53 49 5a 45  [i]&CTRL_LOGSIZE
8e3a0 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ));.  }.  return
8e3b0 20 69 53 69 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   iSize;.}../*.**
8e3c0 20 46 69 6e 64 20 74 68 65 20 66 69 72 73 74 20   Find the first 
8e3d0 65 6e 74 72 79 20 6f 6e 20 74 68 65 20 66 72 65  entry on the fre
8e3e0 65 6c 69 73 74 20 69 4c 6f 67 73 69 7a 65 2e 20  elist iLogsize. 
8e3f0 20 55 6e 6c 69 6e 6b 20 74 68 61 74 0a 2a 2a 20   Unlink that.** 
8e400 65 6e 74 72 79 20 61 6e 64 20 72 65 74 75 72 6e  entry and return
8e410 20 69 74 73 20 69 6e 64 65 78 2e 20 0a 2a 2f 0a   its index. .*/.
8e420 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79  static int memsy
8e430 73 35 55 6e 6c 69 6e 6b 46 69 72 73 74 28 69 6e  s5UnlinkFirst(in
8e440 74 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69  t iLogsize){.  i
8e450 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 46 69 72  nt i;.  int iFir
8e460 73 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69  st;..  assert( i
8e470 4c 6f 67 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c  Logsize>=0 && iL
8e480 6f 67 73 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29  ogsize<=LOGMAX )
8e490 3b 0a 20 20 69 20 3d 20 69 46 69 72 73 74 20 3d  ;.  i = iFirst =
8e4a0 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74   mem5.aiFreelist
8e4b0 5b 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 61 73  [iLogsize];.  as
8e4c0 73 65 72 74 28 20 69 46 69 72 73 74 3e 3d 30 20  sert( iFirst>=0 
8e4d0 29 3b 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20  );.  while( i>0 
8e4e0 29 7b 0a 20 20 20 20 69 66 28 20 69 3c 69 46 69  ){.    if( i<iFi
8e4f0 72 73 74 20 29 20 69 46 69 72 73 74 20 3d 20 69  rst ) iFirst = i
8e500 3b 0a 20 20 20 20 69 20 3d 20 4d 45 4d 35 4c 49  ;.    i = MEM5LI
8e510 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b 0a 20 20 7d  NK(i)->next;.  }
8e520 0a 20 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b  .  memsys5Unlink
8e530 28 69 46 69 72 73 74 2c 20 69 4c 6f 67 73 69 7a  (iFirst, iLogsiz
8e540 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 46 69  e);.  return iFi
8e550 72 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  rst;.}../*.** Re
8e560 74 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20  turn a block of 
8e570 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61  memory of at lea
8e580 73 74 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a  st nBytes in siz
8e590 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c  e..** Return NUL
8e5a0 4c 20 69 66 20 75 6e 61 62 6c 65 2e 20 20 52 65  L if unable.  Re
8e5b0 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 42 79  turn NULL if nBy
8e5c0 74 65 73 3d 3d 30 2e 0a 2a 2a 0a 2a 2a 20 54 68  tes==0..**.** Th
8e5d0 65 20 63 61 6c 6c 65 72 20 67 75 61 72 61 6e 74  e caller guarant
8e5e0 65 65 73 20 74 68 61 74 20 6e 42 79 74 65 20 70  ees that nByte p
8e5f0 6f 73 69 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54  ositive..**.** T
8e600 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20 6f 62  he caller has ob
8e610 74 61 69 6e 65 64 20 61 20 6d 75 74 65 78 20 70  tained a mutex p
8e620 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b 69 6e 67  rior to invoking
8e630 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65   this.** routine
8e640 20 73 6f 20 74 68 65 72 65 20 69 73 20 6e 65 76   so there is nev
8e650 65 72 20 61 6e 79 20 63 68 61 6e 63 65 20 74 68  er any chance th
8e660 61 74 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a 2a  at two or more.*
8e670 2a 20 74 68 72 65 61 64 73 20 63 61 6e 20 62 65  * threads can be
8e680 20 69 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65   in this routine
8e690 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
8e6a0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
8e6b0 64 20 2a 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f 63  d *memsys5Malloc
8e6c0 55 6e 73 61 66 65 28 69 6e 74 20 6e 42 79 74 65  Unsafe(int nByte
8e6d0 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  ){.  int i;     
8e6e0 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
8e6f0 66 20 61 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 5d  f a mem5.aPool[]
8e700 20 73 6c 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 69   slot */.  int i
8e710 42 69 6e 3b 20 20 20 20 20 20 20 20 2f 2a 20 49  Bin;        /* I
8e720 6e 64 65 78 20 69 6e 74 6f 20 6d 65 6d 35 2e 61  ndex into mem5.a
8e730 69 46 72 65 65 6c 69 73 74 5b 5d 20 2a 2f 0a 20  iFreelist[] */. 
8e740 20 69 6e 74 20 69 46 75 6c 6c 53 7a 3b 20 20 20   int iFullSz;   
8e750 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 6c 6c    /* Size of all
8e760 6f 63 61 74 69 6f 6e 20 72 6f 75 6e 64 65 64 20  ocation rounded 
8e770 75 70 20 74 6f 20 70 6f 77 65 72 20 6f 66 20 32  up to power of 2
8e780 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 6f 67 73 69   */.  int iLogsi
8e790 7a 65 3b 20 20 20 20 2f 2a 20 4c 6f 67 32 20 6f  ze;    /* Log2 o
8e7a0 66 20 69 46 75 6c 6c 53 7a 2f 50 4f 57 32 5f 4d  f iFullSz/POW2_M
8e7b0 49 4e 20 2a 2f 0a 0a 20 20 2f 2a 20 6e 42 79 74  IN */..  /* nByt
8e7c0 65 20 6d 75 73 74 20 62 65 20 61 20 70 6f 73 69  e must be a posi
8e7d0 74 69 76 65 20 2a 2f 0a 20 20 61 73 73 65 72 74  tive */.  assert
8e7e0 28 20 6e 42 79 74 65 3e 30 20 29 3b 0a 0a 20 20  ( nByte>0 );..  
8e7f0 2f 2a 20 4b 65 65 70 20 74 72 61 63 6b 20 6f 66  /* Keep track of
8e800 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c   the maximum all
8e810 6f 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 2e  ocation request.
8e820 20 20 45 76 65 6e 20 75 6e 66 75 6c 66 69 6c 6c    Even unfulfill
8e830 65 64 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 73  ed.  ** requests
8e840 20 61 72 65 20 63 6f 75 6e 74 65 64 20 2a 2f 0a   are counted */.
8e850 20 20 69 66 28 20 28 75 33 32 29 6e 42 79 74 65    if( (u32)nByte
8e860 3e 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74  >mem5.maxRequest
8e870 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 61 78   ){.    mem5.max
8e880 52 65 71 75 65 73 74 20 3d 20 6e 42 79 74 65 3b  Request = nByte;
8e890 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 62 6f 72 74  .  }..  /* Abort
8e8a0 20 69 66 20 74 68 65 20 72 65 71 75 65 73 74 65   if the requeste
8e8b0 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a  d allocation siz
8e8c0 65 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e  e is larger than
8e8d0 20 74 68 65 20 6c 61 72 67 65 73 74 0a 20 20 2a   the largest.  *
8e8e0 2a 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 20 74  * power of two t
8e8f0 68 61 74 20 77 65 20 63 61 6e 20 72 65 70 72 65  hat we can repre
8e900 73 65 6e 74 20 75 73 69 6e 67 20 33 32 2d 62 69  sent using 32-bi
8e910 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
8e920 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 42  s..  */.  if( nB
8e930 79 74 65 20 3e 20 30 78 34 30 30 30 30 30 30 30  yte > 0x40000000
8e940 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
8e950 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f 75 6e  ;.  }..  /* Roun
8e960 64 20 6e 42 79 74 65 20 75 70 20 74 6f 20 74 68  d nByte up to th
8e970 65 20 6e 65 78 74 20 76 61 6c 69 64 20 70 6f 77  e next valid pow
8e980 65 72 20 6f 66 20 74 77 6f 20 2a 2f 0a 20 20 66  er of two */.  f
8e990 6f 72 28 69 46 75 6c 6c 53 7a 3d 6d 65 6d 35 2e  or(iFullSz=mem5.
8e9a0 73 7a 41 74 6f 6d 2c 20 69 4c 6f 67 73 69 7a 65  szAtom, iLogsize
8e9b0 3d 30 3b 20 69 46 75 6c 6c 53 7a 3c 6e 42 79 74  =0; iFullSz<nByt
8e9c0 65 3b 20 69 46 75 6c 6c 53 7a 20 2a 3d 20 32 2c  e; iFullSz *= 2,
8e9d0 20 69 4c 6f 67 73 69 7a 65 2b 2b 29 7b 7d 0a 0a   iLogsize++){}..
8e9e0 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 6d    /* Make sure m
8e9f0 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69  em5.aiFreelist[i
8ea00 4c 6f 67 73 69 7a 65 5d 20 63 6f 6e 74 61 69 6e  Logsize] contain
8ea10 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 66  s at least one f
8ea20 72 65 65 0a 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20  ree.  ** block. 
8ea30 20 49 66 20 6e 6f 74 2c 20 74 68 65 6e 20 73 70   If not, then sp
8ea40 6c 69 74 20 61 20 62 6c 6f 63 6b 20 6f 66 20 74  lit a block of t
8ea50 68 65 20 6e 65 78 74 20 6c 61 72 67 65 72 20 70  he next larger p
8ea60 6f 77 65 72 20 6f 66 0a 20 20 2a 2a 20 74 77 6f  ower of.  ** two
8ea70 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72 65   in order to cre
8ea80 61 74 65 20 61 20 6e 65 77 20 66 72 65 65 20 62  ate a new free b
8ea90 6c 6f 63 6b 20 6f 66 20 73 69 7a 65 20 69 4c 6f  lock of size iLo
8eaa0 67 73 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f  gsize..  */.  fo
8eab0 72 28 69 42 69 6e 3d 69 4c 6f 67 73 69 7a 65 3b  r(iBin=iLogsize;
8eac0 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74   mem5.aiFreelist
8ead0 5b 69 42 69 6e 5d 3c 30 20 26 26 20 69 42 69 6e  [iBin]<0 && iBin
8eae0 3c 3d 4c 4f 47 4d 41 58 3b 20 69 42 69 6e 2b 2b  <=LOGMAX; iBin++
8eaf0 29 7b 7d 0a 20 20 69 66 28 20 69 42 69 6e 3e 4c  ){}.  if( iBin>L
8eb00 4f 47 4d 41 58 20 29 20 72 65 74 75 72 6e 20 30  OGMAX ) return 0
8eb10 3b 0a 20 20 69 20 3d 20 6d 65 6d 73 79 73 35 55  ;.  i = memsys5U
8eb20 6e 6c 69 6e 6b 46 69 72 73 74 28 69 42 69 6e 29  nlinkFirst(iBin)
8eb30 3b 0a 20 20 77 68 69 6c 65 28 20 69 42 69 6e 3e  ;.  while( iBin>
8eb40 69 4c 6f 67 73 69 7a 65 20 29 7b 0a 20 20 20 20  iLogsize ){.    
8eb50 69 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 0a 20 20  int newSize;..  
8eb60 20 20 69 42 69 6e 2d 2d 3b 0a 20 20 20 20 6e 65    iBin--;.    ne
8eb70 77 53 69 7a 65 20 3d 20 31 20 3c 3c 20 69 42 69  wSize = 1 << iBi
8eb80 6e 3b 0a 20 20 20 20 6d 65 6d 35 2e 61 43 74 72  n;.    mem5.aCtr
8eb90 6c 5b 69 2b 6e 65 77 53 69 7a 65 5d 20 3d 20 43  l[i+newSize] = C
8eba0 54 52 4c 5f 46 52 45 45 20 7c 20 69 42 69 6e 3b  TRL_FREE | iBin;
8ebb0 0a 20 20 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b  .    memsys5Link
8ebc0 28 69 2b 6e 65 77 53 69 7a 65 2c 20 69 42 69 6e  (i+newSize, iBin
8ebd0 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 43  );.  }.  mem5.aC
8ebe0 74 72 6c 5b 69 5d 20 3d 20 69 4c 6f 67 73 69 7a  trl[i] = iLogsiz
8ebf0 65 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20  e;..  /* Update 
8ec00 61 6c 6c 6f 63 61 74 6f 72 20 70 65 72 66 6f 72  allocator perfor
8ec10 6d 61 6e 63 65 20 73 74 61 74 69 73 74 69 63 73  mance statistics
8ec20 2e 20 2a 2f 0a 20 20 6d 65 6d 35 2e 6e 41 6c 6c  . */.  mem5.nAll
8ec30 6f 63 2b 2b 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74  oc++;.  mem5.tot
8ec40 61 6c 41 6c 6c 6f 63 20 2b 3d 20 69 46 75 6c 6c  alAlloc += iFull
8ec50 53 7a 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c  Sz;.  mem5.total
8ec60 45 78 63 65 73 73 20 2b 3d 20 69 46 75 6c 6c 53  Excess += iFullS
8ec70 7a 20 2d 20 6e 42 79 74 65 3b 0a 20 20 6d 65 6d  z - nByte;.  mem
8ec80 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 2b 2b  5.currentCount++
8ec90 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74  ;.  mem5.current
8eca0 4f 75 74 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a  Out += iFullSz;.
8ecb0 20 20 69 66 28 20 6d 65 6d 35 2e 6d 61 78 43 6f    if( mem5.maxCo
8ecc0 75 6e 74 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74  unt<mem5.current
8ecd0 43 6f 75 6e 74 20 29 20 6d 65 6d 35 2e 6d 61 78  Count ) mem5.max
8ece0 43 6f 75 6e 74 20 3d 20 6d 65 6d 35 2e 63 75 72  Count = mem5.cur
8ecf0 72 65 6e 74 43 6f 75 6e 74 3b 0a 20 20 69 66 28  rentCount;.  if(
8ed00 20 6d 65 6d 35 2e 6d 61 78 4f 75 74 3c 6d 65 6d   mem5.maxOut<mem
8ed10 35 2e 63 75 72 72 65 6e 74 4f 75 74 20 29 20 6d  5.currentOut ) m
8ed20 65 6d 35 2e 6d 61 78 4f 75 74 20 3d 20 6d 65 6d  em5.maxOut = mem
8ed30 35 2e 63 75 72 72 65 6e 74 4f 75 74 3b 0a 0a 20  5.currentOut;.. 
8ed40 20 2f 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69   /* Return a poi
8ed50 6e 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f  nter to the allo
8ed60 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20 2a 2f  cated memory. */
8ed70 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a  .  return (void*
8ed80 29 26 6d 65 6d 35 2e 7a 50 6f 6f 6c 5b 69 2a 6d  )&mem5.zPool[i*m
8ed90 65 6d 35 2e 73 7a 41 74 6f 6d 5d 3b 0a 7d 0a 0a  em5.szAtom];.}..
8eda0 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75  /*.** Free an ou
8edb0 74 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79  tstanding memory
8edc0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a   allocation..*/.
8edd0 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73  static void mems
8ede0 79 73 35 46 72 65 65 55 6e 73 61 66 65 28 76 6f  ys5FreeUnsafe(vo
8edf0 69 64 20 2a 70 4f 6c 64 29 7b 0a 20 20 75 33 32  id *pOld){.  u32
8ee00 20 73 69 7a 65 2c 20 69 4c 6f 67 73 69 7a 65 3b   size, iLogsize;
8ee10 0a 20 20 69 6e 74 20 69 42 6c 6f 63 6b 3b 0a 0a  .  int iBlock;..
8ee20 20 20 2f 2a 20 53 65 74 20 69 42 6c 6f 63 6b 20    /* Set iBlock 
8ee30 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  to the index of 
8ee40 74 68 65 20 62 6c 6f 63 6b 20 70 6f 69 6e 74 65  the block pointe
8ee50 64 20 74 6f 20 62 79 20 70 4f 6c 64 20 69 6e 20  d to by pOld in 
8ee60 0a 20 20 2a 2a 20 74 68 65 20 61 72 72 61 79 20  .  ** the array 
8ee70 6f 66 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 62  of mem5.szAtom b
8ee80 79 74 65 20 62 6c 6f 63 6b 73 20 70 6f 69 6e 74  yte blocks point
8ee90 65 64 20 74 6f 20 62 79 20 6d 65 6d 35 2e 7a 50  ed to by mem5.zP
8eea0 6f 6f 6c 2e 0a 20 20 2a 2f 0a 20 20 69 42 6c 6f  ool..  */.  iBlo
8eeb0 63 6b 20 3d 20 28 28 75 38 20 2a 29 70 4f 6c 64  ck = ((u8 *)pOld
8eec0 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29 2f 6d 65 6d  -mem5.zPool)/mem
8eed0 35 2e 73 7a 41 74 6f 6d 3b 0a 0a 20 20 2f 2a 20  5.szAtom;..  /* 
8eee0 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70  Check that the p
8eef0 6f 69 6e 74 65 72 20 70 4f 6c 64 20 70 6f 69 6e  ointer pOld poin
8ef00 74 73 20 74 6f 20 61 20 76 61 6c 69 64 2c 20 6e  ts to a valid, n
8ef10 6f 6e 2d 66 72 65 65 20 62 6c 6f 63 6b 2e 20 2a  on-free block. *
8ef20 2f 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f  /.  assert( iBlo
8ef30 63 6b 3e 3d 30 20 26 26 20 69 42 6c 6f 63 6b 3c  ck>=0 && iBlock<
8ef40 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 20  mem5.nBlock );. 
8ef50 20 61 73 73 65 72 74 28 20 28 28 75 38 20 2a 29   assert( ((u8 *)
8ef60 70 4f 6c 64 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29  pOld-mem5.zPool)
8ef70 25 6d 65 6d 35 2e 73 7a 41 74 6f 6d 3d 3d 30 20  %mem5.szAtom==0 
8ef80 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d 65  );.  assert( (me
8ef90 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d  m5.aCtrl[iBlock]
8efa0 20 26 20 43 54 52 4c 5f 46 52 45 45 29 3d 3d 30   & CTRL_FREE)==0
8efb0 20 29 3b 0a 0a 20 20 69 4c 6f 67 73 69 7a 65 20   );..  iLogsize 
8efc0 3d 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c  = mem5.aCtrl[iBl
8efd0 6f 63 6b 5d 20 26 20 43 54 52 4c 5f 4c 4f 47 53  ock] & CTRL_LOGS
8efe0 49 5a 45 3b 0a 20 20 73 69 7a 65 20 3d 20 31 3c  IZE;.  size = 1<
8eff0 3c 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 61 73 73  <iLogsize;.  ass
8f000 65 72 74 28 20 69 42 6c 6f 63 6b 2b 73 69 7a 65  ert( iBlock+size
8f010 2d 31 3c 28 75 33 32 29 6d 65 6d 35 2e 6e 42 6c  -1<(u32)mem5.nBl
8f020 6f 63 6b 20 29 3b 0a 0a 20 20 6d 65 6d 35 2e 61  ock );..  mem5.a
8f030 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 7c 3d 20  Ctrl[iBlock] |= 
8f040 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 6d 65 6d  CTRL_FREE;.  mem
8f050 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 2b 73  5.aCtrl[iBlock+s
8f060 69 7a 65 2d 31 5d 20 7c 3d 20 43 54 52 4c 5f 46  ize-1] |= CTRL_F
8f070 52 45 45 3b 0a 20 20 61 73 73 65 72 74 28 20 6d  REE;.  assert( m
8f080 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74  em5.currentCount
8f090 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
8f0a0 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3e  mem5.currentOut>
8f0b0 3d 28 73 69 7a 65 2a 6d 65 6d 35 2e 73 7a 41 74  =(size*mem5.szAt
8f0c0 6f 6d 29 20 29 3b 0a 20 20 6d 65 6d 35 2e 63 75  om) );.  mem5.cu
8f0d0 72 72 65 6e 74 43 6f 75 6e 74 2d 2d 3b 0a 20 20  rrentCount--;.  
8f0e0 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20  mem5.currentOut 
8f0f0 2d 3d 20 73 69 7a 65 2a 6d 65 6d 35 2e 73 7a 41  -= size*mem5.szA
8f100 74 6f 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 6d  tom;.  assert( m
8f110 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 3e 30  em5.currentOut>0
8f120 20 7c 7c 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74   || mem5.current
8f130 43 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 61 73  Count==0 );.  as
8f140 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65  sert( mem5.curre
8f150 6e 74 43 6f 75 6e 74 3e 30 20 7c 7c 20 6d 65 6d  ntCount>0 || mem
8f160 35 2e 63 75 72 72 65 6e 74 4f 75 74 3d 3d 30 20  5.currentOut==0 
8f170 29 3b 0a 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c  );..  mem5.aCtrl
8f180 5b 69 42 6c 6f 63 6b 5d 20 3d 20 43 54 52 4c 5f  [iBlock] = CTRL_
8f190 46 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a 65 3b  FREE | iLogsize;
8f1a0 0a 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53  .  while( ALWAYS
8f1b0 28 69 4c 6f 67 73 69 7a 65 3c 4c 4f 47 4d 41 58  (iLogsize<LOGMAX
8f1c0 29 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 42 75  ) ){.    int iBu
8f1d0 64 64 79 3b 0a 20 20 20 20 69 66 28 20 28 69 42  ddy;.    if( (iB
8f1e0 6c 6f 63 6b 3e 3e 69 4c 6f 67 73 69 7a 65 29 20  lock>>iLogsize) 
8f1f0 26 20 31 20 29 7b 0a 20 20 20 20 20 20 69 42 75  & 1 ){.      iBu
8f200 64 64 79 20 3d 20 69 42 6c 6f 63 6b 20 2d 20 73  ddy = iBlock - s
8f210 69 7a 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ize;.    }else{.
8f220 20 20 20 20 20 20 69 42 75 64 64 79 20 3d 20 69        iBuddy = i
8f230 42 6c 6f 63 6b 20 2b 20 73 69 7a 65 3b 0a 20 20  Block + size;.  
8f240 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
8f250 69 42 75 64 64 79 3e 3d 30 20 29 3b 0a 20 20 20  iBuddy>=0 );.   
8f260 20 69 66 28 20 28 69 42 75 64 64 79 2b 28 31 3c   if( (iBuddy+(1<
8f270 3c 69 4c 6f 67 73 69 7a 65 29 29 3e 6d 65 6d 35  <iLogsize))>mem5
8f280 2e 6e 42 6c 6f 63 6b 20 29 20 62 72 65 61 6b 3b  .nBlock ) break;
8f290 0a 20 20 20 20 69 66 28 20 6d 65 6d 35 2e 61 43  .    if( mem5.aC
8f2a0 74 72 6c 5b 69 42 75 64 64 79 5d 21 3d 28 43 54  trl[iBuddy]!=(CT
8f2b0 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73 69  RL_FREE | iLogsi
8f2c0 7a 65 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ze) ) break;.   
8f2d0 20 6d 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69   memsys5Unlink(i
8f2e0 42 75 64 64 79 2c 20 69 4c 6f 67 73 69 7a 65 29  Buddy, iLogsize)
8f2f0 3b 0a 20 20 20 20 69 4c 6f 67 73 69 7a 65 2b 2b  ;.    iLogsize++
8f300 3b 0a 20 20 20 20 69 66 28 20 69 42 75 64 64 79  ;.    if( iBuddy
8f310 3c 69 42 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20  <iBlock ){.     
8f320 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64   mem5.aCtrl[iBud
8f330 64 79 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45 20  dy] = CTRL_FREE 
8f340 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20  | iLogsize;.    
8f350 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c    mem5.aCtrl[iBl
8f360 6f 63 6b 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20  ock] = 0;.      
8f370 69 42 6c 6f 63 6b 20 3d 20 69 42 75 64 64 79 3b  iBlock = iBuddy;
8f380 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
8f390 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c    mem5.aCtrl[iBl
8f3a0 6f 63 6b 5d 20 3d 20 43 54 52 4c 5f 46 52 45 45  ock] = CTRL_FREE
8f3b0 20 7c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 20   | iLogsize;.   
8f3c0 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42     mem5.aCtrl[iB
8f3d0 75 64 64 79 5d 20 3d 20 30 3b 0a 20 20 20 20 7d  uddy] = 0;.    }
8f3e0 0a 20 20 20 20 73 69 7a 65 20 2a 3d 20 32 3b 0a  .    size *= 2;.
8f3f0 20 20 7d 0a 20 20 6d 65 6d 73 79 73 35 4c 69 6e    }.  memsys5Lin
8f400 6b 28 69 42 6c 6f 63 6b 2c 20 69 4c 6f 67 73 69  k(iBlock, iLogsi
8f410 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c  ze);.}../*.** Al
8f420 6c 6f 63 61 74 65 20 6e 42 79 74 65 73 20 6f 66  locate nBytes of
8f430 20 6d 65 6d 6f 72 79 0a 2a 2f 0a 73 74 61 74 69   memory.*/.stati
8f440 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35 4d  c void *memsys5M
8f450 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 73  alloc(int nBytes
8f460 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  ){.  sqlite3_int
8f470 36 34 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28  64 *p = 0;.  if(
8f480 20 6e 42 79 74 65 73 3e 30 20 29 7b 0a 20 20 20   nBytes>0 ){.   
8f490 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29 3b   memsys5Enter();
8f4a0 0a 20 20 20 20 70 20 3d 20 6d 65 6d 73 79 73 35  .    p = memsys5
8f4b0 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79  MallocUnsafe(nBy
8f4c0 74 65 73 29 3b 0a 20 20 20 20 6d 65 6d 73 79 73  tes);.    memsys
8f4d0 35 4c 65 61 76 65 28 29 3b 0a 20 20 7d 0a 20 20  5Leave();.  }.  
8f4e0 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b  return (void*)p;
8f4f0 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20   .}../*.** Free 
8f500 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  memory..**.** Th
8f510 65 20 6f 75 74 65 72 20 6c 61 79 65 72 20 6d 65  e outer layer me
8f520 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 70  mory allocator p
8f530 72 65 76 65 6e 74 73 20 74 68 69 73 20 72 6f 75  revents this rou
8f540 74 69 6e 65 20 66 72 6f 6d 0a 2a 2a 20 62 65 69  tine from.** bei
8f550 6e 67 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70  ng called with p
8f560 50 72 69 6f 72 3d 3d 30 2e 0a 2a 2f 0a 73 74 61  Prior==0..*/.sta
8f570 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35  tic void memsys5
8f580 46 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f  Free(void *pPrio
8f590 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50  r){.  assert( pP
8f5a0 72 69 6f 72 21 3d 30 20 29 3b 0a 20 20 6d 65 6d  rior!=0 );.  mem
8f5b0 73 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 6d  sys5Enter();.  m
8f5c0 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61 66 65  emsys5FreeUnsafe
8f5d0 28 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d 73  (pPrior);.  mems
8f5e0 79 73 35 4c 65 61 76 65 28 29 3b 20 20 0a 7d 0a  ys5Leave();  .}.
8f5f0 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
8f600 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69  e size of an exi
8f610 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c  sting memory all
8f620 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  ocation..**.** T
8f630 68 65 20 6f 75 74 65 72 20 6c 61 79 65 72 20 6d  he outer layer m
8f640 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
8f650 70 72 65 76 65 6e 74 73 20 74 68 69 73 20 72 6f  prevents this ro
8f660 75 74 69 6e 65 20 66 72 6f 6d 0a 2a 2a 20 62 65  utine from.** be
8f670 69 6e 67 20 63 61 6c 6c 65 64 20 77 69 74 68 20  ing called with 
8f680 70 50 72 69 6f 72 3d 3d 30 2e 20 20 0a 2a 2a 0a  pPrior==0.  .**.
8f690 2a 2a 20 6e 42 79 74 65 73 20 69 73 20 61 6c 77  ** nBytes is alw
8f6a0 61 79 73 20 61 20 76 61 6c 75 65 20 6f 62 74 61  ays a value obta
8f6b0 69 6e 65 64 20 66 72 6f 6d 20 61 20 70 72 69 6f  ined from a prio
8f6c0 72 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 6d 65 6d  r call to.** mem
8f6d0 73 79 73 35 52 6f 75 6e 64 28 29 2e 20 20 48 65  sys5Round().  He
8f6e0 6e 63 65 20 6e 42 79 74 65 73 20 69 73 20 61 6c  nce nBytes is al
8f6f0 77 61 79 73 20 61 20 6e 6f 6e 2d 6e 65 67 61 74  ways a non-negat
8f700 69 76 65 20 70 6f 77 65 72 0a 2a 2a 20 6f 66 20  ive power.** of 
8f710 74 77 6f 2e 20 20 49 66 20 6e 42 79 74 65 73 3d  two.  If nBytes=
8f720 3d 30 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  =0 that means th
8f730 61 74 20 61 6e 20 6f 76 65 72 73 69 7a 65 20 61  at an oversize a
8f740 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 28 61 6e  llocation.** (an
8f750 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67   allocation larg
8f760 65 72 20 74 68 61 6e 20 30 78 34 30 30 30 30 30  er than 0x400000
8f770 30 30 29 20 77 61 73 20 72 65 71 75 65 73 74 65  00) was requeste
8f780 64 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 72 6f  d and this.** ro
8f790 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 72 65 74  utine should ret
8f7a0 75 72 6e 20 30 20 77 69 74 68 6f 75 74 20 66 72  urn 0 without fr
8f7b0 65 65 69 6e 67 20 70 50 72 69 6f 72 2e 0a 2a 2f  eeing pPrior..*/
8f7c0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65  .static void *me
8f7d0 6d 73 79 73 35 52 65 61 6c 6c 6f 63 28 76 6f 69  msys5Realloc(voi
8f7e0 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e  d *pPrior, int n
8f7f0 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f  Bytes){.  int nO
8f800 6c 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20  ld;.  void *p;. 
8f810 20 61 73 73 65 72 74 28 20 70 50 72 69 6f 72 21   assert( pPrior!
8f820 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
8f830 28 6e 42 79 74 65 73 26 28 6e 42 79 74 65 73 2d  (nBytes&(nBytes-
8f840 31 29 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  1))==0 );.  asse
8f850 72 74 28 20 6e 42 79 74 65 73 3e 3d 30 20 29 3b  rt( nBytes>=0 );
8f860 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3d 3d 30  .  if( nBytes==0
8f870 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
8f880 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20 3d 20 6d  ;.  }.  nOld = m
8f890 65 6d 73 79 73 35 53 69 7a 65 28 70 50 72 69 6f  emsys5Size(pPrio
8f8a0 72 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 73  r);.  if( nBytes
8f8b0 3c 3d 6e 4f 6c 64 20 29 7b 0a 20 20 20 20 72 65  <=nOld ){.    re
8f8c0 74 75 72 6e 20 70 50 72 69 6f 72 3b 0a 20 20 7d  turn pPrior;.  }
8f8d0 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28  .  memsys5Enter(
8f8e0 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73 79 73 35  );.  p = memsys5
8f8f0 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 6e 42 79  MallocUnsafe(nBy
8f900 74 65 73 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  tes);.  if( p ){
8f910 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20 70  .    memcpy(p, p
8f920 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20 20  Prior, nOld);.  
8f930 20 20 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73    memsys5FreeUns
8f940 61 66 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d  afe(pPrior);.  }
8f950 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28  .  memsys5Leave(
8f960 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d  );.  return p;.}
8f970 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70  ../*.** Round up
8f980 20 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20   a request size 
8f990 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69  to the next vali
8f9a0 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a  d allocation siz
8f9b0 65 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 61 6c  e.  If.** the al
8f9c0 6c 6f 63 61 74 69 6f 6e 20 69 73 20 74 6f 6f 20  location is too 
8f9d0 6c 61 72 67 65 20 74 6f 20 62 65 20 68 61 6e 64  large to be hand
8f9e0 6c 65 64 20 62 79 20 74 68 69 73 20 61 6c 6c 6f  led by this allo
8f9f0 63 61 74 69 6f 6e 20 73 79 73 74 65 6d 2c 0a 2a  cation system,.*
8fa00 2a 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a  * return 0..**.*
8fa10 2a 20 41 6c 6c 20 61 6c 6c 6f 63 61 74 69 6f 6e  * All allocation
8fa20 73 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65  s must be a powe
8fa30 72 20 6f 66 20 74 77 6f 20 61 6e 64 20 6d 75 73  r of two and mus
8fa40 74 20 62 65 20 65 78 70 72 65 73 73 65 64 20 62  t be expressed b
8fa50 79 20 61 0a 2a 2a 20 33 32 2d 62 69 74 20 73 69  y a.** 32-bit si
8fa60 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 20 20 48  gned integer.  H
8fa70 65 6e 63 65 20 74 68 65 20 6c 61 72 67 65 73 74  ence the largest
8fa80 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 30   allocation is 0
8fa90 78 34 30 30 30 30 30 30 30 0a 2a 2a 20 6f 72 20  x40000000.** or 
8faa0 31 30 37 33 37 34 31 38 32 34 20 62 79 74 65 73  1073741824 bytes
8fab0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
8fac0 6d 65 6d 73 79 73 35 52 6f 75 6e 64 75 70 28 69  memsys5Roundup(i
8fad0 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 46 75  nt n){.  int iFu
8fae0 6c 6c 53 7a 3b 0a 20 20 69 66 28 20 6e 20 3e 20  llSz;.  if( n > 
8faf0 30 78 34 30 30 30 30 30 30 30 20 29 20 72 65 74  0x40000000 ) ret
8fb00 75 72 6e 20 30 3b 0a 20 20 66 6f 72 28 69 46 75  urn 0;.  for(iFu
8fb10 6c 6c 53 7a 3d 6d 65 6d 35 2e 73 7a 41 74 6f 6d  llSz=mem5.szAtom
8fb20 3b 20 69 46 75 6c 6c 53 7a 3c 6e 3b 20 69 46 75  ; iFullSz<n; iFu
8fb30 6c 6c 53 7a 20 2a 3d 20 32 29 3b 0a 20 20 72 65  llSz *= 2);.  re
8fb40 74 75 72 6e 20 69 46 75 6c 6c 53 7a 3b 0a 7d 0a  turn iFullSz;.}.
8fb50 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
8fb60 65 20 63 65 69 6c 69 6e 67 20 6f 66 20 74 68 65  e ceiling of the
8fb70 20 6c 6f 67 61 72 69 74 68 6d 20 62 61 73 65 20   logarithm base 
8fb80 32 20 6f 66 20 69 56 61 6c 75 65 2e 0a 2a 2a 0a  2 of iValue..**.
8fb90 2a 2a 20 45 78 61 6d 70 6c 65 73 3a 20 20 20 6d  ** Examples:   m
8fba0 65 6d 73 79 73 35 4c 6f 67 28 31 29 20 2d 3e 20  emsys5Log(1) -> 
8fbb0 30 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  0.**            
8fbc0 20 6d 65 6d 73 79 73 35 4c 6f 67 28 32 29 20 2d   memsys5Log(2) -
8fbd0 3e 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  > 1.**          
8fbe0 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28 34 29     memsys5Log(4)
8fbf0 20 2d 3e 20 32 0a 2a 2a 20 20 20 20 20 20 20 20   -> 2.**        
8fc00 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f 67 28       memsys5Log(
8fc10 35 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20 20 20  5) -> 3.**      
8fc20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35 4c 6f         memsys5Lo
8fc30 67 28 38 29 20 2d 3e 20 33 0a 2a 2a 20 20 20 20  g(8) -> 3.**    
8fc40 20 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 35           memsys5
8fc50 4c 6f 67 28 39 29 20 2d 3e 20 34 0a 2a 2f 0a 73  Log(9) -> 4.*/.s
8fc60 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73  tatic int memsys
8fc70 35 4c 6f 67 28 69 6e 74 20 69 56 61 6c 75 65 29  5Log(int iValue)
8fc80 7b 0a 20 20 69 6e 74 20 69 4c 6f 67 3b 0a 20 20  {.  int iLog;.  
8fc90 66 6f 72 28 69 4c 6f 67 3d 30 3b 20 28 31 3c 3c  for(iLog=0; (1<<
8fca0 69 4c 6f 67 29 3c 69 56 61 6c 75 65 3b 20 69 4c  iLog)<iValue; iL
8fcb0 6f 67 2b 2b 29 3b 0a 20 20 72 65 74 75 72 6e 20  og++);.  return 
8fcc0 69 4c 6f 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  iLog;.}../*.** I
8fcd0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65  nitialize the me
8fce0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 2e 0a  mory allocator..
8fcf0 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
8fd00 6e 65 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64  ne is not thread
8fd10 73 61 66 65 2e 20 20 54 68 65 20 63 61 6c 6c 65  safe.  The calle
8fd20 72 20 6d 75 73 74 20 62 65 20 68 6f 6c 64 69 6e  r must be holdin
8fd30 67 20 61 20 6d 75 74 65 78 0a 2a 2a 20 74 6f 20  g a mutex.** to 
8fd40 70 72 65 76 65 6e 74 20 6d 75 6c 74 69 70 6c 65  prevent multiple
8fd50 20 74 68 72 65 61 64 73 20 66 72 6f 6d 20 65 6e   threads from en
8fd60 74 65 72 69 6e 67 20 61 74 20 74 68 65 20 73 61  tering at the sa
8fd70 6d 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73 74 61 74  me time..*/.stat
8fd80 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 49 6e  ic int memsys5In
8fd90 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64  it(void *NotUsed
8fda0 29 7b 0a 20 20 69 6e 74 20 69 69 3b 20 20 20 20  ){.  int ii;    
8fdb0 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
8fdc0 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  counter */.  int
8fdd0 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20   nByte;         
8fde0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
8fdf0 65 73 20 6f 66 20 6d 65 6d 6f 72 79 20 61 76 61  es of memory ava
8fe00 69 6c 61 62 6c 65 20 74 6f 20 74 68 69 73 20 61  ilable to this a
8fe10 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20 20 75 38  llocator */.  u8
8fe20 20 2a 7a 42 79 74 65 3b 20 20 20 20 20 20 20 20   *zByte;        
8fe30 20 2f 2a 20 4d 65 6d 6f 72 79 20 75 73 61 62 6c   /* Memory usabl
8fe40 65 20 62 79 20 74 68 69 73 20 61 6c 6c 6f 63 61  e by this alloca
8fe50 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 69  tor */.  int nMi
8fe60 6e 4c 6f 67 3b 20 20 20 20 20 20 20 2f 2a 20 4c  nLog;       /* L
8fe70 6f 67 20 62 61 73 65 20 32 20 6f 66 20 6d 69 6e  og base 2 of min
8fe80 69 6d 75 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  imum allocation 
8fe90 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20 2a 2f  size in bytes */
8fea0 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20  .  int iOffset; 
8feb0 20 20 20 20 20 20 2f 2a 20 41 6e 20 6f 66 66 73        /* An offs
8fec0 65 74 20 69 6e 74 6f 20 6d 65 6d 35 2e 61 43 74  et into mem5.aCt
8fed0 72 6c 5b 5d 20 2a 2f 0a 0a 20 20 55 4e 55 53 45  rl[] */..  UNUSE
8fee0 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
8fef0 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 46 6f 72 20  sed);..  /* For 
8ff00 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20  the purposes of 
8ff10 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 64 69  this routine, di
8ff20 73 61 62 6c 65 20 74 68 65 20 6d 75 74 65 78 20  sable the mutex 
8ff30 2a 2f 0a 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20  */.  mem5.mutex 
8ff40 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 73  = 0;..  /* The s
8ff50 69 7a 65 20 6f 66 20 61 20 4d 65 6d 35 4c 69 6e  ize of a Mem5Lin
8ff60 6b 20 6f 62 6a 65 63 74 20 6d 75 73 74 20 62 65  k object must be
8ff70 20 61 20 70 6f 77 65 72 20 6f 66 20 74 77 6f 2e   a power of two.
8ff80 20 20 56 65 72 69 66 79 20 74 68 61 74 0a 20 20    Verify that.  
8ff90 2a 2a 20 74 68 69 73 20 69 73 20 63 61 73 65 2e  ** this is case.
8ffa0 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
8ffb0 28 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69 6e 6b  (sizeof(Mem5Link
8ffc0 29 26 28 73 69 7a 65 6f 66 28 4d 65 6d 35 4c 69  )&(sizeof(Mem5Li
8ffd0 6e 6b 29 2d 31 29 29 3d 3d 30 20 29 3b 0a 0a 20  nk)-1))==0 );.. 
8ffe0 20 6e 42 79 74 65 20 3d 20 73 71 6c 69 74 65 33   nByte = sqlite3
8fff0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 48 65  GlobalConfig.nHe
90000 61 70 3b 0a 20 20 7a 42 79 74 65 20 3d 20 28 75  ap;.  zByte = (u
90010 38 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  8*)sqlite3Global
90020 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a 20 20  Config.pHeap;.  
90030 61 73 73 65 72 74 28 20 7a 42 79 74 65 21 3d 30  assert( zByte!=0
90040 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f   );  /* sqlite3_
90050 63 6f 6e 66 69 67 28 29 20 64 6f 65 73 20 6e 6f  config() does no
90060 74 20 61 6c 6c 6f 77 20 6f 74 68 65 72 77 69 73  t allow otherwis
90070 65 20 2a 2f 0a 0a 20 20 6e 4d 69 6e 4c 6f 67 20  e */..  nMinLog 
90080 3d 20 6d 65 6d 73 79 73 35 4c 6f 67 28 73 71 6c  = memsys5Log(sql
90090 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
900a0 2e 6d 6e 52 65 71 29 3b 0a 20 20 6d 65 6d 35 2e  .mnReq);.  mem5.
900b0 73 7a 41 74 6f 6d 20 3d 20 28 31 3c 3c 6e 4d 69  szAtom = (1<<nMi
900c0 6e 4c 6f 67 29 3b 0a 20 20 77 68 69 6c 65 28 20  nLog);.  while( 
900d0 28 69 6e 74 29 73 69 7a 65 6f 66 28 4d 65 6d 35  (int)sizeof(Mem5
900e0 4c 69 6e 6b 29 3e 6d 65 6d 35 2e 73 7a 41 74 6f  Link)>mem5.szAto
900f0 6d 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 73 7a  m ){.    mem5.sz
90100 41 74 6f 6d 20 3d 20 6d 65 6d 35 2e 73 7a 41 74  Atom = mem5.szAt
90110 6f 6d 20 3c 3c 20 31 3b 0a 20 20 7d 0a 0a 20 20  om << 1;.  }..  
90120 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 3d 20 28 6e  mem5.nBlock = (n
90130 42 79 74 65 20 2f 20 28 6d 65 6d 35 2e 73 7a 41  Byte / (mem5.szA
90140 74 6f 6d 2b 73 69 7a 65 6f 66 28 75 38 29 29 29  tom+sizeof(u8)))
90150 3b 0a 20 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 3d  ;.  mem5.zPool =
90160 20 7a 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e 61   zByte;.  mem5.a
90170 43 74 72 6c 20 3d 20 28 75 38 20 2a 29 26 6d 65  Ctrl = (u8 *)&me
90180 6d 35 2e 7a 50 6f 6f 6c 5b 6d 65 6d 35 2e 6e 42  m5.zPool[mem5.nB
90190 6c 6f 63 6b 2a 6d 65 6d 35 2e 73 7a 41 74 6f 6d  lock*mem5.szAtom
901a0 5d 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  ];..  for(ii=0; 
901b0 69 69 3c 3d 4c 4f 47 4d 41 58 3b 20 69 69 2b 2b  ii<=LOGMAX; ii++
901c0 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 61 69 46 72  ){.    mem5.aiFr
901d0 65 65 6c 69 73 74 5b 69 69 5d 20 3d 20 2d 31 3b  eelist[ii] = -1;
901e0 0a 20 20 7d 0a 0a 20 20 69 4f 66 66 73 65 74 20  .  }..  iOffset 
901f0 3d 20 30 3b 0a 20 20 66 6f 72 28 69 69 3d 4c 4f  = 0;.  for(ii=LO
90200 47 4d 41 58 3b 20 69 69 3e 3d 30 3b 20 69 69 2d  GMAX; ii>=0; ii-
90210 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c  -){.    int nAll
90220 6f 63 20 3d 20 28 31 3c 3c 69 69 29 3b 0a 20 20  oc = (1<<ii);.  
90230 20 20 69 66 28 20 28 69 4f 66 66 73 65 74 2b 6e    if( (iOffset+n
90240 41 6c 6c 6f 63 29 3c 3d 6d 65 6d 35 2e 6e 42 6c  Alloc)<=mem5.nBl
90250 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  ock ){.      mem
90260 35 2e 61 43 74 72 6c 5b 69 4f 66 66 73 65 74 5d  5.aCtrl[iOffset]
90270 20 3d 20 69 69 20 7c 20 43 54 52 4c 5f 46 52 45   = ii | CTRL_FRE
90280 45 3b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73 35  E;.      memsys5
90290 4c 69 6e 6b 28 69 4f 66 66 73 65 74 2c 20 69 69  Link(iOffset, ii
902a0 29 3b 0a 20 20 20 20 20 20 69 4f 66 66 73 65 74  );.      iOffset
902b0 20 2b 3d 20 6e 41 6c 6c 6f 63 3b 0a 20 20 20 20   += nAlloc;.    
902c0 7d 0a 20 20 20 20 61 73 73 65 72 74 28 28 69 4f  }.    assert((iO
902d0 66 66 73 65 74 2b 6e 41 6c 6c 6f 63 29 3e 6d 65  ffset+nAlloc)>me
902e0 6d 35 2e 6e 42 6c 6f 63 6b 29 3b 0a 20 20 7d 0a  m5.nBlock);.  }.
902f0 0a 20 20 2f 2a 20 49 66 20 61 20 6d 75 74 65 78  .  /* If a mutex
90300 20 69 73 20 72 65 71 75 69 72 65 64 20 66 6f 72   is required for
90310 20 6e 6f 72 6d 61 6c 20 6f 70 65 72 61 74 69 6f   normal operatio
90320 6e 2c 20 61 6c 6c 6f 63 61 74 65 20 6f 6e 65 20  n, allocate one 
90330 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
90340 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65  GlobalConfig.bMe
90350 6d 73 74 61 74 3d 3d 30 20 29 7b 0a 20 20 20 20  mstat==0 ){.    
90360 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20 73 71 6c  mem5.mutex = sql
90370 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
90380 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
90390 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a 0a 20 20  IC_MEM);.  }..  
903a0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
903b0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69  ;.}../*.** Deini
903c0 74 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64  tialize this mod
903d0 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ule..*/.static v
903e0 6f 69 64 20 6d 65 6d 73 79 73 35 53 68 75 74 64  oid memsys5Shutd
903f0 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65  own(void *NotUse
90400 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  d){.  UNUSED_PAR
90410 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
90420 0a 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20  .  mem5.mutex = 
90430 30 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a  0;.  return;.}..
90440 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
90450 53 54 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68  ST./*.** Open th
90460 65 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64  e file indicated
90470 20 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67   and write a log
90480 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20   of all unfreed 
90490 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63  memory .** alloc
904a0 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74  ations into that
904b0 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   log..*/.SQLITE_
904c0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
904d0 69 74 65 33 4d 65 6d 73 79 73 35 44 75 6d 70 28  ite3Memsys5Dump(
904e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
904f0 65 6e 61 6d 65 29 7b 0a 20 20 46 49 4c 45 20 2a  ename){.  FILE *
90500 6f 75 74 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c  out;.  int i, j,
90510 20 6e 3b 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f   n;.  int nMinLo
90520 67 3b 0a 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e  g;..  if( zFilen
90530 61 6d 65 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e  ame==0 || zFilen
90540 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20  ame[0]==0 ){.   
90550 20 6f 75 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20   out = stdout;. 
90560 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20   }else{.    out 
90570 3d 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d  = fopen(zFilenam
90580 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 66 28  e, "w");.    if(
90590 20 6f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20   out==0 ){.     
905a0 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c   fprintf(stderr,
905b0 20 22 2a 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f   "** Unable to o
905c0 75 74 70 75 74 20 6d 65 6d 6f 72 79 20 64 65 62  utput memory deb
905d0 75 67 20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25  ug output log: %
905e0 73 20 2a 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20  s **\n",.       
905f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a                 z
90600 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20  Filename);.     
90610 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
90620 20 7d 0a 20 20 6d 65 6d 73 79 73 35 45 6e 74 65   }.  memsys5Ente
90630 72 28 29 3b 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d  r();.  nMinLog =
90640 20 6d 65 6d 73 79 73 35 4c 6f 67 28 6d 65 6d 35   memsys5Log(mem5
90650 2e 73 7a 41 74 6f 6d 29 3b 0a 20 20 66 6f 72 28  .szAtom);.  for(
90660 69 3d 30 3b 20 69 3c 3d 4c 4f 47 4d 41 58 20 26  i=0; i<=LOGMAX &
90670 26 20 69 2b 6e 4d 69 6e 4c 6f 67 3c 33 32 3b 20  & i+nMinLog<32; 
90680 69 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28 6e 3d  i++){.    for(n=
90690 30 2c 20 6a 3d 6d 65 6d 35 2e 61 69 46 72 65 65  0, j=mem5.aiFree
906a0 6c 69 73 74 5b 69 5d 3b 20 6a 3e 3d 30 3b 20 6a  list[i]; j>=0; j
906b0 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 6a 29 2d 3e   = MEM5LINK(j)->
906c0 6e 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20  next, n++){}.   
906d0 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 66   fprintf(out, "f
906e0 72 65 65 6c 69 73 74 20 69 74 65 6d 73 20 6f 66  reelist items of
906f0 20 73 69 7a 65 20 25 64 3a 20 25 64 5c 6e 22 2c   size %d: %d\n",
90700 20 6d 65 6d 35 2e 73 7a 41 74 6f 6d 20 3c 3c 20   mem5.szAtom << 
90710 69 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 66 70 72  i, n);.  }.  fpr
90720 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e  intf(out, "mem5.
90730 6e 41 6c 6c 6f 63 20 20 20 20 20 20 20 3d 20 25  nAlloc       = %
90740 6c 6c 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6e 41 6c  llu\n", mem5.nAl
90750 6c 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28  loc);.  fprintf(
90760 6f 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c  out, "mem5.total
90770 41 6c 6c 6f 63 20 20 20 3d 20 25 6c 6c 75 5c 6e  Alloc   = %llu\n
90780 22 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c  ", mem5.totalAll
90790 6f 63 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  oc);.  fprintf(o
907a0 75 74 2c 20 22 6d 65 6d 35 2e 74 6f 74 61 6c 45  ut, "mem5.totalE
907b0 78 63 65 73 73 20 20 3d 20 25 6c 6c 75 5c 6e 22  xcess  = %llu\n"
907c0 2c 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65  , mem5.totalExce
907d0 73 73 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  ss);.  fprintf(o
907e0 75 74 2c 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e  ut, "mem5.curren
907f0 74 4f 75 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20  tOut   = %u\n", 
90800 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 29  mem5.currentOut)
90810 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c  ;.  fprintf(out,
90820 20 22 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f   "mem5.currentCo
90830 75 6e 74 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d  unt = %u\n", mem
90840 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 29 3b  5.currentCount);
90850 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20  .  fprintf(out, 
90860 22 6d 65 6d 35 2e 6d 61 78 4f 75 74 20 20 20 20  "mem5.maxOut    
90870 20 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35     = %u\n", mem5
90880 2e 6d 61 78 4f 75 74 29 3b 0a 20 20 66 70 72 69  .maxOut);.  fpri
90890 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35 2e 6d  ntf(out, "mem5.m
908a0 61 78 43 6f 75 6e 74 20 20 20 20 20 3d 20 25 75  axCount     = %u
908b0 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 43 6f 75  \n", mem5.maxCou
908c0 6e 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  nt);.  fprintf(o
908d0 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 52 65 71  ut, "mem5.maxReq
908e0 75 65 73 74 20 20 20 3d 20 25 75 5c 6e 22 2c 20  uest   = %u\n", 
908f0 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 29  mem5.maxRequest)
90900 3b 0a 20 20 6d 65 6d 73 79 73 35 4c 65 61 76 65  ;.  memsys5Leave
90910 28 29 3b 0a 20 20 69 66 28 20 6f 75 74 3d 3d 73  ();.  if( out==s
90920 74 64 6f 75 74 20 29 7b 0a 20 20 20 20 66 66 6c  tdout ){.    ffl
90930 75 73 68 28 73 74 64 6f 75 74 29 3b 0a 20 20 7d  ush(stdout);.  }
90940 65 6c 73 65 7b 0a 20 20 20 20 66 63 6c 6f 73 65  else{.    fclose
90950 28 6f 75 74 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  (out);.  }.}.#en
90960 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  dif../*.** This 
90970 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f  routine is the o
90980 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74  nly routine in t
90990 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78  his file with ex
909a0 74 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61  ternal .** linka
909b0 67 65 2e 20 49 74 20 72 65 74 75 72 6e 73 20 61  ge. It returns a
909c0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74   pointer to a st
909d0 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 65 6d  atic sqlite3_mem
909e0 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 73 74 72 75  _methods.** stru
909f0 63 74 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74  ct populated wit
90a00 68 20 74 68 65 20 6d 65 6d 73 79 73 35 20 6d 65  h the memsys5 me
90a10 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  thods..*/.SQLITE
90a20 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73  _PRIVATE const s
90a30 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
90a40 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65  ds *sqlite3MemGe
90a50 74 4d 65 6d 73 79 73 35 28 76 6f 69 64 29 7b 0a  tMemsys5(void){.
90a60 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
90a70 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
90a80 64 73 20 6d 65 6d 73 79 73 35 4d 65 74 68 6f 64  ds memsys5Method
90a90 73 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79  s = {.     memsy
90aa0 73 35 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d  s5Malloc,.     m
90ab0 65 6d 73 79 73 35 46 72 65 65 2c 0a 20 20 20 20  emsys5Free,.    
90ac0 20 6d 65 6d 73 79 73 35 52 65 61 6c 6c 6f 63 2c   memsys5Realloc,
90ad0 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 53 69 7a  .     memsys5Siz
90ae0 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52  e,.     memsys5R
90af0 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d  oundup,.     mem
90b00 73 79 73 35 49 6e 69 74 2c 0a 20 20 20 20 20 6d  sys5Init,.     m
90b10 65 6d 73 79 73 35 53 68 75 74 64 6f 77 6e 2c 0a  emsys5Shutdown,.
90b20 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65       0.  };.  re
90b30 74 75 72 6e 20 26 6d 65 6d 73 79 73 35 4d 65 74  turn &memsys5Met
90b40 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  hods;.}..#endif 
90b50 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  /* SQLITE_ENABLE
90b60 5f 4d 45 4d 53 59 53 35 20 2a 2f 0a 0a 2f 2a 2a  _MEMSYS5 */../**
90b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
90b80 20 6f 66 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a   of mem5.c *****
90b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
90bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
90bd0 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 63 20  in file mutex.c 
90be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
90c10 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31  ** 2007 August 1
90c20 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  4.**.** The auth
90c30 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
90c40 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
90c50 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
90c60 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
90c70 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
90c80 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
90c90 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
90ca0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
90cb0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
90cc0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
90cd0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
90ce0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
90cf0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
90d00 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
90d10 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
90d20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
90d30 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
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 2a 2a 2a  ****************
90d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
90d80 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
90d90 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e  ntains the C fun
90da0 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c  ctions that impl
90db0 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a  ement mutexes..*
90dc0 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
90dd0 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61  ontains code tha
90de0 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 61 63 72 6f  t is common acro
90df0 73 73 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70  ss all mutex imp
90e00 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 0a 2a  lementations...*
90e10 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 2e  *.** $Id: mutex.
90e20 63 2c 76 20 31 2e 33 31 20 32 30 30 39 2f 30 37  c,v 1.31 2009/07
90e30 2f 31 36 20 31 38 3a 32 31 3a 31 38 20 64 72 68  /16 18:21:18 drh
90e40 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 64   Exp $.*/..#if d
90e50 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
90e60 42 55 47 29 20 26 26 20 21 64 65 66 69 6e 65 64  BUG) && !defined
90e70 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d  (SQLITE_MUTEX_OM
90e80 49 54 29 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 64 65  IT)./*.** For de
90e90 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73 65 73  bugging purposes
90ea0 2c 20 72 65 63 6f 72 64 20 77 68 65 6e 20 74 68  , record when th
90eb0 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65  e mutex subsyste
90ec0 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  m is initialized
90ed0 0a 2a 2a 20 61 6e 64 20 75 6e 69 6e 69 74 69 61  .** and uninitia
90ee0 6c 69 7a 65 64 20 73 6f 20 74 68 61 74 20 77 65  lized so that we
90ef0 20 63 61 6e 20 61 73 73 65 72 74 28 29 20 69 66   can assert() if
90f00 20 74 68 65 72 65 20 69 73 20 61 6e 20 61 74 74   there is an att
90f10 65 6d 70 74 20 74 6f 0a 2a 2a 20 61 6c 6c 6f 63  empt to.** alloc
90f20 61 74 65 20 61 20 6d 75 74 65 78 20 77 68 69 6c  ate a mutex whil
90f30 65 20 74 68 65 20 73 79 73 74 65 6d 20 69 73 20  e the system is 
90f40 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a  uninitialized..*
90f50 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f  /.static SQLITE_
90f60 57 53 44 20 69 6e 74 20 6d 75 74 65 78 49 73 49  WSD int mutexIsI
90f70 6e 69 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 20  nit = 0;.#endif 
90f80 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  /* SQLITE_DEBUG 
90f90 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  */...#ifndef SQL
90fa0 49 54 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 2f  ITE_MUTEX_OMIT./
90fb0 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
90fc0 74 68 65 20 6d 75 74 65 78 20 73 79 73 74 65 6d  the mutex system
90fd0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
90fe0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  ATE int sqlite3M
90ff0 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20  utexInit(void){ 
91000 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
91010 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 73 71 6c  TE_OK;.  if( sql
91020 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
91030 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20  .bCoreMutex ){. 
91040 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47     if( !sqlite3G
91050 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65  lobalConfig.mute
91060 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 29 7b  x.xMutexAlloc ){
91070 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
91080 20 78 4d 75 74 65 78 41 6c 6c 6f 63 20 6d 65 74   xMutexAlloc met
91090 68 6f 64 20 68 61 73 20 6e 6f 74 20 62 65 65 6e  hod has not been
910a0 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 75   set, then the u
910b0 73 65 72 20 64 69 64 20 6e 6f 74 0a 20 20 20 20  ser did not.    
910c0 20 20 2a 2a 20 69 6e 73 74 61 6c 6c 20 61 20 6d    ** install a m
910d0 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74  utex implementat
910e0 69 6f 6e 20 76 69 61 20 73 71 6c 69 74 65 33 5f  ion via sqlite3_
910f0 63 6f 6e 66 69 67 28 29 20 70 72 69 6f 72 20 74  config() prior t
91100 6f 20 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69  o .      ** sqli
91110 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
91120 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 2e 20 54   being called. T
91130 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70 69 65 73  his block copies
91140 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 20 20 20   pointers to.   
91150 20 20 20 2a 2a 20 74 68 65 20 64 65 66 61 75 6c     ** the defaul
91160 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  t implementation
91170 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69 74 65   into the sqlite
91180 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 73 74  3GlobalConfig st
91190 72 75 63 74 75 72 65 2e 0a 20 20 20 20 20 20 2a  ructure..      *
911a0 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  /.      sqlite3_
911b0 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 70  mutex_methods *p
911c0 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33 44 65  From = sqlite3De
911d0 66 61 75 6c 74 4d 75 74 65 78 28 29 3b 0a 20 20  faultMutex();.  
911e0 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
911f0 78 5f 6d 65 74 68 6f 64 73 20 2a 70 54 6f 20 3d  x_methods *pTo =
91200 20 26 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43   &sqlite3GlobalC
91210 6f 6e 66 69 67 2e 6d 75 74 65 78 3b 0a 0a 20 20  onfig.mutex;..  
91220 20 20 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20      memcpy(pTo, 
91230 70 46 72 6f 6d 2c 20 6f 66 66 73 65 74 6f 66 28  pFrom, offsetof(
91240 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
91250 74 68 6f 64 73 2c 20 78 4d 75 74 65 78 41 6c 6c  thods, xMutexAll
91260 6f 63 29 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63  oc));.      memc
91270 70 79 28 26 70 54 6f 2d 3e 78 4d 75 74 65 78 46  py(&pTo->xMutexF
91280 72 65 65 2c 20 26 70 46 72 6f 6d 2d 3e 78 4d 75  ree, &pFrom->xMu
91290 74 65 78 46 72 65 65 2c 0a 20 20 20 20 20 20 20  texFree,.       
912a0 20 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70 54        sizeof(*pT
912b0 6f 29 20 2d 20 6f 66 66 73 65 74 6f 66 28 73 71  o) - offsetof(sq
912c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
912d0 6f 64 73 2c 20 78 4d 75 74 65 78 46 72 65 65 29  ods, xMutexFree)
912e0 29 3b 0a 20 20 20 20 20 20 70 54 6f 2d 3e 78 4d  );.      pTo->xM
912f0 75 74 65 78 41 6c 6c 6f 63 20 3d 20 70 46 72 6f  utexAlloc = pFro
91300 6d 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 3b 0a  m->xMutexAlloc;.
91310 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73      }.    rc = s
91320 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
91330 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 49  ig.mutex.xMutexI
91340 6e 69 74 28 29 3b 0a 20 20 7d 0a 0a 23 69 66 64  nit();.  }..#ifd
91350 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
91360 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d 75    GLOBAL(int, mu
91370 74 65 78 49 73 49 6e 69 74 29 20 3d 20 31 3b 0a  texIsInit) = 1;.
91380 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e  #endif..  return
91390 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68   rc;.}../*.** Sh
913a0 75 74 64 6f 77 6e 20 74 68 65 20 6d 75 74 65 78  utdown the mutex
913b0 20 73 79 73 74 65 6d 2e 20 54 68 69 73 20 63 61   system. This ca
913c0 6c 6c 20 66 72 65 65 73 20 72 65 73 6f 75 72 63  ll frees resourc
913d0 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a  es allocated by.
913e0 2a 2a 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49  ** sqlite3MutexI
913f0 6e 69 74 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  nit()..*/.SQLITE
91400 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
91410 69 74 65 33 4d 75 74 65 78 45 6e 64 28 76 6f 69  ite3MutexEnd(voi
91420 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  d){.  int rc = S
91430 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
91440 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
91450 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78  fig.mutex.xMutex
91460 45 6e 64 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  End ){.    rc = 
91470 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
91480 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78  fig.mutex.xMutex
91490 45 6e 64 28 29 3b 0a 20 20 7d 0a 0a 23 69 66 64  End();.  }..#ifd
914a0 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
914b0 20 20 47 4c 4f 42 41 4c 28 69 6e 74 2c 20 6d 75    GLOBAL(int, mu
914c0 74 65 78 49 73 49 6e 69 74 29 20 3d 20 30 3b 0a  texIsInit) = 0;.
914d0 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e  #endif..  return
914e0 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
914f0 74 72 69 65 76 65 20 61 20 70 6f 69 6e 74 65 72  trieve a pointer
91500 20 74 6f 20 61 20 73 74 61 74 69 63 20 6d 75 74   to a static mut
91510 65 78 20 6f 72 20 61 6c 6c 6f 63 61 74 65 20 61  ex or allocate a
91520 20 6e 65 77 20 64 79 6e 61 6d 69 63 20 6f 6e 65   new dynamic one
91530 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
91540 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73  sqlite3_mutex *s
91550 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
91560 6f 63 28 69 6e 74 20 69 64 29 7b 0a 23 69 66 6e  oc(int id){.#ifn
91570 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
91580 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73  AUTOINIT.  if( s
91590 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
915a0 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  e() ) return 0;.
915b0 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
915c0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
915d0 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78  fig.mutex.xMutex
915e0 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a 53 51  Alloc(id);.}..SQ
915f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
91600 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69  ite3_mutex *sqli
91610 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e  te3MutexAlloc(in
91620 74 20 69 64 29 7b 0a 20 20 69 66 28 20 21 73 71  t id){.  if( !sq
91630 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
91640 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a  g.bCoreMutex ){.
91650 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
91660 7d 0a 20 20 61 73 73 65 72 74 28 20 47 4c 4f 42  }.  assert( GLOB
91670 41 4c 28 69 6e 74 2c 20 6d 75 74 65 78 49 73 49  AL(int, mutexIsI
91680 6e 69 74 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  nit) );.  return
91690 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
916a0 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65  nfig.mutex.xMute
916b0 78 41 6c 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a 2f  xAlloc(id);.}../
916c0 2a 0a 2a 2a 20 46 72 65 65 20 61 20 64 79 6e 61  *.** Free a dyna
916d0 6d 69 63 20 6d 75 74 65 78 2e 0a 2a 2f 0a 53 51  mic mutex..*/.SQ
916e0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
916f0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65  lite3_mutex_free
91700 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
91710 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  p){.  if( p ){. 
91720 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
91730 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75  Config.mutex.xMu
91740 74 65 78 46 72 65 65 28 70 29 3b 0a 20 20 7d 0a  texFree(p);.  }.
91750 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20  }../*.** Obtain 
91760 74 68 65 20 6d 75 74 65 78 20 70 2e 20 49 66 20  the mutex p. If 
91770 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 65 61  some other threa
91780 64 20 61 6c 72 65 61 64 79 20 68 61 73 20 74 68  d already has th
91790 65 20 6d 75 74 65 78 2c 20 62 6c 6f 63 6b 0a 2a  e mutex, block.*
917a0 2a 20 75 6e 74 69 6c 20 69 74 20 63 61 6e 20 62  * until it can b
917b0 65 20 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f 0a 53  e obtained..*/.S
917c0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
917d0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
917e0 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  er(sqlite3_mutex
917f0 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b   *p){.  if( p ){
91800 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
91810 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78  alConfig.mutex.x
91820 4d 75 74 65 78 45 6e 74 65 72 28 70 29 3b 0a 20  MutexEnter(p);. 
91830 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 62 74 61   }.}../*.** Obta
91840 69 6e 20 74 68 65 20 6d 75 74 65 78 20 70 2e 20  in the mutex p. 
91850 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72  If successful, r
91860 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
91870 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61   Otherwise, if a
91880 6e 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65 61 64  nother.** thread
91890 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74 65 78   holds the mutex
918a0 20 61 6e 64 20 69 74 20 63 61 6e 6e 6f 74 20 62   and it cannot b
918b0 65 20 6f 62 74 61 69 6e 65 64 2c 20 72 65 74 75  e obtained, retu
918c0 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a  rn SQLITE_BUSY..
918d0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
918e0 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  t sqlite3_mutex_
918f0 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  try(sqlite3_mute
91900 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20  x *p){.  int rc 
91910 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
91920 66 28 20 70 20 29 7b 0a 20 20 20 20 72 65 74 75  f( p ){.    retu
91930 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  rn sqlite3Global
91940 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75  Config.mutex.xMu
91950 74 65 78 54 72 79 28 70 29 3b 0a 20 20 7d 0a 20  texTry(p);.  }. 
91960 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
91970 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
91980 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72  _mutex_leave() r
91990 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d  outine exits a m
919a0 75 74 65 78 20 74 68 61 74 20 77 61 73 20 70 72  utex that was pr
919b0 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 65 6e 74 65  eviously.** ente
919c0 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20  red by the same 
919d0 74 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 68  thread.  The beh
919e0 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e  avior is undefin
919f0 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20  ed if the mutex 
91a00 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72 72 65  .** is not curre
91a10 6e 74 6c 79 20 65 6e 74 65 72 65 64 2e 20 49 66  ntly entered. If
91a20 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20   a NULL pointer 
91a30 69 73 20 70 61 73 73 65 64 20 61 73 20 61 6e 20  is passed as an 
91a40 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 68 69 73  argument.** this
91a50 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
91a60 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  o-op..*/.SQLITE_
91a70 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
91a80 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c  _mutex_leave(sql
91a90 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
91aa0 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73    if( p ){.    s
91ab0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
91ac0 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 4c  ig.mutex.xMutexL
91ad0 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  eave(p);.  }.}..
91ae0 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f  #ifndef NDEBUG./
91af0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
91b00 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e  _mutex_held() an
91b10 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
91b20 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e  notheld() routin
91b30 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65  e are.** intende
91b40 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65  d for use inside
91b50 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
91b60 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ents..*/.SQLITE_
91b70 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
91b80 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c 69 74  mutex_held(sqlit
91b90 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
91ba0 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 73  return p==0 || s
91bb0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
91bc0 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 48  ig.mutex.xMutexH
91bd0 65 6c 64 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45  eld(p);.}.SQLITE
91be0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
91bf0 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28 73  _mutex_notheld(s
91c00 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
91c10 7b 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20  {.  return p==0 
91c20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  || sqlite3Global
91c30 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75  Config.mutex.xMu
91c40 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 3b 0a 7d  texNotheld(p);.}
91c50 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20  .#endif..#endif 
91c60 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  /* SQLITE_MUTEX_
91c70 4f 4d 49 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  OMIT */../******
91c80 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
91c90 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  mutex.c ********
91ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91cc0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
91cd0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
91ce0 69 6c 65 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63  ile mutex_noop.c
91cf0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
91d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91d10 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
91d20 30 30 38 20 4f 63 74 6f 62 65 72 20 30 37 0a 2a  008 October 07.*
91d30 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
91d40 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
91d50 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
91d60 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
91d70 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
91d80 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
91d90 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
91da0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
91db0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
91dc0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
91dd0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
91de0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
91df0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
91e00 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
91e10 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
91e20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
91e30 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
91e40 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
91e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
91e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
91e90 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
91ea0 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69  ins the C functi
91eb0 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ons that impleme
91ec0 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a  nt mutexes..**.*
91ed0 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74  * This implement
91ee0 61 74 69 6f 6e 20 69 6e 20 74 68 69 73 20 66 69  ation in this fi
91ef0 6c 65 20 64 6f 65 73 20 6e 6f 74 20 70 72 6f 76  le does not prov
91f00 69 64 65 20 61 6e 79 20 6d 75 74 75 61 6c 0a 2a  ide any mutual.*
91f10 2a 20 65 78 63 6c 75 73 69 6f 6e 20 61 6e 64 20  * exclusion and 
91f20 69 73 20 74 68 75 73 20 73 75 69 74 61 62 6c 65  is thus suitable
91f30 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e   for use only in
91f40 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a   applications.**
91f50 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65   that use SQLite
91f60 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 74 68 72   in a single thr
91f70 65 61 64 2e 20 20 54 68 65 20 72 6f 75 74 69 6e  ead.  The routin
91f80 65 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20 68 65  es defined.** he
91f90 72 65 20 61 72 65 20 70 6c 61 63 65 2d 68 6f 6c  re are place-hol
91fa0 64 65 72 73 2e 20 20 41 70 70 6c 69 63 61 74 69  ders.  Applicati
91fb0 6f 6e 73 20 63 61 6e 20 73 75 62 73 74 69 74 75  ons can substitu
91fc0 74 65 20 77 6f 72 6b 69 6e 67 0a 2a 2a 20 6d 75  te working.** mu
91fd0 74 65 78 20 72 6f 75 74 69 6e 65 73 20 61 74 20  tex routines at 
91fe0 73 74 61 72 74 2d 74 69 6d 65 20 75 73 69 6e 67  start-time using
91ff0 20 74 68 65 0a 2a 2a 0a 2a 2a 20 20 20 20 20 73   the.**.**     s
92000 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53 51  qlite3_config(SQ
92010 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45  LITE_CONFIG_MUTE
92020 58 2c 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 69 6e 74  X,...).**.** int
92030 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  erface..**.** If
92040 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 53   compiled with S
92050 51 4c 49 54 45 5f 44 45 42 55 47 2c 20 74 68 65  QLITE_DEBUG, the
92060 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 67  n additional log
92070 69 63 20 69 73 20 69 6e 73 65 72 74 65 64 0a 2a  ic is inserted.*
92080 2a 20 74 68 61 74 20 64 6f 65 73 20 65 72 72 6f  * that does erro
92090 72 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20 6d 75  r checking on mu
920a0 74 65 78 65 73 20 74 6f 20 6d 61 6b 65 20 73 75  texes to make su
920b0 72 65 20 74 68 65 79 20 61 72 65 20 62 65 69 6e  re they are bein
920c0 67 0a 2a 2a 20 63 61 6c 6c 65 64 20 63 6f 72 72  g.** called corr
920d0 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  ectly..**.** $Id
920e0 3a 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 2c 76  : mutex_noop.c,v
920f0 20 31 2e 33 20 32 30 30 38 2f 31 32 2f 30 35 20   1.3 2008/12/05 
92100 31 37 3a 31 37 3a 30 38 20 64 72 68 20 45 78 70  17:17:08 drh Exp
92110 20 24 0a 2a 2f 0a 0a 0a 23 69 66 20 64 65 66 69   $.*/...#if defi
92120 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  ned(SQLITE_MUTEX
92130 5f 4e 4f 4f 50 29 20 26 26 20 21 64 65 66 69 6e  _NOOP) && !defin
92140 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
92150 0a 2f 2a 0a 2a 2a 20 53 74 75 62 20 72 6f 75 74  ./*.** Stub rout
92160 69 6e 65 73 20 66 6f 72 20 61 6c 6c 20 6d 75 74  ines for all mut
92170 65 78 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 0a 2a  ex methods..**.*
92180 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 73 20  * This routines 
92190 70 72 6f 76 69 64 65 20 6e 6f 20 6d 75 74 75 61  provide no mutua
921a0 6c 20 65 78 63 6c 75 73 69 6f 6e 20 6f 72 20 65  l exclusion or e
921b0 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 2e 0a 2a  rror checking..*
921c0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f  /.static int noo
921d0 70 4d 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74  pMutexHeld(sqlit
921e0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20 72 65  e3_mutex *p){ re
921f0 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61 74 69 63  turn 1; }.static
92200 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 4e 6f   int noopMutexNo
92210 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75  theld(sqlite3_mu
92220 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 20  tex *p){ return 
92230 31 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  1; }.static int 
92240 6e 6f 6f 70 4d 75 74 65 78 49 6e 69 74 28 76 6f  noopMutexInit(vo
92250 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49  id){ return SQLI
92260 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20  TE_OK; }.static 
92270 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 64  int noopMutexEnd
92280 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53  (void){ return S
92290 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74  QLITE_OK; }.stat
922a0 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ic sqlite3_mutex
922b0 20 2a 6e 6f 6f 70 4d 75 74 65 78 41 6c 6c 6f 63   *noopMutexAlloc
922c0 28 69 6e 74 20 69 64 29 7b 20 72 65 74 75 72 6e  (int id){ return
922d0 20 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a   (sqlite3_mutex*
922e0 29 38 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69  )8; }.static voi
922f0 64 20 6e 6f 6f 70 4d 75 74 65 78 46 72 65 65 28  d noopMutexFree(
92300 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
92310 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61  ){ return; }.sta
92320 74 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74  tic void noopMut
92330 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f  exEnter(sqlite3_
92340 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72  mutex *p){ retur
92350 6e 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  n; }.static int 
92360 6e 6f 6f 70 4d 75 74 65 78 54 72 79 28 73 71 6c  noopMutexTry(sql
92370 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 20  ite3_mutex *p){ 
92380 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
92390 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ; }.static void 
923a0 6e 6f 6f 70 4d 75 74 65 78 4c 65 61 76 65 28 73  noopMutexLeave(s
923b0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
923c0 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 0a 53 51 4c  { return; }..SQL
923d0 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69  ITE_PRIVATE sqli
923e0 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
923f0 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c  s *sqlite3Defaul
92400 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20  tMutex(void){.  
92410 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d  static sqlite3_m
92420 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75  utex_methods sMu
92430 74 65 78 20 3d 20 7b 0a 20 20 20 20 6e 6f 6f 70  tex = {.    noop
92440 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 6e  MutexInit,.    n
92450 6f 6f 70 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20  oopMutexEnd,.   
92460 20 6e 6f 6f 70 4d 75 74 65 78 41 6c 6c 6f 63 2c   noopMutexAlloc,
92470 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 46 72  .    noopMutexFr
92480 65 65 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65  ee,.    noopMute
92490 78 45 6e 74 65 72 2c 0a 20 20 20 20 6e 6f 6f 70  xEnter,.    noop
924a0 4d 75 74 65 78 54 72 79 2c 0a 20 20 20 20 6e 6f  MutexTry,.    no
924b0 6f 70 4d 75 74 65 78 4c 65 61 76 65 2c 0a 0a 20  opMutexLeave,.. 
924c0 20 20 20 6e 6f 6f 70 4d 75 74 65 78 48 65 6c 64     noopMutexHeld
924d0 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4e  ,.    noopMutexN
924e0 6f 74 68 65 6c 64 0a 20 20 7d 3b 0a 0a 20 20 72  otheld.  };..  r
924f0 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d  eturn &sMutex;.}
92500 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e  .#endif /* defin
92510 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ed(SQLITE_MUTEX_
92520 4e 4f 4f 50 29 20 26 26 20 21 64 65 66 69 6e 65  NOOP) && !define
92530 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20  d(SQLITE_DEBUG) 
92540 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28  */..#if defined(
92550 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f  SQLITE_MUTEX_NOO
92560 50 29 20 26 26 20 64 65 66 69 6e 65 64 28 53 51  P) && defined(SQ
92570 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a  LITE_DEBUG)./*.*
92580 2a 20 49 6e 20 74 68 69 73 20 69 6d 70 6c 65 6d  * In this implem
92590 65 6e 74 61 74 69 6f 6e 2c 20 65 72 72 6f 72 20  entation, error 
925a0 63 68 65 63 6b 69 6e 67 20 69 73 20 70 72 6f 76  checking is prov
925b0 69 64 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  ided for testing
925c0 0a 2a 2a 20 61 6e 64 20 64 65 62 75 67 67 69 6e  .** and debuggin
925d0 67 20 70 75 72 70 6f 73 65 73 2e 20 20 54 68 65  g purposes.  The
925e0 20 6d 75 74 65 78 65 73 20 73 74 69 6c 6c 20 64   mutexes still d
925f0 6f 20 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 6e  o not provide an
92600 79 0a 2a 2a 20 6d 75 74 75 61 6c 20 65 78 63 6c  y.** mutual excl
92610 75 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  usion..*/../*.**
92620 20 54 68 65 20 6d 75 74 65 78 20 6f 62 6a 65 63   The mutex objec
92630 74 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  t.*/.struct sqli
92640 74 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 69 6e  te3_mutex {.  in
92650 74 20 69 64 3b 20 20 20 20 20 2f 2a 20 54 68 65  t id;     /* The
92660 20 6d 75 74 65 78 20 74 79 70 65 20 2a 2f 0a 20   mutex type */. 
92670 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 2f 2a 20   int cnt;    /* 
92680 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
92690 73 20 77 69 74 68 6f 75 74 20 61 20 6d 61 74 63  s without a matc
926a0 68 69 6e 67 20 6c 65 61 76 65 20 2a 2f 0a 7d 3b  hing leave */.};
926b0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ../*.** The sqli
926c0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29  te3_mutex_held()
926d0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74   and sqlite3_mut
926e0 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75  ex_notheld() rou
926f0 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65  tine are.** inte
92700 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e 73  nded for use ins
92710 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61  ide assert() sta
92720 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74  tements..*/.stat
92730 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65  ic int debugMute
92740 78 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75  xHeld(sqlite3_mu
92750 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  tex *p){.  retur
92760 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 63 6e 74  n p==0 || p->cnt
92770 3e 30 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  >0;.}.static int
92780 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65   debugMutexNothe
92790 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
927a0 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70   *p){.  return p
927b0 3d 3d 30 20 7c 7c 20 70 2d 3e 63 6e 74 3d 3d 30  ==0 || p->cnt==0
927c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  ;.}../*.** Initi
927d0 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74  alize and deinit
927e0 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78  ialize the mutex
927f0 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73   subsystem..*/.s
92800 74 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d  tatic int debugM
92810 75 74 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20  utexInit(void){ 
92820 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
92830 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64  ; }.static int d
92840 65 62 75 67 4d 75 74 65 78 45 6e 64 28 76 6f 69  ebugMutexEnd(voi
92850 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54  d){ return SQLIT
92860 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK; }../*.** T
92870 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
92880 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e 65  _alloc() routine
92890 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65 77   allocates a new
928a0 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72 65  .** mutex and re
928b0 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
928c0 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72 65  to it.  If it re
928d0 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68  turns NULL.** th
928e0 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61 20  at means that a 
928f0 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20  mutex could not 
92900 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 0a 2a  be allocated. .*
92910 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33  /.static sqlite3
92920 5f 6d 75 74 65 78 20 2a 64 65 62 75 67 4d 75 74  _mutex *debugMut
92930 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29 7b  exAlloc(int id){
92940 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65  .  static sqlite
92950 33 5f 6d 75 74 65 78 20 61 53 74 61 74 69 63 5b  3_mutex aStatic[
92960 36 5d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  6];.  sqlite3_mu
92970 74 65 78 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20  tex *pNew = 0;. 
92980 20 73 77 69 74 63 68 28 20 69 64 20 29 7b 0a 20   switch( id ){. 
92990 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d     case SQLITE_M
929a0 55 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63  UTEX_FAST:.    c
929b0 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ase SQLITE_MUTEX
929c0 5f 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20  _RECURSIVE: {.  
929d0 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74      pNew = sqlit
929e0 65 33 4d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  e3Malloc(sizeof(
929f0 2a 70 4e 65 77 29 29 3b 0a 20 20 20 20 20 20 69  *pNew));.      i
92a00 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20  f( pNew ){.     
92a10 20 20 20 70 4e 65 77 2d 3e 69 64 20 3d 20 69 64     pNew->id = id
92a20 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e  ;.        pNew->
92a30 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  cnt = 0;.      }
92a40 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
92a50 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a    }.    default:
92a60 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
92a70 20 69 64 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20   id-2 >= 0 );.  
92a80 20 20 20 20 61 73 73 65 72 74 28 20 69 64 2d 32      assert( id-2
92a90 20 3c 20 28 69 6e 74 29 28 73 69 7a 65 6f 66 28   < (int)(sizeof(
92aa0 61 53 74 61 74 69 63 29 2f 73 69 7a 65 6f 66 28  aStatic)/sizeof(
92ab0 61 53 74 61 74 69 63 5b 30 5d 29 29 20 29 3b 0a  aStatic[0])) );.
92ac0 20 20 20 20 20 20 70 4e 65 77 20 3d 20 26 61 53        pNew = &aS
92ad0 74 61 74 69 63 5b 69 64 2d 32 5d 3b 0a 20 20 20  tatic[id-2];.   
92ae0 20 20 20 70 4e 65 77 2d 3e 69 64 20 3d 20 69 64     pNew->id = id
92af0 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
92b00 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
92b10 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pNew;.}../*.**
92b20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65   This routine de
92b30 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76  allocates a prev
92b40 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64  iously allocated
92b50 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 74 69   mutex..*/.stati
92b60 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74 65  c void debugMute
92b70 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75  xFree(sqlite3_mu
92b80 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  tex *p){.  asser
92b90 74 28 20 70 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a  t( p->cnt==0 );.
92ba0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d    assert( p->id=
92bb0 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41  =SQLITE_MUTEX_FA
92bc0 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c  ST || p->id==SQL
92bd0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
92be0 49 56 45 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  IVE );.  sqlite3
92bf0 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a  _free(p);.}../*.
92c00 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  ** The sqlite3_m
92c10 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64  utex_enter() and
92c20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74   sqlite3_mutex_t
92c30 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74  ry() routines at
92c40 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65  tempt.** to ente
92c50 72 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61  r a mutex.  If a
92c60 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73  nother thread is
92c70 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20   already within 
92c80 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71  the mutex,.** sq
92c90 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
92ca0 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61  r() will block a
92cb0 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  nd sqlite3_mutex
92cc0 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75  _try() will retu
92cd0 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53  rn.** SQLITE_BUS
92ce0 59 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f  Y.  The sqlite3_
92cf0 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65  mutex_try() inte
92d00 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 51  rface returns SQ
92d10 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20  LITE_OK.** upon 
92d20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79  successful entry
92d30 2e 20 20 4d 75 74 65 78 65 73 20 63 72 65 61 74  .  Mutexes creat
92d40 65 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f  ed using SQLITE_
92d50 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
92d60 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65  can.** be entere
92d70 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73  d multiple times
92d80 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72   by the same thr
92d90 65 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61  ead.  In such ca
92da0 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65  ses the,.** mute
92db0 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 64  x must be exited
92dc0 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72   an equal number
92dd0 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65   of times before
92de0 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a   another thread.
92df0 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49  ** can enter.  I
92e00 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61  f the same threa
92e10 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72  d tries to enter
92e20 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20   any other kind 
92e30 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65  of mutex.** more
92e40 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20   than once, the 
92e50 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65  behavior is unde
92e60 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  fined..*/.static
92e70 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74 65 78   void debugMutex
92e80 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d 75  Enter(sqlite3_mu
92e90 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  tex *p){.  asser
92ea0 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  t( p->id==SQLITE
92eb0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
92ec0 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 4e 6f   || debugMutexNo
92ed0 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d  theld(p) );.  p-
92ee0 3e 63 6e 74 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63  >cnt++;.}.static
92ef0 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78 54   int debugMutexT
92f00 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ry(sqlite3_mutex
92f10 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
92f20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
92f30 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c  TEX_RECURSIVE ||
92f40 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65   debugMutexNothe
92f50 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63 6e  ld(p) );.  p->cn
92f60 74 2b 2b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  t++;.  return SQ
92f70 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
92f80 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
92f90 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74  tex_leave() rout
92fa0 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65  ine exits a mute
92fb0 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72  x that was.** pr
92fc0 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64  eviously entered
92fd0 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72   by the same thr
92fe0 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69  ead.  The behavi
92ff0 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e  or.** is undefin
93000 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20  ed if the mutex 
93010 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  is not currently
93020 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69   entered or.** i
93030 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
93040 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69  allocated.  SQLi
93050 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f  te will never do
93060 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74   either..*/.stat
93070 69 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74  ic void debugMut
93080 65 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f  exLeave(sqlite3_
93090 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73  mutex *p){.  ass
930a0 65 72 74 28 20 64 65 62 75 67 4d 75 74 65 78 48  ert( debugMutexH
930b0 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63  eld(p) );.  p->c
930c0 6e 74 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28 20  nt--;.  assert( 
930d0 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
930e0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c  TEX_RECURSIVE ||
930f0 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68 65   debugMutexNothe
93100 6c 64 28 70 29 20 29 3b 0a 7d 0a 0a 53 51 4c 49  ld(p) );.}..SQLI
93110 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74  TE_PRIVATE sqlit
93120 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
93130 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c 74   *sqlite3Default
93140 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73  Mutex(void){.  s
93150 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 75  tatic sqlite3_mu
93160 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74  tex_methods sMut
93170 65 78 20 3d 20 7b 0a 20 20 20 20 64 65 62 75 67  ex = {.    debug
93180 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 64  MutexInit,.    d
93190 65 62 75 67 4d 75 74 65 78 45 6e 64 2c 0a 20 20  ebugMutexEnd,.  
931a0 20 20 64 65 62 75 67 4d 75 74 65 78 41 6c 6c 6f    debugMutexAllo
931b0 63 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74 65  c,.    debugMute
931c0 78 46 72 65 65 2c 0a 20 20 20 20 64 65 62 75 67  xFree,.    debug
931d0 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20 20  MutexEnter,.    
931e0 64 65 62 75 67 4d 75 74 65 78 54 72 79 2c 0a 20  debugMutexTry,. 
931f0 20 20 20 64 65 62 75 67 4d 75 74 65 78 4c 65 61     debugMutexLea
93200 76 65 2c 0a 0a 20 20 20 20 64 65 62 75 67 4d 75  ve,..    debugMu
93210 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 64 65 62  texHeld,.    deb
93220 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20  ugMutexNotheld. 
93230 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73   };..  return &s
93240 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20  Mutex;.}.#endif 
93250 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  /* defined(SQLIT
93260 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26  E_MUTEX_NOOP) &&
93270 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
93280 44 45 42 55 47 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  DEBUG) */../****
93290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
932a0 66 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 20 2a  f mutex_noop.c *
932b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
932c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
932d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
932e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
932f0 20 66 69 6c 65 20 6d 75 74 65 78 5f 6f 73 32 2e   file mutex_os2.
93300 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
93310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
93330 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38 0a   2007 August 28.
93340 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
93350 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
93360 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
93370 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
93380 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
93390 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
933a0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
933b0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
933c0 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
933d0 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
933e0 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
933f0 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
93400 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
93410 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
93420 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
93430 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
93440 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
93450 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
93460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
934a0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
934b0 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74  ains the C funct
934c0 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d  ions that implem
934d0 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20  ent mutexes for 
934e0 4f 53 2f 32 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  OS/2.**.** $Id: 
934f0 6d 75 74 65 78 5f 6f 73 32 2e 63 2c 76 20 31 2e  mutex_os2.c,v 1.
93500 31 31 20 32 30 30 38 2f 31 31 2f 32 32 20 31 39  11 2008/11/22 19
93510 3a 35 30 3a 35 34 20 70 77 65 69 6c 62 61 63 68  :50:54 pweilbach
93520 65 72 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a  er Exp $.*/../*.
93530 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74  ** The code in t
93540 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79  his file is only
93550 20 75 73 65 64 20 69 66 20 53 51 4c 49 54 45 5f   used if SQLITE_
93560 4d 55 54 45 58 5f 4f 53 32 20 69 73 20 64 65 66  MUTEX_OS2 is def
93570 69 6e 65 64 2e 0a 2a 2a 20 53 65 65 20 74 68 65  ined..** See the
93580 20 6d 75 74 65 78 2e 68 20 66 69 6c 65 20 66 6f   mutex.h file fo
93590 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 69  r details..*/.#i
935a0 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45  fdef SQLITE_MUTE
935b0 58 5f 4f 53 32 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  X_OS2../********
935c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4f  ************** O
935d0 53 2f 32 20 4d 75 74 65 78 20 49 6d 70 6c 65 6d  S/2 Mutex Implem
935e0 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a 2a  entation *******
935f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
93600 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65  **.** This imple
93610 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 75 74  mentation of mut
93620 65 78 65 73 20 69 73 20 62 75 69 6c 74 20 75 73  exes is built us
93630 69 6e 67 20 74 68 65 20 4f 53 2f 32 20 41 50 49  ing the OS/2 API
93640 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ..*/../*.** The 
93650 6d 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a 2a 20  mutex object.** 
93660 45 61 63 68 20 72 65 63 75 72 73 69 76 65 20 6d  Each recursive m
93670 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61  utex is an insta
93680 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
93690 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a  wing structure..
936a0 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  */.struct sqlite
936b0 33 5f 6d 75 74 65 78 20 7b 0a 20 20 48 4d 54 58  3_mutex {.  HMTX
936c0 20 6d 75 74 65 78 3b 20 20 20 20 20 20 20 2f 2a   mutex;       /*
936d0 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69   Mutex controlli
936e0 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20  ng the lock */. 
936f0 20 69 6e 74 20 20 69 64 3b 20 20 20 20 20 20 20   int  id;       
93700 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65     /* Mutex type
93710 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 52 65 66 3b   */.  int  nRef;
93720 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
93730 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20  r of references 
93740 2a 2f 0a 20 20 54 49 44 20 20 6f 77 6e 65 72 3b  */.  TID  owner;
93750 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65 61 64         /* Thread
93760 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 6d 75   holding this mu
93770 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69  tex */.};..#defi
93780 6e 65 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49  ne OS2_MUTEX_INI
93790 54 49 41 4c 49 5a 45 52 20 20 20 30 2c 30 2c 30  TIALIZER   0,0,0
937a0 2c 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  ,0../*.** Initia
937b0 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69  lize and deiniti
937c0 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20  alize the mutex 
937d0 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74  subsystem..*/.st
937e0 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65  atic int os2Mute
937f0 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74  xInit(void){ ret
93800 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d  urn SQLITE_OK; }
93810 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d  .static int os2M
93820 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20 72  utexEnd(void){ r
93830 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
93840 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71   }../*.** The sq
93850 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
93860 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f  c() routine allo
93870 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d  cates a new.** m
93880 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e 73  utex and returns
93890 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74   a pointer to it
938a0 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e 73  .  If it returns
938b0 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65   NULL.** that me
938c0 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65 78  ans that a mutex
938d0 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c   could not be al
938e0 6c 6f 63 61 74 65 64 2e 20 0a 2a 2a 20 53 51 4c  located. .** SQL
938f0 69 74 65 20 77 69 6c 6c 20 75 6e 77 69 6e 64 20  ite will unwind 
93900 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72 65  its stack and re
93910 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20 20  turn an error.  
93920 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  The argument.** 
93930 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  to sqlite3_mutex
93940 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65 20  _alloc() is one 
93950 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65 72  of these integer
93960 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a   constants:.**.*
93970 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20  * <ul>.** <li>  
93980 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
93990 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T               
939a0 30 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  0.** <li>  SQLIT
939b0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
939c0 45 20 20 20 20 20 20 20 20 20 20 31 0a 2a 2a 20  E          1.** 
939d0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
939e0 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
939f0 20 20 20 20 20 20 32 0a 2a 2a 20 3c 6c 69 3e 20        2.** <li> 
93a00 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
93a10 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20 20  ATIC_MEM        
93a20 20 33 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49   3.** <li>  SQLI
93a30 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
93a40 50 52 4e 47 20 20 20 20 20 20 20 20 34 0a 2a 2a  PRNG        4.**
93a50 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68 65   </ul>.**.** The
93a60 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73 74   first two const
93a70 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69 74  ants cause sqlit
93a80 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
93a90 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61 20   to create.** a 
93aa0 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65 20  new mutex.  The 
93ab0 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65 63  new mutex is rec
93ac0 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c 49  ursive when SQLI
93ad0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
93ae0 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62 75  VE.** is used bu
93af0 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c  t not necessaril
93b00 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54 45  y so when SQLITE
93b10 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20 75  _MUTEX_FAST is u
93b20 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74 65  sed..** The mute
93b30 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  x implementation
93b40 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
93b50 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e 63  o make a distinc
93b60 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e 20  tion.** between 
93b70 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
93b80 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49 54  URSIVE and SQLIT
93b90 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66 20  E_MUTEX_FAST if 
93ba0 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 77  it does.** not w
93bb0 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51 4c  ant to.  But SQL
93bc0 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72 65  ite will only re
93bd0 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69 76  quest a recursiv
93be0 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63 61  e mutex in.** ca
93bf0 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65 61  ses where it rea
93c00 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20 20  lly needs one.  
93c10 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d  If a faster non-
93c20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a  recursive mutex.
93c30 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
93c40 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f  n is available o
93c50 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66  n the host platf
93c60 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73  orm, the mutex s
93c70 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68  ubsystem.** migh
93c80 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20  t return such a 
93c90 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73  mutex in respons
93ca0 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45  e to SQLITE_MUTE
93cb0 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54 68  X_FAST..**.** Th
93cc0 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64 20  e other allowed 
93cd0 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73 71  parameters to sq
93ce0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
93cf0 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e 0a  c() each return.
93d00 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
93d10 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69 73  a static preexis
93d20 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 68 72  ting mutex.  Thr
93d30 65 65 20 73 74 61 74 69 63 20 6d 75 74 65 78 65  ee static mutexe
93d40 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79  s are.** used by
93d50 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 65 72   the current ver
93d60 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20  sion of SQLite. 
93d70 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73   Future versions
93d80 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61   of SQLite.** ma
93d90 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c  y add additional
93da0 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e   static mutexes.
93db0 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 65 73    Static mutexes
93dc0 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61   are for interna
93dd0 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69  l.** use by SQLi
93de0 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63  te only.  Applic
93df0 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20  ations that use 
93e00 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73  SQLite mutexes s
93e10 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c  hould.** use onl
93e20 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75  y the dynamic mu
93e30 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 20 62  texes returned b
93e40 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  y SQLITE_MUTEX_F
93e50 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45  AST or.** SQLITE
93e60 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
93e70 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
93e80 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65 20  t if one of the 
93e90 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70 61  dynamic mutex pa
93ea0 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54 45  rameters (SQLITE
93eb0 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20 6f  _MUTEX_FAST.** o
93ec0 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  r SQLITE_MUTEX_R
93ed0 45 43 55 52 53 49 56 45 29 20 69 73 20 75 73 65  ECURSIVE) is use
93ee0 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f 6d  d then sqlite3_m
93ef0 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a 20  utex_alloc().** 
93f00 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 65 72  returns a differ
93f10 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76 65  ent mutex on eve
93f20 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66 6f  ry call.  But fo
93f30 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20  r the static.** 
93f40 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65  mutex types, the
93f50 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72   same mutex is r
93f60 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79  eturned on every
93f70 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a   call that has.*
93f80 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20  * the same type 
93f90 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  number..*/.stati
93fa0 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  c sqlite3_mutex 
93fb0 2a 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63 28 69  *os2MutexAlloc(i
93fc0 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 71 6c  nt iType){.  sql
93fd0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 20 3d 20  ite3_mutex *p = 
93fe0 4e 55 4c 4c 3b 0a 20 20 73 77 69 74 63 68 28 20  NULL;.  switch( 
93ff0 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73  iType ){.    cas
94000 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  e SQLITE_MUTEX_F
94010 41 53 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51  AST:.    case SQ
94020 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
94030 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 20  SIVE: {.      p 
94040 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a  = sqlite3MallocZ
94050 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20  ero( sizeof(*p) 
94060 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 29  );.      if( p )
94070 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64 20  {.        p->id 
94080 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 20  = iType;.       
94090 20 69 66 28 20 44 6f 73 43 72 65 61 74 65 4d 75   if( DosCreateMu
940a0 74 65 78 53 65 6d 28 20 30 2c 20 26 70 2d 3e 6d  texSem( 0, &p->m
940b0 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 20 29  utex, 0, FALSE )
940c0 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   != NO_ERROR ){.
940d0 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
940e0 33 5f 66 72 65 65 28 20 70 20 29 3b 0a 20 20 20  3_free( p );.   
940f0 20 20 20 20 20 20 20 70 20 3d 20 4e 55 4c 4c 3b         p = NULL;
94100 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
94110 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
94120 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
94130 74 3a 20 7b 0a 20 20 20 20 20 20 73 74 61 74 69  t: {.      stati
94140 63 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20 69  c volatile int i
94150 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 20  sInit = 0;.     
94160 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f   static sqlite3_
94170 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74 65  mutex staticMute
94180 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20  xes[] = {.      
94190 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e    { OS2_MUTEX_IN
941a0 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20  ITIALIZER, },.  
941b0 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45        { OS2_MUTE
941c0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d  X_INITIALIZER, }
941d0 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32 5f  ,.        { OS2_
941e0 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
941f0 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20  R, },.        { 
94200 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41  OS2_MUTEX_INITIA
94210 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20  LIZER, },.      
94220 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e    { OS2_MUTEX_IN
94230 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20  ITIALIZER, },.  
94240 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45        { OS2_MUTE
94250 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d  X_INITIALIZER, }
94260 2c 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20  ,.      };.     
94270 20 69 66 20 28 20 21 69 73 49 6e 69 74 20 29 7b   if ( !isInit ){
94280 0a 20 20 20 20 20 20 20 20 41 50 49 52 45 54 20  .        APIRET 
94290 72 63 3b 0a 20 20 20 20 20 20 20 20 50 54 49 42  rc;.        PTIB
942a0 20 70 74 69 62 3b 0a 20 20 20 20 20 20 20 20 50   ptib;.        P
942b0 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20 20 20  PIB ppib;.      
942c0 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 0a 20 20    HMTX mutex;.  
942d0 20 20 20 20 20 20 63 68 61 72 20 6e 61 6d 65 5b        char name[
942e0 33 32 5d 3b 0a 20 20 20 20 20 20 20 20 44 6f 73  32];.        Dos
942f0 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 20 26  GetInfoBlocks( &
94300 70 74 69 62 2c 20 26 70 70 69 62 20 29 3b 0a 20  ptib, &ppib );. 
94310 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73         sqlite3_s
94320 6e 70 72 69 6e 74 66 28 20 73 69 7a 65 6f 66 28  nprintf( sizeof(
94330 6e 61 6d 65 29 2c 20 6e 61 6d 65 2c 20 22 5c 5c  name), name, "\\
94340 53 45 4d 33 32 5c 5c 53 51 4c 49 54 45 25 30 34  SEM32\\SQLITE%04
94350 78 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  x",.            
94360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 70                pp
94370 69 62 2d 3e 70 69 62 5f 75 6c 70 69 64 20 29 3b  ib->pib_ulpid );
94380 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
94390 21 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20  !isInit ){.     
943a0 20 20 20 20 20 6d 75 74 65 78 20 3d 20 30 3b 0a       mutex = 0;.
943b0 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 44            rc = D
943c0 6f 73 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d  osCreateMutexSem
943d0 28 20 6e 61 6d 65 2c 20 26 6d 75 74 65 78 2c 20  ( name, &mutex, 
943e0 30 2c 20 46 41 4c 53 45 29 3b 0a 20 20 20 20 20  0, FALSE);.     
943f0 20 20 20 20 20 69 66 28 20 72 63 20 3d 3d 20 4e       if( rc == N
94400 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20  O_ERROR ){.     
94410 20 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20         unsigned 
94420 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 20  int i;.         
94430 20 20 20 69 66 28 20 21 69 73 49 6e 69 74 20 29     if( !isInit )
94440 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
94450 66 6f 72 28 20 69 20 3d 20 30 3b 20 69 20 3c 20  for( i = 0; i < 
94460 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74  sizeof(staticMut
94470 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74 61  exes)/sizeof(sta
94480 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 3b 20  ticMutexes[0]); 
94490 69 2b 2b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  i++ ){.         
944a0 20 20 20 20 20 20 20 44 6f 73 43 72 65 61 74 65         DosCreate
944b0 4d 75 74 65 78 53 65 6d 28 20 30 2c 20 26 73 74  MutexSem( 0, &st
944c0 61 74 69 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d  aticMutexes[i].m
944d0 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 20 29  utex, 0, FALSE )
944e0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
944f0 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }.              
94500 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20  isInit = 1;.    
94510 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
94520 20 20 20 20 20 20 44 6f 73 43 6c 6f 73 65 4d 75        DosCloseMu
94530 74 65 78 53 65 6d 28 20 6d 75 74 65 78 20 29 3b  texSem( mutex );
94540 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
94550 20 69 66 28 20 72 63 20 3d 3d 20 45 52 52 4f 52   if( rc == ERROR
94560 5f 44 55 50 4c 49 43 41 54 45 5f 4e 41 4d 45 20  _DUPLICATE_NAME 
94570 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 44  ){.            D
94580 6f 73 53 6c 65 65 70 28 20 31 20 29 3b 0a 20 20  osSleep( 1 );.  
94590 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
945a0 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72             retur
945b0 6e 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  n p;.          }
945c0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
945d0 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   }.      assert(
945e0 20 69 54 79 70 65 2d 32 20 3e 3d 20 30 20 29 3b   iType-2 >= 0 );
945f0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
94600 54 79 70 65 2d 32 20 3c 20 73 69 7a 65 6f 66 28  Type-2 < sizeof(
94610 73 74 61 74 69 63 4d 75 74 65 78 65 73 29 2f 73  staticMutexes)/s
94620 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65  izeof(staticMute
94630 78 65 73 5b 30 5d 29 20 29 3b 0a 20 20 20 20 20  xes[0]) );.     
94640 20 70 20 3d 20 26 73 74 61 74 69 63 4d 75 74 65   p = &staticMute
94650 78 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20  xes[iType-2];.  
94660 20 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70      p->id = iTyp
94670 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  e;.      break;.
94680 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
94690 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  rn p;.}.../*.** 
946a0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 61  This routine dea
946b0 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76 69  llocates a previ
946c0 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  ously allocated 
946d0 6d 75 74 65 78 2e 0a 2a 2a 20 53 51 4c 69 74 65  mutex..** SQLite
946e0 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64   is careful to d
946f0 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 20  eallocate every 
94700 6d 75 74 65 78 20 74 68 61 74 20 69 74 20 61 6c  mutex that it al
94710 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73 74 61 74  locates..*/.stat
94720 69 63 20 76 6f 69 64 20 6f 73 32 4d 75 74 65 78  ic void os2Mutex
94730 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  Free(sqlite3_mut
94740 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d  ex *p){.  if( p=
94750 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61  =0 ) return;.  a
94760 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d  ssert( p->nRef==
94770 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
94780 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
94790 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64  EX_FAST || p->id
947a0 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  ==SQLITE_MUTEX_R
947b0 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20 44 6f  ECURSIVE );.  Do
947c0 73 43 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20  sCloseMutexSem( 
947d0 70 2d 3e 6d 75 74 65 78 20 29 3b 0a 20 20 73 71  p->mutex );.  sq
947e0 6c 69 74 65 33 5f 66 72 65 65 28 20 70 20 29 3b  lite3_free( p );
947f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  .}../*.** The sq
94800 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
94810 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  r() and sqlite3_
94820 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74  mutex_try() rout
94830 69 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20  ines attempt.** 
94840 74 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78  to enter a mutex
94850 2e 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68  .  If another th
94860 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20  read is already 
94870 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78  within the mutex
94880 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74  ,.** sqlite3_mut
94890 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20  ex_enter() will 
948a0 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65  block and sqlite
948b0 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69  3_mutex_try() wi
948c0 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  ll return.** SQL
948d0 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73  ITE_BUSY.  The s
948e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
948f0 28 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  () interface ret
94900 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a  urns SQLITE_OK.*
94910 2a 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75  * upon successfu
94920 6c 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65  l entry.  Mutexe
94930 73 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20  s created using 
94940 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43  SQLITE_MUTEX_REC
94950 55 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65  URSIVE can.** be
94960 20 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c   entered multipl
94970 65 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73  e times by the s
94980 61 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20  ame thread.  In 
94990 73 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a  such cases the,.
949a0 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65  ** mutex must be
949b0 20 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c   exited an equal
949c0 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73   number of times
949d0 20 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20   before another 
949e0 74 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e  thread.** can en
949f0 74 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d  ter.  If the sam
94a00 65 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74  e thread tries t
94a10 6f 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65  o enter any othe
94a20 72 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a  r kind of mutex.
94a30 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63  ** more than onc
94a40 65 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  e, the behavior 
94a50 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f  is undefined..*/
94a60 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32  .static void os2
94a70 4d 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74  MutexEnter(sqlit
94a80 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
94a90 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 68  TID tid;.  PID h
94aa0 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e 47 20  older1;.  ULONG 
94ab0 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28 20 70  holder2;.  if( p
94ac0 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
94ad0 61 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53  assert( p->id==S
94ae0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
94af0 52 53 49 56 45 20 7c 7c 20 6f 73 32 4d 75 74 65  RSIVE || os2Mute
94b00 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20  xNotheld(p) );. 
94b10 20 44 6f 73 52 65 71 75 65 73 74 4d 75 74 65 78   DosRequestMutex
94b20 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 53 45  Sem(p->mutex, SE
94b30 4d 5f 49 4e 44 45 46 49 4e 49 54 45 5f 57 41 49  M_INDEFINITE_WAI
94b40 54 29 3b 0a 20 20 44 6f 73 51 75 65 72 79 4d 75  T);.  DosQueryMu
94b50 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c  texSem(p->mutex,
94b60 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69 64 2c   &holder1, &tid,
94b70 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20 70 2d   &holder2);.  p-
94b80 3e 6f 77 6e 65 72 20 3d 20 74 69 64 3b 0a 20 20  >owner = tid;.  
94b90 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61  p->nRef++;.}.sta
94ba0 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74 65 78  tic int os2Mutex
94bb0 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  Try(sqlite3_mute
94bc0 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b  x *p){.  int rc;
94bd0 0a 20 20 54 49 44 20 74 69 64 3b 0a 20 20 50 49  .  TID tid;.  PI
94be0 44 20 68 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f  D holder1;.  ULO
94bf0 4e 47 20 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66  NG holder2;.  if
94c00 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
94c10 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73  SQLITE_OK;.  ass
94c20 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  ert( p->id==SQLI
94c30 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
94c40 56 45 20 7c 7c 20 6f 73 32 4d 75 74 65 78 4e 6f  VE || os2MutexNo
94c50 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 69 66  theld(p) );.  if
94c60 28 20 44 6f 73 52 65 71 75 65 73 74 4d 75 74 65  ( DosRequestMute
94c70 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 53  xSem(p->mutex, S
94c80 45 4d 5f 49 4d 4d 45 44 49 41 54 45 5f 52 45 54  EM_IMMEDIATE_RET
94c90 55 52 4e 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52  URN) == NO_ERROR
94ca0 29 20 7b 0a 20 20 20 20 44 6f 73 51 75 65 72 79  ) {.    DosQuery
94cb0 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65  MutexSem(p->mute
94cc0 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74 69  x, &holder1, &ti
94cd0 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20 20  d, &holder2);.  
94ce0 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 74 69 64    p->owner = tid
94cf0 3b 0a 20 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b  ;.    p->nRef++;
94d00 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
94d10 5f 4f 4b 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a  _OK;.  } else {.
94d20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
94d30 42 55 53 59 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  BUSY;.  }..  ret
94d40 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
94d50 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
94d60 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69  ex_leave() routi
94d70 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78  ne exits a mutex
94d80 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65   that was.** pre
94d90 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20  viously entered 
94da0 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  by the same thre
94db0 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f  ad.  The behavio
94dc0 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65  r.** is undefine
94dd0 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69  d if the mutex i
94de0 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
94df0 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73  entered or.** is
94e00 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61   not currently a
94e10 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74  llocated.  SQLit
94e20 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20  e will never do 
94e30 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  either..*/.stati
94e40 63 20 76 6f 69 64 20 6f 73 32 4d 75 74 65 78 4c  c void os2MutexL
94e50 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  eave(sqlite3_mut
94e60 65 78 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69  ex *p){.  TID ti
94e70 64 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31  d;.  PID holder1
94e80 3b 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72  ;.  ULONG holder
94e90 32 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  2;.  if( p==0 ) 
94ea0 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74  return;.  assert
94eb0 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  ( p->nRef>0 );. 
94ec0 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65   DosQueryMutexSe
94ed0 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c  m(p->mutex, &hol
94ee0 64 65 72 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c  der1, &tid, &hol
94ef0 64 65 72 32 29 3b 0a 20 20 61 73 73 65 72 74 28  der2);.  assert(
94f00 20 70 2d 3e 6f 77 6e 65 72 3d 3d 74 69 64 20 29   p->owner==tid )
94f10 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20  ;.  p->nRef--;. 
94f20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66   assert( p->nRef
94f30 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51  ==0 || p->id==SQ
94f40 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
94f50 53 49 56 45 20 29 3b 0a 20 20 44 6f 73 52 65 6c  SIVE );.  DosRel
94f60 65 61 73 65 4d 75 74 65 78 53 65 6d 28 70 2d 3e  easeMutexSem(p->
94f70 6d 75 74 65 78 29 3b 0a 7d 0a 0a 23 69 66 64 65  mutex);.}..#ifde
94f80 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f  f SQLITE_DEBUG./
94f90 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
94fa0 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e  _mutex_held() an
94fb0 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
94fc0 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e  notheld() routin
94fd0 65 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65  e are.** intende
94fe0 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65  d for use inside
94ff0 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d   assert() statem
95000 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ents..*/.static 
95010 69 6e 74 20 6f 73 32 4d 75 74 65 78 48 65 6c 64  int os2MutexHeld
95020 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
95030 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b 0a 20  p){.  TID tid;. 
95040 20 50 49 44 20 70 69 64 3b 0a 20 20 55 4c 4f 4e   PID pid;.  ULON
95050 47 20 75 6c 43 6f 75 6e 74 3b 0a 20 20 50 54 49  G ulCount;.  PTI
95060 42 20 70 74 69 62 3b 0a 20 20 69 66 28 20 70 21  B ptib;.  if( p!
95070 3d 30 20 29 20 7b 0a 20 20 20 20 44 6f 73 51 75  =0 ) {.    DosQu
95080 65 72 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d  eryMutexSem(p->m
95090 75 74 65 78 2c 20 26 70 69 64 2c 20 26 74 69 64  utex, &pid, &tid
950a0 2c 20 26 75 6c 43 6f 75 6e 74 29 3b 0a 20 20 7d  , &ulCount);.  }
950b0 20 65 6c 73 65 20 7b 0a 20 20 20 20 44 6f 73 47   else {.    DosG
950c0 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 26 70 74  etInfoBlocks(&pt
950d0 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 74  ib, NULL);.    t
950e0 69 64 20 3d 20 70 74 69 62 2d 3e 74 69 62 5f 70  id = ptib->tib_p
950f0 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74 69 64  tib2->tib2_ultid
95100 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
95110 3d 3d 30 20 7c 7c 20 28 70 2d 3e 6e 52 65 66 21  ==0 || (p->nRef!
95120 3d 30 20 26 26 20 70 2d 3e 6f 77 6e 65 72 3d 3d  =0 && p->owner==
95130 74 69 64 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69  tid);.}.static i
95140 6e 74 20 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65  nt os2MutexNothe
95150 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
95160 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b   *p){.  TID tid;
95170 0a 20 20 50 49 44 20 70 69 64 3b 0a 20 20 55 4c  .  PID pid;.  UL
95180 4f 4e 47 20 75 6c 43 6f 75 6e 74 3b 0a 20 20 50  ONG ulCount;.  P
95190 54 49 42 20 70 74 69 62 3b 0a 20 20 69 66 28 20  TIB ptib;.  if( 
951a0 70 21 3d 20 30 20 29 20 7b 0a 20 20 20 20 44 6f  p!= 0 ) {.    Do
951b0 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70  sQueryMutexSem(p
951c0 2d 3e 6d 75 74 65 78 2c 20 26 70 69 64 2c 20 26  ->mutex, &pid, &
951d0 74 69 64 2c 20 26 75 6c 43 6f 75 6e 74 29 3b 0a  tid, &ulCount);.
951e0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 44    } else {.    D
951f0 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28  osGetInfoBlocks(
95200 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b 0a 20 20  &ptib, NULL);.  
95210 20 20 74 69 64 20 3d 20 70 74 69 62 2d 3e 74 69    tid = ptib->ti
95220 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c  b_ptib2->tib2_ul
95230 74 69 64 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  tid;.  }.  retur
95240 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52 65  n p==0 || p->nRe
95250 66 3d 3d 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72  f==0 || p->owner
95260 21 3d 74 69 64 3b 0a 7d 0a 23 65 6e 64 69 66 0a  !=tid;.}.#endif.
95270 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
95280 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65  sqlite3_mutex_me
95290 74 68 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65  thods *sqlite3De
952a0 66 61 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29  faultMutex(void)
952b0 7b 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74  {.  static sqlit
952c0 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
952d0 20 73 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20   sMutex = {.    
952e0 6f 73 32 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20  os2MutexInit,.  
952f0 20 20 6f 73 32 4d 75 74 65 78 45 6e 64 2c 0a 20    os2MutexEnd,. 
95300 20 20 20 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63     os2MutexAlloc
95310 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 46 72  ,.    os2MutexFr
95320 65 65 2c 0a 20 20 20 20 6f 73 32 4d 75 74 65 78  ee,.    os2Mutex
95330 45 6e 74 65 72 2c 0a 20 20 20 20 6f 73 32 4d 75  Enter,.    os2Mu
95340 74 65 78 54 72 79 2c 0a 20 20 20 20 6f 73 32 4d  texTry,.    os2M
95350 75 74 65 78 4c 65 61 76 65 2c 0a 23 69 66 64 65  utexLeave,.#ifde
95360 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
95370 20 20 20 6f 73 32 4d 75 74 65 78 48 65 6c 64 2c     os2MutexHeld,
95380 0a 20 20 20 20 6f 73 32 4d 75 74 65 78 4e 6f 74  .    os2MutexNot
95390 68 65 6c 64 0a 23 65 6e 64 69 66 0a 20 20 7d 3b  held.#endif.  };
953a0 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74  ..  return &sMut
953b0 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ex;.}.#endif /* 
953c0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32  SQLITE_MUTEX_OS2
953d0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
953e0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74 65  **** End of mute
953f0 78 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  x_os2.c ********
95400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95420 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
95430 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
95440 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 2a 2a 2a  mutex_unix.c ***
95450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95470 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20  ***/./*.** 2007 
95480 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a 2a 2a 20  August 28.**.** 
95490 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
954a0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
954b0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
954c0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
954d0 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
954e0 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
954f0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
95500 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
95510 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
95520 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
95530 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
95540 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
95550 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
95560 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
95570 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
95580 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
95590 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
955a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
955b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
955c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
955d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
955e0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
955f0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74   file contains t
95600 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 74  he C functions t
95610 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75  hat implement mu
95620 74 65 78 65 73 20 66 6f 72 20 70 74 68 72 65 61  texes for pthrea
95630 64 73 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75  ds.**.** $Id: mu
95640 74 65 78 5f 75 6e 69 78 2e 63 2c 76 20 31 2e 31  tex_unix.c,v 1.1
95650 36 20 32 30 30 38 2f 31 32 2f 30 38 20 31 38 3a  6 2008/12/08 18:
95660 31 39 3a 31 38 20 64 72 68 20 45 78 70 20 24 0a  19:18 drh Exp $.
95670 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f  */../*.** The co
95680 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
95690 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20  is only used if 
956a0 77 65 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67  we are compiling
956b0 20 74 68 72 65 61 64 73 61 66 65 0a 2a 2a 20 75   threadsafe.** u
956c0 6e 64 65 72 20 75 6e 69 78 20 77 69 74 68 20 70  nder unix with p
956d0 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e  threads..**.** N
956e0 6f 74 65 20 74 68 61 74 20 74 68 69 73 20 69 6d  ote that this im
956f0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 72 65 71  plementation req
95700 75 69 72 65 73 20 61 20 76 65 72 73 69 6f 6e 20  uires a version 
95710 6f 66 20 70 74 68 72 65 61 64 73 20 74 68 61 74  of pthreads that
95720 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20 72 65 63  .** supports rec
95730 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 2e 0a  ursive mutexes..
95740 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
95750 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 53 0a  _MUTEX_PTHREADS.
95760 0a 23 69 6e 63 6c 75 64 65 20 3c 70 74 68 72 65  .#include <pthre
95770 61 64 2e 68 3e 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61  ad.h>.../*.** Ea
95780 63 68 20 72 65 63 75 72 73 69 76 65 20 6d 75 74  ch recursive mut
95790 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  ex is an instanc
957a0 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
957b0 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  ng structure..*/
957c0 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
957d0 6d 75 74 65 78 20 7b 0a 20 20 70 74 68 72 65 61  mutex {.  pthrea
957e0 64 5f 6d 75 74 65 78 5f 74 20 6d 75 74 65 78 3b  d_mutex_t mutex;
957f0 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f       /* Mutex co
95800 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f  ntrolling the lo
95810 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64 3b 20  ck */.  int id; 
95820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
95830 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65     /* Mutex type
95840 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20   */.  int nRef; 
95850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
95860 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
95870 74 72 61 6e 63 65 73 20 2a 2f 0a 20 20 70 74 68  trances */.  pth
95880 72 65 61 64 5f 74 20 6f 77 6e 65 72 3b 20 20 20  read_t owner;   
95890 20 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65 61          /* Threa
958a0 64 20 74 68 61 74 20 69 73 20 77 69 74 68 69 6e  d that is within
958b0 20 74 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 23   this mutex */.#
958c0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
958d0 55 47 0a 20 20 69 6e 74 20 74 72 61 63 65 3b 20  UG.  int trace; 
958e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
958f0 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61 63 65  /* True to trace
95900 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 65 6e 64   changes */.#end
95910 69 66 0a 7d 3b 0a 23 69 66 64 65 66 20 53 51 4c  if.};.#ifdef SQL
95920 49 54 45 5f 44 45 42 55 47 0a 23 64 65 66 69 6e  ITE_DEBUG.#defin
95930 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f  e SQLITE3_MUTEX_
95940 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20 50 54  INITIALIZER { PT
95950 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54  HREAD_MUTEX_INIT
95960 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 2c 20 28  IALIZER, 0, 0, (
95970 70 74 68 72 65 61 64 5f 74 29 30 2c 20 30 20 7d  pthread_t)0, 0 }
95980 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53  .#else.#define S
95990 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49  QLITE3_MUTEX_INI
959a0 54 49 41 4c 49 5a 45 52 20 7b 20 50 54 48 52 45  TIALIZER { PTHRE
959b0 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c  AD_MUTEX_INITIAL
959c0 49 5a 45 52 2c 20 30 2c 20 30 2c 20 28 70 74 68  IZER, 0, 0, (pth
959d0 72 65 61 64 5f 74 29 30 20 7d 0a 23 65 6e 64 69  read_t)0 }.#endi
959e0 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  f../*.** The sql
959f0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
95a00 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75  ) and sqlite3_mu
95a10 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f  tex_notheld() ro
95a20 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74  utine are.** int
95a30 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 6f 6e  ended for use on
95a40 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65 72 74  ly inside assert
95a50 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20  () statements.  
95a60 4f 6e 20 73 6f 6d 65 20 70 6c 61 74 66 6f 72 6d  On some platform
95a70 73 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 69 67 68  s,.** there migh
95a80 74 20 62 65 20 72 61 63 65 20 63 6f 6e 64 69 74  t be race condit
95a90 69 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 63 61  ions that can ca
95aa0 75 73 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e  use these routin
95ab0 65 73 20 74 6f 0a 2a 2a 20 64 65 6c 69 76 65 72  es to.** deliver
95ac0 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73 75 6c   incorrect resul
95ad0 74 73 2e 20 20 49 6e 20 70 61 72 74 69 63 75 6c  ts.  In particul
95ae0 61 72 2c 20 69 66 20 70 74 68 72 65 61 64 5f 65  ar, if pthread_e
95af0 71 75 61 6c 28 29 20 69 73 0a 2a 2a 20 6e 6f 74  qual() is.** not
95b00 20 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72 61   an atomic opera
95b10 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 73 65  tion, then these
95b20 20 72 6f 75 74 69 6e 65 73 20 6d 69 67 68 74 20   routines might 
95b30 64 65 6c 69 76 65 72 79 0a 2a 2a 20 69 6e 63 6f  delivery.** inco
95b40 72 72 65 63 74 20 72 65 73 75 6c 74 73 2e 20 20  rrect results.  
95b50 4f 6e 20 6d 6f 73 74 20 70 6c 61 74 66 6f 72 6d  On most platform
95b60 73 2c 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c  s, pthread_equal
95b70 28 29 20 69 73 20 61 20 0a 2a 2a 20 63 6f 6d 70  () is a .** comp
95b80 61 72 69 73 6f 6e 20 6f 66 20 74 77 6f 20 69 6e  arison of two in
95b90 74 65 67 65 72 73 20 61 6e 64 20 69 73 20 74 68  tegers and is th
95ba0 65 72 65 66 6f 72 65 20 61 74 6f 6d 69 63 2e 20  erefore atomic. 
95bb0 20 42 75 74 20 77 65 20 61 72 65 0a 2a 2a 20 74   But we are.** t
95bc0 6f 6c 64 20 74 68 61 74 20 48 50 55 58 20 69 73  old that HPUX is
95bd0 20 6e 6f 74 20 73 75 63 68 20 61 20 70 6c 61 74   not such a plat
95be0 66 6f 72 6d 2e 20 20 49 66 20 73 6f 2c 20 74 68  form.  If so, th
95bf0 65 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  en these routine
95c00 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 61 6c  s.** will not al
95c10 77 61 79 73 20 77 6f 72 6b 20 63 6f 72 72 65 63  ways work correc
95c20 74 6c 79 20 6f 6e 20 48 50 55 58 2e 0a 2a 2a 0a  tly on HPUX..**.
95c30 2a 2a 20 4f 6e 20 74 68 6f 73 65 20 70 6c 61 74  ** On those plat
95c40 66 6f 72 6d 73 20 77 68 65 72 65 20 70 74 68 72  forms where pthr
95c50 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 20 6e  ead_equal() is n
95c60 6f 74 20 61 74 6f 6d 69 63 2c 20 53 51 4c 69 74  ot atomic, SQLit
95c70 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63  e.** should be c
95c80 6f 6d 70 69 6c 65 64 20 77 69 74 68 6f 75 74 20  ompiled without 
95c90 2d 44 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61  -DSQLITE_DEBUG a
95ca0 6e 64 20 77 69 74 68 20 2d 44 4e 44 45 42 55 47  nd with -DNDEBUG
95cb0 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 73 75 72 65   to.** make sure
95cc0 20 6e 6f 20 61 73 73 65 72 74 28 29 20 73 74 61   no assert() sta
95cd0 74 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c  tements are eval
95ce0 75 61 74 65 64 20 61 6e 64 20 68 65 6e 63 65 20  uated and hence 
95cf0 74 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65  these.** routine
95d00 73 20 61 72 65 20 6e 65 76 65 72 20 63 61 6c 6c  s are never call
95d10 65 64 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69  ed..*/.#if !defi
95d20 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64  ned(NDEBUG) || d
95d30 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
95d40 42 55 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20  BUG).static int 
95d50 70 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64  pthreadMutexHeld
95d60 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
95d70 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 2d  p){.  return (p-
95d80 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 74 68 72  >nRef!=0 && pthr
95d90 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e  ead_equal(p->own
95da0 65 72 2c 20 70 74 68 72 65 61 64 5f 73 65 6c 66  er, pthread_self
95db0 28 29 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69  ()));.}.static i
95dc0 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e  nt pthreadMutexN
95dd0 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  otheld(sqlite3_m
95de0 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75  utex *p){.  retu
95df0 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c  rn p->nRef==0 ||
95e00 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70   pthread_equal(p
95e10 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65 61 64  ->owner, pthread
95e20 5f 73 65 6c 66 28 29 29 3d 3d 30 3b 0a 7d 0a 23  _self())==0;.}.#
95e30 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69  endif../*.** Ini
95e40 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e  tialize and dein
95e50 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74  itialize the mut
95e60 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f  ex subsystem..*/
95e70 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72  .static int pthr
95e80 65 61 64 4d 75 74 65 78 49 6e 69 74 28 76 6f 69  eadMutexInit(voi
95e90 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54  d){ return SQLIT
95ea0 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69  E_OK; }.static i
95eb0 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 45  nt pthreadMutexE
95ec0 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  nd(void){ return
95ed0 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f   SQLITE_OK; }../
95ee0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
95ef0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72  _mutex_alloc() r
95f00 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73  outine allocates
95f10 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20   a new.** mutex 
95f20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f  and returns a po
95f30 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66  inter to it.  If
95f40 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c   it returns NULL
95f50 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74  .** that means t
95f60 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c  hat a mutex coul
95f70 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74  d not be allocat
95f80 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77  ed.  SQLite.** w
95f90 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73  ill unwind its s
95fa0 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20  tack and return 
95fb0 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20 61  an error.  The a
95fc0 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71  rgument.** to sq
95fd0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
95fe0 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68  c() is one of th
95ff0 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73  ese integer cons
96000 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c  tants:.**.** <ul
96010 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  >.** <li>  SQLIT
96020 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20  E_MUTEX_FAST.** 
96030 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
96040 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20  EX_RECURSIVE.** 
96050 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
96060 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
96070 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45  .** <li>  SQLITE
96080 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45  _MUTEX_STATIC_ME
96090 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  M.** <li>  SQLIT
960a0 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
960b0 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  EM2.** <li>  SQL
960c0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
960d0 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53  _PRNG.** <li>  S
960e0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
960f0 49 43 5f 4c 52 55 0a 2a 2a 20 3c 6c 69 3e 20 20  IC_LRU.** <li>  
96100 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
96110 54 49 43 5f 4c 52 55 32 0a 2a 2a 20 3c 2f 75 6c  TIC_LRU2.** </ul
96120 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  >.**.** The firs
96130 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20  t two constants 
96140 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75  cause sqlite3_mu
96150 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63  tex_alloc() to c
96160 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d  reate.** a new m
96170 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d  utex.  The new m
96180 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76  utex is recursiv
96190 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55  e when SQLITE_MU
961a0 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a  TEX_RECURSIVE.**
961b0 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74   is used but not
961c0 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20   necessarily so 
961d0 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45  when SQLITE_MUTE
961e0 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 0a  X_FAST is used..
961f0 2a 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70  ** The mutex imp
96200 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73  lementation does
96210 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b   not need to mak
96220 65 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a  e a distinction.
96230 2a 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54  ** between SQLIT
96240 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
96250 45 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54  E and SQLITE_MUT
96260 45 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f  EX_FAST if it do
96270 65 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74  es.** not want t
96280 6f 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20 77  o.  But SQLite w
96290 69 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74  ill only request
962a0 20 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74   a recursive mut
962b0 65 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77  ex in.** cases w
962c0 68 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e  here it really n
962d0 65 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20  eeds one.  If a 
962e0 66 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72  faster non-recur
962f0 73 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d  sive mutex.** im
96300 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20  plementation is 
96310 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65  available on the
96320 20 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20   host platform, 
96330 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73  the mutex subsys
96340 74 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74  tem.** might ret
96350 75 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78  urn such a mutex
96360 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20   in response to 
96370 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
96380 54 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68  T..**.** The oth
96390 65 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d  er allowed param
963a0 65 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33  eters to sqlite3
963b0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65  _mutex_alloc() e
963c0 61 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20  ach return.** a 
963d0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61  pointer to a sta
963e0 74 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20  tic preexisting 
963f0 6d 75 74 65 78 2e 20 20 53 69 78 20 73 74 61 74  mutex.  Six stat
96400 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a  ic mutexes are.*
96410 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75  * used by the cu
96420 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66  rrent version of
96430 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65   SQLite.  Future
96440 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
96450 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61  ite.** may add a
96460 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63  dditional static
96470 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 69   mutexes.  Stati
96480 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f  c mutexes are fo
96490 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73  r internal.** us
964a0 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79  e by SQLite only
964b0 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20  .  Applications 
964c0 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 20  that use SQLite 
964d0 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a  mutexes should.*
964e0 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64  * use only the d
964f0 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72  ynamic mutexes r
96500 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54  eturned by SQLIT
96510 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a  E_MUTEX_FAST or.
96520 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ** SQLITE_MUTEX_
96530 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a  RECURSIVE..**.**
96540 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e   Note that if on
96550 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63  e of the dynamic
96560 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72   mutex parameter
96570 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  s (SQLITE_MUTEX_
96580 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54  FAST.** or SQLIT
96590 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
965a0 45 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20  E) is used then 
965b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
965c0 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73  loc().** returns
965d0 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74   a different mut
965e0 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c  ex on every call
965f0 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 73  .  But for the s
96600 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78 20  tatic .** mutex 
96610 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20  types, the same 
96620 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 6e 65  mutex is returne
96630 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20  d on every call 
96640 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20  that has.** the 
96650 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72  same type number
96660 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69  ..*/.static sqli
96670 74 65 33 5f 6d 75 74 65 78 20 2a 70 74 68 72 65  te3_mutex *pthre
96680 61 64 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74  adMutexAlloc(int
96690 20 69 54 79 70 65 29 7b 0a 20 20 73 74 61 74 69   iType){.  stati
966a0 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  c sqlite3_mutex 
966b0 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 5d 20  staticMutexes[] 
966c0 3d 20 7b 0a 20 20 20 20 53 51 4c 49 54 45 33 5f  = {.    SQLITE3_
966d0 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
966e0 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d  R,.    SQLITE3_M
966f0 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52  UTEX_INITIALIZER
96700 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55  ,.    SQLITE3_MU
96710 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c  TEX_INITIALIZER,
96720 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54  .    SQLITE3_MUT
96730 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a  EX_INITIALIZER,.
96740 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45      SQLITE3_MUTE
96750 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20  X_INITIALIZER,. 
96760 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58     SQLITE3_MUTEX
96770 5f 49 4e 49 54 49 41 4c 49 5a 45 52 0a 20 20 7d  _INITIALIZER.  }
96780 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
96790 78 20 2a 70 3b 0a 20 20 73 77 69 74 63 68 28 20  x *p;.  switch( 
967a0 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73  iType ){.    cas
967b0 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  e SQLITE_MUTEX_R
967c0 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20  ECURSIVE: {.    
967d0 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c    p = sqlite3Mal
967e0 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28  locZero( sizeof(
967f0 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28  *p) );.      if(
96800 20 70 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c   p ){.#ifdef SQL
96810 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45  ITE_HOMEGROWN_RE
96820 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 20  CURSIVE_MUTEX.  
96830 20 20 20 20 20 20 2f 2a 20 49 66 20 72 65 63 75        /* If recu
96840 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 61 72  rsive mutexes ar
96850 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c  e not available,
96860 20 77 65 20 77 69 6c 6c 20 68 61 76 65 20 74 6f   we will have to
96870 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 75 69 6c  .        ** buil
96880 64 20 6f 75 72 20 6f 77 6e 2e 20 20 53 65 65 20  d our own.  See 
96890 62 65 6c 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20  below. */.      
968a0 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f    pthread_mutex_
968b0 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20  init(&p->mutex, 
968c0 30 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20  0);.#else.      
968d0 20 20 2f 2a 20 55 73 65 20 61 20 72 65 63 75 72    /* Use a recur
968e0 73 69 76 65 20 6d 75 74 65 78 20 69 66 20 69 74  sive mutex if it
968f0 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f   is available */
96900 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64  .        pthread
96910 5f 6d 75 74 65 78 61 74 74 72 5f 74 20 72 65 63  _mutexattr_t rec
96920 75 72 73 69 76 65 41 74 74 72 3b 0a 20 20 20 20  ursiveAttr;.    
96930 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65      pthread_mute
96940 78 61 74 74 72 5f 69 6e 69 74 28 26 72 65 63 75  xattr_init(&recu
96950 72 73 69 76 65 41 74 74 72 29 3b 0a 20 20 20 20  rsiveAttr);.    
96960 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65      pthread_mute
96970 78 61 74 74 72 5f 73 65 74 74 79 70 65 28 26 72  xattr_settype(&r
96980 65 63 75 72 73 69 76 65 41 74 74 72 2c 20 50 54  ecursiveAttr, PT
96990 48 52 45 41 44 5f 4d 55 54 45 58 5f 52 45 43 55  HREAD_MUTEX_RECU
969a0 52 53 49 56 45 29 3b 0a 20 20 20 20 20 20 20 20  RSIVE);.        
969b0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e  pthread_mutex_in
969c0 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 26 72  it(&p->mutex, &r
969d0 65 63 75 72 73 69 76 65 41 74 74 72 29 3b 0a 20  ecursiveAttr);. 
969e0 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d         pthread_m
969f0 75 74 65 78 61 74 74 72 5f 64 65 73 74 72 6f 79  utexattr_destroy
96a00 28 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29  (&recursiveAttr)
96a10 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  ;.#endif.       
96a20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a   p->id = iType;.
96a30 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
96a40 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
96a50 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ase SQLITE_MUTEX
96a60 5f 46 41 53 54 3a 20 7b 0a 20 20 20 20 20 20 70  _FAST: {.      p
96a70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
96a80 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29  Zero( sizeof(*p)
96a90 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20   );.      if( p 
96aa0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64  ){.        p->id
96ab0 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20   = iType;.      
96ac0 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f    pthread_mutex_
96ad0 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20  init(&p->mutex, 
96ae0 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0);.      }.    
96af0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
96b00 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
96b10 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70      assert( iTyp
96b20 65 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20  e-2 >= 0 );.    
96b30 20 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d    assert( iType-
96b40 32 20 3c 20 41 72 72 61 79 53 69 7a 65 28 73 74  2 < ArraySize(st
96b50 61 74 69 63 4d 75 74 65 78 65 73 29 20 29 3b 0a  aticMutexes) );.
96b60 20 20 20 20 20 20 70 20 3d 20 26 73 74 61 74 69        p = &stati
96b70 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d 32  cMutexes[iType-2
96b80 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d  ];.      p->id =
96b90 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 72   iType;.      br
96ba0 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
96bb0 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f   return p;.}.../
96bc0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
96bd0 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20  e deallocates a 
96be0 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c  previously.** al
96bf0 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20 20  located mutex.  
96c00 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75  SQLite is carefu
96c10 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20  l to deallocate 
96c20 65 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74  every.** mutex t
96c30 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73  hat it allocates
96c40 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
96c50 20 70 74 68 72 65 61 64 4d 75 74 65 78 46 72 65   pthreadMutexFre
96c60 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  e(sqlite3_mutex 
96c70 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
96c80 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61  ->nRef==0 );.  a
96c90 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
96ca0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20  LITE_MUTEX_FAST 
96cb0 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  || p->id==SQLITE
96cc0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
96cd0 20 29 3b 0a 20 20 70 74 68 72 65 61 64 5f 6d 75   );.  pthread_mu
96ce0 74 65 78 5f 64 65 73 74 72 6f 79 28 26 70 2d 3e  tex_destroy(&p->
96cf0 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65  mutex);.  sqlite
96d00 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a  3_free(p);.}../*
96d10 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
96d20 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e  mutex_enter() an
96d30 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
96d40 74 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61  try() routines a
96d50 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74  ttempt.** to ent
96d60 65 72 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20  er a mutex.  If 
96d70 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69  another thread i
96d80 73 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e  s already within
96d90 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73   the mutex,.** s
96da0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
96db0 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20  er() will block 
96dc0 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  and sqlite3_mute
96dd0 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74  x_try() will ret
96de0 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55  urn.** SQLITE_BU
96df0 53 59 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  SY.  The sqlite3
96e00 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74  _mutex_try() int
96e10 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 53  erface returns S
96e20 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e  QLITE_OK.** upon
96e30 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72   successful entr
96e40 79 2e 20 20 4d 75 74 65 78 65 73 20 63 72 65 61  y.  Mutexes crea
96e50 74 65 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45  ted using SQLITE
96e60 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
96e70 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72   can.** be enter
96e80 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  ed multiple time
96e90 73 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68  s by the same th
96ea0 72 65 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63  read.  In such c
96eb0 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74  ases the,.** mut
96ec0 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65  ex must be exite
96ed0 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65  d an equal numbe
96ee0 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72  r of times befor
96ef0 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64  e another thread
96f00 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20  .** can enter.  
96f10 49 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  If the same thre
96f20 61 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65  ad tries to ente
96f30 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64  r any other kind
96f40 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72   of mutex.** mor
96f50 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65  e than once, the
96f60 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64   behavior is und
96f70 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  efined..*/.stati
96f80 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75  c void pthreadMu
96f90 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33  texEnter(sqlite3
96fa0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73  _mutex *p){.  as
96fb0 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c  sert( p->id==SQL
96fc0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
96fd0 49 56 45 20 7c 7c 20 70 74 68 72 65 61 64 4d 75  IVE || pthreadMu
96fe0 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b  texNotheld(p) );
96ff0 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
97000 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53  HOMEGROWN_RECURS
97010 49 56 45 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49  IVE_MUTEX.  /* I
97020 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  f recursive mute
97030 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69  xes are not avai
97040 6c 61 62 6c 65 2c 20 74 68 65 6e 20 77 65 20 68  lable, then we h
97050 61 76 65 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a  ave to grow.  **
97060 20 6f 75 72 20 6f 77 6e 2e 20 20 54 68 69 73 20   our own.  This 
97070 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61  implementation a
97080 73 73 75 6d 65 73 20 74 68 61 74 20 70 74 68 72  ssumes that pthr
97090 65 61 64 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a  ead_equal().  **
970a0 20 69 73 20 61 74 6f 6d 69 63 20 2d 20 74 68 61   is atomic - tha
970b0 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 64  t it cannot be d
970c0 65 63 65 69 76 65 64 20 69 6e 74 6f 20 74 68 69  eceived into thi
970d0 6e 6b 69 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20  nking self.  ** 
970e0 61 6e 64 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65  and p->owner are
970f0 20 65 71 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e   equal if p->own
97100 65 72 20 63 68 61 6e 67 65 73 20 62 65 74 77 65  er changes betwe
97110 65 6e 20 74 77 6f 20 76 61 6c 75 65 73 0a 20 20  en two values.  
97120 2a 2a 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20  ** that are not 
97130 65 71 75 61 6c 20 74 6f 20 73 65 6c 66 20 77 68  equal to self wh
97140 69 6c 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73  ile the comparis
97150 6f 6e 20 69 73 20 74 61 6b 69 6e 67 20 70 6c 61  on is taking pla
97160 63 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d  ce..  ** This im
97170 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73  plementation als
97180 6f 20 61 73 73 75 6d 65 73 20 61 20 63 6f 68 65  o assumes a cohe
97190 72 65 6e 74 20 63 61 63 68 65 20 2d 20 74 68 61  rent cache - tha
971a0 74 20 0a 20 20 2a 2a 20 73 65 70 61 72 61 74 65  t .  ** separate
971b0 20 70 72 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f   processes canno
971c0 74 20 72 65 61 64 20 64 69 66 66 65 72 65 6e 74  t read different
971d0 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65   values from the
971e0 20 73 61 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65   same.  ** addre
971f0 73 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 74  ss at the same t
97200 69 6d 65 2e 20 20 49 66 20 65 69 74 68 65 72 20  ime.  If either 
97210 6f 66 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e  of these two con
97220 64 69 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65  ditions.  ** are
97230 20 6e 6f 74 20 6d 65 74 2c 20 74 68 65 6e 20 74   not met, then t
97240 68 65 20 6d 75 74 65 78 65 73 20 77 69 6c 6c 20  he mutexes will 
97250 66 61 69 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d  fail and problem
97260 73 20 77 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20  s will result.. 
97270 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 70 74 68 72   */.  {.    pthr
97280 65 61 64 5f 74 20 73 65 6c 66 20 3d 20 70 74 68  ead_t self = pth
97290 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20  read_self();.   
972a0 20 69 66 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26   if( p->nRef>0 &
972b0 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28  & pthread_equal(
972c0 70 2d 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20  p->owner, self) 
972d0 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66  ){.      p->nRef
972e0 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ++;.    }else{. 
972f0 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74       pthread_mut
97300 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65  ex_lock(&p->mute
97310 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  x);.      assert
97320 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a  ( p->nRef==0 );.
97330 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d        p->owner =
97340 20 73 65 6c 66 3b 0a 20 20 20 20 20 20 70 2d 3e   self;.      p->
97350 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  nRef = 1;.    }.
97360 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 55    }.#else.  /* U
97370 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  se the built-in 
97380 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65  recursive mutexe
97390 73 20 69 66 20 74 68 65 79 20 61 72 65 20 61 76  s if they are av
973a0 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  ailable..  */.  
973b0 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f  pthread_mutex_lo
973c0 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20  ck(&p->mutex);. 
973d0 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72   p->owner = pthr
973e0 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 70 2d  ead_self();.  p-
973f0 3e 6e 52 65 66 2b 2b 3b 0a 23 65 6e 64 69 66 0a  >nRef++;.#endif.
97400 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
97410 45 42 55 47 0a 20 20 69 66 28 20 70 2d 3e 74 72  EBUG.  if( p->tr
97420 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e 74  ace ){.    print
97430 66 28 22 65 6e 74 65 72 20 6d 75 74 65 78 20 25  f("enter mutex %
97440 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 65 66  p (%d) with nRef
97450 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72  =%d\n", p, p->tr
97460 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20  ace, p->nRef);. 
97470 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74   }.#endif.}.stat
97480 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75  ic int pthreadMu
97490 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d  texTry(sqlite3_m
974a0 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20  utex *p){.  int 
974b0 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  rc;.  assert( p-
974c0 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
974d0 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 70  X_RECURSIVE || p
974e0 74 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65  threadMutexNothe
974f0 6c 64 28 70 29 20 29 3b 0a 0a 23 69 66 64 65 66  ld(p) );..#ifdef
97500 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57   SQLITE_HOMEGROW
97510 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45  N_RECURSIVE_MUTE
97520 58 0a 20 20 2f 2a 20 49 66 20 72 65 63 75 72 73  X.  /* If recurs
97530 69 76 65 20 6d 75 74 65 78 65 73 20 61 72 65 20  ive mutexes are 
97540 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 74  not available, t
97550 68 65 6e 20 77 65 20 68 61 76 65 20 74 6f 20 67  hen we have to g
97560 72 6f 77 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e  row.  ** our own
97570 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e  .  This implemen
97580 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74  tation assumes t
97590 68 61 74 20 70 74 68 72 65 61 64 5f 65 71 75 61  hat pthread_equa
975a0 6c 28 29 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d  l().  ** is atom
975b0 69 63 20 2d 20 74 68 61 74 20 69 74 20 63 61 6e  ic - that it can
975c0 6e 6f 74 20 62 65 20 64 65 63 65 69 76 65 64 20  not be deceived 
975d0 69 6e 74 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65  into thinking se
975e0 6c 66 0a 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f  lf.  ** and p->o
975f0 77 6e 65 72 20 61 72 65 20 65 71 75 61 6c 20 69  wner are equal i
97600 66 20 70 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67  f p->owner chang
97610 65 73 20 62 65 74 77 65 65 6e 20 74 77 6f 20 76  es between two v
97620 61 6c 75 65 73 0a 20 20 2a 2a 20 74 68 61 74 20  alues.  ** that 
97630 61 72 65 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f  are not equal to
97640 20 73 65 6c 66 20 77 68 69 6c 65 20 74 68 65 20   self while the 
97650 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 61  comparison is ta
97660 6b 69 6e 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a  king place..  **
97670 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61   This implementa
97680 74 69 6f 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65  tion also assume
97690 73 20 61 20 63 6f 68 65 72 65 6e 74 20 63 61 63  s a coherent cac
976a0 68 65 20 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20  he - that .  ** 
976b0 73 65 70 61 72 61 74 65 20 70 72 6f 63 65 73 73  separate process
976c0 65 73 20 63 61 6e 6e 6f 74 20 72 65 61 64 20 64  es cannot read d
976d0 69 66 66 65 72 65 6e 74 20 76 61 6c 75 65 73 20  ifferent values 
976e0 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 0a 20 20  from the same.  
976f0 2a 2a 20 61 64 64 72 65 73 73 20 61 74 20 74 68  ** address at th
97700 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 49 66  e same time.  If
97710 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65   either of these
97720 20 74 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a   two conditions.
97730 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74    ** are not met
97740 2c 20 74 68 65 6e 20 74 68 65 20 6d 75 74 65 78  , then the mutex
97750 65 73 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64  es will fail and
97760 20 70 72 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72   problems will r
97770 65 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a  esult..  */.  {.
97780 20 20 20 20 70 74 68 72 65 61 64 5f 74 20 73 65      pthread_t se
97790 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c  lf = pthread_sel
977a0 66 28 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  f();.    if( p->
977b0 6e 52 65 66 3e 30 20 26 26 20 70 74 68 72 65 61  nRef>0 && pthrea
977c0 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72  d_equal(p->owner
977d0 2c 20 73 65 6c 66 29 20 29 7b 0a 20 20 20 20 20  , self) ){.     
977e0 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20   p->nRef++;.    
977f0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
97800 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
97810 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 72  pthread_mutex_tr
97820 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29  ylock(&p->mutex)
97830 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ==0 ){.      ass
97840 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20  ert( p->nRef==0 
97850 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65  );.      p->owne
97860 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20 20 20 20  r = self;.      
97870 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20  p->nRef = 1;.   
97880 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
97890 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  K;.    }else{.  
978a0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
978b0 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  BUSY;.    }.  }.
978c0 23 65 6c 73 65 0a 20 20 2f 2a 20 55 73 65 20 74  #else.  /* Use t
978d0 68 65 20 62 75 69 6c 74 2d 69 6e 20 72 65 63 75  he built-in recu
978e0 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 69 66  rsive mutexes if
978f0 20 74 68 65 79 20 61 72 65 20 61 76 61 69 6c 61   they are availa
97900 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ble..  */.  if( 
97910 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 72  pthread_mutex_tr
97920 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29  ylock(&p->mutex)
97930 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 6f 77  ==0 ){.    p->ow
97940 6e 65 72 20 3d 20 70 74 68 72 65 61 64 5f 73 65  ner = pthread_se
97950 6c 66 28 29 3b 0a 20 20 20 20 70 2d 3e 6e 52 65  lf();.    p->nRe
97960 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  f++;.    rc = SQ
97970 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65  LITE_OK;.  }else
97980 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
97990 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64  E_BUSY;.  }.#end
979a0 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
979b0 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 72 63  E_DEBUG.  if( rc
979c0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
979d0 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70  ->trace ){.    p
979e0 72 69 6e 74 66 28 22 65 6e 74 65 72 20 6d 75 74  rintf("enter mut
979f0 65 78 20 25 70 20 28 25 64 29 20 77 69 74 68 20  ex %p (%d) with 
97a00 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70  nRef=%d\n", p, p
97a10 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66  ->trace, p->nRef
97a20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
97a30 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
97a40 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
97a50 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f  mutex_leave() ro
97a60 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75  utine exits a mu
97a70 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20  tex that was.** 
97a80 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72  previously enter
97a90 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74  ed by the same t
97aa0 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61  hread.  The beha
97ab0 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66  vior.** is undef
97ac0 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65  ined if the mute
97ad0 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74  x is not current
97ae0 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a  ly entered or.**
97af0 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
97b00 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51  y allocated.  SQ
97b10 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20  Lite will never 
97b20 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74  do either..*/.st
97b30 61 74 69 63 20 76 6f 69 64 20 70 74 68 72 65 61  atic void pthrea
97b40 64 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69  dMutexLeave(sqli
97b50 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
97b60 20 61 73 73 65 72 74 28 20 70 74 68 72 65 61 64   assert( pthread
97b70 4d 75 74 65 78 48 65 6c 64 28 70 29 20 29 3b 0a  MutexHeld(p) );.
97b80 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61    p->nRef--;.  a
97b90 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d  ssert( p->nRef==
97ba0 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  0 || p->id==SQLI
97bb0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
97bc0 56 45 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51  VE );..#ifdef SQ
97bd0 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52  LITE_HOMEGROWN_R
97be0 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20  ECURSIVE_MUTEX. 
97bf0 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20   if( p->nRef==0 
97c00 29 7b 0a 20 20 20 20 70 74 68 72 65 61 64 5f 6d  ){.    pthread_m
97c10 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e  utex_unlock(&p->
97c20 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 23 65 6c 73  mutex);.  }.#els
97c30 65 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65  e.  pthread_mute
97c40 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74  x_unlock(&p->mut
97c50 65 78 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66  ex);.#endif..#if
97c60 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
97c70 0a 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20  .  if( p->trace 
97c80 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 6c  ){.    printf("l
97c90 65 61 76 65 20 6d 75 74 65 78 20 25 70 20 28 25  eave mutex %p (%
97ca0 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64 5c  d) with nRef=%d\
97cb0 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c  n", p, p->trace,
97cc0 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23   p->nRef);.  }.#
97cd0 65 6e 64 69 66 0a 7d 0a 0a 53 51 4c 49 54 45 5f  endif.}..SQLITE_
97ce0 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f  PRIVATE sqlite3_
97cf0 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73  mutex_methods *s
97d00 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74  qlite3DefaultMut
97d10 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  ex(void){.  stat
97d20 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ic sqlite3_mutex
97d30 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20  _methods sMutex 
97d40 3d 20 7b 0a 20 20 20 20 70 74 68 72 65 61 64 4d  = {.    pthreadM
97d50 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 70 74  utexInit,.    pt
97d60 68 72 65 61 64 4d 75 74 65 78 45 6e 64 2c 0a 20  hreadMutexEnd,. 
97d70 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 41     pthreadMutexA
97d80 6c 6c 6f 63 2c 0a 20 20 20 20 70 74 68 72 65 61  lloc,.    pthrea
97d90 64 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20  dMutexFree,.    
97da0 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e 74 65  pthreadMutexEnte
97db0 72 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75  r,.    pthreadMu
97dc0 74 65 78 54 72 79 2c 0a 20 20 20 20 70 74 68 72  texTry,.    pthr
97dd0 65 61 64 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23  eadMutexLeave,.#
97de0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
97df0 55 47 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75  UG.    pthreadMu
97e00 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 70 74 68  texHeld,.    pth
97e10 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64  readMutexNotheld
97e20 0a 23 65 6c 73 65 0a 20 20 20 20 30 2c 0a 20 20  .#else.    0,.  
97e30 20 20 30 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a    0.#endif.  };.
97e40 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75 74 65  .  return &sMute
97e50 78 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  x;.}..#endif /* 
97e60 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 50 54 48  SQLITE_MUTEX_PTH
97e70 52 45 41 44 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  READ */../******
97e80 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
97e90 6d 75 74 65 78 5f 75 6e 69 78 2e 63 20 2a 2a 2a  mutex_unix.c ***
97ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97ec0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
97ed0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
97ee0 69 6c 65 20 6d 75 74 65 78 5f 77 33 32 2e 63 20  ile mutex_w32.c 
97ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97f10 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
97f20 30 30 37 20 41 75 67 75 73 74 20 31 34 0a 2a 2a  007 August 14.**
97f30 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
97f40 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
97f50 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
97f60 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
97f70 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
97f80 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
97f90 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
97fa0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
97fb0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
97fc0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
97fd0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
97fe0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
97ff0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
98000 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
98010 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
98020 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
98030 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
98040 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
98050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
98080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
98090 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
980a0 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f  ns the C functio
980b0 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  ns that implemen
980c0 74 20 6d 75 74 65 78 65 73 20 66 6f 72 20 77 69  t mutexes for wi
980d0 6e 33 32 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d  n32.**.** $Id: m
980e0 75 74 65 78 5f 77 33 32 2e 63 2c 76 20 31 2e 31  utex_w32.c,v 1.1
980f0 38 20 32 30 30 39 2f 30 38 2f 31 30 20 30 33 3a  8 2009/08/10 03:
98100 32 33 3a 32 31 20 73 68 61 6e 65 20 45 78 70 20  23:21 shane Exp 
98110 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  $.*/../*.** The 
98120 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c  code in this fil
98130 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69  e is only used i
98140 66 20 77 65 20 61 72 65 20 63 6f 6d 70 69 6c 69  f we are compili
98150 6e 67 20 6d 75 6c 74 69 74 68 72 65 61 64 65 64  ng multithreaded
98160 0a 2a 2a 20 6f 6e 20 61 20 77 69 6e 33 32 20 73  .** on a win32 s
98170 79 73 74 65 6d 2e 0a 2a 2f 0a 23 69 66 64 65 66  ystem..*/.#ifdef
98180 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33   SQLITE_MUTEX_W3
98190 32 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 72 65  2../*.** Each re
981a0 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 73  cursive mutex is
981b0 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
981c0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
981d0 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75  ructure..*/.stru
981e0 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ct sqlite3_mutex
981f0 20 7b 0a 20 20 43 52 49 54 49 43 41 4c 5f 53 45   {.  CRITICAL_SE
98200 43 54 49 4f 4e 20 6d 75 74 65 78 3b 20 20 20 20  CTION mutex;    
98210 2f 2a 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c  /* Mutex control
98220 6c 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f  ling the lock */
98230 0a 20 20 69 6e 74 20 69 64 3b 20 20 20 20 20 20  .  int id;      
98240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
98250 20 4d 75 74 65 78 20 74 79 70 65 20 2a 2f 0a 20   Mutex type */. 
98260 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20   int nRef;      
98270 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
98280 75 6d 62 65 72 20 6f 66 20 65 6e 74 65 72 61 6e  umber of enteran
98290 63 65 73 20 2a 2f 0a 20 20 44 57 4f 52 44 20 6f  ces */.  DWORD o
982a0 77 6e 65 72 3b 20 20 20 20 20 20 20 20 20 20 20  wner;           
982b0 20 20 20 20 2f 2a 20 54 68 72 65 61 64 20 68 6f      /* Thread ho
982c0 6c 64 69 6e 67 20 74 68 69 73 20 6d 75 74 65 78  lding this mutex
982d0 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65   */.};../*.** Re
982e0 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e 2d 7a  turn true (non-z
982f0 65 72 6f 29 20 69 66 20 77 65 20 61 72 65 20 72  ero) if we are r
98300 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57 69 6e  unning under Win
98310 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e 58 50  NT, Win2K, WinXP
98320 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e 20 20  ,.** or WinCE.  
98330 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28 7a 65  Return false (ze
98340 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c 20 57  ro) for Win95, W
98350 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45 2e 0a  in98, or WinME..
98360 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20 61 6e  **.** Here is an
98370 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f 62 73   interesting obs
98380 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e 39 35  ervation:  Win95
98390 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57 69 6e  , Win98, and Win
983a0 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65 20 4c  ME lack.** the L
983b0 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49 2e  ockFileEx() API.
983c0 20 20 42 75 74 20 77 65 20 63 61 6e 20 73 74 69    But we can sti
983d0 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20 6c 69  ll statically li
983e0 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61 74 0a  nk against that.
983f0 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67 20 61  ** API as long a
98400 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c 6c 20  s we don't call 
98410 69 74 20 77 69 6e 20 72 75 6e 6e 69 6e 67 20 57  it win running W
98420 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 41 20 63  in95/98/ME.  A c
98430 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69 73 20 72  all to.** this r
98440 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74  outine is used t
98450 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74  o determine if t
98460 68 65 20 68 6f 73 74 20 69 73 20 57 69 6e 39 35  he host is Win95
98470 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20 57 69 6e  /98/ME or.** Win
98480 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74 68 61 74  NT/2K/XP so that
98490 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68   we will know wh
984a0 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 77 65 20  ether or not we 
984b0 63 61 6e 20 73 61 66 65 6c 79 20 63 61 6c 6c 0a  can safely call.
984c0 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45  ** the LockFileE
984d0 78 28 29 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 6d  x() API..**.** m
984e0 75 74 65 78 49 73 4e 54 28 29 20 69 73 20 6f 6e  utexIsNT() is on
984f0 6c 79 20 75 73 65 64 20 66 6f 72 20 74 68 65 20  ly used for the 
98500 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 61 6c  TryEnterCritical
98510 53 65 63 74 69 6f 6e 28 29 20 41 50 49 20 63 61  Section() API ca
98520 6c 6c 2c 0a 2a 2a 20 77 68 69 63 68 20 69 73 20  ll,.** which is 
98530 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69  only available i
98540 66 20 79 6f 75 72 20 61 70 70 6c 69 63 61 74 69  f your applicati
98550 6f 6e 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20  on was compiled 
98560 77 69 74 68 20 0a 2a 2a 20 5f 57 49 4e 33 32 5f  with .** _WIN32_
98570 57 49 4e 4e 54 20 64 65 66 69 6e 65 64 20 74 6f  WINNT defined to
98580 20 61 20 76 61 6c 75 65 20 3e 3d 20 30 78 30 34   a value >= 0x04
98590 30 30 2e 20 20 43 75 72 72 65 6e 74 6c 79 2c 20  00.  Currently, 
985a0 74 68 65 20 6f 6e 6c 79 0a 2a 2a 20 63 61 6c 6c  the only.** call
985b0 20 74 6f 20 54 72 79 45 6e 74 65 72 43 72 69 74   to TryEnterCrit
985c0 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20 69 73  icalSection() is
985d0 20 23 69 66 64 65 66 27 65 64 20 6f 75 74 2c 20   #ifdef'ed out, 
985e0 73 6f 20 23 69 66 64 65 66 20 0a 2a 2a 20 74 68  so #ifdef .** th
985f0 69 73 20 6f 75 74 20 61 73 20 77 65 6c 6c 2e 0a  is out as well..
98600 2a 2f 0a 23 69 66 20 30 0a 23 69 66 20 53 51 4c  */.#if 0.#if SQL
98610 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 23 20 64  ITE_OS_WINCE.# d
98620 65 66 69 6e 65 20 6d 75 74 65 78 49 73 4e 54 28  efine mutexIsNT(
98630 29 20 20 28 31 29 0a 23 65 6c 73 65 0a 20 20 73  )  (1).#else.  s
98640 74 61 74 69 63 20 69 6e 74 20 6d 75 74 65 78 49  tatic int mutexI
98650 73 4e 54 28 76 6f 69 64 29 7b 0a 20 20 20 20 73  sNT(void){.    s
98660 74 61 74 69 63 20 69 6e 74 20 6f 73 54 79 70 65  tatic int osType
98670 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 6f 73   = 0;.    if( os
98680 54 79 70 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Type==0 ){.     
98690 20 4f 53 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73   OSVERSIONINFO s
986a0 49 6e 66 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66  Info;.      sInf
986b0 6f 2e 64 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66  o.dwOSVersionInf
986c0 6f 53 69 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73  oSize = sizeof(s
986d0 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 47 65 74  Info);.      Get
986e0 56 65 72 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f  VersionEx(&sInfo
986f0 29 3b 0a 20 20 20 20 20 20 6f 73 54 79 70 65 20  );.      osType 
98700 3d 20 73 49 6e 66 6f 2e 64 77 50 6c 61 74 66 6f  = sInfo.dwPlatfo
98710 72 6d 49 64 3d 3d 56 45 52 5f 50 4c 41 54 46 4f  rmId==VER_PLATFO
98720 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f 20 32 20  RM_WIN32_NT ? 2 
98730 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  : 1;.    }.    r
98740 65 74 75 72 6e 20 6f 73 54 79 70 65 3d 3d 32 3b  eturn osType==2;
98750 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
98760 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 2a  QLITE_OS_WINCE *
98770 2f 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  /.#endif..#ifdef
98780 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a   SQLITE_DEBUG./*
98790 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
987a0 6d 75 74 65 78 5f 68 65 6c 64 28 29 20 61 6e 64  mutex_held() and
987b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e   sqlite3_mutex_n
987c0 6f 74 68 65 6c 64 28 29 20 72 6f 75 74 69 6e 65  otheld() routine
987d0 20 61 72 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64   are.** intended
987e0 20 66 6f 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e   for use only in
987f0 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74  side assert() st
98800 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61  atements..*/.sta
98810 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78  tic int winMutex
98820 48 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74  Held(sqlite3_mut
98830 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  ex *p){.  return
98840 20 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26 20 70   p->nRef!=0 && p
98850 2d 3e 6f 77 6e 65 72 3d 3d 47 65 74 43 75 72 72  ->owner==GetCurr
98860 65 6e 74 54 68 72 65 61 64 49 64 28 29 3b 0a 7d  entThreadId();.}
98870 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4d  .static int winM
98880 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69  utexNotheld(sqli
98890 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
988a0 20 72 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3d   return p->nRef=
988b0 3d 30 20 7c 7c 20 70 2d 3e 6f 77 6e 65 72 21 3d  =0 || p->owner!=
988c0 47 65 74 43 75 72 72 65 6e 74 54 68 72 65 61 64  GetCurrentThread
988d0 49 64 28 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  Id();.}.#endif..
988e0 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  ./*.** Initializ
988f0 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c 69  e and deinitiali
98900 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 75 62  ze the mutex sub
98910 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69  system..*/.stati
98920 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  c sqlite3_mutex 
98930 77 69 6e 4d 75 74 65 78 5f 73 74 61 74 69 63 4d  winMutex_staticM
98940 75 74 65 78 65 73 5b 36 5d 3b 0a 73 74 61 74 69  utexes[6];.stati
98950 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 5f 69  c int winMutex_i
98960 73 49 6e 69 74 20 3d 20 30 3b 0a 2f 2a 20 41 73  sInit = 0;./* As
98970 20 77 69 6e 4d 75 74 65 78 49 6e 69 74 28 29 20   winMutexInit() 
98980 61 6e 64 20 77 69 6e 4d 75 74 65 78 45 6e 64 28  and winMutexEnd(
98990 29 20 61 72 65 20 63 61 6c 6c 65 64 20 61 73 20  ) are called as 
989a0 70 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20 73  part.** of the s
989b0 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
989c0 65 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 73 68  e and sqlite3_sh
989d0 75 74 64 6f 77 6e 28 29 0a 2a 2a 20 70 72 6f 63  utdown().** proc
989e0 65 73 73 69 6e 67 2c 20 74 68 65 20 22 69 6e 74  essing, the "int
989f0 65 72 6c 6f 63 6b 65 64 22 20 6d 61 67 69 63 20  erlocked" magic 
98a00 69 73 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 0a  is probably not.
98a10 2a 2a 20 73 74 72 69 63 74 6c 79 20 6e 65 63 65  ** strictly nece
98a20 73 73 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  ssary..*/.static
98a30 20 6c 6f 6e 67 20 77 69 6e 4d 75 74 65 78 5f 6c   long winMutex_l
98a40 6f 63 6b 20 3d 20 30 3b 0a 0a 73 74 61 74 69 63  ock = 0;..static
98a50 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 49 6e 69   int winMutexIni
98a60 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20 54  t(void){ .  /* T
98a70 68 65 20 66 69 72 73 74 20 74 6f 20 69 6e 63 72  he first to incr
98a80 65 6d 65 6e 74 20 74 6f 20 31 20 64 6f 65 73 20  ement to 1 does 
98a90 61 63 74 75 61 6c 20 69 6e 69 74 69 61 6c 69 7a  actual initializ
98aa0 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 49  ation */.  if( I
98ab0 6e 74 65 72 6c 6f 63 6b 65 64 43 6f 6d 70 61 72  nterlockedCompar
98ac0 65 45 78 63 68 61 6e 67 65 28 26 77 69 6e 4d 75  eExchange(&winMu
98ad0 74 65 78 5f 6c 6f 63 6b 2c 20 31 2c 20 30 29 3d  tex_lock, 1, 0)=
98ae0 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  =0 ){.    int i;
98af0 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
98b00 41 72 72 61 79 53 69 7a 65 28 77 69 6e 4d 75 74  ArraySize(winMut
98b10 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73  ex_staticMutexes
98b20 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 49  ); i++){.      I
98b30 6e 69 74 69 61 6c 69 7a 65 43 72 69 74 69 63 61  nitializeCritica
98b40 6c 53 65 63 74 69 6f 6e 28 26 77 69 6e 4d 75 74  lSection(&winMut
98b50 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78 65 73  ex_staticMutexes
98b60 5b 69 5d 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  [i].mutex);.    
98b70 7d 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 5f 69  }.    winMutex_i
98b80 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 65 6c  sInit = 1;.  }el
98b90 73 65 7b 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 6f  se{.    /* Someo
98ba0 6e 65 20 65 6c 73 65 20 69 73 20 69 6e 20 74 68  ne else is in th
98bb0 65 20 70 72 6f 63 65 73 73 20 6f 66 20 69 6e 69  e process of ini
98bc0 74 69 6e 67 20 74 68 65 20 73 74 61 74 69 63 20  ting the static 
98bd0 6d 75 74 65 78 65 73 20 2a 2f 0a 20 20 20 20 77  mutexes */.    w
98be0 68 69 6c 65 28 20 21 77 69 6e 4d 75 74 65 78 5f  hile( !winMutex_
98bf0 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20  isInit ){.      
98c00 53 6c 65 65 70 28 31 29 3b 0a 20 20 20 20 7d 0a  Sleep(1);.    }.
98c10 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
98c20 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 73 74 61 74  ITE_OK; .}..stat
98c30 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 45  ic int winMutexE
98c40 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20  nd(void){ .  /* 
98c50 54 68 65 20 66 69 72 73 74 20 74 6f 20 64 65 63  The first to dec
98c60 72 65 6d 65 6e 74 20 74 6f 20 30 20 64 6f 65 73  rement to 0 does
98c70 20 61 63 74 75 61 6c 20 73 68 75 74 64 6f 77 6e   actual shutdown
98c80 20 0a 20 20 2a 2a 20 28 77 68 69 63 68 20 73 68   .  ** (which sh
98c90 6f 75 6c 64 20 62 65 20 74 68 65 20 6c 61 73 74  ould be the last
98ca0 20 74 6f 20 73 68 75 74 64 6f 77 6e 2e 29 20 2a   to shutdown.) *
98cb0 2f 0a 20 20 69 66 28 20 49 6e 74 65 72 6c 6f 63  /.  if( Interloc
98cc0 6b 65 64 43 6f 6d 70 61 72 65 45 78 63 68 61 6e  kedCompareExchan
98cd0 67 65 28 26 77 69 6e 4d 75 74 65 78 5f 6c 6f 63  ge(&winMutex_loc
98ce0 6b 2c 20 30 2c 20 31 29 3d 3d 31 20 29 7b 0a 20  k, 0, 1)==1 ){. 
98cf0 20 20 20 69 66 28 20 77 69 6e 4d 75 74 65 78 5f     if( winMutex_
98d00 69 73 49 6e 69 74 3d 3d 31 20 29 7b 0a 20 20 20  isInit==1 ){.   
98d10 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
98d20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72 72 61 79  for(i=0; i<Array
98d30 53 69 7a 65 28 77 69 6e 4d 75 74 65 78 5f 73 74  Size(winMutex_st
98d40 61 74 69 63 4d 75 74 65 78 65 73 29 3b 20 69 2b  aticMutexes); i+
98d50 2b 29 7b 0a 20 20 20 20 20 20 20 20 44 65 6c 65  +){.        Dele
98d60 74 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f  teCriticalSectio
98d70 6e 28 26 77 69 6e 4d 75 74 65 78 5f 73 74 61 74  n(&winMutex_stat
98d80 69 63 4d 75 74 65 78 65 73 5b 69 5d 2e 6d 75 74  icMutexes[i].mut
98d90 65 78 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ex);.      }.   
98da0 20 20 20 77 69 6e 4d 75 74 65 78 5f 69 73 49 6e     winMutex_isIn
98db0 69 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  it = 0;.    }.  
98dc0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
98dd0 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  E_OK; .}../*.** 
98de0 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
98df0 78 5f 61 6c 6c 6f 63 28 29 20 72 6f 75 74 69 6e  x_alloc() routin
98e00 65 20 61 6c 6c 6f 63 61 74 65 73 20 61 20 6e 65  e allocates a ne
98e10 77 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 72  w.** mutex and r
98e20 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
98e30 20 74 6f 20 69 74 2e 20 20 49 66 20 69 74 20 72   to it.  If it r
98e40 65 74 75 72 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74  eturns NULL.** t
98e50 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 61  hat means that a
98e60 20 6d 75 74 65 78 20 63 6f 75 6c 64 20 6e 6f 74   mutex could not
98e70 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20   be allocated.  
98e80 53 51 4c 69 74 65 0a 2a 2a 20 77 69 6c 6c 20 75  SQLite.** will u
98e90 6e 77 69 6e 64 20 69 74 73 20 73 74 61 63 6b 20  nwind its stack 
98ea0 61 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72  and return an er
98eb0 72 6f 72 2e 20 20 54 68 65 20 61 72 67 75 6d 65  ror.  The argume
98ec0 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33  nt.** to sqlite3
98ed0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 69  _mutex_alloc() i
98ee0 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 69  s one of these i
98ef0 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 73  nteger constants
98f00 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  :.**.** <ul>.** 
98f10 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
98f20 45 58 5f 46 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20  EX_FAST.** <li> 
98f30 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
98f40 43 55 52 53 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20  CURSIVE.** <li> 
98f50 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
98f60 41 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c  ATIC_MASTER.** <
98f70 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
98f80 58 5f 53 54 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20  X_STATIC_MEM.** 
98f90 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
98fa0 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 32 0a 2a  EX_STATIC_MEM2.*
98fb0 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d  * <li>  SQLITE_M
98fc0 55 54 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47  UTEX_STATIC_PRNG
98fd0 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45  .** <li>  SQLITE
98fe0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52  _MUTEX_STATIC_LR
98ff0 55 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  U.** <li>  SQLIT
99000 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c  E_MUTEX_STATIC_L
99010 52 55 32 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a  RU2.** </ul>.**.
99020 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 77 6f  ** The first two
99030 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 75 73 65   constants cause
99040 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
99050 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65 61 74 65  lloc() to create
99060 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74 65 78 2e  .** a new mutex.
99070 20 20 54 68 65 20 6e 65 77 20 6d 75 74 65 78 20    The new mutex 
99080 69 73 20 72 65 63 75 72 73 69 76 65 20 77 68 65  is recursive whe
99090 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  n SQLITE_MUTEX_R
990a0 45 43 55 52 53 49 56 45 0a 2a 2a 20 69 73 20 75  ECURSIVE.** is u
990b0 73 65 64 20 62 75 74 20 6e 6f 74 20 6e 65 63 65  sed but not nece
990c0 73 73 61 72 69 6c 79 20 73 6f 20 77 68 65 6e 20  ssarily so when 
990d0 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
990e0 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a 20 54 68  T is used..** Th
990f0 65 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e  e mutex implemen
99100 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20  tation does not 
99110 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 64  need to make a d
99120 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a 20 62 65  istinction.** be
99130 74 77 65 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54  tween SQLITE_MUT
99140 45 58 5f 52 45 43 55 52 53 49 56 45 20 61 6e 64  EX_RECURSIVE and
99150 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
99160 53 54 20 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a  ST if it does.**
99170 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e 20 20 42   not want to.  B
99180 75 74 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6f  ut SQLite will o
99190 6e 6c 79 20 72 65 71 75 65 73 74 20 61 20 72 65  nly request a re
991a0 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 69 6e  cursive mutex in
991b0 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65 20  .** cases where 
991c0 69 74 20 72 65 61 6c 6c 79 20 6e 65 65 64 73 20  it really needs 
991d0 6f 6e 65 2e 20 20 49 66 20 61 20 66 61 73 74 65  one.  If a faste
991e0 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20  r non-recursive 
991f0 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c 65 6d 65  mutex.** impleme
99200 6e 74 61 74 69 6f 6e 20 69 73 20 61 76 61 69 6c  ntation is avail
99210 61 62 6c 65 20 6f 6e 20 74 68 65 20 68 6f 73 74  able on the host
99220 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68 65 20 6d   platform, the m
99230 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 0a 2a  utex subsystem.*
99240 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20 73  * might return s
99250 75 63 68 20 61 20 6d 75 74 65 78 20 69 6e 20 72  uch a mutex in r
99260 65 73 70 6f 6e 73 65 20 74 6f 20 53 51 4c 49 54  esponse to SQLIT
99270 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e 0a 2a 2a  E_MUTEX_FAST..**
99280 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72 20 61 6c  .** The other al
99290 6c 6f 77 65 64 20 70 61 72 61 6d 65 74 65 72 73  lowed parameters
992a0 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   to sqlite3_mute
992b0 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63 68 20 72  x_alloc() each r
992c0 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74  eturn.** a point
992d0 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 70  er to a static p
992e0 72 65 65 78 69 73 74 69 6e 67 20 6d 75 74 65 78  reexisting mutex
992f0 2e 20 20 53 69 78 20 73 74 61 74 69 63 20 6d 75  .  Six static mu
99300 74 65 78 65 73 20 61 72 65 0a 2a 2a 20 75 73 65  texes are.** use
99310 64 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74  d by the current
99320 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69   version of SQLi
99330 74 65 2e 20 20 46 75 74 75 72 65 20 76 65 72 73  te.  Future vers
99340 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a  ions of SQLite.*
99350 2a 20 6d 61 79 20 61 64 64 20 61 64 64 69 74 69  * may add additi
99360 6f 6e 61 6c 20 73 74 61 74 69 63 20 6d 75 74 65  onal static mute
99370 78 65 73 2e 20 20 53 74 61 74 69 63 20 6d 75 74  xes.  Static mut
99380 65 78 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74  exes are for int
99390 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20  ernal.** use by 
993a0 53 51 4c 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70  SQLite only.  Ap
993b0 70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20  plications that 
993c0 75 73 65 20 53 51 4c 69 74 65 20 6d 75 74 65 78  use SQLite mutex
993d0 65 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65  es should.** use
993e0 20 6f 6e 6c 79 20 74 68 65 20 64 79 6e 61 6d 69   only the dynami
993f0 63 20 6d 75 74 65 78 65 73 20 72 65 74 75 72 6e  c mutexes return
99400 65 64 20 62 79 20 53 51 4c 49 54 45 5f 4d 55 54  ed by SQLITE_MUT
99410 45 58 5f 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51  EX_FAST or.** SQ
99420 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
99430 53 49 56 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65  SIVE..**.** Note
99440 20 74 68 61 74 20 69 66 20 6f 6e 65 20 6f 66 20   that if one of 
99450 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65  the dynamic mute
99460 78 20 70 61 72 61 6d 65 74 65 72 73 20 28 53 51  x parameters (SQ
99470 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a  LITE_MUTEX_FAST.
99480 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54  ** or SQLITE_MUT
99490 45 58 5f 52 45 43 55 52 53 49 56 45 29 20 69 73  EX_RECURSIVE) is
994a0 20 75 73 65 64 20 74 68 65 6e 20 73 71 6c 69 74   used then sqlit
994b0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
994c0 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20 64 69  .** returns a di
994d0 66 66 65 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e  fferent mutex on
994e0 20 65 76 65 72 79 20 63 61 6c 6c 2e 20 20 42 75   every call.  Bu
994f0 74 20 66 6f 72 20 74 68 65 20 73 74 61 74 69 63  t for the static
99500 20 0a 2a 2a 20 6d 75 74 65 78 20 74 79 70 65 73   .** mutex types
99510 2c 20 74 68 65 20 73 61 6d 65 20 6d 75 74 65 78  , the same mutex
99520 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20   is returned on 
99530 65 76 65 72 79 20 63 61 6c 6c 20 74 68 61 74 20  every call that 
99540 68 61 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20  has.** the same 
99550 74 79 70 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a  type number..*/.
99560 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d  static sqlite3_m
99570 75 74 65 78 20 2a 77 69 6e 4d 75 74 65 78 41 6c  utex *winMutexAl
99580 6c 6f 63 28 69 6e 74 20 69 54 79 70 65 29 7b 0a  loc(int iType){.
99590 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
995a0 2a 70 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 69  *p;..  switch( i
995b0 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73 65  Type ){.    case
995c0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
995d0 53 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c  ST:.    case SQL
995e0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
995f0 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 20 3d  IVE: {.      p =
99600 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65   sqlite3MallocZe
99610 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29  ro( sizeof(*p) )
99620 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20 29 7b  ;.      if( p ){
99630 20 20 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64    .        p->id
99640 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20   = iType;.      
99650 20 20 49 6e 69 74 69 61 6c 69 7a 65 43 72 69 74    InitializeCrit
99660 69 63 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e  icalSection(&p->
99670 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 7d 0a  mutex);.      }.
99680 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
99690 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20   }.    default: 
996a0 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
996b0 77 69 6e 4d 75 74 65 78 5f 69 73 49 6e 69 74 3d  winMutex_isInit=
996c0 3d 31 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  =1 );.      asse
996d0 72 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20 30  rt( iType-2 >= 0
996e0 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
996f0 28 20 69 54 79 70 65 2d 32 20 3c 20 41 72 72 61  ( iType-2 < Arra
99700 79 53 69 7a 65 28 77 69 6e 4d 75 74 65 78 5f 73  ySize(winMutex_s
99710 74 61 74 69 63 4d 75 74 65 78 65 73 29 20 29 3b  taticMutexes) );
99720 0a 20 20 20 20 20 20 70 20 3d 20 26 77 69 6e 4d  .      p = &winM
99730 75 74 65 78 5f 73 74 61 74 69 63 4d 75 74 65 78  utex_staticMutex
99740 65 73 5b 69 54 79 70 65 2d 32 5d 3b 0a 20 20 20  es[iType-2];.   
99750 20 20 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65     p->id = iType
99760 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
99770 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
99780 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54  n p;.}.../*.** T
99790 68 69 73 20 72 6f 75 74 69 6e 65 20 64 65 61 6c  his routine deal
997a0 6c 6f 63 61 74 65 73 20 61 20 70 72 65 76 69 6f  locates a previo
997b0 75 73 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65  usly.** allocate
997c0 64 20 6d 75 74 65 78 2e 20 20 53 51 4c 69 74 65  d mutex.  SQLite
997d0 20 69 73 20 63 61 72 65 66 75 6c 20 74 6f 20 64   is careful to d
997e0 65 61 6c 6c 6f 63 61 74 65 20 65 76 65 72 79 0a  eallocate every.
997f0 2a 2a 20 6d 75 74 65 78 20 74 68 61 74 20 69 74  ** mutex that it
99800 20 61 6c 6c 6f 63 61 74 65 73 2e 0a 2a 2f 0a 73   allocates..*/.s
99810 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 4d 75  tatic void winMu
99820 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f  texFree(sqlite3_
99830 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73  mutex *p){.  ass
99840 65 72 74 28 20 70 20 29 3b 0a 20 20 61 73 73 65  ert( p );.  asse
99850 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29  rt( p->nRef==0 )
99860 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  ;.  assert( p->i
99870 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
99880 46 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53  FAST || p->id==S
99890 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
998a0 52 53 49 56 45 20 29 3b 0a 20 20 44 65 6c 65 74  RSIVE );.  Delet
998b0 65 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e  eCriticalSection
998c0 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73  (&p->mutex);.  s
998d0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
998e0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
998f0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
99900 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d  () and sqlite3_m
99910 75 74 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69  utex_try() routi
99920 6e 65 73 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74  nes attempt.** t
99930 6f 20 65 6e 74 65 72 20 61 20 6d 75 74 65 78 2e  o enter a mutex.
99940 20 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72    If another thr
99950 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20 77  ead is already w
99960 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78 2c  ithin the mutex,
99970 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  .** sqlite3_mute
99980 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20 62  x_enter() will b
99990 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65 33  lock and sqlite3
999a0 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69 6c  _mutex_try() wil
999b0 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49  l return.** SQLI
999c0 54 45 5f 42 55 53 59 2e 20 20 54 68 65 20 73 71  TE_BUSY.  The sq
999d0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
999e0 29 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  ) interface retu
999f0 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a  rns SQLITE_OK.**
99a00 20 75 70 6f 6e 20 73 75 63 63 65 73 73 66 75 6c   upon successful
99a10 20 65 6e 74 72 79 2e 20 20 4d 75 74 65 78 65 73   entry.  Mutexes
99a20 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 53   created using S
99a30 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
99a40 52 53 49 56 45 20 63 61 6e 0a 2a 2a 20 62 65 20  RSIVE can.** be 
99a50 65 6e 74 65 72 65 64 20 6d 75 6c 74 69 70 6c 65  entered multiple
99a60 20 74 69 6d 65 73 20 62 79 20 74 68 65 20 73 61   times by the sa
99a70 6d 65 20 74 68 72 65 61 64 2e 20 20 49 6e 20 73  me thread.  In s
99a80 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a  uch cases the,.*
99a90 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  * mutex must be 
99aa0 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20  exited an equal 
99ab0 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  number of times 
99ac0 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74  before another t
99ad0 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74  hread.** can ent
99ae0 65 72 2e 20 20 49 66 20 74 68 65 20 73 61 6d 65  er.  If the same
99af0 20 74 68 72 65 61 64 20 74 72 69 65 73 20 74 6f   thread tries to
99b00 20 65 6e 74 65 72 20 61 6e 79 20 6f 74 68 65 72   enter any other
99b10 20 6b 69 6e 64 20 6f 66 20 6d 75 74 65 78 0a 2a   kind of mutex.*
99b20 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65  * more than once
99b30 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 69  , the behavior i
99b40 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  s undefined..*/.
99b50 73 74 61 74 69 63 20 76 6f 69 64 20 77 69 6e 4d  static void winM
99b60 75 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65  utexEnter(sqlite
99b70 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61  3_mutex *p){.  a
99b80 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
99b90 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
99ba0 53 49 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78  SIVE || winMutex
99bb0 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20  Notheld(p) );.  
99bc0 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63  EnterCriticalSec
99bd0 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 3b  tion(&p->mutex);
99be0 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 47 65  .  p->owner = Ge
99bf0 74 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64  tCurrentThreadId
99c00 28 29 3b 20 0a 20 20 70 2d 3e 6e 52 65 66 2b 2b  (); .  p->nRef++
99c10 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77  ;.}.static int w
99c20 69 6e 4d 75 74 65 78 54 72 79 28 73 71 6c 69 74  inMutexTry(sqlit
99c30 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
99c40 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
99c50 42 55 53 59 3b 0a 20 20 61 73 73 65 72 74 28 20  BUSY;.  assert( 
99c60 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
99c70 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c  TEX_RECURSIVE ||
99c80 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65 6c 64   winMutexNotheld
99c90 28 70 29 20 29 3b 0a 20 20 2f 2a 0a 20 20 2a 2a  (p) );.  /*.  **
99ca0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
99cb0 65 78 5f 74 72 79 28 29 20 72 6f 75 74 69 6e 65  ex_try() routine
99cc0 20 69 73 20 76 65 72 79 20 72 61 72 65 6c 79 20   is very rarely 
99cd0 75 73 65 64 2c 20 61 6e 64 20 77 68 65 6e 20 69  used, and when i
99ce0 74 0a 20 20 2a 2a 20 69 73 20 75 73 65 64 20 69  t.  ** is used i
99cf0 74 20 69 73 20 6d 65 72 65 6c 79 20 61 6e 20 6f  t is merely an o
99d00 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 53 6f  ptimization.  So
99d10 20 69 74 20 69 73 20 4f 4b 20 66 6f 72 20 69 74   it is OK for it
99d20 20 74 6f 20 61 6c 77 61 79 73 0a 20 20 2a 2a 20   to always.  ** 
99d30 66 61 69 6c 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a  fail.  .  **.  *
99d40 2a 20 54 68 65 20 54 72 79 45 6e 74 65 72 43 72  * The TryEnterCr
99d50 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28 29 20  iticalSection() 
99d60 69 6e 74 65 72 66 61 63 65 20 69 73 20 6f 6e 6c  interface is onl
99d70 79 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 57  y available on W
99d80 69 6e 4e 54 2e 0a 20 20 2a 2a 20 41 6e 64 20 73  inNT..  ** And s
99d90 6f 6d 65 20 77 69 6e 64 6f 77 73 20 63 6f 6d 70  ome windows comp
99da0 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 69  ilers complain i
99db0 66 20 79 6f 75 20 74 72 79 20 74 6f 20 75 73 65  f you try to use
99dc0 20 69 74 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a   it without.  **
99dd0 20 66 69 72 73 74 20 64 6f 69 6e 67 20 73 6f 6d   first doing som
99de0 65 20 23 64 65 66 69 6e 65 73 20 74 68 61 74 20  e #defines that 
99df0 70 72 65 76 65 6e 74 20 53 51 4c 69 74 65 20 66  prevent SQLite f
99e00 72 6f 6d 20 62 75 69 6c 64 69 6e 67 20 6f 6e 20  rom building on 
99e10 57 69 6e 39 38 2e 0a 20 20 2a 2a 20 46 6f 72 20  Win98..  ** For 
99e20 74 68 61 74 20 72 65 61 73 6f 6e 2c 20 77 65 20  that reason, we 
99e30 77 69 6c 6c 20 6f 6d 69 74 20 74 68 69 73 20 6f  will omit this o
99e40 70 74 69 6d 69 7a 61 74 69 6f 6e 20 66 6f 72 20  ptimization for 
99e50 6e 6f 77 2e 20 20 53 65 65 0a 20 20 2a 2a 20 74  now.  See.  ** t
99e60 69 63 6b 65 74 20 23 32 36 38 35 2e 0a 20 20 2a  icket #2685..  *
99e70 2f 0a 23 69 66 20 30 0a 20 20 69 66 28 20 6d 75  /.#if 0.  if( mu
99e80 74 65 78 49 73 4e 54 28 29 20 26 26 20 54 72 79  texIsNT() && Try
99e90 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63  EnterCriticalSec
99ea0 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29 20  tion(&p->mutex) 
99eb0 29 7b 0a 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20  ){.    p->owner 
99ec0 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68 72 65  = GetCurrentThre
99ed0 61 64 49 64 28 29 3b 0a 20 20 20 20 70 2d 3e 6e  adId();.    p->n
99ee0 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20  Ref++;.    rc = 
99ef0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 23  SQLITE_OK;.  }.#
99f00 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41  else.  UNUSED_PA
99f10 52 41 4d 45 54 45 52 28 70 29 3b 0a 23 65 6e 64  RAMETER(p);.#end
99f20 69 66 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  if.  return rc;.
99f30 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
99f40 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
99f50 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73  () routine exits
99f60 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61   a mutex that wa
99f70 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20  s.** previously 
99f80 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73  entered by the s
99f90 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65  ame thread.  The
99fa0 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20   behavior.** is 
99fb0 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65  undefined if the
99fc0 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75   mutex is not cu
99fd0 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20  rrently entered 
99fe0 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72  or.** is not cur
99ff0 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64  rently allocated
9a000 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e  .  SQLite will n
9a010 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a  ever do either..
9a020 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
9a030 69 6e 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c  inMutexLeave(sql
9a040 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
9a050 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65    assert( p->nRe
9a060 66 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  f>0 );.  assert(
9a070 20 70 2d 3e 6f 77 6e 65 72 3d 3d 47 65 74 43 75   p->owner==GetCu
9a080 72 72 65 6e 74 54 68 72 65 61 64 49 64 28 29 20  rrentThreadId() 
9a090 29 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a  );.  p->nRef--;.
9a0a0 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65    assert( p->nRe
9a0b0 66 3d 3d 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53  f==0 || p->id==S
9a0c0 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
9a0d0 52 53 49 56 45 20 29 3b 0a 20 20 4c 65 61 76 65  RSIVE );.  Leave
9a0e0 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28  CriticalSection(
9a0f0 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 0a 53  &p->mutex);.}..S
9a100 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71  QLITE_PRIVATE sq
9a110 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68  lite3_mutex_meth
9a120 6f 64 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61  ods *sqlite3Defa
9a130 75 6c 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a  ultMutex(void){.
9a140 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33    static sqlite3
9a150 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73  _mutex_methods s
9a160 4d 75 74 65 78 20 3d 20 7b 0a 20 20 20 20 77 69  Mutex = {.    wi
9a170 6e 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20  nMutexInit,.    
9a180 77 69 6e 4d 75 74 65 78 45 6e 64 2c 0a 20 20 20  winMutexEnd,.   
9a190 20 77 69 6e 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a   winMutexAlloc,.
9a1a0 20 20 20 20 77 69 6e 4d 75 74 65 78 46 72 65 65      winMutexFree
9a1b0 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 45 6e  ,.    winMutexEn
9a1c0 74 65 72 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65  ter,.    winMute
9a1d0 78 54 72 79 2c 0a 20 20 20 20 77 69 6e 4d 75 74  xTry,.    winMut
9a1e0 65 78 4c 65 61 76 65 2c 0a 23 69 66 64 65 66 20  exLeave,.#ifdef 
9a1f0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20  SQLITE_DEBUG.   
9a200 20 77 69 6e 4d 75 74 65 78 48 65 6c 64 2c 0a 20   winMutexHeld,. 
9a210 20 20 20 77 69 6e 4d 75 74 65 78 4e 6f 74 68 65     winMutexNothe
9a220 6c 64 0a 23 65 6c 73 65 0a 20 20 20 20 30 2c 0a  ld.#else.    0,.
9a230 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 20 20 7d      0.#endif.  }
9a240 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73 4d 75  ;..  return &sMu
9a250 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  tex;.}.#endif /*
9a260 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33   SQLITE_MUTEX_W3
9a270 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  2 */../*********
9a280 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74  ***** End of mut
9a290 65 78 5f 77 33 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a  ex_w32.c *******
9a2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a2c0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
9a2d0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
9a2e0 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a 2a   malloc.c ******
9a2f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a310 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
9a320 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
9a330 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
9a340 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
9a350 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
9a360 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
9a370 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
9a380 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
9a390 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
9a3a0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
9a3b0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
9a3c0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
9a3d0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
9a3e0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
9a3f0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
9a400 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
9a410 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
9a420 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
9a430 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
9a440 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
9a450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
9a490 2a 2a 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  ** Memory alloca
9a4a0 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 75  tion functions u
9a4b0 73 65 64 20 74 68 72 6f 75 67 68 6f 75 74 20 73  sed throughout s
9a4c0 71 6c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  qlite..**.** $Id
9a4d0 3a 20 6d 61 6c 6c 6f 63 2e 63 2c 76 20 31 2e 36  : malloc.c,v 1.6
9a4e0 36 20 32 30 30 39 2f 30 37 2f 31 37 20 31 31 3a  6 2009/07/17 11:
9a4f0 34 34 3a 30 37 20 64 72 68 20 45 78 70 20 24 0a  44:07 drh Exp $.
9a500 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  */../*.** This r
9a510 6f 75 74 69 6e 65 20 72 75 6e 73 20 77 68 65 6e  outine runs when
9a520 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
9a530 63 61 74 6f 72 20 73 65 65 73 20 74 68 61 74 20  cator sees that 
9a540 74 68 65 0a 2a 2a 20 74 6f 74 61 6c 20 6d 65 6d  the.** total mem
9a550 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69  ory allocation i
9a560 73 20 61 62 6f 75 74 20 74 6f 20 65 78 63 65 65  s about to excee
9a570 64 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 0a  d the soft heap.
9a580 2a 2a 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 73 74 61  ** limit..*/.sta
9a590 74 69 63 20 76 6f 69 64 20 73 6f 66 74 48 65 61  tic void softHea
9a5a0 70 4c 69 6d 69 74 45 6e 66 6f 72 63 65 72 28 0a  pLimitEnforcer(.
9a5b0 20 20 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c    void *NotUsed,
9a5c0 20 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36   .  sqlite3_int6
9a5d0 34 20 4e 6f 74 55 73 65 64 32 2c 0a 20 20 69 6e  4 NotUsed2,.  in
9a5e0 74 20 61 6c 6c 6f 63 53 69 7a 65 0a 29 7b 0a 20  t allocSize.){. 
9a5f0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
9a600 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55  R2(NotUsed, NotU
9a610 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33  sed2);.  sqlite3
9a620 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28  _release_memory(
9a630 61 6c 6c 6f 63 53 69 7a 65 29 3b 0a 7d 0a 0a 2f  allocSize);.}../
9a640 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 6f 66  *.** Set the sof
9a650 74 20 68 65 61 70 2d 73 69 7a 65 20 6c 69 6d 69  t heap-size limi
9a660 74 20 66 6f 72 20 74 68 65 20 6c 69 62 72 61 72  t for the librar
9a670 79 2e 20 50 61 73 73 69 6e 67 20 61 20 7a 65 72  y. Passing a zer
9a680 6f 20 6f 72 20 0a 2a 2a 20 6e 65 67 61 74 69 76  o or .** negativ
9a690 65 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65  e value indicate
9a6a0 73 20 6e 6f 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 53  s no limit..*/.S
9a6b0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
9a6c0 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61 70  qlite3_soft_heap
9a6d0 5f 6c 69 6d 69 74 28 69 6e 74 20 6e 29 7b 0a 20  _limit(int n){. 
9a6e0 20 73 71 6c 69 74 65 33 5f 75 69 6e 74 36 34 20   sqlite3_uint64 
9a6f0 69 4c 69 6d 69 74 3b 0a 20 20 69 6e 74 20 6f 76  iLimit;.  int ov
9a700 65 72 61 67 65 3b 0a 20 20 69 66 28 20 6e 3c 30  erage;.  if( n<0
9a710 20 29 7b 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d   ){.    iLimit =
9a720 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
9a730 20 69 4c 69 6d 69 74 20 3d 20 6e 3b 0a 20 20 7d   iLimit = n;.  }
9a740 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
9a750 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20  OMIT_AUTOINIT.  
9a760 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
9a770 7a 65 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69  ze();.#endif.  i
9a780 66 28 20 69 4c 69 6d 69 74 3e 30 20 29 7b 0a 20  f( iLimit>0 ){. 
9a790 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79     sqlite3Memory
9a7a0 41 6c 61 72 6d 28 73 6f 66 74 48 65 61 70 4c 69  Alarm(softHeapLi
9a7b0 6d 69 74 45 6e 66 6f 72 63 65 72 2c 20 30 2c 20  mitEnforcer, 0, 
9a7c0 69 4c 69 6d 69 74 29 3b 0a 20 20 7d 65 6c 73 65  iLimit);.  }else
9a7d0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  {.    sqlite3Mem
9a7e0 6f 72 79 41 6c 61 72 6d 28 30 2c 20 30 2c 20 30  oryAlarm(0, 0, 0
9a7f0 29 3b 0a 20 20 7d 0a 20 20 6f 76 65 72 61 67 65  );.  }.  overage
9a800 20 3d 20 28 69 6e 74 29 28 73 71 6c 69 74 65 33   = (int)(sqlite3
9a810 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 20 2d  _memory_used() -
9a820 20 28 69 36 34 29 6e 29 3b 0a 20 20 69 66 28 20   (i64)n);.  if( 
9a830 6f 76 65 72 61 67 65 3e 30 20 29 7b 0a 20 20 20  overage>0 ){.   
9a840 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65   sqlite3_release
9a850 5f 6d 65 6d 6f 72 79 28 6f 76 65 72 61 67 65 29  _memory(overage)
9a860 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ;.  }.}../*.** A
9a870 74 74 65 6d 70 74 20 74 6f 20 72 65 6c 65 61 73  ttempt to releas
9a880 65 20 75 70 20 74 6f 20 6e 20 62 79 74 65 73 20  e up to n bytes 
9a890 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c  of non-essential
9a8a0 20 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e 74 6c   memory currentl
9a8b0 79 0a 2a 2a 20 68 65 6c 64 20 62 79 20 53 51 4c  y.** held by SQL
9a8c0 69 74 65 2e 20 41 6e 20 65 78 61 6d 70 6c 65 20  ite. An example 
9a8d0 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c  of non-essential
9a8e0 20 6d 65 6d 6f 72 79 20 69 73 20 6d 65 6d 6f 72   memory is memor
9a8f0 79 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 61 63  y used to.** cac
9a900 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67 65  he database page
9a910 73 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 63  s that are not c
9a920 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e  urrently in use.
9a930 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
9a940 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61  nt sqlite3_relea
9a950 73 65 5f 6d 65 6d 6f 72 79 28 69 6e 74 20 6e 29  se_memory(int n)
9a960 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  {.#ifdef SQLITE_
9a970 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41  ENABLE_MEMORY_MA
9a980 4e 41 47 45 4d 45 4e 54 0a 20 20 69 6e 74 20 6e  NAGEMENT.  int n
9a990 52 65 74 20 3d 20 30 3b 0a 20 20 6e 52 65 74 20  Ret = 0;.  nRet 
9a9a0 2b 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  += sqlite3Pcache
9a9b0 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e 2d  ReleaseMemory(n-
9a9c0 6e 52 65 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  nRet);.  return 
9a9d0 6e 52 65 74 3b 0a 23 65 6c 73 65 0a 20 20 55 4e  nRet;.#else.  UN
9a9e0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e  USED_PARAMETER(n
9a9f0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
9aa00 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 7d 0a  TE_OK;.#endif.}.
9aa10 0a 2f 2a 0a 2a 2a 20 53 74 61 74 65 20 69 6e 66  ./*.** State inf
9aa20 6f 72 6d 61 74 69 6f 6e 20 6c 6f 63 61 6c 20 74  ormation local t
9aa30 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  o the memory all
9aa40 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
9aa50 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c  m..*/.static SQL
9aa60 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 4d  ITE_WSD struct M
9aa70 65 6d 30 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f 2a  em0Global {.  /*
9aa80 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20   Number of free 
9aa90 70 61 67 65 73 20 66 6f 72 20 73 63 72 61 74 63  pages for scratc
9aaa0 68 20 61 6e 64 20 70 61 67 65 2d 63 61 63 68 65  h and page-cache
9aab0 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 75 33 32   memory */.  u32
9aac0 20 6e 53 63 72 61 74 63 68 46 72 65 65 3b 0a 20   nScratchFree;. 
9aad0 20 75 33 32 20 6e 50 61 67 65 46 72 65 65 3b 0a   u32 nPageFree;.
9aae0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
9aaf0 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20   *mutex;        
9ab00 20 2f 2a 20 4d 75 74 65 78 20 74 6f 20 73 65 72   /* Mutex to ser
9ab10 69 61 6c 69 7a 65 20 61 63 63 65 73 73 20 2a 2f  ialize access */
9ab20 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  /*.  ** The 
9ab30 61 6c 61 72 6d 20 63 61 6c 6c 62 61 63 6b 20 61  alarm callback a
9ab40 6e 64 20 69 74 73 20 61 72 67 75 6d 65 6e 74 73  nd its arguments
9ab50 2e 20 20 54 68 65 20 6d 65 6d 30 2e 6d 75 74 65  .  The mem0.mute
9ab60 78 20 6c 6f 63 6b 20 77 69 6c 6c 0a 20 20 2a 2a  x lock will.  **
9ab70 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65 20 74   be held while t
9ab80 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 72  he callback is r
9ab90 75 6e 6e 69 6e 67 2e 20 20 52 65 63 75 72 73 69  unning.  Recursi
9aba0 76 65 20 63 61 6c 6c 73 20 69 6e 74 6f 0a 20 20  ve calls into.  
9abb0 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75  ** the memory su
9abc0 62 73 79 73 74 65 6d 20 61 72 65 20 61 6c 6c 6f  bsystem are allo
9abd0 77 65 64 2c 20 62 75 74 20 6e 6f 20 6e 65 77 20  wed, but no new 
9abe0 63 61 6c 6c 62 61 63 6b 73 20 77 69 6c 6c 20 62  callbacks will b
9abf0 65 0a 20 20 2a 2a 20 69 73 73 75 65 64 2e 0a 20  e.  ** issued.. 
9ac00 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
9ac10 74 36 34 20 61 6c 61 72 6d 54 68 72 65 73 68 6f  t64 alarmThresho
9ac20 6c 64 3b 0a 20 20 76 6f 69 64 20 28 2a 61 6c 61  ld;.  void (*ala
9ac30 72 6d 43 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64  rmCallback)(void
9ac40 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  *, sqlite3_int64
9ac50 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 2a 61  ,int);.  void *a
9ac60 6c 61 72 6d 41 72 67 3b 0a 0a 20 20 2f 2a 0a 20  larmArg;..  /*. 
9ac70 20 2a 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20   ** Pointers to 
9ac80 74 68 65 20 65 6e 64 20 6f 66 20 73 71 6c 69 74  the end of sqlit
9ac90 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
9aca0 53 63 72 61 74 63 68 20 61 6e 64 0a 20 20 2a 2a  Scratch and.  **
9acb0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
9acc0 6e 66 69 67 2e 70 50 61 67 65 20 74 6f 20 61 20  nfig.pPage to a 
9acd0 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20  block of memory 
9ace0 74 68 61 74 20 72 65 63 6f 72 64 73 0a 20 20 2a  that records.  *
9acf0 2a 20 77 68 69 63 68 20 70 61 67 65 73 20 61 72  * which pages ar
9ad00 65 20 61 76 61 69 6c 61 62 6c 65 2e 0a 20 20 2a  e available..  *
9ad10 2f 0a 20 20 75 33 32 20 2a 61 53 63 72 61 74 63  /.  u32 *aScratc
9ad20 68 46 72 65 65 3b 0a 20 20 75 33 32 20 2a 61 50  hFree;.  u32 *aP
9ad30 61 67 65 46 72 65 65 3b 0a 7d 20 6d 65 6d 30 20  ageFree;.} mem0 
9ad40 3d 20 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20  = { 0, 0, 0, 0, 
9ad50 30 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a 23  0, 0, 0, 0 };..#
9ad60 64 65 66 69 6e 65 20 6d 65 6d 30 20 47 4c 4f 42  define mem0 GLOB
9ad70 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 30 47 6c  AL(struct Mem0Gl
9ad80 6f 62 61 6c 2c 20 6d 65 6d 30 29 0a 0a 2f 2a 0a  obal, mem0)../*.
9ad90 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  ** Initialize th
9ada0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
9adb0 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a  ion subsystem..*
9adc0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
9add0 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c   int sqlite3Mall
9ade0 6f 63 49 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20  ocInit(void){.  
9adf0 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
9ae00 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f  lConfig.m.xMallo
9ae10 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  c==0 ){.    sqli
9ae20 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74  te3MemSetDefault
9ae30 28 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74  ();.  }.  memset
9ae40 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a 65 6f  (&mem0, 0, sizeo
9ae50 66 28 6d 65 6d 30 29 29 3b 0a 20 20 69 66 28 20  f(mem0));.  if( 
9ae60 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9ae70 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29  fig.bCoreMutex )
9ae80 7b 0a 20 20 20 20 6d 65 6d 30 2e 6d 75 74 65 78  {.    mem0.mutex
9ae90 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
9aea0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
9aeb0 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20  X_STATIC_MEM);. 
9aec0 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
9aed0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63  GlobalConfig.pSc
9aee0 72 61 74 63 68 20 26 26 20 73 71 6c 69 74 65 33  ratch && sqlite3
9aef0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53  GlobalConfig.szS
9af00 63 72 61 74 63 68 3e 3d 31 30 30 0a 20 20 20 20  cratch>=100.    
9af10 20 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62    && sqlite3Glob
9af20 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63  alConfig.nScratc
9af30 68 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  h>=0 ){.    int 
9af40 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  i;.    sqlite3Gl
9af50 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72  obalConfig.szScr
9af60 61 74 63 68 20 3d 20 52 4f 55 4e 44 44 4f 57 4e  atch = ROUNDDOWN
9af70 38 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  8(sqlite3GlobalC
9af80 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 2d  onfig.szScratch-
9af90 34 29 3b 0a 20 20 20 20 6d 65 6d 30 2e 61 53 63  4);.    mem0.aSc
9afa0 72 61 74 63 68 46 72 65 65 20 3d 20 28 75 33 32  ratchFree = (u32
9afb0 2a 29 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74  *)&((char*)sqlit
9afc0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
9afd0 53 63 72 61 74 63 68 29 0a 20 20 20 20 20 20 20  Scratch).       
9afe0 20 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69             [sqli
9aff0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9b000 73 7a 53 63 72 61 74 63 68 2a 73 71 6c 69 74 65  szScratch*sqlite
9b010 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53  3GlobalConfig.nS
9b020 63 72 61 74 63 68 5d 3b 0a 20 20 20 20 66 6f 72  cratch];.    for
9b030 28 69 3d 30 3b 20 69 3c 73 71 6c 69 74 65 33 47  (i=0; i<sqlite3G
9b040 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72  lobalConfig.nScr
9b050 61 74 63 68 3b 20 69 2b 2b 29 7b 20 6d 65 6d 30  atch; i++){ mem0
9b060 2e 61 53 63 72 61 74 63 68 46 72 65 65 5b 69 5d  .aScratchFree[i]
9b070 20 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30   = i; }.    mem0
9b080 2e 6e 53 63 72 61 74 63 68 46 72 65 65 20 3d 20  .nScratchFree = 
9b090 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9b0a0 66 69 67 2e 6e 53 63 72 61 74 63 68 3b 0a 20 20  fig.nScratch;.  
9b0b0 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
9b0c0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
9b0d0 53 63 72 61 74 63 68 20 3d 20 30 3b 0a 20 20 20  Scratch = 0;.   
9b0e0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
9b0f0 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 20 3d  nfig.szScratch =
9b100 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71   0;.  }.  if( sq
9b110 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9b120 67 2e 70 50 61 67 65 20 26 26 20 73 71 6c 69 74  g.pPage && sqlit
9b130 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73  e3GlobalConfig.s
9b140 7a 50 61 67 65 3e 3d 35 31 32 0a 20 20 20 20 20  zPage>=512.     
9b150 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   && sqlite3Globa
9b160 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65 3e 3d 31  lConfig.nPage>=1
9b170 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
9b180 20 20 20 69 6e 74 20 6f 76 65 72 68 65 61 64 3b     int overhead;
9b190 0a 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 52 4f  .    int sz = RO
9b1a0 55 4e 44 44 4f 57 4e 38 28 73 71 6c 69 74 65 33  UNDDOWN8(sqlite3
9b1b0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50  GlobalConfig.szP
9b1c0 61 67 65 29 3b 0a 20 20 20 20 69 6e 74 20 6e 20  age);.    int n 
9b1d0 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
9b1e0 6f 6e 66 69 67 2e 6e 50 61 67 65 3b 0a 20 20 20  onfig.nPage;.   
9b1f0 20 6f 76 65 72 68 65 61 64 20 3d 20 28 34 2a 6e   overhead = (4*n
9b200 20 2b 20 73 7a 20 2d 20 31 29 2f 73 7a 3b 0a 20   + sz - 1)/sz;. 
9b210 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
9b220 43 6f 6e 66 69 67 2e 6e 50 61 67 65 20 2d 3d 20  Config.nPage -= 
9b230 6f 76 65 72 68 65 61 64 3b 0a 20 20 20 20 6d 65  overhead;.    me
9b240 6d 30 2e 61 50 61 67 65 46 72 65 65 20 3d 20 28  m0.aPageFree = (
9b250 75 33 32 2a 29 26 28 28 63 68 61 72 2a 29 73 71  u32*)&((char*)sq
9b260 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9b270 67 2e 70 50 61 67 65 29 0a 20 20 20 20 20 20 20  g.pPage).       
9b280 20 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69             [sqli
9b290 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9b2a0 73 7a 50 61 67 65 2a 73 71 6c 69 74 65 33 47 6c  szPage*sqlite3Gl
9b2b0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 50 61 67 65  obalConfig.nPage
9b2c0 5d 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ];.    for(i=0; 
9b2d0 69 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  i<sqlite3GlobalC
9b2e0 6f 6e 66 69 67 2e 6e 50 61 67 65 3b 20 69 2b 2b  onfig.nPage; i++
9b2f0 29 7b 20 6d 65 6d 30 2e 61 50 61 67 65 46 72 65  ){ mem0.aPageFre
9b300 65 5b 69 5d 20 3d 20 69 3b 20 7d 0a 20 20 20 20  e[i] = i; }.    
9b310 6d 65 6d 30 2e 6e 50 61 67 65 46 72 65 65 20 3d  mem0.nPageFree =
9b320 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
9b330 6e 66 69 67 2e 6e 50 61 67 65 3b 0a 20 20 7d 65  nfig.nPage;.  }e
9b340 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
9b350 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61  GlobalConfig.pPa
9b360 67 65 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69  ge = 0;.    sqli
9b370 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9b380 73 7a 50 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a  szPage = 0;.  }.
9b390 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
9b3a0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
9b3b0 49 6e 69 74 28 73 71 6c 69 74 65 33 47 6c 6f 62  Init(sqlite3Glob
9b3c0 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70 41 70 70 44  alConfig.m.pAppD
9b3d0 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  ata);.}../*.** D
9b3e0 65 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20  einitialize the 
9b3f0 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
9b400 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a  n subsystem..*/.
9b410 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
9b420 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  oid sqlite3Mallo
9b430 63 45 6e 64 28 76 6f 69 64 29 7b 0a 20 20 69 66  cEnd(void){.  if
9b440 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
9b450 6f 6e 66 69 67 2e 6d 2e 78 53 68 75 74 64 6f 77  onfig.m.xShutdow
9b460 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  n ){.    sqlite3
9b470 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
9b480 53 68 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 33  Shutdown(sqlite3
9b490 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70  GlobalConfig.m.p
9b4a0 41 70 70 44 61 74 61 29 3b 0a 20 20 7d 0a 20 20  AppData);.  }.  
9b4b0 6d 65 6d 73 65 74 28 26 6d 65 6d 30 2c 20 30 2c  memset(&mem0, 0,
9b4c0 20 73 69 7a 65 6f 66 28 6d 65 6d 30 29 29 3b 0a   sizeof(mem0));.
9b4d0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
9b4e0 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65  the amount of me
9b4f0 6d 6f 72 79 20 63 75 72 72 65 6e 74 6c 79 20 63  mory currently c
9b500 68 65 63 6b 65 64 20 6f 75 74 2e 0a 2a 2f 0a 53  hecked out..*/.S
9b510 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
9b520 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f  3_int64 sqlite3_
9b530 6d 65 6d 6f 72 79 5f 75 73 65 64 28 76 6f 69 64  memory_used(void
9b540 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a  ){.  int n, mx;.
9b550 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
9b560 72 65 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  res;.  sqlite3_s
9b570 74 61 74 75 73 28 53 51 4c 49 54 45 5f 53 54 41  tatus(SQLITE_STA
9b580 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c  TUS_MEMORY_USED,
9b590 20 26 6e 2c 20 26 6d 78 2c 20 30 29 3b 0a 20 20   &n, &mx, 0);.  
9b5a0 72 65 73 20 3d 20 28 73 71 6c 69 74 65 33 5f 69  res = (sqlite3_i
9b5b0 6e 74 36 34 29 6e 3b 20 20 2f 2a 20 57 6f 72 6b  nt64)n;  /* Work
9b5c0 20 61 72 6f 75 6e 64 20 62 75 67 20 69 6e 20 42   around bug in B
9b5d0 6f 72 6c 61 6e 64 20 43 2e 20 54 69 63 6b 65 74  orland C. Ticket
9b5e0 20 23 33 32 31 36 20 2a 2f 0a 20 20 72 65 74 75   #3216 */.  retu
9b5f0 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rn res;.}../*.**
9b600 20 52 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69   Return the maxi
9b610 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65  mum amount of me
9b620 6d 6f 72 79 20 74 68 61 74 20 68 61 73 20 65 76  mory that has ev
9b630 65 72 20 62 65 65 6e 0a 2a 2a 20 63 68 65 63 6b  er been.** check
9b640 65 64 20 6f 75 74 20 73 69 6e 63 65 20 65 69 74  ed out since eit
9b650 68 65 72 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  her the beginnin
9b660 67 20 6f 66 20 74 68 69 73 20 70 72 6f 63 65 73  g of this proces
9b670 73 0a 2a 2a 20 6f 72 20 73 69 6e 63 65 20 74 68  s.** or since th
9b680 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 72 65  e most recent re
9b690 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  set..*/.SQLITE_A
9b6a0 50 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  PI sqlite3_int64
9b6b0 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f   sqlite3_memory_
9b6c0 68 69 67 68 77 61 74 65 72 28 69 6e 74 20 72 65  highwater(int re
9b6d0 73 65 74 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20  setFlag){.  int 
9b6e0 6e 2c 20 6d 78 3b 0a 20 20 73 71 6c 69 74 65 33  n, mx;.  sqlite3
9b6f0 5f 69 6e 74 36 34 20 72 65 73 3b 0a 20 20 73 71  _int64 res;.  sq
9b700 6c 69 74 65 33 5f 73 74 61 74 75 73 28 53 51 4c  lite3_status(SQL
9b710 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52  ITE_STATUS_MEMOR
9b720 59 5f 55 53 45 44 2c 20 26 6e 2c 20 26 6d 78 2c  Y_USED, &n, &mx,
9b730 20 72 65 73 65 74 46 6c 61 67 29 3b 0a 20 20 72   resetFlag);.  r
9b740 65 73 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e  es = (sqlite3_in
9b750 74 36 34 29 6d 78 3b 20 20 2f 2a 20 57 6f 72 6b  t64)mx;  /* Work
9b760 20 61 72 6f 75 6e 64 20 62 75 67 20 69 6e 20 42   around bug in B
9b770 6f 72 6c 61 6e 64 20 43 2e 20 54 69 63 6b 65 74  orland C. Ticket
9b780 20 23 33 32 31 36 20 2a 2f 0a 20 20 72 65 74 75   #3216 */.  retu
9b790 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rn res;.}../*.**
9b7a0 20 43 68 61 6e 67 65 20 74 68 65 20 61 6c 61 72   Change the alar
9b7b0 6d 20 63 61 6c 6c 62 61 63 6b 0a 2a 2f 0a 53 51  m callback.*/.SQ
9b7c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
9b7d0 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c   sqlite3MemoryAl
9b7e0 61 72 6d 28 0a 20 20 76 6f 69 64 28 2a 78 43 61  arm(.  void(*xCa
9b7f0 6c 6c 62 61 63 6b 29 28 76 6f 69 64 20 2a 70 41  llback)(void *pA
9b800 72 67 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  rg, sqlite3_int6
9b810 34 20 75 73 65 64 2c 69 6e 74 20 4e 29 2c 0a 20  4 used,int N),. 
9b820 20 76 6f 69 64 20 2a 70 41 72 67 2c 0a 20 20 73   void *pArg,.  s
9b830 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 54 68  qlite3_int64 iTh
9b840 72 65 73 68 6f 6c 64 0a 29 7b 0a 20 20 73 71 6c  reshold.){.  sql
9b850 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
9b860 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
9b870 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61  mem0.alarmCallba
9b880 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a  ck = xCallback;.
9b890 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 67 20    mem0.alarmArg 
9b8a0 3d 20 70 41 72 67 3b 0a 20 20 6d 65 6d 30 2e 61  = pArg;.  mem0.a
9b8b0 6c 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 3d 20  larmThreshold = 
9b8c0 69 54 68 72 65 73 68 6f 6c 64 3b 0a 20 20 73 71  iThreshold;.  sq
9b8d0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
9b8e0 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20  e(mem0.mutex);. 
9b8f0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
9b900 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  K;.}..#ifndef SQ
9b910 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43  LITE_OMIT_DEPREC
9b920 41 54 45 44 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65  ATED./*.** Depre
9b930 63 61 74 65 64 20 65 78 74 65 72 6e 61 6c 20 69  cated external i
9b940 6e 74 65 72 66 61 63 65 2e 20 20 49 6e 74 65 72  nterface.  Inter
9b950 6e 61 6c 2f 63 6f 72 65 20 53 51 4c 69 74 65 20  nal/core SQLite 
9b960 63 6f 64 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 63  code.** should c
9b970 61 6c 6c 20 73 71 6c 69 74 65 33 4d 65 6d 6f 72  all sqlite3Memor
9b980 79 41 6c 61 72 6d 2e 0a 2a 2f 0a 53 51 4c 49 54  yAlarm..*/.SQLIT
9b990 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
9b9a0 33 5f 6d 65 6d 6f 72 79 5f 61 6c 61 72 6d 28 0a  3_memory_alarm(.
9b9b0 20 20 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61 63    void(*xCallbac
9b9c0 6b 29 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 73  k)(void *pArg, s
9b9d0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 75 73 65  qlite3_int64 use
9b9e0 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69 64  d,int N),.  void
9b9f0 20 2a 70 41 72 67 2c 0a 20 20 73 71 6c 69 74 65   *pArg,.  sqlite
9ba00 33 5f 69 6e 74 36 34 20 69 54 68 72 65 73 68 6f  3_int64 iThresho
9ba10 6c 64 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  ld.){.  return s
9ba20 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72  qlite3MemoryAlar
9ba30 6d 28 78 43 61 6c 6c 62 61 63 6b 2c 20 70 41 72  m(xCallback, pAr
9ba40 67 2c 20 69 54 68 72 65 73 68 6f 6c 64 29 3b 0a  g, iThreshold);.
9ba50 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
9ba60 54 72 69 67 67 65 72 20 74 68 65 20 61 6c 61 72  Trigger the alar
9ba70 6d 20 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  m .*/.static voi
9ba80 64 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41  d sqlite3MallocA
9ba90 6c 61 72 6d 28 69 6e 74 20 6e 42 79 74 65 29 7b  larm(int nByte){
9baa0 0a 20 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62  .  void (*xCallb
9bab0 61 63 6b 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74  ack)(void*,sqlit
9bac0 65 33 5f 69 6e 74 36 34 2c 69 6e 74 29 3b 0a 20  e3_int64,int);. 
9bad0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e   sqlite3_int64 n
9bae0 6f 77 55 73 65 64 3b 0a 20 20 76 6f 69 64 20 2a  owUsed;.  void *
9baf0 70 41 72 67 3b 0a 20 20 69 66 28 20 6d 65 6d 30  pArg;.  if( mem0
9bb00 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 3d 3d  .alarmCallback==
9bb10 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 78 43  0 ) return;.  xC
9bb20 61 6c 6c 62 61 63 6b 20 3d 20 6d 65 6d 30 2e 61  allback = mem0.a
9bb30 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 3b 0a 20 20  larmCallback;.  
9bb40 6e 6f 77 55 73 65 64 20 3d 20 73 71 6c 69 74 65  nowUsed = sqlite
9bb50 33 53 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c  3StatusValue(SQL
9bb60 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52  ITE_STATUS_MEMOR
9bb70 59 5f 55 53 45 44 29 3b 0a 20 20 70 41 72 67 20  Y_USED);.  pArg 
9bb80 3d 20 6d 65 6d 30 2e 61 6c 61 72 6d 41 72 67 3b  = mem0.alarmArg;
9bb90 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c  .  mem0.alarmCal
9bba0 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 73 71 6c  lback = 0;.  sql
9bbb0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
9bbc0 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
9bbd0 78 43 61 6c 6c 62 61 63 6b 28 70 41 72 67 2c 20  xCallback(pArg, 
9bbe0 6e 6f 77 55 73 65 64 2c 20 6e 42 79 74 65 29 3b  nowUsed, nByte);
9bbf0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
9bc00 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65  _enter(mem0.mute
9bc10 78 29 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d  x);.  mem0.alarm
9bc20 43 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c  Callback = xCall
9bc30 62 61 63 6b 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61  back;.  mem0.ala
9bc40 72 6d 41 72 67 20 3d 20 70 41 72 67 3b 0a 7d 0a  rmArg = pArg;.}.
9bc50 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 20 6d 65 6d 6f  ./*.** Do a memo
9bc60 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 69  ry allocation wi
9bc70 74 68 20 73 74 61 74 69 73 74 69 63 73 20 61 6e  th statistics an
9bc80 64 20 61 6c 61 72 6d 73 2e 20 20 41 73 73 75 6d  d alarms.  Assum
9bc90 65 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73  e the.** lock is
9bca0 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2e 0a 2a   already held..*
9bcb0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 61 6c  /.static int mal
9bcc0 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28 69 6e 74  locWithAlarm(int
9bcd0 20 6e 2c 20 76 6f 69 64 20 2a 2a 70 70 29 7b 0a   n, void **pp){.
9bce0 20 20 69 6e 74 20 6e 46 75 6c 6c 3b 0a 20 20 76    int nFull;.  v
9bcf0 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74  oid *p;.  assert
9bd00 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
9bd10 68 65 6c 64 28 6d 65 6d 30 2e 6d 75 74 65 78 29  held(mem0.mutex)
9bd20 20 29 3b 0a 20 20 6e 46 75 6c 6c 20 3d 20 73 71   );.  nFull = sq
9bd30 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9bd40 67 2e 6d 2e 78 52 6f 75 6e 64 75 70 28 6e 29 3b  g.m.xRoundup(n);
9bd50 0a 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73  .  sqlite3Status
9bd60 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Set(SQLITE_STATU
9bd70 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20 6e  S_MALLOC_SIZE, n
9bd80 29 3b 0a 20 20 69 66 28 20 6d 65 6d 30 2e 61 6c  );.  if( mem0.al
9bd90 61 72 6d 43 61 6c 6c 62 61 63 6b 21 3d 30 20 29  armCallback!=0 )
9bda0 7b 0a 20 20 20 20 69 6e 74 20 6e 55 73 65 64 20  {.    int nUsed 
9bdb0 3d 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 56  = sqlite3StatusV
9bdc0 61 6c 75 65 28 53 51 4c 49 54 45 5f 53 54 41 54  alue(SQLITE_STAT
9bdd0 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 29 3b  US_MEMORY_USED);
9bde0 0a 20 20 20 20 69 66 28 20 6e 55 73 65 64 2b 6e  .    if( nUsed+n
9bdf0 46 75 6c 6c 20 3e 3d 20 6d 65 6d 30 2e 61 6c 61  Full >= mem0.ala
9be00 72 6d 54 68 72 65 73 68 6f 6c 64 20 29 7b 0a 20  rmThreshold ){. 
9be10 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c       sqlite3Mall
9be20 6f 63 41 6c 61 72 6d 28 6e 46 75 6c 6c 29 3b 0a  ocAlarm(nFull);.
9be30 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 20 3d 20      }.  }.  p = 
9be40 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9be50 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 46  fig.m.xMalloc(nF
9be60 75 6c 6c 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30  ull);.  if( p==0
9be70 20 26 26 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61   && mem0.alarmCa
9be80 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 73 71  llback ){.    sq
9be90 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d  lite3MallocAlarm
9bea0 28 6e 46 75 6c 6c 29 3b 0a 20 20 20 20 70 20 3d  (nFull);.    p =
9beb0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
9bec0 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e  nfig.m.xMalloc(n
9bed0 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20 69 66 28  Full);.  }.  if(
9bee0 20 70 20 29 7b 0a 20 20 20 20 6e 46 75 6c 6c 20   p ){.    nFull 
9bef0 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53  = sqlite3MallocS
9bf00 69 7a 65 28 70 29 3b 0a 20 20 20 20 73 71 6c 69  ize(p);.    sqli
9bf10 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c  te3StatusAdd(SQL
9bf20 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52  ITE_STATUS_MEMOR
9bf30 59 5f 55 53 45 44 2c 20 6e 46 75 6c 6c 29 3b 0a  Y_USED, nFull);.
9bf40 20 20 7d 0a 20 20 2a 70 70 20 3d 20 70 3b 0a 20    }.  *pp = p;. 
9bf50 20 72 65 74 75 72 6e 20 6e 46 75 6c 6c 3b 0a 7d   return nFull;.}
9bf60 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
9bf70 20 6d 65 6d 6f 72 79 2e 20 20 54 68 69 73 20 72   memory.  This r
9bf80 6f 75 74 69 6e 65 20 69 73 20 6c 69 6b 65 20 73  outine is like s
9bf90 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
9bfa0 65 78 63 65 70 74 20 74 68 61 74 20 69 74 0a 2a  except that it.*
9bfb0 2a 20 61 73 73 75 6d 65 73 20 74 68 65 20 6d 65  * assumes the me
9bfc0 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d 20 68  mory subsystem h
9bfd0 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
9bfe0 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2f 0a  initialized..*/.
9bff0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
9c000 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c  oid *sqlite3Mall
9c010 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69  oc(int n){.  voi
9c020 64 20 2a 70 3b 0a 20 20 69 66 28 20 6e 3c 3d 30  d *p;.  if( n<=0
9c030 20 7c 7c 20 6e 3e 3d 30 78 37 66 66 66 66 66 30   || n>=0x7fffff0
9c040 30 20 29 7b 0a 20 20 20 20 2f 2a 20 41 20 6d 65  0 ){.    /* A me
9c050 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
9c060 6f 66 20 61 20 6e 75 6d 62 65 72 20 6f 66 20 62  of a number of b
9c070 79 74 65 73 20 77 68 69 63 68 20 69 73 20 6e 65  ytes which is ne
9c080 61 72 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 20  ar the maximum. 
9c090 20 20 20 2a 2a 20 73 69 67 6e 65 64 20 69 6e 74     ** signed int
9c0a0 65 67 65 72 20 76 61 6c 75 65 20 6d 69 67 68 74  eger value might
9c0b0 20 63 61 75 73 65 20 61 6e 20 69 6e 74 65 67 65   cause an intege
9c0c0 72 20 6f 76 65 72 66 6c 6f 77 20 69 6e 73 69 64  r overflow insid
9c0d0 65 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20  e of the.    ** 
9c0e0 78 4d 61 6c 6c 6f 63 28 29 2e 20 20 48 65 6e 63  xMalloc().  Henc
9c0f0 65 20 77 65 20 6c 69 6d 69 74 20 74 68 65 20 6d  e we limit the m
9c100 61 78 69 6d 75 6d 20 73 69 7a 65 20 74 6f 20 30  aximum size to 0
9c110 78 37 66 66 66 66 66 30 30 2c 20 67 69 76 69 6e  x7fffff00, givin
9c120 67 0a 20 20 20 20 2a 2a 20 32 35 35 20 62 79 74  g.    ** 255 byt
9c130 65 73 20 6f 66 20 6f 76 65 72 68 65 61 64 2e 20  es of overhead. 
9c140 20 53 51 4c 69 74 65 20 69 74 73 65 6c 66 20 77   SQLite itself w
9c150 69 6c 6c 20 6e 65 76 65 72 20 75 73 65 20 61 6e  ill never use an
9c160 79 74 68 69 6e 67 20 6e 65 61 72 0a 20 20 20 20  ything near.    
9c170 2a 2a 20 74 68 69 73 20 61 6d 6f 75 6e 74 2e 20  ** this amount. 
9c180 20 54 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f   The only way to
9c190 20 72 65 61 63 68 20 74 68 65 20 6c 69 6d 69 74   reach the limit
9c1a0 20 69 73 20 77 69 74 68 20 73 71 6c 69 74 65 33   is with sqlite3
9c1b0 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20 20  _malloc() */.   
9c1c0 20 70 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20   p = 0;.  }else 
9c1d0 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
9c1e0 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74  lConfig.bMemstat
9c1f0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
9c200 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30  mutex_enter(mem0
9c210 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 6d 61 6c  .mutex);.    mal
9c220 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28 6e 2c 20  locWithAlarm(n, 
9c230 26 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  &p);.    sqlite3
9c240 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d  _mutex_leave(mem
9c250 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73  0.mutex);.  }els
9c260 65 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74  e{.    p = sqlit
9c270 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
9c280 2e 78 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d  .xMalloc(n);.  }
9c290 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
9c2a0 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69  /*.** This versi
9c2b0 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79  on of the memory
9c2c0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66   allocation is f
9c2d0 6f 72 20 75 73 65 20 62 79 20 74 68 65 20 61 70  or use by the ap
9c2e0 70 6c 69 63 61 74 69 6f 6e 2e 0a 2a 2a 20 46 69  plication..** Fi
9c2f0 72 73 74 20 6d 61 6b 65 20 73 75 72 65 20 74 68  rst make sure th
9c300 65 20 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74  e memory subsyst
9c310 65 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65  em is initialize
9c320 64 2c 20 74 68 65 6e 20 64 6f 20 74 68 65 0a 2a  d, then do the.*
9c330 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f  * allocation..*/
9c340 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
9c350 20 2a 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   *sqlite3_malloc
9c360 28 69 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 65 66  (int n){.#ifndef
9c370 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
9c380 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69  OINIT.  if( sqli
9c390 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
9c3a0 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e   ) return 0;.#en
9c3b0 64 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  dif.  return sql
9c3c0 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 7d  ite3Malloc(n);.}
9c3d0 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 68 72  ../*.** Each thr
9c3e0 65 61 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76  ead may only hav
9c3f0 65 20 61 20 73 69 6e 67 6c 65 20 6f 75 74 73 74  e a single outst
9c400 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f  anding allocatio
9c410 6e 20 66 72 6f 6d 0a 2a 2a 20 78 53 63 72 61 74  n from.** xScrat
9c420 63 68 4d 61 6c 6c 6f 63 28 29 2e 20 20 57 65 20  chMalloc().  We 
9c430 76 65 72 69 66 79 20 74 68 69 73 20 63 6f 6e 73  verify this cons
9c440 74 72 61 69 6e 74 20 69 6e 20 74 68 65 20 73 69  traint in the si
9c450 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 0a 2a 2a  ngle-threaded.**
9c460 20 63 61 73 65 20 62 79 20 73 65 74 74 69 6e 67   case by setting
9c470 20 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74   scratchAllocOut
9c480 20 74 6f 20 31 20 77 68 65 6e 20 61 6e 20 61 6c   to 1 when an al
9c490 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f  location.** is o
9c4a0 75 74 73 74 61 6e 64 69 6e 67 20 63 6c 65 61 72  utstanding clear
9c4b0 69 6e 67 20 69 74 20 77 68 65 6e 20 74 68 65 20  ing it when the 
9c4c0 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72  allocation is fr
9c4d0 65 65 64 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  eed..*/.#if SQLI
9c4e0 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30  TE_THREADSAFE==0
9c4f0 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45   && !defined(NDE
9c500 42 55 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20  BUG).static int 
9c510 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20  scratchAllocOut 
9c520 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  = 0;.#endif.../*
9c530 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d  .** Allocate mem
9c540 6f 72 79 20 74 68 61 74 20 69 73 20 74 6f 20 62  ory that is to b
9c550 65 20 75 73 65 64 20 61 6e 64 20 72 65 6c 65 61  e used and relea
9c560 73 65 64 20 72 69 67 68 74 20 61 77 61 79 2e 0a  sed right away..
9c570 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
9c580 69 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 61 6c  is similar to al
9c590 6c 6f 63 61 28 29 20 69 6e 20 74 68 61 74 20 69  loca() in that i
9c5a0 74 20 69 73 20 6e 6f 74 20 69 6e 74 65 6e 64 65  t is not intende
9c5b0 64 0a 2a 2a 20 66 6f 72 20 73 69 74 75 61 74 69  d.** for situati
9c5c0 6f 6e 73 20 77 68 65 72 65 20 74 68 65 20 6d 65  ons where the me
9c5d0 6d 6f 72 79 20 6d 69 67 68 74 20 62 65 20 68 65  mory might be he
9c5e0 6c 64 20 6c 6f 6e 67 2d 74 65 72 6d 2e 20 20 54  ld long-term.  T
9c5f0 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69  his.** routine i
9c600 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 67 65  s intended to ge
9c610 74 20 6d 65 6d 6f 72 79 20 74 6f 20 6f 6c 64 20  t memory to old 
9c620 6c 61 72 67 65 20 74 72 61 6e 73 69 65 6e 74 20  large transient 
9c630 64 61 74 61 0a 2a 2a 20 73 74 72 75 63 74 75 72  data.** structur
9c640 65 73 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 6f  es that would no
9c650 74 20 6e 6f 72 6d 61 6c 6c 79 20 66 69 74 20 6f  t normally fit o
9c660 6e 20 74 68 65 20 73 74 61 63 6b 20 6f 66 20 61  n the stack of a
9c670 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64 20 70 72  n.** embedded pr
9c680 6f 63 65 73 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49  ocessor..*/.SQLI
9c690 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
9c6a0 2a 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d  *sqlite3ScratchM
9c6b0 61 6c 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20  alloc(int n){.  
9c6c0 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72  void *p;.  asser
9c6d0 74 28 20 6e 3e 30 20 29 3b 0a 0a 23 69 66 20 53  t( n>0 );..#if S
9c6e0 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
9c6f0 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 28  ==0 && !defined(
9c700 4e 44 45 42 55 47 29 0a 20 20 2f 2a 20 56 65 72  NDEBUG).  /* Ver
9c710 69 66 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65  ify that no more
9c720 20 74 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63   than one scratc
9c730 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72  h allocation per
9c740 20 74 68 72 65 61 64 0a 20 20 2a 2a 20 69 73 20   thread.  ** is 
9c750 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f  outstanding at o
9c760 6e 65 20 74 69 6d 65 2e 20 20 28 54 68 69 73 20  ne time.  (This 
9c770 69 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20  is only checked 
9c780 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 69 6e 67  in the.  ** sing
9c790 6c 65 2d 74 68 72 65 61 64 65 64 20 63 61 73 65  le-threaded case
9c7a0 20 73 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20   since checking 
9c7b0 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72  in the multi-thr
9c7c0 65 61 64 65 64 20 63 61 73 65 0a 20 20 2a 2a 20  eaded case.  ** 
9c7d0 77 6f 75 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f  would be much mo
9c7e0 72 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29  re complicated.)
9c7f0 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 73 63   */.  assert( sc
9c800 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 3d 3d 30  ratchAllocOut==0
9c810 20 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66   );.#endif..  if
9c820 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
9c830 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3c  onfig.szScratch<
9c840 6e 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 63  n ){.    goto sc
9c850 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a  ratch_overflow;.
9c860 20 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 73    }else{  .    s
9c870 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
9c880 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a  er(mem0.mutex);.
9c890 20 20 20 20 69 66 28 20 6d 65 6d 30 2e 6e 53 63      if( mem0.nSc
9c8a0 72 61 74 63 68 46 72 65 65 3d 3d 30 20 29 7b 0a  ratchFree==0 ){.
9c8b0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
9c8c0 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d  tex_leave(mem0.m
9c8d0 75 74 65 78 29 3b 0a 20 20 20 20 20 20 67 6f 74  utex);.      got
9c8e0 6f 20 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c  o scratch_overfl
9c8f0 6f 77 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ow;.    }else{. 
9c900 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
9c910 20 20 69 20 3d 20 6d 65 6d 30 2e 61 53 63 72 61    i = mem0.aScra
9c920 74 63 68 46 72 65 65 5b 2d 2d 6d 65 6d 30 2e 6e  tchFree[--mem0.n
9c930 53 63 72 61 74 63 68 46 72 65 65 5d 3b 0a 20 20  ScratchFree];.  
9c940 20 20 20 20 69 20 2a 3d 20 73 71 6c 69 74 65 33      i *= sqlite3
9c950 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53  GlobalConfig.szS
9c960 63 72 61 74 63 68 3b 0a 20 20 20 20 20 20 73 71  cratch;.      sq
9c970 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53  lite3StatusAdd(S
9c980 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52  QLITE_STATUS_SCR
9c990 41 54 43 48 5f 55 53 45 44 2c 20 31 29 3b 0a 20  ATCH_USED, 1);. 
9c9a0 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74       sqlite3Stat
9c9b0 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41  usSet(SQLITE_STA
9c9c0 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45  TUS_SCRATCH_SIZE
9c9d0 2c 20 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , n);.      sqli
9c9e0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
9c9f0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20  mem0.mutex);.   
9ca00 20 20 20 70 20 3d 20 28 76 6f 69 64 2a 29 26 28     p = (void*)&(
9ca10 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c  (char*)sqlite3Gl
9ca20 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61  obalConfig.pScra
9ca30 74 63 68 29 5b 69 5d 3b 0a 20 20 20 20 20 20 61  tch)[i];.      a
9ca40 73 73 65 72 74 28 20 20 28 28 28 75 38 2a 29 70  ssert(  (((u8*)p
9ca50 20 2d 20 28 75 38 2a 29 30 29 20 26 20 37 29 3d   - (u8*)0) & 7)=
9ca60 3d 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  =0 );.    }.  }.
9ca70 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
9ca80 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65 66  DSAFE==0 && !def
9ca90 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 20 73  ined(NDEBUG).  s
9caa0 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d  cratchAllocOut =
9cab0 20 70 21 3d 30 3b 0a 23 65 6e 64 69 66 0a 0a 20   p!=0;.#endif.. 
9cac0 20 72 65 74 75 72 6e 20 70 3b 0a 0a 73 63 72 61   return p;..scra
9cad0 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3a 0a 20 20  tch_overflow:.  
9cae0 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
9caf0 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74  lConfig.bMemstat
9cb00 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
9cb10 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30  mutex_enter(mem0
9cb20 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c  .mutex);.    sql
9cb30 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51  ite3StatusSet(SQ
9cb40 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41  LITE_STATUS_SCRA
9cb50 54 43 48 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20  TCH_SIZE, n);.  
9cb60 20 20 6e 20 3d 20 6d 61 6c 6c 6f 63 57 69 74 68    n = mallocWith
9cb70 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b 0a 20 20  Alarm(n, &p);.  
9cb80 20 20 69 66 28 20 70 20 29 20 73 71 6c 69 74 65    if( p ) sqlite
9cb90 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54  3StatusAdd(SQLIT
9cba0 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43 48  E_STATUS_SCRATCH
9cbb0 5f 4f 56 45 52 46 4c 4f 57 2c 20 6e 29 3b 0a 20  _OVERFLOW, n);. 
9cbc0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
9cbd0 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65  _leave(mem0.mute
9cbe0 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  x);.  }else{.   
9cbf0 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62   p = sqlite3Glob
9cc00 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c  alConfig.m.xMall
9cc10 6f 63 28 6e 29 3b 0a 20 20 7d 0a 23 69 66 20 53  oc(n);.  }.#if S
9cc20 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
9cc30 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64 28  ==0 && !defined(
9cc40 4e 44 45 42 55 47 29 0a 20 20 73 63 72 61 74 63  NDEBUG).  scratc
9cc50 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d 30  hAllocOut = p!=0
9cc60 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72  ;.#endif.  retur
9cc70 6e 20 70 3b 20 20 20 20 0a 7d 0a 53 51 4c 49 54  n p;    .}.SQLIT
9cc80 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
9cc90 71 6c 69 74 65 33 53 63 72 61 74 63 68 46 72 65  qlite3ScratchFre
9cca0 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69 66  e(void *p){.  if
9ccb0 28 20 70 20 29 7b 0a 0a 23 69 66 20 53 51 4c 49  ( p ){..#if SQLI
9ccc0 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30  TE_THREADSAFE==0
9ccd0 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45   && !defined(NDE
9cce0 42 55 47 29 0a 20 20 20 20 2f 2a 20 56 65 72 69  BUG).    /* Veri
9ccf0 66 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20  fy that no more 
9cd00 74 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 68  than one scratch
9cd10 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20   allocation per 
9cd20 74 68 72 65 61 64 0a 20 20 20 20 2a 2a 20 69 73  thread.    ** is
9cd30 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74 20   outstanding at 
9cd40 6f 6e 65 20 74 69 6d 65 2e 20 20 28 54 68 69 73  one time.  (This
9cd50 20 69 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64   is only checked
9cd60 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 73   in the.    ** s
9cd70 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 63  ingle-threaded c
9cd80 61 73 65 20 73 69 6e 63 65 20 63 68 65 63 6b 69  ase since checki
9cd90 6e 67 20 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d  ng in the multi-
9cda0 74 68 72 65 61 64 65 64 20 63 61 73 65 0a 20 20  threaded case.  
9cdb0 20 20 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d 75    ** would be mu
9cdc0 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63 61  ch more complica
9cdd0 74 65 64 2e 29 20 2a 2f 0a 20 20 20 20 61 73 73  ted.) */.    ass
9cde0 65 72 74 28 20 73 63 72 61 74 63 68 41 6c 6c 6f  ert( scratchAllo
9cdf0 63 4f 75 74 3d 3d 31 20 29 3b 0a 20 20 20 20 73  cOut==1 );.    s
9ce00 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d  cratchAllocOut =
9ce10 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20   0;.#endif..    
9ce20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
9ce30 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68  lConfig.pScratch
9ce40 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 7c  ==0.           |
9ce50 7c 20 70 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61  | p<sqlite3Globa
9ce60 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68  lConfig.pScratch
9ce70 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70  .           || p
9ce80 3e 3d 28 76 6f 69 64 2a 29 6d 65 6d 30 2e 61 53  >=(void*)mem0.aS
9ce90 63 72 61 74 63 68 46 72 65 65 20 29 7b 0a 20 20  cratchFree ){.  
9cea0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47      if( sqlite3G
9ceb0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d  lobalConfig.bMem
9cec0 73 74 61 74 20 29 7b 0a 20 20 20 20 20 20 20 20  stat ){.        
9ced0 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69  int iSize = sqli
9cee0 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29  te3MallocSize(p)
9cef0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
9cf00 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65  3_mutex_enter(me
9cf10 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  m0.mutex);.     
9cf20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73     sqlite3Status
9cf30 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Add(SQLITE_STATU
9cf40 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c  S_SCRATCH_OVERFL
9cf50 4f 57 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20  OW, -iSize);.   
9cf60 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74       sqlite3Stat
9cf70 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41  usAdd(SQLITE_STA
9cf80 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c  TUS_MEMORY_USED,
9cf90 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 20 20   -iSize);.      
9cfa0 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
9cfb0 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29  onfig.m.xFree(p)
9cfc0 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
9cfd0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
9cfe0 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  m0.mutex);.     
9cff0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
9d000 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9d010 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a  fig.m.xFree(p);.
9d020 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
9d030 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a  e{.      int i;.
9d040 20 20 20 20 20 20 69 20 3d 20 28 69 6e 74 29 28        i = (int)(
9d050 28 75 38 2a 29 70 20 2d 20 28 75 38 2a 29 73 71  (u8*)p - (u8*)sq
9d060 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9d070 67 2e 70 53 63 72 61 74 63 68 29 3b 0a 20 20 20  g.pScratch);.   
9d080 20 20 20 69 20 2f 3d 20 73 71 6c 69 74 65 33 47     i /= sqlite3G
9d090 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63  lobalConfig.szSc
9d0a0 72 61 74 63 68 3b 0a 20 20 20 20 20 20 61 73 73  ratch;.      ass
9d0b0 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 73  ert( i>=0 && i<s
9d0c0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9d0d0 69 67 2e 6e 53 63 72 61 74 63 68 20 29 3b 0a 20  ig.nScratch );. 
9d0e0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
9d0f0 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75  ex_enter(mem0.mu
9d100 74 65 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65  tex);.      asse
9d110 72 74 28 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63  rt( mem0.nScratc
9d120 68 46 72 65 65 3c 28 75 33 32 29 73 71 6c 69 74  hFree<(u32)sqlit
9d130 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
9d140 53 63 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20  Scratch );.     
9d150 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72   mem0.aScratchFr
9d160 65 65 5b 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68  ee[mem0.nScratch
9d170 46 72 65 65 2b 2b 5d 20 3d 20 69 3b 0a 20 20 20  Free++] = i;.   
9d180 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73     sqlite3Status
9d190 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Add(SQLITE_STATU
9d1a0 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44 2c 20  S_SCRATCH_USED, 
9d1b0 2d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  -1);.      sqlit
9d1c0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
9d1d0 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  em0.mutex);.    
9d1e0 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  }.  }.}../*.** T
9d1f0 52 55 45 20 69 66 20 70 20 69 73 20 61 20 6c 6f  RUE if p is a lo
9d200 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 61  okaside memory a
9d210 6c 6c 6f 63 61 74 69 6f 6e 20 66 72 6f 6d 20 64  llocation from d
9d220 62 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  b.*/.#ifndef SQL
9d230 49 54 45 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49  ITE_OMIT_LOOKASI
9d240 44 45 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73  DE.static int is
9d250 4c 6f 6f 6b 61 73 69 64 65 28 73 71 6c 69 74 65  Lookaside(sqlite
9d260 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 29 7b  3 *db, void *p){
9d270 0a 20 20 72 65 74 75 72 6e 20 64 62 20 26 26 20  .  return db && 
9d280 70 20 26 26 20 70 3e 3d 64 62 2d 3e 6c 6f 6f 6b  p && p>=db->look
9d290 61 73 69 64 65 2e 70 53 74 61 72 74 20 26 26 20  aside.pStart && 
9d2a0 70 3c 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  p<db->lookaside.
9d2b0 70 45 6e 64 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64  pEnd;.}.#else.#d
9d2c0 65 66 69 6e 65 20 69 73 4c 6f 6f 6b 61 73 69 64  efine isLookasid
9d2d0 65 28 41 2c 42 29 20 30 0a 23 65 6e 64 69 66 0a  e(A,B) 0.#endif.
9d2e0 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
9d2f0 65 20 73 69 7a 65 20 6f 66 20 61 20 6d 65 6d 6f  e size of a memo
9d300 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72  ry allocation pr
9d310 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65  eviously obtaine
9d320 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65  d from.** sqlite
9d330 33 4d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c  3Malloc() or sql
9d340 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a  ite3_malloc()..*
9d350 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
9d360 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c   int sqlite3Mall
9d370 6f 63 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b  ocSize(void *p){
9d380 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
9d390 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
9d3a0 78 53 69 7a 65 28 70 29 3b 0a 7d 0a 53 51 4c 49  xSize(p);.}.SQLI
9d3b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
9d3c0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69  qlite3DbMallocSi
9d3d0 7a 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ze(sqlite3 *db, 
9d3e0 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65  void *p){.  asse
9d3f0 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  rt( db==0 || sql
9d400 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
9d410 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
9d420 69 66 28 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28  if( isLookaside(
9d430 64 62 2c 20 70 29 20 29 7b 0a 20 20 20 20 72 65  db, p) ){.    re
9d440 74 75 72 6e 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  turn db->lookasi
9d450 64 65 2e 73 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a  de.sz;.  }else{.
9d460 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
9d470 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
9d480 2e 78 53 69 7a 65 28 70 29 3b 0a 20 20 7d 0a 7d  .xSize(p);.  }.}
9d490 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d  ../*.** Free mem
9d4a0 6f 72 79 20 70 72 65 76 69 6f 75 73 6c 79 20 6f  ory previously o
9d4b0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c  btained from sql
9d4c0 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f  ite3Malloc()..*/
9d4d0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
9d4e0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76 6f   sqlite3_free(vo
9d4f0 69 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 3d  id *p){.  if( p=
9d500 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  =0 ) return;.  i
9d510 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
9d520 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20  Config.bMemstat 
9d530 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  ){.    sqlite3_m
9d540 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e  utex_enter(mem0.
9d550 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69  mutex);.    sqli
9d560 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c  te3StatusAdd(SQL
9d570 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52  ITE_STATUS_MEMOR
9d580 59 5f 55 53 45 44 2c 20 2d 73 71 6c 69 74 65 33  Y_USED, -sqlite3
9d590 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 29 3b 0a  MallocSize(p));.
9d5a0 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
9d5b0 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28  lConfig.m.xFree(
9d5c0 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  p);.    sqlite3_
9d5d0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30  mutex_leave(mem0
9d5e0 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65  .mutex);.  }else
9d5f0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  {.    sqlite3Glo
9d600 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65  balConfig.m.xFre
9d610 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  e(p);.  }.}../*.
9d620 2a 2a 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 74  ** Free memory t
9d630 68 61 74 20 6d 69 67 68 74 20 62 65 20 61 73 73  hat might be ass
9d640 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 70  ociated with a p
9d650 61 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61  articular databa
9d660 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  se.** connection
9d670 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
9d680 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
9d690 44 62 46 72 65 65 28 73 71 6c 69 74 65 33 20 2a  DbFree(sqlite3 *
9d6a0 64 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20  db, void *p){.  
9d6b0 61 73 73 65 72 74 28 20 64 62 3d 3d 30 20 7c 7c  assert( db==0 ||
9d6c0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
9d6d0 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
9d6e0 3b 0a 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73  ;.  if( isLookas
9d6f0 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20  ide(db, p) ){.  
9d700 20 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20    LookasideSlot 
9d710 2a 70 42 75 66 20 3d 20 28 4c 6f 6f 6b 61 73 69  *pBuf = (Lookasi
9d720 64 65 53 6c 6f 74 2a 29 70 3b 0a 20 20 20 20 70  deSlot*)p;.    p
9d730 42 75 66 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d  Buf->pNext = db-
9d740 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65  >lookaside.pFree
9d750 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73  ;.    db->lookas
9d760 69 64 65 2e 70 46 72 65 65 20 3d 20 70 42 75 66  ide.pFree = pBuf
9d770 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73  ;.    db->lookas
9d780 69 64 65 2e 6e 4f 75 74 2d 2d 3b 0a 20 20 7d 65  ide.nOut--;.  }e
9d790 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
9d7a0 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a  _free(p);.  }.}.
9d7b0 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
9d7c0 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69  e size of an exi
9d7d0 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c  sting memory all
9d7e0 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54  ocation.*/.SQLIT
9d7f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
9d800 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 28 76  sqlite3Realloc(v
9d810 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e  oid *pOld, int n
9d820 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20 6e 4f  Bytes){.  int nO
9d830 6c 64 2c 20 6e 4e 65 77 3b 0a 20 20 76 6f 69 64  ld, nNew;.  void
9d840 20 2a 70 4e 65 77 3b 0a 20 20 69 66 28 20 70 4f   *pNew;.  if( pO
9d850 6c 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ld==0 ){.    ret
9d860 75 72 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  urn sqlite3Mallo
9d870 63 28 6e 42 79 74 65 73 29 3b 0a 20 20 7d 0a 20  c(nBytes);.  }. 
9d880 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 30 20 29   if( nBytes<=0 )
9d890 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
9d8a0 65 65 28 70 4f 6c 64 29 3b 0a 20 20 20 20 72 65  ee(pOld);.    re
9d8b0 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66  turn 0;.  }.  if
9d8c0 28 20 6e 42 79 74 65 73 3e 3d 30 78 37 66 66 66  ( nBytes>=0x7fff
9d8d0 66 66 30 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54  ff00 ){.    /* T
9d8e0 68 65 20 30 78 37 66 66 66 66 30 30 20 6c 69 6d  he 0x7ffff00 lim
9d8f0 69 74 20 74 65 72 6d 20 69 73 20 65 78 70 6c 61  it term is expla
9d900 69 6e 65 64 20 69 6e 20 63 6f 6d 6d 65 6e 74 73  ined in comments
9d910 20 6f 6e 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f   on sqlite3Mallo
9d920 63 28 29 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  c() */.    retur
9d930 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64 20  n 0;.  }.  nOld 
9d940 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53  = sqlite3MallocS
9d950 69 7a 65 28 70 4f 6c 64 29 3b 0a 20 20 6e 4e 65  ize(pOld);.  nNe
9d960 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  w = sqlite3Globa
9d970 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e 64  lConfig.m.xRound
9d980 75 70 28 6e 42 79 74 65 73 29 3b 0a 20 20 69 66  up(nBytes);.  if
9d990 28 20 6e 4f 6c 64 3d 3d 6e 4e 65 77 20 29 7b 0a  ( nOld==nNew ){.
9d9a0 20 20 20 20 70 4e 65 77 20 3d 20 70 4f 6c 64 3b      pNew = pOld;
9d9b0 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c  .  }else if( sql
9d9c0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9d9d0 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20  .bMemstat ){.   
9d9e0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
9d9f0 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29  nter(mem0.mutex)
9da00 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61  ;.    sqlite3Sta
9da10 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f 53 54  tusSet(SQLITE_ST
9da20 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45  ATUS_MALLOC_SIZE
9da30 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 69  , nBytes);.    i
9da40 66 28 20 73 71 6c 69 74 65 33 53 74 61 74 75 73  f( sqlite3Status
9da50 56 61 6c 75 65 28 53 51 4c 49 54 45 5f 53 54 41  Value(SQLITE_STA
9da60 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 29  TUS_MEMORY_USED)
9da70 2b 6e 4e 65 77 2d 6e 4f 6c 64 20 3e 3d 20 0a 20  +nNew-nOld >= . 
9da80 20 20 20 20 20 20 20 20 20 6d 65 6d 30 2e 61 6c           mem0.al
9da90 61 72 6d 54 68 72 65 73 68 6f 6c 64 20 29 7b 0a  armThreshold ){.
9daa0 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c        sqlite3Mal
9dab0 6c 6f 63 41 6c 61 72 6d 28 6e 4e 65 77 2d 6e 4f  locAlarm(nNew-nO
9dac0 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ld);.    }.    p
9dad0 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f  New = sqlite3Glo
9dae0 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61  balConfig.m.xRea
9daf0 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29  lloc(pOld, nNew)
9db00 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d  ;.    if( pNew==
9db10 30 20 26 26 20 6d 65 6d 30 2e 61 6c 61 72 6d 43  0 && mem0.alarmC
9db20 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20  allback ){.     
9db30 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c   sqlite3MallocAl
9db40 61 72 6d 28 6e 42 79 74 65 73 29 3b 0a 20 20 20  arm(nBytes);.   
9db50 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65     pNew = sqlite
9db60 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
9db70 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e  xRealloc(pOld, n
9db80 4e 65 77 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  New);.    }.    
9db90 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20  if( pNew ){.    
9dba0 20 20 6e 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    nNew = sqlite3
9dbb0 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4e 65 77 29  MallocSize(pNew)
9dbc0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  ;.      sqlite3S
9dbd0 74 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f  tatusAdd(SQLITE_
9dbe0 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53  STATUS_MEMORY_US
9dbf0 45 44 2c 20 6e 4e 65 77 2d 6e 4f 6c 64 29 3b 0a  ED, nNew-nOld);.
9dc00 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
9dc10 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
9dc20 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c  m0.mutex);.  }el
9dc30 73 65 7b 0a 20 20 20 20 70 4e 65 77 20 3d 20 73  se{.    pNew = s
9dc40 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9dc50 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f  ig.m.xRealloc(pO
9dc60 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20 20 7d 0a 20  ld, nNew);.  }. 
9dc70 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
9dc80 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 75 62 6c 69  ./*.** The publi
9dc90 63 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 73  c interface to s
9dca0 71 6c 69 74 65 33 52 65 61 6c 6c 6f 63 2e 20 20  qlite3Realloc.  
9dcb0 4d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74  Make sure that t
9dcc0 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 75 62  he memory.** sub
9dcd0 73 79 73 74 65 6d 20 69 73 20 69 6e 69 74 69 61  system is initia
9dce0 6c 69 7a 65 64 20 70 72 69 6f 72 20 74 6f 20 69  lized prior to i
9dcf0 6e 76 6f 6b 69 6e 67 20 73 71 6c 69 74 65 52 65  nvoking sqliteRe
9dd00 61 6c 6c 6f 63 2e 0a 2a 2f 0a 53 51 4c 49 54 45  alloc..*/.SQLITE
9dd10 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
9dd20 65 33 5f 72 65 61 6c 6c 6f 63 28 76 6f 69 64 20  e3_realloc(void 
9dd30 2a 70 4f 6c 64 2c 20 69 6e 74 20 6e 29 7b 0a 23  *pOld, int n){.#
9dd40 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
9dd50 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66  IT_AUTOINIT.  if
9dd60 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  ( sqlite3_initia
9dd70 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20  lize() ) return 
9dd80 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75  0;.#endif.  retu
9dd90 72 6e 20 73 71 6c 69 74 65 33 52 65 61 6c 6c 6f  rn sqlite3Reallo
9dda0 63 28 70 4f 6c 64 2c 20 6e 29 3b 0a 7d 0a 0a 0a  c(pOld, n);.}...
9ddb0 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  /*.** Allocate a
9ddc0 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79 2e 0a  nd zero memory..
9ddd0 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  */ .SQLITE_PRIVA
9dde0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
9ddf0 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e 74 20 6e  MallocZero(int n
9de00 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d 20 73  ){.  void *p = s
9de10 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b  qlite3Malloc(n);
9de20 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
9de30 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29 3b  memset(p, 0, n);
9de40 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
9de50 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
9de60 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f  te and zero memo
9de70 72 79 2e 20 20 49 66 20 74 68 65 20 61 6c 6c 6f  ry.  If the allo
9de80 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20 6d 61  cation fails, ma
9de90 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63  ke.** the malloc
9dea0 46 61 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74  Failed flag in t
9deb0 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f  he connection po
9dec0 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  inter..*/.SQLITE
9ded0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
9dee0 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
9def0 72 6f 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ro(sqlite3 *db, 
9df00 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a  int n){.  void *
9df10 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  p = sqlite3DbMal
9df20 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20  locRaw(db, n);. 
9df30 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 6d 65   if( p ){.    me
9df40 6d 73 65 74 28 70 2c 20 30 2c 20 6e 29 3b 0a 20  mset(p, 0, n);. 
9df50 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d   }.  return p;.}
9df60 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
9df70 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d 6f 72 79   and zero memory
9df80 2e 20 20 49 66 20 74 68 65 20 61 6c 6c 6f 63 61  .  If the alloca
9df90 74 69 6f 6e 20 66 61 69 6c 73 2c 20 6d 61 6b 65  tion fails, make
9dfa0 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61  .** the mallocFa
9dfb0 69 6c 65 64 20 66 6c 61 67 20 69 6e 20 74 68 65  iled flag in the
9dfc0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 6f 69 6e   connection poin
9dfd0 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 62  ter..**.** If db
9dfe0 21 3d 30 20 61 6e 64 20 64 62 2d 3e 6d 61 6c 6c  !=0 and db->mall
9dff0 6f 63 46 61 69 6c 65 64 20 69 73 20 74 72 75 65  ocFailed is true
9e000 20 28 69 6e 64 69 63 61 74 69 6e 67 20 61 20 70   (indicating a p
9e010 72 69 6f 72 20 6d 61 6c 6c 6f 63 0a 2a 2a 20 66  rior malloc.** f
9e020 61 69 6c 75 72 65 20 6f 6e 20 74 68 65 20 73 61  ailure on the sa
9e030 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  me database conn
9e040 65 63 74 69 6f 6e 29 20 74 68 65 6e 20 61 6c 77  ection) then alw
9e050 61 79 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2a  ays return 0..**
9e060 20 48 65 6e 63 65 20 66 6f 72 20 61 20 70 61 72   Hence for a par
9e070 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65  ticular database
9e080 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 6f 6e 63   connection, onc
9e090 65 20 6d 61 6c 6c 6f 63 20 73 74 61 72 74 73 0a  e malloc starts.
9e0a0 2a 2a 20 66 61 69 6c 69 6e 67 2c 20 69 74 20 66  ** failing, it f
9e0b0 61 69 6c 73 20 63 6f 6e 73 69 73 74 65 6e 74 6c  ails consistentl
9e0c0 79 20 75 6e 74 69 6c 20 6d 61 6c 6c 6f 63 46 61  y until mallocFa
9e0d0 69 6c 65 64 20 69 73 20 72 65 73 65 74 2e 0a 2a  iled is reset..*
9e0e0 2a 20 54 68 69 73 20 69 73 20 61 6e 20 69 6d 70  * This is an imp
9e0f0 6f 72 74 61 6e 74 20 61 73 73 75 6d 70 74 69 6f  ortant assumptio
9e100 6e 2e 20 20 54 68 65 72 65 20 61 72 65 20 6d 61  n.  There are ma
9e110 6e 79 20 70 6c 61 63 65 73 20 69 6e 20 74 68 65  ny places in the
9e120 0a 2a 2a 20 63 6f 64 65 20 74 68 61 74 20 64 6f  .** code that do
9e130 20 74 68 69 6e 67 73 20 6c 69 6b 65 20 74 68 69   things like thi
9e140 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  s:.**.**        
9e150 20 69 6e 74 20 2a 61 20 3d 20 28 69 6e 74 2a 29   int *a = (int*)
9e160 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
9e170 61 77 28 64 62 2c 20 31 30 30 29 3b 0a 2a 2a 20  aw(db, 100);.** 
9e180 20 20 20 20 20 20 20 20 69 6e 74 20 2a 62 20 3d          int *b =
9e190 20 28 69 6e 74 2a 29 73 71 6c 69 74 65 33 44 62   (int*)sqlite3Db
9e1a0 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 32 30  MallocRaw(db, 20
9e1b0 30 29 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 69  0);.**         i
9e1c0 66 28 20 62 20 29 20 61 5b 31 30 5d 20 3d 20 39  f( b ) a[10] = 9
9e1d0 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72  ;.**.** In other
9e1e0 20 77 6f 72 64 73 2c 20 69 66 20 61 20 73 75 62   words, if a sub
9e1f0 73 65 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 28  sequent malloc (
9e200 65 78 3a 20 22 62 22 29 20 77 6f 72 6b 65 64 2c  ex: "b") worked,
9e210 20 69 74 20 69 73 20 61 73 73 75 6d 65 64 0a 2a   it is assumed.*
9e220 2a 20 74 68 61 74 20 61 6c 6c 20 70 72 69 6f 72  * that all prior
9e230 20 6d 61 6c 6c 6f 63 73 20 28 65 78 3a 20 22 61   mallocs (ex: "a
9e240 22 29 20 77 6f 72 6b 65 64 20 74 6f 6f 2e 0a 2a  ") worked too..*
9e250 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
9e260 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62   void *sqlite3Db
9e270 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c 69 74 65  MallocRaw(sqlite
9e280 33 20 2a 64 62 2c 20 69 6e 74 20 6e 29 7b 0a 20  3 *db, int n){. 
9e290 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73 65   void *p;.  asse
9e2a0 72 74 28 20 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  rt( db==0 || sql
9e2b0 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
9e2c0 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 23 69  db->mutex) );.#i
9e2d0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
9e2e0 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a 20 20 69 66  T_LOOKASIDE.  if
9e2f0 28 20 64 62 20 29 7b 0a 20 20 20 20 4c 6f 6f 6b  ( db ){.    Look
9e300 61 73 69 64 65 53 6c 6f 74 20 2a 70 42 75 66 3b  asideSlot *pBuf;
9e310 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  .    if( db->mal
9e320 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
9e330 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
9e340 20 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6c   }.    if( db->l
9e350 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65  ookaside.bEnable
9e360 64 20 26 26 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f 6b  d && n<=db->look
9e370 61 73 69 64 65 2e 73 7a 0a 20 20 20 20 20 20 20  aside.sz.       
9e380 20 20 26 26 20 28 70 42 75 66 20 3d 20 64 62 2d    && (pBuf = db-
9e390 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65  >lookaside.pFree
9e3a0 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 62  )!=0 ){.      db
9e3b0 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65  ->lookaside.pFre
9e3c0 65 20 3d 20 70 42 75 66 2d 3e 70 4e 65 78 74 3b  e = pBuf->pNext;
9e3d0 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61  .      db->looka
9e3e0 73 69 64 65 2e 6e 4f 75 74 2b 2b 3b 0a 20 20 20  side.nOut++;.   
9e3f0 20 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61     if( db->looka
9e400 73 69 64 65 2e 6e 4f 75 74 3e 64 62 2d 3e 6c 6f  side.nOut>db->lo
9e410 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20 29 7b  okaside.mxOut ){
9e420 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f  .        db->loo
9e430 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20 3d 20 64  kaside.mxOut = d
9e440 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75  b->lookaside.nOu
9e450 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  t;.      }.     
9e460 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 70   return (void*)p
9e470 42 75 66 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  Buf;.    }.  }.#
9e480 65 6c 73 65 0a 20 20 69 66 28 20 64 62 20 26 26  else.  if( db &&
9e490 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
9e4a0 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  d ){.    return 
9e4b0 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  0;.  }.#endif.  
9e4c0 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  p = sqlite3Mallo
9e4d0 63 28 6e 29 3b 0a 20 20 69 66 28 20 21 70 20 26  c(n);.  if( !p &
9e4e0 26 20 64 62 20 29 7b 0a 20 20 20 20 64 62 2d 3e  & db ){.    db->
9e4f0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
9e500 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
9e510 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 69 7a  ;.}../*.** Resiz
9e520 65 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20 6d  e the block of m
9e530 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 6f  emory pointed to
9e540 20 62 79 20 70 20 74 6f 20 6e 20 62 79 74 65 73   by p to n bytes
9e550 2e 20 49 66 20 74 68 65 0a 2a 2a 20 72 65 73 69  . If the.** resi
9e560 7a 65 20 66 61 69 6c 73 2c 20 73 65 74 20 74 68  ze fails, set th
9e570 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66  e mallocFailed f
9e580 6c 61 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65  lag in the conne
9e590 63 74 69 6f 6e 20 6f 62 6a 65 63 74 2e 0a 2a 2f  ction object..*/
9e5a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
9e5b0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52  void *sqlite3DbR
9e5c0 65 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 20 2a  ealloc(sqlite3 *
9e5d0 64 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69 6e 74  db, void *p, int
9e5e0 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4e 65   n){.  void *pNe
9e5f0 77 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28  w = 0;.  assert(
9e600 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65   db!=0 );.  asse
9e610 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
9e620 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
9e630 29 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d  ) );.  if( db->m
9e640 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20 29  allocFailed==0 )
9e650 7b 0a 20 20 20 20 69 66 28 20 70 3d 3d 30 20 29  {.    if( p==0 )
9e660 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 73  {.      return s
9e670 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
9e680 77 28 64 62 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a  w(db, n);.    }.
9e690 20 20 20 20 69 66 28 20 69 73 4c 6f 6f 6b 61 73      if( isLookas
9e6a0 69 64 65 28 64 62 2c 20 70 29 20 29 7b 0a 20 20  ide(db, p) ){.  
9e6b0 20 20 20 20 69 66 28 20 6e 3c 3d 64 62 2d 3e 6c      if( n<=db->l
9e6c0 6f 6f 6b 61 73 69 64 65 2e 73 7a 20 29 7b 0a 20  ookaside.sz ){. 
9e6d0 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 3b         return p;
9e6e0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
9e6f0 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  New = sqlite3DbM
9e700 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29 3b  allocRaw(db, n);
9e710 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20  .      if( pNew 
9e720 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  ){.        memcp
9e730 79 28 70 4e 65 77 2c 20 70 2c 20 64 62 2d 3e 6c  y(pNew, p, db->l
9e740 6f 6f 6b 61 73 69 64 65 2e 73 7a 29 3b 0a 20 20  ookaside.sz);.  
9e750 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
9e760 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20  ree(db, p);.    
9e770 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
9e780 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69       pNew = sqli
9e790 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 2c 20 6e  te3_realloc(p, n
9e7a0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e  );.      if( !pN
9e7b0 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62  ew ){.        db
9e7c0 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
9e7d0 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
9e7e0 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  }.  }.  return p
9e7f0 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74  New;.}../*.** At
9e800 74 65 6d 70 74 20 74 6f 20 72 65 61 6c 6c 6f 63  tempt to realloc
9e810 61 74 65 20 70 2e 20 20 49 66 20 74 68 65 20 72  ate p.  If the r
9e820 65 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c  eallocation fail
9e830 73 2c 20 74 68 65 6e 20 66 72 65 65 20 70 0a 2a  s, then free p.*
9e840 2a 20 61 6e 64 20 73 65 74 20 74 68 65 20 6d 61  * and set the ma
9e850 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20  llocFailed flag 
9e860 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
9e870 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53  connection..*/.S
9e880 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
9e890 69 64 20 2a 73 71 6c 69 74 65 33 44 62 52 65 61  id *sqlite3DbRea
9e8a0 6c 6c 6f 63 4f 72 46 72 65 65 28 73 71 6c 69 74  llocOrFree(sqlit
9e8b0 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a 70 2c  e3 *db, void *p,
9e8c0 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20   int n){.  void 
9e8d0 2a 70 4e 65 77 3b 0a 20 20 70 4e 65 77 20 3d 20  *pNew;.  pNew = 
9e8e0 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
9e8f0 28 64 62 2c 20 70 2c 20 6e 29 3b 0a 20 20 69 66  (db, p, n);.  if
9e900 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 73  ( !pNew ){.    s
9e910 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
9e920 20 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   p);.  }.  retur
9e930 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pNew;.}../*.**
9e940 20 4d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20   Make a copy of 
9e950 61 20 73 74 72 69 6e 67 20 69 6e 20 6d 65 6d 6f  a string in memo
9e960 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
9e970 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e   sqliteMalloc().
9e980 20 54 68 65 73 65 20 0a 2a 2a 20 66 75 6e 63 74   These .** funct
9e990 69 6f 6e 73 20 63 61 6c 6c 20 73 71 6c 69 74 65  ions call sqlite
9e9a0 33 4d 61 6c 6c 6f 63 52 61 77 28 29 20 64 69 72  3MallocRaw() dir
9e9b0 65 63 74 6c 79 20 69 6e 73 74 65 61 64 20 6f 66  ectly instead of
9e9c0 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e   sqliteMalloc().
9e9d0 20 54 68 69 73 0a 2a 2a 20 69 73 20 62 65 63 61   This.** is beca
9e9e0 75 73 65 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20  use when memory 
9e9f0 64 65 62 75 67 67 69 6e 67 20 69 73 20 74 75 72  debugging is tur
9ea00 6e 65 64 20 6f 6e 2c 20 74 68 65 73 65 20 74 77  ned on, these tw
9ea10 6f 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  o functions are 
9ea20 0a 2a 2a 20 63 61 6c 6c 65 64 20 76 69 61 20 6d  .** called via m
9ea30 61 63 72 6f 73 20 74 68 61 74 20 72 65 63 6f 72  acros that recor
9ea40 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 66 69  d the current fi
9ea50 6c 65 20 61 6e 64 20 6c 69 6e 65 20 6e 75 6d 62  le and line numb
9ea60 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20 54 68 72  er in the.** Thr
9ea70 65 61 64 44 61 74 61 20 73 74 72 75 63 74 75 72  eadData structur
9ea80 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
9ea90 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
9eaa0 65 33 44 62 53 74 72 44 75 70 28 73 71 6c 69 74  e3DbStrDup(sqlit
9eab0 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68  e3 *db, const ch
9eac0 61 72 20 2a 7a 29 7b 0a 20 20 63 68 61 72 20 2a  ar *z){.  char *
9ead0 7a 4e 65 77 3b 0a 20 20 73 69 7a 65 5f 74 20 6e  zNew;.  size_t n
9eae0 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a  ;.  if( z==0 ){.
9eaf0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
9eb00 7d 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 53  }.  n = sqlite3S
9eb10 74 72 6c 65 6e 33 30 28 7a 29 20 2b 20 31 3b 0a  trlen30(z) + 1;.
9eb20 20 20 61 73 73 65 72 74 28 20 28 6e 26 30 78 37    assert( (n&0x7
9eb30 66 66 66 66 66 66 66 29 3d 3d 6e 20 29 3b 0a 20  fffffff)==n );. 
9eb40 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   zNew = sqlite3D
9eb50 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 28  bMallocRaw(db, (
9eb60 69 6e 74 29 6e 29 3b 0a 20 20 69 66 28 20 7a 4e  int)n);.  if( zN
9eb70 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79  ew ){.    memcpy
9eb80 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20  (zNew, z, n);.  
9eb90 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 65 77 3b  }.  return zNew;
9eba0 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
9ebb0 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44  E char *sqlite3D
9ebc0 62 53 74 72 4e 44 75 70 28 73 71 6c 69 74 65 33  bStrNDup(sqlite3
9ebd0 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   *db, const char
9ebe0 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 63   *z, int n){.  c
9ebf0 68 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 69 66 28  har *zNew;.  if(
9ec00 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74   z==0 ){.    ret
9ec10 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73  urn 0;.  }.  ass
9ec20 65 72 74 28 20 28 6e 26 30 78 37 66 66 66 66 66  ert( (n&0x7fffff
9ec30 66 66 29 3d 3d 6e 20 29 3b 0a 20 20 7a 4e 65 77  ff)==n );.  zNew
9ec40 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
9ec50 6f 63 52 61 77 28 64 62 2c 20 6e 2b 31 29 3b 0a  ocRaw(db, n+1);.
9ec60 20 20 69 66 28 20 7a 4e 65 77 20 29 7b 0a 20 20    if( zNew ){.  
9ec70 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 7a    memcpy(zNew, z
9ec80 2c 20 6e 29 3b 0a 20 20 20 20 7a 4e 65 77 5b 6e  , n);.    zNew[n
9ec90 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  ] = 0;.  }.  ret
9eca0 75 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a  urn zNew;.}../*.
9ecb0 2a 2a 20 43 72 65 61 74 65 20 61 20 73 74 72 69  ** Create a stri
9ecc0 6e 67 20 66 72 6f 6d 20 74 68 65 20 7a 46 72 6f  ng from the zFro
9ecd0 6d 61 74 20 61 72 67 75 6d 65 6e 74 20 61 6e 64  mat argument and
9ece0 20 74 68 65 20 76 61 5f 6c 69 73 74 20 74 68 61   the va_list tha
9ecf0 74 20 66 6f 6c 6c 6f 77 73 2e 0a 2a 2a 20 53 74  t follows..** St
9ed00 6f 72 65 20 74 68 65 20 73 74 72 69 6e 67 20 69  ore the string i
9ed10 6e 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  n memory obtaine
9ed20 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c  d from sqliteMal
9ed30 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b 65 20 2a  loc() and make *
9ed40 70 7a 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74  pz.** point to t
9ed50 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53  hat string..*/.S
9ed60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
9ed70 69 64 20 73 71 6c 69 74 65 33 53 65 74 53 74 72  id sqlite3SetStr
9ed80 69 6e 67 28 63 68 61 72 20 2a 2a 70 7a 2c 20 73  ing(char **pz, s
9ed90 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73  qlite3 *db, cons
9eda0 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c  t char *zFormat,
9edb0 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74   ...){.  va_list
9edc0 20 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a   ap;.  char *z;.
9edd0 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20  .  va_start(ap, 
9ede0 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20  zFormat);.  z = 
9edf0 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28  sqlite3VMPrintf(
9ee00 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29  db, zFormat, ap)
9ee10 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  ;.  va_end(ap);.
9ee20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
9ee30 64 62 2c 20 2a 70 7a 29 3b 0a 20 20 2a 70 7a 20  db, *pz);.  *pz 
9ee40 3d 20 7a 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54  = z;.}.../*.** T
9ee50 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73  his function mus
9ee60 74 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f  t be called befo
9ee70 72 65 20 65 78 69 74 69 6e 67 20 61 6e 79 20 41  re exiting any A
9ee80 50 49 20 66 75 6e 63 74 69 6f 6e 20 28 69 2e 65  PI function (i.e
9ee90 2e 20 0a 2a 2a 20 72 65 74 75 72 6e 69 6e 67 20  . .** returning 
9eea0 63 6f 6e 74 72 6f 6c 20 74 6f 20 74 68 65 20 75  control to the u
9eeb0 73 65 72 29 20 74 68 61 74 20 68 61 73 20 63 61  ser) that has ca
9eec0 6c 6c 65 64 20 73 71 6c 69 74 65 33 5f 6d 61 6c  lled sqlite3_mal
9eed0 6c 6f 63 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65  loc or.** sqlite
9eee0 33 5f 72 65 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a 2a  3_realloc..**.**
9eef0 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 76 61   The returned va
9ef00 6c 75 65 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20  lue is normally 
9ef10 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 65  a copy of the se
9ef20 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  cond argument to
9ef30 20 74 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f   this.** functio
9ef40 6e 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20 61  n. However, if a
9ef50 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72   malloc() failur
9ef60 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 20 73  e has occurred s
9ef70 69 6e 63 65 20 74 68 65 20 70 72 65 76 69 6f 75  ince the previou
9ef80 73 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 20  s.** invocation 
9ef90 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 73 20  SQLITE_NOMEM is 
9efa0 72 65 74 75 72 6e 65 64 20 69 6e 73 74 65 61 64  returned instead
9efb0 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  . .**.** If the 
9efc0 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20  first argument, 
9efd0 64 62 2c 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20  db, is not NULL 
9efe0 61 6e 64 20 61 20 6d 61 6c 6c 6f 63 28 29 20 65  and a malloc() e
9eff0 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65  rror has occurre
9f000 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 63  d,.** then the c
9f010 6f 6e 6e 65 63 74 69 6f 6e 20 65 72 72 6f 72 2d  onnection error-
9f020 63 6f 64 65 20 28 74 68 65 20 76 61 6c 75 65 20  code (the value 
9f030 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69  returned by sqli
9f040 74 65 33 5f 65 72 72 63 6f 64 65 28 29 29 0a 2a  te3_errcode()).*
9f050 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51 4c 49  * is set to SQLI
9f060 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 53 51 4c  TE_NOMEM..*/.SQL
9f070 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
9f080 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 73  sqlite3ApiExit(s
9f090 71 6c 69 74 65 33 2a 20 64 62 2c 20 69 6e 74 20  qlite3* db, int 
9f0a0 72 63 29 7b 0a 20 20 2f 2a 20 49 66 20 74 68 65  rc){.  /* If the
9f0b0 20 64 62 20 68 61 6e 64 6c 65 20 69 73 20 6e 6f   db handle is no
9f0c0 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 77 65 20  t NULL, then we 
9f0d0 6d 75 73 74 20 68 6f 6c 64 20 74 68 65 20 63 6f  must hold the co
9f0e0 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 0a  nnection handle.
9f0f0 20 20 2a 2a 20 6d 75 74 65 78 20 68 65 72 65 2e    ** mutex here.
9f100 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 20 72   Otherwise the r
9f110 65 61 64 20 28 61 6e 64 20 70 6f 73 73 69 62 6c  ead (and possibl
9f120 65 20 77 72 69 74 65 29 20 6f 66 20 64 62 2d 3e  e write) of db->
9f130 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 0a 20 20  mallocFailed .  
9f140 2a 2a 20 69 73 20 75 6e 73 61 66 65 2c 20 61 73  ** is unsafe, as
9f150 20 69 73 20 74 68 65 20 63 61 6c 6c 20 74 6f 20   is the call to 
9f160 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 2e 0a  sqlite3Error()..
9f170 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 21    */.  assert( !
9f180 64 62 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  db || sqlite3_mu
9f190 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
9f1a0 65 78 29 20 29 3b 0a 20 20 69 66 28 20 64 62 20  ex) );.  if( db 
9f1b0 26 26 20 28 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  && (db->mallocFa
9f1c0 69 6c 65 64 20 7c 7c 20 72 63 3d 3d 53 51 4c 49  iled || rc==SQLI
9f1d0 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 29 20  TE_IOERR_NOMEM) 
9f1e0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
9f1f0 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4e  ror(db, SQLITE_N
9f200 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20 20 20 64 62  OMEM, 0);.    db
9f210 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
9f220 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   0;.    rc = SQL
9f230 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
9f240 20 72 65 74 75 72 6e 20 72 63 20 26 20 28 64 62   return rc & (db
9f250 20 3f 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20 3a   ? db->errMask :
9f260 20 30 78 66 66 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a   0xff);.}../****
9f270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
9f280 66 20 6d 61 6c 6c 6f 63 2e 63 20 2a 2a 2a 2a 2a  f malloc.c *****
9f290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
9f2c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
9f2d0 20 66 69 6c 65 20 70 72 69 6e 74 66 2e 63 20 2a   file printf.c *
9f2e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f2f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
9f310 20 54 68 65 20 22 70 72 69 6e 74 66 22 20 63 6f   The "printf" co
9f320 64 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20  de that follows 
9f330 64 61 74 65 73 20 66 72 6f 6d 20 74 68 65 20 31  dates from the 1
9f340 39 38 30 27 73 2e 20 20 49 74 20 69 73 20 69 6e  980's.  It is in
9f350 0a 2a 2a 20 74 68 65 20 70 75 62 6c 69 63 20 64  .** the public d
9f360 6f 6d 61 69 6e 2e 20 20 54 68 65 20 6f 72 69 67  omain.  The orig
9f370 69 6e 61 6c 20 63 6f 6d 6d 65 6e 74 73 20 61 72  inal comments ar
9f380 65 20 69 6e 63 6c 75 64 65 64 20 68 65 72 65 20  e included here 
9f390 66 6f 72 0a 2a 2a 20 63 6f 6d 70 6c 65 74 65 6e  for.** completen
9f3a0 65 73 73 2e 20 20 54 68 65 79 20 61 72 65 20 76  ess.  They are v
9f3b0 65 72 79 20 6f 75 74 2d 6f 66 2d 64 61 74 65 20  ery out-of-date 
9f3c0 62 75 74 20 6d 69 67 68 74 20 62 65 20 75 73 65  but might be use
9f3d0 66 75 6c 20 61 73 0a 2a 2a 20 61 6e 20 68 69 73  ful as.** an his
9f3e0 74 6f 72 69 63 61 6c 20 72 65 66 65 72 65 6e 63  torical referenc
9f3f0 65 2e 20 20 4d 6f 73 74 20 6f 66 20 74 68 65 20  e.  Most of the 
9f400 22 65 6e 68 61 6e 63 65 6d 65 6e 74 73 22 20 68  "enhancements" h
9f410 61 76 65 20 62 65 65 6e 20 62 61 63 6b 65 64 0a  ave been backed.
9f420 2a 2a 20 6f 75 74 20 73 6f 20 74 68 61 74 20 74  ** out so that t
9f430 68 65 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79  he functionality
9f440 20 69 73 20 6e 6f 77 20 74 68 65 20 73 61 6d 65   is now the same
9f450 20 61 73 20 73 74 61 6e 64 61 72 64 20 70 72 69   as standard pri
9f460 6e 74 66 28 29 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  ntf()..**.** $Id
9f470 3a 20 70 72 69 6e 74 66 2e 63 2c 76 20 31 2e 31  : printf.c,v 1.1
9f480 30 34 20 32 30 30 39 2f 30 36 2f 30 33 20 30 31  04 2009/06/03 01
9f490 3a 32 34 3a 35 34 20 64 72 68 20 45 78 70 20 24  :24:54 drh Exp $
9f4a0 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
9f4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9f4e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
9f4f0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
9f500 6e 67 20 6d 6f 64 75 6c 65 73 20 69 73 20 61 6e  ng modules is an
9f510 20 65 6e 68 61 6e 63 65 64 20 72 65 70 6c 61 63   enhanced replac
9f520 65 6d 65 6e 74 20 66 6f 72 20 74 68 65 20 22 70  ement for the "p
9f530 72 69 6e 74 66 22 20 73 75 62 72 6f 75 74 69 6e  rintf" subroutin
9f540 65 73 0a 2a 2a 20 66 6f 75 6e 64 20 69 6e 20 74  es.** found in t
9f550 68 65 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69  he standard C li
9f560 62 72 61 72 79 2e 20 20 54 68 65 20 66 6f 6c 6c  brary.  The foll
9f570 6f 77 69 6e 67 20 65 6e 68 61 6e 63 65 6d 65 6e  owing enhancemen
9f580 74 73 20 61 72 65 0a 2a 2a 20 73 75 70 70 6f 72  ts are.** suppor
9f590 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ted:.**.**      
9f5a0 2b 20 20 41 64 64 69 74 69 6f 6e 61 6c 20 66 75  +  Additional fu
9f5b0 6e 63 74 69 6f 6e 73 2e 20 20 54 68 65 20 73 74  nctions.  The st
9f5c0 61 6e 64 61 72 64 20 73 65 74 20 6f 66 20 22 70  andard set of "p
9f5d0 72 69 6e 74 66 22 20 66 75 6e 63 74 69 6f 6e 73  rintf" functions
9f5e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 63 6c  .**         incl
9f5f0 75 64 65 73 20 70 72 69 6e 74 66 2c 20 66 70 72  udes printf, fpr
9f600 69 6e 74 66 2c 20 73 70 72 69 6e 74 66 2c 20 76  intf, sprintf, v
9f610 70 72 69 6e 74 66 2c 20 76 66 70 72 69 6e 74 66  printf, vfprintf
9f620 2c 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20  , and.**        
9f630 20 76 73 70 72 69 6e 74 66 2e 20 20 54 68 69 73   vsprintf.  This
9f640 20 6d 6f 64 75 6c 65 20 61 64 64 73 20 74 68 65   module adds the
9f650 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a   following:.**.*
9f660 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 73  *           *  s
9f670 6e 70 72 69 6e 74 66 20 2d 2d 20 57 6f 72 6b 73  nprintf -- Works
9f680 20 6c 69 6b 65 20 73 70 72 69 6e 74 66 2c 20 62   like sprintf, b
9f690 75 74 20 68 61 73 20 61 6e 20 65 78 74 72 61 20  ut has an extra 
9f6a0 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 20  argument.**     
9f6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9f6c0 20 20 20 20 20 77 68 69 63 68 20 69 73 20 74 68       which is th
9f6d0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 75  e size of the bu
9f6e0 66 66 65 72 20 77 72 69 74 74 65 6e 20 74 6f 2e  ffer written to.
9f6f0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .**.**          
9f700 20 2a 20 20 6d 70 72 69 6e 74 66 20 2d 2d 20 20   *  mprintf --  
9f710 53 69 6d 69 6c 61 72 20 74 6f 20 73 70 72 69 6e  Similar to sprin
9f720 74 66 2e 20 20 57 72 69 74 65 73 20 6f 75 74 70  tf.  Writes outp
9f730 75 74 20 74 6f 20 6d 65 6d 6f 72 79 0a 2a 2a 20  ut to memory.** 
9f740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9f750 20 20 20 20 20 20 20 20 20 6f 62 74 61 69 6e 65           obtaine
9f760 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a  d from malloc..*
9f770 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a  *.**           *
9f780 20 20 78 70 72 69 6e 74 66 20 2d 2d 20 20 43 61    xprintf --  Ca
9f790 6c 6c 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 74  lls a function t
9f7a0 6f 20 64 69 73 70 6f 73 65 20 6f 66 20 6f 75 74  o dispose of out
9f7b0 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  put..**.**      
9f7c0 20 20 20 20 20 2a 20 20 6e 70 72 69 6e 74 66 20       *  nprintf 
9f7d0 2d 2d 20 20 4e 6f 20 6f 75 74 70 75 74 2c 20 62  --  No output, b
9f7e0 75 74 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  ut returns the n
9f7f0 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
9f800 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ers.**          
9f810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9f820 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76 65 20  that would have 
9f830 62 65 65 6e 20 6f 75 74 70 75 74 20 62 79 20 70  been output by p
9f840 72 69 6e 74 66 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  rintf..**.**    
9f850 20 20 20 20 20 20 20 2a 20 20 41 20 76 2d 20 76         *  A v- v
9f860 65 72 73 69 6f 6e 20 28 65 78 3a 20 76 73 6e 70  ersion (ex: vsnp
9f870 72 69 6e 74 66 29 20 6f 66 20 65 76 65 72 79 20  rintf) of every 
9f880 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 6c 73 6f  function is also
9f890 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
9f8a0 20 73 75 70 70 6c 69 65 64 2e 0a 2a 2a 0a 2a 2a   supplied..**.**
9f8b0 20 20 20 20 20 20 2b 20 20 41 20 66 65 77 20 65        +  A few e
9f8c0 78 74 65 6e 73 69 6f 6e 73 20 74 6f 20 74 68 65  xtensions to the
9f8d0 20 66 6f 72 6d 61 74 74 69 6e 67 20 6e 6f 74 61   formatting nota
9f8e0 74 69 6f 6e 20 61 72 65 20 73 75 70 70 6f 72 74  tion are support
9f8f0 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  ed:.**.**       
9f900 20 20 20 20 2a 20 20 54 68 65 20 22 3d 22 20 66      *  The "=" f
9f910 6c 61 67 20 28 73 69 6d 69 6c 61 72 20 74 6f 20  lag (similar to 
9f920 22 2d 22 29 20 63 61 75 73 65 73 20 74 68 65 20  "-") causes the 
9f930 6f 75 74 70 75 74 20 74 6f 20 62 65 0a 2a 2a 20  output to be.** 
9f940 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65 20               be 
9f950 63 65 6e 74 65 72 65 64 20 69 6e 20 74 68 65 20  centered in the 
9f960 61 70 70 72 6f 70 72 69 61 74 65 6c 79 20 73 69  appropriately si
9f970 7a 65 64 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a  zed field..**.**
9f980 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 54 68             *  Th
9f990 65 20 25 62 20 66 69 65 6c 64 20 6f 75 74 70 75  e %b field outpu
9f9a0 74 73 20 61 6e 20 69 6e 74 65 67 65 72 20 69 6e  ts an integer in
9f9b0 20 62 69 6e 61 72 79 20 6e 6f 74 61 74 69 6f 6e   binary notation
9f9c0 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  ..**.**         
9f9d0 20 20 2a 20 20 54 68 65 20 25 63 20 66 69 65 6c    *  The %c fiel
9f9e0 64 20 6e 6f 77 20 61 63 63 65 70 74 73 20 61 20  d now accepts a 
9f9f0 70 72 65 63 69 73 69 6f 6e 2e 20 20 54 68 65 20  precision.  The 
9fa00 63 68 61 72 61 63 74 65 72 20 6f 75 74 70 75 74  character output
9fa10 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
9fa20 20 69 73 20 72 65 70 65 61 74 65 64 20 62 79 20   is repeated by 
9fa30 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69  the number of ti
9fa40 6d 65 73 20 74 68 65 20 70 72 65 63 69 73 69 6f  mes the precisio
9fa50 6e 20 73 70 65 63 69 66 69 65 73 2e 0a 2a 2a 0a  n specifies..**.
9fa60 2a 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20  **           *  
9fa70 54 68 65 20 25 27 20 66 69 65 6c 64 20 77 6f 72  The %' field wor
9fa80 6b 73 20 6c 69 6b 65 20 25 63 2c 20 62 75 74 20  ks like %c, but 
9fa90 74 61 6b 65 73 20 61 73 20 69 74 73 20 63 68 61  takes as its cha
9faa0 72 61 63 74 65 72 20 74 68 65 0a 2a 2a 20 20 20  racter the.**   
9fab0 20 20 20 20 20 20 20 20 20 20 20 6e 65 78 74 20             next 
9fac0 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65  character of the
9fad0 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 2c 20   format string, 
9fae0 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65 20 6e  instead of the n
9faf0 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ext.**          
9fb00 20 20 20 20 61 72 67 75 6d 65 6e 74 2e 20 20 46      argument.  F
9fb10 6f 72 20 65 78 61 6d 70 6c 65 2c 20 20 70 72 69  or example,  pri
9fb20 6e 74 66 28 22 25 2e 37 38 27 2d 22 29 20 20 70  ntf("%.78'-")  p
9fb30 72 69 6e 74 73 20 37 38 20 6d 69 6e 75 73 0a 2a  rints 78 minus.*
9fb40 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  *              s
9fb50 69 67 6e 73 2c 20 74 68 65 20 73 61 6d 65 20 61  igns, the same a
9fb60 73 20 20 70 72 69 6e 74 66 28 22 25 2e 37 38 63  s  printf("%.78c
9fb70 22 2c 27 2d 27 29 2e 0a 2a 2a 0a 2a 2a 20 20 20  ",'-')..**.**   
9fb80 20 20 20 2b 20 20 57 68 65 6e 20 63 6f 6d 70 69     +  When compi
9fb90 6c 65 64 20 75 73 69 6e 67 20 47 43 43 20 6f 6e  led using GCC on
9fba0 20 61 20 53 50 41 52 43 2c 20 74 68 69 73 20 76   a SPARC, this v
9fbb0 65 72 73 69 6f 6e 20 6f 66 20 70 72 69 6e 74 66  ersion of printf
9fbc0 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 66   is.**         f
9fbd0 61 73 74 65 72 20 74 68 61 6e 20 74 68 65 20 6c  aster than the l
9fbe0 69 62 72 61 72 79 20 70 72 69 6e 74 66 20 66 6f  ibrary printf fo
9fbf0 72 20 53 55 4e 20 4f 53 20 34 2e 31 2e 0a 2a 2a  r SUN OS 4.1..**
9fc00 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 6c 6c 20  .**      +  All 
9fc10 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 66 75  functions are fu
9fc20 6c 6c 79 20 72 65 65 6e 74 72 61 6e 74 2e 0a 2a  lly reentrant..*
9fc30 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  *.*/../*.** Conv
9fc40 65 72 73 69 6f 6e 20 74 79 70 65 73 20 66 61 6c  ersion types fal
9fc50 6c 20 69 6e 74 6f 20 76 61 72 69 6f 75 73 20 63  l into various c
9fc60 61 74 65 67 6f 72 69 65 73 20 61 73 20 64 65 66  ategories as def
9fc70 69 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 66  ined by the.** f
9fc80 6f 6c 6c 6f 77 69 6e 67 20 65 6e 75 6d 65 72 61  ollowing enumera
9fc90 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  tion..*/.#define
9fca0 20 65 74 52 41 44 49 58 20 20 20 20 20 20 20 31   etRADIX       1
9fcb0 20 2f 2a 20 49 6e 74 65 67 65 72 20 74 79 70 65   /* Integer type
9fcc0 73 2e 20 20 25 64 2c 20 25 78 2c 20 25 6f 2c 20  s.  %d, %x, %o, 
9fcd0 61 6e 64 20 73 6f 20 66 6f 72 74 68 20 2a 2f 0a  and so forth */.
9fce0 23 64 65 66 69 6e 65 20 65 74 46 4c 4f 41 54 20  #define etFLOAT 
9fcf0 20 20 20 20 20 20 32 20 2f 2a 20 46 6c 6f 61 74        2 /* Float
9fd00 69 6e 67 20 70 6f 69 6e 74 2e 20 20 25 66 20 2a  ing point.  %f *
9fd10 2f 0a 23 64 65 66 69 6e 65 20 65 74 45 58 50 20  /.#define etEXP 
9fd20 20 20 20 20 20 20 20 20 33 20 2f 2a 20 45 78 70          3 /* Exp
9fd30 6f 6e 65 6e 74 69 6f 6e 61 6c 20 6e 6f 74 61 74  onentional notat
9fd40 69 6f 6e 2e 20 25 65 20 61 6e 64 20 25 45 20 2a  ion. %e and %E *
9fd50 2f 0a 23 64 65 66 69 6e 65 20 65 74 47 45 4e 45  /.#define etGENE
9fd60 52 49 43 20 20 20 20 20 34 20 2f 2a 20 46 6c 6f  RIC     4 /* Flo
9fd70 61 74 69 6e 67 20 6f 72 20 65 78 70 6f 6e 65 6e  ating or exponen
9fd80 74 69 61 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20  tial, depending 
9fd90 6f 6e 20 65 78 70 6f 6e 65 6e 74 2e 20 25 67 20  on exponent. %g 
9fda0 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 49 5a  */.#define etSIZ
9fdb0 45 20 20 20 20 20 20 20 20 35 20 2f 2a 20 52 65  E        5 /* Re
9fdc0 74 75 72 6e 20 6e 75 6d 62 65 72 20 6f 66 20 63  turn number of c
9fdd0 68 61 72 61 63 74 65 72 73 20 70 72 6f 63 65 73  haracters proces
9fde0 73 65 64 20 73 6f 20 66 61 72 2e 20 25 6e 20 2a  sed so far. %n *
9fdf0 2f 0a 23 64 65 66 69 6e 65 20 65 74 53 54 52 49  /.#define etSTRI
9fe00 4e 47 20 20 20 20 20 20 36 20 2f 2a 20 53 74 72  NG      6 /* Str
9fe10 69 6e 67 73 2e 20 25 73 20 2a 2f 0a 23 64 65 66  ings. %s */.#def
9fe20 69 6e 65 20 65 74 44 59 4e 53 54 52 49 4e 47 20  ine etDYNSTRING 
9fe30 20 20 37 20 2f 2a 20 44 79 6e 61 6d 69 63 61 6c    7 /* Dynamical
9fe40 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 74 72  ly allocated str
9fe50 69 6e 67 73 2e 20 25 7a 20 2a 2f 0a 23 64 65 66  ings. %z */.#def
9fe60 69 6e 65 20 65 74 50 45 52 43 45 4e 54 20 20 20  ine etPERCENT   
9fe70 20 20 38 20 2f 2a 20 50 65 72 63 65 6e 74 20 73    8 /* Percent s
9fe80 79 6d 62 6f 6c 2e 20 25 25 20 2a 2f 0a 23 64 65  ymbol. %% */.#de
9fe90 66 69 6e 65 20 65 74 43 48 41 52 58 20 20 20 20  fine etCHARX    
9fea0 20 20 20 39 20 2f 2a 20 43 68 61 72 61 63 74 65     9 /* Characte
9feb0 72 73 2e 20 25 63 20 2a 2f 0a 2f 2a 20 54 68 65  rs. %c */./* The
9fec0 20 72 65 73 74 20 61 72 65 20 65 78 74 65 6e 73   rest are extens
9fed0 69 6f 6e 73 2c 20 6e 6f 74 20 6e 6f 72 6d 61 6c  ions, not normal
9fee0 6c 79 20 66 6f 75 6e 64 20 69 6e 20 70 72 69 6e  ly found in prin
9fef0 74 66 28 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20  tf() */.#define 
9ff00 65 74 53 51 4c 45 53 43 41 50 45 20 20 31 30 20  etSQLESCAPE  10 
9ff10 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74 68 20  /* Strings with 
9ff20 27 5c 27 27 20 64 6f 75 62 6c 65 64 2e 20 20 25  '\'' doubled.  %
9ff30 71 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53  q */.#define etS
9ff40 51 4c 45 53 43 41 50 45 32 20 31 31 20 2f 2a 20  QLESCAPE2 11 /* 
9ff50 53 74 72 69 6e 67 73 20 77 69 74 68 20 27 5c 27  Strings with '\'
9ff60 27 20 64 6f 75 62 6c 65 64 20 61 6e 64 20 65 6e  ' doubled and en
9ff70 63 6c 6f 73 65 64 20 69 6e 20 27 27 2c 0a 20 20  closed in '',.  
9ff80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9ff90 20 20 20 20 20 20 20 20 4e 55 4c 4c 20 70 6f 69          NULL poi
9ffa0 6e 74 65 72 73 20 72 65 70 6c 61 63 65 64 20 62  nters replaced b
9ffb0 79 20 53 51 4c 20 4e 55 4c 4c 2e 20 20 25 51 20  y SQL NULL.  %Q 
9ffc0 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 54 4f 4b  */.#define etTOK
9ffd0 45 4e 20 20 20 20 20 20 31 32 20 2f 2a 20 61 20  EN      12 /* a 
9ffe0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54 6f 6b  pointer to a Tok
9fff0 65 6e 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  en structure */.
a0000 23 64 65 66 69 6e 65 20 65 74 53 52 43 4c 49 53  #define etSRCLIS
a0010 54 20 20 20 20 31 33 20 2f 2a 20 61 20 70 6f 69  T    13 /* a poi
a0020 6e 74 65 72 20 74 6f 20 61 20 53 72 63 4c 69 73  nter to a SrcLis
a0030 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 50  t */.#define etP
a0040 4f 49 4e 54 45 52 20 20 20 20 31 34 20 2f 2a 20  OINTER    14 /* 
a0050 54 68 65 20 25 70 20 63 6f 6e 76 65 72 73 69 6f  The %p conversio
a0060 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53  n */.#define etS
a0070 51 4c 45 53 43 41 50 45 33 20 31 35 20 2f 2a 20  QLESCAPE3 15 /* 
a0080 25 77 20 2d 3e 20 53 74 72 69 6e 67 73 20 77 69  %w -> Strings wi
a0090 74 68 20 27 5c 22 27 20 64 6f 75 62 6c 65 64 20  th '\"' doubled 
a00a0 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 4f 52 44  */.#define etORD
a00b0 49 4e 41 4c 20 20 20 20 31 36 20 2f 2a 20 25 72  INAL    16 /* %r
a00c0 20 2d 3e 20 31 73 74 2c 20 32 6e 64 2c 20 33 72   -> 1st, 2nd, 3r
a00d0 64 2c 20 34 74 68 2c 20 65 74 63 2e 20 20 45 6e  d, 4th, etc.  En
a00e0 67 6c 69 73 68 20 6f 6e 6c 79 20 2a 2f 0a 0a 23  glish only */..#
a00f0 64 65 66 69 6e 65 20 65 74 49 4e 56 41 4c 49 44  define etINVALID
a0100 20 20 20 20 20 30 20 2f 2a 20 41 6e 79 20 75 6e       0 /* Any un
a0110 72 65 63 6f 67 6e 69 7a 65 64 20 63 6f 6e 76 65  recognized conve
a0120 72 73 69 6f 6e 20 74 79 70 65 20 2a 2f 0a 0a 0a  rsion type */...
a0130 2f 2a 0a 2a 2a 20 41 6e 20 22 65 74 42 79 74 65  /*.** An "etByte
a0140 22 20 69 73 20 61 6e 20 38 2d 62 69 74 20 75 6e  " is an 8-bit un
a0150 73 69 67 6e 65 64 20 76 61 6c 75 65 2e 0a 2a 2f  signed value..*/
a0160 0a 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65  .typedef unsigne
a0170 64 20 63 68 61 72 20 65 74 42 79 74 65 3b 0a 0a  d char etByte;..
a0180 2f 2a 0a 2a 2a 20 45 61 63 68 20 62 75 69 6c 74  /*.** Each built
a0190 69 6e 20 63 6f 6e 76 65 72 73 69 6f 6e 20 63 68  in conversion ch
a01a0 61 72 61 63 74 65 72 20 28 65 78 3a 20 74 68 65  aracter (ex: the
a01b0 20 27 64 27 20 69 6e 20 22 25 64 22 29 20 69 73   'd' in "%d") is
a01c0 20 64 65 73 63 72 69 62 65 64 0a 2a 2a 20 62 79   described.** by
a01d0 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
a01e0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
a01f0 72 75 63 74 75 72 65 0a 2a 2f 0a 74 79 70 65 64  ructure.*/.typed
a0200 65 66 20 73 74 72 75 63 74 20 65 74 5f 69 6e 66  ef struct et_inf
a0210 6f 20 7b 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61  o {   /* Informa
a0220 74 69 6f 6e 20 61 62 6f 75 74 20 65 61 63 68 20  tion about each 
a0230 66 6f 72 6d 61 74 20 66 69 65 6c 64 20 2a 2f 0a  format field */.
a0240 20 20 63 68 61 72 20 66 6d 74 74 79 70 65 3b 20    char fmttype; 
a0250 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
a0260 65 20 66 6f 72 6d 61 74 20 66 69 65 6c 64 20 63  e format field c
a0270 6f 64 65 20 6c 65 74 74 65 72 20 2a 2f 0a 20 20  ode letter */.  
a0280 65 74 42 79 74 65 20 62 61 73 65 3b 20 20 20 20  etByte base;    
a0290 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
a02a0 62 61 73 65 20 66 6f 72 20 72 61 64 69 78 20 63  base for radix c
a02b0 6f 6e 76 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 65  onversion */.  e
a02c0 74 42 79 74 65 20 66 6c 61 67 73 3b 20 20 20 20  tByte flags;    
a02d0 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
a02e0 72 20 6d 6f 72 65 20 6f 66 20 46 4c 41 47 5f 20  r more of FLAG_ 
a02f0 63 6f 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77 20  constants below 
a0300 2a 2f 0a 20 20 65 74 42 79 74 65 20 74 79 70 65  */.  etByte type
a0310 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
a0320 20 43 6f 6e 76 65 72 73 69 6f 6e 20 70 61 72 61   Conversion para
a0330 64 69 67 6d 20 2a 2f 0a 20 20 65 74 42 79 74 65  digm */.  etByte
a0340 20 63 68 61 72 73 65 74 3b 20 20 20 20 20 20 20   charset;       
a0350 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74     /* Offset int
a0360 6f 20 61 44 69 67 69 74 73 5b 5d 20 6f 66 20 74  o aDigits[] of t
a0370 68 65 20 64 69 67 69 74 73 20 73 74 72 69 6e 67  he digits string
a0380 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 70 72 65   */.  etByte pre
a0390 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f  fix;           /
a03a0 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 50  * Offset into aP
a03b0 72 65 66 69 78 5b 5d 20 6f 66 20 74 68 65 20 70  refix[] of the p
a03c0 72 65 66 69 78 20 73 74 72 69 6e 67 20 2a 2f 0a  refix string */.
a03d0 7d 20 65 74 5f 69 6e 66 6f 3b 0a 0a 2f 2a 0a 2a  } et_info;../*.*
a03e0 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73  * Allowed values
a03f0 20 66 6f 72 20 65 74 5f 69 6e 66 6f 2e 66 6c 61   for et_info.fla
a0400 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4c  gs.*/.#define FL
a0410 41 47 5f 53 49 47 4e 45 44 20 20 31 20 20 20 20  AG_SIGNED  1    
a0420 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
a0430 76 61 6c 75 65 20 74 6f 20 63 6f 6e 76 65 72 74  value to convert
a0440 20 69 73 20 73 69 67 6e 65 64 20 2a 2f 0a 23 64   is signed */.#d
a0450 65 66 69 6e 65 20 46 4c 41 47 5f 49 4e 54 45 52  efine FLAG_INTER
a0460 4e 20 20 32 20 20 20 20 20 2f 2a 20 54 72 75 65  N  2     /* True
a0470 20 69 66 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c   if for internal
a0480 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65   use only */.#de
a0490 66 69 6e 65 20 46 4c 41 47 5f 53 54 52 49 4e 47  fine FLAG_STRING
a04a0 20 20 34 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 77    4     /* Allow
a04b0 20 69 6e 66 69 6e 69 74 79 20 70 72 65 63 69 73   infinity precis
a04c0 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54  ion */.../*.** T
a04d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62  he following tab
a04e0 6c 65 20 69 73 20 73 65 61 72 63 68 65 64 20 6c  le is searched l
a04f0 69 6e 65 61 72 6c 79 2c 20 73 6f 20 69 74 20 69  inearly, so it i
a0500 73 20 67 6f 6f 64 20 74 6f 20 70 75 74 20 74 68  s good to put th
a0510 65 0a 2a 2a 20 6d 6f 73 74 20 66 72 65 71 75 65  e.** most freque
a0520 6e 74 6c 79 20 75 73 65 64 20 63 6f 6e 76 65 72  ntly used conver
a0530 73 69 6f 6e 20 74 79 70 65 73 20 66 69 72 73 74  sion types first
a0540 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
a0550 74 20 63 68 61 72 20 61 44 69 67 69 74 73 5b 5d  t char aDigits[]
a0560 20 3d 20 22 30 31 32 33 34 35 36 37 38 39 41 42   = "0123456789AB
a0570 43 44 45 46 30 31 32 33 34 35 36 37 38 39 61 62  CDEF0123456789ab
a0580 63 64 65 66 22 3b 0a 73 74 61 74 69 63 20 63 6f  cdef";.static co
a0590 6e 73 74 20 63 68 61 72 20 61 50 72 65 66 69 78  nst char aPrefix
a05a0 5b 5d 20 3d 20 22 2d 78 30 5c 30 30 30 58 30 22  [] = "-x0\000X0"
a05b0 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 65  ;.static const e
a05c0 74 5f 69 6e 66 6f 20 66 6d 74 69 6e 66 6f 5b 5d  t_info fmtinfo[]
a05d0 20 3d 20 7b 0a 20 20 7b 20 20 27 64 27 2c 20 31   = {.  {  'd', 1
a05e0 30 2c 20 31 2c 20 65 74 52 41 44 49 58 2c 20 20  0, 1, etRADIX,  
a05f0 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b      0,  0 },.  {
a0600 20 20 27 73 27 2c 20 20 30 2c 20 34 2c 20 65 74    's',  0, 4, et
a0610 53 54 52 49 4e 47 2c 20 20 20 20 20 30 2c 20 20  STRING,     0,  
a0620 30 20 7d 2c 0a 20 20 7b 20 20 27 67 27 2c 20 20  0 },.  {  'g',  
a0630 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 43 2c  0, 1, etGENERIC,
a0640 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b      30, 0 },.  {
a0650 20 20 27 7a 27 2c 20 20 30 2c 20 34 2c 20 65 74    'z',  0, 4, et
a0660 44 59 4e 53 54 52 49 4e 47 2c 20 20 30 2c 20 20  DYNSTRING,  0,  
a0670 30 20 7d 2c 0a 20 20 7b 20 20 27 71 27 2c 20 20  0 },.  {  'q',  
a0680 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 41 50  0, 4, etSQLESCAP
a0690 45 2c 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b  E,  0,  0 },.  {
a06a0 20 20 27 51 27 2c 20 20 30 2c 20 34 2c 20 65 74    'Q',  0, 4, et
a06b0 53 51 4c 45 53 43 41 50 45 32 2c 20 30 2c 20 20  SQLESCAPE2, 0,  
a06c0 30 20 7d 2c 0a 20 20 7b 20 20 27 77 27 2c 20 20  0 },.  {  'w',  
a06d0 30 2c 20 34 2c 20 65 74 53 51 4c 45 53 43 41 50  0, 4, etSQLESCAP
a06e0 45 33 2c 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b  E3, 0,  0 },.  {
a06f0 20 20 27 63 27 2c 20 20 30 2c 20 30 2c 20 65 74    'c',  0, 0, et
a0700 43 48 41 52 58 2c 20 20 20 20 20 20 30 2c 20 20  CHARX,      0,  
a0710 30 20 7d 2c 0a 20 20 7b 20 20 27 6f 27 2c 20 20  0 },.  {  'o',  
a0720 38 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20 20  8, 0, etRADIX,  
a0730 20 20 20 20 30 2c 20 20 32 20 7d 2c 0a 20 20 7b      0,  2 },.  {
a0740 20 20 27 75 27 2c 20 31 30 2c 20 30 2c 20 65 74    'u', 10, 0, et
a0750 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20 20  RADIX,      0,  
a0760 30 20 7d 2c 0a 20 20 7b 20 20 27 78 27 2c 20 31  0 },.  {  'x', 1
a0770 36 2c 20 30 2c 20 65 74 52 41 44 49 58 2c 20 20  6, 0, etRADIX,  
a0780 20 20 20 20 31 36 2c 20 31 20 7d 2c 0a 20 20 7b      16, 1 },.  {
a0790 20 20 27 58 27 2c 20 31 36 2c 20 30 2c 20 65 74    'X', 16, 0, et
a07a0 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c 20 20  RADIX,      0,  
a07b0 34 20 7d 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c  4 },.#ifndef SQL
a07c0 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
a07d0 47 5f 50 4f 49 4e 54 0a 20 20 7b 20 20 27 66 27  G_POINT.  {  'f'
a07e0 2c 20 20 30 2c 20 31 2c 20 65 74 46 4c 4f 41 54  ,  0, 1, etFLOAT
a07f0 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a  ,      0,  0 },.
a0800 20 20 7b 20 20 27 65 27 2c 20 20 30 2c 20 31 2c    {  'e',  0, 1,
a0810 20 65 74 45 58 50 2c 20 20 20 20 20 20 20 20 33   etEXP,        3
a0820 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 20 27 45 27  0, 0 },.  {  'E'
a0830 2c 20 20 30 2c 20 31 2c 20 65 74 45 58 50 2c 20  ,  0, 1, etEXP, 
a0840 20 20 20 20 20 20 20 31 34 2c 20 30 20 7d 2c 0a         14, 0 },.
a0850 20 20 7b 20 20 27 47 27 2c 20 20 30 2c 20 31 2c    {  'G',  0, 1,
a0860 20 65 74 47 45 4e 45 52 49 43 2c 20 20 20 20 31   etGENERIC,    1
a0870 34 2c 20 30 20 7d 2c 0a 23 65 6e 64 69 66 0a 20  4, 0 },.#endif. 
a0880 20 7b 20 20 27 69 27 2c 20 31 30 2c 20 31 2c 20   {  'i', 10, 1, 
a0890 65 74 52 41 44 49 58 2c 20 20 20 20 20 20 30 2c  etRADIX,      0,
a08a0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 6e 27 2c    0 },.  {  'n',
a08b0 20 20 30 2c 20 30 2c 20 65 74 53 49 5a 45 2c 20    0, 0, etSIZE, 
a08c0 20 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20        0,  0 },. 
a08d0 20 7b 20 20 27 25 27 2c 20 20 30 2c 20 30 2c 20   {  '%',  0, 0, 
a08e0 65 74 50 45 52 43 45 4e 54 2c 20 20 20 20 30 2c  etPERCENT,    0,
a08f0 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27 70 27 2c    0 },.  {  'p',
a0900 20 31 36 2c 20 30 2c 20 65 74 50 4f 49 4e 54 45   16, 0, etPOINTE
a0910 52 2c 20 20 20 20 30 2c 20 20 31 20 7d 2c 0a 0a  R,    0,  1 },..
a0920 2f 2a 20 41 6c 6c 20 74 68 65 20 72 65 73 74 20  /* All the rest 
a0930 68 61 76 65 20 74 68 65 20 46 4c 41 47 5f 49 4e  have the FLAG_IN
a0940 54 45 52 4e 20 62 69 74 20 73 65 74 20 61 6e 64  TERN bit set and
a0950 20 61 72 65 20 74 68 75 73 20 66 6f 72 20 69 6e   are thus for in
a0960 74 65 72 6e 61 6c 0a 2a 2a 20 75 73 65 20 6f 6e  ternal.** use on
a0970 6c 79 20 2a 2f 0a 20 20 7b 20 20 27 54 27 2c 20  ly */.  {  'T', 
a0980 20 30 2c 20 32 2c 20 65 74 54 4f 4b 45 4e 2c 20   0, 2, etTOKEN, 
a0990 20 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20       0,  0 },.  
a09a0 7b 20 20 27 53 27 2c 20 20 30 2c 20 32 2c 20 65  {  'S',  0, 2, e
a09b0 74 53 52 43 4c 49 53 54 2c 20 20 20 20 30 2c 20  tSRCLIST,    0, 
a09c0 20 30 20 7d 2c 0a 20 20 7b 20 20 27 72 27 2c 20   0 },.  {  'r', 
a09d0 31 30 2c 20 33 2c 20 65 74 4f 52 44 49 4e 41 4c  10, 3, etORDINAL
a09e0 2c 20 20 20 20 30 2c 20 20 30 20 7d 2c 0a 7d 3b  ,    0,  0 },.};
a09f0 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54  ../*.** If SQLIT
a0a00 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
a0a10 50 4f 49 4e 54 20 69 73 20 64 65 66 69 6e 65 64  POINT is defined
a0a20 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74  , then none of t
a0a30 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  he floating poin
a0a40 74 0a 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73  t.** conversions
a0a50 20 77 69 6c 6c 20 77 6f 72 6b 2e 0a 2a 2f 0a 23   will work..*/.#
a0a60 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
a0a70 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e  IT_FLOATING_POIN
a0a80 54 0a 2f 2a 0a 2a 2a 20 22 2a 76 61 6c 22 20 69  T./*.** "*val" i
a0a90 73 20 61 20 64 6f 75 62 6c 65 20 73 75 63 68 20  s a double such 
a0aa0 74 68 61 74 20 30 2e 31 20 3c 3d 20 2a 76 61 6c  that 0.1 <= *val
a0ab0 20 3c 20 31 30 2e 30 0a 2a 2a 20 52 65 74 75 72   < 10.0.** Retur
a0ac0 6e 20 74 68 65 20 61 73 63 69 69 20 63 6f 64 65  n the ascii code
a0ad0 20 66 6f 72 20 74 68 65 20 6c 65 61 64 69 6e 67   for the leading
a0ae0 20 64 69 67 69 74 20 6f 66 20 2a 76 61 6c 2c 20   digit of *val, 
a0af0 74 68 65 6e 0a 2a 2a 20 6d 75 6c 74 69 70 6c 79  then.** multiply
a0b00 20 22 2a 76 61 6c 22 20 62 79 20 31 30 2e 30 20   "*val" by 10.0 
a0b10 74 6f 20 72 65 6e 6f 72 6d 61 6c 69 7a 65 2e 0a  to renormalize..
a0b20 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 2a  **.** Example:.*
a0b30 2a 20 20 20 20 20 69 6e 70 75 74 3a 20 20 20 20  *     input:    
a0b40 20 2a 76 61 6c 20 3d 20 33 2e 31 34 31 35 39 0a   *val = 3.14159.
a0b50 2a 2a 20 20 20 20 20 6f 75 74 70 75 74 3a 20 20  **     output:  
a0b60 20 20 2a 76 61 6c 20 3d 20 31 2e 34 31 35 39 20    *val = 1.4159 
a0b70 20 20 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75     function retu
a0b80 72 6e 20 3d 20 27 33 27 0a 2a 2a 0a 2a 2a 20 54  rn = '3'.**.** T
a0b90 68 65 20 63 6f 75 6e 74 65 72 20 2a 63 6e 74 20  he counter *cnt 
a0ba0 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65  is incremented e
a0bb0 61 63 68 20 74 69 6d 65 2e 20 20 41 66 74 65 72  ach time.  After
a0bc0 20 63 6f 75 6e 74 65 72 20 65 78 63 65 65 64 73   counter exceeds
a0bd0 0a 2a 2a 20 31 36 20 28 74 68 65 20 6e 75 6d 62  .** 16 (the numb
a0be0 65 72 20 6f 66 20 73 69 67 6e 69 66 69 63 61 6e  er of significan
a0bf0 74 20 64 69 67 69 74 73 20 69 6e 20 61 20 36 34  t digits in a 64
a0c00 2d 62 69 74 20 66 6c 6f 61 74 29 20 27 30 27 20  -bit float) '0' 
a0c10 69 73 0a 2a 2a 20 61 6c 77 61 79 73 20 72 65 74  is.** always ret
a0c20 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  urned..*/.static
a0c30 20 63 68 61 72 20 65 74 5f 67 65 74 64 69 67 69   char et_getdigi
a0c40 74 28 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50  t(LONGDOUBLE_TYP
a0c50 45 20 2a 76 61 6c 2c 20 69 6e 74 20 2a 63 6e 74  E *val, int *cnt
a0c60 29 7b 0a 20 20 69 6e 74 20 64 69 67 69 74 3b 0a  ){.  int digit;.
a0c70 20 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50    LONGDOUBLE_TYP
a0c80 45 20 64 3b 0a 20 20 69 66 28 20 28 2a 63 6e 74  E d;.  if( (*cnt
a0c90 29 2b 2b 20 3e 3d 20 31 36 20 29 20 72 65 74 75  )++ >= 16 ) retu
a0ca0 72 6e 20 27 30 27 3b 0a 20 20 64 69 67 69 74 20  rn '0';.  digit 
a0cb0 3d 20 28 69 6e 74 29 2a 76 61 6c 3b 0a 20 20 64  = (int)*val;.  d
a0cc0 20 3d 20 64 69 67 69 74 3b 0a 20 20 64 69 67 69   = digit;.  digi
a0cd0 74 20 2b 3d 20 27 30 27 3b 0a 20 20 2a 76 61 6c  t += '0';.  *val
a0ce0 20 3d 20 28 2a 76 61 6c 20 2d 20 64 29 2a 31 30   = (*val - d)*10
a0cf0 2e 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 63 68  .0;.  return (ch
a0d00 61 72 29 64 69 67 69 74 3b 0a 7d 0a 23 65 6e 64  ar)digit;.}.#end
a0d10 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
a0d20 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
a0d30 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e   */../*.** Appen
a0d40 64 20 4e 20 73 70 61 63 65 20 63 68 61 72 61 63  d N space charac
a0d50 74 65 72 73 20 74 6f 20 74 68 65 20 67 69 76 65  ters to the give
a0d60 6e 20 73 74 72 69 6e 67 20 62 75 66 66 65 72 2e  n string buffer.
a0d70 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
a0d80 61 70 70 65 6e 64 53 70 61 63 65 28 53 74 72 41  appendSpace(StrA
a0d90 63 63 75 6d 20 2a 70 41 63 63 75 6d 2c 20 69 6e  ccum *pAccum, in
a0da0 74 20 4e 29 7b 0a 20 20 73 74 61 74 69 63 20 63  t N){.  static c
a0db0 6f 6e 73 74 20 63 68 61 72 20 7a 53 70 61 63 65  onst char zSpace
a0dc0 73 5b 5d 20 3d 20 22 20 20 20 20 20 20 20 20 20  s[] = "         
a0dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a0de0 20 20 20 20 22 3b 0a 20 20 77 68 69 6c 65 28 20      ";.  while( 
a0df0 4e 3e 3d 28 69 6e 74 29 73 69 7a 65 6f 66 28 7a  N>=(int)sizeof(z
a0e00 53 70 61 63 65 73 29 2d 31 20 29 7b 0a 20 20 20  Spaces)-1 ){.   
a0e10 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
a0e20 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a  Append(pAccum, z
a0e30 53 70 61 63 65 73 2c 20 73 69 7a 65 6f 66 28 7a  Spaces, sizeof(z
a0e40 53 70 61 63 65 73 29 2d 31 29 3b 0a 20 20 20 20  Spaces)-1);.    
a0e50 4e 20 2d 3d 20 73 69 7a 65 6f 66 28 7a 53 70 61  N -= sizeof(zSpa
a0e60 63 65 73 29 2d 31 3b 0a 20 20 7d 0a 20 20 69 66  ces)-1;.  }.  if
a0e70 28 20 4e 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c  ( N>0 ){.    sql
a0e80 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65  ite3StrAccumAppe
a0e90 6e 64 28 70 41 63 63 75 6d 2c 20 7a 53 70 61 63  nd(pAccum, zSpac
a0ea0 65 73 2c 20 4e 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  es, N);.  }.}../
a0eb0 2a 0a 2a 2a 20 4f 6e 20 6d 61 63 68 69 6e 65 73  *.** On machines
a0ec0 20 77 69 74 68 20 61 20 73 6d 61 6c 6c 20 73 74   with a small st
a0ed0 61 63 6b 20 73 69 7a 65 2c 20 79 6f 75 20 63 61  ack size, you ca
a0ee0 6e 20 72 65 64 65 66 69 6e 65 20 74 68 65 0a 2a  n redefine the.*
a0ef0 2a 20 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42  * SQLITE_PRINT_B
a0f00 55 46 5f 53 49 5a 45 20 74 6f 20 62 65 20 6c 65  UF_SIZE to be le
a0f10 73 73 20 74 68 61 6e 20 33 35 30 2e 0a 2a 2f 0a  ss than 350..*/.
a0f20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 50  #ifndef SQLITE_P
a0f30 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 0a 23 20  RINT_BUF_SIZE.# 
a0f40 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
a0f50 45 5f 53 4d 41 4c 4c 5f 53 54 41 43 4b 29 0a 23  E_SMALL_STACK).#
a0f60 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
a0f70 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20  _PRINT_BUF_SIZE 
a0f80 35 30 0a 23 20 65 6c 73 65 0a 23 20 20 20 64 65  50.# else.#   de
a0f90 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 49 4e  fine SQLITE_PRIN
a0fa0 54 5f 42 55 46 5f 53 49 5a 45 20 33 35 30 0a 23  T_BUF_SIZE 350.#
a0fb0 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 64   endif.#endif.#d
a0fc0 65 66 69 6e 65 20 65 74 42 55 46 53 49 5a 45 20  efine etBUFSIZE 
a0fd0 53 51 4c 49 54 45 5f 50 52 49 4e 54 5f 42 55 46  SQLITE_PRINT_BUF
a0fe0 5f 53 49 5a 45 20 20 2f 2a 20 53 69 7a 65 20 6f  _SIZE  /* Size o
a0ff0 66 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66  f the output buf
a1000 66 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  fer */../*.** Th
a1010 65 20 72 6f 6f 74 20 70 72 6f 67 72 61 6d 2e 20  e root program. 
a1020 20 41 6c 6c 20 76 61 72 69 61 74 69 6f 6e 73 20   All variations 
a1030 63 61 6c 6c 20 74 68 69 73 20 63 6f 72 65 2e 0a  call this core..
a1040 2a 2a 0a 2a 2a 20 49 4e 50 55 54 53 3a 0a 2a 2a  **.** INPUTS:.**
a1050 20 20 20 66 75 6e 63 20 20 20 54 68 69 73 20 69     func   This i
a1060 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
a1070 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 69 6e 67   function taking
a1080 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73   three arguments
a1090 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 31  .**            1
a10a0 2e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  . A pointer to a
a10b0 6e 79 74 68 69 6e 67 2e 20 20 53 61 6d 65 20 61  nything.  Same a
a10c0 73 20 74 68 65 20 22 61 72 67 22 20 70 61 72 61  s the "arg" para
a10d0 6d 65 74 65 72 2e 0a 2a 2a 20 20 20 20 20 20 20  meter..**       
a10e0 20 20 20 20 20 32 2e 20 41 20 70 6f 69 6e 74 65       2. A pointe
a10f0 72 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66  r to the list of
a1100 20 63 68 61 72 61 63 74 65 72 73 20 74 6f 20 62   characters to b
a1110 65 20 6f 75 74 70 75 74 0a 2a 2a 20 20 20 20 20  e output.**     
a1120 20 20 20 20 20 20 20 20 20 20 28 4e 6f 74 65 2c            (Note,
a1130 20 74 68 69 73 20 6c 69 73 74 20 69 73 20 4e 4f   this list is NO
a1140 54 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74 65  T null terminate
a1150 64 2e 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  d.).**          
a1160 20 20 33 2e 20 41 6e 20 69 6e 74 65 67 65 72 20    3. An integer 
a1170 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63  number of charac
a1180 74 65 72 73 20 74 6f 20 62 65 20 6f 75 74 70 75  ters to be outpu
a1190 74 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  t..**           
a11a0 20 20 20 20 28 4e 6f 74 65 3a 20 54 68 69 73 20      (Note: This 
a11b0 6e 75 6d 62 65 72 20 6d 69 67 68 74 20 62 65 20  number might be 
a11c0 7a 65 72 6f 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 61  zero.).**.**   a
a11d0 72 67 20 20 20 20 54 68 69 73 20 69 73 20 74 68  rg    This is th
a11e0 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 79  e pointer to any
a11f0 74 68 69 6e 67 20 77 68 69 63 68 20 77 69 6c 6c  thing which will
a1200 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68   be passed as th
a1210 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 69  e.**          fi
a1220 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
a1230 22 66 75 6e 63 22 2e 20 20 55 73 65 20 69 74 20  "func".  Use it 
a1240 66 6f 72 20 77 68 61 74 65 76 65 72 20 79 6f 75  for whatever you
a1250 20 6c 69 6b 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 66   like..**.**   f
a1260 6d 74 20 20 20 20 54 68 69 73 20 69 73 20 74 68  mt    This is th
a1270 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e 67 2c  e format string,
a1280 20 61 73 20 69 6e 20 74 68 65 20 75 73 75 61 6c   as in the usual
a1290 20 70 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20   print..**.**   
a12a0 61 70 20 20 20 20 20 54 68 69 73 20 69 73 20 61  ap     This is a
a12b0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 6c 69   pointer to a li
a12c0 73 74 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e  st of arguments.
a12d0 20 20 53 61 6d 65 20 61 73 20 69 6e 0a 2a 2a 20    Same as in.** 
a12e0 20 20 20 20 20 20 20 20 20 76 66 70 72 69 6e 74           vfprint
a12f0 2e 0a 2a 2a 0a 2a 2a 20 4f 55 54 50 55 54 53 3a  ..**.** OUTPUTS:
a1300 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 54 68 65  .**          The
a1310 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73   return value is
a1320 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65   the total numbe
a1330 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  r of characters 
a1340 73 65 6e 74 20 74 6f 0a 2a 2a 20 20 20 20 20 20  sent to.**      
a1350 20 20 20 20 74 68 65 20 66 75 6e 63 74 69 6f 6e      the function
a1360 20 22 66 75 6e 63 22 2e 20 20 52 65 74 75 72 6e   "func".  Return
a1370 73 20 2d 31 20 6f 6e 20 61 20 65 72 72 6f 72 2e  s -1 on a error.
a1380 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74  .**.** Note that
a1390 20 74 68 65 20 6f 72 64 65 72 20 69 6e 20 77 68   the order in wh
a13a0 69 63 68 20 61 75 74 6f 6d 61 74 69 63 20 76 61  ich automatic va
a13b0 72 69 61 62 6c 65 73 20 61 72 65 20 64 65 63 6c  riables are decl
a13c0 61 72 65 64 20 62 65 6c 6f 77 0a 2a 2a 20 73 65  ared below.** se
a13d0 65 6d 73 20 74 6f 20 6d 61 6b 65 20 61 20 62 69  ems to make a bi
a13e0 67 20 64 69 66 66 65 72 65 6e 63 65 20 69 6e 20  g difference in 
a13f0 64 65 74 65 72 6d 69 6e 69 6e 67 20 68 6f 77 20  determining how 
a1400 66 61 73 74 20 74 68 69 73 20 62 65 61 73 74 0a  fast this beast.
a1410 2a 2a 20 77 69 6c 6c 20 72 75 6e 2e 0a 2a 2f 0a  ** will run..*/.
a1420 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
a1430 6f 69 64 20 73 71 6c 69 74 65 33 56 58 50 72 69  oid sqlite3VXPri
a1440 6e 74 66 28 0a 20 20 53 74 72 41 63 63 75 6d 20  ntf(.  StrAccum 
a1450 2a 70 41 63 63 75 6d 2c 20 20 20 20 20 20 20 20  *pAccum,        
a1460 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 63 63            /* Acc
a1470 75 6d 75 6c 61 74 65 20 72 65 73 75 6c 74 73 20  umulate results 
a1480 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 75 73  here */.  int us
a1490 65 45 78 74 65 6e 64 65 64 2c 20 20 20 20 20 20  eExtended,      
a14a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a14b0 41 6c 6c 6f 77 20 65 78 74 65 6e 64 65 64 20 25  Allow extended %
a14c0 2d 63 6f 6e 76 65 72 73 69 6f 6e 73 20 2a 2f 0a  -conversions */.
a14d0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 6d    const char *fm
a14e0 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
a14f0 20 20 20 20 20 2f 2a 20 46 6f 72 6d 61 74 20 73       /* Format s
a1500 74 72 69 6e 67 20 2a 2f 0a 20 20 76 61 5f 6c 69  tring */.  va_li
a1510 73 74 20 61 70 20 20 20 20 20 20 20 20 20 20 20  st ap           
a1520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a1530 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 29 7b   arguments */.){
a1540 0a 20 20 69 6e 74 20 63 3b 20 20 20 20 20 20 20  .  int c;       
a1550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a1560 20 4e 65 78 74 20 63 68 61 72 61 63 74 65 72 20   Next character 
a1570 69 6e 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74  in the format st
a1580 72 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ring */.  char *
a1590 62 75 66 70 74 3b 20 20 20 20 20 20 20 20 20 20  bufpt;          
a15a0 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
a15b0 74 6f 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f  to the conversio
a15c0 6e 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e  n buffer */.  in
a15d0 74 20 70 72 65 63 69 73 69 6f 6e 3b 20 20 20 20  t precision;    
a15e0 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 63           /* Prec
a15f0 69 73 69 6f 6e 20 6f 66 20 74 68 65 20 63 75 72  ision of the cur
a1600 72 65 6e 74 20 66 69 65 6c 64 20 2a 2f 0a 20 20  rent field */.  
a1610 69 6e 74 20 6c 65 6e 67 74 68 3b 20 20 20 20 20  int length;     
a1620 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
a1630 6e 67 74 68 20 6f 66 20 74 68 65 20 66 69 65 6c  ngth of the fiel
a1640 64 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 3b 20  d */.  int idx; 
a1650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1660 20 20 2f 2a 20 41 20 67 65 6e 65 72 61 6c 20 70    /* A general p
a1670 75 72 70 6f 73 65 20 6c 6f 6f 70 20 63 6f 75 6e  urpose loop coun
a1680 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 77 69 64  ter */.  int wid
a1690 74 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  th;             
a16a0 20 20 20 20 2f 2a 20 57 69 64 74 68 20 6f 66 20      /* Width of 
a16b0 74 68 65 20 63 75 72 72 65 6e 74 20 66 69 65 6c  the current fiel
a16c0 64 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  d */.  etByte fl
a16d0 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 3b 20  ag_leftjustify; 
a16e0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 22 2d 22    /* True if "-"
a16f0 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74   flag is present
a1700 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61   */.  etByte fla
a1710 67 5f 70 6c 75 73 73 69 67 6e 3b 20 20 20 20 20  g_plussign;     
a1720 20 2f 2a 20 54 72 75 65 20 69 66 20 22 2b 22 20   /* True if "+" 
a1730 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20  flag is present 
a1740 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67  */.  etByte flag
a1750 5f 62 6c 61 6e 6b 73 69 67 6e 3b 20 20 20 20 20  _blanksign;     
a1760 2f 2a 20 54 72 75 65 20 69 66 20 22 20 22 20 66  /* True if " " f
a1770 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a  lag is present *
a1780 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f  /.  etByte flag_
a1790 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 3b 20 2f  alternateform; /
a17a0 2a 20 54 72 75 65 20 69 66 20 22 23 22 20 66 6c  * True if "#" fl
a17b0 61 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f  ag is present */
a17c0 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 61  .  etByte flag_a
a17d0 6c 74 66 6f 72 6d 32 3b 20 20 20 20 20 20 2f 2a  ltform2;      /*
a17e0 20 54 72 75 65 20 69 66 20 22 21 22 20 66 6c 61   True if "!" fla
a17f0 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a  g is present */.
a1800 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 7a 65    etByte flag_ze
a1810 72 6f 70 61 64 3b 20 20 20 20 20 20 20 2f 2a 20  ropad;       /* 
a1820 54 72 75 65 20 69 66 20 66 69 65 6c 64 20 77 69  True if field wi
a1830 64 74 68 20 63 6f 6e 73 74 61 6e 74 20 73 74 61  dth constant sta
a1840 72 74 73 20 77 69 74 68 20 7a 65 72 6f 20 2a 2f  rts with zero */
a1850 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 6c  .  etByte flag_l
a1860 6f 6e 67 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ong;          /*
a1870 20 54 72 75 65 20 69 66 20 22 6c 22 20 66 6c 61   True if "l" fla
a1880 67 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a  g is present */.
a1890 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 6c 6f    etByte flag_lo
a18a0 6e 67 6c 6f 6e 67 3b 20 20 20 20 20 20 2f 2a 20  nglong;      /* 
a18b0 54 72 75 65 20 69 66 20 74 68 65 20 22 6c 6c 22  True if the "ll"
a18c0 20 66 6c 61 67 20 69 73 20 70 72 65 73 65 6e 74   flag is present
a18d0 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 64 6f 6e   */.  etByte don
a18e0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
a18f0 20 2f 2a 20 4c 6f 6f 70 20 74 65 72 6d 69 6e 61   /* Loop termina
a1900 74 69 6f 6e 20 66 6c 61 67 20 2a 2f 0a 20 20 73  tion flag */.  s
a1910 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 6c 6f 6e  qlite_uint64 lon
a1920 67 76 61 6c 75 65 3b 20 20 20 2f 2a 20 56 61 6c  gvalue;   /* Val
a1930 75 65 20 66 6f 72 20 69 6e 74 65 67 65 72 20 74  ue for integer t
a1940 79 70 65 73 20 2a 2f 0a 20 20 4c 4f 4e 47 44 4f  ypes */.  LONGDO
a1950 55 42 4c 45 5f 54 59 50 45 20 72 65 61 6c 76 61  UBLE_TYPE realva
a1960 6c 75 65 3b 20 2f 2a 20 56 61 6c 75 65 20 66 6f  lue; /* Value fo
a1970 72 20 72 65 61 6c 20 74 79 70 65 73 20 2a 2f 0a  r real types */.
a1980 20 20 63 6f 6e 73 74 20 65 74 5f 69 6e 66 6f 20    const et_info 
a1990 2a 69 6e 66 6f 70 3b 20 20 20 20 20 20 2f 2a 20  *infop;      /* 
a19a0 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61  Pointer to the a
a19b0 70 70 72 6f 70 72 69 61 74 65 20 69 6e 66 6f 20  ppropriate info 
a19c0 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 63  structure */.  c
a19d0 68 61 72 20 62 75 66 5b 65 74 42 55 46 53 49 5a  har buf[etBUFSIZ
a19e0 45 5d 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  E];       /* Con
a19f0 76 65 72 73 69 6f 6e 20 62 75 66 66 65 72 20 2a  version buffer *
a1a00 2f 0a 20 20 63 68 61 72 20 70 72 65 66 69 78 3b  /.  char prefix;
a1a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
a1a20 2a 20 50 72 65 66 69 78 20 63 68 61 72 61 63 74  * Prefix charact
a1a30 65 72 2e 20 20 22 2b 22 20 6f 72 20 22 2d 22 20  er.  "+" or "-" 
a1a40 6f 72 20 22 20 22 20 6f 72 20 27 5c 30 27 2e 20  or " " or '\0'. 
a1a50 2a 2f 0a 20 20 65 74 42 79 74 65 20 78 74 79 70  */.  etByte xtyp
a1a60 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  e = 0;          
a1a70 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 70 61  /* Conversion pa
a1a80 72 61 64 69 67 6d 20 2a 2f 0a 20 20 63 68 61 72  radigm */.  char
a1a90 20 2a 7a 45 78 74 72 61 3b 20 20 20 20 20 20 20   *zExtra;       
a1aa0 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20         /* Extra 
a1ab0 6d 65 6d 6f 72 79 20 75 73 65 64 20 66 6f 72 20  memory used for 
a1ac0 65 74 54 43 4c 45 53 43 41 50 45 20 63 6f 6e 76  etTCLESCAPE conv
a1ad0 65 72 73 69 6f 6e 73 20 2a 2f 0a 23 69 66 6e 64  ersions */.#ifnd
a1ae0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
a1af0 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20  LOATING_POINT.  
a1b00 69 6e 74 20 20 65 78 70 2c 20 65 32 3b 20 20 20  int  exp, e2;   
a1b10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 65 78             /* ex
a1b20 70 6f 6e 65 6e 74 20 6f 66 20 72 65 61 6c 20 6e  ponent of real n
a1b30 75 6d 62 65 72 73 20 2a 2f 0a 20 20 64 6f 75 62  umbers */.  doub
a1b40 6c 65 20 72 6f 75 6e 64 65 72 3b 20 20 20 20 20  le rounder;     
a1b50 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 66         /* Used f
a1b60 6f 72 20 72 6f 75 6e 64 69 6e 67 20 66 6c 6f 61  or rounding floa
a1b70 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65  ting point value
a1b80 73 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  s */.  etByte fl
a1b90 61 67 5f 64 70 3b 20 20 20 20 20 20 20 20 20 20  ag_dp;          
a1ba0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 64 65 63    /* True if dec
a1bb0 69 6d 61 6c 20 70 6f 69 6e 74 20 73 68 6f 75 6c  imal point shoul
a1bc0 64 20 62 65 20 73 68 6f 77 6e 20 2a 2f 0a 20 20  d be shown */.  
a1bd0 65 74 42 79 74 65 20 66 6c 61 67 5f 72 74 7a 3b  etByte flag_rtz;
a1be0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
a1bf0 75 65 20 69 66 20 74 72 61 69 6c 69 6e 67 20 7a  ue if trailing z
a1c00 65 72 6f 73 20 73 68 6f 75 6c 64 20 62 65 20 72  eros should be r
a1c10 65 6d 6f 76 65 64 20 2a 2f 0a 20 20 65 74 42 79  emoved */.  etBy
a1c20 74 65 20 66 6c 61 67 5f 65 78 70 3b 20 20 20 20  te flag_exp;    
a1c30 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
a1c40 6f 20 66 6f 72 63 65 20 64 69 73 70 6c 61 79 20  o force display 
a1c50 6f 66 20 74 68 65 20 65 78 70 6f 6e 65 6e 74 20  of the exponent 
a1c60 2a 2f 0a 20 20 69 6e 74 20 6e 73 64 3b 20 20 20  */.  int nsd;   
a1c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a1c80 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 69 67  /* Number of sig
a1c90 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 73 20  nificant digits 
a1ca0 72 65 74 75 72 6e 65 64 20 2a 2f 0a 23 65 6e 64  returned */.#end
a1cb0 69 66 0a 0a 20 20 6c 65 6e 67 74 68 20 3d 20 30  if..  length = 0
a1cc0 3b 0a 20 20 62 75 66 70 74 20 3d 20 30 3b 0a 20  ;.  bufpt = 0;. 
a1cd0 20 66 6f 72 28 3b 20 28 63 3d 28 2a 66 6d 74 29   for(; (c=(*fmt)
a1ce0 29 21 3d 30 3b 20 2b 2b 66 6d 74 29 7b 0a 20 20  )!=0; ++fmt){.  
a1cf0 20 20 69 66 28 20 63 21 3d 27 25 27 20 29 7b 0a    if( c!='%' ){.
a1d00 20 20 20 20 20 20 69 6e 74 20 61 6d 74 3b 0a 20        int amt;. 
a1d10 20 20 20 20 20 62 75 66 70 74 20 3d 20 28 63 68       bufpt = (ch
a1d20 61 72 20 2a 29 66 6d 74 3b 0a 20 20 20 20 20 20  ar *)fmt;.      
a1d30 61 6d 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 77  amt = 1;.      w
a1d40 68 69 6c 65 28 20 28 63 3d 28 2a 2b 2b 66 6d 74  hile( (c=(*++fmt
a1d50 29 29 21 3d 27 25 27 20 26 26 20 63 21 3d 30 20  ))!='%' && c!=0 
a1d60 29 20 61 6d 74 2b 2b 3b 0a 20 20 20 20 20 20 73  ) amt++;.      s
a1d70 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70  qlite3StrAccumAp
a1d80 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 62 75 66  pend(pAccum, buf
a1d90 70 74 2c 20 61 6d 74 29 3b 0a 20 20 20 20 20 20  pt, amt);.      
a1da0 69 66 28 20 63 3d 3d 30 20 29 20 62 72 65 61 6b  if( c==0 ) break
a1db0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
a1dc0 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 3d 3d 30 20  (c=(*++fmt))==0 
a1dd0 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
a1de0 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70  StrAccumAppend(p
a1df0 41 63 63 75 6d 2c 20 22 25 22 2c 20 31 29 3b 0a  Accum, "%", 1);.
a1e00 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
a1e10 20 7d 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 6f   }.    /* Find o
a1e20 75 74 20 77 68 61 74 20 66 6c 61 67 73 20 61 72  ut what flags ar
a1e30 65 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 20  e present */.   
a1e40 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66   flag_leftjustif
a1e50 79 20 3d 20 66 6c 61 67 5f 70 6c 75 73 73 69 67  y = flag_plussig
a1e60 6e 20 3d 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69  n = flag_blanksi
a1e70 67 6e 20 3d 20 0a 20 20 20 20 20 66 6c 61 67 5f  gn = .     flag_
a1e80 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20 3d 20  alternateform = 
a1e90 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 3d 20  flag_altform2 = 
a1ea0 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 3d 20 30  flag_zeropad = 0
a1eb0 3b 0a 20 20 20 20 64 6f 6e 65 20 3d 20 30 3b 0a  ;.    done = 0;.
a1ec0 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 73 77      do{.      sw
a1ed0 69 74 63 68 28 20 63 20 29 7b 0a 20 20 20 20 20  itch( c ){.     
a1ee0 20 20 20 63 61 73 65 20 27 2d 27 3a 20 20 20 66     case '-':   f
a1ef0 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20  lag_leftjustify 
a1f00 3d 20 31 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a  = 1;     break;.
a1f10 20 20 20 20 20 20 20 20 63 61 73 65 20 27 2b 27          case '+'
a1f20 3a 20 20 20 66 6c 61 67 5f 70 6c 75 73 73 69 67  :   flag_plussig
a1f30 6e 20 3d 20 31 3b 20 20 20 20 20 20 20 20 62 72  n = 1;        br
a1f40 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
a1f50 65 20 27 20 27 3a 20 20 20 66 6c 61 67 5f 62 6c  e ' ':   flag_bl
a1f60 61 6e 6b 73 69 67 6e 20 3d 20 31 3b 20 20 20 20  anksign = 1;    
a1f70 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
a1f80 20 20 63 61 73 65 20 27 23 27 3a 20 20 20 66 6c    case '#':   fl
a1f90 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d  ag_alternateform
a1fa0 20 3d 20 31 3b 20 20 20 62 72 65 61 6b 3b 0a 20   = 1;   break;. 
a1fb0 20 20 20 20 20 20 20 63 61 73 65 20 27 21 27 3a         case '!':
a1fc0 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32     flag_altform2
a1fd0 20 3d 20 31 3b 20 20 20 20 20 20 20 20 62 72 65   = 1;        bre
a1fe0 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
a1ff0 20 27 30 27 3a 20 20 20 66 6c 61 67 5f 7a 65 72   '0':   flag_zer
a2000 6f 70 61 64 20 3d 20 31 3b 20 20 20 20 20 20 20  opad = 1;       
a2010 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
a2020 20 64 65 66 61 75 6c 74 3a 20 20 20 20 64 6f 6e   default:    don
a2030 65 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20  e = 1;          
a2040 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
a2050 20 20 20 20 7d 0a 20 20 20 20 7d 77 68 69 6c 65      }.    }while
a2060 28 20 21 64 6f 6e 65 20 26 26 20 28 63 3d 28 2a  ( !done && (c=(*
a2070 2b 2b 66 6d 74 29 29 21 3d 30 20 29 3b 0a 20 20  ++fmt))!=0 );.  
a2080 20 20 2f 2a 20 47 65 74 20 74 68 65 20 66 69 65    /* Get the fie
a2090 6c 64 20 77 69 64 74 68 20 2a 2f 0a 20 20 20 20  ld width */.    
a20a0 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 69  width = 0;.    i
a20b0 66 28 20 63 3d 3d 27 2a 27 20 29 7b 0a 20 20 20  f( c=='*' ){.   
a20c0 20 20 20 77 69 64 74 68 20 3d 20 76 61 5f 61 72     width = va_ar
a20d0 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20  g(ap,int);.     
a20e0 20 69 66 28 20 77 69 64 74 68 3c 30 20 29 7b 0a   if( width<0 ){.
a20f0 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 65 66          flag_lef
a2100 74 6a 75 73 74 69 66 79 20 3d 20 31 3b 0a 20 20  tjustify = 1;.  
a2110 20 20 20 20 20 20 77 69 64 74 68 20 3d 20 2d 77        width = -w
a2120 69 64 74 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20  idth;.      }.  
a2130 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a      c = *++fmt;.
a2140 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
a2150 20 77 68 69 6c 65 28 20 63 3e 3d 27 30 27 20 26   while( c>='0' &
a2160 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20 20 20  & c<='9' ){.    
a2170 20 20 20 20 77 69 64 74 68 20 3d 20 77 69 64 74      width = widt
a2180 68 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b 0a  h*10 + c - '0';.
a2190 20 20 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66          c = *++f
a21a0 6d 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  mt;.      }.    
a21b0 7d 0a 20 20 20 20 69 66 28 20 77 69 64 74 68 20  }.    if( width 
a21c0 3e 20 65 74 42 55 46 53 49 5a 45 2d 31 30 20 29  > etBUFSIZE-10 )
a21d0 7b 0a 20 20 20 20 20 20 77 69 64 74 68 20 3d 20  {.      width = 
a21e0 65 74 42 55 46 53 49 5a 45 2d 31 30 3b 0a 20 20  etBUFSIZE-10;.  
a21f0 20 20 7d 0a 20 20 20 20 2f 2a 20 47 65 74 20 74    }.    /* Get t
a2200 68 65 20 70 72 65 63 69 73 69 6f 6e 20 2a 2f 0a  he precision */.
a2210 20 20 20 20 69 66 28 20 63 3d 3d 27 2e 27 20 29      if( c=='.' )
a2220 7b 0a 20 20 20 20 20 20 70 72 65 63 69 73 69 6f  {.      precisio
a2230 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 20 3d  n = 0;.      c =
a2240 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 69   *++fmt;.      i
a2250 66 28 20 63 3d 3d 27 2a 27 20 29 7b 0a 20 20 20  f( c=='*' ){.   
a2260 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d       precision =
a2270 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b   va_arg(ap,int);
a2280 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65  .        if( pre
a2290 63 69 73 69 6f 6e 3c 30 20 29 20 70 72 65 63 69  cision<0 ) preci
a22a0 73 69 6f 6e 20 3d 20 2d 70 72 65 63 69 73 69 6f  sion = -precisio
a22b0 6e 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 2a  n;.        c = *
a22c0 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 7d 65 6c  ++fmt;.      }el
a22d0 73 65 7b 0a 20 20 20 20 20 20 20 20 77 68 69 6c  se{.        whil
a22e0 65 28 20 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d  e( c>='0' && c<=
a22f0 27 39 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20  '9' ){.         
a2300 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 70 72 65   precision = pre
a2310 63 69 73 69 6f 6e 2a 31 30 20 2b 20 63 20 2d 20  cision*10 + c - 
a2320 27 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 63  '0';.          c
a2330 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20   = *++fmt;.     
a2340 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
a2350 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 72   }else{.      pr
a2360 65 63 69 73 69 6f 6e 20 3d 20 2d 31 3b 0a 20 20  ecision = -1;.  
a2370 20 20 7d 0a 20 20 20 20 2f 2a 20 47 65 74 20 74    }.    /* Get t
a2380 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 74 79  he conversion ty
a2390 70 65 20 6d 6f 64 69 66 69 65 72 20 2a 2f 0a 20  pe modifier */. 
a23a0 20 20 20 69 66 28 20 63 3d 3d 27 6c 27 20 29 7b     if( c=='l' ){
a23b0 0a 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67  .      flag_long
a23c0 20 3d 20 31 3b 0a 20 20 20 20 20 20 63 20 3d 20   = 1;.      c = 
a23d0 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20 20 69 66  *++fmt;.      if
a23e0 28 20 63 3d 3d 27 6c 27 20 29 7b 0a 20 20 20 20  ( c=='l' ){.    
a23f0 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e      flag_longlon
a2400 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 63  g = 1;.        c
a2410 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20   = *++fmt;.     
a2420 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
a2430 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20  flag_longlong = 
a2440 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
a2450 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 6c 61 67  else{.      flag
a2460 5f 6c 6f 6e 67 20 3d 20 66 6c 61 67 5f 6c 6f 6e  _long = flag_lon
a2470 67 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 20 20 7d  glong = 0;.    }
a2480 0a 20 20 20 20 2f 2a 20 46 65 74 63 68 20 74 68  .    /* Fetch th
a2490 65 20 69 6e 66 6f 20 65 6e 74 72 79 20 66 6f 72  e info entry for
a24a0 20 74 68 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20   the field */.  
a24b0 20 20 69 6e 66 6f 70 20 3d 20 26 66 6d 74 69 6e    infop = &fmtin
a24c0 66 6f 5b 30 5d 3b 0a 20 20 20 20 78 74 79 70 65  fo[0];.    xtype
a24d0 20 3d 20 65 74 49 4e 56 41 4c 49 44 3b 0a 20 20   = etINVALID;.  
a24e0 20 20 66 6f 72 28 69 64 78 3d 30 3b 20 69 64 78    for(idx=0; idx
a24f0 3c 41 72 72 61 79 53 69 7a 65 28 66 6d 74 69 6e  <ArraySize(fmtin
a2500 66 6f 29 3b 20 69 64 78 2b 2b 29 7b 0a 20 20 20  fo); idx++){.   
a2510 20 20 20 69 66 28 20 63 3d 3d 66 6d 74 69 6e 66     if( c==fmtinf
a2520 6f 5b 69 64 78 5d 2e 66 6d 74 74 79 70 65 20 29  o[idx].fmttype )
a2530 7b 0a 20 20 20 20 20 20 20 20 69 6e 66 6f 70 20  {.        infop 
a2540 3d 20 26 66 6d 74 69 6e 66 6f 5b 69 64 78 5d 3b  = &fmtinfo[idx];
a2550 0a 20 20 20 20 20 20 20 20 69 66 28 20 75 73 65  .        if( use
a2560 45 78 74 65 6e 64 65 64 20 7c 7c 20 28 69 6e 66  Extended || (inf
a2570 6f 70 2d 3e 66 6c 61 67 73 20 26 20 46 4c 41 47  op->flags & FLAG
a2580 5f 49 4e 54 45 52 4e 29 3d 3d 30 20 29 7b 0a 20  _INTERN)==0 ){. 
a2590 20 20 20 20 20 20 20 20 20 78 74 79 70 65 20 3d           xtype =
a25a0 20 69 6e 66 6f 70 2d 3e 74 79 70 65 3b 0a 20 20   infop->type;.  
a25b0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
a25c0 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
a25d0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a25e0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
a25f0 20 20 20 20 7d 0a 20 20 20 20 7a 45 78 74 72 61      }.    zExtra
a2600 20 3d 20 30 3b 0a 0a 0a 20 20 20 20 2f 2a 20 4c   = 0;...    /* L
a2610 69 6d 69 74 20 74 68 65 20 70 72 65 63 69 73 69  imit the precisi
a2620 6f 6e 20 74 6f 20 70 72 65 76 65 6e 74 20 6f 76  on to prevent ov
a2630 65 72 66 6c 6f 77 69 6e 67 20 62 75 66 5b 5d 20  erflowing buf[] 
a2640 64 75 72 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f  during conversio
a2650 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 72 65  n */.    if( pre
a2660 63 69 73 69 6f 6e 3e 65 74 42 55 46 53 49 5a 45  cision>etBUFSIZE
a2670 2d 34 30 20 26 26 20 28 69 6e 66 6f 70 2d 3e 66  -40 && (infop->f
a2680 6c 61 67 73 20 26 20 46 4c 41 47 5f 53 54 52 49  lags & FLAG_STRI
a2690 4e 47 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  NG)==0 ){.      
a26a0 70 72 65 63 69 73 69 6f 6e 20 3d 20 65 74 42 55  precision = etBU
a26b0 46 53 49 5a 45 2d 34 30 3b 0a 20 20 20 20 7d 0a  FSIZE-40;.    }.
a26c0 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 41  .    /*.    ** A
a26d0 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 76 61  t this point, va
a26e0 72 69 61 62 6c 65 73 20 61 72 65 20 69 6e 69 74  riables are init
a26f0 69 61 6c 69 7a 65 64 20 61 73 20 66 6f 6c 6c 6f  ialized as follo
a2700 77 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ws:.    **.    *
a2710 2a 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61  *   flag_alterna
a2720 74 65 66 6f 72 6d 20 20 20 20 20 20 20 20 20 20  teform          
a2730 54 52 55 45 20 69 66 20 61 20 27 23 27 20 69 73  TRUE if a '#' is
a2740 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a   present..    **
a2750 20 20 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32     flag_altform2
a2760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54                 T
a2770 52 55 45 20 69 66 20 61 20 27 21 27 20 69 73 20  RUE if a '!' is 
a2780 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20  present..    ** 
a2790 20 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20    flag_plussign 
a27a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52                TR
a27b0 55 45 20 69 66 20 61 20 27 2b 27 20 69 73 20 70  UE if a '+' is p
a27c0 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20  resent..    **  
a27d0 20 66 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66   flag_leftjustif
a27e0 79 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55  y            TRU
a27f0 45 20 69 66 20 61 20 27 2d 27 20 69 73 20 70 72  E if a '-' is pr
a2800 65 73 65 6e 74 20 6f 72 20 69 66 20 74 68 65 0a  esent or if the.
a2810 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
a2820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2830 20 20 20 20 20 66 69 65 6c 64 20 77 69 64 74 68       field width
a2840 20 77 61 73 20 6e 65 67 61 74 69 76 65 2e 0a 20   was negative.. 
a2850 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f 7a 65 72     **   flag_zer
a2860 6f 70 61 64 20 20 20 20 20 20 20 20 20 20 20 20  opad            
a2870 20 20 20 20 54 52 55 45 20 69 66 20 74 68 65 20      TRUE if the 
a2880 77 69 64 74 68 20 62 65 67 61 6e 20 77 69 74 68  width began with
a2890 20 30 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61   0..    **   fla
a28a0 67 5f 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 20  g_long          
a28b0 20 20 20 20 20 20 20 20 20 54 52 55 45 20 69 66           TRUE if
a28c0 20 74 68 65 20 6c 65 74 74 65 72 20 27 6c 27 20   the letter 'l' 
a28d0 28 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a 20  (ell) prefixed. 
a28e0 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20     **           
a28f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2900 20 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73 69      the conversi
a2910 6f 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20  on character..  
a2920 20 20 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f 6e 67    **   flag_long
a2930 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 20 20 20  long            
a2940 20 20 20 54 52 55 45 20 69 66 20 74 68 65 20 6c     TRUE if the l
a2950 65 74 74 65 72 20 27 6c 6c 27 20 28 65 6c 6c 20  etter 'll' (ell 
a2960 65 6c 6c 29 20 70 72 65 66 69 78 65 64 0a 20 20  ell) prefixed.  
a2970 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20    **            
a2980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2990 20 20 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f     the conversio
a29a0 6e 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20  n character..   
a29b0 20 2a 2a 20 20 20 66 6c 61 67 5f 62 6c 61 6e 6b   **   flag_blank
a29c0 73 69 67 6e 20 20 20 20 20 20 20 20 20 20 20 20  sign            
a29d0 20 20 54 52 55 45 20 69 66 20 61 20 27 20 27 20    TRUE if a ' ' 
a29e0 69 73 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20  is present..    
a29f0 2a 2a 20 20 20 77 69 64 74 68 20 20 20 20 20 20  **   width      
a2a00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2a10 20 54 68 65 20 73 70 65 63 69 66 69 65 64 20 66   The specified f
a2a20 69 65 6c 64 20 77 69 64 74 68 2e 20 20 54 68 69  ield width.  Thi
a2a30 73 20 69 73 0a 20 20 20 20 2a 2a 20 20 20 20 20  s is.    **     
a2a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2a50 20 20 20 20 20 20 20 20 20 20 61 6c 77 61 79 73            always
a2a60 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 20   non-negative.  
a2a70 5a 65 72 6f 20 69 73 20 74 68 65 20 64 65 66 61  Zero is the defa
a2a80 75 6c 74 2e 0a 20 20 20 20 2a 2a 20 20 20 70 72  ult..    **   pr
a2a90 65 63 69 73 69 6f 6e 20 20 20 20 20 20 20 20 20  ecision         
a2aa0 20 20 20 20 20 20 20 20 20 20 54 68 65 20 73 70            The sp
a2ab0 65 63 69 66 69 65 64 20 70 72 65 63 69 73 69 6f  ecified precisio
a2ac0 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a  n.  The default.
a2ad0 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
a2ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2af0 20 20 20 20 20 69 73 20 2d 31 2e 0a 20 20 20 20       is -1..    
a2b00 2a 2a 20 20 20 78 74 79 70 65 20 20 20 20 20 20  **   xtype      
a2b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2b20 20 54 68 65 20 63 6c 61 73 73 20 6f 66 20 74 68   The class of th
a2b30 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 20 20  e conversion..  
a2b40 20 20 2a 2a 20 20 20 69 6e 66 6f 70 20 20 20 20    **   infop    
a2b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2b60 20 20 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68     Pointer to th
a2b70 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 69 6e  e appropriate in
a2b80 66 6f 20 73 74 72 75 63 74 2e 0a 20 20 20 20 2a  fo struct..    *
a2b90 2f 0a 20 20 20 20 73 77 69 74 63 68 28 20 78 74  /.    switch( xt
a2ba0 79 70 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73  ype ){.      cas
a2bb0 65 20 65 74 50 4f 49 4e 54 45 52 3a 0a 20 20 20  e etPOINTER:.   
a2bc0 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f       flag_longlo
a2bd0 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 61 72  ng = sizeof(char
a2be0 2a 29 3d 3d 73 69 7a 65 6f 66 28 69 36 34 29 3b  *)==sizeof(i64);
a2bf0 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f  .        flag_lo
a2c00 6e 67 20 3d 20 73 69 7a 65 6f 66 28 63 68 61 72  ng = sizeof(char
a2c10 2a 29 3d 3d 73 69 7a 65 6f 66 28 6c 6f 6e 67 20  *)==sizeof(long 
a2c20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a  int);.        /*
a2c30 20 46 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e   Fall through in
a2c40 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73 65  to the next case
a2c50 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 65   */.      case e
a2c60 74 4f 52 44 49 4e 41 4c 3a 0a 20 20 20 20 20 20  tORDINAL:.      
a2c70 63 61 73 65 20 65 74 52 41 44 49 58 3a 0a 20 20  case etRADIX:.  
a2c80 20 20 20 20 20 20 69 66 28 20 69 6e 66 6f 70 2d        if( infop-
a2c90 3e 66 6c 61 67 73 20 26 20 46 4c 41 47 5f 53 49  >flags & FLAG_SI
a2ca0 47 4e 45 44 20 29 7b 0a 20 20 20 20 20 20 20 20  GNED ){.        
a2cb0 20 20 69 36 34 20 76 3b 0a 20 20 20 20 20 20 20    i64 v;.       
a2cc0 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67     if( flag_long
a2cd0 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 20 20  long ){.        
a2ce0 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67 28 61      v = va_arg(a
a2cf0 70 2c 69 36 34 29 3b 0a 20 20 20 20 20 20 20 20  p,i64);.        
a2d00 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67    }else if( flag
a2d10 5f 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 20  _long ){.       
a2d20 20 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67 28       v = va_arg(
a2d30 61 70 2c 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20  ap,long int);.  
a2d40 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
a2d50 20 20 20 20 20 20 20 20 20 20 20 76 20 3d 20 76             v = v
a2d60 61 5f 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20  a_arg(ap,int);. 
a2d70 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
a2d80 20 20 20 20 20 69 66 28 20 76 3c 30 20 29 7b 0a       if( v<0 ){.
a2d90 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67              long
a2da0 76 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20 20 20  value = -v;.    
a2db0 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d          prefix =
a2dc0 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 20 20   '-';.          
a2dd0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
a2de0 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76     longvalue = v
a2df0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ;.            if
a2e00 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20  ( flag_plussign 
a2e10 29 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20  )        prefix 
a2e20 3d 20 27 2b 27 3b 0a 20 20 20 20 20 20 20 20 20  = '+';.         
a2e30 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67     else if( flag
a2e40 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20 70 72  _blanksign )  pr
a2e50 65 66 69 78 20 3d 20 27 20 27 3b 0a 20 20 20 20  efix = ' ';.    
a2e60 20 20 20 20 20 20 20 20 65 6c 73 65 20 20 20 20          else    
a2e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2e80 20 20 20 70 72 65 66 69 78 20 3d 20 30 3b 0a 20     prefix = 0;. 
a2e90 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
a2ea0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a2eb0 20 20 20 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e      if( flag_lon
a2ec0 67 6c 6f 6e 67 20 29 7b 0a 20 20 20 20 20 20 20  glong ){.       
a2ed0 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d       longvalue =
a2ee0 20 76 61 5f 61 72 67 28 61 70 2c 75 36 34 29 3b   va_arg(ap,u64);
a2ef0 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
a2f00 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 20 29   if( flag_long )
a2f10 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f  {.            lo
a2f20 6e 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 67  ngvalue = va_arg
a2f30 28 61 70 2c 75 6e 73 69 67 6e 65 64 20 6c 6f 6e  (ap,unsigned lon
a2f40 67 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20  g int);.        
a2f50 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
a2f60 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d       longvalue =
a2f70 20 76 61 5f 61 72 67 28 61 70 2c 75 6e 73 69 67   va_arg(ap,unsig
a2f80 6e 65 64 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ned int);.      
a2f90 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
a2fa0 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 20  prefix = 0;.    
a2fb0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
a2fc0 28 20 6c 6f 6e 67 76 61 6c 75 65 3d 3d 30 20 29  ( longvalue==0 )
a2fd0 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66   flag_alternatef
a2fe0 6f 72 6d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  orm = 0;.       
a2ff0 20 69 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70 61   if( flag_zeropa
a3000 64 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3c 77  d && precision<w
a3010 69 64 74 68 2d 28 70 72 65 66 69 78 21 3d 30 29  idth-(prefix!=0)
a3020 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72   ){.          pr
a3030 65 63 69 73 69 6f 6e 20 3d 20 77 69 64 74 68 2d  ecision = width-
a3040 28 70 72 65 66 69 78 21 3d 30 29 3b 0a 20 20 20  (prefix!=0);.   
a3050 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62       }.        b
a3060 75 66 70 74 20 3d 20 26 62 75 66 5b 65 74 42 55  ufpt = &buf[etBU
a3070 46 53 49 5a 45 2d 31 5d 3b 0a 20 20 20 20 20 20  FSIZE-1];.      
a3080 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 4f    if( xtype==etO
a3090 52 44 49 4e 41 4c 20 29 7b 0a 20 20 20 20 20 20  RDINAL ){.      
a30a0 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
a30b0 20 63 68 61 72 20 7a 4f 72 64 5b 5d 20 3d 20 22   char zOrd[] = "
a30c0 74 68 73 74 6e 64 72 64 22 3b 0a 20 20 20 20 20  thstndrd";.     
a30d0 20 20 20 20 20 69 6e 74 20 78 20 3d 20 28 69 6e       int x = (in
a30e0 74 29 28 6c 6f 6e 67 76 61 6c 75 65 20 25 20 31  t)(longvalue % 1
a30f0 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  0);.          if
a3100 28 20 78 3e 3d 34 20 7c 7c 20 28 6c 6f 6e 67 76  ( x>=4 || (longv
a3110 61 6c 75 65 2f 31 30 29 25 31 30 3d 3d 31 20 29  alue/10)%10==1 )
a3120 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 20  {.            x 
a3130 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  = 0;.          }
a3140 0a 20 20 20 20 20 20 20 20 20 20 62 75 66 5b 65  .          buf[e
a3150 74 42 55 46 53 49 5a 45 2d 33 5d 20 3d 20 7a 4f  tBUFSIZE-3] = zO
a3160 72 64 5b 78 2a 32 5d 3b 0a 20 20 20 20 20 20 20  rd[x*2];.       
a3170 20 20 20 62 75 66 5b 65 74 42 55 46 53 49 5a 45     buf[etBUFSIZE
a3180 2d 32 5d 20 3d 20 7a 4f 72 64 5b 78 2a 32 2b 31  -2] = zOrd[x*2+1
a3190 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 62 75 66  ];.          buf
a31a0 70 74 20 2d 3d 20 32 3b 0a 20 20 20 20 20 20 20  pt -= 2;.       
a31b0 20 7d 0a 20 20 20 20 20 20 20 20 7b 0a 20 20 20   }.        {.   
a31c0 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 20         register 
a31d0 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 73 65 74  const char *cset
a31e0 3b 20 20 20 20 20 20 2f 2a 20 55 73 65 20 72 65  ;      /* Use re
a31f0 67 69 73 74 65 72 73 20 66 6f 72 20 73 70 65 65  gisters for spee
a3200 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72  d */.          r
a3210 65 67 69 73 74 65 72 20 69 6e 74 20 62 61 73 65  egister int base
a3220 3b 0a 20 20 20 20 20 20 20 20 20 20 63 73 65 74  ;.          cset
a3230 20 3d 20 26 61 44 69 67 69 74 73 5b 69 6e 66 6f   = &aDigits[info
a3240 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0a 20 20 20  p->charset];.   
a3250 20 20 20 20 20 20 20 62 61 73 65 20 3d 20 69 6e         base = in
a3260 66 6f 70 2d 3e 62 61 73 65 3b 0a 20 20 20 20 20  fop->base;.     
a3270 20 20 20 20 20 64 6f 7b 20 20 20 20 20 20 20 20       do{        
a3280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a3290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a32a0 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 6f     /* Convert to
a32b0 20 61 73 63 69 69 20 2a 2f 0a 20 20 20 20 20 20   ascii */.      
a32c0 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 29        *(--bufpt)
a32d0 20 3d 20 63 73 65 74 5b 6c 6f 6e 67 76 61 6c 75   = cset[longvalu
a32e0 65 25 62 61 73 65 5d 3b 0a 20 20 20 20 20 20 20  e%base];.       
a32f0 20 20 20 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d       longvalue =
a3300 20 6c 6f 6e 67 76 61 6c 75 65 2f 62 61 73 65 3b   longvalue/base;
a3310 0a 20 20 20 20 20 20 20 20 20 20 7d 77 68 69 6c  .          }whil
a3320 65 28 20 6c 6f 6e 67 76 61 6c 75 65 3e 30 20 29  e( longvalue>0 )
a3330 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
a3340 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e      length = (in
a3350 74 29 28 26 62 75 66 5b 65 74 42 55 46 53 49 5a  t)(&buf[etBUFSIZ
a3360 45 2d 31 5d 2d 62 75 66 70 74 29 3b 0a 20 20 20  E-1]-bufpt);.   
a3370 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72 65       for(idx=pre
a3380 63 69 73 69 6f 6e 2d 6c 65 6e 67 74 68 3b 20 69  cision-length; i
a3390 64 78 3e 30 3b 20 69 64 78 2d 2d 29 7b 0a 20 20  dx>0; idx--){.  
a33a0 20 20 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70          *(--bufp
a33b0 74 29 20 3d 20 27 30 27 3b 20 20 20 20 20 20 20  t) = '0';       
a33c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a33d0 20 20 20 20 20 20 2f 2a 20 5a 65 72 6f 20 70 61        /* Zero pa
a33e0 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20  d */.        }. 
a33f0 20 20 20 20 20 20 20 69 66 28 20 70 72 65 66 69         if( prefi
a3400 78 20 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d  x ) *(--bufpt) =
a3410 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 20   prefix;        
a3420 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 73 69         /* Add si
a3430 67 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66  gn */.        if
a3440 28 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65  ( flag_alternate
a3450 66 6f 72 6d 20 26 26 20 69 6e 66 6f 70 2d 3e 70  form && infop->p
a3460 72 65 66 69 78 20 29 7b 20 20 20 20 20 20 2f 2a  refix ){      /*
a3470 20 41 64 64 20 22 30 22 20 6f 72 20 22 30 78 22   Add "0" or "0x"
a3480 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 63 6f   */.          co
a3490 6e 73 74 20 63 68 61 72 20 2a 70 72 65 3b 0a 20  nst char *pre;. 
a34a0 20 20 20 20 20 20 20 20 20 63 68 61 72 20 78 3b           char x;
a34b0 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 20 3d  .          pre =
a34c0 20 26 61 50 72 65 66 69 78 5b 69 6e 66 6f 70 2d   &aPrefix[infop-
a34d0 3e 70 72 65 66 69 78 5d 3b 0a 20 20 20 20 20 20  >prefix];.      
a34e0 20 20 20 20 66 6f 72 28 3b 20 28 78 3d 28 2a 70      for(; (x=(*p
a34f0 72 65 29 29 21 3d 30 3b 20 70 72 65 2b 2b 29 20  re))!=0; pre++) 
a3500 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 78 3b 0a  *(--bufpt) = x;.
a3510 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a3520 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e 74 29    length = (int)
a3530 28 26 62 75 66 5b 65 74 42 55 46 53 49 5a 45 2d  (&buf[etBUFSIZE-
a3540 31 5d 2d 62 75 66 70 74 29 3b 0a 20 20 20 20 20  1]-bufpt);.     
a3550 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
a3560 63 61 73 65 20 65 74 46 4c 4f 41 54 3a 0a 20 20  case etFLOAT:.  
a3570 20 20 20 20 63 61 73 65 20 65 74 45 58 50 3a 0a      case etEXP:.
a3580 20 20 20 20 20 20 63 61 73 65 20 65 74 47 45 4e        case etGEN
a3590 45 52 49 43 3a 0a 20 20 20 20 20 20 20 20 72 65  ERIC:.        re
a35a0 61 6c 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 67  alvalue = va_arg
a35b0 28 61 70 2c 64 6f 75 62 6c 65 29 3b 0a 23 69 66  (ap,double);.#if
a35c0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
a35d0 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
a35e0 20 20 20 20 20 20 20 20 69 66 28 20 70 72 65 63          if( prec
a35f0 69 73 69 6f 6e 3c 30 20 29 20 70 72 65 63 69 73  ision<0 ) precis
a3600 69 6f 6e 20 3d 20 36 3b 20 20 20 20 20 20 20 20  ion = 6;        
a3610 20 2f 2a 20 53 65 74 20 64 65 66 61 75 6c 74 20   /* Set default 
a3620 70 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 20 20 20  precision */.   
a3630 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69       if( precisi
a3640 6f 6e 3e 65 74 42 55 46 53 49 5a 45 2f 32 2d 31  on>etBUFSIZE/2-1
a3650 30 20 29 20 70 72 65 63 69 73 69 6f 6e 20 3d 20  0 ) precision = 
a3660 65 74 42 55 46 53 49 5a 45 2f 32 2d 31 30 3b 0a  etBUFSIZE/2-10;.
a3670 20 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c          if( real
a3680 76 61 6c 75 65 3c 30 2e 30 20 29 7b 0a 20 20 20  value<0.0 ){.   
a3690 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65         realvalue
a36a0 20 3d 20 2d 72 65 61 6c 76 61 6c 75 65 3b 0a 20   = -realvalue;. 
a36b0 20 20 20 20 20 20 20 20 20 70 72 65 66 69 78 20           prefix 
a36c0 3d 20 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 7d  = '-';.        }
a36d0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
a36e0 69 66 28 20 66 6c 61 67 5f 70 6c 75 73 73 69 67  if( flag_plussig
a36f0 6e 20 29 20 20 20 20 20 20 20 20 20 20 70 72 65  n )          pre
a3700 66 69 78 20 3d 20 27 2b 27 3b 0a 20 20 20 20 20  fix = '+';.     
a3710 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c       else if( fl
a3720 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 29 20 20  ag_blanksign )  
a3730 20 20 70 72 65 66 69 78 20 3d 20 27 20 27 3b 0a    prefix = ' ';.
a3740 20 20 20 20 20 20 20 20 20 20 65 6c 73 65 20 20            else  
a3750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a3760 20 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20         prefix = 
a3770 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
a3780 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d       if( xtype==
a3790 65 74 47 45 4e 45 52 49 43 20 26 26 20 70 72 65  etGENERIC && pre
a37a0 63 69 73 69 6f 6e 3e 30 20 29 20 70 72 65 63 69  cision>0 ) preci
a37b0 73 69 6f 6e 2d 2d 3b 0a 23 69 66 20 30 0a 20 20  sion--;.#if 0.  
a37c0 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e 64 69 6e        /* Roundin
a37d0 67 20 77 6f 72 6b 73 20 6c 69 6b 65 20 42 53 44  g works like BSD
a37e0 20 77 68 65 6e 20 74 68 65 20 63 6f 6e 73 74 61   when the consta
a37f0 6e 74 20 30 2e 34 39 39 39 20 69 73 20 75 73 65  nt 0.4999 is use
a3800 64 2e 20 20 57 69 65 72 64 21 20 2a 2f 0a 20 20  d.  Wierd! */.  
a3810 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d 70 72        for(idx=pr
a3820 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64 65 72  ecision, rounder
a3830 3d 30 2e 34 39 39 39 3b 20 69 64 78 3e 30 3b 20  =0.4999; idx>0; 
a3840 69 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d  idx--, rounder*=
a3850 30 2e 31 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20  0.1);.#else.    
a3860 20 20 20 20 2f 2a 20 49 74 20 6d 61 6b 65 73 20      /* It makes 
a3870 6d 6f 72 65 20 73 65 6e 73 65 20 74 6f 20 75 73  more sense to us
a3880 65 20 30 2e 35 20 2a 2f 0a 20 20 20 20 20 20 20  e 0.5 */.       
a3890 20 66 6f 72 28 69 64 78 3d 70 72 65 63 69 73 69   for(idx=precisi
a38a0 6f 6e 2c 20 72 6f 75 6e 64 65 72 3d 30 2e 35 3b  on, rounder=0.5;
a38b0 20 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c 20 72   idx>0; idx--, r
a38c0 6f 75 6e 64 65 72 2a 3d 30 2e 31 29 7b 7d 0a 23  ounder*=0.1){}.#
a38d0 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 69 66  endif.        if
a38e0 28 20 78 74 79 70 65 3d 3d 65 74 46 4c 4f 41 54  ( xtype==etFLOAT
a38f0 20 29 20 72 65 61 6c 76 61 6c 75 65 20 2b 3d 20   ) realvalue += 
a3900 72 6f 75 6e 64 65 72 3b 0a 20 20 20 20 20 20 20  rounder;.       
a3910 20 2f 2a 20 4e 6f 72 6d 61 6c 69 7a 65 20 72 65   /* Normalize re
a3920 61 6c 76 61 6c 75 65 20 74 6f 20 77 69 74 68 69  alvalue to withi
a3930 6e 20 31 30 2e 30 20 3e 20 72 65 61 6c 76 61 6c  n 10.0 > realval
a3940 75 65 20 3e 3d 20 31 2e 30 20 2a 2f 0a 20 20 20  ue >= 1.0 */.   
a3950 20 20 20 20 20 65 78 70 20 3d 20 30 3b 0a 20 20       exp = 0;.  
a3960 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
a3970 33 49 73 4e 61 4e 28 28 64 6f 75 62 6c 65 29 72  3IsNaN((double)r
a3980 65 61 6c 76 61 6c 75 65 29 20 29 7b 0a 20 20 20  ealvalue) ){.   
a3990 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22         bufpt = "
a39a0 4e 61 4e 22 3b 0a 20 20 20 20 20 20 20 20 20 20  NaN";.          
a39b0 6c 65 6e 67 74 68 20 3d 20 33 3b 0a 20 20 20 20  length = 3;.    
a39c0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
a39d0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
a39e0 66 28 20 72 65 61 6c 76 61 6c 75 65 3e 30 2e 30  f( realvalue>0.0
a39f0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68   ){.          wh
a3a00 69 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d  ile( realvalue>=
a3a10 31 65 33 32 20 26 26 20 65 78 70 3c 3d 33 35 30  1e32 && exp<=350
a3a20 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d   ){ realvalue *=
a3a30 20 31 65 2d 33 32 3b 20 65 78 70 2b 3d 33 32 3b   1e-32; exp+=32;
a3a40 20 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69   }.          whi
a3a50 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31  le( realvalue>=1
a3a60 65 38 20 26 26 20 65 78 70 3c 3d 33 35 30 20 29  e8 && exp<=350 )
a3a70 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31  { realvalue *= 1
a3a80 65 2d 38 3b 20 65 78 70 2b 3d 38 3b 20 7d 0a 20  e-8; exp+=8; }. 
a3a90 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20           while( 
a3aa0 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20  realvalue>=10.0 
a3ab0 26 26 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 72  && exp<=350 ){ r
a3ac0 65 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b  ealvalue *= 0.1;
a3ad0 20 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20   exp++; }.      
a3ae0 20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76      while( realv
a3af0 61 6c 75 65 3c 31 65 2d 38 20 29 7b 20 72 65 61  alue<1e-8 ){ rea
a3b00 6c 76 61 6c 75 65 20 2a 3d 20 31 65 38 3b 20 65  lvalue *= 1e8; e
a3b10 78 70 2d 3d 38 3b 20 7d 0a 20 20 20 20 20 20 20  xp-=8; }.       
a3b20 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61     while( realva
a3b30 6c 75 65 3c 31 2e 30 20 29 7b 20 72 65 61 6c 76  lue<1.0 ){ realv
a3b40 61 6c 75 65 20 2a 3d 20 31 30 2e 30 3b 20 65 78  alue *= 10.0; ex
a3b50 70 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 20  p--; }.         
a3b60 20 69 66 28 20 65 78 70 3e 33 35 30 20 29 7b 0a   if( exp>350 ){.
a3b70 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
a3b80 70 72 65 66 69 78 3d 3d 27 2d 27 20 29 7b 0a 20  prefix=='-' ){. 
a3b90 20 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66               buf
a3ba0 70 74 20 3d 20 22 2d 49 6e 66 22 3b 0a 20 20 20  pt = "-Inf";.   
a3bb0 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69           }else i
a3bc0 66 28 20 70 72 65 66 69 78 3d 3d 27 2b 27 20 29  f( prefix=='+' )
a3bd0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
a3be0 62 75 66 70 74 20 3d 20 22 2b 49 6e 66 22 3b 0a  bufpt = "+Inf";.
a3bf0 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73              }els
a3c00 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e{.             
a3c10 20 62 75 66 70 74 20 3d 20 22 49 6e 66 22 3b 0a   bufpt = "Inf";.
a3c20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
a3c30 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68            length
a3c40 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
a3c50 33 30 28 62 75 66 70 74 29 3b 0a 20 20 20 20 20  30(bufpt);.     
a3c60 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
a3c70 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a3c80 20 20 7d 0a 20 20 20 20 20 20 20 20 62 75 66 70    }.        bufp
a3c90 74 20 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20  t = buf;.       
a3ca0 20 2f 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 49   /*.        ** I
a3cb0 66 20 74 68 65 20 66 69 65 6c 64 20 74 79 70 65  f the field type
a3cc0 20 69 73 20 65 74 47 45 4e 45 52 49 43 2c 20 74   is etGENERIC, t
a3cd0 68 65 6e 20 63 6f 6e 76 65 72 74 20 74 6f 20 65  hen convert to e
a3ce0 69 74 68 65 72 20 65 74 45 58 50 0a 20 20 20 20  ither etEXP.    
a3cf0 20 20 20 20 2a 2a 20 6f 72 20 65 74 46 4c 4f 41      ** or etFLOA
a3d00 54 2c 20 61 73 20 61 70 70 72 6f 70 72 69 61 74  T, as appropriat
a3d10 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  e..        */.  
a3d20 20 20 20 20 20 20 66 6c 61 67 5f 65 78 70 20 3d        flag_exp =
a3d30 20 78 74 79 70 65 3d 3d 65 74 45 58 50 3b 0a 20   xtype==etEXP;. 
a3d40 20 20 20 20 20 20 20 69 66 28 20 78 74 79 70 65         if( xtype
a3d50 21 3d 65 74 46 4c 4f 41 54 20 29 7b 0a 20 20 20  !=etFLOAT ){.   
a3d60 20 20 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65         realvalue
a3d70 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a 20 20 20   += rounder;.   
a3d80 20 20 20 20 20 20 20 69 66 28 20 72 65 61 6c 76         if( realv
a3d90 61 6c 75 65 3e 3d 31 30 2e 30 20 29 7b 20 72 65  alue>=10.0 ){ re
a3da0 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b 20  alvalue *= 0.1; 
a3db0 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20  exp++; }.       
a3dc0 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 78   }.        if( x
a3dd0 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 43 20  type==etGENERIC 
a3de0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c 61  ){.          fla
a3df0 67 5f 72 74 7a 20 3d 20 21 66 6c 61 67 5f 61 6c  g_rtz = !flag_al
a3e00 74 65 72 6e 61 74 65 66 6f 72 6d 3b 0a 20 20 20  ternateform;.   
a3e10 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3c 2d         if( exp<-
a3e20 34 20 7c 7c 20 65 78 70 3e 70 72 65 63 69 73 69  4 || exp>precisi
a3e30 6f 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  on ){.          
a3e40 20 20 78 74 79 70 65 20 3d 20 65 74 45 58 50 3b    xtype = etEXP;
a3e50 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
a3e60 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 72  {.            pr
a3e70 65 63 69 73 69 6f 6e 20 3d 20 70 72 65 63 69 73  ecision = precis
a3e80 69 6f 6e 20 2d 20 65 78 70 3b 0a 20 20 20 20 20  ion - exp;.     
a3e90 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20 65         xtype = e
a3ea0 74 46 4c 4f 41 54 3b 0a 20 20 20 20 20 20 20 20  tFLOAT;.        
a3eb0 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73    }.        }els
a3ec0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c 61  e{.          fla
a3ed0 67 5f 72 74 7a 20 3d 20 30 3b 0a 20 20 20 20 20  g_rtz = 0;.     
a3ee0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
a3ef0 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 7b   xtype==etEXP ){
a3f00 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d 20  .          e2 = 
a3f10 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  0;.        }else
a3f20 7b 0a 20 20 20 20 20 20 20 20 20 20 65 32 20 3d  {.          e2 =
a3f30 20 65 78 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a   exp;.        }.
a3f40 20 20 20 20 20 20 20 20 6e 73 64 20 3d 20 30 3b          nsd = 0;
a3f50 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f 64 70  .        flag_dp
a3f60 20 3d 20 28 70 72 65 63 69 73 69 6f 6e 3e 30 20   = (precision>0 
a3f70 3f 31 3a 30 29 20 7c 20 66 6c 61 67 5f 61 6c 74  ?1:0) | flag_alt
a3f80 65 72 6e 61 74 65 66 6f 72 6d 20 7c 20 66 6c 61  ernateform | fla
a3f90 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a 20 20 20 20  g_altform2;.    
a3fa0 20 20 20 20 2f 2a 20 54 68 65 20 73 69 67 6e 20      /* The sign 
a3fb0 69 6e 20 66 72 6f 6e 74 20 6f 66 20 74 68 65 20  in front of the 
a3fc0 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 20  number */.      
a3fd0 20 20 69 66 28 20 70 72 65 66 69 78 20 29 7b 0a    if( prefix ){.
a3fe0 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70            *(bufp
a3ff0 74 2b 2b 29 20 3d 20 70 72 65 66 69 78 3b 0a 20  t++) = prefix;. 
a4000 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a4010 20 2f 2a 20 44 69 67 69 74 73 20 70 72 69 6f 72   /* Digits prior
a4020 20 74 6f 20 74 68 65 20 64 65 63 69 6d 61 6c 20   to the decimal 
a4030 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20  point */.       
a4040 20 69 66 28 20 65 32 3c 30 20 29 7b 0a 20 20 20   if( e2<0 ){.   
a4050 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b         *(bufpt++
a4060 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20 20  ) = '0';.       
a4070 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
a4080 20 20 66 6f 72 28 3b 20 65 32 3e 3d 30 3b 20 65    for(; e2>=0; e
a4090 32 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20  2--){.          
a40a0 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 65    *(bufpt++) = e
a40b0 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 61 6c  t_getdigit(&real
a40c0 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 20 20  value,&nsd);.   
a40d0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a40e0 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68   }.        /* Th
a40f0 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20  e decimal point 
a4100 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 66  */.        if( f
a4110 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20 20 20 20  lag_dp ){.      
a4120 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d      *(bufpt++) =
a4130 20 27 2e 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a   '.';.        }.
a4140 20 20 20 20 20 20 20 20 2f 2a 20 22 30 22 20 64          /* "0" d
a4150 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65 20  igits after the 
a4160 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 62 75  decimal point bu
a4170 74 20 62 65 66 6f 72 65 20 74 68 65 20 66 69 72  t before the fir
a4180 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 69  st.        ** si
a4190 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 20  gnificant digit 
a41a0 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 2a 2f  of the number */
a41b0 0a 20 20 20 20 20 20 20 20 66 6f 72 28 65 32 2b  .        for(e2+
a41c0 2b 3b 20 65 32 3c 30 3b 20 70 72 65 63 69 73 69  +; e2<0; precisi
a41d0 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b 0a 20 20 20  on--, e2++){.   
a41e0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
a41f0 72 65 63 69 73 69 6f 6e 3e 30 20 29 3b 0a 20 20  recision>0 );.  
a4200 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b          *(bufpt+
a4210 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20  +) = '0';.      
a4220 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 53    }.        /* S
a4230 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74  ignificant digit
a4240 73 20 61 66 74 65 72 20 74 68 65 20 64 65 63 69  s after the deci
a4250 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20  mal point */.   
a4260 20 20 20 20 20 77 68 69 6c 65 28 20 28 70 72 65       while( (pre
a4270 63 69 73 69 6f 6e 2d 2d 29 3e 30 20 29 7b 0a 20  cision--)>0 ){. 
a4280 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74           *(bufpt
a4290 2b 2b 29 20 3d 20 65 74 5f 67 65 74 64 69 67 69  ++) = et_getdigi
a42a0 74 28 26 72 65 61 6c 76 61 6c 75 65 2c 26 6e 73  t(&realvalue,&ns
a42b0 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  d);.        }.  
a42c0 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20        /* Remove 
a42d0 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20 61  trailing zeros a
a42e0 6e 64 20 74 68 65 20 22 2e 22 20 69 66 20 6e 6f  nd the "." if no
a42f0 20 64 69 67 69 74 73 20 66 6f 6c 6c 6f 77 20 74   digits follow t
a4300 68 65 20 22 2e 22 20 2a 2f 0a 20 20 20 20 20 20  he "." */.      
a4310 20 20 69 66 28 20 66 6c 61 67 5f 72 74 7a 20 26    if( flag_rtz &
a4320 26 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20  & flag_dp ){.   
a4330 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 62 75         while( bu
a4340 66 70 74 5b 2d 31 5d 3d 3d 27 30 27 20 29 20 2a  fpt[-1]=='0' ) *
a4350 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b 0a 20  (--bufpt) = 0;. 
a4360 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
a4370 20 62 75 66 70 74 3e 62 75 66 20 29 3b 0a 20 20   bufpt>buf );.  
a4380 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66 70          if( bufp
a4390 74 5b 2d 31 5d 3d 3d 27 2e 27 20 29 7b 0a 20 20  t[-1]=='.' ){.  
a43a0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66 6c            if( fl
a43b0 61 67 5f 61 6c 74 66 6f 72 6d 32 20 29 7b 0a 20  ag_altform2 ){. 
a43c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 28 62               *(b
a43d0 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a 20  ufpt++) = '0';. 
a43e0 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
a43f0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
a4400 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b 0a  *(--bufpt) = 0;.
a4410 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
a4420 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a4430 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 41    }.        /* A
a4440 64 64 20 74 68 65 20 22 65 4e 4e 4e 22 20 73 75  dd the "eNNN" su
a4450 66 66 69 78 20 2a 2f 0a 20 20 20 20 20 20 20 20  ffix */.        
a4460 69 66 28 20 66 6c 61 67 5f 65 78 70 20 7c 7c 20  if( flag_exp || 
a4470 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 7b 0a  xtype==etEXP ){.
a4480 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70            *(bufp
a4490 74 2b 2b 29 20 3d 20 61 44 69 67 69 74 73 5b 69  t++) = aDigits[i
a44a0 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b 0a  nfop->charset];.
a44b0 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65 78            if( ex
a44c0 70 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  p<0 ){.         
a44d0 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20     *(bufpt++) = 
a44e0 27 2d 27 3b 20 65 78 70 20 3d 20 2d 65 78 70 3b  '-'; exp = -exp;
a44f0 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
a4500 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 28  {.            *(
a4510 62 75 66 70 74 2b 2b 29 20 3d 20 27 2b 27 3b 0a  bufpt++) = '+';.
a4520 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
a4530 20 20 20 20 20 20 69 66 28 20 65 78 70 3e 3d 31        if( exp>=1
a4540 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  00 ){.          
a4550 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20 28    *(bufpt++) = (
a4560 63 68 61 72 29 28 28 65 78 70 2f 31 30 30 29 2b  char)((exp/100)+
a4570 27 30 27 29 3b 20 20 20 20 20 20 20 20 2f 2a 20  '0');        /* 
a4580 31 30 30 27 73 20 64 69 67 69 74 20 2a 2f 0a 20  100's digit */. 
a4590 20 20 20 20 20 20 20 20 20 20 20 65 78 70 20 25             exp %
a45a0 3d 20 31 30 30 3b 0a 20 20 20 20 20 20 20 20 20  = 100;.         
a45b0 20 7d 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62   }.          *(b
a45c0 75 66 70 74 2b 2b 29 20 3d 20 28 63 68 61 72 29  ufpt++) = (char)
a45d0 28 65 78 70 2f 31 30 2b 27 30 27 29 3b 20 20 20  (exp/10+'0');   
a45e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 30 27            /* 10'
a45f0 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 20  s digit */.     
a4600 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20       *(bufpt++) 
a4610 3d 20 28 63 68 61 72 29 28 65 78 70 25 31 30 2b  = (char)(exp%10+
a4620 27 30 27 29 3b 20 20 20 20 20 20 20 20 20 20 20  '0');           
a4630 20 20 2f 2a 20 31 27 73 20 64 69 67 69 74 20 2a    /* 1's digit *
a4640 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  /.        }.    
a4650 20 20 20 20 2a 62 75 66 70 74 20 3d 20 30 3b 0a      *bufpt = 0;.
a4660 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
a4670 63 6f 6e 76 65 72 74 65 64 20 6e 75 6d 62 65 72  converted number
a4680 20 69 73 20 69 6e 20 62 75 66 5b 5d 20 61 6e 64   is in buf[] and
a4690 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65 64   zero terminated
a46a0 2e 20 4f 75 74 70 75 74 20 69 74 2e 0a 20 20 20  . Output it..   
a46b0 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61       ** Note tha
a46c0 74 20 74 68 65 20 6e 75 6d 62 65 72 20 69 73 20  t the number is 
a46d0 69 6e 20 74 68 65 20 75 73 75 61 6c 20 6f 72 64  in the usual ord
a46e0 65 72 2c 20 6e 6f 74 20 72 65 76 65 72 73 65 64  er, not reversed
a46f0 20 61 73 20 77 69 74 68 0a 20 20 20 20 20 20 20   as with.       
a4700 20 2a 2a 20 69 6e 74 65 67 65 72 20 63 6f 6e 76   ** integer conv
a4710 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20 20  ersions. */.    
a4720 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 28 69 6e      length = (in
a4730 74 29 28 62 75 66 70 74 2d 62 75 66 29 3b 0a 20  t)(bufpt-buf);. 
a4740 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62         bufpt = b
a4750 75 66 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20  uf;..        /* 
a4760 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 20 41  Special case:  A
a4770 64 64 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73  dd leading zeros
a4780 20 69 66 20 74 68 65 20 66 6c 61 67 5f 7a 65 72   if the flag_zer
a4790 6f 70 61 64 20 66 6c 61 67 20 69 73 0a 20 20 20  opad flag is.   
a47a0 20 20 20 20 20 2a 2a 20 73 65 74 20 61 6e 64 20       ** set and 
a47b0 77 65 20 61 72 65 20 6e 6f 74 20 6c 65 66 74 20  we are not left 
a47c0 6a 75 73 74 69 66 69 65 64 20 2a 2f 0a 20 20 20  justified */.   
a47d0 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f 7a 65       if( flag_ze
a47e0 72 6f 70 61 64 20 26 26 20 21 66 6c 61 67 5f 6c  ropad && !flag_l
a47f0 65 66 74 6a 75 73 74 69 66 79 20 26 26 20 6c 65  eftjustify && le
a4800 6e 67 74 68 20 3c 20 77 69 64 74 68 29 7b 0a 20  ngth < width){. 
a4810 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a           int i;.
a4820 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6e 50            int nP
a4830 61 64 20 3d 20 77 69 64 74 68 20 2d 20 6c 65 6e  ad = width - len
a4840 67 74 68 3b 0a 20 20 20 20 20 20 20 20 20 20 66  gth;.          f
a4850 6f 72 28 69 3d 77 69 64 74 68 3b 20 69 3e 3d 6e  or(i=width; i>=n
a4860 50 61 64 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20  Pad; i--){.     
a4870 20 20 20 20 20 20 20 62 75 66 70 74 5b 69 5d 20         bufpt[i] 
a4880 3d 20 62 75 66 70 74 5b 69 2d 6e 50 61 64 5d 3b  = bufpt[i-nPad];
a4890 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
a48a0 20 20 20 20 20 20 20 69 20 3d 20 70 72 65 66 69         i = prefi
a48b0 78 21 3d 30 3b 0a 20 20 20 20 20 20 20 20 20 20  x!=0;.          
a48c0 77 68 69 6c 65 28 20 6e 50 61 64 2d 2d 20 29 20  while( nPad-- ) 
a48d0 62 75 66 70 74 5b 69 2b 2b 5d 20 3d 20 27 30 27  bufpt[i++] = '0'
a48e0 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67  ;.          leng
a48f0 74 68 20 3d 20 77 69 64 74 68 3b 0a 20 20 20 20  th = width;.    
a4900 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
a4910 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
a4920 20 20 63 61 73 65 20 65 74 53 49 5a 45 3a 0a 20    case etSIZE:. 
a4930 20 20 20 20 20 20 20 2a 28 76 61 5f 61 72 67 28         *(va_arg(
a4940 61 70 2c 69 6e 74 2a 29 29 20 3d 20 70 41 63 63  ap,int*)) = pAcc
a4950 75 6d 2d 3e 6e 43 68 61 72 3b 0a 20 20 20 20 20  um->nChar;.     
a4960 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74     length = widt
a4970 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62  h = 0;.        b
a4980 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
a4990 20 65 74 50 45 52 43 45 4e 54 3a 0a 20 20 20 20   etPERCENT:.    
a49a0 20 20 20 20 62 75 66 5b 30 5d 20 3d 20 27 25 27      buf[0] = '%'
a49b0 3b 0a 20 20 20 20 20 20 20 20 62 75 66 70 74 20  ;.        bufpt 
a49c0 3d 20 62 75 66 3b 0a 20 20 20 20 20 20 20 20 6c  = buf;.        l
a49d0 65 6e 67 74 68 20 3d 20 31 3b 0a 20 20 20 20 20  ength = 1;.     
a49e0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
a49f0 63 61 73 65 20 65 74 43 48 41 52 58 3a 0a 20 20  case etCHARX:.  
a4a00 20 20 20 20 20 20 63 20 3d 20 76 61 5f 61 72 67        c = va_arg
a4a10 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20  (ap,int);.      
a4a20 20 20 62 75 66 5b 30 5d 20 3d 20 28 63 68 61 72    buf[0] = (char
a4a30 29 63 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  )c;.        if( 
a4a40 70 72 65 63 69 73 69 6f 6e 3e 3d 30 20 29 7b 0a  precision>=0 ){.
a4a50 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 64            for(id
a4a60 78 3d 31 3b 20 69 64 78 3c 70 72 65 63 69 73 69  x=1; idx<precisi
a4a70 6f 6e 3b 20 69 64 78 2b 2b 29 20 62 75 66 5b 69  on; idx++) buf[i
a4a80 64 78 5d 20 3d 20 28 63 68 61 72 29 63 3b 0a 20  dx] = (char)c;. 
a4a90 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20           length 
a4aa0 3d 20 70 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20  = precision;.   
a4ab0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
a4ac0 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 31 3b        length =1;
a4ad0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a4ae0 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a     bufpt = buf;.
a4af0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
a4b00 20 20 20 20 20 63 61 73 65 20 65 74 53 54 52 49       case etSTRI
a4b10 4e 47 3a 0a 20 20 20 20 20 20 63 61 73 65 20 65  NG:.      case e
a4b20 74 44 59 4e 53 54 52 49 4e 47 3a 0a 20 20 20 20  tDYNSTRING:.    
a4b30 20 20 20 20 62 75 66 70 74 20 3d 20 76 61 5f 61      bufpt = va_a
a4b40 72 67 28 61 70 2c 63 68 61 72 2a 29 3b 0a 20 20  rg(ap,char*);.  
a4b50 20 20 20 20 20 20 69 66 28 20 62 75 66 70 74 3d        if( bufpt=
a4b60 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
a4b70 62 75 66 70 74 20 3d 20 22 22 3b 0a 20 20 20 20  bufpt = "";.    
a4b80 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 78 74      }else if( xt
a4b90 79 70 65 3d 3d 65 74 44 59 4e 53 54 52 49 4e 47  ype==etDYNSTRING
a4ba0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 45   ){.          zE
a4bb0 78 74 72 61 20 3d 20 62 75 66 70 74 3b 0a 20 20  xtra = bufpt;.  
a4bc0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a4bd0 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3e 3d 30  if( precision>=0
a4be0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6f   ){.          fo
a4bf0 72 28 6c 65 6e 67 74 68 3d 30 3b 20 6c 65 6e 67  r(length=0; leng
a4c00 74 68 3c 70 72 65 63 69 73 69 6f 6e 20 26 26 20  th<precision && 
a4c10 62 75 66 70 74 5b 6c 65 6e 67 74 68 5d 3b 20 6c  bufpt[length]; l
a4c20 65 6e 67 74 68 2b 2b 29 7b 7d 0a 20 20 20 20 20  ength++){}.     
a4c30 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
a4c40 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 73 71 6c      length = sql
a4c50 69 74 65 33 53 74 72 6c 65 6e 33 30 28 62 75 66  ite3Strlen30(buf
a4c60 70 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  pt);.        }. 
a4c70 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
a4c80 20 20 20 20 63 61 73 65 20 65 74 53 51 4c 45 53      case etSQLES
a4c90 43 41 50 45 3a 0a 20 20 20 20 20 20 63 61 73 65  CAPE:.      case
a4ca0 20 65 74 53 51 4c 45 53 43 41 50 45 32 3a 0a 20   etSQLESCAPE2:. 
a4cb0 20 20 20 20 20 63 61 73 65 20 65 74 53 51 4c 45       case etSQLE
a4cc0 53 43 41 50 45 33 3a 20 7b 0a 20 20 20 20 20 20  SCAPE3: {.      
a4cd0 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 2c 20 69    int i, j, n, i
a4ce0 73 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69  snull;.        i
a4cf0 6e 74 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20 20  nt needQuote;.  
a4d00 20 20 20 20 20 20 63 68 61 72 20 63 68 3b 0a 20        char ch;. 
a4d10 20 20 20 20 20 20 20 63 68 61 72 20 71 20 3d 20         char q = 
a4d20 28 28 78 74 79 70 65 3d 3d 65 74 53 51 4c 45 53  ((xtype==etSQLES
a4d30 43 41 50 45 33 29 3f 27 22 27 3a 27 5c 27 27 29  CAPE3)?'"':'\'')
a4d40 3b 20 20 20 2f 2a 20 51 75 6f 74 65 20 63 68 61  ;   /* Quote cha
a4d50 72 61 63 74 65 72 20 2a 2f 0a 20 20 20 20 20 20  racter */.      
a4d60 20 20 63 68 61 72 20 2a 65 73 63 61 72 67 20 3d    char *escarg =
a4d70 20 76 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a   va_arg(ap,char*
a4d80 29 3b 0a 20 20 20 20 20 20 20 20 69 73 6e 75 6c  );.        isnul
a4d90 6c 20 3d 20 65 73 63 61 72 67 3d 3d 30 3b 0a 20  l = escarg==0;. 
a4da0 20 20 20 20 20 20 20 69 66 28 20 69 73 6e 75 6c         if( isnul
a4db0 6c 20 29 20 65 73 63 61 72 67 20 3d 20 28 78 74  l ) escarg = (xt
a4dc0 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50 45  ype==etSQLESCAPE
a4dd0 32 20 3f 20 22 4e 55 4c 4c 22 20 3a 20 22 28 4e  2 ? "NULL" : "(N
a4de0 55 4c 4c 29 22 29 3b 0a 20 20 20 20 20 20 20 20  ULL)");.        
a4df0 66 6f 72 28 69 3d 6e 3d 30 3b 20 28 63 68 3d 65  for(i=n=0; (ch=e
a4e00 73 63 61 72 67 5b 69 5d 29 21 3d 30 3b 20 69 2b  scarg[i])!=0; i+
a4e10 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
a4e20 28 20 63 68 3d 3d 71 20 29 20 20 6e 2b 2b 3b 0a  ( ch==q )  n++;.
a4e30 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a4e40 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 21 69    needQuote = !i
a4e50 73 6e 75 6c 6c 20 26 26 20 78 74 79 70 65 3d 3d  snull && xtype==
a4e60 65 74 53 51 4c 45 53 43 41 50 45 32 3b 0a 20 20  etSQLESCAPE2;.  
a4e70 20 20 20 20 20 20 6e 20 2b 3d 20 69 20 2b 20 31        n += i + 1
a4e80 20 2b 20 6e 65 65 64 51 75 6f 74 65 2a 32 3b 0a   + needQuote*2;.
a4e90 20 20 20 20 20 20 20 20 69 66 28 20 6e 3e 65 74          if( n>et
a4ea0 42 55 46 53 49 5a 45 20 29 7b 0a 20 20 20 20 20  BUFSIZE ){.     
a4eb0 20 20 20 20 20 62 75 66 70 74 20 3d 20 7a 45 78       bufpt = zEx
a4ec0 74 72 61 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  tra = sqlite3Mal
a4ed0 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 20 20 20 20  loc( n );.      
a4ee0 20 20 20 20 69 66 28 20 62 75 66 70 74 3d 3d 30      if( bufpt==0
a4ef0 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
a4f00 70 41 63 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61  pAccum->mallocFa
a4f10 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
a4f20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
a4f30 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a4f40 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
a4f50 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a     bufpt = buf;.
a4f60 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a4f70 20 20 6a 20 3d 20 30 3b 0a 20 20 20 20 20 20 20    j = 0;.       
a4f80 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29   if( needQuote )
a4f90 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b   bufpt[j++] = q;
a4fa0 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30  .        for(i=0
a4fb0 3b 20 28 63 68 3d 65 73 63 61 72 67 5b 69 5d 29  ; (ch=escarg[i])
a4fc0 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  !=0; i++){.     
a4fd0 20 20 20 20 20 62 75 66 70 74 5b 6a 2b 2b 5d 20       bufpt[j++] 
a4fe0 3d 20 63 68 3b 0a 20 20 20 20 20 20 20 20 20 20  = ch;.          
a4ff0 69 66 28 20 63 68 3d 3d 71 20 29 20 62 75 66 70  if( ch==q ) bufp
a5000 74 5b 6a 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 20  t[j++] = ch;.   
a5010 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
a5020 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 62  f( needQuote ) b
a5030 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a 20  ufpt[j++] = q;. 
a5040 20 20 20 20 20 20 20 62 75 66 70 74 5b 6a 5d 20         bufpt[j] 
a5050 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e  = 0;.        len
a5060 67 74 68 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20  gth = j;.       
a5070 20 2f 2a 20 54 68 65 20 70 72 65 63 69 73 69 6f   /* The precisio
a5080 6e 20 69 73 20 69 67 6e 6f 72 65 64 20 6f 6e 20  n is ignored on 
a5090 25 71 20 61 6e 64 20 25 51 20 2a 2f 0a 20 20 20  %q and %Q */.   
a50a0 20 20 20 20 20 2f 2a 20 69 66 28 20 70 72 65 63       /* if( prec
a50b0 69 73 69 6f 6e 3e 3d 30 20 26 26 20 70 72 65 63  ision>=0 && prec
a50c0 69 73 69 6f 6e 3c 6c 65 6e 67 74 68 20 29 20 6c  ision<length ) l
a50d0 65 6e 67 74 68 20 3d 20 70 72 65 63 69 73 69 6f  ength = precisio
a50e0 6e 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 62 72  n; */.        br
a50f0 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
a5100 20 20 20 63 61 73 65 20 65 74 54 4f 4b 45 4e 3a     case etTOKEN:
a5110 20 7b 0a 20 20 20 20 20 20 20 20 54 6f 6b 65 6e   {.        Token
a5120 20 2a 70 54 6f 6b 65 6e 20 3d 20 76 61 5f 61 72   *pToken = va_ar
a5130 67 28 61 70 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 20  g(ap, Token*);. 
a5140 20 20 20 20 20 20 20 69 66 28 20 70 54 6f 6b 65         if( pToke
a5150 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  n ){.          s
a5160 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70  qlite3StrAccumAp
a5170 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 28 63 6f  pend(pAccum, (co
a5180 6e 73 74 20 63 68 61 72 2a 29 70 54 6f 6b 65 6e  nst char*)pToken
a5190 2d 3e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b  ->z, pToken->n);
a51a0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a51b0 20 20 20 6c 65 6e 67 74 68 20 3d 20 77 69 64 74     length = widt
a51c0 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62  h = 0;.        b
a51d0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
a51e0 20 20 20 20 63 61 73 65 20 65 74 53 52 43 4c 49      case etSRCLI
a51f0 53 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 72  ST: {.        Sr
a5200 63 4c 69 73 74 20 2a 70 53 72 63 20 3d 20 76 61  cList *pSrc = va
a5210 5f 61 72 67 28 61 70 2c 20 53 72 63 4c 69 73 74  _arg(ap, SrcList
a5220 2a 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  *);.        int 
a5230 6b 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69  k = va_arg(ap, i
a5240 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 73 74 72  nt);.        str
a5250 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
a5260 20 2a 70 49 74 65 6d 20 3d 20 26 70 53 72 63 2d   *pItem = &pSrc-
a5270 3e 61 5b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 61  >a[k];.        a
a5280 73 73 65 72 74 28 20 6b 3e 3d 30 20 26 26 20 6b  ssert( k>=0 && k
a5290 3c 70 53 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20  <pSrc->nSrc );. 
a52a0 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d         if( pItem
a52b0 2d 3e 7a 44 61 74 61 62 61 73 65 20 29 7b 0a 20  ->zDatabase ){. 
a52c0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
a52d0 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70  StrAccumAppend(p
a52e0 41 63 63 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 44  Accum, pItem->zD
a52f0 61 74 61 62 61 73 65 2c 20 2d 31 29 3b 0a 20 20  atabase, -1);.  
a5300 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
a5310 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41  trAccumAppend(pA
a5320 63 63 75 6d 2c 20 22 2e 22 2c 20 31 29 3b 0a 20  ccum, ".", 1);. 
a5330 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a5340 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
a5350 41 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 70  Append(pAccum, p
a5360 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 2d 31 29  Item->zName, -1)
a5370 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68  ;.        length
a5380 20 3d 20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20   = width = 0;.  
a5390 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
a53a0 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75     }.      defau
a53b0 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 61 73  lt: {.        as
a53c0 73 65 72 74 28 20 78 74 79 70 65 3d 3d 65 74 49  sert( xtype==etI
a53d0 4e 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 20 20  NVALID );.      
a53e0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
a53f0 7d 0a 20 20 20 20 7d 2f 2a 20 45 6e 64 20 73 77  }.    }/* End sw
a5400 69 74 63 68 20 6f 76 65 72 20 74 68 65 20 66 6f  itch over the fo
a5410 72 6d 61 74 20 74 79 70 65 20 2a 2f 0a 20 20 20  rmat type */.   
a5420 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 74   /*.    ** The t
a5430 65 78 74 20 6f 66 20 74 68 65 20 63 6f 6e 76 65  ext of the conve
a5440 72 73 69 6f 6e 20 69 73 20 70 6f 69 6e 74 65 64  rsion is pointed
a5450 20 74 6f 20 62 79 20 22 62 75 66 70 74 22 20 61   to by "bufpt" a
a5460 6e 64 20 69 73 0a 20 20 20 20 2a 2a 20 22 6c 65  nd is.    ** "le
a5470 6e 67 74 68 22 20 63 68 61 72 61 63 74 65 72 73  ngth" characters
a5480 20 6c 6f 6e 67 2e 20 20 54 68 65 20 66 69 65 6c   long.  The fiel
a5490 64 20 77 69 64 74 68 20 69 73 20 22 77 69 64 74  d width is "widt
a54a0 68 22 2e 20 20 44 6f 0a 20 20 20 20 2a 2a 20 74  h".  Do.    ** t
a54b0 68 65 20 6f 75 74 70 75 74 2e 0a 20 20 20 20 2a  he output..    *
a54c0 2f 0a 20 20 20 20 69 66 28 20 21 66 6c 61 67 5f  /.    if( !flag_
a54d0 6c 65 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20  leftjustify ){. 
a54e0 20 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e       register in
a54f0 74 20 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20  t nspace;.      
a5500 6e 73 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c  nspace = width-l
a5510 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28  ength;.      if(
a5520 20 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20   nspace>0 ){.   
a5530 20 20 20 20 20 61 70 70 65 6e 64 53 70 61 63 65       appendSpace
a5540 28 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29  (pAccum, nspace)
a5550 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
a5560 20 20 20 20 69 66 28 20 6c 65 6e 67 74 68 3e 30      if( length>0
a5570 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
a5580 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
a5590 70 41 63 63 75 6d 2c 20 62 75 66 70 74 2c 20 6c  pAccum, bufpt, l
a55a0 65 6e 67 74 68 29 3b 0a 20 20 20 20 7d 0a 20 20  ength);.    }.  
a55b0 20 20 69 66 28 20 66 6c 61 67 5f 6c 65 66 74 6a    if( flag_leftj
a55c0 75 73 74 69 66 79 20 29 7b 0a 20 20 20 20 20 20  ustify ){.      
a55d0 72 65 67 69 73 74 65 72 20 69 6e 74 20 6e 73 70  register int nsp
a55e0 61 63 65 3b 0a 20 20 20 20 20 20 6e 73 70 61 63  ace;.      nspac
a55f0 65 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 74 68  e = width-length
a5600 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 73 70 61  ;.      if( nspa
a5610 63 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ce>0 ){.        
a5620 61 70 70 65 6e 64 53 70 61 63 65 28 70 41 63 63  appendSpace(pAcc
a5630 75 6d 2c 20 6e 73 70 61 63 65 29 3b 0a 20 20 20  um, nspace);.   
a5640 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
a5650 66 28 20 7a 45 78 74 72 61 20 29 7b 0a 20 20 20  f( zExtra ){.   
a5660 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
a5670 7a 45 78 74 72 61 29 3b 0a 20 20 20 20 7d 0a 20  zExtra);.    }. 
a5680 20 7d 2f 2a 20 45 6e 64 20 66 6f 72 20 6c 6f 6f   }/* End for loo
a5690 70 20 6f 76 65 72 20 74 68 65 20 66 6f 72 6d 61  p over the forma
a56a0 74 20 73 74 72 69 6e 67 20 2a 2f 0a 7d 20 2f 2a  t string */.} /*
a56b0 20 45 6e 64 20 6f 66 20 66 75 6e 63 74 69 6f 6e   End of function
a56c0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e   */../*.** Appen
a56d0 64 20 4e 20 62 79 74 65 73 20 6f 66 20 74 65 78  d N bytes of tex
a56e0 74 20 66 72 6f 6d 20 7a 20 74 6f 20 74 68 65 20  t from z to the 
a56f0 53 74 72 41 63 63 75 6d 20 6f 62 6a 65 63 74 2e  StrAccum object.
a5700 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
a5710 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
a5720 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 53 74  trAccumAppend(St
a5730 72 41 63 63 75 6d 20 2a 70 2c 20 63 6f 6e 73 74  rAccum *p, const
a5740 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 4e 29   char *z, int N)
a5750 7b 0a 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30  {.  assert( z!=0
a5760 20 7c 7c 20 4e 3d 3d 30 20 29 3b 0a 20 20 69 66   || N==0 );.  if
a5770 28 20 70 2d 3e 74 6f 6f 42 69 67 20 7c 20 70 2d  ( p->tooBig | p-
a5780 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
a5790 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 70 2d  .    testcase(p-
a57a0 3e 74 6f 6f 42 69 67 29 3b 0a 20 20 20 20 74 65  >tooBig);.    te
a57b0 73 74 63 61 73 65 28 70 2d 3e 6d 61 6c 6c 6f 63  stcase(p->malloc
a57c0 46 61 69 6c 65 64 29 3b 0a 20 20 20 20 72 65 74  Failed);.    ret
a57d0 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e  urn;.  }.  if( N
a57e0 3c 30 20 29 7b 0a 20 20 20 20 4e 20 3d 20 73 71  <0 ){.    N = sq
a57f0 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29  lite3Strlen30(z)
a5800 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3d 3d 30  ;.  }.  if( N==0
a5810 20 7c 7c 20 4e 45 56 45 52 28 7a 3d 3d 30 29 20   || NEVER(z==0) 
a5820 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
a5830 20 7d 0a 20 20 69 66 28 20 70 2d 3e 6e 43 68 61   }.  if( p->nCha
a5840 72 2b 4e 20 3e 3d 20 70 2d 3e 6e 41 6c 6c 6f 63  r+N >= p->nAlloc
a5850 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4e   ){.    char *zN
a5860 65 77 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e  ew;.    if( !p->
a5870 75 73 65 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20  useMalloc ){.   
a5880 20 20 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 31     p->tooBig = 1
a5890 3b 0a 20 20 20 20 20 20 4e 20 3d 20 70 2d 3e 6e  ;.      N = p->n
a58a0 41 6c 6c 6f 63 20 2d 20 70 2d 3e 6e 43 68 61 72  Alloc - p->nChar
a58b0 20 2d 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20   - 1;.      if( 
a58c0 4e 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  N<=0 ){.        
a58d0 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a  return;.      }.
a58e0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
a58f0 20 69 36 34 20 73 7a 4e 65 77 20 3d 20 70 2d 3e   i64 szNew = p->
a5900 6e 43 68 61 72 3b 0a 20 20 20 20 20 20 73 7a 4e  nChar;.      szN
a5910 65 77 20 2b 3d 20 4e 20 2b 20 31 3b 0a 20 20 20  ew += N + 1;.   
a5920 20 20 20 69 66 28 20 73 7a 4e 65 77 20 3e 20 70     if( szNew > p
a5930 2d 3e 6d 78 41 6c 6c 6f 63 20 29 7b 0a 20 20 20  ->mxAlloc ){.   
a5940 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41       sqlite3StrA
a5950 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a 20 20  ccumReset(p);.  
a5960 20 20 20 20 20 20 70 2d 3e 74 6f 6f 42 69 67 20        p->tooBig 
a5970 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 72 65 74  = 1;.        ret
a5980 75 72 6e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  urn;.      }else
a5990 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e 41 6c  {.        p->nAl
a59a0 6c 6f 63 20 3d 20 28 69 6e 74 29 73 7a 4e 65 77  loc = (int)szNew
a59b0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
a59c0 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62  zNew = sqlite3Db
a59d0 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c  MallocRaw(p->db,
a59e0 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20   p->nAlloc );.  
a59f0 20 20 20 20 69 66 28 20 7a 4e 65 77 20 29 7b 0a      if( zNew ){.
a5a00 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a          memcpy(z
a5a10 4e 65 77 2c 20 70 2d 3e 7a 54 65 78 74 2c 20 70  New, p->zText, p
a5a20 2d 3e 6e 43 68 61 72 29 3b 0a 20 20 20 20 20 20  ->nChar);.      
a5a30 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
a5a40 6d 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20 20  mReset(p);.     
a5a50 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 7a 4e     p->zText = zN
a5a60 65 77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ew;.      }else{
a5a70 0a 20 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c 6c  .        p->mall
a5a80 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
a5a90 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
a5aa0 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a 20  AccumReset(p);. 
a5ab0 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
a5ac0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
a5ad0 0a 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 7a 54  .  memcpy(&p->zT
a5ae0 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d 2c 20 7a  ext[p->nChar], z
a5af0 2c 20 4e 29 3b 0a 20 20 70 2d 3e 6e 43 68 61 72  , N);.  p->nChar
a5b00 20 2b 3d 20 4e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   += N;.}../*.** 
a5b10 46 69 6e 69 73 68 20 6f 66 66 20 61 20 73 74 72  Finish off a str
a5b20 69 6e 67 20 62 79 20 6d 61 6b 69 6e 67 20 73 75  ing by making su
a5b30 72 65 20 69 74 20 69 73 20 7a 65 72 6f 2d 74 65  re it is zero-te
a5b40 72 6d 69 6e 61 74 65 64 2e 0a 2a 2a 20 52 65 74  rminated..** Ret
a5b50 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
a5b60 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 73   the resulting s
a5b70 74 72 69 6e 67 2e 20 20 52 65 74 75 72 6e 20 61  tring.  Return a
a5b80 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65 72   NULL.** pointer
a5b90 20 69 66 20 61 6e 79 20 6b 69 6e 64 20 6f 66 20   if any kind of 
a5ba0 65 72 72 6f 72 20 77 61 73 20 65 6e 63 6f 75 6e  error was encoun
a5bb0 74 65 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  tered..*/.SQLITE
a5bc0 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
a5bd0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69  qlite3StrAccumFi
a5be0 6e 69 73 68 28 53 74 72 41 63 63 75 6d 20 2a 70  nish(StrAccum *p
a5bf0 29 7b 0a 20 20 69 66 28 20 70 2d 3e 7a 54 65 78  ){.  if( p->zTex
a5c00 74 20 29 7b 0a 20 20 20 20 70 2d 3e 7a 54 65 78  t ){.    p->zTex
a5c10 74 5b 70 2d 3e 6e 43 68 61 72 5d 20 3d 20 30 3b  t[p->nChar] = 0;
a5c20 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 73 65 4d  .    if( p->useM
a5c30 61 6c 6c 6f 63 20 26 26 20 70 2d 3e 7a 54 65 78  alloc && p->zTex
a5c40 74 3d 3d 70 2d 3e 7a 42 61 73 65 20 29 7b 0a 20  t==p->zBase ){. 
a5c50 20 20 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20       p->zText = 
a5c60 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
a5c70 61 77 28 70 2d 3e 64 62 2c 20 70 2d 3e 6e 43 68  aw(p->db, p->nCh
a5c80 61 72 2b 31 20 29 3b 0a 20 20 20 20 20 20 69 66  ar+1 );.      if
a5c90 28 20 70 2d 3e 7a 54 65 78 74 20 29 7b 0a 20 20  ( p->zText ){.  
a5ca0 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2d 3e        memcpy(p->
a5cb0 7a 54 65 78 74 2c 20 70 2d 3e 7a 42 61 73 65 2c  zText, p->zBase,
a5cc0 20 70 2d 3e 6e 43 68 61 72 2b 31 29 3b 0a 20 20   p->nChar+1);.  
a5cd0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
a5ce0 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c     p->mallocFail
a5cf0 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  ed = 1;.      }.
a5d00 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
a5d10 72 6e 20 70 2d 3e 7a 54 65 78 74 3b 0a 7d 0a 0a  rn p->zText;.}..
a5d20 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 61 6e 20 53  /*.** Reset an S
a5d30 74 72 41 63 63 75 6d 20 73 74 72 69 6e 67 2e 20  trAccum string. 
a5d40 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 61 6c   Reclaim all mal
a5d50 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2f  loced memory..*/
a5d60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a5d70 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72 41  void sqlite3StrA
a5d80 63 63 75 6d 52 65 73 65 74 28 53 74 72 41 63 63  ccumReset(StrAcc
a5d90 75 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d  um *p){.  if( p-
a5da0 3e 7a 54 65 78 74 21 3d 70 2d 3e 7a 42 61 73 65  >zText!=p->zBase
a5db0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
a5dc0 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e  bFree(p->db, p->
a5dd0 7a 54 65 78 74 29 3b 0a 20 20 7d 0a 20 20 70 2d  zText);.  }.  p-
a5de0 3e 7a 54 65 78 74 20 3d 20 30 3b 0a 7d 0a 0a 2f  >zText = 0;.}../
a5df0 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
a5e00 61 20 73 74 72 69 6e 67 20 61 63 63 75 6d 75 6c  a string accumul
a5e10 61 74 6f 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ator.*/.SQLITE_P
a5e20 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
a5e30 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28  te3StrAccumInit(
a5e40 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 63 68 61  StrAccum *p, cha
a5e50 72 20 2a 7a 42 61 73 65 2c 20 69 6e 74 20 6e 2c  r *zBase, int n,
a5e60 20 69 6e 74 20 6d 78 29 7b 0a 20 20 70 2d 3e 7a   int mx){.  p->z
a5e70 54 65 78 74 20 3d 20 70 2d 3e 7a 42 61 73 65 20  Text = p->zBase 
a5e80 3d 20 7a 42 61 73 65 3b 0a 20 20 70 2d 3e 64 62  = zBase;.  p->db
a5e90 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 43 68 61 72   = 0;.  p->nChar
a5ea0 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 41 6c 6c 6f   = 0;.  p->nAllo
a5eb0 63 20 3d 20 6e 3b 0a 20 20 70 2d 3e 6d 78 41 6c  c = n;.  p->mxAl
a5ec0 6c 6f 63 20 3d 20 6d 78 3b 0a 20 20 70 2d 3e 75  loc = mx;.  p->u
a5ed0 73 65 4d 61 6c 6c 6f 63 20 3d 20 31 3b 0a 20 20  seMalloc = 1;.  
a5ee0 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 30 3b 0a 20  p->tooBig = 0;. 
a5ef0 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64   p->mallocFailed
a5f00 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50   = 0;.}../*.** P
a5f10 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72 79  rint into memory
a5f20 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
a5f30 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20  qliteMalloc().  
a5f40 55 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  Use the internal
a5f50 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e  .** %-conversion
a5f60 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a   extensions..*/.
a5f70 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
a5f80 68 61 72 20 2a 73 71 6c 69 74 65 33 56 4d 50 72  har *sqlite3VMPr
a5f90 69 6e 74 66 28 73 71 6c 69 74 65 33 20 2a 64 62  intf(sqlite3 *db
a5fa0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
a5fb0 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73 74 20 61  ormat, va_list a
a5fc0 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20  p){.  char *z;. 
a5fd0 20 63 68 61 72 20 7a 42 61 73 65 5b 53 51 4c 49   char zBase[SQLI
a5fe0 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a  TE_PRINT_BUF_SIZ
a5ff0 45 5d 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61  E];.  StrAccum a
a6000 63 63 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62  cc;.  assert( db
a6010 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  !=0 );.  sqlite3
a6020 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63  StrAccumInit(&ac
a6030 63 2c 20 7a 42 61 73 65 2c 20 73 69 7a 65 6f 66  c, zBase, sizeof
a6040 28 7a 42 61 73 65 29 2c 0a 20 20 20 20 20 20 20  (zBase),.       
a6050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
a6060 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
a6070 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 29 3b  _LIMIT_LENGTH]);
a6080 0a 20 20 61 63 63 2e 64 62 20 3d 20 64 62 3b 0a  .  acc.db = db;.
a6090 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74    sqlite3VXPrint
a60a0 66 28 26 61 63 63 2c 20 31 2c 20 7a 46 6f 72 6d  f(&acc, 1, zForm
a60b0 61 74 2c 20 61 70 29 3b 0a 20 20 7a 20 3d 20 73  at, ap);.  z = s
a60c0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69  qlite3StrAccumFi
a60d0 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 69 66  nish(&acc);.  if
a60e0 28 20 61 63 63 2e 6d 61 6c 6c 6f 63 46 61 69 6c  ( acc.mallocFail
a60f0 65 64 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61  ed ){.    db->ma
a6100 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
a6110 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a    }.  return z;.
a6120 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69  }../*.** Print i
a6130 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69  nto memory obtai
a6140 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d  ned from sqliteM
a6150 61 6c 6c 6f 63 28 29 2e 20 20 55 73 65 20 74 68  alloc().  Use th
a6160 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d  e internal.** %-
a6170 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e  conversion exten
a6180 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  sions..*/.SQLITE
a6190 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73  _PRIVATE char *s
a61a0 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 73 71  qlite3MPrintf(sq
a61b0 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
a61c0 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20   char *zFormat, 
a61d0 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
a61e0 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20  ap;.  char *z;. 
a61f0 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46   va_start(ap, zF
a6200 6f 72 6d 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71  ormat);.  z = sq
a6210 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62  lite3VMPrintf(db
a6220 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a  , zFormat, ap);.
a6230 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
a6240 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a  return z;.}../*.
a6250 2a 2a 20 4c 69 6b 65 20 73 71 6c 69 74 65 33 4d  ** Like sqlite3M
a6260 50 72 69 6e 74 66 28 29 2c 20 62 75 74 20 63 61  Printf(), but ca
a6270 6c 6c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  ll sqlite3DbFree
a6280 28 29 20 6f 6e 20 7a 53 74 72 20 61 66 74 65 72  () on zStr after
a6290 20 66 6f 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 74   formatting.** t
a62a0 68 65 20 73 74 72 69 6e 67 20 61 6e 64 20 62 65  he string and be
a62b0 66 6f 72 65 20 72 65 74 75 72 6e 6e 69 6e 67 2e  fore returnning.
a62c0 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69    This routine i
a62d0 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65  s intended to be
a62e0 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6d 6f 64 69   used.** to modi
a62f0 66 79 20 61 6e 20 65 78 69 73 74 69 6e 67 20 73  fy an existing s
a6300 74 72 69 6e 67 2e 20 20 46 6f 72 20 65 78 61 6d  tring.  For exam
a6310 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ple:.**.**      
a6320 20 78 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69   x = sqlite3MPri
a6330 6e 74 66 28 64 62 2c 20 78 2c 20 22 70 72 65 66  ntf(db, x, "pref
a6340 69 78 20 25 73 20 73 75 66 66 69 78 22 2c 20 78  ix %s suffix", x
a6350 29 3b 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f  );.**.*/.SQLITE_
a6360 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71  PRIVATE char *sq
a6370 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 73 71  lite3MAppendf(sq
a6380 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20  lite3 *db, char 
a6390 2a 7a 53 74 72 2c 20 63 6f 6e 73 74 20 63 68 61  *zStr, const cha
a63a0 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29  r *zFormat, ...)
a63b0 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a  {.  va_list ap;.
a63c0 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f    char *z;.  va_
a63d0 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61  start(ap, zForma
a63e0 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65  t);.  z = sqlite
a63f0 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46  3VMPrintf(db, zF
a6400 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61  ormat, ap);.  va
a6410 5f 65 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69  _end(ap);.  sqli
a6420 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53  te3DbFree(db, zS
a6430 74 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b  tr);.  return z;
a6440 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20  .}../*.** Print 
a6450 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  into memory obta
a6460 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
a6470 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 20 4f 6d 69  3_malloc().  Omi
a6480 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a  t the internal.*
a6490 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65  * %-conversion e
a64a0 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51  xtensions..*/.SQ
a64b0 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73  LITE_API char *s
a64c0 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 28  qlite3_vmprintf(
a64d0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
a64e0 6d 61 74 2c 20 76 61 5f 6c 69 73 74 20 61 70 29  mat, va_list ap)
a64f0 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63  {.  char *z;.  c
a6500 68 61 72 20 7a 42 61 73 65 5b 53 51 4c 49 54 45  har zBase[SQLITE
a6510 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 5d  _PRINT_BUF_SIZE]
a6520 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63 63  ;.  StrAccum acc
a6530 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
a6540 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20  _OMIT_AUTOINIT. 
a6550 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e 69   if( sqlite3_ini
a6560 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65 74 75  tialize() ) retu
a6570 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 73  rn 0;.#endif.  s
a6580 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e  qlite3StrAccumIn
a6590 69 74 28 26 61 63 63 2c 20 7a 42 61 73 65 2c 20  it(&acc, zBase, 
a65a0 73 69 7a 65 6f 66 28 7a 42 61 73 65 29 2c 20 53  sizeof(zBase), S
a65b0 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48  QLITE_MAX_LENGTH
a65c0 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 58 50 72  );.  sqlite3VXPr
a65d0 69 6e 74 66 28 26 61 63 63 2c 20 30 2c 20 7a 46  intf(&acc, 0, zF
a65e0 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 7a 20  ormat, ap);.  z 
a65f0 3d 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75  = sqlite3StrAccu
a6600 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 20  mFinish(&acc);. 
a6610 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a   return z;.}../*
a6620 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d  .** Print into m
a6630 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
a6640 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  rom sqlite3_mall
a6650 6f 63 28 29 28 29 2e 20 20 4f 6d 69 74 20 74 68  oc()().  Omit th
a6660 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d  e internal.** %-
a6670 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65 6e  conversion exten
a6680 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  sions..*/.SQLITE
a6690 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74  _API char *sqlit
a66a0 65 33 5f 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74  e3_mprintf(const
a66b0 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20   char *zFormat, 
a66c0 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
a66d0 61 70 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 23  ap;.  char *z;.#
a66e0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
a66f0 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66  IT_AUTOINIT.  if
a6700 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  ( sqlite3_initia
a6710 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20  lize() ) return 
a6720 30 3b 0a 23 65 6e 64 69 66 0a 20 20 76 61 5f 73  0;.#endif.  va_s
a6730 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74  tart(ap, zFormat
a6740 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33  );.  z = sqlite3
a6750 5f 76 6d 70 72 69 6e 74 66 28 7a 46 6f 72 6d 61  _vmprintf(zForma
a6760 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64  t, ap);.  va_end
a6770 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a  (ap);.  return z
a6780 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 73 71 6c 69 74  ;.}../*.** sqlit
a6790 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 77 6f  e3_snprintf() wo
a67a0 72 6b 73 20 6c 69 6b 65 20 73 6e 70 72 69 6e 74  rks like snprint
a67b0 66 28 29 20 65 78 63 65 70 74 20 74 68 61 74 20  f() except that 
a67c0 69 74 20 69 67 6e 6f 72 65 73 20 74 68 65 0a 2a  it ignores the.*
a67d0 2a 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 6c 65  * current locale
a67e0 20 73 65 74 74 69 6e 67 73 2e 20 20 54 68 69 73   settings.  This
a67f0 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 66 6f   is important fo
a6800 72 20 53 51 4c 69 74 65 20 62 65 63 61 75 73 65  r SQLite because
a6810 20 77 65 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 61   we.** are not a
a6820 62 6c 65 20 74 6f 20 75 73 65 20 61 20 22 2c 22  ble to use a ","
a6830 20 61 73 20 74 68 65 20 64 65 63 69 6d 61 6c 20   as the decimal 
a6840 70 6f 69 6e 74 20 69 6e 20 70 6c 61 63 65 20 6f  point in place o
a6850 66 20 22 2e 22 20 61 73 0a 2a 2a 20 73 70 65 63  f "." as.** spec
a6860 69 66 69 65 64 20 62 79 20 73 6f 6d 65 20 6c 6f  ified by some lo
a6870 63 61 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cales..*/.SQLITE
a6880 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74  _API char *sqlit
a6890 65 33 5f 73 6e 70 72 69 6e 74 66 28 69 6e 74 20  e3_snprintf(int 
a68a0 6e 2c 20 63 68 61 72 20 2a 7a 42 75 66 2c 20 63  n, char *zBuf, c
a68b0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d  onst char *zForm
a68c0 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 63 68 61 72  at, ...){.  char
a68d0 20 2a 7a 3b 0a 20 20 76 61 5f 6c 69 73 74 20 61   *z;.  va_list a
a68e0 70 3b 0a 20 20 53 74 72 41 63 63 75 6d 20 61 63  p;.  StrAccum ac
a68f0 63 3b 0a 0a 20 20 69 66 28 20 6e 3c 3d 30 20 29  c;..  if( n<=0 )
a6900 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 7a 42 75  {.    return zBu
a6910 66 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  f;.  }.  sqlite3
a6920 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63  StrAccumInit(&ac
a6930 63 2c 20 7a 42 75 66 2c 20 6e 2c 20 30 29 3b 0a  c, zBuf, n, 0);.
a6940 20 20 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20    acc.useMalloc 
a6950 3d 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28  = 0;.  va_start(
a6960 61 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 73  ap,zFormat);.  s
a6970 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 26  qlite3VXPrintf(&
a6980 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 2c  acc, 0, zFormat,
a6990 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61   ap);.  va_end(a
a69a0 70 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65  p);.  z = sqlite
a69b0 33 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28  3StrAccumFinish(
a69c0 26 61 63 63 29 3b 0a 20 20 72 65 74 75 72 6e 20  &acc);.  return 
a69d0 7a 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65  z;.}..#if define
a69e0 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a  d(SQLITE_DEBUG).
a69f0 2f 2a 0a 2a 2a 20 41 20 76 65 72 73 69 6f 6e 20  /*.** A version 
a6a00 6f 66 20 70 72 69 6e 74 66 28 29 20 74 68 61 74  of printf() that
a6a10 20 75 6e 64 65 72 73 74 61 6e 64 73 20 25 6c 6c   understands %ll
a6a20 64 2e 20 20 55 73 65 64 20 66 6f 72 20 64 65 62  d.  Used for deb
a6a30 75 67 67 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 70  ugging..** The p
a6a40 72 69 6e 74 66 28 29 20 62 75 69 6c 74 20 69 6e  rintf() built in
a6a50 74 6f 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73  to some versions
a6a60 20 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 65 73   of windows does
a6a70 20 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20   not understand 
a6a80 25 6c 6c 64 0a 2a 2a 20 61 6e 64 20 73 65 67 66  %lld.** and segf
a6a90 61 75 6c 74 73 20 69 66 20 79 6f 75 20 67 69 76  aults if you giv
a6aa0 65 20 69 74 20 61 20 6c 6f 6e 67 20 6c 6f 6e 67  e it a long long
a6ab0 20 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   int..*/.SQLITE_
a6ac0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
a6ad0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
a6ae0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72  const char *zFor
a6af0 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f  mat, ...){.  va_
a6b00 6c 69 73 74 20 61 70 3b 0a 20 20 53 74 72 41 63  list ap;.  StrAc
a6b10 63 75 6d 20 61 63 63 3b 0a 20 20 63 68 61 72 20  cum acc;.  char 
a6b20 7a 42 75 66 5b 35 30 30 5d 3b 0a 20 20 73 71 6c  zBuf[500];.  sql
a6b30 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74  ite3StrAccumInit
a6b40 28 26 61 63 63 2c 20 7a 42 75 66 2c 20 73 69 7a  (&acc, zBuf, siz
a6b50 65 6f 66 28 7a 42 75 66 29 2c 20 30 29 3b 0a 20  eof(zBuf), 0);. 
a6b60 20 61 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d   acc.useMalloc =
a6b70 20 30 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61   0;.  va_start(a
a6b80 70 2c 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 73 71  p,zFormat);.  sq
a6b90 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28 26 61  lite3VXPrintf(&a
a6ba0 63 63 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20  cc, 0, zFormat, 
a6bb0 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  ap);.  va_end(ap
a6bc0 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41  );.  sqlite3StrA
a6bd0 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63 29  ccumFinish(&acc)
a6be0 3b 0a 20 20 66 70 72 69 6e 74 66 28 73 74 64 6f  ;.  fprintf(stdo
a6bf0 75 74 2c 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a  ut,"%s", zBuf);.
a6c00 20 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29    fflush(stdout)
a6c10 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a  ;.}.#endif../***
a6c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
a6c30 6f 66 20 70 72 69 6e 74 66 2e 63 20 2a 2a 2a 2a  of printf.c ****
a6c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
a6c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
a6c80 6e 20 66 69 6c 65 20 72 61 6e 64 6f 6d 2e 63 20  n file random.c 
a6c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
a6cc0 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
a6cd0 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   15.**.** The au
a6ce0 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
a6cf0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
a6d00 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
a6d10 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
a6d20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
a6d30 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
a6d40 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
a6d50 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
a6d60 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
a6d70 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
a6d80 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
a6d90 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
a6da0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
a6db0 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
a6dc0 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
a6dd0 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
a6de0 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
a6df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a6e30 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
a6e40 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 6f  contains code to
a6e50 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 70 73 65   implement a pse
a6e60 75 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65  udo-random numbe
a6e70 72 0a 2a 2a 20 67 65 6e 65 72 61 74 6f 72 20 28  r.** generator (
a6e80 50 52 4e 47 29 20 66 6f 72 20 53 51 4c 69 74 65  PRNG) for SQLite
a6e90 2e 0a 2a 2a 0a 2a 2a 20 52 61 6e 64 6f 6d 20 6e  ..**.** Random n
a6ea0 75 6d 62 65 72 73 20 61 72 65 20 75 73 65 64 20  umbers are used 
a6eb0 62 79 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 64  by some of the d
a6ec0 61 74 61 62 61 73 65 20 62 61 63 6b 65 6e 64 73  atabase backends
a6ed0 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20   in order.** to 
a6ee0 67 65 6e 65 72 61 74 65 20 72 61 6e 64 6f 6d 20  generate random 
a6ef0 69 6e 74 65 67 65 72 20 6b 65 79 73 20 66 6f 72  integer keys for
a6f00 20 74 61 62 6c 65 73 20 6f 72 20 72 61 6e 64 6f   tables or rando
a6f10 6d 20 66 69 6c 65 6e 61 6d 65 73 2e 0a 2a 2a 0a  m filenames..**.
a6f20 2a 2a 20 24 49 64 3a 20 72 61 6e 64 6f 6d 2e 63  ** $Id: random.c
a6f30 2c 76 20 31 2e 32 39 20 32 30 30 38 2f 31 32 2f  ,v 1.29 2008/12/
a6f40 31 30 20 31 39 3a 32 36 3a 32 34 20 64 72 68 20  10 19:26:24 drh 
a6f50 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6c  Exp $.*/.../* Al
a6f60 6c 20 74 68 72 65 61 64 73 20 73 68 61 72 65 20  l threads share 
a6f70 61 20 73 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 20  a single random 
a6f80 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72  number generator
a6f90 2e 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74  ..** This struct
a6fa0 75 72 65 20 69 73 20 74 68 65 20 63 75 72 72 65  ure is the curre
a6fb0 6e 74 20 73 74 61 74 65 20 6f 66 20 74 68 65 20  nt state of the 
a6fc0 67 65 6e 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74  generator..*/.st
a6fd0 61 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20  atic SQLITE_WSD 
a6fe0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72  struct sqlite3Pr
a6ff0 6e 67 54 79 70 65 20 7b 0a 20 20 75 6e 73 69 67  ngType {.  unsig
a7000 6e 65 64 20 63 68 61 72 20 69 73 49 6e 69 74 3b  ned char isInit;
a7010 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
a7020 65 20 69 66 20 69 6e 69 74 69 61 6c 69 7a 65 64  e if initialized
a7030 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   */.  unsigned c
a7040 68 61 72 20 69 2c 20 6a 3b 20 20 20 20 20 20 20  har i, j;       
a7050 20 20 20 20 20 2f 2a 20 53 74 61 74 65 20 76 61       /* State va
a7060 72 69 61 62 6c 65 73 20 2a 2f 0a 20 20 75 6e 73  riables */.  uns
a7070 69 67 6e 65 64 20 63 68 61 72 20 73 5b 32 35 36  igned char s[256
a7080 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  ];          /* S
a7090 74 61 74 65 20 76 61 72 69 61 62 6c 65 73 20 2a  tate variables *
a70a0 2f 0a 7d 20 73 71 6c 69 74 65 33 50 72 6e 67 3b  /.} sqlite3Prng;
a70b0 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 73 69  ../*.** Get a si
a70c0 6e 67 6c 65 20 38 2d 62 69 74 20 72 61 6e 64 6f  ngle 8-bit rando
a70d0 6d 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65  m value from the
a70e0 20 52 43 34 20 50 52 4e 47 2e 20 20 54 68 65 20   RC4 PRNG.  The 
a70f0 4d 75 74 65 78 0a 2a 2a 20 6d 75 73 74 20 62 65  Mutex.** must be
a7100 20 68 65 6c 64 20 77 68 69 6c 65 20 65 78 65 63   held while exec
a7110 75 74 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69  uting this routi
a7120 6e 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 79 20 6e 6f  ne..**.** Why no
a7130 74 20 6a 75 73 74 20 75 73 65 20 61 20 6c 69 62  t just use a lib
a7140 72 61 72 79 20 72 61 6e 64 6f 6d 20 67 65 6e 65  rary random gene
a7150 72 61 74 6f 72 20 6c 69 6b 65 20 6c 72 61 6e 64  rator like lrand
a7160 34 38 28 29 20 66 6f 72 20 74 68 69 73 3f 0a 2a  48() for this?.*
a7170 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 4f 50  * Because the OP
a7180 5f 4e 65 77 52 6f 77 69 64 20 6f 70 63 6f 64 65  _NewRowid opcode
a7190 20 69 6e 20 74 68 65 20 56 44 42 45 20 64 65 70   in the VDBE dep
a71a0 65 6e 64 73 20 6f 6e 20 68 61 76 69 6e 67 20 61  ends on having a
a71b0 20 76 65 72 79 0a 2a 2a 20 67 6f 6f 64 20 73 6f   very.** good so
a71c0 75 72 63 65 20 6f 66 20 72 61 6e 64 6f 6d 20 6e  urce of random n
a71d0 75 6d 62 65 72 73 2e 20 20 54 68 65 20 6c 72 61  umbers.  The lra
a71e0 6e 64 34 38 28 29 20 6c 69 62 72 61 72 79 20 66  nd48() library f
a71f0 75 6e 63 74 69 6f 6e 20 6d 61 79 0a 2a 2a 20 77  unction may.** w
a7200 65 6c 6c 20 62 65 20 67 6f 6f 64 20 65 6e 6f 75  ell be good enou
a7210 67 68 2e 20 20 42 75 74 20 6d 61 79 62 65 20 6e  gh.  But maybe n
a7220 6f 74 2e 20 20 4f 72 20 6d 61 79 62 65 20 6c 72  ot.  Or maybe lr
a7230 61 6e 64 34 38 28 29 20 68 61 73 20 73 6f 6d 65  and48() has some
a7240 0a 2a 2a 20 73 75 62 74 6c 65 20 70 72 6f 62 6c  .** subtle probl
a7250 65 6d 73 20 6f 6e 20 73 6f 6d 65 20 73 79 73 74  ems on some syst
a7260 65 6d 73 20 74 68 61 74 20 63 6f 75 6c 64 20 63  ems that could c
a7270 61 75 73 65 20 70 72 6f 62 6c 65 6d 73 2e 20 20  ause problems.  
a7280 49 74 20 69 73 20 68 61 72 64 0a 2a 2a 20 74 6f  It is hard.** to
a7290 20 6b 6e 6f 77 2e 20 20 54 6f 20 6d 69 6e 69 6d   know.  To minim
a72a0 69 7a 65 20 74 68 65 20 72 69 73 6b 20 6f 66 20  ize the risk of 
a72b0 70 72 6f 62 6c 65 6d 73 20 64 75 65 20 74 6f 20  problems due to 
a72c0 62 61 64 20 6c 72 61 6e 64 34 38 28 29 0a 2a 2a  bad lrand48().**
a72d0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
a72e0 2c 20 53 51 4c 69 74 65 20 75 73 65 73 20 74 68  , SQLite uses th
a72f0 69 73 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72  is random number
a7300 20 67 65 6e 65 72 61 74 6f 72 20 62 61 73 65 64   generator based
a7310 0a 2a 2a 20 6f 6e 20 52 43 34 2c 20 77 68 69 63  .** on RC4, whic
a7320 68 20 77 65 20 6b 6e 6f 77 20 77 6f 72 6b 73 20  h we know works 
a7330 76 65 72 79 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a  very well..**.**
a7340 20 28 4c 61 74 65 72 29 3a 20 20 41 63 74 75 61   (Later):  Actua
a7350 6c 6c 79 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64  lly, OP_NewRowid
a7360 20 64 6f 65 73 20 6e 6f 74 20 64 65 70 65 6e 64   does not depend
a7370 20 6f 6e 20 61 20 67 6f 6f 64 20 73 6f 75 72 63   on a good sourc
a7380 65 20 6f 66 0a 2a 2a 20 72 61 6e 64 6f 6d 6e 65  e of.** randomne
a7390 73 73 20 61 6e 79 20 6d 6f 72 65 2e 20 20 42 75  ss any more.  Bu
a73a0 74 20 77 65 20 77 69 6c 6c 20 6c 65 61 76 65 20  t we will leave 
a73b0 74 68 69 73 20 63 6f 64 65 20 69 6e 20 61 6c 6c  this code in all
a73c0 20 74 68 65 20 73 61 6d 65 2e 0a 2a 2f 0a 73 74   the same..*/.st
a73d0 61 74 69 63 20 75 38 20 72 61 6e 64 6f 6d 42 79  atic u8 randomBy
a73e0 74 65 28 76 6f 69 64 29 7b 0a 20 20 75 6e 73 69  te(void){.  unsi
a73f0 67 6e 65 64 20 63 68 61 72 20 74 3b 0a 0a 0a 20  gned char t;... 
a7400 20 2f 2a 20 54 68 65 20 22 77 73 64 50 72 6e 67   /* The "wsdPrng
a7410 22 20 6d 61 63 72 6f 20 77 69 6c 6c 20 72 65 73  " macro will res
a7420 6f 6c 76 65 20 74 6f 20 74 68 65 20 70 73 65 75  olve to the pseu
a7430 64 6f 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72  do-random number
a7440 20 67 65 6e 65 72 61 74 6f 72 0a 20 20 2a 2a 20   generator.  ** 
a7450 73 74 61 74 65 20 76 65 63 74 6f 72 2e 20 20 49  state vector.  I
a7460 66 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69  f writable stati
a7470 63 20 64 61 74 61 20 69 73 20 75 6e 73 75 70 70  c data is unsupp
a7480 6f 72 74 65 64 20 6f 6e 20 74 68 65 20 74 61 72  orted on the tar
a7490 67 65 74 2c 0a 20 20 2a 2a 20 77 65 20 68 61 76  get,.  ** we hav
a74a0 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20  e to locate the 
a74b0 73 74 61 74 65 20 76 65 63 74 6f 72 20 61 74 20  state vector at 
a74c0 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74 68  run-time.  In th
a74d0 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 20 20  e more common.  
a74e0 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 77 72  ** case where wr
a74f0 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61  itable static da
a7500 74 61 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c  ta is supported,
a7510 20 77 73 64 50 72 6e 67 20 63 61 6e 20 72 65 66   wsdPrng can ref
a7520 65 72 20 64 69 72 65 63 74 6c 79 0a 20 20 2a 2a  er directly.  **
a7530 20 74 6f 20 74 68 65 20 22 73 71 6c 69 74 65 33   to the "sqlite3
a7540 50 72 6e 67 22 20 73 74 61 74 65 20 76 65 63 74  Prng" state vect
a7550 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f 76  or declared abov
a7560 65 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53  e..  */.#ifdef S
a7570 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 20  QLITE_OMIT_WSD. 
a7580 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 50   struct sqlite3P
a7590 72 6e 67 54 79 70 65 20 2a 70 20 3d 20 26 47 4c  rngType *p = &GL
a75a0 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69  OBAL(struct sqli
a75b0 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c  te3PrngType, sql
a75c0 69 74 65 33 50 72 6e 67 29 3b 0a 23 20 64 65 66  ite3Prng);.# def
a75d0 69 6e 65 20 77 73 64 50 72 6e 67 20 70 5b 30 5d  ine wsdPrng p[0]
a75e0 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
a75f0 77 73 64 50 72 6e 67 20 73 71 6c 69 74 65 33 50  wsdPrng sqlite3P
a7600 72 6e 67 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 2f  rng.#endif...  /
a7610 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
a7620 20 73 74 61 74 65 20 6f 66 20 74 68 65 20 72 61   state of the ra
a7630 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67 65 6e 65  ndom number gene
a7640 72 61 74 6f 72 20 6f 6e 63 65 2c 0a 20 20 2a 2a  rator once,.  **
a7650 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20   the first time 
a7660 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
a7670 63 61 6c 6c 65 64 2e 20 20 54 68 65 20 73 65 65  called.  The see
a7680 64 20 76 61 6c 75 65 20 64 6f 65 73 0a 20 20 2a  d value does.  *
a7690 2a 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 63 6f  * not need to co
a76a0 6e 74 61 69 6e 20 61 20 6c 6f 74 20 6f 66 20 72  ntain a lot of r
a76b0 61 6e 64 6f 6d 6e 65 73 73 20 73 69 6e 63 65 20  andomness since 
a76c0 77 65 20 61 72 65 20 6e 6f 74 0a 20 20 2a 2a 20  we are not.  ** 
a76d0 74 72 79 69 6e 67 20 74 6f 20 64 6f 20 73 65 63  trying to do sec
a76e0 75 72 65 20 65 6e 63 72 79 70 74 69 6f 6e 20 6f  ure encryption o
a76f0 72 20 61 6e 79 74 68 69 6e 67 20 6c 69 6b 65 20  r anything like 
a7700 74 68 61 74 2e 2e 2e 0a 20 20 2a 2a 0a 20 20 2a  that....  **.  *
a7710 2a 20 4e 6f 74 68 69 6e 67 20 69 6e 20 74 68 69  * Nothing in thi
a7720 73 20 66 69 6c 65 20 6f 72 20 61 6e 79 77 68 65  s file or anywhe
a7730 72 65 20 65 6c 73 65 20 69 6e 20 53 51 4c 69 74  re else in SQLit
a7740 65 20 64 6f 65 73 20 61 6e 79 20 6b 69 6e 64 20  e does any kind 
a7750 6f 66 0a 20 20 2a 2a 20 65 6e 63 72 79 70 74 69  of.  ** encrypti
a7760 6f 6e 2e 20 20 54 68 65 20 52 43 34 20 61 6c 67  on.  The RC4 alg
a7770 6f 72 69 74 68 6d 20 69 73 20 62 65 69 6e 67 20  orithm is being 
a7780 75 73 65 64 20 61 73 20 61 20 50 52 4e 47 20 28  used as a PRNG (
a7790 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d 0a 20 20  pseudo-random.  
a77a0 2a 2a 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 61  ** number genera
a77b0 74 6f 72 29 20 6e 6f 74 20 61 73 20 61 6e 20 65  tor) not as an e
a77c0 6e 63 72 79 70 74 69 6f 6e 20 64 65 76 69 63 65  ncryption device
a77d0 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 77 73  ..  */.  if( !ws
a77e0 64 50 72 6e 67 2e 69 73 49 6e 69 74 20 29 7b 0a  dPrng.isInit ){.
a77f0 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 63      int i;.    c
a7800 68 61 72 20 6b 5b 32 35 36 5d 3b 0a 20 20 20 20  har k[256];.    
a7810 77 73 64 50 72 6e 67 2e 6a 20 3d 20 30 3b 0a 20  wsdPrng.j = 0;. 
a7820 20 20 20 77 73 64 50 72 6e 67 2e 69 20 3d 20 30     wsdPrng.i = 0
a7830 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 52  ;.    sqlite3OsR
a7840 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65  andomness(sqlite
a7850 33 5f 76 66 73 5f 66 69 6e 64 28 30 29 2c 20 32  3_vfs_find(0), 2
a7860 35 36 2c 20 6b 29 3b 0a 20 20 20 20 66 6f 72 28  56, k);.    for(
a7870 69 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b 2b 29  i=0; i<256; i++)
a7880 7b 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e  {.      wsdPrng.
a7890 73 5b 69 5d 20 3d 20 28 75 38 29 69 3b 0a 20 20  s[i] = (u8)i;.  
a78a0 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b    }.    for(i=0;
a78b0 20 69 3c 32 35 36 3b 20 69 2b 2b 29 7b 0a 20 20   i<256; i++){.  
a78c0 20 20 20 20 77 73 64 50 72 6e 67 2e 6a 20 2b 3d      wsdPrng.j +=
a78d0 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 20 2b 20   wsdPrng.s[i] + 
a78e0 6b 5b 69 5d 3b 0a 20 20 20 20 20 20 74 20 3d 20  k[i];.      t = 
a78f0 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e  wsdPrng.s[wsdPrn
a7900 67 2e 6a 5d 3b 0a 20 20 20 20 20 20 77 73 64 50  g.j];.      wsdP
a7910 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a 5d  rng.s[wsdPrng.j]
a7920 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d 3b   = wsdPrng.s[i];
a7930 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e 73  .      wsdPrng.s
a7940 5b 69 5d 20 3d 20 74 3b 0a 20 20 20 20 7d 0a 20  [i] = t;.    }. 
a7950 20 20 20 77 73 64 50 72 6e 67 2e 69 73 49 6e 69     wsdPrng.isIni
a7960 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  t = 1;.  }..  /*
a7970 20 47 65 6e 65 72 61 74 65 20 61 6e 64 20 72 65   Generate and re
a7980 74 75 72 6e 20 73 69 6e 67 6c 65 20 72 61 6e 64  turn single rand
a7990 6f 6d 20 62 79 74 65 0a 20 20 2a 2f 0a 20 20 77  om byte.  */.  w
a79a0 73 64 50 72 6e 67 2e 69 2b 2b 3b 0a 20 20 74 20  sdPrng.i++;.  t 
a79b0 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64 50  = wsdPrng.s[wsdP
a79c0 72 6e 67 2e 69 5d 3b 0a 20 20 77 73 64 50 72 6e  rng.i];.  wsdPrn
a79d0 67 2e 6a 20 2b 3d 20 74 3b 0a 20 20 77 73 64 50  g.j += t;.  wsdP
a79e0 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 69 5d  rng.s[wsdPrng.i]
a79f0 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64   = wsdPrng.s[wsd
a7a00 50 72 6e 67 2e 6a 5d 3b 0a 20 20 77 73 64 50 72  Prng.j];.  wsdPr
a7a10 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 6a 5d 20  ng.s[wsdPrng.j] 
a7a20 3d 20 74 3b 0a 20 20 74 20 2b 3d 20 77 73 64 50  = t;.  t += wsdP
a7a30 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e 69 5d  rng.s[wsdPrng.i]
a7a40 3b 0a 20 20 72 65 74 75 72 6e 20 77 73 64 50 72  ;.  return wsdPr
a7a50 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a  ng.s[t];.}../*.*
a7a60 2a 20 52 65 74 75 72 6e 20 4e 20 72 61 6e 64 6f  * Return N rando
a7a70 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a 53 51 4c 49  m bytes..*/.SQLI
a7a80 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
a7a90 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 69  te3_randomness(i
a7aa0 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 70 42 75 66  nt N, void *pBuf
a7ab0 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ){.  unsigned ch
a7ac0 61 72 20 2a 7a 42 75 66 20 3d 20 70 42 75 66 3b  ar *zBuf = pBuf;
a7ad0 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
a7ae0 41 44 53 41 46 45 0a 20 20 73 71 6c 69 74 65 33  ADSAFE.  sqlite3
a7af0 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20  _mutex *mutex = 
a7b00 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
a7b10 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
a7b20 54 41 54 49 43 5f 50 52 4e 47 29 3b 0a 23 65 6e  TATIC_PRNG);.#en
a7b30 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  dif.  sqlite3_mu
a7b40 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29  tex_enter(mutex)
a7b50 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29  ;.  while( N-- )
a7b60 7b 0a 20 20 20 20 2a 28 7a 42 75 66 2b 2b 29 20  {.    *(zBuf++) 
a7b70 3d 20 72 61 6e 64 6f 6d 42 79 74 65 28 29 3b 0a  = randomByte();.
a7b80 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
a7b90 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29  tex_leave(mutex)
a7ba0 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
a7bb0 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e  ITE_OMIT_BUILTIN
a7bc0 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 46 6f 72 20  _TEST./*.** For 
a7bd0 74 65 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73  testing purposes
a7be0 2c 20 77 65 20 73 6f 6d 65 74 69 6d 65 73 20 77  , we sometimes w
a7bf0 61 6e 74 20 74 6f 20 70 72 65 73 65 72 76 65 20  ant to preserve 
a7c00 74 68 65 20 73 74 61 74 65 20 6f 66 0a 2a 2a 20  the state of.** 
a7c10 50 52 4e 47 20 61 6e 64 20 72 65 73 74 6f 72 65  PRNG and restore
a7c20 20 74 68 65 20 50 52 4e 47 20 74 6f 20 69 74 73   the PRNG to its
a7c30 20 73 61 76 65 64 20 73 74 61 74 65 20 61 74 20   saved state at 
a7c40 61 20 6c 61 74 65 72 20 74 69 6d 65 2c 20 6f 72  a later time, or
a7c50 0a 2a 2a 20 74 6f 20 72 65 73 65 74 20 74 68 65  .** to reset the
a7c60 20 50 52 4e 47 20 74 6f 20 69 74 73 20 69 6e 69   PRNG to its ini
a7c70 74 69 61 6c 20 73 74 61 74 65 2e 20 20 54 68 65  tial state.  The
a7c80 73 65 20 72 6f 75 74 69 6e 65 73 20 61 63 63 6f  se routines acco
a7c90 6d 70 6c 69 73 68 0a 2a 2a 20 74 68 6f 73 65 20  mplish.** those 
a7ca0 74 61 73 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tasks..**.** The
a7cb0 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f   sqlite3_test_co
a7cc0 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72 66 61 63  ntrol() interfac
a7cd0 65 20 63 61 6c 6c 73 20 74 68 65 73 65 20 72 6f  e calls these ro
a7ce0 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20 63 6f 6e  utines to.** con
a7cf0 74 72 6f 6c 20 74 68 65 20 50 52 4e 47 2e 0a 2a  trol the PRNG..*
a7d00 2f 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f  /.static SQLITE_
a7d10 57 53 44 20 73 74 72 75 63 74 20 73 71 6c 69 74  WSD struct sqlit
a7d20 65 33 50 72 6e 67 54 79 70 65 20 73 71 6c 69 74  e3PrngType sqlit
a7d30 65 33 53 61 76 65 64 50 72 6e 67 3b 0a 53 51 4c  e3SavedPrng;.SQL
a7d40 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
a7d50 20 73 71 6c 69 74 65 33 50 72 6e 67 53 61 76 65   sqlite3PrngSave
a7d60 53 74 61 74 65 28 76 6f 69 64 29 7b 0a 20 20 6d  State(void){.  m
a7d70 65 6d 63 70 79 28 0a 20 20 20 20 26 47 4c 4f 42  emcpy(.    &GLOB
a7d80 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74 65  AL(struct sqlite
a7d90 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69 74  3PrngType, sqlit
a7da0 65 33 53 61 76 65 64 50 72 6e 67 29 2c 0a 20 20  e3SavedPrng),.  
a7db0 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74    &GLOBAL(struct
a7dc0 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65   sqlite3PrngType
a7dd0 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 2c 0a  , sqlite3Prng),.
a7de0 20 20 20 20 73 69 7a 65 6f 66 28 73 71 6c 69 74      sizeof(sqlit
a7df0 65 33 50 72 6e 67 29 0a 20 20 29 3b 0a 7d 0a 53  e3Prng).  );.}.S
a7e00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
a7e10 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65  id sqlite3PrngRe
a7e20 73 74 6f 72 65 53 74 61 74 65 28 76 6f 69 64 29  storeState(void)
a7e30 7b 0a 20 20 6d 65 6d 63 70 79 28 0a 20 20 20 20  {.  memcpy(.    
a7e40 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73  &GLOBAL(struct s
a7e50 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20  qlite3PrngType, 
a7e60 73 71 6c 69 74 65 33 50 72 6e 67 29 2c 0a 20 20  sqlite3Prng),.  
a7e70 20 20 26 47 4c 4f 42 41 4c 28 73 74 72 75 63 74    &GLOBAL(struct
a7e80 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65   sqlite3PrngType
a7e90 2c 20 73 71 6c 69 74 65 33 53 61 76 65 64 50 72  , sqlite3SavedPr
a7ea0 6e 67 29 2c 0a 20 20 20 20 73 69 7a 65 6f 66 28  ng),.    sizeof(
a7eb0 73 71 6c 69 74 65 33 50 72 6e 67 29 0a 20 20 29  sqlite3Prng).  )
a7ec0 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
a7ed0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
a7ee0 72 6e 67 52 65 73 65 74 53 74 61 74 65 28 76 6f  rngResetState(vo
a7ef0 69 64 29 7b 0a 20 20 47 4c 4f 42 41 4c 28 73 74  id){.  GLOBAL(st
a7f00 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67  ruct sqlite3Prng
a7f10 54 79 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e  Type, sqlite3Prn
a7f20 67 29 2e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 7d  g).isInit = 0;.}
a7f30 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
a7f40 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54  E_OMIT_BUILTIN_T
a7f50 45 53 54 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  EST */../*******
a7f60 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72  ******* End of r
a7f70 61 6e 64 6f 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  andom.c ********
a7f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7fa0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
a7fb0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
a7fc0 6c 65 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a  le utf.c *******
a7fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a7ff0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
a8000 30 34 20 41 70 72 69 6c 20 31 33 0a 2a 2a 0a 2a  04 April 13.**.*
a8010 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
a8020 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
a8030 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
a8040 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
a8050 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
a8060 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
a8070 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
a8080 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
a8090 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
a80a0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
a80b0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
a80c0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
a80d0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
a80e0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
a80f0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
a8100 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
a8110 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
a8120 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
a8130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
a8170 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
a8180 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 74   routines used t
a8190 6f 20 74 72 61 6e 73 6c 61 74 65 20 62 65 74 77  o translate betw
a81a0 65 65 6e 20 55 54 46 2d 38 2c 20 0a 2a 2a 20 55  een UTF-8, .** U
a81b0 54 46 2d 31 36 2c 20 55 54 46 2d 31 36 42 45 2c  TF-16, UTF-16BE,
a81c0 20 61 6e 64 20 55 54 46 2d 31 36 4c 45 2e 0a 2a   and UTF-16LE..*
a81d0 2a 0a 2a 2a 20 24 49 64 3a 20 75 74 66 2e 63 2c  *.** $Id: utf.c,
a81e0 76 20 31 2e 37 33 20 32 30 30 39 2f 30 34 2f 30  v 1.73 2009/04/0
a81f0 31 20 31 38 3a 34 30 3a 33 32 20 64 72 68 20 45  1 18:40:32 drh E
a8200 78 70 20 24 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73  xp $.**.** Notes
a8210 20 6f 6e 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a   on UTF-8:.**.**
a8220 20 20 20 42 79 74 65 2d 30 20 20 20 20 42 79 74     Byte-0    Byt
a8230 65 2d 31 20 20 20 20 42 79 74 65 2d 32 20 20 20  e-1    Byte-2   
a8240 20 42 79 74 65 2d 33 20 20 20 20 56 61 6c 75 65   Byte-3    Value
a8250 0a 2a 2a 20 20 30 78 78 78 78 78 78 78 20 20 20  .**  0xxxxxxx   
a8260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a8270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 30                00
a8280 30 30 30 30 30 30 20 30 30 30 30 30 30 30 30 20  000000 00000000 
a8290 30 78 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 30  0xxxxxxx.**  110
a82a0 79 79 79 79 79 20 20 31 30 78 78 78 78 78 78 20  yyyyy  10xxxxxx 
a82b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a82c0 20 20 20 20 20 20 30 30 30 30 30 30 30 30 20 30        00000000 0
a82d0 30 30 30 30 79 79 79 20 79 79 78 78 78 78 78 78  0000yyy yyxxxxxx
a82e0 0a 2a 2a 20 20 31 31 31 30 7a 7a 7a 7a 20 20 31  .**  1110zzzz  1
a82f0 30 79 79 79 79 79 79 20 20 31 30 78 78 78 78 78  0yyyyyy  10xxxxx
a8300 78 20 20 20 20 20 20 20 20 20 20 20 20 20 30 30  x             00
a8310 30 30 30 30 30 30 20 7a 7a 7a 7a 79 79 79 79 20  000000 zzzzyyyy 
a8320 79 79 78 78 78 78 78 78 0a 2a 2a 20 20 31 31 31  yyxxxxxx.**  111
a8330 31 30 75 75 75 20 20 31 30 75 75 7a 7a 7a 7a 20  10uuu  10uuzzzz 
a8340 20 31 30 79 79 79 79 79 79 20 20 31 30 78 78 78   10yyyyyy  10xxx
a8350 78 78 78 20 20 20 30 30 30 75 75 75 75 75 20 7a  xxx   000uuuuu z
a8360 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78  zzzyyyy yyxxxxxx
a8370 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20  .**.**.** Notes 
a8380 6f 6e 20 55 54 46 2d 31 36 3a 20 20 28 77 69 74  on UTF-16:  (wit
a8390 68 20 77 77 77 77 2b 31 3d 3d 75 75 75 75 75 29  h wwww+1==uuuuu)
a83a0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 57 6f 72 64  .**.**      Word
a83b0 2d 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20  -0              
a83c0 20 57 6f 72 64 2d 31 20 20 20 20 20 20 20 20 20   Word-1         
a83d0 20 56 61 6c 75 65 0a 2a 2a 20 20 31 31 30 31 31   Value.**  11011
a83e0 30 77 77 20 77 77 7a 7a 7a 7a 79 79 20 20 20 31  0ww wwzzzzyy   1
a83f0 31 30 31 31 31 79 79 20 79 79 78 78 78 78 78 78  10111yy yyxxxxxx
a8400 20 20 20 20 30 30 30 75 75 75 75 75 20 7a 7a 7a      000uuuuu zzz
a8410 7a 79 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a  zyyyy yyxxxxxx.*
a8420 2a 20 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78  *  zzzzyyyy yyxx
a8430 78 78 78 78 20 20 20 20 20 20 20 20 20 20 20 20  xxxx            
a8440 20 20 20 20 20 20 20 20 20 20 20 20 30 30 30 30              0000
a8450 30 30 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79 79  0000 zzzzyyyy yy
a8460 78 78 78 78 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20  xxxxxx.**.**.** 
a8470 42 4f 4d 20 6f 72 20 42 79 74 65 20 4f 72 64 65  BOM or Byte Orde
a8480 72 20 4d 61 72 6b 3a 0a 2a 2a 20 20 20 20 20 30  r Mark:.**     0
a8490 78 66 66 20 30 78 66 65 20 20 20 6c 69 74 74 6c  xff 0xfe   littl
a84a0 65 2d 65 6e 64 69 61 6e 20 75 74 66 2d 31 36 20  e-endian utf-16 
a84b0 66 6f 6c 6c 6f 77 73 0a 2a 2a 20 20 20 20 20 30  follows.**     0
a84c0 78 66 65 20 30 78 66 66 20 20 20 62 69 67 2d 65  xfe 0xff   big-e
a84d0 6e 64 69 61 6e 20 75 74 66 2d 31 36 20 66 6f 6c  ndian utf-16 fol
a84e0 6c 6f 77 73 0a 2a 2a 0a 2a 2f 0a 2f 2a 2a 2a 2a  lows.**.*/./****
a84f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
a8500 64 65 20 76 64 62 65 49 6e 74 2e 68 20 69 6e 20  de vdbeInt.h in 
a8510 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 75 74  the middle of ut
a8520 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f.c ************
a8530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
a8540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
a8550 20 66 69 6c 65 20 76 64 62 65 49 6e 74 2e 68 20   file vdbeInt.h 
a8560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
a8590 20 32 30 30 33 20 53 65 70 74 65 6d 62 65 72 20   2003 September 
a85a0 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  6.**.** The auth
a85b0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
a85c0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
a85d0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
a85e0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
a85f0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
a8600 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
a8610 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
a8620 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
a8630 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
a8640 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
a8650 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
a8660 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
a8670 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
a8680 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
a8690 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
a86a0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
a86b0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
a86c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a86d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a86e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a86f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a8700 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
a8710 68 65 61 64 65 72 20 66 69 6c 65 20 66 6f 72 20  header file for 
a8720 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74  information that
a8730 20 69 73 20 70 72 69 76 61 74 65 20 74 6f 20 74   is private to t
a8740 68 65 0a 2a 2a 20 56 44 42 45 2e 20 20 54 68 69  he.** VDBE.  Thi
a8750 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73  s information us
a8760 65 64 20 74 6f 20 61 6c 6c 20 62 65 20 61 74 20  ed to all be at 
a8770 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73  the top of the s
a8780 69 6e 67 6c 65 0a 2a 2a 20 73 6f 75 72 63 65 20  ingle.** source 
a8790 63 6f 64 65 20 66 69 6c 65 20 22 76 64 62 65 2e  code file "vdbe.
a87a0 63 22 2e 20 20 57 68 65 6e 20 74 68 61 74 20 66  c".  When that f
a87b0 69 6c 65 20 62 65 63 61 6d 65 20 74 6f 6f 20 62  ile became too b
a87c0 69 67 20 28 6f 76 65 72 0a 2a 2a 20 36 30 30 30  ig (over.** 6000
a87d0 20 6c 69 6e 65 73 20 6c 6f 6e 67 29 20 69 74 20   lines long) it 
a87e0 77 61 73 20 73 70 6c 69 74 20 75 70 20 69 6e 74  was split up int
a87f0 6f 20 73 65 76 65 72 61 6c 20 73 6d 61 6c 6c 65  o several smalle
a8800 72 20 66 69 6c 65 73 20 61 6e 64 0a 2a 2a 20 74  r files and.** t
a8810 68 69 73 20 68 65 61 64 65 72 20 69 6e 66 6f 72  his header infor
a8820 6d 61 74 69 6f 6e 20 77 61 73 20 66 61 63 74 6f  mation was facto
a8830 72 65 64 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24  red out..**.** $
a8840 49 64 3a 20 76 64 62 65 49 6e 74 2e 68 2c 76 20  Id: vdbeInt.h,v 
a8850 31 2e 31 37 34 20 32 30 30 39 2f 30 36 2f 32 33  1.174 2009/06/23
a8860 20 31 34 3a 31 35 3a 30 34 20 64 72 68 20 45 78   14:15:04 drh Ex
a8870 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f  p $.*/.#ifndef _
a8880 56 44 42 45 49 4e 54 5f 48 5f 0a 23 64 65 66 69  VDBEINT_H_.#defi
a8890 6e 65 20 5f 56 44 42 45 49 4e 54 5f 48 5f 0a 0a  ne _VDBEINT_H_..
a88a0 2f 2a 0a 2a 2a 20 53 51 4c 20 69 73 20 74 72 61  /*.** SQL is tra
a88b0 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 73  nslated into a s
a88c0 65 71 75 65 6e 63 65 20 6f 66 20 69 6e 73 74 72  equence of instr
a88d0 75 63 74 69 6f 6e 73 20 74 6f 20 62 65 0a 2a 2a  uctions to be.**
a88e0 20 65 78 65 63 75 74 65 64 20 62 79 20 61 20 76   executed by a v
a88f0 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20  irtual machine. 
a8900 20 45 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f   Each instructio
a8910 6e 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  n is an instance
a8920 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  .** of the follo
a8930 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a  wing structure..
a8940 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
a8950 74 20 56 64 62 65 4f 70 20 4f 70 3b 0a 0a 2f 2a  t VdbeOp Op;../*
a8960 0a 2a 2a 20 42 6f 6f 6c 65 61 6e 20 76 61 6c 75  .** Boolean valu
a8970 65 73 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e  es.*/.typedef un
a8980 73 69 67 6e 65 64 20 63 68 61 72 20 42 6f 6f 6c  signed char Bool
a8990 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 75 72 73 6f  ;../*.** A curso
a89a0 72 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 69  r is a pointer i
a89b0 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 42 54 72  nto a single BTr
a89c0 65 65 20 77 69 74 68 69 6e 20 61 20 64 61 74 61  ee within a data
a89d0 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 54 68  base file..** Th
a89e0 65 20 63 75 72 73 6f 72 20 63 61 6e 20 73 65 65  e cursor can see
a89f0 6b 20 74 6f 20 61 20 42 54 72 65 65 20 65 6e 74  k to a BTree ent
a8a00 72 79 20 77 69 74 68 20 61 20 70 61 72 74 69 63  ry with a partic
a8a10 75 6c 61 72 20 6b 65 79 2c 20 6f 72 0a 2a 2a 20  ular key, or.** 
a8a20 6c 6f 6f 70 20 6f 76 65 72 20 61 6c 6c 20 65 6e  loop over all en
a8a30 74 72 69 65 73 20 6f 66 20 74 68 65 20 42 74 72  tries of the Btr
a8a40 65 65 2e 20 20 59 6f 75 20 63 61 6e 20 61 6c 73  ee.  You can als
a8a50 6f 20 69 6e 73 65 72 74 20 6e 65 77 20 42 54 72  o insert new BTr
a8a60 65 65 0a 2a 2a 20 65 6e 74 72 69 65 73 20 6f 72  ee.** entries or
a8a70 20 72 65 74 72 69 65 76 65 20 74 68 65 20 6b 65   retrieve the ke
a8a80 79 20 6f 72 20 64 61 74 61 20 66 72 6f 6d 20 74  y or data from t
a8a90 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 68  he entry that th
a8aa0 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 63  e cursor.** is c
a8ab0 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e  urrently pointin
a8ac0 67 20 74 6f 2e 0a 2a 2a 20 0a 2a 2a 20 45 76 65  g to..** .** Eve
a8ad0 72 79 20 63 75 72 73 6f 72 20 74 68 61 74 20 74  ry cursor that t
a8ae0 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  he virtual machi
a8af0 6e 65 20 68 61 73 20 6f 70 65 6e 20 69 73 20 72  ne has open is r
a8b00 65 70 72 65 73 65 6e 74 65 64 20 62 79 20 61 6e  epresented by an
a8b10 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  .** instance of 
a8b20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
a8b30 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 49  ructure..**.** I
a8b40 66 20 74 68 65 20 56 64 62 65 43 75 72 73 6f 72  f the VdbeCursor
a8b50 2e 69 73 54 72 69 67 67 65 72 52 6f 77 20 66 6c  .isTriggerRow fl
a8b60 61 67 20 69 73 20 73 65 74 20 69 74 20 6d 65 61  ag is set it mea
a8b70 6e 73 20 74 68 61 74 20 74 68 69 73 20 63 75 72  ns that this cur
a8b80 73 6f 72 20 69 73 0a 2a 2a 20 72 65 61 6c 6c 79  sor is.** really
a8b90 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 74 68   a single row th
a8ba0 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 68  at represents th
a8bb0 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65  e NEW or OLD pse
a8bc0 75 64 6f 2d 74 61 62 6c 65 20 6f 66 0a 2a 2a 20  udo-table of.** 
a8bd0 61 20 72 6f 77 20 74 72 69 67 67 65 72 2e 20 20  a row trigger.  
a8be0 54 68 65 20 64 61 74 61 20 66 6f 72 20 74 68 65  The data for the
a8bf0 20 72 6f 77 20 69 73 20 73 74 6f 72 65 64 20 69   row is stored i
a8c00 6e 20 56 64 62 65 43 75 72 73 6f 72 2e 70 44 61  n VdbeCursor.pDa
a8c10 74 61 20 61 6e 64 0a 2a 2a 20 74 68 65 20 72 6f  ta and.** the ro
a8c20 77 69 64 20 69 73 20 69 6e 20 56 64 62 65 43 75  wid is in VdbeCu
a8c30 72 73 6f 72 2e 69 4b 65 79 2e 0a 2a 2f 0a 73 74  rsor.iKey..*/.st
a8c40 72 75 63 74 20 56 64 62 65 43 75 72 73 6f 72 20  ruct VdbeCursor 
a8c50 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  {.  BtCursor *pC
a8c60 75 72 73 6f 72 3b 20 20 20 20 2f 2a 20 54 68 65  ursor;    /* The
a8c70 20 63 75 72 73 6f 72 20 73 74 72 75 63 74 75 72   cursor structur
a8c80 65 20 6f 66 20 74 68 65 20 62 61 63 6b 65 6e 64  e of the backend
a8c90 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
a8ca0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
a8cb0 6e 64 65 78 20 6f 66 20 63 75 72 73 6f 72 20 64  ndex of cursor d
a8cc0 61 74 61 62 61 73 65 20 69 6e 20 64 62 2d 3e 61  atabase in db->a
a8cd0 44 62 5b 5d 20 28 6f 72 20 2d 31 29 20 2a 2f 0a  Db[] (or -1) */.
a8ce0 20 20 69 36 34 20 6c 61 73 74 52 6f 77 69 64 3b    i64 lastRowid;
a8cf0 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20          /* Last 
a8d00 72 6f 77 69 64 20 66 72 6f 6d 20 61 20 4e 65 78  rowid from a Nex
a8d10 74 20 6f 72 20 4e 65 78 74 49 64 78 20 6f 70 65  t or NextIdx ope
a8d20 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 42 6f 6f 6c  ration */.  Bool
a8d30 20 7a 65 72 6f 65 64 3b 20 20 20 20 20 20 20 20   zeroed;        
a8d40 20 20 2f 2a 20 54 72 75 65 20 69 66 20 7a 65 72    /* True if zer
a8d50 6f 65 64 20 6f 75 74 20 61 6e 64 20 72 65 61 64  oed out and read
a8d60 79 20 66 6f 72 20 72 65 75 73 65 20 2a 2f 0a 20  y for reuse */. 
a8d70 20 42 6f 6f 6c 20 72 6f 77 69 64 49 73 56 61 6c   Bool rowidIsVal
a8d80 69 64 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 69  id;    /* True i
a8d90 66 20 6c 61 73 74 52 6f 77 69 64 20 69 73 20 76  f lastRowid is v
a8da0 61 6c 69 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 61  alid */.  Bool a
a8db0 74 46 69 72 73 74 3b 20 20 20 20 20 20 20 20 20  tFirst;         
a8dc0 2f 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e 74  /* True if point
a8dd0 69 6e 67 20 74 6f 20 66 69 72 73 74 20 65 6e 74  ing to first ent
a8de0 72 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20 75 73 65  ry */.  Bool use
a8df0 52 61 6e 64 6f 6d 52 6f 77 69 64 3b 20 20 2f 2a  RandomRowid;  /*
a8e00 20 47 65 6e 65 72 61 74 65 20 6e 65 77 20 72 65   Generate new re
a8e10 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 73 65 6d  cord numbers sem
a8e20 69 2d 72 61 6e 64 6f 6d 6c 79 20 2a 2f 0a 20 20  i-randomly */.  
a8e30 42 6f 6f 6c 20 6e 75 6c 6c 52 6f 77 3b 20 20 20  Bool nullRow;   
a8e40 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
a8e50 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 72   pointing to a r
a8e60 6f 77 20 77 69 74 68 20 6e 6f 20 64 61 74 61 20  ow with no data 
a8e70 2a 2f 0a 20 20 42 6f 6f 6c 20 64 65 66 65 72 72  */.  Bool deferr
a8e80 65 64 4d 6f 76 65 74 6f 3b 20 20 2f 2a 20 41 20  edMoveto;  /* A 
a8e90 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42  call to sqlite3B
a8ea0 74 72 65 65 4d 6f 76 65 74 6f 28 29 20 69 73 20  treeMoveto() is 
a8eb0 6e 65 65 64 65 64 20 2a 2f 0a 20 20 42 6f 6f 6c  needed */.  Bool
a8ec0 20 69 73 54 61 62 6c 65 3b 20 20 20 20 20 20 20   isTable;       
a8ed0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 20 74    /* True if a t
a8ee0 61 62 6c 65 20 72 65 71 75 69 72 69 6e 67 20 69  able requiring i
a8ef0 6e 74 65 67 65 72 20 6b 65 79 73 20 2a 2f 0a 20  nteger keys */. 
a8f00 20 42 6f 6f 6c 20 69 73 49 6e 64 65 78 3b 20 20   Bool isIndex;  
a8f10 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
a8f20 66 20 61 6e 20 69 6e 64 65 78 20 63 6f 6e 74 61  f an index conta
a8f30 69 6e 69 6e 67 20 6b 65 79 73 20 6f 6e 6c 79 20  ining keys only 
a8f40 2d 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20 69  - no data */.  i
a8f50 36 34 20 6d 6f 76 65 74 6f 54 61 72 67 65 74 3b  64 movetoTarget;
a8f60 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
a8f70 20 74 6f 20 74 68 65 20 64 65 66 65 72 72 65 64   to the deferred
a8f80 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
a8f90 65 74 6f 28 29 20 2a 2f 0a 20 20 42 74 72 65 65  eto() */.  Btree
a8fa0 20 2a 70 42 74 3b 20 20 20 20 20 20 20 20 20 20   *pBt;          
a8fb0 20 2f 2a 20 53 65 70 61 72 61 74 65 20 66 69 6c   /* Separate fil
a8fc0 65 20 68 6f 6c 64 69 6e 67 20 74 65 6d 70 6f 72  e holding tempor
a8fd0 61 72 79 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  ary table */.  i
a8fe0 6e 74 20 70 73 65 75 64 6f 54 61 62 6c 65 52 65  nt pseudoTableRe
a8ff0 67 3b 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72  g;   /* Register
a9000 20 68 6f 6c 64 69 6e 67 20 70 73 65 75 64 6f 74   holding pseudot
a9010 61 62 6c 65 20 63 6f 6e 74 65 6e 74 2e 20 2a 2f  able content. */
a9020 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79  .  KeyInfo *pKey
a9030 49 6e 66 6f 3b 20 20 20 20 2f 2a 20 49 6e 66 6f  Info;    /* Info
a9040 20 61 62 6f 75 74 20 69 6e 64 65 78 20 6b 65 79   about index key
a9050 73 20 6e 65 65 64 65 64 20 62 79 20 69 6e 64 65  s needed by inde
a9060 78 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 69  x cursors */.  i
a9070 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20  nt nField;      
a9080 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
a9090 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20  f fields in the 
a90a0 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20  header */.  i64 
a90b0 73 65 71 43 6f 75 6e 74 3b 20 20 20 20 20 20 20  seqCount;       
a90c0 20 20 2f 2a 20 53 65 71 75 65 6e 63 65 20 63 6f    /* Sequence co
a90d0 75 6e 74 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74  unter */.  sqlit
a90e0 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a  e3_vtab_cursor *
a90f0 70 56 74 61 62 43 75 72 73 6f 72 3b 20 20 2f 2a  pVtabCursor;  /*
a9100 20 54 68 65 20 63 75 72 73 6f 72 20 66 6f 72 20   The cursor for 
a9110 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
a9120 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  */.  const sqlit
a9130 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
a9140 6c 65 3b 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c  le;     /* Modul
a9150 65 20 66 6f 72 20 63 75 72 73 6f 72 20 70 56 74  e for cursor pVt
a9160 61 62 43 75 72 73 6f 72 20 2a 2f 0a 0a 20 20 2f  abCursor */..  /
a9170 2a 20 52 65 73 75 6c 74 20 6f 66 20 6c 61 73 74  * Result of last
a9180 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
a9190 65 74 6f 28 29 20 64 6f 6e 65 20 62 79 20 61 6e  eto() done by an
a91a0 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 20 6f 72   OP_NotExists or
a91b0 20 0a 20 20 2a 2a 20 4f 50 5f 49 73 55 6e 69 71   .  ** OP_IsUniq
a91c0 75 65 20 6f 70 63 6f 64 65 20 6f 6e 20 74 68 69  ue opcode on thi
a91d0 73 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 69  s cursor. */.  i
a91e0 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 3b 0a 0a  nt seekResult;..
a91f0 20 20 2f 2a 20 43 61 63 68 65 64 20 69 6e 66 6f    /* Cached info
a9200 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
a9210 65 20 68 65 61 64 65 72 20 66 6f 72 20 74 68 65  e header for the
a9220 20 64 61 74 61 20 72 65 63 6f 72 64 20 74 68 61   data record tha
a9230 74 20 74 68 65 0a 20 20 2a 2a 20 63 75 72 73 6f  t the.  ** curso
a9240 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70  r is currently p
a9250 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 4f 6e 6c  ointing to.  Onl
a9260 79 20 76 61 6c 69 64 20 69 66 20 63 61 63 68 65  y valid if cache
a9270 53 74 61 74 75 73 20 6d 61 74 63 68 65 73 0a 20  Status matches. 
a9280 20 2a 2a 20 56 64 62 65 2e 63 61 63 68 65 43 74   ** Vdbe.cacheCt
a9290 72 2e 20 20 56 64 62 65 2e 63 61 63 68 65 43 74  r.  Vdbe.cacheCt
a92a0 72 20 77 69 6c 6c 20 6e 65 76 65 72 20 74 61 6b  r will never tak
a92b0 65 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f  e on the value o
a92c0 66 0a 20 20 2a 2a 20 43 41 43 48 45 5f 53 54 41  f.  ** CACHE_STA
a92d0 4c 45 20 61 6e 64 20 73 6f 20 73 65 74 74 69 6e  LE and so settin
a92e0 67 20 63 61 63 68 65 53 74 61 74 75 73 3d 43 41  g cacheStatus=CA
a92f0 43 48 45 5f 53 54 41 4c 45 20 67 75 61 72 61 6e  CHE_STALE guaran
a9300 74 65 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 74  tees that.  ** t
a9310 68 65 20 63 61 63 68 65 20 69 73 20 6f 75 74 20  he cache is out 
a9320 6f 66 20 64 61 74 65 2e 0a 20 20 2a 2a 0a 20 20  of date..  **.  
a9330 2a 2a 20 61 52 6f 77 20 6d 69 67 68 74 20 70 6f  ** aRow might po
a9340 69 6e 74 20 74 6f 20 28 65 70 68 65 6d 65 72 61  int to (ephemera
a9350 6c 29 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  l) data for the 
a9360 63 75 72 72 65 6e 74 20 72 6f 77 2c 20 6f 72 20  current row, or 
a9370 69 74 20 6d 69 67 68 74 0a 20 20 2a 2a 20 62 65  it might.  ** be
a9380 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 75 33   NULL..  */.  u3
a9390 32 20 63 61 63 68 65 53 74 61 74 75 73 3b 20 20  2 cacheStatus;  
a93a0 20 20 20 20 2f 2a 20 43 61 63 68 65 20 69 73 20      /* Cache is 
a93b0 76 61 6c 69 64 20 69 66 20 74 68 69 73 20 6d 61  valid if this ma
a93c0 74 63 68 65 73 20 56 64 62 65 2e 63 61 63 68 65  tches Vdbe.cache
a93d0 43 74 72 20 2a 2f 0a 20 20 69 6e 74 20 70 61 79  Ctr */.  int pay
a93e0 6c 6f 61 64 53 69 7a 65 3b 20 20 20 20 20 20 2f  loadSize;      /
a93f0 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  * Total number o
a9400 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 72  f bytes in the r
a9410 65 63 6f 72 64 20 2a 2f 0a 20 20 75 33 32 20 2a  ecord */.  u32 *
a9420 61 54 79 70 65 3b 20 20 20 20 20 20 20 20 20 20  aType;          
a9430 20 2f 2a 20 54 79 70 65 20 76 61 6c 75 65 73 20   /* Type values 
a9440 66 6f 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 20  for all entries 
a9450 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f  in the record */
a9460 0a 20 20 75 33 32 20 2a 61 4f 66 66 73 65 74 3b  .  u32 *aOffset;
a9470 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68           /* Cach
a9480 65 64 20 6f 66 66 73 65 74 73 20 74 6f 20 74 68  ed offsets to th
a9490 65 20 73 74 61 72 74 20 6f 66 20 65 61 63 68 20  e start of each 
a94a0 63 6f 6c 75 6d 6e 73 20 64 61 74 61 20 2a 2f 0a  columns data */.
a94b0 20 20 75 38 20 2a 61 52 6f 77 3b 20 20 20 20 20    u8 *aRow;     
a94c0 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20          /* Data 
a94d0 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
a94e0 72 6f 77 2c 20 69 66 20 61 6c 6c 20 6f 6e 20 6f  row, if all on o
a94f0 6e 65 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 74 79  ne page */.};.ty
a9500 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62  pedef struct Vdb
a9510 65 43 75 72 73 6f 72 20 56 64 62 65 43 75 72 73  eCursor VdbeCurs
a9520 6f 72 3b 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20  or;../*.** When 
a9530 61 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 69 73  a sub-program is
a9540 20 65 78 65 63 75 74 65 64 20 28 4f 50 5f 50 72   executed (OP_Pr
a9550 6f 67 72 61 6d 29 2c 20 61 20 73 74 72 75 63 74  ogram), a struct
a9560 75 72 65 20 6f 66 20 74 68 69 73 20 74 79 70 65  ure of this type
a9570 0a 2a 2a 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  .** is allocated
a9580 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 75   to store the cu
a9590 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74  rrent value of t
a95a0 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74  he program count
a95b0 65 72 2c 20 61 73 0a 2a 2a 20 77 65 6c 6c 20 61  er, as.** well a
a95c0 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 65  s the current me
a95d0 6d 6f 72 79 20 63 65 6c 6c 20 61 72 72 61 79 20  mory cell array 
a95e0 61 6e 64 20 76 61 72 69 6f 75 73 20 6f 74 68 65  and various othe
a95f0 72 20 66 72 61 6d 65 20 73 70 65 63 69 66 69 63  r frame specific
a9600 0a 2a 2a 20 76 61 6c 75 65 73 20 73 74 6f 72 65  .** values store
a9610 64 20 69 6e 20 74 68 65 20 56 64 62 65 20 73 74  d in the Vdbe st
a9620 72 75 63 74 2e 20 57 68 65 6e 20 74 68 65 20 73  ruct. When the s
a9630 75 62 2d 70 72 6f 67 72 61 6d 20 69 73 20 66 69  ub-program is fi
a9640 6e 69 73 68 65 64 2c 20 0a 2a 2a 20 74 68 65 73  nished, .** thes
a9650 65 20 76 61 6c 75 65 73 20 61 72 65 20 63 6f 70  e values are cop
a9660 69 65 64 20 62 61 63 6b 20 74 6f 20 74 68 65 20  ied back to the 
a9670 56 64 62 65 20 66 72 6f 6d 20 74 68 65 20 56 64  Vdbe from the Vd
a9680 62 65 46 72 61 6d 65 20 73 74 72 75 63 74 75 72  beFrame structur
a9690 65 2c 0a 2a 2a 20 72 65 73 74 6f 72 69 6e 67 20  e,.** restoring 
a96a0 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65  the state of the
a96b0 20 56 4d 20 74 6f 20 61 73 20 69 74 20 77 61 73   VM to as it was
a96c0 20 62 65 66 6f 72 65 20 74 68 65 20 73 75 62 2d   before the sub-
a96d0 70 72 6f 67 72 61 6d 0a 2a 2a 20 62 65 67 61 6e  program.** began
a96e0 20 65 78 65 63 75 74 69 6e 67 2e 0a 2a 2a 0a 2a   executing..**.*
a96f0 2a 20 46 72 61 6d 65 73 20 61 72 65 20 73 74 6f  * Frames are sto
a9700 72 65 64 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20  red in a linked 
a9710 6c 69 73 74 20 68 65 61 64 65 64 20 61 74 20 56  list headed at V
a9720 64 62 65 2e 70 50 61 72 65 6e 74 2e 20 56 64 62  dbe.pParent. Vdb
a9730 65 2e 70 50 61 72 65 6e 74 0a 2a 2a 20 69 73 20  e.pParent.** is 
a9740 74 68 65 20 70 61 72 65 6e 74 20 6f 66 20 74 68  the parent of th
a9750 65 20 63 75 72 72 65 6e 74 20 66 72 61 6d 65 2c  e current frame,
a9760 20 6f 72 20 7a 65 72 6f 20 69 66 20 74 68 65 20   or zero if the 
a9770 63 75 72 72 65 6e 74 20 66 72 61 6d 65 0a 2a 2a  current frame.**
a9780 20 69 73 20 74 68 65 20 6d 61 69 6e 20 56 64 62   is the main Vdb
a9790 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 74 79  e program..*/.ty
a97a0 70 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62  pedef struct Vdb
a97b0 65 46 72 61 6d 65 20 56 64 62 65 46 72 61 6d 65  eFrame VdbeFrame
a97c0 3b 0a 73 74 72 75 63 74 20 56 64 62 65 46 72 61  ;.struct VdbeFra
a97d0 6d 65 20 7b 0a 20 20 56 64 62 65 20 2a 76 3b 20  me {.  Vdbe *v; 
a97e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
a97f0 2a 20 56 4d 20 74 68 69 73 20 66 72 61 6d 65 20  * VM this frame 
a9800 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a 20 20  belongs to */.  
a9810 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20  int pc;         
a9820 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72          /* Progr
a9830 61 6d 20 43 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  am Counter */.  
a9840 4f 70 20 2a 61 4f 70 3b 20 20 20 20 20 20 20 20  Op *aOp;        
a9850 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72          /* Progr
a9860 61 6d 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20  am instructions 
a9870 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 3b 20 20 20  */.  int nOp;   
a9880 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
a9890 53 69 7a 65 20 6f 66 20 61 4f 70 20 61 72 72 61  Size of aOp arra
a98a0 79 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d  y */.  Mem *aMem
a98b0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
a98c0 2a 20 41 72 72 61 79 20 6f 66 20 6d 65 6d 6f 72  * Array of memor
a98d0 79 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 69 6e 74  y cells */.  int
a98e0 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20   nMem;          
a98f0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
a9900 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 4d 65  f entries in aMe
a9910 6d 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73 6f  m */.  VdbeCurso
a9920 72 20 2a 2a 61 70 43 73 72 3b 20 20 20 20 20 2f  r **apCsr;     /
a9930 2a 20 45 6c 65 6d 65 6e 74 20 6f 66 20 56 64 62  * Element of Vdb
a9940 65 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 75  e cursors */.  u
a9950 31 36 20 6e 43 75 72 73 6f 72 3b 20 20 20 20 20  16 nCursor;     
a9960 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
a9970 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
a9980 70 43 73 72 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  pCsr */.  void *
a9990 74 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20  token;          
a99a0 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 53 75 62    /* Copy of Sub
a99b0 50 72 6f 67 72 61 6d 2e 74 6f 6b 65 6e 20 2a 2f  Program.token */
a99c0 0a 20 20 69 6e 74 20 6e 43 68 69 6c 64 4d 65 6d  .  int nChildMem
a99d0 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ;          /* Nu
a99e0 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 63  mber of memory c
a99f0 65 6c 6c 73 20 66 6f 72 20 63 68 69 6c 64 20 66  ells for child f
a9a00 72 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  rame */.  int nC
a9a10 68 69 6c 64 43 73 72 3b 20 20 20 20 20 20 20 20  hildCsr;        
a9a20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
a9a30 75 72 73 6f 72 73 20 66 6f 72 20 63 68 69 6c 64  ursors for child
a9a40 20 66 72 61 6d 65 20 2a 2f 0a 20 20 69 36 34 20   frame */.  i64 
a9a50 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20 20  lastRowid;      
a9a60 20 20 20 20 2f 2a 20 4c 61 73 74 20 69 6e 73 65      /* Last inse
a9a70 72 74 20 72 6f 77 69 64 20 28 73 71 6c 69 74 65  rt rowid (sqlite
a9a80 33 2e 6c 61 73 74 52 6f 77 69 64 29 20 2a 2f 0a  3.lastRowid) */.
a9a90 20 20 69 6e 74 20 6e 43 68 61 6e 67 65 3b 20 20    int nChange;  
a9aa0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61            /* Sta
a9ab0 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 73 20 28  tement changes (
a9ac0 56 64 62 65 2e 6e 43 68 61 6e 67 65 73 29 20 20  Vdbe.nChanges)  
a9ad0 20 20 20 2a 2f 0a 20 20 56 64 62 65 46 72 61 6d     */.  VdbeFram
a9ae0 65 20 2a 70 50 61 72 65 6e 74 3b 20 20 20 20 20  e *pParent;     
a9af0 2f 2a 20 50 61 72 65 6e 74 20 6f 66 20 74 68 69  /* Parent of thi
a9b00 73 20 66 72 61 6d 65 20 2a 2f 0a 7d 3b 0a 0a 23  s frame */.};..#
a9b10 64 65 66 69 6e 65 20 56 64 62 65 46 72 61 6d 65  define VdbeFrame
a9b20 4d 65 6d 28 70 29 20 28 28 4d 65 6d 20 2a 29 26  Mem(p) ((Mem *)&
a9b30 28 28 75 38 20 2a 29 70 29 5b 52 4f 55 4e 44 38  ((u8 *)p)[ROUND8
a9b40 28 73 69 7a 65 6f 66 28 56 64 62 65 46 72 61 6d  (sizeof(VdbeFram
a9b50 65 29 29 5d 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 76  e))])../*.** A v
a9b60 61 6c 75 65 20 66 6f 72 20 56 64 62 65 43 75 72  alue for VdbeCur
a9b70 73 6f 72 2e 63 61 63 68 65 56 61 6c 69 64 20 74  sor.cacheValid t
a9b80 68 61 74 20 6d 65 61 6e 73 20 74 68 65 20 63 61  hat means the ca
a9b90 63 68 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e  che is always in
a9ba0 76 61 6c 69 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e  valid..*/.#defin
a9bb0 65 20 43 41 43 48 45 5f 53 54 41 4c 45 20 30 0a  e CACHE_STALE 0.
a9bc0 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 6c  ./*.** Internall
a9bd0 79 2c 20 74 68 65 20 76 64 62 65 20 6d 61 6e 69  y, the vdbe mani
a9be0 70 75 6c 61 74 65 73 20 6e 65 61 72 6c 79 20 61  pulates nearly a
a9bf0 6c 6c 20 53 51 4c 20 76 61 6c 75 65 73 20 61 73  ll SQL values as
a9c00 20 4d 65 6d 0a 2a 2a 20 73 74 72 75 63 74 75 72   Mem.** structur
a9c10 65 73 2e 20 45 61 63 68 20 4d 65 6d 20 73 74 72  es. Each Mem str
a9c20 75 63 74 20 6d 61 79 20 63 61 63 68 65 20 6d 75  uct may cache mu
a9c30 6c 74 69 70 6c 65 20 72 65 70 72 65 73 65 6e 74  ltiple represent
a9c40 61 74 69 6f 6e 73 20 28 73 74 72 69 6e 67 2c 0a  ations (string,.
a9c50 2a 2a 20 69 6e 74 65 67 65 72 20 65 74 63 2e 29  ** integer etc.)
a9c60 20 6f 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c   of the same val
a9c70 75 65 2e 20 20 41 20 76 61 6c 75 65 20 28 61 6e  ue.  A value (an
a9c80 64 20 74 68 65 72 65 66 6f 72 65 20 4d 65 6d 20  d therefore Mem 
a9c90 73 74 72 75 63 74 75 72 65 29 0a 2a 2a 20 68 61  structure).** ha
a9ca0 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  s the following 
a9cb0 70 72 6f 70 65 72 74 69 65 73 3a 0a 2a 2a 0a 2a  properties:.**.*
a9cc0 2a 20 45 61 63 68 20 76 61 6c 75 65 20 68 61 73  * Each value has
a9cd0 20 61 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65   a manifest type
a9ce0 2e 20 54 68 65 20 6d 61 6e 69 66 65 73 74 20 74  . The manifest t
a9cf0 79 70 65 20 6f 66 20 74 68 65 20 76 61 6c 75 65  ype of the value
a9d00 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 20   stored.** in a 
a9d10 4d 65 6d 20 73 74 72 75 63 74 20 69 73 20 72 65  Mem struct is re
a9d20 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 4d 65  turned by the Me
a9d30 6d 54 79 70 65 28 4d 65 6d 2a 29 20 6d 61 63 72  mType(Mem*) macr
a9d40 6f 2e 20 54 68 65 20 74 79 70 65 20 69 73 0a 2a  o. The type is.*
a9d50 2a 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f  * one of SQLITE_
a9d60 4e 55 4c 4c 2c 20 53 51 4c 49 54 45 5f 49 4e 54  NULL, SQLITE_INT
a9d70 45 47 45 52 2c 20 53 51 4c 49 54 45 5f 52 45 41  EGER, SQLITE_REA
a9d80 4c 2c 20 53 51 4c 49 54 45 5f 54 45 58 54 20 6f  L, SQLITE_TEXT o
a9d90 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 4c 4f 42  r.** SQLITE_BLOB
a9da0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d 20  ..*/.struct Mem 
a9db0 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20  {.  union {.    
a9dc0 69 36 34 20 69 3b 20 20 20 20 20 20 20 20 20 20  i64 i;          
a9dd0 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76      /* Integer v
a9de0 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20 69 6e 74  alue. */.    int
a9df0 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20 20 20   nZero;         
a9e00 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 62 69   /* Used when bi
a9e10 74 20 4d 45 4d 5f 5a 65 72 6f 20 69 73 20 73 65  t MEM_Zero is se
a9e20 74 20 69 6e 20 66 6c 61 67 73 20 2a 2f 0a 20 20  t in flags */.  
a9e30 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 3b    FuncDef *pDef;
a9e40 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 6f 6e        /* Used on
a9e50 6c 79 20 77 68 65 6e 20 66 6c 61 67 73 3d 3d 4d  ly when flags==M
a9e60 45 4d 5f 41 67 67 20 2a 2f 0a 20 20 20 20 52 6f  EM_Agg */.    Ro
a9e70 77 53 65 74 20 2a 70 52 6f 77 53 65 74 3b 20 20  wSet *pRowSet;  
a9e80 20 20 2f 2a 20 55 73 65 64 20 6f 6e 6c 79 20 77    /* Used only w
a9e90 68 65 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 52  hen flags==MEM_R
a9ea0 6f 77 53 65 74 20 2a 2f 0a 20 20 20 20 56 64 62  owSet */.    Vdb
a9eb0 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20  eFrame *pFrame; 
a9ec0 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20 66 6c   /* Used when fl
a9ed0 61 67 73 3d 3d 4d 45 4d 5f 46 72 61 6d 65 20 2a  ags==MEM_Frame *
a9ee0 2f 0a 20 20 7d 20 75 3b 0a 20 20 64 6f 75 62 6c  /.  } u;.  doubl
a9ef0 65 20 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f  e r;           /
a9f00 2a 20 52 65 61 6c 20 76 61 6c 75 65 20 2a 2f 0a  * Real value */.
a9f10 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
a9f20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73        /* The ass
a9f30 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65  ociated database
a9f40 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
a9f50 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20   char *z;       
a9f60 20 20 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f       /* String o
a9f70 72 20 42 4c 4f 42 20 76 61 6c 75 65 20 2a 2f 0a  r BLOB value */.
a9f80 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20    int n;        
a9f90 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
a9fa0 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 69 6e  of characters in
a9fb0 20 73 74 72 69 6e 67 20 76 61 6c 75 65 2c 20 65   string value, e
a9fc0 78 63 6c 75 64 69 6e 67 20 27 5c 30 27 20 2a 2f  xcluding '\0' */
a9fd0 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20  .  u16 flags;   
a9fe0 20 20 20 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63         /* Some c
a9ff0 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 4d 45  ombination of ME
aa000 4d 5f 4e 75 6c 6c 2c 20 4d 45 4d 5f 53 74 72 2c  M_Null, MEM_Str,
aa010 20 4d 45 4d 5f 44 79 6e 2c 20 65 74 63 2e 20 2a   MEM_Dyn, etc. *
aa020 2f 0a 20 20 75 38 20 20 74 79 70 65 3b 20 20 20  /.  u8  type;   
aa030 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
aa040 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 53  f SQLITE_NULL, S
aa050 51 4c 49 54 45 5f 54 45 58 54 2c 20 53 51 4c 49  QLITE_TEXT, SQLI
aa060 54 45 5f 49 4e 54 45 47 45 52 2c 20 65 74 63 20  TE_INTEGER, etc 
aa070 2a 2f 0a 20 20 75 38 20 20 65 6e 63 3b 20 20 20  */.  u8  enc;   
aa080 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 49           /* SQLI
aa090 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f  TE_UTF8, SQLITE_
aa0a0 55 54 46 31 36 42 45 2c 20 53 51 4c 49 54 45 5f  UTF16BE, SQLITE_
aa0b0 55 54 46 31 36 4c 45 20 2a 2f 0a 20 20 76 6f 69  UTF16LE */.  voi
aa0c0 64 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a  d (*xDel)(void *
aa0d0 29 3b 20 20 2f 2a 20 49 66 20 6e 6f 74 20 6e 75  );  /* If not nu
aa0e0 6c 6c 2c 20 63 61 6c 6c 20 74 68 69 73 20 66 75  ll, call this fu
aa0f0 6e 63 74 69 6f 6e 20 74 6f 20 64 65 6c 65 74 65  nction to delete
aa100 20 4d 65 6d 2e 7a 20 2a 2f 0a 20 20 63 68 61 72   Mem.z */.  char
aa110 20 2a 7a 4d 61 6c 6c 6f 63 3b 20 20 20 20 20 20   *zMalloc;      
aa120 2f 2a 20 44 79 6e 61 6d 69 63 20 62 75 66 66 65  /* Dynamic buffe
aa130 72 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73  r allocated by s
aa140 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
aa150 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 4f 6e 65 20 6f 72  */.};../* One or
aa160 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 66 6f 6c   more of the fol
aa170 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 61 72 65  lowing flags are
aa180 20 73 65 74 20 74 6f 20 69 6e 64 69 63 61 74 65   set to indicate
aa190 20 74 68 65 20 76 61 6c 69 64 4f 4b 0a 2a 2a 20   the validOK.** 
aa1a0 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20  representations 
aa1b0 6f 66 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f  of the value sto
aa1c0 72 65 64 20 69 6e 20 74 68 65 20 4d 65 6d 20 73  red in the Mem s
aa1d0 74 72 75 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  truct..**.** If 
aa1e0 74 68 65 20 4d 45 4d 5f 4e 75 6c 6c 20 66 6c 61  the MEM_Null fla
aa1f0 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74  g is set, then t
aa200 68 65 20 76 61 6c 75 65 20 69 73 20 61 6e 20 53  he value is an S
aa210 51 4c 20 4e 55 4c 4c 20 76 61 6c 75 65 2e 0a 2a  QL NULL value..*
aa220 2a 20 4e 6f 20 6f 74 68 65 72 20 66 6c 61 67 73  * No other flags
aa230 20 6d 61 79 20 62 65 20 73 65 74 20 69 6e 20 74   may be set in t
aa240 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  his case..**.** 
aa250 49 66 20 74 68 65 20 4d 45 4d 5f 53 74 72 20 66  If the MEM_Str f
aa260 6c 61 67 20 69 73 20 73 65 74 20 74 68 65 6e 20  lag is set then 
aa270 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 61 74 20  Mem.z points at 
aa280 61 20 73 74 72 69 6e 67 20 72 65 70 72 65 73 65  a string represe
aa290 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 55 73 75 61  ntation..** Usua
aa2a0 6c 6c 79 20 74 68 69 73 20 69 73 20 65 6e 63 6f  lly this is enco
aa2b0 64 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20  ded in the same 
aa2c0 75 6e 69 63 6f 64 65 20 65 6e 63 6f 64 69 6e 67  unicode encoding
aa2d0 20 61 73 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20   as the main.** 
aa2e0 64 61 74 61 62 61 73 65 20 28 73 65 65 20 62 65  database (see be
aa2f0 6c 6f 77 20 66 6f 72 20 65 78 63 65 70 74 69 6f  low for exceptio
aa300 6e 73 29 2e 20 49 66 20 74 68 65 20 4d 45 4d 5f  ns). If the MEM_
aa310 54 65 72 6d 20 66 6c 61 67 20 69 73 20 61 6c 73  Term flag is als
aa320 6f 0a 2a 2a 20 73 65 74 2c 20 74 68 65 6e 20 74  o.** set, then t
aa330 68 65 20 73 74 72 69 6e 67 20 69 73 20 6e 75 6c  he string is nul
aa340 20 74 65 72 6d 69 6e 61 74 65 64 2e 20 54 68 65   terminated. The
aa350 20 4d 45 4d 5f 49 6e 74 20 61 6e 64 20 4d 45 4d   MEM_Int and MEM
aa360 5f 52 65 61 6c 20 0a 2a 2a 20 66 6c 61 67 73 20  _Real .** flags 
aa370 6d 61 79 20 63 6f 65 78 69 73 74 20 77 69 74 68  may coexist with
aa380 20 74 68 65 20 4d 45 4d 5f 53 74 72 20 66 6c 61   the MEM_Str fla
aa390 67 2e 0a 2a 2a 0a 2a 2a 20 4d 75 6c 74 69 70 6c  g..**.** Multipl
aa3a0 65 20 6f 66 20 74 68 65 73 65 20 76 61 6c 75 65  e of these value
aa3b0 73 20 63 61 6e 20 61 70 70 65 61 72 20 69 6e 20  s can appear in 
aa3c0 4d 65 6d 2e 66 6c 61 67 73 2e 20 20 42 75 74 20  Mem.flags.  But 
aa3d0 6f 6e 6c 79 20 6f 6e 65 0a 2a 2a 20 61 74 20 61  only one.** at a
aa3e0 20 74 69 6d 65 20 63 61 6e 20 61 70 70 65 61 72   time can appear
aa3f0 20 69 6e 20 4d 65 6d 2e 74 79 70 65 2e 0a 2a 2f   in Mem.type..*/
aa400 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 4e 75 6c  .#define MEM_Nul
aa410 6c 20 20 20 20 20 20 30 78 30 30 30 31 20 20 20  l      0x0001   
aa420 2f 2a 20 56 61 6c 75 65 20 69 73 20 4e 55 4c 4c  /* Value is NULL
aa430 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f   */.#define MEM_
aa440 53 74 72 20 20 20 20 20 20 20 30 78 30 30 30 32  Str       0x0002
aa450 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 61     /* Value is a
aa460 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69   string */.#defi
aa470 6e 65 20 4d 45 4d 5f 49 6e 74 20 20 20 20 20 20  ne MEM_Int      
aa480 20 30 78 30 30 30 34 20 20 20 2f 2a 20 56 61 6c   0x0004   /* Val
aa490 75 65 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  ue is an integer
aa4a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f   */.#define MEM_
aa4b0 52 65 61 6c 20 20 20 20 20 20 30 78 30 30 30 38  Real      0x0008
aa4c0 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 61     /* Value is a
aa4d0 20 72 65 61 6c 20 6e 75 6d 62 65 72 20 2a 2f 0a   real number */.
aa4e0 23 64 65 66 69 6e 65 20 4d 45 4d 5f 42 6c 6f 62  #define MEM_Blob
aa4f0 20 20 20 20 20 20 30 78 30 30 31 30 20 20 20 2f        0x0010   /
aa500 2a 20 56 61 6c 75 65 20 69 73 20 61 20 42 4c 4f  * Value is a BLO
aa510 42 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  B */.#define MEM
aa520 5f 52 6f 77 53 65 74 20 20 20 20 30 78 30 30 32  _RowSet    0x002
aa530 30 20 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20  0   /* Value is 
aa540 61 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20  a RowSet object 
aa550 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 46  */.#define MEM_F
aa560 72 61 6d 65 20 20 20 20 20 30 78 30 30 34 30 20  rame     0x0040 
aa570 20 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 20    /* Value is a 
aa580 56 64 62 65 46 72 61 6d 65 20 6f 62 6a 65 63 74  VdbeFrame object
aa590 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f   */.#define MEM_
aa5a0 54 79 70 65 4d 61 73 6b 20 20 30 78 30 30 66 66  TypeMask  0x00ff
aa5b0 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 74 79     /* Mask of ty
aa5c0 70 65 20 62 69 74 73 20 2a 2f 0a 0a 2f 2a 20 57  pe bits */../* W
aa5d0 68 65 6e 65 76 65 72 20 4d 65 6d 20 63 6f 6e 74  henever Mem cont
aa5e0 61 69 6e 73 20 61 20 76 61 6c 69 64 20 73 74 72  ains a valid str
aa5f0 69 6e 67 20 6f 72 20 62 6c 6f 62 20 72 65 70 72  ing or blob repr
aa600 65 73 65 6e 74 61 74 69 6f 6e 2c 20 6f 6e 65 20  esentation, one 
aa610 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77  of.** the follow
aa620 69 6e 67 20 66 6c 61 67 73 20 6d 75 73 74 20 62  ing flags must b
aa630 65 20 73 65 74 20 74 6f 20 64 65 74 65 72 6d 69  e set to determi
aa640 6e 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 6d 61  ne the memory ma
aa650 6e 61 67 65 6d 65 6e 74 0a 2a 2a 20 70 6f 6c 69  nagement.** poli
aa660 63 79 20 66 6f 72 20 4d 65 6d 2e 7a 2e 20 20 54  cy for Mem.z.  T
aa670 68 65 20 4d 45 4d 5f 54 65 72 6d 20 66 6c 61 67  he MEM_Term flag
aa680 20 74 65 6c 6c 73 20 75 73 20 77 68 65 74 68 65   tells us whethe
aa690 72 20 6f 72 20 6e 6f 74 20 74 68 65 0a 2a 2a 20  r or not the.** 
aa6a0 73 74 72 69 6e 67 20 69 73 20 5c 30 30 30 20 6f  string is \000 o
aa6b0 72 20 5c 75 30 30 30 30 20 74 65 72 6d 69 6e 61  r \u0000 termina
aa6c0 74 65 64 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  ted.*/.#define M
aa6d0 45 4d 5f 54 65 72 6d 20 20 20 20 20 20 30 78 30  EM_Term      0x0
aa6e0 32 30 30 20 20 20 2f 2a 20 53 74 72 69 6e 67 20  200   /* String 
aa6f0 72 65 70 20 69 73 20 6e 75 6c 20 74 65 72 6d 69  rep is nul termi
aa700 6e 61 74 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  nated */.#define
aa710 20 4d 45 4d 5f 44 79 6e 20 20 20 20 20 20 20 30   MEM_Dyn       0
aa720 78 30 34 30 30 20 20 20 2f 2a 20 4e 65 65 64 20  x0400   /* Need 
aa730 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 46 72  to call sqliteFr
aa740 65 65 28 29 20 6f 6e 20 4d 65 6d 2e 7a 20 2a 2f  ee() on Mem.z */
aa750 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 53 74 61  .#define MEM_Sta
aa760 74 69 63 20 20 20 20 30 78 30 38 30 30 20 20 20  tic    0x0800   
aa770 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20  /* Mem.z points 
aa780 74 6f 20 61 20 73 74 61 74 69 63 20 73 74 72 69  to a static stri
aa790 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45  ng */.#define ME
aa7a0 4d 5f 45 70 68 65 6d 20 20 20 20 20 30 78 31 30  M_Ephem     0x10
aa7b0 30 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f  00   /* Mem.z po
aa7c0 69 6e 74 73 20 74 6f 20 61 6e 20 65 70 68 65 6d  ints to an ephem
aa7d0 65 72 61 6c 20 73 74 72 69 6e 67 20 2a 2f 0a 23  eral string */.#
aa7e0 64 65 66 69 6e 65 20 4d 45 4d 5f 41 67 67 20 20  define MEM_Agg  
aa7f0 20 20 20 20 20 30 78 32 30 30 30 20 20 20 2f 2a       0x2000   /*
aa800 20 4d 65 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f   Mem.z points to
aa810 20 61 6e 20 61 67 67 20 66 75 6e 63 74 69 6f 6e   an agg function
aa820 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 23 64 65 66   context */.#def
aa830 69 6e 65 20 4d 45 4d 5f 5a 65 72 6f 20 20 20 20  ine MEM_Zero    
aa840 20 20 30 78 34 30 30 30 20 20 20 2f 2a 20 4d 65    0x4000   /* Me
aa850 6d 2e 69 20 63 6f 6e 74 61 69 6e 73 20 63 6f 75  m.i contains cou
aa860 6e 74 20 6f 66 20 30 73 20 61 70 70 65 6e 64 65  nt of 0s appende
aa870 64 20 74 6f 20 62 6c 6f 62 20 2a 2f 0a 0a 23 69  d to blob */..#i
aa880 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
aa890 5f 49 4e 43 52 42 4c 4f 42 0a 20 20 23 75 6e 64  _INCRBLOB.  #und
aa8a0 65 66 20 4d 45 4d 5f 5a 65 72 6f 0a 20 20 23 64  ef MEM_Zero.  #d
aa8b0 65 66 69 6e 65 20 4d 45 4d 5f 5a 65 72 6f 20 30  efine MEM_Zero 0
aa8c0 78 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f  x0000.#endif.../
aa8d0 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 79 20 65  *.** Clear any e
aa8e0 78 69 73 74 69 6e 67 20 74 79 70 65 20 66 6c 61  xisting type fla
aa8f0 67 73 20 66 72 6f 6d 20 61 20 4d 65 6d 20 61 6e  gs from a Mem an
aa900 64 20 72 65 70 6c 61 63 65 20 74 68 65 6d 20 77  d replace them w
aa910 69 74 68 20 66 0a 2a 2f 0a 23 64 65 66 69 6e 65  ith f.*/.#define
aa920 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28   MemSetTypeFlag(
aa930 70 2c 20 66 29 20 5c 0a 20 20 20 28 28 70 29 2d  p, f) \.   ((p)-
aa940 3e 66 6c 61 67 73 20 3d 20 28 28 70 29 2d 3e 66  >flags = ((p)->f
aa950 6c 61 67 73 26 7e 28 4d 45 4d 5f 54 79 70 65 4d  lags&~(MEM_TypeM
aa960 61 73 6b 7c 4d 45 4d 5f 5a 65 72 6f 29 29 7c 66  ask|MEM_Zero))|f
aa970 29 0a 0a 0a 2f 2a 20 41 20 56 64 62 65 46 75 6e  ).../* A VdbeFun
aa980 63 20 69 73 20 6a 75 73 74 20 61 20 46 75 6e 63  c is just a Func
aa990 44 65 66 20 28 64 65 66 69 6e 65 64 20 69 6e 20  Def (defined in 
aa9a0 73 71 6c 69 74 65 49 6e 74 2e 68 29 20 74 68 61  sqliteInt.h) tha
aa9b0 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 64  t contains.** ad
aa9c0 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
aa9d0 74 69 6f 6e 20 61 62 6f 75 74 20 61 75 78 69 6c  tion about auxil
aa9e0 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  iary information
aa9f0 20 62 6f 75 6e 64 20 74 6f 20 61 72 67 75 6d 65   bound to argume
aaa00 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 66 75  nts.** of the fu
aaa10 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73  nction.  This is
aaa20 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
aaa30 6e 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f 67  nt the sqlite3_g
aaa40 65 74 5f 61 75 78 64 61 74 61 28 29 0a 2a 2a 20  et_auxdata().** 
aaa50 61 6e 64 20 73 71 6c 69 74 65 33 5f 73 65 74 5f  and sqlite3_set_
aaa60 61 75 78 64 61 74 61 28 29 20 41 50 49 73 2e 20  auxdata() APIs. 
aaa70 20 54 68 65 20 22 61 75 78 64 61 74 61 22 20 69   The "auxdata" i
aaa80 73 20 73 6f 6d 65 20 61 75 78 69 6c 69 61 72 79  s some auxiliary
aaa90 20 64 61 74 61 0a 2a 2a 20 74 68 61 74 20 63 61   data.** that ca
aaaa0 6e 20 62 65 20 61 73 73 6f 63 69 61 74 65 64 20  n be associated 
aaab0 77 69 74 68 20 61 20 63 6f 6e 73 74 61 6e 74 20  with a constant 
aaac0 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 20 66 75  argument to a fu
aaad0 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 0a 2a 2a  nction.  This.**
aaae0 20 61 6c 6c 6f 77 73 20 66 75 6e 63 74 69 6f 6e   allows function
aaaf0 73 20 73 75 63 68 20 61 73 20 22 72 65 67 65 78  s such as "regex
aab00 70 22 20 74 6f 20 63 6f 6d 70 69 6c 65 20 74 68  p" to compile th
aab10 65 69 72 20 63 6f 6e 73 74 61 6e 74 20 72 65 67  eir constant reg
aab20 75 6c 61 72 0a 2a 2a 20 65 78 70 72 65 73 73 69  ular.** expressi
aab30 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f 6e 63 65  on argument once
aab40 20 61 6e 64 20 72 65 75 73 65 64 20 74 68 65 20   and reused the 
aab50 63 6f 6d 70 69 6c 65 64 20 63 6f 64 65 20 66 6f  compiled code fo
aab60 72 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 69 6e  r multiple.** in
aab70 76 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74  vocations..*/.st
aab80 72 75 63 74 20 56 64 62 65 46 75 6e 63 20 7b 0a  ruct VdbeFunc {.
aab90 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63    FuncDef *pFunc
aaba0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
aabb0 2f 2a 20 54 68 65 20 64 65 66 69 6e 69 74 69 6f  /* The definitio
aabc0 6e 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  n of the functio
aabd0 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 75 78 3b  n */.  int nAux;
aabe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aabf0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
aac00 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61  f entries alloca
aac10 74 65 64 20 66 6f 72 20 61 70 41 75 78 5b 5d 20  ted for apAux[] 
aac20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 75 78 44  */.  struct AuxD
aac30 61 74 61 20 7b 0a 20 20 20 20 76 6f 69 64 20 2a  ata {.    void *
aac40 70 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 20  pAux;           
aac50 20 20 20 20 20 20 20 20 2f 2a 20 41 75 78 20 64          /* Aux d
aac60 61 74 61 20 66 6f 72 20 74 68 65 20 69 2d 74 68  ata for the i-th
aac70 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 20 20 20   argument */.   
aac80 20 76 6f 69 64 20 28 2a 78 44 65 6c 65 74 65 29   void (*xDelete)
aac90 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 20 2f  (void *);      /
aaca0 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72  * Destructor for
aacb0 20 74 68 65 20 61 75 78 20 64 61 74 61 20 2a 2f   the aux data */
aacc0 0a 20 20 7d 20 61 70 41 75 78 5b 31 5d 3b 20 20  .  } apAux[1];  
aacd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aace0 20 2f 2a 20 4f 6e 65 20 73 6c 6f 74 20 66 6f 72   /* One slot for
aacf0 20 65 61 63 68 20 66 75 6e 63 74 69 6f 6e 20 61   each function a
aad00 72 67 75 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f  rgument */.};../
aad10 2a 0a 2a 2a 20 54 68 65 20 22 63 6f 6e 74 65 78  *.** The "contex
aad20 74 22 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20  t" argument for 
aad30 61 20 69 6e 73 74 61 6c 6c 61 62 6c 65 20 66 75  a installable fu
aad40 6e 63 74 69 6f 6e 2e 20 20 41 20 70 6f 69 6e 74  nction.  A point
aad50 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74  er to an.** inst
aad60 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
aad70 75 63 74 75 72 65 20 69 73 20 74 68 65 20 66 69  ucture is the fi
aad80 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
aad90 74 68 65 20 72 6f 75 74 69 6e 65 73 20 75 73 65  the routines use
aada0 64 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 74  d.** implement t
aadb0 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73  he SQL functions
aadc0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73  ..**.** There is
aadd0 20 61 20 74 79 70 65 64 65 66 20 66 6f 72 20 74   a typedef for t
aade0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 6e  his structure in
aadf0 20 73 71 6c 69 74 65 2e 68 2e 20 20 53 6f 20 61   sqlite.h.  So a
aae00 6c 6c 20 72 6f 75 74 69 6e 65 73 2c 0a 2a 2a 20  ll routines,.** 
aae10 65 76 65 6e 20 74 68 65 20 70 75 62 6c 69 63 20  even the public 
aae20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 53 51 4c  interface to SQL
aae30 69 74 65 2c 20 63 61 6e 20 75 73 65 20 61 20 70  ite, can use a p
aae40 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73  ointer to this s
aae50 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 42 75 74  tructure..** But
aae60 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 74 68   this file is th
aae70 65 20 6f 6e 6c 79 20 70 6c 61 63 65 20 77 68 65  e only place whe
aae80 72 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  re the internal 
aae90 64 65 74 61 69 6c 73 20 6f 66 20 74 68 69 73 0a  details of this.
aaea0 2a 2a 20 73 74 72 75 63 74 75 72 65 20 61 72 65  ** structure are
aaeb0 20 6b 6e 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68   known..**.** Th
aaec0 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
aaed0 64 65 66 69 6e 65 64 20 69 6e 73 69 64 65 20 6f  defined inside o
aaee0 66 20 76 64 62 65 49 6e 74 2e 68 20 62 65 63 61  f vdbeInt.h beca
aaef0 75 73 65 20 69 74 20 75 73 65 73 20 73 75 62 73  use it uses subs
aaf00 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 28 4d 65  tructures.** (Me
aaf10 6d 29 20 77 68 69 63 68 20 61 72 65 20 6f 6e 6c  m) which are onl
aaf20 79 20 64 65 66 69 6e 65 64 20 74 68 65 72 65 2e  y defined there.
aaf30 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74  .*/.struct sqlit
aaf40 65 33 5f 63 6f 6e 74 65 78 74 20 7b 0a 20 20 46  e3_context {.  F
aaf50 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 3b 20 20  uncDef *pFunc;  
aaf60 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
aaf70 74 6f 20 66 75 6e 63 74 69 6f 6e 20 69 6e 66 6f  to function info
aaf80 72 6d 61 74 69 6f 6e 2e 20 20 4d 55 53 54 20 42  rmation.  MUST B
aaf90 45 20 46 49 52 53 54 20 2a 2f 0a 20 20 56 64 62  E FIRST */.  Vdb
aafa0 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63  eFunc *pVdbeFunc
aafb0 3b 20 20 2f 2a 20 41 75 78 69 6c 61 72 79 20 64  ;  /* Auxilary d
aafc0 61 74 61 2c 20 69 66 20 63 72 65 61 74 65 64 2e  ata, if created.
aafd0 20 2a 2f 0a 20 20 4d 65 6d 20 73 3b 20 20 20 20   */.  Mem s;    
aafe0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
aaff0 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
ab000 69 73 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a  is stored here *
ab010 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20  /.  Mem *pMem;  
ab020 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d            /* Mem
ab030 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 74 6f  ory cell used to
ab040 20 73 74 6f 72 65 20 61 67 67 72 65 67 61 74 65   store aggregate
ab050 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e   context */.  in
ab060 74 20 69 73 45 72 72 6f 72 3b 20 20 20 20 20 20  t isError;      
ab070 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64      /* Error cod
ab080 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  e returned by th
ab090 65 20 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f 0a 20  e function. */. 
ab0a0 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
ab0b0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74         /* Collat
ab0c0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a  ing sequence */.
ab0d0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 53 65 74 20  };../*.** A Set 
ab0e0 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65  structure is use
ab0f0 64 20 66 6f 72 20 71 75 69 63 6b 20 74 65 73 74  d for quick test
ab100 69 6e 67 20 74 6f 20 73 65 65 20 69 66 20 61 20  ing to see if a 
ab110 76 61 6c 75 65 0a 2a 2a 20 69 73 20 70 61 72 74  value.** is part
ab120 20 6f 66 20 61 20 73 6d 61 6c 6c 20 73 65 74 2e   of a small set.
ab130 20 20 53 65 74 73 20 61 72 65 20 75 73 65 64 20    Sets are used 
ab140 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 63 6f 64  to implement cod
ab150 65 20 6c 69 6b 65 0a 2a 2a 20 74 68 69 73 3a 0a  e like.** this:.
ab160 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 78 2e  **            x.
ab170 79 20 49 4e 20 28 27 68 69 27 2c 27 68 6f 6f 27  y IN ('hi','hoo'
ab180 2c 27 68 75 6d 27 29 0a 2a 2f 0a 74 79 70 65 64  ,'hum').*/.typed
ab190 65 66 20 73 74 72 75 63 74 20 53 65 74 20 53 65  ef struct Set Se
ab1a0 74 3b 0a 73 74 72 75 63 74 20 53 65 74 20 7b 0a  t;.struct Set {.
ab1b0 20 20 48 61 73 68 20 68 61 73 68 3b 20 20 20 20    Hash hash;    
ab1c0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 65           /* A se
ab1d0 74 20 69 73 20 6a 75 73 74 20 61 20 68 61 73 68  t is just a hash
ab1e0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61 73 68   table */.  Hash
ab1f0 45 6c 65 6d 20 2a 70 72 65 76 3b 20 20 20 20 20  Elem *prev;     
ab200 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 6c 79     /* Previously
ab210 20 61 63 63 65 73 73 65 64 20 68 61 73 68 20 65   accessed hash e
ab220 6c 65 6d 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  lemen */.};../*.
ab230 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
ab240 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61  f the virtual ma
ab250 63 68 69 6e 65 2e 20 20 54 68 69 73 20 73 74 72  chine.  This str
ab260 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20  ucture contains 
ab270 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20  the complete.** 
ab280 73 74 61 74 65 20 6f 66 20 74 68 65 20 76 69 72  state of the vir
ab290 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a  tual machine..**
ab2a0 0a 2a 2a 20 54 68 65 20 22 73 71 6c 69 74 65 33  .** The "sqlite3
ab2b0 5f 73 74 6d 74 22 20 73 74 72 75 63 74 75 72 65  _stmt" structure
ab2c0 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73   pointer that is
ab2d0 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c   returned by sql
ab2e0 69 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29 0a 2a  ite3_compile().*
ab2f0 2a 20 69 73 20 72 65 61 6c 6c 79 20 61 20 70 6f  * is really a po
ab300 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74  inter to an inst
ab310 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
ab320 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  ucture..**.** Th
ab330 65 20 56 64 62 65 2e 69 6e 56 74 61 62 4d 65 74  e Vdbe.inVtabMet
ab340 68 6f 64 20 76 61 72 69 61 62 6c 65 20 69 73 20  hod variable is 
ab350 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20  set to non-zero 
ab360 66 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e  for the duration
ab370 20 6f 66 0a 2a 2a 20 61 6e 79 20 76 69 72 74 75   of.** any virtu
ab380 61 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f 64 20  al table method 
ab390 69 6e 76 6f 63 61 74 69 6f 6e 73 20 6d 61 64 65  invocations made
ab3a0 20 62 79 20 74 68 65 20 76 64 62 65 20 70 72 6f   by the vdbe pro
ab3b0 67 72 61 6d 2e 20 49 74 20 69 73 0a 2a 2a 20 73  gram. It is.** s
ab3c0 65 74 20 74 6f 20 32 20 66 6f 72 20 78 44 65 73  et to 2 for xDes
ab3d0 74 72 6f 79 20 6d 65 74 68 6f 64 20 63 61 6c 6c  troy method call
ab3e0 73 20 61 6e 64 20 31 20 66 6f 72 20 61 6c 6c 20  s and 1 for all 
ab3f0 6f 74 68 65 72 20 6d 65 74 68 6f 64 73 2e 20 54  other methods. T
ab400 68 69 73 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20  his.** variable 
ab410 69 73 20 75 73 65 64 20 66 6f 72 20 74 77 6f 20  is used for two 
ab420 70 75 72 70 6f 73 65 73 3a 20 74 6f 20 61 6c 6c  purposes: to all
ab430 6f 77 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68  ow xDestroy meth
ab440 6f 64 73 20 74 6f 20 65 78 65 63 75 74 65 0a 2a  ods to execute.*
ab450 2a 20 22 44 52 4f 50 20 54 41 42 4c 45 22 20 73  * "DROP TABLE" s
ab460 74 61 74 65 6d 65 6e 74 73 20 61 6e 64 20 74 6f  tatements and to
ab470 20 70 72 65 76 65 6e 74 20 73 6f 6d 65 20 6e 61   prevent some na
ab480 73 74 79 20 73 69 64 65 20 65 66 66 65 63 74 73  sty side effects
ab490 20 6f 66 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61   of.** malloc fa
ab4a0 69 6c 75 72 65 20 77 68 65 6e 20 53 51 4c 69 74  ilure when SQLit
ab4b0 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 72 65 63  e is invoked rec
ab4c0 75 72 73 69 76 65 6c 79 20 62 79 20 61 20 76 69  ursively by a vi
ab4d0 72 74 75 61 6c 20 74 61 62 6c 65 20 0a 2a 2a 20  rtual table .** 
ab4e0 6d 65 74 68 6f 64 20 66 75 6e 63 74 69 6f 6e 2e  method function.
ab4f0 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 20  .*/.struct Vdbe 
ab500 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
ab510 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
ab520 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
ab530 65 63 74 69 6f 6e 20 74 68 61 74 20 6f 77 6e 73  ection that owns
ab540 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20   this statement 
ab550 2a 2f 0a 20 20 56 64 62 65 20 2a 70 50 72 65 76  */.  Vdbe *pPrev
ab560 2c 2a 70 4e 65 78 74 3b 20 20 20 20 20 2f 2a 20  ,*pNext;     /* 
ab570 4c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 56  Linked list of V
ab580 44 42 45 73 20 77 69 74 68 20 74 68 65 20 73 61  DBEs with the sa
ab590 6d 65 20 56 64 62 65 2e 64 62 20 2a 2f 0a 20 20  me Vdbe.db */.  
ab5a0 69 6e 74 20 6e 4f 70 3b 20 20 20 20 20 20 20 20  int nOp;        
ab5b0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
ab5c0 72 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e  r of instruction
ab5d0 73 20 69 6e 20 74 68 65 20 70 72 6f 67 72 61 6d  s in the program
ab5e0 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70 41 6c 6c   */.  int nOpAll
ab5f0 6f 63 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  oc;           /*
ab600 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73   Number of slots
ab610 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61   allocated for a
ab620 4f 70 5b 5d 20 2a 2f 0a 20 20 4f 70 20 2a 61 4f  Op[] */.  Op *aO
ab630 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
ab640 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 68 6f    /* Space to ho
ab650 6c 64 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  ld the virtual m
ab660 61 63 68 69 6e 65 27 73 20 70 72 6f 67 72 61 6d  achine's program
ab670 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c   */.  int nLabel
ab680 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
ab690 20 4e 75 6d 62 65 72 20 6f 66 20 6c 61 62 65 6c   Number of label
ab6a0 73 20 75 73 65 64 20 2a 2f 0a 20 20 69 6e 74 20  s used */.  int 
ab6b0 6e 4c 61 62 65 6c 41 6c 6c 6f 63 3b 20 20 20 20  nLabelAlloc;    
ab6c0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
ab6d0 20 73 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64   slots allocated
ab6e0 20 69 6e 20 61 4c 61 62 65 6c 5b 5d 20 2a 2f 0a   in aLabel[] */.
ab6f0 20 20 69 6e 74 20 2a 61 4c 61 62 65 6c 3b 20 20    int *aLabel;  
ab700 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61            /* Spa
ab710 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6c  ce to hold the l
ab720 61 62 65 6c 73 20 2a 2f 0a 20 20 4d 65 6d 20 2a  abels */.  Mem *
ab730 2a 61 70 41 72 67 3b 20 20 20 20 20 20 20 20 20  *apArg;         
ab740 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20     /* Arguments 
ab750 74 6f 20 63 75 72 72 65 6e 74 6c 79 20 65 78 65  to currently exe
ab760 63 75 74 69 6e 67 20 75 73 65 72 20 66 75 6e 63  cuting user func
ab770 74 69 6f 6e 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61  tion */.  Mem *a
ab780 43 6f 6c 4e 61 6d 65 3b 20 20 20 20 20 20 20 20  ColName;        
ab790 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65    /* Column name
ab7a0 73 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20  s to return */. 
ab7b0 20 4d 65 6d 20 2a 70 52 65 73 75 6c 74 53 65 74   Mem *pResultSet
ab7c0 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  ;        /* Poin
ab7d0 74 65 72 20 74 6f 20 61 6e 20 61 72 72 61 79 20  ter to an array 
ab7e0 6f 66 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20 20  of results */.  
ab7f0 75 31 36 20 6e 52 65 73 43 6f 6c 75 6d 6e 3b 20  u16 nResColumn; 
ab800 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
ab810 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
ab820 6f 6e 65 20 72 6f 77 20 6f 66 20 74 68 65 20 72  one row of the r
ab830 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 75  esult set */.  u
ab840 31 36 20 6e 43 75 72 73 6f 72 3b 20 20 20 20 20  16 nCursor;     
ab850 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
ab860 20 6f 66 20 73 6c 6f 74 73 20 69 6e 20 61 70 43   of slots in apC
ab870 73 72 5b 5d 20 2a 2f 0a 20 20 56 64 62 65 43 75  sr[] */.  VdbeCu
ab880 72 73 6f 72 20 2a 2a 61 70 43 73 72 3b 20 20 20  rsor **apCsr;   
ab890 20 20 2f 2a 20 4f 6e 65 20 65 6c 65 6d 65 6e 74    /* One element
ab8a0 20 6f 66 20 74 68 69 73 20 61 72 72 61 79 20 66   of this array f
ab8b0 6f 72 20 65 61 63 68 20 6f 70 65 6e 20 63 75 72  or each open cur
ab8c0 73 6f 72 20 2a 2f 0a 20 20 75 38 20 65 72 72 6f  sor */.  u8 erro
ab8d0 72 41 63 74 69 6f 6e 3b 20 20 20 20 20 20 20 20  rAction;        
ab8e0 20 2f 2a 20 52 65 63 6f 76 65 72 79 20 61 63 74   /* Recovery act
ab8f0 69 6f 6e 20 74 6f 20 64 6f 20 69 6e 20 63 61 73  ion to do in cas
ab900 65 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 2a 2f  e of an error */
ab910 0a 20 20 75 38 20 6f 6b 56 61 72 3b 20 20 20 20  .  u8 okVar;    
ab920 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
ab930 75 65 20 69 66 20 61 7a 56 61 72 5b 5d 20 68 61  ue if azVar[] ha
ab940 73 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a  s been initializ
ab950 65 64 20 2a 2f 0a 20 20 75 31 36 20 6e 56 61 72  ed */.  u16 nVar
ab960 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ab970 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
ab980 72 69 65 73 20 69 6e 20 61 56 61 72 5b 5d 20 2a  ries in aVar[] *
ab990 2f 0a 20 20 4d 65 6d 20 2a 61 56 61 72 3b 20 20  /.  Mem *aVar;  
ab9a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
ab9b0 61 6c 75 65 73 20 66 6f 72 20 74 68 65 20 4f 50  alues for the OP
ab9c0 5f 56 61 72 69 61 62 6c 65 20 6f 70 63 6f 64 65  _Variable opcode
ab9d0 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a  . */.  char **az
ab9e0 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Var;           /
ab9f0 2a 20 4e 61 6d 65 20 6f 66 20 76 61 72 69 61 62  * Name of variab
aba00 6c 65 73 20 2a 2f 0a 20 20 75 33 32 20 6d 61 67  les */.  u32 mag
aba10 69 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ic;             
aba20 20 2f 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65 72   /* Magic number
aba30 20 66 6f 72 20 73 61 6e 69 74 79 20 63 68 65 63   for sanity chec
aba40 6b 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d  king */.  int nM
aba50 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  em;             
aba60 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d    /* Number of m
aba70 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 73 20  emory locations 
aba80 63 75 72 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61  currently alloca
aba90 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 4d  ted */.  Mem *aM
abaa0 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  em;             
abab0 20 2f 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 6c   /* The memory l
abac0 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 75 33  ocations */.  u3
abad0 32 20 63 61 63 68 65 43 74 72 3b 20 20 20 20 20  2 cacheCtr;     
abae0 20 20 20 20 20 20 2f 2a 20 56 64 62 65 43 75 72        /* VdbeCur
abaf0 73 6f 72 20 72 6f 77 20 63 61 63 68 65 20 67 65  sor row cache ge
abb00 6e 65 72 61 74 69 6f 6e 20 63 6f 75 6e 74 65 72  neration counter
abb10 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20   */.  int pc;   
abb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
abb30 20 54 68 65 20 70 72 6f 67 72 61 6d 20 63 6f 75   The program cou
abb40 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 63  nter */.  int rc
abb50 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
abb60 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65    /* Value to re
abb70 74 75 72 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a  turn */.  char *
abb80 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20 20  zErrMsg;        
abb90 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73 73 61    /* Error messa
abba0 67 65 20 77 72 69 74 74 65 6e 20 68 65 72 65 20  ge written here 
abbb0 2a 2f 0a 20 20 75 38 20 65 78 70 6c 61 69 6e 3b  */.  u8 explain;
abbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
abbd0 54 72 75 65 20 69 66 20 45 58 50 4c 41 49 4e 20  True if EXPLAIN 
abbe0 70 72 65 73 65 6e 74 20 6f 6e 20 53 51 4c 20 63  present on SQL c
abbf0 6f 6d 6d 61 6e 64 20 2a 2f 0a 20 20 75 38 20 63  ommand */.  u8 c
abc00 68 61 6e 67 65 43 6e 74 4f 6e 3b 20 20 20 20 20  hangeCntOn;     
abc10 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 75      /* True to u
abc20 70 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65  pdate the change
abc30 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 38  -counter */.  u8
abc40 20 65 78 70 69 72 65 64 3b 20 20 20 20 20 20 20   expired;       
abc50 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
abc60 20 74 68 65 20 56 4d 20 6e 65 65 64 73 20 74 6f   the VM needs to
abc70 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20 2a   be recompiled *
abc80 2f 0a 20 20 75 38 20 6d 69 6e 57 72 69 74 65 46  /.  u8 minWriteF
abc90 69 6c 65 46 6f 72 6d 61 74 3b 20 20 2f 2a 20 4d  ileFormat;  /* M
abca0 69 6e 69 6d 75 6d 20 66 69 6c 65 20 66 6f 72 6d  inimum file form
abcb0 61 74 20 66 6f 72 20 77 72 69 74 61 62 6c 65 20  at for writable 
abcc0 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 2a  database files *
abcd0 2f 0a 20 20 75 38 20 69 6e 56 74 61 62 4d 65 74  /.  u8 inVtabMet
abce0 68 6f 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 53  hod;        /* S
abcf0 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76  ee comments abov
abd00 65 20 2a 2f 0a 20 20 75 38 20 75 73 65 73 53 74  e */.  u8 usesSt
abd10 6d 74 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 2f  mtJournal;     /
abd20 2a 20 54 72 75 65 20 69 66 20 75 73 65 73 20 61  * True if uses a
abd30 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e   statement journ
abd40 61 6c 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f  al */.  u8 readO
abd50 6e 6c 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  nly;            
abd60 2f 2a 20 54 72 75 65 20 66 6f 72 20 72 65 61 64  /* True for read
abd70 2d 6f 6e 6c 79 20 73 74 61 74 65 6d 65 6e 74 73  -only statements
abd80 20 2a 2f 0a 20 20 75 38 20 69 73 50 72 65 70 61   */.  u8 isPrepa
abd90 72 65 56 32 3b 20 20 20 20 20 20 20 20 20 2f 2a  reV2;         /*
abda0 20 54 72 75 65 20 69 66 20 70 72 65 70 61 72 65   True if prepare
abdb0 64 20 77 69 74 68 20 70 72 65 70 61 72 65 5f 76  d with prepare_v
abdc0 32 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68  2() */.  int nCh
abdd0 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  ange;           
abde0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 62   /* Number of db
abdf0 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 73 69   changes made si
abe00 6e 63 65 20 6c 61 73 74 20 72 65 73 65 74 20 2a  nce last reset *
abe10 2f 0a 20 20 69 6e 74 20 62 74 72 65 65 4d 61 73  /.  int btreeMas
abe20 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42  k;          /* B
abe30 69 74 6d 61 73 6b 20 6f 66 20 64 62 2d 3e 61 44  itmask of db->aD
abe40 62 5b 5d 20 65 6e 74 72 69 65 73 20 72 65 66 65  b[] entries refe
abe50 72 65 6e 63 65 64 20 2a 2f 0a 20 20 69 36 34 20  renced */.  i64 
abe60 73 74 61 72 74 54 69 6d 65 3b 20 20 20 20 20 20  startTime;      
abe70 20 20 20 20 2f 2a 20 54 69 6d 65 20 77 68 65 6e      /* Time when
abe80 20 71 75 65 72 79 20 73 74 61 72 74 65 64 20 2d   query started -
abe90 20 75 73 65 64 20 66 6f 72 20 70 72 6f 66 69 6c   used for profil
abea0 69 6e 67 20 2a 2f 0a 20 20 42 74 72 65 65 4d 75  ing */.  BtreeMu
abeb0 74 65 78 41 72 72 61 79 20 61 4d 75 74 65 78 3b  texArray aMutex;
abec0 20 2f 2a 20 41 6e 20 61 72 72 61 79 20 6f 66 20   /* An array of 
abed0 42 74 72 65 65 20 75 73 65 64 20 68 65 72 65 20  Btree used here 
abee0 61 6e 64 20 6e 65 65 64 69 6e 67 20 6c 6f 63 6b  and needing lock
abef0 73 20 2a 2f 0a 20 20 69 6e 74 20 61 43 6f 75 6e  s */.  int aCoun
abf00 74 65 72 5b 32 5d 3b 20 20 20 20 20 20 20 20 2f  ter[2];        /
abf10 2a 20 43 6f 75 6e 74 65 72 73 20 75 73 65 64 20  * Counters used 
abf20 62 79 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f  by sqlite3_stmt_
abf30 73 74 61 74 75 73 28 29 20 2a 2f 0a 20 20 63 68  status() */.  ch
abf40 61 72 20 2a 7a 53 71 6c 3b 20 20 20 20 20 20 20  ar *zSql;       
abf50 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66        /* Text of
abf60 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
abf70 6e 74 20 74 68 61 74 20 67 65 6e 65 72 61 74 65  nt that generate
abf80 64 20 74 68 69 73 20 2a 2f 0a 20 20 76 6f 69 64  d this */.  void
abf90 20 2a 70 46 72 65 65 3b 20 20 20 20 20 20 20 20   *pFree;        
abfa0 20 20 20 20 2f 2a 20 46 72 65 65 20 74 68 69 73      /* Free this
abfb0 20 77 68 65 6e 20 64 65 6c 65 74 69 6e 67 20 74   when deleting t
abfc0 68 65 20 76 64 62 65 20 2a 2f 0a 20 20 69 36 34  he vdbe */.  i64
abfd0 20 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74 3b 20   nFkConstraint; 
abfe0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
abff0 66 20 69 6d 6d 2e 20 46 4b 20 63 6f 6e 73 74 72  f imm. FK constr
ac000 61 69 6e 74 73 20 74 68 69 73 20 56 4d 20 2a 2f  aints this VM */
ac010 0a 20 20 69 36 34 20 6e 53 74 6d 74 44 65 66 43  .  i64 nStmtDefC
ac020 6f 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  ons;       /* Nu
ac030 6d 62 65 72 20 6f 66 20 64 65 66 2e 20 63 6f 6e  mber of def. con
ac040 73 74 72 61 69 6e 74 73 20 77 68 65 6e 20 73 74  straints when st
ac050 6d 74 20 73 74 61 72 74 65 64 20 2a 2f 0a 20 20  mt started */.  
ac060 69 6e 74 20 69 53 74 61 74 65 6d 65 6e 74 3b 20  int iStatement; 
ac070 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74 65          /* State
ac080 6d 65 6e 74 20 6e 75 6d 62 65 72 20 28 6f 72 20  ment number (or 
ac090 30 20 69 66 20 68 61 73 20 6e 6f 74 20 6f 70 65  0 if has not ope
ac0a0 6e 65 64 20 73 74 6d 74 29 20 2a 2f 0a 23 69 66  ned stmt) */.#if
ac0b0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
ac0c0 0a 20 20 46 49 4c 45 20 2a 74 72 61 63 65 3b 20  .  FILE *trace; 
ac0d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
ac0e0 69 74 65 20 61 6e 20 65 78 65 63 75 74 69 6f 6e  ite an execution
ac0f0 20 74 72 61 63 65 20 68 65 72 65 2c 20 69 66 20   trace here, if 
ac100 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a 23 65 6e 64  not NULL */.#end
ac110 69 66 0a 20 20 56 64 62 65 46 72 61 6d 65 20 2a  if.  VdbeFrame *
ac120 70 46 72 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20  pFrame;      /* 
ac130 50 61 72 65 6e 74 20 66 72 61 6d 65 20 2a 2f 0a  Parent frame */.
ac140 20 20 69 6e 74 20 6e 46 72 61 6d 65 3b 20 20 20    int nFrame;   
ac150 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
ac160 62 65 72 20 6f 66 20 66 72 61 6d 65 73 20 69 6e  ber of frames in
ac170 20 70 46 72 61 6d 65 20 6c 69 73 74 20 2a 2f 0a   pFrame list */.
ac180 20 20 75 33 32 20 65 78 70 6d 61 73 6b 3b 20 20    u32 expmask;  
ac190 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69 6e            /* Bin
ac1a0 64 69 6e 67 20 74 6f 20 74 68 65 73 65 20 76 61  ding to these va
ac1b0 72 73 20 69 6e 76 61 6c 69 64 61 74 65 73 20 56  rs invalidates V
ac1c0 4d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  M */.};../*.** T
ac1d0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65  he following are
ac1e0 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73 20   allowed values 
ac1f0 66 6f 72 20 56 64 62 65 2e 6d 61 67 69 63 0a 2a  for Vdbe.magic.*
ac200 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d  /.#define VDBE_M
ac210 41 47 49 43 5f 49 4e 49 54 20 20 20 20 20 30 78  AGIC_INIT     0x
ac220 32 36 62 63 65 61 61 35 20 20 20 20 2f 2a 20 42  26bceaa5    /* B
ac230 75 69 6c 64 69 6e 67 20 61 20 56 44 42 45 20 70  uilding a VDBE p
ac240 72 6f 67 72 61 6d 20 2a 2f 0a 23 64 65 66 69 6e  rogram */.#defin
ac250 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e  e VDBE_MAGIC_RUN
ac260 20 20 20 20 20 20 30 78 62 64 66 32 30 64 61 33        0xbdf20da3
ac270 20 20 20 20 2f 2a 20 56 44 42 45 20 69 73 20 72      /* VDBE is r
ac280 65 61 64 79 20 74 6f 20 65 78 65 63 75 74 65 20  eady to execute 
ac290 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f  */.#define VDBE_
ac2a0 4d 41 47 49 43 5f 48 41 4c 54 20 20 20 20 20 30  MAGIC_HALT     0
ac2b0 78 35 31 39 63 32 39 37 33 20 20 20 20 2f 2a 20  x519c2973    /* 
ac2c0 56 44 42 45 20 68 61 73 20 63 6f 6d 70 6c 65 74  VDBE has complet
ac2d0 65 64 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f 0a  ed execution */.
ac2e0 23 64 65 66 69 6e 65 20 56 44 42 45 5f 4d 41 47  #define VDBE_MAG
ac2f0 49 43 5f 44 45 41 44 20 20 20 20 20 30 78 62 36  IC_DEAD     0xb6
ac300 30 36 63 33 63 38 20 20 20 20 2f 2a 20 54 68 65  06c3c8    /* The
ac310 20 56 44 42 45 20 68 61 73 20 62 65 65 6e 20 64   VDBE has been d
ac320 65 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a 2f  eallocated */../
ac330 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 70 72  *.** Function pr
ac340 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51 4c 49  ototypes.*/.SQLI
ac350 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
ac360 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43  sqlite3VdbeFreeC
ac370 75 72 73 6f 72 28 56 64 62 65 20 2a 2c 20 56 64  ursor(Vdbe *, Vd
ac380 62 65 43 75 72 73 6f 72 2a 29 3b 0a 76 6f 69 64  beCursor*);.void
ac390 20 73 71 6c 69 74 65 56 64 62 65 50 6f 70 53 74   sqliteVdbePopSt
ac3a0 61 63 6b 28 56 64 62 65 2a 2c 69 6e 74 29 3b 0a  ack(Vdbe*,int);.
ac3b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
ac3c0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 75  nt sqlite3VdbeCu
ac3d0 72 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 65 43  rsorMoveto(VdbeC
ac3e0 75 72 73 6f 72 2a 29 3b 0a 23 69 66 20 64 65 66  ursor*);.#if def
ac3f0 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
ac400 47 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 56 44  G) || defined(VD
ac410 42 45 5f 50 52 4f 46 49 4c 45 29 0a 53 51 4c 49  BE_PROFILE).SQLI
ac420 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
ac430 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74  sqlite3VdbePrint
ac440 4f 70 28 46 49 4c 45 2a 2c 20 69 6e 74 2c 20 4f  Op(FILE*, int, O
ac450 70 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49  p*);.#endif.SQLI
ac460 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73  TE_PRIVATE u32 s
ac470 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
ac480 54 79 70 65 4c 65 6e 28 75 33 32 29 3b 0a 53 51  TypeLen(u32);.SQ
ac490 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32  LITE_PRIVATE u32
ac4a0 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
ac4b0 61 6c 54 79 70 65 28 4d 65 6d 2a 2c 20 69 6e 74  alType(Mem*, int
ac4c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
ac4d0 45 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62  E u32 sqlite3Vdb
ac4e0 65 53 65 72 69 61 6c 50 75 74 28 75 6e 73 69 67  eSerialPut(unsig
ac4f0 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20  ned char*, int, 
ac500 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  Mem*, int);.SQLI
ac510 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73  TE_PRIVATE u32 s
ac520 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
ac530 47 65 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e  Get(const unsign
ac540 65 64 20 63 68 61 72 2a 2c 20 75 33 32 2c 20 4d  ed char*, u32, M
ac550 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  em*);.SQLITE_PRI
ac560 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
ac570 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 61  3VdbeDeleteAuxDa
ac580 74 61 28 56 64 62 65 46 75 6e 63 2a 2c 20 69 6e  ta(VdbeFunc*, in
ac590 74 29 3b 0a 0a 69 6e 74 20 73 71 6c 69 74 65 32  t);..int sqlite2
ac5a0 42 74 72 65 65 4b 65 79 43 6f 6d 70 61 72 65 28  BtreeKeyCompare(
ac5b0 42 74 43 75 72 73 6f 72 20 2a 2c 20 63 6f 6e 73  BtCursor *, cons
ac5c0 74 20 76 6f 69 64 20 2a 2c 20 69 6e 74 2c 20 69  t void *, int, i
ac5d0 6e 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49  nt, int *);.SQLI
ac5e0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
ac5f0 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65 79  qlite3VdbeIdxKey
ac600 43 6f 6d 70 61 72 65 28 56 64 62 65 43 75 72 73  Compare(VdbeCurs
ac610 6f 72 2a 2c 55 6e 70 61 63 6b 65 64 52 65 63 6f  or*,UnpackedReco
ac620 72 64 2a 2c 69 6e 74 2a 29 3b 0a 53 51 4c 49 54  rd*,int*);.SQLIT
ac630 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
ac640 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69  lite3VdbeIdxRowi
ac650 64 28 73 71 6c 69 74 65 33 2a 2c 20 42 74 43 75  d(sqlite3*, BtCu
ac660 72 73 6f 72 20 2a 2c 20 69 36 34 20 2a 29 3b 0a  rsor *, i64 *);.
ac670 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
ac680 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d  nt sqlite3MemCom
ac690 70 61 72 65 28 63 6f 6e 73 74 20 4d 65 6d 2a 2c  pare(const Mem*,
ac6a0 20 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e   const Mem*, con
ac6b0 73 74 20 43 6f 6c 6c 53 65 71 2a 29 3b 0a 53 51  st CollSeq*);.SQ
ac6c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
ac6d0 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63   sqlite3VdbeExec
ac6e0 28 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  (Vdbe*);.SQLITE_
ac6f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
ac700 74 65 33 56 64 62 65 4c 69 73 74 28 56 64 62 65  te3VdbeList(Vdbe
ac710 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
ac720 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
ac730 62 65 48 61 6c 74 28 56 64 62 65 2a 29 3b 0a 53  beHalt(Vdbe*);.S
ac740 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
ac750 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  t sqlite3VdbeCha
ac760 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65 6d 20  ngeEncoding(Mem 
ac770 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
ac780 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
ac790 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67  te3VdbeMemTooBig
ac7a0 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  (Mem*);.SQLITE_P
ac7b0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
ac7c0 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 4d 65  e3VdbeMemCopy(Me
ac7d0 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 29 3b  m*, const Mem*);
ac7e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ac7f0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
ac800 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 4d  MemShallowCopy(M
ac810 65 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 2c  em*, const Mem*,
ac820 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
ac830 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
ac840 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 4d 65  e3VdbeMemMove(Me
ac850 6d 2a 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54  m*, Mem*);.SQLIT
ac860 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
ac870 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54  lite3VdbeMemNulT
ac880 65 72 6d 69 6e 61 74 65 28 4d 65 6d 2a 29 3b 0a  erminate(Mem*);.
ac890 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
ac8a0 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
ac8b0 6d 53 65 74 53 74 72 28 4d 65 6d 2a 2c 20 63 6f  mSetStr(Mem*, co
ac8c0 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20  nst char*, int, 
ac8d0 75 38 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64  u8, void(*)(void
ac8e0 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  *));.SQLITE_PRIV
ac8f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
ac900 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28  VdbeMemSetInt64(
ac910 4d 65 6d 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49  Mem*, i64);.SQLI
ac920 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
ac930 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
ac940 74 44 6f 75 62 6c 65 28 4d 65 6d 2a 2c 20 64 6f  tDouble(Mem*, do
ac950 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52  uble);.SQLITE_PR
ac960 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
ac970 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c  e3VdbeMemSetNull
ac980 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  (Mem*);.SQLITE_P
ac990 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
ac9a0 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72  te3VdbeMemSetZer
ac9b0 6f 42 6c 6f 62 28 4d 65 6d 2a 2c 69 6e 74 29 3b  oBlob(Mem*,int);
ac9c0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
ac9d0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
ac9e0 4d 65 6d 53 65 74 52 6f 77 53 65 74 28 4d 65 6d  MemSetRowSet(Mem
ac9f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
aca00 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
aca10 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62  beMemMakeWriteab
aca20 6c 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  le(Mem*);.SQLITE
aca30 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
aca40 69 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e  ite3VdbeMemStrin
aca50 67 69 66 79 28 4d 65 6d 2a 2c 20 69 6e 74 29 3b  gify(Mem*, int);
aca60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
aca70 69 36 34 20 73 71 6c 69 74 65 33 56 64 62 65 49  i64 sqlite3VdbeI
aca80 6e 74 56 61 6c 75 65 28 4d 65 6d 2a 29 3b 0a 53  ntValue(Mem*);.S
aca90 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
acaa0 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
acab0 49 6e 74 65 67 65 72 69 66 79 28 4d 65 6d 2a 29  Integerify(Mem*)
acac0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
acad0 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65 33 56   double sqlite3V
acae0 64 62 65 52 65 61 6c 56 61 6c 75 65 28 4d 65 6d  dbeRealValue(Mem
acaf0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
acb00 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
acb10 64 62 65 49 6e 74 65 67 65 72 41 66 66 69 6e 69  dbeIntegerAffini
acb20 74 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  ty(Mem*);.SQLITE
acb30 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
acb40 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69  ite3VdbeMemReali
acb50 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  fy(Mem*);.SQLITE
acb60 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
acb70 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72  ite3VdbeMemNumer
acb80 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54  ify(Mem*);.SQLIT
acb90 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
acba0 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d  lite3VdbeMemFrom
acbb0 42 74 72 65 65 28 42 74 43 75 72 73 6f 72 2a 2c  Btree(BtCursor*,
acbc0 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 4d 65 6d 2a  int,int,int,Mem*
acbd0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
acbe0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
acbf0 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d 65 6d  beMemRelease(Mem
acc00 20 2a 70 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49   *p);.SQLITE_PRI
acc10 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
acc20 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45  3VdbeMemReleaseE
acc30 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a 70 29 3b  xternal(Mem *p);
acc40 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
acc50 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
acc60 65 6d 46 69 6e 61 6c 69 7a 65 28 4d 65 6d 2a 2c  emFinalize(Mem*,
acc70 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51 4c 49   FuncDef*);.SQLI
acc80 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
acc90 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4f 70   char *sqlite3Op
acca0 63 6f 64 65 4e 61 6d 65 28 69 6e 74 29 3b 0a 53  codeName(int);.S
accb0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
accc0 74 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63  t sqlite3VdbeOpc
accd0 6f 64 65 48 61 73 50 72 6f 70 65 72 74 79 28 69  odeHasProperty(i
acce0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
accf0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
acd00 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
acd10 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e  Mem *pMem, int n
acd20 2c 20 69 6e 74 20 70 72 65 73 65 72 76 65 29 3b  , int preserve);
acd30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
acd40 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43  int sqlite3VdbeC
acd50 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 56 64  loseStatement(Vd
acd60 62 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  be *, int);.SQLI
acd70 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
acd80 73 71 6c 69 74 65 33 56 64 62 65 46 72 61 6d 65  sqlite3VdbeFrame
acd90 44 65 6c 65 74 65 28 56 64 62 65 46 72 61 6d 65  Delete(VdbeFrame
acda0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
acdb0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
acdc0 62 65 46 72 61 6d 65 52 65 73 74 6f 72 65 28 56  beFrameRestore(V
acdd0 64 62 65 46 72 61 6d 65 20 2a 29 3b 0a 53 51 4c  dbeFrame *);.SQL
acde0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
acdf0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
ace00 74 6f 72 65 54 79 70 65 28 4d 65 6d 20 2a 70 4d  toreType(Mem *pM
ace10 65 6d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  em);..#ifndef SQ
ace20 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47  LITE_OMIT_FOREIG
ace30 4e 5f 4b 45 59 0a 53 51 4c 49 54 45 5f 50 52 49  N_KEY.SQLITE_PRI
ace40 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
ace50 56 64 62 65 43 68 65 63 6b 46 6b 28 56 64 62 65  VdbeCheckFk(Vdbe
ace60 20 2a 2c 20 69 6e 74 29 3b 0a 23 65 6c 73 65 0a   *, int);.#else.
ace70 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
ace80 56 64 62 65 43 68 65 63 6b 46 6b 28 70 2c 69 29  VdbeCheckFk(p,i)
ace90 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64   0.#endif..#ifnd
acea0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
aceb0 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49  HARED_CACHE.SQLI
acec0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
aced0 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78  sqlite3VdbeMutex
acee0 41 72 72 61 79 45 6e 74 65 72 28 56 64 62 65 20  ArrayEnter(Vdbe 
acef0 2a 70 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  *p);.#else.# def
acf00 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ine sqlite3VdbeM
acf10 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 70  utexArrayEnter(p
acf20 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45  ).#endif..SQLITE
acf30 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
acf40 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73  ite3VdbeMemTrans
acf50 6c 61 74 65 28 4d 65 6d 2a 2c 20 75 38 29 3b 0a  late(Mem*, u8);.
acf60 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
acf70 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
acf80 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
acf90 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 56 64  3VdbePrintSql(Vd
acfa0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
acfb0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
acfc0 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79  te3VdbeMemPretty
acfd0 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c  Print(Mem *pMem,
acfe0 20 63 68 61 72 20 2a 7a 42 75 66 29 3b 0a 23 65   char *zBuf);.#e
acff0 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56  ndif.SQLITE_PRIV
ad000 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
ad010 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28  dbeMemHandleBom(
ad020 4d 65 6d 20 2a 70 4d 65 6d 29 3b 0a 0a 23 69 66  Mem *pMem);..#if
ad030 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
ad040 5f 49 4e 43 52 42 4c 4f 42 0a 53 51 4c 49 54 45  _INCRBLOB.SQLITE
ad050 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
ad060 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70  qlite3VdbeMemExp
ad070 61 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a 29 3b 0a  andBlob(Mem *);.
ad080 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20  #else.  #define 
ad090 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78  sqlite3VdbeMemEx
ad0a0 70 61 6e 64 42 6c 6f 62 28 78 29 20 53 51 4c 49  pandBlob(x) SQLI
ad0b0 54 45 5f 4f 4b 0a 23 65 6e 64 69 66 0a 0a 23 65  TE_OK.#endif..#e
ad0c0 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
ad0d0 28 5f 56 44 42 45 49 4e 54 5f 48 5f 29 20 2a 2f  (_VDBEINT_H_) */
ad0e0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
ad0f0 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 49 6e 74  * End of vdbeInt
ad100 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
ad110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ad120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ad130 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
ad140 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
ad150 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
ad160 6e 20 75 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  n utf.c ********
ad170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ad180 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
ad190 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 0a 2f  E_AMALGAMATION./
ad1a0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
ad1b0 6e 67 20 63 6f 6e 73 74 61 6e 74 20 76 61 6c 75  ng constant valu
ad1c0 65 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65  e is used by the
ad1d0 20 53 51 4c 49 54 45 5f 42 49 47 45 4e 44 49 41   SQLITE_BIGENDIA
ad1e0 4e 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45 5f  N and.** SQLITE_
ad1f0 4c 49 54 54 4c 45 45 4e 44 49 41 4e 20 6d 61 63  LITTLEENDIAN mac
ad200 72 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ros..*/.SQLITE_P
ad210 52 49 56 41 54 45 20 63 6f 6e 73 74 20 69 6e 74  RIVATE const int
ad220 20 73 71 6c 69 74 65 33 6f 6e 65 20 3d 20 31 3b   sqlite3one = 1;
ad230 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
ad240 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20 2a  E_AMALGAMATION *
ad250 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6c 6f  /../*.** This lo
ad260 6f 6b 75 70 20 74 61 62 6c 65 20 69 73 20 75 73  okup table is us
ad270 65 64 20 74 6f 20 68 65 6c 70 20 64 65 63 6f 64  ed to help decod
ad280 65 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65  e the first byte
ad290 20 6f 66 0a 2a 2a 20 61 20 6d 75 6c 74 69 2d 62   of.** a multi-b
ad2a0 79 74 65 20 55 54 46 38 20 63 68 61 72 61 63 74  yte UTF8 charact
ad2b0 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f  er..*/.static co
ad2c0 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
ad2d0 72 20 73 71 6c 69 74 65 33 55 74 66 38 54 72 61  r sqlite3Utf8Tra
ad2e0 6e 73 31 5b 5d 20 3d 20 7b 0a 20 20 30 78 30 30  ns1[] = {.  0x00
ad2f0 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78  , 0x01, 0x02, 0x
ad300 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20  03, 0x04, 0x05, 
ad310 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78  0x06, 0x07,.  0x
ad320 30 38 2c 20 30 78 30 39 2c 20 30 78 30 61 2c 20  08, 0x09, 0x0a, 
ad330 30 78 30 62 2c 20 30 78 30 63 2c 20 30 78 30 64  0x0b, 0x0c, 0x0d
ad340 2c 20 30 78 30 65 2c 20 30 78 30 66 2c 0a 20 20  , 0x0e, 0x0f,.  
ad350 30 78 31 30 2c 20 30 78 31 31 2c 20 30 78 31 32  0x10, 0x11, 0x12
ad360 2c 20 30 78 31 33 2c 20 30 78 31 34 2c 20 30 78  , 0x13, 0x14, 0x
ad370 31 35 2c 20 30 78 31 36 2c 20 30 78 31 37 2c 0a  15, 0x16, 0x17,.
ad380 20 20 30 78 31 38 2c 20 30 78 31 39 2c 20 30 78    0x18, 0x19, 0x
ad390 31 61 2c 20 30 78 31 62 2c 20 30 78 31 63 2c 20  1a, 0x1b, 0x1c, 
ad3a0 30 78 31 64 2c 20 30 78 31 65 2c 20 30 78 31 66  0x1d, 0x1e, 0x1f
ad3b0 2c 0a 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20  ,.  0x00, 0x01, 
ad3c0 30 78 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34  0x02, 0x03, 0x04
ad3d0 2c 20 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78  , 0x05, 0x06, 0x
ad3e0 30 37 2c 0a 20 20 30 78 30 38 2c 20 30 78 30 39  07,.  0x08, 0x09
ad3f0 2c 20 30 78 30 61 2c 20 30 78 30 62 2c 20 30 78  , 0x0a, 0x0b, 0x
ad400 30 63 2c 20 30 78 30 64 2c 20 30 78 30 65 2c 20  0c, 0x0d, 0x0e, 
ad410 30 78 30 66 2c 0a 20 20 30 78 30 30 2c 20 30 78  0x0f,.  0x00, 0x
ad420 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33 2c 20  01, 0x02, 0x03, 
ad430 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78 30 36  0x04, 0x05, 0x06
ad440 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 30 2c 20  , 0x07,.  0x00, 
ad450 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33  0x01, 0x02, 0x03
ad460 2c 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78  , 0x00, 0x01, 0x
ad470 30 30 2c 20 30 78 30 30 2c 0a 7d 3b 0a 0a 0a 23  00, 0x00,.};...#
ad480 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55 54 46  define WRITE_UTF
ad490 38 28 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20  8(zOut, c) {    
ad4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad4b0 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3c        \.  if( c<
ad4c0 30 78 30 30 30 38 30 20 29 7b 20 20 20 20 20 20  0x00080 ){      
ad4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ad4f0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28  .    *zOut++ = (
ad500 75 38 29 28 63 26 30 78 46 46 29 3b 20 20 20 20  u8)(c&0xFF);    
ad510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad520 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20          \.  }   
ad530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad560 20 5c 0a 20 20 65 6c 73 65 20 69 66 28 20 63 3c   \.  else if( c<
ad570 30 78 30 30 38 30 30 20 29 7b 20 20 20 20 20 20  0x00800 ){      
ad580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad590 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
ad5a0 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 43 30 20 2b  *zOut++ = 0xC0 +
ad5b0 20 28 75 38 29 28 28 63 3e 3e 36 29 26 30 78 31   (u8)((c>>6)&0x1
ad5c0 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  F);             
ad5d0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
ad5e0 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28 63   = 0x80 + (u8)(c
ad5f0 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20   & 0x3F);       
ad600 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
ad610 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }               
ad620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad640 20 20 20 20 20 5c 0a 20 20 65 6c 73 65 20 69 66       \.  else if
ad650 28 20 63 3c 30 78 31 30 30 30 30 20 29 7b 20 20  ( c<0x10000 ){  
ad660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ad680 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78      *zOut++ = 0x
ad690 45 30 20 2b 20 28 75 38 29 28 28 63 3e 3e 31 32  E0 + (u8)((c>>12
ad6a0 29 26 30 78 30 46 29 3b 20 20 20 20 20 20 20 20  )&0x0F);        
ad6b0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f         \.    *zO
ad6c0 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 75  ut++ = 0x80 + (u
ad6d0 38 29 28 28 63 3e 3e 36 29 20 26 20 30 78 33 46  8)((c>>6) & 0x3F
ad6e0 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
ad6f0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
ad700 30 78 38 30 20 2b 20 28 75 38 29 28 63 20 26 20  0x80 + (u8)(c & 
ad710 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20  0x3F);          
ad720 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c           \.  }el
ad730 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  se{             
ad740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad760 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
ad770 3d 20 30 78 46 30 20 2b 20 28 75 38 29 28 28 63  = 0xF0 + (u8)((c
ad780 3e 3e 31 38 29 20 26 20 30 78 30 37 29 3b 20 20  >>18) & 0x07);  
ad790 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
ad7a0 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20   *zOut++ = 0x80 
ad7b0 2b 20 28 75 38 29 28 28 63 3e 3e 31 32 29 20 26  + (u8)((c>>12) &
ad7c0 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20 20   0x3F);         
ad7d0 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
ad7e0 2b 20 3d 20 30 78 38 30 20 2b 20 28 75 38 29 28  + = 0x80 + (u8)(
ad7f0 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29 3b 20  (c>>6) & 0x3F); 
ad800 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
ad810 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38     *zOut++ = 0x8
ad820 30 20 2b 20 28 75 38 29 28 63 20 26 20 30 78 33  0 + (u8)(c & 0x3
ad830 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  F);             
ad840 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20        \.  }     
ad850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ad880 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 54  .}..#define WRIT
ad890 45 5f 55 54 46 31 36 4c 45 28 7a 4f 75 74 2c 20  E_UTF16LE(zOut, 
ad8a0 63 29 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  c) {            
ad8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad8c0 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20          \.  if( 
ad8d0 63 3c 3d 30 78 46 46 46 46 20 29 7b 20 20 20 20  c<=0xFFFF ){    
ad8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ad910 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75      *zOut++ = (u
ad920 38 29 28 63 26 30 78 30 30 46 46 29 3b 20 20 20  8)(c&0x00FF);   
ad930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad950 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
ad960 2b 20 3d 20 28 75 38 29 28 28 63 3e 3e 38 29 26  + = (u8)((c>>8)&
ad970 30 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20  0x00FF);        
ad980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad990 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65            \.  }e
ad9a0 6c 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20  lse{            
ad9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad9c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad9d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ad9e0 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
ad9f0 28 75 38 29 28 28 28 63 3e 3e 31 30 29 26 30 78  (u8)(((c>>10)&0x
ada00 30 30 33 46 29 20 2b 20 28 28 28 63 2d 30 78 31  003F) + (((c-0x1
ada10 30 30 30 30 29 3e 3e 31 30 29 26 30 78 30 30 43  0000)>>10)&0x00C
ada20 30 29 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75  0));  \.    *zOu
ada30 74 2b 2b 20 3d 20 28 75 38 29 28 30 78 30 30 44  t++ = (u8)(0x00D
ada40 38 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30  8 + (((c-0x10000
ada50 29 3e 3e 31 38 29 26 30 78 30 33 29 29 3b 20 20  )>>18)&0x03));  
ada60 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
ada70 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29    *zOut++ = (u8)
ada80 28 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20  (c&0x00FF);     
ada90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adab0 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
adac0 3d 20 28 75 38 29 28 30 78 30 30 44 43 20 2b 20  = (u8)(0x00DC + 
adad0 28 28 63 3e 3e 38 29 26 30 78 30 33 29 29 3b 20  ((c>>8)&0x03)); 
adae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adaf0 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20          \.  }   
adb00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
adb40 7d 0a 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45  }..#define WRITE
adb50 5f 55 54 46 31 36 42 45 28 7a 4f 75 74 2c 20 63  _UTF16BE(zOut, c
adb60 29 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  ) {             
adb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adb80 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63         \.  if( c
adb90 3c 3d 30 78 46 46 46 46 20 29 7b 20 20 20 20 20  <=0xFFFF ){     
adba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adbb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adbc0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
adbd0 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38     *zOut++ = (u8
adbe0 29 28 28 63 3e 3e 38 29 26 30 78 30 30 46 46 29  )((c>>8)&0x00FF)
adbf0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
adc00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adc10 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
adc20 20 3d 20 28 75 38 29 28 63 26 30 78 30 30 46 46   = (u8)(c&0x00FF
adc30 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
adc40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adc50 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c           \.  }el
adc60 73 65 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  se{             
adc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adc90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
adca0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28  .    *zOut++ = (
adcb0 75 38 29 28 30 78 30 30 44 38 20 2b 20 28 28 28  u8)(0x00D8 + (((
adcc0 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 38 29 26  c-0x10000)>>18)&
adcd0 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 20  0x03));         
adce0 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
adcf0 2b 2b 20 3d 20 28 75 38 29 28 28 28 63 3e 3e 31  ++ = (u8)(((c>>1
add00 30 29 26 30 78 30 30 33 46 29 20 2b 20 28 28 28  0)&0x003F) + (((
add10 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 30 29 26  c-0x10000)>>10)&
add20 30 78 30 30 43 30 29 29 3b 20 20 5c 0a 20 20 20  0x00C0));  \.   
add30 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 75 38 29 28   *zOut++ = (u8)(
add40 30 78 30 30 44 43 20 2b 20 28 28 63 3e 3e 38 29  0x00DC + ((c>>8)
add50 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20  &0x03));        
add60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
add70 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
add80 20 28 75 38 29 28 63 26 30 78 30 30 46 46 29 3b   (u8)(c&0x00FF);
add90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
addb0 20 20 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20         \.  }    
addc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
addd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
addf0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d               \.}
ade00 0a 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55  ..#define READ_U
ade10 54 46 31 36 4c 45 28 7a 49 6e 2c 20 54 45 52 4d  TF16LE(zIn, TERM
ade20 2c 20 63 29 7b 20 20 20 20 20 20 20 20 20 20 20  , c){           
ade30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade40 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20          \.  c = 
ade50 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20  (*zIn++);       
ade60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ade90 5c 0a 20 20 63 20 2b 3d 20 28 28 2a 7a 49 6e 2b  \.  c += ((*zIn+
adea0 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20  +)<<8);         
adeb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aded0 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20          \.  if( 
adee0 63 3e 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30  c>=0xD800 && c<0
adef0 78 45 30 30 30 20 26 26 20 54 45 52 4d 20 29 7b  xE000 && TERM ){
adf00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf20 5c 0a 20 20 20 20 69 6e 74 20 63 32 20 3d 20 28  \.    int c2 = (
adf30 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20  *zIn++);        
adf40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adf60 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 32          \.    c2
adf70 20 2b 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38   += ((*zIn++)<<8
adf80 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
adf90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
adfb0 5c 0a 20 20 20 20 63 20 3d 20 28 63 32 26 30 78  \.    c = (c2&0x
adfc0 30 33 46 46 29 20 2b 20 28 28 63 26 30 78 30 30  03FF) + ((c&0x00
adfd0 33 46 29 3c 3c 31 30 29 20 2b 20 28 28 28 63 26  3F)<<10) + (((c&
adfe0 30 78 30 33 43 30 29 2b 30 78 30 30 34 30 29 3c  0x03C0)+0x0040)<
adff0 3c 31 30 29 3b 20 20 20 5c 0a 20 20 7d 20 20 20  <10);   \.  }   
ae000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae040 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20 52 45 41  \.}..#define REA
ae050 44 5f 55 54 46 31 36 42 45 28 7a 49 6e 2c 20 54  D_UTF16BE(zIn, T
ae060 45 52 4d 2c 20 63 29 7b 20 20 20 20 20 20 20 20  ERM, c){        
ae070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae080 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 63             \.  c
ae090 20 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29   = ((*zIn++)<<8)
ae0a0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
ae0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae0c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae0d0 20 20 20 5c 0a 20 20 63 20 2b 3d 20 28 2a 7a 49     \.  c += (*zI
ae0e0 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20  n++);           
ae0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae110 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69             \.  i
ae120 66 28 20 63 3e 3d 30 78 44 38 30 30 20 26 26 20  f( c>=0xD800 && 
ae130 63 3c 30 78 45 30 30 30 20 26 26 20 54 45 52 4d  c<0xE000 && TERM
ae140 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20   ){             
ae150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae160 20 20 20 5c 0a 20 20 20 20 69 6e 74 20 63 32 20     \.    int c2 
ae170 3d 20 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b  = ((*zIn++)<<8);
ae180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae1a0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
ae1b0 20 63 32 20 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b   c2 += (*zIn++);
ae1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae1f0 20 20 20 5c 0a 20 20 20 20 63 20 3d 20 28 63 32     \.    c = (c2
ae200 26 30 78 30 33 46 46 29 20 2b 20 28 28 63 26 30  &0x03FF) + ((c&0
ae210 78 30 30 33 46 29 3c 3c 31 30 29 20 2b 20 28 28  x003F)<<10) + ((
ae220 28 63 26 30 78 30 33 43 30 29 2b 30 78 30 30 34  (c&0x03C0)+0x004
ae230 30 29 3c 3c 31 30 29 3b 20 20 20 5c 0a 20 20 7d  0)<<10);   \.  }
ae240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae270 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae280 20 20 20 5c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72     \.}../*.** Tr
ae290 61 6e 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65  anslate a single
ae2a0 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72   UTF-8 character
ae2b0 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 75 6e  .  Return the un
ae2c0 69 63 6f 64 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a  icode value..**.
ae2d0 2a 2a 20 44 75 72 69 6e 67 20 74 72 61 6e 73 6c  ** During transl
ae2e0 61 74 69 6f 6e 2c 20 61 73 73 75 6d 65 20 74 68  ation, assume th
ae2f0 61 74 20 74 68 65 20 62 79 74 65 20 74 68 61 74  at the byte that
ae300 20 7a 54 65 72 6d 20 70 6f 69 6e 74 73 0a 2a 2a   zTerm points.**
ae310 20 69 73 20 61 20 30 78 30 30 2e 0a 2a 2a 0a 2a   is a 0x00..**.*
ae320 2a 20 57 72 69 74 65 20 61 20 70 6f 69 6e 74 65  * Write a pointe
ae330 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 75 6e  r to the next un
ae340 72 65 61 64 20 62 79 74 65 20 62 61 63 6b 20 69  read byte back i
ae350 6e 74 6f 20 2a 70 7a 4e 65 78 74 2e 0a 2a 2a 0a  nto *pzNext..**.
ae360 2a 2a 20 4e 6f 74 65 73 20 4f 6e 20 49 6e 76 61  ** Notes On Inva
ae370 6c 69 64 20 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a  lid UTF-8:.**.**
ae380 20 20 2a 20 20 54 68 69 73 20 72 6f 75 74 69 6e    *  This routin
ae390 65 20 6e 65 76 65 72 20 61 6c 6c 6f 77 73 20 61  e never allows a
ae3a0 20 37 2d 62 69 74 20 63 68 61 72 61 63 74 65 72   7-bit character
ae3b0 20 28 30 78 30 30 20 74 68 72 6f 75 67 68 20 30   (0x00 through 0
ae3c0 78 37 66 29 20 74 6f 0a 2a 2a 20 20 20 20 20 62  x7f) to.**     b
ae3d0 65 20 65 6e 63 6f 64 65 64 20 61 73 20 61 20 6d  e encoded as a m
ae3e0 75 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 63  ulti-byte charac
ae3f0 74 65 72 2e 20 20 41 6e 79 20 6d 75 6c 74 69 2d  ter.  Any multi-
ae400 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 74  byte character t
ae410 68 61 74 0a 2a 2a 20 20 20 20 20 61 74 74 65 6d  hat.**     attem
ae420 70 74 73 20 74 6f 20 65 6e 63 6f 64 65 20 61 20  pts to encode a 
ae430 76 61 6c 75 65 20 62 65 74 77 65 65 6e 20 30 78  value between 0x
ae440 30 30 20 61 6e 64 20 30 78 37 66 20 69 73 20 72  00 and 0x7f is r
ae450 65 6e 64 65 72 65 64 20 61 73 20 30 78 66 66 66  endered as 0xfff
ae460 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20 20 54 68 69  d..**.**  *  Thi
ae470 73 20 72 6f 75 74 69 6e 65 20 6e 65 76 65 72 20  s routine never 
ae480 61 6c 6c 6f 77 73 20 61 20 55 54 46 31 36 20 73  allows a UTF16 s
ae490 75 72 72 6f 67 61 74 65 20 76 61 6c 75 65 20 74  urrogate value t
ae4a0 6f 20 62 65 20 65 6e 63 6f 64 65 64 2e 0a 2a 2a  o be encoded..**
ae4b0 20 20 20 20 20 49 66 20 61 20 6d 75 6c 74 69 2d       If a multi-
ae4c0 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20 61  byte character a
ae4d0 74 74 65 6d 70 74 73 20 74 6f 20 65 6e 63 6f 64  ttempts to encod
ae4e0 65 20 61 20 76 61 6c 75 65 20 62 65 74 77 65 65  e a value betwee
ae4f0 6e 0a 2a 2a 20 20 20 20 20 30 78 64 38 30 30 20  n.**     0xd800 
ae500 61 6e 64 20 30 78 65 30 30 30 20 74 68 65 6e 20  and 0xe000 then 
ae510 69 74 20 69 73 20 72 65 6e 64 65 72 65 64 20 61  it is rendered a
ae520 73 20 30 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20  s 0xfffd..**.** 
ae530 20 2a 20 20 42 79 74 65 73 20 69 6e 20 74 68 65   *  Bytes in the
ae540 20 72 61 6e 67 65 20 6f 66 20 30 78 38 30 20 74   range of 0x80 t
ae550 68 72 6f 75 67 68 20 30 78 62 66 20 77 68 69 63  hrough 0xbf whic
ae560 68 20 6f 63 63 75 72 20 61 73 20 74 68 65 20 66  h occur as the f
ae570 69 72 73 74 0a 2a 2a 20 20 20 20 20 62 79 74 65  irst.**     byte
ae580 20 6f 66 20 61 20 63 68 61 72 61 63 74 65 72 20   of a character 
ae590 61 72 65 20 69 6e 74 65 72 70 72 65 74 65 64 20  are interpreted 
ae5a0 61 73 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63  as single-byte c
ae5b0 68 61 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20  haracters.**    
ae5c0 20 61 6e 64 20 72 65 6e 64 65 72 65 64 20 61 73   and rendered as
ae5d0 20 74 68 65 6d 73 65 6c 76 65 73 20 65 76 65 6e   themselves even
ae5e0 20 74 68 6f 75 67 68 20 74 68 65 79 20 61 72 65   though they are
ae5f0 20 74 65 63 68 6e 69 63 61 6c 6c 79 0a 2a 2a 20   technically.** 
ae600 20 20 20 20 69 6e 76 61 6c 69 64 20 63 68 61 72      invalid char
ae610 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 2a  acters..**.**  *
ae620 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61    This routine a
ae630 63 63 65 70 74 73 20 61 6e 20 69 6e 66 69 6e 69  ccepts an infini
ae640 74 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 66  te number of dif
ae650 66 65 72 65 6e 74 20 55 54 46 38 20 65 6e 63 6f  ferent UTF8 enco
ae660 64 69 6e 67 73 0a 2a 2a 20 20 20 20 20 66 6f 72  dings.**     for
ae670 20 75 6e 69 63 6f 64 65 20 76 61 6c 75 65 73 20   unicode values 
ae680 30 78 38 30 20 61 6e 64 20 67 72 65 61 74 65 72  0x80 and greater
ae690 2e 20 20 49 74 20 64 6f 20 6e 6f 74 20 63 68 61  .  It do not cha
ae6a0 6e 67 65 20 6f 76 65 72 2d 6c 65 6e 67 74 68 0a  nge over-length.
ae6b0 2a 2a 20 20 20 20 20 65 6e 63 6f 64 69 6e 67 73  **     encodings
ae6c0 20 74 6f 20 30 78 66 66 66 64 20 61 73 20 73 6f   to 0xfffd as so
ae6d0 6d 65 20 73 79 73 74 65 6d 73 20 72 65 63 6f 6d  me systems recom
ae6e0 6d 65 6e 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  mend..*/.#define
ae6f0 20 52 45 41 44 5f 55 54 46 38 28 7a 49 6e 2c 20   READ_UTF8(zIn, 
ae700 7a 54 65 72 6d 2c 20 63 29 20 20 20 20 20 20 20  zTerm, c)       
ae710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae720 20 20 20 20 5c 0a 20 20 63 20 3d 20 2a 28 7a 49      \.  c = *(zI
ae730 6e 2b 2b 29 3b 20 20 20 20 20 20 20 20 20 20 20  n++);           
ae740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae760 20 5c 0a 20 20 69 66 28 20 63 3e 3d 30 78 63 30   \.  if( c>=0xc0
ae770 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20   ){             
ae780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
ae7a0 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55      c = sqlite3U
ae7b0 74 66 38 54 72 61 6e 73 31 5b 63 2d 30 78 63 30  tf8Trans1[c-0xc0
ae7c0 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ];              
ae7d0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
ae7e0 20 77 68 69 6c 65 28 20 7a 49 6e 21 3d 7a 54 65   while( zIn!=zTe
ae7f0 72 6d 20 26 26 20 28 2a 7a 49 6e 20 26 20 30 78  rm && (*zIn & 0x
ae800 63 30 29 3d 3d 30 78 38 30 20 29 7b 20 20 20 20  c0)==0x80 ){    
ae810 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 20 20          \.      
ae820 63 20 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30 78  c = (c<<6) + (0x
ae830 33 66 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b 20  3f & *(zIn++)); 
ae840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae850 20 20 20 20 20 5c 0a 20 20 20 20 7d 20 20 20 20       \.    }    
ae860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae890 20 20 5c 0a 20 20 20 20 69 66 28 20 63 3c 30 78    \.    if( c<0x
ae8a0 38 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20  80              
ae8b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ae8c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
ae8d0 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26 30  .        || (c&0
ae8e0 78 46 46 46 46 46 38 30 30 29 3d 3d 30 78 44 38  xFFFFF800)==0xD8
ae8f0 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20  00              
ae900 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
ae910 20 20 20 20 20 20 7c 7c 20 28 63 26 30 78 46 46        || (c&0xFF
ae920 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46 45 20  FFFFFE)==0xFFFE 
ae930 29 7b 20 20 63 20 3d 20 30 78 46 46 46 44 3b 20  ){  c = 0xFFFD; 
ae940 7d 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 53  }        \.  }.S
ae950 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
ae960 74 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  t sqlite3Utf8Rea
ae970 64 28 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  d(.  const unsig
ae980 6e 65 64 20 63 68 61 72 20 2a 7a 49 6e 2c 20 20  ned char *zIn,  
ae990 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79       /* First by
ae9a0 74 65 20 6f 66 20 55 54 46 2d 38 20 63 68 61 72  te of UTF-8 char
ae9b0 61 63 74 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74  acter */.  const
ae9c0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
ae9d0 2a 70 7a 4e 65 78 74 20 20 20 20 2f 2a 20 57 72  *pzNext    /* Wr
ae9e0 69 74 65 20 66 69 72 73 74 20 62 79 74 65 20 70  ite first byte p
ae9f0 61 73 74 20 55 54 46 2d 38 20 63 68 61 72 20 68  ast UTF-8 char h
aea00 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
aea10 63 3b 0a 0a 20 20 2f 2a 20 53 61 6d 65 20 61 73  c;..  /* Same as
aea20 20 52 45 41 44 5f 55 54 46 38 28 29 20 61 62 6f   READ_UTF8() abo
aea30 76 65 20 62 75 74 20 77 69 74 68 6f 75 74 20 74  ve but without t
aea40 68 65 20 7a 54 65 72 6d 20 70 61 72 61 6d 65 74  he zTerm paramet
aea50 65 72 2e 0a 20 20 2a 2a 20 46 6f 72 20 74 68 69  er..  ** For thi
aea60 73 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 73  s routine, we as
aea70 73 75 6d 65 20 74 68 65 20 55 54 46 38 20 73 74  sume the UTF8 st
aea80 72 69 6e 67 20 69 73 20 61 6c 77 61 79 73 20 7a  ring is always z
aea90 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e 0a  ero-terminated..
aeaa0 20 20 2a 2f 0a 20 20 63 20 3d 20 2a 28 7a 49 6e    */.  c = *(zIn
aeab0 2b 2b 29 3b 0a 20 20 69 66 28 20 63 3e 3d 30 78  ++);.  if( c>=0x
aeac0 63 30 20 29 7b 0a 20 20 20 20 63 20 3d 20 73 71  c0 ){.    c = sq
aead0 6c 69 74 65 33 55 74 66 38 54 72 61 6e 73 31 5b  lite3Utf8Trans1[
aeae0 63 2d 30 78 63 30 5d 3b 0a 20 20 20 20 77 68 69  c-0xc0];.    whi
aeaf0 6c 65 28 20 28 2a 7a 49 6e 20 26 20 30 78 63 30  le( (*zIn & 0xc0
aeb00 29 3d 3d 30 78 38 30 20 29 7b 0a 20 20 20 20 20  )==0x80 ){.     
aeb10 20 63 20 3d 20 28 63 3c 3c 36 29 20 2b 20 28 30   c = (c<<6) + (0
aeb20 78 33 66 20 26 20 2a 28 7a 49 6e 2b 2b 29 29 3b  x3f & *(zIn++));
aeb30 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 63  .    }.    if( c
aeb40 3c 30 78 38 30 0a 20 20 20 20 20 20 20 20 7c 7c  <0x80.        ||
aeb50 20 28 63 26 30 78 46 46 46 46 46 38 30 30 29 3d   (c&0xFFFFF800)=
aeb60 3d 30 78 44 38 30 30 0a 20 20 20 20 20 20 20 20  =0xD800.        
aeb70 7c 7c 20 28 63 26 30 78 46 46 46 46 46 46 46 45  || (c&0xFFFFFFFE
aeb80 29 3d 3d 30 78 46 46 46 45 20 29 7b 20 20 63 20  )==0xFFFE ){  c 
aeb90 3d 20 30 78 46 46 46 44 3b 20 7d 0a 20 20 7d 0a  = 0xFFFD; }.  }.
aeba0 20 20 2a 70 7a 4e 65 78 74 20 3d 20 7a 49 6e 3b    *pzNext = zIn;
aebb0 0a 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a 0a  .  return c;.}..
aebc0 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  .../*.** If the 
aebd0 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45 20  TRANSLATE_TRACE 
aebe0 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65 64  macro is defined
aebf0 2c 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 65  , the value of e
aec00 61 63 68 20 4d 65 6d 20 69 73 0a 2a 2a 20 70 72  ach Mem is.** pr
aec10 69 6e 74 65 64 20 6f 6e 20 73 74 64 65 72 72 20  inted on stderr 
aec20 6f 6e 20 74 68 65 20 77 61 79 20 69 6e 74 6f 20  on the way into 
aec30 61 6e 64 20 6f 75 74 20 6f 66 20 73 71 6c 69 74  and out of sqlit
aec40 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c 61  e3VdbeMemTransla
aec50 74 65 28 29 2e 0a 2a 2f 20 0a 2f 2a 20 23 64 65  te()..*/ ./* #de
aec60 66 69 6e 65 20 54 52 41 4e 53 4c 41 54 45 5f 54  fine TRANSLATE_T
aec70 52 41 43 45 20 31 20 2a 2f 0a 0a 23 69 66 6e 64  RACE 1 */..#ifnd
aec80 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
aec90 54 46 31 36 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  TF16./*.** This 
aeca0 72 6f 75 74 69 6e 65 20 74 72 61 6e 73 66 6f 72  routine transfor
aecb0 6d 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  ms the internal 
aecc0 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75 73  text encoding us
aecd0 65 64 20 62 79 20 70 4d 65 6d 20 74 6f 0a 2a 2a  ed by pMem to.**
aece0 20 64 65 73 69 72 65 64 45 6e 63 2e 20 49 74 20   desiredEnc. It 
aecf0 69 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74  is an error if t
aed00 68 65 20 73 74 72 69 6e 67 20 69 73 20 61 6c 72  he string is alr
aed10 65 61 64 79 20 6f 66 20 74 68 65 20 64 65 73 69  eady of the desi
aed20 72 65 64 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 2c  red.** encoding,
aed30 20 6f 72 20 69 66 20 2a 70 4d 65 6d 20 64 6f 65   or if *pMem doe
aed40 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20  s not contain a 
aed50 73 74 72 69 6e 67 20 76 61 6c 75 65 2e 0a 2a 2f  string value..*/
aed60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
aed70 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
aed80 65 6d 54 72 61 6e 73 6c 61 74 65 28 4d 65 6d 20  emTranslate(Mem 
aed90 2a 70 4d 65 6d 2c 20 75 38 20 64 65 73 69 72 65  *pMem, u8 desire
aeda0 64 45 6e 63 29 7b 0a 20 20 69 6e 74 20 6c 65 6e  dEnc){.  int len
aedb0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
aedc0 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
aedd0 6c 65 6e 67 74 68 20 6f 66 20 6f 75 74 70 75 74  length of output
aede0 20 73 74 72 69 6e 67 20 69 6e 20 62 79 74 65 73   string in bytes
aedf0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   */.  unsigned c
aee00 68 61 72 20 2a 7a 4f 75 74 3b 20 20 20 20 20 20  har *zOut;      
aee10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
aee20 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a  utput buffer */.
aee30 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
aee40 2a 7a 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20  *zIn;           
aee50 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75 74          /* Input
aee60 20 69 74 65 72 61 74 6f 72 20 2a 2f 0a 20 20 75   iterator */.  u
aee70 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 54  nsigned char *zT
aee80 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  erm;            
aee90 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 69       /* End of i
aeea0 6e 70 75 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  nput */.  unsign
aeeb0 65 64 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20  ed char *z;     
aeec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aeed0 2f 2a 20 4f 75 74 70 75 74 20 69 74 65 72 61 74  /* Output iterat
aeee0 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  or */.  unsigned
aeef0 20 69 6e 74 20 63 3b 0a 0a 20 20 61 73 73 65 72   int c;..  asser
aef00 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c  t( pMem->db==0 |
aef10 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  | sqlite3_mutex_
aef20 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d  held(pMem->db->m
aef30 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
aef40 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d  t( pMem->flags&M
aef50 45 4d 5f 53 74 72 20 29 3b 0a 20 20 61 73 73 65  EM_Str );.  asse
aef60 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64  rt( pMem->enc!=d
aef70 65 73 69 72 65 64 45 6e 63 20 29 3b 0a 20 20 61  esiredEnc );.  a
aef80 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63  ssert( pMem->enc
aef90 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
aefa0 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 0a   pMem->n>=0 );..
aefb0 23 69 66 20 64 65 66 69 6e 65 64 28 54 52 41 4e  #if defined(TRAN
aefc0 53 4c 41 54 45 5f 54 52 41 43 45 29 20 26 26 20  SLATE_TRACE) && 
aefd0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
aefe0 45 42 55 47 29 0a 20 20 7b 0a 20 20 20 20 63 68  EBUG).  {.    ch
aeff0 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20  ar zBuf[100];.  
af000 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
af010 50 72 65 74 74 79 50 72 69 6e 74 28 70 4d 65 6d  PrettyPrint(pMem
af020 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 72  , zBuf);.    fpr
af030 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 49 4e  intf(stderr, "IN
af040 50 55 54 3a 20 20 25 73 5c 6e 22 2c 20 7a 42 75  PUT:  %s\n", zBu
af050 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  f);.  }.#endif..
af060 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72 61 6e    /* If the tran
af070 73 6c 61 74 69 6f 6e 20 69 73 20 62 65 74 77 65  slation is betwe
af080 65 6e 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65  en UTF-16 little
af090 20 61 6e 64 20 62 69 67 20 65 6e 64 69 61 6e 2c   and big endian,
af0a0 20 74 68 65 6e 20 0a 20 20 2a 2a 20 61 6c 6c 20   then .  ** all 
af0b0 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65 64  that is required
af0c0 20 69 73 20 74 6f 20 73 77 61 70 20 74 68 65 20   is to swap the 
af0d0 62 79 74 65 20 6f 72 64 65 72 2e 20 54 68 69 73  byte order. This
af0e0 20 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65 64   case is handled
af0f0 0a 20 20 2a 2a 20 64 69 66 66 65 72 65 6e 74 6c  .  ** differentl
af100 79 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65 72  y from the other
af110 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4d  s..  */.  if( pM
af120 65 6d 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45 5f  em->enc!=SQLITE_
af130 55 54 46 38 20 26 26 20 64 65 73 69 72 65 64 45  UTF8 && desiredE
af140 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20  nc!=SQLITE_UTF8 
af150 29 7b 0a 20 20 20 20 75 38 20 74 65 6d 70 3b 0a  ){.    u8 temp;.
af160 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20      int rc;.    
af170 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
af180 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65  MemMakeWriteable
af190 28 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20  (pMem);.    if( 
af1a0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
af1b0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72  .      assert( r
af1c0 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
af1d0 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
af1e0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
af1f0 20 20 7d 0a 20 20 20 20 7a 49 6e 20 3d 20 28 75    }.    zIn = (u
af200 38 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20  8*)pMem->z;.    
af210 7a 54 65 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65  zTerm = &zIn[pMe
af220 6d 2d 3e 6e 26 7e 31 5d 3b 0a 20 20 20 20 77 68  m->n&~1];.    wh
af230 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29  ile( zIn<zTerm )
af240 7b 0a 20 20 20 20 20 20 74 65 6d 70 20 3d 20 2a  {.      temp = *
af250 7a 49 6e 3b 0a 20 20 20 20 20 20 2a 7a 49 6e 20  zIn;.      *zIn 
af260 3d 20 2a 28 7a 49 6e 2b 31 29 3b 0a 20 20 20 20  = *(zIn+1);.    
af270 20 20 7a 49 6e 2b 2b 3b 0a 20 20 20 20 20 20 2a    zIn++;.      *
af280 7a 49 6e 2b 2b 20 3d 20 74 65 6d 70 3b 0a 20 20  zIn++ = temp;.  
af290 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e    }.    pMem->en
af2a0 63 20 3d 20 64 65 73 69 72 65 64 45 6e 63 3b 0a  c = desiredEnc;.
af2b0 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 6c 61      goto transla
af2c0 74 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f  te_out;.  }..  /
af2d0 2a 20 53 65 74 20 6c 65 6e 20 74 6f 20 74 68 65  * Set len to the
af2e0 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
af2f0 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65  of bytes require
af300 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74 20  d in the output 
af310 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 69 66 28  buffer. */.  if(
af320 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c   desiredEnc==SQL
af330 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20  ITE_UTF8 ){.    
af340 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 65 72 74 69  /* When converti
af350 6e 67 20 66 72 6f 6d 20 55 54 46 2d 31 36 2c 20  ng from UTF-16, 
af360 74 68 65 20 6d 61 78 69 6d 75 6d 20 67 72 6f 77  the maximum grow
af370 74 68 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 0a  th results from.
af380 20 20 20 20 2a 2a 20 74 72 61 6e 73 6c 61 74 69      ** translati
af390 6e 67 20 61 20 32 2d 62 79 74 65 20 63 68 61 72  ng a 2-byte char
af3a0 61 63 74 65 72 20 74 6f 20 61 20 34 2d 62 79 74  acter to a 4-byt
af3b0 65 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65  e UTF-8 characte
af3c0 72 2e 0a 20 20 20 20 2a 2a 20 41 20 73 69 6e 67  r..    ** A sing
af3d0 6c 65 20 62 79 74 65 20 69 73 20 72 65 71 75 69  le byte is requi
af3e0 72 65 64 20 66 6f 72 20 74 68 65 20 6f 75 74 70  red for the outp
af3f0 75 74 20 73 74 72 69 6e 67 0a 20 20 20 20 2a 2a  ut string.    **
af400 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 2e   nul-terminator.
af410 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4d 65 6d  .    */.    pMem
af420 2d 3e 6e 20 26 3d 20 7e 31 3b 0a 20 20 20 20 6c  ->n &= ~1;.    l
af430 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20 32  en = pMem->n * 2
af440 20 2b 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   + 1;.  }else{. 
af450 20 20 20 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 65     /* When conve
af460 72 74 69 6e 67 20 66 72 6f 6d 20 55 54 46 2d 38  rting from UTF-8
af470 20 74 6f 20 55 54 46 2d 31 36 20 74 68 65 20 6d   to UTF-16 the m
af480 61 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 69 73  aximum growth is
af490 20 63 61 75 73 65 64 0a 20 20 20 20 2a 2a 20 77   caused.    ** w
af4a0 68 65 6e 20 61 20 31 2d 62 79 74 65 20 55 54 46  hen a 1-byte UTF
af4b0 2d 38 20 63 68 61 72 61 63 74 65 72 20 69 73 20  -8 character is 
af4c0 74 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f 20  translated into 
af4d0 61 20 32 2d 62 79 74 65 20 55 54 46 2d 31 36 0a  a 2-byte UTF-16.
af4e0 20 20 20 20 2a 2a 20 63 68 61 72 61 63 74 65 72      ** character
af4f0 2e 20 54 77 6f 20 62 79 74 65 73 20 61 72 65 20  . Two bytes are 
af500 72 65 71 75 69 72 65 64 20 69 6e 20 74 68 65 20  required in the 
af510 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 66 6f  output buffer fo
af520 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e 75 6c  r the.    ** nul
af530 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20 20  -terminator..   
af540 20 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d 20 70 4d   */.    len = pM
af550 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 32 3b 0a 20  em->n * 2 + 2;. 
af560 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 7a 49 6e   }..  /* Set zIn
af570 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65   to point at the
af580 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 69 6e   start of the in
af590 70 75 74 20 62 75 66 66 65 72 20 61 6e 64 20 7a  put buffer and z
af5a0 54 65 72 6d 20 74 6f 20 70 6f 69 6e 74 20 31 0a  Term to point 1.
af5b0 20 20 2a 2a 20 62 79 74 65 20 70 61 73 74 20 74    ** byte past t
af5c0 68 65 20 65 6e 64 2e 0a 20 20 2a 2a 0a 20 20 2a  he end..  **.  *
af5d0 2a 20 56 61 72 69 61 62 6c 65 20 7a 4f 75 74 20  * Variable zOut 
af5e0 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20  is set to point 
af5f0 61 74 20 74 68 65 20 6f 75 74 70 75 74 20 62 75  at the output bu
af600 66 66 65 72 2c 20 73 70 61 63 65 20 6f 62 74 61  ffer, space obta
af610 69 6e 65 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 73  ined.  ** from s
af620 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e  qlite3_malloc().
af630 0a 20 20 2a 2f 0a 20 20 7a 49 6e 20 3d 20 28 75  .  */.  zIn = (u
af640 38 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 7a 54  8*)pMem->z;.  zT
af650 65 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d 2d  erm = &zIn[pMem-
af660 3e 6e 5d 3b 0a 20 20 7a 4f 75 74 20 3d 20 73 71  >n];.  zOut = sq
af670 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
af680 28 70 4d 65 6d 2d 3e 64 62 2c 20 6c 65 6e 29 3b  (pMem->db, len);
af690 0a 20 20 69 66 28 20 21 7a 4f 75 74 20 29 7b 0a  .  if( !zOut ){.
af6a0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
af6b0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 7a  E_NOMEM;.  }.  z
af6c0 20 3d 20 7a 4f 75 74 3b 0a 0a 20 20 69 66 28 20   = zOut;..  if( 
af6d0 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54  pMem->enc==SQLIT
af6e0 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 69 66  E_UTF8 ){.    if
af6f0 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51  ( desiredEnc==SQ
af700 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a  LITE_UTF16LE ){.
af710 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 2d        /* UTF-8 -
af720 3e 20 55 54 46 2d 31 36 20 4c 69 74 74 6c 65 2d  > UTF-16 Little-
af730 65 6e 64 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20  endian */.      
af740 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d  while( zIn<zTerm
af750 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 63   ){.        /* c
af760 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65   = sqlite3Utf8Re
af770 61 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28  ad(zIn, zTerm, (
af780 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29  const u8**)&zIn)
af790 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 52 45 41  ; */.        REA
af7a0 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72  D_UTF8(zIn, zTer
af7b0 6d 2c 20 63 29 3b 0a 20 20 20 20 20 20 20 20 57  m, c);.        W
af7c0 52 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20  RITE_UTF16LE(z, 
af7d0 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  c);.      }.    
af7e0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73  }else{.      ass
af7f0 65 72 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d  ert( desiredEnc=
af800 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20  =SQLITE_UTF16BE 
af810 29 3b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d  );.      /* UTF-
af820 38 20 2d 3e 20 55 54 46 2d 31 36 20 42 69 67 2d  8 -> UTF-16 Big-
af830 65 6e 64 69 61 6e 20 2a 2f 0a 20 20 20 20 20 20  endian */.      
af840 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d  while( zIn<zTerm
af850 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 63   ){.        /* c
af860 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65   = sqlite3Utf8Re
af870 61 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20 28  ad(zIn, zTerm, (
af880 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29  const u8**)&zIn)
af890 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 52 45 41  ; */.        REA
af8a0 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65 72  D_UTF8(zIn, zTer
af8b0 6d 2c 20 63 29 3b 0a 20 20 20 20 20 20 20 20 57  m, c);.        W
af8c0 52 49 54 45 5f 55 54 46 31 36 42 45 28 7a 2c 20  RITE_UTF16BE(z, 
af8d0 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  c);.      }.    
af8e0 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20  }.    pMem->n = 
af8f0 28 69 6e 74 29 28 7a 20 2d 20 7a 4f 75 74 29 3b  (int)(z - zOut);
af900 0a 20 20 20 20 2a 7a 2b 2b 20 3d 20 30 3b 0a 20  .    *z++ = 0;. 
af910 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
af920 72 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d  rt( desiredEnc==
af930 53 51 4c 49 54 45 5f 55 54 46 38 20 29 3b 0a 20  SQLITE_UTF8 );. 
af940 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63     if( pMem->enc
af950 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  ==SQLITE_UTF16LE
af960 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46   ){.      /* UTF
af970 2d 31 36 20 4c 69 74 74 6c 65 2d 65 6e 64 69 61  -16 Little-endia
af980 6e 20 2d 3e 20 55 54 46 2d 38 20 2a 2f 0a 20 20  n -> UTF-8 */.  
af990 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a      while( zIn<z
af9a0 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  Term ){.        
af9b0 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 49 6e  READ_UTF16LE(zIn
af9c0 2c 20 7a 49 6e 3c 7a 54 65 72 6d 2c 20 63 29 3b  , zIn<zTerm, c);
af9d0 20 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f   .        WRITE_
af9e0 55 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20  UTF8(z, c);.    
af9f0 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
afa00 20 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 42       /* UTF-16 B
afa10 69 67 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46  ig-endian -> UTF
afa20 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c  -8 */.      whil
afa30 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a  e( zIn<zTerm ){.
afa40 20 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46          READ_UTF
afa50 31 36 42 45 28 7a 49 6e 2c 20 7a 49 6e 3c 7a 54  16BE(zIn, zIn<zT
afa60 65 72 6d 2c 20 63 29 3b 20 0a 20 20 20 20 20 20  erm, c); .      
afa70 20 20 57 52 49 54 45 5f 55 54 46 38 28 7a 2c 20    WRITE_UTF8(z, 
afa80 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  c);.      }.    
afa90 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20  }.    pMem->n = 
afaa0 28 69 6e 74 29 28 7a 20 2d 20 7a 4f 75 74 29 3b  (int)(z - zOut);
afab0 0a 20 20 7d 0a 20 20 2a 7a 20 3d 20 30 3b 0a 20  .  }.  *z = 0;. 
afac0 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e   assert( (pMem->
afad0 6e 2b 28 64 65 73 69 72 65 64 45 6e 63 3d 3d 53  n+(desiredEnc==S
afae0 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 29  QLITE_UTF8?1:2))
afaf0 3c 3d 6c 65 6e 20 29 3b 0a 0a 20 20 73 71 6c 69  <=len );..  sqli
afb00 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
afb10 65 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d  e(pMem);.  pMem-
afb20 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f  >flags &= ~(MEM_
afb30 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79 6e 7c 4d  Static|MEM_Dyn|M
afb40 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 70 4d 65  EM_Ephem);.  pMe
afb50 6d 2d 3e 65 6e 63 20 3d 20 64 65 73 69 72 65 64  m->enc = desired
afb60 45 6e 63 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61  Enc;.  pMem->fla
afb70 67 73 20 7c 3d 20 28 4d 45 4d 5f 54 65 72 6d 7c  gs |= (MEM_Term|
afb80 4d 45 4d 5f 44 79 6e 29 3b 0a 20 20 70 4d 65 6d  MEM_Dyn);.  pMem
afb90 2d 3e 7a 20 3d 20 28 63 68 61 72 2a 29 7a 4f 75  ->z = (char*)zOu
afba0 74 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c  t;.  pMem->zMall
afbb0 6f 63 20 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a 0a 74  oc = pMem->z;..t
afbc0 72 61 6e 73 6c 61 74 65 5f 6f 75 74 3a 0a 23 69  ranslate_out:.#i
afbd0 66 20 64 65 66 69 6e 65 64 28 54 52 41 4e 53 4c  f defined(TRANSL
afbe0 41 54 45 5f 54 52 41 43 45 29 20 26 26 20 64 65  ATE_TRACE) && de
afbf0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
afc00 55 47 29 0a 20 20 7b 0a 20 20 20 20 63 68 61 72  UG).  {.    char
afc10 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20   zBuf[100];.    
afc20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72  sqlite3VdbeMemPr
afc30 65 74 74 79 50 72 69 6e 74 28 70 4d 65 6d 2c 20  ettyPrint(pMem, 
afc40 7a 42 75 66 29 3b 0a 20 20 20 20 66 70 72 69 6e  zBuf);.    fprin
afc50 74 66 28 73 74 64 65 72 72 2c 20 22 4f 55 54 50  tf(stderr, "OUTP
afc60 55 54 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29  UT: %s\n", zBuf)
afc70 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72  ;.  }.#endif.  r
afc80 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
afc90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
afca0 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 66 6f  outine checks fo
afcb0 72 20 61 20 62 79 74 65 2d 6f 72 64 65 72 20 6d  r a byte-order m
afcc0 61 72 6b 20 61 74 20 74 68 65 20 62 65 67 69 6e  ark at the begin
afcd0 6e 69 6e 67 20 6f 66 20 74 68 65 20 0a 2a 2a 20  ning of the .** 
afce0 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 73 74  UTF-16 string st
afcf0 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 2e 20 49  ored in *pMem. I
afd00 66 20 6f 6e 65 20 69 73 20 70 72 65 73 65 6e 74  f one is present
afd10 2c 20 69 74 20 69 73 20 72 65 6d 6f 76 65 64 20  , it is removed 
afd20 61 6e 64 0a 2a 2a 20 74 68 65 20 65 6e 63 6f 64  and.** the encod
afd30 69 6e 67 20 6f 66 20 74 68 65 20 4d 65 6d 20 61  ing of the Mem a
afd40 64 6a 75 73 74 65 64 2e 20 54 68 69 73 20 72 6f  djusted. This ro
afd50 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 64  utine does not d
afd60 6f 20 61 6e 79 0a 2a 2a 20 62 79 74 65 2d 73 77  o any.** byte-sw
afd70 61 70 70 69 6e 67 2c 20 69 74 20 6a 75 73 74 20  apping, it just 
afd80 73 65 74 73 20 4d 65 6d 2e 65 6e 63 20 61 70 70  sets Mem.enc app
afd90 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a  ropriately..**.*
afda0 2a 20 54 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  * The allocation
afdb0 20 28 73 74 61 74 69 63 2c 20 64 79 6e 61 6d 69   (static, dynami
afdc0 63 20 65 74 63 2e 29 20 61 6e 64 20 65 6e 63 6f  c etc.) and enco
afdd0 64 69 6e 67 20 6f 66 20 74 68 65 20 4d 65 6d 20  ding of the Mem 
afde0 6d 61 79 20 62 65 0a 2a 2a 20 63 68 61 6e 67 65  may be.** change
afdf0 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  d by this functi
afe00 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
afe10 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
afe20 33 56 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f  3VdbeMemHandleBo
afe30 6d 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20  m(Mem *pMem){.  
afe40 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
afe50 4f 4b 3b 0a 20 20 75 38 20 62 6f 6d 20 3d 20 30  OK;.  u8 bom = 0
afe60 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65  ;..  assert( pMe
afe70 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a 20 20 69 66 28  m->n>=0 );.  if(
afe80 20 70 4d 65 6d 2d 3e 6e 3e 31 20 29 7b 0a 20 20   pMem->n>1 ){.  
afe90 20 20 75 38 20 62 31 20 3d 20 2a 28 75 38 20 2a    u8 b1 = *(u8 *
afea0 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 75 38  )pMem->z;.    u8
afeb0 20 62 32 20 3d 20 2a 28 28 28 75 38 20 2a 29 70   b2 = *(((u8 *)p
afec0 4d 65 6d 2d 3e 7a 29 20 2b 20 31 29 3b 0a 20 20  Mem->z) + 1);.  
afed0 20 20 69 66 28 20 62 31 3d 3d 30 78 46 45 20 26    if( b1==0xFE &
afee0 26 20 62 32 3d 3d 30 78 46 46 20 29 7b 0a 20 20  & b2==0xFF ){.  
afef0 20 20 20 20 62 6f 6d 20 3d 20 53 51 4c 49 54 45      bom = SQLITE
aff00 5f 55 54 46 31 36 42 45 3b 0a 20 20 20 20 7d 0a  _UTF16BE;.    }.
aff10 20 20 20 20 69 66 28 20 62 31 3d 3d 30 78 46 46      if( b1==0xFF
aff20 20 26 26 20 62 32 3d 3d 30 78 46 45 20 29 7b 0a   && b2==0xFE ){.
aff30 20 20 20 20 20 20 62 6f 6d 20 3d 20 53 51 4c 49        bom = SQLI
aff40 54 45 5f 55 54 46 31 36 4c 45 3b 0a 20 20 20 20  TE_UTF16LE;.    
aff50 7d 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 20 62  }.  }.  .  if( b
aff60 6f 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  om ){.    rc = s
aff70 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b  qlite3VdbeMemMak
aff80 65 57 72 69 74 65 61 62 6c 65 28 70 4d 65 6d 29  eWriteable(pMem)
aff90 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
affa0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
affb0 20 70 4d 65 6d 2d 3e 6e 20 2d 3d 20 32 3b 0a 20   pMem->n -= 2;. 
affc0 20 20 20 20 20 6d 65 6d 6d 6f 76 65 28 70 4d 65       memmove(pMe
affd0 6d 2d 3e 7a 2c 20 26 70 4d 65 6d 2d 3e 7a 5b 32  m->z, &pMem->z[2
affe0 5d 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20 20 20  ], pMem->n);.   
afff0 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d     pMem->z[pMem-
b0000 3e 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20  >n] = '\0';.    
b0010 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e    pMem->z[pMem->
b0020 6e 2b 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20  n+1] = '\0';.   
b0030 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c     pMem->flags |
b0040 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20  = MEM_Term;.    
b0050 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 62 6f    pMem->enc = bo
b0060 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  m;.    }.  }.  r
b0070 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64  eturn rc;.}.#end
b0080 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
b0090 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a  T_UTF16 */../*.*
b00a0 2a 20 70 5a 20 69 73 20 61 20 55 54 46 2d 38 20  * pZ is a UTF-8 
b00b0 65 6e 63 6f 64 65 64 20 75 6e 69 63 6f 64 65 20  encoded unicode 
b00c0 73 74 72 69 6e 67 2e 20 49 66 20 6e 42 79 74 65  string. If nByte
b00d0 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65   is less than ze
b00e0 72 6f 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 74 68  ro,.** return th
b00f0 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 63  e number of unic
b0100 6f 64 65 20 63 68 61 72 61 63 74 65 72 73 20 69  ode characters i
b0110 6e 20 70 5a 20 75 70 20 74 6f 20 28 62 75 74 20  n pZ up to (but 
b0120 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 29 0a 2a  not including).*
b0130 2a 20 74 68 65 20 66 69 72 73 74 20 30 78 30 30  * the first 0x00
b0140 20 62 79 74 65 2e 20 49 66 20 6e 42 79 74 65 20   byte. If nByte 
b0150 69 73 20 6e 6f 74 20 6c 65 73 73 20 74 68 61 6e  is not less than
b0160 20 7a 65 72 6f 2c 20 72 65 74 75 72 6e 20 74 68   zero, return th
b0170 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 75  e.** number of u
b0180 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74 65 72  nicode character
b0190 73 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 6e  s in the first n
b01a0 42 79 74 65 20 6f 66 20 70 5a 20 28 6f 72 20 75  Byte of pZ (or u
b01b0 70 20 74 6f 20 0a 2a 2a 20 74 68 65 20 66 69 72  p to .** the fir
b01c0 73 74 20 30 78 30 30 2c 20 77 68 69 63 68 65 76  st 0x00, whichev
b01d0 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74 29 2e  er comes first).
b01e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
b01f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74  TE int sqlite3Ut
b0200 66 38 43 68 61 72 4c 65 6e 28 63 6f 6e 73 74 20  f8CharLen(const 
b0210 63 68 61 72 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e  char *zIn, int n
b0220 42 79 74 65 29 7b 0a 20 20 69 6e 74 20 72 20 3d  Byte){.  int r =
b0230 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a   0;.  const u8 *
b0240 7a 20 3d 20 28 63 6f 6e 73 74 20 75 38 2a 29 7a  z = (const u8*)z
b0250 49 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a  In;.  const u8 *
b0260 7a 54 65 72 6d 3b 0a 20 20 69 66 28 20 6e 42 79  zTerm;.  if( nBy
b0270 74 65 3e 3d 30 20 29 7b 0a 20 20 20 20 7a 54 65  te>=0 ){.    zTe
b0280 72 6d 20 3d 20 26 7a 5b 6e 42 79 74 65 5d 3b 0a  rm = &z[nByte];.
b0290 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 54 65    }else{.    zTe
b02a0 72 6d 20 3d 20 28 63 6f 6e 73 74 20 75 38 2a 29  rm = (const u8*)
b02b0 28 2d 31 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  (-1);.  }.  asse
b02c0 72 74 28 20 7a 3c 3d 7a 54 65 72 6d 20 29 3b 0a  rt( z<=zTerm );.
b02d0 20 20 77 68 69 6c 65 28 20 2a 7a 21 3d 30 20 26    while( *z!=0 &
b02e0 26 20 7a 3c 7a 54 65 72 6d 20 29 7b 0a 20 20 20  & z<zTerm ){.   
b02f0 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46   SQLITE_SKIP_UTF
b0300 38 28 7a 29 3b 0a 20 20 20 20 72 2b 2b 3b 0a 20  8(z);.    r++;. 
b0310 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d   }.  return r;.}
b0320 0a 0a 2f 2a 20 54 68 69 73 20 74 65 73 74 20 66  ../* This test f
b0330 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20 63  unction is not c
b0340 75 72 72 65 6e 74 6c 79 20 75 73 65 64 20 62 79  urrently used by
b0350 20 74 68 65 20 61 75 74 6f 6d 61 74 65 64 20 74   the automated t
b0360 65 73 74 2d 73 75 69 74 65 2e 20 0a 2a 2a 20 48  est-suite. .** H
b0370 65 6e 63 65 20 69 74 20 69 73 20 6f 6e 6c 79 20  ence it is only 
b0380 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 64 65 62  available in deb
b0390 75 67 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69  ug builds..*/.#i
b03a0 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
b03b0 5f 54 45 53 54 29 20 26 26 20 64 65 66 69 6e 65  _TEST) && define
b03c0 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a  d(SQLITE_DEBUG).
b03d0 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65 20  /*.** Translate 
b03e0 55 54 46 2d 38 20 74 6f 20 55 54 46 2d 38 2e 0a  UTF-8 to UTF-8..
b03f0 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 61 73 20 74  **.** This has t
b0400 68 65 20 65 66 66 65 63 74 20 6f 66 20 6d 61 6b  he effect of mak
b0410 69 6e 67 20 73 75 72 65 20 74 68 61 74 20 74 68  ing sure that th
b0420 65 20 73 74 72 69 6e 67 20 69 73 20 77 65 6c 6c  e string is well
b0430 2d 66 6f 72 6d 65 64 0a 2a 2a 20 55 54 46 2d 38  -formed.** UTF-8
b0440 2e 20 20 4d 69 73 63 6f 64 65 64 20 63 68 61 72  .  Miscoded char
b0450 61 63 74 65 72 73 20 61 72 65 20 72 65 6d 6f 76  acters are remov
b0460 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 72  ed..**.** The tr
b0470 61 6e 73 6c 61 74 69 6f 6e 20 69 73 20 64 6f 6e  anslation is don
b0480 65 20 69 6e 2d 70 6c 61 63 65 20 28 73 69 6e 63  e in-place (sinc
b0490 65 20 69 74 20 69 73 20 69 6d 70 6f 73 73 69 62  e it is impossib
b04a0 6c 65 20 66 6f 72 20 74 68 65 0a 2a 2a 20 63 6f  le for the.** co
b04b0 72 72 65 63 74 20 55 54 46 2d 38 20 65 6e 63 6f  rrect UTF-8 enco
b04c0 64 69 6e 67 20 74 6f 20 62 65 20 6c 6f 6e 67 65  ding to be longe
b04d0 72 20 74 68 61 6e 20 61 20 6d 61 6c 66 6f 72 6d  r than a malform
b04e0 65 64 20 65 6e 63 6f 64 69 6e 67 29 2e 0a 2a 2f  ed encoding)..*/
b04f0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b0500 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66 38 54  int sqlite3Utf8T
b0510 6f 38 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  o8(unsigned char
b0520 20 2a 7a 49 6e 29 7b 0a 20 20 75 6e 73 69 67 6e   *zIn){.  unsign
b0530 65 64 20 63 68 61 72 20 2a 7a 4f 75 74 20 3d 20  ed char *zOut = 
b0540 7a 49 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  zIn;.  unsigned 
b0550 63 68 61 72 20 2a 7a 53 74 61 72 74 20 3d 20 7a  char *zStart = z
b0560 49 6e 3b 0a 20 20 75 33 32 20 63 3b 0a 0a 20 20  In;.  u32 c;..  
b0570 77 68 69 6c 65 28 20 7a 49 6e 5b 30 5d 20 29 7b  while( zIn[0] ){
b0580 0a 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33  .    c = sqlite3
b0590 55 74 66 38 52 65 61 64 28 7a 49 6e 2c 20 28 63  Utf8Read(zIn, (c
b05a0 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b  onst u8**)&zIn);
b05b0 0a 20 20 20 20 69 66 28 20 63 21 3d 30 78 66 66  .    if( c!=0xff
b05c0 66 64 20 29 7b 0a 20 20 20 20 20 20 57 52 49 54  fd ){.      WRIT
b05d0 45 5f 55 54 46 38 28 7a 4f 75 74 2c 20 63 29 3b  E_UTF8(zOut, c);
b05e0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 7a 4f  .    }.  }.  *zO
b05f0 75 74 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e  ut = 0;.  return
b0600 20 28 69 6e 74 29 28 7a 4f 75 74 20 2d 20 7a 53   (int)(zOut - zS
b0610 74 61 72 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  tart);.}.#endif.
b0620 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
b0630 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a  OMIT_UTF16./*.**
b0640 20 43 6f 6e 76 65 72 74 20 61 20 55 54 46 2d 31   Convert a UTF-1
b0650 36 20 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20  6 string in the 
b0660 6e 61 74 69 76 65 20 65 6e 63 6f 64 69 6e 67 20  native encoding 
b0670 69 6e 74 6f 20 61 20 55 54 46 2d 38 20 73 74 72  into a UTF-8 str
b0680 69 6e 67 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74  ing..** Memory t
b0690 6f 20 68 6f 6c 64 20 74 68 65 20 55 54 46 2d 38  o hold the UTF-8
b06a0 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69   string is obtai
b06b0 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
b06c0 5f 6d 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74  _malloc and must
b06d0 0a 2a 2a 20 62 65 20 66 72 65 65 64 20 62 79 20  .** be freed by 
b06e0 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  the calling func
b06f0 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 4e 55 4c 4c  tion..**.** NULL
b0700 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
b0710 74 68 65 72 65 20 69 73 20 61 6e 20 61 6c 6c 6f  there is an allo
b0720 63 61 74 69 6f 6e 20 65 72 72 6f 72 2e 0a 2a 2f  cation error..*/
b0730 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b0740 63 68 61 72 20 2a 73 71 6c 69 74 65 33 55 74 66  char *sqlite3Utf
b0750 31 36 74 6f 38 28 73 71 6c 69 74 65 33 20 2a 64  16to8(sqlite3 *d
b0760 62 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  b, const void *z
b0770 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20  , int nByte){.  
b0780 4d 65 6d 20 6d 3b 0a 20 20 6d 65 6d 73 65 74 28  Mem m;.  memset(
b0790 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 29  &m, 0, sizeof(m)
b07a0 29 3b 0a 20 20 6d 2e 64 62 20 3d 20 64 62 3b 0a  );.  m.db = db;.
b07b0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
b07c0 53 65 74 53 74 72 28 26 6d 2c 20 7a 2c 20 6e 42  SetStr(&m, z, nB
b07d0 79 74 65 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  yte, SQLITE_UTF1
b07e0 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f  6NATIVE, SQLITE_
b07f0 53 54 41 54 49 43 29 3b 0a 20 20 73 71 6c 69 74  STATIC);.  sqlit
b0800 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
b0810 64 69 6e 67 28 26 6d 2c 20 53 51 4c 49 54 45 5f  ding(&m, SQLITE_
b0820 55 54 46 38 29 3b 0a 20 20 69 66 28 20 64 62 2d  UTF8);.  if( db-
b0830 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
b0840 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
b0850 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a  MemRelease(&m);.
b0860 20 20 20 20 6d 2e 7a 20 3d 20 30 3b 0a 20 20 7d      m.z = 0;.  }
b0870 0a 20 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c  .  assert( (m.fl
b0880 61 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 21  ags & MEM_Term)!
b0890 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  =0 || db->malloc
b08a0 46 61 69 6c 65 64 20 29 3b 0a 20 20 61 73 73 65  Failed );.  asse
b08b0 72 74 28 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d  rt( (m.flags & M
b08c0 45 4d 5f 53 74 72 29 21 3d 30 20 7c 7c 20 64 62  EM_Str)!=0 || db
b08d0 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
b08e0 3b 0a 20 20 72 65 74 75 72 6e 20 28 6d 2e 66 6c  ;.  return (m.fl
b08f0 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 21 3d  ags & MEM_Dyn)!=
b0900 30 20 3f 20 6d 2e 7a 20 3a 20 73 71 6c 69 74 65  0 ? m.z : sqlite
b0910 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 6d 2e  3DbStrDup(db, m.
b0920 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  z);.}../*.** Con
b0930 76 65 72 74 20 61 20 55 54 46 2d 38 20 73 74 72  vert a UTF-8 str
b0940 69 6e 67 20 74 6f 20 74 68 65 20 55 54 46 2d 31  ing to the UTF-1
b0950 36 20 65 6e 63 6f 64 69 6e 67 20 73 70 65 63 69  6 encoding speci
b0960 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65  fied by paramete
b0970 72 0a 2a 2a 20 65 6e 63 2e 20 41 20 70 6f 69 6e  r.** enc. A poin
b0980 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 73  ter to the new s
b0990 74 72 69 6e 67 20 69 73 20 72 65 74 75 72 6e 65  tring is returne
b09a0 64 2c 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65  d, and the value
b09b0 20 6f 66 20 2a 70 6e 4f 75 74 0a 2a 2a 20 69 73   of *pnOut.** is
b09c0 20 73 65 74 20 74 6f 20 74 68 65 20 6c 65 6e 67   set to the leng
b09d0 74 68 20 6f 66 20 74 68 65 20 72 65 74 75 72 6e  th of the return
b09e0 65 64 20 73 74 72 69 6e 67 20 69 6e 20 62 79 74  ed string in byt
b09f0 65 73 2e 20 54 68 65 20 63 61 6c 6c 20 73 68 6f  es. The call sho
b0a00 75 6c 64 0a 2a 2a 20 61 72 72 61 6e 67 65 20 74  uld.** arrange t
b0a10 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62  o call sqlite3Db
b0a20 46 72 65 65 28 29 20 6f 6e 20 74 68 65 20 72 65  Free() on the re
b0a30 74 75 72 6e 65 64 20 70 6f 69 6e 74 65 72 20 77  turned pointer w
b0a40 68 65 6e 20 69 74 20 69 73 0a 2a 2a 20 6e 6f 20  hen it is.** no 
b0a50 6c 6f 6e 67 65 72 20 72 65 71 75 69 72 65 64 2e  longer required.
b0a60 0a 2a 2a 20 0a 2a 2a 20 49 66 20 61 20 6d 61 6c  .** .** If a mal
b0a70 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f 63 63 75  loc failure occu
b0a80 72 73 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  rs, NULL is retu
b0a90 72 6e 65 64 20 61 6e 64 20 74 68 65 20 64 62 2e  rned and the db.
b0aa0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 0a 2a 2a 20  mallocFailed.** 
b0ab0 66 6c 61 67 20 73 65 74 2e 0a 2a 2f 0a 23 69 66  flag set..*/.#if
b0ac0 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
b0ad0 45 5f 53 54 41 54 32 0a 53 51 4c 49 54 45 5f 50  E_STAT2.SQLITE_P
b0ae0 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c  RIVATE char *sql
b0af0 69 74 65 33 55 74 66 38 74 6f 31 36 28 73 71 6c  ite3Utf8to16(sql
b0b00 69 74 65 33 20 2a 64 62 2c 20 75 38 20 65 6e 63  ite3 *db, u8 enc
b0b10 2c 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e  , char *z, int n
b0b20 2c 20 69 6e 74 20 2a 70 6e 4f 75 74 29 7b 0a 20  , int *pnOut){. 
b0b30 20 4d 65 6d 20 6d 3b 0a 20 20 6d 65 6d 73 65 74   Mem m;.  memset
b0b40 28 26 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d  (&m, 0, sizeof(m
b0b50 29 29 3b 0a 20 20 6d 2e 64 62 20 3d 20 64 62 3b  ));.  m.db = db;
b0b60 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
b0b70 6d 53 65 74 53 74 72 28 26 6d 2c 20 7a 2c 20 6e  mSetStr(&m, z, n
b0b80 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  , SQLITE_UTF8, S
b0b90 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
b0ba0 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
b0bb0 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 26 6d 2c  MemTranslate(&m,
b0bc0 20 65 6e 63 29 20 29 7b 0a 20 20 20 20 61 73 73   enc) ){.    ass
b0bd0 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ert( db->mallocF
b0be0 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 74  ailed );.    ret
b0bf0 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73  urn 0;.  }.  ass
b0c00 65 72 74 28 20 6d 2e 7a 3d 3d 6d 2e 7a 4d 61 6c  ert( m.z==m.zMal
b0c10 6c 6f 63 20 29 3b 0a 20 20 2a 70 6e 4f 75 74 20  loc );.  *pnOut 
b0c20 3d 20 6d 2e 6e 3b 0a 20 20 72 65 74 75 72 6e 20  = m.n;.  return 
b0c30 6d 2e 7a 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  m.z;.}.#endif../
b0c40 2a 0a 2a 2a 20 7a 49 6e 20 69 73 20 61 20 55 54  *.** zIn is a UT
b0c50 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 75 6e 69  F-16 encoded uni
b0c60 63 6f 64 65 20 73 74 72 69 6e 67 20 61 74 20 6c  code string at l
b0c70 65 61 73 74 20 6e 43 68 61 72 20 63 68 61 72 61  east nChar chara
b0c80 63 74 65 72 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 52  cters long..** R
b0c90 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
b0ca0 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65   of bytes in the
b0cb0 20 66 69 72 73 74 20 6e 43 68 61 72 20 75 6e 69   first nChar uni
b0cc0 63 6f 64 65 20 63 68 61 72 61 63 74 65 72 73 0a  code characters.
b0cd0 2a 2a 20 69 6e 20 70 5a 2e 20 20 6e 43 68 61 72  ** in pZ.  nChar
b0ce0 20 6d 75 73 74 20 62 65 20 6e 6f 6e 2d 6e 65 67   must be non-neg
b0cf0 61 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ative..*/.SQLITE
b0d00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
b0d10 69 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e  ite3Utf16ByteLen
b0d20 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 49 6e  (const void *zIn
b0d30 2c 20 69 6e 74 20 6e 43 68 61 72 29 7b 0a 20 20  , int nChar){.  
b0d40 69 6e 74 20 63 3b 0a 20 20 75 6e 73 69 67 6e 65  int c;.  unsigne
b0d50 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 20  d char const *z 
b0d60 3d 20 7a 49 6e 3b 0a 20 20 69 6e 74 20 6e 20 3d  = zIn;.  int n =
b0d70 20 30 3b 0a 20 20 0a 20 20 69 66 28 20 53 51 4c   0;.  .  if( SQL
b0d80 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3d  ITE_UTF16NATIVE=
b0d90 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20  =SQLITE_UTF16BE 
b0da0 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 3c  ){.    while( n<
b0db0 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20 20 52  nChar ){.      R
b0dc0 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 31  EAD_UTF16BE(z, 1
b0dd0 2c 20 63 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b  , c);.      n++;
b0de0 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
b0df0 20 20 20 20 77 68 69 6c 65 28 20 6e 3c 6e 43 68      while( n<nCh
b0e00 61 72 20 29 7b 0a 20 20 20 20 20 20 52 45 41 44  ar ){.      READ
b0e10 5f 55 54 46 31 36 4c 45 28 7a 2c 20 31 2c 20 63  _UTF16LE(z, 1, c
b0e20 29 3b 0a 20 20 20 20 20 20 6e 2b 2b 3b 0a 20 20  );.      n++;.  
b0e30 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
b0e40 20 28 69 6e 74 29 28 7a 2d 28 75 6e 73 69 67 6e   (int)(z-(unsign
b0e50 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 29  ed char const *)
b0e60 7a 49 6e 29 3b 0a 7d 0a 0a 23 69 66 20 64 65 66  zIn);.}..#if def
b0e70 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
b0e80 29 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  )./*.** This rou
b0e90 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66  tine is called f
b0ea0 72 6f 6d 20 74 68 65 20 54 43 4c 20 74 65 73 74  rom the TCL test
b0eb0 20 66 75 6e 63 74 69 6f 6e 20 22 74 72 61 6e 73   function "trans
b0ec0 6c 61 74 65 5f 73 65 6c 66 74 65 73 74 22 2e 0a  late_selftest"..
b0ed0 2a 2a 20 49 74 20 63 68 65 63 6b 73 20 74 68 61  ** It checks tha
b0ee0 74 20 74 68 65 20 70 72 69 6d 69 74 69 76 65 73  t the primitives
b0ef0 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e 67   for serializing
b0f00 20 61 6e 64 20 64 65 73 65 72 69 61 6c 69 7a 69   and deserializi
b0f10 6e 67 0a 2a 2a 20 63 68 61 72 61 63 74 65 72 73  ng.** characters
b0f20 20 69 6e 20 65 61 63 68 20 65 6e 63 6f 64 69 6e   in each encodin
b0f30 67 20 61 72 65 20 69 6e 76 65 72 73 65 73 20 6f  g are inverses o
b0f40 66 20 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f  f each other..*/
b0f50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b0f60 76 6f 69 64 20 73 71 6c 69 74 65 33 55 74 66 53  void sqlite3UtfS
b0f70 65 6c 66 54 65 73 74 28 76 6f 69 64 29 7b 0a 20  elfTest(void){. 
b0f80 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 2c   unsigned int i,
b0f90 20 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   t;.  unsigned c
b0fa0 68 61 72 20 7a 42 75 66 5b 32 30 5d 3b 0a 20 20  har zBuf[20];.  
b0fb0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
b0fc0 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73  ;.  int n;.  uns
b0fd0 69 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20 20  igned int c;..  
b0fe0 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31  for(i=0; i<0x001
b0ff0 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20  10000; i++){.   
b1000 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57   z = zBuf;.    W
b1010 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 69 29 3b  RITE_UTF8(z, i);
b1020 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a  .    n = (int)(z
b1030 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73 73 65  -zBuf);.    asse
b1040 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20  rt( n>0 && n<=4 
b1050 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b  );.    z[0] = 0;
b1060 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20  .    z = zBuf;. 
b1070 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74     c = sqlite3Ut
b1080 66 38 52 65 61 64 28 7a 2c 20 28 63 6f 6e 73 74  f8Read(z, (const
b1090 20 75 38 2a 2a 29 26 7a 29 3b 0a 20 20 20 20 74   u8**)&z);.    t
b10a0 20 3d 20 69 3b 0a 20 20 20 20 69 66 28 20 69 3e   = i;.    if( i>
b10b0 3d 30 78 44 38 30 30 20 26 26 20 69 3c 3d 30 78  =0xD800 && i<=0x
b10c0 44 46 46 46 20 29 20 74 20 3d 20 30 78 46 46 46  DFFF ) t = 0xFFF
b10d0 44 3b 0a 20 20 20 20 69 66 28 20 28 69 26 30 78  D;.    if( (i&0x
b10e0 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46 46 46  FFFFFFFE)==0xFFF
b10f0 45 20 29 20 74 20 3d 20 30 78 46 46 46 44 3b 0a  E ) t = 0xFFFD;.
b1100 20 20 20 20 61 73 73 65 72 74 28 20 63 3d 3d 74      assert( c==t
b1110 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
b1120 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b 0a 20  (z-zBuf)==n );. 
b1130 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
b1140 30 78 30 30 31 31 30 30 30 30 3b 20 69 2b 2b 29  0x00110000; i++)
b1150 7b 0a 20 20 20 20 69 66 28 20 69 3e 3d 30 78 44  {.    if( i>=0xD
b1160 38 30 30 20 26 26 20 69 3c 30 78 45 30 30 30 20  800 && i<0xE000 
b1170 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
b1180 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57 52  z = zBuf;.    WR
b1190 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c 20 69  ITE_UTF16LE(z, i
b11a0 29 3b 0a 20 20 20 20 6e 20 3d 20 28 69 6e 74 29  );.    n = (int)
b11b0 28 7a 2d 7a 42 75 66 29 3b 0a 20 20 20 20 61 73  (z-zBuf);.    as
b11c0 73 65 72 74 28 20 6e 3e 30 20 26 26 20 6e 3c 3d  sert( n>0 && n<=
b11d0 34 20 29 3b 0a 20 20 20 20 7a 5b 30 5d 20 3d 20  4 );.    z[0] = 
b11e0 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b  0;.    z = zBuf;
b11f0 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31 36 4c  .    READ_UTF16L
b1200 45 28 7a 2c 20 31 2c 20 63 29 3b 0a 20 20 20 20  E(z, 1, c);.    
b1210 61 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a  assert( c==i );.
b1220 20 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d 7a      assert( (z-z
b1230 42 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20  Buf)==n );.  }. 
b1240 20 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30   for(i=0; i<0x00
b1250 31 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20  110000; i++){.  
b1260 20 20 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20    if( i>=0xD800 
b1270 26 26 20 69 3c 30 78 45 30 30 30 20 29 20 63 6f  && i<0xE000 ) co
b1280 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20  ntinue;.    z = 
b1290 7a 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f  zBuf;.    WRITE_
b12a0 55 54 46 31 36 42 45 28 7a 2c 20 69 29 3b 0a 20  UTF16BE(z, i);. 
b12b0 20 20 20 6e 20 3d 20 28 69 6e 74 29 28 7a 2d 7a     n = (int)(z-z
b12c0 42 75 66 29 3b 0a 20 20 20 20 61 73 73 65 72 74  Buf);.    assert
b12d0 28 20 6e 3e 30 20 26 26 20 6e 3c 3d 34 20 29 3b  ( n>0 && n<=4 );
b12e0 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20  .    z[0] = 0;. 
b12f0 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20     z = zBuf;.   
b1300 20 52 45 41 44 5f 55 54 46 31 36 42 45 28 7a 2c   READ_UTF16BE(z,
b1310 20 31 2c 20 63 29 3b 0a 20 20 20 20 61 73 73 65   1, c);.    asse
b1320 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20 20 20 20  rt( c==i );.    
b1330 61 73 73 65 72 74 28 20 28 7a 2d 7a 42 75 66 29  assert( (z-zBuf)
b1340 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  ==n );.  }.}.#en
b1350 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45  dif /* SQLITE_TE
b1360 53 54 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20  ST */.#endif /* 
b1370 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
b1380 36 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  6 */../*********
b1390 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 75 74 66  ***** End of utf
b13a0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
b13b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b13c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b13d0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
b13e0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
b13f0 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   util.c ********
b1400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1420 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
b1430 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
b1440 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
b1450 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
b1460 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
b1470 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
b1480 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
b1490 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
b14a0 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
b14b0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
b14c0 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
b14d0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
b14e0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
b14f0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
b1500 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
b1510 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
b1520 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
b1530 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
b1540 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
b1550 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
b1560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b1590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
b15a0 55 74 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e  Utility function
b15b0 73 20 75 73 65 64 20 74 68 72 6f 75 67 68 6f 75  s used throughou
b15c0 74 20 73 71 6c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20  t sqlite..**.** 
b15d0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
b15e0 6e 73 20 66 75 6e 63 74 69 6f 6e 73 20 66 6f 72  ns functions for
b15f0 20 61 6c 6c 6f 63 61 74 69 6e 67 20 6d 65 6d 6f   allocating memo
b1600 72 79 2c 20 63 6f 6d 70 61 72 69 6e 67 0a 2a 2a  ry, comparing.**
b1610 20 73 74 72 69 6e 67 73 2c 20 61 6e 64 20 73 74   strings, and st
b1620 75 66 66 20 6c 69 6b 65 20 74 68 61 74 2e 0a 2a  uff like that..*
b1630 2a 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  *.*/.#ifdef SQLI
b1640 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 0a 23 20  TE_HAVE_ISNAN.# 
b1650 69 6e 63 6c 75 64 65 20 3c 6d 61 74 68 2e 68 3e  include <math.h>
b1660 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
b1670 6f 75 74 69 6e 65 20 6e 65 65 64 65 64 20 74 6f  outine needed to
b1680 20 73 75 70 70 6f 72 74 20 74 68 65 20 74 65 73   support the tes
b1690 74 63 61 73 65 28 29 20 6d 61 63 72 6f 2e 0a 2a  tcase() macro..*
b16a0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
b16b0 43 4f 56 45 52 41 47 45 5f 54 45 53 54 0a 53 51  COVERAGE_TEST.SQ
b16c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
b16d0 64 20 73 71 6c 69 74 65 33 43 6f 76 65 72 61 67  d sqlite3Coverag
b16e0 65 28 69 6e 74 20 78 29 7b 0a 20 20 73 74 61 74  e(int x){.  stat
b16f0 69 63 20 69 6e 74 20 64 75 6d 6d 79 20 3d 20 30  ic int dummy = 0
b1700 3b 0a 20 20 64 75 6d 6d 79 20 2b 3d 20 78 3b 0a  ;.  dummy += x;.
b1710 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
b1720 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
b1730 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  he floating poin
b1740 74 20 76 61 6c 75 65 20 69 73 20 4e 6f 74 20 61  t value is Not a
b1750 20 4e 75 6d 62 65 72 20 28 4e 61 4e 29 2e 0a 2a   Number (NaN)..*
b1760 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 6d 61 74  *.** Use the mat
b1770 68 20 6c 69 62 72 61 72 79 20 69 73 6e 61 6e 28  h library isnan(
b1780 29 20 66 75 6e 63 74 69 6f 6e 20 69 66 20 63 6f  ) function if co
b1790 6d 70 69 6c 65 64 20 77 69 74 68 20 53 51 4c 49  mpiled with SQLI
b17a0 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 2e 0a 2a  TE_HAVE_ISNAN..*
b17b0 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 77 65 20  * Otherwise, we 
b17c0 68 61 76 65 20 6f 75 72 20 6f 77 6e 20 69 6d 70  have our own imp
b17d0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 74 68 61 74  lementation that
b17e0 20 77 6f 72 6b 73 20 6f 6e 20 6d 6f 73 74 20 73   works on most s
b17f0 79 73 74 65 6d 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ystems..*/.SQLIT
b1800 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
b1810 6c 69 74 65 33 49 73 4e 61 4e 28 64 6f 75 62 6c  lite3IsNaN(doubl
b1820 65 20 78 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20  e x){.  int rc; 
b1830 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 72    /* The value r
b1840 65 74 75 72 6e 20 2a 2f 0a 23 69 66 20 21 64 65  eturn */.#if !de
b1850 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 56  fined(SQLITE_HAV
b1860 45 5f 49 53 4e 41 4e 29 0a 20 20 2f 2a 0a 20 20  E_ISNAN).  /*.  
b1870 2a 2a 20 53 79 73 74 65 6d 73 20 74 68 61 74 20  ** Systems that 
b1880 73 75 70 70 6f 72 74 20 74 68 65 20 69 73 6e 61  support the isna
b1890 6e 28 29 20 6c 69 62 72 61 72 79 20 66 75 6e 63  n() library func
b18a0 74 69 6f 6e 20 73 68 6f 75 6c 64 20 70 72 6f 62  tion should prob
b18b0 61 62 6c 79 0a 20 20 2a 2a 20 6d 61 6b 65 20 75  ably.  ** make u
b18c0 73 65 20 6f 66 20 69 74 20 62 79 20 63 6f 6d 70  se of it by comp
b18d0 69 6c 69 6e 67 20 77 69 74 68 20 2d 44 53 51 4c  iling with -DSQL
b18e0 49 54 45 5f 48 41 56 45 5f 49 53 4e 41 4e 2e 20  ITE_HAVE_ISNAN. 
b18f0 20 42 75 74 20 77 65 20 68 61 76 65 0a 20 20 2a   But we have.  *
b1900 2a 20 66 6f 75 6e 64 20 74 68 61 74 20 6d 61 6e  * found that man
b1910 79 20 73 79 73 74 65 6d 73 20 64 6f 20 6e 6f 74  y systems do not
b1920 20 68 61 76 65 20 61 20 77 6f 72 6b 69 6e 67 20   have a working 
b1930 69 73 6e 61 6e 28 29 20 66 75 6e 63 74 69 6f 6e  isnan() function
b1940 20 73 6f 0a 20 20 2a 2a 20 74 68 69 73 20 69 6d   so.  ** this im
b1950 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20  plementation is 
b1960 70 72 6f 76 69 64 65 64 20 61 73 20 61 6e 20 61  provided as an a
b1970 6c 74 65 72 6e 61 74 69 76 65 2e 0a 20 20 2a 2a  lternative..  **
b1980 0a 20 20 2a 2a 20 54 68 69 73 20 4e 61 4e 20 74  .  ** This NaN t
b1990 65 73 74 20 73 6f 6d 65 74 69 6d 65 73 20 66 61  est sometimes fa
b19a0 69 6c 73 20 69 66 20 63 6f 6d 70 69 6c 65 64 20  ils if compiled 
b19b0 6f 6e 20 47 43 43 20 77 69 74 68 20 2d 66 66 61  on GCC with -ffa
b19c0 73 74 2d 6d 61 74 68 2e 0a 20 20 2a 2a 20 4f 6e  st-math..  ** On
b19d0 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c   the other hand,
b19e0 20 74 68 65 20 75 73 65 20 6f 66 20 2d 66 66 61   the use of -ffa
b19f0 73 74 2d 6d 61 74 68 20 63 6f 6d 65 73 20 77 69  st-math comes wi
b1a00 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  th the following
b1a10 0a 20 20 2a 2a 20 77 61 72 6e 69 6e 67 3a 0a 20  .  ** warning:. 
b1a20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 54 68   **.  **      Th
b1a30 69 73 20 6f 70 74 69 6f 6e 20 5b 2d 66 66 61 73  is option [-ffas
b1a40 74 2d 6d 61 74 68 5d 20 73 68 6f 75 6c 64 20 6e  t-math] should n
b1a50 65 76 65 72 20 62 65 20 74 75 72 6e 65 64 20 6f  ever be turned o
b1a60 6e 20 62 79 20 61 6e 79 0a 20 20 2a 2a 20 20 20  n by any.  **   
b1a70 20 20 20 2d 4f 20 6f 70 74 69 6f 6e 20 73 69 6e     -O option sin
b1a80 63 65 20 69 74 20 63 61 6e 20 72 65 73 75 6c 74  ce it can result
b1a90 20 69 6e 20 69 6e 63 6f 72 72 65 63 74 20 6f 75   in incorrect ou
b1aa0 74 70 75 74 20 66 6f 72 20 70 72 6f 67 72 61 6d  tput for program
b1ab0 73 0a 20 20 2a 2a 20 20 20 20 20 20 77 68 69 63  s.  **      whic
b1ac0 68 20 64 65 70 65 6e 64 20 6f 6e 20 61 6e 20 65  h depend on an e
b1ad0 78 61 63 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74  xact implementat
b1ae0 69 6f 6e 20 6f 66 20 49 45 45 45 20 6f 72 20 49  ion of IEEE or I
b1af0 53 4f 20 0a 20 20 2a 2a 20 20 20 20 20 20 72 75  SO .  **      ru
b1b00 6c 65 73 2f 73 70 65 63 69 66 69 63 61 74 69 6f  les/specificatio
b1b10 6e 73 20 66 6f 72 20 6d 61 74 68 20 66 75 6e 63  ns for math func
b1b20 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  tions..  **.  **
b1b30 20 55 6e 64 65 72 20 4d 53 56 43 2c 20 74 68 69   Under MSVC, thi
b1b40 73 20 4e 61 4e 20 74 65 73 74 20 6d 61 79 20 66  s NaN test may f
b1b50 61 69 6c 20 69 66 20 63 6f 6d 70 69 6c 65 64 20  ail if compiled 
b1b60 77 69 74 68 20 61 20 66 6c 6f 61 74 69 6e 67 2d  with a floating-
b1b70 0a 20 20 2a 2a 20 70 6f 69 6e 74 20 70 72 65 63  .  ** point prec
b1b80 69 73 69 6f 6e 20 6d 6f 64 65 20 6f 74 68 65 72  ision mode other
b1b90 20 74 68 61 6e 20 2f 66 70 3a 70 72 65 63 69 73   than /fp:precis
b1ba0 65 2e 20 20 46 72 6f 6d 20 74 68 65 20 4d 53 44  e.  From the MSD
b1bb0 4e 20 0a 20 20 2a 2a 20 64 6f 63 75 6d 65 6e 74  N .  ** document
b1bc0 61 74 69 6f 6e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  ation:.  **.  **
b1bd0 20 20 20 20 20 20 54 68 65 20 63 6f 6d 70 69 6c        The compil
b1be0 65 72 20 5b 77 69 74 68 20 2f 66 70 3a 70 72 65  er [with /fp:pre
b1bf0 63 69 73 65 5d 20 77 69 6c 6c 20 70 72 6f 70 65  cise] will prope
b1c00 72 6c 79 20 68 61 6e 64 6c 65 20 63 6f 6d 70 61  rly handle compa
b1c10 72 69 73 6f 6e 73 20 0a 20 20 2a 2a 20 20 20 20  risons .  **    
b1c20 20 20 69 6e 76 6f 6c 76 69 6e 67 20 4e 61 4e 2e    involving NaN.
b1c30 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 78 20   For example, x 
b1c40 21 3d 20 78 20 65 76 61 6c 75 61 74 65 73 20 74  != x evaluates t
b1c50 6f 20 74 72 75 65 20 69 66 20 78 20 69 73 20 4e  o true if x is N
b1c60 61 4e 20 0a 20 20 2a 2a 20 20 20 20 20 20 2e 2e  aN .  **      ..
b1c70 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 5f 5f  ..  */.#ifdef __
b1c80 46 41 53 54 5f 4d 41 54 48 5f 5f 0a 23 20 65 72  FAST_MATH__.# er
b1c90 72 6f 72 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  ror SQLite will 
b1ca0 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63 74  not work correct
b1cb0 6c 79 20 77 69 74 68 20 74 68 65 20 2d 66 66 61  ly with the -ffa
b1cc0 73 74 2d 6d 61 74 68 20 6f 70 74 69 6f 6e 20 6f  st-math option o
b1cd0 66 20 47 43 43 2e 0a 23 65 6e 64 69 66 0a 20 20  f GCC..#endif.  
b1ce0 76 6f 6c 61 74 69 6c 65 20 64 6f 75 62 6c 65 20  volatile double 
b1cf0 79 20 3d 20 78 3b 0a 20 20 76 6f 6c 61 74 69 6c  y = x;.  volatil
b1d00 65 20 64 6f 75 62 6c 65 20 7a 20 3d 20 79 3b 0a  e double z = y;.
b1d10 20 20 72 63 20 3d 20 28 79 21 3d 7a 29 3b 0a 23    rc = (y!=z);.#
b1d20 65 6c 73 65 20 20 2f 2a 20 69 66 20 64 65 66 69  else  /* if defi
b1d30 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 56 45 5f  ned(SQLITE_HAVE_
b1d40 49 53 4e 41 4e 29 20 2a 2f 0a 20 20 72 63 20 3d  ISNAN) */.  rc =
b1d50 20 69 73 6e 61 6e 28 78 29 3b 0a 23 65 6e 64 69   isnan(x);.#endi
b1d60 66 20 2f 2a 20 53 51 4c 49 54 45 5f 48 41 56 45  f /* SQLITE_HAVE
b1d70 5f 49 53 4e 41 4e 20 2a 2f 0a 20 20 74 65 73 74  _ISNAN */.  test
b1d80 63 61 73 65 28 20 72 63 20 29 3b 0a 20 20 72 65  case( rc );.  re
b1d90 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
b1da0 2a 20 43 6f 6d 70 75 74 65 20 61 20 73 74 72 69  * Compute a stri
b1db0 6e 67 20 6c 65 6e 67 74 68 20 74 68 61 74 20 69  ng length that i
b1dc0 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 77 68 61  s limited to wha
b1dd0 74 20 63 61 6e 20 62 65 20 73 74 6f 72 65 64 20  t can be stored 
b1de0 69 6e 0a 2a 2a 20 6c 6f 77 65 72 20 33 30 20 62  in.** lower 30 b
b1df0 69 74 73 20 6f 66 20 61 20 33 32 2d 62 69 74 20  its of a 32-bit 
b1e00 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 0a  signed integer..
b1e10 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20  **.** The value 
b1e20 72 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 6e 65  returned will ne
b1e30 76 65 72 20 62 65 20 6e 65 67 61 74 69 76 65 2e  ver be negative.
b1e40 20 20 4e 6f 72 20 77 69 6c 6c 20 69 74 20 65 76    Nor will it ev
b1e50 65 72 20 62 65 20 67 72 65 61 74 65 72 0a 2a 2a  er be greater.**
b1e60 20 74 68 61 6e 20 74 68 65 20 61 63 74 75 61 6c   than the actual
b1e70 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73   length of the s
b1e80 74 72 69 6e 67 2e 20 20 46 6f 72 20 76 65 72 79  tring.  For very
b1e90 20 6c 6f 6e 67 20 73 74 72 69 6e 67 73 20 28 67   long strings (g
b1ea0 72 65 61 74 65 72 0a 2a 2a 20 74 68 61 6e 20 31  reater.** than 1
b1eb0 47 69 42 29 20 74 68 65 20 76 61 6c 75 65 20 72  GiB) the value r
b1ec0 65 74 75 72 6e 65 64 20 6d 69 67 68 74 20 62 65  eturned might be
b1ed0 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 74   less than the t
b1ee0 72 75 65 20 73 74 72 69 6e 67 20 6c 65 6e 67 74  rue string lengt
b1ef0 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  h..*/.SQLITE_PRI
b1f00 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
b1f10 53 74 72 6c 65 6e 33 30 28 63 6f 6e 73 74 20 63  Strlen30(const c
b1f20 68 61 72 20 2a 7a 29 7b 0a 20 20 63 6f 6e 73 74  har *z){.  const
b1f30 20 63 68 61 72 20 2a 7a 32 20 3d 20 7a 3b 0a 20   char *z2 = z;. 
b1f40 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75   if( z==0 ) retu
b1f50 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 2a  rn 0;.  while( *
b1f60 7a 32 20 29 7b 20 7a 32 2b 2b 3b 20 7d 0a 20 20  z2 ){ z2++; }.  
b1f70 72 65 74 75 72 6e 20 30 78 33 66 66 66 66 66 66  return 0x3ffffff
b1f80 66 20 26 20 28 69 6e 74 29 28 7a 32 20 2d 20 7a  f & (int)(z2 - z
b1f90 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  );.}../*.** Set 
b1fa0 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
b1fb0 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 65  error code and e
b1fc0 72 72 6f 72 20 73 74 72 69 6e 67 20 66 6f 72 20  rror string for 
b1fd0 74 68 65 20 73 71 6c 69 74 65 0a 2a 2a 20 68 61  the sqlite.** ha
b1fe0 6e 64 6c 65 20 22 64 62 22 2e 20 54 68 65 20 65  ndle "db". The e
b1ff0 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 65 74  rror code is set
b2000 20 74 6f 20 22 65 72 72 5f 63 6f 64 65 22 2e 0a   to "err_code"..
b2010 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 6e  **.** If it is n
b2020 6f 74 20 4e 55 4c 4c 2c 20 73 74 72 69 6e 67 20  ot NULL, string 
b2030 7a 46 6f 72 6d 61 74 20 73 70 65 63 69 66 69 65  zFormat specifie
b2040 73 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20  s the format of 
b2050 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 73 74 72  the.** error str
b2060 69 6e 67 20 69 6e 20 74 68 65 20 73 74 79 6c 65  ing in the style
b2070 20 6f 66 20 74 68 65 20 70 72 69 6e 74 66 20 66   of the printf f
b2080 75 6e 63 74 69 6f 6e 73 3a 20 54 68 65 20 66 6f  unctions: The fo
b2090 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 66 6f 72 6d 61  llowing.** forma
b20a0 74 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65  t characters are
b20b0 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20   allowed:.**.** 
b20c0 20 20 20 20 20 25 73 20 20 20 20 20 20 49 6e 73       %s      Ins
b20d0 65 72 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20  ert a string.** 
b20e0 20 20 20 20 20 25 7a 20 20 20 20 20 20 41 20 73       %z      A s
b20f0 74 72 69 6e 67 20 74 68 61 74 20 73 68 6f 75 6c  tring that shoul
b2100 64 20 62 65 20 66 72 65 65 64 20 61 66 74 65 72  d be freed after
b2110 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 25 64 20   use.**      %d 
b2120 20 20 20 20 20 49 6e 73 65 72 74 20 61 6e 20 69       Insert an i
b2130 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 25  nteger.**      %
b2140 54 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20  T      Insert a 
b2150 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53  token.**      %S
b2160 20 20 20 20 20 20 49 6e 73 65 72 74 20 74 68 65        Insert the
b2170 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f   first element o
b2180 66 20 61 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a  f a SrcList.**.*
b2190 2a 20 7a 46 6f 72 6d 61 74 20 61 6e 64 20 61 6e  * zFormat and an
b21a0 79 20 73 74 72 69 6e 67 20 74 6f 6b 65 6e 73 20  y string tokens 
b21b0 74 68 61 74 20 66 6f 6c 6c 6f 77 20 69 74 20 61  that follow it a
b21c0 72 65 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65  re assumed to be
b21d0 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 69 6e 20 55  .** encoded in U
b21e0 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 63  TF-8..**.** To c
b21f0 6c 65 61 72 20 74 68 65 20 6d 6f 73 74 20 72 65  lear the most re
b2200 63 65 6e 74 20 65 72 72 6f 72 20 66 6f 72 20 73  cent error for s
b2210 71 6c 69 74 65 20 68 61 6e 64 6c 65 20 22 64 62  qlite handle "db
b2220 22 2c 20 73 71 6c 69 74 65 33 45 72 72 6f 72 0a  ", sqlite3Error.
b2230 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c  ** should be cal
b2240 6c 65 64 20 77 69 74 68 20 65 72 72 5f 63 6f 64  led with err_cod
b2250 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f  e set to SQLITE_
b2260 4f 4b 20 61 6e 64 20 7a 46 6f 72 6d 61 74 20 73  OK and zFormat s
b2270 65 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c 2e 0a 2a  et.** to NULL..*
b2280 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b2290 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72   void sqlite3Err
b22a0 6f 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  or(sqlite3 *db, 
b22b0 69 6e 74 20 65 72 72 5f 63 6f 64 65 2c 20 63 6f  int err_code, co
b22c0 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61  nst char *zForma
b22d0 74 2c 20 2e 2e 2e 29 7b 0a 20 20 69 66 28 20 64  t, ...){.  if( d
b22e0 62 20 26 26 20 28 64 62 2d 3e 70 45 72 72 20 7c  b && (db->pErr |
b22f0 7c 20 28 64 62 2d 3e 70 45 72 72 20 3d 20 73 71  | (db->pErr = sq
b2300 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62  lite3ValueNew(db
b2310 29 29 21 3d 30 29 20 29 7b 0a 20 20 20 20 64 62  ))!=0) ){.    db
b2320 2d 3e 65 72 72 43 6f 64 65 20 3d 20 65 72 72 5f  ->errCode = err_
b2330 63 6f 64 65 3b 0a 20 20 20 20 69 66 28 20 7a 46  code;.    if( zF
b2340 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20 20 20 63  ormat ){.      c
b2350 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20 20 76 61  har *z;.      va
b2360 5f 6c 69 73 74 20 61 70 3b 0a 20 20 20 20 20 20  _list ap;.      
b2370 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f  va_start(ap, zFo
b2380 72 6d 61 74 29 3b 0a 20 20 20 20 20 20 7a 20 3d  rmat);.      z =
b2390 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74 66   sqlite3VMPrintf
b23a0 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70  (db, zFormat, ap
b23b0 29 3b 0a 20 20 20 20 20 20 76 61 5f 65 6e 64 28  );.      va_end(
b23c0 61 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ap);.      sqlit
b23d0 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 64 62  e3ValueSetStr(db
b23e0 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 7a 2c 20 53  ->pErr, -1, z, S
b23f0 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49  QLITE_UTF8, SQLI
b2400 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20  TE_DYNAMIC);.   
b2410 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
b2420 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72  lite3ValueSetStr
b2430 28 64 62 2d 3e 70 45 72 72 2c 20 30 2c 20 30 2c  (db->pErr, 0, 0,
b2440 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51   SQLITE_UTF8, SQ
b2450 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
b2460 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
b2470 20 41 64 64 20 61 6e 20 65 72 72 6f 72 20 6d 65   Add an error me
b2480 73 73 61 67 65 20 74 6f 20 70 50 61 72 73 65 2d  ssage to pParse-
b2490 3e 7a 45 72 72 4d 73 67 20 61 6e 64 20 69 6e 63  >zErrMsg and inc
b24a0 72 65 6d 65 6e 74 20 70 50 61 72 73 65 2d 3e 6e  rement pParse->n
b24b0 45 72 72 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  Err..** The foll
b24c0 6f 77 69 6e 67 20 66 6f 72 6d 61 74 74 69 6e 67  owing formatting
b24d0 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20   characters are 
b24e0 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a 2a 20 20  allowed:.**.**  
b24f0 20 20 20 20 25 73 20 20 20 20 20 20 49 6e 73 65      %s      Inse
b2500 72 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 20  rt a string.**  
b2510 20 20 20 20 25 7a 20 20 20 20 20 20 41 20 73 74      %z      A st
b2520 72 69 6e 67 20 74 68 61 74 20 73 68 6f 75 6c 64  ring that should
b2530 20 62 65 20 66 72 65 65 64 20 61 66 74 65 72 20   be freed after 
b2540 75 73 65 0a 2a 2a 20 20 20 20 20 20 25 64 20 20  use.**      %d  
b2550 20 20 20 20 49 6e 73 65 72 74 20 61 6e 20 69 6e      Insert an in
b2560 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 25 54  teger.**      %T
b2570 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 20 74        Insert a t
b2580 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 20 25 53 20  oken.**      %S 
b2590 20 20 20 20 20 49 6e 73 65 72 74 20 74 68 65 20       Insert the 
b25a0 66 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66  first element of
b25b0 20 61 20 53 72 63 4c 69 73 74 0a 2a 2a 0a 2a 2a   a SrcList.**.**
b25c0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73   This function s
b25d0 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20 74 6f  hould be used to
b25e0 20 72 65 70 6f 72 74 20 61 6e 79 20 65 72 72 6f   report any erro
b25f0 72 20 74 68 61 74 20 6f 63 63 75 72 73 20 77 68  r that occurs wh
b2600 69 6c 73 74 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e  ilst.** compilin
b2610 67 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65  g an SQL stateme
b2620 6e 74 20 28 69 2e 65 2e 20 77 69 74 68 69 6e 20  nt (i.e. within 
b2630 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
b2640 29 29 2e 20 54 68 65 0a 2a 2a 20 6c 61 73 74 20  )). The.** last 
b2650 74 68 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65  thing the sqlite
b2660 33 5f 70 72 65 70 61 72 65 28 29 20 66 75 6e 63  3_prepare() func
b2670 74 69 6f 6e 20 64 6f 65 73 20 69 73 20 63 6f 70  tion does is cop
b2680 79 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 73  y the error.** s
b2690 74 6f 72 65 64 20 62 79 20 74 68 69 73 20 66 75  tored by this fu
b26a0 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20  nction into the 
b26b0 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
b26c0 75 73 69 6e 67 20 73 71 6c 69 74 65 33 45 72 72  using sqlite3Err
b26d0 6f 72 28 29 2e 0a 2a 2a 20 46 75 6e 63 74 69 6f  or()..** Functio
b26e0 6e 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29  n sqlite3Error()
b26f0 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64 20   should be used 
b2700 64 75 72 69 6e 67 20 73 74 61 74 65 6d 65 6e 74  during statement
b2710 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20 28 73   execution.** (s
b2720 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 65 74  qlite3_step() et
b2730 63 2e 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  c.)..*/.SQLITE_P
b2740 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
b2750 74 65 33 45 72 72 6f 72 4d 73 67 28 50 61 72 73  te3ErrorMsg(Pars
b2760 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74  e *pParse, const
b2770 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20   char *zFormat, 
b2780 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20  ...){.  va_list 
b2790 61 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ap;.  sqlite3 *d
b27a0 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
b27b0 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b    pParse->nErr++
b27c0 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
b27d0 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e 7a 45  e(db, pParse->zE
b27e0 72 72 4d 73 67 29 3b 0a 20 20 76 61 5f 73 74 61  rrMsg);.  va_sta
b27f0 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b  rt(ap, zFormat);
b2800 0a 20 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  .  pParse->zErrM
b2810 73 67 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72  sg = sqlite3VMPr
b2820 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74  intf(db, zFormat
b2830 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28  , ap);.  va_end(
b2840 61 70 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 72  ap);.  pParse->r
b2850 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
b2860 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72  ;.}../*.** Clear
b2870 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
b2880 67 65 20 69 6e 20 70 50 61 72 73 65 2c 20 69 66  ge in pParse, if
b2890 20 61 6e 79 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50   any.*/.SQLITE_P
b28a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
b28b0 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 50 61  te3ErrorClear(Pa
b28c0 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20  rse *pParse){.  
b28d0 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 50  sqlite3DbFree(pP
b28e0 61 72 73 65 2d 3e 64 62 2c 20 70 50 61 72 73 65  arse->db, pParse
b28f0 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 50  ->zErrMsg);.  pP
b2900 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  arse->zErrMsg = 
b2910 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 45 72  0;.  pParse->nEr
b2920 72 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  r = 0;.}../*.** 
b2930 43 6f 6e 76 65 72 74 20 61 6e 20 53 51 4c 2d 73  Convert an SQL-s
b2940 74 79 6c 65 20 71 75 6f 74 65 64 20 73 74 72 69  tyle quoted stri
b2950 6e 67 20 69 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c  ng into a normal
b2960 20 73 74 72 69 6e 67 20 62 79 20 72 65 6d 6f 76   string by remov
b2970 69 6e 67 0a 2a 2a 20 74 68 65 20 71 75 6f 74 65  ing.** the quote
b2980 20 63 68 61 72 61 63 74 65 72 73 2e 20 20 54 68   characters.  Th
b2990 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20  e conversion is 
b29a0 64 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20  done in-place.  
b29b0 49 66 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20  If the.** input 
b29c0 64 6f 65 73 20 6e 6f 74 20 62 65 67 69 6e 20 77  does not begin w
b29d0 69 74 68 20 61 20 71 75 6f 74 65 20 63 68 61 72  ith a quote char
b29e0 61 63 74 65 72 2c 20 74 68 65 6e 20 74 68 69 73  acter, then this
b29f0 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61   routine.** is a
b2a00 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68   no-op..**.** Th
b2a10 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 6d  e input string m
b2a20 75 73 74 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d  ust be zero-term
b2a30 69 6e 61 74 65 64 2e 20 20 41 20 6e 65 77 20 7a  inated.  A new z
b2a40 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72 0a 2a  ero-terminator.*
b2a50 2a 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68  * is added to th
b2a60 65 20 64 65 71 75 6f 74 65 64 20 73 74 72 69 6e  e dequoted strin
b2a70 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  g..**.** The ret
b2a80 75 72 6e 20 76 61 6c 75 65 20 69 73 20 2d 31 20  urn value is -1 
b2a90 69 66 20 6e 6f 20 64 65 71 75 6f 74 69 6e 67 20  if no dequoting 
b2aa0 6f 63 63 75 72 73 20 6f 72 20 74 68 65 20 6c 65  occurs or the le
b2ab0 6e 67 74 68 20 6f 66 20 74 68 65 0a 2a 2a 20 64  ngth of the.** d
b2ac0 65 71 75 6f 74 65 64 20 73 74 72 69 6e 67 2c 20  equoted string, 
b2ad0 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68 65  exclusive of the
b2ae0 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72   zero terminator
b2af0 2c 20 69 66 20 64 65 71 75 6f 74 69 6e 67 20 64  , if dequoting d
b2b00 6f 65 73 0a 2a 2a 20 6f 63 63 75 72 2e 0a 2a 2a  oes.** occur..**
b2b10 0a 2a 2a 20 32 30 30 32 2d 46 65 62 2d 31 34 3a  .** 2002-Feb-14:
b2b20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
b2b30 20 65 78 74 65 6e 64 65 64 20 74 6f 20 72 65 6d   extended to rem
b2b40 6f 76 65 20 4d 53 2d 41 63 63 65 73 73 20 73 74  ove MS-Access st
b2b50 79 6c 65 0a 2a 2a 20 62 72 61 63 6b 65 74 73 20  yle.** brackets 
b2b60 66 72 6f 6d 20 61 72 6f 75 6e 64 20 69 64 65 6e  from around iden
b2b70 74 69 66 65 72 73 2e 20 20 46 6f 72 20 65 78 61  tifers.  For exa
b2b80 6d 70 6c 65 3a 20 20 22 5b 61 2d 62 2d 63 5d 22  mple:  "[a-b-c]"
b2b90 20 62 65 63 6f 6d 65 73 0a 2a 2a 20 22 61 2d 62   becomes.** "a-b
b2ba0 2d 63 22 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  -c"..*/.SQLITE_P
b2bb0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
b2bc0 65 33 44 65 71 75 6f 74 65 28 63 68 61 72 20 2a  e3Dequote(char *
b2bd0 7a 29 7b 0a 20 20 63 68 61 72 20 71 75 6f 74 65  z){.  char quote
b2be0 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20  ;.  int i, j;.  
b2bf0 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72  if( z==0 ) retur
b2c00 6e 20 2d 31 3b 0a 20 20 71 75 6f 74 65 20 3d 20  n -1;.  quote = 
b2c10 7a 5b 30 5d 3b 0a 20 20 73 77 69 74 63 68 28 20  z[0];.  switch( 
b2c20 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 63 61 73  quote ){.    cas
b2c30 65 20 27 5c 27 27 3a 20 20 62 72 65 61 6b 3b 0a  e '\'':  break;.
b2c40 20 20 20 20 63 61 73 65 20 27 22 27 3a 20 20 20      case '"':   
b2c50 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
b2c60 27 60 27 3a 20 20 20 62 72 65 61 6b 3b 20 20 20  '`':   break;   
b2c70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
b2c80 46 6f 72 20 4d 79 53 51 4c 20 63 6f 6d 70 61 74  For MySQL compat
b2c90 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 63  ibility */.    c
b2ca0 61 73 65 20 27 5b 27 3a 20 20 20 71 75 6f 74 65  ase '[':   quote
b2cb0 20 3d 20 27 5d 27 3b 20 20 62 72 65 61 6b 3b 20   = ']';  break; 
b2cc0 20 2f 2a 20 46 6f 72 20 4d 53 20 53 71 6c 53 65   /* For MS SqlSe
b2cd0 72 76 65 72 20 63 6f 6d 70 61 74 69 62 69 6c 69  rver compatibili
b2ce0 74 79 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c  ty */.    defaul
b2cf0 74 3a 20 20 20 20 72 65 74 75 72 6e 20 2d 31 3b  t:    return -1;
b2d00 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31 2c 20  .  }.  for(i=1, 
b2d10 6a 3d 30 3b 20 41 4c 57 41 59 53 28 7a 5b 69 5d  j=0; ALWAYS(z[i]
b2d20 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  ); i++){.    if(
b2d30 20 7a 5b 69 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a   z[i]==quote ){.
b2d40 20 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d        if( z[i+1]
b2d50 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 20  ==quote ){.     
b2d60 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74     z[j++] = quot
b2d70 65 3b 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a  e;.        i++;.
b2d80 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
b2d90 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
b2da0 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
b2db0 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b       z[j++] = z[
b2dc0 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  i];.    }.  }.  
b2dd0 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 72 65 74 75  z[j] = 0;.  retu
b2de0 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76  rn j;.}../* Conv
b2df0 65 6e 69 65 6e 74 20 73 68 6f 72 74 2d 68 61 6e  enient short-han
b2e00 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 70 70  d */.#define Upp
b2e10 65 72 54 6f 4c 6f 77 65 72 20 73 71 6c 69 74 65  erToLower sqlite
b2e20 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 0a 0a 2f  3UpperToLower../
b2e30 2a 0a 2a 2a 20 53 6f 6d 65 20 73 79 73 74 65 6d  *.** Some system
b2e40 73 20 68 61 76 65 20 73 74 72 69 63 6d 70 28 29  s have stricmp()
b2e50 2e 20 20 4f 74 68 65 72 73 20 68 61 76 65 20 73  .  Others have s
b2e60 74 72 63 61 73 65 63 6d 70 28 29 2e 20 20 42 65  trcasecmp().  Be
b2e70 63 61 75 73 65 0a 2a 2a 20 74 68 65 72 65 20 69  cause.** there i
b2e80 73 20 6e 6f 20 63 6f 6e 73 69 73 74 65 6e 63 79  s no consistency
b2e90 2c 20 77 65 20 77 69 6c 6c 20 64 65 66 69 6e 65  , we will define
b2ea0 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 53 51 4c   our own..*/.SQL
b2eb0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b2ec0 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 63  sqlite3StrICmp(c
b2ed0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74  onst char *zLeft
b2ee0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52  , const char *zR
b2ef0 69 67 68 74 29 7b 0a 20 20 72 65 67 69 73 74 65  ight){.  registe
b2f00 72 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  r unsigned char 
b2f10 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75  *a, *b;.  a = (u
b2f20 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a  nsigned char *)z
b2f30 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73  Left;.  b = (uns
b2f40 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69  igned char *)zRi
b2f50 67 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 2a 61  ght;.  while( *a
b2f60 21 3d 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f  !=0 && UpperToLo
b2f70 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f  wer[*a]==UpperTo
b2f80 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b  Lower[*b]){ a++;
b2f90 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e   b++; }.  return
b2fa0 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61   UpperToLower[*a
b2fb0 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 72  ] - UpperToLower
b2fc0 5b 2a 62 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  [*b];.}.SQLITE_A
b2fd0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  PI int sqlite3_s
b2fe0 74 72 6e 69 63 6d 70 28 63 6f 6e 73 74 20 63 68  trnicmp(const ch
b2ff0 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e 73 74  ar *zLeft, const
b3000 20 63 68 61 72 20 2a 7a 52 69 67 68 74 2c 20 69   char *zRight, i
b3010 6e 74 20 4e 29 7b 0a 20 20 72 65 67 69 73 74 65  nt N){.  registe
b3020 72 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  r unsigned char 
b3030 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75  *a, *b;.  a = (u
b3040 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a  nsigned char *)z
b3050 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73  Left;.  b = (uns
b3060 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69  igned char *)zRi
b3070 67 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d  ght;.  while( N-
b3080 2d 20 3e 20 30 20 26 26 20 2a 61 21 3d 30 20 26  - > 0 && *a!=0 &
b3090 26 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a  & UpperToLower[*
b30a0 61 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f 77 65 72  a]==UpperToLower
b30b0 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62 2b 2b 3b  [*b]){ a++; b++;
b30c0 20 7d 0a 20 20 72 65 74 75 72 6e 20 4e 3c 30 20   }.  return N<0 
b30d0 3f 20 30 20 3a 20 55 70 70 65 72 54 6f 4c 6f 77  ? 0 : UpperToLow
b30e0 65 72 5b 2a 61 5d 20 2d 20 55 70 70 65 72 54 6f  er[*a] - UpperTo
b30f0 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a 0a 2f 2a  Lower[*b];.}../*
b3100 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20  .** Return TRUE 
b3110 69 66 20 7a 20 69 73 20 61 20 70 75 72 65 20 6e  if z is a pure n
b3120 75 6d 65 72 69 63 20 73 74 72 69 6e 67 2e 20 20  umeric string.  
b3130 52 65 74 75 72 6e 20 46 41 4c 53 45 20 61 6e 64  Return FALSE and
b3140 20 6c 65 61 76 65 0a 2a 2a 20 2a 72 65 61 6c 6e   leave.** *realn
b3150 75 6d 20 75 6e 63 68 61 6e 67 65 64 20 69 66 20  um unchanged if 
b3160 74 68 65 20 73 74 72 69 6e 67 20 63 6f 6e 74 61  the string conta
b3170 69 6e 73 20 61 6e 79 20 63 68 61 72 61 63 74 65  ins any characte
b3180 72 20 77 68 69 63 68 20 69 73 20 6e 6f 74 0a 2a  r which is not.*
b3190 2a 20 70 61 72 74 20 6f 66 20 61 20 6e 75 6d 62  * part of a numb
b31a0 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  er..**.** If the
b31b0 20 73 74 72 69 6e 67 20 69 73 20 70 75 72 65 20   string is pure 
b31c0 6e 75 6d 65 72 69 63 2c 20 73 65 74 20 2a 72 65  numeric, set *re
b31d0 61 6c 6e 75 6d 20 74 6f 20 54 52 55 45 20 69 66  alnum to TRUE if
b31e0 20 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 63   the string.** c
b31f0 6f 6e 74 61 69 6e 73 20 74 68 65 20 27 2e 27 20  ontains the '.' 
b3200 63 68 61 72 61 63 74 65 72 20 6f 72 20 61 6e 20  character or an 
b3210 22 45 2b 30 30 30 22 20 73 74 79 6c 65 20 65 78  "E+000" style ex
b3220 70 6f 6e 65 6e 74 69 61 74 69 6f 6e 20 73 75 66  ponentiation suf
b3230 66 69 78 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73  fix..** Otherwis
b3240 65 20 73 65 74 20 2a 72 65 61 6c 6e 75 6d 20 74  e set *realnum t
b3250 6f 20 46 41 4c 53 45 2e 20 20 4e 6f 74 65 20 74  o FALSE.  Note t
b3260 68 61 74 20 6a 75 73 74 20 62 65 63 61 75 65 20  hat just becaue 
b3270 2a 72 65 61 6c 6e 75 6d 20 69 73 0a 2a 2a 20 66  *realnum is.** f
b3280 61 6c 73 65 20 64 6f 65 73 20 6e 6f 74 20 6d 65  alse does not me
b3290 61 6e 20 74 68 61 74 20 74 68 65 20 6e 75 6d 62  an that the numb
b32a0 65 72 20 63 61 6e 20 62 65 20 73 75 63 63 65 73  er can be succes
b32b0 73 66 75 6c 6c 79 20 63 6f 6e 76 65 72 74 65 64  sfully converted
b32c0 20 69 6e 74 6f 0a 2a 2a 20 61 6e 20 69 6e 74 65   into.** an inte
b32d0 67 65 72 20 2d 20 69 74 20 6d 69 67 68 74 20 62  ger - it might b
b32e0 65 20 74 6f 6f 20 62 69 67 2e 0a 2a 2a 0a 2a 2a  e too big..**.**
b32f0 20 41 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67   An empty string
b3300 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 6e   is considered n
b3310 6f 6e 2d 6e 75 6d 65 72 69 63 2e 0a 2a 2f 0a 53  on-numeric..*/.S
b3320 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
b3330 74 20 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65  t sqlite3IsNumbe
b3340 72 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c  r(const char *z,
b3350 20 69 6e 74 20 2a 72 65 61 6c 6e 75 6d 2c 20 75   int *realnum, u
b3360 38 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 69 6e  8 enc){.  int in
b3370 63 72 20 3d 20 28 65 6e 63 3d 3d 53 51 4c 49 54  cr = (enc==SQLIT
b3380 45 5f 55 54 46 38 3f 31 3a 32 29 3b 0a 20 20 69  E_UTF8?1:2);.  i
b3390 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55  f( enc==SQLITE_U
b33a0 54 46 31 36 42 45 20 29 20 7a 2b 2b 3b 0a 20 20  TF16BE ) z++;.  
b33b0 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 7c 7c 20 2a  if( *z=='-' || *
b33c0 7a 3d 3d 27 2b 27 20 29 20 7a 20 2b 3d 20 69 6e  z=='+' ) z += in
b33d0 63 72 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74  cr;.  if( !sqlit
b33e0 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b  e3Isdigit(*z) ){
b33f0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
b3400 20 7d 0a 20 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a   }.  z += incr;.
b3410 20 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 30 3b 0a    *realnum = 0;.
b3420 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33    while( sqlite3
b3430 49 73 64 69 67 69 74 28 2a 7a 29 20 29 7b 20 7a  Isdigit(*z) ){ z
b3440 20 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 69 66   += incr; }.  if
b3450 28 20 2a 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20  ( *z=='.' ){.   
b3460 20 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20   z += incr;.    
b3470 69 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 69  if( !sqlite3Isdi
b3480 67 69 74 28 2a 7a 29 20 29 20 72 65 74 75 72 6e  git(*z) ) return
b3490 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73   0;.    while( s
b34a0 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a  qlite3Isdigit(*z
b34b0 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20  ) ){ z += incr; 
b34c0 7d 0a 20 20 20 20 2a 72 65 61 6c 6e 75 6d 20 3d  }.    *realnum =
b34d0 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 2a 7a   1;.  }.  if( *z
b34e0 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27 45 27  =='e' || *z=='E'
b34f0 20 29 7b 0a 20 20 20 20 7a 20 2b 3d 20 69 6e 63   ){.    z += inc
b3500 72 3b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 27  r;.    if( *z=='
b3510 2b 27 20 7c 7c 20 2a 7a 3d 3d 27 2d 27 20 29 20  +' || *z=='-' ) 
b3520 7a 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69  z += incr;.    i
b3530 66 28 20 21 73 71 6c 69 74 65 33 49 73 64 69 67  f( !sqlite3Isdig
b3540 69 74 28 2a 7a 29 20 29 20 72 65 74 75 72 6e 20  it(*z) ) return 
b3550 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73 71  0;.    while( sq
b3560 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29  lite3Isdigit(*z)
b3570 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72 3b 20 7d   ){ z += incr; }
b3580 0a 20 20 20 20 2a 72 65 61 6c 6e 75 6d 20 3d 20  .    *realnum = 
b3590 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  1;.  }.  return 
b35a0 2a 7a 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  *z==0;.}../*.** 
b35b0 54 68 65 20 73 74 72 69 6e 67 20 7a 5b 5d 20 69  The string z[] i
b35c0 73 20 61 6e 20 41 53 43 49 49 20 72 65 70 72 65  s an ASCII repre
b35d0 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 72  sentation of a r
b35e0 65 61 6c 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 43  eal number..** C
b35f0 6f 6e 76 65 72 74 20 74 68 69 73 20 73 74 72 69  onvert this stri
b3600 6e 67 20 74 6f 20 61 20 64 6f 75 62 6c 65 2e 0a  ng to a double..
b3610 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
b3620 6e 65 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  ne assumes that 
b3630 7a 5b 5d 20 72 65 61 6c 6c 79 20 69 73 20 61 20  z[] really is a 
b3640 76 61 6c 69 64 20 6e 75 6d 62 65 72 2e 20 20 49  valid number.  I
b3650 66 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 2c 20  f it.** is not, 
b3660 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 75 6e  the result is un
b3670 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  defined..**.** T
b3680 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
b3690 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74  sed instead of t
b36a0 68 65 20 6c 69 62 72 61 72 79 20 61 74 6f 66 28  he library atof(
b36b0 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 63 61 75  ) function becau
b36c0 73 65 0a 2a 2a 20 74 68 65 20 6c 69 62 72 61 72  se.** the librar
b36d0 79 20 61 74 6f 66 28 29 20 6d 69 67 68 74 20 77  y atof() might w
b36e0 61 6e 74 20 74 6f 20 75 73 65 20 22 2c 22 20 61  ant to use "," a
b36f0 73 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f  s the decimal po
b3700 69 6e 74 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f  int instead.** o
b3710 66 20 22 2e 22 20 64 65 70 65 6e 64 69 6e 67 20  f "." depending 
b3720 6f 6e 20 68 6f 77 20 6c 6f 63 61 6c 65 20 69 73  on how locale is
b3730 20 73 65 74 2e 20 20 42 75 74 20 74 68 61 74 20   set.  But that 
b3740 77 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62  would cause prob
b3750 6c 65 6d 73 0a 2a 2a 20 66 6f 72 20 53 51 4c 2e  lems.** for SQL.
b3760 20 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69 6e    So this routin
b3770 65 20 61 6c 77 61 79 73 20 75 73 65 73 20 22 2e  e always uses ".
b3780 22 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20  " regardless of 
b3790 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  locale..*/.SQLIT
b37a0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
b37b0 6c 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74 20  lite3AtoF(const 
b37c0 63 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65 20  char *z, double 
b37d0 2a 70 52 65 73 75 6c 74 29 7b 0a 23 69 66 6e 64  *pResult){.#ifnd
b37e0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
b37f0 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20  LOATING_POINT.  
b3800 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 42 65 67  const char *zBeg
b3810 69 6e 20 3d 20 7a 3b 0a 20 20 2f 2a 20 73 69 67  in = z;.  /* sig
b3820 6e 20 2a 20 73 69 67 6e 69 66 69 63 61 6e 64 20  n * significand 
b3830 2a 20 28 31 30 20 5e 20 28 65 73 69 67 6e 20 2a  * (10 ^ (esign *
b3840 20 65 78 70 6f 6e 65 6e 74 29 29 20 2a 2f 0a 20   exponent)) */. 
b3850 20 69 6e 74 20 73 69 67 6e 20 3d 20 31 3b 20 20   int sign = 1;  
b3860 20 2f 2a 20 73 69 67 6e 20 6f 66 20 73 69 67 6e   /* sign of sign
b3870 69 66 69 63 61 6e 64 20 2a 2f 0a 20 20 69 36 34  ificand */.  i64
b3880 20 73 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20   s = 0;      /* 
b3890 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a 20  significand */. 
b38a0 20 69 6e 74 20 64 20 3d 20 30 3b 20 20 20 20 20   int d = 0;     
b38b0 20 2f 2a 20 61 64 6a 75 73 74 20 65 78 70 6f 6e   /* adjust expon
b38c0 65 6e 74 20 66 6f 72 20 73 68 69 66 74 69 6e 67  ent for shifting
b38d0 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 2a   decimal point *
b38e0 2f 0a 20 20 69 6e 74 20 65 73 69 67 6e 20 3d 20  /.  int esign = 
b38f0 31 3b 20 20 2f 2a 20 73 69 67 6e 20 6f 66 20 65  1;  /* sign of e
b3900 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  xponent */.  int
b3910 20 65 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20   e = 0;      /* 
b3920 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 64 6f  exponent */.  do
b3930 75 62 6c 65 20 72 65 73 75 6c 74 3b 0a 20 20 69  uble result;.  i
b3940 6e 74 20 6e 44 69 67 69 74 73 20 3d 20 30 3b 0a  nt nDigits = 0;.
b3950 0a 20 20 2f 2a 20 73 6b 69 70 20 6c 65 61 64 69  .  /* skip leadi
b3960 6e 67 20 73 70 61 63 65 73 20 2a 2f 0a 20 20 77  ng spaces */.  w
b3970 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73  hile( sqlite3Iss
b3980 70 61 63 65 28 2a 7a 29 20 29 20 7a 2b 2b 3b 0a  pace(*z) ) z++;.
b3990 20 20 2f 2a 20 67 65 74 20 73 69 67 6e 20 6f 66    /* get sign of
b39a0 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a 2f 0a   significand */.
b39b0 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b    if( *z=='-' ){
b39c0 0a 20 20 20 20 73 69 67 6e 20 3d 20 2d 31 3b 0a  .    sign = -1;.
b39d0 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 65 6c 73 65      z++;.  }else
b39e0 20 69 66 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a   if( *z=='+' ){.
b39f0 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 2f      z++;.  }.  /
b3a00 2a 20 73 6b 69 70 20 6c 65 61 64 69 6e 67 20 7a  * skip leading z
b3a10 65 72 6f 65 73 20 2a 2f 0a 20 20 77 68 69 6c 65  eroes */.  while
b3a20 28 20 7a 5b 30 5d 3d 3d 27 30 27 20 29 20 7a 2b  ( z[0]=='0' ) z+
b3a30 2b 2c 20 6e 44 69 67 69 74 73 2b 2b 3b 0a 0a 20  +, nDigits++;.. 
b3a40 20 2f 2a 20 63 6f 70 79 20 6d 61 78 20 73 69 67   /* copy max sig
b3a50 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74 73 20  nificant digits 
b3a60 74 6f 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a  to significand *
b3a70 2f 0a 20 20 77 68 69 6c 65 28 20 73 71 6c 69 74  /.  while( sqlit
b3a80 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20 26 26  e3Isdigit(*z) &&
b3a90 20 73 3c 28 28 4c 41 52 47 45 53 54 5f 49 4e 54   s<((LARGEST_INT
b3aa0 36 34 2d 39 29 2f 31 30 29 20 29 7b 0a 20 20 20  64-9)/10) ){.   
b3ab0 20 73 20 3d 20 73 2a 31 30 20 2b 20 28 2a 7a 20   s = s*10 + (*z 
b3ac0 2d 20 27 30 27 29 3b 0a 20 20 20 20 7a 2b 2b 2c  - '0');.    z++,
b3ad0 20 6e 44 69 67 69 74 73 2b 2b 3b 0a 20 20 7d 0a   nDigits++;.  }.
b3ae0 20 20 2f 2a 20 73 6b 69 70 20 6e 6f 6e 2d 73 69    /* skip non-si
b3af0 67 6e 69 66 69 63 61 6e 74 20 73 69 67 6e 69 66  gnificant signif
b3b00 69 63 61 6e 64 20 64 69 67 69 74 73 0a 20 20 2a  icand digits.  *
b3b10 2a 20 28 69 6e 63 72 65 61 73 65 20 65 78 70 6f  * (increase expo
b3b20 6e 65 6e 74 20 62 79 20 64 20 74 6f 20 73 68 69  nent by d to shi
b3b30 66 74 20 64 65 63 69 6d 61 6c 20 6c 65 66 74 29  ft decimal left)
b3b40 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 73 71 6c   */.  while( sql
b3b50 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20  ite3Isdigit(*z) 
b3b60 29 20 7a 2b 2b 2c 20 6e 44 69 67 69 74 73 2b 2b  ) z++, nDigits++
b3b70 2c 20 64 2b 2b 3b 0a 0a 20 20 2f 2a 20 69 66 20  , d++;..  /* if 
b3b80 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 69 73  decimal point is
b3b90 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 66   present */.  if
b3ba0 28 20 2a 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20  ( *z=='.' ){.   
b3bb0 20 7a 2b 2b 3b 0a 20 20 20 20 2f 2a 20 63 6f 70   z++;.    /* cop
b3bc0 79 20 64 69 67 69 74 73 20 66 72 6f 6d 20 61 66  y digits from af
b3bd0 74 65 72 20 64 65 63 69 6d 61 6c 20 74 6f 20 73  ter decimal to s
b3be0 69 67 6e 69 66 69 63 61 6e 64 0a 20 20 20 20 2a  ignificand.    *
b3bf0 2a 20 28 64 65 63 72 65 61 73 65 20 65 78 70 6f  * (decrease expo
b3c00 6e 65 6e 74 20 62 79 20 64 20 74 6f 20 73 68 69  nent by d to shi
b3c10 66 74 20 64 65 63 69 6d 61 6c 20 72 69 67 68 74  ft decimal right
b3c20 29 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20  ) */.    while( 
b3c30 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28 2a  sqlite3Isdigit(*
b3c40 7a 29 20 26 26 20 73 3c 28 28 4c 41 52 47 45 53  z) && s<((LARGES
b3c50 54 5f 49 4e 54 36 34 2d 39 29 2f 31 30 29 20 29  T_INT64-9)/10) )
b3c60 7b 0a 20 20 20 20 20 20 73 20 3d 20 73 2a 31 30  {.      s = s*10
b3c70 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a 20   + (*z - '0');. 
b3c80 20 20 20 20 20 7a 2b 2b 2c 20 6e 44 69 67 69 74       z++, nDigit
b3c90 73 2b 2b 2c 20 64 2d 2d 3b 0a 20 20 20 20 7d 0a  s++, d--;.    }.
b3ca0 20 20 20 20 2f 2a 20 73 6b 69 70 20 6e 6f 6e 2d      /* skip non-
b3cb0 73 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69  significant digi
b3cc0 74 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28  ts */.    while(
b3cd0 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28   sqlite3Isdigit(
b3ce0 2a 7a 29 20 29 20 7a 2b 2b 2c 20 6e 44 69 67 69  *z) ) z++, nDigi
b3cf0 74 73 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ts++;.  }..  /* 
b3d00 69 66 20 65 78 70 6f 6e 65 6e 74 20 69 73 20 70  if exponent is p
b3d10 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 66 28 20  resent */.  if( 
b3d20 2a 7a 3d 3d 27 65 27 20 7c 7c 20 2a 7a 3d 3d 27  *z=='e' || *z=='
b3d30 45 27 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20  E' ){.    z++;. 
b3d40 20 20 20 2f 2a 20 67 65 74 20 73 69 67 6e 20 6f     /* get sign o
b3d50 66 20 65 78 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20  f exponent */.  
b3d60 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b    if( *z=='-' ){
b3d70 0a 20 20 20 20 20 20 65 73 69 67 6e 20 3d 20 2d  .      esign = -
b3d80 31 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20  1;.      z++;.  
b3d90 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d    }else if( *z==
b3da0 27 2b 27 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b  '+' ){.      z++
b3db0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 63  ;.    }.    /* c
b3dc0 6f 70 79 20 64 69 67 69 74 73 20 74 6f 20 65 78  opy digits to ex
b3dd0 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 20 20 77 68  ponent */.    wh
b3de0 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64 69  ile( sqlite3Isdi
b3df0 67 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20 20 20  git(*z) ){.     
b3e00 20 65 20 3d 20 65 2a 31 30 20 2b 20 28 2a 7a 20   e = e*10 + (*z 
b3e10 2d 20 27 30 27 29 3b 0a 20 20 20 20 20 20 7a 2b  - '0');.      z+
b3e20 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  +;.    }.  }..  
b3e30 2f 2a 20 61 64 6a 75 73 74 20 65 78 70 6f 6e 65  /* adjust expone
b3e40 6e 74 20 62 79 20 64 2c 20 61 6e 64 20 75 70 64  nt by d, and upd
b3e50 61 74 65 20 73 69 67 6e 20 2a 2f 0a 20 20 65 20  ate sign */.  e 
b3e60 3d 20 28 65 2a 65 73 69 67 6e 29 20 2b 20 64 3b  = (e*esign) + d;
b3e70 0a 20 20 69 66 28 20 65 3c 30 20 29 20 7b 0a 20  .  if( e<0 ) {. 
b3e80 20 20 20 65 73 69 67 6e 20 3d 20 2d 31 3b 0a 20     esign = -1;. 
b3e90 20 20 20 65 20 2a 3d 20 2d 31 3b 0a 20 20 7d 20     e *= -1;.  } 
b3ea0 65 6c 73 65 20 7b 0a 20 20 20 20 65 73 69 67 6e  else {.    esign
b3eb0 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   = 1;.  }..  /* 
b3ec0 69 66 20 30 20 73 69 67 6e 69 66 69 63 61 6e 64  if 0 significand
b3ed0 20 2a 2f 0a 20 20 69 66 28 20 21 73 20 29 20 7b   */.  if( !s ) {
b3ee0 0a 20 20 20 20 2f 2a 20 49 6e 20 74 68 65 20 49  .    /* In the I
b3ef0 45 45 45 20 37 35 34 20 73 74 61 6e 64 61 72 64  EEE 754 standard
b3f00 2c 20 7a 65 72 6f 20 69 73 20 73 69 67 6e 65 64  , zero is signed
b3f10 2e 0a 20 20 20 20 2a 2a 20 41 64 64 20 74 68 65  ..    ** Add the
b3f20 20 73 69 67 6e 20 69 66 20 77 65 27 76 65 20 73   sign if we've s
b3f30 65 65 6e 20 61 74 20 6c 65 61 73 74 20 6f 6e 65  een at least one
b3f40 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20 72 65   digit */.    re
b3f50 73 75 6c 74 20 3d 20 28 73 69 67 6e 3c 30 20 26  sult = (sign<0 &
b3f60 26 20 6e 44 69 67 69 74 73 29 20 3f 20 2d 28 64  & nDigits) ? -(d
b3f70 6f 75 62 6c 65 29 30 20 3a 20 28 64 6f 75 62 6c  ouble)0 : (doubl
b3f80 65 29 30 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a  e)0;.  } else {.
b3f90 20 20 20 20 2f 2a 20 61 74 74 65 6d 70 74 20 74      /* attempt t
b3fa0 6f 20 72 65 64 75 63 65 20 65 78 70 6f 6e 65 6e  o reduce exponen
b3fb0 74 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 73 69  t */.    if( esi
b3fc0 67 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 77 68  gn>0 ){.      wh
b3fd0 69 6c 65 28 20 73 3c 28 4c 41 52 47 45 53 54 5f  ile( s<(LARGEST_
b3fe0 49 4e 54 36 34 2f 31 30 29 20 26 26 20 65 3e 30  INT64/10) && e>0
b3ff0 20 29 20 65 2d 2d 2c 73 2a 3d 31 30 3b 0a 20 20   ) e--,s*=10;.  
b4000 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 77    }else{.      w
b4010 68 69 6c 65 28 20 21 28 73 25 31 30 29 20 26 26  hile( !(s%10) &&
b4020 20 65 3e 30 20 29 20 65 2d 2d 2c 73 2f 3d 31 30   e>0 ) e--,s/=10
b4030 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
b4040 61 64 6a 75 73 74 20 74 68 65 20 73 69 67 6e 20  adjust the sign 
b4050 6f 66 20 73 69 67 6e 69 66 69 63 61 6e 64 20 2a  of significand *
b4060 2f 0a 20 20 20 20 73 20 3d 20 73 69 67 6e 3c 30  /.    s = sign<0
b4070 20 3f 20 2d 73 20 3a 20 73 3b 0a 0a 20 20 20 20   ? -s : s;..    
b4080 2f 2a 20 69 66 20 65 78 70 6f 6e 65 6e 74 2c 20  /* if exponent, 
b4090 73 63 61 6c 65 20 73 69 67 6e 69 66 69 63 61 6e  scale significan
b40a0 64 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65  d as appropriate
b40b0 0a 20 20 20 20 2a 2a 20 61 6e 64 20 73 74 6f 72  .    ** and stor
b40c0 65 20 69 6e 20 72 65 73 75 6c 74 2e 20 2a 2f 0a  e in result. */.
b40d0 20 20 20 20 69 66 28 20 65 20 29 7b 0a 20 20 20      if( e ){.   
b40e0 20 20 20 64 6f 75 62 6c 65 20 73 63 61 6c 65 20     double scale 
b40f0 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20 2f 2a 20  = 1.0;.      /* 
b4100 61 74 74 65 6d 70 74 20 74 6f 20 68 61 6e 64 6c  attempt to handl
b4110 65 20 65 78 74 72 65 6d 65 6c 79 20 73 6d 61 6c  e extremely smal
b4120 6c 2f 6c 61 72 67 65 20 6e 75 6d 62 65 72 73 20  l/large numbers 
b4130 62 65 74 74 65 72 20 2a 2f 0a 20 20 20 20 20 20  better */.      
b4140 69 66 28 20 65 3e 33 30 37 20 26 26 20 65 3c 33  if( e>307 && e<3
b4150 34 32 20 29 7b 0a 20 20 20 20 20 20 20 20 77 68  42 ){.        wh
b4160 69 6c 65 28 20 65 25 33 30 38 20 29 20 7b 20 73  ile( e%308 ) { s
b4170 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 3b 20  cale *= 1.0e+1; 
b4180 65 20 2d 3d 20 31 3b 20 7d 0a 20 20 20 20 20 20  e -= 1; }.      
b4190 20 20 69 66 28 20 65 73 69 67 6e 3c 30 20 29 7b    if( esign<0 ){
b41a0 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c  .          resul
b41b0 74 20 3d 20 73 20 2f 20 73 63 61 6c 65 3b 0a 20  t = s / scale;. 
b41c0 20 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 20           result 
b41d0 2f 3d 20 31 2e 30 65 2b 33 30 38 3b 0a 20 20 20  /= 1.0e+308;.   
b41e0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
b41f0 20 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 73        result = s
b4200 20 2a 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 20   * scale;.      
b4210 20 20 20 20 72 65 73 75 6c 74 20 2a 3d 20 31 2e      result *= 1.
b4220 30 65 2b 33 30 38 3b 0a 20 20 20 20 20 20 20 20  0e+308;.        
b4230 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  }.      }else{. 
b4240 20 20 20 20 20 20 20 2f 2a 20 31 2e 30 65 2b 32         /* 1.0e+2
b4250 32 20 69 73 20 74 68 65 20 6c 61 72 67 65 73 74  2 is the largest
b4260 20 70 6f 77 65 72 20 6f 66 20 31 30 20 74 68 61   power of 10 tha
b4270 6e 20 63 61 6e 20 62 65 20 0a 20 20 20 20 20 20  n can be .      
b4280 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 65 64    ** represented
b4290 20 65 78 61 63 74 6c 79 2e 20 2a 2f 0a 20 20 20   exactly. */.   
b42a0 20 20 20 20 20 77 68 69 6c 65 28 20 65 25 32 32       while( e%22
b42b0 20 29 20 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e   ) { scale *= 1.
b42c0 30 65 2b 31 3b 20 65 20 2d 3d 20 31 3b 20 7d 0a  0e+1; e -= 1; }.
b42d0 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 65          while( e
b42e0 3e 30 20 29 20 7b 20 73 63 61 6c 65 20 2a 3d 20  >0 ) { scale *= 
b42f0 31 2e 30 65 2b 32 32 3b 20 65 20 2d 3d 20 32 32  1.0e+22; e -= 22
b4300 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ; }.        if( 
b4310 65 73 69 67 6e 3c 30 20 29 7b 0a 20 20 20 20 20  esign<0 ){.     
b4320 20 20 20 20 20 72 65 73 75 6c 74 20 3d 20 73 20       result = s 
b4330 2f 20 73 63 61 6c 65 3b 0a 20 20 20 20 20 20 20  / scale;.       
b4340 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
b4350 20 20 72 65 73 75 6c 74 20 3d 20 73 20 2a 20 73    result = s * s
b4360 63 61 6c 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  cale;.        }.
b4370 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c        }.    } el
b4380 73 65 20 7b 0a 20 20 20 20 20 20 72 65 73 75 6c  se {.      resul
b4390 74 20 3d 20 28 64 6f 75 62 6c 65 29 73 3b 0a 20  t = (double)s;. 
b43a0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 73     }.  }..  /* s
b43b0 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
b43c0 2a 2f 0a 20 20 2a 70 52 65 73 75 6c 74 20 3d 20  */.  *pResult = 
b43d0 72 65 73 75 6c 74 3b 0a 0a 20 20 2f 2a 20 72 65  result;..  /* re
b43e0 74 75 72 6e 20 6e 75 6d 62 65 72 20 6f 66 20 63  turn number of c
b43f0 68 61 72 61 63 74 65 72 73 20 75 73 65 64 20 2a  haracters used *
b4400 2f 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29  /.  return (int)
b4410 28 7a 20 2d 20 7a 42 65 67 69 6e 29 3b 0a 23 65  (z - zBegin);.#e
b4420 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  lse.  return sql
b4430 69 74 65 33 41 74 6f 69 36 34 28 7a 2c 20 70 52  ite3Atoi64(z, pR
b4440 65 73 75 6c 74 29 3b 0a 23 65 6e 64 69 66 20 2f  esult);.#endif /
b4450 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  * SQLITE_OMIT_FL
b4460 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a 2f 0a  OATING_POINT */.
b4470 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65  }../*.** Compare
b4480 20 74 68 65 20 31 39 2d 63 68 61 72 61 63 74 65   the 19-characte
b4490 72 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20 61 67  r string zNum ag
b44a0 61 69 6e 73 74 20 74 68 65 20 74 65 78 74 20 72  ainst the text r
b44b0 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a  epresentation.**
b44c0 20 76 61 6c 75 65 20 32 5e 36 33 3a 20 20 39 32   value 2^63:  92
b44d0 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30  2337203685477580
b44e0 38 2e 20 20 52 65 74 75 72 6e 20 6e 65 67 61 74  8.  Return negat
b44f0 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70 6f  ive, zero, or po
b4500 73 69 74 69 76 65 0a 2a 2a 20 69 66 20 7a 4e 75  sitive.** if zNu
b4510 6d 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20  m is less than, 
b4520 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65  equal to, or gre
b4530 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 73 74  ater than the st
b4540 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 69  ring..**.** Unli
b4550 6b 65 20 6d 65 6d 63 6d 70 28 29 20 74 68 69 73  ke memcmp() this
b4560 20 72 6f 75 74 69 6e 65 20 69 73 20 67 75 61 72   routine is guar
b4570 61 6e 74 65 65 64 20 74 6f 20 72 65 74 75 72 6e  anteed to return
b4580 20 74 68 65 20 64 69 66 66 65 72 65 6e 63 65 0a   the difference.
b4590 2a 2a 20 69 6e 20 74 68 65 20 76 61 6c 75 65 73  ** in the values
b45a0 20 6f 66 20 74 68 65 20 6c 61 73 74 20 64 69 67   of the last dig
b45b0 69 74 20 69 66 20 74 68 65 20 6f 6e 6c 79 20 64  it if the only d
b45c0 69 66 66 65 72 65 6e 63 65 20 69 73 20 69 6e 20  ifference is in 
b45d0 74 68 65 0a 2a 2a 20 6c 61 73 74 20 64 69 67 69  the.** last digi
b45e0 74 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78 61 6d  t.  So, for exam
b45f0 70 6c 65 2c 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  ple,.**.**      
b4600 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 22 39  compare2pow63("9
b4610 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38  2233720368547758
b4620 30 30 22 29 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c 20  00").**.** will 
b4630 72 65 74 75 72 6e 20 2d 38 2e 0a 2a 2f 0a 73 74  return -8..*/.st
b4640 61 74 69 63 20 69 6e 74 20 63 6f 6d 70 61 72 65  atic int compare
b4650 32 70 6f 77 36 33 28 63 6f 6e 73 74 20 63 68 61  2pow63(const cha
b4660 72 20 2a 7a 4e 75 6d 29 7b 0a 20 20 69 6e 74 20  r *zNum){.  int 
b4670 63 3b 0a 20 20 63 20 3d 20 6d 65 6d 63 6d 70 28  c;.  c = memcmp(
b4680 7a 4e 75 6d 2c 22 39 32 32 33 33 37 32 30 33 36  zNum,"9223372036
b4690 38 35 34 37 37 35 38 30 22 2c 31 38 29 2a 31 30  85477580",18)*10
b46a0 3b 0a 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a  ;.  if( c==0 ){.
b46b0 20 20 20 20 63 20 3d 20 7a 4e 75 6d 5b 31 38 5d      c = zNum[18]
b46c0 20 2d 20 27 38 27 3b 0a 20 20 7d 0a 20 20 72 65   - '8';.  }.  re
b46d0 74 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  turn c;.}.../*.*
b46e0 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66  * Return TRUE if
b46f0 20 7a 4e 75 6d 20 69 73 20 61 20 36 34 2d 62 69   zNum is a 64-bi
b4700 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
b4710 20 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68   and write.** th
b4720 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69  e value of the i
b4730 6e 74 65 67 65 72 20 69 6e 74 6f 20 2a 70 4e 75  nteger into *pNu
b4740 6d 2e 20 20 49 66 20 7a 4e 75 6d 20 69 73 20 6e  m.  If zNum is n
b4750 6f 74 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a  ot an integer.**
b4760 20 6f 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65   or is an intege
b4770 72 20 74 68 61 74 20 69 73 20 74 6f 6f 20 6c 61  r that is too la
b4780 72 67 65 20 74 6f 20 62 65 20 65 78 70 72 65 73  rge to be expres
b4790 73 65 64 20 77 69 74 68 20 36 34 20 62 69 74 73  sed with 64 bits
b47a0 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e  ,.** then return
b47b0 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 68   false..**.** Wh
b47c0 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
b47d0 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 77  was originally w
b47e0 72 69 74 74 65 6e 20 69 74 20 64 65 61 6c 74 20  ritten it dealt 
b47f0 77 69 74 68 20 6f 6e 6c 79 0a 2a 2a 20 33 32 2d  with only.** 32-
b4800 62 69 74 20 6e 75 6d 62 65 72 73 2e 20 20 41 74  bit numbers.  At
b4810 20 74 68 61 74 20 74 69 6d 65 2c 20 69 74 20 77   that time, it w
b4820 61 73 20 6d 75 63 68 20 66 61 73 74 65 72 20 74  as much faster t
b4830 68 61 6e 20 74 68 65 0a 2a 2a 20 61 74 6f 69 28  han the.** atoi(
b4840 29 20 6c 69 62 72 61 72 79 20 72 6f 75 74 69 6e  ) library routin
b4850 65 20 69 6e 20 52 65 64 48 61 74 20 37 2e 32 2e  e in RedHat 7.2.
b4860 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
b4870 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74  TE int sqlite3At
b4880 6f 69 36 34 28 63 6f 6e 73 74 20 63 68 61 72 20  oi64(const char 
b4890 2a 7a 4e 75 6d 2c 20 69 36 34 20 2a 70 4e 75 6d  *zNum, i64 *pNum
b48a0 29 7b 0a 20 20 69 36 34 20 76 20 3d 20 30 3b 0a  ){.  i64 v = 0;.
b48b0 20 20 69 6e 74 20 6e 65 67 3b 0a 20 20 69 6e 74    int neg;.  int
b48c0 20 69 2c 20 63 3b 0a 20 20 63 6f 6e 73 74 20 63   i, c;.  const c
b48d0 68 61 72 20 2a 7a 53 74 61 72 74 3b 0a 20 20 77  har *zStart;.  w
b48e0 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73  hile( sqlite3Iss
b48f0 70 61 63 65 28 2a 7a 4e 75 6d 29 20 29 20 7a 4e  pace(*zNum) ) zN
b4900 75 6d 2b 2b 3b 0a 20 20 69 66 28 20 2a 7a 4e 75  um++;.  if( *zNu
b4910 6d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65  m=='-' ){.    ne
b4920 67 20 3d 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b  g = 1;.    zNum+
b4930 2b 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 2a  +;.  }else if( *
b4940 7a 4e 75 6d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20  zNum=='+' ){.   
b4950 20 6e 65 67 20 3d 20 30 3b 0a 20 20 20 20 7a 4e   neg = 0;.    zN
b4960 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  um++;.  }else{. 
b4970 20 20 20 6e 65 67 20 3d 20 30 3b 0a 20 20 7d 0a     neg = 0;.  }.
b4980 20 20 7a 53 74 61 72 74 20 3d 20 7a 4e 75 6d 3b    zStart = zNum;
b4990 0a 20 20 77 68 69 6c 65 28 20 7a 4e 75 6d 5b 30  .  while( zNum[0
b49a0 5d 3d 3d 27 30 27 20 29 7b 20 7a 4e 75 6d 2b 2b  ]=='0' ){ zNum++
b49b0 3b 20 7d 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72  ; } /* Skip over
b49c0 20 6c 65 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20   leading zeros. 
b49d0 54 69 63 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a  Ticket #2454 */.
b49e0 20 20 66 6f 72 28 69 3d 30 3b 20 28 63 3d 7a 4e    for(i=0; (c=zN
b49f0 75 6d 5b 69 5d 29 3e 3d 27 30 27 20 26 26 20 63  um[i])>='0' && c
b4a00 3c 3d 27 39 27 3b 20 69 2b 2b 29 7b 0a 20 20 20  <='9'; i++){.   
b4a10 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20   v = v*10 + c - 
b4a20 27 30 27 3b 0a 20 20 7d 0a 20 20 2a 70 4e 75 6d  '0';.  }.  *pNum
b4a30 20 3d 20 6e 65 67 20 3f 20 2d 76 20 3a 20 76 3b   = neg ? -v : v;
b4a40 0a 20 20 69 66 28 20 63 21 3d 30 20 7c 7c 20 28  .  if( c!=0 || (
b4a50 69 3d 3d 30 20 26 26 20 7a 53 74 61 72 74 3d 3d  i==0 && zStart==
b4a60 7a 4e 75 6d 29 20 7c 7c 20 69 3e 31 39 20 29 7b  zNum) || i>19 ){
b4a70 0a 20 20 20 20 2f 2a 20 7a 4e 75 6d 20 69 73 20  .    /* zNum is 
b4a80 65 6d 70 74 79 20 6f 72 20 63 6f 6e 74 61 69 6e  empty or contain
b4a90 73 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 20 74 65  s non-numeric te
b4aa0 78 74 20 6f 72 20 69 73 20 6c 6f 6e 67 65 72 0a  xt or is longer.
b4ab0 20 20 20 20 2a 2a 20 74 68 61 6e 20 31 39 20 64      ** than 19 d
b4ac0 69 67 69 74 73 20 28 74 68 75 73 20 67 75 61 72  igits (thus guar
b4ad0 61 6e 74 69 6e 67 20 74 68 61 74 20 69 74 20 69  anting that it i
b4ae0 73 20 74 6f 6f 20 6c 61 72 67 65 29 20 2a 2f 0a  s too large) */.
b4af0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
b4b00 7d 65 6c 73 65 20 69 66 28 20 69 3c 31 39 20 29  }else if( i<19 )
b4b10 7b 0a 20 20 20 20 2f 2a 20 4c 65 73 73 20 74 68  {.    /* Less th
b4b20 61 6e 20 31 39 20 64 69 67 69 74 73 2c 20 73 6f  an 19 digits, so
b4b30 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 69 74   we know that it
b4b40 20 66 69 74 73 20 69 6e 20 36 34 20 62 69 74 73   fits in 64 bits
b4b50 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31   */.    return 1
b4b60 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
b4b70 2a 20 31 39 2d 64 69 67 69 74 20 6e 75 6d 62 65  * 19-digit numbe
b4b80 72 73 20 6d 75 73 74 20 62 65 20 6e 6f 20 6c 61  rs must be no la
b4b90 72 67 65 72 20 74 68 61 6e 20 39 32 32 33 33 37  rger than 922337
b4ba0 32 30 33 36 38 35 34 37 37 35 38 30 37 20 69 66  2036854775807 if
b4bb0 20 70 6f 73 69 74 69 76 65 0a 20 20 20 20 2a 2a   positive.    **
b4bc0 20 6f 72 20 39 32 32 33 33 37 32 30 33 36 38 35   or 922337203685
b4bd0 34 37 37 35 38 30 38 20 69 66 20 6e 65 67 61 74  4775808 if negat
b4be0 69 76 65 2e 20 20 4e 6f 74 65 20 74 68 61 74 20  ive.  Note that 
b4bf0 39 32 32 33 33 37 32 30 33 36 38 35 34 36 36 35  9223372036854665
b4c00 38 30 38 0a 20 20 20 20 2a 2a 20 69 73 20 32 5e  808.    ** is 2^
b4c10 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  63. */.    retur
b4c20 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28  n compare2pow63(
b4c30 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d  zNum)<neg;.  }.}
b4c40 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69  ../*.** The stri
b4c50 6e 67 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e  ng zNum represen
b4c60 74 73 20 61 6e 20 75 6e 73 69 67 6e 65 64 20 69  ts an unsigned i
b4c70 6e 74 65 67 65 72 2e 20 20 54 68 65 20 7a 4e 75  nteger.  The zNu
b4c80 6d 20 73 74 72 69 6e 67 0a 2a 2a 20 63 6f 6e 73  m string.** cons
b4c90 69 73 74 73 20 6f 66 20 6f 6e 65 20 6f 72 20 6d  ists of one or m
b4ca0 6f 72 65 20 64 69 67 69 74 20 63 68 61 72 61 63  ore digit charac
b4cb0 74 65 72 73 20 61 6e 64 20 69 73 20 74 65 72 6d  ters and is term
b4cc0 69 6e 61 74 65 64 20 62 79 0a 2a 2a 20 61 20 7a  inated by.** a z
b4cd0 65 72 6f 20 63 68 61 72 61 63 74 65 72 2e 20 20  ero character.  
b4ce0 41 6e 79 20 73 74 72 61 79 20 63 68 61 72 61 63  Any stray charac
b4cf0 74 65 72 73 20 69 6e 20 7a 4e 75 6d 20 72 65 73  ters in zNum res
b4d00 75 6c 74 20 69 6e 20 75 6e 64 65 66 69 6e 65 64  ult in undefined
b4d10 0a 2a 2a 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a  .** behavior..**
b4d20 0a 2a 2a 20 49 66 20 74 68 65 20 75 6e 73 69 67  .** If the unsig
b4d30 6e 65 64 20 69 6e 74 65 67 65 72 20 74 68 61 74  ned integer that
b4d40 20 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 73   zNum represents
b4d50 20 77 69 6c 6c 20 66 69 74 20 69 6e 20 61 0a 2a   will fit in a.*
b4d60 2a 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20  * 64-bit signed 
b4d70 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20  integer, return 
b4d80 54 52 55 45 2e 20 20 4f 74 68 65 72 77 69 73 65  TRUE.  Otherwise
b4d90 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a 2a   return FALSE..*
b4da0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 65 67 46  *.** If the negF
b4db0 6c 61 67 20 70 61 72 61 6d 65 74 65 72 20 69 73  lag parameter is
b4dc0 20 74 72 75 65 2c 20 74 68 61 74 20 6d 65 61 6e   true, that mean
b4dd0 73 20 74 68 61 74 20 7a 4e 75 6d 20 72 65 61 6c  s that zNum real
b4de0 6c 79 20 72 65 70 72 65 73 65 6e 74 73 0a 2a 2a  ly represents.**
b4df0 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62   a negative numb
b4e00 65 72 2e 20 20 28 54 68 65 20 6c 65 61 64 69 6e  er.  (The leadin
b4e10 67 20 22 2d 22 20 69 73 20 6f 6d 69 74 74 65 64  g "-" is omitted
b4e20 20 66 72 6f 6d 20 7a 4e 75 6d 2e 29 20 20 54 68   from zNum.)  Th
b4e30 69 73 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  is.** parameter 
b4e40 69 73 20 6e 65 65 64 65 64 20 74 6f 20 64 65 74  is needed to det
b4e50 65 72 6d 69 6e 65 20 61 20 62 6f 75 6e 64 61 72  ermine a boundar
b4e60 79 20 63 61 73 65 2e 20 20 41 20 73 74 72 69 6e  y case.  A strin
b4e70 67 0a 2a 2a 20 6f 66 20 22 39 32 32 33 33 37 33  g.** of "9223373
b4e80 30 33 36 38 35 34 37 37 35 38 30 38 22 20 72 65  036854775808" re
b4e90 74 75 72 6e 73 20 66 61 6c 73 65 20 69 66 20 6e  turns false if n
b4ea0 65 67 46 6c 61 67 20 69 73 20 66 61 6c 73 65 20  egFlag is false 
b4eb0 6f 72 20 74 72 75 65 0a 2a 2a 20 69 66 20 6e 65  or true.** if ne
b4ec0 67 46 6c 61 67 20 69 73 20 74 72 75 65 2e 0a 2a  gFlag is true..*
b4ed0 2a 0a 2a 2a 20 4c 65 61 64 69 6e 67 20 7a 65 72  *.** Leading zer
b4ee0 6f 73 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a  os are ignored..
b4ef0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b4f00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 74  E int sqlite3Fit
b4f10 73 49 6e 36 34 42 69 74 73 28 63 6f 6e 73 74 20  sIn64Bits(const 
b4f20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20  char *zNum, int 
b4f30 6e 65 67 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20  negFlag){.  int 
b4f40 69 3b 0a 20 20 69 6e 74 20 6e 65 67 20 3d 20 30  i;.  int neg = 0
b4f50 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 7a 4e 75  ;..  assert( zNu
b4f60 6d 5b 30 5d 3e 3d 27 30 27 20 26 26 20 7a 4e 75  m[0]>='0' && zNu
b4f70 6d 5b 30 5d 3c 3d 27 39 27 20 29 3b 20 2f 2a 20  m[0]<='9' ); /* 
b4f80 7a 4e 75 6d 20 69 73 20 61 6e 20 75 6e 73 69 67  zNum is an unsig
b4f90 6e 65 64 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20  ned number */.. 
b4fa0 20 69 66 28 20 6e 65 67 46 6c 61 67 20 29 20 6e   if( negFlag ) n
b4fb0 65 67 20 3d 20 31 2d 6e 65 67 3b 0a 20 20 77 68  eg = 1-neg;.  wh
b4fc0 69 6c 65 28 20 2a 7a 4e 75 6d 3d 3d 27 30 27 20  ile( *zNum=='0' 
b4fd0 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 20 20  ){.    zNum++;  
b4fe0 20 2f 2a 20 53 6b 69 70 20 6c 65 61 64 69 6e 67   /* Skip leading
b4ff0 20 7a 65 72 6f 73 2e 20 20 54 69 63 6b 65 74 20   zeros.  Ticket 
b5000 23 32 34 35 34 20 2a 2f 0a 20 20 7d 0a 20 20 66  #2454 */.  }.  f
b5010 6f 72 28 69 3d 30 3b 20 7a 4e 75 6d 5b 69 5d 3b  or(i=0; zNum[i];
b5020 20 69 2b 2b 29 7b 20 61 73 73 65 72 74 28 20 7a   i++){ assert( z
b5030 4e 75 6d 5b 69 5d 3e 3d 27 30 27 20 26 26 20 7a  Num[i]>='0' && z
b5040 4e 75 6d 5b 69 5d 3c 3d 27 39 27 20 29 3b 20 7d  Num[i]<='9' ); }
b5050 0a 20 20 69 66 28 20 69 3c 31 39 20 29 7b 0a 20  .  if( i<19 ){. 
b5060 20 20 20 2f 2a 20 47 75 61 72 61 6e 74 65 65 64     /* Guaranteed
b5070 20 74 6f 20 66 69 74 20 69 66 20 6c 65 73 73 20   to fit if less 
b5080 74 68 61 6e 20 31 39 20 64 69 67 69 74 73 20 2a  than 19 digits *
b5090 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  /.    return 1;.
b50a0 20 20 7d 65 6c 73 65 20 69 66 28 20 69 3e 31 39    }else if( i>19
b50b0 20 29 7b 0a 20 20 20 20 2f 2a 20 47 75 61 72 61   ){.    /* Guara
b50c0 6e 74 65 65 64 20 74 6f 20 62 65 20 74 6f 6f 20  nteed to be too 
b50d0 62 69 67 20 69 66 20 67 72 65 61 74 65 72 20 74  big if greater t
b50e0 68 61 6e 20 31 39 20 64 69 67 69 74 73 20 2a 2f  han 19 digits */
b50f0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
b5100 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43   }else{.    /* C
b5110 6f 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20 32  ompare against 2
b5120 5e 36 33 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75  ^63. */.    retu
b5130 72 6e 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33  rn compare2pow63
b5140 28 7a 4e 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a  (zNum)<neg;.  }.
b5150 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 7a 4e 75 6d  }../*.** If zNum
b5160 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20 69   represents an i
b5170 6e 74 65 67 65 72 20 74 68 61 74 20 77 69 6c 6c  nteger that will
b5180 20 66 69 74 20 69 6e 20 33 32 2d 62 69 74 73 2c   fit in 32-bits,
b5190 20 74 68 65 6e 20 73 65 74 0a 2a 2a 20 2a 70 56   then set.** *pV
b51a0 61 6c 75 65 20 74 6f 20 74 68 61 74 20 69 6e 74  alue to that int
b51b0 65 67 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20  eger and return 
b51c0 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69 73 65  true.  Otherwise
b51d0 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a   return false..*
b51e0 2a 0a 2a 2a 20 41 6e 79 20 6e 6f 6e 2d 6e 75 6d  *.** Any non-num
b51f0 65 72 69 63 20 63 68 61 72 61 63 74 65 72 73 20  eric characters 
b5200 74 68 61 74 20 66 6f 6c 6c 6f 77 69 6e 67 20 7a  that following z
b5210 4e 75 6d 20 61 72 65 20 69 67 6e 6f 72 65 64 2e  Num are ignored.
b5220 0a 2a 2a 20 54 68 69 73 20 69 73 20 64 69 66 66  .** This is diff
b5230 65 72 65 6e 74 20 66 72 6f 6d 20 73 71 6c 69 74  erent from sqlit
b5240 65 33 41 74 6f 69 36 34 28 29 20 77 68 69 63 68  e3Atoi64() which
b5250 20 72 65 71 75 69 72 65 73 20 74 68 65 0a 2a 2a   requires the.**
b5260 20 69 6e 70 75 74 20 6e 75 6d 62 65 72 20 74 6f   input number to
b5270 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61   be zero-termina
b5280 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
b5290 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
b52a0 65 33 47 65 74 49 6e 74 33 32 28 63 6f 6e 73 74  e3GetInt32(const
b52b0 20 63 68 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74   char *zNum, int
b52c0 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 73 71 6c   *pValue){.  sql
b52d0 69 74 65 5f 69 6e 74 36 34 20 76 20 3d 20 30 3b  ite_int64 v = 0;
b52e0 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 69  .  int i, c;.  i
b52f0 6e 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20 69 66  nt neg = 0;.  if
b5300 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2d 27 20 29  ( zNum[0]=='-' )
b5310 7b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20  {.    neg = 1;. 
b5320 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c     zNum++;.  }el
b5330 73 65 20 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d  se if( zNum[0]==
b5340 27 2b 27 20 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b  '+' ){.    zNum+
b5350 2b 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20  +;.  }.  while( 
b5360 7a 4e 75 6d 5b 30 5d 3d 3d 27 30 27 20 29 20 7a  zNum[0]=='0' ) z
b5370 4e 75 6d 2b 2b 3b 0a 20 20 66 6f 72 28 69 3d 30  Num++;.  for(i=0
b5380 3b 20 69 3c 31 31 20 26 26 20 28 63 20 3d 20 7a  ; i<11 && (c = z
b5390 4e 75 6d 5b 69 5d 20 2d 20 27 30 27 29 3e 3d 30  Num[i] - '0')>=0
b53a0 20 26 26 20 63 3c 3d 39 3b 20 69 2b 2b 29 7b 0a   && c<=9; i++){.
b53b0 20 20 20 20 76 20 3d 20 76 2a 31 30 20 2b 20 63      v = v*10 + c
b53c0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  ;.  }..  /* The 
b53d0 6c 6f 6e 67 65 73 74 20 64 65 63 69 6d 61 6c 20  longest decimal 
b53e0 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f  representation o
b53f0 66 20 61 20 33 32 20 62 69 74 20 69 6e 74 65 67  f a 32 bit integ
b5400 65 72 20 69 73 20 31 30 20 64 69 67 69 74 73 3a  er is 10 digits:
b5410 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20  .  **.  **      
b5420 20 20 20 20 20 20 20 31 32 33 34 35 36 37 38 39         123456789
b5430 30 0a 20 20 2a 2a 20 20 20 20 20 32 5e 33 31 20  0.  **     2^31 
b5440 2d 3e 20 32 31 34 37 34 38 33 36 34 38 0a 20 20  -> 2147483648.  
b5450 2a 2f 0a 20 20 69 66 28 20 69 3e 31 30 20 29 7b  */.  if( i>10 ){
b5460 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
b5470 20 7d 0a 20 20 69 66 28 20 76 2d 6e 65 67 3e 32   }.  if( v-neg>2
b5480 31 34 37 34 38 33 36 34 37 20 29 7b 0a 20 20 20  147483647 ){.   
b5490 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
b54a0 20 69 66 28 20 6e 65 67 20 29 7b 0a 20 20 20 20   if( neg ){.    
b54b0 76 20 3d 20 2d 76 3b 0a 20 20 7d 0a 20 20 2a 70  v = -v;.  }.  *p
b54c0 56 61 6c 75 65 20 3d 20 28 69 6e 74 29 76 3b 0a  Value = (int)v;.
b54d0 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f    return 1;.}../
b54e0 2a 0a 2a 2a 20 54 68 65 20 76 61 72 69 61 62 6c  *.** The variabl
b54f0 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
b5500 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 61 73 20   encoding is as 
b5510 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b  follows:.**.** K
b5520 45 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 41  EY:.**         A
b5530 20 3d 20 30 78 78 78 78 78 78 78 20 20 20 20 37   = 0xxxxxxx    7
b5540 20 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e   bits of data an
b5550 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a  d one flag bit.*
b5560 2a 20 20 20 20 20 20 20 20 20 42 20 3d 20 31 78  *         B = 1x
b5570 78 78 78 78 78 78 20 20 20 20 37 20 62 69 74 73  xxxxxx    7 bits
b5580 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65   of data and one
b5590 20 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20 20 20   flag bit.**    
b55a0 20 20 20 20 20 43 20 3d 20 78 78 78 78 78 78 78       C = xxxxxxx
b55b0 78 20 20 20 20 38 20 62 69 74 73 20 6f 66 20 64  x    8 bits of d
b55c0 61 74 61 0a 2a 2a 0a 2a 2a 20 20 37 20 62 69 74  ata.**.**  7 bit
b55d0 73 20 2d 20 41 0a 2a 2a 20 31 34 20 62 69 74 73  s - A.** 14 bits
b55e0 20 2d 20 42 41 0a 2a 2a 20 32 31 20 62 69 74 73   - BA.** 21 bits
b55f0 20 2d 20 42 42 41 0a 2a 2a 20 32 38 20 62 69 74   - BBA.** 28 bit
b5600 73 20 2d 20 42 42 42 41 0a 2a 2a 20 33 35 20 62  s - BBBA.** 35 b
b5610 69 74 73 20 2d 20 42 42 42 42 41 0a 2a 2a 20 34  its - BBBBA.** 4
b5620 32 20 62 69 74 73 20 2d 20 42 42 42 42 42 41 0a  2 bits - BBBBBA.
b5630 2a 2a 20 34 39 20 62 69 74 73 20 2d 20 42 42 42  ** 49 bits - BBB
b5640 42 42 42 41 0a 2a 2a 20 35 36 20 62 69 74 73 20  BBBA.** 56 bits 
b5650 2d 20 42 42 42 42 42 42 42 41 0a 2a 2a 20 36 34  - BBBBBBBA.** 64
b5660 20 62 69 74 73 20 2d 20 42 42 42 42 42 42 42 42   bits - BBBBBBBB
b5670 43 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74  C.*/../*.** Writ
b5680 65 20 61 20 36 34 2d 62 69 74 20 76 61 72 69 61  e a 64-bit varia
b5690 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67  ble-length integ
b56a0 65 72 20 74 6f 20 6d 65 6d 6f 72 79 20 73 74 61  er to memory sta
b56b0 72 74 69 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a  rting at p[0]..*
b56c0 2a 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20  * The length of 
b56d0 64 61 74 61 20 77 72 69 74 65 20 77 69 6c 6c 20  data write will 
b56e0 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64  be between 1 and
b56f0 20 39 20 62 79 74 65 73 2e 20 20 54 68 65 20 6e   9 bytes.  The n
b5700 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65  umber.** of byte
b5710 73 20 77 72 69 74 74 65 6e 20 69 73 20 72 65 74  s written is ret
b5720 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 76  urned..**.** A v
b5730 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69  ariable-length i
b5740 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73 20  nteger consists 
b5750 6f 66 20 74 68 65 20 6c 6f 77 65 72 20 37 20 62  of the lower 7 b
b5760 69 74 73 20 6f 66 20 65 61 63 68 20 62 79 74 65  its of each byte
b5770 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20 62 79 74 65  .** for all byte
b5780 73 20 74 68 61 74 20 68 61 76 65 20 74 68 65 20  s that have the 
b5790 38 74 68 20 62 69 74 20 73 65 74 20 61 6e 64 20  8th bit set and 
b57a0 6f 6e 65 20 62 79 74 65 20 77 69 74 68 20 74 68  one byte with th
b57b0 65 20 38 74 68 0a 2a 2a 20 62 69 74 20 63 6c 65  e 8th.** bit cle
b57c0 61 72 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20  ar.  Except, if 
b57d0 77 65 20 67 65 74 20 74 6f 20 74 68 65 20 39 74  we get to the 9t
b57e0 68 20 62 79 74 65 2c 20 69 74 20 73 74 6f 72 65  h byte, it store
b57f0 73 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 38 20  s the full.** 8 
b5800 62 69 74 73 20 61 6e 64 20 69 73 20 74 68 65 20  bits and is the 
b5810 6c 61 73 74 20 62 79 74 65 2e 0a 2a 2f 0a 53 51  last byte..*/.SQ
b5820 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
b5830 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e   sqlite3PutVarin
b5840 74 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t(unsigned char 
b5850 2a 70 2c 20 75 36 34 20 76 29 7b 0a 20 20 69 6e  *p, u64 v){.  in
b5860 74 20 69 2c 20 6a 2c 20 6e 3b 0a 20 20 75 38 20  t i, j, n;.  u8 
b5870 62 75 66 5b 31 30 5d 3b 0a 20 20 69 66 28 20 76  buf[10];.  if( v
b5880 20 26 20 28 28 28 75 36 34 29 30 78 66 66 30 30   & (((u64)0xff00
b5890 30 30 30 30 29 3c 3c 33 32 29 20 29 7b 0a 20 20  0000)<<32) ){.  
b58a0 20 20 70 5b 38 5d 20 3d 20 28 75 38 29 76 3b 0a    p[8] = (u8)v;.
b58b0 20 20 20 20 76 20 3e 3e 3d 20 38 3b 0a 20 20 20      v >>= 8;.   
b58c0 20 66 6f 72 28 69 3d 37 3b 20 69 3e 3d 30 3b 20   for(i=7; i>=0; 
b58d0 69 2d 2d 29 7b 0a 20 20 20 20 20 20 70 5b 69 5d  i--){.      p[i]
b58e0 20 3d 20 28 75 38 29 28 28 76 20 26 20 30 78 37   = (u8)((v & 0x7
b58f0 66 29 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20  f) | 0x80);.    
b5900 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 20 20 7d    v >>= 7;.    }
b5910 0a 20 20 20 20 72 65 74 75 72 6e 20 39 3b 0a 20  .    return 9;. 
b5920 20 7d 20 20 20 20 0a 20 20 6e 20 3d 20 30 3b 0a   }    .  n = 0;.
b5930 20 20 64 6f 7b 0a 20 20 20 20 62 75 66 5b 6e 2b    do{.    buf[n+
b5940 2b 5d 20 3d 20 28 75 38 29 28 28 76 20 26 20 30  +] = (u8)((v & 0
b5950 78 37 66 29 20 7c 20 30 78 38 30 29 3b 0a 20 20  x7f) | 0x80);.  
b5960 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68    v >>= 7;.  }wh
b5970 69 6c 65 28 20 76 21 3d 30 20 29 3b 0a 20 20 62  ile( v!=0 );.  b
b5980 75 66 5b 30 5d 20 26 3d 20 30 78 37 66 3b 0a 20  uf[0] &= 0x7f;. 
b5990 20 61 73 73 65 72 74 28 20 6e 3c 3d 39 20 29 3b   assert( n<=9 );
b59a0 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 6e 2d  .  for(i=0, j=n-
b59b0 31 3b 20 6a 3e 3d 30 3b 20 6a 2d 2d 2c 20 69 2b  1; j>=0; j--, i+
b59c0 2b 29 7b 0a 20 20 20 20 70 5b 69 5d 20 3d 20 62  +){.    p[i] = b
b59d0 75 66 5b 6a 5d 3b 0a 20 20 7d 0a 20 20 72 65 74  uf[j];.  }.  ret
b59e0 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn n;.}../*.** 
b59f0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
b5a00 61 20 66 61 73 74 65 72 20 76 65 72 73 69 6f 6e  a faster version
b5a10 20 6f 66 20 73 71 6c 69 74 65 33 50 75 74 56 61   of sqlite3PutVa
b5a20 72 69 6e 74 28 29 20 74 68 61 74 20 6f 6e 6c 79  rint() that only
b5a30 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 33 32  .** works for 32
b5a40 2d 62 69 74 20 70 6f 73 69 74 69 76 65 20 69 6e  -bit positive in
b5a50 74 65 67 65 72 73 20 61 6e 64 20 77 68 69 63 68  tegers and which
b5a60 20 69 73 20 6f 70 74 69 6d 69 7a 65 64 20 66 6f   is optimized fo
b5a70 72 0a 2a 2a 20 74 68 65 20 63 6f 6d 6d 6f 6e 20  r.** the common 
b5a80 63 61 73 65 20 6f 66 20 73 6d 61 6c 6c 20 69 6e  case of small in
b5a90 74 65 67 65 72 73 2e 20 20 41 20 4d 41 43 52 4f  tegers.  A MACRO
b5aa0 20 76 65 72 73 69 6f 6e 2c 20 70 75 74 56 61 72   version, putVar
b5ab0 69 6e 74 33 32 2c 0a 2a 2a 20 69 73 20 70 72 6f  int32,.** is pro
b5ac0 76 69 64 65 64 20 77 68 69 63 68 20 69 6e 6c 69  vided which inli
b5ad0 6e 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62  nes the single-b
b5ae0 79 74 65 20 63 61 73 65 2e 20 20 41 6c 6c 20 63  yte case.  All c
b5af0 6f 64 65 20 73 68 6f 75 6c 64 20 75 73 65 0a 2a  ode should use.*
b5b00 2a 20 74 68 65 20 4d 41 43 52 4f 20 76 65 72 73  * the MACRO vers
b5b10 69 6f 6e 20 61 73 20 74 68 69 73 20 66 75 6e 63  ion as this func
b5b20 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65  tion assumes the
b5b30 20 73 69 6e 67 6c 65 2d 62 79 74 65 20 63 61 73   single-byte cas
b5b40 65 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79  e has.** already
b5b50 20 62 65 65 6e 20 68 61 6e 64 6c 65 64 2e 0a 2a   been handled..*
b5b60 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b5b70 20 69 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56   int sqlite3PutV
b5b80 61 72 69 6e 74 33 32 28 75 6e 73 69 67 6e 65 64  arint32(unsigned
b5b90 20 63 68 61 72 20 2a 70 2c 20 75 33 32 20 76 29   char *p, u32 v)
b5ba0 7b 0a 23 69 66 6e 64 65 66 20 70 75 74 56 61 72  {.#ifndef putVar
b5bb0 69 6e 74 33 32 0a 20 20 69 66 28 20 28 76 20 26  int32.  if( (v &
b5bc0 20 7e 30 78 37 66 29 3d 3d 30 20 29 7b 0a 20 20   ~0x7f)==0 ){.  
b5bd0 20 20 70 5b 30 5d 20 3d 20 76 3b 0a 20 20 20 20    p[0] = v;.    
b5be0 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65  return 1;.  }.#e
b5bf0 6e 64 69 66 0a 20 20 69 66 28 20 28 76 20 26 20  ndif.  if( (v & 
b5c00 7e 30 78 33 66 66 66 29 3d 3d 30 20 29 7b 0a 20  ~0x3fff)==0 ){. 
b5c10 20 20 20 70 5b 30 5d 20 3d 20 28 75 38 29 28 28     p[0] = (u8)((
b5c20 76 3e 3e 37 29 20 7c 20 30 78 38 30 29 3b 0a 20  v>>7) | 0x80);. 
b5c30 20 20 20 70 5b 31 5d 20 3d 20 28 75 38 29 28 76     p[1] = (u8)(v
b5c40 20 26 20 30 78 37 66 29 3b 0a 20 20 20 20 72 65   & 0x7f);.    re
b5c50 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 20 20 72 65  turn 2;.  }.  re
b5c60 74 75 72 6e 20 73 71 6c 69 74 65 33 50 75 74 56  turn sqlite3PutV
b5c70 61 72 69 6e 74 28 70 2c 20 76 29 3b 0a 7d 0a 0a  arint(p, v);.}..
b5c80 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 36 34 2d  /*.** Read a 64-
b5c90 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e  bit variable-len
b5ca0 67 74 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d  gth integer from
b5cb0 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67   memory starting
b5cc0 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74   at p[0]..** Ret
b5cd0 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
b5ce0 66 20 62 79 74 65 73 20 72 65 61 64 2e 20 20 54  f bytes read.  T
b5cf0 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72  he value is stor
b5d00 65 64 20 69 6e 20 2a 76 2e 0a 2a 2f 0a 53 51 4c  ed in *v..*/.SQL
b5d10 49 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73  ITE_PRIVATE u8 s
b5d20 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 28  qlite3GetVarint(
b5d30 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
b5d40 68 61 72 20 2a 70 2c 20 75 36 34 20 2a 76 29 7b  har *p, u64 *v){
b5d50 0a 20 20 75 33 32 20 61 2c 62 2c 73 3b 0a 0a 20  .  u32 a,b,s;.. 
b5d60 20 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a   a = *p;.  /* a:
b5d70 20 70 30 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a   p0 (unmasked) *
b5d80 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38 30  /.  if (!(a&0x80
b5d90 29 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20 3d 20  )).  {.    *v = 
b5da0 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  a;.    return 1;
b5db0 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20 20 62  .  }..  p++;.  b
b5dc0 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70   = *p;.  /* b: p
b5dd0 31 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  1 (unmasked) */.
b5de0 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29    if (!(b&0x80))
b5df0 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 30 78  .  {.    a &= 0x
b5e00 37 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37  7f;.    a = a<<7
b5e10 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20  ;.    a |= b;.  
b5e20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72 65    *v = a;.    re
b5e30 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 70  turn 2;.  }..  p
b5e40 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b  ++;.  a = a<<14;
b5e50 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a  .  a |= *p;.  /*
b5e60 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70 32 20   a: p0<<14 | p2 
b5e70 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20  (unmasked) */.  
b5e80 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20  if (!(a&0x80)). 
b5e90 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37   {.    a &= (0x7
b5ea0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20  f<<14)|(0x7f);. 
b5eb0 20 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20     b &= 0x7f;.  
b5ec0 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20    b = b<<7;.    
b5ed0 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76 20 3d  a |= b;.    *v =
b5ee0 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 33   a;.    return 3
b5ef0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 53 45 31  ;.  }..  /* CSE1
b5f00 20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20   from below */. 
b5f10 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29   a &= (0x7f<<14)
b5f20 7c 28 30 78 37 66 29 3b 0a 20 20 70 2b 2b 3b 0a  |(0x7f);.  p++;.
b5f30 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62    b = b<<14;.  b
b5f40 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20   |= *p;.  /* b: 
b5f50 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75 6e 6d  p1<<14 | p3 (unm
b5f60 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28  asked) */.  if (
b5f70 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20  !(b&0x80)).  {. 
b5f80 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31     b &= (0x7f<<1
b5f90 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 2f  4)|(0x7f);.    /
b5fa0 2a 20 6d 6f 76 65 64 20 43 53 45 31 20 75 70 20  * moved CSE1 up 
b5fb0 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28  */.    /* a &= (
b5fc0 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29  0x7f<<14)|(0x7f)
b5fd0 3b 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 3c 3c  ; */.    a = a<<
b5fe0 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20  7;.    a |= b;. 
b5ff0 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20 72     *v = a;.    r
b6000 65 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20  eturn 4;.  }..  
b6010 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70  /* a: p0<<14 | p
b6020 32 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20  2 (masked) */.  
b6030 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c 20 70  /* b: p1<<14 | p
b6040 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  3 (unmasked) */.
b6050 20 20 2f 2a 20 31 3a 73 61 76 65 20 6f 66 66 20    /* 1:save off 
b6060 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20  p0<<21 | p1<<14 
b6070 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61  | p2<<7 | p3 (ma
b6080 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20 6d 6f  sked) */.  /* mo
b6090 76 65 64 20 43 53 45 31 20 75 70 20 2a 2f 0a 20  ved CSE1 up */. 
b60a0 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c   /* a &= (0x7f<<
b60b0 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f 0a 20  14)|(0x7f); */. 
b60c0 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29   b &= (0x7f<<14)
b60d0 7c 28 30 78 37 66 29 3b 0a 20 20 73 20 3d 20 61  |(0x7f);.  s = a
b60e0 3b 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c 31 34  ;.  /* s: p0<<14
b60f0 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 29 20 2a   | p2 (masked) *
b6100 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20  /..  p++;.  a = 
b6110 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70  a<<14;.  a |= *p
b6120 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 32 38  ;.  /* a: p0<<28
b6130 20 7c 20 70 32 3c 3c 31 34 20 7c 20 70 34 20 28   | p2<<14 | p4 (
b6140 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69  unmasked) */.  i
b6150 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20 20  f (!(a&0x80)).  
b6160 7b 0a 20 20 20 20 2f 2a 20 77 65 20 63 61 6e 20  {.    /* we can 
b6170 73 6b 69 70 20 74 68 65 73 65 20 63 61 75 73 65  skip these cause
b6180 20 74 68 65 79 20 77 65 72 65 20 28 65 66 66 65   they were (effe
b6190 63 74 69 76 65 6c 79 29 20 64 6f 6e 65 20 61 62  ctively) done ab
b61a0 6f 76 65 20 69 6e 20 63 61 6c 63 27 69 6e 67 20  ove in calc'ing 
b61b0 73 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d  s */.    /* a &=
b61c0 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 78 37   (0x7f<<28)|(0x7
b61d0 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a  f<<14)|(0x7f); *
b61e0 2f 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20 28 30  /.    /* b &= (0
b61f0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
b6200 20 2a 2f 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37   */.    b = b<<7
b6210 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20  ;.    a |= b;.  
b6220 20 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20 20    s = s>>18;.   
b6230 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c   *v = ((u64)s)<<
b6240 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75  32 | a;.    retu
b6250 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rn 5;.  }..  /* 
b6260 32 3a 73 61 76 65 20 6f 66 66 20 70 30 3c 3c 32  2:save off p0<<2
b6270 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70 32 3c  1 | p1<<14 | p2<
b6280 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65 64 29  <7 | p3 (masked)
b6290 20 2a 2f 0a 20 20 73 20 3d 20 73 3c 3c 37 3b 0a   */.  s = s<<7;.
b62a0 20 20 73 20 7c 3d 20 62 3b 0a 20 20 2f 2a 20 73    s |= b;.  /* s
b62b0 3a 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31  : p0<<21 | p1<<1
b62c0 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28  4 | p2<<7 | p3 (
b62d0 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20 70 2b  masked) */..  p+
b62e0 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a  +;.  b = b<<14;.
b62f0 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20    b |= *p;.  /* 
b6300 62 3a 20 70 31 3c 3c 32 38 20 7c 20 70 33 3c 3c  b: p1<<28 | p3<<
b6310 31 34 20 7c 20 70 35 20 28 75 6e 6d 61 73 6b 65  14 | p5 (unmaske
b6320 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26  d) */.  if (!(b&
b6330 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f  0x80)).  {.    /
b6340 2a 20 77 65 20 63 61 6e 20 73 6b 69 70 20 74 68  * we can skip th
b6350 69 73 20 63 61 75 73 65 20 69 74 20 77 61 73 20  is cause it was 
b6360 28 65 66 66 65 63 74 69 76 65 6c 79 29 20 64 6f  (effectively) do
b6370 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61 6c 63  ne above in calc
b6380 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20 2f 2a  'ing s */.    /*
b6390 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 29   b &= (0x7f<<28)
b63a0 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37  |(0x7f<<14)|(0x7
b63b0 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20  f); */.    a &= 
b63c0 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66  (0x7f<<14)|(0x7f
b63d0 29 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b  );.    a = a<<7;
b63e0 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20  .    a |= b;.   
b63f0 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20 20 20   s = s>>18;.    
b6400 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c 33  *v = ((u64)s)<<3
b6410 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75 72  2 | a;.    retur
b6420 6e 20 36 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b  n 6;.  }..  p++;
b6430 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20  .  a = a<<14;.  
b6440 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a  a |= *p;.  /* a:
b6450 20 70 32 3c 3c 32 38 20 7c 20 70 34 3c 3c 31 34   p2<<28 | p4<<14
b6460 20 7c 20 70 36 20 28 75 6e 6d 61 73 6b 65 64 29   | p6 (unmasked)
b6470 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78   */.  if (!(a&0x
b6480 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26  80)).  {.    a &
b6490 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28 30 78  = (0x1f<<28)|(0x
b64a0 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
b64b0 20 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c      b &= (0x7f<<
b64c0 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20  14)|(0x7f);.    
b64d0 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 61 20  b = b<<7;.    a 
b64e0 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20 73 3e  |= b;.    s = s>
b64f0 3e 31 31 3b 0a 20 20 20 20 2a 76 20 3d 20 28 28  >11;.    *v = ((
b6500 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a  u64)s)<<32 | a;.
b6510 20 20 20 20 72 65 74 75 72 6e 20 37 3b 0a 20 20      return 7;.  
b6520 7d 0a 0a 20 20 2f 2a 20 43 53 45 32 20 66 72 6f  }..  /* CSE2 fro
b6530 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 61 20 26  m below */.  a &
b6540 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  = (0x7f<<14)|(0x
b6550 37 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20 20 62 20  7f);.  p++;.  b 
b6560 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20  = b<<14;.  b |= 
b6570 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 33 3c 3c  *p;.  /* b: p3<<
b6580 32 38 20 7c 20 70 35 3c 3c 31 34 20 7c 20 70 37  28 | p5<<14 | p7
b6590 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20   (unmasked) */. 
b65a0 20 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a   if (!(b&0x80)).
b65b0 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28 30 78    {.    b &= (0x
b65c0 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31  1f<<28)|(0x7f<<1
b65d0 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 2f  4)|(0x7f);.    /
b65e0 2a 20 6d 6f 76 65 64 20 43 53 45 32 20 75 70 20  * moved CSE2 up 
b65f0 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d 20 28  */.    /* a &= (
b6600 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29  0x7f<<14)|(0x7f)
b6610 3b 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61 3c 3c  ; */.    a = a<<
b6620 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20  7;.    a |= b;. 
b6630 20 20 20 73 20 3d 20 73 3e 3e 34 3b 0a 20 20 20     s = s>>4;.   
b6640 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c 3c   *v = ((u64)s)<<
b6650 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74 75  32 | a;.    retu
b6660 72 6e 20 38 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b  rn 8;.  }..  p++
b6670 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 35 3b 0a 20  ;.  a = a<<15;. 
b6680 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61   a |= *p;.  /* a
b6690 3a 20 70 34 3c 3c 32 39 20 7c 20 70 36 3c 3c 31  : p4<<29 | p6<<1
b66a0 35 20 7c 20 70 38 20 28 75 6e 6d 61 73 6b 65 64  5 | p8 (unmasked
b66b0 29 20 2a 2f 0a 0a 20 20 2f 2a 20 6d 6f 76 65 64  ) */..  /* moved
b66c0 20 43 53 45 32 20 75 70 20 2a 2f 0a 20 20 2f 2a   CSE2 up */.  /*
b66d0 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32 39 29   a &= (0x7f<<29)
b66e0 7c 28 30 78 37 66 3c 3c 31 35 29 7c 28 30 78 66  |(0x7f<<15)|(0xf
b66f0 66 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 20 28 30  f); */.  b &= (0
b6700 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
b6710 0a 20 20 62 20 3d 20 62 3c 3c 38 3b 0a 20 20 61  .  b = b<<8;.  a
b6720 20 7c 3d 20 62 3b 0a 0a 20 20 73 20 3d 20 73 3c   |= b;..  s = s<
b6730 3c 34 3b 0a 20 20 62 20 3d 20 70 5b 2d 34 5d 3b  <4;.  b = p[-4];
b6740 0a 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20  .  b &= 0x7f;.  
b6750 62 20 3d 20 62 3e 3e 33 3b 0a 20 20 73 20 7c 3d  b = b>>3;.  s |=
b6760 20 62 3b 0a 0a 20 20 2a 76 20 3d 20 28 28 75 36   b;..  *v = ((u6
b6770 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a 0a 20  4)s)<<32 | a;.. 
b6780 20 72 65 74 75 72 6e 20 39 3b 0a 7d 0a 0a 2f 2a   return 9;.}../*
b6790 0a 2a 2a 20 52 65 61 64 20 61 20 33 32 2d 62 69  .** Read a 32-bi
b67a0 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  t variable-lengt
b67b0 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d  h integer from m
b67c0 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61  emory starting a
b67d0 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74 75 72  t p[0]..** Retur
b67e0 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
b67f0 62 79 74 65 73 20 72 65 61 64 2e 20 20 54 68 65  bytes read.  The
b6800 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64   value is stored
b6810 20 69 6e 20 2a 76 2e 0a 2a 2a 0a 2a 2a 20 49 66   in *v..**.** If
b6820 20 74 68 65 20 76 61 72 69 6e 74 20 73 74 6f 72   the varint stor
b6830 65 64 20 69 6e 20 70 5b 30 5d 20 69 73 20 6c 61  ed in p[0] is la
b6840 72 67 65 72 20 74 68 61 6e 20 63 61 6e 20 66 69  rger than can fi
b6850 74 20 69 6e 20 61 20 33 32 2d 62 69 74 20 75 6e  t in a 32-bit un
b6860 73 69 67 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65  signed.** intege
b6870 72 2c 20 74 68 65 6e 20 73 65 74 20 2a 76 20 74  r, then set *v t
b6880 6f 20 30 78 66 66 66 66 66 66 66 66 2e 0a 2a 2a  o 0xffffffff..**
b6890 0a 2a 2a 20 41 20 4d 41 43 52 4f 20 76 65 72 73  .** A MACRO vers
b68a0 69 6f 6e 2c 20 67 65 74 56 61 72 69 6e 74 33 32  ion, getVarint32
b68b0 2c 20 69 73 20 70 72 6f 76 69 64 65 64 20 77 68  , is provided wh
b68c0 69 63 68 20 69 6e 6c 69 6e 65 73 20 74 68 65 20  ich inlines the 
b68d0 0a 2a 2a 20 73 69 6e 67 6c 65 2d 62 79 74 65 20  .** single-byte 
b68e0 63 61 73 65 2e 20 20 41 6c 6c 20 63 6f 64 65 20  case.  All code 
b68f0 73 68 6f 75 6c 64 20 75 73 65 20 74 68 65 20 4d  should use the M
b6900 41 43 52 4f 20 76 65 72 73 69 6f 6e 20 61 73 20  ACRO version as 
b6910 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f  .** this functio
b6920 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20 73 69  n assumes the si
b6930 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 20 68  ngle-byte case h
b6940 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
b6950 68 61 6e 64 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49  handled..*/.SQLI
b6960 54 45 5f 50 52 49 56 41 54 45 20 75 38 20 73 71  TE_PRIVATE u8 sq
b6970 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32  lite3GetVarint32
b6980 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20  (const unsigned 
b6990 63 68 61 72 20 2a 70 2c 20 75 33 32 20 2a 76 29  char *p, u32 *v)
b69a0 7b 0a 20 20 75 33 32 20 61 2c 62 3b 0a 0a 20 20  {.  u32 a,b;..  
b69b0 2f 2a 20 54 68 65 20 31 2d 62 79 74 65 20 63 61  /* The 1-byte ca
b69c0 73 65 2e 20 20 4f 76 65 72 77 68 65 6c 6d 69 6e  se.  Overwhelmin
b69d0 67 6c 79 20 74 68 65 20 6d 6f 73 74 20 63 6f 6d  gly the most com
b69e0 6d 6f 6e 2e 20 20 48 61 6e 64 6c 65 64 20 69 6e  mon.  Handled in
b69f0 6c 69 6e 65 0a 20 20 2a 2a 20 62 79 20 74 68 65  line.  ** by the
b6a00 20 67 65 74 56 61 72 69 6e 33 32 28 29 20 6d 61   getVarin32() ma
b6a10 63 72 6f 20 2a 2f 0a 20 20 61 20 3d 20 2a 70 3b  cro */.  a = *p;
b6a20 0a 20 20 2f 2a 20 61 3a 20 70 30 20 28 75 6e 6d  .  /* a: p0 (unm
b6a30 61 73 6b 65 64 29 20 2a 2f 0a 23 69 66 6e 64 65  asked) */.#ifnde
b6a40 66 20 67 65 74 56 61 72 69 6e 74 33 32 0a 20 20  f getVarint32.  
b6a50 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a 20  if (!(a&0x80)). 
b6a60 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73   {.    /* Values
b6a70 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20 31   between 0 and 1
b6a80 32 37 20 2a 2f 0a 20 20 20 20 2a 76 20 3d 20 61  27 */.    *v = a
b6a90 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
b6aa0 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
b6ab0 20 54 68 65 20 32 2d 62 79 74 65 20 63 61 73 65   The 2-byte case
b6ac0 20 2a 2f 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d   */.  p++;.  b =
b6ad0 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 20   *p;.  /* b: p1 
b6ae0 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20  (unmasked) */.  
b6af0 69 66 20 28 21 28 62 26 30 78 38 30 29 29 0a 20  if (!(b&0x80)). 
b6b00 20 7b 0a 20 20 20 20 2f 2a 20 56 61 6c 75 65 73   {.    /* Values
b6b10 20 62 65 74 77 65 65 6e 20 31 32 38 20 61 6e 64   between 128 and
b6b20 20 31 36 33 38 33 20 2a 2f 0a 20 20 20 20 61 20   16383 */.    a 
b6b30 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 61 20 3d  &= 0x7f;.    a =
b6b40 20 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20   a<<7;.    *v = 
b6b50 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72  a | b;.    retur
b6b60 6e 20 32 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54  n 2;.  }..  /* T
b6b70 68 65 20 33 2d 62 79 74 65 20 63 61 73 65 20 2a  he 3-byte case *
b6b80 2f 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61  /.  p++;.  a = a
b6b90 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b  <<14;.  a |= *p;
b6ba0 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20  .  /* a: p0<<14 
b6bb0 7c 20 70 32 20 28 75 6e 6d 61 73 6b 65 64 29 20  | p2 (unmasked) 
b6bc0 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78 38  */.  if (!(a&0x8
b6bd0 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20 56  0)).  {.    /* V
b6be0 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 36  alues between 16
b6bf0 33 38 34 20 61 6e 64 20 32 30 39 37 31 35 31 20  384 and 2097151 
b6c00 2a 2f 0a 20 20 20 20 61 20 26 3d 20 28 30 78 37  */.    a &= (0x7
b6c10 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20  f<<14)|(0x7f);. 
b6c20 20 20 20 62 20 26 3d 20 30 78 37 66 3b 0a 20 20     b &= 0x7f;.  
b6c30 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20    b = b<<7;.    
b6c40 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20 20 20  *v = a | b;.    
b6c50 72 65 74 75 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20  return 3;.  }.. 
b6c60 20 2f 2a 20 41 20 33 32 2d 62 69 74 20 76 61 72   /* A 32-bit var
b6c70 69 6e 74 20 69 73 20 75 73 65 64 20 74 6f 20 73  int is used to s
b6c80 74 6f 72 65 20 73 69 7a 65 20 69 6e 66 6f 72 6d  tore size inform
b6c90 61 74 69 6f 6e 20 69 6e 20 62 74 72 65 65 73 2e  ation in btrees.
b6ca0 0a 20 20 2a 2a 20 4f 62 6a 65 63 74 73 20 61 72  .  ** Objects ar
b6cb0 65 20 72 61 72 65 6c 79 20 6c 61 72 67 65 72 20  e rarely larger 
b6cc0 74 68 61 6e 20 32 4d 69 42 20 6c 69 6d 69 74 20  than 2MiB limit 
b6cd0 6f 66 20 61 20 33 2d 62 79 74 65 20 76 61 72 69  of a 3-byte vari
b6ce0 6e 74 2e 0a 20 20 2a 2a 20 41 20 33 2d 62 79 74  nt..  ** A 3-byt
b6cf0 65 20 76 61 72 69 6e 74 20 69 73 20 73 75 66 66  e varint is suff
b6d00 69 63 69 65 6e 74 2c 20 66 6f 72 20 65 78 61 6d  icient, for exam
b6d10 70 6c 65 2c 20 74 6f 20 72 65 63 6f 72 64 20 74  ple, to record t
b6d20 68 65 20 73 69 7a 65 0a 20 20 2a 2a 20 6f 66 20  he size.  ** of 
b6d30 61 20 31 30 34 38 35 36 39 2d 62 79 74 65 20 42  a 1048569-byte B
b6d40 4c 4f 42 20 6f 72 20 73 74 72 69 6e 67 2e 0a 20  LOB or string.. 
b6d50 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 6f 6e 6c 79   **.  ** We only
b6d60 20 75 6e 72 6f 6c 6c 20 74 68 65 20 66 69 72 73   unroll the firs
b6d70 74 20 31 2d 2c 20 32 2d 2c 20 61 6e 64 20 33 2d  t 1-, 2-, and 3-
b6d80 20 62 79 74 65 20 63 61 73 65 73 2e 20 20 54 68   byte cases.  Th
b6d90 65 20 76 65 72 79 0a 20 20 2a 2a 20 72 61 72 65  e very.  ** rare
b6da0 20 6c 61 72 67 65 72 20 63 61 73 65 73 20 63 61   larger cases ca
b6db0 6e 20 62 65 20 68 61 6e 64 6c 65 64 20 62 79 20  n be handled by 
b6dc0 74 68 65 20 73 6c 6f 77 65 72 20 36 34 2d 62 69  the slower 64-bi
b6dd0 74 20 76 61 72 69 6e 74 0a 20 20 2a 2a 20 72 6f  t varint.  ** ro
b6de0 75 74 69 6e 65 2e 0a 20 20 2a 2f 0a 23 69 66 20  utine..  */.#if 
b6df0 31 0a 20 20 7b 0a 20 20 20 20 75 36 34 20 76 36  1.  {.    u64 v6
b6e00 34 3b 0a 20 20 20 20 75 38 20 6e 3b 0a 0a 20 20  4;.    u8 n;..  
b6e10 20 20 70 20 2d 3d 20 32 3b 0a 20 20 20 20 6e 20    p -= 2;.    n 
b6e20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69  = sqlite3GetVari
b6e30 6e 74 28 70 2c 20 26 76 36 34 29 3b 0a 20 20 20  nt(p, &v64);.   
b6e40 20 61 73 73 65 72 74 28 20 6e 3e 33 20 26 26 20   assert( n>3 && 
b6e50 6e 3c 3d 39 20 29 3b 0a 20 20 20 20 69 66 28 20  n<=9 );.    if( 
b6e60 28 76 36 34 20 26 20 53 51 4c 49 54 45 5f 4d 41  (v64 & SQLITE_MA
b6e70 58 5f 55 33 32 29 21 3d 76 36 34 20 29 7b 0a 20  X_U32)!=v64 ){. 
b6e80 20 20 20 20 20 2a 76 20 3d 20 30 78 66 66 66 66       *v = 0xffff
b6e90 66 66 66 66 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ffff;.    }else{
b6ea0 0a 20 20 20 20 20 20 2a 76 20 3d 20 28 75 33 32  .      *v = (u32
b6eb0 29 76 36 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20  )v64;.    }.    
b6ec0 72 65 74 75 72 6e 20 6e 3b 0a 20 20 7d 0a 0a 23  return n;.  }..#
b6ed0 65 6c 73 65 0a 20 20 2f 2a 20 46 6f 72 20 66 6f  else.  /* For fo
b6ee0 6c 6c 6f 77 69 6e 67 20 63 6f 64 65 20 28 6b 65  llowing code (ke
b6ef0 70 74 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61  pt for historica
b6f00 6c 20 72 65 63 6f 72 64 20 6f 6e 6c 79 29 20 73  l record only) s
b6f10 68 6f 77 73 20 61 6e 0a 20 20 2a 2a 20 75 6e 72  hows an.  ** unr
b6f20 6f 6c 6c 69 6e 67 20 66 6f 72 20 74 68 65 20 33  olling for the 3
b6f30 2d 20 61 6e 64 20 34 2d 62 79 74 65 20 76 61 72  - and 4-byte var
b6f40 69 6e 74 20 63 61 73 65 73 2e 20 20 54 68 69 73  int cases.  This
b6f50 20 63 6f 64 65 20 69 73 0a 20 20 2a 2a 20 73 6c   code is.  ** sl
b6f60 69 67 68 74 6c 79 20 66 61 73 74 65 72 2c 20 62  ightly faster, b
b6f70 75 74 20 69 74 20 69 73 20 61 6c 73 6f 20 6c 61  ut it is also la
b6f80 72 67 65 72 20 61 6e 64 20 6d 75 63 68 20 68 61  rger and much ha
b6f90 72 64 65 72 20 74 6f 20 74 65 73 74 2e 0a 20 20  rder to test..  
b6fa0 2a 2f 0a 20 20 70 2b 2b 3b 0a 20 20 62 20 3d 20  */.  p++;.  b = 
b6fb0 62 3c 3c 31 34 3b 0a 20 20 62 20 7c 3d 20 2a 70  b<<14;.  b |= *p
b6fc0 3b 0a 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 34  ;.  /* b: p1<<14
b6fd0 20 7c 20 70 33 20 28 75 6e 6d 61 73 6b 65 64 29   | p3 (unmasked)
b6fe0 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78   */.  if (!(b&0x
b6ff0 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20  80)).  {.    /* 
b7000 56 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 32  Values between 2
b7010 30 39 37 31 35 32 20 61 6e 64 20 32 36 38 34 33  097152 and 26843
b7020 35 34 35 35 20 2a 2f 0a 20 20 20 20 62 20 26 3d  5455 */.    b &=
b7030 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37   (0x7f<<14)|(0x7
b7040 66 29 3b 0a 20 20 20 20 61 20 26 3d 20 28 30 78  f);.    a &= (0x
b7050 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
b7060 20 20 20 20 61 20 3d 20 61 3c 3c 37 3b 0a 20 20      a = a<<7;.  
b7070 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b 0a 20 20    *v = a | b;.  
b7080 20 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 7d 0a    return 4;.  }.
b7090 0a 20 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c  .  p++;.  a = a<
b70a0 3c 31 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a  <14;.  a |= *p;.
b70b0 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 32 38 20 7c    /* a: p0<<28 |
b70c0 20 70 32 3c 3c 31 34 20 7c 20 70 34 20 28 75 6e   p2<<14 | p4 (un
b70d0 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20  masked) */.  if 
b70e0 28 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a  (!(a&0x80)).  {.
b70f0 20 20 20 20 2f 2a 20 57 61 6c 75 65 73 20 20 62      /* Walues  b
b7100 65 74 77 65 65 6e 20 32 36 38 34 33 35 34 35 36  etween 268435456
b7110 20 61 6e 64 20 33 34 33 35 39 37 33 38 33 36 37   and 34359738367
b7120 20 2a 2f 0a 20 20 20 20 61 20 26 3d 20 28 30 78   */.    a &= (0x
b7130 31 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c 3c 31  1f<<28)|(0x7f<<1
b7140 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62  4)|(0x7f);.    b
b7150 20 26 3d 20 28 30 78 31 66 3c 3c 32 38 29 7c 28   &= (0x1f<<28)|(
b7160 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29  0x7f<<14)|(0x7f)
b7170 3b 0a 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a  ;.    b = b<<7;.
b7180 20 20 20 20 2a 76 20 3d 20 61 20 7c 20 62 3b 0a      *v = a | b;.
b7190 20 20 20 20 72 65 74 75 72 6e 20 35 3b 0a 20 20      return 5;.  
b71a0 7d 0a 0a 20 20 2f 2a 20 57 65 20 63 61 6e 20 6f  }..  /* We can o
b71b0 6e 6c 79 20 72 65 61 63 68 20 74 68 69 73 20 70  nly reach this p
b71c0 6f 69 6e 74 20 77 68 65 6e 20 72 65 61 64 69 6e  oint when readin
b71d0 67 20 61 20 63 6f 72 72 75 70 74 20 64 61 74 61  g a corrupt data
b71e0 62 61 73 65 0a 20 20 2a 2a 20 66 69 6c 65 2e 20  base.  ** file. 
b71f0 20 49 6e 20 74 68 61 74 20 63 61 73 65 20 77 65   In that case we
b7200 20 61 72 65 20 6e 6f 74 20 69 6e 20 61 6e 79 20   are not in any 
b7210 68 75 72 72 79 2e 20 20 55 73 65 20 74 68 65 20  hurry.  Use the 
b7220 28 72 65 6c 61 74 69 76 65 6c 79 0a 20 20 2a 2a  (relatively.  **
b7230 20 73 6c 6f 77 29 20 67 65 6e 65 72 61 6c 2d 70   slow) general-p
b7240 75 72 70 6f 73 65 20 73 71 6c 69 74 65 33 47 65  urpose sqlite3Ge
b7250 74 56 61 72 69 6e 74 28 29 20 72 6f 75 74 69 6e  tVarint() routin
b7260 65 20 74 6f 20 65 78 74 72 61 63 74 20 74 68 65  e to extract the
b7270 0a 20 20 2a 2a 20 76 61 6c 75 65 2e 20 2a 2f 0a  .  ** value. */.
b7280 20 20 7b 0a 20 20 20 20 75 36 34 20 76 36 34 3b    {.    u64 v64;
b7290 0a 20 20 20 20 75 38 20 6e 3b 0a 0a 20 20 20 20  .    u8 n;..    
b72a0 70 20 2d 3d 20 34 3b 0a 20 20 20 20 6e 20 3d 20  p -= 4;.    n = 
b72b0 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74  sqlite3GetVarint
b72c0 28 70 2c 20 26 76 36 34 29 3b 0a 20 20 20 20 61  (p, &v64);.    a
b72d0 73 73 65 72 74 28 20 6e 3e 35 20 26 26 20 6e 3c  ssert( n>5 && n<
b72e0 3d 39 20 29 3b 0a 20 20 20 20 2a 76 20 3d 20 28  =9 );.    *v = (
b72f0 75 33 32 29 76 36 34 3b 0a 20 20 20 20 72 65 74  u32)v64;.    ret
b7300 75 72 6e 20 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69  urn n;.  }.#endi
b7310 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  f.}../*.** Retur
b7320 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
b7330 62 79 74 65 73 20 74 68 61 74 20 77 69 6c 6c 20  bytes that will 
b7340 62 65 20 6e 65 65 64 65 64 20 74 6f 20 73 74 6f  be needed to sto
b7350 72 65 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20  re the given.** 
b7360 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 0a  64-bit integer..
b7370 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b7380 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 72  E int sqlite3Var
b7390 69 6e 74 4c 65 6e 28 75 36 34 20 76 29 7b 0a 20  intLen(u64 v){. 
b73a0 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 64 6f   int i = 0;.  do
b73b0 7b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 76  {.    i++;.    v
b73c0 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65   >>= 7;.  }while
b73d0 28 20 76 21 3d 30 20 26 26 20 41 4c 57 41 59 53  ( v!=0 && ALWAYS
b73e0 28 69 3c 39 29 20 29 3b 0a 20 20 72 65 74 75 72  (i<9) );.  retur
b73f0 6e 20 69 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52  n i;.}.../*.** R
b7400 65 61 64 20 6f 72 20 77 72 69 74 65 20 61 20 66  ead or write a f
b7410 6f 75 72 2d 62 79 74 65 20 62 69 67 2d 65 6e 64  our-byte big-end
b7420 69 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75  ian integer valu
b7430 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
b7440 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65 33  VATE u32 sqlite3
b7450 47 65 74 34 62 79 74 65 28 63 6f 6e 73 74 20 75  Get4byte(const u
b7460 38 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20  8 *p){.  return 
b7470 28 70 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 70 5b  (p[0]<<24) | (p[
b7480 31 5d 3c 3c 31 36 29 20 7c 20 28 70 5b 32 5d 3c  1]<<16) | (p[2]<
b7490 3c 38 29 20 7c 20 70 5b 33 5d 3b 0a 7d 0a 53 51  <8) | p[3];.}.SQ
b74a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
b74b0 64 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74  d sqlite3Put4byt
b74c0 65 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  e(unsigned char 
b74d0 2a 70 2c 20 75 33 32 20 76 29 7b 0a 20 20 70 5b  *p, u32 v){.  p[
b74e0 30 5d 20 3d 20 28 75 38 29 28 76 3e 3e 32 34 29  0] = (u8)(v>>24)
b74f0 3b 0a 20 20 70 5b 31 5d 20 3d 20 28 75 38 29 28  ;.  p[1] = (u8)(
b7500 76 3e 3e 31 36 29 3b 0a 20 20 70 5b 32 5d 20 3d  v>>16);.  p[2] =
b7510 20 28 75 38 29 28 76 3e 3e 38 29 3b 0a 20 20 70   (u8)(v>>8);.  p
b7520 5b 33 5d 20 3d 20 28 75 38 29 76 3b 0a 7d 0a 0a  [3] = (u8)v;.}..
b7530 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
b7540 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f  QLITE_OMIT_BLOB_
b7550 4c 49 54 45 52 41 4c 29 20 7c 7c 20 64 65 66 69  LITERAL) || defi
b7560 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 53 5f 43  ned(SQLITE_HAS_C
b7570 4f 44 45 43 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e  ODEC)./*.** Tran
b7580 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 20 62  slate a single b
b7590 79 74 65 20 6f 66 20 48 65 78 20 69 6e 74 6f 20  yte of Hex into 
b75a0 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 54  an integer..** T
b75b0 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79  his routine only
b75c0 20 77 6f 72 6b 73 20 69 66 20 68 20 72 65 61 6c   works if h real
b75d0 6c 79 20 69 73 20 61 20 76 61 6c 69 64 20 68 65  ly is a valid he
b75e0 78 61 64 65 63 69 6d 61 6c 0a 2a 2a 20 63 68 61  xadecimal.** cha
b75f0 72 61 63 74 65 72 3a 20 20 30 2e 2e 39 61 2e 2e  racter:  0..9a..
b7600 66 41 2e 2e 46 0a 2a 2f 0a 73 74 61 74 69 63 20  fA..F.*/.static 
b7610 75 38 20 68 65 78 54 6f 49 6e 74 28 69 6e 74 20  u8 hexToInt(int 
b7620 68 29 7b 0a 20 20 61 73 73 65 72 74 28 20 28 68  h){.  assert( (h
b7630 3e 3d 27 30 27 20 26 26 20 68 3c 3d 27 39 27 29  >='0' && h<='9')
b7640 20 7c 7c 20 20 28 68 3e 3d 27 61 27 20 26 26 20   ||  (h>='a' && 
b7650 68 3c 3d 27 66 27 29 20 7c 7c 20 20 28 68 3e 3d  h<='f') ||  (h>=
b7660 27 41 27 20 26 26 20 68 3c 3d 27 46 27 29 20 29  'A' && h<='F') )
b7670 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
b7680 41 53 43 49 49 0a 20 20 68 20 2b 3d 20 39 2a 28  ASCII.  h += 9*(
b7690 31 26 28 68 3e 3e 36 29 29 3b 0a 23 65 6e 64 69  1&(h>>6));.#endi
b76a0 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
b76b0 45 42 43 44 49 43 0a 20 20 68 20 2b 3d 20 39 2a  EBCDIC.  h += 9*
b76c0 28 31 26 7e 28 68 3e 3e 34 29 29 3b 0a 23 65 6e  (1&~(h>>4));.#en
b76d0 64 69 66 0a 20 20 72 65 74 75 72 6e 20 28 75 38  dif.  return (u8
b76e0 29 28 68 20 26 20 30 78 66 29 3b 0a 7d 0a 23 65  )(h & 0xf);.}.#e
b76f0 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f  ndif /* !SQLITE_
b7700 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41  OMIT_BLOB_LITERA
b7710 4c 20 7c 7c 20 53 51 4c 49 54 45 5f 48 41 53 5f  L || SQLITE_HAS_
b7720 43 4f 44 45 43 20 2a 2f 0a 0a 23 69 66 20 21 64  CODEC */..#if !d
b7730 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
b7740 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 29  IT_BLOB_LITERAL)
b7750 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
b7760 54 45 5f 48 41 53 5f 43 4f 44 45 43 29 0a 2f 2a  TE_HAS_CODEC)./*
b7770 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 42 4c  .** Convert a BL
b7780 4f 42 20 6c 69 74 65 72 61 6c 20 6f 66 20 74 68  OB literal of th
b7790 65 20 66 6f 72 6d 20 22 78 27 68 68 68 68 68 68  e form "x'hhhhhh
b77a0 27 22 20 69 6e 74 6f 20 69 74 73 20 62 69 6e 61  '" into its bina
b77b0 72 79 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 52 65  ry.** value.  Re
b77c0 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
b77d0 6f 20 69 74 73 20 62 69 6e 61 72 79 20 76 61 6c  o its binary val
b77e0 75 65 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f  ue.  Space to ho
b77f0 6c 64 20 74 68 65 0a 2a 2a 20 62 69 6e 61 72 79  ld the.** binary
b7800 20 76 61 6c 75 65 20 68 61 73 20 62 65 65 6e 20   value has been 
b7810 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61  obtained from ma
b7820 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20 62 65  lloc and must be
b7830 20 66 72 65 65 64 20 62 79 0a 2a 2a 20 74 68 65   freed by.** the
b7840 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65   calling routine
b7850 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
b7860 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
b7870 33 48 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74  3HexToBlob(sqlit
b7880 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68  e3 *db, const ch
b7890 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20  ar *z, int n){. 
b78a0 20 63 68 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20   char *zBlob;.  
b78b0 69 6e 74 20 69 3b 0a 0a 20 20 7a 42 6c 6f 62 20  int i;..  zBlob 
b78c0 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65  = (char *)sqlite
b78d0 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
b78e0 20 6e 2f 32 20 2b 20 31 29 3b 0a 20 20 6e 2d 2d   n/2 + 1);.  n--
b78f0 3b 0a 20 20 69 66 28 20 7a 42 6c 6f 62 20 29 7b  ;.  if( zBlob ){
b7900 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
b7910 6e 3b 20 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20  n; i+=2){.      
b7920 7a 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20 28 68 65  zBlob[i/2] = (he
b7930 78 54 6f 49 6e 74 28 7a 5b 69 5d 29 3c 3c 34 29  xToInt(z[i])<<4)
b7940 20 7c 20 68 65 78 54 6f 49 6e 74 28 7a 5b 69 2b   | hexToInt(z[i+
b7950 31 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a  1]);.    }.    z
b7960 42 6c 6f 62 5b 69 2f 32 5d 20 3d 20 30 3b 0a 20  Blob[i/2] = 0;. 
b7970 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 42 6c 6f   }.  return zBlo
b7980 62 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21  b;.}.#endif /* !
b7990 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42  SQLITE_OMIT_BLOB
b79a0 5f 4c 49 54 45 52 41 4c 20 7c 7c 20 53 51 4c 49  _LITERAL || SQLI
b79b0 54 45 5f 48 41 53 5f 43 4f 44 45 43 20 2a 2f 0a  TE_HAS_CODEC */.
b79c0 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
b79d0 68 65 20 73 71 6c 69 74 65 2e 6d 61 67 69 63 20  he sqlite.magic 
b79e0 66 72 6f 6d 20 53 51 4c 49 54 45 5f 4d 41 47 49  from SQLITE_MAGI
b79f0 43 5f 4f 50 45 4e 20 74 6f 20 53 51 4c 49 54 45  C_OPEN to SQLITE
b7a00 5f 4d 41 47 49 43 5f 42 55 53 59 2e 0a 2a 2a 20  _MAGIC_BUSY..** 
b7a10 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  Return an error 
b7a20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68  (non-zero) if th
b7a30 65 20 6d 61 67 69 63 20 77 61 73 20 6e 6f 74 20  e magic was not 
b7a40 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45  SQLITE_MAGIC_OPE
b7a50 4e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 72  N.** when this r
b7a60 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
b7a70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
b7a80 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77  tine is called w
b7a90 68 65 6e 20 65 6e 74 65 72 69 6e 67 20 61 6e 20  hen entering an 
b7aa0 53 51 4c 69 74 65 20 41 50 49 2e 20 20 54 68 65  SQLite API.  The
b7ab0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50   SQLITE_MAGIC_OP
b7ac0 45 4e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 64 69  EN.** value indi
b7ad0 63 61 74 65 73 20 74 68 61 74 20 74 68 65 20 64  cates that the d
b7ae0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
b7af0 6f 6e 20 70 61 73 73 65 64 20 69 6e 74 6f 20 74  on passed into t
b7b00 68 65 20 41 50 49 20 69 73 0a 2a 2a 20 6f 70 65  he API is.** ope
b7b10 6e 20 61 6e 64 20 69 73 20 6e 6f 74 20 62 65 69  n and is not bei
b7b20 6e 67 20 75 73 65 64 20 62 79 20 61 6e 6f 74 68  ng used by anoth
b7b30 65 72 20 74 68 72 65 61 64 2e 20 20 42 79 20 63  er thread.  By c
b7b40 68 61 6e 67 69 6e 67 20 74 68 65 20 76 61 6c 75  hanging the valu
b7b50 65 0a 2a 2a 20 74 6f 20 53 51 4c 49 54 45 5f 4d  e.** to SQLITE_M
b7b60 41 47 49 43 5f 42 55 53 59 20 77 65 20 69 6e 64  AGIC_BUSY we ind
b7b70 69 63 61 74 65 20 74 68 61 74 20 74 68 65 20 63  icate that the c
b7b80 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 69 6e 20  onnection is in 
b7b90 75 73 65 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 53  use..** sqlite3S
b7ba0 61 66 65 74 79 4f 66 66 28 29 20 62 65 6c 6f 77  afetyOff() below
b7bb0 20 77 69 6c 6c 20 63 68 61 6e 67 65 20 74 68 65   will change the
b7bc0 20 76 61 6c 75 65 20 62 61 63 6b 20 74 6f 20 53   value back to S
b7bd0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e  QLITE_MAGIC_OPEN
b7be0 0a 2a 2a 20 77 68 65 6e 20 74 68 65 20 41 50 49  .** when the API
b7bf0 20 65 78 69 74 73 2e 20 0a 2a 2a 0a 2a 2a 20 54   exits. .**.** T
b7c00 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
b7c10 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65 74 65   attempt to dete
b7c20 63 74 20 69 66 20 74 77 6f 20 74 68 72 65 61 64  ct if two thread
b7c30 73 20 75 73 65 20 74 68 65 0a 2a 2a 20 73 61 6d  s use the.** sam
b7c40 65 20 73 71 6c 69 74 65 2a 20 70 6f 69 6e 74 65  e sqlite* pointe
b7c50 72 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69  r at the same ti
b7c60 6d 65 2e 20 20 54 68 65 72 65 20 69 73 20 61 20  me.  There is a 
b7c70 72 61 63 65 20 0a 2a 2a 20 63 6f 6e 64 69 74 69  race .** conditi
b7c80 6f 6e 20 73 6f 20 69 74 20 69 73 20 70 6f 73 73  on so it is poss
b7c90 69 62 6c 65 20 74 68 61 74 20 74 68 65 20 65 72  ible that the er
b7ca0 72 6f 72 20 69 73 20 6e 6f 74 20 64 65 74 65 63  ror is not detec
b7cb0 74 65 64 2e 0a 2a 2a 20 42 75 74 20 75 73 75 61  ted..** But usua
b7cc0 6c 6c 79 20 74 68 65 20 70 72 6f 62 6c 65 6d 20  lly the problem 
b7cd0 77 69 6c 6c 20 62 65 20 73 65 65 6e 2e 20 20 54  will be seen.  T
b7ce0 68 65 20 72 65 73 75 6c 74 20 77 69 6c 6c 20 62  he result will b
b7cf0 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 77 68  e an.** error wh
b7d00 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 20  ich can be used 
b7d10 74 6f 20 64 65 62 75 67 20 74 68 65 20 61 70 70  to debug the app
b7d20 6c 69 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73  lication that is
b7d30 0a 2a 2a 20 75 73 69 6e 67 20 53 51 4c 69 74 65  .** using SQLite
b7d40 20 69 6e 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a   incorrectly..**
b7d50 0a 2a 2a 20 54 69 63 6b 65 74 20 23 32 30 32 3a  .** Ticket #202:
b7d60 20 20 49 66 20 64 62 2d 3e 6d 61 67 69 63 20 69    If db->magic i
b7d70 73 20 6e 6f 74 20 61 20 76 61 6c 69 64 20 6f 70  s not a valid op
b7d80 65 6e 20 76 61 6c 75 65 2c 20 74 61 6b 65 20 63  en value, take c
b7d90 61 72 65 20 6e 6f 74 0a 2a 2a 20 74 6f 20 6d 6f  are not.** to mo
b7da0 64 69 66 79 20 74 68 65 20 64 62 20 73 74 72 75  dify the db stru
b7db0 63 74 75 72 65 20 61 74 20 61 6c 6c 2e 20 20 49  cture at all.  I
b7dc0 74 20 63 6f 75 6c 64 20 62 65 20 74 68 61 74 20  t could be that 
b7dd0 64 62 20 69 73 20 61 20 73 74 61 6c 65 0a 2a 2a  db is a stale.**
b7de0 20 70 6f 69 6e 74 65 72 2e 20 20 49 6e 20 6f 74   pointer.  In ot
b7df0 68 65 72 20 77 6f 72 64 73 2c 20 69 74 20 63 6f  her words, it co
b7e00 75 6c 64 20 62 65 20 74 68 61 74 20 74 68 65 72  uld be that ther
b7e10 65 20 68 61 73 20 62 65 65 6e 20 61 20 70 72 69  e has been a pri
b7e20 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71  or.** call to sq
b7e30 6c 69 74 65 33 5f 63 6c 6f 73 65 28 64 62 29 20  lite3_close(db) 
b7e40 61 6e 64 20 64 62 20 68 61 73 20 62 65 65 6e 20  and db has been 
b7e50 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 20 41 6e  deallocated.  An
b7e60 64 20 77 65 20 64 6f 0a 2a 2a 20 6e 6f 74 20 77  d we do.** not w
b7e70 61 6e 74 20 74 6f 20 77 72 69 74 65 20 69 6e 74  ant to write int
b7e80 6f 20 64 65 61 6c 6c 6f 63 61 74 65 64 20 6d 65  o deallocated me
b7e90 6d 6f 72 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  mory..*/.#ifdef 
b7ea0 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c  SQLITE_DEBUG.SQL
b7eb0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b7ec0 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
b7ed0 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
b7ee0 69 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53  if( db->magic==S
b7ef0 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e  QLITE_MAGIC_OPEN
b7f00 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69   ){.    db->magi
b7f10 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  c = SQLITE_MAGIC
b7f20 5f 42 55 53 59 3b 0a 20 20 20 20 61 73 73 65 72  _BUSY;.    asser
b7f30 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
b7f40 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
b7f50 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   );.    return 0
b7f60 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 64 62  ;.  }else if( db
b7f70 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f  ->magic==SQLITE_
b7f80 4d 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20 20  MAGIC_BUSY ){.  
b7f90 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51    db->magic = SQ
b7fa0 4c 49 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52  LITE_MAGIC_ERROR
b7fb0 3b 0a 20 20 20 20 64 62 2d 3e 75 31 2e 69 73 49  ;.    db->u1.isI
b7fc0 6e 74 65 72 72 75 70 74 65 64 20 3d 20 31 3b 0a  nterrupted = 1;.
b7fd0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a    }.  return 1;.
b7fe0 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
b7ff0 43 68 61 6e 67 65 20 74 68 65 20 6d 61 67 69 63  Change the magic
b8000 20 66 72 6f 6d 20 53 51 4c 49 54 45 5f 4d 41 47   from SQLITE_MAG
b8010 49 43 5f 42 55 53 59 20 74 6f 20 53 51 4c 49 54  IC_BUSY to SQLIT
b8020 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 2e 0a 2a 2a  E_MAGIC_OPEN..**
b8030 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   Return an error
b8040 20 28 6e 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 74   (non-zero) if t
b8050 68 65 20 6d 61 67 69 63 20 77 61 73 20 6e 6f 74  he magic was not
b8060 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55   SQLITE_MAGIC_BU
b8070 53 59 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20  SY.** when this 
b8080 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
b8090 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  d..*/.#ifdef SQL
b80a0 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45  ITE_DEBUG.SQLITE
b80b0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
b80c0 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 73 71  ite3SafetyOff(sq
b80d0 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 66  lite3 *db){.  if
b80e0 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c  ( db->magic==SQL
b80f0 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20 29  ITE_MAGIC_BUSY )
b8100 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20  {.    db->magic 
b8110 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f  = SQLITE_MAGIC_O
b8120 50 45 4e 3b 0a 20 20 20 20 61 73 73 65 72 74 28  PEN;.    assert(
b8130 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
b8140 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
b8150 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
b8160 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d    }else{.    db-
b8170 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f  >magic = SQLITE_
b8180 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20 20 20  MAGIC_ERROR;.   
b8190 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72   db->u1.isInterr
b81a0 75 70 74 65 64 20 3d 20 31 3b 0a 20 20 20 20 72  upted = 1;.    r
b81b0 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23  eturn 1;.  }.}.#
b81c0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65  endif../*.** Che
b81d0 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ck to make sure 
b81e0 77 65 20 68 61 76 65 20 61 20 76 61 6c 69 64 20  we have a valid 
b81f0 64 62 20 70 6f 69 6e 74 65 72 2e 20 20 54 68 69  db pointer.  Thi
b8200 73 20 74 65 73 74 20 69 73 20 6e 6f 74 0a 2a 2a  s test is not.**
b8210 20 66 6f 6f 6c 70 72 6f 6f 66 20 62 75 74 20 69   foolproof but i
b8220 74 20 64 6f 65 73 20 70 72 6f 76 69 64 65 20 73  t does provide s
b8230 6f 6d 65 20 6d 65 61 73 75 72 65 20 6f 66 20 70  ome measure of p
b8240 72 6f 74 65 63 74 69 6f 6e 20 61 67 61 69 6e 73  rotection agains
b8250 74 0a 2a 2a 20 6d 69 73 75 73 65 20 6f 66 20 74  t.** misuse of t
b8260 68 65 20 69 6e 74 65 72 66 61 63 65 20 73 75 63  he interface suc
b8270 68 20 61 73 20 70 61 73 73 69 6e 67 20 69 6e 20  h as passing in 
b8280 64 62 20 70 6f 69 6e 74 65 72 73 20 74 68 61 74  db pointers that
b8290 20 61 72 65 0a 2a 2a 20 4e 55 4c 4c 20 6f 72 20   are.** NULL or 
b82a0 77 68 69 63 68 20 68 61 76 65 20 62 65 65 6e 20  which have been 
b82b0 70 72 65 76 69 6f 75 73 6c 79 20 63 6c 6f 73 65  previously close
b82c0 64 2e 20 20 49 66 20 74 68 69 73 20 72 6f 75 74  d.  If this rout
b82d0 69 6e 65 20 72 65 74 75 72 6e 73 0a 2a 2a 20 31  ine returns.** 1
b82e0 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74   it means that t
b82f0 68 65 20 64 62 20 70 6f 69 6e 74 65 72 20 69 73  he db pointer is
b8300 20 76 61 6c 69 64 20 61 6e 64 20 30 20 69 66 20   valid and 0 if 
b8310 69 74 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65  it should not be
b8320 0a 2a 2a 20 64 65 72 65 66 65 72 65 6e 63 65 64  .** dereferenced
b8330 20 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 2e   for any reason.
b8340 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75    The calling fu
b8350 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 69 6e  nction should in
b8360 76 6f 6b 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d  voke.** SQLITE_M
b8370 49 53 55 53 45 20 69 6d 6d 65 64 69 61 74 65 6c  ISUSE immediatel
b8380 79 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33  y..**.** sqlite3
b8390 53 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 29 20  SafetyCheckOk() 
b83a0 72 65 71 75 69 72 65 73 20 74 68 61 74 20 74 68  requires that th
b83b0 65 20 64 62 20 70 6f 69 6e 74 65 72 20 62 65 20  e db pointer be 
b83c0 76 61 6c 69 64 20 66 6f 72 0a 2a 2a 20 75 73 65  valid for.** use
b83d0 2e 20 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  .  sqlite3Safety
b83e0 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 29 20  CheckSickOrOk() 
b83f0 61 6c 6c 6f 77 73 20 61 20 64 62 20 70 6f 69 6e  allows a db poin
b8400 74 65 72 20 74 68 61 74 20 66 61 69 6c 65 64 20  ter that failed 
b8410 74 6f 0a 2a 2a 20 6f 70 65 6e 20 70 72 6f 70 65  to.** open prope
b8420 72 6c 79 20 61 6e 64 20 69 73 20 6e 6f 74 20 66  rly and is not f
b8430 69 74 20 66 6f 72 20 67 65 6e 65 72 61 6c 20 75  it for general u
b8440 73 65 20 62 75 74 20 77 68 69 63 68 20 63 61 6e  se but which can
b8450 20 62 65 0a 2a 2a 20 75 73 65 64 20 61 73 20 61   be.** used as a
b8460 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71  n argument to sq
b8470 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20 6f  lite3_errmsg() o
b8480 72 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28  r sqlite3_close(
b8490 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
b84a0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
b84b0 53 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 73 71  SafetyCheckOk(sq
b84c0 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 75 33  lite3 *db){.  u3
b84d0 32 20 6d 61 67 69 63 3b 0a 20 20 69 66 28 20 64  2 magic;.  if( d
b84e0 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  b==0 ) return 0;
b84f0 0a 20 20 6d 61 67 69 63 20 3d 20 64 62 2d 3e 6d  .  magic = db->m
b8500 61 67 69 63 3b 0a 20 20 69 66 28 20 6d 61 67 69  agic;.  if( magi
b8510 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  c!=SQLITE_MAGIC_
b8520 4f 50 45 4e 20 0a 23 69 66 64 65 66 20 53 51 4c  OPEN .#ifdef SQL
b8530 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 26  ITE_DEBUG.     &
b8540 26 20 6d 61 67 69 63 21 3d 53 51 4c 49 54 45 5f  & magic!=SQLITE_
b8550 4d 41 47 49 43 5f 42 55 53 59 0a 23 65 6e 64 69  MAGIC_BUSY.#endi
b8560 66 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72  f.  ){.    retur
b8570 6e 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  n 0;.  }else{.  
b8580 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
b8590 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
b85a0 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66 65   int sqlite3Safe
b85b0 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28  tyCheckSickOrOk(
b85c0 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
b85d0 75 33 32 20 6d 61 67 69 63 3b 0a 20 20 6d 61 67  u32 magic;.  mag
b85e0 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69 63 3b 0a  ic = db->magic;.
b85f0 20 20 69 66 28 20 6d 61 67 69 63 21 3d 53 51 4c    if( magic!=SQL
b8600 49 54 45 5f 4d 41 47 49 43 5f 53 49 43 4b 20 26  ITE_MAGIC_SICK &
b8610 26 0a 20 20 20 20 20 20 6d 61 67 69 63 21 3d 53  &.      magic!=S
b8620 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e  QLITE_MAGIC_OPEN
b8630 20 26 26 0a 20 20 20 20 20 20 6d 61 67 69 63 21   &&.      magic!
b8640 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55  =SQLITE_MAGIC_BU
b8650 53 59 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  SY ) return 0;. 
b8660 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
b8670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
b8680 64 20 6f 66 20 75 74 69 6c 2e 63 20 2a 2a 2a 2a  d of util.c ****
b8690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b86a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b86b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
b86c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
b86d0 67 69 6e 20 66 69 6c 65 20 68 61 73 68 2e 63 20  gin file hash.c 
b86e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b86f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
b8710 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
b8720 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 22.**.** The 
b8730 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
b8740 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
b8750 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
b8760 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
b8770 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
b8780 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
b8790 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
b87a0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
b87b0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
b87c0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
b87d0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
b87e0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
b87f0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
b8800 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
b8810 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
b8820 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
b8830 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
b8840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b8880 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ****.** This is 
b8890 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
b88a0 6f 6e 20 6f 66 20 67 65 6e 65 72 69 63 20 68 61  on of generic ha
b88b0 73 68 2d 74 61 62 6c 65 73 0a 2a 2a 20 75 73 65  sh-tables.** use
b88c0 64 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a  d in SQLite..**.
b88d0 2a 2a 20 24 49 64 3a 20 68 61 73 68 2e 63 2c 76  ** $Id: hash.c,v
b88e0 20 31 2e 33 38 20 32 30 30 39 2f 30 35 2f 30 39   1.38 2009/05/09
b88f0 20 32 33 3a 32 39 3a 31 32 20 64 72 68 20 45 78   23:29:12 drh Ex
b8900 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 54 75 72 6e 20  p $.*/../* Turn 
b8910 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f  bulk memory into
b8920 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 6f 62   a hash table ob
b8930 6a 65 63 74 20 62 79 20 69 6e 69 74 69 61 6c 69  ject by initiali
b8940 7a 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 65 6c  zing the.** fiel
b8950 64 73 20 6f 66 20 74 68 65 20 48 61 73 68 20 73  ds of the Hash s
b8960 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
b8970 22 70 4e 65 77 22 20 69 73 20 61 20 70 6f 69 6e  "pNew" is a poin
b8980 74 65 72 20 74 6f 20 74 68 65 20 68 61 73 68 20  ter to the hash 
b8990 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 74 6f  table that is to
b89a0 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e   be initialized.
b89b0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
b89c0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48  TE void sqlite3H
b89d0 61 73 68 49 6e 69 74 28 48 61 73 68 20 2a 70 4e  ashInit(Hash *pN
b89e0 65 77 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  ew){.  assert( p
b89f0 4e 65 77 21 3d 30 20 29 3b 0a 20 20 70 4e 65 77  New!=0 );.  pNew
b8a00 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 70  ->first = 0;.  p
b8a10 4e 65 77 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a  New->count = 0;.
b8a20 20 20 70 4e 65 77 2d 3e 68 74 73 69 7a 65 20 3d    pNew->htsize =
b8a30 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 68 74 20 3d   0;.  pNew->ht =
b8a40 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65   0;.}../* Remove
b8a50 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 66 72 6f   all entries fro
b8a60 6d 20 61 20 68 61 73 68 20 74 61 62 6c 65 2e 20  m a hash table. 
b8a70 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d   Reclaim all mem
b8a80 6f 72 79 2e 0a 2a 2a 20 43 61 6c 6c 20 74 68 69  ory..** Call thi
b8a90 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 64 65 6c  s routine to del
b8aa0 65 74 65 20 61 20 68 61 73 68 20 74 61 62 6c 65  ete a hash table
b8ab0 20 6f 72 20 74 6f 20 72 65 73 65 74 20 61 20 68   or to reset a h
b8ac0 61 73 68 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20  ash table.** to 
b8ad0 74 68 65 20 65 6d 70 74 79 20 73 74 61 74 65 2e  the empty state.
b8ae0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
b8af0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48  TE void sqlite3H
b8b00 61 73 68 43 6c 65 61 72 28 48 61 73 68 20 2a 70  ashClear(Hash *p
b8b10 48 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a  H){.  HashElem *
b8b20 65 6c 65 6d 3b 20 20 20 20 20 20 20 20 20 2f 2a  elem;         /*
b8b30 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
b8b40 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f  r all elements o
b8b50 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 0a  f the table */..
b8b60 20 20 61 73 73 65 72 74 28 20 70 48 21 3d 30 20    assert( pH!=0 
b8b70 29 3b 0a 20 20 65 6c 65 6d 20 3d 20 70 48 2d 3e  );.  elem = pH->
b8b80 66 69 72 73 74 3b 0a 20 20 70 48 2d 3e 66 69 72  first;.  pH->fir
b8b90 73 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  st = 0;.  sqlite
b8ba0 33 5f 66 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a  3_free(pH->ht);.
b8bb0 20 20 70 48 2d 3e 68 74 20 3d 20 30 3b 0a 20 20    pH->ht = 0;.  
b8bc0 70 48 2d 3e 68 74 73 69 7a 65 20 3d 20 30 3b 0a  pH->htsize = 0;.
b8bd0 20 20 77 68 69 6c 65 28 20 65 6c 65 6d 20 29 7b    while( elem ){
b8be0 0a 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 6e  .    HashElem *n
b8bf0 65 78 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d  ext_elem = elem-
b8c00 3e 6e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74  >next;.    sqlit
b8c10 65 33 5f 66 72 65 65 28 65 6c 65 6d 29 3b 0a 20  e3_free(elem);. 
b8c20 20 20 20 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65     elem = next_e
b8c30 6c 65 6d 3b 0a 20 20 7d 0a 20 20 70 48 2d 3e 63  lem;.  }.  pH->c
b8c40 6f 75 6e 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  ount = 0;.}../*.
b8c50 2a 2a 20 54 68 65 20 68 61 73 68 69 6e 67 20 66  ** The hashing f
b8c60 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  unction..*/.stat
b8c70 69 63 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  ic unsigned int 
b8c80 73 74 72 48 61 73 68 28 63 6f 6e 73 74 20 63 68  strHash(const ch
b8c90 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 4b 65 79 29  ar *z, int nKey)
b8ca0 7b 0a 20 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20  {.  int h = 0;. 
b8cb0 20 61 73 73 65 72 74 28 20 6e 4b 65 79 3e 3d 30   assert( nKey>=0
b8cc0 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4b 65   );.  while( nKe
b8cd0 79 20 3e 20 30 20 20 29 7b 0a 20 20 20 20 68 20  y > 0  ){.    h 
b8ce0 3d 20 28 68 3c 3c 33 29 20 5e 20 68 20 5e 20 73  = (h<<3) ^ h ^ s
b8cf0 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77  qlite3UpperToLow
b8d00 65 72 5b 28 75 6e 73 69 67 6e 65 64 20 63 68 61  er[(unsigned cha
b8d10 72 29 2a 7a 2b 2b 5d 3b 0a 20 20 20 20 6e 4b 65  r)*z++];.    nKe
b8d20 79 2d 2d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  y--;.  }.  retur
b8d30 6e 20 68 3b 0a 7d 0a 0a 0a 2f 2a 20 4c 69 6e 6b  n h;.}.../* Link
b8d40 20 70 4e 65 77 20 65 6c 65 6d 65 6e 74 20 69 6e   pNew element in
b8d50 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  to the hash tabl
b8d60 65 20 70 48 2e 20 20 49 66 20 70 45 6e 74 72 79  e pH.  If pEntry
b8d70 21 3d 30 20 74 68 65 6e 20 61 6c 73 6f 0a 2a 2a  !=0 then also.**
b8d80 20 69 6e 73 65 72 74 20 70 4e 65 77 20 69 6e 74   insert pNew int
b8d90 6f 20 74 68 65 20 70 45 6e 74 72 79 20 68 61 73  o the pEntry has
b8da0 68 20 62 75 63 6b 65 74 2e 0a 2a 2f 0a 73 74 61  h bucket..*/.sta
b8db0 74 69 63 20 76 6f 69 64 20 69 6e 73 65 72 74 45  tic void insertE
b8dc0 6c 65 6d 65 6e 74 28 0a 20 20 48 61 73 68 20 2a  lement(.  Hash *
b8dd0 70 48 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  pH,             
b8de0 20 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65   /* The complete
b8df0 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20   hash table */. 
b8e00 20 73 74 72 75 63 74 20 5f 68 74 20 2a 70 45 6e   struct _ht *pEn
b8e10 74 72 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 65  try,    /* The e
b8e20 6e 74 72 79 20 69 6e 74 6f 20 77 68 69 63 68 20  ntry into which 
b8e30 70 4e 65 77 20 69 73 20 69 6e 73 65 72 74 65 64  pNew is inserted
b8e40 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a   */.  HashElem *
b8e50 70 4e 65 77 20 20 20 20 20 20 20 20 20 2f 2a 20  pNew         /* 
b8e60 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62  The element to b
b8e70 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 29 7b  e inserted */.){
b8e80 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 70 48 65  .  HashElem *pHe
b8e90 61 64 3b 20 20 20 20 20 20 20 2f 2a 20 46 69 72  ad;       /* Fir
b8ea0 73 74 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61  st element alrea
b8eb0 64 79 20 69 6e 20 70 45 6e 74 72 79 20 2a 2f 0a  dy in pEntry */.
b8ec0 20 20 69 66 28 20 70 45 6e 74 72 79 20 29 7b 0a    if( pEntry ){.
b8ed0 20 20 20 20 70 48 65 61 64 20 3d 20 70 45 6e 74      pHead = pEnt
b8ee0 72 79 2d 3e 63 6f 75 6e 74 20 3f 20 70 45 6e 74  ry->count ? pEnt
b8ef0 72 79 2d 3e 63 68 61 69 6e 20 3a 20 30 3b 0a 20  ry->chain : 0;. 
b8f00 20 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74     pEntry->count
b8f10 2b 2b 3b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e  ++;.    pEntry->
b8f20 63 68 61 69 6e 20 3d 20 70 4e 65 77 3b 0a 20 20  chain = pNew;.  
b8f30 7d 65 6c 73 65 7b 0a 20 20 20 20 70 48 65 61 64  }else{.    pHead
b8f40 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 0;.  }.  if( 
b8f50 70 48 65 61 64 20 29 7b 0a 20 20 20 20 70 4e 65  pHead ){.    pNe
b8f60 77 2d 3e 6e 65 78 74 20 3d 20 70 48 65 61 64 3b  w->next = pHead;
b8f70 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 76 20  .    pNew->prev 
b8f80 3d 20 70 48 65 61 64 2d 3e 70 72 65 76 3b 0a 20  = pHead->prev;. 
b8f90 20 20 20 69 66 28 20 70 48 65 61 64 2d 3e 70 72     if( pHead->pr
b8fa0 65 76 20 29 7b 20 70 48 65 61 64 2d 3e 70 72 65  ev ){ pHead->pre
b8fb0 76 2d 3e 6e 65 78 74 20 3d 20 70 4e 65 77 3b 20  v->next = pNew; 
b8fc0 7d 0a 20 20 20 20 65 6c 73 65 20 20 20 20 20 20  }.    else      
b8fd0 20 20 20 20 20 20 20 7b 20 70 48 2d 3e 66 69 72         { pH->fir
b8fe0 73 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20  st = pNew; }.   
b8ff0 20 70 48 65 61 64 2d 3e 70 72 65 76 20 3d 20 70   pHead->prev = p
b9000 4e 65 77 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  New;.  }else{.  
b9010 20 20 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70    pNew->next = p
b9020 48 2d 3e 66 69 72 73 74 3b 0a 20 20 20 20 69 66  H->first;.    if
b9030 28 20 70 48 2d 3e 66 69 72 73 74 20 29 7b 20 70  ( pH->first ){ p
b9040 48 2d 3e 66 69 72 73 74 2d 3e 70 72 65 76 20 3d  H->first->prev =
b9050 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 4e 65   pNew; }.    pNe
b9060 77 2d 3e 70 72 65 76 20 3d 20 30 3b 0a 20 20 20  w->prev = 0;.   
b9070 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65   pH->first = pNe
b9080 77 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 20 52 65  w;.  }.}.../* Re
b9090 73 69 7a 65 20 74 68 65 20 68 61 73 68 20 74 61  size the hash ta
b90a0 62 6c 65 20 73 6f 20 74 68 61 74 20 69 74 20 63  ble so that it c
b90b0 61 6e 74 61 69 6e 73 20 22 6e 65 77 5f 73 69 7a  antains "new_siz
b90c0 65 22 20 62 75 63 6b 65 74 73 2e 0a 2a 2a 0a 2a  e" buckets..**.*
b90d0 2a 20 54 68 65 20 68 61 73 68 20 74 61 62 6c 65  * The hash table
b90e0 20 6d 69 67 68 74 20 66 61 69 6c 20 74 6f 20 72   might fail to r
b90f0 65 73 69 7a 65 20 69 66 20 73 71 6c 69 74 65 33  esize if sqlite3
b9100 5f 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20  _malloc() fails 
b9110 6f 72 0a 2a 2a 20 69 66 20 74 68 65 20 6e 65 77  or.** if the new
b9120 20 73 69 7a 65 20 69 73 20 74 68 65 20 73 61 6d   size is the sam
b9130 65 20 61 73 20 74 68 65 20 70 72 69 6f 72 20 73  e as the prior s
b9140 69 7a 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ize..** Return T
b9150 52 55 45 20 69 66 20 74 68 65 20 72 65 73 69 7a  RUE if the resiz
b9160 65 20 6f 63 63 75 72 73 20 61 6e 64 20 66 61 6c  e occurs and fal
b9170 73 65 20 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74  se if not..*/.st
b9180 61 74 69 63 20 69 6e 74 20 72 65 68 61 73 68 28  atic int rehash(
b9190 48 61 73 68 20 2a 70 48 2c 20 75 6e 73 69 67 6e  Hash *pH, unsign
b91a0 65 64 20 69 6e 74 20 6e 65 77 5f 73 69 7a 65 29  ed int new_size)
b91b0 7b 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20 2a  {.  struct _ht *
b91c0 6e 65 77 5f 68 74 3b 20 20 20 20 20 20 20 20 20  new_ht;         
b91d0 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 68 61     /* The new ha
b91e0 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61  sh table */.  Ha
b91f0 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 2c 20 2a 6e  shElem *elem, *n
b9200 65 78 74 5f 65 6c 65 6d 3b 20 20 20 20 2f 2a 20  ext_elem;    /* 
b9210 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72  For looping over
b9220 20 65 78 69 73 74 69 6e 67 20 65 6c 65 6d 65 6e   existing elemen
b9230 74 73 20 2a 2f 0a 0a 23 69 66 20 53 51 4c 49 54  ts */..#if SQLIT
b9240 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49  E_MALLOC_SOFT_LI
b9250 4d 49 54 3e 30 0a 20 20 69 66 28 20 6e 65 77 5f  MIT>0.  if( new_
b9260 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72 75  size*sizeof(stru
b9270 63 74 20 5f 68 74 29 3e 53 51 4c 49 54 45 5f 4d  ct _ht)>SQLITE_M
b9280 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54  ALLOC_SOFT_LIMIT
b9290 20 29 7b 0a 20 20 20 20 6e 65 77 5f 73 69 7a 65   ){.    new_size
b92a0 20 3d 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43   = SQLITE_MALLOC
b92b0 5f 53 4f 46 54 5f 4c 49 4d 49 54 2f 73 69 7a 65  _SOFT_LIMIT/size
b92c0 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 3b 0a  of(struct _ht);.
b92d0 20 20 7d 0a 20 20 69 66 28 20 6e 65 77 5f 73 69    }.  if( new_si
b92e0 7a 65 3d 3d 70 48 2d 3e 68 74 73 69 7a 65 20 29  ze==pH->htsize )
b92f0 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69   return 0;.#endi
b9300 66 0a 0a 20 20 2f 2a 20 54 68 65 20 69 6e 61 62  f..  /* The inab
b9310 69 6c 69 74 79 20 74 6f 20 61 6c 6c 6f 63 61 74  ility to allocat
b9320 65 73 20 73 70 61 63 65 20 66 6f 72 20 61 20 6c  es space for a l
b9330 61 72 67 65 72 20 68 61 73 68 20 74 61 62 6c 65  arger hash table
b9340 20 69 73 0a 20 20 2a 2a 20 61 20 70 65 72 66 6f   is.  ** a perfo
b9350 72 6d 61 6e 63 65 20 68 69 74 20 62 75 74 20 69  rmance hit but i
b9360 74 20 69 73 20 6e 6f 74 20 61 20 66 61 74 61 6c  t is not a fatal
b9370 20 65 72 72 6f 72 2e 20 20 53 6f 20 6d 61 72 6b   error.  So mark
b9380 20 74 68 65 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61   the.  ** alloca
b9390 74 69 6f 6e 20 61 73 20 61 20 62 65 6e 69 67 6e  tion as a benign
b93a0 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
b93b0 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f  BeginBenignMallo
b93c0 63 28 29 3b 0a 20 20 6e 65 77 5f 68 74 20 3d 20  c();.  new_ht = 
b93d0 28 73 74 72 75 63 74 20 5f 68 74 20 2a 29 73 71  (struct _ht *)sq
b93e0 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 65 77  lite3Malloc( new
b93f0 5f 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72  _size*sizeof(str
b9400 75 63 74 20 5f 68 74 29 20 29 3b 0a 20 20 73 71  uct _ht) );.  sq
b9410 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61  lite3EndBenignMa
b9420 6c 6c 6f 63 28 29 3b 0a 0a 20 20 69 66 28 20 6e  lloc();..  if( n
b9430 65 77 5f 68 74 3d 3d 30 20 29 20 72 65 74 75 72  ew_ht==0 ) retur
b9440 6e 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  n 0;.  sqlite3_f
b9450 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70  ree(pH->ht);.  p
b9460 48 2d 3e 68 74 20 3d 20 6e 65 77 5f 68 74 3b 0a  H->ht = new_ht;.
b9470 20 20 70 48 2d 3e 68 74 73 69 7a 65 20 3d 20 6e    pH->htsize = n
b9480 65 77 5f 73 69 7a 65 20 3d 20 73 71 6c 69 74 65  ew_size = sqlite
b9490 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 6e 65 77 5f  3MallocSize(new_
b94a0 68 74 29 2f 73 69 7a 65 6f 66 28 73 74 72 75 63  ht)/sizeof(struc
b94b0 74 20 5f 68 74 29 3b 0a 20 20 6d 65 6d 73 65 74  t _ht);.  memset
b94c0 28 6e 65 77 5f 68 74 2c 20 30 2c 20 6e 65 77 5f  (new_ht, 0, new_
b94d0 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72 75  size*sizeof(stru
b94e0 63 74 20 5f 68 74 29 29 3b 0a 20 20 66 6f 72 28  ct _ht));.  for(
b94f0 65 6c 65 6d 3d 70 48 2d 3e 66 69 72 73 74 2c 20  elem=pH->first, 
b9500 70 48 2d 3e 66 69 72 73 74 3d 30 3b 20 65 6c 65  pH->first=0; ele
b9510 6d 3b 20 65 6c 65 6d 20 3d 20 6e 65 78 74 5f 65  m; elem = next_e
b9520 6c 65 6d 29 7b 0a 20 20 20 20 75 6e 73 69 67 6e  lem){.    unsign
b9530 65 64 20 69 6e 74 20 68 20 3d 20 73 74 72 48 61  ed int h = strHa
b9540 73 68 28 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 65  sh(elem->pKey, e
b9550 6c 65 6d 2d 3e 6e 4b 65 79 29 20 25 20 6e 65 77  lem->nKey) % new
b9560 5f 73 69 7a 65 3b 0a 20 20 20 20 6e 65 78 74 5f  _size;.    next_
b9570 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78  elem = elem->nex
b9580 74 3b 0a 20 20 20 20 69 6e 73 65 72 74 45 6c 65  t;.    insertEle
b9590 6d 65 6e 74 28 70 48 2c 20 26 6e 65 77 5f 68 74  ment(pH, &new_ht
b95a0 5b 68 5d 2c 20 65 6c 65 6d 29 3b 0a 20 20 7d 0a  [h], elem);.  }.
b95b0 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f    return 1;.}../
b95c0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
b95d0 28 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73  (for internal us
b95e0 65 20 6f 6e 6c 79 29 20 6c 6f 63 61 74 65 73 20  e only) locates 
b95f0 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 20 61 6e  an element in an
b9600 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 74  .** hash table t
b9610 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 20  hat matches the 
b9620 67 69 76 65 6e 20 6b 65 79 2e 20 20 54 68 65 20  given key.  The 
b9630 68 61 73 68 20 66 6f 72 20 74 68 69 73 20 6b 65  hash for this ke
b9640 79 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79  y has.** already
b9650 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 20 61   been computed a
b9660 6e 64 20 69 73 20 70 61 73 73 65 64 20 61 73 20  nd is passed as 
b9670 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65  the 4th paramete
b9680 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 48 61 73  r..*/.static Has
b9690 68 45 6c 65 6d 20 2a 66 69 6e 64 45 6c 65 6d 65  hElem *findEleme
b96a0 6e 74 47 69 76 65 6e 48 61 73 68 28 0a 20 20 63  ntGivenHash(.  c
b96b0 6f 6e 73 74 20 48 61 73 68 20 2a 70 48 2c 20 20  onst Hash *pH,  
b96c0 20 20 20 2f 2a 20 54 68 65 20 70 48 20 74 6f 20     /* The pH to 
b96d0 62 65 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20  be searched */. 
b96e0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4b 65   const char *pKe
b96f0 79 2c 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20  y,   /* The key 
b9700 77 65 20 61 72 65 20 73 65 61 72 63 68 69 6e 67  we are searching
b9710 20 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b   for */.  int nK
b9720 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ey,           /*
b9730 20 42 79 74 65 73 20 69 6e 20 6b 65 79 20 28 6e   Bytes in key (n
b9740 6f 74 20 63 6f 75 6e 74 69 6e 67 20 7a 65 72 6f  ot counting zero
b9750 20 74 65 72 6d 69 6e 61 74 6f 72 29 20 2a 2f 0a   terminator) */.
b9760 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68    unsigned int h
b9770 20 20 20 20 20 20 2f 2a 20 54 68 65 20 68 61 73        /* The has
b9780 68 20 66 6f 72 20 74 68 69 73 20 6b 65 79 2e 20  h for this key. 
b9790 2a 2f 0a 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d  */.){.  HashElem
b97a0 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 20 20   *elem;         
b97b0 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74         /* Used t
b97c0 6f 20 6c 6f 6f 70 20 74 68 72 75 20 74 68 65 20  o loop thru the 
b97d0 65 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a  element list */.
b97e0 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20    int count;    
b97f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b9800 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c   /* Number of el
b9810 65 6d 65 6e 74 73 20 6c 65 66 74 20 74 6f 20 74  ements left to t
b9820 65 73 74 20 2a 2f 0a 0a 20 20 69 66 28 20 70 48  est */..  if( pH
b9830 2d 3e 68 74 20 29 7b 0a 20 20 20 20 73 74 72 75  ->ht ){.    stru
b9840 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 20 3d  ct _ht *pEntry =
b9850 20 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20   &pH->ht[h];.   
b9860 20 65 6c 65 6d 20 3d 20 70 45 6e 74 72 79 2d 3e   elem = pEntry->
b9870 63 68 61 69 6e 3b 0a 20 20 20 20 63 6f 75 6e 74  chain;.    count
b9880 20 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74   = pEntry->count
b9890 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 65  ;.  }else{.    e
b98a0 6c 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b  lem = pH->first;
b98b0 0a 20 20 20 20 63 6f 75 6e 74 20 3d 20 70 48 2d  .    count = pH-
b98c0 3e 63 6f 75 6e 74 3b 0a 20 20 7d 0a 20 20 77 68  >count;.  }.  wh
b98d0 69 6c 65 28 20 63 6f 75 6e 74 2d 2d 20 26 26 20  ile( count-- && 
b98e0 41 4c 57 41 59 53 28 65 6c 65 6d 29 20 29 7b 0a  ALWAYS(elem) ){.
b98f0 20 20 20 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 4b      if( elem->nK
b9900 65 79 3d 3d 6e 4b 65 79 20 26 26 20 73 71 6c 69  ey==nKey && sqli
b9910 74 65 33 53 74 72 4e 49 43 6d 70 28 65 6c 65 6d  te3StrNICmp(elem
b9920 2d 3e 70 4b 65 79 2c 70 4b 65 79 2c 6e 4b 65 79  ->pKey,pKey,nKey
b9930 29 3d 3d 30 20 29 7b 20 0a 20 20 20 20 20 20 72  )==0 ){ .      r
b9940 65 74 75 72 6e 20 65 6c 65 6d 3b 0a 20 20 20 20  eturn elem;.    
b9950 7d 0a 20 20 20 20 65 6c 65 6d 20 3d 20 65 6c 65  }.    elem = ele
b9960 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 72  m->next;.  }.  r
b9970 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 52  eturn 0;.}../* R
b9980 65 6d 6f 76 65 20 61 20 73 69 6e 67 6c 65 20 65  emove a single e
b9990 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 68 61  ntry from the ha
b99a0 73 68 20 74 61 62 6c 65 20 67 69 76 65 6e 20 61  sh table given a
b99b0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74   pointer to that
b99c0 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 61 6e 64 20  .** element and 
b99d0 61 20 68 61 73 68 20 6f 6e 20 74 68 65 20 65 6c  a hash on the el
b99e0 65 6d 65 6e 74 27 73 20 6b 65 79 2e 0a 2a 2f 0a  ement's key..*/.
b99f0 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6d 6f  static void remo
b9a00 76 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61  veElementGivenHa
b9a10 73 68 28 0a 20 20 48 61 73 68 20 2a 70 48 2c 20  sh(.  Hash *pH, 
b9a20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
b9a30 48 20 63 6f 6e 74 61 69 6e 69 6e 67 20 22 65 6c  H containing "el
b9a40 65 6d 22 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65  em" */.  HashEle
b9a50 6d 2a 20 65 6c 65 6d 2c 20 20 20 2f 2a 20 54 68  m* elem,   /* Th
b9a60 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20  e element to be 
b9a70 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65  removed from the
b9a80 20 70 48 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65   pH */.  unsigne
b9a90 64 20 69 6e 74 20 68 20 20 20 20 2f 2a 20 48 61  d int h    /* Ha
b9aa0 73 68 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  sh value for the
b9ab0 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20   element */.){. 
b9ac0 20 73 74 72 75 63 74 20 5f 68 74 20 2a 70 45 6e   struct _ht *pEn
b9ad0 74 72 79 3b 0a 20 20 69 66 28 20 65 6c 65 6d 2d  try;.  if( elem-
b9ae0 3e 70 72 65 76 20 29 7b 0a 20 20 20 20 65 6c 65  >prev ){.    ele
b9af0 6d 2d 3e 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20  m->prev->next = 
b9b00 65 6c 65 6d 2d 3e 6e 65 78 74 3b 20 0a 20 20 7d  elem->next; .  }
b9b10 65 6c 73 65 7b 0a 20 20 20 20 70 48 2d 3e 66 69  else{.    pH->fi
b9b20 72 73 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74  rst = elem->next
b9b30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 65 6c 65 6d  ;.  }.  if( elem
b9b40 2d 3e 6e 65 78 74 20 29 7b 0a 20 20 20 20 65 6c  ->next ){.    el
b9b50 65 6d 2d 3e 6e 65 78 74 2d 3e 70 72 65 76 20 3d  em->next->prev =
b9b60 20 65 6c 65 6d 2d 3e 70 72 65 76 3b 0a 20 20 7d   elem->prev;.  }
b9b70 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29 7b  .  if( pH->ht ){
b9b80 0a 20 20 20 20 70 45 6e 74 72 79 20 3d 20 26 70  .    pEntry = &p
b9b90 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20 20 69 66  H->ht[h];.    if
b9ba0 28 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3d  ( pEntry->chain=
b9bb0 3d 65 6c 65 6d 20 29 7b 0a 20 20 20 20 20 20 70  =elem ){.      p
b9bc0 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 65  Entry->chain = e
b9bd0 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 7d  lem->next;.    }
b9be0 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75  .    pEntry->cou
b9bf0 6e 74 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74  nt--;.    assert
b9c00 28 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3e  ( pEntry->count>
b9c10 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  =0 );.  }.  sqli
b9c20 74 65 33 5f 66 72 65 65 28 20 65 6c 65 6d 20 29  te3_free( elem )
b9c30 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2d 2d 3b  ;.  pH->count--;
b9c40 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74  .  if( pH->count
b9c50 3c 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  <=0 ){.    asser
b9c60 74 28 20 70 48 2d 3e 66 69 72 73 74 3d 3d 30 20  t( pH->first==0 
b9c70 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
b9c80 48 2d 3e 63 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20  H->count==0 );. 
b9c90 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c     sqlite3HashCl
b9ca0 65 61 72 28 70 48 29 3b 0a 20 20 7d 0a 7d 0a 0a  ear(pH);.  }.}..
b9cb0 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f  /* Attempt to lo
b9cc0 63 61 74 65 20 61 6e 20 65 6c 65 6d 65 6e 74 20  cate an element 
b9cd0 6f 66 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  of the hash tabl
b9ce0 65 20 70 48 20 77 69 74 68 20 61 20 6b 65 79 0a  e pH with a key.
b9cf0 2a 2a 20 74 68 61 74 20 6d 61 74 63 68 65 73 20  ** that matches 
b9d00 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 52 65 74 75  pKey,nKey.  Retu
b9d10 72 6e 20 74 68 65 20 64 61 74 61 20 66 6f 72 20  rn the data for 
b9d20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 69 66 20  this element if 
b9d30 69 74 20 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20  it is.** found, 
b9d40 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65  or NULL if there
b9d50 20 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f   is no match..*/
b9d60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b9d70 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73  void *sqlite3Has
b9d80 68 46 69 6e 64 28 63 6f 6e 73 74 20 48 61 73 68  hFind(const Hash
b9d90 20 2a 70 48 2c 20 63 6f 6e 73 74 20 63 68 61 72   *pH, const char
b9da0 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79   *pKey, int nKey
b9db0 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65  ){.  HashElem *e
b9dc0 6c 65 6d 3b 20 20 20 20 2f 2a 20 54 68 65 20 65  lem;    /* The e
b9dd0 6c 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 74 63  lement that matc
b9de0 68 65 73 20 6b 65 79 20 2a 2f 0a 20 20 75 6e 73  hes key */.  uns
b9df0 69 67 6e 65 64 20 69 6e 74 20 68 3b 20 20 20 20  igned int h;    
b9e00 2f 2a 20 41 20 68 61 73 68 20 6f 6e 20 6b 65 79  /* A hash on key
b9e10 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
b9e20 48 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  H!=0 );.  assert
b9e30 28 20 70 4b 65 79 21 3d 30 20 29 3b 0a 20 20 61  ( pKey!=0 );.  a
b9e40 73 73 65 72 74 28 20 6e 4b 65 79 3e 3d 30 20 29  ssert( nKey>=0 )
b9e50 3b 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20 29  ;.  if( pH->ht )
b9e60 7b 0a 20 20 20 20 68 20 3d 20 73 74 72 48 61 73  {.    h = strHas
b9e70 68 28 70 4b 65 79 2c 20 6e 4b 65 79 29 20 25 20  h(pKey, nKey) % 
b9e80 70 48 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 7d 65  pH->htsize;.  }e
b9e90 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 30 3b 0a  lse{.    h = 0;.
b9ea0 20 20 7d 0a 20 20 65 6c 65 6d 20 3d 20 66 69 6e    }.  elem = fin
b9eb0 64 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73  dElementGivenHas
b9ec0 68 28 70 48 2c 20 70 4b 65 79 2c 20 6e 4b 65 79  h(pH, pKey, nKey
b9ed0 2c 20 68 29 3b 0a 20 20 72 65 74 75 72 6e 20 65  , h);.  return e
b9ee0 6c 65 6d 20 3f 20 65 6c 65 6d 2d 3e 64 61 74 61  lem ? elem->data
b9ef0 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 20 49 6e 73 65   : 0;.}../* Inse
b9f00 72 74 20 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e  rt an element in
b9f10 74 6f 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  to the hash tabl
b9f20 65 20 70 48 2e 20 20 54 68 65 20 6b 65 79 20 69  e pH.  The key i
b9f30 73 20 70 4b 65 79 2c 6e 4b 65 79 0a 2a 2a 20 61  s pKey,nKey.** a
b9f40 6e 64 20 74 68 65 20 64 61 74 61 20 69 73 20 22  nd the data is "
b9f50 64 61 74 61 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  data"..**.** If 
b9f60 6e 6f 20 65 6c 65 6d 65 6e 74 20 65 78 69 73 74  no element exist
b9f70 73 20 77 69 74 68 20 61 20 6d 61 74 63 68 69 6e  s with a matchin
b9f80 67 20 6b 65 79 2c 20 74 68 65 6e 20 61 20 6e 65  g key, then a ne
b9f90 77 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 69 73 20  w.** element is 
b9fa0 63 72 65 61 74 65 64 20 61 6e 64 20 4e 55 4c 4c  created and NULL
b9fb0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
b9fc0 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20 65  .** If another e
b9fd0 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 65  lement already e
b9fe0 78 69 73 74 73 20 77 69 74 68 20 74 68 65 20 73  xists with the s
b9ff0 61 6d 65 20 6b 65 79 2c 20 74 68 65 6e 20 74 68  ame key, then th
ba000 65 0a 2a 2a 20 6e 65 77 20 64 61 74 61 20 72 65  e.** new data re
ba010 70 6c 61 63 65 73 20 74 68 65 20 6f 6c 64 20 64  places the old d
ba020 61 74 61 20 61 6e 64 20 74 68 65 20 6f 6c 64 20  ata and the old 
ba030 64 61 74 61 20 69 73 20 72 65 74 75 72 6e 65 64  data is returned
ba040 2e 0a 2a 2a 20 54 68 65 20 6b 65 79 20 69 73 20  ..** The key is 
ba050 6e 6f 74 20 63 6f 70 69 65 64 20 69 6e 20 74 68  not copied in th
ba060 69 73 20 69 6e 73 74 61 6e 63 65 2e 20 20 49 66  is instance.  If
ba070 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2c   a malloc fails,
ba080 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 77   then.** the new
ba090 20 64 61 74 61 20 69 73 20 72 65 74 75 72 6e 65   data is returne
ba0a0 64 20 61 6e 64 20 74 68 65 20 68 61 73 68 20 74  d and the hash t
ba0b0 61 62 6c 65 20 69 73 20 75 6e 63 68 61 6e 67 65  able is unchange
ba0c0 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
ba0d0 22 64 61 74 61 22 20 70 61 72 61 6d 65 74 65 72  "data" parameter
ba0e0 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
ba0f0 6e 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20  n is NULL, then 
ba100 74 68 65 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 63  the.** element c
ba110 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
ba120 22 6b 65 79 22 20 69 73 20 72 65 6d 6f 76 65 64  "key" is removed
ba130 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74   from the hash t
ba140 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  able..*/.SQLITE_
ba150 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
ba160 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
ba170 48 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74 20  Hash *pH, const 
ba180 63 68 61 72 20 2a 70 4b 65 79 2c 20 69 6e 74 20  char *pKey, int 
ba190 6e 4b 65 79 2c 20 76 6f 69 64 20 2a 64 61 74 61  nKey, void *data
ba1a0 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  ){.  unsigned in
ba1b0 74 20 68 3b 20 20 20 20 20 20 20 2f 2a 20 74 68  t h;       /* th
ba1c0 65 20 68 61 73 68 20 6f 66 20 74 68 65 20 6b 65  e hash of the ke
ba1d0 79 20 6d 6f 64 75 6c 6f 20 68 61 73 68 20 74 61  y modulo hash ta
ba1e0 62 6c 65 20 73 69 7a 65 20 2a 2f 0a 20 20 48 61  ble size */.  Ha
ba1f0 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20  shElem *elem;   
ba200 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c      /* Used to l
ba210 6f 6f 70 20 74 68 72 75 20 74 68 65 20 65 6c 65  oop thru the ele
ba220 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 48  ment list */.  H
ba230 61 73 68 45 6c 65 6d 20 2a 6e 65 77 5f 65 6c 65  ashElem *new_ele
ba240 6d 3b 20 20 20 2f 2a 20 4e 65 77 20 65 6c 65 6d  m;   /* New elem
ba250 65 6e 74 20 61 64 64 65 64 20 74 6f 20 74 68 65  ent added to the
ba260 20 70 48 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74   pH */..  assert
ba270 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 61 73 73  ( pH!=0 );.  ass
ba280 65 72 74 28 20 70 4b 65 79 21 3d 30 20 29 3b 0a  ert( pKey!=0 );.
ba290 20 20 61 73 73 65 72 74 28 20 6e 4b 65 79 3e 3d    assert( nKey>=
ba2a0 30 20 29 3b 0a 20 20 69 66 28 20 70 48 2d 3e 68  0 );.  if( pH->h
ba2b0 74 73 69 7a 65 20 29 7b 0a 20 20 20 20 68 20 3d  tsize ){.    h =
ba2c0 20 73 74 72 48 61 73 68 28 70 4b 65 79 2c 20 6e   strHash(pKey, n
ba2d0 4b 65 79 29 20 25 20 70 48 2d 3e 68 74 73 69 7a  Key) % pH->htsiz
ba2e0 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  e;.  }else{.    
ba2f0 68 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 65 6c 65  h = 0;.  }.  ele
ba300 6d 20 3d 20 66 69 6e 64 45 6c 65 6d 65 6e 74 47  m = findElementG
ba310 69 76 65 6e 48 61 73 68 28 70 48 2c 70 4b 65 79  ivenHash(pH,pKey
ba320 2c 6e 4b 65 79 2c 68 29 3b 0a 20 20 69 66 28 20  ,nKey,h);.  if( 
ba330 65 6c 65 6d 20 29 7b 0a 20 20 20 20 76 6f 69 64  elem ){.    void
ba340 20 2a 6f 6c 64 5f 64 61 74 61 20 3d 20 65 6c 65   *old_data = ele
ba350 6d 2d 3e 64 61 74 61 3b 0a 20 20 20 20 69 66 28  m->data;.    if(
ba360 20 64 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20   data==0 ){.    
ba370 20 20 72 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 47    removeElementG
ba380 69 76 65 6e 48 61 73 68 28 70 48 2c 65 6c 65 6d  ivenHash(pH,elem
ba390 2c 68 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ,h);.    }else{.
ba3a0 20 20 20 20 20 20 65 6c 65 6d 2d 3e 64 61 74 61        elem->data
ba3b0 20 3d 20 64 61 74 61 3b 0a 20 20 20 20 20 20 65   = data;.      e
ba3c0 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 70 4b 65 79  lem->pKey = pKey
ba3d0 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 6e  ;.      assert(n
ba3e0 4b 65 79 3d 3d 65 6c 65 6d 2d 3e 6e 4b 65 79 29  Key==elem->nKey)
ba3f0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
ba400 72 6e 20 6f 6c 64 5f 64 61 74 61 3b 0a 20 20 7d  rn old_data;.  }
ba410 0a 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20 29  .  if( data==0 )
ba420 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 6e 65 77   return 0;.  new
ba430 5f 65 6c 65 6d 20 3d 20 28 48 61 73 68 45 6c 65  _elem = (HashEle
ba440 6d 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  m*)sqlite3Malloc
ba450 28 20 73 69 7a 65 6f 66 28 48 61 73 68 45 6c 65  ( sizeof(HashEle
ba460 6d 29 20 29 3b 0a 20 20 69 66 28 20 6e 65 77 5f  m) );.  if( new_
ba470 65 6c 65 6d 3d 3d 30 20 29 20 72 65 74 75 72 6e  elem==0 ) return
ba480 20 64 61 74 61 3b 0a 20 20 6e 65 77 5f 65 6c 65   data;.  new_ele
ba490 6d 2d 3e 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a  m->pKey = pKey;.
ba4a0 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 6e 4b 65 79    new_elem->nKey
ba4b0 20 3d 20 6e 4b 65 79 3b 0a 20 20 6e 65 77 5f 65   = nKey;.  new_e
ba4c0 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61  lem->data = data
ba4d0 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2b 2b 3b  ;.  pH->count++;
ba4e0 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74  .  if( pH->count
ba4f0 3e 3d 31 30 20 26 26 20 70 48 2d 3e 63 6f 75 6e  >=10 && pH->coun
ba500 74 20 3e 20 32 2a 70 48 2d 3e 68 74 73 69 7a 65  t > 2*pH->htsize
ba510 20 29 7b 0a 20 20 20 20 69 66 28 20 72 65 68 61   ){.    if( reha
ba520 73 68 28 70 48 2c 20 70 48 2d 3e 63 6f 75 6e 74  sh(pH, pH->count
ba530 2a 32 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73  *2) ){.      ass
ba540 65 72 74 28 20 70 48 2d 3e 68 74 73 69 7a 65 3e  ert( pH->htsize>
ba550 30 20 29 3b 0a 20 20 20 20 20 20 68 20 3d 20 73  0 );.      h = s
ba560 74 72 48 61 73 68 28 70 4b 65 79 2c 20 6e 4b 65  trHash(pKey, nKe
ba570 79 29 20 25 20 70 48 2d 3e 68 74 73 69 7a 65 3b  y) % pH->htsize;
ba580 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
ba590 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20 69   pH->ht ){.    i
ba5a0 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c  nsertElement(pH,
ba5b0 20 26 70 48 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77   &pH->ht[h], new
ba5c0 5f 65 6c 65 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b  _elem);.  }else{
ba5d0 0a 20 20 20 20 69 6e 73 65 72 74 45 6c 65 6d 65  .    insertEleme
ba5e0 6e 74 28 70 48 2c 20 30 2c 20 6e 65 77 5f 65 6c  nt(pH, 0, new_el
ba5f0 65 6d 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  em);.  }.  retur
ba600 6e 20 30 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  n 0;.}../*******
ba610 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68  ******* End of h
ba620 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ash.c **********
ba630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba650 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
ba660 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
ba670 6c 65 20 6f 70 63 6f 64 65 73 2e 63 20 2a 2a 2a  le opcodes.c ***
ba680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ba6a0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 41 75 74 6f 6d  ******/./* Autom
ba6b0 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74  atically generat
ba6c0 65 64 2e 20 20 44 6f 20 6e 6f 74 20 65 64 69 74  ed.  Do not edit
ba6d0 20 2a 2f 0a 2f 2a 20 53 65 65 20 74 68 65 20 6d   */./* See the m
ba6e0 6b 6f 70 63 6f 64 65 63 2e 61 77 6b 20 73 63 72  kopcodec.awk scr
ba6f0 69 70 74 20 66 6f 72 20 64 65 74 61 69 6c 73 2e  ipt for details.
ba700 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64   */.#if !defined
ba710 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50  (SQLITE_OMIT_EXP
ba720 4c 41 49 4e 29 20 7c 7c 20 21 64 65 66 69 6e 65  LAIN) || !define
ba730 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66  d(NDEBUG) || def
ba740 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46 49 4c  ined(VDBE_PROFIL
ba750 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
ba760 4c 49 54 45 5f 44 45 42 55 47 29 0a 53 51 4c 49  LITE_DEBUG).SQLI
ba770 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
ba780 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4f 70   char *sqlite3Op
ba790 63 6f 64 65 4e 61 6d 65 28 69 6e 74 20 69 29 7b  codeName(int i){
ba7a0 0a 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  . static const c
ba7b0 68 61 72 20 2a 63 6f 6e 73 74 20 61 7a 4e 61 6d  har *const azNam
ba7c0 65 5b 5d 20 3d 20 7b 20 22 3f 22 2c 0a 20 20 20  e[] = { "?",.   
ba7d0 20 20 2f 2a 20 20 20 31 20 2a 2f 20 22 52 65 61    /*   1 */ "Rea
ba7e0 64 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f  dCookie",.     /
ba7f0 2a 20 20 20 32 20 2a 2f 20 22 41 75 74 6f 43 6f  *   2 */ "AutoCo
ba800 6d 6d 69 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  mmit",.     /*  
ba810 20 33 20 2a 2f 20 22 46 6f 75 6e 64 22 2c 0a 20   3 */ "Found",. 
ba820 20 20 20 20 2f 2a 20 20 20 34 20 2a 2f 20 22 4e      /*   4 */ "N
ba830 75 6c 6c 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a  ullRow",.     /*
ba840 20 20 20 35 20 2a 2f 20 22 52 6f 77 53 65 74 54     5 */ "RowSetT
ba850 65 73 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20  est",.     /*   
ba860 36 20 2a 2f 20 22 56 61 72 69 61 62 6c 65 22 2c  6 */ "Variable",
ba870 0a 20 20 20 20 20 2f 2a 20 20 20 37 20 2a 2f 20  .     /*   7 */ 
ba880 22 52 65 61 6c 41 66 66 69 6e 69 74 79 22 2c 0a  "RealAffinity",.
ba890 20 20 20 20 20 2f 2a 20 20 20 38 20 2a 2f 20 22       /*   8 */ "
ba8a0 53 6f 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  Sort",.     /*  
ba8b0 20 39 20 2a 2f 20 22 41 66 66 69 6e 69 74 79 22   9 */ "Affinity"
ba8c0 2c 0a 20 20 20 20 20 2f 2a 20 20 31 30 20 2a 2f  ,.     /*  10 */
ba8d0 20 22 49 66 4e 6f 74 22 2c 0a 20 20 20 20 20 2f   "IfNot",.     /
ba8e0 2a 20 20 31 31 20 2a 2f 20 22 47 6f 73 75 62 22  *  11 */ "Gosub"
ba8f0 2c 0a 20 20 20 20 20 2f 2a 20 20 31 32 20 2a 2f  ,.     /*  12 */
ba900 20 22 4e 6f 74 46 6f 75 6e 64 22 2c 0a 20 20 20   "NotFound",.   
ba910 20 20 2f 2a 20 20 31 33 20 2a 2f 20 22 52 65 73    /*  13 */ "Res
ba920 75 6c 74 52 6f 77 22 2c 0a 20 20 20 20 20 2f 2a  ultRow",.     /*
ba930 20 20 31 34 20 2a 2f 20 22 53 65 65 6b 4c 65 22    14 */ "SeekLe"
ba940 2c 0a 20 20 20 20 20 2f 2a 20 20 31 35 20 2a 2f  ,.     /*  15 */
ba950 20 22 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f   "Rowid",.     /
ba960 2a 20 20 31 36 20 2a 2f 20 22 43 72 65 61 74 65  *  16 */ "Create
ba970 49 6e 64 65 78 22 2c 0a 20 20 20 20 20 2f 2a 20  Index",.     /* 
ba980 20 31 37 20 2a 2f 20 22 45 78 70 6c 61 69 6e 22   17 */ "Explain"
ba990 2c 0a 20 20 20 20 20 2f 2a 20 20 31 38 20 2a 2f  ,.     /*  18 */
ba9a0 20 22 44 72 6f 70 49 6e 64 65 78 22 2c 0a 20 20   "DropIndex",.  
ba9b0 20 20 20 2f 2a 20 20 31 39 20 2a 2f 20 22 4e 6f     /*  19 */ "No
ba9c0 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 30 20  t",.     /*  20 
ba9d0 2a 2f 20 22 4e 75 6c 6c 22 2c 0a 20 20 20 20 20  */ "Null",.     
ba9e0 2f 2a 20 20 32 31 20 2a 2f 20 22 50 72 6f 67 72  /*  21 */ "Progr
ba9f0 61 6d 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 32  am",.     /*  22
baa00 20 2a 2f 20 22 49 6e 74 36 34 22 2c 0a 20 20 20   */ "Int64",.   
baa10 20 20 2f 2a 20 20 32 33 20 2a 2f 20 22 4c 6f 61    /*  23 */ "Loa
baa20 64 41 6e 61 6c 79 73 69 73 22 2c 0a 20 20 20 20  dAnalysis",.    
baa30 20 2f 2a 20 20 32 34 20 2a 2f 20 22 49 64 78 49   /*  24 */ "IdxI
baa40 6e 73 65 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20  nsert",.     /* 
baa50 20 32 35 20 2a 2f 20 22 56 55 70 64 61 74 65 22   25 */ "VUpdate"
baa60 2c 0a 20 20 20 20 20 2f 2a 20 20 32 36 20 2a 2f  ,.     /*  26 */
baa70 20 22 4e 65 78 74 22 2c 0a 20 20 20 20 20 2f 2a   "Next",.     /*
baa80 20 20 32 37 20 2a 2f 20 22 53 65 65 6b 4c 74 22    27 */ "SeekLt"
baa90 2c 0a 20 20 20 20 20 2f 2a 20 20 32 38 20 2a 2f  ,.     /*  28 */
baaa0 20 22 52 65 77 69 6e 64 22 2c 0a 20 20 20 20 20   "Rewind",.     
baab0 2f 2a 20 20 32 39 20 2a 2f 20 22 52 6f 77 53 65  /*  29 */ "RowSe
baac0 74 52 65 61 64 22 2c 0a 20 20 20 20 20 2f 2a 20  tRead",.     /* 
baad0 20 33 30 20 2a 2f 20 22 4c 61 73 74 22 2c 0a 20   30 */ "Last",. 
baae0 20 20 20 20 2f 2a 20 20 33 31 20 2a 2f 20 22 4d      /*  31 */ "M
baaf0 75 73 74 42 65 49 6e 74 22 2c 0a 20 20 20 20 20  ustBeInt",.     
bab00 2f 2a 20 20 33 32 20 2a 2f 20 22 49 6e 63 72 56  /*  32 */ "IncrV
bab10 61 63 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a 20  acuum",.     /* 
bab20 20 33 33 20 2a 2f 20 22 53 74 72 69 6e 67 22 2c   33 */ "String",
bab30 0a 20 20 20 20 20 2f 2a 20 20 33 34 20 2a 2f 20  .     /*  34 */ 
bab40 22 56 46 69 6c 74 65 72 22 2c 0a 20 20 20 20 20  "VFilter",.     
bab50 2f 2a 20 20 33 35 20 2a 2f 20 22 43 6f 75 6e 74  /*  35 */ "Count
bab60 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 36 20 2a  ",.     /*  36 *
bab70 2f 20 22 43 6c 6f 73 65 22 2c 0a 20 20 20 20 20  / "Close",.     
bab80 2f 2a 20 20 33 37 20 2a 2f 20 22 41 67 67 46 69  /*  37 */ "AggFi
bab90 6e 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33  nal",.     /*  3
baba0 38 20 2a 2f 20 22 52 6f 77 44 61 74 61 22 2c 0a  8 */ "RowData",.
babb0 20 20 20 20 20 2f 2a 20 20 33 39 20 2a 2f 20 22       /*  39 */ "
babc0 49 64 78 52 6f 77 69 64 22 2c 0a 20 20 20 20 20  IdxRowid",.     
babd0 2f 2a 20 20 34 30 20 2a 2f 20 22 50 61 72 61 6d  /*  40 */ "Param
babe0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 31 20 2a  ",.     /*  41 *
babf0 2f 20 22 50 61 67 65 63 6f 75 6e 74 22 2c 0a 20  / "Pagecount",. 
bac00 20 20 20 20 2f 2a 20 20 34 32 20 2a 2f 20 22 53      /*  42 */ "S
bac10 65 65 6b 47 65 22 2c 0a 20 20 20 20 20 2f 2a 20  eekGe",.     /* 
bac20 20 34 33 20 2a 2f 20 22 4f 70 65 6e 50 73 65 75   43 */ "OpenPseu
bac30 64 6f 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34 34  do",.     /*  44
bac40 20 2a 2f 20 22 48 61 6c 74 22 2c 0a 20 20 20 20   */ "Halt",.    
bac50 20 2f 2a 20 20 34 35 20 2a 2f 20 22 43 6f 6d 70   /*  45 */ "Comp
bac60 61 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34  are",.     /*  4
bac70 36 20 2a 2f 20 22 4e 65 77 52 6f 77 69 64 22 2c  6 */ "NewRowid",
bac80 0a 20 20 20 20 20 2f 2a 20 20 34 37 20 2a 2f 20  .     /*  47 */ 
bac90 22 49 64 78 4c 54 22 2c 0a 20 20 20 20 20 2f 2a  "IdxLT",.     /*
baca0 20 20 34 38 20 2a 2f 20 22 53 65 65 6b 47 74 22    48 */ "SeekGt"
bacb0 2c 0a 20 20 20 20 20 2f 2a 20 20 34 39 20 2a 2f  ,.     /*  49 */
bacc0 20 22 4d 65 6d 4d 61 78 22 2c 0a 20 20 20 20 20   "MemMax",.     
bacd0 2f 2a 20 20 35 30 20 2a 2f 20 22 46 75 6e 63 74  /*  50 */ "Funct
bace0 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35  ion",.     /*  5
bacf0 31 20 2a 2f 20 22 49 6e 74 65 67 72 69 74 79 43  1 */ "IntegrityC
bad00 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 32 20  k",.     /*  52 
bad10 2a 2f 20 22 46 6b 43 6f 75 6e 74 65 72 22 2c 0a  */ "FkCounter",.
bad20 20 20 20 20 20 2f 2a 20 20 35 33 20 2a 2f 20 22       /*  53 */ "
bad30 53 43 6f 70 79 22 2c 0a 20 20 20 20 20 2f 2a 20  SCopy",.     /* 
bad40 20 35 34 20 2a 2f 20 22 49 66 4e 65 67 22 2c 0a   54 */ "IfNeg",.
bad50 20 20 20 20 20 2f 2a 20 20 35 35 20 2a 2f 20 22       /*  55 */ "
bad60 4e 6f 74 45 78 69 73 74 73 22 2c 0a 20 20 20 20  NotExists",.    
bad70 20 2f 2a 20 20 35 36 20 2a 2f 20 22 56 44 65 73   /*  56 */ "VDes
bad80 74 72 6f 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20  troy",.     /*  
bad90 35 37 20 2a 2f 20 22 49 64 78 44 65 6c 65 74 65  57 */ "IdxDelete
bada0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 38 20 2a  ",.     /*  58 *
badb0 2f 20 22 56 61 63 75 75 6d 22 2c 0a 20 20 20 20  / "Vacuum",.    
badc0 20 2f 2a 20 20 35 39 20 2a 2f 20 22 43 6f 70 79   /*  59 */ "Copy
badd0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 30 20 2a  ",.     /*  60 *
bade0 2f 20 22 49 66 22 2c 0a 20 20 20 20 20 2f 2a 20  / "If",.     /* 
badf0 20 36 31 20 2a 2f 20 22 4a 75 6d 70 22 2c 0a 20   61 */ "Jump",. 
bae00 20 20 20 20 2f 2a 20 20 36 32 20 2a 2f 20 22 44      /*  62 */ "D
bae10 65 73 74 72 6f 79 22 2c 0a 20 20 20 20 20 2f 2a  estroy",.     /*
bae20 20 20 36 33 20 2a 2f 20 22 41 67 67 53 74 65 70    63 */ "AggStep
bae30 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 34 20 2a  ",.     /*  64 *
bae40 2f 20 22 43 6c 65 61 72 22 2c 0a 20 20 20 20 20  / "Clear",.     
bae50 2f 2a 20 20 36 35 20 2a 2f 20 22 49 6e 73 65 72  /*  65 */ "Inser
bae60 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 36 20  t",.     /*  66 
bae70 2a 2f 20 22 50 65 72 6d 75 74 61 74 69 6f 6e 22  */ "Permutation"
bae80 2c 0a 20 20 20 20 20 2f 2a 20 20 36 37 20 2a 2f  ,.     /*  67 */
bae90 20 22 56 42 65 67 69 6e 22 2c 0a 20 20 20 20 20   "VBegin",.     
baea0 2f 2a 20 20 36 38 20 2a 2f 20 22 4f 72 22 2c 0a  /*  68 */ "Or",.
baeb0 20 20 20 20 20 2f 2a 20 20 36 39 20 2a 2f 20 22       /*  69 */ "
baec0 41 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37  And",.     /*  7
baed0 30 20 2a 2f 20 22 4f 70 65 6e 45 70 68 65 6d 65  0 */ "OpenEpheme
baee0 72 61 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37  ral",.     /*  7
baef0 31 20 2a 2f 20 22 49 64 78 47 45 22 2c 0a 20 20  1 */ "IdxGE",.  
baf00 20 20 20 2f 2a 20 20 37 32 20 2a 2f 20 22 54 72     /*  72 */ "Tr
baf10 61 63 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37  ace",.     /*  7
baf20 33 20 2a 2f 20 22 49 73 4e 75 6c 6c 22 2c 0a 20  3 */ "IsNull",. 
baf30 20 20 20 20 2f 2a 20 20 37 34 20 2a 2f 20 22 4e      /*  74 */ "N
baf40 6f 74 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a  otNull",.     /*
baf50 20 20 37 35 20 2a 2f 20 22 4e 65 22 2c 0a 20 20    75 */ "Ne",.  
baf60 20 20 20 2f 2a 20 20 37 36 20 2a 2f 20 22 45 71     /*  76 */ "Eq
baf70 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 37 20 2a  ",.     /*  77 *
baf80 2f 20 22 47 74 22 2c 0a 20 20 20 20 20 2f 2a 20  / "Gt",.     /* 
baf90 20 37 38 20 2a 2f 20 22 4c 65 22 2c 0a 20 20 20   78 */ "Le",.   
bafa0 20 20 2f 2a 20 20 37 39 20 2a 2f 20 22 4c 74 22    /*  79 */ "Lt"
bafb0 2c 0a 20 20 20 20 20 2f 2a 20 20 38 30 20 2a 2f  ,.     /*  80 */
bafc0 20 22 47 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20   "Ge",.     /*  
bafd0 38 31 20 2a 2f 20 22 4d 61 6b 65 52 65 63 6f 72  81 */ "MakeRecor
bafe0 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 32 20  d",.     /*  82 
baff0 2a 2f 20 22 42 69 74 41 6e 64 22 2c 0a 20 20 20  */ "BitAnd",.   
bb000 20 20 2f 2a 20 20 38 33 20 2a 2f 20 22 42 69 74    /*  83 */ "Bit
bb010 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 34  Or",.     /*  84
bb020 20 2a 2f 20 22 53 68 69 66 74 4c 65 66 74 22 2c   */ "ShiftLeft",
bb030 0a 20 20 20 20 20 2f 2a 20 20 38 35 20 2a 2f 20  .     /*  85 */ 
bb040 22 53 68 69 66 74 52 69 67 68 74 22 2c 0a 20 20  "ShiftRight",.  
bb050 20 20 20 2f 2a 20 20 38 36 20 2a 2f 20 22 41 64     /*  86 */ "Ad
bb060 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 37 20  d",.     /*  87 
bb070 2a 2f 20 22 53 75 62 74 72 61 63 74 22 2c 0a 20  */ "Subtract",. 
bb080 20 20 20 20 2f 2a 20 20 38 38 20 2a 2f 20 22 4d      /*  88 */ "M
bb090 75 6c 74 69 70 6c 79 22 2c 0a 20 20 20 20 20 2f  ultiply",.     /
bb0a0 2a 20 20 38 39 20 2a 2f 20 22 44 69 76 69 64 65  *  89 */ "Divide
bb0b0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 30 20 2a  ",.     /*  90 *
bb0c0 2f 20 22 52 65 6d 61 69 6e 64 65 72 22 2c 0a 20  / "Remainder",. 
bb0d0 20 20 20 20 2f 2a 20 20 39 31 20 2a 2f 20 22 43      /*  91 */ "C
bb0e0 6f 6e 63 61 74 22 2c 0a 20 20 20 20 20 2f 2a 20  oncat",.     /* 
bb0f0 20 39 32 20 2a 2f 20 22 59 69 65 6c 64 22 2c 0a   92 */ "Yield",.
bb100 20 20 20 20 20 2f 2a 20 20 39 33 20 2a 2f 20 22       /*  93 */ "
bb110 42 69 74 4e 6f 74 22 2c 0a 20 20 20 20 20 2f 2a  BitNot",.     /*
bb120 20 20 39 34 20 2a 2f 20 22 53 74 72 69 6e 67 38    94 */ "String8
bb130 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 35 20 2a  ",.     /*  95 *
bb140 2f 20 22 53 65 74 43 6f 6f 6b 69 65 22 2c 0a 20  / "SetCookie",. 
bb150 20 20 20 20 2f 2a 20 20 39 36 20 2a 2f 20 22 50      /*  96 */ "P
bb160 72 65 76 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39  rev",.     /*  9
bb170 37 20 2a 2f 20 22 44 72 6f 70 54 72 69 67 67 65  7 */ "DropTrigge
bb180 72 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 38 20  r",.     /*  98 
bb190 2a 2f 20 22 46 6b 49 66 5a 65 72 6f 22 2c 0a 20  */ "FkIfZero",. 
bb1a0 20 20 20 20 2f 2a 20 20 39 39 20 2a 2f 20 22 56      /*  99 */ "V
bb1b0 43 6f 6c 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a  Column",.     /*
bb1c0 20 31 30 30 20 2a 2f 20 22 52 65 74 75 72 6e 22   100 */ "Return"
bb1d0 2c 0a 20 20 20 20 20 2f 2a 20 31 30 31 20 2a 2f  ,.     /* 101 */
bb1e0 20 22 4f 70 65 6e 57 72 69 74 65 22 2c 0a 20 20   "OpenWrite",.  
bb1f0 20 20 20 2f 2a 20 31 30 32 20 2a 2f 20 22 49 6e     /* 102 */ "In
bb200 74 65 67 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20  teger",.     /* 
bb210 31 30 33 20 2a 2f 20 22 54 72 61 6e 73 61 63 74  103 */ "Transact
bb220 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30  ion",.     /* 10
bb230 34 20 2a 2f 20 22 49 66 50 6f 73 22 2c 0a 20 20  4 */ "IfPos",.  
bb240 20 20 20 2f 2a 20 31 30 35 20 2a 2f 20 22 52 6f     /* 105 */ "Ro
bb250 77 53 65 74 41 64 64 22 2c 0a 20 20 20 20 20 2f  wSetAdd",.     /
bb260 2a 20 31 30 36 20 2a 2f 20 22 43 6f 6c 6c 53 65  * 106 */ "CollSe
bb270 71 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 37 20  q",.     /* 107 
bb280 2a 2f 20 22 53 61 76 65 70 6f 69 6e 74 22 2c 0a  */ "Savepoint",.
bb290 20 20 20 20 20 2f 2a 20 31 30 38 20 2a 2f 20 22       /* 108 */ "
bb2a0 56 52 65 6e 61 6d 65 22 2c 0a 20 20 20 20 20 2f  VRename",.     /
bb2b0 2a 20 31 30 39 20 2a 2f 20 22 53 65 71 75 65 6e  * 109 */ "Sequen
bb2c0 63 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 30  ce",.     /* 110
bb2d0 20 2a 2f 20 22 48 61 6c 74 49 66 4e 75 6c 6c 22   */ "HaltIfNull"
bb2e0 2c 0a 20 20 20 20 20 2f 2a 20 31 31 31 20 2a 2f  ,.     /* 111 */
bb2f0 20 22 56 43 72 65 61 74 65 22 2c 0a 20 20 20 20   "VCreate",.    
bb300 20 2f 2a 20 31 31 32 20 2a 2f 20 22 43 72 65 61   /* 112 */ "Crea
bb310 74 65 54 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f  teTable",.     /
bb320 2a 20 31 31 33 20 2a 2f 20 22 41 64 64 49 6d 6d  * 113 */ "AddImm
bb330 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 34 20 2a  ",.     /* 114 *
bb340 2f 20 22 44 72 6f 70 54 61 62 6c 65 22 2c 0a 20  / "DropTable",. 
bb350 20 20 20 20 2f 2a 20 31 31 35 20 2a 2f 20 22 49      /* 115 */ "I
bb360 73 55 6e 69 71 75 65 22 2c 0a 20 20 20 20 20 2f  sUnique",.     /
bb370 2a 20 31 31 36 20 2a 2f 20 22 56 4f 70 65 6e 22  * 116 */ "VOpen"
bb380 2c 0a 20 20 20 20 20 2f 2a 20 31 31 37 20 2a 2f  ,.     /* 117 */
bb390 20 22 49 66 5a 65 72 6f 22 2c 0a 20 20 20 20 20   "IfZero",.     
bb3a0 2f 2a 20 31 31 38 20 2a 2f 20 22 4e 6f 6f 70 22  /* 118 */ "Noop"
bb3b0 2c 0a 20 20 20 20 20 2f 2a 20 31 31 39 20 2a 2f  ,.     /* 119 */
bb3c0 20 22 49 6e 73 65 72 74 49 6e 74 22 2c 0a 20 20   "InsertInt",.  
bb3d0 20 20 20 2f 2a 20 31 32 30 20 2a 2f 20 22 52 6f     /* 120 */ "Ro
bb3e0 77 4b 65 79 22 2c 0a 20 20 20 20 20 2f 2a 20 31  wKey",.     /* 1
bb3f0 32 31 20 2a 2f 20 22 45 78 70 69 72 65 22 2c 0a  21 */ "Expire",.
bb400 20 20 20 20 20 2f 2a 20 31 32 32 20 2a 2f 20 22       /* 122 */ "
bb410 44 65 6c 65 74 65 22 2c 0a 20 20 20 20 20 2f 2a  Delete",.     /*
bb420 20 31 32 33 20 2a 2f 20 22 42 6c 6f 62 22 2c 0a   123 */ "Blob",.
bb430 20 20 20 20 20 2f 2a 20 31 32 34 20 2a 2f 20 22       /* 124 */ "
bb440 4d 6f 76 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31  Move",.     /* 1
bb450 32 35 20 2a 2f 20 22 47 6f 74 6f 22 2c 0a 20 20  25 */ "Goto",.  
bb460 20 20 20 2f 2a 20 31 32 36 20 2a 2f 20 22 50 61     /* 126 */ "Pa
bb470 72 73 65 53 63 68 65 6d 61 22 2c 0a 20 20 20 20  rseSchema",.    
bb480 20 2f 2a 20 31 32 37 20 2a 2f 20 22 56 4e 65 78   /* 127 */ "VNex
bb490 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 38 20  t",.     /* 128 
bb4a0 2a 2f 20 22 53 65 65 6b 22 2c 0a 20 20 20 20 20  */ "Seek",.     
bb4b0 2f 2a 20 31 32 39 20 2a 2f 20 22 54 61 62 6c 65  /* 129 */ "Table
bb4c0 4c 6f 63 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 31  Lock",.     /* 1
bb4d0 33 30 20 2a 2f 20 22 52 65 61 6c 22 2c 0a 20 20  30 */ "Real",.  
bb4e0 20 20 20 2f 2a 20 31 33 31 20 2a 2f 20 22 56 65     /* 131 */ "Ve
bb4f0 72 69 66 79 43 6f 6f 6b 69 65 22 2c 0a 20 20 20  rifyCookie",.   
bb500 20 20 2f 2a 20 31 33 32 20 2a 2f 20 22 43 6f 6c    /* 132 */ "Col
bb510 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33  umn",.     /* 13
bb520 33 20 2a 2f 20 22 4f 70 65 6e 52 65 61 64 22 2c  3 */ "OpenRead",
bb530 0a 20 20 20 20 20 2f 2a 20 31 33 34 20 2a 2f 20  .     /* 134 */ 
bb540 22 52 65 73 65 74 43 6f 75 6e 74 22 2c 0a 20 20  "ResetCount",.  
bb550 20 20 20 2f 2a 20 31 33 35 20 2a 2f 20 22 4e 6f     /* 135 */ "No
bb560 74 55 73 65 64 5f 31 33 35 22 2c 0a 20 20 20 20  tUsed_135",.    
bb570 20 2f 2a 20 31 33 36 20 2a 2f 20 22 4e 6f 74 55   /* 136 */ "NotU
bb580 73 65 64 5f 31 33 36 22 2c 0a 20 20 20 20 20 2f  sed_136",.     /
bb590 2a 20 31 33 37 20 2a 2f 20 22 4e 6f 74 55 73 65  * 137 */ "NotUse
bb5a0 64 5f 31 33 37 22 2c 0a 20 20 20 20 20 2f 2a 20  d_137",.     /* 
bb5b0 31 33 38 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f  138 */ "NotUsed_
bb5c0 31 33 38 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33  138",.     /* 13
bb5d0 39 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33  9 */ "NotUsed_13
bb5e0 39 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 30 20  9",.     /* 140 
bb5f0 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 34 30 22  */ "NotUsed_140"
bb600 2c 0a 20 20 20 20 20 2f 2a 20 31 34 31 20 2a 2f  ,.     /* 141 */
bb610 20 22 54 6f 54 65 78 74 22 2c 0a 20 20 20 20 20   "ToText",.     
bb620 2f 2a 20 31 34 32 20 2a 2f 20 22 54 6f 42 6c 6f  /* 142 */ "ToBlo
bb630 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 33 20  b",.     /* 143 
bb640 2a 2f 20 22 54 6f 4e 75 6d 65 72 69 63 22 2c 0a  */ "ToNumeric",.
bb650 20 20 20 20 20 2f 2a 20 31 34 34 20 2a 2f 20 22       /* 144 */ "
bb660 54 6f 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20  ToInt",.     /* 
bb670 31 34 35 20 2a 2f 20 22 54 6f 52 65 61 6c 22 2c  145 */ "ToReal",
bb680 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20 61  .  };.  return a
bb690 7a 4e 61 6d 65 5b 69 5d 3b 0a 7d 0a 23 65 6e 64  zName[i];.}.#end
bb6a0 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  if../***********
bb6b0 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64  *** End of opcod
bb6c0 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  es.c ***********
bb6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb6f0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
bb700 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f  *** Begin file o
bb710 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  s_os2.c ********
bb720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb740 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 46  **/./*.** 2006 F
bb750 65 62 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20  eb 14.**.** The 
bb760 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
bb770 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
bb780 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
bb790 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
bb7a0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
bb7b0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
bb7c0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
bb7d0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
bb7e0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
bb7f0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
bb800 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
bb810 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
bb820 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
bb830 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
bb840 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
bb850 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
bb860 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
bb870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb8a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bb8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
bb8c0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
bb8d0 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  ns code that is 
bb8e0 73 70 65 63 69 66 69 63 20 74 6f 20 4f 53 2f 32  specific to OS/2
bb8f0 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f  ..**.** $Id: os_
bb900 6f 73 32 2e 63 2c 76 20 31 2e 36 33 20 32 30 30  os2.c,v 1.63 200
bb910 38 2f 31 32 2f 31 30 20 31 39 3a 32 36 3a 32 34  8/12/10 19:26:24
bb920 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a   drh Exp $.*/...
bb930 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53  #if SQLITE_OS_OS
bb940 32 0a 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20  2../*.** A Note 
bb950 41 62 6f 75 74 20 4d 65 6d 6f 72 79 20 41 6c 6c  About Memory All
bb960 6f 63 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54  ocation:.**.** T
bb970 68 69 73 20 64 72 69 76 65 72 20 75 73 65 73 20  his driver uses 
bb980 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 20  malloc()/free() 
bb990 64 69 72 65 63 74 6c 79 20 72 61 74 68 65 72 20  directly rather 
bb9a0 74 68 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75  than going throu
bb9b0 67 68 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65  gh.** the SQLite
bb9c0 2d 77 72 61 70 70 65 72 73 20 73 71 6c 69 74 65  -wrappers sqlite
bb9d0 33 5f 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c 69 74  3_malloc()/sqlit
bb9e0 65 33 5f 66 72 65 65 28 29 2e 20 20 54 68 6f 73  e3_free().  Thos
bb9f0 65 20 77 72 61 70 70 65 72 73 0a 2a 2a 20 61 72  e wrappers.** ar
bba00 65 20 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75  e designed for u
bba10 73 65 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 73  se on embedded s
bba20 79 73 74 65 6d 73 20 77 68 65 72 65 20 6d 65 6d  ystems where mem
bba30 6f 72 79 20 69 73 20 73 63 61 72 63 65 20 61 6e  ory is scarce an
bba40 64 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  d.** malloc fail
bba50 75 72 65 73 20 68 61 70 70 65 6e 20 66 72 65 71  ures happen freq
bba60 75 65 6e 74 6c 79 2e 20 20 4f 53 2f 32 20 64 6f  uently.  OS/2 do
bba70 65 73 20 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79  es not typically
bba80 20 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64   run on.** embed
bba90 64 65 64 20 73 79 73 74 65 6d 73 2c 20 61 6e 64  ded systems, and
bbaa0 20 77 68 65 6e 20 69 74 20 64 6f 65 73 20 74 68   when it does th
bbab0 65 20 64 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72  e developers nor
bbac0 6d 61 6c 6c 79 20 68 61 76 65 20 62 69 67 67 65  mally have bigge
bbad0 72 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f  r.** problems to
bbae0 20 77 6f 72 72 79 20 61 62 6f 75 74 20 74 68 61   worry about tha
bbaf0 6e 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66  n running out of
bbb00 20 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65   memory.  So the
bbb10 72 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63  re is not.** a c
bbb20 6f 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74  ompelling need t
bbb30 6f 20 75 73 65 20 74 68 65 20 77 72 61 70 70 65  o use the wrappe
bbb40 72 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68  rs..**.** But th
bbb50 65 72 65 20 69 73 20 61 20 67 6f 6f 64 20 72 65  ere is a good re
bbb60 61 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20  ason to not use 
bbb70 74 68 65 20 77 72 61 70 70 65 72 73 2e 20 20 49  the wrappers.  I
bbb80 66 20 77 65 20 75 73 65 20 74 68 65 0a 2a 2a 20  f we use the.** 
bbb90 77 72 61 70 70 65 72 73 20 74 68 65 6e 20 77 65  wrappers then we
bbba0 20 77 69 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61   will get simula
bbbb0 74 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  ted malloc() fai
bbbc0 6c 75 72 65 73 20 77 69 74 68 69 6e 20 74 68 69  lures within thi
bbbd0 73 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 41 6e  s.** driver.  An
bbbe0 64 20 74 68 61 74 20 63 61 75 73 65 73 20 61 6c  d that causes al
bbbf0 6c 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c  l kinds of probl
bbc00 65 6d 73 20 66 6f 72 20 6f 75 72 20 74 65 73 74  ems for our test
bbc10 73 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20  s.  We.** could 
bbc20 65 6e 68 61 6e 63 65 20 53 51 4c 69 74 65 20 74  enhance SQLite t
bbc30 6f 20 64 65 61 6c 20 77 69 74 68 20 73 69 6d 75  o deal with simu
bbc40 6c 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69  lated malloc fai
bbc50 6c 75 72 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20  lures within.** 
bbc60 74 68 65 20 4f 53 20 64 72 69 76 65 72 2c 20 62  the OS driver, b
bbc70 75 74 20 74 68 65 20 63 6f 64 65 20 74 6f 20 64  ut the code to d
bbc80 65 61 6c 20 77 69 74 68 20 74 68 6f 73 65 20 66  eal with those f
bbc90 61 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74  ailure would not
bbca0 0a 2a 2a 20 62 65 20 65 78 65 72 63 69 73 65 64  .** be exercised
bbcb0 20 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69 63 68   on Linux (which
bbcc0 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74   does not need t
bbcd0 6f 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68  o malloc() in th
bbce0 65 20 64 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64  e driver).** and
bbcf0 20 73 6f 20 77 65 20 77 6f 75 6c 64 20 68 61 76   so we would hav
bbd00 65 20 64 69 66 66 69 63 75 6c 74 79 20 77 72 69  e difficulty wri
bbd10 74 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65  ting coverage te
bbd20 73 74 73 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20  sts for that.** 
bbd30 63 6f 64 65 2e 20 20 42 65 74 74 65 72 20 74 6f  code.  Better to
bbd40 20 6c 65 61 76 65 20 74 68 65 20 63 6f 64 65 20   leave the code 
bbd50 6f 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a  out, we think..*
bbd60 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f  *.** The point o
bbd70 66 20 74 68 69 73 20 64 69 73 63 75 73 73 69 6f  f this discussio
bbd80 6e 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  n is as follows:
bbd90 20 20 57 68 65 6e 20 63 72 65 61 74 69 6e 67 20    When creating 
bbda0 61 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65  a new.** OS laye
bbdb0 72 20 66 6f 72 20 61 6e 20 65 6d 62 65 64 64 65  r for an embedde
bbdc0 64 20 73 79 73 74 65 6d 2c 20 69 66 20 79 6f 75  d system, if you
bbdd0 20 75 73 65 20 74 68 69 73 20 66 69 6c 65 20 61   use this file a
bbde0 73 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a  s an example,.**
bbdf0 20 61 76 6f 69 64 20 74 68 65 20 75 73 65 20 6f   avoid the use o
bbe00 66 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28  f malloc()/free(
bbe10 29 2e 20 20 54 68 6f 73 65 20 72 6f 75 74 69 6e  ).  Those routin
bbe20 65 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 4f 53  es work ok on OS
bbe30 2f 32 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62  /2.** desktops b
bbe40 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69  ut not so well i
bbe50 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65  n embedded syste
bbe60 6d 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61  ms..*/../*.** Ma
bbe70 63 72 6f 73 20 75 73 65 64 20 74 6f 20 64 65 74  cros used to det
bbe80 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f  ermine whether o
bbe90 72 20 6e 6f 74 20 74 6f 20 75 73 65 20 74 68 72  r not to use thr
bbea0 65 61 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66  eads..*/.#if def
bbeb0 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 48 52 45  ined(SQLITE_THRE
bbec0 41 44 53 41 46 45 29 20 26 26 20 53 51 4c 49 54  ADSAFE) && SQLIT
bbed0 45 5f 54 48 52 45 41 44 53 41 46 45 0a 23 20 64  E_THREADSAFE.# d
bbee0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 32  efine SQLITE_OS2
bbef0 5f 54 48 52 45 41 44 53 20 31 0a 23 65 6e 64 69  _THREADS 1.#endi
bbf00 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65  f../*.** Include
bbf10 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f   code that is co
bbf20 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a  mmon to all os_*
bbf30 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a  .c files.*/./***
bbf40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
bbf50 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20  ude os_common.h 
bbf60 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
bbf70 20 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a   os_os2.c ******
bbf80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
bbf90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
bbfa0 6e 20 66 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e  n file os_common
bbfb0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
bbfc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
bbfe0 2a 20 32 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a  * 2004 May 22.**
bbff0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
bc000 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
bc010 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
bc020 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
bc030 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
bc040 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
bc050 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
bc060 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
bc070 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
bc080 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
bc090 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
bc0a0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
bc0b0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
bc0c0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
bc0d0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
bc0e0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
bc0f0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
bc100 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
bc110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc150 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
bc160 65 20 63 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f  e contains macro
bc170 73 20 61 6e 64 20 61 20 6c 69 74 74 6c 65 20 62  s and a little b
bc180 69 74 20 6f 66 20 63 6f 64 65 20 74 68 61 74 20  it of code that 
bc190 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20  is common to.** 
bc1a0 61 6c 6c 20 6f 66 20 74 68 65 20 70 6c 61 74 66  all of the platf
bc1b0 6f 72 6d 2d 73 70 65 63 69 66 69 63 20 66 69 6c  orm-specific fil
bc1c0 65 73 20 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20  es (os_*.c) and 
bc1d0 69 73 20 23 69 6e 63 6c 75 64 65 64 20 69 6e 74  is #included int
bc1e0 6f 20 74 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 73  o those.** files
bc1f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ..**.** This fil
bc200 65 20 73 68 6f 75 6c 64 20 62 65 20 23 69 6e 63  e should be #inc
bc210 6c 75 64 65 64 20 62 79 20 74 68 65 20 6f 73 5f  luded by the os_
bc220 2a 2e 63 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20  *.c files only. 
bc230 20 49 74 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20   It is not a.** 
bc240 67 65 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20  general purpose 
bc250 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a  header file..**.
bc260 2a 2a 20 24 49 64 3a 20 6f 73 5f 63 6f 6d 6d 6f  ** $Id: os_commo
bc270 6e 2e 68 2c 76 20 31 2e 33 38 20 32 30 30 39 2f  n.h,v 1.38 2009/
bc280 30 32 2f 32 34 20 31 38 3a 34 30 3a 35 30 20 64  02/24 18:40:50 d
bc290 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
bc2a0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f 53 5f  .*/.#ifndef _OS_
bc2b0 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e  COMMON_H_.#defin
bc2c0 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a  e _OS_COMMON_H_.
bc2d0 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20  ./*.** At least 
bc2e0 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 73 6c  two bugs have sl
bc2f0 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 73 65  ipped in because
bc300 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 65 20   we changed the 
bc310 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a 20  MEMORY_DEBUG.** 
bc320 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 45 5f  macro to SQLITE_
bc330 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 20 6f  DEBUG and some o
bc340 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 20 68  lder makefiles h
bc350 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 64 65  ave not yet made
bc360 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e 20   the.** switch. 
bc370 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
bc380 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74 63 68  ode should catch
bc390 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 61 74   this problem at
bc3a0 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a   compile-time..*
bc3b0 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 59 5f  /.#ifdef MEMORY_
bc3c0 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20 22 54  DEBUG.# error "T
bc3d0 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 20  he MEMORY_DEBUG 
bc3e0 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65 74  macro is obsolet
bc3f0 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45 5f 44  e.  Use SQLITE_D
bc400 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22 0a 23  EBUG instead.".#
bc410 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
bc420 4c 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54  LITE_DEBUG.SQLIT
bc430 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
bc440 6c 69 74 65 33 4f 53 54 72 61 63 65 20 3d 20 30  lite3OSTrace = 0
bc450 3b 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ;.#define OSTRAC
bc460 45 31 28 58 29 20 20 20 20 20 20 20 20 20 69 66  E1(X)         if
bc470 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  ( sqlite3OSTrace
bc480 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
bc490 72 69 6e 74 66 28 58 29 0a 23 64 65 66 69 6e 65  rintf(X).#define
bc4a0 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 20 20   OSTRACE2(X,Y)  
bc4b0 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
bc4c0 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65  OSTrace ) sqlite
bc4d0 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59  3DebugPrintf(X,Y
bc4e0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
bc4f0 45 33 28 58 2c 59 2c 5a 29 20 20 20 20 20 69 66  E3(X,Y,Z)     if
bc500 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  ( sqlite3OSTrace
bc510 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
bc520 72 69 6e 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65  rintf(X,Y,Z).#de
bc530 66 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c  fine OSTRACE4(X,
bc540 59 2c 5a 2c 41 29 20 20 20 69 66 28 20 73 71 6c  Y,Z,A)   if( sql
bc550 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71  ite3OSTrace ) sq
bc560 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
bc570 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e  (X,Y,Z,A).#defin
bc580 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a  e OSTRACE5(X,Y,Z
bc590 2c 41 2c 42 29 20 69 66 28 20 73 71 6c 69 74 65  ,A,B) if( sqlite
bc5a0 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74  3OSTrace ) sqlit
bc5b0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
bc5c0 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65  Y,Z,A,B).#define
bc5d0 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c   OSTRACE6(X,Y,Z,
bc5e0 41 2c 42 2c 43 29 20 5c 0a 20 20 20 20 69 66 28  A,B,C) \.    if(
bc5f0 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20  sqlite3OSTrace) 
bc600 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
bc610 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a  tf(X,Y,Z,A,B,C).
bc620 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37  #define OSTRACE7
bc630 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20  (X,Y,Z,A,B,C,D) 
bc640 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33  \.    if(sqlite3
bc650 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33  OSTrace) sqlite3
bc660 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
bc670 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65  Z,A,B,C,D).#else
bc680 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
bc690 31 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53 54  1(X).#define OST
bc6a0 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65 66 69  RACE2(X,Y).#defi
bc6b0 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c  ne OSTRACE3(X,Y,
bc6c0 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  Z).#define OSTRA
bc6d0 43 45 34 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65  CE4(X,Y,Z,A).#de
bc6e0 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58 2c  fine OSTRACE5(X,
bc6f0 59 2c 5a 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65  Y,Z,A,B).#define
bc700 20 4f 53 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c   OSTRACE6(X,Y,Z,
bc710 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f  A,B,C).#define O
bc720 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c  STRACE7(X,Y,Z,A,
bc730 42 2c 43 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f  B,C,D).#endif../
bc740 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20  *.** Macros for 
bc750 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 72 61 63  performance trac
bc760 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74  ing.  Normally t
bc770 75 72 6e 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79  urned off.  Only
bc780 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38   works.** on i48
bc790 36 20 68 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23  6 hardware..*/.#
bc7a0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 50 45 52  ifdef SQLITE_PER
bc7b0 46 4f 52 4d 41 4e 43 45 5f 54 52 41 43 45 0a 0a  FORMANCE_TRACE..
bc7c0 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20  /* .** hwtime.h 
bc7d0 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20  contains inline 
bc7e0 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 66  assembler code f
bc7f0 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20  or implementing 
bc800 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 6d  .** high-perform
bc810 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 74  ance timing rout
bc820 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  ines..*/./******
bc830 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
bc840 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 65   hwtime.h in the
bc850 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 63 6f   middle of os_co
bc860 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mmon.h *********
bc870 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
bc880 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
bc890 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a  ile hwtime.h ***
bc8a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc8b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bc8c0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
bc8d0 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a  008 May 27.**.**
bc8e0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
bc8f0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
bc900 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
bc910 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
bc920 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
bc930 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
bc940 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
bc950 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
bc960 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
bc970 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
bc980 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
bc990 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
bc9a0 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
bc9b0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
bc9c0 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
bc9d0 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
bc9e0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
bc9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bca00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bca10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bca20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bca30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
bca40 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
bca50 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61  ontains inline a
bca60 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 74 72  sm code for retr
bca70 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 65 72  ieving "high-per
bca80 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75  formance".** cou
bca90 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 63 6c  nters for x86 cl
bcaa0 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20  ass CPUs..**.** 
bcab0 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20  $Id: hwtime.h,v 
bcac0 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31 20 31  1.3 2008/08/01 1
bcad0 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20 45 78  4:33:15 shane Ex
bcae0 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f  p $.*/.#ifndef _
bcaf0 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e  HWTIME_H_.#defin
bcb00 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a  e _HWTIME_H_../*
bcb10 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
bcb20 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77  g routine only w
bcb30 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d  orks on pentium-
bcb40 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 72 29  class (or newer)
bcb50 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20   processors..** 
bcb60 49 74 20 75 73 65 73 20 74 68 65 20 52 44 54 53  It uses the RDTS
bcb70 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 64  C opcode to read
bcb80 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75 6e 74   the cycle count
bcb90 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 68   value out of th
bcba0 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 61  e.** processor a
bcbb0 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61 74 20  nd returns that 
bcbc0 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63 61 6e  value.  This can
bcbd0 20 62 65 20 75 73 65 64 20 66 6f 72 20 68 69 67   be used for hig
bcbe0 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69  h-res.** profili
bcbf0 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69  ng..*/.#if (defi
bcc00 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c  ned(__GNUC__) ||
bcc10 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45   defined(_MSC_VE
bcc20 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20 20 28  R)) && \.      (
bcc30 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c  defined(i386) ||
bcc40 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f   defined(__i386_
bcc50 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d  _) || defined(_M
bcc60 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66 20 64  _IX86))..  #if d
bcc70 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29  efined(__GNUC__)
bcc80 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73  ..  __inline__ s
bcc90 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
bcca0 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
bccb0 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  {.     unsigned 
bccc0 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20  int lo, hi;.    
bccd0 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74   __asm__ __volat
bcce0 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a  ile__ ("rdtsc" :
bccf0 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22   "=a" (lo), "=d"
bcd00 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72 65 74   (hi));.     ret
bcd10 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e 74  urn (sqlite_uint
bcd20 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f  64)hi << 32 | lo
bcd30 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64  ;.  }..  #elif d
bcd40 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29  efined(_MSC_VER)
bcd50 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e  ..  __declspec(n
bcd60 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73  aked) __inline s
bcd70 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63  qlite_uint64 __c
bcd80 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77 74 69  decl sqlite3Hwti
bcd90 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f  me(void){.     _
bcda0 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 72  _asm {.        r
bcdb0 64 74 73 63 0a 20 20 20 20 20 20 20 20 72 65 74  dtsc.        ret
bcdc0 20 20 20 20 20 20 20 3b 20 72 65 74 75 72 6e 20         ; return 
bcdd0 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45 41 58  value at EDX:EAX
bcde0 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23  .     }.  }..  #
bcdf0 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 65  endif..#elif (de
bce00 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20  fined(__GNUC__) 
bce10 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36  && defined(__x86
bce20 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c  _64__))..  __inl
bce30 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e  ine__ sqlite_uin
bce40 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t64 sqlite3Hwtim
bce50 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75  e(void){.      u
bce60 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c  nsigned long val
bce70 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20  ;.      __asm__ 
bce80 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72  __volatile__ ("r
bce90 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28 76 61  dtsc" : "=A" (va
bcea0 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  l));.      retur
bceb0 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c  n val;.  }. .#el
bcec0 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e  if (defined(__GN
bced0 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64  UC__) && defined
bcee0 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f  (__ppc__))..  __
bcef0 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f  inline__ sqlite_
bcf00 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77  uint64 sqlite3Hw
bcf10 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20  time(void){.    
bcf20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20    unsigned long 
bcf30 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 20  long retval;.   
bcf40 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67     unsigned long
bcf50 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61   junk;.      __a
bcf60 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f  sm__ __volatile_
bcf70 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20  _ ("\n\.        
bcf80 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20    1:      mftbu 
bcf90 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20    %1\n\.        
bcfa0 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 20 20            mftb  
bcfb0 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20    %L0\n\.       
bcfc0 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 75             mftbu
bcfd0 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20     %0\n\.       
bcfe0 20 20 20 20 20 20 20 20 20 20 20 63 6d 70 77 20             cmpw 
bcff0 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20     %0,%1\n\.    
bd000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6e                bn
bd010 65 20 20 20 20 20 31 62 22 0a 20 20 20 20 20 20  e     1b".      
bd020 20 20 20 20 20 20 20 20 20 20 20 20 3a 20 22 3d              : "=
bd030 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22 3d 72  r" (retval), "=r
bd040 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20  " (junk));.     
bd050 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a   return retval;.
bd060 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65    }..#else..  #e
bd070 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d  rror Need implem
bd080 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69  entation of sqli
bd090 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20  te3Hwtime() for 
bd0a0 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a  your platform...
bd0b0 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d    /*.  ** To com
bd0c0 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69 6d 70  pile without imp
bd0d0 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65  lementing sqlite
bd0e0 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f  3Hwtime() for yo
bd0f0 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a  ur platform,.  *
bd100 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65  * you can remove
bd110 20 74 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f   the above #erro
bd120 72 20 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f  r and use the fo
bd130 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75  llowing.  ** stu
bd140 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75  b function.  You
bd150 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e   will lose timin
bd160 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61  g support for ma
bd170 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64  ny.  ** of the d
bd180 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73  ebugging and tes
bd190 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20  ting utilities, 
bd1a0 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74  but it should at
bd1b0 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70  .  ** least comp
bd1c0 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a  ile and run..  *
bd1d0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
bd1e0 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34     sqlite_uint64
bd1f0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76   sqlite3Hwtime(v
bd200 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28 28 73  oid){ return ((s
bd210 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 3b  qlite_uint64)0);
bd220 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64   }..#endif..#end
bd230 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f  if /* !defined(_
bd240 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f  HWTIME_H_) */../
bd250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
bd260 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a  nd of hwtime.h *
bd270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
bd2a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
bd2b0 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
bd2c0 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f  we left off in o
bd2d0 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a  s_common.h *****
bd2e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
bd2f0 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69  static sqlite_ui
bd300 6e 74 36 34 20 67 5f 73 74 61 72 74 3b 0a 73 74  nt64 g_start;.st
bd310 61 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74  atic sqlite_uint
bd320 36 34 20 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64  64 g_elapsed;.#d
bd330 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52  efine TIMER_STAR
bd340 54 20 20 20 20 20 20 20 67 5f 73 74 61 72 74 3d  T       g_start=
bd350 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 0a  sqlite3Hwtime().
bd360 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e  #define TIMER_EN
bd370 44 20 20 20 20 20 20 20 20 20 67 5f 65 6c 61 70  D         g_elap
bd380 73 65 64 3d 73 71 6c 69 74 65 33 48 77 74 69 6d  sed=sqlite3Hwtim
bd390 65 28 29 2d 67 5f 73 74 61 72 74 0a 23 64 65 66  e()-g_start.#def
bd3a0 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45  ine TIMER_ELAPSE
bd3b0 44 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64 0a  D     g_elapsed.
bd3c0 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 54 49  #else.#define TI
bd3d0 4d 45 52 5f 53 54 41 52 54 0a 23 64 65 66 69 6e  MER_START.#defin
bd3e0 65 20 54 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66  e TIMER_END.#def
bd3f0 69 6e 65 20 54 49 4d 45 52 5f 45 4c 41 50 53 45  ine TIMER_ELAPSE
bd400 44 20 20 20 20 20 28 28 73 71 6c 69 74 65 5f 75  D     ((sqlite_u
bd410 69 6e 74 36 34 29 30 29 0a 23 65 6e 64 69 66 0a  int64)0).#endif.
bd420 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d  ./*.** If we com
bd430 70 69 6c 65 20 77 69 74 68 20 74 68 65 20 53 51  pile with the SQ
bd440 4c 49 54 45 5f 54 45 53 54 20 6d 61 63 72 6f 20  LITE_TEST macro 
bd450 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 66 6f  set, then the fo
bd460 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a  llowing block.**
bd470 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 67 69   of code will gi
bd480 76 65 20 75 73 20 74 68 65 20 61 62 69 6c 69 74  ve us the abilit
bd490 79 20 74 6f 20 73 69 6d 75 6c 61 74 65 20 61 20  y to simulate a 
bd4a0 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20  disk I/O error. 
bd4b0 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73 65 64   This.** is used
bd4c0 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 68 65   for testing the
bd4d0 20 49 2f 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f   I/O recovery lo
bd4e0 67 69 63 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  gic..*/.#ifdef S
bd4f0 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
bd500 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
bd510 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d  3_io_error_hit =
bd520 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   0;            /
bd530 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  * Total number o
bd540 66 20 49 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a  f I/O Errors */.
bd550 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
bd560 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
bd570 68 61 72 64 68 69 74 20 3d 20 30 3b 20 20 20 20  hardhit = 0;    
bd580 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
bd590 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f   non-benign erro
bd5a0 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  rs */.SQLITE_API
bd5b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
bd5c0 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20  error_pending = 
bd5d0 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75  0;        /* Cou
bd5e0 6e 74 20 64 6f 77 6e 20 74 6f 20 66 69 72 73 74  nt down to first
bd5f0 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51   I/O error */.SQ
bd600 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
bd610 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
bd620 72 73 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20  rsist = 0;      
bd630 20 20 2f 2a 20 54 72 75 65 20 69 66 20 49 2f 4f    /* True if I/O
bd640 20 65 72 72 6f 72 73 20 70 65 72 73 69 73 74 20   errors persist 
bd650 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
bd660 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  t sqlite3_io_err
bd670 6f 72 5f 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20  or_benign = 0;  
bd680 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
bd690 66 20 65 72 72 6f 72 73 20 61 72 65 20 62 65 6e  f errors are ben
bd6a0 69 67 6e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ign */.SQLITE_AP
bd6b0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 69  I int sqlite3_di
bd6c0 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d  skfull_pending =
bd6d0 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   0;.SQLITE_API i
bd6e0 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  nt sqlite3_diskf
bd6f0 75 6c 6c 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65  ull = 0;.#define
bd700 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
bd710 42 65 6e 69 67 6e 28 58 29 20 73 71 6c 69 74 65  Benign(X) sqlite
bd720 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67  3_io_error_benig
bd730 6e 3d 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69  n=(X).#define Si
bd740 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 43 4f  mulateIOError(CO
bd750 44 45 29 20 20 5c 0a 20 20 69 66 28 20 28 73 71  DE)  \.  if( (sq
bd760 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
bd770 65 72 73 69 73 74 20 26 26 20 73 71 6c 69 74 65  ersist && sqlite
bd780 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20  3_io_error_hit) 
bd790 5c 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69  \.       || sqli
bd7a0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e  te3_io_error_pen
bd7b0 64 69 6e 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c  ding-- == 1 )  \
bd7c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7b  .              {
bd7d0 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20   local_ioerr(); 
bd7e0 43 4f 44 45 3b 20 7d 0a 73 74 61 74 69 63 20 76  CODE; }.static v
bd7f0 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28  oid local_ioerr(
bd800 29 7b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 49  ){.  IOTRACE(("I
bd810 4f 45 52 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c  OERR\n"));.  sql
bd820 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69  ite3_io_error_hi
bd830 74 2b 2b 3b 0a 20 20 69 66 28 20 21 73 71 6c 69  t++;.  if( !sqli
bd840 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e  te3_io_error_ben
bd850 69 67 6e 20 29 20 73 71 6c 69 74 65 33 5f 69 6f  ign ) sqlite3_io
bd860 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b  _error_hardhit++
bd870 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 53 69 6d 75  ;.}.#define Simu
bd880 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f  lateDiskfullErro
bd890 72 28 43 4f 44 45 29 20 5c 0a 20 20 20 69 66 28  r(CODE) \.   if(
bd8a0 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
bd8b0 6c 5f 70 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20  l_pending ){ \. 
bd8c0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f      if( sqlite3_
bd8d0 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67  diskfull_pending
bd8e0 20 3d 3d 20 31 20 29 7b 20 5c 0a 20 20 20 20 20   == 1 ){ \.     
bd8f0 20 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b    local_ioerr();
bd900 20 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65   \.       sqlite
bd910 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20  3_diskfull = 1; 
bd920 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  \.       sqlite3
bd930 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20  _io_error_hit = 
bd940 31 3b 20 5c 0a 20 20 20 20 20 20 20 43 4f 44 45  1; \.       CODE
bd950 3b 20 5c 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20  ; \.     }else{ 
bd960 5c 0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  \.       sqlite3
bd970 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e  _diskfull_pendin
bd980 67 2d 2d 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a  g--; \.     } \.
bd990 20 20 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69     }.#else.#defi
bd9a0 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  ne SimulateIOErr
bd9b0 6f 72 42 65 6e 69 67 6e 28 58 29 0a 23 64 65 66  orBenign(X).#def
bd9c0 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ine SimulateIOEr
bd9d0 72 6f 72 28 41 29 0a 23 64 65 66 69 6e 65 20 53  ror(A).#define S
bd9e0 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45  imulateDiskfullE
bd9f0 72 72 6f 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a  rror(A).#endif..
bda00 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 65 73 74 69  /*.** When testi
bda10 6e 67 2c 20 6b 65 65 70 20 61 20 63 6f 75 6e 74  ng, keep a count
bda20 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   of the number o
bda30 66 20 6f 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f  f open files..*/
bda40 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
bda50 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  EST.SQLITE_API i
bda60 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  nt sqlite3_open_
bda70 66 69 6c 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a  file_count = 0;.
bda80 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e  #define OpenCoun
bda90 74 65 72 28 58 29 20 20 73 71 6c 69 74 65 33 5f  ter(X)  sqlite3_
bdaa0 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b  open_file_count+
bdab0 3d 28 58 29 0a 23 65 6c 73 65 0a 23 64 65 66 69  =(X).#else.#defi
bdac0 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58  ne OpenCounter(X
bdad0 29 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66  ).#endif..#endif
bdae0 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 4f 53   /* !defined(_OS
bdaf0 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a  _COMMON_H_) */..
bdb00 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
bdb10 45 6e 64 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e  End of os_common
bdb20 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
bdb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
bdb50 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
bdb60 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
bdb70 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
bdb80 6f 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a  os_os2.c *******
bdb90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
bdba0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f 73 32 46 69  ./*.** The os2Fi
bdbb0 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20  le structure is 
bdbc0 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69  subclass of sqli
bdbd0 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69 66 69  te3_file specifi
bdbe0 63 20 66 6f 72 20 74 68 65 20 4f 53 2f 32 0a 2a  c for the OS/2.*
bdbf0 2a 20 70 72 6f 74 61 62 69 6c 69 74 79 20 6c 61  * protability la
bdc00 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  yer..*/.typedef 
bdc10 73 74 72 75 63 74 20 6f 73 32 46 69 6c 65 20 6f  struct os2File o
bdc20 73 32 46 69 6c 65 3b 0a 73 74 72 75 63 74 20 6f  s2File;.struct o
bdc30 73 32 46 69 6c 65 20 7b 0a 20 20 63 6f 6e 73 74  s2File {.  const
bdc40 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
bdc50 6f 64 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 2f  ods *pMethod;  /
bdc60 2a 20 41 6c 77 61 79 73 20 74 68 65 20 66 69 72  * Always the fir
bdc70 73 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 48 46  st entry */.  HF
bdc80 49 4c 45 20 68 3b 20 20 20 20 20 20 20 20 20 20  ILE h;          
bdc90 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c          /* Handl
bdca0 65 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20  e for accessing 
bdcb0 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68  the file */.  ch
bdcc0 61 72 2a 20 70 61 74 68 54 6f 44 65 6c 3b 20 20  ar* pathToDel;  
bdcd0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
bdce0 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74  of file to delet
bdcf0 65 20 6f 6e 20 63 6c 6f 73 65 2c 20 4e 55 4c 4c  e on close, NULL
bdd00 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 75 6e 73   if not */.  uns
bdd10 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b 74  igned char lockt
bdd20 79 70 65 3b 20 20 20 2f 2a 20 54 79 70 65 20 6f  ype;   /* Type o
bdd30 66 20 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79  f lock currently
bdd40 20 68 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69   held on this fi
bdd50 6c 65 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e  le */.};..#defin
bdd60 65 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 31  e LOCK_TIMEOUT 1
bdd70 30 4c 20 2f 2a 20 74 68 65 20 64 65 66 61 75 6c  0L /* the defaul
bdd80 74 20 6c 6f 63 6b 69 6e 67 20 74 69 6d 65 6f 75  t locking timeou
bdd90 74 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  t */../*********
bdda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bddb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bddc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bddd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bdde0 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74  ****.** The next
bddf0 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e   group of routin
bde00 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  es implement the
bde10 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73 70 65   I/O methods spe
bde20 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 65  cified.** by the
bde30 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
bde40 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a 2a  ods object..****
bde50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bde60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bde70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bde80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bde90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a  **********/../*.
bdea0 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e  ** Close a file.
bdeb0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
bdec0 73 32 43 6c 6f 73 65 28 20 73 71 6c 69 74 65 33  s2Close( sqlite3
bded0 5f 66 69 6c 65 20 2a 69 64 20 29 7b 0a 20 20 41  _file *id ){.  A
bdee0 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52  PIRET rc = NO_ER
bdef0 52 4f 52 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a  ROR;.  os2File *
bdf00 70 46 69 6c 65 3b 0a 20 20 69 66 28 20 69 64 20  pFile;.  if( id 
bdf10 26 26 20 28 70 46 69 6c 65 20 3d 20 28 6f 73 32  && (pFile = (os2
bdf20 46 69 6c 65 2a 29 69 64 29 20 21 3d 20 30 20 29  File*)id) != 0 )
bdf30 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 20  {.    OSTRACE2( 
bdf40 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 70 46  "CLOSE %d\n", pF
bdf50 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 72 63  ile->h );.    rc
bdf60 20 3d 20 44 6f 73 43 6c 6f 73 65 28 20 70 46 69   = DosClose( pFi
bdf70 6c 65 2d 3e 68 20 29 3b 0a 20 20 20 20 70 46 69  le->h );.    pFi
bdf80 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e  le->locktype = N
bdf90 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 69 66 28 20  O_LOCK;.    if( 
bdfa0 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c  pFile->pathToDel
bdfb0 20 21 3d 20 4e 55 4c 4c 20 29 7b 0a 20 20 20 20   != NULL ){.    
bdfc0 20 20 72 63 20 3d 20 44 6f 73 46 6f 72 63 65 44    rc = DosForceD
bdfd0 65 6c 65 74 65 28 20 28 50 53 5a 29 70 46 69 6c  elete( (PSZ)pFil
bdfe0 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a  e->pathToDel );.
bdff0 20 20 20 20 20 20 66 72 65 65 28 20 70 46 69 6c        free( pFil
be000 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a  e->pathToDel );.
be010 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74        pFile->pat
be020 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20  hToDel = NULL;. 
be030 20 20 20 7d 0a 20 20 20 20 69 64 20 3d 20 30 3b     }.    id = 0;
be040 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74 65 72  .    OpenCounter
be050 28 20 2d 31 20 29 3b 0a 20 20 7d 0a 0a 20 20 72  ( -1 );.  }..  r
be060 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45  eturn rc == NO_E
be070 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  RROR ? SQLITE_OK
be080 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b   : SQLITE_IOERR;
be090 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64  .}../*.** Read d
be0a0 61 74 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20  ata from a file 
be0b0 69 6e 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20  into a buffer.  
be0c0 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
be0d0 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73   if all.** bytes
be0e0 20 77 65 72 65 20 72 65 61 64 20 73 75 63 63 65   were read succe
be0f0 73 73 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49  ssfully and SQLI
be100 54 45 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74  TE_IOERR if anyt
be110 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f  hing goes.** wro
be120 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
be130 74 20 6f 73 32 52 65 61 64 28 0a 20 20 73 71 6c  t os2Read(.  sql
be140 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20  ite3_file *id,  
be150 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
be160 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72 6f  File to read fro
be170 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75  m */.  void *pBu
be180 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f,              
be190 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
be1a0 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 74 68 69  content into thi
be1b0 73 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e  s buffer */.  in
be1c0 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20  t amt,          
be1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
be1e0 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
be1f0 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71   to read */.  sq
be200 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73  lite3_int64 offs
be210 65 74 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  et            /*
be220 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61   Begin reading a
be230 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f  t this offset */
be240 0a 29 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65  .){.  ULONG file
be250 4c 6f 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20  Location = 0L;. 
be260 20 55 4c 4f 4e 47 20 67 6f 74 3b 0a 20 20 6f 73   ULONG got;.  os
be270 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  2File *pFile = (
be280 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61  os2File*)id;.  a
be290 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a  ssert( id!=0 );.
be2a0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
be2b0 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  r( return SQLITE
be2c0 5f 49 4f 45 52 52 5f 52 45 41 44 20 29 3b 0a 20  _IOERR_READ );. 
be2d0 20 4f 53 54 52 41 43 45 33 28 20 22 52 45 41 44   OSTRACE3( "READ
be2e0 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20   %d lock=%d\n", 
be2f0 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d  pFile->h, pFile-
be300 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 69  >locktype );.  i
be310 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50 74 72  f( DosSetFilePtr
be320 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 65  (pFile->h, offse
be330 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20 26  t, FILE_BEGIN, &
be340 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 21 3d  fileLocation) !=
be350 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
be360 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
be370 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20  OERR;.  }.  if( 
be380 44 6f 73 52 65 61 64 28 20 70 46 69 6c 65 2d 3e  DosRead( pFile->
be390 68 2c 20 70 42 75 66 2c 20 61 6d 74 2c 20 26 67  h, pBuf, amt, &g
be3a0 6f 74 20 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52  ot ) != NO_ERROR
be3b0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
be3c0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44  QLITE_IOERR_READ
be3d0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f 74 20  ;.  }.  if( got 
be3e0 3d 3d 20 28 55 4c 4f 4e 47 29 61 6d 74 20 29 0a  == (ULONG)amt ).
be3f0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
be400 45 5f 4f 4b 3b 0a 20 20 65 6c 73 65 20 7b 0a 20  E_OK;.  else {. 
be410 20 20 20 2f 2a 20 55 6e 72 65 61 64 20 70 6f 72     /* Unread por
be420 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 69 6e 70  tions of the inp
be430 75 74 20 62 75 66 66 65 72 20 6d 75 73 74 20 62  ut buffer must b
be440 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f  e zero-filled */
be450 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63  .    memset(&((c
be460 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c  har*)pBuf)[got],
be470 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20   0, amt-got);.  
be480 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
be490 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44  IOERR_SHORT_READ
be4a0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  ;.  }.}../*.** W
be4b0 72 69 74 65 20 64 61 74 61 20 66 72 6f 6d 20 61  rite data from a
be4c0 20 62 75 66 66 65 72 20 69 6e 74 6f 20 61 20 66   buffer into a f
be4d0 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c  ile.  Return SQL
be4e0 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73  ITE_OK on succes
be4f0 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68  s.** or some oth
be500 65 72 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 6e  er error code on
be510 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61   failure..*/.sta
be520 74 69 63 20 69 6e 74 20 6f 73 32 57 72 69 74 65  tic int os2Write
be530 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  (.  sqlite3_file
be540 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 20   *id,           
be550 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 77      /* File to w
be560 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a 20 20 63  rite into */.  c
be570 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c  onst void *pBuf,
be580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
be590 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f 20 62  * The bytes to b
be5a0 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69  e written */.  i
be5b0 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20  nt amt,         
be5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
be5d0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
be5e0 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20  s to write */.  
be5f0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66  sqlite3_int64 of
be600 66 73 65 74 20 20 20 20 20 20 20 20 20 20 20 20  fset            
be610 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 74  /* Offset into t
be620 68 65 20 66 69 6c 65 20 74 6f 20 62 65 67 69 6e  he file to begin
be630 20 77 72 69 74 69 6e 67 20 61 74 20 2a 2f 0a 29   writing at */.)
be640 7b 0a 20 20 55 4c 4f 4e 47 20 66 69 6c 65 4c 6f  {.  ULONG fileLo
be650 63 61 74 69 6f 6e 20 3d 20 30 4c 3b 0a 20 20 41  cation = 0L;.  A
be660 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52  PIRET rc = NO_ER
be670 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 77 72 6f  ROR;.  ULONG wro
be680 74 65 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70  te;.  os2File *p
be690 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a  File = (os2File*
be6a0 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 69  )id;.  assert( i
be6b0 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61  d!=0 );.  Simula
be6c0 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72  teIOError( retur
be6d0 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 57  n SQLITE_IOERR_W
be6e0 52 49 54 45 20 29 3b 0a 20 20 53 69 6d 75 6c 61  RITE );.  Simula
be6f0 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28  teDiskfullError(
be700 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46   return SQLITE_F
be710 55 4c 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  ULL );.  OSTRACE
be720 33 28 20 22 57 52 49 54 45 20 25 64 20 6c 6f 63  3( "WRITE %d loc
be730 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  k=%d\n", pFile->
be740 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  h, pFile->lockty
be750 70 65 20 29 3b 0a 20 20 69 66 28 20 44 6f 73 53  pe );.  if( DosS
be760 65 74 46 69 6c 65 50 74 72 28 70 46 69 6c 65 2d  etFilePtr(pFile-
be770 3e 68 2c 20 6f 66 66 73 65 74 2c 20 46 49 4c 45  >h, offset, FILE
be780 5f 42 45 47 49 4e 2c 20 26 66 69 6c 65 4c 6f 63  _BEGIN, &fileLoc
be790 61 74 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 52 52  ation) != NO_ERR
be7a0 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OR ){.    return
be7b0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20   SQLITE_IOERR;. 
be7c0 20 7d 0a 20 20 61 73 73 65 72 74 28 20 61 6d 74   }.  assert( amt
be7d0 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 61  >0 );.  while( a
be7e0 6d 74 20 3e 20 30 20 26 26 0a 20 20 20 20 20 20  mt > 0 &&.      
be7f0 20 20 20 28 20 72 63 20 3d 20 44 6f 73 57 72 69     ( rc = DosWri
be800 74 65 28 20 70 46 69 6c 65 2d 3e 68 2c 20 28 50  te( pFile->h, (P
be810 56 4f 49 44 29 70 42 75 66 2c 20 61 6d 74 2c 20  VOID)pBuf, amt, 
be820 26 77 72 6f 74 65 20 29 20 29 20 3d 3d 20 4e 4f  &wrote ) ) == NO
be830 5f 45 52 52 4f 52 20 26 26 0a 20 20 20 20 20 20  _ERROR &&.      
be840 20 20 20 77 72 6f 74 65 20 3e 20 30 0a 20 20 29     wrote > 0.  )
be850 7b 0a 20 20 20 20 61 6d 74 20 2d 3d 20 77 72 6f  {.    amt -= wro
be860 74 65 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 26  te;.    pBuf = &
be870 28 28 63 68 61 72 2a 29 70 42 75 66 29 5b 77 72  ((char*)pBuf)[wr
be880 6f 74 65 5d 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  ote];.  }..  ret
be890 75 72 6e 20 28 20 72 63 20 21 3d 20 4e 4f 5f 45  urn ( rc != NO_E
be8a0 52 52 4f 52 20 7c 7c 20 61 6d 74 20 3e 20 28 69  RROR || amt > (i
be8b0 6e 74 29 77 72 6f 74 65 20 29 20 3f 20 53 51 4c  nt)wrote ) ? SQL
be8c0 49 54 45 5f 46 55 4c 4c 20 3a 20 53 51 4c 49 54  ITE_FULL : SQLIT
be8d0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK;.}../*.** T
be8e0 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20  runcate an open 
be8f0 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66  file to a specif
be900 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74  ied size.*/.stat
be910 69 63 20 69 6e 74 20 6f 73 32 54 72 75 6e 63 61  ic int os2Trunca
be920 74 65 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  te( sqlite3_file
be930 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65 20   *id, i64 nByte 
be940 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d  ){.  APIRET rc =
be950 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f 73 32   NO_ERROR;.  os2
be960 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f  File *pFile = (o
be970 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f 53  s2File*)id;.  OS
be980 54 52 41 43 45 33 28 20 22 54 52 55 4e 43 41 54  TRACE3( "TRUNCAT
be990 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70 46  E %d %lld\n", pF
be9a0 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b  ile->h, nByte );
be9b0 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
be9c0 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54  or( return SQLIT
be9d0 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45  E_IOERR_TRUNCATE
be9e0 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 53 65   );.  rc = DosSe
be9f0 74 46 69 6c 65 53 69 7a 65 28 20 70 46 69 6c 65  tFileSize( pFile
bea00 2d 3e 68 2c 20 6e 42 79 74 65 20 29 3b 0a 20 20  ->h, nByte );.  
bea10 72 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f  return rc == NO_
bea20 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f  ERROR ? SQLITE_O
bea30 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  K : SQLITE_IOERR
bea40 5f 54 52 55 4e 43 41 54 45 3b 0a 7d 0a 0a 23 69  _TRUNCATE;.}..#i
bea50 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
bea60 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65  ./*.** Count the
bea70 20 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73   number of fulls
bea80 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20  yncs and normal 
bea90 73 79 6e 63 73 2e 20 20 54 68 69 73 20 69 73 20  syncs.  This is 
beaa0 75 73 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20  used to test.** 
beab0 74 68 61 74 20 73 79 6e 63 73 20 61 6e 64 20 66  that syncs and f
beac0 75 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 63  ullsyncs are occ
bead0 75 72 69 6e 67 20 61 74 20 74 68 65 20 72 69 67  uring at the rig
beae0 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c  ht times..*/.SQL
beaf0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
beb00 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 3d  te3_sync_count =
beb10 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   0;.SQLITE_API i
beb20 6e 74 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73  nt sqlite3_fulls
beb30 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23  ync_count = 0;.#
beb40 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  endif../*.** Mak
beb50 65 20 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65  e sure all write
beb60 73 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61  s to a particula
beb70 72 20 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69  r file are commi
beb80 74 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f  tted to disk..*/
beb90 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 53  .static int os2S
beba0 79 6e 63 28 20 73 71 6c 69 74 65 33 5f 66 69 6c  ync( sqlite3_fil
bebb0 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73  e *id, int flags
bebc0 20 29 7b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70   ){.  os2File *p
bebd0 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a  File = (os2File*
bebe0 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 33 28  )id;.  OSTRACE3(
bebf0 20 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3d 25   "SYNC %d lock=%
bec00 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
bec10 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
bec20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  );.#ifdef SQLITE
bec30 5f 54 45 53 54 0a 20 20 69 66 28 20 66 6c 61 67  _TEST.  if( flag
bec40 73 20 26 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f  s & SQLITE_SYNC_
bec50 46 55 4c 4c 29 7b 0a 20 20 20 20 73 71 6c 69 74  FULL){.    sqlit
bec60 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e  e3_fullsync_coun
bec70 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  t++;.  }.  sqlit
bec80 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b  e3_sync_count++;
bec90 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20  .#endif.  /* If 
beca0 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  we compiled with
becb0 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53   the SQLITE_NO_S
becc0 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73  YNC flag, then s
becd0 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a  yncing is a.  **
bece0 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64   no-op.  */.#ifd
becf0 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e  ef SQLITE_NO_SYN
bed00 43 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  C.  UNUSED_PARAM
bed10 45 54 45 52 28 70 46 69 6c 65 29 3b 0a 20 20 72  ETER(pFile);.  r
bed20 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
bed30 0a 23 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20  .#else.  return 
bed40 44 6f 73 52 65 73 65 74 42 75 66 66 65 72 28 20  DosResetBuffer( 
bed50 70 46 69 6c 65 2d 3e 68 20 29 20 3d 3d 20 4e 4f  pFile->h ) == NO
bed60 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f  _ERROR ? SQLITE_
bed70 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52  OK : SQLITE_IOER
bed80 52 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a  R;.#endif.}../*.
bed90 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65  ** Determine the
beda0 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66   current size of
bedb0 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73   a file in bytes
bedc0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
bedd0 73 32 46 69 6c 65 53 69 7a 65 28 20 73 71 6c 69  s2FileSize( sqli
bede0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71  te3_file *id, sq
bedf0 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69  lite3_int64 *pSi
bee00 7a 65 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72  ze ){.  APIRET r
bee10 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20  c = NO_ERROR;.  
bee20 46 49 4c 45 53 54 41 54 55 53 33 20 66 73 74 73  FILESTATUS3 fsts
bee30 33 46 69 6c 65 49 6e 66 6f 3b 0a 20 20 6d 65 6d  3FileInfo;.  mem
bee40 73 65 74 28 26 66 73 74 73 33 46 69 6c 65 49 6e  set(&fsts3FileIn
bee50 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 73  fo, 0, sizeof(fs
bee60 74 73 33 46 69 6c 65 49 6e 66 6f 29 29 3b 0a 20  ts3FileInfo));. 
bee70 20 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29   assert( id!=0 )
bee80 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ;.  SimulateIOEr
bee90 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49  ror( return SQLI
beea0 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 20 29  TE_IOERR_FSTAT )
beeb0 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72  ;.  rc = DosQuer
beec0 79 46 69 6c 65 49 6e 66 6f 28 20 28 28 6f 73 32  yFileInfo( ((os2
beed0 46 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 46 49  File*)id)->h, FI
beee0 4c 5f 53 54 41 4e 44 41 52 44 2c 20 26 66 73 74  L_STANDARD, &fst
beef0 73 33 46 69 6c 65 49 6e 66 6f 2c 20 73 69 7a 65  s3FileInfo, size
bef00 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33 29 20  of(FILESTATUS3) 
bef10 29 3b 0a 20 20 69 66 28 20 72 63 20 3d 3d 20 4e  );.  if( rc == N
bef20 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 2a  O_ERROR ){.    *
bef30 70 53 69 7a 65 20 3d 20 66 73 74 73 33 46 69 6c  pSize = fsts3Fil
bef40 65 49 6e 66 6f 2e 63 62 46 69 6c 65 3b 0a 20 20  eInfo.cbFile;.  
bef50 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
bef60 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OK;.  }else{.   
bef70 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
bef80 4f 45 52 52 5f 46 53 54 41 54 3b 0a 20 20 7d 0a  OERR_FSTAT;.  }.
bef90 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65  }../*.** Acquire
befa0 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e 0a   a reader lock..
befb0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
befc0 74 52 65 61 64 4c 6f 63 6b 28 20 6f 73 32 46 69  tReadLock( os2Fi
befd0 6c 65 20 2a 70 46 69 6c 65 20 29 7b 0a 20 20 46  le *pFile ){.  F
befe0 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65  ILELOCK  LockAre
beff0 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 55  a,.            U
bf000 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 41 50 49  nlockArea;.  API
bf010 52 45 54 20 72 65 73 3b 0a 20 20 6d 65 6d 73 65  RET res;.  memse
bf020 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20  t(&LockArea, 0, 
bf030 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29  sizeof(LockArea)
bf040 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c  );.  memset(&Unl
bf050 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65  ockArea, 0, size
bf060 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b  of(UnlockArea));
bf070 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66  .  LockArea.lOff
bf080 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49 52  set = SHARED_FIR
bf090 53 54 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  ST;.  LockArea.l
bf0a0 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f 53  Range = SHARED_S
bf0b0 49 5a 45 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72 65  IZE;.  UnlockAre
bf0c0 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a  a.lOffset = 0L;.
bf0d0 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61    UnlockArea.lRa
bf0e0 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 72 65 73 20  nge = 0L;.  res 
bf0f0 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b  = DosSetFileLock
bf100 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e  s( pFile->h, &Un
bf110 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41  lockArea, &LockA
bf120 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55  rea, LOCK_TIMEOU
bf130 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 54 52 41  T, 1L );.  OSTRA
bf140 43 45 33 28 20 22 47 45 54 52 45 41 44 4c 4f 43  CE3( "GETREADLOC
bf150 4b 20 25 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20  K %d res=%d\n", 
bf160 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b  pFile->h, res );
bf170 0a 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d  .  return res;.}
bf180 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64 6f 20 61 20 72  ../*.** Undo a r
bf190 65 61 64 6c 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69  eadlock.*/.stati
bf1a0 63 20 69 6e 74 20 75 6e 6c 6f 63 6b 52 65 61 64  c int unlockRead
bf1b0 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20 2a 69  Lock( os2File *i
bf1c0 64 20 29 7b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20  d ){.  FILELOCK 
bf1d0 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20   LockArea,.     
bf1e0 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65         UnlockAre
bf1f0 61 3b 0a 20 20 41 50 49 52 45 54 20 72 65 73 3b  a;.  APIRET res;
bf200 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41  .  memset(&LockA
bf210 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c  rea, 0, sizeof(L
bf220 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d  ockArea));.  mem
bf230 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c  set(&UnlockArea,
bf240 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63   0, sizeof(Unloc
bf250 6b 41 72 65 61 29 29 3b 0a 20 20 4c 6f 63 6b 41  kArea));.  LockA
bf260 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c  rea.lOffset = 0L
bf270 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61  ;.  LockArea.lRa
bf280 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 55 6e 6c 6f  nge = 0L;.  Unlo
bf290 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
bf2a0 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a 20   SHARED_FIRST;. 
bf2b0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e   UnlockArea.lRan
bf2c0 67 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45  ge = SHARED_SIZE
bf2d0 3b 0a 20 20 72 65 73 20 3d 20 44 6f 73 53 65 74  ;.  res = DosSet
bf2e0 46 69 6c 65 4c 6f 63 6b 73 28 20 69 64 2d 3e 68  FileLocks( id->h
bf2f0 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26  , &UnlockArea, &
bf300 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54  LockArea, LOCK_T
bf310 49 4d 45 4f 55 54 2c 20 31 4c 20 29 3b 0a 20 20  IMEOUT, 1L );.  
bf320 4f 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43  OSTRACE3( "UNLOC
bf330 4b 2d 52 45 41 44 4c 4f 43 4b 20 66 69 6c 65 20  K-READLOCK file 
bf340 68 61 6e 64 6c 65 3d 25 64 20 72 65 73 3d 25 64  handle=%d res=%d
bf350 3f 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 72 65 73  ?\n", id->h, res
bf360 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65 73   );.  return res
bf370 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20  ;.}../*.** Lock 
bf380 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68  the file with th
bf390 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64  e lock specified
bf3a0 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f   by parameter lo
bf3b0 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20  cktype - one.** 
bf3c0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
bf3d0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20  :.**.**     (1) 
bf3e0 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20  SHARED_LOCK.**  
bf3f0 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f     (2) RESERVED_
bf400 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20  LOCK.**     (3) 
bf410 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20  PENDING_LOCK.** 
bf420 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56      (4) EXCLUSIV
bf430 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d  E_LOCK.**.** Som
bf440 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75  etimes when requ
bf450 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20  esting one lock 
bf460 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61  state, additiona
bf470 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a  l lock states.**
bf480 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e   are inserted in
bf490 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c   between.  The l
bf4a0 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69  ocking might fai
bf4b0 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20  l on one of the 
bf4c0 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74  later.** transit
bf4d0 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65  ions leaving the
bf4e0 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66   lock state diff
bf4f0 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20  erent from what 
bf500 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a  it started but.*
bf510 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66  * still short of
bf520 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20   its goal.  The 
bf530 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20  following chart 
bf540 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65  shows the allowe
bf550 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73  d.** transitions
bf560 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65   and the inserte
bf570 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73  d intermediate s
bf580 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  tates:.**.**    
bf590 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52  UNLOCKED -> SHAR
bf5a0 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20  ED.**    SHARED 
bf5b0 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20  -> RESERVED.**  
bf5c0 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e    SHARED -> (PEN
bf5d0 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49  DING) -> EXCLUSI
bf5e0 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45  VE.**    RESERVE
bf5f0 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d  D -> (PENDING) -
bf600 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20  > EXCLUSIVE.**  
bf610 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43    PENDING -> EXC
bf620 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69  LUSIVE.**.** Thi
bf630 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f  s routine will o
bf640 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c  nly increase a l
bf650 6f 63 6b 2e 20 20 54 68 65 20 6f 73 32 55 6e 6c  ock.  The os2Unl
bf660 6f 63 6b 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a  ock() routine.**
bf670 20 65 72 61 73 65 73 20 61 6c 6c 20 6c 6f 63 6b   erases all lock
bf680 73 20 61 74 20 6f 6e 63 65 20 61 6e 64 20 72 65  s at once and re
bf690 74 75 72 6e 73 20 75 73 20 69 6d 6d 65 64 69 61  turns us immedia
bf6a0 74 65 6c 79 20 74 6f 20 6c 6f 63 6b 69 6e 67 20  tely to locking 
bf6b0 6c 65 76 65 6c 20 30 2e 0a 2a 2a 20 49 74 20 69  level 0..** It i
bf6c0 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74  s not possible t
bf6d0 6f 20 6c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b  o lower the lock
bf6e0 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 65 20 73 74  ing level one st
bf6f0 65 70 20 61 74 20 61 20 74 69 6d 65 2e 20 20 59  ep at a time.  Y
bf700 6f 75 0a 2a 2a 20 6d 75 73 74 20 67 6f 20 73 74  ou.** must go st
bf710 72 61 69 67 68 74 20 74 6f 20 6c 6f 63 6b 69 6e  raight to lockin
bf720 67 20 6c 65 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74  g level 0..*/.st
bf730 61 74 69 63 20 69 6e 74 20 6f 73 32 4c 6f 63 6b  atic int os2Lock
bf740 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  ( sqlite3_file *
bf750 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
bf760 20 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53   ){.  int rc = S
bf770 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20  QLITE_OK;       
bf780 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66  /* Return code f
bf790 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e 65 73 20  rom subroutines 
bf7a0 2a 2f 0a 20 20 41 50 49 52 45 54 20 72 65 73 20  */.  APIRET res 
bf7b0 3d 20 4e 4f 5f 45 52 52 4f 52 3b 20 20 20 20 2f  = NO_ERROR;    /
bf7c0 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 6e 20 4f  * Result of an O
bf7d0 53 2f 32 20 6c 6f 63 6b 20 63 61 6c 6c 20 2a 2f  S/2 lock call */
bf7e0 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b 74 79  .  int newLockty
bf7f0 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 74  pe;       /* Set
bf800 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
bf810 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65 20 62   to this value b
bf820 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20 2a 2f  efore exiting */
bf830 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64 69 6e  .  int gotPendin
bf840 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54 72 75  gLock = 0;/* Tru
bf850 65 20 69 66 20 77 65 20 61 63 71 75 69 72 65 64  e if we acquired
bf860 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20   a PENDING lock 
bf870 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20 20 46  this time */.  F
bf880 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41 72 65  ILELOCK  LockAre
bf890 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 55  a,.            U
bf8a0 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6f 73 32  nlockArea;.  os2
bf8b0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f  File *pFile = (o
bf8c0 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 6d 65  s2File*)id;.  me
bf8d0 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c 20  mset(&LockArea, 
bf8e0 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41 72  0, sizeof(LockAr
bf8f0 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26  ea));.  memset(&
bf900 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73  UnlockArea, 0, s
bf910 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65 61  izeof(UnlockArea
bf920 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46  ));.  assert( pF
bf930 69 6c 65 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52  ile!=0 );.  OSTR
bf940 41 43 45 34 28 20 22 4c 4f 43 4b 20 25 64 20 25  ACE4( "LOCK %d %
bf950 64 20 77 61 73 20 25 64 5c 6e 22 2c 20 70 46 69  d was %d\n", pFi
bf960 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c  le->h, locktype,
bf970 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
bf980 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   );..  /* If the
bf990 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  re is already a 
bf9a0 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70  lock of this typ
bf9b0 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69  e or more restri
bf9c0 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a  ctive on the.  *
bf9d0 2a 20 6f 73 32 46 69 6c 65 2c 20 64 6f 20 6e 6f  * os2File, do no
bf9e0 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65  thing. Don't use
bf9f0 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65   the end_lock: e
bfa00 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a  xit path, as.  *
bfa10 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  * sqlite3_mutex_
bfa20 65 6e 74 65 72 28 29 20 68 61 73 6e 27 74 20 62  enter() hasn't b
bfa30 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a  een called yet..
bfa40 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65    */.  if( pFile
bfa50 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b  ->locktype>=lock
bfa60 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52  type ){.    OSTR
bfa70 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 25  ACE3( "LOCK %d %
bfa80 64 20 6f 6b 20 28 61 6c 72 65 61 64 79 20 68 65  d ok (already he
bfa90 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  ld)\n", pFile->h
bfaa0 2c 20 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20  , locktype );.  
bfab0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
bfac0 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61  OK;.  }..  /* Ma
bfad0 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b  ke sure the lock
bfae0 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20  ing sequence is 
bfaf0 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61  correct.  */.  a
bfb00 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f  ssert( pFile->lo
bfb10 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20  cktype!=NO_LOCK 
bfb20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  || locktype==SHA
bfb30 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73  RED_LOCK );.  as
bfb40 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d  sert( locktype!=
bfb50 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a  PENDING_LOCK );.
bfb60 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
bfb70 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43  pe!=RESERVED_LOC
bfb80 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  K || pFile->lock
bfb90 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
bfba0 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20  K );..  /* Lock 
bfbb0 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  the PENDING_LOCK
bfbc0 20 62 79 74 65 20 69 66 20 77 65 20 6e 65 65 64   byte if we need
bfbd0 20 74 6f 20 61 63 71 75 69 72 65 20 61 20 50 45   to acquire a PE
bfbe0 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a 20 20  NDING lock or.  
bfbf0 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b  ** a SHARED lock
bfc00 2e 20 20 49 66 20 77 65 20 61 72 65 20 61 63 71  .  If we are acq
bfc10 75 69 72 69 6e 67 20 61 20 53 48 41 52 45 44 20  uiring a SHARED 
bfc20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75 69 73  lock, the acquis
bfc30 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20 74 68  ition of.  ** th
bfc40 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62  e PENDING_LOCK b
bfc50 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61 72 79  yte is temporary
bfc60 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b  ..  */.  newLock
bfc70 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f  type = pFile->lo
bfc80 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 70 46  cktype;.  if( pF
bfc90 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e  ile->locktype==N
bfca0 4f 5f 4c 4f 43 4b 0a 20 20 20 20 20 20 7c 7c 20  O_LOCK.      || 
bfcb0 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55  (locktype==EXCLU
bfcc0 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69  SIVE_LOCK && pFi
bfcd0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45  le->locktype==RE
bfce0 53 45 52 56 45 44 5f 4c 4f 43 4b 29 0a 20 20 29  SERVED_LOCK).  )
bfcf0 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  {.    LockArea.l
bfd00 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47  Offset = PENDING
bfd10 5f 42 59 54 45 3b 0a 20 20 20 20 4c 6f 63 6b 41  _BYTE;.    LockA
bfd20 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b  rea.lRange = 1L;
bfd30 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e  .    UnlockArea.
bfd40 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20  lOffset = 0L;.  
bfd50 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61    UnlockArea.lRa
bfd60 6e 67 65 20 3d 20 30 4c 3b 0a 0a 20 20 20 20 2f  nge = 0L;..    /
bfd70 2a 20 77 61 69 74 20 6c 6f 6e 67 65 72 20 74 68  * wait longer th
bfd80 61 6e 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 20  an LOCK_TIMEOUT 
bfd90 68 65 72 65 20 6e 6f 74 20 74 6f 20 68 61 76 65  here not to have
bfda0 20 74 6f 20 74 72 79 20 6d 75 6c 74 69 70 6c 65   to try multiple
bfdb0 20 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20 72 65   times */.    re
bfdc0 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f  s = DosSetFileLo
bfdd0 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26  cks( pFile->h, &
bfde0 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63  UnlockArea, &Loc
bfdf0 6b 41 72 65 61 2c 20 31 30 30 4c 2c 20 30 4c 20  kArea, 100L, 0L 
bfe00 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d  );.    if( res =
bfe10 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
bfe20 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f      gotPendingLo
bfe30 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 4f 53  ck = 1;.      OS
bfe40 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64  TRACE3( "LOCK %d
bfe50 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62 6f   pending lock bo
bfe60 6f 6c 65 61 6e 20 73 65 74 2e 20 20 72 65 73 3d  olean set.  res=
bfe70 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
bfe80 20 72 65 73 20 29 3b 0a 20 20 20 20 7d 0a 20 20   res );.    }.  
bfe90 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20  }..  /* Acquire 
bfea0 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 0a 20 20  a shared lock.  
bfeb0 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70  */.  if( locktyp
bfec0 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26  e==SHARED_LOCK &
bfed0 26 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f  & res == NO_ERRO
bfee0 52 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  R ){.    assert(
bfef0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
bff00 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20  ==NO_LOCK );.   
bff10 20 72 65 73 20 3d 20 67 65 74 52 65 61 64 4c 6f   res = getReadLo
bff20 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 69  ck(pFile);.    i
bff30 66 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52  f( res == NO_ERR
bff40 4f 52 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c  OR ){.      newL
bff50 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44  ocktype = SHARED
bff60 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20  _LOCK;.    }.   
bff70 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43 4b   OSTRACE3( "LOCK
bff80 20 25 64 20 61 63 71 75 69 72 65 20 73 68 61 72   %d acquire shar
bff90 65 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c  ed lock. res=%d\
bffa0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65  n", pFile->h, re
bffb0 73 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  s );.  }..  /* A
bffc0 63 71 75 69 72 65 20 61 20 52 45 53 45 52 56 45  cquire a RESERVE
bffd0 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66  D lock.  */.  if
bffe0 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45  ( locktype==RESE
bfff0 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73  RVED_LOCK && res
c0000 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a   == NO_ERROR ){.
c0010 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c      assert( pFil
c0020 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  e->locktype==SHA
c0030 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20  RED_LOCK );.    
c0040 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  LockArea.lOffset
c0050 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45   = RESERVED_BYTE
c0060 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  ;.    LockArea.l
c0070 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20  Range = 1L;.    
c0080 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73  UnlockArea.lOffs
c0090 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c  et = 0L;.    Unl
c00a0 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
c00b0 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44   0L;.    res = D
c00c0 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20  osSetFileLocks( 
c00d0 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63  pFile->h, &Unloc
c00e0 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61  kArea, &LockArea
c00f0 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20  , LOCK_TIMEOUT, 
c0100 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72 65  0L );.    if( re
c0110 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  s == NO_ERROR ){
c0120 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79  .      newLockty
c0130 70 65 20 3d 20 52 45 53 45 52 56 45 44 5f 4c 4f  pe = RESERVED_LO
c0140 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53  CK;.    }.    OS
c0150 54 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64  TRACE3( "LOCK %d
c0160 20 61 63 71 75 69 72 65 20 72 65 73 65 72 76 65   acquire reserve
c0170 64 20 6c 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e  d lock. res=%d\n
c0180 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73  ", pFile->h, res
c0190 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63   );.  }..  /* Ac
c01a0 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20  quire a PENDING 
c01b0 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20  lock.  */.  if( 
c01c0 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53  locktype==EXCLUS
c01d0 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20  IVE_LOCK && res 
c01e0 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20  == NO_ERROR ){. 
c01f0 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d     newLocktype =
c0200 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20   PENDING_LOCK;. 
c0210 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63     gotPendingLoc
c0220 6b 20 3d 20 30 3b 0a 20 20 20 20 4f 53 54 52 41  k = 0;.    OSTRA
c0230 43 45 32 28 20 22 4c 4f 43 4b 20 25 64 20 61 63  CE2( "LOCK %d ac
c0240 71 75 69 72 65 20 70 65 6e 64 69 6e 67 20 6c 6f  quire pending lo
c0250 63 6b 2e 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b  ck. pending lock
c0260 20 62 6f 6f 6c 65 61 6e 20 75 6e 73 65 74 2e 5c   boolean unset.\
c0270 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a  n", pFile->h );.
c0280 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72    }..  /* Acquir
c0290 65 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  e an EXCLUSIVE l
c02a0 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c  ock.  */.  if( l
c02b0 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49  ocktype==EXCLUSI
c02c0 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d  VE_LOCK && res =
c02d0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
c02e0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
c02f0 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52 45  >locktype>=SHARE
c0300 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65  D_LOCK );.    re
c0310 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f  s = unlockReadLo
c0320 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f  ck(pFile);.    O
c0330 53 54 52 41 43 45 32 28 20 22 75 6e 72 65 61 64  STRACE2( "unread
c0340 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65  lock = %d\n", re
c0350 73 20 29 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65  s );.    LockAre
c0360 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 41 52  a.lOffset = SHAR
c0370 45 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 4c 6f  ED_FIRST;.    Lo
c0380 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
c0390 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20  SHARED_SIZE;.   
c03a0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66   UnlockArea.lOff
c03b0 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e  set = 0L;.    Un
c03c0 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
c03d0 3d 20 30 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20  = 0L;.    res = 
c03e0 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
c03f0 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
c0400 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
c0410 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
c0420 20 30 4c 20 29 3b 0a 20 20 20 20 69 66 28 20 72   0L );.    if( r
c0430 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29  es == NO_ERROR )
c0440 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74  {.      newLockt
c0450 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 5f  ype = EXCLUSIVE_
c0460 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  LOCK;.    }else{
c0470 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 28  .      OSTRACE2(
c0480 20 22 4f 53 2f 32 20 65 72 72 6f 72 2d 63 6f 64   "OS/2 error-cod
c0490 65 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20 29  e = %d\n", res )
c04a0 3b 0a 20 20 20 20 20 20 67 65 74 52 65 61 64 4c  ;.      getReadL
c04b0 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20  ock(pFile);.    
c04c0 7d 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20  }.    OSTRACE3( 
c04d0 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 72 65  "LOCK %d acquire
c04e0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e   exclusive lock.
c04f0 20 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69    res=%d\n", pFi
c0500 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20  le->h, res );.  
c0510 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 61 72  }..  /* If we ar
c0520 65 20 68 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44  e holding a PEND
c0530 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75  ING lock that ou
c0540 67 68 74 20 74 6f 20 62 65 20 72 65 6c 65 61 73  ght to be releas
c0550 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65  ed, then.  ** re
c0560 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20  lease it now..  
c0570 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50 65 6e 64  */.  if( gotPend
c0580 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74  ingLock && lockt
c0590 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
c05a0 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 3b 0a 20   ){.    int r;. 
c05b0 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66     LockArea.lOff
c05c0 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f  set = 0L;.    Lo
c05d0 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20  ckArea.lRange = 
c05e0 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72  0L;.    UnlockAr
c05f0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e  ea.lOffset = PEN
c0600 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55  DING_BYTE;.    U
c0610 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65  nlockArea.lRange
c0620 20 3d 20 31 4c 3b 0a 20 20 20 20 72 20 3d 20 44   = 1L;.    r = D
c0630 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20  osSetFileLocks( 
c0640 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63  pFile->h, &Unloc
c0650 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61  kArea, &LockArea
c0660 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20  , LOCK_TIMEOUT, 
c0670 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43  0L );.    OSTRAC
c0680 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 75 6e 6c  E3( "LOCK %d unl
c0690 6f 63 6b 69 6e 67 20 70 65 6e 64 69 6e 67 2f 69  ocking pending/i
c06a0 73 20 73 68 61 72 65 64 2e 20 72 3d 25 64 5c 6e  s shared. r=%d\n
c06b0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 20 29  ", pFile->h, r )
c06c0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61  ;.  }..  /* Upda
c06d0 74 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20  te the state of 
c06e0 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c  the lock has hel
c06f0 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65  d in the file de
c0700 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 20  scriptor then.  
c0710 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61 70  ** return the ap
c0720 70 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c 74  propriate result
c0730 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   code..  */.  if
c0740 28 20 72 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f  ( res == NO_ERRO
c0750 52 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  R ){.    rc = SQ
c0760 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65  LITE_OK;.  }else
c0770 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 20  {.    OSTRACE4( 
c0780 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20 25 64 20  "LOCK FAILED %d 
c0790 74 72 79 69 6e 67 20 66 6f 72 20 25 64 20 62 75  trying for %d bu
c07a0 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20 70 46 69  t got %d\n", pFi
c07b0 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20  le->h,.         
c07c0 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e       locktype, n
c07d0 65 77 4c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20  ewLocktype );.  
c07e0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55    rc = SQLITE_BU
c07f0 53 59 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d  SY;.  }.  pFile-
c0800 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6e 65 77 4c  >locktype = newL
c0810 6f 63 6b 74 79 70 65 3b 0a 20 20 4f 53 54 52 41  ocktype;.  OSTRA
c0820 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 6e 6f  CE3( "LOCK %d no
c0830 77 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  w %d\n", pFile->
c0840 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  h, pFile->lockty
c0850 70 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  pe );.  return r
c0860 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
c0870 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20   routine checks 
c0880 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45  if there is a RE
c0890 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64  SERVED lock held
c08a0 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65   on the specifie
c08b0 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69  d.** file by thi
c08c0 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70  s or any other p
c08d0 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20  rocess. If such 
c08e0 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20  a lock is held, 
c08f0 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65  return.** non-ze
c0900 72 6f 2c 20 6f 74 68 65 72 77 69 73 65 20 7a 65  ro, otherwise ze
c0910 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ro..*/.static in
c0920 74 20 6f 73 32 43 68 65 63 6b 52 65 73 65 72 76  t os2CheckReserv
c0930 65 64 4c 6f 63 6b 28 20 73 71 6c 69 74 65 33 5f  edLock( sqlite3_
c0940 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70  file *id, int *p
c0950 4f 75 74 20 29 7b 0a 20 20 69 6e 74 20 72 20 3d  Out ){.  int r =
c0960 20 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70   0;.  os2File *p
c0970 46 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a  File = (os2File*
c0980 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 70  )id;.  assert( p
c0990 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66 28  File!=0 );.  if(
c09a0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
c09b0 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  >=RESERVED_LOCK 
c09c0 29 7b 0a 20 20 20 20 72 20 3d 20 31 3b 0a 20 20  ){.    r = 1;.  
c09d0 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53    OSTRACE3( "TES
c09e0 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20  T WR-LOCK %d %d 
c09f0 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c  (local)\n", pFil
c0a00 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 65 6c  e->h, r );.  }el
c0a10 73 65 7b 0a 20 20 20 20 46 49 4c 45 4c 4f 43 4b  se{.    FILELOCK
c0a20 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20    LockArea,.    
c0a30 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63 6b            Unlock
c0a40 41 72 65 61 3b 0a 20 20 20 20 41 50 49 52 45 54  Area;.    APIRET
c0a50 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a   rc = NO_ERROR;.
c0a60 20 20 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b      memset(&Lock
c0a70 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Area, 0, sizeof(
c0a80 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 20  LockArea));.    
c0a90 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72  memset(&UnlockAr
c0aa0 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e  ea, 0, sizeof(Un
c0ab0 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 20 20  lockArea));.    
c0ac0 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  LockArea.lOffset
c0ad0 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45   = RESERVED_BYTE
c0ae0 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c  ;.    LockArea.l
c0af0 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20  Range = 1L;.    
c0b00 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73  UnlockArea.lOffs
c0b10 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c  et = 0L;.    Unl
c0b20 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
c0b30 20 30 4c 3b 0a 20 20 20 20 72 63 20 3d 20 44 6f   0L;.    rc = Do
c0b40 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70  sSetFileLocks( p
c0b50 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b  File->h, &Unlock
c0b60 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c  Area, &LockArea,
c0b70 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30   LOCK_TIMEOUT, 0
c0b80 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45  L );.    OSTRACE
c0b90 33 28 20 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b  3( "TEST WR-LOCK
c0ba0 20 25 64 20 6c 6f 63 6b 20 72 65 73 65 72 76 65   %d lock reserve
c0bb0 64 20 62 79 74 65 20 72 63 3d 25 64 5c 6e 22 2c  d byte rc=%d\n",
c0bc0 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 20 29 3b   pFile->h, rc );
c0bd0 0a 20 20 20 20 69 66 28 20 72 63 20 3d 3d 20 4e  .    if( rc == N
c0be0 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20  O_ERROR ){.     
c0bf0 20 41 50 49 52 45 54 20 72 63 75 20 3d 20 4e 4f   APIRET rcu = NO
c0c00 5f 45 52 52 4f 52 3b 20 2f 2a 20 72 65 74 75 72  _ERROR; /* retur
c0c10 6e 20 63 6f 64 65 20 66 6f 72 20 75 6e 6c 6f 63  n code for unloc
c0c20 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 4c 6f  king */.      Lo
c0c30 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
c0c40 20 30 4c 3b 0a 20 20 20 20 20 20 4c 6f 63 6b 41   0L;.      LockA
c0c50 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b  rea.lRange = 0L;
c0c60 0a 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65  .      UnlockAre
c0c70 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45  a.lOffset = RESE
c0c80 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 20  RVED_BYTE;.     
c0c90 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e   UnlockArea.lRan
c0ca0 67 65 20 3d 20 31 4c 3b 0a 20 20 20 20 20 20 72  ge = 1L;.      r
c0cb0 63 75 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c  cu = DosSetFileL
c0cc0 6f 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20  ocks( pFile->h, 
c0cd0 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f  &UnlockArea, &Lo
c0ce0 63 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d  ckArea, LOCK_TIM
c0cf0 45 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20  EOUT, 0L );.    
c0d00 20 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53    OSTRACE3( "TES
c0d10 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 75 6e 6c  T WR-LOCK %d unl
c0d20 6f 63 6b 20 72 65 73 65 72 76 65 64 20 62 79 74  ock reserved byt
c0d30 65 20 72 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  e r=%d\n", pFile
c0d40 2d 3e 68 2c 20 72 63 75 20 29 3b 0a 20 20 20 20  ->h, rcu );.    
c0d50 7d 0a 20 20 20 20 72 20 3d 20 21 28 72 63 20 3d  }.    r = !(rc =
c0d60 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 20 20  = NO_ERROR);.   
c0d70 20 4f 53 54 52 41 43 45 33 28 20 22 54 45 53 54   OSTRACE3( "TEST
c0d80 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28   WR-LOCK %d %d (
c0d90 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69 6c  remote)\n", pFil
c0da0 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20 7d 0a 20  e->h, r );.  }. 
c0db0 20 2a 70 4f 75 74 20 3d 20 72 3b 0a 20 20 72 65   *pOut = r;.  re
c0dc0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
c0dd0 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74  }../*.** Lower t
c0de0 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c  he locking level
c0df0 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70   on file descrip
c0e00 74 6f 72 20 69 64 20 74 6f 20 6c 6f 63 6b 74 79  tor id to lockty
c0e10 70 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a  pe.  locktype.**
c0e20 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20   must be either 
c0e30 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45  NO_LOCK or SHARE
c0e40 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66  D_LOCK..**.** If
c0e50 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76   the locking lev
c0e60 65 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64  el of the file d
c0e70 65 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72  escriptor is alr
c0e80 65 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77  eady at or below
c0e90 0a 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65  .** the requeste
c0ea0 64 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c  d locking level,
c0eb0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
c0ec0 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20   a no-op..**.** 
c0ed0 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62  It is not possib
c0ee0 6c 65 20 66 6f 72 20 74 68 69 73 20 72 6f 75 74  le for this rout
c0ef0 69 6e 65 20 74 6f 20 66 61 69 6c 20 69 66 20 74  ine to fail if t
c0f00 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
c0f10 6e 74 0a 2a 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b  nt.** is NO_LOCK
c0f20 2e 20 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64  .  If the second
c0f30 20 61 72 67 75 6d 65 6e 74 20 69 73 20 53 48 41   argument is SHA
c0f40 52 45 44 5f 4c 4f 43 4b 20 74 68 65 6e 20 74 68  RED_LOCK then th
c0f50 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69  is routine.** mi
c0f60 67 68 74 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ght return SQLIT
c0f70 45 5f 49 4f 45 52 52 3b 0a 2a 2f 0a 73 74 61 74  E_IOERR;.*/.stat
c0f80 69 63 20 69 6e 74 20 6f 73 32 55 6e 6c 6f 63 6b  ic int os2Unlock
c0f90 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  ( sqlite3_file *
c0fa0 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65  id, int locktype
c0fb0 20 29 7b 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a   ){.  int type;.
c0fc0 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65    os2File *pFile
c0fd0 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b   = (os2File*)id;
c0fe0 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 53  .  APIRET rc = S
c0ff0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 41 50 49 52  QLITE_OK;.  APIR
c1000 45 54 20 72 65 73 20 3d 20 4e 4f 5f 45 52 52 4f  ET res = NO_ERRO
c1010 52 3b 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c  R;.  FILELOCK  L
c1020 6f 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20  ockArea,.       
c1030 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b       UnlockArea;
c1040 0a 20 20 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41  .  memset(&LockA
c1050 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c  rea, 0, sizeof(L
c1060 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 6d 65 6d  ockArea));.  mem
c1070 73 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c  set(&UnlockArea,
c1080 20 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63   0, sizeof(Unloc
c1090 6b 41 72 65 61 29 29 3b 0a 20 20 61 73 73 65 72  kArea));.  asser
c10a0 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a 20  t( pFile!=0 );. 
c10b0 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
c10c0 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e<=SHARED_LOCK )
c10d0 3b 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 55  ;.  OSTRACE4( "U
c10e0 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 77  NLOCK %d to %d w
c10f0 61 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  as %d\n", pFile-
c1100 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46  >h, locktype, pF
c1110 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b  ile->locktype );
c1120 0a 20 20 74 79 70 65 20 3d 20 70 46 69 6c 65 2d  .  type = pFile-
c1130 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28  >locktype;.  if(
c1140 20 74 79 70 65 3e 3d 45 58 43 4c 55 53 49 56 45   type>=EXCLUSIVE
c1150 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63  _LOCK ){.    Loc
c1160 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
c1170 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61  0L;.    LockArea
c1180 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20  .lRange = 0L;.  
c1190 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66    UnlockArea.lOf
c11a0 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46 49  fset = SHARED_FI
c11b0 52 53 54 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41  RST;.    UnlockA
c11c0 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41  rea.lRange = SHA
c11d0 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 72 65  RED_SIZE;.    re
c11e0 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f  s = DosSetFileLo
c11f0 63 6b 73 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26  cks( pFile->h, &
c1200 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63  UnlockArea, &Loc
c1210 6b 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45  kArea, LOCK_TIME
c1220 4f 55 54 2c 20 30 4c 20 29 3b 0a 20 20 20 20 4f  OUT, 0L );.    O
c1230 53 54 52 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b  STRACE3( "UNLOCK
c1240 20 25 64 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   %d exclusive lo
c1250 63 6b 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46  ck res=%d\n", pF
c1260 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20  ile->h, res );. 
c1270 20 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d     if( locktype=
c1280 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20  =SHARED_LOCK && 
c1290 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c  getReadLock(pFil
c12a0 65 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29  e) != NO_ERROR )
c12b0 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20  {.      /* This 
c12c0 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70  should never hap
c12d0 70 65 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64 20  pen.  We should 
c12e0 61 6c 77 61 79 73 20 62 65 20 61 62 6c 65 20 74  always be able t
c12f0 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61 63 71  o.      ** reacq
c1300 75 69 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f  uire the read lo
c1310 63 6b 20 2a 2f 0a 20 20 20 20 20 20 4f 53 54 52  ck */.      OSTR
c1320 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64  ACE3( "UNLOCK %d
c1330 20 74 6f 20 25 64 20 67 65 74 52 65 61 64 4c 6f   to %d getReadLo
c1340 63 6b 28 29 20 66 61 69 6c 65 64 5c 6e 22 2c 20  ck() failed\n", 
c1350 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79  pFile->h, lockty
c1360 70 65 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  pe );.      rc =
c1370 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e   SQLITE_IOERR_UN
c1380 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  LOCK;.    }.  }.
c1390 20 20 69 66 28 20 74 79 70 65 3e 3d 52 45 53 45    if( type>=RESE
c13a0 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  RVED_LOCK ){.   
c13b0 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65   LockArea.lOffse
c13c0 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b  t = 0L;.    Lock
c13d0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c  Area.lRange = 0L
c13e0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ;.    UnlockArea
c13f0 2e 6c 4f 66 66 73 65 74 20 3d 20 52 45 53 45 52  .lOffset = RESER
c1400 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e  VED_BYTE;.    Un
c1410 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
c1420 3d 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20  = 1L;.    res = 
c1430 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
c1440 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
c1450 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
c1460 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
c1470 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41   0L );.    OSTRA
c1480 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20  CE3( "UNLOCK %d 
c1490 72 65 73 65 72 76 65 64 20 72 65 73 3d 25 64 5c  reserved res=%d\
c14a0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65  n", pFile->h, re
c14b0 73 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6c  s );.  }.  if( l
c14c0 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b  ocktype==NO_LOCK
c14d0 20 26 26 20 74 79 70 65 3e 3d 53 48 41 52 45 44   && type>=SHARED
c14e0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73  _LOCK ){.    res
c14f0 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63   = unlockReadLoc
c1500 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53  k(pFile);.    OS
c1510 54 52 41 43 45 35 28 20 22 55 4e 4c 4f 43 4b 20  TRACE5( "UNLOCK 
c1520 25 64 20 69 73 20 25 64 20 77 61 6e 74 20 25 64  %d is %d want %d
c1530 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c   res=%d\n", pFil
c1540 65 2d 3e 68 2c 20 74 79 70 65 2c 20 6c 6f 63 6b  e->h, type, lock
c1550 74 79 70 65 2c 20 72 65 73 20 29 3b 0a 20 20 7d  type, res );.  }
c1560 0a 20 20 69 66 28 20 74 79 70 65 3e 3d 50 45 4e  .  if( type>=PEN
c1570 44 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  DING_LOCK ){.   
c1580 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65   LockArea.lOffse
c1590 74 20 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b  t = 0L;.    Lock
c15a0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c  Area.lRange = 0L
c15b0 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61  ;.    UnlockArea
c15c0 2e 6c 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49  .lOffset = PENDI
c15d0 4e 47 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c  NG_BYTE;.    Unl
c15e0 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
c15f0 20 31 4c 3b 0a 20 20 20 20 72 65 73 20 3d 20 44   1L;.    res = D
c1600 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20  osSetFileLocks( 
c1610 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63  pFile->h, &Unloc
c1620 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61  kArea, &LockArea
c1630 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20  , LOCK_TIMEOUT, 
c1640 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41 43  0L );.    OSTRAC
c1650 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 70  E3( "UNLOCK %d p
c1660 65 6e 64 69 6e 67 20 72 65 73 3d 25 64 5c 6e 22  ending res=%d\n"
c1670 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20  , pFile->h, res 
c1680 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e  );.  }.  pFile->
c1690 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74  locktype = lockt
c16a0 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45 33 28  ype;.  OSTRACE3(
c16b0 20 22 55 4e 4c 4f 43 4b 20 25 64 20 6e 6f 77 20   "UNLOCK %d now 
c16c0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
c16d0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
c16e0 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   );.  return rc;
c16f0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f  .}../*.** Contro
c1700 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66 20 74  l and query of t
c1710 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e  he open file han
c1720 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  dle..*/.static i
c1730 6e 74 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72 6f  nt os2FileContro
c1740 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  l(sqlite3_file *
c1750 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64  id, int op, void
c1760 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63   *pArg){.  switc
c1770 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73  h( op ){.    cas
c1780 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c  e SQLITE_FCNTL_L
c1790 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20  OCKSTATE: {.    
c17a0 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20    *(int*)pArg = 
c17b0 28 28 6f 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e  ((os2File*)id)->
c17c0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20  locktype;.      
c17d0 4f 53 54 52 41 43 45 33 28 20 22 46 43 4e 54 4c  OSTRACE3( "FCNTL
c17e0 5f 4c 4f 43 4b 53 54 41 54 45 20 25 64 20 6c 6f  _LOCKSTATE %d lo
c17f0 63 6b 3d 25 64 5c 6e 22 2c 20 28 28 6f 73 32 46  ck=%d\n", ((os2F
c1800 69 6c 65 2a 29 69 64 29 2d 3e 68 2c 20 28 28 6f  ile*)id)->h, ((o
c1810 73 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63  s2File*)id)->loc
c1820 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 20 20 72  ktype );.      r
c1830 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
c1840 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
c1850 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
c1860 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
c1870 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a  n the sector siz
c1880 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68  e in bytes of th
c1890 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f  e underlying blo
c18a0 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a  ck device for.**
c18b0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66   the specified f
c18c0 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d  ile. This is alm
c18d0 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 62  ost always 512 b
c18e0 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65  ytes, but may be
c18f0 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73  .** larger for s
c1900 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a  ome devices..**.
c1910 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61  ** SQLite code a
c1920 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63  ssumes this func
c1930 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c  tion cannot fail
c1940 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65  . It also assume
c1950 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f  s that.** if two
c1960 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 74   files are creat
c1970 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66  ed in the same f
c1980 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63  ile-system direc
c1990 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20  tory (i.e..** a 
c19a0 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 73  database and its
c19b0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74   journal file) t
c19c0 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 73  hat the sector s
c19d0 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a  ize will be the.
c19e0 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68  ** same for both
c19f0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
c1a00 6f 73 32 53 65 63 74 6f 72 53 69 7a 65 28 73 71  os2SectorSize(sq
c1a10 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b  lite3_file *id){
c1a20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
c1a30 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f 52 5f  _DEFAULT_SECTOR_
c1a40 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  SIZE;.}../*.** R
c1a50 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72 20 6f  eturn a vector o
c1a60 66 20 64 65 76 69 63 65 20 63 68 61 72 61 63 74  f device charact
c1a70 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73 74 61  eristics..*/.sta
c1a80 74 69 63 20 69 6e 74 20 6f 73 32 44 65 76 69 63  tic int os2Devic
c1a90 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
c1aa0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
c1ab0 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  d){.  return 0;.
c1ac0 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61 63  }.../*.** Charac
c1ad0 74 65 72 20 73 65 74 20 63 6f 6e 76 65 72 73 69  ter set conversi
c1ae0 6f 6e 20 6f 62 6a 65 63 74 73 20 75 73 65 64 20  on objects used 
c1af0 62 79 20 63 6f 6e 76 65 72 73 69 6f 6e 20 72 6f  by conversion ro
c1b00 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  utines..*/.stati
c1b10 63 20 55 63 6f 6e 76 4f 62 6a 65 63 74 20 75 63  c UconvObject uc
c1b20 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 20 2f 2a 20  Utf8 = NULL; /* 
c1b30 63 6f 6e 76 65 72 74 20 62 65 74 77 65 65 6e 20  convert between 
c1b40 55 54 46 2d 38 20 61 6e 64 20 55 43 53 2d 32 20  UTF-8 and UCS-2 
c1b50 2a 2f 0a 73 74 61 74 69 63 20 55 63 6f 6e 76 4f  */.static UconvO
c1b60 62 6a 65 63 74 20 75 63 6c 43 70 20 3d 20 4e 55  bject uclCp = NU
c1b70 4c 4c 3b 20 20 2f 2a 20 63 6f 6e 76 65 72 74 20  LL;  /* convert 
c1b80 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 20 63 6f  between local co
c1b90 64 65 70 61 67 65 20 61 6e 64 20 55 43 53 2d 32  depage and UCS-2
c1ba0 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65   */../*.** Helpe
c1bb0 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e  r function to in
c1bc0 69 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e  itialize the con
c1bd0 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63 74 73 20  version objects 
c1be0 66 72 6f 6d 20 61 6e 64 20 74 6f 20 55 54 46 2d  from and to UTF-
c1bf0 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  8..*/.static voi
c1c00 64 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63  d initUconvObjec
c1c10 74 73 28 20 76 6f 69 64 20 29 7b 0a 20 20 69 66  ts( void ){.  if
c1c20 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f 6e 76  ( UniCreateUconv
c1c30 4f 62 6a 65 63 74 28 20 55 54 46 5f 38 2c 20 26  Object( UTF_8, &
c1c40 75 63 55 74 66 38 20 29 20 21 3d 20 55 4c 53 5f  ucUtf8 ) != ULS_
c1c50 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 75 63  SUCCESS ).    uc
c1c60 55 74 66 38 20 3d 20 4e 55 4c 4c 3b 0a 20 20 69  Utf8 = NULL;.  i
c1c70 66 20 28 20 55 6e 69 43 72 65 61 74 65 55 63 6f  f ( UniCreateUco
c1c80 6e 76 4f 62 6a 65 63 74 28 20 28 55 6e 69 43 68  nvObject( (UniCh
c1c90 61 72 20 2a 29 4c 22 40 70 61 74 68 3d 79 65 73  ar *)L"@path=yes
c1ca0 22 2c 20 26 75 63 6c 43 70 20 29 20 21 3d 20 55  ", &uclCp ) != U
c1cb0 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20 20  LS_SUCCESS ).   
c1cc0 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 0a 7d   uclCp = NULL;.}
c1cd0 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66  ../*.** Helper f
c1ce0 75 6e 63 74 69 6f 6e 20 74 6f 20 66 72 65 65 20  unction to free 
c1cf0 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f  the conversion o
c1d00 62 6a 65 63 74 73 20 66 72 6f 6d 20 61 6e 64 20  bjects from and 
c1d10 74 6f 20 55 54 46 2d 38 2e 0a 2a 2f 0a 73 74 61  to UTF-8..*/.sta
c1d20 74 69 63 20 76 6f 69 64 20 66 72 65 65 55 63 6f  tic void freeUco
c1d30 6e 76 4f 62 6a 65 63 74 73 28 20 76 6f 69 64 20  nvObjects( void 
c1d40 29 7b 0a 20 20 69 66 20 28 20 75 63 55 74 66 38  ){.  if ( ucUtf8
c1d50 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65 55 63   ).    UniFreeUc
c1d60 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 55 74 66  onvObject( ucUtf
c1d70 38 20 29 3b 0a 20 20 69 66 20 28 20 75 63 6c 43  8 );.  if ( uclC
c1d80 70 20 29 0a 20 20 20 20 55 6e 69 46 72 65 65 55  p ).    UniFreeU
c1d90 63 6f 6e 76 4f 62 6a 65 63 74 28 20 75 63 6c 43  convObject( uclC
c1da0 70 20 29 3b 0a 20 20 75 63 55 74 66 38 20 3d 20  p );.  ucUtf8 = 
c1db0 4e 55 4c 4c 3b 0a 20 20 75 63 6c 43 70 20 3d 20  NULL;.  uclCp = 
c1dc0 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48  NULL;.}../*.** H
c1dd0 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74  elper function t
c1de0 6f 20 63 6f 6e 76 65 72 74 20 55 54 46 2d 38 20  o convert UTF-8 
c1df0 66 69 6c 65 6e 61 6d 65 73 20 74 6f 20 6c 6f 63  filenames to loc
c1e00 61 6c 20 4f 53 2f 32 20 63 6f 64 65 70 61 67 65  al OS/2 codepage
c1e10 2e 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 65  ..** The two-ste
c1e20 70 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73 74  p process: first
c1e30 20 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e 63   convert the inc
c1e40 6f 6d 69 6e 67 20 55 54 46 2d 38 20 73 74 72 69  oming UTF-8 stri
c1e50 6e 67 0a 2a 2a 20 69 6e 74 6f 20 55 43 53 2d 32  ng.** into UCS-2
c1e60 20 61 6e 64 20 74 68 65 6e 20 66 72 6f 6d 20 55   and then from U
c1e70 43 53 2d 32 20 74 6f 20 74 68 65 20 63 75 72 72  CS-2 to the curr
c1e80 65 6e 74 20 63 6f 64 65 70 61 67 65 2e 0a 2a 2a  ent codepage..**
c1e90 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 63 68   The returned ch
c1ea0 61 72 20 70 6f 69 6e 74 65 72 20 68 61 73 20 74  ar pointer has t
c1eb0 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2f 0a 73  o be freed..*/.s
c1ec0 74 61 74 69 63 20 63 68 61 72 20 2a 63 6f 6e 76  tatic char *conv
c1ed0 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28  ertUtf8PathToCp(
c1ee0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 69 6e 20   const char *in 
c1ef0 29 7b 0a 20 20 55 6e 69 43 68 61 72 20 74 65 6d  ){.  UniChar tem
c1f00 70 50 61 74 68 5b 43 43 48 4d 41 58 50 41 54 48  pPath[CCHMAXPATH
c1f10 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75 74 20 3d  ];.  char *out =
c1f20 20 28 63 68 61 72 20 2a 29 63 61 6c 6c 6f 63 28   (char *)calloc(
c1f30 20 43 43 48 4d 41 58 50 41 54 48 2c 20 31 20 29   CCHMAXPATH, 1 )
c1f40 3b 0a 0a 20 20 69 66 28 20 21 6f 75 74 20 29 0a  ;..  if( !out ).
c1f50 20 20 20 20 72 65 74 75 72 6e 20 4e 55 4c 4c 3b      return NULL;
c1f60 0a 0a 20 20 69 66 28 20 21 75 63 55 74 66 38 20  ..  if( !ucUtf8 
c1f70 7c 7c 20 21 75 63 6c 43 70 20 29 0a 20 20 20 20  || !uclCp ).    
c1f80 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73  initUconvObjects
c1f90 28 29 3b 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d  ();..  /* determ
c1fa0 69 6e 65 20 73 74 72 69 6e 67 20 66 6f 72 20 74  ine string for t
c1fb0 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66  he conversion of
c1fc0 20 55 54 46 2d 38 20 77 68 69 63 68 20 69 73 20   UTF-8 which is 
c1fd0 43 50 31 32 30 38 20 2a 2f 0a 20 20 69 66 28 20  CP1208 */.  if( 
c1fe0 55 6e 69 53 74 72 54 6f 55 63 73 28 20 75 63 55  UniStrToUcs( ucU
c1ff0 74 66 38 2c 20 74 65 6d 70 50 61 74 68 2c 20 28  tf8, tempPath, (
c2000 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d 41  char *)in, CCHMA
c2010 58 50 41 54 48 20 29 20 21 3d 20 55 4c 53 5f 53  XPATH ) != ULS_S
c2020 55 43 43 45 53 53 20 29 0a 20 20 20 20 72 65 74  UCCESS ).    ret
c2030 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 20 63  urn out; /* if c
c2040 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c 73 2c  onversion fails,
c2050 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d 70 74   return the empt
c2060 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 2f  y string */..  /
c2070 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72  * conversion for
c2080 20 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67   current codepag
c2090 65 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 75  e which can be u
c20a0 73 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a 2f  sed for paths */
c20b0 0a 20 20 55 6e 69 53 74 72 46 72 6f 6d 55 63 73  .  UniStrFromUcs
c20c0 28 20 75 63 6c 43 70 2c 20 6f 75 74 2c 20 74 65  ( uclCp, out, te
c20d0 6d 70 50 61 74 68 2c 20 43 43 48 4d 41 58 50 41  mpPath, CCHMAXPA
c20e0 54 48 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  TH );..  return 
c20f0 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65  out;.}../*.** He
c2100 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f  lper function to
c2110 20 63 6f 6e 76 65 72 74 20 66 69 6c 65 6e 61 6d   convert filenam
c2120 65 73 20 66 72 6f 6d 20 6c 6f 63 61 6c 20 63 6f  es from local co
c2130 64 65 70 61 67 65 20 74 6f 20 55 54 46 2d 38 2e  depage to UTF-8.
c2140 0a 2a 2a 20 54 68 65 20 74 77 6f 2d 73 74 65 70  .** The two-step
c2150 20 70 72 6f 63 65 73 73 3a 20 66 69 72 73 74 20   process: first 
c2160 63 6f 6e 76 65 72 74 20 74 68 65 20 69 6e 63 6f  convert the inco
c2170 6d 69 6e 67 20 63 6f 64 65 70 61 67 65 2d 73 70  ming codepage-sp
c2180 65 63 69 66 69 63 0a 2a 2a 20 73 74 72 69 6e 67  ecific.** string
c2190 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e 64 20   into UCS-2 and 
c21a0 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d 32 20  then from UCS-2 
c21b0 74 6f 20 74 68 65 20 63 6f 64 65 70 61 67 65 20  to the codepage 
c21c0 6f 66 20 55 54 46 2d 38 2e 0a 2a 2a 20 54 68 65  of UTF-8..** The
c21d0 20 72 65 74 75 72 6e 65 64 20 63 68 61 72 20 70   returned char p
c21e0 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20 62 65  ointer has to be
c21f0 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68   freed..**.** Th
c2200 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e  is function is n
c2210 6f 6e 2d 73 74 61 74 69 63 20 74 6f 20 62 65 20  on-static to be 
c2220 61 62 6c 65 20 74 6f 20 75 73 65 20 74 68 69 73  able to use this
c2230 20 69 6e 20 73 68 65 6c 6c 2e 63 20 61 6e 64 0a   in shell.c and.
c2240 2a 2a 20 73 69 6d 69 6c 61 72 20 61 70 70 6c 69  ** similar appli
c2250 63 61 74 69 6f 6e 73 20 74 68 61 74 20 74 61 6b  cations that tak
c2260 65 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 20 61  e command line a
c2270 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 63 68 61  rguments..*/.cha
c2280 72 20 2a 63 6f 6e 76 65 72 74 43 70 50 61 74 68  r *convertCpPath
c2290 54 6f 55 74 66 38 28 20 63 6f 6e 73 74 20 63 68  ToUtf8( const ch
c22a0 61 72 20 2a 69 6e 20 29 7b 0a 20 20 55 6e 69 43  ar *in ){.  UniC
c22b0 68 61 72 20 74 65 6d 70 50 61 74 68 5b 43 43 48  har tempPath[CCH
c22c0 4d 41 58 50 41 54 48 5d 3b 0a 20 20 63 68 61 72  MAXPATH];.  char
c22d0 20 2a 6f 75 74 20 3d 20 28 63 68 61 72 20 2a 29   *out = (char *)
c22e0 63 61 6c 6c 6f 63 28 20 43 43 48 4d 41 58 50 41  calloc( CCHMAXPA
c22f0 54 48 2c 20 31 20 29 3b 0a 0a 20 20 69 66 28 20  TH, 1 );..  if( 
c2300 21 6f 75 74 20 29 0a 20 20 20 20 72 65 74 75 72  !out ).    retur
c2310 6e 20 4e 55 4c 4c 3b 0a 0a 20 20 69 66 28 20 21  n NULL;..  if( !
c2320 75 63 55 74 66 38 20 7c 7c 20 21 75 63 6c 43 70  ucUtf8 || !uclCp
c2330 20 29 0a 20 20 20 20 69 6e 69 74 55 63 6f 6e 76   ).    initUconv
c2340 4f 62 6a 65 63 74 73 28 29 3b 0a 0a 20 20 2f 2a  Objects();..  /*
c2350 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20   conversion for 
c2360 63 75 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65  current codepage
c2370 20 77 68 69 63 68 20 63 61 6e 20 62 65 20 75 73   which can be us
c2380 65 64 20 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a  ed for paths */.
c2390 20 20 69 66 28 20 55 6e 69 53 74 72 54 6f 55 63    if( UniStrToUc
c23a0 73 28 20 75 63 6c 43 70 2c 20 74 65 6d 70 50 61  s( uclCp, tempPa
c23b0 74 68 2c 20 28 63 68 61 72 20 2a 29 69 6e 2c 20  th, (char *)in, 
c23c0 43 43 48 4d 41 58 50 41 54 48 20 29 20 21 3d 20  CCHMAXPATH ) != 
c23d0 55 4c 53 5f 53 55 43 43 45 53 53 20 29 0a 20 20  ULS_SUCCESS ).  
c23e0 20 20 72 65 74 75 72 6e 20 6f 75 74 3b 20 2f 2a    return out; /*
c23f0 20 69 66 20 63 6f 6e 76 65 72 73 69 6f 6e 20 66   if conversion f
c2400 61 69 6c 73 2c 20 72 65 74 75 72 6e 20 74 68 65  ails, return the
c2410 20 65 6d 70 74 79 20 73 74 72 69 6e 67 20 2a 2f   empty string */
c2420 0a 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e 65  ..  /* determine
c2430 20 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20   string for the 
c2440 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55 54  conversion of UT
c2450 46 2d 38 20 77 68 69 63 68 20 69 73 20 43 50 31  F-8 which is CP1
c2460 32 30 38 20 2a 2f 0a 20 20 55 6e 69 53 74 72 46  208 */.  UniStrF
c2470 72 6f 6d 55 63 73 28 20 75 63 55 74 66 38 2c 20  romUcs( ucUtf8, 
c2480 6f 75 74 2c 20 74 65 6d 70 50 61 74 68 2c 20 43  out, tempPath, C
c2490 43 48 4d 41 58 50 41 54 48 20 29 3b 0a 0a 20 20  CHMAXPATH );..  
c24a0 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f  return out;.}../
c24b0 2a 0a 2a 2a 20 54 68 69 73 20 76 65 63 74 6f 72  *.** This vector
c24c0 20 64 65 66 69 6e 65 73 20 61 6c 6c 20 74 68 65   defines all the
c24d0 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 63 61   methods that ca
c24e0 6e 20 6f 70 65 72 61 74 65 20 6f 6e 20 61 6e 0a  n operate on an.
c24f0 2a 2a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ** sqlite3_file 
c2500 66 6f 72 20 6f 73 32 2e 0a 2a 2f 0a 73 74 61 74  for os2..*/.stat
c2510 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
c2520 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 73 32 49  _io_methods os2I
c2530 6f 4d 65 74 68 6f 64 20 3d 20 7b 0a 20 20 31 2c  oMethod = {.  1,
c2540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2550 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73          /* iVers
c2560 69 6f 6e 20 2a 2f 0a 20 20 6f 73 32 43 6c 6f 73  ion */.  os2Clos
c2570 65 2c 0a 20 20 6f 73 32 52 65 61 64 2c 0a 20 20  e,.  os2Read,.  
c2580 6f 73 32 57 72 69 74 65 2c 0a 20 20 6f 73 32 54  os2Write,.  os2T
c2590 72 75 6e 63 61 74 65 2c 0a 20 20 6f 73 32 53 79  runcate,.  os2Sy
c25a0 6e 63 2c 0a 20 20 6f 73 32 46 69 6c 65 53 69 7a  nc,.  os2FileSiz
c25b0 65 2c 0a 20 20 6f 73 32 4c 6f 63 6b 2c 0a 20 20  e,.  os2Lock,.  
c25c0 6f 73 32 55 6e 6c 6f 63 6b 2c 0a 20 20 6f 73 32  os2Unlock,.  os2
c25d0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
c25e0 6b 2c 0a 20 20 6f 73 32 46 69 6c 65 43 6f 6e 74  k,.  os2FileCont
c25f0 72 6f 6c 2c 0a 20 20 6f 73 32 53 65 63 74 6f 72  rol,.  os2Sector
c2600 53 69 7a 65 2c 0a 20 20 6f 73 32 44 65 76 69 63  Size,.  os2Devic
c2610 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73  eCharacteristics
c2620 0a 7d 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .};../**********
c2630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2670 2a 0a 2a 2a 20 48 65 72 65 20 65 6e 64 73 20 74  *.** Here ends t
c2680 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 74  he I/O methods t
c2690 68 61 74 20 66 6f 72 6d 20 74 68 65 20 73 71 6c  hat form the sql
c26a0 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
c26b0 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  object..**.** Th
c26c0 65 20 6e 65 78 74 20 62 6c 6f 63 6b 20 6f 66 20  e next block of 
c26d0 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  code implements 
c26e0 74 68 65 20 56 46 53 20 6d 65 74 68 6f 64 73 2e  the VFS methods.
c26f0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
c2700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c2730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
c2740 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 74  /*.** Create a t
c2750 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61  emporary file na
c2760 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42 75  me in zBuf.  zBu
c2770 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 65 6e  f must be big en
c2780 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20  ough to.** hold 
c2790 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e  at pVfs->mxPathn
c27a0 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 2e 0a  ame characters..
c27b0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
c27c0 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42  tTempname(int nB
c27d0 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 20 29  uf, char *zBuf )
c27e0 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  {.  static const
c27f0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a   unsigned char z
c2800 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61  Chars[] =.    "a
c2810 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71  bcdefghijklmnopq
c2820 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22  rstuvwxyz".    "
c2830 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50  ABCDEFGHIJKLMNOP
c2840 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20  QRSTUVWXYZ".    
c2850 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20  "0123456789";.  
c2860 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61 72  int i, j;.  char
c2870 20 7a 54 65 6d 70 50 61 74 68 42 75 66 5b 33 5d   zTempPathBuf[3]
c2880 3b 0a 20 20 50 53 5a 20 7a 54 65 6d 70 50 61 74  ;.  PSZ zTempPat
c2890 68 20 3d 20 28 50 53 5a 29 26 7a 54 65 6d 70 50  h = (PSZ)&zTempP
c28a0 61 74 68 42 75 66 3b 0a 20 20 69 66 28 20 73 71  athBuf;.  if( sq
c28b0 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63  lite3_temp_direc
c28c0 74 6f 72 79 20 29 7b 0a 20 20 20 20 7a 54 65 6d  tory ){.    zTem
c28d0 70 50 61 74 68 20 3d 20 73 71 6c 69 74 65 33 5f  pPath = sqlite3_
c28e0 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 3b 0a  temp_directory;.
c28f0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28    }else{.    if(
c2900 20 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53   DosScanEnv( (PS
c2910 5a 29 22 54 45 4d 50 22 2c 20 26 7a 54 65 6d 70  Z)"TEMP", &zTemp
c2920 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20  Path ) ){.      
c2930 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20  if( DosScanEnv( 
c2940 28 50 53 5a 29 22 54 4d 50 22 2c 20 26 7a 54 65  (PSZ)"TMP", &zTe
c2950 6d 70 50 61 74 68 20 29 20 29 7b 0a 20 20 20 20  mpPath ) ){.    
c2960 20 20 20 20 69 66 28 20 44 6f 73 53 63 61 6e 45      if( DosScanE
c2970 6e 76 28 20 28 50 53 5a 29 22 54 4d 50 44 49 52  nv( (PSZ)"TMPDIR
c2980 22 2c 20 26 7a 54 65 6d 70 50 61 74 68 20 29 20  ", &zTempPath ) 
c2990 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 55 4c  ){.           UL
c29a0 4f 4e 47 20 75 6c 44 72 69 76 65 4e 75 6d 20 3d  ONG ulDriveNum =
c29b0 20 30 2c 20 75 6c 44 72 69 76 65 4d 61 70 20 3d   0, ulDriveMap =
c29c0 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 44   0;.           D
c29d0 6f 73 51 75 65 72 79 43 75 72 72 65 6e 74 44 69  osQueryCurrentDi
c29e0 73 6b 28 20 26 75 6c 44 72 69 76 65 4e 75 6d 2c  sk( &ulDriveNum,
c29f0 20 26 75 6c 44 72 69 76 65 4d 61 70 20 29 3b 0a   &ulDriveMap );.
c2a00 20 20 20 20 20 20 20 20 20 20 20 73 70 72 69 6e             sprin
c2a10 74 66 28 20 28 63 68 61 72 2a 29 7a 54 65 6d 70  tf( (char*)zTemp
c2a20 50 61 74 68 2c 20 22 25 63 3a 22 2c 20 28 63 68  Path, "%c:", (ch
c2a30 61 72 29 28 20 27 41 27 20 2b 20 75 6c 44 72 69  ar)( 'A' + ulDri
c2a40 76 65 4e 75 6d 20 2d 20 31 20 29 20 29 3b 0a 20  veNum - 1 ) );. 
c2a50 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
c2a60 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20  .    }.  }.  /* 
c2a70 53 74 72 69 70 20 6f 66 66 20 61 20 74 72 61 69  Strip off a trai
c2a80 6c 69 6e 67 20 73 6c 61 73 68 65 73 20 6f 72 20  ling slashes or 
c2a90 62 61 63 6b 73 6c 61 73 68 65 73 2c 20 6f 74 68  backslashes, oth
c2aa0 65 72 77 69 73 65 20 77 65 20 77 6f 75 6c 64 20  erwise we would 
c2ab0 67 65 74 20 2a 0a 20 20 20 2a 20 6d 75 6c 74 69  get *.   * multi
c2ac0 70 6c 65 20 28 62 61 63 6b 29 73 6c 61 73 68 65  ple (back)slashe
c2ad0 73 20 77 68 69 63 68 20 63 61 75 73 65 73 20 44  s which causes D
c2ae0 6f 73 4f 70 65 6e 28 29 20 74 6f 20 66 61 69 6c  osOpen() to fail
c2af0 2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a  .              *
c2b00 0a 20 20 20 2a 20 54 72 61 69 6c 69 6e 67 20 73  .   * Trailing s
c2b10 70 61 63 65 73 20 61 72 65 20 6e 6f 74 20 61 6c  paces are not al
c2b20 6c 6f 77 65 64 2c 20 65 69 74 68 65 72 2e 20 20  lowed, either.  
c2b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2b40 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 6a            */.  j
c2b50 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
c2b60 33 30 28 7a 54 65 6d 70 50 61 74 68 29 3b 0a 20  30(zTempPath);. 
c2b70 20 77 68 69 6c 65 28 20 6a 20 3e 20 30 20 26 26   while( j > 0 &&
c2b80 20 28 20 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31   ( zTempPath[j-1
c2b90 5d 20 3d 3d 20 27 5c 5c 27 20 7c 7c 20 7a 54 65  ] == '\\' || zTe
c2ba0 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27  mpPath[j-1] == '
c2bb0 2f 27 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  /'.             
c2bc0 20 20 20 20 20 20 20 7c 7c 20 7a 54 65 6d 70 50         || zTempP
c2bd0 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27 20 27 20  ath[j-1] == ' ' 
c2be0 29 20 29 7b 0a 20 20 20 20 6a 2d 2d 3b 0a 20 20  ) ){.    j--;.  
c2bf0 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68 5b 6a 5d  }.  zTempPath[j]
c2c00 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66 28 20 21   = '\0';.  if( !
c2c10 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72  sqlite3_temp_dir
c2c20 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20 63 68  ectory ){.    ch
c2c30 61 72 20 2a 7a 54 65 6d 70 50 61 74 68 55 54 46  ar *zTempPathUTF
c2c40 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61 74 68   = convertCpPath
c2c50 54 6f 55 74 66 38 28 20 7a 54 65 6d 70 50 61 74  ToUtf8( zTempPat
c2c60 68 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  h );.    sqlite3
c2c70 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 2d  _snprintf( nBuf-
c2c80 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 20  30, zBuf,.      
c2c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c2ca0 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d  "%s\\"SQLITE_TEM
c2cb0 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a  P_FILE_PREFIX, z
c2cc0 54 65 6d 70 50 61 74 68 55 54 46 20 29 3b 0a 20  TempPathUTF );. 
c2cd0 20 20 20 66 72 65 65 28 20 7a 54 65 6d 70 50 61     free( zTempPa
c2ce0 74 68 55 54 46 20 29 3b 0a 20 20 7d 65 6c 73 65  thUTF );.  }else
c2cf0 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  {.    sqlite3_sn
c2d00 70 72 69 6e 74 66 28 20 6e 42 75 66 2d 33 30 2c  printf( nBuf-30,
c2d10 20 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20   zBuf,.         
c2d20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 25 73               "%s
c2d30 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d 50 5f 46  \\"SQLITE_TEMP_F
c2d40 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a 54 65 6d  ILE_PREFIX, zTem
c2d50 70 50 61 74 68 20 29 3b 0a 20 20 7d 0a 20 20 6a  pPath );.  }.  j
c2d60 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
c2d70 33 30 28 20 7a 42 75 66 20 29 3b 0a 20 20 73 71  30( zBuf );.  sq
c2d80 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73  lite3_randomness
c2d90 28 20 32 30 2c 20 26 7a 42 75 66 5b 6a 5d 20 29  ( 20, &zBuf[j] )
c2da0 3b 0a 20 20 66 6f 72 28 20 69 20 3d 20 30 3b 20  ;.  for( i = 0; 
c2db0 69 20 3c 20 32 30 3b 20 69 2b 2b 2c 20 6a 2b 2b  i < 20; i++, j++
c2dc0 20 29 7b 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20   ){.    zBuf[j] 
c2dd0 3d 20 28 63 68 61 72 29 7a 43 68 61 72 73 5b 20  = (char)zChars[ 
c2de0 28 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29  ((unsigned char)
c2df0 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66  zBuf[j])%(sizeof
c2e00 28 7a 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20  (zChars)-1) ];. 
c2e10 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30   }.  zBuf[j] = 0
c2e20 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 20 22 54  ;.  OSTRACE2( "T
c2e30 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20 25 73  EMP FILENAME: %s
c2e40 5c 6e 22 2c 20 7a 42 75 66 20 29 3b 0a 20 20 72  \n", zBuf );.  r
c2e50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
c2e60 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20  .}.../*.** Turn 
c2e70 61 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e  a relative pathn
c2e80 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20  ame into a full 
c2e90 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69 74 65  pathname.  Write
c2ea0 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 74   the full.** pat
c2eb0 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 46 75 6c 6c  hname into zFull
c2ec0 5b 5d 2e 20 20 7a 46 75 6c 6c 5b 5d 20 77 69 6c  [].  zFull[] wil
c2ed0 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 70 56  l be at least pV
c2ee0 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 2a  fs->mxPathname.*
c2ef0 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e  * bytes in size.
c2f00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
c2f10 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a  s2FullPathname(.
c2f20 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
c2f30 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Vfs,          /*
c2f40 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20   Pointer to vfs 
c2f50 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73  object */.  cons
c2f60 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76  t char *zRelativ
c2f70 65 2c 20 20 20 20 20 20 2f 2a 20 50 6f 73 73 69  e,      /* Possi
c2f80 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69 6e 70  bly relative inp
c2f90 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69 6e 74  ut path */.  int
c2fa0 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20 20 20   nFull,         
c2fb0 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
c2fc0 20 6f 66 20 6f 75 74 70 75 74 20 62 75 66 66 65   of output buffe
c2fd0 72 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20  r in bytes */.  
c2fe0 63 68 61 72 20 2a 7a 46 75 6c 6c 20 20 20 20 20  char *zFull     
c2ff0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
c3000 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a  utput buffer */.
c3010 29 7b 0a 20 20 63 68 61 72 20 2a 7a 52 65 6c 61  ){.  char *zRela
c3020 74 69 76 65 43 70 20 3d 20 63 6f 6e 76 65 72 74  tiveCp = convert
c3030 55 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a 52  Utf8PathToCp( zR
c3040 65 6c 61 74 69 76 65 20 29 3b 0a 20 20 63 68 61  elative );.  cha
c3050 72 20 7a 46 75 6c 6c 43 70 5b 43 43 48 4d 41 58  r zFullCp[CCHMAX
c3060 50 41 54 48 5d 20 3d 20 22 5c 30 22 3b 0a 20 20  PATH] = "\0";.  
c3070 63 68 61 72 20 2a 7a 46 75 6c 6c 55 54 46 3b 0a  char *zFullUTF;.
c3080 20 20 41 50 49 52 45 54 20 72 63 20 3d 20 44 6f    APIRET rc = Do
c3090 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f 28 20  sQueryPathInfo( 
c30a0 7a 52 65 6c 61 74 69 76 65 43 70 2c 20 46 49 4c  zRelativeCp, FIL
c30b0 5f 51 55 45 52 59 46 55 4c 4c 4e 41 4d 45 2c 20  _QUERYFULLNAME, 
c30c0 7a 46 75 6c 6c 43 70 2c 0a 20 20 20 20 20 20 20  zFullCp,.       
c30d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c30e0 20 20 20 20 20 20 20 20 20 43 43 48 4d 41 58 50           CCHMAXP
c30f0 41 54 48 20 29 3b 0a 20 20 66 72 65 65 28 20 7a  ATH );.  free( z
c3100 52 65 6c 61 74 69 76 65 43 70 20 29 3b 0a 20 20  RelativeCp );.  
c3110 7a 46 75 6c 6c 55 54 46 20 3d 20 63 6f 6e 76 65  zFullUTF = conve
c3120 72 74 43 70 50 61 74 68 54 6f 55 74 66 38 28 20  rtCpPathToUtf8( 
c3130 7a 46 75 6c 6c 43 70 20 29 3b 0a 20 20 73 71 6c  zFullCp );.  sql
c3140 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e  ite3_snprintf( n
c3150 46 75 6c 6c 2c 20 7a 46 75 6c 6c 2c 20 7a 46 75  Full, zFull, zFu
c3160 6c 6c 55 54 46 20 29 3b 0a 20 20 66 72 65 65 28  llUTF );.  free(
c3170 20 7a 46 75 6c 6c 55 54 46 20 29 3b 0a 20 20 72   zFullUTF );.  r
c3180 65 74 75 72 6e 20 72 63 20 3d 3d 20 4e 4f 5f 45  eturn rc == NO_E
c3190 52 52 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  RROR ? SQLITE_OK
c31a0 20 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b   : SQLITE_IOERR;
c31b0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20  .}.../*.** Open 
c31c0 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  a file..*/.stati
c31d0 63 20 69 6e 74 20 6f 73 32 4f 70 65 6e 28 0a 20  c int os2Open(. 
c31e0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
c31f0 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  fs,            /
c3200 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20  * Not used */.  
c3210 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
c3220 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e,            /*
c3230 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c   Name of the fil
c3240 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66  e */.  sqlite3_f
c3250 69 6c 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20  ile *id,        
c3260 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68       /* Write th
c3270 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20 68 61  e SQLite file ha
c3280 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20 69  ndle here */.  i
c3290 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nt flags,       
c32a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c32b0 4f 70 65 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20  Open mode flags 
c32c0 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c  */.  int *pOutFl
c32d0 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 20  ags             
c32e0 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72 65 74     /* Status ret
c32f0 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29 7b 0a  urn flags */.){.
c3300 20 20 48 46 49 4c 45 20 68 3b 0a 20 20 55 4c 4f    HFILE h;.  ULO
c3310 4e 47 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75  NG ulFileAttribu
c3320 74 65 20 3d 20 46 49 4c 45 5f 4e 4f 52 4d 41 4c  te = FILE_NORMAL
c3330 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f 70 65 6e  ;.  ULONG ulOpen
c3340 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20 55 4c 4f  Flags = 0;.  ULO
c3350 4e 47 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 3d 20  NG ulOpenMode = 
c3360 30 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46  0;.  os2File *pF
c3370 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29  ile = (os2File*)
c3380 69 64 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20  id;.  APIRET rc 
c3390 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 4c  = NO_ERROR;.  UL
c33a0 4f 4e 47 20 75 6c 41 63 74 69 6f 6e 3b 0a 20 20  ONG ulAction;.  
c33b0 63 68 61 72 20 2a 7a 4e 61 6d 65 43 70 3b 0a 20  char *zNameCp;. 
c33c0 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d 65 5b 43   char zTmpname[C
c33d0 43 48 4d 41 58 50 41 54 48 2b 31 5d 3b 20 20 20  CHMAXPATH+1];   
c33e0 20 2f 2a 20 42 75 66 66 65 72 20 74 6f 20 68 6f   /* Buffer to ho
c33f0 6c 64 20 6e 61 6d 65 20 6f 66 20 74 65 6d 70 20  ld name of temp 
c3400 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66  file */..  /* If
c3410 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
c3420 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e  ment to this fun
c3430 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67  ction is NULL, g
c3440 65 6e 65 72 61 74 65 20 61 20 0a 20 20 2a 2a 20  enerate a .  ** 
c3450 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e  temporary file n
c3460 61 6d 65 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f  ame to use .  */
c3470 0a 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 29 7b  .  if( !zName ){
c3480 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 67 65  .    int rc = ge
c3490 74 54 65 6d 70 6e 61 6d 65 28 43 43 48 4d 41 58  tTempname(CCHMAX
c34a0 50 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65  PATH+1, zTmpname
c34b0 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
c34c0 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
c34d0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
c34e0 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 7a   }.    zName = z
c34f0 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 0a 20  Tmpname;.  }... 
c3500 20 6d 65 6d 73 65 74 28 20 70 46 69 6c 65 2c 20   memset( pFile, 
c3510 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46 69 6c 65  0, sizeof(*pFile
c3520 29 20 29 3b 0a 0a 20 20 4f 53 54 52 41 43 45 32  ) );..  OSTRACE2
c3530 28 20 22 4f 50 45 4e 20 77 61 6e 74 20 25 64 5c  ( "OPEN want %d\
c3540 6e 22 2c 20 66 6c 61 67 73 20 29 3b 0a 0a 20 20  n", flags );..  
c3550 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49  if( flags & SQLI
c3560 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
c3570 45 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d  E ){.    ulOpenM
c3580 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 43 45  ode |= OPEN_ACCE
c3590 53 53 5f 52 45 41 44 57 52 49 54 45 3b 0a 20 20  SS_READWRITE;.  
c35a0 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45    OSTRACE1( "OPE
c35b0 4e 20 72 65 61 64 2f 77 72 69 74 65 5c 6e 22 20  N read/write\n" 
c35c0 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
c35d0 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50  ulOpenMode |= OP
c35e0 45 4e 5f 41 43 43 45 53 53 5f 52 45 41 44 4f 4e  EN_ACCESS_READON
c35f0 4c 59 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31  LY;.    OSTRACE1
c3600 28 20 22 4f 50 45 4e 20 72 65 61 64 20 6f 6e 6c  ( "OPEN read onl
c3610 79 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69  y\n" );.  }..  i
c3620 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f( flags & SQLIT
c3630 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 29 7b  E_OPEN_CREATE ){
c3640 0a 20 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73  .    ulOpenFlags
c3650 20 7c 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f   |= OPEN_ACTION_
c3660 4f 50 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c  OPEN_IF_EXISTS |
c3670 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 43 52 45   OPEN_ACTION_CRE
c3680 41 54 45 5f 49 46 5f 4e 45 57 3b 0a 20 20 20 20  ATE_IF_NEW;.    
c3690 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20  OSTRACE1( "OPEN 
c36a0 6f 70 65 6e 20 6e 65 77 2f 63 72 65 61 74 65 5c  open new/create\
c36b0 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  n" );.  }else{. 
c36c0 20 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20 7c     ulOpenFlags |
c36d0 3d 20 4f 50 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50  = OPEN_ACTION_OP
c36e0 45 4e 5f 49 46 5f 45 58 49 53 54 53 20 7c 20 4f  EN_IF_EXISTS | O
c36f0 50 45 4e 5f 41 43 54 49 4f 4e 5f 46 41 49 4c 5f  PEN_ACTION_FAIL_
c3700 49 46 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 54 52  IF_NEW;.    OSTR
c3710 41 43 45 31 28 20 22 4f 50 45 4e 20 6f 70 65 6e  ACE1( "OPEN open
c3720 20 65 78 69 73 74 69 6e 67 5c 6e 22 20 29 3b 0a   existing\n" );.
c3730 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73    }..  if( flags
c3740 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d   & SQLITE_OPEN_M
c3750 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 75 6c  AIN_DB ){.    ul
c3760 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e  OpenMode |= OPEN
c3770 5f 53 48 41 52 45 5f 44 45 4e 59 4e 4f 4e 45 3b  _SHARE_DENYNONE;
c3780 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20 22  .    OSTRACE1( "
c3790 4f 50 45 4e 20 73 68 61 72 65 20 72 65 61 64 2f  OPEN share read/
c37a0 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 65  write\n" );.  }e
c37b0 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d  lse{.    ulOpenM
c37c0 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 41 52  ode |= OPEN_SHAR
c37d0 45 5f 44 45 4e 59 57 52 49 54 45 3b 0a 20 20 20  E_DENYWRITE;.   
c37e0 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e   OSTRACE1( "OPEN
c37f0 20 73 68 61 72 65 20 72 65 61 64 20 6f 6e 6c 79   share read only
c3800 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  \n" );.  }..  if
c3810 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ( flags & SQLITE
c3820 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c  _OPEN_DELETEONCL
c3830 4f 53 45 20 29 7b 0a 20 20 20 20 63 68 61 72 20  OSE ){.    char 
c3840 70 61 74 68 55 74 66 38 5b 43 43 48 4d 41 58 50  pathUtf8[CCHMAXP
c3850 41 54 48 5d 3b 0a 23 69 66 64 65 66 20 4e 44 45  ATH];.#ifdef NDE
c3860 42 55 47 20 2f 2a 20 77 68 65 6e 20 64 65 62 75  BUG /* when debu
c3870 67 67 69 6e 67 20 77 65 20 77 61 6e 74 20 74 6f  gging we want to
c3880 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73   make sure it is
c3890 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 20 20   deleted */.    
c38a0 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65 20  ulFileAttribute 
c38b0 3d 20 46 49 4c 45 5f 48 49 44 44 45 4e 3b 0a 23  = FILE_HIDDEN;.#
c38c0 65 6e 64 69 66 0a 20 20 20 20 6f 73 32 46 75 6c  endif.    os2Ful
c38d0 6c 50 61 74 68 6e 61 6d 65 28 20 70 56 66 73 2c  lPathname( pVfs,
c38e0 20 7a 4e 61 6d 65 2c 20 43 43 48 4d 41 58 50 41   zName, CCHMAXPA
c38f0 54 48 2c 20 70 61 74 68 55 74 66 38 20 29 3b 0a  TH, pathUtf8 );.
c3900 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54      pFile->pathT
c3910 6f 44 65 6c 20 3d 20 63 6f 6e 76 65 72 74 55 74  oDel = convertUt
c3920 66 38 50 61 74 68 54 6f 43 70 28 20 70 61 74 68  f8PathToCp( path
c3930 55 74 66 38 20 29 3b 0a 20 20 20 20 4f 53 54 52  Utf8 );.    OSTR
c3940 41 43 45 31 28 20 22 4f 50 45 4e 20 68 69 64 64  ACE1( "OPEN hidd
c3950 65 6e 2f 64 65 6c 65 74 65 20 6f 6e 20 63 6c 6f  en/delete on clo
c3960 73 65 20 66 69 6c 65 20 61 74 74 72 69 62 75 74  se file attribut
c3970 65 73 5c 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65  es\n" );.  }else
c3980 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70 61 74  {.    pFile->pat
c3990 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20  hToDel = NULL;. 
c39a0 20 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50     OSTRACE1( "OP
c39b0 45 4e 20 6e 6f 72 6d 61 6c 20 66 69 6c 65 20 61  EN normal file a
c39c0 74 74 72 69 62 75 74 65 5c 6e 22 20 29 3b 0a 20  ttribute\n" );. 
c39d0 20 7d 0a 0a 20 20 2f 2a 20 61 6c 77 61 79 73 20   }..  /* always 
c39e0 6f 70 65 6e 20 69 6e 20 72 61 6e 64 6f 6d 20 61  open in random a
c39f0 63 63 65 73 73 20 6d 6f 64 65 20 66 6f 72 20 70  ccess mode for p
c3a00 6f 73 73 69 62 6c 79 20 62 65 74 74 65 72 20 73  ossibly better s
c3a10 70 65 65 64 20 2a 2f 0a 20 20 75 6c 4f 70 65 6e  peed */.  ulOpen
c3a20 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41  Mode |= OPEN_FLA
c3a30 47 53 5f 52 41 4e 44 4f 4d 3b 0a 20 20 75 6c 4f  GS_RANDOM;.  ulO
c3a40 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f  penMode |= OPEN_
c3a50 46 4c 41 47 53 5f 46 41 49 4c 5f 4f 4e 5f 45 52  FLAGS_FAIL_ON_ER
c3a60 52 4f 52 3b 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64  ROR;.  ulOpenMod
c3a70 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f  e |= OPEN_FLAGS_
c3a80 4e 4f 49 4e 48 45 52 49 54 3b 0a 0a 20 20 7a 4e  NOINHERIT;..  zN
c3a90 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55  ameCp = convertU
c3aa0 74 66 38 50 61 74 68 54 6f 43 70 28 20 7a 4e 61  tf8PathToCp( zNa
c3ab0 6d 65 20 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73  me );.  rc = Dos
c3ac0 4f 70 65 6e 28 20 28 50 53 5a 29 7a 4e 61 6d 65  Open( (PSZ)zName
c3ad0 43 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  Cp,.            
c3ae0 20 20 20 20 26 68 2c 0a 20 20 20 20 20 20 20 20      &h,.        
c3af0 20 20 20 20 20 20 20 20 26 75 6c 41 63 74 69 6f          &ulActio
c3b00 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n,.             
c3b10 20 20 20 30 4c 2c 0a 20 20 20 20 20 20 20 20 20     0L,.         
c3b20 20 20 20 20 20 20 20 75 6c 46 69 6c 65 41 74 74         ulFileAtt
c3b30 72 69 62 75 74 65 2c 0a 20 20 20 20 20 20 20 20  ribute,.        
c3b40 20 20 20 20 20 20 20 20 75 6c 4f 70 65 6e 46 6c          ulOpenFl
c3b50 61 67 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ags,.           
c3b60 20 20 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 2c       ulOpenMode,
c3b70 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
c3b80 20 28 50 45 41 4f 50 32 29 4e 55 4c 4c 20 29 3b   (PEAOP2)NULL );
c3b90 0a 20 20 66 72 65 65 28 20 7a 4e 61 6d 65 43 70  .  free( zNameCp
c3ba0 20 29 3b 0a 20 20 69 66 28 20 72 63 20 21 3d 20   );.  if( rc != 
c3bb0 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
c3bc0 4f 53 54 52 41 43 45 37 28 20 22 4f 50 45 4e 20  OSTRACE7( "OPEN 
c3bd0 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c 65 20 72  Invalid handle r
c3be0 63 3d 25 64 3a 20 7a 4e 61 6d 65 3d 25 73 2c 20  c=%d: zName=%s, 
c3bf0 75 6c 41 63 74 69 6f 6e 3d 25 23 6c 78 2c 20 75  ulAction=%#lx, u
c3c00 6c 41 74 74 72 3d 25 23 6c 78 2c 20 75 6c 46 6c  lAttr=%#lx, ulFl
c3c10 61 67 73 3d 25 23 6c 78 2c 20 75 6c 4d 6f 64 65  ags=%#lx, ulMode
c3c20 3d 25 23 6c 78 5c 6e 22 2c 0a 20 20 20 20 20 20  =%#lx\n",.      
c3c30 20 20 20 20 20 20 20 20 72 63 2c 20 7a 4e 61 6d          rc, zNam
c3c40 65 2c 20 75 6c 41 63 74 69 6f 6e 2c 20 75 6c 46  e, ulAction, ulF
c3c50 69 6c 65 41 74 74 72 69 62 75 74 65 2c 20 75 6c  ileAttribute, ul
c3c60 4f 70 65 6e 46 6c 61 67 73 2c 20 75 6c 4f 70 65  OpenFlags, ulOpe
c3c70 6e 4d 6f 64 65 20 29 3b 0a 20 20 20 20 69 66 28  nMode );.    if(
c3c80 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65   pFile->pathToDe
c3c90 6c 20 29 0a 20 20 20 20 20 20 66 72 65 65 28 20  l ).      free( 
c3ca0 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c  pFile->pathToDel
c3cb0 20 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 70   );.    pFile->p
c3cc0 61 74 68 54 6f 44 65 6c 20 3d 20 4e 55 4c 4c 3b  athToDel = NULL;
c3cd0 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26  .    if( flags &
c3ce0 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
c3cf0 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20  DWRITE ){.      
c3d00 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45 4e 20  OSTRACE2( "OPEN 
c3d10 25 64 20 49 6e 76 61 6c 69 64 20 68 61 6e 64 6c  %d Invalid handl
c3d20 65 5c 6e 22 2c 20 28 28 66 6c 61 67 73 20 7c 20  e\n", ((flags | 
c3d30 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
c3d40 4f 4e 4c 59 29 20 26 20 7e 53 51 4c 49 54 45 5f  ONLY) & ~SQLITE_
c3d50 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 20  OPEN_READWRITE) 
c3d60 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
c3d70 6f 73 32 4f 70 65 6e 28 20 70 56 66 73 2c 20 7a  os2Open( pVfs, z
c3d80 4e 61 6d 65 2c 20 69 64 2c 0a 20 20 20 20 20 20  Name, id,.      
c3d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3da0 28 28 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45  ((flags | SQLITE
c3db0 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 20  _OPEN_READONLY) 
c3dc0 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  & ~SQLITE_OPEN_R
c3dd0 45 41 44 57 52 49 54 45 29 2c 0a 20 20 20 20 20  EADWRITE),.     
c3de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3df0 20 70 4f 75 74 46 6c 61 67 73 20 29 3b 0a 20 20   pOutFlags );.  
c3e00 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
c3e10 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e  eturn SQLITE_CAN
c3e20 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d  TOPEN;.    }.  }
c3e30 0a 0a 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67  ..  if( pOutFlag
c3e40 73 20 29 7b 0a 20 20 20 20 2a 70 4f 75 74 46 6c  s ){.    *pOutFl
c3e50 61 67 73 20 3d 20 66 6c 61 67 73 20 26 20 53 51  ags = flags & SQ
c3e60 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
c3e70 49 54 45 20 3f 20 53 51 4c 49 54 45 5f 4f 50 45  ITE ? SQLITE_OPE
c3e80 4e 5f 52 45 41 44 57 52 49 54 45 20 3a 20 53 51  N_READWRITE : SQ
c3e90 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e  LITE_OPEN_READON
c3ea0 4c 59 3b 0a 20 20 7d 0a 0a 20 20 70 46 69 6c 65  LY;.  }..  pFile
c3eb0 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 6f 73 32  ->pMethod = &os2
c3ec0 49 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69 6c  IoMethod;.  pFil
c3ed0 65 2d 3e 68 20 3d 20 68 3b 0a 20 20 4f 70 65 6e  e->h = h;.  Open
c3ee0 43 6f 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 4f  Counter(+1);.  O
c3ef0 53 54 52 41 43 45 33 28 20 22 4f 50 45 4e 20 25  STRACE3( "OPEN %
c3f00 64 20 70 4f 75 74 46 6c 61 67 73 3d 25 64 5c 6e  d pOutFlags=%d\n
c3f10 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 4f 75  ", pFile->h, pOu
c3f20 74 46 6c 61 67 73 20 29 3b 0a 20 20 72 65 74 75  tFlags );.  retu
c3f30 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
c3f40 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68  ./*.** Delete th
c3f50 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2f  e named file..*/
c3f60 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 44  .static int os2D
c3f70 65 6c 65 74 65 28 0a 20 20 73 71 6c 69 74 65 33  elete(.  sqlite3
c3f80 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20  _vfs *pVfs,     
c3f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3fa0 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f  /* Not used on o
c3fb0 73 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  s2 */.  const ch
c3fc0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20  ar *zFilename,  
c3fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
c3fe0 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74  * Name of file t
c3ff0 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e  o delete */.  in
c4000 74 20 73 79 6e 63 44 69 72 20 20 20 20 20 20 20  t syncDir       
c4010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4020 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64       /* Not used
c4030 20 6f 6e 20 6f 73 32 20 2a 2f 0a 29 7b 0a 20 20   on os2 */.){.  
c4040 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45  APIRET rc = NO_E
c4050 52 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a 7a 46  RROR;.  char *zF
c4060 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76  ilenameCp = conv
c4070 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28  ertUtf8PathToCp(
c4080 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 20   zFilename );.  
c4090 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
c40a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
c40b0 4f 45 52 52 5f 44 45 4c 45 54 45 20 29 3b 0a 20  OERR_DELETE );. 
c40c0 20 72 63 20 3d 20 44 6f 73 44 65 6c 65 74 65 28   rc = DosDelete(
c40d0 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65 43   (PSZ)zFilenameC
c40e0 70 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 46 69  p );.  free( zFi
c40f0 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20 4f 53  lenameCp );.  OS
c4100 54 52 41 43 45 32 28 20 22 44 45 4c 45 54 45 20  TRACE2( "DELETE 
c4110 5c 22 25 73 5c 22 5c 6e 22 2c 20 7a 46 69 6c 65  \"%s\"\n", zFile
c4120 6e 61 6d 65 20 29 3b 0a 20 20 72 65 74 75 72 6e  name );.  return
c4130 20 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20   rc == NO_ERROR 
c4140 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51  ? SQLITE_OK : SQ
c4150 4c 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54  LITE_IOERR_DELET
c4160 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  E;.}../*.** Chec
c4170 6b 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 20  k the existance 
c4180 61 6e 64 20 73 74 61 74 75 73 20 6f 66 20 61 20  and status of a 
c4190 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
c41a0 69 6e 74 20 6f 73 32 41 63 63 65 73 73 28 0a 20  int os2Access(. 
c41b0 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
c41c0 66 73 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f  fs,        /* No
c41d0 74 20 75 73 65 64 20 6f 6e 20 6f 73 32 20 2a 2f  t used on os2 */
c41e0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
c41f0 46 69 6c 65 6e 61 6d 65 2c 20 20 20 20 2f 2a 20  Filename,    /* 
c4200 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20  Name of file to 
c4210 63 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 66  check */.  int f
c4220 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20  lags,           
c4230 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20       /* Type of 
c4240 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e 20  test to make on 
c4250 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69  this file */.  i
c4260 6e 74 20 2a 70 4f 75 74 20 20 20 20 20 20 20 20  nt *pOut        
c4270 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
c4280 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a  e results here *
c4290 2f 0a 29 7b 0a 20 20 46 49 4c 45 53 54 41 54 55  /.){.  FILESTATU
c42a0 53 33 20 66 73 74 73 33 43 6f 6e 66 69 67 49 6e  S3 fsts3ConfigIn
c42b0 66 6f 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20  fo;.  APIRET rc 
c42c0 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 68  = NO_ERROR;.  ch
c42d0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43 70 20  ar *zFilenameCp 
c42e0 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74  = convertUtf8Pat
c42f0 68 54 6f 43 70 28 20 7a 46 69 6c 65 6e 61 6d 65  hToCp( zFilename
c4300 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 20 26   );..  memset( &
c4310 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2c  fsts3ConfigInfo,
c4320 20 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 73 33   0, sizeof(fsts3
c4330 43 6f 6e 66 69 67 49 6e 66 6f 29 20 29 3b 0a 20  ConfigInfo) );. 
c4340 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79 50 61   rc = DosQueryPa
c4350 74 68 49 6e 66 6f 28 20 28 50 53 5a 29 7a 46 69  thInfo( (PSZ)zFi
c4360 6c 65 6e 61 6d 65 43 70 2c 20 46 49 4c 5f 53 54  lenameCp, FIL_ST
c4370 41 4e 44 41 52 44 2c 0a 20 20 20 20 20 20 20 20  ANDARD,.        
c4380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4390 20 26 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66   &fsts3ConfigInf
c43a0 6f 2c 20 73 69 7a 65 6f 66 28 46 49 4c 45 53 54  o, sizeof(FILEST
c43b0 41 54 55 53 33 29 20 29 3b 0a 20 20 66 72 65 65  ATUS3) );.  free
c43c0 28 20 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b  ( zFilenameCp );
c43d0 0a 20 20 4f 53 54 52 41 43 45 34 28 20 22 41 43  .  OSTRACE4( "AC
c43e0 43 45 53 53 20 66 73 74 73 33 43 6f 6e 66 69 67  CESS fsts3Config
c43f0 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 3d 25 64  Info.attrFile=%d
c4400 20 66 6c 61 67 73 3d 25 64 20 72 63 3d 25 64 5c   flags=%d rc=%d\
c4410 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
c4420 66 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e  fsts3ConfigInfo.
c4430 61 74 74 72 46 69 6c 65 2c 20 66 6c 61 67 73 2c  attrFile, flags,
c4440 20 72 63 20 29 3b 0a 20 20 73 77 69 74 63 68 28   rc );.  switch(
c4450 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 63 61   flags ){.    ca
c4460 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  se SQLITE_ACCESS
c4470 5f 52 45 41 44 3a 0a 20 20 20 20 63 61 73 65 20  _READ:.    case 
c4480 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58  SQLITE_ACCESS_EX
c4490 49 53 54 53 3a 0a 20 20 20 20 20 20 72 63 20 3d  ISTS:.      rc =
c44a0 20 28 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52   (rc == NO_ERROR
c44b0 29 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45  );.      OSTRACE
c44c0 33 28 20 22 41 43 43 45 53 53 20 25 73 20 61 63  3( "ACCESS %s ac
c44d0 63 65 73 73 20 6f 66 20 72 65 61 64 20 61 6e 64  cess of read and
c44e0 20 65 78 69 73 74 73 20 20 72 63 3d 25 64 5c 6e   exists  rc=%d\n
c44f0 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 63  ", zFilename, rc
c4500 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
c4510 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
c4520 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54  _ACCESS_READWRIT
c4530 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20 28 72  E:.      rc = (r
c4540 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 29 20 26  c == NO_ERROR) &
c4550 26 20 28 20 28 66 73 74 73 33 43 6f 6e 66 69 67  & ( (fsts3Config
c4560 49 6e 66 6f 2e 61 74 74 72 46 69 6c 65 20 26 20  Info.attrFile & 
c4570 46 49 4c 45 5f 52 45 41 44 4f 4e 4c 59 29 20 3d  FILE_READONLY) =
c4580 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 4f 53 54  = 0 );.      OST
c4590 52 41 43 45 33 28 20 22 41 43 43 45 53 53 20 25  RACE3( "ACCESS %
c45a0 73 20 61 63 63 65 73 73 20 6f 66 20 72 65 61 64  s access of read
c45b0 2f 77 72 69 74 65 20 20 72 63 3d 25 64 5c 6e 22  /write  rc=%d\n"
c45c0 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 72 63 20  , zFilename, rc 
c45d0 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
c45e0 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20      default:.   
c45f0 20 20 20 61 73 73 65 72 74 28 20 21 22 49 6e 76     assert( !"Inv
c4600 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75 6d  alid flags argum
c4610 65 6e 74 22 20 29 3b 0a 20 20 7d 0a 20 20 2a 70  ent" );.  }.  *p
c4620 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72 65 74 75  Out = rc;.  retu
c4630 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
c4640 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
c4650 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e  _OMIT_LOAD_EXTEN
c4660 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72  SION./*.** Inter
c4670 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e  faces for openin
c4680 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 61  g a shared libra
c4690 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72  ry, finding entr
c46a0 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68  y points.** with
c46b0 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c 69  in the shared li
c46c0 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69  brary, and closi
c46d0 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c 69  ng the shared li
c46e0 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20  brary..*/./*.** 
c46f0 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f  Interfaces for o
c4700 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20  pening a shared 
c4710 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67  library, finding
c4720 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a   entry points.**
c4730 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 72   within the shar
c4740 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20  ed library, and 
c4750 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72  closing the shar
c4760 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 73  ed library..*/.s
c4770 74 61 74 69 63 20 76 6f 69 64 20 2a 6f 73 32 44  tatic void *os2D
c4780 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66  lOpen(sqlite3_vf
c4790 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63  s *pVfs, const c
c47a0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b  har *zFilename){
c47b0 0a 20 20 55 43 48 41 52 20 6c 6f 61 64 45 72 72  .  UCHAR loadErr
c47c0 5b 32 35 36 5d 3b 0a 20 20 48 4d 4f 44 55 4c 45  [256];.  HMODULE
c47d0 20 68 6d 6f 64 3b 0a 20 20 41 50 49 52 45 54 20   hmod;.  APIRET 
c47e0 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c  rc;.  char *zFil
c47f0 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76 65 72  enameCp = conver
c4800 74 55 74 66 38 50 61 74 68 54 6f 43 70 28 7a 46  tUtf8PathToCp(zF
c4810 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 72 63 20 3d  ilename);.  rc =
c4820 20 44 6f 73 4c 6f 61 64 4d 6f 64 75 6c 65 28 28   DosLoadModule((
c4830 50 53 5a 29 6c 6f 61 64 45 72 72 2c 20 73 69 7a  PSZ)loadErr, siz
c4840 65 6f 66 28 6c 6f 61 64 45 72 72 29 2c 20 7a 46  eof(loadErr), zF
c4850 69 6c 65 6e 61 6d 65 43 70 2c 20 26 68 6d 6f 64  ilenameCp, &hmod
c4860 29 3b 0a 20 20 66 72 65 65 28 7a 46 69 6c 65 6e  );.  free(zFilen
c4870 61 6d 65 43 70 29 3b 0a 20 20 72 65 74 75 72 6e  ameCp);.  return
c4880 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20   rc != NO_ERROR 
c4890 3f 20 30 20 3a 20 28 76 6f 69 64 2a 29 68 6d 6f  ? 0 : (void*)hmo
c48a0 64 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 41 20 6e 6f 2d  d;.}./*.** A no-
c48b0 6f 70 20 73 69 6e 63 65 20 74 68 65 20 65 72 72  op since the err
c48c0 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
c48d0 6e 65 64 20 6f 6e 20 74 68 65 20 44 6f 73 4c 6f  ned on the DosLo
c48e0 61 64 4d 6f 64 75 6c 65 20 63 61 6c 6c 2e 0a 2a  adModule call..*
c48f0 2a 20 6f 73 32 44 6c 6f 70 65 6e 20 72 65 74 75  * os2Dlopen retu
c4900 72 6e 73 20 7a 65 72 6f 20 69 66 20 44 6f 73 4c  rns zero if DosL
c4910 6f 61 64 4d 6f 64 75 6c 65 20 69 73 20 6e 6f 74  oadModule is not
c4920 20 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2f 0a   successful..*/.
c4930 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73 32 44  static void os2D
c4940 6c 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76  lError(sqlite3_v
c4950 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42  fs *pVfs, int nB
c4960 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75  uf, char *zBufOu
c4970 74 29 7b 0a 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a  t){./* no-op */.
c4980 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6f  }.static void *o
c4990 73 32 44 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f  s2DlSym(sqlite3_
c49a0 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64 20  vfs *pVfs, void 
c49b0 2a 70 48 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20  *pHandle, const 
c49c0 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b 0a  char *zSymbol){.
c49d0 20 20 50 46 4e 20 70 66 6e 3b 0a 20 20 41 50 49    PFN pfn;.  API
c49e0 52 45 54 20 72 63 3b 0a 20 20 72 63 20 3d 20 44  RET rc;.  rc = D
c49f0 6f 73 51 75 65 72 79 50 72 6f 63 41 64 64 72 28  osQueryProcAddr(
c4a00 28 48 4d 4f 44 55 4c 45 29 70 48 61 6e 64 6c 65  (HMODULE)pHandle
c4a10 2c 20 30 4c 2c 20 7a 53 79 6d 62 6f 6c 2c 20 26  , 0L, zSymbol, &
c4a20 70 66 6e 29 3b 0a 20 20 69 66 28 20 72 63 20 21  pfn);.  if( rc !
c4a30 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
c4a40 20 20 2f 2a 20 69 66 20 74 68 65 20 73 79 6d 62    /* if the symb
c4a50 6f 6c 20 69 74 73 65 6c 66 20 77 61 73 20 6e 6f  ol itself was no
c4a60 74 20 66 6f 75 6e 64 2c 20 73 65 61 72 63 68 20  t found, search 
c4a70 61 67 61 69 6e 20 66 6f 72 20 74 68 65 20 73 61  again for the sa
c4a80 6d 65 0a 20 20 20 20 20 2a 20 73 79 6d 62 6f 6c  me.     * symbol
c4a90 20 77 69 74 68 20 61 6e 20 65 78 74 72 61 20 75   with an extra u
c4aa0 6e 64 65 72 73 63 6f 72 65 2c 20 74 68 61 74 20  nderscore, that 
c4ab0 6d 69 67 68 74 20 62 65 20 6e 65 65 64 65 64 20  might be needed 
c4ac0 64 65 70 65 6e 64 69 6e 67 0a 20 20 20 20 20 2a  depending.     *
c4ad0 20 6f 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67 20   on the calling 
c4ae0 63 6f 6e 76 65 6e 74 69 6f 6e 20 2a 2f 0a 20 20  convention */.  
c4af0 20 20 63 68 61 72 20 5f 7a 53 79 6d 62 6f 6c 5b    char _zSymbol[
c4b00 32 35 36 5d 20 3d 20 22 5f 22 3b 0a 20 20 20 20  256] = "_";.    
c4b10 73 74 72 6e 63 61 74 28 5f 7a 53 79 6d 62 6f 6c  strncat(_zSymbol
c4b20 2c 20 7a 53 79 6d 62 6f 6c 2c 20 32 35 35 29 3b  , zSymbol, 255);
c4b30 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 51 75 65  .    rc = DosQue
c4b40 72 79 50 72 6f 63 41 64 64 72 28 28 48 4d 4f 44  ryProcAddr((HMOD
c4b50 55 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30 4c 2c  ULE)pHandle, 0L,
c4b60 20 5f 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 6e 29   _zSymbol, &pfn)
c4b70 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
c4b80 63 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20  c != NO_ERROR ? 
c4b90 30 20 3a 20 28 76 6f 69 64 2a 29 70 66 6e 3b 0a  0 : (void*)pfn;.
c4ba0 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 73  }.static void os
c4bb0 32 44 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  2DlClose(sqlite3
c4bc0 5f 76 66 73 20 2a 70 56 66 73 2c 20 76 6f 69 64  _vfs *pVfs, void
c4bd0 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 44 6f   *pHandle){.  Do
c4be0 73 46 72 65 65 4d 6f 64 75 6c 65 28 28 48 4d 4f  sFreeModule((HMO
c4bf0 44 55 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a 7d  DULE)pHandle);.}
c4c00 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c  .#else /* if SQL
c4c10 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58  ITE_OMIT_LOAD_EX
c4c20 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e  TENSION is defin
c4c30 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65  ed: */.  #define
c4c40 20 6f 73 32 44 6c 4f 70 65 6e 20 30 0a 20 20 23   os2DlOpen 0.  #
c4c50 64 65 66 69 6e 65 20 6f 73 32 44 6c 45 72 72 6f  define os2DlErro
c4c60 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20 6f 73  r 0.  #define os
c4c70 32 44 6c 53 79 6d 20 30 0a 20 20 23 64 65 66 69  2DlSym 0.  #defi
c4c80 6e 65 20 6f 73 32 44 6c 43 6c 6f 73 65 20 30 0a  ne os2DlClose 0.
c4c90 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 57  #endif.../*.** W
c4ca0 72 69 74 65 20 75 70 20 74 6f 20 6e 42 75 66 20  rite up to nBuf 
c4cb0 62 79 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 6e  bytes of randomn
c4cc0 65 73 73 20 69 6e 74 6f 20 7a 42 75 66 2e 0a 2a  ess into zBuf..*
c4cd0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  /.static int os2
c4ce0 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c 69 74  Randomness(sqlit
c4cf0 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e  e3_vfs *pVfs, in
c4d00 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42  t nBuf, char *zB
c4d10 75 66 20 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20  uf ){.  int n = 
c4d20 30 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  0;.#if defined(S
c4d30 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 6e 20  QLITE_TEST).  n 
c4d40 3d 20 6e 42 75 66 3b 0a 20 20 6d 65 6d 73 65 74  = nBuf;.  memset
c4d50 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b  (zBuf, 0, nBuf);
c4d60 0a 23 65 6c 73 65 0a 20 20 69 6e 74 20 73 69 7a  .#else.  int siz
c4d70 65 6f 66 55 4c 6f 6e 67 20 3d 20 73 69 7a 65 6f  eofULong = sizeo
c4d80 66 28 55 4c 4f 4e 47 29 3b 0a 20 20 69 66 28 20  f(ULONG);.  if( 
c4d90 28 69 6e 74 29 73 69 7a 65 6f 66 28 44 41 54 45  (int)sizeof(DATE
c4da0 54 49 4d 45 29 20 3c 3d 20 6e 42 75 66 20 2d 20  TIME) <= nBuf - 
c4db0 6e 20 29 7b 0a 20 20 20 20 44 41 54 45 54 49 4d  n ){.    DATETIM
c4dc0 45 20 78 3b 0a 20 20 20 20 44 6f 73 47 65 74 44  E x;.    DosGetD
c4dd0 61 74 65 54 69 6d 65 28 26 78 29 3b 0a 20 20 20  ateTime(&x);.   
c4de0 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d   memcpy(&zBuf[n]
c4df0 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78 29 29  , &x, sizeof(x))
c4e00 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f  ;.    n += sizeo
c4e10 66 28 78 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  f(x);.  }..  if(
c4e20 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20   sizeofULong <= 
c4e30 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20  nBuf - n ){.    
c4e40 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20 44  PPIB ppib;.    D
c4e50 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28  osGetInfoBlocks(
c4e60 4e 55 4c 4c 2c 20 26 70 70 69 62 29 3b 0a 20 20  NULL, &ppib);.  
c4e70 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e    memcpy(&zBuf[n
c4e80 5d 2c 20 26 70 70 69 62 2d 3e 70 69 62 5f 75 6c  ], &ppib->pib_ul
c4e90 70 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67  pid, sizeofULong
c4ea0 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65  );.    n += size
c4eb0 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20  ofULong;.  }..  
c4ec0 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20  if( sizeofULong 
c4ed0 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20  <= nBuf - n ){. 
c4ee0 20 20 20 50 54 49 42 20 70 74 69 62 3b 0a 20 20     PTIB ptib;.  
c4ef0 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63    DosGetInfoBloc
c4f00 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c 29 3b  ks(&ptib, NULL);
c4f10 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75  .    memcpy(&zBu
c4f20 66 5b 6e 5d 2c 20 26 70 74 69 62 2d 3e 74 69 62  f[n], &ptib->tib
c4f30 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f 75 6c 74  _ptib2->tib2_ult
c4f40 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29  id, sizeofULong)
c4f50 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f  ;.    n += sizeo
c4f60 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a 20 20 2f  fULong;.  }..  /
c4f70 2a 20 69 66 20 77 65 20 73 74 69 6c 6c 20 68 61  * if we still ha
c4f80 76 65 6e 27 74 20 66 69 6c 6c 65 64 20 74 68 65  ven't filled the
c4f90 20 62 75 66 66 65 72 20 79 65 74 20 74 68 65 20   buffer yet the 
c4fa0 66 6f 6c 6c 6f 77 69 6e 67 20 77 69 6c 6c 20 2a  following will *
c4fb0 2f 0a 20 20 2f 2a 20 67 72 61 62 20 65 76 65 72  /.  /* grab ever
c4fc0 79 74 68 69 6e 67 20 6f 6e 63 65 20 69 6e 73 74  ything once inst
c4fd0 65 61 64 20 6f 66 20 6d 61 6b 69 6e 67 20 73 65  ead of making se
c4fe0 76 65 72 61 6c 20 63 61 6c 6c 73 20 66 6f 72 20  veral calls for 
c4ff0 61 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 2a 2f  a single item */
c5000 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f  .  if( sizeofULo
c5010 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29  ng <= nBuf - n )
c5020 7b 0a 20 20 20 20 55 4c 4f 4e 47 20 75 6c 53 79  {.    ULONG ulSy
c5030 73 49 6e 66 6f 5b 51 53 56 5f 4d 41 58 5d 3b 0a  sInfo[QSV_MAX];.
c5040 20 20 20 20 44 6f 73 51 75 65 72 79 53 79 73 49      DosQuerySysI
c5050 6e 66 6f 28 31 4c 2c 20 51 53 56 5f 4d 41 58 2c  nfo(1L, QSV_MAX,
c5060 20 75 6c 53 79 73 49 6e 66 6f 2c 20 73 69 7a 65   ulSysInfo, size
c5070 6f 66 55 4c 6f 6e 67 20 2a 20 51 53 56 5f 4d 41  ofULong * QSV_MA
c5080 58 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28  X);..    memcpy(
c5090 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73  &zBuf[n], &ulSys
c50a0 49 6e 66 6f 5b 51 53 56 5f 4d 53 5f 43 4f 55 4e  Info[QSV_MS_COUN
c50b0 54 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c  T - 1], sizeofUL
c50c0 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73  ong);.    n += s
c50d0 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 0a 20 20 20  izeofULong;..   
c50e0 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67   if( sizeofULong
c50f0 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a   <= nBuf - n ){.
c5100 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42        memcpy(&zB
c5110 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66  uf[n], &ulSysInf
c5120 6f 5b 51 53 56 5f 54 49 4d 45 52 5f 49 4e 54 45  o[QSV_TIMER_INTE
c5130 52 56 41 4c 20 2d 20 31 5d 2c 20 73 69 7a 65 6f  RVAL - 1], sizeo
c5140 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e  fULong);.      n
c5150 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b   += sizeofULong;
c5160 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
c5170 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42  izeofULong <= nB
c5180 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20 20 20 20  uf - n ){.      
c5190 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c  memcpy(&zBuf[n],
c51a0 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f   &ulSysInfo[QSV_
c51b0 54 49 4d 45 5f 4c 4f 57 20 2d 20 31 5d 2c 20 73  TIME_LOW - 1], s
c51c0 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20  izeofULong);.   
c51d0 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c     n += sizeofUL
c51e0 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ong;.    }.    i
c51f0 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c  f( sizeofULong <
c5200 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20  = nBuf - n ){.  
c5210 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66      memcpy(&zBuf
c5220 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b  [n], &ulSysInfo[
c5230 51 53 56 5f 54 49 4d 45 5f 48 49 47 48 20 2d 20  QSV_TIME_HIGH - 
c5240 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29  1], sizeofULong)
c5250 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a  ;.      n += siz
c5260 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a  eofULong;.    }.
c5270 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c      if( sizeofUL
c5280 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20  ong <= nBuf - n 
c5290 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
c52a0 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73  &zBuf[n], &ulSys
c52b0 49 6e 66 6f 5b 51 53 56 5f 54 4f 54 41 56 41 49  Info[QSV_TOTAVAI
c52c0 4c 4d 45 4d 20 2d 20 31 5d 2c 20 73 69 7a 65 6f  LMEM - 1], sizeo
c52d0 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 20 20 6e  fULong);.      n
c52e0 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b   += sizeofULong;
c52f0 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
c5300 66 0a 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d  f..  return n;.}
c5310 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f  ../*.** Sleep fo
c5320 72 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65  r a little while
c5330 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d  .  Return the am
c5340 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65  ount of time sle
c5350 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d  pt..** The argum
c5360 65 6e 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ent is the numbe
c5370 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64  r of microsecond
c5380 73 20 77 65 20 77 61 6e 74 20 74 6f 20 73 6c 65  s we want to sle
c5390 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  ep..** The retur
c53a0 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6e  n value is the n
c53b0 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65  umber of microse
c53c0 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70 20 61  conds of sleep a
c53d0 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65  ctually.** reque
c53e0 73 74 65 64 20 66 72 6f 6d 20 74 68 65 20 75 6e  sted from the un
c53f0 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69  derlying operati
c5400 6e 67 20 73 79 73 74 65 6d 2c 20 61 20 6e 75 6d  ng system, a num
c5410 62 65 72 20 77 68 69 63 68 0a 2a 2a 20 6d 69 67  ber which.** mig
c5420 68 74 20 62 65 20 67 72 65 61 74 65 72 20 74 68  ht be greater th
c5430 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74  an or equal to t
c5440 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 62 75 74  he argument, but
c5450 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20 74 68 61   not less.** tha
c5460 6e 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a  n the argument..
c5470 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  */.static int os
c5480 32 53 6c 65 65 70 28 20 73 71 6c 69 74 65 33 5f  2Sleep( sqlite3_
c5490 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6d  vfs *pVfs, int m
c54a0 69 63 72 6f 73 65 63 20 29 7b 0a 20 20 44 6f 73  icrosec ){.  Dos
c54b0 53 6c 65 65 70 28 20 28 6d 69 63 72 6f 73 65 63  Sleep( (microsec
c54c0 2f 31 30 30 30 29 20 29 3b 0a 20 20 72 65 74 75  /1000) );.  retu
c54d0 72 6e 20 6d 69 63 72 6f 73 65 63 3b 0a 7d 0a 0a  rn microsec;.}..
c54e0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
c54f0 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66  ing variable, if
c5500 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65   set to a non-ze
c5510 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65  ro value, become
c5520 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20  s the result.** 
c5530 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 73 71  returned from sq
c5540 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69  lite3OsCurrentTi
c5550 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73 20 75  me().  This is u
c5560 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e  sed for testing.
c5570 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
c5580 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
c5590 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75  I int sqlite3_cu
c55a0 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 0a  rrent_time = 0;.
c55b0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69  #endif../*.** Fi
c55c0 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  nd the current t
c55d0 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61  ime (in Universa
c55e0 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69  l Coordinated Ti
c55f0 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a  me).  Write the.
c5600 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20  ** current time 
c5610 61 6e 64 20 64 61 74 65 20 61 73 20 61 20 4a 75  and date as a Ju
c5620 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20  lian Day number 
c5630 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a  into *prNow and.
c5640 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65  ** return 0.  Re
c5650 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 74 69  turn 1 if the ti
c5660 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e  me and date cann
c5670 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a  ot be found..*/.
c5680 69 6e 74 20 6f 73 32 43 75 72 72 65 6e 74 54 69  int os2CurrentTi
c5690 6d 65 28 20 73 71 6c 69 74 65 33 5f 76 66 73 20  me( sqlite3_vfs 
c56a0 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a 70  *pVfs, double *p
c56b0 72 4e 6f 77 20 29 7b 0a 20 20 64 6f 75 62 6c 65  rNow ){.  double
c56c0 20 6e 6f 77 3b 0a 20 20 53 48 4f 52 54 20 6d 69   now;.  SHORT mi
c56d0 6e 75 74 65 3b 20 2f 2a 20 6e 65 65 64 73 20 74  nute; /* needs t
c56e0 6f 20 62 65 20 61 62 6c 65 20 74 6f 20 63 6f 70  o be able to cop
c56f0 65 20 77 69 74 68 20 6e 65 67 61 74 69 76 65 20  e with negative 
c5700 74 69 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20  timezone offset 
c5710 2a 2f 0a 20 20 55 53 48 4f 52 54 20 73 65 63 6f  */.  USHORT seco
c5720 6e 64 2c 20 68 6f 75 72 2c 0a 20 20 20 20 20 20  nd, hour,.      
c5730 20 20 20 64 61 79 2c 20 6d 6f 6e 74 68 2c 20 79     day, month, y
c5740 65 61 72 3b 0a 20 20 44 41 54 45 54 49 4d 45 20  ear;.  DATETIME 
c5750 64 74 3b 0a 20 20 44 6f 73 47 65 74 44 61 74 65  dt;.  DosGetDate
c5760 54 69 6d 65 28 20 26 64 74 20 29 3b 0a 20 20 73  Time( &dt );.  s
c5770 65 63 6f 6e 64 20 3d 20 28 55 53 48 4f 52 54 29  econd = (USHORT)
c5780 64 74 2e 73 65 63 6f 6e 64 73 3b 0a 20 20 6d 69  dt.seconds;.  mi
c5790 6e 75 74 65 20 3d 20 28 53 48 4f 52 54 29 64 74  nute = (SHORT)dt
c57a0 2e 6d 69 6e 75 74 65 73 20 2b 20 64 74 2e 74 69  .minutes + dt.ti
c57b0 6d 65 7a 6f 6e 65 3b 0a 20 20 68 6f 75 72 20 3d  mezone;.  hour =
c57c0 20 28 55 53 48 4f 52 54 29 64 74 2e 68 6f 75 72   (USHORT)dt.hour
c57d0 73 3b 0a 20 20 64 61 79 20 3d 20 28 55 53 48 4f  s;.  day = (USHO
c57e0 52 54 29 64 74 2e 64 61 79 3b 0a 20 20 6d 6f 6e  RT)dt.day;.  mon
c57f0 74 68 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e  th = (USHORT)dt.
c5800 6d 6f 6e 74 68 3b 0a 20 20 79 65 61 72 20 3d 20  month;.  year = 
c5810 28 55 53 48 4f 52 54 29 64 74 2e 79 65 61 72 3b  (USHORT)dt.year;
c5820 0a 0a 20 20 2f 2a 20 43 61 6c 63 75 6c 61 74 69  ..  /* Calculati
c5830 6f 6e 73 20 66 72 6f 6d 20 68 74 74 70 3a 2f 2f  ons from http://
c5840 77 77 77 2e 61 73 74 72 6f 2e 6b 65 65 6c 65 2e  www.astro.keele.
c5850 61 63 2e 75 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f  ac.uk/~rno/Astro
c5860 6e 6f 6d 79 2f 68 6a 64 2e 68 74 6d 6c 0a 20 20  nomy/hjd.html.  
c5870 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 61 73     http://www.as
c5880 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75 6b 2f  tro.keele.ac.uk/
c5890 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79 2f 68  ~rno/Astronomy/h
c58a0 6a 64 2d 30 2e 31 2e 63 20 2a 2f 0a 20 20 2f 2a  jd-0.1.c */.  /*
c58b0 20 43 61 6c 63 75 6c 61 74 65 20 74 68 65 20 4a   Calculate the J
c58c0 75 6c 69 61 6e 20 64 61 79 73 20 2a 2f 0a 20 20  ulian days */.  
c58d0 6e 6f 77 20 3d 20 64 61 79 20 2d 20 33 32 30 37  now = day - 3207
c58e0 36 20 2b 0a 20 20 20 20 31 34 36 31 2a 28 79 65  6 +.    1461*(ye
c58f0 61 72 20 2b 20 34 38 30 30 20 2b 20 28 6d 6f 6e  ar + 4800 + (mon
c5900 74 68 20 2d 20 31 34 29 2f 31 32 29 2f 34 20 2b  th - 14)/12)/4 +
c5910 0a 20 20 20 20 33 36 37 2a 28 6d 6f 6e 74 68 20  .    367*(month 
c5920 2d 20 32 20 2d 20 28 6d 6f 6e 74 68 20 2d 20 31  - 2 - (month - 1
c5930 34 29 2f 31 32 2a 31 32 29 2f 31 32 20 2d 0a 20  4)/12*12)/12 -. 
c5940 20 20 20 33 2a 28 28 79 65 61 72 20 2b 20 34 39     3*((year + 49
c5950 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d 20 31 34  00 + (month - 14
c5960 29 2f 31 32 29 2f 31 30 30 29 2f 34 3b 0a 0a 20  )/12)/100)/4;.. 
c5970 20 2f 2a 20 41 64 64 20 74 68 65 20 66 72 61 63   /* Add the frac
c5980 74 69 6f 6e 61 6c 20 68 6f 75 72 73 2c 20 6d 69  tional hours, mi
c5990 6e 73 20 61 6e 64 20 73 65 63 6f 6e 64 73 20 2a  ns and seconds *
c59a0 2f 0a 20 20 6e 6f 77 20 2b 3d 20 28 68 6f 75 72  /.  now += (hour
c59b0 20 2b 20 31 32 2e 30 29 2f 32 34 2e 30 3b 0a 20   + 12.0)/24.0;. 
c59c0 20 6e 6f 77 20 2b 3d 20 6d 69 6e 75 74 65 2f 31   now += minute/1
c59d0 34 34 30 2e 30 3b 0a 20 20 6e 6f 77 20 2b 3d 20  440.0;.  now += 
c59e0 73 65 63 6f 6e 64 2f 38 36 34 30 30 2e 30 3b 0a  second/86400.0;.
c59f0 20 20 2a 70 72 4e 6f 77 20 3d 20 6e 6f 77 3b 0a    *prNow = now;.
c5a00 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
c5a10 53 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ST.  if( sqlite3
c5a20 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b  _current_time ){
c5a30 0a 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71  .    *prNow = sq
c5a40 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69  lite3_current_ti
c5a50 6d 65 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34  me/86400.0 + 244
c5a60 30 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64  0587.5;.  }.#end
c5a70 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  if.  return 0;.}
c5a80 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  ..static int os2
c5a90 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73 71 6c  GetLastError(sql
c5aa0 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20  ite3_vfs *pVfs, 
c5ab0 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a  int nBuf, char *
c5ac0 7a 42 75 66 29 7b 0a 20 20 72 65 74 75 72 6e 20  zBuf){.  return 
c5ad0 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74  0;.}../*.** Init
c5ae0 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69  ialize and deini
c5af0 74 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 72  tialize the oper
c5b00 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74  ating system int
c5b10 65 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54  erface..*/.SQLIT
c5b20 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
c5b30 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b  3_os_init(void){
c5b40 0a 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65  .  static sqlite
c5b50 33 5f 76 66 73 20 6f 73 32 56 66 73 20 3d 20 7b  3_vfs os2Vfs = {
c5b60 0a 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 20  .    1,         
c5b70 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73          /* iVers
c5b80 69 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f  ion */.    sizeo
c5b90 66 28 6f 73 32 46 69 6c 65 29 2c 20 20 20 2f 2a  f(os2File),   /*
c5ba0 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20   szOsFile */.   
c5bb0 20 43 43 48 4d 41 58 50 41 54 48 2c 20 20 20 20   CCHMAXPATH,    
c5bc0 20 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d      /* mxPathnam
c5bd0 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20  e */.    0,     
c5be0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70              /* p
c5bf0 4e 65 78 74 20 2a 2f 0a 20 20 20 20 22 6f 73 32  Next */.    "os2
c5c00 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ",             /
c5c10 2a 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30  * zName */.    0
c5c20 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
c5c30 20 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f    /* pAppData */
c5c40 0a 0a 20 20 20 20 6f 73 32 4f 70 65 6e 2c 20 20  ..    os2Open,  
c5c50 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65           /* xOpe
c5c60 6e 20 2a 2f 0a 20 20 20 20 6f 73 32 44 65 6c 65  n */.    os2Dele
c5c70 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78  te,         /* x
c5c80 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 6f 73  Delete */.    os
c5c90 32 41 63 63 65 73 73 2c 20 20 20 20 20 20 20 20  2Access,        
c5ca0 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a 20   /* xAccess */. 
c5cb0 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e 61     os2FullPathna
c5cc0 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61  me,   /* xFullPa
c5cd0 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 6f 73  thname */.    os
c5ce0 32 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20  2DlOpen,        
c5cf0 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20   /* xDlOpen */. 
c5d00 20 20 20 6f 73 32 44 6c 45 72 72 6f 72 2c 20 20     os2DlError,  
c5d10 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f        /* xDlErro
c5d20 72 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 53 79  r */.    os2DlSy
c5d30 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78  m,          /* x
c5d40 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 6f 73 32  DlSym */.    os2
c5d50 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20  DlClose,        
c5d60 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20  /* xDlClose */. 
c5d70 20 20 20 6f 73 32 52 61 6e 64 6f 6d 6e 65 73 73     os2Randomness
c5d80 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d  ,     /* xRandom
c5d90 6e 65 73 73 20 2a 2f 0a 20 20 20 20 6f 73 32 53  ness */.    os2S
c5da0 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 20 2f  leep,          /
c5db0 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20  * xSleep */.    
c5dc0 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65 2c 20  os2CurrentTime, 
c5dd0 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69     /* xCurrentTi
c5de0 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 47 65 74  me */.    os2Get
c5df0 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f 2a 20  LastError    /* 
c5e00 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f  xGetLastError */
c5e10 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f  .  };.  sqlite3_
c5e20 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 6f 73  vfs_register(&os
c5e30 32 56 66 73 2c 20 31 29 3b 0a 20 20 69 6e 69 74  2Vfs, 1);.  init
c5e40 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a  UconvObjects();.
c5e50 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
c5e60 4f 4b 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  OK;.}.SQLITE_API
c5e70 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f   int sqlite3_os_
c5e80 65 6e 64 28 76 6f 69 64 29 7b 0a 20 20 66 72 65  end(void){.  fre
c5e90 65 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b  eUconvObjects();
c5ea0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
c5eb0 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  _OK;.}..#endif /
c5ec0 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 20  * SQLITE_OS_OS2 
c5ed0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
c5ee0 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 6f 73  *** End of os_os
c5ef0 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  2.c ************
c5f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5f20 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
c5f30 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f  *** Begin file o
c5f40 73 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a  s_unix.c *******
c5f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5f60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c5f70 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d  **/./*.** 2004 M
c5f80 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ay 22.**.** The 
c5f90 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
c5fa0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
c5fb0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
c5fc0 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
c5fd0 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
c5fe0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
c5ff0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
c6000 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
c6010 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
c6020 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
c6030 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
c6040 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
c6050 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
c6060 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
c6070 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
c6080 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
c6090 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
c60a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c60b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c60c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c60d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c60e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
c60f0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
c6100 6e 73 20 74 68 65 20 56 46 53 20 69 6d 70 6c 65  ns the VFS imple
c6110 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 75 6e  mentation for un
c6120 69 78 2d 6c 69 6b 65 20 6f 70 65 72 61 74 69 6e  ix-like operatin
c6130 67 20 73 79 73 74 65 6d 73 0a 2a 2a 20 69 6e 63  g systems.** inc
c6140 6c 75 64 65 20 4c 69 6e 75 78 2c 20 4d 61 63 4f  lude Linux, MacO
c6150 53 58 2c 20 2a 42 53 44 2c 20 51 4e 58 2c 20 56  SX, *BSD, QNX, V
c6160 78 57 6f 72 6b 73 2c 20 41 49 58 2c 20 48 50 55  xWorks, AIX, HPU
c6170 58 2c 20 61 6e 64 20 6f 74 68 65 72 73 2e 0a 2a  X, and others..*
c6180 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 61  *.** There are a
c6190 63 74 75 61 6c 6c 79 20 73 65 76 65 72 61 6c 20  ctually several 
c61a0 64 69 66 66 65 72 65 6e 74 20 56 46 53 20 69 6d  different VFS im
c61b0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 69 6e  plementations in
c61c0 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a 20 54   this file..** T
c61d0 68 65 20 64 69 66 66 65 72 65 6e 63 65 73 20 61  he differences a
c61e0 72 65 20 69 6e 20 74 68 65 20 77 61 79 20 74 68  re in the way th
c61f0 61 74 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20  at file locking 
c6200 69 73 20 64 6f 6e 65 2e 20 20 54 68 65 20 64 65  is done.  The de
c6210 66 61 75 6c 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65  fault.** impleme
c6220 6e 74 61 74 69 6f 6e 20 75 73 65 73 20 50 6f 73  ntation uses Pos
c6230 69 78 20 41 64 76 69 73 6f 72 79 20 4c 6f 63 6b  ix Advisory Lock
c6240 73 2e 20 20 41 6c 74 65 72 6e 61 74 69 76 65 20  s.  Alternative 
c6250 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a  implementations.
c6260 2a 2a 20 75 73 65 20 66 6c 6f 63 6b 28 29 2c 20  ** use flock(), 
c6270 64 6f 74 2d 66 69 6c 65 73 2c 20 76 61 72 69 6f  dot-files, vario
c6280 75 73 20 70 72 6f 70 72 69 65 74 61 72 79 20 6c  us proprietary l
c6290 6f 63 6b 69 6e 67 20 73 63 68 65 6d 61 73 2c 20  ocking schemas, 
c62a0 6f 72 20 73 69 6d 70 6c 79 0a 2a 2a 20 73 6b 69  or simply.** ski
c62b0 70 20 6c 6f 63 6b 69 6e 67 20 61 6c 6c 20 74 6f  p locking all to
c62c0 67 65 74 68 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  gether..**.** Th
c62d0 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 20 69  is source file i
c62e0 73 20 6f 72 67 61 6e 69 7a 65 64 20 69 6e 74 6f  s organized into
c62f0 20 64 69 76 69 73 69 6f 6e 73 20 77 68 65 72 65   divisions where
c6300 20 74 68 65 20 6c 6f 67 69 63 20 66 6f 72 20 76   the logic for v
c6310 61 72 69 6f 75 73 0a 2a 2a 20 73 75 62 66 75 6e  arious.** subfun
c6320 63 74 69 6f 6e 73 20 69 73 20 63 6f 6e 74 61 69  ctions is contai
c6330 6e 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 61  ned within the a
c6340 70 70 72 6f 70 72 69 61 74 65 20 64 69 76 69 73  ppropriate divis
c6350 69 6f 6e 2e 20 20 50 4c 45 41 53 45 0a 2a 2a 20  ion.  PLEASE.** 
c6360 4b 45 45 50 20 54 48 45 20 53 54 52 55 43 54 55  KEEP THE STRUCTU
c6370 52 45 20 4f 46 20 54 48 49 53 20 46 49 4c 45 20  RE OF THIS FILE 
c6380 49 4e 54 41 43 54 2e 20 20 4e 65 77 20 63 6f 64  INTACT.  New cod
c6390 65 20 73 68 6f 75 6c 64 20 62 65 20 70 6c 61 63  e should be plac
c63a0 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f 72  ed.** in the cor
c63b0 72 65 63 74 20 64 69 76 69 73 69 6f 6e 20 61 6e  rect division an
c63c0 64 20 73 68 6f 75 6c 64 20 62 65 20 63 6c 65 61  d should be clea
c63d0 72 6c 79 20 6c 61 62 65 6c 65 64 2e 0a 2a 2a 0a  rly labeled..**.
c63e0 2a 2a 20 54 68 65 20 6c 61 79 6f 75 74 20 6f 66  ** The layout of
c63f0 20 64 69 76 69 73 69 6f 6e 73 20 69 73 20 61 73   divisions is as
c6400 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
c6410 20 20 2a 20 20 47 65 6e 65 72 61 6c 2d 70 75 72    *  General-pur
c6420 70 6f 73 65 20 64 65 63 6c 61 72 61 74 69 6f 6e  pose declaration
c6430 73 20 61 6e 64 20 75 74 69 6c 69 74 79 20 66 75  s and utility fu
c6440 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a 20  nctions..**   * 
c6450 20 55 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20   Unique file ID 
c6460 6c 6f 67 69 63 20 75 73 65 64 20 62 79 20 56 78  logic used by Vx
c6470 57 6f 72 6b 73 2e 0a 2a 2a 20 20 20 2a 20 20 56  Works..**   *  V
c6480 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 70  arious locking p
c6490 72 69 6d 69 74 69 76 65 20 69 6d 70 6c 65 6d 65  rimitive impleme
c64a0 6e 74 61 74 69 6f 6e 73 20 28 61 6c 6c 20 65 78  ntations (all ex
c64b0 63 65 70 74 20 70 72 6f 78 79 20 6c 6f 63 6b 69  cept proxy locki
c64c0 6e 67 29 3a 0a 2a 2a 20 20 20 20 20 20 2b 20 66  ng):.**      + f
c64d0 6f 72 20 50 6f 73 69 78 20 41 64 76 69 73 6f 72  or Posix Advisor
c64e0 79 20 4c 6f 63 6b 73 0a 2a 2a 20 20 20 20 20 20  y Locks.**      
c64f0 2b 20 66 6f 72 20 6e 6f 2d 6f 70 20 6c 6f 63 6b  + for no-op lock
c6500 73 0a 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20  s.**      + for 
c6510 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 73 0a 2a  dot-file locks.*
c6520 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 66 6c 6f  *      + for flo
c6530 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20  ck() locking.** 
c6540 20 20 20 20 20 2b 20 66 6f 72 20 6e 61 6d 65 64       + for named
c6550 20 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 73   semaphore locks
c6560 20 28 56 78 57 6f 72 6b 73 20 6f 6e 6c 79 29 0a   (VxWorks only).
c6570 2a 2a 20 20 20 20 20 20 2b 20 66 6f 72 20 41 46  **      + for AF
c6580 50 20 66 69 6c 65 73 79 73 74 65 6d 20 6c 6f 63  P filesystem loc
c6590 6b 73 20 28 4d 61 63 4f 53 58 20 6f 6e 6c 79 29  ks (MacOSX only)
c65a0 0a 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33  .**   *  sqlite3
c65b0 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 6e 6f  _file methods no
c65c0 74 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  t associated wit
c65d0 68 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 20  h locking..**   
c65e0 2a 20 20 44 65 66 69 6e 69 74 69 6f 6e 73 20 6f  *  Definitions o
c65f0 66 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  f sqlite3_io_met
c6600 68 6f 64 73 20 6f 62 6a 65 63 74 73 20 66 6f 72  hods objects for
c6610 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20   all locking.** 
c6620 20 20 20 20 20 6d 65 74 68 6f 64 73 20 70 6c 75       methods plu
c6630 73 20 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74  s "finder" funct
c6640 69 6f 6e 73 20 66 6f 72 20 65 61 63 68 20 6c 6f  ions for each lo
c6650 63 6b 69 6e 67 20 6d 65 74 68 6f 64 2e 0a 2a 2a  cking method..**
c6660 20 20 20 2a 20 20 73 71 6c 69 74 65 33 5f 76 66     *  sqlite3_vf
c6670 73 20 6d 65 74 68 6f 64 20 69 6d 70 6c 65 6d 65  s method impleme
c6680 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20 20 2a  ntations..**   *
c6690 20 20 4c 6f 63 6b 69 6e 67 20 70 72 69 6d 69 74    Locking primit
c66a0 69 76 65 73 20 66 6f 72 20 74 68 65 20 70 72 6f  ives for the pro
c66b0 78 79 20 75 62 65 72 2d 6c 6f 63 6b 69 6e 67 2d  xy uber-locking-
c66c0 6d 65 74 68 6f 64 2e 20 28 4d 61 63 4f 53 58 20  method. (MacOSX 
c66d0 6f 6e 6c 79 29 0a 2a 2a 20 20 20 2a 20 20 44 65  only).**   *  De
c66e0 66 69 6e 69 74 69 6f 6e 73 20 6f 66 20 73 71 6c  finitions of sql
c66f0 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63 74 73  ite3_vfs objects
c6700 20 66 6f 72 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67   for all locking
c6710 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 20 20 20 20   methods.**     
c6720 20 70 6c 75 73 20 69 6d 70 6c 65 6d 65 6e 74 61   plus implementa
c6730 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33  tions of sqlite3
c6740 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73  _os_init() and s
c6750 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e  qlite3_os_end().
c6760 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f  .*/.#if SQLITE_O
c6770 53 5f 55 4e 49 58 20 20 20 20 20 20 20 20 20 20  S_UNIX          
c6780 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65      /* This file
c6790 20 69 73 20 75 73 65 64 20 6f 6e 20 75 6e 69 78   is used on unix
c67a0 20 6f 6e 6c 79 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20   only */../*.** 
c67b0 54 68 65 72 65 20 61 72 65 20 76 61 72 69 6f 75  There are variou
c67c0 73 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 66 69  s methods for fi
c67d0 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65 64 20  le locking used 
c67e0 66 6f 72 20 63 6f 6e 63 75 72 72 65 6e 63 79 0a  for concurrency.
c67f0 2a 2a 20 63 6f 6e 74 72 6f 6c 3a 0a 2a 2a 0a 2a  ** control:.**.*
c6800 2a 20 20 20 31 2e 20 50 4f 53 49 58 20 6c 6f 63  *   1. POSIX loc
c6810 6b 69 6e 67 20 28 74 68 65 20 64 65 66 61 75 6c  king (the defaul
c6820 74 29 2c 0a 2a 2a 20 20 20 32 2e 20 4e 6f 20 6c  t),.**   2. No l
c6830 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 33 2e 20  ocking,.**   3. 
c6840 44 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 69 6e 67  Dot-file locking
c6850 2c 0a 2a 2a 20 20 20 34 2e 20 66 6c 6f 63 6b 28  ,.**   4. flock(
c6860 29 20 6c 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20  ) locking,.**   
c6870 35 2e 20 41 46 50 20 6c 6f 63 6b 69 6e 67 20 28  5. AFP locking (
c6880 4f 53 58 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 20  OSX only),.**   
c6890 36 2e 20 4e 61 6d 65 64 20 50 4f 53 49 58 20 73  6. Named POSIX s
c68a0 65 6d 61 70 68 6f 72 65 73 20 28 56 58 57 6f 72  emaphores (VXWor
c68b0 6b 73 20 6f 6e 6c 79 29 2c 0a 2a 2a 20 20 20 37  ks only),.**   7
c68c0 2e 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e  . proxy locking.
c68d0 20 28 4f 53 58 20 6f 6e 6c 79 29 0a 2a 2a 0a 2a   (OSX only).**.*
c68e0 2a 20 53 74 79 6c 65 73 20 34 2c 20 35 2c 20 61  * Styles 4, 5, a
c68f0 6e 64 20 37 20 61 72 65 20 6f 6e 6c 79 20 61 76  nd 7 are only av
c6900 61 69 6c 61 62 6c 65 20 6f 66 20 53 51 4c 49 54  ailable of SQLIT
c6910 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
c6920 5f 53 54 59 4c 45 0a 2a 2a 20 69 73 20 64 65 66  _STYLE.** is def
c6930 69 6e 65 64 20 74 6f 20 31 2e 20 20 54 68 65 20  ined to 1.  The 
c6940 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
c6950 43 4b 49 4e 47 5f 53 54 59 4c 45 20 61 6c 73 6f  CKING_STYLE also
c6960 20 65 6e 61 62 6c 65 73 20 61 75 74 6f 6d 61 74   enables automat
c6970 69 63 0a 2a 2a 20 73 65 6c 65 63 74 69 6f 6e 20  ic.** selection 
c6980 6f 66 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  of the appropria
c6990 74 65 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65  te locking style
c69a0 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 66 69   based on the fi
c69b0 6c 65 73 79 73 74 65 6d 0a 2a 2a 20 77 68 65 72  lesystem.** wher
c69c0 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  e the database i
c69d0 73 20 6c 6f 63 61 74 65 64 2e 20 20 0a 2a 2f 0a  s located.  .*/.
c69e0 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
c69f0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
c6a00 4e 47 5f 53 54 59 4c 45 29 0a 23 20 20 69 66 20  NG_STYLE).#  if 
c6a10 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f  defined(__APPLE_
c6a20 5f 29 0a 23 20 20 20 20 64 65 66 69 6e 65 20 53  _).#    define S
c6a30 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
c6a40 4b 49 4e 47 5f 53 54 59 4c 45 20 31 0a 23 20 20  KING_STYLE 1.#  
c6a50 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69 6e 65  else.#    define
c6a60 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
c6a70 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 30 0a 23  OCKING_STYLE 0.#
c6a80 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a    endif.#endif..
c6a90 2f 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 74 68 65  /*.** Define the
c6aa0 20 4f 53 5f 56 58 57 4f 52 4b 53 20 70 72 65 2d   OS_VXWORKS pre-
c6ab0 70 72 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 20  processor macro 
c6ac0 74 6f 20 31 20 69 66 20 62 75 69 6c 64 69 6e 67  to 1 if building
c6ad0 20 6f 6e 20 0a 2a 2a 20 76 78 77 6f 72 6b 73 2c   on .** vxworks,
c6ae0 20 6f 72 20 30 20 6f 74 68 65 72 77 69 73 65 2e   or 0 otherwise.
c6af0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4f 53 5f 56  .*/.#ifndef OS_V
c6b00 58 57 4f 52 4b 53 0a 23 20 20 69 66 20 64 65 66  XWORKS.#  if def
c6b10 69 6e 65 64 28 5f 5f 52 54 50 5f 5f 29 20 7c 7c  ined(__RTP__) ||
c6b20 20 64 65 66 69 6e 65 64 28 5f 57 52 53 5f 4b 45   defined(_WRS_KE
c6b30 52 4e 45 4c 29 0a 23 20 20 20 20 64 65 66 69 6e  RNEL).#    defin
c6b40 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 31 0a 23  e OS_VXWORKS 1.#
c6b50 20 20 65 6c 73 65 0a 23 20 20 20 20 64 65 66 69    else.#    defi
c6b60 6e 65 20 4f 53 5f 56 58 57 4f 52 4b 53 20 30 0a  ne OS_VXWORKS 0.
c6b70 23 20 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  #  endif.#endif.
c6b80 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 23 64 65  ./*.** These #de
c6b90 66 69 6e 65 73 20 73 68 6f 75 6c 64 20 65 6e 61  fines should ena
c6ba0 62 6c 65 20 3e 32 47 42 20 66 69 6c 65 20 73 75  ble >2GB file su
c6bb0 70 70 6f 72 74 20 6f 6e 20 50 6f 73 69 78 20 69  pport on Posix i
c6bc0 66 20 74 68 65 0a 2a 2a 20 75 6e 64 65 72 6c 79  f the.** underly
c6bd0 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79  ing operating sy
c6be0 73 74 65 6d 20 73 75 70 70 6f 72 74 73 20 69 74  stem supports it
c6bf0 2e 20 20 49 66 20 74 68 65 20 4f 53 20 6c 61 63  .  If the OS lac
c6c00 6b 73 0a 2a 2a 20 6c 61 72 67 65 20 66 69 6c 65  ks.** large file
c6c10 20 73 75 70 70 6f 72 74 2c 20 74 68 65 73 65 20   support, these 
c6c20 73 68 6f 75 6c 64 20 62 65 20 6e 6f 2d 6f 70 73  should be no-ops
c6c30 2e 0a 2a 2a 0a 2a 2a 20 4c 61 72 67 65 20 66 69  ..**.** Large fi
c6c40 6c 65 20 73 75 70 70 6f 72 74 20 63 61 6e 20 62  le support can b
c6c50 65 20 64 69 73 61 62 6c 65 64 20 75 73 69 6e 67  e disabled using
c6c60 20 74 68 65 20 2d 44 53 51 4c 49 54 45 5f 44 49   the -DSQLITE_DI
c6c70 53 41 42 4c 45 5f 4c 46 53 20 73 77 69 74 63 68  SABLE_LFS switch
c6c80 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69  .** on the compi
c6c90 6c 65 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65  ler command line
c6ca0 2e 20 20 54 68 69 73 20 69 73 20 6e 65 63 65 73  .  This is neces
c6cb0 73 61 72 79 20 69 66 20 79 6f 75 20 61 72 65 20  sary if you are 
c6cc0 63 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 6f 6e 20  compiling.** on 
c6cd0 61 20 72 65 63 65 6e 74 20 6d 61 63 68 69 6e 65  a recent machine
c6ce0 20 28 65 78 3a 20 52 65 64 48 61 74 20 37 2e 32   (ex: RedHat 7.2
c6cf0 29 20 62 75 74 20 79 6f 75 20 77 61 6e 74 20 79  ) but you want y
c6d00 6f 75 72 20 63 6f 64 65 20 74 6f 20 77 6f 72 6b  our code to work
c6d10 0a 2a 2a 20 6f 6e 20 61 6e 20 6f 6c 64 65 72 20  .** on an older 
c6d20 6d 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64  machine (ex: Red
c6d30 48 61 74 20 36 2e 30 29 2e 20 20 49 66 20 79 6f  Hat 6.0).  If yo
c6d40 75 20 63 6f 6d 70 69 6c 65 20 6f 6e 20 52 65 64  u compile on Red
c6d50 48 61 74 20 37 2e 32 0a 2a 2a 20 77 69 74 68 6f  Hat 7.2.** witho
c6d60 75 74 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20  ut this option, 
c6d70 4c 46 53 20 69 73 20 65 6e 61 62 6c 65 2e 20 20  LFS is enable.  
c6d80 42 75 74 20 4c 46 53 20 64 6f 65 73 20 6e 6f 74  But LFS does not
c6d90 20 65 78 69 73 74 20 69 6e 20 74 68 65 20 6b 65   exist in the ke
c6da0 72 6e 65 6c 0a 2a 2a 20 69 6e 20 52 65 64 48 61  rnel.** in RedHa
c6db0 74 20 36 2e 30 2c 20 73 6f 20 74 68 65 20 63 6f  t 6.0, so the co
c6dc0 64 65 20 77 6f 6e 27 74 20 77 6f 72 6b 2e 20 20  de won't work.  
c6dd0 48 65 6e 63 65 2c 20 66 6f 72 20 6d 61 78 69 6d  Hence, for maxim
c6de0 75 6d 20 62 69 6e 61 72 79 0a 2a 2a 20 70 6f 72  um binary.** por
c6df0 74 61 62 69 6c 69 74 79 20 79 6f 75 20 73 68 6f  tability you sho
c6e00 75 6c 64 20 6f 6d 69 74 20 4c 46 53 2e 0a 2a 2a  uld omit LFS..**
c6e10 0a 2a 2a 20 54 68 65 20 70 72 65 76 69 6f 75 73  .** The previous
c6e20 20 70 61 72 61 67 72 61 70 68 20 77 61 73 20 77   paragraph was w
c6e30 72 69 74 74 65 6e 20 69 6e 20 32 30 30 35 2e 20  ritten in 2005. 
c6e40 20 28 54 68 69 73 20 70 61 72 61 67 72 61 70 68   (This paragraph
c6e50 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 6f   is written.** o
c6e60 6e 20 32 30 30 38 2d 31 31 2d 32 38 2e 29 20 54  n 2008-11-28.) T
c6e70 68 65 73 65 20 64 61 79 73 2c 20 61 6c 6c 20 4c  hese days, all L
c6e80 69 6e 75 78 20 6b 65 72 6e 65 6c 73 20 73 75 70  inux kernels sup
c6e90 70 6f 72 74 20 6c 61 72 67 65 20 66 69 6c 65 73  port large files
c6ea0 2c 20 73 6f 0a 2a 2a 20 79 6f 75 20 73 68 6f 75  , so.** you shou
c6eb0 6c 64 20 70 72 6f 62 61 62 6c 79 20 6c 65 61 76  ld probably leav
c6ec0 65 20 4c 46 53 20 65 6e 61 62 6c 65 64 2e 20 20  e LFS enabled.  
c6ed0 42 75 74 20 73 6f 6d 65 20 65 6d 62 65 64 64 65  But some embedde
c6ee0 64 20 70 6c 61 74 66 6f 72 6d 73 20 6d 69 67 68  d platforms migh
c6ef0 74 0a 2a 2a 20 6c 61 63 6b 20 4c 46 53 20 69 6e  t.** lack LFS in
c6f00 20 77 68 69 63 68 20 63 61 73 65 20 74 68 65 20   which case the 
c6f10 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c  SQLITE_DISABLE_L
c6f20 46 53 20 6d 61 63 72 6f 20 6d 69 67 68 74 20 73  FS macro might s
c6f30 74 69 6c 6c 20 62 65 20 75 73 65 66 75 6c 2e 0a  till be useful..
c6f40 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
c6f50 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 0a 23 20  E_DISABLE_LFS.# 
c6f60 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 5f 46 49  define _LARGE_FI
c6f70 4c 45 20 20 20 20 20 20 20 31 0a 23 20 69 66 6e  LE       1.# ifn
c6f80 64 65 66 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54  def _FILE_OFFSET
c6f90 5f 42 49 54 53 0a 23 20 20 20 64 65 66 69 6e 65  _BITS.#   define
c6fa0 20 5f 46 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49   _FILE_OFFSET_BI
c6fb0 54 53 20 36 34 0a 23 20 65 6e 64 69 66 0a 23 20  TS 64.# endif.# 
c6fc0 64 65 66 69 6e 65 20 5f 4c 41 52 47 45 46 49 4c  define _LARGEFIL
c6fd0 45 5f 53 4f 55 52 43 45 20 31 0a 23 65 6e 64 69  E_SOURCE 1.#endi
c6fe0 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 61 6e 64 61 72  f../*.** standar
c6ff0 64 20 69 6e 63 6c 75 64 65 20 66 69 6c 65 73 2e  d include files.
c7000 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79  .*/.#include <sy
c7010 73 2f 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c  s/types.h>.#incl
c7020 75 64 65 20 3c 73 79 73 2f 73 74 61 74 2e 68 3e  ude <sys/stat.h>
c7030 0a 23 69 6e 63 6c 75 64 65 20 3c 66 63 6e 74 6c  .#include <fcntl
c7040 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e  .h>.#include <un
c7050 69 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  istd.h>.#include
c7060 20 3c 73 79 73 2f 74 69 6d 65 2e 68 3e 0a 23 69   <sys/time.h>.#i
c7070 6e 63 6c 75 64 65 20 3c 65 72 72 6e 6f 2e 68 3e  nclude <errno.h>
c7080 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  ..#if SQLITE_ENA
c7090 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
c70a0 45 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 73  E.# include <sys
c70b0 2f 69 6f 63 74 6c 2e 68 3e 0a 23 20 69 66 20 4f  /ioctl.h>.# if O
c70c0 53 5f 56 58 57 4f 52 4b 53 0a 23 20 20 69 6e 63  S_VXWORKS.#  inc
c70d0 6c 75 64 65 20 3c 73 65 6d 61 70 68 6f 72 65 2e  lude <semaphore.
c70e0 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 6c  h>.#  include <l
c70f0 69 6d 69 74 73 2e 68 3e 0a 23 20 65 6c 73 65 0a  imits.h>.# else.
c7100 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f  #  include <sys/
c7110 66 69 6c 65 2e 68 3e 0a 23 20 20 69 6e 63 6c 75  file.h>.#  inclu
c7120 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e 68 3e  de <sys/param.h>
c7130 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79 73  .#  include <sys
c7140 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 20 65 6e 64 69  /mount.h>.# endi
c7150 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  f.#endif /* SQLI
c7160 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
c7170 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a  G_STYLE */../*.*
c7180 2a 20 49 66 20 77 65 20 61 72 65 20 74 6f 20 62  * If we are to b
c7190 65 20 74 68 72 65 61 64 2d 73 61 66 65 2c 20 69  e thread-safe, i
c71a0 6e 63 6c 75 64 65 20 74 68 65 20 70 74 68 72 65  nclude the pthre
c71b0 61 64 73 20 68 65 61 64 65 72 20 61 6e 64 20 64  ads header and d
c71c0 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53 51 4c  efine.** the SQL
c71d0 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41 44 53  ITE_UNIX_THREADS
c71e0 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 20 53   macro..*/.#if S
c71f0 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
c7200 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
c7210 5f 55 4e 49 58 5f 54 48 52 45 41 44 53 20 31 0a  _UNIX_THREADS 1.
c7220 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65  #endif../*.** De
c7230 66 61 75 6c 74 20 70 65 72 6d 69 73 73 69 6f 6e  fault permission
c7240 73 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67 20  s when creating 
c7250 61 20 6e 65 77 20 66 69 6c 65 0a 2a 2f 0a 23 69  a new file.*/.#i
c7260 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 46  fndef SQLITE_DEF
c7270 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53  AULT_FILE_PERMIS
c7280 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20 53  SIONS.# define S
c7290 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49  QLITE_DEFAULT_FI
c72a0 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 20 30  LE_PERMISSIONS 0
c72b0 36 34 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 20  644.#endif../*. 
c72c0 2a 2a 20 44 65 66 61 75 6c 74 20 70 65 72 6d 69  ** Default permi
c72d0 73 73 69 6f 6e 73 20 77 68 65 6e 20 63 72 65 61  ssions when crea
c72e0 74 69 6e 67 20 61 75 74 6f 20 70 72 6f 78 79 20  ting auto proxy 
c72f0 64 69 72 0a 20 2a 2f 0a 23 69 66 6e 64 65 66 20  dir. */.#ifndef 
c7300 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50  SQLITE_DEFAULT_P
c7310 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53 49  ROXYDIR_PERMISSI
c7320 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  ONS.# define SQL
c7330 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58  ITE_DEFAULT_PROX
c7340 59 44 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53  YDIR_PERMISSIONS
c7350 20 30 37 35 35 0a 23 65 6e 64 69 66 0a 0a 2f 2a   0755.#endif../*
c7360 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 75 70 70  .** Maximum supp
c7370 6f 72 74 65 64 20 70 61 74 68 2d 6c 65 6e 67 74  orted path-lengt
c7380 68 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41  h..*/.#define MA
c7390 58 5f 50 41 54 48 4e 41 4d 45 20 35 31 32 0a 0a  X_PATHNAME 512..
c73a0 2f 2a 0a 2a 2a 20 4f 6e 6c 79 20 73 65 74 20 74  /*.** Only set t
c73b0 68 65 20 6c 61 73 74 45 72 72 6e 6f 20 69 66 20  he lastErrno if 
c73c0 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69  the error code i
c73d0 73 20 61 20 72 65 61 6c 20 65 72 72 6f 72 20 61  s a real error a
c73e0 6e 64 20 6e 6f 74 20 0a 2a 2a 20 61 20 6e 6f 72  nd not .** a nor
c73f0 6d 61 6c 20 65 78 70 65 63 74 65 64 20 72 65 74  mal expected ret
c7400 75 72 6e 20 63 6f 64 65 20 6f 66 20 53 51 4c 49  urn code of SQLI
c7410 54 45 5f 42 55 53 59 20 6f 72 20 53 51 4c 49 54  TE_BUSY or SQLIT
c7420 45 5f 4f 4b 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  E_OK.*/.#define 
c7430 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 78 29  IS_LOCK_ERROR(x)
c7440 20 20 28 28 78 20 21 3d 20 53 51 4c 49 54 45 5f    ((x != SQLITE_
c7450 4f 4b 29 20 26 26 20 28 78 20 21 3d 20 53 51 4c  OK) && (x != SQL
c7460 49 54 45 5f 42 55 53 59 29 29 0a 0a 0a 2f 2a 0a  ITE_BUSY)).../*.
c7470 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 2c 20 61 66  ** Sometimes, af
c7480 74 65 72 20 61 20 66 69 6c 65 20 68 61 6e 64 6c  ter a file handl
c7490 65 20 69 73 20 63 6c 6f 73 65 64 20 62 79 20 53  e is closed by S
c74a0 51 4c 69 74 65 2c 20 74 68 65 20 66 69 6c 65 20  QLite, the file 
c74b0 64 65 73 63 72 69 70 74 6f 72 0a 2a 2a 20 63 61  descriptor.** ca
c74c0 6e 6e 6f 74 20 62 65 20 63 6c 6f 73 65 64 20 69  nnot be closed i
c74d0 6d 6d 65 64 69 61 74 65 6c 79 2e 20 49 6e 20 74  mmediately. In t
c74e0 68 65 73 65 20 63 61 73 65 73 2c 20 69 6e 73 74  hese cases, inst
c74f0 61 6e 63 65 73 20 6f 66 20 74 68 65 20 66 6f 6c  ances of the fol
c7500 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 72 75 63 74  lowing.** struct
c7510 75 72 65 20 61 72 65 20 75 73 65 64 20 74 6f 20  ure are used to 
c7520 73 74 6f 72 65 20 74 68 65 20 66 69 6c 65 20 64  store the file d
c7530 65 73 63 72 69 70 74 6f 72 20 77 68 69 6c 65 20  escriptor while 
c7540 77 61 69 74 69 6e 67 20 66 6f 72 20 61 6e 0a 2a  waiting for an.*
c7550 2a 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f  * opportunity to
c7560 20 65 69 74 68 65 72 20 63 6c 6f 73 65 20 6f 72   either close or
c7570 20 72 65 75 73 65 20 69 74 2e 0a 2a 2f 0a 74 79   reuse it..*/.ty
c7580 70 65 64 65 66 20 73 74 72 75 63 74 20 55 6e 69  pedef struct Uni
c7590 78 55 6e 75 73 65 64 46 64 20 55 6e 69 78 55 6e  xUnusedFd UnixUn
c75a0 75 73 65 64 46 64 3b 0a 73 74 72 75 63 74 20 55  usedFd;.struct U
c75b0 6e 69 78 55 6e 75 73 65 64 46 64 20 7b 0a 20 20  nixUnusedFd {.  
c75c0 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20 20 20  int fd;         
c75d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c            /* Fil
c75e0 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20  e descriptor to 
c75f0 63 6c 6f 73 65 20 2a 2f 0a 20 20 69 6e 74 20 66  close */.  int f
c7600 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
c7610 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 74 68       /* Flags th
c7620 69 73 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  is file descript
c7630 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 77 69  or was opened wi
c7640 74 68 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 73  th */.  UnixUnus
c7650 65 64 46 64 20 2a 70 4e 65 78 74 3b 20 20 20 20  edFd *pNext;    
c7660 20 20 2f 2a 20 4e 65 78 74 20 75 6e 75 73 65 64    /* Next unused
c7670 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
c7680 20 6f 6e 20 73 61 6d 65 20 66 69 6c 65 20 2a 2f   on same file */
c7690 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 75  .};../*.** The u
c76a0 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 72  nixFile structur
c76b0 65 20 69 73 20 73 75 62 63 6c 61 73 73 20 6f 66  e is subclass of
c76c0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 70   sqlite3_file sp
c76d0 65 63 69 66 69 63 20 74 6f 20 74 68 65 20 75 6e  ecific to the un
c76e0 69 78 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65 6d  ix.** VFS implem
c76f0 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 79  entations..*/.ty
c7700 70 65 64 65 66 20 73 74 72 75 63 74 20 75 6e 69  pedef struct uni
c7710 78 46 69 6c 65 20 75 6e 69 78 46 69 6c 65 3b 0a  xFile unixFile;.
c7720 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 20  struct unixFile 
c7730 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  {.  sqlite3_io_m
c7740 65 74 68 6f 64 73 20 63 6f 6e 73 74 20 2a 70 4d  ethods const *pM
c7750 65 74 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 79  ethod;  /* Alway
c7760 73 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72  s the first entr
c7770 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e  y */.  struct un
c7780 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e  ixOpenCnt *pOpen
c7790 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20  ;       /* Info 
c77a0 61 62 6f 75 74 20 61 6c 6c 20 6f 70 65 6e 20 66  about all open f
c77b0 64 27 73 20 6f 6e 20 74 68 69 73 20 69 6e 6f 64  d's on this inod
c77c0 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75 6e  e */.  struct un
c77d0 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63  ixLockInfo *pLoc
c77e0 6b 3b 20 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20  k;      /* Info 
c77f0 61 62 6f 75 74 20 6c 6f 63 6b 73 20 6f 6e 20 74  about locks on t
c7800 68 69 73 20 69 6e 6f 64 65 20 2a 2f 0a 20 20 69  his inode */.  i
c7810 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20 20  nt h;           
c7820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7830 2f 2a 20 54 68 65 20 66 69 6c 65 20 64 65 73 63  /* The file desc
c7840 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20  riptor */.  int 
c7850 64 69 72 66 64 3b 20 20 20 20 20 20 20 20 20 20  dirfd;          
c7860 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c7870 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  File descriptor 
c7880 66 6f 72 20 74 68 65 20 64 69 72 65 63 74 6f 72  for the director
c7890 79 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  y */.  unsigned 
c78a0 63 68 61 72 20 6c 6f 63 6b 74 79 70 65 3b 20 20  char locktype;  
c78b0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
c78c0 79 70 65 20 6f 66 20 6c 6f 63 6b 20 68 65 6c 64  ype of lock held
c78d0 20 6f 6e 20 74 68 69 73 20 66 64 20 2a 2f 0a 20   on this fd */. 
c78e0 20 69 6e 74 20 6c 61 73 74 45 72 72 6e 6f 3b 20   int lastErrno; 
c78f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7900 20 20 2f 2a 20 54 68 65 20 75 6e 69 78 20 65 72    /* The unix er
c7910 72 6e 6f 20 66 72 6f 6d 20 74 68 65 20 6c 61 73  rno from the las
c7920 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a 2f 0a 20  t I/O error */. 
c7930 20 76 6f 69 64 20 2a 6c 6f 63 6b 69 6e 67 43 6f   void *lockingCo
c7940 6e 74 65 78 74 3b 20 20 20 20 20 20 20 20 20 20  ntext;          
c7950 20 20 2f 2a 20 4c 6f 63 6b 69 6e 67 20 73 74 79    /* Locking sty
c7960 6c 65 20 73 70 65 63 69 66 69 63 20 73 74 61 74  le specific stat
c7970 65 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75 73 65  e */.  UnixUnuse
c7980 64 46 64 20 2a 70 55 6e 75 73 65 64 3b 20 20 20  dFd *pUnused;   
c7990 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65 2d 61          /* Pre-a
c79a0 6c 6c 6f 63 61 74 65 64 20 55 6e 69 78 55 6e 75  llocated UnixUnu
c79b0 73 65 64 46 64 20 2a 2f 0a 20 20 69 6e 74 20 66  sedFd */.  int f
c79c0 69 6c 65 46 6c 61 67 73 3b 20 20 20 20 20 20 20  ileFlags;       
c79d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
c79e0 69 73 63 65 6c 6c 61 6e 6f 75 73 20 66 6c 61 67  iscellanous flag
c79f0 73 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  s */.#if SQLITE_
c7a00 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
c7a10 54 59 4c 45 0a 20 20 69 6e 74 20 6f 70 65 6e 46  TYLE.  int openF
c7a20 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
c7a30 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
c7a40 6c 61 67 73 20 73 70 65 63 69 66 69 65 64 20 61  lags specified a
c7a50 74 20 6f 70 65 6e 28 29 20 2a 2f 0a 23 65 6e 64  t open() */.#end
c7a60 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  if.#if SQLITE_TH
c7a70 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69  READSAFE && defi
c7a80 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 20  ned(__linux__). 
c7a90 20 70 74 68 72 65 61 64 5f 74 20 74 69 64 3b 20   pthread_t tid; 
c7aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c7ab0 20 20 2f 2a 20 54 68 65 20 74 68 72 65 61 64 20    /* The thread 
c7ac0 74 68 61 74 20 22 6f 77 6e 73 22 20 74 68 69 73  that "owns" this
c7ad0 20 75 6e 69 78 46 69 6c 65 20 2a 2f 0a 23 65 6e   unixFile */.#en
c7ae0 64 69 66 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52  dif.#if OS_VXWOR
c7af0 4b 53 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 74  KS.  int isDelet
c7b00 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
c7b10 20 20 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20        /* Delete 
c7b20 6f 6e 20 63 6c 6f 73 65 20 69 66 20 74 72 75 65  on close if true
c7b30 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 76 78 77   */.  struct vxw
c7b40 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70 49 64 3b  orksFileId *pId;
c7b50 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 71 75 65         /* Unique
c7b60 20 66 69 6c 65 20 49 44 20 2a 2f 0a 23 65 6e 64   file ID */.#end
c7b70 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  if.#ifndef NDEBU
c7b80 47 0a 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20  G.  /* The next 
c7b90 67 72 6f 75 70 20 6f 66 20 76 61 72 69 61 62 6c  group of variabl
c7ba0 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 74  es are used to t
c7bb0 72 61 63 6b 20 77 68 65 74 68 65 72 20 6f 72 20  rack whether or 
c7bc0 6e 6f 74 20 74 68 65 0a 20 20 2a 2a 20 74 72 61  not the.  ** tra
c7bd0 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72  nsaction counter
c7be0 20 69 6e 20 62 79 74 65 73 20 32 34 2d 32 37 20   in bytes 24-27 
c7bf0 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  of database file
c7c00 73 20 61 72 65 20 75 70 64 61 74 65 64 0a 20 20  s are updated.  
c7c10 2a 2a 20 77 68 65 6e 65 76 65 72 20 61 6e 79 20  ** whenever any 
c7c20 70 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61  part of the data
c7c30 62 61 73 65 20 63 68 61 6e 67 65 73 2e 20 20 41  base changes.  A
c7c40 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c  n assertion faul
c7c50 74 20 77 69 6c 6c 0a 20 20 2a 2a 20 6f 63 63 75  t will.  ** occu
c7c60 72 20 69 66 20 61 20 66 69 6c 65 20 69 73 20 75  r if a file is u
c7c70 70 64 61 74 65 64 20 77 69 74 68 6f 75 74 20 61  pdated without a
c7c80 6c 73 6f 20 75 70 64 61 74 69 6e 67 20 74 68 65  lso updating the
c7c90 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a   transaction.  *
c7ca0 2a 20 63 6f 75 6e 74 65 72 2e 20 20 54 68 69 73  * counter.  This
c7cb0 20 74 65 73 74 20 69 73 20 6d 61 64 65 20 74 6f   test is made to
c7cc0 20 61 76 6f 69 64 20 6e 65 77 20 70 72 6f 62 6c   avoid new probl
c7cd0 65 6d 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 74  ems similar to t
c7ce0 68 65 0a 20 20 2a 2a 20 6f 6e 65 20 64 65 73 63  he.  ** one desc
c7cf0 72 69 62 65 64 20 62 79 20 74 69 63 6b 65 74 20  ribed by ticket 
c7d00 23 33 35 38 34 2e 20 0a 20 20 2a 2f 0a 20 20 75  #3584. .  */.  u
c7d10 6e 73 69 67 6e 65 64 20 63 68 61 72 20 74 72 61  nsigned char tra
c7d20 6e 73 43 6e 74 72 43 68 6e 67 3b 20 20 20 2f 2a  nsCntrChng;   /*
c7d30 20 54 72 75 65 20 69 66 20 74 68 65 20 74 72 61   True if the tra
c7d40 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 65 72  nsaction counter
c7d50 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75 6e   changed */.  un
c7d60 73 69 67 6e 65 64 20 63 68 61 72 20 64 62 55 70  signed char dbUp
c7d70 64 61 74 65 3b 20 20 20 20 20 20 20 20 2f 2a 20  date;        /* 
c7d80 54 72 75 65 20 69 66 20 61 6e 79 20 70 61 72 74  True if any part
c7d90 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69 6c   of database fil
c7da0 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75  e changed */.  u
c7db0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 69 6e 4e  nsigned char inN
c7dc0 6f 72 6d 61 6c 57 72 69 74 65 3b 20 20 20 2f 2a  ormalWrite;   /*
c7dd0 20 54 72 75 65 20 69 66 20 69 6e 20 61 20 6e 6f   True if in a no
c7de0 72 6d 61 6c 20 77 72 69 74 65 20 6f 70 65 72 61  rmal write opera
c7df0 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a 23  tion */.#endif.#
c7e00 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
c7e10 54 0a 20 20 2f 2a 20 49 6e 20 74 65 73 74 20 6d  T.  /* In test m
c7e20 6f 64 65 2c 20 69 6e 63 72 65 61 73 65 20 74 68  ode, increase th
c7e30 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73 20 73  e size of this s
c7e40 74 72 75 63 74 75 72 65 20 61 20 62 69 74 20 73  tructure a bit s
c7e50 6f 20 74 68 61 74 20 0a 20 20 2a 2a 20 69 74 20  o that .  ** it 
c7e60 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  is larger than t
c7e70 68 65 20 73 74 72 75 63 74 20 43 72 61 73 68 46  he struct CrashF
c7e80 69 6c 65 20 64 65 66 69 6e 65 64 20 69 6e 20 74  ile defined in t
c7e90 65 73 74 36 2e 63 2e 0a 20 20 2a 2f 0a 20 20 63  est6.c..  */.  c
c7ea0 68 61 72 20 61 50 61 64 64 69 6e 67 5b 33 32 5d  har aPadding[32]
c7eb0 3b 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a  ;.#endif.};../*.
c7ec0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
c7ed0 20 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 20 62   macros define b
c7ee0 69 74 73 20 69 6e 20 75 6e 69 78 46 69 6c 65 2e  its in unixFile.
c7ef0 66 69 6c 65 46 6c 61 67 73 0a 2a 2f 0a 23 64 65  fileFlags.*/.#de
c7f00 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 48 4f 4c  fine SQLITE_WHOL
c7f10 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 20  E_FILE_LOCKING  
c7f20 30 78 30 30 30 31 20 20 20 2f 2a 20 55 73 65 20  0x0001   /* Use 
c7f30 77 68 6f 6c 65 2d 66 69 6c 65 20 6c 6f 63 6b 69  whole-file locki
c7f40 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63  ng */../*.** Inc
c7f50 6c 75 64 65 20 63 6f 64 65 20 74 68 61 74 20 69  lude code that i
c7f60 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20  s common to all 
c7f70 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a 2a 2f 0a  os_*.c files.*/.
c7f80 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
c7f90 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f 6d 6d 6f  Include os_commo
c7fa0 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  n.h in the middl
c7fb0 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e 63 20 2a  e of os_unix.c *
c7fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
c7fd0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
c7fe0 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 63 6f  Begin file os_co
c7ff0 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mmon.h *********
c8000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
c8020 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32  /*.** 2004 May 2
c8030 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  2.**.** The auth
c8040 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
c8050 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
c8060 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
c8070 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
c8080 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
c8090 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
c80a0 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
c80b0 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
c80c0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
c80d0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
c80e0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
c80f0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
c8100 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
c8110 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
c8120 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
c8130 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
c8140 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
c8150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8190 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
c81a0 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6d   file contains m
c81b0 61 63 72 6f 73 20 61 6e 64 20 61 20 6c 69 74 74  acros and a litt
c81c0 6c 65 20 62 69 74 20 6f 66 20 63 6f 64 65 20 74  le bit of code t
c81d0 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f  hat is common to
c81e0 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 70  .** all of the p
c81f0 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69 66 69 63  latform-specific
c8200 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e 63 29 20   files (os_*.c) 
c8210 61 6e 64 20 69 73 20 23 69 6e 63 6c 75 64 65 64  and is #included
c8220 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a 2a 20 66   into those.** f
c8230 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  iles..**.** This
c8240 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20   file should be 
c8250 23 69 6e 63 6c 75 64 65 64 20 62 79 20 74 68 65  #included by the
c8260 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 20 6f 6e   os_*.c files on
c8270 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 61  ly.  It is not a
c8280 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70 75 72 70  .** general purp
c8290 6f 73 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e  ose header file.
c82a0 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f 63  .**.** $Id: os_c
c82b0 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33 38 20 32  ommon.h,v 1.38 2
c82c0 30 30 39 2f 30 32 2f 32 34 20 31 38 3a 34 30 3a  009/02/24 18:40:
c82d0 35 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  50 danielk1977 E
c82e0 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  xp $.*/.#ifndef 
c82f0 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64  _OS_COMMON_H_.#d
c8300 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e  efine _OS_COMMON
c8310 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65  _H_../*.** At le
c8320 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 61 76  ast two bugs hav
c8330 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63  e slipped in bec
c8340 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 64 20  ause we changed 
c8350 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47  the MEMORY_DEBUG
c8360 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c  .** macro to SQL
c8370 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f  ITE_DEBUG and so
c8380 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c  me older makefil
c8390 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20  es have not yet 
c83a0 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74  made the.** swit
c83b0 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ch.  The followi
c83c0 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63  ng code should c
c83d0 61 74 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65  atch this proble
c83e0 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  m at compile-tim
c83f0 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d  e..*/.#ifdef MEM
c8400 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f  ORY_DEBUG.# erro
c8410 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45  r "The MEMORY_DE
c8420 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73  BUG macro is obs
c8430 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49  olete.  Use SQLI
c8440 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64  TE_DEBUG instead
c8450 2e 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65  .".#endif..#ifde
c8460 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53  f SQLITE_DEBUG.S
c8470 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
c8480 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  t sqlite3OSTrace
c8490 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 53   = 0;.#define OS
c84a0 54 52 41 43 45 31 28 58 29 20 20 20 20 20 20 20  TRACE1(X)       
c84b0 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54    if( sqlite3OST
c84c0 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65  race ) sqlite3De
c84d0 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23 64 65  bugPrintf(X).#de
c84e0 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c  fine OSTRACE2(X,
c84f0 59 29 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  Y)       if( sql
c8500 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73 71  ite3OSTrace ) sq
c8510 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
c8520 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53  (X,Y).#define OS
c8530 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20 20 20  TRACE3(X,Y,Z)   
c8540 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54    if( sqlite3OST
c8550 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65  race ) sqlite3De
c8560 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29  bugPrintf(X,Y,Z)
c8570 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
c8580 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69 66 28  4(X,Y,Z,A)   if(
c8590 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20   sqlite3OSTrace 
c85a0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
c85b0 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a 23 64  intf(X,Y,Z,A).#d
c85c0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35 28 58  efine OSTRACE5(X
c85d0 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20 73 71  ,Y,Z,A,B) if( sq
c85e0 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20 73  lite3OSTrace ) s
c85f0 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
c8600 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65  f(X,Y,Z,A,B).#de
c8610 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c  fine OSTRACE6(X,
c8620 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20 20 20  Y,Z,A,B,C) \.   
c8630 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61   if(sqlite3OSTra
c8640 63 65 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  ce) sqlite3Debug
c8650 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42  Printf(X,Y,Z,A,B
c8660 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,C).#define OSTR
c8670 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  ACE7(X,Y,Z,A,B,C
c8680 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c  ,D) \.    if(sql
c8690 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c  ite3OSTrace) sql
c86a0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
c86b0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23  X,Y,Z,A,B,C,D).#
c86c0 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 53 54  else.#define OST
c86d0 52 41 43 45 31 28 58 29 0a 23 64 65 66 69 6e 65  RACE1(X).#define
c86e0 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29 0a 23   OSTRACE2(X,Y).#
c86f0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28  define OSTRACE3(
c8700 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f  X,Y,Z).#define O
c8710 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29  STRACE4(X,Y,Z,A)
c8720 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
c8730 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23 64 65  5(X,Y,Z,A,B).#de
c8740 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28 58 2c  fine OSTRACE6(X,
c8750 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69  Y,Z,A,B,C).#defi
c8760 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c  ne OSTRACE7(X,Y,
c8770 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e 64 69  Z,A,B,C,D).#endi
c8780 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20  f../*.** Macros 
c8790 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  for performance 
c87a0 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d 61 6c  tracing.  Normal
c87b0 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e 20 20  ly turned off.  
c87c0 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20 6f 6e  Only works.** on
c87d0 20 69 34 38 36 20 68 61 72 64 77 61 72 65 2e 0a   i486 hardware..
c87e0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
c87f0 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54 52 41  _PERFORMANCE_TRA
c8800 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d  CE../* .** hwtim
c8810 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c  e.h contains inl
c8820 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20 63 6f  ine assembler co
c8830 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74  de for implement
c8840 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70 65 72  ing .** high-per
c8850 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e 67 20  formance timing 
c8860 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a  routines..*/./**
c8870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
c8880 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20 69 6e  lude hwtime.h in
c8890 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f   the middle of o
c88a0 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a  s_common.h *****
c88b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
c88c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
c88d0 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65 2e 68  in file hwtime.h
c88e0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
c88f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
c8910 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37 0a 2a  ** 2008 May 27.*
c8920 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
c8930 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
c8940 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
c8950 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
c8960 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
c8970 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
c8980 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
c8990 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
c89a0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
c89b0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
c89c0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
c89d0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
c89e0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
c89f0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
c8a00 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
c8a10 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
c8a20 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
c8a30 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
c8a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8a80 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  **.**.** This fi
c8a90 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69  le contains inli
c8aa0 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f 72 20  ne asm code for 
c8ab0 72 65 74 72 69 65 76 69 6e 67 20 22 68 69 67 68  retrieving "high
c8ac0 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a  -performance".**
c8ad0 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20 78 38   counters for x8
c8ae0 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a 2a 2a  6 class CPUs..**
c8af0 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d 65 2e  .** $Id: hwtime.
c8b00 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 38 2f  h,v 1.3 2008/08/
c8b10 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68 61 6e  01 14:33:15 shan
c8b20 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64  e Exp $.*/.#ifnd
c8b30 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 23 64  ef _HWTIME_H_.#d
c8b40 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f 48 5f  efine _HWTIME_H_
c8b50 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
c8b60 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 6f 6e  owing routine on
c8b70 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65 6e 74  ly works on pent
c8b80 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20 6e 65  ium-class (or ne
c8b90 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72 73 2e  wer) processors.
c8ba0 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68 65 20  .** It uses the 
c8bb0 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74 6f 20  RDTSC opcode to 
c8bc0 72 65 61 64 20 74 68 65 20 63 79 63 6c 65 20 63  read the cycle c
c8bd0 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74 20 6f  ount value out o
c8be0 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65 73 73  f the.** process
c8bf0 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73 20 74  or and returns t
c8c00 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68 69 73  hat value.  This
c8c10 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72   can be used for
c8c20 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70 72 6f   high-res.** pro
c8c30 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28  filing..*/.#if (
c8c40 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f  defined(__GNUC__
c8c50 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 53  ) || defined(_MS
c8c60 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20 20 20  C_VER)) && \.   
c8c70 20 20 20 28 64 65 66 69 6e 65 64 28 69 33 38 36     (defined(i386
c8c80 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 69  ) || defined(__i
c8c90 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65  386__) || define
c8ca0 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20 20 23  d(_M_IX86))..  #
c8cb0 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55  if defined(__GNU
c8cc0 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65  C__)..  __inline
c8cd0 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  __ sqlite_uint64
c8ce0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76   sqlite3Hwtime(v
c8cf0 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73 69 67  oid){.     unsig
c8d00 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a  ned int lo, hi;.
c8d10 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76       __asm__ __v
c8d20 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73  olatile__ ("rdts
c8d30 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29 2c 20  c" : "=a" (lo), 
c8d40 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20 20 20  "=d" (hi));.    
c8d50 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65 5f   return (sqlite_
c8d60 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33 32 20  uint64)hi << 32 
c8d70 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c  | lo;.  }..  #el
c8d80 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f  if defined(_MSC_
c8d90 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c 73 70  VER)..  __declsp
c8da0 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e 6c 69  ec(naked) __inli
c8db0 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  ne sqlite_uint64
c8dc0 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74 65 33   __cdecl sqlite3
c8dd0 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20  Hwtime(void){.  
c8de0 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20 20 20     __asm {.     
c8df0 20 20 20 72 64 74 73 63 0a 20 20 20 20 20 20 20     rdtsc.       
c8e00 20 72 65 74 20 20 20 20 20 20 20 3b 20 72 65 74   ret       ; ret
c8e10 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45 44 58  urn value at EDX
c8e20 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20 7d 0a  :EAX.     }.  }.
c8e30 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c 69 66  .  #endif..#elif
c8e40 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43   (defined(__GNUC
c8e50 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f  __) && defined(_
c8e60 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f  _x86_64__))..  _
c8e70 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65  _inline__ sqlite
c8e80 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48  _uint64 sqlite3H
c8e90 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20  wtime(void){.   
c8ea0 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67     unsigned long
c8eb0 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f 61 73   val;.      __as
c8ec0 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f  m__ __volatile__
c8ed0 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 41 22   ("rdtsc" : "=A"
c8ee0 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20 20 72   (val));.      r
c8ef0 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20  eturn val;.  }. 
c8f00 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28  .#elif (defined(
c8f10 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66  __GNUC__) && def
c8f20 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a  ined(__ppc__))..
c8f30 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c    __inline__ sql
c8f40 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74  ite_uint64 sqlit
c8f50 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a  e3Hwtime(void){.
c8f60 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c        unsigned l
c8f70 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61 6c 3b  ong long retval;
c8f80 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  .      unsigned 
c8f90 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20 20 20  long junk;.     
c8fa0 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74   __asm__ __volat
c8fb0 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20 20 20  ile__ ("\n\.    
c8fc0 20 20 20 20 20 20 31 3a 20 20 20 20 20 20 6d 66        1:      mf
c8fd0 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20 20 20  tbu   %1\n\.    
c8fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 66                mf
c8ff0 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20 20 20  tb    %L0\n\.   
c9000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d                 m
c9010 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20 20 20  ftbu   %0\n\.   
c9020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
c9030 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e 5c 0a  mpw    %0,%1\n\.
c9040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9050 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a 20 20    bne     1b".  
c9060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9070 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c 29 2c  : "=r" (retval),
c9080 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b 0a 20   "=r" (junk));. 
c9090 20 20 20 20 20 72 65 74 75 72 6e 20 72 65 74 76       return retv
c90a0 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a  al;.  }..#else..
c90b0 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20 69 6d    #error Need im
c90c0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
c90d0 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20  sqlite3Hwtime() 
c90e0 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72  for your platfor
c90f0 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f  m...  /*.  ** To
c9100 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f 75 74   compile without
c9110 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 73 71   implementing sq
c9120 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f  lite3Hwtime() fo
c9130 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2c  r your platform,
c9140 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20 72 65  .  ** you can re
c9150 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65 20 23  move the above #
c9160 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20 74 68  error and use th
c9170 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a  e following.  **
c9180 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e 2e 20   stub function. 
c9190 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65 20 74   You will lose t
c91a0 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20 66 6f  iming support fo
c91b0 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66 20 74  r many.  ** of t
c91c0 68 65 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64  he debugging and
c91d0 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69 74 69   testing utiliti
c91e0 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f 75 6c  es, but it shoul
c91f0 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73 74 20  d at.  ** least 
c9200 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 2e  compile and run.
c9210 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  .  */.SQLITE_PRI
c9220 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f 75 69  VATE   sqlite_ui
c9230 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69  nt64 sqlite3Hwti
c9240 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  me(void){ return
c9250 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34   ((sqlite_uint64
c9260 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a  )0); }..#endif..
c9270 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
c9280 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29 20 2a  ed(_HWTIME_H_) *
c9290 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
c92a0 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69 6d 65  ** End of hwtime
c92b0 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
c92c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c92d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c92e0 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
c92f0 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
c9300 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
c9310 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a  in os_common.h *
c9320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9330 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c 69 74  */..static sqlit
c9340 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 61 72 74  e_uint64 g_start
c9350 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 5f  ;.static sqlite_
c9360 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 73 65 64  uint64 g_elapsed
c9370 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  ;.#define TIMER_
c9380 53 54 41 52 54 20 20 20 20 20 20 20 67 5f 73 74  START       g_st
c9390 61 72 74 3d 73 71 6c 69 74 65 33 48 77 74 69 6d  art=sqlite3Hwtim
c93a0 65 28 29 0a 23 64 65 66 69 6e 65 20 54 49 4d 45  e().#define TIME
c93b0 52 5f 45 4e 44 20 20 20 20 20 20 20 20 20 67 5f  R_END         g_
c93c0 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 65 33 48  elapsed=sqlite3H
c93d0 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72 74 0a  wtime()-g_start.
c93e0 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c  #define TIMER_EL
c93f0 41 50 53 45 44 20 20 20 20 20 67 5f 65 6c 61 70  APSED     g_elap
c9400 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  sed.#else.#defin
c9410 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a 23 64  e TIMER_START.#d
c9420 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 0a  efine TIMER_END.
c9430 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4c  #define TIMER_EL
c9440 41 50 53 45 44 20 20 20 20 20 28 28 73 71 6c 69  APSED     ((sqli
c9450 74 65 5f 75 69 6e 74 36 34 29 30 29 0a 23 65 6e  te_uint64)0).#en
c9460 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65  dif../*.** If we
c9470 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 74 68   compile with th
c9480 65 20 53 51 4c 49 54 45 5f 54 45 53 54 20 6d 61  e SQLITE_TEST ma
c9490 63 72 6f 20 73 65 74 2c 20 74 68 65 6e 20 74 68  cro set, then th
c94a0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63  e following bloc
c94b0 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77 69 6c  k.** of code wil
c94c0 6c 20 67 69 76 65 20 75 73 20 74 68 65 20 61 62  l give us the ab
c94d0 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c 61 74  ility to simulat
c94e0 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72  e a disk I/O err
c94f0 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69 73 20  or.  This.** is 
c9500 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67  used for testing
c9510 20 74 68 65 20 49 2f 4f 20 72 65 63 6f 76 65 72   the I/O recover
c9520 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69 66 64  y logic..*/.#ifd
c9530 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
c9540 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
c9550 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68  lite3_io_error_h
c9560 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  it = 0;         
c9570 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62     /* Total numb
c9580 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f 72 73  er of I/O Errors
c9590 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
c95a0 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
c95b0 72 6f 72 5f 68 61 72 64 68 69 74 20 3d 20 30 3b  ror_hardhit = 0;
c95c0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
c95d0 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67 6e 20  r of non-benign 
c95e0 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45  errors */.SQLITE
c95f0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
c9600 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e  _io_error_pendin
c9610 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  g = 0;        /*
c9620 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f 20 66   Count down to f
c9630 69 72 73 74 20 49 2f 4f 20 65 72 72 6f 72 20 2a  irst I/O error *
c9640 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
c9650 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
c9660 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b 20 20  r_persist = 0;  
c9670 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
c9680 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 65 72 73   I/O errors pers
c9690 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ist */.SQLITE_AP
c96a0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f  I int sqlite3_io
c96b0 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 3d 20  _error_benign = 
c96c0 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  0;         /* Tr
c96d0 75 65 20 69 66 20 65 72 72 6f 72 73 20 61 72 65  ue if errors are
c96e0 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c 49 54   benign */.SQLIT
c96f0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
c9700 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69  3_diskfull_pendi
c9710 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41  ng = 0;.SQLITE_A
c9720 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64  PI int sqlite3_d
c9730 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23 64 65  iskfull = 0;.#de
c9740 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45  fine SimulateIOE
c9750 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20 73 71  rrorBenign(X) sq
c9760 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62  lite3_io_error_b
c9770 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66 69 6e  enign=(X).#defin
c9780 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f  e SimulateIOErro
c9790 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69 66 28  r(CODE)  \.  if(
c97a0 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72   (sqlite3_io_err
c97b0 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20 73 71  or_persist && sq
c97c0 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68  lite3_io_error_h
c97d0 69 74 29 20 5c 0a 20 20 20 20 20 20 20 7c 7c 20  it) \.       || 
c97e0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
c97f0 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20 31 20  _pending-- == 1 
c9800 29 20 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20  )  \.           
c9810 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65 72 72     { local_ioerr
c9820 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74 61 74  (); CODE; }.stat
c9830 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f 69 6f  ic void local_io
c9840 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41 43 45  err(){.  IOTRACE
c9850 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b 0a 20  (("IOERR\n"));. 
c9860 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
c9870 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28 20 21  r_hit++;.  if( !
c9880 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
c9890 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c 69 74 65  _benign ) sqlite
c98a0 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68  3_io_error_hardh
c98b0 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e 65 20  it++;.}.#define 
c98c0 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c  SimulateDiskfull
c98d0 45 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a 20 20  Error(CODE) \.  
c98e0 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73   if( sqlite3_dis
c98f0 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 29 7b  kfull_pending ){
c9900 20 5c 0a 20 20 20 20 20 69 66 28 20 73 71 6c 69   \.     if( sqli
c9910 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e  te3_diskfull_pen
c9920 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c 0a 20  ding == 1 ){ \. 
c9930 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f 65 72        local_ioer
c9940 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 20 73 71  r(); \.       sq
c9950 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d  lite3_diskfull =
c9960 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c   1; \.       sql
c9970 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69  ite3_io_error_hi
c9980 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20  t = 1; \.       
c9990 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d 65 6c  CODE; \.     }el
c99a0 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 73 71 6c  se{ \.       sql
c99b0 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65  ite3_diskfull_pe
c99c0 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20 20 20  nding--; \.     
c99d0 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65 0a 23  } \.   }.#else.#
c99e0 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49  define SimulateI
c99f0 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 0a  OErrorBenign(X).
c9a00 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
c9a10 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 65 66 69  IOError(A).#defi
c9a20 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66  ne SimulateDiskf
c9a30 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65 6e 64  ullError(A).#end
c9a40 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74  if../*.** When t
c9a50 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 61 20 63  esting, keep a c
c9a60 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d 62  ount of the numb
c9a70 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c 65 73  er of open files
c9a80 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
c9a90 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41  TE_TEST.SQLITE_A
c9aa0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
c9ab0 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 20 3d  pen_file_count =
c9ac0 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e   0;.#define Open
c9ad0 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71 6c 69  Counter(X)  sqli
c9ae0 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f  te3_open_file_co
c9af0 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65 0a 23  unt+=(X).#else.#
c9b00 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74  define OpenCount
c9b10 65 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65  er(X).#endif..#e
c9b20 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64  ndif /* !defined
c9b30 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 29 20  (_OS_COMMON_H_) 
c9b40 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
c9b50 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 63 6f  *** End of os_co
c9b60 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mmon.h *********
c9b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9b90 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
c9ba0 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77  *** Continuing w
c9bb0 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66  here we left off
c9bc0 20 69 6e 20 6f 73 5f 75 6e 69 78 2e 63 20 2a 2a   in os_unix.c **
c9bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9be0 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 69 6e  **/../*.** Defin
c9bf0 65 20 76 61 72 69 6f 75 73 20 6d 61 63 72 6f 73  e various macros
c9c00 20 74 68 61 74 20 61 72 65 20 6d 69 73 73 69 6e   that are missin
c9c10 67 20 66 72 6f 6d 20 73 6f 6d 65 20 73 79 73 74  g from some syst
c9c20 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ems..*/.#ifndef 
c9c30 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 65  O_LARGEFILE.# de
c9c40 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45  fine O_LARGEFILE
c9c50 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66   0.#endif.#ifdef
c9c60 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f   SQLITE_DISABLE_
c9c70 4c 46 53 0a 23 20 75 6e 64 65 66 20 4f 5f 4c 41  LFS.# undef O_LA
c9c80 52 47 45 46 49 4c 45 0a 23 20 64 65 66 69 6e 65  RGEFILE.# define
c9c90 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20 30 0a 23   O_LARGEFILE 0.#
c9ca0 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4f 5f  endif.#ifndef O_
c9cb0 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 64 65 66 69 6e  NOFOLLOW.# defin
c9cc0 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 20 30 0a 23  e O_NOFOLLOW 0.#
c9cd0 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4f 5f  endif.#ifndef O_
c9ce0 42 49 4e 41 52 59 0a 23 20 64 65 66 69 6e 65 20  BINARY.# define 
c9cf0 4f 5f 42 49 4e 41 52 59 20 30 0a 23 65 6e 64 69  O_BINARY 0.#endi
c9d00 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 44 4a 47  f../*.** The DJG
c9d10 50 50 20 63 6f 6d 70 69 6c 65 72 20 65 6e 76 69  PP compiler envi
c9d20 72 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b 73 20 6d 6f  ronment looks mo
c9d30 73 74 6c 79 20 6c 69 6b 65 20 55 6e 69 78 2c 20  stly like Unix, 
c9d40 62 75 74 20 69 74 0a 2a 2a 20 6c 61 63 6b 73 20  but it.** lacks 
c9d50 74 68 65 20 66 63 6e 74 6c 28 29 20 73 79 73 74  the fcntl() syst
c9d60 65 6d 20 63 61 6c 6c 2e 20 20 53 6f 20 72 65 64  em call.  So red
c9d70 65 66 69 6e 65 20 66 63 6e 74 6c 28 29 20 74 6f  efine fcntl() to
c9d80 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a   be something.**
c9d90 20 74 68 61 74 20 61 6c 77 61 79 73 20 73 75 63   that always suc
c9da0 63 65 65 64 73 2e 20 20 54 68 69 73 20 6d 65 61  ceeds.  This mea
c9db0 6e 73 20 74 68 61 74 20 6c 6f 63 6b 69 6e 67 20  ns that locking 
c9dc0 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72 20 75  does not occur u
c9dd0 6e 64 65 72 0a 2a 2a 20 44 4a 47 50 50 2e 20 20  nder.** DJGPP.  
c9de0 42 75 74 20 69 74 20 69 73 20 44 4f 53 20 2d 20  But it is DOS - 
c9df0 77 68 61 74 20 64 69 64 20 79 6f 75 20 65 78 70  what did you exp
c9e00 65 63 74 3f 0a 2a 2f 0a 23 69 66 64 65 66 20 5f  ect?.*/.#ifdef _
c9e10 5f 44 4a 47 50 50 5f 5f 0a 23 20 64 65 66 69 6e  _DJGPP__.# defin
c9e20 65 20 66 63 6e 74 6c 28 41 2c 42 2c 43 29 20 30  e fcntl(A,B,C) 0
c9e30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
c9e40 68 65 20 74 68 72 65 61 64 69 64 20 6d 61 63 72  he threadid macr
c9e50 6f 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 74 68  o resolves to th
c9e60 65 20 74 68 72 65 61 64 2d 69 64 20 6f 72 20 74  e thread-id or t
c9e70 6f 20 30 2e 20 20 55 73 65 64 20 66 6f 72 0a 2a  o 0.  Used for.*
c9e80 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64 65  * testing and de
c9e90 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f  bugging only..*/
c9ea0 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
c9eb0 41 44 53 41 46 45 0a 23 64 65 66 69 6e 65 20 74  ADSAFE.#define t
c9ec0 68 72 65 61 64 69 64 20 70 74 68 72 65 61 64 5f  hreadid pthread_
c9ed0 73 65 6c 66 28 29 0a 23 65 6c 73 65 0a 23 64 65  self().#else.#de
c9ee0 66 69 6e 65 20 74 68 72 65 61 64 69 64 20 30 0a  fine threadid 0.
c9ef0 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 48  #endif.../*.** H
c9f00 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 73 20  elper functions 
c9f10 74 6f 20 6f 62 74 61 69 6e 20 61 6e 64 20 72 65  to obtain and re
c9f20 6c 69 6e 71 75 69 73 68 20 74 68 65 20 67 6c 6f  linquish the glo
c9f30 62 61 6c 20 6d 75 74 65 78 2e 20 54 68 65 0a 2a  bal mutex. The.*
c9f40 2a 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 69  * global mutex i
c9f50 73 20 75 73 65 64 20 74 6f 20 70 72 6f 74 65 63  s used to protec
c9f60 74 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e  t the unixOpenCn
c9f70 74 2c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20  t, unixLockInfo 
c9f80 61 6e 64 0a 2a 2a 20 76 78 77 6f 72 6b 73 46 69  and.** vxworksFi
c9f90 6c 65 49 64 20 6f 62 6a 65 63 74 73 20 75 73 65  leId objects use
c9fa0 64 20 62 79 20 74 68 69 73 20 66 69 6c 65 2c 20  d by this file, 
c9fb0 61 6c 6c 20 6f 66 20 77 68 69 63 68 20 6d 61 79  all of which may
c9fc0 20 62 65 20 0a 2a 2a 20 73 68 61 72 65 64 20 62   be .** shared b
c9fd0 79 20 6d 75 6c 74 69 70 6c 65 20 74 68 72 65 61  y multiple threa
c9fe0 64 73 2e 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69  ds..**.** Functi
c9ff0 6f 6e 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64  on unixMutexHeld
ca000 28 29 20 69 73 20 75 73 65 64 20 74 6f 20 61 73  () is used to as
ca010 73 65 72 74 28 29 20 74 68 61 74 20 74 68 65 20  sert() that the 
ca020 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 0a 2a 2a  global mutex .**
ca030 20 69 73 20 68 65 6c 64 20 77 68 65 6e 20 72 65   is held when re
ca040 71 75 69 72 65 64 2e 20 54 68 69 73 20 66 75 6e  quired. This fun
ca050 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73  ction is only us
ca060 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61 73  ed as part of as
ca070 73 65 72 74 28 29 20 0a 2a 2a 20 73 74 61 74 65  sert() .** state
ca080 6d 65 6e 74 73 2e 20 65 2e 67 2e 0a 2a 2a 0a 2a  ments. e.g..**.*
ca090 2a 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74  *   unixEnterMut
ca0a0 65 78 28 29 0a 2a 2a 20 20 20 20 20 61 73 73 65  ex().**     asse
ca0b0 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48 65 6c  rt( unixMutexHel
ca0c0 64 28 29 20 29 3b 0a 2a 2a 20 20 20 75 6e 69 78  d() );.**   unix
ca0d0 45 6e 74 65 72 4c 65 61 76 65 28 29 0a 2a 2f 0a  EnterLeave().*/.
ca0e0 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78  static void unix
ca0f0 45 6e 74 65 72 4d 75 74 65 78 28 76 6f 69 64 29  EnterMutex(void)
ca100 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
ca110 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d  x_enter(sqlite3M
ca120 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
ca130 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
ca140 53 54 45 52 29 29 3b 0a 7d 0a 73 74 61 74 69 63  STER));.}.static
ca150 20 76 6f 69 64 20 75 6e 69 78 4c 65 61 76 65 4d   void unixLeaveM
ca160 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 71  utex(void){.  sq
ca170 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
ca180 65 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c  e(sqlite3MutexAl
ca190 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
ca1a0 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29  _STATIC_MASTER))
ca1b0 3b 0a 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ;.}.#ifdef SQLIT
ca1c0 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63 20 69  E_DEBUG.static i
ca1d0 6e 74 20 75 6e 69 78 4d 75 74 65 78 48 65 6c 64  nt unixMutexHeld
ca1e0 28 76 6f 69 64 29 20 7b 0a 20 20 72 65 74 75 72  (void) {.  retur
ca1f0 6e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  n sqlite3_mutex_
ca200 68 65 6c 64 28 73 71 6c 69 74 65 33 4d 75 74 65  held(sqlite3Mute
ca210 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
ca220 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
ca230 52 29 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a  R));.}.#endif...
ca240 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
ca250 42 55 47 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72  BUG./*.** Helper
ca260 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 70 72   function for pr
ca270 69 6e 74 69 6e 67 20 6f 75 74 20 74 72 61 63 65  inting out trace
ca280 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f   information fro
ca290 6d 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a 20 62  m debugging.** b
ca2a0 69 6e 61 72 69 65 73 2e 20 54 68 69 73 20 72 65  inaries. This re
ca2b0 74 75 72 6e 73 20 74 68 65 20 73 74 72 69 6e 67  turns the string
ca2c0 20 72 65 70 72 65 73 65 74 61 74 69 6f 6e 20 6f   represetation o
ca2d0 66 20 74 68 65 20 73 75 70 70 6c 69 65 64 0a 2a  f the supplied.*
ca2e0 2a 20 69 6e 74 65 67 65 72 20 6c 6f 63 6b 2d 74  * integer lock-t
ca2f0 79 70 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ype..*/.static c
ca300 6f 6e 73 74 20 63 68 61 72 20 2a 6c 6f 63 6b 74  onst char *lockt
ca310 79 70 65 4e 61 6d 65 28 69 6e 74 20 6c 6f 63 6b  ypeName(int lock
ca320 74 79 70 65 29 7b 0a 20 20 73 77 69 74 63 68 28  type){.  switch(
ca330 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20   locktype ){.   
ca340 20 63 61 73 65 20 4e 4f 5f 4c 4f 43 4b 3a 20 72   case NO_LOCK: r
ca350 65 74 75 72 6e 20 22 4e 4f 4e 45 22 3b 0a 20 20  eturn "NONE";.  
ca360 20 20 63 61 73 65 20 53 48 41 52 45 44 5f 4c 4f    case SHARED_LO
ca370 43 4b 3a 20 72 65 74 75 72 6e 20 22 53 48 41 52  CK: return "SHAR
ca380 45 44 22 3b 0a 20 20 20 20 63 61 73 65 20 52 45  ED";.    case RE
ca390 53 45 52 56 45 44 5f 4c 4f 43 4b 3a 20 72 65 74  SERVED_LOCK: ret
ca3a0 75 72 6e 20 22 52 45 53 45 52 56 45 44 22 3b 0a  urn "RESERVED";.
ca3b0 20 20 20 20 63 61 73 65 20 50 45 4e 44 49 4e 47      case PENDING
ca3c0 5f 4c 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 50  _LOCK: return "P
ca3d0 45 4e 44 49 4e 47 22 3b 0a 20 20 20 20 63 61 73  ENDING";.    cas
ca3e0 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  e EXCLUSIVE_LOCK
ca3f0 3a 20 72 65 74 75 72 6e 20 22 45 58 43 4c 55 53  : return "EXCLUS
ca400 49 56 45 22 3b 0a 20 20 7d 0a 20 20 72 65 74 75  IVE";.  }.  retu
ca410 72 6e 20 22 45 52 52 4f 52 22 3b 0a 7d 0a 23 65  rn "ERROR";.}.#e
ca420 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
ca430 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a 2f  ITE_LOCK_TRACE./
ca440 2a 0a 2a 2a 20 50 72 69 6e 74 20 6f 75 74 20 69  *.** Print out i
ca450 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
ca460 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20 6f 70 65   all locking ope
ca470 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54  rations..**.** T
ca480 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
ca490 73 65 64 20 66 6f 72 20 74 72 6f 75 62 6c 65 73  sed for troubles
ca4a0 68 6f 6f 74 69 6e 67 20 6c 6f 63 6b 73 20 6f 6e  hooting locks on
ca4b0 20 6d 75 6c 74 69 74 68 72 65 61 64 65 64 0a 2a   multithreaded.*
ca4c0 2a 20 70 6c 61 74 66 6f 72 6d 73 2e 20 20 45 6e  * platforms.  En
ca4d0 61 62 6c 65 20 62 79 20 63 6f 6d 70 69 6c 69 6e  able by compilin
ca4e0 67 20 77 69 74 68 20 74 68 65 20 2d 44 53 51 4c  g with the -DSQL
ca4f0 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43 45 0a 2a  ITE_LOCK_TRACE.*
ca500 2a 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 6f  * command-line o
ca510 70 74 69 6f 6e 20 6f 6e 20 74 68 65 20 63 6f 6d  ption on the com
ca520 70 69 6c 65 72 2e 20 20 54 68 69 73 20 63 6f 64  piler.  This cod
ca530 65 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 0a 2a 2a  e is normally.**
ca540 20 74 75 72 6e 65 64 20 6f 66 66 2e 0a 2a 2f 0a   turned off..*/.
ca550 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 54  static int lockT
ca560 72 61 63 65 28 69 6e 74 20 66 64 2c 20 69 6e 74  race(int fd, int
ca570 20 6f 70 2c 20 73 74 72 75 63 74 20 66 6c 6f 63   op, struct floc
ca580 6b 20 2a 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a  k *p){.  char *z
ca590 4f 70 4e 61 6d 65 2c 20 2a 7a 54 79 70 65 3b 0a  OpName, *zType;.
ca5a0 20 20 69 6e 74 20 73 3b 0a 20 20 69 6e 74 20 73    int s;.  int s
ca5b0 61 76 65 64 45 72 72 6e 6f 3b 0a 20 20 69 66 28  avedErrno;.  if(
ca5c0 20 6f 70 3d 3d 46 5f 47 45 54 4c 4b 20 29 7b 0a   op==F_GETLK ){.
ca5d0 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d 20 22 47      zOpName = "G
ca5e0 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69  ETLK";.  }else i
ca5f0 66 28 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 29  f( op==F_SETLK )
ca600 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d 20  {.    zOpName = 
ca610 22 53 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73 65  "SETLK";.  }else
ca620 7b 0a 20 20 20 20 73 20 3d 20 66 63 6e 74 6c 28  {.    s = fcntl(
ca630 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20 20 20 20  fd, op, p);.    
ca640 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
ca650 74 66 28 22 66 63 6e 74 6c 20 75 6e 6b 6e 6f 77  tf("fcntl unknow
ca660 6e 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 66  n %d %d %d\n", f
ca670 64 2c 20 6f 70 2c 20 73 29 3b 0a 20 20 20 20 72  d, op, s);.    r
ca680 65 74 75 72 6e 20 73 3b 0a 20 20 7d 0a 20 20 69  eturn s;.  }.  i
ca690 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f  f( p->l_type==F_
ca6a0 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79  RDLCK ){.    zTy
ca6b0 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b 0a 20 20  pe = "RDLCK";.  
ca6c0 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 6c 5f 74  }else if( p->l_t
ca6d0 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20 29 7b 0a  ype==F_WRLCK ){.
ca6e0 20 20 20 20 7a 54 79 70 65 20 3d 20 22 57 52 4c      zType = "WRL
ca6f0 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  CK";.  }else if(
ca700 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 55 4e   p->l_type==F_UN
ca710 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54 79 70 65  LCK ){.    zType
ca720 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20 20 7d 65   = "UNLCK";.  }e
ca730 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
ca740 20 30 20 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65   0 );.  }.  asse
ca750 72 74 28 20 70 2d 3e 6c 5f 77 68 65 6e 63 65 3d  rt( p->l_whence=
ca760 3d 53 45 45 4b 5f 53 45 54 20 29 3b 0a 20 20 73  =SEEK_SET );.  s
ca770 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20 6f 70 2c   = fcntl(fd, op,
ca780 20 70 29 3b 0a 20 20 73 61 76 65 64 45 72 72 6e   p);.  savedErrn
ca790 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 73 71 6c  o = errno;.  sql
ca7a0 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
ca7b0 22 66 63 6e 74 6c 20 25 64 20 25 64 20 25 73 20  "fcntl %d %d %s 
ca7c0 25 73 20 25 64 20 25 64 20 25 64 20 25 64 5c 6e  %s %d %d %d %d\n
ca7d0 22 2c 0a 20 20 20 20 20 74 68 72 65 61 64 69 64  ",.     threadid
ca7e0 2c 20 66 64 2c 20 7a 4f 70 4e 61 6d 65 2c 20 7a  , fd, zOpName, z
ca7f0 54 79 70 65 2c 20 28 69 6e 74 29 70 2d 3e 6c 5f  Type, (int)p->l_
ca800 73 74 61 72 74 2c 20 28 69 6e 74 29 70 2d 3e 6c  start, (int)p->l
ca810 5f 6c 65 6e 2c 0a 20 20 20 20 20 28 69 6e 74 29  _len,.     (int)
ca820 70 2d 3e 6c 5f 70 69 64 2c 20 73 29 3b 0a 20 20  p->l_pid, s);.  
ca830 69 66 28 20 73 3d 3d 28 2d 31 29 20 26 26 20 6f  if( s==(-1) && o
ca840 70 3d 3d 46 5f 53 45 54 4c 4b 20 26 26 20 28 70  p==F_SETLK && (p
ca850 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52 44 4c 43  ->l_type==F_RDLC
ca860 4b 20 7c 7c 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d  K || p->l_type==
ca870 46 5f 57 52 4c 43 4b 29 20 29 7b 0a 20 20 20 20  F_WRLCK) ){.    
ca880 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c 32 3b  struct flock l2;
ca890 0a 20 20 20 20 6c 32 20 3d 20 2a 70 3b 0a 20 20  .    l2 = *p;.  
ca8a0 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47 45    fcntl(fd, F_GE
ca8b0 54 4c 4b 2c 20 26 6c 32 29 3b 0a 20 20 20 20 69  TLK, &l2);.    i
ca8c0 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f  f( l2.l_type==F_
ca8d0 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a  RDLCK ){.      z
ca8e0 54 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b 0a  Type = "RDLCK";.
ca8f0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 32      }else if( l2
ca900 2e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b  .l_type==F_WRLCK
ca910 20 29 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20   ){.      zType 
ca920 3d 20 22 57 52 4c 43 4b 22 3b 0a 20 20 20 20 7d  = "WRLCK";.    }
ca930 65 6c 73 65 20 69 66 28 20 6c 32 2e 6c 5f 74 79  else if( l2.l_ty
ca940 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20  pe==F_UNLCK ){. 
ca950 20 20 20 20 20 7a 54 79 70 65 20 3d 20 22 55 4e       zType = "UN
ca960 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  LCK";.    }else{
ca970 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30  .      assert( 0
ca980 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71   );.    }.    sq
ca990 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
ca9a0 28 22 66 63 6e 74 6c 2d 66 61 69 6c 75 72 65 2d  ("fcntl-failure-
ca9b0 72 65 61 73 6f 6e 3a 20 25 73 20 25 64 20 25 64  reason: %s %d %d
ca9c0 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20 20 7a   %d\n",.       z
ca9d0 54 79 70 65 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f  Type, (int)l2.l_
ca9e0 73 74 61 72 74 2c 20 28 69 6e 74 29 6c 32 2e 6c  start, (int)l2.l
ca9f0 5f 6c 65 6e 2c 20 28 69 6e 74 29 6c 32 2e 6c 5f  _len, (int)l2.l_
caa00 70 69 64 29 3b 0a 20 20 7d 0a 20 20 65 72 72 6e  pid);.  }.  errn
caa10 6f 20 3d 20 73 61 76 65 64 45 72 72 6e 6f 3b 0a  o = savedErrno;.
caa20 20 20 72 65 74 75 72 6e 20 73 3b 0a 7d 0a 23 64    return s;.}.#d
caa30 65 66 69 6e 65 20 66 63 6e 74 6c 20 6c 6f 63 6b  efine fcntl lock
caa40 54 72 61 63 65 0a 23 65 6e 64 69 66 20 2f 2a 20  Trace.#endif /* 
caa50 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43  SQLITE_LOCK_TRAC
caa60 45 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  E */..../*.** Th
caa70 69 73 20 72 6f 75 74 69 6e 65 20 74 72 61 6e 73  is routine trans
caa80 6c 61 74 65 73 20 61 20 73 74 61 6e 64 61 72 64  lates a standard
caa90 20 50 4f 53 49 58 20 65 72 72 6e 6f 20 63 6f 64   POSIX errno cod
caaa0 65 20 69 6e 74 6f 20 73 6f 6d 65 74 68 69 6e 67  e into something
caab0 0a 2a 2a 20 75 73 65 66 75 6c 20 74 6f 20 74 68  .** useful to th
caac0 65 20 63 6c 69 65 6e 74 73 20 6f 66 20 74 68 65  e clients of the
caad0 20 73 71 6c 69 74 65 33 20 66 75 6e 63 74 69 6f   sqlite3 functio
caae0 6e 73 2e 20 20 53 70 65 63 69 66 69 63 61 6c 6c  ns.  Specificall
caaf0 79 2c 20 69 74 20 69 73 0a 2a 2a 20 69 6e 74 65  y, it is.** inte
cab00 6e 64 65 64 20 74 6f 20 74 72 61 6e 73 6c 61 74  nded to translat
cab10 65 20 61 20 76 61 72 69 65 74 79 20 6f 66 20 22  e a variety of "
cab20 74 72 79 20 61 67 61 69 6e 22 20 65 72 72 6f 72  try again" error
cab30 73 20 69 6e 74 6f 20 53 51 4c 49 54 45 5f 42 55  s into SQLITE_BU
cab40 53 59 0a 2a 2a 20 61 6e 64 20 61 20 76 61 72 69  SY.** and a vari
cab50 65 74 79 20 6f 66 20 22 70 6c 65 61 73 65 20 63  ety of "please c
cab60 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 64 65  lose the file de
cab70 73 63 72 69 70 74 6f 72 20 4e 4f 57 22 20 65 72  scriptor NOW" er
cab80 72 6f 72 73 20 69 6e 74 6f 20 0a 2a 2a 20 53 51  rors into .** SQ
cab90 4c 49 54 45 5f 49 4f 45 52 52 0a 2a 2a 20 0a 2a  LITE_IOERR.** .*
caba0 2a 20 45 72 72 6f 72 73 20 64 75 72 69 6e 67 20  * Errors during 
cabb0 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f  initialization o
cabc0 66 20 6c 6f 63 6b 73 2c 20 6f 72 20 66 69 6c 65  f locks, or file
cabd0 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74 20   system support 
cabe0 66 6f 72 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 73 68  for locks,.** sh
cabf0 6f 75 6c 64 20 68 61 6e 64 6c 65 20 45 4e 4f 4c  ould handle ENOL
cac00 43 4b 2c 20 45 4e 4f 54 53 55 50 2c 20 45 4f 50  CK, ENOTSUP, EOP
cac10 4e 4f 54 53 55 50 50 20 73 65 70 61 72 61 74 65  NOTSUPP separate
cac20 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ly..*/.static in
cac30 74 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f  t sqliteErrorFro
cac40 6d 50 6f 73 69 78 45 72 72 6f 72 28 69 6e 74 20  mPosixError(int 
cac50 70 6f 73 69 78 45 72 72 6f 72 2c 20 69 6e 74 20  posixError, int 
cac60 73 71 6c 69 74 65 49 4f 45 72 72 29 20 7b 0a 20  sqliteIOErr) {. 
cac70 20 73 77 69 74 63 68 20 28 70 6f 73 69 78 45 72   switch (posixEr
cac80 72 6f 72 29 20 7b 0a 20 20 63 61 73 65 20 30 3a  ror) {.  case 0:
cac90 20 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c   .    return SQL
caca0 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20 20 63  ITE_OK;.    .  c
cacb0 61 73 65 20 45 41 47 41 49 4e 3a 0a 20 20 63 61  ase EAGAIN:.  ca
cacc0 73 65 20 45 54 49 4d 45 44 4f 55 54 3a 0a 20 20  se ETIMEDOUT:.  
cacd0 63 61 73 65 20 45 42 55 53 59 3a 0a 20 20 63 61  case EBUSY:.  ca
cace0 73 65 20 45 49 4e 54 52 3a 0a 20 20 63 61 73 65  se EINTR:.  case
cacf0 20 45 4e 4f 4c 43 4b 3a 20 20 0a 20 20 20 20 2f   ENOLCK:  .    /
cad00 2a 20 72 61 6e 64 6f 6d 20 4e 46 53 20 72 65 74  * random NFS ret
cad10 72 79 20 65 72 72 6f 72 2c 20 75 6e 6c 65 73 73  ry error, unless
cad20 20 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79 73   during file sys
cad30 74 65 6d 20 73 75 70 70 6f 72 74 20 0a 20 20 20  tem support .   
cad40 20 20 2a 20 69 6e 74 72 6f 73 70 65 63 74 69 6f    * introspectio
cad50 6e 2c 20 69 6e 20 77 68 69 63 68 20 69 74 20 61  n, in which it a
cad60 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77 68  ctually means wh
cad70 61 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 20 20  at it says */.  
cad80 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
cad90 42 55 53 59 3b 0a 20 20 20 20 0a 20 20 63 61 73  BUSY;.    .  cas
cada0 65 20 45 41 43 43 45 53 3a 20 0a 20 20 20 20 2f  e EACCES: .    /
cadb0 2a 20 45 41 43 43 45 53 20 69 73 20 6c 69 6b 65  * EACCES is like
cadc0 20 45 41 47 41 49 4e 20 64 75 72 69 6e 67 20 6c   EAGAIN during l
cadd0 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e  ocking operation
cade0 73 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f  s, but not any o
cadf0 74 68 65 72 20 74 69 6d 65 2a 2f 0a 20 20 20 20  ther time*/.    
cae00 69 66 28 20 28 73 71 6c 69 74 65 49 4f 45 72 72  if( (sqliteIOErr
cae10 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52   == SQLITE_IOERR
cae20 5f 4c 4f 43 4b 29 20 7c 7c 20 0a 09 28 73 71 6c  _LOCK) || ..(sql
cae30 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c 49  iteIOErr == SQLI
cae40 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29  TE_IOERR_UNLOCK)
cae50 20 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49 4f 45   || ..(sqliteIOE
cae60 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45  rr == SQLITE_IOE
cae70 52 52 5f 52 44 4c 4f 43 4b 29 20 7c 7c 0a 09 28  RR_RDLOCK) ||..(
cae80 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53  sqliteIOErr == S
cae90 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43  QLITE_IOERR_CHEC
caea0 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 20 29  KRESERVEDLOCK) )
caeb0 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
caec0 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
caed0 7d 0a 20 20 20 20 2f 2a 20 65 6c 73 65 20 66 61  }.    /* else fa
caee0 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20 20  ll through */.  
caef0 63 61 73 65 20 45 50 45 52 4d 3a 20 0a 20 20 20  case EPERM: .   
caf00 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 50   return SQLITE_P
caf10 45 52 4d 3b 0a 20 20 20 20 0a 20 20 63 61 73 65  ERM;.    .  case
caf20 20 45 44 45 41 44 4c 4b 3a 0a 20 20 20 20 72 65   EDEADLK:.    re
caf30 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
caf40 52 5f 42 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 0a  R_BLOCKED;.    .
caf50 23 69 66 20 45 4f 50 4e 4f 54 53 55 50 50 21 3d  #if EOPNOTSUPP!=
caf60 45 4e 4f 54 53 55 50 0a 20 20 63 61 73 65 20 45  ENOTSUP.  case E
caf70 4f 50 4e 4f 54 53 55 50 50 3a 20 0a 20 20 20 20  OPNOTSUPP: .    
caf80 2f 2a 20 73 6f 6d 65 74 68 69 6e 67 20 77 65 6e  /* something wen
caf90 74 20 74 65 72 72 69 62 6c 79 20 61 77 72 79 2c  t terribly awry,
cafa0 20 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 66   unless during f
cafb0 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f  ile system suppo
cafc0 72 74 20 0a 20 20 20 20 20 2a 20 69 6e 74 72 6f  rt .     * intro
cafd0 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68 69  spection, in whi
cafe0 63 68 20 69 74 20 61 63 74 75 61 6c 6c 79 20 6d  ch it actually m
caff0 65 61 6e 73 20 77 68 61 74 20 69 74 20 73 61 79  eans what it say
cb000 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64  s */.#endif.#ifd
cb010 65 66 20 45 4e 4f 54 53 55 50 0a 20 20 63 61 73  ef ENOTSUP.  cas
cb020 65 20 45 4e 4f 54 53 55 50 3a 20 0a 20 20 20 20  e ENOTSUP: .    
cb030 2f 2a 20 69 6e 76 61 6c 69 64 20 66 64 2c 20 75  /* invalid fd, u
cb040 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69 6c  nless during fil
cb050 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74  e system support
cb060 20 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c 20   introspection, 
cb070 69 6e 20 77 68 69 63 68 20 0a 20 20 20 20 20 2a  in which .     *
cb080 20 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65 61   it actually mea
cb090 6e 73 20 77 68 61 74 20 69 74 20 73 61 79 73 20  ns what it says 
cb0a0 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 61 73 65  */.#endif.  case
cb0b0 20 45 49 4f 3a 0a 20 20 63 61 73 65 20 45 42 41   EIO:.  case EBA
cb0c0 44 46 3a 0a 20 20 63 61 73 65 20 45 49 4e 56 41  DF:.  case EINVA
cb0d0 4c 3a 0a 20 20 63 61 73 65 20 45 4e 4f 54 43 4f  L:.  case ENOTCO
cb0e0 4e 4e 3a 0a 20 20 63 61 73 65 20 45 4e 4f 44 45  NN:.  case ENODE
cb0f0 56 3a 0a 20 20 63 61 73 65 20 45 4e 58 49 4f 3a  V:.  case ENXIO:
cb100 0a 20 20 63 61 73 65 20 45 4e 4f 45 4e 54 3a 0a  .  case ENOENT:.
cb110 20 20 63 61 73 65 20 45 53 54 41 4c 45 3a 0a 20    case ESTALE:. 
cb120 20 63 61 73 65 20 45 4e 4f 53 59 53 3a 0a 20 20   case ENOSYS:.  
cb130 20 20 2f 2a 20 74 68 65 73 65 20 73 68 6f 75 6c    /* these shoul
cb140 64 20 66 6f 72 63 65 20 74 68 65 20 63 6c 69 65  d force the clie
cb150 6e 74 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20  nt to close the 
cb160 66 69 6c 65 20 61 6e 64 20 72 65 63 6f 6e 6e 65  file and reconne
cb170 63 74 20 2a 2f 0a 20 20 20 20 0a 20 20 64 65 66  ct */.    .  def
cb180 61 75 6c 74 3a 20 0a 20 20 20 20 72 65 74 75 72  ault: .    retur
cb190 6e 20 73 71 6c 69 74 65 49 4f 45 72 72 3b 0a 20  n sqliteIOErr;. 
cb1a0 20 7d 0a 7d 0a 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a   }.}..../*******
cb1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb1c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cb1f0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
cb200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
cb210 20 55 6e 69 71 75 65 20 46 69 6c 65 20 49 44 20   Unique File ID 
cb220 55 74 69 6c 69 74 79 20 55 73 65 64 20 42 79 20  Utility Used By 
cb230 56 78 57 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a  VxWorks ********
cb240 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4f 6e  *******.**.** On
cb250 20 6d 6f 73 74 20 76 65 72 73 69 6f 6e 73 20 6f   most versions o
cb260 66 20 75 6e 69 78 2c 20 77 65 20 63 61 6e 20 67  f unix, we can g
cb270 65 74 20 61 20 75 6e 69 71 75 65 20 49 44 20 66  et a unique ID f
cb280 6f 72 20 61 20 66 69 6c 65 20 62 79 20 63 6f 6e  or a file by con
cb290 63 61 74 65 6e 61 74 69 6e 67 0a 2a 2a 20 74 68  catenating.** th
cb2a0 65 20 64 65 76 69 63 65 20 6e 75 6d 62 65 72 20  e device number 
cb2b0 61 6e 64 20 74 68 65 20 69 6e 6f 64 65 20 6e 75  and the inode nu
cb2c0 6d 62 65 72 2e 20 20 42 75 74 20 74 68 69 73 20  mber.  But this 
cb2d0 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f 6e  does not work on
cb2e0 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20 4f 6e 20   VxWorks..** On 
cb2f0 56 78 57 6f 72 6b 73 2c 20 61 20 75 6e 69 71 75  VxWorks, a uniqu
cb300 65 20 66 69 6c 65 20 69 64 20 6d 75 73 74 20 62  e file id must b
cb310 65 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 63  e based on the c
cb320 61 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d  anonical filenam
cb330 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74  e..**.** A point
cb340 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63  er to an instanc
cb350 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
cb360 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 61 6e  ng structure can
cb370 20 62 65 20 75 73 65 64 20 61 73 20 61 0a 2a 2a   be used as a.**
cb380 20 75 6e 69 71 75 65 20 66 69 6c 65 20 49 44 20   unique file ID 
cb390 69 6e 20 56 78 57 6f 72 6b 73 2e 20 20 45 61 63  in VxWorks.  Eac
cb3a0 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  h instance of th
cb3b0 69 73 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  is structure con
cb3c0 74 61 69 6e 73 0a 2a 2a 20 61 20 63 6f 70 79 20  tains.** a copy 
cb3d0 6f 66 20 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c  of the canonical
cb3e0 20 66 69 6c 65 6e 61 6d 65 2e 20 20 54 68 65 72   filename.  Ther
cb3f0 65 20 69 73 20 61 6c 73 6f 20 61 20 72 65 66 65  e is also a refe
cb400 72 65 6e 63 65 20 63 6f 75 6e 74 2e 20 20 0a 2a  rence count.  .*
cb410 2a 20 54 68 65 20 73 74 72 75 63 74 75 72 65 20  * The structure 
cb420 69 73 20 72 65 63 6c 61 69 6d 65 64 20 77 68 65  is reclaimed whe
cb430 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
cb440 70 6f 69 6e 74 65 72 73 20 74 6f 20 69 74 20 64  pointers to it d
cb450 72 6f 70 73 20 74 6f 0a 2a 2a 20 7a 65 72 6f 2e  rops to.** zero.
cb460 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  .**.** There are
cb470 20 6e 65 76 65 72 20 76 65 72 79 20 6d 61 6e 79   never very many
cb480 20 66 69 6c 65 73 20 6f 70 65 6e 20 61 74 20 6f   files open at o
cb490 6e 65 20 74 69 6d 65 20 61 6e 64 20 6c 6f 6f 6b  ne time and look
cb4a0 75 70 73 20 61 72 65 20 6e 6f 74 0a 2a 2a 20 61  ups are not.** a
cb4b0 20 70 65 72 66 6f 72 6d 61 6e 63 65 2d 63 72 69   performance-cri
cb4c0 74 69 63 61 6c 20 70 61 74 68 2c 20 73 6f 20 69  tical path, so i
cb4d0 74 20 69 73 20 73 75 66 66 69 63 69 65 6e 74 20  t is sufficient 
cb4e0 74 6f 20 70 75 74 20 74 68 65 73 65 0a 2a 2a 20  to put these.** 
cb4f0 73 74 72 75 63 74 75 72 65 73 20 6f 6e 20 61 20  structures on a 
cb500 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a  linked list..*/.
cb510 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69  struct vxworksFi
cb520 6c 65 49 64 20 7b 0a 20 20 73 74 72 75 63 74 20  leId {.  struct 
cb530 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70  vxworksFileId *p
cb540 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 69  Next;  /* Next i
cb550 6e 20 61 20 6c 69 73 74 20 6f 66 20 74 68 65 6d  n a list of them
cb560 20 61 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 52   all */.  int nR
cb570 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ef;             
cb580 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
cb590 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20  r of references 
cb5a0 74 6f 20 74 68 69 73 20 6f 6e 65 20 2a 2f 0a 20  to this one */. 
cb5b0 20 69 6e 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20   int nName;     
cb5c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
cb5d0 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20  * Length of the 
cb5e0 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 5b 5d  zCanonicalName[]
cb5f0 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 63 68 61   string */.  cha
cb600 72 20 2a 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d  r *zCanonicalNam
cb610 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61  e;         /* Ca
cb620 6e 6f 6e 69 63 61 6c 20 66 69 6c 65 6e 61 6d 65  nonical filename
cb630 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 20 4f 53 5f 56   */.};..#if OS_V
cb640 58 57 4f 52 4b 53 0a 2f 2a 20 0a 2a 2a 20 41 6c  XWORKS./* .** Al
cb650 6c 20 75 6e 69 71 75 65 20 66 69 6c 65 6e 61 6d  l unique filenam
cb660 65 73 20 61 72 65 20 68 65 6c 64 20 6f 6e 20 61  es are held on a
cb670 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68 65 61   linked list hea
cb680 64 65 64 20 62 79 20 74 68 69 73 0a 2a 2a 20 76  ded by this.** v
cb690 61 72 69 61 62 6c 65 3a 0a 2a 2f 0a 73 74 61 74  ariable:.*/.stat
cb6a0 69 63 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b  ic struct vxwork
cb6b0 73 46 69 6c 65 49 64 20 2a 76 78 77 6f 72 6b 73  sFileId *vxworks
cb6c0 46 69 6c 65 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f  FileList = 0;../
cb6d0 2a 0a 2a 2a 20 53 69 6d 70 6c 69 66 79 20 61 20  *.** Simplify a 
cb6e0 66 69 6c 65 6e 61 6d 65 20 69 6e 74 6f 20 69 74  filename into it
cb6f0 73 20 63 61 6e 6f 6e 69 63 61 6c 20 66 6f 72 6d  s canonical form
cb700 0a 2a 2a 20 62 79 20 6d 61 6b 69 6e 67 20 74 68  .** by making th
cb710 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 6e  e following chan
cb720 67 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 2a 20 72 65  ges:.**.**  * re
cb730 6d 6f 76 69 6e 67 20 61 6e 79 20 74 72 61 69 6c  moving any trail
cb740 69 6e 67 20 61 6e 64 20 64 75 70 6c 69 63 61 74  ing and duplicat
cb750 65 20 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65 72  e /.**  * conver
cb760 74 20 2f 2e 2f 20 69 6e 74 6f 20 6a 75 73 74 20  t /./ into just 
cb770 2f 0a 2a 2a 20 20 2a 20 63 6f 6e 76 65 72 74 20  /.**  * convert 
cb780 2f 41 2f 2e 2e 2f 20 77 68 65 72 65 20 41 20 69  /A/../ where A i
cb790 73 20 61 6e 79 20 73 69 6d 70 6c 65 20 6e 61 6d  s any simple nam
cb7a0 65 20 69 6e 74 6f 20 6a 75 73 74 20 2f 0a 2a 2a  e into just /.**
cb7b0 0a 2a 2a 20 43 68 61 6e 67 65 73 20 61 72 65 20  .** Changes are 
cb7c0 6d 61 64 65 20 69 6e 2d 70 6c 61 63 65 2e 20 20  made in-place.  
cb7d0 52 65 74 75 72 6e 20 74 68 65 20 6e 65 77 20 6e  Return the new n
cb7e0 61 6d 65 20 6c 65 6e 67 74 68 2e 0a 2a 2a 0a 2a  ame length..**.*
cb7f0 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 66  * The original f
cb800 69 6c 65 6e 61 6d 65 20 69 73 20 69 6e 20 7a 5b  ilename is in z[
cb810 30 2e 2e 6e 2d 31 5d 2e 20 20 52 65 74 75 72 6e  0..n-1].  Return
cb820 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
cb830 2a 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20  * characters in 
cb840 74 68 65 20 73 69 6d 70 6c 69 66 69 65 64 20 6e  the simplified n
cb850 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ame..*/.static i
cb860 6e 74 20 76 78 77 6f 72 6b 73 53 69 6d 70 6c 69  nt vxworksSimpli
cb870 66 79 4e 61 6d 65 28 63 68 61 72 20 2a 7a 2c 20  fyName(char *z, 
cb880 69 6e 74 20 6e 29 7b 0a 20 20 69 6e 74 20 69 2c  int n){.  int i,
cb890 20 6a 3b 0a 20 20 77 68 69 6c 65 28 20 6e 3e 31   j;.  while( n>1
cb8a0 20 26 26 20 7a 5b 6e 2d 31 5d 3d 3d 27 2f 27 20   && z[n-1]=='/' 
cb8b0 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 66 6f 72 28  ){ n--; }.  for(
cb8c0 69 3d 6a 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29  i=j=0; i<n; i++)
cb8d0 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d  {.    if( z[i]==
cb8e0 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 69 66 28  '/' ){.      if(
cb8f0 20 7a 5b 69 2b 31 5d 3d 3d 27 2f 27 20 29 20 63   z[i+1]=='/' ) c
cb900 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 69  ontinue;.      i
cb910 66 28 20 7a 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26  f( z[i+1]=='.' &
cb920 26 20 69 2b 32 3c 6e 20 26 26 20 7a 5b 69 2b 32  & i+2<n && z[i+2
cb930 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20  ]=='/' ){.      
cb940 20 20 69 20 2b 3d 20 31 3b 0a 20 20 20 20 20 20    i += 1;.      
cb950 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
cb960 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 7a 5b    }.      if( z[
cb970 69 2b 31 5d 3d 3d 27 2e 27 20 26 26 20 69 2b 33  i+1]=='.' && i+3
cb980 3c 6e 20 26 26 20 7a 5b 69 2b 32 5d 3d 3d 27 2e  <n && z[i+2]=='.
cb990 27 20 26 26 20 7a 5b 69 2b 33 5d 3d 3d 27 2f 27  ' && z[i+3]=='/'
cb9a0 20 29 7b 0a 20 20 20 20 20 20 20 20 77 68 69 6c   ){.        whil
cb9b0 65 28 20 6a 3e 30 20 26 26 20 7a 5b 6a 2d 31 5d  e( j>0 && z[j-1]
cb9c0 21 3d 27 2f 27 20 29 7b 20 6a 2d 2d 3b 20 7d 0a  !='/' ){ j--; }.
cb9d0 20 20 20 20 20 20 20 20 69 66 28 20 6a 3e 30 20          if( j>0 
cb9e0 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20 20  ){ j--; }.      
cb9f0 20 20 69 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20    i += 2;.      
cba00 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
cba10 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 5b    }.    }.    z[
cba20 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 7d  j++] = z[i];.  }
cba30 0a 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 72  .  z[j] = 0;.  r
cba40 65 74 75 72 6e 20 6a 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn j;.}../*.*
cba50 2a 20 46 69 6e 64 20 61 20 75 6e 69 71 75 65 20  * Find a unique 
cba60 66 69 6c 65 20 49 44 20 66 6f 72 20 74 68 65 20  file ID for the 
cba70 67 69 76 65 6e 20 61 62 73 6f 6c 75 74 65 20 70  given absolute p
cba80 61 74 68 6e 61 6d 65 2e 20 20 52 65 74 75 72 6e  athname.  Return
cba90 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  .** a pointer to
cbaa0 20 74 68 65 20 76 78 77 6f 72 6b 73 46 69 6c 65   the vxworksFile
cbab0 49 64 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73  Id object.  This
cbac0 20 70 6f 69 6e 74 65 72 20 69 73 20 74 68 65 20   pointer is the 
cbad0 75 6e 69 71 75 65 0a 2a 2a 20 66 69 6c 65 20 49  unique.** file I
cbae0 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 52 65  D..**.** The nRe
cbaf0 66 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 76  f field of the v
cbb00 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a  xworksFileId obj
cbb10 65 63 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74  ect is increment
cbb20 65 64 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65  ed before.** the
cbb30 20 6f 62 6a 65 63 74 20 69 73 20 72 65 74 75 72   object is retur
cbb40 6e 65 64 2e 20 20 41 20 6e 65 77 20 76 78 77 6f  ned.  A new vxwo
cbb50 72 6b 73 46 69 6c 65 49 64 20 6f 62 6a 65 63 74  rksFileId object
cbb60 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 61   is created.** a
cbb70 6e 64 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  nd added to the 
cbb80 67 6c 6f 62 61 6c 20 6c 69 73 74 20 69 66 20 6e  global list if n
cbb90 65 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a 20  ecessary..**.** 
cbba0 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  If a memory allo
cbbb0 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63  cation error occ
cbbc0 75 72 73 2c 20 72 65 74 75 72 6e 20 4e 55 4c 4c  urs, return NULL
cbbd0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75  ..*/.static stru
cbbe0 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64  ct vxworksFileId
cbbf0 20 2a 76 78 77 6f 72 6b 73 46 69 6e 64 46 69 6c   *vxworksFindFil
cbc00 65 49 64 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  eId(const char *
cbc10 7a 41 62 73 6f 6c 75 74 65 4e 61 6d 65 29 7b 0a  zAbsoluteName){.
cbc20 20 20 73 74 72 75 63 74 20 76 78 77 6f 72 6b 73    struct vxworks
cbc30 46 69 6c 65 49 64 20 2a 70 4e 65 77 3b 20 20 20  FileId *pNew;   
cbc40 20 20 20 20 20 20 2f 2a 20 73 65 61 72 63 68 20        /* search 
cbc50 6b 65 79 20 61 6e 64 20 6e 65 77 20 66 69 6c 65  key and new file
cbc60 20 49 44 20 2a 2f 0a 20 20 73 74 72 75 63 74 20   ID */.  struct 
cbc70 76 78 77 6f 72 6b 73 46 69 6c 65 49 64 20 2a 70  vxworksFileId *p
cbc80 43 61 6e 64 69 64 61 74 65 3b 20 20 20 2f 2a 20  Candidate;   /* 
cbc90 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72  For looping over
cbca0 20 65 78 69 73 74 69 6e 67 20 66 69 6c 65 20 49   existing file I
cbcb0 44 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20  Ds */.  int n;  
cbcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cbcd0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
cbce0 65 6e 67 74 68 20 6f 66 20 7a 41 62 73 6f 6c 75  ength of zAbsolu
cbcf0 74 65 4e 61 6d 65 20 73 74 72 69 6e 67 20 2a 2f  teName string */
cbd00 0a 0a 20 20 61 73 73 65 72 74 28 20 7a 41 62 73  ..  assert( zAbs
cbd10 6f 6c 75 74 65 4e 61 6d 65 5b 30 5d 3d 3d 27 2f  oluteName[0]=='/
cbd20 27 20 29 3b 0a 20 20 6e 20 3d 20 28 69 6e 74 29  ' );.  n = (int)
cbd30 73 74 72 6c 65 6e 28 7a 41 62 73 6f 6c 75 74 65  strlen(zAbsolute
cbd40 4e 61 6d 65 29 3b 0a 20 20 70 4e 65 77 20 3d 20  Name);.  pNew = 
cbd50 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20  sqlite3_malloc( 
cbd60 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 2b 20  sizeof(*pNew) + 
cbd70 28 6e 2b 31 29 20 29 3b 0a 20 20 69 66 28 20 70  (n+1) );.  if( p
cbd80 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  New==0 ) return 
cbd90 30 3b 0a 20 20 70 4e 65 77 2d 3e 7a 43 61 6e 6f  0;.  pNew->zCano
cbda0 6e 69 63 61 6c 4e 61 6d 65 20 3d 20 28 63 68 61  nicalName = (cha
cbdb0 72 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a 20 20 6d  r*)&pNew[1];.  m
cbdc0 65 6d 63 70 79 28 70 4e 65 77 2d 3e 7a 43 61 6e  emcpy(pNew->zCan
cbdd0 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 7a 41 62 73  onicalName, zAbs
cbde0 6f 6c 75 74 65 4e 61 6d 65 2c 20 6e 2b 31 29 3b  oluteName, n+1);
cbdf0 0a 20 20 6e 20 3d 20 76 78 77 6f 72 6b 73 53 69  .  n = vxworksSi
cbe00 6d 70 6c 69 66 79 4e 61 6d 65 28 70 4e 65 77 2d  mplifyName(pNew-
cbe10 3e 7a 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c  >zCanonicalName,
cbe20 20 6e 29 3b 0a 0a 20 20 2f 2a 20 53 65 61 72 63   n);..  /* Searc
cbe30 68 20 66 6f 72 20 61 6e 20 65 78 69 73 74 69 6e  h for an existin
cbe40 67 20 65 6e 74 72 79 20 74 68 61 74 20 6d 61 74  g entry that mat
cbe50 63 68 69 6e 67 20 74 68 65 20 63 61 6e 6f 6e 69  ching the canoni
cbe60 63 61 6c 20 6e 61 6d 65 2e 0a 20 20 2a 2a 20 49  cal name..  ** I
cbe70 66 20 66 6f 75 6e 64 2c 20 69 6e 63 72 65 6d 65  f found, increme
cbe80 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  nt the reference
cbe90 20 63 6f 75 6e 74 20 61 6e 64 20 72 65 74 75 72   count and retur
cbea0 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 20  n a pointer to. 
cbeb0 20 2a 2a 20 74 68 65 20 65 78 69 73 74 69 6e 67   ** the existing
cbec0 20 66 69 6c 65 20 49 44 2e 0a 20 20 2a 2f 0a 20   file ID..  */. 
cbed0 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
cbee0 29 3b 0a 20 20 66 6f 72 28 70 43 61 6e 64 69 64  );.  for(pCandid
cbef0 61 74 65 3d 76 78 77 6f 72 6b 73 46 69 6c 65 4c  ate=vxworksFileL
cbf00 69 73 74 3b 20 70 43 61 6e 64 69 64 61 74 65 3b  ist; pCandidate;
cbf10 20 70 43 61 6e 64 69 64 61 74 65 3d 70 43 61 6e   pCandidate=pCan
cbf20 64 69 64 61 74 65 2d 3e 70 4e 65 78 74 29 7b 0a  didate->pNext){.
cbf30 20 20 20 20 69 66 28 20 70 43 61 6e 64 69 64 61      if( pCandida
cbf40 74 65 2d 3e 6e 4e 61 6d 65 3d 3d 6e 20 0a 20 20  te->nName==n .  
cbf50 20 20 20 26 26 20 6d 65 6d 63 6d 70 28 70 43 61     && memcmp(pCa
cbf60 6e 64 69 64 61 74 65 2d 3e 7a 43 61 6e 6f 6e 69  ndidate->zCanoni
cbf70 63 61 6c 4e 61 6d 65 2c 20 70 4e 65 77 2d 3e 7a  calName, pNew->z
cbf80 43 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 2c 20 6e  CanonicalName, n
cbf90 29 3d 3d 30 0a 20 20 20 20 29 7b 0a 20 20 20 20  )==0.    ){.    
cbfa0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
cbfb0 70 4e 65 77 29 3b 0a 20 20 20 20 20 20 20 70 43  pNew);.       pC
cbfc0 61 6e 64 69 64 61 74 65 2d 3e 6e 52 65 66 2b 2b  andidate->nRef++
cbfd0 3b 0a 20 20 20 20 20 20 20 75 6e 69 78 4c 65 61  ;.       unixLea
cbfe0 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20  veMutex();.     
cbff0 20 20 72 65 74 75 72 6e 20 70 43 61 6e 64 69 64    return pCandid
cc000 61 74 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ate;.    }.  }..
cc010 20 20 2f 2a 20 4e 6f 20 6d 61 74 63 68 20 77 61    /* No match wa
cc020 73 20 66 6f 75 6e 64 2e 20 20 57 65 20 77 69 6c  s found.  We wil
cc030 6c 20 6d 61 6b 65 20 61 20 6e 65 77 20 66 69 6c  l make a new fil
cc040 65 20 49 44 20 2a 2f 0a 20 20 70 4e 65 77 2d 3e  e ID */.  pNew->
cc050 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 4e 65 77  nRef = 1;.  pNew
cc060 2d 3e 6e 4e 61 6d 65 20 3d 20 6e 3b 0a 20 20 70  ->nName = n;.  p
cc070 4e 65 77 2d 3e 70 4e 65 78 74 20 3d 20 76 78 77  New->pNext = vxw
cc080 6f 72 6b 73 46 69 6c 65 4c 69 73 74 3b 0a 20 20  orksFileList;.  
cc090 76 78 77 6f 72 6b 73 46 69 6c 65 4c 69 73 74 20  vxworksFileList 
cc0a0 3d 20 70 4e 65 77 3b 0a 20 20 75 6e 69 78 4c 65  = pNew;.  unixLe
cc0b0 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65  aveMutex();.  re
cc0c0 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a  turn pNew;.}../*
cc0d0 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68  .** Decrement th
cc0e0 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
cc0f0 74 20 6f 6e 20 61 20 76 78 77 6f 72 6b 73 46 69  t on a vxworksFi
cc100 6c 65 49 64 20 6f 62 6a 65 63 74 2e 20 20 46 72  leId object.  Fr
cc110 65 65 0a 2a 2a 20 74 68 65 20 6f 62 6a 65 63 74  ee.** the object
cc120 20 77 68 65 6e 20 74 68 65 20 72 65 66 65 72 65   when the refere
cc130 6e 63 65 20 63 6f 75 6e 74 20 72 65 61 63 68 65  nce count reache
cc140 73 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69  s zero..*/.stati
cc150 63 20 76 6f 69 64 20 76 78 77 6f 72 6b 73 52 65  c void vxworksRe
cc160 6c 65 61 73 65 46 69 6c 65 49 64 28 73 74 72 75  leaseFileId(stru
cc170 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65 49 64  ct vxworksFileId
cc180 20 2a 70 49 64 29 7b 0a 20 20 75 6e 69 78 45 6e   *pId){.  unixEn
cc190 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 61 73  terMutex();.  as
cc1a0 73 65 72 74 28 20 70 49 64 2d 3e 6e 52 65 66 3e  sert( pId->nRef>
cc1b0 30 20 29 3b 0a 20 20 70 49 64 2d 3e 6e 52 65 66  0 );.  pId->nRef
cc1c0 2d 2d 3b 0a 20 20 69 66 28 20 70 49 64 2d 3e 6e  --;.  if( pId->n
cc1d0 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74  Ref==0 ){.    st
cc1e0 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c 65  ruct vxworksFile
cc1f0 49 64 20 2a 2a 70 70 3b 0a 20 20 20 20 66 6f 72  Id **pp;.    for
cc200 28 70 70 3d 26 76 78 77 6f 72 6b 73 46 69 6c 65  (pp=&vxworksFile
cc210 4c 69 73 74 3b 20 2a 70 70 20 26 26 20 2a 70 70  List; *pp && *pp
cc220 21 3d 70 49 64 3b 20 70 70 20 3d 20 26 28 28 2a  !=pId; pp = &((*
cc230 70 70 29 2d 3e 70 4e 65 78 74 29 29 7b 7d 0a 20  pp)->pNext)){}. 
cc240 20 20 20 61 73 73 65 72 74 28 20 2a 70 70 3d 3d     assert( *pp==
cc250 70 49 64 20 29 3b 0a 20 20 20 20 2a 70 70 20 3d  pId );.    *pp =
cc260 20 70 49 64 2d 3e 70 4e 65 78 74 3b 0a 20 20 20   pId->pNext;.   
cc270 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 49   sqlite3_free(pI
cc280 64 29 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 4c 65  d);.  }.  unixLe
cc290 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 23 65  aveMutex();.}.#e
cc2a0 6e 64 69 66 20 2f 2a 20 4f 53 5f 56 58 57 4f 52  ndif /* OS_VXWOR
cc2b0 4b 53 20 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  KS */./*********
cc2c0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 55 6e  ****** End of Un
cc2d0 69 71 75 65 20 46 69 6c 65 20 49 44 20 55 74 69  ique File ID Uti
cc2e0 6c 69 74 79 20 55 73 65 64 20 42 79 20 56 78 57  lity Used By VxW
cc2f0 6f 72 6b 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  orks ***********
cc300 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *****.**********
cc310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc350 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  ****/.../*******
cc360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc3a0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
cc3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc3c0 2a 2a 2a 20 50 6f 73 69 78 20 41 64 76 69 73 6f  *** Posix Adviso
cc3d0 72 79 20 4c 6f 63 6b 69 6e 67 20 2a 2a 2a 2a 2a  ry Locking *****
cc3e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
cc3f0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 50 4f  *******.**.** PO
cc400 53 49 58 20 61 64 76 69 73 6f 72 79 20 6c 6f 63  SIX advisory loc
cc410 6b 73 20 61 72 65 20 62 72 6f 6b 65 6e 20 62 79  ks are broken by
cc420 20 64 65 73 69 67 6e 2e 20 20 41 4e 53 49 20 53   design.  ANSI S
cc430 54 44 20 31 30 30 33 2e 31 20 28 31 39 39 36 29  TD 1003.1 (1996)
cc440 0a 2a 2a 20 73 65 63 74 69 6f 6e 20 36 2e 35 2e  .** section 6.5.
cc450 32 2e 32 20 6c 69 6e 65 73 20 34 38 33 20 74 68  2.2 lines 483 th
cc460 72 6f 75 67 68 20 34 39 30 20 73 70 65 63 69 66  rough 490 specif
cc470 79 20 74 68 61 74 20 77 68 65 6e 20 61 20 70 72  y that when a pr
cc480 6f 63 65 73 73 0a 2a 2a 20 73 65 74 73 20 6f 72  ocess.** sets or
cc490 20 63 6c 65 61 72 73 20 61 20 6c 6f 63 6b 2c 20   clears a lock, 
cc4a0 74 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f  that operation o
cc4b0 76 65 72 72 69 64 65 73 20 61 6e 79 20 70 72 69  verrides any pri
cc4c0 6f 72 20 6c 6f 63 6b 73 20 73 65 74 0a 2a 2a 20  or locks set.** 
cc4d0 62 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63  by the same proc
cc4e0 65 73 73 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f  ess.  It does no
cc4f0 74 20 65 78 70 6c 69 63 69 74 6c 79 20 73 61 79  t explicitly say
cc500 20 73 6f 2c 20 62 75 74 20 74 68 69 73 20 69 6d   so, but this im
cc510 70 6c 69 65 73 0a 2a 2a 20 74 68 61 74 20 69 74  plies.** that it
cc520 20 6f 76 65 72 72 69 64 65 73 20 6c 6f 63 6b 73   overrides locks
cc530 20 73 65 74 20 62 79 20 74 68 65 20 73 61 6d 65   set by the same
cc540 20 70 72 6f 63 65 73 73 20 75 73 69 6e 67 20 61   process using a
cc550 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66 69   different.** fi
cc560 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20 20  le descriptor.  
cc570 43 6f 6e 73 69 64 65 72 20 74 68 69 73 20 74 65  Consider this te
cc580 73 74 20 63 61 73 65 3a 0a 2a 2a 0a 2a 2a 20 20  st case:.**.**  
cc590 20 20 20 20 20 69 6e 74 20 66 64 31 20 3d 20 6f       int fd1 = o
cc5a0 70 65 6e 28 22 2e 2f 66 69 6c 65 31 22 2c 20 4f  pen("./file1", O
cc5b0 5f 52 44 57 52 7c 4f 5f 43 52 45 41 54 2c 20 30  _RDWR|O_CREAT, 0
cc5c0 36 34 34 29 3b 0a 2a 2a 20 20 20 20 20 20 20 69  644);.**       i
cc5d0 6e 74 20 66 64 32 20 3d 20 6f 70 65 6e 28 22 2e  nt fd2 = open(".
cc5e0 2f 66 69 6c 65 32 22 2c 20 4f 5f 52 44 57 52 7c  /file2", O_RDWR|
cc5f0 4f 5f 43 52 45 41 54 2c 20 30 36 34 34 29 3b 0a  O_CREAT, 0644);.
cc600 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 2e 2f  **.** Suppose ./
cc610 66 69 6c 65 31 20 61 6e 64 20 2e 2f 66 69 6c 65  file1 and ./file
cc620 32 20 61 72 65 20 72 65 61 6c 6c 79 20 74 68 65  2 are really the
cc630 20 73 61 6d 65 20 66 69 6c 65 20 28 62 65 63 61   same file (beca
cc640 75 73 65 0a 2a 2a 20 6f 6e 65 20 69 73 20 61 20  use.** one is a 
cc650 68 61 72 64 20 6f 72 20 73 79 6d 62 6f 6c 69 63  hard or symbolic
cc660 20 6c 69 6e 6b 20 74 6f 20 74 68 65 20 6f 74 68   link to the oth
cc670 65 72 29 20 74 68 65 6e 20 69 66 20 79 6f 75 20  er) then if you 
cc680 73 65 74 0a 2a 2a 20 61 6e 20 65 78 63 6c 75 73  set.** an exclus
cc690 69 76 65 20 6c 6f 63 6b 20 6f 6e 20 66 64 31 2c  ive lock on fd1,
cc6a0 20 74 68 65 6e 20 74 72 79 20 74 6f 20 67 65 74   then try to get
cc6b0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   an exclusive lo
cc6c0 63 6b 0a 2a 2a 20 6f 6e 20 66 64 32 2c 20 69 74  ck.** on fd2, it
cc6d0 20 77 6f 72 6b 73 2e 20 20 49 20 77 6f 75 6c 64   works.  I would
cc6e0 20 68 61 76 65 20 65 78 70 65 63 74 65 64 20 74   have expected t
cc6f0 68 65 20 73 65 63 6f 6e 64 20 6c 6f 63 6b 20 74  he second lock t
cc700 6f 0a 2a 2a 20 66 61 69 6c 20 73 69 6e 63 65 20  o.** fail since 
cc710 74 68 65 72 65 20 77 61 73 20 61 6c 72 65 61 64  there was alread
cc720 79 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  y a lock on the 
cc730 66 69 6c 65 20 64 75 65 20 74 6f 20 66 64 31 2e  file due to fd1.
cc740 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 73 6f 2e 20  .** But not so. 
cc750 20 53 69 6e 63 65 20 62 6f 74 68 20 6c 6f 63 6b   Since both lock
cc760 73 20 63 61 6d 65 20 66 72 6f 6d 20 74 68 65 20  s came from the 
cc770 73 61 6d 65 20 70 72 6f 63 65 73 73 2c 20 74 68  same process, th
cc780 65 0a 2a 2a 20 73 65 63 6f 6e 64 20 6f 76 65 72  e.** second over
cc790 72 69 64 65 73 20 74 68 65 20 66 69 72 73 74 2c  rides the first,
cc7a0 20 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65   even though the
cc7b0 79 20 77 65 72 65 20 6f 6e 20 64 69 66 66 65 72  y were on differ
cc7c0 65 6e 74 0a 2a 2a 20 66 69 6c 65 20 64 65 73 63  ent.** file desc
cc7d0 72 69 70 74 6f 72 73 20 6f 70 65 6e 65 64 20 6f  riptors opened o
cc7e0 6e 20 64 69 66 66 65 72 65 6e 74 20 66 69 6c 65  n different file
cc7f0 20 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68   names..**.** Th
cc800 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65  is means that we
cc810 20 63 61 6e 6e 6f 74 20 75 73 65 20 50 4f 53 49   cannot use POSI
cc820 58 20 6c 6f 63 6b 73 20 74 6f 20 73 79 6e 63 68  X locks to synch
cc830 72 6f 6e 69 7a 65 20 66 69 6c 65 20 61 63 63 65  ronize file acce
cc840 73 73 0a 2a 2a 20 61 6d 6f 6e 67 20 63 6f 6d 70  ss.** among comp
cc850 65 74 69 6e 67 20 74 68 72 65 61 64 73 20 6f 66  eting threads of
cc860 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73   the same proces
cc870 73 2e 20 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20  s.  POSIX locks 
cc880 77 69 6c 6c 20 77 6f 72 6b 20 66 69 6e 65 0a 2a  will work fine.*
cc890 2a 20 74 6f 20 73 79 6e 63 68 72 6f 6e 69 7a 65  * to synchronize
cc8a0 20 61 63 63 65 73 73 20 66 6f 72 20 74 68 72 65   access for thre
cc8b0 61 64 73 20 69 6e 20 73 65 70 61 72 61 74 65 20  ads in separate 
cc8c0 70 72 6f 63 65 73 73 65 73 2c 20 62 75 74 20 6e  processes, but n
cc8d0 6f 74 0a 2a 2a 20 74 68 72 65 61 64 73 20 77 69  ot.** threads wi
cc8e0 74 68 69 6e 20 74 68 65 20 73 61 6d 65 20 70 72  thin the same pr
cc8f0 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20  ocess..**.** To 
cc900 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 65 20  work around the 
cc910 70 72 6f 62 6c 65 6d 2c 20 53 51 4c 69 74 65 20  problem, SQLite 
cc920 68 61 73 20 74 6f 20 6d 61 6e 61 67 65 20 66 69  has to manage fi
cc930 6c 65 20 6c 6f 63 6b 73 20 69 6e 74 65 72 6e 61  le locks interna
cc940 6c 6c 79 0a 2a 2a 20 6f 6e 20 69 74 73 20 6f 77  lly.** on its ow
cc950 6e 2e 20 20 57 68 65 6e 65 76 65 72 20 61 20 6e  n.  Whenever a n
cc960 65 77 20 64 61 74 61 62 61 73 65 20 69 73 20 6f  ew database is o
cc970 70 65 6e 65 64 2c 20 77 65 20 68 61 76 65 20 74  pened, we have t
cc980 6f 20 66 69 6e 64 20 74 68 65 0a 2a 2a 20 73 70  o find the.** sp
cc990 65 63 69 66 69 63 20 69 6e 6f 64 65 20 6f 66 20  ecific inode of 
cc9a0 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
cc9b0 65 20 28 74 68 65 20 69 6e 6f 64 65 20 69 73 20  e (the inode is 
cc9c0 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68  determined by th
cc9d0 65 0a 2a 2a 20 73 74 5f 64 65 76 20 61 6e 64 20  e.** st_dev and 
cc9e0 73 74 5f 69 6e 6f 20 66 69 65 6c 64 73 20 6f 66  st_ino fields of
cc9f0 20 74 68 65 20 73 74 61 74 20 73 74 72 75 63 74   the stat struct
cca00 75 72 65 20 74 68 61 74 20 66 73 74 61 74 28 29  ure that fstat()
cca10 20 66 69 6c 6c 73 20 69 6e 29 0a 2a 2a 20 61 6e   fills in).** an
cca20 64 20 63 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b  d check for lock
cca30 73 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 69  s already existi
cca40 6e 67 20 6f 6e 20 74 68 61 74 20 69 6e 6f 64 65  ng on that inode
cca50 2e 20 20 57 68 65 6e 20 6c 6f 63 6b 73 20 61 72  .  When locks ar
cca60 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 6f 72 20  e.** created or 
cca70 72 65 6d 6f 76 65 64 2c 20 77 65 20 68 61 76 65  removed, we have
cca80 20 74 6f 20 6c 6f 6f 6b 20 61 74 20 6f 75 72 20   to look at our 
cca90 6f 77 6e 20 69 6e 74 65 72 6e 61 6c 20 72 65 63  own internal rec
ccaa0 6f 72 64 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 6f  ord of the.** lo
ccab0 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 61 6e  cks to see if an
ccac0 6f 74 68 65 72 20 74 68 72 65 61 64 20 68 61 73  other thread has
ccad0 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74 20   previously set 
ccae0 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 20 73  a lock on that s
ccaf0 61 6d 65 0a 2a 2a 20 69 6e 6f 64 65 2e 0a 2a 2a  ame.** inode..**
ccb00 0a 2a 2a 20 28 41 73 69 64 65 3a 20 54 68 65 20  .** (Aside: The 
ccb10 75 73 65 20 6f 66 20 69 6e 6f 64 65 20 6e 75 6d  use of inode num
ccb20 62 65 72 73 20 61 73 20 75 6e 69 71 75 65 20 49  bers as unique I
ccb30 44 73 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b  Ds does not work
ccb40 20 6f 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 20   on VxWorks..** 
ccb50 46 6f 72 20 56 78 57 6f 72 6b 73 2c 20 77 65 20  For VxWorks, we 
ccb60 68 61 76 65 20 74 6f 20 75 73 65 20 74 68 65 20  have to use the 
ccb70 61 6c 74 65 72 6e 61 74 69 76 65 20 75 6e 69 71  alternative uniq
ccb80 75 65 20 49 44 20 73 79 73 74 65 6d 20 62 61 73  ue ID system bas
ccb90 65 64 20 6f 6e 0a 2a 2a 20 63 61 6e 6f 6e 69 63  ed on.** canonic
ccba0 61 6c 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20  al filename and 
ccbb0 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 74  implemented in t
ccbc0 68 65 20 70 72 65 76 69 6f 75 73 20 64 69 76 69  he previous divi
ccbd0 73 69 6f 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65  sion.).**.** The
ccbe0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 73 74   sqlite3_file st
ccbf0 72 75 63 74 75 72 65 20 66 6f 72 20 50 4f 53 49  ructure for POSI
ccc00 58 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 6a  X is no longer j
ccc10 75 73 74 20 61 6e 20 69 6e 74 65 67 65 72 20 66  ust an integer f
ccc20 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 6f  ile.** descripto
ccc30 72 2e 20 20 49 74 20 69 73 20 6e 6f 77 20 61 20  r.  It is now a 
ccc40 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 68  structure that h
ccc50 6f 6c 64 73 20 74 68 65 20 69 6e 74 65 67 65 72  olds the integer
ccc60 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72 69 70   file.** descrip
ccc70 74 6f 72 20 61 6e 64 20 61 20 70 6f 69 6e 74 65  tor and a pointe
ccc80 72 20 74 6f 20 61 20 73 74 72 75 63 74 75 72 65  r to a structure
ccc90 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20   that describes 
ccca0 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20  the internal.** 
cccb0 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 63 6f 72  locks on the cor
cccc0 72 65 73 70 6f 6e 64 69 6e 67 20 69 6e 6f 64 65  responding inode
cccd0 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20  .  There is one 
ccce0 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72  locking structur
cccf0 65 0a 2a 2a 20 70 65 72 20 69 6e 6f 64 65 2c 20  e.** per inode, 
ccd00 73 6f 20 69 66 20 74 68 65 20 73 61 6d 65 20 69  so if the same i
ccd10 6e 6f 64 65 20 69 73 20 6f 70 65 6e 65 64 20 74  node is opened t
ccd20 77 69 63 65 2c 20 62 6f 74 68 20 75 6e 69 78 46  wice, both unixF
ccd30 69 6c 65 20 73 74 72 75 63 74 75 72 65 73 0a 2a  ile structures.*
ccd40 2a 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73  * point to the s
ccd50 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75  ame locking stru
ccd60 63 74 75 72 65 2e 20 20 54 68 65 20 6c 6f 63 6b  cture.  The lock
ccd70 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 6b 65  ing structure ke
ccd80 65 70 73 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e  eps.** a referen
ccd90 63 65 20 63 6f 75 6e 74 20 28 73 6f 20 77 65 20  ce count (so we 
ccda0 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 6e 20 74  will know when t
ccdb0 6f 20 64 65 6c 65 74 65 20 69 74 29 20 61 6e 64  o delete it) and
ccdc0 20 61 20 22 63 6e 74 22 0a 2a 2a 20 66 69 65 6c   a "cnt".** fiel
ccdd0 64 20 74 68 61 74 20 74 65 6c 6c 73 20 75 73 20  d that tells us 
ccde0 69 74 73 20 69 6e 74 65 72 6e 61 6c 20 6c 6f 63  its internal loc
ccdf0 6b 20 73 74 61 74 75 73 2e 20 20 63 6e 74 3d 3d  k status.  cnt==
cce00 30 20 6d 65 61 6e 73 20 74 68 65 0a 2a 2a 20 66  0 means the.** f
cce10 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2e  ile is unlocked.
cce20 20 20 63 6e 74 3d 3d 2d 31 20 6d 65 61 6e 73 20    cnt==-1 means 
cce30 74 68 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20  the file has an 
cce40 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 0a  exclusive lock..
cce50 2a 2a 20 63 6e 74 3e 30 20 6d 65 61 6e 73 20 74  ** cnt>0 means t
cce60 68 65 72 65 20 61 72 65 20 63 6e 74 20 73 68 61  here are cnt sha
cce70 72 65 64 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65  red locks on the
cce80 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79   file..**.** Any
cce90 20 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63 6b   attempt to lock
ccea0 20 6f 72 20 75 6e 6c 6f 63 6b 20 61 20 66 69 6c   or unlock a fil
cceb0 65 20 66 69 72 73 74 20 63 68 65 63 6b 73 20 74  e first checks t
ccec0 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74  he locking.** st
cced0 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 66 63  ructure.  The fc
ccee0 6e 74 6c 28 29 20 73 79 73 74 65 6d 20 63 61 6c  ntl() system cal
ccef0 6c 20 69 73 20 6f 6e 6c 79 20 69 6e 76 6f 6b 65  l is only invoke
ccf00 64 20 74 6f 20 73 65 74 20 61 20 0a 2a 2a 20 50  d to set a .** P
ccf10 4f 53 49 58 20 6c 6f 63 6b 20 69 66 20 74 68 65  OSIX lock if the
ccf20 20 69 6e 74 65 72 6e 61 6c 20 6c 6f 63 6b 20 73   internal lock s
ccf30 74 72 75 63 74 75 72 65 20 74 72 61 6e 73 69 74  tructure transit
ccf40 69 6f 6e 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20  ions between.** 
ccf50 61 20 6c 6f 63 6b 65 64 20 61 6e 64 20 61 6e 20  a locked and an 
ccf60 75 6e 6c 6f 63 6b 65 64 20 73 74 61 74 65 2e 0a  unlocked state..
ccf70 2a 2a 0a 2a 2a 20 42 75 74 20 77 61 69 74 3a 20  **.** But wait: 
ccf80 20 74 68 65 72 65 20 61 72 65 20 79 65 74 20 6d   there are yet m
ccf90 6f 72 65 20 70 72 6f 62 6c 65 6d 73 20 77 69 74  ore problems wit
ccfa0 68 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79  h POSIX advisory
ccfb0 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 49 66   locks..**.** If
ccfc0 20 79 6f 75 20 63 6c 6f 73 65 20 61 20 66 69 6c   you close a fil
ccfd0 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 68 61  e descriptor tha
ccfe0 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 66 69  t points to a fi
ccff0 6c 65 20 74 68 61 74 20 68 61 73 20 6c 6f 63 6b  le that has lock
cd000 73 2c 0a 2a 2a 20 61 6c 6c 20 6c 6f 63 6b 73 20  s,.** all locks 
cd010 6f 6e 20 74 68 61 74 20 66 69 6c 65 20 74 68 61  on that file tha
cd020 74 20 61 72 65 20 6f 77 6e 65 64 20 62 79 20 74  t are owned by t
cd030 68 65 20 63 75 72 72 65 6e 74 20 70 72 6f 63 65  he current proce
cd040 73 73 20 61 72 65 0a 2a 2a 20 72 65 6c 65 61 73  ss are.** releas
cd050 65 64 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72 6f  ed.  To work aro
cd060 75 6e 64 20 74 68 69 73 20 70 72 6f 62 6c 65 6d  und this problem
cd070 2c 20 65 61 63 68 20 75 6e 69 78 46 69 6c 65 20  , each unixFile 
cd080 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
cd090 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  ns.** a pointer 
cd0a0 74 6f 20 61 6e 20 75 6e 69 78 4f 70 65 6e 43 6e  to an unixOpenCn
cd0b0 74 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  t structure.  Th
cd0c0 65 72 65 20 69 73 20 6f 6e 65 20 75 6e 69 78 4f  ere is one unixO
cd0d0 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65  penCnt structure
cd0e0 0a 2a 2a 20 70 65 72 20 6f 70 65 6e 20 69 6e 6f  .** per open ino
cd0f0 64 65 2c 20 77 68 69 63 68 20 6d 65 61 6e 73 20  de, which means 
cd100 74 68 61 74 20 6d 75 6c 74 69 70 6c 65 20 75 6e  that multiple un
cd110 69 78 46 69 6c 65 20 63 61 6e 20 70 6f 69 6e 74  ixFile can point
cd120 20 74 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20   to a single.** 
cd130 75 6e 69 78 4f 70 65 6e 43 6e 74 2e 20 20 57 68  unixOpenCnt.  Wh
cd140 65 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73  en an attempt is
cd150 20 6d 61 64 65 20 74 6f 20 63 6c 6f 73 65 20 61   made to close a
cd160 6e 20 75 6e 69 78 46 69 6c 65 2c 20 69 66 20 74  n unixFile, if t
cd170 68 65 72 65 20 61 72 65 0a 2a 2a 20 6f 74 68 65  here are.** othe
cd180 72 20 75 6e 69 78 46 69 6c 65 20 6f 70 65 6e 20  r unixFile open 
cd190 6f 6e 20 74 68 65 20 73 61 6d 65 20 69 6e 6f 64  on the same inod
cd1a0 65 20 74 68 61 74 20 61 72 65 20 68 6f 6c 64 69  e that are holdi
cd1b0 6e 67 20 6c 6f 63 6b 73 2c 20 74 68 65 20 63 61  ng locks, the ca
cd1c0 6c 6c 0a 2a 2a 20 74 6f 20 63 6c 6f 73 65 28 29  ll.** to close()
cd1d0 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
cd1e0 70 74 6f 72 20 69 73 20 64 65 66 65 72 72 65 64  ptor is deferred
cd1f0 20 75 6e 74 69 6c 20 61 6c 6c 20 6f 66 20 74 68   until all of th
cd200 65 20 6c 6f 63 6b 73 20 63 6c 65 61 72 2e 0a 2a  e locks clear..*
cd210 2a 20 54 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e  * The unixOpenCn
cd220 74 20 73 74 72 75 63 74 75 72 65 20 6b 65 65 70  t structure keep
cd230 73 20 61 20 6c 69 73 74 20 6f 66 20 66 69 6c 65  s a list of file
cd240 20 64 65 73 63 72 69 70 74 6f 72 73 20 74 68 61   descriptors tha
cd250 74 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 62 65 20  t need to.** be 
cd260 63 6c 6f 73 65 64 20 61 6e 64 20 74 68 61 74 20  closed and that 
cd270 6c 69 73 74 20 69 73 20 77 61 6c 6b 65 64 20 28  list is walked (
cd280 61 6e 64 20 63 6c 65 61 72 65 64 29 20 77 68 65  and cleared) whe
cd290 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 0a  n the last lock.
cd2a0 2a 2a 20 63 6c 65 61 72 73 2e 0a 2a 2a 0a 2a 2a  ** clears..**.**
cd2b0 20 59 65 74 20 61 6e 6f 74 68 65 72 20 70 72 6f   Yet another pro
cd2c0 62 6c 65 6d 3a 20 20 4c 69 6e 75 78 54 68 72 65  blem:  LinuxThre
cd2d0 61 64 73 20 64 6f 20 6e 6f 74 20 70 6c 61 79 20  ads do not play 
cd2e0 77 65 6c 6c 20 77 69 74 68 20 70 6f 73 69 78 20  well with posix 
cd2f0 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 4d 61 6e  locks..**.** Man
cd300 79 20 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e 73  y older versions
cd310 20 6f 66 20 6c 69 6e 75 78 20 75 73 65 20 74 68   of linux use th
cd320 65 20 4c 69 6e 75 78 54 68 72 65 61 64 73 20 6c  e LinuxThreads l
cd330 69 62 72 61 72 79 20 77 68 69 63 68 20 69 73 0a  ibrary which is.
cd340 2a 2a 20 6e 6f 74 20 70 6f 73 69 78 20 63 6f 6d  ** not posix com
cd350 70 6c 69 61 6e 74 2e 20 20 55 6e 64 65 72 20 4c  pliant.  Under L
cd360 69 6e 75 78 54 68 72 65 61 64 73 2c 20 61 20 6c  inuxThreads, a l
cd370 6f 63 6b 20 63 72 65 61 74 65 64 20 62 79 20 74  ock created by t
cd380 68 72 65 61 64 0a 2a 2a 20 41 20 63 61 6e 6e 6f  hread.** A canno
cd390 74 20 62 65 20 6d 6f 64 69 66 69 65 64 20 6f 72  t be modified or
cd3a0 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20 61   overridden by a
cd3b0 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61   different threa
cd3c0 64 20 42 2e 0a 2a 2a 20 4f 6e 6c 79 20 74 68 72  d B..** Only thr
cd3d0 65 61 64 20 41 20 63 61 6e 20 6d 6f 64 69 66 79  ead A can modify
cd3e0 20 74 68 65 20 6c 6f 63 6b 2e 20 20 4c 6f 63 6b   the lock.  Lock
cd3f0 69 6e 67 20 62 65 68 61 76 69 6f 72 20 69 73 20  ing behavior is 
cd400 63 6f 72 72 65 63 74 0a 2a 2a 20 69 66 20 74 68  correct.** if th
cd410 65 20 61 70 70 6c 69 61 74 69 6f 6e 20 75 73 65  e appliation use
cd420 73 20 74 68 65 20 6e 65 77 65 72 20 4e 61 74 69  s the newer Nati
cd430 76 65 20 50 6f 73 69 78 20 54 68 72 65 61 64 20  ve Posix Thread 
cd440 4c 69 62 72 61 72 79 20 28 4e 50 54 4c 29 0a 2a  Library (NPTL).*
cd450 2a 20 6f 6e 20 6c 69 6e 75 78 20 2d 20 77 69 74  * on linux - wit
cd460 68 20 4e 50 54 4c 20 61 20 6c 6f 63 6b 20 63 72  h NPTL a lock cr
cd470 65 61 74 65 64 20 62 79 20 74 68 72 65 61 64 20  eated by thread 
cd480 41 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 6c  A can override l
cd490 6f 63 6b 73 0a 2a 2a 20 69 6e 20 74 68 72 65 61  ocks.** in threa
cd4a0 64 20 42 2e 20 20 42 75 74 20 74 68 65 72 65 20  d B.  But there 
cd4b0 69 73 20 6e 6f 20 77 61 79 20 74 6f 20 6b 6e 6f  is no way to kno
cd4c0 77 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  w at compile-tim
cd4d0 65 20 77 68 69 63 68 0a 2a 2a 20 74 68 72 65 61  e which.** threa
cd4e0 64 69 6e 67 20 6c 69 62 72 61 72 79 20 69 73 20  ding library is 
cd4f0 62 65 69 6e 67 20 75 73 65 64 2e 20 20 53 6f 20  being used.  So 
cd500 74 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20  there is no way 
cd510 74 6f 20 6b 6e 6f 77 20 61 74 0a 2a 2a 20 63 6f  to know at.** co
cd520 6d 70 69 6c 65 2d 74 69 6d 65 20 77 68 65 74 68  mpile-time wheth
cd530 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64  er or not thread
cd540 20 41 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20   A can override 
cd550 6c 6f 63 6b 73 20 6f 6e 20 74 68 72 65 61 64 20  locks on thread 
cd560 42 2e 0a 2a 2a 20 57 65 20 68 61 76 65 20 74 6f  B..** We have to
cd570 20 64 6f 20 61 20 72 75 6e 2d 74 69 6d 65 20 63   do a run-time c
cd580 68 65 63 6b 20 74 6f 20 64 69 73 63 6f 76 65 72  heck to discover
cd590 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66   the behavior of
cd5a0 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20   the.** current 
cd5b0 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 4f  process..**.** O
cd5c0 6e 20 73 79 73 74 65 6d 73 20 77 68 65 72 65 20  n systems where 
cd5d0 74 68 72 65 61 64 20 41 20 69 73 20 75 6e 61 62  thread A is unab
cd5e0 6c 65 20 74 6f 20 6d 6f 64 69 66 79 20 6c 6f 63  le to modify loc
cd5f0 6b 73 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a  ks created by.**
cd600 20 74 68 72 65 61 64 20 42 2c 20 77 65 20 68 61   thread B, we ha
cd610 76 65 20 74 6f 20 6b 65 65 70 20 74 72 61 63 6b  ve to keep track
cd620 20 6f 66 20 77 68 69 63 68 20 74 68 72 65 61 64   of which thread
cd630 20 63 72 65 61 74 65 64 20 65 61 63 68 0a 2a 2a   created each.**
cd640 20 6c 6f 63 6b 2e 20 20 48 65 6e 63 65 20 74 68   lock.  Hence th
cd650 65 72 65 20 69 73 20 61 6e 20 65 78 74 72 61 20  ere is an extra 
cd660 66 69 65 6c 64 20 69 6e 20 74 68 65 20 6b 65 79  field in the key
cd670 20 74 6f 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b   to the unixLock
cd680 49 6e 66 6f 0a 2a 2a 20 73 74 72 75 63 74 75 72  Info.** structur
cd690 65 20 74 6f 20 72 65 63 6f 72 64 20 74 68 69 73  e to record this
cd6a0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 41   information.  A
cd6b0 6e 64 20 6f 6e 20 74 68 6f 73 65 20 73 79 73 74  nd on those syst
cd6c0 65 6d 73 20 69 74 0a 2a 2a 20 69 73 20 69 6c 6c  ems it.** is ill
cd6d0 65 67 61 6c 20 74 6f 20 62 65 67 69 6e 20 61 20  egal to begin a 
cd6e0 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 6f  transaction in o
cd6f0 6e 65 20 74 68 72 65 61 64 20 61 6e 64 20 66 69  ne thread and fi
cd700 6e 69 73 68 20 69 74 0a 2a 2a 20 69 6e 20 61 6e  nish it.** in an
cd710 6f 74 68 65 72 2e 20 20 46 6f 72 20 74 68 69 73  other.  For this
cd720 20 6c 61 74 74 65 72 20 72 65 73 74 72 69 63 74   latter restrict
cd730 69 6f 6e 2c 20 74 68 65 72 65 20 69 73 20 6e 6f  ion, there is no
cd740 20 77 6f 72 6b 2d 61 72 6f 75 6e 64 2e 0a 2a 2a   work-around..**
cd750 20 49 74 20 69 73 20 61 20 6c 69 6d 69 74 61 74   It is a limitat
cd760 69 6f 6e 20 6f 66 20 4c 69 6e 75 78 54 68 72 65  ion of LinuxThre
cd770 61 64 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53  ads..*/../*.** S
cd780 65 74 20 6f 72 20 63 68 65 63 6b 20 74 68 65 20  et or check the 
cd790 75 6e 69 78 46 69 6c 65 2e 74 69 64 20 66 69 65  unixFile.tid fie
cd7a0 6c 64 2e 20 20 54 68 69 73 20 66 69 65 6c 64 20  ld.  This field 
cd7b0 69 73 20 73 65 74 20 77 68 65 6e 20 61 6e 20 75  is set when an u
cd7c0 6e 69 78 46 69 6c 65 0a 2a 2a 20 69 73 20 66 69  nixFile.** is fi
cd7d0 72 73 74 20 6f 70 65 6e 65 64 2e 20 20 41 6c 6c  rst opened.  All
cd7e0 20 73 75 62 73 65 71 75 65 6e 74 20 75 73 65 73   subsequent uses
cd7f0 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65   of the unixFile
cd800 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68 65   verify that the
cd810 0a 2a 2a 20 73 61 6d 65 20 74 68 72 65 61 64 20  .** same thread 
cd820 69 73 20 6f 70 65 72 61 74 69 6e 67 20 6f 6e 20  is operating on 
cd830 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 20 20 53  the unixFile.  S
cd840 6f 6d 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  ome operating sy
cd850 73 74 65 6d 73 20 64 6f 0a 2a 2a 20 6e 6f 74 20  stems do.** not 
cd860 61 6c 6c 6f 77 20 6c 6f 63 6b 73 20 74 6f 20 62  allow locks to b
cd870 65 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79 20  e overridden by 
cd880 6f 74 68 65 72 20 74 68 72 65 61 64 73 20 61 6e  other threads an
cd890 64 20 74 68 61 74 20 72 65 73 74 72 69 63 74 69  d that restricti
cd8a0 6f 6e 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74  on.** means that
cd8b0 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62 61   sqlite3* databa
cd8c0 73 65 20 68 61 6e 64 6c 65 73 20 63 61 6e 6e 6f  se handles canno
cd8d0 74 20 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d 20  t be moved from 
cd8e0 6f 6e 65 20 74 68 72 65 61 64 0a 2a 2a 20 74 6f  one thread.** to
cd8f0 20 61 6e 6f 74 68 65 72 20 77 68 69 6c 65 20 6c   another while l
cd900 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 2e 0a 2a  ocks are held..*
cd910 2a 0a 2a 2a 20 56 65 72 73 69 6f 6e 20 33 2e 33  *.** Version 3.3
cd920 2e 31 20 28 32 30 30 36 2d 30 31 2d 31 35 29 3a  .1 (2006-01-15):
cd930 20 20 75 6e 69 78 46 69 6c 65 20 63 61 6e 20 62    unixFile can b
cd940 65 20 6d 6f 76 65 64 20 66 72 6f 6d 20 6f 6e 65  e moved from one
cd950 20 74 68 72 65 61 64 20 74 6f 0a 2a 2a 20 61 6e   thread to.** an
cd960 6f 74 68 65 72 20 61 73 20 6c 6f 6e 67 20 61 73  other as long as
cd970 20 77 65 20 61 72 65 20 72 75 6e 6e 69 6e 67 20   we are running 
cd980 6f 6e 20 61 20 73 79 73 74 65 6d 20 74 68 61 74  on a system that
cd990 20 73 75 70 70 6f 72 74 73 20 74 68 72 65 61 64   supports thread
cd9a0 73 0a 2a 2a 20 6f 76 65 72 72 69 64 69 6e 67 20  s.** overriding 
cd9b0 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b  each others lock
cd9c0 73 20 28 77 68 69 63 68 20 69 73 20 6e 6f 77 20  s (which is now 
cd9d0 74 68 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 20  the most common 
cd9e0 62 65 68 61 76 69 6f 72 29 0a 2a 2a 20 6f 72 20  behavior).** or 
cd9f0 69 66 20 6e 6f 20 6c 6f 63 6b 73 20 61 72 65 20  if no locks are 
cda00 68 65 6c 64 2e 20 20 42 75 74 20 74 68 65 20 75  held.  But the u
cda10 6e 69 78 46 69 6c 65 2e 70 4c 6f 63 6b 20 66 69  nixFile.pLock fi
cda20 65 6c 64 20 6e 65 65 64 73 20 74 6f 20 62 65 0a  eld needs to be.
cda30 2a 2a 20 72 65 63 6f 6d 70 75 74 65 64 20 62 65  ** recomputed be
cda40 63 61 75 73 65 20 69 74 73 20 6b 65 79 20 69 6e  cause its key in
cda50 63 6c 75 64 65 73 20 74 68 65 20 74 68 72 65 61  cludes the threa
cda60 64 2d 69 64 2e 20 20 53 65 65 20 74 68 65 20 0a  d-id.  See the .
cda70 2a 2a 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72  ** transferOwner
cda80 73 68 69 70 28 29 20 66 75 6e 63 74 69 6f 6e 20  ship() function 
cda90 62 65 6c 6f 77 20 66 6f 72 20 61 64 64 69 74 69  below for additi
cdaa0 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
cdab0 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  .*/.#if SQLITE_T
cdac0 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66  HREADSAFE && def
cdad0 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a  ined(__linux__).
cdae0 23 20 64 65 66 69 6e 65 20 53 45 54 5f 54 48 52  # define SET_THR
cdaf0 45 41 44 49 44 28 58 29 20 20 20 28 58 29 2d 3e  EADID(X)   (X)->
cdb00 74 69 64 20 3d 20 70 74 68 72 65 61 64 5f 73 65  tid = pthread_se
cdb10 6c 66 28 29 0a 23 20 64 65 66 69 6e 65 20 43 48  lf().# define CH
cdb20 45 43 4b 5f 54 48 52 45 41 44 49 44 28 58 29 20  ECK_THREADID(X) 
cdb30 28 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65  (threadsOverride
cdb40 45 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 3d  EachOthersLocks=
cdb50 3d 30 20 26 26 20 5c 0a 20 20 20 20 20 20 20 20  =0 && \.        
cdb60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cdb70 20 20 20 20 21 70 74 68 72 65 61 64 5f 65 71 75      !pthread_equ
cdb80 61 6c 28 28 58 29 2d 3e 74 69 64 2c 20 70 74 68  al((X)->tid, pth
cdb90 72 65 61 64 5f 73 65 6c 66 28 29 29 29 0a 23 65  read_self())).#e
cdba0 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53 45 54  lse.# define SET
cdbb0 5f 54 48 52 45 41 44 49 44 28 58 29 0a 23 20 64  _THREADID(X).# d
cdbc0 65 66 69 6e 65 20 43 48 45 43 4b 5f 54 48 52 45  efine CHECK_THRE
cdbd0 41 44 49 44 28 58 29 20 30 0a 23 65 6e 64 69 66  ADID(X) 0.#endif
cdbe0 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
cdbf0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
cdc00 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 73  wing structure s
cdc10 65 72 76 65 73 20 61 73 20 74 68 65 20 6b 65 79  erves as the key
cdc20 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61   used.** to loca
cdc30 74 65 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  te a particular 
cdc40 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74 72 75  unixOpenCnt stru
cdc50 63 74 75 72 65 20 67 69 76 65 6e 20 69 74 73 20  cture given its 
cdc60 69 6e 6f 64 65 2e 20 20 54 68 69 73 0a 2a 2a 20  inode.  This.** 
cdc70 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  is the same as t
cdc80 68 65 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 65  he unixLockKey e
cdc90 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20 74  xcept that the t
cdca0 68 72 65 61 64 20 49 44 20 69 73 20 6f 6d 69 74  hread ID is omit
cdcb0 74 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75  ted..*/.struct u
cdcc0 6e 69 78 46 69 6c 65 49 64 20 7b 0a 20 20 64 65  nixFileId {.  de
cdcd0 76 5f 74 20 64 65 76 3b 20 20 20 20 20 20 20 20  v_t dev;        
cdce0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 76            /* Dev
cdcf0 69 63 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 69  ice number */.#i
cdd00 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73  f OS_VXWORKS.  s
cdd10 74 72 75 63 74 20 76 78 77 6f 72 6b 73 46 69 6c  truct vxworksFil
cdd20 65 49 64 20 2a 70 49 64 3b 20 20 2f 2a 20 55 6e  eId *pId;  /* Un
cdd30 69 71 75 65 20 66 69 6c 65 20 49 44 20 66 6f 72  ique file ID for
cdd40 20 76 78 77 6f 72 6b 73 2e 20 2a 2f 0a 23 65 6c   vxworks. */.#el
cdd50 73 65 0a 20 20 69 6e 6f 5f 74 20 69 6e 6f 3b 20  se.  ino_t ino; 
cdd60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cdd70 20 2f 2a 20 49 6e 6f 64 65 20 6e 75 6d 62 65 72   /* Inode number
cdd80 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f   */.#endif.};../
cdd90 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
cdda0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
cddb0 67 20 73 74 72 75 63 74 75 72 65 20 73 65 72 76  g structure serv
cddc0 65 73 20 61 73 20 74 68 65 20 6b 65 79 20 75 73  es as the key us
cddd0 65 64 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20  ed.** to locate 
cdde0 61 20 70 61 72 74 69 63 75 6c 61 72 20 75 6e 69  a particular uni
cddf0 78 4c 6f 63 6b 49 6e 66 6f 20 73 74 72 75 63 74  xLockInfo struct
cde00 75 72 65 20 67 69 76 65 6e 20 69 74 73 20 69 6e  ure given its in
cde10 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ode..**.** If th
cde20 72 65 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76 65  reads cannot ove
cde30 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65 72  rride each other
cde40 73 20 6c 6f 63 6b 73 20 28 4c 69 6e 75 78 54 68  s locks (LinuxTh
cde50 72 65 61 64 73 29 2c 20 74 68 65 6e 20 77 65 0a  reads), then we.
cde60 2a 2a 20 73 65 74 20 74 68 65 20 75 6e 69 78 4c  ** set the unixL
cde70 6f 63 6b 4b 65 79 2e 74 69 64 20 66 69 65 6c 64  ockKey.tid field
cde80 20 74 6f 20 74 68 65 20 74 68 72 65 61 64 20 49   to the thread I
cde90 44 2e 20 20 49 66 20 74 68 72 65 61 64 73 20 63  D.  If threads c
cdea0 61 6e 20 6f 76 65 72 72 69 64 65 0a 2a 2a 20 65  an override.** e
cdeb0 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73  ach others locks
cdec0 20 28 50 6f 73 69 78 20 61 6e 64 20 4e 50 54 4c   (Posix and NPTL
cded0 29 20 74 68 65 6e 20 74 69 64 20 69 73 20 61 6c  ) then tid is al
cdee0 77 61 79 73 20 73 65 74 20 74 6f 20 7a 65 72 6f  ways set to zero
cdef0 2e 0a 2a 2a 20 74 69 64 20 69 73 20 6f 6d 69 74  ..** tid is omit
cdf00 74 65 64 20 69 66 20 77 65 20 63 6f 6d 70 69 6c  ted if we compil
cdf10 65 20 77 69 74 68 6f 75 74 20 74 68 72 65 61 64  e without thread
cdf20 69 6e 67 20 73 75 70 70 6f 72 74 20 6f 72 20 6f  ing support or o
cdf30 6e 20 61 6e 20 4f 53 0a 2a 2a 20 6f 74 68 65 72  n an OS.** other
cdf40 20 74 68 61 6e 20 6c 69 6e 75 78 2e 0a 2a 2f 0a   than linux..*/.
cdf50 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b 4b  struct unixLockK
cdf60 65 79 20 7b 0a 20 20 73 74 72 75 63 74 20 75 6e  ey {.  struct un
cdf70 69 78 46 69 6c 65 49 64 20 66 69 64 3b 20 20 2f  ixFileId fid;  /
cdf80 2a 20 55 6e 69 71 75 65 20 69 64 65 6e 74 69 66  * Unique identif
cdf90 69 65 72 20 66 6f 72 20 74 68 65 20 66 69 6c 65  ier for the file
cdfa0 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54   */.#if SQLITE_T
cdfb0 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66  HREADSAFE && def
cdfc0 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a  ined(__linux__).
cdfd0 20 20 70 74 68 72 65 61 64 5f 74 20 74 69 64 3b    pthread_t tid;
cdfe0 20 20 2f 2a 20 54 68 72 65 61 64 20 49 44 20 6f    /* Thread ID o
cdff0 66 20 6c 6f 63 6b 20 6f 77 6e 65 72 2e 20 5a 65  f lock owner. Ze
ce000 72 6f 20 69 66 20 6e 6f 74 20 75 73 69 6e 67 20  ro if not using 
ce010 4c 69 6e 75 78 54 68 72 65 61 64 73 20 2a 2f 0a  LinuxThreads */.
ce020 23 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  #endif.};../*.**
ce030 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
ce040 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
ce050 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63  ructure is alloc
ce060 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 6f 70  ated for each op
ce070 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 4f 72  en.** inode.  Or
ce080 2c 20 6f 6e 20 4c 69 6e 75 78 54 68 72 65 61 64  , on LinuxThread
ce090 73 2c 20 74 68 65 72 65 20 69 73 20 6f 6e 65 20  s, there is one 
ce0a0 6f 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75  of these structu
ce0b0 72 65 73 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20  res for.** each 
ce0c0 69 6e 6f 64 65 20 6f 70 65 6e 65 64 20 62 79 20  inode opened by 
ce0d0 65 61 63 68 20 74 68 72 65 61 64 2e 0a 2a 2a 0a  each thread..**.
ce0e0 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e 6f 64  ** A single inod
ce0f0 65 20 63 61 6e 20 68 61 76 65 20 6d 75 6c 74 69  e can have multi
ce100 70 6c 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  ple file descrip
ce110 74 6f 72 73 2c 20 73 6f 20 65 61 63 68 20 75 6e  tors, so each un
ce120 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74  ixFile.** struct
ce130 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70  ure contains a p
ce140 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73  ointer to an ins
ce150 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62  tance of this ob
ce160 6a 65 63 74 20 61 6e 64 20 74 68 69 73 0a 2a 2a  ject and this.**
ce170 20 6f 62 6a 65 63 74 20 6b 65 65 70 73 20 61 20   object keeps a 
ce180 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75 6d  count of the num
ce190 62 65 72 20 6f 66 20 75 6e 69 78 46 69 6c 65 20  ber of unixFile 
ce1a0 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 74 2e 0a  pointing to it..
ce1b0 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69 78 4c 6f  */.struct unixLo
ce1c0 63 6b 49 6e 66 6f 20 7b 0a 20 20 73 74 72 75 63  ckInfo {.  struc
ce1d0 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 6c 6f  t unixLockKey lo
ce1e0 63 6b 4b 65 79 3b 20 20 20 20 20 2f 2a 20 54 68  ckKey;     /* Th
ce1f0 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a  e lookup key */.
ce200 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20    int cnt;      
ce210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce220 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 53    /* Number of S
ce230 48 41 52 45 44 20 6c 6f 63 6b 73 20 68 65 6c 64  HARED locks held
ce240 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f 63 6b 74 79   */.  int lockty
ce250 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  pe;             
ce260 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
ce270 53 48 41 52 45 44 5f 4c 4f 43 4b 2c 20 52 45 53  SHARED_LOCK, RES
ce280 45 52 56 45 44 5f 4c 4f 43 4b 20 65 74 63 2e 20  ERVED_LOCK etc. 
ce290 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20  */.  int nRef;  
ce2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ce2b0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
ce2c0 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  f pointers to th
ce2d0 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  is structure */.
ce2e0 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63    struct unixLoc
ce2f0 6b 49 6e 66 6f 20 2a 70 4e 65 78 74 3b 20 20 20  kInfo *pNext;   
ce300 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c    /* List of all
ce310 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 6f 62   unixLockInfo ob
ce320 6a 65 63 74 73 20 2a 2f 0a 20 20 73 74 72 75 63  jects */.  struc
ce330 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a  t unixLockInfo *
ce340 70 50 72 65 76 3b 20 20 20 20 20 2f 2a 20 20 20  pPrev;     /*   
ce350 20 2e 2e 2e 2e 20 64 6f 75 62 6c 79 20 6c 69 6e   .... doubly lin
ce360 6b 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ked */.};../*.**
ce370 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
ce380 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
ce390 72 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63  ructure is alloc
ce3a0 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 6f 70  ated for each op
ce3b0 65 6e 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 54 68  en.** inode.  Th
ce3c0 69 73 20 73 74 72 75 63 74 75 72 65 20 6b 65 65  is structure kee
ce3d0 70 73 20 74 72 61 63 6b 20 6f 66 20 74 68 65 20  ps track of the 
ce3e0 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20  number of locks 
ce3f0 6f 6e 20 74 68 61 74 0a 2a 2a 20 69 6e 6f 64 65  on that.** inode
ce400 2e 20 20 49 66 20 61 20 63 6c 6f 73 65 20 69 73  .  If a close is
ce410 20 61 74 74 65 6d 70 74 65 64 20 61 67 61 69 6e   attempted again
ce420 73 74 20 61 6e 20 69 6e 6f 64 65 20 74 68 61 74  st an inode that
ce430 20 69 73 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6c   is holding.** l
ce440 6f 63 6b 73 2c 20 74 68 65 20 63 6c 6f 73 65 20  ocks, the close 
ce450 69 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69  is deferred unti
ce460 6c 20 61 6c 6c 20 6c 6f 63 6b 73 20 63 6c 65 61  l all locks clea
ce470 72 20 62 79 20 61 64 64 69 6e 67 20 74 68 65 0a  r by adding the.
ce480 2a 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ** file descript
ce490 6f 72 20 74 6f 20 62 65 20 63 6c 6f 73 65 64 20  or to be closed 
ce4a0 74 6f 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c  to the pending l
ce4b0 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a  ist..**.** TODO:
ce4c0 20 20 43 6f 6e 73 69 64 65 72 20 63 68 61 6e 67    Consider chang
ce4d0 69 6e 67 20 74 68 69 73 20 73 6f 20 74 68 61 74  ing this so that
ce4e0 20 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61   there is only a
ce4f0 20 73 69 6e 67 6c 65 20 66 69 6c 65 0a 2a 2a 20   single file.** 
ce500 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 65  descriptor for e
ce510 61 63 68 20 6f 70 65 6e 20 66 69 6c 65 2c 20 65  ach open file, e
ce520 76 65 6e 20 77 68 65 6e 20 69 74 20 69 73 20 6f  ven when it is o
ce530 70 65 6e 65 64 20 6d 75 6c 74 69 70 6c 65 20 74  pened multiple t
ce540 69 6d 65 73 2e 0a 2a 2a 20 54 68 65 20 63 6c 6f  imes..** The clo
ce550 73 65 28 29 20 73 79 73 74 65 6d 20 63 61 6c 6c  se() system call
ce560 20 77 6f 75 6c 64 20 6f 6e 6c 79 20 6f 63 63 75   would only occu
ce570 72 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20  r when the last 
ce580 64 61 74 61 62 61 73 65 0a 2a 2a 20 75 73 69 6e  database.** usin
ce590 67 20 74 68 65 20 66 69 6c 65 20 63 6c 6f 73 65  g the file close
ce5a0 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 75 6e 69  s..*/.struct uni
ce5b0 78 4f 70 65 6e 43 6e 74 20 7b 0a 20 20 73 74 72  xOpenCnt {.  str
ce5c0 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20 66  uct unixFileId f
ce5d0 69 6c 65 49 64 3b 20 20 20 2f 2a 20 54 68 65 20  ileId;   /* The 
ce5e0 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a 20 20  lookup key */.  
ce5f0 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20  int nRef;       
ce600 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
ce610 75 6d 62 65 72 20 6f 66 20 70 6f 69 6e 74 65 72  umber of pointer
ce620 73 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74  s to this struct
ce630 75 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 6f  ure */.  int nLo
ce640 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ck;             
ce650 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
ce660 66 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f  f outstanding lo
ce670 63 6b 73 20 2a 2f 0a 20 20 55 6e 69 78 55 6e 75  cks */.  UnixUnu
ce680 73 65 64 46 64 20 2a 70 55 6e 75 73 65 64 3b 20  sedFd *pUnused; 
ce690 20 20 20 20 20 2f 2a 20 55 6e 75 73 65 64 20 66       /* Unused f
ce6a0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20  ile descriptors 
ce6b0 74 6f 20 63 6c 6f 73 65 20 2a 2f 0a 23 69 66 20  to close */.#if 
ce6c0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 65 6d  OS_VXWORKS.  sem
ce6d0 5f 74 20 2a 70 53 65 6d 3b 20 20 20 20 20 20 20  _t *pSem;       
ce6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
ce6f0 20 4e 61 6d 65 64 20 50 4f 53 49 58 20 73 65 6d   Named POSIX sem
ce700 61 70 68 6f 72 65 20 2a 2f 0a 20 20 63 68 61 72  aphore */.  char
ce710 20 61 53 65 6d 4e 61 6d 65 5b 4d 41 58 5f 50 41   aSemName[MAX_PA
ce720 54 48 4e 41 4d 45 2b 32 5d 3b 20 20 20 2f 2a 20  THNAME+2];   /* 
ce730 4e 61 6d 65 20 6f 66 20 74 68 61 74 20 73 65 6d  Name of that sem
ce740 61 70 68 6f 72 65 20 2a 2f 0a 23 65 6e 64 69 66  aphore */.#endif
ce750 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70  .  struct unixOp
ce760 65 6e 43 6e 74 20 2a 70 4e 65 78 74 2c 20 2a 70  enCnt *pNext, *p
ce770 50 72 65 76 3b 20 20 20 2f 2a 20 4c 69 73 74 20  Prev;   /* List 
ce780 6f 66 20 61 6c 6c 20 75 6e 69 78 4f 70 65 6e 43  of all unixOpenC
ce790 6e 74 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 7d 3b  nt objects */.};
ce7a0 0a 0a 2f 2a 0a 2a 2a 20 4c 69 73 74 73 20 6f 66  ../*.** Lists of
ce7b0 20 61 6c 6c 20 75 6e 69 78 4c 6f 63 6b 49 6e 66   all unixLockInf
ce7c0 6f 20 61 6e 64 20 75 6e 69 78 4f 70 65 6e 43 6e  o and unixOpenCn
ce7d0 74 20 6f 62 6a 65 63 74 73 2e 20 20 54 68 65 73  t objects.  Thes
ce7e0 65 20 75 73 65 64 20 74 6f 20 62 65 20 68 61 73  e used to be has
ce7f0 68 0a 2a 2a 20 74 61 62 6c 65 73 2e 20 20 42 75  h.** tables.  Bu
ce800 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
ce810 6f 62 6a 65 63 74 73 20 69 73 20 72 61 72 65 6c  objects is rarel
ce820 79 20 6d 6f 72 65 20 74 68 61 6e 20 61 20 64 6f  y more than a do
ce830 7a 65 6e 20 61 6e 64 0a 2a 2a 20 6e 65 76 65 72  zen and.** never
ce840 20 65 78 63 65 65 64 73 20 61 20 66 65 77 20 74   exceeds a few t
ce850 68 6f 75 73 61 6e 64 2e 20 20 41 6e 64 20 6c 6f  housand.  And lo
ce860 6f 6b 75 70 20 69 73 20 6e 6f 74 20 6f 6e 20 61  okup is not on a
ce870 20 63 72 69 74 69 63 61 6c 0a 2a 2a 20 70 61 74   critical.** pat
ce880 68 20 73 6f 20 61 20 73 69 6d 70 6c 65 20 6c 69  h so a simple li
ce890 6e 6b 65 64 20 6c 69 73 74 20 77 69 6c 6c 20 73  nked list will s
ce8a0 75 66 66 69 63 65 2e 0a 2a 2f 0a 73 74 61 74 69  uffice..*/.stati
ce8b0 63 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63  c struct unixLoc
ce8c0 6b 49 6e 66 6f 20 2a 6c 6f 63 6b 4c 69 73 74 20  kInfo *lockList 
ce8d0 3d 20 30 3b 0a 73 74 61 74 69 63 20 73 74 72 75  = 0;.static stru
ce8e0 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a  ct unixOpenCnt *
ce8f0 6f 70 65 6e 4c 69 73 74 20 3d 20 30 3b 0a 0a 2f  openList = 0;../
ce900 2a 0a 2a 2a 20 54 68 69 73 20 76 61 72 69 61 62  *.** This variab
ce910 6c 65 20 72 65 6d 65 6d 62 65 72 73 20 77 68 65  le remembers whe
ce920 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65  ther or not thre
ce930 61 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65  ads can override
ce940 20 65 61 63 68 20 6f 74 68 65 72 73 0a 2a 2a 20   each others.** 
ce950 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  locks..**.**    
ce960 30 3a 20 20 4e 6f 2e 20 20 54 68 72 65 61 64 73  0:  No.  Threads
ce970 20 63 61 6e 6e 6f 74 20 6f 76 65 72 72 69 64 65   cannot override
ce980 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63   each others loc
ce990 6b 73 2e 20 20 28 4c 69 6e 75 78 54 68 72 65 61  ks.  (LinuxThrea
ce9a0 64 73 29 0a 2a 2a 20 20 20 20 31 3a 20 20 59 65  ds).**    1:  Ye
ce9b0 73 2e 20 20 54 68 72 65 61 64 73 20 63 61 6e 20  s.  Threads can 
ce9c0 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 6f 74  override each ot
ce9d0 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20 28 50 6f  hers locks.  (Po
ce9e0 73 69 78 20 26 20 4e 4c 50 54 29 0a 2a 2a 20 20  six & NLPT).**  
ce9f0 20 2d 31 3a 20 20 57 65 20 64 6f 6e 27 74 20 6b   -1:  We don't k
cea00 6e 6f 77 20 79 65 74 2e 0a 2a 2a 0a 2a 2a 20 4f  now yet..**.** O
cea10 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c 20  n some systems, 
cea20 77 65 20 6b 6e 6f 77 20 61 74 20 63 6f 6d 70 69  we know at compi
cea30 6c 65 2d 74 69 6d 65 20 69 66 20 74 68 72 65 61  le-time if threa
cea40 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20  ds can override 
cea50 65 61 63 68 0a 2a 2a 20 6f 74 68 65 72 73 20 6c  each.** others l
cea60 6f 63 6b 73 2e 20 20 4f 6e 20 74 68 6f 73 65 20  ocks.  On those 
cea70 73 79 73 74 65 6d 73 2c 20 74 68 65 20 53 51 4c  systems, the SQL
cea80 49 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52  ITE_THREAD_OVERR
cea90 49 44 45 5f 4c 4f 43 4b 20 6d 61 63 72 6f 0a 2a  IDE_LOCK macro.*
ceaa0 2a 20 77 69 6c 6c 20 62 65 20 73 65 74 20 61 70  * will be set ap
ceab0 70 72 6f 70 72 69 61 74 65 6c 79 2e 20 20 4f 6e  propriately.  On
ceac0 20 6f 74 68 65 72 20 73 79 73 74 65 6d 73 2c 20   other systems, 
cead0 77 65 20 68 61 76 65 20 74 6f 20 63 68 65 63 6b  we have to check
ceae0 20 61 74 0a 2a 2a 20 72 75 6e 74 69 6d 65 2e 20   at.** runtime. 
ceaf0 20 4f 6e 20 74 68 65 73 65 20 6c 61 74 74 65 72   On these latter
ceb00 20 73 79 73 74 65 6d 73 2c 20 53 51 4c 54 49 45   systems, SQLTIE
ceb10 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45  _THREAD_OVERRIDE
ceb20 5f 4c 4f 43 4b 20 69 73 0a 2a 2a 20 75 6e 64 65  _LOCK is.** unde
ceb30 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  fined..**.** Thi
ceb40 73 20 76 61 72 69 61 62 6c 65 20 6e 6f 72 6d 61  s variable norma
ceb50 6c 6c 79 20 68 61 73 20 66 69 6c 65 20 73 63 6f  lly has file sco
ceb60 70 65 20 6f 6e 6c 79 2e 20 20 42 75 74 20 64 75  pe only.  But du
ceb70 72 69 6e 67 20 74 65 73 74 69 6e 67 2c 20 77 65  ring testing, we
ceb80 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 61 20 67 6c   make.** it a gl
ceb90 6f 62 61 6c 20 73 6f 20 74 68 61 74 20 74 68 65  obal so that the
ceba0 20 74 65 73 74 20 63 6f 64 65 20 63 61 6e 20 63   test code can c
cebb0 68 61 6e 67 65 20 69 74 73 20 76 61 6c 75 65 20  hange its value 
cebc0 69 6e 20 6f 72 64 65 72 20 74 6f 20 76 65 72 69  in order to veri
cebd0 66 79 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 72  fy.** that the r
cebe0 69 67 68 74 20 73 74 75 66 66 20 68 61 70 70 65  ight stuff happe
cebf0 6e 73 20 69 6e 20 65 69 74 68 65 72 20 63 61 73  ns in either cas
cec00 65 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  e..*/.#if SQLITE
cec10 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 64  _THREADSAFE && d
cec20 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f  efined(__linux__
cec30 29 0a 23 20 20 69 66 6e 64 65 66 20 53 51 4c 49  ).#  ifndef SQLI
cec40 54 45 5f 54 48 52 45 41 44 5f 4f 56 45 52 52 49  TE_THREAD_OVERRI
cec50 44 45 5f 4c 4f 43 4b 0a 23 20 20 20 20 64 65 66  DE_LOCK.#    def
cec60 69 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41  ine SQLITE_THREA
cec70 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20  D_OVERRIDE_LOCK 
cec80 2d 31 0a 23 20 20 65 6e 64 69 66 0a 23 20 20 69  -1.#  endif.#  i
cec90 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
ceca0 0a 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65 72  .int threadsOver
cecb0 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f  rideEachOthersLo
cecc0 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 52  cks = SQLITE_THR
cecd0 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43  EAD_OVERRIDE_LOC
cece0 4b 3b 0a 23 20 20 65 6c 73 65 0a 73 74 61 74 69  K;.#  else.stati
cecf0 63 20 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65  c int threadsOve
ced00 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c  rrideEachOthersL
ced10 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48  ocks = SQLITE_TH
ced20 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f  READ_OVERRIDE_LO
ced30 43 4b 3b 0a 23 20 20 65 6e 64 69 66 0a 23 65 6e  CK;.#  endif.#en
ced40 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  dif../*.** This 
ced50 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20  structure holds 
ced60 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73  information pass
ced70 65 64 20 69 6e 74 6f 20 69 6e 64 69 76 69 64 75  ed into individu
ced80 61 6c 20 74 65 73 74 0a 2a 2a 20 74 68 72 65 61  al test.** threa
ced90 64 73 20 62 79 20 74 68 65 20 74 65 73 74 54 68  ds by the testTh
ceda0 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61 76  readLockingBehav
cedb0 69 6f 72 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a  ior() routine..*
cedc0 2f 0a 73 74 72 75 63 74 20 74 68 72 65 61 64 54  /.struct threadT
cedd0 65 73 74 44 61 74 61 20 7b 0a 20 20 69 6e 74 20  estData {.  int 
cede0 66 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  fd;             
cedf0 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 62 65     /* File to be
cee00 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 73 74 72   locked */.  str
cee10 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 20  uct flock lock; 
cee20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 69      /* The locki
cee30 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a  ng operation */.
cee40 20 20 69 6e 74 20 72 65 73 75 6c 74 3b 20 20 20    int result;   
cee50 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75           /* Resu
cee60 6c 74 20 6f 66 20 74 68 65 20 6c 6f 63 6b 69 6e  lt of the lockin
cee70 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 7d  g operation */.}
cee80 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  ;..#if SQLITE_TH
cee90 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69  READSAFE && defi
ceea0 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 2f  ned(__linux__)./
ceeb0 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
ceec0 6f 6e 20 69 73 20 75 73 65 64 20 61 73 20 74 68  on is used as th
ceed0 65 20 6d 61 69 6e 20 72 6f 75 74 69 6e 65 20 66  e main routine f
ceee0 6f 72 20 61 20 74 68 72 65 61 64 20 6c 61 75 6e  or a thread laun
ceef0 63 68 65 64 20 62 79 0a 2a 2a 20 74 65 73 74 54  ched by.** testT
cef00 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61  hreadLockingBeha
cef10 76 69 6f 72 28 29 2e 20 49 74 20 74 65 73 74 73  vior(). It tests
cef20 20 77 68 65 74 68 65 72 20 74 68 65 20 73 68 61   whether the sha
cef30 72 65 64 2d 6c 6f 63 6b 20 6f 62 74 61 69 6e 65  red-lock obtaine
cef40 64 0a 2a 2a 20 62 79 20 74 68 65 20 6d 61 69 6e  d.** by the main
cef50 20 74 68 72 65 61 64 20 69 6e 20 74 65 73 74 54   thread in testT
cef60 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68 61  hreadLockingBeha
cef70 76 69 6f 72 28 29 20 63 6f 6e 66 6c 69 63 74 73  vior() conflicts
cef80 20 77 69 74 68 20 61 0a 2a 2a 20 68 79 70 6f 74   with a.** hypot
cef90 68 65 74 69 63 61 6c 20 77 72 69 74 65 2d 6c 6f  hetical write-lo
cefa0 63 6b 20 6f 62 74 61 69 6e 65 64 20 62 79 20 74  ck obtained by t
cefb0 68 69 73 20 74 68 72 65 61 64 20 6f 6e 20 74 68  his thread on th
cefc0 65 20 73 61 6d 65 20 66 69 6c 65 2e 0a 2a 2a 0a  e same file..**.
cefd0 2a 2a 20 54 68 65 20 77 72 69 74 65 2d 6c 6f 63  ** The write-loc
cefe0 6b 20 69 73 20 6e 6f 74 20 61 63 74 75 61 6c 6c  k is not actuall
ceff0 79 20 61 63 71 75 69 72 65 64 2c 20 61 73 20 74  y acquired, as t
cf000 68 69 73 20 69 73 20 6e 6f 74 20 70 6f 73 73 69  his is not possi
cf010 62 6c 65 20 69 66 20 0a 2a 2a 20 74 68 65 20 66  ble if .** the f
cf020 69 6c 65 20 69 73 20 6f 70 65 6e 20 69 6e 20 72  ile is open in r
cf030 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20 28 73  ead-only mode (s
cf040 65 65 20 74 69 63 6b 65 74 20 23 33 34 37 32 29  ee ticket #3472)
cf050 2e 0a 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f 69  ..*/ .static voi
cf060 64 20 2a 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67  d *threadLocking
cf070 54 65 73 74 28 76 6f 69 64 20 2a 70 41 72 67 29  Test(void *pArg)
cf080 7b 0a 20 20 73 74 72 75 63 74 20 74 68 72 65 61  {.  struct threa
cf090 64 54 65 73 74 44 61 74 61 20 2a 70 44 61 74 61  dTestData *pData
cf0a0 20 3d 20 28 73 74 72 75 63 74 20 74 68 72 65 61   = (struct threa
cf0b0 64 54 65 73 74 44 61 74 61 2a 29 70 41 72 67 3b  dTestData*)pArg;
cf0c0 0a 20 20 70 44 61 74 61 2d 3e 72 65 73 75 6c 74  .  pData->result
cf0d0 20 3d 20 66 63 6e 74 6c 28 70 44 61 74 61 2d 3e   = fcntl(pData->
cf0e0 66 64 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 70 44  fd, F_GETLK, &pD
cf0f0 61 74 61 2d 3e 6c 6f 63 6b 29 3b 0a 20 20 72 65  ata->lock);.  re
cf100 74 75 72 6e 20 70 41 72 67 3b 0a 7d 0a 23 65 6e  turn pArg;.}.#en
cf110 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 48  dif /* SQLITE_TH
cf120 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66 69  READSAFE && defi
cf130 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 20 2a  ned(__linux__) *
cf140 2f 0a 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  /...#if SQLITE_T
cf150 48 52 45 41 44 53 41 46 45 20 26 26 20 64 65 66  HREADSAFE && def
cf160 69 6e 65 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a  ined(__linux__).
cf170 2f 2a 0a 2a 2a 20 54 68 69 73 20 70 72 6f 63 65  /*.** This proce
cf180 64 75 72 65 20 61 74 74 65 6d 70 74 73 20 74 6f  dure attempts to
cf190 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68   determine wheth
cf1a0 65 72 20 6f 72 20 6e 6f 74 20 74 68 72 65 61 64  er or not thread
cf1b0 73 0a 2a 2a 20 63 61 6e 20 6f 76 65 72 72 69 64  s.** can overrid
cf1c0 65 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f  e each others lo
cf1d0 63 6b 73 20 74 68 65 6e 20 73 65 74 73 20 74 68  cks then sets th
cf1e0 65 20 0a 2a 2a 20 74 68 72 65 61 64 73 4f 76 65  e .** threadsOve
cf1f0 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c  rrideEachOthersL
cf200 6f 63 6b 73 20 76 61 72 69 61 62 6c 65 20 61 70  ocks variable ap
cf210 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a  propriately..*/.
cf220 73 74 61 74 69 63 20 76 6f 69 64 20 74 65 73 74  static void test
cf230 54 68 72 65 61 64 4c 6f 63 6b 69 6e 67 42 65 68  ThreadLockingBeh
cf240 61 76 69 6f 72 28 69 6e 74 20 66 64 5f 6f 72 69  avior(int fd_ori
cf250 67 29 7b 0a 20 20 69 6e 74 20 66 64 3b 0a 20 20  g){.  int fd;.  
cf260 69 6e 74 20 72 63 3b 0a 20 20 73 74 72 75 63 74  int rc;.  struct
cf270 20 74 68 72 65 61 64 54 65 73 74 44 61 74 61 20   threadTestData 
cf280 64 3b 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63  d;.  struct floc
cf290 6b 20 6c 3b 0a 20 20 70 74 68 72 65 61 64 5f 74  k l;.  pthread_t
cf2a0 20 74 3b 0a 0a 20 20 66 64 20 3d 20 64 75 70 28   t;..  fd = dup(
cf2b0 66 64 5f 6f 72 69 67 29 3b 0a 20 20 69 66 28 20  fd_orig);.  if( 
cf2c0 66 64 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  fd<0 ) return;. 
cf2d0 20 6d 65 6d 73 65 74 28 26 6c 2c 20 30 2c 20 73   memset(&l, 0, s
cf2e0 69 7a 65 6f 66 28 6c 29 29 3b 0a 20 20 6c 2e 6c  izeof(l));.  l.l
cf2f0 5f 74 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b  _type = F_RDLCK;
cf300 0a 20 20 6c 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a  .  l.l_len = 1;.
cf310 20 20 6c 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b    l.l_start = 0;
cf320 0a 20 20 6c 2e 6c 5f 77 68 65 6e 63 65 20 3d 20  .  l.l_whence = 
cf330 53 45 45 4b 5f 53 45 54 3b 0a 20 20 72 63 20 3d  SEEK_SET;.  rc =
cf340 20 66 63 6e 74 6c 28 66 64 5f 6f 72 69 67 2c 20   fcntl(fd_orig, 
cf350 46 5f 53 45 54 4c 4b 2c 20 26 6c 29 3b 0a 20 20  F_SETLK, &l);.  
cf360 69 66 28 20 72 63 21 3d 30 20 29 20 72 65 74 75  if( rc!=0 ) retu
cf370 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 2c  rn;.  memset(&d,
cf380 20 30 2c 20 73 69 7a 65 6f 66 28 64 29 29 3b 0a   0, sizeof(d));.
cf390 20 20 64 2e 66 64 20 3d 20 66 64 3b 0a 20 20 64    d.fd = fd;.  d
cf3a0 2e 6c 6f 63 6b 20 3d 20 6c 3b 0a 20 20 64 2e 6c  .lock = l;.  d.l
cf3b0 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57  ock.l_type = F_W
cf3c0 52 4c 43 4b 3b 0a 20 20 69 66 28 20 70 74 68 72  RLCK;.  if( pthr
cf3d0 65 61 64 5f 63 72 65 61 74 65 28 26 74 2c 20 30  ead_create(&t, 0
cf3e0 2c 20 74 68 72 65 61 64 4c 6f 63 6b 69 6e 67 54  , threadLockingT
cf3f0 65 73 74 2c 20 26 64 29 3d 3d 30 20 29 7b 0a 20  est, &d)==0 ){. 
cf400 20 20 20 70 74 68 72 65 61 64 5f 6a 6f 69 6e 28     pthread_join(
cf410 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 63 6c 6f  t, 0);.  }.  clo
cf420 73 65 28 66 64 29 3b 0a 20 20 69 66 28 20 64 2e  se(fd);.  if( d.
cf430 72 65 73 75 6c 74 21 3d 30 20 29 20 72 65 74 75  result!=0 ) retu
cf440 72 6e 3b 0a 20 20 74 68 72 65 61 64 73 4f 76 65  rn;.  threadsOve
cf450 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c  rrideEachOthersL
cf460 6f 63 6b 73 20 3d 20 28 64 2e 6c 6f 63 6b 2e 6c  ocks = (d.lock.l
cf470 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 29 3b  _type==F_UNLCK);
cf480 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
cf490 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 26  ITE_THREADSAFE &
cf4a0 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e 75  & defined(__linu
cf4b0 78 5f 5f 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52  x__) */../*.** R
cf4c0 65 6c 65 61 73 65 20 61 20 75 6e 69 78 4c 6f 63  elease a unixLoc
cf4d0 6b 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  kInfo structure 
cf4e0 70 72 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63  previously alloc
cf4f0 61 74 65 64 20 62 79 20 66 69 6e 64 4c 6f 63 6b  ated by findLock
cf500 49 6e 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  Info()..**.** Th
cf510 65 20 6d 75 74 65 78 20 65 6e 74 65 72 65 64 20  e mutex entered 
cf520 75 73 69 6e 67 20 74 68 65 20 75 6e 69 78 45 6e  using the unixEn
cf530 74 65 72 4d 75 74 65 78 28 29 20 66 75 6e 63 74  terMutex() funct
cf540 69 6f 6e 20 6d 75 73 74 20 62 65 20 68 65 6c 64  ion must be held
cf550 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 66 75  .** when this fu
cf560 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
cf570 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
cf580 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f   releaseLockInfo
cf590 28 73 74 72 75 63 74 20 75 6e 69 78 4c 6f 63 6b  (struct unixLock
cf5a0 49 6e 66 6f 20 2a 70 4c 6f 63 6b 29 7b 0a 20 20  Info *pLock){.  
cf5b0 61 73 73 65 72 74 28 20 75 6e 69 78 4d 75 74 65  assert( unixMute
cf5c0 78 48 65 6c 64 28 29 20 29 3b 0a 20 20 69 66 28  xHeld() );.  if(
cf5d0 20 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c   pLock ){.    pL
cf5e0 6f 63 6b 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20  ock->nRef--;.   
cf5f0 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 6e 52 65 66   if( pLock->nRef
cf600 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ==0 ){.      if(
cf610 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 20 29 7b   pLock->pPrev ){
cf620 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
cf630 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e 70   pLock->pPrev->p
cf640 4e 65 78 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20  Next==pLock );. 
cf650 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 50         pLock->pP
cf660 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4c 6f  rev->pNext = pLo
cf670 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  ck->pNext;.     
cf680 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
cf690 61 73 73 65 72 74 28 20 6c 6f 63 6b 4c 69 73 74  assert( lockList
cf6a0 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20 20  ==pLock );.     
cf6b0 20 20 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70 4c     lockList = pL
cf6c0 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  ock->pNext;.    
cf6d0 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4c    }.      if( pL
cf6e0 6f 63 6b 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20  ock->pNext ){.  
cf6f0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c        assert( pL
cf700 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65  ock->pNext->pPre
cf710 76 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20  v==pLock );.    
cf720 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74      pLock->pNext
cf730 2d 3e 70 50 72 65 76 20 3d 20 70 4c 6f 63 6b 2d  ->pPrev = pLock-
cf740 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a  >pPrev;.      }.
cf750 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
cf760 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 7d  ee(pLock);.    }
cf770 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
cf780 6c 65 61 73 65 20 61 20 75 6e 69 78 4f 70 65 6e  lease a unixOpen
cf790 43 6e 74 20 73 74 72 75 63 74 75 72 65 20 70 72  Cnt structure pr
cf7a0 65 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74  eviously allocat
cf7b0 65 64 20 62 79 20 66 69 6e 64 4c 6f 63 6b 49 6e  ed by findLockIn
cf7c0 66 6f 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  fo()..**.** The 
cf7d0 6d 75 74 65 78 20 65 6e 74 65 72 65 64 20 75 73  mutex entered us
cf7e0 69 6e 67 20 74 68 65 20 75 6e 69 78 45 6e 74 65  ing the unixEnte
cf7f0 72 4d 75 74 65 78 28 29 20 66 75 6e 63 74 69 6f  rMutex() functio
cf800 6e 20 6d 75 73 74 20 62 65 20 68 65 6c 64 0a 2a  n must be held.*
cf810 2a 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63  * when this func
cf820 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a  tion is called..
cf830 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72  */.static void r
cf840 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28 73 74  eleaseOpenCnt(st
cf850 72 75 63 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74  ruct unixOpenCnt
cf860 20 2a 70 4f 70 65 6e 29 7b 0a 20 20 61 73 73 65   *pOpen){.  asse
cf870 72 74 28 20 75 6e 69 78 4d 75 74 65 78 48 65 6c  rt( unixMutexHel
cf880 64 28 29 20 29 3b 0a 20 20 69 66 28 20 70 4f 70  d() );.  if( pOp
cf890 65 6e 20 29 7b 0a 20 20 20 20 70 4f 70 65 6e 2d  en ){.    pOpen-
cf8a0 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28  >nRef--;.    if(
cf8b0 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 3d 3d 30 20   pOpen->nRef==0 
cf8c0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70  ){.      if( pOp
cf8d0 65 6e 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20  en->pPrev ){.   
cf8e0 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
cf8f0 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74  en->pPrev->pNext
cf900 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20  ==pOpen );.     
cf910 20 20 20 70 4f 70 65 6e 2d 3e 70 50 72 65 76 2d     pOpen->pPrev-
cf920 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65 6e 2d 3e  >pNext = pOpen->
cf930 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 65 6c  pNext;.      }el
cf940 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65  se{.        asse
cf950 72 74 28 20 6f 70 65 6e 4c 69 73 74 3d 3d 70 4f  rt( openList==pO
cf960 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 6f  pen );.        o
cf970 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e 2d  penList = pOpen-
cf980 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a  >pNext;.      }.
cf990 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e 2d        if( pOpen-
cf9a0 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 20 20  >pNext ){.      
cf9b0 20 20 61 73 73 65 72 74 28 20 70 4f 70 65 6e 2d    assert( pOpen-
cf9c0 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d 3d 70  >pNext->pPrev==p
cf9d0 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 20 20  Open );.        
cf9e0 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e 70 50  pOpen->pNext->pP
cf9f0 72 65 76 20 3d 20 70 4f 70 65 6e 2d 3e 70 50 72  rev = pOpen->pPr
cfa00 65 76 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 20  ev;.      }.#if 
cfa10 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
cfa20 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c  E && defined(__l
cfa30 69 6e 75 78 5f 5f 29 0a 20 20 20 20 20 20 61 73  inux__).      as
cfa40 73 65 72 74 28 20 21 70 4f 70 65 6e 2d 3e 70 55  sert( !pOpen->pU
cfa50 6e 75 73 65 64 20 7c 7c 20 74 68 72 65 61 64 73  nused || threads
cfa60 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65  OverrideEachOthe
cfa70 72 73 4c 6f 63 6b 73 3d 3d 30 20 29 3b 0a 23 65  rsLocks==0 );.#e
cfa80 6e 64 69 66 0a 0a 20 20 20 20 20 20 2f 2a 20 49  ndif..      /* I
cfa90 66 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64  f pOpen->pUnused
cfaa0 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 2c 20 74 68   is not null, th
cfab0 65 6e 20 6d 65 6d 6f 72 79 20 61 6e 64 20 66 69  en memory and fi
cfac0 6c 65 2d 64 65 73 63 72 69 70 74 6f 72 73 0a 20  le-descriptors. 
cfad0 20 20 20 20 20 2a 2a 20 61 72 65 20 6c 65 61 6b       ** are leak
cfae0 65 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  ed..      **.   
cfaf0 20 20 20 2a 2a 20 54 68 69 73 20 77 69 6c 6c 20     ** This will 
cfb00 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 66 2c 20  only happen if, 
cfb10 75 6e 64 65 72 20 4c 69 6e 75 78 74 68 72 65 61  under Linuxthrea
cfb20 64 73 2c 20 74 68 65 20 75 73 65 72 20 68 61 73  ds, the user has
cfb30 20 6f 70 65 6e 65 64 0a 20 20 20 20 20 20 2a 2a   opened.      **
cfb40 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
cfb50 6e 20 6f 6e 65 20 74 68 72 65 61 64 2c 20 74 68  n one thread, th
cfb60 65 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 63  en attempts to c
cfb70 6c 6f 73 65 20 74 68 65 20 64 61 74 61 62 61 73  lose the databas
cfb80 65 0a 20 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c  e.      ** handl
cfb90 65 20 66 72 6f 6d 20 61 6e 6f 74 68 65 72 20 74  e from another t
cfba0 68 72 65 61 64 20 28 77 69 74 68 6f 75 74 20 66  hread (without f
cfbb0 69 72 73 74 20 75 6e 6c 6f 63 6b 69 6e 67 20 74  irst unlocking t
cfbc0 68 65 20 64 62 20 66 69 6c 65 29 2e 0a 20 20 20  he db file)..   
cfbd0 20 20 20 2a 2a 20 54 68 69 73 20 69 73 20 61 20     ** This is a 
cfbe0 6d 69 73 75 73 65 2e 20 20 2a 2f 0a 20 20 20 20  misuse.  */.    
cfbf0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
cfc00 4f 70 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Open);.    }.  }
cfc10 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20  .}../*.** Given 
cfc20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  a file descripto
cfc30 72 2c 20 6c 6f 63 61 74 65 20 75 6e 69 78 4c 6f  r, locate unixLo
cfc40 63 6b 49 6e 66 6f 20 61 6e 64 20 75 6e 69 78 4f  ckInfo and unixO
cfc50 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65  penCnt structure
cfc60 73 20 74 68 61 74 0a 2a 2a 20 64 65 73 63 72 69  s that.** descri
cfc70 62 65 73 20 74 68 61 74 20 66 69 6c 65 20 64 65  bes that file de
cfc80 73 63 72 69 70 74 6f 72 2e 20 20 43 72 65 61 74  scriptor.  Creat
cfc90 65 20 6e 65 77 20 6f 6e 65 73 20 69 66 20 6e 65  e new ones if ne
cfca0 63 65 73 73 61 72 79 2e 20 20 54 68 65 0a 2a 2a  cessary.  The.**
cfcb0 20 72 65 74 75 72 6e 20 76 61 6c 75 65 73 20 6d   return values m
cfcc0 69 67 68 74 20 62 65 20 75 6e 69 6e 69 74 69 61  ight be uninitia
cfcd0 6c 69 7a 65 64 20 69 66 20 61 6e 20 65 72 72 6f  lized if an erro
cfce0 72 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20  r occurs..**.** 
cfcf0 54 68 65 20 6d 75 74 65 78 20 65 6e 74 65 72 65  The mutex entere
cfd00 64 20 75 73 69 6e 67 20 74 68 65 20 75 6e 69 78  d using the unix
cfd10 45 6e 74 65 72 4d 75 74 65 78 28 29 20 66 75 6e  EnterMutex() fun
cfd20 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 68 65  ction must be he
cfd30 6c 64 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20  ld.** when this 
cfd40 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
cfd50 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ed..**.** Return
cfd60 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20   an appropriate 
cfd70 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73  error code..*/.s
cfd80 74 61 74 69 63 20 69 6e 74 20 66 69 6e 64 4c 6f  tatic int findLo
cfd90 63 6b 49 6e 66 6f 28 0a 20 20 75 6e 69 78 46 69  ckInfo(.  unixFi
cfda0 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20 20 20  le *pFile,      
cfdb0 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e 69 78           /* Unix
cfdc0 20 66 69 6c 65 20 77 69 74 68 20 66 69 6c 65 20   file with file 
cfdd0 64 65 73 63 20 75 73 65 64 20 69 6e 20 74 68 65  desc used in the
cfde0 20 6b 65 79 20 2a 2f 0a 20 20 73 74 72 75 63 74   key */.  struct
cfdf0 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 2a   unixLockInfo **
cfe00 70 70 4c 6f 63 6b 2c 20 20 2f 2a 20 52 65 74 75  ppLock,  /* Retu
cfe10 72 6e 20 74 68 65 20 75 6e 69 78 4c 6f 63 6b 49  rn the unixLockI
cfe20 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 68 65  nfo structure he
cfe30 72 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 75  re */.  struct u
cfe40 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 2a 70 70 4f  nixOpenCnt **ppO
cfe50 70 65 6e 20 20 20 20 2f 2a 20 52 65 74 75 72 6e  pen    /* Return
cfe60 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74   the unixOpenCnt
cfe70 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65 20   structure here 
cfe80 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20  */.){.  int rc; 
cfe90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cfea0 20 20 20 20 20 20 20 2f 2a 20 53 79 73 74 65 6d         /* System
cfeb0 20 63 61 6c 6c 20 72 65 74 75 72 6e 20 63 6f 64   call return cod
cfec0 65 20 2a 2f 0a 20 20 69 6e 74 20 66 64 3b 20 20  e */.  int fd;  
cfed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cfee0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c        /* The fil
cfef0 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72  e descriptor for
cff00 20 70 46 69 6c 65 20 2a 2f 0a 20 20 73 74 72 75   pFile */.  stru
cff10 63 74 20 75 6e 69 78 4c 6f 63 6b 4b 65 79 20 6c  ct unixLockKey l
cff20 6f 63 6b 4b 65 79 3b 20 20 20 20 2f 2a 20 4c 6f  ockKey;    /* Lo
cff30 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20 74 68 65  okup key for the
cff40 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 73 74   unixLockInfo st
cff50 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 73 74 72  ructure */.  str
cff60 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20 66  uct unixFileId f
cff70 69 6c 65 49 64 3b 20 20 20 20 20 20 2f 2a 20 4c  ileId;      /* L
cff80 6f 6f 6b 75 70 20 6b 65 79 20 66 6f 72 20 74 68  ookup key for th
cff90 65 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 73 74  e unixOpenCnt st
cffa0 72 75 63 74 20 2a 2f 0a 20 20 73 74 72 75 63 74  ruct */.  struct
cffb0 20 73 74 61 74 20 73 74 61 74 62 75 66 3b 20 20   stat statbuf;  
cffc0 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 2d           /* Low-
cffd0 6c 65 76 65 6c 20 66 69 6c 65 20 69 6e 66 6f 72  level file infor
cffe0 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75  mation */.  stru
cfff0 63 74 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20  ct unixLockInfo 
d0000 2a 70 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 43 61  *pLock = 0;/* Ca
d0010 6e 64 69 64 61 74 65 20 75 6e 69 78 4c 6f 63 6b  ndidate unixLock
d0020 49 6e 66 6f 20 6f 62 6a 65 63 74 20 2a 2f 0a 20  Info object */. 
d0030 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e   struct unixOpen
d0040 43 6e 74 20 2a 70 4f 70 65 6e 3b 20 20 20 20 20  Cnt *pOpen;     
d0050 2f 2a 20 43 61 6e 64 69 64 61 74 65 20 75 6e 69  /* Candidate uni
d0060 78 4f 70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 20  xOpenCnt object 
d0070 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 75 6e  */..  assert( un
d0080 69 78 4d 75 74 65 78 48 65 6c 64 28 29 20 29 3b  ixMutexHeld() );
d0090 0a 0a 20 20 2f 2a 20 47 65 74 20 6c 6f 77 2d 6c  ..  /* Get low-l
d00a0 65 76 65 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  evel information
d00b0 20 61 62 6f 75 74 20 74 68 65 20 66 69 6c 65 20   about the file 
d00c0 74 68 61 74 20 77 65 20 63 61 6e 20 75 73 65 64  that we can used
d00d0 20 74 6f 0a 20 20 2a 2a 20 63 72 65 61 74 65 20   to.  ** create 
d00e0 61 20 75 6e 69 71 75 65 20 6e 61 6d 65 20 66 6f  a unique name fo
d00f0 72 20 74 68 65 20 66 69 6c 65 2e 0a 20 20 2a 2f  r the file..  */
d0100 0a 20 20 66 64 20 3d 20 70 46 69 6c 65 2d 3e 68  .  fd = pFile->h
d0110 3b 0a 20 20 72 63 20 3d 20 66 73 74 61 74 28 66  ;.  rc = fstat(f
d0120 64 2c 20 26 73 74 61 74 62 75 66 29 3b 0a 20 20  d, &statbuf);.  
d0130 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20 20  if( rc!=0 ){.   
d0140 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
d0150 6f 20 3d 20 65 72 72 6e 6f 3b 0a 23 69 66 64 65  o = errno;.#ifde
d0160 66 20 45 4f 56 45 52 46 4c 4f 57 0a 20 20 20 20  f EOVERFLOW.    
d0170 69 66 28 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45  if( pFile->lastE
d0180 72 72 6e 6f 3d 3d 45 4f 56 45 52 46 4c 4f 57 20  rrno==EOVERFLOW 
d0190 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
d01a0 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69 66 0a 20 20  NOLFS;.#endif.  
d01b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d01c0 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a 23 69 66 64  IOERR;.  }..#ifd
d01d0 65 66 20 5f 5f 41 50 50 4c 45 5f 5f 0a 20 20 2f  ef __APPLE__.  /
d01e0 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e 20 61 6e 20  * On OS X on an 
d01f0 6d 73 64 6f 73 20 66 69 6c 65 73 79 73 74 65 6d  msdos filesystem
d0200 2c 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62  , the inode numb
d0210 65 72 20 69 73 20 72 65 70 6f 72 74 65 64 0a 20  er is reported. 
d0220 20 2a 2a 20 69 6e 63 6f 72 72 65 63 74 6c 79 20   ** incorrectly 
d0230 66 6f 72 20 7a 65 72 6f 2d 73 69 7a 65 20 66 69  for zero-size fi
d0240 6c 65 73 2e 20 20 53 65 65 20 74 69 63 6b 65 74  les.  See ticket
d0250 20 23 33 32 36 30 2e 20 20 54 6f 20 77 6f 72 6b   #3260.  To work
d0260 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 20 74 68 69  .  ** around thi
d0270 73 20 70 72 6f 62 6c 65 6d 20 28 77 65 20 63 6f  s problem (we co
d0280 6e 73 69 64 65 72 20 69 74 20 61 20 62 75 67 20  nsider it a bug 
d0290 69 6e 20 4f 53 20 58 2c 20 6e 6f 74 20 53 51 4c  in OS X, not SQL
d02a0 69 74 65 29 0a 20 20 2a 2a 20 77 65 20 61 6c 77  ite).  ** we alw
d02b0 61 79 73 20 69 6e 63 72 65 61 73 65 20 74 68 65  ays increase the
d02c0 20 66 69 6c 65 20 73 69 7a 65 20 74 6f 20 31 20   file size to 1 
d02d0 62 79 20 77 72 69 74 69 6e 67 20 61 20 73 69 6e  by writing a sin
d02e0 67 6c 65 20 62 79 74 65 0a 20 20 2a 2a 20 70 72  gle byte.  ** pr
d02f0 69 6f 72 20 74 6f 20 61 63 63 65 73 73 69 6e 67  ior to accessing
d0300 20 74 68 65 20 69 6e 6f 64 65 20 6e 75 6d 62 65   the inode numbe
d0310 72 2e 20 20 54 68 65 20 6f 6e 65 20 62 79 74 65  r.  The one byte
d0320 20 77 72 69 74 74 65 6e 20 69 73 0a 20 20 2a 2a   written is.  **
d0330 20 61 6e 20 41 53 43 49 49 20 27 53 27 20 63 68   an ASCII 'S' ch
d0340 61 72 61 63 74 65 72 20 77 68 69 63 68 20 61 6c  aracter which al
d0350 73 6f 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65  so happens to be
d0360 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 0a   the first byte.
d0370 20 20 2a 2a 20 69 6e 20 74 68 65 20 68 65 61 64    ** in the head
d0380 65 72 20 6f 66 20 65 76 65 72 79 20 53 51 4c 69  er of every SQLi
d0390 74 65 20 64 61 74 61 62 61 73 65 2e 20 20 49 6e  te database.  In
d03a0 20 74 68 69 73 20 77 61 79 2c 20 69 66 20 74 68   this way, if th
d03b0 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 20 72 61  ere.  ** is a ra
d03c0 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20 73 75 63  ce condition suc
d03d0 68 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74  h that another t
d03e0 68 72 65 61 64 20 68 61 73 20 61 6c 72 65 61 64  hread has alread
d03f0 79 20 70 6f 70 75 6c 61 74 65 64 0a 20 20 2a 2a  y populated.  **
d0400 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20   the first page 
d0410 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  of the database,
d0420 20 6e 6f 20 64 61 6d 61 67 65 20 69 73 20 64 6f   no damage is do
d0430 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  ne..  */.  if( s
d0440 74 61 74 62 75 66 2e 73 74 5f 73 69 7a 65 3d 3d  tatbuf.st_size==
d0450 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 77 72  0 ){.    rc = wr
d0460 69 74 65 28 66 64 2c 20 22 53 22 2c 20 31 29 3b  ite(fd, "S", 1);
d0470 0a 20 20 20 20 69 66 28 20 72 63 21 3d 31 20 29  .    if( rc!=1 )
d0480 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
d0490 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 20  QLITE_IOERR;.   
d04a0 20 7d 0a 20 20 20 20 72 63 20 3d 20 66 73 74 61   }.    rc = fsta
d04b0 74 28 66 64 2c 20 26 73 74 61 74 62 75 66 29 3b  t(fd, &statbuf);
d04c0 0a 20 20 20 20 69 66 28 20 72 63 21 3d 30 20 29  .    if( rc!=0 )
d04d0 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  {.      pFile->l
d04e0 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  astErrno = errno
d04f0 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
d0500 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 20  QLITE_IOERR;.   
d0510 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20   }.  }.#endif.. 
d0520 20 6d 65 6d 73 65 74 28 26 6c 6f 63 6b 4b 65 79   memset(&lockKey
d0530 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6c 6f 63 6b  , 0, sizeof(lock
d0540 4b 65 79 29 29 3b 0a 20 20 6c 6f 63 6b 4b 65 79  Key));.  lockKey
d0550 2e 66 69 64 2e 64 65 76 20 3d 20 73 74 61 74 62  .fid.dev = statb
d0560 75 66 2e 73 74 5f 64 65 76 3b 0a 23 69 66 20 4f  uf.st_dev;.#if O
d0570 53 5f 56 58 57 4f 52 4b 53 0a 20 20 6c 6f 63 6b  S_VXWORKS.  lock
d0580 4b 65 79 2e 66 69 64 2e 70 49 64 20 3d 20 70 46  Key.fid.pId = pF
d0590 69 6c 65 2d 3e 70 49 64 3b 0a 23 65 6c 73 65 0a  ile->pId;.#else.
d05a0 20 20 6c 6f 63 6b 4b 65 79 2e 66 69 64 2e 69 6e    lockKey.fid.in
d05b0 6f 20 3d 20 73 74 61 74 62 75 66 2e 73 74 5f 69  o = statbuf.st_i
d05c0 6e 6f 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 53  no;.#endif.#if S
d05d0 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
d05e0 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 6c 69   && defined(__li
d05f0 6e 75 78 5f 5f 29 0a 20 20 69 66 28 20 74 68 72  nux__).  if( thr
d0600 65 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68  eadsOverrideEach
d0610 4f 74 68 65 72 73 4c 6f 63 6b 73 3c 30 20 29 7b  OthersLocks<0 ){
d0620 0a 20 20 20 20 74 65 73 74 54 68 72 65 61 64 4c  .    testThreadL
d0630 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 66  ockingBehavior(f
d0640 64 29 3b 0a 20 20 7d 0a 20 20 6c 6f 63 6b 4b 65  d);.  }.  lockKe
d0650 79 2e 74 69 64 20 3d 20 74 68 72 65 61 64 73 4f  y.tid = threadsO
d0660 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65 72  verrideEachOther
d0670 73 4c 6f 63 6b 73 20 3f 20 30 20 3a 20 70 74 68  sLocks ? 0 : pth
d0680 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 23 65 6e  read_self();.#en
d0690 64 69 66 0a 20 20 66 69 6c 65 49 64 20 3d 20 6c  dif.  fileId = l
d06a0 6f 63 6b 4b 65 79 2e 66 69 64 3b 0a 20 20 69 66  ockKey.fid;.  if
d06b0 28 20 70 70 4c 6f 63 6b 21 3d 30 20 29 7b 0a 20  ( ppLock!=0 ){. 
d06c0 20 20 20 70 4c 6f 63 6b 20 3d 20 6c 6f 63 6b 4c     pLock = lockL
d06d0 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  ist;.    while( 
d06e0 70 4c 6f 63 6b 20 26 26 20 6d 65 6d 63 6d 70 28  pLock && memcmp(
d06f0 26 6c 6f 63 6b 4b 65 79 2c 20 26 70 4c 6f 63 6b  &lockKey, &pLock
d0700 2d 3e 6c 6f 63 6b 4b 65 79 2c 20 73 69 7a 65 6f  ->lockKey, sizeo
d0710 66 28 6c 6f 63 6b 4b 65 79 29 29 20 29 7b 0a 20  f(lockKey)) ){. 
d0720 20 20 20 20 20 70 4c 6f 63 6b 20 3d 20 70 4c 6f       pLock = pLo
d0730 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d  ck->pNext;.    }
d0740 0a 20 20 20 20 69 66 28 20 70 4c 6f 63 6b 3d 3d  .    if( pLock==
d0750 30 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63 6b  0 ){.      pLock
d0760 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
d0770 63 28 20 73 69 7a 65 6f 66 28 2a 70 4c 6f 63 6b  c( sizeof(*pLock
d0780 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  ) );.      if( p
d0790 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Lock==0 ){.     
d07a0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
d07b0 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 67 6f  OMEM;.        go
d07c0 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b  to exit_findlock
d07d0 69 6e 66 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20  info;.      }.  
d07e0 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4c 6f 63      memcpy(&pLoc
d07f0 6b 2d 3e 6c 6f 63 6b 4b 65 79 2c 26 6c 6f 63 6b  k->lockKey,&lock
d0800 4b 65 79 2c 73 69 7a 65 6f 66 28 6c 6f 63 6b 4b  Key,sizeof(lockK
d0810 65 79 29 29 3b 0a 20 20 20 20 20 20 70 4c 6f 63  ey));.      pLoc
d0820 6b 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20  k->nRef = 1;.   
d0830 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20 3d 20     pLock->cnt = 
d0840 30 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e  0;.      pLock->
d0850 6c 6f 63 6b 74 79 70 65 20 3d 20 30 3b 0a 20 20  locktype = 0;.  
d0860 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74      pLock->pNext
d0870 20 3d 20 6c 6f 63 6b 4c 69 73 74 3b 0a 20 20 20   = lockList;.   
d0880 20 20 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 20     pLock->pPrev 
d0890 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 6c  = 0;.      if( l
d08a0 6f 63 6b 4c 69 73 74 20 29 20 6c 6f 63 6b 4c 69  ockList ) lockLi
d08b0 73 74 2d 3e 70 50 72 65 76 20 3d 20 70 4c 6f 63  st->pPrev = pLoc
d08c0 6b 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 4c 69 73  k;.      lockLis
d08d0 74 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 20 20 7d  t = pLock;.    }
d08e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4c 6f 63  else{.      pLoc
d08f0 6b 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d  k->nRef++;.    }
d0900 0a 20 20 20 20 2a 70 70 4c 6f 63 6b 20 3d 20 70  .    *ppLock = p
d0910 4c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Lock;.  }.  if( 
d0920 70 70 4f 70 65 6e 21 3d 30 20 29 7b 0a 20 20 20  ppOpen!=0 ){.   
d0930 20 70 4f 70 65 6e 20 3d 20 6f 70 65 6e 4c 69 73   pOpen = openLis
d0940 74 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 4f  t;.    while( pO
d0950 70 65 6e 20 26 26 20 6d 65 6d 63 6d 70 28 26 66  pen && memcmp(&f
d0960 69 6c 65 49 64 2c 20 26 70 4f 70 65 6e 2d 3e 66  ileId, &pOpen->f
d0970 69 6c 65 49 64 2c 20 73 69 7a 65 6f 66 28 66 69  ileId, sizeof(fi
d0980 6c 65 49 64 29 29 20 29 7b 0a 20 20 20 20 20 20  leId)) ){.      
d0990 70 4f 70 65 6e 20 3d 20 70 4f 70 65 6e 2d 3e 70  pOpen = pOpen->p
d09a0 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Next;.    }.    
d09b0 69 66 28 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a  if( pOpen==0 ){.
d09c0 20 20 20 20 20 20 70 4f 70 65 6e 20 3d 20 73 71        pOpen = sq
d09d0 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69  lite3_malloc( si
d09e0 7a 65 6f 66 28 2a 70 4f 70 65 6e 29 20 29 3b 0a  zeof(*pOpen) );.
d09f0 20 20 20 20 20 20 69 66 28 20 70 4f 70 65 6e 3d        if( pOpen=
d0a00 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  =0 ){.        re
d0a10 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 4c  leaseLockInfo(pL
d0a20 6f 63 6b 29 3b 0a 20 20 20 20 20 20 20 20 72 63  ock);.        rc
d0a30 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
d0a40 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 78  .        goto ex
d0a50 69 74 5f 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3b  it_findlockinfo;
d0a60 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6d  .      }.      m
d0a70 65 6d 73 65 74 28 70 4f 70 65 6e 2c 20 30 2c 20  emset(pOpen, 0, 
d0a80 73 69 7a 65 6f 66 28 2a 70 4f 70 65 6e 29 29 3b  sizeof(*pOpen));
d0a90 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 66 69  .      pOpen->fi
d0aa0 6c 65 49 64 20 3d 20 66 69 6c 65 49 64 3b 0a 20  leId = fileId;. 
d0ab0 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66       pOpen->nRef
d0ac0 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 65   = 1;.      pOpe
d0ad0 6e 2d 3e 70 4e 65 78 74 20 3d 20 6f 70 65 6e 4c  n->pNext = openL
d0ae0 69 73 74 3b 0a 20 20 20 20 20 20 69 66 28 20 6f  ist;.      if( o
d0af0 70 65 6e 4c 69 73 74 20 29 20 6f 70 65 6e 4c 69  penList ) openLi
d0b00 73 74 2d 3e 70 50 72 65 76 20 3d 20 70 4f 70 65  st->pPrev = pOpe
d0b10 6e 3b 0a 20 20 20 20 20 20 6f 70 65 6e 4c 69 73  n;.      openLis
d0b20 74 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 20 20 7d  t = pOpen;.    }
d0b30 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4f 70 65  else{.      pOpe
d0b40 6e 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 7d  n->nRef++;.    }
d0b50 0a 20 20 20 20 2a 70 70 4f 70 65 6e 20 3d 20 70  .    *ppOpen = p
d0b60 4f 70 65 6e 3b 0a 20 20 7d 0a 0a 65 78 69 74 5f  Open;.  }..exit_
d0b70 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3a 0a 20 20  findlockinfo:.  
d0b80 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
d0b90 0a 2a 2a 20 49 66 20 77 65 20 61 72 65 20 63 75  .** If we are cu
d0ba0 72 72 65 6e 74 6c 79 20 69 6e 20 61 20 64 69 66  rrently in a dif
d0bb0 66 65 72 65 6e 74 20 74 68 72 65 61 64 20 74 68  ferent thread th
d0bc0 61 6e 20 74 68 65 20 74 68 72 65 61 64 20 74 68  an the thread th
d0bd0 61 74 20 74 68 65 0a 2a 2a 20 75 6e 69 78 46 69  at the.** unixFi
d0be0 6c 65 20 61 72 67 75 6d 65 6e 74 20 62 65 6c 6f  le argument belo
d0bf0 6e 67 73 20 74 6f 2c 20 74 68 65 6e 20 74 72 61  ngs to, then tra
d0c00 6e 73 66 65 72 20 6f 77 6e 65 72 73 68 69 70 20  nsfer ownership 
d0c10 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 0a  of the unixFile.
d0c20 2a 2a 20 6f 76 65 72 20 74 6f 20 74 68 65 20 63  ** over to the c
d0c30 75 72 72 65 6e 74 20 74 68 72 65 61 64 2e 0a 2a  urrent thread..*
d0c40 2a 0a 2a 2a 20 41 20 75 6e 69 78 46 69 6c 65 20  *.** A unixFile 
d0c50 69 73 20 6f 6e 6c 79 20 6f 77 6e 65 64 20 62 79  is only owned by
d0c60 20 61 20 74 68 72 65 61 64 20 6f 6e 20 73 79 73   a thread on sys
d0c70 74 65 6d 73 20 74 68 61 74 20 75 73 65 20 4c 69  tems that use Li
d0c80 6e 75 78 54 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a  nuxThreads..**.*
d0c90 2a 20 4f 77 6e 65 72 73 68 69 70 20 74 72 61 6e  * Ownership tran
d0ca0 73 66 65 72 20 69 73 20 6f 6e 6c 79 20 61 6c 6c  sfer is only all
d0cb0 6f 77 65 64 20 69 66 20 74 68 65 20 75 6e 69 78  owed if the unix
d0cc0 46 69 6c 65 20 69 73 20 63 75 72 72 65 6e 74 6c  File is currentl
d0cd0 79 20 75 6e 6c 6f 63 6b 65 64 2e 0a 2a 2a 20 49  y unlocked..** I
d0ce0 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 20 69  f the unixFile i
d0cf0 73 20 6c 6f 63 6b 65 64 20 61 6e 64 20 61 6e 20  s locked and an 
d0d00 6f 77 6e 65 72 73 68 69 70 20 69 73 20 77 72 6f  ownership is wro
d0d10 6e 67 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 0a  ng, then return.
d0d20 2a 2a 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  ** SQLITE_MISUSE
d0d30 2e 20 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  .  SQLITE_OK is 
d0d40 72 65 74 75 72 6e 65 64 20 69 66 20 65 76 65 72  returned if ever
d0d50 79 74 68 69 6e 67 20 77 6f 72 6b 73 2e 0a 2a 2f  ything works..*/
d0d60 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
d0d70 41 44 53 41 46 45 20 26 26 20 64 65 66 69 6e 65  ADSAFE && define
d0d80 64 28 5f 5f 6c 69 6e 75 78 5f 5f 29 0a 73 74 61  d(__linux__).sta
d0d90 74 69 63 20 69 6e 74 20 74 72 61 6e 73 66 65 72  tic int transfer
d0da0 4f 77 6e 65 72 73 68 69 70 28 75 6e 69 78 46 69  Ownership(unixFi
d0db0 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e  le *pFile){.  in
d0dc0 74 20 72 63 3b 0a 20 20 70 74 68 72 65 61 64 5f  t rc;.  pthread_
d0dd0 74 20 68 53 65 6c 66 3b 0a 20 20 69 66 28 20 74  t hSelf;.  if( t
d0de0 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61  hreadsOverrideEa
d0df0 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 29 7b  chOthersLocks ){
d0e00 0a 20 20 20 20 2f 2a 20 4f 77 6e 65 72 73 68 69  .    /* Ownershi
d0e10 70 20 74 72 61 6e 73 66 65 72 73 20 6e 6f 74 20  p transfers not 
d0e20 6e 65 65 64 65 64 20 6f 6e 20 74 68 69 73 20 73  needed on this s
d0e30 79 73 74 65 6d 20 2a 2f 0a 20 20 20 20 72 65 74  ystem */.    ret
d0e40 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
d0e50 20 7d 0a 20 20 68 53 65 6c 66 20 3d 20 70 74 68   }.  hSelf = pth
d0e60 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 69  read_self();.  i
d0e70 66 28 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c  f( pthread_equal
d0e80 28 70 46 69 6c 65 2d 3e 74 69 64 2c 20 68 53 65  (pFile->tid, hSe
d0e90 6c 66 29 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65  lf) ){.    /* We
d0ea0 20 61 72 65 20 73 74 69 6c 6c 20 69 6e 20 74 68   are still in th
d0eb0 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 2a 2f  e same thread */
d0ec0 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 22 4e  .    OSTRACE1("N
d0ed0 6f 2d 74 72 61 6e 73 66 65 72 2c 20 73 61 6d 65  o-transfer, same
d0ee0 20 74 68 72 65 61 64 5c 6e 22 29 3b 0a 20 20 20   thread\n");.   
d0ef0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
d0f00 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46 69  K;.  }.  if( pFi
d0f10 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f  le->locktype!=NO
d0f20 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 2f 2a 20  _LOCK ){.    /* 
d0f30 57 65 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65  We cannot change
d0f40 20 6f 77 6e 65 72 73 68 69 70 20 77 68 69 6c 65   ownership while
d0f50 20 77 65 20 61 72 65 20 68 6f 6c 64 69 6e 67 20   we are holding 
d0f60 61 20 6c 6f 63 6b 21 20 2a 2f 0a 20 20 20 20 72  a lock! */.    r
d0f70 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
d0f80 55 53 45 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41  USE;.  }.  OSTRA
d0f90 43 45 34 28 22 54 72 61 6e 73 66 65 72 20 6f 77  CE4("Transfer ow
d0fa0 6e 65 72 73 68 69 70 20 6f 66 20 25 64 20 66 72  nership of %d fr
d0fb0 6f 6d 20 25 64 20 74 6f 20 25 64 5c 6e 22 2c 0a  om %d to %d\n",.
d0fc0 20 20 20 20 20 20 20 20 20 20 20 20 70 46 69 6c              pFil
d0fd0 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 74 69 64  e->h, pFile->tid
d0fe0 2c 20 68 53 65 6c 66 29 3b 0a 20 20 70 46 69 6c  , hSelf);.  pFil
d0ff0 65 2d 3e 74 69 64 20 3d 20 68 53 65 6c 66 3b 0a  e->tid = hSelf;.
d1000 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 70 4c 6f    if (pFile->pLo
d1010 63 6b 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 20 20  ck != NULL) {.  
d1020 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e 66    releaseLockInf
d1030 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29 3b  o(pFile->pLock);
d1040 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f  .    rc = findLo
d1050 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2c 20 26 70  ckInfo(pFile, &p
d1060 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2c 20 30 29 3b  File->pLock, 0);
d1070 0a 20 20 20 20 4f 53 54 52 41 43 45 35 28 22 4c  .    OSTRACE5("L
d1080 4f 43 4b 20 20 20 20 25 64 20 69 73 20 6e 6f 77  OCK    %d is now
d1090 20 25 73 28 25 73 2c 25 64 29 5c 6e 22 2c 20 70   %s(%s,%d)\n", p
d10a0 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20  File->h,.       
d10b0 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65      locktypeName
d10c0 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65  (pFile->locktype
d10d0 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c 6f  ),.           lo
d10e0 63 6b 74 79 70 65 4e 61 6d 65 28 70 46 69 6c 65  cktypeName(pFile
d10f0 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70  ->pLock->locktyp
d1100 65 29 2c 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b  e), pFile->pLock
d1110 2d 3e 63 6e 74 29 3b 0a 20 20 20 20 72 65 74 75  ->cnt);.    retu
d1120 72 6e 20 72 63 3b 0a 20 20 7d 20 65 6c 73 65 20  rn rc;.  } else 
d1130 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
d1140 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 23 65  ITE_OK;.  }.}.#e
d1150 6c 73 65 20 20 2f 2a 20 69 66 20 6e 6f 74 20 53  lse  /* if not S
d1160 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
d1170 20 2a 2f 0a 20 20 2f 2a 20 4f 6e 20 73 69 6e 67   */.  /* On sing
d1180 6c 65 2d 74 68 72 65 61 64 65 64 20 62 75 69 6c  le-threaded buil
d1190 64 73 2c 20 6f 77 6e 65 72 73 68 69 70 20 74 72  ds, ownership tr
d11a0 61 6e 73 66 65 72 20 69 73 20 61 20 6e 6f 2d 6f  ansfer is a no-o
d11b0 70 20 2a 2f 0a 23 20 64 65 66 69 6e 65 20 74 72  p */.# define tr
d11c0 61 6e 73 66 65 72 4f 77 6e 65 72 73 68 69 70 28  ansferOwnership(
d11d0 58 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e  X) SQLITE_OK.#en
d11e0 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 48  dif /* SQLITE_TH
d11f0 52 45 41 44 53 41 46 45 20 2a 2f 0a 0a 0a 2f 2a  READSAFE */.../*
d1200 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
d1210 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65   checks if there
d1220 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c   is a RESERVED l
d1230 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20  ock held on the 
d1240 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c  specified.** fil
d1250 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79  e by this or any
d1260 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20   other process. 
d1270 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69  If such a lock i
d1280 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70 52 65  s held, set *pRe
d1290 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e 6f 6e  sOut.** to a non
d12a0 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74 68 65  -zero value othe
d12b0 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74 20 69  rwise *pResOut i
d12c0 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20 20  s set to zero.  
d12d0 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
d12e0 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20 53 51  .** is set to SQ
d12f0 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73 20 61  LITE_OK unless a
d1300 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63 63 75  n I/O error occu
d1310 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b 20 63  rs during lock c
d1320 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74  hecking..*/.stat
d1330 69 63 20 69 6e 74 20 75 6e 69 78 43 68 65 63 6b  ic int unixCheck
d1340 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c  ReservedLock(sql
d1350 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
d1360 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20  nt *pResOut){.  
d1370 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
d1380 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76  OK;.  int reserv
d1390 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69  ed = 0;.  unixFi
d13a0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
d13b0 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 53 69  xFile*)id;..  Si
d13c0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72  mulateIOError( r
d13d0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
d13e0 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44  RR_CHECKRESERVED
d13f0 4c 4f 43 4b 3b 20 29 3b 0a 0a 20 20 61 73 73 65  LOCK; );..  asse
d1400 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 75  rt( pFile );.  u
d1410 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  nixEnterMutex();
d1420 20 2f 2a 20 42 65 63 61 75 73 65 20 70 46 69 6c   /* Because pFil
d1430 65 2d 3e 70 4c 6f 63 6b 20 69 73 20 73 68 61 72  e->pLock is shar
d1440 65 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 64  ed across thread
d1450 73 20 2a 2f 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  s */..  /* Check
d1460 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e 20   if a thread in 
d1470 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f 6c  this process hol
d1480 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a  ds such a lock *
d1490 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70  /.  if( pFile->p
d14a0 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53  Lock->locktype>S
d14b0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20  HARED_LOCK ){.  
d14c0 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a    reserved = 1;.
d14d0 20 20 7d 0a 0a 20 20 2f 2a 20 4f 74 68 65 72 77    }..  /* Otherw
d14e0 69 73 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20  ise see if some 
d14f0 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f  other process ho
d1500 6c 64 73 20 69 74 2e 0a 20 20 2a 2f 0a 23 69 66  lds it..  */.#if
d1510 6e 64 65 66 20 5f 5f 44 4a 47 50 50 5f 5f 0a 20  ndef __DJGPP__. 
d1520 20 69 66 28 20 21 72 65 73 65 72 76 65 64 20 29   if( !reserved )
d1530 7b 0a 20 20 20 20 73 74 72 75 63 74 20 66 6c 6f  {.    struct flo
d1540 63 6b 20 6c 6f 63 6b 3b 0a 20 20 20 20 6c 6f 63  ck lock;.    loc
d1550 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45  k.l_whence = SEE
d1560 4b 5f 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e  K_SET;.    lock.
d1570 6c 5f 73 74 61 72 74 20 3d 20 52 45 53 45 52 56  l_start = RESERV
d1580 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63  ED_BYTE;.    loc
d1590 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 20  k.l_len = 1;.   
d15a0 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46   lock.l_type = F
d15b0 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 69 66 20 28  _WRLCK;.    if (
d15c0 2d 31 20 3d 3d 20 66 63 6e 74 6c 28 70 46 69 6c  -1 == fcntl(pFil
d15d0 65 2d 3e 68 2c 20 46 5f 47 45 54 4c 4b 2c 20 26  e->h, F_GETLK, &
d15e0 6c 6f 63 6b 29 29 20 7b 0a 20 20 20 20 20 20 69  lock)) {.      i
d15f0 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e  nt tErrno = errn
d1600 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  o;.      rc = sq
d1610 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
d1620 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
d1630 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45  SQLITE_IOERR_CHE
d1640 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 3b  CKRESERVEDLOCK);
d1650 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  .      pFile->la
d1660 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
d1670 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28  ;.    } else if(
d1680 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 21 3d 46 5f   lock.l_type!=F_
d1690 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 72  UNLCK ){.      r
d16a0 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 20  eserved = 1;.   
d16b0 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20   }.  }.#endif.  
d16c0 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65  .  unixLeaveMute
d16d0 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28  x();.  OSTRACE4(
d16e0 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64  "TEST WR-LOCK %d
d16f0 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65   %d %d\n", pFile
d1700 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65  ->h, rc, reserve
d1710 64 29 3b 0a 0a 20 20 2a 70 52 65 73 4f 75 74 20  d);..  *pResOut 
d1720 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65  = reserved;.  re
d1730 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
d1740 2a 20 50 65 72 66 6f 72 6d 20 61 20 66 69 6c 65  * Perform a file
d1750 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69   locking operati
d1760 6f 6e 20 6f 6e 20 61 20 72 61 6e 67 65 20 6f 66  on on a range of
d1770 20 62 79 74 65 73 20 69 6e 20 61 20 66 69 6c 65   bytes in a file
d1780 2e 0a 2a 2a 20 54 68 65 20 22 6f 70 22 20 70 61  ..** The "op" pa
d1790 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62  rameter should b
d17a0 65 20 6f 6e 65 20 6f 66 20 46 5f 52 44 4c 43 4b  e one of F_RDLCK
d17b0 2c 20 46 5f 57 52 4c 43 4b 2c 20 6f 72 20 46 5f  , F_WRLCK, or F_
d17c0 55 4e 4c 43 4b 2e 0a 2a 2a 20 52 65 74 75 72 6e  UNLCK..** Return
d17d0 20 30 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72   0 on success or
d17e0 20 2d 31 20 66 6f 72 20 66 61 69 6c 75 72 65 2e   -1 for failure.
d17f0 20 20 4f 6e 20 66 61 69 6c 75 72 65 2c 20 77 72    On failure, wr
d1800 69 74 65 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a  ite the error.**
d1810 20 63 6f 64 65 20 69 6e 74 6f 20 2a 70 45 72 72   code into *pErr
d1820 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  code..**.** If t
d1830 68 65 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f  he SQLITE_WHOLE_
d1840 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20 62 69 74  FILE_LOCKING bit
d1850 20 69 73 20 63 6c 65 61 72 2c 20 74 68 65 6e 20   is clear, then 
d1860 6f 6e 6c 79 20 6c 6f 63 6b 0a 2a 2a 20 74 68 65  only lock.** the
d1870 20 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 20   range of bytes 
d1880 6f 6e 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70  on the locking p
d1890 61 67 65 20 62 65 74 77 65 65 6e 20 53 48 41 52  age between SHAR
d18a0 45 44 5f 46 49 52 53 54 20 61 6e 64 0a 2a 2a 20  ED_FIRST and.** 
d18b0 53 48 41 52 45 44 5f 53 49 5a 45 2e 20 20 49 66  SHARED_SIZE.  If
d18c0 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49   SQLITE_WHOLE_FI
d18d0 4c 45 5f 4c 4f 43 4b 49 4e 47 20 69 73 20 73 65  LE_LOCKING is se
d18e0 74 2c 20 74 68 65 6e 20 6c 6f 63 6b 20 61 6c 6c  t, then lock all
d18f0 0a 2a 2a 20 62 79 74 65 73 20 66 72 6f 6d 20 30  .** bytes from 0
d1900 20 75 70 20 74 6f 20 62 75 74 20 6e 6f 74 20 69   up to but not i
d1910 6e 63 6c 75 64 69 6e 67 20 50 45 4e 44 49 4e 47  ncluding PENDING
d1920 5f 42 59 54 45 2c 20 61 6e 64 20 61 6c 6c 20 62  _BYTE, and all b
d1930 79 74 65 73 0a 2a 2a 20 74 68 61 74 20 66 6f 6c  ytes.** that fol
d1940 6c 6f 77 20 53 48 41 52 45 44 5f 46 49 52 53 54  low SHARED_FIRST
d1950 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72  ..**.** In other
d1960 20 77 6f 72 64 73 2c 20 6f 66 20 53 51 4c 49 54   words, of SQLIT
d1970 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43  E_WHOLE_FILE_LOC
d1980 4b 49 4e 47 20 69 66 20 66 61 6c 73 65 20 28 74  KING if false (t
d1990 68 65 20 68 69 73 74 6f 72 69 63 61 6c 0a 2a 2a  he historical.**
d19a0 20 64 65 66 61 75 6c 74 20 63 61 73 65 29 20 74   default case) t
d19b0 68 65 6e 20 6f 6e 6c 79 20 6c 6f 63 6b 20 61 20  hen only lock a 
d19c0 73 6d 61 6c 6c 20 72 61 6e 67 65 20 6f 66 20 62  small range of b
d19d0 79 74 65 73 20 66 72 6f 6d 20 53 48 41 52 45 44  ytes from SHARED
d19e0 5f 46 49 52 53 54 0a 2a 2a 20 74 68 72 6f 75 67  _FIRST.** throug
d19f0 68 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b 53  h SHARED_FIRST+S
d1a00 48 41 52 45 44 5f 53 49 5a 45 2d 31 2e 20 20 42  HARED_SIZE-1.  B
d1a10 75 74 20 69 66 20 53 51 4c 49 54 45 5f 57 48 4f  ut if SQLITE_WHO
d1a20 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 20  LE_FILE_LOCKING 
d1a30 69 73 0a 2a 2a 20 74 72 75 65 20 74 68 65 6e 20  is.** true then 
d1a40 6c 6f 63 6b 20 65 76 65 72 79 20 62 79 74 65 20  lock every byte 
d1a50 69 6e 20 74 68 65 20 66 69 6c 65 20 65 78 63 65  in the file exce
d1a60 70 74 20 66 6f 72 20 50 45 4e 44 49 4e 47 5f 42  pt for PENDING_B
d1a70 59 54 45 20 61 6e 64 0a 2a 2a 20 52 45 53 45 52  YTE and.** RESER
d1a80 56 45 44 5f 42 59 54 45 2e 0a 2a 2a 0a 2a 2a 20  VED_BYTE..**.** 
d1a90 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c  SQLITE_WHOLE_FIL
d1aa0 45 5f 4c 4f 43 4b 49 4e 47 3d 74 72 75 65 20 6f  E_LOCKING=true o
d1ab0 76 65 72 6c 61 70 73 20 53 51 4c 49 54 45 5f 57  verlaps SQLITE_W
d1ac0 48 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e  HOLE_FILE_LOCKIN
d1ad0 47 3d 66 61 6c 73 65 0a 2a 2a 20 61 6e 64 20 73  G=false.** and s
d1ae0 6f 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 63  o the locking sc
d1af0 68 65 6d 65 73 20 61 72 65 20 63 6f 6d 70 61 74  hemes are compat
d1b00 69 62 6c 65 2e 20 20 4f 6e 65 20 74 79 70 65 20  ible.  One type 
d1b10 6f 66 20 6c 6f 63 6b 20 77 69 6c 6c 0a 2a 2a 20  of lock will.** 
d1b20 65 66 66 65 63 74 69 76 65 6c 79 20 65 78 63 6c  effectively excl
d1b30 75 64 65 20 74 68 65 20 6f 74 68 65 72 20 74 79  ude the other ty
d1b40 70 65 2e 20 20 54 68 65 20 72 65 61 73 6f 6e 20  pe.  The reason 
d1b50 66 6f 72 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a  for using the.**
d1b60 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49   SQLITE_WHOLE_FI
d1b70 4c 45 5f 4c 4f 43 4b 49 4e 47 3d 74 72 75 65 20  LE_LOCKING=true 
d1b80 69 73 20 74 68 61 74 20 62 79 20 69 6e 64 69 63  is that by indic
d1b90 61 74 69 6e 67 20 74 68 65 20 66 75 6c 6c 20 72  ating the full r
d1ba0 61 6e 67 65 0a 2a 2a 20 6f 66 20 62 79 74 65 73  ange.** of bytes
d1bb0 20 74 6f 20 62 65 20 72 65 61 64 20 6f 72 20 77   to be read or w
d1bc0 72 69 74 74 65 6e 2c 20 77 65 20 67 69 76 65 20  ritten, we give 
d1bd0 68 69 6e 74 73 20 74 6f 20 4e 46 53 20 74 6f 20  hints to NFS to 
d1be0 68 65 6c 70 20 69 74 0a 2a 2a 20 6d 61 69 6e 74  help it.** maint
d1bf0 61 69 6e 20 63 61 63 68 65 20 63 6f 68 65 72 65  ain cache cohere
d1c00 6e 63 79 2e 20 20 4f 6e 20 74 68 65 20 6f 74 68  ncy.  On the oth
d1c10 65 72 20 68 61 6e 64 2c 20 77 68 6f 6c 65 20 66  er hand, whole f
d1c20 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 69  ile locking.** i
d1c30 73 20 73 6c 6f 77 65 72 2c 20 73 6f 20 77 65 20  s slower, so we 
d1c40 64 6f 6e 27 74 20 77 61 6e 74 20 74 6f 20 75 73  don't want to us
d1c50 65 20 69 74 20 65 78 63 65 70 74 20 66 6f 72 20  e it except for 
d1c60 4e 46 53 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  NFS..*/.static i
d1c70 6e 74 20 72 61 6e 67 65 4c 6f 63 6b 28 75 6e 69  nt rangeLock(uni
d1c80 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 69 6e  xFile *pFile, in
d1c90 74 20 6f 70 2c 20 69 6e 74 20 2a 70 45 72 72 63  t op, int *pErrc
d1ca0 6f 64 65 29 7b 0a 20 20 73 74 72 75 63 74 20 66  ode){.  struct f
d1cb0 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74  lock lock;.  int
d1cc0 20 72 63 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 74 79   rc;.  lock.l_ty
d1cd0 70 65 20 3d 20 6f 70 3b 0a 20 20 6c 6f 63 6b 2e  pe = op;.  lock.
d1ce0 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45 44  l_start = SHARED
d1cf0 5f 46 49 52 53 54 3b 0a 20 20 6c 6f 63 6b 2e 6c  _FIRST;.  lock.l
d1d00 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53  _whence = SEEK_S
d1d10 45 54 3b 0a 20 20 69 66 28 20 28 70 46 69 6c 65  ET;.  if( (pFile
d1d20 2d 3e 66 69 6c 65 46 6c 61 67 73 20 26 20 53 51  ->fileFlags & SQ
d1d30 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c 45 5f  LITE_WHOLE_FILE_
d1d40 4c 4f 43 4b 49 4e 47 29 3d 3d 30 20 29 7b 0a 20  LOCKING)==0 ){. 
d1d50 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20     lock.l_len = 
d1d60 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20 20  SHARED_SIZE;.   
d1d70 20 72 63 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c   rc = fcntl(pFil
d1d80 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26  e->h, F_SETLK, &
d1d90 6c 6f 63 6b 29 3b 0a 20 20 20 20 2a 70 45 72 72  lock);.    *pErr
d1da0 63 6f 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  code = errno;.  
d1db0 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f 63 6b 2e  }else{.    lock.
d1dc0 6c 5f 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 72  l_len = 0;.    r
d1dd0 63 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d  c = fcntl(pFile-
d1de0 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f  >h, F_SETLK, &lo
d1df0 63 6b 29 3b 0a 20 20 20 20 2a 70 45 72 72 63 6f  ck);.    *pErrco
d1e00 64 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  de = errno;.    
d1e10 69 66 28 20 4e 45 56 45 52 28 6f 70 3d 3d 46 5f  if( NEVER(op==F_
d1e20 55 4e 4c 43 4b 29 20 7c 7c 20 72 63 21 3d 28 2d  UNLCK) || rc!=(-
d1e30 31 29 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b  1) ){.      lock
d1e40 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a 20 20  .l_start = 0;.  
d1e50 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d      lock.l_len =
d1e60 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20   PENDING_BYTE;. 
d1e70 20 20 20 20 20 72 63 20 3d 20 66 63 6e 74 6c 28       rc = fcntl(
d1e80 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c  pFile->h, F_SETL
d1e90 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 20  K, &lock);.     
d1ea0 20 69 66 28 20 41 4c 57 41 59 53 28 6f 70 21 3d   if( ALWAYS(op!=
d1eb0 46 5f 55 4e 4c 43 4b 29 20 26 26 20 72 63 3d 3d  F_UNLCK) && rc==
d1ec0 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20  (-1) ){.        
d1ed0 2a 70 45 72 72 63 6f 64 65 20 3d 20 65 72 72 6e  *pErrcode = errn
d1ee0 6f 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e  o;.        lock.
d1ef0 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43 4b  l_type = F_UNLCK
d1f00 3b 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c  ;.        lock.l
d1f10 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45 44 5f  _start = SHARED_
d1f20 46 49 52 53 54 3b 0a 20 20 20 20 20 20 20 20 6c  FIRST;.        l
d1f30 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 3b 0a 20  ock.l_len = 0;. 
d1f40 20 20 20 20 20 20 20 66 63 6e 74 6c 28 70 46 69         fcntl(pFi
d1f50 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20  le->h, F_SETLK, 
d1f60 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 7d 0a  &lock);.      }.
d1f70 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
d1f80 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
d1f90 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69  Lock the file wi
d1fa0 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63  th the lock spec
d1fb0 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74  ified by paramet
d1fc0 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e  er locktype - on
d1fd0 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c  e.** of the foll
d1fe0 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  owing:.**.**    
d1ff0 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b   (1) SHARED_LOCK
d2000 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45  .**     (2) RESE
d2010 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  RVED_LOCK.**    
d2020 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43   (3) PENDING_LOC
d2030 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43  K.**     (4) EXC
d2040 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a  LUSIVE_LOCK.**.*
d2050 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e  * Sometimes when
d2060 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20   requesting one 
d2070 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69  lock state, addi
d2080 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74  tional lock stat
d2090 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74  es.** are insert
d20a0 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20  ed in between.  
d20b0 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68  The locking migh
d20c0 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66  t fail on one of
d20d0 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72   the later.** tr
d20e0 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e  ansitions leavin
d20f0 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65  g the lock state
d2100 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20   different from 
d2110 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20  what it started 
d2120 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f  but.** still sho
d2130 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20  rt of its goal. 
d2140 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
d2150 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61  hart shows the a
d2160 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69  llowed.** transi
d2170 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e  tions and the in
d2180 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69  serted intermedi
d2190 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a  ate states:.**.*
d21a0 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e  *    UNLOCKED ->
d21b0 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48   SHARED.**    SH
d21c0 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44  ARED -> RESERVED
d21d0 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e  .**    SHARED ->
d21e0 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58   (PENDING) -> EX
d21f0 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45  CLUSIVE.**    RE
d2200 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49  SERVED -> (PENDI
d2210 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  NG) -> EXCLUSIVE
d2220 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d  .**    PENDING -
d2230 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a  > EXCLUSIVE.**.*
d2240 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * This routine w
d2250 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73  ill only increas
d2260 65 20 61 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74  e a lock.  Use t
d2270 68 65 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f  he sqlite3OsUnlo
d2280 63 6b 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  ck().** routine 
d2290 74 6f 20 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69  to lower a locki
d22a0 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61  ng level..*/.sta
d22b0 74 69 63 20 69 6e 74 20 75 6e 69 78 4c 6f 63 6b  tic int unixLock
d22c0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
d22d0 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29  d, int locktype)
d22e0 7b 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f  {.  /* The follo
d22f0 77 69 6e 67 20 64 65 73 63 72 69 62 65 73 20 74  wing describes t
d2300 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
d2310 6e 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73  n of the various
d2320 20 6c 6f 63 6b 73 20 61 6e 64 0a 20 20 2a 2a 20   locks and.  ** 
d2330 6c 6f 63 6b 20 74 72 61 6e 73 69 74 69 6f 6e 73  lock transitions
d2340 20 69 6e 20 74 65 72 6d 73 20 6f 66 20 74 68 65   in terms of the
d2350 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20   POSIX advisory 
d2360 73 68 61 72 65 64 20 61 6e 64 20 65 78 63 6c 75  shared and exclu
d2370 73 69 76 65 0a 20 20 2a 2a 20 6c 6f 63 6b 20 70  sive.  ** lock p
d2380 72 69 6d 69 74 69 76 65 73 20 28 63 61 6c 6c 65  rimitives (calle
d2390 64 20 72 65 61 64 2d 6c 6f 63 6b 73 20 61 6e 64  d read-locks and
d23a0 20 77 72 69 74 65 2d 6c 6f 63 6b 73 20 62 65 6c   write-locks bel
d23b0 6f 77 2c 20 74 6f 20 61 76 6f 69 64 0a 20 20 2a  ow, to avoid.  *
d23c0 2a 20 63 6f 6e 66 75 73 69 6f 6e 20 77 69 74 68  * confusion with
d23d0 20 53 51 4c 69 74 65 20 6c 6f 63 6b 20 6e 61 6d   SQLite lock nam
d23e0 65 73 29 2e 20 54 68 65 20 61 6c 67 6f 72 69 74  es). The algorit
d23f0 68 6d 73 20 61 72 65 20 63 6f 6d 70 6c 69 63 61  hms are complica
d2400 74 65 64 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c  ted.  ** slightl
d2410 79 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 62 65  y in order to be
d2420 20 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68   compatible with
d2430 20 77 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73   windows systems
d2440 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 0a   simultaneously.
d2450 20 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 74    ** accessing t
d2460 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
d2470 20 66 69 6c 65 2c 20 69 6e 20 63 61 73 65 20 74   file, in case t
d2480 68 61 74 20 69 73 20 65 76 65 72 20 72 65 71 75  hat is ever requ
d2490 69 72 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ired..  **.  ** 
d24a0 53 79 6d 62 6f 6c 73 20 64 65 66 69 6e 65 64 20  Symbols defined 
d24b0 69 6e 20 6f 73 2e 68 20 69 6e 64 65 6e 74 69 66  in os.h indentif
d24c0 79 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20 62  y the 'pending b
d24d0 79 74 65 27 20 61 6e 64 20 74 68 65 20 27 72 65  yte' and the 're
d24e0 73 65 72 76 65 64 0a 20 20 2a 2a 20 62 79 74 65  served.  ** byte
d24f0 27 2c 20 65 61 63 68 20 73 69 6e 67 6c 65 20 62  ', each single b
d2500 79 74 65 73 20 61 74 20 77 65 6c 6c 20 6b 6e 6f  ytes at well kno
d2510 77 6e 20 6f 66 66 73 65 74 73 2c 20 61 6e 64 20  wn offsets, and 
d2520 74 68 65 20 27 73 68 61 72 65 64 20 62 79 74 65  the 'shared byte
d2530 0a 20 20 2a 2a 20 72 61 6e 67 65 27 2c 20 61 20  .  ** range', a 
d2540 72 61 6e 67 65 20 6f 66 20 35 31 30 20 62 79 74  range of 510 byt
d2550 65 73 20 61 74 20 61 20 77 65 6c 6c 20 6b 6e 6f  es at a well kno
d2560 77 6e 20 6f 66 66 73 65 74 2e 0a 20 20 2a 2a 0a  wn offset..  **.
d2570 20 20 2a 2a 20 54 6f 20 6f 62 74 61 69 6e 20 61    ** To obtain a
d2580 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 61 20   SHARED lock, a 
d2590 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74  read-lock is obt
d25a0 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 27 70 65  ained on the 'pe
d25b0 6e 64 69 6e 67 0a 20 20 2a 2a 20 62 79 74 65 27  nding.  ** byte'
d25c0 2e 20 20 49 66 20 74 68 69 73 20 69 73 20 73 75  .  If this is su
d25d0 63 63 65 73 73 66 75 6c 2c 20 61 20 72 61 6e 64  ccessful, a rand
d25e0 6f 6d 20 62 79 74 65 20 66 72 6f 6d 20 74 68 65  om byte from the
d25f0 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20   'shared byte.  
d2600 2a 2a 20 72 61 6e 67 65 27 20 69 73 20 72 65 61  ** range' is rea
d2610 64 2d 6c 6f 63 6b 65 64 20 61 6e 64 20 74 68 65  d-locked and the
d2620 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 27 70 65   lock on the 'pe
d2630 6e 64 69 6e 67 20 62 79 74 65 27 20 72 65 6c 65  nding byte' rele
d2640 61 73 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ased..  **.  ** 
d2650 41 20 70 72 6f 63 65 73 73 20 6d 61 79 20 6f 6e  A process may on
d2660 6c 79 20 6f 62 74 61 69 6e 20 61 20 52 45 53 45  ly obtain a RESE
d2670 52 56 45 44 20 6c 6f 63 6b 20 61 66 74 65 72 20  RVED lock after 
d2680 69 74 20 68 61 73 20 61 20 53 48 41 52 45 44 20  it has a SHARED 
d2690 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 41 20 52 45 53  lock..  ** A RES
d26a0 45 52 56 45 44 20 6c 6f 63 6b 20 69 73 20 69 6d  ERVED lock is im
d26b0 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 67 72 61  plemented by gra
d26c0 62 62 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f  bbing a write-lo
d26d0 63 6b 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 27  ck on the.  ** '
d26e0 72 65 73 65 72 76 65 64 20 62 79 74 65 27 2e 20  reserved byte'. 
d26f0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f  .  **.  ** A pro
d2700 63 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62  cess may only ob
d2710 74 61 69 6e 20 61 20 50 45 4e 44 49 4e 47 20 6c  tain a PENDING l
d2720 6f 63 6b 20 61 66 74 65 72 20 69 74 20 68 61 73  ock after it has
d2730 20 6f 62 74 61 69 6e 65 64 20 61 0a 20 20 2a 2a   obtained a.  **
d2740 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20 41 20   SHARED lock. A 
d2750 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20  PENDING lock is 
d2760 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 6f  implemented by o
d2770 62 74 61 69 6e 69 6e 67 20 61 20 77 72 69 74 65  btaining a write
d2780 2d 6c 6f 63 6b 0a 20 20 2a 2a 20 6f 6e 20 74 68  -lock.  ** on th
d2790 65 20 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27  e 'pending byte'
d27a0 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 74  . This ensures t
d27b0 68 61 74 20 6e 6f 20 6e 65 77 20 53 48 41 52 45  hat no new SHARE
d27c0 44 20 6c 6f 63 6b 73 20 63 61 6e 20 62 65 0a 20  D locks can be. 
d27d0 20 2a 2a 20 6f 62 74 61 69 6e 65 64 2c 20 62 75   ** obtained, bu
d27e0 74 20 65 78 69 73 74 69 6e 67 20 53 48 41 52 45  t existing SHARE
d27f0 44 20 6c 6f 63 6b 73 20 61 72 65 20 61 6c 6c 6f  D locks are allo
d2800 77 65 64 20 74 6f 20 70 65 72 73 69 73 74 2e 20  wed to persist. 
d2810 41 20 70 72 6f 63 65 73 73 0a 20 20 2a 2a 20 64  A process.  ** d
d2820 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 20  oes not have to 
d2830 6f 62 74 61 69 6e 20 61 20 52 45 53 45 52 56 45  obtain a RESERVE
d2840 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 77 61  D lock on the wa
d2850 79 20 74 6f 20 61 20 50 45 4e 44 49 4e 47 20 6c  y to a PENDING l
d2860 6f 63 6b 2e 0a 20 20 2a 2a 20 54 68 69 73 20 70  ock..  ** This p
d2870 72 6f 70 65 72 74 79 20 69 73 20 75 73 65 64 20  roperty is used 
d2880 62 79 20 74 68 65 20 61 6c 67 6f 72 69 74 68 6d  by the algorithm
d2890 20 66 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63   for rolling bac
d28a0 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  k a journal file
d28b0 0a 20 20 2a 2a 20 61 66 74 65 72 20 61 20 63 72  .  ** after a cr
d28c0 61 73 68 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41  ash..  **.  ** A
d28d0 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  n EXCLUSIVE lock
d28e0 2c 20 6f 62 74 61 69 6e 65 64 20 61 66 74 65 72  , obtained after
d28f0 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20   a PENDING lock 
d2900 69 73 20 68 65 6c 64 2c 20 69 73 0a 20 20 2a 2a  is held, is.  **
d2910 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20   implemented by 
d2920 6f 62 74 61 69 6e 69 6e 67 20 61 20 77 72 69 74  obtaining a writ
d2930 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 65 6e  e-lock on the en
d2940 74 69 72 65 20 27 73 68 61 72 65 64 20 62 79 74  tire 'shared byt
d2950 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 2e 20 53  e.  ** range'. S
d2960 69 6e 63 65 20 61 6c 6c 20 6f 74 68 65 72 20 6c  ince all other l
d2970 6f 63 6b 73 20 72 65 71 75 69 72 65 20 61 20 72  ocks require a r
d2980 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 6f 6e 65 20  ead-lock on one 
d2990 6f 66 20 74 68 65 20 62 79 74 65 73 0a 20 20 2a  of the bytes.  *
d29a0 2a 20 77 69 74 68 69 6e 20 74 68 69 73 20 72 61  * within this ra
d29b0 6e 67 65 2c 20 74 68 69 73 20 65 6e 73 75 72 65  nge, this ensure
d29c0 73 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20  s that no other 
d29d0 6c 6f 63 6b 73 20 61 72 65 20 68 65 6c 64 20 6f  locks are held o
d29e0 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62  n the.  ** datab
d29f0 61 73 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ase. .  **.  ** 
d2a00 54 68 65 20 72 65 61 73 6f 6e 20 61 20 73 69 6e  The reason a sin
d2a10 67 6c 65 20 62 79 74 65 20 63 61 6e 6e 6f 74 20  gle byte cannot 
d2a20 62 65 20 75 73 65 64 20 69 6e 73 74 65 61 64 20  be used instead 
d2a30 6f 66 20 74 68 65 20 27 73 68 61 72 65 64 20 62  of the 'shared b
d2a40 79 74 65 0a 20 20 2a 2a 20 72 61 6e 67 65 27 20  yte.  ** range' 
d2a50 69 73 20 74 68 61 74 20 73 6f 6d 65 20 76 65 72  is that some ver
d2a60 73 69 6f 6e 73 20 6f 66 20 77 69 6e 64 6f 77 73  sions of windows
d2a70 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20   do not support 
d2a80 72 65 61 64 2d 6c 6f 63 6b 73 2e 20 42 79 0a 20  read-locks. By. 
d2a90 20 2a 2a 20 6c 6f 63 6b 69 6e 67 20 61 20 72 61   ** locking a ra
d2aa0 6e 64 6f 6d 20 62 79 74 65 20 66 72 6f 6d 20 61  ndom byte from a
d2ab0 20 72 61 6e 67 65 2c 20 63 6f 6e 63 75 72 72 65   range, concurre
d2ac0 6e 74 20 53 48 41 52 45 44 20 6c 6f 63 6b 73 20  nt SHARED locks 
d2ad0 6d 61 79 20 65 78 69 73 74 0a 20 20 2a 2a 20 65  may exist.  ** e
d2ae0 76 65 6e 20 69 66 20 74 68 65 20 6c 6f 63 6b 69  ven if the locki
d2af0 6e 67 20 70 72 69 6d 69 74 69 76 65 20 75 73 65  ng primitive use
d2b00 64 20 69 73 20 61 6c 77 61 79 73 20 61 20 77 72  d is always a wr
d2b10 69 74 65 2d 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20  ite-lock..  */. 
d2b20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
d2b30 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20  _OK;.  unixFile 
d2b40 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
d2b50 6c 65 2a 29 69 64 3b 0a 20 20 73 74 72 75 63 74  le*)id;.  struct
d2b60 20 75 6e 69 78 4c 6f 63 6b 49 6e 66 6f 20 2a 70   unixLockInfo *p
d2b70 4c 6f 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c  Lock = pFile->pL
d2b80 6f 63 6b 3b 0a 20 20 73 74 72 75 63 74 20 66 6c  ock;.  struct fl
d2b90 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69 6e 74 20  ock lock;.  int 
d2ba0 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20 74 45 72  s = 0;.  int tEr
d2bb0 72 6e 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  rno;..  assert( 
d2bc0 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41  pFile );.  OSTRA
d2bd0 43 45 37 28 22 4c 4f 43 4b 20 20 20 20 25 64 20  CE7("LOCK    %d 
d2be0 25 73 20 77 61 73 20 25 73 28 25 73 2c 25 64 29  %s was %s(%s,%d)
d2bf0 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c   pid=%d\n", pFil
d2c00 65 2d 3e 68 2c 0a 20 20 20 20 20 20 6c 6f 63 6b  e->h,.      lock
d2c10 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70  typeName(locktyp
d2c20 65 29 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65  e), locktypeName
d2c30 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65  (pFile->locktype
d2c40 29 2c 0a 20 20 20 20 20 20 6c 6f 63 6b 74 79 70  ),.      locktyp
d2c50 65 4e 61 6d 65 28 70 4c 6f 63 6b 2d 3e 6c 6f 63  eName(pLock->loc
d2c60 6b 74 79 70 65 29 2c 20 70 4c 6f 63 6b 2d 3e 63  ktype), pLock->c
d2c70 6e 74 20 2c 20 67 65 74 70 69 64 28 29 29 3b 0a  nt , getpid());.
d2c80 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69  .  /* If there i
d2c90 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b  s already a lock
d2ca0 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 6f 72   of this type or
d2cb0 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 69 76   more restrictiv
d2cc0 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 75 6e  e on the.  ** un
d2cd0 69 78 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69  ixFile, do nothi
d2ce0 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68  ng. Don't use th
d2cf0 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74  e end_lock: exit
d2d00 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 75   path, as.  ** u
d2d10 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 20  nixEnterMutex() 
d2d20 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c  hasn't been call
d2d30 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69  ed yet..  */.  i
d2d40 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
d2d50 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a  pe>=locktype ){.
d2d60 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 4c 4f      OSTRACE3("LO
d2d70 43 4b 20 20 20 20 25 64 20 25 73 20 6f 6b 20 28  CK    %d %s ok (
d2d80 61 6c 72 65 61 64 79 20 68 65 6c 64 29 5c 6e 22  already held)\n"
d2d90 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20  , pFile->h,.    
d2da0 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65          locktype
d2db0 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 29 3b  Name(locktype));
d2dc0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d2dd0 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  TE_OK;.  }..  /*
d2de0 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c   Make sure the l
d2df0 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20  ocking sequence 
d2e00 69 73 20 63 6f 72 72 65 63 74 2e 0a 20 20 2a 2a  is correct..  **
d2e10 20 20 28 31 29 20 57 65 20 6e 65 76 65 72 20 6d    (1) We never m
d2e20 6f 76 65 20 66 72 6f 6d 20 75 6e 6c 6f 63 6b 65  ove from unlocke
d2e30 64 20 74 6f 20 61 6e 79 74 68 69 6e 67 20 68 69  d to anything hi
d2e40 67 68 65 72 20 74 68 61 6e 20 73 68 61 72 65 64  gher than shared
d2e50 20 6c 6f 63 6b 2e 0a 20 20 2a 2a 20 20 28 32 29   lock..  **  (2)
d2e60 20 53 51 4c 69 74 65 20 6e 65 76 65 72 20 65 78   SQLite never ex
d2e70 70 6c 69 63 69 74 6c 79 20 72 65 71 75 65 73 74  plicitly request
d2e80 73 20 61 20 70 65 6e 64 69 67 20 6c 6f 63 6b 2e  s a pendig lock.
d2e90 0a 20 20 2a 2a 20 20 28 33 29 20 41 20 73 68 61  .  **  (3) A sha
d2ea0 72 65 64 20 6c 6f 63 6b 20 69 73 20 61 6c 77 61  red lock is alwa
d2eb0 79 73 20 68 65 6c 64 20 77 68 65 6e 20 61 20 72  ys held when a r
d2ec0 65 73 65 72 76 65 20 6c 6f 63 6b 20 69 73 20 72  eserve lock is r
d2ed0 65 71 75 65 73 74 65 64 2e 0a 20 20 2a 2f 0a 20  equested..  */. 
d2ee0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e   assert( pFile->
d2ef0 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43  locktype!=NO_LOC
d2f00 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53  K || locktype==S
d2f10 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
d2f20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
d2f30 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29  !=PENDING_LOCK )
d2f40 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b  ;.  assert( lock
d2f50 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c  type!=RESERVED_L
d2f60 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f  OCK || pFile->lo
d2f70 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
d2f80 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 69  OCK );..  /* Thi
d2f90 73 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64 65  s mutex is neede
d2fa0 64 20 62 65 63 61 75 73 65 20 70 46 69 6c 65 2d  d because pFile-
d2fb0 3e 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 64  >pLock is shared
d2fc0 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 0a   across threads.
d2fd0 20 20 2a 2f 0a 20 20 75 6e 69 78 45 6e 74 65 72    */.  unixEnter
d2fe0 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d  Mutex();..  /* M
d2ff0 61 6b 65 20 73 75 72 65 20 74 68 65 20 63 75 72  ake sure the cur
d3000 72 65 6e 74 20 74 68 72 65 61 64 20 6f 77 6e 73  rent thread owns
d3010 20 74 68 65 20 70 46 69 6c 65 2e 0a 20 20 2a 2f   the pFile..  */
d3020 0a 20 20 72 63 20 3d 20 74 72 61 6e 73 66 65 72  .  rc = transfer
d3030 4f 77 6e 65 72 73 68 69 70 28 70 46 69 6c 65 29  Ownership(pFile)
d3040 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
d3050 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 6e 69  TE_OK ){.    uni
d3060 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  xLeaveMutex();. 
d3070 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
d3080 7d 0a 20 20 70 4c 6f 63 6b 20 3d 20 70 46 69 6c  }.  pLock = pFil
d3090 65 2d 3e 70 4c 6f 63 6b 3b 0a 0a 20 20 2f 2a 20  e->pLock;..  /* 
d30a0 49 66 20 73 6f 6d 65 20 74 68 72 65 61 64 20 75  If some thread u
d30b0 73 69 6e 67 20 74 68 69 73 20 50 49 44 20 68 61  sing this PID ha
d30c0 73 20 61 20 6c 6f 63 6b 20 76 69 61 20 61 20 64  s a lock via a d
d30d0 69 66 66 65 72 65 6e 74 20 75 6e 69 78 46 69 6c  ifferent unixFil
d30e0 65 2a 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20 74  e*.  ** handle t
d30f0 68 61 74 20 70 72 65 63 6c 75 64 65 73 20 74 68  hat precludes th
d3100 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b  e requested lock
d3110 2c 20 72 65 74 75 72 6e 20 42 55 53 59 2e 0a 20  , return BUSY.. 
d3120 20 2a 2f 0a 20 20 69 66 28 20 28 70 46 69 6c 65   */.  if( (pFile
d3130 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 70 4c 6f 63  ->locktype!=pLoc
d3140 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 26 26 20 0a  k->locktype && .
d3150 20 20 20 20 20 20 20 20 20 20 28 70 4c 6f 63 6b            (pLock
d3160 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 50 45 4e 44  ->locktype>=PEND
d3170 49 4e 47 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b  ING_LOCK || lock
d3180 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b  type>SHARED_LOCK
d3190 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 63 20 3d  )).  ){.    rc =
d31a0 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
d31b0 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b    goto end_lock;
d31c0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 20  .  }..  /* If a 
d31d0 53 48 41 52 45 44 20 6c 6f 63 6b 20 69 73 20 72  SHARED lock is r
d31e0 65 71 75 65 73 74 65 64 2c 20 61 6e 64 20 73 6f  equested, and so
d31f0 6d 65 20 74 68 72 65 61 64 20 75 73 69 6e 67 20  me thread using 
d3200 74 68 69 73 20 50 49 44 20 61 6c 72 65 61 64 79  this PID already
d3210 0a 20 20 2a 2a 20 68 61 73 20 61 20 53 48 41 52  .  ** has a SHAR
d3220 45 44 20 6f 72 20 52 45 53 45 52 56 45 44 20 6c  ED or RESERVED l
d3230 6f 63 6b 2c 20 74 68 65 6e 20 69 6e 63 72 65 6d  ock, then increm
d3240 65 6e 74 20 72 65 66 65 72 65 6e 63 65 20 63 6f  ent reference co
d3250 75 6e 74 73 20 61 6e 64 0a 20 20 2a 2a 20 72 65  unts and.  ** re
d3260 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a  turn SQLITE_OK..
d3270 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74    */.  if( lockt
d3280 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
d3290 20 26 26 20 0a 20 20 20 20 20 20 28 70 4c 6f 63   && .      (pLoc
d32a0 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  k->locktype==SHA
d32b0 52 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 4c 6f 63  RED_LOCK || pLoc
d32c0 6b 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53  k->locktype==RES
d32d0 45 52 56 45 44 5f 4c 4f 43 4b 29 20 29 7b 0a 20  ERVED_LOCK) ){. 
d32e0 20 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74     assert( lockt
d32f0 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
d3300 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
d3310 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
d3320 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
d3330 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3e 30 20 29  ( pLock->cnt>0 )
d3340 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  ;.    pFile->loc
d3350 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c  ktype = SHARED_L
d3360 4f 43 4b 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e  OCK;.    pLock->
d3370 63 6e 74 2b 2b 3b 0a 20 20 20 20 70 46 69 6c 65  cnt++;.    pFile
d3380 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b  ->pOpen->nLock++
d3390 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c  ;.    goto end_l
d33a0 6f 63 6b 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20  ock;.  }...  /* 
d33b0 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 69  A PENDING lock i
d33c0 73 20 6e 65 65 64 65 64 20 62 65 66 6f 72 65 20  s needed before 
d33d0 61 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52  acquiring a SHAR
d33e0 45 44 20 6c 6f 63 6b 20 61 6e 64 20 62 65 66 6f  ED lock and befo
d33f0 72 65 0a 20 20 2a 2a 20 61 63 71 75 69 72 69 6e  re.  ** acquirin
d3400 67 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  g an EXCLUSIVE l
d3410 6f 63 6b 2e 20 20 46 6f 72 20 74 68 65 20 53 48  ock.  For the SH
d3420 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68 65 20 50  ARED lock, the P
d3430 45 4e 44 49 4e 47 20 77 69 6c 6c 0a 20 20 2a 2a  ENDING will.  **
d3440 20 62 65 20 72 65 6c 65 61 73 65 64 2e 0a 20 20   be released..  
d3450 2a 2f 0a 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20  */.  lock.l_len 
d3460 3d 20 31 4c 3b 0a 20 20 6c 6f 63 6b 2e 6c 5f 77  = 1L;.  lock.l_w
d3470 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54  hence = SEEK_SET
d3480 3b 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65  ;.  if( locktype
d3490 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 0a 20  ==SHARED_LOCK . 
d34a0 20 20 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70       || (locktyp
d34b0 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  e==EXCLUSIVE_LOC
d34c0 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  K && pFile->lock
d34d0 74 79 70 65 3c 50 45 4e 44 49 4e 47 5f 4c 4f 43  type<PENDING_LOC
d34e0 4b 29 0a 20 20 29 7b 0a 20 20 20 20 6c 6f 63 6b  K).  ){.    lock
d34f0 2e 6c 5f 74 79 70 65 20 3d 20 28 6c 6f 63 6b 74  .l_type = (lockt
d3500 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
d3510 3f 46 5f 52 44 4c 43 4b 3a 46 5f 57 52 4c 43 4b  ?F_RDLCK:F_WRLCK
d3520 29 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74  );.    lock.l_st
d3530 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59  art = PENDING_BY
d3540 54 45 3b 0a 20 20 20 20 73 20 3d 20 66 63 6e 74  TE;.    s = fcnt
d3550 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45  l(pFile->h, F_SE
d3560 54 4c 4b 2c 20 26 6c 6f 63 6b 29 3b 0a 20 20 20  TLK, &lock);.   
d3570 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 29 7b 0a   if( s==(-1) ){.
d3580 20 20 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65        tErrno = e
d3590 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d  rrno;.      rc =
d35a0 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d   sqliteErrorFrom
d35b0 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e  PosixError(tErrn
d35c0 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  o, SQLITE_IOERR_
d35d0 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28  LOCK);.      if(
d35e0 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72   IS_LOCK_ERROR(r
d35f0 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46  c) ){.        pF
d3600 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d  ile->lastErrno =
d3610 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d   tErrno;.      }
d3620 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  .      goto end_
d3630 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  lock;.    }.  }.
d3640 0a 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f  ..  /* If contro
d3650 6c 20 67 65 74 73 20 74 6f 20 74 68 69 73 20 70  l gets to this p
d3660 6f 69 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 61  oint, then actua
d3670 6c 6c 79 20 67 6f 20 61 68 65 61 64 20 61 6e 64  lly go ahead and
d3680 20 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61   make.  ** opera
d3690 74 69 6e 67 20 73 79 73 74 65 6d 20 63 61 6c 6c  ting system call
d36a0 73 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66  s for the specif
d36b0 69 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20  ied lock..  */. 
d36c0 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53   if( locktype==S
d36d0 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20  HARED_LOCK ){.  
d36e0 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d    assert( pLock-
d36f0 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61  >cnt==0 );.    a
d3700 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f  ssert( pLock->lo
d3710 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 0a 20 20  cktype==0 );..  
d3720 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74 68 65    /* Now get the
d3730 20 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f 0a 20 20   read-lock */.  
d3740 20 20 73 20 3d 20 72 61 6e 67 65 4c 6f 63 6b 28    s = rangeLock(
d3750 70 46 69 6c 65 2c 20 46 5f 52 44 4c 43 4b 2c 20  pFile, F_RDLCK, 
d3760 26 74 45 72 72 6e 6f 29 3b 0a 0a 20 20 20 20 2f  &tErrno);..    /
d3770 2a 20 44 72 6f 70 20 74 68 65 20 74 65 6d 70 6f  * Drop the tempo
d3780 72 61 72 79 20 50 45 4e 44 49 4e 47 20 6c 6f 63  rary PENDING loc
d3790 6b 20 2a 2f 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f  k */.    lock.l_
d37a0 73 74 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f  start = PENDING_
d37b0 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c  BYTE;.    lock.l
d37c0 5f 6c 65 6e 20 3d 20 31 4c 3b 0a 20 20 20 20 6c  _len = 1L;.    l
d37d0 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55  ock.l_type = F_U
d37e0 4e 4c 43 4b 3b 0a 20 20 20 20 69 66 28 20 66 63  NLCK;.    if( fc
d37f0 6e 74 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f  ntl(pFile->h, F_
d3800 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 30  SETLK, &lock)!=0
d3810 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 20   ){.      if( s 
d3820 21 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20 20 20  != -1 ){.       
d3830 20 2f 2a 20 54 68 69 73 20 63 6f 75 6c 64 20 68   /* This could h
d3840 61 70 70 65 6e 20 77 69 74 68 20 61 20 6e 65 74  appen with a net
d3850 77 6f 72 6b 20 6d 6f 75 6e 74 20 2a 2f 0a 20 20  work mount */.  
d3860 20 20 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65        tErrno = e
d3870 72 72 6e 6f 3b 20 0a 20 20 20 20 20 20 20 20 72  rrno; .        r
d3880 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46  c = sqliteErrorF
d3890 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45  romPosixError(tE
d38a0 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45  rrno, SQLITE_IOE
d38b0 52 52 5f 55 4e 4c 4f 43 4b 29 3b 20 0a 20 20 20  RR_UNLOCK); .   
d38c0 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b       if( IS_LOCK
d38d0 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20  _ERROR(rc) ){.  
d38e0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c          pFile->l
d38f0 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e  astErrno = tErrn
d3900 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  o;.        }.   
d3910 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f       goto end_lo
d3920 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ck;.      }.    
d3930 7d 0a 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31  }.    if( s==(-1
d3940 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  ) ){.      rc = 
d3950 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50  sqliteErrorFromP
d3960 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f  osixError(tErrno
d3970 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c  , SQLITE_IOERR_L
d3980 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20  OCK);.      if( 
d3990 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63  IS_LOCK_ERROR(rc
d39a0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69  ) ){.        pFi
d39b0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
d39c0 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a  tErrno;.      }.
d39d0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
d39e0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d39f0 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a   = SHARED_LOCK;.
d3a00 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70        pFile->pOp
d3a10 65 6e 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20  en->nLock++;.   
d3a20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 20 3d 20     pLock->cnt = 
d3a30 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  1;.    }.  }else
d3a40 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45   if( locktype==E
d3a50 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26  XCLUSIVE_LOCK &&
d3a60 20 70 4c 6f 63 6b 2d 3e 63 6e 74 3e 31 20 29 7b   pLock->cnt>1 ){
d3a70 0a 20 20 20 20 2f 2a 20 57 65 20 61 72 65 20 74  .    /* We are t
d3a80 72 79 69 6e 67 20 66 6f 72 20 61 6e 20 65 78 63  rying for an exc
d3a90 6c 75 73 69 76 65 20 6c 6f 63 6b 20 62 75 74 20  lusive lock but 
d3aa0 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69  another thread i
d3ab0 6e 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 73 61  n this.    ** sa
d3ac0 6d 65 20 70 72 6f 63 65 73 73 20 69 73 20 73 74  me process is st
d3ad0 69 6c 6c 20 68 6f 6c 64 69 6e 67 20 61 20 73 68  ill holding a sh
d3ae0 61 72 65 64 20 6c 6f 63 6b 2e 20 2a 2f 0a 20 20  ared lock. */.  
d3af0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55    rc = SQLITE_BU
d3b00 53 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  SY;.  }else{.   
d3b10 20 2f 2a 20 54 68 65 20 72 65 71 75 65 73 74 20   /* The request 
d3b20 77 61 73 20 66 6f 72 20 61 20 52 45 53 45 52 56  was for a RESERV
d3b30 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20  ED or EXCLUSIVE 
d3b40 6c 6f 63 6b 2e 20 20 49 74 20 69 73 0a 20 20 20  lock.  It is.   
d3b50 20 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 61 74   ** assumed that
d3b60 20 74 68 65 72 65 20 69 73 20 61 20 53 48 41 52   there is a SHAR
d3b70 45 44 20 6f 72 20 67 72 65 61 74 65 72 20 6c 6f  ED or greater lo
d3b80 63 6b 20 6f 6e 20 74 68 65 20 66 69 6c 65 0a 20  ck on the file. 
d3b90 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 2e 0a 20     ** already.. 
d3ba0 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
d3bb0 28 20 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ( 0!=pFile->lock
d3bc0 74 79 70 65 20 29 3b 0a 20 20 20 20 6c 6f 63 6b  type );.    lock
d3bd0 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 57 52 4c 43  .l_type = F_WRLC
d3be0 4b 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 6c  K;.    switch( l
d3bf0 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 20  ocktype ){.     
d3c00 20 63 61 73 65 20 52 45 53 45 52 56 45 44 5f 4c   case RESERVED_L
d3c10 4f 43 4b 3a 0a 20 20 20 20 20 20 20 20 6c 6f 63  OCK:.        loc
d3c20 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 52 45 53 45  k.l_start = RESE
d3c30 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20 20  RVED_BYTE;.     
d3c40 20 20 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69     s = fcntl(pFi
d3c50 6c 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20  le->h, F_SETLK, 
d3c60 26 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 20 20  &lock);.        
d3c70 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
d3c80 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
d3c90 20 20 20 20 20 63 61 73 65 20 45 58 43 4c 55 53       case EXCLUS
d3ca0 49 56 45 5f 4c 4f 43 4b 3a 0a 20 20 20 20 20 20  IVE_LOCK:.      
d3cb0 20 20 73 20 3d 20 72 61 6e 67 65 4c 6f 63 6b 28    s = rangeLock(
d3cc0 70 46 69 6c 65 2c 20 46 5f 57 52 4c 43 4b 2c 20  pFile, F_WRLCK, 
d3cd0 26 74 45 72 72 6e 6f 29 3b 0a 20 20 20 20 20 20  &tErrno);.      
d3ce0 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 64    break;.      d
d3cf0 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20  efault:.        
d3d00 61 73 73 65 72 74 28 30 29 3b 0a 20 20 20 20 7d  assert(0);.    }
d3d10 0a 20 20 20 20 69 66 28 20 73 3d 3d 28 2d 31 29  .    if( s==(-1)
d3d20 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
d3d30 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
d3d40 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
d3d50 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f   SQLITE_IOERR_LO
d3d60 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49  CK);.      if( I
d3d70 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29  S_LOCK_ERROR(rc)
d3d80 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c   ){.        pFil
d3d90 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74  e->lastErrno = t
d3da0 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20  Errno;.      }. 
d3db0 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 0a 23 69 66     }.  }.  ..#if
d3dc0 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a  ndef NDEBUG.  /*
d3dd0 20 53 65 74 20 75 70 20 74 68 65 20 74 72 61 6e   Set up the tran
d3de0 73 61 63 74 69 6f 6e 2d 63 6f 75 6e 74 65 72 20  saction-counter 
d3df0 63 68 61 6e 67 65 20 63 68 65 63 6b 69 6e 67 20  change checking 
d3e00 66 6c 61 67 73 20 77 68 65 6e 0a 20 20 2a 2a 20  flags when.  ** 
d3e10 74 72 61 6e 73 69 74 69 6f 6e 69 6e 67 20 66 72  transitioning fr
d3e20 6f 6d 20 61 20 53 48 41 52 45 44 20 74 6f 20 61  om a SHARED to a
d3e30 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 2e 20   RESERVED lock. 
d3e40 20 54 68 65 20 63 68 61 6e 67 65 0a 20 20 2a 2a   The change.  **
d3e50 20 66 72 6f 6d 20 53 48 41 52 45 44 20 74 6f 20   from SHARED to 
d3e60 52 45 53 45 52 56 45 44 20 6d 61 72 6b 73 20 74  RESERVED marks t
d3e70 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  he beginning of 
d3e80 61 20 6e 6f 72 6d 61 6c 0a 20 20 2a 2a 20 77 72  a normal.  ** wr
d3e90 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 20 28 6e  ite operation (n
d3ea0 6f 74 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61 6c  ot a hot journal
d3eb0 20 72 6f 6c 6c 62 61 63 6b 29 2e 0a 20 20 2a 2f   rollback)..  */
d3ec0 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
d3ed0 45 5f 4f 4b 0a 20 20 20 26 26 20 70 46 69 6c 65  E_OK.   && pFile
d3ee0 2d 3e 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52  ->locktype<=SHAR
d3ef0 45 44 5f 4c 4f 43 4b 0a 20 20 20 26 26 20 6c 6f  ED_LOCK.   && lo
d3f00 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44  cktype==RESERVED
d3f10 5f 4c 4f 43 4b 0a 20 20 29 7b 0a 20 20 20 20 70  _LOCK.  ){.    p
d3f20 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72 43  File->transCntrC
d3f30 68 6e 67 20 3d 20 30 3b 0a 20 20 20 20 70 46 69  hng = 0;.    pFi
d3f40 6c 65 2d 3e 64 62 55 70 64 61 74 65 20 3d 20 30  le->dbUpdate = 0
d3f50 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e  ;.    pFile->inN
d3f60 6f 72 6d 61 6c 57 72 69 74 65 20 3d 20 31 3b 0a  ormalWrite = 1;.
d3f70 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 0a 20 20 69    }.#endif...  i
d3f80 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
d3f90 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c   ){.    pFile->l
d3fa0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79  ocktype = lockty
d3fb0 70 65 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c  pe;.    pLock->l
d3fc0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79  ocktype = lockty
d3fd0 70 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  pe;.  }else if( 
d3fe0 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53  locktype==EXCLUS
d3ff0 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  IVE_LOCK ){.    
d4000 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
d4010 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a  = PENDING_LOCK;.
d4020 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74      pLock->lockt
d4030 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f  ype = PENDING_LO
d4040 43 4b 3b 0a 20 20 7d 0a 0a 65 6e 64 5f 6c 6f 63  CK;.  }..end_loc
d4050 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75  k:.  unixLeaveMu
d4060 74 65 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45  tex();.  OSTRACE
d4070 34 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73  4("LOCK    %d %s
d4080 20 25 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68   %s\n", pFile->h
d4090 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c  , locktypeName(l
d40a0 6f 63 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20  ocktype), .     
d40b0 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f   rc==SQLITE_OK ?
d40c0 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22   "ok" : "failed"
d40d0 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
d40e0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  }../*.** Close a
d40f0 6c 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  ll file descript
d4100 6f 72 73 20 61 63 63 75 6d 75 61 74 65 64 20 69  ors accumuated i
d4110 6e 20 74 68 65 20 75 6e 69 78 4f 70 65 6e 43 6e  n the unixOpenCn
d4120 74 2d 3e 70 55 6e 75 73 65 64 20 6c 69 73 74 2e  t->pUnused list.
d4130 0a 2a 2a 20 49 66 20 61 6c 6c 20 73 75 63 68 20  .** If all such 
d4140 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73  file descriptors
d4150 20 61 72 65 20 63 6c 6f 73 65 64 20 77 69 74 68   are closed with
d4160 6f 75 74 20 65 72 72 6f 72 2c 20 74 68 65 20 6c  out error, the l
d4170 69 73 74 20 69 73 0a 2a 2a 20 63 6c 65 61 72 65  ist is.** cleare
d4180 64 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20  d and SQLITE_OK 
d4190 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
d41a0 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 61 6e  Otherwise, if an
d41b0 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74   error occurs, t
d41c0 68 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79  hen successfully
d41d0 20 63 6c 6f 73 65 64 20 66 69 6c 65 20 64 65 73   closed file des
d41e0 63 72 69 70 74 6f 72 0a 2a 2a 20 65 6e 74 72 69  criptor.** entri
d41f0 65 73 20 61 72 65 20 72 65 6d 6f 76 65 64 20 66  es are removed f
d4200 72 6f 6d 20 74 68 65 20 6c 69 73 74 2c 20 61 6e  rom the list, an
d4210 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43  d SQLITE_IOERR_C
d4220 4c 4f 53 45 20 72 65 74 75 72 6e 65 64 2e 20 0a  LOSE returned. .
d4230 2a 2a 20 6e 6f 74 20 64 65 6c 65 74 65 64 20 61  ** not deleted a
d4240 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  nd SQLITE_IOERR_
d4250 43 4c 4f 53 45 20 72 65 74 75 72 6e 65 64 2e 0a  CLOSE returned..
d4260 2a 2f 20 0a 73 74 61 74 69 63 20 69 6e 74 20 63  */ .static int c
d4270 6c 6f 73 65 50 65 6e 64 69 6e 67 46 64 73 28 75  losePendingFds(u
d4280 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b  nixFile *pFile){
d4290 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
d42a0 54 45 5f 4f 4b 3b 0a 20 20 73 74 72 75 63 74 20  TE_OK;.  struct 
d42b0 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70 4f 70  unixOpenCnt *pOp
d42c0 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65  en = pFile->pOpe
d42d0 6e 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46  n;.  UnixUnusedF
d42e0 64 20 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a 20  d *pError = 0;. 
d42f0 20 55 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70   UnixUnusedFd *p
d4300 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64  ;.  UnixUnusedFd
d4310 20 2a 70 4e 65 78 74 3b 0a 20 20 66 6f 72 28 70   *pNext;.  for(p
d4320 3d 70 4f 70 65 6e 2d 3e 70 55 6e 75 73 65 64 3b  =pOpen->pUnused;
d4330 20 70 3b 20 70 3d 70 4e 65 78 74 29 7b 0a 20 20   p; p=pNext){.  
d4340 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70 4e 65    pNext = p->pNe
d4350 78 74 3b 0a 20 20 20 20 69 66 28 20 63 6c 6f 73  xt;.    if( clos
d4360 65 28 70 2d 3e 66 64 29 20 29 7b 0a 20 20 20 20  e(p->fd) ){.    
d4370 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d4380 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
d4390 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f    rc = SQLITE_IO
d43a0 45 52 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20  ERR_CLOSE;.     
d43b0 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 45 72 72   p->pNext = pErr
d43c0 6f 72 3b 0a 20 20 20 20 20 20 70 45 72 72 6f 72  or;.      pError
d43d0 20 3d 20 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = p;.    }else{
d43e0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
d43f0 72 65 65 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20  ree(p);.    }.  
d4400 7d 0a 20 20 70 4f 70 65 6e 2d 3e 70 55 6e 75 73  }.  pOpen->pUnus
d4410 65 64 20 3d 20 70 45 72 72 6f 72 3b 0a 20 20 72  ed = pError;.  r
d4420 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
d4430 2a 2a 20 41 64 64 20 74 68 65 20 66 69 6c 65 20  ** Add the file 
d4440 64 65 73 63 72 69 70 74 6f 72 20 75 73 65 64 20  descriptor used 
d4450 62 79 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 70  by file handle p
d4460 46 69 6c 65 20 74 6f 20 74 68 65 20 63 6f 72 72  File to the corr
d4470 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 70 55 6e  esponding.** pUn
d4480 75 73 65 64 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74  used list..*/.st
d4490 61 74 69 63 20 76 6f 69 64 20 73 65 74 50 65 6e  atic void setPen
d44a0 64 69 6e 67 46 64 28 75 6e 69 78 46 69 6c 65 20  dingFd(unixFile 
d44b0 2a 70 46 69 6c 65 29 7b 0a 20 20 73 74 72 75 63  *pFile){.  struc
d44c0 74 20 75 6e 69 78 4f 70 65 6e 43 6e 74 20 2a 70  t unixOpenCnt *p
d44d0 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d 3e 70 4f  Open = pFile->pO
d44e0 70 65 6e 3b 0a 20 20 55 6e 69 78 55 6e 75 73 65  pen;.  UnixUnuse
d44f0 64 46 64 20 2a 70 20 3d 20 70 46 69 6c 65 2d 3e  dFd *p = pFile->
d4500 70 55 6e 75 73 65 64 3b 0a 20 20 70 2d 3e 70 4e  pUnused;.  p->pN
d4510 65 78 74 20 3d 20 70 4f 70 65 6e 2d 3e 70 55 6e  ext = pOpen->pUn
d4520 75 73 65 64 3b 0a 20 20 70 4f 70 65 6e 2d 3e 70  used;.  pOpen->p
d4530 55 6e 75 73 65 64 20 3d 20 70 3b 0a 20 20 70 46  Unused = p;.  pF
d4540 69 6c 65 2d 3e 68 20 3d 20 2d 31 3b 0a 20 20 70  ile->h = -1;.  p
d4550 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64 20 3d 20  File->pUnused = 
d4560 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65  0;.}../*.** Lowe
d4570 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65  r the locking le
d4580 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63  vel on file desc
d4590 72 69 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20  riptor pFile to 
d45a0 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74  locktype.  lockt
d45b0 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65  ype.** must be e
d45c0 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72  ither NO_LOCK or
d45d0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a   SHARED_LOCK..**
d45e0 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69  .** If the locki
d45f0 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20  ng level of the 
d4600 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
d4610 69 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72  is already at or
d4620 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65   below.** the re
d4630 71 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20  quested locking 
d4640 6c 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74  level, this rout
d4650 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ine is a no-op..
d4660 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
d4670 69 78 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33  ixUnlock(sqlite3
d4680 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c  _file *id, int l
d4690 6f 63 6b 74 79 70 65 29 7b 0a 20 20 75 6e 69 78  ocktype){.  unix
d46a0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
d46b0 6e 69 78 46 69 6c 65 2a 29 69 64 3b 20 2f 2a 20  nixFile*)id; /* 
d46c0 54 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 2a 2f  The open file */
d46d0 0a 20 20 73 74 72 75 63 74 20 75 6e 69 78 4c 6f  .  struct unixLo
d46e0 63 6b 49 6e 66 6f 20 2a 70 4c 6f 63 6b 3b 20 20  ckInfo *pLock;  
d46f0 20 20 20 20 2f 2a 20 53 74 72 75 63 74 75 72 65      /* Structure
d4700 20 64 65 73 63 72 69 62 69 6e 67 20 63 75 72 72   describing curr
d4710 65 6e 74 20 6c 6f 63 6b 20 73 74 61 74 65 20 2a  ent lock state *
d4720 2f 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b  /.  struct flock
d4730 20 6c 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20   lock;          
d4740 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74       /* Informat
d4750 69 6f 6e 20 70 61 73 73 65 64 20 69 6e 74 6f 20  ion passed into 
d4760 66 63 6e 74 6c 28 29 20 2a 2f 0a 20 20 69 6e 74  fcntl() */.  int
d4770 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
d4780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
d4790 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f   Return code fro
d47a0 6d 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65  m this interface
d47b0 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20 20   */.  int h;    
d47c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d47d0 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e         /* The un
d47e0 64 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 64 65  derlying file de
d47f0 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e  scriptor */.  in
d4800 74 20 74 45 72 72 6e 6f 3b 20 20 20 20 20 20 20  t tErrno;       
d4810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
d4820 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 66 72 6f  * Error code fro
d4830 6d 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 65 72  m system call er
d4840 72 6f 72 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72  rors */..  asser
d4850 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53  t( pFile );.  OS
d4860 54 52 41 43 45 37 28 22 55 4e 4c 4f 43 4b 20 20  TRACE7("UNLOCK  
d4870 25 64 20 25 64 20 77 61 73 20 25 64 28 25 64 2c  %d %d was %d(%d,
d4880 25 64 29 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70  %d) pid=%d\n", p
d4890 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70  File->h, locktyp
d48a0 65 2c 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  e,.      pFile->
d48b0 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d  locktype, pFile-
d48c0 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65  >pLock->locktype
d48d0 2c 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e  , pFile->pLock->
d48e0 63 6e 74 2c 20 67 65 74 70 69 64 28 29 29 3b 0a  cnt, getpid());.
d48f0 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74  .  assert( lockt
d4900 79 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype<=SHARED_LOCK
d4910 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   );.  if( pFile-
d4920 3e 6c 6f 63 6b 74 79 70 65 3c 3d 6c 6f 63 6b 74  >locktype<=lockt
d4930 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ype ){.    retur
d4940 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
d4950 0a 20 20 69 66 28 20 43 48 45 43 4b 5f 54 48 52  .  if( CHECK_THR
d4960 45 41 44 49 44 28 70 46 69 6c 65 29 20 29 7b 0a  EADID(pFile) ){.
d4970 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
d4980 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
d4990 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
d49a0 3b 0a 20 20 68 20 3d 20 70 46 69 6c 65 2d 3e 68  ;.  h = pFile->h
d49b0 3b 0a 20 20 70 4c 6f 63 6b 20 3d 20 70 46 69 6c  ;.  pLock = pFil
d49c0 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 61 73 73 65  e->pLock;.  asse
d49d0 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74 21 3d  rt( pLock->cnt!=
d49e0 30 20 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65  0 );.  if( pFile
d49f0 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45  ->locktype>SHARE
d4a00 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 61 73  D_LOCK ){.    as
d4a10 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 6c 6f 63  sert( pLock->loc
d4a20 6b 74 79 70 65 3d 3d 70 46 69 6c 65 2d 3e 6c 6f  ktype==pFile->lo
d4a30 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 53 69  cktype );.    Si
d4a40 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e  mulateIOErrorBen
d4a50 69 67 6e 28 31 29 3b 0a 20 20 20 20 53 69 6d 75  ign(1);.    Simu
d4a60 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 68 3d 28  lateIOError( h=(
d4a70 2d 31 29 20 29 0a 20 20 20 20 53 69 6d 75 6c 61  -1) ).    Simula
d4a80 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28  teIOErrorBenign(
d4a90 30 29 3b 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  0);..#ifndef NDE
d4aa0 42 55 47 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20  BUG.    /* When 
d4ab0 72 65 64 75 63 69 6e 67 20 61 20 6c 6f 63 6b 20  reducing a lock 
d4ac0 73 75 63 68 20 74 68 61 74 20 6f 74 68 65 72 20  such that other 
d4ad0 70 72 6f 63 65 73 73 65 73 20 63 61 6e 20 73 74  processes can st
d4ae0 61 72 74 0a 20 20 20 20 2a 2a 20 72 65 61 64 69  art.    ** readi
d4af0 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ng the database 
d4b00 66 69 6c 65 20 61 67 61 69 6e 2c 20 6d 61 6b 65  file again, make
d4b10 20 73 75 72 65 20 74 68 61 74 20 74 68 65 0a 20   sure that the. 
d4b20 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f     ** transactio
d4b30 6e 20 63 6f 75 6e 74 65 72 20 77 61 73 20 75 70  n counter was up
d4b40 64 61 74 65 64 20 69 66 20 61 6e 79 20 70 61 72  dated if any par
d4b50 74 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  t of the databas
d4b60 65 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 68  e.    ** file ch
d4b70 61 6e 67 65 64 2e 20 20 49 66 20 74 68 65 20 74  anged.  If the t
d4b80 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74  ransaction count
d4b90 65 72 20 69 73 20 6e 6f 74 20 75 70 64 61 74 65  er is not update
d4ba0 64 2c 0a 20 20 20 20 2a 2a 20 6f 74 68 65 72 20  d,.    ** other 
d4bb0 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74  connections to t
d4bc0 68 65 20 73 61 6d 65 20 66 69 6c 65 20 6d 69 67  he same file mig
d4bd0 68 74 20 6e 6f 74 20 72 65 61 6c 69 7a 65 20 74  ht not realize t
d4be0 68 61 74 0a 20 20 20 20 2a 2a 20 74 68 65 20 66  hat.    ** the f
d4bf0 69 6c 65 20 68 61 73 20 63 68 61 6e 67 65 64 20  ile has changed 
d4c00 61 6e 64 20 68 65 6e 63 65 20 6d 69 67 68 74 20  and hence might 
d4c10 6e 6f 74 20 6b 6e 6f 77 20 74 6f 20 66 6c 75 73  not know to flus
d4c20 68 20 74 68 65 69 72 0a 20 20 20 20 2a 2a 20 63  h their.    ** c
d4c30 61 63 68 65 2e 20 20 54 68 65 20 75 73 65 20 6f  ache.  The use o
d4c40 66 20 61 20 73 74 61 6c 65 20 63 61 63 68 65 20  f a stale cache 
d4c50 63 61 6e 20 6c 65 61 64 20 74 6f 20 64 61 74 61  can lead to data
d4c60 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e  base corruption.
d4c70 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
d4c80 72 74 28 20 70 46 69 6c 65 2d 3e 69 6e 4e 6f 72  rt( pFile->inNor
d4c90 6d 61 6c 57 72 69 74 65 3d 3d 30 0a 20 20 20 20  malWrite==0.    
d4ca0 20 20 20 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 64       || pFile->d
d4cb0 62 55 70 64 61 74 65 3d 3d 30 0a 20 20 20 20 20  bUpdate==0.     
d4cc0 20 20 20 20 7c 7c 20 70 46 69 6c 65 2d 3e 74 72      || pFile->tr
d4cd0 61 6e 73 43 6e 74 72 43 68 6e 67 3d 3d 31 20 29  ansCntrChng==1 )
d4ce0 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 69 6e 4e  ;.    pFile->inN
d4cf0 6f 72 6d 61 6c 57 72 69 74 65 20 3d 20 30 3b 0a  ormalWrite = 0;.
d4d00 23 65 6e 64 69 66 0a 0a 0a 20 20 20 20 69 66 28  #endif...    if(
d4d10 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45   locktype==SHARE
d4d20 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20  D_LOCK ){.      
d4d30 69 66 28 20 72 61 6e 67 65 4c 6f 63 6b 28 70 46  if( rangeLock(pF
d4d40 69 6c 65 2c 20 46 5f 52 44 4c 43 4b 2c 20 26 74  ile, F_RDLCK, &t
d4d50 45 72 72 6e 6f 29 3d 3d 28 2d 31 29 20 29 7b 0a  Errno)==(-1) ){.
d4d60 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
d4d70 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
d4d80 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53  xError(tErrno, S
d4d90 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f  QLITE_IOERR_RDLO
d4da0 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  CK);.        if(
d4db0 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72   IS_LOCK_ERROR(r
d4dc0 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  c) ){.          
d4dd0 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
d4de0 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20   = tErrno;.     
d4df0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 67 6f 74     }.        got
d4e00 6f 20 65 6e 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20  o end_unlock;.  
d4e10 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
d4e20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f  lock.l_type = F_
d4e30 55 4e 4c 43 4b 3b 0a 20 20 20 20 6c 6f 63 6b 2e  UNLCK;.    lock.
d4e40 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f  l_whence = SEEK_
d4e50 53 45 54 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f  SET;.    lock.l_
d4e60 73 74 61 72 74 20 3d 20 50 45 4e 44 49 4e 47 5f  start = PENDING_
d4e70 42 59 54 45 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c  BYTE;.    lock.l
d4e80 5f 6c 65 6e 20 3d 20 32 4c 3b 20 20 61 73 73 65  _len = 2L;  asse
d4e90 72 74 28 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  rt( PENDING_BYTE
d4ea0 2b 31 3d 3d 52 45 53 45 52 56 45 44 5f 42 59 54  +1==RESERVED_BYT
d4eb0 45 20 29 3b 0a 20 20 20 20 69 66 28 20 66 63 6e  E );.    if( fcn
d4ec0 74 6c 28 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26  tl(h, F_SETLK, &
d4ed0 6c 6f 63 6b 29 21 3d 28 2d 31 29 20 29 7b 0a 20  lock)!=(-1) ){. 
d4ee0 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b       pLock->lock
d4ef0 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f  type = SHARED_LO
d4f00 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  CK;.    }else{. 
d4f10 20 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72       tErrno = er
d4f20 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  rno;.      rc = 
d4f30 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50  sqliteErrorFromP
d4f40 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f  osixError(tErrno
d4f50 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55  , SQLITE_IOERR_U
d4f60 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66  NLOCK);.      if
d4f70 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  ( IS_LOCK_ERROR(
d4f80 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70  rc) ){.        p
d4f90 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
d4fa0 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20  = tErrno;.      
d4fb0 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64  }.      goto end
d4fc0 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20  _unlock;.    }. 
d4fd0 20 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70   }.  if( locktyp
d4fe0 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20  e==NO_LOCK ){.  
d4ff0 20 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65    struct unixOpe
d5000 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 0a 0a 20 20  nCnt *pOpen;..  
d5010 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20 74    /* Decrement t
d5020 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 63  he shared lock c
d5030 6f 75 6e 74 65 72 2e 20 20 52 65 6c 65 61 73 65  ounter.  Release
d5040 20 74 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20   the lock using 
d5050 61 6e 0a 20 20 20 20 2a 2a 20 4f 53 20 63 61 6c  an.    ** OS cal
d5060 6c 20 6f 6e 6c 79 20 77 68 65 6e 20 61 6c 6c 20  l only when all 
d5070 74 68 72 65 61 64 73 20 69 6e 20 74 68 69 73 20  threads in this 
d5080 73 61 6d 65 20 70 72 6f 63 65 73 73 20 68 61 76  same process hav
d5090 65 20 72 65 6c 65 61 73 65 64 0a 20 20 20 20 2a  e released.    *
d50a0 2a 20 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 20 20  * the lock..    
d50b0 2a 2f 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 63 6e  */.    pLock->cn
d50c0 74 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f  t--;.    if( pLo
d50d0 63 6b 2d 3e 63 6e 74 3d 3d 30 20 29 7b 0a 20 20  ck->cnt==0 ){.  
d50e0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20      lock.l_type 
d50f0 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20 20 20  = F_UNLCK;.     
d5100 20 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d   lock.l_whence =
d5110 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 20   SEEK_SET;.     
d5120 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20   lock.l_start = 
d5130 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 30 4c 3b  lock.l_len = 0L;
d5140 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49  .      SimulateI
d5150 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29 3b  OErrorBenign(1);
d5160 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49  .      SimulateI
d5170 4f 45 72 72 6f 72 28 20 68 3d 28 2d 31 29 20 29  OError( h=(-1) )
d5180 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49  .      SimulateI
d5190 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b  OErrorBenign(0);
d51a0 0a 20 20 20 20 20 20 69 66 28 20 66 63 6e 74 6c  .      if( fcntl
d51b0 28 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f  (h, F_SETLK, &lo
d51c0 63 6b 29 21 3d 28 2d 31 29 20 29 7b 0a 20 20 20  ck)!=(-1) ){.   
d51d0 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b       pLock->lock
d51e0 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a  type = NO_LOCK;.
d51f0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
d5200 20 20 20 20 20 74 45 72 72 6e 6f 20 3d 20 65 72       tErrno = er
d5210 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 63 20  rno;.        rc 
d5220 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f  = sqliteErrorFro
d5230 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72  mPosixError(tErr
d5240 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  no, SQLITE_IOERR
d5250 5f 55 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20  _UNLOCK);.      
d5260 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
d5270 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20  ROR(rc) ){.     
d5280 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74       pFile->last
d5290 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a  Errno = tErrno;.
d52a0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
d52b0 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70    pLock->locktyp
d52c0 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20  e = NO_LOCK;.   
d52d0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b       pFile->lock
d52e0 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a  type = NO_LOCK;.
d52f0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
d5300 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20     /* Decrement 
d5310 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 6c 6f 63  the count of loc
d5320 6b 73 20 61 67 61 69 6e 73 74 20 74 68 69 73 20  ks against this 
d5330 73 61 6d 65 20 66 69 6c 65 2e 20 20 57 68 65 6e  same file.  When
d5340 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f 75 6e   the.    ** coun
d5350 74 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 20  t reaches zero, 
d5360 63 6c 6f 73 65 20 61 6e 79 20 6f 74 68 65 72 20  close any other 
d5370 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73  file descriptors
d5380 20 77 68 6f 73 65 20 63 6c 6f 73 65 0a 20 20 20   whose close.   
d5390 20 2a 2a 20 77 61 73 20 64 65 66 65 72 72 65 64   ** was deferred
d53a0 20 62 65 63 61 75 73 65 20 6f 66 20 6f 75 74 73   because of outs
d53b0 74 61 6e 64 69 6e 67 20 6c 6f 63 6b 73 2e 0a 20  tanding locks.. 
d53c0 20 20 20 2a 2f 0a 20 20 20 20 70 4f 70 65 6e 20     */.    pOpen 
d53d0 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a  = pFile->pOpen;.
d53e0 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b      pOpen->nLock
d53f0 2d 2d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  --;.    assert( 
d5400 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e 3d 30 20  pOpen->nLock>=0 
d5410 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 65 6e  );.    if( pOpen
d5420 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20  ->nLock==0 ){.  
d5430 20 20 20 20 69 6e 74 20 72 63 32 20 3d 20 63 6c      int rc2 = cl
d5440 6f 73 65 50 65 6e 64 69 6e 67 46 64 73 28 70 46  osePendingFds(pF
d5450 69 6c 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ile);.      if( 
d5460 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
d5470 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 63  .        rc = rc
d5480 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  2;.      }.    }
d5490 0a 20 20 7d 0a 09 0a 65 6e 64 5f 75 6e 6c 6f 63  .  }...end_unloc
d54a0 6b 3a 0a 20 20 75 6e 69 78 4c 65 61 76 65 4d 75  k:.  unixLeaveMu
d54b0 74 65 78 28 29 3b 0a 20 20 69 66 28 20 72 63 3d  tex();.  if( rc=
d54c0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 70 46 69  =SQLITE_OK ) pFi
d54d0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c  le->locktype = l
d54e0 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 75 72  ocktype;.  retur
d54f0 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
d5500 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 70 65 72  his function per
d5510 66 6f 72 6d 73 20 74 68 65 20 70 61 72 74 73 20  forms the parts 
d5520 6f 66 20 74 68 65 20 22 63 6c 6f 73 65 20 66 69  of the "close fi
d5530 6c 65 22 20 6f 70 65 72 61 74 69 6f 6e 20 0a 2a  le" operation .*
d5540 2a 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20  * common to all 
d5550 6c 6f 63 6b 69 6e 67 20 73 63 68 65 6d 65 73 2e  locking schemes.
d5560 20 49 74 20 63 6c 6f 73 65 73 20 74 68 65 20 64   It closes the d
d5570 69 72 65 63 74 6f 72 79 20 61 6e 64 20 66 69 6c  irectory and fil
d5580 65 0a 2a 2a 20 68 61 6e 64 6c 65 73 2c 20 69 66  e.** handles, if
d5590 20 74 68 65 79 20 61 72 65 20 76 61 6c 69 64 2c   they are valid,
d55a0 20 61 6e 64 20 73 65 74 73 20 61 6c 6c 20 66 69   and sets all fi
d55b0 65 6c 64 73 20 6f 66 20 74 68 65 20 75 6e 69 78  elds of the unix
d55c0 46 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 75 72  File.** structur
d55d0 65 20 74 6f 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 74  e to 0..**.** It
d55e0 20 69 73 20 2a 6e 6f 74 2a 20 6e 65 63 65 73 73   is *not* necess
d55f0 61 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ary to hold the 
d5600 6d 75 74 65 78 20 77 68 65 6e 20 74 68 69 73 20  mutex when this 
d5610 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
d5620 64 2c 0a 2a 2a 20 65 76 65 6e 20 6f 6e 20 56 78  d,.** even on Vx
d5630 57 6f 72 6b 73 2e 20 20 41 20 6d 75 74 65 78 20  Works.  A mutex 
d5640 77 69 6c 6c 20 62 65 20 61 63 71 75 69 72 65 64  will be acquired
d5650 20 6f 6e 20 56 78 57 6f 72 6b 73 20 62 79 20 74   on VxWorks by t
d5660 68 65 0a 2a 2a 20 76 78 77 6f 72 6b 73 52 65 6c  he.** vxworksRel
d5670 65 61 73 65 46 69 6c 65 49 64 28 29 20 72 6f 75  easeFileId() rou
d5680 74 69 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tine..*/.static 
d5690 69 6e 74 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c  int closeUnixFil
d56a0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
d56b0 69 64 29 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20  id){.  unixFile 
d56c0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
d56d0 6c 65 2a 29 69 64 3b 0a 20 20 69 66 28 20 70 46  le*)id;.  if( pF
d56e0 69 6c 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ile ){.    if( p
d56f0 46 69 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 20 29  File->dirfd>=0 )
d5700 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 72 72 20  {.      int err 
d5710 3d 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 64  = close(pFile->d
d5720 69 72 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28  irfd);.      if(
d5730 20 65 72 72 20 29 7b 0a 20 20 20 20 20 20 20 20   err ){.        
d5740 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
d5750 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
d5760 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d5770 49 4f 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b  IOERR_DIR_CLOSE;
d5780 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
d5790 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 64 69 72        pFile->dir
d57a0 66 64 3d 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20  fd=-1;.      }. 
d57b0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 46 69     }.    if( pFi
d57c0 6c 65 2d 3e 68 3e 3d 30 20 29 7b 0a 20 20 20 20  le->h>=0 ){.    
d57d0 20 20 69 6e 74 20 65 72 72 20 3d 20 63 6c 6f 73    int err = clos
d57e0 65 28 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20  e(pFile->h);.   
d57f0 20 20 20 69 66 28 20 65 72 72 20 29 7b 0a 20 20     if( err ){.  
d5800 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
d5810 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
d5820 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
d5830 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f 53  QLITE_IOERR_CLOS
d5840 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  E;.      }.    }
d5850 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a  .#if OS_VXWORKS.
d5860 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70      if( pFile->p
d5870 49 64 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  Id ){.      if( 
d5880 70 46 69 6c 65 2d 3e 69 73 44 65 6c 65 74 65 20  pFile->isDelete 
d5890 29 7b 0a 20 20 20 20 20 20 20 20 75 6e 6c 69 6e  ){.        unlin
d58a0 6b 28 70 46 69 6c 65 2d 3e 70 49 64 2d 3e 7a 43  k(pFile->pId->zC
d58b0 61 6e 6f 6e 69 63 61 6c 4e 61 6d 65 29 3b 0a 20  anonicalName);. 
d58c0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 76 78 77       }.      vxw
d58d0 6f 72 6b 73 52 65 6c 65 61 73 65 46 69 6c 65 49  orksReleaseFileI
d58e0 64 28 70 46 69 6c 65 2d 3e 70 49 64 29 3b 0a 20  d(pFile->pId);. 
d58f0 20 20 20 20 20 70 46 69 6c 65 2d 3e 70 49 64 20       pFile->pId 
d5900 3d 20 30 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  = 0;.    }.#endi
d5910 66 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28 22  f.    OSTRACE2("
d5920 43 4c 4f 53 45 20 20 20 25 2d 33 64 5c 6e 22 2c  CLOSE   %-3d\n",
d5930 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20 20   pFile->h);.    
d5940 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b  OpenCounter(-1);
d5950 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
d5960 65 28 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64  e(pFile->pUnused
d5970 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 46  );.    memset(pF
d5980 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 75  ile, 0, sizeof(u
d5990 6e 69 78 46 69 6c 65 29 29 3b 0a 20 20 7d 0a 20  nixFile));.  }. 
d59a0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
d59b0 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73  K;.}../*.** Clos
d59c0 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  e a file..*/.sta
d59d0 74 69 63 20 69 6e 74 20 75 6e 69 78 43 6c 6f 73  tic int unixClos
d59e0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
d59f0 69 64 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  id){.  int rc = 
d5a00 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
d5a10 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46   id ){.    unixF
d5a20 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
d5a30 69 78 46 69 6c 65 20 2a 29 69 64 3b 0a 20 20 20  ixFile *)id;.   
d5a40 20 75 6e 69 78 55 6e 6c 6f 63 6b 28 69 64 2c 20   unixUnlock(id, 
d5a50 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 75 6e  NO_LOCK);.    un
d5a60 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a  ixEnterMutex();.
d5a70 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70      if( pFile->p
d5a80 4f 70 65 6e 20 26 26 20 70 46 69 6c 65 2d 3e 70  Open && pFile->p
d5a90 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b 0a 20  Open->nLock ){. 
d5aa0 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65       /* If there
d5ab0 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e 67   are outstanding
d5ac0 20 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74 20 61   locks, do not a
d5ad0 63 74 75 61 6c 6c 79 20 63 6c 6f 73 65 20 74 68  ctually close th
d5ae0 65 20 66 69 6c 65 20 6a 75 73 74 0a 20 20 20 20  e file just.    
d5af0 20 20 2a 2a 20 79 65 74 20 62 65 63 61 75 73 65    ** yet because
d5b00 20 74 68 61 74 20 77 6f 75 6c 64 20 63 6c 65 61   that would clea
d5b10 72 20 74 68 6f 73 65 20 6c 6f 63 6b 73 2e 20 20  r those locks.  
d5b20 49 6e 73 74 65 61 64 2c 20 61 64 64 20 74 68 65  Instead, add the
d5b30 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64   file.      ** d
d5b40 65 73 63 72 69 70 74 6f 72 20 74 6f 20 70 4f 70  escriptor to pOp
d5b50 65 6e 2d 3e 70 55 6e 75 73 65 64 20 6c 69 73 74  en->pUnused list
d5b60 2e 20 20 49 74 20 77 69 6c 6c 20 62 65 20 61 75  .  It will be au
d5b70 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73  tomatically clos
d5b80 65 64 20 0a 20 20 20 20 20 20 2a 2a 20 77 68 65  ed .      ** whe
d5b90 6e 20 74 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20  n the last lock 
d5ba0 69 73 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 20  is cleared..    
d5bb0 20 20 2a 2f 0a 20 20 20 20 20 20 73 65 74 50 65    */.      setPe
d5bc0 6e 64 69 6e 67 46 64 28 70 46 69 6c 65 29 3b 0a  ndingFd(pFile);.
d5bd0 20 20 20 20 7d 0a 20 20 20 20 72 65 6c 65 61 73      }.    releas
d5be0 65 4c 6f 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d  eLockInfo(pFile-
d5bf0 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20 20 72 65 6c  >pLock);.    rel
d5c00 65 61 73 65 4f 70 65 6e 43 6e 74 28 70 46 69 6c  easeOpenCnt(pFil
d5c10 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20 20 72  e->pOpen);.    r
d5c20 63 20 3d 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c  c = closeUnixFil
d5c30 65 28 69 64 29 3b 0a 20 20 20 20 75 6e 69 78 4c  e(id);.    unixL
d5c40 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d  eaveMutex();.  }
d5c50 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
d5c60 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
d5c70 20 45 6e 64 20 6f 66 20 74 68 65 20 70 6f 73 69   End of the posi
d5c80 78 20 61 64 76 69 73 6f 72 79 20 6c 6f 63 6b 20  x advisory lock 
d5c90 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
d5ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5cb0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
d5cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5cf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
d5d00 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
d5d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5d50 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
d5d60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5d70 20 4e 6f 2d 6f 70 20 4c 6f 63 6b 69 6e 67 20 2a   No-op Locking *
d5d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d5da0 2a 0a 2a 2a 0a 2a 2a 20 4f 66 20 74 68 65 20 76  *.**.** Of the v
d5db0 61 72 69 6f 75 73 20 6c 6f 63 6b 69 6e 67 20 69  arious locking i
d5dc0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 61  mplementations a
d5dd0 76 61 69 6c 61 62 6c 65 2c 20 74 68 69 73 20 69  vailable, this i
d5de0 73 20 62 79 20 66 61 72 20 74 68 65 0a 2a 2a 20  s by far the.** 
d5df0 73 69 6d 70 6c 65 73 74 3a 20 20 6c 6f 63 6b 69  simplest:  locki
d5e00 6e 67 20 69 73 20 69 67 6e 6f 72 65 64 2e 20 20  ng is ignored.  
d5e10 4e 6f 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61  No attempt is ma
d5e20 64 65 20 74 6f 20 6c 6f 63 6b 20 74 68 65 20 64  de to lock the d
d5e30 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20  atabase.** file 
d5e40 66 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20 77  for reading or w
d5e50 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68  riting..**.** Th
d5e60 69 73 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20  is locking mode 
d5e70 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66  is appropriate f
d5e80 6f 72 20 75 73 65 20 6f 6e 20 72 65 61 64 2d 6f  or use on read-o
d5e90 6e 6c 79 20 64 61 74 61 62 61 73 65 73 0a 2a 2a  nly databases.**
d5ea0 20 28 65 78 3a 20 64 61 74 61 62 61 73 65 73 20   (ex: databases 
d5eb0 74 68 61 74 20 61 72 65 20 62 75 72 6e 65 64 20  that are burned 
d5ec0 69 6e 74 6f 20 43 44 2d 52 4f 4d 2c 20 66 6f 72  into CD-ROM, for
d5ed0 20 65 78 61 6d 70 6c 65 2e 29 20 20 49 74 20 63   example.)  It c
d5ee0 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62 65 20 75 73  an.** also be us
d5ef0 65 64 20 69 66 20 74 68 65 20 61 70 70 6c 69 63  ed if the applic
d5f00 61 74 69 6f 6e 20 65 6d 70 6c 6f 79 73 20 73 6f  ation employs so
d5f10 6d 65 20 65 78 74 65 72 6e 61 6c 20 6d 65 63 68  me external mech
d5f20 61 6e 69 73 6d 20 74 6f 0a 2a 2a 20 70 72 65 76  anism to.** prev
d5f30 65 6e 74 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73  ent simultaneous
d5f40 20 61 63 63 65 73 73 20 6f 66 20 74 68 65 20 73   access of the s
d5f50 61 6d 65 20 64 61 74 61 62 61 73 65 20 62 79 20  ame database by 
d5f60 74 77 6f 20 6f 72 20 6d 6f 72 65 0a 2a 2a 20 64  two or more.** d
d5f70 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
d5f80 6f 6e 73 2e 20 20 42 75 74 20 74 68 65 72 65 20  ons.  But there 
d5f90 69 73 20 61 20 73 65 72 69 6f 75 73 20 72 69 73  is a serious ris
d5fa0 6b 20 6f 66 20 64 61 74 61 62 61 73 65 0a 2a 2a  k of database.**
d5fb0 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 66 20 74   corruption if t
d5fc0 68 69 73 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65  his locking mode
d5fd0 20 69 73 20 75 73 65 64 20 69 6e 20 73 69 74 75   is used in situ
d5fe0 61 74 69 6f 6e 73 20 77 68 65 72 65 20 6d 75 6c  ations where mul
d5ff0 74 69 70 6c 65 0a 2a 2a 20 64 61 74 61 62 61 73  tiple.** databas
d6000 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72  e connections ar
d6010 65 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20  e accessing the 
d6020 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 66 69  same database fi
d6030 6c 65 20 61 74 20 74 68 65 20 73 61 6d 65 0a 2a  le at the same.*
d6040 2a 20 74 69 6d 65 20 61 6e 64 20 6f 6e 65 20 6f  * time and one o
d6050 72 20 6d 6f 72 65 20 6f 66 20 74 68 6f 73 65 20  r more of those 
d6060 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65 20  connections are 
d6070 77 72 69 74 69 6e 67 2e 0a 2a 2f 0a 0a 73 74 61  writing..*/..sta
d6080 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 43 68  tic int nolockCh
d6090 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
d60a0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f  sqlite3_file *No
d60b0 74 55 73 65 64 2c 20 69 6e 74 20 2a 70 52 65 73  tUsed, int *pRes
d60c0 4f 75 74 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  Out){.  UNUSED_P
d60d0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
d60e0 29 3b 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20  );.  *pResOut = 
d60f0 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  0;.  return SQLI
d6100 54 45 5f 4f 4b 3b 0a 7d 0a 73 74 61 74 69 63 20  TE_OK;.}.static 
d6110 69 6e 74 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 28 73  int nolockLock(s
d6120 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74  qlite3_file *Not
d6130 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65  Used, int NotUse
d6140 64 32 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  d2){.  UNUSED_PA
d6150 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64  RAMETER2(NotUsed
d6160 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 72  , NotUsed2);.  r
d6170 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
d6180 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f  .}.static int no
d6190 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74  lockUnlock(sqlit
d61a0 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64  e3_file *NotUsed
d61b0 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b  , int NotUsed2){
d61c0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
d61d0 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f  TER2(NotUsed, No
d61e0 74 55 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72  tUsed2);.  retur
d61f0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
d6200 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20  /*.** Close the 
d6210 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
d6220 69 6e 74 20 6e 6f 6c 6f 63 6b 43 6c 6f 73 65 28  int nolockClose(
d6230 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
d6240 29 20 7b 0a 20 20 72 65 74 75 72 6e 20 63 6c 6f  ) {.  return clo
d6250 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a  seUnixFile(id);.
d6260 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
d6270 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74  ******* End of t
d6280 68 65 20 6e 6f 2d 6f 70 20 6c 6f 63 6b 20 69 6d  he no-op lock im
d6290 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a  plementation ***
d62a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d62b0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
d62c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d62d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d62e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d62f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6300 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
d6310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d6350 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***.************
d6360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
d6370 67 69 6e 20 64 6f 74 2d 66 69 6c 65 20 4c 6f 63  gin dot-file Loc
d6380 6b 69 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  king ***********
d6390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d63a0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 6f  ***.**.** The do
d63b0 74 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 69 6d  tfile locking im
d63c0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65  plementation use
d63d0 73 20 74 68 65 20 65 78 69 73 74 61 6e 63 65 20  s the existance 
d63e0 6f 66 20 73 65 70 61 72 61 74 65 20 6c 6f 63 6b  of separate lock
d63f0 0a 2a 2a 20 66 69 6c 65 73 20 69 6e 20 6f 72 64  .** files in ord
d6400 65 72 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61 63  er to control ac
d6410 63 65 73 73 20 74 6f 20 74 68 65 20 64 61 74 61  cess to the data
d6420 62 61 73 65 2e 20 20 54 68 69 73 20 77 6f 72 6b  base.  This work
d6430 73 20 6f 6e 20 6a 75 73 74 0a 2a 2a 20 61 62 6f  s on just.** abo
d6440 75 74 20 65 76 65 72 79 20 66 69 6c 65 73 79 73  ut every filesys
d6450 74 65 6d 20 69 6d 61 67 69 6e 61 62 6c 65 2e 20  tem imaginable. 
d6460 20 42 75 74 20 74 68 65 72 65 20 61 72 65 20 73   But there are s
d6470 65 72 69 6f 75 73 20 64 6f 77 6e 73 69 64 65 73  erious downsides
d6480 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20  :.**.**    (1)  
d6490 54 68 65 72 65 20 69 73 20 7a 65 72 6f 20 63 6f  There is zero co
d64a0 6e 63 75 72 72 65 6e 63 79 2e 20 20 41 20 73 69  ncurrency.  A si
d64b0 6e 67 6c 65 20 72 65 61 64 65 72 20 62 6c 6f 63  ngle reader bloc
d64c0 6b 73 20 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20  ks all other.** 
d64d0 20 20 20 20 20 20 20 20 63 6f 6e 6e 65 63 74 69          connecti
d64e0 6f 6e 73 20 66 72 6f 6d 20 72 65 61 64 69 6e 67  ons from reading
d64f0 20 6f 72 20 77 72 69 74 69 6e 67 20 74 68 65 20   or writing the 
d6500 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  database..**.** 
d6510 20 20 20 28 32 29 20 20 41 6e 20 61 70 70 6c 69     (2)  An appli
d6520 63 61 74 69 6f 6e 20 63 72 61 73 68 20 6f 72 20  cation crash or 
d6530 70 6f 77 65 72 20 6c 6f 73 73 20 63 61 6e 20 6c  power loss can l
d6540 65 61 76 65 20 73 74 61 6c 65 20 6c 6f 63 6b 20  eave stale lock 
d6550 66 69 6c 65 73 0a 2a 2a 20 20 20 20 20 20 20 20  files.**        
d6560 20 73 69 74 74 69 6e 67 20 61 72 6f 75 6e 64 20   sitting around 
d6570 74 68 61 74 20 6e 65 65 64 20 74 6f 20 62 65 20  that need to be 
d6580 63 6c 65 61 72 65 64 20 6d 61 6e 75 61 6c 6c 79  cleared manually
d6590 2e 0a 2a 2a 0a 2a 2a 20 4e 65 76 65 72 74 68 65  ..**.** Neverthe
d65a0 6c 65 73 73 2c 20 61 20 64 6f 74 6c 6f 63 6b 20  less, a dotlock 
d65b0 69 73 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74  is an appropriat
d65c0 65 20 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 66  e locking mode f
d65d0 6f 72 20 75 73 65 20 69 66 20 6e 6f 0a 2a 2a 20  or use if no.** 
d65e0 6f 74 68 65 72 20 6c 6f 63 6b 69 6e 67 20 73 74  other locking st
d65f0 72 61 74 65 67 79 20 69 73 20 61 76 61 69 6c 61  rategy is availa
d6600 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 44 6f 74 66 69  ble..**.** Dotfi
d6610 6c 65 20 6c 6f 63 6b 69 6e 67 20 77 6f 72 6b 73  le locking works
d6620 20 62 79 20 63 72 65 61 74 69 6e 67 20 61 20 66   by creating a f
d6630 69 6c 65 20 69 6e 20 74 68 65 20 73 61 6d 65 20  ile in the same 
d6640 64 69 72 65 63 74 6f 72 79 20 61 73 20 74 68 65  directory as the
d6650 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 6e 64  .** database and
d6660 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6e   with the same n
d6670 61 6d 65 20 62 75 74 20 77 69 74 68 20 61 20 22  ame but with a "
d6680 2e 6c 6f 63 6b 22 20 65 78 74 65 6e 73 69 6f 6e  .lock" extension
d6690 20 61 64 64 65 64 2e 0a 2a 2a 20 54 68 65 20 65   added..** The e
d66a0 78 69 73 74 61 6e 63 65 20 6f 66 20 61 20 6c 6f  xistance of a lo
d66b0 63 6b 20 66 69 6c 65 20 69 6d 70 6c 69 65 73 20  ck file implies 
d66c0 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63  an EXCLUSIVE loc
d66d0 6b 2e 20 20 41 6c 6c 20 6f 74 68 65 72 20 6c 6f  k.  All other lo
d66e0 63 6b 0a 2a 2a 20 74 79 70 65 73 20 28 53 48 41  ck.** types (SHA
d66f0 52 45 44 2c 20 52 45 53 45 52 56 45 44 2c 20 50  RED, RESERVED, P
d6700 45 4e 44 49 4e 47 29 20 61 72 65 20 6d 61 70 70  ENDING) are mapp
d6710 65 64 20 69 6e 74 6f 20 45 58 43 4c 55 53 49 56  ed into EXCLUSIV
d6720 45 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  E..*/../*.** The
d6730 20 66 69 6c 65 20 73 75 66 66 69 78 20 61 64 64   file suffix add
d6740 65 64 20 74 6f 20 74 68 65 20 64 61 74 61 20 62  ed to the data b
d6750 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20  ase filename in 
d6760 6f 72 64 65 72 20 74 6f 20 63 72 65 61 74 65 20  order to create 
d6770 74 68 65 0a 2a 2a 20 6c 6f 63 6b 20 66 69 6c 65  the.** lock file
d6780 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 4f 54  ..*/.#define DOT
d6790 4c 4f 43 4b 5f 53 55 46 46 49 58 20 22 2e 6c 6f  LOCK_SUFFIX ".lo
d67a0 63 6b 22 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ck"../*.** This 
d67b0 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69  routine checks i
d67c0 66 20 74 68 65 72 65 20 69 73 20 61 20 52 45 53  f there is a RES
d67d0 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20  ERVED lock held 
d67e0 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64  on the specified
d67f0 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68 69 73  .** file by this
d6800 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70 72   or any other pr
d6810 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68 20 61  ocess. If such a
d6820 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c 20 73   lock is held, s
d6830 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a 20 74  et *pResOut.** t
d6840 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c  o a non-zero val
d6850 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a 70 52  ue otherwise *pR
d6860 65 73 4f 75 74 20 69 73 20 73 65 74 20 74 6f 20  esOut is set to 
d6870 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74 75 72  zero.  The retur
d6880 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65  n value.** is se
d6890 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b 20 75  t to SQLITE_OK u
d68a0 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65 72 72  nless an I/O err
d68b0 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67  or occurs during
d68c0 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 2e 0a   lock checking..
d68d0 2a 2a 0a 2a 2a 20 49 6e 20 64 6f 74 66 69 6c 65  **.** In dotfile
d68e0 20 6c 6f 63 6b 69 6e 67 2c 20 65 69 74 68 65 72   locking, either
d68f0 20 61 20 6c 6f 63 6b 20 65 78 69 73 74 73 20 6f   a lock exists o
d6900 72 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e 20 20  r it does not.  
d6910 53 6f 20 69 6e 20 74 68 69 73 0a 2a 2a 20 76 61  So in this.** va
d6920 72 69 61 74 69 6f 6e 20 6f 66 20 43 68 65 63 6b  riation of Check
d6930 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 29 2c 20  ReservedLock(), 
d6940 2a 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20  *pResOut is set 
d6950 74 6f 20 74 72 75 65 20 69 66 20 61 6e 79 20 6c  to true if any l
d6960 6f 63 6b 0a 2a 2a 20 69 73 20 68 65 6c 64 20 6f  ock.** is held o
d6970 6e 20 74 68 65 20 66 69 6c 65 20 61 6e 64 20 66  n the file and f
d6980 61 6c 73 65 20 69 66 20 74 68 65 20 66 69 6c 65  alse if the file
d6990 20 69 73 20 75 6e 6c 6f 63 6b 65 64 2e 0a 2a 2f   is unlocked..*/
d69a0 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c  .static int dotl
d69b0 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64  ockCheckReserved
d69c0 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  Lock(sqlite3_fil
d69d0 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73  e *id, int *pRes
d69e0 4f 75 74 29 20 7b 0a 20 20 69 6e 74 20 72 63 20  Out) {.  int rc 
d69f0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
d6a00 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b  nt reserved = 0;
d6a10 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
d6a20 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
d6a30 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49  id;..  SimulateI
d6a40 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53  OError( return S
d6a50 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43  QLITE_IOERR_CHEC
d6a60 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29  KRESERVEDLOCK; )
d6a70 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70  ;.  .  assert( p
d6a80 46 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68  File );..  /* Ch
d6a90 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20  eck if a thread 
d6aa0 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20  in this process 
d6ab0 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63  holds such a loc
d6ac0 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65  k */.  if( pFile
d6ad0 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45  ->locktype>SHARE
d6ae0 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 2f 2a  D_LOCK ){.    /*
d6af0 20 45 69 74 68 65 72 20 74 68 69 73 20 63 6f 6e   Either this con
d6b00 6e 65 63 74 69 6f 6e 20 6f 72 20 73 6f 6d 65 20  nection or some 
d6b10 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e  other connection
d6b20 20 69 6e 20 74 68 65 20 73 61 6d 65 20 70 72 6f   in the same pro
d6b30 63 65 73 73 0a 20 20 20 20 2a 2a 20 68 6f 6c 64  cess.    ** hold
d6b40 73 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  s a lock on the 
d6b50 66 69 6c 65 2e 20 20 4e 6f 20 6e 65 65 64 20 74  file.  No need t
d6b60 6f 20 63 68 65 63 6b 20 66 75 72 74 68 65 72 2e  o check further.
d6b70 20 2a 2f 0a 20 20 20 20 72 65 73 65 72 76 65 64   */.    reserved
d6b80 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 1;.  }else{. 
d6b90 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63 6b 20 69     /* The lock i
d6ba0 73 20 68 65 6c 64 20 69 66 20 61 6e 64 20 6f 6e  s held if and on
d6bb0 6c 79 20 69 66 20 74 68 65 20 6c 6f 63 6b 66 69  ly if the lockfi
d6bc0 6c 65 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20  le exists */.   
d6bd0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 6f   const char *zLo
d6be0 63 6b 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20  ckFile = (const 
d6bf0 63 68 61 72 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63  char*)pFile->loc
d6c00 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20  kingContext;.   
d6c10 20 72 65 73 65 72 76 65 64 20 3d 20 61 63 63 65   reserved = acce
d6c20 73 73 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20 30 29  ss(zLockFile, 0)
d6c30 3d 3d 30 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41  ==0;.  }.  OSTRA
d6c40 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43  CE4("TEST WR-LOC
d6c50 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70  K %d %d %d\n", p
d6c60 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73  File->h, rc, res
d6c70 65 72 76 65 64 29 3b 0a 20 20 2a 70 52 65 73 4f  erved);.  *pResO
d6c80 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20  ut = reserved;. 
d6c90 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
d6ca0 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69  *.** Lock the fi
d6cb0 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b  le with the lock
d6cc0 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61   specified by pa
d6cd0 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65  rameter locktype
d6ce0 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65   - one.** of the
d6cf0 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a   following:.**.*
d6d00 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44  *     (1) SHARED
d6d10 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29  _LOCK.**     (2)
d6d20 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a   RESERVED_LOCK.*
d6d30 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e  *     (3) PENDIN
d6d40 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34  G_LOCK.**     (4
d6d50 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ) EXCLUSIVE_LOCK
d6d60 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73  .**.** Sometimes
d6d70 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67   when requesting
d6d80 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c   one lock state,
d6d90 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b   additional lock
d6da0 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69   states.** are i
d6db0 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65  nserted in betwe
d6dc0 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67  en.  The locking
d6dd0 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f   might fail on o
d6de0 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a  ne of the later.
d6df0 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c  ** transitions l
d6e00 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20  eaving the lock 
d6e10 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20  state different 
d6e20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61  from what it sta
d6e30 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c  rted but.** stil
d6e40 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67  l short of its g
d6e50 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  oal.  The follow
d6e60 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20  ing chart shows 
d6e70 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74  the allowed.** t
d6e80 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74  ransitions and t
d6e90 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65  he inserted inte
d6ea0 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a  rmediate states:
d6eb0 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b  .**.**    UNLOCK
d6ec0 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20  ED -> SHARED.** 
d6ed0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53     SHARED -> RES
d6ee0 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52  ERVED.**    SHAR
d6ef0 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20  ED -> (PENDING) 
d6f00 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20  -> EXCLUSIVE.** 
d6f10 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28     RESERVED -> (
d6f20 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c  PENDING) -> EXCL
d6f30 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44  USIVE.**    PEND
d6f40 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  ING -> EXCLUSIVE
d6f50 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
d6f60 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e  ine will only in
d6f70 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20  crease a lock.  
d6f80 55 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f  Use the sqlite3O
d6f90 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75  sUnlock().** rou
d6fa0 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20  tine to lower a 
d6fb0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a  locking level..*
d6fc0 2a 0a 2a 2a 20 57 69 74 68 20 64 6f 74 66 69 6c  *.** With dotfil
d6fd0 65 20 6c 6f 63 6b 69 6e 67 2c 20 77 65 20 72 65  e locking, we re
d6fe0 61 6c 6c 79 20 6f 6e 6c 79 20 73 75 70 70 6f 72  ally only suppor
d6ff0 74 20 73 74 61 74 65 20 28 34 29 3a 20 45 58 43  t state (4): EXC
d7000 4c 55 53 49 56 45 2e 0a 2a 2a 20 42 75 74 20 77  LUSIVE..** But w
d7010 65 20 74 72 61 63 6b 20 74 68 65 20 6f 74 68 65  e track the othe
d7020 72 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 73  r locking levels
d7030 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 2a 2f 0a   internally..*/.
d7040 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f  static int dotlo
d7050 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  ckLock(sqlite3_f
d7060 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63  ile *id, int loc
d7070 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46  ktype) {.  unixF
d7080 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
d7090 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e  ixFile*)id;.  in
d70a0 74 20 66 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4c  t fd;.  char *zL
d70b0 6f 63 6b 46 69 6c 65 20 3d 20 28 63 68 61 72 20  ockFile = (char 
d70c0 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67  *)pFile->locking
d70d0 43 6f 6e 74 65 78 74 3b 0a 20 20 69 6e 74 20 72  Context;.  int r
d70e0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
d70f0 0a 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65  .  /* If we have
d7100 20 61 6e 79 20 6c 6f 63 6b 2c 20 74 68 65 6e 20   any lock, then 
d7110 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61 6c  the lock file al
d7120 72 65 61 64 79 20 65 78 69 73 74 73 2e 20 20 41  ready exists.  A
d7130 6c 6c 20 77 65 20 68 61 76 65 0a 20 20 2a 2a 20  ll we have.  ** 
d7140 74 6f 20 64 6f 20 69 73 20 61 64 6a 75 73 74 20  to do is adjust 
d7150 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20 72 65 63  our internal rec
d7160 6f 72 64 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20  ord of the lock 
d7170 6c 65 76 65 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66  level..  */.  if
d7180 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
d7190 65 20 3e 20 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20  e > NO_LOCK ){. 
d71a0 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
d71b0 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 23  pe = locktype;.#
d71c0 69 66 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 20  if !OS_VXWORKS. 
d71d0 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 75 70 64     /* Always upd
d71e0 61 74 65 20 74 68 65 20 74 69 6d 65 73 74 61 6d  ate the timestam
d71f0 70 20 6f 6e 20 74 68 65 20 6f 6c 64 20 66 69 6c  p on the old fil
d7200 65 20 2a 2f 0a 20 20 20 20 75 74 69 6d 65 73 28  e */.    utimes(
d7210 7a 4c 6f 63 6b 46 69 6c 65 2c 20 4e 55 4c 4c 29  zLockFile, NULL)
d7220 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 65 74  ;.#endif.    ret
d7230 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
d7240 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62 20   }.  .  /* grab 
d7250 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
d7260 6b 20 2a 2f 0a 20 20 66 64 20 3d 20 6f 70 65 6e  k */.  fd = open
d7270 28 7a 4c 6f 63 6b 46 69 6c 65 2c 4f 5f 52 44 4f  (zLockFile,O_RDO
d7280 4e 4c 59 7c 4f 5f 43 52 45 41 54 7c 4f 5f 45 58  NLY|O_CREAT|O_EX
d7290 43 4c 2c 30 36 30 30 29 3b 0a 20 20 69 66 28 20  CL,0600);.  if( 
d72a0 66 64 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 66  fd<0 ){.    /* f
d72b0 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e 2f 63 72  ailed to open/cr
d72c0 65 61 74 65 20 74 68 65 20 66 69 6c 65 2c 20 73  eate the file, s
d72d0 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d 61 79 20  omeone else may 
d72e0 68 61 76 65 20 73 74 6f 6c 65 6e 20 74 68 65 20  have stolen the 
d72f0 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74 20  lock */.    int 
d7300 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
d7310 20 20 20 20 69 66 28 20 45 45 58 49 53 54 20 3d      if( EEXIST =
d7320 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20 20 20 20  = tErrno ){.    
d7330 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55    rc = SQLITE_BU
d7340 53 59 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b  SY;.    } else {
d7350 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
d7360 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78  teErrorFromPosix
d7370 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51  Error(tErrno, SQ
d7380 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29  LITE_IOERR_LOCK)
d7390 3b 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c  ;.      if( IS_L
d73a0 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b  OCK_ERROR(rc) ){
d73b0 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  .        pFile->
d73c0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72  lastErrno = tErr
d73d0 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  no;.      }.    
d73e0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  }.    return rc;
d73f0 0a 20 20 7d 20 0a 20 20 69 66 28 20 63 6c 6f 73  .  } .  if( clos
d7400 65 28 66 64 29 20 29 7b 0a 20 20 20 20 70 46 69  e(fd) ){.    pFi
d7410 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
d7420 65 72 72 6e 6f 3b 0a 20 20 20 20 72 63 20 3d 20  errno;.    rc = 
d7430 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 4c 4f  SQLITE_IOERR_CLO
d7440 53 45 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20  SE;.  }.  .  /* 
d7450 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68 65 20  got it, set the 
d7460 74 79 70 65 20 61 6e 64 20 72 65 74 75 72 6e 20  type and return 
d7470 6f 6b 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 6c  ok */.  pFile->l
d7480 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79  ocktype = lockty
d7490 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  pe;.  return rc;
d74a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20  .}../*.** Lower 
d74b0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  the locking leve
d74c0 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69  l on file descri
d74d0 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f  ptor pFile to lo
d74e0 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70  cktype.  locktyp
d74f0 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74  e.** must be eit
d7500 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53  her NO_LOCK or S
d7510 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a  HARED_LOCK..**.*
d7520 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67  * If the locking
d7530 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69   level of the fi
d7540 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73  le descriptor is
d7550 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62   already at or b
d7560 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75  elow.** the requ
d7570 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65  ested locking le
d7580 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  vel, this routin
d7590 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  e is a no-op..**
d75a0 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 6c 6f 63  .** When the loc
d75b0 6b 69 6e 67 20 6c 65 76 65 6c 20 72 65 61 63 68  king level reach
d75c0 65 73 20 4e 4f 5f 4c 4f 43 4b 2c 20 64 65 6c 65  es NO_LOCK, dele
d75d0 74 65 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65  te the lock file
d75e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
d75f0 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71  dotlockUnlock(sq
d7600 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
d7610 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a  int locktype) {.
d7620 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
d7630 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
d7640 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63 6b  d;.  char *zLock
d7650 46 69 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 70  File = (char *)p
d7660 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
d7670 74 65 78 74 3b 0a 0a 20 20 61 73 73 65 72 74 28  text;..  assert(
d7680 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52   pFile );.  OSTR
d7690 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64  ACE5("UNLOCK  %d
d76a0 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d 25   %d was %d pid=%
d76b0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
d76c0 6c 6f 63 6b 74 79 70 65 2c 0a 09 20 20 20 70 46  locktype,..   pF
d76d0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67  ile->locktype, g
d76e0 65 74 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65  etpid());.  asse
d76f0 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48  rt( locktype<=SH
d7700 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a  ARED_LOCK );.  .
d7710 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f    /* no-op if po
d7720 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20  ssible */.  if( 
d7730 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
d7740 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20  =locktype ){.   
d7750 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
d7760 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f 20  K;.  }..  /* To 
d7770 64 6f 77 6e 67 72 61 64 65 20 74 6f 20 73 68 61  downgrade to sha
d7780 72 65 64 2c 20 73 69 6d 70 6c 79 20 75 70 64 61  red, simply upda
d7790 74 65 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20  te our internal 
d77a0 6e 6f 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20  notion of the.  
d77b0 2a 2a 20 6c 6f 63 6b 20 73 74 61 74 65 2e 20 20  ** lock state.  
d77c0 4e 6f 20 6e 65 65 64 20 74 6f 20 6d 65 73 73 20  No need to mess 
d77d0 77 69 74 68 20 74 68 65 20 66 69 6c 65 20 6f 6e  with the file on
d77e0 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66   disk..  */.  if
d77f0 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ( locktype==SHAR
d7800 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 70  ED_LOCK ){.    p
d7810 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
d7820 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20   SHARED_LOCK;.  
d7830 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d7840 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20  OK;.  }.  .  /* 
d7850 54 6f 20 66 75 6c 6c 79 20 75 6e 6c 6f 63 6b 20  To fully unlock 
d7860 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 64 65  the database, de
d7870 6c 65 74 65 20 74 68 65 20 6c 6f 63 6b 20 66 69  lete the lock fi
d7880 6c 65 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  le */.  assert( 
d7890 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43  locktype==NO_LOC
d78a0 4b 20 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6e  K );.  if( unlin
d78b0 6b 28 7a 4c 6f 63 6b 46 69 6c 65 29 20 29 7b 0a  k(zLockFile) ){.
d78c0 20 20 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a      int rc = 0;.
d78d0 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d      int tErrno =
d78e0 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28 20   errno;.    if( 
d78f0 45 4e 4f 45 4e 54 20 21 3d 20 74 45 72 72 6e 6f  ENOENT != tErrno
d7900 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
d7910 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
d7920 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
d7930 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e   SQLITE_IOERR_UN
d7940 4c 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20 20  LOCK);.    }.   
d7950 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52   if( IS_LOCK_ERR
d7960 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20  OR(rc) ){.      
d7970 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
d7980 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d   = tErrno;.    }
d7990 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 20  .    return rc; 
d79a0 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f  .  }.  pFile->lo
d79b0 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b  cktype = NO_LOCK
d79c0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
d79d0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
d79e0 6c 6f 73 65 20 61 20 66 69 6c 65 2e 20 20 4d 61  lose a file.  Ma
d79f0 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b  ke sure the lock
d7a00 20 68 61 73 20 62 65 65 6e 20 72 65 6c 65 61 73   has been releas
d7a10 65 64 20 62 65 66 6f 72 65 20 63 6c 6f 73 69 6e  ed before closin
d7a20 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
d7a30 20 64 6f 74 6c 6f 63 6b 43 6c 6f 73 65 28 73 71   dotlockClose(sq
d7a40 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20  lite3_file *id) 
d7a50 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66  {.  int rc;.  if
d7a60 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78  ( id ){.    unix
d7a70 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
d7a80 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20  nixFile*)id;.   
d7a90 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69   dotlockUnlock(i
d7aa0 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20  d, NO_LOCK);.   
d7ab0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46   sqlite3_free(pF
d7ac0 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
d7ad0 65 78 74 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d  ext);.  }.  rc =
d7ae0 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69   closeUnixFile(i
d7af0 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  d);.  return rc;
d7b00 0a 7d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}./************
d7b10 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 68  ****** End of th
d7b20 65 20 64 6f 74 2d 66 69 6c 65 20 6c 6f 63 6b 20  e dot-file lock 
d7b30 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
d7b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7b50 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
d7b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7ba0 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
d7bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7bf0 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***.************
d7c00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
d7c10 65 67 69 6e 20 66 6c 6f 63 6b 20 4c 6f 63 6b 69  egin flock Locki
d7c20 6e 67 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ng *************
d7c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d7c40 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68  ***.**.** Use th
d7c50 65 20 66 6c 6f 63 6b 28 29 20 73 79 73 74 65 6d  e flock() system
d7c60 20 63 61 6c 6c 20 74 6f 20 64 6f 20 66 69 6c 65   call to do file
d7c70 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20   locking..**.** 
d7c80 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20  flock() locking 
d7c90 69 73 20 6c 69 6b 65 20 64 6f 74 2d 66 69 6c 65  is like dot-file
d7ca0 20 6c 6f 63 6b 69 6e 67 20 69 6e 20 74 68 61 74   locking in that
d7cb0 20 74 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20   the various.** 
d7cc0 66 69 6e 65 2d 67 72 61 69 6e 20 6c 6f 63 6b 69  fine-grain locki
d7cd0 6e 67 20 6c 65 76 65 6c 73 20 73 75 70 70 6f 72  ng levels suppor
d7ce0 74 65 64 20 62 79 20 53 51 4c 69 74 65 20 61 72  ted by SQLite ar
d7cf0 65 20 63 6f 6c 6c 61 70 73 65 64 20 69 6e 74 6f  e collapsed into
d7d00 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 65 78 63  .** a single exc
d7d10 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 49 6e  lusive lock.  In
d7d20 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 53 48   other words, SH
d7d30 41 52 45 44 2c 20 52 45 53 45 52 56 45 44 2c 20  ARED, RESERVED, 
d7d40 61 6e 64 0a 2a 2a 20 50 45 4e 44 49 4e 47 20 6c  and.** PENDING l
d7d50 6f 63 6b 73 20 61 72 65 20 74 68 65 20 73 61 6d  ocks are the sam
d7d60 65 20 74 68 69 6e 67 20 61 73 20 61 6e 20 45 58  e thing as an EX
d7d70 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 53  CLUSIVE lock.  S
d7d80 51 4c 69 74 65 0a 2a 2a 20 73 74 69 6c 6c 20 77  QLite.** still w
d7d90 6f 72 6b 73 20 77 68 65 6e 20 79 6f 75 20 64 6f  orks when you do
d7da0 20 74 68 69 73 2c 20 62 75 74 20 63 6f 6e 63 75   this, but concu
d7db0 72 72 65 6e 63 79 20 69 73 20 72 65 64 75 63 65  rrency is reduce
d7dc0 64 20 73 69 6e 63 65 0a 2a 2a 20 6f 6e 6c 79 20  d since.** only 
d7dd0 61 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73  a single process
d7de0 20 63 61 6e 20 62 65 20 72 65 61 64 69 6e 67 20   can be reading 
d7df0 74 68 65 20 64 61 74 61 62 61 73 65 20 61 74 20  the database at 
d7e00 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6d  a time..**.** Om
d7e10 69 74 20 74 68 69 73 20 73 65 63 74 69 6f 6e 20  it this section 
d7e20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  if SQLITE_ENABLE
d7e30 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 69  _LOCKING_STYLE i
d7e40 73 20 74 75 72 6e 65 64 20 6f 66 66 20 6f 72 20  s turned off or 
d7e50 69 66 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67 20  if.** compiling 
d7e60 66 6f 72 20 56 58 57 4f 52 4b 53 2e 0a 2a 2f 0a  for VXWORKS..*/.
d7e70 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  #if SQLITE_ENABL
d7e80 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
d7e90 26 26 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 0a  && !OS_VXWORKS..
d7ea0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
d7eb0 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65  ne checks if the
d7ec0 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44  re is a RESERVED
d7ed0 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68   lock held on th
d7ee0 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66  e specified.** f
d7ef0 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61  ile by this or a
d7f00 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  ny other process
d7f10 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b  . If such a lock
d7f20 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70   is held, set *p
d7f30 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e  ResOut.** to a n
d7f40 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74  on-zero value ot
d7f50 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74  herwise *pResOut
d7f60 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e   is set to zero.
d7f70 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c    The return val
d7f80 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20  ue.** is set to 
d7f90 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73  SQLITE_OK unless
d7fa0 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63   an I/O error oc
d7fb0 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b  curs during lock
d7fc0 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74   checking..*/.st
d7fd0 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 43 68  atic int flockCh
d7fe0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
d7ff0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
d8000 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b  , int *pResOut){
d8010 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
d8020 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73  TE_OK;.  int res
d8030 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69  erved = 0;.  uni
d8040 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
d8050 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20  unixFile*)id;.  
d8060 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
d8070 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54  or( return SQLIT
d8080 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53  E_IOERR_CHECKRES
d8090 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20  ERVEDLOCK; );.  
d80a0 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
d80b0 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63   );.  .  /* Chec
d80c0 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e  k if a thread in
d80d0 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f   this process ho
d80e0 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20  lds such a lock 
d80f0 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  */.  if( pFile->
d8100 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f  locktype>SHARED_
d8110 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65  LOCK ){.    rese
d8120 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  rved = 1;.  }.  
d8130 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20  .  /* Otherwise 
d8140 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65  see if some othe
d8150 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20  r process holds 
d8160 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65  it. */.  if( !re
d8170 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20 2f 2a  served ){.    /*
d8180 20 61 74 74 65 6d 70 74 20 74 6f 20 67 65 74 20   attempt to get 
d8190 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20  the lock */.    
d81a0 69 6e 74 20 6c 72 63 20 3d 20 66 6c 6f 63 6b 28  int lrc = flock(
d81b0 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 45  pFile->h, LOCK_E
d81c0 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 3b 0a 20 20  X | LOCK_NB);.  
d81d0 20 20 69 66 28 20 21 6c 72 63 20 29 7b 0a 20 20    if( !lrc ){.  
d81e0 20 20 20 20 2f 2a 20 67 6f 74 20 74 68 65 20 6c      /* got the l
d81f0 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74 20 2a  ock, unlock it *
d8200 2f 0a 20 20 20 20 20 20 6c 72 63 20 3d 20 66 6c  /.      lrc = fl
d8210 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f  ock(pFile->h, LO
d8220 43 4b 5f 55 4e 29 3b 0a 20 20 20 20 20 20 69 66  CK_UN);.      if
d8230 20 28 20 6c 72 63 20 29 20 7b 0a 20 20 20 20 20   ( lrc ) {.     
d8240 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20     int tErrno = 
d8250 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 2f  errno;.        /
d8260 2a 20 75 6e 6c 6f 63 6b 20 66 61 69 6c 65 64 20  * unlock failed 
d8270 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f  with an error */
d8280 0a 20 20 20 20 20 20 20 20 6c 72 63 20 3d 20 73  .        lrc = s
d8290 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
d82a0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
d82b0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e   SQLITE_IOERR_UN
d82c0 4c 4f 43 4b 29 3b 20 0a 20 20 20 20 20 20 20 20  LOCK); .        
d82d0 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f  if( IS_LOCK_ERRO
d82e0 52 28 6c 72 63 29 20 29 7b 0a 20 20 20 20 20 20  R(lrc) ){.      
d82f0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
d8300 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20  rrno = tErrno;. 
d8310 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 72           rc = lr
d8320 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  c;.        }.   
d8330 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20     }.    } else 
d8340 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 45 72 72  {.      int tErr
d8350 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
d8360 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a    reserved = 1;.
d8370 20 20 20 20 20 20 2f 2a 20 73 6f 6d 65 6f 6e 65        /* someone
d8380 20 65 6c 73 65 20 6d 69 67 68 74 20 68 61 76 65   else might have
d8390 20 69 74 20 72 65 73 65 72 76 65 64 20 2a 2f 0a   it reserved */.
d83a0 20 20 20 20 20 20 6c 72 63 20 3d 20 73 71 6c 69        lrc = sqli
d83b0 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78  teErrorFromPosix
d83c0 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51  Error(tErrno, SQ
d83d0 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29  LITE_IOERR_LOCK)
d83e0 3b 20 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f  ; .      if( IS_
d83f0 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 29 20  LOCK_ERROR(lrc) 
d8400 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65  ){.        pFile
d8410 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45  ->lastErrno = tE
d8420 72 72 6e 6f 3b 0a 20 20 20 20 20 20 20 20 72 63  rrno;.        rc
d8430 20 3d 20 6c 72 63 3b 0a 20 20 20 20 20 20 7d 0a   = lrc;.      }.
d8440 20 20 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 52      }.  }.  OSTR
d8450 41 43 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f  ACE4("TEST WR-LO
d8460 43 4b 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20  CK %d %d %d\n", 
d8470 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65  pFile->h, rc, re
d8480 73 65 72 76 65 64 29 3b 0a 0a 23 69 66 64 65 66  served);..#ifdef
d8490 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46   SQLITE_IGNORE_F
d84a0 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53  LOCK_LOCK_ERRORS
d84b0 0a 20 20 69 66 28 20 28 72 63 20 26 20 53 51 4c  .  if( (rc & SQL
d84c0 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51  ITE_IOERR) == SQ
d84d0 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20  LITE_IOERR ){.  
d84e0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
d84f0 3b 0a 20 20 20 20 72 65 73 65 72 76 65 64 3d 31  ;.    reserved=1
d8500 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;.  }.#endif /* 
d8510 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c  SQLITE_IGNORE_FL
d8520 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 20  OCK_LOCK_ERRORS 
d8530 2a 2f 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20  */.  *pResOut = 
d8540 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75  reserved;.  retu
d8550 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
d8560 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69  Lock the file wi
d8570 74 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63  th the lock spec
d8580 69 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74  ified by paramet
d8590 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e  er locktype - on
d85a0 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c  e.** of the foll
d85b0 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  owing:.**.**    
d85c0 20 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b   (1) SHARED_LOCK
d85d0 0a 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45  .**     (2) RESE
d85e0 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20  RVED_LOCK.**    
d85f0 20 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43   (3) PENDING_LOC
d8600 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43  K.**     (4) EXC
d8610 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a  LUSIVE_LOCK.**.*
d8620 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e  * Sometimes when
d8630 20 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20   requesting one 
d8640 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69  lock state, addi
d8650 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74  tional lock stat
d8660 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74  es.** are insert
d8670 65 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20  ed in between.  
d8680 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68  The locking migh
d8690 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66  t fail on one of
d86a0 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72   the later.** tr
d86b0 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e  ansitions leavin
d86c0 67 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65  g the lock state
d86d0 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20   different from 
d86e0 77 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20  what it started 
d86f0 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f  but.** still sho
d8700 72 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20  rt of its goal. 
d8710 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   The following c
d8720 68 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61  hart shows the a
d8730 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69  llowed.** transi
d8740 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e  tions and the in
d8750 73 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69  serted intermedi
d8760 61 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a  ate states:.**.*
d8770 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e  *    UNLOCKED ->
d8780 20 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48   SHARED.**    SH
d8790 41 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44  ARED -> RESERVED
d87a0 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e  .**    SHARED ->
d87b0 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58   (PENDING) -> EX
d87c0 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45  CLUSIVE.**    RE
d87d0 53 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49  SERVED -> (PENDI
d87e0 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  NG) -> EXCLUSIVE
d87f0 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d  .**    PENDING -
d8800 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a  > EXCLUSIVE.**.*
d8810 2a 20 66 6c 6f 63 6b 28 29 20 6f 6e 6c 79 20 72  * flock() only r
d8820 65 61 6c 6c 79 20 73 75 70 70 6f 72 74 20 45 58  eally support EX
d8830 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 2e 20 20  CLUSIVE locks.  
d8840 57 65 20 74 72 61 63 6b 20 69 6e 74 65 72 6d 65  We track interme
d8850 64 69 61 74 65 0a 2a 2a 20 6c 6f 63 6b 20 73 74  diate.** lock st
d8860 61 74 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69  ates in the sqli
d8870 74 65 33 5f 66 69 6c 65 20 73 74 72 75 63 74 75  te3_file structu
d8880 72 65 2c 20 62 75 74 20 61 6c 6c 20 6c 6f 63 6b  re, but all lock
d8890 73 20 53 48 41 52 45 44 20 6f 72 0a 2a 2a 20 61  s SHARED or.** a
d88a0 62 6f 76 65 20 61 72 65 20 72 65 61 6c 6c 79 20  bove are really 
d88b0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 73 20  EXCLUSIVE locks 
d88c0 61 6e 64 20 65 78 63 6c 75 64 65 20 61 6c 6c 20  and exclude all 
d88d0 6f 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 20  other processes 
d88e0 66 72 6f 6d 0a 2a 2a 20 61 63 63 65 73 73 20 74  from.** access t
d88f0 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  he file..**.** T
d8900 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c  his routine will
d8910 20 6f 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61   only increase a
d8920 20 6c 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20   lock.  Use the 
d8930 73 71 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28  sqlite3OsUnlock(
d8940 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20  ).** routine to 
d8950 6c 6f 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20  lower a locking 
d8960 6c 65 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63  level..*/.static
d8970 20 69 6e 74 20 66 6c 6f 63 6b 4c 6f 63 6b 28 73   int flockLock(s
d8980 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
d8990 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b   int locktype) {
d89a0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
d89b0 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c  TE_OK;.  unixFil
d89c0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
d89d0 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73  File*)id;..  ass
d89e0 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 0a 20  ert( pFile );.. 
d89f0 20 2f 2a 20 69 66 20 77 65 20 61 6c 72 65 61 64   /* if we alread
d8a00 79 20 68 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69  y have a lock, i
d8a10 74 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20  t is exclusive. 
d8a20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75   .  ** Just adju
d8a30 73 74 20 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e  st level and pun
d8a40 74 20 6f 6e 20 6f 75 74 74 61 20 68 65 72 65 2e  t on outta here.
d8a50 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d   */.  if (pFile-
d8a60 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c  >locktype > NO_L
d8a70 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65  OCK) {.    pFile
d8a80 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63  ->locktype = loc
d8a90 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 74 75 72  ktype;.    retur
d8aa0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
d8ab0 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62 20 61 6e  .  .  /* grab an
d8ac0 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
d8ad0 2a 2f 0a 20 20 0a 20 20 69 66 20 28 66 6c 6f 63  */.  .  if (floc
d8ae0 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b  k(pFile->h, LOCK
d8af0 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29 29 20  _EX | LOCK_NB)) 
d8b00 7b 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f  {.    int tErrno
d8b10 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 2f 2a   = errno;.    /*
d8b20 20 64 69 64 6e 27 74 20 67 65 74 2c 20 6d 75 73   didn't get, mus
d8b30 74 20 62 65 20 62 75 73 79 20 2a 2f 0a 20 20 20  t be busy */.   
d8b40 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f   rc = sqliteErro
d8b50 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28  rFromPosixError(
d8b60 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49  tErrno, SQLITE_I
d8b70 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  OERR_LOCK);.    
d8b80 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f  if( IS_LOCK_ERRO
d8b90 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70  R(rc) ){.      p
d8ba0 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
d8bb0 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a  = tErrno;.    }.
d8bc0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f    } else {.    /
d8bd0 2a 20 67 6f 74 20 69 74 2c 20 73 65 74 20 74 68  * got it, set th
d8be0 65 20 74 79 70 65 20 61 6e 64 20 72 65 74 75 72  e type and retur
d8bf0 6e 20 6f 6b 20 2a 2f 0a 20 20 20 20 70 46 69 6c  n ok */.    pFil
d8c00 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f  e->locktype = lo
d8c10 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 4f 53  cktype;.  }.  OS
d8c20 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20  TRACE4("LOCK    
d8c30 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69  %d %s %s\n", pFi
d8c40 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e  le->h, locktypeN
d8c50 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a  ame(locktype), .
d8c60 20 20 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53             rc==S
d8c70 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20  QLITE_OK ? "ok" 
d8c80 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 23 69 66  : "failed");.#if
d8c90 64 65 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52  def SQLITE_IGNOR
d8ca0 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52  E_FLOCK_LOCK_ERR
d8cb0 4f 52 53 0a 20 20 69 66 28 20 28 72 63 20 26 20  ORS.  if( (rc & 
d8cc0 53 51 4c 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d  SQLITE_IOERR) ==
d8cd0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b   SQLITE_IOERR ){
d8ce0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
d8cf0 5f 42 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64 69  _BUSY;.  }.#endi
d8d00 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f  f /* SQLITE_IGNO
d8d10 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52  RE_FLOCK_LOCK_ER
d8d20 52 4f 52 53 20 2a 2f 0a 20 20 72 65 74 75 72 6e  RORS */.  return
d8d30 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4c   rc;.}.../*.** L
d8d40 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67  ower the locking
d8d50 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64   level on file d
d8d60 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65 20  escriptor pFile 
d8d70 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c 6f  to locktype.  lo
d8d80 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20 62  cktype.** must b
d8d90 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b  e either NO_LOCK
d8da0 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b 2e   or SHARED_LOCK.
d8db0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f  .**.** If the lo
d8dc0 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20 74  cking level of t
d8dd0 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  he file descript
d8de0 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61 74  or is already at
d8df0 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68 65   or below.** the
d8e00 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 69   requested locki
d8e10 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20 72  ng level, this r
d8e20 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
d8e30 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  p..*/.static int
d8e40 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c   flockUnlock(sql
d8e50 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
d8e60 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20  nt locktype) {. 
d8e70 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
d8e80 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
d8e90 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70  ;.  .  assert( p
d8ea0 46 69 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43  File );.  OSTRAC
d8eb0 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25  E5("UNLOCK  %d %
d8ec0 64 20 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c  d was %d pid=%d\
d8ed0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  n", pFile->h, lo
d8ee0 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20  cktype,.        
d8ef0 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
d8f00 70 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 20  pe, getpid());. 
d8f10 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
d8f20 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29  e<=SHARED_LOCK )
d8f30 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20  ;.  .  /* no-op 
d8f40 69 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a 20  if possible */. 
d8f50 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
d8f60 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20 29  type==locktype )
d8f70 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
d8f80 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20  ITE_OK;.  }.  . 
d8f90 20 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20 6a   /* shared can j
d8fa0 75 73 74 20 62 65 20 73 65 74 20 62 65 63 61 75  ust be set becau
d8fb0 73 65 20 77 65 20 61 6c 77 61 79 73 20 68 61 76  se we always hav
d8fc0 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 2a  e an exclusive *
d8fd0 2f 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 70 65  /.  if (locktype
d8fe0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20 7b  ==SHARED_LOCK) {
d8ff0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  .    pFile->lock
d9000 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b  type = locktype;
d9010 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d9020 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20  TE_OK;.  }.  .  
d9030 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 2c 20 75  /* no, really, u
d9040 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 6e 74 20  nlock. */.  int 
d9050 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65  rc = flock(pFile
d9060 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e 29 3b 0a 20  ->h, LOCK_UN);. 
d9070 20 69 66 20 28 72 63 29 20 7b 0a 20 20 20 20 69   if (rc) {.    i
d9080 6e 74 20 72 2c 20 74 45 72 72 6e 6f 20 3d 20 65  nt r, tErrno = e
d9090 72 72 6e 6f 3b 0a 20 20 20 20 72 20 3d 20 73 71  rrno;.    r = sq
d90a0 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73  liteErrorFromPos
d90b0 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20  ixError(tErrno, 
d90c0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c  SQLITE_IOERR_UNL
d90d0 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53  OCK);.    if( IS
d90e0 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 29 20 29  _LOCK_ERROR(r) )
d90f0 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  {.      pFile->l
d9100 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e  astErrno = tErrn
d9110 6f 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20  o;.    }.#ifdef 
d9120 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 46 4c  SQLITE_IGNORE_FL
d9130 4f 43 4b 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a  OCK_LOCK_ERRORS.
d9140 20 20 20 20 69 66 28 20 28 72 20 26 20 53 51 4c      if( (r & SQL
d9150 49 54 45 5f 49 4f 45 52 52 29 20 3d 3d 20 53 51  ITE_IOERR) == SQ
d9160 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b 0a 20 20  LITE_IOERR ){.  
d9170 20 20 20 20 72 20 3d 20 53 51 4c 49 54 45 5f 42      r = SQLITE_B
d9180 55 53 59 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  USY;.    }.#endi
d9190 66 20 2f 2a 20 53 51 4c 49 54 45 5f 49 47 4e 4f  f /* SQLITE_IGNO
d91a0 52 45 5f 46 4c 4f 43 4b 5f 4c 4f 43 4b 5f 45 52  RE_FLOCK_LOCK_ER
d91b0 52 4f 52 53 20 2a 2f 0a 20 20 20 20 0a 20 20 20  RORS */.    .   
d91c0 20 72 65 74 75 72 6e 20 72 3b 0a 20 20 7d 20 65   return r;.  } e
d91d0 6c 73 65 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d  lse {.    pFile-
d91e0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c  >locktype = NO_L
d91f0 4f 43 4b 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  OCK;.    return 
d9200 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d  SQLITE_OK;.  }.}
d9210 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20  ../*.** Close a 
d9220 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
d9230 69 6e 74 20 66 6c 6f 63 6b 43 6c 6f 73 65 28 73  int flockClose(s
d9240 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
d9250 20 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20   {.  if( id ){. 
d9260 20 20 20 66 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69     flockUnlock(i
d9270 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 7d  d, NO_LOCK);.  }
d9280 0a 20 20 72 65 74 75 72 6e 20 63 6c 6f 73 65 55  .  return closeU
d9290 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a 7d 0a 0a  nixFile(id);.}..
d92a0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
d92b0 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
d92c0 53 54 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 57  STYLE && !OS_VXW
d92d0 4f 52 4b 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  ORK */../*******
d92e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
d92f0 20 6f 66 20 74 68 65 20 66 6c 6f 63 6b 20 6c 6f   of the flock lo
d9300 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ck implementatio
d9310 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n **************
d9320 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  *******.********
d9330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9370 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  ******/../******
d9380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d93a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d93b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d93c0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  ********.*******
d93d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d93e0 2a 20 42 65 67 69 6e 20 4e 61 6d 65 64 20 53 65  * Begin Named Se
d93f0 6d 61 70 68 6f 72 65 20 4c 6f 63 6b 69 6e 67 20  maphore Locking 
d9400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
d9410 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 4e  ********.**.** N
d9420 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 6c  amed semaphore l
d9430 6f 63 6b 69 6e 67 20 69 73 20 6f 6e 6c 79 20 73  ocking is only s
d9440 75 70 70 6f 72 74 65 64 20 6f 6e 20 56 78 57 6f  upported on VxWo
d9450 72 6b 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 6d 61 70  rks..**.** Semap
d9460 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20  hore locking is 
d9470 6c 69 6b 65 20 64 6f 74 2d 6c 6f 63 6b 20 61 6e  like dot-lock an
d9480 64 20 66 6c 6f 63 6b 20 69 6e 20 74 68 61 74 20  d flock in that 
d9490 69 74 20 72 65 61 6c 6c 79 20 6f 6e 6c 79 0a 2a  it really only.*
d94a0 2a 20 73 75 70 70 6f 72 74 73 20 45 58 43 4c 55  * supports EXCLU
d94b0 53 49 56 45 20 6c 6f 63 6b 69 6e 67 2e 20 20 4f  SIVE locking.  O
d94c0 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 70 72 6f  nly a single pro
d94d0 63 65 73 73 20 63 61 6e 20 72 65 61 64 20 6f 72  cess can read or
d94e0 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 20 64 61   write.** the da
d94f0 74 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 61  tabase file at a
d9500 20 74 69 6d 65 2e 20 20 54 68 69 73 20 72 65 64   time.  This red
d9510 75 63 65 73 20 70 6f 74 65 6e 74 69 61 6c 20 63  uces potential c
d9520 6f 6e 63 75 72 72 65 6e 63 79 2c 20 62 75 74 0a  oncurrency, but.
d9530 2a 2a 20 6d 61 6b 65 73 20 74 68 65 20 6c 6f 63  ** makes the loc
d9540 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  k implementation
d9550 20 6d 75 63 68 20 65 61 73 69 65 72 2e 0a 2a 2f   much easier..*/
d9560 0a 23 69 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a  .#if OS_VXWORKS.
d9570 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
d9580 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68  ine checks if th
d9590 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45  ere is a RESERVE
d95a0 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74  D lock held on t
d95b0 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20  he specified.** 
d95c0 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20  file by this or 
d95d0 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73  any other proces
d95e0 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63  s. If such a loc
d95f0 6b 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a  k is held, set *
d9600 70 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20  pResOut.** to a 
d9610 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f  non-zero value o
d9620 74 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75  therwise *pResOu
d9630 74 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f  t is set to zero
d9640 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  .  The return va
d9650 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f  lue.** is set to
d9660 20 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73   SQLITE_OK unles
d9670 73 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f  s an I/O error o
d9680 63 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63  ccurs during loc
d9690 6b 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73  k checking..*/.s
d96a0 74 61 74 69 63 20 69 6e 74 20 73 65 6d 43 68 65  tatic int semChe
d96b0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73  ckReservedLock(s
d96c0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
d96d0 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20 7b   int *pResOut) {
d96e0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
d96f0 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 65 73  TE_OK;.  int res
d9700 65 72 76 65 64 20 3d 20 30 3b 0a 20 20 75 6e 69  erved = 0;.  uni
d9710 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  xFile *pFile = (
d9720 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20  unixFile*)id;.. 
d9730 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
d9740 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ( return SQLITE_
d9750 49 4f 45 52 52 5f 43 48 45 43 4b 52 45 53 45 52  IOERR_CHECKRESER
d9760 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20  VEDLOCK; );.  . 
d9770 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29   assert( pFile )
d9780 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66  ;..  /* Check if
d9790 20 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69   a thread in thi
d97a0 73 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20  s process holds 
d97b0 73 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20  such a lock */. 
d97c0 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   if( pFile->lock
d97d0 74 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b  type>SHARED_LOCK
d97e0 20 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64   ){.    reserved
d97f0 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f   = 1;.  }.  .  /
d9800 2a 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20  * Otherwise see 
d9810 69 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72  if some other pr
d9820 6f 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 20  ocess holds it. 
d9830 2a 2f 0a 20 20 69 66 28 20 21 72 65 73 65 72 76  */.  if( !reserv
d9840 65 64 20 29 7b 0a 20 20 20 20 73 65 6d 5f 74 20  ed ){.    sem_t 
d9850 2a 70 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70  *pSem = pFile->p
d9860 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a 20 20 20 20  Open->pSem;.    
d9870 73 74 72 75 63 74 20 73 74 61 74 20 73 74 61 74  struct stat stat
d9880 42 75 66 3b 0a 0a 20 20 20 20 69 66 28 20 73 65  Buf;..    if( se
d9890 6d 5f 74 72 79 77 61 69 74 28 70 53 65 6d 29 3d  m_trywait(pSem)=
d98a0 3d 2d 31 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  =-1 ){.      int
d98b0 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b   tErrno = errno;
d98c0 0a 20 20 20 20 20 20 69 66 28 20 45 41 47 41 49  .      if( EAGAI
d98d0 4e 20 21 3d 20 74 45 72 72 6e 6f 20 29 7b 0a 20  N != tErrno ){. 
d98e0 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
d98f0 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78  teErrorFromPosix
d9900 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51  Error(tErrno, SQ
d9910 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b  LITE_IOERR_CHECK
d9920 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 3b 0a 20  RESERVEDLOCK);. 
d9930 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61         pFile->la
d9940 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f  stErrno = tErrno
d9950 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b  ;.      } else {
d9960 0a 20 20 20 20 20 20 20 20 2f 2a 20 73 6f 6d 65  .        /* some
d9970 6f 6e 65 20 65 6c 73 65 20 68 61 73 20 74 68 65  one else has the
d9980 20 6c 6f 63 6b 20 77 68 65 6e 20 77 65 20 61 72   lock when we ar
d9990 65 20 69 6e 20 4e 4f 5f 4c 4f 43 4b 20 2a 2f 0a  e in NO_LOCK */.
d99a0 20 20 20 20 20 20 20 20 72 65 73 65 72 76 65 64          reserved
d99b0 20 3d 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74   = (pFile->lockt
d99c0 79 70 65 20 3c 20 53 48 41 52 45 44 5f 4c 4f 43  ype < SHARED_LOC
d99d0 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  K);.      }.    
d99e0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20  }else{.      /* 
d99f0 77 65 20 63 6f 75 6c 64 20 68 61 76 65 20 69 74  we could have it
d9a00 20 69 66 20 77 65 20 77 61 6e 74 20 69 74 20 2a   if we want it *
d9a10 2f 0a 20 20 20 20 20 20 73 65 6d 5f 70 6f 73 74  /.      sem_post
d9a20 28 70 53 65 6d 29 3b 0a 20 20 20 20 7d 0a 20 20  (pSem);.    }.  
d9a30 7d 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 45  }.  OSTRACE4("TE
d9a40 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64  ST WR-LOCK %d %d
d9a50 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68   %d\n", pFile->h
d9a60 2c 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b  , rc, reserved);
d9a70 0a 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72  ..  *pResOut = r
d9a80 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75 72  eserved;.  retur
d9a90 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  n rc;.}../*.** L
d9aa0 6f 63 6b 20 74 68 65 20 66 69 6c 65 20 77 69 74  ock the file wit
d9ab0 68 20 74 68 65 20 6c 6f 63 6b 20 73 70 65 63 69  h the lock speci
d9ac0 66 69 65 64 20 62 79 20 70 61 72 61 6d 65 74 65  fied by paramete
d9ad0 72 20 6c 6f 63 6b 74 79 70 65 20 2d 20 6f 6e 65  r locktype - one
d9ae0 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  .** of the follo
d9af0 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  wing:.**.**     
d9b00 28 31 29 20 53 48 41 52 45 44 5f 4c 4f 43 4b 0a  (1) SHARED_LOCK.
d9b10 2a 2a 20 20 20 20 20 28 32 29 20 52 45 53 45 52  **     (2) RESER
d9b20 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20  VED_LOCK.**     
d9b30 28 33 29 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  (3) PENDING_LOCK
d9b40 0a 2a 2a 20 20 20 20 20 28 34 29 20 45 58 43 4c  .**     (4) EXCL
d9b50 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a  USIVE_LOCK.**.**
d9b60 20 53 6f 6d 65 74 69 6d 65 73 20 77 68 65 6e 20   Sometimes when 
d9b70 72 65 71 75 65 73 74 69 6e 67 20 6f 6e 65 20 6c  requesting one l
d9b80 6f 63 6b 20 73 74 61 74 65 2c 20 61 64 64 69 74  ock state, addit
d9b90 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73 74 61 74 65  ional lock state
d9ba0 73 0a 2a 2a 20 61 72 65 20 69 6e 73 65 72 74 65  s.** are inserte
d9bb0 64 20 69 6e 20 62 65 74 77 65 65 6e 2e 20 20 54  d in between.  T
d9bc0 68 65 20 6c 6f 63 6b 69 6e 67 20 6d 69 67 68 74  he locking might
d9bd0 20 66 61 69 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20   fail on one of 
d9be0 74 68 65 20 6c 61 74 65 72 0a 2a 2a 20 74 72 61  the later.** tra
d9bf0 6e 73 69 74 69 6f 6e 73 20 6c 65 61 76 69 6e 67  nsitions leaving
d9c00 20 74 68 65 20 6c 6f 63 6b 20 73 74 61 74 65 20   the lock state 
d9c10 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 77  different from w
d9c20 68 61 74 20 69 74 20 73 74 61 72 74 65 64 20 62  hat it started b
d9c30 75 74 0a 2a 2a 20 73 74 69 6c 6c 20 73 68 6f 72  ut.** still shor
d9c40 74 20 6f 66 20 69 74 73 20 67 6f 61 6c 2e 20 20  t of its goal.  
d9c50 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68  The following ch
d9c60 61 72 74 20 73 68 6f 77 73 20 74 68 65 20 61 6c  art shows the al
d9c70 6c 6f 77 65 64 0a 2a 2a 20 74 72 61 6e 73 69 74  lowed.** transit
d9c80 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 69 6e 73  ions and the ins
d9c90 65 72 74 65 64 20 69 6e 74 65 72 6d 65 64 69 61  erted intermedia
d9ca0 74 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a  te states:.**.**
d9cb0 20 20 20 20 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20      UNLOCKED -> 
d9cc0 53 48 41 52 45 44 0a 2a 2a 20 20 20 20 53 48 41  SHARED.**    SHA
d9cd0 52 45 44 20 2d 3e 20 52 45 53 45 52 56 45 44 0a  RED -> RESERVED.
d9ce0 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20  **    SHARED -> 
d9cf0 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43  (PENDING) -> EXC
d9d00 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 52 45 53  LUSIVE.**    RES
d9d10 45 52 56 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e  ERVED -> (PENDIN
d9d20 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a  G) -> EXCLUSIVE.
d9d30 2a 2a 20 20 20 20 50 45 4e 44 49 4e 47 20 2d 3e  **    PENDING ->
d9d40 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a   EXCLUSIVE.**.**
d9d50 20 53 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 73   Semaphore locks
d9d60 20 6f 6e 6c 79 20 72 65 61 6c 6c 79 20 73 75 70   only really sup
d9d70 70 6f 72 74 20 45 58 43 4c 55 53 49 56 45 20 6c  port EXCLUSIVE l
d9d80 6f 63 6b 73 2e 20 20 57 65 20 74 72 61 63 6b 20  ocks.  We track 
d9d90 69 6e 74 65 72 6d 65 64 69 61 74 65 0a 2a 2a 20  intermediate.** 
d9da0 6c 6f 63 6b 20 73 74 61 74 65 73 20 69 6e 20 74  lock states in t
d9db0 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  he sqlite3_file 
d9dc0 73 74 72 75 63 74 75 72 65 2c 20 62 75 74 20 61  structure, but a
d9dd0 6c 6c 20 6c 6f 63 6b 73 20 53 48 41 52 45 44 20  ll locks SHARED 
d9de0 6f 72 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20  or.** above are 
d9df0 72 65 61 6c 6c 79 20 45 58 43 4c 55 53 49 56 45  really EXCLUSIVE
d9e00 20 6c 6f 63 6b 73 20 61 6e 64 20 65 78 63 6c 75   locks and exclu
d9e10 64 65 20 61 6c 6c 20 6f 74 68 65 72 20 70 72 6f  de all other pro
d9e20 63 65 73 73 65 73 20 66 72 6f 6d 0a 2a 2a 20 61  cesses from.** a
d9e30 63 63 65 73 73 20 74 68 65 20 66 69 6c 65 2e 0a  ccess the file..
d9e40 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
d9e50 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63  ne will only inc
d9e60 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 55  rease a lock.  U
d9e70 73 65 20 74 68 65 20 73 71 6c 69 74 65 33 4f 73  se the sqlite3Os
d9e80 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72 6f 75 74  Unlock().** rout
d9e90 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20 61 20 6c  ine to lower a l
d9ea0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e 0a 2a 2f  ocking level..*/
d9eb0 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6d 4c  .static int semL
d9ec0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
d9ed0 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79   *id, int lockty
d9ee0 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65  pe) {.  unixFile
d9ef0 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
d9f00 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20 66  ile*)id;.  int f
d9f10 64 3b 0a 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d  d;.  sem_t *pSem
d9f20 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d   = pFile->pOpen-
d9f30 3e 70 53 65 6d 3b 0a 20 20 69 6e 74 20 72 63 20  >pSem;.  int rc 
d9f40 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
d9f50 2f 2a 20 69 66 20 77 65 20 61 6c 72 65 61 64 79  /* if we already
d9f60 20 68 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74   have a lock, it
d9f70 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20   is exclusive.  
d9f80 0a 20 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73  .  ** Just adjus
d9f90 74 20 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74  t level and punt
d9fa0 20 6f 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20   on outta here. 
d9fb0 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  */.  if (pFile->
d9fc0 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f  locktype > NO_LO
d9fd0 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d  CK) {.    pFile-
d9fe0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b  >locktype = lock
d9ff0 74 79 70 65 3b 0a 20 20 20 20 72 63 20 3d 20 53  type;.    rc = S
da000 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 67 6f  QLITE_OK;.    go
da010 74 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b  to sem_end_lock;
da020 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6c 6f 63  .  }.  .  /* loc
da030 6b 20 73 65 6d 61 70 68 6f 72 65 20 6e 6f 77 20  k semaphore now 
da040 62 75 74 20 62 61 69 6c 20 6f 75 74 20 77 68 65  but bail out whe
da050 6e 20 61 6c 72 65 61 64 79 20 6c 6f 63 6b 65 64  n already locked
da060 2e 20 2a 2f 0a 20 20 69 66 28 20 73 65 6d 5f 74  . */.  if( sem_t
da070 72 79 77 61 69 74 28 70 53 65 6d 29 3d 3d 2d 31  rywait(pSem)==-1
da080 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
da090 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f  ITE_BUSY;.    go
da0a0 74 6f 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b  to sem_end_lock;
da0b0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 67 6f 74 20 69  .  }..  /* got i
da0c0 74 2c 20 73 65 74 20 74 68 65 20 74 79 70 65 20  t, set the type 
da0d0 61 6e 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f  and return ok */
da0e0 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  .  pFile->lockty
da0f0 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 0a  pe = locktype;..
da100 20 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3a 0a 20   sem_end_lock:. 
da110 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
da120 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c  *.** Lower the l
da130 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20  ocking level on 
da140 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
da150 70 46 69 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70  pFile to locktyp
da160 65 2e 20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20  e.  locktype.** 
da170 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e  must be either N
da180 4f 5f 4c 4f 43 4b 20 6f 72 20 53 48 41 52 45 44  O_LOCK or SHARED
da190 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  _LOCK..**.** If 
da1a0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  the locking leve
da1b0 6c 20 6f 66 20 74 68 65 20 66 69 6c 65 20 64 65  l of the file de
da1c0 73 63 72 69 70 74 6f 72 20 69 73 20 61 6c 72 65  scriptor is alre
da1d0 61 64 79 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a  ady at or below.
da1e0 2a 2a 20 74 68 65 20 72 65 71 75 65 73 74 65 64  ** the requested
da1f0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20   locking level, 
da200 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
da210 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74  a no-op..*/.stat
da220 69 63 20 69 6e 74 20 73 65 6d 55 6e 6c 6f 63 6b  ic int semUnlock
da230 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
da240 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29  d, int locktype)
da250 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70   {.  unixFile *p
da260 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
da270 2a 29 69 64 3b 0a 20 20 73 65 6d 5f 74 20 2a 70  *)id;.  sem_t *p
da280 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70  Sem = pFile->pOp
da290 65 6e 2d 3e 70 53 65 6d 3b 0a 0a 20 20 61 73 73  en->pSem;..  ass
da2a0 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20  ert( pFile );.  
da2b0 61 73 73 65 72 74 28 20 70 53 65 6d 20 29 3b 0a  assert( pSem );.
da2c0 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c 4f    OSTRACE5("UNLO
da2d0 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 25 64  CK  %d %d was %d
da2e0 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c   pid=%d\n", pFil
da2f0 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 0a  e->h, locktype,.
da300 09 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  .   pFile->lockt
da310 79 70 65 2c 20 67 65 74 70 69 64 28 29 29 3b 0a  ype, getpid());.
da320 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
da330 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe<=SHARED_LOCK 
da340 29 3b 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2d 6f 70  );.  .  /* no-op
da350 20 69 66 20 70 6f 73 73 69 62 6c 65 20 2a 2f 0a   if possible */.
da360 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63    if( pFile->loc
da370 6b 74 79 70 65 3d 3d 6c 6f 63 6b 74 79 70 65 20  ktype==locktype 
da380 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
da390 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a  LITE_OK;.  }.  .
da3a0 20 20 2f 2a 20 73 68 61 72 65 64 20 63 61 6e 20    /* shared can 
da3b0 6a 75 73 74 20 62 65 20 73 65 74 20 62 65 63 61  just be set beca
da3c0 75 73 65 20 77 65 20 61 6c 77 61 79 73 20 68 61  use we always ha
da3d0 76 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20  ve an exclusive 
da3e0 2a 2f 0a 20 20 69 66 20 28 6c 6f 63 6b 74 79 70  */.  if (locktyp
da3f0 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 29 20  e==SHARED_LOCK) 
da400 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  {.    pFile->loc
da410 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65  ktype = locktype
da420 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
da430 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 0a 20  ITE_OK;.  }.  . 
da440 20 2f 2a 20 6e 6f 2c 20 72 65 61 6c 6c 79 20 75   /* no, really u
da450 6e 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 69 66 20 28  nlock. */.  if (
da460 20 73 65 6d 5f 70 6f 73 74 28 70 53 65 6d 29 3d   sem_post(pSem)=
da470 3d 2d 31 20 29 20 7b 0a 20 20 20 20 69 6e 74 20  =-1 ) {.    int 
da480 72 63 2c 20 74 45 72 72 6e 6f 20 3d 20 65 72 72  rc, tErrno = err
da490 6e 6f 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  no;.    rc = sql
da4a0 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
da4b0 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53  xError(tErrno, S
da4c0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f  QLITE_IOERR_UNLO
da4d0 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 49 53 5f  CK);.    if( IS_
da4e0 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29  LOCK_ERROR(rc) )
da4f0 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  {.      pFile->l
da500 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e  astErrno = tErrn
da510 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  o;.    }.    ret
da520 75 72 6e 20 72 63 3b 20 0a 20 20 7d 0a 20 20 70  urn rc; .  }.  p
da530 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
da540 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 72 65 74 75   NO_LOCK;.  retu
da550 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
da560 0a 2f 2a 0a 20 2a 2a 20 43 6c 6f 73 65 20 61 20  ./*. ** Close a 
da570 66 69 6c 65 2e 0a 20 2a 2f 0a 73 74 61 74 69 63  file.. */.static
da580 20 69 6e 74 20 73 65 6d 43 6c 6f 73 65 28 73 71   int semClose(sq
da590 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20  lite3_file *id) 
da5a0 7b 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20  {.  if( id ){.  
da5b0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
da5c0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
da5d0 64 3b 0a 20 20 20 20 73 65 6d 55 6e 6c 6f 63 6b  d;.    semUnlock
da5e0 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20  (id, NO_LOCK);. 
da5f0 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65     assert( pFile
da600 20 29 3b 0a 20 20 20 20 75 6e 69 78 45 6e 74 65   );.    unixEnte
da610 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65  rMutex();.    re
da620 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46  leaseLockInfo(pF
da630 69 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20  ile->pLock);.   
da640 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28   releaseOpenCnt(
da650 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20  pFile->pOpen);. 
da660 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65     unixLeaveMute
da670 78 28 29 3b 0a 20 20 20 20 63 6c 6f 73 65 55 6e  x();.    closeUn
da680 69 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 7d 0a  ixFile(id);.  }.
da690 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
da6a0 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a  OK;.}..#endif /*
da6b0 20 4f 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 2f   OS_VXWORKS */./
da6c0 2a 0a 2a 2a 20 4e 61 6d 65 64 20 73 65 6d 61 70  *.** Named semap
da6d0 68 6f 72 65 20 6c 6f 63 6b 69 6e 67 20 69 73 20  hore locking is 
da6e0 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 6f  only available o
da6f0 6e 20 56 78 57 6f 72 6b 73 2e 0a 2a 2a 0a 2a 2a  n VxWorks..**.**
da700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
da710 64 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 73  d of the named s
da720 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 20 69 6d  emaphore lock im
da730 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a  plementation ***
da740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
da750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a  ************/...
da7a0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
da7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
da7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
da810 6e 20 41 46 50 20 4c 6f 63 6b 69 6e 67 20 2a 2a  n AFP Locking **
da820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
da830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
da840 2a 2a 0a 2a 2a 20 41 46 50 20 69 73 20 74 68 65  **.** AFP is the
da850 20 41 70 70 6c 65 20 46 69 6c 69 6e 67 20 50 72   Apple Filing Pr
da860 6f 74 6f 63 6f 6c 2e 20 20 41 46 50 20 69 73 20  otocol.  AFP is 
da870 61 20 6e 65 74 77 6f 72 6b 20 66 69 6c 65 73 79  a network filesy
da880 73 74 65 6d 20 66 6f 75 6e 64 0a 2a 2a 20 6f 6e  stem found.** on
da890 20 41 70 70 6c 65 20 4d 61 63 69 6e 74 6f 73 68   Apple Macintosh
da8a0 20 63 6f 6d 70 75 74 65 72 73 20 2d 20 62 6f 74   computers - bot
da8b0 68 20 4f 53 39 20 61 6e 64 20 4f 53 58 2e 0a 2a  h OS9 and OSX..*
da8c0 2a 0a 2a 2a 20 54 68 69 72 64 2d 70 61 72 74 79  *.** Third-party
da8d0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
da8e0 20 6f 66 20 41 46 50 20 61 72 65 20 61 76 61 69   of AFP are avai
da8f0 6c 61 62 6c 65 2e 20 20 42 75 74 20 74 68 69 73  lable.  But this
da900 20 63 6f 64 65 20 68 65 72 65 0a 2a 2a 20 6f 6e   code here.** on
da910 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 4f 53 58 2e  ly works on OSX.
da920 0a 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64  .*/..#if defined
da930 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53  (__APPLE__) && S
da940 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
da950 4b 49 4e 47 5f 53 54 59 4c 45 0a 2f 2a 0a 2a 2a  KING_STYLE./*.**
da960 20 54 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67 43   The afpLockingC
da970 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75 72 65  ontext structure
da980 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 61 66   contains all af
da990 70 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 63 20  p lock specific 
da9a0 73 74 61 74 65 0a 2a 2f 0a 74 79 70 65 64 65 66  state.*/.typedef
da9b0 20 73 74 72 75 63 74 20 61 66 70 4c 6f 63 6b 69   struct afpLocki
da9c0 6e 67 43 6f 6e 74 65 78 74 20 61 66 70 4c 6f 63  ngContext afpLoc
da9d0 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 73 74 72  kingContext;.str
da9e0 75 63 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f  uct afpLockingCo
da9f0 6e 74 65 78 74 20 7b 0a 20 20 75 6e 73 69 67 6e  ntext {.  unsign
daa00 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 73 68 61  ed long long sha
daa10 72 65 64 42 79 74 65 3b 0a 20 20 63 6f 6e 73 74  redByte;.  const
daa20 20 63 68 61 72 20 2a 64 62 50 61 74 68 3b 20 20   char *dbPath;  
daa30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
daa40 6d 65 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 66  me of the open f
daa50 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 73 74 72 75 63  ile */.};..struc
daa60 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50  t ByteRangeLockP
daa70 42 32 0a 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20  B2.{.  unsigned 
daa80 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65 74  long long offset
daa90 3b 20 20 20 20 20 20 20 20 2f 2a 20 6f 66 66 73  ;        /* offs
daaa0 65 74 20 74 6f 20 66 69 72 73 74 20 62 79 74 65  et to first byte
daab0 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e   to lock */.  un
daac0 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
daad0 20 6c 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20   length;        
daae0 2f 2a 20 6e 62 72 20 6f 66 20 62 79 74 65 73 20  /* nbr of bytes 
daaf0 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73  to lock */.  uns
dab00 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
dab10 72 65 74 52 61 6e 67 65 53 74 61 72 74 3b 20 2f  retRangeStart; /
dab20 2a 20 6e 62 72 20 6f 66 20 31 73 74 20 62 79 74  * nbr of 1st byt
dab30 65 20 6c 6f 63 6b 65 64 20 69 66 20 73 75 63 63  e locked if succ
dab40 65 73 73 66 75 6c 20 2a 2f 0a 20 20 75 6e 73 69  essful */.  unsi
dab50 67 6e 65 64 20 63 68 61 72 20 75 6e 4c 6f 63 6b  gned char unLock
dab60 46 6c 61 67 3b 20 20 20 20 20 20 20 20 20 2f 2a  Flag;         /*
dab70 20 31 20 3d 20 75 6e 6c 6f 63 6b 2c 20 30 20 3d   1 = unlock, 0 =
dab80 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 73 69 67   lock */.  unsig
dab90 6e 65 64 20 63 68 61 72 20 73 74 61 72 74 45 6e  ned char startEn
daba0 64 46 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a 20  dFlag;       /* 
dabb0 31 3d 72 65 6c 20 74 6f 20 65 6e 64 20 6f 66 20  1=rel to end of 
dabc0 66 6f 72 6b 2c 20 30 3d 72 65 6c 20 74 6f 20 73  fork, 0=rel to s
dabd0 74 61 72 74 20 2a 2f 0a 20 20 69 6e 74 20 66 64  tart */.  int fd
dabe0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
dabf0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66              /* f
dac00 69 6c 65 20 64 65 73 63 20 74 6f 20 61 73 73 6f  ile desc to asso
dac10 63 20 74 68 69 73 20 6c 6f 63 6b 20 77 69 74 68  c this lock with
dac20 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66 69 6e 65 20   */.};..#define 
dac30 61 66 70 66 73 42 79 74 65 52 61 6e 67 65 4c 6f  afpfsByteRangeLo
dac40 63 6b 32 46 53 43 54 4c 20 20 20 20 20 20 20 20  ck2FSCTL        
dac50 5f 49 4f 57 52 28 27 7a 27 2c 20 32 33 2c 20 73  _IOWR('z', 23, s
dac60 74 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c  truct ByteRangeL
dac70 6f 63 6b 50 42 32 29 0a 0a 2f 2a 0a 2a 2a 20 54  ockPB2)../*.** T
dac80 68 69 73 20 69 73 20 61 20 75 74 69 6c 69 74 79  his is a utility
dac90 20 66 6f 72 20 73 65 74 74 69 6e 67 20 6f 72 20   for setting or 
daca0 63 6c 65 61 72 69 6e 67 20 61 20 62 69 74 2d 72  clearing a bit-r
dacb0 61 6e 67 65 20 6c 6f 63 6b 20 6f 6e 20 61 6e 0a  ange lock on an.
dacc0 2a 2a 20 41 46 50 20 66 69 6c 65 73 79 73 74 65  ** AFP filesyste
dacd0 6d 2e 0a 2a 2a 20 0a 2a 2a 20 52 65 74 75 72 6e  m..** .** Return
dace0 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75   SQLITE_OK on su
dacf0 63 63 65 73 73 2c 20 53 51 4c 49 54 45 5f 42 55  ccess, SQLITE_BU
dad00 53 59 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a  SY on failure..*
dad10 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 70  /.static int afp
dad20 53 65 74 4c 6f 63 6b 28 0a 20 20 63 6f 6e 73 74  SetLock(.  const
dad30 20 63 68 61 72 20 2a 70 61 74 68 2c 20 20 20 20   char *path,    
dad40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
dad50 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74 6f  e of the file to
dad60 20 62 65 20 6c 6f 63 6b 65 64 20 6f 72 20 75 6e   be locked or un
dad70 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75 6e 69 78  locked */.  unix
dad80 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20 20 20 20  File *pFile,    
dad90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70             /* Op
dada0 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70 74  en file descript
dadb0 6f 72 20 6f 6e 20 70 61 74 68 20 2a 2f 0a 20 20  or on path */.  
dadc0 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
dadd0 6e 67 20 6f 66 66 73 65 74 2c 20 20 20 20 20 2f  ng offset,     /
dade0 2a 20 46 69 72 73 74 20 62 79 74 65 20 74 6f 20  * First byte to 
dadf0 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75  be locked */.  u
dae00 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
dae10 67 20 6c 65 6e 67 74 68 2c 20 20 20 20 20 2f 2a  g length,     /*
dae20 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
dae30 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e   to lock */.  in
dae40 74 20 73 65 74 4c 6f 63 6b 46 6c 61 67 20 20 20  t setLockFlag   
dae50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
dae60 54 72 75 65 20 74 6f 20 73 65 74 20 6c 6f 63 6b  True to set lock
dae70 2e 20 20 46 61 6c 73 65 20 74 6f 20 63 6c 65 61  .  False to clea
dae80 72 20 6c 6f 63 6b 20 2a 2f 0a 29 7b 0a 20 20 73  r lock */.){.  s
dae90 74 72 75 63 74 20 42 79 74 65 52 61 6e 67 65 4c  truct ByteRangeL
daea0 6f 63 6b 50 42 32 20 70 62 3b 0a 20 20 69 6e 74  ockPB2 pb;.  int
daeb0 20 65 72 72 3b 0a 20 20 0a 20 20 70 62 2e 75 6e   err;.  .  pb.un
daec0 4c 6f 63 6b 46 6c 61 67 20 3d 20 73 65 74 4c 6f  LockFlag = setLo
daed0 63 6b 46 6c 61 67 20 3f 20 30 20 3a 20 31 3b 0a  ckFlag ? 0 : 1;.
daee0 20 20 70 62 2e 73 74 61 72 74 45 6e 64 46 6c 61    pb.startEndFla
daef0 67 20 3d 20 30 3b 0a 20 20 70 62 2e 6f 66 66 73  g = 0;.  pb.offs
daf00 65 74 20 3d 20 6f 66 66 73 65 74 3b 0a 20 20 70  et = offset;.  p
daf10 62 2e 6c 65 6e 67 74 68 20 3d 20 6c 65 6e 67 74  b.length = lengt
daf20 68 3b 20 0a 20 20 70 62 2e 66 64 20 3d 20 70 46  h; .  pb.fd = pF
daf30 69 6c 65 2d 3e 68 3b 0a 20 20 0a 20 20 4f 53 54  ile->h;.  .  OST
daf40 52 41 43 45 36 28 22 41 46 50 53 45 54 4c 4f 43  RACE6("AFPSETLOC
daf50 4b 20 5b 25 73 5d 20 66 6f 72 20 25 64 25 73 20  K [%s] for %d%s 
daf60 69 6e 20 72 61 6e 67 65 20 25 6c 6c 78 3a 25 6c  in range %llx:%l
daf70 6c 78 5c 6e 22 2c 20 0a 20 20 20 20 28 73 65 74  lx\n", .    (set
daf80 4c 6f 63 6b 46 6c 61 67 3f 22 4f 4e 22 3a 22 4f  LockFlag?"ON":"O
daf90 46 46 22 29 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  FF"), pFile->h, 
dafa0 28 70 62 2e 66 64 3d 3d 2d 31 3f 22 5b 74 65 73  (pb.fd==-1?"[tes
dafb0 74 76 61 6c 2d 31 5d 22 3a 22 22 29 2c 0a 20 20  tval-1]":""),.  
dafc0 20 20 6f 66 66 73 65 74 2c 20 6c 65 6e 67 74 68    offset, length
dafd0 29 3b 0a 20 20 65 72 72 20 3d 20 66 73 63 74 6c  );.  err = fsctl
dafe0 28 70 61 74 68 2c 20 61 66 70 66 73 42 79 74 65  (path, afpfsByte
daff0 52 61 6e 67 65 4c 6f 63 6b 32 46 53 43 54 4c 2c  RangeLock2FSCTL,
db000 20 26 70 62 2c 20 30 29 3b 0a 20 20 69 66 20 28   &pb, 0);.  if (
db010 20 65 72 72 3d 3d 2d 31 20 29 20 7b 0a 20 20 20   err==-1 ) {.   
db020 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 69 6e 74   int rc;.    int
db030 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b   tErrno = errno;
db040 0a 20 20 20 20 4f 53 54 52 41 43 45 34 28 22 41  .    OSTRACE4("A
db050 46 50 53 45 54 4c 4f 43 4b 20 66 61 69 6c 65 64  FPSETLOCK failed
db060 20 74 6f 20 66 73 63 74 6c 28 29 20 27 25 73 27   to fsctl() '%s'
db070 20 25 64 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20   %d %s\n",.     
db080 20 20 20 20 20 20 20 20 70 61 74 68 2c 20 74 45          path, tE
db090 72 72 6e 6f 2c 20 73 74 72 65 72 72 6f 72 28 74  rrno, strerror(t
db0a0 45 72 72 6e 6f 29 29 3b 0a 23 69 66 64 65 66 20  Errno));.#ifdef 
db0b0 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5f 41 46  SQLITE_IGNORE_AF
db0c0 50 5f 4c 4f 43 4b 5f 45 52 52 4f 52 53 0a 20 20  P_LOCK_ERRORS.  
db0d0 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55    rc = SQLITE_BU
db0e0 53 59 3b 0a 23 65 6c 73 65 0a 20 20 20 20 72 63  SY;.#else.    rc
db0f0 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72   = sqliteErrorFr
db100 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72  omPosixError(tEr
db110 72 6e 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 20  rno,.           
db120 20 20 20 20 20 20 20 20 20 73 65 74 4c 6f 63 6b           setLock
db130 46 6c 61 67 20 3f 20 53 51 4c 49 54 45 5f 49 4f  Flag ? SQLITE_IO
db140 45 52 52 5f 4c 4f 43 4b 20 3a 20 53 51 4c 49 54  ERR_LOCK : SQLIT
db150 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b  E_IOERR_UNLOCK);
db160 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
db170 45 5f 49 47 4e 4f 52 45 5f 41 46 50 5f 4c 4f 43  E_IGNORE_AFP_LOC
db180 4b 5f 45 52 52 4f 52 53 20 2a 2f 0a 20 20 20 20  K_ERRORS */.    
db190 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f  if( IS_LOCK_ERRO
db1a0 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 70  R(rc) ){.      p
db1b0 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
db1c0 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d 0a  = tErrno;.    }.
db1d0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
db1e0 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65   } else {.    re
db1f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
db200 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
db210 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73  s routine checks
db220 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52   if there is a R
db230 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c  ESERVED lock hel
db240 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69  d on the specifi
db250 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68  ed.** file by th
db260 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20  is or any other 
db270 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68  process. If such
db280 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c   a lock is held,
db290 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a   set *pResOut.**
db2a0 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76   to a non-zero v
db2b0 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a  alue otherwise *
db2c0 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74  pResOut is set t
db2d0 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74  o zero.  The ret
db2e0 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20  urn value.** is 
db2f0 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b  set to SQLITE_OK
db300 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65   unless an I/O e
db310 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69  rror occurs duri
db320 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67  ng lock checking
db330 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
db340 61 66 70 43 68 65 63 6b 52 65 73 65 72 76 65 64  afpCheckReserved
db350 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  Lock(sqlite3_fil
db360 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73  e *id, int *pRes
db370 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  Out){.  int rc =
db380 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
db390 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a  t reserved = 0;.
db3a0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
db3b0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
db3c0 64 3b 0a 20 20 0a 20 20 53 69 6d 75 6c 61 74 65  d;.  .  Simulate
db3d0 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20  IOError( return 
db3e0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45  SQLITE_IOERR_CHE
db3f0 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20  CKRESERVEDLOCK; 
db400 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20  );.  .  assert( 
db410 70 46 69 6c 65 20 29 3b 0a 20 20 61 66 70 4c 6f  pFile );.  afpLo
db420 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f  ckingContext *co
db430 6e 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b  ntext = (afpLock
db440 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46  ingContext *) pF
db450 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
db460 65 78 74 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65  ext;.  .  /* Che
db470 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69  ck if a thread i
db480 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68  n this process h
db490 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b  olds such a lock
db4a0 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   */.  if( pFile-
db4b0 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44  >locktype>SHARED
db4c0 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73  _LOCK ){.    res
db4d0 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20  erved = 1;.  }. 
db4e0 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65   .  /* Otherwise
db4f0 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68   see if some oth
db500 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73  er process holds
db510 20 69 74 2e 0a 20 20 20 2a 2f 0a 20 20 69 66 28   it..   */.  if(
db520 20 21 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20   !reserved ){.  
db530 20 20 2f 2a 20 6c 6f 63 6b 20 74 68 65 20 52 45    /* lock the RE
db540 53 45 52 56 45 44 20 62 79 74 65 20 2a 2f 0a 20  SERVED byte */. 
db550 20 20 20 69 6e 74 20 6c 72 63 20 3d 20 61 66 70     int lrc = afp
db560 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d  SetLock(context-
db570 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20  >dbPath, pFile, 
db580 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 31  RESERVED_BYTE, 1
db590 2c 31 29 3b 20 20 0a 20 20 20 20 69 66 28 20 53  ,1);  .    if( S
db5a0 51 4c 49 54 45 5f 4f 4b 3d 3d 6c 72 63 20 29 7b  QLITE_OK==lrc ){
db5b0 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 77 65 20  .      /* if we 
db5c0 73 75 63 63 65 65 64 65 64 20 69 6e 20 74 61 6b  succeeded in tak
db5d0 69 6e 67 20 74 68 65 20 72 65 73 65 72 76 65 64  ing the reserved
db5e0 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63 6b 20 69 74   lock, unlock it
db5f0 20 74 6f 20 72 65 73 74 6f 72 65 0a 20 20 20 20   to restore.    
db600 20 20 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e 61    ** the origina
db610 6c 20 73 74 61 74 65 20 2a 2f 0a 20 20 20 20 20  l state */.     
db620 20 6c 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63   lrc = afpSetLoc
db630 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74  k(context->dbPat
db640 68 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56  h, pFile, RESERV
db650 45 44 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b 0a  ED_BYTE, 1, 0);.
db660 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20      } else {.   
db670 20 20 20 2f 2a 20 69 66 20 77 65 20 66 61 69 6c     /* if we fail
db680 65 64 20 74 6f 20 67 65 74 20 74 68 65 20 6c 6f  ed to get the lo
db690 63 6b 20 74 68 65 6e 20 73 6f 6d 65 6f 6e 65 20  ck then someone 
db6a0 65 6c 73 65 20 6d 75 73 74 20 68 61 76 65 20 69  else must have i
db6b0 74 20 2a 2f 0a 20 20 20 20 20 20 72 65 73 65 72  t */.      reser
db6c0 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  ved = 1;.    }. 
db6d0 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
db6e0 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20 20  RROR(lrc) ){.   
db6f0 20 20 20 72 63 3d 6c 72 63 3b 0a 20 20 20 20 7d     rc=lrc;.    }
db700 0a 20 20 7d 0a 20 20 0a 20 20 4f 53 54 52 41 43  .  }.  .  OSTRAC
db710 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b  E4("TEST WR-LOCK
db720 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46   %d %d %d\n", pF
db730 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65  ile->h, rc, rese
db740 72 76 65 64 29 3b 0a 20 20 0a 20 20 2a 70 52 65  rved);.  .  *pRe
db750 73 4f 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b  sOut = reserved;
db760 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
db770 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20  ./*.** Lock the 
db780 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f  file with the lo
db790 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ck specified by 
db7a0 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79  parameter lockty
db7b0 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74  pe - one.** of t
db7c0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a  he following:.**
db7d0 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 52  .**     (1) SHAR
db7e0 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28  ED_LOCK.**     (
db7f0 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  2) RESERVED_LOCK
db800 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44  .**     (3) PEND
db810 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20  ING_LOCK.**     
db820 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f  (4) EXCLUSIVE_LO
db830 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d  CK.**.** Sometim
db840 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 69  es when requesti
db850 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74  ng one lock stat
db860 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f  e, additional lo
db870 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65  ck states.** are
db880 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74   inserted in bet
db890 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69  ween.  The locki
db8a0 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e  ng might fail on
db8b0 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65   one of the late
db8c0 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73  r.** transitions
db8d0 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63   leaving the loc
db8e0 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65 6e  k state differen
db8f0 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73  t from what it s
db900 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74  tarted but.** st
db910 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73  ill short of its
db920 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c   goal.  The foll
db930 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77  owing chart show
db940 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a  s the allowed.**
db950 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64   transitions and
db960 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e   the inserted in
db970 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65  termediate state
db980 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f  s:.**.**    UNLO
db990 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a  CKED -> SHARED.*
db9a0 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52  *    SHARED -> R
db9b0 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48  ESERVED.**    SH
db9c0 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47  ARED -> (PENDING
db9d0 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  ) -> EXCLUSIVE.*
db9e0 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e  *    RESERVED ->
db9f0 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58   (PENDING) -> EX
dba00 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45  CLUSIVE.**    PE
dba10 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49  NDING -> EXCLUSI
dba20 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  VE.**.** This ro
dba30 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20  utine will only 
dba40 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e  increase a lock.
dba50 20 20 55 73 65 20 74 68 65 20 73 71 6c 69 74 65    Use the sqlite
dba60 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a 2a 20 72  3OsUnlock().** r
dba70 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77 65 72 20  outine to lower 
dba80 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 2e  a locking level.
dba90 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .*/.static int a
dbaa0 66 70 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  fpLock(sqlite3_f
dbab0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63  ile *id, int loc
dbac0 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63  ktype){.  int rc
dbad0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
dbae0 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
dbaf0 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
dbb00 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e  .  afpLockingCon
dbb10 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20  text *context = 
dbb20 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  (afpLockingConte
dbb30 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63  xt *) pFile->loc
dbb40 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 0a  kingContext;.  .
dbb50 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
dbb60 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 4c  );.  OSTRACE5("L
dbb70 4f 43 4b 20 20 20 20 25 64 20 25 73 20 77 61 73  OCK    %d %s was
dbb80 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70   %s pid=%d\n", p
dbb90 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20  File->h,.       
dbba0 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c    locktypeName(l
dbbb0 6f 63 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74 79  ocktype), lockty
dbbc0 70 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f  peName(pFile->lo
dbbd0 63 6b 74 79 70 65 29 2c 20 67 65 74 70 69 64 28  cktype), getpid(
dbbe0 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  ));..  /* If the
dbbf0 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  re is already a 
dbc00 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70  lock of this typ
dbc10 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69  e or more restri
dbc20 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a  ctive on the.  *
dbc30 2a 20 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e  * unixFile, do n
dbc40 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73  othing. Don't us
dbc50 65 20 74 68 65 20 61 66 70 5f 65 6e 64 5f 6c 6f  e the afp_end_lo
dbc60 63 6b 3a 20 65 78 69 74 20 70 61 74 68 2c 20 61  ck: exit path, a
dbc70 73 0a 20 20 2a 2a 20 75 6e 69 78 45 6e 74 65 72  s.  ** unixEnter
dbc80 4d 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 62  Mutex() hasn't b
dbc90 65 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a  een called yet..
dbca0 20 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65    */.  if( pFile
dbcb0 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b  ->locktype>=lock
dbcc0 74 79 70 65 20 29 7b 0a 20 20 20 20 4f 53 54 52  type ){.    OSTR
dbcd0 41 43 45 33 28 22 4c 4f 43 4b 20 20 20 20 25 64  ACE3("LOCK    %d
dbce0 20 25 73 20 6f 6b 20 28 61 6c 72 65 61 64 79 20   %s ok (already 
dbcf0 68 65 6c 64 29 5c 6e 22 2c 20 70 46 69 6c 65 2d  held)\n", pFile-
dbd00 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 6c  >h,.           l
dbd10 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63 6b  ocktypeName(lock
dbd20 74 79 70 65 29 29 3b 0a 20 20 20 20 72 65 74 75  type));.    retu
dbd30 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
dbd40 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72  }..  /* Make sur
dbd50 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65  e the locking se
dbd60 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63  quence is correc
dbd70 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  t.  */.  assert(
dbd80 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
dbd90 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63  !=NO_LOCK || loc
dbda0 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f  ktype==SHARED_LO
dbdb0 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  CK );.  assert( 
dbdc0 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e  locktype!=PENDIN
dbdd0 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65  G_LOCK );.  asse
dbde0 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45  rt( locktype!=RE
dbdf0 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70  SERVED_LOCK || p
dbe00 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
dbe10 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SHARED_LOCK );. 
dbe20 20 0a 20 20 2f 2a 20 54 68 69 73 20 6d 75 74 65   .  /* This mute
dbe30 78 20 69 73 20 6e 65 65 64 65 64 20 62 65 63 61  x is needed beca
dbe40 75 73 65 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b  use pFile->pLock
dbe50 20 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73   is shared acros
dbe60 73 20 74 68 72 65 61 64 73 0a 20 20 2a 2f 0a 20  s threads.  */. 
dbe70 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28   unixEnterMutex(
dbe80 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75  );..  /* Make su
dbe90 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  re the current t
dbea0 68 72 65 61 64 20 6f 77 6e 73 20 74 68 65 20 70  hread owns the p
dbeb0 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20  File..  */.  rc 
dbec0 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73  = transferOwners
dbed0 68 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 69 66  hip(pFile);.  if
dbee0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
dbef0 29 7b 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65  ){.    unixLeave
dbf00 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74  Mutex();.    ret
dbf10 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 20 20  urn rc;.  }.    
dbf20 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49 4e 47 20  .  /* A PENDING 
dbf30 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65 64 20 62  lock is needed b
dbf40 65 66 6f 72 65 20 61 63 71 75 69 72 69 6e 67 20  efore acquiring 
dbf50 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20 61 6e  a SHARED lock an
dbf60 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61 63  d before.  ** ac
dbf70 71 75 69 72 69 6e 67 20 61 6e 20 45 58 43 4c 55  quiring an EXCLU
dbf80 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 20  SIVE lock.  For 
dbf90 74 68 65 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c  the SHARED lock,
dbfa0 20 74 68 65 20 50 45 4e 44 49 4e 47 20 77 69 6c   the PENDING wil
dbfb0 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c 65 61 73  l.  ** be releas
dbfc0 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c  ed..  */.  if( l
dbfd0 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
dbfe0 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c 7c 20 28  LOCK .      || (
dbff0 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53  locktype==EXCLUS
dc000 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c  IVE_LOCK && pFil
dc010 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50 45 4e 44  e->locktype<PEND
dc020 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20  ING_LOCK).  ){. 
dc030 20 20 20 69 6e 74 20 66 61 69 6c 65 64 3b 0a 20     int failed;. 
dc040 20 20 20 66 61 69 6c 65 64 20 3d 20 61 66 70 53     failed = afpS
dc050 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e  etLock(context->
dc060 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50  dbPath, pFile, P
dc070 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20  ENDING_BYTE, 1, 
dc080 31 29 3b 0a 20 20 20 20 69 66 20 28 66 61 69 6c  1);.    if (fail
dc090 65 64 29 20 7b 0a 20 20 20 20 20 20 72 63 20 3d  ed) {.      rc =
dc0a0 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 67   failed;.      g
dc0b0 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b  oto afp_end_lock
dc0c0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20  ;.    }.  }.  . 
dc0d0 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 67   /* If control g
dc0e0 65 74 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e  ets to this poin
dc0f0 74 2c 20 74 68 65 6e 20 61 63 74 75 61 6c 6c 79  t, then actually
dc100 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 6d 61   go ahead and ma
dc110 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69 6e  ke.  ** operatin
dc120 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20 66  g system calls f
dc130 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65 64  or the specified
dc140 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66   lock..  */.  if
dc150 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ( locktype==SHAR
dc160 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 69  ED_LOCK ){.    i
dc170 6e 74 20 6c 6b 2c 20 6c 72 63 31 2c 20 6c 72 63  nt lk, lrc1, lrc
dc180 32 3b 0a 20 20 20 20 69 6e 74 20 6c 72 63 31 45  2;.    int lrc1E
dc190 72 72 6e 6f 20 3d 20 30 3b 0a 20 20 20 20 0a 20  rrno = 0;.    . 
dc1a0 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74 68     /* Now get th
dc1b0 65 20 72 65 61 64 2d 6c 6f 63 6b 20 53 48 41 52  e read-lock SHAR
dc1c0 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 20 20 2f  ED_LOCK */.    /
dc1d0 2a 20 6e 6f 74 65 20 74 68 61 74 20 74 68 65 20  * note that the 
dc1e0 71 75 61 6c 69 74 79 20 6f 66 20 74 68 65 20 72  quality of the r
dc1f0 61 6e 64 6f 6d 6e 65 73 73 20 64 6f 65 73 6e 27  andomness doesn'
dc200 74 20 6d 61 74 74 65 72 20 74 68 61 74 20 6d 75  t matter that mu
dc210 63 68 20 2a 2f 0a 20 20 20 20 6c 6b 20 3d 20 72  ch */.    lk = r
dc220 61 6e 64 6f 6d 28 29 3b 20 0a 20 20 20 20 63 6f  andom(); .    co
dc230 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 42 79 74  ntext->sharedByt
dc240 65 20 3d 20 28 6c 6b 20 26 20 30 78 37 66 66 66  e = (lk & 0x7fff
dc250 66 66 66 66 29 25 28 53 48 41 52 45 44 5f 53 49  ffff)%(SHARED_SI
dc260 5a 45 20 2d 20 31 29 3b 0a 20 20 20 20 6c 72 63  ZE - 1);.    lrc
dc270 31 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28 63  1 = afpSetLock(c
dc280 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68 2c 20  ontext->dbPath, 
dc290 70 46 69 6c 65 2c 20 0a 20 20 20 20 20 20 20 20  pFile, .        
dc2a0 20 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b 63    SHARED_FIRST+c
dc2b0 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 42 79  ontext->sharedBy
dc2c0 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20 69  te, 1, 1);.    i
dc2d0 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52  f( IS_LOCK_ERROR
dc2e0 28 6c 72 63 31 29 20 29 7b 0a 20 20 20 20 20 20  (lrc1) ){.      
dc2f0 6c 72 63 31 45 72 72 6e 6f 20 3d 20 70 46 69 6c  lrc1Errno = pFil
dc300 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a 20 20  e->lastErrno;.  
dc310 20 20 7d 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20    }.    /* Drop 
dc320 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 50 45  the temporary PE
dc330 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20  NDING lock */.  
dc340 20 20 6c 72 63 32 20 3d 20 61 66 70 53 65 74 4c    lrc2 = afpSetL
dc350 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50  ock(context->dbP
dc360 61 74 68 2c 20 70 46 69 6c 65 2c 20 50 45 4e 44  ath, pFile, PEND
dc370 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30 29 3b  ING_BYTE, 1, 0);
dc380 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 49 53  .    .    if( IS
dc390 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63 31  _LOCK_ERROR(lrc1
dc3a0 29 20 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c  ) ) {.      pFil
dc3b0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 6c  e->lastErrno = l
dc3c0 72 63 31 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20  rc1Errno;.      
dc3d0 72 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20 20  rc = lrc1;.     
dc3e0 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f   goto afp_end_lo
dc3f0 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69  ck;.    } else i
dc400 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52  f( IS_LOCK_ERROR
dc410 28 6c 72 63 32 29 20 29 7b 0a 20 20 20 20 20 20  (lrc2) ){.      
dc420 72 63 20 3d 20 6c 72 63 32 3b 0a 20 20 20 20 20  rc = lrc2;.     
dc430 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f   goto afp_end_lo
dc440 63 6b 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 69  ck;.    } else i
dc450 66 28 20 6c 72 63 31 20 21 3d 20 53 51 4c 49 54  f( lrc1 != SQLIT
dc460 45 5f 4f 4b 20 29 20 7b 0a 20 20 20 20 20 20 72  E_OK ) {.      r
dc470 63 20 3d 20 6c 72 63 31 3b 0a 20 20 20 20 7d 20  c = lrc1;.    } 
dc480 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 70 46 69  else {.      pFi
dc490 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 53  le->locktype = S
dc4a0 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20  HARED_LOCK;.    
dc4b0 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e    pFile->pOpen->
dc4c0 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 7d 0a 20  nLock++;.    }. 
dc4d0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54   }else{.    /* T
dc4e0 68 65 20 72 65 71 75 65 73 74 20 77 61 73 20 66  he request was f
dc4f0 6f 72 20 61 20 52 45 53 45 52 56 45 44 20 6f 72  or a RESERVED or
dc500 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e   EXCLUSIVE lock.
dc510 20 20 49 74 20 69 73 0a 20 20 20 20 2a 2a 20 61    It is.    ** a
dc520 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 72  ssumed that ther
dc530 65 20 69 73 20 61 20 53 48 41 52 45 44 20 6f 72  e is a SHARED or
dc540 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f 6e   greater lock on
dc550 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a   the file.    **
dc560 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f   already..    */
dc570 0a 20 20 20 20 69 6e 74 20 66 61 69 6c 65 64 20  .    int failed 
dc580 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28  = 0;.    assert(
dc590 20 30 21 3d 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74   0!=pFile->lockt
dc5a0 79 70 65 20 29 3b 0a 20 20 20 20 69 66 20 28 6c  ype );.    if (l
dc5b0 6f 63 6b 74 79 70 65 20 3e 3d 20 52 45 53 45 52  ocktype >= RESER
dc5c0 56 45 44 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c  VED_LOCK && pFil
dc5d0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 52 45  e->locktype < RE
dc5e0 53 45 52 56 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20  SERVED_LOCK) {. 
dc5f0 20 20 20 20 20 20 20 2f 2a 20 41 63 71 75 69 72         /* Acquir
dc600 65 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  e a RESERVED loc
dc610 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 61 69  k */.        fai
dc620 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b  led = afpSetLock
dc630 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68  (context->dbPath
dc640 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45  , pFile, RESERVE
dc650 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 0a 20 20  D_BYTE, 1,1);.  
dc660 20 20 7d 0a 20 20 20 20 69 66 20 28 21 66 61 69    }.    if (!fai
dc670 6c 65 64 20 26 26 20 6c 6f 63 6b 74 79 70 65 20  led && locktype 
dc680 3d 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  == EXCLUSIVE_LOC
dc690 4b 29 20 7b 0a 20 20 20 20 20 20 2f 2a 20 41 63  K) {.      /* Ac
dc6a0 71 75 69 72 65 20 61 6e 20 45 58 43 4c 55 53 49  quire an EXCLUSI
dc6b0 56 45 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20  VE lock */.     
dc6c0 20 20 20 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d     .      /* Rem
dc6d0 6f 76 65 20 74 68 65 20 73 68 61 72 65 64 20 6c  ove the shared l
dc6e0 6f 63 6b 20 62 65 66 6f 72 65 20 74 72 79 69 6e  ock before tryin
dc6f0 67 20 74 68 65 20 72 61 6e 67 65 2e 20 20 77 65  g the range.  we
dc700 27 6c 6c 20 6e 65 65 64 20 74 6f 20 0a 20 20 20  'll need to .   
dc710 20 20 20 2a 2a 20 72 65 65 73 74 61 62 6c 69 73     ** reestablis
dc720 68 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63  h the shared loc
dc730 6b 20 69 66 20 77 65 20 63 61 6e 27 74 20 67 65  k if we can't ge
dc740 74 20 74 68 65 20 20 61 66 70 55 6e 6c 6f 63 6b  t the  afpUnlock
dc750 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
dc760 69 66 28 20 21 28 66 61 69 6c 65 64 20 3d 20 61  if( !(failed = a
dc770 66 70 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78  fpSetLock(contex
dc780 74 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65  t->dbPath, pFile
dc790 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b  , SHARED_FIRST +
dc7a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
dc7b0 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 78            contex
dc7c0 74 2d 3e 73 68 61 72 65 64 42 79 74 65 2c 20 31  t->sharedByte, 1
dc7d0 2c 20 30 29 29 20 29 7b 0a 20 20 20 20 20 20 20  , 0)) ){.       
dc7e0 20 69 6e 74 20 66 61 69 6c 65 64 32 20 3d 20 53   int failed2 = S
dc7f0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
dc800 20 20 2f 2a 20 6e 6f 77 20 61 74 74 65 6d 6d 70    /* now attemmp
dc810 74 20 74 6f 20 67 65 74 20 74 68 65 20 65 78 63  t to get the exc
dc820 6c 75 73 69 76 65 20 6c 6f 63 6b 20 72 61 6e 67  lusive lock rang
dc830 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 61 69  e */.        fai
dc840 6c 65 64 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b  led = afpSetLock
dc850 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50 61 74 68  (context->dbPath
dc860 2c 20 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f  , pFile, SHARED_
dc870 46 49 52 53 54 2c 20 0a 20 20 20 20 20 20 20 20  FIRST, .        
dc880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dc890 20 20 20 20 20 20 20 53 48 41 52 45 44 5f 53 49         SHARED_SI
dc8a0 5a 45 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20  ZE, 1);.        
dc8b0 69 66 28 20 66 61 69 6c 65 64 20 26 26 20 28 66  if( failed && (f
dc8c0 61 69 6c 65 64 32 20 3d 20 61 66 70 53 65 74 4c  ailed2 = afpSetL
dc8d0 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 64 62 50  ock(context->dbP
dc8e0 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20  ath, pFile, .   
dc8f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dc900 20 20 20 20 53 48 41 52 45 44 5f 46 49 52 53 54      SHARED_FIRST
dc910 20 2b 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72   + context->shar
dc920 65 64 42 79 74 65 2c 20 31 2c 20 31 29 29 20 29  edByte, 1, 1)) )
dc930 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43  {.          /* C
dc940 61 6e 27 74 20 72 65 65 73 74 61 62 6c 69 73 68  an't reestablish
dc950 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63 6b   the shared lock
dc960 2e 20 20 53 71 6c 69 74 65 20 63 61 6e 27 74 20  .  Sqlite can't 
dc970 64 65 61 6c 2c 20 74 68 69 73 20 69 73 0a 20 20  deal, this is.  
dc980 20 20 20 20 20 20 20 20 2a 2a 20 61 20 63 72 69          ** a cri
dc990 74 69 63 61 6c 20 49 2f 4f 20 65 72 72 6f 72 0a  tical I/O error.
dc9a0 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
dc9b0 20 20 20 20 20 20 20 72 63 20 3d 20 28 28 66 61         rc = ((fa
dc9c0 69 6c 65 64 20 26 20 53 51 4c 49 54 45 5f 49 4f  iled & SQLITE_IO
dc9d0 45 52 52 29 20 3d 3d 20 53 51 4c 49 54 45 5f 49  ERR) == SQLITE_I
dc9e0 4f 45 52 52 29 20 3f 20 66 61 69 6c 65 64 32 20  OERR) ? failed2 
dc9f0 3a 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  : .             
dca00 20 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c    SQLITE_IOERR_L
dca10 4f 43 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 67  OCK;.          g
dca20 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f 63 6b  oto afp_end_lock
dca30 3b 0a 20 20 20 20 20 20 20 20 7d 20 0a 20 20 20  ;.        } .   
dca40 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
dca50 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 20 0a    rc = failed; .
dca60 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
dca70 20 20 69 66 28 20 66 61 69 6c 65 64 20 29 7b 0a    if( failed ){.
dca80 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65        rc = faile
dca90 64 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a  d;.    }.  }.  .
dcaa0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
dcab0 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65  _OK ){.    pFile
dcac0 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63  ->locktype = loc
dcad0 6b 74 79 70 65 3b 0a 20 20 7d 65 6c 73 65 20 69  ktype;.  }else i
dcae0 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43  f( locktype==EXC
dcaf0 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20  LUSIVE_LOCK ){. 
dcb00 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
dcb10 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43  pe = PENDING_LOC
dcb20 4b 3b 0a 20 20 7d 0a 20 20 0a 61 66 70 5f 65 6e  K;.  }.  .afp_en
dcb30 64 5f 6c 6f 63 6b 3a 0a 20 20 75 6e 69 78 4c 65  d_lock:.  unixLe
dcb40 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f 53  aveMutex();.  OS
dcb50 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20 20 20  TRACE4("LOCK    
dcb60 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70 46 69  %d %s %s\n", pFi
dcb70 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 4e  le->h, locktypeN
dcb80 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c 20 0a  ame(locktype), .
dcb90 20 20 20 20 20 20 20 20 20 72 63 3d 3d 53 51 4c           rc==SQL
dcba0 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20  ITE_OK ? "ok" : 
dcbb0 22 66 61 69 6c 65 64 22 29 3b 0a 20 20 72 65 74  "failed");.  ret
dcbc0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
dcbd0 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69   Lower the locki
dcbe0 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65  ng level on file
dcbf0 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c   descriptor pFil
dcc00 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20  e to locktype.  
dcc10 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74  locktype.** must
dcc20 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f   be either NO_LO
dcc30 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43  CK or SHARED_LOC
dcc40 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  K..**.** If the 
dcc50 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66  locking level of
dcc60 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
dcc70 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20  ptor is already 
dcc80 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74  at or below.** t
dcc90 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63  he requested loc
dcca0 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73  king level, this
dccb0 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
dccc0 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  -op..*/.static i
dccd0 6e 74 20 61 66 70 55 6e 6c 6f 63 6b 28 73 71 6c  nt afpUnlock(sql
dcce0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
dccf0 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20  nt locktype) {. 
dcd00 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
dcd10 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20  _OK;.  unixFile 
dcd20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
dcd30 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 4c 6f 63  le*)id;.  afpLoc
dcd40 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74  kingContext *pCt
dcd50 78 20 3d 20 28 61 66 70 4c 6f 63 6b 69 6e 67 43  x = (afpLockingC
dcd60 6f 6e 74 65 78 74 20 2a 29 20 70 46 69 6c 65 2d  ontext *) pFile-
dcd70 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b  >lockingContext;
dcd80 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c  ..  assert( pFil
dcd90 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28  e );.  OSTRACE5(
dcda0 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77  "UNLOCK  %d %d w
dcdb0 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e 22 2c  as %d pid=%d\n",
dcdc0 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74   pFile->h, lockt
dcdd0 79 70 65 2c 0a 20 20 20 20 20 20 20 20 20 70 46  ype,.         pF
dcde0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67  ile->locktype, g
dcdf0 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 61 73 73  etpid());..  ass
dce00 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53  ert( locktype<=S
dce10 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
dce20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
dce30 79 70 65 3c 3d 6c 6f 63 6b 74 79 70 65 20 29 7b  ype<=locktype ){
dce40 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
dce50 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28  TE_OK;.  }.  if(
dce60 20 43 48 45 43 4b 5f 54 48 52 45 41 44 49 44 28   CHECK_THREADID(
dce70 70 46 69 6c 65 29 20 29 7b 0a 20 20 20 20 72 65  pFile) ){.    re
dce80 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  turn SQLITE_MISU
dce90 53 45 3b 0a 20 20 7d 0a 20 20 75 6e 69 78 45 6e  SE;.  }.  unixEn
dcea0 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66  terMutex();.  if
dceb0 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70  ( pFile->locktyp
dcec0 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b  e>SHARED_LOCK ){
dced0 0a 20 20 20 20 0a 20 20 20 20 69 66 28 20 70 46  .    .    if( pF
dcee0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 45  ile->locktype==E
dcef0 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b  XCLUSIVE_LOCK ){
dcf00 0a 20 20 20 20 20 20 72 63 20 3d 20 61 66 70 53  .      rc = afpS
dcf10 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e 64 62 50  etLock(pCtx->dbP
dcf20 61 74 68 2c 20 70 46 69 6c 65 2c 20 53 48 41 52  ath, pFile, SHAR
dcf30 45 44 5f 46 49 52 53 54 2c 20 53 48 41 52 45 44  ED_FIRST, SHARED
dcf40 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20 20 20 20  _SIZE, 0);.     
dcf50 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
dcf60 4f 4b 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d 3d  OK && locktype==
dcf70 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SHARED_LOCK ){. 
dcf80 20 20 20 20 20 20 20 2f 2a 20 6f 6e 6c 79 20 72         /* only r
dcf90 65 2d 65 73 74 61 62 6c 69 73 68 20 74 68 65 20  e-establish the 
dcfa0 73 68 61 72 65 64 20 6c 6f 63 6b 20 69 66 20 6e  shared lock if n
dcfb0 65 63 65 73 73 61 72 79 20 2a 2f 0a 20 20 20 20  ecessary */.    
dcfc0 20 20 20 20 69 6e 74 20 73 68 61 72 65 64 4c 6f      int sharedLo
dcfd0 63 6b 42 79 74 65 20 3d 20 53 48 41 52 45 44 5f  ckByte = SHARED_
dcfe0 46 49 52 53 54 2b 70 43 74 78 2d 3e 73 68 61 72  FIRST+pCtx->shar
dcff0 65 64 42 79 74 65 3b 0a 20 20 20 20 20 20 20 20  edByte;.        
dd000 72 63 20 3d 20 61 66 70 53 65 74 4c 6f 63 6b 28  rc = afpSetLock(
dd010 70 43 74 78 2d 3e 64 62 50 61 74 68 2c 20 70 46  pCtx->dbPath, pF
dd020 69 6c 65 2c 20 73 68 61 72 65 64 4c 6f 63 6b 42  ile, sharedLockB
dd030 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20  yte, 1, 1);.    
dd040 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
dd050 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
dd060 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  && pFile->lockty
dd070 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  pe>=PENDING_LOCK
dd080 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 61   ){.      rc = a
dd090 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e  fpSetLock(pCtx->
dd0a0 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 50  dbPath, pFile, P
dd0b0 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20  ENDING_BYTE, 1, 
dd0c0 30 29 3b 0a 20 20 20 20 7d 20 0a 20 20 20 20 69  0);.    } .    i
dd0d0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
dd0e0 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74   && pFile->lockt
dd0f0 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f  ype>=RESERVED_LO
dd100 43 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  CK ){.      rc =
dd110 20 61 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78   afpSetLock(pCtx
dd120 2d 3e 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c  ->dbPath, pFile,
dd130 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20   RESERVED_BYTE, 
dd140 31 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  1, 0);.    }.  }
dd150 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74 79 70  else if( locktyp
dd160 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20  e==NO_LOCK ){.  
dd170 20 20 2f 2a 20 63 6c 65 61 72 20 74 68 65 20 73    /* clear the s
dd180 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20  hared lock */.  
dd190 20 20 69 6e 74 20 73 68 61 72 65 64 4c 6f 63 6b    int sharedLock
dd1a0 42 79 74 65 20 3d 20 53 48 41 52 45 44 5f 46 49  Byte = SHARED_FI
dd1b0 52 53 54 2b 70 43 74 78 2d 3e 73 68 61 72 65 64  RST+pCtx->shared
dd1c0 42 79 74 65 3b 0a 20 20 20 20 72 63 20 3d 20 61  Byte;.    rc = a
dd1d0 66 70 53 65 74 4c 6f 63 6b 28 70 43 74 78 2d 3e  fpSetLock(pCtx->
dd1e0 64 62 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 73  dbPath, pFile, s
dd1f0 68 61 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31  haredLockByte, 1
dd200 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  , 0);.  }..  if(
dd210 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
dd220 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74 79  {.    if( lockty
dd230 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20  pe==NO_LOCK ){. 
dd240 20 20 20 20 20 73 74 72 75 63 74 20 75 6e 69 78       struct unix
dd250 4f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 20 3d  OpenCnt *pOpen =
dd260 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20   pFile->pOpen;. 
dd270 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63       pOpen->nLoc
dd280 6b 2d 2d 3b 0a 20 20 20 20 20 20 61 73 73 65 72  k--;.      asser
dd290 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e  t( pOpen->nLock>
dd2a0 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  =0 );.      if( 
dd2b0 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20  pOpen->nLock==0 
dd2c0 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
dd2d0 63 6c 6f 73 65 50 65 6e 64 69 6e 67 46 64 73 28  closePendingFds(
dd2e0 70 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 7d 0a  pFile);.      }.
dd2f0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 75 6e 69 78      }.  }.  unix
dd300 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
dd310 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
dd320 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  K ){.    pFile->
dd330 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74  locktype = lockt
dd340 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ype;.  }.  retur
dd350 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  n rc;.}../*.** C
dd360 6c 6f 73 65 20 61 20 66 69 6c 65 20 26 20 63 6c  lose a file & cl
dd370 65 61 6e 75 70 20 41 46 50 20 73 70 65 63 69 66  eanup AFP specif
dd380 69 63 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65  ic locking conte
dd390 78 74 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  xt .*/.static in
dd3a0 74 20 61 66 70 43 6c 6f 73 65 28 73 71 6c 69 74  t afpClose(sqlit
dd3b0 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20  e3_file *id) {. 
dd3c0 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20 20 75   if( id ){.    u
dd3d0 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
dd3e0 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
dd3f0 20 20 20 20 61 66 70 55 6e 6c 6f 63 6b 28 69 64      afpUnlock(id
dd400 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  , NO_LOCK);.    
dd410 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29  unixEnterMutex()
dd420 3b 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d  ;.    if( pFile-
dd430 3e 70 4f 70 65 6e 20 26 26 20 70 46 69 6c 65 2d  >pOpen && pFile-
dd440 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 20 29 7b  >pOpen->nLock ){
dd450 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
dd460 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69  re are outstandi
dd470 6e 67 20 6c 6f 63 6b 73 2c 20 64 6f 20 6e 6f 74  ng locks, do not
dd480 20 61 63 74 75 61 6c 6c 79 20 63 6c 6f 73 65 20   actually close 
dd490 74 68 65 20 66 69 6c 65 20 6a 75 73 74 0a 20 20  the file just.  
dd4a0 20 20 20 20 2a 2a 20 79 65 74 20 62 65 63 61 75      ** yet becau
dd4b0 73 65 20 74 68 61 74 20 77 6f 75 6c 64 20 63 6c  se that would cl
dd4c0 65 61 72 20 74 68 6f 73 65 20 6c 6f 63 6b 73 2e  ear those locks.
dd4d0 20 20 49 6e 73 74 65 61 64 2c 20 61 64 64 20 74    Instead, add t
dd4e0 68 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a  he file.      **
dd4f0 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 70   descriptor to p
dd500 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 2e 20  Open->aPending. 
dd510 20 49 74 20 77 69 6c 6c 20 62 65 20 61 75 74 6f   It will be auto
dd520 6d 61 74 69 63 61 6c 6c 79 20 63 6c 6f 73 65 64  matically closed
dd530 20 77 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 74   when.      ** t
dd540 68 65 20 6c 61 73 74 20 6c 6f 63 6b 20 69 73 20  he last lock is 
dd550 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 20 20 2a  cleared..      *
dd560 2f 0a 20 20 20 20 20 20 73 65 74 50 65 6e 64 69  /.      setPendi
dd570 6e 67 46 64 28 70 46 69 6c 65 29 3b 0a 20 20 20  ngFd(pFile);.   
dd580 20 7d 0a 20 20 20 20 72 65 6c 65 61 73 65 4f 70   }.    releaseOp
dd590 65 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70  enCnt(pFile->pOp
dd5a0 65 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  en);.    sqlite3
dd5b0 5f 66 72 65 65 28 70 46 69 6c 65 2d 3e 6c 6f 63  _free(pFile->loc
dd5c0 6b 69 6e 67 43 6f 6e 74 65 78 74 29 3b 0a 20 20  kingContext);.  
dd5d0 20 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28    closeUnixFile(
dd5e0 69 64 29 3b 0a 20 20 20 20 75 6e 69 78 4c 65 61  id);.    unixLea
dd5f0 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20  veMutex();.  }. 
dd600 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
dd610 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  K;.}..#endif /* 
dd620 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f  defined(__APPLE_
dd630 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41  _) && SQLITE_ENA
dd640 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
dd650 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63  E */./*.** The c
dd660 6f 64 65 20 61 62 6f 76 65 20 69 73 20 74 68 65  ode above is the
dd670 20 41 46 50 20 6c 6f 63 6b 20 69 6d 70 6c 65 6d   AFP lock implem
dd680 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65 20 63  entation.  The c
dd690 6f 64 65 20 69 73 20 73 70 65 63 69 66 69 63 0a  ode is specific.
dd6a0 2a 2a 20 74 6f 20 4d 61 63 4f 53 58 20 61 6e 64  ** to MacOSX and
dd6b0 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 6f   does not work o
dd6c0 6e 20 6f 74 68 65 72 20 75 6e 69 78 20 70 6c 61  n other unix pla
dd6d0 74 66 6f 72 6d 73 2e 20 20 4e 6f 20 61 6c 74 65  tforms.  No alte
dd6e0 72 6e 61 74 69 76 65 0a 2a 2a 20 69 73 20 61 76  rnative.** is av
dd6f0 61 69 6c 61 62 6c 65 2e 20 20 49 66 20 79 6f 75  ailable.  If you
dd700 20 64 6f 6e 27 74 20 63 6f 6d 70 69 6c 65 20 66   don't compile f
dd710 6f 72 20 61 20 6d 61 63 2c 20 74 68 65 6e 20 74  or a mac, then t
dd720 68 65 20 22 75 6e 69 78 2d 61 66 70 22 0a 2a 2a  he "unix-afp".**
dd730 20 56 46 53 20 69 73 20 6e 6f 74 20 61 76 61 69   VFS is not avai
dd740 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a  lable..**.******
dd750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
dd760 45 6e 64 20 6f 66 20 74 68 65 20 41 46 50 20 6c  End of the AFP l
dd770 6f 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ock implementati
dd780 6f 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  on *************
dd790 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a  *********.******
dd7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd7e0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a 2a 2a  ********/.../***
dd7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd810 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd820 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd830 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a  ***********.****
dd840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4e 6f 6e  ************ Non
dd850 2d 6c 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65 33  -locking sqlite3
dd860 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 2a 2a  _file methods **
dd870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
dd880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
dd890 2a 20 54 68 65 20 6e 65 78 74 20 64 69 76 69 73  * The next divis
dd8a0 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 69 6d 70  ion contains imp
dd8b0 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f 72  lementations for
dd8c0 20 61 6c 6c 20 6d 65 74 68 6f 64 73 20 6f 66 20   all methods of 
dd8d0 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  the .** sqlite3_
dd8e0 66 69 6c 65 20 6f 62 6a 65 63 74 20 6f 74 68 65  file object othe
dd8f0 72 20 74 68 61 6e 20 74 68 65 20 6c 6f 63 6b 69  r than the locki
dd900 6e 67 20 6d 65 74 68 6f 64 73 2e 20 20 54 68 65  ng methods.  The
dd910 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 6d 65 74 68   locking.** meth
dd920 6f 64 73 20 77 65 72 65 20 64 65 66 69 6e 65 64  ods were defined
dd930 20 69 6e 20 64 69 76 69 73 69 6f 6e 73 20 61 62   in divisions ab
dd940 6f 76 65 20 28 6f 6e 65 20 6c 6f 63 6b 69 6e 67  ove (one locking
dd950 20 6d 65 74 68 6f 64 20 70 65 72 0a 2a 2a 20 64   method per.** d
dd960 69 76 69 73 69 6f 6e 29 2e 20 20 54 68 6f 73 65  ivision).  Those
dd970 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20 61 72   methods that ar
dd980 65 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20  e common to all 
dd990 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 73 0a 2a 2a  locking modes.**
dd9a0 20 61 72 65 20 67 61 74 68 65 72 20 74 6f 67 65   are gather toge
dd9b0 74 68 65 72 20 69 6e 74 6f 20 74 68 69 73 20 64  ther into this d
dd9c0 69 76 69 73 69 6f 6e 2e 0a 2a 2f 0a 0a 2f 2a 0a  ivision..*/../*.
dd9d0 2a 2a 20 53 65 65 6b 20 74 6f 20 74 68 65 20 6f  ** Seek to the o
dd9e0 66 66 73 65 74 20 70 61 73 73 65 64 20 61 73 20  ffset passed as 
dd9f0 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
dda00 65 6e 74 2c 20 74 68 65 6e 20 72 65 61 64 20 63  ent, then read c
dda10 6e 74 20 0a 2a 2a 20 62 79 74 65 73 20 69 6e 74  nt .** bytes int
dda20 6f 20 70 42 75 66 2e 20 52 65 74 75 72 6e 20 74  o pBuf. Return t
dda30 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
dda40 65 73 20 61 63 74 75 61 6c 6c 79 20 72 65 61 64  es actually read
dda50 2e 0a 2a 2a 0a 2a 2a 20 4e 42 3a 20 20 49 66 20  ..**.** NB:  If 
dda60 79 6f 75 20 64 65 66 69 6e 65 20 55 53 45 5f 50  you define USE_P
dda70 52 45 41 44 20 6f 72 20 55 53 45 5f 50 52 45 41  READ or USE_PREA
dda80 44 36 34 2c 20 74 68 65 6e 20 69 74 20 6d 69 67  D64, then it mig
dda90 68 74 20 61 6c 73 6f 0a 2a 2a 20 62 65 20 6e 65  ht also.** be ne
ddaa0 63 65 73 73 61 72 79 20 74 6f 20 64 65 66 69 6e  cessary to defin
ddab0 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20  e _XOPEN_SOURCE 
ddac0 74 6f 20 62 65 20 35 30 30 2e 20 20 54 68 69 73  to be 500.  This
ddad0 20 76 61 72 69 65 73 20 66 72 6f 6d 0a 2a 2a 20   varies from.** 
ddae0 6f 6e 65 20 73 79 73 74 65 6d 20 74 6f 20 61 6e  one system to an
ddaf0 6f 74 68 65 72 2e 20 20 53 69 6e 63 65 20 53 51  other.  Since SQ
ddb00 4c 69 74 65 20 64 6f 65 73 20 6e 6f 74 20 64 65  Lite does not de
ddb10 66 69 6e 65 20 55 53 45 5f 50 52 45 41 44 0a 2a  fine USE_PREAD.*
ddb20 2a 20 61 6e 79 20 61 6e 79 20 66 6f 72 6d 20 62  * any any form b
ddb30 79 20 64 65 66 61 75 6c 74 2c 20 77 65 20 77 69  y default, we wi
ddb40 6c 6c 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74  ll not attempt t
ddb50 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f  o define _XOPEN_
ddb60 53 4f 55 52 43 45 2e 0a 2a 2a 20 53 65 65 20 74  SOURCE..** See t
ddb70 69 63 6b 65 74 73 20 23 32 37 34 31 20 61 6e 64  ickets #2741 and
ddb80 20 23 32 36 38 31 2e 0a 2a 2a 0a 2a 2a 20 54 6f   #2681..**.** To
ddb90 20 61 76 6f 69 64 20 73 74 6f 6d 70 69 6e 67 20   avoid stomping 
ddba0 74 68 65 20 65 72 72 6e 6f 20 76 61 6c 75 65 20  the errno value 
ddbb0 6f 6e 20 61 20 66 61 69 6c 65 64 20 72 65 61 64  on a failed read
ddbc0 20 74 68 65 20 6c 61 73 74 45 72 72 6e 6f 20 76   the lastErrno v
ddbd0 61 6c 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 62  alue.** is set b
ddbe0 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e  efore returning.
ddbf0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
ddc00 65 65 6b 41 6e 64 52 65 61 64 28 75 6e 69 78 46  eekAndRead(unixF
ddc10 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33  ile *id, sqlite3
ddc20 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 2c 20 76  _int64 offset, v
ddc30 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20 63  oid *pBuf, int c
ddc40 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a  nt){.  int got;.
ddc50 20 20 69 36 34 20 6e 65 77 4f 66 66 73 65 74 3b    i64 newOffset;
ddc60 0a 20 20 54 49 4d 45 52 5f 53 54 41 52 54 3b 0a  .  TIMER_START;.
ddc70 23 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f  #if defined(USE_
ddc80 50 52 45 41 44 29 0a 20 20 67 6f 74 20 3d 20 70  PREAD).  got = p
ddc90 72 65 61 64 28 69 64 2d 3e 68 2c 20 70 42 75 66  read(id->h, pBuf
ddca0 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a  , cnt, offset);.
ddcb0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
ddcc0 72 28 20 67 6f 74 20 3d 20 2d 31 20 29 3b 0a 23  r( got = -1 );.#
ddcd0 65 6c 69 66 20 64 65 66 69 6e 65 64 28 55 53 45  elif defined(USE
ddce0 5f 50 52 45 41 44 36 34 29 0a 20 20 67 6f 74 20  _PREAD64).  got 
ddcf0 3d 20 70 72 65 61 64 36 34 28 69 64 2d 3e 68 2c  = pread64(id->h,
ddd00 20 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73   pBuf, cnt, offs
ddd10 65 74 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49  et);.  SimulateI
ddd20 4f 45 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31  OError( got = -1
ddd30 20 29 3b 0a 23 65 6c 73 65 0a 20 20 6e 65 77 4f   );.#else.  newO
ddd40 66 66 73 65 74 20 3d 20 6c 73 65 65 6b 28 69 64  ffset = lseek(id
ddd50 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 53 45 45  ->h, offset, SEE
ddd60 4b 5f 53 45 54 29 3b 0a 20 20 53 69 6d 75 6c 61  K_SET);.  Simula
ddd70 74 65 49 4f 45 72 72 6f 72 28 20 6e 65 77 4f 66  teIOError( newOf
ddd80 66 73 65 74 2d 2d 20 29 3b 0a 20 20 69 66 28 20  fset-- );.  if( 
ddd90 6e 65 77 4f 66 66 73 65 74 21 3d 6f 66 66 73 65  newOffset!=offse
ddda0 74 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 65 77  t ){.    if( new
dddb0 4f 66 66 73 65 74 20 3d 3d 20 2d 31 20 29 7b 0a  Offset == -1 ){.
dddc0 20 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65        ((unixFile
dddd0 2a 29 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f  *)id)->lastErrno
ddde0 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 7d 65   = errno;.    }e
dddf0 6c 73 65 7b 0a 20 20 20 20 20 20 28 28 75 6e 69  lse{.      ((uni
dde00 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74  xFile*)id)->last
dde10 45 72 72 6e 6f 20 3d 20 30 3b 09 09 09 0a 20 20  Errno = 0;....  
dde20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 2d    }.    return -
dde30 31 3b 0a 20 20 7d 0a 20 20 67 6f 74 20 3d 20 72  1;.  }.  got = r
dde40 65 61 64 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c  ead(id->h, pBuf,
dde50 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20   cnt);.#endif.  
dde60 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20 69 66 28  TIMER_END;.  if(
dde70 20 67 6f 74 3c 30 20 29 7b 0a 20 20 20 20 28 28   got<0 ){.    ((
dde80 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  unixFile*)id)->l
dde90 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  astErrno = errno
ddea0 3b 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 35  ;.  }.  OSTRACE5
ddeb0 28 22 52 45 41 44 20 20 20 20 25 2d 33 64 20 25  ("READ    %-3d %
ddec0 35 64 20 25 37 6c 6c 64 20 25 6c 6c 75 5c 6e 22  5d %7lld %llu\n"
dded0 2c 20 69 64 2d 3e 68 2c 20 67 6f 74 2c 20 6f 66  , id->h, got, of
ddee0 66 73 65 74 2c 20 54 49 4d 45 52 5f 45 4c 41 50  fset, TIMER_ELAP
ddef0 53 45 44 29 3b 0a 20 20 72 65 74 75 72 6e 20 67  SED);.  return g
ddf00 6f 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61  ot;.}../*.** Rea
ddf10 64 20 64 61 74 61 20 66 72 6f 6d 20 61 20 66 69  d data from a fi
ddf20 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 66 65 72  le into a buffer
ddf30 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  .  Return SQLITE
ddf40 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20 62 79  _OK if all.** by
ddf50 74 65 73 20 77 65 72 65 20 72 65 61 64 20 73 75  tes were read su
ddf60 63 63 65 73 73 66 75 6c 6c 79 20 61 6e 64 20 53  ccessfully and S
ddf70 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 66 20 61  QLITE_IOERR if a
ddf80 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20  nything goes.** 
ddf90 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  wrong..*/.static
ddfa0 20 69 6e 74 20 75 6e 69 78 52 65 61 64 28 0a 20   int unixRead(. 
ddfb0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69   sqlite3_file *i
ddfc0 64 2c 20 0a 20 20 76 6f 69 64 20 2a 70 42 75 66  d, .  void *pBuf
ddfd0 2c 20 0a 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20  , .  int amt,.  
ddfe0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66  sqlite3_int64 of
ddff0 66 73 65 74 0a 29 7b 0a 20 20 75 6e 69 78 46 69  fset.){.  unixFi
de000 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
de010 78 46 69 6c 65 20 2a 29 69 64 3b 0a 20 20 69 6e  xFile *)id;.  in
de020 74 20 67 6f 74 3b 0a 20 20 61 73 73 65 72 74 28  t got;.  assert(
de030 20 69 64 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20   id );..  /* If 
de040 74 68 69 73 20 69 73 20 61 20 64 61 74 61 62 61  this is a databa
de050 73 65 20 66 69 6c 65 20 28 6e 6f 74 20 61 20 6a  se file (not a j
de060 6f 75 72 6e 61 6c 2c 20 6d 61 73 74 65 72 2d 6a  ournal, master-j
de070 6f 75 72 6e 61 6c 20 6f 72 20 74 65 6d 70 0a 20  ournal or temp. 
de080 20 2a 2a 20 66 69 6c 65 29 2c 20 74 68 65 20 62   ** file), the b
de090 79 74 65 73 20 69 6e 20 74 68 65 20 6c 6f 63 6b  ytes in the lock
de0a0 69 6e 67 20 72 61 6e 67 65 20 73 68 6f 75 6c 64  ing range should
de0b0 20 6e 65 76 65 72 20 62 65 20 72 65 61 64 20 6f   never be read o
de0c0 72 20 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20  r written. */.  
de0d0 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 70  assert( pFile->p
de0e0 55 6e 75 73 65 64 3d 3d 30 0a 20 20 20 20 20 20  Unused==0.      
de0f0 20 7c 7c 20 6f 66 66 73 65 74 3e 3d 50 45 4e 44   || offset>=PEND
de100 49 4e 47 5f 42 59 54 45 2b 35 31 32 0a 20 20 20  ING_BYTE+512.   
de110 20 20 20 20 7c 7c 20 6f 66 66 73 65 74 2b 61 6d      || offset+am
de120 74 3c 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 20  t<=PENDING_BYTE 
de130 0a 20 20 29 3b 0a 0a 20 20 67 6f 74 20 3d 20 73  .  );..  got = s
de140 65 65 6b 41 6e 64 52 65 61 64 28 70 46 69 6c 65  eekAndRead(pFile
de150 2c 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20  , offset, pBuf, 
de160 61 6d 74 29 3b 0a 20 20 69 66 28 20 67 6f 74 3d  amt);.  if( got=
de170 3d 61 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 75  =amt ){.    retu
de180 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
de190 7d 65 6c 73 65 20 69 66 28 20 67 6f 74 3c 30 20  }else if( got<0 
de1a0 29 7b 0a 20 20 20 20 2f 2a 20 6c 61 73 74 45 72  ){.    /* lastEr
de1b0 72 6e 6f 20 73 65 74 20 62 79 20 73 65 65 6b 41  rno set by seekA
de1c0 6e 64 52 65 61 64 20 2a 2f 0a 20 20 20 20 72 65  ndRead */.    re
de1d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
de1e0 52 5f 52 45 41 44 3b 0a 20 20 7d 65 6c 73 65 7b  R_READ;.  }else{
de1f0 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74  .    pFile->last
de200 45 72 72 6e 6f 20 3d 20 30 3b 20 2f 2a 20 6e 6f  Errno = 0; /* no
de210 74 20 61 20 73 79 73 74 65 6d 20 65 72 72 6f 72  t a system error
de220 20 2a 2f 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61   */.    /* Unrea
de230 64 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 62  d parts of the b
de240 75 66 66 65 72 20 6d 75 73 74 20 62 65 20 7a 65  uffer must be ze
de250 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20  ro-filled */.   
de260 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a   memset(&((char*
de270 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20  )pBuf)[got], 0, 
de280 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65  amt-got);.    re
de290 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
de2a0 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20  R_SHORT_READ;.  
de2b0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 65 6b 20  }.}../*.** Seek 
de2c0 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20 69 6e  to the offset in
de2d0 20 69 64 2d 3e 6f 66 66 73 65 74 20 74 68 65 6e   id->offset then
de2e0 20 72 65 61 64 20 63 6e 74 20 62 79 74 65 73 20   read cnt bytes 
de2f0 69 6e 74 6f 20 70 42 75 66 2e 0a 2a 2a 20 52 65  into pBuf..** Re
de300 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
de310 6f 66 20 62 79 74 65 73 20 61 63 74 75 61 6c 6c  of bytes actuall
de320 79 20 72 65 61 64 2e 20 20 55 70 64 61 74 65 20  y read.  Update 
de330 74 68 65 20 6f 66 66 73 65 74 2e 0a 2a 2a 0a 2a  the offset..**.*
de340 2a 20 54 6f 20 61 76 6f 69 64 20 73 74 6f 6d 70  * To avoid stomp
de350 69 6e 67 20 74 68 65 20 65 72 72 6e 6f 20 76 61  ing the errno va
de360 6c 75 65 20 6f 6e 20 61 20 66 61 69 6c 65 64 20  lue on a failed 
de370 77 72 69 74 65 20 74 68 65 20 6c 61 73 74 45 72  write the lastEr
de380 72 6e 6f 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20  rno value.** is 
de390 73 65 74 20 62 65 66 6f 72 65 20 72 65 74 75 72  set before retur
de3a0 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ning..*/.static 
de3b0 69 6e 74 20 73 65 65 6b 41 6e 64 57 72 69 74 65  int seekAndWrite
de3c0 28 75 6e 69 78 46 69 6c 65 20 2a 69 64 2c 20 69  (unixFile *id, i
de3d0 36 34 20 6f 66 66 73 65 74 2c 20 63 6f 6e 73 74  64 offset, const
de3e0 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74   void *pBuf, int
de3f0 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 67 6f 74   cnt){.  int got
de400 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 66 73 65  ;.  i64 newOffse
de410 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41 52 54  t;.  TIMER_START
de420 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 55 53  ;.#if defined(US
de430 45 5f 50 52 45 41 44 29 0a 20 20 67 6f 74 20 3d  E_PREAD).  got =
de440 20 70 77 72 69 74 65 28 69 64 2d 3e 68 2c 20 70   pwrite(id->h, p
de450 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65 74  Buf, cnt, offset
de460 29 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64  );.#elif defined
de470 28 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20  (USE_PREAD64).  
de480 67 6f 74 20 3d 20 70 77 72 69 74 65 36 34 28 69  got = pwrite64(i
de490 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c  d->h, pBuf, cnt,
de4a0 20 6f 66 66 73 65 74 29 3b 0a 23 65 6c 73 65 0a   offset);.#else.
de4b0 20 20 6e 65 77 4f 66 66 73 65 74 20 3d 20 6c 73    newOffset = ls
de4c0 65 65 6b 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65  eek(id->h, offse
de4d0 74 2c 20 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20  t, SEEK_SET);.  
de4e0 69 66 28 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f  if( newOffset!=o
de4f0 66 66 73 65 74 20 29 7b 0a 20 20 20 20 69 66 28  ffset ){.    if(
de500 20 6e 65 77 4f 66 66 73 65 74 20 3d 3d 20 2d 31   newOffset == -1
de510 20 29 7b 0a 20 20 20 20 20 20 28 28 75 6e 69 78   ){.      ((unix
de520 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61 73 74 45  File*)id)->lastE
de530 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
de540 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28    }else{.      (
de550 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e  (unixFile*)id)->
de560 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 09 09  lastErrno = 0;..
de570 09 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ..    }.    retu
de580 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f 74  rn -1;.  }.  got
de590 20 3d 20 77 72 69 74 65 28 69 64 2d 3e 68 2c 20   = write(id->h, 
de5a0 70 42 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64  pBuf, cnt);.#end
de5b0 69 66 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a  if.  TIMER_END;.
de5c0 20 20 69 66 28 20 67 6f 74 3c 30 20 29 7b 0a 20    if( got<0 ){. 
de5d0 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69     ((unixFile*)i
de5e0 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  d)->lastErrno = 
de5f0 65 72 72 6e 6f 3b 0a 20 20 7d 0a 0a 20 20 4f 53  errno;.  }..  OS
de600 54 52 41 43 45 35 28 22 57 52 49 54 45 20 20 20  TRACE5("WRITE   
de610 25 2d 33 64 20 25 35 64 20 25 37 6c 6c 64 20 25  %-3d %5d %7lld %
de620 6c 6c 75 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67  llu\n", id->h, g
de630 6f 74 2c 20 6f 66 66 73 65 74 2c 20 54 49 4d 45  ot, offset, TIME
de640 52 5f 45 4c 41 50 53 45 44 29 3b 0a 20 20 72 65  R_ELAPSED);.  re
de650 74 75 72 6e 20 67 6f 74 3b 0a 7d 0a 0a 0a 2f 2a  turn got;.}.../*
de660 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 66  .** Write data f
de670 72 6f 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74  rom a buffer int
de680 6f 20 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72  o a file.  Retur
de690 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
de6a0 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d  uccess.** or som
de6b0 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f  e other error co
de6c0 64 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a  de on failure..*
de6d0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
de6e0 78 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65  xWrite(.  sqlite
de6f0 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20 63  3_file *id, .  c
de700 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c  onst void *pBuf,
de710 20 0a 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73   .  int amt,.  s
de720 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66  qlite3_int64 off
de730 73 65 74 20 0a 29 7b 0a 20 20 75 6e 69 78 46 69  set .){.  unixFi
de740 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
de750 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74  xFile*)id;.  int
de760 20 77 72 6f 74 65 20 3d 20 30 3b 0a 20 20 61 73   wrote = 0;.  as
de770 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 61 73  sert( id );.  as
de780 73 65 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a 0a  sert( amt>0 );..
de790 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
de7a0 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  a database file 
de7b0 28 6e 6f 74 20 61 20 6a 6f 75 72 6e 61 6c 2c 20  (not a journal, 
de7c0 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 6f  master-journal o
de7d0 72 20 74 65 6d 70 0a 20 20 2a 2a 20 66 69 6c 65  r temp.  ** file
de7e0 29 2c 20 74 68 65 20 62 79 74 65 73 20 69 6e 20  ), the bytes in 
de7f0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67  the locking rang
de800 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62  e should never b
de810 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65  e read or writte
de820 6e 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  n. */.  assert( 
de830 70 46 69 6c 65 2d 3e 70 55 6e 75 73 65 64 3d 3d  pFile->pUnused==
de840 30 0a 20 20 20 20 20 20 20 7c 7c 20 6f 66 66 73  0.       || offs
de850 65 74 3e 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45  et>=PENDING_BYTE
de860 2b 35 31 32 0a 20 20 20 20 20 20 20 7c 7c 20 6f  +512.       || o
de870 66 66 73 65 74 2b 61 6d 74 3c 3d 50 45 4e 44 49  ffset+amt<=PENDI
de880 4e 47 5f 42 59 54 45 20 0a 20 20 29 3b 0a 0a 23  NG_BYTE .  );..#
de890 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
de8a0 2f 2a 20 49 66 20 77 65 20 61 72 65 20 64 6f 69  /* If we are doi
de8b0 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 77 72 69 74  ng a normal writ
de8c0 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20  e to a database 
de8d0 66 69 6c 65 20 28 61 73 20 6f 70 70 6f 73 65 64  file (as opposed
de8e0 20 74 6f 0a 20 20 2a 2a 20 64 6f 69 6e 67 20 61   to.  ** doing a
de8f0 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c   hot-journal rol
de900 6c 62 61 63 6b 20 6f 72 20 61 20 77 72 69 74 65  lback or a write
de910 20 74 6f 20 73 6f 6d 65 20 66 69 6c 65 20 6f 74   to some file ot
de920 68 65 72 20 74 68 61 6e 20 61 0a 20 20 2a 2a 20  her than a.  ** 
de930 6e 6f 72 6d 61 6c 20 64 61 74 61 62 61 73 65 20  normal database 
de940 66 69 6c 65 29 20 74 68 65 6e 20 72 65 63 6f 72  file) then recor
de950 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20  d the fact that 
de960 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a  the database.  *
de970 2a 20 68 61 73 20 63 68 61 6e 67 65 64 2e 20 20  * has changed.  
de980 49 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  If the transacti
de990 6f 6e 20 63 6f 75 6e 74 65 72 20 69 73 20 6d 6f  on counter is mo
de9a0 64 69 66 69 65 64 2c 20 72 65 63 6f 72 64 20 74  dified, record t
de9b0 68 61 74 0a 20 20 2a 2a 20 66 61 63 74 20 74 6f  hat.  ** fact to
de9c0 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 46  o..  */.  if( pF
de9d0 69 6c 65 2d 3e 69 6e 4e 6f 72 6d 61 6c 57 72 69  ile->inNormalWri
de9e0 74 65 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d  te ){.    pFile-
de9f0 3e 64 62 55 70 64 61 74 65 20 3d 20 31 3b 20 20  >dbUpdate = 1;  
dea00 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
dea10 68 61 73 20 62 65 65 6e 20 6d 6f 64 69 66 69 65  has been modifie
dea20 64 20 2a 2f 0a 20 20 20 20 69 66 28 20 6f 66 66  d */.    if( off
dea30 73 65 74 3c 3d 32 34 20 26 26 20 6f 66 66 73 65  set<=24 && offse
dea40 74 2b 61 6d 74 3e 3d 32 37 20 29 7b 0a 20 20 20  t+amt>=27 ){.   
dea50 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 20     int rc;.     
dea60 20 63 68 61 72 20 6f 6c 64 43 6e 74 72 5b 34 5d   char oldCntr[4]
dea70 3b 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65  ;.      Simulate
dea80 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 31 29  IOErrorBenign(1)
dea90 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 65 65  ;.      rc = see
deaa0 6b 41 6e 64 52 65 61 64 28 70 46 69 6c 65 2c 20  kAndRead(pFile, 
deab0 32 34 2c 20 6f 6c 64 43 6e 74 72 2c 20 34 29 3b  24, oldCntr, 4);
deac0 0a 20 20 20 20 20 20 53 69 6d 75 6c 61 74 65 49  .      SimulateI
dead0 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 30 29 3b  OErrorBenign(0);
deae0 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 34  .      if( rc!=4
deaf0 20 7c 7c 20 6d 65 6d 63 6d 70 28 6f 6c 64 43 6e   || memcmp(oldCn
deb00 74 72 2c 20 26 28 28 63 68 61 72 2a 29 70 42 75  tr, &((char*)pBu
deb10 66 29 5b 32 34 2d 6f 66 66 73 65 74 5d 2c 20 34  f)[24-offset], 4
deb20 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
deb30 70 46 69 6c 65 2d 3e 74 72 61 6e 73 43 6e 74 72  pFile->transCntr
deb40 43 68 6e 67 20 3d 20 31 3b 20 20 2f 2a 20 54 68  Chng = 1;  /* Th
deb50 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f  e transaction co
deb60 75 6e 74 65 72 20 68 61 73 20 63 68 61 6e 67 65  unter has change
deb70 64 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  d */.      }.   
deb80 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20   }.  }.#endif.. 
deb90 20 77 68 69 6c 65 28 20 61 6d 74 3e 30 20 26 26   while( amt>0 &&
deba0 20 28 77 72 6f 74 65 20 3d 20 73 65 65 6b 41 6e   (wrote = seekAn
debb0 64 57 72 69 74 65 28 70 46 69 6c 65 2c 20 6f 66  dWrite(pFile, of
debc0 66 73 65 74 2c 20 70 42 75 66 2c 20 61 6d 74 29  fset, pBuf, amt)
debd0 29 3e 30 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d  )>0 ){.    amt -
debe0 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 6f 66 66  = wrote;.    off
debf0 73 65 74 20 2b 3d 20 77 72 6f 74 65 3b 0a 20 20  set += wrote;.  
dec00 20 20 70 42 75 66 20 3d 20 26 28 28 63 68 61 72    pBuf = &((char
dec10 2a 29 70 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a  *)pBuf)[wrote];.
dec20 20 20 7d 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f    }.  SimulateIO
dec30 45 72 72 6f 72 28 28 20 77 72 6f 74 65 3d 28 2d  Error(( wrote=(-
dec40 31 29 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20  1), amt=1 ));.  
dec50 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c  SimulateDiskfull
dec60 45 72 72 6f 72 28 28 20 77 72 6f 74 65 3d 30 2c  Error(( wrote=0,
dec70 20 61 6d 74 3d 31 20 29 29 3b 0a 20 20 69 66 28   amt=1 ));.  if(
dec80 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 69 66   amt>0 ){.    if
dec90 28 20 77 72 6f 74 65 3c 30 20 29 7b 0a 20 20 20  ( wrote<0 ){.   
deca0 20 20 20 2f 2a 20 6c 61 73 74 45 72 72 6e 6f 20     /* lastErrno 
decb0 73 65 74 20 62 79 20 73 65 65 6b 41 6e 64 57 72  set by seekAndWr
decc0 69 74 65 20 2a 2f 0a 20 20 20 20 20 20 72 65 74  ite */.      ret
decd0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
dece0 5f 57 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73  _WRITE;.    }els
decf0 65 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  e{.      pFile->
ded00 6c 61 73 74 45 72 72 6e 6f 20 3d 20 30 3b 20 2f  lastErrno = 0; /
ded10 2a 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 65  * not a system e
ded20 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 72 65  rror */.      re
ded30 74 75 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c  turn SQLITE_FULL
ded40 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
ded50 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
ded60 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
ded70 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e  _TEST./*.** Coun
ded80 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
ded90 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f  fullsyncs and no
deda0 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69  rmal syncs.  Thi
dedb0 73 20 69 73 20 75 73 65 64 20 74 6f 20 74 65 73  s is used to tes
dedc0 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20  t.** that syncs 
dedd0 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72  and fullsyncs ar
dede0 65 20 6f 63 63 75 72 72 69 6e 67 20 61 74 20 74  e occurring at t
dedf0 68 65 20 72 69 67 68 74 20 74 69 6d 65 73 2e 0a  he right times..
dee00 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
dee10 74 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63  t sqlite3_sync_c
dee20 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45  ount = 0;.SQLITE
dee30 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
dee40 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20  _fullsync_count 
dee50 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  = 0;.#endif../*.
dee60 2a 2a 20 57 65 20 64 6f 20 6e 6f 74 20 74 72 75  ** We do not tru
dee70 73 74 20 73 79 73 74 65 6d 73 20 74 6f 20 70 72  st systems to pr
dee80 6f 76 69 64 65 20 61 20 77 6f 72 6b 69 6e 67 20  ovide a working 
dee90 66 64 61 74 61 73 79 6e 63 28 29 2e 20 20 53 6f  fdatasync().  So
deea0 6d 65 20 64 6f 2e 0a 2a 2a 20 4f 74 68 65 72 73  me do..** Others
deeb0 20 64 6f 20 6e 6f 2e 20 20 54 6f 20 62 65 20 73   do no.  To be s
deec0 61 66 65 2c 20 77 65 20 77 69 6c 6c 20 73 74 69  afe, we will sti
deed0 63 6b 20 77 69 74 68 20 74 68 65 20 28 73 6c 6f  ck with the (slo
deee0 77 65 72 29 20 66 73 79 6e 63 28 29 2e 0a 2a 2a  wer) fsync()..**
deef0 20 49 66 20 79 6f 75 20 6b 6e 6f 77 20 74 68 61   If you know tha
def00 74 20 79 6f 75 72 20 73 79 73 74 65 6d 20 64 6f  t your system do
def10 65 73 20 73 75 70 70 6f 72 74 20 66 64 61 74 61  es support fdata
def20 73 79 6e 63 28 29 20 63 6f 72 72 65 63 74 6c 79  sync() correctly
def30 2c 0a 2a 2a 20 74 68 65 6e 20 73 69 6d 70 6c 79  ,.** then simply
def40 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 2d 44   compile with -D
def50 66 64 61 74 61 73 79 6e 63 3d 66 64 61 74 61 73  fdatasync=fdatas
def60 79 6e 63 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69  ync.*/.#if !defi
def70 6e 65 64 28 66 64 61 74 61 73 79 6e 63 29 20 26  ned(fdatasync) &
def80 26 20 21 64 65 66 69 6e 65 64 28 5f 5f 6c 69 6e  & !defined(__lin
def90 75 78 5f 5f 29 0a 23 20 64 65 66 69 6e 65 20 66  ux__).# define f
defa0 64 61 74 61 73 79 6e 63 20 66 73 79 6e 63 0a 23  datasync fsync.#
defb0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66  endif../*.** Def
defc0 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53 59  ine HAVE_FULLFSY
defd0 4e 43 20 74 6f 20 30 20 6f 72 20 31 20 64 65 70  NC to 0 or 1 dep
defe0 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65  ending on whethe
deff0 72 20 6f 72 20 6e 6f 74 0a 2a 2a 20 74 68 65 20  r or not.** the 
df000 46 5f 46 55 4c 4c 46 53 59 4e 43 20 6d 61 63 72  F_FULLFSYNC macr
df010 6f 20 69 73 20 64 65 66 69 6e 65 64 2e 20 20 46  o is defined.  F
df020 5f 46 55 4c 4c 46 53 59 4e 43 20 69 73 20 63 75  _FULLFSYNC is cu
df030 72 72 65 6e 74 6c 79 0a 2a 2a 20 6f 6e 6c 79 20  rrently.** only 
df040 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 4d 61 63  available on Mac
df050 20 4f 53 20 58 2e 20 20 42 75 74 20 74 68 61 74   OS X.  But that
df060 20 63 6f 75 6c 64 20 63 68 61 6e 67 65 2e 0a 2a   could change..*
df070 2f 0a 23 69 66 64 65 66 20 46 5f 46 55 4c 4c 46  /.#ifdef F_FULLF
df080 53 59 4e 43 0a 23 20 64 65 66 69 6e 65 20 48 41  SYNC.# define HA
df090 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 31 0a 23  VE_FULLFSYNC 1.#
df0a0 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 48 41  else.# define HA
df0b0 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 30 0a 23  VE_FULLFSYNC 0.#
df0c0 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  endif.../*.** Th
df0d0 65 20 66 73 79 6e 63 28 29 20 73 79 73 74 65 6d  e fsync() system
df0e0 20 63 61 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 77   call does not w
df0f0 6f 72 6b 20 61 73 20 61 64 76 65 72 74 69 73 65  ork as advertise
df100 64 20 6f 6e 20 6d 61 6e 79 0a 2a 2a 20 75 6e 69  d on many.** uni
df110 78 20 73 79 73 74 65 6d 73 2e 20 20 54 68 65 20  x systems.  The 
df120 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 63 65 64  following proced
df130 75 72 65 20 69 73 20 61 6e 20 61 74 74 65 6d 70  ure is an attemp
df140 74 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 69 74 20  t to make.** it 
df150 77 6f 72 6b 20 62 65 74 74 65 72 2e 0a 2a 2a 0a  work better..**.
df160 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 4e 4f  ** The SQLITE_NO
df170 5f 53 59 4e 43 20 6d 61 63 72 6f 20 64 69 73 61  _SYNC macro disa
df180 62 6c 65 73 20 61 6c 6c 20 66 73 79 6e 63 28 29  bles all fsync()
df190 73 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 66  s.  This is usef
df1a0 75 6c 0a 2a 2a 20 66 6f 72 20 74 65 73 74 69 6e  ul.** for testin
df1b0 67 20 77 68 65 6e 20 77 65 20 77 61 6e 74 20 74  g when we want t
df1c0 6f 20 72 75 6e 20 74 68 72 6f 75 67 68 20 74 68  o run through th
df1d0 65 20 74 65 73 74 20 73 75 69 74 65 20 71 75 69  e test suite qui
df1e0 63 6b 6c 79 2e 0a 2a 2a 20 59 6f 75 20 61 72 65  ckly..** You are
df1f0 20 73 74 72 6f 6e 67 6c 79 20 61 64 76 69 73 65   strongly advise
df200 64 20 2a 6e 6f 74 2a 20 74 6f 20 64 65 70 6c 6f  d *not* to deplo
df210 79 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f  y with SQLITE_NO
df220 5f 53 59 4e 43 0a 2a 2a 20 65 6e 61 62 6c 65 64  _SYNC.** enabled
df230 2c 20 68 6f 77 65 76 65 72 2c 20 73 69 6e 63 65  , however, since
df240 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4e 4f 5f   with SQLITE_NO_
df250 53 59 4e 43 20 65 6e 61 62 6c 65 64 2c 20 61 6e  SYNC enabled, an
df260 20 4f 53 20 63 72 61 73 68 0a 2a 2a 20 6f 72 20   OS crash.** or 
df270 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 69  power failure wi
df280 6c 6c 20 6c 69 6b 65 6c 79 20 63 6f 72 72 75 70  ll likely corrup
df290 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  t the database f
df2a0 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  ile..**.** SQLit
df2b0 65 20 73 65 74 73 20 74 68 65 20 64 61 74 61 4f  e sets the dataO
df2c0 6e 6c 79 20 66 6c 61 67 20 69 66 20 74 68 65 20  nly flag if the 
df2d0 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65  size of the file
df2e0 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a   is unchanged..*
df2f0 2a 20 54 68 65 20 69 64 65 61 20 62 65 68 69 6e  * The idea behin
df300 64 20 64 61 74 61 4f 6e 6c 79 20 69 73 20 74 68  d dataOnly is th
df310 61 74 20 69 74 20 73 68 6f 75 6c 64 20 6f 6e 6c  at it should onl
df320 79 20 77 72 69 74 65 20 74 68 65 20 66 69 6c 65  y write the file
df330 20 63 6f 6e 74 65 6e 74 0a 2a 2a 20 74 6f 20 64   content.** to d
df340 69 73 6b 2c 20 6e 6f 74 20 74 68 65 20 69 6e 6f  isk, not the ino
df350 64 65 2e 20 20 57 65 20 6f 6e 6c 79 20 73 65 74  de.  We only set
df360 20 64 61 74 61 4f 6e 6c 79 20 69 66 20 74 68 65   dataOnly if the
df370 20 66 69 6c 65 20 73 69 7a 65 20 69 73 20 0a 2a   file size is .*
df380 2a 20 75 6e 63 68 61 6e 67 65 64 20 73 69 6e 63  * unchanged sinc
df390 65 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20  e the file size 
df3a0 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 69  is part of the i
df3b0 6e 6f 64 65 2e 20 20 48 6f 77 65 76 65 72 2c 20  node.  However, 
df3c0 0a 2a 2a 20 54 65 64 20 54 73 27 6f 20 74 65 6c  .** Ted Ts'o tel
df3d0 6c 73 20 75 73 20 74 68 61 74 20 66 64 61 74 61  ls us that fdata
df3e0 73 79 6e 63 28 29 20 77 69 6c 6c 20 61 6c 73 6f  sync() will also
df3f0 20 77 72 69 74 65 20 74 68 65 20 69 6e 6f 64 65   write the inode
df400 20 69 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20   if the.** file 
df410 73 69 7a 65 20 68 61 73 20 63 68 61 6e 67 65 64  size has changed
df420 2e 20 20 54 68 65 20 6f 6e 6c 79 20 72 65 61 6c  .  The only real
df430 20 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77   difference betw
df440 65 65 6e 20 66 64 61 74 61 73 79 6e 63 28 29 0a  een fdatasync().
df450 2a 2a 20 61 6e 64 20 66 73 79 6e 63 28 29 2c 20  ** and fsync(), 
df460 54 65 64 20 74 65 6c 6c 73 20 75 73 2c 20 69 73  Ted tells us, is
df470 20 74 68 61 74 20 66 64 61 74 61 73 79 6e 63 28   that fdatasync(
df480 29 20 77 69 6c 6c 20 6e 6f 74 20 66 6c 75 73 68  ) will not flush
df490 20 74 68 65 0a 2a 2a 20 69 6e 6f 64 65 20 69 66   the.** inode if
df4a0 20 74 68 65 20 6d 74 69 6d 65 20 6f 72 20 6f 77   the mtime or ow
df4b0 6e 65 72 20 6f 72 20 6f 74 68 65 72 20 69 6e 6f  ner or other ino
df4c0 64 65 20 61 74 74 72 69 62 75 74 65 73 20 68 61  de attributes ha
df4d0 76 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 20 57  ve changed..** W
df4e0 65 20 6f 6e 6c 79 20 63 61 72 65 20 61 62 6f 75  e only care abou
df4f0 74 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65 2c  t the file size,
df500 20 6e 6f 74 20 74 68 65 20 6f 74 68 65 72 20 66   not the other f
df510 69 6c 65 20 61 74 74 72 69 62 75 74 65 73 2c 20  ile attributes, 
df520 73 6f 0a 2a 2a 20 61 73 20 66 61 72 20 61 73 20  so.** as far as 
df530 53 51 4c 69 74 65 20 69 73 20 63 6f 6e 63 65 72  SQLite is concer
df540 6e 65 64 2c 20 61 6e 20 66 64 61 74 61 73 79 6e  ned, an fdatasyn
df550 63 28 29 20 69 73 20 61 6c 77 61 79 73 20 61 64  c() is always ad
df560 65 71 75 61 74 65 2e 0a 2a 2a 20 53 6f 2c 20 77  equate..** So, w
df570 65 20 61 6c 77 61 79 73 20 75 73 65 20 66 64 61  e always use fda
df580 74 61 73 79 6e 63 28 29 20 69 66 20 69 74 20 69  tasync() if it i
df590 73 20 61 76 61 69 6c 61 62 6c 65 2c 20 72 65 67  s available, reg
df5a0 61 72 64 6c 65 73 73 20 6f 66 0a 2a 2a 20 74 68  ardless of.** th
df5b0 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64  e value of the d
df5c0 61 74 61 4f 6e 6c 79 20 66 6c 61 67 2e 0a 2a 2f  ataOnly flag..*/
df5d0 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c  .static int full
df5e0 5f 66 73 79 6e 63 28 69 6e 74 20 66 64 2c 20 69  _fsync(int fd, i
df5f0 6e 74 20 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74  nt fullSync, int
df600 20 64 61 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e   dataOnly){.  in
df610 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  t rc;..  /* The 
df620 66 6f 6c 6c 6f 77 69 6e 67 20 22 69 66 64 65 66  following "ifdef
df630 2f 65 6c 69 66 2f 65 6c 73 65 2f 22 20 62 6c 6f  /elif/else/" blo
df640 63 6b 20 68 61 73 20 74 68 65 20 73 61 6d 65 20  ck has the same 
df650 73 74 72 75 63 74 75 72 65 20 61 73 0a 20 20 2a  structure as.  *
df660 2a 20 74 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e  * the one below.
df670 20 49 74 20 69 73 20 72 65 70 6c 69 63 61 74 65   It is replicate
df680 64 20 68 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f  d here solely to
df690 20 61 76 6f 69 64 20 63 6c 75 74 74 65 72 69 6e   avoid clutterin
df6a0 67 20 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 72  g .  ** up the r
df6b0 65 61 6c 20 63 6f 64 65 20 77 69 74 68 20 74 68  eal code with th
df6c0 65 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54  e UNUSED_PARAMET
df6d0 45 52 28 29 20 6d 61 63 72 6f 73 2e 0a 20 20 2a  ER() macros..  *
df6e0 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
df6f0 4e 4f 5f 53 59 4e 43 0a 20 20 55 4e 55 53 45 44  NO_SYNC.  UNUSED
df700 5f 50 41 52 41 4d 45 54 45 52 28 66 64 29 3b 0a  _PARAMETER(fd);.
df710 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
df720 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20  ER(fullSync);.  
df730 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
df740 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 69  (dataOnly);.#eli
df750 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43  f HAVE_FULLFSYNC
df760 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
df770 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23  TER(dataOnly);.#
df780 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41  else.  UNUSED_PA
df790 52 41 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63  RAMETER(fullSync
df7a0 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
df7b0 4d 45 54 45 52 28 64 61 74 61 4f 6e 6c 79 29 3b  METER(dataOnly);
df7c0 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65  .#endif..  /* Re
df7d0 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 65 72 20  cord the number 
df7e0 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 77 65  of times that we
df7f0 20 64 6f 20 61 20 6e 6f 72 6d 61 6c 20 66 73 79   do a normal fsy
df800 6e 63 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 46  nc() and .  ** F
df810 55 4c 4c 53 59 4e 43 2e 20 20 54 68 69 73 20 69  ULLSYNC.  This i
df820 73 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 65  s used during te
df830 73 74 69 6e 67 20 74 6f 20 76 65 72 69 66 79 20  sting to verify 
df840 74 68 61 74 20 74 68 69 73 20 70 72 6f 63 65 64  that this proced
df850 75 72 65 0a 20 20 2a 2a 20 67 65 74 73 20 63 61  ure.  ** gets ca
df860 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 6f  lled with the co
df870 72 72 65 63 74 20 61 72 67 75 6d 65 6e 74 73 2e  rrect arguments.
df880 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  .  */.#ifdef SQL
df890 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66  ITE_TEST.  if( f
df8a0 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c 69 74 65  ullSync ) sqlite
df8b0 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74  3_fullsync_count
df8c0 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 79  ++;.  sqlite3_sy
df8d0 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64  nc_count++;.#end
df8e0 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63  if..  /* If we c
df8f0 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65  ompiled with the
df900 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20   SQLITE_NO_SYNC 
df910 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69  flag, then synci
df920 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d  ng is a.  ** no-
df930 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53  op.  */.#ifdef S
df940 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20  QLITE_NO_SYNC.  
df950 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
df960 23 65 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46  #elif HAVE_FULLF
df970 53 59 4e 43 0a 20 20 69 66 28 20 66 75 6c 6c 53  SYNC.  if( fullS
df980 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ync ){.    rc = 
df990 66 63 6e 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c  fcntl(fd, F_FULL
df9a0 46 53 59 4e 43 2c 20 30 29 3b 0a 20 20 7d 65 6c  FSYNC, 0);.  }el
df9b0 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a  se{.    rc = 1;.
df9c0 20 20 7d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20    }.  /* If the 
df9d0 46 55 4c 4c 46 53 59 4e 43 20 66 61 69 6c 65 64  FULLFSYNC failed
df9e0 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61  , fall back to a
df9f0 74 74 65 6d 70 74 69 6e 67 20 61 6e 20 66 73 79  ttempting an fsy
dfa00 6e 63 28 29 2e 0a 20 20 2a 2a 20 49 74 20 73 68  nc()..  ** It sh
dfa10 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69  ouldn't be possi
dfa20 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66 73 79 6e  ble for fullfsyn
dfa30 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20 74 68 65  c to fail on the
dfa40 20 6c 6f 63 61 6c 20 0a 20 20 2a 2a 20 66 69 6c   local .  ** fil
dfa50 65 20 73 79 73 74 65 6d 20 28 6f 6e 20 4f 53 58  e system (on OSX
dfa60 29 2c 20 73 6f 20 66 61 69 6c 75 72 65 20 69 6e  ), so failure in
dfa70 64 69 63 61 74 65 73 20 74 68 61 74 20 46 55 4c  dicates that FUL
dfa80 4c 46 53 59 4e 43 0a 20 20 2a 2a 20 69 73 6e 27  LFSYNC.  ** isn'
dfa90 74 20 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20  t supported for 
dfaa0 74 68 69 73 20 66 69 6c 65 20 73 79 73 74 65 6d  this file system
dfab0 2e 20 53 6f 2c 20 61 74 74 65 6d 70 74 20 61 6e  . So, attempt an
dfac0 20 66 73 79 6e 63 20 0a 20 20 2a 2a 20 61 6e 64   fsync .  ** and
dfad0 20 28 66 6f 72 20 6e 6f 77 29 20 69 67 6e 6f 72   (for now) ignor
dfae0 65 20 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f  e the overhead o
dfaf0 66 20 61 20 73 75 70 65 72 66 6c 75 6f 75 73 20  f a superfluous 
dfb00 66 63 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a 20 20  fcntl call.  .  
dfb10 2a 2a 20 49 74 27 64 20 62 65 20 62 65 74 74 65  ** It'd be bette
dfb20 72 20 74 6f 20 64 65 74 65 63 74 20 66 75 6c 6c  r to detect full
dfb30 66 73 79 6e 63 20 73 75 70 70 6f 72 74 20 6f 6e  fsync support on
dfb40 63 65 20 61 6e 64 20 61 76 6f 69 64 20 0a 20 20  ce and avoid .  
dfb50 2a 2a 20 74 68 65 20 66 63 6e 74 6c 20 63 61 6c  ** the fcntl cal
dfb60 6c 20 65 76 65 72 79 20 74 69 6d 65 20 73 79 6e  l every time syn
dfb70 63 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 2a  c is called..  *
dfb80 2f 0a 20 20 69 66 28 20 72 63 20 29 20 72 63 20  /.  if( rc ) rc 
dfb90 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 0a 23 65  = fsync(fd);..#e
dfba0 6c 73 65 20 0a 20 20 72 63 20 3d 20 66 64 61 74  lse .  rc = fdat
dfbb0 61 73 79 6e 63 28 66 64 29 3b 0a 23 69 66 20 4f  async(fd);.#if O
dfbc0 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66 28 20  S_VXWORKS.  if( 
dfbd0 72 63 3d 3d 2d 31 20 26 26 20 65 72 72 6e 6f 3d  rc==-1 && errno=
dfbe0 3d 45 4e 4f 54 53 55 50 20 29 7b 0a 20 20 20 20  =ENOTSUP ){.    
dfbf0 72 63 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a  rc = fsync(fd);.
dfc00 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53    }.#endif /* OS
dfc10 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 23 65 6e 64  _VXWORKS */.#end
dfc20 69 66 20 2f 2a 20 69 66 64 65 66 20 53 51 4c 49  if /* ifdef SQLI
dfc30 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6c 69 66 20  TE_NO_SYNC elif 
dfc40 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 20 2a  HAVE_FULLFSYNC *
dfc50 2f 0a 0a 20 20 69 66 28 20 4f 53 5f 56 58 57 4f  /..  if( OS_VXWO
dfc60 52 4b 53 20 26 26 20 72 63 21 3d 20 2d 31 20 29  RKS && rc!= -1 )
dfc70 7b 0a 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20  {.    rc = 0;.  
dfc80 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
dfc90 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72  ../*.** Make sur
dfca0 65 20 61 6c 6c 20 77 72 69 74 65 73 20 74 6f 20  e all writes to 
dfcb0 61 20 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c  a particular fil
dfcc0 65 20 61 72 65 20 63 6f 6d 6d 69 74 74 65 64 20  e are committed 
dfcd0 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 49  to disk..**.** I
dfce0 66 20 64 61 74 61 4f 6e 6c 79 3d 3d 30 20 74 68  f dataOnly==0 th
dfcf0 65 6e 20 62 6f 74 68 20 74 68 65 20 66 69 6c 65  en both the file
dfd00 20 69 74 73 65 6c 66 20 61 6e 64 20 69 74 73 20   itself and its 
dfd10 6d 65 74 61 64 61 74 61 20 28 66 69 6c 65 0a 2a  metadata (file.*
dfd20 2a 20 73 69 7a 65 2c 20 61 63 63 65 73 73 20 74  * size, access t
dfd30 69 6d 65 2c 20 65 74 63 29 20 61 72 65 20 73 79  ime, etc) are sy
dfd40 6e 63 65 64 2e 20 20 49 66 20 64 61 74 61 4f 6e  nced.  If dataOn
dfd50 6c 79 21 3d 30 20 74 68 65 6e 20 6f 6e 6c 79 20  ly!=0 then only 
dfd60 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64 61 74 61  the.** file data
dfd70 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a 2a 0a 2a   is synced..**.*
dfd80 2a 20 55 6e 64 65 72 20 55 6e 69 78 2c 20 61 6c  * Under Unix, al
dfd90 73 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  so make sure tha
dfda0 74 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  t the directory 
dfdb0 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 66 69  entry for the fi
dfdc0 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 63  le.** has been c
dfdd0 72 65 61 74 65 64 20 62 79 20 66 73 79 6e 63 2d  reated by fsync-
dfde0 69 6e 67 20 74 68 65 20 64 69 72 65 63 74 6f 72  ing the director
dfdf0 79 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  y that contains 
dfe00 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 49 66 20  the file..** If 
dfe10 77 65 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68 69  we do not do thi
dfe20 73 20 61 6e 64 20 77 65 20 65 6e 63 6f 75 6e 74  s and we encount
dfe30 65 72 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75  er a power failu
dfe40 72 65 2c 20 74 68 65 20 64 69 72 65 63 74 6f 72  re, the director
dfe50 79 0a 2a 2a 20 65 6e 74 72 79 20 66 6f 72 20 74  y.** entry for t
dfe60 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69 67 68 74  he journal might
dfe70 20 6e 6f 74 20 65 78 69 73 74 20 61 66 74 65 72   not exist after
dfe80 20 77 65 20 72 65 62 6f 6f 74 2e 20 20 54 68 65   we reboot.  The
dfe90 20 6e 65 78 74 0a 2a 2a 20 53 51 4c 69 74 65 20   next.** SQLite 
dfea0 74 6f 20 61 63 63 65 73 73 20 74 68 65 20 66 69  to access the fi
dfeb0 6c 65 20 77 69 6c 6c 20 6e 6f 74 20 6b 6e 6f 77  le will not know
dfec0 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61   that the journa
dfed0 6c 20 65 78 69 73 74 73 20 28 62 65 63 61 75 73  l exists (becaus
dfee0 65 0a 2a 2a 20 74 68 65 20 64 69 72 65 63 74 6f  e.** the directo
dfef0 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65  ry entry for the
dff00 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 6e 65 76   journal was nev
dff10 65 72 20 63 72 65 61 74 65 64 29 20 61 6e 64 20  er created) and 
dff20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a  the transaction.
dff30 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72 6f 6c 6c  ** will not roll
dff40 20 62 61 63 6b 20 2d 20 70 6f 73 73 69 62 6c 79   back - possibly
dff50 20 6c 65 61 64 69 6e 67 20 74 6f 20 64 61 74 61   leading to data
dff60 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 2e  base corruption.
dff70 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
dff80 6e 69 78 53 79 6e 63 28 73 71 6c 69 74 65 33 5f  nixSync(sqlite3_
dff90 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c  file *id, int fl
dffa0 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  ags){.  int rc;.
dffb0 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c    unixFile *pFil
dffc0 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69  e = (unixFile*)i
dffd0 64 3b 0a 0a 20 20 69 6e 74 20 69 73 44 61 74 61  d;..  int isData
dffe0 4f 6e 6c 79 20 3d 20 28 66 6c 61 67 73 26 53 51  Only = (flags&SQ
dfff0 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e  LITE_SYNC_DATAON
e0000 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 46 75 6c  LY);.  int isFul
e0010 6c 73 79 6e 63 20 3d 20 28 66 6c 61 67 73 26 30  lsync = (flags&0
e0020 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e  x0F)==SQLITE_SYN
e0030 43 5f 46 55 4c 4c 3b 0a 0a 20 20 2f 2a 20 43 68  C_FULL;..  /* Ch
e0040 65 63 6b 20 74 68 61 74 20 6f 6e 65 20 6f 66 20  eck that one of 
e0050 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d  SQLITE_SYNC_NORM
e0060 41 4c 20 6f 72 20 46 55 4c 4c 20 77 61 73 20 70  AL or FULL was p
e0070 61 73 73 65 64 20 2a 2f 0a 20 20 61 73 73 65 72  assed */.  asser
e0080 74 28 28 66 6c 61 67 73 26 30 78 30 46 29 3d 3d  t((flags&0x0F)==
e0090 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d  SQLITE_SYNC_NORM
e00a0 41 4c 0a 20 20 20 20 20 20 7c 7c 20 28 66 6c 61  AL.      || (fla
e00b0 67 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45  gs&0x0F)==SQLITE
e00c0 5f 53 59 4e 43 5f 46 55 4c 4c 0a 20 20 29 3b 0a  _SYNC_FULL.  );.
e00d0 0a 20 20 2f 2a 20 55 6e 69 78 20 63 61 6e 6e 6f  .  /* Unix canno
e00e0 74 2c 20 62 75 74 20 73 6f 6d 65 20 73 79 73 74  t, but some syst
e00f0 65 6d 73 20 6d 61 79 20 72 65 74 75 72 6e 20 53  ems may return S
e0100 51 4c 49 54 45 5f 46 55 4c 4c 20 66 72 6f 6d 20  QLITE_FULL from 
e0110 68 65 72 65 2e 20 54 68 69 73 0a 20 20 2a 2a 20  here. This.  ** 
e0120 6c 69 6e 65 20 69 73 20 74 6f 20 74 65 73 74 20  line is to test 
e0130 74 68 61 74 20 64 6f 69 6e 67 20 73 6f 20 64 6f  that doing so do
e0140 65 73 20 6e 6f 74 20 63 61 75 73 65 20 61 6e 79  es not cause any
e0150 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20 2a 2f 0a   problems..  */.
e0160 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75    SimulateDiskfu
e0170 6c 6c 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20  llError( return 
e0180 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 0a  SQLITE_FULL );..
e0190 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
e01a0 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22 53  );.  OSTRACE2("S
e01b0 59 4e 43 20 20 20 20 25 2d 33 64 5c 6e 22 2c 20  YNC    %-3d\n", 
e01c0 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 72 63 20  pFile->h);.  rc 
e01d0 3d 20 66 75 6c 6c 5f 66 73 79 6e 63 28 70 46 69  = full_fsync(pFi
e01e0 6c 65 2d 3e 68 2c 20 69 73 46 75 6c 6c 73 79 6e  le->h, isFullsyn
e01f0 63 2c 20 69 73 44 61 74 61 4f 6e 6c 79 29 3b 0a  c, isDataOnly);.
e0200 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
e0210 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28  r( rc=1 );.  if(
e0220 20 72 63 20 29 7b 0a 20 20 20 20 70 46 69 6c 65   rc ){.    pFile
e0230 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
e0240 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  rno;.    return 
e0250 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 59  SQLITE_IOERR_FSY
e0260 4e 43 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46  NC;.  }.  if( pF
e0270 69 6c 65 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b  ile->dirfd>=0 ){
e0280 0a 20 20 20 20 69 6e 74 20 65 72 72 3b 0a 20 20  .    int err;.  
e0290 20 20 4f 53 54 52 41 43 45 34 28 22 44 49 52 53    OSTRACE4("DIRS
e02a0 59 4e 43 20 25 2d 33 64 20 28 68 61 76 65 5f 66  YNC %-3d (have_f
e02b0 75 6c 6c 66 73 79 6e 63 3d 25 64 20 66 75 6c 6c  ullfsync=%d full
e02c0 73 79 6e 63 3d 25 64 29 5c 6e 22 2c 20 70 46 69  sync=%d)\n", pFi
e02d0 6c 65 2d 3e 64 69 72 66 64 2c 0a 20 20 20 20 20  le->dirfd,.     
e02e0 20 20 20 20 20 20 20 48 41 56 45 5f 46 55 4c 4c         HAVE_FULL
e02f0 46 53 59 4e 43 2c 20 69 73 46 75 6c 6c 73 79 6e  FSYNC, isFullsyn
e0300 63 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  c);.#ifndef SQLI
e0310 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59  TE_DISABLE_DIRSY
e0320 4e 43 0a 20 20 20 20 2f 2a 20 54 68 65 20 64 69  NC.    /* The di
e0330 72 65 63 74 6f 72 79 20 73 79 6e 63 20 69 73 20  rectory sync is 
e0340 6f 6e 6c 79 20 61 74 74 65 6d 70 74 65 64 20 69  only attempted i
e0350 66 20 66 75 6c 6c 5f 66 73 79 6e 63 20 69 73 0a  f full_fsync is.
e0360 20 20 20 20 2a 2a 20 74 75 72 6e 65 64 20 6f 66      ** turned of
e0370 66 20 6f 72 20 75 6e 61 76 61 69 6c 61 62 6c 65  f or unavailable
e0380 2e 20 20 49 66 20 61 20 66 75 6c 6c 5f 66 73 79  .  If a full_fsy
e0390 6e 63 20 6f 63 63 75 72 72 65 64 20 61 62 6f 76  nc occurred abov
e03a0 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74  e,.    ** then t
e03b0 68 65 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e  he directory syn
e03c0 63 20 69 73 20 73 75 70 65 72 66 6c 75 6f 75 73  c is superfluous
e03d0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
e03e0 20 28 21 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e   (!HAVE_FULLFSYN
e03f0 43 20 7c 7c 20 21 69 73 46 75 6c 6c 73 79 6e 63  C || !isFullsync
e0400 29 20 26 26 20 66 75 6c 6c 5f 66 73 79 6e 63 28  ) && full_fsync(
e0410 70 46 69 6c 65 2d 3e 64 69 72 66 64 2c 30 2c 30  pFile->dirfd,0,0
e0420 29 20 29 7b 0a 20 20 20 20 20 20 20 2f 2a 0a 20  ) ){.       /*. 
e0430 20 20 20 20 20 20 2a 2a 20 57 65 20 68 61 76 65        ** We have
e0440 20 72 65 63 65 69 76 65 64 20 6d 75 6c 74 69 70   received multip
e0450 6c 65 20 72 65 70 6f 72 74 73 20 6f 66 20 66 73  le reports of fs
e0460 79 6e 63 28 29 20 72 65 74 75 72 6e 69 6e 67 0a  ync() returning.
e0470 20 20 20 20 20 20 20 2a 2a 20 65 72 72 6f 72 73         ** errors
e0480 20 77 68 65 6e 20 61 70 70 6c 69 65 64 20 74 6f   when applied to
e0490 20 64 69 72 65 63 74 6f 72 69 65 73 20 6f 6e 20   directories on 
e04a0 63 65 72 74 61 69 6e 20 66 69 6c 65 20 73 79 73  certain file sys
e04b0 74 65 6d 73 2e 0a 20 20 20 20 20 20 20 2a 2a 20  tems..       ** 
e04c0 41 20 66 61 69 6c 65 64 20 64 69 72 65 63 74 6f  A failed directo
e04d0 72 79 20 73 79 6e 63 20 69 73 20 6e 6f 74 20 61  ry sync is not a
e04e0 20 62 69 67 20 64 65 61 6c 2e 20 20 53 6f 20 69   big deal.  So i
e04f0 74 20 73 65 65 6d 73 0a 20 20 20 20 20 20 20 2a  t seems.       *
e0500 2a 20 62 65 74 74 65 72 20 74 6f 20 69 67 6e 6f  * better to igno
e0510 72 65 20 74 68 65 20 65 72 72 6f 72 2e 20 20 54  re the error.  T
e0520 69 63 6b 65 74 20 23 31 36 35 37 0a 20 20 20 20  icket #1657.    
e0530 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 2f 2a 20     */.       /* 
e0540 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
e0550 20 3d 20 65 72 72 6e 6f 3b 20 2a 2f 0a 20 20 20   = errno; */.   
e0560 20 20 20 20 2f 2a 20 72 65 74 75 72 6e 20 53 51      /* return SQ
e0570 4c 49 54 45 5f 49 4f 45 52 52 3b 20 2a 2f 0a 20  LITE_IOERR; */. 
e0580 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
e0590 65 72 72 20 3d 20 63 6c 6f 73 65 28 70 46 69 6c  err = close(pFil
e05a0 65 2d 3e 64 69 72 66 64 29 3b 20 2f 2a 20 4f 6e  e->dirfd); /* On
e05b0 6c 79 20 6e 65 65 64 20 74 6f 20 73 79 6e 63 20  ly need to sync 
e05c0 6f 6e 63 65 2c 20 73 6f 20 63 6c 6f 73 65 20 74  once, so close t
e05d0 68 65 20 2a 2f 0a 20 20 20 20 69 66 28 20 65 72  he */.    if( er
e05e0 72 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20 20  r==0 ){         
e05f0 20 20 20 20 20 2f 2a 20 64 69 72 65 63 74 6f 72       /* director
e0600 79 20 77 68 65 6e 20 77 65 20 61 72 65 20 64 6f  y when we are do
e0610 6e 65 20 2a 2f 0a 20 20 20 20 20 20 70 46 69 6c  ne */.      pFil
e0620 65 2d 3e 64 69 72 66 64 20 3d 20 2d 31 3b 0a 20  e->dirfd = -1;. 
e0630 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
e0640 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
e0650 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20   = errno;.      
e0660 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  rc = SQLITE_IOER
e0670 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20 20 20  R_DIR_CLOSE;.   
e0680 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
e0690 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75  rc;.}../*.** Tru
e06a0 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66 69  ncate an open fi
e06b0 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 69 65  le to a specifie
e06c0 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69 63  d size.*/.static
e06d0 20 69 6e 74 20 75 6e 69 78 54 72 75 6e 63 61 74   int unixTruncat
e06e0 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
e06f0 69 64 2c 20 69 36 34 20 6e 42 79 74 65 29 7b 0a  id, i64 nByte){.
e0700 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65    int rc;.  asse
e0710 72 74 28 20 69 64 20 29 3b 0a 20 20 53 69 6d 75  rt( id );.  Simu
e0720 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74  lateIOError( ret
e0730 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
e0740 5f 54 52 55 4e 43 41 54 45 20 29 3b 0a 20 20 72  _TRUNCATE );.  r
e0750 63 20 3d 20 66 74 72 75 6e 63 61 74 65 28 28 28  c = ftruncate(((
e0760 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 68  unixFile*)id)->h
e0770 2c 20 28 6f 66 66 5f 74 29 6e 42 79 74 65 29 3b  , (off_t)nByte);
e0780 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20  .  if( rc ){.   
e0790 20 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29   ((unixFile*)id)
e07a0 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72  ->lastErrno = er
e07b0 72 6e 6f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  rno;.    return 
e07c0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55  SQLITE_IOERR_TRU
e07d0 4e 43 41 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a  NCATE;.  }else{.
e07e0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
e07f0 45 5f 4f 4b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  E_OK;.  }.}../*.
e0800 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65  ** Determine the
e0810 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66   current size of
e0820 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73   a file in bytes
e0830 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
e0840 6e 69 78 46 69 6c 65 53 69 7a 65 28 73 71 6c 69  nixFileSize(sqli
e0850 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36  te3_file *id, i6
e0860 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74  4 *pSize){.  int
e0870 20 72 63 3b 0a 20 20 73 74 72 75 63 74 20 73 74   rc;.  struct st
e0880 61 74 20 62 75 66 3b 0a 20 20 61 73 73 65 72 74  at buf;.  assert
e0890 28 20 69 64 20 29 3b 0a 20 20 72 63 20 3d 20 66  ( id );.  rc = f
e08a0 73 74 61 74 28 28 28 75 6e 69 78 46 69 6c 65 2a  stat(((unixFile*
e08b0 29 69 64 29 2d 3e 68 2c 20 26 62 75 66 29 3b 0a  )id)->h, &buf);.
e08c0 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f    SimulateIOErro
e08d0 72 28 20 72 63 3d 31 20 29 3b 0a 20 20 69 66 28  r( rc=1 );.  if(
e08e0 20 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 28 28   rc!=0 ){.    ((
e08f0 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  unixFile*)id)->l
e0900 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  astErrno = errno
e0910 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
e0920 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41 54 3b  ITE_IOERR_FSTAT;
e0930 0a 20 20 7d 0a 20 20 2a 70 53 69 7a 65 20 3d 20  .  }.  *pSize = 
e0940 62 75 66 2e 73 74 5f 73 69 7a 65 3b 0a 0a 20 20  buf.st_size;..  
e0950 2f 2a 20 57 68 65 6e 20 6f 70 65 6e 69 6e 67 20  /* When opening 
e0960 61 20 7a 65 72 6f 2d 73 69 7a 65 20 64 61 74 61  a zero-size data
e0970 62 61 73 65 2c 20 74 68 65 20 66 69 6e 64 4c 6f  base, the findLo
e0980 63 6b 49 6e 66 6f 28 29 20 70 72 6f 63 65 64 75  ckInfo() procedu
e0990 72 65 0a 20 20 2a 2a 20 77 72 69 74 65 73 20 61  re.  ** writes a
e09a0 20 73 69 6e 67 6c 65 20 62 79 74 65 20 69 6e 74   single byte int
e09b0 6f 20 74 68 61 74 20 66 69 6c 65 20 69 6e 20 6f  o that file in o
e09c0 72 64 65 72 20 74 6f 20 77 6f 72 6b 20 61 72 6f  rder to work aro
e09d0 75 6e 64 20 61 20 62 75 67 0a 20 20 2a 2a 20 69  und a bug.  ** i
e09e0 6e 20 74 68 65 20 4f 53 2d 58 20 6d 73 64 6f 73  n the OS-X msdos
e09f0 20 66 69 6c 65 73 79 73 74 65 6d 2e 20 20 49 6e   filesystem.  In
e0a00 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20   order to avoid 
e0a10 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 20 75 70  problems with up
e0a20 70 65 72 0a 20 20 2a 2a 20 6c 61 79 65 72 73 2c  per.  ** layers,
e0a30 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65 70 6f   we need to repo
e0a40 72 74 20 74 68 69 73 20 66 69 6c 65 20 73 69 7a  rt this file siz
e0a50 65 20 61 73 20 7a 65 72 6f 20 65 76 65 6e 20 74  e as zero even t
e0a60 68 6f 75 67 68 20 69 74 20 69 73 0a 20 20 2a 2a  hough it is.  **
e0a70 20 72 65 61 6c 6c 79 20 31 2e 20 20 20 54 69 63   really 1.   Tic
e0a80 6b 65 74 20 23 33 32 36 30 2e 0a 20 20 2a 2f 0a  ket #3260..  */.
e0a90 20 20 69 66 28 20 2a 70 53 69 7a 65 3d 3d 31 20    if( *pSize==1 
e0aa0 29 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 0a 0a  ) *pSize = 0;...
e0ab0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e0ac0 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54  OK;.}..#if SQLIT
e0ad0 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
e0ae0 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65  _STYLE && define
e0af0 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a  d(__APPLE__)./*.
e0b00 2a 2a 20 48 61 6e 64 6c 65 72 20 66 6f 72 20 70  ** Handler for p
e0b10 72 6f 78 79 2d 6c 6f 63 6b 69 6e 67 20 66 69 6c  roxy-locking fil
e0b20 65 2d 63 6f 6e 74 72 6f 6c 20 76 65 72 62 73 2e  e-control verbs.
e0b30 20 20 44 65 66 69 6e 65 64 20 62 65 6c 6f 77 20    Defined below 
e0b40 69 6e 20 74 68 65 0a 2a 2a 20 70 72 6f 78 79 69  in the.** proxyi
e0b50 6e 67 20 6c 6f 63 6b 69 6e 67 20 64 69 76 69 73  ng locking divis
e0b60 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ion..*/.static i
e0b70 6e 74 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e 74  nt proxyFileCont
e0b80 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  rol(sqlite3_file
e0b90 2a 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 23 65  *,int,void*);.#e
e0ba0 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66  ndif.../*.** Inf
e0bb0 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 20 63 6f 6e  ormation and con
e0bc0 74 72 6f 6c 20 6f 66 20 61 6e 20 6f 70 65 6e 20  trol of an open 
e0bd0 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a  file handle..*/.
e0be0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46  static int unixF
e0bf0 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69 74  ileControl(sqlit
e0c00 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
e0c10 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29   op, void *pArg)
e0c20 7b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29  {.  switch( op )
e0c30 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
e0c40 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54  E_FCNTL_LOCKSTAT
e0c50 45 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74  E: {.      *(int
e0c60 2a 29 70 41 72 67 20 3d 20 28 28 75 6e 69 78 46  *)pArg = ((unixF
e0c70 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79  ile*)id)->lockty
e0c80 70 65 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  pe;.      return
e0c90 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
e0ca0 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
e0cb0 45 5f 4c 41 53 54 5f 45 52 52 4e 4f 3a 20 7b 0a  E_LAST_ERRNO: {.
e0cc0 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72        *(int*)pAr
e0cd0 67 20 3d 20 28 28 75 6e 69 78 46 69 6c 65 2a 29  g = ((unixFile*)
e0ce0 69 64 29 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b 0a  id)->lastErrno;.
e0cf0 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
e0d00 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 23 69  ITE_OK;.    }.#i
e0d10 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20  fndef NDEBUG.   
e0d20 20 2f 2a 20 54 68 65 20 70 61 67 65 72 20 63 61   /* The pager ca
e0d30 6c 6c 73 20 74 68 69 73 20 6d 65 74 68 6f 64 20  lls this method 
e0d40 74 6f 20 73 69 67 6e 61 6c 20 74 68 61 74 20 69  to signal that i
e0d50 74 20 68 61 73 20 64 6f 6e 65 0a 20 20 20 20 2a  t has done.    *
e0d60 2a 20 61 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 64  * a rollback and
e0d70 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61   that the databa
e0d80 73 65 20 69 73 20 74 68 65 72 65 66 6f 72 65 20  se is therefore 
e0d90 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 0a 20 20  unchanged and.  
e0da0 20 20 2a 2a 20 69 74 20 68 65 6e 63 65 20 69 74    ** it hence it
e0db0 20 69 73 20 4f 4b 20 66 6f 72 20 74 68 65 20 74   is OK for the t
e0dc0 72 61 6e 73 61 63 74 69 6f 6e 20 63 68 61 6e 67  ransaction chang
e0dd0 65 20 63 6f 75 6e 74 65 72 20 74 6f 20 62 65 0a  e counter to be.
e0de0 20 20 20 20 2a 2a 20 75 6e 63 68 61 6e 67 65 64      ** unchanged
e0df0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
e0e00 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 44  e SQLITE_FCNTL_D
e0e10 42 5f 55 4e 43 48 41 4e 47 45 44 3a 20 7b 0a 20  B_UNCHANGED: {. 
e0e20 20 20 20 20 20 28 28 75 6e 69 78 46 69 6c 65 2a       ((unixFile*
e0e30 29 69 64 29 2d 3e 64 62 55 70 64 61 74 65 20 3d  )id)->dbUpdate =
e0e40 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   0;.      return
e0e50 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
e0e60 7d 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c  }.#endif.#if SQL
e0e70 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
e0e80 4e 47 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69  NG_STYLE && defi
e0e90 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20  ned(__APPLE__). 
e0ea0 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 53     case SQLITE_S
e0eb0 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45  ET_LOCKPROXYFILE
e0ec0 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  :.    case SQLIT
e0ed0 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46  E_GET_LOCKPROXYF
e0ee0 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20 72 65 74  ILE: {.      ret
e0ef0 75 72 6e 20 70 72 6f 78 79 46 69 6c 65 43 6f 6e  urn proxyFileCon
e0f00 74 72 6f 6c 28 69 64 2c 6f 70 2c 70 41 72 67 29  trol(id,op,pArg)
e0f10 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f  ;.    }.#endif /
e0f20 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  * SQLITE_ENABLE_
e0f30 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 26 26  LOCKING_STYLE &&
e0f40 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45   defined(__APPLE
e0f50 5f 5f 29 20 2a 2f 0a 20 20 7d 0a 20 20 72 65 74  __) */.  }.  ret
e0f60 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
e0f70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
e0f80 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a  n the sector siz
e0f90 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68  e in bytes of th
e0fa0 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f  e underlying blo
e0fb0 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a  ck device for.**
e0fc0 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66   the specified f
e0fd0 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d  ile. This is alm
e0fe0 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 62  ost always 512 b
e0ff0 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65  ytes, but may be
e1000 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73  .** larger for s
e1010 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a  ome devices..**.
e1020 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61  ** SQLite code a
e1030 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63  ssumes this func
e1040 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c  tion cannot fail
e1050 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65  . It also assume
e1060 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f  s that.** if two
e1070 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 74   files are creat
e1080 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66  ed in the same f
e1090 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63  ile-system direc
e10a0 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20  tory (i.e..** a 
e10b0 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 73  database and its
e10c0 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74   journal file) t
e10d0 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 73  hat the sector s
e10e0 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a  ize will be the.
e10f0 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68  ** same for both
e1100 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e1110 75 6e 69 78 53 65 63 74 6f 72 53 69 7a 65 28 73  unixSectorSize(s
e1120 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 4e 6f 74  qlite3_file *Not
e1130 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f  Used){.  UNUSED_
e1140 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
e1150 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  d);.  return SQL
e1160 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54  ITE_DEFAULT_SECT
e1170 4f 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a  OR_SIZE;.}../*.*
e1180 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64 65 76  * Return the dev
e1190 69 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74  ice characterist
e11a0 69 63 73 20 66 6f 72 20 74 68 65 20 66 69 6c 65  ics for the file
e11b0 2e 20 54 68 69 73 20 69 73 20 61 6c 77 61 79 73  . This is always
e11c0 20 30 20 66 6f 72 20 75 6e 69 78 2e 0a 2a 2f 0a   0 for unix..*/.
e11d0 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 44  static int unixD
e11e0 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73  eviceCharacteris
e11f0 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c  tics(sqlite3_fil
e1200 65 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55  e *NotUsed){.  U
e1210 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
e1220 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75  NotUsed);.  retu
e1230 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48  rn 0;.}../*.** H
e1240 65 72 65 20 65 6e 64 73 20 74 68 65 20 69 6d 70  ere ends the imp
e1250 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61  lementation of a
e1260 6c 6c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ll sqlite3_file 
e1270 6d 65 74 68 6f 64 73 2e 0a 2a 2a 0a 2a 2a 2a 2a  methods..**.****
e1280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1290 2a 2a 20 45 6e 64 20 73 71 6c 69 74 65 33 5f 66  ** End sqlite3_f
e12a0 69 6c 65 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a  ile Methods ****
e12b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e12c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a  ***********.****
e12d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e12e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e12f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e1310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a  **********/../*.
e1320 2a 2a 20 54 68 69 73 20 64 69 76 69 73 69 6f 6e  ** This division
e1330 20 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69   contains defini
e1340 74 69 6f 6e 73 20 6f 66 20 73 71 6c 69 74 65 33  tions of sqlite3
e1350 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  _io_methods obje
e1360 63 74 73 20 74 68 61 74 0a 2a 2a 20 69 6d 70 6c  cts that.** impl
e1370 65 6d 65 6e 74 20 76 61 72 69 6f 75 73 20 66 69  ement various fi
e1380 6c 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74  le locking strat
e1390 65 67 69 65 73 2e 20 20 49 74 20 61 6c 73 6f 20  egies.  It also 
e13a0 63 6f 6e 74 61 69 6e 73 20 64 65 66 69 6e 69 74  contains definit
e13b0 69 6f 6e 73 0a 2a 2a 20 6f 66 20 22 66 69 6e 64  ions.** of "find
e13c0 65 72 22 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20  er" functions.  
e13d0 41 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f  A finder-functio
e13e0 6e 20 69 73 20 75 73 65 64 20 74 6f 20 6c 6f 63  n is used to loc
e13f0 61 74 65 20 74 68 65 20 61 70 70 72 6f 70 72 69  ate the appropri
e1400 61 74 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69  ate.** sqlite3_i
e1410 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  o_methods object
e1420 20 66 6f 72 20 61 20 70 61 72 74 69 63 75 6c 61   for a particula
e1430 72 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  r database file.
e1440 20 20 54 68 65 20 70 41 70 70 44 61 74 61 0a 2a    The pAppData.*
e1450 2a 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 73  * field of the s
e1460 71 6c 69 74 65 33 5f 76 66 73 20 56 46 53 20 6f  qlite3_vfs VFS o
e1470 62 6a 65 63 74 73 20 61 72 65 20 69 6e 69 74 69  bjects are initi
e1480 61 6c 69 7a 65 64 20 74 6f 20 62 65 20 70 6f 69  alized to be poi
e1490 6e 74 65 72 73 20 74 6f 0a 2a 2a 20 74 68 65 20  nters to.** the 
e14a0 63 6f 72 72 65 63 74 20 66 69 6e 64 65 72 2d 66  correct finder-f
e14b0 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68 61 74  unction for that
e14c0 20 56 46 53 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74   VFS..**.** Most
e14d0 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e   finder function
e14e0 73 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  s return a point
e14f0 65 72 20 74 6f 20 61 20 66 69 78 65 64 20 73 71  er to a fixed sq
e1500 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
e1510 0a 2a 2a 20 6f 62 6a 65 63 74 2e 20 20 54 68 65  .** object.  The
e1520 20 6f 6e 6c 79 20 69 6e 74 65 72 65 73 74 69 6e   only interestin
e1530 67 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f  g finder-functio
e1540 6e 20 69 73 20 61 75 74 6f 6c 6f 63 6b 49 6f 46  n is autolockIoF
e1550 69 6e 64 65 72 2c 20 77 68 69 63 68 0a 2a 2a 20  inder, which.** 
e1560 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 66 69 6c  looks at the fil
e1570 65 73 79 73 74 65 6d 20 74 79 70 65 20 61 6e 64  esystem type and
e1580 20 74 72 69 65 73 20 74 6f 20 67 75 65 73 73 20   tries to guess 
e1590 74 68 65 20 62 65 73 74 20 6c 6f 63 6b 69 6e 67  the best locking
e15a0 0a 2a 2a 20 73 74 72 61 74 65 67 79 20 66 72 6f  .** strategy fro
e15b0 6d 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f  m that..**.** Fo
e15c0 72 20 66 69 6e 64 65 72 2d 66 75 6e 74 69 6f 6e  r finder-funtion
e15d0 20 46 2c 20 74 77 6f 20 6f 62 6a 65 63 74 73 20   F, two objects 
e15e0 61 72 65 20 63 72 65 61 74 65 64 3a 0a 2a 2a 0a  are created:.**.
e15f0 2a 2a 20 20 20 20 28 31 29 20 54 68 65 20 72 65  **    (1) The re
e1600 61 6c 20 66 69 6e 64 65 72 2d 66 75 6e 63 74 69  al finder-functi
e1610 6f 6e 20 6e 61 6d 65 64 20 22 46 49 6d 70 74 28  on named "FImpt(
e1620 29 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29  )"..**.**    (2)
e1630 20 41 20 63 6f 6e 73 74 61 6e 74 20 70 6f 69 6e   A constant poin
e1640 74 65 72 20 74 6f 20 74 68 69 73 20 66 75 6e 63  ter to this func
e1650 74 69 6f 6e 20 6e 61 6d 65 64 20 6a 75 73 74 20  tion named just 
e1660 22 46 22 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 41 20  "F"..**.**.** A 
e1670 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 46  pointer to the F
e1680 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64   pointer is used
e1690 20 61 73 20 74 68 65 20 70 41 70 70 44 61 74 61   as the pAppData
e16a0 20 76 61 6c 75 65 20 66 6f 72 20 56 46 53 0a 2a   value for VFS.*
e16b0 2a 20 6f 62 6a 65 63 74 73 2e 20 20 57 65 20 68  * objects.  We h
e16c0 61 76 65 20 74 6f 20 64 6f 20 74 68 69 73 20 69  ave to do this i
e16d0 6e 73 74 65 61 64 20 6f 66 20 6c 65 74 74 69 6e  nstead of lettin
e16e0 67 20 70 41 70 70 44 61 74 61 20 70 6f 69 6e 74  g pAppData point
e16f0 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 61 74 20  .** directly at 
e1700 74 68 65 20 66 69 6e 64 65 72 2d 66 75 6e 63 74  the finder-funct
e1710 69 6f 6e 20 73 69 6e 63 65 20 43 39 30 20 72 75  ion since C90 ru
e1720 6c 65 73 20 70 72 65 76 65 6e 74 20 61 20 76 6f  les prevent a vo
e1730 69 64 2a 0a 2a 2a 20 66 72 6f 6d 20 62 65 20 63  id*.** from be c
e1740 61 73 74 20 69 6e 74 6f 20 61 20 66 75 6e 63 74  ast into a funct
e1750 69 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a  ion pointer..**.
e1760 2a 2a 0a 2a 2a 20 45 61 63 68 20 69 6e 73 74 61  **.** Each insta
e1770 6e 63 65 20 6f 66 20 74 68 69 73 20 6d 61 63 72  nce of this macr
e1780 6f 20 67 65 6e 65 72 61 74 65 73 20 74 77 6f 20  o generates two 
e1790 6f 62 6a 65 63 74 73 3a 0a 2a 2a 0a 2a 2a 20 20  objects:.**.**  
e17a0 20 2a 20 20 41 20 63 6f 6e 73 74 61 6e 74 20 73   *  A constant s
e17b0 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
e17c0 73 20 6f 62 6a 65 63 74 20 63 61 6c 6c 20 4d 45  s object call ME
e17d0 54 48 4f 44 20 74 68 61 74 20 68 61 73 20 6c 6f  THOD that has lo
e17e0 63 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 6d 65  cking.**      me
e17f0 74 68 6f 64 73 20 43 4c 4f 53 45 2c 20 4c 4f 43  thods CLOSE, LOC
e1800 4b 2c 20 55 4e 4c 4f 43 4b 2c 20 43 4b 52 45 53  K, UNLOCK, CKRES
e1810 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  LOCK..**.**   * 
e1820 20 41 6e 20 49 2f 4f 20 6d 65 74 68 6f 64 20 66   An I/O method f
e1830 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 63  inder function c
e1840 61 6c 6c 65 64 20 46 49 4e 44 45 52 20 74 68 61  alled FINDER tha
e1850 74 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  t returns a poin
e1860 74 65 72 0a 2a 2a 20 20 20 20 20 20 74 6f 20 74  ter.**      to t
e1870 68 65 20 4d 45 54 48 4f 44 20 6f 62 6a 65 63 74  he METHOD object
e1880 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73   in the previous
e1890 20 62 75 6c 6c 65 74 2e 0a 2a 2f 0a 23 64 65 66   bullet..*/.#def
e18a0 69 6e 65 20 49 4f 4d 45 54 48 4f 44 53 28 46 49  ine IOMETHODS(FI
e18b0 4e 44 45 52 2c 20 4d 45 54 48 4f 44 2c 20 43 4c  NDER, METHOD, CL
e18c0 4f 53 45 2c 20 4c 4f 43 4b 2c 20 55 4e 4c 4f 43  OSE, LOCK, UNLOC
e18d0 4b 2c 20 43 4b 4c 4f 43 4b 29 20 20 20 20 20 20  K, CKLOCK)      
e18e0 20 20 20 20 20 20 20 20 20 5c 0a 73 74 61 74 69           \.stati
e18f0 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  c const sqlite3_
e1900 69 6f 5f 6d 65 74 68 6f 64 73 20 4d 45 54 48 4f  io_methods METHO
e1910 44 20 3d 20 7b 20 20 20 20 20 20 20 20 20 20 20  D = {           
e1920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1930 20 20 20 20 20 20 20 20 5c 0a 20 20 20 31 2c 20          \.   1, 
e1940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1950 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72           /* iVer
e1960 73 69 6f 6e 20 2a 2f 20 20 20 20 20 20 20 20 20  sion */         
e1970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1980 20 20 20 20 20 20 20 5c 0a 20 20 20 43 4c 4f 53         \.   CLOS
e1990 45 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E,              
e19a0 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73          /* xClos
e19b0 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  e */            
e19c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e19d0 20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 52        \.   unixR
e19e0 65 61 64 2c 20 20 20 20 20 20 20 20 20 20 20 20  ead,            
e19f0 20 20 20 20 20 20 20 2f 2a 20 78 52 65 61 64 20         /* xRead 
e1a00 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
e1a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1a20 20 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 57 72       \.   unixWr
e1a30 69 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ite,            
e1a40 20 20 20 20 20 20 2f 2a 20 78 57 72 69 74 65 20        /* xWrite 
e1a50 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
e1a60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1a70 20 20 20 20 5c 0a 20 20 20 75 6e 69 78 54 72 75      \.   unixTru
e1a80 6e 63 61 74 65 2c 20 20 20 20 20 20 20 20 20 20  ncate,          
e1a90 20 20 20 20 20 2f 2a 20 78 54 72 75 6e 63 61 74       /* xTruncat
e1aa0 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  e */            
e1ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1ac0 20 20 20 5c 0a 20 20 20 75 6e 69 78 53 79 6e 63     \.   unixSync
e1ad0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e1ae0 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 20      /* xSync */ 
e1af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1b10 20 20 5c 0a 20 20 20 75 6e 69 78 46 69 6c 65 53    \.   unixFileS
e1b20 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ize,            
e1b30 20 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20     /* xFileSize 
e1b40 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
e1b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1b60 20 5c 0a 20 20 20 4c 4f 43 4b 2c 20 20 20 20 20   \.   LOCK,     
e1b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1b80 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 20 20 20    /* xLock */   
e1b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1bb0 5c 0a 20 20 20 55 4e 4c 4f 43 4b 2c 20 20 20 20  \.   UNLOCK,    
e1bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1bd0 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 20 20   /* xUnlock */  
e1be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1bf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
e1c00 0a 20 20 20 43 4b 4c 4f 43 4b 2c 20 20 20 20 20  .   CKLOCK,     
e1c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1c20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65  /* xCheckReserve
e1c30 64 4c 6f 63 6b 20 2a 2f 20 20 20 20 20 20 20 20  dLock */        
e1c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
e1c50 20 20 20 75 6e 69 78 46 69 6c 65 43 6f 6e 74 72     unixFileContr
e1c60 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ol,            /
e1c70 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a  * xFileControl *
e1c80 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
e1c90 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
e1ca0 20 20 75 6e 69 78 53 65 63 74 6f 72 53 69 7a 65    unixSectorSize
e1cb0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
e1cc0 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 20   xSectorSize */ 
e1cd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1ce0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
e1cf0 20 75 6e 69 78 44 65 76 69 63 65 43 68 61 72 61   unixDeviceChara
e1d00 63 74 65 72 69 73 74 69 63 73 20 20 20 2f 2a 20  cteristics   /* 
e1d10 78 44 65 76 69 63 65 43 61 70 61 62 69 6c 69 74  xDeviceCapabilit
e1d20 69 65 73 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ies */          
e1d30 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 3b 20             \.}; 
e1d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1d60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1d80 20 20 20 20 20 20 20 20 20 20 5c 0a 73 74 61 74            \.stat
e1d90 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
e1da0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 46 49 4e  _io_methods *FIN
e1db0 44 45 52 23 23 49 6d 70 6c 28 63 6f 6e 73 74 20  DER##Impl(const 
e1dc0 63 68 61 72 20 2a 7a 2c 20 75 6e 69 78 46 69 6c  char *z, unixFil
e1dd0 65 20 2a 70 29 7b 20 20 20 5c 0a 20 20 55 4e 55  e *p){   \.  UNU
e1de0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a 29  SED_PARAMETER(z)
e1df0 3b 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54  ; UNUSED_PARAMET
e1e00 45 52 28 70 29 3b 20 20 20 20 20 20 20 20 20 20  ER(p);          
e1e10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1e20 20 20 20 20 20 20 20 20 5c 0a 20 20 72 65 74 75          \.  retu
e1e30 72 6e 20 26 4d 45 54 48 4f 44 3b 20 20 20 20 20  rn &METHOD;     
e1e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1e70 20 20 20 20 20 20 20 5c 0a 7d 20 20 20 20 20 20         \.}      
e1e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e1ec0 20 20 20 20 20 20 5c 0a 73 74 61 74 69 63 20 63        \.static c
e1ed0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f  onst sqlite3_io_
e1ee0 6d 65 74 68 6f 64 73 20 2a 28 2a 63 6f 6e 73 74  methods *(*const
e1ef0 20 46 49 4e 44 45 52 29 28 63 6f 6e 73 74 20 63   FINDER)(const c
e1f00 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65 20 2a 70  har*,unixFile *p
e1f10 29 20 20 20 20 5c 0a 20 20 20 20 3d 20 46 49 4e  )    \.    = FIN
e1f20 44 45 52 23 23 49 6d 70 6c 3b 0a 0a 2f 2a 0a 2a  DER##Impl;../*.*
e1f30 2a 20 48 65 72 65 20 61 72 65 20 61 6c 6c 20 6f  * Here are all o
e1f40 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f  f the sqlite3_io
e1f50 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 73  _methods objects
e1f60 20 66 6f 72 20 65 61 63 68 20 6f 66 20 74 68 65   for each of the
e1f70 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61  .** locking stra
e1f80 74 65 67 69 65 73 2e 20 20 46 75 6e 63 74 69 6f  tegies.  Functio
e1f90 6e 73 20 74 68 61 74 20 72 65 74 75 72 6e 20 70  ns that return p
e1fa0 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 73 65  ointers to these
e1fb0 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 61 72 65 20   methods.** are 
e1fc0 61 6c 73 6f 20 63 72 65 61 74 65 64 2e 0a 2a 2f  also created..*/
e1fd0 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 70 6f  .IOMETHODS(.  po
e1fe0 73 69 78 49 6f 46 69 6e 64 65 72 2c 20 20 20 20  sixIoFinder,    
e1ff0 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65          /* Finde
e2000 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20  r function name 
e2010 2a 2f 0a 20 20 70 6f 73 69 78 49 6f 4d 65 74 68  */.  posixIoMeth
e2020 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ods,           /
e2030 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  * sqlite3_io_met
e2040 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65  hods object name
e2050 20 2a 2f 0a 20 20 75 6e 69 78 43 6c 6f 73 65 2c   */.  unixClose,
e2060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2070 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64  /* xClose method
e2080 20 2a 2f 0a 20 20 75 6e 69 78 4c 6f 63 6b 2c 20   */.  unixLock, 
e2090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e20a0 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20  /* xLock method 
e20b0 2a 2f 0a 20 20 75 6e 69 78 55 6e 6c 6f 63 6b 2c  */.  unixUnlock,
e20c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
e20d0 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64  * xUnlock method
e20e0 20 2a 2f 0a 20 20 75 6e 69 78 43 68 65 63 6b 52   */.  unixCheckR
e20f0 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20 20  eservedLock     
e2100 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65  /* xCheckReserve
e2110 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  dLock method */.
e2120 29 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 6e  ).IOMETHODS(.  n
e2130 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20 20  olockIoFinder,  
e2140 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64           /* Find
e2150 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65  er function name
e2160 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 49 6f 4d 65   */.  nolockIoMe
e2170 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20  thods,          
e2180 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  /* sqlite3_io_me
e2190 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d  thods object nam
e21a0 65 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 6c 6f  e */.  nolockClo
e21b0 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
e21c0 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f   /* xClose metho
e21d0 64 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 4c 6f 63  d */.  nolockLoc
e21e0 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k,              
e21f0 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64   /* xLock method
e2200 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f   */.  nolockUnlo
e2210 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ck,             
e2220 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f  /* xUnlock metho
e2230 64 20 2a 2f 0a 20 20 6e 6f 6c 6f 63 6b 43 68 65  d */.  nolockChe
e2240 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20  ckReservedLock  
e2250 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76   /* xCheckReserv
e2260 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f  edLock method */
e2270 0a 29 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20  .).IOMETHODS(.  
e2280 64 6f 74 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c  dotlockIoFinder,
e2290 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e            /* Fin
e22a0 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d  der function nam
e22b0 65 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 49 6f  e */.  dotlockIo
e22c0 4d 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20  Methods,        
e22d0 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d   /* sqlite3_io_m
e22e0 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61  ethods object na
e22f0 6d 65 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43  me */.  dotlockC
e2300 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  lose,           
e2310 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68    /* xClose meth
e2320 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 4c  od */.  dotlockL
e2330 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20  ock,            
e2340 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f    /* xLock metho
e2350 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 55 6e  d */.  dotlockUn
e2360 6c 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20  lock,           
e2370 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68   /* xUnlock meth
e2380 6f 64 20 2a 2f 0a 20 20 64 6f 74 6c 6f 63 6b 43  od */.  dotlockC
e2390 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
e23a0 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72    /* xCheckReser
e23b0 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a  vedLock method *
e23c0 2f 0a 29 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f  /.)..#if SQLITE_
e23d0 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
e23e0 54 59 4c 45 20 26 26 20 21 4f 53 5f 56 58 57 4f  TYLE && !OS_VXWO
e23f0 52 4b 53 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20  RKS.IOMETHODS(. 
e2400 20 66 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 2c 20   flockIoFinder, 
e2410 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
e2420 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61  nder function na
e2430 6d 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b 49 6f 4d  me */.  flockIoM
e2440 65 74 68 6f 64 73 2c 20 20 20 20 20 20 20 20 20  ethods,         
e2450 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f    /* sqlite3_io_
e2460 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e  methods object n
e2470 61 6d 65 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 6c  ame */.  flockCl
e2480 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ose,            
e2490 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74     /* xClose met
e24a0 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 4c 6f  hod */.  flockLo
e24b0 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ck,             
e24c0 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68     /* xLock meth
e24d0 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 55 6e 6c  od */.  flockUnl
e24e0 6f 63 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20  ock,            
e24f0 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74    /* xUnlock met
e2500 68 6f 64 20 2a 2f 0a 20 20 66 6c 6f 63 6b 43 68  hod */.  flockCh
e2510 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20  eckReservedLock 
e2520 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73 65     /* xCheckRese
e2530 72 76 65 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20  rvedLock method 
e2540 2a 2f 0a 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66  */.).#endif..#if
e2550 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 49 4f 4d 45   OS_VXWORKS.IOME
e2560 54 48 4f 44 53 28 0a 20 20 73 65 6d 49 6f 46 69  THODS(.  semIoFi
e2570 6e 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 20  nder,           
e2580 20 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e     /* Finder fun
e2590 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20  ction name */.  
e25a0 73 65 6d 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20  semIoMethods,   
e25b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c            /* sql
e25c0 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
e25d0 6f 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20  object name */. 
e25e0 20 73 65 6d 43 6c 6f 73 65 2c 20 20 20 20 20 20   semClose,      
e25f0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43             /* xC
e2600 6c 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20  lose method */. 
e2610 20 73 65 6d 4c 6f 63 6b 2c 20 20 20 20 20 20 20   semLock,       
e2620 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c             /* xL
e2630 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ock method */.  
e2640 73 65 6d 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20  semUnlock,      
e2650 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e            /* xUn
e2660 6c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20  lock method */. 
e2670 20 73 65 6d 43 68 65 63 6b 52 65 73 65 72 76 65   semCheckReserve
e2680 64 4c 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78 43  dLock      /* xC
e2690 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
e26a0 20 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e   method */.).#en
e26b0 64 69 66 0a 0a 23 69 66 20 64 65 66 69 6e 65 64  dif..#if defined
e26c0 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53  (__APPLE__) && S
e26d0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
e26e0 4b 49 4e 47 5f 53 54 59 4c 45 0a 49 4f 4d 45 54  KING_STYLE.IOMET
e26f0 48 4f 44 53 28 0a 20 20 61 66 70 49 6f 46 69 6e  HODS(.  afpIoFin
e2700 64 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  der,            
e2710 20 20 2f 2a 20 46 69 6e 64 65 72 20 66 75 6e 63    /* Finder func
e2720 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 61  tion name */.  a
e2730 66 70 49 6f 4d 65 74 68 6f 64 73 2c 20 20 20 20  fpIoMethods,    
e2740 20 20 20 20 20 20 20 20 20 2f 2a 20 73 71 6c 69           /* sqli
e2750 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f  te3_io_methods o
e2760 62 6a 65 63 74 20 6e 61 6d 65 20 2a 2f 0a 20 20  bject name */.  
e2770 61 66 70 43 6c 6f 73 65 2c 20 20 20 20 20 20 20  afpClose,       
e2780 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c            /* xCl
e2790 6f 73 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ose method */.  
e27a0 61 66 70 4c 6f 63 6b 2c 20 20 20 20 20 20 20 20  afpLock,        
e27b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f            /* xLo
e27c0 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 61  ck method */.  a
e27d0 66 70 55 6e 6c 6f 63 6b 2c 20 20 20 20 20 20 20  fpUnlock,       
e27e0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c           /* xUnl
e27f0 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  ock method */.  
e2800 61 66 70 43 68 65 63 6b 52 65 73 65 72 76 65 64  afpCheckReserved
e2810 4c 6f 63 6b 20 20 20 20 20 20 2f 2a 20 78 43 68  Lock      /* xCh
e2820 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20  eckReservedLock 
e2830 6d 65 74 68 6f 64 20 2a 2f 0a 29 0a 23 65 6e 64  method */.).#end
e2840 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 57  if../*.** The "W
e2850 68 6f 6c 65 20 46 69 6c 65 20 4c 6f 63 6b 69 6e  hole File Lockin
e2860 67 22 20 66 69 6e 64 65 72 20 72 65 74 75 72 6e  g" finder return
e2870 73 20 74 68 65 20 73 61 6d 65 20 73 65 74 20 6f  s the same set o
e2880 66 20 6d 65 74 68 6f 64 73 20 61 73 0a 2a 2a 20  f methods as.** 
e2890 74 68 65 20 70 6f 73 69 78 20 6c 6f 63 6b 69 6e  the posix lockin
e28a0 67 20 66 69 6e 64 65 72 2e 20 20 42 75 74 20 69  g finder.  But i
e28b0 74 20 61 6c 73 6f 20 73 65 74 73 20 74 68 65 20  t also sets the 
e28c0 53 51 4c 49 54 45 5f 57 48 4f 4c 45 5f 46 49 4c  SQLITE_WHOLE_FIL
e28d0 45 5f 4c 4f 43 4b 49 4e 47 0a 2a 2a 20 66 6c 61  E_LOCKING.** fla
e28e0 67 20 74 6f 20 66 6f 72 63 65 20 74 68 65 20 70  g to force the p
e28f0 6f 73 69 78 20 61 64 76 69 73 6f 72 79 20 6c 6f  osix advisory lo
e2900 63 6b 73 20 74 6f 20 63 6f 76 65 72 20 74 68 65  cks to cover the
e2910 20 77 68 6f 6c 65 20 66 69 6c 65 20 69 6e 73 74   whole file inst
e2920 65 61 64 0a 2a 2a 20 6f 66 20 6a 75 73 74 20 61  ead.** of just a
e2930 20 73 6d 61 6c 6c 20 73 70 61 6e 20 6f 66 20 62   small span of b
e2940 79 74 65 73 20 6e 65 61 72 20 74 68 65 20 31 47  ytes near the 1G
e2950 69 42 20 62 6f 75 6e 64 61 72 79 2e 20 20 57 68  iB boundary.  Wh
e2960 6f 6c 65 20 46 69 6c 65 20 4c 6f 63 6b 69 6e 67  ole File Locking
e2970 0a 2a 2a 20 69 73 20 75 73 65 66 75 6c 20 6f 6e  .** is useful on
e2980 20 4e 46 53 2d 6d 6f 75 6e 74 65 64 20 66 69 6c   NFS-mounted fil
e2990 65 73 20 73 69 6e 63 65 20 69 74 20 68 65 6c 70  es since it help
e29a0 73 20 4e 46 53 20 74 6f 20 6d 61 69 6e 74 61 69  s NFS to maintai
e29b0 6e 20 63 61 63 68 65 0a 2a 2a 20 63 6f 68 65 72  n cache.** coher
e29c0 65 6e 63 79 2e 20 20 42 75 74 20 69 74 20 69 73  ency.  But it is
e29d0 20 61 20 64 65 74 72 69 6d 65 6e 74 20 74 6f 20   a detriment to 
e29e0 6f 74 68 65 72 20 66 69 6c 65 73 79 73 74 65 6d  other filesystem
e29f0 73 20 73 69 6e 63 65 20 69 74 20 72 75 6e 73 0a  s since it runs.
e2a00 2a 2a 20 73 6c 6f 77 65 72 2e 0a 2a 2f 0a 73 74  ** slower..*/.st
e2a10 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
e2a20 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70  e3_io_methods *p
e2a30 6f 73 69 78 57 66 6c 49 6f 46 69 6e 64 65 72 49  osixWflIoFinderI
e2a40 6d 70 6c 28 63 6f 6e 73 74 20 63 68 61 72 2a 7a  mpl(const char*z
e2a50 2c 20 75 6e 69 78 46 69 6c 65 2a 70 29 7b 0a 20  , unixFile*p){. 
e2a60 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
e2a70 52 28 7a 29 3b 0a 20 20 70 2d 3e 66 69 6c 65 46  R(z);.  p->fileF
e2a80 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 57 48  lags = SQLITE_WH
e2a90 4f 4c 45 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47  OLE_FILE_LOCKING
e2aa0 3b 0a 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69  ;.  return &posi
e2ab0 78 49 6f 4d 65 74 68 6f 64 73 3b 0a 7d 0a 73 74  xIoMethods;.}.st
e2ac0 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
e2ad0 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 0a 20  e3_io_methods . 
e2ae0 20 2a 28 2a 63 6f 6e 73 74 20 70 6f 73 69 78 57   *(*const posixW
e2af0 66 6c 49 6f 46 69 6e 64 65 72 29 28 63 6f 6e 73  flIoFinder)(cons
e2b00 74 20 63 68 61 72 2a 2c 75 6e 69 78 46 69 6c 65  t char*,unixFile
e2b10 20 2a 70 29 20 3d 20 70 6f 73 69 78 57 66 6c 49   *p) = posixWflI
e2b20 6f 46 69 6e 64 65 72 49 6d 70 6c 3b 0a 0a 2f 2a  oFinderImpl;../*
e2b30 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 6c 6f  .** The proxy lo
e2b40 63 6b 69 6e 67 20 6d 65 74 68 6f 64 20 69 73 20  cking method is 
e2b50 61 20 22 73 75 70 65 72 2d 6d 65 74 68 6f 64 22  a "super-method"
e2b60 20 69 6e 20 74 68 65 20 73 65 6e 73 65 20 74 68   in the sense th
e2b70 61 74 20 69 74 0a 2a 2a 20 6f 70 65 6e 73 20 73  at it.** opens s
e2b80 65 63 6f 6e 64 61 72 79 20 66 69 6c 65 20 64 65  econdary file de
e2b90 73 63 72 69 70 74 6f 72 73 20 66 6f 72 20 74 68  scriptors for th
e2ba0 65 20 63 6f 6e 63 68 20 61 6e 64 20 6c 6f 63 6b  e conch and lock
e2bb0 20 66 69 6c 65 73 20 61 6e 64 0a 2a 2a 20 69 74   files and.** it
e2bc0 20 75 73 65 73 20 70 72 6f 78 79 2c 20 64 6f 74   uses proxy, dot
e2bd0 2d 66 69 6c 65 2c 20 41 46 50 2c 20 61 6e 64 20  -file, AFP, and 
e2be0 66 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 20  flock() locking 
e2bf0 6d 65 74 68 6f 64 73 20 6f 6e 20 74 68 6f 73 65  methods on those
e2c00 0a 2a 2a 20 73 65 63 6f 6e 64 61 72 79 20 66 69  .** secondary fi
e2c10 6c 65 73 2e 20 20 46 6f 72 20 74 68 69 73 20 72  les.  For this r
e2c20 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 76 69 73  eason, the divis
e2c30 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ion that impleme
e2c40 6e 74 73 0a 2a 2a 20 70 72 6f 78 79 20 6c 6f 63  nts.** proxy loc
e2c50 6b 69 6e 67 20 69 73 20 6c 6f 63 61 74 65 64 20  king is located 
e2c60 6d 75 63 68 20 66 75 72 74 68 65 72 20 64 6f 77  much further dow
e2c70 6e 20 69 6e 20 74 68 65 20 66 69 6c 65 2e 20 20  n in the file.  
e2c80 42 75 74 20 77 65 20 6e 65 65 64 0a 2a 2a 20 74  But we need.** t
e2c90 6f 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 64  o go ahead and d
e2ca0 65 66 69 6e 65 20 74 68 65 20 73 71 6c 69 74 65  efine the sqlite
e2cb0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 61 6e 64  3_io_methods and
e2cc0 20 66 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e   finder function
e2cd0 0a 2a 2a 20 66 6f 72 20 70 72 6f 78 79 20 6c 6f  .** for proxy lo
e2ce0 63 6b 69 6e 67 20 68 65 72 65 2e 20 20 53 6f 20  cking here.  So 
e2cf0 77 65 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61  we forward decla
e2d00 72 65 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f  re the I/O metho
e2d10 64 73 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  ds..*/.#if defin
e2d20 65 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26  ed(__APPLE__) &&
e2d30 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c   SQLITE_ENABLE_L
e2d40 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 73 74 61  OCKING_STYLE.sta
e2d50 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f  tic int proxyClo
e2d60 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  se(sqlite3_file*
e2d70 29 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72  );.static int pr
e2d80 6f 78 79 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f  oxyLock(sqlite3_
e2d90 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 73 74 61  file*, int);.sta
e2da0 74 69 63 20 69 6e 74 20 70 72 6f 78 79 55 6e 6c  tic int proxyUnl
e2db0 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
e2dc0 2a 2c 20 69 6e 74 29 3b 0a 73 74 61 74 69 63 20  *, int);.static 
e2dd0 69 6e 74 20 70 72 6f 78 79 43 68 65 63 6b 52 65  int proxyCheckRe
e2de0 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74  servedLock(sqlit
e2df0 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 2a 29 3b  e3_file*, int*);
e2e00 0a 49 4f 4d 45 54 48 4f 44 53 28 0a 20 20 70 72  .IOMETHODS(.  pr
e2e10 6f 78 79 49 6f 46 69 6e 64 65 72 2c 20 20 20 20  oxyIoFinder,    
e2e20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6e 64 65          /* Finde
e2e30 72 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20  r function name 
e2e40 2a 2f 0a 20 20 70 72 6f 78 79 49 6f 4d 65 74 68  */.  proxyIoMeth
e2e50 6f 64 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f  ods,           /
e2e60 2a 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  * sqlite3_io_met
e2e70 68 6f 64 73 20 6f 62 6a 65 63 74 20 6e 61 6d 65  hods object name
e2e80 20 2a 2f 0a 20 20 70 72 6f 78 79 43 6c 6f 73 65   */.  proxyClose
e2e90 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e2ea0 2f 2a 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64  /* xClose method
e2eb0 20 2a 2f 0a 20 20 70 72 6f 78 79 4c 6f 63 6b 2c   */.  proxyLock,
e2ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2ed0 2f 2a 20 78 4c 6f 63 6b 20 6d 65 74 68 6f 64 20  /* xLock method 
e2ee0 2a 2f 0a 20 20 70 72 6f 78 79 55 6e 6c 6f 63 6b  */.  proxyUnlock
e2ef0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
e2f00 2a 20 78 55 6e 6c 6f 63 6b 20 6d 65 74 68 6f 64  * xUnlock method
e2f10 20 2a 2f 0a 20 20 70 72 6f 78 79 43 68 65 63 6b   */.  proxyCheck
e2f20 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 20 20 20  ReservedLock    
e2f30 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65  /* xCheckReserve
e2f40 64 4c 6f 63 6b 20 6d 65 74 68 6f 64 20 2a 2f 0a  dLock method */.
e2f50 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 20 64  ).#endif...#if d
e2f60 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f 5f  efined(__APPLE__
e2f70 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42  ) && SQLITE_ENAB
e2f80 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
e2f90 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 22 66 69  ./* .** This "fi
e2fa0 6e 64 65 72 22 20 66 75 6e 63 74 69 6f 6e 20 61  nder" function a
e2fb0 74 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72  ttempts to deter
e2fc0 6d 69 6e 65 20 74 68 65 20 62 65 73 74 20 6c 6f  mine the best lo
e2fd0 63 6b 69 6e 67 20 73 74 72 61 74 65 67 79 20 0a  cking strategy .
e2fe0 2a 2a 20 66 6f 72 20 74 68 65 20 64 61 74 61 62  ** for the datab
e2ff0 61 73 65 20 66 69 6c 65 20 22 66 69 6c 65 50 61  ase file "filePa
e3000 74 68 22 2e 20 20 49 74 20 74 68 65 6e 20 72 65  th".  It then re
e3010 74 75 72 6e 73 20 74 68 65 20 73 71 6c 69 74 65  turns the sqlite
e3020 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20  3_io_methods.** 
e3030 6f 62 6a 65 63 74 20 74 68 61 74 20 69 6d 70 6c  object that impl
e3040 65 6d 65 6e 74 73 20 74 68 61 74 20 73 74 72 61  ements that stra
e3050 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tegy..**.** This
e3060 20 69 73 20 66 6f 72 20 4d 61 63 4f 53 58 20 6f   is for MacOSX o
e3070 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  nly..*/.static c
e3080 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f  onst sqlite3_io_
e3090 6d 65 74 68 6f 64 73 20 2a 61 75 74 6f 6c 6f 63  methods *autoloc
e30a0 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c 28 0a 20  kIoFinderImpl(. 
e30b0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c   const char *fil
e30c0 65 50 61 74 68 2c 20 20 20 20 2f 2a 20 6e 61 6d  ePath,    /* nam
e30d0 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
e30e0 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 69 78  e file */.  unix
e30f0 46 69 6c 65 20 2a 70 4e 65 77 20 20 20 20 20 20  File *pNew      
e3100 20 20 20 20 20 2f 2a 20 6f 70 65 6e 20 66 69 6c       /* open fil
e3110 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65  e object for the
e3120 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
e3130 2f 0a 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f  /.){.  static co
e3140 6e 73 74 20 73 74 72 75 63 74 20 4d 61 70 70 69  nst struct Mappi
e3150 6e 67 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ng {.    const c
e3160 68 61 72 20 2a 7a 46 69 6c 65 73 79 73 74 65 6d  har *zFilesystem
e3170 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
e3180 2a 20 46 69 6c 65 73 79 73 74 65 6d 20 74 79 70  * Filesystem typ
e3190 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 63 6f  e name */.    co
e31a0 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  nst sqlite3_io_m
e31b0 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64 73  ethods *pMethods
e31c0 3b 20 20 20 2f 2a 20 41 70 70 72 6f 70 72 69 61  ;   /* Appropria
e31d0 74 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68 6f  te locking metho
e31e0 64 20 2a 2f 0a 20 20 7d 20 61 4d 61 70 5b 5d 20  d */.  } aMap[] 
e31f0 3d 20 7b 0a 20 20 20 20 7b 20 22 68 66 73 22 2c  = {.    { "hfs",
e3200 20 20 20 20 26 70 6f 73 69 78 49 6f 4d 65 74 68      &posixIoMeth
e3210 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20 22 75 66  ods },.    { "uf
e3220 73 22 2c 20 20 20 20 26 70 6f 73 69 78 49 6f 4d  s",    &posixIoM
e3230 65 74 68 6f 64 73 20 7d 2c 0a 20 20 20 20 7b 20  ethods },.    { 
e3240 22 61 66 70 66 73 22 2c 20 20 26 61 66 70 49 6f  "afpfs",  &afpIo
e3250 4d 65 74 68 6f 64 73 20 7d 2c 0a 23 69 66 64 65  Methods },.#ifde
e3260 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
e3270 41 46 50 5f 4c 4f 43 4b 49 4e 47 5f 53 4d 42 0a  AFP_LOCKING_SMB.
e3280 20 20 20 20 7b 20 22 73 6d 62 66 73 22 2c 20 20      { "smbfs",  
e3290 26 61 66 70 49 6f 4d 65 74 68 6f 64 73 20 7d 2c  &afpIoMethods },
e32a0 0a 23 65 6c 73 65 0a 20 20 20 20 7b 20 22 73 6d  .#else.    { "sm
e32b0 62 66 73 22 2c 20 20 26 66 6c 6f 63 6b 49 6f 4d  bfs",  &flockIoM
e32c0 65 74 68 6f 64 73 20 7d 2c 0a 23 65 6e 64 69 66  ethods },.#endif
e32d0 0a 20 20 20 20 7b 20 22 77 65 62 64 61 76 22 2c  .    { "webdav",
e32e0 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64   &nolockIoMethod
e32f0 73 20 7d 2c 0a 20 20 20 20 7b 20 30 2c 20 30 20  s },.    { 0, 0 
e3300 7d 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a  }.  };.  int i;.
e3310 20 20 73 74 72 75 63 74 20 73 74 61 74 66 73 20    struct statfs 
e3320 66 73 49 6e 66 6f 3b 0a 20 20 73 74 72 75 63 74  fsInfo;.  struct
e3330 20 66 6c 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b   flock lockInfo;
e3340 0a 0a 20 20 69 66 28 20 21 66 69 6c 65 50 61 74  ..  if( !filePat
e3350 68 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 66  h ){.    /* If f
e3360 69 6c 65 50 61 74 68 3d 3d 4e 55 4c 4c 20 74 68  ilePath==NULL th
e3370 61 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20  at means we are 
e3380 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20 74  dealing with a t
e3390 72 61 6e 73 69 65 6e 74 20 66 69 6c 65 0a 20 20  ransient file.  
e33a0 20 20 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e    ** that does n
e33b0 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f  ot need to be lo
e33c0 63 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 72 65 74  cked. */.    ret
e33d0 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74  urn &nolockIoMet
e33e0 68 6f 64 73 3b 0a 20 20 7d 0a 20 20 69 66 28 20  hods;.  }.  if( 
e33f0 73 74 61 74 66 73 28 66 69 6c 65 50 61 74 68 2c  statfs(filePath,
e3400 20 26 66 73 49 6e 66 6f 29 20 21 3d 20 2d 31 20   &fsInfo) != -1 
e3410 29 7b 0a 20 20 20 20 69 66 28 20 66 73 49 6e 66  ){.    if( fsInf
e3420 6f 2e 66 5f 66 6c 61 67 73 20 26 20 4d 4e 54 5f  o.f_flags & MNT_
e3430 52 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20 20  RDONLY ){.      
e3440 72 65 74 75 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f  return &nolockIo
e3450 4d 65 74 68 6f 64 73 3b 0a 20 20 20 20 7d 0a 20  Methods;.    }. 
e3460 20 20 20 66 6f 72 28 69 3d 30 3b 20 61 4d 61 70     for(i=0; aMap
e3470 5b 69 5d 2e 7a 46 69 6c 65 73 79 73 74 65 6d 3b  [i].zFilesystem;
e3480 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
e3490 20 73 74 72 63 6d 70 28 66 73 49 6e 66 6f 2e 66   strcmp(fsInfo.f
e34a0 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20 61 4d 61  _fstypename, aMa
e34b0 70 5b 69 5d 2e 7a 46 69 6c 65 73 79 73 74 65 6d  p[i].zFilesystem
e34c0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
e34d0 72 65 74 75 72 6e 20 61 4d 61 70 5b 69 5d 2e 70  return aMap[i].p
e34e0 4d 65 74 68 6f 64 73 3b 0a 20 20 20 20 20 20 7d  Methods;.      }
e34f0 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
e3500 20 44 65 66 61 75 6c 74 20 63 61 73 65 2e 20 48   Default case. H
e3510 61 6e 64 6c 65 73 2c 20 61 6d 6f 6e 67 73 74 20  andles, amongst 
e3520 6f 74 68 65 72 73 2c 20 22 6e 66 73 22 2e 0a 20  others, "nfs".. 
e3530 20 2a 2a 20 54 65 73 74 20 62 79 74 65 2d 72 61   ** Test byte-ra
e3540 6e 67 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 66  nge lock using f
e3550 63 6e 74 6c 28 29 2e 20 49 66 20 74 68 65 20 63  cntl(). If the c
e3560 61 6c 6c 20 73 75 63 63 65 65 64 73 2c 20 0a 20  all succeeds, . 
e3570 20 2a 2a 20 61 73 73 75 6d 65 20 74 68 61 74 20   ** assume that 
e3580 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  the file-system 
e3590 73 75 70 70 6f 72 74 73 20 50 4f 53 49 58 20 73  supports POSIX s
e35a0 74 79 6c 65 20 6c 6f 63 6b 73 2e 20 0a 20 20 2a  tyle locks. .  *
e35b0 2f 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c  /.  lockInfo.l_l
e35c0 65 6e 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b 49 6e  en = 1;.  lockIn
e35d0 66 6f 2e 6c 5f 73 74 61 72 74 20 3d 20 30 3b 0a  fo.l_start = 0;.
e35e0 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77 68 65    lockInfo.l_whe
e35f0 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b 0a  nce = SEEK_SET;.
e3600 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74 79 70    lockInfo.l_typ
e3610 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 69  e = F_RDLCK;.  i
e3620 66 28 20 66 63 6e 74 6c 28 70 4e 65 77 2d 3e 68  f( fcntl(pNew->h
e3630 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f 63 6b  , F_GETLK, &lock
e3640 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a 20 20  Info)!=-1 ) {.  
e3650 20 20 70 4e 65 77 2d 3e 66 69 6c 65 46 6c 61 67    pNew->fileFlag
e3660 73 20 3d 20 53 51 4c 49 54 45 5f 57 48 4f 4c 45  s = SQLITE_WHOLE
e3670 5f 46 49 4c 45 5f 4c 4f 43 4b 49 4e 47 3b 0a 20  _FILE_LOCKING;. 
e3680 20 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69 78     return &posix
e3690 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65 6c  IoMethods;.  }el
e36a0 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 26  se{.    return &
e36b0 64 6f 74 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73  dotlockIoMethods
e36c0 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63  ;.  }.}.static c
e36d0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f  onst sqlite3_io_
e36e0 6d 65 74 68 6f 64 73 20 0a 20 20 2a 28 2a 63 6f  methods .  *(*co
e36f0 6e 73 74 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69  nst autolockIoFi
e3700 6e 64 65 72 29 28 63 6f 6e 73 74 20 63 68 61 72  nder)(const char
e3710 2a 2c 75 6e 69 78 46 69 6c 65 2a 29 20 3d 20 61  *,unixFile*) = a
e3720 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49  utolockIoFinderI
e3730 6d 70 6c 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  mpl;..#endif /* 
e3740 64 65 66 69 6e 65 64 28 5f 5f 41 50 50 4c 45 5f  defined(__APPLE_
e3750 5f 29 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41  _) && SQLITE_ENA
e3760 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
e3770 45 20 2a 2f 0a 0a 23 69 66 20 4f 53 5f 56 58 57  E */..#if OS_VXW
e3780 4f 52 4b 53 20 26 26 20 53 51 4c 49 54 45 5f 45  ORKS && SQLITE_E
e3790 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
e37a0 59 4c 45 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20  YLE./* .** This 
e37b0 22 66 69 6e 64 65 72 22 20 66 75 6e 63 74 69 6f  "finder" functio
e37c0 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 64 65  n attempts to de
e37d0 74 65 72 6d 69 6e 65 20 74 68 65 20 62 65 73 74  termine the best
e37e0 20 6c 6f 63 6b 69 6e 67 20 73 74 72 61 74 65 67   locking strateg
e37f0 79 20 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 61  y .** for the da
e3800 74 61 62 61 73 65 20 66 69 6c 65 20 22 66 69 6c  tabase file "fil
e3810 65 50 61 74 68 22 2e 20 20 49 74 20 74 68 65 6e  ePath".  It then
e3820 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 71 6c   returns the sql
e3830 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a  ite3_io_methods.
e3840 2a 2a 20 6f 62 6a 65 63 74 20 74 68 61 74 20 69  ** object that i
e3850 6d 70 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 73  mplements that s
e3860 74 72 61 74 65 67 79 2e 0a 2a 2a 0a 2a 2a 20 54  trategy..**.** T
e3870 68 69 73 20 69 73 20 66 6f 72 20 56 58 57 6f 72  his is for VXWor
e3880 6b 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74  ks only..*/.stat
e3890 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
e38a0 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 61 75 74  _io_methods *aut
e38b0 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70  olockIoFinderImp
e38c0 6c 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  l(.  const char 
e38d0 2a 66 69 6c 65 50 61 74 68 2c 20 20 20 20 2f 2a  *filePath,    /*
e38e0 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   name of the dat
e38f0 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20  abase file */.  
e3900 75 6e 69 78 46 69 6c 65 20 2a 70 4e 65 77 20 20  unixFile *pNew  
e3910 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20           /* the 
e3920 6f 70 65 6e 20 66 69 6c 65 20 6f 62 6a 65 63 74  open file object
e3930 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20   */.){.  struct 
e3940 66 6c 6f 63 6b 20 6c 6f 63 6b 49 6e 66 6f 3b 0a  flock lockInfo;.
e3950 0a 20 20 69 66 28 20 21 66 69 6c 65 50 61 74 68  .  if( !filePath
e3960 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 66 69   ){.    /* If fi
e3970 6c 65 50 61 74 68 3d 3d 4e 55 4c 4c 20 74 68 61  lePath==NULL tha
e3980 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20 64  t means we are d
e3990 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20 74 72  ealing with a tr
e39a0 61 6e 73 69 65 6e 74 20 66 69 6c 65 0a 20 20 20  ansient file.   
e39b0 20 2a 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f   ** that does no
e39c0 74 20 6e 65 65 64 20 74 6f 20 62 65 20 6c 6f 63  t need to be loc
e39d0 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75  ked. */.    retu
e39e0 72 6e 20 26 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68  rn &nolockIoMeth
e39f0 6f 64 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54  ods;.  }..  /* T
e3a00 65 73 74 20 69 66 20 66 63 6e 74 6c 28 29 20 69  est if fcntl() i
e3a10 73 20 73 75 70 70 6f 72 74 65 64 20 61 6e 64 20  s supported and 
e3a20 75 73 65 20 50 4f 53 49 58 20 73 74 79 6c 65 20  use POSIX style 
e3a30 6c 6f 63 6b 73 2e 0a 20 20 2a 2a 20 4f 74 68 65  locks..  ** Othe
e3a40 72 77 69 73 65 20 66 61 6c 6c 20 62 61 63 6b 20  rwise fall back 
e3a50 74 6f 20 74 68 65 20 6e 61 6d 65 64 20 73 65 6d  to the named sem
e3a60 61 70 68 6f 72 65 20 6d 65 74 68 6f 64 2e 0a 20  aphore method.. 
e3a70 20 2a 2f 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c   */.  lockInfo.l
e3a80 5f 6c 65 6e 20 3d 20 31 3b 0a 20 20 6c 6f 63 6b  _len = 1;.  lock
e3a90 49 6e 66 6f 2e 6c 5f 73 74 61 72 74 20 3d 20 30  Info.l_start = 0
e3aa0 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 77  ;.  lockInfo.l_w
e3ab0 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54  hence = SEEK_SET
e3ac0 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f 74  ;.  lockInfo.l_t
e3ad0 79 70 65 20 3d 20 46 5f 52 44 4c 43 4b 3b 0a 20  ype = F_RDLCK;. 
e3ae0 20 69 66 28 20 66 63 6e 74 6c 28 70 4e 65 77 2d   if( fcntl(pNew-
e3af0 3e 68 2c 20 46 5f 47 45 54 4c 4b 2c 20 26 6c 6f  >h, F_GETLK, &lo
e3b00 63 6b 49 6e 66 6f 29 21 3d 2d 31 20 29 20 7b 0a  ckInfo)!=-1 ) {.
e3b10 20 20 20 20 72 65 74 75 72 6e 20 26 70 6f 73 69      return &posi
e3b20 78 49 6f 4d 65 74 68 6f 64 73 3b 0a 20 20 7d 65  xIoMethods;.  }e
e3b30 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
e3b40 26 73 65 6d 49 6f 4d 65 74 68 6f 64 73 3b 0a 20  &semIoMethods;. 
e3b50 20 7d 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73   }.}.static cons
e3b60 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74  t sqlite3_io_met
e3b70 68 6f 64 73 20 0a 20 20 2a 28 2a 63 6f 6e 73 74  hods .  *(*const
e3b80 20 61 75 74 6f 6c 6f 63 6b 49 6f 46 69 6e 64 65   autolockIoFinde
e3b90 72 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 75  r)(const char*,u
e3ba0 6e 69 78 46 69 6c 65 2a 29 20 3d 20 61 75 74 6f  nixFile*) = auto
e3bb0 6c 6f 63 6b 49 6f 46 69 6e 64 65 72 49 6d 70 6c  lockIoFinderImpl
e3bc0 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 4f 53 5f  ;..#endif /* OS_
e3bd0 56 58 57 4f 52 4b 53 20 26 26 20 53 51 4c 49 54  VXWORKS && SQLIT
e3be0 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
e3bf0 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  _STYLE */../*.**
e3c00 20 41 6e 20 61 62 73 74 72 61 63 74 20 74 79 70   An abstract typ
e3c10 65 20 66 6f 72 20 61 20 70 6f 69 6e 74 65 72 20  e for a pointer 
e3c20 74 6f 20 61 20 49 4f 20 6d 65 74 68 6f 64 20 66  to a IO method f
e3c30 69 6e 64 65 72 20 66 75 6e 63 74 69 6f 6e 3a 0a  inder function:.
e3c40 2a 2f 0a 74 79 70 65 64 65 66 20 63 6f 6e 73 74  */.typedef const
e3c50 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
e3c60 6f 64 73 20 2a 28 2a 66 69 6e 64 65 72 5f 74 79  ods *(*finder_ty
e3c70 70 65 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  pe)(const char*,
e3c80 75 6e 69 78 46 69 6c 65 2a 29 3b 0a 0a 0a 2f 2a  unixFile*);.../*
e3c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3cb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a  ***********.****
e3ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3cf0 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33  ******** sqlite3
e3d00 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 2a 2a 2a  _vfs methods ***
e3d10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
e3d30 54 68 69 73 20 64 69 76 69 73 69 6f 6e 20 63 6f  This division co
e3d40 6e 74 61 69 6e 73 20 74 68 65 20 69 6d 70 6c 65  ntains the imple
e3d50 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 65 74  mentation of met
e3d60 68 6f 64 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 73  hods on the.** s
e3d70 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65 63  qlite3_vfs objec
e3d80 74 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69  t..*/../*.** Ini
e3d90 74 69 61 6c 69 7a 65 20 74 68 65 20 63 6f 6e 74  tialize the cont
e3da0 65 6e 74 73 20 6f 66 20 74 68 65 20 75 6e 69 78  ents of the unix
e3db0 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 70  File structure p
e3dc0 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 70 49 64  ointed to by pId
e3dd0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
e3de0 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c 65 28 0a  fillInUnixFile(.
e3df0 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
e3e00 56 66 73 2c 20 20 20 20 20 20 2f 2a 20 50 6f 69  Vfs,      /* Poi
e3e10 6e 74 65 72 20 74 6f 20 76 66 73 20 6f 62 6a 65  nter to vfs obje
e3e20 63 74 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20 20  ct */.  int h,  
e3e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e3e40 2f 2a 20 4f 70 65 6e 20 66 69 6c 65 20 64 65 73  /* Open file des
e3e50 63 72 69 70 74 6f 72 20 6f 66 20 66 69 6c 65 20  criptor of file 
e3e60 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a  being opened */.
e3e70 20 20 69 6e 74 20 64 69 72 66 64 2c 20 20 20 20    int dirfd,    
e3e80 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72            /* Dir
e3e90 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65 73 63  ectory file desc
e3ea0 72 69 70 74 6f 72 20 2a 2f 0a 20 20 73 71 6c 69  riptor */.  sqli
e3eb0 74 65 33 5f 66 69 6c 65 20 2a 70 49 64 2c 20 20  te3_file *pId,  
e3ec0 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 6f 20      /* Write to 
e3ed0 74 68 65 20 75 6e 69 78 46 69 6c 65 20 73 74 72  the unixFile str
e3ee0 75 63 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20  ucture here */. 
e3ef0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
e3f00 6c 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65  lename,  /* Name
e3f10 20 6f 66 20 74 68 65 20 66 69 6c 65 20 62 65 69   of the file bei
e3f20 6e 67 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 69  ng opened */.  i
e3f30 6e 74 20 6e 6f 4c 6f 63 6b 2c 20 20 20 20 20 20  nt noLock,      
e3f40 20 20 20 20 20 20 20 2f 2a 20 4f 6d 69 74 20 6c         /* Omit l
e3f50 6f 63 6b 69 6e 67 20 69 66 20 74 72 75 65 20 2a  ocking if true *
e3f60 2f 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 74 65  /.  int isDelete
e3f70 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
e3f80 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65 20 69  elete on close i
e3f90 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 63  f true */.){.  c
e3fa0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f  onst sqlite3_io_
e3fb0 6d 65 74 68 6f 64 73 20 2a 70 4c 6f 63 6b 69 6e  methods *pLockin
e3fc0 67 53 74 79 6c 65 3b 0a 20 20 75 6e 69 78 46 69  gStyle;.  unixFi
e3fd0 6c 65 20 2a 70 4e 65 77 20 3d 20 28 75 6e 69 78  le *pNew = (unix
e3fe0 46 69 6c 65 20 2a 29 70 49 64 3b 0a 20 20 69 6e  File *)pId;.  in
e3ff0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
e4000 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65  ;..  assert( pNe
e4010 77 2d 3e 70 4c 6f 63 6b 3d 3d 4e 55 4c 4c 20 29  w->pLock==NULL )
e4020 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 65 77  ;.  assert( pNew
e4030 2d 3e 70 4f 70 65 6e 3d 3d 4e 55 4c 4c 20 29 3b  ->pOpen==NULL );
e4040 0a 0a 20 20 2f 2a 20 50 61 72 61 6d 65 74 65 72  ..  /* Parameter
e4050 20 69 73 44 65 6c 65 74 65 20 69 73 20 6f 6e 6c   isDelete is onl
e4060 79 20 75 73 65 64 20 6f 6e 20 76 78 77 6f 72 6b  y used on vxwork
e4070 73 2e 20 45 78 70 72 65 73 73 20 74 68 69 73 20  s. Express this 
e4080 65 78 70 6c 69 63 69 74 6c 79 20 0a 20 20 2a 2a  explicitly .  **
e4090 20 68 65 72 65 20 74 6f 20 70 72 65 76 65 6e 74   here to prevent
e40a0 20 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e   compiler warnin
e40b0 67 73 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20  gs about unused 
e40c0 70 61 72 61 6d 65 74 65 72 73 2e 0a 20 20 2a 2f  parameters..  */
e40d0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
e40e0 54 45 52 28 69 73 44 65 6c 65 74 65 29 3b 0a 0a  TER(isDelete);..
e40f0 20 20 4f 53 54 52 41 43 45 33 28 22 4f 50 45 4e    OSTRACE3("OPEN
e4100 20 20 20 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20      %-3d %s\n", 
e4110 68 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 20 20  h, zFilename);  
e4120 20 20 0a 20 20 70 4e 65 77 2d 3e 68 20 3d 20 68    .  pNew->h = h
e4130 3b 0a 20 20 70 4e 65 77 2d 3e 64 69 72 66 64 20  ;.  pNew->dirfd 
e4140 3d 20 64 69 72 66 64 3b 0a 20 20 53 45 54 5f 54  = dirfd;.  SET_T
e4150 48 52 45 41 44 49 44 28 70 4e 65 77 29 3b 0a 20  HREADID(pNew);. 
e4160 20 70 4e 65 77 2d 3e 66 69 6c 65 46 6c 61 67 73   pNew->fileFlags
e4170 20 3d 20 30 3b 0a 0a 23 69 66 20 4f 53 5f 56 58   = 0;..#if OS_VX
e4180 57 4f 52 4b 53 0a 20 20 70 4e 65 77 2d 3e 70 49  WORKS.  pNew->pI
e4190 64 20 3d 20 76 78 77 6f 72 6b 73 46 69 6e 64 46  d = vxworksFindF
e41a0 69 6c 65 49 64 28 7a 46 69 6c 65 6e 61 6d 65 29  ileId(zFilename)
e41b0 3b 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 49  ;.  if( pNew->pI
e41c0 64 3d 3d 30 20 29 7b 0a 20 20 20 20 6e 6f 4c 6f  d==0 ){.    noLo
e41d0 63 6b 20 3d 20 31 3b 0a 20 20 20 20 72 63 20 3d  ck = 1;.    rc =
e41e0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
e41f0 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28   }.#endif..  if(
e4200 20 6e 6f 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70   noLock ){.    p
e4210 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20 26  LockingStyle = &
e4220 6e 6f 6c 6f 63 6b 49 6f 4d 65 74 68 6f 64 73 3b  nolockIoMethods;
e4230 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4c  .  }else{.    pL
e4240 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20 28 2a  ockingStyle = (*
e4250 2a 28 66 69 6e 64 65 72 5f 74 79 70 65 2a 29 70  *(finder_type*)p
e4260 56 66 73 2d 3e 70 41 70 70 44 61 74 61 29 28 7a  Vfs->pAppData)(z
e4270 46 69 6c 65 6e 61 6d 65 2c 20 70 4e 65 77 29 3b  Filename, pNew);
e4280 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  .#if SQLITE_ENAB
e4290 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
e42a0 0a 20 20 20 20 2f 2a 20 43 61 63 68 65 20 7a 46  .    /* Cache zF
e42b0 69 6c 65 6e 61 6d 65 20 69 6e 20 74 68 65 20 6c  ilename in the l
e42c0 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20 28  ocking context (
e42d0 41 46 50 20 61 6e 64 20 64 6f 74 6c 6f 63 6b 20  AFP and dotlock 
e42e0 6f 76 65 72 72 69 64 65 29 20 66 6f 72 0a 20 20  override) for.  
e42f0 20 20 2a 2a 20 70 72 6f 78 79 4c 6f 63 6b 20 61    ** proxyLock a
e4300 63 74 69 76 61 74 69 6f 6e 20 69 73 20 70 6f 73  ctivation is pos
e4310 73 69 62 6c 65 20 28 72 65 6d 6f 74 65 20 70 72  sible (remote pr
e4320 6f 78 79 20 69 73 20 62 61 73 65 64 20 6f 6e 20  oxy is based on 
e4330 64 62 20 6e 61 6d 65 29 0a 20 20 20 20 2a 2a 20  db name).    ** 
e4340 7a 46 69 6c 65 6e 61 6d 65 20 72 65 6d 61 69 6e  zFilename remain
e4350 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 66 69  s valid until fi
e4360 6c 65 20 69 73 20 63 6c 6f 73 65 64 2c 20 74 6f  le is closed, to
e4370 20 73 75 70 70 6f 72 74 20 2a 2f 0a 20 20 20 20   support */.    
e4380 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  pNew->lockingCon
e4390 74 65 78 74 20 3d 20 28 76 6f 69 64 2a 29 7a 46  text = (void*)zF
e43a0 69 6c 65 6e 61 6d 65 3b 0a 23 65 6e 64 69 66 0a  ilename;.#endif.
e43b0 20 20 7d 0a 0a 20 20 69 66 28 20 70 4c 6f 63 6b    }..  if( pLock
e43c0 69 6e 67 53 74 79 6c 65 20 3d 3d 20 26 70 6f 73  ingStyle == &pos
e43d0 69 78 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20  ixIoMethods ){. 
e43e0 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65     unixEnterMute
e43f0 78 28 29 3b 0a 20 20 20 20 72 63 20 3d 20 66 69  x();.    rc = fi
e4400 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c  ndLockInfo(pNew,
e4410 20 26 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26   &pNew->pLock, &
e4420 70 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20  pNew->pOpen);.  
e4430 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
e4440 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  _OK ){.      /* 
e4450 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
e4460 72 65 64 20 69 6e 20 66 69 6e 64 4c 6f 63 6b 49  red in findLockI
e4470 6e 66 6f 28 29 2c 20 63 6c 6f 73 65 20 74 68 65  nfo(), close the
e4480 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
e4490 0a 20 20 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69  .      ** immedi
e44a0 61 74 65 6c 79 2c 20 62 65 66 6f 72 65 20 72 65  ately, before re
e44b0 6c 65 61 73 69 6e 67 20 74 68 65 20 6d 75 74 65  leasing the mute
e44c0 78 2e 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28  x. findLockInfo(
e44d0 29 20 6d 61 79 20 66 61 69 6c 0a 20 20 20 20 20  ) may fail.     
e44e0 20 2a 2a 20 69 6e 20 74 77 6f 20 73 63 65 6e 61   ** in two scena
e44f0 72 69 6f 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20  rios:.      **. 
e4500 20 20 20 20 20 2a 2a 20 20 20 28 61 29 20 41 20       **   (a) A 
e4510 63 61 6c 6c 20 74 6f 20 66 73 74 61 74 28 29 20  call to fstat() 
e4520 66 61 69 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a  failed..      **
e4530 20 20 20 28 62 29 20 41 20 6d 61 6c 6c 6f 63 20     (b) A malloc 
e4540 66 61 69 6c 65 64 2e 0a 20 20 20 20 20 20 2a 2a  failed..      **
e4550 0a 20 20 20 20 20 20 2a 2a 20 53 63 65 6e 61 72  .      ** Scenar
e4560 69 6f 20 28 62 29 20 6d 61 79 20 6f 6e 6c 79 20  io (b) may only 
e4570 6f 63 63 75 72 20 69 66 20 74 68 65 20 70 72 6f  occur if the pro
e4580 63 65 73 73 20 69 73 20 68 6f 6c 64 69 6e 67 20  cess is holding 
e4590 6e 6f 20 6f 74 68 65 72 0a 20 20 20 20 20 20 2a  no other.      *
e45a0 2a 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  * file descripto
e45b0 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 73  rs open on the s
e45c0 61 6d 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65  ame file. If the
e45d0 72 65 20 77 65 72 65 20 6f 74 68 65 72 20 66 69  re were other fi
e45e0 6c 65 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 63  le.      ** desc
e45f0 72 69 70 74 6f 72 73 20 6f 6e 20 74 68 69 73 20  riptors on this 
e4600 66 69 6c 65 2c 20 74 68 65 6e 20 6e 6f 20 6d 61  file, then no ma
e4610 6c 6c 6f 63 20 77 6f 75 6c 64 20 62 65 20 72 65  lloc would be re
e4620 71 75 69 72 65 64 20 62 79 0a 20 20 20 20 20 20  quired by.      
e4630 2a 2a 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28  ** findLockInfo(
e4640 29 2e 20 49 66 20 74 68 69 73 20 69 73 20 74 68  ). If this is th
e4650 65 20 63 61 73 65 2c 20 69 74 20 69 73 20 71 75  e case, it is qu
e4660 69 74 65 20 73 61 66 65 20 74 6f 20 63 6c 6f 73  ite safe to clos
e4670 65 0a 20 20 20 20 20 20 2a 2a 20 68 61 6e 64 6c  e.      ** handl
e4680 65 20 68 20 2d 20 61 73 20 69 74 20 69 73 20 67  e h - as it is g
e4690 75 61 72 61 6e 74 65 65 64 20 74 68 61 74 20 6e  uaranteed that n
e46a0 6f 20 70 6f 73 69 78 20 6c 6f 63 6b 73 20 77 69  o posix locks wi
e46b0 6c 6c 20 62 65 20 72 65 6c 65 61 73 65 64 0a 20  ll be released. 
e46c0 20 20 20 20 20 2a 2a 20 62 79 20 64 6f 69 6e 67       ** by doing
e46d0 20 73 6f 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20   so..      **.  
e46e0 20 20 20 20 2a 2a 20 49 66 20 73 63 65 6e 61 72      ** If scenar
e46f0 69 6f 20 28 61 29 20 63 61 75 73 65 64 20 74 68  io (a) caused th
e4700 65 20 65 72 72 6f 72 20 74 68 65 6e 20 74 68 69  e error then thi
e4710 6e 67 73 20 61 72 65 20 6e 6f 74 20 73 6f 20 73  ngs are not so s
e4720 61 66 65 2e 20 54 68 65 0a 20 20 20 20 20 20 2a  afe. The.      *
e4730 2a 20 69 6d 70 6c 69 63 69 74 20 61 73 73 75 6d  * implicit assum
e4740 70 74 69 6f 6e 20 68 65 72 65 20 69 73 20 74 68  ption here is th
e4750 61 74 20 69 66 20 66 73 74 61 74 28 29 20 66 61  at if fstat() fa
e4760 69 6c 73 2c 20 74 68 69 6e 67 73 20 61 72 65 20  ils, things are 
e4770 69 6e 0a 20 20 20 20 20 20 2a 2a 20 73 75 63 68  in.      ** such
e4780 20 62 61 64 20 73 68 61 70 65 20 74 68 61 74 20   bad shape that 
e4790 64 72 6f 70 70 69 6e 67 20 61 20 6c 6f 63 6b 20  dropping a lock 
e47a0 6f 72 20 74 77 6f 20 64 6f 65 73 6e 27 74 20 6d  or two doesn't m
e47b0 61 74 74 65 72 20 6d 75 63 68 2e 0a 20 20 20 20  atter much..    
e47c0 20 20 2a 2f 0a 20 20 20 20 20 20 63 6c 6f 73 65    */.      close
e47d0 28 68 29 3b 0a 20 20 20 20 20 20 68 20 3d 20 2d  (h);.      h = -
e47e0 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 69  1;.    }.    uni
e47f0 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  xLeaveMutex();. 
e4800 20 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45   }..#if SQLITE_E
e4810 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
e4820 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28 5f  YLE && defined(_
e4830 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 65 6c 73 65  _APPLE__).  else
e4840 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79   if( pLockingSty
e4850 6c 65 20 3d 3d 20 26 61 66 70 49 6f 4d 65 74 68  le == &afpIoMeth
e4860 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a 20 41 46  ods ){.    /* AF
e4870 50 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 74  P locking uses t
e4880 68 65 20 66 69 6c 65 20 70 61 74 68 20 73 6f 20  he file path so 
e4890 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69  it needs to be i
e48a0 6e 63 6c 75 64 65 64 20 69 6e 0a 20 20 20 20 2a  ncluded in.    *
e48b0 2a 20 74 68 65 20 61 66 70 4c 6f 63 6b 69 6e 67  * the afpLocking
e48c0 43 6f 6e 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a  Context..    */.
e48d0 20 20 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f      afpLockingCo
e48e0 6e 74 65 78 74 20 2a 70 43 74 78 3b 0a 20 20 20  ntext *pCtx;.   
e48f0 20 70 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f   pNew->lockingCo
e4900 6e 74 65 78 74 20 3d 20 70 43 74 78 20 3d 20 73  ntext = pCtx = s
e4910 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73  qlite3_malloc( s
e4920 69 7a 65 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a  izeof(*pCtx) );.
e4930 20 20 20 20 69 66 28 20 70 43 74 78 3d 3d 30 20      if( pCtx==0 
e4940 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
e4950 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
e4960 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20  }else{.      /* 
e4970 4e 42 3a 20 7a 46 69 6c 65 6e 61 6d 65 20 65 78  NB: zFilename ex
e4980 69 73 74 73 20 61 6e 64 20 72 65 6d 61 69 6e 73  ists and remains
e4990 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65   valid until the
e49a0 20 66 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 0a   file is closed.
e49b0 20 20 20 20 20 20 2a 2a 20 61 63 63 6f 72 64 69        ** accordi
e49c0 6e 67 20 74 6f 20 72 65 71 75 69 72 65 6d 65 6e  ng to requiremen
e49d0 74 20 46 31 31 31 34 31 2e 20 20 53 6f 20 77 65  t F11141.  So we
e49e0 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20   do not need to 
e49f0 6d 61 6b 65 20 61 0a 20 20 20 20 20 20 2a 2a 20  make a.      ** 
e4a00 63 6f 70 79 20 6f 66 20 74 68 65 20 66 69 6c 65  copy of the file
e4a10 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 70  name. */.      p
e4a20 43 74 78 2d 3e 64 62 50 61 74 68 20 3d 20 7a 46  Ctx->dbPath = zF
e4a30 69 6c 65 6e 61 6d 65 3b 0a 20 20 20 20 20 20 73  ilename;.      s
e4a40 72 61 6e 64 6f 6d 64 65 76 28 29 3b 0a 20 20 20  randomdev();.   
e4a50 20 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65     unixEnterMute
e4a60 78 28 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  x();.      rc = 
e4a70 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65  findLockInfo(pNe
e4a80 77 2c 20 4e 55 4c 4c 2c 20 26 70 4e 65 77 2d 3e  w, NULL, &pNew->
e4a90 70 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 75 6e  pOpen);.      un
e4aa0 69 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 20  ixLeaveMutex(); 
e4ab0 20 20 20 20 20 20 20 0a 20 20 20 20 7d 0a 20 20         .    }.  
e4ac0 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 65 6c 73 65  }.#endif..  else
e4ad0 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67 53 74 79   if( pLockingSty
e4ae0 6c 65 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f  le == &dotlockIo
e4af0 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f  Methods ){.    /
e4b00 2a 20 44 6f 74 66 69 6c 65 20 6c 6f 63 6b 69 6e  * Dotfile lockin
e4b10 67 20 75 73 65 73 20 74 68 65 20 66 69 6c 65 20  g uses the file 
e4b20 70 61 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73  path so it needs
e4b30 20 74 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20   to be included 
e4b40 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 6f  in.    ** the do
e4b50 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74  tlockLockingCont
e4b60 65 78 74 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ext .    */.    
e4b70 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 3b  char *zLockFile;
e4b80 0a 20 20 20 20 69 6e 74 20 6e 46 69 6c 65 6e 61  .    int nFilena
e4b90 6d 65 3b 0a 20 20 20 20 6e 46 69 6c 65 6e 61 6d  me;.    nFilenam
e4ba0 65 20 3d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28  e = (int)strlen(
e4bb0 7a 46 69 6c 65 6e 61 6d 65 29 20 2b 20 36 3b 0a  zFilename) + 6;.
e4bc0 20 20 20 20 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20      zLockFile = 
e4bd0 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f  (char *)sqlite3_
e4be0 6d 61 6c 6c 6f 63 28 6e 46 69 6c 65 6e 61 6d 65  malloc(nFilename
e4bf0 29 3b 0a 20 20 20 20 69 66 28 20 7a 4c 6f 63 6b  );.    if( zLock
e4c00 46 69 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20  File==0 ){.     
e4c10 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
e4c20 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  EM;.    }else{. 
e4c30 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
e4c40 72 69 6e 74 66 28 6e 46 69 6c 65 6e 61 6d 65 2c  rintf(nFilename,
e4c50 20 7a 4c 6f 63 6b 46 69 6c 65 2c 20 22 25 73 22   zLockFile, "%s"
e4c60 20 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 2c   DOTLOCK_SUFFIX,
e4c70 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20   zFilename);.   
e4c80 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 6c 6f 63   }.    pNew->loc
e4c90 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 7a 4c  kingContext = zL
e4ca0 6f 63 6b 46 69 6c 65 3b 0a 20 20 7d 0a 0a 23 69  ockFile;.  }..#i
e4cb0 66 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 65  f OS_VXWORKS.  e
e4cc0 6c 73 65 20 69 66 28 20 70 4c 6f 63 6b 69 6e 67  lse if( pLocking
e4cd0 53 74 79 6c 65 20 3d 3d 20 26 73 65 6d 49 6f 4d  Style == &semIoM
e4ce0 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f 2a  ethods ){.    /*
e4cf0 20 4e 61 6d 65 64 20 73 65 6d 61 70 68 6f 72 65   Named semaphore
e4d00 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20 74 68   locking uses th
e4d10 65 20 66 69 6c 65 20 70 61 74 68 20 73 6f 20 69  e file path so i
e4d20 74 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20  t needs to be.  
e4d30 20 20 2a 2a 20 69 6e 63 6c 75 64 65 64 20 69 6e    ** included in
e4d40 20 74 68 65 20 73 65 6d 4c 6f 63 6b 69 6e 67 43   the semLockingC
e4d50 6f 6e 74 65 78 74 0a 20 20 20 20 2a 2f 0a 20 20  ontext.    */.  
e4d60 20 20 75 6e 69 78 45 6e 74 65 72 4d 75 74 65 78    unixEnterMutex
e4d70 28 29 3b 0a 20 20 20 20 72 63 20 3d 20 66 69 6e  ();.    rc = fin
e4d80 64 4c 6f 63 6b 49 6e 66 6f 28 70 4e 65 77 2c 20  dLockInfo(pNew, 
e4d90 26 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70  &pNew->pLock, &p
e4da0 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20  New->pOpen);.   
e4db0 20 69 66 28 20 28 72 63 3d 3d 53 51 4c 49 54 45   if( (rc==SQLITE
e4dc0 5f 4f 4b 29 20 26 26 20 28 70 4e 65 77 2d 3e 70  _OK) && (pNew->p
e4dd0 4f 70 65 6e 2d 3e 70 53 65 6d 3d 3d 4e 55 4c 4c  Open->pSem==NULL
e4de0 29 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20  ) ){.      char 
e4df0 2a 7a 53 65 6d 4e 61 6d 65 20 3d 20 70 4e 65 77  *zSemName = pNew
e4e00 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d  ->pOpen->aSemNam
e4e10 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a  e;.      int n;.
e4e20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
e4e30 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54 48 4e  printf(MAX_PATHN
e4e40 41 4d 45 2c 20 7a 53 65 6d 4e 61 6d 65 2c 20 22  AME, zSemName, "
e4e50 2f 25 73 2e 73 65 6d 22 2c 0a 20 20 20 20 20 20  /%s.sem",.      
e4e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e4e70 20 70 4e 65 77 2d 3e 70 49 64 2d 3e 7a 43 61 6e   pNew->pId->zCan
e4e80 6f 6e 69 63 61 6c 4e 61 6d 65 29 3b 0a 20 20 20  onicalName);.   
e4e90 20 20 20 66 6f 72 28 20 6e 3d 31 3b 20 7a 53 65     for( n=1; zSe
e4ea0 6d 4e 61 6d 65 5b 6e 5d 3b 20 6e 2b 2b 20 29 0a  mName[n]; n++ ).
e4eb0 20 20 20 20 20 20 20 20 69 66 28 20 7a 53 65 6d          if( zSem
e4ec0 4e 61 6d 65 5b 6e 5d 3d 3d 27 2f 27 20 29 20 7a  Name[n]=='/' ) z
e4ed0 53 65 6d 4e 61 6d 65 5b 6e 5d 20 3d 20 27 5f 27  SemName[n] = '_'
e4ee0 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4f  ;.      pNew->pO
e4ef0 70 65 6e 2d 3e 70 53 65 6d 20 3d 20 73 65 6d 5f  pen->pSem = sem_
e4f00 6f 70 65 6e 28 7a 53 65 6d 4e 61 6d 65 2c 20 4f  open(zSemName, O
e4f10 5f 43 52 45 41 54 2c 20 30 36 36 36 2c 20 31 29  _CREAT, 0666, 1)
e4f20 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77  ;.      if( pNew
e4f30 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d 3d  ->pOpen->pSem ==
e4f40 20 53 45 4d 5f 46 41 49 4c 45 44 20 29 7b 0a 20   SEM_FAILED ){. 
e4f50 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
e4f60 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
e4f70 20 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61    pNew->pOpen->a
e4f80 53 65 6d 4e 61 6d 65 5b 30 5d 20 3d 20 27 5c 30  SemName[0] = '\0
e4f90 27 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  ';.      }.    }
e4fa0 0a 20 20 20 20 75 6e 69 78 4c 65 61 76 65 4d 75  .    unixLeaveMu
e4fb0 74 65 78 28 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  tex();.  }.#endi
e4fc0 66 0a 20 20 0a 20 20 70 4e 65 77 2d 3e 6c 61 73  f.  .  pNew->las
e4fd0 74 45 72 72 6e 6f 20 3d 20 30 3b 0a 23 69 66 20  tErrno = 0;.#if 
e4fe0 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 69 66 28  OS_VXWORKS.  if(
e4ff0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
e5000 7b 0a 20 20 20 20 75 6e 6c 69 6e 6b 28 7a 46 69  {.    unlink(zFi
e5010 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 69 73 44  lename);.    isD
e5020 65 6c 65 74 65 20 3d 20 30 3b 0a 20 20 7d 0a 20  elete = 0;.  }. 
e5030 20 70 4e 65 77 2d 3e 69 73 44 65 6c 65 74 65 20   pNew->isDelete 
e5040 3d 20 69 73 44 65 6c 65 74 65 3b 0a 23 65 6e 64  = isDelete;.#end
e5050 69 66 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  if.  if( rc!=SQL
e5060 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66  ITE_OK ){.    if
e5070 28 20 64 69 72 66 64 3e 3d 30 20 29 20 63 6c 6f  ( dirfd>=0 ) clo
e5080 73 65 28 64 69 72 66 64 29 3b 20 2f 2a 20 73 69  se(dirfd); /* si
e5090 6c 65 6e 74 20 6c 65 61 6b 20 69 66 20 66 61 69  lent leak if fai
e50a0 6c 2c 20 61 6c 72 65 61 64 79 20 69 6e 20 65 72  l, already in er
e50b0 72 6f 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 68  ror */.    if( h
e50c0 3e 3d 30 20 29 20 63 6c 6f 73 65 28 68 29 3b 0a  >=0 ) close(h);.
e50d0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65    }else{.    pNe
e50e0 77 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 70 4c 6f  w->pMethod = pLo
e50f0 63 6b 69 6e 67 53 74 79 6c 65 3b 0a 20 20 20 20  ckingStyle;.    
e5100 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b  OpenCounter(+1);
e5110 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
e5120 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20  ;.}../*.** Open 
e5130 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  a file descripto
e5140 72 20 74 6f 20 74 68 65 20 64 69 72 65 63 74 6f  r to the directo
e5150 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69  ry containing fi
e5160 6c 65 20 7a 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a  le zFilename..**
e5170 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
e5180 2a 70 46 64 20 69 73 20 73 65 74 20 74 6f 20 74  *pFd is set to t
e5190 68 65 20 6f 70 65 6e 65 64 20 66 69 6c 65 20 64  he opened file d
e51a0 65 73 63 72 69 70 74 6f 72 20 61 6e 64 0a 2a 2a  escriptor and.**
e51b0 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
e51c0 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72  turned. If an er
e51d0 72 6f 72 20 6f 63 63 75 72 73 2c 20 65 69 74 68  ror occurs, eith
e51e0 65 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a  er SQLITE_NOMEM.
e51f0 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 43 41 4e  ** or SQLITE_CAN
e5200 54 4f 50 45 4e 20 69 73 20 72 65 74 75 72 6e 65  TOPEN is returne
e5210 64 20 61 6e 64 20 2a 70 46 64 20 69 73 20 73 65  d and *pFd is se
e5220 74 20 74 6f 20 61 6e 20 75 6e 64 65 66 69 6e 65  t to an undefine
e5230 64 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a  d.** value..**.*
e5240 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69  * If SQLITE_OK i
e5250 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 20  s returned, the 
e5260 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e  caller is respon
e5270 73 69 62 6c 65 20 66 6f 72 20 63 6c 6f 73 69 6e  sible for closin
e5280 67 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 64 65  g.** the file de
e5290 73 63 72 69 70 74 6f 72 20 2a 70 46 64 20 75 73  scriptor *pFd us
e52a0 69 6e 67 20 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a  ing close()..*/.
e52b0 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65 6e 44  static int openD
e52c0 69 72 65 63 74 6f 72 79 28 63 6f 6e 73 74 20 63  irectory(const c
e52d0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20  har *zFilename, 
e52e0 69 6e 74 20 2a 70 46 64 29 7b 0a 20 20 69 6e 74  int *pFd){.  int
e52f0 20 69 69 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20   ii;.  int fd = 
e5300 2d 31 3b 0a 20 20 63 68 61 72 20 7a 44 69 72 6e  -1;.  char zDirn
e5310 61 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45  ame[MAX_PATHNAME
e5320 2b 31 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  +1];..  sqlite3_
e5330 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54  snprintf(MAX_PAT
e5340 48 4e 41 4d 45 2c 20 7a 44 69 72 6e 61 6d 65 2c  HNAME, zDirname,
e5350 20 22 25 73 22 2c 20 7a 46 69 6c 65 6e 61 6d 65   "%s", zFilename
e5360 29 3b 0a 20 20 66 6f 72 28 69 69 3d 28 69 6e 74  );.  for(ii=(int
e5370 29 73 74 72 6c 65 6e 28 7a 44 69 72 6e 61 6d 65  )strlen(zDirname
e5380 29 3b 20 69 69 3e 31 20 26 26 20 7a 44 69 72 6e  ); ii>1 && zDirn
e5390 61 6d 65 5b 69 69 5d 21 3d 27 2f 27 3b 20 69 69  ame[ii]!='/'; ii
e53a0 2d 2d 29 3b 0a 20 20 69 66 28 20 69 69 3e 30 20  --);.  if( ii>0 
e53b0 29 7b 0a 20 20 20 20 7a 44 69 72 6e 61 6d 65 5b  ){.    zDirname[
e53c0 69 69 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20  ii] = '\0';.    
e53d0 66 64 20 3d 20 6f 70 65 6e 28 7a 44 69 72 6e 61  fd = open(zDirna
e53e0 6d 65 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 42  me, O_RDONLY|O_B
e53f0 49 4e 41 52 59 2c 20 30 29 3b 0a 20 20 20 20 69  INARY, 0);.    i
e5400 66 28 20 66 64 3e 3d 30 20 29 7b 0a 23 69 66 64  f( fd>=0 ){.#ifd
e5410 65 66 20 46 44 5f 43 4c 4f 45 58 45 43 0a 20 20  ef FD_CLOEXEC.  
e5420 20 20 20 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f      fcntl(fd, F_
e5430 53 45 54 46 44 2c 20 66 63 6e 74 6c 28 66 64 2c  SETFD, fcntl(fd,
e5440 20 46 5f 47 45 54 46 44 2c 20 30 29 20 7c 20 46   F_GETFD, 0) | F
e5450 44 5f 43 4c 4f 45 58 45 43 29 3b 0a 23 65 6e 64  D_CLOEXEC);.#end
e5460 69 66 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45  if.      OSTRACE
e5470 33 28 22 4f 50 45 4e 44 49 52 20 25 2d 33 64 20  3("OPENDIR %-3d 
e5480 25 73 5c 6e 22 2c 20 66 64 2c 20 7a 44 69 72 6e  %s\n", fd, zDirn
e5490 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ame);.    }.  }.
e54a0 20 20 2a 70 46 64 20 3d 20 66 64 3b 0a 20 20 72    *pFd = fd;.  r
e54b0 65 74 75 72 6e 20 28 66 64 3e 3d 30 3f 53 51 4c  eturn (fd>=0?SQL
e54c0 49 54 45 5f 4f 4b 3a 53 51 4c 49 54 45 5f 43 41  ITE_OK:SQLITE_CA
e54d0 4e 54 4f 50 45 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  NTOPEN);.}../*.*
e54e0 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d 70 6f  * Create a tempo
e54f0 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 69  rary file name i
e5500 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20 6d 75  n zBuf.  zBuf mu
e5510 73 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a  st be allocated.
e5520 2a 2a 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e  ** by the callin
e5530 67 20 70 72 6f 63 65 73 73 20 61 6e 64 20 6d 75  g process and mu
e5540 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75 67 68  st be big enough
e5550 20 74 6f 20 68 6f 6c 64 20 61 74 20 6c 65 61 73   to hold at leas
e5560 74 0a 2a 2a 20 70 56 66 73 2d 3e 6d 78 50 61 74  t.** pVfs->mxPat
e5570 68 6e 61 6d 65 20 62 79 74 65 73 2e 0a 2a 2f 0a  hname bytes..*/.
e5580 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 54 65  static int getTe
e5590 6d 70 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c  mpname(int nBuf,
e55a0 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20   char *zBuf){.  
e55b0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
e55c0 72 20 2a 61 7a 44 69 72 73 5b 5d 20 3d 20 7b 0a  r *azDirs[] = {.
e55d0 20 20 20 20 20 30 2c 0a 20 20 20 20 20 30 2c 0a       0,.     0,.
e55e0 20 20 20 20 20 22 2f 76 61 72 2f 74 6d 70 22 2c       "/var/tmp",
e55f0 0a 20 20 20 20 20 22 2f 75 73 72 2f 74 6d 70 22  .     "/usr/tmp"
e5600 2c 0a 20 20 20 20 20 22 2f 74 6d 70 22 2c 0a 20  ,.     "/tmp",. 
e5610 20 20 20 20 22 2e 22 2c 0a 20 20 7d 3b 0a 20 20      ".",.  };.  
e5620 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
e5630 69 67 6e 65 64 20 63 68 61 72 20 7a 43 68 61 72  igned char zChar
e5640 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63 64 65  s[] =.    "abcde
e5650 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75  fghijklmnopqrstu
e5660 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42 43 44  vwxyz".    "ABCD
e5670 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54  EFGHIJKLMNOPQRST
e5680 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30 31 32  UVWXYZ".    "012
e5690 33 34 35 36 37 38 39 22 3b 0a 20 20 75 6e 73 69  3456789";.  unsi
e56a0 67 6e 65 64 20 69 6e 74 20 69 2c 20 6a 3b 0a 20  gned int i, j;. 
e56b0 20 73 74 72 75 63 74 20 73 74 61 74 20 62 75 66   struct stat buf
e56c0 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
e56d0 7a 44 69 72 20 3d 20 22 2e 22 3b 0a 0a 20 20 2f  zDir = ".";..  /
e56e0 2a 20 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69  * It's odd to si
e56f0 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72  mulate an io-err
e5700 6f 72 20 68 65 72 65 2c 20 62 75 74 20 72 65 61  or here, but rea
e5710 6c 6c 79 20 74 68 69 73 20 69 73 20 6a 75 73 74  lly this is just
e5720 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20  .  ** using the 
e5730 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74  io-error infrast
e5740 72 75 63 74 75 72 65 20 74 6f 20 74 65 73 74 20  ructure to test 
e5750 74 68 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64  that SQLite hand
e5760 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a 20 66 75  les this.  ** fu
e5770 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20  nction failing. 
e5780 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65  .  */.  Simulate
e5790 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20  IOError( return 
e57a0 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 3b 0a  SQLITE_IOERR );.
e57b0 0a 20 20 61 7a 44 69 72 73 5b 30 5d 20 3d 20 73  .  azDirs[0] = s
e57c0 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65  qlite3_temp_dire
e57d0 63 74 6f 72 79 3b 0a 20 20 69 66 20 28 4e 55 4c  ctory;.  if (NUL
e57e0 4c 20 3d 3d 20 61 7a 44 69 72 73 5b 31 5d 29 20  L == azDirs[1]) 
e57f0 7b 0a 20 20 20 20 61 7a 44 69 72 73 5b 31 5d 20  {.    azDirs[1] 
e5800 3d 20 67 65 74 65 6e 76 28 22 54 4d 50 44 49 52  = getenv("TMPDIR
e5810 22 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 66 6f 72  ");.  }.  .  for
e5820 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66 28 61  (i=0; i<sizeof(a
e5830 7a 44 69 72 73 29 2f 73 69 7a 65 6f 66 28 61 7a  zDirs)/sizeof(az
e5840 44 69 72 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a  Dirs[0]); i++){.
e5850 20 20 20 20 69 66 28 20 61 7a 44 69 72 73 5b 69      if( azDirs[i
e5860 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  ]==0 ) continue;
e5870 0a 20 20 20 20 69 66 28 20 73 74 61 74 28 61 7a  .    if( stat(az
e5880 44 69 72 73 5b 69 5d 2c 20 26 62 75 66 29 20 29  Dirs[i], &buf) )
e5890 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69   continue;.    i
e58a0 66 28 20 21 53 5f 49 53 44 49 52 28 62 75 66 2e  f( !S_ISDIR(buf.
e58b0 73 74 5f 6d 6f 64 65 29 20 29 20 63 6f 6e 74 69  st_mode) ) conti
e58c0 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 61 63 63  nue;.    if( acc
e58d0 65 73 73 28 61 7a 44 69 72 73 5b 69 5d 2c 20 30  ess(azDirs[i], 0
e58e0 37 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  7) ) continue;. 
e58f0 20 20 20 7a 44 69 72 20 3d 20 61 7a 44 69 72 73     zDir = azDirs
e5900 5b 69 5d 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a  [i];.    break;.
e5910 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20    }..  /* Check 
e5920 74 68 61 74 20 74 68 65 20 6f 75 74 70 75 74 20  that the output 
e5930 62 75 66 66 65 72 20 69 73 20 6c 61 72 67 65 20  buffer is large 
e5940 65 6e 6f 75 67 68 20 66 6f 72 20 74 68 65 20 74  enough for the t
e5950 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 0a 20  emporary file . 
e5960 20 2a 2a 20 6e 61 6d 65 2e 20 49 66 20 69 74 20   ** name. If it 
e5970 69 73 20 6e 6f 74 2c 20 72 65 74 75 72 6e 20 53  is not, return S
e5980 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 20 20 2a  QLITE_ERROR..  *
e5990 2f 0a 20 20 69 66 28 20 28 73 74 72 6c 65 6e 28  /.  if( (strlen(
e59a0 7a 44 69 72 29 20 2b 20 73 74 72 6c 65 6e 28 53  zDir) + strlen(S
e59b0 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f  QLITE_TEMP_FILE_
e59c0 50 52 45 46 49 58 29 20 2b 20 31 37 29 20 3e 3d  PREFIX) + 17) >=
e59d0 20 28 73 69 7a 65 5f 74 29 6e 42 75 66 20 29 7b   (size_t)nBuf ){
e59e0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
e59f0 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20  TE_ERROR;.  }.. 
e5a00 20 64 6f 7b 0a 20 20 20 20 73 71 6c 69 74 65 33   do{.    sqlite3
e5a10 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 2d 31  _snprintf(nBuf-1
e5a20 37 2c 20 7a 42 75 66 2c 20 22 25 73 2f 22 53 51  7, zBuf, "%s/"SQ
e5a30 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50  LITE_TEMP_FILE_P
e5a40 52 45 46 49 58 2c 20 7a 44 69 72 29 3b 0a 20 20  REFIX, zDir);.  
e5a50 20 20 6a 20 3d 20 28 69 6e 74 29 73 74 72 6c 65    j = (int)strle
e5a60 6e 28 7a 42 75 66 29 3b 0a 20 20 20 20 73 71 6c  n(zBuf);.    sql
e5a70 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28  ite3_randomness(
e5a80 31 35 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20  15, &zBuf[j]);. 
e5a90 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 31 35     for(i=0; i<15
e5aa0 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20  ; i++, j++){.   
e5ab0 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68     zBuf[j] = (ch
e5ac0 61 72 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73  ar)zChars[ ((uns
e5ad0 69 67 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b  igned char)zBuf[
e5ae0 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61  j])%(sizeof(zCha
e5af0 72 73 29 2d 31 29 20 5d 3b 0a 20 20 20 20 7d 0a  rs)-1) ];.    }.
e5b00 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 30 3b      zBuf[j] = 0;
e5b10 0a 20 20 7d 77 68 69 6c 65 28 20 61 63 63 65 73  .  }while( acces
e5b20 73 28 7a 42 75 66 2c 30 29 3d 3d 30 20 29 3b 0a  s(zBuf,0)==0 );.
e5b30 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e5b40 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54  OK;.}..#if SQLIT
e5b50 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
e5b60 5f 53 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65  _STYLE && define
e5b70 64 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 2f 2a 0a  d(__APPLE__)./*.
e5b80 2a 2a 20 52 6f 75 74 69 6e 65 20 74 6f 20 74 72  ** Routine to tr
e5b90 61 6e 73 66 6f 72 6d 20 61 20 75 6e 69 78 46 69  ansform a unixFi
e5ba0 6c 65 20 69 6e 74 6f 20 61 20 70 72 6f 78 79 2d  le into a proxy-
e5bb0 6c 6f 63 6b 69 6e 67 20 75 6e 69 78 46 69 6c 65  locking unixFile
e5bc0 2e 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  ..** Implementat
e5bd0 69 6f 6e 20 69 6e 20 74 68 65 20 70 72 6f 78 79  ion in the proxy
e5be0 2d 6c 6f 63 6b 20 64 69 76 69 73 69 6f 6e 2c 20  -lock division, 
e5bf0 62 75 74 20 75 73 65 64 20 62 79 20 75 6e 69 78  but used by unix
e5c00 4f 70 65 6e 28 29 0a 2a 2a 20 69 66 20 53 51 4c  Open().** if SQL
e5c10 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58 59  ITE_PREFER_PROXY
e5c20 5f 4c 4f 43 4b 49 4e 47 20 69 73 20 64 65 66 69  _LOCKING is defi
e5c30 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ned..*/.static i
e5c40 6e 74 20 70 72 6f 78 79 54 72 61 6e 73 66 6f 72  nt proxyTransfor
e5c50 6d 55 6e 69 78 46 69 6c 65 28 75 6e 69 78 46 69  mUnixFile(unixFi
e5c60 6c 65 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a  le*, const char*
e5c70 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  );.#endif../*.**
e5c80 20 53 65 61 72 63 68 20 66 6f 72 20 61 6e 20 75   Search for an u
e5c90 6e 75 73 65 64 20 66 69 6c 65 20 64 65 73 63 72  nused file descr
e5ca0 69 70 74 6f 72 20 74 68 61 74 20 77 61 73 20 6f  iptor that was o
e5cb0 70 65 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74  pened on the dat
e5cc0 61 62 61 73 65 20 0a 2a 2a 20 66 69 6c 65 20 28  abase .** file (
e5cd0 6e 6f 74 20 61 20 6a 6f 75 72 6e 61 6c 20 6f 72  not a journal or
e5ce0 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20   master-journal 
e5cf0 66 69 6c 65 29 20 69 64 65 6e 74 69 66 69 65 64  file) identified
e5d00 20 62 79 20 70 61 74 68 6e 61 6d 65 0a 2a 2a 20   by pathname.** 
e5d10 7a 50 61 74 68 20 77 69 74 68 20 53 51 4c 49 54  zPath with SQLIT
e5d20 45 5f 4f 50 45 4e 5f 58 58 58 20 66 6c 61 67 73  E_OPEN_XXX flags
e5d30 20 6d 61 74 63 68 69 6e 67 20 74 68 6f 73 65 20   matching those 
e5d40 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65  passed as the se
e5d50 63 6f 6e 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  cond.** argument
e5d60 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
e5d70 6e 2e 0a 2a 2a 0a 2a 2a 20 53 75 63 68 20 61 20  n..**.** Such a 
e5d80 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
e5d90 6d 61 79 20 65 78 69 73 74 20 69 66 20 61 20 64  may exist if a d
e5da0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
e5db0 6f 6e 20 77 61 73 20 63 6c 6f 73 65 64 0a 2a 2a  on was closed.**
e5dc0 20 62 75 74 20 74 68 65 20 61 73 73 6f 63 69 61   but the associa
e5dd0 74 65 64 20 66 69 6c 65 20 64 65 73 63 72 69 70  ted file descrip
e5de0 74 6f 72 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65  tor could not be
e5df0 20 63 6c 6f 73 65 64 20 62 65 63 61 75 73 65 20   closed because 
e5e00 73 6f 6d 65 0a 2a 2a 20 6f 74 68 65 72 20 66 69  some.** other fi
e5e10 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f 70  le descriptor op
e5e20 65 6e 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 66  en on the same f
e5e30 69 6c 65 20 69 73 20 68 6f 6c 64 69 6e 67 20 61  ile is holding a
e5e40 20 66 69 6c 65 2d 6c 6f 63 6b 2e 0a 2a 2a 20 52   file-lock..** R
e5e50 65 66 65 72 20 74 6f 20 63 6f 6d 6d 65 6e 74 73  efer to comments
e5e60 20 69 6e 20 74 68 65 20 75 6e 69 78 43 6c 6f 73   in the unixClos
e5e70 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 61 6e 64  e() function and
e5e80 20 74 68 65 20 6c 65 6e 67 74 68 79 20 63 6f 6d   the lengthy com
e5e90 6d 65 6e 74 0a 2a 2a 20 64 65 73 63 72 69 62 69  ment.** describi
e5ea0 6e 67 20 22 50 6f 73 69 78 20 41 64 76 69 73 6f  ng "Posix Adviso
e5eb0 72 79 20 4c 6f 63 6b 69 6e 67 22 20 61 74 20 74  ry Locking" at t
e5ec0 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 69 73  he start of this
e5ed0 20 66 69 6c 65 20 66 6f 72 20 0a 2a 2a 20 66 75   file for .** fu
e5ee0 72 74 68 65 72 20 64 65 74 61 69 6c 73 2e 20 41  rther details. A
e5ef0 6c 73 6f 2c 20 74 69 63 6b 65 74 20 23 34 30 31  lso, ticket #401
e5f00 38 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 73 75  8..**.** If a su
e5f10 69 74 61 62 6c 65 20 66 69 6c 65 20 64 65 73 63  itable file desc
e5f20 72 69 70 74 6f 72 20 69 73 20 66 6f 75 6e 64 2c  riptor is found,
e5f30 20 74 68 65 6e 20 69 74 20 69 73 20 72 65 74 75   then it is retu
e5f40 72 6e 65 64 2e 20 49 66 20 6e 6f 0a 2a 2a 20 73  rned. If no.** s
e5f50 75 63 68 20 66 69 6c 65 20 64 65 73 63 72 69 70  uch file descrip
e5f60 74 6f 72 20 69 73 20 6c 6f 63 61 74 65 64 2c 20  tor is located, 
e5f70 2d 31 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  -1 is returned..
e5f80 2a 2f 0a 73 74 61 74 69 63 20 55 6e 69 78 55 6e  */.static UnixUn
e5f90 75 73 65 64 46 64 20 2a 66 69 6e 64 52 65 75 73  usedFd *findReus
e5fa0 61 62 6c 65 46 64 28 63 6f 6e 73 74 20 63 68 61  ableFd(const cha
e5fb0 72 20 2a 7a 50 61 74 68 2c 20 69 6e 74 20 66 6c  r *zPath, int fl
e5fc0 61 67 73 29 7b 0a 20 20 55 6e 69 78 55 6e 75 73  ags){.  UnixUnus
e5fd0 65 64 46 64 20 2a 70 55 6e 75 73 65 64 20 3d 20  edFd *pUnused = 
e5fe0 30 3b 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20  0;..  /* Do not 
e5ff0 73 65 61 72 63 68 20 66 6f 72 20 61 6e 20 75 6e  search for an un
e6000 75 73 65 64 20 66 69 6c 65 20 64 65 73 63 72 69  used file descri
e6010 70 74 6f 72 20 6f 6e 20 76 78 77 6f 72 6b 73 2e  ptor on vxworks.
e6020 20 4e 6f 74 20 62 65 63 61 75 73 65 0a 20 20 2a   Not because.  *
e6030 2a 20 76 78 77 6f 72 6b 73 20 77 6f 75 6c 64 20  * vxworks would 
e6040 6e 6f 74 20 62 65 6e 65 66 69 74 20 66 72 6f 6d  not benefit from
e6050 20 74 68 65 20 63 68 61 6e 67 65 20 28 69 74 20   the change (it 
e6060 6d 69 67 68 74 2c 20 77 65 27 72 65 20 6e 6f 74  might, we're not
e6070 20 73 75 72 65 29 2c 0a 20 20 2a 2a 20 62 75 74   sure),.  ** but
e6080 20 62 65 63 61 75 73 65 20 6e 6f 20 77 61 79 20   because no way 
e6090 74 6f 20 74 65 73 74 20 69 74 20 69 73 20 63 75  to test it is cu
e60a0 72 72 65 6e 74 6c 79 20 61 76 61 69 6c 61 62 6c  rrently availabl
e60b0 65 2e 20 49 74 20 69 73 20 62 65 74 74 65 72 20  e. It is better 
e60c0 0a 20 20 2a 2a 20 6e 6f 74 20 74 6f 20 72 69 73  .  ** not to ris
e60d0 6b 20 62 72 65 61 6b 69 6e 67 20 76 78 77 6f 72  k breaking vxwor
e60e0 6b 73 20 73 75 70 70 6f 72 74 20 66 6f 72 20 74  ks support for t
e60f0 68 65 20 73 61 6b 65 20 6f 66 20 73 75 63 68 20  he sake of such 
e6100 61 6e 20 6f 62 73 63 75 72 65 20 0a 20 20 2a 2a  an obscure .  **
e6110 20 66 65 61 74 75 72 65 2e 20 20 2a 2f 0a 23 69   feature.  */.#i
e6120 66 20 21 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20  f !OS_VXWORKS.  
e6130 73 74 72 75 63 74 20 73 74 61 74 20 73 53 74 61  struct stat sSta
e6140 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
e6150 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 73 20       /* Results 
e6160 6f 66 20 73 74 61 74 28 29 20 63 61 6c 6c 20 2a  of stat() call *
e6170 2f 0a 0a 20 20 2f 2a 20 41 20 73 74 61 74 28 29  /..  /* A stat()
e6180 20 63 61 6c 6c 20 6d 61 79 20 66 61 69 6c 20 66   call may fail f
e6190 6f 72 20 76 61 72 69 6f 75 73 20 72 65 61 73 6f  or various reaso
e61a0 6e 73 2e 20 49 66 20 74 68 69 73 20 68 61 70 70  ns. If this happ
e61b0 65 6e 73 2c 20 69 74 20 69 73 0a 20 20 2a 2a 20  ens, it is.  ** 
e61c0 61 6c 6d 6f 73 74 20 63 65 72 74 61 69 6e 20 74  almost certain t
e61d0 68 61 74 20 61 6e 20 6f 70 65 6e 28 29 20 63 61  hat an open() ca
e61e0 6c 6c 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 70  ll on the same p
e61f0 61 74 68 20 77 69 6c 6c 20 61 6c 73 6f 20 66 61  ath will also fa
e6200 69 6c 2e 0a 20 20 2a 2a 20 46 6f 72 20 74 68 69  il..  ** For thi
e6210 73 20 72 65 61 73 6f 6e 2c 20 69 66 20 61 6e 20  s reason, if an 
e6220 65 72 72 6f 72 20 6f 63 63 75 72 73 20 69 6e 20  error occurs in 
e6230 74 68 65 20 73 74 61 74 28 29 20 63 61 6c 6c 20  the stat() call 
e6240 68 65 72 65 2c 20 69 74 20 69 73 0a 20 20 2a 2a  here, it is.  **
e6250 20 69 67 6e 6f 72 65 64 20 61 6e 64 20 2d 31 20   ignored and -1 
e6260 69 73 20 72 65 74 75 72 6e 65 64 2e 20 54 68 65  is returned. The
e6270 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 74 72 79   caller will try
e6280 20 74 6f 20 6f 70 65 6e 20 61 20 6e 65 77 20 66   to open a new f
e6290 69 6c 65 0a 20 20 2a 2a 20 64 65 73 63 72 69 70  ile.  ** descrip
e62a0 74 6f 72 20 6f 6e 20 74 68 65 20 73 61 6d 65 20  tor on the same 
e62b0 70 61 74 68 2c 20 66 61 69 6c 2c 20 61 6e 64 20  path, fail, and 
e62c0 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20  return an error 
e62d0 74 6f 20 53 51 4c 69 74 65 2e 0a 20 20 2a 2a 0a  to SQLite..  **.
e62e0 20 20 2a 2a 20 45 76 65 6e 20 69 66 20 61 20 73    ** Even if a s
e62f0 75 62 73 65 71 75 65 6e 74 20 6f 70 65 6e 28 29  ubsequent open()
e6300 20 63 61 6c 6c 20 64 6f 65 73 20 73 75 63 63 65   call does succe
e6310 65 64 2c 20 74 68 65 20 63 6f 6e 73 65 71 75 65  ed, the conseque
e6320 6e 63 65 73 20 6f 66 0a 20 20 2a 2a 20 6e 6f 74  nces of.  ** not
e6330 20 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 61   searching for a
e6340 20 72 65 73 75 73 61 62 6c 65 20 66 69 6c 65 20   resusable file 
e6350 64 65 73 63 72 69 70 74 6f 72 20 61 72 65 20 6e  descriptor are n
e6360 6f 74 20 64 69 72 65 2e 20 20 2a 2f 0a 20 20 69  ot dire.  */.  i
e6370 66 28 20 30 3d 3d 73 74 61 74 28 7a 50 61 74 68  f( 0==stat(zPath
e6380 2c 20 26 73 53 74 61 74 29 20 29 7b 0a 20 20 20  , &sStat) ){.   
e6390 20 73 74 72 75 63 74 20 75 6e 69 78 4f 70 65 6e   struct unixOpen
e63a0 43 6e 74 20 2a 70 4f 3b 0a 20 20 20 20 73 74 72  Cnt *pO;.    str
e63b0 75 63 74 20 75 6e 69 78 46 69 6c 65 49 64 20 69  uct unixFileId i
e63c0 64 3b 0a 20 20 20 20 69 64 2e 64 65 76 20 3d 20  d;.    id.dev = 
e63d0 73 53 74 61 74 2e 73 74 5f 64 65 76 3b 0a 20 20  sStat.st_dev;.  
e63e0 20 20 69 64 2e 69 6e 6f 20 3d 20 73 53 74 61 74    id.ino = sStat
e63f0 2e 73 74 5f 69 6e 6f 3b 0a 0a 20 20 20 20 75 6e  .st_ino;..    un
e6400 69 78 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a  ixEnterMutex();.
e6410 20 20 20 20 66 6f 72 28 70 4f 3d 6f 70 65 6e 4c      for(pO=openL
e6420 69 73 74 3b 20 70 4f 20 26 26 20 6d 65 6d 63 6d  ist; pO && memcm
e6430 70 28 26 69 64 2c 20 26 70 4f 2d 3e 66 69 6c 65  p(&id, &pO->file
e6440 49 64 2c 20 73 69 7a 65 6f 66 28 69 64 29 29 3b  Id, sizeof(id));
e6450 20 70 4f 3d 70 4f 2d 3e 70 4e 65 78 74 29 3b 0a   pO=pO->pNext);.
e6460 20 20 20 20 69 66 28 20 70 4f 20 29 7b 0a 20 20      if( pO ){.  
e6470 20 20 20 20 55 6e 69 78 55 6e 75 73 65 64 46 64      UnixUnusedFd
e6480 20 2a 2a 70 70 3b 0a 20 20 20 20 20 20 66 6f 72   **pp;.      for
e6490 28 70 70 3d 26 70 4f 2d 3e 70 55 6e 75 73 65 64  (pp=&pO->pUnused
e64a0 3b 20 2a 70 70 20 26 26 20 28 2a 70 70 29 2d 3e  ; *pp && (*pp)->
e64b0 66 6c 61 67 73 21 3d 66 6c 61 67 73 3b 20 70 70  flags!=flags; pp
e64c0 3d 26 28 28 2a 70 70 29 2d 3e 70 4e 65 78 74 29  =&((*pp)->pNext)
e64d0 29 3b 0a 20 20 20 20 20 20 70 55 6e 75 73 65 64  );.      pUnused
e64e0 20 3d 20 2a 70 70 3b 0a 20 20 20 20 20 20 69 66   = *pp;.      if
e64f0 28 20 70 55 6e 75 73 65 64 20 29 7b 0a 20 20 20  ( pUnused ){.   
e6500 20 20 20 20 20 2a 70 70 20 3d 20 70 55 6e 75 73       *pp = pUnus
e6510 65 64 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  ed->pNext;.     
e6520 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 69   }.    }.    uni
e6530 78 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  xLeaveMutex();. 
e6540 20 7d 0a 23 65 6e 64 69 66 20 20 20 20 2f 2a 20   }.#endif    /* 
e6550 69 66 20 21 4f 53 5f 56 58 57 4f 52 4b 53 20 2a  if !OS_VXWORKS *
e6560 2f 0a 20 20 72 65 74 75 72 6e 20 70 55 6e 75 73  /.  return pUnus
e6570 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65  ed;.}../*.** Ope
e6580 6e 20 74 68 65 20 66 69 6c 65 20 7a 50 61 74 68  n the file zPath
e6590 2e 0a 2a 2a 20 0a 2a 2a 20 50 72 65 76 69 6f 75  ..** .** Previou
e65a0 73 6c 79 2c 20 74 68 65 20 53 51 4c 69 74 65 20  sly, the SQLite 
e65b0 4f 53 20 6c 61 79 65 72 20 75 73 65 64 20 74 68  OS layer used th
e65c0 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e  ree functions in
e65d0 20 70 6c 61 63 65 20 6f 66 20 74 68 69 73 0a 2a   place of this.*
e65e0 2a 20 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  * one:.**.**    
e65f0 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52 65   sqlite3OsOpenRe
e6600 61 64 57 72 69 74 65 28 29 3b 0a 2a 2a 20 20 20  adWrite();.**   
e6610 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 52    sqlite3OsOpenR
e6620 65 61 64 4f 6e 6c 79 28 29 3b 0a 2a 2a 20 20 20  eadOnly();.**   
e6630 20 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 45    sqlite3OsOpenE
e6640 78 63 6c 75 73 69 76 65 28 29 3b 0a 2a 2a 0a 2a  xclusive();.**.*
e6650 2a 20 54 68 65 73 65 20 63 61 6c 6c 73 20 63 6f  * These calls co
e6660 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20  rrespond to the 
e6670 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e  following combin
e6680 61 74 69 6f 6e 73 20 6f 66 20 66 6c 61 67 73 3a  ations of flags:
e6690 0a 2a 2a 0a 2a 2a 20 20 20 20 20 52 65 61 64 57  .**.**     ReadW
e66a0 72 69 74 65 28 29 20 2d 3e 20 20 20 20 20 28 52  rite() ->     (R
e66b0 45 41 44 57 52 49 54 45 20 7c 20 43 52 45 41 54  EADWRITE | CREAT
e66c0 45 29 0a 2a 2a 20 20 20 20 20 52 65 61 64 4f 6e  E).**     ReadOn
e66d0 6c 79 28 29 20 20 2d 3e 20 20 20 20 20 28 52 45  ly()  ->     (RE
e66e0 41 44 4f 4e 4c 59 29 20 0a 2a 2a 20 20 20 20 20  ADONLY) .**     
e66f0 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 20  OpenExclusive() 
e6700 2d 3e 20 28 52 45 41 44 57 52 49 54 45 20 7c 20  -> (READWRITE | 
e6710 43 52 45 41 54 45 20 7c 20 45 58 43 4c 55 53 49  CREATE | EXCLUSI
e6720 56 45 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c  VE).**.** The ol
e6730 64 20 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28  d OpenExclusive(
e6740 29 20 61 63 63 65 70 74 65 64 20 61 20 62 6f 6f  ) accepted a boo
e6750 6c 65 61 6e 20 61 72 67 75 6d 65 6e 74 20 2d 20  lean argument - 
e6760 22 64 65 6c 46 6c 61 67 22 2e 20 49 66 0a 2a 2a  "delFlag". If.**
e6770 20 74 72 75 65 2c 20 74 68 65 20 66 69 6c 65 20   true, the file 
e6780 77 61 73 20 63 6f 6e 66 69 67 75 72 65 64 20 74  was configured t
e6790 6f 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  o be automatical
e67a0 6c 79 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20  ly deleted when 
e67b0 74 68 65 0a 2a 2a 20 66 69 6c 65 20 68 61 6e 64  the.** file hand
e67c0 6c 65 20 63 6c 6f 73 65 64 2e 20 54 6f 20 61 63  le closed. To ac
e67d0 68 69 65 76 65 20 74 68 65 20 73 61 6d 65 20 65  hieve the same e
e67e0 66 66 65 63 74 20 75 73 69 6e 67 20 74 68 69 73  ffect using this
e67f0 20 6e 65 77 20 0a 2a 2a 20 69 6e 74 65 72 66 61   new .** interfa
e6800 63 65 2c 20 61 64 64 20 74 68 65 20 44 45 4c 45  ce, add the DELE
e6810 54 45 4f 4e 43 4c 4f 53 45 20 66 6c 61 67 20 74  TEONCLOSE flag t
e6820 6f 20 74 68 6f 73 65 20 73 70 65 63 69 66 69 65  o those specifie
e6830 64 20 61 62 6f 76 65 20 66 6f 72 20 0a 2a 2a 20  d above for .** 
e6840 4f 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 2e  OpenExclusive().
e6850 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
e6860 6e 69 78 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74  nixOpen(.  sqlit
e6870 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20  e3_vfs *pVfs,   
e6880 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
e6890 46 53 20 66 6f 72 20 77 68 69 63 68 20 74 68 69  FS for which thi
e68a0 73 20 69 73 20 74 68 65 20 78 4f 70 65 6e 20 6d  s is the xOpen m
e68b0 65 74 68 6f 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  ethod */.  const
e68c0 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 20 20   char *zPath,   
e68d0 20 20 20 20 20 20 20 20 2f 2a 20 50 61 74 68 6e          /* Pathn
e68e0 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 62  ame of file to b
e68f0 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20 73 71  e opened */.  sq
e6900 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c  lite3_file *pFil
e6910 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  e,         /* Th
e6920 65 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  e file descripto
e6930 72 20 74 6f 20 62 65 20 66 69 6c 6c 65 64 20 69  r to be filled i
e6940 6e 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  n */.  int flags
e6950 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e6960 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 66 6c 61      /* Input fla
e6970 67 73 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 74 68  gs to control th
e6980 65 20 6f 70 65 6e 69 6e 67 20 2a 2f 0a 20 20 69  e opening */.  i
e6990 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20  nt *pOutFlags   
e69a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
e69b0 75 74 70 75 74 20 66 6c 61 67 73 20 72 65 74 75  utput flags retu
e69c0 72 6e 65 64 20 74 6f 20 53 51 4c 69 74 65 20 63  rned to SQLite c
e69d0 6f 72 65 20 2a 2f 0a 29 7b 0a 20 20 75 6e 69 78  ore */.){.  unix
e69e0 46 69 6c 65 20 2a 70 20 3d 20 28 75 6e 69 78 46  File *p = (unixF
e69f0 69 6c 65 20 2a 29 70 46 69 6c 65 3b 0a 20 20 69  ile *)pFile;.  i
e6a00 6e 74 20 66 64 20 3d 20 2d 31 3b 20 20 20 20 20  nt fd = -1;     
e6a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e6a20 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   File descriptor
e6a30 20 72 65 74 75 72 6e 65 64 20 62 79 20 6f 70 65   returned by ope
e6a40 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72  n() */.  int dir
e6a50 66 64 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20  fd = -1;        
e6a60 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72 65 63          /* Direc
e6a70 74 6f 72 79 20 66 69 6c 65 20 64 65 73 63 72 69  tory file descri
e6a80 70 74 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 70  ptor */.  int op
e6a90 65 6e 46 6c 61 67 73 20 3d 20 30 3b 20 20 20 20  enFlags = 0;    
e6aa0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67           /* Flag
e6ab0 73 20 74 6f 20 70 61 73 73 20 74 6f 20 6f 70 65  s to pass to ope
e6ac0 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79  n() */.  int eTy
e6ad0 70 65 20 3d 20 66 6c 61 67 73 26 30 78 46 46 46  pe = flags&0xFFF
e6ae0 46 46 46 30 30 3b 20 20 2f 2a 20 54 79 70 65 20  FFF00;  /* Type 
e6af0 6f 66 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20  of file to open 
e6b00 2a 2f 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 3b  */.  int noLock;
e6b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e6b20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f      /* True to o
e6b30 6d 69 74 20 6c 6f 63 6b 69 6e 67 20 70 72 69 6d  mit locking prim
e6b40 69 74 69 76 65 73 20 2a 2f 0a 20 20 69 6e 74 20  itives */.  int 
e6b50 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
e6b60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 75             /* Fu
e6b70 6e 63 74 69 6f 6e 20 52 65 74 75 72 6e 20 43 6f  nction Return Co
e6b80 64 65 20 2a 2f 0a 0a 20 20 69 6e 74 20 69 73 45  de */..  int isE
e6b90 78 63 6c 75 73 69 76 65 20 20 3d 20 28 66 6c 61  xclusive  = (fla
e6ba0 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
e6bb0 5f 45 58 43 4c 55 53 49 56 45 29 3b 0a 20 20 69  _EXCLUSIVE);.  i
e6bc0 6e 74 20 69 73 44 65 6c 65 74 65 20 20 20 20 20  nt isDelete     
e6bd0 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54  = (flags & SQLIT
e6be0 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43  E_OPEN_DELETEONC
e6bf0 4c 4f 53 45 29 3b 0a 20 20 69 6e 74 20 69 73 43  LOSE);.  int isC
e6c00 72 65 61 74 65 20 20 20 20 20 3d 20 28 66 6c 61  reate     = (fla
e6c10 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
e6c20 5f 43 52 45 41 54 45 29 3b 0a 20 20 69 6e 74 20  _CREATE);.  int 
e6c30 69 73 52 65 61 64 6f 6e 6c 79 20 20 20 3d 20 28  isReadonly   = (
e6c40 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
e6c50 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 3b 0a 20  PEN_READONLY);. 
e6c60 20 69 6e 74 20 69 73 52 65 61 64 57 72 69 74 65   int isReadWrite
e6c70 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c    = (flags & SQL
e6c80 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
e6c90 54 45 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 63 72  TE);..  /* If cr
e6ca0 65 61 74 69 6e 67 20 61 20 6d 61 73 74 65 72 20  eating a master 
e6cb0 6f 72 20 6d 61 69 6e 2d 66 69 6c 65 20 6a 6f 75  or main-file jou
e6cc0 72 6e 61 6c 2c 20 74 68 69 73 20 66 75 6e 63 74  rnal, this funct
e6cd0 69 6f 6e 20 77 69 6c 6c 20 6f 70 65 6e 0a 20 20  ion will open.  
e6ce0 2a 2a 20 61 20 66 69 6c 65 2d 64 65 73 63 72 69  ** a file-descri
e6cf0 70 74 6f 72 20 6f 6e 20 74 68 65 20 64 69 72 65  ptor on the dire
e6d00 63 74 6f 72 79 20 74 6f 6f 2e 20 54 68 65 20 66  ctory too. The f
e6d10 69 72 73 74 20 74 69 6d 65 20 75 6e 69 78 53 79  irst time unixSy
e6d20 6e 63 28 29 0a 20 20 2a 2a 20 69 73 20 63 61 6c  nc().  ** is cal
e6d30 6c 65 64 20 74 68 65 20 64 69 72 65 63 74 6f 72  led the director
e6d40 79 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  y file descripto
e6d50 72 20 77 69 6c 6c 20 62 65 20 66 73 79 6e 63 28  r will be fsync(
e6d60 29 65 64 20 61 6e 64 20 63 6c 6f 73 65 28 29 64  )ed and close()d
e6d70 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f  ..  */.  int isO
e6d80 70 65 6e 44 69 72 65 63 74 6f 72 79 20 3d 20 28  penDirectory = (
e6d90 69 73 43 72 65 61 74 65 20 26 26 20 0a 20 20 20  isCreate && .   
e6da0 20 20 20 28 65 54 79 70 65 3d 3d 53 51 4c 49 54     (eType==SQLIT
e6db0 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f  E_OPEN_MASTER_JO
e6dc0 55 52 4e 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d  URNAL || eType==
e6dd0 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
e6de0 5f 4a 4f 55 52 4e 41 4c 29 0a 20 20 29 3b 0a 0a  _JOURNAL).  );..
e6df0 20 20 2f 2a 20 49 66 20 61 72 67 75 6d 65 6e 74    /* If argument
e6e00 20 7a 50 61 74 68 20 69 73 20 61 20 4e 55 4c 4c   zPath is a NULL
e6e10 20 70 6f 69 6e 74 65 72 2c 20 74 68 69 73 20 66   pointer, this f
e6e20 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 71 75 69  unction is requi
e6e30 72 65 64 20 74 6f 20 6f 70 65 6e 0a 20 20 2a 2a  red to open.  **
e6e40 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c   a temporary fil
e6e50 65 2e 20 55 73 65 20 74 68 69 73 20 62 75 66 66  e. Use this buff
e6e60 65 72 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  er to store the 
e6e70 66 69 6c 65 20 6e 61 6d 65 20 69 6e 2e 0a 20 20  file name in..  
e6e80 2a 2f 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61  */.  char zTmpna
e6e90 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b  me[MAX_PATHNAME+
e6ea0 31 5d 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  1];.  const char
e6eb0 20 2a 7a 4e 61 6d 65 20 3d 20 7a 50 61 74 68 3b   *zName = zPath;
e6ec0 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 65  ..  /* Check the
e6ed0 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65   following state
e6ee0 6d 65 6e 74 73 20 61 72 65 20 74 72 75 65 3a 20  ments are true: 
e6ef0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 28 61 29  .  **.  **   (a)
e6f00 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66 20   Exactly one of 
e6f10 74 68 65 20 52 45 41 44 57 52 49 54 45 20 61 6e  the READWRITE an
e6f20 64 20 52 45 41 44 4f 4e 4c 59 20 66 6c 61 67 73  d READONLY flags
e6f30 20 6d 75 73 74 20 62 65 20 73 65 74 2c 20 61 6e   must be set, an
e6f40 64 20 0a 20 20 2a 2a 20 20 20 28 62 29 20 69 66  d .  **   (b) if
e6f50 20 43 52 45 41 54 45 20 69 73 20 73 65 74 2c 20   CREATE is set, 
e6f60 74 68 65 6e 20 52 45 41 44 57 52 49 54 45 20 6d  then READWRITE m
e6f70 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65 74 2c  ust also be set,
e6f80 20 61 6e 64 0a 20 20 2a 2a 20 20 20 28 63 29 20   and.  **   (c) 
e6f90 69 66 20 45 58 43 4c 55 53 49 56 45 20 69 73 20  if EXCLUSIVE is 
e6fa0 73 65 74 2c 20 74 68 65 6e 20 43 52 45 41 54 45  set, then CREATE
e6fb0 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 73 65   must also be se
e6fc0 74 2e 0a 20 20 2a 2a 20 20 20 28 64 29 20 69 66  t..  **   (d) if
e6fd0 20 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 69   DELETEONCLOSE i
e6fe0 73 20 73 65 74 2c 20 74 68 65 6e 20 43 52 45 41  s set, then CREA
e6ff0 54 45 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20  TE must also be 
e7000 73 65 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  set..  */.  asse
e7010 72 74 28 28 69 73 52 65 61 64 6f 6e 6c 79 3d 3d  rt((isReadonly==
e7020 30 20 7c 7c 20 69 73 52 65 61 64 57 72 69 74 65  0 || isReadWrite
e7030 3d 3d 30 29 20 26 26 20 28 69 73 52 65 61 64 57  ==0) && (isReadW
e7040 72 69 74 65 20 7c 7c 20 69 73 52 65 61 64 6f 6e  rite || isReadon
e7050 6c 79 29 29 3b 0a 20 20 61 73 73 65 72 74 28 69  ly));.  assert(i
e7060 73 43 72 65 61 74 65 3d 3d 30 20 7c 7c 20 69 73  sCreate==0 || is
e7070 52 65 61 64 57 72 69 74 65 29 3b 0a 20 20 61 73  ReadWrite);.  as
e7080 73 65 72 74 28 69 73 45 78 63 6c 75 73 69 76 65  sert(isExclusive
e7090 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61 74 65 29  ==0 || isCreate)
e70a0 3b 0a 20 20 61 73 73 65 72 74 28 69 73 44 65 6c  ;.  assert(isDel
e70b0 65 74 65 3d 3d 30 20 7c 7c 20 69 73 43 72 65 61  ete==0 || isCrea
e70c0 74 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6d  te);..  /* The m
e70d0 61 69 6e 20 44 42 2c 20 6d 61 69 6e 20 6a 6f 75  ain DB, main jou
e70e0 72 6e 61 6c 2c 20 61 6e 64 20 6d 61 73 74 65 72  rnal, and master
e70f0 20 6a 6f 75 72 6e 61 6c 20 61 72 65 20 6e 65 76   journal are nev
e7100 65 72 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  er automatically
e7110 0a 20 20 2a 2a 20 64 65 6c 65 74 65 64 2e 20 4e  .  ** deleted. N
e7120 6f 72 20 61 72 65 20 74 68 65 79 20 65 76 65 72  or are they ever
e7130 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73   temporary files
e7140 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
e7150 28 21 69 73 44 65 6c 65 74 65 20 26 26 20 7a 4e  (!isDelete && zN
e7160 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 21 3d 53  ame) || eType!=S
e7170 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
e7180 44 42 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  DB );.  assert( 
e7190 28 21 69 73 44 65 6c 65 74 65 20 26 26 20 7a 4e  (!isDelete && zN
e71a0 61 6d 65 29 20 7c 7c 20 65 54 79 70 65 21 3d 53  ame) || eType!=S
e71b0 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f  QLITE_OPEN_MAIN_
e71c0 4a 4f 55 52 4e 41 4c 20 29 3b 0a 20 20 61 73 73  JOURNAL );.  ass
e71d0 65 72 74 28 20 28 21 69 73 44 65 6c 65 74 65 20  ert( (!isDelete 
e71e0 26 26 20 7a 4e 61 6d 65 29 20 7c 7c 20 65 54 79  && zName) || eTy
e71f0 70 65 21 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  pe!=SQLITE_OPEN_
e7200 4d 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 29  MASTER_JOURNAL )
e7210 3b 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74  ;..  /* Assert t
e7220 68 61 74 20 74 68 65 20 75 70 70 65 72 20 6c 61  hat the upper la
e7230 79 65 72 20 68 61 73 20 73 65 74 20 6f 6e 65 20  yer has set one 
e7240 6f 66 20 74 68 65 20 22 66 69 6c 65 2d 74 79 70  of the "file-typ
e7250 65 22 20 66 6c 61 67 73 2e 20 2a 2f 0a 20 20 61  e" flags. */.  a
e7260 73 73 65 72 74 28 20 65 54 79 70 65 3d 3d 53 51  ssert( eType==SQ
e7270 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44  LITE_OPEN_MAIN_D
e7280 42 20 20 20 20 20 20 7c 7c 20 65 54 79 70 65 3d  B      || eType=
e7290 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d  =SQLITE_OPEN_TEM
e72a0 50 5f 44 42 20 0a 20 20 20 20 20 20 20 7c 7c 20  P_DB .       || 
e72b0 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50  eType==SQLITE_OP
e72c0 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20  EN_MAIN_JOURNAL 
e72d0 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45  || eType==SQLITE
e72e0 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52 4e  _OPEN_TEMP_JOURN
e72f0 41 4c 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54  AL .       || eT
e7300 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
e7310 5f 53 55 42 4a 4f 55 52 4e 41 4c 20 20 20 7c 7c  _SUBJOURNAL   ||
e7320 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f   eType==SQLITE_O
e7330 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e  PEN_MASTER_JOURN
e7340 41 4c 20 0a 20 20 20 20 20 20 20 7c 7c 20 65 54  AL .       || eT
e7350 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
e7360 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 0a 20 20  _TRANSIENT_DB.  
e7370 29 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20  );..  memset(p, 
e7380 30 2c 20 73 69 7a 65 6f 66 28 75 6e 69 78 46 69  0, sizeof(unixFi
e7390 6c 65 29 29 3b 0a 0a 20 20 69 66 28 20 65 54 79  le));..  if( eTy
e73a0 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  pe==SQLITE_OPEN_
e73b0 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 55  MAIN_DB ){.    U
e73c0 6e 69 78 55 6e 75 73 65 64 46 64 20 2a 70 55 6e  nixUnusedFd *pUn
e73d0 75 73 65 64 3b 0a 20 20 20 20 70 55 6e 75 73 65  used;.    pUnuse
e73e0 64 20 3d 20 66 69 6e 64 52 65 75 73 61 62 6c 65  d = findReusable
e73f0 46 64 28 7a 4e 61 6d 65 2c 20 66 6c 61 67 73 29  Fd(zName, flags)
e7400 3b 0a 20 20 20 20 69 66 28 20 70 55 6e 75 73 65  ;.    if( pUnuse
e7410 64 20 29 7b 0a 20 20 20 20 20 20 66 64 20 3d 20  d ){.      fd = 
e7420 70 55 6e 75 73 65 64 2d 3e 66 64 3b 0a 20 20 20  pUnused->fd;.   
e7430 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 55   }else{.      pU
e7440 6e 75 73 65 64 20 3d 20 73 71 6c 69 74 65 33 5f  nused = sqlite3_
e7450 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 70  malloc(sizeof(*p
e7460 55 6e 75 73 65 64 29 29 3b 0a 20 20 20 20 20 20  Unused));.      
e7470 69 66 28 20 21 70 55 6e 75 73 65 64 20 29 7b 0a  if( !pUnused ){.
e7480 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
e7490 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
e74a0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70     }.    }.    p
e74b0 2d 3e 70 55 6e 75 73 65 64 20 3d 20 70 55 6e 75  ->pUnused = pUnu
e74c0 73 65 64 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  sed;.  }else if(
e74d0 20 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 2f   !zName ){.    /
e74e0 2a 20 49 66 20 7a 4e 61 6d 65 20 69 73 20 4e 55  * If zName is NU
e74f0 4c 4c 2c 20 74 68 65 20 75 70 70 65 72 20 6c 61  LL, the upper la
e7500 79 65 72 20 69 73 20 72 65 71 75 65 73 74 69 6e  yer is requestin
e7510 67 20 61 20 74 65 6d 70 20 66 69 6c 65 2e 20 2a  g a temp file. *
e7520 2f 0a 20 20 20 20 61 73 73 65 72 74 28 69 73 44  /.    assert(isD
e7530 65 6c 65 74 65 20 26 26 20 21 69 73 4f 70 65 6e  elete && !isOpen
e7540 44 69 72 65 63 74 6f 72 79 29 3b 0a 20 20 20 20  Directory);.    
e7550 72 63 20 3d 20 67 65 74 54 65 6d 70 6e 61 6d 65  rc = getTempname
e7560 28 4d 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 2c  (MAX_PATHNAME+1,
e7570 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20 20   zTmpname);.    
e7580 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
e7590 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  K ){.      retur
e75a0 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
e75b0 7a 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d 65  zName = zTmpname
e75c0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 65  ;.  }..  /* Dete
e75d0 72 6d 69 6e 65 20 74 68 65 20 76 61 6c 75 65 20  rmine the value 
e75e0 6f 66 20 74 68 65 20 66 6c 61 67 73 20 70 61 72  of the flags par
e75f0 61 6d 65 74 65 72 20 70 61 73 73 65 64 20 74 6f  ameter passed to
e7600 20 50 4f 53 49 58 20 66 75 6e 63 74 69 6f 6e 0a   POSIX function.
e7610 20 20 2a 2a 20 6f 70 65 6e 28 29 2e 20 54 68 65    ** open(). The
e7620 73 65 20 6d 75 73 74 20 62 65 20 63 61 6c 63 75  se must be calcu
e7630 6c 61 74 65 64 20 65 76 65 6e 20 69 66 20 6f 70  lated even if op
e7640 65 6e 28 29 20 69 73 20 6e 6f 74 20 63 61 6c 6c  en() is not call
e7650 65 64 2c 20 61 73 0a 20 20 2a 2a 20 74 68 65 79  ed, as.  ** they
e7660 20 6d 61 79 20 62 65 20 73 74 6f 72 65 64 20 61   may be stored a
e7670 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 66 69  s part of the fi
e7680 6c 65 20 68 61 6e 64 6c 65 20 61 6e 64 20 75 73  le handle and us
e7690 65 64 20 62 79 20 74 68 65 20 0a 20 20 2a 2a 20  ed by the .  ** 
e76a0 27 63 6f 6e 63 68 20 66 69 6c 65 27 20 6c 6f 63  'conch file' loc
e76b0 6b 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 20 6c  king functions l
e76c0 61 74 65 72 20 6f 6e 2e 20 20 2a 2f 0a 20 20 69  ater on.  */.  i
e76d0 66 28 20 69 73 52 65 61 64 6f 6e 6c 79 20 29 20  f( isReadonly ) 
e76e0 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f   openFlags |= O_
e76f0 52 44 4f 4e 4c 59 3b 0a 20 20 69 66 28 20 69 73  RDONLY;.  if( is
e7700 52 65 61 64 57 72 69 74 65 20 29 20 6f 70 65 6e  ReadWrite ) open
e7710 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 57 52 3b  Flags |= O_RDWR;
e7720 0a 20 20 69 66 28 20 69 73 43 72 65 61 74 65 20  .  if( isCreate 
e7730 29 20 20 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c  )    openFlags |
e7740 3d 20 4f 5f 43 52 45 41 54 3b 0a 20 20 69 66 28  = O_CREAT;.  if(
e7750 20 69 73 45 78 63 6c 75 73 69 76 65 20 29 20 6f   isExclusive ) o
e7760 70 65 6e 46 6c 61 67 73 20 7c 3d 20 28 4f 5f 45  penFlags |= (O_E
e7770 58 43 4c 7c 4f 5f 4e 4f 46 4f 4c 4c 4f 57 29 3b  XCL|O_NOFOLLOW);
e7780 0a 20 20 6f 70 65 6e 46 6c 61 67 73 20 7c 3d 20  .  openFlags |= 
e7790 28 4f 5f 4c 41 52 47 45 46 49 4c 45 7c 4f 5f 42  (O_LARGEFILE|O_B
e77a0 49 4e 41 52 59 29 3b 0a 0a 20 20 69 66 28 20 66  INARY);..  if( f
e77b0 64 3c 30 20 29 7b 0a 20 20 20 20 6d 6f 64 65 5f  d<0 ){.    mode_
e77c0 74 20 6f 70 65 6e 4d 6f 64 65 20 3d 20 28 69 73  t openMode = (is
e77d0 44 65 6c 65 74 65 3f 30 36 30 30 3a 53 51 4c 49  Delete?0600:SQLI
e77e0 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45 5f  TE_DEFAULT_FILE_
e77f0 50 45 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20 20  PERMISSIONS);.  
e7800 20 20 66 64 20 3d 20 6f 70 65 6e 28 7a 4e 61 6d    fd = open(zNam
e7810 65 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 20 6f 70  e, openFlags, op
e7820 65 6e 4d 6f 64 65 29 3b 0a 20 20 20 20 4f 53 54  enMode);.    OST
e7830 52 41 43 45 34 28 22 4f 50 45 4e 58 20 20 20 25  RACE4("OPENX   %
e7840 2d 33 64 20 25 73 20 30 25 6f 5c 6e 22 2c 20 66  -3d %s 0%o\n", f
e7850 64 2c 20 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46 6c  d, zName, openFl
e7860 61 67 73 29 3b 0a 20 20 20 20 69 66 28 20 66 64  ags);.    if( fd
e7870 3c 30 20 26 26 20 65 72 72 6e 6f 21 3d 45 49 53  <0 && errno!=EIS
e7880 44 49 52 20 26 26 20 69 73 52 65 61 64 57 72 69  DIR && isReadWri
e7890 74 65 20 26 26 20 21 69 73 45 78 63 6c 75 73 69  te && !isExclusi
e78a0 76 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 46  ve ){.      /* F
e78b0 61 69 6c 65 64 20 74 6f 20 6f 70 65 6e 20 74 68  ailed to open th
e78c0 65 20 66 69 6c 65 20 66 6f 72 20 72 65 61 64 2f  e file for read/
e78d0 77 72 69 74 65 20 61 63 63 65 73 73 2e 20 54 72  write access. Tr
e78e0 79 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 2a 2f 0a  y read-only. */.
e78f0 20 20 20 20 20 20 66 6c 61 67 73 20 26 3d 20 7e        flags &= ~
e7900 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  (SQLITE_OPEN_REA
e7910 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50  DWRITE|SQLITE_OP
e7920 45 4e 5f 43 52 45 41 54 45 29 3b 0a 20 20 20 20  EN_CREATE);.    
e7930 20 20 6f 70 65 6e 46 6c 61 67 73 20 26 3d 20 7e    openFlags &= ~
e7940 28 4f 5f 52 44 57 52 7c 4f 5f 43 52 45 41 54 29  (O_RDWR|O_CREAT)
e7950 3b 0a 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d  ;.      flags |=
e7960 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
e7970 44 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 6f 70 65  DONLY;.      ope
e7980 6e 46 6c 61 67 73 20 7c 3d 20 4f 5f 52 44 4f 4e  nFlags |= O_RDON
e7990 4c 59 3b 0a 20 20 20 20 20 20 66 64 20 3d 20 6f  LY;.      fd = o
e79a0 70 65 6e 28 7a 4e 61 6d 65 2c 20 6f 70 65 6e 46  pen(zName, openF
e79b0 6c 61 67 73 2c 20 6f 70 65 6e 4d 6f 64 65 29 3b  lags, openMode);
e79c0 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 66  .    }.    if( f
e79d0 64 3c 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  d<0 ){.      rc 
e79e0 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45  = SQLITE_CANTOPE
e79f0 4e 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70  N;.      goto op
e7a00 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a 20 20 20  en_finished;.   
e7a10 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28   }.  }.  assert(
e7a20 20 66 64 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20   fd>=0 );.  if( 
e7a30 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a 20 20 20  pOutFlags ){.   
e7a40 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c   *pOutFlags = fl
e7a50 61 67 73 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  ags;.  }..  if( 
e7a60 70 2d 3e 70 55 6e 75 73 65 64 20 29 7b 0a 20 20  p->pUnused ){.  
e7a70 20 20 70 2d 3e 70 55 6e 75 73 65 64 2d 3e 66 64    p->pUnused->fd
e7a80 20 3d 20 66 64 3b 0a 20 20 20 20 70 2d 3e 70 55   = fd;.    p->pU
e7a90 6e 75 73 65 64 2d 3e 66 6c 61 67 73 20 3d 20 66  nused->flags = f
e7aa0 6c 61 67 73 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  lags;.  }..  if(
e7ab0 20 69 73 44 65 6c 65 74 65 20 29 7b 0a 23 69 66   isDelete ){.#if
e7ac0 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20   OS_VXWORKS.    
e7ad0 7a 50 61 74 68 20 3d 20 7a 4e 61 6d 65 3b 0a 23  zPath = zName;.#
e7ae0 65 6c 73 65 0a 20 20 20 20 75 6e 6c 69 6e 6b 28  else.    unlink(
e7af0 7a 4e 61 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20  zName);.#endif. 
e7b00 20 7d 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e   }.#if SQLITE_EN
e7b10 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
e7b20 4c 45 0a 20 20 65 6c 73 65 7b 0a 20 20 20 20 70  LE.  else{.    p
e7b30 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 3d 20 6f 70  ->openFlags = op
e7b40 65 6e 46 6c 61 67 73 3b 0a 20 20 7d 0a 23 65 6e  enFlags;.  }.#en
e7b50 64 69 66 0a 0a 20 20 69 66 28 20 69 73 4f 70 65  dif..  if( isOpe
e7b60 6e 44 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20  nDirectory ){.  
e7b70 20 20 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63    rc = openDirec
e7b80 74 6f 72 79 28 7a 50 61 74 68 2c 20 26 64 69 72  tory(zPath, &dir
e7b90 66 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  fd);.    if( rc!
e7ba0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
e7bb0 20 20 20 20 2f 2a 20 49 74 20 69 73 20 73 61 66      /* It is saf
e7bc0 65 20 74 6f 20 63 6c 6f 73 65 20 66 64 20 61 74  e to close fd at
e7bd0 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 62 65 63   this point, bec
e7be0 61 75 73 65 20 69 74 20 69 73 20 67 75 61 72 61  ause it is guara
e7bf0 6e 74 65 65 64 20 6e 6f 74 0a 20 20 20 20 20 20  nteed not.      
e7c00 2a 2a 20 74 6f 20 62 65 20 6f 70 65 6e 20 6f 6e  ** to be open on
e7c10 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65   a database file
e7c20 2e 20 49 66 20 69 74 20 77 65 72 65 20 6f 70 65  . If it were ope
e7c30 6e 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20  n on a database 
e7c40 66 69 6c 65 2c 0a 20 20 20 20 20 20 2a 2a 20 69  file,.      ** i
e7c50 74 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 20 73  t would not be s
e7c60 61 66 65 20 74 6f 20 63 6c 6f 73 65 20 61 73 20  afe to close as 
e7c70 74 68 69 73 20 77 6f 75 6c 64 20 72 65 6c 65 61  this would relea
e7c80 73 65 20 61 6e 79 20 6c 6f 63 6b 73 20 68 65 6c  se any locks hel
e7c90 64 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 20 74 68  d.      ** on th
e7ca0 65 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 70  e file by this p
e7cb0 72 6f 63 65 73 73 2e 20 20 2a 2f 0a 20 20 20 20  rocess.  */.    
e7cc0 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21    assert( eType!
e7cd0 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49  =SQLITE_OPEN_MAI
e7ce0 4e 5f 44 42 20 29 3b 0a 20 20 20 20 20 20 63 6c  N_DB );.      cl
e7cf0 6f 73 65 28 66 64 29 3b 20 20 20 20 20 20 20 20  ose(fd);        
e7d00 20 20 20 20 20 2f 2a 20 73 69 6c 65 6e 74 6c 79       /* silently
e7d10 20 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 61   leak if fail, a
e7d20 6c 72 65 61 64 79 20 69 6e 20 65 72 72 6f 72 20  lready in error 
e7d30 2a 2f 0a 20 20 20 20 20 20 67 6f 74 6f 20 6f 70  */.      goto op
e7d40 65 6e 5f 66 69 6e 69 73 68 65 64 3b 0a 20 20 20  en_finished;.   
e7d50 20 7d 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 46   }.  }..#ifdef F
e7d60 44 5f 43 4c 4f 45 58 45 43 0a 20 20 66 63 6e 74  D_CLOEXEC.  fcnt
e7d70 6c 28 66 64 2c 20 46 5f 53 45 54 46 44 2c 20 66  l(fd, F_SETFD, f
e7d80 63 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 46 44  cntl(fd, F_GETFD
e7d90 2c 20 30 29 20 7c 20 46 44 5f 43 4c 4f 45 58 45  , 0) | FD_CLOEXE
e7da0 43 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 6e 6f  C);.#endif..  no
e7db0 4c 6f 63 6b 20 3d 20 65 54 79 70 65 21 3d 53 51  Lock = eType!=SQ
e7dc0 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44  LITE_OPEN_MAIN_D
e7dd0 42 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 50  B;..#if SQLITE_P
e7de0 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43 4b  REFER_PROXY_LOCK
e7df0 49 4e 47 0a 20 20 69 66 28 20 7a 50 61 74 68 21  ING.  if( zPath!
e7e00 3d 4e 55 4c 4c 20 26 26 20 21 6e 6f 4c 6f 63 6b  =NULL && !noLock
e7e10 20 26 26 20 70 56 66 73 2d 3e 78 4f 70 65 6e 20   && pVfs->xOpen 
e7e20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 65 6e 76  ){.    char *env
e7e30 66 6f 72 63 65 20 3d 20 67 65 74 65 6e 76 28 22  force = getenv("
e7e40 53 51 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f  SQLITE_FORCE_PRO
e7e50 58 59 5f 4c 4f 43 4b 49 4e 47 22 29 3b 0a 20 20  XY_LOCKING");.  
e7e60 20 20 69 6e 74 20 75 73 65 50 72 6f 78 79 20 3d    int useProxy =
e7e70 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 53 51 4c 49   0;..    /* SQLI
e7e80 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c  TE_FORCE_PROXY_L
e7e90 4f 43 4b 49 4e 47 3d 3d 31 20 6d 65 61 6e 73 20  OCKING==1 means 
e7ea0 66 6f 72 63 65 20 61 6c 77 61 79 73 20 75 73 65  force always use
e7eb0 20 70 72 6f 78 79 2c 20 30 20 6d 65 61 6e 73 20   proxy, 0 means 
e7ec0 0a 20 20 20 20 2a 2a 20 6e 65 76 65 72 20 75 73  .    ** never us
e7ed0 65 20 70 72 6f 78 79 2c 20 4e 55 4c 4c 20 6d 65  e proxy, NULL me
e7ee0 61 6e 73 20 75 73 65 20 70 72 6f 78 79 20 66 6f  ans use proxy fo
e7ef0 72 20 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65  r non-local file
e7f00 73 20 6f 6e 6c 79 2e 20 20 2a 2f 0a 20 20 20 20  s only.  */.    
e7f10 69 66 28 20 65 6e 76 66 6f 72 63 65 21 3d 4e 55  if( envforce!=NU
e7f20 4c 4c 20 29 7b 0a 20 20 20 20 20 20 75 73 65 50  LL ){.      useP
e7f30 72 6f 78 79 20 3d 20 61 74 6f 69 28 65 6e 76 66  roxy = atoi(envf
e7f40 6f 72 63 65 29 3e 30 3b 0a 20 20 20 20 7d 65 6c  orce)>0;.    }el
e7f50 73 65 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74  se{.      struct
e7f60 20 73 74 61 74 66 73 20 66 73 49 6e 66 6f 3b 0a   statfs fsInfo;.
e7f70 20 20 20 20 20 20 69 66 28 20 73 74 61 74 66 73        if( statfs
e7f80 28 7a 50 61 74 68 2c 20 26 66 73 49 6e 66 6f 29  (zPath, &fsInfo)
e7f90 20 3d 3d 20 2d 31 20 29 7b 0a 20 20 20 20 20 20   == -1 ){.      
e7fa0 20 20 2f 2a 20 49 6e 20 74 68 65 6f 72 79 2c 20    /* In theory, 
e7fb0 74 68 65 20 63 6c 6f 73 65 28 66 64 29 20 63 61  the close(fd) ca
e7fc0 6c 6c 20 69 73 20 73 75 62 2d 6f 70 74 69 6d 61  ll is sub-optima
e7fd0 6c 2e 20 49 66 20 74 68 65 20 66 69 6c 65 20 6f  l. If the file o
e7fe0 70 65 6e 65 64 0a 20 20 20 20 20 20 20 20 2a 2a  pened.        **
e7ff0 20 77 69 74 68 20 66 64 20 69 73 20 61 20 64 61   with fd is a da
e8000 74 61 62 61 73 65 20 66 69 6c 65 2c 20 61 6e 64  tabase file, and
e8010 20 74 68 65 72 65 20 61 72 65 20 6f 74 68 65 72   there are other
e8020 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f 70 65   connections ope
e8030 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20  n.        ** on 
e8040 74 68 61 74 20 66 69 6c 65 20 74 68 61 74 20 61  that file that a
e8050 72 65 20 63 75 72 72 65 6e 74 6c 79 20 68 6f 6c  re currently hol
e8060 64 69 6e 67 20 61 64 76 69 73 6f 72 79 20 6c 6f  ding advisory lo
e8070 63 6b 73 20 6f 6e 20 69 74 2c 0a 20 20 20 20 20  cks on it,.     
e8080 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 63     ** then the c
e8090 61 6c 6c 20 74 6f 20 63 6c 6f 73 65 28 29 20 77  all to close() w
e80a0 69 6c 6c 20 63 61 6e 63 65 6c 20 74 68 6f 73 65  ill cancel those
e80b0 20 6c 6f 63 6b 73 2e 20 49 6e 20 70 72 61 63 74   locks. In pract
e80c0 69 63 65 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20  ice,.        ** 
e80d0 77 65 27 72 65 20 61 73 73 75 6d 69 6e 67 20 74  we're assuming t
e80e0 68 61 74 20 73 74 61 74 66 73 28 29 20 64 6f 65  hat statfs() doe
e80f0 73 6e 27 74 20 66 61 69 6c 20 76 65 72 79 20 6f  sn't fail very o
e8100 66 74 65 6e 2e 20 41 74 20 6c 65 61 73 74 0a 20  ften. At least. 
e8110 20 20 20 20 20 20 20 2a 2a 20 6e 6f 74 20 77 68         ** not wh
e8120 69 6c 65 20 6f 74 68 65 72 20 66 69 6c 65 20 64  ile other file d
e8130 65 73 63 72 69 70 74 6f 72 73 20 6f 70 65 6e 65  escriptors opene
e8140 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 70 72  d by the same pr
e8150 6f 63 65 73 73 20 6f 6e 0a 20 20 20 20 20 20 20  ocess on.       
e8160 20 2a 2a 20 74 68 65 20 73 61 6d 65 20 66 69 6c   ** the same fil
e8170 65 20 61 72 65 20 77 6f 72 6b 69 6e 67 2e 20 20  e are working.  
e8180 2a 2f 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c 61  */.        p->la
e8190 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b  stErrno = errno;
e81a0 0a 20 20 20 20 20 20 20 20 69 66 28 20 64 69 72  .        if( dir
e81b0 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  fd>=0 ){.       
e81c0 20 20 20 63 6c 6f 73 65 28 64 69 72 66 64 29 3b     close(dirfd);
e81d0 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61   /* silently lea
e81e0 6b 20 69 66 20 66 61 69 6c 2c 20 69 6e 20 65 72  k if fail, in er
e81f0 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 7d  ror */.        }
e8200 0a 20 20 20 20 20 20 20 20 63 6c 6f 73 65 28 66  .        close(f
e8210 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20  d); /* silently 
e8220 6c 65 61 6b 20 69 66 20 66 61 69 6c 2c 20 69 6e  leak if fail, in
e8230 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 20 20   error */.      
e8240 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f    rc = SQLITE_IO
e8250 45 52 52 5f 41 43 43 45 53 53 3b 0a 20 20 20 20  ERR_ACCESS;.    
e8260 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 5f 66 69      goto open_fi
e8270 6e 69 73 68 65 64 3b 0a 20 20 20 20 20 20 7d 0a  nished;.      }.
e8280 20 20 20 20 20 20 75 73 65 50 72 6f 78 79 20 3d        useProxy =
e8290 20 21 28 66 73 49 6e 66 6f 2e 66 5f 66 6c 61 67   !(fsInfo.f_flag
e82a0 73 26 4d 4e 54 5f 4c 4f 43 41 4c 29 3b 0a 20 20  s&MNT_LOCAL);.  
e82b0 20 20 7d 0a 20 20 20 20 69 66 28 20 75 73 65 50    }.    if( useP
e82c0 72 6f 78 79 20 29 7b 0a 20 20 20 20 20 20 72 63  roxy ){.      rc
e82d0 20 3d 20 66 69 6c 6c 49 6e 55 6e 69 78 46 69 6c   = fillInUnixFil
e82e0 65 28 70 56 66 73 2c 20 66 64 2c 20 64 69 72 66  e(pVfs, fd, dirf
e82f0 64 2c 20 70 46 69 6c 65 2c 20 7a 50 61 74 68 2c  d, pFile, zPath,
e8300 20 6e 6f 4c 6f 63 6b 2c 20 69 73 44 65 6c 65 74   noLock, isDelet
e8310 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  e);.      if( rc
e8320 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
e8330 20 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78         rc = prox
e8340 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46 69  yTransformUnixFi
e8350 6c 65 28 28 75 6e 69 78 46 69 6c 65 2a 29 70 46  le((unixFile*)pF
e8360 69 6c 65 2c 20 22 3a 61 75 74 6f 3a 22 29 3b 0a  ile, ":auto:");.
e8370 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f        }.      go
e8380 74 6f 20 6f 70 65 6e 5f 66 69 6e 69 73 68 65 64  to open_finished
e8390 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
e83a0 69 66 0a 20 20 0a 20 20 72 63 20 3d 20 66 69 6c  if.  .  rc = fil
e83b0 6c 49 6e 55 6e 69 78 46 69 6c 65 28 70 56 66 73  lInUnixFile(pVfs
e83c0 2c 20 66 64 2c 20 64 69 72 66 64 2c 20 70 46 69  , fd, dirfd, pFi
e83d0 6c 65 2c 20 7a 50 61 74 68 2c 20 6e 6f 4c 6f 63  le, zPath, noLoc
e83e0 6b 2c 20 69 73 44 65 6c 65 74 65 29 3b 0a 6f 70  k, isDelete);.op
e83f0 65 6e 5f 66 69 6e 69 73 68 65 64 3a 0a 20 20 69  en_finished:.  i
e8400 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
e8410 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
e8420 66 72 65 65 28 70 2d 3e 70 55 6e 75 73 65 64 29  free(p->pUnused)
e8430 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
e8440 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c  c;.}.../*.** Del
e8450 65 74 65 20 74 68 65 20 66 69 6c 65 20 61 74 20  ete the file at 
e8460 7a 50 61 74 68 2e 20 49 66 20 74 68 65 20 64 69  zPath. If the di
e8470 72 53 79 6e 63 20 61 72 67 75 6d 65 6e 74 20 69  rSync argument i
e8480 73 20 74 72 75 65 2c 20 66 73 79 6e 63 28 29 0a  s true, fsync().
e8490 2a 2a 20 74 68 65 20 64 69 72 65 63 74 6f 72 79  ** the directory
e84a0 20 61 66 74 65 72 20 64 65 6c 65 74 69 6e 67 20   after deleting 
e84b0 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  the file..*/.sta
e84c0 74 69 63 20 69 6e 74 20 75 6e 69 78 44 65 6c 65  tic int unixDele
e84d0 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  te(.  sqlite3_vf
e84e0 73 20 2a 4e 6f 74 55 73 65 64 2c 20 20 20 20 20  s *NotUsed,     
e84f0 2f 2a 20 56 46 53 20 63 6f 6e 74 61 69 6e 69 6e  /* VFS containin
e8500 67 20 74 68 69 73 20 61 73 20 74 68 65 20 78 44  g this as the xD
e8510 65 6c 65 74 65 20 6d 65 74 68 6f 64 20 2a 2f 0a  elete method */.
e8520 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50    const char *zP
e8530 61 74 68 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e  ath,        /* N
e8540 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 62  ame of file to b
e8550 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69  e deleted */.  i
e8560 6e 74 20 64 69 72 53 79 6e 63 20 20 20 20 20 20  nt dirSync      
e8570 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74           /* If t
e8580 72 75 65 2c 20 66 73 79 6e 63 28 29 20 64 69 72  rue, fsync() dir
e8590 65 63 74 6f 72 79 20 61 66 74 65 72 20 64 65 6c  ectory after del
e85a0 65 74 69 6e 67 20 66 69 6c 65 20 2a 2f 0a 29 7b  eting file */.){
e85b0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
e85c0 54 45 5f 4f 4b 3b 0a 20 20 55 4e 55 53 45 44 5f  TE_OK;.  UNUSED_
e85d0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
e85e0 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f  d);.  SimulateIO
e85f0 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c  Error(return SQL
e8600 49 54 45 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45  ITE_IOERR_DELETE
e8610 29 3b 0a 20 20 75 6e 6c 69 6e 6b 28 7a 50 61 74  );.  unlink(zPat
e8620 68 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  h);.#ifndef SQLI
e8630 54 45 5f 44 49 53 41 42 4c 45 5f 44 49 52 53 59  TE_DISABLE_DIRSY
e8640 4e 43 0a 20 20 69 66 28 20 64 69 72 53 79 6e 63  NC.  if( dirSync
e8650 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 64 3b 0a   ){.    int fd;.
e8660 20 20 20 20 72 63 20 3d 20 6f 70 65 6e 44 69 72      rc = openDir
e8670 65 63 74 6f 72 79 28 7a 50 61 74 68 2c 20 26 66  ectory(zPath, &f
e8680 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  d);.    if( rc==
e8690 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 66  SQLITE_OK ){.#if
e86a0 20 4f 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20   OS_VXWORKS.    
e86b0 20 20 69 66 28 20 66 73 79 6e 63 28 66 64 29 3d    if( fsync(fd)=
e86c0 3d 2d 31 20 29 0a 23 65 6c 73 65 0a 20 20 20 20  =-1 ).#else.    
e86d0 20 20 69 66 28 20 66 73 79 6e 63 28 66 64 29 20    if( fsync(fd) 
e86e0 29 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b  ).#endif.      {
e86f0 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
e8700 4c 49 54 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46  LITE_IOERR_DIR_F
e8710 53 59 4e 43 3b 0a 20 20 20 20 20 20 7d 0a 20 20  SYNC;.      }.  
e8720 20 20 20 20 69 66 28 20 63 6c 6f 73 65 28 66 64      if( close(fd
e8730 29 26 26 21 72 63 20 29 7b 0a 20 20 20 20 20 20  )&&!rc ){.      
e8740 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f    rc = SQLITE_IO
e8750 45 52 52 5f 44 49 52 5f 43 4c 4f 53 45 3b 0a 20  ERR_DIR_CLOSE;. 
e8760 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
e8770 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
e8780 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65   rc;.}../*.** Te
e8790 73 74 20 74 68 65 20 65 78 69 73 74 61 6e 63 65  st the existance
e87a0 20 6f 66 20 6f 72 20 61 63 63 65 73 73 20 70 65   of or access pe
e87b0 72 6d 69 73 73 69 6f 6e 73 20 6f 66 20 66 69 6c  rmissions of fil
e87c0 65 20 7a 50 61 74 68 2e 20 54 68 65 0a 2a 2a 20  e zPath. The.** 
e87d0 74 65 73 74 20 70 65 72 66 6f 72 6d 65 64 20 64  test performed d
e87e0 65 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61  epends on the va
e87f0 6c 75 65 20 6f 66 20 66 6c 61 67 73 3a 0a 2a 2a  lue of flags:.**
e8800 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41  .**     SQLITE_A
e8810 43 43 45 53 53 5f 45 58 49 53 54 53 3a 20 52 65  CCESS_EXISTS: Re
e8820 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 66 69  turn 1 if the fi
e8830 6c 65 20 65 78 69 73 74 73 0a 2a 2a 20 20 20 20  le exists.**    
e8840 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52   SQLITE_ACCESS_R
e8850 45 41 44 57 52 49 54 45 3a 20 52 65 74 75 72 6e  EADWRITE: Return
e8860 20 31 20 69 66 20 74 68 65 20 66 69 6c 65 20 69   1 if the file i
e8870 73 20 72 65 61 64 20 61 6e 64 20 77 72 69 74 61  s read and writa
e8880 62 6c 65 2e 0a 2a 2a 20 20 20 20 20 53 51 4c 49  ble..**     SQLI
e8890 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 4f 4e  TE_ACCESS_READON
e88a0 4c 59 3a 20 52 65 74 75 72 6e 20 31 20 69 66 20  LY: Return 1 if 
e88b0 74 68 65 20 66 69 6c 65 20 69 73 20 72 65 61 64  the file is read
e88c0 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65  able..**.** Othe
e88d0 72 77 69 73 65 20 72 65 74 75 72 6e 20 30 2e 0a  rwise return 0..
e88e0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
e88f0 69 78 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69  ixAccess(.  sqli
e8900 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64  te3_vfs *NotUsed
e8910 2c 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20 63  ,   /* The VFS c
e8920 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73 20 78  ontaining this x
e8930 41 63 63 65 73 73 20 6d 65 74 68 6f 64 20 2a 2f  Access method */
e8940 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
e8950 50 61 74 68 2c 20 20 20 20 20 20 2f 2a 20 50 61  Path,      /* Pa
e8960 74 68 20 6f 66 20 74 68 65 20 66 69 6c 65 20 74  th of the file t
e8970 6f 20 65 78 61 6d 69 6e 65 20 2a 2f 0a 20 20 69  o examine */.  i
e8980 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nt flags,       
e8990 20 20 20 20 20 20 20 2f 2a 20 57 68 61 74 20 64         /* What d
e89a0 6f 20 77 65 20 77 61 6e 74 20 74 6f 20 6c 65 61  o we want to lea
e89b0 72 6e 20 61 62 6f 75 74 20 74 68 65 20 7a 50 61  rn about the zPa
e89c0 74 68 20 66 69 6c 65 3f 20 2a 2f 0a 20 20 69 6e  th file? */.  in
e89d0 74 20 2a 70 52 65 73 4f 75 74 20 20 20 20 20 20  t *pResOut      
e89e0 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 72        /* Write r
e89f0 65 73 75 6c 74 20 62 6f 6f 6c 65 61 6e 20 68 65  esult boolean he
e8a00 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61  re */.){.  int a
e8a10 6d 6f 64 65 20 3d 20 30 3b 0a 20 20 55 4e 55 53  mode = 0;.  UNUS
e8a20 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
e8a30 55 73 65 64 29 3b 0a 20 20 53 69 6d 75 6c 61 74  Used);.  Simulat
e8a40 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e  eIOError( return
e8a50 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 41 43   SQLITE_IOERR_AC
e8a60 43 45 53 53 3b 20 29 3b 0a 20 20 73 77 69 74 63  CESS; );.  switc
e8a70 68 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20  h( flags ){.    
e8a80 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45  case SQLITE_ACCE
e8a90 53 53 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 20  SS_EXISTS:.     
e8aa0 20 61 6d 6f 64 65 20 3d 20 46 5f 4f 4b 3b 0a 20   amode = F_OK;. 
e8ab0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
e8ac0 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45  case SQLITE_ACCE
e8ad0 53 53 5f 52 45 41 44 57 52 49 54 45 3a 0a 20 20  SS_READWRITE:.  
e8ae0 20 20 20 20 61 6d 6f 64 65 20 3d 20 57 5f 4f 4b      amode = W_OK
e8af0 7c 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65  |R_OK;.      bre
e8b00 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c  ak;.    case SQL
e8b10 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a  ITE_ACCESS_READ:
e8b20 0a 20 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 52  .      amode = R
e8b30 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  _OK;.      break
e8b40 3b 0a 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a  ;..    default:.
e8b50 20 20 20 20 20 20 61 73 73 65 72 74 28 21 22 49        assert(!"I
e8b60 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67  nvalid flags arg
e8b70 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a  ument");.  }.  *
e8b80 70 52 65 73 4f 75 74 20 3d 20 28 61 63 63 65 73  pResOut = (acces
e8b90 73 28 7a 50 61 74 68 2c 20 61 6d 6f 64 65 29 3d  s(zPath, amode)=
e8ba0 3d 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  =0);.  return SQ
e8bb0 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a  LITE_OK;.}.../*.
e8bc0 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61 74 69  ** Turn a relati
e8bd0 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f  ve pathname into
e8be0 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65   a full pathname
e8bf0 2e 20 54 68 65 20 72 65 6c 61 74 69 76 65 20 70  . The relative p
e8c00 61 74 68 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64  ath.** is stored
e8c10 20 61 73 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e   as a nul-termin
e8c20 61 74 65 64 20 73 74 72 69 6e 67 20 69 6e 20 74  ated string in t
e8c30 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65  he buffer pointe
e8c40 64 20 74 6f 20 62 79 0a 2a 2a 20 7a 50 61 74 68  d to by.** zPath
e8c50 2e 20 0a 2a 2a 0a 2a 2a 20 7a 4f 75 74 20 70 6f  . .**.** zOut po
e8c60 69 6e 74 73 20 74 6f 20 61 20 62 75 66 66 65 72  ints to a buffer
e8c70 20 6f 66 20 61 74 20 6c 65 61 73 74 20 73 71 6c   of at least sql
e8c80 69 74 65 33 5f 76 66 73 2e 6d 78 50 61 74 68 6e  ite3_vfs.mxPathn
e8c90 61 6d 65 20 62 79 74 65 73 20 0a 2a 2a 20 28 69  ame bytes .** (i
e8ca0 6e 20 74 68 69 73 20 63 61 73 65 2c 20 4d 41 58  n this case, MAX
e8cb0 5f 50 41 54 48 4e 41 4d 45 20 62 79 74 65 73 29  _PATHNAME bytes)
e8cc0 2e 20 54 68 65 20 66 75 6c 6c 2d 70 61 74 68 20  . The full-path 
e8cd0 69 73 20 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a  is written to.**
e8ce0 20 74 68 69 73 20 62 75 66 66 65 72 20 62 65 66   this buffer bef
e8cf0 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
e8d00 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
e8d10 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20  xFullPathname(. 
e8d20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
e8d30 66 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  fs,            /
e8d40 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73  * Pointer to vfs
e8d50 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e   object */.  con
e8d60 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20  st char *zPath, 
e8d70 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
e8d80 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20  ssibly relative 
e8d90 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20  input path */.  
e8da0 69 6e 74 20 6e 4f 75 74 2c 20 20 20 20 20 20 20  int nOut,       
e8db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e8dc0 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 74 20   Size of output 
e8dd0 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20  buffer in bytes 
e8de0 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74 20  */.  char *zOut 
e8df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e8e00 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66     /* Output buf
e8e10 66 65 72 20 2a 2f 0a 29 7b 0a 0a 20 20 2f 2a 20  fer */.){..  /* 
e8e20 49 74 27 73 20 6f 64 64 20 74 6f 20 73 69 6d 75  It's odd to simu
e8e30 6c 61 74 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72  late an io-error
e8e40 20 68 65 72 65 2c 20 62 75 74 20 72 65 61 6c 6c   here, but reall
e8e50 79 20 74 68 69 73 20 69 73 20 6a 75 73 74 0a 20  y this is just. 
e8e60 20 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 69 6f   ** using the io
e8e70 2d 65 72 72 6f 72 20 69 6e 66 72 61 73 74 72 75  -error infrastru
e8e80 63 74 75 72 65 20 74 6f 20 74 65 73 74 20 74 68  cture to test th
e8e90 61 74 20 53 51 4c 69 74 65 20 68 61 6e 64 6c 65  at SQLite handle
e8ea0 73 20 74 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63  s this.  ** func
e8eb0 74 69 6f 6e 20 66 61 69 6c 69 6e 67 2e 20 54 68  tion failing. Th
e8ec0 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 75 6c  is function coul
e8ed0 64 20 66 61 69 6c 20 69 66 2c 20 66 6f 72 20 65  d fail if, for e
e8ee0 78 61 6d 70 6c 65 2c 20 74 68 65 0a 20 20 2a 2a  xample, the.  **
e8ef0 20 63 75 72 72 65 6e 74 20 77 6f 72 6b 69 6e 67   current working
e8f00 20 64 69 72 65 63 74 6f 72 79 20 68 61 73 20 62   directory has b
e8f10 65 65 6e 20 75 6e 6c 69 6e 6b 65 64 2e 0a 20 20  een unlinked..  
e8f20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  */.  SimulateIOE
e8f30 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
e8f40 49 54 45 5f 45 52 52 4f 52 20 29 3b 0a 0a 20 20  ITE_ERROR );..  
e8f50 61 73 73 65 72 74 28 20 70 56 66 73 2d 3e 6d 78  assert( pVfs->mx
e8f60 50 61 74 68 6e 61 6d 65 3d 3d 4d 41 58 5f 50 41  Pathname==MAX_PA
e8f70 54 48 4e 41 4d 45 20 29 3b 0a 20 20 55 4e 55 53  THNAME );.  UNUS
e8f80 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56 66  ED_PARAMETER(pVf
e8f90 73 29 3b 0a 0a 20 20 7a 4f 75 74 5b 6e 4f 75 74  s);..  zOut[nOut
e8fa0 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66  -1] = '\0';.  if
e8fb0 28 20 7a 50 61 74 68 5b 30 5d 3d 3d 27 2f 27 20  ( zPath[0]=='/' 
e8fc0 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  ){.    sqlite3_s
e8fd0 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2c 20 7a 4f  nprintf(nOut, zO
e8fe0 75 74 2c 20 22 25 73 22 2c 20 7a 50 61 74 68 29  ut, "%s", zPath)
e8ff0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
e9000 6e 74 20 6e 43 77 64 3b 0a 20 20 20 20 69 66 28  nt nCwd;.    if(
e9010 20 67 65 74 63 77 64 28 7a 4f 75 74 2c 20 6e 4f   getcwd(zOut, nO
e9020 75 74 2d 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ut-1)==0 ){.    
e9030 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e9040 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a  CANTOPEN;.    }.
e9050 20 20 20 20 6e 43 77 64 20 3d 20 28 69 6e 74 29      nCwd = (int)
e9060 73 74 72 6c 65 6e 28 7a 4f 75 74 29 3b 0a 20 20  strlen(zOut);.  
e9070 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
e9080 74 66 28 6e 4f 75 74 2d 6e 43 77 64 2c 20 26 7a  tf(nOut-nCwd, &z
e9090 4f 75 74 5b 6e 43 77 64 5d 2c 20 22 2f 25 73 22  Out[nCwd], "/%s"
e90a0 2c 20 7a 50 61 74 68 29 3b 0a 20 20 7d 0a 20 20  , zPath);.  }.  
e90b0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
e90c0 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51  ;.}...#ifndef SQ
e90d0 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45  LITE_OMIT_LOAD_E
e90e0 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49  XTENSION./*.** I
e90f0 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f 70  nterfaces for op
e9100 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20 6c  ening a shared l
e9110 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67 20  ibrary, finding 
e9120 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a 20  entry points.** 
e9130 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 72 65  within the share
e9140 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20 63  d library, and c
e9150 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72 65  losing the share
e9160 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69  d library..*/.#i
e9170 6e 63 6c 75 64 65 20 3c 64 6c 66 63 6e 2e 68 3e  nclude <dlfcn.h>
e9180 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 75 6e  .static void *un
e9190 69 78 44 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33  ixDlOpen(sqlite3
e91a0 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 63  _vfs *NotUsed, c
e91b0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
e91c0 6e 61 6d 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f  name){.  UNUSED_
e91d0 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
e91e0 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 64 6c 6f  d);.  return dlo
e91f0 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 52  pen(zFilename, R
e9200 54 4c 44 5f 4e 4f 57 20 7c 20 52 54 4c 44 5f 47  TLD_NOW | RTLD_G
e9210 4c 4f 42 41 4c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  LOBAL);.}../*.**
e9220 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68   SQLite calls th
e9230 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6d 6d 65  is function imme
e9240 64 69 61 74 65 6c 79 20 61 66 74 65 72 20 61 20  diately after a 
e9250 63 61 6c 6c 20 74 6f 20 75 6e 69 78 44 6c 53 79  call to unixDlSy
e9260 6d 28 29 20 6f 72 0a 2a 2a 20 75 6e 69 78 44 6c  m() or.** unixDl
e9270 4f 70 65 6e 28 29 20 66 61 69 6c 73 20 28 72 65  Open() fails (re
e9280 74 75 72 6e 73 20 61 20 6e 75 6c 6c 20 70 6f 69  turns a null poi
e9290 6e 74 65 72 29 2e 20 49 66 20 61 20 6d 6f 72 65  nter). If a more
e92a0 20 64 65 74 61 69 6c 65 64 20 65 72 72 6f 72 0a   detailed error.
e92b0 2a 2a 20 6d 65 73 73 61 67 65 20 69 73 20 61 76  ** message is av
e92c0 61 69 6c 61 62 6c 65 2c 20 69 74 20 69 73 20 77  ailable, it is w
e92d0 72 69 74 74 65 6e 20 74 6f 20 7a 42 75 66 4f 75  ritten to zBufOu
e92e0 74 2e 20 49 66 20 6e 6f 20 65 72 72 6f 72 20 6d  t. If no error m
e92f0 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 61 76 61  essage.** is ava
e9300 69 6c 61 62 6c 65 2c 20 7a 42 75 66 4f 75 74 20  ilable, zBufOut 
e9310 69 73 20 6c 65 66 74 20 75 6e 6d 6f 64 69 66 69  is left unmodifi
e9320 65 64 20 61 6e 64 20 53 51 4c 69 74 65 20 75 73  ed and SQLite us
e9330 65 73 20 61 20 64 65 66 61 75 6c 74 0a 2a 2a 20  es a default.** 
e9340 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a  error message..*
e9350 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e  /.static void un
e9360 69 78 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65  ixDlError(sqlite
e9370 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20  3_vfs *NotUsed, 
e9380 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72 20 2a  int nBuf, char *
e9390 7a 42 75 66 4f 75 74 29 7b 0a 20 20 63 68 61 72  zBufOut){.  char
e93a0 20 2a 7a 45 72 72 3b 0a 20 20 55 4e 55 53 45 44   *zErr;.  UNUSED
e93b0 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
e93c0 65 64 29 3b 0a 20 20 75 6e 69 78 45 6e 74 65 72  ed);.  unixEnter
e93d0 4d 75 74 65 78 28 29 3b 0a 20 20 7a 45 72 72 20  Mutex();.  zErr 
e93e0 3d 20 64 6c 65 72 72 6f 72 28 29 3b 0a 20 20 69  = dlerror();.  i
e93f0 66 28 20 7a 45 72 72 20 29 7b 0a 20 20 20 20 73  f( zErr ){.    s
e9400 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
e9410 6e 42 75 66 2c 20 7a 42 75 66 4f 75 74 2c 20 22  nBuf, zBufOut, "
e9420 25 73 22 2c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a  %s", zErr);.  }.
e9430 20 20 75 6e 69 78 4c 65 61 76 65 4d 75 74 65 78    unixLeaveMutex
e9440 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  ();.}.static voi
e9450 64 20 28 2a 75 6e 69 78 44 6c 53 79 6d 28 73 71  d (*unixDlSym(sq
e9460 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73  lite3_vfs *NotUs
e9470 65 64 2c 20 76 6f 69 64 20 2a 70 2c 20 63 6f 6e  ed, void *p, con
e9480 73 74 20 63 68 61 72 2a 7a 53 79 6d 29 29 28 76  st char*zSym))(v
e9490 6f 69 64 29 7b 0a 20 20 2f 2a 20 0a 20 20 2a 2a  oid){.  /* .  **
e94a0 20 47 43 43 20 77 69 74 68 20 2d 70 65 64 61 6e   GCC with -pedan
e94b0 74 69 63 2d 65 72 72 6f 72 73 20 73 61 79 73 20  tic-errors says 
e94c0 74 68 61 74 20 43 39 30 20 64 6f 65 73 20 6e 6f  that C90 does no
e94d0 74 20 61 6c 6c 6f 77 20 61 20 76 6f 69 64 2a 20  t allow a void* 
e94e0 74 6f 20 62 65 0a 20 20 2a 2a 20 63 61 73 74 20  to be.  ** cast 
e94f0 69 6e 74 6f 20 61 20 70 6f 69 6e 74 65 72 20 74  into a pointer t
e9500 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 20 20 41  o a function.  A
e9510 6e 64 20 79 65 74 20 74 68 65 20 6c 69 62 72 61  nd yet the libra
e9520 72 79 20 64 6c 73 79 6d 28 29 20 72 6f 75 74 69  ry dlsym() routi
e9530 6e 65 0a 20 20 2a 2a 20 72 65 74 75 72 6e 73 20  ne.  ** returns 
e9540 61 20 76 6f 69 64 2a 20 77 68 69 63 68 20 69 73  a void* which is
e9550 20 72 65 61 6c 6c 79 20 61 20 70 6f 69 6e 74 65   really a pointe
e9560 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e  r to a function.
e9570 20 20 53 6f 20 68 6f 77 20 64 6f 20 77 65 0a 20    So how do we. 
e9580 20 2a 2a 20 75 73 65 20 64 6c 73 79 6d 28 29 20   ** use dlsym() 
e9590 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63 2d 65  with -pedantic-e
e95a0 72 72 6f 72 73 3f 0a 20 20 2a 2a 0a 20 20 2a 2a  rrors?.  **.  **
e95b0 20 56 61 72 69 61 62 6c 65 20 78 20 62 65 6c 6f   Variable x belo
e95c0 77 20 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20  w is defined to 
e95d0 62 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  be a pointer to 
e95e0 61 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 69 6e  a function takin
e95f0 67 0a 20 20 2a 2a 20 70 61 72 61 6d 65 74 65 72  g.  ** parameter
e9600 73 20 76 6f 69 64 2a 20 61 6e 64 20 63 6f 6e 73  s void* and cons
e9610 74 20 63 68 61 72 2a 20 61 6e 64 20 72 65 74 75  t char* and retu
e9620 72 6e 69 6e 67 20 61 20 70 6f 69 6e 74 65 72 20  rning a pointer 
e9630 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 20  to a function.. 
e9640 20 2a 2a 20 57 65 20 69 6e 69 74 69 61 6c 69 7a   ** We initializ
e9650 65 20 78 20 62 79 20 61 73 73 69 67 6e 69 6e 67  e x by assigning
e9660 20 69 74 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   it a pointer to
e9670 20 74 68 65 20 64 6c 73 79 6d 28 29 20 66 75 6e   the dlsym() fun
e9680 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 28 54 68 61  ction..  ** (Tha
e9690 74 20 61 73 73 69 67 6e 6d 65 6e 74 20 72 65 71  t assignment req
e96a0 75 69 72 65 73 20 61 20 63 61 73 74 2e 29 20 20  uires a cast.)  
e96b0 54 68 65 6e 20 77 65 20 63 61 6c 6c 20 74 68 65  Then we call the
e96c0 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 0a 20   function that. 
e96d0 20 2a 2a 20 78 20 70 6f 69 6e 74 73 20 74 6f 2e   ** x points to.
e96e0 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69    .  **.  ** Thi
e96f0 73 20 77 6f 72 6b 2d 61 72 6f 75 6e 64 20 69 73  s work-around is
e9700 20 75 6e 6c 69 6b 65 6c 79 20 74 6f 20 77 6f 72   unlikely to wor
e9710 6b 20 63 6f 72 72 65 63 74 6c 79 20 6f 6e 20 61  k correctly on a
e9720 6e 79 20 73 79 73 74 65 6d 20 77 68 65 72 65 0a  ny system where.
e9730 20 20 2a 2a 20 79 6f 75 20 72 65 61 6c 6c 79 20    ** you really 
e9740 63 61 6e 6e 6f 74 20 63 61 73 74 20 61 20 66 75  cannot cast a fu
e9750 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 20 69  nction pointer i
e9760 6e 74 6f 20 76 6f 69 64 2a 2e 20 20 42 75 74 20  nto void*.  But 
e9770 74 68 65 6e 2c 20 6f 6e 20 74 68 65 0a 20 20 2a  then, on the.  *
e9780 2a 20 6f 74 68 65 72 20 68 61 6e 64 2c 20 64 6c  * other hand, dl
e9790 73 79 6d 28 29 20 77 69 6c 6c 20 6e 6f 74 20 77  sym() will not w
e97a0 6f 72 6b 20 6f 6e 20 73 75 63 68 20 61 20 73 79  ork on such a sy
e97b0 73 74 65 6d 20 65 69 74 68 65 72 2c 20 73 6f 20  stem either, so 
e97c0 77 65 20 68 61 76 65 0a 20 20 2a 2a 20 6e 6f 74  we have.  ** not
e97d0 20 72 65 61 6c 6c 79 20 6c 6f 73 74 20 61 6e 79   really lost any
e97e0 74 68 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 76 6f  thing..  */.  vo
e97f0 69 64 20 28 2a 28 2a 78 29 28 76 6f 69 64 2a 2c  id (*(*x)(void*,
e9800 63 6f 6e 73 74 20 63 68 61 72 2a 29 29 28 76 6f  const char*))(vo
e9810 69 64 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  id);.  UNUSED_PA
e9820 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
e9830 3b 0a 20 20 78 20 3d 20 28 76 6f 69 64 28 2a 28  ;.  x = (void(*(
e9840 2a 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63  *)(void*,const c
e9850 68 61 72 2a 29 29 28 76 6f 69 64 29 29 64 6c 73  har*))(void))dls
e9860 79 6d 3b 0a 20 20 72 65 74 75 72 6e 20 28 2a 78  ym;.  return (*x
e9870 29 28 70 2c 20 7a 53 79 6d 29 3b 0a 7d 0a 73 74  )(p, zSym);.}.st
e9880 61 74 69 63 20 76 6f 69 64 20 75 6e 69 78 44 6c  atic void unixDl
e9890 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66  Close(sqlite3_vf
e98a0 73 20 2a 4e 6f 74 55 73 65 64 2c 20 76 6f 69 64  s *NotUsed, void
e98b0 20 2a 70 48 61 6e 64 6c 65 29 7b 0a 20 20 55 4e   *pHandle){.  UN
e98c0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
e98d0 6f 74 55 73 65 64 29 3b 0a 20 20 64 6c 63 6c 6f  otUsed);.  dlclo
e98e0 73 65 28 70 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23  se(pHandle);.}.#
e98f0 65 6c 73 65 20 2f 2a 20 69 66 20 53 51 4c 49 54  else /* if SQLIT
e9900 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
e9910 4e 53 49 4f 4e 20 69 73 20 64 65 66 69 6e 65 64  NSION is defined
e9920 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 75  : */.  #define u
e9930 6e 69 78 44 6c 4f 70 65 6e 20 20 30 0a 20 20 23  nixDlOpen  0.  #
e9940 64 65 66 69 6e 65 20 75 6e 69 78 44 6c 45 72 72  define unixDlErr
e9950 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20 75  or 0.  #define u
e9960 6e 69 78 44 6c 53 79 6d 20 20 20 30 0a 20 20 23  nixDlSym   0.  #
e9970 64 65 66 69 6e 65 20 75 6e 69 78 44 6c 43 6c 6f  define unixDlClo
e9980 73 65 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  se 0.#endif../*.
e9990 2a 2a 20 57 72 69 74 65 20 6e 42 75 66 20 62 79  ** Write nBuf by
e99a0 74 65 73 20 6f 66 20 72 61 6e 64 6f 6d 20 64 61  tes of random da
e99b0 74 61 20 74 6f 20 74 68 65 20 73 75 70 70 6c 69  ta to the suppli
e99c0 65 64 20 62 75 66 66 65 72 20 7a 42 75 66 2e 0a  ed buffer zBuf..
e99d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
e99e0 69 78 52 61 6e 64 6f 6d 6e 65 73 73 28 73 71 6c  ixRandomness(sql
e99f0 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65  ite3_vfs *NotUse
e9a00 64 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61  d, int nBuf, cha
e9a10 72 20 2a 7a 42 75 66 29 7b 0a 20 20 55 4e 55 53  r *zBuf){.  UNUS
e9a20 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
e9a30 55 73 65 64 29 3b 0a 20 20 61 73 73 65 72 74 28  Used);.  assert(
e9a40 28 73 69 7a 65 5f 74 29 6e 42 75 66 3e 3d 28 73  (size_t)nBuf>=(s
e9a50 69 7a 65 6f 66 28 74 69 6d 65 5f 74 29 2b 73 69  izeof(time_t)+si
e9a60 7a 65 6f 66 28 69 6e 74 29 29 29 3b 0a 0a 20 20  zeof(int)));..  
e9a70 2f 2a 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e  /* We have to in
e9a80 69 74 69 61 6c 69 7a 65 20 7a 42 75 66 20 74 6f  itialize zBuf to
e9a90 20 70 72 65 76 65 6e 74 20 76 61 6c 67 72 69 6e   prevent valgrin
e9aa0 64 20 66 72 6f 6d 20 72 65 70 6f 72 74 69 6e 67  d from reporting
e9ab0 0a 20 20 2a 2a 20 65 72 72 6f 72 73 2e 20 20 54  .  ** errors.  T
e9ac0 68 65 20 72 65 70 6f 72 74 73 20 69 73 73 75 65  he reports issue
e9ad0 64 20 62 79 20 76 61 6c 67 72 69 6e 64 20 61 72  d by valgrind ar
e9ae0 65 20 69 6e 63 6f 72 72 65 63 74 20 2d 20 77 65  e incorrect - we
e9af0 20 77 6f 75 6c 64 0a 20 20 2a 2a 20 70 72 65 66   would.  ** pref
e9b00 65 72 20 74 68 61 74 20 74 68 65 20 72 61 6e 64  er that the rand
e9b10 6f 6d 6e 65 73 73 20 62 65 20 69 6e 63 72 65 61  omness be increa
e9b20 73 65 64 20 62 79 20 6d 61 6b 69 6e 67 20 75 73  sed by making us
e9b30 65 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 75 6e  e of the.  ** un
e9b40 69 6e 69 74 69 61 6c 69 7a 65 64 20 73 70 61 63  initialized spac
e9b50 65 20 69 6e 20 7a 42 75 66 20 2d 20 62 75 74 20  e in zBuf - but 
e9b60 76 61 6c 67 72 69 6e 64 20 65 72 72 6f 72 73 20  valgrind errors 
e9b70 74 65 6e 64 20 74 6f 20 77 6f 72 72 79 0a 20 20  tend to worry.  
e9b80 2a 2a 20 73 6f 6d 65 20 75 73 65 72 73 2e 20 20  ** some users.  
e9b90 52 61 74 68 65 72 20 74 68 61 6e 20 61 72 67 75  Rather than argu
e9ba0 65 2c 20 69 74 20 73 65 65 6d 73 20 65 61 73 69  e, it seems easi
e9bb0 65 72 20 6a 75 73 74 20 74 6f 20 69 6e 69 74 69  er just to initi
e9bc0 61 6c 69 7a 65 0a 20 20 2a 2a 20 74 68 65 20 77  alize.  ** the w
e9bd0 68 6f 6c 65 20 61 72 72 61 79 20 61 6e 64 20 73  hole array and s
e9be0 69 6c 65 6e 63 65 20 76 61 6c 67 72 69 6e 64 2c  ilence valgrind,
e9bf0 20 65 76 65 6e 20 69 66 20 74 68 61 74 20 6d 65   even if that me
e9c00 61 6e 73 20 6c 65 73 73 20 72 61 6e 64 6f 6d 6e  ans less randomn
e9c10 65 73 73 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20  ess.  ** in the 
e9c20 72 61 6e 64 6f 6d 20 73 65 65 64 2e 0a 20 20 2a  random seed..  *
e9c30 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 74 65 73 74  *.  ** When test
e9c40 69 6e 67 2c 20 69 6e 69 74 69 61 6c 69 7a 69 6e  ing, initializin
e9c50 67 20 7a 42 75 66 5b 5d 20 74 6f 20 7a 65 72 6f  g zBuf[] to zero
e9c60 20 69 73 20 61 6c 6c 20 77 65 20 64 6f 2e 20 20   is all we do.  
e9c70 54 68 61 74 20 6d 65 61 6e 73 0a 20 20 2a 2a 20  That means.  ** 
e9c80 74 68 61 74 20 77 65 20 61 6c 77 61 79 73 20 75  that we always u
e9c90 73 65 20 74 68 65 20 73 61 6d 65 20 72 61 6e 64  se the same rand
e9ca0 6f 6d 20 6e 75 6d 62 65 72 20 73 65 71 75 65 6e  om number sequen
e9cb0 63 65 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20  ce.  This makes 
e9cc0 74 68 65 0a 20 20 2a 2a 20 74 65 73 74 73 20 72  the.  ** tests r
e9cd0 65 70 65 61 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  epeatable..  */.
e9ce0 20 20 6d 65 6d 73 65 74 28 7a 42 75 66 2c 20 30    memset(zBuf, 0
e9cf0 2c 20 6e 42 75 66 29 3b 0a 23 69 66 20 21 64 65  , nBuf);.#if !de
e9d00 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53  fined(SQLITE_TES
e9d10 54 29 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 70  T).  {.    int p
e9d20 69 64 2c 20 66 64 3b 0a 20 20 20 20 66 64 20 3d  id, fd;.    fd =
e9d30 20 6f 70 65 6e 28 22 2f 64 65 76 2f 75 72 61 6e   open("/dev/uran
e9d40 64 6f 6d 22 2c 20 4f 5f 52 44 4f 4e 4c 59 29 3b  dom", O_RDONLY);
e9d50 0a 20 20 20 20 69 66 28 20 66 64 3c 30 20 29 7b  .    if( fd<0 ){
e9d60 0a 20 20 20 20 20 20 74 69 6d 65 5f 74 20 74 3b  .      time_t t;
e9d70 0a 20 20 20 20 20 20 74 69 6d 65 28 26 74 29 3b  .      time(&t);
e9d80 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 42  .      memcpy(zB
e9d90 75 66 2c 20 26 74 2c 20 73 69 7a 65 6f 66 28 74  uf, &t, sizeof(t
e9da0 29 29 3b 0a 20 20 20 20 20 20 70 69 64 20 3d 20  ));.      pid = 
e9db0 67 65 74 70 69 64 28 29 3b 0a 20 20 20 20 20 20  getpid();.      
e9dc0 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 73 69 7a  memcpy(&zBuf[siz
e9dd0 65 6f 66 28 74 29 5d 2c 20 26 70 69 64 2c 20 73  eof(t)], &pid, s
e9de0 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20 20 20  izeof(pid));.   
e9df0 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f     assert( sizeo
e9e00 66 28 74 29 2b 73 69 7a 65 6f 66 28 70 69 64 29  f(t)+sizeof(pid)
e9e10 3c 3d 28 73 69 7a 65 5f 74 29 6e 42 75 66 20 29  <=(size_t)nBuf )
e9e20 3b 0a 20 20 20 20 20 20 6e 42 75 66 20 3d 20 73  ;.      nBuf = s
e9e30 69 7a 65 6f 66 28 74 29 20 2b 20 73 69 7a 65 6f  izeof(t) + sizeo
e9e40 66 28 70 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73  f(pid);.    }els
e9e50 65 7b 0a 20 20 20 20 20 20 6e 42 75 66 20 3d 20  e{.      nBuf = 
e9e60 72 65 61 64 28 66 64 2c 20 7a 42 75 66 2c 20 6e  read(fd, zBuf, n
e9e70 42 75 66 29 3b 0a 20 20 20 20 20 20 63 6c 6f 73  Buf);.      clos
e9e80 65 28 66 64 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  e(fd);.    }.  }
e9e90 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e  .#endif.  return
e9ea0 20 6e 42 75 66 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a   nBuf;.}.../*.**
e9eb0 20 53 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74   Sleep for a lit
e9ec0 74 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75  tle while.  Retu
e9ed0 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66  rn the amount of
e9ee0 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2a 20   time slept..** 
e9ef0 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20  The argument is 
e9f00 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69  the number of mi
e9f10 63 72 6f 73 65 63 6f 6e 64 73 20 77 65 20 77 61  croseconds we wa
e9f20 6e 74 20 74 6f 20 73 6c 65 65 70 2e 0a 2a 2a 20  nt to sleep..** 
e9f30 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
e9f40 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
e9f50 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 6f  f microseconds o
e9f60 66 20 73 6c 65 65 70 20 61 63 74 75 61 6c 6c 79  f sleep actually
e9f70 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 20 66 72  .** requested fr
e9f80 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  om the underlyin
e9f90 67 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74  g operating syst
e9fa0 65 6d 2c 20 61 20 6e 75 6d 62 65 72 20 77 68 69  em, a number whi
e9fb0 63 68 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 67  ch.** might be g
e9fc0 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65  reater than or e
e9fd0 71 75 61 6c 20 74 6f 20 74 68 65 20 61 72 67 75  qual to the argu
e9fe0 6d 65 6e 74 2c 20 62 75 74 20 6e 6f 74 20 6c 65  ment, but not le
e9ff0 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20 61  ss.** than the a
ea000 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74  rgument..*/.stat
ea010 69 63 20 69 6e 74 20 75 6e 69 78 53 6c 65 65 70  ic int unixSleep
ea020 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f  (sqlite3_vfs *No
ea030 74 55 73 65 64 2c 20 69 6e 74 20 6d 69 63 72 6f  tUsed, int micro
ea040 73 65 63 6f 6e 64 73 29 7b 0a 23 69 66 20 4f 53  seconds){.#if OS
ea050 5f 56 58 57 4f 52 4b 53 0a 20 20 73 74 72 75 63  _VXWORKS.  struc
ea060 74 20 74 69 6d 65 73 70 65 63 20 73 70 3b 0a 0a  t timespec sp;..
ea070 20 20 73 70 2e 74 76 5f 73 65 63 20 3d 20 6d 69    sp.tv_sec = mi
ea080 63 72 6f 73 65 63 6f 6e 64 73 20 2f 20 31 30 30  croseconds / 100
ea090 30 30 30 30 3b 0a 20 20 73 70 2e 74 76 5f 6e 73  0000;.  sp.tv_ns
ea0a0 65 63 20 3d 20 28 6d 69 63 72 6f 73 65 63 6f 6e  ec = (microsecon
ea0b0 64 73 20 25 20 31 30 30 30 30 30 30 29 20 2a 20  ds % 1000000) * 
ea0c0 31 30 30 30 3b 0a 20 20 6e 61 6e 6f 73 6c 65 65  1000;.  nanoslee
ea0d0 70 28 26 73 70 2c 20 4e 55 4c 4c 29 3b 0a 20 20  p(&sp, NULL);.  
ea0e0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
ea0f0 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74  (NotUsed);.  ret
ea100 75 72 6e 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73  urn microseconds
ea110 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28  ;.#elif defined(
ea120 48 41 56 45 5f 55 53 4c 45 45 50 29 20 26 26 20  HAVE_USLEEP) && 
ea130 48 41 56 45 5f 55 53 4c 45 45 50 0a 20 20 75 73  HAVE_USLEEP.  us
ea140 6c 65 65 70 28 6d 69 63 72 6f 73 65 63 6f 6e 64  leep(microsecond
ea150 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  s);.  UNUSED_PAR
ea160 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
ea170 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72 6f 73  .  return micros
ea180 65 63 6f 6e 64 73 3b 0a 23 65 6c 73 65 0a 20 20  econds;.#else.  
ea190 69 6e 74 20 73 65 63 6f 6e 64 73 20 3d 20 28 6d  int seconds = (m
ea1a0 69 63 72 6f 73 65 63 6f 6e 64 73 2b 39 39 39 39  icroseconds+9999
ea1b0 39 39 29 2f 31 30 30 30 30 30 30 3b 0a 20 20 73  99)/1000000;.  s
ea1c0 6c 65 65 70 28 73 65 63 6f 6e 64 73 29 3b 0a 20  leep(seconds);. 
ea1d0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
ea1e0 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65  R(NotUsed);.  re
ea1f0 74 75 72 6e 20 73 65 63 6f 6e 64 73 2a 31 30 30  turn seconds*100
ea200 30 30 30 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a  0000;.#endif.}..
ea210 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
ea220 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66  ing variable, if
ea230 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65   set to a non-ze
ea240 72 6f 20 76 61 6c 75 65 2c 20 69 73 20 69 6e 74  ro value, is int
ea250 65 72 70 72 65 74 65 64 20 61 73 0a 2a 2a 20 74  erpreted as.** t
ea260 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 63  he number of sec
ea270 6f 6e 64 73 20 73 69 6e 63 65 20 31 39 37 30 20  onds since 1970 
ea280 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20 73  and is used to s
ea290 65 74 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  et the result of
ea2a0 0a 2a 2a 20 73 71 6c 69 74 65 33 4f 73 43 75 72  .** sqlite3OsCur
ea2b0 72 65 6e 74 54 69 6d 65 28 29 20 64 75 72 69 6e  rentTime() durin
ea2c0 67 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a 23 69  g testing..*/.#i
ea2d0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
ea2e0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
ea2f0 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f  sqlite3_current_
ea300 74 69 6d 65 20 3d 20 30 3b 20 20 2f 2a 20 46 61  time = 0;  /* Fa
ea310 6b 65 20 73 79 73 74 65 6d 20 74 69 6d 65 20 69  ke system time i
ea320 6e 20 73 65 63 6f 6e 64 73 20 73 69 6e 63 65 20  n seconds since 
ea330 31 39 37 30 2e 20 2a 2f 0a 23 65 6e 64 69 66 0a  1970. */.#endif.
ea340 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20  ./*.** Find the 
ea350 63 75 72 72 65 6e 74 20 74 69 6d 65 20 28 69 6e  current time (in
ea360 20 55 6e 69 76 65 72 73 61 6c 20 43 6f 6f 72 64   Universal Coord
ea370 69 6e 61 74 65 64 20 54 69 6d 65 29 2e 20 20 57  inated Time).  W
ea380 72 69 74 65 20 74 68 65 0a 2a 2a 20 63 75 72 72  rite the.** curr
ea390 65 6e 74 20 74 69 6d 65 20 61 6e 64 20 64 61 74  ent time and dat
ea3a0 65 20 61 73 20 61 20 4a 75 6c 69 61 6e 20 44 61  e as a Julian Da
ea3b0 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f 20 2a 70  y number into *p
ea3c0 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72 65 74 75  rNow and.** retu
ea3d0 72 6e 20 30 2e 20 20 52 65 74 75 72 6e 20 31 20  rn 0.  Return 1 
ea3e0 69 66 20 74 68 65 20 74 69 6d 65 20 61 6e 64 20  if the time and 
ea3f0 64 61 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 66  date cannot be f
ea400 6f 75 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ound..*/.static 
ea410 69 6e 74 20 75 6e 69 78 43 75 72 72 65 6e 74 54  int unixCurrentT
ea420 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ime(sqlite3_vfs 
ea430 2a 4e 6f 74 55 73 65 64 2c 20 64 6f 75 62 6c 65  *NotUsed, double
ea440 20 2a 70 72 4e 6f 77 29 7b 0a 23 69 66 20 64 65   *prNow){.#if de
ea450 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
ea460 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
ea470 29 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20  ).  time_t t;.  
ea480 74 69 6d 65 28 26 74 29 3b 0a 20 20 2a 70 72 4e  time(&t);.  *prN
ea490 6f 77 20 3d 20 28 28 28 73 71 6c 69 74 65 33 5f  ow = (((sqlite3_
ea4a0 69 6e 74 36 34 29 74 29 2f 38 36 34 30 20 2b 20  int64)t)/8640 + 
ea4b0 32 34 34 30 35 38 37 35 29 2f 31 30 3b 0a 23 65  24405875)/10;.#e
ea4c0 6c 69 66 20 64 65 66 69 6e 65 64 28 4e 4f 5f 47  lif defined(NO_G
ea4d0 45 54 54 4f 44 29 0a 20 20 74 69 6d 65 5f 74 20  ETTOD).  time_t 
ea4e0 74 3b 0a 20 20 74 69 6d 65 28 26 74 29 3b 0a 20  t;.  time(&t);. 
ea4f0 20 2a 70 72 4e 6f 77 20 3d 20 74 2f 38 36 34 30   *prNow = t/8640
ea500 30 2e 30 20 2b 20 32 34 34 30 35 38 37 2e 35 3b  0.0 + 2440587.5;
ea510 0a 23 65 6c 69 66 20 4f 53 5f 56 58 57 4f 52 4b  .#elif OS_VXWORK
ea520 53 0a 20 20 73 74 72 75 63 74 20 74 69 6d 65 73  S.  struct times
ea530 70 65 63 20 73 4e 6f 77 3b 0a 20 20 63 6c 6f 63  pec sNow;.  cloc
ea540 6b 5f 67 65 74 74 69 6d 65 28 43 4c 4f 43 4b 5f  k_gettime(CLOCK_
ea550 52 45 41 4c 54 49 4d 45 2c 20 26 73 4e 6f 77 29  REALTIME, &sNow)
ea560 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34  ;.  *prNow = 244
ea570 30 35 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76  0587.5 + sNow.tv
ea580 5f 73 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 73  _sec/86400.0 + s
ea590 4e 6f 77 2e 74 76 5f 6e 73 65 63 2f 38 36 34 30  Now.tv_nsec/8640
ea5a0 30 30 30 30 30 30 30 30 30 30 2e 30 3b 0a 23 65  0000000000.0;.#e
ea5b0 6c 73 65 0a 20 20 73 74 72 75 63 74 20 74 69 6d  lse.  struct tim
ea5c0 65 76 61 6c 20 73 4e 6f 77 3b 0a 20 20 67 65 74  eval sNow;.  get
ea5d0 74 69 6d 65 6f 66 64 61 79 28 26 73 4e 6f 77 2c  timeofday(&sNow,
ea5e0 20 30 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d 20   0);.  *prNow = 
ea5f0 32 34 34 30 35 38 37 2e 35 20 2b 20 73 4e 6f 77  2440587.5 + sNow
ea600 2e 74 76 5f 73 65 63 2f 38 36 34 30 30 2e 30 20  .tv_sec/86400.0 
ea610 2b 20 73 4e 6f 77 2e 74 76 5f 75 73 65 63 2f 38  + sNow.tv_usec/8
ea620 36 34 30 30 30 30 30 30 30 30 2e 30 3b 0a 23 65  6400000000.0;.#e
ea630 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
ea640 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 73  ITE_TEST.  if( s
ea650 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74  qlite3_current_t
ea660 69 6d 65 20 29 7b 0a 20 20 20 20 2a 70 72 4e 6f  ime ){.    *prNo
ea670 77 20 3d 20 73 71 6c 69 74 65 33 5f 63 75 72 72  w = sqlite3_curr
ea680 65 6e 74 5f 74 69 6d 65 2f 38 36 34 30 30 2e 30  ent_time/86400.0
ea690 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 20 20   + 2440587.5;.  
ea6a0 7d 0a 23 65 6e 64 69 66 0a 20 20 55 4e 55 53 45  }.#endif.  UNUSE
ea6b0 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55  D_PARAMETER(NotU
ea6c0 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 30  sed);.  return 0
ea6d0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 65 20 61 64  ;.}../*.** We ad
ea6e0 64 65 64 20 74 68 65 20 78 47 65 74 4c 61 73 74  ded the xGetLast
ea6f0 45 72 72 6f 72 28 29 20 6d 65 74 68 6f 64 20 77  Error() method w
ea700 69 74 68 20 74 68 65 20 69 6e 74 65 6e 74 69 6f  ith the intentio
ea710 6e 20 6f 66 20 70 72 6f 76 69 64 69 6e 67 0a 2a  n of providing.*
ea720 2a 20 62 65 74 74 65 72 20 6c 6f 77 2d 6c 65 76  * better low-lev
ea730 65 6c 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  el error message
ea740 73 20 77 68 65 6e 20 6f 70 65 72 61 74 69 6e 67  s when operating
ea750 2d 73 79 73 74 65 6d 20 70 72 6f 62 6c 65 6d 73  -system problems
ea760 20 63 6f 6d 65 20 75 70 0a 2a 2a 20 64 75 72 69   come up.** duri
ea770 6e 67 20 53 51 4c 69 74 65 20 6f 70 65 72 61 74  ng SQLite operat
ea780 69 6f 6e 2e 20 20 42 75 74 20 73 6f 20 66 61 72  ion.  But so far
ea790 2c 20 6e 6f 6e 65 20 6f 66 20 74 68 61 74 20 68  , none of that h
ea7a0 61 73 20 62 65 65 6e 20 69 6d 70 6c 65 6d 65 6e  as been implemen
ea7b0 74 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 63 6f  ted.** in the co
ea7c0 72 65 2e 20 20 53 6f 20 74 68 69 73 20 72 6f 75  re.  So this rou
ea7d0 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63 61  tine is never ca
ea7e0 6c 6c 65 64 2e 20 20 46 6f 72 20 6e 6f 77 2c 20  lled.  For now, 
ea7f0 69 74 20 69 73 20 6d 65 72 65 6c 79 0a 2a 2a 20  it is merely.** 
ea800 61 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72 2e 0a  a place-holder..
ea810 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e  */.static int un
ea820 69 78 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73  ixGetLastError(s
ea830 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55  qlite3_vfs *NotU
ea840 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55 73 65 64  sed, int NotUsed
ea850 32 2c 20 63 68 61 72 20 2a 4e 6f 74 55 73 65 64  2, char *NotUsed
ea860 33 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  3){.  UNUSED_PAR
ea870 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
ea880 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
ea890 54 45 52 28 4e 6f 74 55 73 65 64 32 29 3b 0a 20  TER(NotUsed2);. 
ea8a0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
ea8b0 52 28 4e 6f 74 55 73 65 64 33 29 3b 0a 20 20 72  R(NotUsed3);.  r
ea8c0 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
ea8d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea8e0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73  ******* End of s
ea8f0 71 6c 69 74 65 33 5f 76 66 73 20 6d 65 74 68 6f  qlite3_vfs metho
ea900 64 73 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ds *************
ea910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
ea920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
ea970 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
ea980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea9a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea9b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
ea9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ea9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
ea9e0 20 50 72 6f 78 79 20 4c 6f 63 6b 69 6e 67 20 2a   Proxy Locking *
ea9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
eaa00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
eaa10 2a 2a 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b  **.** Proxy lock
eaa20 69 6e 67 20 69 73 20 61 20 22 75 62 65 72 2d 6c  ing is a "uber-l
eaa30 6f 63 6b 69 6e 67 2d 6d 65 74 68 6f 64 22 20 69  ocking-method" i
eaa40 6e 20 74 68 69 73 20 73 65 6e 73 65 3a 20 20 49  n this sense:  I
eaa50 74 20 75 73 65 73 20 74 68 65 0a 2a 2a 20 6f 74  t uses the.** ot
eaa60 68 65 72 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68  her locking meth
eaa70 6f 64 73 20 6f 6e 20 73 65 63 6f 6e 64 61 72 79  ods on secondary
eaa80 20 6c 6f 63 6b 20 66 69 6c 65 73 2e 20 20 50 72   lock files.  Pr
eaa90 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 69 73 20 61  oxy locking is a
eaaa0 0a 2a 2a 20 6d 65 74 61 2d 6c 61 79 65 72 20 6f  .** meta-layer o
eaab0 76 65 72 20 74 6f 70 20 6f 66 20 74 68 65 20 70  ver top of the p
eaac0 72 69 6d 69 74 69 76 65 20 6c 6f 63 6b 69 6e 67  rimitive locking
eaad0 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 62 6f   implemented abo
eaae0 76 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 69 73  ve.  For.** this
eaaf0 20 72 65 61 73 6f 6e 2c 20 74 68 65 20 64 69 76   reason, the div
eab00 69 73 69 6f 6e 20 74 68 61 74 20 69 6d 70 6c 65  ision that imple
eab10 6d 65 6e 74 73 20 6f 66 20 70 72 6f 78 79 20 6c  ments of proxy l
eab20 6f 63 6b 69 6e 67 20 69 73 20 64 65 66 65 72 72  ocking is deferr
eab30 65 64 0a 2a 2a 20 75 6e 74 69 6c 20 6c 61 74 65  ed.** until late
eab40 20 69 6e 20 74 68 65 20 66 69 6c 65 20 28 68 65   in the file (he
eab50 72 65 29 20 61 66 74 65 72 20 61 6c 6c 20 6f 66  re) after all of
eab60 20 74 68 65 20 6f 74 68 65 72 20 49 2f 4f 20 6d   the other I/O m
eab70 65 74 68 6f 64 73 20 68 61 76 65 0a 2a 2a 20 62  ethods have.** b
eab80 65 65 6e 20 64 65 66 69 6e 65 64 20 2d 20 73 6f  een defined - so
eab90 20 74 68 61 74 20 74 68 65 20 70 72 69 6d 69 74   that the primit
eaba0 69 76 65 20 6c 6f 63 6b 69 6e 67 20 6d 65 74 68  ive locking meth
eabb0 6f 64 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c  ods are availabl
eabc0 65 0a 2a 2a 20 61 73 20 73 65 72 76 69 63 65 73  e.** as services
eabd0 20 74 6f 20 68 65 6c 70 20 77 69 74 68 20 74 68   to help with th
eabe0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
eabf0 20 6f 66 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e   of proxy lockin
eac00 67 2e 0a 2a 2a 0a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  g..**.****.**.**
eac10 20 54 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63   The default loc
eac20 6b 69 6e 67 20 73 63 68 65 6d 65 73 20 69 6e 20  king schemes in 
eac30 53 51 4c 69 74 65 20 75 73 65 20 62 79 74 65 2d  SQLite use byte-
eac40 72 61 6e 67 65 20 6c 6f 63 6b 73 20 6f 6e 20 74  range locks on t
eac50 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66  he.** database f
eac60 69 6c 65 20 74 6f 20 63 6f 6f 72 64 69 6e 61 74  ile to coordinat
eac70 65 20 73 61 66 65 2c 20 63 6f 6e 63 75 72 72 65  e safe, concurre
eac80 6e 74 20 61 63 63 65 73 73 20 62 79 20 6d 75 6c  nt access by mul
eac90 74 69 70 6c 65 20 72 65 61 64 65 72 73 0a 2a 2a  tiple readers.**
eaca0 20 61 6e 64 20 77 72 69 74 65 72 73 20 5b 68 74   and writers [ht
eacb0 74 70 3a 2f 2f 73 71 6c 69 74 65 2e 6f 72 67 2f  tp://sqlite.org/
eacc0 6c 6f 63 6b 69 6e 67 76 33 2e 68 74 6d 6c 5d 2e  lockingv3.html].
eacd0 20 20 54 68 65 20 66 69 76 65 20 66 69 6c 65 20    The five file 
eace0 6c 6f 63 6b 69 6e 67 0a 2a 2a 20 73 74 61 74 65  locking.** state
eacf0 73 20 28 55 4e 4c 4f 43 4b 45 44 2c 20 50 45 4e  s (UNLOCKED, PEN
ead00 44 49 4e 47 2c 20 53 48 41 52 45 44 2c 20 52 45  DING, SHARED, RE
ead10 53 45 52 56 45 44 2c 20 45 58 43 4c 55 53 49 56  SERVED, EXCLUSIV
ead20 45 29 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74  E) are implement
ead30 65 64 0a 2a 2a 20 61 73 20 50 4f 53 49 58 20 72  ed.** as POSIX r
ead40 65 61 64 20 26 20 77 72 69 74 65 20 6c 6f 63 6b  ead & write lock
ead50 73 20 6f 76 65 72 20 66 69 78 65 64 20 73 65 74  s over fixed set
ead60 20 6f 66 20 6c 6f 63 61 74 69 6f 6e 73 20 28 76   of locations (v
ead70 69 61 20 66 73 63 74 6c 29 2c 0a 2a 2a 20 6f 6e  ia fsctl),.** on
ead80 20 41 46 50 20 61 6e 64 20 53 4d 42 20 6f 6e 6c   AFP and SMB onl
ead90 79 20 65 78 63 6c 75 73 69 76 65 20 62 79 74 65  y exclusive byte
eada0 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 20 61 72 65  -range locks are
eadb0 20 61 76 61 69 6c 61 62 6c 65 20 76 69 61 20 66   available via f
eadc0 73 63 74 6c 0a 2a 2a 20 77 69 74 68 20 5f 49 4f  sctl.** with _IO
eadd0 57 52 28 27 7a 27 2c 20 32 33 2c 20 73 74 72 75  WR('z', 23, stru
eade0 63 74 20 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b  ct ByteRangeLock
eadf0 50 42 32 29 20 74 6f 20 74 72 61 63 6b 20 74 68  PB2) to track th
eae00 65 20 73 61 6d 65 20 35 20 73 74 61 74 65 73 2e  e same 5 states.
eae10 0a 2a 2a 20 54 6f 20 73 69 6d 75 6c 61 74 65 20  .** To simulate 
eae20 61 20 46 5f 52 44 4c 43 4b 20 6f 6e 20 74 68 65  a F_RDLCK on the
eae30 20 73 68 61 72 65 64 20 72 61 6e 67 65 2c 20 6f   shared range, o
eae40 6e 20 41 46 50 20 61 20 72 61 6e 64 6f 6d 6c 79  n AFP a randomly
eae50 20 73 65 6c 65 63 74 65 64 0a 2a 2a 20 61 64 64   selected.** add
eae60 72 65 73 73 20 69 6e 20 74 68 65 20 73 68 61 72  ress in the shar
eae70 65 64 20 72 61 6e 67 65 20 69 73 20 74 61 6b 65  ed range is take
eae80 6e 20 66 6f 72 20 61 20 53 48 41 52 45 44 20 6c  n for a SHARED l
eae90 6f 63 6b 2c 20 74 68 65 20 65 6e 74 69 72 65 0a  ock, the entire.
eaea0 2a 2a 20 73 68 61 72 65 64 20 72 61 6e 67 65 20  ** shared range 
eaeb0 69 73 20 74 61 6b 65 6e 20 66 6f 72 20 61 6e 20  is taken for an 
eaec0 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 29 3a  EXCLUSIVE lock):
eaed0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 50 45 4e 44  .**.**      PEND
eaee0 49 4e 47 5f 42 59 54 45 20 20 20 20 20 20 20 20  ING_BYTE        
eaef0 30 78 34 30 30 30 30 30 30 30 09 09 20 20 20 09  0x40000000..   .
eaf00 0a 2a 2a 20 20 20 20 20 20 52 45 53 45 52 56 45  .**      RESERVE
eaf10 44 5f 42 59 54 45 20 20 20 20 20 20 20 30 78 34  D_BYTE       0x4
eaf20 30 30 30 30 30 30 31 0a 2a 2a 20 20 20 20 20 20  0000001.**      
eaf30 53 48 41 52 45 44 5f 52 41 4e 47 45 20 20 20 20  SHARED_RANGE    
eaf40 20 20 20 20 30 78 34 30 30 30 30 30 30 32 20 2d      0x40000002 -
eaf50 3e 20 30 78 34 30 30 30 30 32 30 30 0a 2a 2a 0a  > 0x40000200.**.
eaf60 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 77 65  ** This works we
eaf70 6c 6c 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20  ll on the local 
eaf80 66 69 6c 65 20 73 79 73 74 65 6d 2c 20 62 75 74  file system, but
eaf90 20 73 68 6f 77 73 20 61 20 6e 65 61 72 6c 79 20   shows a nearly 
eafa0 31 30 30 78 0a 2a 2a 20 73 6c 6f 77 64 6f 77 6e  100x.** slowdown
eafb0 20 69 6e 20 72 65 61 64 20 70 65 72 66 6f 72 6d   in read perform
eafc0 61 6e 63 65 20 6f 6e 20 41 46 50 20 62 65 63 61  ance on AFP beca
eafd0 75 73 65 20 74 68 65 20 41 46 50 20 63 6c 69 65  use the AFP clie
eafe0 6e 74 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 74  nt disables.** t
eaff0 68 65 20 72 65 61 64 20 63 61 63 68 65 20 77 68  he read cache wh
eb000 65 6e 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f  en byte-range lo
eb010 63 6b 73 20 61 72 65 20 70 72 65 73 65 6e 74 2e  cks are present.
eb020 20 20 45 6e 61 62 6c 69 6e 67 20 74 68 65 20 72    Enabling the r
eb030 65 61 64 0a 2a 2a 20 63 61 63 68 65 20 65 78 70  ead.** cache exp
eb040 6f 73 65 73 20 61 20 63 61 63 68 65 20 63 6f 68  oses a cache coh
eb050 65 72 65 6e 63 79 20 70 72 6f 62 6c 65 6d 20 74  erency problem t
eb060 68 61 74 20 69 73 20 70 72 65 73 65 6e 74 20 6f  hat is present o
eb070 6e 20 61 6c 6c 20 4f 53 20 58 0a 2a 2a 20 73 75  n all OS X.** su
eb080 70 70 6f 72 74 65 64 20 6e 65 74 77 6f 72 6b 20  pported network 
eb090 66 69 6c 65 20 73 79 73 74 65 6d 73 2e 20 20 4e  file systems.  N
eb0a0 46 53 20 61 6e 64 20 41 46 50 20 62 6f 74 68 20  FS and AFP both 
eb0b0 6f 62 73 65 72 76 65 20 74 68 65 0a 2a 2a 20 63  observe the.** c
eb0c0 6c 6f 73 65 2d 74 6f 2d 6f 70 65 6e 20 73 65 6d  lose-to-open sem
eb0d0 61 6e 74 69 63 73 20 66 6f 72 20 65 6e 73 75 72  antics for ensur
eb0e0 69 6e 67 20 63 61 63 68 65 20 63 6f 68 65 72 65  ing cache cohere
eb0f0 6e 63 79 0a 2a 2a 20 5b 68 74 74 70 3a 2f 2f 6e  ncy.** [http://n
eb100 66 73 2e 73 6f 75 72 63 65 66 6f 72 67 65 2e 6e  fs.sourceforge.n
eb110 65 74 2f 23 66 61 71 5f 61 38 5d 2c 20 77 68 69  et/#faq_a8], whi
eb120 63 68 20 64 6f 65 73 20 6e 6f 74 20 65 66 66 65  ch does not effe
eb130 63 74 69 76 65 6c 79 0a 2a 2a 20 61 64 64 72 65  ctively.** addre
eb140 73 73 20 74 68 65 20 72 65 71 75 69 72 65 6d 65  ss the requireme
eb150 6e 74 73 20 66 6f 72 20 63 6f 6e 63 75 72 72 65  nts for concurre
eb160 6e 74 20 64 61 74 61 62 61 73 65 20 61 63 63 65  nt database acce
eb170 73 73 20 62 79 20 6d 75 6c 74 69 70 6c 65 0a 2a  ss by multiple.*
eb180 2a 20 72 65 61 64 65 72 73 20 61 6e 64 20 77 72  * readers and wr
eb190 69 74 65 72 73 0a 2a 2a 20 5b 68 74 74 70 3a 2f  iters.** [http:/
eb1a0 2f 77 77 77 2e 6e 61 62 62 6c 65 2e 63 6f 6d 2f  /www.nabble.com/
eb1b0 53 51 4c 69 74 65 2d 6f 6e 2d 4e 46 53 2d 63 61  SQLite-on-NFS-ca
eb1c0 63 68 65 2d 63 6f 68 65 72 65 6e 63 79 2d 74 64  che-coherency-td
eb1d0 31 35 36 35 35 37 30 31 2e 68 74 6d 6c 5d 2e 0a  15655701.html]..
eb1e0 2a 2a 0a 2a 2a 20 54 6f 20 61 64 64 72 65 73 73  **.** To address
eb1f0 20 74 68 65 20 70 65 72 66 6f 72 6d 61 6e 63 65   the performance
eb200 20 61 6e 64 20 63 61 63 68 65 20 63 6f 68 65 72   and cache coher
eb210 65 6e 63 79 20 69 73 73 75 65 73 2c 20 70 72 6f  ency issues, pro
eb220 78 79 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a  xy file locking.
eb230 2a 2a 20 63 68 61 6e 67 65 73 20 74 68 65 20 77  ** changes the w
eb240 61 79 20 64 61 74 61 62 61 73 65 20 61 63 63 65  ay database acce
eb250 73 73 20 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64  ss is controlled
eb260 20 62 79 20 6c 69 6d 69 74 69 6e 67 20 61 63 63   by limiting acc
eb270 65 73 73 20 74 6f 20 61 0a 2a 2a 20 73 69 6e 67  ess to a.** sing
eb280 6c 65 20 68 6f 73 74 20 61 74 20 61 20 74 69 6d  le host at a tim
eb290 65 20 61 6e 64 20 6d 6f 76 69 6e 67 20 66 69 6c  e and moving fil
eb2a0 65 20 6c 6f 63 6b 73 20 6f 66 66 20 6f 66 20 74  e locks off of t
eb2b0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
eb2c0 0a 2a 2a 20 61 6e 64 20 6f 6e 74 6f 20 61 20 70  .** and onto a p
eb2d0 72 6f 78 79 20 66 69 6c 65 20 6f 6e 20 74 68 65  roxy file on the
eb2e0 20 6c 6f 63 61 6c 20 66 69 6c 65 20 73 79 73 74   local file syst
eb2f0 65 6d 2e 20 20 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 55  em.  .**.**.** U
eb300 73 69 6e 67 20 70 72 6f 78 79 20 6c 6f 63 6b 73  sing proxy locks
eb310 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .** ------------
eb320 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 43 20 41 50  -----.**.** C AP
eb330 49 73 0a 2a 2a 0a 2a 2a 20 20 73 71 6c 69 74 65  Is.**.**  sqlite
eb340 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64  3_file_control(d
eb350 62 2c 20 64 62 6e 61 6d 65 2c 20 53 51 4c 49 54  b, dbname, SQLIT
eb360 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46  E_SET_LOCKPROXYF
eb370 49 4c 45 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  ILE,.**         
eb380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 3c 70                <p
eb390 72 6f 78 79 5f 70 61 74 68 3e 20 7c 20 22 3a 61  roxy_path> | ":a
eb3a0 75 74 6f 3a 22 29 3b 0a 2a 2a 20 20 73 71 6c 69  uto:");.**  sqli
eb3b0 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c  te3_file_control
eb3c0 28 64 62 2c 20 64 62 6e 61 6d 65 2c 20 53 51 4c  (db, dbname, SQL
eb3d0 49 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58  ITE_GET_LOCKPROX
eb3e0 59 46 49 4c 45 2c 20 26 3c 70 72 6f 78 79 5f 70  YFILE, &<proxy_p
eb3f0 61 74 68 3e 29 3b 0a 2a 2a 0a 2a 2a 0a 2a 2a 20  ath>);.**.**.** 
eb400 53 51 4c 20 70 72 61 67 6d 61 73 0a 2a 2a 0a 2a  SQL pragmas.**.*
eb410 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62  *  PRAGMA [datab
eb420 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f  ase.]lock_proxy_
eb430 66 69 6c 65 3d 3c 70 72 6f 78 79 5f 70 61 74 68  file=<proxy_path
eb440 3e 20 7c 20 3a 61 75 74 6f 3a 0a 2a 2a 20 20 50  > | :auto:.**  P
eb450 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
eb460 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65  ]lock_proxy_file
eb470 0a 2a 2a 0a 2a 2a 20 53 70 65 63 69 66 79 69 6e  .**.** Specifyin
eb480 67 20 22 3a 61 75 74 6f 3a 22 20 6d 65 61 6e 73  g ":auto:" means
eb490 20 74 68 61 74 20 69 66 20 74 68 65 72 65 20 69   that if there i
eb4a0 73 20 61 20 63 6f 6e 63 68 20 66 69 6c 65 20 77  s a conch file w
eb4b0 69 74 68 20 61 20 6d 61 74 63 68 69 6e 67 0a 2a  ith a matching.*
eb4c0 2a 20 68 6f 73 74 20 49 44 20 69 6e 20 69 74 2c  * host ID in it,
eb4d0 20 74 68 65 20 70 72 6f 78 79 20 70 61 74 68 20   the proxy path 
eb4e0 69 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c  in the conch fil
eb4f0 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 2c 20  e will be used, 
eb500 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20 61 20 70  otherwise.** a p
eb510 72 6f 78 79 20 70 61 74 68 20 62 61 73 65 64 20  roxy path based 
eb520 6f 6e 20 74 68 65 20 75 73 65 72 27 73 20 74 65  on the user's te
eb530 6d 70 20 64 69 72 0a 2a 2a 20 28 76 69 61 20 63  mp dir.** (via c
eb540 6f 6e 66 73 74 72 28 5f 43 53 5f 44 41 52 57 49  onfstr(_CS_DARWI
eb550 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44 49 52 2c  N_USER_TEMP_DIR,
eb560 2e 2e 2e 29 29 20 77 69 6c 6c 20 62 65 20 75 73  ...)) will be us
eb570 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 61 63  ed and the.** ac
eb580 74 75 61 6c 20 70 72 6f 78 79 20 66 69 6c 65 20  tual proxy file 
eb590 6e 61 6d 65 20 69 73 20 67 65 6e 65 72 61 74 65  name is generate
eb5a0 64 20 66 72 6f 6d 20 74 68 65 20 6e 61 6d 65 20  d from the name 
eb5b0 61 6e 64 20 70 61 74 68 20 6f 66 20 74 68 65 0a  and path of the.
eb5c0 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
eb5d0 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a  .  For example:.
eb5e0 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 46 6f 72 20  **.**       For 
eb5f0 64 61 74 61 62 61 73 65 20 70 61 74 68 20 22 2f  database path "/
eb600 55 73 65 72 73 2f 6d 65 2f 66 6f 6f 2e 64 62 22  Users/me/foo.db"
eb610 20 0a 2a 2a 20 20 20 20 20 20 20 54 68 65 20 6c   .**       The l
eb620 6f 63 6b 20 70 61 74 68 20 77 69 6c 6c 20 62 65  ock path will be
eb630 20 22 3c 74 6d 70 64 69 72 3e 2f 73 71 6c 69 74   "<tmpdir>/sqlit
eb640 65 70 6c 6f 63 6b 73 2f 5f 55 73 65 72 73 5f 6d  eplocks/_Users_m
eb650 65 5f 66 6f 6f 2e 64 62 3a 61 75 74 6f 3a 22 29  e_foo.db:auto:")
eb660 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 61 20 6c 6f  .**.** Once a lo
eb670 63 6b 20 70 72 6f 78 79 20 69 73 20 63 6f 6e 66  ck proxy is conf
eb680 69 67 75 72 65 64 20 66 6f 72 20 61 20 64 61 74  igured for a dat
eb690 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
eb6a0 2c 20 69 74 20 63 61 6e 20 6e 6f 74 0a 2a 2a 20  , it can not.** 
eb6b0 62 65 20 72 65 6d 6f 76 65 64 2c 20 68 6f 77 65  be removed, howe
eb6c0 76 65 72 20 69 74 20 6d 61 79 20 62 65 20 73 77  ver it may be sw
eb6d0 69 74 63 68 65 64 20 74 6f 20 61 20 64 69 66 66  itched to a diff
eb6e0 65 72 65 6e 74 20 70 72 6f 78 79 20 70 61 74 68  erent proxy path
eb6f0 20 76 69 61 0a 2a 2a 20 74 68 65 20 61 62 6f 76   via.** the abov
eb700 65 20 41 50 49 73 20 28 61 73 73 75 6d 69 6e 67  e APIs (assuming
eb710 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20   the conch file 
eb720 69 73 20 6e 6f 74 20 62 65 69 6e 67 20 68 65 6c  is not being hel
eb730 64 20 62 79 20 61 6e 6f 74 68 65 72 0a 2a 2a 20  d by another.** 
eb740 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 72 20 70 72  connection or pr
eb750 6f 63 65 73 73 29 2e 20 0a 2a 2a 0a 2a 2a 0a 2a  ocess). .**.**.*
eb760 2a 20 48 6f 77 20 70 72 6f 78 79 20 6c 6f 63 6b  * How proxy lock
eb770 69 6e 67 20 77 6f 72 6b 73 0a 2a 2a 20 2d 2d 2d  ing works.** ---
eb780 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
eb790 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 50 72 6f 78 79  ----.**.** Proxy
eb7a0 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 72 65   file locking re
eb7b0 6c 69 65 73 20 70 72 69 6d 61 72 69 6c 79 20 6f  lies primarily o
eb7c0 6e 20 74 77 6f 20 6e 65 77 20 73 75 70 70 6f 72  n two new suppor
eb7d0 74 69 6e 67 20 66 69 6c 65 73 3a 20 0a 2a 2a 0a  ting files: .**.
eb7e0 2a 2a 20 20 20 2a 20 20 63 6f 6e 63 68 20 66 69  **   *  conch fi
eb7f0 6c 65 20 74 6f 20 6c 69 6d 69 74 20 61 63 63 65  le to limit acce
eb800 73 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ss to the databa
eb810 73 65 20 66 69 6c 65 20 74 6f 20 61 20 73 69 6e  se file to a sin
eb820 67 6c 65 20 68 6f 73 74 0a 2a 2a 20 20 20 20 20  gle host.**     
eb830 20 61 74 20 61 20 74 69 6d 65 0a 2a 2a 0a 2a 2a   at a time.**.**
eb840 20 20 20 2a 20 20 70 72 6f 78 79 20 66 69 6c 65     *  proxy file
eb850 20 74 6f 20 61 63 74 20 61 73 20 61 20 70 72 6f   to act as a pro
eb860 78 79 20 66 6f 72 20 74 68 65 20 61 64 76 69 73  xy for the advis
eb870 6f 72 79 20 6c 6f 63 6b 73 20 6e 6f 72 6d 61 6c  ory locks normal
eb880 6c 79 0a 2a 2a 20 20 20 20 20 20 74 61 6b 65 6e  ly.**      taken
eb890 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
eb8a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 63 68  .**.** The conch
eb8b0 20 66 69 6c 65 20 2d 20 74 6f 20 75 73 65 20 61   file - to use a
eb8c0 20 70 72 6f 78 79 20 66 69 6c 65 2c 20 73 71 6c   proxy file, sql
eb8d0 69 74 65 20 6d 75 73 74 20 66 69 72 73 74 20 22  ite must first "
eb8e0 68 6f 6c 64 20 74 68 65 20 63 6f 6e 63 68 22 0a  hold the conch".
eb8f0 2a 2a 20 62 79 20 74 61 6b 69 6e 67 20 61 6e 20  ** by taking an 
eb900 73 71 6c 69 74 65 2d 73 74 79 6c 65 20 73 68 61  sqlite-style sha
eb910 72 65 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  red lock on the 
eb920 63 6f 6e 63 68 20 66 69 6c 65 2c 20 72 65 61 64  conch file, read
eb930 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65  ing the.** conte
eb940 6e 74 73 20 61 6e 64 20 63 6f 6d 70 61 72 69 6e  nts and comparin
eb950 67 20 74 68 65 20 68 6f 73 74 27 73 20 75 6e 69  g the host's uni
eb960 71 75 65 20 68 6f 73 74 20 49 44 20 28 73 65 65  que host ID (see
eb970 20 62 65 6c 6f 77 29 20 61 6e 64 20 6c 6f 63 6b   below) and lock
eb980 0a 2a 2a 20 70 72 6f 78 79 20 70 61 74 68 20 61  .** proxy path a
eb990 67 61 69 6e 73 74 20 74 68 65 20 76 61 6c 75 65  gainst the value
eb9a0 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  s stored in the 
eb9b0 63 6f 6e 63 68 2e 20 20 54 68 65 20 63 6f 6e 63  conch.  The conc
eb9c0 68 20 66 69 6c 65 20 69 73 0a 2a 2a 20 73 74 6f  h file is.** sto
eb9d0 72 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20  red in the same 
eb9e0 64 69 72 65 63 74 6f 72 79 20 61 73 20 74 68 65  directory as the
eb9f0 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
eba00 6e 64 20 74 68 65 20 66 69 6c 65 20 6e 61 6d 65  nd the file name
eba10 0a 2a 2a 20 69 73 20 70 61 74 74 65 72 6e 65 64  .** is patterned
eba20 20 61 66 74 65 72 20 74 68 65 20 64 61 74 61 62   after the datab
eba30 61 73 65 20 66 69 6c 65 20 6e 61 6d 65 20 61 73  ase file name as
eba40 20 22 2e 3c 64 61 74 61 62 61 73 65 6e 61 6d 65   ".<databasename
eba50 3e 2d 63 6f 6e 63 68 22 2e 0a 2a 2a 20 49 66 20  >-conch"..** If 
eba60 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 20 64  the conch file d
eba70 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20 6f  oes not exist, o
eba80 72 20 69 74 27 73 20 63 6f 6e 74 65 6e 74 73 20  r it's contents 
eba90 64 6f 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65  do not match the
ebaa0 0a 2a 2a 20 68 6f 73 74 20 49 44 20 61 6e 64 2f  .** host ID and/
ebab0 6f 72 20 70 72 6f 78 79 20 70 61 74 68 2c 20 74  or proxy path, t
ebac0 68 65 6e 20 74 68 65 20 6c 6f 63 6b 20 69 73 20  hen the lock is 
ebad0 65 73 63 61 6c 61 74 65 64 20 74 6f 20 61 6e 20  escalated to an 
ebae0 65 78 63 6c 75 73 69 76 65 0a 2a 2a 20 6c 6f 63  exclusive.** loc
ebaf0 6b 20 61 6e 64 20 74 68 65 20 63 6f 6e 63 68 20  k and the conch 
ebb00 66 69 6c 65 20 63 6f 6e 74 65 6e 74 73 20 69 73  file contents is
ebb10 20 75 70 64 61 74 65 64 20 77 69 74 68 20 74 68   updated with th
ebb20 65 20 68 6f 73 74 20 49 44 20 61 6e 64 20 70 72  e host ID and pr
ebb30 6f 78 79 0a 2a 2a 20 70 61 74 68 20 61 6e 64 20  oxy.** path and 
ebb40 74 68 65 20 6c 6f 63 6b 20 69 73 20 64 6f 77 6e  the lock is down
ebb50 67 72 61 64 65 64 20 74 6f 20 61 20 73 68 61 72  graded to a shar
ebb60 65 64 20 6c 6f 63 6b 20 61 67 61 69 6e 2e 20 20  ed lock again.  
ebb70 49 66 20 74 68 65 20 63 6f 6e 63 68 0a 2a 2a 20  If the conch.** 
ebb80 69 73 20 68 65 6c 64 20 62 79 20 61 6e 6f 74 68  is held by anoth
ebb90 65 72 20 70 72 6f 63 65 73 73 20 28 77 69 74 68  er process (with
ebba0 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 29 2c   a shared lock),
ebbb0 20 74 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c   the exclusive l
ebbc0 6f 63 6b 0a 2a 2a 20 77 69 6c 6c 20 66 61 69 6c  ock.** will fail
ebbd0 20 61 6e 64 20 53 51 4c 49 54 45 5f 42 55 53 59   and SQLITE_BUSY
ebbe0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
ebbf0 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 20 66 69  .** The proxy fi
ebc00 6c 65 20 2d 20 61 20 73 69 6e 67 6c 65 2d 62 79  le - a single-by
ebc10 74 65 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72  te file used for
ebc20 20 61 6c 6c 20 61 64 76 69 73 6f 72 79 20 66 69   all advisory fi
ebc30 6c 65 20 6c 6f 63 6b 73 0a 2a 2a 20 6e 6f 72 6d  le locks.** norm
ebc40 61 6c 6c 79 20 74 61 6b 65 6e 20 6f 6e 20 74 68  ally taken on th
ebc50 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
ebc60 20 20 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 66     This allows f
ebc70 6f 72 20 73 61 66 65 20 73 68 61 72 69 6e 67 0a  or safe sharing.
ebc80 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ** of the databa
ebc90 73 65 20 66 69 6c 65 20 66 6f 72 20 6d 75 6c 74  se file for mult
ebca0 69 70 6c 65 20 72 65 61 64 65 72 73 20 61 6e 64  iple readers and
ebcb0 20 77 72 69 74 65 72 73 20 6f 6e 20 74 68 65 20   writers on the 
ebcc0 73 61 6d 65 0a 2a 2a 20 68 6f 73 74 20 28 74 68  same.** host (th
ebcd0 65 20 63 6f 6e 63 68 20 65 6e 73 75 72 65 73 20  e conch ensures 
ebce0 74 68 61 74 20 74 68 65 79 20 61 6c 6c 20 75 73  that they all us
ebcf0 65 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 61 6c  e the same local
ebd00 20 6c 6f 63 6b 20 66 69 6c 65 29 2e 0a 2a 2a 0a   lock file)..**.
ebd10 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 74 68  ** There is a th
ebd20 69 72 64 20 66 69 6c 65 20 2d 20 74 68 65 20 68  ird file - the h
ebd30 6f 73 74 20 49 44 20 66 69 6c 65 20 2d 20 75 73  ost ID file - us
ebd40 65 64 20 61 73 20 61 20 70 65 72 73 69 73 74 65  ed as a persiste
ebd50 6e 74 20 72 65 63 6f 72 64 0a 2a 2a 20 6f 66 20  nt record.** of 
ebd60 61 20 75 6e 69 71 75 65 20 69 64 65 6e 74 69 66  a unique identif
ebd70 69 65 72 20 66 6f 72 20 74 68 65 20 68 6f 73 74  ier for the host
ebd80 2c 20 61 20 31 32 38 2d 62 79 74 65 20 75 6e 69  , a 128-byte uni
ebd90 71 75 65 20 68 6f 73 74 20 69 64 20 66 69 6c 65  que host id file
ebda0 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61 74 68 20  .** in the path 
ebdb0 64 65 66 69 6e 65 64 20 62 79 20 74 68 65 20 48  defined by the H
ebdc0 4f 53 54 49 44 50 41 54 48 20 6d 61 63 72 6f 20  OSTIDPATH macro 
ebdd0 28 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 69  (default value i
ebde0 73 0a 2a 2a 20 2f 4c 69 62 72 61 72 79 2f 43 61  s.** /Library/Ca
ebdf0 63 68 65 73 2f 2e 63 6f 6d 2e 61 70 70 6c 65 2e  ches/.com.apple.
ebe00 73 71 6c 69 74 65 43 6f 6e 63 68 48 6f 73 74 49  sqliteConchHostI
ebe10 64 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 71 75 65 73  d)..**.** Reques
ebe20 74 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 70 72  ting the lock pr
ebe30 6f 78 79 20 64 6f 65 73 20 6e 6f 74 20 69 6d 6d  oxy does not imm
ebe40 65 64 69 61 74 65 6c 79 20 74 61 6b 65 20 74 68  ediately take th
ebe50 65 20 63 6f 6e 63 68 2c 20 69 74 20 69 73 0a 2a  e conch, it is.*
ebe60 2a 20 6f 6e 6c 79 20 74 61 6b 65 6e 20 77 68 65  * only taken whe
ebe70 6e 20 74 68 65 20 66 69 72 73 74 20 72 65 71 75  n the first requ
ebe80 65 73 74 20 74 6f 20 6c 6f 63 6b 20 64 61 74 61  est to lock data
ebe90 62 61 73 65 20 66 69 6c 65 20 69 73 20 6d 61 64  base file is mad
ebea0 65 2e 20 20 0a 2a 2a 20 54 68 69 73 20 6d 61 74  e.  .** This mat
ebeb0 63 68 65 73 20 74 68 65 20 73 65 6d 61 6e 74 69  ches the semanti
ebec0 63 73 20 6f 66 20 74 68 65 20 74 72 61 64 69 74  cs of the tradit
ebed0 69 6f 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20 62 65  ional locking be
ebee0 68 61 76 69 6f 72 2c 20 77 68 65 72 65 0a 2a 2a  havior, where.**
ebef0 20 6f 70 65 6e 69 6e 67 20 61 20 63 6f 6e 6e 65   opening a conne
ebf00 63 74 69 6f 6e 20 74 6f 20 61 20 64 61 74 61 62  ction to a datab
ebf10 61 73 65 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f  ase file does no
ebf20 74 20 74 61 6b 65 20 61 20 6c 6f 63 6b 20 6f 6e  t take a lock on
ebf30 20 69 74 2e 0a 2a 2a 20 54 68 65 20 73 68 61 72   it..** The shar
ebf40 65 64 20 6c 6f 63 6b 20 61 6e 64 20 61 6e 20 6f  ed lock and an o
ebf50 70 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70  pen file descrip
ebf60 74 6f 72 20 61 72 65 20 6d 61 69 6e 74 61 69 6e  tor are maintain
ebf70 65 64 20 75 6e 74 69 6c 20 0a 2a 2a 20 74 68 65  ed until .** the
ebf80 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 74   connection to t
ebf90 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 63  he database is c
ebfa0 6c 6f 73 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 54 68  losed. .**.** Th
ebfb0 65 20 70 72 6f 78 79 20 66 69 6c 65 20 61 6e 64  e proxy file and
ebfc0 20 74 68 65 20 6c 6f 63 6b 20 66 69 6c 65 20 61   the lock file a
ebfd0 72 65 20 6e 65 76 65 72 20 64 65 6c 65 74 65 64  re never deleted
ebfe0 20 73 6f 20 74 68 65 79 20 6f 6e 6c 79 20 6e 65   so they only ne
ebff0 65 64 0a 2a 2a 20 74 6f 20 62 65 20 63 72 65 61  ed.** to be crea
ec000 74 65 64 20 74 68 65 20 66 69 72 73 74 20 74 69  ted the first ti
ec010 6d 65 20 74 68 65 79 20 61 72 65 20 75 73 65 64  me they are used
ec020 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72  ..**.** Configur
ec030 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 0a 2a 2a  ation options.**
ec040 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
ec050 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 20 53 51  ------.**.**  SQ
ec060 4c 49 54 45 5f 50 52 45 46 45 52 5f 50 52 4f 58  LITE_PREFER_PROX
ec070 59 5f 4c 4f 43 4b 49 4e 47 0a 2a 2a 0a 2a 2a 20  Y_LOCKING.**.** 
ec080 20 20 20 20 20 20 44 61 74 61 62 61 73 65 20 66        Database f
ec090 69 6c 65 73 20 61 63 63 65 73 73 65 64 20 6f 6e  iles accessed on
ec0a0 20 6e 6f 6e 2d 6c 6f 63 61 6c 20 66 69 6c 65 20   non-local file 
ec0b0 73 79 73 74 65 6d 73 20 61 72 65 0a 2a 2a 20 20  systems are.**  
ec0c0 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63 61 6c       automatical
ec0d0 6c 79 20 63 6f 6e 66 69 67 75 72 65 64 20 66 6f  ly configured fo
ec0e0 72 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 2c  r proxy locking,
ec0f0 20 6c 6f 63 6b 20 66 69 6c 65 73 20 61 72 65 0a   lock files are.
ec100 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65 64 20 61  **       named a
ec110 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 75 73 69  utomatically usi
ec120 6e 67 20 74 68 65 20 73 61 6d 65 20 6c 6f 67 69  ng the same logi
ec130 63 20 61 73 0a 2a 2a 20 20 20 20 20 20 20 50 52  c as.**       PR
ec140 41 47 4d 41 20 6c 6f 63 6b 5f 70 72 6f 78 79 5f  AGMA lock_proxy_
ec150 66 69 6c 65 3d 22 3a 61 75 74 6f 3a 22 0a 2a 2a  file=":auto:".**
ec160 20 20 20 20 0a 2a 2a 20 20 53 51 4c 49 54 45 5f      .**  SQLITE_
ec170 50 52 4f 58 59 5f 44 45 42 55 47 0a 2a 2a 0a 2a  PROXY_DEBUG.**.*
ec180 2a 20 20 20 20 20 20 20 45 6e 61 62 6c 65 73 20  *       Enables 
ec190 74 68 65 20 6c 6f 67 67 69 6e 67 20 6f 66 20 65  the logging of e
ec1a0 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 64 75  rror messages du
ec1b0 72 69 6e 67 20 68 6f 73 74 20 69 64 20 66 69 6c  ring host id fil
ec1c0 65 0a 2a 2a 20 20 20 20 20 20 20 72 65 74 72 69  e.**       retri
ec1d0 65 76 61 6c 20 61 6e 64 20 63 72 65 61 74 69 6f  eval and creatio
ec1e0 6e 0a 2a 2a 0a 2a 2a 20 20 48 4f 53 54 49 44 50  n.**.**  HOSTIDP
ec1f0 41 54 48 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  ATH.**.**       
ec200 4f 76 65 72 72 69 64 65 73 20 74 68 65 20 64 65  Overrides the de
ec210 66 61 75 6c 74 20 68 6f 73 74 20 49 44 20 66 69  fault host ID fi
ec220 6c 65 20 70 61 74 68 20 6c 6f 63 61 74 69 6f 6e  le path location
ec230 0a 2a 2a 0a 2a 2a 20 20 4c 4f 43 4b 50 52 4f 58  .**.**  LOCKPROX
ec240 59 44 49 52 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  YDIR.**.**      
ec250 20 4f 76 65 72 72 69 64 65 73 20 74 68 65 20 64   Overrides the d
ec260 65 66 61 75 6c 74 20 64 69 72 65 63 74 6f 72 79  efault directory
ec270 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b 20 70   used for lock p
ec280 72 6f 78 79 20 66 69 6c 65 73 20 74 68 61 74 0a  roxy files that.
ec290 2a 2a 20 20 20 20 20 20 20 61 72 65 20 6e 61 6d  **       are nam
ec2a0 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ed automatically
ec2b0 20 76 69 61 20 74 68 65 20 22 3a 61 75 74 6f 3a   via the ":auto:
ec2c0 22 20 73 65 74 74 69 6e 67 0a 2a 2a 0a 2a 2a 20  " setting.**.** 
ec2d0 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
ec2e0 50 52 4f 58 59 44 49 52 5f 50 45 52 4d 49 53 53  PROXYDIR_PERMISS
ec2f0 49 4f 4e 53 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  IONS.**.**      
ec300 20 50 65 72 6d 69 73 73 69 6f 6e 73 20 74 6f 20   Permissions to 
ec310 75 73 65 20 77 68 65 6e 20 63 72 65 61 74 69 6e  use when creatin
ec320 67 20 61 20 64 69 72 65 63 74 6f 72 79 20 66 6f  g a directory fo
ec330 72 20 73 74 6f 72 69 6e 67 20 74 68 65 0a 2a 2a  r storing the.**
ec340 20 20 20 20 20 20 20 6c 6f 63 6b 20 70 72 6f 78         lock prox
ec350 79 20 66 69 6c 65 73 2c 20 6f 6e 6c 79 20 75 73  y files, only us
ec360 65 64 20 77 68 65 6e 20 4c 4f 43 4b 50 52 4f 58  ed when LOCKPROX
ec370 59 44 49 52 20 69 73 20 6e 6f 74 20 73 65 74 2e  YDIR is not set.
ec380 0a 2a 2a 20 20 20 20 0a 2a 2a 20 20 20 20 0a 2a  .**    .**    .*
ec390 2a 20 41 73 20 6d 65 6e 74 69 6f 6e 65 64 20 61  * As mentioned a
ec3a0 62 6f 76 65 2c 20 77 68 65 6e 20 63 6f 6d 70 69  bove, when compi
ec3b0 6c 65 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f  led with SQLITE_
ec3c0 50 52 45 46 45 52 5f 50 52 4f 58 59 5f 4c 4f 43  PREFER_PROXY_LOC
ec3d0 4b 49 4e 47 2c 0a 2a 2a 20 73 65 74 74 69 6e 67  KING,.** setting
ec3e0 20 74 68 65 20 65 6e 76 69 72 6f 6e 6d 65 6e 74   the environment
ec3f0 20 76 61 72 69 61 62 6c 65 20 53 51 4c 49 54 45   variable SQLITE
ec400 5f 46 4f 52 43 45 5f 50 52 4f 58 59 5f 4c 4f 43  _FORCE_PROXY_LOC
ec410 4b 49 4e 47 20 74 6f 20 31 20 77 69 6c 6c 0a 2a  KING to 1 will.*
ec420 2a 20 66 6f 72 63 65 20 70 72 6f 78 79 20 6c 6f  * force proxy lo
ec430 63 6b 69 6e 67 20 74 6f 20 62 65 20 75 73 65 64  cking to be used
ec440 20 66 6f 72 20 65 76 65 72 79 20 64 61 74 61 62   for every datab
ec450 61 73 65 20 66 69 6c 65 20 6f 70 65 6e 65 64 2c  ase file opened,
ec460 20 61 6e 64 20 30 0a 2a 2a 20 77 69 6c 6c 20 66   and 0.** will f
ec470 6f 72 63 65 20 61 75 74 6f 6d 61 74 69 63 20 70  orce automatic p
ec480 72 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 74 6f 20  roxy locking to 
ec490 62 65 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20  be disabled for 
ec4a0 61 6c 6c 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  all database.** 
ec4b0 66 69 6c 65 73 20 28 65 78 70 6c 69 63 69 74 79  files (explicity
ec4c0 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 53 51 4c   calling the SQL
ec4d0 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52 4f 58  ITE_SET_LOCKPROX
ec4e0 59 46 49 4c 45 20 70 72 61 67 6d 61 20 6f 72 0a  YFILE pragma or.
ec4f0 2a 2a 20 73 71 6c 69 74 65 5f 66 69 6c 65 5f 63  ** sqlite_file_c
ec500 6f 6e 74 72 6f 6c 20 41 50 49 20 69 73 20 6e 6f  ontrol API is no
ec510 74 20 61 66 66 65 63 74 65 64 20 62 79 20 53 51  t affected by SQ
ec520 4c 49 54 45 5f 46 4f 52 43 45 5f 50 52 4f 58 59  LITE_FORCE_PROXY
ec530 5f 4c 4f 43 4b 49 4e 47 29 2e 0a 2a 2f 0a 0a 2f  _LOCKING)..*/../
ec540 2a 0a 2a 2a 20 50 72 6f 78 79 20 6c 6f 63 6b 69  *.** Proxy locki
ec550 6e 67 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c  ng is only avail
ec560 61 62 6c 65 20 6f 6e 20 4d 61 63 4f 53 58 20 0a  able on MacOSX .
ec570 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f  */.#if defined(_
ec580 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c  _APPLE__) && SQL
ec590 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
ec5a0 4e 47 5f 53 54 59 4c 45 0a 0a 23 69 66 64 65 66  NG_STYLE..#ifdef
ec5b0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 20   SQLITE_TEST./* 
ec5c0 73 69 6d 75 6c 61 74 65 20 6d 75 6c 74 69 70 6c  simulate multipl
ec5d0 65 20 68 6f 73 74 73 20 62 79 20 63 72 65 61 74  e hosts by creat
ec5e0 69 6e 67 20 75 6e 69 71 75 65 20 68 6f 73 74 69  ing unique hosti
ec5f0 64 20 66 69 6c 65 20 70 61 74 68 73 20 2a 2f 0a  d file paths */.
ec600 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
ec610 71 6c 69 74 65 33 5f 68 6f 73 74 69 64 5f 6e 75  qlite3_hostid_nu
ec620 6d 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f  m = 0;.#endif../
ec630 2a 0a 2a 2a 20 54 68 65 20 70 72 6f 78 79 4c 6f  *.** The proxyLo
ec640 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 68 61 73  ckingContext has
ec650 20 74 68 65 20 70 61 74 68 20 61 6e 64 20 66 69   the path and fi
ec660 6c 65 20 73 74 72 75 63 74 75 72 65 73 20 66 6f  le structures fo
ec670 72 20 74 68 65 20 72 65 6d 6f 74 65 20 0a 2a 2a  r the remote .**
ec680 20 61 6e 64 20 6c 6f 63 61 6c 20 70 72 6f 78 79   and local proxy
ec690 20 66 69 6c 65 73 20 69 6e 20 69 74 0a 2a 2f 0a   files in it.*/.
ec6a0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 70  typedef struct p
ec6b0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
ec6c0 78 74 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43  xt proxyLockingC
ec6d0 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 20 70  ontext;.struct p
ec6e0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
ec6f0 78 74 20 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20  xt {.  unixFile 
ec700 2a 63 6f 6e 63 68 46 69 6c 65 3b 20 20 20 20 20  *conchFile;     
ec710 20 20 20 20 2f 2a 20 4f 70 65 6e 20 63 6f 6e 63      /* Open conc
ec720 68 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72  h file */.  char
ec730 20 2a 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 3b   *conchFilePath;
ec740 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
ec750 20 6f 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69   of the conch fi
ec760 6c 65 20 2a 2f 0a 20 20 75 6e 69 78 46 69 6c 65  le */.  unixFile
ec770 20 2a 6c 6f 63 6b 50 72 6f 78 79 3b 20 20 20 20   *lockProxy;    
ec780 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 72 6f       /* Open pro
ec790 78 79 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f 0a  xy lock file */.
ec7a0 20 20 63 68 61 72 20 2a 6c 6f 63 6b 50 72 6f 78    char *lockProx
ec7b0 79 50 61 74 68 3b 20 20 20 20 20 20 20 20 20 2f  yPath;         /
ec7c0 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 70 72  * Name of the pr
ec7d0 6f 78 79 20 6c 6f 63 6b 20 66 69 6c 65 20 2a 2f  oxy lock file */
ec7e0 0a 20 20 63 68 61 72 20 2a 64 62 50 61 74 68 3b  .  char *dbPath;
ec7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ec800 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6f  /* Name of the o
ec810 70 65 6e 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e  pen file */.  in
ec820 74 20 63 6f 6e 63 68 48 65 6c 64 3b 20 20 20 20  t conchHeld;    
ec830 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
ec840 75 65 20 69 66 20 74 68 65 20 63 6f 6e 63 68 20  ue if the conch 
ec850 69 73 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c  is currently hel
ec860 64 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 6f 6c 64  d */.  void *old
ec870 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 20  LockingContext; 
ec880 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20      /* Original 
ec890 6c 6f 63 6b 69 6e 67 63 6f 6e 74 65 78 74 20 74  lockingcontext t
ec8a0 6f 20 72 65 73 74 6f 72 65 20 6f 6e 20 63 6c 6f  o restore on clo
ec8b0 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  se */.  sqlite3_
ec8c0 69 6f 5f 6d 65 74 68 6f 64 73 20 63 6f 6e 73 74  io_methods const
ec8d0 20 2a 70 4f 6c 64 4d 65 74 68 6f 64 3b 20 20 20   *pOldMethod;   
ec8e0 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 49 2f    /* Original I/
ec8f0 4f 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 63 6c  O methods for cl
ec900 6f 73 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 48 4f  ose */.};../* HO
ec910 53 54 49 44 4c 45 4e 20 61 6e 64 20 43 4f 4e 43  STIDLEN and CONC
ec920 48 4c 45 4e 20 62 6f 74 68 20 69 6e 63 6c 75 64  HLEN both includ
ec930 65 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 20  e space for the 
ec940 73 74 72 69 6e 67 20 0a 2a 2a 20 74 65 72 6d 69  string .** termi
ec950 6e 61 74 69 6e 67 20 6e 75 6c 20 0a 2a 2f 0a 23  nating nul .*/.#
ec960 64 65 66 69 6e 65 20 48 4f 53 54 49 44 4c 45 4e  define HOSTIDLEN
ec970 20 20 20 20 20 20 20 20 20 31 32 38 0a 23 64 65           128.#de
ec980 66 69 6e 65 20 43 4f 4e 43 48 4c 45 4e 20 20 20  fine CONCHLEN   
ec990 20 20 20 20 20 20 20 28 4d 41 58 50 41 54 48 4c         (MAXPATHL
ec9a0 45 4e 2b 48 4f 53 54 49 44 4c 45 4e 2b 31 29 0a  EN+HOSTIDLEN+1).
ec9b0 23 69 66 6e 64 65 66 20 48 4f 53 54 49 44 50 41  #ifndef HOSTIDPA
ec9c0 54 48 0a 23 20 64 65 66 69 6e 65 20 48 4f 53 54  TH.# define HOST
ec9d0 49 44 50 41 54 48 20 20 20 20 20 20 20 22 2f 4c  IDPATH       "/L
ec9e0 69 62 72 61 72 79 2f 43 61 63 68 65 73 2f 2e 63  ibrary/Caches/.c
ec9f0 6f 6d 2e 61 70 70 6c 65 2e 73 71 6c 69 74 65 43  om.apple.sqliteC
eca00 6f 6e 63 68 48 6f 73 74 49 64 22 0a 23 65 6e 64  onchHostId".#end
eca10 69 66 0a 0a 2f 2a 20 62 61 73 69 63 61 6c 6c 79  if../* basically
eca20 20 61 20 63 6f 70 79 20 6f 66 20 75 6e 69 78 52   a copy of unixR
eca30 61 6e 64 6f 6d 6e 65 73 73 20 77 69 74 68 20 64  andomness with d
eca40 69 66 66 65 72 65 6e 74 0a 2a 2a 20 74 65 73 74  ifferent.** test
eca50 20 62 65 68 61 76 69 6f 72 20 62 75 69 6c 74 20   behavior built 
eca60 69 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  in */.static int
eca70 20 70 72 6f 78 79 47 65 6e 65 72 61 74 65 48 6f   proxyGenerateHo
eca80 73 74 49 44 28 63 68 61 72 20 2a 70 48 6f 73 74  stID(char *pHost
eca90 49 44 29 7b 0a 20 20 69 6e 74 20 70 69 64 2c 20  ID){.  int pid, 
ecaa0 66 64 2c 20 6c 65 6e 3b 0a 20 20 75 6e 73 69 67  fd, len;.  unsig
ecab0 6e 65 64 20 63 68 61 72 20 2a 6b 65 79 20 3d 20  ned char *key = 
ecac0 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
ecad0 29 70 48 6f 73 74 49 44 3b 0a 20 20 0a 20 20 6d  )pHostID;.  .  m
ecae0 65 6d 73 65 74 28 6b 65 79 2c 20 30 2c 20 48 4f  emset(key, 0, HO
ecaf0 53 54 49 44 4c 45 4e 29 3b 0a 20 20 6c 65 6e 20  STIDLEN);.  len 
ecb00 3d 20 30 3b 0a 20 20 66 64 20 3d 20 6f 70 65 6e  = 0;.  fd = open
ecb10 28 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c  ("/dev/urandom",
ecb20 20 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20 69 66   O_RDONLY);.  if
ecb30 28 20 66 64 3e 3d 30 20 29 7b 0a 20 20 20 20 6c  ( fd>=0 ){.    l
ecb40 65 6e 20 3d 20 72 65 61 64 28 66 64 2c 20 6b 65  en = read(fd, ke
ecb50 79 2c 20 48 4f 53 54 49 44 4c 45 4e 29 3b 0a 20  y, HOSTIDLEN);. 
ecb60 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20 2f 2a     close(fd); /*
ecb70 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b 20 74   silently leak t
ecb80 68 65 20 66 64 20 69 66 20 69 74 20 66 61 69 6c  he fd if it fail
ecb90 73 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 6c  s */.  }.  if( l
ecba0 65 6e 20 3c 20 48 4f 53 54 49 44 4c 45 4e 20 29  en < HOSTIDLEN )
ecbb0 7b 0a 20 20 20 20 74 69 6d 65 5f 74 20 74 3b 0a  {.    time_t t;.
ecbc0 20 20 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20      time(&t);.  
ecbd0 20 20 6d 65 6d 63 70 79 28 6b 65 79 2c 20 26 74    memcpy(key, &t
ecbe0 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b 0a 20 20  , sizeof(t));.  
ecbf0 20 20 70 69 64 20 3d 20 67 65 74 70 69 64 28 29    pid = getpid()
ecc00 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 6b 65  ;.    memcpy(&ke
ecc10 79 5b 73 69 7a 65 6f 66 28 74 29 5d 2c 20 26 70  y[sizeof(t)], &p
ecc20 69 64 2c 20 73 69 7a 65 6f 66 28 70 69 64 29 29  id, sizeof(pid))
ecc30 3b 0a 20 20 7d 0a 20 20 0a 23 69 66 64 65 66 20  ;.  }.  .#ifdef 
ecc40 4d 41 4b 45 5f 50 52 45 54 54 59 5f 48 4f 53 54  MAKE_PRETTY_HOST
ecc50 49 44 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69  ID.  {.    int i
ecc60 3b 0a 20 20 20 20 2f 2a 20 66 69 6c 74 65 72 20  ;.    /* filter 
ecc70 74 68 65 20 62 79 74 65 73 20 69 6e 74 6f 20 70  the bytes into p
ecc80 72 69 6e 74 61 62 6c 65 20 61 73 63 69 69 20 63  rintable ascii c
ecc90 68 61 72 61 63 74 65 72 73 20 61 6e 64 20 4e 55  haracters and NU
ecca0 4c 20 74 65 72 6d 69 6e 61 74 65 20 2a 2f 0a 20  L terminate */. 
eccb0 20 20 20 6b 65 79 5b 28 48 4f 53 54 49 44 4c 45     key[(HOSTIDLE
eccc0 4e 2d 31 29 5d 20 3d 20 30 78 30 30 3b 0a 20 20  N-1)] = 0x00;.  
eccd0 20 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c 28 48    for( i=0; i<(H
ecce0 4f 53 54 49 44 4c 45 4e 2d 31 29 3b 20 69 2b 2b  OSTIDLEN-1); i++
eccf0 20 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e   ){.      unsign
ecd00 65 64 20 63 68 61 72 20 70 61 20 3d 20 6b 65 79  ed char pa = key
ecd10 5b 69 5d 26 30 78 37 46 3b 0a 20 20 20 20 20 20  [i]&0x7F;.      
ecd20 69 66 28 20 70 61 3c 30 78 32 30 20 29 7b 0a 20  if( pa<0x20 ){. 
ecd30 20 20 20 20 20 20 20 6b 65 79 5b 69 5d 20 3d 20         key[i] = 
ecd40 28 6b 65 79 5b 69 5d 26 30 78 38 30 20 3d 3d 20  (key[i]&0x80 == 
ecd50 30 78 38 30 29 20 3f 20 70 61 2b 30 78 34 30 20  0x80) ? pa+0x40 
ecd60 3a 20 70 61 2b 30 78 32 30 3b 0a 20 20 20 20 20  : pa+0x20;.     
ecd70 20 7d 65 6c 73 65 20 69 66 28 20 70 61 3d 3d 30   }else if( pa==0
ecd80 78 37 46 20 29 7b 0a 20 20 20 20 20 20 20 20 6b  x7F ){.        k
ecd90 65 79 5b 69 5d 20 3d 20 28 6b 65 79 5b 69 5d 26  ey[i] = (key[i]&
ecda0 30 78 38 30 20 3d 3d 20 30 78 38 30 29 20 3f 20  0x80 == 0x80) ? 
ecdb0 70 61 3d 30 78 32 30 20 3a 20 70 61 2b 30 78 37  pa=0x20 : pa+0x7
ecdc0 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  E;.      }.    }
ecdd0 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65  .  }.#endif.  re
ecde0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
ecdf0 7d 0a 0a 2f 2a 20 77 72 69 74 65 73 20 74 68 65  }../* writes the
ece00 20 68 6f 73 74 20 69 64 20 70 61 74 68 20 74 6f   host id path to
ece10 20 70 61 74 68 2c 20 70 61 74 68 20 73 68 6f 75   path, path shou
ece20 6c 64 20 62 65 20 61 6e 20 70 72 65 2d 61 6c 6c  ld be an pre-all
ece30 6f 63 61 74 65 64 20 62 75 66 66 65 72 0a 2a 2a  ocated buffer.**
ece40 20 77 69 74 68 20 65 6e 6f 75 67 68 20 73 70 61   with enough spa
ece50 63 65 20 66 6f 72 20 61 20 70 61 74 68 20 0a 2a  ce for a path .*
ece60 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 72  /.static void pr
ece70 6f 78 79 47 65 74 48 6f 73 74 49 44 50 61 74 68  oxyGetHostIDPath
ece80 28 63 68 61 72 20 2a 70 61 74 68 2c 20 73 69 7a  (char *path, siz
ece90 65 5f 74 20 6c 65 6e 29 7b 0a 20 20 73 74 72 6c  e_t len){.  strl
ecea0 63 70 79 28 70 61 74 68 2c 20 48 4f 53 54 49 44  cpy(path, HOSTID
eceb0 50 41 54 48 2c 20 6c 65 6e 29 3b 0a 23 69 66 64  PATH, len);.#ifd
ecec0 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
eced0 20 69 66 28 20 73 71 6c 69 74 65 33 5f 68 6f 73   if( sqlite3_hos
ecee0 74 69 64 5f 6e 75 6d 3e 30 20 29 7b 0a 20 20 20  tid_num>0 ){.   
ecef0 20 63 68 61 72 20 73 75 66 66 69 78 5b 32 5d 20   char suffix[2] 
ecf00 3d 20 22 31 22 3b 0a 20 20 20 20 73 75 66 66 69  = "1";.    suffi
ecf10 78 5b 30 5d 20 3d 20 73 75 66 66 69 78 5b 30 5d  x[0] = suffix[0]
ecf20 20 2b 20 73 71 6c 69 74 65 33 5f 68 6f 73 74 69   + sqlite3_hosti
ecf30 64 5f 6e 75 6d 3b 0a 20 20 20 20 73 74 72 6c 63  d_num;.    strlc
ecf40 61 74 28 70 61 74 68 2c 20 73 75 66 66 69 78 2c  at(path, suffix,
ecf50 20 6c 65 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69   len);.  }.#endi
ecf60 66 0a 20 20 4f 53 54 52 41 43 45 33 28 22 47 45  f.  OSTRACE3("GE
ecf70 54 48 4f 53 54 49 44 50 41 54 48 20 20 25 73 20  THOSTIDPATH  %s 
ecf80 70 69 64 3d 25 64 5c 6e 22 2c 20 70 61 74 68 2c  pid=%d\n", path,
ecf90 20 67 65 74 70 69 64 28 29 29 3b 0a 7d 0a 0a 2f   getpid());.}../
ecfa0 2a 20 67 65 74 20 74 68 65 20 68 6f 73 74 20 49  * get the host I
ecfb0 44 20 66 72 6f 6d 20 61 20 73 71 6c 69 74 65 20  D from a sqlite 
ecfc0 68 6f 73 74 69 64 20 66 69 6c 65 20 73 74 6f 72  hostid file stor
ecfd0 65 64 20 69 6e 20 74 68 65 20 0a 2a 2a 20 75 73  ed in the .** us
ecfe0 65 72 2d 73 70 65 63 69 66 69 63 20 74 6d 70 20  er-specific tmp 
ecff0 64 69 72 65 63 74 6f 72 79 2c 20 63 72 65 61 74  directory, creat
ed000 65 20 74 68 65 20 49 44 20 69 66 20 69 74 27 73  e the ID if it's
ed010 20 6e 6f 74 20 74 68 65 72 65 20 61 6c 72 65 61   not there alrea
ed020 64 79 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  dy .*/.static in
ed030 74 20 70 72 6f 78 79 47 65 74 48 6f 73 74 49 44  t proxyGetHostID
ed040 28 63 68 61 72 20 2a 70 48 6f 73 74 49 44 2c 20  (char *pHostID, 
ed050 69 6e 74 20 2a 70 45 72 72 6f 72 29 7b 0a 20 20  int *pError){.  
ed060 69 6e 74 20 66 64 3b 0a 20 20 63 68 61 72 20 70  int fd;.  char p
ed070 61 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b  ath[MAXPATHLEN];
ed080 20 0a 20 20 73 69 7a 65 5f 74 20 6c 65 6e 3b 0a   .  size_t len;.
ed090 20 20 69 6e 74 20 72 63 3d 53 51 4c 49 54 45 5f    int rc=SQLITE_
ed0a0 4f 4b 3b 0a 0a 20 20 70 72 6f 78 79 47 65 74 48  OK;..  proxyGetH
ed0b0 6f 73 74 49 44 50 61 74 68 28 70 61 74 68 2c 20  ostIDPath(path, 
ed0c0 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 2f  MAXPATHLEN);.  /
ed0d0 2a 20 74 72 79 20 74 6f 20 63 72 65 61 74 65 20  * try to create 
ed0e0 74 68 65 20 68 6f 73 74 20 49 44 20 66 69 6c 65  the host ID file
ed0f0 2c 20 69 66 20 69 74 20 61 6c 72 65 61 64 79 20  , if it already 
ed100 65 78 69 73 74 73 20 72 65 61 64 20 74 68 65 20  exists read the 
ed110 63 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20 66 64  contents */.  fd
ed120 20 3d 20 6f 70 65 6e 28 70 61 74 68 2c 20 4f 5f   = open(path, O_
ed130 43 52 45 41 54 7c 4f 5f 57 52 4f 4e 4c 59 7c 4f  CREAT|O_WRONLY|O
ed140 5f 45 58 43 4c 2c 20 30 36 34 34 29 3b 0a 20 20  _EXCL, 0644);.  
ed150 69 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20  if( fd<0 ){.    
ed160 69 6e 74 20 65 72 72 3d 65 72 72 6e 6f 3b 0a 09  int err=errno;..
ed170 09 0a 20 20 20 20 69 66 28 20 65 72 72 21 3d 45  ..    if( err!=E
ed180 45 58 49 53 54 20 29 7b 0a 23 69 66 64 65 66 20  EXIST ){.#ifdef 
ed190 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42  SQLITE_PROXY_DEB
ed1a0 55 47 20 2f 2a 20 73 65 74 20 74 68 65 20 73 71  UG /* set the sq
ed1b0 6c 69 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61  lite error messa
ed1c0 67 65 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20  ge instead */.  
ed1d0 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 65      fprintf(stde
ed1e0 72 72 2c 20 22 73 71 6c 69 74 65 20 65 72 72 6f  rr, "sqlite erro
ed1f0 72 20 63 72 65 61 74 69 6e 67 20 68 6f 73 74 20  r creating host 
ed200 49 44 20 66 69 6c 65 20 25 73 3a 20 25 73 5c 6e  ID file %s: %s\n
ed210 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
ed220 20 70 61 74 68 2c 20 73 74 72 65 72 72 6f 72 28   path, strerror(
ed230 65 72 72 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20  err));.#endif.  
ed240 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
ed250 45 5f 50 45 52 4d 3b 0a 20 20 20 20 7d 0a 20 20  E_PERM;.    }.  
ed260 20 20 2f 2a 20 63 6f 75 6c 64 6e 27 74 20 63 72    /* couldn't cr
ed270 65 61 74 65 20 74 68 65 20 66 69 6c 65 2c 20 72  eate the file, r
ed280 65 61 64 20 69 74 20 69 6e 73 74 65 61 64 20 2a  ead it instead *
ed290 2f 0a 20 20 20 20 66 64 20 3d 20 6f 70 65 6e 28  /.    fd = open(
ed2a0 70 61 74 68 2c 20 4f 5f 52 44 4f 4e 4c 59 7c 4f  path, O_RDONLY|O
ed2b0 5f 45 58 43 4c 29 3b 0a 20 20 20 20 69 66 28 20  _EXCL);.    if( 
ed2c0 66 64 3c 30 20 29 7b 0a 23 69 66 64 65 66 20 53  fd<0 ){.#ifdef S
ed2d0 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45 42 55  QLITE_PROXY_DEBU
ed2e0 47 20 2f 2a 20 73 65 74 20 74 68 65 20 73 71 6c  G /* set the sql
ed2f0 69 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  ite error messag
ed300 65 20 69 6e 73 74 65 61 64 20 2a 2f 0a 20 20 20  e instead */.   
ed310 20 20 20 69 6e 74 20 65 72 72 20 3d 20 65 72 72     int err = err
ed320 6e 6f 3b 0a 20 20 20 20 20 20 66 70 72 69 6e 74  no;.      fprint
ed330 66 28 73 74 64 65 72 72 2c 20 22 73 71 6c 69 74  f(stderr, "sqlit
ed340 65 20 65 72 72 6f 72 20 6f 70 65 6e 69 6e 67 20  e error opening 
ed350 68 6f 73 74 20 49 44 20 66 69 6c 65 20 25 73 3a  host ID file %s:
ed360 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20   %s\n",.        
ed370 20 20 20 20 20 20 70 61 74 68 2c 20 73 74 72 65        path, stre
ed380 72 72 6f 72 28 65 72 72 29 29 3b 0a 23 65 6e 64  rror(err));.#end
ed390 69 66 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  if.      return 
ed3a0 53 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 20 20  SQLITE_PERM;.   
ed3b0 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20 70 72 65   }.    len = pre
ed3c0 61 64 28 66 64 2c 20 70 48 6f 73 74 49 44 2c 20  ad(fd, pHostID, 
ed3d0 48 4f 53 54 49 44 4c 45 4e 2c 20 30 29 3b 0a 20  HOSTIDLEN, 0);. 
ed3e0 20 20 20 69 66 28 20 6c 65 6e 3c 30 20 29 7b 0a     if( len<0 ){.
ed3f0 20 20 20 20 20 20 2a 70 45 72 72 6f 72 20 3d 20        *pError = 
ed400 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20  errno;.      rc 
ed410 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52  = SQLITE_IOERR_R
ed420 45 41 44 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  EAD;.    }else i
ed430 66 28 20 6c 65 6e 3c 48 4f 53 54 49 44 4c 45 4e  f( len<HOSTIDLEN
ed440 20 29 7b 0a 20 20 20 20 20 20 2a 70 45 72 72 6f   ){.      *pErro
ed450 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20  r = 0;.      rc 
ed460 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53  = SQLITE_IOERR_S
ed470 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20 20 20 7d  HORT_READ;.    }
ed480 0a 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b 20  .    close(fd); 
ed490 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65 61 6b  /* silently leak
ed4a0 20 74 68 65 20 66 64 20 69 66 20 69 74 20 66 61   the fd if it fa
ed4b0 69 6c 73 20 2a 2f 0a 20 20 20 20 4f 53 54 52 41  ils */.    OSTRA
ed4c0 43 45 33 28 22 47 45 54 48 4f 53 54 49 44 20 20  CE3("GETHOSTID  
ed4d0 72 65 61 64 20 25 73 20 70 69 64 3d 25 64 5c 6e  read %s pid=%d\n
ed4e0 22 2c 20 70 48 6f 73 74 49 44 2c 20 67 65 74 70  ", pHostID, getp
ed4f0 69 64 28 29 29 3b 0a 20 20 20 20 72 65 74 75 72  id());.    retur
ed500 6e 20 72 63 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  n rc;.  }else{. 
ed510 20 20 20 2f 2a 20 77 65 27 72 65 20 63 72 65 61     /* we're crea
ed520 74 69 6e 67 20 74 68 65 20 68 6f 73 74 20 49 44  ting the host ID
ed530 20 66 69 6c 65 20 28 75 73 65 20 61 20 72 61 6e   file (use a ran
ed540 64 6f 6d 20 73 74 72 69 6e 67 20 6f 66 20 62 79  dom string of by
ed550 74 65 73 29 20 2a 2f 0a 20 20 20 20 70 72 6f 78  tes) */.    prox
ed560 79 47 65 6e 65 72 61 74 65 48 6f 73 74 49 44 28  yGenerateHostID(
ed570 70 48 6f 73 74 49 44 29 3b 0a 20 20 20 20 6c 65  pHostID);.    le
ed580 6e 20 3d 20 70 77 72 69 74 65 28 66 64 2c 20 70  n = pwrite(fd, p
ed590 48 6f 73 74 49 44 2c 20 48 4f 53 54 49 44 4c 45  HostID, HOSTIDLE
ed5a0 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 6c  N, 0);.    if( l
ed5b0 65 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 2a 70  en<0 ){.      *p
ed5c0 45 72 72 6f 72 20 3d 20 65 72 72 6e 6f 3b 0a 20  Error = errno;. 
ed5d0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
ed5e0 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20 20  _IOERR_WRITE;.  
ed5f0 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 65 6e 3c    }else if( len<
ed600 48 4f 53 54 49 44 4c 45 4e 20 29 7b 0a 20 20 20  HOSTIDLEN ){.   
ed610 20 20 20 2a 70 45 72 72 6f 72 20 3d 20 30 3b 0a     *pError = 0;.
ed620 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
ed630 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 3b 0a 20  E_IOERR_WRITE;. 
ed640 20 20 20 7d 0a 20 20 20 20 63 6c 6f 73 65 28 66     }.    close(f
ed650 64 29 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20  d); /* silently 
ed660 6c 65 61 6b 20 74 68 65 20 66 64 20 69 66 20 69  leak the fd if i
ed670 74 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 4f  t fails */.    O
ed680 53 54 52 41 43 45 33 28 22 47 45 54 48 4f 53 54  STRACE3("GETHOST
ed690 49 44 20 20 77 72 6f 74 65 20 25 73 20 70 69 64  ID  wrote %s pid
ed6a0 3d 25 64 5c 6e 22 2c 20 70 48 6f 73 74 49 44 2c  =%d\n", pHostID,
ed6b0 20 67 65 74 70 69 64 28 29 29 3b 0a 20 20 20 20   getpid());.    
ed6c0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d  return rc;.  }.}
ed6d0 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  ..static int pro
ed6e0 78 79 47 65 74 4c 6f 63 6b 50 61 74 68 28 63 6f  xyGetLockPath(co
ed6f0 6e 73 74 20 63 68 61 72 20 2a 64 62 50 61 74 68  nst char *dbPath
ed700 2c 20 63 68 61 72 20 2a 6c 50 61 74 68 2c 20 73  , char *lPath, s
ed710 69 7a 65 5f 74 20 6d 61 78 4c 65 6e 29 7b 0a 20  ize_t maxLen){. 
ed720 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e 74 20   int len;.  int 
ed730 64 62 4c 65 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a  dbLen;.  int i;.
ed740 0a 23 69 66 64 65 66 20 4c 4f 43 4b 50 52 4f 58  .#ifdef LOCKPROX
ed750 59 44 49 52 0a 20 20 6c 65 6e 20 3d 20 73 74 72  YDIR.  len = str
ed760 6c 63 70 79 28 6c 50 61 74 68 2c 20 4c 4f 43 4b  lcpy(lPath, LOCK
ed770 50 52 4f 58 59 44 49 52 2c 20 6d 61 78 4c 65 6e  PROXYDIR, maxLen
ed780 29 3b 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66  );.#else.# ifdef
ed790 20 5f 43 53 5f 44 41 52 57 49 4e 5f 55 53 45 52   _CS_DARWIN_USER
ed7a0 5f 54 45 4d 50 5f 44 49 52 0a 20 20 7b 0a 20 20  _TEMP_DIR.  {.  
ed7b0 20 20 63 6f 6e 66 73 74 72 28 5f 43 53 5f 44 41    confstr(_CS_DA
ed7c0 52 57 49 4e 5f 55 53 45 52 5f 54 45 4d 50 5f 44  RWIN_USER_TEMP_D
ed7d0 49 52 2c 20 6c 50 61 74 68 2c 20 6d 61 78 4c 65  IR, lPath, maxLe
ed7e0 6e 29 3b 0a 20 20 20 20 6c 65 6e 20 3d 20 73 74  n);.    len = st
ed7f0 72 6c 63 61 74 28 6c 50 61 74 68 2c 20 22 73 71  rlcat(lPath, "sq
ed800 6c 69 74 65 70 6c 6f 63 6b 73 22 2c 20 6d 61 78  liteplocks", max
ed810 4c 65 6e 29 3b 0a 20 20 20 20 69 66 28 20 6d 6b  Len);.    if( mk
ed820 64 69 72 28 6c 50 61 74 68 2c 20 53 51 4c 49 54  dir(lPath, SQLIT
ed830 45 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 59 44  E_DEFAULT_PROXYD
ed840 49 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29 20  IR_PERMISSIONS) 
ed850 29 7b 0a 20 20 20 20 20 20 2f 2a 20 69 66 20 6d  ){.      /* if m
ed860 6b 64 69 72 20 66 61 69 6c 73 2c 20 68 61 6e 64  kdir fails, hand
ed870 6c 65 20 61 73 20 6c 6f 63 6b 20 66 69 6c 65 20  le as lock file 
ed880 63 72 65 61 74 69 6f 6e 20 66 61 69 6c 75 72 65  creation failure
ed890 20 2a 2f 0a 23 20 20 69 66 64 65 66 20 53 51 4c   */.#  ifdef SQL
ed8a0 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20  ITE_DEBUG.      
ed8b0 69 6e 74 20 65 72 72 20 3d 20 65 72 72 6e 6f 3b  int err = errno;
ed8c0 0a 20 20 20 20 20 20 69 66 28 20 65 72 72 21 3d  .      if( err!=
ed8d0 45 45 58 49 53 54 20 29 7b 0a 20 20 20 20 20 20  EEXIST ){.      
ed8e0 20 20 66 70 72 69 6e 74 66 28 73 74 64 65 72 72    fprintf(stderr
ed8f0 2c 20 22 70 72 6f 78 79 47 65 74 4c 6f 63 6b 50  , "proxyGetLockP
ed900 61 74 68 3a 20 6d 6b 64 69 72 28 25 73 2c 30 25  ath: mkdir(%s,0%
ed910 6f 29 20 65 72 72 6f 72 20 25 64 20 25 73 5c 6e  o) error %d %s\n
ed920 22 2c 20 6c 50 61 74 68 2c 0a 20 20 20 20 20 20  ", lPath,.      
ed930 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
ed940 5f 44 45 46 41 55 4c 54 5f 50 52 4f 58 59 44 49  _DEFAULT_PROXYDI
ed950 52 5f 50 45 52 4d 49 53 53 49 4f 4e 53 2c 20 65  R_PERMISSIONS, e
ed960 72 72 2c 20 73 74 72 65 72 72 6f 72 28 65 72 72  rr, strerror(err
ed970 29 29 3b 0a 20 20 20 20 20 20 7d 0a 23 20 20 65  ));.      }.#  e
ed980 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ndif.    }else{.
ed990 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 22        OSTRACE3("
ed9a0 47 45 54 4c 4f 43 4b 50 41 54 48 20 20 6d 6b 64  GETLOCKPATH  mkd
ed9b0 69 72 20 25 73 20 70 69 64 3d 25 64 5c 6e 22 2c  ir %s pid=%d\n",
ed9c0 20 6c 50 61 74 68 2c 20 67 65 74 70 69 64 28 29   lPath, getpid()
ed9d0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20 20  );.    }.    .  
ed9e0 7d 0a 23 20 65 6c 73 65 0a 20 20 6c 65 6e 20 3d  }.# else.  len =
ed9f0 20 73 74 72 6c 63 70 79 28 6c 50 61 74 68 2c 20   strlcpy(lPath, 
eda00 22 2f 74 6d 70 2f 22 2c 20 6d 61 78 4c 65 6e 29  "/tmp/", maxLen)
eda10 3b 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66  ;.# endif.#endif
eda20 0a 0a 20 20 69 66 28 20 6c 50 61 74 68 5b 6c 65  ..  if( lPath[le
eda30 6e 2d 31 5d 21 3d 27 2f 27 20 29 7b 0a 20 20 20  n-1]!='/' ){.   
eda40 20 6c 65 6e 20 3d 20 73 74 72 6c 63 61 74 28 6c   len = strlcat(l
eda50 50 61 74 68 2c 20 22 2f 22 2c 20 6d 61 78 4c 65  Path, "/", maxLe
eda60 6e 29 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20  n);.  }.  .  /* 
eda70 74 72 61 6e 73 66 6f 72 6d 20 74 68 65 20 64 62  transform the db
eda80 20 70 61 74 68 20 74 6f 20 61 20 75 6e 69 71 75   path to a uniqu
eda90 65 20 63 61 63 68 65 20 6e 61 6d 65 20 2a 2f 0a  e cache name */.
edaa0 20 20 64 62 4c 65 6e 20 3d 20 28 69 6e 74 29 73    dbLen = (int)s
edab0 74 72 6c 65 6e 28 64 62 50 61 74 68 29 3b 0a 20  trlen(dbPath);. 
edac0 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c 64 62 4c   for( i=0; i<dbL
edad0 65 6e 20 26 26 20 28 69 2b 6c 65 6e 2b 37 29 3c  en && (i+len+7)<
edae0 6d 61 78 4c 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20  maxLen; i++){.  
edaf0 20 20 63 68 61 72 20 63 20 3d 20 64 62 50 61 74    char c = dbPat
edb00 68 5b 69 5d 3b 0a 20 20 20 20 6c 50 61 74 68 5b  h[i];.    lPath[
edb10 69 2b 6c 65 6e 5d 20 3d 20 28 63 3d 3d 27 2f 27  i+len] = (c=='/'
edb20 29 3f 27 5f 27 3a 63 3b 0a 20 20 7d 0a 20 20 6c  )?'_':c;.  }.  l
edb30 50 61 74 68 5b 69 2b 6c 65 6e 5d 3d 27 5c 30 27  Path[i+len]='\0'
edb40 3b 0a 20 20 73 74 72 6c 63 61 74 28 6c 50 61 74  ;.  strlcat(lPat
edb50 68 2c 20 22 3a 61 75 74 6f 3a 22 2c 20 6d 61 78  h, ":auto:", max
edb60 4c 65 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  Len);.  return S
edb70 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
edb80 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
edb90 56 46 53 20 66 69 6c 65 20 64 65 73 63 72 69 70  VFS file descrip
edba0 74 6f 72 20 28 73 74 6f 72 65 64 20 69 6e 20 6d  tor (stored in m
edbb0 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
edbc0 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d  rom.** sqlite3_m
edbd0 61 6c 6c 6f 63 29 20 61 6e 64 20 6f 70 65 6e 20  alloc) and open 
edbe0 74 68 65 20 66 69 6c 65 20 6e 61 6d 65 64 20 22  the file named "
edbf0 70 61 74 68 22 20 69 6e 20 74 68 65 20 66 69 6c  path" in the fil
edc00 65 20 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a 2a  e descriptor..**
edc10 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 69  .** The caller i
edc20 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 6e 6f  s responsible no
edc30 74 20 6f 6e 6c 79 20 66 6f 72 20 63 6c 6f 73 69  t only for closi
edc40 6e 67 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  ng the file desc
edc50 72 69 70 74 6f 72 0a 2a 2a 20 62 75 74 20 61 6c  riptor.** but al
edc60 73 6f 20 66 6f 72 20 66 72 65 65 69 6e 67 20 74  so for freeing t
edc70 68 65 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69  he memory associ
edc80 61 74 65 64 20 77 69 74 68 20 74 68 65 20 66 69  ated with the fi
edc90 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 0a 2a  le descriptor..*
edca0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  /.static int pro
edcb0 78 79 43 72 65 61 74 65 55 6e 69 78 46 69 6c 65  xyCreateUnixFile
edcc0 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74  (const char *pat
edcd0 68 2c 20 75 6e 69 78 46 69 6c 65 20 2a 2a 70 70  h, unixFile **pp
edce0 46 69 6c 65 29 20 7b 0a 20 20 75 6e 69 78 46 69  File) {.  unixFi
edcf0 6c 65 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20  le *pNew;.  int 
edd00 66 6c 61 67 73 20 3d 20 53 51 4c 49 54 45 5f 4f  flags = SQLITE_O
edd10 50 45 4e 5f 4d 41 49 4e 5f 44 42 7c 53 51 4c 49  PEN_MAIN_DB|SQLI
edd20 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53  TE_OPEN_CREATE|S
edd30 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
edd40 52 49 54 45 3b 0a 20 20 69 6e 74 20 72 63 20 3d  RITE;.  int rc =
edd50 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 71   SQLITE_OK;.  sq
edd60 6c 69 74 65 33 5f 76 66 73 20 64 75 6d 6d 79 56  lite3_vfs dummyV
edd70 66 73 3b 0a 0a 20 20 70 4e 65 77 20 3d 20 28 75  fs;..  pNew = (u
edd80 6e 69 78 46 69 6c 65 20 2a 29 73 71 6c 69 74 65  nixFile *)sqlite
edd90 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
edda0 75 6e 69 78 46 69 6c 65 29 29 3b 0a 20 20 69 66  unixFile));.  if
eddb0 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 72  ( !pNew ){.    r
eddc0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
eddd0 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74  EM;.  }.  memset
edde0 28 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66  (pNew, 0, sizeof
eddf0 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20 20  (unixFile));..  
ede00 2f 2a 20 43 61 6c 6c 20 75 6e 69 78 4f 70 65 6e  /* Call unixOpen
ede10 28 29 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 70  () to open the p
ede20 72 6f 78 79 20 66 69 6c 65 2e 20 54 68 65 20 66  roxy file. The f
ede30 6c 61 67 73 20 70 61 73 73 65 64 20 74 6f 20 75  lags passed to u
ede40 6e 69 78 4f 70 65 6e 28 29 0a 20 20 2a 2a 20 73  nixOpen().  ** s
ede50 75 67 67 65 73 74 20 74 68 61 74 20 74 68 65 20  uggest that the 
ede60 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65  file being opene
ede70 64 20 69 73 20 61 20 22 6d 61 69 6e 20 64 61 74  d is a "main dat
ede80 61 62 61 73 65 22 2e 20 54 68 69 73 20 69 73 0a  abase". This is.
ede90 20 20 2a 2a 20 6e 65 63 65 73 73 61 72 79 20 61    ** necessary a
edea0 73 20 6f 74 68 65 72 20 66 69 6c 65 20 74 79 70  s other file typ
edeb0 65 73 20 64 6f 20 6e 6f 74 20 6e 65 63 65 73 73  es do not necess
edec0 61 72 69 6c 79 20 73 75 70 70 6f 72 74 20 6c 6f  arily support lo
eded0 63 6b 69 6e 67 2e 20 49 74 0a 20 20 2a 2a 20 69  cking. It.  ** i
edee0 73 20 62 65 74 74 65 72 20 74 6f 20 75 73 65 20  s better to use 
edef0 75 6e 69 78 4f 70 65 6e 28 29 20 69 6e 73 74 65  unixOpen() inste
edf00 61 64 20 6f 66 20 6f 70 65 6e 69 6e 67 20 74 68  ad of opening th
edf10 65 20 66 69 6c 65 20 64 69 72 65 63 74 6c 79 20  e file directly 
edf20 77 69 74 68 0a 20 20 2a 2a 20 6f 70 65 6e 28 29  with.  ** open()
edf30 2c 20 61 73 20 75 6e 69 78 4f 70 65 6e 28 29 20  , as unixOpen() 
edf40 73 65 74 73 20 75 70 20 74 68 65 20 76 61 72 69  sets up the vari
edf50 6f 75 73 20 6d 65 63 68 61 6e 69 73 6d 73 20 72  ous mechanisms r
edf60 65 71 75 69 72 65 64 20 74 6f 0a 20 20 2a 2a 20  equired to.  ** 
edf70 6d 61 6b 65 20 73 75 72 65 20 61 20 63 61 6c 6c  make sure a call
edf80 20 74 6f 20 63 6c 6f 73 65 28 29 20 64 6f 65 73   to close() does
edf90 20 6e 6f 74 20 63 61 75 73 65 20 74 68 65 20 73   not cause the s
edfa0 79 73 74 65 6d 20 74 6f 20 64 69 73 63 61 72 64  ystem to discard
edfb0 0a 20 20 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 6b  .  ** POSIX lock
edfc0 73 20 70 72 65 6d 61 74 75 72 65 6c 79 2e 0a 20  s prematurely.. 
edfd0 20 2a 2a 0a 20 20 2a 2a 20 49 74 20 69 73 20 69   **.  ** It is i
edfe0 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 74 68  mportant that th
edff0 65 20 78 4f 70 65 6e 20 6d 65 6d 62 65 72 20 6f  e xOpen member o
ee000 66 20 74 68 65 20 56 46 53 20 6f 62 6a 65 63 74  f the VFS object
ee010 20 70 61 73 73 65 64 20 74 6f 20 0a 20 20 2a 2a   passed to .  **
ee020 20 75 6e 69 78 4f 70 65 6e 28 29 20 69 73 20 4e   unixOpen() is N
ee030 55 4c 4c 2e 20 54 68 69 73 20 74 65 6c 6c 73 20  ULL. This tells 
ee040 75 6e 69 78 4f 70 65 6e 28 29 20 6d 61 79 20 74  unixOpen() may t
ee050 72 79 20 74 6f 20 6f 70 65 6e 20 61 20 70 72 6f  ry to open a pro
ee060 78 79 2d 66 69 6c 65 20 0a 20 20 2a 2a 20 66 6f  xy-file .  ** fo
ee070 72 20 74 68 65 20 70 72 6f 78 79 2d 66 69 6c 65  r the proxy-file
ee080 20 28 63 72 65 61 74 69 6e 67 20 61 20 70 6f 74   (creating a pot
ee090 65 6e 74 69 61 6c 20 69 6e 66 69 6e 69 74 65 20  ential infinite 
ee0a0 6c 6f 6f 70 29 2e 0a 20 20 2a 2f 0a 20 20 64 75  loop)..  */.  du
ee0b0 6d 6d 79 56 66 73 2e 70 41 70 70 44 61 74 61 20  mmyVfs.pAppData 
ee0c0 3d 20 28 76 6f 69 64 2a 29 26 61 75 74 6f 6c 6f  = (void*)&autolo
ee0d0 63 6b 49 6f 46 69 6e 64 65 72 3b 0a 20 20 64 75  ckIoFinder;.  du
ee0e0 6d 6d 79 56 66 73 2e 78 4f 70 65 6e 20 3d 20 30  mmyVfs.xOpen = 0
ee0f0 3b 0a 20 20 72 63 20 3d 20 75 6e 69 78 4f 70 65  ;.  rc = unixOpe
ee100 6e 28 26 64 75 6d 6d 79 56 66 73 2c 20 70 61 74  n(&dummyVfs, pat
ee110 68 2c 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  h, (sqlite3_file
ee120 20 2a 29 70 4e 65 77 2c 20 66 6c 61 67 73 2c 20   *)pNew, flags, 
ee130 26 66 6c 61 67 73 29 3b 0a 20 20 69 66 28 20 72  &flags);.  if( r
ee140 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
ee150 28 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 4f 50  (flags&SQLITE_OP
ee160 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 20 29 7b 0a  EN_READONLY) ){.
ee170 20 20 20 20 70 4e 65 77 2d 3e 70 4d 65 74 68 6f      pNew->pMetho
ee180 64 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 74  d->xClose((sqlit
ee190 65 33 5f 66 69 6c 65 20 2a 29 70 4e 65 77 29 3b  e3_file *)pNew);
ee1a0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
ee1b0 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 0a  _CANTOPEN;.  }..
ee1c0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
ee1d0 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
ee1e0 65 33 5f 66 72 65 65 28 70 4e 65 77 29 3b 0a 20  e3_free(pNew);. 
ee1f0 20 20 20 70 4e 65 77 20 3d 20 30 3b 0a 20 20 7d     pNew = 0;.  }
ee200 0a 0a 20 20 2a 70 70 46 69 6c 65 20 3d 20 70 4e  ..  *ppFile = pN
ee210 65 77 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ew;.  return rc;
ee220 0a 7d 0a 0a 2f 2a 20 74 61 6b 65 73 20 74 68 65  .}../* takes the
ee230 20 63 6f 6e 63 68 20 62 79 20 74 61 6b 69 6e 67   conch by taking
ee240 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 61   a shared lock a
ee250 6e 64 20 72 65 61 64 20 74 68 65 20 63 6f 6e 74  nd read the cont
ee260 65 6e 74 73 20 63 6f 6e 63 68 2c 20 69 66 20 0a  ents conch, if .
ee270 2a 2a 20 6c 6f 63 6b 50 61 74 68 20 69 73 20 6e  ** lockPath is n
ee280 6f 6e 2d 4e 55 4c 4c 2c 20 74 68 65 20 68 6f 73  on-NULL, the hos
ee290 74 20 49 44 20 61 6e 64 20 6c 6f 63 6b 20 66 69  t ID and lock fi
ee2a0 6c 65 20 70 61 74 68 20 6d 75 73 74 20 6d 61 74  le path must mat
ee2b0 63 68 2e 20 20 41 20 4e 55 4c 4c 20 0a 2a 2a 20  ch.  A NULL .** 
ee2c0 6c 6f 63 6b 50 61 74 68 20 6d 65 61 6e 73 20 74  lockPath means t
ee2d0 68 61 74 20 74 68 65 20 6c 6f 63 6b 50 61 74 68  hat the lockPath
ee2e0 20 69 6e 20 74 68 65 20 63 6f 6e 63 68 20 66 69   in the conch fi
ee2f0 6c 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20  le will be used 
ee300 69 66 20 74 68 65 20 0a 2a 2a 20 68 6f 73 74 20  if the .** host 
ee310 49 44 73 20 6d 61 74 63 68 2c 20 6f 72 20 61 20  IDs match, or a 
ee320 6e 65 77 20 6c 6f 63 6b 20 70 61 74 68 20 77 69  new lock path wi
ee330 6c 6c 20 62 65 20 67 65 6e 65 72 61 74 65 64 20  ll be generated 
ee340 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a  automatically .*
ee350 2a 20 61 6e 64 20 77 72 69 74 74 65 6e 20 74 6f  * and written to
ee360 20 74 68 65 20 63 6f 6e 63 68 20 66 69 6c 65 2e   the conch file.
ee370 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
ee380 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 75 6e  roxyTakeConch(un
ee390 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a  ixFile *pFile){.
ee3a0 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f    proxyLockingCo
ee3b0 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70  ntext *pCtx = (p
ee3c0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
ee3d0 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b  xt *)pFile->lock
ee3e0 69 6e 67 43 6f 6e 74 65 78 74 3b 20 0a 20 20 0a  ingContext; .  .
ee3f0 20 20 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e 63    if( pCtx->conc
ee400 68 48 65 6c 64 3e 30 20 29 7b 0a 20 20 20 20 72  hHeld>0 ){.    r
ee410 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
ee420 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e  .  }else{.    un
ee430 69 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c  ixFile *conchFil
ee440 65 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63 68 46  e = pCtx->conchF
ee450 69 6c 65 3b 0a 20 20 20 20 63 68 61 72 20 74 65  ile;.    char te
ee460 73 74 56 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e  stValue[CONCHLEN
ee470 5d 3b 0a 20 20 20 20 63 68 61 72 20 63 6f 6e 63  ];.    char conc
ee480 68 56 61 6c 75 65 5b 43 4f 4e 43 48 4c 45 4e 5d  hValue[CONCHLEN]
ee490 3b 0a 20 20 20 20 63 68 61 72 20 6c 6f 63 6b 50  ;.    char lockP
ee4a0 61 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e 5d 3b  ath[MAXPATHLEN];
ee4b0 0a 20 20 20 20 63 68 61 72 20 2a 74 4c 6f 63 6b  .    char *tLock
ee4c0 50 61 74 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20  Path = NULL;.   
ee4d0 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
ee4e0 5f 4f 4b 3b 0a 20 20 20 20 69 6e 74 20 72 65 61  _OK;.    int rea
ee4f0 64 52 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  dRc = SQLITE_OK;
ee500 0a 20 20 20 20 69 6e 74 20 73 79 6e 63 50 65 72  .    int syncPer
ee510 6d 73 20 3d 20 30 3b 0a 0a 20 20 20 20 4f 53 54  ms = 0;..    OST
ee520 52 41 43 45 34 28 22 54 41 4b 45 43 4f 4e 43 48  RACE4("TAKECONCH
ee530 20 20 25 64 20 66 6f 72 20 25 73 20 70 69 64 3d    %d for %s pid=
ee540 25 64 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65  %d\n", conchFile
ee550 2d 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ->h,.           
ee560 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f    (pCtx->lockPro
ee570 78 79 50 61 74 68 20 3f 20 70 43 74 78 2d 3e 6c  xyPath ? pCtx->l
ee580 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3a 20 22  ockProxyPath : "
ee590 3a 61 75 74 6f 3a 22 29 2c 20 67 65 74 70 69 64  :auto:"), getpid
ee5a0 28 29 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 63  ());..    rc = c
ee5b0 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f  onchFile->pMetho
ee5c0 64 2d 3e 78 4c 6f 63 6b 28 28 73 71 6c 69 74 65  d->xLock((sqlite
ee5d0 33 5f 66 69 6c 65 2a 29 63 6f 6e 63 68 46 69 6c  3_file*)conchFil
ee5e0 65 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b  e, SHARED_LOCK);
ee5f0 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
ee600 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
ee610 69 6e 74 20 70 45 72 72 6f 72 20 3d 20 30 3b 0a  int pError = 0;.
ee620 20 20 20 20 20 20 6d 65 6d 73 65 74 28 74 65 73        memset(tes
ee630 74 56 61 6c 75 65 2c 20 30 2c 20 43 4f 4e 43 48  tValue, 0, CONCH
ee640 4c 45 4e 29 3b 20 2f 2a 20 63 6f 6e 63 68 20 69  LEN); /* conch i
ee650 73 20 66 69 78 65 64 20 73 69 7a 65 20 2a 2f 0a  s fixed size */.
ee660 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79        rc = proxy
ee670 47 65 74 48 6f 73 74 49 44 28 74 65 73 74 56 61  GetHostID(testVa
ee680 6c 75 65 2c 20 26 70 45 72 72 6f 72 29 3b 0a 20  lue, &pError);. 
ee690 20 20 20 20 20 69 66 28 20 28 72 63 26 30 78 66       if( (rc&0xf
ee6a0 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  f)==SQLITE_IOERR
ee6b0 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c   ){.        pFil
ee6c0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 70  e->lastErrno = p
ee6d0 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20  Error;.      }. 
ee6e0 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e 6c       if( pCtx->l
ee6f0 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a  ockProxyPath ){.
ee700 20 20 20 20 20 20 20 20 73 74 72 6c 63 70 79 28          strlcpy(
ee710 26 74 65 73 74 56 61 6c 75 65 5b 48 4f 53 54 49  &testValue[HOSTI
ee720 44 4c 45 4e 5d 2c 20 70 43 74 78 2d 3e 6c 6f 63  DLEN], pCtx->loc
ee730 6b 50 72 6f 78 79 50 61 74 68 2c 20 4d 41 58 50  kProxyPath, MAXP
ee740 41 54 48 4c 45 4e 29 3b 0a 20 20 20 20 20 20 7d  ATHLEN);.      }
ee750 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
ee760 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
ee770 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74        goto end_t
ee780 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20 20 7d 0a  akeconch;.    }.
ee790 20 20 20 20 0a 20 20 20 20 72 65 61 64 52 63 20      .    readRc 
ee7a0 3d 20 75 6e 69 78 52 65 61 64 28 28 73 71 6c 69  = unixRead((sqli
ee7b0 74 65 33 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68  te3_file *)conch
ee7c0 46 69 6c 65 2c 20 63 6f 6e 63 68 56 61 6c 75 65  File, conchValue
ee7d0 2c 20 43 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a  , CONCHLEN, 0);.
ee7e0 20 20 20 20 69 66 28 20 72 65 61 64 52 63 21 3d      if( readRc!=
ee7f0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f  SQLITE_IOERR_SHO
ee800 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20  RT_READ ){.     
ee810 20 69 66 28 20 72 65 61 64 52 63 21 3d 53 51 4c   if( readRc!=SQL
ee820 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
ee830 20 20 69 66 28 20 28 72 63 26 30 78 66 66 29 3d    if( (rc&0xff)=
ee840 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 29 7b  =SQLITE_IOERR ){
ee850 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65  .          pFile
ee860 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 63 6f  ->lastErrno = co
ee870 6e 63 68 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72  nchFile->lastErr
ee880 6e 6f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  no;.        }.  
ee890 20 20 20 20 20 20 72 63 20 3d 20 72 65 61 64 52        rc = readR
ee8a0 63 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  c;.        goto 
ee8b0 65 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20  end_takeconch;. 
ee8c0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20       }.      /* 
ee8d0 69 66 20 74 68 65 20 63 6f 6e 63 68 20 68 61 73  if the conch has
ee8e0 20 64 61 74 61 20 63 6f 6d 70 61 72 65 20 74 68   data compare th
ee8f0 65 20 63 6f 6e 74 65 6e 74 73 20 2a 2f 0a 20 20  e contents */.  
ee900 20 20 20 20 69 66 28 20 21 70 43 74 78 2d 3e 6c      if( !pCtx->l
ee910 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b 0a  ockProxyPath ){.
ee920 20 20 20 20 20 20 20 20 2f 2a 20 66 6f 72 20 61          /* for a
ee930 75 74 6f 2d 6e 61 6d 65 64 20 6c 6f 63 61 6c 20  uto-named local 
ee940 6c 6f 63 6b 20 66 69 6c 65 2c 20 6a 75 73 74 20  lock file, just 
ee950 63 68 65 63 6b 20 74 68 65 20 68 6f 73 74 20 49  check the host I
ee960 44 20 61 6e 64 20 77 65 27 6c 6c 0a 20 20 20 20  D and we'll.    
ee970 20 20 20 20 20 2a 2a 20 75 73 65 20 74 68 65 20       ** use the 
ee980 6c 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20  local lock file 
ee990 70 61 74 68 20 74 68 61 74 27 73 20 61 6c 72 65  path that's alre
ee9a0 61 64 79 20 69 6e 20 74 68 65 72 65 20 2a 2f 0a  ady in there */.
ee9b0 20 20 20 20 20 20 20 20 69 66 28 20 21 6d 65 6d          if( !mem
ee9c0 63 6d 70 28 74 65 73 74 56 61 6c 75 65 2c 20 63  cmp(testValue, c
ee9d0 6f 6e 63 68 56 61 6c 75 65 2c 20 48 4f 53 54 49  onchValue, HOSTI
ee9e0 44 4c 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20  DLEN) ){.       
ee9f0 20 20 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 28     tLockPath = (
eea00 63 68 61 72 20 2a 29 26 63 6f 6e 63 68 56 61 6c  char *)&conchVal
eea10 75 65 5b 48 4f 53 54 49 44 4c 45 4e 5d 3b 0a 20  ue[HOSTIDLEN];. 
eea20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e           goto en
eea30 64 5f 74 61 6b 65 63 6f 6e 63 68 3b 0a 20 20 20  d_takeconch;.   
eea40 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
eea50 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 77  se{.        /* w
eea60 65 27 76 65 20 67 6f 74 20 74 68 65 20 63 6f 6e  e've got the con
eea70 63 68 20 69 66 20 63 6f 6e 63 68 56 61 6c 75 65  ch if conchValue
eea80 20 6d 61 74 63 68 65 73 20 6f 75 72 20 70 61 74   matches our pat
eea90 68 20 61 6e 64 20 68 6f 73 74 20 49 44 20 2a 2f  h and host ID */
eeaa0 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 6d 65  .        if( !me
eeab0 6d 63 6d 70 28 74 65 73 74 56 61 6c 75 65 2c 20  mcmp(testValue, 
eeac0 63 6f 6e 63 68 56 61 6c 75 65 2c 20 43 4f 4e 43  conchValue, CONC
eead0 48 4c 45 4e 29 20 29 7b 0a 20 20 20 20 20 20 20  HLEN) ){.       
eeae0 20 20 20 67 6f 74 6f 20 65 6e 64 5f 74 61 6b 65     goto end_take
eeaf0 63 6f 6e 63 68 3b 0a 20 20 20 20 20 20 20 20 7d  conch;.        }
eeb00 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
eeb10 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 61 20 73  se{.      /* a s
eeb20 68 6f 72 74 20 72 65 61 64 20 6d 65 61 6e 73 20  hort read means 
eeb30 77 65 27 72 65 20 22 63 72 65 61 74 69 6e 67 22  we're "creating"
eeb40 20 74 68 65 20 63 6f 6e 63 68 20 28 65 76 65 6e   the conch (even
eeb50 20 74 68 6f 75 67 68 20 69 74 20 63 6f 75 6c 64   though it could
eeb60 20 0a 20 20 20 20 20 20 2a 2a 20 68 61 76 65 20   .      ** have 
eeb70 62 65 65 6e 20 75 73 65 72 2d 69 6e 74 65 72 76  been user-interv
eeb80 65 6e 74 69 6f 6e 29 2c 20 69 66 20 77 65 20 61  ention), if we a
eeb90 63 71 75 69 72 65 20 74 68 65 20 65 78 63 6c 75  cquire the exclu
eeba0 73 69 76 65 20 6c 6f 63 6b 2c 0a 20 20 20 20 20  sive lock,.     
eebb0 20 2a 2a 20 77 65 27 6c 6c 20 74 72 79 20 74 6f   ** we'll try to
eebc0 20 6d 61 74 63 68 20 74 68 65 20 63 75 72 72 65   match the curre
eebd0 6e 74 20 6f 6e 2d 64 69 73 6b 20 70 65 72 6d 69  nt on-disk permi
eebe0 73 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 64 61  ssions of the da
eebf0 74 61 62 61 73 65 0a 20 20 20 20 20 20 2a 2f 0a  tabase.      */.
eec00 20 20 20 20 20 20 73 79 6e 63 50 65 72 6d 73 20        syncPerms 
eec10 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a  = 1;.    }.    .
eec20 20 20 20 20 2f 2a 20 65 69 74 68 65 72 20 63 6f      /* either co
eec30 6e 63 68 20 77 61 73 20 65 6d 74 70 79 20 6f 72  nch was emtpy or
eec40 20 64 69 64 6e 27 74 20 6d 61 74 63 68 20 2a 2f   didn't match */
eec50 0a 20 20 20 20 69 66 28 20 21 70 43 74 78 2d 3e  .    if( !pCtx->
eec60 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b  lockProxyPath ){
eec70 0a 20 20 20 20 20 20 70 72 6f 78 79 47 65 74 4c  .      proxyGetL
eec80 6f 63 6b 50 61 74 68 28 70 43 74 78 2d 3e 64 62  ockPath(pCtx->db
eec90 50 61 74 68 2c 20 6c 6f 63 6b 50 61 74 68 2c 20  Path, lockPath, 
eeca0 4d 41 58 50 41 54 48 4c 45 4e 29 3b 0a 20 20 20  MAXPATHLEN);.   
eecb0 20 20 20 74 4c 6f 63 6b 50 61 74 68 20 3d 20 6c     tLockPath = l
eecc0 6f 63 6b 50 61 74 68 3b 0a 20 20 20 20 20 20 73  ockPath;.      s
eecd0 74 72 6c 63 70 79 28 26 74 65 73 74 56 61 6c 75  trlcpy(&testValu
eece0 65 5b 48 4f 53 54 49 44 4c 45 4e 5d 2c 20 6c 6f  e[HOSTIDLEN], lo
eecf0 63 6b 50 61 74 68 2c 20 4d 41 58 50 41 54 48 4c  ckPath, MAXPATHL
eed00 45 4e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a  EN);.    }.    .
eed10 20 20 20 20 2f 2a 20 75 70 64 61 74 65 20 63 6f      /* update co
eed20 6e 63 68 20 77 69 74 68 20 68 6f 73 74 20 61 6e  nch with host an
eed30 64 20 70 61 74 68 20 28 74 68 69 73 20 77 69 6c  d path (this wil
eed40 6c 20 66 61 69 6c 20 69 66 20 6f 74 68 65 72 20  l fail if other 
eed50 70 72 6f 63 65 73 73 0a 20 20 20 20 20 2a 2a 20  process.     ** 
eed60 68 61 73 20 61 20 73 68 61 72 65 64 20 6c 6f 63  has a shared loc
eed70 6b 20 61 6c 72 65 61 64 79 29 20 2a 2f 0a 20 20  k already) */.  
eed80 20 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65    rc = conchFile
eed90 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b  ->pMethod->xLock
eeda0 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29  ((sqlite3_file*)
eedb0 63 6f 6e 63 68 46 69 6c 65 2c 20 45 58 43 4c 55  conchFile, EXCLU
eedc0 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20  SIVE_LOCK);.    
eedd0 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
eede0 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
eedf0 75 6e 69 78 57 72 69 74 65 28 28 73 71 6c 69 74  unixWrite((sqlit
eee00 65 33 5f 66 69 6c 65 20 2a 29 63 6f 6e 63 68 46  e3_file *)conchF
eee10 69 6c 65 2c 20 74 65 73 74 56 61 6c 75 65 2c 20  ile, testValue, 
eee20 43 4f 4e 43 48 4c 45 4e 2c 20 30 29 3b 0a 20 20  CONCHLEN, 0);.  
eee30 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
eee40 54 45 5f 4f 4b 20 26 26 20 73 79 6e 63 50 65 72  TE_OK && syncPer
eee50 6d 73 20 29 7b 0a 20 20 20 20 20 20 20 20 73 74  ms ){.        st
eee60 72 75 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20  ruct stat buf;. 
eee70 20 20 20 20 20 20 20 69 6e 74 20 65 72 72 20 3d         int err =
eee80 20 66 73 74 61 74 28 70 46 69 6c 65 2d 3e 68 2c   fstat(pFile->h,
eee90 20 26 62 75 66 29 3b 0a 20 20 20 20 20 20 20 20   &buf);.        
eeea0 69 66 28 20 65 72 72 3d 3d 30 20 29 7b 0a 20 20  if( err==0 ){.  
eeeb0 20 20 20 20 20 20 20 20 2f 2a 20 74 72 79 20 74          /* try t
eeec0 6f 20 6d 61 74 63 68 20 74 68 65 20 64 61 74 61  o match the data
eeed0 62 61 73 65 20 66 69 6c 65 20 70 65 72 6d 69 73  base file permis
eeee0 73 69 6f 6e 73 2c 20 69 67 6e 6f 72 65 20 66 61  sions, ignore fa
eeef0 69 6c 75 72 65 20 2a 2f 0a 23 69 66 6e 64 65 66  ilure */.#ifndef
eef00 20 53 51 4c 49 54 45 5f 50 52 4f 58 59 5f 44 45   SQLITE_PROXY_DE
eef10 42 55 47 0a 20 20 20 20 20 20 20 20 20 20 66 63  BUG.          fc
eef20 68 6d 6f 64 28 63 6f 6e 63 68 46 69 6c 65 2d 3e  hmod(conchFile->
eef30 68 2c 20 62 75 66 2e 73 74 5f 6d 6f 64 65 29 3b  h, buf.st_mode);
eef40 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20 20 20  .#else.         
eef50 20 69 66 28 20 66 63 68 6d 6f 64 28 63 6f 6e 63   if( fchmod(conc
eef60 68 46 69 6c 65 2d 3e 68 2c 20 62 75 66 2e 73 74  hFile->h, buf.st
eef70 5f 6d 6f 64 65 29 21 3d 30 20 29 7b 0a 20 20 20  _mode)!=0 ){.   
eef80 20 20 20 20 20 20 20 20 20 69 6e 74 20 63 6f 64           int cod
eef90 65 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 20  e = errno;.     
eefa0 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 73         fprintf(s
eefb0 74 64 65 72 72 2c 20 22 66 63 68 6d 6f 64 20 25  tderr, "fchmod %
eefc0 6f 20 46 41 49 4c 45 44 20 77 69 74 68 20 25 64  o FAILED with %d
eefd0 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20   %s\n",.        
eefe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eeff0 20 20 20 20 20 62 75 66 2e 73 74 5f 6d 6f 64 65       buf.st_mode
ef000 2c 20 63 6f 64 65 2c 20 73 74 72 65 72 72 6f 72  , code, strerror
ef010 28 63 6f 64 65 29 29 3b 0a 20 20 20 20 20 20 20  (code));.       
ef020 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20     } else {.    
ef030 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28          fprintf(
ef040 73 74 64 65 72 72 2c 20 22 66 63 68 6d 6f 64 20  stderr, "fchmod 
ef050 25 6f 20 53 55 43 43 45 44 45 44 5c 6e 22 2c 62  %o SUCCEDED\n",b
ef060 75 66 2e 73 74 5f 6d 6f 64 65 29 3b 0a 20 20 20  uf.st_mode);.   
ef070 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
ef080 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
ef090 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 65 72 72    int code = err
ef0a0 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 66 70  no;.          fp
ef0b0 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 53  rintf(stderr, "S
ef0c0 54 41 54 20 46 41 49 4c 45 44 5b 25 64 5d 20 77  TAT FAILED[%d] w
ef0d0 69 74 68 20 25 64 20 25 73 5c 6e 22 2c 20 0a 20  ith %d %s\n", . 
ef0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef0f0 20 20 20 20 20 20 20 20 20 65 72 72 2c 20 63 6f           err, co
ef100 64 65 2c 20 73 74 72 65 72 72 6f 72 28 63 6f 64  de, strerror(cod
ef110 65 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  e));.#endif.    
ef120 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
ef130 20 20 7d 0a 20 20 20 20 63 6f 6e 63 68 46 69 6c    }.    conchFil
ef140 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c  e->pMethod->xUnl
ef150 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c  ock((sqlite3_fil
ef160 65 2a 29 63 6f 6e 63 68 46 69 6c 65 2c 20 53 48  e*)conchFile, SH
ef170 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 0a 65  ARED_LOCK);.  .e
ef180 6e 64 5f 74 61 6b 65 63 6f 6e 63 68 3a 0a 20 20  nd_takeconch:.  
ef190 20 20 4f 53 54 52 41 43 45 32 28 22 54 52 41 4e    OSTRACE2("TRAN
ef1a0 53 50 52 4f 58 59 3a 20 43 4c 4f 53 45 20 20 25  SPROXY: CLOSE  %
ef1b0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b  d\n", pFile->h);
ef1c0 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
ef1d0 49 54 45 5f 4f 4b 20 26 26 20 70 46 69 6c 65 2d  ITE_OK && pFile-
ef1e0 3e 6f 70 65 6e 46 6c 61 67 73 20 29 7b 0a 20 20  >openFlags ){.  
ef1f0 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 68      if( pFile->h
ef200 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66 20 53 54  >=0 ){.#ifdef ST
ef210 52 49 43 54 5f 43 4c 4f 53 45 5f 45 52 52 4f 52  RICT_CLOSE_ERROR
ef220 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6c 6f  .        if( clo
ef230 73 65 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a  se(pFile->h) ){.
ef240 20 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d            pFile-
ef250 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72  >lastErrno = err
ef260 6e 6f 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65  no;.          re
ef270 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
ef280 52 5f 43 4c 4f 53 45 3b 0a 20 20 20 20 20 20 20  R_CLOSE;.       
ef290 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 20   }.#else.       
ef2a0 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 68 29   close(pFile->h)
ef2b0 3b 20 2f 2a 20 73 69 6c 65 6e 74 6c 79 20 6c 65  ; /* silently le
ef2c0 61 6b 20 66 64 20 69 66 20 66 61 69 6c 20 2a 2f  ak fd if fail */
ef2d0 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 0a  .#endif.      }.
ef2e0 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 20 3d        pFile->h =
ef2f0 20 2d 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 66   -1;.      int f
ef300 64 20 3d 20 6f 70 65 6e 28 70 43 74 78 2d 3e 64  d = open(pCtx->d
ef310 62 50 61 74 68 2c 20 70 46 69 6c 65 2d 3e 6f 70  bPath, pFile->op
ef320 65 6e 46 6c 61 67 73 2c 0a 20 20 20 20 20 20 20  enFlags,.       
ef330 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
ef340 49 54 45 5f 44 45 46 41 55 4c 54 5f 46 49 4c 45  ITE_DEFAULT_FILE
ef350 5f 50 45 52 4d 49 53 53 49 4f 4e 53 29 3b 0a 20  _PERMISSIONS);. 
ef360 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 54       OSTRACE2("T
ef370 52 41 4e 53 50 52 4f 58 59 3a 20 4f 50 45 4e 20  RANSPROXY: OPEN 
ef380 20 25 64 5c 6e 22 2c 20 66 64 29 3b 0a 20 20 20   %d\n", fd);.   
ef390 20 20 20 69 66 28 20 66 64 3e 3d 30 20 29 7b 0a     if( fd>=0 ){.
ef3a0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68          pFile->h
ef3b0 20 3d 20 66 64 3b 0a 20 20 20 20 20 20 7d 65 6c   = fd;.      }el
ef3c0 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63 3d 53  se{.        rc=S
ef3d0 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 20  QLITE_CANTOPEN; 
ef3e0 2f 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 3f 20  /* SQLITE_BUSY? 
ef3f0 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 20 63  proxyTakeConch c
ef400 61 6c 6c 65 64 0a 20 20 20 20 20 20 20 20 20 20  alled.          
ef410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef420 20 20 20 20 20 64 75 72 69 6e 67 20 6c 6f 63 6b       during lock
ef430 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20  ing */.      }. 
ef440 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d     }.    if( rc=
ef450 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70  =SQLITE_OK && !p
ef460 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 20 29  Ctx->lockProxy )
ef470 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70 61  {.      char *pa
ef480 74 68 20 3d 20 74 4c 6f 63 6b 50 61 74 68 20 3f  th = tLockPath ?
ef490 20 74 4c 6f 63 6b 50 61 74 68 20 3a 20 70 43 74   tLockPath : pCt
ef4a0 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68  x->lockProxyPath
ef4b0 3b 0a 20 20 20 20 20 20 2f 2a 20 41 43 53 3a 20  ;.      /* ACS: 
ef4c0 4e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 63  Need to make a c
ef4d0 6f 70 79 20 6f 66 20 70 61 74 68 20 73 6f 6d 65  opy of path some
ef4e0 74 69 6d 65 73 20 2a 2f 0a 20 20 20 20 20 20 72  times */.      r
ef4f0 63 20 3d 20 70 72 6f 78 79 43 72 65 61 74 65 55  c = proxyCreateU
ef500 6e 69 78 46 69 6c 65 28 70 61 74 68 2c 20 26 70  nixFile(path, &p
ef510 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 29 3b  Ctx->lockProxy);
ef520 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72  .    }.    if( r
ef530 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
ef540 20 20 20 20 20 20 70 43 74 78 2d 3e 63 6f 6e 63        pCtx->conc
ef550 68 48 65 6c 64 20 3d 20 31 3b 0a 0a 20 20 20 20  hHeld = 1;..    
ef560 20 20 69 66 28 20 74 4c 6f 63 6b 50 61 74 68 20    if( tLockPath 
ef570 29 7b 0a 20 20 20 20 20 20 20 20 70 43 74 78 2d  ){.        pCtx-
ef580 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 3d  >lockProxyPath =
ef590 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
ef5a0 28 30 2c 20 74 4c 6f 63 6b 50 61 74 68 29 3b 0a  (0, tLockPath);.
ef5b0 20 20 20 20 20 20 20 20 69 66 28 20 70 43 74 78          if( pCtx
ef5c0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 2d 3e 70 4d 65  ->lockProxy->pMe
ef5d0 74 68 6f 64 20 3d 3d 20 26 61 66 70 49 6f 4d 65  thod == &afpIoMe
ef5e0 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 20 20 20  thods ){.       
ef5f0 20 20 20 28 28 61 66 70 4c 6f 63 6b 69 6e 67 43     ((afpLockingC
ef600 6f 6e 74 65 78 74 20 2a 29 70 43 74 78 2d 3e 6c  ontext *)pCtx->l
ef610 6f 63 6b 50 72 6f 78 79 2d 3e 6c 6f 63 6b 69 6e  ockProxy->lockin
ef620 67 43 6f 6e 74 65 78 74 29 2d 3e 64 62 50 61 74  gContext)->dbPat
ef630 68 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20  h =.            
ef640 20 20 20 20 20 20 20 20 20 70 43 74 78 2d 3e 6c           pCtx->l
ef650 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20  ockProxyPath;.  
ef660 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
ef670 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20      } else {.   
ef680 20 20 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d     conchFile->pM
ef690 65 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28  ethod->xUnlock((
ef6a0 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f  sqlite3_file*)co
ef6b0 6e 63 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b  nchFile, NO_LOCK
ef6c0 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54  );.    }.    OST
ef6d0 52 41 43 45 33 28 22 54 41 4b 45 43 4f 4e 43 48  RACE3("TAKECONCH
ef6e0 20 20 25 64 20 25 73 5c 6e 22 2c 20 63 6f 6e 63    %d %s\n", conc
ef6f0 68 46 69 6c 65 2d 3e 68 2c 20 72 63 3d 3d 53 51  hFile->h, rc==SQ
ef700 4c 49 54 45 5f 4f 4b 3f 22 6f 6b 22 3a 22 66 61  LITE_OK?"ok":"fa
ef710 69 6c 65 64 22 29 3b 0a 20 20 20 20 72 65 74 75  iled");.    retu
ef720 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  rn rc;.  }.}../*
ef730 0a 2a 2a 20 49 66 20 70 46 69 6c 65 20 68 6f 6c  .** If pFile hol
ef740 64 73 20 61 20 6c 6f 63 6b 20 6f 6e 20 61 20 63  ds a lock on a c
ef750 6f 6e 63 68 20 66 69 6c 65 2c 20 74 68 65 6e 20  onch file, then 
ef760 72 65 6c 65 61 73 65 20 74 68 61 74 20 6c 6f 63  release that loc
ef770 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  k..*/.static int
ef780 20 70 72 6f 78 79 52 65 6c 65 61 73 65 43 6f 6e   proxyReleaseCon
ef790 63 68 28 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  ch(unixFile *pFi
ef7a0 6c 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20  le){.  int rc;  
ef7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ef7c0 20 20 20 2f 2a 20 53 75 62 72 6f 75 74 69 6e 65     /* Subroutine
ef7d0 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   return code */.
ef7e0 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f    proxyLockingCo
ef7f0 6e 74 65 78 74 20 2a 70 43 74 78 3b 20 20 2f 2a  ntext *pCtx;  /*
ef800 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e   The locking con
ef810 74 65 78 74 20 66 6f 72 20 74 68 65 20 70 72 6f  text for the pro
ef820 78 79 20 6c 6f 63 6b 20 2a 2f 0a 20 20 75 6e 69  xy lock */.  uni
ef830 78 46 69 6c 65 20 2a 63 6f 6e 63 68 46 69 6c 65  xFile *conchFile
ef840 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ;        /* Name
ef850 20 6f 66 20 74 68 65 20 63 6f 6e 63 68 20 66 69   of the conch fi
ef860 6c 65 20 2a 2f 0a 0a 20 20 70 43 74 78 20 3d 20  le */..  pCtx = 
ef870 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e  (proxyLockingCon
ef880 74 65 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f  text *)pFile->lo
ef890 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20  ckingContext;.  
ef8a0 63 6f 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 78  conchFile = pCtx
ef8b0 2d 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 4f  ->conchFile;.  O
ef8c0 53 54 52 41 43 45 34 28 22 52 45 4c 45 41 53 45  STRACE4("RELEASE
ef8d0 43 4f 4e 43 48 20 20 25 64 20 66 6f 72 20 25 73  CONCH  %d for %s
ef8e0 20 70 69 64 3d 25 64 5c 6e 22 2c 20 63 6f 6e 63   pid=%d\n", conc
ef8f0 68 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20  hFile->h,.      
ef900 20 20 20 20 20 28 70 43 74 78 2d 3e 6c 6f 63 6b       (pCtx->lock
ef910 50 72 6f 78 79 50 61 74 68 20 3f 20 70 43 74 78  ProxyPath ? pCtx
ef920 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20  ->lockProxyPath 
ef930 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20 0a 20 20  : ":auto:"), .  
ef940 20 20 20 20 20 20 20 20 20 67 65 74 70 69 64 28           getpid(
ef950 29 29 3b 0a 20 20 70 43 74 78 2d 3e 63 6f 6e 63  ));.  pCtx->conc
ef960 68 48 65 6c 64 20 3d 20 30 3b 0a 20 20 72 63 20  hHeld = 0;.  rc 
ef970 3d 20 63 6f 6e 63 68 46 69 6c 65 2d 3e 70 4d 65  = conchFile->pMe
ef980 74 68 6f 64 2d 3e 78 55 6e 6c 6f 63 6b 28 28 73  thod->xUnlock((s
ef990 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 63 6f 6e  qlite3_file*)con
ef9a0 63 68 46 69 6c 65 2c 20 4e 4f 5f 4c 4f 43 4b 29  chFile, NO_LOCK)
ef9b0 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22 52 45  ;.  OSTRACE3("RE
ef9c0 4c 45 41 53 45 43 4f 4e 43 48 20 20 25 64 20 25  LEASECONCH  %d %
ef9d0 73 5c 6e 22 2c 20 63 6f 6e 63 68 46 69 6c 65 2d  s\n", conchFile-
ef9e0 3e 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28  >h,.           (
ef9f0 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20  rc==SQLITE_OK ? 
efa00 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64 22 29  "ok" : "failed")
efa10 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
efa20 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74  }../*.** Given t
efa30 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74  he name of a dat
efa40 61 62 61 73 65 20 66 69 6c 65 2c 20 63 6f 6d 70  abase file, comp
efa50 75 74 65 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  ute the name of 
efa60 69 74 73 20 63 6f 6e 63 68 20 66 69 6c 65 2e 0a  its conch file..
efa70 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 63 6f 6e  ** Store the con
efa80 63 68 20 66 69 6c 65 6e 61 6d 65 20 69 6e 20 6d  ch filename in m
efa90 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
efaa0 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  rom sqlite3_mall
efab0 6f 63 28 29 2e 0a 2a 2a 20 4d 61 6b 65 20 2a 70  oc()..** Make *p
efac0 43 6f 6e 63 68 50 61 74 68 20 70 6f 69 6e 74 20  ConchPath point 
efad0 74 6f 20 74 68 65 20 6e 65 77 20 6e 61 6d 65 2e  to the new name.
efae0 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    Return SQLITE_
efaf0 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a  OK on success.**
efb00 20 6f 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d   or SQLITE_NOMEM
efb10 20 69 66 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62   if unable to ob
efb20 74 61 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a  tain memory..**.
efb30 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73  ** The caller is
efb40 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
efb50 20 65 6e 73 75 72 69 6e 67 20 74 68 61 74 20 74   ensuring that t
efb60 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d  he allocated mem
efb70 6f 72 79 0a 2a 2a 20 73 70 61 63 65 20 69 73 20  ory.** space is 
efb80 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65 65 64  eventually freed
efb90 2e 0a 2a 2a 0a 2a 2a 20 2a 70 43 6f 6e 63 68 50  ..**.** *pConchP
efba0 61 74 68 20 69 73 20 73 65 74 20 74 6f 20 4e 55  ath is set to NU
efbb0 4c 4c 20 69 66 20 61 20 6d 65 6d 6f 72 79 20 61  LL if a memory a
efbc0 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20  llocation error 
efbd0 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69  occurs..*/.stati
efbe0 63 20 69 6e 74 20 70 72 6f 78 79 43 72 65 61 74  c int proxyCreat
efbf0 65 43 6f 6e 63 68 50 61 74 68 6e 61 6d 65 28 63  eConchPathname(c
efc00 68 61 72 20 2a 64 62 50 61 74 68 2c 20 63 68 61  har *dbPath, cha
efc10 72 20 2a 2a 70 43 6f 6e 63 68 50 61 74 68 29 7b  r **pConchPath){
efc20 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
efc30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
efc40 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
efc50 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 20 3d 20   */.  int len = 
efc60 28 69 6e 74 29 73 74 72 6c 65 6e 28 64 62 50 61  (int)strlen(dbPa
efc70 74 68 29 3b 20 2f 2a 20 4c 65 6e 67 74 68 20 6f  th); /* Length o
efc80 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 6e  f database filen
efc90 61 6d 65 20 2d 20 64 62 50 61 74 68 20 2a 2f 0a  ame - dbPath */.
efca0 20 20 63 68 61 72 20 2a 63 6f 6e 63 68 50 61 74    char *conchPat
efcb0 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
efcc0 2f 2a 20 62 75 66 66 65 72 20 69 6e 20 77 68 69  /* buffer in whi
efcd0 63 68 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20  ch to construct 
efce0 63 6f 6e 63 68 20 6e 61 6d 65 20 2a 2f 0a 0a 20  conch name */.. 
efcf0 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61   /* Allocate spa
efd00 63 65 20 66 6f 72 20 74 68 65 20 63 6f 6e 63 68  ce for the conch
efd10 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64 20 69 6e   filename and in
efd20 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6e 61 6d  itialize the nam
efd30 65 20 74 6f 0a 20 20 2a 2a 20 74 68 65 20 6e 61  e to.  ** the na
efd40 6d 65 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e  me of the origin
efd50 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  al database file
efd60 2e 20 2a 2f 20 20 0a 20 20 2a 70 43 6f 6e 63 68  . */  .  *pConch
efd70 50 61 74 68 20 3d 20 63 6f 6e 63 68 50 61 74 68  Path = conchPath
efd80 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74   = (char *)sqlit
efd90 65 33 5f 6d 61 6c 6c 6f 63 28 6c 65 6e 20 2b 20  e3_malloc(len + 
efda0 38 29 3b 0a 20 20 69 66 28 20 63 6f 6e 63 68 50  8);.  if( conchP
efdb0 61 74 68 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  ath==0 ){.    re
efdc0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
efdd0 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28  M;.  }.  memcpy(
efde0 63 6f 6e 63 68 50 61 74 68 2c 20 64 62 50 61 74  conchPath, dbPat
efdf0 68 2c 20 6c 65 6e 2b 31 29 3b 0a 20 20 0a 20 20  h, len+1);.  .  
efe00 2f 2a 20 6e 6f 77 20 69 6e 73 65 72 74 20 61 20  /* now insert a 
efe10 22 2e 22 20 62 65 66 6f 72 65 20 74 68 65 20 6c  "." before the l
efe20 61 73 74 20 2f 20 63 68 61 72 61 63 74 65 72 20  ast / character 
efe30 2a 2f 0a 20 20 66 6f 72 28 20 69 3d 28 6c 65 6e  */.  for( i=(len
efe40 2d 31 29 3b 20 69 3e 3d 30 3b 20 69 2d 2d 20 29  -1); i>=0; i-- )
efe50 7b 0a 20 20 20 20 69 66 28 20 63 6f 6e 63 68 50  {.    if( conchP
efe60 61 74 68 5b 69 5d 3d 3d 27 2f 27 20 29 7b 0a 20  ath[i]=='/' ){. 
efe70 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20       i++;.      
efe80 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
efe90 0a 20 20 63 6f 6e 63 68 50 61 74 68 5b 69 5d 3d  .  conchPath[i]=
efea0 27 2e 27 3b 0a 20 20 77 68 69 6c 65 20 28 20 69  '.';.  while ( i
efeb0 3c 6c 65 6e 20 29 7b 0a 20 20 20 20 63 6f 6e 63  <len ){.    conc
efec0 68 50 61 74 68 5b 69 2b 31 5d 3d 64 62 50 61 74  hPath[i+1]=dbPat
efed0 68 5b 69 5d 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20  h[i];.    i++;. 
efee0 20 7d 0a 0a 20 20 2f 2a 20 61 70 70 65 6e 64 20   }..  /* append 
efef0 74 68 65 20 22 2d 63 6f 6e 63 68 22 20 73 75 66  the "-conch" suf
eff00 66 69 78 20 74 6f 20 74 68 65 20 66 69 6c 65 20  fix to the file 
eff10 2a 2f 0a 20 20 6d 65 6d 63 70 79 28 26 63 6f 6e  */.  memcpy(&con
eff20 63 68 50 61 74 68 5b 69 2b 31 5d 2c 20 22 2d 63  chPath[i+1], "-c
eff30 6f 6e 63 68 22 2c 20 37 29 3b 0a 20 20 61 73 73  onch", 7);.  ass
eff40 65 72 74 28 20 28 69 6e 74 29 73 74 72 6c 65 6e  ert( (int)strlen
eff50 28 63 6f 6e 63 68 50 61 74 68 29 20 3d 3d 20 6c  (conchPath) == l
eff60 65 6e 2b 37 20 29 3b 0a 0a 20 20 72 65 74 75 72  en+7 );..  retur
eff70 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
eff80 0a 2f 2a 20 54 61 6b 65 73 20 61 20 66 75 6c 6c  ./* Takes a full
eff90 79 20 63 6f 6e 66 69 67 75 72 65 64 20 70 72 6f  y configured pro
effa0 78 79 20 6c 6f 63 6b 69 6e 67 2d 73 74 79 6c 65  xy locking-style
effb0 20 75 6e 69 78 20 66 69 6c 65 20 61 6e 64 20 73   unix file and s
effc0 77 69 74 63 68 65 73 0a 2a 2a 20 74 68 65 20 6c  witches.** the l
effd0 6f 63 61 6c 20 6c 6f 63 6b 20 66 69 6c 65 20 70  ocal lock file p
effe0 61 74 68 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ath .*/.static i
efff0 6e 74 20 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f  nt switchLockPro
f0000 78 79 50 61 74 68 28 75 6e 69 78 46 69 6c 65 20  xyPath(unixFile 
f0010 2a 70 46 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68  *pFile, const ch
f0020 61 72 20 2a 70 61 74 68 29 20 7b 0a 20 20 70 72  ar *path) {.  pr
f0030 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  oxyLockingContex
f0040 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79  t *pCtx = (proxy
f0050 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29  LockingContext*)
f0060 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
f0070 6e 74 65 78 74 3b 0a 20 20 63 68 61 72 20 2a 6f  ntext;.  char *o
f0080 6c 64 50 61 74 68 20 3d 20 70 43 74 78 2d 3e 6c  ldPath = pCtx->l
f0090 6f 63 6b 50 72 6f 78 79 50 61 74 68 3b 0a 20 20  ockProxyPath;.  
f00a0 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
f00b0 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 46 69 6c 65  OK;..  if( pFile
f00c0 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c  ->locktype!=NO_L
f00d0 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  OCK ){.    retur
f00e0 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  n SQLITE_BUSY;. 
f00f0 20 7d 20 20 0a 0a 20 20 2f 2a 20 6e 6f 74 68 69   }  ..  /* nothi
f0100 6e 67 20 74 6f 20 64 6f 20 69 66 20 74 68 65 20  ng to do if the 
f0110 70 61 74 68 20 69 73 20 4e 55 4c 4c 2c 20 3a 61  path is NULL, :a
f0120 75 74 6f 3a 20 6f 72 20 6d 61 74 63 68 65 73 20  uto: or matches 
f0130 74 68 65 20 65 78 69 73 74 69 6e 67 20 70 61 74  the existing pat
f0140 68 20 2a 2f 0a 20 20 69 66 28 20 21 70 61 74 68  h */.  if( !path
f0150 20 7c 7c 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30   || path[0]=='\0
f0160 27 20 7c 7c 20 21 73 74 72 63 6d 70 28 70 61 74  ' || !strcmp(pat
f0170 68 2c 20 22 3a 61 75 74 6f 3a 22 29 20 7c 7c 0a  h, ":auto:") ||.
f0180 20 20 20 20 28 6f 6c 64 50 61 74 68 20 26 26 20      (oldPath && 
f0190 21 73 74 72 6e 63 6d 70 28 6f 6c 64 50 61 74 68  !strncmp(oldPath
f01a0 2c 20 70 61 74 68 2c 20 4d 41 58 50 41 54 48 4c  , path, MAXPATHL
f01b0 45 4e 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75  EN)) ){.    retu
f01c0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
f01d0 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6e 69 78 46  }else{.    unixF
f01e0 69 6c 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d  ile *lockProxy =
f01f0 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79   pCtx->lockProxy
f0200 3b 0a 20 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b  ;.    pCtx->lock
f0210 50 72 6f 78 79 3d 4e 55 4c 4c 3b 0a 20 20 20 20  Proxy=NULL;.    
f0220 70 43 74 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20  pCtx->conchHeld 
f0230 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 6c 6f 63  = 0;.    if( loc
f0240 6b 50 72 6f 78 79 21 3d 4e 55 4c 4c 20 29 7b 0a  kProxy!=NULL ){.
f0250 20 20 20 20 20 20 72 63 3d 6c 6f 63 6b 50 72 6f        rc=lockPro
f0260 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c  xy->pMethod->xCl
f0270 6f 73 65 28 28 73 71 6c 69 74 65 33 5f 66 69 6c  ose((sqlite3_fil
f0280 65 20 2a 29 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a  e *)lockProxy);.
f0290 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 72        if( rc ) r
f02a0 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
f02b0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6c 6f 63  sqlite3_free(loc
f02c0 6b 50 72 6f 78 79 29 3b 0a 20 20 20 20 7d 0a 20  kProxy);.    }. 
f02d0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
f02e0 6f 6c 64 50 61 74 68 29 3b 0a 20 20 20 20 70 43  oldPath);.    pC
f02f0 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74  tx->lockProxyPat
f0300 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  h = sqlite3DbStr
f0310 44 75 70 28 30 2c 20 70 61 74 68 29 3b 0a 20 20  Dup(0, path);.  
f0320 7d 0a 20 20 0a 20 20 72 65 74 75 72 6e 20 72 63  }.  .  return rc
f0330 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 46 69 6c 65  ;.}../*.** pFile
f0340 20 69 73 20 61 20 66 69 6c 65 20 74 68 61 74 20   is a file that 
f0350 68 61 73 20 62 65 65 6e 20 6f 70 65 6e 65 64 20  has been opened 
f0360 62 79 20 61 20 70 72 69 6f 72 20 78 4f 70 65 6e  by a prior xOpen
f0370 20 63 61 6c 6c 2e 20 20 64 62 50 61 74 68 0a 2a   call.  dbPath.*
f0380 2a 20 69 73 20 61 20 73 74 72 69 6e 67 20 62 75  * is a string bu
f0390 66 66 65 72 20 61 74 20 6c 65 61 73 74 20 4d 41  ffer at least MA
f03a0 58 50 41 54 48 4c 45 4e 2b 31 20 63 68 61 72 61  XPATHLEN+1 chara
f03b0 63 74 65 72 73 20 69 6e 20 73 69 7a 65 2e 0a 2a  cters in size..*
f03c0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
f03d0 65 20 66 69 6e 64 20 74 68 65 20 66 69 6c 65 6e  e find the filen
f03e0 61 6d 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  ame associated w
f03f0 69 74 68 20 70 46 69 6c 65 20 61 6e 64 20 77 72  ith pFile and wr
f0400 69 74 65 73 20 69 74 0a 2a 2a 20 69 6e 74 20 64  ites it.** int d
f0410 62 50 61 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63  bPath..*/.static
f0420 20 69 6e 74 20 70 72 6f 78 79 47 65 74 44 62 50   int proxyGetDbP
f0430 61 74 68 46 6f 72 55 6e 69 78 46 69 6c 65 28 75  athForUnixFile(u
f0440 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 2c 20  nixFile *pFile, 
f0450 63 68 61 72 20 2a 64 62 50 61 74 68 29 7b 0a 23  char *dbPath){.#
f0460 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 41 50 50  if defined(__APP
f0470 4c 45 5f 5f 29 0a 20 20 69 66 28 20 70 46 69 6c  LE__).  if( pFil
f0480 65 2d 3e 70 4d 65 74 68 6f 64 20 3d 3d 20 26 61  e->pMethod == &a
f0490 66 70 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20  fpIoMethods ){. 
f04a0 20 20 20 2f 2a 20 61 66 70 20 73 74 79 6c 65 20     /* afp style 
f04b0 6b 65 65 70 73 20 61 20 72 65 66 65 72 65 6e 63  keeps a referenc
f04c0 65 20 74 6f 20 74 68 65 20 64 62 20 70 61 74 68  e to the db path
f04d0 20 69 6e 20 74 68 65 20 66 69 6c 65 50 61 74 68   in the filePath
f04e0 20 66 69 65 6c 64 20 0a 20 20 20 20 2a 2a 20 6f   field .    ** o
f04f0 66 20 74 68 65 20 73 74 72 75 63 74 20 2a 2f 0a  f the struct */.
f0500 20 20 20 20 61 73 73 65 72 74 28 20 28 69 6e 74      assert( (int
f0510 29 73 74 72 6c 65 6e 28 28 63 68 61 72 2a 29 70  )strlen((char*)p
f0520 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
f0530 74 65 78 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45  text)<=MAXPATHLE
f0540 4e 20 29 3b 0a 20 20 20 20 73 74 72 63 70 79 28  N );.    strcpy(
f0550 64 62 50 61 74 68 2c 20 28 28 61 66 70 4c 6f 63  dbPath, ((afpLoc
f0560 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70 46  kingContext *)pF
f0570 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
f0580 65 78 74 29 2d 3e 64 62 50 61 74 68 29 3b 0a 20  ext)->dbPath);. 
f0590 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20   }else.#endif.  
f05a0 69 66 28 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68  if( pFile->pMeth
f05b0 6f 64 20 3d 3d 20 26 64 6f 74 6c 6f 63 6b 49 6f  od == &dotlockIo
f05c0 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 2f  Methods ){.    /
f05d0 2a 20 64 6f 74 20 6c 6f 63 6b 20 73 74 79 6c 65  * dot lock style
f05e0 20 75 73 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e   uses the lockin
f05f0 67 20 63 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f  g context to sto
f0600 72 65 20 74 68 65 20 64 6f 74 20 6c 6f 63 6b 0a  re the dot lock.
f0610 20 20 20 20 2a 2a 20 66 69 6c 65 20 70 61 74 68      ** file path
f0620 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c 65 6e 20   */.    int len 
f0630 3d 20 73 74 72 6c 65 6e 28 28 63 68 61 72 20 2a  = strlen((char *
f0640 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43  )pFile->lockingC
f0650 6f 6e 74 65 78 74 29 20 2d 20 73 74 72 6c 65 6e  ontext) - strlen
f0660 28 44 4f 54 4c 4f 43 4b 5f 53 55 46 46 49 58 29  (DOTLOCK_SUFFIX)
f0670 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 64 62 50  ;.    memcpy(dbP
f0680 61 74 68 2c 20 28 63 68 61 72 20 2a 29 70 46 69  ath, (char *)pFi
f0690 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
f06a0 78 74 2c 20 6c 65 6e 20 2b 20 31 29 3b 0a 20 20  xt, len + 1);.  
f06b0 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 61 6c  }else{.    /* al
f06c0 6c 20 6f 74 68 65 72 20 73 74 79 6c 65 73 20 75  l other styles u
f06d0 73 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63  se the locking c
f06e0 6f 6e 74 65 78 74 20 74 6f 20 73 74 6f 72 65 20  ontext to store 
f06f0 74 68 65 20 64 62 20 66 69 6c 65 20 70 61 74 68  the db file path
f0700 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
f0710 73 74 72 6c 65 6e 28 28 63 68 61 72 2a 29 70 46  strlen((char*)pF
f0720 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
f0730 65 78 74 29 3c 3d 4d 41 58 50 41 54 48 4c 45 4e  ext)<=MAXPATHLEN
f0740 20 29 3b 0a 20 20 20 20 73 74 72 63 70 79 28 64   );.    strcpy(d
f0750 62 50 61 74 68 2c 20 28 63 68 61 72 20 2a 29 70  bPath, (char *)p
f0760 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
f0770 74 65 78 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74  text);.  }.  ret
f0780 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
f0790 0a 0a 2f 2a 0a 2a 2a 20 54 61 6b 65 73 20 61 6e  ../*.** Takes an
f07a0 20 61 6c 72 65 61 64 79 20 66 69 6c 6c 65 64 20   already filled 
f07b0 69 6e 20 75 6e 69 78 20 66 69 6c 65 20 61 6e 64  in unix file and
f07c0 20 61 6c 74 65 72 73 20 69 74 20 73 6f 20 61 6c   alters it so al
f07d0 6c 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 0a  l file locking .
f07e0 2a 2a 20 77 69 6c 6c 20 62 65 20 70 65 72 66 6f  ** will be perfo
f07f0 72 6d 65 64 20 6f 6e 20 74 68 65 20 6c 6f 63 61  rmed on the loca
f0800 6c 20 70 72 6f 78 79 20 6c 6f 63 6b 20 66 69 6c  l proxy lock fil
f0810 65 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  e.  The followin
f0820 67 20 66 69 65 6c 64 73 0a 2a 2a 20 61 72 65 20  g fields.** are 
f0830 70 72 65 73 65 72 76 65 64 20 69 6e 20 74 68 65  preserved in the
f0840 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74   locking context
f0850 20 73 6f 20 74 68 61 74 20 74 68 65 79 20 63 61   so that they ca
f0860 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20 61 6e  n be restored an
f0870 64 20 0a 2a 2a 20 74 68 65 20 75 6e 69 78 20 73  d .** the unix s
f0880 74 72 75 63 74 75 72 65 20 70 72 6f 70 65 72 6c  tructure properl
f0890 79 20 63 6c 65 61 6e 65 64 20 75 70 20 61 74 20  y cleaned up at 
f08a0 63 6c 6f 73 65 20 74 69 6d 65 3a 0a 2a 2a 20 20  close time:.**  
f08b0 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
f08c0 0a 2a 2a 20 20 2d 3e 70 4d 65 74 68 6f 64 0a 2a  .**  ->pMethod.*
f08d0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f  /.static int pro
f08e0 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78 46  xyTransformUnixF
f08f0 69 6c 65 28 75 6e 69 78 46 69 6c 65 20 2a 70 46  ile(unixFile *pF
f0900 69 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ile, const char 
f0910 2a 70 61 74 68 29 20 7b 0a 20 20 70 72 6f 78 79  *path) {.  proxy
f0920 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a  LockingContext *
f0930 70 43 74 78 3b 0a 20 20 63 68 61 72 20 64 62 50  pCtx;.  char dbP
f0940 61 74 68 5b 4d 41 58 50 41 54 48 4c 45 4e 2b 31  ath[MAXPATHLEN+1
f0950 5d 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ];       /* Name
f0960 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
f0970 20 66 69 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20   file */.  char 
f0980 2a 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b 0a  *lockPath=NULL;.
f0990 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
f09a0 45 5f 4f 4b 3b 0a 20 20 0a 20 20 69 66 28 20 70  E_OK;.  .  if( p
f09b0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d  File->locktype!=
f09c0 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72  NO_LOCK ){.    r
f09d0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
f09e0 59 3b 0a 20 20 7d 0a 20 20 70 72 6f 78 79 47 65  Y;.  }.  proxyGe
f09f0 74 44 62 50 61 74 68 46 6f 72 55 6e 69 78 46 69  tDbPathForUnixFi
f0a00 6c 65 28 70 46 69 6c 65 2c 20 64 62 50 61 74 68  le(pFile, dbPath
f0a10 29 3b 0a 20 20 69 66 28 20 21 70 61 74 68 20 7c  );.  if( !path |
f0a20 7c 20 70 61 74 68 5b 30 5d 3d 3d 27 5c 30 27 20  | path[0]=='\0' 
f0a30 7c 7c 20 21 73 74 72 63 6d 70 28 70 61 74 68 2c  || !strcmp(path,
f0a40 20 22 3a 61 75 74 6f 3a 22 29 20 29 7b 0a 20 20   ":auto:") ){.  
f0a50 20 20 6c 6f 63 6b 50 61 74 68 3d 4e 55 4c 4c 3b    lockPath=NULL;
f0a60 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6c 6f  .  }else{.    lo
f0a70 63 6b 50 61 74 68 3d 28 63 68 61 72 20 2a 29 70  ckPath=(char *)p
f0a80 61 74 68 3b 0a 20 20 7d 0a 20 20 0a 20 20 4f 53  ath;.  }.  .  OS
f0a90 54 52 41 43 45 34 28 22 54 52 41 4e 53 50 52 4f  TRACE4("TRANSPRO
f0aa0 58 59 20 20 25 64 20 66 6f 72 20 25 73 20 70 69  XY  %d for %s pi
f0ab0 64 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  d=%d\n", pFile->
f0ac0 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28 6c  h,.           (l
f0ad0 6f 63 6b 50 61 74 68 20 3f 20 6c 6f 63 6b 50 61  ockPath ? lockPa
f0ae0 74 68 20 3a 20 22 3a 61 75 74 6f 3a 22 29 2c 20  th : ":auto:"), 
f0af0 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20 70 43  getpid());..  pC
f0b00 74 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  tx = sqlite3_mal
f0b10 6c 6f 63 28 20 73 69 7a 65 6f 66 28 2a 70 43 74  loc( sizeof(*pCt
f0b20 78 29 20 29 3b 0a 20 20 69 66 28 20 70 43 74 78  x) );.  if( pCtx
f0b30 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
f0b40 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
f0b50 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 43 74    }.  memset(pCt
f0b60 78 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 43  x, 0, sizeof(*pC
f0b70 74 78 29 29 3b 0a 0a 20 20 72 63 20 3d 20 70 72  tx));..  rc = pr
f0b80 6f 78 79 43 72 65 61 74 65 43 6f 6e 63 68 50 61  oxyCreateConchPa
f0b90 74 68 6e 61 6d 65 28 64 62 50 61 74 68 2c 20 26  thname(dbPath, &
f0ba0 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50  pCtx->conchFileP
f0bb0 61 74 68 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  ath);.  if( rc==
f0bc0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
f0bd0 20 72 63 20 3d 20 70 72 6f 78 79 43 72 65 61 74   rc = proxyCreat
f0be0 65 55 6e 69 78 46 69 6c 65 28 70 43 74 78 2d 3e  eUnixFile(pCtx->
f0bf0 63 6f 6e 63 68 46 69 6c 65 50 61 74 68 2c 20 26  conchFilePath, &
f0c00 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 29  pCtx->conchFile)
f0c10 3b 0a 20 20 7d 20 20 0a 20 20 69 66 28 20 72 63  ;.  }  .  if( rc
f0c20 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6c  ==SQLITE_OK && l
f0c30 6f 63 6b 50 61 74 68 20 29 7b 0a 20 20 20 20 70  ockPath ){.    p
f0c40 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61  Ctx->lockProxyPa
f0c50 74 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  th = sqlite3DbSt
f0c60 72 44 75 70 28 30 2c 20 6c 6f 63 6b 50 61 74 68  rDup(0, lockPath
f0c70 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63  );.  }..  if( rc
f0c80 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
f0c90 20 20 20 2f 2a 20 61 6c 6c 20 6d 65 6d 6f 72 79     /* all memory
f0ca0 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20 70   is allocated, p
f0cb0 72 6f 78 79 73 20 61 72 65 20 63 72 65 61 74 65  roxys are create
f0cc0 64 20 61 6e 64 20 61 73 73 69 67 6e 65 64 2c 20  d and assigned, 
f0cd0 0a 20 20 20 20 2a 2a 20 73 77 69 74 63 68 20 74  .    ** switch t
f0ce0 68 65 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65  he locking conte
f0cf0 78 74 20 61 6e 64 20 70 4d 65 74 68 6f 64 20 74  xt and pMethod t
f0d00 68 65 6e 20 72 65 74 75 72 6e 2e 0a 20 20 20 20  hen return..    
f0d10 2a 2f 0a 20 20 20 20 70 43 74 78 2d 3e 64 62 50  */.    pCtx->dbP
f0d20 61 74 68 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ath = sqlite3DbS
f0d30 74 72 44 75 70 28 30 2c 20 64 62 50 61 74 68 29  trDup(0, dbPath)
f0d40 3b 0a 20 20 20 20 70 43 74 78 2d 3e 6f 6c 64 4c  ;.    pCtx->oldL
f0d50 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20  ockingContext = 
f0d60 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
f0d70 6e 74 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65  ntext;.    pFile
f0d80 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
f0d90 20 3d 20 70 43 74 78 3b 0a 20 20 20 20 70 43 74   = pCtx;.    pCt
f0da0 78 2d 3e 70 4f 6c 64 4d 65 74 68 6f 64 20 3d 20  x->pOldMethod = 
f0db0 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 3b 0a  pFile->pMethod;.
f0dc0 20 20 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68      pFile->pMeth
f0dd0 6f 64 20 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74  od = &proxyIoMet
f0de0 68 6f 64 73 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  hods;.  }else{. 
f0df0 20 20 20 69 66 28 20 70 43 74 78 2d 3e 63 6f 6e     if( pCtx->con
f0e00 63 68 46 69 6c 65 20 29 7b 20 0a 20 20 20 20 20  chFile ){ .     
f0e10 20 72 63 20 3d 20 70 43 74 78 2d 3e 63 6f 6e 63   rc = pCtx->conc
f0e20 68 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d 3e  hFile->pMethod->
f0e30 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65 33 5f  xClose((sqlite3_
f0e40 66 69 6c 65 20 2a 29 70 43 74 78 2d 3e 63 6f 6e  file *)pCtx->con
f0e50 63 68 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 69  chFile);.      i
f0e60 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
f0e70 63 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  c;.      sqlite3
f0e80 5f 66 72 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63  _free(pCtx->conc
f0e90 68 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20  hFile);.    }.  
f0ea0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
f0eb0 43 74 78 2d 3e 63 6f 6e 63 68 46 69 6c 65 50 61  Ctx->conchFilePa
f0ec0 74 68 29 3b 20 0a 20 20 20 20 73 71 6c 69 74 65  th); .    sqlite
f0ed0 33 5f 66 72 65 65 28 70 43 74 78 29 3b 0a 20 20  3_free(pCtx);.  
f0ee0 7d 0a 20 20 4f 53 54 52 41 43 45 33 28 22 54 52  }.  OSTRACE3("TR
f0ef0 41 4e 53 50 52 4f 58 59 20 20 25 64 20 25 73 5c  ANSPROXY  %d %s\
f0f00 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20  n", pFile->h,.  
f0f10 20 20 20 20 20 20 20 20 20 28 72 63 3d 3d 53 51           (rc==SQ
f0f20 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a  LITE_OK ? "ok" :
f0f30 20 22 66 61 69 6c 65 64 22 29 29 3b 0a 20 20 72   "failed"));.  r
f0f40 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
f0f50 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
f0f60 20 68 61 6e 64 6c 65 73 20 73 71 6c 69 74 65 33   handles sqlite3
f0f70 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 20  _file_control() 
f0f80 63 61 6c 6c 73 20 74 68 61 74 20 61 72 65 20 73  calls that are s
f0f90 70 65 63 69 66 69 63 0a 2a 2a 20 74 6f 20 70 72  pecific.** to pr
f0fa0 6f 78 79 20 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a  oxy locking..*/.
f0fb0 73 74 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79  static int proxy
f0fc0 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c 69  FileControl(sqli
f0fd0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
f0fe0 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67  t op, void *pArg
f0ff0 29 7b 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20  ){.  switch( op 
f1000 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  ){.    case SQLI
f1010 54 45 5f 47 45 54 5f 4c 4f 43 4b 50 52 4f 58 59  TE_GET_LOCKPROXY
f1020 46 49 4c 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e  FILE: {.      un
f1030 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
f1040 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
f1050 20 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e       if( pFile->
f1060 70 4d 65 74 68 6f 64 20 3d 3d 20 26 70 72 6f 78  pMethod == &prox
f1070 79 49 6f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20  yIoMethods ){.  
f1080 20 20 20 20 20 20 70 72 6f 78 79 4c 6f 63 6b 69        proxyLocki
f1090 6e 67 43 6f 6e 74 65 78 74 20 2a 70 43 74 78 20  ngContext *pCtx 
f10a0 3d 20 28 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43  = (proxyLockingC
f10b0 6f 6e 74 65 78 74 2a 29 70 46 69 6c 65 2d 3e 6c  ontext*)pFile->l
f10c0 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20  ockingContext;. 
f10d0 20 20 20 20 20 20 20 70 72 6f 78 79 54 61 6b 65         proxyTake
f10e0 43 6f 6e 63 68 28 70 46 69 6c 65 29 3b 0a 20 20  Conch(pFile);.  
f10f0 20 20 20 20 20 20 69 66 28 20 70 43 74 78 2d 3e        if( pCtx->
f1100 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 29 7b  lockProxyPath ){
f1110 0a 20 20 20 20 20 20 20 20 20 20 2a 28 63 6f 6e  .          *(con
f1120 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72 67 20  st char **)pArg 
f1130 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78  = pCtx->lockProx
f1140 79 50 61 74 68 3b 0a 20 20 20 20 20 20 20 20 7d  yPath;.        }
f1150 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
f1160 2a 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29  *(const char **)
f1170 70 41 72 67 20 3d 20 22 3a 61 75 74 6f 3a 20 28  pArg = ":auto: (
f1180 6e 6f 74 20 68 65 6c 64 29 22 3b 0a 20 20 20 20  not held)";.    
f1190 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20 65 6c      }.      } el
f11a0 73 65 20 7b 0a 20 20 20 20 20 20 20 20 2a 28 63  se {.        *(c
f11b0 6f 6e 73 74 20 63 68 61 72 20 2a 2a 29 70 41 72  onst char **)pAr
f11c0 67 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20  g = NULL;.      
f11d0 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  }.      return S
f11e0 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
f11f0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
f1200 53 45 54 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c  SET_LOCKPROXYFIL
f1210 45 3a 20 7b 0a 20 20 20 20 20 20 75 6e 69 78 46  E: {.      unixF
f1220 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
f1230 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20  ixFile*)id;.    
f1240 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
f1250 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 69 6e 74 20  E_OK;.      int 
f1260 69 73 50 72 6f 78 79 53 74 79 6c 65 20 3d 20 28  isProxyStyle = (
f1270 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 20 3d  pFile->pMethod =
f1280 3d 20 26 70 72 6f 78 79 49 6f 4d 65 74 68 6f 64  = &proxyIoMethod
f1290 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 41  s);.      if( pA
f12a0 72 67 3d 3d 4e 55 4c 4c 20 7c 7c 20 28 63 6f 6e  rg==NULL || (con
f12b0 73 74 20 63 68 61 72 20 2a 29 70 41 72 67 3d 3d  st char *)pArg==
f12c0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  0 ){.        if(
f12d0 20 69 73 50 72 6f 78 79 53 74 79 6c 65 20 29 7b   isProxyStyle ){
f12e0 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 74 75  .          /* tu
f12f0 72 6e 20 6f 66 66 20 70 72 6f 78 79 20 6c 6f 63  rn off proxy loc
f1300 6b 69 6e 67 20 2d 20 6e 6f 74 20 73 75 70 70 6f  king - not suppo
f1310 72 74 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20  rted */.        
f1320 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
f1330 52 4f 52 20 2f 2a 53 51 4c 49 54 45 5f 50 52 4f  ROR /*SQLITE_PRO
f1340 54 4f 43 4f 4c 3f 20 53 51 4c 49 54 45 5f 4d 49  TOCOL? SQLITE_MI
f1350 53 55 53 45 3f 2a 2f 3b 0a 20 20 20 20 20 20 20  SUSE?*/;.       
f1360 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
f1370 20 20 2f 2a 20 74 75 72 6e 20 6f 66 66 20 70 72    /* turn off pr
f1380 6f 78 79 20 6c 6f 63 6b 69 6e 67 20 2d 20 61 6c  oxy locking - al
f1390 72 65 61 64 79 20 6f 66 66 20 2d 20 4e 4f 4f 50  ready off - NOOP
f13a0 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63   */.          rc
f13b0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
f13c0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
f13d0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e  lse{.        con
f13e0 73 74 20 63 68 61 72 20 2a 70 72 6f 78 79 50 61  st char *proxyPa
f13f0 74 68 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  th = (const char
f1400 20 2a 29 70 41 72 67 3b 0a 20 20 20 20 20 20 20   *)pArg;.       
f1410 20 69 66 28 20 69 73 50 72 6f 78 79 53 74 79 6c   if( isProxyStyl
f1420 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  e ){.          p
f1430 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
f1440 78 74 20 2a 70 43 74 78 20 3d 20 0a 20 20 20 20  xt *pCtx = .    
f1450 20 20 20 20 20 20 20 20 28 70 72 6f 78 79 4c 6f          (proxyLo
f1460 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2a 29 70 46  ckingContext*)pF
f1470 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  ile->lockingCont
f1480 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ext;.          i
f1490 66 28 20 21 73 74 72 63 6d 70 28 70 41 72 67 2c  f( !strcmp(pArg,
f14a0 20 22 3a 61 75 74 6f 3a 22 29 20 0a 20 20 20 20   ":auto:") .    
f14b0 20 20 20 20 20 20 20 7c 7c 20 28 70 43 74 78 2d         || (pCtx-
f14c0 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 20 26  >lockProxyPath &
f14d0 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  &.              
f14e0 20 21 73 74 72 6e 63 6d 70 28 70 43 74 78 2d 3e   !strncmp(pCtx->
f14f0 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 2c 20 70  lockProxyPath, p
f1500 72 6f 78 79 50 61 74 68 2c 20 4d 41 58 50 41 54  roxyPath, MAXPAT
f1510 48 4c 45 4e 29 29 0a 20 20 20 20 20 20 20 20 20  HLEN)).         
f1520 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
f1530 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
f1540 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
f1550 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
f1560 3d 20 73 77 69 74 63 68 4c 6f 63 6b 50 72 6f 78  = switchLockProx
f1570 79 50 61 74 68 28 70 46 69 6c 65 2c 20 70 72 6f  yPath(pFile, pro
f1580 78 79 50 61 74 68 29 3b 0a 20 20 20 20 20 20 20  xyPath);.       
f1590 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c     }.        }el
f15a0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a  se{.          /*
f15b0 20 74 75 72 6e 20 6f 6e 20 70 72 6f 78 79 20 66   turn on proxy f
f15c0 69 6c 65 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20  ile locking */. 
f15d0 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 72           rc = pr
f15e0 6f 78 79 54 72 61 6e 73 66 6f 72 6d 55 6e 69 78  oxyTransformUnix
f15f0 46 69 6c 65 28 70 46 69 6c 65 2c 20 70 72 6f 78  File(pFile, prox
f1600 79 50 61 74 68 29 3b 0a 20 20 20 20 20 20 20 20  yPath);.        
f1610 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
f1620 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
f1630 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
f1640 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20        assert( 0 
f1650 29 3b 20 20 2f 2a 20 54 68 65 20 63 61 6c 6c 20  );  /* The call 
f1660 61 73 73 75 72 65 73 20 74 68 61 74 20 6f 6e 6c  assures that onl
f1670 79 20 76 61 6c 69 64 20 6f 70 63 6f 64 65 73 20  y valid opcodes 
f1680 61 72 65 20 73 65 6e 74 20 2a 2f 0a 20 20 20 20  are sent */.    
f1690 7d 0a 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41  }.  }.  /*NOTREA
f16a0 43 48 45 44 2a 2f 0a 20 20 72 65 74 75 72 6e 20  CHED*/.  return 
f16b0 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a  SQLITE_ERROR;.}.
f16c0 0a 2f 2a 0a 2a 2a 20 57 69 74 68 69 6e 20 74 68  ./*.** Within th
f16d0 69 73 20 64 69 76 69 73 69 6f 6e 20 28 74 68 65  is division (the
f16e0 20 70 72 6f 78 79 69 6e 67 20 6c 6f 63 6b 69 6e   proxying lockin
f16f0 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  g implementation
f1700 29 20 74 68 65 20 70 72 6f 63 65 64 75 72 65 73  ) the procedures
f1710 0a 2a 2a 20 61 62 6f 76 65 20 74 68 69 73 20 70  .** above this p
f1720 6f 69 6e 74 20 61 72 65 20 61 6c 6c 20 75 74 69  oint are all uti
f1730 6c 69 74 69 65 73 2e 20 20 54 68 65 20 6c 6f 63  lities.  The loc
f1740 6b 2d 72 65 6c 61 74 65 64 20 6d 65 74 68 6f 64  k-related method
f1750 73 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 78  s of the.** prox
f1760 79 2d 6c 6f 63 6b 69 6e 67 20 73 71 6c 69 74 65  y-locking sqlite
f1770 33 5f 69 6f 5f 6d 65 74 68 6f 64 20 6f 62 6a 65  3_io_method obje
f1780 63 74 20 66 6f 6c 6c 6f 77 2e 0a 2a 2f 0a 0a 0a  ct follow..*/...
f1790 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
f17a0 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74 68 65  ne checks if the
f17b0 72 65 20 69 73 20 61 20 52 45 53 45 52 56 45 44  re is a RESERVED
f17c0 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68   lock held on th
f17d0 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66  e specified.** f
f17e0 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72 20 61  ile by this or a
f17f0 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  ny other process
f1800 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b  . If such a lock
f1810 20 69 73 20 68 65 6c 64 2c 20 73 65 74 20 2a 70   is held, set *p
f1820 52 65 73 4f 75 74 0a 2a 2a 20 74 6f 20 61 20 6e  ResOut.** to a n
f1830 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20 6f 74  on-zero value ot
f1840 68 65 72 77 69 73 65 20 2a 70 52 65 73 4f 75 74  herwise *pResOut
f1850 20 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e   is set to zero.
f1860 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c    The return val
f1870 75 65 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f 20  ue.** is set to 
f1880 53 51 4c 49 54 45 5f 4f 4b 20 75 6e 6c 65 73 73  SQLITE_OK unless
f1890 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63   an I/O error oc
f18a0 63 75 72 73 20 64 75 72 69 6e 67 20 6c 6f 63 6b  curs during lock
f18b0 20 63 68 65 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74   checking..*/.st
f18c0 61 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 68  atic int proxyCh
f18d0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28  eckReservedLock(
f18e0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
f18f0 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 20  , int *pResOut) 
f1900 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  {.  unixFile *pF
f1910 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
f1920 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  )id;.  int rc = 
f1930 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70  proxyTakeConch(p
f1940 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d  File);.  if( rc=
f1950 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
f1960 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f    proxyLockingCo
f1970 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70  ntext *pCtx = (p
f1980 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
f1990 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b  xt *)pFile->lock
f19a0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  ingContext;.    
f19b0 75 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20  unixFile *proxy 
f19c0 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78  = pCtx->lockProx
f19d0 79 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 72  y;.    return pr
f19e0 6f 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 43  oxy->pMethod->xC
f19f0 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
f1a00 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29  ((sqlite3_file*)
f1a10 70 72 6f 78 79 2c 20 70 52 65 73 4f 75 74 29 3b  proxy, pResOut);
f1a20 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
f1a30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20  ;.}../*.** Lock 
f1a40 74 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68  the file with th
f1a50 65 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64  e lock specified
f1a60 20 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f   by parameter lo
f1a70 63 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20  cktype - one.** 
f1a80 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
f1a90 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20  :.**.**     (1) 
f1aa0 53 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20  SHARED_LOCK.**  
f1ab0 20 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f     (2) RESERVED_
f1ac0 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20  LOCK.**     (3) 
f1ad0 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20  PENDING_LOCK.** 
f1ae0 20 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56      (4) EXCLUSIV
f1af0 45 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d  E_LOCK.**.** Som
f1b00 65 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75  etimes when requ
f1b10 65 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20  esting one lock 
f1b20 73 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61  state, additiona
f1b30 6c 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a  l lock states.**
f1b40 20 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e   are inserted in
f1b50 20 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c   between.  The l
f1b60 6f 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69  ocking might fai
f1b70 6c 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20  l on one of the 
f1b80 6c 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74  later.** transit
f1b90 69 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65  ions leaving the
f1ba0 20 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66   lock state diff
f1bb0 65 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20  erent from what 
f1bc0 69 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a  it started but.*
f1bd0 2a 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66  * still short of
f1be0 20 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20   its goal.  The 
f1bf0 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20  following chart 
f1c00 73 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65  shows the allowe
f1c10 64 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73  d.** transitions
f1c20 20 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65   and the inserte
f1c30 64 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73  d intermediate s
f1c40 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  tates:.**.**    
f1c50 55 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52  UNLOCKED -> SHAR
f1c60 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20  ED.**    SHARED 
f1c70 2d 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20  -> RESERVED.**  
f1c80 20 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e    SHARED -> (PEN
f1c90 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49  DING) -> EXCLUSI
f1ca0 56 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45  VE.**    RESERVE
f1cb0 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d  D -> (PENDING) -
f1cc0 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20  > EXCLUSIVE.**  
f1cd0 20 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43    PENDING -> EXC
f1ce0 4c 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69  LUSIVE.**.** Thi
f1cf0 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f  s routine will o
f1d00 6e 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c  nly increase a l
f1d10 6f 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71  ock.  Use the sq
f1d20 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a  lite3OsUnlock().
f1d30 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f  ** routine to lo
f1d40 77 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65  wer a locking le
f1d50 76 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  vel..*/.static i
f1d60 6e 74 20 70 72 6f 78 79 4c 6f 63 6b 28 73 71 6c  nt proxyLock(sql
f1d70 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
f1d80 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20  nt locktype) {. 
f1d90 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
f1da0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
f1db0 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70 72 6f  ;.  int rc = pro
f1dc0 78 79 54 61 6b 65 43 6f 6e 63 68 28 70 46 69 6c  xyTakeConch(pFil
f1dd0 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  e);.  if( rc==SQ
f1de0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70  LITE_OK ){.    p
f1df0 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
f1e00 78 74 20 2a 70 43 74 78 20 3d 20 28 70 72 6f 78  xt *pCtx = (prox
f1e10 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
f1e20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67  *)pFile->locking
f1e30 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 75 6e 69  Context;.    uni
f1e40 78 46 69 6c 65 20 2a 70 72 6f 78 79 20 3d 20 70  xFile *proxy = p
f1e50 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b 0a  Ctx->lockProxy;.
f1e60 20 20 20 20 72 63 20 3d 20 70 72 6f 78 79 2d 3e      rc = proxy->
f1e70 70 4d 65 74 68 6f 64 2d 3e 78 4c 6f 63 6b 28 28  pMethod->xLock((
f1e80 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 70 72  sqlite3_file*)pr
f1e90 6f 78 79 2c 20 6c 6f 63 6b 74 79 70 65 29 3b 0a  oxy, locktype);.
f1ea0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
f1eb0 79 70 65 20 3d 20 70 72 6f 78 79 2d 3e 6c 6f 63  ype = proxy->loc
f1ec0 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20 72 65 74  ktype;.  }.  ret
f1ed0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  urn rc;.}.../*.*
f1ee0 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b  * Lower the lock
f1ef0 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c  ing level on fil
f1f00 65 20 64 65 73 63 72 69 70 74 6f 72 20 70 46 69  e descriptor pFi
f1f10 6c 65 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20  le to locktype. 
f1f20 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73   locktype.** mus
f1f30 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c  t be either NO_L
f1f40 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f  OCK or SHARED_LO
f1f50 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  CK..**.** If the
f1f60 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f   locking level o
f1f70 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72  f the file descr
f1f80 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79  iptor is already
f1f90 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20   at or below.** 
f1fa0 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f  the requested lo
f1fb0 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69  cking level, thi
f1fc0 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
f1fd0 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  o-op..*/.static 
f1fe0 69 6e 74 20 70 72 6f 78 79 55 6e 6c 6f 63 6b 28  int proxyUnlock(
f1ff0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
f2000 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 20  , int locktype) 
f2010 7b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46  {.  unixFile *pF
f2020 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a  ile = (unixFile*
f2030 29 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  )id;.  int rc = 
f2040 70 72 6f 78 79 54 61 6b 65 43 6f 6e 63 68 28 70  proxyTakeConch(p
f2050 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 72 63 3d  File);.  if( rc=
f2060 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
f2070 20 20 70 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f    proxyLockingCo
f2080 6e 74 65 78 74 20 2a 70 43 74 78 20 3d 20 28 70  ntext *pCtx = (p
f2090 72 6f 78 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  roxyLockingConte
f20a0 78 74 20 2a 29 70 46 69 6c 65 2d 3e 6c 6f 63 6b  xt *)pFile->lock
f20b0 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  ingContext;.    
f20c0 75 6e 69 78 46 69 6c 65 20 2a 70 72 6f 78 79 20  unixFile *proxy 
f20d0 3d 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78  = pCtx->lockProx
f20e0 79 3b 0a 20 20 20 20 72 63 20 3d 20 70 72 6f 78  y;.    rc = prox
f20f0 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e 6c  y->pMethod->xUnl
f2100 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69 6c  ock((sqlite3_fil
f2110 65 2a 29 70 72 6f 78 79 2c 20 6c 6f 63 6b 74 79  e*)proxy, lockty
f2120 70 65 29 3b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  pe);.    pFile->
f2130 6c 6f 63 6b 74 79 70 65 20 3d 20 70 72 6f 78 79  locktype = proxy
f2140 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a  ->locktype;.  }.
f2150 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
f2160 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69  /*.** Close a fi
f2170 6c 65 20 74 68 61 74 20 75 73 65 73 20 70 72 6f  le that uses pro
f2180 78 79 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61  xy locks..*/.sta
f2190 74 69 63 20 69 6e 74 20 70 72 6f 78 79 43 6c 6f  tic int proxyClo
f21a0 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  se(sqlite3_file 
f21b0 2a 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64 20  *id) {.  if( id 
f21c0 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20  ){.    unixFile 
f21d0 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69  *pFile = (unixFi
f21e0 6c 65 2a 29 69 64 3b 0a 20 20 20 20 70 72 6f 78  le*)id;.    prox
f21f0 79 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20  yLockingContext 
f2200 2a 70 43 74 78 20 3d 20 28 70 72 6f 78 79 4c 6f  *pCtx = (proxyLo
f2210 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 70  ckingContext *)p
f2220 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
f2230 74 65 78 74 3b 0a 20 20 20 20 75 6e 69 78 46 69  text;.    unixFi
f2240 6c 65 20 2a 6c 6f 63 6b 50 72 6f 78 79 20 3d 20  le *lockProxy = 
f2250 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f 78 79 3b  pCtx->lockProxy;
f2260 0a 20 20 20 20 75 6e 69 78 46 69 6c 65 20 2a 63  .    unixFile *c
f2270 6f 6e 63 68 46 69 6c 65 20 3d 20 70 43 74 78 2d  onchFile = pCtx-
f2280 3e 63 6f 6e 63 68 46 69 6c 65 3b 0a 20 20 20 20  >conchFile;.    
f2290 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
f22a0 4f 4b 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 28  OK;.    .    if(
f22b0 20 6c 6f 63 6b 50 72 6f 78 79 20 29 7b 0a 20 20   lockProxy ){.  
f22c0 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b 50 72 6f      rc = lockPro
f22d0 78 79 2d 3e 70 4d 65 74 68 6f 64 2d 3e 78 55 6e  xy->pMethod->xUn
f22e0 6c 6f 63 6b 28 28 73 71 6c 69 74 65 33 5f 66 69  lock((sqlite3_fi
f22f0 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78 79 2c 20 4e  le*)lockProxy, N
f2300 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69  O_LOCK);.      i
f2310 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
f2320 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 6f  c;.      rc = lo
f2330 63 6b 50 72 6f 78 79 2d 3e 70 4d 65 74 68 6f 64  ckProxy->pMethod
f2340 2d 3e 78 43 6c 6f 73 65 28 28 73 71 6c 69 74 65  ->xClose((sqlite
f2350 33 5f 66 69 6c 65 2a 29 6c 6f 63 6b 50 72 6f 78  3_file*)lockProx
f2360 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  y);.      if( rc
f2370 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
f2380 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
f2390 28 6c 6f 63 6b 50 72 6f 78 79 29 3b 0a 20 20 20  (lockProxy);.   
f23a0 20 20 20 70 43 74 78 2d 3e 6c 6f 63 6b 50 72 6f     pCtx->lockPro
f23b0 78 79 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  xy = 0;.    }.  
f23c0 20 20 69 66 28 20 63 6f 6e 63 68 46 69 6c 65 20    if( conchFile 
f23d0 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 74  ){.      if( pCt
f23e0 78 2d 3e 63 6f 6e 63 68 48 65 6c 64 20 29 7b 0a  x->conchHeld ){.
f23f0 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 72 6f          rc = pro
f2400 78 79 52 65 6c 65 61 73 65 43 6f 6e 63 68 28 70  xyReleaseConch(p
f2410 46 69 6c 65 29 3b 0a 20 20 20 20 20 20 20 20 69  File);.        i
f2420 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
f2430 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  c;.      }.     
f2440 20 72 63 20 3d 20 63 6f 6e 63 68 46 69 6c 65 2d   rc = conchFile-
f2450 3e 70 4d 65 74 68 6f 64 2d 3e 78 43 6c 6f 73 65  >pMethod->xClose
f2460 28 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29  ((sqlite3_file*)
f2470 63 6f 6e 63 68 46 69 6c 65 29 3b 0a 20 20 20 20  conchFile);.    
f2480 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
f2490 6e 20 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69  n rc;.      sqli
f24a0 74 65 33 5f 66 72 65 65 28 63 6f 6e 63 68 46 69  te3_free(conchFi
f24b0 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  le);.    }.    s
f24c0 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 74 78  qlite3_free(pCtx
f24d0 2d 3e 6c 6f 63 6b 50 72 6f 78 79 50 61 74 68 29  ->lockProxyPath)
f24e0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
f24f0 65 65 28 70 43 74 78 2d 3e 63 6f 6e 63 68 46 69  ee(pCtx->conchFi
f2500 6c 65 50 61 74 68 29 3b 0a 20 20 20 20 73 71 6c  lePath);.    sql
f2510 69 74 65 33 5f 66 72 65 65 28 70 43 74 78 2d 3e  ite3_free(pCtx->
f2520 64 62 50 61 74 68 29 3b 0a 20 20 20 20 2f 2a 20  dbPath);.    /* 
f2530 72 65 73 74 6f 72 65 20 74 68 65 20 6f 72 69 67  restore the orig
f2540 69 6e 61 6c 20 6c 6f 63 6b 69 6e 67 20 63 6f 6e  inal locking con
f2550 74 65 78 74 20 61 6e 64 20 70 4d 65 74 68 6f 64  text and pMethod
f2560 20 74 68 65 6e 20 63 6c 6f 73 65 20 69 74 20 2a   then close it *
f2570 2f 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63  /.    pFile->loc
f2580 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20 70 43  kingContext = pC
f2590 74 78 2d 3e 6f 6c 64 4c 6f 63 6b 69 6e 67 43 6f  tx->oldLockingCo
f25a0 6e 74 65 78 74 3b 0a 20 20 20 20 70 46 69 6c 65  ntext;.    pFile
f25b0 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 70 43 74 78  ->pMethod = pCtx
f25c0 2d 3e 70 4f 6c 64 4d 65 74 68 6f 64 3b 0a 20 20  ->pOldMethod;.  
f25d0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
f25e0 43 74 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  Ctx);.    return
f25f0 20 70 46 69 6c 65 2d 3e 70 4d 65 74 68 6f 64 2d   pFile->pMethod-
f2600 3e 78 43 6c 6f 73 65 28 69 64 29 3b 0a 20 20 7d  >xClose(id);.  }
f2610 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
f2620 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 23 65 6e 64 69 66  _OK;.}....#endif
f2630 20 2f 2a 20 64 65 66 69 6e 65 64 28 5f 5f 41 50   /* defined(__AP
f2640 50 4c 45 5f 5f 29 20 26 26 20 53 51 4c 49 54 45  PLE__) && SQLITE
f2650 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
f2660 53 54 59 4c 45 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54  STYLE */./*.** T
f2670 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67  he proxy locking
f2680 20 73 74 79 6c 65 20 69 73 20 69 6e 74 65 6e 64   style is intend
f2690 65 64 20 66 6f 72 20 75 73 65 20 77 69 74 68 20  ed for use with 
f26a0 41 46 50 20 66 69 6c 65 73 79 73 74 65 6d 73 2e  AFP filesystems.
f26b0 0a 2a 2a 20 41 6e 64 20 73 69 6e 63 65 20 41 46  .** And since AF
f26c0 50 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72  P is only suppor
f26d0 74 65 64 20 6f 6e 20 4d 61 63 4f 53 58 2c 20 74  ted on MacOSX, t
f26e0 68 65 20 70 72 6f 78 79 20 6c 6f 63 6b 69 6e 67  he proxy locking
f26f0 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 72 65 73 74   is also.** rest
f2700 72 69 63 74 65 64 20 74 6f 20 4d 61 63 4f 53 58  ricted to MacOSX
f2710 2e 0a 2a 2a 20 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  ..** .**.*******
f2720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
f2730 20 6f 66 20 74 68 65 20 70 72 6f 78 79 20 6c 6f   of the proxy lo
f2740 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ck implementatio
f2750 6e 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n **************
f2760 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  ********.*******
f2770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f2790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f27a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f27b0 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
f27c0 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6f  Initialize the o
f27d0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
f27e0 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a  interface..**.**
f27f0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
f2800 67 69 73 74 65 72 73 20 61 6c 6c 20 56 46 53 20  gisters all VFS 
f2810 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
f2820 66 6f 72 20 75 6e 69 78 2d 6c 69 6b 65 20 6f 70  for unix-like op
f2830 65 72 61 74 69 6e 67 0a 2a 2a 20 73 79 73 74 65  erating.** syste
f2840 6d 73 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ms.  This routin
f2850 65 2c 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74  e, and the sqlit
f2860 65 33 5f 6f 73 5f 65 6e 64 28 29 20 72 6f 75 74  e3_os_end() rout
f2870 69 6e 65 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73  ine that follows
f2880 2c 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 74  ,.** should be t
f2890 68 65 20 6f 6e 6c 79 20 72 6f 75 74 69 6e 65 73  he only routines
f28a0 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 74 68   in this file th
f28b0 61 74 20 61 72 65 20 76 69 73 69 62 6c 65 20 66  at are visible f
f28c0 72 6f 6d 20 6f 74 68 65 72 0a 2a 2a 20 66 69 6c  rom other.** fil
f28d0 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  es..**.** This r
f28e0 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
f28f0 20 6f 6e 63 65 20 64 75 72 69 6e 67 20 53 51 4c   once during SQL
f2900 69 74 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  ite initializati
f2910 6f 6e 20 61 6e 64 20 62 79 20 61 0a 2a 2a 20 73  on and by a.** s
f2920 69 6e 67 6c 65 20 74 68 72 65 61 64 2e 20 20 54  ingle thread.  T
f2930 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
f2940 74 69 6f 6e 20 61 6e 64 20 6d 75 74 65 78 20 73  tion and mutex s
f2950 75 62 73 79 73 74 65 6d 73 20 68 61 76 65 20 6e  ubsystems have n
f2960 6f 74 0a 2a 2a 20 6e 65 63 65 73 73 61 72 69 6c  ot.** necessaril
f2970 79 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a  y been initializ
f2980 65 64 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  ed when this rou
f2990 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20  tine is called, 
f29a0 61 6e 64 20 73 6f 20 74 68 65 79 0a 2a 2a 20 73  and so they.** s
f29b0 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 75 73 65  hould not be use
f29c0 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
f29d0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f   int sqlite3_os_
f29e0 69 6e 69 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f  init(void){ .  /
f29f0 2a 20 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c  * .  ** The foll
f2a00 6f 77 69 6e 67 20 6d 61 63 72 6f 20 64 65 66 69  owing macro defi
f2a10 6e 65 73 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a  nes an initializ
f2a20 65 72 20 66 6f 72 20 61 6e 20 73 71 6c 69 74 65  er for an sqlite
f2a30 33 5f 76 66 73 20 6f 62 6a 65 63 74 2e 0a 20 20  3_vfs object..  
f2a40 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74  ** The name of t
f2a50 68 65 20 56 46 53 20 69 73 20 4e 41 4d 45 2e 20  he VFS is NAME. 
f2a60 20 54 68 65 20 70 41 70 70 44 61 74 61 20 69 73   The pAppData is
f2a70 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
f2a80 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 74 6f 20  pointer.  ** to 
f2a90 74 68 65 20 22 66 69 6e 64 65 72 22 20 66 75 6e  the "finder" fun
f2aa0 63 74 69 6f 6e 2e 20 20 28 70 41 70 70 44 61 74  ction.  (pAppDat
f2ab0 61 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  a is a pointer t
f2ac0 6f 20 61 20 70 6f 69 6e 74 65 72 20 62 65 63 61  o a pointer beca
f2ad0 75 73 65 0a 20 20 2a 2a 20 73 69 6c 6c 79 20 43  use.  ** silly C
f2ae0 39 30 20 72 75 6c 65 73 20 70 72 6f 68 69 62 69  90 rules prohibi
f2af0 74 20 61 20 76 6f 69 64 2a 20 66 72 6f 6d 20 62  t a void* from b
f2b00 65 69 6e 67 20 63 61 73 74 20 74 6f 20 61 20 66  eing cast to a f
f2b10 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72 0a  unction pointer.
f2b20 20 20 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 68    ** and so we h
f2b30 61 76 65 20 74 6f 20 67 6f 20 74 68 72 6f 75 67  ave to go throug
f2b40 68 20 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61  h the intermedia
f2b50 74 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 76  te pointer to av
f2b60 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a  oid problems.  *
f2b70 2a 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67  * when compiling
f2b80 20 77 69 74 68 20 2d 70 65 64 61 6e 74 69 63 2d   with -pedantic-
f2b90 65 72 72 6f 72 73 20 6f 6e 20 47 43 43 2e 29 0a  errors on GCC.).
f2ba0 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 46 49    **.  ** The FI
f2bb0 4e 44 45 52 20 70 61 72 61 6d 65 74 65 72 20 74  NDER parameter t
f2bc0 6f 20 74 68 69 73 20 6d 61 63 72 6f 20 69 73 20  o this macro is 
f2bd0 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
f2be0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 20  pointer to the. 
f2bf0 20 2a 2a 20 66 69 6e 64 65 72 2d 66 75 6e 63 74   ** finder-funct
f2c00 69 6f 6e 2e 20 20 54 68 65 20 66 69 6e 64 65 72  ion.  The finder
f2c10 2d 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e  -function return
f2c20 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
f2c30 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 69  he.  ** sqlite_i
f2c40 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74  o_methods object
f2c50 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
f2c60 20 74 68 65 20 64 65 73 69 72 65 64 20 6c 6f 63   the desired loc
f2c70 6b 69 6e 67 0a 20 20 2a 2a 20 62 65 68 61 76 69  king.  ** behavi
f2c80 6f 72 73 2e 20 20 53 65 65 20 74 68 65 20 64 69  ors.  See the di
f2c90 76 69 73 69 6f 6e 20 61 62 6f 76 65 20 74 68 61  vision above tha
f2ca0 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 49  t contains the I
f2cb0 4f 4d 45 54 48 4f 44 53 0a 20 20 2a 2a 20 6d 61  OMETHODS.  ** ma
f2cc0 63 72 6f 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  cro for addition
f2cd0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20   information on 
f2ce0 66 69 6e 64 65 72 2d 66 75 6e 63 74 69 6f 6e 73  finder-functions
f2cf0 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 73 74  ..  **.  ** Most
f2d00 20 66 69 6e 64 65 72 73 20 73 69 6d 70 6c 79 20   finders simply 
f2d10 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  return a pointer
f2d20 20 74 6f 20 61 20 66 69 78 65 64 20 73 71 6c 69   to a fixed sqli
f2d30 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 0a 20  te3_io_methods. 
f2d40 20 2a 2a 20 6f 62 6a 65 63 74 2e 20 20 42 75 74   ** object.  But
f2d50 20 74 68 65 20 22 61 75 74 6f 6c 6f 63 6b 49 6f   the "autolockIo
f2d60 46 69 6e 64 65 72 22 20 61 76 61 69 6c 61 62 6c  Finder" availabl
f2d70 65 20 6f 6e 20 4d 61 63 4f 53 58 20 64 6f 65 73  e on MacOSX does
f2d80 20 61 20 6c 69 74 74 6c 65 0a 20 20 2a 2a 20 6d   a little.  ** m
f2d90 6f 72 65 20 74 68 61 6e 20 74 68 61 74 3b 20 69  ore than that; i
f2da0 74 20 6c 6f 6f 6b 73 20 61 74 20 74 68 65 20 66  t looks at the f
f2db0 69 6c 65 73 79 73 74 65 6d 20 74 79 70 65 20 74  ilesystem type t
f2dc0 68 61 74 20 68 6f 73 74 73 20 74 68 65 20 0a 20  hat hosts the . 
f2dd0 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c   ** database fil
f2de0 65 20 61 6e 64 20 74 72 69 65 73 20 74 6f 20 63  e and tries to c
f2df0 68 6f 6f 73 65 20 61 6e 20 6c 6f 63 6b 69 6e 67  hoose an locking
f2e00 20 6d 65 74 68 6f 64 20 61 70 70 72 6f 70 72 69   method appropri
f2e10 61 74 65 20 66 6f 72 0a 20 20 2a 2a 20 74 68 61  ate for.  ** tha
f2e20 74 20 66 69 6c 65 73 79 73 74 65 6d 20 74 69 6d  t filesystem tim
f2e30 65 2e 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e  e..  */.  #defin
f2e40 65 20 55 4e 49 58 56 46 53 28 56 46 53 4e 41 4d  e UNIXVFS(VFSNAM
f2e50 45 2c 20 46 49 4e 44 45 52 29 20 7b 20 20 20 20  E, FINDER) {    
f2e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2e70 20 20 20 20 5c 0a 20 20 20 20 31 2c 20 20 20 20      \.    1,    
f2e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2e90 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 20 20  /* iVersion */  
f2ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2eb0 20 20 5c 0a 20 20 20 20 73 69 7a 65 6f 66 28 75    \.    sizeof(u
f2ec0 6e 69 78 46 69 6c 65 29 2c 20 20 20 20 20 2f 2a  nixFile),     /*
f2ed0 20 73 7a 4f 73 46 69 6c 65 20 2a 2f 20 20 20 20   szOsFile */    
f2ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f2ef0 5c 0a 20 20 20 20 4d 41 58 5f 50 41 54 48 4e 41  \.    MAX_PATHNA
f2f00 4d 45 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 6d  ME,         /* m
f2f10 78 50 61 74 68 6e 61 6d 65 20 2a 2f 20 20 20 20  xPathname */    
f2f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
f2f30 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20      0,          
f2f40 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65            /* pNe
f2f50 78 74 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  xt */           
f2f60 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
f2f70 20 20 56 46 53 4e 41 4d 45 2c 20 20 20 20 20 20    VFSNAME,      
f2f80 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e 61 6d 65          /* zName
f2f90 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
f2fa0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
f2fb0 28 76 6f 69 64 2a 29 26 46 49 4e 44 45 52 2c 20  (void*)&FINDER, 
f2fc0 20 20 20 20 20 20 2f 2a 20 70 41 70 70 44 61 74        /* pAppDat
f2fd0 61 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  a */            
f2fe0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e          \.    un
f2ff0 69 78 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20  ixOpen,         
f3000 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f 20      /* xOpen */ 
f3010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3020 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78        \.    unix
f3030 44 65 6c 65 74 65 2c 20 20 20 20 20 20 20 20 20  Delete,         
f3040 20 20 2f 2a 20 78 44 65 6c 65 74 65 20 2a 2f 20    /* xDelete */ 
f3050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3060 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 41 63      \.    unixAc
f3070 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 20 20  cess,           
f3080 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f 20 20 20  /* xAccess */   
f3090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f30a0 20 20 5c 0a 20 20 20 20 75 6e 69 78 46 75 6c 6c    \.    unixFull
f30b0 50 61 74 68 6e 61 6d 65 2c 20 20 20 20 20 2f 2a  Pathname,     /*
f30c0 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 20 2a   xFullPathname *
f30d0 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
f30e0 5c 0a 20 20 20 20 75 6e 69 78 44 6c 4f 70 65 6e  \.    unixDlOpen
f30f0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78  ,           /* x
f3100 44 6c 4f 70 65 6e 20 2a 2f 20 20 20 20 20 20 20  DlOpen */       
f3110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
f3120 20 20 20 20 75 6e 69 78 44 6c 45 72 72 6f 72 2c      unixDlError,
f3130 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c            /* xDl
f3140 45 72 72 6f 72 20 2a 2f 20 20 20 20 20 20 20 20  Error */        
f3150 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
f3160 20 20 75 6e 69 78 44 6c 53 79 6d 2c 20 20 20 20    unixDlSym,    
f3170 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 79          /* xDlSy
f3180 6d 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  m */            
f3190 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
f31a0 75 6e 69 78 44 6c 43 6c 6f 73 65 2c 20 20 20 20  unixDlClose,    
f31b0 20 20 20 20 20 20 2f 2a 20 78 44 6c 43 6c 6f 73        /* xDlClos
f31c0 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20  e */            
f31d0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e          \.    un
f31e0 69 78 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20  ixRandomness,   
f31f0 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65      /* xRandomne
f3200 73 73 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  ss */           
f3210 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78        \.    unix
f3220 53 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 20  Sleep,          
f3230 20 20 2f 2a 20 78 53 6c 65 65 70 20 2a 2f 20 20    /* xSleep */  
f3240 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f3250 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 43 75      \.    unixCu
f3260 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 20 20 20  rrentTime,      
f3270 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20  /* xCurrentTime 
f3280 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
f3290 20 20 5c 0a 20 20 20 20 75 6e 69 78 47 65 74 4c    \.    unixGetL
f32a0 61 73 74 45 72 72 6f 72 20 20 20 20 20 20 2f 2a  astError      /*
f32b0 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a   xGetLastError *
f32c0 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
f32d0 5c 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  \.  }..  /*.  **
f32e0 20 41 6c 6c 20 64 65 66 61 75 6c 74 20 56 46 53   All default VFS
f32f0 65 73 20 66 6f 72 20 75 6e 69 78 20 61 72 65 20  es for unix are 
f3300 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65  contained in the
f3310 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79   following array
f3320 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65  ..  **.  ** Note
f3330 20 74 68 61 74 20 74 68 65 20 73 71 6c 69 74 65   that the sqlite
f3340 33 5f 76 66 73 2e 70 4e 65 78 74 20 66 69 65 6c  3_vfs.pNext fiel
f3350 64 20 6f 66 20 74 68 65 20 56 46 53 20 6f 62 6a  d of the VFS obj
f3360 65 63 74 20 69 73 20 6d 6f 64 69 66 69 65 64 0a  ect is modified.
f3370 20 20 2a 2a 20 62 79 20 74 68 65 20 53 51 4c 69    ** by the SQLi
f3380 74 65 20 63 6f 72 65 20 77 68 65 6e 20 74 68 65  te core when the
f3390 20 56 46 53 20 69 73 20 72 65 67 69 73 74 65 72   VFS is register
f33a0 65 64 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c 6c  ed.  So the foll
f33b0 6f 77 69 6e 67 0a 20 20 2a 2a 20 61 72 72 61 79  owing.  ** array
f33c0 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 73 74   cannot be const
f33d0 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20  ..  */.  static 
f33e0 73 71 6c 69 74 65 33 5f 76 66 73 20 61 56 66 73  sqlite3_vfs aVfs
f33f0 5b 5d 20 3d 20 7b 0a 23 69 66 20 53 51 4c 49 54  [] = {.#if SQLIT
f3400 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
f3410 5f 53 54 59 4c 45 20 26 26 20 28 4f 53 5f 56 58  _STYLE && (OS_VX
f3420 57 4f 52 4b 53 20 7c 7c 20 64 65 66 69 6e 65 64  WORKS || defined
f3430 28 5f 5f 41 50 50 4c 45 5f 5f 29 29 0a 20 20 20  (__APPLE__)).   
f3440 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 22 2c   UNIXVFS("unix",
f3450 20 20 20 20 20 20 20 20 20 20 61 75 74 6f 6c 6f            autolo
f3460 63 6b 49 6f 46 69 6e 64 65 72 20 29 2c 0a 23 65  ckIoFinder ),.#e
f3470 6c 73 65 0a 20 20 20 20 55 4e 49 58 56 46 53 28  lse.    UNIXVFS(
f3480 22 75 6e 69 78 22 2c 20 20 20 20 20 20 20 20 20  "unix",         
f3490 20 70 6f 73 69 78 49 6f 46 69 6e 64 65 72 20 29   posixIoFinder )
f34a0 2c 0a 23 65 6e 64 69 66 0a 20 20 20 20 55 4e 49  ,.#endif.    UNI
f34b0 58 56 46 53 28 22 75 6e 69 78 2d 6e 6f 6e 65 22  XVFS("unix-none"
f34c0 2c 20 20 20 20 20 6e 6f 6c 6f 63 6b 49 6f 46 69  ,     nolockIoFi
f34d0 6e 64 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58  nder ),.    UNIX
f34e0 56 46 53 28 22 75 6e 69 78 2d 64 6f 74 66 69 6c  VFS("unix-dotfil
f34f0 65 22 2c 20 20 64 6f 74 6c 6f 63 6b 49 6f 46 69  e",  dotlockIoFi
f3500 6e 64 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58  nder ),.    UNIX
f3510 56 46 53 28 22 75 6e 69 78 2d 77 66 6c 22 2c 20  VFS("unix-wfl", 
f3520 20 20 20 20 20 70 6f 73 69 78 57 66 6c 49 6f 46       posixWflIoF
f3530 69 6e 64 65 72 20 29 2c 0a 23 69 66 20 4f 53 5f  inder ),.#if OS_
f3540 56 58 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49 58  VXWORKS.    UNIX
f3550 56 46 53 28 22 75 6e 69 78 2d 6e 61 6d 65 64 73  VFS("unix-nameds
f3560 65 6d 22 2c 20 73 65 6d 49 6f 46 69 6e 64 65 72  em", semIoFinder
f3570 20 29 2c 0a 23 65 6e 64 69 66 0a 23 69 66 20 53   ),.#endif.#if S
f3580 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
f3590 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 20 20 55  KING_STYLE.    U
f35a0 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 70 6f 73  NIXVFS("unix-pos
f35b0 69 78 22 2c 20 20 20 20 70 6f 73 69 78 49 6f 46  ix",    posixIoF
f35c0 69 6e 64 65 72 20 29 2c 0a 23 69 66 20 21 4f 53  inder ),.#if !OS
f35d0 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 55 4e 49  _VXWORKS.    UNI
f35e0 58 56 46 53 28 22 75 6e 69 78 2d 66 6c 6f 63 6b  XVFS("unix-flock
f35f0 22 2c 20 20 20 20 66 6c 6f 63 6b 49 6f 46 69 6e  ",    flockIoFin
f3600 64 65 72 20 29 2c 0a 23 65 6e 64 69 66 0a 23 65  der ),.#endif.#e
f3610 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
f3620 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
f3630 54 59 4c 45 20 26 26 20 64 65 66 69 6e 65 64 28  TYLE && defined(
f3640 5f 5f 41 50 50 4c 45 5f 5f 29 0a 20 20 20 20 55  __APPLE__).    U
f3650 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 61 66 70  NIXVFS("unix-afp
f3660 22 2c 20 20 20 20 20 20 61 66 70 49 6f 46 69 6e  ",      afpIoFin
f3670 64 65 72 20 29 2c 0a 20 20 20 20 55 4e 49 58 56  der ),.    UNIXV
f3680 46 53 28 22 75 6e 69 78 2d 70 72 6f 78 79 22 2c  FS("unix-proxy",
f3690 20 20 20 20 70 72 6f 78 79 49 6f 46 69 6e 64 65      proxyIoFinde
f36a0 72 20 29 2c 0a 23 65 6e 64 69 66 0a 20 20 7d 3b  r ),.#endif.  };
f36b0 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
f36c0 69 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  i;          /* L
f36d0 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a  oop counter */..
f36e0 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 61 6c    /* Register al
f36f0 6c 20 56 46 53 65 73 20 64 65 66 69 6e 65 64 20  l VFSes defined 
f3700 69 6e 20 74 68 65 20 61 56 66 73 5b 5d 20 61 72  in the aVfs[] ar
f3710 72 61 79 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  ray */.  for(i=0
f3720 3b 20 69 3c 28 73 69 7a 65 6f 66 28 61 56 66 73  ; i<(sizeof(aVfs
f3730 29 2f 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33  )/sizeof(sqlite3
f3740 5f 76 66 73 29 29 3b 20 69 2b 2b 29 7b 0a 20 20  _vfs)); i++){.  
f3750 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65    sqlite3_vfs_re
f3760 67 69 73 74 65 72 28 26 61 56 66 73 5b 69 5d 2c  gister(&aVfs[i],
f3770 20 69 3d 3d 30 29 3b 0a 20 20 7d 0a 20 20 72 65   i==0);.  }.  re
f3780 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
f3790 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f  .}../*.** Shutdo
f37a0 77 6e 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67  wn the operating
f37b0 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61 63   system interfac
f37c0 65 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 20 6f 70  e..**.** Some op
f37d0 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20  erating systems 
f37e0 6d 69 67 68 74 20 6e 65 65 64 20 74 6f 20 64 6f  might need to do
f37f0 20 73 6f 6d 65 20 63 6c 65 61 6e 75 70 20 69 6e   some cleanup in
f3800 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 0a 2a   this routine,.*
f3810 2a 20 74 6f 20 72 65 6c 65 61 73 65 20 64 79 6e  * to release dyn
f3820 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74  amically allocat
f3830 65 64 20 6f 62 6a 65 63 74 73 2e 20 20 42 75 74  ed objects.  But
f3840 20 6e 6f 74 20 6f 6e 20 75 6e 69 78 2e 0a 2a 2a   not on unix..**
f3850 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
f3860 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 75 6e 69   a no-op for uni
f3870 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  x..*/.SQLITE_API
f3880 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f   int sqlite3_os_
f3890 65 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20 72 65  end(void){ .  re
f38a0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
f38b0 0a 7d 0a 20 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .}. .#endif /* S
f38c0 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 2a 2f  QLITE_OS_UNIX */
f38d0 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
f38e0 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 75 6e 69 78  * End of os_unix
f38f0 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
f3900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3920 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
f3930 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f  * Begin file os_
f3940 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  win.c **********
f3950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3970 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79  /./*.** 2004 May
f3980 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   22.**.** The au
f3990 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
f39a0 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
f39b0 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
f39c0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
f39d0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
f39e0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
f39f0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
f3a00 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
f3a10 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
f3a20 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
f3a30 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
f3a40 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
f3a50 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
f3a60 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
f3a70 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
f3a80 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
f3a90 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
f3aa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3ab0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3ac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3ae0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
f3af0 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
f3b00 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 73 70   code that is sp
f3b10 65 63 69 66 69 63 20 74 6f 20 77 69 6e 64 6f 77  ecific to window
f3b20 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  s..*/.#if SQLITE
f3b30 5f 4f 53 5f 57 49 4e 20 20 20 20 20 20 20 20 20  _OS_WIN         
f3b40 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 66 69        /* This fi
f3b50 6c 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 77  le is used for w
f3b60 69 6e 64 6f 77 73 20 6f 6e 6c 79 20 2a 2f 0a 0a  indows only */..
f3b70 0a 2f 2a 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62  ./*.** A Note Ab
f3b80 6f 75 74 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63  out Memory Alloc
f3b90 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69  ation:.**.** Thi
f3ba0 73 20 64 72 69 76 65 72 20 75 73 65 73 20 6d 61  s driver uses ma
f3bb0 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 20 64 69  lloc()/free() di
f3bc0 72 65 63 74 6c 79 20 72 61 74 68 65 72 20 74 68  rectly rather th
f3bd0 61 6e 20 67 6f 69 6e 67 20 74 68 72 6f 75 67 68  an going through
f3be0 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 2d 77  .** the SQLite-w
f3bf0 72 61 70 70 65 72 73 20 73 71 6c 69 74 65 33 5f  rappers sqlite3_
f3c00 6d 61 6c 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33  malloc()/sqlite3
f3c10 5f 66 72 65 65 28 29 2e 20 20 54 68 6f 73 65 20  _free().  Those 
f3c20 77 72 61 70 70 65 72 73 0a 2a 2a 20 61 72 65 20  wrappers.** are 
f3c30 64 65 73 69 67 6e 65 64 20 66 6f 72 20 75 73 65  designed for use
f3c40 20 6f 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73   on embedded sys
f3c50 74 65 6d 73 20 77 68 65 72 65 20 6d 65 6d 6f 72  tems where memor
f3c60 79 20 69 73 20 73 63 61 72 63 65 20 61 6e 64 0a  y is scarce and.
f3c70 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72  ** malloc failur
f3c80 65 73 20 68 61 70 70 65 6e 20 66 72 65 71 75 65  es happen freque
f3c90 6e 74 6c 79 2e 20 20 57 69 6e 33 32 20 64 6f 65  ntly.  Win32 doe
f3ca0 73 20 6e 6f 74 20 74 79 70 69 63 61 6c 6c 79 20  s not typically 
f3cb0 72 75 6e 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64  run on.** embedd
f3cc0 65 64 20 73 79 73 74 65 6d 73 2c 20 61 6e 64 20  ed systems, and 
f3cd0 77 68 65 6e 20 69 74 20 64 6f 65 73 20 74 68 65  when it does the
f3ce0 20 64 65 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d   developers norm
f3cf0 61 6c 6c 79 20 68 61 76 65 20 62 69 67 67 65 72  ally have bigger
f3d00 0a 2a 2a 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20  .** problems to 
f3d10 77 6f 72 72 79 20 61 62 6f 75 74 20 74 68 61 6e  worry about than
f3d20 20 72 75 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20   running out of 
f3d30 6d 65 6d 6f 72 79 2e 20 20 53 6f 20 74 68 65 72  memory.  So ther
f3d40 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f  e is not.** a co
f3d50 6d 70 65 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f  mpelling need to
f3d60 20 75 73 65 20 74 68 65 20 77 72 61 70 70 65 72   use the wrapper
f3d70 73 2e 0a 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65  s..**.** But the
f3d80 72 65 20 69 73 20 61 20 67 6f 6f 64 20 72 65 61  re is a good rea
f3d90 73 6f 6e 20 74 6f 20 6e 6f 74 20 75 73 65 20 74  son to not use t
f3da0 68 65 20 77 72 61 70 70 65 72 73 2e 20 20 49 66  he wrappers.  If
f3db0 20 77 65 20 75 73 65 20 74 68 65 0a 2a 2a 20 77   we use the.** w
f3dc0 72 61 70 70 65 72 73 20 74 68 65 6e 20 77 65 20  rappers then we 
f3dd0 77 69 6c 6c 20 67 65 74 20 73 69 6d 75 6c 61 74  will get simulat
f3de0 65 64 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c  ed malloc() fail
f3df0 75 72 65 73 20 77 69 74 68 69 6e 20 74 68 69 73  ures within this
f3e00 0a 2a 2a 20 64 72 69 76 65 72 2e 20 20 41 6e 64  .** driver.  And
f3e10 20 74 68 61 74 20 63 61 75 73 65 73 20 61 6c 6c   that causes all
f3e20 20 6b 69 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65   kinds of proble
f3e30 6d 73 20 66 6f 72 20 6f 75 72 20 74 65 73 74 73  ms for our tests
f3e40 2e 20 20 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65  .  We.** could e
f3e50 6e 68 61 6e 63 65 20 53 51 4c 69 74 65 20 74 6f  nhance SQLite to
f3e60 20 64 65 61 6c 20 77 69 74 68 20 73 69 6d 75 6c   deal with simul
f3e70 61 74 65 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c  ated malloc fail
f3e80 75 72 65 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74  ures within.** t
f3e90 68 65 20 4f 53 20 64 72 69 76 65 72 2c 20 62 75  he OS driver, bu
f3ea0 74 20 74 68 65 20 63 6f 64 65 20 74 6f 20 64 65  t the code to de
f3eb0 61 6c 20 77 69 74 68 20 74 68 6f 73 65 20 66 61  al with those fa
f3ec0 69 6c 75 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a  ilure would not.
f3ed0 2a 2a 20 62 65 20 65 78 65 72 63 69 73 65 64 20  ** be exercised 
f3ee0 6f 6e 20 4c 69 6e 75 78 20 28 77 68 69 63 68 20  on Linux (which 
f3ef0 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f  does not need to
f3f00 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65   malloc() in the
f3f10 20 64 72 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20   driver).** and 
f3f20 73 6f 20 77 65 20 77 6f 75 6c 64 20 68 61 76 65  so we would have
f3f30 20 64 69 66 66 69 63 75 6c 74 79 20 77 72 69 74   difficulty writ
f3f40 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65 73  ing coverage tes
f3f50 74 73 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63  ts for that.** c
f3f60 6f 64 65 2e 20 20 42 65 74 74 65 72 20 74 6f 20  ode.  Better to 
f3f70 6c 65 61 76 65 20 74 68 65 20 63 6f 64 65 20 6f  leave the code o
f3f80 75 74 2c 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a  ut, we think..**
f3f90 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 20 6f 66  .** The point of
f3fa0 20 74 68 69 73 20 64 69 73 63 75 73 73 69 6f 6e   this discussion
f3fb0 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20   is as follows: 
f3fc0 20 57 68 65 6e 20 63 72 65 61 74 69 6e 67 20 61   When creating a
f3fd0 20 6e 65 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72   new.** OS layer
f3fe0 20 66 6f 72 20 61 6e 20 65 6d 62 65 64 64 65 64   for an embedded
f3ff0 20 73 79 73 74 65 6d 2c 20 69 66 20 79 6f 75 20   system, if you 
f4000 75 73 65 20 74 68 69 73 20 66 69 6c 65 20 61 73  use this file as
f4010 20 61 6e 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20   an example,.** 
f4020 61 76 6f 69 64 20 74 68 65 20 75 73 65 20 6f 66  avoid the use of
f4030 20 6d 61 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29   malloc()/free()
f4040 2e 20 20 54 68 6f 73 65 20 72 6f 75 74 69 6e 65  .  Those routine
f4050 73 20 77 6f 72 6b 20 6f 6b 20 6f 6e 20 77 69 6e  s work ok on win
f4060 64 6f 77 73 0a 2a 2a 20 64 65 73 6b 74 6f 70 73  dows.** desktops
f4070 20 62 75 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c   but not so well
f4080 20 69 6e 20 65 6d 62 65 64 64 65 64 20 73 79 73   in embedded sys
f4090 74 65 6d 73 2e 0a 2a 2f 0a 0a 23 69 6e 63 6c 75  tems..*/..#inclu
f40a0 64 65 20 3c 77 69 6e 62 61 73 65 2e 68 3e 0a 0a  de <winbase.h>..
f40b0 23 69 66 64 65 66 20 5f 5f 43 59 47 57 49 4e 5f  #ifdef __CYGWIN_
f40c0 5f 0a 23 20 69 6e 63 6c 75 64 65 20 3c 73 79 73  _.# include <sys
f40d0 2f 63 79 67 77 69 6e 2e 68 3e 0a 23 65 6e 64 69  /cygwin.h>.#endi
f40e0 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20  f../*.** Macros 
f40f0 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  used to determin
f4100 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  e whether or not
f4110 20 74 6f 20 75 73 65 20 74 68 72 65 61 64 73 2e   to use threads.
f4120 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
f4130 54 48 52 45 41 44 53 41 46 45 29 20 26 26 20 54  THREADSAFE) && T
f4140 48 52 45 41 44 53 41 46 45 0a 23 20 64 65 66 69  HREADSAFE.# defi
f4150 6e 65 20 53 51 4c 49 54 45 5f 57 33 32 5f 54 48  ne SQLITE_W32_TH
f4160 52 45 41 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a  READS 1.#endif..
f4170 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f  /*.** Include co
f4180 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f  de that is commo
f4190 6e 20 74 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20  n to all os_*.c 
f41a0 66 69 6c 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  files.*/./******
f41b0 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
f41c0 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20   os_common.h in 
f41d0 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73  the middle of os
f41e0 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  _win.c *********
f41f0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
f4200 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
f4210 69 6c 65 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20  ile os_common.h 
f4220 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4240 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
f4250 30 30 34 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a  004 May 22.**.**
f4260 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
f4270 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
f4280 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
f4290 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
f42a0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
f42b0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
f42c0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
f42d0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
f42e0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
f42f0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
f4300 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
f4310 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
f4320 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
f4330 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
f4340 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
f4350 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
f4360 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
f4370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f43a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f43b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
f43c0 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
f43d0 6f 6e 74 61 69 6e 73 20 6d 61 63 72 6f 73 20 61  ontains macros a
f43e0 6e 64 20 61 20 6c 69 74 74 6c 65 20 62 69 74 20  nd a little bit 
f43f0 6f 66 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  of code that is 
f4400 63 6f 6d 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c  common to.** all
f4410 20 6f 66 20 74 68 65 20 70 6c 61 74 66 6f 72 6d   of the platform
f4420 2d 73 70 65 63 69 66 69 63 20 66 69 6c 65 73 20  -specific files 
f4430 28 6f 73 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20  (os_*.c) and is 
f4440 23 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74  #included into t
f4450 68 6f 73 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a  hose.** files..*
f4460 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 73  *.** This file s
f4470 68 6f 75 6c 64 20 62 65 20 23 69 6e 63 6c 75 64  hould be #includ
f4480 65 64 20 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63  ed by the os_*.c
f4490 20 66 69 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74   files only.  It
f44a0 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e   is not a.** gen
f44b0 65 72 61 6c 20 70 75 72 70 6f 73 65 20 68 65 61  eral purpose hea
f44c0 64 65 72 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  der file..**.** 
f44d0 24 49 64 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68  $Id: os_common.h
f44e0 2c 76 20 31 2e 33 38 20 32 30 30 39 2f 30 32 2f  ,v 1.38 2009/02/
f44f0 32 34 20 31 38 3a 34 30 3a 35 30 20 64 61 6e 69  24 18:40:50 dani
f4500 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f  elk1977 Exp $.*/
f4510 0a 23 69 66 6e 64 65 66 20 5f 4f 53 5f 43 4f 4d  .#ifndef _OS_COM
f4520 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f  MON_H_.#define _
f4530 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 0a 2f 2a  OS_COMMON_H_../*
f4540 0a 2a 2a 20 41 74 20 6c 65 61 73 74 20 74 77 6f  .** At least two
f4550 20 62 75 67 73 20 68 61 76 65 20 73 6c 69 70 70   bugs have slipp
f4560 65 64 20 69 6e 20 62 65 63 61 75 73 65 20 77 65  ed in because we
f4570 20 63 68 61 6e 67 65 64 20 74 68 65 20 4d 45 4d   changed the MEM
f4580 4f 52 59 5f 44 45 42 55 47 0a 2a 2a 20 6d 61 63  ORY_DEBUG.** mac
f4590 72 6f 20 74 6f 20 53 51 4c 49 54 45 5f 44 45 42  ro to SQLITE_DEB
f45a0 55 47 20 61 6e 64 20 73 6f 6d 65 20 6f 6c 64 65  UG and some olde
f45b0 72 20 6d 61 6b 65 66 69 6c 65 73 20 68 61 76 65  r makefiles have
f45c0 20 6e 6f 74 20 79 65 74 20 6d 61 64 65 20 74 68   not yet made th
f45d0 65 0a 2a 2a 20 73 77 69 74 63 68 2e 20 20 54 68  e.** switch.  Th
f45e0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65  e following code
f45f0 20 73 68 6f 75 6c 64 20 63 61 74 63 68 20 74 68   should catch th
f4600 69 73 20 70 72 6f 62 6c 65 6d 20 61 74 20 63 6f  is problem at co
f4610 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a 2f 0a 23  mpile-time..*/.#
f4620 69 66 64 65 66 20 4d 45 4d 4f 52 59 5f 44 45 42  ifdef MEMORY_DEB
f4630 55 47 0a 23 20 65 72 72 6f 72 20 22 54 68 65 20  UG.# error "The 
f4640 4d 45 4d 4f 52 59 5f 44 45 42 55 47 20 6d 61 63  MEMORY_DEBUG mac
f4650 72 6f 20 69 73 20 6f 62 73 6f 6c 65 74 65 2e 20  ro is obsolete. 
f4660 20 55 73 65 20 53 51 4c 49 54 45 5f 44 45 42 55   Use SQLITE_DEBU
f4670 47 20 69 6e 73 74 65 61 64 2e 22 0a 23 65 6e 64  G instead.".#end
f4680 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
f4690 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50  E_DEBUG.SQLITE_P
f46a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
f46b0 65 33 4f 53 54 72 61 63 65 20 3d 20 30 3b 0a 23  e3OSTrace = 0;.#
f46c0 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28  define OSTRACE1(
f46d0 58 29 20 20 20 20 20 20 20 20 20 69 66 28 20 73  X)         if( s
f46e0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
f46f0 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
f4700 74 66 28 58 29 0a 23 64 65 66 69 6e 65 20 4f 53  tf(X).#define OS
f4710 54 52 41 43 45 32 28 58 2c 59 29 20 20 20 20 20  TRACE2(X,Y)     
f4720 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54    if( sqlite3OST
f4730 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65  race ) sqlite3De
f4740 62 75 67 50 72 69 6e 74 66 28 58 2c 59 29 0a 23  bugPrintf(X,Y).#
f4750 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 33 28  define OSTRACE3(
f4760 58 2c 59 2c 5a 29 20 20 20 20 20 69 66 28 20 73  X,Y,Z)     if( s
f4770 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
f4780 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
f4790 74 66 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e  tf(X,Y,Z).#defin
f47a0 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a  e OSTRACE4(X,Y,Z
f47b0 2c 41 29 20 20 20 69 66 28 20 73 71 6c 69 74 65  ,A)   if( sqlite
f47c0 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74  3OSTrace ) sqlit
f47d0 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c  e3DebugPrintf(X,
f47e0 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f  Y,Z,A).#define O
f47f0 53 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c  STRACE5(X,Y,Z,A,
f4800 42 29 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53  B) if( sqlite3OS
f4810 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44  Trace ) sqlite3D
f4820 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a  ebugPrintf(X,Y,Z
f4830 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,A,B).#define OS
f4840 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42  TRACE6(X,Y,Z,A,B
f4850 2c 43 29 20 5c 0a 20 20 20 20 69 66 28 73 71 6c  ,C) \.    if(sql
f4860 69 74 65 33 4f 53 54 72 61 63 65 29 20 73 71 6c  ite3OSTrace) sql
f4870 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
f4880 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65  X,Y,Z,A,B,C).#de
f4890 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c  fine OSTRACE7(X,
f48a0 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 20 5c 0a 20  Y,Z,A,B,C,D) \. 
f48b0 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54     if(sqlite3OST
f48c0 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62  race) sqlite3Deb
f48d0 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41  ugPrintf(X,Y,Z,A
f48e0 2c 42 2c 43 2c 44 29 0a 23 65 6c 73 65 0a 23 64  ,B,C,D).#else.#d
f48f0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 31 28 58  efine OSTRACE1(X
f4900 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
f4910 45 32 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20  E2(X,Y).#define 
f4920 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 0a  OSTRACE3(X,Y,Z).
f4930 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 34  #define OSTRACE4
f4940 28 58 2c 59 2c 5a 2c 41 29 0a 23 64 65 66 69 6e  (X,Y,Z,A).#defin
f4950 65 20 4f 53 54 52 41 43 45 35 28 58 2c 59 2c 5a  e OSTRACE5(X,Y,Z
f4960 2c 41 2c 42 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,A,B).#define OS
f4970 54 52 41 43 45 36 28 58 2c 59 2c 5a 2c 41 2c 42  TRACE6(X,Y,Z,A,B
f4980 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,C).#define OSTR
f4990 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  ACE7(X,Y,Z,A,B,C
f49a0 2c 44 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ,D).#endif../*.*
f49b0 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 70 65 72  * Macros for per
f49c0 66 6f 72 6d 61 6e 63 65 20 74 72 61 63 69 6e 67  formance tracing
f49d0 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e  .  Normally turn
f49e0 65 64 20 6f 66 66 2e 20 20 4f 6e 6c 79 20 77 6f  ed off.  Only wo
f49f0 72 6b 73 0a 2a 2a 20 6f 6e 20 69 34 38 36 20 68  rks.** on i486 h
f4a00 61 72 64 77 61 72 65 2e 0a 2a 2f 0a 23 69 66 64  ardware..*/.#ifd
f4a10 65 66 20 53 51 4c 49 54 45 5f 50 45 52 46 4f 52  ef SQLITE_PERFOR
f4a20 4d 41 4e 43 45 5f 54 52 41 43 45 0a 0a 2f 2a 20  MANCE_TRACE../* 
f4a30 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e  .** hwtime.h con
f4a40 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73  tains inline ass
f4a50 65 6d 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20  embler code for 
f4a60 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a  implementing .**
f4a70 20 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63   high-performanc
f4a80 65 20 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65  e timing routine
f4a90 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  s..*/./*********
f4aa0 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77  ***** Include hw
f4ab0 74 69 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69  time.h in the mi
f4ac0 64 64 6c 65 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f  ddle of os_commo
f4ad0 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.h ************
f4ae0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
f4af0 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
f4b00 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a   hwtime.h ******
f4b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4b30 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38  ****/./*.** 2008
f4b40 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68   May 27.**.** Th
f4b50 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
f4b60 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
f4b70 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
f4b80 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
f4b90 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
f4ba0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
f4bb0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
f4bc0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
f4bd0 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
f4be0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
f4bf0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
f4c00 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
f4c10 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
f4c20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
f4c30 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
f4c40 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
f4c50 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
f4c60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
f4cb0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
f4cc0 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20  ains inline asm 
f4cd0 63 6f 64 65 20 66 6f 72 20 72 65 74 72 69 65 76  code for retriev
f4ce0 69 6e 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72  ing "high-perfor
f4cf0 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65  mance".** counte
f4d00 72 73 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73  rs for x86 class
f4d10 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64   CPUs..**.** $Id
f4d20 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33  : hwtime.h,v 1.3
f4d30 20 32 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 33   2008/08/01 14:3
f4d40 33 3a 31 35 20 73 68 61 6e 65 20 45 78 70 20 24  3:15 shane Exp $
f4d50 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54  .*/.#ifndef _HWT
f4d60 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f  IME_H_.#define _
f4d70 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a  HWTIME_H_../*.**
f4d80 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72   The following r
f4d90 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b  outine only work
f4da0 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61  s on pentium-cla
f4db0 73 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72  ss (or newer) pr
f4dc0 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20  ocessors..** It 
f4dd0 75 73 65 73 20 74 68 65 20 52 44 54 53 43 20 6f  uses the RDTSC o
f4de0 70 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68  pcode to read th
f4df0 65 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61  e cycle count va
f4e00 6c 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a  lue out of the.*
f4e10 2a 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20  * processor and 
f4e20 72 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c  returns that val
f4e30 75 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65  ue.  This can be
f4e40 20 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72   used for high-r
f4e50 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e  es.** profiling.
f4e60 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64  .*/.#if (defined
f4e70 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65  (__GNUC__) || de
f4e80 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29  fined(_MSC_VER))
f4e90 20 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66   && \.      (def
f4ea0 69 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65  ined(i386) || de
f4eb0 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20  fined(__i386__) 
f4ec0 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58  || defined(_M_IX
f4ed0 38 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69  86))..  #if defi
f4ee0 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20  ned(__GNUC__).. 
f4ef0 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69   __inline__ sqli
f4f00 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65  te_uint64 sqlite
f4f10 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20  3Hwtime(void){. 
f4f20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74      unsigned int
f4f30 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f   lo, hi;.     __
f4f40 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65  asm__ __volatile
f4f50 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d  __ ("rdtsc" : "=
f4f60 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68  a" (lo), "=d" (h
f4f70 69 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e  i));.     return
f4f80 20 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29   (sqlite_uint64)
f4f90 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20  hi << 32 | lo;. 
f4fa0 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69   }..  #elif defi
f4fb0 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20  ned(_MSC_VER).. 
f4fc0 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65   __declspec(nake
f4fd0 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69  d) __inline sqli
f4fe0 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63  te_uint64 __cdec
f4ff0 6c 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  l sqlite3Hwtime(
f5000 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73  void){.     __as
f5010 6d 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73  m {.        rdts
f5020 63 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 20  c.        ret   
f5030 20 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c      ; return val
f5040 75 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20  ue at EDX:EAX.  
f5050 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64     }.  }..  #end
f5060 69 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e  if..#elif (defin
f5070 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20  ed(__GNUC__) && 
f5080 64 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34  defined(__x86_64
f5090 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65  __))..  __inline
f50a0 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34  __ sqlite_uint64
f50b0 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76   sqlite3Hwtime(v
f50c0 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69  oid){.      unsi
f50d0 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20  gned long val;. 
f50e0 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76       __asm__ __v
f50f0 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73  olatile__ ("rdts
f5100 63 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29  c" : "=A" (val))
f5110 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 76  ;.      return v
f5120 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20  al;.  }. .#elif 
f5130 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f  (defined(__GNUC_
f5140 5f 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f  _) && defined(__
f5150 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c  ppc__))..  __inl
f5160 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e  ine__ sqlite_uin
f5170 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t64 sqlite3Hwtim
f5180 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75  e(void){.      u
f5190 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e  nsigned long lon
f51a0 67 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20  g retval;.      
f51b0 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75  unsigned long ju
f51c0 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f  nk;.      __asm_
f51d0 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28  _ __volatile__ (
f51e0 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31  "\n\.          1
f51f0 3a 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25  :      mftbu   %
f5200 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20  1\n\.           
f5210 20 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 25         mftb    %
f5220 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  L0\n\.          
f5230 20 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 20          mftbu   
f5240 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20  %0\n\.          
f5250 20 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 20          cmpw    
f5260 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20  %0,%1\n\.       
f5270 20 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 20             bne  
f5280 20 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 20     1b".         
f5290 20 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20           : "=r" 
f52a0 28 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28  (retval), "=r" (
f52b0 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65  junk));.      re
f52c0 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d  turn retval;.  }
f52d0 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f  ..#else..  #erro
f52e0 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74  r Need implement
f52f0 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33  ation of sqlite3
f5300 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75  Hwtime() for you
f5310 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f  r platform...  /
f5320 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c  *.  ** To compil
f5330 65 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d  e without implem
f5340 65 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77  enting sqlite3Hw
f5350 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20  time() for your 
f5360 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79  platform,.  ** y
f5370 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68  ou can remove th
f5380 65 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61  e above #error a
f5390 6e 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f  nd use the follo
f53a0 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66  wing.  ** stub f
f53b0 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69  unction.  You wi
f53c0 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73  ll lose timing s
f53d0 75 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a  upport for many.
f53e0 20 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75    ** of the debu
f53f0 67 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e  gging and testin
f5400 67 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74  g utilities, but
f5410 20 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20   it should at.  
f5420 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65  ** least compile
f5430 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53   and run..  */.S
f5440 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
f5450 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71  sqlite_uint64 sq
f5460 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
f5470 29 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69  ){ return ((sqli
f5480 74 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a  te_uint64)0); }.
f5490 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20  .#endif..#endif 
f54a0 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54  /* !defined(_HWT
f54b0 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a  IME_H_) */../***
f54c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
f54d0 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a  of hwtime.h ****
f54e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f54f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
f5510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
f5520 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
f5530 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f 63  left off in os_c
f5540 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  ommon.h ********
f5550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61  **********/..sta
f5560 74 69 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  tic sqlite_uint6
f5570 34 20 67 5f 73 74 61 72 74 3b 0a 73 74 61 74 69  4 g_start;.stati
f5580 63 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  c sqlite_uint64 
f5590 67 5f 65 6c 61 70 73 65 64 3b 0a 23 64 65 66 69  g_elapsed;.#defi
f55a0 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 20 20  ne TIMER_START  
f55b0 20 20 20 20 20 67 5f 73 74 61 72 74 3d 73 71 6c       g_start=sql
f55c0 69 74 65 33 48 77 74 69 6d 65 28 29 0a 23 64 65  ite3Hwtime().#de
f55d0 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e 44 20 20  fine TIMER_END  
f55e0 20 20 20 20 20 20 20 67 5f 65 6c 61 70 73 65 64         g_elapsed
f55f0 3d 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29  =sqlite3Hwtime()
f5600 2d 67 5f 73 74 61 72 74 0a 23 64 65 66 69 6e 65  -g_start.#define
f5610 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20   TIMER_ELAPSED  
f5620 20 20 20 67 5f 65 6c 61 70 73 65 64 0a 23 65 6c     g_elapsed.#el
f5630 73 65 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  se.#define TIMER
f5640 5f 53 54 41 52 54 0a 23 64 65 66 69 6e 65 20 54  _START.#define T
f5650 49 4d 45 52 5f 45 4e 44 0a 23 64 65 66 69 6e 65  IMER_END.#define
f5660 20 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 20 20   TIMER_ELAPSED  
f5670 20 20 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74     ((sqlite_uint
f5680 36 34 29 30 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  64)0).#endif../*
f5690 0a 2a 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c  .** If we compil
f56a0 65 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54  e with the SQLIT
f56b0 45 5f 54 45 53 54 20 6d 61 63 72 6f 20 73 65 74  E_TEST macro set
f56c0 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f  , then the follo
f56d0 77 69 6e 67 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66  wing block.** of
f56e0 20 63 6f 64 65 20 77 69 6c 6c 20 67 69 76 65 20   code will give 
f56f0 75 73 20 74 68 65 20 61 62 69 6c 69 74 79 20 74  us the ability t
f5700 6f 20 73 69 6d 75 6c 61 74 65 20 61 20 64 69 73  o simulate a dis
f5710 6b 20 49 2f 4f 20 65 72 72 6f 72 2e 20 20 54 68  k I/O error.  Th
f5720 69 73 0a 2a 2a 20 69 73 20 75 73 65 64 20 66 6f  is.** is used fo
f5730 72 20 74 65 73 74 69 6e 67 20 74 68 65 20 49 2f  r testing the I/
f5740 4f 20 72 65 63 6f 76 65 72 79 20 6c 6f 67 69 63  O recovery logic
f5750 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
f5760 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41  TE_TEST.SQLITE_A
f5770 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  PI int sqlite3_i
f5780 6f 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 30 3b  o_error_hit = 0;
f5790 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
f57a0 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 49  otal number of I
f57b0 2f 4f 20 45 72 72 6f 72 73 20 2a 2f 0a 53 51 4c  /O Errors */.SQL
f57c0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
f57d0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72  te3_io_error_har
f57e0 64 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20  dhit = 0;       
f57f0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 6f   /* Number of no
f5800 6e 2d 62 65 6e 69 67 6e 20 65 72 72 6f 72 73 20  n-benign errors 
f5810 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
f5820 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  t sqlite3_io_err
f5830 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b 20  or_pending = 0; 
f5840 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20         /* Count 
f5850 64 6f 77 6e 20 74 6f 20 66 69 72 73 74 20 49 2f  down to first I/
f5860 4f 20 65 72 72 6f 72 20 2a 2f 0a 53 51 4c 49 54  O error */.SQLIT
f5870 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
f5880 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69  3_io_error_persi
f5890 73 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  st = 0;        /
f58a0 2a 20 54 72 75 65 20 69 66 20 49 2f 4f 20 65 72  * True if I/O er
f58b0 72 6f 72 73 20 70 65 72 73 69 73 74 20 2a 2f 0a  rors persist */.
f58c0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
f58d0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
f58e0 62 65 6e 69 67 6e 20 3d 20 30 3b 20 20 20 20 20  benign = 0;     
f58f0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 65      /* True if e
f5900 72 72 6f 72 73 20 61 72 65 20 62 65 6e 69 67 6e  rrors are benign
f5910 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
f5920 6e 74 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66  nt sqlite3_diskf
f5930 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 20 30 3b  ull_pending = 0;
f5940 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
f5950 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c  sqlite3_diskfull
f5960 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 69   = 0;.#define Si
f5970 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e  mulateIOErrorBen
f5980 69 67 6e 28 58 29 20 73 71 6c 69 74 65 33 5f 69  ign(X) sqlite3_i
f5990 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 3d 28  o_error_benign=(
f59a0 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c  X).#define Simul
f59b0 61 74 65 49 4f 45 72 72 6f 72 28 43 4f 44 45 29  ateIOError(CODE)
f59c0 20 20 5c 0a 20 20 69 66 28 20 28 73 71 6c 69 74    \.  if( (sqlit
f59d0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 72 73  e3_io_error_pers
f59e0 69 73 74 20 26 26 20 73 71 6c 69 74 65 33 5f 69  ist && sqlite3_i
f59f0 6f 5f 65 72 72 6f 72 5f 68 69 74 29 20 5c 0a 20  o_error_hit) \. 
f5a00 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33        || sqlite3
f5a10 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e  _io_error_pendin
f5a20 67 2d 2d 20 3d 3d 20 31 20 29 20 20 5c 0a 20 20  g-- == 1 )  \.  
f5a30 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 6c 6f              { lo
f5a40 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 43 4f 44  cal_ioerr(); COD
f5a50 45 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  E; }.static void
f5a60 20 6c 6f 63 61 6c 5f 69 6f 65 72 72 28 29 7b 0a   local_ioerr(){.
f5a70 20 20 49 4f 54 52 41 43 45 28 28 22 49 4f 45 52    IOTRACE(("IOER
f5a80 52 5c 6e 22 29 29 3b 0a 20 20 73 71 6c 69 74 65  R\n"));.  sqlite
f5a90 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 2b 2b  3_io_error_hit++
f5aa0 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33  ;.  if( !sqlite3
f5ab0 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e  _io_error_benign
f5ac0 20 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72   ) sqlite3_io_er
f5ad0 72 6f 72 5f 68 61 72 64 68 69 74 2b 2b 3b 0a 7d  ror_hardhit++;.}
f5ae0 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74  .#define Simulat
f5af0 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 43  eDiskfullError(C
f5b00 4f 44 45 29 20 5c 0a 20 20 20 69 66 28 20 73 71  ODE) \.   if( sq
f5b10 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70  lite3_diskfull_p
f5b20 65 6e 64 69 6e 67 20 29 7b 20 5c 0a 20 20 20 20  ending ){ \.    
f5b30 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64 69 73   if( sqlite3_dis
f5b40 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d  kfull_pending ==
f5b50 20 31 20 29 7b 20 5c 0a 20 20 20 20 20 20 20 6c   1 ){ \.       l
f5b60 6f 63 61 6c 5f 69 6f 65 72 72 28 29 3b 20 5c 0a  ocal_ioerr(); \.
f5b70 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64         sqlite3_d
f5b80 69 73 6b 66 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20  iskfull = 1; \. 
f5b90 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6f        sqlite3_io
f5ba0 5f 65 72 72 6f 72 5f 68 69 74 20 3d 20 31 3b 20  _error_hit = 1; 
f5bb0 5c 0a 20 20 20 20 20 20 20 43 4f 44 45 3b 20 5c  \.       CODE; \
f5bc0 0a 20 20 20 20 20 7d 65 6c 73 65 7b 20 5c 0a 20  .     }else{ \. 
f5bd0 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 64 69        sqlite3_di
f5be0 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d  skfull_pending--
f5bf0 3b 20 5c 0a 20 20 20 20 20 7d 20 5c 0a 20 20 20  ; \.     } \.   
f5c00 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  }.#else.#define 
f5c10 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42  SimulateIOErrorB
f5c20 65 6e 69 67 6e 28 58 29 0a 23 64 65 66 69 6e 65  enign(X).#define
f5c30 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
f5c40 28 41 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75  (A).#define Simu
f5c50 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45 72 72 6f  lateDiskfullErro
f5c60 72 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  r(A).#endif../*.
f5c70 2a 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c  ** When testing,
f5c80 20 6b 65 65 70 20 61 20 63 6f 75 6e 74 20 6f 66   keep a count of
f5c90 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f   the number of o
f5ca0 70 65 6e 20 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69  pen files..*/.#i
f5cb0 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
f5cc0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
f5cd0 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c  sqlite3_open_fil
f5ce0 65 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 64 65  e_count = 0;.#de
f5cf0 66 69 6e 65 20 4f 70 65 6e 43 6f 75 6e 74 65 72  fine OpenCounter
f5d00 28 58 29 20 20 73 71 6c 69 74 65 33 5f 6f 70 65  (X)  sqlite3_ope
f5d10 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58  n_file_count+=(X
f5d20 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  ).#else.#define 
f5d30 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 0a 23  OpenCounter(X).#
f5d40 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a  endif..#endif /*
f5d50 20 21 64 65 66 69 6e 65 64 28 5f 4f 53 5f 43 4f   !defined(_OS_CO
f5d60 4d 4d 4f 4e 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a  MMON_H_) */../**
f5d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
f5d80 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20   of os_common.h 
f5d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
f5dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
f5dd0 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
f5de0 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6f 73 5f   left off in os_
f5df0 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  win.c **********
f5e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a  ***********/../*
f5e10 0a 2a 2a 20 53 6f 6d 65 20 6d 69 63 72 6f 73 6f  .** Some microso
f5e20 66 74 20 63 6f 6d 70 69 6c 65 72 73 20 6c 61 63  ft compilers lac
f5e30 6b 20 74 68 69 73 20 64 65 66 69 6e 69 74 69 6f  k this definitio
f5e40 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 49 4e  n..*/.#ifndef IN
f5e50 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49  VALID_FILE_ATTRI
f5e60 42 55 54 45 53 0a 23 20 64 65 66 69 6e 65 20 49  BUTES.# define I
f5e70 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52  NVALID_FILE_ATTR
f5e80 49 42 55 54 45 53 20 28 28 44 57 4f 52 44 29 2d  IBUTES ((DWORD)-
f5e90 31 29 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  1) .#endif../*.*
f5ea0 2a 20 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77  * Determine if w
f5eb0 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69  e are dealing wi
f5ec0 74 68 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77  th WindowsCE - w
f5ed0 68 69 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a  hich has a much.
f5ee0 2a 2a 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a  ** reduced API..
f5ef0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  */.#if SQLITE_OS
f5f00 5f 57 49 4e 43 45 0a 23 20 64 65 66 69 6e 65 20  _WINCE.# define 
f5f10 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49 28  AreFileApisANSI(
f5f20 29 20 31 0a 23 20 64 65 66 69 6e 65 20 46 6f 72  ) 1.# define For
f5f30 6d 61 74 4d 65 73 73 61 67 65 57 28 61 2c 62 2c  matMessageW(a,b,
f5f40 63 2c 64 2c 65 2c 66 2c 67 29 20 30 0a 23 65 6e  c,d,e,f,g) 0.#en
f5f50 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 69 6e 43 45  dif../*.** WinCE
f5f60 20 6c 61 63 6b 73 20 6e 61 74 69 76 65 20 73 75   lacks native su
f5f70 70 70 6f 72 74 20 66 6f 72 20 66 69 6c 65 20 6c  pport for file l
f5f80 6f 63 6b 69 6e 67 20 73 6f 20 77 65 20 68 61 76  ocking so we hav
f5f90 65 20 74 6f 20 66 61 6b 65 20 69 74 0a 2a 2a 20  e to fake it.** 
f5fa0 77 69 74 68 20 73 6f 6d 65 20 63 6f 64 65 20 6f  with some code o
f5fb0 66 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 23 69  f our own..*/.#i
f5fc0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
f5fd0 45 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  E.typedef struct
f5fe0 20 77 69 6e 63 65 4c 6f 63 6b 20 7b 0a 20 20 69   winceLock {.  i
f5ff0 6e 74 20 6e 52 65 61 64 65 72 73 3b 20 20 20 20  nt nReaders;    
f6000 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
f6010 72 65 61 64 65 72 20 6c 6f 63 6b 73 20 6f 62 74  reader locks obt
f6020 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20  ained */.  BOOL 
f6030 62 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 20 2f  bPending;      /
f6040 2a 20 49 6e 64 69 63 61 74 65 73 20 61 20 70 65  * Indicates a pe
f6050 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62  nding lock has b
f6060 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a 2f 0a  een obtained */.
f6070 20 20 42 4f 4f 4c 20 62 52 65 73 65 72 76 65 64    BOOL bReserved
f6080 3b 20 20 20 20 20 2f 2a 20 49 6e 64 69 63 61 74  ;     /* Indicat
f6090 65 73 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f  es a reserved lo
f60a0 63 6b 20 68 61 73 20 62 65 65 6e 20 6f 62 74 61  ck has been obta
f60b0 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f 4c 20 62  ined */.  BOOL b
f60c0 45 78 63 6c 75 73 69 76 65 3b 20 20 20 20 2f 2a  Exclusive;    /*
f60d0 20 49 6e 64 69 63 61 74 65 73 20 61 6e 20 65 78   Indicates an ex
f60e0 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 68 61 73  clusive lock has
f60f0 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 2a   been obtained *
f6100 2f 0a 7d 20 77 69 6e 63 65 4c 6f 63 6b 3b 0a 23  /.} winceLock;.#
f6110 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
f6120 20 77 69 6e 46 69 6c 65 20 73 74 72 75 63 74 75   winFile structu
f6130 72 65 20 69 73 20 61 20 73 75 62 63 6c 61 73 73  re is a subclass
f6140 20 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65   of sqlite3_file
f6150 2a 20 73 70 65 63 69 66 69 63 20 74 6f 20 74 68  * specific to th
f6160 65 20 77 69 6e 33 32 0a 2a 2a 20 70 6f 72 74 61  e win32.** porta
f6170 62 69 6c 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f  bility layer..*/
f6180 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
f6190 77 69 6e 46 69 6c 65 20 77 69 6e 46 69 6c 65 3b  winFile winFile;
f61a0 0a 73 74 72 75 63 74 20 77 69 6e 46 69 6c 65 20  .struct winFile 
f61b0 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  {.  const sqlite
f61c0 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d  3_io_methods *pM
f61d0 65 74 68 6f 64 3b 2f 2a 20 4d 75 73 74 20 62 65  ethod;/* Must be
f61e0 20 66 69 72 73 74 20 2a 2f 0a 20 20 48 41 4e 44   first */.  HAND
f61f0 4c 45 20 68 3b 20 20 20 20 20 20 20 20 20 20 20  LE h;           
f6200 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f      /* Handle fo
f6210 72 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20  r accessing the 
f6220 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  file */.  unsign
f6230 65 64 20 63 68 61 72 20 6c 6f 63 6b 74 79 70 65  ed char locktype
f6240 3b 20 2f 2a 20 54 79 70 65 20 6f 66 20 6c 6f 63  ; /* Type of loc
f6250 6b 20 63 75 72 72 65 6e 74 6c 79 20 68 65 6c 64  k currently held
f6260 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 20 2a 2f   on this file */
f6270 0a 20 20 73 68 6f 72 74 20 73 68 61 72 65 64 4c  .  short sharedL
f6280 6f 63 6b 42 79 74 65 3b 20 20 20 2f 2a 20 52 61  ockByte;   /* Ra
f6290 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 79  ndomly chosen by
f62a0 74 65 20 75 73 65 64 20 61 73 20 61 20 73 68 61  te used as a sha
f62b0 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 44 57  red lock */.  DW
f62c0 4f 52 44 20 6c 61 73 74 45 72 72 6e 6f 3b 20 20  ORD lastErrno;  
f62d0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 69 6e        /* The Win
f62e0 64 6f 77 73 20 65 72 72 6e 6f 20 66 72 6f 6d 20  dows errno from 
f62f0 74 68 65 20 6c 61 73 74 20 49 2f 4f 20 65 72 72  the last I/O err
f6300 6f 72 20 2a 2f 0a 20 20 44 57 4f 52 44 20 73 65  or */.  DWORD se
f6310 63 74 6f 72 53 69 7a 65 3b 20 20 20 20 20 20 20  ctorSize;       
f6320 2f 2a 20 53 65 63 74 6f 72 20 73 69 7a 65 20 6f  /* Sector size o
f6330 66 20 74 68 65 20 64 65 76 69 63 65 20 66 69 6c  f the device fil
f6340 65 20 69 73 20 6f 6e 20 2a 2f 0a 23 69 66 20 53  e is on */.#if S
f6350 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20  QLITE_OS_WINCE. 
f6360 20 57 43 48 41 52 20 2a 7a 44 65 6c 65 74 65 4f   WCHAR *zDeleteO
f6370 6e 43 6c 6f 73 65 3b 20 20 2f 2a 20 4e 61 6d 65  nClose;  /* Name
f6380 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65   of file to dele
f6390 74 65 20 77 68 65 6e 20 63 6c 6f 73 69 6e 67 20  te when closing 
f63a0 2a 2f 0a 20 20 48 41 4e 44 4c 45 20 68 4d 75 74  */.  HANDLE hMut
f63b0 65 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ex;          /* 
f63c0 4d 75 74 65 78 20 75 73 65 64 20 74 6f 20 63 6f  Mutex used to co
f63d0 6e 74 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20  ntrol access to 
f63e0 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 20 20  shared lock */  
f63f0 0a 20 20 48 41 4e 44 4c 45 20 68 53 68 61 72 65  .  HANDLE hShare
f6400 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68  d;         /* Sh
f6410 61 72 65 64 20 6d 65 6d 6f 72 79 20 73 65 67 6d  ared memory segm
f6420 65 6e 74 20 75 73 65 64 20 66 6f 72 20 6c 6f 63  ent used for loc
f6430 6b 69 6e 67 20 2a 2f 0a 20 20 77 69 6e 63 65 4c  king */.  winceL
f6440 6f 63 6b 20 6c 6f 63 61 6c 3b 20 20 20 20 20 20  ock local;      
f6450 20 20 2f 2a 20 4c 6f 63 6b 73 20 6f 62 74 61 69    /* Locks obtai
f6460 6e 65 64 20 62 79 20 74 68 69 73 20 69 6e 73 74  ned by this inst
f6470 61 6e 63 65 20 6f 66 20 77 69 6e 46 69 6c 65 20  ance of winFile 
f6480 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f 63 6b 20 2a  */.  winceLock *
f6490 73 68 61 72 65 64 3b 20 20 20 20 20 20 2f 2a 20  shared;      /* 
f64a0 47 6c 6f 62 61 6c 20 73 68 61 72 65 64 20 6c 6f  Global shared lo
f64b0 63 6b 20 6d 65 6d 6f 72 79 20 66 6f 72 20 74 68  ck memory for th
f64c0 65 20 66 69 6c 65 20 20 2a 2f 0a 23 65 6e 64 69  e file  */.#endi
f64d0 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77  f.};../*.** Forw
f64e0 61 72 64 20 70 72 6f 74 6f 74 79 70 65 73 2e 0a  ard prototypes..
f64f0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
f6500 74 53 65 63 74 6f 72 53 69 7a 65 28 0a 20 20 20  tSectorSize(.   
f6510 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
f6520 66 73 2c 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  fs,.    const ch
f6530 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 20 20 20  ar *zRelative   
f6540 20 20 2f 2a 20 55 54 46 2d 38 20 66 69 6c 65 20    /* UTF-8 file 
f6550 6e 61 6d 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a  name */.);../*.*
f6560 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
f6570 76 61 72 69 61 62 6c 65 20 69 73 20 28 6e 6f 72  variable is (nor
f6580 6d 61 6c 6c 79 29 20 73 65 74 20 6f 6e 63 65 20  mally) set once 
f6590 61 6e 64 20 6e 65 76 65 72 20 63 68 61 6e 67 65  and never change
f65a0 73 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 2e  s.** thereafter.
f65b0 20 20 49 74 20 72 65 63 6f 72 64 73 20 77 68 65    It records whe
f65c0 74 68 65 72 20 74 68 65 20 6f 70 65 72 61 74 69  ther the operati
f65d0 6e 67 20 73 79 73 74 65 6d 20 69 73 20 57 69 6e  ng system is Win
f65e0 39 35 0a 2a 2a 20 6f 72 20 57 69 6e 4e 54 2e 0a  95.** or WinNT..
f65f0 2a 2a 0a 2a 2a 20 30 3a 20 20 20 4f 70 65 72 61  **.** 0:   Opera
f6600 74 69 6e 67 20 73 79 73 74 65 6d 20 75 6e 6b 6e  ting system unkn
f6610 6f 77 6e 2e 0a 2a 2a 20 31 3a 20 20 20 4f 70 65  own..** 1:   Ope
f6620 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73  rating system is
f6630 20 57 69 6e 39 35 2e 0a 2a 2a 20 32 3a 20 20 20   Win95..** 2:   
f6640 4f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  Operating system
f6650 20 69 73 20 57 69 6e 4e 54 2e 0a 2a 2a 0a 2a 2a   is WinNT..**.**
f6660 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 66 61 63   In order to fac
f6670 69 6c 69 74 61 74 65 20 74 65 73 74 69 6e 67 20  ilitate testing 
f6680 6f 6e 20 61 20 57 69 6e 4e 54 20 73 79 73 74 65  on a WinNT syste
f6690 6d 2c 20 74 68 65 20 74 65 73 74 20 66 69 78 74  m, the test fixt
f66a0 75 72 65 0a 2a 2a 20 63 61 6e 20 6d 61 6e 75 61  ure.** can manua
f66b0 6c 6c 79 20 73 65 74 20 74 68 69 73 20 76 61 6c  lly set this val
f66c0 75 65 20 74 6f 20 31 20 74 6f 20 65 6d 75 6c 61  ue to 1 to emula
f66d0 74 65 20 57 69 6e 39 38 20 62 65 68 61 76 69 6f  te Win98 behavio
f66e0 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  r..*/.#ifdef SQL
f66f0 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
f6700 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
f6710 6f 73 5f 74 79 70 65 20 3d 20 30 3b 0a 23 65 6c  os_type = 0;.#el
f6720 73 65 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  se.static int sq
f6730 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20  lite3_os_type = 
f6740 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  0;.#endif../*.**
f6750 20 52 65 74 75 72 6e 20 74 72 75 65 20 28 6e 6f   Return true (no
f6760 6e 2d 7a 65 72 6f 29 20 69 66 20 77 65 20 61 72  n-zero) if we ar
f6770 65 20 72 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20  e running under 
f6780 57 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69  WinNT, Win2K, Wi
f6790 6e 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45  nXP,.** or WinCE
f67a0 2e 20 20 52 65 74 75 72 6e 20 66 61 6c 73 65 20  .  Return false 
f67b0 28 7a 65 72 6f 29 20 66 6f 72 20 57 69 6e 39 35  (zero) for Win95
f67c0 2c 20 57 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d  , Win98, or WinM
f67d0 45 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73  E..**.** Here is
f67e0 20 61 6e 20 69 6e 74 65 72 65 73 74 69 6e 67 20   an interesting 
f67f0 6f 62 73 65 72 76 61 74 69 6f 6e 3a 20 20 57 69  observation:  Wi
f6800 6e 39 35 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20  n95, Win98, and 
f6810 57 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68  WinME lack.** th
f6820 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41  e LockFileEx() A
f6830 50 49 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20  PI.  But we can 
f6840 73 74 69 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79  still statically
f6850 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68   link against th
f6860 61 74 0a 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e  at.** API as lon
f6870 67 20 61 73 20 77 65 20 64 6f 6e 27 74 20 63 61  g as we don't ca
f6880 6c 6c 20 69 74 20 77 68 65 6e 20 72 75 6e 6e 69  ll it when runni
f6890 6e 67 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20  ng Win95/98/ME. 
f68a0 20 41 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68   A call to.** th
f68b0 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
f68c0 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
f68d0 69 66 20 74 68 65 20 68 6f 73 74 20 69 73 20 57  if the host is W
f68e0 69 6e 39 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a  in95/98/ME or.**
f68f0 20 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f 20   WinNT/2K/XP so 
f6900 74 68 61 74 20 77 65 20 77 69 6c 6c 20 6b 6e 6f  that we will kno
f6910 77 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  w whether or not
f6920 20 77 65 20 63 61 6e 20 73 61 66 65 6c 79 20 63   we can safely c
f6930 61 6c 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46  all.** the LockF
f6940 69 6c 65 45 78 28 29 20 41 50 49 2e 0a 2a 2f 0a  ileEx() API..*/.
f6950 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
f6960 4e 43 45 0a 23 20 64 65 66 69 6e 65 20 69 73 4e  NCE.# define isN
f6970 54 28 29 20 20 28 31 29 0a 23 65 6c 73 65 0a 20  T()  (1).#else. 
f6980 20 73 74 61 74 69 63 20 69 6e 74 20 69 73 4e 54   static int isNT
f6990 28 76 6f 69 64 29 7b 0a 20 20 20 20 69 66 28 20  (void){.    if( 
f69a0 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d  sqlite3_os_type=
f69b0 3d 30 20 29 7b 0a 20 20 20 20 20 20 4f 53 56 45  =0 ){.      OSVE
f69c0 52 53 49 4f 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b  RSIONINFO sInfo;
f69d0 0a 20 20 20 20 20 20 73 49 6e 66 6f 2e 64 77 4f  .      sInfo.dwO
f69e0 53 56 65 72 73 69 6f 6e 49 6e 66 6f 53 69 7a 65  SVersionInfoSize
f69f0 20 3d 20 73 69 7a 65 6f 66 28 73 49 6e 66 6f 29   = sizeof(sInfo)
f6a00 3b 0a 20 20 20 20 20 20 47 65 74 56 65 72 73 69  ;.      GetVersi
f6a10 6f 6e 45 78 28 26 73 49 6e 66 6f 29 3b 0a 20 20  onEx(&sInfo);.  
f6a20 20 20 20 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74      sqlite3_os_t
f6a30 79 70 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 6c  ype = sInfo.dwPl
f6a40 61 74 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 4c  atformId==VER_PL
f6a50 41 54 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 20  ATFORM_WIN32_NT 
f6a60 3f 20 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a 20  ? 2 : 1;.    }. 
f6a70 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65     return sqlite
f6a80 33 5f 6f 73 5f 74 79 70 65 3d 3d 32 3b 0a 20 20  3_os_type==2;.  
f6a90 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
f6aa0 54 45 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a  TE_OS_WINCE */..
f6ab0 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20  /*.** Convert a 
f6ac0 55 54 46 2d 38 20 73 74 72 69 6e 67 20 74 6f 20  UTF-8 string to 
f6ad0 6d 69 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64  microsoft unicod
f6ae0 65 20 28 55 54 46 2d 31 36 3f 29 2e 20 0a 2a 2a  e (UTF-16?). .**
f6af0 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c  .** Space to hol
f6b00 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73  d the returned s
f6b10 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65  tring is obtaine
f6b20 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a  d from malloc..*
f6b30 2f 0a 73 74 61 74 69 63 20 57 43 48 41 52 20 2a  /.static WCHAR *
f6b40 75 74 66 38 54 6f 55 6e 69 63 6f 64 65 28 63 6f  utf8ToUnicode(co
f6b50 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  nst char *zFilen
f6b60 61 6d 65 29 7b 0a 20 20 69 6e 74 20 6e 43 68 61  ame){.  int nCha
f6b70 72 3b 0a 20 20 57 43 48 41 52 20 2a 7a 57 69 64  r;.  WCHAR *zWid
f6b80 65 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20 6e 43  eFilename;..  nC
f6b90 68 61 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54  har = MultiByteT
f6ba0 6f 57 69 64 65 43 68 61 72 28 43 50 5f 55 54 46  oWideChar(CP_UTF
f6bb0 38 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c  8, 0, zFilename,
f6bc0 20 2d 31 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 20   -1, NULL, 0);. 
f6bd0 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 20 3d   zWideFilename =
f6be0 20 6d 61 6c 6c 6f 63 28 20 6e 43 68 61 72 2a 73   malloc( nChar*s
f6bf0 69 7a 65 6f 66 28 7a 57 69 64 65 46 69 6c 65 6e  izeof(zWideFilen
f6c00 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28  ame[0]) );.  if(
f6c10 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 3d 3d   zWideFilename==
f6c20 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
f6c30 30 3b 0a 20 20 7d 0a 20 20 6e 43 68 61 72 20 3d  0;.  }.  nChar =
f6c40 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65   MultiByteToWide
f6c50 43 68 61 72 28 43 50 5f 55 54 46 38 2c 20 30 2c  Char(CP_UTF8, 0,
f6c60 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20   zFilename, -1, 
f6c70 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 6e  zWideFilename, n
f6c80 43 68 61 72 29 3b 0a 20 20 69 66 28 20 6e 43 68  Char);.  if( nCh
f6c90 61 72 3d 3d 30 20 29 7b 0a 20 20 20 20 66 72 65  ar==0 ){.    fre
f6ca0 65 28 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29  e(zWideFilename)
f6cb0 3b 0a 20 20 20 20 7a 57 69 64 65 46 69 6c 65 6e  ;.    zWideFilen
f6cc0 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  ame = 0;.  }.  r
f6cd0 65 74 75 72 6e 20 7a 57 69 64 65 46 69 6c 65 6e  eturn zWideFilen
f6ce0 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  ame;.}../*.** Co
f6cf0 6e 76 65 72 74 20 6d 69 63 72 6f 73 6f 66 74 20  nvert microsoft 
f6d00 75 6e 69 63 6f 64 65 20 74 6f 20 55 54 46 2d 38  unicode to UTF-8
f6d10 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  .  Space to hold
f6d20 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   the returned st
f6d30 72 69 6e 67 20 69 73 0a 2a 2a 20 6f 62 74 61 69  ring is.** obtai
f6d40 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28  ned from malloc(
f6d50 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61  )..*/.static cha
f6d60 72 20 2a 75 6e 69 63 6f 64 65 54 6f 55 74 66 38  r *unicodeToUtf8
f6d70 28 63 6f 6e 73 74 20 57 43 48 41 52 20 2a 7a 57  (const WCHAR *zW
f6d80 69 64 65 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20  ideFilename){.  
f6d90 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 63 68 61  int nByte;.  cha
f6da0 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20  r *zFilename;.. 
f6db0 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61   nByte = WideCha
f6dc0 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 5f  rToMultiByte(CP_
f6dd0 55 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 69  UTF8, 0, zWideFi
f6de0 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30  lename, -1, 0, 0
f6df0 2c 20 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 65  , 0, 0);.  zFile
f6e00 6e 61 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e  name = malloc( n
f6e10 42 79 74 65 20 29 3b 0a 20 20 69 66 28 20 7a 46  Byte );.  if( zF
f6e20 69 6c 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20  ilename==0 ){.  
f6e30 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
f6e40 20 20 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68    nByte = WideCh
f6e50 61 72 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50  arToMultiByte(CP
f6e60 5f 55 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46  _UTF8, 0, zWideF
f6e70 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69  ilename, -1, zFi
f6e80 6c 65 6e 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20  lename, nByte,. 
f6e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f6ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20               0, 
f6eb0 30 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 20  0);.  if( nByte 
f6ec0 3d 3d 20 30 20 29 7b 0a 20 20 20 20 66 72 65 65  == 0 ){.    free
f6ed0 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20  (zFilename);.   
f6ee0 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a   zFilename = 0;.
f6ef0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 46 69    }.  return zFi
f6f00 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  lename;.}../*.**
f6f10 20 43 6f 6e 76 65 72 74 20 61 6e 20 61 6e 73 69   Convert an ansi
f6f20 20 73 74 72 69 6e 67 20 74 6f 20 6d 69 63 72 6f   string to micro
f6f30 73 6f 66 74 20 75 6e 69 63 6f 64 65 2c 20 62 61  soft unicode, ba
f6f40 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 63 75  sed on the.** cu
f6f50 72 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 73  rrent codepage s
f6f60 65 74 74 69 6e 67 73 20 66 6f 72 20 66 69 6c 65  ettings for file
f6f70 20 61 70 69 73 2e 0a 2a 2a 20 0a 2a 2a 20 53 70   apis..** .** Sp
f6f80 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ace to hold the 
f6f90 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20  returned string 
f6fa0 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66  is obtained.** f
f6fb0 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2f 0a 73  rom malloc..*/.s
f6fc0 74 61 74 69 63 20 57 43 48 41 52 20 2a 6d 62 63  tatic WCHAR *mbc
f6fd0 73 54 6f 55 6e 69 63 6f 64 65 28 63 6f 6e 73 74  sToUnicode(const
f6fe0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
f6ff0 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a  ){.  int nByte;.
f7000 20 20 57 43 48 41 52 20 2a 7a 4d 62 63 73 46 69    WCHAR *zMbcsFi
f7010 6c 65 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f  lename;.  int co
f7020 64 65 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65  depage = AreFile
f7030 41 70 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f  ApisANSI() ? CP_
f7040 41 43 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a  ACP : CP_OEMCP;.
f7050 0a 20 20 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69  .  nByte = Multi
f7060 42 79 74 65 54 6f 57 69 64 65 43 68 61 72 28 63  ByteToWideChar(c
f7070 6f 64 65 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c  odepage, 0, zFil
f7080 65 6e 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c  ename, -1, NULL,
f7090 30 29 2a 73 69 7a 65 6f 66 28 57 43 48 41 52 29  0)*sizeof(WCHAR)
f70a0 3b 0a 20 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d  ;.  zMbcsFilenam
f70b0 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74  e = malloc( nByt
f70c0 65 2a 73 69 7a 65 6f 66 28 7a 4d 62 63 73 46 69  e*sizeof(zMbcsFi
f70d0 6c 65 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20  lename[0]) );.  
f70e0 69 66 28 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d  if( zMbcsFilenam
f70f0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  e==0 ){.    retu
f7100 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 79 74  rn 0;.  }.  nByt
f7110 65 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57  e = MultiByteToW
f7120 69 64 65 43 68 61 72 28 63 6f 64 65 70 61 67 65  ideChar(codepage
f7130 2c 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20  , 0, zFilename, 
f7140 2d 31 2c 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d  -1, zMbcsFilenam
f7150 65 2c 20 6e 42 79 74 65 29 3b 0a 20 20 69 66 28  e, nByte);.  if(
f7160 20 6e 42 79 74 65 3d 3d 30 20 29 7b 0a 20 20 20   nByte==0 ){.   
f7170 20 66 72 65 65 28 7a 4d 62 63 73 46 69 6c 65 6e   free(zMbcsFilen
f7180 61 6d 65 29 3b 0a 20 20 20 20 7a 4d 62 63 73 46  ame);.    zMbcsF
f7190 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d  ilename = 0;.  }
f71a0 0a 20 20 72 65 74 75 72 6e 20 7a 4d 62 63 73 46  .  return zMbcsF
f71b0 69 6c 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  ilename;.}../*.*
f71c0 2a 20 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73  * Convert micros
f71d0 6f 66 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 6d  oft unicode to m
f71e0 75 6c 74 69 62 79 74 65 20 63 68 61 72 61 63 74  ultibyte charact
f71f0 65 72 20 73 74 72 69 6e 67 2c 20 62 61 73 65 64  er string, based
f7200 20 6f 6e 20 74 68 65 0a 2a 2a 20 75 73 65 72 27   on the.** user'
f7210 73 20 41 6e 73 69 20 63 6f 64 65 70 61 67 65 2e  s Ansi codepage.
f7220 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20  .**.** Space to 
f7230 68 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65  hold the returne
f7240 64 20 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61  d string is obta
f7250 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c  ined from.** mal
f7260 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  loc()..*/.static
f7270 20 63 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f   char *unicodeTo
f7280 4d 62 63 73 28 63 6f 6e 73 74 20 57 43 48 41 52  Mbcs(const WCHAR
f7290 20 2a 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29   *zWideFilename)
f72a0 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20  {.  int nByte;. 
f72b0 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
f72c0 3b 0a 20 20 69 6e 74 20 63 6f 64 65 70 61 67 65  ;.  int codepage
f72d0 20 3d 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e   = AreFileApisAN
f72e0 53 49 28 29 20 3f 20 43 50 5f 41 43 50 20 3a 20  SI() ? CP_ACP : 
f72f0 43 50 5f 4f 45 4d 43 50 3b 0a 0a 20 20 6e 42 79  CP_OEMCP;..  nBy
f7300 74 65 20 3d 20 57 69 64 65 43 68 61 72 54 6f 4d  te = WideCharToM
f7310 75 6c 74 69 42 79 74 65 28 63 6f 64 65 70 61 67  ultiByte(codepag
f7320 65 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 6e  e, 0, zWideFilen
f7330 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 30  ame, -1, 0, 0, 0
f7340 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 65 6e 61 6d  , 0);.  zFilenam
f7350 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74  e = malloc( nByt
f7360 65 20 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65  e );.  if( zFile
f7370 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72  name==0 ){.    r
f7380 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e  eturn 0;.  }.  n
f7390 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 72 54  Byte = WideCharT
f73a0 6f 4d 75 6c 74 69 42 79 74 65 28 63 6f 64 65 70  oMultiByte(codep
f73b0 61 67 65 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c  age, 0, zWideFil
f73c0 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c 65  ename, -1, zFile
f73d0 6e 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 20 20  name, nByte,.   
f73e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f73f0 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 29             0, 0)
f7400 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3d 3d  ;.  if( nByte ==
f7410 20 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a   0 ){.    free(z
f7420 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a  Filename);.    z
f7430 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20  Filename = 0;.  
f7440 7d 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65  }.  return zFile
f7450 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  name;.}../*.** C
f7460 6f 6e 76 65 72 74 20 6d 75 6c 74 69 62 79 74 65  onvert multibyte
f7470 20 63 68 61 72 61 63 74 65 72 20 73 74 72 69 6e   character strin
f7480 67 20 74 6f 20 55 54 46 2d 38 2e 20 20 53 70 61  g to UTF-8.  Spa
f7490 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a  ce to hold the.*
f74a0 2a 20 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e  * returned strin
f74b0 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72  g is obtained fr
f74c0 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a  om malloc()..*/.
f74d0 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20  SQLITE_API char 
f74e0 2a 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d  *sqlite3_win32_m
f74f0 62 63 73 5f 74 6f 5f 75 74 66 38 28 63 6f 6e 73  bcs_to_utf8(cons
f7500 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
f7510 65 29 7b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c  e){.  char *zFil
f7520 65 6e 61 6d 65 55 74 66 38 3b 0a 20 20 57 43 48  enameUtf8;.  WCH
f7530 41 52 20 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20  AR *zTmpWide;.. 
f7540 20 7a 54 6d 70 57 69 64 65 20 3d 20 6d 62 63 73   zTmpWide = mbcs
f7550 54 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e  ToUnicode(zFilen
f7560 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70  ame);.  if( zTmp
f7570 57 69 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72  Wide==0 ){.    r
f7580 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a  eturn 0;.  }.  z
f7590 46 69 6c 65 6e 61 6d 65 55 74 66 38 20 3d 20 75  FilenameUtf8 = u
f75a0 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 6d  nicodeToUtf8(zTm
f75b0 70 57 69 64 65 29 3b 0a 20 20 66 72 65 65 28 7a  pWide);.  free(z
f75c0 54 6d 70 57 69 64 65 29 3b 0a 20 20 72 65 74 75  TmpWide);.  retu
f75d0 72 6e 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38  rn zFilenameUtf8
f75e0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
f75f0 72 74 20 55 54 46 2d 38 20 74 6f 20 6d 75 6c 74  rt UTF-8 to mult
f7600 69 62 79 74 65 20 63 68 61 72 61 63 74 65 72 20  ibyte character 
f7610 73 74 72 69 6e 67 2e 20 20 53 70 61 63 65 20 74  string.  Space t
f7620 6f 20 68 6f 6c 64 20 74 68 65 20 0a 2a 2a 20 72  o hold the .** r
f7630 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69  eturned string i
f7640 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  s obtained from 
f7650 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61  malloc()..*/.sta
f7660 74 69 63 20 63 68 61 72 20 2a 75 74 66 38 54 6f  tic char *utf8To
f7670 4d 62 63 73 28 63 6f 6e 73 74 20 63 68 61 72 20  Mbcs(const char 
f7680 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 63  *zFilename){.  c
f7690 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 4d 62  har *zFilenameMb
f76a0 63 73 3b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6d  cs;.  WCHAR *zTm
f76b0 70 57 69 64 65 3b 0a 0a 20 20 7a 54 6d 70 57 69  pWide;..  zTmpWi
f76c0 64 65 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f  de = utf8ToUnico
f76d0 64 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  de(zFilename);. 
f76e0 20 69 66 28 20 7a 54 6d 70 57 69 64 65 3d 3d 30   if( zTmpWide==0
f76f0 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
f7700 3b 0a 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d  ;.  }.  zFilenam
f7710 65 4d 62 63 73 20 3d 20 75 6e 69 63 6f 64 65 54  eMbcs = unicodeT
f7720 6f 4d 62 63 73 28 7a 54 6d 70 57 69 64 65 29 3b  oMbcs(zTmpWide);
f7730 0a 20 20 66 72 65 65 28 7a 54 6d 70 57 69 64 65  .  free(zTmpWide
f7740 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c  );.  return zFil
f7750 65 6e 61 6d 65 4d 62 63 73 3b 0a 7d 0a 0a 23 69  enameMbcs;.}..#i
f7760 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
f7770 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  E./*************
f7780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f77a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f77b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
f77c0 54 68 69 73 20 73 65 63 74 69 6f 6e 20 63 6f 6e  This section con
f77d0 74 61 69 6e 73 20 63 6f 64 65 20 66 6f 72 20 57  tains code for W
f77e0 69 6e 43 45 20 6f 6e 6c 79 2e 0a 2a 2f 0a 2f 2a  inCE only..*/./*
f77f0 0a 2a 2a 20 57 69 6e 64 6f 77 73 43 45 20 64 6f  .** WindowsCE do
f7800 65 73 20 6e 6f 74 20 68 61 76 65 20 61 20 6c 6f  es not have a lo
f7810 63 61 6c 74 69 6d 65 28 29 20 66 75 6e 63 74 69  caltime() functi
f7820 6f 6e 2e 20 20 53 6f 20 63 72 65 61 74 65 20 61  on.  So create a
f7830 0a 2a 2a 20 73 75 62 73 74 69 74 75 74 65 2e 0a  .** substitute..
f7840 2a 2f 0a 73 74 72 75 63 74 20 74 6d 20 2a 5f 5f  */.struct tm *__
f7850 63 64 65 63 6c 20 6c 6f 63 61 6c 74 69 6d 65 28  cdecl localtime(
f7860 63 6f 6e 73 74 20 74 69 6d 65 5f 74 20 2a 74 29  const time_t *t)
f7870 0a 7b 0a 20 20 73 74 61 74 69 63 20 73 74 72 75  .{.  static stru
f7880 63 74 20 74 6d 20 79 3b 0a 20 20 46 49 4c 45 54  ct tm y;.  FILET
f7890 49 4d 45 20 75 54 6d 2c 20 6c 54 6d 3b 0a 20 20  IME uTm, lTm;.  
f78a0 53 59 53 54 45 4d 54 49 4d 45 20 70 54 6d 3b 0a  SYSTEMTIME pTm;.
f78b0 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
f78c0 74 36 34 3b 0a 20 20 74 36 34 20 3d 20 2a 74 3b  t64;.  t64 = *t;
f78d0 0a 20 20 74 36 34 20 3d 20 28 74 36 34 20 2b 20  .  t64 = (t64 + 
f78e0 31 31 36 34 34 34 37 33 36 30 30 29 2a 31 30 30  11644473600)*100
f78f0 30 30 30 30 30 3b 0a 20 20 75 54 6d 2e 64 77 4c  00000;.  uTm.dwL
f7900 6f 77 44 61 74 65 54 69 6d 65 20 3d 20 28 44 57  owDateTime = (DW
f7910 4f 52 44 29 28 74 36 34 20 26 20 30 78 46 46 46  ORD)(t64 & 0xFFF
f7920 46 46 46 46 46 29 3b 0a 20 20 75 54 6d 2e 64 77  FFFFF);.  uTm.dw
f7930 48 69 67 68 44 61 74 65 54 69 6d 65 3d 20 28 44  HighDateTime= (D
f7940 57 4f 52 44 29 28 74 36 34 20 3e 3e 20 33 32 29  WORD)(t64 >> 32)
f7950 3b 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f 4c 6f  ;.  FileTimeToLo
f7960 63 61 6c 46 69 6c 65 54 69 6d 65 28 26 75 54 6d  calFileTime(&uTm
f7970 2c 26 6c 54 6d 29 3b 0a 20 20 46 69 6c 65 54 69  ,&lTm);.  FileTi
f7980 6d 65 54 6f 53 79 73 74 65 6d 54 69 6d 65 28 26  meToSystemTime(&
f7990 6c 54 6d 2c 26 70 54 6d 29 3b 0a 20 20 79 2e 74  lTm,&pTm);.  y.t
f79a0 6d 5f 79 65 61 72 20 3d 20 70 54 6d 2e 77 59 65  m_year = pTm.wYe
f79b0 61 72 20 2d 20 31 39 30 30 3b 0a 20 20 79 2e 74  ar - 1900;.  y.t
f79c0 6d 5f 6d 6f 6e 20 3d 20 70 54 6d 2e 77 4d 6f 6e  m_mon = pTm.wMon
f79d0 74 68 20 2d 20 31 3b 0a 20 20 79 2e 74 6d 5f 77  th - 1;.  y.tm_w
f79e0 64 61 79 20 3d 20 70 54 6d 2e 77 44 61 79 4f 66  day = pTm.wDayOf
f79f0 57 65 65 6b 3b 0a 20 20 79 2e 74 6d 5f 6d 64 61  Week;.  y.tm_mda
f7a00 79 20 3d 20 70 54 6d 2e 77 44 61 79 3b 0a 20 20  y = pTm.wDay;.  
f7a10 79 2e 74 6d 5f 68 6f 75 72 20 3d 20 70 54 6d 2e  y.tm_hour = pTm.
f7a20 77 48 6f 75 72 3b 0a 20 20 79 2e 74 6d 5f 6d 69  wHour;.  y.tm_mi
f7a30 6e 20 3d 20 70 54 6d 2e 77 4d 69 6e 75 74 65 3b  n = pTm.wMinute;
f7a40 0a 20 20 79 2e 74 6d 5f 73 65 63 20 3d 20 70 54  .  y.tm_sec = pT
f7a50 6d 2e 77 53 65 63 6f 6e 64 3b 0a 20 20 72 65 74  m.wSecond;.  ret
f7a60 75 72 6e 20 26 79 3b 0a 7d 0a 0a 2f 2a 20 54 68  urn &y;.}../* Th
f7a70 69 73 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65  is will never be
f7a80 20 63 61 6c 6c 65 64 2c 20 62 75 74 20 64 65 66   called, but def
f7a90 69 6e 65 64 20 74 6f 20 6d 61 6b 65 20 74 68 65  ined to make the
f7aa0 20 63 6f 64 65 20 63 6f 6d 70 69 6c 65 20 2a 2f   code compile */
f7ab0 0a 23 64 65 66 69 6e 65 20 47 65 74 54 65 6d 70  .#define GetTemp
f7ac0 50 61 74 68 41 28 61 2c 62 29 0a 0a 23 64 65 66  PathA(a,b)..#def
f7ad0 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 28 61 2c 62  ine LockFile(a,b
f7ae0 2c 63 2c 64 2c 65 29 20 20 20 20 20 20 20 77 69  ,c,d,e)       wi
f7af0 6e 63 65 4c 6f 63 6b 46 69 6c 65 28 26 61 2c 20  nceLockFile(&a, 
f7b00 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64 65 66  b, c, d, e).#def
f7b10 69 6e 65 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 61  ine UnlockFile(a
f7b20 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20 20 77 69  ,b,c,d,e)     wi
f7b30 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 26 61  nceUnlockFile(&a
f7b40 2c 20 62 2c 20 63 2c 20 64 2c 20 65 29 0a 23 64  , b, c, d, e).#d
f7b50 65 66 69 6e 65 20 4c 6f 63 6b 46 69 6c 65 45 78  efine LockFileEx
f7b60 28 61 2c 62 2c 63 2c 64 2c 65 2c 66 29 20 20 20  (a,b,c,d,e,f)   
f7b70 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28  winceLockFileEx(
f7b80 26 61 2c 20 62 2c 20 63 2c 20 64 2c 20 65 2c 20  &a, b, c, d, e, 
f7b90 66 29 0a 0a 23 64 65 66 69 6e 65 20 48 41 4e 44  f)..#define HAND
f7ba0 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28 61 29  LE_TO_WINFILE(a)
f7bb0 20 28 77 69 6e 46 69 6c 65 2a 29 26 28 28 63 68   (winFile*)&((ch
f7bc0 61 72 2a 29 61 29 5b 2d 28 69 6e 74 29 6f 66 66  ar*)a)[-(int)off
f7bd0 73 65 74 6f 66 28 77 69 6e 46 69 6c 65 2c 68 29  setof(winFile,h)
f7be0 5d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65  ]../*.** Acquire
f7bf0 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 68   a lock on the h
f7c00 61 6e 64 6c 65 20 68 0a 2a 2f 0a 73 74 61 74 69  andle h.*/.stati
f7c10 63 20 76 6f 69 64 20 77 69 6e 63 65 4d 75 74 65  c void winceMute
f7c20 78 41 63 71 75 69 72 65 28 48 41 4e 44 4c 45 20  xAcquire(HANDLE 
f7c30 68 29 7b 0a 20 20 20 44 57 4f 52 44 20 64 77 45  h){.   DWORD dwE
f7c40 72 72 3b 0a 20 20 20 64 6f 20 7b 0a 20 20 20 20  rr;.   do {.    
f7c50 20 64 77 45 72 72 20 3d 20 57 61 69 74 46 6f 72   dwErr = WaitFor
f7c60 53 69 6e 67 6c 65 4f 62 6a 65 63 74 28 68 2c 20  SingleObject(h, 
f7c70 49 4e 46 49 4e 49 54 45 29 3b 0a 20 20 20 7d 20  INFINITE);.   } 
f7c80 77 68 69 6c 65 20 28 64 77 45 72 72 20 21 3d 20  while (dwErr != 
f7c90 57 41 49 54 5f 4f 42 4a 45 43 54 5f 30 20 26 26  WAIT_OBJECT_0 &&
f7ca0 20 64 77 45 72 72 20 21 3d 20 57 41 49 54 5f 41   dwErr != WAIT_A
f7cb0 42 41 4e 44 4f 4e 45 44 29 3b 0a 7d 0a 2f 2a 0a  BANDONED);.}./*.
f7cc0 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 6c 6f 63  ** Release a loc
f7cd0 6b 20 61 63 71 75 69 72 65 64 20 62 79 20 77 69  k acquired by wi
f7ce0 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28  nceMutexAcquire(
f7cf0 29 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 77 69 6e  ).*/.#define win
f7d00 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 68  ceMutexRelease(h
f7d10 29 20 52 65 6c 65 61 73 65 4d 75 74 65 78 28 68  ) ReleaseMutex(h
f7d20 29 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  )../*.** Create 
f7d30 74 68 65 20 6d 75 74 65 78 20 61 6e 64 20 73 68  the mutex and sh
f7d40 61 72 65 64 20 6d 65 6d 6f 72 79 20 75 73 65 64  ared memory used
f7d50 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 20 69 6e 20   for locking in 
f7d60 74 68 65 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63  the file.** desc
f7d70 72 69 70 74 6f 72 20 70 46 69 6c 65 0a 2a 2f 0a  riptor pFile.*/.
f7d80 73 74 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63  static BOOL winc
f7d90 65 43 72 65 61 74 65 4c 6f 63 6b 28 63 6f 6e 73  eCreateLock(cons
f7da0 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
f7db0 65 2c 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c  e, winFile *pFil
f7dc0 65 29 7b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6f  e){.  WCHAR *zTo
f7dd0 6b 3b 0a 20 20 57 43 48 41 52 20 2a 7a 4e 61 6d  k;.  WCHAR *zNam
f7de0 65 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f 64  e = utf8ToUnicod
f7df0 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  e(zFilename);.  
f7e00 42 4f 4f 4c 20 62 49 6e 69 74 20 3d 20 54 52 55  BOOL bInit = TRU
f7e10 45 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c  E;..  /* Initial
f7e20 69 7a 65 20 74 68 65 20 6c 6f 63 61 6c 20 6c 6f  ize the local lo
f7e30 63 6b 64 61 74 61 20 2a 2f 0a 20 20 5a 65 72 6f  ckdata */.  Zero
f7e40 4d 65 6d 6f 72 79 28 26 70 46 69 6c 65 2d 3e 6c  Memory(&pFile->l
f7e50 6f 63 61 6c 2c 20 73 69 7a 65 6f 66 28 70 46 69  ocal, sizeof(pFi
f7e60 6c 65 2d 3e 6c 6f 63 61 6c 29 29 3b 0a 0a 20 20  le->local));..  
f7e70 2f 2a 20 52 65 70 6c 61 63 65 20 74 68 65 20 62  /* Replace the b
f7e80 61 63 6b 73 6c 61 73 68 65 73 20 66 72 6f 6d 20  ackslashes from 
f7e90 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 61 6e 64  the filename and
f7ea0 20 6c 6f 77 65 72 63 61 73 65 20 69 74 0a 20 20   lowercase it.  
f7eb0 2a 2a 20 74 6f 20 64 65 72 69 76 65 20 61 20 6d  ** to derive a m
f7ec0 75 74 65 78 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20  utex name. */.  
f7ed0 7a 54 6f 6b 20 3d 20 43 68 61 72 4c 6f 77 65 72  zTok = CharLower
f7ee0 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 20  W(zName);.  for 
f7ef0 28 3b 2a 7a 54 6f 6b 3b 7a 54 6f 6b 2b 2b 29 7b  (;*zTok;zTok++){
f7f00 0a 20 20 20 20 69 66 20 28 2a 7a 54 6f 6b 20 3d  .    if (*zTok =
f7f10 3d 20 27 5c 5c 27 29 20 2a 7a 54 6f 6b 20 3d 20  = '\\') *zTok = 
f7f20 27 5f 27 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  '_';.  }..  /* C
f7f30 72 65 61 74 65 2f 6f 70 65 6e 20 74 68 65 20 6e  reate/open the n
f7f40 61 6d 65 64 20 6d 75 74 65 78 20 2a 2f 0a 20 20  amed mutex */.  
f7f50 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20 3d 20  pFile->hMutex = 
f7f60 43 72 65 61 74 65 4d 75 74 65 78 57 28 4e 55 4c  CreateMutexW(NUL
f7f70 4c 2c 20 46 41 4c 53 45 2c 20 7a 4e 61 6d 65 29  L, FALSE, zName)
f7f80 3b 0a 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e  ;.  if (!pFile->
f7f90 68 4d 75 74 65 78 29 7b 0a 20 20 20 20 70 46 69  hMutex){.    pFi
f7fa0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
f7fb0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a  GetLastError();.
f7fc0 20 20 20 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b      free(zName);
f7fd0 0a 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53  .    return FALS
f7fe0 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71  E;.  }..  /* Acq
f7ff0 75 69 72 65 20 74 68 65 20 6d 75 74 65 78 20 62  uire the mutex b
f8000 65 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67  efore continuing
f8010 20 2a 2f 0a 20 20 77 69 6e 63 65 4d 75 74 65 78   */.  winceMutex
f8020 41 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68  Acquire(pFile->h
f8030 4d 75 74 65 78 29 3b 0a 20 20 0a 20 20 2f 2a 20  Mutex);.  .  /* 
f8040 53 69 6e 63 65 20 74 68 65 20 6e 61 6d 65 73 20  Since the names 
f8050 6f 66 20 6e 61 6d 65 64 20 6d 75 74 65 78 65 73  of named mutexes
f8060 2c 20 73 65 6d 61 70 68 6f 72 65 73 2c 20 66 69  , semaphores, fi
f8070 6c 65 20 6d 61 70 70 69 6e 67 73 20 65 74 63 20  le mappings etc 
f8080 61 72 65 20 0a 20 20 2a 2a 20 63 61 73 65 2d 73  are .  ** case-s
f8090 65 6e 73 69 74 69 76 65 2c 20 74 61 6b 65 20 61  ensitive, take a
f80a0 64 76 61 6e 74 61 67 65 20 6f 66 20 74 68 61 74  dvantage of that
f80b0 20 62 79 20 75 70 70 65 72 63 61 73 69 6e 67 20   by uppercasing 
f80c0 74 68 65 20 6d 75 74 65 78 20 6e 61 6d 65 0a 20  the mutex name. 
f80d0 20 2a 2a 20 61 6e 64 20 75 73 69 6e 67 20 74 68   ** and using th
f80e0 61 74 20 61 73 20 74 68 65 20 73 68 61 72 65 64  at as the shared
f80f0 20 66 69 6c 65 6d 61 70 70 69 6e 67 20 6e 61 6d   filemapping nam
f8100 65 2e 0a 20 20 2a 2f 0a 20 20 43 68 61 72 55 70  e..  */.  CharUp
f8110 70 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 70  perW(zName);.  p
f8120 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 20  File->hShared = 
f8130 43 72 65 61 74 65 46 69 6c 65 4d 61 70 70 69 6e  CreateFileMappin
f8140 67 57 28 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c  gW(INVALID_HANDL
f8150 45 5f 56 41 4c 55 45 2c 20 4e 55 4c 4c 2c 0a 20  E_VALUE, NULL,. 
f8160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8180 20 20 20 20 20 20 50 41 47 45 5f 52 45 41 44 57        PAGE_READW
f8190 52 49 54 45 2c 20 30 2c 20 73 69 7a 65 6f 66 28  RITE, 0, sizeof(
f81a0 77 69 6e 63 65 4c 6f 63 6b 29 2c 0a 20 20 20 20  winceLock),.    
f81b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f81c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f81d0 20 20 20 7a 4e 61 6d 65 29 3b 20 20 0a 0a 20 20     zName);  ..  
f81e0 2f 2a 20 53 65 74 20 61 20 66 6c 61 67 20 74 68  /* Set a flag th
f81f0 61 74 20 69 6e 64 69 63 61 74 65 73 20 77 65 27  at indicates we'
f8200 72 65 20 74 68 65 20 66 69 72 73 74 20 74 6f 20  re the first to 
f8210 63 72 65 61 74 65 20 74 68 65 20 6d 65 6d 6f 72  create the memor
f8220 79 20 73 6f 20 69 74 20 0a 20 20 2a 2a 20 6d 75  y so it .  ** mu
f8230 73 74 20 62 65 20 7a 65 72 6f 2d 69 6e 69 74 69  st be zero-initi
f8240 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 66 20 28  alized */.  if (
f8250 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20 3d  GetLastError() =
f8260 3d 20 45 52 52 4f 52 5f 41 4c 52 45 41 44 59 5f  = ERROR_ALREADY_
f8270 45 58 49 53 54 53 29 7b 0a 20 20 20 20 62 49 6e  EXISTS){.    bIn
f8280 69 74 20 3d 20 46 41 4c 53 45 3b 0a 20 20 7d 0a  it = FALSE;.  }.
f8290 0a 20 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a  .  free(zName);.
f82a0 0a 20 20 2f 2a 20 49 66 20 77 65 20 73 75 63 63  .  /* If we succ
f82b0 65 65 64 65 64 20 69 6e 20 6d 61 6b 69 6e 67 20  eeded in making 
f82c0 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72  the shared memor
f82d0 79 20 68 61 6e 64 6c 65 2c 20 6d 61 70 20 69 74  y handle, map it
f82e0 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65  . */.  if (pFile
f82f0 2d 3e 68 53 68 61 72 65 64 29 7b 0a 20 20 20 20  ->hShared){.    
f8300 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 20 3d 20  pFile->shared = 
f8310 28 77 69 6e 63 65 4c 6f 63 6b 2a 29 4d 61 70 56  (winceLock*)MapV
f8320 69 65 77 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d  iewOfFile(pFile-
f8330 3e 68 53 68 61 72 65 64 2c 20 0a 20 20 20 20 20  >hShared, .     
f8340 20 20 20 20 20 20 20 20 46 49 4c 45 5f 4d 41 50          FILE_MAP
f8350 5f 52 45 41 44 7c 46 49 4c 45 5f 4d 41 50 5f 57  _READ|FILE_MAP_W
f8360 52 49 54 45 2c 20 30 2c 20 30 2c 20 73 69 7a 65  RITE, 0, 0, size
f8370 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a  of(winceLock));.
f8380 20 20 20 20 2f 2a 20 49 66 20 6d 61 70 70 69 6e      /* If mappin
f8390 67 20 66 61 69 6c 65 64 2c 20 63 6c 6f 73 65 20  g failed, close 
f83a0 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72  the shared memor
f83b0 79 20 68 61 6e 64 6c 65 20 61 6e 64 20 65 72 61  y handle and era
f83c0 73 65 20 69 74 20 2a 2f 0a 20 20 20 20 69 66 20  se it */.    if 
f83d0 28 21 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 29  (!pFile->shared)
f83e0 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  {.      pFile->l
f83f0 61 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61  astErrno = GetLa
f8400 73 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 20  stError();.     
f8410 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69   CloseHandle(pFi
f8420 6c 65 2d 3e 68 53 68 61 72 65 64 29 3b 0a 20 20  le->hShared);.  
f8430 20 20 20 20 70 46 69 6c 65 2d 3e 68 53 68 61 72      pFile->hShar
f8440 65 64 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d  ed = NULL;.    }
f8450 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 73 68  .  }..  /* If sh
f8460 61 72 65 64 20 6d 65 6d 6f 72 79 20 63 6f 75 6c  ared memory coul
f8470 64 20 6e 6f 74 20 62 65 20 63 72 65 61 74 65 64  d not be created
f8480 2c 20 74 68 65 6e 20 63 6c 6f 73 65 20 74 68 65  , then close the
f8490 20 6d 75 74 65 78 20 61 6e 64 20 66 61 69 6c 20   mutex and fail 
f84a0 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  */.  if (pFile->
f84b0 68 53 68 61 72 65 64 20 3d 3d 20 4e 55 4c 4c 29  hShared == NULL)
f84c0 7b 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78  {.    winceMutex
f84d0 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68  Release(pFile->h
f84e0 4d 75 74 65 78 29 3b 0a 20 20 20 20 43 6c 6f 73  Mutex);.    Clos
f84f0 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68  eHandle(pFile->h
f8500 4d 75 74 65 78 29 3b 0a 20 20 20 20 70 46 69 6c  Mutex);.    pFil
f8510 65 2d 3e 68 4d 75 74 65 78 20 3d 20 4e 55 4c 4c  e->hMutex = NULL
f8520 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c  ;.    return FAL
f8530 53 45 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20  SE;.  }.  .  /* 
f8540 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 73  Initialize the s
f8550 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 69 66 20  hared memory if 
f8560 77 65 27 72 65 20 73 75 70 70 6f 73 65 64 20 74  we're supposed t
f8570 6f 20 2a 2f 0a 20 20 69 66 20 28 62 49 6e 69 74  o */.  if (bInit
f8580 29 20 7b 0a 20 20 20 20 5a 65 72 6f 4d 65 6d 6f  ) {.    ZeroMemo
f8590 72 79 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64  ry(pFile->shared
f85a0 2c 20 73 69 7a 65 6f 66 28 77 69 6e 63 65 4c 6f  , sizeof(winceLo
f85b0 63 6b 29 29 3b 0a 20 20 7d 0a 0a 20 20 77 69 6e  ck));.  }..  win
f85c0 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70  ceMutexRelease(p
f85d0 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20  File->hMutex);. 
f85e0 20 72 65 74 75 72 6e 20 54 52 55 45 3b 0a 7d 0a   return TRUE;.}.
f85f0 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 74  ./*.** Destroy t
f8600 68 65 20 70 61 72 74 20 6f 66 20 77 69 6e 46 69  he part of winFi
f8610 6c 65 20 74 68 61 74 20 64 65 61 6c 73 20 77 69  le that deals wi
f8620 74 68 20 77 69 6e 63 65 20 6c 6f 63 6b 73 0a 2a  th wince locks.*
f8630 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69  /.static void wi
f8640 6e 63 65 44 65 73 74 72 6f 79 4c 6f 63 6b 28 77  nceDestroyLock(w
f8650 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a  inFile *pFile){.
f8660 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 68 4d 75    if (pFile->hMu
f8670 74 65 78 29 7b 0a 20 20 20 20 2f 2a 20 41 63 71  tex){.    /* Acq
f8680 75 69 72 65 20 74 68 65 20 6d 75 74 65 78 20 2a  uire the mutex *
f8690 2f 0a 20 20 20 20 77 69 6e 63 65 4d 75 74 65 78  /.    winceMutex
f86a0 41 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68  Acquire(pFile->h
f86b0 4d 75 74 65 78 29 3b 0a 0a 20 20 20 20 2f 2a 20  Mutex);..    /* 
f86c0 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c  The following bl
f86d0 6f 63 6b 73 20 73 68 6f 75 6c 64 20 70 72 6f 62  ocks should prob
f86e0 61 62 6c 79 20 61 73 73 65 72 74 20 69 6e 20 64  ably assert in d
f86f0 65 62 75 67 20 6d 6f 64 65 2c 20 62 75 74 20 74  ebug mode, but t
f8700 68 65 79 0a 20 20 20 20 20 20 20 61 72 65 20 74  hey.       are t
f8710 6f 20 63 6c 65 61 6e 75 70 20 69 6e 20 63 61 73  o cleanup in cas
f8720 65 20 61 6e 79 20 6c 6f 63 6b 73 20 72 65 6d 61  e any locks rema
f8730 69 6e 65 64 20 6f 70 65 6e 20 2a 2f 0a 20 20 20  ined open */.   
f8740 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61   if (pFile->loca
f8750 6c 2e 6e 52 65 61 64 65 72 73 29 7b 0a 20 20 20  l.nReaders){.   
f8760 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64     pFile->shared
f8770 2d 3e 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a 20  ->nReaders --;. 
f8780 20 20 20 7d 0a 20 20 20 20 69 66 20 28 70 46 69     }.    if (pFi
f8790 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73 65 72  le->local.bReser
f87a0 76 65 64 29 7b 0a 20 20 20 20 20 20 70 46 69 6c  ved){.      pFil
f87b0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65  e->shared->bRese
f87c0 72 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20 20  rved = FALSE;.  
f87d0 20 20 7d 0a 20 20 20 20 69 66 20 28 70 46 69 6c    }.    if (pFil
f87e0 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e  e->local.bPendin
f87f0 67 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  g){.      pFile-
f8800 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e  >shared->bPendin
f8810 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 7d  g = FALSE;.    }
f8820 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
f8830 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 65  local.bExclusive
f8840 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  ){.      pFile->
f8850 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69  shared->bExclusi
f8860 76 65 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20  ve = FALSE;.    
f8870 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 2d 72 65 66  }..    /* De-ref
f8880 65 72 65 6e 63 65 20 61 6e 64 20 63 6c 6f 73 65  erence and close
f8890 20 6f 75 72 20 63 6f 70 79 20 6f 66 20 74 68 65   our copy of the
f88a0 20 73 68 61 72 65 64 20 6d 65 6d 6f 72 79 20 68   shared memory h
f88b0 61 6e 64 6c 65 20 2a 2f 0a 20 20 20 20 55 6e 6d  andle */.    Unm
f88c0 61 70 56 69 65 77 4f 66 46 69 6c 65 28 70 46 69  apViewOfFile(pFi
f88d0 6c 65 2d 3e 73 68 61 72 65 64 29 3b 0a 20 20 20  le->shared);.   
f88e0 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46 69   CloseHandle(pFi
f88f0 6c 65 2d 3e 68 53 68 61 72 65 64 29 3b 0a 0a 20  le->hShared);.. 
f8900 20 20 20 2f 2a 20 44 6f 6e 65 20 77 69 74 68 20     /* Done with 
f8910 74 68 65 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20  the mutex */.   
f8920 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65 61   winceMutexRelea
f8930 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78  se(pFile->hMutex
f8940 29 3b 20 20 20 20 0a 20 20 20 20 43 6c 6f 73 65  );    .    Close
f8950 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e 68 4d  Handle(pFile->hM
f8960 75 74 65 78 29 3b 0a 20 20 20 20 70 46 69 6c 65  utex);.    pFile
f8970 2d 3e 68 4d 75 74 65 78 20 3d 20 4e 55 4c 4c 3b  ->hMutex = NULL;
f8980 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 41  .  }.}../* .** A
f8990 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
f89a0 20 6f 66 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65   of the LockFile
f89b0 28 29 20 41 50 49 20 6f 66 20 77 69 6e 64 6f 77  () API of window
f89c0 73 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2f 0a 73  s for wince.*/.s
f89d0 74 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e 63 65  tatic BOOL wince
f89e0 4c 6f 63 6b 46 69 6c 65 28 0a 20 20 48 41 4e 44  LockFile(.  HAND
f89f0 4c 45 20 2a 70 68 46 69 6c 65 2c 0a 20 20 44 57  LE *phFile,.  DW
f8a00 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74  ORD dwFileOffset
f8a10 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 64 77 46  Low,.  DWORD dwF
f8a20 69 6c 65 4f 66 66 73 65 74 48 69 67 68 2c 0a 20  ileOffsetHigh,. 
f8a30 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66   DWORD nNumberOf
f8a40 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a  BytesToLockLow,.
f8a50 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f    DWORD nNumberO
f8a60 66 42 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68  fBytesToLockHigh
f8a70 0a 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70  .){.  winFile *p
f8a80 46 69 6c 65 20 3d 20 48 41 4e 44 4c 45 5f 54 4f  File = HANDLE_TO
f8a90 5f 57 49 4e 46 49 4c 45 28 70 68 46 69 6c 65 29  _WINFILE(phFile)
f8aa0 3b 0a 20 20 42 4f 4f 4c 20 62 52 65 74 75 72 6e  ;.  BOOL bReturn
f8ab0 20 3d 20 46 41 4c 53 45 3b 0a 0a 20 20 55 4e 55   = FALSE;..  UNU
f8ac0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64 77  SED_PARAMETER(dw
f8ad0 46 69 6c 65 4f 66 66 73 65 74 48 69 67 68 29 3b  FileOffsetHigh);
f8ae0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
f8af0 54 45 52 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74  TER(nNumberOfByt
f8b00 65 73 54 6f 4c 6f 63 6b 48 69 67 68 29 3b 0a 0a  esToLockHigh);..
f8b10 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e 68 4d    if (!pFile->hM
f8b20 75 74 65 78 29 20 72 65 74 75 72 6e 20 54 52 55  utex) return TRU
f8b30 45 3b 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 41  E;.  winceMutexA
f8b40 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d  cquire(pFile->hM
f8b50 75 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 57 61 6e  utex);..  /* Wan
f8b60 74 69 6e 67 20 61 6e 20 65 78 63 6c 75 73 69 76  ting an exclusiv
f8b70 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 69 66 20  e lock? */.  if 
f8b80 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77  (dwFileOffsetLow
f8b90 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45   == (DWORD)SHARE
f8ba0 44 5f 46 49 52 53 54 0a 20 20 20 20 20 20 20 26  D_FIRST.       &
f8bb0 26 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73  & nNumberOfBytes
f8bc0 54 6f 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44 57  ToLockLow == (DW
f8bd0 4f 52 44 29 53 48 41 52 45 44 5f 53 49 5a 45 29  ORD)SHARED_SIZE)
f8be0 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d  {.    if (pFile-
f8bf0 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72  >shared->nReader
f8c00 73 20 3d 3d 20 30 20 26 26 20 70 46 69 6c 65 2d  s == 0 && pFile-
f8c10 3e 73 68 61 72 65 64 2d 3e 62 45 78 63 6c 75 73  >shared->bExclus
f8c20 69 76 65 20 3d 3d 20 30 29 7b 0a 20 20 20 20 20  ive == 0){.     
f8c30 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d    pFile->shared-
f8c40 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 20 54 52  >bExclusive = TR
f8c50 55 45 3b 0a 20 20 20 20 20 20 20 70 46 69 6c 65  UE;.       pFile
f8c60 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69  ->local.bExclusi
f8c70 76 65 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20  ve = TRUE;.     
f8c80 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45    bReturn = TRUE
f8c90 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
f8ca0 2a 20 57 61 6e 74 20 61 20 72 65 61 64 2d 6f 6e  * Want a read-on
f8cb0 6c 79 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65 6c  ly lock? */.  el
f8cc0 73 65 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66  se if (dwFileOff
f8cd0 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44  setLow == (DWORD
f8ce0 29 53 48 41 52 45 44 5f 46 49 52 53 54 20 26 26  )SHARED_FIRST &&
f8cf0 0a 20 20 20 20 20 20 20 20 20 20 20 6e 4e 75 6d  .           nNum
f8d00 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b  berOfBytesToLock
f8d10 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 69  Low == 1){.    i
f8d20 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64  f (pFile->shared
f8d30 2d 3e 62 45 78 63 6c 75 73 69 76 65 20 3d 3d 20  ->bExclusive == 
f8d40 30 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  0){.      pFile-
f8d50 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20  >local.nReaders 
f8d60 2b 2b 3b 0a 20 20 20 20 20 20 69 66 20 28 70 46  ++;.      if (pF
f8d70 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64  ile->local.nRead
f8d80 65 72 73 20 3d 3d 20 31 29 7b 0a 20 20 20 20 20  ers == 1){.     
f8d90 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64     pFile->shared
f8da0 2d 3e 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a 20  ->nReaders ++;. 
f8db0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 52 65       }.      bRe
f8dc0 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20  turn = TRUE;.   
f8dd0 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e   }.  }..  /* Wan
f8de0 74 20 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b  t a pending lock
f8df0 3f 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 20 28  ? */.  else if (
f8e00 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20  dwFileOffsetLow 
f8e10 3d 3d 20 28 44 57 4f 52 44 29 50 45 4e 44 49 4e  == (DWORD)PENDIN
f8e20 47 5f 42 59 54 45 20 26 26 20 6e 4e 75 6d 62 65  G_BYTE && nNumbe
f8e30 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f  rOfBytesToLockLo
f8e40 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20 2f 2a 20  w == 1){.    /* 
f8e50 49 66 20 6e 6f 20 70 65 6e 64 69 6e 67 20 6c 6f  If no pending lo
f8e60 63 6b 20 68 61 73 20 62 65 65 6e 20 61 63 71 75  ck has been acqu
f8e70 69 72 65 64 2c 20 74 68 65 6e 20 61 63 71 75 69  ired, then acqui
f8e80 72 65 20 69 74 20 2a 2f 0a 20 20 20 20 69 66 20  re it */.    if 
f8e90 28 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e  (pFile->shared->
f8ea0 62 50 65 6e 64 69 6e 67 20 3d 3d 20 30 29 20 7b  bPending == 0) {
f8eb0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68  .      pFile->sh
f8ec0 61 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d  ared->bPending =
f8ed0 20 54 52 55 45 3b 0a 20 20 20 20 20 20 70 46 69   TRUE;.      pFi
f8ee0 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69  le->local.bPendi
f8ef0 6e 67 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 20  ng = TRUE;.     
f8f00 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b   bReturn = TRUE;
f8f10 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
f8f20 20 57 61 6e 74 20 61 20 72 65 73 65 72 76 65 64   Want a reserved
f8f30 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 65 6c 73 65   lock? */.  else
f8f40 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65   if (dwFileOffse
f8f50 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29 52  tLow == (DWORD)R
f8f60 45 53 45 52 56 45 44 5f 42 59 54 45 20 26 26 20  ESERVED_BYTE && 
f8f70 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
f8f80 4c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20  LockLow == 1){. 
f8f90 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68     if (pFile->sh
f8fa0 61 72 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20  ared->bReserved 
f8fb0 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70 46  == 0) {.      pF
f8fc0 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65  ile->shared->bRe
f8fd0 73 65 72 76 65 64 20 3d 20 54 52 55 45 3b 0a 20  served = TRUE;. 
f8fe0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61       pFile->loca
f8ff0 6c 2e 62 52 65 73 65 72 76 65 64 20 3d 20 54 52  l.bReserved = TR
f9000 55 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72  UE;.      bRetur
f9010 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a  n = TRUE;.    }.
f9020 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65    }..  winceMute
f9030 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e  xRelease(pFile->
f9040 68 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  hMutex);.  retur
f9050 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a  n bReturn;.}../*
f9060 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74  .** An implement
f9070 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 55 6e 6c  ation of the Unl
f9080 6f 63 6b 46 69 6c 65 20 41 50 49 20 6f 66 20 77  ockFile API of w
f9090 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e 63 65  indows for wince
f90a0 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20  .*/.static BOOL 
f90b0 77 69 6e 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28  winceUnlockFile(
f90c0 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c  .  HANDLE *phFil
f90d0 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c  e,.  DWORD dwFil
f90e0 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57  eOffsetLow,.  DW
f90f0 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74  ORD dwFileOffset
f9100 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e  High,.  DWORD nN
f9110 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e  umberOfBytesToUn
f9120 6c 6f 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44  lockLow,.  DWORD
f9130 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
f9140 6f 55 6e 6c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20  oUnlockHigh.){. 
f9150 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
f9160 3d 20 48 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46  = HANDLE_TO_WINF
f9170 49 4c 45 28 70 68 46 69 6c 65 29 3b 0a 20 20 42  ILE(phFile);.  B
f9180 4f 4f 4c 20 62 52 65 74 75 72 6e 20 3d 20 46 41  OOL bReturn = FA
f9190 4c 53 45 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50  LSE;..  UNUSED_P
f91a0 41 52 41 4d 45 54 45 52 28 64 77 46 69 6c 65 4f  ARAMETER(dwFileO
f91b0 66 66 73 65 74 48 69 67 68 29 3b 0a 20 20 55 4e  ffsetHigh);.  UN
f91c0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e  USED_PARAMETER(n
f91d0 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55  NumberOfBytesToU
f91e0 6e 6c 6f 63 6b 48 69 67 68 29 3b 0a 0a 20 20 69  nlockHigh);..  i
f91f0 66 20 28 21 70 46 69 6c 65 2d 3e 68 4d 75 74 65  f (!pFile->hMute
f9200 78 29 20 72 65 74 75 72 6e 20 54 52 55 45 3b 0a  x) return TRUE;.
f9210 20 20 77 69 6e 63 65 4d 75 74 65 78 41 63 71 75    winceMutexAcqu
f9220 69 72 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65  ire(pFile->hMute
f9230 78 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c 65 61 73  x);..  /* Releas
f9240 69 6e 67 20 61 20 72 65 61 64 65 72 20 6c 6f 63  ing a reader loc
f9250 6b 20 6f 72 20 61 6e 20 65 78 63 6c 75 73 69 76  k or an exclusiv
f9260 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 66 20 28  e lock */.  if (
f9270 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20  dwFileOffsetLow 
f9280 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44  == (DWORD)SHARED
f9290 5f 46 49 52 53 54 29 7b 0a 20 20 20 20 2f 2a 20  _FIRST){.    /* 
f92a0 44 69 64 20 77 65 20 68 61 76 65 20 61 6e 20 65  Did we have an e
f92b0 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 3f 20 2a  xclusive lock? *
f92c0 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d  /.    if (pFile-
f92d0 3e 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76  >local.bExclusiv
f92e0 65 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  e){.      assert
f92f0 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54  (nNumberOfBytesT
f9300 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 28 44  oUnlockLow == (D
f9310 57 4f 52 44 29 53 48 41 52 45 44 5f 53 49 5a 45  WORD)SHARED_SIZE
f9320 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e  );.      pFile->
f9330 6c 6f 63 61 6c 2e 62 45 78 63 6c 75 73 69 76 65  local.bExclusive
f9340 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20   = FALSE;.      
f9350 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62  pFile->shared->b
f9360 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53  Exclusive = FALS
f9370 45 3b 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e  E;.      bReturn
f9380 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 0a   = TRUE;.    }..
f9390 20 20 20 20 2f 2a 20 44 69 64 20 77 65 20 6a 75      /* Did we ju
f93a0 73 74 20 68 61 76 65 20 61 20 72 65 61 64 65 72  st have a reader
f93b0 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 65 6c   lock? */.    el
f93c0 73 65 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f  se if (pFile->lo
f93d0 63 61 6c 2e 6e 52 65 61 64 65 72 73 29 7b 0a 20  cal.nReaders){. 
f93e0 20 20 20 20 20 61 73 73 65 72 74 28 6e 4e 75 6d       assert(nNum
f93f0 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f  berOfBytesToUnlo
f9400 63 6b 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44 29  ckLow == (DWORD)
f9410 53 48 41 52 45 44 5f 53 49 5a 45 20 7c 7c 20 6e  SHARED_SIZE || n
f9420 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55  NumberOfBytesToU
f9430 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 3b 0a  nlockLow == 1);.
f9440 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63        pFile->loc
f9450 61 6c 2e 6e 52 65 61 64 65 72 73 20 2d 2d 3b 0a  al.nReaders --;.
f9460 20 20 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d        if (pFile-
f9470 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20  >local.nReaders 
f9480 3d 3d 20 30 29 0a 20 20 20 20 20 20 7b 0a 20 20  == 0).      {.  
f9490 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61        pFile->sha
f94a0 72 65 64 2d 3e 6e 52 65 61 64 65 72 73 20 2d 2d  red->nReaders --
f94b0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
f94c0 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a  bReturn = TRUE;.
f94d0 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
f94e0 52 65 6c 65 61 73 69 6e 67 20 61 20 70 65 6e 64  Releasing a pend
f94f0 69 6e 67 20 6c 6f 63 6b 20 2a 2f 0a 20 20 65 6c  ing lock */.  el
f9500 73 65 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66  se if (dwFileOff
f9510 73 65 74 4c 6f 77 20 3d 3d 20 28 44 57 4f 52 44  setLow == (DWORD
f9520 29 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 26  )PENDING_BYTE &&
f9530 20 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54   nNumberOfBytesT
f9540 6f 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29  oUnlockLow == 1)
f9550 7b 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d  {.    if (pFile-
f9560 3e 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 29  >local.bPending)
f9570 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  {.      pFile->l
f9580 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20  ocal.bPending = 
f9590 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 69  FALSE;.      pFi
f95a0 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e  le->shared->bPen
f95b0 64 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20  ding = FALSE;.  
f95c0 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52      bReturn = TR
f95d0 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  UE;.    }.  }.  
f95e0 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 72  /* Releasing a r
f95f0 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 2a 2f 0a  eserved lock */.
f9600 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c    else if (dwFil
f9610 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 28 44  eOffsetLow == (D
f9620 57 4f 52 44 29 52 45 53 45 52 56 45 44 5f 42 59  WORD)RESERVED_BY
f9630 54 45 20 26 26 20 6e 4e 75 6d 62 65 72 4f 66 42  TE && nNumberOfB
f9640 79 74 65 73 54 6f 55 6e 6c 6f 63 6b 4c 6f 77 20  ytesToUnlockLow 
f9650 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70  == 1){.    if (p
f9660 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73  File->local.bRes
f9670 65 72 76 65 64 29 20 7b 0a 20 20 20 20 20 20 70  erved) {.      p
f9680 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 52 65 73  File->local.bRes
f9690 65 72 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20  erved = FALSE;. 
f96a0 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72       pFile->shar
f96b0 65 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20  ed->bReserved = 
f96c0 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 62 52 65  FALSE;.      bRe
f96d0 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a 20 20 20  turn = TRUE;.   
f96e0 20 7d 0a 20 20 7d 0a 0a 20 20 77 69 6e 63 65 4d   }.  }..  winceM
f96f0 75 74 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c  utexRelease(pFil
f9700 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20 72 65  e->hMutex);.  re
f9710 74 75 72 6e 20 62 52 65 74 75 72 6e 3b 0a 7d 0a  turn bReturn;.}.
f9720 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d  ./*.** An implem
f9730 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
f9740 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50 49  LockFileEx() API
f9750 20 6f 66 20 77 69 6e 64 6f 77 73 20 66 6f 72 20   of windows for 
f9760 77 69 6e 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20  wince.*/.static 
f9770 42 4f 4f 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69  BOOL winceLockFi
f9780 6c 65 45 78 28 0a 20 20 48 41 4e 44 4c 45 20 2a  leEx(.  HANDLE *
f9790 70 68 46 69 6c 65 2c 0a 20 20 44 57 4f 52 44 20  phFile,.  DWORD 
f97a0 64 77 46 6c 61 67 73 2c 0a 20 20 44 57 4f 52 44  dwFlags,.  DWORD
f97b0 20 64 77 52 65 73 65 72 76 65 64 2c 0a 20 20 44   dwReserved,.  D
f97c0 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42 79  WORD nNumberOfBy
f97d0 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 2c 0a 20 20  tesToLockLow,.  
f97e0 44 57 4f 52 44 20 6e 4e 75 6d 62 65 72 4f 66 42  DWORD nNumberOfB
f97f0 79 74 65 73 54 6f 4c 6f 63 6b 48 69 67 68 2c 0a  ytesToLockHigh,.
f9800 20 20 4c 50 4f 56 45 52 4c 41 50 50 45 44 20 6c    LPOVERLAPPED l
f9810 70 4f 76 65 72 6c 61 70 70 65 64 0a 29 7b 0a 20  pOverlapped.){. 
f9820 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
f9830 52 28 64 77 52 65 73 65 72 76 65 64 29 3b 0a 20  R(dwReserved);. 
f9840 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
f9850 52 28 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73  R(nNumberOfBytes
f9860 54 6f 4c 6f 63 6b 48 69 67 68 29 3b 0a 0a 20 20  ToLockHigh);..  
f9870 2f 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72  /* If the caller
f9880 20 77 61 6e 74 73 20 61 20 73 68 61 72 65 64 20   wants a shared 
f9890 72 65 61 64 20 6c 6f 63 6b 2c 20 66 6f 72 77 61  read lock, forwa
f98a0 72 64 20 74 68 69 73 20 63 61 6c 6c 0a 20 20 2a  rd this call.  *
f98b0 2a 20 74 6f 20 77 69 6e 63 65 4c 6f 63 6b 46 69  * to winceLockFi
f98c0 6c 65 20 2a 2f 0a 20 20 69 66 20 28 6c 70 4f 76  le */.  if (lpOv
f98d0 65 72 6c 61 70 70 65 64 2d 3e 4f 66 66 73 65 74  erlapped->Offset
f98e0 20 3d 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45   == (DWORD)SHARE
f98f0 44 5f 46 49 52 53 54 20 26 26 0a 20 20 20 20 20  D_FIRST &&.     
f9900 20 64 77 46 6c 61 67 73 20 3d 3d 20 31 20 26 26   dwFlags == 1 &&
f9910 0a 20 20 20 20 20 20 6e 4e 75 6d 62 65 72 4f 66  .      nNumberOf
f9920 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20 3d  BytesToLockLow =
f9930 3d 20 28 44 57 4f 52 44 29 53 48 41 52 45 44 5f  = (DWORD)SHARED_
f9940 53 49 5a 45 29 7b 0a 20 20 20 20 72 65 74 75 72  SIZE){.    retur
f9950 6e 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28  n winceLockFile(
f9960 70 68 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46  phFile, SHARED_F
f9970 49 52 53 54 2c 20 30 2c 20 31 2c 20 30 29 3b 0a  IRST, 0, 1, 0);.
f9980 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 46 41 4c    }.  return FAL
f9990 53 45 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 45 6e 64 20  SE;.}./*.** End 
f99a0 6f 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 63  of the special c
f99b0 6f 64 65 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2a  ode for wince.**
f99c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f99d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f99e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f99f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e  ***********/.#en
f9a10 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53  dif /* SQLITE_OS
f9a20 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _WINCE */../****
f9a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65  *********.** The
f9a80 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20 72   next group of r
f9a90 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e  outines implemen
f9aa0 74 20 74 68 65 20 49 2f 4f 20 6d 65 74 68 6f 64  t the I/O method
f9ab0 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62  s specified.** b
f9ac0 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f  y the sqlite3_io
f9ad0 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e  _methods object.
f9ae0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
f9af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
f9b30 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20  ../*.** Close a 
f9b40 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69  file..**.** It i
f9b50 73 20 72 65 70 6f 72 74 65 64 20 74 68 61 74 20  s reported that 
f9b60 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6c  an attempt to cl
f9b70 6f 73 65 20 61 20 68 61 6e 64 6c 65 20 6d 69 67  ose a handle mig
f9b80 68 74 20 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20  ht sometimes.** 
f9b90 66 61 69 6c 2e 20 20 54 68 69 73 20 69 73 20 61  fail.  This is a
f9ba0 20 76 65 72 79 20 75 6e 72 65 61 73 6f 6e 61 62   very unreasonab
f9bb0 6c 65 20 72 65 73 75 6c 74 2c 20 62 75 74 20 77  le result, but w
f9bc0 69 6e 64 6f 77 73 20 69 73 20 6e 6f 74 6f 72 69  indows is notori
f9bd0 6f 75 73 0a 2a 2a 20 66 6f 72 20 62 65 69 6e 67  ous.** for being
f9be0 20 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 73 6f   unreasonable so
f9bf0 20 49 20 64 6f 20 6e 6f 74 20 64 6f 75 62 74 20   I do not doubt 
f9c00 74 68 61 74 20 69 74 20 6d 69 67 68 74 20 68 61  that it might ha
f9c10 70 70 65 6e 2e 20 20 49 66 0a 2a 2a 20 74 68 65  ppen.  If.** the
f9c20 20 63 6c 6f 73 65 20 66 61 69 6c 73 2c 20 77 65   close fails, we
f9c30 20 70 61 75 73 65 20 66 6f 72 20 31 30 30 20 6d   pause for 100 m
f9c40 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20  illiseconds and 
f9c50 74 72 79 20 61 67 61 69 6e 2e 20 20 41 73 0a 2a  try again.  As.*
f9c60 2a 20 6d 61 6e 79 20 61 73 20 4d 58 5f 43 4c 4f  * many as MX_CLO
f9c70 53 45 5f 41 54 54 45 4d 50 54 20 61 74 74 65 6d  SE_ATTEMPT attem
f9c80 70 74 73 20 74 6f 20 63 6c 6f 73 65 20 74 68 65  pts to close the
f9c90 20 68 61 6e 64 6c 65 20 61 72 65 20 6d 61 64 65   handle are made
f9ca0 20 62 65 66 6f 72 65 0a 2a 2a 20 67 69 76 69 6e   before.** givin
f9cb0 67 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69  g up and returni
f9cc0 6e 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a  ng an error..*/.
f9cd0 23 64 65 66 69 6e 65 20 4d 58 5f 43 4c 4f 53 45  #define MX_CLOSE
f9ce0 5f 41 54 54 45 4d 50 54 20 33 0a 73 74 61 74 69  _ATTEMPT 3.stati
f9cf0 63 20 69 6e 74 20 77 69 6e 43 6c 6f 73 65 28 73  c int winClose(s
f9d00 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
f9d10 7b 0a 20 20 69 6e 74 20 72 63 2c 20 63 6e 74 20  {.  int rc, cnt 
f9d20 3d 20 30 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a  = 0;.  winFile *
f9d30 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65  pFile = (winFile
f9d40 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74 28  *)id;..  assert(
f9d50 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52   id!=0 );.  OSTR
f9d60 41 43 45 32 28 22 43 4c 4f 53 45 20 25 64 5c 6e  ACE2("CLOSE %d\n
f9d70 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20  ", pFile->h);.  
f9d80 64 6f 7b 0a 20 20 20 20 72 63 20 3d 20 43 6c 6f  do{.    rc = Clo
f9d90 73 65 48 61 6e 64 6c 65 28 70 46 69 6c 65 2d 3e  seHandle(pFile->
f9da0 68 29 3b 0a 20 20 7d 77 68 69 6c 65 28 20 72 63  h);.  }while( rc
f9db0 3d 3d 30 20 26 26 20 2b 2b 63 6e 74 20 3c 20 4d  ==0 && ++cnt < M
f9dc0 58 5f 43 4c 4f 53 45 5f 41 54 54 45 4d 50 54 20  X_CLOSE_ATTEMPT 
f9dd0 26 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c 20  && (Sleep(100), 
f9de0 31 29 20 29 3b 0a 23 69 66 20 53 51 4c 49 54 45  1) );.#if SQLITE
f9df0 5f 4f 53 5f 57 49 4e 43 45 0a 23 64 65 66 69 6e  _OS_WINCE.#defin
f9e00 65 20 57 49 4e 43 45 5f 44 45 4c 45 54 49 4f 4e  e WINCE_DELETION
f9e10 5f 41 54 54 45 4d 50 54 53 20 33 0a 20 20 77 69  _ATTEMPTS 3.  wi
f9e20 6e 63 65 44 65 73 74 72 6f 79 4c 6f 63 6b 28 70  nceDestroyLock(p
f9e30 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 70 46 69  File);.  if( pFi
f9e40 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f  le->zDeleteOnClo
f9e50 73 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e  se ){.    int cn
f9e60 74 20 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65  t = 0;.    while
f9e70 28 0a 20 20 20 20 20 20 20 20 20 20 20 44 65 6c  (.           Del
f9e80 65 74 65 46 69 6c 65 57 28 70 46 69 6c 65 2d 3e  eteFileW(pFile->
f9e90 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3d  zDeleteOnClose)=
f9ea0 3d 30 0a 20 20 20 20 20 20 20 20 26 26 20 47 65  =0.        && Ge
f9eb0 74 46 69 6c 65 41 74 74 72 69 62 75 74 65 73 57  tFileAttributesW
f9ec0 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f  (pFile->zDeleteO
f9ed0 6e 43 6c 6f 73 65 29 21 3d 30 78 66 66 66 66 66  nClose)!=0xfffff
f9ee0 66 66 66 20 0a 20 20 20 20 20 20 20 20 26 26 20  fff .        && 
f9ef0 63 6e 74 2b 2b 20 3c 20 57 49 4e 43 45 5f 44 45  cnt++ < WINCE_DE
f9f00 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53 0a  LETION_ATTEMPTS.
f9f10 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 53 6c      ){.       Sl
f9f20 65 65 70 28 31 30 30 29 3b 20 20 2f 2a 20 57 61  eep(100);  /* Wa
f9f30 69 74 20 61 20 6c 69 74 74 6c 65 20 62 65 66 6f  it a little befo
f9f40 72 65 20 74 72 79 69 6e 67 20 61 67 61 69 6e 20  re trying again 
f9f50 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 66 72 65  */.    }.    fre
f9f60 65 28 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65  e(pFile->zDelete
f9f70 4f 6e 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a 23 65  OnClose);.  }.#e
f9f80 6e 64 69 66 0a 20 20 4f 70 65 6e 43 6f 75 6e 74  ndif.  OpenCount
f9f90 65 72 28 2d 31 29 3b 0a 20 20 72 65 74 75 72 6e  er(-1);.  return
f9fa0 20 72 63 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20   rc ? SQLITE_OK 
f9fb0 3a 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a  : SQLITE_IOERR;.
f9fc0 7d 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 6d 69  }../*.** Some mi
f9fd0 63 72 6f 73 6f 66 74 20 63 6f 6d 70 69 6c 65 72  crosoft compiler
f9fe0 73 20 6c 61 63 6b 20 74 68 69 73 20 64 65 66 69  s lack this defi
f9ff0 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64  nition..*/.#ifnd
fa000 65 66 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46  ef INVALID_SET_F
fa010 49 4c 45 5f 50 4f 49 4e 54 45 52 0a 23 20 64 65  ILE_POINTER.# de
fa020 66 69 6e 65 20 49 4e 56 41 4c 49 44 5f 53 45 54  fine INVALID_SET
fa030 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 28 28  _FILE_POINTER ((
fa040 44 57 4f 52 44 29 2d 31 29 0a 23 65 6e 64 69 66  DWORD)-1).#endif
fa050 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74  ../*.** Read dat
fa060 61 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e  a from a file in
fa070 74 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 65  to a buffer.  Re
fa080 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69  turn SQLITE_OK i
fa090 66 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77  f all.** bytes w
fa0a0 65 72 65 20 72 65 61 64 20 73 75 63 63 65 73 73  ere read success
fa0b0 66 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45  fully and SQLITE
fa0c0 5f 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69  _IOERR if anythi
fa0d0 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67  ng goes.** wrong
fa0e0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
fa0f0 77 69 6e 52 65 61 64 28 0a 20 20 73 71 6c 69 74  winRead(.  sqlit
fa100 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20  e3_file *id,    
fa110 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f        /* File to
fa120 20 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20   read from */.  
fa130 76 6f 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20  void *pBuf,     
fa140 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72             /* Wr
fa150 69 74 65 20 63 6f 6e 74 65 6e 74 20 69 6e 74 6f  ite content into
fa160 20 74 68 69 73 20 62 75 66 66 65 72 20 2a 2f 0a   this buffer */.
fa170 20 20 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20    int amt,      
fa180 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
fa190 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
fa1a0 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c  to read */.  sql
fa1b0 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65  ite3_int64 offse
fa1c0 74 20 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e  t       /* Begin
fa1d0 20 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 73   reading at this
fa1e0 20 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20   offset */.){.  
fa1f0 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20 3d  LONG upperBits =
fa200 20 28 4c 4f 4e 47 29 28 28 6f 66 66 73 65 74 3e   (LONG)((offset>
fa210 3e 33 32 29 20 26 20 30 78 37 66 66 66 66 66 66  >32) & 0x7ffffff
fa220 66 29 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72  f);.  LONG lower
fa230 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28 6f 66  Bits = (LONG)(of
fa240 66 73 65 74 20 26 20 30 78 66 66 66 66 66 66 66  fset & 0xfffffff
fa250 66 29 3b 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a  f);.  DWORD rc;.
fa260 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65    winFile *pFile
fa270 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b   = (winFile*)id;
fa280 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 3b 0a  .  DWORD error;.
fa290 20 20 44 57 4f 52 44 20 67 6f 74 3b 0a 0a 20 20    DWORD got;..  
fa2a0 61 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b  assert( id!=0 );
fa2b0 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
fa2c0 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45  or(return SQLITE
fa2d0 5f 49 4f 45 52 52 5f 52 45 41 44 29 3b 0a 20 20  _IOERR_READ);.  
fa2e0 4f 53 54 52 41 43 45 33 28 22 52 45 41 44 20 25  OSTRACE3("READ %
fa2f0 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46  d lock=%d\n", pF
fa300 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c  ile->h, pFile->l
fa310 6f 63 6b 74 79 70 65 29 3b 0a 20 20 72 63 20 3d  ocktype);.  rc =
fa320 20 53 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 28   SetFilePointer(
fa330 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42  pFile->h, lowerB
fa340 69 74 73 2c 20 26 75 70 70 65 72 42 69 74 73 2c  its, &upperBits,
fa350 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20   FILE_BEGIN);.  
fa360 69 66 28 20 72 63 3d 3d 49 4e 56 41 4c 49 44 5f  if( rc==INVALID_
fa370 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52  SET_FILE_POINTER
fa380 20 26 26 20 28 65 72 72 6f 72 3d 47 65 74 4c 61   && (error=GetLa
fa390 73 74 45 72 72 6f 72 28 29 29 21 3d 4e 4f 5f 45  stError())!=NO_E
fa3a0 52 52 4f 52 20 29 7b 0a 20 20 20 20 70 46 69 6c  RROR ){.    pFil
fa3b0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65  e->lastErrno = e
fa3c0 72 72 6f 72 3b 0a 20 20 20 20 72 65 74 75 72 6e  rror;.    return
fa3d0 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20   SQLITE_FULL;.  
fa3e0 7d 0a 20 20 69 66 28 20 21 52 65 61 64 46 69 6c  }.  if( !ReadFil
fa3f0 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66  e(pFile->h, pBuf
fa400 2c 20 61 6d 74 2c 20 26 67 6f 74 2c 20 30 29 20  , amt, &got, 0) 
fa410 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  ){.    pFile->la
fa420 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73  stErrno = GetLas
fa430 74 45 72 72 6f 72 28 29 3b 0a 20 20 20 20 72 65  tError();.    re
fa440 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
fa450 52 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20 69 66  R_READ;.  }.  if
fa460 28 20 67 6f 74 3d 3d 28 44 57 4f 52 44 29 61 6d  ( got==(DWORD)am
fa470 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
fa480 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c  SQLITE_OK;.  }el
fa490 73 65 7b 0a 20 20 20 20 2f 2a 20 55 6e 72 65 61  se{.    /* Unrea
fa4a0 64 20 70 61 72 74 73 20 6f 66 20 74 68 65 20 62  d parts of the b
fa4b0 75 66 66 65 72 20 6d 75 73 74 20 62 65 20 7a 65  uffer must be ze
fa4c0 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f 0a 20 20 20  ro-filled */.   
fa4d0 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61 72 2a   memset(&((char*
fa4e0 29 70 42 75 66 29 5b 67 6f 74 5d 2c 20 30 2c 20  )pBuf)[got], 0, 
fa4f0 61 6d 74 2d 67 6f 74 29 3b 0a 20 20 20 20 72 65  amt-got);.    re
fa500 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
fa510 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b 0a 20 20  R_SHORT_READ;.  
fa520 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  }.}../*.** Write
fa530 20 64 61 74 61 20 66 72 6f 6d 20 61 20 62 75 66   data from a buf
fa540 66 65 72 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e  fer into a file.
fa550 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    Return SQLITE_
fa560 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a 2a  OK on success.**
fa570 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65   or some other e
fa580 72 72 6f 72 20 63 6f 64 65 20 6f 6e 20 66 61 69  rror code on fai
fa590 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lure..*/.static 
fa5a0 69 6e 74 20 77 69 6e 57 72 69 74 65 28 0a 20 20  int winWrite(.  
fa5b0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
fa5c0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c  ,         /* Fil
fa5d0 65 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20  e to write into 
fa5e0 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
fa5f0 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20 2f  *pBuf,         /
fa600 2a 20 54 68 65 20 62 79 74 65 73 20 74 6f 20 62  * The bytes to b
fa610 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20 69  e written */.  i
fa620 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20  nt amt,         
fa630 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
fa640 65 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 77  er of bytes to w
fa650 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65  rite */.  sqlite
fa660 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 20 20  3_int64 offset  
fa670 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
fa680 74 6f 20 74 68 65 20 66 69 6c 65 20 74 6f 20 62  to the file to b
fa690 65 67 69 6e 20 77 72 69 74 69 6e 67 20 61 74 20  egin writing at 
fa6a0 2a 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70  */.){.  LONG upp
fa6b0 65 72 42 69 74 73 20 3d 20 28 4c 4f 4e 47 29 28  erBits = (LONG)(
fa6c0 28 6f 66 66 73 65 74 3e 3e 33 32 29 20 26 20 30  (offset>>32) & 0
fa6d0 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20 4c 4f  x7fffffff);.  LO
fa6e0 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 28  NG lowerBits = (
fa6f0 4c 4f 4e 47 29 28 6f 66 66 73 65 74 20 26 20 30  LONG)(offset & 0
fa700 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20 44 57  xffffffff);.  DW
fa710 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c  ORD rc;.  winFil
fa720 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46  e *pFile = (winF
fa730 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f 52 44  ile*)id;.  DWORD
fa740 20 65 72 72 6f 72 3b 0a 20 20 44 57 4f 52 44 20   error;.  DWORD 
fa750 77 72 6f 74 65 20 3d 20 30 3b 0a 0a 20 20 61 73  wrote = 0;..  as
fa760 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20  sert( id!=0 );. 
fa770 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
fa780 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49  (return SQLITE_I
fa790 4f 45 52 52 5f 57 52 49 54 45 29 3b 0a 20 20 53  OERR_WRITE);.  S
fa7a0 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c 6c 45  imulateDiskfullE
fa7b0 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49  rror(return SQLI
fa7c0 54 45 5f 46 55 4c 4c 29 3b 0a 20 20 4f 53 54 52  TE_FULL);.  OSTR
fa7d0 41 43 45 33 28 22 57 52 49 54 45 20 25 64 20 6c  ACE3("WRITE %d l
fa7e0 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65  ock=%d\n", pFile
fa7f0 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  ->h, pFile->lock
fa800 74 79 70 65 29 3b 0a 20 20 72 63 20 3d 20 53 65  type);.  rc = Se
fa810 74 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46 69  tFilePointer(pFi
fa820 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74 73  le->h, lowerBits
fa830 2c 20 26 75 70 70 65 72 42 69 74 73 2c 20 46 49  , &upperBits, FI
fa840 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66 28  LE_BEGIN);.  if(
fa850 20 72 63 3d 3d 49 4e 56 41 4c 49 44 5f 53 45 54   rc==INVALID_SET
fa860 5f 46 49 4c 45 5f 50 4f 49 4e 54 45 52 20 26 26  _FILE_POINTER &&
fa870 20 28 65 72 72 6f 72 3d 47 65 74 4c 61 73 74 45   (error=GetLastE
fa880 72 72 6f 72 28 29 29 21 3d 4e 4f 5f 45 52 52 4f  rror())!=NO_ERRO
fa890 52 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e  R ){.    pFile->
fa8a0 6c 61 73 74 45 72 72 6e 6f 20 3d 20 65 72 72 6f  lastErrno = erro
fa8b0 72 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  r;.    return SQ
fa8c0 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a 20  LITE_FULL;.  }. 
fa8d0 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29   assert( amt>0 )
fa8e0 3b 0a 20 20 77 68 69 6c 65 28 0a 20 20 20 20 20  ;.  while(.     
fa8f0 61 6d 74 3e 30 0a 20 20 20 20 20 26 26 20 28 72  amt>0.     && (r
fa900 63 20 3d 20 57 72 69 74 65 46 69 6c 65 28 70 46  c = WriteFile(pF
fa910 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d  ile->h, pBuf, am
fa920 74 2c 20 26 77 72 6f 74 65 2c 20 30 29 29 21 3d  t, &wrote, 0))!=
fa930 30 0a 20 20 20 20 20 26 26 20 77 72 6f 74 65 3e  0.     && wrote>
fa940 30 0a 20 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d  0.  ){.    amt -
fa950 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42 75  = wrote;.    pBu
fa960 66 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42 75  f = &((char*)pBu
fa970 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 20  f)[wrote];.  }. 
fa980 20 69 66 28 20 21 72 63 20 7c 7c 20 61 6d 74 3e   if( !rc || amt>
fa990 28 69 6e 74 29 77 72 6f 74 65 20 29 7b 0a 20 20  (int)wrote ){.  
fa9a0 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
fa9b0 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f  no = GetLastErro
fa9c0 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  r();.    return 
fa9d0 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d  SQLITE_FULL;.  }
fa9e0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
fa9f0 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72  _OK;.}../*.** Tr
faa00 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20 66  uncate an open f
faa10 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66 69  ile to a specifi
faa20 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74 69  ed size.*/.stati
faa30 63 20 69 6e 74 20 77 69 6e 54 72 75 6e 63 61 74  c int winTruncat
faa40 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  e(sqlite3_file *
faa50 69 64 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  id, sqlite3_int6
faa60 34 20 6e 42 79 74 65 29 7b 0a 20 20 4c 4f 4e 47  4 nByte){.  LONG
faa70 20 75 70 70 65 72 42 69 74 73 20 3d 20 28 4c 4f   upperBits = (LO
faa80 4e 47 29 28 28 6e 42 79 74 65 3e 3e 33 32 29 20  NG)((nByte>>32) 
faa90 26 20 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20  & 0x7fffffff);. 
faaa0 20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 20   LONG lowerBits 
faab0 3d 20 28 4c 4f 4e 47 29 28 6e 42 79 74 65 20 26  = (LONG)(nByte &
faac0 20 30 78 66 66 66 66 66 66 66 66 29 3b 0a 20 20   0xffffffff);.  
faad0 44 57 4f 52 44 20 72 63 3b 0a 20 20 77 69 6e 46  DWORD rc;.  winF
faae0 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69  ile *pFile = (wi
faaf0 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f  nFile*)id;.  DWO
fab00 52 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73 73  RD error;..  ass
fab10 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20  ert( id!=0 );.  
fab20 4f 53 54 52 41 43 45 33 28 22 54 52 55 4e 43 41  OSTRACE3("TRUNCA
fab30 54 45 20 25 64 20 25 6c 6c 64 5c 6e 22 2c 20 70  TE %d %lld\n", p
fab40 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74 65 29 3b  File->h, nByte);
fab50 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
fab60 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45  or(return SQLITE
fab70 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 29  _IOERR_TRUNCATE)
fab80 3b 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c 65  ;.  rc = SetFile
fab90 50 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68  Pointer(pFile->h
faba0 2c 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70  , lowerBits, &up
fabb0 70 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45  perBits, FILE_BE
fabc0 47 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  GIN);.  if( rc==
fabd0 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45  INVALID_SET_FILE
fabe0 5f 50 4f 49 4e 54 45 52 20 26 26 20 28 65 72 72  _POINTER && (err
fabf0 6f 72 3d 47 65 74 4c 61 73 74 45 72 72 6f 72 28  or=GetLastError(
fac00 29 29 21 3d 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a  ))!=NO_ERROR ){.
fac10 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
fac20 72 72 6e 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20  rrno = error;.  
fac30 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
fac40 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a  IOERR_TRUNCATE;.
fac50 20 20 7d 0a 20 20 2f 2a 20 53 65 74 45 6e 64 4f    }.  /* SetEndO
fac60 66 46 69 6c 65 20 77 69 6c 6c 20 66 61 69 6c 20  fFile will fail 
fac70 69 66 20 6e 42 79 74 65 20 69 73 20 6e 65 67 61  if nByte is nega
fac80 74 69 76 65 20 2a 2f 0a 20 20 69 66 28 20 21 53  tive */.  if( !S
fac90 65 74 45 6e 64 4f 66 46 69 6c 65 28 70 46 69 6c  etEndOfFile(pFil
faca0 65 2d 3e 68 29 20 29 7b 0a 20 20 20 20 70 46 69  e->h) ){.    pFi
facb0 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20  le->lastErrno = 
facc0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a  GetLastError();.
facd0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
face0 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45  E_IOERR_TRUNCATE
facf0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
fad00 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66  QLITE_OK;.}..#if
fad10 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
fad20 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20 74 68 65 20  /*.** Count the 
fad30 6e 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 73 79  number of fullsy
fad40 6e 63 73 20 61 6e 64 20 6e 6f 72 6d 61 6c 20 73  ncs and normal s
fad50 79 6e 63 73 2e 20 20 54 68 69 73 20 69 73 20 75  yncs.  This is u
fad60 73 65 64 20 74 6f 20 74 65 73 74 0a 2a 2a 20 74  sed to test.** t
fad70 68 61 74 20 73 79 6e 63 73 20 61 6e 64 20 66 75  hat syncs and fu
fad80 6c 6c 73 79 6e 63 73 20 61 72 65 20 6f 63 63 75  llsyncs are occu
fad90 72 69 6e 67 20 61 74 20 74 68 65 20 72 69 67 68  ring at the righ
fada0 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a 53 51 4c 49  t times..*/.SQLI
fadb0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
fadc0 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20  e3_sync_count = 
fadd0 30 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  0;.SQLITE_API in
fade0 74 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79  t sqlite3_fullsy
fadf0 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65  nc_count = 0;.#e
fae00 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65  ndif../*.** Make
fae10 20 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73   sure all writes
fae20 20 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72   to a particular
fae30 20 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74   file are commit
fae40 74 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2f 0a  ted to disk..*/.
fae50 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 79  static int winSy
fae60 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  nc(sqlite3_file 
fae70 2a 69 64 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b  *id, int flags){
fae80 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
fae90 4e 4f 5f 53 59 4e 43 0a 20 20 77 69 6e 46 69 6c  NO_SYNC.  winFil
faea0 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46  e *pFile = (winF
faeb0 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65  ile*)id;..  asse
faec0 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 4f  rt( id!=0 );.  O
faed0 53 54 52 41 43 45 33 28 22 53 59 4e 43 20 25 64  STRACE3("SYNC %d
faee0 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69   lock=%d\n", pFi
faef0 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f  le->h, pFile->lo
faf00 63 6b 74 79 70 65 29 3b 0a 23 65 6c 73 65 0a 20  cktype);.#else. 
faf10 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
faf20 52 28 69 64 29 3b 0a 23 65 6e 64 69 66 0a 23 69  R(id);.#endif.#i
faf30 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  fndef SQLITE_TES
faf40 54 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  T.  UNUSED_PARAM
faf50 45 54 45 52 28 66 6c 61 67 73 29 3b 0a 23 65 6c  ETER(flags);.#el
faf60 73 65 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26  se.  if( flags &
faf70 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c   SQLITE_SYNC_FUL
faf80 4c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  L ){.    sqlite3
faf90 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 2b  _fullsync_count+
fafa0 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  +;.  }.  sqlite3
fafb0 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23  _sync_count++;.#
fafc0 65 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20 77 65  endif.  /* If we
fafd0 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74   compiled with t
fafe0 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e  he SQLITE_NO_SYN
faff0 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e  C flag, then syn
fb000 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e  cing is a.  ** n
fb010 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66  o-op.  */.#ifdef
fb020 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a   SQLITE_NO_SYNC.
fb030 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
fb040 45 5f 4f 4b 3b 0a 23 65 6c 73 65 0a 20 20 69 66  E_OK;.#else.  if
fb050 28 20 46 6c 75 73 68 46 69 6c 65 42 75 66 66 65  ( FlushFileBuffe
fb060 72 73 28 70 46 69 6c 65 2d 3e 68 29 20 29 7b 0a  rs(pFile->h) ){.
fb070 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
fb080 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
fb090 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72     pFile->lastEr
fb0a0 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45 72 72  rno = GetLastErr
fb0b0 6f 72 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  or();.    return
fb0c0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20   SQLITE_IOERR;. 
fb0d0 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a   }.#endif.}../*.
fb0e0 2a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65  ** Determine the
fb0f0 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66   current size of
fb100 20 61 20 66 69 6c 65 20 69 6e 20 62 79 74 65 73   a file in bytes
fb110 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
fb120 69 6e 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74  inFileSize(sqlit
fb130 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c  e3_file *id, sql
fb140 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a  ite3_int64 *pSiz
fb150 65 29 7b 0a 20 20 44 57 4f 52 44 20 75 70 70 65  e){.  DWORD uppe
fb160 72 42 69 74 73 3b 0a 20 20 44 57 4f 52 44 20 6c  rBits;.  DWORD l
fb170 6f 77 65 72 42 69 74 73 3b 0a 20 20 77 69 6e 46  owerBits;.  winF
fb180 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69  ile *pFile = (wi
fb190 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 44 57 4f  nFile*)id;.  DWO
fb1a0 52 44 20 65 72 72 6f 72 3b 0a 0a 20 20 61 73 73  RD error;..  ass
fb1b0 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20  ert( id!=0 );.  
fb1c0 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
fb1d0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
fb1e0 45 52 52 5f 46 53 54 41 54 29 3b 0a 20 20 6c 6f  ERR_FSTAT);.  lo
fb1f0 77 65 72 42 69 74 73 20 3d 20 47 65 74 46 69 6c  werBits = GetFil
fb200 65 53 69 7a 65 28 70 46 69 6c 65 2d 3e 68 2c 20  eSize(pFile->h, 
fb210 26 75 70 70 65 72 42 69 74 73 29 3b 0a 20 20 69  &upperBits);.  i
fb220 66 28 20 20 20 28 6c 6f 77 65 72 42 69 74 73 20  f(   (lowerBits 
fb230 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f  == INVALID_FILE_
fb240 53 49 5a 45 29 0a 20 20 20 20 20 26 26 20 28 28  SIZE).     && ((
fb250 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73 74 45  error = GetLastE
fb260 72 72 6f 72 28 29 29 20 21 3d 20 4e 4f 5f 45 52  rror()) != NO_ER
fb270 52 4f 52 29 20 29 0a 20 20 7b 0a 20 20 20 20 70  ROR) ).  {.    p
fb280 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
fb290 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72 65 74  = error;.    ret
fb2a0 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
fb2b0 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 20 20 2a 70  _FSTAT;.  }.  *p
fb2c0 53 69 7a 65 20 3d 20 28 28 28 73 71 6c 69 74 65  Size = (((sqlite
fb2d0 33 5f 69 6e 74 36 34 29 75 70 70 65 72 42 69 74  3_int64)upperBit
fb2e0 73 29 3c 3c 33 32 29 20 2b 20 6c 6f 77 65 72 42  s)<<32) + lowerB
fb2f0 69 74 73 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  its;.  return SQ
fb300 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
fb310 2a 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f  * LOCKFILE_FAIL_
fb320 49 4d 4d 45 44 49 41 54 45 4c 59 20 69 73 20 75  IMMEDIATELY is u
fb330 6e 64 65 66 69 6e 65 64 20 6f 6e 20 73 6f 6d 65  ndefined on some
fb340 20 57 69 6e 64 6f 77 73 20 73 79 73 74 65 6d 73   Windows systems
fb350 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4c 4f 43  ..*/.#ifndef LOC
fb360 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45 44  KFILE_FAIL_IMMED
fb370 49 41 54 45 4c 59 0a 23 20 64 65 66 69 6e 65 20  IATELY.# define 
fb380 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d  LOCKFILE_FAIL_IM
fb390 4d 45 44 49 41 54 45 4c 59 20 31 0a 23 65 6e 64  MEDIATELY 1.#end
fb3a0 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72  if../*.** Acquir
fb3b0 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b 2e  e a reader lock.
fb3c0 0a 2a 2a 20 44 69 66 66 65 72 65 6e 74 20 41 50  .** Different AP
fb3d0 49 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63  I routines are c
fb3e0 61 6c 6c 65 64 20 64 65 70 65 6e 64 69 6e 67 20  alled depending 
fb3f0 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  on whether or no
fb400 74 20 74 68 69 73 0a 2a 2a 20 69 73 20 57 69 6e  t this.** is Win
fb410 39 35 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a 2f 0a  95 or WinNT..*/.
fb420 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 52 65  static int getRe
fb430 61 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a  adLock(winFile *
fb440 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65  pFile){.  int re
fb450 73 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20  s;.  if( isNT() 
fb460 29 7b 0a 20 20 20 20 4f 56 45 52 4c 41 50 50 45  ){.    OVERLAPPE
fb470 44 20 6f 76 6c 70 3b 0a 20 20 20 20 6f 76 6c 70  D ovlp;.    ovlp
fb480 2e 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45 44  .Offset = SHARED
fb490 5f 46 49 52 53 54 3b 0a 20 20 20 20 6f 76 6c 70  _FIRST;.    ovlp
fb4a0 2e 4f 66 66 73 65 74 48 69 67 68 20 3d 20 30 3b  .OffsetHigh = 0;
fb4b0 0a 20 20 20 20 6f 76 6c 70 2e 68 45 76 65 6e 74  .    ovlp.hEvent
fb4c0 20 3d 20 30 3b 0a 20 20 20 20 72 65 73 20 3d 20   = 0;.    res = 
fb4d0 4c 6f 63 6b 46 69 6c 65 45 78 28 70 46 69 6c 65  LockFileEx(pFile
fb4e0 2d 3e 68 2c 20 4c 4f 43 4b 46 49 4c 45 5f 46 41  ->h, LOCKFILE_FA
fb4f0 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 2c 0a  IL_IMMEDIATELY,.
fb500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fb510 20 20 20 20 20 30 2c 20 53 48 41 52 45 44 5f 53       0, SHARED_S
fb520 49 5a 45 2c 20 30 2c 20 26 6f 76 6c 70 29 3b 0a  IZE, 0, &ovlp);.
fb530 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69  /* isNT() is 1 i
fb540 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
fb550 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c  E==1, so this el
fb560 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63  se is never exec
fb570 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 51  uted. .*/.#if SQ
fb580 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30  LITE_OS_WINCE==0
fb590 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
fb5a0 74 20 6c 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65  t lk;.    sqlite
fb5b0 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69 7a  3_randomness(siz
fb5c0 65 6f 66 28 6c 6b 29 2c 20 26 6c 6b 29 3b 0a 20  eof(lk), &lk);. 
fb5d0 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64     pFile->shared
fb5e0 4c 6f 63 6b 42 79 74 65 20 3d 20 28 73 68 6f 72  LockByte = (shor
fb5f0 74 29 28 28 6c 6b 20 26 20 30 78 37 66 66 66 66  t)((lk & 0x7ffff
fb600 66 66 66 29 25 28 53 48 41 52 45 44 5f 53 49 5a  fff)%(SHARED_SIZ
fb610 45 20 2d 20 31 29 29 3b 0a 20 20 20 20 72 65 73  E - 1));.    res
fb620 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c   = LockFile(pFil
fb630 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52  e->h, SHARED_FIR
fb640 53 54 2b 70 46 69 6c 65 2d 3e 73 68 61 72 65 64  ST+pFile->shared
fb650 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31 2c 20  LockByte, 0, 1, 
fb660 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20  0);.#endif.  }. 
fb670 20 69 66 28 20 72 65 73 20 3d 3d 20 30 20 29 7b   if( res == 0 ){
fb680 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74  .    pFile->last
fb690 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73 74 45  Errno = GetLastE
fb6a0 72 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 20 72 65  rror();.  }.  re
fb6b0 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a  turn res;.}../*.
fb6c0 2a 2a 20 55 6e 64 6f 20 61 20 72 65 61 64 6c 6f  ** Undo a readlo
fb6d0 63 6b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  ck.*/.static int
fb6e0 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28   unlockReadLock(
fb6f0 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 29 7b  winFile *pFile){
fb700 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 66  .  int res;.  if
fb710 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20  ( isNT() ){.    
fb720 72 65 73 20 3d 20 55 6e 6c 6f 63 6b 46 69 6c 65  res = UnlockFile
fb730 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52 45  (pFile->h, SHARE
fb740 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41 52  D_FIRST, 0, SHAR
fb750 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 2f 2a 20  ED_SIZE, 0);./* 
fb760 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53  isNT() is 1 if S
fb770 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d  QLITE_OS_WINCE==
fb780 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20  1, so this else 
fb790 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65  is never execute
fb7a0 64 2e 20 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  d. .*/.#if SQLIT
fb7b0 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20  E_OS_WINCE==0.  
fb7c0 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 73 20 3d  }else{.    res =
fb7d0 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c   UnlockFile(pFil
fb7e0 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52  e->h, SHARED_FIR
fb7f0 53 54 20 2b 20 70 46 69 6c 65 2d 3e 73 68 61 72  ST + pFile->shar
fb800 65 64 4c 6f 63 6b 42 79 74 65 2c 20 30 2c 20 31  edLockByte, 0, 1
fb810 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  , 0);.#endif.  }
fb820 0a 20 20 69 66 28 20 72 65 73 20 3d 3d 20 30 20  .  if( res == 0 
fb830 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61  ){.    pFile->la
fb840 73 74 45 72 72 6e 6f 20 3d 20 47 65 74 4c 61 73  stErrno = GetLas
fb850 74 45 72 72 6f 72 28 29 3b 0a 20 20 7d 0a 20 20  tError();.  }.  
fb860 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f  return res;.}../
fb870 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69  *.** Lock the fi
fb880 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b  le with the lock
fb890 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 61   specified by pa
fb8a0 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65  rameter locktype
fb8b0 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65   - one.** of the
fb8c0 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a   following:.**.*
fb8d0 2a 20 20 20 20 20 28 31 29 20 53 48 41 52 45 44  *     (1) SHARED
fb8e0 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29  _LOCK.**     (2)
fb8f0 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a   RESERVED_LOCK.*
fb900 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e  *     (3) PENDIN
fb910 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34  G_LOCK.**     (4
fb920 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  ) EXCLUSIVE_LOCK
fb930 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73  .**.** Sometimes
fb940 20 77 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67   when requesting
fb950 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c   one lock state,
fb960 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b   additional lock
fb970 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69   states.** are i
fb980 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74 77 65  nserted in betwe
fb990 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67  en.  The locking
fb9a0 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f   might fail on o
fb9b0 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a  ne of the later.
fb9c0 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c  ** transitions l
fb9d0 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20  eaving the lock 
fb9e0 73 74 61 74 65 20 64 69 66 66 65 72 65 6e 74 20  state different 
fb9f0 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61  from what it sta
fba00 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c  rted but.** stil
fba10 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67  l short of its g
fba20 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  oal.  The follow
fba30 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77 73 20  ing chart shows 
fba40 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74  the allowed.** t
fba50 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74  ransitions and t
fba60 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65  he inserted inte
fba70 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 73 3a  rmediate states:
fba80 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b  .**.**    UNLOCK
fba90 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20  ED -> SHARED.** 
fbaa0 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53     SHARED -> RES
fbab0 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52  ERVED.**    SHAR
fbac0 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20  ED -> (PENDING) 
fbad0 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20  -> EXCLUSIVE.** 
fbae0 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28     RESERVED -> (
fbaf0 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c  PENDING) -> EXCL
fbb00 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44  USIVE.**    PEND
fbb10 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  ING -> EXCLUSIVE
fbb20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
fbb30 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e  ine will only in
fbb40 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20  crease a lock.  
fbb50 54 68 65 20 77 69 6e 55 6e 6c 6f 63 6b 28 29 20  The winUnlock() 
fbb60 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 72 61 73 65  routine.** erase
fbb70 73 20 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20 6f  s all locks at o
fbb80 6e 63 65 20 61 6e 64 20 72 65 74 75 72 6e 73 20  nce and returns 
fbb90 75 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  us immediately t
fbba0 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  o locking level 
fbbb0 30 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20  0..** It is not 
fbbc0 70 6f 73 73 69 62 6c 65 20 74 6f 20 6c 6f 77 65  possible to lowe
fbbd0 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65  r the locking le
fbbe0 76 65 6c 20 6f 6e 65 20 73 74 65 70 20 61 74 20  vel one step at 
fbbf0 61 20 74 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20  a time.  You.** 
fbc00 6d 75 73 74 20 67 6f 20 73 74 72 61 69 67 68 74  must go straight
fbc10 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65   to locking leve
fbc20 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  l 0..*/.static i
fbc30 6e 74 20 77 69 6e 4c 6f 63 6b 28 73 71 6c 69 74  nt winLock(sqlit
fbc40 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
fbc50 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e   locktype){.  in
fbc60 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
fbc70 3b 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63  ;    /* Return c
fbc80 6f 64 65 20 66 72 6f 6d 20 73 75 62 72 6f 75 74  ode from subrout
fbc90 69 6e 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65  ines */.  int re
fbca0 73 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20  s = 1;          
fbcb0 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 20   /* Result of a 
fbcc0 77 69 6e 64 6f 77 73 20 6c 6f 63 6b 20 63 61 6c  windows lock cal
fbcd0 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f  l */.  int newLo
fbce0 63 6b 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a  cktype;       /*
fbcf0 20 53 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   Set pFile->lock
fbd00 74 79 70 65 20 74 6f 20 74 68 69 73 20 76 61 6c  type to this val
fbd10 75 65 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e  ue before exitin
fbd20 67 20 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65  g */.  int gotPe
fbd30 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a  ndingLock = 0;/*
fbd40 20 54 72 75 65 20 69 66 20 77 65 20 61 63 71 75   True if we acqu
fbd50 69 72 65 64 20 61 20 50 45 4e 44 49 4e 47 20 6c  ired a PENDING l
fbd60 6f 63 6b 20 74 68 69 73 20 74 69 6d 65 20 2a 2f  ock this time */
fbd70 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c  .  winFile *pFil
fbd80 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64  e = (winFile*)id
fbd90 3b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 20  ;.  DWORD error 
fbda0 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 0a 20 20 61  = NO_ERROR;..  a
fbdb0 73 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a  ssert( id!=0 );.
fbdc0 20 20 4f 53 54 52 41 43 45 35 28 22 4c 4f 43 4b    OSTRACE5("LOCK
fbdd0 20 25 64 20 25 64 20 77 61 73 20 25 64 28 25 64   %d %d was %d(%d
fbde0 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  )\n",.          
fbdf0 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79  pFile->h, lockty
fbe00 70 65 2c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  pe, pFile->lockt
fbe10 79 70 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61 72  ype, pFile->shar
fbe20 65 64 4c 6f 63 6b 42 79 74 65 29 3b 0a 0a 20 20  edLockByte);..  
fbe30 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  /* If there is a
fbe40 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66  lready a lock of
fbe50 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f   this type or mo
fbe60 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20 6f  re restrictive o
fbe70 6e 20 74 68 65 0a 20 20 2a 2a 20 4f 73 46 69 6c  n the.  ** OsFil
fbe80 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 20 44  e, do nothing. D
fbe90 6f 6e 27 74 20 75 73 65 20 74 68 65 20 65 6e 64  on't use the end
fbea0 5f 6c 6f 63 6b 3a 20 65 78 69 74 20 70 61 74 68  _lock: exit path
fbeb0 2c 20 61 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65  , as.  ** sqlite
fbec0 33 4f 73 45 6e 74 65 72 4d 75 74 65 78 28 29 20  3OsEnterMutex() 
fbed0 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c  hasn't been call
fbee0 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69  ed yet..  */.  i
fbef0 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
fbf00 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a  pe>=locktype ){.
fbf10 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
fbf20 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  E_OK;.  }..  /* 
fbf30 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c 6f  Make sure the lo
fbf40 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69  cking sequence i
fbf50 73 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20  s correct.  */. 
fbf60 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e   assert( pFile->
fbf70 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43  locktype!=NO_LOC
fbf80 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53  K || locktype==S
fbf90 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
fbfa0 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65  assert( locktype
fbfb0 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29  !=PENDING_LOCK )
fbfc0 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b  ;.  assert( lock
fbfd0 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f 4c  type!=RESERVED_L
fbfe0 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f  OCK || pFile->lo
fbff0 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
fc000 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63  OCK );..  /* Loc
fc010 6b 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f  k the PENDING_LO
fc020 43 4b 20 62 79 74 65 20 69 66 20 77 65 20 6e 65  CK byte if we ne
fc030 65 64 20 74 6f 20 61 63 71 75 69 72 65 20 61 20  ed to acquire a 
fc040 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 6f 72 0a  PENDING lock or.
fc050 20 20 2a 2a 20 61 20 53 48 41 52 45 44 20 6c 6f    ** a SHARED lo
fc060 63 6b 2e 20 20 49 66 20 77 65 20 61 72 65 20 61  ck.  If we are a
fc070 63 71 75 69 72 69 6e 67 20 61 20 53 48 41 52 45  cquiring a SHARE
fc080 44 20 6c 6f 63 6b 2c 20 74 68 65 20 61 63 71 75  D lock, the acqu
fc090 69 73 69 74 69 6f 6e 20 6f 66 0a 20 20 2a 2a 20  isition of.  ** 
fc0a0 74 68 65 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b  the PENDING_LOCK
fc0b0 20 62 79 74 65 20 69 73 20 74 65 6d 70 6f 72 61   byte is tempora
fc0c0 72 79 2e 0a 20 20 2a 2f 0a 20 20 6e 65 77 4c 6f  ry..  */.  newLo
fc0d0 63 6b 74 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e  cktype = pFile->
fc0e0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 69 66 28 20  locktype;.  if( 
fc0f0 20 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79    (pFile->lockty
fc100 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 29 0a 20 20 20  pe==NO_LOCK).   
fc110 20 20 7c 7c 20 28 20 20 20 28 6c 6f 63 6b 74 79    || (   (lockty
fc120 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f  pe==EXCLUSIVE_LO
fc130 43 4b 29 0a 20 20 20 20 20 20 20 20 20 26 26 20  CK).         && 
fc140 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65  (pFile->locktype
fc150 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 29  ==RESERVED_LOCK)
fc160 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 63  ).  ){.    int c
fc170 6e 74 20 3d 20 33 3b 0a 20 20 20 20 77 68 69 6c  nt = 3;.    whil
fc180 65 28 20 63 6e 74 2d 2d 3e 30 20 26 26 20 28 72  e( cnt-->0 && (r
fc190 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46  es = LockFile(pF
fc1a0 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f  ile->h, PENDING_
fc1b0 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 29 3d  BYTE, 0, 1, 0))=
fc1c0 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  =0 ){.      /* T
fc1d0 72 79 20 33 20 74 69 6d 65 73 20 74 6f 20 67 65  ry 3 times to ge
fc1e0 74 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c 6f  t the pending lo
fc1f0 63 6b 2e 20 20 54 68 65 20 70 65 6e 64 69 6e 67  ck.  The pending
fc200 20 6c 6f 63 6b 20 6d 69 67 68 74 20 62 65 0a 20   lock might be. 
fc210 20 20 20 20 20 2a 2a 20 68 65 6c 64 20 62 79 20       ** held by 
fc220 61 6e 6f 74 68 65 72 20 72 65 61 64 65 72 20 70  another reader p
fc230 72 6f 63 65 73 73 20 77 68 6f 20 77 69 6c 6c 20  rocess who will 
fc240 72 65 6c 65 61 73 65 20 69 74 20 6d 6f 6d 65 6e  release it momen
fc250 74 61 72 69 6c 79 2e 0a 20 20 20 20 20 20 2a 2f  tarily..      */
fc260 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32 28  .      OSTRACE2(
fc270 22 63 6f 75 6c 64 20 6e 6f 74 20 67 65 74 20 61  "could not get a
fc280 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20 63   PENDING lock. c
fc290 6e 74 3d 25 64 5c 6e 22 2c 20 63 6e 74 29 3b 0a  nt=%d\n", cnt);.
fc2a0 20 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b 0a        Sleep(1);.
fc2b0 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 50 65 6e      }.    gotPen
fc2c0 64 69 6e 67 4c 6f 63 6b 20 3d 20 72 65 73 3b 0a  dingLock = res;.
fc2d0 20 20 20 20 69 66 28 20 21 72 65 73 20 29 7b 0a      if( !res ){.
fc2e0 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65        error = Ge
fc2f0 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20  tLastError();.  
fc300 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63    }.  }..  /* Ac
fc310 71 75 69 72 65 20 61 20 73 68 61 72 65 64 20 6c  quire a shared l
fc320 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c  ock.  */.  if( l
fc330 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
fc340 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20  LOCK && res ){. 
fc350 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65     assert( pFile
fc360 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c  ->locktype==NO_L
fc370 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d  OCK );.    res =
fc380 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69   getReadLock(pFi
fc390 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73  le);.    if( res
fc3a0 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63   ){.      newLoc
fc3b0 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c  ktype = SHARED_L
fc3c0 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  OCK;.    }else{.
fc3d0 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47 65        error = Ge
fc3e0 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20 20  tLastError();.  
fc3f0 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63    }.  }..  /* Ac
fc400 71 75 69 72 65 20 61 20 52 45 53 45 52 56 45 44  quire a RESERVED
fc410 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28   lock.  */.  if(
fc420 20 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52   locktype==RESER
fc430 56 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20  VED_LOCK && res 
fc440 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
fc450 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d  File->locktype==
fc460 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SHARED_LOCK );. 
fc470 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c     res = LockFil
fc480 65 28 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45  e(pFile->h, RESE
fc490 52 56 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c  RVED_BYTE, 0, 1,
fc4a0 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73   0);.    if( res
fc4b0 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63   ){.      newLoc
fc4c0 6b 74 79 70 65 20 3d 20 52 45 53 45 52 56 45 44  ktype = RESERVED
fc4d0 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65  _LOCK;.    }else
fc4e0 7b 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20  {.      error = 
fc4f0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a  GetLastError();.
fc500 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
fc510 41 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e  Acquire a PENDIN
fc520 47 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20 20 69 66  G lock.  */.  if
fc530 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c  ( locktype==EXCL
fc540 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 72 65  USIVE_LOCK && re
fc550 73 20 29 7b 0a 20 20 20 20 6e 65 77 4c 6f 63 6b  s ){.    newLock
fc560 74 79 70 65 20 3d 20 50 45 4e 44 49 4e 47 5f 4c  type = PENDING_L
fc570 4f 43 4b 3b 0a 20 20 20 20 67 6f 74 50 65 6e 64  OCK;.    gotPend
fc580 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 7d  ingLock = 0;.  }
fc590 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61  ..  /* Acquire a
fc5a0 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  n EXCLUSIVE lock
fc5b0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b  .  */.  if( lock
fc5c0 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f  type==EXCLUSIVE_
fc5d0 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a 20  LOCK && res ){. 
fc5e0 20 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65     assert( pFile
fc5f0 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 53 48 41 52  ->locktype>=SHAR
fc600 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72  ED_LOCK );.    r
fc610 65 73 20 3d 20 75 6e 6c 6f 63 6b 52 65 61 64 4c  es = unlockReadL
fc620 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20  ock(pFile);.    
fc630 4f 53 54 52 41 43 45 32 28 22 75 6e 72 65 61 64  OSTRACE2("unread
fc640 6c 6f 63 6b 20 3d 20 25 64 5c 6e 22 2c 20 72 65  lock = %d\n", re
fc650 73 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f  s);.    res = Lo
fc660 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c  ckFile(pFile->h,
fc670 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c 20 30   SHARED_FIRST, 0
fc680 2c 20 53 48 41 52 45 44 5f 53 49 5a 45 2c 20 30  , SHARED_SIZE, 0
fc690 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29  );.    if( res )
fc6a0 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74  {.      newLockt
fc6b0 79 70 65 20 3d 20 45 58 43 4c 55 53 49 56 45 5f  ype = EXCLUSIVE_
fc6c0 4c 4f 43 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  LOCK;.    }else{
fc6d0 0a 20 20 20 20 20 20 65 72 72 6f 72 20 3d 20 47  .      error = G
fc6e0 65 74 4c 61 73 74 45 72 72 6f 72 28 29 3b 0a 20  etLastError();. 
fc6f0 20 20 20 20 20 4f 53 54 52 41 43 45 32 28 22 65       OSTRACE2("e
fc700 72 72 6f 72 2d 63 6f 64 65 20 3d 20 25 64 5c 6e  rror-code = %d\n
fc710 22 2c 20 65 72 72 6f 72 29 3b 0a 20 20 20 20 20  ", error);.     
fc720 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69   getReadLock(pFi
fc730 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  le);.    }.  }..
fc740 20 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 68    /* If we are h
fc750 6f 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e 47  olding a PENDING
fc760 20 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68 74   lock that ought
fc770 20 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64 2c   to be released,
fc780 20 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 61   then.  ** relea
fc790 73 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a  se it now..  */.
fc7a0 20 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e 67    if( gotPending
fc7b0 4c 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 65  Lock && locktype
fc7c0 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b  ==SHARED_LOCK ){
fc7d0 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28  .    UnlockFile(
fc7e0 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e  pFile->h, PENDIN
fc7f0 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29  G_BYTE, 0, 1, 0)
fc800 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61  ;.  }..  /* Upda
fc810 74 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20  te the state of 
fc820 74 68 65 20 6c 6f 63 6b 20 68 61 73 20 68 65 6c  the lock has hel
fc830 64 20 69 6e 20 74 68 65 20 66 69 6c 65 20 64 65  d in the file de
fc840 73 63 72 69 70 74 6f 72 20 74 68 65 6e 0a 20 20  scriptor then.  
fc850 2a 2a 20 72 65 74 75 72 6e 20 74 68 65 20 61 70  ** return the ap
fc860 70 72 6f 70 72 69 61 74 65 20 72 65 73 75 6c 74  propriate result
fc870 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   code..  */.  if
fc880 28 20 72 65 73 20 29 7b 0a 20 20 20 20 72 63 20  ( res ){.    rc 
fc890 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  = SQLITE_OK;.  }
fc8a0 65 6c 73 65 7b 0a 20 20 20 20 4f 53 54 52 41 43  else{.    OSTRAC
fc8b0 45 34 28 22 4c 4f 43 4b 20 46 41 49 4c 45 44 20  E4("LOCK FAILED 
fc8c0 25 64 20 74 72 79 69 6e 67 20 66 6f 72 20 25 64  %d trying for %d
fc8d0 20 62 75 74 20 67 6f 74 20 25 64 5c 6e 22 2c 20   but got %d\n", 
fc8e0 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20  pFile->h,.      
fc8f0 20 20 20 20 20 6c 6f 63 6b 74 79 70 65 2c 20 6e       locktype, n
fc900 65 77 4c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 20  ewLocktype);.   
fc910 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
fc920 6f 20 3d 20 65 72 72 6f 72 3b 0a 20 20 20 20 72  o = error;.    r
fc930 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
fc940 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f  .  }.  pFile->lo
fc950 63 6b 74 79 70 65 20 3d 20 28 75 38 29 6e 65 77  cktype = (u8)new
fc960 4c 6f 63 6b 74 79 70 65 3b 0a 20 20 72 65 74 75  Locktype;.  retu
fc970 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
fc980 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65  This routine che
fc990 63 6b 73 20 69 66 20 74 68 65 72 65 20 69 73 20  cks if there is 
fc9a0 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  a RESERVED lock 
fc9b0 68 65 6c 64 20 6f 6e 20 74 68 65 20 73 70 65 63  held on the spec
fc9c0 69 66 69 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79  ified.** file by
fc9d0 20 74 68 69 73 20 6f 72 20 61 6e 79 20 6f 74 68   this or any oth
fc9e0 65 72 20 70 72 6f 63 65 73 73 2e 20 49 66 20 73  er process. If s
fc9f0 75 63 68 20 61 20 6c 6f 63 6b 20 69 73 20 68 65  uch a lock is he
fca00 6c 64 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f  ld, return.** no
fca10 6e 2d 7a 65 72 6f 2c 20 6f 74 68 65 72 77 69 73  n-zero, otherwis
fca20 65 20 7a 65 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69  e zero..*/.stati
fca30 63 20 69 6e 74 20 77 69 6e 43 68 65 63 6b 52 65  c int winCheckRe
fca40 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74  servedLock(sqlit
fca50 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
fca60 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e   *pResOut){.  in
fca70 74 20 72 63 3b 0a 20 20 77 69 6e 46 69 6c 65 20  t rc;.  winFile 
fca80 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c  *pFile = (winFil
fca90 65 2a 29 69 64 3b 0a 0a 20 20 61 73 73 65 72 74  e*)id;..  assert
fcaa0 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 69 66 28  ( id!=0 );.  if(
fcab0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
fcac0 3e 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  >=RESERVED_LOCK 
fcad0 29 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20  ){.    rc = 1;. 
fcae0 20 20 20 4f 53 54 52 41 43 45 33 28 22 54 45 53     OSTRACE3("TES
fcaf0 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20  T WR-LOCK %d %d 
fcb00 28 6c 6f 63 61 6c 29 5c 6e 22 2c 20 70 46 69 6c  (local)\n", pFil
fcb10 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d 65 6c  e->h, rc);.  }el
fcb20 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 4c 6f 63  se{.    rc = Loc
fcb30 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  kFile(pFile->h, 
fcb40 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30  RESERVED_BYTE, 0
fcb50 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  , 1, 0);.    if(
fcb60 20 72 63 20 29 7b 0a 20 20 20 20 20 20 55 6e 6c   rc ){.      Unl
fcb70 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68  ockFile(pFile->h
fcb80 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c  , RESERVED_BYTE,
fcb90 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d   0, 1, 0);.    }
fcba0 0a 20 20 20 20 72 63 20 3d 20 21 72 63 3b 0a 20  .    rc = !rc;. 
fcbb0 20 20 20 4f 53 54 52 41 43 45 33 28 22 54 45 53     OSTRACE3("TES
fcbc0 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20  T WR-LOCK %d %d 
fcbd0 28 72 65 6d 6f 74 65 29 5c 6e 22 2c 20 70 46 69  (remote)\n", pFi
fcbe0 6c 65 2d 3e 68 2c 20 72 63 29 3b 0a 20 20 7d 0a  le->h, rc);.  }.
fcbf0 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 63 3b    *pResOut = rc;
fcc00 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
fcc10 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f  _OK;.}../*.** Lo
fcc20 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  wer the locking 
fcc30 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20 64 65  level on file de
fcc40 73 63 72 69 70 74 6f 72 20 69 64 20 74 6f 20 6c  scriptor id to l
fcc50 6f 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79  ocktype.  lockty
fcc60 70 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69  pe.** must be ei
fcc70 74 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20  ther NO_LOCK or 
fcc80 53 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a  SHARED_LOCK..**.
fcc90 2a 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e  ** If the lockin
fcca0 67 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66  g level of the f
fccb0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69  ile descriptor i
fccc0 73 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20  s already at or 
fccd0 62 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71  below.** the req
fcce0 75 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c  uested locking l
fccf0 65 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69  evel, this routi
fcd00 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ne is a no-op..*
fcd10 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70  *.** It is not p
fcd20 6f 73 73 69 62 6c 65 20 66 6f 72 20 74 68 69 73  ossible for this
fcd30 20 72 6f 75 74 69 6e 65 20 74 6f 20 66 61 69 6c   routine to fail
fcd40 20 69 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61   if the second a
fcd50 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 4e 4f  rgument.** is NO
fcd60 5f 4c 4f 43 4b 2e 20 20 49 66 20 74 68 65 20 73  _LOCK.  If the s
fcd70 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69  econd argument i
fcd80 73 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 74 68  s SHARED_LOCK th
fcd90 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a  en this routine.
fcda0 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72 6e 20  ** might return 
fcdb0 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 2a 2f  SQLITE_IOERR;.*/
fcdc0 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 55  .static int winU
fcdd0 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  nlock(sqlite3_fi
fcde0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b  le *id, int lock
fcdf0 74 79 70 65 29 7b 0a 20 20 69 6e 74 20 74 79 70  type){.  int typ
fce00 65 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46  e;.  winFile *pF
fce10 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29  ile = (winFile*)
fce20 69 64 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  id;.  int rc = S
fce30 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 61 73 73 65  QLITE_OK;.  asse
fce40 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b 0a  rt( pFile!=0 );.
fce50 20 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79    assert( lockty
fce60 70 65 3c 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe<=SHARED_LOCK 
fce70 29 3b 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55  );.  OSTRACE5("U
fce80 4e 4c 4f 43 4b 20 25 64 20 74 6f 20 25 64 20 77  NLOCK %d to %d w
fce90 61 73 20 25 64 28 25 64 29 5c 6e 22 2c 20 70 46  as %d(%d)\n", pF
fcea0 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65  ile->h, locktype
fceb0 2c 0a 20 20 20 20 20 20 20 20 20 20 70 46 69 6c  ,.          pFil
fcec0 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69  e->locktype, pFi
fced0 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79  le->sharedLockBy
fcee0 74 65 29 3b 0a 20 20 74 79 70 65 20 3d 20 70 46  te);.  type = pF
fcef0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20  ile->locktype;. 
fcf00 20 69 66 28 20 74 79 70 65 3e 3d 45 58 43 4c 55   if( type>=EXCLU
fcf10 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  SIVE_LOCK ){.   
fcf20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c   UnlockFile(pFil
fcf30 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52  e->h, SHARED_FIR
fcf40 53 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 49  ST, 0, SHARED_SI
fcf50 5a 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  ZE, 0);.    if( 
fcf60 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44  locktype==SHARED
fcf70 5f 4c 4f 43 4b 20 26 26 20 21 67 65 74 52 65 61  _LOCK && !getRea
fcf80 64 4c 6f 63 6b 28 70 46 69 6c 65 29 20 29 7b 0a  dLock(pFile) ){.
fcf90 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73 68        /* This sh
fcfa0 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65  ould never happe
fcfb0 6e 2e 20 20 57 65 20 73 68 6f 75 6c 64 20 61 6c  n.  We should al
fcfc0 77 61 79 73 20 62 65 20 61 62 6c 65 20 74 6f 0a  ways be able to.
fcfd0 20 20 20 20 20 20 2a 2a 20 72 65 61 63 71 75 69        ** reacqui
fcfe0 72 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b  re the read lock
fcff0 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 53   */.      rc = S
fd000 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f  QLITE_IOERR_UNLO
fd010 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  CK;.    }.  }.  
fd020 69 66 28 20 74 79 70 65 3e 3d 52 45 53 45 52 56  if( type>=RESERV
fd030 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 55  ED_LOCK ){.    U
fd040 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d  nlockFile(pFile-
fd050 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54  >h, RESERVED_BYT
fd060 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d  E, 0, 1, 0);.  }
fd070 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  .  if( locktype=
fd080 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20 74 79 70 65  =NO_LOCK && type
fd090 3e 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b  >=SHARED_LOCK ){
fd0a0 0a 20 20 20 20 75 6e 6c 6f 63 6b 52 65 61 64 4c  .    unlockReadL
fd0b0 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 7d 0a  ock(pFile);.  }.
fd0c0 20 20 69 66 28 20 74 79 70 65 3e 3d 50 45 4e 44    if( type>=PEND
fd0d0 49 4e 47 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  ING_LOCK ){.    
fd0e0 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c 65  UnlockFile(pFile
fd0f0 2d 3e 68 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54  ->h, PENDING_BYT
fd100 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d  E, 0, 1, 0);.  }
fd110 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  .  pFile->lockty
fd120 70 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79 70  pe = (u8)locktyp
fd130 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  e;.  return rc;.
fd140 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f 6c  }../*.** Control
fd150 20 61 6e 64 20 71 75 65 72 79 20 6f 66 20 74 68   and query of th
fd160 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 61 6e 64  e open file hand
fd170 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
fd180 74 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72 6f 6c  t winFileControl
fd190 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
fd1a0 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20  d, int op, void 
fd1b0 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74 63 68  *pArg){.  switch
fd1c0 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65  ( op ){.    case
fd1d0 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f   SQLITE_FCNTL_LO
fd1e0 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20 20 20  CKSTATE: {.     
fd1f0 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d 20 28   *(int*)pArg = (
fd200 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c  (winFile*)id)->l
fd210 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20 20 72  ocktype;.      r
fd220 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
fd230 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
fd240 53 51 4c 49 54 45 5f 4c 41 53 54 5f 45 52 52 4e  SQLITE_LAST_ERRN
fd250 4f 3a 20 7b 0a 20 20 20 20 20 20 2a 28 69 6e 74  O: {.      *(int
fd260 2a 29 70 41 72 67 20 3d 20 28 69 6e 74 29 28 28  *)pArg = (int)((
fd270 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 61  winFile*)id)->la
fd280 73 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72  stErrno;.      r
fd290 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
fd2a0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
fd2b0 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
fd2c0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
fd2d0 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a  n the sector siz
fd2e0 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74 68  e in bytes of th
fd2f0 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62 6c 6f  e underlying blo
fd300 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a 2a 2a  ck device for.**
fd310 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 66   the specified f
fd320 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61 6c 6d  ile. This is alm
fd330 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32 20 62  ost always 512 b
fd340 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20 62 65  ytes, but may be
fd350 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72 20 73  .** larger for s
fd360 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a 2a 0a  ome devices..**.
fd370 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65 20 61  ** SQLite code a
fd380 73 73 75 6d 65 73 20 74 68 69 73 20 66 75 6e 63  ssumes this func
fd390 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61 69 6c  tion cannot fail
fd3a0 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75 6d 65  . It also assume
fd3b0 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74 77 6f  s that.** if two
fd3c0 20 66 69 6c 65 73 20 61 72 65 20 63 72 65 61 74   files are creat
fd3d0 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 66  ed in the same f
fd3e0 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72 65 63  ile-system direc
fd3f0 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20 61 20  tory (i.e..** a 
fd400 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 74 73  database and its
fd410 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29 20 74   journal file) t
fd420 68 61 74 20 74 68 65 20 73 65 63 74 6f 72 20 73  hat the sector s
fd430 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68 65 0a  ize will be the.
fd440 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f 74 68  ** same for both
fd450 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
fd460 77 69 6e 53 65 63 74 6f 72 53 69 7a 65 28 73 71  winSectorSize(sq
fd470 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b  lite3_file *id){
fd480 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d 30  .  assert( id!=0
fd490 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e   );.  return (in
fd4a0 74 29 28 28 28 77 69 6e 46 69 6c 65 2a 29 69 64  t)(((winFile*)id
fd4b0 29 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 3b 0a  )->sectorSize);.
fd4c0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
fd4d0 61 20 76 65 63 74 6f 72 20 6f 66 20 64 65 76 69  a vector of devi
fd4e0 63 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69  ce characteristi
fd4f0 63 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  cs..*/.static in
fd500 74 20 77 69 6e 44 65 76 69 63 65 43 68 61 72 61  t winDeviceChara
fd510 63 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74  cteristics(sqlit
fd520 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20  e3_file *id){.  
fd530 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
fd540 28 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 30  (id);.  return 0
fd550 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
fd560 76 65 63 74 6f 72 20 64 65 66 69 6e 65 73 20 61  vector defines a
fd570 6c 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 20 74  ll the methods t
fd580 68 61 74 20 63 61 6e 20 6f 70 65 72 61 74 65 20  hat can operate 
fd590 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33  on an.** sqlite3
fd5a0 5f 66 69 6c 65 20 66 6f 72 20 77 69 6e 33 32 2e  _file for win32.
fd5b0 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
fd5c0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
fd5d0 6f 64 73 20 77 69 6e 49 6f 4d 65 74 68 6f 64 20  ods winIoMethod 
fd5e0 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20  = {.  1,        
fd5f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fd600 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20  /* iVersion */. 
fd610 20 77 69 6e 43 6c 6f 73 65 2c 0a 20 20 77 69 6e   winClose,.  win
fd620 52 65 61 64 2c 0a 20 20 77 69 6e 57 72 69 74 65  Read,.  winWrite
fd630 2c 0a 20 20 77 69 6e 54 72 75 6e 63 61 74 65 2c  ,.  winTruncate,
fd640 0a 20 20 77 69 6e 53 79 6e 63 2c 0a 20 20 77 69  .  winSync,.  wi
fd650 6e 46 69 6c 65 53 69 7a 65 2c 0a 20 20 77 69 6e  nFileSize,.  win
fd660 4c 6f 63 6b 2c 0a 20 20 77 69 6e 55 6e 6c 6f 63  Lock,.  winUnloc
fd670 6b 2c 0a 20 20 77 69 6e 43 68 65 63 6b 52 65 73  k,.  winCheckRes
fd680 65 72 76 65 64 4c 6f 63 6b 2c 0a 20 20 77 69 6e  ervedLock,.  win
fd690 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20 77  FileControl,.  w
fd6a0 69 6e 53 65 63 74 6f 72 53 69 7a 65 2c 0a 20 20  inSectorSize,.  
fd6b0 77 69 6e 44 65 76 69 63 65 43 68 61 72 61 63 74  winDeviceCharact
fd6c0 65 72 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a 2a  eristics.};../**
fd6d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd6e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd6f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd710 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 72  *********.** Her
fd720 65 20 65 6e 64 73 20 74 68 65 20 49 2f 4f 20 6d  e ends the I/O m
fd730 65 74 68 6f 64 73 20 74 68 61 74 20 66 6f 72 6d  ethods that form
fd740 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f   the sqlite3_io_
fd750 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a  methods object..
fd760 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 62  **.** The next b
fd770 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 69 6d 70  lock of code imp
fd780 6c 65 6d 65 6e 74 73 20 74 68 65 20 56 46 53 20  lements the VFS 
fd790 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a 2a  methods..*******
fd7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd7e0 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f  *****/../*.** Co
fd7f0 6e 76 65 72 74 20 61 20 55 54 46 2d 38 20 66 69  nvert a UTF-8 fi
fd800 6c 65 6e 61 6d 65 20 69 6e 74 6f 20 77 68 61 74  lename into what
fd810 65 76 65 72 20 66 6f 72 6d 20 74 68 65 20 75 6e  ever form the un
fd820 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 6f 70 65 72  derlying.** oper
fd830 61 74 69 6e 67 20 73 79 73 74 65 6d 20 77 61 6e  ating system wan
fd840 74 73 20 66 69 6c 65 6e 61 6d 65 73 20 69 6e 2e  ts filenames in.
fd850 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20    Space to hold 
fd860 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73  the result.** is
fd870 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d   obtained from m
fd880 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20 62  alloc and must b
fd890 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20 63  e freed by the c
fd8a0 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69  alling.** functi
fd8b0 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
fd8c0 69 64 20 2a 63 6f 6e 76 65 72 74 55 74 66 38 46  id *convertUtf8F
fd8d0 69 6c 65 6e 61 6d 65 28 63 6f 6e 73 74 20 63 68  ilename(const ch
fd8e0 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a  ar *zFilename){.
fd8f0 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74    void *zConvert
fd900 65 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 69 73  ed = 0;.  if( is
fd910 4e 54 28 29 20 29 7b 0a 20 20 20 20 7a 43 6f 6e  NT() ){.    zCon
fd920 76 65 72 74 65 64 20 3d 20 75 74 66 38 54 6f 55  verted = utf8ToU
fd930 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65  nicode(zFilename
fd940 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20  );./* isNT() is 
fd950 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  1 if SQLITE_OS_W
fd960 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73  INCE==1, so this
fd970 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65   else is never e
fd980 78 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66  xecuted. .*/.#if
fd990 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
fd9a0 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ==0.  }else{.   
fd9b0 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75 74   zConverted = ut
fd9c0 66 38 54 6f 4d 62 63 73 28 7a 46 69 6c 65 6e 61  f8ToMbcs(zFilena
fd9d0 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  me);.#endif.  }.
fd9e0 20 20 2f 2a 20 63 61 6c 6c 65 72 20 77 69 6c 6c    /* caller will
fd9f0 20 68 61 6e 64 6c 65 20 6f 75 74 20 6f 66 20 6d   handle out of m
fda00 65 6d 6f 72 79 20 2a 2f 0a 20 20 72 65 74 75 72  emory */.  retur
fda10 6e 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 7d 0a  n zConverted;.}.
fda20 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
fda30 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e  temporary file n
fda40 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42  ame in zBuf.  zB
fda50 75 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 65  uf must be big e
fda60 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64  nough to.** hold
fda70 20 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68   at pVfs->mxPath
fda80 6e 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 2e  name characters.
fda90 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
fdaa0 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e  etTempname(int n
fdab0 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29  Buf, char *zBuf)
fdac0 7b 0a 20 20 73 74 61 74 69 63 20 63 68 61 72 20  {.  static char 
fdad0 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22  zChars[] =.    "
fdae0 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70  abcdefghijklmnop
fdaf0 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20  qrstuvwxyz".    
fdb00 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  "ABCDEFGHIJKLMNO
fdb10 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20  PQRSTUVWXYZ".   
fdb20 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20   "0123456789";. 
fdb30 20 73 69 7a 65 5f 74 20 69 2c 20 6a 3b 0a 20 20   size_t i, j;.  
fdb40 63 68 61 72 20 7a 54 65 6d 70 50 61 74 68 5b 4d  char zTempPath[M
fdb50 41 58 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69 66  AX_PATH+1];.  if
fdb60 28 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64  ( sqlite3_temp_d
fdb70 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20  irectory ){.    
fdb80 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
fdb90 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54  (MAX_PATH-30, zT
fdba0 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 73  empPath, "%s", s
fdbb0 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65  qlite3_temp_dire
fdbc0 63 74 6f 72 79 29 3b 0a 20 20 7d 65 6c 73 65 20  ctory);.  }else 
fdbd0 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20  if( isNT() ){.  
fdbe0 20 20 63 68 61 72 20 2a 7a 4d 75 6c 74 69 3b 0a    char *zMulti;.
fdbf0 20 20 20 20 57 43 48 41 52 20 7a 57 69 64 65 50      WCHAR zWideP
fdc00 61 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20  ath[MAX_PATH];. 
fdc10 20 20 20 47 65 74 54 65 6d 70 50 61 74 68 57 28     GetTempPathW(
fdc20 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 57 69  MAX_PATH-30, zWi
fdc30 64 65 50 61 74 68 29 3b 0a 20 20 20 20 7a 4d 75  dePath);.    zMu
fdc40 6c 74 69 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55  lti = unicodeToU
fdc50 74 66 38 28 7a 57 69 64 65 50 61 74 68 29 3b 0a  tf8(zWidePath);.
fdc60 20 20 20 20 69 66 28 20 7a 4d 75 6c 74 69 20 29      if( zMulti )
fdc70 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
fdc80 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54  snprintf(MAX_PAT
fdc90 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c  H-30, zTempPath,
fdca0 20 22 25 73 22 2c 20 7a 4d 75 6c 74 69 29 3b 0a   "%s", zMulti);.
fdcb0 20 20 20 20 20 20 66 72 65 65 28 7a 4d 75 6c 74        free(zMult
fdcc0 69 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  i);.    }else{. 
fdcd0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
fdce0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
fdcf0 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69  /* isNT() is 1 i
fdd00 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
fdd10 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c  E==1, so this el
fdd20 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63  se is never exec
fdd30 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20  uted. .** Since 
fdd40 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f  the ASCII versio
fdd50 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f  n of these Windo
fdd60 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78  ws API do not ex
fdd70 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a  ist for WINCE,.*
fdd80 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74  * it's important
fdd90 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63   to not referenc
fdda0 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45  e them for WINCE
fddb0 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20   builds..*/.#if 
fddc0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
fddd0 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  =0.  }else{.    
fdde0 63 68 61 72 20 2a 7a 55 74 66 38 3b 0a 20 20 20  char *zUtf8;.   
fddf0 20 63 68 61 72 20 7a 4d 62 63 73 50 61 74 68 5b   char zMbcsPath[
fde00 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 20 20 20 47  MAX_PATH];.    G
fde10 65 74 54 65 6d 70 50 61 74 68 41 28 4d 41 58 5f  etTempPathA(MAX_
fde20 50 41 54 48 2d 33 30 2c 20 7a 4d 62 63 73 50 61  PATH-30, zMbcsPa
fde30 74 68 29 3b 0a 20 20 20 20 7a 55 74 66 38 20 3d  th);.    zUtf8 =
fde40 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d   sqlite3_win32_m
fde50 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 4d 62 63  bcs_to_utf8(zMbc
fde60 73 50 61 74 68 29 3b 0a 20 20 20 20 69 66 28 20  sPath);.    if( 
fde70 7a 55 74 66 38 20 29 7b 0a 20 20 20 20 20 20 73  zUtf8 ){.      s
fde80 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
fde90 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65  MAX_PATH-30, zTe
fdea0 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 7a 55  mpPath, "%s", zU
fdeb0 74 66 38 29 3b 0a 20 20 20 20 20 20 66 72 65 65  tf8);.      free
fdec0 28 7a 55 74 66 38 29 3b 0a 20 20 20 20 7d 65 6c  (zUtf8);.    }el
fded0 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  se{.      return
fdee0 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
fdef0 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
fdf00 20 20 66 6f 72 28 69 3d 73 71 6c 69 74 65 33 53    for(i=sqlite3S
fdf10 74 72 6c 65 6e 33 30 28 7a 54 65 6d 70 50 61 74  trlen30(zTempPat
fdf20 68 29 3b 20 69 3e 30 20 26 26 20 7a 54 65 6d 70  h); i>0 && zTemp
fdf30 50 61 74 68 5b 69 2d 31 5d 3d 3d 27 5c 5c 27 3b  Path[i-1]=='\\';
fdf40 20 69 2d 2d 29 7b 7d 0a 20 20 7a 54 65 6d 70 50   i--){}.  zTempP
fdf50 61 74 68 5b 69 5d 20 3d 20 30 3b 0a 20 20 73 71  ath[i] = 0;.  sq
fdf60 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
fdf70 42 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20  Buf-30, zBuf,.  
fdf80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fdf90 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45   "%s\\"SQLITE_TE
fdfa0 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20  MP_FILE_PREFIX, 
fdfb0 7a 54 65 6d 70 50 61 74 68 29 3b 0a 20 20 6a 20  zTempPath);.  j 
fdfc0 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
fdfd0 30 28 7a 42 75 66 29 3b 0a 20 20 73 71 6c 69 74  0(zBuf);.  sqlit
fdfe0 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 32 30  e3_randomness(20
fdff0 2c 20 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 66  , &zBuf[j]);.  f
fe000 6f 72 28 69 3d 30 3b 20 69 3c 32 30 3b 20 69 2b  or(i=0; i<20; i+
fe010 2b 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 7a 42 75  +, j++){.    zBu
fe020 66 5b 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43 68  f[j] = (char)zCh
fe030 61 72 73 5b 20 28 28 75 6e 73 69 67 6e 65 64 20  ars[ ((unsigned 
fe040 63 68 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73  char)zBuf[j])%(s
fe050 69 7a 65 6f 66 28 7a 43 68 61 72 73 29 2d 31 29  izeof(zChars)-1)
fe060 20 5d 3b 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6a   ];.  }.  zBuf[j
fe070 5d 20 3d 20 30 3b 0a 20 20 4f 53 54 52 41 43 45  ] = 0;.  OSTRACE
fe080 32 28 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45  2("TEMP FILENAME
fe090 3a 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a  : %s\n", zBuf);.
fe0a0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
fe0b0 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  OK; .}../*.** Th
fe0c0 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f  e return value o
fe0d0 66 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73  f getLastErrorMs
fe0e0 67 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66 20  g.** is zero if 
fe0f0 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  the error messag
fe100 65 20 66 69 74 73 20 69 6e 20 74 68 65 20 62 75  e fits in the bu
fe110 66 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72  ffer, or non-zer
fe120 6f 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 28  o.** otherwise (
fe130 69 66 20 74 68 65 20 6d 65 73 73 61 67 65 20 77  if the message w
fe140 61 73 20 74 72 75 6e 63 61 74 65 64 29 2e 0a 2a  as truncated)..*
fe150 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74  /.static int get
fe160 4c 61 73 74 45 72 72 6f 72 4d 73 67 28 69 6e 74  LastErrorMsg(int
fe170 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75   nBuf, char *zBu
fe180 66 29 7b 0a 20 20 2f 2a 20 46 6f 72 6d 61 74 4d  f){.  /* FormatM
fe190 65 73 73 61 67 65 20 72 65 74 75 72 6e 73 20 30  essage returns 0
fe1a0 20 6f 6e 20 66 61 69 6c 75 72 65 2e 20 20 4f 74   on failure.  Ot
fe1b0 68 65 72 77 69 73 65 20 69 74 0a 20 20 2a 2a 20  herwise it.  ** 
fe1c0 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62  returns the numb
fe1d0 65 72 20 6f 66 20 54 43 48 41 52 73 20 77 72 69  er of TCHARs wri
fe1e0 74 74 65 6e 20 74 6f 20 74 68 65 20 6f 75 74 70  tten to the outp
fe1f0 75 74 0a 20 20 2a 2a 20 62 75 66 66 65 72 2c 20  ut.  ** buffer, 
fe200 65 78 63 6c 75 64 69 6e 67 20 74 68 65 20 74 65  excluding the te
fe210 72 6d 69 6e 61 74 69 6e 67 20 6e 75 6c 6c 20 63  rminating null c
fe220 68 61 72 2e 0a 20 20 2a 2f 0a 20 20 44 57 4f 52  har..  */.  DWOR
fe230 44 20 65 72 72 6f 72 20 3d 20 47 65 74 4c 61 73  D error = GetLas
fe240 74 45 72 72 6f 72 28 29 3b 0a 20 20 44 57 4f 52  tError();.  DWOR
fe250 44 20 64 77 4c 65 6e 20 3d 20 30 3b 0a 20 20 63  D dwLen = 0;.  c
fe260 68 61 72 20 2a 7a 4f 75 74 3b 0a 0a 20 20 69 66  har *zOut;..  if
fe270 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20  ( isNT() ){.    
fe280 57 43 48 41 52 20 2a 7a 54 65 6d 70 57 69 64 65  WCHAR *zTempWide
fe290 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 64 77 4c   = NULL;.    dwL
fe2a0 65 6e 20 3d 20 46 6f 72 6d 61 74 4d 65 73 73 61  en = FormatMessa
fe2b0 67 65 57 28 46 4f 52 4d 41 54 5f 4d 45 53 53 41  geW(FORMAT_MESSA
fe2c0 47 45 5f 41 4c 4c 4f 43 41 54 45 5f 42 55 46 46  GE_ALLOCATE_BUFF
fe2d0 45 52 20 7c 20 46 4f 52 4d 41 54 5f 4d 45 53 53  ER | FORMAT_MESS
fe2e0 41 47 45 5f 46 52 4f 4d 5f 53 59 53 54 45 4d 20  AGE_FROM_SYSTEM 
fe2f0 7c 20 46 4f 52 4d 41 54 5f 4d 45 53 53 41 47 45  | FORMAT_MESSAGE
fe300 5f 49 47 4e 4f 52 45 5f 49 4e 53 45 52 54 53 2c  _IGNORE_INSERTS,
fe310 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
fe320 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c 4c              NULL
fe330 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
fe340 20 20 20 20 20 20 20 20 20 20 20 20 20 65 72 72               err
fe350 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  or,.            
fe360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
fe370 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
fe380 20 20 20 20 20 20 20 20 20 20 20 20 20 28 4c 50               (LP
fe390 57 53 54 52 29 20 26 7a 54 65 6d 70 57 69 64 65  WSTR) &zTempWide
fe3a0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
fe3b0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 0a               0,.
fe3c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe3d0 20 20 20 20 20 20 20 20 20 20 20 30 29 3b 0a 20             0);. 
fe3e0 20 20 20 69 66 28 20 64 77 4c 65 6e 20 3e 20 30     if( dwLen > 0
fe3f0 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 61 6c 6c   ){.      /* all
fe400 6f 63 61 74 65 20 61 20 62 75 66 66 65 72 20 61  ocate a buffer a
fe410 6e 64 20 63 6f 6e 76 65 72 74 20 74 6f 20 55 54  nd convert to UT
fe420 46 38 20 2a 2f 0a 20 20 20 20 20 20 7a 4f 75 74  F8 */.      zOut
fe430 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55 74 66 38   = unicodeToUtf8
fe440 28 7a 54 65 6d 70 57 69 64 65 29 3b 0a 20 20 20  (zTempWide);.   
fe450 20 20 20 2f 2a 20 66 72 65 65 20 74 68 65 20 73     /* free the s
fe460 79 73 74 65 6d 20 62 75 66 66 65 72 20 61 6c 6c  ystem buffer all
fe470 6f 63 61 74 65 64 20 62 79 20 46 6f 72 6d 61 74  ocated by Format
fe480 4d 65 73 73 61 67 65 20 2a 2f 0a 20 20 20 20 20  Message */.     
fe490 20 4c 6f 63 61 6c 46 72 65 65 28 7a 54 65 6d 70   LocalFree(zTemp
fe4a0 57 69 64 65 29 3b 0a 20 20 20 20 7d 0a 2f 2a 20  Wide);.    }./* 
fe4b0 69 73 4e 54 28 29 20 69 73 20 31 20 69 66 20 53  isNT() is 1 if S
fe4c0 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d  QLITE_OS_WINCE==
fe4d0 31 2c 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20  1, so this else 
fe4e0 69 73 20 6e 65 76 65 72 20 65 78 65 63 75 74 65  is never execute
fe4f0 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65  d. .** Since the
fe500 20 41 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f   ASCII version o
fe510 66 20 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20  f these Windows 
fe520 41 50 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74  API do not exist
fe530 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69   for WINCE,.** i
fe540 74 27 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f  t's important to
fe550 20 6e 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74   not reference t
fe560 68 65 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75  hem for WINCE bu
fe570 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c  ilds..*/.#if SQL
fe580 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a  ITE_OS_WINCE==0.
fe590 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61    }else{.    cha
fe5a0 72 20 2a 7a 54 65 6d 70 20 3d 20 4e 55 4c 4c 3b  r *zTemp = NULL;
fe5b0 0a 20 20 20 20 64 77 4c 65 6e 20 3d 20 46 6f 72  .    dwLen = For
fe5c0 6d 61 74 4d 65 73 73 61 67 65 41 28 46 4f 52 4d  matMessageA(FORM
fe5d0 41 54 5f 4d 45 53 53 41 47 45 5f 41 4c 4c 4f 43  AT_MESSAGE_ALLOC
fe5e0 41 54 45 5f 42 55 46 46 45 52 20 7c 20 46 4f 52  ATE_BUFFER | FOR
fe5f0 4d 41 54 5f 4d 45 53 53 41 47 45 5f 46 52 4f 4d  MAT_MESSAGE_FROM
fe600 5f 53 59 53 54 45 4d 20 7c 20 46 4f 52 4d 41 54  _SYSTEM | FORMAT
fe610 5f 4d 45 53 53 41 47 45 5f 49 47 4e 4f 52 45 5f  _MESSAGE_IGNORE_
fe620 49 4e 53 45 52 54 53 2c 0a 20 20 20 20 20 20 20  INSERTS,.       
fe630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe640 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20 20 20      NULL,.      
fe650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe660 20 20 20 20 20 65 72 72 6f 72 2c 0a 20 20 20 20       error,.    
fe670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe680 20 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20         0,.      
fe690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe6a0 20 20 20 20 20 28 4c 50 53 54 52 29 20 26 7a 54       (LPSTR) &zT
fe6b0 65 6d 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20  emp,.           
fe6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe6d0 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
fe6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 29                0)
fe6f0 3b 0a 20 20 20 20 69 66 28 20 64 77 4c 65 6e 20  ;.    if( dwLen 
fe700 3e 20 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  > 0 ){.      /* 
fe710 61 6c 6c 6f 63 61 74 65 20 61 20 62 75 66 66 65  allocate a buffe
fe720 72 20 61 6e 64 20 63 6f 6e 76 65 72 74 20 74 6f  r and convert to
fe730 20 55 54 46 38 20 2a 2f 0a 20 20 20 20 20 20 7a   UTF8 */.      z
fe740 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 77 69  Out = sqlite3_wi
fe750 6e 33 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38  n32_mbcs_to_utf8
fe760 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 2f  (zTemp);.      /
fe770 2a 20 66 72 65 65 20 74 68 65 20 73 79 73 74 65  * free the syste
fe780 6d 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74  m buffer allocat
fe790 65 64 20 62 79 20 46 6f 72 6d 61 74 4d 65 73 73  ed by FormatMess
fe7a0 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 4c 6f 63  age */.      Loc
fe7b0 61 6c 46 72 65 65 28 7a 54 65 6d 70 29 3b 0a 20  alFree(zTemp);. 
fe7c0 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
fe7d0 20 20 69 66 28 20 30 20 3d 3d 20 64 77 4c 65 6e    if( 0 == dwLen
fe7e0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
fe7f0 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a  snprintf(nBuf, z
fe800 42 75 66 2c 20 22 4f 73 45 72 72 6f 72 20 30 78  Buf, "OsError 0x
fe810 25 78 20 28 25 75 29 22 2c 20 65 72 72 6f 72 2c  %x (%u)", error,
fe820 20 65 72 72 6f 72 29 3b 0a 20 20 7d 65 6c 73 65   error);.  }else
fe830 7b 0a 20 20 20 20 2f 2a 20 63 6f 70 79 20 61 20  {.    /* copy a 
fe840 6d 61 78 69 6d 75 6d 20 6f 66 20 6e 42 75 66 20  maximum of nBuf 
fe850 63 68 61 72 73 20 74 6f 20 6f 75 74 70 75 74 20  chars to output 
fe860 62 75 66 66 65 72 20 2a 2f 0a 20 20 20 20 73 71  buffer */.    sq
fe870 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
fe880 42 75 66 2c 20 7a 42 75 66 2c 20 22 25 73 22 2c  Buf, zBuf, "%s",
fe890 20 7a 4f 75 74 29 3b 0a 20 20 20 20 2f 2a 20 66   zOut);.    /* f
fe8a0 72 65 65 20 74 68 65 20 55 54 46 38 20 62 75 66  ree the UTF8 buf
fe8b0 66 65 72 20 2a 2f 0a 20 20 20 20 66 72 65 65 28  fer */.    free(
fe8c0 7a 4f 75 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74  zOut);.  }.  ret
fe8d0 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
fe8e0 4f 70 65 6e 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a  Open a file..*/.
fe8f0 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e 4f 70  static int winOp
fe900 65 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  en(.  sqlite3_vf
fe910 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20  s *pVfs,        
fe920 2f 2a 20 4e 6f 74 20 75 73 65 64 20 2a 2f 0a 20  /* Not used */. 
fe930 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
fe940 6d 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  me,        /* Na
fe950 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 28  me of the file (
fe960 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69  UTF-8) */.  sqli
fe970 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20  te3_file *id,   
fe980 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
fe990 68 65 20 53 51 4c 69 74 65 20 66 69 6c 65 20 68  he SQLite file h
fe9a0 61 6e 64 6c 65 20 68 65 72 65 20 2a 2f 0a 20 20  andle here */.  
fe9b0 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20  int flags,      
fe9c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65            /* Ope
fe9d0 6e 20 6d 6f 64 65 20 66 6c 61 67 73 20 2a 2f 0a  n mode flags */.
fe9e0 20 20 69 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73    int *pOutFlags
fe9f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
fea00 74 61 74 75 73 20 72 65 74 75 72 6e 20 66 6c 61  tatus return fla
fea10 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 41 4e 44 4c  gs */.){.  HANDL
fea20 45 20 68 3b 0a 20 20 44 57 4f 52 44 20 64 77 44  E h;.  DWORD dwD
fea30 65 73 69 72 65 64 41 63 63 65 73 73 3b 0a 20 20  esiredAccess;.  
fea40 44 57 4f 52 44 20 64 77 53 68 61 72 65 4d 6f 64  DWORD dwShareMod
fea50 65 3b 0a 20 20 44 57 4f 52 44 20 64 77 43 72 65  e;.  DWORD dwCre
fea60 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e  ationDisposition
fea70 3b 0a 20 20 44 57 4f 52 44 20 64 77 46 6c 61 67  ;.  DWORD dwFlag
fea80 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d  sAndAttributes =
fea90 20 30 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f   0;.#if SQLITE_O
feaa0 53 5f 57 49 4e 43 45 0a 20 20 69 6e 74 20 69 73  S_WINCE.  int is
feab0 54 65 6d 70 20 3d 20 30 3b 0a 23 65 6e 64 69 66  Temp = 0;.#endif
feac0 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c  .  winFile *pFil
fead0 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64  e = (winFile*)id
feae0 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65  ;.  void *zConve
feaf0 72 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 20  rted;           
feb00 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 6e 61 6d        /* Filenam
feb10 65 20 69 6e 20 4f 53 20 65 6e 63 6f 64 69 6e 67  e in OS encoding
feb20 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
feb30 20 2a 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a 4e   *zUtf8Name = zN
feb40 61 6d 65 3b 20 20 20 20 2f 2a 20 46 69 6c 65 6e  ame;    /* Filen
feb50 61 6d 65 20 69 6e 20 55 54 46 2d 38 20 65 6e 63  ame in UTF-8 enc
feb60 6f 64 69 6e 67 20 2a 2f 0a 20 20 63 68 61 72 20  oding */.  char 
feb70 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50 41 54  zTmpname[MAX_PAT
feb80 48 2b 31 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20  H+1];        /* 
feb90 42 75 66 66 65 72 20 75 73 65 64 20 74 6f 20 63  Buffer used to c
feba0 72 65 61 74 65 20 74 65 6d 70 20 66 69 6c 65 6e  reate temp filen
febb0 61 6d 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ame */..  assert
febc0 28 20 69 64 21 3d 30 20 29 3b 0a 20 20 55 4e 55  ( id!=0 );.  UNU
febd0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56  SED_PARAMETER(pV
febe0 66 73 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  fs);..  /* If th
febf0 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
fec00 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  t to this functi
fec10 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 67 65 6e 65  on is NULL, gene
fec20 72 61 74 65 20 61 20 0a 20 20 2a 2a 20 74 65 6d  rate a .  ** tem
fec30 70 6f 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65  porary file name
fec40 20 74 6f 20 75 73 65 20 0a 20 20 2a 2f 0a 20 20   to use .  */.  
fec50 69 66 28 20 21 7a 55 74 66 38 4e 61 6d 65 20 29  if( !zUtf8Name )
fec60 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 67  {.    int rc = g
fec70 65 74 54 65 6d 70 6e 61 6d 65 28 4d 41 58 5f 50  etTempname(MAX_P
fec80 41 54 48 2b 31 2c 20 7a 54 6d 70 6e 61 6d 65 29  ATH+1, zTmpname)
fec90 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
feca0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
fecb0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
fecc0 7d 0a 20 20 20 20 7a 55 74 66 38 4e 61 6d 65 20  }.    zUtf8Name 
fecd0 3d 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a  = zTmpname;.  }.
fece0 0a 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 68  .  /* Convert th
fecf0 65 20 66 69 6c 65 6e 61 6d 65 20 74 6f 20 74 68  e filename to th
fed00 65 20 73 79 73 74 65 6d 20 65 6e 63 6f 64 69 6e  e system encodin
fed10 67 2e 20 2a 2f 0a 20 20 7a 43 6f 6e 76 65 72 74  g. */.  zConvert
fed20 65 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38  ed = convertUtf8
fed30 46 69 6c 65 6e 61 6d 65 28 7a 55 74 66 38 4e 61  Filename(zUtf8Na
fed40 6d 65 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76  me);.  if( zConv
fed50 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20  erted==0 ){.    
fed60 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
fed70 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  MEM;.  }..  if( 
fed80 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
fed90 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b  PEN_READWRITE ){
feda0 0a 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63  .    dwDesiredAc
fedb0 63 65 73 73 20 3d 20 47 45 4e 45 52 49 43 5f 52  cess = GENERIC_R
fedc0 45 41 44 20 7c 20 47 45 4e 45 52 49 43 5f 57 52  EAD | GENERIC_WR
fedd0 49 54 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ITE;.  }else{.  
fede0 20 20 64 77 44 65 73 69 72 65 64 41 63 63 65 73    dwDesiredAcces
fedf0 73 20 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 44  s = GENERIC_READ
fee00 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 51 4c 49 54  ;.  }.  /* SQLIT
fee10 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45  E_OPEN_EXCLUSIVE
fee20 20 69 73 20 75 73 65 64 20 74 6f 20 6d 61 6b 65   is used to make
fee30 20 73 75 72 65 20 74 68 61 74 20 61 20 6e 65 77   sure that a new
fee40 20 66 69 6c 65 20 69 73 20 0a 20 20 2a 2a 20 63   file is .  ** c
fee50 72 65 61 74 65 64 2e 20 53 51 4c 69 74 65 20 64  reated. SQLite d
fee60 6f 65 73 6e 27 74 20 75 73 65 20 69 74 20 74 6f  oesn't use it to
fee70 20 69 6e 64 69 63 61 74 65 20 22 65 78 63 6c 75   indicate "exclu
fee80 73 69 76 65 20 61 63 63 65 73 73 22 20 0a 20 20  sive access" .  
fee90 2a 2a 20 61 73 20 69 74 20 69 73 20 75 73 75 61  ** as it is usua
feea0 6c 6c 79 20 75 6e 64 65 72 73 74 6f 6f 64 2e 0a  lly understood..
feeb0 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 21 28    */.  assert(!(
feec0 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
feed0 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 29 20 7c  PEN_EXCLUSIVE) |
feee0 7c 20 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54  | (flags & SQLIT
feef0 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 29 3b  E_OPEN_CREATE));
fef00 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53  .  if( flags & S
fef10 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55  QLITE_OPEN_EXCLU
fef20 53 49 56 45 20 29 7b 0a 20 20 20 20 2f 2a 20 43  SIVE ){.    /* C
fef30 72 65 61 74 65 73 20 61 20 6e 65 77 20 66 69 6c  reates a new fil
fef40 65 2c 20 6f 6e 6c 79 20 69 66 20 69 74 20 64 6f  e, only if it do
fef50 65 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65  es not already e
fef60 78 69 73 74 2e 20 2a 2f 0a 20 20 20 20 2f 2a 20  xist. */.    /* 
fef70 49 66 20 74 68 65 20 66 69 6c 65 20 65 78 69 73  If the file exis
fef80 74 73 2c 20 69 74 20 66 61 69 6c 73 2e 20 2a 2f  ts, it fails. */
fef90 0a 20 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44  .    dwCreationD
fefa0 69 73 70 6f 73 69 74 69 6f 6e 20 3d 20 43 52 45  isposition = CRE
fefb0 41 54 45 5f 4e 45 57 3b 0a 20 20 7d 65 6c 73 65  ATE_NEW;.  }else
fefc0 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
fefd0 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20  ITE_OPEN_CREATE 
fefe0 29 7b 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 65  ){.    /* Open e
feff0 78 69 73 74 69 6e 67 20 66 69 6c 65 2c 20 6f 72  xisting file, or
ff000 20 63 72 65 61 74 65 20 69 66 20 69 74 20 64 6f   create if it do
ff010 65 73 6e 27 74 20 65 78 69 73 74 20 2a 2f 0a 20  esn't exist */. 
ff020 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73     dwCreationDis
ff030 70 6f 73 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f  position = OPEN_
ff040 41 4c 57 41 59 53 3b 0a 20 20 7d 65 6c 73 65 7b  ALWAYS;.  }else{
ff050 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 73 20 61 20  .    /* Opens a 
ff060 66 69 6c 65 2c 20 6f 6e 6c 79 20 69 66 20 69 74  file, only if it
ff070 20 65 78 69 73 74 73 2e 20 2a 2f 0a 20 20 20 20   exists. */.    
ff080 64 77 43 72 65 61 74 69 6f 6e 44 69 73 70 6f 73  dwCreationDispos
ff090 69 74 69 6f 6e 20 3d 20 4f 50 45 4e 5f 45 58 49  ition = OPEN_EXI
ff0a0 53 54 49 4e 47 3b 0a 20 20 7d 0a 20 20 64 77 53  STING;.  }.  dwS
ff0b0 68 61 72 65 4d 6f 64 65 20 3d 20 46 49 4c 45 5f  hareMode = FILE_
ff0c0 53 48 41 52 45 5f 52 45 41 44 20 7c 20 46 49 4c  SHARE_READ | FIL
ff0d0 45 5f 53 48 41 52 45 5f 57 52 49 54 45 3b 0a 20  E_SHARE_WRITE;. 
ff0e0 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
ff0f0 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f  ITE_OPEN_DELETEO
ff100 4e 43 4c 4f 53 45 20 29 7b 0a 23 69 66 20 53 51  NCLOSE ){.#if SQ
ff110 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20  LITE_OS_WINCE.  
ff120 20 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72    dwFlagsAndAttr
ff130 69 62 75 74 65 73 20 3d 20 46 49 4c 45 5f 41 54  ibutes = FILE_AT
ff140 54 52 49 42 55 54 45 5f 48 49 44 44 45 4e 3b 0a  TRIBUTE_HIDDEN;.
ff150 20 20 20 20 69 73 54 65 6d 70 20 3d 20 31 3b 0a      isTemp = 1;.
ff160 23 65 6c 73 65 0a 20 20 20 20 64 77 46 6c 61 67  #else.    dwFlag
ff170 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 3d  sAndAttributes =
ff180 20 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f   FILE_ATTRIBUTE_
ff190 54 45 4d 50 4f 52 41 52 59 0a 20 20 20 20 20 20  TEMPORARY.      
ff1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff1b0 20 20 20 20 20 20 20 20 20 7c 20 46 49 4c 45 5f           | FILE_
ff1c0 41 54 54 52 49 42 55 54 45 5f 48 49 44 44 45 4e  ATTRIBUTE_HIDDEN
ff1d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
ff1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff1f0 7c 20 46 49 4c 45 5f 46 4c 41 47 5f 44 45 4c 45  | FILE_FLAG_DELE
ff200 54 45 5f 4f 4e 5f 43 4c 4f 53 45 3b 0a 23 65 6e  TE_ON_CLOSE;.#en
ff210 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  dif.  }else{.   
ff220 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69   dwFlagsAndAttri
ff230 62 75 74 65 73 20 3d 20 46 49 4c 45 5f 41 54 54  butes = FILE_ATT
ff240 52 49 42 55 54 45 5f 4e 4f 52 4d 41 4c 3b 0a 20  RIBUTE_NORMAL;. 
ff250 20 7d 0a 20 20 2f 2a 20 52 65 70 6f 72 74 73 20   }.  /* Reports 
ff260 66 72 6f 6d 20 74 68 65 20 69 6e 74 65 72 6e 65  from the interne
ff270 74 20 61 72 65 20 74 68 61 74 20 70 65 72 66 6f  t are that perfo
ff280 72 6d 61 6e 63 65 20 69 73 20 61 6c 77 61 79 73  rmance is always
ff290 0a 20 20 2a 2a 20 62 65 74 74 65 72 20 69 66 20  .  ** better if 
ff2a0 46 49 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 4f 4d  FILE_FLAG_RANDOM
ff2b0 5f 41 43 43 45 53 53 20 69 73 20 75 73 65 64 2e  _ACCESS is used.
ff2c0 20 20 54 69 63 6b 65 74 20 23 32 36 39 39 2e 20    Ticket #2699. 
ff2d0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  */.#if SQLITE_OS
ff2e0 5f 57 49 4e 43 45 0a 20 20 64 77 46 6c 61 67 73  _WINCE.  dwFlags
ff2f0 41 6e 64 41 74 74 72 69 62 75 74 65 73 20 7c 3d  AndAttributes |=
ff300 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41 4e 44 4f   FILE_FLAG_RANDO
ff310 4d 5f 41 43 43 45 53 53 3b 0a 23 65 6e 64 69 66  M_ACCESS;.#endif
ff320 0a 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b  .  if( isNT() ){
ff330 0a 20 20 20 20 68 20 3d 20 43 72 65 61 74 65 46  .    h = CreateF
ff340 69 6c 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f  ileW((WCHAR*)zCo
ff350 6e 76 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20  nverted,.       
ff360 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 2c  dwDesiredAccess,
ff370 0a 20 20 20 20 20 20 20 64 77 53 68 61 72 65 4d  .       dwShareM
ff380 6f 64 65 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c  ode,.       NULL
ff390 2c 0a 20 20 20 20 20 20 20 64 77 43 72 65 61 74  ,.       dwCreat
ff3a0 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 2c 0a  ionDisposition,.
ff3b0 20 20 20 20 20 20 20 64 77 46 6c 61 67 73 41 6e         dwFlagsAn
ff3c0 64 41 74 74 72 69 62 75 74 65 73 2c 0a 20 20 20  dAttributes,.   
ff3d0 20 20 20 20 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a      NULL.    );.
ff3e0 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69  /* isNT() is 1 i
ff3f0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
ff400 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c  E==1, so this el
ff410 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63  se is never exec
ff420 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20  uted. .** Since 
ff430 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f  the ASCII versio
ff440 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f  n of these Windo
ff450 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78  ws API do not ex
ff460 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a  ist for WINCE,.*
ff470 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74  * it's important
ff480 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63   to not referenc
ff490 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45  e them for WINCE
ff4a0 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20   builds..*/.#if 
ff4b0 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
ff4c0 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  =0.  }else{.    
ff4d0 68 20 3d 20 43 72 65 61 74 65 46 69 6c 65 41 28  h = CreateFileA(
ff4e0 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65  (char*)zConverte
ff4f0 64 2c 0a 20 20 20 20 20 20 20 64 77 44 65 73 69  d,.       dwDesi
ff500 72 65 64 41 63 63 65 73 73 2c 0a 20 20 20 20 20  redAccess,.     
ff510 20 20 64 77 53 68 61 72 65 4d 6f 64 65 2c 0a 20    dwShareMode,. 
ff520 20 20 20 20 20 20 4e 55 4c 4c 2c 0a 20 20 20 20        NULL,.    
ff530 20 20 20 64 77 43 72 65 61 74 69 6f 6e 44 69 73     dwCreationDis
ff540 70 6f 73 69 74 69 6f 6e 2c 0a 20 20 20 20 20 20  position,.      
ff550 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74 72 69   dwFlagsAndAttri
ff560 62 75 74 65 73 2c 0a 20 20 20 20 20 20 20 4e 55  butes,.       NU
ff570 4c 4c 0a 20 20 20 20 29 3b 0a 23 65 6e 64 69 66  LL.    );.#endif
ff580 0a 20 20 7d 0a 20 20 69 66 28 20 68 3d 3d 49 4e  .  }.  if( h==IN
ff590 56 41 4c 49 44 5f 48 41 4e 44 4c 45 5f 56 41 4c  VALID_HANDLE_VAL
ff5a0 55 45 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a  UE ){.    free(z
ff5b0 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20 20  Converted);.    
ff5c0 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49  if( flags & SQLI
ff5d0 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
ff5e0 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  E ){.      retur
ff5f0 6e 20 77 69 6e 4f 70 65 6e 28 70 56 66 73 2c 20  n winOpen(pVfs, 
ff600 7a 4e 61 6d 65 2c 20 69 64 2c 20 0a 20 20 20 20  zName, id, .    
ff610 20 20 20 20 20 20 20 20 20 28 28 66 6c 61 67 73           ((flags
ff620 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  |SQLITE_OPEN_REA
ff630 44 4f 4e 4c 59 29 26 7e 53 51 4c 49 54 45 5f 4f  DONLY)&~SQLITE_O
ff640 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 2c 20  PEN_READWRITE), 
ff650 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 20 20  pOutFlags);.    
ff660 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74  }else{.      ret
ff670 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  urn SQLITE_CANTO
ff680 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  PEN;.    }.  }. 
ff690 20 69 66 28 20 70 4f 75 74 46 6c 61 67 73 20 29   if( pOutFlags )
ff6a0 7b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20  {.    if( flags 
ff6b0 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  & SQLITE_OPEN_RE
ff6c0 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20  ADWRITE ){.     
ff6d0 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 53 51   *pOutFlags = SQ
ff6e0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
ff6f0 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ITE;.    }else{.
ff700 20 20 20 20 20 20 2a 70 4f 75 74 46 6c 61 67 73        *pOutFlags
ff710 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   = SQLITE_OPEN_R
ff720 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a 20  EADONLY;.    }. 
ff730 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 46 69 6c   }.  memset(pFil
ff740 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 46  e, 0, sizeof(*pF
ff750 69 6c 65 29 29 3b 0a 20 20 70 46 69 6c 65 2d 3e  ile));.  pFile->
ff760 70 4d 65 74 68 6f 64 20 3d 20 26 77 69 6e 49 6f  pMethod = &winIo
ff770 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69 6c 65 2d  Method;.  pFile-
ff780 3e 68 20 3d 20 68 3b 0a 20 20 70 46 69 6c 65 2d  >h = h;.  pFile-
ff790 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 4e 4f 5f  >lastErrno = NO_
ff7a0 45 52 52 4f 52 3b 0a 20 20 70 46 69 6c 65 2d 3e  ERROR;.  pFile->
ff7b0 73 65 63 74 6f 72 53 69 7a 65 20 3d 20 67 65 74  sectorSize = get
ff7c0 53 65 63 74 6f 72 53 69 7a 65 28 70 56 66 73 2c  SectorSize(pVfs,
ff7d0 20 7a 55 74 66 38 4e 61 6d 65 29 3b 0a 23 69 66   zUtf8Name);.#if
ff7e0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
ff7f0 0a 20 20 69 66 28 20 28 66 6c 61 67 73 20 26 20  .  if( (flags & 
ff800 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  (SQLITE_OPEN_REA
ff810 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50  DWRITE|SQLITE_OP
ff820 45 4e 5f 4d 41 49 4e 5f 44 42 29 29 20 3d 3d 0a  EN_MAIN_DB)) ==.
ff830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
ff840 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
ff850 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45  WRITE|SQLITE_OPE
ff860 4e 5f 4d 41 49 4e 5f 44 42 29 0a 20 20 20 20 20  N_MAIN_DB).     
ff870 20 20 26 26 20 21 77 69 6e 63 65 43 72 65 61 74    && !winceCreat
ff880 65 4c 6f 63 6b 28 7a 4e 61 6d 65 2c 20 70 46 69  eLock(zName, pFi
ff890 6c 65 29 0a 20 20 29 7b 0a 20 20 20 20 43 6c 6f  le).  ){.    Clo
ff8a0 73 65 48 61 6e 64 6c 65 28 68 29 3b 0a 20 20 20  seHandle(h);.   
ff8b0 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64   free(zConverted
ff8c0 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
ff8d0 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20  LITE_CANTOPEN;. 
ff8e0 20 7d 0a 20 20 69 66 28 20 69 73 54 65 6d 70 20   }.  if( isTemp 
ff8f0 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 7a 44  ){.    pFile->zD
ff900 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 20 3d 20 7a  eleteOnClose = z
ff910 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 7d 65 6c  Converted;.  }el
ff920 73 65 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20  se.#endif.  {.  
ff930 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65    free(zConverte
ff940 64 29 3b 0a 20 20 7d 0a 20 20 4f 70 65 6e 43 6f  d);.  }.  OpenCo
ff950 75 6e 74 65 72 28 2b 31 29 3b 0a 20 20 72 65 74  unter(+1);.  ret
ff960 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
ff970 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74  ../*.** Delete t
ff980 68 65 20 6e 61 6d 65 64 20 66 69 6c 65 2e 0a 2a  he named file..*
ff990 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 77  *.** Note that w
ff9a0 69 6e 64 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20  indows does not 
ff9b0 61 6c 6c 6f 77 20 61 20 66 69 6c 65 20 74 6f 20  allow a file to 
ff9c0 62 65 20 64 65 6c 65 74 65 64 20 69 66 20 73 6f  be deleted if so
ff9d0 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 72 6f 63  me other.** proc
ff9e0 65 73 73 20 68 61 73 20 69 74 20 6f 70 65 6e 2e  ess has it open.
ff9f0 20 20 53 6f 6d 65 74 69 6d 65 73 20 61 20 76 69    Sometimes a vi
ffa00 72 75 73 20 73 63 61 6e 6e 65 72 20 6f 72 20 69  rus scanner or i
ffa10 6e 64 65 78 69 6e 67 20 70 72 6f 67 72 61 6d 0a  ndexing program.
ffa20 2a 2a 20 77 69 6c 6c 20 6f 70 65 6e 20 61 20 6a  ** will open a j
ffa30 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 68 6f 72  ournal file shor
ffa40 74 6c 79 20 61 66 74 65 72 20 69 74 20 69 73 20  tly after it is 
ffa50 63 72 65 61 74 65 64 20 69 6e 20 6f 72 64 65 72  created in order
ffa60 20 74 6f 20 64 6f 0a 2a 2a 20 77 68 61 74 65 76   to do.** whatev
ffa70 65 72 20 69 74 20 64 6f 65 73 2e 20 20 57 68 69  er it does.  Whi
ffa80 6c 65 20 74 68 69 73 20 6f 74 68 65 72 20 70 72  le this other pr
ffa90 6f 63 65 73 73 20 69 73 20 68 6f 6c 64 69 6e 67  ocess is holding
ffaa0 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 6f 70 65   the.** file ope
ffab0 6e 2c 20 77 65 20 77 69 6c 6c 20 62 65 20 75 6e  n, we will be un
ffac0 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 69  able to delete i
ffad0 74 2e 20 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75  t.  To work arou
ffae0 6e 64 20 74 68 69 73 0a 2a 2a 20 70 72 6f 62 6c  nd this.** probl
ffaf0 65 6d 2c 20 77 65 20 64 65 6c 61 79 20 31 30 30  em, we delay 100
ffb00 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 61 6e   milliseconds an
ffb10 64 20 74 72 79 20 74 6f 20 64 65 6c 65 74 65 20  d try to delete 
ffb20 61 67 61 69 6e 2e 20 20 55 70 0a 2a 2a 20 74 6f  again.  Up.** to
ffb30 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54   MX_DELETION_ATT
ffb40 45 4d 50 54 73 20 64 65 6c 65 74 69 6f 6e 20 61  EMPTs deletion a
ffb50 74 74 65 6d 70 74 73 20 61 72 65 20 72 75 6e 20  ttempts are run 
ffb60 62 65 66 6f 72 65 20 67 69 76 69 6e 67 0a 2a 2a  before giving.**
ffb70 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 69 6e   up and returnin
ffb80 67 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23  g an error..*/.#
ffb90 64 65 66 69 6e 65 20 4d 58 5f 44 45 4c 45 54 49  define MX_DELETI
ffba0 4f 4e 5f 41 54 54 45 4d 50 54 53 20 35 0a 73 74  ON_ATTEMPTS 5.st
ffbb0 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65 6c 65  atic int winDele
ffbc0 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  te(.  sqlite3_vf
ffbd0 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20  s *pVfs,        
ffbe0 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e    /* Not used on
ffbf0 20 77 69 6e 33 32 20 2a 2f 0a 20 20 63 6f 6e 73   win32 */.  cons
ffc00 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
ffc10 65 2c 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  e,      /* Name 
ffc20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65 74  of file to delet
ffc30 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79 6e 63 44  e */.  int syncD
ffc40 69 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ir              
ffc50 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64 20 6f     /* Not used o
ffc60 6e 20 77 69 6e 33 32 20 2a 2f 0a 29 7b 0a 20 20  n win32 */.){.  
ffc70 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 44  int cnt = 0;.  D
ffc80 57 4f 52 44 20 72 63 3b 0a 20 20 44 57 4f 52 44  WORD rc;.  DWORD
ffc90 20 65 72 72 6f 72 20 3d 20 30 3b 0a 20 20 76 6f   error = 0;.  vo
ffca0 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d  id *zConverted =
ffcb0 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65   convertUtf8File
ffcc0 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b  name(zFilename);
ffcd0 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
ffce0 54 45 52 28 70 56 66 73 29 3b 0a 20 20 55 4e 55  TER(pVfs);.  UNU
ffcf0 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 73 79  SED_PARAMETER(sy
ffd00 6e 63 44 69 72 29 3b 0a 20 20 69 66 28 20 7a 43  ncDir);.  if( zC
ffd10 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20  onverted==0 ){. 
ffd20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
ffd30 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 53 69  _NOMEM;.  }.  Si
ffd40 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72 65  mulateIOError(re
ffd50 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
ffd60 52 5f 44 45 4c 45 54 45 29 3b 0a 20 20 69 66 28  R_DELETE);.  if(
ffd70 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 64   isNT() ){.    d
ffd80 6f 7b 0a 20 20 20 20 20 20 44 65 6c 65 74 65 46  o{.      DeleteF
ffd90 69 6c 65 57 28 7a 43 6f 6e 76 65 72 74 65 64 29  ileW(zConverted)
ffda0 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 20 20  ;.    }while(   
ffdb0 28 20 20 20 28 28 72 63 20 3d 20 47 65 74 46 69  (   ((rc = GetFi
ffdc0 6c 65 41 74 74 72 69 62 75 74 65 73 57 28 7a 43  leAttributesW(zC
ffdd0 6f 6e 76 65 72 74 65 64 29 29 20 21 3d 20 49 4e  onverted)) != IN
ffde0 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49  VALID_FILE_ATTRI
ffdf0 42 55 54 45 53 29 0a 20 20 20 20 20 20 20 20 20  BUTES).         
ffe00 20 20 20 20 20 20 7c 7c 20 28 28 65 72 72 6f 72        || ((error
ffe10 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28   = GetLastError(
ffe20 29 29 20 3d 3d 20 45 52 52 4f 52 5f 41 43 43 45  )) == ERROR_ACCE
ffe30 53 53 5f 44 45 4e 49 45 44 29 29 0a 20 20 20 20  SS_DENIED)).    
ffe40 20 20 20 20 20 20 20 26 26 20 28 2b 2b 63 6e 74         && (++cnt
ffe50 20 3c 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41   < MX_DELETION_A
ffe60 54 54 45 4d 50 54 53 29 0a 20 20 20 20 20 20 20  TTEMPTS).       
ffe70 20 20 20 20 26 26 20 28 53 6c 65 65 70 28 31 30      && (Sleep(10
ffe80 30 29 2c 20 31 29 20 29 3b 0a 2f 2a 20 69 73 4e  0), 1) );./* isN
ffe90 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49  T() is 1 if SQLI
ffea0 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20  TE_OS_WINCE==1, 
ffeb0 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20  so this else is 
ffec0 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20  never executed. 
ffed0 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53  .** Since the AS
ffee0 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  CII version of t
ffef0 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49  hese Windows API
fff00 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f   do not exist fo
fff10 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73  r WINCE,.** it's
fff20 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f   important to no
fff30 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d  t reference them
fff40 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64   for WINCE build
fff50 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  s..*/.#if SQLITE
fff60 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d  _OS_WINCE==0.  }
fff70 65 6c 73 65 7b 0a 20 20 20 20 64 6f 7b 0a 20 20  else{.    do{.  
fff80 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65 41 28      DeleteFileA(
fff90 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 20  zConverted);.   
fffa0 20 7d 77 68 69 6c 65 28 20 20 20 28 20 20 20 28   }while(   (   (
fffb0 28 72 63 20 3d 20 47 65 74 46 69 6c 65 41 74 74  (rc = GetFileAtt
fffc0 72 69 62 75 74 65 73 41 28 7a 43 6f 6e 76 65 72  ributesA(zConver
fffd0 74 65 64 29 29 20 21 3d 20 49 4e 56 41 4c 49 44  ted)) != INVALID
fffe0 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45 53  _FILE_ATTRIBUTES
ffff0 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ).              
10000 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d 20 47 65   || ((error = Ge
10001 74 4c 61 73 74 45 72 72 6f 72 28 29 29 20 3d 3d  tLastError()) ==
10002 20 45 52 52 4f 52 5f 41 43 43 45 53 53 5f 44 45   ERROR_ACCESS_DE
10003 4e 49 45 44 29 29 0a 20 20 20 20 20 20 20 20 20  NIED)).         
10004 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c 20 4d 58    && (++cnt < MX
10005 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50  _DELETION_ATTEMP
10006 54 53 29 0a 20 20 20 20 20 20 20 20 20 20 20 26  TS).           &
10007 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c 20 31  & (Sleep(100), 1
10008 29 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  ) );.#endif.  }.
10009 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74 65    free(zConverte
1000a 64 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28 22  d);.  OSTRACE2("
1000b 44 45 4c 45 54 45 20 5c 22 25 73 5c 22 5c 6e 22  DELETE \"%s\"\n"
1000c 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20  , zFilename);.  
1000d 72 65 74 75 72 6e 20 28 20 20 20 28 72 63 20 3d  return (   (rc =
1000e 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41  = INVALID_FILE_A
1000f 54 54 52 49 42 55 54 45 53 29 20 0a 20 20 20 20  TTRIBUTES) .    
10010 20 20 20 20 20 20 26 26 20 28 65 72 72 6f 72 20        && (error 
10011 3d 3d 20 45 52 52 4f 52 5f 46 49 4c 45 5f 4e 4f  == ERROR_FILE_NO
10012 54 5f 46 4f 55 4e 44 29 29 20 3f 20 53 51 4c 49  T_FOUND)) ? SQLI
10013 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49  TE_OK : SQLITE_I
10014 4f 45 52 52 5f 44 45 4c 45 54 45 3b 0a 7d 0a 0a  OERR_DELETE;.}..
10015 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20  /*.** Check the 
10016 65 78 69 73 74 61 6e 63 65 20 61 6e 64 20 73 74  existance and st
10017 61 74 75 73 20 6f 66 20 61 20 66 69 6c 65 2e 0a  atus of a file..
10018 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
10019 6e 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74  nAccess(.  sqlit
1001a 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20  e3_vfs *pVfs,   
1001b 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65        /* Not use
1001c 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20 20  d on win32 */.  
1001d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
1001e 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61  ename,     /* Na
1001f 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 63 68  me of file to ch
10020 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61  eck */.  int fla
10021 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gs,             
10022 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 74      /* Type of t
10023 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e 20 74  est to make on t
10024 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e  his file */.  in
10025 74 20 2a 70 52 65 73 4f 75 74 20 20 20 20 20 20  t *pResOut      
10026 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
10027 20 52 65 73 75 6c 74 20 2a 2f 0a 29 7b 0a 20 20   Result */.){.  
10028 44 57 4f 52 44 20 61 74 74 72 3b 0a 20 20 69 6e  DWORD attr;.  in
10029 74 20 72 63 20 3d 20 30 3b 0a 20 20 76 6f 69 64  t rc = 0;.  void
1002a 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63   *zConverted = c
1002b 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61  onvertUtf8Filena
1002c 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  me(zFilename);. 
1002d 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
1002e 52 28 70 56 66 73 29 3b 0a 20 20 69 66 28 20 7a  R(pVfs);.  if( z
1002f 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a  Converted==0 ){.
10030 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
10031 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69  E_NOMEM;.  }.  i
10032 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20  f( isNT() ){.   
10033 20 61 74 74 72 20 3d 20 47 65 74 46 69 6c 65 41   attr = GetFileA
10034 74 74 72 69 62 75 74 65 73 57 28 28 57 43 48 41  ttributesW((WCHA
10035 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  R*)zConverted);.
10036 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69  /* isNT() is 1 i
10037 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
10038 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c  E==1, so this el
10039 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63  se is never exec
1003a 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20  uted. .** Since 
1003b 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f  the ASCII versio
1003c 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f  n of these Windo
1003d 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78  ws API do not ex
1003e 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a  ist for WINCE,.*
1003f 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74  * it's important
10040 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63   to not referenc
10041 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45  e them for WINCE
10042 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20   builds..*/.#if 
10043 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
10044 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  =0.  }else{.    
10045 61 74 74 72 20 3d 20 47 65 74 46 69 6c 65 41 74  attr = GetFileAt
10046 74 72 69 62 75 74 65 73 41 28 28 63 68 61 72 2a  tributesA((char*
10047 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23 65  )zConverted);.#e
10048 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 65 65 28  ndif.  }.  free(
10049 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20 73  zConverted);.  s
1004a 77 69 74 63 68 28 20 66 6c 61 67 73 20 29 7b 0a  witch( flags ){.
1004b 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
1004c 41 43 43 45 53 53 5f 52 45 41 44 3a 0a 20 20 20  ACCESS_READ:.   
1004d 20 63 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43   case SQLITE_ACC
1004e 45 53 53 5f 45 58 49 53 54 53 3a 0a 20 20 20 20  ESS_EXISTS:.    
1004f 20 20 72 63 20 3d 20 61 74 74 72 21 3d 49 4e 56    rc = attr!=INV
10050 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42  ALID_FILE_ATTRIB
10051 55 54 45 53 3b 0a 20 20 20 20 20 20 62 72 65 61  UTES;.      brea
10052 6b 3b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  k;.    case SQLI
10053 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52  TE_ACCESS_READWR
10054 49 54 45 3a 0a 20 20 20 20 20 20 72 63 20 3d 20  ITE:.      rc = 
10055 28 61 74 74 72 20 26 20 46 49 4c 45 5f 41 54 54  (attr & FILE_ATT
10056 52 49 42 55 54 45 5f 52 45 41 44 4f 4e 4c 59 29  RIBUTE_READONLY)
10057 3d 3d 30 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ==0;.      break
10058 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20  ;.    default:. 
10059 20 20 20 20 20 61 73 73 65 72 74 28 21 22 49 6e       assert(!"In
1005a 76 61 6c 69 64 20 66 6c 61 67 73 20 61 72 67 75  valid flags argu
1005b 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a 20 20 2a 70  ment");.  }.  *p
1005c 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a 20 20 72  ResOut = rc;.  r
1005d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1005e 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20  .}.../*.** Turn 
1005f 61 20 72 65 6c 61 74 69 76 65 20 70 61 74 68 6e  a relative pathn
10060 61 6d 65 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20  ame into a full 
10061 70 61 74 68 6e 61 6d 65 2e 20 20 57 72 69 74 65  pathname.  Write
10062 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20 70 61 74   the full.** pat
10063 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 4f 75 74 5b  hname into zOut[
10064 5d 2e 20 20 7a 4f 75 74 5b 5d 20 77 69 6c 6c 20  ].  zOut[] will 
10065 62 65 20 61 74 20 6c 65 61 73 74 20 70 56 66 73  be at least pVfs
10066 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a 2a 2a 20  ->mxPathname.** 
10067 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a  bytes in size..*
10068 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69 6e  /.static int win
10069 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 0a 20 20  FullPathname(.  
1006a 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
1006b 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  s,            /*
1006c 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73 20   Pointer to vfs 
1006d 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63 6f 6e 73  object */.  cons
1006e 74 20 63 68 61 72 20 2a 7a 52 65 6c 61 74 69 76  t char *zRelativ
1006f 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 73  e,        /* Pos
10070 73 69 62 6c 79 20 72 65 6c 61 74 69 76 65 20 69  sibly relative i
10071 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a 20 20 69  nput path */.  i
10072 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20 20 20 20  nt nFull,       
10073 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10074 53 69 7a 65 20 6f 66 20 6f 75 74 70 75 74 20 62  Size of output b
10075 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20 2a  uffer in bytes *
10076 2f 0a 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 20  /.  char *zFull 
10077 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10078 20 20 2f 2a 20 4f 75 74 70 75 74 20 62 75 66 66    /* Output buff
10079 65 72 20 2a 2f 0a 29 7b 0a 20 20 0a 23 69 66 20  er */.){.  .#if 
1007a 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e  defined(__CYGWIN
1007b 5f 5f 29 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  __).  UNUSED_PAR
1007c 41 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20  AMETER(nFull);. 
1007d 20 63 79 67 77 69 6e 5f 63 6f 6e 76 5f 74 6f 5f   cygwin_conv_to_
1007e 66 75 6c 6c 5f 77 69 6e 33 32 5f 70 61 74 68 28  full_win32_path(
1007f 7a 52 65 6c 61 74 69 76 65 2c 20 7a 46 75 6c 6c  zRelative, zFull
10080 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
10081 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23  TE_OK;.#endif..#
10082 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
10083 43 45 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  CE.  UNUSED_PARA
10084 4d 45 54 45 52 28 6e 46 75 6c 6c 29 3b 0a 20 20  METER(nFull);.  
10085 2f 2a 20 57 69 6e 43 45 20 68 61 73 20 6e 6f 20  /* WinCE has no 
10086 63 6f 6e 63 65 70 74 20 6f 66 20 61 20 72 65 6c  concept of a rel
10087 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65 2c 20  ative pathname, 
10088 6f 72 20 73 6f 20 49 20 61 6d 20 74 6f 6c 64 2e  or so I am told.
10089 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e   */.  sqlite3_sn
1008a 70 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50  printf(pVfs->mxP
1008b 61 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20  athname, zFull, 
1008c 22 25 73 22 2c 20 7a 52 65 6c 61 74 69 76 65 29  "%s", zRelative)
1008d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1008e 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 0a 23 69  E_OK;.#endif..#i
1008f 66 20 21 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  f !SQLITE_OS_WIN
10090 43 45 20 26 26 20 21 64 65 66 69 6e 65 64 28 5f  CE && !defined(_
10091 5f 43 59 47 57 49 4e 5f 5f 29 0a 20 20 69 6e 74  _CYGWIN__).  int
10092 20 6e 42 79 74 65 3b 0a 20 20 76 6f 69 64 20 2a   nByte;.  void *
10093 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 63 68  zConverted;.  ch
10094 61 72 20 2a 7a 4f 75 74 3b 0a 20 20 55 4e 55 53  ar *zOut;.  UNUS
10095 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e 46 75  ED_PARAMETER(nFu
10096 6c 6c 29 3b 0a 20 20 7a 43 6f 6e 76 65 72 74 65  ll);.  zConverte
10097 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46  d = convertUtf8F
10098 69 6c 65 6e 61 6d 65 28 7a 52 65 6c 61 74 69 76  ilename(zRelativ
10099 65 29 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29  e);.  if( isNT()
1009a 20 29 7b 0a 20 20 20 20 57 43 48 41 52 20 2a 7a   ){.    WCHAR *z
1009b 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65 20  Temp;.    nByte 
1009c 3d 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d  = GetFullPathNam
1009d 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76  eW((WCHAR*)zConv
1009e 65 72 74 65 64 2c 20 30 2c 20 30 2c 20 30 29 20  erted, 0, 0, 0) 
1009f 2b 20 33 3b 0a 20 20 20 20 7a 54 65 6d 70 20 3d  + 3;.    zTemp =
100a0 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73   malloc( nByte*s
100a1 69 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20  izeof(zTemp[0]) 
100a2 29 3b 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70  );.    if( zTemp
100a3 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 72 65  ==0 ){.      fre
100a4 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  e(zConverted);. 
100a5 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
100a6 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
100a7 20 20 20 20 47 65 74 46 75 6c 6c 50 61 74 68 4e      GetFullPathN
100a8 61 6d 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f  ameW((WCHAR*)zCo
100a9 6e 76 65 72 74 65 64 2c 20 6e 42 79 74 65 2c 20  nverted, nByte, 
100aa 7a 54 65 6d 70 2c 20 30 29 3b 0a 20 20 20 20 66  zTemp, 0);.    f
100ab 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ree(zConverted);
100ac 0a 20 20 20 20 7a 4f 75 74 20 3d 20 75 6e 69 63  .    zOut = unic
100ad 6f 64 65 54 6f 55 74 66 38 28 7a 54 65 6d 70 29  odeToUtf8(zTemp)
100ae 3b 0a 20 20 20 20 66 72 65 65 28 7a 54 65 6d 70  ;.    free(zTemp
100af 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20  );./* isNT() is 
100b0 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  1 if SQLITE_OS_W
100b1 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73  INCE==1, so this
100b2 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65   else is never e
100b3 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e  xecuted. .** Sin
100b4 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72  ce the ASCII ver
100b5 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69  sion of these Wi
100b6 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74  ndows API do not
100b7 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45   exist for WINCE
100b8 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74  ,.** it's import
100b9 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72  ant to not refer
100ba 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49  ence them for WI
100bb 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23  NCE builds..*/.#
100bc 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
100bd 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20  CE==0.  }else{. 
100be 20 20 20 63 68 61 72 20 2a 7a 54 65 6d 70 3b 0a     char *zTemp;.
100bf 20 20 20 20 6e 42 79 74 65 20 3d 20 47 65 74 46      nByte = GetF
100c0 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28 63 68  ullPathNameA((ch
100c1 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20  ar*)zConverted, 
100c2 30 2c 20 30 2c 20 30 29 20 2b 20 33 3b 0a 20 20  0, 0, 0) + 3;.  
100c3 20 20 7a 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f 63    zTemp = malloc
100c4 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a  ( nByte*sizeof(z
100c5 54 65 6d 70 5b 30 5d 29 20 29 3b 0a 20 20 20 20  Temp[0]) );.    
100c6 69 66 28 20 7a 54 65 6d 70 3d 3d 30 20 29 7b 0a  if( zTemp==0 ){.
100c7 20 20 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76        free(zConv
100c8 65 72 74 65 64 29 3b 0a 20 20 20 20 20 20 72 65  erted);.      re
100c9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
100ca 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 47 65 74  M;.    }.    Get
100cb 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28 63  FullPathNameA((c
100cc 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c  har*)zConverted,
100cd 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30   nByte, zTemp, 0
100ce 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e  );.    free(zCon
100cf 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7a 4f 75  verted);.    zOu
100d0 74 20 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e 33  t = sqlite3_win3
100d1 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a  2_mbcs_to_utf8(z
100d2 54 65 6d 70 29 3b 0a 20 20 20 20 66 72 65 65 28  Temp);.    free(
100d3 7a 54 65 6d 70 29 3b 0a 23 65 6e 64 69 66 0a 20  zTemp);.#endif. 
100d4 20 7d 0a 20 20 69 66 28 20 7a 4f 75 74 20 29 7b   }.  if( zOut ){
100d5 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
100d6 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50 61  rintf(pVfs->mxPa
100d7 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22  thname, zFull, "
100d8 25 73 22 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20  %s", zOut);.    
100d9 66 72 65 65 28 7a 4f 75 74 29 3b 0a 20 20 20 20  free(zOut);.    
100da 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
100db 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
100dc 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
100dd 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d  EM;.  }.#endif.}
100de 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20  ../*.** Get the 
100df 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66 20 74  sector size of t
100e0 68 65 20 64 65 76 69 63 65 20 75 73 65 64 20 74  he device used t
100e1 6f 20 73 74 6f 72 65 0a 2a 2a 20 66 69 6c 65 2e  o store.** file.
100e2 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
100e3 65 74 53 65 63 74 6f 72 53 69 7a 65 28 0a 20 20  etSectorSize(.  
100e4 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
100e5 56 66 73 2c 0a 20 20 20 20 63 6f 6e 73 74 20 63  Vfs,.    const c
100e6 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 20 20  har *zRelative  
100e7 20 20 20 2f 2a 20 55 54 46 2d 38 20 66 69 6c 65     /* UTF-8 file
100e8 20 6e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20 44 57   name */.){.  DW
100e9 4f 52 44 20 62 79 74 65 73 50 65 72 53 65 63 74  ORD bytesPerSect
100ea 6f 72 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41  or = SQLITE_DEFA
100eb 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b  ULT_SECTOR_SIZE;
100ec 0a 20 20 2f 2a 20 47 65 74 44 69 73 6b 46 72 65  .  /* GetDiskFre
100ed 65 53 70 61 63 65 20 69 73 20 6e 6f 74 20 73 75  eSpace is not su
100ee 70 70 6f 72 74 65 64 20 75 6e 64 65 72 20 57 49  pported under WI
100ef 4e 43 45 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54  NCE */.#if SQLIT
100f0 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 55 4e 55  E_OS_WINCE.  UNU
100f1 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 70 56  SED_PARAMETER(pV
100f2 66 73 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  fs);.  UNUSED_PA
100f3 52 41 4d 45 54 45 52 28 7a 52 65 6c 61 74 69 76  RAMETER(zRelativ
100f4 65 29 3b 0a 23 65 6c 73 65 0a 20 20 63 68 61 72  e);.#else.  char
100f5 20 7a 46 75 6c 6c 70 61 74 68 5b 4d 41 58 5f 50   zFullpath[MAX_P
100f6 41 54 48 2b 31 5d 3b 0a 20 20 69 6e 74 20 72 63  ATH+1];.  int rc
100f7 3b 0a 20 20 44 57 4f 52 44 20 64 77 52 65 74 20  ;.  DWORD dwRet 
100f8 3d 20 30 3b 0a 20 20 44 57 4f 52 44 20 64 77 44  = 0;.  DWORD dwD
100f9 75 6d 6d 79 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  ummy;..  /*.  **
100fa 20 57 65 20 6e 65 65 64 20 74 6f 20 67 65 74 20   We need to get 
100fb 74 68 65 20 66 75 6c 6c 20 70 61 74 68 20 6e 61  the full path na
100fc 6d 65 20 6f 66 20 74 68 65 20 66 69 6c 65 0a 20  me of the file. 
100fd 20 2a 2a 20 74 6f 20 67 65 74 20 74 68 65 20 64   ** to get the d
100fe 72 69 76 65 20 6c 65 74 74 65 72 20 74 6f 20 6c  rive letter to l
100ff 6f 6f 6b 20 75 70 20 74 68 65 20 73 65 63 74 6f  ook up the secto
10100 72 0a 20 20 2a 2a 20 73 69 7a 65 2e 0a 20 20 2a  r.  ** size..  *
10101 2f 0a 20 20 72 63 20 3d 20 77 69 6e 46 75 6c 6c  /.  rc = winFull
10102 50 61 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a  Pathname(pVfs, z
10103 52 65 6c 61 74 69 76 65 2c 20 4d 41 58 5f 50 41  Relative, MAX_PA
10104 54 48 2c 20 7a 46 75 6c 6c 70 61 74 68 29 3b 0a  TH, zFullpath);.
10105 20 20 69 66 28 20 72 63 20 3d 3d 20 53 51 4c 49    if( rc == SQLI
10106 54 45 5f 4f 4b 20 29 0a 20 20 7b 0a 20 20 20 20  TE_OK ).  {.    
10107 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64  void *zConverted
10108 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69   = convertUtf8Fi
10109 6c 65 6e 61 6d 65 28 7a 46 75 6c 6c 70 61 74 68  lename(zFullpath
1010a 29 3b 0a 20 20 20 20 69 66 28 20 7a 43 6f 6e 76  );.    if( zConv
1010b 65 72 74 65 64 20 29 7b 0a 20 20 20 20 20 20 69  erted ){.      i
1010c 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20  f( isNT() ){.   
1010d 20 20 20 20 20 2f 2a 20 74 72 69 6d 20 70 61 74       /* trim pat
1010e 68 20 74 6f 20 6a 75 73 74 20 64 72 69 76 65 20  h to just drive 
1010f 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 20  reference */.   
10110 20 20 20 20 20 57 43 48 41 52 20 2a 70 20 3d 20       WCHAR *p = 
10111 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20 20 20 20  zConverted;.    
10112 20 20 20 20 66 6f 72 28 3b 2a 70 3b 70 2b 2b 29      for(;*p;p++)
10113 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
10114 2a 70 20 3d 3d 20 27 5c 5c 27 20 29 7b 0a 20 20  *p == '\\' ){.  
10115 20 20 20 20 20 20 20 20 20 20 2a 70 20 3d 20 27            *p = '
10116 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20  \0';.           
10117 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
10118 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
10119 20 20 20 20 20 20 64 77 52 65 74 20 3d 20 47 65        dwRet = Ge
1011a 74 44 69 73 6b 46 72 65 65 53 70 61 63 65 57 28  tDiskFreeSpaceW(
1011b 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74  (WCHAR*)zConvert
1011c 65 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ed,.            
1011d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1011e 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a        &dwDummy,.
1011f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10121 20 20 26 62 79 74 65 73 50 65 72 53 65 63 74 6f    &bytesPerSecto
10122 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
10123 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10124 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c 0a 20       &dwDummy,. 
10125 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10126 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10127 20 26 64 77 44 75 6d 6d 79 29 3b 0a 20 20 20 20   &dwDummy);.    
10128 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
10129 20 2f 2a 20 74 72 69 6d 20 70 61 74 68 20 74 6f   /* trim path to
1012a 20 6a 75 73 74 20 64 72 69 76 65 20 72 65 66 65   just drive refe
1012b 72 65 6e 63 65 20 2a 2f 0a 20 20 20 20 20 20 20  rence */.       
1012c 20 63 68 61 72 20 2a 70 20 3d 20 28 63 68 61 72   char *p = (char
1012d 20 2a 29 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 20   *)zConverted;. 
1012e 20 20 20 20 20 20 20 66 6f 72 28 3b 2a 70 3b 70         for(;*p;p
1012f 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
10130 66 28 20 2a 70 20 3d 3d 20 27 5c 5c 27 20 29 7b  f( *p == '\\' ){
10131 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 20  .            *p 
10132 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20  = '\0';.        
10133 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
10134 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
10135 0a 20 20 20 20 20 20 20 20 64 77 52 65 74 20 3d  .        dwRet =
10136 20 47 65 74 44 69 73 6b 46 72 65 65 53 70 61 63   GetDiskFreeSpac
10137 65 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76 65  eA((char*)zConve
10138 72 74 65 64 2c 0a 20 20 20 20 20 20 20 20 20 20  rted,.          
10139 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1013a 20 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79          &dwDummy
1013b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1013c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1013d 20 20 20 20 26 62 79 74 65 73 50 65 72 53 65 63      &bytesPerSec
1013e 74 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  tor,.           
1013f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10140 20 20 20 20 20 20 20 26 64 77 44 75 6d 6d 79 2c         &dwDummy,
10141 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
10142 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10143 20 20 20 26 64 77 44 75 6d 6d 79 29 3b 0a 20 20     &dwDummy);.  
10144 20 20 20 20 7d 0a 20 20 20 20 20 20 66 72 65 65      }.      free
10145 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20  (zConverted);.  
10146 20 20 7d 0a 20 20 20 20 69 66 28 20 21 64 77 52    }.    if( !dwR
10147 65 74 20 29 7b 0a 20 20 20 20 20 20 62 79 74 65  et ){.      byte
10148 73 50 65 72 53 65 63 74 6f 72 20 3d 20 53 51 4c  sPerSector = SQL
10149 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54  ITE_DEFAULT_SECT
1014a 4f 52 5f 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20  OR_SIZE;.    }. 
1014b 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75   }.#endif.  retu
1014c 72 6e 20 28 69 6e 74 29 20 62 79 74 65 73 50 65  rn (int) bytesPe
1014d 72 53 65 63 74 6f 72 3b 20 0a 7d 0a 0a 23 69 66  rSector; .}..#if
1014e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1014f 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a  _LOAD_EXTENSION.
10150 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73  /*.** Interfaces
10151 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73   for opening a s
10152 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66  hared library, f
10153 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69  inding entry poi
10154 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68  nts.** within th
10155 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  e shared library
10156 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68  , and closing th
10157 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79  e shared library
10158 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72  ..*/./*.** Inter
10159 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e  faces for openin
1015a 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 61  g a shared libra
1015b 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72  ry, finding entr
1015c 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68  y points.** with
1015d 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c 69  in the shared li
1015e 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69  brary, and closi
1015f 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c 69  ng the shared li
10160 62 72 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  brary..*/.static
10161 20 76 6f 69 64 20 2a 77 69 6e 44 6c 4f 70 65 6e   void *winDlOpen
10162 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
10163 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  fs, const char *
10164 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 48 41  zFilename){.  HA
10165 4e 44 4c 45 20 68 3b 0a 20 20 76 6f 69 64 20 2a  NDLE h;.  void *
10166 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e  zConverted = con
10167 76 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65  vertUtf8Filename
10168 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 55  (zFilename);.  U
10169 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
1016a 70 56 66 73 29 3b 0a 20 20 69 66 28 20 7a 43 6f  pVfs);.  if( zCo
1016b 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a 20 20  nverted==0 ){.  
1016c 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
1016d 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a    if( isNT() ){.
1016e 20 20 20 20 68 20 3d 20 4c 6f 61 64 4c 69 62 72      h = LoadLibr
1016f 61 72 79 57 28 28 57 43 48 41 52 2a 29 7a 43 6f  aryW((WCHAR*)zCo
10170 6e 76 65 72 74 65 64 29 3b 0a 2f 2a 20 69 73 4e  nverted);./* isN
10171 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c 49  T() is 1 if SQLI
10172 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20  TE_OS_WINCE==1, 
10173 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73 20  so this else is 
10174 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e 20  never executed. 
10175 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41 53  .** Since the AS
10176 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  CII version of t
10177 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50 49  hese Windows API
10178 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66 6f   do not exist fo
10179 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27 73  r WINCE,.** it's
1017a 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e 6f   important to no
1017b 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 6d  t reference them
1017c 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c 64   for WINCE build
1017d 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  s..*/.#if SQLITE
1017e 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20 7d  _OS_WINCE==0.  }
1017f 65 6c 73 65 7b 0a 20 20 20 20 68 20 3d 20 4c 6f  else{.    h = Lo
10180 61 64 4c 69 62 72 61 72 79 41 28 28 63 68 61 72  adLibraryA((char
10181 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 23  *)zConverted);.#
10182 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72 65 65  endif.  }.  free
10183 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20  (zConverted);.  
10184 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 68 3b  return (void*)h;
10185 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  .}.static void w
10186 69 6e 44 6c 45 72 72 6f 72 28 73 71 6c 69 74 65  inDlError(sqlite
10187 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74  3_vfs *pVfs, int
10188 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75   nBuf, char *zBu
10189 66 4f 75 74 29 7b 0a 20 20 55 4e 55 53 45 44 5f  fOut){.  UNUSED_
1018a 50 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b  PARAMETER(pVfs);
1018b 0a 20 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d  .  getLastErrorM
1018c 73 67 28 6e 42 75 66 2c 20 7a 42 75 66 4f 75 74  sg(nBuf, zBufOut
1018d 29 3b 0a 7d 0a 76 6f 69 64 20 28 2a 77 69 6e 44  );.}.void (*winD
1018e 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73  lSym(sqlite3_vfs
1018f 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48   *pVfs, void *pH
10190 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61  andle, const cha
10191 72 20 2a 7a 53 79 6d 62 6f 6c 29 29 28 76 6f 69  r *zSymbol))(voi
10192 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  d){.  UNUSED_PAR
10193 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 23 69  AMETER(pVfs);.#i
10194 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
10195 45 0a 20 20 2f 2a 20 54 68 65 20 47 65 74 50 72  E.  /* The GetPr
10196 6f 63 41 64 64 72 65 73 73 41 28 29 20 72 6f 75  ocAddressA() rou
10197 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 61 76 61  tine is only ava
10198 69 6c 61 62 6c 65 20 6f 6e 20 77 69 6e 63 65 2e  ilable on wince.
10199 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 76 6f   */.  return (vo
1019a 69 64 28 2a 29 28 76 6f 69 64 29 29 47 65 74 50  id(*)(void))GetP
1019b 72 6f 63 41 64 64 72 65 73 73 41 28 28 48 41 4e  rocAddressA((HAN
1019c 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53 79  DLE)pHandle, zSy
1019d 6d 62 6f 6c 29 3b 0a 23 65 6c 73 65 0a 20 20 2f  mbol);.#else.  /
1019e 2a 20 41 6c 6c 20 6f 74 68 65 72 20 77 69 6e 64  * All other wind
1019f 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73 20 65 78  ows platforms ex
101a0 70 65 63 74 20 47 65 74 50 72 6f 63 41 64 64 72  pect GetProcAddr
101a1 65 73 73 28 29 20 74 6f 20 74 61 6b 65 0a 20 20  ess() to take.  
101a2 2a 2a 20 61 6e 20 41 6e 73 69 20 73 74 72 69 6e  ** an Ansi strin
101a3 67 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20  g regardless of 
101a4 74 68 65 20 5f 55 4e 49 43 4f 44 45 20 73 65 74  the _UNICODE set
101a5 74 69 6e 67 20 2a 2f 0a 20 20 72 65 74 75 72 6e  ting */.  return
101a6 20 28 76 6f 69 64 28 2a 29 28 76 6f 69 64 29 29   (void(*)(void))
101a7 47 65 74 50 72 6f 63 41 64 64 72 65 73 73 28 28  GetProcAddress((
101a8 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20  HANDLE)pHandle, 
101a9 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6e 64 69 66  zSymbol);.#endif
101aa 0a 7d 0a 76 6f 69 64 20 77 69 6e 44 6c 43 6c 6f  .}.void winDlClo
101ab 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  se(sqlite3_vfs *
101ac 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e  pVfs, void *pHan
101ad 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  dle){.  UNUSED_P
101ae 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a  ARAMETER(pVfs);.
101af 20 20 46 72 65 65 4c 69 62 72 61 72 79 28 28 48    FreeLibrary((H
101b0 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 29 3b 0a  ANDLE)pHandle);.
101b1 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 53 51  }.#else /* if SQ
101b2 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45  LITE_OMIT_LOAD_E
101b3 58 54 45 4e 53 49 4f 4e 20 69 73 20 64 65 66 69  XTENSION is defi
101b4 6e 65 64 3a 20 2a 2f 0a 20 20 23 64 65 66 69 6e  ned: */.  #defin
101b5 65 20 77 69 6e 44 6c 4f 70 65 6e 20 20 30 0a 20  e winDlOpen  0. 
101b6 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 45 72   #define winDlEr
101b7 72 6f 72 20 30 0a 20 20 23 64 65 66 69 6e 65 20  ror 0.  #define 
101b8 77 69 6e 44 6c 53 79 6d 20 20 20 30 0a 20 20 23  winDlSym   0.  #
101b9 64 65 66 69 6e 65 20 77 69 6e 44 6c 43 6c 6f 73  define winDlClos
101ba 65 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a  e 0.#endif.../*.
101bb 2a 2a 20 57 72 69 74 65 20 75 70 20 74 6f 20 6e  ** Write up to n
101bc 42 75 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e  Buf bytes of ran
101bd 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 7a 42 75  domness into zBu
101be 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  f..*/.static int
101bf 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 28 73   winRandomness(s
101c0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
101c1 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72  , int nBuf, char
101c2 20 2a 7a 42 75 66 29 7b 0a 20 20 69 6e 74 20 6e   *zBuf){.  int n
101c3 20 3d 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50   = 0;.  UNUSED_P
101c4 41 52 41 4d 45 54 45 52 28 70 56 66 73 29 3b 0a  ARAMETER(pVfs);.
101c5 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
101c6 54 45 5f 54 45 53 54 29 0a 20 20 6e 20 3d 20 6e  TE_TEST).  n = n
101c7 42 75 66 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 42  Buf;.  memset(zB
101c8 75 66 2c 20 30 2c 20 6e 42 75 66 29 3b 0a 23 65  uf, 0, nBuf);.#e
101c9 6c 73 65 0a 20 20 69 66 28 20 73 69 7a 65 6f 66  lse.  if( sizeof
101ca 28 53 59 53 54 45 4d 54 49 4d 45 29 3c 3d 6e 42  (SYSTEMTIME)<=nB
101cb 75 66 2d 6e 20 29 7b 0a 20 20 20 20 53 59 53 54  uf-n ){.    SYST
101cc 45 4d 54 49 4d 45 20 78 3b 0a 20 20 20 20 47 65  EMTIME x;.    Ge
101cd 74 53 79 73 74 65 6d 54 69 6d 65 28 26 78 29 3b  tSystemTime(&x);
101ce 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75  .    memcpy(&zBu
101cf 66 5b 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66  f[n], &x, sizeof
101d0 28 78 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73  (x));.    n += s
101d1 69 7a 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 20 20  izeof(x);.  }.  
101d2 69 66 28 20 73 69 7a 65 6f 66 28 44 57 4f 52 44  if( sizeof(DWORD
101d3 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20  )<=nBuf-n ){.   
101d4 20 44 57 4f 52 44 20 70 69 64 20 3d 20 47 65 74   DWORD pid = Get
101d5 43 75 72 72 65 6e 74 50 72 6f 63 65 73 73 49 64  CurrentProcessId
101d6 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  ();.    memcpy(&
101d7 7a 42 75 66 5b 6e 5d 2c 20 26 70 69 64 2c 20 73  zBuf[n], &pid, s
101d8 69 7a 65 6f 66 28 70 69 64 29 29 3b 0a 20 20 20  izeof(pid));.   
101d9 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 70 69 64   n += sizeof(pid
101da 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a  );.  }.  if( siz
101db 65 6f 66 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66  eof(DWORD)<=nBuf
101dc 2d 6e 20 29 7b 0a 20 20 20 20 44 57 4f 52 44 20  -n ){.    DWORD 
101dd 63 6e 74 20 3d 20 47 65 74 54 69 63 6b 43 6f 75  cnt = GetTickCou
101de 6e 74 28 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  nt();.    memcpy
101df 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 63 6e 74 2c  (&zBuf[n], &cnt,
101e0 20 73 69 7a 65 6f 66 28 63 6e 74 29 29 3b 0a 20   sizeof(cnt));. 
101e1 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 63     n += sizeof(c
101e2 6e 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  nt);.  }.  if( s
101e3 69 7a 65 6f 66 28 4c 41 52 47 45 5f 49 4e 54 45  izeof(LARGE_INTE
101e4 47 45 52 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a  GER)<=nBuf-n ){.
101e5 20 20 20 20 4c 41 52 47 45 5f 49 4e 54 45 47 45      LARGE_INTEGE
101e6 52 20 69 3b 0a 20 20 20 20 51 75 65 72 79 50 65  R i;.    QueryPe
101e7 72 66 6f 72 6d 61 6e 63 65 43 6f 75 6e 74 65 72  rformanceCounter
101e8 28 26 69 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  (&i);.    memcpy
101e9 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 69 2c 20 73  (&zBuf[n], &i, s
101ea 69 7a 65 6f 66 28 69 29 29 3b 0a 20 20 20 20 6e  izeof(i));.    n
101eb 20 2b 3d 20 73 69 7a 65 6f 66 28 69 29 3b 0a 20   += sizeof(i);. 
101ec 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75   }.#endif.  retu
101ed 72 6e 20 6e 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  rn n;.}.../*.** 
101ee 53 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74  Sleep for a litt
101ef 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72  le while.  Retur
101f0 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20  n the amount of 
101f1 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 73  time slept..*/.s
101f2 74 61 74 69 63 20 69 6e 74 20 77 69 6e 53 6c 65  tatic int winSle
101f3 65 70 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  ep(sqlite3_vfs *
101f4 70 56 66 73 2c 20 69 6e 74 20 6d 69 63 72 6f 73  pVfs, int micros
101f5 65 63 29 7b 0a 20 20 53 6c 65 65 70 28 28 6d 69  ec){.  Sleep((mi
101f6 63 72 6f 73 65 63 2b 39 39 39 29 2f 31 30 30 30  crosec+999)/1000
101f7 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  );.  UNUSED_PARA
101f8 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 72  METER(pVfs);.  r
101f9 65 74 75 72 6e 20 28 28 6d 69 63 72 6f 73 65 63  eturn ((microsec
101fa 2b 39 39 39 29 2f 31 30 30 30 29 2a 31 30 30 30  +999)/1000)*1000
101fb 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  ;.}../*.** The f
101fc 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c  ollowing variabl
101fd 65 2c 20 69 66 20 73 65 74 20 74 6f 20 61 20 6e  e, if set to a n
101fe 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62  on-zero value, b
101ff 65 63 6f 6d 65 73 20 74 68 65 20 72 65 73 75 6c  ecomes the resul
10200 74 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 66 72  t.** returned fr
10201 6f 6d 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72  om sqlite3OsCurr
10202 65 6e 74 54 69 6d 65 28 29 2e 20 20 54 68 69 73  entTime().  This
10203 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73   is used for tes
10204 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  ting..*/.#ifdef 
10205 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49  SQLITE_TEST.SQLI
10206 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
10207 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 20  e3_current_time 
10208 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  = 0;.#endif../*.
10209 2a 2a 20 46 69 6e 64 20 74 68 65 20 63 75 72 72  ** Find the curr
1020a 65 6e 74 20 74 69 6d 65 20 28 69 6e 20 55 6e 69  ent time (in Uni
1020b 76 65 72 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74  versal Coordinat
1020c 65 64 20 54 69 6d 65 29 2e 20 20 57 72 69 74 65  ed Time).  Write
1020d 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20   the.** current 
1020e 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 61 73  time and date as
1020f 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75   a Julian Day nu
10210 6d 62 65 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77  mber into *prNow
10211 20 61 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30   and.** return 0
10212 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66 20 74  .  Return 1 if t
10213 68 65 20 74 69 6d 65 20 61 6e 64 20 64 61 74 65  he time and date
10214 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64   cannot be found
10215 2e 0a 2a 2f 0a 69 6e 74 20 77 69 6e 43 75 72 72  ..*/.int winCurr
10216 65 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f  entTime(sqlite3_
10217 76 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c  vfs *pVfs, doubl
10218 65 20 2a 70 72 4e 6f 77 29 7b 0a 20 20 46 49 4c  e *prNow){.  FIL
10219 45 54 49 4d 45 20 66 74 3b 0a 20 20 2f 2a 20 46  ETIME ft;.  /* F
1021a 49 4c 45 54 49 4d 45 20 73 74 72 75 63 74 75 72  ILETIME structur
1021b 65 20 69 73 20 61 20 36 34 2d 62 69 74 20 76 61  e is a 64-bit va
1021c 6c 75 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67  lue representing
1021d 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 0a   the number of .
1021e 20 20 20 20 20 31 30 30 2d 6e 61 6e 6f 73 65 63       100-nanosec
1021f 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73 20 73 69  ond intervals si
10220 6e 63 65 20 4a 61 6e 75 61 72 79 20 31 2c 20 31  nce January 1, 1
10221 36 30 31 20 28 3d 20 4a 44 20 32 33 30 35 38 31  601 (= JD 230581
10222 33 2e 35 29 2e 20 0a 20 20 2a 2f 0a 20 20 73 71  3.5). .  */.  sq
10223 6c 69 74 65 33 5f 69 6e 74 36 34 20 74 69 6d 65  lite3_int64 time
10224 57 3b 20 20 20 2f 2a 20 57 68 6f 6c 65 20 64 61  W;   /* Whole da
10225 79 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ys */.  sqlite3_
10226 69 6e 74 36 34 20 74 69 6d 65 46 3b 20 20 20 2f  int64 timeF;   /
10227 2a 20 46 72 61 63 74 69 6f 6e 61 6c 20 44 61 79  * Fractional Day
10228 73 20 2a 2f 0a 0a 20 20 2f 2a 20 4e 75 6d 62 65  s */..  /* Numbe
10229 72 20 6f 66 20 31 30 30 2d 6e 61 6e 6f 73 65 63  r of 100-nanosec
1022a 6f 6e 64 20 69 6e 74 65 72 76 61 6c 73 20 69 6e  ond intervals in
1022b 20 61 20 73 69 6e 67 6c 65 20 64 61 79 20 2a 2f   a single day */
1022c 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
1022d 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 6e 74  sqlite3_int64 nt
1022e 75 50 65 72 44 61 79 20 3d 20 0a 20 20 20 20 20  uPerDay = .     
1022f 20 31 30 30 30 30 30 30 30 2a 28 73 71 6c 69 74   10000000*(sqlit
10230 65 33 5f 69 6e 74 36 34 29 38 36 34 30 30 3b 0a  e3_int64)86400;.
10231 0a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20  .  /* Number of 
10232 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64 20 69  100-nanosecond i
10233 6e 74 65 72 76 61 6c 73 20 69 6e 20 68 61 6c 66  ntervals in half
10234 20 6f 66 20 61 20 64 61 79 20 2a 2f 0a 20 20 73   of a day */.  s
10235 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
10236 74 65 33 5f 69 6e 74 36 34 20 6e 74 75 50 65 72  te3_int64 ntuPer
10237 48 61 6c 66 44 61 79 20 3d 20 0a 20 20 20 20 20  HalfDay = .     
10238 20 31 30 30 30 30 30 30 30 2a 28 73 71 6c 69 74   10000000*(sqlit
10239 65 33 5f 69 6e 74 36 34 29 34 33 32 30 30 3b 0a  e3_int64)43200;.
1023a 0a 20 20 2f 2a 20 32 5e 33 32 20 2d 20 74 6f 20  .  /* 2^32 - to 
1023b 61 76 6f 69 64 20 75 73 65 20 6f 66 20 4c 4c 20  avoid use of LL 
1023c 61 6e 64 20 77 61 72 6e 69 6e 67 73 20 69 6e 20  and warnings in 
1023d 67 63 63 20 2a 2f 0a 20 20 73 74 61 74 69 63 20  gcc */.  static 
1023e 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6e  const sqlite3_in
1023f 74 36 34 20 6d 61 78 33 32 42 69 74 56 61 6c 75  t64 max32BitValu
10240 65 20 3d 20 0a 20 20 20 20 20 20 28 73 71 6c 69  e = .      (sqli
10241 74 65 33 5f 69 6e 74 36 34 29 32 30 30 30 30 30  te3_int64)200000
10242 30 30 30 30 20 2b 20 28 73 71 6c 69 74 65 33 5f  0000 + (sqlite3_
10243 69 6e 74 36 34 29 32 30 30 30 30 30 30 30 30 30  int64)2000000000
10244 20 2b 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36   + (sqlite3_int6
10245 34 29 32 39 34 39 36 37 32 39 36 3b 0a 0a 23 69  4)294967296;..#i
10246 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
10247 45 0a 20 20 53 59 53 54 45 4d 54 49 4d 45 20 74  E.  SYSTEMTIME t
10248 69 6d 65 3b 0a 20 20 47 65 74 53 79 73 74 65 6d  ime;.  GetSystem
10249 54 69 6d 65 28 26 74 69 6d 65 29 3b 0a 20 20 2f  Time(&time);.  /
1024a 2a 20 69 66 20 53 79 73 74 65 6d 54 69 6d 65 54  * if SystemTimeT
1024b 6f 46 69 6c 65 54 69 6d 65 28 29 20 66 61 69 6c  oFileTime() fail
1024c 73 2c 20 69 74 20 72 65 74 75 72 6e 73 20 7a 65  s, it returns ze
1024d 72 6f 2e 20 2a 2f 0a 20 20 69 66 20 28 21 53 79  ro. */.  if (!Sy
1024e 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69  stemTimeToFileTi
1024f 6d 65 28 26 74 69 6d 65 2c 26 66 74 29 29 7b 0a  me(&time,&ft)){.
10250 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
10251 7d 0a 23 65 6c 73 65 0a 20 20 47 65 74 53 79 73  }.#else.  GetSys
10252 74 65 6d 54 69 6d 65 41 73 46 69 6c 65 54 69 6d  temTimeAsFileTim
10253 65 28 20 26 66 74 20 29 3b 0a 23 65 6e 64 69 66  e( &ft );.#endif
10254 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
10255 54 45 52 28 70 56 66 73 29 3b 0a 20 20 74 69 6d  TER(pVfs);.  tim
10256 65 57 20 3d 20 28 28 28 73 71 6c 69 74 65 33 5f  eW = (((sqlite3_
10257 69 6e 74 36 34 29 66 74 2e 64 77 48 69 67 68 44  int64)ft.dwHighD
10258 61 74 65 54 69 6d 65 29 2a 6d 61 78 33 32 42 69  ateTime)*max32Bi
10259 74 56 61 6c 75 65 29 20 2b 20 28 73 71 6c 69 74  tValue) + (sqlit
1025a 65 33 5f 69 6e 74 36 34 29 66 74 2e 64 77 4c 6f  e3_int64)ft.dwLo
1025b 77 44 61 74 65 54 69 6d 65 3b 0a 20 20 74 69 6d  wDateTime;.  tim
1025c 65 46 20 3d 20 74 69 6d 65 57 20 25 20 6e 74 75  eF = timeW % ntu
1025d 50 65 72 44 61 79 3b 20 20 20 20 20 20 20 20 20  PerDay;         
1025e 20 2f 2a 20 66 72 61 63 74 69 6f 6e 61 6c 20 64   /* fractional d
1025f 61 79 73 20 28 31 30 30 2d 6e 61 6e 6f 73 65 63  ays (100-nanosec
10260 6f 6e 64 73 29 20 2a 2f 0a 20 20 74 69 6d 65 57  onds) */.  timeW
10261 20 3d 20 74 69 6d 65 57 20 2f 20 6e 74 75 50 65   = timeW / ntuPe
10262 72 44 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f  rDay;          /
10263 2a 20 77 68 6f 6c 65 20 64 61 79 73 20 2a 2f 0a  * whole days */.
10264 20 20 74 69 6d 65 57 20 3d 20 74 69 6d 65 57 20    timeW = timeW 
10265 2b 20 32 33 30 35 38 31 33 3b 20 20 20 20 20 20  + 2305813;      
10266 20 20 20 20 20 20 2f 2a 20 61 64 64 20 77 68 6f        /* add who
10267 6c 65 20 64 61 79 73 20 28 66 72 6f 6d 20 32 33  le days (from 23
10268 30 35 38 31 33 2e 35 29 20 2a 2f 0a 20 20 74 69  05813.5) */.  ti
10269 6d 65 46 20 3d 20 74 69 6d 65 46 20 2b 20 6e 74  meF = timeF + nt
1026a 75 50 65 72 48 61 6c 66 44 61 79 3b 20 20 20 20  uPerHalfDay;    
1026b 20 20 2f 2a 20 61 64 64 20 68 61 6c 66 20 61 20    /* add half a 
1026c 64 61 79 20 28 66 72 6f 6d 20 32 33 30 35 38 31  day (from 230581
1026d 33 2e 35 29 20 2a 2f 0a 20 20 74 69 6d 65 57 20  3.5) */.  timeW 
1026e 3d 20 74 69 6d 65 57 20 2b 20 28 74 69 6d 65 46  = timeW + (timeF
1026f 2f 6e 74 75 50 65 72 44 61 79 29 3b 20 20 2f 2a  /ntuPerDay);  /*
10270 20 61 64 64 20 77 68 6f 6c 65 20 64 61 79 20 69   add whole day i
10271 66 20 68 61 6c 66 20 64 61 79 20 6d 61 64 65 20  f half day made 
10272 6f 6e 65 20 2a 2f 0a 20 20 74 69 6d 65 46 20 3d  one */.  timeF =
10273 20 74 69 6d 65 46 20 25 20 6e 74 75 50 65 72 44   timeF % ntuPerD
10274 61 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ay;          /* 
10275 63 6f 6d 70 75 74 65 20 6e 65 77 20 66 72 61 63  compute new frac
10276 74 69 6f 6e 61 6c 20 64 61 79 73 20 2a 2f 0a 20  tional days */. 
10277 20 2a 70 72 4e 6f 77 20 3d 20 28 64 6f 75 62 6c   *prNow = (doubl
10278 65 29 74 69 6d 65 57 20 2b 20 28 28 64 6f 75 62  e)timeW + ((doub
10279 6c 65 29 74 69 6d 65 46 20 2f 20 28 64 6f 75 62  le)timeF / (doub
1027a 6c 65 29 6e 74 75 50 65 72 44 61 79 29 3b 0a 23  le)ntuPerDay);.#
1027b 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
1027c 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  T.  if( sqlite3_
1027d 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a  current_time ){.
1027e 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 28 28 64      *prNow = ((d
1027f 6f 75 62 6c 65 29 73 71 6c 69 74 65 33 5f 63 75  ouble)sqlite3_cu
10280 72 72 65 6e 74 5f 74 69 6d 65 20 2b 20 28 64 6f  rrent_time + (do
10281 75 62 6c 65 29 34 33 32 30 30 29 20 2f 20 28 64  uble)43200) / (d
10282 6f 75 62 6c 65 29 38 36 34 30 30 20 2b 20 28 64  ouble)86400 + (d
10283 6f 75 62 6c 65 29 32 34 34 30 35 38 37 3b 0a 20  ouble)2440587;. 
10284 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75   }.#endif.  retu
10285 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  rn 0;.}../*.** T
10286 68 65 20 69 64 65 61 20 69 73 20 74 68 61 74 20  he idea is that 
10287 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 6f  this function wo
10288 72 6b 73 20 6c 69 6b 65 20 61 20 63 6f 6d 62 69  rks like a combi
10289 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 47 65 74  nation of.** Get
1028a 4c 61 73 74 45 72 72 6f 72 28 29 20 61 6e 64 20  LastError() and 
1028b 46 6f 72 6d 61 74 4d 65 73 73 61 67 65 28 29 20  FormatMessage() 
1028c 6f 6e 20 77 69 6e 64 6f 77 73 20 28 6f 72 20 65  on windows (or e
1028d 72 72 6e 6f 20 61 6e 64 0a 2a 2a 20 73 74 72 65  rrno and.** stre
1028e 72 72 6f 72 5f 72 28 29 20 6f 6e 20 75 6e 69 78  rror_r() on unix
1028f 29 2e 20 41 66 74 65 72 20 61 6e 20 65 72 72 6f  ). After an erro
10290 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 79  r is returned by
10291 20 61 6e 20 4f 53 0a 2a 2a 20 66 75 6e 63 74 69   an OS.** functi
10292 6f 6e 2c 20 53 51 4c 69 74 65 20 63 61 6c 6c 73  on, SQLite calls
10293 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77   this function w
10294 69 74 68 20 7a 42 75 66 20 70 6f 69 6e 74 69 6e  ith zBuf pointin
10295 67 20 74 6f 0a 2a 2a 20 61 20 62 75 66 66 65 72  g to.** a buffer
10296 20 6f 66 20 6e 42 75 66 20 62 79 74 65 73 2e 20   of nBuf bytes. 
10297 54 68 65 20 4f 53 20 6c 61 79 65 72 20 73 68 6f  The OS layer sho
10298 75 6c 64 20 70 6f 70 75 6c 61 74 65 20 74 68 65  uld populate the
10299 0a 2a 2a 20 62 75 66 66 65 72 20 77 69 74 68 20  .** buffer with 
1029a 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64  a nul-terminated
1029b 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 65   UTF-8 encoded e
1029c 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20  rror message.** 
1029d 64 65 73 63 72 69 62 69 6e 67 20 74 68 65 20 6c  describing the l
1029e 61 73 74 20 49 4f 20 65 72 72 6f 72 20 74 6f 20  ast IO error to 
1029f 68 61 76 65 20 6f 63 63 75 72 72 65 64 20 77 69  have occurred wi
102a0 74 68 69 6e 20 74 68 65 20 63 61 6c 6c 69 6e 67  thin the calling
102a1 0a 2a 2a 20 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a  .** thread..**.*
102a2 2a 20 49 66 20 74 68 65 20 65 72 72 6f 72 20 6d  * If the error m
102a3 65 73 73 61 67 65 20 69 73 20 74 6f 6f 20 6c 61  essage is too la
102a4 72 67 65 20 66 6f 72 20 74 68 65 20 73 75 70 70  rge for the supp
102a5 6c 69 65 64 20 62 75 66 66 65 72 2c 0a 2a 2a 20  lied buffer,.** 
102a6 69 74 20 73 68 6f 75 6c 64 20 62 65 20 74 72 75  it should be tru
102a7 6e 63 61 74 65 64 2e 20 54 68 65 20 72 65 74 75  ncated. The retu
102a8 72 6e 20 76 61 6c 75 65 20 6f 66 20 78 47 65 74  rn value of xGet
102a9 4c 61 73 74 45 72 72 6f 72 0a 2a 2a 20 69 73 20  LastError.** is 
102aa 7a 65 72 6f 20 69 66 20 74 68 65 20 65 72 72 6f  zero if the erro
102ab 72 20 6d 65 73 73 61 67 65 20 66 69 74 73 20 69  r message fits i
102ac 6e 20 74 68 65 20 62 75 66 66 65 72 2c 20 6f 72  n the buffer, or
102ad 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20 6f 74 68   non-zero.** oth
102ae 65 72 77 69 73 65 20 28 69 66 20 74 68 65 20 6d  erwise (if the m
102af 65 73 73 61 67 65 20 77 61 73 20 74 72 75 6e 63  essage was trunc
102b0 61 74 65 64 29 2e 20 49 66 20 6e 6f 6e 2d 7a 65  ated). If non-ze
102b1 72 6f 20 69 73 20 72 65 74 75 72 6e 65 64 2c 0a  ro is returned,.
102b2 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20 6e 6f  ** then it is no
102b3 74 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 69  t necessary to i
102b4 6e 63 6c 75 64 65 20 74 68 65 20 6e 75 6c 2d 74  nclude the nul-t
102b5 65 72 6d 69 6e 61 74 6f 72 20 63 68 61 72 61 63  erminator charac
102b6 74 65 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 75  ter.** in the ou
102b7 74 70 75 74 20 62 75 66 66 65 72 2e 0a 2a 2a 0a  tput buffer..**.
102b8 2a 2a 20 4e 6f 74 20 73 75 70 70 6c 79 69 6e 67  ** Not supplying
102b9 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
102ba 65 20 77 69 6c 6c 20 68 61 76 65 20 6e 6f 20 61  e will have no a
102bb 64 76 65 72 73 65 20 65 66 66 65 63 74 0a 2a 2a  dverse effect.**
102bc 20 6f 6e 20 53 51 4c 69 74 65 2e 20 49 74 20 69   on SQLite. It i
102bd 73 20 66 69 6e 65 20 74 6f 20 68 61 76 65 20 61  s fine to have a
102be 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
102bf 20 74 68 61 74 20 6e 65 76 65 72 0a 2a 2a 20 72   that never.** r
102c0 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20  eturns an error 
102c1 6d 65 73 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20  message:.**.**  
102c2 20 69 6e 74 20 78 47 65 74 4c 61 73 74 45 72 72   int xGetLastErr
102c3 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  or(sqlite3_vfs *
102c4 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20  pVfs, int nBuf, 
102c5 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a 2a 2a 20  char *zBuf){.** 
102c6 20 20 20 20 61 73 73 65 72 74 28 7a 42 75 66 5b      assert(zBuf[
102c7 30 5d 3d 3d 27 5c 30 27 29 3b 0a 2a 2a 20 20 20  0]=='\0');.**   
102c8 20 20 72 65 74 75 72 6e 20 30 3b 0a 2a 2a 20 20    return 0;.**  
102c9 20 7d 0a 2a 2a 0a 2a 2a 20 48 6f 77 65 76 65 72   }.**.** However
102ca 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6d 65 73   if an error mes
102cb 73 61 67 65 20 69 73 20 73 75 70 70 6c 69 65 64  sage is supplied
102cc 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 69 6e 63  , it will be inc
102cd 6f 72 70 6f 72 61 74 65 64 0a 2a 2a 20 62 79 20  orporated.** by 
102ce 73 71 6c 69 74 65 20 69 6e 74 6f 20 74 68 65 20  sqlite into the 
102cf 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 61 76  error message av
102d0 61 69 6c 61 62 6c 65 20 74 6f 20 74 68 65 20 75  ailable to the u
102d1 73 65 72 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c  ser using.** sql
102d2 69 74 65 33 5f 65 72 72 6d 73 67 28 29 2c 20 70  ite3_errmsg(), p
102d3 6f 73 73 69 62 6c 79 20 6d 61 6b 69 6e 67 20 49  ossibly making I
102d4 4f 20 65 72 72 6f 72 73 20 65 61 73 69 65 72 20  O errors easier 
102d5 74 6f 20 64 65 62 75 67 2e 0a 2a 2f 0a 73 74 61  to debug..*/.sta
102d6 74 69 63 20 69 6e 74 20 77 69 6e 47 65 74 4c 61  tic int winGetLa
102d7 73 74 45 72 72 6f 72 28 73 71 6c 69 74 65 33 5f  stError(sqlite3_
102d8 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e  vfs *pVfs, int n
102d9 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29  Buf, char *zBuf)
102da 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
102db 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 72 65  ETER(pVfs);.  re
102dc 74 75 72 6e 20 67 65 74 4c 61 73 74 45 72 72 6f  turn getLastErro
102dd 72 4d 73 67 28 6e 42 75 66 2c 20 7a 42 75 66 29  rMsg(nBuf, zBuf)
102de 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  ;.}../*.** Initi
102df 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74  alize and deinit
102e0 69 61 6c 69 7a 65 20 74 68 65 20 6f 70 65 72 61  ialize the opera
102e1 74 69 6e 67 20 73 79 73 74 65 6d 20 69 6e 74 65  ting system inte
102e2 72 66 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rface..*/.SQLITE
102e3 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
102e4 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a  _os_init(void){.
102e5 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33    static sqlite3
102e6 5f 76 66 73 20 77 69 6e 56 66 73 20 3d 20 7b 0a  _vfs winVfs = {.
102e7 20 20 20 20 31 2c 20 20 20 20 20 20 20 20 20 20      1,          
102e8 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69         /* iVersi
102e9 6f 6e 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66  on */.    sizeof
102ea 28 77 69 6e 46 69 6c 65 29 2c 20 20 20 2f 2a 20  (winFile),   /* 
102eb 73 7a 4f 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20  szOsFile */.    
102ec 4d 41 58 5f 50 41 54 48 2c 20 20 20 20 20 20 20  MAX_PATH,       
102ed 20 20 20 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65     /* mxPathname
102ee 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20   */.    0,      
102ef 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 4e             /* pN
102f0 65 78 74 20 2a 2f 0a 20 20 20 20 22 77 69 6e 33  ext */.    "win3
102f1 32 22 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  2",           /*
102f2 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c   zName */.    0,
102f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
102f4 20 2f 2a 20 70 41 70 70 44 61 74 61 20 2a 2f 0a   /* pAppData */.
102f5 20 0a 20 20 20 20 77 69 6e 4f 70 65 6e 2c 20 20   .    winOpen,  
102f6 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65           /* xOpe
102f7 6e 20 2a 2f 0a 20 20 20 20 77 69 6e 44 65 6c 65  n */.    winDele
102f8 74 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78  te,         /* x
102f9 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 77 69  Delete */.    wi
102fa 6e 41 63 63 65 73 73 2c 20 20 20 20 20 20 20 20  nAccess,        
102fb 20 2f 2a 20 78 41 63 63 65 73 73 20 2a 2f 0a 20   /* xAccess */. 
102fc 20 20 20 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61     winFullPathna
102fd 6d 65 2c 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61  me,   /* xFullPa
102fe 74 68 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 77 69  thname */.    wi
102ff 6e 44 6c 4f 70 65 6e 2c 20 20 20 20 20 20 20 20  nDlOpen,        
10300 20 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20   /* xDlOpen */. 
10301 20 20 20 77 69 6e 44 6c 45 72 72 6f 72 2c 20 20     winDlError,  
10302 20 20 20 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f        /* xDlErro
10303 72 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 53 79  r */.    winDlSy
10304 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78  m,          /* x
10305 44 6c 53 79 6d 20 2a 2f 0a 20 20 20 20 77 69 6e  DlSym */.    win
10306 44 6c 43 6c 6f 73 65 2c 20 20 20 20 20 20 20 20  DlClose,        
10307 2f 2a 20 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20  /* xDlClose */. 
10308 20 20 20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73     winRandomness
10309 2c 20 20 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d  ,     /* xRandom
1030a 6e 65 73 73 20 2a 2f 0a 20 20 20 20 77 69 6e 53  ness */.    winS
1030b 6c 65 65 70 2c 20 20 20 20 20 20 20 20 20 20 2f  leep,          /
1030c 2a 20 78 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20  * xSleep */.    
1030d 77 69 6e 43 75 72 72 65 6e 74 54 69 6d 65 2c 20  winCurrentTime, 
1030e 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 54 69     /* xCurrentTi
1030f 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e 47 65 74  me */.    winGet
10310 4c 61 73 74 45 72 72 6f 72 20 20 20 20 2f 2a 20  LastError    /* 
10311 78 47 65 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f  xGetLastError */
10312 0a 20 20 7d 3b 0a 0a 20 20 73 71 6c 69 74 65 33  .  };..  sqlite3
10313 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 77  _vfs_register(&w
10314 69 6e 56 66 73 2c 20 31 29 3b 0a 20 20 72 65 74  inVfs, 1);.  ret
10315 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a  urn SQLITE_OK; .
10316 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
10317 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28   sqlite3_os_end(
10318 76 6f 69 64 29 7b 20 0a 20 20 72 65 74 75 72 6e  void){ .  return
10319 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23   SQLITE_OK;.}..#
1031a 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1031b 4f 53 5f 57 49 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  OS_WIN */../****
1031c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
1031d 66 20 6f 73 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a  f os_win.c *****
1031e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1031f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
10321 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
10322 20 66 69 6c 65 20 62 69 74 76 65 63 2e 63 20 2a   file bitvec.c *
10323 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10324 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10325 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
10326 20 32 30 30 38 20 46 65 62 72 75 61 72 79 20 31   2008 February 1
10327 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  6.**.** The auth
10328 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
10329 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
1032a 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
1032b 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
1032c 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
1032d 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
1032e 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
1032f 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
10330 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
10331 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
10332 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
10333 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
10334 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
10335 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
10336 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
10337 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
10338 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
10339 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1033a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1033b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1033c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1033d 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d  .** This file im
1033e 70 6c 65 6d 65 6e 74 73 20 61 6e 20 6f 62 6a 65  plements an obje
1033f 63 74 20 74 68 61 74 20 72 65 70 72 65 73 65 6e  ct that represen
10340 74 73 20 61 20 66 69 78 65 64 2d 6c 65 6e 67 74  ts a fixed-lengt
10341 68 0a 2a 2a 20 62 69 74 6d 61 70 2e 20 20 42 69  h.** bitmap.  Bi
10342 74 73 20 61 72 65 20 6e 75 6d 62 65 72 65 64 20  ts are numbered 
10343 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 31 2e  starting with 1.
10344 0a 2a 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20  .**.** A bitmap 
10345 69 73 20 75 73 65 64 20 74 6f 20 72 65 63 6f 72  is used to recor
10346 64 20 77 68 69 63 68 20 70 61 67 65 73 20 6f 66  d which pages of
10347 20 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65   a database file
10348 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a 20 6a 6f   have been.** jo
10349 75 72 6e 61 6c 6c 65 64 20 64 75 72 69 6e 67 20  urnalled during 
1034a 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 6f  a transaction, o
1034b 72 20 77 68 69 63 68 20 70 61 67 65 73 20 68 61  r which pages ha
1034c 76 65 20 74 68 65 20 22 64 6f 6e 74 2d 77 72 69  ve the "dont-wri
1034d 74 65 22 0a 2a 2a 20 70 72 6f 70 65 72 74 79 2e  te".** property.
1034e 20 20 55 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 61    Usually only a
1034f 20 66 65 77 20 70 61 67 65 73 20 61 72 65 20 6d   few pages are m
10350 65 65 74 20 65 69 74 68 65 72 20 63 6f 6e 64 69  eet either condi
10351 74 69 6f 6e 2e 0a 2a 2a 20 53 6f 20 74 68 65 20  tion..** So the 
10352 62 69 74 6d 61 70 20 69 73 20 75 73 75 61 6c 6c  bitmap is usuall
10353 79 20 73 70 61 72 73 65 20 61 6e 64 20 68 61 73  y sparse and has
10354 20 6c 6f 77 20 63 61 72 64 69 6e 61 6c 69 74 79   low cardinality
10355 2e 0a 2a 2a 20 42 75 74 20 73 6f 6d 65 74 69 6d  ..** But sometim
10356 65 73 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20  es (for example 
10357 77 68 65 6e 20 64 75 72 69 6e 67 20 61 20 44 52  when during a DR
10358 4f 50 20 6f 66 20 61 20 6c 61 72 67 65 20 74 61  OP of a large ta
10359 62 6c 65 29 20 6d 6f 73 74 0a 2a 2a 20 6f 72 20  ble) most.** or 
1035a 61 6c 6c 20 6f 66 20 74 68 65 20 70 61 67 65 73  all of the pages
1035b 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 63   in a database c
1035c 61 6e 20 67 65 74 20 6a 6f 75 72 6e 61 6c 6c 65  an get journalle
1035d 64 2e 20 20 49 6e 20 74 68 6f 73 65 20 63 61 73  d.  In those cas
1035e 65 73 2c 20 0a 2a 2a 20 74 68 65 20 62 69 74 6d  es, .** the bitm
1035f 61 70 20 62 65 63 6f 6d 65 73 20 64 65 6e 73 65  ap becomes dense
10360 20 77 69 74 68 20 68 69 67 68 20 63 61 72 64 69   with high cardi
10361 6e 61 6c 69 74 79 2e 20 20 54 68 65 20 61 6c 67  nality.  The alg
10362 6f 72 69 74 68 6d 20 6e 65 65 64 73 20 0a 2a 2a  orithm needs .**
10363 20 74 6f 20 68 61 6e 64 6c 65 20 62 6f 74 68 20   to handle both 
10364 63 61 73 65 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a  cases well..**.*
10365 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74 68  * The size of th
10366 65 20 62 69 74 6d 61 70 20 69 73 20 66 69 78 65  e bitmap is fixe
10367 64 20 77 68 65 6e 20 74 68 65 20 6f 62 6a 65 63  d when the objec
10368 74 20 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a  t is created..**
10369 0a 2a 2a 20 41 6c 6c 20 62 69 74 73 20 61 72 65  .** All bits are
1036a 20 63 6c 65 61 72 20 77 68 65 6e 20 74 68 65 20   clear when the 
1036b 62 69 74 6d 61 70 20 69 73 20 63 72 65 61 74 65  bitmap is create
1036c 64 2e 20 20 49 6e 64 69 76 69 64 75 61 6c 20 62  d.  Individual b
1036d 69 74 73 0a 2a 2a 20 6d 61 79 20 62 65 20 73 65  its.** may be se
1036e 74 20 6f 72 20 63 6c 65 61 72 65 64 20 6f 6e 65  t or cleared one
1036f 20 61 74 20 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a   at a time..**.*
10370 2a 20 54 65 73 74 20 6f 70 65 72 61 74 69 6f 6e  * Test operation
10371 73 20 61 72 65 20 61 62 6f 75 74 20 31 30 30 20  s are about 100 
10372 74 69 6d 65 73 20 6d 6f 72 65 20 63 6f 6d 6d 6f  times more commo
10373 6e 20 74 68 61 74 20 73 65 74 20 6f 70 65 72 61  n that set opera
10374 74 69 6f 6e 73 2e 0a 2a 2a 20 43 6c 65 61 72 20  tions..** Clear 
10375 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20 65  operations are e
10376 78 63 65 65 64 69 6e 67 6c 79 20 72 61 72 65 2e  xceedingly rare.
10377 20 20 54 68 65 72 65 20 61 72 65 20 75 73 75 61    There are usua
10378 6c 6c 79 20 62 65 74 77 65 65 6e 0a 2a 2a 20 35  lly between.** 5
10379 20 61 6e 64 20 35 30 30 20 73 65 74 20 6f 70 65   and 500 set ope
1037a 72 61 74 69 6f 6e 73 20 70 65 72 20 42 69 74 76  rations per Bitv
1037b 65 63 20 6f 62 6a 65 63 74 2c 20 74 68 6f 75 67  ec object, thoug
1037c 68 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  h the number of 
1037d 73 65 74 73 20 63 61 6e 0a 2a 2a 20 73 6f 6d 65  sets can.** some
1037e 74 69 6d 65 73 20 67 72 6f 77 20 69 6e 74 6f 20  times grow into 
1037f 74 65 6e 73 20 6f 66 20 74 68 6f 75 73 61 6e 64  tens of thousand
10380 73 20 6f 72 20 6c 61 72 67 65 72 2e 20 20 54 68  s or larger.  Th
10381 65 20 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a  e size of the.**
10382 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74 20 69   Bitvec object i
10383 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
10384 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74  pages in the dat
10385 61 62 61 73 65 20 66 69 6c 65 20 61 74 20 74 68  abase file at th
10386 65 0a 2a 2a 20 73 74 61 72 74 20 6f 66 20 61 20  e.** start of a 
10387 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 6e 64  transaction, and
10388 20 69 73 20 74 68 75 73 20 75 73 75 61 6c 6c 79   is thus usually
10389 20 6c 65 73 73 20 74 68 61 6e 20 61 20 66 65 77   less than a few
1038a 20 74 68 6f 75 73 61 6e 64 2c 0a 2a 2a 20 62 75   thousand,.** bu
1038b 74 20 63 61 6e 20 62 65 20 61 73 20 6c 61 72 67  t can be as larg
1038c 65 20 61 73 20 32 20 62 69 6c 6c 69 6f 6e 20 66  e as 2 billion f
1038d 6f 72 20 61 20 72 65 61 6c 6c 79 20 62 69 67 20  or a really big 
1038e 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  database..**.** 
1038f 40 28 23 29 20 24 49 64 3a 20 62 69 74 76 65 63  @(#) $Id: bitvec
10390 2e 63 2c 76 20 31 2e 31 37 20 32 30 30 39 2f 30  .c,v 1.17 2009/0
10391 37 2f 32 35 20 31 37 3a 33 33 3a 32 36 20 64 72  7/25 17:33:26 dr
10392 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 53  h Exp $.*/../* S
10393 69 7a 65 20 6f 66 20 74 68 65 20 42 69 74 76 65  ize of the Bitve
10394 63 20 73 74 72 75 63 74 75 72 65 20 69 6e 20 62  c structure in b
10395 79 74 65 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65  ytes. */.#define
10396 20 42 49 54 56 45 43 5f 53 5a 20 20 20 20 20 20   BITVEC_SZ      
10397 20 20 28 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29    (sizeof(void*)
10398 2a 31 32 38 29 20 20 2f 2a 20 35 31 32 20 6f 6e  *128)  /* 512 on
10399 20 33 32 62 69 74 2e 20 20 31 30 32 34 20 6f 6e   32bit.  1024 on
1039a 20 36 34 62 69 74 20 2a 2f 0a 0a 2f 2a 20 52 6f   64bit */../* Ro
1039b 75 6e 64 20 74 68 65 20 75 6e 69 6f 6e 20 73 69  und the union si
1039c 7a 65 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 6e  ze down to the n
1039d 65 61 72 65 73 74 20 70 6f 69 6e 74 65 72 20 62  earest pointer b
1039e 6f 75 6e 64 61 72 79 2c 20 73 69 6e 63 65 20 74  oundary, since t
1039f 68 61 74 27 73 20 68 6f 77 20 0a 2a 2a 20 69 74  hat's how .** it
103a0 20 77 69 6c 6c 20 62 65 20 61 6c 69 67 6e 65 64   will be aligned
103a1 20 77 69 74 68 69 6e 20 74 68 65 20 42 69 74 76   within the Bitv
103a2 65 63 20 73 74 72 75 63 74 2e 20 2a 2f 0a 23 64  ec struct. */.#d
103a3 65 66 69 6e 65 20 42 49 54 56 45 43 5f 55 53 49  efine BITVEC_USI
103a4 5a 45 20 20 20 20 20 28 28 28 42 49 54 56 45 43  ZE     (((BITVEC
103a5 5f 53 5a 2d 28 33 2a 73 69 7a 65 6f 66 28 75 33  _SZ-(3*sizeof(u3
103a6 32 29 29 29 2f 73 69 7a 65 6f 66 28 42 69 74 76  2)))/sizeof(Bitv
103a7 65 63 2a 29 29 2a 73 69 7a 65 6f 66 28 42 69 74  ec*))*sizeof(Bit
103a8 76 65 63 2a 29 29 0a 0a 2f 2a 20 54 79 70 65 20  vec*))../* Type 
103a9 6f 66 20 74 68 65 20 61 72 72 61 79 20 22 65 6c  of the array "el
103aa 65 6d 65 6e 74 22 20 66 6f 72 20 74 68 65 20 62  ement" for the b
103ab 69 74 6d 61 70 20 72 65 70 72 65 73 65 6e 74 61  itmap representa
103ac 74 69 6f 6e 2e 20 0a 2a 2a 20 53 68 6f 75 6c 64  tion. .** Should
103ad 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32   be a power of 2
103ae 2c 20 61 6e 64 20 69 64 65 61 6c 6c 79 2c 20 65  , and ideally, e
103af 76 65 6e 6c 79 20 64 69 76 69 64 65 20 69 6e 74  venly divide int
103b0 6f 20 42 49 54 56 45 43 5f 55 53 49 5a 45 2e 20  o BITVEC_USIZE. 
103b1 0a 2a 2a 20 53 65 74 74 69 6e 67 20 74 68 69 73  .** Setting this
103b2 20 74 6f 20 74 68 65 20 22 6e 61 74 75 72 61 6c   to the "natural
103b3 20 77 6f 72 64 22 20 73 69 7a 65 20 6f 66 20 79   word" size of y
103b4 6f 75 72 20 43 50 55 20 6d 61 79 20 69 6d 70 72  our CPU may impr
103b5 6f 76 65 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e  ove.** performan
103b6 63 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  ce. */.#define B
103b7 49 54 56 45 43 5f 54 45 4c 45 4d 20 20 20 20 20  ITVEC_TELEM     
103b8 75 38 0a 2f 2a 20 53 69 7a 65 2c 20 69 6e 20 62  u8./* Size, in b
103b9 69 74 73 2c 20 6f 66 20 74 68 65 20 62 69 74 6d  its, of the bitm
103ba 61 70 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 23  ap element. */.#
103bb 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 53 5a  define BITVEC_SZ
103bc 45 4c 45 4d 20 20 20 20 38 0a 2f 2a 20 4e 75 6d  ELEM    8./* Num
103bd 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
103be 69 6e 20 61 20 62 69 74 6d 61 70 20 61 72 72 61  in a bitmap arra
103bf 79 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49  y. */.#define BI
103c0 54 56 45 43 5f 4e 45 4c 45 4d 20 20 20 20 20 28  TVEC_NELEM     (
103c1 42 49 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a  BITVEC_USIZE/siz
103c2 65 6f 66 28 42 49 54 56 45 43 5f 54 45 4c 45 4d  eof(BITVEC_TELEM
103c3 29 29 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20  ))./* Number of 
103c4 62 69 74 73 20 69 6e 20 74 68 65 20 62 69 74 6d  bits in the bitm
103c5 61 70 20 61 72 72 61 79 2e 20 2a 2f 0a 23 64 65  ap array. */.#de
103c6 66 69 6e 65 20 42 49 54 56 45 43 5f 4e 42 49 54  fine BITVEC_NBIT
103c7 20 20 20 20 20 20 28 42 49 54 56 45 43 5f 4e 45        (BITVEC_NE
103c8 4c 45 4d 2a 42 49 54 56 45 43 5f 53 5a 45 4c 45  LEM*BITVEC_SZELE
103c9 4d 29 0a 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66  M)../* Number of
103ca 20 75 33 32 20 76 61 6c 75 65 73 20 69 6e 20 68   u32 values in h
103cb 61 73 68 20 74 61 62 6c 65 2e 20 2a 2f 0a 23 64  ash table. */.#d
103cc 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e 49 4e  efine BITVEC_NIN
103cd 54 20 20 20 20 20 20 28 42 49 54 56 45 43 5f 55  T      (BITVEC_U
103ce 53 49 5a 45 2f 73 69 7a 65 6f 66 28 75 33 32 29  SIZE/sizeof(u32)
103cf 29 0a 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d  )./* Maximum num
103d0 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
103d1 6e 20 68 61 73 68 20 74 61 62 6c 65 20 62 65 66  n hash table bef
103d2 6f 72 65 20 0a 2a 2a 20 73 75 62 2d 64 69 76 69  ore .** sub-divi
103d3 64 69 6e 67 20 61 6e 64 20 72 65 2d 68 61 73 68  ding and re-hash
103d4 69 6e 67 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ing. */.#define 
103d5 42 49 54 56 45 43 5f 4d 58 48 41 53 48 20 20 20  BITVEC_MXHASH   
103d6 20 28 42 49 54 56 45 43 5f 4e 49 4e 54 2f 32 29   (BITVEC_NINT/2)
103d7 0a 2f 2a 20 48 61 73 68 69 6e 67 20 66 75 6e 63  ./* Hashing func
103d8 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 61 48 61  tion for the aHa
103d9 73 68 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  sh representatio
103da 6e 2e 0a 2a 2a 20 45 6d 70 69 72 69 63 61 6c 20  n..** Empirical 
103db 74 65 73 74 69 6e 67 20 73 68 6f 77 65 64 20 74  testing showed t
103dc 68 61 74 20 74 68 65 20 2a 33 37 20 6d 75 6c 74  hat the *37 mult
103dd 69 70 6c 69 65 72 20 0a 2a 2a 20 28 61 6e 20 61  iplier .** (an a
103de 72 62 69 74 72 61 72 79 20 70 72 69 6d 65 29 69  rbitrary prime)i
103df 6e 20 74 68 65 20 68 61 73 68 20 66 75 6e 63 74  n the hash funct
103e0 69 6f 6e 20 70 72 6f 76 69 64 65 64 20 0a 2a 2a  ion provided .**
103e1 20 6e 6f 20 66 65 77 65 72 20 63 6f 6c 6c 69 73   no fewer collis
103e2 69 6f 6e 73 20 74 68 61 6e 20 74 68 65 20 6e 6f  ions than the no
103e3 2d 6f 70 20 2a 31 2e 20 2a 2f 0a 23 64 65 66 69  -op *1. */.#defi
103e4 6e 65 20 42 49 54 56 45 43 5f 48 41 53 48 28 58  ne BITVEC_HASH(X
103e5 29 20 20 20 28 28 28 58 29 2a 31 29 25 42 49 54  )   (((X)*1)%BIT
103e6 56 45 43 5f 4e 49 4e 54 29 0a 0a 23 64 65 66 69  VEC_NINT)..#defi
103e7 6e 65 20 42 49 54 56 45 43 5f 4e 50 54 52 20 20  ne BITVEC_NPTR  
103e8 20 20 20 20 28 42 49 54 56 45 43 5f 55 53 49 5a      (BITVEC_USIZ
103e9 45 2f 73 69 7a 65 6f 66 28 42 69 74 76 65 63 20  E/sizeof(Bitvec 
103ea 2a 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 62 69  *)).../*.** A bi
103eb 74 6d 61 70 20 69 73 20 61 6e 20 69 6e 73 74 61  tmap is an insta
103ec 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
103ed 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a  wing structure..
103ee 2a 2a 0a 2a 2a 20 54 68 69 73 20 62 69 74 6d 61  **.** This bitma
103ef 70 20 72 65 63 6f 72 64 73 20 74 68 65 20 65 78  p records the ex
103f0 69 73 74 61 6e 63 65 20 6f 66 20 7a 65 72 6f 20  istance of zero 
103f1 6f 72 20 6d 6f 72 65 20 62 69 74 73 0a 2a 2a 20  or more bits.** 
103f2 77 69 74 68 20 76 61 6c 75 65 73 20 62 65 74 77  with values betw
103f3 65 65 6e 20 31 20 61 6e 64 20 69 53 69 7a 65 2c  een 1 and iSize,
103f4 20 69 6e 63 6c 75 73 69 76 65 2e 0a 2a 2a 0a 2a   inclusive..**.*
103f5 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65  * There are thre
103f6 65 20 70 6f 73 73 69 62 6c 65 20 72 65 70 72 65  e possible repre
103f7 73 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68  sentations of th
103f8 65 20 62 69 74 6d 61 70 2e 0a 2a 2a 20 49 66 20  e bitmap..** If 
103f9 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42  iSize<=BITVEC_NB
103fa 49 54 2c 20 74 68 65 6e 20 42 69 74 76 65 63 2e  IT, then Bitvec.
103fb 75 2e 61 42 69 74 6d 61 70 5b 5d 20 69 73 20 61  u.aBitmap[] is a
103fc 20 73 74 72 61 69 67 68 74 0a 2a 2a 20 62 69 74   straight.** bit
103fd 6d 61 70 2e 20 20 54 68 65 20 6c 65 61 73 74 20  map.  The least 
103fe 73 69 67 6e 69 66 69 63 61 6e 74 20 62 69 74 20  significant bit 
103ff 69 73 20 62 69 74 20 31 2e 0a 2a 2a 0a 2a 2a 20  is bit 1..**.** 
10400 49 66 20 69 53 69 7a 65 3e 42 49 54 56 45 43 5f  If iSize>BITVEC_
10401 4e 42 49 54 20 61 6e 64 20 69 44 69 76 69 73 6f  NBIT and iDiviso
10402 72 3d 3d 30 20 74 68 65 6e 20 42 69 74 76 65 63  r==0 then Bitvec
10403 2e 75 2e 61 48 61 73 68 5b 5d 20 69 73 0a 2a 2a  .u.aHash[] is.**
10404 20 61 20 68 61 73 68 20 74 61 62 6c 65 20 74 68   a hash table th
10405 61 74 20 77 69 6c 6c 20 68 6f 6c 64 20 75 70 20  at will hold up 
10406 74 6f 20 42 49 54 56 45 43 5f 4d 58 48 41 53 48  to BITVEC_MXHASH
10407 20 64 69 73 74 69 6e 63 74 20 76 61 6c 75 65 73   distinct values
10408 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ..**.** Otherwis
10409 65 2c 20 74 68 65 20 76 61 6c 75 65 20 69 20 69  e, the value i i
1040a 73 20 72 65 64 69 72 65 63 74 65 64 20 69 6e 74  s redirected int
1040b 6f 20 6f 6e 65 20 6f 66 20 42 49 54 56 45 43 5f  o one of BITVEC_
1040c 4e 50 54 52 0a 2a 2a 20 73 75 62 2d 62 69 74 6d  NPTR.** sub-bitm
1040d 61 70 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  aps pointed to b
1040e 79 20 42 69 74 76 65 63 2e 75 2e 61 70 53 75 62  y Bitvec.u.apSub
1040f 5b 5d 2e 20 20 45 61 63 68 20 73 75 62 62 69 74  [].  Each subbit
10410 6d 61 70 0a 2a 2a 20 68 61 6e 64 6c 65 73 20 75  map.** handles u
10411 70 20 74 6f 20 69 44 69 76 69 73 6f 72 20 73 65  p to iDivisor se
10412 70 61 72 61 74 65 20 76 61 6c 75 65 73 20 6f 66  parate values of
10413 20 69 2e 20 20 61 70 53 75 62 5b 30 5d 20 68 6f   i.  apSub[0] ho
10414 6c 64 73 0a 2a 2a 20 76 61 6c 75 65 73 20 62 65  lds.** values be
10415 74 77 65 65 6e 20 31 20 61 6e 64 20 69 44 69 76  tween 1 and iDiv
10416 69 73 6f 72 2e 20 20 61 70 53 75 62 5b 31 5d 20  isor.  apSub[1] 
10417 68 6f 6c 64 73 20 76 61 6c 75 65 73 20 62 65 74  holds values bet
10418 77 65 65 6e 0a 2a 2a 20 69 44 69 76 69 73 6f 72  ween.** iDivisor
10419 2b 31 20 61 6e 64 20 32 2a 69 44 69 76 69 73 6f  +1 and 2*iDiviso
1041a 72 2e 20 20 61 70 53 75 62 5b 4e 5d 20 68 6f 6c  r.  apSub[N] hol
1041b 64 73 20 76 61 6c 75 65 73 20 62 65 74 77 65 65  ds values betwee
1041c 6e 0a 2a 2a 20 4e 2a 69 44 69 76 69 73 6f 72 2b  n.** N*iDivisor+
1041d 31 20 61 6e 64 20 28 4e 2b 31 29 2a 69 44 69 76  1 and (N+1)*iDiv
1041e 69 73 6f 72 2e 20 20 45 61 63 68 20 73 75 62 62  isor.  Each subb
1041f 69 74 6d 61 70 20 69 73 20 6e 6f 72 6d 61 6c 69  itmap is normali
10420 7a 65 64 0a 2a 2a 20 74 6f 20 68 6f 6c 64 20 64  zed.** to hold d
10421 65 61 6c 20 77 69 74 68 20 76 61 6c 75 65 73 20  eal with values 
10422 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 69 44  between 1 and iD
10423 69 76 69 73 6f 72 2e 0a 2a 2f 0a 73 74 72 75 63  ivisor..*/.struc
10424 74 20 42 69 74 76 65 63 20 7b 0a 20 20 75 33 32  t Bitvec {.  u32
10425 20 69 53 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20   iSize;      /* 
10426 4d 61 78 69 6d 75 6d 20 62 69 74 20 69 6e 64 65  Maximum bit inde
10427 78 2e 20 20 4d 61 78 20 69 53 69 7a 65 20 69 73  x.  Max iSize is
10428 20 34 2c 32 39 34 2c 39 36 37 2c 32 39 36 2e 20   4,294,967,296. 
10429 2a 2f 0a 20 20 75 33 32 20 6e 53 65 74 3b 20 20  */.  u32 nSet;  
1042a 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1042b 66 20 62 69 74 73 20 74 68 61 74 20 61 72 65 20  f bits that are 
1042c 73 65 74 20 2d 20 6f 6e 6c 79 20 76 61 6c 69 64  set - only valid
1042d 20 66 6f 72 20 61 48 61 73 68 0a 20 20 20 20 20   for aHash.     
1042e 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
1042f 65 6c 65 6d 65 6e 74 2e 20 20 4d 61 78 20 69 73  element.  Max is
10430 20 42 49 54 56 45 43 5f 4e 49 4e 54 2e 20 20 46   BITVEC_NINT.  F
10431 6f 72 20 42 49 54 56 45 43 5f 53 5a 20 6f 66 20  or BITVEC_SZ of 
10432 35 31 32 2c 0a 20 20 20 20 20 20 20 20 20 20 20  512,.           
10433 20 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 77         ** this w
10434 6f 75 6c 64 20 62 65 20 31 32 35 2e 20 2a 2f 0a  ould be 125. */.
10435 20 20 75 33 32 20 69 44 69 76 69 73 6f 72 3b 20    u32 iDivisor; 
10436 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
10437 69 74 73 20 68 61 6e 64 6c 65 64 20 62 79 20 65  its handled by e
10438 61 63 68 20 61 70 53 75 62 5b 5d 20 65 6e 74 72  ach apSub[] entr
10439 79 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  y. */.          
1043a 20 20 20 20 20 20 20 20 2f 2a 20 53 68 6f 75 6c          /* Shoul
1043b 64 20 3e 3d 30 20 66 6f 72 20 61 70 53 75 62 20  d >=0 for apSub 
1043c 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20  element. */.    
1043d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1043e 20 4d 61 78 20 69 44 69 76 69 73 6f 72 20 69 73   Max iDivisor is
1043f 20 6d 61 78 28 75 33 32 29 20 2f 20 42 49 54 56   max(u32) / BITV
10440 45 43 5f 4e 50 54 52 20 2b 20 31 2e 20 20 2a 2f  EC_NPTR + 1.  */
10441 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
10442 20 20 20 2f 2a 20 46 6f 72 20 61 20 42 49 54 56     /* For a BITV
10443 45 43 5f 53 5a 20 6f 66 20 35 31 32 2c 20 74 68  EC_SZ of 512, th
10444 69 73 20 77 6f 75 6c 64 20 62 65 20 33 34 2c 33  is would be 34,3
10445 35 39 2c 37 33 39 2e 20 2a 2f 0a 20 20 75 6e 69  59,739. */.  uni
10446 6f 6e 20 7b 0a 20 20 20 20 42 49 54 56 45 43 5f  on {.    BITVEC_
10447 54 45 4c 45 4d 20 61 42 69 74 6d 61 70 5b 42 49  TELEM aBitmap[BI
10448 54 56 45 43 5f 4e 45 4c 45 4d 5d 3b 20 20 20 20  TVEC_NELEM];    
10449 2f 2a 20 42 69 74 6d 61 70 20 72 65 70 72 65 73  /* Bitmap repres
1044a 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20  entation */.    
1044b 75 33 32 20 61 48 61 73 68 5b 42 49 54 56 45 43  u32 aHash[BITVEC
1044c 5f 4e 49 4e 54 5d 3b 20 20 20 20 20 20 2f 2a 20  _NINT];      /* 
1044d 48 61 73 68 20 74 61 62 6c 65 20 72 65 70 72 65  Hash table repre
1044e 73 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20  sentation */.   
1044f 20 42 69 74 76 65 63 20 2a 61 70 53 75 62 5b 42   Bitvec *apSub[B
10450 49 54 56 45 43 5f 4e 50 54 52 5d 3b 20 20 2f 2a  ITVEC_NPTR];  /*
10451 20 52 65 63 75 72 73 69 76 65 20 72 65 70 72 65   Recursive repre
10452 73 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 7d  sentation */.  }
10453 20 75 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72   u;.};../*.** Cr
10454 65 61 74 65 20 61 20 6e 65 77 20 62 69 74 6d 61  eate a new bitma
10455 70 20 6f 62 6a 65 63 74 20 61 62 6c 65 20 74 6f  p object able to
10456 20 68 61 6e 64 6c 65 20 62 69 74 73 20 62 65 74   handle bits bet
10457 77 65 65 6e 20 30 20 61 6e 64 20 69 53 69 7a 65  ween 0 and iSize
10458 2c 0a 2a 2a 20 69 6e 63 6c 75 73 69 76 65 2e 20  ,.** inclusive. 
10459 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
1045a 72 20 74 6f 20 74 68 65 20 6e 65 77 20 6f 62 6a  r to the new obj
1045b 65 63 74 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c  ect.  Return NUL
1045c 4c 20 69 66 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 20  L if .** malloc 
1045d 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  fails..*/.SQLITE
1045e 5f 50 52 49 56 41 54 45 20 42 69 74 76 65 63 20  _PRIVATE Bitvec 
1045f 2a 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72  *sqlite3BitvecCr
10460 65 61 74 65 28 75 33 32 20 69 53 69 7a 65 29 7b  eate(u32 iSize){
10461 0a 20 20 42 69 74 76 65 63 20 2a 70 3b 0a 20 20  .  Bitvec *p;.  
10462 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 2a  assert( sizeof(*
10463 70 29 3d 3d 42 49 54 56 45 43 5f 53 5a 20 29 3b  p)==BITVEC_SZ );
10464 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61  .  p = sqlite3Ma
10465 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66  llocZero( sizeof
10466 28 2a 70 29 20 29 3b 0a 20 20 69 66 28 20 70 20  (*p) );.  if( p 
10467 29 7b 0a 20 20 20 20 70 2d 3e 69 53 69 7a 65 20  ){.    p->iSize 
10468 3d 20 69 53 69 7a 65 3b 0a 20 20 7d 0a 20 20 72  = iSize;.  }.  r
10469 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
1046a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
1046b 66 20 74 68 65 20 69 2d 74 68 20 62 69 74 20 69  f the i-th bit i
1046c 73 20 73 65 74 2e 20 20 52 65 74 75 72 6e 20 74  s set.  Return t
1046d 72 75 65 20 6f 72 20 66 61 6c 73 65 2e 0a 2a 2a  rue or false..**
1046e 20 49 66 20 70 20 69 73 20 4e 55 4c 4c 20 28 69   If p is NULL (i
1046f 66 20 74 68 65 20 62 69 74 6d 61 70 20 68 61 73  f the bitmap has
10470 20 6e 6f 74 20 62 65 65 6e 20 63 72 65 61 74 65   not been create
10471 64 29 20 6f 72 20 69 66 0a 2a 2a 20 69 20 69 73  d) or if.** i is
10472 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74   out of range, t
10473 68 65 6e 20 72 65 74 75 72 6e 20 66 61 6c 73 65  hen return false
10474 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
10475 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
10476 69 74 76 65 63 54 65 73 74 28 42 69 74 76 65 63  itvecTest(Bitvec
10477 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20 20 69   *p, u32 i){.  i
10478 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
10479 20 30 3b 0a 20 20 69 66 28 20 69 3e 70 2d 3e 69   0;.  if( i>p->i
1047a 53 69 7a 65 20 7c 7c 20 69 3d 3d 30 20 29 20 72  Size || i==0 ) r
1047b 65 74 75 72 6e 20 30 3b 0a 20 20 69 2d 2d 3b 0a  eturn 0;.  i--;.
1047c 20 20 77 68 69 6c 65 28 20 70 2d 3e 69 44 69 76    while( p->iDiv
1047d 69 73 6f 72 20 29 7b 0a 20 20 20 20 75 33 32 20  isor ){.    u32 
1047e 62 69 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 76 69  bin = i/p->iDivi
1047f 73 6f 72 3b 0a 20 20 20 20 69 20 3d 20 69 25 70  sor;.    i = i%p
10480 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20  ->iDivisor;.    
10481 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62  p = p->u.apSub[b
10482 69 6e 5d 3b 0a 20 20 20 20 69 66 20 28 21 70 29  in];.    if (!p)
10483 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20   {.      return 
10484 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  0;.    }.  }.  i
10485 66 28 20 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 54  f( p->iSize<=BIT
10486 56 45 43 5f 4e 42 49 54 20 29 7b 0a 20 20 20 20  VEC_NBIT ){.    
10487 72 65 74 75 72 6e 20 28 70 2d 3e 75 2e 61 42 69  return (p->u.aBi
10488 74 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a  tmap[i/BITVEC_SZ
10489 45 4c 45 4d 5d 20 26 20 28 31 3c 3c 28 69 26 28  ELEM] & (1<<(i&(
1048a 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 2d 31 29  BITVEC_SZELEM-1)
1048b 29 29 29 21 3d 30 3b 0a 20 20 7d 20 65 6c 73 65  )))!=0;.  } else
1048c 7b 0a 20 20 20 20 75 33 32 20 68 20 3d 20 42 49  {.    u32 h = BI
1048d 54 56 45 43 5f 48 41 53 48 28 69 2b 2b 29 3b 0a  TVEC_HASH(i++);.
1048e 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 75 2e      while( p->u.
1048f 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20  aHash[h] ){.    
10490 20 20 69 66 28 20 70 2d 3e 75 2e 61 48 61 73 68    if( p->u.aHash
10491 5b 68 5d 3d 3d 69 20 29 20 72 65 74 75 72 6e 20  [h]==i ) return 
10492 31 3b 0a 20 20 20 20 20 20 68 20 3d 20 28 68 2b  1;.      h = (h+
10493 31 29 20 25 20 42 49 54 56 45 43 5f 4e 49 4e 54  1) % BITVEC_NINT
10494 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
10495 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  rn 0;.  }.}../*.
10496 2a 2a 20 53 65 74 20 74 68 65 20 69 2d 74 68 20  ** Set the i-th 
10497 62 69 74 2e 20 20 52 65 74 75 72 6e 20 30 20 6f  bit.  Return 0 o
10498 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 61 6e  n success and an
10499 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 0a 2a   error code if.*
1049a 2a 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20  * anything goes 
1049b 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  wrong..**.** Thi
1049c 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20  s routine might 
1049d 63 61 75 73 65 20 73 75 62 2d 62 69 74 6d 61 70  cause sub-bitmap
1049e 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65  s to be allocate
1049f 64 2e 20 20 46 61 69 6c 69 6e 67 0a 2a 2a 20 74  d.  Failing.** t
104a0 6f 20 67 65 74 20 74 68 65 20 6d 65 6d 6f 72 79  o get the memory
104a1 20 6e 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20   needed to hold 
104a2 74 68 65 20 73 75 62 2d 62 69 74 6d 61 70 20 69  the sub-bitmap i
104a3 73 20 74 68 65 20 6f 6e 6c 79 0a 2a 2a 20 74 68  s the only.** th
104a4 61 74 20 63 61 6e 20 67 6f 20 77 72 6f 6e 67 20  at can go wrong 
104a5 77 69 74 68 20 61 6e 20 69 6e 73 65 72 74 2c 20  with an insert, 
104a6 61 73 73 75 6d 69 6e 67 20 70 20 61 6e 64 20 69  assuming p and i
104a7 20 61 72 65 20 76 61 6c 69 64 2e 0a 2a 2a 0a 2a   are valid..**.*
104a8 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75  * The calling fu
104a9 6e 63 74 69 6f 6e 20 6d 75 73 74 20 65 6e 73 75  nction must ensu
104aa 72 65 20 74 68 61 74 20 70 20 69 73 20 61 20 76  re that p is a v
104ab 61 6c 69 64 20 42 69 74 76 65 63 20 6f 62 6a 65  alid Bitvec obje
104ac 63 74 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 74  ct.** and that t
104ad 68 65 20 76 61 6c 75 65 20 66 6f 72 20 22 69 22  he value for "i"
104ae 20 69 73 20 77 69 74 68 69 6e 20 72 61 6e 67 65   is within range
104af 20 6f 66 20 74 68 65 20 42 69 74 76 65 63 20 6f   of the Bitvec o
104b0 62 6a 65 63 74 2e 0a 2a 2a 20 4f 74 68 65 72 77  bject..** Otherw
104b1 69 73 65 20 74 68 65 20 62 65 68 61 76 69 6f 72  ise the behavior
104b2 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a   is undefined..*
104b3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
104b4 20 69 6e 74 20 73 71 6c 69 74 65 33 42 69 74 76   int sqlite3Bitv
104b5 65 63 53 65 74 28 42 69 74 76 65 63 20 2a 70 2c  ecSet(Bitvec *p,
104b6 20 75 33 32 20 69 29 7b 0a 20 20 75 33 32 20 68   u32 i){.  u32 h
104b7 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
104b8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
104b9 0a 20 20 61 73 73 65 72 74 28 20 69 3e 30 20 29  .  assert( i>0 )
104ba 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3c 3d 70  ;.  assert( i<=p
104bb 2d 3e 69 53 69 7a 65 20 29 3b 0a 20 20 69 2d 2d  ->iSize );.  i--
104bc 3b 0a 20 20 77 68 69 6c 65 28 28 70 2d 3e 69 53  ;.  while((p->iS
104bd 69 7a 65 20 3e 20 42 49 54 56 45 43 5f 4e 42 49  ize > BITVEC_NBI
104be 54 29 20 26 26 20 70 2d 3e 69 44 69 76 69 73 6f  T) && p->iDiviso
104bf 72 29 20 7b 0a 20 20 20 20 75 33 32 20 62 69 6e  r) {.    u32 bin
104c0 20 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72   = i/p->iDivisor
104c1 3b 0a 20 20 20 20 69 20 3d 20 69 25 70 2d 3e 69  ;.    i = i%p->i
104c2 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 69 66 28  Divisor;.    if(
104c3 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d   p->u.apSub[bin]
104c4 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  ==0 ){.      p->
104c5 75 2e 61 70 53 75 62 5b 62 69 6e 5d 20 3d 20 73  u.apSub[bin] = s
104c6 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61  qlite3BitvecCrea
104c7 74 65 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20  te( p->iDivisor 
104c8 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  );.      if( p->
104c9 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20  u.apSub[bin]==0 
104ca 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
104cb 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20  NOMEM;.    }.   
104cc 20 70 20 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b   p = p->u.apSub[
104cd 62 69 6e 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20  bin];.  }.  if( 
104ce 70 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 43  p->iSize<=BITVEC
104cf 5f 4e 42 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e  _NBIT ){.    p->
104d0 75 2e 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 56  u.aBitmap[i/BITV
104d1 45 43 5f 53 5a 45 4c 45 4d 5d 20 7c 3d 20 31 20  EC_SZELEM] |= 1 
104d2 3c 3c 20 28 69 26 28 42 49 54 56 45 43 5f 53 5a  << (i&(BITVEC_SZ
104d3 45 4c 45 4d 2d 31 29 29 3b 0a 20 20 20 20 72 65  ELEM-1));.    re
104d4 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
104d5 20 20 7d 0a 20 20 68 20 3d 20 42 49 54 56 45 43    }.  h = BITVEC
104d6 5f 48 41 53 48 28 69 2b 2b 29 3b 0a 20 20 2f 2a  _HASH(i++);.  /*
104d7 20 69 66 20 74 68 65 72 65 20 77 61 73 6e 27 74   if there wasn't
104d8 20 61 20 68 61 73 68 20 63 6f 6c 6c 69 73 69 6f   a hash collisio
104d9 6e 2c 20 61 6e 64 20 74 68 69 73 20 64 6f 65 73  n, and this does
104da 6e 27 74 20 2a 2f 0a 20 20 2f 2a 20 63 6f 6d 70  n't */.  /* comp
104db 6c 65 74 65 6c 79 20 66 69 6c 6c 20 74 68 65 20  letely fill the 
104dc 68 61 73 68 2c 20 74 68 65 6e 20 6a 75 73 74 20  hash, then just 
104dd 61 64 64 20 69 74 20 77 69 74 68 6f 75 74 20 2a  add it without *
104de 2f 0a 20 20 2f 2a 20 77 6f 72 72 69 6e 67 20 61  /.  /* worring a
104df 62 6f 75 74 20 73 75 62 2d 64 69 76 69 64 69 6e  bout sub-dividin
104e0 67 20 61 6e 64 20 72 65 2d 68 61 73 68 69 6e 67  g and re-hashing
104e1 2e 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e 75  . */.  if( !p->u
104e2 2e 61 48 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20  .aHash[h] ){.   
104e3 20 69 66 20 28 70 2d 3e 6e 53 65 74 3c 28 42 49   if (p->nSet<(BI
104e4 54 56 45 43 5f 4e 49 4e 54 2d 31 29 29 20 7b 0a  TVEC_NINT-1)) {.
104e5 20 20 20 20 20 20 67 6f 74 6f 20 62 69 74 76 65        goto bitve
104e6 63 5f 73 65 74 5f 65 6e 64 3b 0a 20 20 20 20 7d  c_set_end;.    }
104e7 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 67 6f   else {.      go
104e8 74 6f 20 62 69 74 76 65 63 5f 73 65 74 5f 72 65  to bitvec_set_re
104e9 68 61 73 68 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  hash;.    }.  }.
104ea 20 20 2f 2a 20 74 68 65 72 65 20 77 61 73 20 61    /* there was a
104eb 20 63 6f 6c 6c 69 73 69 6f 6e 2c 20 63 68 65 63   collision, chec
104ec 6b 20 74 6f 20 73 65 65 20 69 66 20 69 74 27 73  k to see if it's
104ed 20 61 6c 72 65 61 64 79 20 2a 2f 0a 20 20 2f 2a   already */.  /*
104ee 20 69 6e 20 68 61 73 68 2c 20 69 66 20 6e 6f 74   in hash, if not
104ef 2c 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61 20  , try to find a 
104f0 73 70 6f 74 20 66 6f 72 20 69 74 20 2a 2f 0a 20  spot for it */. 
104f1 20 64 6f 20 7b 0a 20 20 20 20 69 66 28 20 70 2d   do {.    if( p-
104f2 3e 75 2e 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29  >u.aHash[h]==i )
104f3 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
104f4 4b 3b 0a 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20  K;.    h++;.    
104f5 69 66 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49  if( h>=BITVEC_NI
104f6 4e 54 20 29 20 68 20 3d 20 30 3b 0a 20 20 7d 20  NT ) h = 0;.  } 
104f7 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73  while( p->u.aHas
104f8 68 5b 68 5d 20 29 3b 0a 20 20 2f 2a 20 77 65 20  h[h] );.  /* we 
104f9 64 69 64 6e 27 74 20 66 69 6e 64 20 69 74 20 69  didn't find it i
104fa 6e 20 74 68 65 20 68 61 73 68 2e 20 20 68 20 70  n the hash.  h p
104fb 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72  oints to the fir
104fc 73 74 20 2a 2f 0a 20 20 2f 2a 20 61 76 61 69 6c  st */.  /* avail
104fd 61 62 6c 65 20 66 72 65 65 20 73 70 6f 74 2e 20  able free spot. 
104fe 63 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  check to see if 
104ff 74 68 69 73 20 69 73 20 67 6f 69 6e 67 20 74 6f  this is going to
10500 20 2a 2f 0a 20 20 2f 2a 20 6d 61 6b 65 20 6f 75   */.  /* make ou
10501 72 20 68 61 73 68 20 74 6f 6f 20 22 66 75 6c 6c  r hash too "full
10502 22 2e 20 20 2a 2f 0a 62 69 74 76 65 63 5f 73 65  ".  */.bitvec_se
10503 74 5f 72 65 68 61 73 68 3a 0a 20 20 69 66 28 20  t_rehash:.  if( 
10504 70 2d 3e 6e 53 65 74 3e 3d 42 49 54 56 45 43 5f  p->nSet>=BITVEC_
10505 4d 58 48 41 53 48 20 29 7b 0a 20 20 20 20 75 6e  MXHASH ){.    un
10506 73 69 67 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20  signed int j;.  
10507 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 75 33    int rc;.    u3
10508 32 20 2a 61 69 56 61 6c 75 65 73 20 3d 20 73 71  2 *aiValues = sq
10509 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52  lite3StackAllocR
1050a 61 77 28 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e  aw(0, sizeof(p->
1050b 75 2e 61 48 61 73 68 29 29 3b 0a 20 20 20 20 69  u.aHash));.    i
1050c 66 28 20 61 69 56 61 6c 75 65 73 3d 3d 30 20 29  f( aiValues==0 )
1050d 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
1050e 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
1050f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65   }else{.      me
10510 6d 63 70 79 28 61 69 56 61 6c 75 65 73 2c 20 70  mcpy(aiValues, p
10511 2d 3e 75 2e 61 48 61 73 68 2c 20 73 69 7a 65 6f  ->u.aHash, sizeo
10512 66 28 70 2d 3e 75 2e 61 48 61 73 68 29 29 3b 0a  f(p->u.aHash));.
10513 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 2d 3e        memset(p->
10514 75 2e 61 70 53 75 62 2c 20 30 2c 20 73 69 7a 65  u.apSub, 0, size
10515 6f 66 28 70 2d 3e 75 2e 61 70 53 75 62 29 29 3b  of(p->u.apSub));
10516 0a 20 20 20 20 20 20 70 2d 3e 69 44 69 76 69 73  .      p->iDivis
10517 6f 72 20 3d 20 28 70 2d 3e 69 53 69 7a 65 20 2b  or = (p->iSize +
10518 20 42 49 54 56 45 43 5f 4e 50 54 52 20 2d 20 31   BITVEC_NPTR - 1
10519 29 2f 42 49 54 56 45 43 5f 4e 50 54 52 3b 0a 20  )/BITVEC_NPTR;. 
1051a 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1051b 33 42 69 74 76 65 63 53 65 74 28 70 2c 20 69 29  3BitvecSet(p, i)
1051c 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b  ;.      for(j=0;
1051d 20 6a 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20   j<BITVEC_NINT; 
1051e 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66  j++){.        if
1051f 28 20 61 69 56 61 6c 75 65 73 5b 6a 5d 20 29 20  ( aiValues[j] ) 
10520 72 63 20 7c 3d 20 73 71 6c 69 74 65 33 42 69 74  rc |= sqlite3Bit
10521 76 65 63 53 65 74 28 70 2c 20 61 69 56 61 6c 75  vecSet(p, aiValu
10522 65 73 5b 6a 5d 29 3b 0a 20 20 20 20 20 20 7d 0a  es[j]);.      }.
10523 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61        sqlite3Sta
10524 63 6b 46 72 65 65 28 30 2c 20 61 69 56 61 6c 75  ckFree(0, aiValu
10525 65 73 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  es);.      retur
10526 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  n rc;.    }.  }.
10527 62 69 74 76 65 63 5f 73 65 74 5f 65 6e 64 3a 0a  bitvec_set_end:.
10528 20 20 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 70    p->nSet++;.  p
10529 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 69  ->u.aHash[h] = i
1052a 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1052b 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
1052c 6c 65 61 72 20 74 68 65 20 69 2d 74 68 20 62 69  lear the i-th bi
1052d 74 2e 0a 2a 2a 0a 2a 2a 20 70 42 75 66 20 6d 75  t..**.** pBuf mu
1052e 73 74 20 62 65 20 61 20 70 6f 69 6e 74 65 72 20  st be a pointer 
1052f 74 6f 20 61 74 20 6c 65 61 73 74 20 42 49 54 56  to at least BITV
10530 45 43 5f 53 5a 20 62 79 74 65 73 20 6f 66 20 74  EC_SZ bytes of t
10531 65 6d 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65  emporary storage
10532 0a 2a 2a 20 74 68 61 74 20 42 69 74 76 65 63 43  .** that BitvecC
10533 6c 65 61 72 20 63 61 6e 20 75 73 65 20 74 6f 20  lear can use to 
10534 72 65 62 75 69 6c 74 20 69 74 73 20 68 61 73 68  rebuilt its hash
10535 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54   table..*/.SQLIT
10536 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
10537 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65 61  qlite3BitvecClea
10538 72 28 42 69 74 76 65 63 20 2a 70 2c 20 75 33 32  r(Bitvec *p, u32
10539 20 69 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b   i, void *pBuf){
1053a 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
1053b 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  turn;.  assert( 
1053c 69 3e 30 20 29 3b 0a 20 20 69 2d 2d 3b 0a 20 20  i>0 );.  i--;.  
1053d 77 68 69 6c 65 28 20 70 2d 3e 69 44 69 76 69 73  while( p->iDivis
1053e 6f 72 20 29 7b 0a 20 20 20 20 75 33 32 20 62 69  or ){.    u32 bi
1053f 6e 20 3d 20 69 2f 70 2d 3e 69 44 69 76 69 73 6f  n = i/p->iDiviso
10540 72 3b 0a 20 20 20 20 69 20 3d 20 69 25 70 2d 3e  r;.    i = i%p->
10541 69 44 69 76 69 73 6f 72 3b 0a 20 20 20 20 70 20  iDivisor;.    p 
10542 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e  = p->u.apSub[bin
10543 5d 3b 0a 20 20 20 20 69 66 20 28 21 70 29 20 7b  ];.    if (!p) {
10544 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
10545 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70     }.  }.  if( p
10546 2d 3e 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f  ->iSize<=BITVEC_
10547 4e 42 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75  NBIT ){.    p->u
10548 2e 61 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45  .aBitmap[i/BITVE
10549 43 5f 53 5a 45 4c 45 4d 5d 20 26 3d 20 7e 28 31  C_SZELEM] &= ~(1
1054a 20 3c 3c 20 28 69 26 28 42 49 54 56 45 43 5f 53   << (i&(BITVEC_S
1054b 5a 45 4c 45 4d 2d 31 29 29 29 3b 0a 20 20 7d 65  ZELEM-1)));.  }e
1054c 6c 73 65 7b 0a 20 20 20 20 75 6e 73 69 67 6e 65  lse{.    unsigne
1054d 64 20 69 6e 74 20 6a 3b 0a 20 20 20 20 75 33 32  d int j;.    u32
1054e 20 2a 61 69 56 61 6c 75 65 73 20 3d 20 70 42 75   *aiValues = pBu
1054f 66 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 69  f;.    memcpy(ai
10550 56 61 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61  Values, p->u.aHa
10551 73 68 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e  sh, sizeof(p->u.
10552 61 48 61 73 68 29 29 3b 0a 20 20 20 20 6d 65 6d  aHash));.    mem
10553 73 65 74 28 70 2d 3e 75 2e 61 48 61 73 68 2c 20  set(p->u.aHash, 
10554 30 2c 20 73 69 7a 65 6f 66 28 70 2d 3e 75 2e 61  0, sizeof(p->u.a
10555 48 61 73 68 29 29 3b 0a 20 20 20 20 70 2d 3e 6e  Hash));.    p->n
10556 53 65 74 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72  Set = 0;.    for
10557 28 6a 3d 30 3b 20 6a 3c 42 49 54 56 45 43 5f 4e  (j=0; j<BITVEC_N
10558 49 4e 54 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  INT; j++){.     
10559 20 69 66 28 20 61 69 56 61 6c 75 65 73 5b 6a 5d   if( aiValues[j]
1055a 20 26 26 20 61 69 56 61 6c 75 65 73 5b 6a 5d 21   && aiValues[j]!
1055b 3d 28 69 2b 31 29 20 29 7b 0a 20 20 20 20 20 20  =(i+1) ){.      
1055c 20 20 75 33 32 20 68 20 3d 20 42 49 54 56 45 43    u32 h = BITVEC
1055d 5f 48 41 53 48 28 61 69 56 61 6c 75 65 73 5b 6a  _HASH(aiValues[j
1055e 5d 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 70 2d  ]-1);.        p-
1055f 3e 6e 53 65 74 2b 2b 3b 0a 20 20 20 20 20 20 20  >nSet++;.       
10560 20 77 68 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61   while( p->u.aHa
10561 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 20 20 20  sh[h] ){.       
10562 20 20 20 68 2b 2b 3b 0a 20 20 20 20 20 20 20 20     h++;.        
10563 20 20 69 66 28 20 68 3e 3d 42 49 54 56 45 43 5f    if( h>=BITVEC_
10564 4e 49 4e 54 20 29 20 68 20 3d 20 30 3b 0a 20 20  NINT ) h = 0;.  
10565 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
10566 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20  p->u.aHash[h] = 
10567 61 69 56 61 6c 75 65 73 5b 6a 5d 3b 0a 20 20 20  aiValues[j];.   
10568 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d     }.    }.  }.}
10569 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20  ../*.** Destroy 
1056a 61 20 62 69 74 6d 61 70 20 6f 62 6a 65 63 74 2e  a bitmap object.
1056b 20 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65    Reclaim all me
1056c 6d 6f 72 79 20 75 73 65 64 2e 0a 2a 2f 0a 53 51  mory used..*/.SQ
1056d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1056e 64 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44  d sqlite3BitvecD
1056f 65 73 74 72 6f 79 28 42 69 74 76 65 63 20 2a 70  estroy(Bitvec *p
10570 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  ){.  if( p==0 ) 
10571 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d  return;.  if( p-
10572 3e 69 44 69 76 69 73 6f 72 20 29 7b 0a 20 20 20  >iDivisor ){.   
10573 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b   unsigned int i;
10574 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
10575 42 49 54 56 45 43 5f 4e 50 54 52 3b 20 69 2b 2b  BITVEC_NPTR; i++
10576 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
10577 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 2d  BitvecDestroy(p-
10578 3e 75 2e 61 70 53 75 62 5b 69 5d 29 3b 0a 20 20  >u.apSub[i]);.  
10579 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65    }.  }.  sqlite
1057a 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a  3_free(p);.}../*
1057b 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76  .** Return the v
1057c 61 6c 75 65 20 6f 66 20 74 68 65 20 69 53 69 7a  alue of the iSiz
1057d 65 20 70 61 72 61 6d 65 74 65 72 20 73 70 65 63  e parameter spec
1057e 69 66 69 65 64 20 77 68 65 6e 20 42 69 74 76 65  ified when Bitve
1057f 63 20 2a 70 0a 2a 2a 20 77 61 73 20 63 72 65 61  c *p.** was crea
10580 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
10581 52 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74  RIVATE u32 sqlit
10582 65 33 42 69 74 76 65 63 53 69 7a 65 28 42 69 74  e3BitvecSize(Bit
10583 76 65 63 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  vec *p){.  retur
10584 6e 20 70 2d 3e 69 53 69 7a 65 3b 0a 7d 0a 0a 23  n p->iSize;.}..#
10585 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
10586 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 0a  IT_BUILTIN_TEST.
10587 2f 2a 0a 2a 2a 20 4c 65 74 20 56 5b 5d 20 62 65  /*.** Let V[] be
10588 20 61 6e 20 61 72 72 61 79 20 6f 66 20 75 6e 73   an array of uns
10589 69 67 6e 65 64 20 63 68 61 72 61 63 74 65 72 73  igned characters
1058a 20 73 75 66 66 69 63 69 65 6e 74 20 74 6f 20 68   sufficient to h
1058b 6f 6c 64 0a 2a 2a 20 75 70 20 74 6f 20 4e 20 62  old.** up to N b
1058c 69 74 73 2e 20 20 4c 65 74 20 49 20 62 65 20 61  its.  Let I be a
1058d 6e 20 69 6e 74 65 67 65 72 20 62 65 74 77 65 65  n integer betwee
1058e 6e 20 30 20 61 6e 64 20 4e 2e 20 20 30 3c 3d 49  n 0 and N.  0<=I
1058f 3c 4e 2e 0a 2a 2a 20 54 68 65 6e 20 74 68 65 20  <N..** Then the 
10590 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73  following macros
10591 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
10592 73 65 74 2c 20 63 6c 65 61 72 2c 20 6f 72 20 74  set, clear, or t
10593 65 73 74 0a 2a 2a 20 69 6e 64 69 76 69 64 75 61  est.** individua
10594 6c 20 62 69 74 73 20 77 69 74 68 69 6e 20 56 2e  l bits within V.
10595 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 45 54 42  .*/.#define SETB
10596 49 54 28 56 2c 49 29 20 20 20 20 20 20 56 5b 49  IT(V,I)      V[I
10597 3e 3e 33 5d 20 7c 3d 20 28 31 3c 3c 28 49 26 37  >>3] |= (1<<(I&7
10598 29 29 0a 23 64 65 66 69 6e 65 20 43 4c 45 41 52  )).#define CLEAR
10599 42 49 54 28 56 2c 49 29 20 20 20 20 56 5b 49 3e  BIT(V,I)    V[I>
1059a 3e 33 5d 20 26 3d 20 7e 28 31 3c 3c 28 49 26 37  >3] &= ~(1<<(I&7
1059b 29 29 0a 23 64 65 66 69 6e 65 20 54 45 53 54 42  )).#define TESTB
1059c 49 54 28 56 2c 49 29 20 20 20 20 20 28 56 5b 49  IT(V,I)     (V[I
1059d 3e 3e 33 5d 26 28 31 3c 3c 28 49 26 37 29 29 29  >>3]&(1<<(I&7)))
1059e 21 3d 30 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  !=0../*.** This 
1059f 72 6f 75 74 69 6e 65 20 72 75 6e 73 20 61 6e 20  routine runs an 
105a0 65 78 74 65 6e 73 69 76 65 20 74 65 73 74 20 6f  extensive test o
105a1 66 20 74 68 65 20 42 69 74 76 65 63 20 63 6f 64  f the Bitvec cod
105a2 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70  e..**.** The inp
105a3 75 74 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f  ut is an array o
105a4 66 20 69 6e 74 65 67 65 72 73 20 74 68 61 74 20  f integers that 
105a5 61 63 74 73 20 61 73 20 61 20 70 72 6f 67 72 61  acts as a progra
105a6 6d 0a 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65  m.** to test the
105a7 20 42 69 74 76 65 63 2e 20 20 54 68 65 20 69 6e   Bitvec.  The in
105a8 74 65 67 65 72 73 20 61 72 65 20 6f 70 63 6f 64  tegers are opcod
105a9 65 73 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a 20 62  es followed.** b
105aa 79 20 30 2c 20 31 2c 20 6f 72 20 33 20 6f 70 65  y 0, 1, or 3 ope
105ab 72 61 6e 64 73 2c 20 64 65 70 65 6e 64 69 6e 67  rands, depending
105ac 20 6f 6e 20 74 68 65 20 6f 70 63 6f 64 65 2e 20   on the opcode. 
105ad 20 41 6e 6f 74 68 65 72 0a 2a 2a 20 6f 70 63 6f   Another.** opco
105ae 64 65 20 66 6f 6c 6c 6f 77 73 20 69 6d 6d 65 64  de follows immed
105af 69 61 74 65 6c 79 20 61 66 74 65 72 20 74 68 65  iately after the
105b0 20 6c 61 73 74 20 6f 70 65 72 61 6e 64 2e 0a 2a   last operand..*
105b1 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 36  *.** There are 6
105b2 20 6f 70 63 6f 64 65 73 20 6e 75 6d 62 65 72 65   opcodes numbere
105b3 64 20 66 72 6f 6d 20 30 20 74 68 72 6f 75 67 68  d from 0 through
105b4 20 35 2e 20 20 30 20 69 73 20 74 68 65 0a 2a 2a   5.  0 is the.**
105b5 20 22 68 61 6c 74 22 20 6f 70 63 6f 64 65 20 61   "halt" opcode a
105b6 6e 64 20 63 61 75 73 65 73 20 74 68 65 20 74 65  nd causes the te
105b7 73 74 20 74 6f 20 65 6e 64 2e 0a 2a 2a 0a 2a 2a  st to end..**.**
105b8 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 48      0          H
105b9 61 6c 74 20 61 6e 64 20 72 65 74 75 72 6e 20 74  alt and return t
105ba 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72  he number of err
105bb 6f 72 73 0a 2a 2a 20 20 20 20 31 20 4e 20 53 20  ors.**    1 N S 
105bc 58 20 20 20 20 53 65 74 20 4e 20 62 69 74 73 20  X    Set N bits 
105bd 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53  beginning with S
105be 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 69 6e   and incrementin
105bf 67 20 62 79 20 58 0a 2a 2a 20 20 20 20 32 20 4e  g by X.**    2 N
105c0 20 53 20 58 20 20 20 20 43 6c 65 61 72 20 4e 20   S X    Clear N 
105c1 62 69 74 73 20 62 65 67 69 6e 6e 69 6e 67 20 77  bits beginning w
105c2 69 74 68 20 53 20 61 6e 64 20 69 6e 63 72 65 6d  ith S and increm
105c3 65 6e 74 69 6e 67 20 62 79 20 58 0a 2a 2a 20 20  enting by X.**  
105c4 20 20 33 20 4e 20 20 20 20 20 20 20 20 53 65 74    3 N        Set
105c5 20 4e 20 72 61 6e 64 6f 6d 6c 79 20 63 68 6f 73   N randomly chos
105c6 65 6e 20 62 69 74 73 0a 2a 2a 20 20 20 20 34 20  en bits.**    4 
105c7 4e 20 20 20 20 20 20 20 20 43 6c 65 61 72 20 4e  N        Clear N
105c8 20 72 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e   randomly chosen
105c9 20 62 69 74 73 0a 2a 2a 20 20 20 20 35 20 4e 20   bits.**    5 N 
105ca 53 20 58 20 20 20 20 53 65 74 20 4e 20 62 69 74  S X    Set N bit
105cb 73 20 66 72 6f 6d 20 53 20 69 6e 63 72 65 6d 65  s from S increme
105cc 6e 74 20 58 20 69 6e 20 61 72 72 61 79 20 6f 6e  nt X in array on
105cd 6c 79 2c 20 6e 6f 74 20 69 6e 20 62 69 74 76 65  ly, not in bitve
105ce 63 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 70 63 6f  c.**.** The opco
105cf 64 65 73 20 31 20 74 68 72 6f 75 67 68 20 34 20  des 1 through 4 
105d0 70 65 72 66 6f 72 6d 20 73 65 74 20 61 6e 64 20  perform set and 
105d1 63 6c 65 61 72 20 6f 70 65 72 61 74 69 6f 6e 73  clear operations
105d2 20 61 72 65 20 70 65 72 66 6f 72 6d 65 64 0a 2a   are performed.*
105d3 2a 20 6f 6e 20 62 6f 74 68 20 61 20 42 69 74 76  * on both a Bitv
105d4 65 63 20 6f 62 6a 65 63 74 20 61 6e 64 20 6f 6e  ec object and on
105d5 20 61 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20   a linear array 
105d6 6f 66 20 62 69 74 73 20 6f 62 74 61 69 6e 65 64  of bits obtained
105d7 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a   from malloc..**
105d8 20 4f 70 63 6f 64 65 20 35 20 77 6f 72 6b 73 20   Opcode 5 works 
105d9 6f 6e 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72  on the linear ar
105da 72 61 79 20 6f 6e 6c 79 2c 20 6e 6f 74 20 6f 6e  ray only, not on
105db 20 74 68 65 20 42 69 74 76 65 63 2e 0a 2a 2a 20   the Bitvec..** 
105dc 4f 70 63 6f 64 65 20 35 20 69 73 20 75 73 65 64  Opcode 5 is used
105dd 20 74 6f 20 64 65 6c 69 62 65 72 61 74 65 6c 79   to deliberately
105de 20 69 6e 64 75 63 65 20 61 20 66 61 75 6c 74 20   induce a fault 
105df 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63  in order to.** c
105e0 6f 6e 66 69 72 6d 20 74 68 61 74 20 65 72 72 6f  onfirm that erro
105e1 72 20 64 65 74 65 63 74 69 6f 6e 20 77 6f 72 6b  r detection work
105e2 73 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20  s..**.** At the 
105e3 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20 74 68  conclusion of th
105e4 65 20 74 65 73 74 20 74 68 65 20 6c 69 6e 65 61  e test the linea
105e5 72 20 61 72 72 61 79 20 69 73 20 63 6f 6d 70 61  r array is compa
105e6 72 65 64 0a 2a 2a 20 61 67 61 69 6e 73 74 20 74  red.** against t
105e7 68 65 20 42 69 74 76 65 63 20 6f 62 6a 65 63 74  he Bitvec object
105e8 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20  .  If there are 
105e9 61 6e 79 20 64 69 66 66 65 72 65 6e 63 65 73 2c  any differences,
105ea 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20  .** an error is 
105eb 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68  returned.  If th
105ec 65 79 20 61 72 65 20 74 68 65 20 73 61 6d 65 2c  ey are the same,
105ed 20 7a 65 72 6f 20 69 73 20 72 65 74 75 72 6e 65   zero is returne
105ee 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d 65  d..**.** If a me
105ef 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
105f0 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65  error occurs, re
105f1 74 75 72 6e 20 2d 31 2e 0a 2a 2f 0a 53 51 4c 49  turn -1..*/.SQLI
105f2 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
105f3 71 6c 69 74 65 33 42 69 74 76 65 63 42 75 69 6c  qlite3BitvecBuil
105f4 74 69 6e 54 65 73 74 28 69 6e 74 20 73 7a 2c 20  tinTest(int sz, 
105f5 69 6e 74 20 2a 61 4f 70 29 7b 0a 20 20 42 69 74  int *aOp){.  Bit
105f6 76 65 63 20 2a 70 42 69 74 76 65 63 20 3d 20 30  vec *pBitvec = 0
105f7 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ;.  unsigned cha
105f8 72 20 2a 70 56 20 3d 20 30 3b 0a 20 20 69 6e 74  r *pV = 0;.  int
105f9 20 72 63 20 3d 20 2d 31 3b 0a 20 20 69 6e 74 20   rc = -1;.  int 
105fa 69 2c 20 6e 78 2c 20 70 63 2c 20 6f 70 3b 0a 20  i, nx, pc, op;. 
105fb 20 76 6f 69 64 20 2a 70 54 6d 70 53 70 61 63 65   void *pTmpSpace
105fc 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  ;..  /* Allocate
105fd 20 74 68 65 20 42 69 74 76 65 63 20 74 6f 20 62   the Bitvec to b
105fe 65 20 74 65 73 74 65 64 20 61 6e 64 20 61 20 6c  e tested and a l
105ff 69 6e 65 61 72 20 61 72 72 61 79 20 6f 66 0a 20  inear array of. 
10600 20 2a 2a 20 62 69 74 73 20 74 6f 20 61 63 74 20   ** bits to act 
10601 61 73 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  as the reference
10602 20 2a 2f 0a 20 20 70 42 69 74 76 65 63 20 3d 20   */.  pBitvec = 
10603 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65  sqlite3BitvecCre
10604 61 74 65 28 20 73 7a 20 29 3b 0a 20 20 70 56 20  ate( sz );.  pV 
10605 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
10606 28 20 28 73 7a 2b 37 29 2f 38 20 2b 20 31 20 29  ( (sz+7)/8 + 1 )
10607 3b 0a 20 20 70 54 6d 70 53 70 61 63 65 20 3d 20  ;.  pTmpSpace = 
10608 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 42  sqlite3_malloc(B
10609 49 54 56 45 43 5f 53 5a 29 3b 0a 20 20 69 66 28  ITVEC_SZ);.  if(
1060a 20 70 42 69 74 76 65 63 3d 3d 30 20 7c 7c 20 70   pBitvec==0 || p
1060b 56 3d 3d 30 20 7c 7c 20 70 54 6d 70 53 70 61 63  V==0 || pTmpSpac
1060c 65 3d 3d 30 20 20 29 20 67 6f 74 6f 20 62 69 74  e==0  ) goto bit
1060d 76 65 63 5f 65 6e 64 3b 0a 20 20 6d 65 6d 73 65  vec_end;.  memse
1060e 74 28 70 56 2c 20 30 2c 20 28 73 7a 2b 37 29 2f  t(pV, 0, (sz+7)/
1060f 38 20 2b 20 31 29 3b 0a 0a 20 20 2f 2a 20 4e 55  8 + 1);..  /* NU
10610 4c 4c 20 70 42 69 74 76 65 63 20 74 65 73 74 73  LL pBitvec tests
10611 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 69 74   */.  sqlite3Bit
10612 76 65 63 53 65 74 28 30 2c 20 31 29 3b 0a 20 20  vecSet(0, 1);.  
10613 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65  sqlite3BitvecCle
10614 61 72 28 30 2c 20 31 2c 20 70 54 6d 70 53 70 61  ar(0, 1, pTmpSpa
10615 63 65 29 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20 74  ce);..  /* Run t
10616 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20  he program */.  
10617 70 63 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28  pc = 0;.  while(
10618 20 28 6f 70 20 3d 20 61 4f 70 5b 70 63 5d 29 21   (op = aOp[pc])!
10619 3d 30 20 29 7b 0a 20 20 20 20 73 77 69 74 63 68  =0 ){.    switch
1061a 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 61  ( op ){.      ca
1061b 73 65 20 31 3a 0a 20 20 20 20 20 20 63 61 73 65  se 1:.      case
1061c 20 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20 35   2:.      case 5
1061d 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d  : {.        nx =
1061e 20 34 3b 0a 20 20 20 20 20 20 20 20 69 20 3d 20   4;.        i = 
1061f 61 4f 70 5b 70 63 2b 32 5d 20 2d 20 31 3b 0a 20  aOp[pc+2] - 1;. 
10620 20 20 20 20 20 20 20 61 4f 70 5b 70 63 2b 32 5d         aOp[pc+2]
10621 20 2b 3d 20 61 4f 70 5b 70 63 2b 33 5d 3b 0a 20   += aOp[pc+3];. 
10622 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
10623 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
10624 20 33 3a 0a 20 20 20 20 20 20 63 61 73 65 20 34   3:.      case 4
10625 3a 20 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74  : .      default
10626 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 20 3d  : {.        nx =
10627 20 32 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   2;.        sqli
10628 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73  te3_randomness(s
10629 69 7a 65 6f 66 28 69 29 2c 20 26 69 29 3b 0a 20  izeof(i), &i);. 
1062a 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1062b 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1062c 69 66 28 20 28 2d 2d 61 4f 70 5b 70 63 2b 31 5d  if( (--aOp[pc+1]
1062d 29 20 3e 20 30 20 29 20 6e 78 20 3d 20 30 3b 0a  ) > 0 ) nx = 0;.
1062e 20 20 20 20 70 63 20 2b 3d 20 6e 78 3b 0a 20 20      pc += nx;.  
1062f 20 20 69 20 3d 20 28 69 20 26 20 30 78 37 66 66    i = (i & 0x7ff
10630 66 66 66 66 66 29 25 73 7a 3b 0a 20 20 20 20 69  fffff)%sz;.    i
10631 66 28 20 28 6f 70 20 26 20 31 29 21 3d 30 20 29  f( (op & 1)!=0 )
10632 7b 0a 20 20 20 20 20 20 53 45 54 42 49 54 28 70  {.      SETBIT(p
10633 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 20  V, (i+1));.     
10634 20 69 66 28 20 6f 70 21 3d 35 20 29 7b 0a 20 20   if( op!=5 ){.  
10635 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
10636 33 42 69 74 76 65 63 53 65 74 28 70 42 69 74 76  3BitvecSet(pBitv
10637 65 63 2c 20 69 2b 31 29 20 29 20 67 6f 74 6f 20  ec, i+1) ) goto 
10638 62 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20 20 20  bitvec_end;.    
10639 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
1063a 20 20 20 20 20 43 4c 45 41 52 42 49 54 28 70 56       CLEARBIT(pV
1063b 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 20 20  , (i+1));.      
1063c 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c 65  sqlite3BitvecCle
1063d 61 72 28 70 42 69 74 76 65 63 2c 20 69 2b 31 2c  ar(pBitvec, i+1,
1063e 20 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20   pTmpSpace);.   
1063f 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73   }.  }..  /* Tes
10640 74 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  t to make sure t
10641 68 65 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20  he linear array 
10642 65 78 61 63 74 6c 79 20 6d 61 74 63 68 65 73 20  exactly matches 
10643 74 68 65 0a 20 20 2a 2a 20 42 69 74 76 65 63 20  the.  ** Bitvec 
10644 6f 62 6a 65 63 74 2e 20 20 53 74 61 72 74 20 77  object.  Start w
10645 69 74 68 20 74 68 65 20 61 73 73 75 6d 70 74 69  ith the assumpti
10646 6f 6e 20 74 68 61 74 20 74 68 65 79 20 64 6f 0a  on that they do.
10647 20 20 2a 2a 20 6d 61 74 63 68 20 28 72 63 3d 3d    ** match (rc==
10648 30 29 2e 20 20 43 68 61 6e 67 65 20 72 63 20 74  0).  Change rc t
10649 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20  o non-zero if a 
1064a 64 69 73 63 72 65 70 61 6e 63 79 0a 20 20 2a 2a  discrepancy.  **
1064b 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f 0a   is found..  */.
1064c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 69    rc = sqlite3Bi
1064d 74 76 65 63 54 65 73 74 28 30 2c 30 29 20 2b 20  tvecTest(0,0) + 
1064e 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73  sqlite3BitvecTes
1064f 74 28 70 42 69 74 76 65 63 2c 20 73 7a 2b 31 29  t(pBitvec, sz+1)
10650 0a 20 20 20 20 20 20 20 20 20 20 2b 20 73 71 6c  .          + sql
10651 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70  ite3BitvecTest(p
10652 42 69 74 76 65 63 2c 20 30 29 0a 20 20 20 20 20  Bitvec, 0).     
10653 20 20 20 20 20 2b 20 28 73 71 6c 69 74 65 33 42       + (sqlite3B
10654 69 74 76 65 63 53 69 7a 65 28 70 42 69 74 76 65  itvecSize(pBitve
10655 63 29 20 2d 20 73 7a 29 3b 0a 20 20 66 6f 72 28  c) - sz);.  for(
10656 69 3d 31 3b 20 69 3c 3d 73 7a 3b 20 69 2b 2b 29  i=1; i<=sz; i++)
10657 7b 0a 20 20 20 20 69 66 28 20 20 28 54 45 53 54  {.    if(  (TEST
10658 42 49 54 28 70 56 2c 69 29 29 21 3d 73 71 6c 69  BIT(pV,i))!=sqli
10659 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70 42  te3BitvecTest(pB
1065a 69 74 76 65 63 2c 69 29 20 29 7b 0a 20 20 20 20  itvec,i) ){.    
1065b 20 20 72 63 20 3d 20 69 3b 0a 20 20 20 20 20 20    rc = i;.      
1065c 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
1065d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6c 6c 6f  ..  /* Free allo
1065e 63 61 74 65 64 20 73 74 72 75 63 74 75 72 65 20  cated structure 
1065f 2a 2f 0a 62 69 74 76 65 63 5f 65 6e 64 3a 0a 20  */.bitvec_end:. 
10660 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54   sqlite3_free(pT
10661 6d 70 53 70 61 63 65 29 3b 0a 20 20 73 71 6c 69  mpSpace);.  sqli
10662 74 65 33 5f 66 72 65 65 28 70 56 29 3b 0a 20 20  te3_free(pV);.  
10663 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65 73  sqlite3BitvecDes
10664 74 72 6f 79 28 70 42 69 74 76 65 63 29 3b 0a 20  troy(pBitvec);. 
10665 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65   return rc;.}.#e
10666 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
10667 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54  MIT_BUILTIN_TEST
10668 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
10669 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 69 74 76  **** End of bitv
1066a 65 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ec.c ***********
1066b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1066c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1066d 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
1066e 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
1066f 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  pcache.c *******
10670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10671 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10672 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20  ***/./*.** 2008 
10673 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20  August 05.**.** 
10674 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
10675 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
10676 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
10677 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
10678 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
10679 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
1067a 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
1067b 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
1067c 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
1067d 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
1067e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
1067f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
10680 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
10681 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
10682 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
10683 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
10684 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
10685 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10686 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10687 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10688 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10689 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
1068a 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73   file implements
1068b 20 74 68 61 74 20 70 61 67 65 20 63 61 63 68 65   that page cache
1068c 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64  ..**.** @(#) $Id
1068d 3a 20 70 63 61 63 68 65 2e 63 2c 76 20 31 2e 34  : pcache.c,v 1.4
1068e 37 20 32 30 30 39 2f 30 37 2f 32 35 20 31 31 3a  7 2009/07/25 11:
1068f 34 36 3a 34 39 20 64 61 6e 69 65 6c 6b 31 39 37  46:49 danielk197
10690 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a  7 Exp $.*/../*.*
10691 2a 20 41 20 63 6f 6d 70 6c 65 74 65 20 70 61 67  * A complete pag
10692 65 20 63 61 63 68 65 20 69 73 20 61 6e 20 69 6e  e cache is an in
10693 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
10694 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72  tructure..*/.str
10695 75 63 74 20 50 43 61 63 68 65 20 7b 0a 20 20 50  uct PCache {.  P
10696 67 48 64 72 20 2a 70 44 69 72 74 79 2c 20 2a 70  gHdr *pDirty, *p
10697 44 69 72 74 79 54 61 69 6c 3b 20 20 20 20 20 20  DirtyTail;      
10698 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69     /* List of di
10699 72 74 79 20 70 61 67 65 73 20 69 6e 20 4c 52 55  rty pages in LRU
1069a 20 6f 72 64 65 72 20 2a 2f 0a 20 20 50 67 48 64   order */.  PgHd
1069b 72 20 2a 70 53 79 6e 63 65 64 3b 20 20 20 20 20  r *pSynced;     
1069c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1069d 2f 2a 20 4c 61 73 74 20 73 79 6e 63 65 64 20 70  /* Last synced p
1069e 61 67 65 20 69 6e 20 64 69 72 74 79 20 70 61 67  age in dirty pag
1069f 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20  e list */.  int 
106a0 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20  nRef;           
106a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
106a2 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66  /* Number of ref
106a3 65 72 65 6e 63 65 64 20 70 61 67 65 73 20 2a 2f  erenced pages */
106a4 0a 20 20 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20  .  int nMax;    
106a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
106a6 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67         /* Config
106a7 75 72 65 64 20 63 61 63 68 65 20 73 69 7a 65 20  ured cache size 
106a8 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b  */.  int szPage;
106a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
106aa 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
106ab 20 6f 66 20 65 76 65 72 79 20 70 61 67 65 20 69   of every page i
106ac 6e 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f 0a  n this cache */.
106ad 20 20 69 6e 74 20 73 7a 45 78 74 72 61 3b 20 20    int szExtra;  
106ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
106af 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
106b0 20 65 78 74 72 61 20 73 70 61 63 65 20 66 6f 72   extra space for
106b1 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20   each page */.  
106b2 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20  int bPurgeable; 
106b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
106b4 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70      /* True if p
106b5 61 67 65 73 20 61 72 65 20 6f 6e 20 62 61 63 6b  ages are on back
106b6 69 6e 67 20 73 74 6f 72 65 20 2a 2f 0a 20 20 69  ing store */.  i
106b7 6e 74 20 28 2a 78 53 74 72 65 73 73 29 28 76 6f  nt (*xStress)(vo
106b8 69 64 2a 2c 50 67 48 64 72 2a 29 3b 20 20 20 20  id*,PgHdr*);    
106b9 20 20 20 2f 2a 20 43 61 6c 6c 20 74 6f 20 74 72     /* Call to tr
106ba 79 20 6d 61 6b 65 20 61 20 70 61 67 65 20 63 6c  y make a page cl
106bb 65 61 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  ean */.  void *p
106bc 53 74 72 65 73 73 3b 20 20 20 20 20 20 20 20 20  Stress;         
106bd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
106be 41 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72  Argument to xStr
106bf 65 73 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ess */.  sqlite3
106c0 5f 70 63 61 63 68 65 20 2a 70 43 61 63 68 65 3b  _pcache *pCache;
106c1 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
106c2 50 6c 75 67 67 61 62 6c 65 20 63 61 63 68 65 20  Pluggable cache 
106c3 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 50 67 48 64  module */.  PgHd
106c4 72 20 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20  r *pPage1;      
106c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
106c6 2f 2a 20 52 65 66 65 72 65 6e 63 65 20 74 6f 20  /* Reference to 
106c7 70 61 67 65 20 31 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  page 1 */.};../*
106c8 0a 2a 2a 20 53 6f 6d 65 20 6f 66 20 74 68 65 20  .** Some of the 
106c9 61 73 73 65 72 74 28 29 20 6d 61 63 72 6f 73 20  assert() macros 
106ca 69 6e 20 74 68 69 73 20 63 6f 64 65 20 61 72 65  in this code are
106cb 20 74 6f 6f 20 65 78 70 65 6e 73 69 76 65 20 74   too expensive t
106cc 6f 20 72 75 6e 0a 2a 2a 20 65 76 65 6e 20 64 75  o run.** even du
106cd 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 64 65 62 75  ring normal debu
106ce 67 67 69 6e 67 2e 20 20 55 73 65 20 74 68 65 6d  gging.  Use them
106cf 20 6f 6e 6c 79 20 72 61 72 65 6c 79 20 6f 6e 20   only rarely on 
106d0 6c 6f 6e 67 2d 72 75 6e 6e 69 6e 67 0a 2a 2a 20  long-running.** 
106d1 74 65 73 74 73 2e 20 20 45 6e 61 62 6c 65 20 74  tests.  Enable t
106d2 68 65 20 65 78 70 65 6e 73 69 76 65 20 61 73 73  he expensive ass
106d3 65 72 74 73 20 75 73 69 6e 67 20 74 68 65 0a 2a  erts using the.*
106d4 2a 20 2d 44 53 51 4c 49 54 45 5f 45 4e 41 42 4c  * -DSQLITE_ENABL
106d5 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45  E_EXPENSIVE_ASSE
106d6 52 54 3d 31 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  RT=1 compile-tim
106d7 65 20 6f 70 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66  e option..*/.#if
106d8 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
106d9 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45  E_EXPENSIVE_ASSE
106da 52 54 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65  RT.# define expe
106db 6e 73 69 76 65 5f 61 73 73 65 72 74 28 58 29 20  nsive_assert(X) 
106dc 20 61 73 73 65 72 74 28 58 29 0a 23 65 6c 73 65   assert(X).#else
106dd 0a 23 20 64 65 66 69 6e 65 20 65 78 70 65 6e 73  .# define expens
106de 69 76 65 5f 61 73 73 65 72 74 28 58 29 0a 23 65  ive_assert(X).#e
106df 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
106e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
106e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 4c 69 6e 6b 65 64  ********* Linked
106e2 20 4c 69 73 74 20 4d 61 6e 61 67 65 6d 65 6e 74   List Management
106e3 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
106e4 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 20 21 64 65 66  *****/..#if !def
106e5 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20  ined(NDEBUG) && 
106e6 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
106e7 4e 41 42 4c 45 5f 45 58 50 45 4e 53 49 56 45 5f  NABLE_EXPENSIVE_
106e8 41 53 53 45 52 54 29 0a 2f 2a 0a 2a 2a 20 43 68  ASSERT)./*.** Ch
106e9 65 63 6b 20 74 68 61 74 20 74 68 65 20 70 43 61  eck that the pCa
106ea 63 68 65 2d 3e 70 53 79 6e 63 65 64 20 76 61 72  che->pSynced var
106eb 69 61 62 6c 65 20 69 73 20 73 65 74 20 63 6f 72  iable is set cor
106ec 72 65 63 74 6c 79 2e 20 49 66 20 69 74 0a 2a 2a  rectly. If it.**
106ed 20 69 73 20 6e 6f 74 2c 20 65 69 74 68 65 72 20   is not, either 
106ee 66 61 69 6c 20 61 6e 20 61 73 73 65 72 74 20 6f  fail an assert o
106ef 72 20 72 65 74 75 72 6e 20 7a 65 72 6f 2e 20 4f  r return zero. O
106f0 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e  therwise, return
106f1 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 54 68  .** non-zero. Th
106f2 69 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20  is is only used 
106f3 69 6e 20 64 65 62 75 67 67 69 6e 67 20 62 75 69  in debugging bui
106f4 6c 64 73 2c 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  lds, as follows:
106f5 0a 2a 2a 0a 2a 2a 20 20 20 65 78 70 65 6e 73 69  .**.**   expensi
106f6 76 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68  ve_assert( pcach
106f7 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70 43 61  eCheckSynced(pCa
106f8 63 68 65 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69  che) );.*/.stati
106f9 63 20 69 6e 74 20 70 63 61 63 68 65 43 68 65 63  c int pcacheChec
106fa 6b 53 79 6e 63 65 64 28 50 43 61 63 68 65 20 2a  kSynced(PCache *
106fb 70 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64 72  pCache){.  PgHdr
106fc 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70 43 61   *p;.  for(p=pCa
106fd 63 68 65 2d 3e 70 44 69 72 74 79 54 61 69 6c 3b  che->pDirtyTail;
106fe 20 70 21 3d 70 43 61 63 68 65 2d 3e 70 53 79 6e   p!=pCache->pSyn
106ff 63 65 64 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79  ced; p=p->pDirty
10700 50 72 65 76 29 7b 0a 20 20 20 20 61 73 73 65 72  Prev){.    asser
10701 74 28 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70  t( p->nRef || (p
10702 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45  ->flags&PGHDR_NE
10703 45 44 5f 53 59 4e 43 29 20 29 3b 0a 20 20 7d 0a  ED_SYNC) );.  }.
10704 20 20 72 65 74 75 72 6e 20 28 70 3d 3d 30 20 7c    return (p==0 |
10705 7c 20 70 2d 3e 6e 52 65 66 20 7c 7c 20 28 70 2d  | p->nRef || (p-
10706 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
10707 44 5f 53 59 4e 43 29 3d 3d 30 29 3b 0a 7d 0a 23  D_SYNC)==0);.}.#
10708 65 6e 64 69 66 20 2f 2a 20 21 4e 44 45 42 55 47  endif /* !NDEBUG
10709 20 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c   && SQLITE_ENABL
1070a 45 5f 45 58 50 45 4e 53 49 56 45 5f 41 53 53 45  E_EXPENSIVE_ASSE
1070b 52 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d  RT */../*.** Rem
1070c 6f 76 65 20 70 61 67 65 20 70 50 61 67 65 20 66  ove page pPage f
1070d 72 6f 6d 20 74 68 65 20 6c 69 73 74 20 6f 66 20  rom the list of 
1070e 64 69 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a  dirty pages..*/.
1070f 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63  static void pcac
10710 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74  heRemoveFromDirt
10711 79 4c 69 73 74 28 50 67 48 64 72 20 2a 70 50 61  yList(PgHdr *pPa
10712 67 65 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70  ge){.  PCache *p
10713 20 3d 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65   = pPage->pCache
10714 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ;..  assert( pPa
10715 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 7c  ge->pDirtyNext |
10716 7c 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72  | pPage==p->pDir
10717 74 79 54 61 69 6c 20 29 3b 0a 20 20 61 73 73 65  tyTail );.  asse
10718 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74  rt( pPage->pDirt
10719 79 50 72 65 76 20 7c 7c 20 70 50 61 67 65 3d 3d  yPrev || pPage==
1071a 70 2d 3e 70 44 69 72 74 79 20 29 3b 0a 0a 20 20  p->pDirty );..  
1071b 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 50 43  /* Update the PC
1071c 61 63 68 65 31 2e 70 53 79 6e 63 65 64 20 76 61  ache1.pSynced va
1071d 72 69 61 62 6c 65 20 69 66 20 6e 65 63 65 73 73  riable if necess
1071e 61 72 79 2e 20 2a 2f 0a 20 20 69 66 28 20 70 2d  ary. */.  if( p-
1071f 3e 70 53 79 6e 63 65 64 3d 3d 70 50 61 67 65 20  >pSynced==pPage 
10720 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 53  ){.    PgHdr *pS
10721 79 6e 63 65 64 20 3d 20 70 50 61 67 65 2d 3e 70  ynced = pPage->p
10722 44 69 72 74 79 50 72 65 76 3b 0a 20 20 20 20 77  DirtyPrev;.    w
10723 68 69 6c 65 28 20 70 53 79 6e 63 65 64 20 26 26  hile( pSynced &&
10724 20 28 70 53 79 6e 63 65 64 2d 3e 66 6c 61 67 73   (pSynced->flags
10725 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
10726 29 20 29 7b 0a 20 20 20 20 20 20 70 53 79 6e 63  ) ){.      pSync
10727 65 64 20 3d 20 70 53 79 6e 63 65 64 2d 3e 70 44  ed = pSynced->pD
10728 69 72 74 79 50 72 65 76 3b 0a 20 20 20 20 7d 0a  irtyPrev;.    }.
10729 20 20 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d      p->pSynced =
1072a 20 70 53 79 6e 63 65 64 3b 0a 20 20 7d 0a 0a 20   pSynced;.  }.. 
1072b 20 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72   if( pPage->pDir
1072c 74 79 4e 65 78 74 20 29 7b 0a 20 20 20 20 70 50  tyNext ){.    pP
1072d 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d  age->pDirtyNext-
1072e 3e 70 44 69 72 74 79 50 72 65 76 20 3d 20 70 50  >pDirtyPrev = pP
1072f 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 3b  age->pDirtyPrev;
10730 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
10731 73 65 72 74 28 20 70 50 61 67 65 3d 3d 70 2d 3e  sert( pPage==p->
10732 70 44 69 72 74 79 54 61 69 6c 20 29 3b 0a 20 20  pDirtyTail );.  
10733 20 20 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20    p->pDirtyTail 
10734 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50  = pPage->pDirtyP
10735 72 65 76 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  rev;.  }.  if( p
10736 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76  Page->pDirtyPrev
10737 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70   ){.    pPage->p
10738 44 69 72 74 79 50 72 65 76 2d 3e 70 44 69 72 74  DirtyPrev->pDirt
10739 79 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70  yNext = pPage->p
1073a 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d 65 6c  DirtyNext;.  }el
1073b 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
1073c 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79  pPage==p->pDirty
1073d 20 29 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74   );.    p->pDirt
1073e 79 20 3d 20 70 50 61 67 65 2d 3e 70 44 69 72 74  y = pPage->pDirt
1073f 79 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 70 50 61  yNext;.  }.  pPa
10740 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d  ge->pDirtyNext =
10741 20 30 3b 0a 20 20 70 50 61 67 65 2d 3e 70 44 69   0;.  pPage->pDi
10742 72 74 79 50 72 65 76 20 3d 20 30 3b 0a 0a 20 20  rtyPrev = 0;..  
10743 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72 74  expensive_assert
10744 28 20 70 63 61 63 68 65 43 68 65 63 6b 53 79 6e  ( pcacheCheckSyn
10745 63 65 64 28 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a  ced(p) );.}../*.
10746 2a 2a 20 41 64 64 20 70 61 67 65 20 70 50 61 67  ** Add page pPag
10747 65 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66  e to the head of
10748 20 74 68 65 20 64 69 72 74 79 20 6c 69 73 74 20   the dirty list 
10749 28 50 43 61 63 68 65 31 2e 70 44 69 72 74 79 20  (PCache1.pDirty 
1074a 69 73 20 73 65 74 20 74 6f 0a 2a 2a 20 70 50 61  is set to.** pPa
1074b 67 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ge)..*/.static v
1074c 6f 69 64 20 70 63 61 63 68 65 41 64 64 54 6f 44  oid pcacheAddToD
1074d 69 72 74 79 4c 69 73 74 28 50 67 48 64 72 20 2a  irtyList(PgHdr *
1074e 70 50 61 67 65 29 7b 0a 20 20 50 43 61 63 68 65  pPage){.  PCache
1074f 20 2a 70 20 3d 20 70 50 61 67 65 2d 3e 70 43 61   *p = pPage->pCa
10750 63 68 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  che;..  assert( 
10751 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78  pPage->pDirtyNex
10752 74 3d 3d 30 20 26 26 20 70 50 61 67 65 2d 3e 70  t==0 && pPage->p
10753 44 69 72 74 79 50 72 65 76 3d 3d 30 20 26 26 20  DirtyPrev==0 && 
10754 70 2d 3e 70 44 69 72 74 79 21 3d 70 50 61 67 65  p->pDirty!=pPage
10755 20 29 3b 0a 0a 20 20 70 50 61 67 65 2d 3e 70 44   );..  pPage->pD
10756 69 72 74 79 4e 65 78 74 20 3d 20 70 2d 3e 70 44  irtyNext = p->pD
10757 69 72 74 79 3b 0a 20 20 69 66 28 20 70 50 61 67  irty;.  if( pPag
10758 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 29 7b  e->pDirtyNext ){
10759 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
1075a 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e  ge->pDirtyNext->
1075b 70 44 69 72 74 79 50 72 65 76 3d 3d 30 20 29 3b  pDirtyPrev==0 );
1075c 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44 69 72  .    pPage->pDir
1075d 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 72  tyNext->pDirtyPr
1075e 65 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 7d 0a  ev = pPage;.  }.
1075f 20 20 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50    p->pDirty = pP
10760 61 67 65 3b 0a 20 20 69 66 28 20 21 70 2d 3e 70  age;.  if( !p->p
10761 44 69 72 74 79 54 61 69 6c 20 29 7b 0a 20 20 20  DirtyTail ){.   
10762 20 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 3d   p->pDirtyTail =
10763 20 70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 69 66   pPage;.  }.  if
10764 28 20 21 70 2d 3e 70 53 79 6e 63 65 64 20 26 26  ( !p->pSynced &&
10765 20 30 3d 3d 28 70 50 61 67 65 2d 3e 66 6c 61 67   0==(pPage->flag
10766 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  s&PGHDR_NEED_SYN
10767 43 29 20 29 7b 0a 20 20 20 20 70 2d 3e 70 53 79  C) ){.    p->pSy
10768 6e 63 65 64 20 3d 20 70 50 61 67 65 3b 0a 20 20  nced = pPage;.  
10769 7d 0a 20 20 65 78 70 65 6e 73 69 76 65 5f 61 73  }.  expensive_as
1076a 73 65 72 74 28 20 70 63 61 63 68 65 43 68 65 63  sert( pcacheChec
1076b 6b 53 79 6e 63 65 64 28 70 29 20 29 3b 0a 7d 0a  kSynced(p) );.}.
1076c 0a 2f 2a 0a 2a 2a 20 57 72 61 70 70 65 72 20 61  ./*.** Wrapper a
1076d 72 6f 75 6e 64 20 74 68 65 20 70 6c 75 67 67 61  round the plugga
1076e 62 6c 65 20 63 61 63 68 65 73 20 78 55 6e 70 69  ble caches xUnpi
1076f 6e 20 6d 65 74 68 6f 64 2e 20 49 66 20 74 68 65  n method. If the
10770 20 63 61 63 68 65 20 69 73 0a 2a 2a 20 62 65 69   cache is.** bei
10771 6e 67 20 75 73 65 64 20 66 6f 72 20 61 6e 20 69  ng used for an i
10772 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73  n-memory databas
10773 65 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  e, this function
10774 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a   is a no-op..*/.
10775 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63  static void pcac
10776 68 65 55 6e 70 69 6e 28 50 67 48 64 72 20 2a 70  heUnpin(PgHdr *p
10777 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61  ){.  PCache *pCa
10778 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b  che = p->pCache;
10779 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62  .  if( pCache->b
1077a 50 75 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20  Purgeable ){.   
1077b 20 69 66 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20   if( p->pgno==1 
1077c 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 65 2d  ){.      pCache-
1077d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a 20 20 20  >pPage1 = 0;.   
1077e 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c   }.    sqlite3Gl
1077f 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68  obalConfig.pcach
10780 65 2e 78 55 6e 70 69 6e 28 70 43 61 63 68 65 2d  e.xUnpin(pCache-
10781 3e 70 43 61 63 68 65 2c 20 70 2c 20 30 29 3b 0a  >pCache, p, 0);.
10782 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a    }.}../********
10783 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10784 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10785 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65  *********** Gene
10786 72 61 6c 20 49 6e 74 65 72 66 61 63 65 73 20 2a  ral Interfaces *
10787 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6e 69 74  *****.**.** Init
10788 69 61 6c 69 7a 65 20 61 6e 64 20 73 68 75 74 64  ialize and shutd
10789 6f 77 6e 20 74 68 65 20 70 61 67 65 20 63 61 63  own the page cac
1078a 68 65 20 73 75 62 73 79 73 74 65 6d 2e 20 4e 65  he subsystem. Ne
1078b 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 0a  ither of these .
1078c 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65  ** functions are
1078d 20 74 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a   threadsafe..*/.
1078e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1078f 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  nt sqlite3Pcache
10790 49 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69 64 29  Initialize(void)
10791 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47  {.  if( sqlite3G
10792 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63  lobalConfig.pcac
10793 68 65 2e 78 49 6e 69 74 3d 3d 30 20 29 7b 0a 20  he.xInit==0 ){. 
10794 20 20 20 73 71 6c 69 74 65 33 50 43 61 63 68 65     sqlite3PCache
10795 53 65 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20  SetDefault();.  
10796 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  }.  return sqlit
10797 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
10798 63 61 63 68 65 2e 78 49 6e 69 74 28 73 71 6c 69  cache.xInit(sqli
10799 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
1079a 70 63 61 63 68 65 2e 70 41 72 67 29 3b 0a 7d 0a  pcache.pArg);.}.
1079b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1079c 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
1079d 65 53 68 75 74 64 6f 77 6e 28 76 6f 69 64 29 7b  eShutdown(void){
1079e 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
1079f 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68  obalConfig.pcach
107a0 65 2e 78 53 68 75 74 64 6f 77 6e 20 29 7b 0a 20  e.xShutdown ){. 
107a1 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
107a2 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 53  Config.pcache.xS
107a3 68 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 33 47  hutdown(sqlite3G
107a4 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63  lobalConfig.pcac
107a5 68 65 2e 70 41 72 67 29 3b 0a 20 20 7d 0a 7d 0a  he.pArg);.  }.}.
107a6 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
107a7 65 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73 20  e size in bytes 
107a8 6f 66 20 61 20 50 43 61 63 68 65 20 6f 62 6a 65  of a PCache obje
107a9 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ct..*/.SQLITE_PR
107aa 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
107ab 33 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69 64  3PcacheSize(void
107ac 29 7b 20 72 65 74 75 72 6e 20 73 69 7a 65 6f 66  ){ return sizeof
107ad 28 50 43 61 63 68 65 29 3b 20 7d 0a 0a 2f 2a 0a  (PCache); }../*.
107ae 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
107af 50 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 53  PCache object. S
107b0 74 6f 72 61 67 65 20 73 70 61 63 65 20 74 6f 20  torage space to 
107b1 68 6f 6c 64 20 74 68 65 20 6f 62 6a 65 63 74 0a  hold the object.
107b2 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  ** has already b
107b3 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e  een allocated an
107b4 64 20 69 73 20 70 61 73 73 65 64 20 69 6e 20 61  d is passed in a
107b5 73 20 74 68 65 20 70 20 70 6f 69 6e 74 65 72 2e  s the p pointer.
107b6 20 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20   .** The caller 
107b7 64 69 73 63 6f 76 65 72 73 20 68 6f 77 20 6d 75  discovers how mu
107b8 63 68 20 73 70 61 63 65 20 6e 65 65 64 73 20 74  ch space needs t
107b9 6f 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 20 62  o be allocated b
107ba 79 20 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 73 71  y .** calling sq
107bb 6c 69 74 65 33 50 63 61 63 68 65 53 69 7a 65 28  lite3PcacheSize(
107bc 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
107bd 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
107be 33 50 63 61 63 68 65 4f 70 65 6e 28 0a 20 20 69  3PcacheOpen(.  i
107bf 6e 74 20 73 7a 50 61 67 65 2c 20 20 20 20 20 20  nt szPage,      
107c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
107c1 69 7a 65 20 6f 66 20 65 76 65 72 79 20 70 61 67  ize of every pag
107c2 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 78 74  e */.  int szExt
107c3 72 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ra,             
107c4 20 20 20 20 2f 2a 20 45 78 74 72 61 20 73 70 61      /* Extra spa
107c5 63 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ce associated wi
107c6 74 68 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a  th each page */.
107c7 20 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65    int bPurgeable
107c8 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
107c9 2a 20 54 72 75 65 20 69 66 20 70 61 67 65 73 20  * True if pages 
107ca 61 72 65 20 6f 6e 20 62 61 63 6b 69 6e 67 20 73  are on backing s
107cb 74 6f 72 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  tore */.  int (*
107cc 78 53 74 72 65 73 73 29 28 76 6f 69 64 2a 2c 50  xStress)(void*,P
107cd 67 48 64 72 2a 29 2c 2f 2a 20 43 61 6c 6c 20 74  gHdr*),/* Call t
107ce 6f 20 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61  o try to make pa
107cf 67 65 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76  ges clean */.  v
107d0 6f 69 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20  oid *pStress,   
107d1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
107d2 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65  rgument to xStre
107d3 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a  ss */.  PCache *
107d4 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p               
107d5 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c 6f 63       /* Prealloc
107d6 61 74 65 64 20 73 70 61 63 65 20 66 6f 72 20 74  ated space for t
107d7 68 65 20 50 43 61 63 68 65 20 2a 2f 0a 29 7b 0a  he PCache */.){.
107d8 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73    memset(p, 0, s
107d9 69 7a 65 6f 66 28 50 43 61 63 68 65 29 29 3b 0a  izeof(PCache));.
107da 20 20 70 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a    p->szPage = sz
107db 50 61 67 65 3b 0a 20 20 70 2d 3e 73 7a 45 78 74  Page;.  p->szExt
107dc 72 61 20 3d 20 73 7a 45 78 74 72 61 3b 0a 20 20  ra = szExtra;.  
107dd 70 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 3d 20  p->bPurgeable = 
107de 62 50 75 72 67 65 61 62 6c 65 3b 0a 20 20 70 2d  bPurgeable;.  p-
107df 3e 78 53 74 72 65 73 73 20 3d 20 78 53 74 72 65  >xStress = xStre
107e0 73 73 3b 0a 20 20 70 2d 3e 70 53 74 72 65 73 73  ss;.  p->pStress
107e1 20 3d 20 70 53 74 72 65 73 73 3b 0a 20 20 70 2d   = pStress;.  p-
107e2 3e 6e 4d 61 78 20 3d 20 31 30 30 3b 0a 7d 0a 0a  >nMax = 100;.}..
107e3 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
107e4 20 70 61 67 65 20 73 69 7a 65 20 66 6f 72 20 50   page size for P
107e5 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 54 68  Cache object. Th
107e6 65 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e  e caller must en
107e7 73 75 72 65 20 74 68 61 74 20 74 68 65 72 65 0a  sure that there.
107e8 2a 2a 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61  ** are no outsta
107e9 6e 64 69 6e 67 20 70 61 67 65 20 72 65 66 65 72  nding page refer
107ea 65 6e 63 65 73 20 77 68 65 6e 20 74 68 69 73 20  ences when this 
107eb 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
107ec 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
107ed 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
107ee 65 33 50 63 61 63 68 65 53 65 74 50 61 67 65 53  e3PcacheSetPageS
107ef 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61 63  ize(PCache *pCac
107f0 68 65 2c 20 69 6e 74 20 73 7a 50 61 67 65 29 7b  he, int szPage){
107f1 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68  .  assert( pCach
107f2 65 2d 3e 6e 52 65 66 3d 3d 30 20 26 26 20 70 43  e->nRef==0 && pC
107f3 61 63 68 65 2d 3e 70 44 69 72 74 79 3d 3d 30 20  ache->pDirty==0 
107f4 29 3b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d  );.  if( pCache-
107f5 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73  >pCache ){.    s
107f6 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
107f7 69 67 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72  ig.pcache.xDestr
107f8 6f 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68  oy(pCache->pCach
107f9 65 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e  e);.    pCache->
107fa 70 43 61 63 68 65 20 3d 20 30 3b 0a 20 20 7d 0a  pCache = 0;.  }.
107fb 20 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65    pCache->szPage
107fc 20 3d 20 73 7a 50 61 67 65 3b 0a 7d 0a 0a 2f 2a   = szPage;.}../*
107fd 0a 2a 2a 20 54 72 79 20 74 6f 20 6f 62 74 61 69  .** Try to obtai
107fe 6e 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68  n a page from th
107ff 65 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49  e cache..*/.SQLI
10800 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
10801 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74 63  qlite3PcacheFetc
10802 68 28 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61  h(.  PCache *pCa
10803 63 68 65 2c 20 20 20 20 20 20 20 2f 2a 20 4f 62  che,       /* Ob
10804 74 61 69 6e 20 74 68 65 20 70 61 67 65 20 66 72  tain the page fr
10805 6f 6d 20 74 68 69 73 20 63 61 63 68 65 20 2a 2f  om this cache */
10806 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20  .  Pgno pgno,   
10807 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
10808 20 6e 75 6d 62 65 72 20 74 6f 20 6f 62 74 61 69   number to obtai
10809 6e 20 2a 2f 0a 20 20 69 6e 74 20 63 72 65 61 74  n */.  int creat
1080a 65 46 6c 61 67 2c 20 20 20 20 20 20 20 2f 2a 20  eFlag,       /* 
1080b 49 66 20 74 72 75 65 2c 20 63 72 65 61 74 65 20  If true, create 
1080c 70 61 67 65 20 69 66 20 69 74 20 64 6f 65 73 20  page if it does 
1080d 6e 6f 74 20 65 78 69 73 74 20 61 6c 72 65 61 64  not exist alread
1080e 79 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 2a 70  y */.  PgHdr **p
1080f 70 50 61 67 65 20 20 20 20 20 20 20 20 2f 2a 20  pPage        /* 
10810 57 72 69 74 65 20 74 68 65 20 70 61 67 65 20 68  Write the page h
10811 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 50 67 48 64  ere */.){.  PgHd
10812 72 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20  r *pPage = 0;.  
10813 69 6e 74 20 65 43 72 65 61 74 65 3b 0a 0a 20 20  int eCreate;..  
10814 61 73 73 65 72 74 28 20 70 43 61 63 68 65 21 3d  assert( pCache!=
10815 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63  0 );.  assert( c
10816 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 7c 7c 20  reateFlag==1 || 
10817 63 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 29 3b  createFlag==0 );
10818 0a 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 3e  .  assert( pgno>
10819 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  0 );..  /* If th
1081a 65 20 70 6c 75 67 67 61 62 6c 65 20 63 61 63 68  e pluggable cach
1081b 65 20 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68  e (sqlite3_pcach
1081c 65 2a 29 20 68 61 73 20 6e 6f 74 20 62 65 65 6e  e*) has not been
1081d 20 61 6c 6c 6f 63 61 74 65 64 2c 0a 20 20 2a 2a   allocated,.  **
1081e 20 61 6c 6c 6f 63 61 74 65 20 69 74 20 6e 6f 77   allocate it now
1081f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 43  ..  */.  if( !pC
10820 61 63 68 65 2d 3e 70 43 61 63 68 65 20 26 26 20  ache->pCache && 
10821 63 72 65 61 74 65 46 6c 61 67 20 29 7b 0a 20 20  createFlag ){.  
10822 20 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65    sqlite3_pcache
10823 20 2a 70 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79   *p;.    int nBy
10824 74 65 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20  te;.    nByte = 
10825 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 2b  pCache->szPage +
10826 20 70 43 61 63 68 65 2d 3e 73 7a 45 78 74 72 61   pCache->szExtra
10827 20 2b 20 73 69 7a 65 6f 66 28 50 67 48 64 72 29   + sizeof(PgHdr)
10828 3b 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65  ;.    p = sqlite
10829 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
1082a 61 63 68 65 2e 78 43 72 65 61 74 65 28 6e 42 79  ache.xCreate(nBy
1082b 74 65 2c 20 70 43 61 63 68 65 2d 3e 62 50 75 72  te, pCache->bPur
1082c 67 65 61 62 6c 65 29 3b 0a 20 20 20 20 69 66 28  geable);.    if(
1082d 20 21 70 20 29 7b 0a 20 20 20 20 20 20 72 65 74   !p ){.      ret
1082e 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
1082f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
10830 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
10831 70 63 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a  pcache.xCachesiz
10832 65 28 70 2c 20 70 43 61 63 68 65 2d 3e 6e 4d 61  e(p, pCache->nMa
10833 78 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e  x);.    pCache->
10834 70 43 61 63 68 65 20 3d 20 70 3b 0a 20 20 7d 0a  pCache = p;.  }.
10835 0a 20 20 65 43 72 65 61 74 65 20 3d 20 63 72 65  .  eCreate = cre
10836 61 74 65 46 6c 61 67 20 2a 20 28 31 20 2b 20 28  ateFlag * (1 + (
10837 21 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61  !pCache->bPurgea
10838 62 6c 65 20 7c 7c 20 21 70 43 61 63 68 65 2d 3e  ble || !pCache->
10839 70 44 69 72 74 79 29 29 3b 0a 20 20 69 66 28 20  pDirty));.  if( 
1083a 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29  pCache->pCache )
1083b 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 73 71  {.    pPage = sq
1083c 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
1083d 67 2e 70 63 61 63 68 65 2e 78 46 65 74 63 68 28  g.pcache.xFetch(
1083e 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20  pCache->pCache, 
1083f 70 67 6e 6f 2c 20 65 43 72 65 61 74 65 29 3b 0a  pgno, eCreate);.
10840 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 50 61 67    }..  if( !pPag
10841 65 20 26 26 20 65 43 72 65 61 74 65 3d 3d 31 20  e && eCreate==1 
10842 29 7b 0a 20 20 20 20 50 67 48 64 72 20 2a 70 50  ){.    PgHdr *pP
10843 67 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20  g;..    /* Find 
10844 61 20 64 69 72 74 79 20 70 61 67 65 20 74 6f 20  a dirty page to 
10845 77 72 69 74 65 2d 6f 75 74 20 61 6e 64 20 72 65  write-out and re
10846 63 79 63 6c 65 2e 20 46 69 72 73 74 20 74 72 79  cycle. First try
10847 20 74 6f 20 66 69 6e 64 20 61 20 0a 20 20 20 20   to find a .    
10848 2a 2a 20 70 61 67 65 20 74 68 61 74 20 64 6f 65  ** page that doe
10849 73 20 6e 6f 74 20 72 65 71 75 69 72 65 20 61 20  s not require a 
1084a 6a 6f 75 72 6e 61 6c 2d 73 79 6e 63 20 28 6f 6e  journal-sync (on
1084b 65 20 77 69 74 68 20 50 47 48 44 52 5f 4e 45 45  e with PGHDR_NEE
1084c 44 5f 53 59 4e 43 0a 20 20 20 20 2a 2a 20 63 6c  D_SYNC.    ** cl
1084d 65 61 72 65 64 29 2c 20 62 75 74 20 69 66 20 74  eared), but if t
1084e 68 61 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69  hat is not possi
1084f 62 6c 65 20 73 65 74 74 6c 65 20 66 6f 72 20 61  ble settle for a
10850 6e 79 20 6f 74 68 65 72 20 0a 20 20 20 20 2a 2a  ny other .    **
10851 20 75 6e 72 65 66 65 72 65 6e 63 65 64 20 64 69   unreferenced di
10852 72 74 79 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f  rty page..    */
10853 0a 20 20 20 20 65 78 70 65 6e 73 69 76 65 5f 61  .    expensive_a
10854 73 73 65 72 74 28 20 70 63 61 63 68 65 43 68 65  ssert( pcacheChe
10855 63 6b 53 79 6e 63 65 64 28 70 43 61 63 68 65 29  ckSynced(pCache)
10856 20 29 3b 0a 20 20 20 20 66 6f 72 28 70 50 67 3d   );.    for(pPg=
10857 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 3b  pCache->pSynced;
10858 20 0a 20 20 20 20 20 20 20 20 70 50 67 20 26 26   .        pPg &&
10859 20 28 70 50 67 2d 3e 6e 52 65 66 20 7c 7c 20 28   (pPg->nRef || (
1085a 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  pPg->flags&PGHDR
1085b 5f 4e 45 45 44 5f 53 59 4e 43 29 29 3b 20 0a 20  _NEED_SYNC)); . 
1085c 20 20 20 20 20 20 20 70 50 67 3d 70 50 67 2d 3e         pPg=pPg->
1085d 70 44 69 72 74 79 50 72 65 76 0a 20 20 20 20 29  pDirtyPrev.    )
1085e 3b 0a 20 20 20 20 69 66 28 20 21 70 50 67 20 29  ;.    if( !pPg )
1085f 7b 0a 20 20 20 20 20 20 66 6f 72 28 70 50 67 3d  {.      for(pPg=
10860 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61  pCache->pDirtyTa
10861 69 6c 3b 20 70 50 67 20 26 26 20 70 50 67 2d 3e  il; pPg && pPg->
10862 6e 52 65 66 3b 20 70 50 67 3d 70 50 67 2d 3e 70  nRef; pPg=pPg->p
10863 44 69 72 74 79 50 72 65 76 29 3b 0a 20 20 20 20  DirtyPrev);.    
10864 7d 0a 20 20 20 20 69 66 28 20 70 50 67 20 29 7b  }.    if( pPg ){
10865 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20  .      int rc;. 
10866 20 20 20 20 20 72 63 20 3d 20 70 43 61 63 68 65       rc = pCache
10867 2d 3e 78 53 74 72 65 73 73 28 70 43 61 63 68 65  ->xStress(pCache
10868 2d 3e 70 53 74 72 65 73 73 2c 20 70 50 67 29 3b  ->pStress, pPg);
10869 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
1086a 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d  QLITE_OK && rc!=
1086b 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20  SQLITE_BUSY ){. 
1086c 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
1086d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1086e 0a 20 20 20 20 70 50 61 67 65 20 3d 20 73 71 6c  .    pPage = sql
1086f 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
10870 2e 70 63 61 63 68 65 2e 78 46 65 74 63 68 28 70  .pcache.xFetch(p
10871 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70  Cache->pCache, p
10872 67 6e 6f 2c 20 32 29 3b 0a 20 20 7d 0a 0a 20 20  gno, 2);.  }..  
10873 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20 20  if( pPage ){.   
10874 20 69 66 28 20 21 70 50 61 67 65 2d 3e 70 44 61   if( !pPage->pDa
10875 74 61 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73  ta ){.      mems
10876 65 74 28 70 50 61 67 65 2c 20 30 2c 20 73 69 7a  et(pPage, 0, siz
10877 65 6f 66 28 50 67 48 64 72 29 20 2b 20 70 43 61  eof(PgHdr) + pCa
10878 63 68 65 2d 3e 73 7a 45 78 74 72 61 29 3b 0a 20  che->szExtra);. 
10879 20 20 20 20 20 70 50 61 67 65 2d 3e 70 45 78 74       pPage->pExt
1087a 72 61 20 3d 20 28 76 6f 69 64 2a 29 26 70 50 61  ra = (void*)&pPa
1087b 67 65 5b 31 5d 3b 0a 20 20 20 20 20 20 70 50 61  ge[1];.      pPa
1087c 67 65 2d 3e 70 44 61 74 61 20 3d 20 28 76 6f 69  ge->pData = (voi
1087d 64 20 2a 29 26 28 28 63 68 61 72 20 2a 29 70 50  d *)&((char *)pP
1087e 61 67 65 29 5b 73 69 7a 65 6f 66 28 50 67 48 64  age)[sizeof(PgHd
1087f 72 29 20 2b 20 70 43 61 63 68 65 2d 3e 73 7a 45  r) + pCache->szE
10880 78 74 72 61 5d 3b 0a 20 20 20 20 20 20 70 50 61  xtra];.      pPa
10881 67 65 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61  ge->pCache = pCa
10882 63 68 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65  che;.      pPage
10883 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20  ->pgno = pgno;. 
10884 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
10885 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d 3d   pPage->pCache==
10886 70 43 61 63 68 65 20 29 3b 0a 20 20 20 20 61 73  pCache );.    as
10887 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67 6e  sert( pPage->pgn
10888 6f 3d 3d 70 67 6e 6f 20 29 3b 0a 20 20 20 20 61  o==pgno );.    a
10889 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 45  ssert( pPage->pE
1088a 78 74 72 61 3d 3d 28 76 6f 69 64 20 2a 29 26 70  xtra==(void *)&p
1088b 50 61 67 65 5b 31 5d 20 29 3b 0a 0a 20 20 20 20  Page[1] );..    
1088c 69 66 28 20 30 3d 3d 70 50 61 67 65 2d 3e 6e 52  if( 0==pPage->nR
1088d 65 66 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63  ef ){.      pCac
1088e 68 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20  he->nRef++;.    
1088f 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e 52 65  }.    pPage->nRe
10890 66 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 67 6e  f++;.    if( pgn
10891 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 43  o==1 ){.      pC
10892 61 63 68 65 2d 3e 70 50 61 67 65 31 20 3d 20 70  ache->pPage1 = p
10893 50 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Page;.    }.  }.
10894 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50 61 67    *ppPage = pPag
10895 65 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61  e;.  return (pPa
10896 67 65 3d 3d 30 20 26 26 20 65 43 72 65 61 74 65  ge==0 && eCreate
10897 29 20 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  ) ? SQLITE_NOMEM
10898 20 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a   : SQLITE_OK;.}.
10899 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e 74  ./*.** Decrement
1089a 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 63   the reference c
1089b 6f 75 6e 74 20 6f 6e 20 61 20 70 61 67 65 2e 20  ount on a page. 
1089c 49 66 20 74 68 65 20 70 61 67 65 20 69 73 20 63  If the page is c
1089d 6c 65 61 6e 20 61 6e 64 20 74 68 65 0a 2a 2a 20  lean and the.** 
1089e 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20  reference count 
1089f 64 72 6f 70 73 20 74 6f 20 30 2c 20 74 68 65 6e  drops to 0, then
108a0 20 69 74 20 69 73 20 6d 61 64 65 20 65 6c 69 62   it is made elib
108a1 6c 65 20 66 6f 72 20 72 65 63 79 63 6c 69 6e 67  le for recycling
108a2 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
108a3 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
108a4 50 63 61 63 68 65 52 65 6c 65 61 73 65 28 50 67  PcacheRelease(Pg
108a5 48 64 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  Hdr *p){.  asser
108a6 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a  t( p->nRef>0 );.
108a7 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69    p->nRef--;.  i
108a8 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b  f( p->nRef==0 ){
108a9 0a 20 20 20 20 50 43 61 63 68 65 20 2a 70 43 61  .    PCache *pCa
108aa 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b  che = p->pCache;
108ab 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 52 65  .    pCache->nRe
108ac 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 28 70 2d  f--;.    if( (p-
108ad 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52  >flags&PGHDR_DIR
108ae 54 59 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  TY)==0 ){.      
108af 70 63 61 63 68 65 55 6e 70 69 6e 28 70 29 3b 0a  pcacheUnpin(p);.
108b0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
108b1 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70 61 67   /* Move the pag
108b2 65 20 74 6f 20 74 68 65 20 68 65 61 64 20 6f 66  e to the head of
108b3 20 74 68 65 20 64 69 72 74 79 20 6c 69 73 74 2e   the dirty list.
108b4 20 2a 2f 0a 20 20 20 20 20 20 70 63 61 63 68 65   */.      pcache
108b5 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c  RemoveFromDirtyL
108b6 69 73 74 28 70 29 3b 0a 20 20 20 20 20 20 70 63  ist(p);.      pc
108b7 61 63 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69  acheAddToDirtyLi
108b8 73 74 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  st(p);.    }.  }
108b9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65 61  .}../*.** Increa
108ba 73 65 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  se the reference
108bb 20 63 6f 75 6e 74 20 6f 66 20 61 20 73 75 70 70   count of a supp
108bc 6c 69 65 64 20 70 61 67 65 20 62 79 20 31 2e 0a  lied page by 1..
108bd 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
108be 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
108bf 61 63 68 65 52 65 66 28 50 67 48 64 72 20 2a 70  acheRef(PgHdr *p
108c0 29 7b 0a 20 20 61 73 73 65 72 74 28 70 2d 3e 6e  ){.  assert(p->n
108c1 52 65 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e 52 65  Ref>0);.  p->nRe
108c2 66 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72  f++;.}../*.** Dr
108c3 6f 70 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74  op a page from t
108c4 68 65 20 63 61 63 68 65 2e 20 54 68 65 72 65 20  he cache. There 
108c5 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 20  must be exactly 
108c6 6f 6e 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f  one reference to
108c7 20 74 68 65 0a 2a 2a 20 70 61 67 65 2e 20 54 68   the.** page. Th
108c8 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 6c 65  is function dele
108c9 74 65 73 20 74 68 61 74 20 72 65 66 65 72 65 6e  tes that referen
108ca 63 65 2c 20 73 6f 20 61 66 74 65 72 20 69 74 20  ce, so after it 
108cb 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 70  returns the.** p
108cc 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  age pointed to b
108cd 79 20 70 20 69 73 20 69 6e 76 61 6c 69 64 2e 0a  y p is invalid..
108ce 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
108cf 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
108d0 61 63 68 65 44 72 6f 70 28 50 67 48 64 72 20 2a  acheDrop(PgHdr *
108d1 70 29 7b 0a 20 20 50 43 61 63 68 65 20 2a 70 43  p){.  PCache *pC
108d2 61 63 68 65 3b 0a 20 20 61 73 73 65 72 74 28 20  ache;.  assert( 
108d3 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a 20 20  p->nRef==1 );.  
108d4 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 50 47 48  if( p->flags&PGH
108d5 44 52 5f 44 49 52 54 59 20 29 7b 0a 20 20 20 20  DR_DIRTY ){.    
108d6 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d  pcacheRemoveFrom
108d7 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20  DirtyList(p);.  
108d8 7d 0a 20 20 70 43 61 63 68 65 20 3d 20 70 2d 3e  }.  pCache = p->
108d9 70 43 61 63 68 65 3b 0a 20 20 70 43 61 63 68 65  pCache;.  pCache
108da 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20  ->nRef--;.  if( 
108db 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20  p->pgno==1 ){.  
108dc 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31    pCache->pPage1
108dd 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69   = 0;.  }.  sqli
108de 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
108df 70 63 61 63 68 65 2e 78 55 6e 70 69 6e 28 70 43  pcache.xUnpin(pC
108e0 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c  ache->pCache, p,
108e1 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61   1);.}../*.** Ma
108e2 6b 65 20 73 75 72 65 20 74 68 65 20 70 61 67 65  ke sure the page
108e3 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20 64 69   is marked as di
108e4 72 74 79 2e 20 49 66 20 69 74 20 69 73 6e 27 74  rty. If it isn't
108e5 20 64 69 72 74 79 20 61 6c 72 65 61 64 79 2c 0a   dirty already,.
108e6 2a 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a  ** make it so..*
108e7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
108e8 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
108e9 63 68 65 4d 61 6b 65 44 69 72 74 79 28 50 67 48  cheMakeDirty(PgH
108ea 64 72 20 2a 70 29 7b 0a 20 20 70 2d 3e 66 6c 61  dr *p){.  p->fla
108eb 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 44 4f 4e  gs &= ~PGHDR_DON
108ec 54 5f 57 52 49 54 45 3b 0a 20 20 61 73 73 65 72  T_WRITE;.  asser
108ed 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a  t( p->nRef>0 );.
108ee 20 20 69 66 28 20 30 3d 3d 28 70 2d 3e 66 6c 61    if( 0==(p->fla
108ef 67 73 20 26 20 50 47 48 44 52 5f 44 49 52 54 59  gs & PGHDR_DIRTY
108f0 29 20 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67  ) ){.    p->flag
108f1 73 20 7c 3d 20 50 47 48 44 52 5f 44 49 52 54 59  s |= PGHDR_DIRTY
108f2 3b 0a 20 20 20 20 70 63 61 63 68 65 41 64 64 54  ;.    pcacheAddT
108f3 6f 44 69 72 74 79 4c 69 73 74 28 20 70 29 3b 0a  oDirtyList( p);.
108f4 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b    }.}../*.** Mak
108f5 65 20 73 75 72 65 20 74 68 65 20 70 61 67 65 20  e sure the page 
108f6 69 73 20 6d 61 72 6b 65 64 20 61 73 20 63 6c 65  is marked as cle
108f7 61 6e 2e 20 49 66 20 69 74 20 69 73 6e 27 74 20  an. If it isn't 
108f8 63 6c 65 61 6e 20 61 6c 72 65 61 64 79 2c 0a 2a  clean already,.*
108f9 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f  * make it so..*/
108fa 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
108fb 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
108fc 68 65 4d 61 6b 65 43 6c 65 61 6e 28 50 67 48 64  heMakeClean(PgHd
108fd 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 28 70 2d  r *p){.  if( (p-
108fe 3e 66 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44  >flags & PGHDR_D
108ff 49 52 54 59 29 20 29 7b 0a 20 20 20 20 70 63 61  IRTY) ){.    pca
10900 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72  cheRemoveFromDir
10901 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 70  tyList(p);.    p
10902 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 50 47 48  ->flags &= ~(PGH
10903 44 52 5f 44 49 52 54 59 7c 50 47 48 44 52 5f 4e  DR_DIRTY|PGHDR_N
10904 45 45 44 5f 53 59 4e 43 29 3b 0a 20 20 20 20 69  EED_SYNC);.    i
10905 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b  f( p->nRef==0 ){
10906 0a 20 20 20 20 20 20 70 63 61 63 68 65 55 6e 70  .      pcacheUnp
10907 69 6e 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  in(p);.    }.  }
10908 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65  .}../*.** Make e
10909 76 65 72 79 20 70 61 67 65 20 69 6e 20 74 68 65  very page in the
1090a 20 63 61 63 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f   cache clean..*/
1090b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1090c 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
1090d 68 65 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68  heCleanAll(PCach
1090e 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50 67  e *pCache){.  Pg
1090f 48 64 72 20 2a 70 3b 0a 20 20 77 68 69 6c 65 28  Hdr *p;.  while(
10910 20 28 70 20 3d 20 70 43 61 63 68 65 2d 3e 70 44   (p = pCache->pD
10911 69 72 74 79 29 21 3d 30 20 29 7b 0a 20 20 20 20  irty)!=0 ){.    
10912 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b  sqlite3PcacheMak
10913 65 43 6c 65 61 6e 28 70 29 3b 0a 20 20 7d 0a 7d  eClean(p);.  }.}
10914 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74 68  ../*.** Clear th
10915 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  e PGHDR_NEED_SYN
10916 43 20 66 6c 61 67 20 66 72 6f 6d 20 61 6c 6c 20  C flag from all 
10917 64 69 72 74 79 20 70 61 67 65 73 2e 0a 2a 2f 0a  dirty pages..*/.
10918 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
10919 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
1091a 65 43 6c 65 61 72 53 79 6e 63 46 6c 61 67 73 28  eClearSyncFlags(
1091b 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b  PCache *pCache){
1091c 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66  .  PgHdr *p;.  f
1091d 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69  or(p=pCache->pDi
1091e 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70 44 69  rty; p; p=p->pDi
1091f 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 70 2d  rtyNext){.    p-
10920 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52  >flags &= ~PGHDR
10921 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 7d 0a  _NEED_SYNC;.  }.
10922 20 20 70 43 61 63 68 65 2d 3e 70 53 79 6e 63 65    pCache->pSynce
10923 64 20 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72  d = pCache->pDir
10924 74 79 54 61 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  tyTail;.}../*.**
10925 20 43 68 61 6e 67 65 20 74 68 65 20 70 61 67 65   Change the page
10926 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20   number of page 
10927 70 20 74 6f 20 6e 65 77 50 67 6e 6f 2e 20 0a 2a  p to newPgno. .*
10928 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
10929 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
1092a 63 68 65 4d 6f 76 65 28 50 67 48 64 72 20 2a 70  cheMove(PgHdr *p
1092b 2c 20 50 67 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b  , Pgno newPgno){
1092c 0a 20 20 50 43 61 63 68 65 20 2a 70 43 61 63 68  .  PCache *pCach
1092d 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b 0a 20  e = p->pCache;. 
1092e 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66   assert( p->nRef
1092f 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
10930 6e 65 77 50 67 6e 6f 3e 30 20 29 3b 0a 20 20 73  newPgno>0 );.  s
10931 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
10932 69 67 2e 70 63 61 63 68 65 2e 78 52 65 6b 65 79  ig.pcache.xRekey
10933 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c  (pCache->pCache,
10934 20 70 2c 20 70 2d 3e 70 67 6e 6f 2c 20 6e 65 77   p, p->pgno, new
10935 50 67 6e 6f 29 3b 0a 20 20 70 2d 3e 70 67 6e 6f  Pgno);.  p->pgno
10936 20 3d 20 6e 65 77 50 67 6e 6f 3b 0a 20 20 69 66   = newPgno;.  if
10937 28 20 28 70 2d 3e 66 6c 61 67 73 26 50 47 48 44  ( (p->flags&PGHD
10938 52 5f 44 49 52 54 59 29 20 26 26 20 28 70 2d 3e  R_DIRTY) && (p->
10939 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44  flags&PGHDR_NEED
1093a 5f 53 59 4e 43 29 20 29 7b 0a 20 20 20 20 70 63  _SYNC) ){.    pc
1093b 61 63 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69  acheRemoveFromDi
1093c 72 74 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20  rtyList(p);.    
1093d 70 63 61 63 68 65 41 64 64 54 6f 44 69 72 74 79  pcacheAddToDirty
1093e 4c 69 73 74 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  List(p);.  }.}..
1093f 2f 2a 0a 2a 2a 20 44 72 6f 70 20 65 76 65 72 79  /*.** Drop every
10940 20 63 61 63 68 65 20 65 6e 74 72 79 20 77 68 6f   cache entry who
10941 73 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69  se page number i
10942 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 22  s greater than "
10943 70 67 6e 6f 22 2e 20 54 68 65 0a 2a 2a 20 63 61  pgno". The.** ca
10944 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72 65  ller must ensure
10945 20 74 68 61 74 20 74 68 65 72 65 20 61 72 65 20   that there are 
10946 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 72  no outstanding r
10947 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61 6e 79  eferences to any
10948 20 70 61 67 65 73 0a 2a 2a 20 6f 74 68 65 72 20   pages.** other 
10949 74 68 61 6e 20 70 61 67 65 20 31 20 77 69 74 68  than page 1 with
1094a 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 20 67   a page number g
1094b 72 65 61 74 65 72 20 74 68 61 6e 20 70 67 6e 6f  reater than pgno
1094c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65  ..**.** If there
1094d 20 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 20   is a reference 
1094e 74 6f 20 70 61 67 65 20 31 20 61 6e 64 20 74 68  to page 1 and th
1094f 65 20 70 67 6e 6f 20 70 61 72 61 6d 65 74 65 72  e pgno parameter
10950 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 0a   passed to this.
10951 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 30  ** function is 0
10952 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61 20  , then the data 
10953 61 72 65 61 20 61 73 73 6f 63 69 61 74 65 64 20  area associated 
10954 77 69 74 68 20 70 61 67 65 20 31 20 69 73 20 7a  with page 1 is z
10955 65 72 6f 65 64 2c 20 62 75 74 0a 2a 2a 20 74 68  eroed, but.** th
10956 65 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69 73  e page object is
10957 20 6e 6f 74 20 64 72 6f 70 70 65 64 2e 0a 2a 2f   not dropped..*/
10958 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
10959 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
1095a 68 65 54 72 75 6e 63 61 74 65 28 50 43 61 63 68  heTruncate(PCach
1095b 65 20 2a 70 43 61 63 68 65 2c 20 50 67 6e 6f 20  e *pCache, Pgno 
1095c 70 67 6e 6f 29 7b 0a 20 20 69 66 28 20 70 43 61  pgno){.  if( pCa
1095d 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20  che->pCache ){. 
1095e 20 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 20     PgHdr *p;.   
1095f 20 50 67 48 64 72 20 2a 70 4e 65 78 74 3b 0a 20   PgHdr *pNext;. 
10960 20 20 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d     for(p=pCache-
10961 3e 70 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 4e  >pDirty; p; p=pN
10962 65 78 74 29 7b 0a 20 20 20 20 20 20 70 4e 65 78  ext){.      pNex
10963 74 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e 65 78  t = p->pDirtyNex
10964 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  t;.      if( p->
10965 70 67 6e 6f 3e 70 67 6e 6f 20 29 7b 0a 20 20 20  pgno>pgno ){.   
10966 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
10967 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54  flags&PGHDR_DIRT
10968 59 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  Y );.        sql
10969 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c  ite3PcacheMakeCl
1096a 65 61 6e 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a  ean(p);.      }.
1096b 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 67      }.    if( pg
1096c 6e 6f 3d 3d 30 20 26 26 20 70 43 61 63 68 65 2d  no==0 && pCache-
1096d 3e 70 50 61 67 65 31 20 29 7b 0a 20 20 20 20 20  >pPage1 ){.     
1096e 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65 2d 3e   memset(pCache->
1096f 70 50 61 67 65 31 2d 3e 70 44 61 74 61 2c 20 30  pPage1->pData, 0
10970 2c 20 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65  , pCache->szPage
10971 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20 3d 20  );.      pgno = 
10972 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  1;.    }.    sql
10973 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
10974 2e 70 63 61 63 68 65 2e 78 54 72 75 6e 63 61 74  .pcache.xTruncat
10975 65 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65  e(pCache->pCache
10976 2c 20 70 67 6e 6f 2b 31 29 3b 0a 20 20 7d 0a 7d  , pgno+1);.  }.}
10977 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20  ../*.** Close a 
10978 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cache..*/.SQLITE
10979 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1097a 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65  lite3PcacheClose
1097b 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29  (PCache *pCache)
1097c 7b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e  {.  if( pCache->
1097d 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71  pCache ){.    sq
1097e 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
1097f 67 2e 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f  g.pcache.xDestro
10980 79 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65  y(pCache->pCache
10981 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  );.  }.}../* .**
10982 20 44 69 73 63 61 72 64 20 74 68 65 20 63 6f 6e   Discard the con
10983 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 61 63  tents of the cac
10984 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  he..*/.SQLITE_PR
10985 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
10986 65 33 50 63 61 63 68 65 43 6c 65 61 72 28 50 43  e3PcacheClear(PC
10987 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20  ache *pCache){. 
10988 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 72   sqlite3PcacheTr
10989 75 6e 63 61 74 65 28 70 43 61 63 68 65 2c 20 30  uncate(pCache, 0
1098a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67  );.}../*.** Merg
1098b 65 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 70  e two lists of p
1098c 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 20 62  ages connected b
1098d 79 20 70 44 69 72 74 79 20 61 6e 64 20 69 6e 20  y pDirty and in 
1098e 70 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20 44  pgno order..** D
1098f 6f 20 6e 6f 74 20 62 6f 74 68 20 66 69 78 69 6e  o not both fixin
10990 67 20 74 68 65 20 70 44 69 72 74 79 50 72 65 76  g the pDirtyPrev
10991 20 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73 74   pointers..*/.st
10992 61 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 63  atic PgHdr *pcac
10993 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 74  heMergeDirtyList
10994 28 50 67 48 64 72 20 2a 70 41 2c 20 50 67 48 64  (PgHdr *pA, PgHd
10995 72 20 2a 70 42 29 7b 0a 20 20 50 67 48 64 72 20  r *pB){.  PgHdr 
10996 72 65 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b 0a  result, *pTail;.
10997 20 20 70 54 61 69 6c 20 3d 20 26 72 65 73 75 6c    pTail = &resul
10998 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26  t;.  while( pA &
10999 26 20 70 42 20 29 7b 0a 20 20 20 20 69 66 28 20  & pB ){.    if( 
1099a 70 41 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 6e  pA->pgno<pB->pgn
1099b 6f 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c  o ){.      pTail
1099c 2d 3e 70 44 69 72 74 79 20 3d 20 70 41 3b 0a 20  ->pDirty = pA;. 
1099d 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 41 3b       pTail = pA;
1099e 0a 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e  .      pA = pA->
1099f 70 44 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c 73  pDirty;.    }els
109a0 65 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e  e{.      pTail->
109a1 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 20  pDirty = pB;.   
109a2 20 20 20 70 54 61 69 6c 20 3d 20 70 42 3b 0a 20     pTail = pB;. 
109a3 20 20 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 44       pB = pB->pD
109a4 69 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  irty;.    }.  }.
109a5 20 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20 20    if( pA ){.    
109a6 70 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20  pTail->pDirty = 
109a7 70 41 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  pA;.  }else if( 
109a8 70 42 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d  pB ){.    pTail-
109a9 3e 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20  >pDirty = pB;.  
109aa 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 61 69 6c  }else{.    pTail
109ab 2d 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20  ->pDirty = 0;.  
109ac 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 73 75 6c  }.  return resul
109ad 74 2e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a  t.pDirty;.}../*.
109ae 2a 2a 20 53 6f 72 74 20 74 68 65 20 6c 69 73 74  ** Sort the list
109af 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 63 63   of pages in acc
109b0 65 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20  ending order by 
109b1 70 67 6e 6f 2e 20 20 50 61 67 65 73 20 61 72 65  pgno.  Pages are
109b2 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79  .** connected by
109b3 20 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 73   pDirty pointers
109b4 2e 20 20 54 68 65 20 70 44 69 72 74 79 50 72 65  .  The pDirtyPre
109b5 76 20 70 6f 69 6e 74 65 72 73 20 61 72 65 0a 2a  v pointers are.*
109b6 2a 20 63 6f 72 72 75 70 74 65 64 20 62 79 20 74  * corrupted by t
109b7 68 69 73 20 73 6f 72 74 2e 0a 2a 2a 0a 2a 2a 20  his sort..**.** 
109b8 53 69 6e 63 65 20 74 68 65 72 65 20 63 61 6e 6e  Since there cann
109b9 6f 74 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20  ot be more than 
109ba 32 5e 33 31 20 64 69 73 74 69 6e 63 74 20 70 61  2^31 distinct pa
109bb 67 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73  ges in a databas
109bc 65 2c 0a 2a 2a 20 74 68 65 72 65 20 63 61 6e 6e  e,.** there cann
109bd 6f 74 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20  ot be more than 
109be 33 31 20 62 75 63 6b 65 74 73 20 72 65 71 75 69  31 buckets requi
109bf 72 65 64 20 62 79 20 74 68 65 20 6d 65 72 67 65  red by the merge
109c0 20 73 6f 72 74 65 72 2e 0a 2a 2a 20 4f 6e 65 20   sorter..** One 
109c1 65 78 74 72 61 20 62 75 63 6b 65 74 20 69 73 20  extra bucket is 
109c2 61 64 64 65 64 20 74 6f 20 63 61 74 63 68 20 6f  added to catch o
109c3 76 65 72 66 6c 6f 77 20 69 6e 20 63 61 73 65 20  verflow in case 
109c4 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 65 76 65  something.** eve
109c5 72 20 63 68 61 6e 67 65 73 20 74 6f 20 6d 61 6b  r changes to mak
109c6 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 73  e the previous s
109c7 65 6e 74 65 6e 63 65 20 69 6e 63 6f 72 72 65 63  entence incorrec
109c8 74 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 5f  t..*/.#define N_
109c9 53 4f 52 54 5f 42 55 43 4b 45 54 20 20 33 32 0a  SORT_BUCKET  32.
109ca 73 74 61 74 69 63 20 50 67 48 64 72 20 2a 70 63  static PgHdr *pc
109cb 61 63 68 65 53 6f 72 74 44 69 72 74 79 4c 69 73  acheSortDirtyLis
109cc 74 28 50 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20  t(PgHdr *pIn){. 
109cd 20 50 67 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54   PgHdr *a[N_SORT
109ce 5f 42 55 43 4b 45 54 5d 2c 20 2a 70 3b 0a 20 20  _BUCKET], *p;.  
109cf 69 6e 74 20 69 3b 0a 20 20 6d 65 6d 73 65 74 28  int i;.  memset(
109d0 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61 29 29  a, 0, sizeof(a))
109d1 3b 0a 20 20 77 68 69 6c 65 28 20 70 49 6e 20 29  ;.  while( pIn )
109d2 7b 0a 20 20 20 20 70 20 3d 20 70 49 6e 3b 0a 20  {.    p = pIn;. 
109d3 20 20 20 70 49 6e 20 3d 20 70 2d 3e 70 44 69 72     pIn = p->pDir
109d4 74 79 3b 0a 20 20 20 20 70 2d 3e 70 44 69 72 74  ty;.    p->pDirt
109d5 79 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69  y = 0;.    for(i
109d6 3d 30 3b 20 41 4c 57 41 59 53 28 69 3c 4e 5f 53  =0; ALWAYS(i<N_S
109d7 4f 52 54 5f 42 55 43 4b 45 54 2d 31 29 3b 20 69  ORT_BUCKET-1); i
109d8 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61  ++){.      if( a
109d9 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  [i]==0 ){.      
109da 20 20 61 5b 69 5d 20 3d 20 70 3b 0a 20 20 20 20    a[i] = p;.    
109db 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
109dc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
109dd 70 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44  p = pcacheMergeD
109de 69 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20 70  irtyList(a[i], p
109df 29 3b 0a 20 20 20 20 20 20 20 20 61 5b 69 5d 20  );.        a[i] 
109e0 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
109e1 20 7d 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52   }.    if( NEVER
109e2 28 69 3d 3d 4e 5f 53 4f 52 54 5f 42 55 43 4b 45  (i==N_SORT_BUCKE
109e3 54 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a  T-1) ){.      /*
109e4 20 54 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68   To get here, th
109e5 65 72 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32  ere need to be 2
109e6 5e 28 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29  ^(N_SORT_BUCKET)
109e7 20 65 6c 65 6d 65 6e 74 73 20 69 6e 0a 20 20 20   elements in.   
109e8 20 20 20 2a 2a 20 74 68 65 20 69 6e 70 75 74 20     ** the input 
109e9 6c 69 73 74 2e 20 20 42 75 74 20 74 68 61 74 20  list.  But that 
109ea 69 73 20 69 6d 70 6f 73 73 69 62 6c 65 2e 0a 20  is impossible.. 
109eb 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 5b       */.      a[
109ec 69 5d 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65  i] = pcacheMerge
109ed 44 69 72 74 79 4c 69 73 74 28 61 5b 69 5d 2c 20  DirtyList(a[i], 
109ee 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  p);.    }.  }.  
109ef 70 20 3d 20 61 5b 30 5d 3b 0a 20 20 66 6f 72 28  p = a[0];.  for(
109f0 69 3d 31 3b 20 69 3c 4e 5f 53 4f 52 54 5f 42 55  i=1; i<N_SORT_BU
109f1 43 4b 45 54 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  CKET; i++){.    
109f2 70 20 3d 20 70 63 61 63 68 65 4d 65 72 67 65 44  p = pcacheMergeD
109f3 69 72 74 79 4c 69 73 74 28 70 2c 20 61 5b 69 5d  irtyList(p, a[i]
109f4 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
109f5 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  p;.}../*.** Retu
109f6 72 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c  rn a list of all
109f7 20 64 69 72 74 79 20 70 61 67 65 73 20 69 6e 20   dirty pages in 
109f8 74 68 65 20 63 61 63 68 65 2c 20 73 6f 72 74 65  the cache, sorte
109f9 64 20 62 79 20 70 61 67 65 20 6e 75 6d 62 65 72  d by page number
109fa 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
109fb 41 54 45 20 50 67 48 64 72 20 2a 73 71 6c 69 74  ATE PgHdr *sqlit
109fc 65 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 73  e3PcacheDirtyLis
109fd 74 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65  t(PCache *pCache
109fe 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20  ){.  PgHdr *p;. 
109ff 20 66 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70   for(p=pCache->p
10a00 44 69 72 74 79 3b 20 70 3b 20 70 3d 70 2d 3e 70  Dirty; p; p=p->p
10a01 44 69 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20  DirtyNext){.    
10a02 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 2d 3e 70  p->pDirty = p->p
10a03 44 69 72 74 79 4e 65 78 74 3b 0a 20 20 7d 0a 20  DirtyNext;.  }. 
10a04 20 72 65 74 75 72 6e 20 70 63 61 63 68 65 53 6f   return pcacheSo
10a05 72 74 44 69 72 74 79 4c 69 73 74 28 70 43 61 63  rtDirtyList(pCac
10a06 68 65 2d 3e 70 44 69 72 74 79 29 3b 0a 7d 0a 0a  he->pDirty);.}..
10a07 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  /* .** Return th
10a08 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
10a09 66 20 72 65 66 65 72 65 6e 63 65 64 20 70 61 67  f referenced pag
10a0a 65 73 20 68 65 6c 64 20 62 79 20 74 68 65 20 63  es held by the c
10a0b 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ache..*/.SQLITE_
10a0c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
10a0d 74 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e  te3PcacheRefCoun
10a0e 74 28 50 43 61 63 68 65 20 2a 70 43 61 63 68 65  t(PCache *pCache
10a0f 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 43 61 63  ){.  return pCac
10a10 68 65 2d 3e 6e 52 65 66 3b 0a 7d 0a 0a 2f 2a 0a  he->nRef;.}../*.
10a11 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
10a12 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63  mber of referenc
10a13 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 73  es to the page s
10a14 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72  upplied as an ar
10a15 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  gument..*/.SQLIT
10a16 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
10a17 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 52  lite3PcachePageR
10a18 65 66 63 6f 75 6e 74 28 50 67 48 64 72 20 2a 70  efcount(PgHdr *p
10a19 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e  ){.  return p->n
10a1a 52 65 66 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52  Ref;.}../* .** R
10a1b 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61 6c 20  eturn the total 
10a1c 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
10a1d 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a 2a 2f  in the cache..*/
10a1e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
10a1f 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68  int sqlite3Pcach
10a20 65 50 61 67 65 63 6f 75 6e 74 28 50 43 61 63 68  ePagecount(PCach
10a21 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e  e *pCache){.  in
10a22 74 20 6e 50 61 67 65 20 3d 20 30 3b 0a 20 20 69  t nPage = 0;.  i
10a23 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68  f( pCache->pCach
10a24 65 20 29 7b 0a 20 20 20 20 6e 50 61 67 65 20 3d  e ){.    nPage =
10a25 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
10a26 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 50 61 67  nfig.pcache.xPag
10a27 65 63 6f 75 6e 74 28 70 43 61 63 68 65 2d 3e 70  ecount(pCache->p
10a28 43 61 63 68 65 29 3b 0a 20 20 7d 0a 20 20 72 65  Cache);.  }.  re
10a29 74 75 72 6e 20 6e 50 61 67 65 3b 0a 7d 0a 0a 23  turn nPage;.}..#
10a2a 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
10a2b 54 0a 2f 2a 0a 2a 2a 20 47 65 74 20 74 68 65 20  T./*.** Get the 
10a2c 73 75 67 67 65 73 74 65 64 20 63 61 63 68 65 2d  suggested cache-
10a2d 73 69 7a 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53  size value..*/.S
10a2e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
10a2f 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 47  t sqlite3PcacheG
10a30 65 74 43 61 63 68 65 73 69 7a 65 28 50 43 61 63  etCachesize(PCac
10a31 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72  he *pCache){.  r
10a32 65 74 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 4d  eturn pCache->nM
10a33 61 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ax;.}.#endif../*
10a34 0a 2a 2a 20 53 65 74 20 74 68 65 20 73 75 67 67  .** Set the sugg
10a35 65 73 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65  ested cache-size
10a36 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54   value..*/.SQLIT
10a37 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
10a38 71 6c 69 74 65 33 50 63 61 63 68 65 53 65 74 43  qlite3PcacheSetC
10a39 61 63 68 65 73 69 7a 65 28 50 43 61 63 68 65 20  achesize(PCache 
10a3a 2a 70 43 61 63 68 65 2c 20 69 6e 74 20 6d 78 50  *pCache, int mxP
10a3b 61 67 65 29 7b 0a 20 20 70 43 61 63 68 65 2d 3e  age){.  pCache->
10a3c 6e 4d 61 78 20 3d 20 6d 78 50 61 67 65 3b 0a 20  nMax = mxPage;. 
10a3d 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61   if( pCache->pCa
10a3e 63 68 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  che ){.    sqlit
10a3f 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
10a40 63 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65  cache.xCachesize
10a41 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c  (pCache->pCache,
10a42 20 6d 78 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a   mxPage);.  }.}.
10a43 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
10a44 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 29  ITE_CHECK_PAGES)
10a45 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
10a46 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20  TE_DEBUG)./*.** 
10a47 46 6f 72 20 61 6c 6c 20 64 69 72 74 79 20 70 61  For all dirty pa
10a48 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e  ges currently in
10a49 20 74 68 65 20 63 61 63 68 65 2c 20 69 6e 76 6f   the cache, invo
10a4a 6b 65 20 74 68 65 20 73 70 65 63 69 66 69 65 64  ke the specified
10a4b 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 2e 20 54 68  .** callback. Th
10a4c 69 73 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20  is is only used 
10a4d 69 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43 48  if the SQLITE_CH
10a4e 45 43 4b 5f 50 41 47 45 53 20 6d 61 63 72 6f 20  ECK_PAGES macro 
10a4f 69 73 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a  is.** defined..*
10a50 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
10a51 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
10a52 63 68 65 49 74 65 72 61 74 65 44 69 72 74 79 28  cheIterateDirty(
10a53 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20  PCache *pCache, 
10a54 76 6f 69 64 20 28 2a 78 49 74 65 72 29 28 50 67  void (*xIter)(Pg
10a55 48 64 72 20 2a 29 29 7b 0a 20 20 50 67 48 64 72  Hdr *)){.  PgHdr
10a56 20 2a 70 44 69 72 74 79 3b 0a 20 20 66 6f 72 28   *pDirty;.  for(
10a57 70 44 69 72 74 79 3d 70 43 61 63 68 65 2d 3e 70  pDirty=pCache->p
10a58 44 69 72 74 79 3b 20 70 44 69 72 74 79 3b 20 70  Dirty; pDirty; p
10a59 44 69 72 74 79 3d 70 44 69 72 74 79 2d 3e 70 44  Dirty=pDirty->pD
10a5a 69 72 74 79 4e 65 78 74 29 7b 0a 20 20 20 20 78  irtyNext){.    x
10a5b 49 74 65 72 28 70 44 69 72 74 79 29 3b 0a 20 20  Iter(pDirty);.  
10a5c 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a  }.}.#endif../***
10a5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
10a5e 6f 66 20 70 63 61 63 68 65 2e 63 20 2a 2a 2a 2a  of pcache.c ****
10a5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
10a62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
10a63 6e 20 66 69 6c 65 20 70 63 61 63 68 65 31 2e 63  n file pcache1.c
10a64 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
10a65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
10a67 2a 20 32 30 30 38 20 4e 6f 76 65 6d 62 65 72 20  * 2008 November 
10a68 30 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  05.**.** The aut
10a69 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
10a6a 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
10a6b 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
10a6c 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
10a6d 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
10a6e 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
10a6f 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
10a70 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
10a71 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
10a72 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
10a73 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
10a74 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
10a75 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
10a76 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
10a77 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
10a78 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
10a79 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
10a7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10a7e 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
10a7f 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  e implements the
10a80 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 63 61   default page ca
10a81 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  che implementati
10a82 6f 6e 20 28 74 68 65 0a 2a 2a 20 73 71 6c 69 74  on (the.** sqlit
10a83 65 33 5f 70 63 61 63 68 65 20 69 6e 74 65 72 66  e3_pcache interf
10a84 61 63 65 29 2e 20 49 74 20 61 6c 73 6f 20 63 6f  ace). It also co
10a85 6e 74 61 69 6e 73 20 70 61 72 74 20 6f 66 20 74  ntains part of t
10a86 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
10a87 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c 49  n.** of the SQLI
10a88 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41  TE_CONFIG_PAGECA
10a89 43 48 45 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  CHE and sqlite3_
10a8a 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 29  release_memory()
10a8b 20 66 65 61 74 75 72 65 73 2e 0a 2a 2a 20 49 66   features..** If
10a8c 20 74 68 65 20 64 65 66 61 75 6c 74 20 70 61 67   the default pag
10a8d 65 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e  e cache implemen
10a8e 74 61 74 69 6f 6e 20 69 73 20 6f 76 65 72 72 69  tation is overri
10a8f 64 65 6e 2c 20 74 68 65 6e 20 6e 65 69 74 68 65  den, then neithe
10a90 72 20 6f 66 0a 2a 2a 20 74 68 65 73 65 20 74 77  r of.** these tw
10a91 6f 20 66 65 61 74 75 72 65 73 20 61 72 65 20 61  o features are a
10a92 76 61 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  vailable..**.** 
10a93 40 28 23 29 20 24 49 64 3a 20 70 63 61 63 68 65  @(#) $Id: pcache
10a94 31 2e 63 2c 76 20 31 2e 31 39 20 32 30 30 39 2f  1.c,v 1.19 2009/
10a95 30 37 2f 31 37 20 31 31 3a 34 34 3a 30 37 20 64  07/17 11:44:07 d
10a96 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 74 79  rh Exp $.*/...ty
10a97 70 65 64 65 66 20 73 74 72 75 63 74 20 50 43 61  pedef struct PCa
10a98 63 68 65 31 20 50 43 61 63 68 65 31 3b 0a 74 79  che1 PCache1;.ty
10a99 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48  pedef struct PgH
10a9a 64 72 31 20 50 67 48 64 72 31 3b 0a 74 79 70 65  dr1 PgHdr1;.type
10a9b 64 65 66 20 73 74 72 75 63 74 20 50 67 46 72 65  def struct PgFre
10a9c 65 73 6c 6f 74 20 50 67 46 72 65 65 73 6c 6f 74  eslot PgFreeslot
10a9d 3b 0a 0a 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74  ;../* Pointers t
10a9e 6f 20 73 74 72 75 63 74 75 72 65 73 20 6f 66 20  o structures of 
10a9f 74 68 69 73 20 74 79 70 65 20 61 72 65 20 63 61  this type are ca
10aa0 73 74 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20  st and returned 
10aa1 61 73 20 0a 2a 2a 20 6f 70 61 71 75 65 20 73 71  as .** opaque sq
10aa2 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 20 68 61  lite3_pcache* ha
10aa3 6e 64 6c 65 73 0a 2a 2f 0a 73 74 72 75 63 74 20  ndles.*/.struct 
10aa4 50 43 61 63 68 65 31 20 7b 0a 20 20 2f 2a 20 43  PCache1 {.  /* C
10aa5 61 63 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69  ache configurati
10aa6 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 2e 20 50  on parameters. P
10aa7 61 67 65 20 73 69 7a 65 20 28 73 7a 50 61 67 65  age size (szPage
10aa8 29 20 61 6e 64 20 74 68 65 20 70 75 72 67 65 61  ) and the purgea
10aa9 62 6c 65 0a 20 20 2a 2a 20 66 6c 61 67 20 28 62  ble.  ** flag (b
10aaa 50 75 72 67 65 61 62 6c 65 29 20 61 72 65 20 73  Purgeable) are s
10aab 65 74 20 77 68 65 6e 20 74 68 65 20 63 61 63 68  et when the cach
10aac 65 20 69 73 20 63 72 65 61 74 65 64 2e 20 6e 4d  e is created. nM
10aad 61 78 20 6d 61 79 20 62 65 20 0a 20 20 2a 2a 20  ax may be .  ** 
10aae 6d 6f 64 69 66 69 65 64 20 61 74 20 61 6e 79 20  modified at any 
10aaf 74 69 6d 65 20 62 79 20 61 20 63 61 6c 6c 20 74  time by a call t
10ab0 6f 20 74 68 65 20 70 63 61 63 68 65 31 43 61 63  o the pcache1Cac
10ab1 68 65 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 2e  heSize() method.
10ab2 0a 20 20 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c  .  ** The global
10ab3 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68   mutex must be h
10ab4 65 6c 64 20 77 68 65 6e 20 61 63 63 65 73 73 69  eld when accessi
10ab5 6e 67 20 6e 4d 61 78 2e 0a 20 20 2a 2f 0a 20 20  ng nMax..  */.  
10ab6 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20  int szPage;     
10ab7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10ab8 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61      /* Size of a
10ab9 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 73 20 69  llocated pages i
10aba 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74  n bytes */.  int
10abb 20 62 50 75 72 67 65 61 62 6c 65 3b 20 20 20 20   bPurgeable;    
10abc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10abd 20 2f 2a 20 54 72 75 65 20 69 66 20 63 61 63 68   /* True if cach
10abe 65 20 69 73 20 70 75 72 67 65 61 62 6c 65 20 2a  e is purgeable *
10abf 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  /.  unsigned int
10ac0 20 6e 4d 69 6e 3b 20 20 20 20 20 20 20 20 20 20   nMin;          
10ac1 20 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d          /* Minim
10ac2 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  um number of pag
10ac3 65 73 20 72 65 73 65 72 76 65 64 20 2a 2f 0a 20  es reserved */. 
10ac4 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 4d   unsigned int nM
10ac5 61 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ax;             
10ac6 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72       /* Configur
10ac7 65 64 20 22 63 61 63 68 65 5f 73 69 7a 65 22 20  ed "cache_size" 
10ac8 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20 48  value */..  /* H
10ac9 61 73 68 20 74 61 62 6c 65 20 6f 66 20 61 6c 6c  ash table of all
10aca 20 70 61 67 65 73 2e 20 54 68 65 20 66 6f 6c 6c   pages. The foll
10acb 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 73 20  owing variables 
10acc 6d 61 79 20 6f 6e 6c 79 20 62 65 20 61 63 63 65  may only be acce
10acd 73 73 65 64 0a 20 20 2a 2a 20 77 68 65 6e 20 74  ssed.  ** when t
10ace 68 65 20 61 63 63 65 73 73 6f 72 20 69 73 20 68  he accessor is h
10acf 6f 6c 64 69 6e 67 20 74 68 65 20 67 6c 6f 62 61  olding the globa
10ad0 6c 20 6d 75 74 65 78 20 28 73 65 65 20 70 63 61  l mutex (see pca
10ad1 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29  che1EnterMutex()
10ad2 20 0a 20 20 2a 2a 20 61 6e 64 20 70 63 61 63 68   .  ** and pcach
10ad3 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 29 2e  e1LeaveMutex()).
10ad4 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  .  */.  unsigned
10ad5 20 69 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c 65   int nRecyclable
10ad6 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
10ad7 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
10ad8 6e 20 74 68 65 20 4c 52 55 20 6c 69 73 74 20 2a  n the LRU list *
10ad9 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  /.  unsigned int
10ada 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20   nPage;         
10adb 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c          /* Total
10adc 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
10add 20 69 6e 20 61 70 48 61 73 68 20 2a 2f 0a 20 20   in apHash */.  
10ade 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 48 61  unsigned int nHa
10adf 73 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  sh;             
10ae0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
10ae1 20 73 6c 6f 74 73 20 69 6e 20 61 70 48 61 73 68   slots in apHash
10ae2 5b 5d 20 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a  [] */.  PgHdr1 *
10ae3 2a 61 70 48 61 73 68 3b 20 20 20 20 20 20 20 20  *apHash;        
10ae4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
10ae5 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66 61  ash table for fa
10ae6 73 74 20 6c 6f 6f 6b 75 70 20 62 79 20 6b 65 79  st lookup by key
10ae7 20 2a 2f 0a 0a 20 20 75 6e 73 69 67 6e 65 64 20   */..  unsigned 
10ae8 69 6e 74 20 69 4d 61 78 4b 65 79 3b 20 20 20 20  int iMaxKey;    
10ae9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61             /* La
10aea 72 67 65 73 74 20 6b 65 79 20 73 65 65 6e 20 73  rgest key seen s
10aeb 69 6e 63 65 20 78 54 72 75 6e 63 61 74 65 28 29  ince xTruncate()
10aec 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61   */.};../*.** Ea
10aed 63 68 20 63 61 63 68 65 20 65 6e 74 72 79 20 69  ch cache entry i
10aee 73 20 72 65 70 72 65 73 65 6e 74 65 64 20 62 79  s represented by
10aef 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
10af0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a  the following .*
10af1 2a 20 73 74 72 75 63 74 75 72 65 2e 20 41 20 62  * structure. A b
10af2 75 66 66 65 72 20 6f 66 20 50 67 48 64 72 31 2e  uffer of PgHdr1.
10af3 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 62  pCache->szPage b
10af4 79 74 65 73 20 69 73 20 61 6c 6c 6f 63 61 74 65  ytes is allocate
10af5 64 20 0a 2a 2a 20 64 69 72 65 63 74 6c 79 20 62  d .** directly b
10af6 65 66 6f 72 65 20 74 68 69 73 20 73 74 72 75 63  efore this struc
10af7 74 75 72 65 20 69 6e 20 6d 65 6d 6f 72 79 20 28  ture in memory (
10af8 73 65 65 20 74 68 65 20 50 47 48 44 52 31 5f 54  see the PGHDR1_T
10af9 4f 5f 50 41 47 45 28 29 20 0a 2a 2a 20 6d 61 63  O_PAGE() .** mac
10afa 72 6f 20 62 65 6c 6f 77 29 2e 0a 2a 2f 0a 73 74  ro below)..*/.st
10afb 72 75 63 74 20 50 67 48 64 72 31 20 7b 0a 20 20  ruct PgHdr1 {.  
10afc 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4b 65  unsigned int iKe
10afd 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  y;             /
10afe 2a 20 4b 65 79 20 76 61 6c 75 65 20 28 70 61 67  * Key value (pag
10aff 65 20 6e 75 6d 62 65 72 29 20 2a 2f 0a 20 20 50  e number) */.  P
10b00 67 48 64 72 31 20 2a 70 4e 65 78 74 3b 20 20 20  gHdr1 *pNext;   
10b01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10b02 20 4e 65 78 74 20 69 6e 20 68 61 73 68 20 74 61   Next in hash ta
10b03 62 6c 65 20 63 68 61 69 6e 20 2a 2f 0a 20 20 50  ble chain */.  P
10b04 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 3b 20  Cache1 *pCache; 
10b05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
10b06 20 43 61 63 68 65 20 74 68 61 74 20 63 75 72 72   Cache that curr
10b07 65 6e 74 6c 79 20 6f 77 6e 73 20 74 68 69 73 20  ently owns this 
10b08 70 61 67 65 20 2a 2f 0a 20 20 50 67 48 64 72 31  page */.  PgHdr1
10b09 20 2a 70 4c 72 75 4e 65 78 74 3b 20 20 20 20 20   *pLruNext;     
10b0a 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74           /* Next
10b0b 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20   in LRU list of 
10b0c 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a  unpinned pages *
10b0d 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75  /.  PgHdr1 *pLru
10b0e 50 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 20  Prev;           
10b0f 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 69     /* Previous i
10b10 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75 6e  n LRU list of un
10b11 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f 0a  pinned pages */.
10b12 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 73  };../*.** Free s
10b13 6c 6f 74 73 20 69 6e 20 74 68 65 20 61 6c 6c 6f  lots in the allo
10b14 63 61 74 6f 72 20 75 73 65 64 20 74 6f 20 64 69  cator used to di
10b15 76 69 64 65 20 75 70 20 74 68 65 20 62 75 66 66  vide up the buff
10b16 65 72 20 70 72 6f 76 69 64 65 64 20 75 73 69 6e  er provided usin
10b17 67 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f  g.** the SQLITE_
10b18 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
10b19 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a 73   mechanism..*/.s
10b1a 74 72 75 63 74 20 50 67 46 72 65 65 73 6c 6f 74  truct PgFreeslot
10b1b 20 7b 0a 20 20 50 67 46 72 65 65 73 6c 6f 74 20   {.  PgFreeslot 
10b1c 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78 74  *pNext;  /* Next
10b1d 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 7d 3b   free slot */.};
10b1e 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20 64  ../*.** Global d
10b1f 61 74 61 20 75 73 65 64 20 62 79 20 74 68 69 73  ata used by this
10b20 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69   cache..*/.stati
10b21 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72  c SQLITE_WSD str
10b22 75 63 74 20 50 43 61 63 68 65 47 6c 6f 62 61 6c  uct PCacheGlobal
10b23 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   {.  sqlite3_mut
10b24 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20  ex *mutex;      
10b25 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74 61 74           /* stat
10b26 69 63 20 6d 75 74 65 78 20 4d 55 54 45 58 5f 53  ic mutex MUTEX_S
10b27 54 41 54 49 43 5f 4c 52 55 20 2a 2f 0a 0a 20 20  TATIC_LRU */..  
10b28 69 6e 74 20 6e 4d 61 78 50 61 67 65 3b 20 20 20  int nMaxPage;   
10b29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b2a 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e 4d      /* Sum of nM
10b2b 61 78 50 61 67 65 20 66 6f 72 20 70 75 72 67 65  axPage for purge
10b2c 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20  able caches */. 
10b2d 20 69 6e 74 20 6e 4d 69 6e 50 61 67 65 3b 20 20   int nMinPage;  
10b2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b2f 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e       /* Sum of n
10b30 4d 69 6e 50 61 67 65 20 66 6f 72 20 70 75 72 67  MinPage for purg
10b31 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a  eable caches */.
10b32 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 50 61    int nCurrentPa
10b33 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ge;             
10b34 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
10b35 6f 66 20 70 75 72 67 65 61 62 6c 65 20 70 61 67  of purgeable pag
10b36 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a  es allocated */.
10b37 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 48 65    PgHdr1 *pLruHe
10b38 61 64 2c 20 2a 70 4c 72 75 54 61 69 6c 3b 20 20  ad, *pLruTail;  
10b39 20 20 20 20 20 20 2f 2a 20 4c 52 55 20 6c 69 73        /* LRU lis
10b3a 74 20 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70 61  t of unpinned pa
10b3b 67 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 56 61 72  ges */..  /* Var
10b3c 69 61 62 6c 65 73 20 72 65 6c 61 74 65 64 20 74  iables related t
10b3d 6f 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  o SQLITE_CONFIG_
10b3e 50 41 47 45 43 41 43 48 45 20 73 65 74 74 69 6e  PAGECACHE settin
10b3f 67 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 53  gs. */.  int szS
10b40 6c 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  lot;            
10b41 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10b42 53 69 7a 65 20 6f 66 20 65 61 63 68 20 66 72 65  Size of each fre
10b43 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 76 6f 69 64  e slot */.  void
10b44 20 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b   *pStart, *pEnd;
10b45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b46 2f 2a 20 42 6f 75 6e 64 73 20 6f 66 20 70 61 67  /* Bounds of pag
10b47 65 63 61 63 68 65 20 6d 61 6c 6c 6f 63 20 72 61  ecache malloc ra
10b48 6e 67 65 20 2a 2f 0a 20 20 50 67 46 72 65 65 73  nge */.  PgFrees
10b49 6c 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 20 20  lot *pFree;     
10b4a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10b4b 46 72 65 65 20 70 61 67 65 20 62 6c 6f 63 6b 73  Free page blocks
10b4c 20 2a 2f 0a 20 20 69 6e 74 20 69 73 49 6e 69 74   */.  int isInit
10b4d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
10b4e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
10b4f 65 20 69 66 20 69 6e 69 74 69 61 6c 69 7a 65 64  e if initialized
10b50 20 2a 2f 0a 7d 20 70 63 61 63 68 65 31 5f 67 3b   */.} pcache1_g;
10b51 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 6f 64 65  ../*.** All code
10b52 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 73 68   in this file sh
10b53 6f 75 6c 64 20 61 63 63 65 73 73 20 74 68 65 20  ould access the 
10b54 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65  global structure
10b55 20 61 62 6f 76 65 20 76 69 61 20 74 68 65 0a 2a   above via the.*
10b56 2a 20 61 6c 69 61 73 20 22 70 63 61 63 68 65 31  * alias "pcache1
10b57 22 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73 20  ". This ensures 
10b58 74 68 61 74 20 74 68 65 20 57 53 44 20 65 6d 75  that the WSD emu
10b59 6c 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 77  lation is used w
10b5a 68 65 6e 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e 67  hen.** compiling
10b5b 20 66 6f 72 20 73 79 73 74 65 6d 73 20 74 68 61   for systems tha
10b5c 74 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74  t do not support
10b5d 20 72 65 61 6c 20 57 53 44 2e 0a 2a 2f 0a 23 64   real WSD..*/.#d
10b5e 65 66 69 6e 65 20 70 63 61 63 68 65 31 20 28 47  efine pcache1 (G
10b5f 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 50 43 61  LOBAL(struct PCa
10b60 63 68 65 47 6c 6f 62 61 6c 2c 20 70 63 61 63 68  cheGlobal, pcach
10b61 65 31 5f 67 29 29 0a 0a 2f 2a 0a 2a 2a 20 57 68  e1_g))../*.** Wh
10b62 65 6e 20 61 20 50 67 48 64 72 31 20 73 74 72 75  en a PgHdr1 stru
10b63 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61 74  cture is allocat
10b64 65 64 2c 20 74 68 65 20 61 73 73 6f 63 69 61 74  ed, the associat
10b65 65 64 20 50 43 61 63 68 65 31 2e 73 7a 50 61 67  ed PCache1.szPag
10b66 65 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20 64 61  e.** bytes of da
10b67 74 61 20 61 72 65 20 6c 6f 63 61 74 65 64 20 64  ta are located d
10b68 69 72 65 63 74 6c 79 20 62 65 66 6f 72 65 20 69  irectly before i
10b69 74 20 69 6e 20 6d 65 6d 6f 72 79 20 28 69 2e 65  t in memory (i.e
10b6a 2e 20 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20 73  . the total.** s
10b6b 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63  ize of the alloc
10b6c 61 74 69 6f 6e 20 69 73 20 73 69 7a 65 6f 66 28  ation is sizeof(
10b6d 50 67 48 64 72 31 29 2b 50 43 61 63 68 65 31 2e  PgHdr1)+PCache1.
10b6e 73 7a 50 61 67 65 20 62 79 74 65 29 2e 20 54 68  szPage byte). Th
10b6f 65 0a 2a 2a 20 50 47 48 44 52 31 5f 54 4f 5f 50  e.** PGHDR1_TO_P
10b70 41 47 45 28 29 20 6d 61 63 72 6f 20 74 61 6b 65  AGE() macro take
10b71 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
10b72 20 50 67 48 64 72 31 20 73 74 72 75 63 74 75 72   PgHdr1 structur
10b73 65 20 61 73 0a 2a 2a 20 61 6e 20 61 72 67 75 6d  e as.** an argum
10b74 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 73 20  ent and returns 
10b75 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
10b76 20 61 73 73 6f 63 69 61 74 65 64 20 62 6c 6f 63   associated bloc
10b77 6b 20 6f 66 20 73 7a 50 61 67 65 0a 2a 2a 20 62  k of szPage.** b
10b78 79 74 65 73 2e 20 54 68 65 20 50 41 47 45 5f 54  ytes. The PAGE_T
10b79 4f 5f 50 47 48 44 52 31 28 29 20 6d 61 63 72 6f  O_PGHDR1() macro
10b7a 20 64 6f 65 73 20 74 68 65 20 6f 70 70 6f 73 69   does the opposi
10b7b 74 65 3a 20 69 74 73 20 61 72 67 75 6d 65 6e 74  te: its argument
10b7c 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72   is.** a pointer
10b7d 20 74 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 73   to a block of s
10b7e 7a 50 61 67 65 20 62 79 74 65 73 20 6f 66 20 64  zPage bytes of d
10b7f 61 74 61 20 61 6e 64 20 74 68 65 20 72 65 74 75  ata and the retu
10b80 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61  rn value is.** a
10b81 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
10b82 61 73 73 6f 63 69 61 74 65 64 20 50 67 48 64 72  associated PgHdr
10b83 31 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  1 structure..**.
10b84 2a 2a 20 20 20 61 73 73 65 72 74 28 20 50 47 48  **   assert( PGH
10b85 44 52 31 5f 54 4f 5f 50 41 47 45 28 50 41 47 45  DR1_TO_PAGE(PAGE
10b86 5f 54 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68  _TO_PGHDR1(pCach
10b87 65 2c 20 58 29 29 3d 3d 58 20 29 3b 0a 2a 2f 0a  e, X))==X );.*/.
10b88 23 64 65 66 69 6e 65 20 50 47 48 44 52 31 5f 54  #define PGHDR1_T
10b89 4f 5f 50 41 47 45 28 70 29 20 20 20 20 28 76 6f  O_PAGE(p)    (vo
10b8a 69 64 2a 29 28 28 28 63 68 61 72 2a 29 70 29 20  id*)(((char*)p) 
10b8b 2d 20 70 2d 3e 70 43 61 63 68 65 2d 3e 73 7a 50  - p->pCache->szP
10b8c 61 67 65 29 0a 23 64 65 66 69 6e 65 20 50 41 47  age).#define PAG
10b8d 45 5f 54 4f 5f 50 47 48 44 52 31 28 63 2c 20 70  E_TO_PGHDR1(c, p
10b8e 29 20 28 50 67 48 64 72 31 2a 29 28 28 28 63 68  ) (PgHdr1*)(((ch
10b8f 61 72 2a 29 70 29 20 2b 20 63 2d 3e 73 7a 50 61  ar*)p) + c->szPa
10b90 67 65 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f  ge)../*.** Macro
10b91 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 64 20 6c  s to enter and l
10b92 65 61 76 65 20 74 68 65 20 67 6c 6f 62 61 6c 20  eave the global 
10b93 4c 52 55 20 6d 75 74 65 78 2e 0a 2a 2f 0a 23 64  LRU mutex..*/.#d
10b94 65 66 69 6e 65 20 70 63 61 63 68 65 31 45 6e 74  efine pcache1Ent
10b95 65 72 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65  erMutex() sqlite
10b96 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 63  3_mutex_enter(pc
10b97 61 63 68 65 31 2e 6d 75 74 65 78 29 0a 23 64 65  ache1.mutex).#de
10b98 66 69 6e 65 20 70 63 61 63 68 65 31 4c 65 61 76  fine pcache1Leav
10b99 65 4d 75 74 65 78 28 29 20 73 71 6c 69 74 65 33  eMutex() sqlite3
10b9a 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 63 61  _mutex_leave(pca
10b9b 63 68 65 31 2e 6d 75 74 65 78 29 0a 0a 2f 2a 2a  che1.mutex)../**
10b9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10b9f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
10ba1 2a 2a 2a 2a 2a 2a 2a 20 50 61 67 65 20 41 6c 6c  ******* Page All
10ba2 6f 63 61 74 69 6f 6e 2f 53 51 4c 49 54 45 5f 43  ocation/SQLITE_C
10ba3 4f 4e 46 49 47 5f 50 43 41 43 48 45 20 52 65 6c  ONFIG_PCACHE Rel
10ba4 61 74 65 64 20 46 75 6e 63 74 69 6f 6e 73 20 2a  ated Functions *
10ba5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
10ba6 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
10ba7 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 75  ion is called du
10ba8 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74  ring initializat
10ba9 69 6f 6e 20 69 66 20 61 20 73 74 61 74 69 63 20  ion if a static 
10baa 62 75 66 66 65 72 20 69 73 20 0a 2a 2a 20 73 75  buffer is .** su
10bab 70 70 6c 69 65 64 20 74 6f 20 75 73 65 20 66 6f  pplied to use fo
10bac 72 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65  r the page-cache
10bad 20 62 79 20 70 61 73 73 69 6e 67 20 74 68 65 20   by passing the 
10bae 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41  SQLITE_CONFIG_PA
10baf 47 45 43 41 43 48 45 0a 2a 2a 20 76 65 72 62 20  GECACHE.** verb 
10bb0 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  to sqlite3_confi
10bb1 67 28 29 2e 20 50 61 72 61 6d 65 74 65 72 20 70  g(). Parameter p
10bb2 42 75 66 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e  Buf points to an
10bb3 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67   allocation larg
10bb4 65 0a 2a 2a 20 65 6e 6f 75 67 68 20 74 6f 20 63  e.** enough to c
10bb5 6f 6e 74 61 69 6e 20 27 6e 27 20 62 75 66 66 65  ontain 'n' buffe
10bb6 72 73 20 6f 66 20 27 73 7a 27 20 62 79 74 65 73  rs of 'sz' bytes
10bb7 20 65 61 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45   each..*/.SQLITE
10bb8 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
10bb9 6c 69 74 65 33 50 43 61 63 68 65 42 75 66 66 65  lite3PCacheBuffe
10bba 72 53 65 74 75 70 28 76 6f 69 64 20 2a 70 42 75  rSetup(void *pBu
10bbb 66 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e  f, int sz, int n
10bbc 29 7b 0a 20 20 69 66 28 20 70 63 61 63 68 65 31  ){.  if( pcache1
10bbd 2e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 50  .isInit ){.    P
10bbe 67 46 72 65 65 73 6c 6f 74 20 2a 70 3b 0a 20 20  gFreeslot *p;.  
10bbf 20 20 73 7a 20 3d 20 52 4f 55 4e 44 44 4f 57 4e    sz = ROUNDDOWN
10bc0 38 28 73 7a 29 3b 0a 20 20 20 20 70 63 61 63 68  8(sz);.    pcach
10bc1 65 31 2e 73 7a 53 6c 6f 74 20 3d 20 73 7a 3b 0a  e1.szSlot = sz;.
10bc2 20 20 20 20 70 63 61 63 68 65 31 2e 70 53 74 61      pcache1.pSta
10bc3 72 74 20 3d 20 70 42 75 66 3b 0a 20 20 20 20 70  rt = pBuf;.    p
10bc4 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 30  cache1.pFree = 0
10bc5 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 6e 2d 2d  ;.    while( n--
10bc6 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 28 50   ){.      p = (P
10bc7 67 46 72 65 65 73 6c 6f 74 2a 29 70 42 75 66 3b  gFreeslot*)pBuf;
10bc8 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20  .      p->pNext 
10bc9 3d 20 70 63 61 63 68 65 31 2e 70 46 72 65 65 3b  = pcache1.pFree;
10bca 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70  .      pcache1.p
10bcb 46 72 65 65 20 3d 20 70 3b 0a 20 20 20 20 20 20  Free = p;.      
10bcc 70 42 75 66 20 3d 20 28 76 6f 69 64 2a 29 26 28  pBuf = (void*)&(
10bcd 28 63 68 61 72 2a 29 70 42 75 66 29 5b 73 7a 5d  (char*)pBuf)[sz]
10bce 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63  ;.    }.    pcac
10bcf 68 65 31 2e 70 45 6e 64 20 3d 20 70 42 75 66 3b  he1.pEnd = pBuf;
10bd0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61  .  }.}../*.** Ma
10bd1 6c 6c 6f 63 20 66 75 6e 63 74 69 6f 6e 20 75 73  lloc function us
10bd2 65 64 20 77 69 74 68 69 6e 20 74 68 69 73 20 66  ed within this f
10bd3 69 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ile to allocate 
10bd4 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62  space from the b
10bd5 75 66 66 65 72 0a 2a 2a 20 63 6f 6e 66 69 67 75  uffer.** configu
10bd6 72 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65  red using sqlite
10bd7 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f  3_config(SQLITE_
10bd8 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
10bd9 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20  ) option. If no 
10bda 0a 2a 2a 20 73 75 63 68 20 62 75 66 66 65 72 20  .** such buffer 
10bdb 65 78 69 73 74 73 20 6f 72 20 74 68 65 72 65 20  exists or there 
10bdc 69 73 20 6e 6f 20 73 70 61 63 65 20 6c 65 66 74  is no space left
10bdd 20 69 6e 20 69 74 2c 20 74 68 69 73 20 66 75 6e   in it, this fun
10bde 63 74 69 6f 6e 20 66 61 6c 6c 73 20 0a 2a 2a 20  ction falls .** 
10bdf 62 61 63 6b 20 74 6f 20 73 71 6c 69 74 65 33 4d  back to sqlite3M
10be0 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74  alloc()..*/.stat
10be1 69 63 20 76 6f 69 64 20 2a 70 63 61 63 68 65 31  ic void *pcache1
10be2 41 6c 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29  Alloc(int nByte)
10be3 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61  {.  void *p;.  a
10be4 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
10be5 75 74 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65  utex_held(pcache
10be6 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66  1.mutex) );.  if
10be7 28 20 6e 42 79 74 65 3c 3d 70 63 61 63 68 65 31  ( nByte<=pcache1
10be8 2e 73 7a 53 6c 6f 74 20 26 26 20 70 63 61 63 68  .szSlot && pcach
10be9 65 31 2e 70 46 72 65 65 20 29 7b 0a 20 20 20 20  e1.pFree ){.    
10bea 61 73 73 65 72 74 28 20 70 63 61 63 68 65 31 2e  assert( pcache1.
10beb 69 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 70 20  isInit );.    p 
10bec 3d 20 28 50 67 48 64 72 31 20 2a 29 70 63 61 63  = (PgHdr1 *)pcac
10bed 68 65 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70  he1.pFree;.    p
10bee 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70  cache1.pFree = p
10bef 63 61 63 68 65 31 2e 70 46 72 65 65 2d 3e 70 4e  cache1.pFree->pN
10bf0 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ext;.    sqlite3
10bf1 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45  StatusSet(SQLITE
10bf2 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48  _STATUS_PAGECACH
10bf3 45 5f 53 49 5a 45 2c 20 6e 42 79 74 65 29 3b 0a  E_SIZE, nByte);.
10bf4 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75      sqlite3Statu
10bf5 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54  sAdd(SQLITE_STAT
10bf6 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45  US_PAGECACHE_USE
10bf7 44 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  D, 1);.  }else{.
10bf8 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  .    /* Allocate
10bf9 20 61 20 6e 65 77 20 62 75 66 66 65 72 20 75 73   a new buffer us
10bfa 69 6e 67 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  ing sqlite3Mallo
10bfb 63 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20  c. Before doing 
10bfc 73 6f 2c 20 65 78 69 74 20 74 68 65 0a 20 20 20  so, exit the.   
10bfd 20 2a 2a 20 67 6c 6f 62 61 6c 20 70 63 61 63 68   ** global pcach
10bfe 65 20 6d 75 74 65 78 20 61 6e 64 20 75 6e 6c 6f  e mutex and unlo
10bff 63 6b 20 74 68 65 20 70 61 67 65 72 2d 63 61 63  ck the pager-cac
10c00 68 65 20 6f 62 6a 65 63 74 20 70 43 61 63 68 65  he object pCache
10c01 2e 20 54 68 69 73 20 69 73 20 0a 20 20 20 20 2a  . This is .    *
10c02 2a 20 73 6f 20 74 68 61 74 20 69 66 20 74 68 65  * so that if the
10c03 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f   attempt to allo
10c04 63 61 74 65 20 61 20 6e 65 77 20 62 75 66 66 65  cate a new buffe
10c05 72 20 63 61 75 73 65 73 20 74 68 65 20 74 68 65  r causes the the
10c06 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 66 69 67 75   .    ** configu
10c07 72 65 64 20 73 6f 66 74 2d 68 65 61 70 2d 6c 69  red soft-heap-li
10c08 6d 69 74 20 74 6f 20 62 65 20 62 72 65 61 63 68  mit to be breach
10c09 65 64 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 70  ed, it will be p
10c0a 6f 73 73 69 62 6c 65 20 74 6f 0a 20 20 20 20 2a  ossible to.    *
10c0b 2a 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79  * reclaim memory
10c0c 20 66 72 6f 6d 20 74 68 69 73 20 70 61 67 65 72   from this pager
10c0d 2d 63 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20  -cache..    */. 
10c0e 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d     pcache1LeaveM
10c0f 75 74 65 78 28 29 3b 0a 20 20 20 20 70 20 3d 20  utex();.    p = 
10c10 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42  sqlite3Malloc(nB
10c11 79 74 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65  yte);.    pcache
10c12 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  1EnterMutex();. 
10c13 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20     if( p ){.    
10c14 20 20 69 6e 74 20 73 7a 20 3d 20 73 71 6c 69 74    int sz = sqlit
10c15 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b  e3MallocSize(p);
10c16 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74  .      sqlite3St
10c17 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53  atusAdd(SQLITE_S
10c18 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f  TATUS_PAGECACHE_
10c19 4f 56 45 52 46 4c 4f 57 2c 20 73 7a 29 3b 0a 20  OVERFLOW, sz);. 
10c1a 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
10c1b 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72  n p;.}../*.** Fr
10c1c 65 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20  ee an allocated 
10c1d 62 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20  buffer obtained 
10c1e 66 72 6f 6d 20 70 63 61 63 68 65 31 41 6c 6c 6f  from pcache1Allo
10c1f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  c()..*/.static v
10c20 6f 69 64 20 70 63 61 63 68 65 31 46 72 65 65 28  oid pcache1Free(
10c21 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65  void *p){.  asse
10c22 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
10c23 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d  x_held(pcache1.m
10c24 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70  utex) );.  if( p
10c25 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
10c26 69 66 28 20 70 3e 3d 70 63 61 63 68 65 31 2e 70  if( p>=pcache1.p
10c27 53 74 61 72 74 20 26 26 20 70 3c 70 63 61 63 68  Start && p<pcach
10c28 65 31 2e 70 45 6e 64 20 29 7b 0a 20 20 20 20 50  e1.pEnd ){.    P
10c29 67 46 72 65 65 73 6c 6f 74 20 2a 70 53 6c 6f 74  gFreeslot *pSlot
10c2a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61  ;.    sqlite3Sta
10c2b 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
10c2c 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55  ATUS_PAGECACHE_U
10c2d 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20 20 70 53  SED, -1);.    pS
10c2e 6c 6f 74 20 3d 20 28 50 67 46 72 65 65 73 6c 6f  lot = (PgFreeslo
10c2f 74 2a 29 70 3b 0a 20 20 20 20 70 53 6c 6f 74 2d  t*)p;.    pSlot-
10c30 3e 70 4e 65 78 74 20 3d 20 70 63 61 63 68 65 31  >pNext = pcache1
10c31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63 61 63  .pFree;.    pcac
10c32 68 65 31 2e 70 46 72 65 65 20 3d 20 70 53 6c 6f  he1.pFree = pSlo
10c33 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  t;.  }else{.    
10c34 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69  int iSize = sqli
10c35 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29  te3MallocSize(p)
10c36 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61  ;.    sqlite3Sta
10c37 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
10c38 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f  ATUS_PAGECACHE_O
10c39 56 45 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29  VERFLOW, -iSize)
10c3a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
10c3b 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ee(p);.  }.}../*
10c3c 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e  .** Allocate a n
10c3d 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69  ew page object i
10c3e 6e 69 74 69 61 6c 6c 79 20 61 73 73 6f 63 69 61  nitially associa
10c3f 74 65 64 20 77 69 74 68 20 63 61 63 68 65 20 70  ted with cache p
10c40 43 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  Cache..*/.static
10c41 20 50 67 48 64 72 31 20 2a 70 63 61 63 68 65 31   PgHdr1 *pcache1
10c42 41 6c 6c 6f 63 50 61 67 65 28 50 43 61 63 68 65  AllocPage(PCache
10c43 31 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e  1 *pCache){.  in
10c44 74 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66  t nByte = sizeof
10c45 28 50 67 48 64 72 31 29 20 2b 20 70 43 61 63 68  (PgHdr1) + pCach
10c46 65 2d 3e 73 7a 50 61 67 65 3b 0a 20 20 76 6f 69  e->szPage;.  voi
10c47 64 20 2a 70 50 67 20 3d 20 70 63 61 63 68 65 31  d *pPg = pcache1
10c48 41 6c 6c 6f 63 28 6e 42 79 74 65 29 3b 0a 20 20  Alloc(nByte);.  
10c49 50 67 48 64 72 31 20 2a 70 3b 0a 20 20 69 66 28  PgHdr1 *p;.  if(
10c4a 20 70 50 67 20 29 7b 0a 20 20 20 20 70 20 3d 20   pPg ){.    p = 
10c4b 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70  PAGE_TO_PGHDR1(p
10c4c 43 61 63 68 65 2c 20 70 50 67 29 3b 0a 20 20 20  Cache, pPg);.   
10c4d 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75   if( pCache->bPu
10c4e 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20  rgeable ){.     
10c4f 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e   pcache1.nCurren
10c50 74 50 61 67 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20  tPage++;.    }. 
10c51 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20   }else{.    p = 
10c52 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
10c53 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65  p;.}../*.** Free
10c54 20 61 20 70 61 67 65 20 6f 62 6a 65 63 74 20 61   a page object a
10c55 6c 6c 6f 63 61 74 65 64 20 62 79 20 70 63 61 63  llocated by pcac
10c56 68 65 31 41 6c 6c 6f 63 50 61 67 65 28 29 2e 0a  he1AllocPage()..
10c57 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65  **.** The pointe
10c58 72 20 69 73 20 61 6c 6c 6f 77 65 64 20 74 6f 20  r is allowed to 
10c59 62 65 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69  be NULL, which i
10c5a 73 20 70 72 75 64 65 6e 74 2e 20 20 42 75 74 20  s prudent.  But 
10c5b 69 74 20 74 75 72 6e 73 20 6f 75 74 0a 2a 2a 20  it turns out.** 
10c5c 74 68 61 74 20 74 68 65 20 63 75 72 72 65 6e 74  that the current
10c5d 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
10c5e 68 61 70 70 65 6e 73 20 74 6f 20 6e 65 76 65 72  happens to never
10c5f 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69   call this routi
10c60 6e 65 0a 2a 2a 20 77 69 74 68 20 61 20 4e 55 4c  ne.** with a NUL
10c61 4c 20 70 6f 69 6e 74 65 72 2c 20 73 6f 20 77 65  L pointer, so we
10c62 20 6d 61 72 6b 20 74 68 65 20 4e 55 4c 4c 20 74   mark the NULL t
10c63 65 73 74 20 77 69 74 68 20 41 4c 57 41 59 53 28  est with ALWAYS(
10c64 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
10c65 64 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67  d pcache1FreePag
10c66 65 28 50 67 48 64 72 31 20 2a 70 29 7b 0a 20 20  e(PgHdr1 *p){.  
10c67 69 66 28 20 41 4c 57 41 59 53 28 70 29 20 29 7b  if( ALWAYS(p) ){
10c68 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 43 61 63  .    if( p->pCac
10c69 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29  he->bPurgeable )
10c6a 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e  {.      pcache1.
10c6b 6e 43 75 72 72 65 6e 74 50 61 67 65 2d 2d 3b 0a  nCurrentPage--;.
10c6c 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65      }.    pcache
10c6d 31 46 72 65 65 28 50 47 48 44 52 31 5f 54 4f 5f  1Free(PGHDR1_TO_
10c6e 50 41 47 45 28 70 29 29 3b 0a 20 20 7d 0a 7d 0a  PAGE(p));.  }.}.
10c6f 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75  ./*.** Malloc fu
10c70 6e 63 74 69 6f 6e 20 75 73 65 64 20 62 79 20 53  nction used by S
10c71 51 4c 69 74 65 20 74 6f 20 6f 62 74 61 69 6e 20  QLite to obtain 
10c72 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62  space from the b
10c73 75 66 66 65 72 20 63 6f 6e 66 69 67 75 72 65 64  uffer configured
10c74 0a 2a 2a 20 75 73 69 6e 67 20 73 71 6c 69 74 65  .** using sqlite
10c75 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f  3_config(SQLITE_
10c76 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
10c77 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20  ) option. If no 
10c78 73 75 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 65  such buffer.** e
10c79 78 69 73 74 73 2c 20 74 68 69 73 20 66 75 6e 63  xists, this func
10c7a 74 69 6f 6e 20 66 61 6c 6c 73 20 62 61 63 6b 20  tion falls back 
10c7b 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  to sqlite3Malloc
10c7c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ()..*/.SQLITE_PR
10c7d 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
10c7e 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e  te3PageMalloc(in
10c7f 74 20 73 7a 29 7b 0a 20 20 76 6f 69 64 20 2a 70  t sz){.  void *p
10c80 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ;.  pcache1Enter
10c81 4d 75 74 65 78 28 29 3b 0a 20 20 70 20 3d 20 70  Mutex();.  p = p
10c82 63 61 63 68 65 31 41 6c 6c 6f 63 28 73 7a 29 3b  cache1Alloc(sz);
10c83 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d  .  pcache1LeaveM
10c84 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e  utex();.  return
10c85 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65   p;.}../*.** Fre
10c86 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62  e an allocated b
10c87 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66  uffer obtained f
10c88 72 6f 6d 20 73 71 6c 69 74 65 33 50 61 67 65 4d  rom sqlite3PageM
10c89 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49  alloc()..*/.SQLI
10c8a 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
10c8b 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28  sqlite3PageFree(
10c8c 76 6f 69 64 20 2a 70 29 7b 0a 20 20 70 63 61 63  void *p){.  pcac
10c8d 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  he1EnterMutex();
10c8e 0a 20 20 70 63 61 63 68 65 31 46 72 65 65 28 70  .  pcache1Free(p
10c8f 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76  );.  pcache1Leav
10c90 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 2a  eMutex();.}../**
10c91 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c92 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c93 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c94 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c95 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
10c96 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20  ******* General 
10c97 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 46  Implementation F
10c98 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a  unctions *******
10c99 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10c9a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
10c9b 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
10c9c 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72  ion is used to r
10c9d 65 73 69 7a 65 20 74 68 65 20 68 61 73 68 20 74  esize the hash t
10c9e 61 62 6c 65 20 75 73 65 64 20 62 79 20 74 68 65  able used by the
10c9f 20 63 61 63 68 65 20 70 61 73 73 65 64 0a 2a 2a   cache passed.**
10ca0 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72   as the first ar
10ca1 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  gument..**.** Th
10ca2 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d  e global mutex m
10ca3 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e  ust be held when
10ca4 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
10ca5 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61  s called..*/.sta
10ca6 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 52  tic int pcache1R
10ca7 65 73 69 7a 65 48 61 73 68 28 50 43 61 63 68 65  esizeHash(PCache
10ca8 31 20 2a 70 29 7b 0a 20 20 50 67 48 64 72 31 20  1 *p){.  PgHdr1 
10ca9 2a 2a 61 70 4e 65 77 3b 0a 20 20 75 6e 73 69 67  **apNew;.  unsig
10caa 6e 65 64 20 69 6e 74 20 6e 4e 65 77 3b 0a 20 20  ned int nNew;.  
10cab 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a  unsigned int i;.
10cac 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
10cad 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63  e3_mutex_held(pc
10cae 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a  ache1.mutex) );.
10caf 0a 20 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e 48 61  .  nNew = p->nHa
10cb0 73 68 2a 32 3b 0a 20 20 69 66 28 20 6e 4e 65 77  sh*2;.  if( nNew
10cb1 3c 32 35 36 20 29 7b 0a 20 20 20 20 6e 4e 65 77  <256 ){.    nNew
10cb2 20 3d 20 32 35 36 3b 0a 20 20 7d 0a 0a 20 20 70   = 256;.  }..  p
10cb3 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78  cache1LeaveMutex
10cb4 28 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 48 61  ();.  if( p->nHa
10cb5 73 68 20 29 7b 20 73 71 6c 69 74 65 33 42 65 67  sh ){ sqlite3Beg
10cb6 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  inBenignMalloc()
10cb7 3b 20 7d 0a 20 20 61 70 4e 65 77 20 3d 20 28 50  ; }.  apNew = (P
10cb8 67 48 64 72 31 20 2a 2a 29 73 71 6c 69 74 65 33  gHdr1 **)sqlite3
10cb9 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50  _malloc(sizeof(P
10cba 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b 0a  gHdr1 *)*nNew);.
10cbb 20 20 69 66 28 20 70 2d 3e 6e 48 61 73 68 20 29    if( p->nHash )
10cbc 7b 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69  { sqlite3EndBeni
10cbd 67 6e 4d 61 6c 6c 6f 63 28 29 3b 20 7d 0a 20 20  gnMalloc(); }.  
10cbe 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65  pcache1EnterMute
10cbf 78 28 29 3b 0a 20 20 69 66 28 20 61 70 4e 65 77  x();.  if( apNew
10cc0 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 61   ){.    memset(a
10cc1 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f 66 28  pNew, 0, sizeof(
10cc2 50 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b  PgHdr1 *)*nNew);
10cc3 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
10cc4 70 2d 3e 6e 48 61 73 68 3b 20 69 2b 2b 29 7b 0a  p->nHash; i++){.
10cc5 20 20 20 20 20 20 50 67 48 64 72 31 20 2a 70 50        PgHdr1 *pP
10cc6 61 67 65 3b 0a 20 20 20 20 20 20 50 67 48 64 72  age;.      PgHdr
10cc7 31 20 2a 70 4e 65 78 74 20 3d 20 70 2d 3e 61 70  1 *pNext = p->ap
10cc8 48 61 73 68 5b 69 5d 3b 0a 20 20 20 20 20 20 77  Hash[i];.      w
10cc9 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d 20 70  hile( (pPage = p
10cca 4e 65 78 74 29 21 3d 30 20 29 7b 0a 20 20 20 20  Next)!=0 ){.    
10ccb 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74      unsigned int
10ccc 20 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79   h = pPage->iKey
10ccd 20 25 20 6e 4e 65 77 3b 0a 20 20 20 20 20 20 20   % nNew;.       
10cce 20 70 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e   pNext = pPage->
10ccf 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70  pNext;.        p
10cd0 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 61 70  Page->pNext = ap
10cd1 4e 65 77 5b 68 5d 3b 0a 20 20 20 20 20 20 20 20  New[h];.        
10cd2 61 70 4e 65 77 5b 68 5d 20 3d 20 70 50 61 67 65  apNew[h] = pPage
10cd3 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
10cd4 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
10cd5 28 70 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 20  (p->apHash);.   
10cd6 20 70 2d 3e 61 70 48 61 73 68 20 3d 20 61 70 4e   p->apHash = apN
10cd7 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 48 61 73 68  ew;.    p->nHash
10cd8 20 3d 20 6e 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20   = nNew;.  }..  
10cd9 72 65 74 75 72 6e 20 28 70 2d 3e 61 70 48 61 73  return (p->apHas
10cda 68 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20  h ? SQLITE_OK : 
10cdb 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d  SQLITE_NOMEM);.}
10cdc 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
10cdd 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e  ction is used in
10cde 74 65 72 6e 61 6c 6c 79 20 74 6f 20 72 65 6d 6f  ternally to remo
10cdf 76 65 20 74 68 65 20 70 61 67 65 20 70 50 61 67  ve the page pPag
10ce0 65 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 67  e from the .** g
10ce1 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 2c 20  lobal LRU list, 
10ce2 69 66 20 69 73 20 70 61 72 74 20 6f 66 20 69 74  if is part of it
10ce3 2e 20 49 66 20 70 50 61 67 65 20 69 73 20 6e 6f  . If pPage is no
10ce4 74 20 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c  t part of the gl
10ce5 6f 62 61 6c 0a 2a 2a 20 4c 52 55 20 6c 69 73 74  obal.** LRU list
10ce6 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63  , then this func
10ce7 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tion is a no-op.
10ce8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61  .**.** The globa
10ce9 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  l mutex must be 
10cea 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66  held when this f
10ceb 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
10cec 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
10ced 64 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65  d pcache1PinPage
10cee 28 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b  (PgHdr1 *pPage){
10cef 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
10cf0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63  e3_mutex_held(pc
10cf1 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a  ache1.mutex) );.
10cf2 20 20 69 66 28 20 70 50 61 67 65 20 26 26 20 28    if( pPage && (
10cf3 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20  pPage->pLruNext 
10cf4 7c 7c 20 70 50 61 67 65 3d 3d 70 63 61 63 68 65  || pPage==pcache
10cf5 31 2e 70 4c 72 75 54 61 69 6c 29 20 29 7b 0a 20  1.pLruTail) ){. 
10cf6 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c     if( pPage->pL
10cf7 72 75 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20  ruPrev ){.      
10cf8 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 2d  pPage->pLruPrev-
10cf9 3e 70 4c 72 75 4e 65 78 74 20 3d 20 70 50 61 67  >pLruNext = pPag
10cfa 65 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20  e->pLruNext;.   
10cfb 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65   }.    if( pPage
10cfc 2d 3e 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20  ->pLruNext ){.  
10cfd 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e      pPage->pLruN
10cfe 65 78 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20  ext->pLruPrev = 
10cff 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b  pPage->pLruPrev;
10d00 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
10d01 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3d  cache1.pLruHead=
10d02 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20  =pPage ){.      
10d03 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64  pcache1.pLruHead
10d04 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65   = pPage->pLruNe
10d05 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  xt;.    }.    if
10d06 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61  ( pcache1.pLruTa
10d07 69 6c 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20  il==pPage ){.   
10d08 20 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54     pcache1.pLruT
10d09 61 69 6c 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72  ail = pPage->pLr
10d0a 75 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20  uPrev;.    }.   
10d0b 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74   pPage->pLruNext
10d0c 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d   = 0;.    pPage-
10d0d 3e 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20  >pLruPrev = 0;. 
10d0e 20 20 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65     pPage->pCache
10d0f 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2d 2d 3b  ->nRecyclable--;
10d10 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52  .  }.}.../*.** R
10d11 65 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 73  emove the page s
10d12 75 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72  upplied as an ar
10d13 67 75 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20  gument from the 
10d14 68 61 73 68 20 74 61 62 6c 65 20 0a 2a 2a 20 28  hash table .** (
10d15 50 43 61 63 68 65 31 2e 61 70 48 61 73 68 20 73  PCache1.apHash s
10d16 74 72 75 63 74 75 72 65 29 20 74 68 61 74 20 69  tructure) that i
10d17 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 73  t is currently s
10d18 74 6f 72 65 64 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20  tored in..**.** 
10d19 54 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78  The global mutex
10d1a 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68   must be held wh
10d1b 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
10d1c 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73   is called..*/.s
10d1d 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
10d1e 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68  e1RemoveFromHash
10d1f 28 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b  (PgHdr1 *pPage){
10d20 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
10d21 68 3b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43  h;.  PCache1 *pC
10d22 61 63 68 65 20 3d 20 70 50 61 67 65 2d 3e 70 43  ache = pPage->pC
10d23 61 63 68 65 3b 0a 20 20 50 67 48 64 72 31 20 2a  ache;.  PgHdr1 *
10d24 2a 70 70 3b 0a 0a 20 20 68 20 3d 20 70 50 61 67  *pp;..  h = pPag
10d25 65 2d 3e 69 4b 65 79 20 25 20 70 43 61 63 68 65  e->iKey % pCache
10d26 2d 3e 6e 48 61 73 68 3b 0a 20 20 66 6f 72 28 70  ->nHash;.  for(p
10d27 70 3d 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73  p=&pCache->apHas
10d28 68 5b 68 5d 3b 20 28 2a 70 70 29 21 3d 70 50 61  h[h]; (*pp)!=pPa
10d29 67 65 3b 20 70 70 3d 26 28 2a 70 70 29 2d 3e 70  ge; pp=&(*pp)->p
10d2a 4e 65 78 74 29 3b 0a 20 20 2a 70 70 20 3d 20 28  Next);.  *pp = (
10d2b 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20  *pp)->pNext;..  
10d2c 70 43 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d 3b  pCache->nPage--;
10d2d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
10d2e 72 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79  re are currently
10d2f 20 6d 6f 72 65 20 74 68 61 6e 20 70 63 61 63 68   more than pcach
10d30 65 2e 6e 4d 61 78 50 61 67 65 20 70 61 67 65 73  e.nMaxPage pages
10d31 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74 72 79 0a   allocated, try.
10d32 2a 2a 20 74 6f 20 72 65 63 79 63 6c 65 20 70 61  ** to recycle pa
10d33 67 65 73 20 74 6f 20 72 65 64 75 63 65 20 74 68  ges to reduce th
10d34 65 20 6e 75 6d 62 65 72 20 61 6c 6c 6f 63 61 74  e number allocat
10d35 65 64 20 74 6f 20 70 63 61 63 68 65 2e 6e 4d 61  ed to pcache.nMa
10d36 78 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  xPage..*/.static
10d37 20 76 6f 69 64 20 70 63 61 63 68 65 31 45 6e 66   void pcache1Enf
10d38 6f 72 63 65 4d 61 78 50 61 67 65 28 76 6f 69 64  orceMaxPage(void
10d39 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
10d3a 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
10d3b 70 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29  pcache1.mutex) )
10d3c 3b 0a 20 20 77 68 69 6c 65 28 20 70 63 61 63 68  ;.  while( pcach
10d3d 65 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e  e1.nCurrentPage>
10d3e 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65  pcache1.nMaxPage
10d3f 20 26 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75   && pcache1.pLru
10d40 54 61 69 6c 20 29 7b 0a 20 20 20 20 50 67 48 64  Tail ){.    PgHd
10d41 72 31 20 2a 70 20 3d 20 70 63 61 63 68 65 31 2e  r1 *p = pcache1.
10d42 70 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20 70 63  pLruTail;.    pc
10d43 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b  ache1PinPage(p);
10d44 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f  .    pcache1Remo
10d45 76 65 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20  veFromHash(p);. 
10d46 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61     pcache1FreePa
10d47 67 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ge(p);.  }.}../*
10d48 0a 2a 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20  .** Discard all 
10d49 70 61 67 65 73 20 66 72 6f 6d 20 63 61 63 68 65  pages from cache
10d4a 20 70 43 61 63 68 65 20 77 69 74 68 20 61 20 70   pCache with a p
10d4b 61 67 65 20 6e 75 6d 62 65 72 20 28 6b 65 79 20  age number (key 
10d4c 76 61 6c 75 65 29 20 0a 2a 2a 20 67 72 65 61 74  value) .** great
10d4d 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  er than or equal
10d4e 20 74 6f 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20   to iLimit. Any 
10d4f 70 69 6e 6e 65 64 20 70 61 67 65 73 20 74 68 61  pinned pages tha
10d50 74 20 6d 65 65 74 20 74 68 69 73 20 0a 2a 2a 20  t meet this .** 
10d51 63 72 69 74 65 72 69 61 20 61 72 65 20 75 6e 70  criteria are unp
10d52 69 6e 6e 65 64 20 62 65 66 6f 72 65 20 74 68 65  inned before the
10d53 79 20 61 72 65 20 64 69 73 63 61 72 64 65 64 2e  y are discarded.
10d54 0a 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61  .**.** The globa
10d55 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  l mutex must be 
10d56 68 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66  held when this f
10d57 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
10d58 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
10d59 64 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74  d pcache1Truncat
10d5a 65 55 6e 73 61 66 65 28 0a 20 20 50 43 61 63 68  eUnsafe(.  PCach
10d5b 65 31 20 2a 70 43 61 63 68 65 2c 20 0a 20 20 75  e1 *pCache, .  u
10d5c 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d  nsigned int iLim
10d5d 69 74 20 0a 29 7b 0a 20 20 54 45 53 54 4f 4e 4c  it .){.  TESTONL
10d5e 59 28 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  Y( unsigned int 
10d5f 6e 50 61 67 65 20 3d 20 30 3b 20 29 20 20 20 20  nPage = 0; )    
10d60 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 61 73 73    /* Used to ass
10d61 65 72 74 20 70 43 61 63 68 65 2d 3e 6e 50 61 67  ert pCache->nPag
10d62 65 20 69 73 20 63 6f 72 72 65 63 74 20 2a 2f 0a  e is correct */.
10d63 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68    unsigned int h
10d64 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
10d65 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
10d66 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b  cache1.mutex) );
10d67 0a 20 20 66 6f 72 28 68 3d 30 3b 20 68 3c 70 43  .  for(h=0; h<pC
10d68 61 63 68 65 2d 3e 6e 48 61 73 68 3b 20 68 2b 2b  ache->nHash; h++
10d69 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20 2a 2a  ){.    PgHdr1 **
10d6a 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70  pp = &pCache->ap
10d6b 48 61 73 68 5b 68 5d 3b 20 0a 20 20 20 20 50 67  Hash[h]; .    Pg
10d6c 48 64 72 31 20 2a 70 50 61 67 65 3b 0a 20 20 20  Hdr1 *pPage;.   
10d6d 20 77 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d   while( (pPage =
10d6e 20 2a 70 70 29 21 3d 30 20 29 7b 0a 20 20 20 20   *pp)!=0 ){.    
10d6f 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 4b 65    if( pPage->iKe
10d70 79 3e 3d 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20  y>=iLimit ){.   
10d71 20 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 50 61       pCache->nPa
10d72 67 65 2d 2d 3b 0a 20 20 20 20 20 20 20 20 2a 70  ge--;.        *p
10d73 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78 74  p = pPage->pNext
10d74 3b 0a 20 20 20 20 20 20 20 20 70 63 61 63 68 65  ;.        pcache
10d75 31 50 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b  1PinPage(pPage);
10d76 0a 20 20 20 20 20 20 20 20 70 63 61 63 68 65 31  .        pcache1
10d77 46 72 65 65 50 61 67 65 28 70 50 61 67 65 29 3b  FreePage(pPage);
10d78 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
10d79 20 20 20 20 20 20 70 70 20 3d 20 26 70 50 61 67        pp = &pPag
10d7a 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  e->pNext;.      
10d7b 20 20 54 45 53 54 4f 4e 4c 59 28 20 6e 50 61 67    TESTONLY( nPag
10d7c 65 2b 2b 3b 20 29 0a 20 20 20 20 20 20 7d 0a 20  e++; ).      }. 
10d7d 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72     }.  }.  asser
10d7e 74 28 20 70 43 61 63 68 65 2d 3e 6e 50 61 67 65  t( pCache->nPage
10d7f 3d 3d 6e 50 61 67 65 20 29 3b 0a 7d 0a 0a 2f 2a  ==nPage );.}../*
10d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d83 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
10d85 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69 74 65 33  ******** sqlite3
10d86 5f 70 63 61 63 68 65 20 4d 65 74 68 6f 64 73 20  _pcache Methods 
10d87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d88 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
10d89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
10d8a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
10d8b 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c  ation of the sql
10d8c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 49 6e 69  ite3_pcache.xIni
10d8d 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61  t method..*/.sta
10d8e 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 49  tic int pcache1I
10d8f 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65  nit(void *NotUse
10d90 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  d){.  UNUSED_PAR
10d91 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
10d92 0a 20 20 61 73 73 65 72 74 28 20 70 63 61 63 68  .  assert( pcach
10d93 65 31 2e 69 73 49 6e 69 74 3d 3d 30 20 29 3b 0a  e1.isInit==0 );.
10d94 20 20 6d 65 6d 73 65 74 28 26 70 63 61 63 68 65    memset(&pcache
10d95 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 63 61  1, 0, sizeof(pca
10d96 63 68 65 31 29 29 3b 0a 20 20 69 66 28 20 73 71  che1));.  if( sq
10d97 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
10d98 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a  g.bCoreMutex ){.
10d99 20 20 20 20 70 63 61 63 68 65 31 2e 6d 75 74 65      pcache1.mute
10d9a 78 20 3d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  x = sqlite3_mute
10d9b 78 5f 61 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  x_alloc(SQLITE_M
10d9c 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 29  UTEX_STATIC_LRU)
10d9d 3b 0a 20 20 7d 0a 20 20 70 63 61 63 68 65 31 2e  ;.  }.  pcache1.
10d9e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 72 65  isInit = 1;.  re
10d9f 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
10da0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
10da1 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ntation of the s
10da2 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 53  qlite3_pcache.xS
10da3 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 2e 0a  hutdown method..
10da4 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  ** Note that the
10da5 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 61 6c   static mutex al
10da6 6c 6f 63 61 74 65 64 20 69 6e 20 78 49 6e 69 74  located in xInit
10da7 20 64 6f 65 73 20 0a 2a 2a 20 6e 6f 74 20 6e 65   does .** not ne
10da8 65 64 20 74 6f 20 62 65 20 66 72 65 65 64 2e 0a  ed to be freed..
10da9 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
10daa 63 61 63 68 65 31 53 68 75 74 64 6f 77 6e 28 76  cache1Shutdown(v
10dab 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20  oid *NotUsed){. 
10dac 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
10dad 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 61 73  R(NotUsed);.  as
10dae 73 65 72 74 28 20 70 63 61 63 68 65 31 2e 69 73  sert( pcache1.is
10daf 49 6e 69 74 21 3d 30 20 29 3b 0a 20 20 6d 65 6d  Init!=0 );.  mem
10db0 73 65 74 28 26 70 63 61 63 68 65 31 2c 20 30 2c  set(&pcache1, 0,
10db1 20 73 69 7a 65 6f 66 28 70 63 61 63 68 65 31 29   sizeof(pcache1)
10db2 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  );.}../*.** Impl
10db3 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
10db4 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  e sqlite3_pcache
10db5 2e 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 2e  .xCreate method.
10db6 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  .**.** Allocate 
10db7 61 20 6e 65 77 20 63 61 63 68 65 2e 0a 2a 2f 0a  a new cache..*/.
10db8 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 70  static sqlite3_p
10db9 63 61 63 68 65 20 2a 70 63 61 63 68 65 31 43 72  cache *pcache1Cr
10dba 65 61 74 65 28 69 6e 74 20 73 7a 50 61 67 65 2c  eate(int szPage,
10dbb 20 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 29   int bPurgeable)
10dbc 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61  {.  PCache1 *pCa
10dbd 63 68 65 3b 0a 0a 20 20 70 43 61 63 68 65 20 3d  che;..  pCache =
10dbe 20 28 50 43 61 63 68 65 31 20 2a 29 73 71 6c 69   (PCache1 *)sqli
10dbf 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
10dc0 66 28 50 43 61 63 68 65 31 29 29 3b 0a 20 20 69  f(PCache1));.  i
10dc1 66 28 20 70 43 61 63 68 65 20 29 7b 0a 20 20 20  f( pCache ){.   
10dc2 20 6d 65 6d 73 65 74 28 70 43 61 63 68 65 2c 20   memset(pCache, 
10dc3 30 2c 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65  0, sizeof(PCache
10dc4 31 29 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d  1));.    pCache-
10dc5 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61 67 65  >szPage = szPage
10dc6 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 62 50  ;.    pCache->bP
10dc7 75 72 67 65 61 62 6c 65 20 3d 20 28 62 50 75 72  urgeable = (bPur
10dc8 67 65 61 62 6c 65 20 3f 20 31 20 3a 20 30 29 3b  geable ? 1 : 0);
10dc9 0a 20 20 20 20 69 66 28 20 62 50 75 72 67 65 61  .    if( bPurgea
10dca 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 43 61  ble ){.      pCa
10dcb 63 68 65 2d 3e 6e 4d 69 6e 20 3d 20 31 30 3b 0a  che->nMin = 10;.
10dcc 20 20 20 20 20 20 70 63 61 63 68 65 31 45 6e 74        pcache1Ent
10dcd 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20  erMutex();.     
10dce 20 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67   pcache1.nMinPag
10dcf 65 20 2b 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69  e += pCache->nMi
10dd0 6e 3b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31  n;.      pcache1
10dd1 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
10dd2 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
10dd3 20 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65   (sqlite3_pcache
10dd4 20 2a 29 70 43 61 63 68 65 3b 0a 7d 0a 0a 2f 2a   *)pCache;.}../*
10dd5 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
10dd6 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  on of the sqlite
10dd7 33 5f 70 63 61 63 68 65 2e 78 43 61 63 68 65 73  3_pcache.xCaches
10dd8 69 7a 65 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a  ize method. .**.
10dd9 2a 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65  ** Configure the
10dda 20 63 61 63 68 65 5f 73 69 7a 65 20 6c 69 6d 69   cache_size limi
10ddb 74 20 66 6f 72 20 61 20 63 61 63 68 65 2e 0a 2a  t for a cache..*
10ddc 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63  /.static void pc
10ddd 61 63 68 65 31 43 61 63 68 65 73 69 7a 65 28 73  ache1Cachesize(s
10dde 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70  qlite3_pcache *p
10ddf 2c 20 69 6e 74 20 6e 4d 61 78 29 7b 0a 20 20 50  , int nMax){.  P
10de0 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d  Cache1 *pCache =
10de1 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20   (PCache1 *)p;. 
10de2 20 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75   if( pCache->bPu
10de3 72 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 70  rgeable ){.    p
10de4 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78  cache1EnterMutex
10de5 28 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 2e  ();.    pcache1.
10de6 6e 4d 61 78 50 61 67 65 20 2b 3d 20 28 6e 4d 61  nMaxPage += (nMa
10de7 78 20 2d 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78  x - pCache->nMax
10de8 29 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e  );.    pCache->n
10de9 4d 61 78 20 3d 20 6e 4d 61 78 3b 0a 20 20 20 20  Max = nMax;.    
10dea 70 63 61 63 68 65 31 45 6e 66 6f 72 63 65 4d 61  pcache1EnforceMa
10deb 78 50 61 67 65 28 29 3b 0a 20 20 20 20 70 63 61  xPage();.    pca
10dec 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29  che1LeaveMutex()
10ded 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ;.  }.}../*.** I
10dee 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
10def 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61   the sqlite3_pca
10df0 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74 20 6d  che.xPagecount m
10df1 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 69  ethod. .*/.stati
10df2 63 20 69 6e 74 20 70 63 61 63 68 65 31 50 61 67  c int pcache1Pag
10df3 65 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 70  ecount(sqlite3_p
10df4 63 61 63 68 65 20 2a 70 29 7b 0a 20 20 69 6e 74  cache *p){.  int
10df5 20 6e 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74   n;.  pcache1Ent
10df6 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 6e 20 3d  erMutex();.  n =
10df7 20 28 28 50 43 61 63 68 65 31 20 2a 29 70 29 2d   ((PCache1 *)p)-
10df8 3e 6e 50 61 67 65 3b 0a 20 20 70 63 61 63 68 65  >nPage;.  pcache
10df9 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  1LeaveMutex();. 
10dfa 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a   return n;.}../*
10dfb 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
10dfc 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  on of the sqlite
10dfd 33 5f 70 63 61 63 68 65 2e 78 46 65 74 63 68 20  3_pcache.xFetch 
10dfe 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 46  method. .**.** F
10dff 65 74 63 68 20 61 20 70 61 67 65 20 62 79 20 6b  etch a page by k
10e00 65 79 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20  ey value..**.** 
10e01 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 61  Whether or not a
10e02 20 6e 65 77 20 70 61 67 65 20 6d 61 79 20 62 65   new page may be
10e03 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68   allocated by th
10e04 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 70 65  is function depe
10e05 6e 64 73 20 6f 6e 0a 2a 2a 20 74 68 65 20 76 61  nds on.** the va
10e06 6c 75 65 20 6f 66 20 74 68 65 20 63 72 65 61 74  lue of the creat
10e07 65 46 6c 61 67 20 61 72 67 75 6d 65 6e 74 2e 20  eFlag argument. 
10e08 20 30 20 6d 65 61 6e 73 20 64 6f 20 6e 6f 74 20   0 means do not 
10e09 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 0a 2a  allocate a new.*
10e0a 2a 20 70 61 67 65 2e 20 20 31 20 6d 65 61 6e 73  * page.  1 means
10e0b 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   allocate a new 
10e0c 70 61 67 65 20 69 66 20 73 70 61 63 65 20 69 73  page if space is
10e0d 20 65 61 73 69 6c 79 20 61 76 61 69 6c 61 62 6c   easily availabl
10e0e 65 2e 20 20 32 20 0a 2a 2a 20 6d 65 61 6e 73 20  e.  2 .** means 
10e0f 74 6f 20 74 72 79 20 72 65 61 6c 6c 79 20 68 61  to try really ha
10e10 72 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61  rd to allocate a
10e11 20 6e 65 77 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a   new page..**.**
10e12 20 46 6f 72 20 61 20 6e 6f 6e 2d 70 75 72 67 65   For a non-purge
10e13 61 62 6c 65 20 63 61 63 68 65 20 28 61 20 63 61  able cache (a ca
10e14 63 68 65 20 75 73 65 64 20 61 73 20 74 68 65 20  che used as the 
10e15 73 74 6f 72 61 67 65 20 66 6f 72 20 61 6e 20 69  storage for an i
10e16 6e 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61  n-memory.** data
10e17 62 61 73 65 29 20 74 68 65 72 65 20 69 73 20 72  base) there is r
10e18 65 61 6c 6c 79 20 6e 6f 20 64 69 66 66 65 72 65  eally no differe
10e19 6e 63 65 20 62 65 74 77 65 65 6e 20 63 72 65 61  nce between crea
10e1a 74 65 46 6c 61 67 20 31 20 61 6e 64 20 32 2e 20  teFlag 1 and 2. 
10e1b 20 53 6f 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69   So.** the calli
10e1c 6e 67 20 66 75 6e 63 74 69 6f 6e 20 28 70 63 61  ng function (pca
10e1d 63 68 65 2e 63 29 20 77 69 6c 6c 20 6e 65 76 65  che.c) will neve
10e1e 72 20 68 61 76 65 20 61 20 63 72 65 61 74 65 46  r have a createF
10e1f 6c 61 67 20 6f 66 20 31 20 6f 6e 0a 2a 2a 20 61  lag of 1 on.** a
10e20 20 6e 6f 6e 2d 70 75 72 67 61 62 6c 65 20 63 61   non-purgable ca
10e21 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  che..**.** There
10e22 20 61 72 65 20 74 68 72 65 65 20 64 69 66 66 65   are three diffe
10e23 72 65 6e 74 20 61 70 70 72 6f 61 63 68 65 73 20  rent approaches 
10e24 74 6f 20 6f 62 74 61 69 6e 69 6e 67 20 73 70 61  to obtaining spa
10e25 63 65 20 66 6f 72 20 61 20 70 61 67 65 2c 0a 2a  ce for a page,.*
10e26 2a 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74  * depending on t
10e27 68 65 20 76 61 6c 75 65 20 6f 66 20 70 61 72 61  he value of para
10e28 6d 65 74 65 72 20 63 72 65 61 74 65 46 6c 61 67  meter createFlag
10e29 20 28 77 68 69 63 68 20 6d 61 79 20 62 65 20 30   (which may be 0
10e2a 2c 20 31 20 6f 72 20 32 29 2e 0a 2a 2a 0a 2a 2a  , 1 or 2)..**.**
10e2b 20 20 20 31 2e 20 52 65 67 61 72 64 6c 65 73 73     1. Regardless
10e2c 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66   of the value of
10e2d 20 63 72 65 61 74 65 46 6c 61 67 2c 20 74 68 65   createFlag, the
10e2e 20 63 61 63 68 65 20 69 73 20 73 65 61 72 63 68   cache is search
10e2f 65 64 20 66 6f 72 20 61 20 0a 2a 2a 20 20 20 20  ed for a .**    
10e30 20 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65    copy of the re
10e31 71 75 65 73 74 65 64 20 70 61 67 65 2e 20 49 66  quested page. If
10e32 20 6f 6e 65 20 69 73 20 66 6f 75 6e 64 2c 20 69   one is found, i
10e33 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  t is returned..*
10e34 2a 0a 2a 2a 20 20 20 32 2e 20 49 66 20 63 72 65  *.**   2. If cre
10e35 61 74 65 46 6c 61 67 3d 3d 30 20 61 6e 64 20 74  ateFlag==0 and t
10e36 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61  he page is not a
10e37 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 63 61  lready in the ca
10e38 63 68 65 2c 20 4e 55 4c 4c 20 69 73 0a 2a 2a 20  che, NULL is.** 
10e39 20 20 20 20 20 72 65 74 75 72 6e 65 64 2e 0a 2a       returned..*
10e3a 2a 0a 2a 2a 20 20 20 33 2e 20 49 66 20 63 72 65  *.**   3. If cre
10e3b 61 74 65 46 6c 61 67 20 69 73 20 31 2c 20 61 6e  ateFlag is 1, an
10e3c 64 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f  d the page is no
10e3d 74 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  t already in the
10e3e 20 63 61 63 68 65 2c 0a 2a 2a 20 20 20 20 20 20   cache,.**      
10e3f 61 6e 64 20 69 66 20 65 69 74 68 65 72 20 6f 66  and if either of
10e40 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   the following a
10e41 72 65 20 74 72 75 65 2c 20 72 65 74 75 72 6e 20  re true, return 
10e42 4e 55 4c 4c 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  NULL:.**.**     
10e43 20 20 28 61 29 20 74 68 65 20 6e 75 6d 62 65 72    (a) the number
10e44 20 6f 66 20 70 61 67 65 73 20 70 69 6e 6e 65 64   of pages pinned
10e45 20 62 79 20 74 68 65 20 63 61 63 68 65 20 69 73   by the cache is
10e46 20 67 72 65 61 74 65 72 20 74 68 61 6e 0a 2a 2a   greater than.**
10e47 20 20 20 20 20 20 20 20 20 20 20 50 43 61 63 68             PCach
10e48 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 20 20  e1.nMax, or.**  
10e49 20 20 20 20 20 28 62 29 20 74 68 65 20 6e 75 6d       (b) the num
10e4a 62 65 72 20 6f 66 20 70 61 67 65 73 20 70 69 6e  ber of pages pin
10e4b 6e 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65  ned by the cache
10e4c 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
10e4d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 68  .**           th
10e4e 65 20 73 75 6d 20 6f 66 20 6e 4d 61 78 20 66 6f  e sum of nMax fo
10e4f 72 20 61 6c 6c 20 70 75 72 67 65 61 62 6c 65 20  r all purgeable 
10e50 63 61 63 68 65 73 2c 20 6c 65 73 73 20 74 68 65  caches, less the
10e51 20 73 75 6d 20 6f 66 20 0a 2a 2a 20 20 20 20 20   sum of .**     
10e52 20 20 20 20 20 20 6e 4d 69 6e 20 66 6f 72 20 61        nMin for a
10e53 6c 6c 20 6f 74 68 65 72 20 70 75 72 67 65 61 62  ll other purgeab
10e54 6c 65 20 63 61 63 68 65 73 2e 20 0a 2a 2a 0a 2a  le caches. .**.*
10e55 2a 20 20 20 34 2e 20 49 66 20 6e 6f 6e 65 20 6f  *   4. If none o
10e56 66 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65  f the first thre
10e57 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 70 70  e conditions app
10e58 6c 79 20 61 6e 64 20 74 68 65 20 63 61 63 68 65  ly and the cache
10e59 20 69 73 20 6d 61 72 6b 65 64 0a 2a 2a 20 20 20   is marked.**   
10e5a 20 20 20 61 73 20 70 75 72 67 65 61 62 6c 65 2c     as purgeable,
10e5b 20 61 6e 64 20 69 66 20 6f 6e 65 20 6f 66 20 74   and if one of t
10e5c 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20  he following is 
10e5d 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  true:.**.**     
10e5e 20 20 28 61 29 20 54 68 65 20 6e 75 6d 62 65 72    (a) The number
10e5f 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 63 61   of pages alloca
10e60 74 65 64 20 66 6f 72 20 74 68 65 20 63 61 63 68  ted for the cach
10e61 65 20 69 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a  e is already .**
10e62 20 20 20 20 20 20 20 20 20 20 20 50 43 61 63 68             PCach
10e63 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a 2a 0a 2a  e1.nMax, or.**.*
10e64 2a 20 20 20 20 20 20 20 28 62 29 20 54 68 65 20  *       (b) The 
10e65 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
10e66 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 6c  allocated for al
10e67 6c 20 70 75 72 67 65 61 62 6c 65 20 63 61 63 68  l purgeable cach
10e68 65 73 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20  es is.**        
10e69 20 20 20 61 6c 72 65 61 64 79 20 65 71 75 61 6c     already equal
10e6a 20 74 6f 20 6f 72 20 67 72 65 61 74 65 72 20 74   to or greater t
10e6b 68 61 6e 20 74 68 65 20 73 75 6d 20 6f 66 20 6e  han the sum of n
10e6c 4d 61 78 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 20  Max for all.**  
10e6d 20 20 20 20 20 20 20 20 20 70 75 72 67 65 61 62           purgeab
10e6e 6c 65 20 63 61 63 68 65 73 2c 0a 2a 2a 0a 2a 2a  le caches,.**.**
10e6f 20 20 20 20 20 20 74 68 65 6e 20 61 74 74 65 6d        then attem
10e70 70 74 20 74 6f 20 72 65 63 79 63 6c 65 20 61 20  pt to recycle a 
10e71 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 4c 52  page from the LR
10e72 55 20 6c 69 73 74 2e 20 49 66 20 69 74 20 69 73  U list. If it is
10e73 20 74 68 65 20 72 69 67 68 74 0a 2a 2a 20 20 20   the right.**   
10e74 20 20 20 73 69 7a 65 2c 20 72 65 74 75 72 6e 20     size, return 
10e75 74 68 65 20 72 65 63 79 63 6c 65 64 20 62 75 66  the recycled buf
10e76 66 65 72 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  fer. Otherwise, 
10e77 66 72 65 65 20 74 68 65 20 62 75 66 66 65 72 20  free the buffer 
10e78 61 6e 64 0a 2a 2a 20 20 20 20 20 20 70 72 6f 63  and.**      proc
10e79 65 65 64 20 74 6f 20 73 74 65 70 20 35 2e 20 0a  eed to step 5. .
10e7a 2a 2a 0a 2a 2a 20 20 20 35 2e 20 4f 74 68 65 72  **.**   5. Other
10e7b 77 69 73 65 2c 20 61 6c 6c 6f 63 61 74 65 20 61  wise, allocate a
10e7c 6e 64 20 72 65 74 75 72 6e 20 61 20 6e 65 77 20  nd return a new 
10e7d 70 61 67 65 20 62 75 66 66 65 72 2e 0a 2a 2f 0a  page buffer..*/.
10e7e 73 74 61 74 69 63 20 76 6f 69 64 20 2a 70 63 61  static void *pca
10e7f 63 68 65 31 46 65 74 63 68 28 73 71 6c 69 74 65  che1Fetch(sqlite
10e80 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 75 6e 73  3_pcache *p, uns
10e81 69 67 6e 65 64 20 69 6e 74 20 69 4b 65 79 2c 20  igned int iKey, 
10e82 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 29 7b  int createFlag){
10e83 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
10e84 6e 50 69 6e 6e 65 64 3b 0a 20 20 50 43 61 63 68  nPinned;.  PCach
10e85 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43  e1 *pCache = (PC
10e86 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 50 67 48  ache1 *)p;.  PgH
10e87 64 72 31 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a  dr1 *pPage = 0;.
10e88 0a 20 20 61 73 73 65 72 74 28 20 70 43 61 63 68  .  assert( pCach
10e89 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 7c 7c  e->bPurgeable ||
10e8a 20 63 72 65 61 74 65 46 6c 61 67 21 3d 31 20 29   createFlag!=1 )
10e8b 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ;.  pcache1Enter
10e8c 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28 20 63  Mutex();.  if( c
10e8d 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73  reateFlag==1 ) s
10e8e 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67  qlite3BeginBenig
10e8f 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 2f 2a  nMalloc();..  /*
10e90 20 53 65 61 72 63 68 20 74 68 65 20 68 61 73 68   Search the hash
10e91 20 74 61 62 6c 65 20 66 6f 72 20 61 6e 20 65 78   table for an ex
10e92 69 73 74 69 6e 67 20 65 6e 74 72 79 2e 20 2a 2f  isting entry. */
10e93 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d 3e 6e  .  if( pCache->n
10e94 48 61 73 68 3e 30 20 29 7b 0a 20 20 20 20 75 6e  Hash>0 ){.    un
10e95 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 69  signed int h = i
10e96 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48  Key % pCache->nH
10e97 61 73 68 3b 0a 20 20 20 20 66 6f 72 28 70 50 61  ash;.    for(pPa
10e98 67 65 3d 70 43 61 63 68 65 2d 3e 61 70 48 61 73  ge=pCache->apHas
10e99 68 5b 68 5d 3b 20 70 50 61 67 65 26 26 70 50 61  h[h]; pPage&&pPa
10e9a 67 65 2d 3e 69 4b 65 79 21 3d 69 4b 65 79 3b 20  ge->iKey!=iKey; 
10e9b 70 50 61 67 65 3d 70 50 61 67 65 2d 3e 70 4e 65  pPage=pPage->pNe
10e9c 78 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  xt);.  }..  if( 
10e9d 70 50 61 67 65 20 7c 7c 20 63 72 65 61 74 65 46  pPage || createF
10e9e 6c 61 67 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63  lag==0 ){.    pc
10e9f 61 63 68 65 31 50 69 6e 50 61 67 65 28 70 50 61  ache1PinPage(pPa
10ea0 67 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 66 65  ge);.    goto fe
10ea1 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20  tch_out;.  }..  
10ea2 2f 2a 20 53 74 65 70 20 33 20 6f 66 20 68 65 61  /* Step 3 of hea
10ea3 64 65 72 20 63 6f 6d 6d 65 6e 74 2e 20 2a 2f 0a  der comment. */.
10ea4 20 20 6e 50 69 6e 6e 65 64 20 3d 20 70 43 61 63    nPinned = pCac
10ea5 68 65 2d 3e 6e 50 61 67 65 20 2d 20 70 43 61 63  he->nPage - pCac
10ea6 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 3b  he->nRecyclable;
10ea7 0a 20 20 69 66 28 20 63 72 65 61 74 65 46 6c 61  .  if( createFla
10ea8 67 3d 3d 31 20 26 26 20 28 0a 20 20 20 20 20 20  g==1 && (.      
10ea9 20 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 63 61 63    nPinned>=(pcac
10eaa 68 65 31 2e 6e 4d 61 78 50 61 67 65 2b 70 43 61  he1.nMaxPage+pCa
10eab 63 68 65 2d 3e 6e 4d 69 6e 2d 70 63 61 63 68 65  che->nMin-pcache
10eac 31 2e 6e 4d 69 6e 50 61 67 65 29 0a 20 20 20 20  1.nMinPage).    
10ead 20 7c 7c 20 6e 50 69 6e 6e 65 64 3e 3d 28 70 43   || nPinned>=(pC
10eae 61 63 68 65 2d 3e 6e 4d 61 78 20 2a 20 39 20 2f  ache->nMax * 9 /
10eaf 20 31 30 29 0a 20 20 29 29 7b 0a 20 20 20 20 67   10).  )){.    g
10eb0 6f 74 6f 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20  oto fetch_out;. 
10eb1 20 7d 0a 0a 20 20 69 66 28 20 70 43 61 63 68 65   }..  if( pCache
10eb2 2d 3e 6e 50 61 67 65 3e 3d 70 43 61 63 68 65 2d  ->nPage>=pCache-
10eb3 3e 6e 48 61 73 68 20 26 26 20 70 63 61 63 68 65  >nHash && pcache
10eb4 31 52 65 73 69 7a 65 48 61 73 68 28 70 43 61 63  1ResizeHash(pCac
10eb5 68 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  he) ){.    goto 
10eb6 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  fetch_out;.  }..
10eb7 20 20 2f 2a 20 53 74 65 70 20 34 2e 20 54 72 79    /* Step 4. Try
10eb8 20 74 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61   to recycle a pa
10eb9 67 65 20 62 75 66 66 65 72 20 69 66 20 61 70 70  ge buffer if app
10eba 72 6f 70 72 69 61 74 65 2e 20 2a 2f 0a 20 20 69  ropriate. */.  i
10ebb 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67  f( pCache->bPurg
10ebc 65 61 62 6c 65 20 26 26 20 70 63 61 63 68 65 31  eable && pcache1
10ebd 2e 70 4c 72 75 54 61 69 6c 20 26 26 20 28 0a 20  .pLruTail && (. 
10ebe 20 20 20 20 28 70 43 61 63 68 65 2d 3e 6e 50 61      (pCache->nPa
10ebf 67 65 2b 31 3e 3d 70 43 61 63 68 65 2d 3e 6e 4d  ge+1>=pCache->nM
10ec0 61 78 29 20 7c 7c 20 70 63 61 63 68 65 31 2e 6e  ax) || pcache1.n
10ec1 43 75 72 72 65 6e 74 50 61 67 65 3e 3d 70 63 61  CurrentPage>=pca
10ec2 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 0a 20 20  che1.nMaxPage.  
10ec3 29 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20  )){.    pPage = 
10ec4 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c  pcache1.pLruTail
10ec5 3b 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6d  ;.    pcache1Rem
10ec6 6f 76 65 46 72 6f 6d 48 61 73 68 28 70 50 61 67  oveFromHash(pPag
10ec7 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65 31 50  e);.    pcache1P
10ec8 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  inPage(pPage);. 
10ec9 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 43     if( pPage->pC
10eca 61 63 68 65 2d 3e 73 7a 50 61 67 65 21 3d 70 43  ache->szPage!=pC
10ecb 61 63 68 65 2d 3e 73 7a 50 61 67 65 20 29 7b 0a  ache->szPage ){.
10ecc 20 20 20 20 20 20 70 63 61 63 68 65 31 46 72 65        pcache1Fre
10ecd 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
10ece 20 20 20 20 70 50 61 67 65 20 3d 20 30 3b 0a 20      pPage = 0;. 
10ecf 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
10ed0 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74  pcache1.nCurrent
10ed1 50 61 67 65 20 2d 3d 20 28 70 50 61 67 65 2d 3e  Page -= (pPage->
10ed2 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62  pCache->bPurgeab
10ed3 6c 65 20 2d 20 70 43 61 63 68 65 2d 3e 62 50 75  le - pCache->bPu
10ed4 72 67 65 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a  rgeable);.    }.
10ed5 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35    }..  /* Step 5
10ed6 2e 20 49 66 20 61 20 75 73 61 62 6c 65 20 70 61  . If a usable pa
10ed7 67 65 20 62 75 66 66 65 72 20 68 61 73 20 73 74  ge buffer has st
10ed8 69 6c 6c 20 6e 6f 74 20 62 65 65 6e 20 66 6f 75  ill not been fou
10ed9 6e 64 2c 20 0a 20 20 2a 2a 20 61 74 74 65 6d 70  nd, .  ** attemp
10eda 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20  t to allocate a 
10edb 6e 65 77 20 6f 6e 65 2e 20 0a 20 20 2a 2f 0a 20  new one. .  */. 
10edc 20 69 66 28 20 21 70 50 61 67 65 20 29 7b 0a 20   if( !pPage ){. 
10edd 20 20 20 70 50 61 67 65 20 3d 20 70 63 61 63 68     pPage = pcach
10ede 65 31 41 6c 6c 6f 63 50 61 67 65 28 70 43 61 63  e1AllocPage(pCac
10edf 68 65 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  he);.  }..  if( 
10ee0 70 50 61 67 65 20 29 7b 0a 20 20 20 20 75 6e 73  pPage ){.    uns
10ee1 69 67 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 4b  igned int h = iK
10ee2 65 79 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61  ey % pCache->nHa
10ee3 73 68 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e  sh;.    pCache->
10ee4 6e 50 61 67 65 2b 2b 3b 0a 20 20 20 20 70 50 61  nPage++;.    pPa
10ee5 67 65 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b  ge->iKey = iKey;
10ee6 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4e 65 78  .    pPage->pNex
10ee7 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70 48 61  t = pCache->apHa
10ee8 73 68 5b 68 5d 3b 0a 20 20 20 20 70 50 61 67 65  sh[h];.    pPage
10ee9 2d 3e 70 43 61 63 68 65 20 3d 20 70 43 61 63 68  ->pCache = pCach
10eea 65 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 4c  e;.    pPage->pL
10eeb 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20  ruPrev = 0;.    
10eec 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20  pPage->pLruNext 
10eed 3d 20 30 3b 0a 20 20 20 20 2a 28 76 6f 69 64 20  = 0;.    *(void 
10eee 2a 2a 29 28 50 47 48 44 52 31 5f 54 4f 5f 50 41  **)(PGHDR1_TO_PA
10eef 47 45 28 70 50 61 67 65 29 29 20 3d 20 30 3b 0a  GE(pPage)) = 0;.
10ef0 20 20 20 20 70 43 61 63 68 65 2d 3e 61 70 48 61      pCache->apHa
10ef1 73 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a 20  sh[h] = pPage;. 
10ef2 20 7d 0a 0a 66 65 74 63 68 5f 6f 75 74 3a 0a 20   }..fetch_out:. 
10ef3 20 69 66 28 20 70 50 61 67 65 20 26 26 20 69 4b   if( pPage && iK
10ef4 65 79 3e 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b  ey>pCache->iMaxK
10ef5 65 79 20 29 7b 0a 20 20 20 20 70 43 61 63 68 65  ey ){.    pCache
10ef6 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69 4b 65 79  ->iMaxKey = iKey
10ef7 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 72 65 61  ;.  }.  if( crea
10ef8 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c 69  teFlag==1 ) sqli
10ef9 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
10efa 6f 63 28 29 3b 0a 20 20 70 63 61 63 68 65 31 4c  oc();.  pcache1L
10efb 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72  eaveMutex();.  r
10efc 65 74 75 72 6e 20 28 70 50 61 67 65 20 3f 20 50  eturn (pPage ? P
10efd 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70 50  GHDR1_TO_PAGE(pP
10efe 61 67 65 29 20 3a 20 30 29 3b 0a 7d 0a 0a 0a 2f  age) : 0);.}.../
10eff 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
10f00 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74  ion of the sqlit
10f01 65 33 5f 70 63 61 63 68 65 2e 78 55 6e 70 69 6e  e3_pcache.xUnpin
10f02 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 4d   method..**.** M
10f03 61 72 6b 20 61 20 70 61 67 65 20 61 73 20 75 6e  ark a page as un
10f04 70 69 6e 6e 65 64 20 28 65 6c 69 67 69 62 6c 65  pinned (eligible
10f05 20 66 6f 72 20 61 73 79 6e 63 68 72 6f 6e 6f 75   for asynchronou
10f06 73 20 72 65 63 79 63 6c 69 6e 67 29 2e 0a 2a 2f  s recycling)..*/
10f07 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61  .static void pca
10f08 63 68 65 31 55 6e 70 69 6e 28 73 71 6c 69 74 65  che1Unpin(sqlite
10f09 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 76 6f 69  3_pcache *p, voi
10f0a 64 20 2a 70 50 67 2c 20 69 6e 74 20 72 65 75 73  d *pPg, int reus
10f0b 65 55 6e 6c 69 6b 65 6c 79 29 7b 0a 20 20 50 43  eUnlikely){.  PC
10f0c 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20  ache1 *pCache = 
10f0d 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20  (PCache1 *)p;.  
10f0e 50 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20  PgHdr1 *pPage = 
10f0f 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70  PAGE_TO_PGHDR1(p
10f10 43 61 63 68 65 2c 20 70 50 67 29 3b 0a 20 0a 20  Cache, pPg);. . 
10f11 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
10f12 70 43 61 63 68 65 3d 3d 70 43 61 63 68 65 20 29  pCache==pCache )
10f13 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ;.  pcache1Enter
10f14 4d 75 74 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 49  Mutex();..  /* I
10f15 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20 74 6f  t is an error to
10f16 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74   call this funct
10f17 69 6f 6e 20 69 66 20 74 68 65 20 70 61 67 65 20  ion if the page 
10f18 69 73 20 61 6c 72 65 61 64 79 20 0a 20 20 2a 2a  is already .  **
10f19 20 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c 6f   part of the glo
10f1a 62 61 6c 20 4c 52 55 20 6c 69 73 74 2e 0a 20 20  bal LRU list..  
10f1b 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  */.  assert( pPa
10f1c 67 65 2d 3e 70 4c 72 75 50 72 65 76 3d 3d 30 20  ge->pLruPrev==0 
10f1d 26 26 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65  && pPage->pLruNe
10f1e 78 74 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  xt==0 );.  asser
10f1f 74 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48  t( pcache1.pLruH
10f20 65 61 64 21 3d 70 50 61 67 65 20 26 26 20 70 63  ead!=pPage && pc
10f21 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 21 3d  ache1.pLruTail!=
10f22 70 50 61 67 65 20 29 3b 0a 0a 20 20 69 66 28 20  pPage );..  if( 
10f23 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 20 7c 7c  reuseUnlikely ||
10f24 20 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e   pcache1.nCurren
10f25 74 50 61 67 65 3e 70 63 61 63 68 65 31 2e 6e 4d  tPage>pcache1.nM
10f26 61 78 50 61 67 65 20 29 7b 0a 20 20 20 20 70 63  axPage ){.    pc
10f27 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48  ache1RemoveFromH
10f28 61 73 68 28 70 50 61 67 65 29 3b 0a 20 20 20 20  ash(pPage);.    
10f29 70 63 61 63 68 65 31 46 72 65 65 50 61 67 65 28  pcache1FreePage(
10f2a 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b  pPage);.  }else{
10f2b 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20  .    /* Add the 
10f2c 70 61 67 65 20 74 6f 20 74 68 65 20 67 6c 6f 62  page to the glob
10f2d 61 6c 20 4c 52 55 20 6c 69 73 74 2e 20 4e 6f 72  al LRU list. Nor
10f2e 6d 61 6c 6c 79 2c 20 74 68 65 20 70 61 67 65 20  mally, the page 
10f2f 69 73 20 61 64 64 65 64 20 74 6f 0a 20 20 20 20  is added to.    
10f30 2a 2a 20 74 68 65 20 68 65 61 64 20 6f 66 20 74  ** the head of t
10f31 68 65 20 6c 69 73 74 20 28 6c 61 73 74 20 70 61  he list (last pa
10f32 67 65 20 74 6f 20 62 65 20 72 65 63 79 63 6c 65  ge to be recycle
10f33 64 29 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20  d). However, if 
10f34 74 68 65 20 0a 20 20 20 20 2a 2a 20 72 65 75 73  the .    ** reus
10f35 65 55 6e 6c 69 6b 65 6c 79 20 66 6c 61 67 20 70  eUnlikely flag p
10f36 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66 75  assed to this fu
10f37 6e 63 74 69 6f 6e 20 69 73 20 74 72 75 65 2c 20  nction is true, 
10f38 74 68 65 20 70 61 67 65 20 69 73 20 61 64 64 65  the page is adde
10f39 64 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20  d.    ** to the 
10f3a 74 61 69 6c 20 6f 66 20 74 68 65 20 6c 69 73 74  tail of the list
10f3b 20 28 66 69 72 73 74 20 70 61 67 65 20 74 6f 20   (first page to 
10f3c 62 65 20 72 65 63 79 63 6c 65 64 29 2e 0a 20 20  be recycled)..  
10f3d 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 63 61    */.    if( pca
10f3e 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20 29 7b  che1.pLruHead ){
10f3f 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70  .      pcache1.p
10f40 4c 72 75 48 65 61 64 2d 3e 70 4c 72 75 50 72 65  LruHead->pLruPre
10f41 76 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 20  v = pPage;.     
10f42 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74   pPage->pLruNext
10f43 20 3d 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48   = pcache1.pLruH
10f44 65 61 64 3b 0a 20 20 20 20 20 20 70 63 61 63 68  ead;.      pcach
10f45 65 31 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50  e1.pLruHead = pP
10f46 61 67 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  age;.    }else{.
10f47 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c        pcache1.pL
10f48 72 75 54 61 69 6c 20 3d 20 70 50 61 67 65 3b 0a  ruTail = pPage;.
10f49 20 20 20 20 20 20 70 63 61 63 68 65 31 2e 70 4c        pcache1.pL
10f4a 72 75 48 65 61 64 20 3d 20 70 50 61 67 65 3b 0a  ruHead = pPage;.
10f4b 20 20 20 20 7d 0a 20 20 20 20 70 43 61 63 68 65      }.    pCache
10f4c 2d 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2b 2b 3b  ->nRecyclable++;
10f4d 0a 20 20 7d 0a 0a 20 20 70 63 61 63 68 65 31 4c  .  }..  pcache1L
10f4e 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a  eaveMutex();.}..
10f4f 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
10f50 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69  tion of the sqli
10f51 74 65 33 5f 70 63 61 63 68 65 2e 78 52 65 6b 65  te3_pcache.xReke
10f52 79 20 6d 65 74 68 6f 64 2e 20 0a 2a 2f 0a 73 74  y method. .*/.st
10f53 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65  atic void pcache
10f54 31 52 65 6b 65 79 28 0a 20 20 73 71 6c 69 74 65  1Rekey(.  sqlite
10f55 33 5f 70 63 61 63 68 65 20 2a 70 2c 0a 20 20 76  3_pcache *p,.  v
10f56 6f 69 64 20 2a 70 50 67 2c 0a 20 20 75 6e 73 69  oid *pPg,.  unsi
10f57 67 6e 65 64 20 69 6e 74 20 69 4f 6c 64 2c 0a 20  gned int iOld,. 
10f58 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4e   unsigned int iN
10f59 65 77 0a 29 7b 0a 20 20 50 43 61 63 68 65 31 20  ew.){.  PCache1 
10f5a 2a 70 43 61 63 68 65 20 3d 20 28 50 43 61 63 68  *pCache = (PCach
10f5b 65 31 20 2a 29 70 3b 0a 20 20 50 67 48 64 72 31  e1 *)p;.  PgHdr1
10f5c 20 2a 70 50 61 67 65 20 3d 20 50 41 47 45 5f 54   *pPage = PAGE_T
10f5d 4f 5f 50 47 48 44 52 31 28 70 43 61 63 68 65 2c  O_PGHDR1(pCache,
10f5e 20 70 50 67 29 3b 0a 20 20 50 67 48 64 72 31 20   pPg);.  PgHdr1 
10f5f 2a 2a 70 70 3b 0a 20 20 75 6e 73 69 67 6e 65 64  **pp;.  unsigned
10f60 20 69 6e 74 20 68 3b 20 0a 20 20 61 73 73 65 72   int h; .  asser
10f61 74 28 20 70 50 61 67 65 2d 3e 69 4b 65 79 3d 3d  t( pPage->iKey==
10f62 69 4f 6c 64 20 29 3b 0a 20 20 61 73 73 65 72 74  iOld );.  assert
10f63 28 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 3d  ( pPage->pCache=
10f64 3d 70 43 61 63 68 65 20 29 3b 0a 0a 20 20 70 63  =pCache );..  pc
10f65 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28  ache1EnterMutex(
10f66 29 3b 0a 0a 20 20 68 20 3d 20 69 4f 6c 64 25 70  );..  h = iOld%p
10f67 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20 20  Cache->nHash;.  
10f68 70 70 20 3d 20 26 70 43 61 63 68 65 2d 3e 61 70  pp = &pCache->ap
10f69 48 61 73 68 5b 68 5d 3b 0a 20 20 77 68 69 6c 65  Hash[h];.  while
10f6a 28 20 28 2a 70 70 29 21 3d 70 50 61 67 65 20 29  ( (*pp)!=pPage )
10f6b 7b 0a 20 20 20 20 70 70 20 3d 20 26 28 2a 70 70  {.    pp = &(*pp
10f6c 29 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20  )->pNext;.  }.  
10f6d 2a 70 70 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65  *pp = pPage->pNe
10f6e 78 74 3b 0a 0a 20 20 68 20 3d 20 69 4e 65 77 25  xt;..  h = iNew%
10f6f 70 43 61 63 68 65 2d 3e 6e 48 61 73 68 3b 0a 20  pCache->nHash;. 
10f70 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20 69   pPage->iKey = i
10f71 4e 65 77 3b 0a 20 20 70 50 61 67 65 2d 3e 70 4e  New;.  pPage->pN
10f72 65 78 74 20 3d 20 70 43 61 63 68 65 2d 3e 61 70  ext = pCache->ap
10f73 48 61 73 68 5b 68 5d 3b 0a 20 20 70 43 61 63 68  Hash[h];.  pCach
10f74 65 2d 3e 61 70 48 61 73 68 5b 68 5d 20 3d 20 70  e->apHash[h] = p
10f75 50 61 67 65 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  Page;..  /* The 
10f76 78 52 65 6b 65 79 28 29 20 69 6e 74 65 72 66 61  xRekey() interfa
10f77 63 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20  ce is only used 
10f78 74 6f 20 6d 6f 76 65 20 70 61 67 65 73 20 65 61  to move pages ea
10f79 72 6c 69 65 72 20 69 6e 20 74 68 65 0a 20 20 2a  rlier in the.  *
10f7a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  * database file 
10f7b 28 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 6f 76  (in order to mov
10f7c 65 20 61 6c 6c 20 66 72 65 65 20 70 61 67 65 73  e all free pages
10f7d 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74   to the end of t
10f7e 68 65 0a 20 20 2a 2a 20 66 69 6c 65 20 77 68 65  he.  ** file whe
10f7f 72 65 20 74 68 65 79 20 63 61 6e 20 62 65 20 74  re they can be t
10f80 72 75 6e 63 61 74 65 64 20 6f 66 66 2e 29 20 20  runcated off.)  
10f81 48 65 6e 63 65 2c 20 69 74 20 69 73 20 6e 6f 74  Hence, it is not
10f82 20 70 6f 73 73 69 62 6c 65 0a 20 20 2a 2a 20 66   possible.  ** f
10f83 6f 72 20 74 68 65 20 6e 65 77 20 70 61 67 65 20  or the new page 
10f84 6e 75 6d 62 65 72 20 74 6f 20 62 65 20 67 72 65  number to be gre
10f85 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 6c 61  ater than the la
10f86 72 67 65 73 74 20 70 72 65 76 69 6f 75 73 6c 79  rgest previously
10f87 0a 20 20 2a 2a 20 66 65 74 63 68 65 64 20 70 61  .  ** fetched pa
10f88 67 65 2e 20 20 42 75 74 20 77 65 20 72 65 74 61  ge.  But we reta
10f89 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  in the following
10f8a 20 74 65 73 74 20 69 6e 20 63 61 73 65 20 78 52   test in case xR
10f8b 65 6b 65 79 28 29 0a 20 20 2a 2a 20 62 65 67 69  ekey().  ** begi
10f8c 6e 73 20 74 6f 20 62 65 20 75 73 65 64 20 69 6e  ns to be used in
10f8d 20 64 69 66 66 65 72 65 6e 74 20 77 61 79 73 20   different ways 
10f8e 69 6e 20 74 68 65 20 66 75 74 75 72 65 2e 0a 20  in the future.. 
10f8f 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28   */.  if( NEVER(
10f90 69 4e 65 77 3e 70 43 61 63 68 65 2d 3e 69 4d 61  iNew>pCache->iMa
10f91 78 4b 65 79 29 20 29 7b 0a 20 20 20 20 70 43 61  xKey) ){.    pCa
10f92 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20 3d 20 69  che->iMaxKey = i
10f93 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 70 63 61 63  New;.  }..  pcac
10f94 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  he1LeaveMutex();
10f95 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
10f96 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
10f97 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78  sqlite3_pcache.x
10f98 54 72 75 6e 63 61 74 65 20 6d 65 74 68 6f 64 2e  Truncate method.
10f99 20 0a 2a 2a 0a 2a 2a 20 44 69 73 63 61 72 64 20   .**.** Discard 
10f9a 61 6c 6c 20 75 6e 70 69 6e 6e 65 64 20 70 61 67  all unpinned pag
10f9b 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 20  es in the cache 
10f9c 77 69 74 68 20 61 20 70 61 67 65 20 6e 75 6d 62  with a page numb
10f9d 65 72 20 65 71 75 61 6c 20 74 6f 0a 2a 2a 20 6f  er equal to.** o
10f9e 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 70  r greater than p
10f9f 61 72 61 6d 65 74 65 72 20 69 4c 69 6d 69 74 2e  arameter iLimit.
10fa0 20 41 6e 79 20 70 69 6e 6e 65 64 20 70 61 67 65   Any pinned page
10fa1 73 20 77 69 74 68 20 61 20 70 61 67 65 20 6e 75  s with a page nu
10fa2 6d 62 65 72 0a 2a 2a 20 65 71 75 61 6c 20 74 6f  mber.** equal to
10fa3 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e   or greater than
10fa4 20 69 4c 69 6d 69 74 20 61 72 65 20 69 6d 70 6c   iLimit are impl
10fa5 69 63 69 74 6c 79 20 75 6e 70 69 6e 6e 65 64 2e  icitly unpinned.
10fa6 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
10fa7 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 28  pcache1Truncate(
10fa8 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a  sqlite3_pcache *
10fa9 70 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  p, unsigned int 
10faa 69 4c 69 6d 69 74 29 7b 0a 20 20 50 43 61 63 68  iLimit){.  PCach
10fab 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50 43  e1 *pCache = (PC
10fac 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 70 63 61  ache1 *)p;.  pca
10fad 63 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29  che1EnterMutex()
10fae 3b 0a 20 20 69 66 28 20 69 4c 69 6d 69 74 3c 3d  ;.  if( iLimit<=
10faf 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65 79 20  pCache->iMaxKey 
10fb0 29 7b 0a 20 20 20 20 70 63 61 63 68 65 31 54 72  ){.    pcache1Tr
10fb1 75 6e 63 61 74 65 55 6e 73 61 66 65 28 70 43 61  uncateUnsafe(pCa
10fb2 63 68 65 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20  che, iLimit);.  
10fb3 20 20 70 43 61 63 68 65 2d 3e 69 4d 61 78 4b 65    pCache->iMaxKe
10fb4 79 20 3d 20 69 4c 69 6d 69 74 2d 31 3b 0a 20 20  y = iLimit-1;.  
10fb5 7d 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65  }.  pcache1Leave
10fb6 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Mutex();.}../*.*
10fb7 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
10fb8 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
10fb9 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79 20  pcache.xDestroy 
10fba 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 44  method. .**.** D
10fbb 65 73 74 72 6f 79 20 61 20 63 61 63 68 65 20 61  estroy a cache a
10fbc 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 70  llocated using p
10fbd 63 61 63 68 65 31 43 72 65 61 74 65 28 29 2e 0a  cache1Create()..
10fbe 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
10fbf 63 61 63 68 65 31 44 65 73 74 72 6f 79 28 73 71  cache1Destroy(sq
10fc0 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 29  lite3_pcache *p)
10fc1 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61  {.  PCache1 *pCa
10fc2 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a  che = (PCache1 *
10fc3 29 70 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74  )p;.  pcache1Ent
10fc4 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61  erMutex();.  pca
10fc5 63 68 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61  che1TruncateUnsa
10fc6 66 65 28 70 43 61 63 68 65 2c 20 30 29 3b 0a 20  fe(pCache, 0);. 
10fc7 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67   pcache1.nMaxPag
10fc8 65 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 61  e -= pCache->nMa
10fc9 78 3b 0a 20 20 70 63 61 63 68 65 31 2e 6e 4d 69  x;.  pcache1.nMi
10fca 6e 50 61 67 65 20 2d 3d 20 70 43 61 63 68 65 2d  nPage -= pCache-
10fcb 3e 6e 4d 69 6e 3b 0a 20 20 70 63 61 63 68 65 31  >nMin;.  pcache1
10fcc 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 28 29  EnforceMaxPage()
10fcd 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65  ;.  pcache1Leave
10fce 4d 75 74 65 78 28 29 3b 0a 20 20 73 71 6c 69 74  Mutex();.  sqlit
10fcf 65 33 5f 66 72 65 65 28 70 43 61 63 68 65 2d 3e  e3_free(pCache->
10fd0 61 70 48 61 73 68 29 3b 0a 20 20 73 71 6c 69 74  apHash);.  sqlit
10fd1 65 33 5f 66 72 65 65 28 70 43 61 63 68 65 29 3b  e3_free(pCache);
10fd2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
10fd3 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
10fd4 64 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c  d during initial
10fd5 69 7a 61 74 69 6f 6e 20 28 73 71 6c 69 74 65 33  ization (sqlite3
10fd6 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 29 20 74  _initialize()) t
10fd7 6f 0a 2a 2a 20 69 6e 73 74 61 6c 6c 20 74 68 65  o.** install the
10fd8 20 64 65 66 61 75 6c 74 20 70 6c 75 67 67 61 62   default pluggab
10fd9 6c 65 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 2c  le cache module,
10fda 20 61 73 73 75 6d 69 6e 67 20 74 68 65 20 75 73   assuming the us
10fdb 65 72 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 61 6c  er has not.** al
10fdc 72 65 61 64 79 20 70 72 6f 76 69 64 65 64 20 61  ready provided a
10fdd 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 2e 0a 2a  n alternative..*
10fde 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
10fdf 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61   void sqlite3PCa
10fe0 63 68 65 53 65 74 44 65 66 61 75 6c 74 28 76 6f  cheSetDefault(vo
10fe1 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71  id){.  static sq
10fe2 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74  lite3_pcache_met
10fe3 68 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68  hods defaultMeth
10fe4 6f 64 73 20 3d 20 7b 0a 20 20 20 20 30 2c 20 20  ods = {.    0,  
10fe5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10fe6 20 20 20 20 20 2f 2a 20 70 41 72 67 20 2a 2f 0a       /* pArg */.
10fe7 20 20 20 20 70 63 61 63 68 65 31 49 6e 69 74 2c      pcache1Init,
10fe8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10fe9 78 49 6e 69 74 20 2a 2f 0a 20 20 20 20 70 63 61  xInit */.    pca
10fea 63 68 65 31 53 68 75 74 64 6f 77 6e 2c 20 20 20  che1Shutdown,   
10feb 20 20 20 20 20 20 2f 2a 20 78 53 68 75 74 64 6f        /* xShutdo
10fec 77 6e 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65  wn */.    pcache
10fed 31 43 72 65 61 74 65 2c 20 20 20 20 20 20 20 20  1Create,        
10fee 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20 2a 2f     /* xCreate */
10fef 0a 20 20 20 20 70 63 61 63 68 65 31 43 61 63 68  .    pcache1Cach
10ff0 65 73 69 7a 65 2c 20 20 20 20 20 20 20 20 2f 2a  esize,        /*
10ff1 20 78 43 61 63 68 65 73 69 7a 65 20 2a 2f 0a 20   xCachesize */. 
10ff2 20 20 20 70 63 61 63 68 65 31 50 61 67 65 63 6f     pcache1Pageco
10ff3 75 6e 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 78  unt,        /* x
10ff4 50 61 67 65 63 6f 75 6e 74 20 2a 2f 0a 20 20 20  Pagecount */.   
10ff5 20 70 63 61 63 68 65 31 46 65 74 63 68 2c 20 20   pcache1Fetch,  
10ff6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 65            /* xFe
10ff7 74 63 68 20 2a 2f 0a 20 20 20 20 70 63 61 63 68  tch */.    pcach
10ff8 65 31 55 6e 70 69 6e 2c 20 20 20 20 20 20 20 20  e1Unpin,        
10ff9 20 20 20 20 2f 2a 20 78 55 6e 70 69 6e 20 2a 2f      /* xUnpin */
10ffa 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6b 65  .    pcache1Reke
10ffb 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  y,            /*
10ffc 20 78 52 65 6b 65 79 20 2a 2f 0a 20 20 20 20 70   xRekey */.    p
10ffd 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 2c 20  cache1Truncate, 
10ffe 20 20 20 20 20 20 20 20 2f 2a 20 78 54 72 75 6e          /* xTrun
10fff 63 61 74 65 20 2a 2f 0a 20 20 20 20 70 63 61 63  cate */.    pcac
11000 68 65 31 44 65 73 74 72 6f 79 20 20 20 20 20 20  he1Destroy      
11001 20 20 20 20 20 2f 2a 20 78 44 65 73 74 72 6f 79       /* xDestroy
11002 20 2a 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74   */.  };.  sqlit
11003 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45  e3_config(SQLITE
11004 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 2c 20  _CONFIG_PCACHE, 
11005 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29  &defaultMethods)
11006 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
11007 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59  TE_ENABLE_MEMORY
11008 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a 2a  _MANAGEMENT./*.*
11009 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
1100a 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 66 72 65  is called to fre
1100b 65 20 73 75 70 65 72 66 6c 75 6f 75 73 20 64 79  e superfluous dy
1100c 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
1100d 74 65 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 65  ted memory.** he
1100e 6c 64 20 62 79 20 74 68 65 20 70 61 67 65 72 20  ld by the pager 
1100f 73 79 73 74 65 6d 2e 20 4d 65 6d 6f 72 79 20 69  system. Memory i
11010 6e 20 75 73 65 20 62 79 20 61 6e 79 20 53 51 4c  n use by any SQL
11011 69 74 65 20 70 61 67 65 72 20 61 6c 6c 6f 63 61  ite pager alloca
11012 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 75  ted.** by the cu
11013 72 72 65 6e 74 20 74 68 72 65 61 64 20 6d 61 79  rrent thread may
11014 20 62 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65   be sqlite3_free
11015 28 29 65 64 2e 0a 2a 2a 0a 2a 2a 20 6e 52 65 71  ()ed..**.** nReq
11016 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
11017 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72  f bytes of memor
11018 79 20 72 65 71 75 69 72 65 64 2e 20 4f 6e 63 65  y required. Once
11019 20 74 68 69 73 20 6d 75 63 68 20 68 61 73 0a 2a   this much has.*
1101a 2a 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2c  * been released,
1101b 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65   the function re
1101c 74 75 72 6e 73 2e 20 54 68 65 20 72 65 74 75 72  turns. The retur
1101d 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 74  n value is the t
1101e 6f 74 61 6c 20 6e 75 6d 62 65 72 20 0a 2a 2a 20  otal number .** 
1101f 6f 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f  of bytes of memo
11020 72 79 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2f 0a  ry released..*/.
11021 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
11022 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  nt sqlite3Pcache
11023 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e  ReleaseMemory(in
11024 74 20 6e 52 65 71 29 7b 0a 20 20 69 6e 74 20 6e  t nReq){.  int n
11025 46 72 65 65 20 3d 20 30 3b 0a 20 20 69 66 28 20  Free = 0;.  if( 
11026 70 63 61 63 68 65 31 2e 70 53 74 61 72 74 3d 3d  pcache1.pStart==
11027 30 20 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20  0 ){.    PgHdr1 
11028 2a 70 3b 0a 20 20 20 20 70 63 61 63 68 65 31 45  *p;.    pcache1E
11029 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20  nterMutex();.   
1102a 20 77 68 69 6c 65 28 20 28 6e 52 65 71 3c 30 20   while( (nReq<0 
1102b 7c 7c 20 6e 46 72 65 65 3c 6e 52 65 71 29 20 26  || nFree<nReq) &
1102c 26 20 28 70 3d 70 63 61 63 68 65 31 2e 70 4c 72  & (p=pcache1.pLr
1102d 75 54 61 69 6c 29 20 29 7b 0a 20 20 20 20 20 20  uTail) ){.      
1102e 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69 74 65 33  nFree += sqlite3
1102f 4d 61 6c 6c 6f 63 53 69 7a 65 28 50 47 48 44 52  MallocSize(PGHDR
11030 31 5f 54 4f 5f 50 41 47 45 28 70 29 29 3b 0a 20  1_TO_PAGE(p));. 
11031 20 20 20 20 20 70 63 61 63 68 65 31 50 69 6e 50       pcache1PinP
11032 61 67 65 28 70 29 3b 0a 20 20 20 20 20 20 70 63  age(p);.      pc
11033 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48  ache1RemoveFromH
11034 61 73 68 28 70 29 3b 0a 20 20 20 20 20 20 70 63  ash(p);.      pc
11035 61 63 68 65 31 46 72 65 65 50 61 67 65 28 70 29  ache1FreePage(p)
11036 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63  ;.    }.    pcac
11037 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  he1LeaveMutex();
11038 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 46  .  }.  return nF
11039 72 65 65 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  ree;.}.#endif /*
1103a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
1103b 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
1103c 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   */..#ifdef SQLI
1103d 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68  TE_TEST./*.** Th
1103e 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
1103f 73 65 64 20 62 79 20 74 65 73 74 20 70 72 6f 63  sed by test proc
11040 65 64 75 72 65 73 20 74 6f 20 69 6e 73 70 65 63  edures to inspec
11041 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73  t the internal s
11042 74 61 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 67  tate.** of the g
11043 6c 6f 62 61 6c 20 63 61 63 68 65 2e 0a 2a 2f 0a  lobal cache..*/.
11044 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
11045 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
11046 65 53 74 61 74 73 28 0a 20 20 69 6e 74 20 2a 70  eStats(.  int *p
11047 6e 43 75 72 72 65 6e 74 2c 20 20 20 20 20 20 2f  nCurrent,      /
11048 2a 20 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 6d  * OUT: Total num
11049 62 65 72 20 6f 66 20 70 61 67 65 73 20 63 61 63  ber of pages cac
1104a 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  hed */.  int *pn
1104b 4d 61 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Max,          /*
1104c 20 4f 55 54 3a 20 47 6c 6f 62 61 6c 20 6d 61 78   OUT: Global max
1104d 69 6d 75 6d 20 63 61 63 68 65 20 73 69 7a 65 20  imum cache size 
1104e 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d 69 6e 2c  */.  int *pnMin,
1104f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
11050 3a 20 53 75 6d 20 6f 66 20 50 43 61 63 68 65 31  : Sum of PCache1
11051 2e 6e 4d 69 6e 20 66 6f 72 20 70 75 72 67 65 61  .nMin for purgea
11052 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20  ble caches */.  
11053 69 6e 74 20 2a 70 6e 52 65 63 79 63 6c 61 62 6c  int *pnRecyclabl
11054 65 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 74  e    /* OUT: Tot
11055 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  al number of pag
11056 65 73 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72  es available for
11057 20 72 65 63 79 63 6c 69 6e 67 20 2a 2f 0a 29 7b   recycling */.){
11058 0a 20 20 50 67 48 64 72 31 20 2a 70 3b 0a 20 20  .  PgHdr1 *p;.  
11059 69 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 20  int nRecyclable 
1105a 3d 20 30 3b 0a 20 20 66 6f 72 28 70 3d 70 63 61  = 0;.  for(p=pca
1105b 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3b 20 70  che1.pLruHead; p
1105c 3b 20 70 3d 70 2d 3e 70 4c 72 75 4e 65 78 74 29  ; p=p->pLruNext)
1105d 7b 0a 20 20 20 20 6e 52 65 63 79 63 6c 61 62 6c  {.    nRecyclabl
1105e 65 2b 2b 3b 0a 20 20 7d 0a 20 20 2a 70 6e 43 75  e++;.  }.  *pnCu
1105f 72 72 65 6e 74 20 3d 20 70 63 61 63 68 65 31 2e  rrent = pcache1.
11060 6e 43 75 72 72 65 6e 74 50 61 67 65 3b 0a 20 20  nCurrentPage;.  
11061 2a 70 6e 4d 61 78 20 3d 20 70 63 61 63 68 65 31  *pnMax = pcache1
11062 2e 6e 4d 61 78 50 61 67 65 3b 0a 20 20 2a 70 6e  .nMaxPage;.  *pn
11063 4d 69 6e 20 3d 20 70 63 61 63 68 65 31 2e 6e 4d  Min = pcache1.nM
11064 69 6e 50 61 67 65 3b 0a 20 20 2a 70 6e 52 65 63  inPage;.  *pnRec
11065 79 63 6c 61 62 6c 65 20 3d 20 6e 52 65 63 79 63  yclable = nRecyc
11066 6c 61 62 6c 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a  lable;.}.#endif.
11067 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
11068 20 45 6e 64 20 6f 66 20 70 63 61 63 68 65 31 2e   End of pcache1.
11069 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1106a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1106b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1106c 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1106d 20 42 65 67 69 6e 20 66 69 6c 65 20 72 6f 77 73   Begin file rows
1106e 65 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  et.c ***********
1106f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
11071 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 44 65 63 65  ./*.** 2008 Dece
11072 6d 62 65 72 20 33 0a 2a 2a 0a 2a 2a 20 54 68 65  mber 3.**.** The
11073 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
11074 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
11075 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
11076 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
11077 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
11078 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
11079 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
1107a 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
1107b 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
1107c 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
1107d 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
1107e 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
1107f 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
11080 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
11081 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
11082 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
11083 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
11084 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11085 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11086 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11087 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11088 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
11089 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
1108a 74 73 20 61 6e 20 6f 62 6a 65 63 74 20 77 65 20  ts an object we 
1108b 63 61 6c 6c 20 61 20 22 52 6f 77 53 65 74 22 2e  call a "RowSet".
1108c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 52 6f 77 53 65  .**.** The RowSe
1108d 74 20 6f 62 6a 65 63 74 20 69 73 20 61 20 63 6f  t object is a co
1108e 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 72 6f 77 69  llection of rowi
1108f 64 73 2e 20 20 52 6f 77 69 64 73 0a 2a 2a 20 61  ds.  Rowids.** a
11090 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f  re inserted into
11091 20 74 68 65 20 52 6f 77 53 65 74 20 69 6e 20 61   the RowSet in a
11092 6e 20 61 72 62 69 74 72 61 72 79 20 6f 72 64 65  n arbitrary orde
11093 72 2e 20 20 49 6e 73 65 72 74 73 0a 2a 2a 20 63  r.  Inserts.** c
11094 61 6e 20 62 65 20 69 6e 74 65 72 6d 69 78 65 64  an be intermixed
11095 20 77 69 74 68 20 74 65 73 74 73 20 74 6f 20 73   with tests to s
11096 65 65 20 69 66 20 61 20 67 69 76 65 6e 20 72 6f  ee if a given ro
11097 77 69 64 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20  wid has been.** 
11098 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 73 65 72  previously inser
11099 74 65 64 20 69 6e 74 6f 20 74 68 65 20 52 6f 77  ted into the Row
1109a 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72  Set..**.** After
1109b 20 61 6c 6c 20 69 6e 73 65 72 74 73 20 61 72 65   all inserts are
1109c 20 66 69 6e 69 73 68 65 64 2c 20 69 74 20 69 73   finished, it is
1109d 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 65 78 74   possible to ext
1109e 72 61 63 74 20 74 68 65 0a 2a 2a 20 65 6c 65 6d  ract the.** elem
1109f 65 6e 74 73 20 6f 66 20 74 68 65 20 52 6f 77 53  ents of the RowS
110a0 65 74 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64  et in sorted ord
110a1 65 72 2e 20 20 4f 6e 63 65 20 74 68 69 73 20 65  er.  Once this e
110a2 78 74 72 61 63 74 69 6f 6e 0a 2a 2a 20 70 72 6f  xtraction.** pro
110a3 63 65 73 73 20 68 61 73 20 73 74 61 72 74 65 64  cess has started
110a4 2c 20 6e 6f 20 6e 65 77 20 65 6c 65 6d 65 6e 74  , no new element
110a5 73 20 6d 61 79 20 62 65 20 69 6e 73 65 72 74 65  s may be inserte
110a6 64 2e 0a 2a 2a 0a 2a 2a 20 48 65 6e 63 65 2c 20  d..**.** Hence, 
110a7 74 68 65 20 70 72 69 6d 69 74 69 76 65 20 6f 70  the primitive op
110a8 65 72 61 74 69 6f 6e 73 20 66 6f 72 20 61 20 52  erations for a R
110a9 6f 77 53 65 74 20 61 72 65 3a 0a 2a 2a 0a 2a 2a  owSet are:.**.**
110aa 20 20 20 20 43 52 45 41 54 45 0a 2a 2a 20 20 20      CREATE.**   
110ab 20 49 4e 53 45 52 54 0a 2a 2a 20 20 20 20 54 45   INSERT.**    TE
110ac 53 54 0a 2a 2a 20 20 20 20 53 4d 41 4c 4c 45 53  ST.**    SMALLES
110ad 54 0a 2a 2a 20 20 20 20 44 45 53 54 52 4f 59 0a  T.**    DESTROY.
110ae 2a 2a 0a 2a 2a 20 54 68 65 20 43 52 45 41 54 45  **.** The CREATE
110af 20 61 6e 64 20 44 45 53 54 52 4f 59 20 70 72 69   and DESTROY pri
110b0 6d 69 74 69 76 65 73 20 61 72 65 20 74 68 65 20  mitives are the 
110b1 63 6f 6e 73 74 72 75 63 74 6f 72 20 61 6e 64 20  constructor and 
110b2 64 65 73 74 72 75 63 74 6f 72 2c 0a 2a 2a 20 6f  destructor,.** o
110b3 62 76 69 6f 75 73 6c 79 2e 20 20 54 68 65 20 49  bviously.  The I
110b4 4e 53 45 52 54 20 70 72 69 6d 69 74 69 76 65 20  NSERT primitive 
110b5 61 64 64 73 20 61 20 6e 65 77 20 65 6c 65 6d 65  adds a new eleme
110b6 6e 74 20 74 6f 20 74 68 65 20 52 6f 77 53 65 74  nt to the RowSet
110b7 2e 0a 2a 2a 20 54 45 53 54 20 63 68 65 63 6b 73  ..** TEST checks
110b8 20 74 6f 20 73 65 65 20 69 66 20 61 6e 20 65 6c   to see if an el
110b9 65 6d 65 6e 74 20 69 73 20 61 6c 72 65 61 64 79  ement is already
110ba 20 69 6e 20 74 68 65 20 52 6f 77 53 65 74 2e 20   in the RowSet. 
110bb 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20 65 78 74   SMALLEST.** ext
110bc 72 61 63 74 73 20 74 68 65 20 6c 65 61 73 74 20  racts the least 
110bd 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20 52  value from the R
110be 6f 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  owSet..**.** The
110bf 20 49 4e 53 45 52 54 20 70 72 69 6d 69 74 69 76   INSERT primitiv
110c0 65 20 6d 69 67 68 74 20 61 6c 6c 6f 63 61 74 65  e might allocate
110c1 20 61 64 64 69 74 69 6f 6e 61 6c 20 6d 65 6d 6f   additional memo
110c2 72 79 2e 20 20 4d 65 6d 6f 72 79 20 69 73 0a 2a  ry.  Memory is.*
110c3 2a 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 63  * allocated in c
110c4 68 75 6e 6b 73 20 73 6f 20 6d 6f 73 74 20 49 4e  hunks so most IN
110c5 53 45 52 54 73 20 64 6f 20 6e 6f 20 61 6c 6c 6f  SERTs do no allo
110c6 63 61 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 69  cation.  There i
110c7 73 20 61 6e 20 0a 2a 2a 20 75 70 70 65 72 20 62  s an .** upper b
110c8 6f 75 6e 64 20 6f 6e 20 74 68 65 20 73 69 7a 65  ound on the size
110c9 20 6f 66 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65   of allocated me
110ca 6d 6f 72 79 2e 20 20 4e 6f 20 6d 65 6d 6f 72 79  mory.  No memory
110cb 20 69 73 20 66 72 65 65 64 0a 2a 2a 20 75 6e 74   is freed.** unt
110cc 69 6c 20 44 45 53 54 52 4f 59 2e 0a 2a 2a 0a 2a  il DESTROY..**.*
110cd 2a 20 54 68 65 20 54 45 53 54 20 70 72 69 6d 69  * The TEST primi
110ce 74 69 76 65 20 69 6e 63 6c 75 64 65 73 20 61 20  tive includes a 
110cf 22 62 61 74 63 68 22 20 6e 75 6d 62 65 72 2e 20  "batch" number. 
110d0 20 54 68 65 20 54 45 53 54 20 70 72 69 6d 69 74   The TEST primit
110d1 69 76 65 0a 2a 2a 20 77 69 6c 6c 20 6f 6e 6c 79  ive.** will only
110d2 20 73 65 65 20 65 6c 65 6d 65 6e 74 73 20 74 68   see elements th
110d3 61 74 20 77 65 72 65 20 69 6e 73 65 72 74 65 64  at were inserted
110d4 20 62 65 66 6f 72 65 20 74 68 65 20 6c 61 73 74   before the last
110d5 20 63 68 61 6e 67 65 0a 2a 2a 20 69 6e 20 74 68   change.** in th
110d6 65 20 62 61 74 63 68 20 6e 75 6d 62 65 72 2e 20  e batch number. 
110d7 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
110d8 20 69 66 20 61 6e 20 49 4e 53 45 52 54 20 6f 63   if an INSERT oc
110d9 63 75 72 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20  curs between.** 
110da 74 77 6f 20 54 45 53 54 73 20 77 68 65 72 65 20  two TESTs where 
110db 74 68 65 20 54 45 53 54 73 20 68 61 76 65 20 74  the TESTs have t
110dc 68 65 20 73 61 6d 65 20 62 61 74 63 68 20 6e 75  he same batch nu
110dd 62 6d 65 72 2c 20 74 68 65 6e 20 74 68 65 0a 2a  bmer, then the.*
110de 2a 20 76 61 6c 75 65 20 61 64 64 65 64 20 62 79  * value added by
110df 20 74 68 65 20 49 4e 53 45 52 54 20 77 69 6c 6c   the INSERT will
110e0 20 6e 6f 74 20 62 65 20 76 69 73 69 62 6c 65 20   not be visible 
110e1 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 54 45  to the second TE
110e2 53 54 2e 0a 2a 2a 20 54 68 65 20 69 6e 69 74 69  ST..** The initi
110e3 61 6c 20 62 61 74 63 68 20 6e 75 6d 62 65 72 20  al batch number 
110e4 69 73 20 7a 65 72 6f 2c 20 73 6f 20 69 66 20 74  is zero, so if t
110e5 68 65 20 76 65 72 79 20 66 69 72 73 74 20 54 45  he very first TE
110e6 53 54 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61  ST contains.** a
110e7 20 6e 6f 6e 2d 7a 65 72 6f 20 62 61 74 63 68 20   non-zero batch 
110e8 6e 75 6d 62 65 72 2c 20 69 74 20 77 69 6c 6c 20  number, it will 
110e9 73 65 65 20 61 6c 6c 20 70 72 69 6f 72 20 49 4e  see all prior IN
110ea 53 45 52 54 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 20  SERTs..**.** No 
110eb 49 4e 53 45 52 54 73 20 6d 61 79 20 6f 63 63 75  INSERTs may occu
110ec 72 73 20 61 66 74 65 72 20 61 20 53 4d 41 4c 4c  rs after a SMALL
110ed 45 53 54 2e 20 20 41 6e 20 61 73 73 65 72 74 69  EST.  An asserti
110ee 6f 6e 20 77 69 6c 6c 20 66 61 69 6c 20 69 66 0a  on will fail if.
110ef 2a 2a 20 74 68 61 74 20 69 73 20 61 74 74 65 6d  ** that is attem
110f0 70 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  pted..**.** The 
110f1 63 6f 73 74 20 6f 66 20 61 6e 20 49 4e 53 45 52  cost of an INSER
110f2 54 20 69 73 20 72 6f 75 67 68 6c 79 20 63 6f 6e  T is roughly con
110f3 73 74 61 6e 74 2e 20 20 28 53 6f 6d 65 74 69 6d  stant.  (Sometim
110f4 65 20 6e 65 77 20 6d 65 6d 6f 72 79 0a 2a 2a 20  e new memory.** 
110f5 68 61 73 20 74 6f 20 62 65 20 61 6c 6c 6f 63 61  has to be alloca
110f6 74 65 64 20 6f 6e 20 61 6e 20 49 4e 53 45 52 54  ted on an INSERT
110f7 2e 29 20 20 54 68 65 20 63 6f 73 74 20 6f 66 20  .)  The cost of 
110f8 61 20 54 45 53 54 20 77 69 74 68 20 61 20 6e 65  a TEST with a ne
110f9 77 0a 2a 2a 20 62 61 74 63 68 20 6e 75 6d 62 65  w.** batch numbe
110fa 72 20 69 73 20 4f 28 4e 6c 6f 67 4e 29 20 77 68  r is O(NlogN) wh
110fb 65 72 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d  ere N is the num
110fc 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
110fd 69 6e 20 74 68 65 20 52 6f 77 53 65 74 2e 0a 2a  in the RowSet..*
110fe 2a 20 54 68 65 20 63 6f 73 74 20 6f 66 20 61 20  * The cost of a 
110ff 54 45 53 54 20 75 73 69 6e 67 20 74 68 65 20 73  TEST using the s
11100 61 6d 65 20 62 61 74 63 68 20 6e 75 6d 62 65 72  ame batch number
11101 20 69 73 20 4f 28 6c 6f 67 4e 29 2e 20 20 54 68   is O(logN).  Th
11102 65 20 63 6f 73 74 0a 2a 2a 20 6f 66 20 74 68 65  e cost.** of the
11103 20 66 69 72 73 74 20 53 4d 41 4c 4c 45 53 54 20   first SMALLEST 
11104 69 73 20 4f 28 4e 6c 6f 67 4e 29 2e 20 20 53 65  is O(NlogN).  Se
11105 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71 75  cond and subsequ
11106 65 6e 74 20 53 4d 41 4c 4c 45 53 54 0a 2a 2a 20  ent SMALLEST.** 
11107 70 72 69 6d 69 74 69 76 65 73 20 61 72 65 20 63  primitives are c
11108 6f 6e 73 74 61 6e 74 20 74 69 6d 65 2e 20 20 54  onstant time.  T
11109 68 65 20 63 6f 73 74 20 6f 66 20 44 45 53 54 52  he cost of DESTR
1110a 4f 59 20 69 73 20 4f 28 4e 29 2e 0a 2a 2a 0a 2a  OY is O(N)..**.*
1110b 2a 20 54 68 65 72 65 20 69 73 20 61 6e 20 61 64  * There is an ad
1110c 64 65 64 20 63 6f 73 74 20 6f 66 20 4f 28 4e 29  ded cost of O(N)
1110d 20 77 68 65 6e 20 73 77 69 74 63 68 69 6e 67 20   when switching 
1110e 62 65 74 77 65 65 6e 20 54 45 53 54 20 61 6e 64  between TEST and
1110f 0a 2a 2a 20 53 4d 41 4c 4c 45 53 54 20 70 72 69  .** SMALLEST pri
11110 6d 69 74 69 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 24  mitives..**.** $
11111 49 64 3a 20 72 6f 77 73 65 74 2e 63 2c 76 20 31  Id: rowset.c,v 1
11112 2e 37 20 32 30 30 39 2f 30 35 2f 32 32 20 30 31  .7 2009/05/22 01
11113 3a 30 30 3a 31 33 20 64 72 68 20 45 78 70 20 24  :00:13 drh Exp $
11114 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 61 72 67  .*/.../*.** Targ
11115 65 74 20 73 69 7a 65 20 66 6f 72 20 61 6c 6c 6f  et size for allo
11116 63 61 74 69 6f 6e 20 63 68 75 6e 6b 73 2e 0a 2a  cation chunks..*
11117 2f 0a 23 64 65 66 69 6e 65 20 52 4f 57 53 45 54  /.#define ROWSET
11118 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f 53 49 5a 45  _ALLOCATION_SIZE
11119 20 31 30 32 34 0a 0a 2f 2a 0a 2a 2a 20 54 68 65   1024../*.** The
1111a 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 65   number of rowse
1111b 74 20 65 6e 74 72 69 65 73 20 70 65 72 20 61 6c  t entries per al
1111c 6c 6f 63 61 74 69 6f 6e 20 63 68 75 6e 6b 2e 0a  location chunk..
1111d 2a 2f 0a 23 64 65 66 69 6e 65 20 52 4f 57 53 45  */.#define ROWSE
1111e 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e  T_ENTRY_PER_CHUN
1111f 4b 20 20 5c 0a 20 20 20 20 20 20 20 20 20 20 20  K  \.           
11120 20 20 20 20 20 20 20 20 20 20 20 20 28 28 52 4f              ((RO
11121 57 53 45 54 5f 41 4c 4c 4f 43 41 54 49 4f 4e 5f  WSET_ALLOCATION_
11122 53 49 5a 45 2d 38 29 2f 73 69 7a 65 6f 66 28 73  SIZE-8)/sizeof(s
11123 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
11124 79 29 29 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20  y))../*.** Each 
11125 65 6e 74 72 79 20 69 6e 20 61 20 52 6f 77 53 65  entry in a RowSe
11126 74 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  t is an instance
11127 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
11128 67 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 72  g object..*/.str
11129 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
1112a 7b 20 20 20 20 20 20 20 20 20 20 20 20 0a 20 20  {            .  
1112b 69 36 34 20 76 3b 20 20 20 20 20 20 20 20 20 20  i64 v;          
1112c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1112d 20 52 4f 57 49 44 20 76 61 6c 75 65 20 66 6f 72   ROWID value for
1112e 20 74 68 69 73 20 65 6e 74 72 79 20 2a 2f 0a 20   this entry */. 
1112f 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e   struct RowSetEn
11130 74 72 79 20 2a 70 52 69 67 68 74 3b 20 20 20 2f  try *pRight;   /
11131 2a 20 52 69 67 68 74 20 73 75 62 74 72 65 65 20  * Right subtree 
11132 28 6c 61 72 67 65 72 20 65 6e 74 72 69 65 73 29  (larger entries)
11133 20 6f 72 20 6c 69 73 74 20 2a 2f 0a 20 20 73 74   or list */.  st
11134 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79  ruct RowSetEntry
11135 20 2a 70 4c 65 66 74 3b 20 20 20 20 2f 2a 20 4c   *pLeft;    /* L
11136 65 66 74 20 73 75 62 74 72 65 65 20 28 73 6d 61  eft subtree (sma
11137 6c 6c 65 72 20 65 6e 74 72 69 65 73 29 20 2a 2f  ller entries) */
11138 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 77 53 65  .};../*.** RowSe
11139 74 45 6e 74 72 79 20 6f 62 6a 65 63 74 73 20 61  tEntry objects a
1113a 72 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20  re allocated in 
1113b 6c 61 72 67 65 20 63 68 75 6e 6b 73 20 28 69 6e  large chunks (in
1113c 73 74 61 6e 63 65 73 20 6f 66 20 74 68 65 0a 2a  stances of the.*
1113d 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  * following stru
1113e 63 74 75 72 65 29 20 74 6f 20 72 65 64 75 63 65  cture) to reduce
1113f 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
11140 6f 6e 20 6f 76 65 72 68 65 61 64 2e 20 20 54 68  on overhead.  Th
11141 65 0a 2a 2a 20 63 68 75 6e 6b 73 20 61 72 65 20  e.** chunks are 
11142 6b 65 70 74 20 6f 6e 20 61 20 6c 69 6e 6b 65 64  kept on a linked
11143 20 6c 69 73 74 20 73 6f 20 74 68 61 74 20 74 68   list so that th
11144 65 79 20 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f  ey can be deallo
11145 63 61 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68  cated.** when th
11146 65 20 52 6f 77 53 65 74 20 69 73 20 64 65 73 74  e RowSet is dest
11147 72 6f 79 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74  royed..*/.struct
11148 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 7b 0a 20   RowSetChunk {. 
11149 20 73 74 72 75 63 74 20 52 6f 77 53 65 74 43 68   struct RowSetCh
1114a 75 6e 6b 20 2a 70 4e 65 78 74 43 68 75 6e 6b 3b  unk *pNextChunk;
1114b 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
1114c 63 68 75 6e 6b 20 6f 6e 20 6c 69 73 74 20 6f 66  chunk on list of
1114d 20 74 68 65 6d 20 61 6c 6c 20 2a 2f 0a 20 20 73   them all */.  s
1114e 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
1114f 79 20 61 45 6e 74 72 79 5b 52 4f 57 53 45 54 5f  y aEntry[ROWSET_
11150 45 4e 54 52 59 5f 50 45 52 5f 43 48 55 4e 4b 5d  ENTRY_PER_CHUNK]
11151 3b 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 65  ; /* Allocated e
11152 6e 74 72 69 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  ntries */.};../*
11153 0a 2a 2a 20 41 20 52 6f 77 53 65 74 20 69 6e 20  .** A RowSet in 
11154 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
11155 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
11156 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 41 20  ucture..**.** A 
11157 74 79 70 65 64 65 66 20 6f 66 20 74 68 69 73 20  typedef of this 
11158 73 74 72 75 63 74 75 72 65 20 69 66 20 66 6f 75  structure if fou
11159 6e 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e  nd in sqliteInt.
1115a 68 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 6f 77  h..*/.struct Row
1115b 53 65 74 20 7b 0a 20 20 73 74 72 75 63 74 20 52  Set {.  struct R
1115c 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70 43 68 75  owSetChunk *pChu
1115d 6e 6b 3b 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f  nk;    /* List o
1115e 66 20 61 6c 6c 20 63 68 75 6e 6b 20 61 6c 6c 6f  f all chunk allo
1115f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 73 71 6c  cations */.  sql
11160 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
11161 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
11162 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
11163 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72 75  ection */.  stru
11164 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
11165 70 45 6e 74 72 79 3b 20 20 20 20 2f 2a 20 4c 69  pEntry;    /* Li
11166 73 74 20 6f 66 20 65 6e 74 72 69 65 73 20 75 73  st of entries us
11167 69 6e 67 20 70 52 69 67 68 74 20 2a 2f 0a 20 20  ing pRight */.  
11168 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
11169 72 79 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 2f  ry *pLast;     /
1116a 2a 20 4c 61 73 74 20 65 6e 74 72 79 20 6f 6e 20  * Last entry on 
1116b 74 68 65 20 70 45 6e 74 72 79 20 6c 69 73 74 20  the pEntry list 
1116c 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53  */.  struct RowS
1116d 65 74 45 6e 74 72 79 20 2a 70 46 72 65 73 68 3b  etEntry *pFresh;
1116e 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 6f 66      /* Source of
1116f 20 6e 65 77 20 65 6e 74 72 79 20 6f 62 6a 65 63   new entry objec
11170 74 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52  ts */.  struct R
11171 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 54 72 65  owSetEntry *pTre
11172 65 3b 20 20 20 20 20 2f 2a 20 42 69 6e 61 72 79  e;     /* Binary
11173 20 74 72 65 65 20 6f 66 20 65 6e 74 72 69 65 73   tree of entries
11174 20 2a 2f 0a 20 20 75 31 36 20 6e 46 72 65 73 68   */.  u16 nFresh
11175 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
11176 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
11177 66 20 6f 62 6a 65 63 74 73 20 6f 6e 20 70 46 72  f objects on pFr
11178 65 73 68 20 2a 2f 0a 20 20 75 38 20 69 73 53 6f  esh */.  u8 isSo
11179 72 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 20  rted;           
1117a 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
1117b 69 66 20 70 45 6e 74 72 79 20 69 73 20 73 6f 72  if pEntry is sor
1117c 74 65 64 20 2a 2f 0a 20 20 75 38 20 69 42 61 74  ted */.  u8 iBat
1117d 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ch;             
1117e 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
1117f 6e 74 20 69 6e 73 65 72 74 20 62 61 74 63 68 20  nt insert batch 
11180 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 75 72  */.};../*.** Tur
11181 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e  n bulk memory in
11182 74 6f 20 61 20 52 6f 77 53 65 74 20 6f 62 6a 65  to a RowSet obje
11183 63 74 2e 20 20 4e 20 62 79 74 65 73 20 6f 66 20  ct.  N bytes of 
11184 6d 65 6d 6f 72 79 0a 2a 2a 20 61 72 65 20 61 76  memory.** are av
11185 61 69 6c 61 62 6c 65 20 61 74 20 70 53 70 61 63  ailable at pSpac
11186 65 2e 20 20 54 68 65 20 64 62 20 70 6f 69 6e 74  e.  The db point
11187 65 72 20 69 73 20 75 73 65 64 20 61 73 20 61 20  er is used as a 
11188 6d 65 6d 6f 72 79 20 63 6f 6e 74 65 78 74 0a 2a  memory context.*
11189 2a 20 66 6f 72 20 61 6e 79 20 73 75 62 73 65 71  * for any subseq
1118a 75 65 6e 74 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  uent allocations
1118b 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20 6f 63   that need to oc
1118c 63 75 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 61  cur..** Return a
1118d 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
1118e 6e 65 77 20 52 6f 77 53 65 74 20 6f 62 6a 65 63  new RowSet objec
1118f 74 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 6d 75 73 74  t..**.** It must
11190 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61   be the case tha
11191 74 20 4e 20 69 73 20 73 75 66 66 69 63 69 65 6e  t N is sufficien
11192 74 20 74 6f 20 6d 61 6b 65 20 61 20 52 6f 77 73  t to make a Rows
11193 65 74 2e 20 20 49 66 20 6e 6f 74 0a 2a 2a 20 61  et.  If not.** a
11194 6e 20 61 73 73 65 72 74 69 6f 6e 20 66 61 75 6c  n assertion faul
11195 74 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 0a 2a 2a  t occurs..** .**
11196 20 49 66 20 4e 20 69 73 20 6c 61 72 67 65 72 20   If N is larger 
11197 74 68 61 6e 20 74 68 65 20 6d 69 6e 69 6d 75 6d  than the minimum
11198 2c 20 75 73 65 20 74 68 65 20 73 75 72 70 6c 75  , use the surplu
11199 73 20 61 73 20 61 6e 20 69 6e 69 74 69 61 6c 0a  s as an initial.
1119a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66  ** allocation of
1119b 20 65 6e 74 72 69 65 73 20 61 76 61 69 6c 61 62   entries availab
1119c 6c 65 20 74 6f 20 62 65 20 66 69 6c 6c 65 64 2e  le to be filled.
1119d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1119e 54 45 20 52 6f 77 53 65 74 20 2a 73 71 6c 69 74  TE RowSet *sqlit
1119f 65 33 52 6f 77 53 65 74 49 6e 69 74 28 73 71 6c  e3RowSetInit(sql
111a0 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a  ite3 *db, void *
111a1 70 53 70 61 63 65 2c 20 75 6e 73 69 67 6e 65 64  pSpace, unsigned
111a2 20 69 6e 74 20 4e 29 7b 0a 20 20 52 6f 77 53 65   int N){.  RowSe
111a3 74 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20  t *p;.  assert( 
111a4 4e 20 3e 3d 20 52 4f 55 4e 44 38 28 73 69 7a 65  N >= ROUND8(size
111a5 6f 66 28 2a 70 29 29 20 29 3b 0a 20 20 70 20 3d  of(*p)) );.  p =
111a6 20 70 53 70 61 63 65 3b 0a 20 20 70 2d 3e 70 43   pSpace;.  p->pC
111a7 68 75 6e 6b 20 3d 20 30 3b 0a 20 20 70 2d 3e 64  hunk = 0;.  p->d
111a8 62 20 3d 20 64 62 3b 0a 20 20 70 2d 3e 70 45 6e  b = db;.  p->pEn
111a9 74 72 79 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 4c  try = 0;.  p->pL
111aa 61 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 54  ast = 0;.  p->pT
111ab 72 65 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 46  ree = 0;.  p->pF
111ac 72 65 73 68 20 3d 20 28 73 74 72 75 63 74 20 52  resh = (struct R
111ad 6f 77 53 65 74 45 6e 74 72 79 2a 29 28 52 4f 55  owSetEntry*)(ROU
111ae 4e 44 38 28 73 69 7a 65 6f 66 28 2a 70 29 29 20  ND8(sizeof(*p)) 
111af 2b 20 28 63 68 61 72 2a 29 70 29 3b 0a 20 20 70  + (char*)p);.  p
111b0 2d 3e 6e 46 72 65 73 68 20 3d 20 28 75 31 36 29  ->nFresh = (u16)
111b1 28 28 4e 20 2d 20 52 4f 55 4e 44 38 28 73 69 7a  ((N - ROUND8(siz
111b2 65 6f 66 28 2a 70 29 29 29 2f 73 69 7a 65 6f 66  eof(*p)))/sizeof
111b3 28 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e  (struct RowSetEn
111b4 74 72 79 29 29 3b 0a 20 20 70 2d 3e 69 73 53 6f  try));.  p->isSo
111b5 72 74 65 64 20 3d 20 31 3b 0a 20 20 70 2d 3e 69  rted = 1;.  p->i
111b6 42 61 74 63 68 20 3d 20 30 3b 0a 20 20 72 65 74  Batch = 0;.  ret
111b7 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
111b8 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c 6c 20 63  Deallocate all c
111b9 68 75 6e 6b 73 20 66 72 6f 6d 20 61 20 52 6f 77  hunks from a Row
111ba 53 65 74 2e 20 20 54 68 69 73 20 66 72 65 65 73  Set.  This frees
111bb 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 74 68 61 74   all memory that
111bc 0a 2a 2a 20 74 68 65 20 52 6f 77 53 65 74 20 68  .** the RowSet h
111bd 61 73 20 61 6c 6c 6f 63 61 74 65 64 20 6f 76 65  as allocated ove
111be 72 20 69 74 73 20 6c 69 66 65 74 69 6d 65 2e 20  r its lifetime. 
111bf 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
111c0 0a 2a 2a 20 74 68 65 20 64 65 73 74 72 75 63 74  .** the destruct
111c1 6f 72 20 66 6f 72 20 74 68 65 20 52 6f 77 53 65  or for the RowSe
111c2 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
111c3 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
111c4 33 52 6f 77 53 65 74 43 6c 65 61 72 28 52 6f 77  3RowSetClear(Row
111c5 53 65 74 20 2a 70 29 7b 0a 20 20 73 74 72 75 63  Set *p){.  struc
111c6 74 20 52 6f 77 53 65 74 43 68 75 6e 6b 20 2a 70  t RowSetChunk *p
111c7 43 68 75 6e 6b 2c 20 2a 70 4e 65 78 74 43 68 75  Chunk, *pNextChu
111c8 6e 6b 3b 0a 20 20 66 6f 72 28 70 43 68 75 6e 6b  nk;.  for(pChunk
111c9 3d 70 2d 3e 70 43 68 75 6e 6b 3b 20 70 43 68 75  =p->pChunk; pChu
111ca 6e 6b 3b 20 70 43 68 75 6e 6b 20 3d 20 70 4e 65  nk; pChunk = pNe
111cb 78 74 43 68 75 6e 6b 29 7b 0a 20 20 20 20 70 4e  xtChunk){.    pN
111cc 65 78 74 43 68 75 6e 6b 20 3d 20 70 43 68 75 6e  extChunk = pChun
111cd 6b 2d 3e 70 4e 65 78 74 43 68 75 6e 6b 3b 0a 20  k->pNextChunk;. 
111ce 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
111cf 28 70 2d 3e 64 62 2c 20 70 43 68 75 6e 6b 29 3b  (p->db, pChunk);
111d0 0a 20 20 7d 0a 20 20 70 2d 3e 70 43 68 75 6e 6b  .  }.  p->pChunk
111d1 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 46 72 65 73   = 0;.  p->nFres
111d2 68 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 45 6e 74  h = 0;.  p->pEnt
111d3 72 79 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 4c 61  ry = 0;.  p->pLa
111d4 73 74 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 54 72  st = 0;.  p->pTr
111d5 65 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53  ee = 0;.  p->isS
111d6 6f 72 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a  orted = 1;.}../*
111d7 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 6e 65 77  .** Insert a new
111d8 20 76 61 6c 75 65 20 69 6e 74 6f 20 61 20 52 6f   value into a Ro
111d9 77 53 65 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  wSet..**.** The 
111da 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61  mallocFailed fla
111db 67 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  g of the databas
111dc 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
111dd 73 65 74 20 69 66 20 61 0a 2a 2a 20 6d 65 6d 6f  set if a.** memo
111de 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61  ry allocation fa
111df 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ils..*/.SQLITE_P
111e0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
111e1 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28  te3RowSetInsert(
111e2 52 6f 77 53 65 74 20 2a 70 2c 20 69 36 34 20 72  RowSet *p, i64 r
111e3 6f 77 69 64 29 7b 0a 20 20 73 74 72 75 63 74 20  owid){.  struct 
111e4 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 45 6e  RowSetEntry *pEn
111e5 74 72 79 3b 20 20 2f 2a 20 54 68 65 20 6e 65 77  try;  /* The new
111e6 20 65 6e 74 72 79 20 2a 2f 0a 20 20 73 74 72 75   entry */.  stru
111e7 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
111e8 70 4c 61 73 74 3b 20 20 20 2f 2a 20 54 68 65 20  pLast;   /* The 
111e9 6c 61 73 74 20 70 72 69 6f 72 20 65 6e 74 72 79  last prior entry
111ea 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 21   */.  assert( p!
111eb 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e  =0 );.  if( p->n
111ec 46 72 65 73 68 3d 3d 30 20 29 7b 0a 20 20 20 20  Fresh==0 ){.    
111ed 73 74 72 75 63 74 20 52 6f 77 53 65 74 43 68 75  struct RowSetChu
111ee 6e 6b 20 2a 70 4e 65 77 3b 0a 20 20 20 20 70 4e  nk *pNew;.    pN
111ef 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  ew = sqlite3DbMa
111f0 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62 2c 20 73  llocRaw(p->db, s
111f1 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20  izeof(*pNew));. 
111f2 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29     if( pNew==0 )
111f3 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  {.      return;.
111f4 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e      }.    pNew->
111f5 70 4e 65 78 74 43 68 75 6e 6b 20 3d 20 70 2d 3e  pNextChunk = p->
111f6 70 43 68 75 6e 6b 3b 0a 20 20 20 20 70 2d 3e 70  pChunk;.    p->p
111f7 43 68 75 6e 6b 20 3d 20 70 4e 65 77 3b 0a 20 20  Chunk = pNew;.  
111f8 20 20 70 2d 3e 70 46 72 65 73 68 20 3d 20 70 4e    p->pFresh = pN
111f9 65 77 2d 3e 61 45 6e 74 72 79 3b 0a 20 20 20 20  ew->aEntry;.    
111fa 70 2d 3e 6e 46 72 65 73 68 20 3d 20 52 4f 57 53  p->nFresh = ROWS
111fb 45 54 5f 45 4e 54 52 59 5f 50 45 52 5f 43 48 55  ET_ENTRY_PER_CHU
111fc 4e 4b 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79  NK;.  }.  pEntry
111fd 20 3d 20 70 2d 3e 70 46 72 65 73 68 2b 2b 3b 0a   = p->pFresh++;.
111fe 20 20 70 2d 3e 6e 46 72 65 73 68 2d 2d 3b 0a 20    p->nFresh--;. 
111ff 20 70 45 6e 74 72 79 2d 3e 76 20 3d 20 72 6f 77   pEntry->v = row
11200 69 64 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 70 52  id;.  pEntry->pR
11201 69 67 68 74 20 3d 20 30 3b 0a 20 20 70 4c 61 73  ight = 0;.  pLas
11202 74 20 3d 20 70 2d 3e 70 4c 61 73 74 3b 0a 20 20  t = p->pLast;.  
11203 69 66 28 20 70 4c 61 73 74 20 29 7b 0a 20 20 20  if( pLast ){.   
11204 20 69 66 28 20 70 2d 3e 69 73 53 6f 72 74 65 64   if( p->isSorted
11205 20 26 26 20 72 6f 77 69 64 3c 3d 70 4c 61 73 74   && rowid<=pLast
11206 2d 3e 76 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  ->v ){.      p->
11207 69 73 53 6f 72 74 65 64 20 3d 20 30 3b 0a 20 20  isSorted = 0;.  
11208 20 20 7d 0a 20 20 20 20 70 4c 61 73 74 2d 3e 70    }.    pLast->p
11209 52 69 67 68 74 20 3d 20 70 45 6e 74 72 79 3b 0a  Right = pEntry;.
1120a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
1120b 65 72 74 28 20 70 2d 3e 70 45 6e 74 72 79 3d 3d  ert( p->pEntry==
1120c 30 20 29 3b 20 2f 2a 20 46 69 72 65 73 20 69 66  0 ); /* Fires if
1120d 20 49 4e 53 45 52 54 20 61 66 74 65 72 20 53 4d   INSERT after SM
1120e 41 4c 4c 45 53 54 20 2a 2f 0a 20 20 20 20 70 2d  ALLEST */.    p-
1120f 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 79  >pEntry = pEntry
11210 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4c 61 73 74  ;.  }.  p->pLast
11211 20 3d 20 70 45 6e 74 72 79 3b 0a 7d 0a 0a 2f 2a   = pEntry;.}../*
11212 0a 2a 2a 20 4d 65 72 67 65 20 74 77 6f 20 6c 69  .** Merge two li
11213 73 74 73 20 6f 66 20 52 6f 77 53 65 74 45 6e 74  sts of RowSetEnt
11214 72 79 20 6f 62 6a 65 63 74 73 2e 20 20 52 65 6d  ry objects.  Rem
11215 6f 76 65 20 64 75 70 6c 69 63 61 74 65 73 2e 0a  ove duplicates..
11216 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20  **.** The input 
11217 6c 69 73 74 73 20 61 72 65 20 63 6f 6e 6e 65 63  lists are connec
11218 74 65 64 20 76 69 61 20 70 52 69 67 68 74 20 70  ted via pRight p
11219 6f 69 6e 74 65 72 73 20 61 6e 64 20 61 72 65 20  ointers and are 
1121a 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74 6f 20 65  .** assumed to e
1121b 61 63 68 20 61 6c 72 65 61 64 79 20 62 65 20 69  ach already be i
1121c 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a  n sorted order..
1121d 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74  */.static struct
1121e 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 72 6f   RowSetEntry *ro
1121f 77 53 65 74 4d 65 72 67 65 28 0a 20 20 73 74 72  wSetMerge(.  str
11220 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20  uct RowSetEntry 
11221 2a 70 41 2c 20 20 20 20 2f 2a 20 46 69 72 73 74  *pA,    /* First
11222 20 73 6f 72 74 65 64 20 6c 69 73 74 20 74 6f 20   sorted list to 
11223 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a 20 20 73  be merged */.  s
11224 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74 72  truct RowSetEntr
11225 79 20 2a 70 42 20 20 20 20 20 2f 2a 20 53 65 63  y *pB     /* Sec
11226 6f 6e 64 20 73 6f 72 74 65 64 20 6c 69 73 74 20  ond sorted list 
11227 74 6f 20 62 65 20 6d 65 72 67 65 64 20 2a 2f 0a  to be merged */.
11228 29 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53  ){.  struct RowS
11229 65 74 45 6e 74 72 79 20 68 65 61 64 3b 0a 20 20  etEntry head;.  
1122a 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
1122b 72 79 20 2a 70 54 61 69 6c 3b 0a 0a 20 20 70 54  ry *pTail;..  pT
1122c 61 69 6c 20 3d 20 26 68 65 61 64 3b 0a 20 20 77  ail = &head;.  w
1122d 68 69 6c 65 28 20 70 41 20 26 26 20 70 42 20 29  hile( pA && pB )
1122e 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 41  {.    assert( pA
1122f 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20 70  ->pRight==0 || p
11230 41 2d 3e 76 3c 3d 70 41 2d 3e 70 52 69 67 68 74  A->v<=pA->pRight
11231 2d 3e 76 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ->v );.    asser
11232 74 28 20 70 42 2d 3e 70 52 69 67 68 74 3d 3d 30  t( pB->pRight==0
11233 20 7c 7c 20 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70   || pB->v<=pB->p
11234 52 69 67 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20  Right->v );.    
11235 69 66 28 20 70 41 2d 3e 76 3c 70 42 2d 3e 76 20  if( pA->v<pB->v 
11236 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e  ){.      pTail->
11237 70 52 69 67 68 74 20 3d 20 70 41 3b 0a 20 20 20  pRight = pA;.   
11238 20 20 20 70 41 20 3d 20 70 41 2d 3e 70 52 69 67     pA = pA->pRig
11239 68 74 3b 0a 20 20 20 20 20 20 70 54 61 69 6c 20  ht;.      pTail 
1123a 3d 20 70 54 61 69 6c 2d 3e 70 52 69 67 68 74 3b  = pTail->pRight;
1123b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
1123c 42 2d 3e 76 3c 70 41 2d 3e 76 20 29 7b 0a 20 20  B->v<pA->v ){.  
1123d 20 20 20 20 70 54 61 69 6c 2d 3e 70 52 69 67 68      pTail->pRigh
1123e 74 20 3d 20 70 42 3b 0a 20 20 20 20 20 20 70 42  t = pB;.      pB
1123f 20 3d 20 70 42 2d 3e 70 52 69 67 68 74 3b 0a 20   = pB->pRight;. 
11240 20 20 20 20 20 70 54 61 69 6c 20 3d 20 70 54 61       pTail = pTa
11241 69 6c 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20  il->pRight;.    
11242 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 41 20  }else{.      pA 
11243 3d 20 70 41 2d 3e 70 52 69 67 68 74 3b 0a 20 20  = pA->pRight;.  
11244 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 41    }.  }.  if( pA
11245 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
11246 70 41 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c  pA->pRight==0 ||
11247 20 70 41 2d 3e 76 3c 3d 70 41 2d 3e 70 52 69 67   pA->v<=pA->pRig
11248 68 74 2d 3e 76 20 29 3b 0a 20 20 20 20 70 54 61  ht->v );.    pTa
11249 69 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70 41 3b  il->pRight = pA;
1124a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
1124b 73 65 72 74 28 20 70 42 3d 3d 30 20 7c 7c 20 70  sert( pB==0 || p
1124c 42 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c 20  B->pRight==0 || 
1124d 70 42 2d 3e 76 3c 3d 70 42 2d 3e 70 52 69 67 68  pB->v<=pB->pRigh
1124e 74 2d 3e 76 20 29 3b 0a 20 20 20 20 70 54 61 69  t->v );.    pTai
1124f 6c 2d 3e 70 52 69 67 68 74 20 3d 20 70 42 3b 0a  l->pRight = pB;.
11250 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 65 61    }.  return hea
11251 64 2e 70 52 69 67 68 74 3b 0a 7d 0a 0a 2f 2a 0a  d.pRight;.}../*.
11252 2a 2a 20 53 6f 72 74 20 61 6c 6c 20 65 6c 65 6d  ** Sort all elem
11253 65 6e 74 73 20 6f 6e 20 74 68 65 20 70 45 6e 74  ents on the pEnt
11254 72 79 20 6c 69 73 74 20 6f 66 20 74 68 65 20 52  ry list of the R
11255 6f 77 53 65 74 20 69 6e 74 6f 20 61 73 63 65 6e  owSet into ascen
11256 64 69 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 20 0a  ding order..*/ .
11257 73 74 61 74 69 63 20 76 6f 69 64 20 72 6f 77 53  static void rowS
11258 65 74 53 6f 72 74 28 52 6f 77 53 65 74 20 2a 70  etSort(RowSet *p
11259 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  ){.  unsigned in
1125a 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 52 6f  t i;.  struct Ro
1125b 77 53 65 74 45 6e 74 72 79 20 2a 70 45 6e 74 72  wSetEntry *pEntr
1125c 79 3b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53  y;.  struct RowS
1125d 65 74 45 6e 74 72 79 20 2a 61 42 75 63 6b 65 74  etEntry *aBucket
1125e 5b 34 30 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28  [40];..  assert(
1125f 20 70 2d 3e 69 73 53 6f 72 74 65 64 3d 3d 30 20   p->isSorted==0 
11260 29 3b 0a 20 20 6d 65 6d 73 65 74 28 61 42 75 63  );.  memset(aBuc
11261 6b 65 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 61  ket, 0, sizeof(a
11262 42 75 63 6b 65 74 29 29 3b 0a 20 20 77 68 69 6c  Bucket));.  whil
11263 65 28 20 70 2d 3e 70 45 6e 74 72 79 20 29 7b 0a  e( p->pEntry ){.
11264 20 20 20 20 70 45 6e 74 72 79 20 3d 20 70 2d 3e      pEntry = p->
11265 70 45 6e 74 72 79 3b 0a 20 20 20 20 70 2d 3e 70  pEntry;.    p->p
11266 45 6e 74 72 79 20 3d 20 70 45 6e 74 72 79 2d 3e  Entry = pEntry->
11267 70 52 69 67 68 74 3b 0a 20 20 20 20 70 45 6e 74  pRight;.    pEnt
11268 72 79 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b 0a  ry->pRight = 0;.
11269 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 61 42 75      for(i=0; aBu
1126a 63 6b 65 74 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20  cket[i]; i++){. 
1126b 20 20 20 20 20 70 45 6e 74 72 79 20 3d 20 72 6f       pEntry = ro
1126c 77 53 65 74 4d 65 72 67 65 28 61 42 75 63 6b 65  wSetMerge(aBucke
1126d 74 5b 69 5d 2c 20 70 45 6e 74 72 79 29 3b 0a 20  t[i], pEntry);. 
1126e 20 20 20 20 20 61 42 75 63 6b 65 74 5b 69 5d 20       aBucket[i] 
1126f 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  = 0;.    }.    a
11270 42 75 63 6b 65 74 5b 69 5d 20 3d 20 70 45 6e 74  Bucket[i] = pEnt
11271 72 79 3b 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79  ry;.  }.  pEntry
11272 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b   = 0;.  for(i=0;
11273 20 69 3c 73 69 7a 65 6f 66 28 61 42 75 63 6b 65   i<sizeof(aBucke
11274 74 29 2f 73 69 7a 65 6f 66 28 61 42 75 63 6b 65  t)/sizeof(aBucke
11275 74 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20  t[0]); i++){.   
11276 20 70 45 6e 74 72 79 20 3d 20 72 6f 77 53 65 74   pEntry = rowSet
11277 4d 65 72 67 65 28 70 45 6e 74 72 79 2c 20 61 42  Merge(pEntry, aB
11278 75 63 6b 65 74 5b 69 5d 29 3b 0a 20 20 7d 0a 20  ucket[i]);.  }. 
11279 20 70 2d 3e 70 45 6e 74 72 79 20 3d 20 70 45 6e   p->pEntry = pEn
1127a 74 72 79 3b 0a 20 20 70 2d 3e 70 4c 61 73 74 20  try;.  p->pLast 
1127b 3d 20 30 3b 0a 20 20 70 2d 3e 69 73 53 6f 72 74  = 0;.  p->isSort
1127c 65 64 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  ed = 1;.}.../*.*
1127d 2a 20 54 68 65 20 69 6e 70 75 74 2c 20 70 49 6e  * The input, pIn
1127e 2c 20 69 73 20 61 20 62 69 6e 61 72 79 20 74 72  , is a binary tr
1127f 65 65 20 28 6f 72 20 73 75 62 74 72 65 65 29 20  ee (or subtree) 
11280 6f 66 20 52 6f 77 53 65 74 45 6e 74 72 79 20 6f  of RowSetEntry o
11281 62 6a 65 63 74 73 2e 0a 2a 2a 20 43 6f 6e 76 65  bjects..** Conve
11282 72 74 20 74 68 69 73 20 74 72 65 65 20 69 6e 74  rt this tree int
11283 6f 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  o a linked list 
11284 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 74 68 65  connected by the
11285 20 70 52 69 67 68 74 20 70 6f 69 6e 74 65 72 73   pRight pointers
11286 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72 6e 20 70  .** and return p
11287 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 66  ointers to the f
11288 69 72 73 74 20 61 6e 64 20 6c 61 73 74 20 65 6c  irst and last el
11289 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6e 65  ements of the ne
1128a 77 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69  w list..*/.stati
1128b 63 20 76 6f 69 64 20 72 6f 77 53 65 74 54 72 65  c void rowSetTre
1128c 65 54 6f 4c 69 73 74 28 0a 20 20 73 74 72 75 63  eToList(.  struc
1128d 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70  t RowSetEntry *p
1128e 49 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52  In,         /* R
1128f 6f 6f 74 20 6f 66 20 74 68 65 20 69 6e 70 75 74  oot of the input
11290 20 74 72 65 65 20 2a 2f 0a 20 20 73 74 72 75 63   tree */.  struc
11291 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a  t RowSetEntry **
11292 70 70 46 69 72 73 74 2c 20 20 20 20 2f 2a 20 57  ppFirst,    /* W
11293 72 69 74 65 20 68 65 61 64 20 6f 66 20 74 68 65  rite head of the
11294 20 6f 75 74 70 75 74 20 6c 69 73 74 20 68 65 72   output list her
11295 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f  e */.  struct Ro
11296 77 53 65 74 45 6e 74 72 79 20 2a 2a 70 70 4c 61  wSetEntry **ppLa
11297 73 74 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65  st      /* Write
11298 20 74 61 69 6c 20 6f 66 20 74 68 65 20 6f 75 74   tail of the out
11299 70 75 74 20 6c 69 73 74 20 68 65 72 65 20 2a 2f  put list here */
1129a 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 49  .){.  assert( pI
1129b 6e 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 49  n!=0 );.  if( pI
1129c 6e 2d 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20  n->pLeft ){.    
1129d 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
1129e 72 79 20 2a 70 3b 0a 20 20 20 20 72 6f 77 53 65  ry *p;.    rowSe
1129f 74 54 72 65 65 54 6f 4c 69 73 74 28 70 49 6e 2d  tTreeToList(pIn-
112a0 3e 70 4c 65 66 74 2c 20 70 70 46 69 72 73 74 2c  >pLeft, ppFirst,
112a1 20 26 70 29 3b 0a 20 20 20 20 70 2d 3e 70 52 69   &p);.    p->pRi
112a2 67 68 74 20 3d 20 70 49 6e 3b 0a 20 20 7d 65 6c  ght = pIn;.  }el
112a3 73 65 7b 0a 20 20 20 20 2a 70 70 46 69 72 73 74  se{.    *ppFirst
112a4 20 3d 20 70 49 6e 3b 0a 20 20 7d 0a 20 20 69 66   = pIn;.  }.  if
112a5 28 20 70 49 6e 2d 3e 70 52 69 67 68 74 20 29 7b  ( pIn->pRight ){
112a6 0a 20 20 20 20 72 6f 77 53 65 74 54 72 65 65 54  .    rowSetTreeT
112a7 6f 4c 69 73 74 28 70 49 6e 2d 3e 70 52 69 67 68  oList(pIn->pRigh
112a8 74 2c 20 26 70 49 6e 2d 3e 70 52 69 67 68 74 2c  t, &pIn->pRight,
112a9 20 70 70 4c 61 73 74 29 3b 0a 20 20 7d 65 6c 73   ppLast);.  }els
112aa 65 7b 0a 20 20 20 20 2a 70 70 4c 61 73 74 20 3d  e{.    *ppLast =
112ab 20 70 49 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65   pIn;.  }.  asse
112ac 72 74 28 20 28 2a 70 70 4c 61 73 74 29 2d 3e 70  rt( (*ppLast)->p
112ad 52 69 67 68 74 3d 3d 30 20 29 3b 0a 7d 0a 0a 0a  Right==0 );.}...
112ae 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20  /*.** Convert a 
112af 73 6f 72 74 65 64 20 6c 69 73 74 20 6f 66 20 65  sorted list of e
112b0 6c 65 6d 65 6e 74 73 20 28 63 6f 6e 6e 65 63 74  lements (connect
112b1 65 64 20 62 79 20 70 52 69 67 68 74 29 20 69 6e  ed by pRight) in
112b2 74 6f 20 61 20 62 69 6e 61 72 79 0a 2a 2a 20 74  to a binary.** t
112b3 72 65 65 20 77 69 74 68 20 64 65 70 74 68 20 6f  ree with depth o
112b4 66 20 69 44 65 70 74 68 2e 20 20 41 20 64 65 70  f iDepth.  A dep
112b5 74 68 20 6f 66 20 31 20 6d 65 61 6e 73 20 74 68  th of 1 means th
112b6 65 20 74 72 65 65 20 63 6f 6e 74 61 69 6e 73 20  e tree contains 
112b7 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6e 6f 64 65  a single.** node
112b8 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20   taken from the 
112b9 68 65 61 64 20 6f 66 20 2a 70 70 4c 69 73 74 2e  head of *ppList.
112ba 20 20 41 20 64 65 70 74 68 20 6f 66 20 32 20 6d    A depth of 2 m
112bb 65 61 6e 73 20 61 20 74 72 65 65 20 77 69 74 68  eans a tree with
112bc 0a 2a 2a 20 74 68 72 65 65 20 6e 6f 64 65 73 2e  .** three nodes.
112bd 20 20 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a    And so forth..
112be 2a 2a 0a 2a 2a 20 55 73 65 20 61 73 20 6d 61 6e  **.** Use as man
112bf 79 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74  y entries from t
112c0 68 65 20 69 6e 70 75 74 20 6c 69 73 74 20 61 73  he input list as
112c1 20 72 65 71 75 69 72 65 64 20 61 6e 64 20 75 70   required and up
112c2 64 61 74 65 20 74 68 65 0a 2a 2a 20 2a 70 70 4c  date the.** *ppL
112c3 69 73 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  ist to point to 
112c4 74 68 65 20 75 6e 75 73 65 64 20 65 6c 65 6d 65  the unused eleme
112c5 6e 74 73 20 6f 66 20 74 68 65 20 6c 69 73 74 2e  nts of the list.
112c6 20 20 49 66 20 74 68 65 20 69 6e 70 75 74 0a 2a    If the input.*
112c7 2a 20 6c 69 73 74 20 63 6f 6e 74 61 69 6e 73 20  * list contains 
112c8 74 6f 6f 20 66 65 77 20 65 6c 65 6d 65 6e 74 73  too few elements
112c9 2c 20 74 68 65 6e 20 63 6f 6e 73 74 72 75 63 74  , then construct
112ca 20 61 6e 20 69 6e 63 6f 6d 70 6c 65 74 65 20 74   an incomplete t
112cb 72 65 65 0a 2a 2a 20 61 6e 64 20 6c 65 61 76 65  ree.** and leave
112cc 20 2a 70 70 4c 69 73 74 20 73 65 74 20 74 6f 20   *ppList set to 
112cd 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  NULL..**.** Retu
112ce 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
112cf 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65 20  the root of the 
112d0 63 6f 6e 73 74 72 75 63 74 65 64 20 62 69 6e 61  constructed bina
112d1 72 79 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74  ry tree..*/.stat
112d2 69 63 20 73 74 72 75 63 74 20 52 6f 77 53 65 74  ic struct RowSet
112d3 45 6e 74 72 79 20 2a 72 6f 77 53 65 74 4e 44 65  Entry *rowSetNDe
112d4 65 70 54 72 65 65 28 0a 20 20 73 74 72 75 63 74  epTree(.  struct
112d5 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 2a 70   RowSetEntry **p
112d6 70 4c 69 73 74 2c 0a 20 20 69 6e 74 20 69 44 65  pList,.  int iDe
112d7 70 74 68 0a 29 7b 0a 20 20 73 74 72 75 63 74 20  pth.){.  struct 
112d8 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 70 3b 20  RowSetEntry *p; 
112d9 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20          /* Root 
112da 6f 66 20 74 68 65 20 6e 65 77 20 74 72 65 65 20  of the new tree 
112db 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53  */.  struct RowS
112dc 65 74 45 6e 74 72 79 20 2a 70 4c 65 66 74 3b 20  etEntry *pLeft; 
112dd 20 20 20 20 2f 2a 20 4c 65 66 74 20 73 75 62 74      /* Left subt
112de 72 65 65 20 2a 2f 0a 20 20 69 66 28 20 2a 70 70  ree */.  if( *pp
112df 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72  List==0 ){.    r
112e0 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69  eturn 0;.  }.  i
112e1 66 28 20 69 44 65 70 74 68 3d 3d 31 20 29 7b 0a  f( iDepth==1 ){.
112e2 20 20 20 20 70 20 3d 20 2a 70 70 4c 69 73 74 3b      p = *ppList;
112e3 0a 20 20 20 20 2a 70 70 4c 69 73 74 20 3d 20 70  .    *ppList = p
112e4 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 70 2d  ->pRight;.    p-
112e5 3e 70 4c 65 66 74 20 3d 20 70 2d 3e 70 52 69 67  >pLeft = p->pRig
112e6 68 74 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75  ht = 0;.    retu
112e7 72 6e 20 70 3b 0a 20 20 7d 0a 20 20 70 4c 65 66  rn p;.  }.  pLef
112e8 74 20 3d 20 72 6f 77 53 65 74 4e 44 65 65 70 54  t = rowSetNDeepT
112e9 72 65 65 28 70 70 4c 69 73 74 2c 20 69 44 65 70  ree(ppList, iDep
112ea 74 68 2d 31 29 3b 0a 20 20 70 20 3d 20 2a 70 70  th-1);.  p = *pp
112eb 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 3d 3d 30  List;.  if( p==0
112ec 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70   ){.    return p
112ed 4c 65 66 74 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70  Left;.  }.  p->p
112ee 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b 0a 20 20  Left = pLeft;.  
112ef 2a 70 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69  *ppList = p->pRi
112f0 67 68 74 3b 0a 20 20 70 2d 3e 70 52 69 67 68 74  ght;.  p->pRight
112f1 20 3d 20 72 6f 77 53 65 74 4e 44 65 65 70 54 72   = rowSetNDeepTr
112f2 65 65 28 70 70 4c 69 73 74 2c 20 69 44 65 70 74  ee(ppList, iDept
112f3 68 2d 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  h-1);.  return p
112f4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
112f5 72 74 20 61 20 73 6f 72 74 65 64 20 6c 69 73 74  rt a sorted list
112f6 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 69 6e 74   of elements int
112f7 6f 20 61 20 62 69 6e 61 72 79 20 74 72 65 65 2e  o a binary tree.
112f8 20 4d 61 6b 65 20 74 68 65 20 74 72 65 65 0a 2a   Make the tree.*
112f9 2a 20 61 73 20 64 65 65 70 20 61 73 20 69 74 20  * as deep as it 
112fa 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6e 20 6f  needs to be in o
112fb 72 64 65 72 20 74 6f 20 63 6f 6e 74 61 69 6e 20  rder to contain 
112fc 74 68 65 20 65 6e 74 69 72 65 20 6c 69 73 74 2e  the entire list.
112fd 0a 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63  .*/.static struc
112fe 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a 72  t RowSetEntry *r
112ff 6f 77 53 65 74 4c 69 73 74 54 6f 54 72 65 65 28  owSetListToTree(
11300 73 74 72 75 63 74 20 52 6f 77 53 65 74 45 6e 74  struct RowSetEnt
11301 72 79 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e  ry *pList){.  in
11302 74 20 69 44 65 70 74 68 3b 20 20 20 20 20 20 20  t iDepth;       
11303 20 20 20 20 2f 2a 20 44 65 70 74 68 20 6f 66 20      /* Depth of 
11304 74 68 65 20 74 72 65 65 20 73 6f 20 66 61 72 20  the tree so far 
11305 2a 2f 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53  */.  struct RowS
11306 65 74 45 6e 74 72 79 20 2a 70 3b 20 20 20 20 20  etEntry *p;     
11307 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 74 72 65    /* Current tre
11308 65 20 72 6f 6f 74 20 2a 2f 0a 20 20 73 74 72 75  e root */.  stru
11309 63 74 20 52 6f 77 53 65 74 45 6e 74 72 79 20 2a  ct RowSetEntry *
1130a 70 4c 65 66 74 3b 20 20 20 2f 2a 20 4c 65 66 74  pLeft;   /* Left
1130b 20 73 75 62 74 72 65 65 20 2a 2f 0a 0a 20 20 61   subtree */..  a
1130c 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20  ssert( pList!=0 
1130d 29 3b 0a 20 20 70 20 3d 20 70 4c 69 73 74 3b 0a  );.  p = pList;.
1130e 20 20 70 4c 69 73 74 20 3d 20 70 2d 3e 70 52 69    pList = p->pRi
1130f 67 68 74 3b 0a 20 20 70 2d 3e 70 4c 65 66 74 20  ght;.  p->pLeft 
11310 3d 20 70 2d 3e 70 52 69 67 68 74 20 3d 20 30 3b  = p->pRight = 0;
11311 0a 20 20 66 6f 72 28 69 44 65 70 74 68 3d 31 3b  .  for(iDepth=1;
11312 20 70 4c 69 73 74 3b 20 69 44 65 70 74 68 2b 2b   pList; iDepth++
11313 29 7b 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 70  ){.    pLeft = p
11314 3b 0a 20 20 20 20 70 20 3d 20 70 4c 69 73 74 3b  ;.    p = pList;
11315 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 70 2d 3e  .    pList = p->
11316 70 52 69 67 68 74 3b 0a 20 20 20 20 70 2d 3e 70  pRight;.    p->p
11317 4c 65 66 74 20 3d 20 70 4c 65 66 74 3b 0a 20 20  Left = pLeft;.  
11318 20 20 70 2d 3e 70 52 69 67 68 74 20 3d 20 72 6f    p->pRight = ro
11319 77 53 65 74 4e 44 65 65 70 54 72 65 65 28 26 70  wSetNDeepTree(&p
1131a 4c 69 73 74 2c 20 69 44 65 70 74 68 29 3b 0a 20  List, iDepth);. 
1131b 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d   }.  return p;.}
1131c 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ../*.** Convert 
1131d 74 68 65 20 6c 69 73 74 20 69 6e 20 70 2d 3e 70  the list in p->p
1131e 45 6e 74 72 79 20 69 6e 74 6f 20 61 20 73 6f 72  Entry into a sor
1131f 74 65 64 20 6c 69 73 74 20 69 66 20 69 74 20 69  ted list if it i
11320 73 20 6e 6f 74 0a 2a 2a 20 73 6f 72 74 65 64 20  s not.** sorted 
11321 61 6c 72 65 61 64 79 2e 20 20 49 66 20 74 68 65  already.  If the
11322 72 65 20 69 73 20 61 20 62 69 6e 61 72 79 20 74  re is a binary t
11323 72 65 65 20 6f 6e 20 70 2d 3e 70 54 72 65 65 2c  ree on p->pTree,
11324 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e 76 65 72 74   then.** convert
11325 20 69 74 20 69 6e 74 6f 20 61 20 6c 69 73 74 20   it into a list 
11326 74 6f 6f 20 61 6e 64 20 6d 65 72 67 65 20 69 74  too and merge it
11327 20 69 6e 74 6f 20 74 68 65 20 70 2d 3e 70 45 6e   into the p->pEn
11328 74 72 79 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61  try list..*/.sta
11329 74 69 63 20 76 6f 69 64 20 72 6f 77 53 65 74 54  tic void rowSetT
1132a 6f 4c 69 73 74 28 52 6f 77 53 65 74 20 2a 70 29  oList(RowSet *p)
1132b 7b 0a 20 20 69 66 28 20 21 70 2d 3e 69 73 53 6f  {.  if( !p->isSo
1132c 72 74 65 64 20 29 7b 0a 20 20 20 20 72 6f 77 53  rted ){.    rowS
1132d 65 74 53 6f 72 74 28 70 29 3b 0a 20 20 7d 0a 20  etSort(p);.  }. 
1132e 20 69 66 28 20 70 2d 3e 70 54 72 65 65 20 29 7b   if( p->pTree ){
1132f 0a 20 20 20 20 73 74 72 75 63 74 20 52 6f 77 53  .    struct RowS
11330 65 74 45 6e 74 72 79 20 2a 70 48 65 61 64 2c 20  etEntry *pHead, 
11331 2a 70 54 61 69 6c 3b 0a 20 20 20 20 72 6f 77 53  *pTail;.    rowS
11332 65 74 54 72 65 65 54 6f 4c 69 73 74 28 70 2d 3e  etTreeToList(p->
11333 70 54 72 65 65 2c 20 26 70 48 65 61 64 2c 20 26  pTree, &pHead, &
11334 70 54 61 69 6c 29 3b 0a 20 20 20 20 70 2d 3e 70  pTail);.    p->p
11335 54 72 65 65 20 3d 20 30 3b 0a 20 20 20 20 70 2d  Tree = 0;.    p-
11336 3e 70 45 6e 74 72 79 20 3d 20 72 6f 77 53 65 74  >pEntry = rowSet
11337 4d 65 72 67 65 28 70 2d 3e 70 45 6e 74 72 79 2c  Merge(p->pEntry,
11338 20 70 48 65 61 64 29 3b 0a 20 20 7d 0a 7d 0a 0a   pHead);.  }.}..
11339 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68  /*.** Extract th
1133a 65 20 73 6d 61 6c 6c 65 73 74 20 65 6c 65 6d 65  e smallest eleme
1133b 6e 74 20 66 72 6f 6d 20 74 68 65 20 52 6f 77 53  nt from the RowS
1133c 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 65  et..** Write the
1133d 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 2a 70   element into *p
1133e 52 6f 77 69 64 2e 20 20 52 65 74 75 72 6e 20 31  Rowid.  Return 1
1133f 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 52 65   on success.  Re
11340 74 75 72 6e 0a 2a 2a 20 30 20 69 66 20 74 68 65  turn.** 0 if the
11341 20 52 6f 77 53 65 74 20 69 73 20 61 6c 72 65 61   RowSet is alrea
11342 64 79 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20  dy empty..**.** 
11343 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74 69  After this routi
11344 6e 65 20 68 61 73 20 62 65 65 6e 20 63 61 6c 6c  ne has been call
11345 65 64 2c 20 74 68 65 20 73 71 6c 69 74 65 33 52  ed, the sqlite3R
11346 6f 77 53 65 74 49 6e 73 65 72 74 28 29 0a 2a 2a  owSetInsert().**
11347 20 72 6f 75 74 69 6e 65 20 6d 61 79 20 6e 6f 74   routine may not
11348 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e   be called again
11349 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  .  .*/.SQLITE_PR
1134a 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1134b 33 52 6f 77 53 65 74 4e 65 78 74 28 52 6f 77 53  3RowSetNext(RowS
1134c 65 74 20 2a 70 2c 20 69 36 34 20 2a 70 52 6f 77  et *p, i64 *pRow
1134d 69 64 29 7b 0a 20 20 72 6f 77 53 65 74 54 6f 4c  id){.  rowSetToL
1134e 69 73 74 28 70 29 3b 0a 20 20 69 66 28 20 70 2d  ist(p);.  if( p-
1134f 3e 70 45 6e 74 72 79 20 29 7b 0a 20 20 20 20 2a  >pEntry ){.    *
11350 70 52 6f 77 69 64 20 3d 20 70 2d 3e 70 45 6e 74  pRowid = p->pEnt
11351 72 79 2d 3e 76 3b 0a 20 20 20 20 70 2d 3e 70 45  ry->v;.    p->pE
11352 6e 74 72 79 20 3d 20 70 2d 3e 70 45 6e 74 72 79  ntry = p->pEntry
11353 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 69 66  ->pRight;.    if
11354 28 20 70 2d 3e 70 45 6e 74 72 79 3d 3d 30 20 29  ( p->pEntry==0 )
11355 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  {.      sqlite3R
11356 6f 77 53 65 74 43 6c 65 61 72 28 70 29 3b 0a 20  owSetClear(p);. 
11357 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
11358 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  1;.  }else{.    
11359 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a  return 0;.  }.}.
1135a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20  ./*.** Check to 
1135b 73 65 65 20 69 66 20 65 6c 65 6d 65 6e 74 20 69  see if element i
1135c 52 6f 77 69 64 20 77 61 73 20 69 6e 73 65 72 74  Rowid was insert
1135d 65 64 20 69 6e 74 6f 20 74 68 65 20 74 68 65 20  ed into the the 
1135e 72 6f 77 73 65 74 20 61 73 0a 2a 2a 20 70 61 72  rowset as.** par
1135f 74 20 6f 66 20 61 6e 79 20 69 6e 73 65 72 74 20  t of any insert 
11360 62 61 74 63 68 20 70 72 69 6f 72 20 74 6f 20 69  batch prior to i
11361 42 61 74 63 68 2e 20 20 52 65 74 75 72 6e 20 31  Batch.  Return 1
11362 20 6f 72 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45   or 0..*/.SQLITE
11363 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
11364 69 74 65 33 52 6f 77 53 65 74 54 65 73 74 28 52  ite3RowSetTest(R
11365 6f 77 53 65 74 20 2a 70 52 6f 77 53 65 74 2c 20  owSet *pRowSet, 
11366 75 38 20 69 42 61 74 63 68 2c 20 73 71 6c 69 74  u8 iBatch, sqlit
11367 65 33 5f 69 6e 74 36 34 20 69 52 6f 77 69 64 29  e3_int64 iRowid)
11368 7b 0a 20 20 73 74 72 75 63 74 20 52 6f 77 53 65  {.  struct RowSe
11369 74 45 6e 74 72 79 20 2a 70 3b 0a 20 20 69 66 28  tEntry *p;.  if(
1136a 20 69 42 61 74 63 68 21 3d 70 52 6f 77 53 65 74   iBatch!=pRowSet
1136b 2d 3e 69 42 61 74 63 68 20 29 7b 0a 20 20 20 20  ->iBatch ){.    
1136c 69 66 28 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e  if( pRowSet->pEn
1136d 74 72 79 20 29 7b 0a 20 20 20 20 20 20 72 6f 77  try ){.      row
1136e 53 65 74 54 6f 4c 69 73 74 28 70 52 6f 77 53 65  SetToList(pRowSe
1136f 74 29 3b 0a 20 20 20 20 20 20 70 52 6f 77 53 65  t);.      pRowSe
11370 74 2d 3e 70 54 72 65 65 20 3d 20 72 6f 77 53 65  t->pTree = rowSe
11371 74 4c 69 73 74 54 6f 54 72 65 65 28 70 52 6f 77  tListToTree(pRow
11372 53 65 74 2d 3e 70 45 6e 74 72 79 29 3b 0a 20 20  Set->pEntry);.  
11373 20 20 20 20 70 52 6f 77 53 65 74 2d 3e 70 45 6e      pRowSet->pEn
11374 74 72 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  try = 0;.      p
11375 52 6f 77 53 65 74 2d 3e 70 4c 61 73 74 20 3d 20  RowSet->pLast = 
11376 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 6f  0;.    }.    pRo
11377 77 53 65 74 2d 3e 69 42 61 74 63 68 20 3d 20 69  wSet->iBatch = i
11378 42 61 74 63 68 3b 0a 20 20 7d 0a 20 20 70 20 3d  Batch;.  }.  p =
11379 20 70 52 6f 77 53 65 74 2d 3e 70 54 72 65 65 3b   pRowSet->pTree;
1137a 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20  .  while( p ){. 
1137b 20 20 20 69 66 28 20 70 2d 3e 76 3c 69 52 6f 77     if( p->v<iRow
1137c 69 64 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20  id ){.      p = 
1137d 70 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 7d  p->pRight;.    }
1137e 65 6c 73 65 20 69 66 28 20 70 2d 3e 76 3e 69 52  else if( p->v>iR
1137f 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 70 20  owid ){.      p 
11380 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20  = p->pLeft;.    
11381 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74  }else{.      ret
11382 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 1;.    }.  }
11383 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
11384 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
11385 45 6e 64 20 6f 66 20 72 6f 77 73 65 74 2e 63 20  End of rowset.c 
11386 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11387 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
11388 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
11389 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1138a 42 65 67 69 6e 20 66 69 6c 65 20 70 61 67 65 72  Begin file pager
1138b 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1138c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1138d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1138e 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
1138f 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
11390 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
11391 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
11392 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
11393 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
11394 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
11395 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
11396 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
11397 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
11398 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
11399 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
1139a 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
1139b 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
1139c 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
1139d 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
1139e 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
1139f 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
113a0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
113a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113a2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
113a5 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ******.** This i
113a6 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  s the implementa
113a7 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 65  tion of the page
113a8 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d   cache subsystem
113a9 20 6f 72 20 22 70 61 67 65 72 22 2e 0a 2a 2a 20   or "pager"..** 
113aa 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 69 73  .** The pager is
113ab 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20   used to access 
113ac 61 20 64 61 74 61 62 61 73 65 20 64 69 73 6b 20  a database disk 
113ad 66 69 6c 65 2e 20 20 49 74 20 69 6d 70 6c 65 6d  file.  It implem
113ae 65 6e 74 73 0a 2a 2a 20 61 74 6f 6d 69 63 20 63  ents.** atomic c
113af 6f 6d 6d 69 74 20 61 6e 64 20 72 6f 6c 6c 62 61  ommit and rollba
113b0 63 6b 20 74 68 72 6f 75 67 68 20 74 68 65 20 75  ck through the u
113b1 73 65 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c 20  se of a journal 
113b2 66 69 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73 20  file that.** is 
113b3 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20 74 68  separate from th
113b4 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
113b5 20 20 54 68 65 20 70 61 67 65 72 20 61 6c 73 6f    The pager also
113b6 20 69 6d 70 6c 65 6d 65 6e 74 73 20 66 69 6c 65   implements file
113b7 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 74 6f 20 70  .** locking to p
113b8 72 65 76 65 6e 74 20 74 77 6f 20 70 72 6f 63 65  revent two proce
113b9 73 73 65 73 20 66 72 6f 6d 20 77 72 69 74 69 6e  sses from writin
113ba 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61 62  g the same datab
113bb 61 73 65 0a 2a 2a 20 66 69 6c 65 20 73 69 6d 75  ase.** file simu
113bc 6c 74 61 6e 65 6f 75 73 6c 79 2c 20 6f 72 20 6f  ltaneously, or o
113bd 6e 65 20 70 72 6f 63 65 73 73 20 66 72 6f 6d 20  ne process from 
113be 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 61  reading the data
113bf 62 61 73 65 20 77 68 69 6c 65 0a 2a 2a 20 61 6e  base while.** an
113c0 6f 74 68 65 72 20 69 73 20 77 72 69 74 69 6e 67  other is writing
113c1 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64  ..**.** @(#) $Id
113c2 3a 20 70 61 67 65 72 2e 63 2c 76 20 31 2e 36 32  : pager.c,v 1.62
113c3 39 20 32 30 30 39 2f 30 38 2f 31 30 20 31 37 3a  9 2009/08/10 17:
113c4 34 38 3a 35 37 20 64 72 68 20 45 78 70 20 24 0a  48:57 drh Exp $.
113c5 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
113c6 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 0a 2f  E_OMIT_DISKIO../
113c7 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20  *.** Macros for 
113c8 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e 67 2e  troubleshooting.
113c9 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65    Normally turne
113ca 64 20 6f 66 66 0a 2a 2f 0a 23 69 66 20 30 0a 69  d off.*/.#if 0.i
113cb 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 54  nt sqlite3PagerT
113cc 72 61 63 65 3d 31 3b 20 20 2f 2a 20 54 72 75 65  race=1;  /* True
113cd 20 74 6f 20 65 6e 61 62 6c 65 20 74 72 61 63 69   to enable traci
113ce 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 73 71  ng */.#define sq
113cf 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
113d0 20 70 72 69 6e 74 66 0a 23 64 65 66 69 6e 65 20   printf.#define 
113d1 50 41 47 45 52 54 52 41 43 45 28 58 29 20 20 20  PAGERTRACE(X)   
113d2 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 67    if( sqlite3Pag
113d3 65 72 54 72 61 63 65 20 29 7b 20 73 71 6c 69 74  erTrace ){ sqlit
113d4 65 33 44 65 62 75 67 50 72 69 6e 74 66 20 58 3b  e3DebugPrintf X;
113d5 20 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65   }.#else.#define
113d6 20 50 41 47 45 52 54 52 41 43 45 28 58 29 0a 23   PAGERTRACE(X).#
113d7 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
113d8 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 6d   following two m
113d9 61 63 72 6f 73 20 61 72 65 20 75 73 65 64 20 77  acros are used w
113da 69 74 68 69 6e 20 74 68 65 20 50 41 47 45 52 54  ithin the PAGERT
113db 52 41 43 45 28 29 20 6d 61 63 72 6f 73 20 61 62  RACE() macros ab
113dc 6f 76 65 0a 2a 2a 20 74 6f 20 70 72 69 6e 74 20  ove.** to print 
113dd 6f 75 74 20 66 69 6c 65 2d 64 65 73 63 72 69 70  out file-descrip
113de 74 6f 72 73 2e 20 0a 2a 2a 0a 2a 2a 20 50 41 47  tors. .**.** PAG
113df 45 52 49 44 28 29 20 74 61 6b 65 73 20 61 20 70  ERID() takes a p
113e0 6f 69 6e 74 65 72 20 74 6f 20 61 20 50 61 67 65  ointer to a Page
113e1 72 20 73 74 72 75 63 74 20 61 73 20 69 74 73 20  r struct as its 
113e2 61 72 67 75 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a  argument. The.**
113e3 20 61 73 73 6f 63 69 61 74 65 64 20 66 69 6c 65   associated file
113e4 2d 64 65 73 63 72 69 70 74 6f 72 20 69 73 20 72  -descriptor is r
113e5 65 74 75 72 6e 65 64 2e 20 46 49 4c 45 48 41 4e  eturned. FILEHAN
113e6 44 4c 45 49 44 28 29 20 74 61 6b 65 73 20 61 6e  DLEID() takes an
113e7 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 0a 2a 2a   sqlite3_file.**
113e8 20 73 74 72 75 63 74 20 61 73 20 69 74 73 20 61   struct as its a
113e9 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66  rgument..*/.#def
113ea 69 6e 65 20 50 41 47 45 52 49 44 28 70 29 20 28  ine PAGERID(p) (
113eb 28 69 6e 74 29 28 70 2d 3e 66 64 29 29 0a 23 64  (int)(p->fd)).#d
113ec 65 66 69 6e 65 20 46 49 4c 45 48 41 4e 44 4c 45  efine FILEHANDLE
113ed 49 44 28 66 64 29 20 28 28 69 6e 74 29 66 64 29  ID(fd) ((int)fd)
113ee 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65  ../*.** The page
113ef 20 63 61 63 68 65 20 61 73 20 61 20 77 68 6f 6c   cache as a whol
113f0 65 20 69 73 20 61 6c 77 61 79 73 20 69 6e 20 6f  e is always in o
113f1 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ne of the follow
113f2 69 6e 67 0a 2a 2a 20 73 74 61 74 65 73 3a 0a 2a  ing.** states:.*
113f3 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 55 4e 4c  *.**   PAGER_UNL
113f4 4f 43 4b 20 20 20 20 20 20 20 20 54 68 65 20 70  OCK        The p
113f5 61 67 65 20 63 61 63 68 65 20 69 73 20 6e 6f 74  age cache is not
113f6 20 63 75 72 72 65 6e 74 6c 79 20 72 65 61 64 69   currently readi
113f7 6e 67 20 6f 72 20 0a 2a 2a 20 20 20 20 20 20 20  ng or .**       
113f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113f9 77 72 69 74 69 6e 67 20 74 68 65 20 64 61 74 61  writing the data
113fa 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 72  base file.  Ther
113fb 65 20 69 73 20 6e 6f 0a 2a 2a 20 20 20 20 20 20  e is no.**      
113fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
113fd 20 64 61 74 61 20 68 65 6c 64 20 69 6e 20 6d 65   data held in me
113fe 6d 6f 72 79 2e 20 20 54 68 69 73 20 69 73 20 74  mory.  This is t
113ff 68 65 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 20 20  he initial.**   
11400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11401 20 20 20 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a      state..**.**
11402 20 20 20 50 41 47 45 52 5f 53 48 41 52 45 44 20     PAGER_SHARED 
11403 20 20 20 20 20 20 20 54 68 65 20 70 61 67 65 20         The page 
11404 63 61 63 68 65 20 69 73 20 72 65 61 64 69 6e 67  cache is reading
11405 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a   the database..*
11406 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
11407 20 20 20 20 20 20 20 20 57 72 69 74 69 6e 67 20          Writing 
11408 69 73 20 6e 6f 74 20 70 65 72 6d 69 74 74 65 64  is not permitted
11409 2e 20 20 54 68 65 72 65 20 63 61 6e 20 62 65 0a  .  There can be.
1140a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
1140b 20 20 20 20 20 20 20 20 20 6d 75 6c 74 69 70 6c           multipl
1140c 65 20 72 65 61 64 65 72 73 20 61 63 63 65 73 73  e readers access
1140d 69 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74  ing the same dat
1140e 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20  abase.**        
1140f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66                 f
11410 69 6c 65 20 61 74 20 74 68 65 20 73 61 6d 65 20  ile at the same 
11411 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41  time..**.**   PA
11412 47 45 52 5f 52 45 53 45 52 56 45 44 20 20 20 20  GER_RESERVED    
11413 20 20 54 68 69 73 20 70 72 6f 63 65 73 73 20 68    This process h
11414 61 73 20 72 65 73 65 72 76 65 64 20 74 68 65 20  as reserved the 
11415 64 61 74 61 62 61 73 65 20 66 6f 72 20 77 72 69  database for wri
11416 74 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20  ting.**         
11417 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 75                bu
11418 74 20 68 61 73 20 6e 6f 74 20 79 65 74 20 6d 61  t has not yet ma
11419 64 65 20 61 6e 79 20 63 68 61 6e 67 65 73 2e 20  de any changes. 
1141a 20 4f 6e 6c 79 20 6f 6e 65 20 70 72 6f 63 65 73   Only one proces
1141b 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  s.**            
1141c 20 20 20 20 20 20 20 20 20 20 20 61 74 20 61 20             at a 
1141d 74 69 6d 65 20 63 61 6e 20 72 65 73 65 72 76 65  time can reserve
1141e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
1141f 54 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20  The original.** 
11420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11421 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20 66        database f
11422 69 6c 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e  ile has not been
11423 20 6d 6f 64 69 66 69 65 64 20 73 6f 20 6f 74 68   modified so oth
11424 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  er.**           
11425 20 20 20 20 20 20 20 20 20 20 20 20 70 72 6f 63              proc
11426 65 73 73 65 73 20 6d 61 79 20 73 74 69 6c 6c 20  esses may still 
11427 62 65 20 72 65 61 64 69 6e 67 20 74 68 65 20 6f  be reading the o
11428 6e 2d 64 69 73 6b 0a 2a 2a 20 20 20 20 20 20 20  n-disk.**       
11429 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1142a 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
1142b 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 45 58 43  *.**   PAGER_EXC
1142c 4c 55 53 49 56 45 20 20 20 20 20 54 68 65 20 70  LUSIVE     The p
1142d 61 67 65 20 63 61 63 68 65 20 69 73 20 77 72 69  age cache is wri
1142e 74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73  ting the databas
1142f 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  e..**           
11430 20 20 20 20 20 20 20 20 20 20 20 20 41 63 63 65              Acce
11431 73 73 20 69 73 20 65 78 63 6c 75 73 69 76 65 2e  ss is exclusive.
11432 20 20 4e 6f 20 6f 74 68 65 72 20 70 72 6f 63 65    No other proce
11433 73 73 65 73 20 6f 72 0a 2a 2a 20 20 20 20 20 20  sses or.**      
11434 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11435 20 74 68 72 65 61 64 73 20 63 61 6e 20 62 65 20   threads can be 
11436 72 65 61 64 69 6e 67 20 6f 72 20 77 72 69 74 69  reading or writi
11437 6e 67 20 77 68 69 6c 65 20 6f 6e 65 0a 2a 2a 20  ng while one.** 
11438 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11439 20 20 20 20 20 20 70 72 6f 63 65 73 73 20 69 73        process is
1143a 20 77 72 69 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20   writing..**.** 
1143b 20 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20 20    PAGER_SYNCED  
1143c 20 20 20 20 20 20 54 68 65 20 70 61 67 65 72 20        The pager 
1143d 6d 6f 76 65 73 20 74 6f 20 74 68 69 73 20 73 74  moves to this st
1143e 61 74 65 20 66 72 6f 6d 20 50 41 47 45 52 5f 45  ate from PAGER_E
1143f 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 20  XCLUSIVE.**     
11440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11441 20 20 61 66 74 65 72 20 61 6c 6c 20 64 69 72 74    after all dirt
11442 79 20 70 61 67 65 73 20 68 61 76 65 20 62 65 65  y pages have bee
11443 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  n written to the
11444 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
11445 20 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61            databa
11446 73 65 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20  se file and the 
11447 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 73 79  file has been sy
11448 6e 63 65 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20  nced to.**      
11449 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1144a 20 64 69 73 6b 2e 20 41 6c 6c 20 74 68 61 74 20   disk. All that 
1144b 72 65 6d 61 69 6e 73 20 74 6f 20 64 6f 20 69 73  remains to do is
1144c 20 74 6f 20 72 65 6d 6f 76 65 20 6f 72 0a 2a 2a   to remove or.**
1144d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1144e 20 20 20 20 20 20 20 74 72 75 6e 63 61 74 65 20         truncate 
1144f 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
11450 20 61 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63   and the transac
11451 74 69 6f 6e 20 0a 2a 2a 20 20 20 20 20 20 20 20  tion .**        
11452 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77                 w
11453 69 6c 6c 20 62 65 20 63 6f 6d 6d 69 74 74 65 64  ill be committed
11454 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65  ..**.** The page
11455 20 63 61 63 68 65 20 63 6f 6d 65 73 20 75 70 20   cache comes up 
11456 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e  in PAGER_UNLOCK.
11457 20 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65    The first time
11458 20 61 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67   a.** sqlite3Pag
11459 65 72 47 65 74 28 29 20 6f 63 63 75 72 73 2c 20  erGet() occurs, 
1145a 74 68 65 20 73 74 61 74 65 20 74 72 61 6e 73 69  the state transi
1145b 74 69 6f 6e 73 20 74 6f 20 50 41 47 45 52 5f 53  tions to PAGER_S
1145c 48 41 52 45 44 2e 0a 2a 2a 20 41 66 74 65 72 20  HARED..** After 
1145d 61 6c 6c 20 70 61 67 65 73 20 68 61 76 65 20 62  all pages have b
1145e 65 65 6e 20 72 65 6c 65 61 73 65 64 20 75 73 69  een released usi
1145f 6e 67 20 73 71 6c 69 74 65 5f 70 61 67 65 5f 75  ng sqlite_page_u
11460 6e 72 65 66 28 29 2c 0a 2a 2a 20 74 68 65 20 73  nref(),.** the s
11461 74 61 74 65 20 74 72 61 6e 73 69 74 69 6f 6e 73  tate transitions
11462 20 62 61 63 6b 20 74 6f 20 50 41 47 45 52 5f 55   back to PAGER_U
11463 4e 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69 72 73  NLOCK.  The firs
11464 74 20 74 69 6d 65 0a 2a 2a 20 74 68 61 74 20 73  t time.** that s
11465 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
11466 28 29 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68  () is called, th
11467 65 20 73 74 61 74 65 20 74 72 61 6e 73 69 74 69  e state transiti
11468 6f 6e 73 20 74 6f 0a 2a 2a 20 50 41 47 45 52 5f  ons to.** PAGER_
11469 52 45 53 45 52 56 45 44 2e 20 20 28 4e 6f 74 65  RESERVED.  (Note
1146a 20 74 68 61 74 20 73 71 6c 69 74 65 33 50 61 67   that sqlite3Pag
1146b 65 72 57 72 69 74 65 28 29 20 63 61 6e 20 6f 6e  erWrite() can on
1146c 6c 79 20 62 65 0a 2a 2a 20 63 61 6c 6c 65 64 20  ly be.** called 
1146d 6f 6e 20 61 6e 20 6f 75 74 73 74 61 6e 64 69 6e  on an outstandin
1146e 67 20 70 61 67 65 20 77 68 69 63 68 20 6d 65 61  g page which mea
1146f 6e 73 20 74 68 61 74 20 74 68 65 20 70 61 67 65  ns that the page
11470 72 20 6d 75 73 74 0a 2a 2a 20 62 65 20 69 6e 20  r must.** be in 
11471 50 41 47 45 52 5f 53 48 41 52 45 44 20 62 65 66  PAGER_SHARED bef
11472 6f 72 65 20 69 74 20 74 72 61 6e 73 69 74 69 6f  ore it transitio
11473 6e 73 20 74 6f 20 50 41 47 45 52 5f 52 45 53 45  ns to PAGER_RESE
11474 52 56 45 44 2e 29 0a 2a 2a 20 50 41 47 45 52 5f  RVED.).** PAGER_
11475 52 45 53 45 52 56 45 44 20 6d 65 61 6e 73 20 74  RESERVED means t
11476 68 61 74 20 74 68 65 72 65 20 69 73 20 61 6e 20  hat there is an 
11477 6f 70 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f  open rollback jo
11478 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20 74 72  urnal..** The tr
11479 61 6e 73 69 74 69 6f 6e 20 74 6f 20 50 41 47 45  ansition to PAGE
1147a 52 5f 45 58 43 4c 55 53 49 56 45 20 6f 63 63 75  R_EXCLUSIVE occu
1147b 72 73 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68  rs before any ch
1147c 61 6e 67 65 73 0a 2a 2a 20 61 72 65 20 6d 61 64  anges.** are mad
1147d 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  e to the databas
1147e 65 20 66 69 6c 65 2c 20 74 68 6f 75 67 68 20 77  e file, though w
1147f 72 69 74 65 73 20 74 6f 20 74 68 65 20 72 6f 6c  rites to the rol
11480 6c 62 61 63 6b 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  lback.** journal
11481 20 6f 63 63 75 72 73 20 77 69 74 68 20 6a 75 73   occurs with jus
11482 74 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44  t PAGER_RESERVED
11483 2e 20 20 41 66 74 65 72 20 61 6e 20 73 71 6c 69  .  After an sqli
11484 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b  te3PagerRollback
11485 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33  ().** or sqlite3
11486 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65  PagerCommitPhase
11487 54 77 6f 28 29 2c 20 74 68 65 20 73 74 61 74 65  Two(), the state
11488 20 63 61 6e 20 67 6f 20 62 61 63 6b 20 74 6f 20   can go back to 
11489 50 41 47 45 52 5f 53 48 41 52 45 44 2c 0a 2a 2a  PAGER_SHARED,.**
1148a 20 6f 72 20 69 74 20 63 61 6e 20 73 74 61 79 20   or it can stay 
1148b 61 74 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49  at PAGER_EXCLUSI
1148c 56 45 20 69 66 20 77 65 20 61 72 65 20 69 6e 20  VE if we are in 
1148d 65 78 63 6c 75 73 69 76 65 20 61 63 63 65 73 73  exclusive access
1148e 20 6d 6f 64 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e   mode..*/.#defin
1148f 65 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 20  e PAGER_UNLOCK  
11490 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 50 41      0.#define PA
11491 47 45 52 5f 53 48 41 52 45 44 20 20 20 20 20 20  GER_SHARED      
11492 31 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 53  1   /* same as S
11493 48 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64  HARED_LOCK */.#d
11494 65 66 69 6e 65 20 50 41 47 45 52 5f 52 45 53 45  efine PAGER_RESE
11495 52 56 45 44 20 20 20 20 32 20 20 20 2f 2a 20 73  RVED    2   /* s
11496 61 6d 65 20 61 73 20 52 45 53 45 52 56 45 44 5f  ame as RESERVED_
11497 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65 20  LOCK */.#define 
11498 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20  PAGER_EXCLUSIVE 
11499 20 20 34 20 20 20 2f 2a 20 73 61 6d 65 20 61 73    4   /* same as
1149a 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20   EXCLUSIVE_LOCK 
1149b 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  */.#define PAGER
1149c 5f 53 59 4e 43 45 44 20 20 20 20 20 20 35 0a 0a  _SYNCED      5..
1149d 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72 6f 20 75 73  /*.** A macro us
1149e 65 64 20 66 6f 72 20 69 6e 76 6f 6b 69 6e 67 20  ed for invoking 
1149f 74 68 65 20 63 6f 64 65 63 20 69 66 20 74 68 65  the codec if the
114a0 72 65 20 69 73 20 6f 6e 65 0a 2a 2f 0a 23 69 66  re is one.*/.#if
114a1 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43  def SQLITE_HAS_C
114a2 4f 44 45 43 0a 23 20 64 65 66 69 6e 65 20 43 4f  ODEC.# define CO
114a3 44 45 43 31 28 50 2c 44 2c 4e 2c 58 2c 45 29 20  DEC1(P,D,N,X,E) 
114a4 5c 0a 20 20 20 20 69 66 28 20 50 2d 3e 78 43 6f  \.    if( P->xCo
114a5 64 65 63 20 26 26 20 50 2d 3e 78 43 6f 64 65 63  dec && P->xCodec
114a6 28 50 2d 3e 70 43 6f 64 65 63 2c 44 2c 4e 2c 58  (P->pCodec,D,N,X
114a7 29 3d 3d 30 20 29 7b 20 45 3b 20 7d 0a 23 20 64  )==0 ){ E; }.# d
114a8 65 66 69 6e 65 20 43 4f 44 45 43 32 28 50 2c 44  efine CODEC2(P,D
114a9 2c 4e 2c 58 2c 45 2c 4f 29 20 5c 0a 20 20 20 20  ,N,X,E,O) \.    
114aa 69 66 28 20 50 2d 3e 78 43 6f 64 65 63 3d 3d 30  if( P->xCodec==0
114ab 20 29 7b 20 4f 3d 28 63 68 61 72 2a 29 44 3b 20   ){ O=(char*)D; 
114ac 7d 65 6c 73 65 20 5c 0a 20 20 20 20 69 66 28 20  }else \.    if( 
114ad 28 4f 3d 28 63 68 61 72 2a 29 28 50 2d 3e 78 43  (O=(char*)(P->xC
114ae 6f 64 65 63 28 50 2d 3e 70 43 6f 64 65 63 2c 44  odec(P->pCodec,D
114af 2c 4e 2c 58 29 29 29 3d 3d 30 20 29 7b 20 45 3b  ,N,X)))==0 ){ E;
114b0 20 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e   }.#else.# defin
114b1 65 20 43 4f 44 45 43 31 28 50 2c 44 2c 4e 2c 58  e CODEC1(P,D,N,X
114b2 2c 45 29 20 20 20 2f 2a 20 4e 4f 2d 4f 50 20 2a  ,E)   /* NO-OP *
114b3 2f 0a 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43  /.# define CODEC
114b4 32 28 50 2c 44 2c 4e 2c 58 2c 45 2c 4f 29 20 4f  2(P,D,N,X,E,O) O
114b5 3d 28 63 68 61 72 2a 29 44 0a 23 65 6e 64 69 66  =(char*)D.#endif
114b6 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69  ../*.** The maxi
114b7 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73 65 63 74  mum allowed sect
114b8 6f 72 20 73 69 7a 65 2e 20 36 34 4b 69 42 2e 20  or size. 64KiB. 
114b9 49 66 20 74 68 65 20 78 53 65 63 74 6f 72 73 69  If the xSectorsi
114ba 7a 65 28 29 20 6d 65 74 68 6f 64 20 0a 2a 2a 20  ze() method .** 
114bb 72 65 74 75 72 6e 73 20 61 20 76 61 6c 75 65 20  returns a value 
114bc 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 69 73  larger than this
114bd 2c 20 74 68 65 6e 20 4d 41 58 5f 53 45 43 54 4f  , then MAX_SECTO
114be 52 5f 53 49 5a 45 20 69 73 20 75 73 65 64 20 69  R_SIZE is used i
114bf 6e 73 74 65 61 64 2e 0a 2a 2a 20 54 68 69 73 20  nstead..** This 
114c0 63 6f 75 6c 64 20 63 6f 6e 63 65 69 76 61 62 6c  could conceivabl
114c1 79 20 63 61 75 73 65 20 63 6f 72 72 75 70 74 69  y cause corrupti
114c2 6f 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 70  on following a p
114c3 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f 6e 0a  ower failure on.
114c4 2a 2a 20 73 75 63 68 20 61 20 73 79 73 74 65 6d  ** such a system
114c5 2e 20 54 68 69 73 20 69 73 20 63 75 72 72 65 6e  . This is curren
114c6 74 6c 79 20 61 6e 20 75 6e 64 6f 63 75 6d 65 6e  tly an undocumen
114c7 74 65 64 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 23 64  ted limit..*/.#d
114c8 65 66 69 6e 65 20 4d 41 58 5f 53 45 43 54 4f 52  efine MAX_SECTOR
114c9 5f 53 49 5a 45 20 30 78 31 30 30 30 30 0a 0a 2f  _SIZE 0x10000../
114ca 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
114cb 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
114cc 67 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61  g structure is a
114cd 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63  llocated for eac
114ce 68 20 61 63 74 69 76 65 0a 2a 2a 20 73 61 76 65  h active.** save
114cf 70 6f 69 6e 74 20 61 6e 64 20 73 74 61 74 65 6d  point and statem
114d0 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
114d1 69 6e 20 74 68 65 20 73 79 73 74 65 6d 2e 20 41  in the system. A
114d2 6c 6c 20 73 75 63 68 20 73 74 72 75 63 74 75 72  ll such structur
114d3 65 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72 65 64  es.** are stored
114d4 20 69 6e 20 74 68 65 20 50 61 67 65 72 2e 61 53   in the Pager.aS
114d5 61 76 65 70 6f 69 6e 74 5b 5d 20 61 72 72 61 79  avepoint[] array
114d6 2c 20 77 68 69 63 68 20 69 73 20 61 6c 6c 6f 63  , which is alloc
114d7 61 74 65 64 20 61 6e 64 0a 2a 2a 20 72 65 73 69  ated and.** resi
114d8 7a 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65  zed using sqlite
114d9 33 52 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a  3Realloc()..**.*
114da 2a 20 57 68 65 6e 20 61 20 73 61 76 65 70 6f 69  * When a savepoi
114db 6e 74 20 69 73 20 63 72 65 61 74 65 64 2c 20 74  nt is created, t
114dc 68 65 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e  he PagerSavepoin
114dd 74 2e 69 48 64 72 4f 66 66 73 65 74 20 66 69 65  t.iHdrOffset fie
114de 6c 64 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20  ld is.** set to 
114df 30 2e 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 2d  0. If a journal-
114e0 68 65 61 64 65 72 20 69 73 20 77 72 69 74 74 65  header is writte
114e1 6e 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20  n into the main 
114e2 6a 6f 75 72 6e 61 6c 20 77 68 69 6c 65 0a 2a 2a  journal while.**
114e3 20 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 69   the savepoint i
114e4 73 20 61 63 74 69 76 65 2c 20 74 68 65 6e 20 69  s active, then i
114e5 48 64 72 4f 66 66 73 65 74 20 69 73 20 73 65 74  HdrOffset is set
114e6 20 74 6f 20 74 68 65 20 62 79 74 65 20 6f 66 66   to the byte off
114e7 73 65 74 20 0a 2a 2a 20 69 6d 6d 65 64 69 61 74  set .** immediat
114e8 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  ely following th
114e9 65 20 6c 61 73 74 20 6a 6f 75 72 6e 61 6c 20 72  e last journal r
114ea 65 63 6f 72 64 20 77 72 69 74 74 65 6e 20 69 6e  ecord written in
114eb 74 6f 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 6a  to the main.** j
114ec 6f 75 72 6e 61 6c 20 62 65 66 6f 72 65 20 74 68  ournal before th
114ed 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72  e journal-header
114ee 2e 20 54 68 69 73 20 69 73 20 72 65 71 75 69 72  . This is requir
114ef 65 64 20 64 75 72 69 6e 67 20 73 61 76 65 70 6f  ed during savepo
114f0 69 6e 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20  int.** rollback 
114f1 28 73 65 65 20 70 61 67 65 72 50 6c 61 79 62 61  (see pagerPlayba
114f2 63 6b 53 61 76 65 70 6f 69 6e 74 28 29 29 2e 0a  ckSavepoint())..
114f3 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
114f4 74 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74  t PagerSavepoint
114f5 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 3b   PagerSavepoint;
114f6 0a 73 74 72 75 63 74 20 50 61 67 65 72 53 61 76  .struct PagerSav
114f7 65 70 6f 69 6e 74 20 7b 0a 20 20 69 36 34 20 69  epoint {.  i64 i
114f8 4f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20  Offset;         
114f9 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72 74          /* Start
114fa 69 6e 67 20 6f 66 66 73 65 74 20 69 6e 20 6d 61  ing offset in ma
114fb 69 6e 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20  in journal */.  
114fc 69 36 34 20 69 48 64 72 4f 66 66 73 65 74 3b 20  i64 iHdrOffset; 
114fd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
114fe 53 65 65 20 61 62 6f 76 65 20 2a 2f 0a 20 20 42  See above */.  B
114ff 69 74 76 65 63 20 2a 70 49 6e 53 61 76 65 70 6f  itvec *pInSavepo
11500 69 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 53  int;        /* S
11501 65 74 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  et of pages in t
11502 68 69 73 20 73 61 76 65 70 6f 69 6e 74 20 2a 2f  his savepoint */
11503 0a 20 20 50 67 6e 6f 20 6e 4f 72 69 67 3b 20 20  .  Pgno nOrig;  
11504 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11505 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6e 75 6d 62  /* Original numb
11506 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 66  er of pages in f
11507 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 53  ile */.  Pgno iS
11508 75 62 52 65 63 3b 20 20 20 20 20 20 20 20 20 20  ubRec;          
11509 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
1150a 66 20 66 69 72 73 74 20 72 65 63 6f 72 64 20 69  f first record i
1150b 6e 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f  n sub-journal */
1150c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6f 70 65  .};../*.** A ope
1150d 6e 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20  n page cache is 
1150e 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  an instance of t
1150f 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
11510 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 65 72  ucture..**.** er
11511 72 43 6f 64 65 0a 2a 2a 0a 2a 2a 20 20 20 50 61  rCode.**.**   Pa
11512 67 65 72 2e 65 72 72 43 6f 64 65 20 6d 61 79 20  ger.errCode may 
11513 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54 45  be set to SQLITE
11514 5f 49 4f 45 52 52 2c 20 53 51 4c 49 54 45 5f 43  _IOERR, SQLITE_C
11515 4f 52 52 55 50 54 2c 20 6f 72 0a 2a 2a 20 20 20  ORRUPT, or.**   
11516 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e 20  or SQLITE_FULL. 
11517 4f 6e 63 65 20 6f 6e 65 20 6f 66 20 74 68 65 20  Once one of the 
11518 66 69 72 73 74 20 74 68 72 65 65 20 65 72 72 6f  first three erro
11519 72 73 20 6f 63 63 75 72 73 2c 20 69 74 20 70 65  rs occurs, it pe
1151a 72 73 69 73 74 73 0a 2a 2a 20 20 20 61 6e 64 20  rsists.**   and 
1151b 69 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 74  is returned as t
1151c 68 65 20 72 65 73 75 6c 74 20 6f 66 20 65 76 65  he result of eve
1151d 72 79 20 6d 61 6a 6f 72 20 70 61 67 65 72 20 41  ry major pager A
1151e 50 49 20 63 61 6c 6c 2e 20 20 54 68 65 0a 2a 2a  PI call.  The.**
1151f 20 20 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 72     SQLITE_FULL r
11520 65 74 75 72 6e 20 63 6f 64 65 20 69 73 20 73 6c  eturn code is sl
11521 69 67 68 74 6c 79 20 64 69 66 66 65 72 65 6e 74  ightly different
11522 2e 20 49 74 20 70 65 72 73 69 73 74 73 20 6f 6e  . It persists on
11523 6c 79 20 75 6e 74 69 6c 20 74 68 65 0a 2a 2a 20  ly until the.** 
11524 20 20 6e 65 78 74 20 73 75 63 63 65 73 73 66 75    next successfu
11525 6c 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 70 65  l rollback is pe
11526 72 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65 20 70  rformed on the p
11527 61 67 65 72 20 63 61 63 68 65 2e 20 41 6c 73 6f  ager cache. Also
11528 2c 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 46 55  ,.**   SQLITE_FU
11529 4c 4c 20 64 6f 65 73 20 6e 6f 74 20 61 66 66 65  LL does not affe
1152a 63 74 20 74 68 65 20 73 71 6c 69 74 65 33 50 61  ct the sqlite3Pa
1152b 67 65 72 47 65 74 28 29 20 61 6e 64 20 73 71 6c  gerGet() and sql
1152c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28  ite3PagerLookup(
1152d 29 0a 2a 2a 20 20 20 41 50 49 73 2c 20 74 68 65  ).**   APIs, the
1152e 79 20 6d 61 79 20 73 74 69 6c 6c 20 62 65 20 75  y may still be u
1152f 73 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79  sed successfully
11530 2e 0a 2a 2a 0a 2a 2a 20 64 62 53 69 7a 65 56 61  ..**.** dbSizeVa
11531 6c 69 64 2c 20 64 62 53 69 7a 65 2c 20 64 62 4f  lid, dbSize, dbO
11532 72 69 67 53 69 7a 65 2c 20 64 62 46 69 6c 65 53  rigSize, dbFileS
11533 69 7a 65 0a 2a 2a 0a 2a 2a 20 20 20 4d 61 6e 61  ize.**.**   Mana
11534 67 69 6e 67 20 74 68 65 20 73 69 7a 65 20 6f 66  ging the size of
11535 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
11536 6c 65 20 69 6e 20 70 61 67 65 73 20 69 73 20 61  le in pages is a
11537 20 6c 69 74 74 6c 65 20 63 6f 6d 70 6c 69 63 61   little complica
11538 74 65 64 2e 0a 2a 2a 20 20 20 54 68 65 20 76 61  ted..**   The va
11539 72 69 61 62 6c 65 20 50 61 67 65 72 2e 64 62 53  riable Pager.dbS
1153a 69 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ize contains the
1153b 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
1153c 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62 61   that the databa
1153d 73 65 0a 2a 2a 20 20 20 69 6d 61 67 65 20 63 75  se.**   image cu
1153e 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73  rrently contains
1153f 2e 20 41 73 20 74 68 65 20 64 61 74 61 62 61 73  . As the databas
11540 65 20 69 6d 61 67 65 20 67 72 6f 77 73 20 6f 72  e image grows or
11541 20 73 68 72 69 6e 6b 73 20 74 68 69 73 0a 2a 2a   shrinks this.**
11542 20 20 20 76 61 72 69 61 62 6c 65 20 69 73 20 75     variable is u
11543 70 64 61 74 65 64 2e 20 54 68 65 20 76 61 72 69  pdated. The vari
11544 61 62 6c 65 20 50 61 67 65 72 2e 64 62 46 69 6c  able Pager.dbFil
11545 65 53 69 7a 65 20 63 6f 6e 74 61 69 6e 73 20 74  eSize contains t
11546 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 6f  he number.**   o
11547 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64  f pages in the d
11548 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54 68  atabase file. Th
11549 69 73 20 6d 61 79 20 62 65 20 64 69 66 66 65 72  is may be differ
1154a 65 6e 74 20 66 72 6f 6d 20 50 61 67 65 72 2e 64  ent from Pager.d
1154b 62 53 69 7a 65 0a 2a 2a 20 20 20 69 66 20 73 6f  bSize.**   if so
1154c 6d 65 20 70 61 67 65 73 20 68 61 76 65 20 62 65  me pages have be
1154d 65 6e 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74  en appended to t
1154e 68 65 20 64 61 74 61 62 61 73 65 20 69 6d 61 67  he database imag
1154f 65 20 62 75 74 20 6e 6f 74 20 79 65 74 20 77 72  e but not yet wr
11550 69 74 74 65 6e 0a 2a 2a 20 20 20 6f 75 74 20 66  itten.**   out f
11551 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 74 6f  rom the cache to
11552 20 74 68 65 20 61 63 74 75 61 6c 20 66 69 6c 65   the actual file
11553 20 6f 6e 20 64 69 73 6b 2e 20 4f 72 20 69 66 20   on disk. Or if 
11554 74 68 65 20 69 6d 61 67 65 20 68 61 73 20 62 65  the image has be
11555 65 6e 0a 2a 2a 20 20 20 74 72 75 6e 63 61 74 65  en.**   truncate
11556 64 20 62 79 20 61 6e 20 69 6e 63 72 65 6d 65 6e  d by an incremen
11557 74 61 6c 2d 76 61 63 75 75 6d 20 6f 70 65 72 61  tal-vacuum opera
11558 74 69 6f 6e 2e 20 54 68 65 20 50 61 67 65 72 2e  tion. The Pager.
11559 64 62 4f 72 69 67 53 69 7a 65 20 76 61 72 69 61  dbOrigSize varia
1155a 62 6c 65 0a 2a 2a 20 20 20 63 6f 6e 74 61 69 6e  ble.**   contain
1155b 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
1155c 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74  pages in the dat
1155d 61 62 61 73 65 20 69 6d 61 67 65 20 77 68 65 6e  abase image when
1155e 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20   the current.** 
1155f 20 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61    transaction wa
11560 73 20 6f 70 65 6e 65 64 2e 20 54 68 65 20 63 6f  s opened. The co
11561 6e 74 65 6e 74 73 20 6f 66 20 61 6c 6c 20 74 68  ntents of all th
11562 72 65 65 20 6f 66 20 74 68 65 73 65 20 76 61 72  ree of these var
11563 69 61 62 6c 65 73 20 69 73 0a 2a 2a 20 20 20 6f  iables is.**   o
11564 6e 6c 79 20 67 75 61 72 61 6e 74 65 65 64 20 74  nly guaranteed t
11565 6f 20 62 65 20 63 6f 72 72 65 63 74 20 69 66 20  o be correct if 
11566 74 68 65 20 62 6f 6f 6c 65 61 6e 20 50 61 67 65  the boolean Page
11567 72 2e 64 62 53 69 7a 65 56 61 6c 69 64 20 69 73  r.dbSizeValid is
11568 20 74 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 54   true..**.**   T
11569 4f 44 4f 3a 20 55 6e 64 65 72 20 77 68 61 74 20  ODO: Under what 
1156a 63 6f 6e 64 69 74 69 6f 6e 73 20 69 73 20 64 62  conditions is db
1156b 53 69 7a 65 56 61 6c 69 64 20 73 65 74 3f 20 43  SizeValid set? C
1156c 6c 65 61 72 65 64 3f 0a 2a 2a 0a 2a 2a 20 63 68  leared?.**.** ch
1156d 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 0a 2a 2a  angeCountDone.**
1156e 0a 2a 2a 20 20 20 54 68 69 73 20 62 6f 6f 6c 65  .**   This boole
1156f 61 6e 20 76 61 72 69 61 62 6c 65 20 69 73 20 75  an variable is u
11570 73 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  sed to make sure
11571 20 74 68 61 74 20 74 68 65 20 63 68 61 6e 67 65   that the change
11572 2d 63 6f 75 6e 74 65 72 20 0a 2a 2a 20 20 20 28  -counter .**   (
11573 74 68 65 20 34 2d 62 79 74 65 20 68 65 61 64 65  the 4-byte heade
11574 72 20 66 69 65 6c 64 20 61 74 20 62 79 74 65 20  r field at byte 
11575 6f 66 66 73 65 74 20 32 34 20 6f 66 20 74 68 65  offset 24 of the
11576 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 29 20   database file) 
11577 69 73 20 0a 2a 2a 20 20 20 6e 6f 74 20 75 70 64  is .**   not upd
11578 61 74 65 64 20 6d 6f 72 65 20 6f 66 74 65 6e 20  ated more often 
11579 74 68 61 6e 20 6e 65 63 65 73 73 61 72 79 2e 20  than necessary. 
1157a 0a 2a 2a 0a 2a 2a 20 20 20 49 74 20 69 73 20 73  .**.**   It is s
1157b 65 74 20 74 6f 20 74 72 75 65 20 77 68 65 6e 20  et to true when 
1157c 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  the change-count
1157d 65 72 20 66 69 65 6c 64 20 69 73 20 75 70 64 61  er field is upda
1157e 74 65 64 2c 20 77 68 69 63 68 20 0a 2a 2a 20 20  ted, which .**  
1157f 20 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e   can only happen
11580 20 69 66 20 61 6e 20 65 78 63 6c 75 73 69 76 65   if an exclusive
11581 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 20 6f 6e   lock is held on
11582 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
11583 6c 65 2e 0a 2a 2a 20 20 20 49 74 20 69 73 20 63  le..**   It is c
11584 6c 65 61 72 65 64 20 28 73 65 74 20 74 6f 20 66  leared (set to f
11585 61 6c 73 65 29 20 77 68 65 6e 65 76 65 72 20 61  alse) whenever a
11586 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
11587 20 69 73 20 0a 2a 2a 20 20 20 72 65 6c 69 6e 71   is .**   relinq
11588 75 69 73 68 65 64 20 6f 6e 20 74 68 65 20 64 61  uished on the da
11589 74 61 62 61 73 65 20 66 69 6c 65 2e 20 45 61 63  tabase file. Eac
1158a 68 20 74 69 6d 65 20 61 20 74 72 61 6e 73 61 63  h time a transac
1158b 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65  tion is committe
1158c 64 2c 0a 2a 2a 20 20 20 54 68 65 20 63 68 61 6e  d,.**   The chan
1158d 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67  geCountDone flag
1158e 20 69 73 20 69 6e 73 70 65 63 74 65 64 2e 20 49   is inspected. I
1158f 66 20 69 74 20 69 73 20 74 72 75 65 2c 20 74 68  f it is true, th
11590 65 20 77 6f 72 6b 20 6f 66 0a 2a 2a 20 20 20 75  e work of.**   u
11591 70 64 61 74 69 6e 67 20 74 68 65 20 63 68 61 6e  pdating the chan
11592 67 65 2d 63 6f 75 6e 74 65 72 20 69 73 20 6f 6d  ge-counter is om
11593 69 74 74 65 64 20 66 6f 72 20 74 68 65 20 63 75  itted for the cu
11594 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  rrent transactio
11595 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20  n..**.**   This 
11596 6d 65 63 68 61 6e 69 73 6d 20 6d 65 61 6e 73 20  mechanism means 
11597 74 68 61 74 20 77 68 65 6e 20 72 75 6e 6e 69 6e  that when runnin
11598 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d  g in exclusive m
11599 6f 64 65 2c 20 61 20 63 6f 6e 6e 65 63 74 69 6f  ode, a connectio
1159a 6e 20 0a 2a 2a 20 20 20 6e 65 65 64 20 6f 6e 6c  n .**   need onl
1159b 79 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61  y update the cha
1159c 6e 67 65 2d 63 6f 75 6e 74 65 72 20 6f 6e 63 65  nge-counter once
1159d 2c 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20  , for the first 
1159e 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20  transaction.**  
1159f 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a   committed..**.*
115a0 2a 20 64 62 4d 6f 64 69 66 69 65 64 0a 2a 2a 0a  * dbModified.**.
115a1 2a 2a 20 20 20 54 68 65 20 64 62 4d 6f 64 69 66  **   The dbModif
115a2 69 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 20  ied flag is set 
115a3 77 68 65 6e 65 76 65 72 20 61 20 64 61 74 61 62  whenever a datab
115a4 61 73 65 20 70 61 67 65 20 69 73 20 64 69 72 74  ase page is dirt
115a5 69 65 64 2e 0a 2a 2a 20 20 20 49 74 20 69 73 20  ied..**   It is 
115a6 63 6c 65 61 72 65 64 20 61 74 20 74 68 65 20 65  cleared at the e
115a7 6e 64 20 6f 66 20 65 61 63 68 20 74 72 61 6e 73  nd of each trans
115a8 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20  action..**.**   
115a9 49 74 20 69 73 20 75 73 65 64 20 77 68 65 6e 20  It is used when 
115aa 63 6f 6d 6d 69 74 74 69 6e 67 20 6f 72 20 6f 74  committing or ot
115ab 68 65 72 77 69 73 65 20 65 6e 64 69 6e 67 20 61  herwise ending a
115ac 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66   transaction. If
115ad 0a 2a 2a 20 20 20 74 68 65 20 64 62 4d 6f 64 69  .**   the dbModi
115ae 66 69 65 64 20 66 6c 61 67 20 69 73 20 63 6c 65  fied flag is cle
115af 61 72 20 74 68 65 6e 20 6c 65 73 73 20 77 6f 72  ar then less wor
115b0 6b 20 68 61 73 20 74 6f 20 62 65 20 64 6f 6e 65  k has to be done
115b1 2e 0a 2a 2a 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 53  ..**.** journalS
115b2 74 61 72 74 65 64 0a 2a 2a 0a 2a 2a 20 20 20 54  tarted.**.**   T
115b3 68 69 73 20 66 6c 61 67 20 69 73 20 73 65 74 20  his flag is set 
115b4 77 68 65 6e 65 76 65 72 20 74 68 65 20 74 68 65  whenever the the
115b5 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 69 73   main journal is
115b6 20 73 79 6e 63 65 64 2e 20 0a 2a 2a 0a 2a 2a 20   synced. .**.** 
115b7 20 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74    The point of t
115b8 68 69 73 20 66 6c 61 67 20 69 73 20 74 68 61 74  his flag is that
115b9 20 69 74 20 6d 75 73 74 20 62 65 20 73 65 74 20   it must be set 
115ba 61 66 74 65 72 20 74 68 65 20 0a 2a 2a 20 20 20  after the .**   
115bb 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65  first journal he
115bc 61 64 65 72 20 69 6e 20 61 20 6a 6f 75 72 6e 61  ader in a journa
115bd 6c 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20  l file has been 
115be 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 0a  synced to disk..
115bf 2a 2a 20 20 20 41 66 74 65 72 20 74 68 69 73 20  **   After this 
115c0 68 61 73 20 68 61 70 70 65 6e 65 64 2c 20 6e 65  has happened, ne
115c1 77 20 70 61 67 65 73 20 61 70 70 65 6e 64 65 64  w pages appended
115c2 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
115c3 20 0a 2a 2a 20 20 20 64 6f 20 6e 6f 74 20 6e 65   .**   do not ne
115c4 65 64 20 74 68 65 20 50 47 48 44 52 5f 4e 45 45  ed the PGHDR_NEE
115c5 44 5f 53 59 4e 43 20 66 6c 61 67 20 73 65 74 2c  D_SYNC flag set,
115c6 20 61 73 20 74 68 65 79 20 64 6f 20 6e 6f 74 20   as they do not 
115c7 6e 65 65 64 0a 2a 2a 20 20 20 74 6f 20 77 61 69  need.**   to wai
115c8 74 20 66 6f 72 20 61 20 6a 6f 75 72 6e 61 6c 20  t for a journal 
115c9 73 79 6e 63 20 62 65 66 6f 72 65 20 74 68 65 79  sync before they
115ca 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20   can be written 
115cb 6f 75 74 20 74 6f 0a 2a 2a 20 20 20 74 68 65 20  out to.**   the 
115cc 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28 73  database file (s
115cd 65 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 67 65  ee function page
115ce 72 5f 77 72 69 74 65 28 29 29 2e 0a 2a 2a 20 20  r_write())..**  
115cf 20 0a 2a 2a 20 73 65 74 4d 61 73 74 65 72 0a 2a   .** setMaster.*
115d0 2a 0a 2a 2a 20 20 20 54 68 69 73 20 76 61 72 69  *.**   This vari
115d1 61 62 6c 65 20 69 73 20 75 73 65 64 20 74 6f 20  able is used to 
115d2 65 6e 73 75 72 65 20 74 68 61 74 20 74 68 65 20  ensure that the 
115d3 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
115d4 69 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20 28 69  ile name.**   (i
115d5 66 20 61 6e 79 29 20 69 73 20 6f 6e 6c 79 20 77  f any) is only w
115d6 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20  ritten into the 
115d7 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f 6e 63  journal file onc
115d8 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 57 68 65 6e 20  e..**.**   When 
115d9 63 6f 6d 6d 69 74 74 69 6e 67 20 61 20 74 72 61  committing a tra
115da 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 20 6d 61  nsaction, the ma
115db 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
115dc 65 20 6e 61 6d 65 20 28 69 66 20 61 6e 79 29 0a  e name (if any).
115dd 2a 2a 20 20 20 6d 61 79 20 62 65 20 77 72 69 74  **   may be writ
115de 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75  ten into the jou
115df 72 6e 61 6c 20 66 69 6c 65 20 77 68 69 6c 65 20  rnal file while 
115e0 74 68 65 20 70 61 67 65 72 20 69 73 20 73 74 69  the pager is sti
115e1 6c 6c 20 69 6e 0a 2a 2a 20 20 20 50 41 47 45 52  ll in.**   PAGER
115e2 5f 52 45 53 45 52 56 45 44 20 73 74 61 74 65 20  _RESERVED state 
115e3 28 73 65 65 20 43 6f 6d 6d 69 74 50 68 61 73 65  (see CommitPhase
115e4 4f 6e 65 28 29 20 66 6f 72 20 74 68 65 20 61 63  One() for the ac
115e5 74 69 6f 6e 29 2e 20 49 74 0a 2a 2a 20 20 20 74  tion). It.**   t
115e6 68 65 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20  hen attempts to 
115e7 75 70 67 72 61 64 65 20 74 6f 20 61 6e 20 65 78  upgrade to an ex
115e8 63 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 49 66  clusive lock. If
115e9 20 74 68 69 73 20 61 74 74 65 6d 70 74 0a 2a 2a   this attempt.**
115ea 20 20 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 53     fails, then S
115eb 51 4c 49 54 45 5f 42 55 53 59 20 6d 61 79 20 62  QLITE_BUSY may b
115ec 65 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68  e returned to th
115ed 65 20 75 73 65 72 20 61 6e 64 20 74 68 65 20 75  e user and the u
115ee 73 65 72 0a 2a 2a 20 20 20 6d 61 79 20 61 74 74  ser.**   may att
115ef 65 6d 70 74 20 74 6f 20 63 6f 6d 6d 69 74 20 74  empt to commit t
115f0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  he transaction a
115f1 67 61 69 6e 20 6c 61 74 65 72 20 28 63 61 6c 6c  gain later (call
115f2 69 6e 67 0a 2a 2a 20 20 20 43 6f 6d 6d 69 74 50  ing.**   CommitP
115f3 68 61 73 65 4f 6e 65 28 29 20 61 67 61 69 6e 29  haseOne() again)
115f4 2e 20 54 68 69 73 20 66 6c 61 67 20 69 73 20 75  . This flag is u
115f5 73 65 64 20 74 6f 20 65 6e 73 75 72 65 20 74 68  sed to ensure th
115f6 61 74 20 74 68 65 20 0a 2a 2a 20 20 20 6d 61 73  at the .**   mas
115f7 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65  ter journal name
115f8 20 69 73 20 6f 6e 6c 79 20 77 72 69 74 74 65 6e   is only written
115f9 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   to the journal 
115fa 66 69 6c 65 20 74 68 65 20 66 69 72 73 74 0a 2a  file the first.*
115fb 2a 20 20 20 74 69 6d 65 20 43 6f 6d 6d 69 74 50  *   time CommitP
115fc 68 61 73 65 4f 6e 65 28 29 20 69 73 20 63 61 6c  haseOne() is cal
115fd 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 64 6f 4e 6f 74  led..**.** doNot
115fe 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69  Sync.**.**   Thi
115ff 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65  s variable is se
11600 74 20 61 6e 64 20 63 6c 65 61 72 65 64 20 62 79  t and cleared by
11601 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
11602 74 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 6e 65 65 64  te()..**.** need
11603 53 79 6e 63 0a 2a 2a 0a 2a 2a 20 20 20 54 4f 44  Sync.**.**   TOD
11604 4f 3a 20 49 74 20 6d 69 67 68 74 20 62 65 20 65  O: It might be e
11605 61 73 69 65 72 20 74 6f 20 73 65 74 20 74 68 69  asier to set thi
11606 73 20 76 61 72 69 61 62 6c 65 20 69 6e 20 77 72  s variable in wr
11607 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28 29 0a  iteJournalHdr().
11608 2a 2a 20 20 20 61 6e 64 20 77 72 69 74 65 4d 61  **   and writeMa
11609 73 74 65 72 4a 6f 75 72 6e 61 6c 28 29 20 6f 6e  sterJournal() on
1160a 6c 79 2e 20 43 68 61 6e 67 65 20 69 74 73 20 6d  ly. Change its m
1160b 65 61 6e 69 6e 67 20 74 6f 20 22 75 6e 73 79 6e  eaning to "unsyn
1160c 63 65 64 20 64 61 74 61 0a 2a 2a 20 20 20 68 61  ced data.**   ha
1160d 73 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74  s been written t
1160e 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 22 2e 0a  o the journal"..
1160f 2a 2a 0a 2a 2a 20 73 75 62 6a 49 6e 4d 65 6d 6f  **.** subjInMemo
11610 72 79 0a 2a 2a 0a 2a 2a 20 20 20 54 68 69 73 20  ry.**.**   This 
11611 69 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 72  is a boolean var
11612 69 61 62 6c 65 2e 20 49 66 20 74 72 75 65 2c 20  iable. If true, 
11613 74 68 65 6e 20 61 6e 79 20 72 65 71 75 69 72 65  then any require
11614 64 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a  d sub-journal.**
11615 20 20 20 69 73 20 6f 70 65 6e 65 64 20 61 73 20     is opened as 
11616 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75  an in-memory jou
11617 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20 66 61  rnal file. If fa
11618 6c 73 65 2c 20 74 68 65 6e 20 69 6e 2d 6d 65 6d  lse, then in-mem
11619 6f 72 79 0a 2a 2a 20 20 20 73 75 62 2d 6a 6f 75  ory.**   sub-jou
1161a 72 6e 61 6c 73 20 61 72 65 20 6f 6e 6c 79 20 75  rnals are only u
1161b 73 65 64 20 66 6f 72 20 69 6e 2d 6d 65 6d 6f 72  sed for in-memor
1161c 79 20 70 61 67 65 72 20 66 69 6c 65 73 2e 0a 2a  y pager files..*
1161d 2f 0a 73 74 72 75 63 74 20 50 61 67 65 72 20 7b  /.struct Pager {
1161e 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
1161f 70 56 66 73 3b 20 20 20 20 20 20 20 20 20 20 2f  pVfs;          /
11620 2a 20 4f 53 20 66 75 6e 63 74 69 6f 6e 73 20 74  * OS functions t
11621 6f 20 75 73 65 20 66 6f 72 20 49 4f 20 2a 2f 0a  o use for IO */.
11622 20 20 75 38 20 65 78 63 6c 75 73 69 76 65 4d 6f    u8 exclusiveMo
11623 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  de;           /*
11624 20 42 6f 6f 6c 65 61 6e 2e 20 54 72 75 65 20 69   Boolean. True i
11625 66 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 3d  f locking_mode==
11626 45 58 43 4c 55 53 49 56 45 20 2a 2f 0a 20 20 75  EXCLUSIVE */.  u
11627 38 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 20 20  8 journalMode;  
11628 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
11629 20 6f 66 20 74 68 65 20 50 41 47 45 52 5f 4a 4f   of the PAGER_JO
1162a 55 52 4e 41 4c 4d 4f 44 45 5f 2a 20 76 61 6c 75  URNALMODE_* valu
1162b 65 73 20 2a 2f 0a 20 20 75 38 20 75 73 65 4a 6f  es */.  u8 useJo
1162c 75 72 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20  urnal;          
1162d 20 20 20 20 2f 2a 20 55 73 65 20 61 20 72 6f 6c      /* Use a rol
1162e 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 6f 6e  lback journal on
1162f 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20   this file */.  
11630 75 38 20 6e 6f 52 65 61 64 6c 6f 63 6b 3b 20 20  u8 noReadlock;  
11631 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
11632 6f 20 6e 6f 74 20 62 6f 74 68 65 72 20 74 6f 20  o not bother to 
11633 6f 62 74 61 69 6e 20 72 65 61 64 6c 6f 63 6b 73  obtain readlocks
11634 20 2a 2f 0a 20 20 75 38 20 6e 6f 53 79 6e 63 3b   */.  u8 noSync;
11635 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11636 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 73 79 6e 63    /* Do not sync
11637 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 66 20   the journal if 
11638 74 72 75 65 20 2a 2f 0a 20 20 75 38 20 66 75 6c  true */.  u8 ful
11639 6c 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20  lSync;          
1163a 20 20 20 20 20 20 2f 2a 20 44 6f 20 65 78 74 72        /* Do extr
1163b 61 20 73 79 6e 63 73 20 6f 66 20 74 68 65 20 6a  a syncs of the j
1163c 6f 75 72 6e 61 6c 20 66 6f 72 20 72 6f 62 75 73  ournal for robus
1163d 74 6e 65 73 73 20 2a 2f 0a 20 20 75 38 20 73 79  tness */.  u8 sy
1163e 6e 63 5f 66 6c 61 67 73 3b 20 20 20 20 20 20 20  nc_flags;       
1163f 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66         /* One of
11640 20 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 6f 72 20   SYNC_NORMAL or 
11641 53 59 4e 43 5f 46 55 4c 4c 20 2a 2f 0a 20 20 75  SYNC_FULL */.  u
11642 38 20 74 65 6d 70 46 69 6c 65 3b 20 20 20 20 20  8 tempFile;     
11643 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 46             /* zF
11644 69 6c 65 6e 61 6d 65 20 69 73 20 61 20 74 65 6d  ilename is a tem
11645 70 6f 72 61 72 79 20 66 69 6c 65 20 2a 2f 0a 20  porary file */. 
11646 20 75 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20   u8 readOnly;   
11647 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11648 54 72 75 65 20 66 6f 72 20 61 20 72 65 61 64 2d  True for a read-
11649 6f 6e 6c 79 20 64 61 74 61 62 61 73 65 20 2a 2f  only database */
1164a 0a 20 20 75 38 20 6d 65 6d 44 62 3b 20 20 20 20  .  u8 memDb;    
1164b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1164c 2a 20 54 72 75 65 20 74 6f 20 69 6e 68 69 62 69  * True to inhibi
1164d 74 20 61 6c 6c 20 66 69 6c 65 20 49 2f 4f 20 2a  t all file I/O *
1164e 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  /..  /* The foll
1164f 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 63 6f 6e 74  owing block cont
11650 61 69 6e 73 20 74 68 6f 73 65 20 63 6c 61 73 73  ains those class
11651 20 6d 65 6d 62 65 72 73 20 74 68 61 74 20 61 72   members that ar
11652 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 20 20  e dynamically.  
11653 2a 2a 20 6d 6f 64 69 66 69 65 64 20 64 75 72 69  ** modified duri
11654 6e 67 20 6e 6f 72 6d 61 6c 20 6f 70 65 72 61 74  ng normal operat
11655 69 6f 6e 73 2e 20 54 68 65 20 6f 74 68 65 72 20  ions. The other 
11656 76 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68 69  variables in thi
11657 73 20 73 74 72 75 63 74 75 72 65 0a 20 20 2a 2a  s structure.  **
11658 20 61 72 65 20 65 69 74 68 65 72 20 63 6f 6e 73   are either cons
11659 74 61 6e 74 20 74 68 72 6f 75 67 68 6f 75 74 20  tant throughout 
1165a 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20  the lifetime of 
1165b 74 68 65 20 70 61 67 65 72 2c 20 6f 72 20 65 6c  the pager, or el
1165c 73 65 0a 20 20 2a 2a 20 75 73 65 64 20 74 6f 20  se.  ** used to 
1165d 73 74 6f 72 65 20 63 6f 6e 66 69 67 75 72 61 74  store configurat
1165e 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 73 20 74  ion parameters t
1165f 68 61 74 20 61 66 66 65 63 74 20 74 68 65 20 77  hat affect the w
11660 61 79 20 74 68 65 20 70 61 67 65 72 20 0a 20 20  ay the pager .  
11661 2a 2a 20 6f 70 65 72 61 74 65 73 2e 0a 20 20 2a  ** operates..  *
11662 2a 0a 20 20 2a 2a 20 54 68 65 20 27 73 74 61 74  *.  ** The 'stat
11663 65 27 20 76 61 72 69 61 62 6c 65 20 69 73 20 64  e' variable is d
11664 65 73 63 72 69 62 65 64 20 69 6e 20 6d 6f 72 65  escribed in more
11665 20 64 65 74 61 69 6c 20 61 6c 6f 6e 67 20 77 69   detail along wi
11666 74 68 20 74 68 65 0a 20 20 2a 2a 20 64 65 73 63  th the.  ** desc
11667 72 69 70 74 69 6f 6e 73 20 6f 66 20 74 68 65 20  riptions of the 
11668 76 61 6c 75 65 73 20 69 74 20 6d 61 79 20 74 61  values it may ta
11669 6b 65 20 2d 20 50 41 47 45 52 5f 55 4e 4c 4f 43  ke - PAGER_UNLOC
1166a 4b 20 65 74 63 2e 20 4d 61 6e 79 20 6f 66 20 74  K etc. Many of t
1166b 68 65 0a 20 20 2a 2a 20 6f 74 68 65 72 20 76 61  he.  ** other va
1166c 72 69 61 62 6c 65 73 20 69 6e 20 74 68 69 73 20  riables in this 
1166d 62 6c 6f 63 6b 20 61 72 65 20 64 65 73 63 72 69  block are descri
1166e 62 65 64 20 69 6e 20 74 68 65 20 63 6f 6d 6d 65  bed in the comme
1166f 6e 74 20 64 69 72 65 63 74 6c 79 20 0a 20 20 2a  nt directly .  *
11670 2a 20 61 62 6f 76 65 20 74 68 69 73 20 63 6c 61  * above this cla
11671 73 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 20  ss definition.. 
11672 20 2a 2f 0a 20 20 75 38 20 73 74 61 74 65 3b 20   */.  u8 state; 
11673 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11674 20 20 2f 2a 20 50 41 47 45 52 5f 55 4e 4c 4f 43    /* PAGER_UNLOC
11675 4b 2c 20 5f 53 48 41 52 45 44 2c 20 5f 52 45 53  K, _SHARED, _RES
11676 45 52 56 45 44 2c 20 65 74 63 2e 20 2a 2f 0a 20  ERVED, etc. */. 
11677 20 75 38 20 64 62 4d 6f 64 69 66 69 65 64 3b 20   u8 dbModified; 
11678 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11679 54 72 75 65 20 69 66 20 74 68 65 72 65 20 61 72  True if there ar
1167a 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20 74 6f  e any changes to
1167b 20 74 68 65 20 44 62 20 2a 2f 0a 20 20 75 38 20   the Db */.  u8 
1167c 6e 65 65 64 53 79 6e 63 3b 20 20 20 20 20 20 20  needSync;       
1167d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
1167e 20 69 66 20 61 6e 20 66 73 79 6e 63 28 29 20 69   if an fsync() i
1167f 73 20 6e 65 65 64 65 64 20 6f 6e 20 74 68 65 20  s needed on the 
11680 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20  journal */.  u8 
11681 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 3b 20  journalStarted; 
11682 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
11683 20 69 66 20 68 65 61 64 65 72 20 6f 66 20 6a 6f   if header of jo
11684 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20  urnal is synced 
11685 2a 2f 0a 20 20 75 38 20 63 68 61 6e 67 65 43 6f  */.  u8 changeCo
11686 75 6e 74 44 6f 6e 65 3b 20 20 20 20 20 20 20 20  untDone;        
11687 20 2f 2a 20 53 65 74 20 61 66 74 65 72 20 69 6e   /* Set after in
11688 63 72 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 63  crementing the c
11689 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72 20 2a 2f  hange-counter */
1168a 0a 20 20 75 38 20 73 65 74 4d 61 73 74 65 72 3b  .  u8 setMaster;
1168b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1168c 2a 20 54 72 75 65 20 69 66 20 61 20 6d 2d 6a 20  * True if a m-j 
1168d 6e 61 6d 65 20 68 61 73 20 62 65 65 6e 20 77 72  name has been wr
1168e 69 74 74 65 6e 20 74 6f 20 6a 72 6e 6c 20 2a 2f  itten to jrnl */
1168f 0a 20 20 75 38 20 64 6f 4e 6f 74 53 79 6e 63 3b  .  u8 doNotSync;
11690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11691 2a 20 42 6f 6f 6c 65 61 6e 2e 20 57 68 69 6c 65  * Boolean. While
11692 20 74 72 75 65 2c 20 64 6f 20 6e 6f 74 20 73 70   true, do not sp
11693 69 6c 6c 20 74 68 65 20 63 61 63 68 65 20 2a 2f  ill the cache */
11694 0a 20 20 75 38 20 64 62 53 69 7a 65 56 61 6c 69  .  u8 dbSizeVali
11695 64 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  d;             /
11696 2a 20 53 65 74 20 77 68 65 6e 20 64 62 53 69 7a  * Set when dbSiz
11697 65 20 69 73 20 63 6f 72 72 65 63 74 20 2a 2f 0a  e is correct */.
11698 20 20 75 38 20 73 75 62 6a 49 6e 4d 65 6d 6f 72    u8 subjInMemor
11699 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  y;            /*
1169a 20 54 72 75 65 20 74 6f 20 75 73 65 20 69 6e 2d   True to use in-
1169b 6d 65 6d 6f 72 79 20 73 75 62 2d 6a 6f 75 72 6e  memory sub-journ
1169c 61 6c 73 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62  als */.  Pgno db
1169d 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20  Size;           
1169e 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1169f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64  f pages in the d
116a0 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 50 67 6e  atabase */.  Pgn
116a1 6f 20 64 62 4f 72 69 67 53 69 7a 65 3b 20 20 20  o dbOrigSize;   
116a2 20 20 20 20 20 20 20 20 20 2f 2a 20 64 62 53 69           /* dbSi
116a3 7a 65 20 62 65 66 6f 72 65 20 74 68 65 20 63 75  ze before the cu
116a4 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  rrent transactio
116a5 6e 20 2a 2f 0a 20 20 50 67 6e 6f 20 64 62 46 69  n */.  Pgno dbFi
116a6 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20  leSize;         
116a7 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
116a8 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61 74  pages in the dat
116a9 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20  abase file */.  
116aa 69 6e 74 20 65 72 72 43 6f 64 65 3b 20 20 20 20  int errCode;    
116ab 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
116ac 6e 65 20 6f 66 20 73 65 76 65 72 61 6c 20 6b 69  ne of several ki
116ad 6e 64 73 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f  nds of errors */
116ae 0a 20 20 69 6e 74 20 6e 52 65 63 3b 20 20 20 20  .  int nRec;    
116af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
116b0 2a 20 50 61 67 65 73 20 6a 6f 75 72 6e 61 6c 6c  * Pages journall
116b1 65 64 20 73 69 6e 63 65 20 6c 61 73 74 20 6a 2d  ed since last j-
116b2 68 65 61 64 65 72 20 77 72 69 74 74 65 6e 20 2a  header written *
116b3 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 49 6e 69  /.  u32 cksumIni
116b4 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
116b5 2f 2a 20 51 75 61 73 69 2d 72 61 6e 64 6f 6d 20  /* Quasi-random 
116b6 76 61 6c 75 65 20 61 64 64 65 64 20 74 6f 20 65  value added to e
116b7 76 65 72 79 20 63 68 65 63 6b 73 75 6d 20 2a 2f  very checksum */
116b8 0a 20 20 75 33 32 20 6e 53 75 62 52 65 63 3b 20  .  u32 nSubRec; 
116b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
116ba 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 63 6f  * Number of reco
116bb 72 64 73 20 77 72 69 74 74 65 6e 20 74 6f 20 73  rds written to s
116bc 75 62 2d 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20  ub-journal */.  
116bd 42 69 74 76 65 63 20 2a 70 49 6e 4a 6f 75 72 6e  Bitvec *pInJourn
116be 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  al;         /* O
116bf 6e 65 20 62 69 74 20 66 6f 72 20 65 61 63 68 20  ne bit for each 
116c0 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61  page in the data
116c1 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20 73  base file */.  s
116c2 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 3b  qlite3_file *fd;
116c3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
116c4 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66 6f  le descriptor fo
116c5 72 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  r database */.  
116c6 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 6a 66  sqlite3_file *jf
116c7 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  d;          /* F
116c8 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 66  ile descriptor f
116c9 6f 72 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20  or main journal 
116ca 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  */.  sqlite3_fil
116cb 65 20 2a 73 6a 66 64 3b 20 20 20 20 20 20 20 20  e *sjfd;        
116cc 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69 70   /* File descrip
116cd 74 6f 72 20 66 6f 72 20 73 75 62 2d 6a 6f 75 72  tor for sub-jour
116ce 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75  nal */.  i64 jou
116cf 72 6e 61 6c 4f 66 66 3b 20 20 20 20 20 20 20 20  rnalOff;        
116d0 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
116d1 77 72 69 74 65 20 6f 66 66 73 65 74 20 69 6e 20  write offset in 
116d2 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
116d3 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61   */.  i64 journa
116d4 6c 48 64 72 3b 20 20 20 20 20 20 20 20 20 20 20  lHdr;           
116d5 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74    /* Byte offset
116d6 20 74 6f 20 70 72 65 76 69 6f 75 73 20 6a 6f 75   to previous jou
116d7 72 6e 61 6c 20 68 65 61 64 65 72 20 2a 2f 0a 20  rnal header */. 
116d8 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 20   PagerSavepoint 
116d9 2a 61 53 61 76 65 70 6f 69 6e 74 3b 20 2f 2a 20  *aSavepoint; /* 
116da 41 72 72 61 79 20 6f 66 20 61 63 74 69 76 65 20  Array of active 
116db 73 61 76 65 70 6f 69 6e 74 73 20 2a 2f 0a 20 20  savepoints */.  
116dc 69 6e 74 20 6e 53 61 76 65 70 6f 69 6e 74 3b 20  int nSavepoint; 
116dd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
116de 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74  umber of element
116df 73 20 69 6e 20 61 53 61 76 65 70 6f 69 6e 74 5b  s in aSavepoint[
116e0 5d 20 2a 2f 0a 20 20 63 68 61 72 20 64 62 46 69  ] */.  char dbFi
116e1 6c 65 56 65 72 73 5b 31 36 5d 3b 20 20 20 20 20  leVers[16];     
116e2 20 20 20 2f 2a 20 43 68 61 6e 67 65 73 20 77 68     /* Changes wh
116e3 65 6e 65 76 65 72 20 64 61 74 61 62 61 73 65 20  enever database 
116e4 66 69 6c 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a  file changes */.
116e5 20 20 75 33 32 20 73 65 63 74 6f 72 53 69 7a 65    u32 sectorSize
116e6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
116e7 20 41 73 73 75 6d 65 64 20 73 65 63 74 6f 72 20   Assumed sector 
116e8 73 69 7a 65 20 64 75 72 69 6e 67 20 72 6f 6c 6c  size during roll
116e9 62 61 63 6b 20 2a 2f 0a 0a 20 20 75 31 36 20 6e  back */..  u16 n
116ea 45 78 74 72 61 3b 20 20 20 20 20 20 20 20 20 20  Extra;          
116eb 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68         /* Add th
116ec 69 73 20 6d 61 6e 79 20 62 79 74 65 73 20 74 6f  is many bytes to
116ed 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20   each in-memory 
116ee 70 61 67 65 20 2a 2f 0a 20 20 69 31 36 20 6e 52  page */.  i16 nR
116ef 65 73 65 72 76 65 3b 20 20 20 20 20 20 20 20 20  eserve;         
116f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
116f1 6f 66 20 75 6e 75 73 65 64 20 62 79 74 65 73 20  of unused bytes 
116f2 61 74 20 65 6e 64 20 6f 66 20 65 61 63 68 20 70  at end of each p
116f3 61 67 65 20 2a 2f 0a 20 20 75 33 32 20 76 66 73  age */.  u32 vfs
116f4 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  Flags;          
116f5 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 66 6f       /* Flags fo
116f6 72 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f  r sqlite3_vfs.xO
116f7 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 70  pen() */.  int p
116f8 61 67 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20  ageSize;        
116f9 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
116fa 20 6f 66 20 62 79 74 65 73 20 69 6e 20 61 20 70   of bytes in a p
116fb 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78  age */.  Pgno mx
116fc 50 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20  Pgno;           
116fd 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
116fe 61 6c 6c 6f 77 65 64 20 73 69 7a 65 20 6f 66 20  allowed size of 
116ff 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  the database */.
11700 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d    char *zFilenam
11701 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
11702 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   Name of the dat
11703 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a 20 20  abase file */.  
11704 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b 20  char *zJournal; 
11705 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
11706 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  ame of the journ
11707 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74  al file */.  int
11708 20 28 2a 78 42 75 73 79 48 61 6e 64 6c 65 72 29   (*xBusyHandler)
11709 28 76 6f 69 64 2a 29 3b 20 2f 2a 20 46 75 6e 63  (void*); /* Func
1170a 74 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 77 68 65  tion to call whe
1170b 6e 20 62 75 73 79 20 2a 2f 0a 20 20 76 6f 69 64  n busy */.  void
1170c 20 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72   *pBusyHandlerAr
1170d 67 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65  g;      /* Conte
1170e 78 74 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20  xt argument for 
1170f 78 42 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a  xBusyHandler */.
11710 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
11711 53 54 0a 20 20 69 6e 74 20 6e 48 69 74 2c 20 6e  ST.  int nHit, n
11712 4d 69 73 73 3b 20 20 20 20 20 20 20 20 20 20 20  Miss;           
11713 20 2f 2a 20 43 61 63 68 65 20 68 69 74 73 20 61   /* Cache hits a
11714 6e 64 20 6d 69 73 73 69 6e 67 20 2a 2f 0a 20 20  nd missing */.  
11715 69 6e 74 20 6e 52 65 61 64 2c 20 6e 57 72 69 74  int nRead, nWrit
11716 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  e;          /* D
11717 61 74 61 62 61 73 65 20 70 61 67 65 73 20 72 65  atabase pages re
11718 61 64 2f 77 72 69 74 74 65 6e 20 2a 2f 0a 23 65  ad/written */.#e
11719 6e 64 69 66 0a 20 20 76 6f 69 64 20 28 2a 78 52  ndif.  void (*xR
1171a 65 69 6e 69 74 65 72 29 28 44 62 50 61 67 65 2a  einiter)(DbPage*
1171b 29 3b 20 2f 2a 20 43 61 6c 6c 20 74 68 69 73 20  ); /* Call this 
1171c 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 72 65 6c  routine when rel
1171d 6f 61 64 69 6e 67 20 70 61 67 65 73 20 2a 2f 0a  oading pages */.
1171e 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41  #ifdef SQLITE_HA
1171f 53 5f 43 4f 44 45 43 0a 20 20 76 6f 69 64 20 2a  S_CODEC.  void *
11720 28 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c  (*xCodec)(void*,
11721 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b  void*,Pgno,int);
11722 20 2f 2a 20 52 6f 75 74 69 6e 65 20 66 6f 72 20   /* Routine for 
11723 65 6e 2f 64 65 63 6f 64 69 6e 67 20 64 61 74 61  en/decoding data
11724 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 6f   */.  void (*xCo
11725 64 65 63 53 69 7a 65 43 68 6e 67 29 28 76 6f 69  decSizeChng)(voi
11726 64 2a 2c 69 6e 74 2c 69 6e 74 29 3b 20 2f 2a 20  d*,int,int); /* 
11727 4e 6f 74 69 66 79 20 6f 66 20 70 61 67 65 20 73  Notify of page s
11728 69 7a 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 20  ize changes */. 
11729 20 76 6f 69 64 20 28 2a 78 43 6f 64 65 63 46 72   void (*xCodecFr
1172a 65 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20  ee)(void*);     
1172b 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 72          /* Destr
1172c 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 63 6f  uctor for the co
1172d 64 65 63 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70  dec */.  void *p
1172e 43 6f 64 65 63 3b 20 20 20 20 20 20 20 20 20 20  Codec;          
1172f 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72       /* First ar
11730 67 75 6d 65 6e 74 20 74 6f 20 78 43 6f 64 65 63  gument to xCodec
11731 2e 2e 2e 20 6d 65 74 68 6f 64 73 20 2a 2f 0a 23  ... methods */.#
11732 65 6e 64 69 66 0a 20 20 63 68 61 72 20 2a 70 54  endif.  char *pT
11733 6d 70 53 70 61 63 65 3b 20 20 20 20 20 20 20 20  mpSpace;        
11734 20 20 20 20 2f 2a 20 50 61 67 65 72 2e 70 61 67      /* Pager.pag
11735 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73  eSize bytes of s
11736 70 61 63 65 20 66 6f 72 20 74 6d 70 20 75 73 65  pace for tmp use
11737 20 2a 2f 0a 20 20 69 36 34 20 6a 6f 75 72 6e 61   */.  i64 journa
11738 6c 53 69 7a 65 4c 69 6d 69 74 3b 20 20 20 20 20  lSizeLimit;     
11739 20 20 2f 2a 20 53 69 7a 65 20 6c 69 6d 69 74 20    /* Size limit 
1173a 66 6f 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a  for persistent j
1173b 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a  ournal files */.
1173c 20 20 50 43 61 63 68 65 20 2a 70 50 43 61 63 68    PCache *pPCach
1173d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
1173e 20 50 6f 69 6e 74 65 72 20 74 6f 20 70 61 67 65   Pointer to page
1173f 20 63 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f   cache object */
11740 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  .  sqlite3_backu
11741 70 20 2a 70 42 61 63 6b 75 70 3b 20 20 20 20 2f  p *pBackup;    /
11742 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6c 69 73  * Pointer to lis
11743 74 20 6f 66 20 6f 6e 67 6f 69 6e 67 20 62 61 63  t of ongoing bac
11744 6b 75 70 20 70 72 6f 63 65 73 73 65 73 20 2a 2f  kup processes */
11745 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  .};../*.** The f
11746 6f 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20  ollowing global 
11747 76 61 72 69 61 62 6c 65 73 20 68 6f 6c 64 20 63  variables hold c
11748 6f 75 6e 74 65 72 73 20 75 73 65 64 20 66 6f 72  ounters used for
11749 0a 2a 2a 20 74 65 73 74 69 6e 67 20 70 75 72 70  .** testing purp
1174a 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 54 68 65 73  oses only.  Thes
1174b 65 20 76 61 72 69 61 62 6c 65 73 20 64 6f 20 6e  e variables do n
1174c 6f 74 20 65 78 69 73 74 20 69 6e 0a 2a 2a 20 61  ot exist in.** a
1174d 20 6e 6f 6e 2d 74 65 73 74 69 6e 67 20 62 75 69   non-testing bui
1174e 6c 64 2e 20 20 54 68 65 73 65 20 76 61 72 69 61  ld.  These varia
1174f 62 6c 65 73 20 61 72 65 20 6e 6f 74 20 74 68 72  bles are not thr
11750 65 61 64 2d 73 61 66 65 2e 0a 2a 2f 0a 23 69 66  ead-safe..*/.#if
11751 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
11752 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
11753 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 72 65 61  qlite3_pager_rea
11754 64 64 62 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20  ddb_count = 0;  
11755 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66    /* Number of f
11756 75 6c 6c 20 70 61 67 65 73 20 72 65 61 64 20 66  ull pages read f
11757 72 6f 6d 20 44 42 20 2a 2f 0a 53 51 4c 49 54 45  rom DB */.SQLITE
11758 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
11759 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f 63  _pager_writedb_c
1175a 6f 75 6e 74 20 3d 20 30 3b 20 20 20 2f 2a 20 4e  ount = 0;   /* N
1175b 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 61  umber of full pa
1175c 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20 44  ges written to D
1175d 42 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  B */.SQLITE_API 
1175e 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65  int sqlite3_page
1175f 72 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74 20 3d  r_writej_count =
11760 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72   0;    /* Number
11761 20 6f 66 20 70 61 67 65 73 20 77 72 69 74 74 65   of pages writte
11762 6e 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a  n to journal */.
11763 23 20 64 65 66 69 6e 65 20 50 41 47 45 52 5f 49  # define PAGER_I
11764 4e 43 52 28 76 29 20 20 76 2b 2b 0a 23 65 6c 73  NCR(v)  v++.#els
11765 65 0a 23 20 64 65 66 69 6e 65 20 50 41 47 45 52  e.# define PAGER
11766 5f 49 4e 43 52 28 76 29 0a 23 65 6e 64 69 66 0a  _INCR(v).#endif.
11767 0a 0a 0a 2f 2a 0a 2a 2a 20 4a 6f 75 72 6e 61 6c  .../*.** Journal
11768 20 66 69 6c 65 73 20 62 65 67 69 6e 20 77 69 74   files begin wit
11769 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  h the following 
1176a 6d 61 67 69 63 20 73 74 72 69 6e 67 2e 20 20 54  magic string.  T
1176b 68 65 20 64 61 74 61 0a 2a 2a 20 77 61 73 20 6f  he data.** was o
1176c 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 2f 64 65  btained from /de
1176d 76 2f 72 61 6e 64 6f 6d 2e 20 20 49 74 20 69 73  v/random.  It is
1176e 20 75 73 65 64 20 6f 6e 6c 79 20 61 73 20 61 20   used only as a 
1176f 73 61 6e 69 74 79 20 63 68 65 63 6b 2e 0a 2a 2a  sanity check..**
11770 0a 2a 2a 20 53 69 6e 63 65 20 76 65 72 73 69 6f  .** Since versio
11771 6e 20 32 2e 38 2e 30 2c 20 74 68 65 20 6a 6f 75  n 2.8.0, the jou
11772 72 6e 61 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 74  rnal format cont
11773 61 69 6e 73 20 61 64 64 69 74 69 6f 6e 61 6c 20  ains additional 
11774 73 61 6e 69 74 79 0a 2a 2a 20 63 68 65 63 6b 69  sanity.** checki
11775 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20  ng information. 
11776 20 49 66 20 74 68 65 20 70 6f 77 65 72 20 66 61   If the power fa
11777 69 6c 73 20 77 68 69 6c 65 20 74 68 65 20 6a 6f  ils while the jo
11778 75 72 6e 61 6c 20 69 73 20 62 65 69 6e 67 0a 2a  urnal is being.*
11779 2a 20 77 72 69 74 74 65 6e 2c 20 73 65 6d 69 2d  * written, semi-
1177a 72 61 6e 64 6f 6d 20 67 61 72 62 61 67 65 20 64  random garbage d
1177b 61 74 61 20 6d 69 67 68 74 20 61 70 70 65 61 72  ata might appear
1177c 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a   in the journal.
1177d 2a 2a 20 66 69 6c 65 20 61 66 74 65 72 20 70 6f  ** file after po
1177e 77 65 72 20 69 73 20 72 65 73 74 6f 72 65 64 2e  wer is restored.
1177f 20 20 49 66 20 61 6e 20 61 74 74 65 6d 70 74 20    If an attempt 
11780 69 73 20 74 68 65 6e 20 6d 61 64 65 0a 2a 2a 20  is then made.** 
11781 74 6f 20 72 6f 6c 6c 20 74 68 65 20 6a 6f 75 72  to roll the jour
11782 6e 61 6c 20 62 61 63 6b 2c 20 74 68 65 20 64 61  nal back, the da
11783 74 61 62 61 73 65 20 63 6f 75 6c 64 20 62 65 20  tabase could be 
11784 63 6f 72 72 75 70 74 65 64 2e 20 20 54 68 65 20  corrupted.  The 
11785 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 73 61  additional.** sa
11786 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 64 61  nity checking da
11787 74 61 20 69 73 20 61 6e 20 61 74 74 65 6d 70 74  ta is an attempt
11788 20 74 6f 20 64 69 73 63 6f 76 65 72 20 74 68 65   to discover the
11789 20 67 61 72 62 61 67 65 20 69 6e 20 74 68 65 0a   garbage in the.
1178a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 69  ** journal and i
1178b 67 6e 6f 72 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20  gnore it..**.** 
1178c 54 68 65 20 73 61 6e 69 74 79 20 63 68 65 63 6b  The sanity check
1178d 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ing information 
1178e 66 6f 72 20 74 68 65 20 6e 65 77 20 6a 6f 75 72  for the new jour
1178f 6e 61 6c 20 66 6f 72 6d 61 74 20 63 6f 6e 73 69  nal format consi
11790 73 74 73 0a 2a 2a 20 6f 66 20 61 20 33 32 2d 62  sts.** of a 32-b
11791 69 74 20 63 68 65 63 6b 73 75 6d 20 6f 6e 20 65  it checksum on e
11792 61 63 68 20 70 61 67 65 20 6f 66 20 64 61 74 61  ach page of data
11793 2e 20 20 54 68 65 20 63 68 65 63 6b 73 75 6d 20  .  The checksum 
11794 63 6f 76 65 72 73 20 62 6f 74 68 0a 2a 2a 20 74  covers both.** t
11795 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 61  he page number a
11796 6e 64 20 74 68 65 20 70 50 61 67 65 72 2d 3e 70  nd the pPager->p
11797 61 67 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66  ageSize bytes of
11798 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 70 61   data for the pa
11799 67 65 2e 0a 2a 2a 20 54 68 69 73 20 63 6b 73 75  ge..** This cksu
1179a 6d 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64  m is initialized
1179b 20 74 6f 20 61 20 33 32 2d 62 69 74 20 72 61 6e   to a 32-bit ran
1179c 64 6f 6d 20 76 61 6c 75 65 20 74 68 61 74 20 61  dom value that a
1179d 70 70 65 61 72 73 20 69 6e 20 74 68 65 0a 2a 2a  ppears in the.**
1179e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 72 69   journal file ri
1179f 67 68 74 20 61 66 74 65 72 20 74 68 65 20 68 65  ght after the he
117a0 61 64 65 72 2e 20 20 54 68 65 20 72 61 6e 64 6f  ader.  The rando
117a1 6d 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 69 73  m initializer is
117a2 20 69 6d 70 6f 72 74 61 6e 74 2c 0a 2a 2a 20 62   important,.** b
117a3 65 63 61 75 73 65 20 67 61 72 62 61 67 65 20 64  ecause garbage d
117a4 61 74 61 20 74 68 61 74 20 61 70 70 65 61 72 73  ata that appears
117a5 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61   at the end of a
117a6 20 6a 6f 75 72 6e 61 6c 20 69 73 20 6c 69 6b 65   journal is like
117a7 6c 79 0a 2a 2a 20 64 61 74 61 20 74 68 61 74 20  ly.** data that 
117a8 77 61 73 20 6f 6e 63 65 20 69 6e 20 6f 74 68 65  was once in othe
117a9 72 20 66 69 6c 65 73 20 74 68 61 74 20 68 61 76  r files that hav
117aa 65 20 6e 6f 77 20 62 65 65 6e 20 64 65 6c 65 74  e now been delet
117ab 65 64 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 67  ed.  If the.** g
117ac 61 72 62 61 67 65 20 64 61 74 61 20 63 61 6d 65  arbage data came
117ad 20 66 72 6f 6d 20 61 6e 20 6f 62 73 6f 6c 65 74   from an obsolet
117ae 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20  e journal file, 
117af 74 68 65 20 63 68 65 63 6b 73 75 6d 73 20 6d 69  the checksums mi
117b0 67 68 74 0a 2a 2a 20 62 65 20 63 6f 72 72 65 63  ght.** be correc
117b1 74 2e 20 20 42 75 74 20 62 79 20 69 6e 69 74 69  t.  But by initi
117b2 61 6c 69 7a 69 6e 67 20 74 68 65 20 63 68 65 63  alizing the chec
117b3 6b 73 75 6d 20 74 6f 20 72 61 6e 64 6f 6d 20 76  ksum to random v
117b4 61 6c 75 65 20 77 68 69 63 68 0a 2a 2a 20 69 73  alue which.** is
117b5 20 64 69 66 66 65 72 65 6e 74 20 66 6f 72 20 65   different for e
117b6 76 65 72 79 20 6a 6f 75 72 6e 61 6c 2c 20 77 65  very journal, we
117b7 20 6d 69 6e 69 6d 69 7a 65 20 74 68 61 74 20 72   minimize that r
117b8 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  isk..*/.static c
117b9 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
117ba 61 72 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  ar aJournalMagic
117bb 5b 5d 20 3d 20 7b 0a 20 20 30 78 64 39 2c 20 30  [] = {.  0xd9, 0
117bc 78 64 35 2c 20 30 78 30 35 2c 20 30 78 66 39 2c  xd5, 0x05, 0xf9,
117bd 20 30 78 32 30 2c 20 30 78 61 31 2c 20 30 78 36   0x20, 0xa1, 0x6
117be 33 2c 20 30 78 64 37 2c 0a 7d 3b 0a 0a 2f 2a 0a  3, 0xd7,.};../*.
117bf 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74  ** The size of t
117c0 68 65 20 6f 66 20 65 61 63 68 20 70 61 67 65 20  he of each page 
117c1 72 65 63 6f 72 64 20 69 6e 20 74 68 65 20 6a 6f  record in the jo
117c2 75 72 6e 61 6c 20 69 73 20 67 69 76 65 6e 20 62  urnal is given b
117c3 79 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69  y.** the followi
117c4 6e 67 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 64 65  ng macro..*/.#de
117c5 66 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f  fine JOURNAL_PG_
117c6 53 5a 28 70 50 61 67 65 72 29 20 20 28 28 70 50  SZ(pPager)  ((pP
117c7 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 20  ager->pageSize) 
117c8 2b 20 38 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  + 8)../*.** The 
117c9 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73  journal header s
117ca 69 7a 65 20 66 6f 72 20 74 68 69 73 20 70 61 67  ize for this pag
117cb 65 72 2e 20 54 68 69 73 20 69 73 20 75 73 75 61  er. This is usua
117cc 6c 6c 79 20 74 68 65 20 73 61 6d 65 20 0a 2a 2a  lly the same .**
117cd 20 73 69 7a 65 20 61 73 20 61 20 73 69 6e 67 6c   size as a singl
117ce 65 20 64 69 73 6b 20 73 65 63 74 6f 72 2e 20 53  e disk sector. S
117cf 65 65 20 61 6c 73 6f 20 73 65 74 53 65 63 74 6f  ee also setSecto
117d0 72 53 69 7a 65 28 29 2e 0a 2a 2f 0a 23 64 65 66  rSize()..*/.#def
117d1 69 6e 65 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  ine JOURNAL_HDR_
117d2 53 5a 28 70 50 61 67 65 72 29 20 28 70 50 61 67  SZ(pPager) (pPag
117d3 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 0a  er->sectorSize).
117d4 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f  ./*.** The macro
117d5 20 4d 45 4d 44 42 20 69 73 20 74 72 75 65 20 69   MEMDB is true i
117d6 66 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67  f we are dealing
117d7 20 77 69 74 68 20 61 6e 20 69 6e 2d 6d 65 6d 6f   with an in-memo
117d8 72 79 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20  ry database..** 
117d9 57 65 20 64 6f 20 74 68 69 73 20 61 73 20 61 20  We do this as a 
117da 6d 61 63 72 6f 20 73 6f 20 74 68 61 74 20 69 66  macro so that if
117db 20 74 68 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54   the SQLITE_OMIT
117dc 5f 4d 45 4d 4f 52 59 44 42 20 6d 61 63 72 6f 20  _MEMORYDB macro 
117dd 69 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 20 76  is set,.** the v
117de 61 6c 75 65 20 6f 66 20 4d 45 4d 44 42 20 77 69  alue of MEMDB wi
117df 6c 6c 20 62 65 20 61 20 63 6f 6e 73 74 61 6e 74  ll be a constant
117e0 20 61 6e 64 20 74 68 65 20 63 6f 6d 70 69 6c 65   and the compile
117e1 72 20 77 69 6c 6c 20 6f 70 74 69 6d 69 7a 65 0a  r will optimize.
117e2 2a 2a 20 6f 75 74 20 63 6f 64 65 20 74 68 61 74  ** out code that
117e3 20 77 6f 75 6c 64 20 6e 65 76 65 72 20 65 78 65   would never exe
117e4 63 75 74 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  cute..*/.#ifdef 
117e5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f  SQLITE_OMIT_MEMO
117e6 52 59 44 42 0a 23 20 64 65 66 69 6e 65 20 4d 45  RYDB.# define ME
117e7 4d 44 42 20 30 0a 23 65 6c 73 65 0a 23 20 64 65  MDB 0.#else.# de
117e8 66 69 6e 65 20 4d 45 4d 44 42 20 70 50 61 67 65  fine MEMDB pPage
117e9 72 2d 3e 6d 65 6d 44 62 0a 23 65 6e 64 69 66 0a  r->memDb.#endif.
117ea 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d  ./*.** The maxim
117eb 75 6d 20 6c 65 67 61 6c 20 70 61 67 65 20 6e 75  um legal page nu
117ec 6d 62 65 72 20 69 73 20 28 32 5e 33 31 20 2d 20  mber is (2^31 - 
117ed 31 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50  1)..*/.#define P
117ee 41 47 45 52 5f 4d 41 58 5f 50 47 4e 4f 20 32 31  AGER_MAX_PGNO 21
117ef 34 37 34 38 33 36 34 37 0a 0a 23 69 66 6e 64 65  47483647..#ifnde
117f0 66 20 4e 44 45 42 55 47 20 0a 2f 2a 0a 2a 2a 20  f NDEBUG ./*.** 
117f1 55 73 61 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 61  Usage:.**.**   a
117f2 73 73 65 72 74 28 20 61 73 73 65 72 74 5f 70 61  ssert( assert_pa
117f3 67 65 72 5f 73 74 61 74 65 28 70 50 61 67 65 72  ger_state(pPager
117f4 29 20 29 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ) );.*/.static i
117f5 6e 74 20 61 73 73 65 72 74 5f 70 61 67 65 72 5f  nt assert_pager_
117f6 73 74 61 74 65 28 50 61 67 65 72 20 2a 70 50 61  state(Pager *pPa
117f7 67 65 72 29 7b 0a 0a 20 20 2f 2a 20 41 20 74 65  ger){..  /* A te
117f8 6d 70 2d 66 69 6c 65 20 69 73 20 61 6c 77 61 79  mp-file is alway
117f9 73 20 69 6e 20 50 41 47 45 52 5f 45 58 43 4c 55  s in PAGER_EXCLU
117fa 53 49 56 45 20 6f 72 20 50 41 47 45 52 5f 53 59  SIVE or PAGER_SY
117fb 4e 43 45 44 20 73 74 61 74 65 2e 20 2a 2f 0a 20  NCED state. */. 
117fc 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
117fd 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20 7c 7c 20  >tempFile==0 || 
117fe 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50  pPager->state>=P
117ff 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20 29  AGER_EXCLUSIVE )
11800 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 63 68 61 6e  ;..  /* The chan
11801 67 65 43 6f 75 6e 74 44 6f 6e 65 20 66 6c 61 67  geCountDone flag
11802 20 69 73 20 61 6c 77 61 79 73 20 73 65 74 20 66   is always set f
11803 6f 72 20 74 65 6d 70 2d 66 69 6c 65 73 20 2a 2f  or temp-files */
11804 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
11805 72 2d 3e 74 65 6d 70 46 69 6c 65 3d 3d 30 20 7c  r->tempFile==0 |
11806 7c 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65  | pPager->change
11807 43 6f 75 6e 74 44 6f 6e 65 20 29 3b 0a 0a 20 20  CountDone );..  
11808 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64  return 1;.}.#end
11809 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  if../*.** Return
1180a 20 74 72 75 65 20 69 66 20 69 74 20 69 73 20 6e   true if it is n
1180b 65 63 65 73 73 61 72 79 20 74 6f 20 77 72 69 74  ecessary to writ
1180c 65 20 70 61 67 65 20 2a 70 50 67 20 69 6e 74 6f  e page *pPg into
1180d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c   the sub-journal
1180e 2e 0a 2a 2a 20 41 20 70 61 67 65 20 6e 65 65 64  ..** A page need
1180f 73 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e 20  s to be written 
11810 69 6e 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f 75  into the sub-jou
11811 72 6e 61 6c 20 69 66 20 74 68 65 72 65 20 65 78  rnal if there ex
11812 69 73 74 73 20 6f 6e 65 0a 2a 2a 20 6f 72 20 6d  ists one.** or m
11813 6f 72 65 20 6f 70 65 6e 20 73 61 76 65 70 6f 69  ore open savepoi
11814 6e 74 73 20 66 6f 72 20 77 68 69 63 68 3a 0a 2a  nts for which:.*
11815 2a 0a 2a 2a 20 20 20 2a 20 54 68 65 20 70 61 67  *.**   * The pag
11816 65 2d 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73  e-number is less
11817 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
11818 6f 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74  o PagerSavepoint
11819 2e 6e 4f 72 69 67 2c 20 61 6e 64 0a 2a 2a 20 20  .nOrig, and.**  
1181a 20 2a 20 54 68 65 20 62 69 74 20 63 6f 72 72 65   * The bit corre
1181b 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20  sponding to the 
1181c 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 20 6e  page-number is n
1181d 6f 74 20 73 65 74 20 69 6e 0a 2a 2a 20 20 20 20  ot set in.**    
1181e 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e   PagerSavepoint.
1181f 70 49 6e 53 61 76 65 70 6f 69 6e 74 2e 0a 2a 2f  pInSavepoint..*/
11820 0a 73 74 61 74 69 63 20 69 6e 74 20 73 75 62 6a  .static int subj
11821 52 65 71 75 69 72 65 73 50 61 67 65 28 50 67 48  RequiresPage(PgH
11822 64 72 20 2a 70 50 67 29 7b 0a 20 20 50 67 6e 6f  dr *pPg){.  Pgno
11823 20 70 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e   pgno = pPg->pgn
11824 6f 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  o;.  Pager *pPag
11825 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72  er = pPg->pPager
11826 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72  ;.  int i;.  for
11827 28 69 3d 30 3b 20 69 3c 70 50 61 67 65 72 2d 3e  (i=0; i<pPager->
11828 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 2b 2b 29  nSavepoint; i++)
11829 7b 0a 20 20 20 20 50 61 67 65 72 53 61 76 65 70  {.    PagerSavep
1182a 6f 69 6e 74 20 2a 70 20 3d 20 26 70 50 61 67 65  oint *p = &pPage
1182b 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69 5d  r->aSavepoint[i]
1182c 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 4f 72  ;.    if( p->nOr
1182d 69 67 3e 3d 70 67 6e 6f 20 26 26 20 30 3d 3d 73  ig>=pgno && 0==s
1182e 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74  qlite3BitvecTest
1182f 28 70 2d 3e 70 49 6e 53 61 76 65 70 6f 69 6e 74  (p->pInSavepoint
11830 2c 20 70 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20  , pgno) ){.     
11831 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
11832 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
11833 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
11834 20 74 72 75 65 20 69 66 20 74 68 65 20 70 61 67   true if the pag
11835 65 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20  e is already in 
11836 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
11837 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
11838 70 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 50 67  pageInJournal(Pg
11839 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 72 65 74  Hdr *pPg){.  ret
1183a 75 72 6e 20 73 71 6c 69 74 65 33 42 69 74 76 65  urn sqlite3Bitve
1183b 63 54 65 73 74 28 70 50 67 2d 3e 70 50 61 67 65  cTest(pPg->pPage
1183c 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70  r->pInJournal, p
1183d 50 67 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a 2f 2a  Pg->pgno);.}../*
1183e 0a 2a 2a 20 52 65 61 64 20 61 20 33 32 2d 62 69  .** Read a 32-bi
1183f 74 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 74  t integer from t
11840 68 65 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65  he given file de
11841 73 63 72 69 70 74 6f 72 2e 20 20 53 74 6f 72 65  scriptor.  Store
11842 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20   the integer.** 
11843 74 68 61 74 20 69 73 20 72 65 61 64 20 69 6e 20  that is read in 
11844 2a 70 52 65 73 2e 20 20 52 65 74 75 72 6e 20 53  *pRes.  Return S
11845 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72  QLITE_OK if ever
11846 79 74 68 69 6e 67 20 77 6f 72 6b 65 64 2c 20 6f  ything worked, o
11847 72 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 63 6f  r an.** error co
11848 64 65 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20  de is something 
11849 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a  goes wrong..**.*
1184a 2a 20 41 6c 6c 20 76 61 6c 75 65 73 20 61 72 65  * All values are
1184b 20 73 74 6f 72 65 64 20 6f 6e 20 64 69 73 6b 20   stored on disk 
1184c 61 73 20 62 69 67 2d 65 6e 64 69 61 6e 2e 0a 2a  as big-endian..*
1184d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61  /.static int rea
1184e 64 33 32 62 69 74 73 28 73 71 6c 69 74 65 33 5f  d32bits(sqlite3_
1184f 66 69 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f 66  file *fd, i64 of
11850 66 73 65 74 2c 20 75 33 32 20 2a 70 52 65 73 29  fset, u32 *pRes)
11851 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  {.  unsigned cha
11852 72 20 61 63 5b 34 5d 3b 0a 20 20 69 6e 74 20 72  r ac[4];.  int r
11853 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
11854 64 28 66 64 2c 20 61 63 2c 20 73 69 7a 65 6f 66  d(fd, ac, sizeof
11855 28 61 63 29 2c 20 6f 66 66 73 65 74 29 3b 0a 20  (ac), offset);. 
11856 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
11857 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20  OK ){.    *pRes 
11858 3d 20 73 71 6c 69 74 65 33 47 65 74 34 62 79 74  = sqlite3Get4byt
11859 65 28 61 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74  e(ac);.  }.  ret
1185a 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1185b 20 57 72 69 74 65 20 61 20 33 32 2d 62 69 74 20   Write a 32-bit 
1185c 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 61 20 73  integer into a s
1185d 74 72 69 6e 67 20 62 75 66 66 65 72 20 69 6e 20  tring buffer in 
1185e 62 69 67 2d 65 6e 64 69 61 6e 20 62 79 74 65 20  big-endian byte 
1185f 6f 72 64 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e  order..*/.#defin
11860 65 20 70 75 74 33 32 62 69 74 73 28 41 2c 42 29  e put32bits(A,B)
11861 20 20 73 71 6c 69 74 65 33 50 75 74 34 62 79 74    sqlite3Put4byt
11862 65 28 28 75 38 2a 29 41 2c 42 29 0a 0a 2f 2a 0a  e((u8*)A,B)../*.
11863 2a 2a 20 57 72 69 74 65 20 61 20 33 32 2d 62 69  ** Write a 32-bi
11864 74 20 69 6e 74 65 67 65 72 20 69 6e 74 6f 20 74  t integer into t
11865 68 65 20 67 69 76 65 6e 20 66 69 6c 65 20 64 65  he given file de
11866 73 63 72 69 70 74 6f 72 2e 20 20 52 65 74 75 72  scriptor.  Retur
11867 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f  n SQLITE_OK.** o
11868 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20  n success or an 
11869 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73 6f  error code is so
1186a 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f  mething goes wro
1186b 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
1186c 74 20 77 72 69 74 65 33 32 62 69 74 73 28 73 71  t write32bits(sq
1186d 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20  lite3_file *fd, 
1186e 69 36 34 20 6f 66 66 73 65 74 2c 20 75 33 32 20  i64 offset, u32 
1186f 76 61 6c 29 7b 0a 20 20 63 68 61 72 20 61 63 5b  val){.  char ac[
11870 34 5d 3b 0a 20 20 70 75 74 33 32 62 69 74 73 28  4];.  put32bits(
11871 61 63 2c 20 76 61 6c 29 3b 0a 20 20 72 65 74 75  ac, val);.  retu
11872 72 6e 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74  rn sqlite3OsWrit
11873 65 28 66 64 2c 20 61 63 2c 20 34 2c 20 6f 66 66  e(fd, ac, 4, off
11874 73 65 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  set);.}../*.** T
11875 68 65 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  he argument to t
11876 68 69 73 20 6d 61 63 72 6f 20 69 73 20 61 20 66  his macro is a f
11877 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 28  ile descriptor (
11878 74 79 70 65 20 73 71 6c 69 74 65 33 5f 66 69 6c  type sqlite3_fil
11879 65 2a 29 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30  e*)..** Return 0
1187a 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 6f 70   if it is not op
1187b 65 6e 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 20  en, or non-zero 
1187c 28 62 75 74 20 6e 6f 74 20 31 29 20 69 66 20 69  (but not 1) if i
1187d 74 20 69 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  t is..**.** This
1187e 20 69 73 20 73 6f 20 74 68 61 74 20 65 78 70 72   is so that expr
1187f 65 73 73 69 6f 6e 73 20 63 61 6e 20 62 65 20 77  essions can be w
11880 72 69 74 74 65 6e 20 61 73 3a 0a 2a 2a 0a 2a 2a  ritten as:.**.**
11881 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50     if( isOpen(pP
11882 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 20 2e 2e  ager->jfd) ){ ..
11883 2e 0a 2a 2a 0a 2a 2a 20 69 6e 73 74 65 61 64 20  ..**.** instead 
11884 6f 66 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 70  of.**.**   if( p
11885 50 61 67 65 72 2d 3e 6a 66 64 2d 3e 70 4d 65 74  Pager->jfd->pMet
11886 68 6f 64 73 20 29 7b 20 2e 2e 2e 0a 2a 2f 0a 23  hods ){ ....*/.#
11887 64 65 66 69 6e 65 20 69 73 4f 70 65 6e 28 70 46  define isOpen(pF
11888 64 29 20 28 28 70 46 64 29 2d 3e 70 4d 65 74 68  d) ((pFd)->pMeth
11889 6f 64 73 29 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 66  ods)../*.** If f
1188a 69 6c 65 20 70 46 64 20 69 73 20 6f 70 65 6e 2c  ile pFd is open,
1188b 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 4f 73 55   call sqlite3OsU
1188c 6e 6c 6f 63 6b 28 29 20 6f 6e 20 69 74 2e 0a 2a  nlock() on it..*
1188d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 55  /.static int osU
1188e 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  nlock(sqlite3_fi
1188f 6c 65 20 2a 70 46 64 2c 20 69 6e 74 20 65 4c 6f  le *pFd, int eLo
11890 63 6b 29 7b 0a 20 20 69 66 28 20 21 69 73 4f 70  ck){.  if( !isOp
11891 65 6e 28 70 46 64 29 20 29 7b 0a 20 20 20 20 72  en(pFd) ){.    r
11892 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
11893 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71  .  }.  return sq
11894 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 70 46  lite3OsUnlock(pF
11895 64 2c 20 65 4c 6f 63 6b 29 3b 0a 7d 0a 0a 2f 2a  d, eLock);.}../*
11896 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
11897 6e 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65  n determines whe
11898 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
11899 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74  atomic-write opt
1189a 69 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6e  imization.** can
1189b 20 62 65 20 75 73 65 64 20 77 69 74 68 20 74 68   be used with th
1189c 69 73 20 70 61 67 65 72 2e 20 54 68 65 20 6f 70  is pager. The op
1189d 74 69 6d 69 7a 61 74 69 6f 6e 20 63 61 6e 20 62  timization can b
1189e 65 20 75 73 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a  e used if:.**.**
1189f 20 20 28 61 29 20 74 68 65 20 76 61 6c 75 65 20    (a) the value 
118a0 72 65 74 75 72 6e 65 64 20 62 79 20 4f 73 44 65  returned by OsDe
118a1 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
118a2 69 63 73 28 29 20 69 6e 64 69 63 61 74 65 73 20  ics() indicates 
118a3 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 61 20 64  that.**      a d
118a4 61 74 61 62 61 73 65 20 70 61 67 65 20 6d 61 79  atabase page may
118a5 20 62 65 20 77 72 69 74 74 65 6e 20 61 74 6f 6d   be written atom
118a6 69 63 61 6c 6c 79 2c 20 61 6e 64 0a 2a 2a 20 20  ically, and.**  
118a7 28 62 29 20 74 68 65 20 76 61 6c 75 65 20 72 65  (b) the value re
118a8 74 75 72 6e 65 64 20 62 79 20 4f 73 53 65 63 74  turned by OsSect
118a9 6f 72 53 69 7a 65 28 29 20 69 73 20 6c 65 73 73  orSize() is less
118aa 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 0a 2a   than or equal.*
118ab 2a 20 20 20 20 20 20 74 6f 20 74 68 65 20 70 61  *      to the pa
118ac 67 65 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 54  ge size..**.** T
118ad 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  he optimization 
118ae 69 73 20 61 6c 73 6f 20 61 6c 77 61 79 73 20 65  is also always e
118af 6e 61 62 6c 65 64 20 66 6f 72 20 74 65 6d 70 6f  nabled for tempo
118b0 72 61 72 79 20 66 69 6c 65 73 2e 20 49 74 20 69  rary files. It i
118b1 73 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 74 6f  s.** an error to
118b2 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74   call this funct
118b3 69 6f 6e 20 69 66 20 70 50 61 67 65 72 20 69 73  ion if pPager is
118b4 20 6f 70 65 6e 65 64 20 6f 6e 20 61 6e 20 69 6e   opened on an in
118b5 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62  -memory.** datab
118b6 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ase..**.** If th
118b7 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 63  e optimization c
118b8 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 2c 20 30  annot be used, 0
118b9 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66   is returned. If
118ba 20 69 74 20 63 61 6e 20 62 65 20 75 73 65 64 2c   it can be used,
118bb 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 76 61 6c  .** then the val
118bc 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 74  ue returned is t
118bd 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6a  he size of the j
118be 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 68 65 6e  ournal file when
118bf 20 69 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20   it.** contains 
118c0 72 6f 6c 6c 62 61 63 6b 20 64 61 74 61 20 66 6f  rollback data fo
118c1 72 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 70 61  r exactly one pa
118c2 67 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ge..*/.#ifdef SQ
118c3 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d  LITE_ENABLE_ATOM
118c4 49 43 5f 57 52 49 54 45 0a 73 74 61 74 69 63 20  IC_WRITE.static 
118c5 69 6e 74 20 6a 72 6e 6c 42 75 66 66 65 72 53 69  int jrnlBufferSi
118c6 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ze(Pager *pPager
118c7 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 4d 45  ){.  assert( !ME
118c8 4d 44 42 20 29 3b 0a 20 20 69 66 28 20 21 70 50  MDB );.  if( !pP
118c9 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29  ager->tempFile )
118ca 7b 0a 20 20 20 20 69 6e 74 20 64 63 3b 20 20 20  {.    int dc;   
118cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
118cc 20 20 20 20 20 20 20 20 2f 2a 20 44 65 76 69 63          /* Devic
118cd 65 20 63 68 61 72 61 63 74 65 72 69 73 74 69 63  e characteristic
118ce 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53 65  s */.    int nSe
118cf 63 74 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20  ctor;           
118d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 65             /* Se
118d1 63 74 6f 72 20 73 69 7a 65 20 2a 2f 0a 20 20 20  ctor size */.   
118d2 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20   int szPage;    
118d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
118d4 20 20 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20     /* Page size 
118d5 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20  */..    assert( 
118d6 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66  isOpen(pPager->f
118d7 64 29 20 29 3b 0a 20 20 20 20 64 63 20 3d 20 73  d) );.    dc = s
118d8 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68  qlite3OsDeviceCh
118d9 61 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50  aracteristics(pP
118da 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 6e  ager->fd);.    n
118db 53 65 63 74 6f 72 20 3d 20 70 50 61 67 65 72 2d  Sector = pPager-
118dc 3e 73 65 63 74 6f 72 53 69 7a 65 3b 0a 20 20 20  >sectorSize;.   
118dd 20 73 7a 50 61 67 65 20 3d 20 70 50 61 67 65 72   szPage = pPager
118de 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 0a 20 20 20  ->pageSize;..   
118df 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49   assert(SQLITE_I
118e0 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d 3d  OCAP_ATOMIC512==
118e1 28 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20 61  (512>>8));.    a
118e2 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49 4f 43  ssert(SQLITE_IOC
118e3 41 50 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d 28 36  AP_ATOMIC64K==(6
118e4 35 35 33 36 3e 3e 38 29 29 3b 0a 20 20 20 20 69  5536>>8));.    i
118e5 66 28 20 30 3d 3d 28 64 63 26 28 53 51 4c 49 54  f( 0==(dc&(SQLIT
118e6 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 7c 28  E_IOCAP_ATOMIC|(
118e7 73 7a 50 61 67 65 3e 3e 38 29 29 20 7c 7c 20 6e  szPage>>8)) || n
118e8 53 65 63 74 6f 72 3e 73 7a 50 61 67 65 29 20 29  Sector>szPage) )
118e9 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
118ea 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  ;.    }.  }..  r
118eb 65 74 75 72 6e 20 4a 4f 55 52 4e 41 4c 5f 48 44  eturn JOURNAL_HD
118ec 52 5f 53 5a 28 70 50 61 67 65 72 29 20 2b 20 4a  R_SZ(pPager) + J
118ed 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61  OURNAL_PG_SZ(pPa
118ee 67 65 72 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  ger);.}.#endif..
118ef 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f  /*.** If SQLITE_
118f0 43 48 45 43 4b 5f 50 41 47 45 53 20 69 73 20 64  CHECK_PAGES is d
118f1 65 66 69 6e 65 64 20 74 68 65 6e 20 77 65 20 64  efined then we d
118f2 6f 20 73 6f 6d 65 20 73 61 6e 69 74 79 20 63 68  o some sanity ch
118f3 65 63 6b 69 6e 67 0a 2a 2a 20 6f 6e 20 74 68 65  ecking.** on the
118f4 20 63 61 63 68 65 20 75 73 69 6e 67 20 61 20 68   cache using a h
118f5 61 73 68 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54  ash function.  T
118f6 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72 20  his is used for 
118f7 74 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20 64  testing.** and d
118f8 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a  ebugging only..*
118f9 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
118fa 43 48 45 43 4b 5f 50 41 47 45 53 0a 2f 2a 0a 2a  CHECK_PAGES./*.*
118fb 2a 20 52 65 74 75 72 6e 20 61 20 33 32 2d 62 69  * Return a 32-bi
118fc 74 20 68 61 73 68 20 6f 66 20 74 68 65 20 70 61  t hash of the pa
118fd 67 65 20 64 61 74 61 20 66 6f 72 20 70 50 61 67  ge data for pPag
118fe 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 32  e..*/.static u32
118ff 20 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28   pager_datahash(
11900 69 6e 74 20 6e 42 79 74 65 2c 20 75 6e 73 69 67  int nByte, unsig
11901 6e 65 64 20 63 68 61 72 20 2a 70 44 61 74 61 29  ned char *pData)
11902 7b 0a 20 20 75 33 32 20 68 61 73 68 20 3d 20 30  {.  u32 hash = 0
11903 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72  ;.  int i;.  for
11904 28 69 3d 30 3b 20 69 3c 6e 42 79 74 65 3b 20 69  (i=0; i<nByte; i
11905 2b 2b 29 7b 0a 20 20 20 20 68 61 73 68 20 3d 20  ++){.    hash = 
11906 28 68 61 73 68 2a 31 30 33 39 29 20 2b 20 70 44  (hash*1039) + pD
11907 61 74 61 5b 69 5d 3b 0a 20 20 7d 0a 20 20 72 65  ata[i];.  }.  re
11908 74 75 72 6e 20 68 61 73 68 3b 0a 7d 0a 73 74 61  turn hash;.}.sta
11909 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 70 61  tic u32 pager_pa
1190a 67 65 68 61 73 68 28 50 67 48 64 72 20 2a 70 50  gehash(PgHdr *pP
1190b 61 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  age){.  return p
1190c 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 70 50  ager_datahash(pP
1190d 61 67 65 2d 3e 70 50 61 67 65 72 2d 3e 70 61 67  age->pPager->pag
1190e 65 53 69 7a 65 2c 20 28 75 6e 73 69 67 6e 65 64  eSize, (unsigned
1190f 20 63 68 61 72 20 2a 29 70 50 61 67 65 2d 3e 70   char *)pPage->p
11910 44 61 74 61 29 3b 0a 7d 0a 73 74 61 74 69 63 20  Data);.}.static 
11911 76 6f 69 64 20 70 61 67 65 72 5f 73 65 74 5f 70  void pager_set_p
11912 61 67 65 68 61 73 68 28 50 67 48 64 72 20 2a 70  agehash(PgHdr *p
11913 50 61 67 65 29 7b 0a 20 20 70 50 61 67 65 2d 3e  Page){.  pPage->
11914 70 61 67 65 48 61 73 68 20 3d 20 70 61 67 65 72  pageHash = pager
11915 5f 70 61 67 65 68 61 73 68 28 70 50 61 67 65 29  _pagehash(pPage)
11916 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43  ;.}../*.** The C
11917 48 45 43 4b 5f 50 41 47 45 20 6d 61 63 72 6f 20  HECK_PAGE macro 
11918 74 61 6b 65 73 20 61 20 50 67 48 64 72 2a 20 61  takes a PgHdr* a
11919 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 2e 20 49  s an argument. I
1191a 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50  f SQLITE_CHECK_P
1191b 41 47 45 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e  AGES.** is defin
1191c 65 64 2c 20 61 6e 64 20 4e 44 45 42 55 47 20 69  ed, and NDEBUG i
1191d 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 2c 20 61  s not defined, a
1191e 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65  n assert() state
1191f 6d 65 6e 74 20 63 68 65 63 6b 73 0a 2a 2a 20 74  ment checks.** t
11920 68 61 74 20 74 68 65 20 70 61 67 65 20 69 73 20  hat the page is 
11921 65 69 74 68 65 72 20 64 69 72 74 79 20 6f 72 20  either dirty or 
11922 73 74 69 6c 6c 20 6d 61 74 63 68 65 73 20 74 68  still matches th
11923 65 20 63 61 6c 63 75 6c 61 74 65 64 20 70 61 67  e calculated pag
11924 65 2d 68 61 73 68 2e 0a 2a 2f 0a 23 64 65 66 69  e-hash..*/.#defi
11925 6e 65 20 43 48 45 43 4b 5f 50 41 47 45 28 78 29  ne CHECK_PAGE(x)
11926 20 63 68 65 63 6b 50 61 67 65 28 78 29 0a 73 74   checkPage(x).st
11927 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 50  atic void checkP
11928 61 67 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b  age(PgHdr *pPg){
11929 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
1192a 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a   = pPg->pPager;.
1192b 20 20 61 73 73 65 72 74 28 20 21 70 50 67 2d 3e    assert( !pPg->
1192c 70 61 67 65 48 61 73 68 20 7c 7c 20 70 50 61 67  pageHash || pPag
1192d 65 72 2d 3e 65 72 72 43 6f 64 65 0a 20 20 20 20  er->errCode.    
1192e 20 20 7c 7c 20 28 70 50 67 2d 3e 66 6c 61 67 73    || (pPg->flags
1192f 26 50 47 48 44 52 5f 44 49 52 54 59 29 20 7c 7c  &PGHDR_DIRTY) ||
11930 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 3d 3d   pPg->pageHash==
11931 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70  pager_pagehash(p
11932 50 67 29 20 29 3b 0a 7d 0a 0a 23 65 6c 73 65 0a  Pg) );.}..#else.
11933 23 64 65 66 69 6e 65 20 70 61 67 65 72 5f 64 61  #define pager_da
11934 74 61 68 61 73 68 28 58 2c 59 29 20 20 30 0a 23  tahash(X,Y)  0.#
11935 64 65 66 69 6e 65 20 70 61 67 65 72 5f 70 61 67  define pager_pag
11936 65 68 61 73 68 28 58 29 20 20 30 0a 23 64 65 66  ehash(X)  0.#def
11937 69 6e 65 20 43 48 45 43 4b 5f 50 41 47 45 28 78  ine CHECK_PAGE(x
11938 29 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c  ).#endif  /* SQL
11939 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 20  ITE_CHECK_PAGES 
1193a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74  */../*.** When t
1193b 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 74 68  his is called th
1193c 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66  e journal file f
1193d 6f 72 20 70 61 67 65 72 20 70 50 61 67 65 72 20  or pager pPager 
1193e 6d 75 73 74 20 62 65 20 6f 70 65 6e 2e 0a 2a 2a  must be open..**
1193f 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61   This function a
11940 74 74 65 6d 70 74 73 20 74 6f 20 72 65 61 64 20  ttempts to read 
11941 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  a master journal
11942 20 66 69 6c 65 20 6e 61 6d 65 20 66 72 6f 6d 20   file name from 
11943 74 68 65 20 0a 2a 2a 20 65 6e 64 20 6f 66 20 74  the .** end of t
11944 68 65 20 66 69 6c 65 20 61 6e 64 2c 20 69 66 20  he file and, if 
11945 73 75 63 63 65 73 73 66 75 6c 2c 20 63 6f 70 69  successful, copi
11946 65 73 20 69 74 20 69 6e 74 6f 20 6d 65 6d 6f 72  es it into memor
11947 79 20 73 75 70 70 6c 69 65 64 20 0a 2a 2a 20 62  y supplied .** b
11948 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 53 65  y the caller. Se
11949 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65  e comments above
1194a 20 77 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72   writeMasterJour
1194b 6e 61 6c 28 29 20 66 6f 72 20 74 68 65 20 66 6f  nal() for the fo
1194c 72 6d 61 74 0a 2a 2a 20 75 73 65 64 20 74 6f 20  rmat.** used to 
1194d 73 74 6f 72 65 20 61 20 6d 61 73 74 65 72 20 6a  store a master j
1194e 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65  ournal file name
1194f 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 61   at the end of a
11950 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a   journal file..*
11951 2a 0a 2a 2a 20 7a 4d 61 73 74 65 72 20 6d 75 73  *.** zMaster mus
11952 74 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66  t point to a buf
11953 66 65 72 20 6f 66 20 61 74 20 6c 65 61 73 74 20  fer of at least 
11954 6e 4d 61 73 74 65 72 20 62 79 74 65 73 20 61 6c  nMaster bytes al
11955 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a 20 74 68  located by.** th
11956 65 20 63 61 6c 6c 65 72 2e 20 54 68 69 73 20 73  e caller. This s
11957 68 6f 75 6c 64 20 62 65 20 73 71 6c 69 74 65 33  hould be sqlite3
11958 5f 76 66 73 2e 6d 78 50 61 74 68 6e 61 6d 65 2b  _vfs.mxPathname+
11959 31 20 28 74 6f 20 65 6e 73 75 72 65 20 74 68 65  1 (to ensure the
1195a 72 65 20 69 73 0a 2a 2a 20 65 6e 6f 75 67 68 20  re is.** enough 
1195b 73 70 61 63 65 20 74 6f 20 77 72 69 74 65 20 74  space to write t
1195c 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
1195d 6c 20 6e 61 6d 65 29 2e 20 49 66 20 74 68 65 20  l name). If the 
1195e 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a  master journal.*
1195f 2a 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 6a 6f  * name in the jo
11960 75 72 6e 61 6c 20 69 73 20 6c 6f 6e 67 65 72 20  urnal is longer 
11961 74 68 61 6e 20 6e 4d 61 73 74 65 72 20 62 79 74  than nMaster byt
11962 65 73 20 28 69 6e 63 6c 75 64 69 6e 67 20 61 0a  es (including a.
11963 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f  ** nul-terminato
11964 72 29 2c 20 74 68 65 6e 20 74 68 69 73 20 69 73  r), then this is
11965 20 68 61 6e 64 6c 65 64 20 61 73 20 69 66 20 6e   handled as if n
11966 6f 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  o master journal
11967 20 6e 61 6d 65 0a 2a 2a 20 77 65 72 65 20 70 72   name.** were pr
11968 65 73 65 6e 74 20 69 6e 20 74 68 65 20 6a 6f 75  esent in the jou
11969 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  rnal..**.** If a
1196a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1196b 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65  file name is pre
1196c 73 65 6e 74 20 61 74 20 74 68 65 20 65 6e 64 20  sent at the end 
1196d 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a  of the journal.*
1196e 2a 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20  * file, then it 
1196f 69 73 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74  is copied into t
11970 68 65 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65  he buffer pointe
11971 64 20 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 2e  d to by zMaster.
11972 20 41 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e   A.** nul-termin
11973 61 74 6f 72 20 62 79 74 65 20 69 73 20 61 70 70  ator byte is app
11974 65 6e 64 65 64 20 74 6f 20 74 68 65 20 62 75 66  ended to the buf
11975 66 65 72 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  fer following th
11976 65 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72  e master.** jour
11977 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 2e 0a 2a  nal file name..*
11978 2a 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 64 65  *.** If it is de
11979 74 65 72 6d 69 6e 65 64 20 74 68 61 74 20 6e 6f  termined that no
1197a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1197b 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65  file name is pre
1197c 73 65 6e 74 20 0a 2a 2a 20 7a 4d 61 73 74 65 72  sent .** zMaster
1197d 5b 30 5d 20 69 73 20 73 65 74 20 74 6f 20 30 20  [0] is set to 0 
1197e 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65  and SQLITE_OK re
1197f 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  turned..**.** If
11980 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
11981 20 77 68 69 6c 65 20 72 65 61 64 69 6e 67 20 66   while reading f
11982 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  rom the journal 
11983 66 69 6c 65 2c 20 61 6e 20 53 51 4c 69 74 65 0a  file, an SQLite.
11984 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  ** error code is
11985 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74   returned..*/.st
11986 61 74 69 63 20 69 6e 74 20 72 65 61 64 4d 61 73  atic int readMas
11987 74 65 72 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74  terJournal(sqlit
11988 65 33 5f 66 69 6c 65 20 2a 70 4a 72 6e 6c 2c 20  e3_file *pJrnl, 
11989 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c 20 75  char *zMaster, u
1198a 33 32 20 6e 4d 61 73 74 65 72 29 7b 0a 20 20 69  32 nMaster){.  i
1198b 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
1198c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
1198d 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 75 33  urn code */.  u3
1198e 32 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20  2 len;          
1198f 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67           /* Leng
11990 74 68 20 69 6e 20 62 79 74 65 73 20 6f 66 20 6d  th in bytes of m
11991 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61  aster journal na
11992 6d 65 20 2a 2f 0a 20 20 69 36 34 20 73 7a 4a 3b  me */.  i64 szJ;
11993 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11994 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65     /* Total size
11995 20 69 6e 20 62 79 74 65 73 20 6f 66 20 6a 6f 75   in bytes of jou
11996 72 6e 61 6c 20 66 69 6c 65 20 70 4a 72 6e 6c 20  rnal file pJrnl 
11997 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b 20  */.  u32 cksum; 
11998 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11999 2f 2a 20 4d 4a 20 63 68 65 63 6b 73 75 6d 20 76  /* MJ checksum v
1199a 61 6c 75 65 20 72 65 61 64 20 66 72 6f 6d 20 6a  alue read from j
1199b 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20  ournal */.  u32 
1199c 75 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  u;              
1199d 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e         /* Unsign
1199e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  ed loop counter 
1199f 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
119a0 61 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20 20 20  ar aMagic[8];   
119a1 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f 20 68  /* A buffer to h
119a2 6f 6c 64 20 74 68 65 20 6d 61 67 69 63 20 68 65  old the magic he
119a3 61 64 65 72 20 2a 2f 0a 20 20 7a 4d 61 73 74 65  ader */.  zMaste
119a4 72 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20  r[0] = '\0';..  
119a5 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28  if( SQLITE_OK!=(
119a6 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69  rc = sqlite3OsFi
119a7 6c 65 53 69 7a 65 28 70 4a 72 6e 6c 2c 20 26 73  leSize(pJrnl, &s
119a8 7a 4a 29 29 0a 20 20 20 7c 7c 20 73 7a 4a 3c 31  zJ)).   || szJ<1
119a9 36 0a 20 20 20 7c 7c 20 53 51 4c 49 54 45 5f 4f  6.   || SQLITE_O
119aa 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32 62  K!=(rc = read32b
119ab 69 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d 31  its(pJrnl, szJ-1
119ac 36 2c 20 26 6c 65 6e 29 29 0a 20 20 20 7c 7c 20  6, &len)).   || 
119ad 6c 65 6e 3e 3d 6e 4d 61 73 74 65 72 20 0a 20 20  len>=nMaster .  
119ae 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   || SQLITE_OK!=(
119af 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28  rc = read32bits(
119b0 70 4a 72 6e 6c 2c 20 73 7a 4a 2d 31 32 2c 20 26  pJrnl, szJ-12, &
119b1 63 6b 73 75 6d 29 29 0a 20 20 20 7c 7c 20 53 51  cksum)).   || SQ
119b2 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73  LITE_OK!=(rc = s
119b3 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 4a 72  qlite3OsRead(pJr
119b4 6e 6c 2c 20 61 4d 61 67 69 63 2c 20 38 2c 20 73  nl, aMagic, 8, s
119b5 7a 4a 2d 38 29 29 0a 20 20 20 7c 7c 20 6d 65 6d  zJ-8)).   || mem
119b6 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75  cmp(aMagic, aJou
119b7 72 6e 61 6c 4d 61 67 69 63 2c 20 38 29 0a 20 20  rnalMagic, 8).  
119b8 20 7c 7c 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28   || SQLITE_OK!=(
119b9 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65  rc = sqlite3OsRe
119ba 61 64 28 70 4a 72 6e 6c 2c 20 7a 4d 61 73 74 65  ad(pJrnl, zMaste
119bb 72 2c 20 6c 65 6e 2c 20 73 7a 4a 2d 31 36 2d 6c  r, len, szJ-16-l
119bc 65 6e 29 29 0a 20 20 29 7b 0a 20 20 20 20 72 65  en)).  ){.    re
119bd 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
119be 2f 2a 20 53 65 65 20 69 66 20 74 68 65 20 63 68  /* See if the ch
119bf 65 63 6b 73 75 6d 20 6d 61 74 63 68 65 73 20 74  ecksum matches t
119c0 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
119c1 6c 20 6e 61 6d 65 20 2a 2f 0a 20 20 66 6f 72 28  l name */.  for(
119c2 75 3d 30 3b 20 75 3c 6c 65 6e 3b 20 75 2b 2b 29  u=0; u<len; u++)
119c3 7b 0a 20 20 20 20 63 6b 73 75 6d 20 2d 3d 20 7a  {.    cksum -= z
119c4 4d 61 73 74 65 72 5b 75 5d 3b 0a 20 20 7d 0a 20  Master[u];.  }. 
119c5 20 69 66 28 20 63 6b 73 75 6d 20 29 7b 0a 20 20   if( cksum ){.  
119c6 20 20 2f 2a 20 49 66 20 74 68 65 20 63 68 65 63    /* If the chec
119c7 6b 73 75 6d 20 64 6f 65 73 6e 27 74 20 61 64 64  ksum doesn't add
119c8 20 75 70 2c 20 74 68 65 6e 20 6f 6e 65 20 6f 72   up, then one or
119c9 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 64 69 73   more of the dis
119ca 6b 20 73 65 63 74 6f 72 73 0a 20 20 20 20 2a 2a  k sectors.    **
119cb 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
119cc 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
119cd 69 6c 65 6e 61 6d 65 20 69 73 20 63 6f 72 72 75  ilename is corru
119ce 70 74 65 64 2e 20 54 68 69 73 20 6d 65 61 6e 73  pted. This means
119cf 0a 20 20 20 20 2a 2a 20 64 65 66 69 6e 69 74 65  .    ** definite
119d0 6c 79 20 72 6f 6c 6c 20 62 61 63 6b 2c 20 73 6f  ly roll back, so
119d1 20 6a 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c   just return SQL
119d2 49 54 45 5f 4f 4b 20 61 6e 64 20 72 65 70 6f 72  ITE_OK and repor
119d3 74 20 61 20 28 6e 75 6c 29 0a 20 20 20 20 2a 2a  t a (nul).    **
119d4 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20   master-journal 
119d5 66 69 6c 65 6e 61 6d 65 2e 0a 20 20 20 20 2a 2f  filename..    */
119d6 0a 20 20 20 20 6c 65 6e 20 3d 20 30 3b 0a 20 20  .    len = 0;.  
119d7 7d 0a 20 20 7a 4d 61 73 74 65 72 5b 6c 65 6e 5d  }.  zMaster[len]
119d8 20 3d 20 27 5c 30 27 3b 0a 20 20 20 0a 20 20 72   = '\0';.   .  r
119d9 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
119da 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
119db 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66 20 74   the offset of t
119dc 68 65 20 73 65 63 74 6f 72 20 62 6f 75 6e 64 61  he sector bounda
119dd 72 79 20 61 74 20 6f 72 20 69 6d 6d 65 64 69 61  ry at or immedia
119de 74 65 6c 79 20 0a 2a 2a 20 66 6f 6c 6c 6f 77 69  tely .** followi
119df 6e 67 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  ng the value in 
119e0 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
119e1 66 66 2c 20 61 73 73 75 6d 69 6e 67 20 61 20 73  ff, assuming a s
119e2 65 63 74 6f 72 20 0a 2a 2a 20 73 69 7a 65 20 6f  ector .** size o
119e3 66 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72  f pPager->sector
119e4 53 69 7a 65 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a  Size bytes..**.*
119e5 2a 20 69 2e 65 20 66 6f 72 20 61 20 73 65 63 74  * i.e for a sect
119e6 6f 72 20 73 69 7a 65 20 6f 66 20 35 31 32 3a 0a  or size of 512:.
119e7 2a 2a 0a 2a 2a 20 20 20 50 61 67 65 72 2e 6a 6f  **.**   Pager.jo
119e8 75 72 6e 61 6c 4f 66 66 20 20 20 20 20 20 20 20  urnalOff        
119e9 20 20 52 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a    Return value.*
119ea 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *   ------------
119eb 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
119ec 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
119ed 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20   0              
119ee 20 20 20 20 20 20 20 20 20 20 20 30 0a 2a 2a 20             0.** 
119ef 20 20 35 31 32 20 20 20 20 20 20 20 20 20 20 20    512           
119f0 20 20 20 20 20 20 20 20 20 20 20 20 35 31 32 0a              512.
119f1 2a 2a 20 20 20 31 30 30 20 20 20 20 20 20 20 20  **   100        
119f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
119f3 31 32 0a 2a 2a 20 20 20 32 30 30 30 20 20 20 20  12.**   2000    
119f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
119f5 20 20 32 30 34 38 0a 2a 2a 20 0a 2a 2f 0a 73 74    2048.** .*/.st
119f6 61 74 69 63 20 69 36 34 20 6a 6f 75 72 6e 61 6c  atic i64 journal
119f7 48 64 72 4f 66 66 73 65 74 28 50 61 67 65 72 20  HdrOffset(Pager 
119f8 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 36 34 20  *pPager){.  i64 
119f9 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 69 36  offset = 0;.  i6
119fa 34 20 63 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f  4 c = pPager->jo
119fb 75 72 6e 61 6c 4f 66 66 3b 0a 20 20 69 66 28 20  urnalOff;.  if( 
119fc 63 20 29 7b 0a 20 20 20 20 6f 66 66 73 65 74 20  c ){.    offset 
119fd 3d 20 28 28 63 2d 31 29 2f 4a 4f 55 52 4e 41 4c  = ((c-1)/JOURNAL
119fe 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20  _HDR_SZ(pPager) 
119ff 2b 20 31 29 20 2a 20 4a 4f 55 52 4e 41 4c 5f 48  + 1) * JOURNAL_H
11a00 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20  DR_SZ(pPager);. 
11a01 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66   }.  assert( off
11a02 73 65 74 25 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  set%JOURNAL_HDR_
11a03 53 5a 28 70 50 61 67 65 72 29 3d 3d 30 20 29 3b  SZ(pPager)==0 );
11a04 0a 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 65  .  assert( offse
11a05 74 3e 3d 63 20 29 3b 0a 20 20 61 73 73 65 72 74  t>=c );.  assert
11a06 28 20 28 6f 66 66 73 65 74 2d 63 29 3c 4a 4f 55  ( (offset-c)<JOU
11a07 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67  RNAL_HDR_SZ(pPag
11a08 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  er) );.  return 
11a09 6f 66 66 73 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  offset;.}../*.**
11a0a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   The journal fil
11a0b 65 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 77  e must be open w
11a0c 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
11a0d 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a  n is called..**.
11a0e 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
11a0f 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74   is a no-op if t
11a10 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11a11 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 77 72 69  has not been wri
11a12 74 74 65 6e 20 74 6f 0a 2a 2a 20 77 69 74 68 69  tten to.** withi
11a13 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72  n the current tr
11a14 61 6e 73 61 63 74 69 6f 6e 20 28 69 2e 65 2e 20  ansaction (i.e. 
11a15 69 66 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c  if Pager.journal
11a16 4f 66 66 3d 3d 30 29 2e 0a 2a 2a 0a 2a 2a 20 49  Off==0)..**.** I
11a17 66 20 64 6f 54 72 75 6e 63 61 74 65 20 69 73 20  f doTruncate is 
11a18 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 20 74 68 65 20  non-zero or the 
11a19 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 53 69 7a  Pager.journalSiz
11a1a 65 4c 69 6d 69 74 20 76 61 72 69 61 62 6c 65 20  eLimit variable 
11a1b 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 30 2c 20  is.** set to 0, 
11a1c 74 68 65 6e 20 74 72 75 6e 63 61 74 65 20 74 68  then truncate th
11a1d 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74  e journal file t
11a1e 6f 20 7a 65 72 6f 20 62 79 74 65 73 20 69 6e 20  o zero bytes in 
11a1f 73 69 7a 65 2e 20 4f 74 68 65 72 77 69 73 65 2c  size. Otherwise,
11a20 0a 2a 2a 20 7a 65 72 6f 20 74 68 65 20 32 38 2d  .** zero the 28-
11a21 62 79 74 65 20 68 65 61 64 65 72 20 61 74 20 74  byte header at t
11a22 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
11a23 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 6e  journal file. In
11a24 20 65 69 74 68 65 72 20 63 61 73 65 2c 20 0a 2a   either case, .*
11a25 2a 20 69 66 20 74 68 65 20 70 61 67 65 72 20 69  * if the pager i
11a26 73 20 6e 6f 74 20 69 6e 20 6e 6f 2d 73 79 6e 63  s not in no-sync
11a27 20 6d 6f 64 65 2c 20 73 79 6e 63 20 74 68 65 20   mode, sync the 
11a28 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 6d 6d  journal file imm
11a29 65 64 69 61 74 65 6c 79 20 0a 2a 2a 20 61 66 74  ediately .** aft
11a2a 65 72 20 77 72 69 74 69 6e 67 20 6f 72 20 74 72  er writing or tr
11a2b 75 6e 63 61 74 69 6e 67 20 69 74 2e 0a 2a 2a 0a  uncating it..**.
11a2c 2a 2a 20 49 66 20 50 61 67 65 72 2e 6a 6f 75 72  ** If Pager.jour
11a2d 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 69 73 20  nalSizeLimit is 
11a2e 73 65 74 20 74 6f 20 61 20 70 6f 73 69 74 69 76  set to a positiv
11a2f 65 2c 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75  e, non-zero valu
11a30 65 2c 20 61 6e 64 0a 2a 2a 20 66 6f 6c 6c 6f 77  e, and.** follow
11a31 69 6e 67 20 74 68 65 20 74 72 75 6e 63 61 74 69  ing the truncati
11a32 6f 6e 20 6f 72 20 7a 65 72 6f 69 6e 67 20 64 65  on or zeroing de
11a33 73 63 72 69 62 65 64 20 61 62 6f 76 65 20 74 68  scribed above th
11a34 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 0a 2a  e size of the .*
11a35 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  * journal file i
11a36 6e 20 62 79 74 65 73 20 69 73 20 6c 61 72 67 65  n bytes is large
11a37 72 20 74 68 61 6e 20 74 68 69 73 20 76 61 6c 75  r than this valu
11a38 65 2c 20 74 68 65 6e 20 74 72 75 6e 63 61 74 65  e, then truncate
11a39 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20   the.** journal 
11a3a 66 69 6c 65 20 74 6f 20 50 61 67 65 72 2e 6a 6f  file to Pager.jo
11a3b 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 62  urnalSizeLimit b
11a3c 79 74 65 73 2e 20 54 68 65 20 6a 6f 75 72 6e 61  ytes. The journa
11a3d 6c 20 66 69 6c 65 20 64 6f 65 73 0a 2a 2a 20 6e  l file does.** n
11a3e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 73 79  ot need to be sy
11a3f 6e 63 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 74  nced following t
11a40 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a  his operation..*
11a41 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72  *.** If an IO er
11a42 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 62 61 6e  ror occurs, aban
11a43 64 6f 6e 20 70 72 6f 63 65 73 73 69 6e 67 20 61  don processing a
11a44 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 49 4f  nd return the IO
11a45 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a 20   error code..** 
11a46 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75 72  Otherwise, retur
11a47 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a  n SQLITE_OK..*/.
11a48 73 74 61 74 69 63 20 69 6e 74 20 7a 65 72 6f 4a  static int zeroJ
11a49 6f 75 72 6e 61 6c 48 64 72 28 50 61 67 65 72 20  ournalHdr(Pager 
11a4a 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 64 6f 54  *pPager, int doT
11a4b 72 75 6e 63 61 74 65 29 7b 0a 20 20 69 6e 74 20  runcate){.  int 
11a4c 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
11a4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11a4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11a4f 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
11a50 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e    assert( isOpen
11a51 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b  (pPager->jfd) );
11a52 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a  .  if( pPager->j
11a53 6f 75 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20  ournalOff ){.   
11a54 20 63 6f 6e 73 74 20 69 36 34 20 69 4c 69 6d 69   const i64 iLimi
11a55 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  t = pPager->jour
11a56 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 20 20 20  nalSizeLimit;   
11a57 20 2f 2a 20 4c 6f 63 61 6c 20 63 61 63 68 65 20   /* Local cache 
11a58 6f 66 20 6a 73 6c 20 2a 2f 0a 0a 20 20 20 20 49  of jsl */..    I
11a59 4f 54 52 41 43 45 28 28 22 4a 5a 45 52 4f 48 44  OTRACE(("JZEROHD
11a5a 52 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29  R %p\n", pPager)
11a5b 29 0a 20 20 20 20 69 66 28 20 64 6f 54 72 75 6e  ).    if( doTrun
11a5c 63 61 74 65 20 7c 7c 20 69 4c 69 6d 69 74 3d 3d  cate || iLimit==
11a5d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
11a5e 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74  sqlite3OsTruncat
11a5f 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 30  e(pPager->jfd, 0
11a60 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
11a61 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
11a62 20 63 68 61 72 20 7a 65 72 6f 48 64 72 5b 32 38   char zeroHdr[28
11a63 5d 20 3d 20 7b 30 7d 3b 0a 20 20 20 20 20 20 72  ] = {0};.      r
11a64 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69  c = sqlite3OsWri
11a65 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  te(pPager->jfd, 
11a66 7a 65 72 6f 48 64 72 2c 20 73 69 7a 65 6f 66 28  zeroHdr, sizeof(
11a67 7a 65 72 6f 48 64 72 29 2c 20 30 29 3b 0a 20 20  zeroHdr), 0);.  
11a68 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d    }.    if( rc==
11a69 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 70 50  SQLITE_OK && !pP
11a6a 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a  ager->noSync ){.
11a6b 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
11a6c 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d  e3OsSync(pPager-
11a6d 3e 6a 66 64 2c 20 53 51 4c 49 54 45 5f 53 59 4e  >jfd, SQLITE_SYN
11a6e 43 5f 44 41 54 41 4f 4e 4c 59 7c 70 50 61 67 65  C_DATAONLY|pPage
11a6f 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 29 3b 0a  r->sync_flags);.
11a70 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 74      }..    /* At
11a71 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 20   this point the 
11a72 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63  transaction is c
11a73 6f 6d 6d 69 74 74 65 64 20 62 75 74 20 74 68 65  ommitted but the
11a74 20 77 72 69 74 65 20 6c 6f 63 6b 20 0a 20 20 20   write lock .   
11a75 20 2a 2a 20 69 73 20 73 74 69 6c 6c 20 68 65 6c   ** is still hel
11a76 64 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 20 49  d on the file. I
11a77 66 20 74 68 65 72 65 20 69 73 20 61 20 73 69 7a  f there is a siz
11a78 65 20 6c 69 6d 69 74 20 63 6f 6e 66 69 67 75 72  e limit configur
11a79 65 64 20 66 6f 72 20 0a 20 20 20 20 2a 2a 20 74  ed for .    ** t
11a7a 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f  he persistent jo
11a7b 75 72 6e 61 6c 20 61 6e 64 20 74 68 65 20 6a 6f  urnal and the jo
11a7c 75 72 6e 61 6c 20 66 69 6c 65 20 63 75 72 72 65  urnal file curre
11a7d 6e 74 6c 79 20 63 6f 6e 73 75 6d 65 73 20 6d 6f  ntly consumes mo
11a7e 72 65 0a 20 20 20 20 2a 2a 20 73 70 61 63 65 20  re.    ** space 
11a7f 74 68 61 6e 20 74 68 61 74 20 6c 69 6d 69 74 20  than that limit 
11a80 61 6c 6c 6f 77 73 20 66 6f 72 2c 20 74 72 75 6e  allows for, trun
11a81 63 61 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65  cate it now. The
11a82 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20  re is no need.  
11a83 20 20 2a 2a 20 74 6f 20 73 79 6e 63 20 74 68 65    ** to sync the
11a84 20 66 69 6c 65 20 66 6f 6c 6c 6f 77 69 6e 67 20   file following 
11a85 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 0a  this operation..
11a86 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72      */.    if( r
11a87 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
11a88 69 4c 69 6d 69 74 3e 30 20 29 7b 0a 20 20 20 20  iLimit>0 ){.    
11a89 20 20 69 36 34 20 73 7a 3b 0a 20 20 20 20 20 20    i64 sz;.      
11a8a 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46 69  rc = sqlite3OsFi
11a8b 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e 6a  leSize(pPager->j
11a8c 66 64 2c 20 26 73 7a 29 3b 0a 20 20 20 20 20 20  fd, &sz);.      
11a8d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
11a8e 4b 20 26 26 20 73 7a 3e 69 4c 69 6d 69 74 20 29  K && sz>iLimit )
11a8f 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
11a90 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65  qlite3OsTruncate
11a91 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 4c  (pPager->jfd, iL
11a92 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20  imit);.      }. 
11a93 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
11a94 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
11a95 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11a96 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65  must be open whe
11a97 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
11a98 73 20 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72  s called. A jour
11a99 6e 61 6c 0a 2a 2a 20 68 65 61 64 65 72 20 28 4a  nal.** header (J
11a9a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79  OURNAL_HDR_SZ by
11a9b 74 65 73 29 20 69 73 20 77 72 69 74 74 65 6e 20  tes) is written 
11a9c 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  into the journal
11a9d 20 66 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a 20   file at the.** 
11a9e 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e  current location
11a9f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d  ..**.** The form
11aa0 61 74 20 66 6f 72 20 74 68 65 20 6a 6f 75 72 6e  at for the journ
11aa1 61 6c 20 68 65 61 64 65 72 20 69 73 20 61 73 20  al header is as 
11aa2 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 20 2d 20 38 20  follows:.** - 8 
11aa3 62 79 74 65 73 3a 20 4d 61 67 69 63 20 69 64 65  bytes: Magic ide
11aa4 6e 74 69 66 79 69 6e 67 20 6a 6f 75 72 6e 61 6c  ntifying journal
11aa5 20 66 6f 72 6d 61 74 2e 0a 2a 2a 20 2d 20 34 20   format..** - 4 
11aa6 62 79 74 65 73 3a 20 4e 75 6d 62 65 72 20 6f 66  bytes: Number of
11aa7 20 72 65 63 6f 72 64 73 20 69 6e 20 6a 6f 75 72   records in jour
11aa8 6e 61 6c 2c 20 6f 72 20 2d 31 20 6e 6f 2d 73 79  nal, or -1 no-sy
11aa9 6e 63 20 6d 6f 64 65 20 69 73 20 6f 6e 2e 0a 2a  nc mode is on..*
11aaa 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 52 61 6e  * - 4 bytes: Ran
11aab 64 6f 6d 20 6e 75 6d 62 65 72 20 75 73 65 64 20  dom number used 
11aac 66 6f 72 20 70 61 67 65 20 68 61 73 68 2e 0a 2a  for page hash..*
11aad 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 49 6e 69  * - 4 bytes: Ini
11aae 74 69 61 6c 20 64 61 74 61 62 61 73 65 20 70 61  tial database pa
11aaf 67 65 20 63 6f 75 6e 74 2e 0a 2a 2a 20 2d 20 34  ge count..** - 4
11ab0 20 62 79 74 65 73 3a 20 53 65 63 74 6f 72 20 73   bytes: Sector s
11ab1 69 7a 65 20 75 73 65 64 20 62 79 20 74 68 65 20  ize used by the 
11ab2 70 72 6f 63 65 73 73 20 74 68 61 74 20 77 72 6f  process that wro
11ab3 74 65 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 2e  te this journal.
11ab4 0a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 44  .** - 4 bytes: D
11ab5 61 74 61 62 61 73 65 20 70 61 67 65 20 73 69 7a  atabase page siz
11ab6 65 2e 0a 2a 2a 20 0a 2a 2a 20 46 6f 6c 6c 6f 77  e..** .** Follow
11ab7 65 64 20 62 79 20 28 4a 4f 55 52 4e 41 4c 5f 48  ed by (JOURNAL_H
11ab8 44 52 5f 53 5a 20 2d 20 32 38 29 20 62 79 74 65  DR_SZ - 28) byte
11ab9 73 20 6f 66 20 75 6e 75 73 65 64 20 73 70 61 63  s of unused spac
11aba 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
11abb 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72   writeJournalHdr
11abc 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
11abd 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
11abe 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20  TE_OK;          
11abf 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
11ac0 20 63 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20   code */.  char 
11ac1 2a 7a 48 65 61 64 65 72 20 3d 20 70 50 61 67 65  *zHeader = pPage
11ac2 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 20 20 2f  r->pTmpSpace;  /
11ac3 2a 20 54 65 6d 70 6f 72 61 72 79 20 73 70 61 63  * Temporary spac
11ac4 65 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64 20  e used to build 
11ac5 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20  header */.  u32 
11ac6 6e 48 65 61 64 65 72 20 3d 20 70 50 61 67 65 72  nHeader = pPager
11ac7 2d 3e 70 61 67 65 53 69 7a 65 3b 20 20 20 20 20  ->pageSize;     
11ac8 2f 2a 20 53 69 7a 65 20 6f 66 20 62 75 66 66 65  /* Size of buffe
11ac9 72 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  r pointed to by 
11aca 7a 48 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32  zHeader */.  u32
11acb 20 6e 57 72 69 74 65 3b 20 20 20 20 20 20 20 20   nWrite;        
11acc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11acd 20 2f 2a 20 42 79 74 65 73 20 6f 66 20 68 65 61   /* Bytes of hea
11ace 64 65 72 20 73 65 63 74 6f 72 20 77 72 69 74 74  der sector writt
11acf 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 69 3b 20  en */.  int ii; 
11ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11ad1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
11ad2 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a  oop counter */..
11ad3 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e    assert( isOpen
11ad4 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b  (pPager->jfd) );
11ad5 20 20 20 20 20 20 2f 2a 20 4a 6f 75 72 6e 61 6c        /* Journal
11ad6 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70   file must be op
11ad7 65 6e 2e 20 2a 2f 0a 0a 20 20 69 66 28 20 6e 48  en. */..  if( nH
11ad8 65 61 64 65 72 3e 4a 4f 55 52 4e 41 4c 5f 48 44  eader>JOURNAL_HD
11ad9 52 5f 53 5a 28 70 50 61 67 65 72 29 20 29 7b 0a  R_SZ(pPager) ){.
11ada 20 20 20 20 6e 48 65 61 64 65 72 20 3d 20 4a 4f      nHeader = JO
11adb 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
11adc 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ger);.  }..  /* 
11add 49 66 20 74 68 65 72 65 20 61 72 65 20 61 63 74  If there are act
11ade 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73 20 61  ive savepoints a
11adf 6e 64 20 61 6e 79 20 6f 66 20 74 68 65 6d 20 77  nd any of them w
11ae0 65 72 65 20 63 72 65 61 74 65 64 20 0a 20 20 2a  ere created .  *
11ae1 2a 20 73 69 6e 63 65 20 74 68 65 20 6d 6f 73 74  * since the most
11ae2 20 72 65 63 65 6e 74 20 6a 6f 75 72 6e 61 6c 20   recent journal 
11ae3 68 65 61 64 65 72 20 77 61 73 20 77 72 69 74 74  header was writt
11ae4 65 6e 2c 20 75 70 64 61 74 65 20 74 68 65 20 0a  en, update the .
11ae5 20 20 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f    ** PagerSavepo
11ae6 69 6e 74 2e 69 48 64 72 4f 66 66 73 65 74 20 66  int.iHdrOffset f
11ae7 69 65 6c 64 73 20 6e 6f 77 2e 0a 20 20 2a 2f 0a  ields now..  */.
11ae8 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70    for(ii=0; ii<p
11ae9 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e  Pager->nSavepoin
11aea 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 66  t; ii++){.    if
11aeb 28 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70  ( pPager->aSavep
11aec 6f 69 6e 74 5b 69 69 5d 2e 69 48 64 72 4f 66 66  oint[ii].iHdrOff
11aed 73 65 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  set==0 ){.      
11aee 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69  pPager->aSavepoi
11aef 6e 74 5b 69 69 5d 2e 69 48 64 72 4f 66 66 73 65  nt[ii].iHdrOffse
11af0 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  t = pPager->jour
11af1 6e 61 6c 4f 66 66 3b 0a 20 20 20 20 7d 0a 20 20  nalOff;.    }.  
11af2 7d 0a 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75  }..  pPager->jou
11af3 72 6e 61 6c 48 64 72 20 3d 20 70 50 61 67 65 72  rnalHdr = pPager
11af4 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 6a  ->journalOff = j
11af5 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73 65 74 28  ournalHdrOffset(
11af6 70 50 61 67 65 72 29 3b 0a 0a 20 20 2f 2a 20 0a  pPager);..  /* .
11af7 20 20 2a 2a 20 57 72 69 74 65 20 74 68 65 20 6e    ** Write the n
11af8 52 65 63 20 46 69 65 6c 64 20 2d 20 74 68 65 20  Rec Field - the 
11af9 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 20 72  number of page r
11afa 65 63 6f 72 64 73 20 74 68 61 74 20 66 6f 6c 6c  ecords that foll
11afb 6f 77 20 74 68 69 73 0a 20 20 2a 2a 20 6a 6f 75  ow this.  ** jou
11afc 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 4e 6f 72  rnal header. Nor
11afd 6d 61 6c 6c 79 2c 20 7a 65 72 6f 20 69 73 20 77  mally, zero is w
11afe 72 69 74 74 65 6e 20 74 6f 20 74 68 69 73 20 76  ritten to this v
11aff 61 6c 75 65 20 61 74 20 74 68 69 73 20 74 69 6d  alue at this tim
11b00 65 2e 0a 20 20 2a 2a 20 41 66 74 65 72 20 74 68  e..  ** After th
11b01 65 20 72 65 63 6f 72 64 73 20 61 72 65 20 61 64  e records are ad
11b02 64 65 64 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ded to the journ
11b03 61 6c 20 28 61 6e 64 20 74 68 65 20 6a 6f 75 72  al (and the jour
11b04 6e 61 6c 20 73 79 6e 63 65 64 2c 20 0a 20 20 2a  nal synced, .  *
11b05 2a 20 69 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e  * if in full-syn
11b06 63 20 6d 6f 64 65 29 2c 20 74 68 65 20 7a 65 72  c mode), the zer
11b07 6f 20 69 73 20 6f 76 65 72 77 72 69 74 74 65 6e  o is overwritten
11b08 20 77 69 74 68 20 74 68 65 20 74 72 75 65 20 6e   with the true n
11b09 75 6d 62 65 72 0a 20 20 2a 2a 20 6f 66 20 72 65  umber.  ** of re
11b0a 63 6f 72 64 73 20 28 73 65 65 20 73 79 6e 63 4a  cords (see syncJ
11b0b 6f 75 72 6e 61 6c 28 29 29 2e 0a 20 20 2a 2a 0a  ournal())..  **.
11b0c 20 20 2a 2a 20 41 20 66 61 73 74 65 72 20 61 6c    ** A faster al
11b0d 74 65 72 6e 61 74 69 76 65 20 69 73 20 74 6f 20  ternative is to 
11b0e 77 72 69 74 65 20 30 78 46 46 46 46 46 46 46 46  write 0xFFFFFFFF
11b0f 20 74 6f 20 74 68 65 20 6e 52 65 63 20 66 69 65   to the nRec fie
11b10 6c 64 2e 20 57 68 65 6e 0a 20 20 2a 2a 20 72 65  ld. When.  ** re
11b11 61 64 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61  ading the journa
11b12 6c 20 74 68 69 73 20 76 61 6c 75 65 20 74 65 6c  l this value tel
11b13 6c 73 20 53 51 4c 69 74 65 20 74 6f 20 61 73 73  ls SQLite to ass
11b14 75 6d 65 20 74 68 61 74 20 74 68 65 0a 20 20 2a  ume that the.  *
11b15 2a 20 72 65 73 74 20 6f 66 20 74 68 65 20 6a 6f  * rest of the jo
11b16 75 72 6e 61 6c 20 66 69 6c 65 20 63 6f 6e 74 61  urnal file conta
11b17 69 6e 73 20 76 61 6c 69 64 20 70 61 67 65 20 72  ins valid page r
11b18 65 63 6f 72 64 73 2e 20 54 68 69 73 20 61 73 73  ecords. This ass
11b19 75 6d 70 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20  umption.  ** is 
11b1a 64 61 6e 67 65 72 6f 75 73 2c 20 61 73 20 69 66  dangerous, as if
11b1b 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72   a failure occur
11b1c 72 65 64 20 77 68 69 6c 73 74 20 77 72 69 74 69  red whilst writi
11b1d 6e 67 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61  ng to the journa
11b1e 6c 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74 20 6d  l.  ** file it m
11b1f 61 79 20 63 6f 6e 74 61 69 6e 20 73 6f 6d 65 20  ay contain some 
11b20 67 61 72 62 61 67 65 20 64 61 74 61 2e 20 54 68  garbage data. Th
11b21 65 72 65 20 61 72 65 20 74 77 6f 20 73 63 65 6e  ere are two scen
11b22 61 72 69 6f 73 0a 20 20 2a 2a 20 77 68 65 72 65  arios.  ** where
11b23 20 74 68 69 73 20 72 69 73 6b 20 63 61 6e 20 62   this risk can b
11b24 65 20 69 67 6e 6f 72 65 64 3a 0a 20 20 2a 2a 0a  e ignored:.  **.
11b25 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74 68    **   * When th
11b26 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 6e 6f  e pager is in no
11b27 2d 73 79 6e 63 20 6d 6f 64 65 2e 20 43 6f 72 72  -sync mode. Corr
11b28 75 70 74 69 6f 6e 20 63 61 6e 20 66 6f 6c 6c 6f  uption can follo
11b29 77 20 61 0a 20 20 2a 2a 20 20 20 20 20 70 6f 77  w a.  **     pow
11b2a 65 72 20 66 61 69 6c 75 72 65 20 69 6e 20 74 68  er failure in th
11b2b 69 73 20 63 61 73 65 20 61 6e 79 77 61 79 2e 0a  is case anyway..
11b2c 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57 68    **.  **   * Wh
11b2d 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f 49 4f  en the SQLITE_IO
11b2e 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 20  CAP_SAFE_APPEND 
11b2f 66 6c 61 67 20 69 73 20 73 65 74 2e 20 54 68 69  flag is set. Thi
11b30 73 20 67 75 61 72 61 6e 74 65 65 73 0a 20 20 2a  s guarantees.  *
11b31 2a 20 20 20 20 20 74 68 61 74 20 67 61 72 62 61  *     that garba
11b32 67 65 20 64 61 74 61 20 69 73 20 6e 65 76 65 72  ge data is never
11b33 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65   appended to the
11b34 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 20   journal file.. 
11b35 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73   */.  assert( is
11b36 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29  Open(pPager->fd)
11b37 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79   || pPager->noSy
11b38 6e 63 20 29 3b 0a 20 20 69 66 28 20 28 70 50 61  nc );.  if( (pPa
11b39 67 65 72 2d 3e 6e 6f 53 79 6e 63 29 20 7c 7c 20  ger->noSync) || 
11b3a 28 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c  (pPager->journal
11b3b 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
11b3c 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 0a  NALMODE_MEMORY).
11b3d 20 20 20 7c 7c 20 28 73 71 6c 69 74 65 33 4f 73     || (sqlite3Os
11b3e 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
11b3f 73 74 69 63 73 28 70 50 61 67 65 72 2d 3e 66 64  stics(pPager->fd
11b40 29 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  )&SQLITE_IOCAP_S
11b41 41 46 45 5f 41 50 50 45 4e 44 29 20 0a 20 20 29  AFE_APPEND) .  )
11b42 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 48 65  {.    memcpy(zHe
11b43 61 64 65 72 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61  ader, aJournalMa
11b44 67 69 63 2c 20 73 69 7a 65 6f 66 28 61 4a 6f 75  gic, sizeof(aJou
11b45 72 6e 61 6c 4d 61 67 69 63 29 29 3b 0a 20 20 20  rnalMagic));.   
11b46 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61   put32bits(&zHea
11b47 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72  der[sizeof(aJour
11b48 6e 61 6c 4d 61 67 69 63 29 5d 2c 20 30 78 66 66  nalMagic)], 0xff
11b49 66 66 66 66 66 66 29 3b 0a 20 20 7d 65 6c 73 65  ffffff);.  }else
11b4a 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 7a 48 65  {.    memset(zHe
11b4b 61 64 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28  ader, 0, sizeof(
11b4c 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 34  aJournalMagic)+4
11b4d 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65  );.  }..  /* The
11b4e 20 72 61 6e 64 6f 6d 20 63 68 65 63 6b 2d 68 61   random check-ha
11b4f 73 68 20 69 6e 69 74 69 61 6c 69 73 65 72 20 2a  sh initialiser *
11b50 2f 20 0a 20 20 73 71 6c 69 74 65 33 5f 72 61 6e  / .  sqlite3_ran
11b51 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 70  domness(sizeof(p
11b52 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74  Pager->cksumInit
11b53 29 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b 73 75  ), &pPager->cksu
11b54 6d 49 6e 69 74 29 3b 0a 20 20 70 75 74 33 32 62  mInit);.  put32b
11b55 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69 7a  its(&zHeader[siz
11b56 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  eof(aJournalMagi
11b57 63 29 2b 34 5d 2c 20 70 50 61 67 65 72 2d 3e 63  c)+4], pPager->c
11b58 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 2f 2a 20  ksumInit);.  /* 
11b59 54 68 65 20 69 6e 69 74 69 61 6c 20 64 61 74 61  The initial data
11b5a 62 61 73 65 20 73 69 7a 65 20 2a 2f 0a 20 20 70  base size */.  p
11b5b 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65  ut32bits(&zHeade
11b5c 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61  r[sizeof(aJourna
11b5d 6c 4d 61 67 69 63 29 2b 38 5d 2c 20 70 50 61 67  lMagic)+8], pPag
11b5e 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 29 3b  er->dbOrigSize);
11b5f 0a 20 20 2f 2a 20 54 68 65 20 61 73 73 75 6d 65  .  /* The assume
11b60 64 20 73 65 63 74 6f 72 20 73 69 7a 65 20 66 6f  d sector size fo
11b61 72 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 2a  r this process *
11b62 2f 0a 20 20 70 75 74 33 32 62 69 74 73 28 26 7a  /.  put32bits(&z
11b63 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a  Header[sizeof(aJ
11b64 6f 75 72 6e 61 6c 4d 61 67 69 63 29 2b 31 32 5d  ournalMagic)+12]
11b65 2c 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72  , pPager->sector
11b66 53 69 7a 65 29 3b 0a 0a 20 20 2f 2a 20 54 68 65  Size);..  /* The
11b67 20 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20   page size */.  
11b68 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64  put32bits(&zHead
11b69 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e  er[sizeof(aJourn
11b6a 61 6c 4d 61 67 69 63 29 2b 31 36 5d 2c 20 70 50  alMagic)+16], pP
11b6b 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b  ager->pageSize);
11b6c 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a  ..  /* Initializ
11b6d 69 6e 67 20 74 68 65 20 74 61 69 6c 20 6f 66 20  ing the tail of 
11b6e 74 68 65 20 62 75 66 66 65 72 20 69 73 20 6e 6f  the buffer is no
11b6f 74 20 6e 65 63 65 73 73 61 72 79 2e 20 20 45 76  t necessary.  Ev
11b70 65 72 79 74 68 69 6e 67 0a 20 20 2a 2a 20 77 6f  erything.  ** wo
11b71 72 6b 73 20 66 69 6e 64 20 69 66 20 74 68 65 20  rks find if the 
11b72 66 6f 6c 6c 6f 77 69 6e 67 20 6d 65 6d 73 65 74  following memset
11b73 28 29 20 69 73 20 6f 6d 69 74 74 65 64 2e 20 20  () is omitted.  
11b74 42 75 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67  But initializing
11b75 0a 20 20 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79  .  ** the memory
11b76 20 70 72 65 76 65 6e 74 73 20 76 61 6c 67 72 69   prevents valgri
11b77 6e 64 20 66 72 6f 6d 20 63 6f 6d 70 6c 61 69 6e  nd from complain
11b78 69 6e 67 2c 20 73 6f 20 77 65 20 61 72 65 20 77  ing, so we are w
11b79 69 6c 6c 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 74  illing to.  ** t
11b7a 61 6b 65 20 74 68 65 20 70 65 72 66 6f 72 6d 61  ake the performa
11b7b 6e 63 65 20 68 69 74 2e 0a 20 20 2a 2f 0a 20 20  nce hit..  */.  
11b7c 6d 65 6d 73 65 74 28 26 7a 48 65 61 64 65 72 5b  memset(&zHeader[
11b7d 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d  sizeof(aJournalM
11b7e 61 67 69 63 29 2b 32 30 5d 2c 20 30 2c 0a 20 20  agic)+20], 0,.  
11b7f 20 20 20 20 20 20 20 6e 48 65 61 64 65 72 2d 28         nHeader-(
11b80 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d  sizeof(aJournalM
11b81 61 67 69 63 29 2b 32 30 29 29 3b 0a 0a 20 20 2f  agic)+20));..  /
11b82 2a 20 49 6e 20 74 68 65 6f 72 79 2c 20 69 74 20  * In theory, it 
11b83 69 73 20 6f 6e 6c 79 20 6e 65 63 65 73 73 61 72  is only necessar
11b84 79 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 32  y to write the 2
11b85 38 20 62 79 74 65 73 20 74 68 61 74 20 74 68 65  8 bytes that the
11b86 20 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 68   .  ** journal h
11b87 65 61 64 65 72 20 63 6f 6e 73 75 6d 65 73 20 74  eader consumes t
11b88 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  o the journal fi
11b89 6c 65 20 68 65 72 65 2e 20 54 68 65 6e 20 69 6e  le here. Then in
11b8a 63 72 65 6d 65 6e 74 20 74 68 65 20 0a 20 20 2a  crement the .  *
11b8b 2a 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f  * Pager.journalO
11b8c 66 66 20 76 61 72 69 61 62 6c 65 20 62 79 20 4a  ff variable by J
11b8d 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 73 6f  OURNAL_HDR_SZ so
11b8e 20 74 68 61 74 20 74 68 65 20 6e 65 78 74 20 0a   that the next .
11b8f 20 20 2a 2a 20 72 65 63 6f 72 64 20 69 73 20 77    ** record is w
11b90 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 66 6f  ritten to the fo
11b91 6c 6c 6f 77 69 6e 67 20 73 65 63 74 6f 72 20 28  llowing sector (
11b92 6c 65 61 76 69 6e 67 20 61 20 67 61 70 20 69 6e  leaving a gap in
11b93 20 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 74   the file.  ** t
11b94 68 61 74 20 77 69 6c 6c 20 62 65 20 69 6d 70 6c  hat will be impl
11b95 69 63 69 74 6c 79 20 66 69 6c 6c 65 64 20 69 6e  icitly filled in
11b96 20 62 79 20 74 68 65 20 4f 53 29 2e 0a 20 20 2a   by the OS)..  *
11b97 2a 0a 20 20 2a 2a 20 48 6f 77 65 76 65 72 20 69  *.  ** However i
11b98 74 20 68 61 73 20 62 65 65 6e 20 64 69 73 63 6f  t has been disco
11b99 76 65 72 65 64 20 74 68 61 74 20 6f 6e 20 73 6f  vered that on so
11b9a 6d 65 20 73 79 73 74 65 6d 73 20 74 68 69 73 20  me systems this 
11b9b 70 61 74 74 65 72 6e 20 63 61 6e 20 0a 20 20 2a  pattern can .  *
11b9c 2a 20 62 65 20 73 69 67 6e 69 66 69 63 61 6e 74  * be significant
11b9d 6c 79 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 63  ly slower than c
11b9e 6f 6e 74 69 67 75 6f 75 73 6c 79 20 77 72 69 74  ontiguously writ
11b9f 69 6e 67 20 64 61 74 61 20 74 6f 20 74 68 65 20  ing data to the 
11ba0 66 69 6c 65 2c 0a 20 20 2a 2a 20 65 76 65 6e 20  file,.  ** even 
11ba1 69 66 20 74 68 61 74 20 6d 65 61 6e 73 20 65 78  if that means ex
11ba2 70 6c 69 63 69 74 6c 79 20 77 72 69 74 69 6e 67  plicitly writing
11ba3 20 64 61 74 61 20 74 6f 20 74 68 65 20 62 6c 6f   data to the blo
11ba4 63 6b 20 6f 66 20 0a 20 20 2a 2a 20 28 4a 4f 55  ck of .  ** (JOU
11ba5 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38  RNAL_HDR_SZ - 28
11ba6 29 20 62 79 74 65 73 20 74 68 61 74 20 77 69 6c  ) bytes that wil
11ba7 6c 20 6e 6f 74 20 62 65 20 75 73 65 64 2e 20 53  l not be used. S
11ba8 6f 20 74 68 61 74 20 69 73 20 77 68 61 74 0a 20  o that is what. 
11ba9 20 2a 2a 20 69 73 20 64 6f 6e 65 2e 20 0a 20 20   ** is done. .  
11baa 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 6c 6f 6f 70  **.  ** The loop
11bab 20 69 73 20 72 65 71 75 69 72 65 64 20 68 65 72   is required her
11bac 65 20 69 6e 20 63 61 73 65 20 74 68 65 20 73 65  e in case the se
11bad 63 74 6f 72 2d 73 69 7a 65 20 69 73 20 6c 61 72  ctor-size is lar
11bae 67 65 72 20 74 68 61 6e 20 74 68 65 20 0a 20 20  ger than the .  
11baf 2a 2a 20 64 61 74 61 62 61 73 65 20 70 61 67 65  ** database page
11bb0 20 73 69 7a 65 2e 20 53 69 6e 63 65 20 74 68 65   size. Since the
11bb1 20 7a 48 65 61 64 65 72 20 62 75 66 66 65 72 20   zHeader buffer 
11bb2 69 73 20 6f 6e 6c 79 20 50 61 67 65 72 2e 70 61  is only Pager.pa
11bb3 67 65 53 69 7a 65 0a 20 20 2a 2a 20 62 79 74 65  geSize.  ** byte
11bb4 73 20 69 6e 20 73 69 7a 65 2c 20 6d 6f 72 65 20  s in size, more 
11bb5 74 68 61 6e 20 6f 6e 65 20 63 61 6c 6c 20 74 6f  than one call to
11bb6 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
11bb7 29 20 6d 61 79 20 62 65 20 72 65 71 75 69 72 65  ) may be require
11bb8 64 0a 20 20 2a 2a 20 74 6f 20 70 6f 70 75 6c 61  d.  ** to popula
11bb9 74 65 20 74 68 65 20 65 6e 74 69 72 65 20 6a 6f  te the entire jo
11bba 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 65 63  urnal header sec
11bbb 74 6f 72 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72  tor..  */ .  for
11bbc 28 6e 57 72 69 74 65 3d 30 3b 20 72 63 3d 3d 53  (nWrite=0; rc==S
11bbd 51 4c 49 54 45 5f 4f 4b 26 26 6e 57 72 69 74 65  QLITE_OK&&nWrite
11bbe 3c 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28  <JOURNAL_HDR_SZ(
11bbf 70 50 61 67 65 72 29 3b 20 6e 57 72 69 74 65 2b  pPager); nWrite+
11bc0 3d 6e 48 65 61 64 65 72 29 7b 0a 20 20 20 20 49  =nHeader){.    I
11bc1 4f 54 52 41 43 45 28 28 22 4a 48 44 52 20 25 70  OTRACE(("JHDR %p
11bc2 20 25 6c 6c 64 20 25 64 5c 6e 22 2c 20 70 50 61   %lld %d\n", pPa
11bc3 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ger, pPager->jou
11bc4 72 6e 61 6c 48 64 72 2c 20 6e 48 65 61 64 65 72  rnalHdr, nHeader
11bc5 29 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  )).    rc = sqli
11bc6 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65  te3OsWrite(pPage
11bc7 72 2d 3e 6a 66 64 2c 20 7a 48 65 61 64 65 72 2c  r->jfd, zHeader,
11bc8 20 6e 48 65 61 64 65 72 2c 20 70 50 61 67 65 72   nHeader, pPager
11bc9 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20  ->journalOff);. 
11bca 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
11bcb 61 6c 4f 66 66 20 2b 3d 20 6e 48 65 61 64 65 72  alOff += nHeader
11bcc 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
11bcd 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  rc;.}../*.** The
11bce 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75   journal file mu
11bcf 73 74 20 62 65 20 6f 70 65 6e 20 77 68 65 6e 20  st be open when 
11bd0 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2e 20  this is called. 
11bd1 41 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  A journal header
11bd2 20 66 69 6c 65 0a 2a 2a 20 28 4a 4f 55 52 4e 41   file.** (JOURNA
11bd3 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65 73 29 20  L_HDR_SZ bytes) 
11bd4 69 73 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65  is read from the
11bd5 20 63 75 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f   current locatio
11bd6 6e 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  n in the journal
11bd7 0a 2a 2a 20 66 69 6c 65 2e 20 54 68 65 20 63 75  .** file. The cu
11bd8 72 72 65 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 69  rrent location i
11bd9 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  n the journal fi
11bda 6c 65 20 69 73 20 67 69 76 65 6e 20 62 79 0a 2a  le is given by.*
11bdb 2a 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  * pPager->journa
11bdc 6c 4f 66 66 2e 20 53 65 65 20 63 6f 6d 6d 65 6e  lOff. See commen
11bdd 74 73 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f  ts above functio
11bde 6e 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64  n writeJournalHd
11bdf 72 28 29 20 66 6f 72 0a 2a 2a 20 61 20 64 65 73  r() for.** a des
11be0 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65 20  cription of the 
11be1 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66  journal header f
11be2 6f 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ormat..**.** If 
11be3 74 68 65 20 68 65 61 64 65 72 20 69 73 20 72 65  the header is re
11be4 61 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c  ad successfully,
11be5 20 2a 70 4e 52 65 63 20 69 73 20 73 65 74 20 74   *pNRec is set t
11be6 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a  o the number of.
11be7 2a 2a 20 70 61 67 65 20 72 65 63 6f 72 64 73 20  ** page records 
11be8 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 68  following this h
11be9 65 61 64 65 72 20 61 6e 64 20 2a 70 44 62 53 69  eader and *pDbSi
11bea 7a 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  ze is set to the
11beb 20 73 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20   size of the.** 
11bec 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 20  database before 
11bed 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
11bee 62 65 67 61 6e 2c 20 69 6e 20 70 61 67 65 73 2e  began, in pages.
11bef 20 41 6c 73 6f 2c 20 70 50 61 67 65 72 2d 3e 63   Also, pPager->c
11bf0 6b 73 75 6d 49 6e 69 74 0a 2a 2a 20 69 73 20 73  ksumInit.** is s
11bf1 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  et to the value 
11bf2 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f  read from the jo
11bf3 75 72 6e 61 6c 20 68 65 61 64 65 72 2e 20 53 51  urnal header. SQ
11bf4 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
11bf5 6e 65 64 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63  ned.** in this c
11bf6 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ase..**.** If th
11bf7 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  e journal header
11bf8 20 66 69 6c 65 20 61 70 70 65 61 72 73 20 74 6f   file appears to
11bf9 20 62 65 20 63 6f 72 72 75 70 74 65 64 2c 20 53   be corrupted, S
11bfa 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 0a 2a 2a  QLITE_DONE is.**
11bfb 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70   returned and *p
11bfc 4e 52 65 63 20 61 6e 64 20 2a 50 44 62 53 69 7a  NRec and *PDbSiz
11bfd 65 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 2e  e are undefined.
11bfe 20 20 49 66 20 4a 4f 55 52 4e 41 4c 5f 48 44 52    If JOURNAL_HDR
11bff 5f 53 5a 20 62 79 74 65 73 0a 2a 2a 20 63 61 6e  _SZ bytes.** can
11c00 6e 6f 74 20 62 65 20 72 65 61 64 20 66 72 6f 6d  not be read from
11c01 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
11c02 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  e an error code 
11c03 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
11c04 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64 4a  static int readJ
11c05 6f 75 72 6e 61 6c 48 64 72 28 0a 20 20 50 61 67  ournalHdr(.  Pag
11c06 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20 20  er *pPager,     
11c07 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
11c08 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69  er object */.  i
11c09 6e 74 20 69 73 48 6f 74 2c 0a 20 20 69 36 34 20  nt isHot,.  i64 
11c0a 6a 6f 75 72 6e 61 6c 53 69 7a 65 2c 20 20 20 20  journalSize,    
11c0b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
11c0c 20 6f 66 20 74 68 65 20 6f 70 65 6e 20 6a 6f 75   of the open jou
11c0d 72 6e 61 6c 20 66 69 6c 65 20 69 6e 20 62 79 74  rnal file in byt
11c0e 65 73 20 2a 2f 0a 20 20 75 33 32 20 2a 70 4e 52  es */.  u32 *pNR
11c0f 65 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ec,             
11c10 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 56 61 6c       /* OUT: Val
11c11 75 65 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65  ue read from the
11c12 20 6e 52 65 63 20 66 69 65 6c 64 20 2a 2f 0a 20   nRec field */. 
11c13 20 75 33 32 20 2a 70 44 62 53 69 7a 65 20 20 20   u32 *pDbSize   
11c14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11c15 20 4f 55 54 3a 20 56 61 6c 75 65 20 6f 66 20 6f   OUT: Value of o
11c16 72 69 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65  riginal database
11c17 20 73 69 7a 65 20 66 69 65 6c 64 20 2a 2f 0a 29   size field */.)
11c18 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  {.  int rc;     
11c19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11c1a 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
11c1b 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
11c1c 61 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20 20 20  ar aMagic[8];   
11c1d 20 20 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f    /* A buffer to
11c1e 20 68 6f 6c 64 20 74 68 65 20 6d 61 67 69 63 20   hold the magic 
11c1f 68 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20  header */.  i64 
11c20 69 48 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20  iHdrOff;        
11c21 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
11c22 65 74 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65  et of journal he
11c23 61 64 65 72 20 62 65 69 6e 67 20 72 65 61 64 20  ader being read 
11c24 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 73  */..  assert( is
11c25 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
11c26 29 20 29 3b 20 20 20 20 20 20 2f 2a 20 4a 6f 75  ) );      /* Jou
11c27 72 6e 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62  rnal file must b
11c28 65 20 6f 70 65 6e 2e 20 2a 2f 0a 0a 20 20 2f 2a  e open. */..  /*
11c29 20 41 64 76 61 6e 63 65 20 50 61 67 65 72 2e 6a   Advance Pager.j
11c2a 6f 75 72 6e 61 6c 4f 66 66 20 74 6f 20 74 68 65  ournalOff to the
11c2b 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 6e 65   start of the ne
11c2c 78 74 20 73 65 63 74 6f 72 2e 20 49 66 20 74 68  xt sector. If th
11c2d 65 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66  e.  ** journal f
11c2e 69 6c 65 20 69 73 20 74 6f 6f 20 73 6d 61 6c 6c  ile is too small
11c2f 20 66 6f 72 20 74 68 65 72 65 20 74 6f 20 62 65   for there to be
11c30 20 61 20 68 65 61 64 65 72 20 73 74 6f 72 65 64   a header stored
11c31 20 61 74 20 74 68 69 73 0a 20 20 2a 2a 20 70 6f   at this.  ** po
11c32 69 6e 74 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  int, return SQLI
11c33 54 45 5f 44 4f 4e 45 2e 0a 20 20 2a 2f 0a 20 20  TE_DONE..  */.  
11c34 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
11c35 66 66 20 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f  ff = journalHdrO
11c36 66 66 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20  ffset(pPager);. 
11c37 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75   if( pPager->jou
11c38 72 6e 61 6c 4f 66 66 2b 4a 4f 55 52 4e 41 4c 5f  rnalOff+JOURNAL_
11c39 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 3e  HDR_SZ(pPager) >
11c3a 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 20 29 7b 0a   journalSize ){.
11c3b 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
11c3c 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 48  E_DONE;.  }.  iH
11c3d 64 72 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e  drOff = pPager->
11c3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f  journalOff;..  /
11c3f 2a 20 52 65 61 64 20 69 6e 20 74 68 65 20 66 69  * Read in the fi
11c40 72 73 74 20 38 20 62 79 74 65 73 20 6f 66 20 74  rst 8 bytes of t
11c41 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  he journal heade
11c42 72 2e 20 49 66 20 74 68 65 79 20 64 6f 20 6e 6f  r. If they do no
11c43 74 20 6d 61 74 63 68 0a 20 20 2a 2a 20 74 68 65  t match.  ** the
11c44 20 20 6d 61 67 69 63 20 73 74 72 69 6e 67 20 66    magic string f
11c45 6f 75 6e 64 20 61 74 20 74 68 65 20 73 74 61 72  ound at the star
11c46 74 20 6f 66 20 65 61 63 68 20 6a 6f 75 72 6e 61  t of each journa
11c47 6c 20 68 65 61 64 65 72 2c 20 72 65 74 75 72 6e  l header, return
11c48 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 4f 4e  .  ** SQLITE_DON
11c49 45 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f  E. If an IO erro
11c4a 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e  r occurs, return
11c4b 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20   an error code. 
11c4c 4f 74 68 65 72 77 69 73 65 2c 0a 20 20 2a 2a 20  Otherwise,.  ** 
11c4d 70 72 6f 63 65 65 64 2e 0a 20 20 2a 2f 0a 20 20  proceed..  */.  
11c4e 69 66 28 20 69 73 48 6f 74 20 7c 7c 20 69 48 64  if( isHot || iHd
11c4f 72 4f 66 66 21 3d 70 50 61 67 65 72 2d 3e 6a 6f  rOff!=pPager->jo
11c50 75 72 6e 61 6c 48 64 72 20 29 7b 0a 20 20 20 20  urnalHdr ){.    
11c51 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65  rc = sqlite3OsRe
11c52 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  ad(pPager->jfd, 
11c53 61 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66 28 61  aMagic, sizeof(a
11c54 4d 61 67 69 63 29 2c 20 69 48 64 72 4f 66 66 29  Magic), iHdrOff)
11c55 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a  ;.    if( rc ){.
11c56 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
11c57 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d  .    }.    if( m
11c58 65 6d 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a  emcmp(aMagic, aJ
11c59 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a  ournalMagic, siz
11c5a 65 6f 66 28 61 4d 61 67 69 63 29 29 21 3d 30 20  eof(aMagic))!=0 
11c5b 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
11c5c 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20  SQLITE_DONE;.   
11c5d 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61   }.  }..  /* Rea
11c5e 64 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65  d the first thre
11c5f 65 20 33 32 2d 62 69 74 20 66 69 65 6c 64 73 20  e 32-bit fields 
11c60 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  of the journal h
11c61 65 61 64 65 72 3a 20 54 68 65 20 6e 52 65 63 0a  eader: The nRec.
11c62 20 20 2a 2a 20 66 69 65 6c 64 2c 20 74 68 65 20    ** field, the 
11c63 63 68 65 63 6b 73 75 6d 2d 69 6e 69 74 69 61 6c  checksum-initial
11c64 69 7a 65 72 20 61 6e 64 20 74 68 65 20 64 61 74  izer and the dat
11c65 61 62 61 73 65 20 73 69 7a 65 20 61 74 20 74 68  abase size at th
11c66 65 20 73 74 61 72 74 0a 20 20 2a 2a 20 6f 66 20  e start.  ** of 
11c67 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  the transaction.
11c68 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72   Return an error
11c69 20 63 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e   code if anythin
11c6a 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 20 20  g goes wrong..  
11c6b 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  */.  if( SQLITE_
11c6c 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64 33 32  OK!=(rc = read32
11c6d 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64  bits(pPager->jfd
11c6e 2c 20 69 48 64 72 4f 66 66 2b 38 2c 20 70 4e 52  , iHdrOff+8, pNR
11c6f 65 63 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54  ec)).   || SQLIT
11c70 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64  E_OK!=(rc = read
11c71 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a  32bits(pPager->j
11c72 66 64 2c 20 69 48 64 72 4f 66 66 2b 31 32 2c 20  fd, iHdrOff+12, 
11c73 26 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e  &pPager->cksumIn
11c74 69 74 29 29 0a 20 20 20 7c 7c 20 53 51 4c 49 54  it)).   || SQLIT
11c75 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64  E_OK!=(rc = read
11c76 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a  32bits(pPager->j
11c77 66 64 2c 20 69 48 64 72 4f 66 66 2b 31 36 2c 20  fd, iHdrOff+16, 
11c78 70 44 62 53 69 7a 65 29 29 0a 20 20 29 7b 0a 20  pDbSize)).  ){. 
11c79 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
11c7a 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  }..  if( pPager-
11c7b 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 30 20 29  >journalOff==0 )
11c7c 7b 0a 20 20 20 20 75 33 32 20 69 50 61 67 65 53  {.    u32 iPageS
11c7d 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
11c7e 20 20 20 2f 2a 20 50 61 67 65 2d 73 69 7a 65 20     /* Page-size 
11c7f 66 69 65 6c 64 20 6f 66 20 6a 6f 75 72 6e 61 6c  field of journal
11c80 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 75   header */.    u
11c81 33 32 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 20  32 iSectorSize; 
11c82 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
11c83 65 63 74 6f 72 2d 73 69 7a 65 20 66 69 65 6c 64  ector-size field
11c84 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64   of journal head
11c85 65 72 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 50  er */.    u16 iP
11c86 61 67 65 53 69 7a 65 31 36 3b 20 20 20 20 20 20  ageSize16;      
11c87 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f         /* Copy o
11c88 66 20 69 50 61 67 65 53 69 7a 65 20 69 6e 20 31  f iPageSize in 1
11c89 36 2d 62 69 74 20 76 61 72 69 61 62 6c 65 20 2a  6-bit variable *
11c8a 2f 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74  /..    /* Read t
11c8b 68 65 20 70 61 67 65 2d 73 69 7a 65 20 61 6e 64  he page-size and
11c8c 20 73 65 63 74 6f 72 2d 73 69 7a 65 20 6a 6f 75   sector-size jou
11c8d 72 6e 61 6c 20 68 65 61 64 65 72 20 66 69 65 6c  rnal header fiel
11c8e 64 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 53  ds. */.    if( S
11c8f 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20  QLITE_OK!=(rc = 
11c90 72 65 61 64 33 32 62 69 74 73 28 70 50 61 67 65  read32bits(pPage
11c91 72 2d 3e 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b  r->jfd, iHdrOff+
11c92 32 30 2c 20 26 69 53 65 63 74 6f 72 53 69 7a 65  20, &iSectorSize
11c93 29 29 0a 20 20 20 20 20 7c 7c 20 53 51 4c 49 54  )).     || SQLIT
11c94 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 72 65 61 64  E_OK!=(rc = read
11c95 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a  32bits(pPager->j
11c96 66 64 2c 20 69 48 64 72 4f 66 66 2b 32 34 2c 20  fd, iHdrOff+24, 
11c97 26 69 50 61 67 65 53 69 7a 65 29 29 0a 20 20 20  &iPageSize)).   
11c98 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
11c99 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20   rc;.    }..    
11c9a 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  /* Check that th
11c9b 65 20 76 61 6c 75 65 73 20 72 65 61 64 20 66 72  e values read fr
11c9c 6f 6d 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65  om the page-size
11c9d 20 61 6e 64 20 73 65 63 74 6f 72 2d 73 69 7a 65   and sector-size
11c9e 20 66 69 65 6c 64 73 0a 20 20 20 20 2a 2a 20 61   fields.    ** a
11c9f 72 65 20 77 69 74 68 69 6e 20 72 61 6e 67 65 2e  re within range.
11ca0 20 54 6f 20 62 65 20 27 69 6e 20 72 61 6e 67 65   To be 'in range
11ca1 27 2c 20 62 6f 74 68 20 76 61 6c 75 65 73 20 6e  ', both values n
11ca2 65 65 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65  eed to be a powe
11ca3 72 0a 20 20 20 20 2a 2a 20 6f 66 20 74 77 6f 20  r.    ** of two 
11ca4 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20  greater than or 
11ca5 65 71 75 61 6c 20 74 6f 20 35 31 32 2c 20 61 6e  equal to 512, an
11ca6 64 20 6e 6f 74 20 67 72 65 61 74 65 72 20 74 68  d not greater th
11ca7 61 6e 20 74 68 65 69 72 20 0a 20 20 20 20 2a 2a  an their .    **
11ca8 20 72 65 73 70 65 63 74 69 76 65 20 63 6f 6d 70   respective comp
11ca9 69 6c 65 20 74 69 6d 65 20 6d 61 78 69 6d 75 6d  ile time maximum
11caa 20 6c 69 6d 69 74 73 2e 0a 20 20 20 20 2a 2f 0a   limits..    */.
11cab 20 20 20 20 69 66 28 20 69 50 61 67 65 53 69 7a      if( iPageSiz
11cac 65 3c 35 31 32 20 20 20 20 20 20 20 20 20 20 20  e<512           
11cad 20 20 20 20 20 20 20 7c 7c 20 69 53 65 63 74 6f         || iSecto
11cae 72 53 69 7a 65 3c 35 31 32 0a 20 20 20 20 20 7c  rSize<512.     |
11caf 7c 20 69 50 61 67 65 53 69 7a 65 3e 53 51 4c 49  | iPageSize>SQLI
11cb0 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45  TE_MAX_PAGE_SIZE
11cb1 20 7c 7c 20 69 53 65 63 74 6f 72 53 69 7a 65 3e   || iSectorSize>
11cb2 4d 41 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 0a  MAX_SECTOR_SIZE.
11cb3 20 20 20 20 20 7c 7c 20 28 28 69 50 61 67 65 53       || ((iPageS
11cb4 69 7a 65 2d 31 29 26 69 50 61 67 65 53 69 7a 65  ize-1)&iPageSize
11cb5 29 21 3d 30 20 20 20 7c 7c 20 28 28 69 53 65 63  )!=0   || ((iSec
11cb6 74 6f 72 53 69 7a 65 2d 31 29 26 69 53 65 63 74  torSize-1)&iSect
11cb7 6f 72 53 69 7a 65 29 21 3d 30 20 0a 20 20 20 20  orSize)!=0 .    
11cb8 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  ){.      /* If t
11cb9 68 65 20 65 69 74 68 65 72 20 74 68 65 20 70 61  he either the pa
11cba 67 65 2d 73 69 7a 65 20 6f 72 20 73 65 63 74 6f  ge-size or secto
11cbb 72 2d 73 69 7a 65 20 69 6e 20 74 68 65 20 6a 6f  r-size in the jo
11cbc 75 72 6e 61 6c 2d 68 65 61 64 65 72 20 69 73 20  urnal-header is 
11cbd 0a 20 20 20 20 20 20 2a 2a 20 69 6e 76 61 6c 69  .      ** invali
11cbe 64 2c 20 74 68 65 6e 20 74 68 65 20 70 72 6f 63  d, then the proc
11cbf 65 73 73 20 74 68 61 74 20 77 72 6f 74 65 20 74  ess that wrote t
11cc0 68 65 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65  he journal-heade
11cc1 72 20 6d 75 73 74 20 68 61 76 65 20 0a 20 20 20  r must have .   
11cc2 20 20 20 2a 2a 20 63 72 61 73 68 65 64 20 62 65     ** crashed be
11cc3 66 6f 72 65 20 74 68 65 20 68 65 61 64 65 72 20  fore the header 
11cc4 77 61 73 20 73 79 6e 63 65 64 2e 20 49 6e 20 74  was synced. In t
11cc5 68 69 73 20 63 61 73 65 20 73 74 6f 70 20 72 65  his case stop re
11cc6 61 64 69 6e 67 20 0a 20 20 20 20 20 20 2a 2a 20  ading .      ** 
11cc7 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
11cc8 20 68 65 72 65 2e 0a 20 20 20 20 20 20 2a 2f 0a   here..      */.
11cc9 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
11cca 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a  ITE_DONE;.    }.
11ccb 0a 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74  .    /* Update t
11ccc 68 65 20 70 61 67 65 2d 73 69 7a 65 20 74 6f 20  he page-size to 
11ccd 6d 61 74 63 68 20 74 68 65 20 76 61 6c 75 65 20  match the value 
11cce 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f  read from the jo
11ccf 75 72 6e 61 6c 2e 20 0a 20 20 20 20 2a 2a 20 55  urnal. .    ** U
11cd0 73 65 20 61 20 74 65 73 74 63 61 73 65 28 29 20  se a testcase() 
11cd1 6d 61 63 72 6f 20 74 6f 20 6d 61 6b 65 20 73 75  macro to make su
11cd2 72 65 20 74 68 61 74 20 6d 61 6c 6c 6f 63 20 66  re that malloc f
11cd3 61 69 6c 75 72 65 20 77 69 74 68 69 6e 20 0a 20  ailure within . 
11cd4 20 20 20 2a 2a 20 50 61 67 65 72 53 65 74 50 61     ** PagerSetPa
11cd5 67 65 73 69 7a 65 28 29 20 69 73 20 74 65 73 74  gesize() is test
11cd6 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ed..    */.    i
11cd7 50 61 67 65 53 69 7a 65 31 36 20 3d 20 28 75 31  PageSize16 = (u1
11cd8 36 29 69 50 61 67 65 53 69 7a 65 3b 0a 20 20 20  6)iPageSize;.   
11cd9 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
11cda 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 50  erSetPagesize(pP
11cdb 61 67 65 72 2c 20 26 69 50 61 67 65 53 69 7a 65  ager, &iPageSize
11cdc 31 36 2c 20 2d 31 29 3b 0a 20 20 20 20 74 65 73  16, -1);.    tes
11cdd 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54  tcase( rc!=SQLIT
11cde 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 61 73 73 65  E_OK );.    asse
11cdf 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  rt( rc!=SQLITE_O
11ce0 4b 20 7c 7c 20 69 50 61 67 65 53 69 7a 65 31 36  K || iPageSize16
11ce1 3d 3d 28 75 31 36 29 69 50 61 67 65 53 69 7a 65  ==(u16)iPageSize
11ce2 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61   );..    /* Upda
11ce3 74 65 20 74 68 65 20 61 73 73 75 6d 65 64 20 73  te the assumed s
11ce4 65 63 74 6f 72 2d 73 69 7a 65 20 74 6f 20 6d 61  ector-size to ma
11ce5 74 63 68 20 74 68 65 20 76 61 6c 75 65 20 75 73  tch the value us
11ce6 65 64 20 62 79 20 0a 20 20 20 20 2a 2a 20 74 68  ed by .    ** th
11ce7 65 20 70 72 6f 63 65 73 73 20 74 68 61 74 20 63  e process that c
11ce8 72 65 61 74 65 64 20 74 68 69 73 20 6a 6f 75 72  reated this jour
11ce9 6e 61 6c 2e 20 49 66 20 74 68 69 73 20 6a 6f 75  nal. If this jou
11cea 72 6e 61 6c 20 77 61 73 0a 20 20 20 20 2a 2a 20  rnal was.    ** 
11ceb 63 72 65 61 74 65 64 20 62 79 20 61 20 70 72 6f  created by a pro
11cec 63 65 73 73 20 6f 74 68 65 72 20 74 68 61 6e 20  cess other than 
11ced 74 68 69 73 20 6f 6e 65 2c 20 74 68 65 6e 20 74  this one, then t
11cee 68 69 73 20 72 6f 75 74 69 6e 65 0a 20 20 20 20  his routine.    
11cef 2a 2a 20 69 73 20 62 65 69 6e 67 20 63 61 6c 6c  ** is being call
11cf0 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 70  ed from within p
11cf1 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29 2e  ager_playback().
11cf2 20 54 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65   The local value
11cf3 0a 20 20 20 20 2a 2a 20 6f 66 20 50 61 67 65 72  .    ** of Pager
11cf4 2e 73 65 63 74 6f 72 53 69 7a 65 20 69 73 20 72  .sectorSize is r
11cf5 65 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 65  estored at the e
11cf6 6e 64 20 6f 66 20 74 68 61 74 20 72 6f 75 74 69  nd of that routi
11cf7 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  ne..    */.    p
11cf8 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a  Pager->sectorSiz
11cf9 65 20 3d 20 69 53 65 63 74 6f 72 53 69 7a 65 3b  e = iSectorSize;
11cfa 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65 72 2d 3e  .  }..  pPager->
11cfb 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 4a 4f  journalOff += JO
11cfc 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
11cfd 67 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ger);.  return r
11cfe 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69  c;.}.../*.** Wri
11cff 74 65 20 74 68 65 20 73 75 70 70 6c 69 65 64 20  te the supplied 
11d00 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e  master journal n
11d01 61 6d 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75  ame into the jou
11d02 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61  rnal file for pa
11d03 67 65 72 0a 2a 2a 20 70 50 61 67 65 72 20 61 74  ger.** pPager at
11d04 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63   the current loc
11d05 61 74 69 6f 6e 2e 20 54 68 65 20 6d 61 73 74 65  ation. The maste
11d06 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 6d  r journal name m
11d07 75 73 74 20 62 65 20 74 68 65 20 6c 61 73 74 0a  ust be the last.
11d08 2a 2a 20 74 68 69 6e 67 20 77 72 69 74 74 65 6e  ** thing written
11d09 20 74 6f 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69   to a journal fi
11d0a 6c 65 2e 20 49 66 20 74 68 65 20 70 61 67 65 72  le. If the pager
11d0b 20 69 73 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63   is in full-sync
11d0c 20 6d 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 6a 6f   mode, the.** jo
11d0d 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72  urnal file descr
11d0e 69 70 74 6f 72 20 69 73 20 61 64 76 61 6e 63 65  iptor is advance
11d0f 64 20 74 6f 20 74 68 65 20 6e 65 78 74 20 73 65  d to the next se
11d10 63 74 6f 72 20 62 6f 75 6e 64 61 72 79 20 62 65  ctor boundary be
11d11 66 6f 72 65 0a 2a 2a 20 61 6e 79 74 68 69 6e 67  fore.** anything
11d12 20 69 73 20 77 72 69 74 74 65 6e 2e 20 54 68 65   is written. The
11d13 20 66 6f 72 6d 61 74 20 69 73 3a 0a 2a 2a 0a 2a   format is:.**.*
11d14 2a 20 20 20 2b 20 34 20 62 79 74 65 73 3a 20 50  *   + 4 bytes: P
11d15 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 2e 0a 2a 2a  AGER_MJ_PGNO..**
11d16 20 20 20 2b 20 4e 20 62 79 74 65 73 3a 20 4d 61     + N bytes: Ma
11d17 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
11d18 65 6e 61 6d 65 20 69 6e 20 75 74 66 2d 38 2e 0a  ename in utf-8..
11d19 2a 2a 20 20 20 2b 20 34 20 62 79 74 65 73 3a 20  **   + 4 bytes: 
11d1a 4e 20 28 6c 65 6e 67 74 68 20 6f 66 20 6d 61 73  N (length of mas
11d1b 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65  ter journal name
11d1c 20 69 6e 20 62 79 74 65 73 2c 20 6e 6f 20 6e 75   in bytes, no nu
11d1d 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 29 2e 0a 2a  l-terminator)..*
11d1e 2a 20 20 20 2b 20 34 20 62 79 74 65 73 3a 20 4d  *   + 4 bytes: M
11d1f 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61  aster journal na
11d20 6d 65 20 63 68 65 63 6b 73 75 6d 2e 0a 2a 2a 20  me checksum..** 
11d21 20 20 2b 20 38 20 62 79 74 65 73 3a 20 61 4a 6f    + 8 bytes: aJo
11d22 75 72 6e 61 6c 4d 61 67 69 63 5b 5d 2e 0a 2a 2a  urnalMagic[]..**
11d23 0a 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 6a  .** The master j
11d24 6f 75 72 6e 61 6c 20 70 61 67 65 20 63 68 65 63  ournal page chec
11d25 6b 73 75 6d 20 69 73 20 74 68 65 20 73 75 6d 20  ksum is the sum 
11d26 6f 66 20 74 68 65 20 62 79 74 65 73 20 69 6e 20  of the bytes in 
11d27 74 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f  the master.** jo
11d28 75 72 6e 61 6c 20 6e 61 6d 65 2c 20 77 68 65 72  urnal name, wher
11d29 65 20 65 61 63 68 20 62 79 74 65 20 69 73 20 69  e each byte is i
11d2a 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61 20  nterpreted as a 
11d2b 73 69 67 6e 65 64 20 38 2d 62 69 74 20 69 6e 74  signed 8-bit int
11d2c 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a  eger..**.** If z
11d2d 4d 61 73 74 65 72 20 69 73 20 61 20 4e 55 4c 4c  Master is a NULL
11d2e 20 70 6f 69 6e 74 65 72 20 28 6f 63 63 75 72 73   pointer (occurs
11d2f 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 64 61   for a single da
11d30 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 69  tabase transacti
11d31 6f 6e 29 2c 20 0a 2a 2a 20 74 68 69 73 20 63 61  on), .** this ca
11d32 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ll is a no-op..*
11d33 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72 69  /.static int wri
11d34 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28  teMasterJournal(
11d35 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 63  Pager *pPager, c
11d36 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74  onst char *zMast
11d37 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20  er){.  int rc;  
11d38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11d39 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
11d3a 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20  n code */.  int 
11d3b 6e 4d 61 73 74 65 72 3b 20 20 20 20 20 20 20 20  nMaster;        
11d3c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
11d3d 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 69 6e 67  Length of string
11d3e 20 7a 4d 61 73 74 65 72 20 2a 2f 0a 20 20 69 36   zMaster */.  i6
11d3f 34 20 69 48 64 72 4f 66 66 3b 20 20 20 20 20 20  4 iHdrOff;      
11d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11d41 2a 20 4f 66 66 73 65 74 20 6f 66 20 68 65 61 64  * Offset of head
11d42 65 72 20 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69  er in journal fi
11d43 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6a 72 6e 6c  le */.  i64 jrnl
11d44 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20  Size;           
11d45 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
11d46 20 6f 66 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65   of journal file
11d47 20 6f 6e 20 64 69 73 6b 20 2a 2f 0a 20 20 75 33   on disk */.  u3
11d48 32 20 63 6b 73 75 6d 20 3d 20 30 3b 20 20 20 20  2 cksum = 0;    
11d49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
11d4a 2a 20 43 68 65 63 6b 73 75 6d 20 6f 66 20 73 74  * Checksum of st
11d4b 72 69 6e 67 20 7a 4d 61 73 74 65 72 20 2a 2f 0a  ring zMaster */.
11d4c 0a 20 20 69 66 28 20 21 7a 4d 61 73 74 65 72 20  .  if( !zMaster 
11d4d 7c 7c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61  || pPager->setMa
11d4e 73 74 65 72 0a 20 20 20 7c 7c 20 70 50 61 67 65  ster.   || pPage
11d4f 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d  r->journalMode==
11d50 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
11d51 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 7c 7c 20  E_MEMORY .   || 
11d52 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
11d53 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
11d54 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 29 7b  ALMODE_OFF .  ){
11d55 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
11d56 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 50 61  TE_OK;.  }.  pPa
11d57 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d  ger->setMaster =
11d58 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20 69 73   1;.  assert( is
11d59 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
11d5a 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 61 6c 63 75  ) );..  /* Calcu
11d5b 6c 61 74 65 20 74 68 65 20 6c 65 6e 67 74 68 20  late the length 
11d5c 69 6e 20 62 79 74 65 73 20 61 6e 64 20 74 68 65  in bytes and the
11d5d 20 63 68 65 63 6b 73 75 6d 20 6f 66 20 7a 4d 61   checksum of zMa
11d5e 73 74 65 72 20 2a 2f 0a 20 20 66 6f 72 28 6e 4d  ster */.  for(nM
11d5f 61 73 74 65 72 3d 30 3b 20 7a 4d 61 73 74 65 72  aster=0; zMaster
11d60 5b 6e 4d 61 73 74 65 72 5d 3b 20 6e 4d 61 73 74  [nMaster]; nMast
11d61 65 72 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d  er++){.    cksum
11d62 20 2b 3d 20 7a 4d 61 73 74 65 72 5b 6e 4d 61 73   += zMaster[nMas
11d63 74 65 72 5d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ter];.  }..  /* 
11d64 49 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20  If in full-sync 
11d65 6d 6f 64 65 2c 20 61 64 76 61 6e 63 65 20 74 6f  mode, advance to
11d66 20 74 68 65 20 6e 65 78 74 20 64 69 73 6b 20 73   the next disk s
11d67 65 63 74 6f 72 20 62 65 66 6f 72 65 20 77 72 69  ector before wri
11d68 74 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6d 61  ting.  ** the ma
11d69 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d  ster journal nam
11d6a 65 2e 20 54 68 69 73 20 69 73 20 69 6e 20 63 61  e. This is in ca
11d6b 73 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  se the previous 
11d6c 70 61 67 65 20 77 72 69 74 74 65 6e 20 74 6f 0a  page written to.
11d6d 20 20 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c    ** the journal
11d6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
11d6f 6e 20 73 79 6e 63 65 64 2e 0a 20 20 2a 2f 0a 20  n synced..  */. 
11d70 20 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75 6c   if( pPager->ful
11d71 6c 53 79 6e 63 20 29 7b 0a 20 20 20 20 70 50 61  lSync ){.    pPa
11d72 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
11d73 3d 20 6a 6f 75 72 6e 61 6c 48 64 72 4f 66 66 73  = journalHdrOffs
11d74 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a  et(pPager);.  }.
11d75 20 20 69 48 64 72 4f 66 66 20 3d 20 70 50 61 67    iHdrOff = pPag
11d76 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a  er->journalOff;.
11d77 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20  .  /* Write the 
11d78 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 64  master journal d
11d79 61 74 61 20 74 6f 20 74 68 65 20 65 6e 64 20 6f  ata to the end o
11d7a 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
11d7b 6c 65 2e 20 49 66 0a 20 20 2a 2a 20 61 6e 20 65  le. If.  ** an e
11d7c 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74  rror occurs, ret
11d7d 75 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f  urn the error co
11d7e 64 65 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  de to the caller
11d7f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 30 20  ..  */.  if( (0 
11d80 21 3d 20 28 72 63 20 3d 20 77 72 69 74 65 33 32  != (rc = write32
11d81 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64  bits(pPager->jfd
11d82 2c 20 69 48 64 72 4f 66 66 2c 20 50 41 47 45 52  , iHdrOff, PAGER
11d83 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29  _MJ_PGNO(pPager)
11d84 29 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20  ))).   || (0 != 
11d85 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57  (rc = sqlite3OsW
11d86 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  rite(pPager->jfd
11d87 2c 20 7a 4d 61 73 74 65 72 2c 20 6e 4d 61 73 74  , zMaster, nMast
11d88 65 72 2c 20 69 48 64 72 4f 66 66 2b 34 29 29 29  er, iHdrOff+4)))
11d89 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28 72 63  .   || (0 != (rc
11d8a 20 3d 20 77 72 69 74 65 33 32 62 69 74 73 28 70   = write32bits(p
11d8b 50 61 67 65 72 2d 3e 6a 66 64 2c 20 69 48 64 72  Pager->jfd, iHdr
11d8c 4f 66 66 2b 34 2b 6e 4d 61 73 74 65 72 2c 20 6e  Off+4+nMaster, n
11d8d 4d 61 73 74 65 72 29 29 29 0a 20 20 20 7c 7c 20  Master))).   || 
11d8e 28 30 20 21 3d 20 28 72 63 20 3d 20 77 72 69 74  (0 != (rc = writ
11d8f 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e  e32bits(pPager->
11d90 6a 66 64 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e  jfd, iHdrOff+4+n
11d91 4d 61 73 74 65 72 2b 34 2c 20 63 6b 73 75 6d 29  Master+4, cksum)
11d92 29 29 0a 20 20 20 7c 7c 20 28 30 20 21 3d 20 28  )).   || (0 != (
11d93 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72  rc = sqlite3OsWr
11d94 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ite(pPager->jfd,
11d95 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20   aJournalMagic, 
11d96 38 2c 20 69 48 64 72 4f 66 66 2b 34 2b 6e 4d 61  8, iHdrOff+4+nMa
11d97 73 74 65 72 2b 38 29 29 29 0a 20 20 29 7b 0a 20  ster+8))).  ){. 
11d98 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
11d99 7d 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  }.  pPager->jour
11d9a 6e 61 6c 4f 66 66 20 2b 3d 20 28 6e 4d 61 73 74  nalOff += (nMast
11d9b 65 72 2b 32 30 29 3b 0a 20 20 70 50 61 67 65 72  er+20);.  pPager
11d9c 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 21 70 50  ->needSync = !pP
11d9d 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 0a 20  ager->noSync;.. 
11d9e 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 72   /* If the pager
11d9f 20 69 73 20 69 6e 20 70 65 72 69 73 74 65 6e 74   is in peristent
11da0 2d 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 2c 20 74  -journal mode, t
11da1 68 65 6e 20 74 68 65 20 70 68 79 73 69 63 61 6c  hen the physical
11da2 20 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 66   .  ** journal-f
11da3 69 6c 65 20 6d 61 79 20 65 78 74 65 6e 64 20 70  ile may extend p
11da4 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74  ast the end of t
11da5 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61  he master-journa
11da6 6c 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 6e 64 20  l name.  ** and 
11da7 38 20 62 79 74 65 73 20 6f 66 20 6d 61 67 69 63  8 bytes of magic
11da8 20 64 61 74 61 20 6a 75 73 74 20 77 72 69 74 74   data just writt
11da9 65 6e 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 20  en to the file. 
11daa 54 68 69 73 20 69 73 20 0a 20 20 2a 2a 20 64 61  This is .  ** da
11dab 6e 67 65 72 6f 75 73 20 62 65 63 61 75 73 65 20  ngerous because 
11dac 74 68 65 20 63 6f 64 65 20 74 6f 20 72 6f 6c 6c  the code to roll
11dad 62 61 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e  back a hot-journ
11dae 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20 77 69 6c  al file.  ** wil
11daf 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f  l not be able to
11db0 20 66 69 6e 64 20 74 68 65 20 6d 61 73 74 65 72   find the master
11db1 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 74 6f  -journal name to
11db2 20 64 65 74 65 72 6d 69 6e 65 20 0a 20 20 2a 2a   determine .  **
11db3 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
11db4 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 68  the journal is h
11db5 6f 74 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45  ot. .  **.  ** E
11db6 61 73 69 65 73 74 20 74 68 69 6e 67 20 74 6f 20  asiest thing to 
11db7 64 6f 20 69 6e 20 74 68 69 73 20 73 63 65 6e 61  do in this scena
11db8 72 69 6f 20 69 73 20 74 6f 20 74 72 75 6e 63 61  rio is to trunca
11db9 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a  te the journal .
11dba 20 20 2a 2a 20 66 69 6c 65 20 74 6f 20 74 68 65    ** file to the
11dbb 20 72 65 71 75 69 72 65 64 20 73 69 7a 65 2e 0a   required size..
11dbc 20 20 2a 2f 20 0a 20 20 69 66 28 20 53 51 4c 49    */ .  if( SQLI
11dbd 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c  TE_OK==(rc = sql
11dbe 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70  ite3OsFileSize(p
11dbf 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 6a 72 6e  Pager->jfd, &jrn
11dc0 6c 53 69 7a 65 29 29 0a 20 20 20 26 26 20 6a 72  lSize)).   && jr
11dc1 6e 6c 53 69 7a 65 3e 70 50 61 67 65 72 2d 3e 6a  nlSize>pPager->j
11dc2 6f 75 72 6e 61 6c 4f 66 66 0a 20 20 29 7b 0a 20  ournalOff.  ){. 
11dc3 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
11dc4 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65 72  sTruncate(pPager
11dc5 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a  ->jfd, pPager->j
11dc6 6f 75 72 6e 61 6c 4f 66 66 29 3b 0a 20 20 7d 0a  ournalOff);.  }.
11dc7 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
11dc8 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61 20 70 61 67  /*.** Find a pag
11dc9 65 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61  e in the hash ta
11dca 62 6c 65 20 67 69 76 65 6e 20 69 74 73 20 70 61  ble given its pa
11dcb 67 65 20 6e 75 6d 62 65 72 2e 20 52 65 74 75 72  ge number. Retur
11dcc 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74  n.** a pointer t
11dcd 6f 20 74 68 65 20 70 61 67 65 20 6f 72 20 4e 55  o the page or NU
11dce 4c 4c 20 69 66 20 74 68 65 20 72 65 71 75 65 73  LL if the reques
11dcf 74 65 64 20 70 61 67 65 20 69 73 20 6e 6f 74 20  ted page is not 
11dd0 0a 2a 2a 20 61 6c 72 65 61 64 79 20 69 6e 20 6d  .** already in m
11dd1 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  emory..*/.static
11dd2 20 50 67 48 64 72 20 2a 70 61 67 65 72 5f 6c 6f   PgHdr *pager_lo
11dd3 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61 67  okup(Pager *pPag
11dd4 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a  er, Pgno pgno){.
11dd5 20 20 50 67 48 64 72 20 2a 70 3b 20 20 20 20 20    PgHdr *p;     
11dd6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11dd7 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 76 61      /* Return va
11dd8 6c 75 65 20 2a 2f 0a 0a 20 20 2f 2a 20 49 74 20  lue */..  /* It 
11dd9 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
11dda 66 6f 72 20 61 20 63 61 6c 6c 20 74 6f 20 50 63  for a call to Pc
11ddb 61 63 68 65 46 65 74 63 68 28 29 20 77 69 74 68  acheFetch() with
11ddc 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 30 20 74   createFlag==0 t
11ddd 6f 0a 20 20 2a 2a 20 66 61 69 6c 2c 20 73 69 6e  o.  ** fail, sin
11dde 63 65 20 6e 6f 20 61 74 74 65 6d 70 74 20 74 6f  ce no attempt to
11ddf 20 61 6c 6c 6f 63 61 74 65 20 64 79 6e 61 6d 69   allocate dynami
11de0 63 20 6d 65 6d 6f 72 79 20 77 69 6c 6c 20 62 65  c memory will be
11de1 20 6d 61 64 65 2e 0a 20 20 2a 2f 0a 20 20 28 76   made..  */.  (v
11de2 6f 69 64 29 73 71 6c 69 74 65 33 50 63 61 63 68  oid)sqlite3Pcach
11de3 65 46 65 74 63 68 28 70 50 61 67 65 72 2d 3e 70  eFetch(pPager->p
11de4 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 30 2c  PCache, pgno, 0,
11de5 20 26 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70   &p);.  return p
11de6 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 65 73  ;.}../*.** Unles
11de7 73 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69  s the pager is i
11de8 6e 20 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 64  n error-state, d
11de9 69 73 63 61 72 64 20 61 6c 6c 20 69 6e 2d 6d 65  iscard all in-me
11dea 6d 6f 72 79 20 70 61 67 65 73 2e 20 49 66 0a 2a  mory pages. If.*
11deb 2a 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69  * the pager is i
11dec 6e 20 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 74  n error-state, t
11ded 68 65 6e 20 74 68 69 73 20 63 61 6c 6c 20 69 73  hen this call is
11dee 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20   a no-op..**.** 
11def 54 4f 44 4f 3a 20 57 68 79 20 63 61 6e 20 77 65  TODO: Why can we
11df0 20 6e 6f 74 20 72 65 73 65 74 20 74 68 65 20 70   not reset the p
11df1 61 67 65 72 20 77 68 69 6c 65 20 69 6e 20 65 72  ager while in er
11df2 72 6f 72 20 73 74 61 74 65 3f 0a 2a 2f 0a 73 74  ror state?.*/.st
11df3 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f  atic void pager_
11df4 72 65 73 65 74 28 50 61 67 65 72 20 2a 70 50 61  reset(Pager *pPa
11df5 67 65 72 29 7b 0a 20 20 69 66 28 20 53 51 4c 49  ger){.  if( SQLI
11df6 54 45 5f 4f 4b 3d 3d 70 50 61 67 65 72 2d 3e 65  TE_OK==pPager->e
11df7 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 73 71  rrCode ){.    sq
11df8 6c 69 74 65 33 42 61 63 6b 75 70 52 65 73 74 61  lite3BackupResta
11df9 72 74 28 70 50 61 67 65 72 2d 3e 70 42 61 63 6b  rt(pPager->pBack
11dfa 75 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  up);.    sqlite3
11dfb 50 63 61 63 68 65 43 6c 65 61 72 28 70 50 61 67  PcacheClear(pPag
11dfc 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20  er->pPCache);.  
11dfd 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
11dfe 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 0a 7d  Valid = 0;.  }.}
11dff 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6c 6c  ../*.** Free all
11e00 20 73 74 72 75 63 74 75 72 65 73 20 69 6e 20 74   structures in t
11e01 68 65 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f  he Pager.aSavepo
11e02 69 6e 74 5b 5d 20 61 72 72 61 79 20 61 6e 64 20  int[] array and 
11e03 73 65 74 20 62 6f 74 68 0a 2a 2a 20 50 61 67 65  set both.** Page
11e04 72 2e 61 53 61 76 65 70 6f 69 6e 74 20 61 6e 64  r.aSavepoint and
11e05 20 50 61 67 65 72 2e 6e 53 61 76 65 70 6f 69 6e   Pager.nSavepoin
11e06 74 20 74 6f 20 7a 65 72 6f 2e 20 43 6c 6f 73 65  t to zero. Close
11e07 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c   the sub-journal
11e08 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 6f 70 65  .** if it is ope
11e09 6e 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 20  n and the pager 
11e0a 69 73 20 6e 6f 74 20 69 6e 20 65 78 63 6c 75 73  is not in exclus
11e0b 69 76 65 20 6d 6f 64 65 2e 0a 2a 2f 0a 73 74 61  ive mode..*/.sta
11e0c 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65  tic void release
11e0d 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 50 61  AllSavepoints(Pa
11e0e 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
11e0f 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20  int ii;         
11e10 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f        /* Iterato
11e11 72 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68  r for looping th
11e12 72 6f 75 67 68 20 50 61 67 65 72 2e 61 53 61 76  rough Pager.aSav
11e13 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 66 6f 72 28  epoint */.  for(
11e14 69 69 3d 30 3b 20 69 69 3c 70 50 61 67 65 72 2d  ii=0; ii<pPager-
11e15 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b  >nSavepoint; ii+
11e16 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  +){.    sqlite3B
11e17 69 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61  itvecDestroy(pPa
11e18 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b  ger->aSavepoint[
11e19 69 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74  ii].pInSavepoint
11e1a 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 50  );.  }.  if( !pP
11e1b 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d  ager->exclusiveM
11e1c 6f 64 65 20 7c 7c 20 73 71 6c 69 74 65 33 49 73  ode || sqlite3Is
11e1d 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65  MemJournal(pPage
11e1e 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20 20  r->sjfd) ){.    
11e1f 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70  sqlite3OsClose(p
11e20 50 61 67 65 72 2d 3e 73 6a 66 64 29 3b 0a 20 20  Pager->sjfd);.  
11e21 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  }.  sqlite3_free
11e22 28 70 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f  (pPager->aSavepo
11e23 69 6e 74 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  int);.  pPager->
11e24 61 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a  aSavepoint = 0;.
11e25 20 20 70 50 61 67 65 72 2d 3e 6e 53 61 76 65 70    pPager->nSavep
11e26 6f 69 6e 74 20 3d 20 30 3b 0a 20 20 70 50 61 67  oint = 0;.  pPag
11e27 65 72 2d 3e 6e 53 75 62 52 65 63 20 3d 20 30 3b  er->nSubRec = 0;
11e28 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
11e29 65 20 62 69 74 20 6e 75 6d 62 65 72 20 70 67 6e  e bit number pgn
11e2a 6f 20 69 6e 20 74 68 65 20 50 61 67 65 72 53 61  o in the PagerSa
11e2b 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70  vepoint.pInSavep
11e2c 6f 69 6e 74 20 0a 2a 2a 20 62 69 74 76 65 63 73  oint .** bitvecs
11e2d 20 6f 66 20 61 6c 6c 20 6f 70 65 6e 20 73 61 76   of all open sav
11e2e 65 70 6f 69 6e 74 73 2e 20 52 65 74 75 72 6e 20  epoints. Return 
11e2f 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 73 75 63  SQLITE_OK if suc
11e30 63 65 73 73 66 75 6c 0a 2a 2a 20 6f 72 20 53 51  cessful.** or SQ
11e31 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20  LITE_NOMEM if a 
11e32 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 6f  malloc failure o
11e33 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  ccurs..*/.static
11e34 20 69 6e 74 20 61 64 64 54 6f 53 61 76 65 70 6f   int addToSavepo
11e35 69 6e 74 42 69 74 76 65 63 73 28 50 61 67 65 72  intBitvecs(Pager
11e36 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f 20 70   *pPager, Pgno p
11e37 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 69 69 3b 20  gno){.  int ii; 
11e38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
11e39 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
11e3a 72 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20  r */.  int rc = 
11e3b 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
11e3c 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20   /* Result code 
11e3d 2a 2f 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  */..  for(ii=0; 
11e3e 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 61 76 65  ii<pPager->nSave
11e3f 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20  point; ii++){.  
11e40 20 20 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74    PagerSavepoint
11e41 20 2a 70 20 3d 20 26 70 50 61 67 65 72 2d 3e 61   *p = &pPager->a
11e42 53 61 76 65 70 6f 69 6e 74 5b 69 69 5d 3b 0a 20  Savepoint[ii];. 
11e43 20 20 20 69 66 28 20 70 67 6e 6f 3c 3d 70 2d 3e     if( pgno<=p->
11e44 6e 4f 72 69 67 20 29 7b 0a 20 20 20 20 20 20 72  nOrig ){.      r
11e45 63 20 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 76  c |= sqlite3Bitv
11e46 65 63 53 65 74 28 70 2d 3e 70 49 6e 53 61 76 65  ecSet(p->pInSave
11e47 70 6f 69 6e 74 2c 20 70 67 6e 6f 29 3b 0a 20 20  point, pgno);.  
11e48 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63      testcase( rc
11e49 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
11e4a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
11e4b 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
11e4c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
11e4d 4d 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  M );.    }.  }. 
11e4e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
11e4f 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20 74 68 65 20  *.** Unlock the 
11e50 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54  database file. T
11e51 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
11e52 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 70  a no-op if the p
11e53 61 67 65 72 0a 2a 2a 20 69 73 20 69 6e 20 65 78  ager.** is in ex
11e54 63 6c 75 73 69 76 65 20 6d 6f 64 65 2e 0a 2a 2a  clusive mode..**
11e55 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65 72  .** If the pager
11e56 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e   is currently in
11e57 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20 64 69   error state, di
11e58 73 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e  scard the conten
11e59 74 73 20 6f 66 20 0a 2a 2a 20 74 68 65 20 63 61  ts of .** the ca
11e5a 63 68 65 20 61 6e 64 20 72 65 73 65 74 20 74 68  che and reset th
11e5b 65 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72  e Pager structur
11e5c 65 20 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65  e internal state
11e5d 2e 20 49 66 20 74 68 65 72 65 20 69 73 0a 2a 2a  . If there is.**
11e5e 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c   an open journal
11e5f 2d 66 69 6c 65 2c 20 74 68 65 6e 20 74 68 65 20  -file, then the 
11e60 6e 65 78 74 20 74 69 6d 65 20 61 20 73 68 61 72  next time a shar
11e61 65 64 2d 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69  ed-lock is obtai
11e62 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 70 61  ned.** on the pa
11e63 67 65 72 20 66 69 6c 65 20 28 62 79 20 74 68 69  ger file (by thi
11e64 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 70  s or any other p
11e65 72 6f 63 65 73 73 29 2c 20 69 74 20 77 69 6c 6c  rocess), it will
11e66 20 62 65 0a 2a 2a 20 74 72 65 61 74 65 64 20 61   be.** treated a
11e67 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  s a hot-journal 
11e68 61 6e 64 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e  and rolled back.
11e69 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
11e6a 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 50 61 67  pager_unlock(Pag
11e6b 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69  er *pPager){.  i
11e6c 66 28 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c  f( !pPager->excl
11e6d 75 73 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20  usiveMode ){.   
11e6e 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
11e6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
11e70 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
11e71 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 63  .    /* Always c
11e72 6c 6f 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  lose the journal
11e73 20 66 69 6c 65 20 77 68 65 6e 20 64 72 6f 70 70   file when dropp
11e74 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65  ing the database
11e75 20 6c 6f 63 6b 2e 0a 20 20 20 20 2a 2a 20 4f 74   lock..    ** Ot
11e76 68 65 72 77 69 73 65 2c 20 61 6e 6f 74 68 65 72  herwise, another
11e77 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69 74 68   connection with
11e78 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 64 65   journal_mode=de
11e79 6c 65 74 65 20 6d 69 67 68 74 0a 20 20 20 20 2a  lete might.    *
11e7a 2a 20 64 65 6c 65 74 65 20 74 68 65 20 66 69 6c  * delete the fil
11e7b 65 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72  e out from under
11e7c 20 75 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20   us..    */.    
11e7d 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70  sqlite3OsClose(p
11e7e 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20  Pager->jfd);.   
11e7f 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44 65   sqlite3BitvecDe
11e80 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70 49  stroy(pPager->pI
11e81 6e 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 70  nJournal);.    p
11e82 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
11e83 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 6c 65 61  l = 0;.    relea
11e84 73 65 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28  seAllSavepoints(
11e85 70 50 61 67 65 72 29 3b 0a 0a 20 20 20 20 2f 2a  pPager);..    /*
11e86 20 49 66 20 74 68 65 20 66 69 6c 65 20 69 73 20   If the file is 
11e87 75 6e 6c 6f 63 6b 65 64 2c 20 73 6f 6d 65 62 6f  unlocked, somebo
11e88 64 79 20 65 6c 73 65 20 6d 69 67 68 74 20 63 68  dy else might ch
11e89 61 6e 67 65 20 69 74 2e 20 54 68 65 0a 20 20 20  ange it. The.   
11e8a 20 2a 2a 20 76 61 6c 75 65 73 20 73 74 6f 72 65   ** values store
11e8b 64 20 69 6e 20 50 61 67 65 72 2e 64 62 53 69 7a  d in Pager.dbSiz
11e8c 65 20 65 74 63 2e 20 6d 69 67 68 74 20 62 65 63  e etc. might bec
11e8d 6f 6d 65 20 69 6e 76 61 6c 69 64 20 69 66 0a 20  ome invalid if. 
11e8e 20 20 20 2a 2a 20 74 68 69 73 20 68 61 70 70 65     ** this happe
11e8f 6e 73 2e 20 54 4f 44 4f 3a 20 52 65 61 6c 6c 79  ns. TODO: Really
11e90 2c 20 74 68 69 73 20 64 6f 65 73 6e 27 74 20 6e  , this doesn't n
11e91 65 65 64 20 74 6f 20 62 65 20 63 6c 65 61 72 65  eed to be cleare
11e92 64 0a 20 20 20 20 2a 2a 20 75 6e 74 69 6c 20 74  d.    ** until t
11e93 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65  he change-counte
11e94 72 20 63 68 65 63 6b 20 66 61 69 6c 73 20 69 6e  r check fails in
11e95 20 50 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b   PagerSharedLock
11e96 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  ()..    */.    p
11e97 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c  Pager->dbSizeVal
11e98 69 64 20 3d 20 30 3b 0a 0a 20 20 20 20 72 63 20  id = 0;..    rc 
11e99 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61 67 65  = osUnlock(pPage
11e9a 72 2d 3e 66 64 2c 20 4e 4f 5f 4c 4f 43 4b 29 3b  r->fd, NO_LOCK);
11e9b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20  .    if( rc ){. 
11e9c 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72       pPager->err
11e9d 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 20 20 7d  Code = rc;.    }
11e9e 0a 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 55  .    IOTRACE(("U
11e9f 4e 4c 4f 43 4b 20 25 70 5c 6e 22 2c 20 70 50 61  NLOCK %p\n", pPa
11ea0 67 65 72 29 29 0a 0a 20 20 20 20 2f 2a 20 49 66  ger))..    /* If
11ea1 20 50 61 67 65 72 2e 65 72 72 43 6f 64 65 20 69   Pager.errCode i
11ea2 73 20 73 65 74 2c 20 74 68 65 20 63 6f 6e 74 65  s set, the conte
11ea3 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 72  nts of the pager
11ea4 20 63 61 63 68 65 20 63 61 6e 6e 6f 74 20 62 65   cache cannot be
11ea5 0a 20 20 20 20 2a 2a 20 74 72 75 73 74 65 64 2e  .    ** trusted.
11ea6 20 4e 6f 77 20 74 68 61 74 20 74 68 65 20 70 61   Now that the pa
11ea7 67 65 72 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f  ger file is unlo
11ea8 63 6b 65 64 2c 20 74 68 65 20 63 6f 6e 74 65 6e  cked, the conten
11ea9 74 73 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a  ts of the.    **
11eaa 20 63 61 63 68 65 20 63 61 6e 20 62 65 20 64 69   cache can be di
11eab 73 63 61 72 64 65 64 20 61 6e 64 20 74 68 65 20  scarded and the 
11eac 65 72 72 6f 72 20 63 6f 64 65 20 73 61 66 65 6c  error code safel
11ead 79 20 63 6c 65 61 72 65 64 2e 0a 20 20 20 20 2a  y cleared..    *
11eae 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72  /.    if( pPager
11eaf 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20  ->errCode ){.   
11eb0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
11eb1 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
11eb2 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
11eb3 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
11eb4 20 20 20 7d 0a 20 20 20 20 20 20 70 61 67 65 72     }.      pager
11eb5 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a  _reset(pPager);.
11eb6 20 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65      }..    pPage
11eb7 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e 74 44 6f  r->changeCountDo
11eb8 6e 65 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67  ne = 0;.    pPag
11eb9 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45  er->state = PAGE
11eba 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 7d 0a 7d 0a  R_UNLOCK;.  }.}.
11ebb 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
11ebc 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 63  tion should be c
11ebd 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 49 4f  alled when an IO
11ebe 45 52 52 2c 20 43 4f 52 52 55 50 54 20 6f 72 20  ERR, CORRUPT or 
11ebf 46 55 4c 4c 20 65 72 72 6f 72 0a 2a 2a 20 6d 61  FULL error.** ma
11ec0 79 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 2e  y have occurred.
11ec1 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
11ec2 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ent is a pointer
11ec3 20 74 6f 20 74 68 65 20 70 61 67 65 72 20 0a 2a   to the pager .*
11ec4 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65  * structure, the
11ec5 20 73 65 63 6f 6e 64 20 74 68 65 20 65 72 72 6f   second the erro
11ec6 72 2d 63 6f 64 65 20 61 62 6f 75 74 20 74 6f 20  r-code about to 
11ec7 62 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 61  be returned by a
11ec8 20 70 61 67 65 72 20 0a 2a 2a 20 41 50 49 20 66   pager .** API f
11ec9 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 76 61 6c  unction. The val
11eca 75 65 20 72 65 74 75 72 6e 65 64 20 69 73 20 61  ue returned is a
11ecb 20 63 6f 70 79 20 6f 66 20 74 68 65 20 73 65 63   copy of the sec
11ecc 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a  ond argument .**
11ecd 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
11ece 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  n. .**.** If the
11ecf 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
11ed0 20 69 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52   is SQLITE_IOERR
11ed1 2c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  , SQLITE_CORRUPT
11ed2 2c 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c  , or SQLITE_FULL
11ed3 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 62 65  .** the error be
11ed4 63 6f 6d 65 73 20 70 65 72 73 69 73 74 65 6e 74  comes persistent
11ed5 2e 20 55 6e 74 69 6c 20 74 68 65 20 70 65 72 73  . Until the pers
11ed6 69 73 74 65 6e 20 65 72 72 6f 72 20 69 73 20 63  isten error is c
11ed7 6c 65 61 72 65 64 2c 0a 2a 2a 20 73 75 62 73 65  leared,.** subse
11ed8 71 75 65 6e 74 20 41 50 49 20 63 61 6c 6c 73 20  quent API calls 
11ed9 6f 6e 20 74 68 69 73 20 50 61 67 65 72 20 77 69  on this Pager wi
11eda 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 72  ll immediately r
11edb 65 74 75 72 6e 20 74 68 65 20 73 61 6d 65 20 0a  eturn the same .
11edc 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a  ** error code..*
11edd 2a 0a 2a 2a 20 41 20 70 65 72 73 69 73 74 65 6e  *.** A persisten
11ede 74 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 65  t error indicate
11edf 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65  s that the conte
11ee0 6e 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 72  nts of the pager
11ee1 2d 63 61 63 68 65 20 0a 2a 2a 20 63 61 6e 6e 6f  -cache .** canno
11ee2 74 20 62 65 20 74 72 75 73 74 65 64 2e 20 54 68  t be trusted. Th
11ee3 69 73 20 73 74 61 74 65 20 63 61 6e 20 62 65 20  is state can be 
11ee4 63 6c 65 61 72 65 64 20 62 79 20 63 6f 6d 70 6c  cleared by compl
11ee5 65 74 65 6c 79 20 64 69 73 63 61 72 64 69 6e 67  etely discarding
11ee6 20 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74   .** the content
11ee7 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63  s of the pager-c
11ee8 61 63 68 65 2e 20 49 66 20 61 20 74 72 61 6e 73  ache. If a trans
11ee9 61 63 74 69 6f 6e 20 77 61 73 20 61 63 74 69 76  action was activ
11eea 65 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 65  e when.** the pe
11eeb 72 73 69 73 74 65 6e 74 20 65 72 72 6f 72 20 6f  rsistent error o
11eec 63 63 75 72 72 65 64 2c 20 74 68 65 6e 20 74 68  ccurred, then th
11eed 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  e rollback journ
11eee 61 6c 20 6d 61 79 20 6e 65 65 64 0a 2a 2a 20 74  al may need.** t
11eef 6f 20 62 65 20 72 65 70 6c 61 79 65 64 20 74 6f  o be replayed to
11ef0 20 72 65 73 74 6f 72 65 20 74 68 65 20 63 6f 6e   restore the con
11ef1 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64 61 74  tents of the dat
11ef2 61 62 61 73 65 20 66 69 6c 65 20 28 61 73 20 69  abase file (as i
11ef3 66 0a 2a 2a 20 69 74 20 77 65 72 65 20 61 20 68  f.** it were a h
11ef4 6f 74 2d 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2f 0a  ot-journal)..*/.
11ef5 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
11ef6 5f 65 72 72 6f 72 28 50 61 67 65 72 20 2a 70 50  _error(Pager *pP
11ef7 61 67 65 72 2c 20 69 6e 74 20 72 63 29 7b 0a 20  ager, int rc){. 
11ef8 20 69 6e 74 20 72 63 32 20 3d 20 72 63 20 26 20   int rc2 = rc & 
11ef9 30 78 66 66 3b 0a 20 20 61 73 73 65 72 74 28 20  0xff;.  assert( 
11efa 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
11efb 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 61 73 73   !MEMDB );.  ass
11efc 65 72 74 28 0a 20 20 20 20 20 20 20 70 50 61 67  ert(.       pPag
11efd 65 72 2d 3e 65 72 72 43 6f 64 65 3d 3d 53 51 4c  er->errCode==SQL
11efe 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a 20 20 20 20  ITE_FULL ||.    
11eff 20 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f     pPager->errCo
11f00 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  de==SQLITE_OK ||
11f01 0a 20 20 20 20 20 20 20 28 70 50 61 67 65 72 2d  .       (pPager-
11f02 3e 65 72 72 43 6f 64 65 20 26 20 30 78 66 66 29  >errCode & 0xff)
11f03 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 0a 20  ==SQLITE_IOERR. 
11f04 20 29 3b 0a 20 20 69 66 28 20 72 63 32 3d 3d 53   );.  if( rc2==S
11f05 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 20 72 63  QLITE_FULL || rc
11f06 32 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 20  2==SQLITE_IOERR 
11f07 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65  ){.    pPager->e
11f08 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 7d  rrCode = rc;.  }
11f09 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
11f0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 61  ./*.** Execute a
11f0b 20 72 6f 6c 6c 62 61 63 6b 20 69 66 20 61 20 74   rollback if a t
11f0c 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63  ransaction is ac
11f0d 74 69 76 65 20 61 6e 64 20 75 6e 6c 6f 63 6b 20  tive and unlock 
11f0e 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65  the .** database
11f0f 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49 66   file. .**.** If
11f10 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 61   the pager has a
11f11 6c 72 65 61 64 79 20 65 6e 74 65 72 65 64 20 74  lready entered t
11f12 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20  he error state, 
11f13 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 0a  do not attempt .
11f14 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  ** the rollback 
11f15 61 74 20 74 68 69 73 20 74 69 6d 65 2e 20 49 6e  at this time. In
11f16 73 74 65 61 64 2c 20 70 61 67 65 72 5f 75 6e 6c  stead, pager_unl
11f17 6f 63 6b 28 29 20 69 73 20 63 61 6c 6c 65 64 2e  ock() is called.
11f18 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20   The.** call to 
11f19 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 77  pager_unlock() w
11f1a 69 6c 6c 20 64 69 73 63 61 72 64 20 61 6c 6c 20  ill discard all 
11f1b 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 2c  in-memory pages,
11f1c 20 75 6e 6c 6f 63 6b 0a 2a 2a 20 74 68 65 20 64   unlock.** the d
11f1d 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64  atabase file and
11f1e 20 63 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72   clear the error
11f1f 20 73 74 61 74 65 2e 20 49 66 20 74 68 69 73 20   state. If this 
11f20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68  means that.** th
11f21 65 72 65 20 69 73 20 61 20 68 6f 74 2d 6a 6f 75  ere is a hot-jou
11f22 72 6e 61 6c 20 6c 65 66 74 20 69 6e 20 74 68 65  rnal left in the
11f23 20 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20 74 68   file-system, th
11f24 65 20 6e 65 78 74 20 63 6f 6e 6e 65 63 74 69 6f  e next connectio
11f25 6e 0a 2a 2a 20 74 6f 20 6f 62 74 61 69 6e 20 61  n.** to obtain a
11f26 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 6f 6e 20   shared lock on 
11f27 74 68 65 20 70 61 67 65 72 20 28 77 68 69 63 68  the pager (which
11f28 20 6d 61 79 20 62 65 20 74 68 69 73 20 6f 6e 65   may be this one
11f29 29 20 77 69 6c 6c 0a 2a 2a 20 72 6f 6c 6c 20 69  ) will.** roll i
11f2a 74 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66  t back..**.** If
11f2b 20 74 68 65 20 70 61 67 65 72 20 68 61 73 20 6e   the pager has n
11f2c 6f 74 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72  ot already enter
11f2d 65 64 20 74 68 65 20 65 72 72 6f 72 20 73 74 61  ed the error sta
11f2e 74 65 2c 20 62 75 74 20 61 6e 20 49 4f 20 6f 72  te, but an IO or
11f2f 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72  .** malloc error
11f30 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 61   occurs during a
11f31 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 6e 20   rollback, then 
11f32 74 68 69 73 20 77 69 6c 6c 20 69 74 73 65 6c 66  this will itself
11f33 20 63 61 75 73 65 20 0a 2a 2a 20 74 68 65 20 70   cause .** the p
11f34 61 67 65 72 20 74 6f 20 65 6e 74 65 72 20 74 68  ager to enter th
11f35 65 20 65 72 72 6f 72 20 73 74 61 74 65 2e 20 57  e error state. W
11f36 68 69 63 68 20 77 69 6c 6c 20 62 65 20 63 6c 65  hich will be cle
11f37 61 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63  ared by the.** c
11f38 61 6c 6c 20 74 6f 20 70 61 67 65 72 5f 75 6e 6c  all to pager_unl
11f39 6f 63 6b 28 29 2c 20 61 73 20 64 65 73 63 72 69  ock(), as descri
11f3a 62 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74  bed above..*/.st
11f3b 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72 55  atic void pagerU
11f3c 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b  nlockAndRollback
11f3d 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
11f3e 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65  .  if( pPager->e
11f3f 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f  rrCode==SQLITE_O
11f40 4b 20 26 26 20 70 50 61 67 65 72 2d 3e 73 74 61  K && pPager->sta
11f41 74 65 3e 3d 50 41 47 45 52 5f 52 45 53 45 52 56  te>=PAGER_RESERV
11f42 45 44 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ED ){.    sqlite
11f43 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c  3BeginBenignMall
11f44 6f 63 28 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  oc();.    sqlite
11f45 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 70  3PagerRollback(p
11f46 50 61 67 65 72 29 3b 0a 20 20 20 20 73 71 6c 69  Pager);.    sqli
11f47 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
11f48 6f 63 28 29 3b 0a 20 20 7d 0a 20 20 70 61 67 65  oc();.  }.  page
11f49 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29  r_unlock(pPager)
11f4a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
11f4b 72 6f 75 74 69 6e 65 20 65 6e 64 73 20 61 20 74  routine ends a t
11f4c 72 61 6e 73 61 63 74 69 6f 6e 2e 20 41 20 74 72  ransaction. A tr
11f4d 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 75 73 75  ansaction is usu
11f4e 61 6c 6c 79 20 65 6e 64 65 64 20 62 79 20 0a 2a  ally ended by .*
11f4f 2a 20 65 69 74 68 65 72 20 61 20 43 4f 4d 4d 49  * either a COMMI
11f50 54 20 6f 72 20 61 20 52 4f 4c 4c 42 41 43 4b 20  T or a ROLLBACK 
11f51 6f 70 65 72 61 74 69 6f 6e 2e 20 54 68 69 73 20  operation. This 
11f52 72 6f 75 74 69 6e 65 20 6d 61 79 20 62 65 20 63  routine may be c
11f53 61 6c 6c 65 64 20 0a 2a 2a 20 61 66 74 65 72 20  alled .** after 
11f54 72 6f 6c 6c 62 61 63 6b 20 6f 66 20 61 20 68 6f  rollback of a ho
11f55 74 2d 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 69 66  t-journal, or if
11f56 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
11f57 20 77 68 69 6c 65 20 6f 70 65 6e 69 6e 67 0a 2a   while opening.*
11f58 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  * the journal fi
11f59 6c 65 20 6f 72 20 77 72 69 74 69 6e 67 20 74 68  le or writing th
11f5a 65 20 76 65 72 79 20 66 69 72 73 74 20 6a 6f 75  e very first jou
11f5b 72 6e 61 6c 2d 68 65 61 64 65 72 20 6f 66 20 61  rnal-header of a
11f5c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 74 72 61  .** database tra
11f5d 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 0a 2a 2a  nsaction..** .**
11f5e 20 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73   If the pager is
11f5f 20 69 6e 20 50 41 47 45 52 5f 53 48 41 52 45 44   in PAGER_SHARED
11f60 20 6f 72 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b   or PAGER_UNLOCK
11f61 20 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73   state when this
11f62 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 63  .** routine is c
11f63 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e  alled, it is a n
11f64 6f 2d 6f 70 20 28 72 65 74 75 72 6e 73 20 53 51  o-op (returns SQ
11f65 4c 49 54 45 5f 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20  LITE_OK)..**.** 
11f66 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 79 20 61  Otherwise, any a
11f67 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74 73  ctive savepoints
11f68 20 61 72 65 20 72 65 6c 65 61 73 65 64 2e 0a 2a   are released..*
11f69 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72  *.** If the jour
11f6a 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e  nal file is open
11f6b 2c 20 74 68 65 6e 20 69 74 20 69 73 20 22 66 69  , then it is "fi
11f6c 6e 61 6c 69 7a 65 64 22 2e 20 4f 6e 63 65 20 61  nalized". Once a
11f6d 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 66 69 6c   journal .** fil
11f6e 65 20 68 61 73 20 62 65 65 6e 20 66 69 6e 61 6c  e has been final
11f6f 69 7a 65 64 20 69 74 20 69 73 20 6e 6f 74 20 70  ized it is not p
11f70 6f 73 73 69 62 6c 65 20 74 6f 20 75 73 65 20 69  ossible to use i
11f71 74 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20 61  t to roll back a
11f72 20 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e   .** transaction
11f73 2e 20 4e 6f 72 20 77 69 6c 6c 20 69 74 20 62 65  . Nor will it be
11f74 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f 20 62   considered to b
11f75 65 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20  e a hot-journal 
11f76 62 79 20 74 68 69 73 0a 2a 2a 20 6f 72 20 61 6e  by this.** or an
11f77 79 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65  y other database
11f78 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 45 78 61   connection. Exa
11f79 63 74 6c 79 20 68 6f 77 20 61 20 6a 6f 75 72 6e  ctly how a journ
11f7a 61 6c 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 0a  al is finalized.
11f7b 2a 2a 20 64 65 70 65 6e 64 73 20 6f 6e 20 77 68  ** depends on wh
11f7c 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
11f7d 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e   pager is runnin
11f7e 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20 6d  g in exclusive m
11f7f 6f 64 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20 63  ode and.** the c
11f80 75 72 72 65 6e 74 20 6a 6f 75 72 6e 61 6c 2d 6d  urrent journal-m
11f81 6f 64 65 20 28 50 61 67 65 72 2e 6a 6f 75 72 6e  ode (Pager.journ
11f82 61 6c 4d 6f 64 65 20 76 61 6c 75 65 29 2c 20 61  alMode value), a
11f83 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
11f84 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d     journalMode==
11f85 4d 45 4d 4f 52 59 0a 2a 2a 20 20 20 20 20 4a 6f  MEMORY.**     Jo
11f86 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72  urnal file descr
11f87 69 70 74 6f 72 20 69 73 20 73 69 6d 70 6c 79 20  iptor is simply 
11f88 63 6c 6f 73 65 64 2e 20 54 68 69 73 20 64 65 73  closed. This des
11f89 74 72 6f 79 73 20 61 6e 20 0a 2a 2a 20 20 20 20  troys an .**    
11f8a 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e   in-memory journ
11f8b 61 6c 2e 0a 2a 2a 0a 2a 2a 20 20 20 6a 6f 75 72  al..**.**   jour
11f8c 6e 61 6c 4d 6f 64 65 3d 3d 54 52 55 4e 43 41 54  nalMode==TRUNCAT
11f8d 45 0a 2a 2a 20 20 20 20 20 4a 6f 75 72 6e 61 6c  E.**     Journal
11f8e 20 66 69 6c 65 20 69 73 20 74 72 75 6e 63 61 74   file is truncat
11f8f 65 64 20 74 6f 20 7a 65 72 6f 20 62 79 74 65 73  ed to zero bytes
11f90 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20   in size..**.** 
11f91 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50    journalMode==P
11f92 45 52 53 49 53 54 0a 2a 2a 20 20 20 20 20 54 68  ERSIST.**     Th
11f93 65 20 66 69 72 73 74 20 32 38 20 62 79 74 65 73  e first 28 bytes
11f94 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   of the journal 
11f95 66 69 6c 65 20 61 72 65 20 7a 65 72 6f 65 64 2e  file are zeroed.
11f96 20 54 68 69 73 20 69 6e 76 61 6c 69 64 61 74 65   This invalidate
11f97 73 0a 2a 2a 20 20 20 20 20 74 68 65 20 66 69 72  s.**     the fir
11f98 73 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  st journal heade
11f99 72 20 69 6e 20 74 68 65 20 66 69 6c 65 2c 20 61  r in the file, a
11f9a 6e 64 20 68 65 6e 63 65 20 74 68 65 20 65 6e 74  nd hence the ent
11f9b 69 72 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20  ire journal.**  
11f9c 20 20 20 66 69 6c 65 2e 20 41 6e 20 69 6e 76 61     file. An inva
11f9d 6c 69 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  lid journal file
11f9e 20 63 61 6e 6e 6f 74 20 62 65 20 72 6f 6c 6c 65   cannot be rolle
11f9f 64 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20  d back..**.**   
11fa0 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 44 45 4c  journalMode==DEL
11fa1 45 54 45 0a 2a 2a 20 20 20 20 20 54 68 65 20 6a  ETE.**     The j
11fa2 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 63  ournal file is c
11fa3 6c 6f 73 65 64 20 61 6e 64 20 64 65 6c 65 74 65  losed and delete
11fa4 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f  d using sqlite3O
11fa5 73 44 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a  sDelete()..**.**
11fa6 20 20 20 20 20 49 66 20 74 68 65 20 70 61 67 65       If the page
11fa7 72 20 69 73 20 72 75 6e 6e 69 6e 67 20 69 6e 20  r is running in 
11fa8 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65 2c 20  exclusive mode, 
11fa9 74 68 69 73 20 6d 65 74 68 6f 64 20 6f 66 20 66  this method of f
11faa 69 6e 61 6c 69 7a 69 6e 67 0a 2a 2a 20 20 20 20  inalizing.**    
11fab 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
11fac 65 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 2e  e is never used.
11fad 20 49 6e 73 74 65 61 64 2c 20 69 66 20 74 68 65   Instead, if the
11fae 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 69 73 0a   journalMode is.
11faf 2a 2a 20 20 20 20 20 44 45 4c 45 54 45 20 61 6e  **     DELETE an
11fb0 64 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69  d the pager is i
11fb1 6e 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65  n exclusive mode
11fb2 2c 20 74 68 65 20 6d 65 74 68 6f 64 20 64 65 73  , the method des
11fb3 63 72 69 62 65 64 20 75 6e 64 65 72 0a 2a 2a 20  cribed under.** 
11fb4 20 20 20 20 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d      journalMode=
11fb5 3d 50 45 52 53 49 53 54 20 69 73 20 75 73 65 64  =PERSIST is used
11fb6 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a 2a 20   instead..**.** 
11fb7 41 66 74 65 72 20 74 68 65 20 6a 6f 75 72 6e 61  After the journa
11fb8 6c 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 2c 20  l is finalized, 
11fb9 69 66 20 72 75 6e 6e 69 6e 67 20 69 6e 20 6e 6f  if running in no
11fba 6e 2d 65 78 63 6c 75 73 69 76 65 20 6d 6f 64 65  n-exclusive mode
11fbb 2c 20 74 68 65 0a 2a 2a 20 70 61 67 65 72 20 6d  , the.** pager m
11fbc 6f 76 65 73 20 74 6f 20 50 41 47 45 52 5f 53 48  oves to PAGER_SH
11fbd 41 52 45 44 20 73 74 61 74 65 20 28 61 6e 64 20  ARED state (and 
11fbe 64 6f 77 6e 67 72 61 64 65 73 20 74 68 65 20 6c  downgrades the l
11fbf 6f 63 6b 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 61  ock on the.** da
11fc0 74 61 62 61 73 65 20 66 69 6c 65 20 61 63 63 6f  tabase file acco
11fc1 72 64 69 6e 67 6c 79 29 2e 0a 2a 2a 0a 2a 2a 20  rdingly)..**.** 
11fc2 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20  If the pager is 
11fc3 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75  running in exclu
11fc4 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20 69 73  sive mode and is
11fc5 20 69 6e 20 50 41 47 45 52 5f 53 59 4e 43 45 44   in PAGER_SYNCED
11fc6 20 73 74 61 74 65 2c 0a 2a 2a 20 69 74 20 6d 6f   state,.** it mo
11fc7 76 65 73 20 74 6f 20 50 41 47 45 52 5f 45 58 43  ves to PAGER_EXC
11fc8 4c 55 53 49 56 45 2e 20 4e 6f 20 6c 6f 63 6b 73  LUSIVE. No locks
11fc9 20 61 72 65 20 64 6f 77 6e 67 72 61 64 65 64 20   are downgraded 
11fca 77 68 65 6e 20 72 75 6e 6e 69 6e 67 20 69 6e 0a  when running in.
11fcb 2a 2a 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64  ** exclusive mod
11fcc 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f  e..**.** SQLITE_
11fcd 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  OK is returned i
11fce 66 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63 75 72  f no error occur
11fcf 73 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  s. If an error o
11fd0 63 63 75 72 73 20 64 75 72 69 6e 67 0a 2a 2a 20  ccurs during.** 
11fd1 61 6e 79 20 6f 66 20 74 68 65 20 49 4f 20 6f 70  any of the IO op
11fd2 65 72 61 74 69 6f 6e 73 20 74 6f 20 66 69 6e 61  erations to fina
11fd3 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  lize the journal
11fd4 20 66 69 6c 65 20 6f 72 20 75 6e 6c 6f 63 6b 20   file or unlock 
11fd5 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
11fd6 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72 6f  then the IO erro
11fd7 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
11fd8 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20  ed to the user. 
11fd9 49 66 20 74 68 65 20 0a 2a 2a 20 6f 70 65 72 61  If the .** opera
11fda 74 69 6f 6e 20 74 6f 20 66 69 6e 61 6c 69 7a 65  tion to finalize
11fdb 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
11fdc 65 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 74 68  e fails, then th
11fdd 65 20 63 6f 64 65 20 73 74 69 6c 6c 0a 2a 2a 20  e code still.** 
11fde 74 72 69 65 73 20 74 6f 20 75 6e 6c 6f 63 6b 20  tries to unlock 
11fdf 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
11fe0 65 20 69 66 20 6e 6f 74 20 69 6e 20 65 78 63 6c  e if not in excl
11fe1 75 73 69 76 65 20 6d 6f 64 65 2e 20 49 66 20 74  usive mode. If t
11fe2 68 65 0a 2a 2a 20 75 6e 6c 6f 63 6b 20 6f 70 65  he.** unlock ope
11fe3 72 61 74 69 6f 6e 20 66 61 69 6c 73 20 61 73 20  ration fails as 
11fe4 77 65 6c 6c 2c 20 74 68 65 6e 20 74 68 65 20 66  well, then the f
11fe5 69 72 73 74 20 65 72 72 6f 72 20 63 6f 64 65 20  irst error code 
11fe6 72 65 6c 61 74 65 64 0a 2a 2a 20 74 6f 20 74 68  related.** to th
11fe7 65 20 66 69 72 73 74 20 65 72 72 6f 72 20 65 6e  e first error en
11fe8 63 6f 75 6e 74 65 72 65 64 20 28 74 68 65 20 6a  countered (the j
11fe9 6f 75 72 6e 61 6c 20 66 69 6e 61 6c 69 7a 61 74  ournal finalizat
11fea 69 6f 6e 20 6f 6e 65 29 20 69 73 0a 2a 2a 20 72  ion one) is.** r
11feb 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74  eturned..*/.stat
11fec 69 63 20 69 6e 74 20 70 61 67 65 72 5f 65 6e 64  ic int pager_end
11fed 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 67  _transaction(Pag
11fee 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
11fef 68 61 73 4d 61 73 74 65 72 29 7b 0a 20 20 69 6e  hasMaster){.  in
11ff0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
11ff1 3b 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20  ;      /* Error 
11ff2 63 6f 64 65 20 66 72 6f 6d 20 6a 6f 75 72 6e 61  code from journa
11ff3 6c 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 6f  l finalization o
11ff4 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  peration */.  in
11ff5 74 20 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f  t rc2 = SQLITE_O
11ff6 4b 3b 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20  K;     /* Error 
11ff7 63 6f 64 65 20 66 72 6f 6d 20 64 62 20 66 69 6c  code from db fil
11ff8 65 20 75 6e 6c 6f 63 6b 20 6f 70 65 72 61 74 69  e unlock operati
11ff9 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 50 61  on */..  if( pPa
11ffa 67 65 72 2d 3e 73 74 61 74 65 3c 50 41 47 45 52  ger->state<PAGER
11ffb 5f 52 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20  _RESERVED ){.   
11ffc 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
11ffd 4b 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65  K;.  }.  release
11ffe 41 6c 6c 53 61 76 65 70 6f 69 6e 74 73 28 70 50  AllSavepoints(pP
11fff 61 67 65 72 29 3b 0a 0a 20 20 61 73 73 65 72 74  ager);..  assert
12000 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
12001 3e 6a 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d  >jfd) || pPager-
12002 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29  >pInJournal==0 )
12003 3b 0a 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70  ;.  if( isOpen(p
12004 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 0a  Pager->jfd) ){..
12005 20 20 20 20 2f 2a 20 46 69 6e 61 6c 69 7a 65 20      /* Finalize 
12006 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
12007 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c  . */.    if( sql
12008 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c  ite3IsMemJournal
12009 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b  (pPager->jfd) ){
1200a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1200b 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
1200c 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
1200d 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b 0a  LMODE_MEMORY );.
1200e 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43        sqlite3OsC
1200f 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  lose(pPager->jfd
12010 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  );.    }else if(
12011 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
12012 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
12013 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45  NALMODE_TRUNCATE
12014 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50   ){.      if( pP
12015 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
12016 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
12017 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
12018 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
12019 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1201a 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65  OsTruncate(pPage
1201b 72 2d 3e 6a 66 64 2c 20 30 29 3b 0a 20 20 20 20  r->jfd, 0);.    
1201c 20 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72    }.      pPager
1201d 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30  ->journalOff = 0
1201e 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  ;.      pPager->
1201f 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d  journalStarted =
12020 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   0;.    }else if
12021 28 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73  ( pPager->exclus
12022 69 76 65 4d 6f 64 65 20 0a 20 20 20 20 20 7c 7c  iveMode .     ||
12023 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
12024 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
12025 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a  NALMODE_PERSIST.
12026 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20      ){.      rc 
12027 3d 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64 72  = zeroJournalHdr
12028 28 70 50 61 67 65 72 2c 20 68 61 73 4d 61 73 74  (pPager, hasMast
12029 65 72 29 3b 0a 20 20 20 20 20 20 70 61 67 65 72  er);.      pager
1202a 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72  _error(pPager, r
1202b 63 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72  c);.      pPager
1202c 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20 30  ->journalOff = 0
1202d 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  ;.      pPager->
1202e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d  journalStarted =
1202f 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   0;.    }else{. 
12030 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 72 61       /* This bra
12031 6e 63 68 20 6d 61 79 20 62 65 20 65 78 65 63 75  nch may be execu
12032 74 65 64 20 77 69 74 68 20 50 61 67 65 72 2e 6a  ted with Pager.j
12033 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 4d 45 4d 4f  ournalMode==MEMO
12034 52 59 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 61  RY if.      ** a
12035 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 77 61 73   hot-journal was
12036 20 6a 75 73 74 20 72 6f 6c 6c 65 64 20 62 61 63   just rolled bac
12037 6b 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  k. In this case 
12038 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20  the journal.    
12039 20 20 2a 2a 20 66 69 6c 65 20 73 68 6f 75 6c 64    ** file should
1203a 20 62 65 20 63 6c 6f 73 65 64 20 61 6e 64 20 64   be closed and d
1203b 65 6c 65 74 65 64 2e 20 49 66 20 74 68 69 73 20  eleted. If this 
1203c 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 72 69 74 65  connection write
1203d 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68  s to.      ** th
1203e 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
1203f 20 69 74 20 77 69 6c 6c 20 64 6f 20 73 6f 20 75   it will do so u
12040 73 69 6e 67 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  sing an in-memor
12041 79 20 6a 6f 75 72 6e 61 6c 2e 20 20 2a 2f 0a 20  y journal.  */. 
12042 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
12043 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
12044 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
12045 4f 44 45 5f 44 45 4c 45 54 45 20 0a 20 20 20 20  ODE_DELETE .    
12046 20 20 20 20 20 20 20 7c 7c 20 70 50 61 67 65 72         || pPager
12047 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50  ->journalMode==P
12048 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45  AGER_JOURNALMODE
12049 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20 20 20 29  _MEMORY .      )
1204a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  ;.      sqlite3O
1204b 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a  sClose(pPager->j
1204c 66 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  fd);.      if( !
1204d 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
1204e 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
1204f 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65   sqlite3OsDelete
12050 28 70 50 61 67 65 72 2d 3e 70 56 66 73 2c 20 70  (pPager->pVfs, p
12051 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c  Pager->zJournal,
12052 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
12053 20 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54   }..#ifdef SQLIT
12054 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20  E_CHECK_PAGES.  
12055 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49    sqlite3PcacheI
12056 74 65 72 61 74 65 44 69 72 74 79 28 70 50 61 67  terateDirty(pPag
12057 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67  er->pPCache, pag
12058 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 29  er_set_pagehash)
12059 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 73 71  ;.#endif..    sq
1205a 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e  lite3PcacheClean
1205b 41 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61  All(pPager->pPCa
1205c 63 68 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  che);.    sqlite
1205d 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70  3BitvecDestroy(p
1205e 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
1205f 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  l);.    pPager->
12060 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a  pInJournal = 0;.
12061 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63      pPager->nRec
12062 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28   = 0;.  }..  if(
12063 20 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73   !pPager->exclus
12064 69 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 72  iveMode ){.    r
12065 63 32 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50  c2 = osUnlock(pP
12066 61 67 65 72 2d 3e 66 64 2c 20 53 48 41 52 45 44  ager->fd, SHARED
12067 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 70 50 61 67  _LOCK);.    pPag
12068 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45  er->state = PAGE
12069 52 5f 53 48 41 52 45 44 3b 0a 20 20 20 20 70 50  R_SHARED;.    pP
1206a 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e  ager->changeCoun
1206b 74 44 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d 65 6c  tDone = 0;.  }el
1206c 73 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  se if( pPager->s
1206d 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43  tate==PAGER_SYNC
1206e 45 44 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72  ED ){.    pPager
1206f 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f  ->state = PAGER_
12070 45 58 43 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 20  EXCLUSIVE;.  }. 
12071 20 70 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74   pPager->setMast
12072 65 72 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72  er = 0;.  pPager
12073 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a  ->needSync = 0;.
12074 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69    pPager->dbModi
12075 66 69 65 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20  fied = 0;..  /* 
12076 54 4f 44 4f 3a 20 49 73 20 74 68 69 73 20 6f 70  TODO: Is this op
12077 74 69 6d 61 6c 3f 20 57 68 79 20 69 73 20 74 68  timal? Why is th
12078 65 20 64 62 20 73 69 7a 65 20 69 6e 76 61 6c 69  e db size invali
12079 64 61 74 65 64 20 68 65 72 65 20 0a 20 20 2a 2a  dated here .  **
1207a 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 62 61   when the databa
1207b 73 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 75  se file is not u
1207c 6e 6c 6f 63 6b 65 64 3f 20 2a 2f 0a 20 20 70 50  nlocked? */.  pP
1207d 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65  ager->dbOrigSize
1207e 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 50   = 0;.  sqlite3P
1207f 63 61 63 68 65 54 72 75 6e 63 61 74 65 28 70 50  cacheTruncate(pP
12080 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70  ager->pPCache, p
12081 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a  Pager->dbSize);.
12082 20 20 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a    if( !MEMDB ){.
12083 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69      pPager->dbSi
12084 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d  zeValid = 0;.  }
12085 0a 0a 20 20 72 65 74 75 72 6e 20 28 72 63 3d 3d  ..  return (rc==
12086 53 51 4c 49 54 45 5f 4f 4b 3f 72 63 32 3a 72 63  SQLITE_OK?rc2:rc
12087 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61  );.}../*.** Para
12088 6d 65 74 65 72 20 61 44 61 74 61 20 6d 75 73 74  meter aData must
12089 20 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66   point to a buff
1208a 65 72 20 6f 66 20 70 50 61 67 65 72 2d 3e 70 61  er of pPager->pa
1208b 67 65 53 69 7a 65 20 62 79 74 65 73 0a 2a 2a 20  geSize bytes.** 
1208c 6f 66 20 64 61 74 61 2e 20 43 6f 6d 70 75 74 65  of data. Compute
1208d 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 63 68   and return a ch
1208e 65 63 6b 73 75 6d 20 62 61 73 65 64 20 6f 6e 74  ecksum based ont
1208f 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
12090 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 20 6f 66   the .** page of
12091 20 64 61 74 61 20 61 6e 64 20 74 68 65 20 63 75   data and the cu
12092 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 70  rrent value of p
12093 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69 74  Pager->cksumInit
12094 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ..**.** This is 
12095 6e 6f 74 20 61 20 72 65 61 6c 20 63 68 65 63 6b  not a real check
12096 73 75 6d 2e 20 49 74 20 69 73 20 72 65 61 6c 6c  sum. It is reall
12097 79 20 6a 75 73 74 20 74 68 65 20 73 75 6d 20 6f  y just the sum o
12098 66 20 74 68 65 20 0a 2a 2a 20 72 61 6e 64 6f 6d  f the .** random
12099 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 28   initial value (
1209a 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69  pPager->cksumIni
1209b 74 29 20 61 6e 64 20 65 76 65 72 79 20 32 30 30  t) and every 200
1209c 74 68 20 62 79 74 65 0a 2a 2a 20 6f 66 20 74 68  th byte.** of th
1209d 65 20 70 61 67 65 20 64 61 74 61 2c 20 73 74 61  e page data, sta
1209e 72 74 69 6e 67 20 77 69 74 68 20 62 79 74 65 20  rting with byte 
1209f 6f 66 66 73 65 74 20 28 70 50 61 67 65 72 2d 3e  offset (pPager->
120a0 70 61 67 65 53 69 7a 65 25 32 30 30 29 2e 0a 2a  pageSize%200)..*
120a1 2a 20 45 61 63 68 20 62 79 74 65 20 69 73 20 69  * Each byte is i
120a2 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61 6e  nterpreted as an
120a3 20 38 2d 62 69 74 20 75 6e 73 69 67 6e 65 64 20   8-bit unsigned 
120a4 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 43  integer..**.** C
120a5 68 61 6e 67 69 6e 67 20 74 68 65 20 66 6f 72 6d  hanging the form
120a6 75 6c 61 20 75 73 65 64 20 74 6f 20 63 6f 6d 70  ula used to comp
120a7 75 74 65 20 74 68 69 73 20 63 68 65 63 6b 73 75  ute this checksu
120a8 6d 20 72 65 73 75 6c 74 73 20 69 6e 20 61 6e 0a  m results in an.
120a9 2a 2a 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20  ** incompatible 
120aa 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72  journal file for
120ab 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6a 6f  mat..**.** If jo
120ac 75 72 6e 61 6c 20 63 6f 72 72 75 70 74 69 6f 6e  urnal corruption
120ad 20 6f 63 63 75 72 73 20 64 75 65 20 74 6f 20 61   occurs due to a
120ae 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 2c 20   power failure, 
120af 74 68 65 20 6d 6f 73 74 20 6c 69 6b 65 6c 79 20  the most likely 
120b0 0a 2a 2a 20 73 63 65 6e 61 72 69 6f 20 69 73 20  .** scenario is 
120b1 74 68 61 74 20 6f 6e 65 20 65 6e 64 20 6f 72 20  that one end or 
120b2 74 68 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65  the other of the
120b3 20 72 65 63 6f 72 64 20 77 69 6c 6c 20 62 65 20   record will be 
120b4 63 68 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 74 20  changed. .** It 
120b5 69 73 20 6d 75 63 68 20 6c 65 73 73 20 6c 69 6b  is much less lik
120b6 65 6c 79 20 74 68 61 74 20 74 68 65 20 74 77 6f  ely that the two
120b7 20 65 6e 64 73 20 6f 66 20 74 68 65 20 6a 6f 75   ends of the jou
120b8 72 6e 61 6c 20 72 65 63 6f 72 64 20 77 69 6c 6c  rnal record will
120b9 20 62 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61   be.** correct a
120ba 6e 64 20 74 68 65 20 6d 69 64 64 6c 65 20 62 65  nd the middle be
120bb 20 63 6f 72 72 75 70 74 2e 20 20 54 68 75 73 2c   corrupt.  Thus,
120bc 20 74 68 69 73 20 22 63 68 65 63 6b 73 75 6d 22   this "checksum"
120bd 20 73 63 68 65 6d 65 2c 0a 2a 2a 20 74 68 6f 75   scheme,.** thou
120be 67 68 20 66 61 73 74 20 61 6e 64 20 73 69 6d 70  gh fast and simp
120bf 6c 65 2c 20 63 61 74 63 68 65 73 20 74 68 65 20  le, catches the 
120c0 6d 6f 73 74 6c 79 20 6c 69 6b 65 6c 79 20 6b 69  mostly likely ki
120c1 6e 64 20 6f 66 20 63 6f 72 72 75 70 74 69 6f 6e  nd of corruption
120c2 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 33 32 20  ..*/.static u32 
120c3 70 61 67 65 72 5f 63 6b 73 75 6d 28 50 61 67 65  pager_cksum(Page
120c4 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74  r *pPager, const
120c5 20 75 38 20 2a 61 44 61 74 61 29 7b 0a 20 20 75   u8 *aData){.  u
120c6 33 32 20 63 6b 73 75 6d 20 3d 20 70 50 61 67 65  32 cksum = pPage
120c7 72 2d 3e 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20  r->cksumInit;   
120c8 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75        /* Checksu
120c9 6d 20 76 61 6c 75 65 20 74 6f 20 72 65 74 75 72  m value to retur
120ca 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70  n */.  int i = p
120cb 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2d  Pager->pageSize-
120cc 32 30 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  200;          /*
120cd 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
120ce 0a 20 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b  .  while( i>0 ){
120cf 0a 20 20 20 20 63 6b 73 75 6d 20 2b 3d 20 61 44  .    cksum += aD
120d0 61 74 61 5b 69 5d 3b 0a 20 20 20 20 69 20 2d 3d  ata[i];.    i -=
120d1 20 32 30 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   200;.  }.  retu
120d2 72 6e 20 63 6b 73 75 6d 3b 0a 7d 0a 0a 2f 2a 0a  rn cksum;.}../*.
120d3 2a 2a 20 52 65 61 64 20 61 20 73 69 6e 67 6c 65  ** Read a single
120d4 20 70 61 67 65 20 66 72 6f 6d 20 65 69 74 68 65   page from eithe
120d5 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  r the journal fi
120d6 6c 65 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e  le (if isMainJrn
120d7 6c 3d 3d 31 29 20 6f 72 0a 2a 2a 20 66 72 6f 6d  l==1) or.** from
120d8 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c   the sub-journal
120d9 20 28 69 66 20 69 73 4d 61 69 6e 4a 72 6e 6c 3d   (if isMainJrnl=
120da 3d 30 29 20 61 6e 64 20 70 6c 61 79 62 61 63 6b  =0) and playback
120db 20 74 68 61 74 20 70 61 67 65 2e 0a 2a 2a 20 54   that page..** T
120dc 68 65 20 70 61 67 65 20 62 65 67 69 6e 73 20 61  he page begins a
120dd 74 20 6f 66 66 73 65 74 20 2a 70 4f 66 66 73 65  t offset *pOffse
120de 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 2e  t into the file.
120df 20 54 68 65 20 2a 70 4f 66 66 73 65 74 0a 2a 2a   The *pOffset.**
120e0 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65 61   value is increa
120e1 73 65 64 20 74 6f 20 74 68 65 20 73 74 61 72 74  sed to the start
120e2 20 6f 66 20 74 68 65 20 6e 65 78 74 20 70 61 67   of the next pag
120e3 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e in the journal
120e4 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 4d 61  ..**.** The isMa
120e5 69 6e 4a 72 6e 6c 20 66 6c 61 67 20 69 73 20 74  inJrnl flag is t
120e6 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20 74  rue if this is t
120e7 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b  he main rollback
120e8 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 0a 2a 2a 20   journal and.** 
120e9 66 61 6c 73 65 20 66 6f 72 20 74 68 65 20 73 74  false for the st
120ea 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e  atement journal.
120eb 20 20 54 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62    The main rollb
120ec 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 75 73 65 73  ack journal uses
120ed 0a 2a 2a 20 63 68 65 63 6b 73 75 6d 73 20 2d 20  .** checksums - 
120ee 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f  the statement jo
120ef 75 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74 2e 0a  urnal does not..
120f0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67  **.** If the pag
120f1 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
120f2 70 61 67 65 20 72 65 63 6f 72 64 20 72 65 61 64  page record read
120f3 20 66 72 6f 6d 20 74 68 65 20 28 73 75 62 2d 29   from the (sub-)
120f4 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20  journal file.** 
120f5 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
120f6 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75  the current valu
120f7 65 20 6f 66 20 50 61 67 65 72 2e 64 62 53 69 7a  e of Pager.dbSiz
120f8 65 2c 20 74 68 65 6e 20 70 6c 61 79 62 61 63 6b  e, then playback
120f9 20 69 73 0a 2a 2a 20 73 6b 69 70 70 65 64 20 61   is.** skipped a
120fa 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  nd SQLITE_OK is 
120fb 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
120fc 49 66 20 70 44 6f 6e 65 20 69 73 20 6e 6f 74 20  If pDone is not 
120fd 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73  NULL, then it is
120fe 20 61 20 72 65 63 6f 72 64 20 6f 66 20 70 61 67   a record of pag
120ff 65 73 20 74 68 61 74 20 68 61 76 65 20 61 6c 72  es that have alr
12100 65 61 64 79 0a 2a 2a 20 62 65 65 6e 20 70 6c 61  eady.** been pla
12101 79 65 64 20 62 61 63 6b 2e 20 20 49 66 20 74 68  yed back.  If th
12102 65 20 70 61 67 65 20 61 74 20 2a 70 4f 66 66 73  e page at *pOffs
12103 65 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  et has already b
12104 65 65 6e 20 70 6c 61 79 65 64 20 62 61 63 6b 0a  een played back.
12105 2a 2a 20 28 69 66 20 74 68 65 20 63 6f 72 72 65  ** (if the corre
12106 73 70 6f 6e 64 69 6e 67 20 70 44 6f 6e 65 20 62  sponding pDone b
12107 69 74 20 69 73 20 73 65 74 29 20 74 68 65 6e 20  it is set) then 
12108 73 6b 69 70 20 74 68 65 20 70 6c 61 79 62 61 63  skip the playbac
12109 6b 2e 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  k..** Make sure 
1210a 74 68 65 20 70 44 6f 6e 65 20 62 69 74 20 63 6f  the pDone bit co
1210b 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74  rresponding to t
1210c 68 65 20 2a 70 4f 66 66 73 65 74 20 70 61 67 65  he *pOffset page
1210d 20 69 73 20 73 65 74 0a 2a 2a 20 70 72 69 6f 72   is set.** prior
1210e 20 74 6f 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a   to returning..*
1210f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 67 65  *.** If the page
12110 20 72 65 63 6f 72 64 20 69 73 20 73 75 63 63 65   record is succe
12111 73 73 66 75 6c 6c 79 20 72 65 61 64 20 66 72 6f  ssfully read fro
12112 6d 20 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72  m the (sub-)jour
12113 6e 61 6c 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20  nal file.** and 
12114 70 6c 61 79 65 64 20 62 61 63 6b 2c 20 74 68 65  played back, the
12115 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  n SQLITE_OK is r
12116 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 49  eturned. If an I
12117 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 0a 2a  O error occurs.*
12118 2a 20 77 68 69 6c 65 20 72 65 61 64 69 6e 67 20  * while reading 
12119 74 68 65 20 72 65 63 6f 72 64 20 66 72 6f 6d 20  the record from 
1211a 74 68 65 20 28 73 75 62 2d 29 6a 6f 75 72 6e 61  the (sub-)journa
1211b 6c 20 66 69 6c 65 20 6f 72 20 77 68 69 6c 65 20  l file or while 
1211c 77 72 69 74 69 6e 67 0a 2a 2a 20 74 6f 20 74 68  writing.** to th
1211d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
1211e 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72   then the IO err
1211f 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
12120 6e 65 64 2e 20 49 66 20 64 61 74 61 0a 2a 2a 20  ned. If data.** 
12121 69 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  is successfully 
12122 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20 28 73  read from the (s
12123 75 62 2d 29 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ub-)journal file
12124 20 62 75 74 20 61 70 70 65 61 72 73 20 74 6f 20   but appears to 
12125 62 65 0a 2a 2a 20 63 6f 72 72 75 70 74 65 64 2c  be.** corrupted,
12126 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20   SQLITE_DONE is 
12127 72 65 74 75 72 6e 65 64 2e 20 44 61 74 61 20 69  returned. Data i
12128 73 20 63 6f 6e 73 69 64 65 72 65 64 20 63 6f 72  s considered cor
12129 72 75 70 74 65 64 20 69 6e 0a 2a 2a 20 74 77 6f  rupted in.** two
1212a 20 63 69 72 63 75 6d 73 74 61 6e 63 65 73 3a 0a   circumstances:.
1212b 2a 2a 20 0a 2a 2a 20 20 20 2a 20 49 66 20 74 68  ** .**   * If th
1212c 65 20 72 65 63 6f 72 64 20 70 61 67 65 2d 6e 75  e record page-nu
1212d 6d 62 65 72 20 69 73 20 69 6c 6c 65 67 61 6c 20  mber is illegal 
1212e 28 30 20 6f 72 20 50 41 47 45 52 5f 4d 4a 5f 50  (0 or PAGER_MJ_P
1212f 47 4e 4f 29 2c 20 6f 72 0a 2a 2a 20 20 20 2a 20  GNO), or.**   * 
12130 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 69 73  If the record is
12131 20 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61   being rolled ba
12132 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e  ck from the main
12133 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a   journal file.**
12134 20 20 20 20 20 61 6e 64 20 74 68 65 20 63 68 65       and the che
12135 63 6b 73 75 6d 20 66 69 65 6c 64 20 64 6f 65 73  cksum field does
12136 20 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20 72   not match the r
12137 65 63 6f 72 64 20 63 6f 6e 74 65 6e 74 2e 0a 2a  ecord content..*
12138 2a 0a 2a 2a 20 4e 65 69 74 68 65 72 20 6f 66 20  *.** Neither of 
12139 74 68 65 73 65 20 74 77 6f 20 73 63 65 6e 61 72  these two scenar
1213a 69 6f 73 20 61 72 65 20 70 6f 73 73 69 62 6c 65  ios are possible
1213b 20 64 75 72 69 6e 67 20 61 20 73 61 76 65 70 6f   during a savepo
1213c 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a  int rollback..**
1213d 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20 61  .** If this is a
1213e 20 73 61 76 65 70 6f 69 6e 74 20 72 6f 6c 6c 62   savepoint rollb
1213f 61 63 6b 2c 20 74 68 65 6e 20 6d 65 6d 6f 72 79  ack, then memory
12140 20 6d 61 79 20 68 61 76 65 20 74 6f 20 62 65 20   may have to be 
12141 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 2a 2a 20 61  dynamically.** a
12142 6c 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73  llocated by this
12143 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 74 68   function. If th
12144 69 73 20 69 73 20 74 68 65 20 63 61 73 65 20 61  is is the case a
12145 6e 64 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e  nd an allocation
12146 20 66 61 69 6c 73 2c 0a 2a 2a 20 53 51 4c 49 54   fails,.** SQLIT
12147 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72  E_NOMEM is retur
12148 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ned..*/.static i
12149 6e 74 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63  nt pager_playbac
1214a 6b 5f 6f 6e 65 5f 70 61 67 65 28 0a 20 20 50 61  k_one_page(.  Pa
1214b 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20 20  ger *pPager,    
1214c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1214d 68 65 20 70 61 67 65 72 20 62 65 69 6e 67 20 70  he pager being p
1214e 6c 61 79 65 64 20 62 61 63 6b 20 2a 2f 0a 20 20  layed back */.  
1214f 69 6e 74 20 69 73 4d 61 69 6e 4a 72 6e 6c 2c 20  int isMainJrnl, 
12150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
12151 20 31 20 2d 3e 20 6d 61 69 6e 20 6a 6f 75 72 6e   1 -> main journ
12152 61 6c 2e 20 30 20 2d 3e 20 73 75 62 2d 6a 6f 75  al. 0 -> sub-jou
12153 72 6e 61 6c 2e 20 2a 2f 0a 20 20 69 6e 74 20 69  rnal. */.  int i
12154 73 55 6e 73 79 6e 63 2c 20 20 20 20 20 20 20 20  sUnsync,        
12155 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
12156 20 69 66 20 72 65 61 64 69 6e 67 20 66 72 6f 6d   if reading from
12157 20 75 6e 73 79 6e 63 65 64 20 6d 61 69 6e 20 6a   unsynced main j
12158 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 69 36 34 20  ournal */.  i64 
12159 2a 70 4f 66 66 73 65 74 2c 20 20 20 20 20 20 20  *pOffset,       
1215a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66            /* Off
1215b 73 65 74 20 6f 66 20 72 65 63 6f 72 64 20 74 6f  set of record to
1215c 20 70 6c 61 79 62 61 63 6b 20 2a 2f 0a 20 20 69   playback */.  i
1215d 6e 74 20 69 73 53 61 76 65 70 6e 74 2c 20 20 20  nt isSavepnt,   
1215e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1215f 54 72 75 65 20 66 6f 72 20 61 20 73 61 76 65 70  True for a savep
12160 6f 69 6e 74 20 72 6f 6c 6c 62 61 63 6b 20 2a 2f  oint rollback */
12161 0a 20 20 42 69 74 76 65 63 20 2a 70 44 6f 6e 65  .  Bitvec *pDone
12162 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12163 20 2f 2a 20 42 69 74 76 65 63 20 6f 66 20 70 61   /* Bitvec of pa
12164 67 65 73 20 61 6c 72 65 61 64 79 20 70 6c 61 79  ges already play
12165 65 64 20 62 61 63 6b 20 2a 2f 0a 29 7b 0a 20 20  ed back */.){.  
12166 69 6e 74 20 72 63 3b 0a 20 20 50 67 48 64 72 20  int rc;.  PgHdr 
12167 2a 70 50 67 3b 20 20 20 20 20 20 20 20 20 20 20  *pPg;           
12168 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 65 78          /* An ex
12169 69 73 74 69 6e 67 20 70 61 67 65 20 69 6e 20 74  isting page in t
1216a 68 65 20 63 61 63 68 65 20 2a 2f 0a 20 20 50 67  he cache */.  Pg
1216b 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20 20 20 20  no pgno;        
1216c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1216d 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  he page number o
1216e 66 20 61 20 70 61 67 65 20 69 6e 20 6a 6f 75 72  f a page in jour
1216f 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73  nal */.  u32 cks
12170 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  um;             
12171 20 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 73         /* Checks
12172 75 6d 20 75 73 65 64 20 66 6f 72 20 73 61 6e 69  um used for sani
12173 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20  ty checking */. 
12174 20 75 38 20 2a 61 44 61 74 61 3b 20 20 20 20 20   u8 *aData;     
12175 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
12176 2a 20 54 65 6d 70 6f 72 61 72 79 20 73 74 6f 72  * Temporary stor
12177 61 67 65 20 66 6f 72 20 74 68 65 20 70 61 67 65  age for the page
12178 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69   */.  sqlite3_fi
12179 6c 65 20 2a 6a 66 64 3b 20 20 20 20 20 20 20 20  le *jfd;        
1217a 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65 20      /* The file 
1217b 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 74  descriptor for t
1217c 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
1217d 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 28 69  */..  assert( (i
1217e 73 4d 61 69 6e 4a 72 6e 6c 26 7e 31 29 3d 3d 30  sMainJrnl&~1)==0
1217f 20 29 3b 20 20 20 20 20 20 2f 2a 20 69 73 4d 61   );      /* isMa
12180 69 6e 4a 72 6e 6c 20 69 73 20 30 20 6f 72 20 31  inJrnl is 0 or 1
12181 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 69   */.  assert( (i
12182 73 53 61 76 65 70 6e 74 26 7e 31 29 3d 3d 30 20  sSavepnt&~1)==0 
12183 29 3b 20 20 20 20 20 20 20 2f 2a 20 69 73 53 61  );       /* isSa
12184 76 65 70 6e 74 20 69 73 20 30 20 6f 72 20 31 20  vepnt is 0 or 1 
12185 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 73 4d  */.  assert( isM
12186 61 69 6e 4a 72 6e 6c 20 7c 7c 20 70 44 6f 6e 65  ainJrnl || pDone
12187 20 29 3b 20 20 20 20 20 2f 2a 20 70 44 6f 6e 65   );     /* pDone
12188 20 61 6c 77 61 79 73 20 75 73 65 64 20 6f 6e 20   always used on 
12189 73 75 62 2d 6a 6f 75 72 6e 61 6c 73 20 2a 2f 0a  sub-journals */.
1218a 20 20 61 73 73 65 72 74 28 20 69 73 53 61 76 65    assert( isSave
1218b 70 6e 74 20 7c 7c 20 70 44 6f 6e 65 3d 3d 30 20  pnt || pDone==0 
1218c 29 3b 20 20 20 2f 2a 20 70 44 6f 6e 65 20 6e 65  );   /* pDone ne
1218d 76 65 72 20 75 73 65 64 20 6f 6e 20 6e 6f 6e 2d  ver used on non-
1218e 73 61 76 65 70 6f 69 6e 74 20 2a 2f 0a 0a 20 20  savepoint */..  
1218f 61 44 61 74 61 20 3d 20 28 75 38 2a 29 70 50 61  aData = (u8*)pPa
12190 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a  ger->pTmpSpace;.
12191 20 20 61 73 73 65 72 74 28 20 61 44 61 74 61 20    assert( aData 
12192 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65  );         /* Te
12193 6d 70 20 73 74 6f 72 61 67 65 20 6d 75 73 74 20  mp storage must 
12194 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65  have already bee
12195 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 0a  n allocated */..
12196 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 70 61    /* Read the pa
12197 67 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 70 61  ge number and pa
12198 67 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65  ge data from the
12199 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 73 75 62 2d   journal or sub-
1219a 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69 6c  journal.  ** fil
1219b 65 2e 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72  e. Return an err
1219c 6f 72 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63  or code to the c
1219d 61 6c 6c 65 72 20 69 66 20 61 6e 20 49 4f 20 65  aller if an IO e
1219e 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 20 20 2a  rror occurs..  *
1219f 2f 0a 20 20 6a 66 64 20 3d 20 69 73 4d 61 69 6e  /.  jfd = isMain
121a0 4a 72 6e 6c 20 3f 20 70 50 61 67 65 72 2d 3e 6a  Jrnl ? pPager->j
121a1 66 64 20 3a 20 70 50 61 67 65 72 2d 3e 73 6a 66  fd : pPager->sjf
121a2 64 3b 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32  d;.  rc = read32
121a3 62 69 74 73 28 6a 66 64 2c 20 2a 70 4f 66 66 73  bits(jfd, *pOffs
121a4 65 74 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 69 66  et, &pgno);.  if
121a5 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
121a6 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72  ) return rc;.  r
121a7 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61  c = sqlite3OsRea
121a8 64 28 6a 66 64 2c 20 61 44 61 74 61 2c 20 70 50  d(jfd, aData, pP
121a9 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20  ager->pageSize, 
121aa 28 2a 70 4f 66 66 73 65 74 29 2b 34 29 3b 0a 20  (*pOffset)+4);. 
121ab 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
121ac 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
121ad 20 20 2a 70 4f 66 66 73 65 74 20 2b 3d 20 70 50    *pOffset += pP
121ae 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 2b  ager->pageSize +
121af 20 34 20 2b 20 69 73 4d 61 69 6e 4a 72 6e 6c 2a   4 + isMainJrnl*
121b0 34 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20  4;..  /* Sanity 
121b1 63 68 65 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20  checking on the 
121b2 70 61 67 65 2e 20 20 54 68 69 73 20 69 73 20 6d  page.  This is m
121b3 6f 72 65 20 69 6d 70 6f 72 74 61 6e 74 20 74 68  ore important th
121b4 61 74 20 49 20 6f 72 69 67 69 6e 61 6c 6c 79 0a  at I originally.
121b5 20 20 2a 2a 20 74 68 6f 75 67 68 74 2e 20 20 49    ** thought.  I
121b6 66 20 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72  f a power failur
121b7 65 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74  e occurs while t
121b8 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65  he journal is be
121b9 69 6e 67 20 77 72 69 74 74 65 6e 2c 0a 20 20 2a  ing written,.  *
121ba 2a 20 69 74 20 63 6f 75 6c 64 20 63 61 75 73 65  * it could cause
121bb 20 69 6e 76 61 6c 69 64 20 64 61 74 61 20 74 6f   invalid data to
121bc 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f   be written into
121bd 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 57   the journal.  W
121be 65 20 6e 65 65 64 20 74 6f 0a 20 20 2a 2a 20 64  e need to.  ** d
121bf 65 74 65 63 74 20 74 68 69 73 20 69 6e 76 61 6c  etect this inval
121c0 69 64 20 64 61 74 61 20 28 77 69 74 68 20 68 69  id data (with hi
121c1 67 68 20 70 72 6f 62 61 62 69 6c 69 74 79 29 20  gh probability) 
121c2 61 6e 64 20 69 67 6e 6f 72 65 20 69 74 2e 0a 20  and ignore it.. 
121c3 20 2a 2f 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d   */.  if( pgno==
121c4 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52  0 || pgno==PAGER
121c5 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29  _MJ_PGNO(pPager)
121c6 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
121c7 21 69 73 53 61 76 65 70 6e 74 20 29 3b 0a 20 20  !isSavepnt );.  
121c8 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
121c9 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20  DONE;.  }.  if( 
121ca 70 67 6e 6f 3e 28 50 67 6e 6f 29 70 50 61 67 65  pgno>(Pgno)pPage
121cb 72 2d 3e 64 62 53 69 7a 65 20 7c 7c 20 73 71 6c  r->dbSize || sql
121cc 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 70  ite3BitvecTest(p
121cd 44 6f 6e 65 2c 20 70 67 6e 6f 29 20 29 7b 0a 20  Done, pgno) ){. 
121ce 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
121cf 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69  _OK;.  }.  if( i
121d0 73 4d 61 69 6e 4a 72 6e 6c 20 29 7b 0a 20 20 20  sMainJrnl ){.   
121d1 20 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73   rc = read32bits
121d2 28 6a 66 64 2c 20 28 2a 70 4f 66 66 73 65 74 29  (jfd, (*pOffset)
121d3 2d 34 2c 20 26 63 6b 73 75 6d 29 3b 0a 20 20 20  -4, &cksum);.   
121d4 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
121d5 20 72 63 3b 0a 20 20 20 20 69 66 28 20 21 69 73   rc;.    if( !is
121d6 53 61 76 65 70 6e 74 20 26 26 20 70 61 67 65 72  Savepnt && pager
121d7 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 2c 20 61  _cksum(pPager, a
121d8 44 61 74 61 29 21 3d 63 6b 73 75 6d 20 29 7b 0a  Data)!=cksum ){.
121d9 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
121da 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 0a  ITE_DONE;.    }.
121db 20 20 7d 0a 0a 20 20 69 66 28 20 70 44 6f 6e 65    }..  if( pDone
121dc 20 26 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65   && (rc = sqlite
121dd 33 42 69 74 76 65 63 53 65 74 28 70 44 6f 6e 65  3BitvecSet(pDone
121de 2c 20 70 67 6e 6f 29 29 21 3d 53 51 4c 49 54 45  , pgno))!=SQLITE
121df 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
121e0 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  n rc;.  }..  ass
121e1 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61  ert( pPager->sta
121e2 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 56  te==PAGER_RESERV
121e3 45 44 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74  ED || pPager->st
121e4 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55  ate>=PAGER_EXCLU
121e5 53 49 56 45 20 29 3b 0a 0a 20 20 2f 2a 20 49 66  SIVE );..  /* If
121e6 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69 6e   the pager is in
121e7 20 52 45 53 45 52 56 45 44 20 73 74 61 74 65 2c   RESERVED state,
121e8 20 74 68 65 6e 20 74 68 65 72 65 20 6d 75 73 74   then there must
121e9 20 62 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68   be a copy of th
121ea 69 73 0a 20 20 2a 2a 20 70 61 67 65 20 69 6e 20  is.  ** page in 
121eb 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e  the pager cache.
121ec 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 6a 75   In this case ju
121ed 73 74 20 75 70 64 61 74 65 20 74 68 65 20 70 61  st update the pa
121ee 67 65 72 20 63 61 63 68 65 2c 0a 20 20 2a 2a 20  ger cache,.  ** 
121ef 6e 6f 74 20 74 68 65 20 64 61 74 61 62 61 73 65  not the database
121f0 20 66 69 6c 65 2e 20 54 68 65 20 70 61 67 65 20   file. The page 
121f1 69 73 20 6c 65 66 74 20 6d 61 72 6b 65 64 20 64  is left marked d
121f2 69 72 74 79 20 69 6e 20 74 68 69 73 20 63 61 73  irty in this cas
121f3 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20  e..  **.  ** An 
121f4 65 78 63 65 70 74 69 6f 6e 20 74 6f 20 74 68 65  exception to the
121f5 20 61 62 6f 76 65 20 72 75 6c 65 3a 20 49 66 20   above rule: If 
121f6 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  the database is 
121f7 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 0a  in no-sync mode.
121f8 20 20 2a 2a 20 61 6e 64 20 61 20 70 61 67 65 20    ** and a page 
121f9 69 73 20 6d 6f 76 65 64 20 64 75 72 69 6e 67 20  is moved during 
121fa 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76  an incremental v
121fb 61 63 75 75 6d 20 74 68 65 6e 20 74 68 65 20 70  acuum then the p
121fc 61 67 65 20 6d 61 79 0a 20 20 2a 2a 20 6e 6f 74  age may.  ** not
121fd 20 62 65 20 69 6e 20 74 68 65 20 70 61 67 65 72   be in the pager
121fe 20 63 61 63 68 65 2e 20 4c 61 74 65 72 3a 20 69   cache. Later: i
121ff 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 6f 72 20  f a malloc() or 
12200 49 4f 20 65 72 72 6f 72 20 6f 63 63 75 72 73 0a  IO error occurs.
12201 20 20 2a 2a 20 64 75 72 69 6e 67 20 61 20 4d 6f    ** during a Mo
12202 76 65 70 61 67 65 28 29 20 63 61 6c 6c 2c 20 74  vepage() call, t
12203 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d 61 79  hen the page may
12204 20 6e 6f 74 20 62 65 20 69 6e 20 74 68 65 20 63   not be in the c
12205 61 63 68 65 0a 20 20 2a 2a 20 65 69 74 68 65 72  ache.  ** either
12206 2e 20 53 6f 20 74 68 65 20 63 6f 6e 64 69 74 69  . So the conditi
12207 6f 6e 20 64 65 73 63 72 69 62 65 64 20 69 6e 20  on described in 
12208 74 68 65 20 61 62 6f 76 65 20 70 61 72 61 67 72  the above paragr
12209 61 70 68 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20  aph is not.  ** 
1220a 61 73 73 65 72 74 28 29 61 62 6c 65 2e 0a 20 20  assert()able..  
1220b 2a 2a 0a 20 20 2a 2a 20 49 66 20 69 6e 20 45 58  **.  ** If in EX
1220c 43 4c 55 53 49 56 45 20 73 74 61 74 65 2c 20 74  CLUSIVE state, t
1220d 68 65 6e 20 77 65 20 75 70 64 61 74 65 20 74 68  hen we update th
1220e 65 20 70 61 67 65 72 20 63 61 63 68 65 20 69 66  e pager cache if
1220f 20 69 74 20 65 78 69 73 74 73 0a 20 20 2a 2a 20   it exists.  ** 
12210 61 6e 64 20 74 68 65 20 6d 61 69 6e 20 66 69 6c  and the main fil
12211 65 2e 20 54 68 65 20 70 61 67 65 20 69 73 20 74  e. The page is t
12212 68 65 6e 20 6d 61 72 6b 65 64 20 6e 6f 74 20 64  hen marked not d
12213 69 72 74 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  irty..  **.  ** 
12214 54 69 63 6b 65 74 20 23 31 31 37 31 3a 20 20 54  Ticket #1171:  T
12215 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75  he statement jou
12216 72 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61  rnal might conta
12217 69 6e 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20  in page content 
12218 74 68 61 74 20 69 73 0a 20 20 2a 2a 20 64 69 66  that is.  ** dif
12219 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68 65 20  ferent from the 
1221a 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 61 74 20  page content at 
1221b 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65  the start of the
1221c 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20   transaction..  
1221d 2a 2a 20 54 68 69 73 20 6f 63 63 75 72 73 20 77  ** This occurs w
1221e 68 65 6e 20 61 20 70 61 67 65 20 69 73 20 63 68  hen a page is ch
1221f 61 6e 67 65 64 20 70 72 69 6f 72 20 74 6f 20 74  anged prior to t
12220 68 65 20 73 74 61 72 74 20 6f 66 20 61 20 73 74  he start of a st
12221 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 74 68 65  atement.  ** the
12222 6e 20 63 68 61 6e 67 65 64 20 61 67 61 69 6e 20  n changed again 
12223 77 69 74 68 69 6e 20 74 68 65 20 73 74 61 74 65  within the state
12224 6d 65 6e 74 2e 20 20 57 68 65 6e 20 72 6f 6c 6c  ment.  When roll
12225 69 6e 67 20 62 61 63 6b 20 73 75 63 68 20 61 0a  ing back such a.
12226 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 77    ** statement w
12227 65 20 6d 75 73 74 20 6e 6f 74 20 77 72 69 74 65  e must not write
12228 20 74 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   to the original
12229 20 64 61 74 61 62 61 73 65 20 75 6e 6c 65 73 73   database unless
1222a 20 77 65 20 6b 6e 6f 77 0a 20 20 2a 2a 20 66 6f   we know.  ** fo
1222b 72 20 63 65 72 74 61 69 6e 20 74 68 61 74 20 6f  r certain that o
1222c 72 69 67 69 6e 61 6c 20 70 61 67 65 20 63 6f 6e  riginal page con
1222d 74 65 6e 74 73 20 61 72 65 20 73 79 6e 63 65 64  tents are synced
1222e 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 72   into the main r
1222f 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 6a 6f 75  ollback.  ** jou
12230 72 6e 61 6c 2e 20 20 4f 74 68 65 72 77 69 73 65  rnal.  Otherwise
12231 2c 20 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6d  , a power loss m
12232 69 67 68 74 20 6c 65 61 76 65 20 6d 6f 64 69 66  ight leave modif
12233 69 65 64 20 64 61 74 61 20 69 6e 20 74 68 65 0a  ied data in the.
12234 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69    ** database fi
12235 6c 65 20 77 69 74 68 6f 75 74 20 61 6e 20 65 6e  le without an en
12236 74 72 79 20 69 6e 20 74 68 65 20 72 6f 6c 6c 62  try in the rollb
12237 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 74 68 61 74  ack journal that
12238 20 63 61 6e 0a 20 20 2a 2a 20 72 65 73 74 6f 72   can.  ** restor
12239 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  e the database t
1223a 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 66  o its original f
1223b 6f 72 6d 2e 20 20 54 77 6f 20 63 6f 6e 64 69 74  orm.  Two condit
1223c 69 6f 6e 73 20 6d 75 73 74 20 62 65 0a 20 20 2a  ions must be.  *
1223d 2a 20 6d 65 74 20 62 65 66 6f 72 65 20 77 72 69  * met before wri
1223e 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61  ting to the data
1223f 62 61 73 65 20 66 69 6c 65 73 2e 20 28 31 29 20  base files. (1) 
12240 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 75 73  the database mus
12241 74 20 62 65 0a 20 20 2a 2a 20 6c 6f 63 6b 65 64  t be.  ** locked
12242 2e 20 20 28 32 29 20 77 65 20 6b 6e 6f 77 20 74  .  (2) we know t
12243 68 61 74 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  hat the original
12244 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 69 73   page content is
12245 20 66 75 6c 6c 79 20 73 79 6e 63 65 64 0a 20 20   fully synced.  
12246 2a 2a 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a  ** in the main j
12247 6f 75 72 6e 61 6c 20 65 69 74 68 65 72 20 62 65  ournal either be
12248 63 61 75 73 65 20 74 68 65 20 70 61 67 65 20 69  cause the page i
12249 73 20 6e 6f 74 20 69 6e 20 63 61 63 68 65 20 6f  s not in cache o
1224a 72 20 65 6c 73 65 0a 20 20 2a 2a 20 74 68 65 20  r else.  ** the 
1224b 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61  page is marked a
1224c 73 20 6e 65 65 64 53 79 6e 63 3d 3d 30 2e 0a 20  s needSync==0.. 
1224d 20 2a 2a 0a 20 20 2a 2a 20 32 30 30 38 2d 30 34   **.  ** 2008-04
1224e 2d 31 34 3a 20 20 57 68 65 6e 20 61 74 74 65 6d  -14:  When attem
1224f 70 74 69 6e 67 20 74 6f 20 76 61 63 75 75 6d 20  pting to vacuum 
12250 61 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61  a corrupt databa
12251 73 65 20 66 69 6c 65 2c 20 69 74 0a 20 20 2a 2a  se file, it.  **
12252 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20   is possible to 
12253 66 61 69 6c 20 61 20 73 74 61 74 65 6d 65 6e 74  fail a statement
12254 20 6f 6e 20 61 20 64 61 74 61 62 61 73 65 20 74   on a database t
12255 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 79 65 74  hat does not yet
12256 20 65 78 69 73 74 2e 0a 20 20 2a 2a 20 44 6f 20   exist..  ** Do 
12257 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 77  not attempt to w
12258 72 69 74 65 20 69 66 20 64 61 74 61 62 61 73 65  rite if database
12259 20 66 69 6c 65 20 68 61 73 20 6e 65 76 65 72 20   file has never 
1225a 62 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 20 20 2a  been opened..  *
1225b 2f 0a 20 20 70 50 67 20 3d 20 70 61 67 65 72 5f  /.  pPg = pager_
1225c 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c 20 70  lookup(pPager, p
1225d 67 6e 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20  gno);.  assert( 
1225e 70 50 67 20 7c 7c 20 21 4d 45 4d 44 42 20 29 3b  pPg || !MEMDB );
1225f 0a 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22  .  PAGERTRACE(("
12260 50 4c 41 59 42 41 43 4b 20 25 64 20 70 61 67 65  PLAYBACK %d page
12261 20 25 64 20 68 61 73 68 28 25 30 38 78 29 20 25   %d hash(%08x) %
12262 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  s\n",.          
12263 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61       PAGERID(pPa
12264 67 65 72 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65  ger), pgno, page
12265 72 5f 64 61 74 61 68 61 73 68 28 70 50 61 67 65  r_datahash(pPage
12266 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 61 44 61  r->pageSize, aDa
12267 74 61 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ta),.           
12268 20 20 20 20 28 69 73 4d 61 69 6e 4a 72 6e 6c 3f      (isMainJrnl?
12269 22 6d 61 69 6e 2d 6a 6f 75 72 6e 61 6c 22 3a 22  "main-journal":"
1226a 73 75 62 2d 6a 6f 75 72 6e 61 6c 22 29 0a 20 20  sub-journal").  
1226b 29 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67 65  ));.  if( (pPage
1226c 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f  r->state>=PAGER_
1226d 45 58 43 4c 55 53 49 56 45 29 0a 20 20 20 26 26  EXCLUSIVE).   &&
1226e 20 28 70 50 67 3d 3d 30 20 7c 7c 20 30 3d 3d 28   (pPg==0 || 0==(
1226f 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  pPg->flags&PGHDR
12270 5f 4e 45 45 44 5f 53 59 4e 43 29 29 0a 20 20 20  _NEED_SYNC)).   
12271 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72  && isOpen(pPager
12272 2d 3e 66 64 29 0a 20 20 20 26 26 20 21 69 73 55  ->fd).   && !isU
12273 6e 73 79 6e 63 0a 20 20 29 7b 0a 20 20 20 20 69  nsync.  ){.    i
12274 36 34 20 6f 66 73 74 20 3d 20 28 70 67 6e 6f 2d  64 ofst = (pgno-
12275 31 29 2a 28 69 36 34 29 70 50 61 67 65 72 2d 3e  1)*(i64)pPager->
12276 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 72 63  pageSize;.    rc
12277 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
12278 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 61 44  e(pPager->fd, aD
12279 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67  ata, pPager->pag
1227a 65 53 69 7a 65 2c 20 6f 66 73 74 29 3b 0a 20 20  eSize, ofst);.  
1227b 20 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65    if( pgno>pPage
1227c 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 29 7b  r->dbFileSize ){
1227d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 64  .      pPager->d
1227e 62 46 69 6c 65 53 69 7a 65 20 3d 20 70 67 6e 6f  bFileSize = pgno
1227f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
12280 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70 20  pPager->pBackup 
12281 29 7b 0a 20 20 20 20 20 20 43 4f 44 45 43 31 28  ){.      CODEC1(
12282 70 50 61 67 65 72 2c 20 61 44 61 74 61 2c 20 70  pPager, aData, p
12283 67 6e 6f 2c 20 33 2c 20 72 63 3d 53 51 4c 49 54  gno, 3, rc=SQLIT
12284 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 20 20  E_NOMEM);.      
12285 73 71 6c 69 74 65 33 42 61 63 6b 75 70 55 70 64  sqlite3BackupUpd
12286 61 74 65 28 70 50 61 67 65 72 2d 3e 70 42 61 63  ate(pPager->pBac
12287 6b 75 70 2c 20 70 67 6e 6f 2c 20 61 44 61 74 61  kup, pgno, aData
12288 29 3b 0a 20 20 20 20 20 20 43 4f 44 45 43 31 28  );.      CODEC1(
12289 70 50 61 67 65 72 2c 20 61 44 61 74 61 2c 20 70  pPager, aData, p
1228a 67 6e 6f 2c 20 30 2c 20 72 63 3d 53 51 4c 49 54  gno, 0, rc=SQLIT
1228b 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20 20 20 7d 0a  E_NOMEM);.    }.
1228c 20 20 7d 65 6c 73 65 20 69 66 28 20 21 69 73 4d    }else if( !isM
1228d 61 69 6e 4a 72 6e 6c 20 26 26 20 70 50 67 3d 3d  ainJrnl && pPg==
1228e 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74  0 ){.    /* If t
1228f 68 69 73 20 69 73 20 61 20 72 6f 6c 6c 62 61 63  his is a rollbac
12290 6b 20 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74  k of a savepoint
12291 20 61 6e 64 20 64 61 74 61 20 77 61 73 20 6e 6f   and data was no
12292 74 20 77 72 69 74 74 65 6e 20 74 6f 0a 20 20 20  t written to.   
12293 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65   ** the database
12294 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 69 73   and the page is
12295 20 6e 6f 74 20 69 6e 2d 6d 65 6d 6f 72 79 2c 20   not in-memory, 
12296 74 68 65 72 65 20 69 73 20 61 20 70 6f 74 65 6e  there is a poten
12297 74 69 61 6c 0a 20 20 20 20 2a 2a 20 70 72 6f 62  tial.    ** prob
12298 6c 65 6d 2e 20 57 68 65 6e 20 74 68 65 20 70 61  lem. When the pa
12299 67 65 20 69 73 20 6e 65 78 74 20 66 65 74 63 68  ge is next fetch
1229a 65 64 20 62 79 20 74 68 65 20 62 2d 74 72 65 65  ed by the b-tree
1229b 20 6c 61 79 65 72 2c 20 69 74 20 0a 20 20 20 20   layer, it .    
1229c 2a 2a 20 77 69 6c 6c 20 62 65 20 72 65 61 64 20  ** will be read 
1229d 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
1229e 65 20 66 69 6c 65 2c 20 77 68 69 63 68 20 6d 61  e file, which ma
1229f 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20  y or may not be 
122a0 0a 20 20 20 20 2a 2a 20 63 75 72 72 65 6e 74 2e  .    ** current.
122a1 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20   .    **.    ** 
122a2 54 68 65 72 65 20 61 72 65 20 61 20 63 6f 75 70  There are a coup
122a3 6c 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74 20  le of different 
122a4 77 61 79 73 20 74 68 69 73 20 63 61 6e 20 68 61  ways this can ha
122a5 70 70 65 6e 2e 20 41 6c 6c 20 61 72 65 20 71 75  ppen. All are qu
122a6 69 74 65 0a 20 20 20 20 2a 2a 20 6f 62 73 63 75  ite.    ** obscu
122a7 72 65 2e 20 57 68 65 6e 20 72 75 6e 6e 69 6e 67  re. When running
122a8 20 69 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20   in synchronous 
122a9 6d 6f 64 65 2c 20 74 68 69 73 20 63 61 6e 20 6f  mode, this can o
122aa 6e 6c 79 20 68 61 70 70 65 6e 20 0a 20 20 20 20  nly happen .    
122ab 2a 2a 20 69 66 20 74 68 65 20 70 61 67 65 20 69  ** if the page i
122ac 73 20 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69  s on the free-li
122ad 73 74 20 61 74 20 74 68 65 20 73 74 61 72 74 20  st at the start 
122ae 6f 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  of the transacti
122af 6f 6e 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20  on, then.    ** 
122b0 70 6f 70 75 6c 61 74 65 64 2c 20 74 68 65 6e 20  populated, then 
122b1 6d 6f 76 65 64 20 75 73 69 6e 67 20 73 71 6c 69  moved using sqli
122b2 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65  te3PagerMovepage
122b3 28 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ()..    **.    *
122b4 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f 6e 20 69  * The solution i
122b5 73 20 74 6f 20 61 64 64 20 61 6e 20 69 6e 2d 6d  s to add an in-m
122b6 65 6d 6f 72 79 20 70 61 67 65 20 74 6f 20 74 68  emory page to th
122b7 65 20 63 61 63 68 65 20 63 6f 6e 74 61 69 6e 69  e cache containi
122b8 6e 67 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61  ng.    ** the da
122b9 74 61 20 6a 75 73 74 20 72 65 61 64 20 66 72 6f  ta just read fro
122ba 6d 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61  m the sub-journa
122bb 6c 2e 20 4d 61 72 6b 20 74 68 65 20 70 61 67 65  l. Mark the page
122bc 20 61 73 20 64 69 72 74 79 20 0a 20 20 20 20 2a   as dirty .    *
122bd 2a 20 61 6e 64 20 69 66 20 74 68 65 20 70 61 67  * and if the pag
122be 65 72 20 72 65 71 75 69 72 65 73 20 61 20 6a 6f  er requires a jo
122bf 75 72 6e 61 6c 2d 73 79 6e 63 2c 20 74 68 65 6e  urnal-sync, then
122c0 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65 20 61   mark the page a
122c1 73 20 0a 20 20 20 20 2a 2a 20 72 65 71 75 69 72  s .    ** requir
122c2 69 6e 67 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 79  ing a journal-sy
122c3 6e 63 20 62 65 66 6f 72 65 20 69 74 20 69 73 20  nc before it is 
122c4 77 72 69 74 74 65 6e 2e 0a 20 20 20 20 2a 2f 0a  written..    */.
122c5 20 20 20 20 61 73 73 65 72 74 28 20 69 73 53 61      assert( isSa
122c6 76 65 70 6e 74 20 29 3b 0a 20 20 20 20 69 66 28  vepnt );.    if(
122c7 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61   (rc = sqlite3Pa
122c8 67 65 72 41 63 71 75 69 72 65 28 70 50 61 67 65  gerAcquire(pPage
122c9 72 2c 20 70 67 6e 6f 2c 20 26 70 50 67 2c 20 31  r, pgno, &pPg, 1
122ca 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  ))!=SQLITE_OK ){
122cb 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
122cc 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 67 2d  ;.    }.    pPg-
122cd 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52  >flags &= ~PGHDR
122ce 5f 4e 45 45 44 5f 52 45 41 44 3b 0a 20 20 20 20  _NEED_READ;.    
122cf 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b  sqlite3PcacheMak
122d0 65 44 69 72 74 79 28 70 50 67 29 3b 0a 20 20 7d  eDirty(pPg);.  }
122d1 0a 20 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20  .  if( pPg ){.  
122d2 20 20 2f 2a 20 4e 6f 20 70 61 67 65 20 73 68 6f    /* No page sho
122d3 75 6c 64 20 65 76 65 72 20 62 65 20 65 78 70 6c  uld ever be expl
122d4 69 63 69 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61  icitly rolled ba
122d5 63 6b 20 74 68 61 74 20 69 73 20 69 6e 20 75 73  ck that is in us
122d6 65 2c 20 65 78 63 65 70 74 0a 20 20 20 20 2a 2a  e, except.    **
122d7 20 66 6f 72 20 70 61 67 65 20 31 20 77 68 69 63   for page 1 whic
122d8 68 20 69 73 20 68 65 6c 64 20 69 6e 20 75 73 65  h is held in use
122d9 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65   in order to kee
122da 70 20 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68  p the lock on th
122db 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73  e.    ** databas
122dc 65 20 61 63 74 69 76 65 2e 20 48 6f 77 65 76 65  e active. Howeve
122dd 72 20 73 75 63 68 20 61 20 70 61 67 65 20 6d 61  r such a page ma
122de 79 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  y be rolled back
122df 20 61 73 20 61 20 72 65 73 75 6c 74 0a 20 20 20   as a result.   
122e0 20 2a 2a 20 6f 66 20 61 6e 20 69 6e 74 65 72 6e   ** of an intern
122e1 61 6c 20 65 72 72 6f 72 20 72 65 73 75 6c 74 69  al error resulti
122e2 6e 67 20 69 6e 20 61 6e 20 61 75 74 6f 6d 61 74  ng in an automat
122e3 69 63 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a  ic call to.    *
122e4 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f  * sqlite3PagerRo
122e5 6c 6c 62 61 63 6b 28 29 2e 0a 20 20 20 20 2a 2f  llback()..    */
122e6 0a 20 20 20 20 76 6f 69 64 20 2a 70 44 61 74 61  .    void *pData
122e7 3b 0a 20 20 20 20 70 44 61 74 61 20 3d 20 70 50  ;.    pData = pP
122e8 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20 6d 65  g->pData;.    me
122e9 6d 63 70 79 28 70 44 61 74 61 2c 20 61 44 61 74  mcpy(pData, aDat
122ea 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  a, pPager->pageS
122eb 69 7a 65 29 3b 0a 20 20 20 20 70 50 61 67 65 72  ize);.    pPager
122ec 2d 3e 78 52 65 69 6e 69 74 65 72 28 70 50 67 29  ->xReiniter(pPg)
122ed 3b 0a 20 20 20 20 69 66 28 20 69 73 4d 61 69 6e  ;.    if( isMain
122ee 4a 72 6e 6c 20 26 26 20 28 21 69 73 53 61 76 65  Jrnl && (!isSave
122ef 70 6e 74 20 7c 7c 20 2a 70 4f 66 66 73 65 74 3c  pnt || *pOffset<
122f0 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c  =pPager->journal
122f1 48 64 72 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a  Hdr) ){.      /*
122f2 20 49 66 20 74 68 65 20 63 6f 6e 74 65 6e 74 73   If the contents
122f3 20 6f 66 20 74 68 69 73 20 70 61 67 65 20 77 65   of this page we
122f4 72 65 20 6a 75 73 74 20 72 65 73 74 6f 72 65 64  re just restored
122f5 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 0a   from the main .
122f6 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c        ** journal
122f7 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 73 20   file, then its 
122f8 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62 65 20  content must be 
122f9 61 73 20 74 68 65 79 20 77 65 72 65 20 77 68 65  as they were whe
122fa 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20  n the .      ** 
122fb 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20  transaction was 
122fc 66 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 49 6e  first opened. In
122fd 20 74 68 69 73 20 63 61 73 65 20 77 65 20 63 61   this case we ca
122fe 6e 20 6d 61 72 6b 20 74 68 65 20 70 61 67 65 0a  n mark the page.
122ff 20 20 20 20 20 20 2a 2a 20 61 73 20 63 6c 65 61        ** as clea
12300 6e 2c 20 73 69 6e 63 65 20 74 68 65 72 65 20 77  n, since there w
12301 69 6c 6c 20 62 65 20 6e 6f 20 6e 65 65 64 20 74  ill be no need t
12302 6f 20 77 72 69 74 65 20 69 74 20 6f 75 74 20 74  o write it out t
12303 6f 20 74 68 65 2e 0a 20 20 20 20 20 20 2a 2a 0a  o the..      **.
12304 20 20 20 20 20 20 2a 2a 20 54 68 65 72 65 20 69        ** There i
12305 73 20 6f 6e 65 20 65 78 63 65 70 74 69 6f 6e 20  s one exception 
12306 74 6f 20 74 68 69 73 20 72 75 6c 65 2e 20 49 66  to this rule. If
12307 20 74 68 65 20 70 61 67 65 20 69 73 20 62 65 69   the page is bei
12308 6e 67 20 72 6f 6c 6c 65 64 0a 20 20 20 20 20 20  ng rolled.      
12309 2a 2a 20 62 61 63 6b 20 61 73 20 70 61 72 74 20  ** back as part 
1230a 6f 66 20 61 20 73 61 76 65 70 6f 69 6e 74 20 28  of a savepoint (
1230b 6f 72 20 73 74 61 74 65 6d 65 6e 74 29 20 72 6f  or statement) ro
1230c 6c 6c 62 61 63 6b 20 66 72 6f 6d 20 61 6e 20 0a  llback from an .
1230d 20 20 20 20 20 20 2a 2a 20 75 6e 73 79 6e 63 65        ** unsynce
1230e 64 20 70 6f 72 74 69 6f 6e 20 6f 66 20 74 68 65  d portion of the
1230f 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69   main journal fi
12310 6c 65 2c 20 74 68 65 6e 20 69 74 20 69 73 20 6e  le, then it is n
12311 6f 74 20 73 61 66 65 0a 20 20 20 20 20 20 2a 2a  ot safe.      **
12312 20 74 6f 20 6d 61 72 6b 20 74 68 65 20 70 61 67   to mark the pag
12313 65 20 61 73 20 63 6c 65 61 6e 2e 20 54 68 69 73  e as clean. This
12314 20 69 73 20 62 65 63 61 75 73 65 20 6d 61 72 6b   is because mark
12315 69 6e 67 20 74 68 65 20 70 61 67 65 20 61 73 0a  ing the page as.
12316 20 20 20 20 20 20 2a 2a 20 63 6c 65 61 6e 20 77        ** clean w
12317 69 6c 6c 20 63 6c 65 61 72 20 74 68 65 20 50 47  ill clear the PG
12318 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c  HDR_NEED_SYNC fl
12319 61 67 2e 20 53 69 6e 63 65 20 74 68 65 20 70 61  ag. Since the pa
1231a 67 65 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 61  ge is.      ** a
1231b 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 6a 6f  lready in the jo
1231c 75 72 6e 61 6c 20 66 69 6c 65 20 28 72 65 63 6f  urnal file (reco
1231d 72 64 65 64 20 69 6e 20 50 61 67 65 72 2e 70 49  rded in Pager.pI
1231e 6e 4a 6f 75 72 6e 61 6c 29 20 61 6e 64 0a 20 20  nJournal) and.  
1231f 20 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44 52      ** the PGHDR
12320 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20  _NEED_SYNC flag 
12321 69 73 20 63 6c 65 61 72 65 64 2c 20 69 66 20 74  is cleared, if t
12322 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74 74  he page is writt
12323 65 6e 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 61  en to.      ** a
12324 67 61 69 6e 20 77 69 74 68 69 6e 20 74 68 69 73  gain within this
12325 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 74   transaction, it
12326 20 77 69 6c 6c 20 62 65 20 6d 61 72 6b 65 64 20   will be marked 
12327 61 73 20 64 69 72 74 79 20 62 75 74 0a 20 20 20  as dirty but.   
12328 20 20 20 2a 2a 20 74 68 65 20 50 47 48 44 52 5f     ** the PGHDR_
12329 4e 45 45 44 5f 53 59 4e 43 20 66 6c 61 67 20 77  NEED_SYNC flag w
1232a 69 6c 6c 20 6e 6f 74 20 62 65 20 73 65 74 2e 20  ill not be set. 
1232b 49 74 20 63 6f 75 6c 64 20 74 68 65 6e 20 70 6f  It could then po
1232c 74 65 6e 74 69 61 6c 6c 79 0a 20 20 20 20 20 20  tentially.      
1232d 2a 2a 20 62 65 20 77 72 69 74 74 65 6e 20 6f 75  ** be written ou
1232e 74 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62  t into the datab
1232f 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65 20  ase file before 
12330 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  its journal file
12331 0a 20 20 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e  .      ** segmen
12332 74 20 69 73 20 73 79 6e 63 65 64 2e 20 49 66 20  t is synced. If 
12333 61 20 63 72 61 73 68 20 6f 63 63 75 72 73 20 64  a crash occurs d
12334 75 72 69 6e 67 20 6f 72 20 66 6f 6c 6c 6f 77 69  uring or followi
12335 6e 67 20 74 68 69 73 2c 0a 20 20 20 20 20 20 2a  ng this,.      *
12336 2a 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75  * database corru
12337 70 74 69 6f 6e 20 6d 61 79 20 65 6e 73 75 65 2e  ption may ensue.
12338 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
12339 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b  sqlite3PcacheMak
1233a 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 20  eClean(pPg);.   
1233b 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45   }.#ifdef SQLITE
1233c 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20 20  _CHECK_PAGES.   
1233d 20 70 50 67 2d 3e 70 61 67 65 48 61 73 68 20 3d   pPg->pageHash =
1233e 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28   pager_pagehash(
1233f 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  pPg);.#endif.   
12340 20 2f 2a 20 49 66 20 74 68 69 73 20 77 61 73 20   /* If this was 
12341 70 61 67 65 20 31 2c 20 74 68 65 6e 20 72 65 73  page 1, then res
12342 74 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20 6f  tore the value o
12343 66 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56 65  f Pager.dbFileVe
12344 72 73 2e 0a 20 20 20 20 2a 2a 20 44 6f 20 74 68  rs..    ** Do th
12345 69 73 20 62 65 66 6f 72 65 20 61 6e 79 20 64 65  is before any de
12346 63 6f 64 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 69  coding. */.    i
12347 66 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20  f( pgno==1 ){.  
12348 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67      memcpy(&pPag
12349 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20  er->dbFileVers, 
1234a 26 28 28 75 38 2a 29 70 44 61 74 61 29 5b 32 34  &((u8*)pData)[24
1234b 5d 2c 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d  ],sizeof(pPager-
1234c 3e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20  >dbFileVers));. 
1234d 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63     }..    /* Dec
1234e 6f 64 65 20 74 68 65 20 70 61 67 65 20 6a 75 73  ode the page jus
1234f 74 20 72 65 61 64 20 66 72 6f 6d 20 64 69 73 6b  t read from disk
12350 20 2a 2f 0a 20 20 20 20 43 4f 44 45 43 31 28 70   */.    CODEC1(p
12351 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50  Pager, pData, pP
12352 67 2d 3e 70 67 6e 6f 2c 20 33 2c 20 72 63 3d 53  g->pgno, 3, rc=S
12353 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20  QLITE_NOMEM);.  
12354 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52    sqlite3PcacheR
12355 65 6c 65 61 73 65 28 70 50 67 29 3b 0a 20 20 7d  elease(pPg);.  }
12356 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
12357 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72  ./*.** Parameter
12358 20 7a 4d 61 73 74 65 72 20 69 73 20 74 68 65 20   zMaster is the 
12359 6e 61 6d 65 20 6f 66 20 61 20 6d 61 73 74 65 72  name of a master
1235a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 41   journal file. A
1235b 20 73 69 6e 67 6c 65 20 6a 6f 75 72 6e 61 6c 0a   single journal.
1235c 2a 2a 20 66 69 6c 65 20 74 68 61 74 20 72 65 66  ** file that ref
1235d 65 72 72 65 64 20 74 6f 20 74 68 65 20 6d 61 73  erred to the mas
1235e 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
1235f 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 72   has just been r
12360 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 20 54  olled back..** T
12361 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63  his routine chec
12362 6b 73 20 69 66 20 69 74 20 69 73 20 70 6f 73 73  ks if it is poss
12363 69 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74  ible to delete t
12364 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
12365 6c 20 66 69 6c 65 2c 0a 2a 2a 20 61 6e 64 20 64  l file,.** and d
12366 6f 65 73 20 73 6f 20 69 66 20 69 74 20 69 73 2e  oes so if it is.
12367 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20  .**.** Argument 
12368 7a 4d 61 73 74 65 72 20 6d 61 79 20 70 6f 69 6e  zMaster may poin
12369 74 20 74 6f 20 50 61 67 65 72 2e 70 54 6d 70 53  t to Pager.pTmpS
1236a 70 61 63 65 2e 20 53 6f 20 74 68 61 74 20 62 75  pace. So that bu
1236b 66 66 65 72 20 69 73 20 6e 6f 74 20 0a 2a 2a 20  ffer is not .** 
1236c 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73  available for us
1236d 65 20 77 69 74 68 69 6e 20 74 68 69 73 20 66 75  e within this fu
1236e 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68  nction..**.** Wh
1236f 65 6e 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72  en a master jour
12370 6e 61 6c 20 66 69 6c 65 20 69 73 20 63 72 65 61  nal file is crea
12371 74 65 64 2c 20 69 74 20 69 73 20 70 6f 70 75 6c  ted, it is popul
12372 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6e 61  ated with the na
12373 6d 65 73 20 0a 2a 2a 20 6f 66 20 61 6c 6c 20 6f  mes .** of all o
12374 66 20 69 74 73 20 63 68 69 6c 64 20 6a 6f 75 72  f its child jour
12375 6e 61 6c 73 2c 20 6f 6e 65 20 61 66 74 65 72 20  nals, one after 
12376 61 6e 6f 74 68 65 72 2c 20 66 6f 72 6d 61 74 74  another, formatt
12377 65 64 20 61 73 20 75 74 66 2d 38 20 0a 2a 2a 20  ed as utf-8 .** 
12378 65 6e 63 6f 64 65 64 20 74 65 78 74 2e 20 54 68  encoded text. Th
12379 65 20 65 6e 64 20 6f 66 20 65 61 63 68 20 63 68  e end of each ch
1237a 69 6c 64 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ild journal file
1237b 20 69 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20   is marked with 
1237c 61 20 0a 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e  a .** nul-termin
1237d 61 74 6f 72 20 62 79 74 65 20 28 30 78 30 30 29  ator byte (0x00)
1237e 2e 20 69 2e 65 2e 20 74 68 65 20 65 6e 74 69 72  . i.e. the entir
1237f 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20  e contents of a 
12380 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a  master journal.*
12381 2a 20 66 69 6c 65 20 66 6f 72 20 61 20 74 72 61  * file for a tra
12382 6e 73 61 63 74 69 6f 6e 20 69 6e 76 6f 6c 76 69  nsaction involvi
12383 6e 67 20 74 77 6f 20 64 61 74 61 62 61 73 65 73  ng two databases
12384 20 6d 69 67 68 74 20 62 65 3a 0a 2a 2a 0a 2a 2a   might be:.**.**
12385 20 20 20 22 2f 68 6f 6d 65 2f 62 69 6c 6c 2f 61     "/home/bill/a
12386 2e 64 62 2d 6a 6f 75 72 6e 61 6c 5c 78 30 30 2f  .db-journal\x00/
12387 68 6f 6d 65 2f 62 69 6c 6c 2f 62 2e 64 62 2d 6a  home/bill/b.db-j
12388 6f 75 72 6e 61 6c 5c 78 30 30 22 0a 2a 2a 0a 2a  ournal\x00".**.*
12389 2a 20 41 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  * A master journ
1238a 61 6c 20 66 69 6c 65 20 6d 61 79 20 6f 6e 6c 79  al file may only
1238b 20 62 65 20 64 65 6c 65 74 65 64 20 6f 6e 63 65   be deleted once
1238c 20 61 6c 6c 20 6f 66 20 69 74 73 20 63 68 69 6c   all of its chil
1238d 64 20 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 73 20 68  d .** journals h
1238e 61 76 65 20 62 65 65 6e 20 72 6f 6c 6c 65 64 20  ave been rolled 
1238f 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  back..**.** This
12390 20 66 75 6e 63 74 69 6f 6e 20 72 65 61 64 73 20   function reads 
12391 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
12392 74 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e  the master-journ
12393 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 0a 2a 2a  al file into .**
12394 20 6d 65 6d 6f 72 79 20 61 6e 64 20 6c 6f 6f 70   memory and loop
12395 73 20 74 68 72 6f 75 67 68 20 65 61 63 68 20 6f  s through each o
12396 66 20 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72  f the child jour
12397 6e 61 6c 20 6e 61 6d 65 73 2e 20 46 6f 72 0a 2a  nal names. For.*
12398 2a 20 65 61 63 68 20 63 68 69 6c 64 20 6a 6f 75  * each child jou
12399 72 6e 61 6c 2c 20 69 74 20 63 68 65 63 6b 73 20  rnal, it checks 
1239a 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 69 66  if:.**.**   * if
1239b 20 74 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e   the child journ
1239c 61 6c 20 65 78 69 73 74 73 2c 20 61 6e 64 20 69  al exists, and i
1239d 66 20 73 6f 0a 2a 2a 20 20 20 2a 20 69 66 20 74  f so.**   * if t
1239e 68 65 20 63 68 69 6c 64 20 6a 6f 75 72 6e 61 6c  he child journal
1239f 20 63 6f 6e 74 61 69 6e 73 20 61 20 72 65 66 65   contains a refe
123a0 72 65 6e 63 65 20 74 6f 20 6d 61 73 74 65 72 20  rence to master 
123a1 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20  journal .**     
123a2 66 69 6c 65 20 7a 4d 61 73 74 65 72 0a 2a 2a 0a  file zMaster.**.
123a3 2a 2a 20 49 66 20 61 20 63 68 69 6c 64 20 6a 6f  ** If a child jo
123a4 75 72 6e 61 6c 20 63 61 6e 20 62 65 20 66 6f 75  urnal can be fou
123a5 6e 64 20 74 68 61 74 20 6d 61 74 63 68 65 73 20  nd that matches 
123a6 62 6f 74 68 20 6f 66 20 74 68 65 20 63 72 69 74  both of the crit
123a7 65 72 69 61 0a 2a 2a 20 61 62 6f 76 65 2c 20 74  eria.** above, t
123a8 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
123a9 75 72 6e 73 20 77 69 74 68 6f 75 74 20 64 6f 69  urns without doi
123aa 6e 67 20 61 6e 79 74 68 69 6e 67 2e 20 4f 74 68  ng anything. Oth
123ab 65 72 77 69 73 65 2c 20 69 66 0a 2a 2a 20 6e 6f  erwise, if.** no
123ac 20 73 75 63 68 20 63 68 69 6c 64 20 6a 6f 75 72   such child jour
123ad 6e 61 6c 20 63 61 6e 20 62 65 20 66 6f 75 6e 64  nal can be found
123ae 2c 20 66 69 6c 65 20 7a 4d 61 73 74 65 72 20 69  , file zMaster i
123af 73 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 0a 2a  s deleted from.*
123b0 2a 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65  * the file-syste
123b1 6d 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4f  m using sqlite3O
123b2 73 44 65 6c 65 74 65 28 29 2e 0a 2a 2a 0a 2a 2a  sDelete()..**.**
123b3 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20   If an IO error 
123b4 77 69 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63  within this func
123b5 74 69 6f 6e 2c 20 61 6e 20 65 72 72 6f 72 20 63  tion, an error c
123b6 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
123b7 20 54 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f   This.** functio
123b8 6e 20 61 6c 6c 6f 63 61 74 65 73 20 6d 65 6d 6f  n allocates memo
123b9 72 79 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71  ry by calling sq
123ba 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 29 2e 20 49  lite3Malloc(). I
123bb 66 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a  f an allocation.
123bc 2a 2a 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45  ** fails, SQLITE
123bd 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e  _NOMEM is return
123be 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  ed. Otherwise, i
123bf 66 20 6e 6f 20 49 4f 20 6f 72 20 6d 61 6c 6c 6f  f no IO or mallo
123c0 63 20 65 72 72 6f 72 73 20 0a 2a 2a 20 6f 63 63  c errors .** occ
123c1 75 72 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  ur, SQLITE_OK is
123c2 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
123c3 20 54 4f 44 4f 3a 20 54 68 69 73 20 66 75 6e 63   TODO: This func
123c4 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 73 20 61  tion allocates a
123c5 20 73 69 6e 67 6c 65 20 62 6c 6f 63 6b 20 6f 66   single block of
123c6 20 6d 65 6d 6f 72 79 20 74 6f 20 6c 6f 61 64 0a   memory to load.
123c7 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20 63 6f  ** the entire co
123c8 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 6d 61  ntents of the ma
123c9 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
123ca 65 2e 20 54 68 69 73 20 63 6f 75 6c 64 20 62 65  e. This could be
123cb 0a 2a 2a 20 61 20 63 6f 75 70 6c 65 20 6f 66 20  .** a couple of 
123cc 6b 69 6c 6f 62 79 74 65 73 20 6f 72 20 73 6f 20  kilobytes or so 
123cd 2d 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 6c 61  - potentially la
123ce 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 70 61  rger than the pa
123cf 67 65 20 0a 2a 2a 20 73 69 7a 65 2e 0a 2a 2f 0a  ge .** size..*/.
123d0 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
123d1 5f 64 65 6c 6d 61 73 74 65 72 28 50 61 67 65 72  _delmaster(Pager
123d2 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20   *pPager, const 
123d3 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b 0a  char *zMaster){.
123d4 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
123d5 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70 56  Vfs = pPager->pV
123d6 66 73 3b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  fs;.  int rc;   
123d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
123d8 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
123d9 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  /.  sqlite3_file
123da 20 2a 70 4d 61 73 74 65 72 3b 20 20 20 20 2f 2a   *pMaster;    /*
123db 20 4d 61 6c 6c 6f 63 27 64 20 6d 61 73 74 65 72   Malloc'd master
123dc 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 64 65  -journal file de
123dd 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 73 71  scriptor */.  sq
123de 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 6f 75  lite3_file *pJou
123df 72 6e 61 6c 3b 20 20 20 2f 2a 20 4d 61 6c 6c 6f  rnal;   /* Mallo
123e0 63 27 64 20 63 68 69 6c 64 2d 6a 6f 75 72 6e 61  c'd child-journa
123e1 6c 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  l file descripto
123e2 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d 61  r */.  char *zMa
123e3 73 74 65 72 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b  sterJournal = 0;
123e4 20 2f 2a 20 43 6f 6e 74 65 6e 74 73 20 6f 66 20   /* Contents of 
123e5 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
123e6 69 6c 65 20 2a 2f 0a 20 20 69 36 34 20 6e 4d 61  ile */.  i64 nMa
123e7 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 20 20 20 20  sterJournal;    
123e8 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6d 61     /* Size of ma
123e9 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
123ea 65 20 2a 2f 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  e */..  /* Alloc
123eb 61 74 65 20 73 70 61 63 65 20 66 6f 72 20 62 6f  ate space for bo
123ec 74 68 20 74 68 65 20 70 4a 6f 75 72 6e 61 6c 20  th the pJournal 
123ed 61 6e 64 20 70 4d 61 73 74 65 72 20 66 69 6c 65  and pMaster file
123ee 20 64 65 73 63 72 69 70 74 6f 72 73 2e 0a 20 20   descriptors..  
123ef 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  ** If successful
123f0 2c 20 6f 70 65 6e 20 74 68 65 20 6d 61 73 74 65  , open the maste
123f1 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66  r journal file f
123f2 6f 72 20 72 65 61 64 69 6e 67 2e 0a 20 20 2a 2f  or reading..  */
123f3 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 28 73 71  .  pMaster = (sq
123f4 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 73 71 6c  lite3_file *)sql
123f5 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70  ite3MallocZero(p
123f6 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 2a 20  Vfs->szOsFile * 
123f7 32 29 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c 20 3d  2);.  pJournal =
123f8 20 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a   (sqlite3_file *
123f9 29 28 28 28 75 38 20 2a 29 70 4d 61 73 74 65 72  )(((u8 *)pMaster
123fa 29 20 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69  ) + pVfs->szOsFi
123fb 6c 65 29 3b 0a 20 20 69 66 28 20 21 70 4d 61 73  le);.  if( !pMas
123fc 74 65 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ter ){.    rc = 
123fd 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
123fe 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74  }else{.    const
123ff 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 28 53 51   int flags = (SQ
12400 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e  LITE_OPEN_READON
12401 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  LY|SQLITE_OPEN_M
12402 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 29 3b 0a  ASTER_JOURNAL);.
12403 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
12404 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 7a 4d 61  OsOpen(pVfs, zMa
12405 73 74 65 72 2c 20 70 4d 61 73 74 65 72 2c 20 66  ster, pMaster, f
12406 6c 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  lags, 0);.  }.  
12407 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
12408 4b 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74  K ) goto delmast
12409 65 72 5f 6f 75 74 3b 0a 0a 20 20 72 63 20 3d 20  er_out;..  rc = 
1240a 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a  sqlite3OsFileSiz
1240b 65 28 70 4d 61 73 74 65 72 2c 20 26 6e 4d 61 73  e(pMaster, &nMas
1240c 74 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 69  terJournal);.  i
1240d 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1240e 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65   ) goto delmaste
1240f 72 5f 6f 75 74 3b 0a 0a 20 20 69 66 28 20 6e 4d  r_out;..  if( nM
12410 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3e 30 20 29  asterJournal>0 )
12411 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4a 6f 75  {.    char *zJou
12412 72 6e 61 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a  rnal;.    char *
12413 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 30 3b 0a  zMasterPtr = 0;.
12414 20 20 20 20 69 6e 74 20 6e 4d 61 73 74 65 72 50      int nMasterP
12415 74 72 20 3d 20 70 56 66 73 2d 3e 6d 78 50 61 74  tr = pVfs->mxPat
12416 68 6e 61 6d 65 2b 31 3b 0a 0a 20 20 20 20 2f 2a  hname+1;..    /*
12417 20 4c 6f 61 64 20 74 68 65 20 65 6e 74 69 72 65   Load the entire
12418 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
12419 66 69 6c 65 20 69 6e 74 6f 20 73 70 61 63 65 20  file into space 
1241a 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 20 20  obtained from.  
1241b 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c    ** sqlite3_mal
1241c 6c 6f 63 28 29 20 61 6e 64 20 70 6f 69 6e 74 65  loc() and pointe
1241d 64 20 74 6f 20 62 79 20 7a 4d 61 73 74 65 72 4a  d to by zMasterJ
1241e 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20 2a 2f 0a  ournal. .    */.
1241f 20 20 20 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e      zMasterJourn
12420 61 6c 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  al = sqlite3Mall
12421 6f 63 28 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a  oc((int)nMasterJ
12422 6f 75 72 6e 61 6c 20 2b 20 6e 4d 61 73 74 65 72  ournal + nMaster
12423 50 74 72 20 2b 20 31 29 3b 0a 20 20 20 20 69 66  Ptr + 1);.    if
12424 28 20 21 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61  ( !zMasterJourna
12425 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  l ){.      rc = 
12426 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
12427 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74      goto delmast
12428 65 72 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  er_out;.    }.  
12429 20 20 7a 4d 61 73 74 65 72 50 74 72 20 3d 20 26    zMasterPtr = &
1242a 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 5b 6e  zMasterJournal[n
1242b 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 2b 31 5d  MasterJournal+1]
1242c 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1242d 65 33 4f 73 52 65 61 64 28 70 4d 61 73 74 65 72  e3OsRead(pMaster
1242e 2c 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  , zMasterJournal
1242f 2c 20 28 69 6e 74 29 6e 4d 61 73 74 65 72 4a 6f  , (int)nMasterJo
12430 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20 69  urnal, 0);.    i
12431 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
12432 20 29 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65   ) goto delmaste
12433 72 5f 6f 75 74 3b 0a 20 20 20 20 7a 4d 61 73 74  r_out;.    zMast
12434 65 72 4a 6f 75 72 6e 61 6c 5b 6e 4d 61 73 74 65  erJournal[nMaste
12435 72 4a 6f 75 72 6e 61 6c 5d 20 3d 20 30 3b 0a 0a  rJournal] = 0;..
12436 20 20 20 20 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a      zJournal = z
12437 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 3b 0a 20  MasterJournal;. 
12438 20 20 20 77 68 69 6c 65 28 20 28 7a 4a 6f 75 72     while( (zJour
12439 6e 61 6c 2d 7a 4d 61 73 74 65 72 4a 6f 75 72 6e  nal-zMasterJourn
1243a 61 6c 29 3c 6e 4d 61 73 74 65 72 4a 6f 75 72 6e  al)<nMasterJourn
1243b 61 6c 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  al ){.      int 
1243c 65 78 69 73 74 73 3b 0a 20 20 20 20 20 20 72 63  exists;.      rc
1243d 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65   = sqlite3OsAcce
1243e 73 73 28 70 56 66 73 2c 20 7a 4a 6f 75 72 6e 61  ss(pVfs, zJourna
1243f 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  l, SQLITE_ACCESS
12440 5f 45 58 49 53 54 53 2c 20 26 65 78 69 73 74 73  _EXISTS, &exists
12441 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
12442 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
12443 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61        goto delma
12444 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20  ster_out;.      
12445 7d 0a 20 20 20 20 20 20 69 66 28 20 65 78 69 73  }.      if( exis
12446 74 73 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  ts ){.        /*
12447 20 4f 6e 65 20 6f 66 20 74 68 65 20 6a 6f 75 72   One of the jour
12448 6e 61 6c 73 20 70 6f 69 6e 74 65 64 20 74 6f 20  nals pointed to 
12449 62 79 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  by the master jo
1244a 75 72 6e 61 6c 20 65 78 69 73 74 73 2e 0a 20 20  urnal exists..  
1244b 20 20 20 20 20 20 2a 2a 20 4f 70 65 6e 20 69 74        ** Open it
1244c 20 61 6e 64 20 63 68 65 63 6b 20 69 66 20 69 74   and check if it
1244d 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20 6d   points at the m
1244e 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 49  aster journal. I
1244f 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 6f 2c  f.        ** so,
12450 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20   return without 
12451 64 65 6c 65 74 69 6e 67 20 74 68 65 20 6d 61 73  deleting the mas
12452 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
12453 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
12454 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 20 20 20       int c;.    
12455 20 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20      int flags = 
12456 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  (SQLITE_OPEN_REA
12457 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f 50 45  DONLY|SQLITE_OPE
12458 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 29 3b  N_MAIN_JOURNAL);
12459 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
1245a 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73  lite3OsOpen(pVfs
1245b 2c 20 7a 4a 6f 75 72 6e 61 6c 2c 20 70 4a 6f 75  , zJournal, pJou
1245c 72 6e 61 6c 2c 20 66 6c 61 67 73 2c 20 30 29 3b  rnal, flags, 0);
1245d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
1245e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1245f 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c          goto del
12460 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20  master_out;.    
12461 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 72      }..        r
12462 63 20 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f  c = readMasterJo
12463 75 72 6e 61 6c 28 70 4a 6f 75 72 6e 61 6c 2c 20  urnal(pJournal, 
12464 7a 4d 61 73 74 65 72 50 74 72 2c 20 6e 4d 61 73  zMasterPtr, nMas
12465 74 65 72 50 74 72 29 3b 0a 20 20 20 20 20 20 20  terPtr);.       
12466 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
12467 70 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20  pJournal);.     
12468 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
12469 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1246a 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74 65 72    goto delmaster
1246b 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  _out;.        }.
1246c 0a 20 20 20 20 20 20 20 20 63 20 3d 20 7a 4d 61  .        c = zMa
1246d 73 74 65 72 50 74 72 5b 30 5d 21 3d 30 20 26 26  sterPtr[0]!=0 &&
1246e 20 73 74 72 63 6d 70 28 7a 4d 61 73 74 65 72 50   strcmp(zMasterP
1246f 74 72 2c 20 7a 4d 61 73 74 65 72 29 3d 3d 30 3b  tr, zMaster)==0;
12470 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 20 29  .        if( c )
12471 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57  {.          /* W
12472 65 20 68 61 76 65 20 61 20 6d 61 74 63 68 2e 20  e have a match. 
12473 44 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 74 68  Do not delete th
12474 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
12475 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 20 20   file. */.      
12476 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73 74      goto delmast
12477 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20 20  er_out;.        
12478 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
12479 7a 4a 6f 75 72 6e 61 6c 20 2b 3d 20 28 73 71 6c  zJournal += (sql
1247a 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4a 6f  ite3Strlen30(zJo
1247b 75 72 6e 61 6c 29 2b 31 29 3b 0a 20 20 20 20 7d  urnal)+1);.    }
1247c 0a 20 20 7d 0a 20 20 0a 20 20 72 63 20 3d 20 73  .  }.  .  rc = s
1247d 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 70  qlite3OsDelete(p
1247e 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 30 29  Vfs, zMaster, 0)
1247f 3b 0a 0a 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74  ;..delmaster_out
12480 3a 0a 20 20 69 66 28 20 7a 4d 61 73 74 65 72 4a  :.  if( zMasterJ
12481 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71  ournal ){.    sq
12482 6c 69 74 65 33 5f 66 72 65 65 28 7a 4d 61 73 74  lite3_free(zMast
12483 65 72 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 7d 20  erJournal);.  } 
12484 20 0a 20 20 69 66 28 20 70 4d 61 73 74 65 72 20   .  if( pMaster 
12485 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73  ){.    sqlite3Os
12486 43 6c 6f 73 65 28 70 4d 61 73 74 65 72 29 3b 0a  Close(pMaster);.
12487 20 20 20 20 61 73 73 65 72 74 28 20 21 69 73 4f      assert( !isO
12488 70 65 6e 28 70 4a 6f 75 72 6e 61 6c 29 20 29 3b  pen(pJournal) );
12489 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66  .  }.  sqlite3_f
1248a 72 65 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20  ree(pMaster);.  
1248b 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
1248c 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
1248d 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 63 68  on is used to ch
1248e 61 6e 67 65 20 74 68 65 20 61 63 74 75 61 6c 20  ange the actual 
1248f 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61  size of the data
12490 62 61 73 65 20 0a 2a 2a 20 66 69 6c 65 20 69 6e  base .** file in
12491 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d   the file-system
12492 2e 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70  . This only happ
12493 65 6e 73 20 77 68 65 6e 20 63 6f 6d 6d 69 74 74  ens when committ
12494 69 6e 67 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ing a transactio
12495 6e 2c 0a 2a 2a 20 6f 72 20 72 6f 6c 6c 69 6e 67  n,.** or rolling
12496 20 62 61 63 6b 20 61 20 74 72 61 6e 73 61 63 74   back a transact
12497 69 6f 6e 20 28 69 6e 63 6c 75 64 69 6e 67 20 72  ion (including r
12498 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 68 6f  olling back a ho
12499 74 2d 6a 6f 75 72 6e 61 6c 29 2e 0a 2a 2a 0a 2a  t-journal)..**.*
1249a 2a 20 49 66 20 74 68 65 20 6d 61 69 6e 20 64 61  * If the main da
1249b 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6e  tabase file is n
1249c 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 61 6e 20 65  ot open, or an e
1249d 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 69 73  xclusive lock is
1249e 20 6e 6f 74 0a 2a 2a 20 68 65 6c 64 2c 20 74 68   not.** held, th
1249f 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  is function is a
124a0 20 6e 6f 2d 6f 70 2e 20 4f 74 68 65 72 77 69 73   no-op. Otherwis
124a1 65 2c 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  e, the size of t
124a2 68 65 20 66 69 6c 65 20 69 73 0a 2a 2a 20 63 68  he file is.** ch
124a3 61 6e 67 65 64 20 74 6f 20 6e 50 61 67 65 20 70  anged to nPage p
124a4 61 67 65 73 20 28 6e 50 61 67 65 2a 70 50 61 67  ages (nPage*pPag
124a5 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 62 79 74  er->pageSize byt
124a6 65 73 29 2e 20 49 66 20 74 68 65 20 66 69 6c 65  es). If the file
124a7 0a 2a 2a 20 6f 6e 20 64 69 73 6b 20 69 73 20 63  .** on disk is c
124a8 75 72 72 65 6e 74 6c 79 20 6c 61 72 67 65 72 20  urrently larger 
124a9 74 68 61 6e 20 6e 50 61 67 65 20 70 61 67 65 73  than nPage pages
124aa 2c 20 74 68 65 6e 20 75 73 65 20 74 68 65 20 56  , then use the V
124ab 46 53 0a 2a 2a 20 78 54 72 75 6e 63 61 74 65 28  FS.** xTruncate(
124ac 29 20 6d 65 74 68 6f 64 20 74 6f 20 74 72 75 6e  ) method to trun
124ad 63 61 74 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 4f  cate it..**.** O
124ae 72 2c 20 69 74 20 6d 69 67 68 74 20 6d 69 67 68  r, it might migh
124af 74 20 62 65 20 74 68 65 20 63 61 73 65 20 74 68  t be the case th
124b0 61 74 20 74 68 65 20 66 69 6c 65 20 6f 6e 20 64  at the file on d
124b1 69 73 6b 20 69 73 20 73 6d 61 6c 6c 65 72 20 74  isk is smaller t
124b2 68 61 6e 20 0a 2a 2a 20 6e 50 61 67 65 20 70 61  han .** nPage pa
124b3 67 65 73 2e 20 53 6f 6d 65 20 6f 70 65 72 61 74  ges. Some operat
124b4 69 6e 67 20 73 79 73 74 65 6d 20 69 6d 70 6c 65  ing system imple
124b5 6d 65 6e 74 61 74 69 6f 6e 73 20 63 61 6e 20 67  mentations can g
124b6 65 74 20 63 6f 6e 66 75 73 65 64 20 69 66 20 0a  et confused if .
124b7 2a 2a 20 79 6f 75 20 74 72 79 20 74 6f 20 74 72  ** you try to tr
124b8 75 6e 63 61 74 65 20 61 20 66 69 6c 65 20 74 6f  uncate a file to
124b9 20 73 6f 6d 65 20 73 69 7a 65 20 74 68 61 74 20   some size that 
124ba 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 69  is larger than i
124bb 74 20 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20  t .** currently 
124bc 69 73 2c 20 73 6f 20 64 65 74 65 63 74 20 74 68  is, so detect th
124bd 69 73 20 63 61 73 65 20 61 6e 64 20 77 72 69 74  is case and writ
124be 65 20 61 20 73 69 6e 67 6c 65 20 7a 65 72 6f 20  e a single zero 
124bf 62 79 74 65 20 74 6f 20 0a 2a 2a 20 74 68 65 20  byte to .** the 
124c0 65 6e 64 20 6f 66 20 74 68 65 20 6e 65 77 20 66  end of the new f
124c1 69 6c 65 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a  ile instead..**.
124c2 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c  ** If successful
124c3 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
124c4 4f 4b 2e 20 49 66 20 61 6e 20 49 4f 20 65 72 72  OK. If an IO err
124c5 6f 72 20 6f 63 63 75 72 73 20 77 68 69 6c 65 20  or occurs while 
124c6 6d 6f 64 69 66 79 69 6e 67 0a 2a 2a 20 74 68 65  modifying.** the
124c7 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
124c8 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72  return the error
124c9 20 63 6f 64 65 20 74 6f 20 74 68 65 20 63 61 6c   code to the cal
124ca 6c 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ler..*/.static i
124cb 6e 74 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74  nt pager_truncat
124cc 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  e(Pager *pPager,
124cd 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20 20   Pgno nPage){.  
124ce 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
124cf 4f 4b 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72  OK;.  if( pPager
124d0 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45  ->state>=PAGER_E
124d1 58 43 4c 55 53 49 56 45 20 26 26 20 69 73 4f 70  XCLUSIVE && isOp
124d2 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 29  en(pPager->fd) )
124d3 7b 0a 20 20 20 20 69 36 34 20 63 75 72 72 65 6e  {.    i64 curren
124d4 74 53 69 7a 65 2c 20 6e 65 77 53 69 7a 65 3b 0a  tSize, newSize;.
124d5 20 20 20 20 2f 2a 20 54 4f 44 4f 3a 20 49 73 20      /* TODO: Is 
124d6 69 74 20 73 61 66 65 20 74 6f 20 75 73 65 20 50  it safe to use P
124d7 61 67 65 72 2e 64 62 46 69 6c 65 53 69 7a 65 20  ager.dbFileSize 
124d8 68 65 72 65 3f 20 2a 2f 0a 20 20 20 20 72 63 20  here? */.    rc 
124d9 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53  = sqlite3OsFileS
124da 69 7a 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20  ize(pPager->fd, 
124db 26 63 75 72 72 65 6e 74 53 69 7a 65 29 3b 0a 20  &currentSize);. 
124dc 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 70 50 61     newSize = pPa
124dd 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2a 28 69  ger->pageSize*(i
124de 36 34 29 6e 50 61 67 65 3b 0a 20 20 20 20 69 66  64)nPage;.    if
124df 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
124e0 26 26 20 63 75 72 72 65 6e 74 53 69 7a 65 21 3d  && currentSize!=
124e1 6e 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 20 20  newSize ){.     
124e2 20 69 66 28 20 63 75 72 72 65 6e 74 53 69 7a 65   if( currentSize
124e3 3e 6e 65 77 53 69 7a 65 20 29 7b 0a 20 20 20 20  >newSize ){.    
124e4 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
124e5 4f 73 54 72 75 6e 63 61 74 65 28 70 50 61 67 65  OsTruncate(pPage
124e6 72 2d 3e 66 64 2c 20 6e 65 77 53 69 7a 65 29 3b  r->fd, newSize);
124e7 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
124e8 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
124e9 65 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72  e3OsWrite(pPager
124ea 2d 3e 66 64 2c 20 22 22 2c 20 31 2c 20 6e 65 77  ->fd, "", 1, new
124eb 53 69 7a 65 2d 31 29 3b 0a 20 20 20 20 20 20 7d  Size-1);.      }
124ec 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
124ed 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
124ee 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 46 69      pPager->dbFi
124ef 6c 65 53 69 7a 65 20 3d 20 6e 50 61 67 65 3b 0a  leSize = nPage;.
124f0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
124f1 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
124f2 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20  ../*.** Set the 
124f3 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 61 67  value of the Pag
124f4 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 61  er.sectorSize va
124f5 72 69 61 62 6c 65 20 66 6f 72 20 74 68 65 20 67  riable for the g
124f6 69 76 65 6e 0a 2a 2a 20 70 61 67 65 72 20 62 61  iven.** pager ba
124f7 73 65 64 20 6f 6e 20 74 68 65 20 76 61 6c 75 65  sed on the value
124f8 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
124f9 20 78 53 65 63 74 6f 72 53 69 7a 65 20 6d 65 74   xSectorSize met
124fa 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20 6f 70  hod.** of the op
124fb 65 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  en database file
124fc 2e 20 54 68 65 20 73 65 63 74 6f 72 20 73 69 7a  . The sector siz
124fd 65 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 75  e will be used u
124fe 73 65 64 20 0a 2a 2a 20 74 6f 20 64 65 74 65 72  sed .** to deter
124ff 6d 69 6e 65 20 74 68 65 20 73 69 7a 65 20 61 6e  mine the size an
12500 64 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 6a  d alignment of j
12501 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 61 6e  ournal header an
12502 64 20 0a 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75  d .** master jou
12503 72 6e 61 6c 20 70 6f 69 6e 74 65 72 73 20 77 69  rnal pointers wi
12504 74 68 69 6e 20 63 72 65 61 74 65 64 20 6a 6f 75  thin created jou
12505 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a  rnal files..**.*
12506 2a 20 46 6f 72 20 74 65 6d 70 6f 72 61 72 79 20  * For temporary 
12507 66 69 6c 65 73 20 74 68 65 20 65 66 66 65 63 74  files the effect
12508 69 76 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20  ive sector size 
12509 69 73 20 61 6c 77 61 79 73 20 35 31 32 20 62 79  is always 512 by
1250a 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72  tes..**.** Other
1250b 77 69 73 65 2c 20 66 6f 72 20 6e 6f 6e 2d 74 65  wise, for non-te
1250c 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2c 20 74  mporary files, t
1250d 68 65 20 65 66 66 65 63 74 69 76 65 20 73 65 63  he effective sec
1250e 74 6f 72 20 73 69 7a 65 20 69 73 0a 2a 2a 20 74  tor size is.** t
1250f 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
12510 64 20 62 79 20 74 68 65 20 78 53 65 63 74 6f 72  d by the xSector
12511 53 69 7a 65 28 29 20 6d 65 74 68 6f 64 20 72 6f  Size() method ro
12512 75 6e 64 65 64 20 75 70 20 74 6f 20 35 31 32 20  unded up to 512 
12513 69 66 0a 2a 2a 20 69 74 20 69 73 20 6c 65 73 73  if.** it is less
12514 20 74 68 61 6e 20 35 31 32 2c 20 6f 72 20 72 6f   than 512, or ro
12515 75 6e 64 65 64 20 64 6f 77 6e 20 74 6f 20 4d 41  unded down to MA
12516 58 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 69 66  X_SECTOR_SIZE if
12517 20 69 74 0a 2a 2a 20 69 73 20 67 72 65 61 74 65   it.** is greate
12518 72 20 74 68 61 6e 20 4d 41 58 5f 53 45 43 54 4f  r than MAX_SECTO
12519 52 5f 53 49 5a 45 2e 0a 2a 2f 0a 73 74 61 74 69  R_SIZE..*/.stati
1251a 63 20 76 6f 69 64 20 73 65 74 53 65 63 74 6f 72  c void setSector
1251b 53 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67  Size(Pager *pPag
1251c 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69  er){.  assert( i
1251d 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64  sOpen(pPager->fd
1251e 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d  ) || pPager->tem
1251f 70 46 69 6c 65 20 29 3b 0a 0a 20 20 69 66 28 20  pFile );..  if( 
12520 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c  !pPager->tempFil
12521 65 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 63 74  e ){.    /* Sect
12522 6f 72 20 73 69 7a 65 20 64 6f 65 73 6e 27 74 20  or size doesn't 
12523 6d 61 74 74 65 72 20 66 6f 72 20 74 65 6d 70 6f  matter for tempo
12524 72 61 72 79 20 66 69 6c 65 73 2e 20 41 6c 73 6f  rary files. Also
12525 2c 20 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a  , the file.    *
12526 2a 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 62  * may not have b
12527 65 65 6e 20 6f 70 65 6e 65 64 20 79 65 74 2c 20  een opened yet, 
12528 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68  in which case th
12529 65 20 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29  e OsSectorSize()
1252a 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c  .    ** call wil
1252b 6c 20 73 65 67 66 61 75 6c 74 2e 0a 20 20 20 20  l segfault..    
1252c 2a 2f 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73  */.    pPager->s
1252d 65 63 74 6f 72 53 69 7a 65 20 3d 20 73 71 6c 69  ectorSize = sqli
1252e 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28  te3OsSectorSize(
1252f 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 7d  pPager->fd);.  }
12530 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
12531 65 63 74 6f 72 53 69 7a 65 3c 35 31 32 20 29 7b  ectorSize<512 ){
12532 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63  .    pPager->sec
12533 74 6f 72 53 69 7a 65 20 3d 20 35 31 32 3b 0a 20  torSize = 512;. 
12534 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d   }.  if( pPager-
12535 3e 73 65 63 74 6f 72 53 69 7a 65 3e 4d 41 58 5f  >sectorSize>MAX_
12536 53 45 43 54 4f 52 5f 53 49 5a 45 20 29 7b 0a 20  SECTOR_SIZE ){. 
12537 20 20 20 61 73 73 65 72 74 28 20 4d 41 58 5f 53     assert( MAX_S
12538 45 43 54 4f 52 5f 53 49 5a 45 3e 3d 35 31 32 20  ECTOR_SIZE>=512 
12539 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73  );.    pPager->s
1253a 65 63 74 6f 72 53 69 7a 65 20 3d 20 4d 41 58 5f  ectorSize = MAX_
1253b 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 20 20 7d  SECTOR_SIZE;.  }
1253c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61  .}../*.** Playba
1253d 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61  ck the journal a
1253e 6e 64 20 74 68 75 73 20 72 65 73 74 6f 72 65 20  nd thus restore 
1253f 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
12540 65 20 74 6f 0a 2a 2a 20 74 68 65 20 73 74 61 74  e to.** the stat
12541 65 20 69 74 20 77 61 73 20 69 6e 20 62 65 66 6f  e it was in befo
12542 72 65 20 77 65 20 73 74 61 72 74 65 64 20 6d 61  re we started ma
12543 6b 69 6e 67 20 63 68 61 6e 67 65 73 2e 20 20 0a  king changes.  .
12544 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61  **.** The journa
12545 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 69 73  l file format is
12546 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 0a 2a 2a   as follows: .**
12547 0a 2a 2a 20 20 28 31 29 20 20 38 20 62 79 74 65  .**  (1)  8 byte
12548 20 70 72 65 66 69 78 2e 20 20 41 20 63 6f 70 79   prefix.  A copy
12549 20 6f 66 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69   of aJournalMagi
1254a 63 5b 5d 2e 0a 2a 2a 20 20 28 32 29 20 20 34 20  c[]..**  (2)  4 
1254b 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20  byte big-endian 
1254c 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73  integer which is
1254d 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76   the number of v
1254e 61 6c 69 64 20 70 61 67 65 20 72 65 63 6f 72 64  alid page record
1254f 73 0a 2a 2a 20 20 20 20 20 20 20 69 6e 20 74 68  s.**       in th
12550 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 66 20 74  e journal.  If t
12551 68 69 73 20 76 61 6c 75 65 20 69 73 20 30 78 66  his value is 0xf
12552 66 66 66 66 66 66 66 2c 20 74 68 65 6e 20 63 6f  fffffff, then co
12553 6d 70 75 74 65 20 74 68 65 0a 2a 2a 20 20 20 20  mpute the.**    
12554 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67     number of pag
12555 65 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74  e records from t
12556 68 65 20 6a 6f 75 72 6e 61 6c 20 73 69 7a 65 2e  he journal size.
12557 0a 2a 2a 20 20 28 33 29 20 20 34 20 62 79 74 65  .**  (3)  4 byte
12558 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65   big-endian inte
12559 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65  ger which is the
1255a 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 66   initial value f
1255b 6f 72 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20  or the .**      
1255c 20 73 61 6e 69 74 79 20 63 68 65 63 6b 73 75 6d   sanity checksum
1255d 2e 0a 2a 2a 20 20 28 34 29 20 20 34 20 62 79 74  ..**  (4)  4 byt
1255e 65 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20  e integer which 
1255f 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
12560 20 70 61 67 65 73 20 74 6f 20 74 72 75 6e 63 61   pages to trunca
12561 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  te the.**       
12562 64 61 74 61 62 61 73 65 20 74 6f 20 64 75 72 69  database to duri
12563 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a  ng a rollback..*
12564 2a 20 20 28 35 29 20 20 34 20 62 79 74 65 20 62  *  (5)  4 byte b
12565 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65  ig-endian intege
12566 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 73  r which is the s
12567 65 63 74 6f 72 20 73 69 7a 65 2e 20 20 54 68 65  ector size.  The
12568 20 68 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 20   header.**      
12569 20 69 73 20 74 68 69 73 20 6d 61 6e 79 20 62 79   is this many by
1256a 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20  tes in size..** 
1256b 20 28 36 29 20 20 34 20 62 79 74 65 20 62 69 67   (6)  4 byte big
1256c 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20  -endian integer 
1256d 77 68 69 63 68 20 69 73 20 74 68 65 20 70 61 67  which is the pag
1256e 65 20 63 61 73 65 2e 0a 2a 2a 20 20 28 37 29 20  e case..**  (7) 
1256f 20 34 20 62 79 74 65 20 69 6e 74 65 67 65 72 20   4 byte integer 
12570 77 68 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d  which is the num
12571 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
12572 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
12573 61 6c 0a 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65  al.**       name
12574 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6d 61 79  .  The value may
12575 20 62 65 20 7a 65 72 6f 20 28 69 6e 64 69 63 61   be zero (indica
12576 74 65 20 74 68 61 74 20 74 68 65 72 65 20 69 73  te that there is
12577 20 6e 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 20 20   no master.**   
12578 20 20 20 20 6a 6f 75 72 6e 61 6c 2e 29 0a 2a 2a      journal.).**
12579 20 20 28 38 29 20 20 4e 20 62 79 74 65 73 20 6f    (8)  N bytes o
1257a 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  f the master jou
1257b 72 6e 61 6c 20 6e 61 6d 65 2e 20 20 54 68 65 20  rnal name.  The 
1257c 6e 61 6d 65 20 77 69 6c 6c 20 62 65 20 6e 75 6c  name will be nul
1257d 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 20  -terminated.**  
1257e 20 20 20 20 20 61 6e 64 20 6d 69 67 68 74 20 62       and might b
1257f 65 20 73 68 6f 72 74 65 72 20 74 68 61 6e 20 74  e shorter than t
12580 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72  he value read fr
12581 6f 6d 20 28 35 29 2e 20 20 49 66 20 74 68 65 20  om (5).  If the 
12582 66 69 72 73 74 20 62 79 74 65 0a 2a 2a 20 20 20  first byte.**   
12583 20 20 20 20 6f 66 20 74 68 65 20 6e 61 6d 65 20      of the name 
12584 69 73 20 5c 30 30 30 20 74 68 65 6e 20 74 68 65  is \000 then the
12585 72 65 20 69 73 20 6e 6f 20 6d 61 73 74 65 72 20  re is no master 
12586 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61  journal.  The ma
12587 73 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 6a 6f  ster.**       jo
12588 75 72 6e 61 6c 20 6e 61 6d 65 20 69 73 20 73 74  urnal name is st
12589 6f 72 65 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a  ored in UTF-8..*
1258a 2a 20 20 28 39 29 20 20 5a 65 72 6f 20 6f 72 20  *  (9)  Zero or 
1258b 6d 6f 72 65 20 70 61 67 65 73 20 69 6e 73 74 61  more pages insta
1258c 6e 63 65 73 2c 20 65 61 63 68 20 61 73 20 66 6f  nces, each as fo
1258d 6c 6c 6f 77 73 3a 0a 2a 2a 20 20 20 20 20 20 20  llows:.**       
1258e 20 2b 20 20 34 20 62 79 74 65 20 70 61 67 65 20   +  4 byte page 
1258f 6e 75 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 20 20  number..**      
12590 20 20 2b 20 20 70 50 61 67 65 72 2d 3e 70 61 67    +  pPager->pag
12591 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64  eSize bytes of d
12592 61 74 61 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b  ata..**        +
12593 20 20 34 20 62 79 74 65 20 63 68 65 63 6b 73 75    4 byte checksu
12594 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 65 20  m.**.** When we 
12595 73 70 65 61 6b 20 6f 66 20 74 68 65 20 6a 6f 75  speak of the jou
12596 72 6e 61 6c 20 68 65 61 64 65 72 2c 20 77 65 20  rnal header, we 
12597 6d 65 61 6e 20 74 68 65 20 66 69 72 73 74 20 38  mean the first 8
12598 20 69 74 65 6d 73 20 61 62 6f 76 65 2e 0a 2a 2a   items above..**
12599 20 45 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74   Each entry in t
1259a 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6e  he journal is an
1259b 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
1259c 20 39 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a 2a 2a   9th item..**.**
1259d 20 43 61 6c 6c 20 74 68 65 20 76 61 6c 75 65 20   Call the value 
1259e 66 72 6f 6d 20 74 68 65 20 73 65 63 6f 6e 64 20  from the second 
1259f 62 75 6c 6c 65 74 20 22 6e 52 65 63 22 2e 20 20  bullet "nRec".  
125a0 6e 52 65 63 20 69 73 20 74 68 65 20 6e 75 6d 62  nRec is the numb
125a1 65 72 20 6f 66 0a 2a 2a 20 76 61 6c 69 64 20 70  er of.** valid p
125a2 61 67 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74  age entries in t
125a3 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e 20  he journal.  In 
125a4 6d 6f 73 74 20 63 61 73 65 73 2c 20 79 6f 75 20  most cases, you 
125a5 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a  can compute the.
125a6 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63  ** value of nRec
125a7 20 66 72 6f 6d 20 74 68 65 20 73 69 7a 65 20 6f   from the size o
125a8 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
125a9 6c 65 2e 20 20 42 75 74 20 69 66 20 61 20 70 6f  le.  But if a po
125aa 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f  wer.** failure o
125ab 63 63 75 72 72 65 64 20 77 68 69 6c 65 20 74 68  ccurred while th
125ac 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65  e journal was be
125ad 69 6e 67 20 77 72 69 74 74 65 6e 2c 20 69 74 20  ing written, it 
125ae 63 6f 75 6c 64 20 62 65 20 74 68 65 0a 2a 2a 20  could be the.** 
125af 63 61 73 65 20 74 68 61 74 20 74 68 65 20 73 69  case that the si
125b0 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  ze of the journa
125b1 6c 20 66 69 6c 65 20 68 61 64 20 61 6c 72 65 61  l file had alrea
125b2 64 79 20 62 65 65 6e 20 69 6e 63 72 65 61 73 65  dy been increase
125b3 64 20 62 75 74 0a 2a 2a 20 74 68 65 20 65 78 74  d but.** the ext
125b4 72 61 20 65 6e 74 72 69 65 73 20 68 61 64 20 6e  ra entries had n
125b5 6f 74 20 79 65 74 20 6d 61 64 65 20 69 74 20 73  ot yet made it s
125b6 61 66 65 6c 79 20 74 6f 20 64 69 73 6b 2e 20 20  afely to disk.  
125b7 49 6e 20 73 75 63 68 20 61 20 63 61 73 65 2c 0a  In such a case,.
125b8 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
125b9 6e 52 65 63 20 63 6f 6d 70 75 74 65 64 20 66 72  nRec computed fr
125ba 6f 6d 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65  om the file size
125bb 20 77 6f 75 6c 64 20 62 65 20 74 6f 6f 20 6c 61   would be too la
125bc 72 67 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 61  rge.  For.** tha
125bd 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 61 6c 77  t reason, we alw
125be 61 79 73 20 75 73 65 20 74 68 65 20 6e 52 65 63  ays use the nRec
125bf 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 68 65   value in the he
125c0 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ader..**.** If t
125c1 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69 73  he nRec value is
125c2 20 30 78 66 66 66 66 66 66 66 66 20 69 74 20 6d   0xffffffff it m
125c3 65 61 6e 73 20 74 68 61 74 20 6e 52 65 63 20 73  eans that nRec s
125c4 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65  hould be compute
125c5 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 66 69  d.** from the fi
125c6 6c 65 20 73 69 7a 65 2e 20 20 54 68 69 73 20 76  le size.  This v
125c7 61 6c 75 65 20 69 73 20 75 73 65 64 20 77 68 65  alue is used whe
125c8 6e 20 74 68 65 20 75 73 65 72 20 73 65 6c 65 63  n the user selec
125c9 74 73 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73 79 6e  ts the.** no-syn
125ca 63 20 6f 70 74 69 6f 6e 20 66 6f 72 20 74 68 65  c option for the
125cb 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 20 70 6f 77   journal.  A pow
125cc 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64  er failure could
125cd 20 6c 65 61 64 20 74 6f 20 63 6f 72 72 75 70 74   lead to corrupt
125ce 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63  ion.** in this c
125cf 61 73 65 2e 20 20 42 75 74 20 66 6f 72 20 74 68  ase.  But for th
125d0 69 6e 67 73 20 6c 69 6b 65 20 74 65 6d 70 6f 72  ings like tempor
125d1 61 72 79 20 74 61 62 6c 65 20 28 77 68 69 63 68  ary table (which
125d2 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 65 6c 65   will be.** dele
125d3 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 6f 77  ted when the pow
125d4 65 72 20 69 73 20 72 65 73 74 6f 72 65 64 29 20  er is restored) 
125d5 77 65 20 64 6f 6e 27 74 20 63 61 72 65 2e 20 20  we don't care.  
125d6 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69  .**.** If the fi
125d7 6c 65 20 6f 70 65 6e 65 64 20 61 73 20 74 68 65  le opened as the
125d8 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
125d9 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d   not a well-form
125da 65 64 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69  ed.** journal fi
125db 6c 65 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 65  le then all page
125dc 73 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73  s up to the firs
125dd 74 20 63 6f 72 72 75 70 74 65 64 20 70 61 67 65  t corrupted page
125de 20 61 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62   are rolled.** b
125df 61 63 6b 20 28 6f 72 20 6e 6f 20 70 61 67 65 73  ack (or no pages
125e0 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   if the journal 
125e1 68 65 61 64 65 72 20 69 73 20 63 6f 72 72 75 70  header is corrup
125e2 74 65 64 29 2e 20 54 68 65 20 6a 6f 75 72 6e 61  ted). The journa
125e3 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20 74 68 65  l file.** is the
125e4 6e 20 64 65 6c 65 74 65 64 20 61 6e 64 20 53 51  n deleted and SQ
125e5 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64  LITE_OK returned
125e6 2c 20 6a 75 73 74 20 61 73 20 69 66 20 6e 6f 20  , just as if no 
125e7 63 6f 72 72 75 70 74 69 6f 6e 20 68 61 64 0a 2a  corruption had.*
125e8 2a 20 62 65 65 6e 20 65 6e 63 6f 75 6e 74 65 72  * been encounter
125e9 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ed..**.** If an 
125ea 49 2f 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20  I/O or malloc() 
125eb 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68  error occurs, th
125ec 65 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 69  e journal-file i
125ed 73 20 6e 6f 74 20 64 65 6c 65 74 65 64 0a 2a 2a  s not deleted.**
125ee 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f   and an error co
125ef 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  de is returned..
125f0 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 48 6f 74 20  **.** The isHot 
125f1 70 61 72 61 6d 65 74 65 72 20 69 6e 64 69 63 61  parameter indica
125f2 74 65 73 20 74 68 61 74 20 77 65 20 61 72 65 20  tes that we are 
125f3 74 72 79 69 6e 67 20 74 6f 20 72 6f 6c 6c 62 61  trying to rollba
125f4 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20  ck a journal.** 
125f5 74 68 61 74 20 6d 69 67 68 74 20 62 65 20 61 20  that might be a 
125f6 68 6f 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4f 72  hot journal.  Or
125f7 2c 20 69 74 20 63 6f 75 6c 64 20 62 65 20 74 68  , it could be th
125f8 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69  at the journal i
125f9 73 20 0a 2a 2a 20 70 72 65 73 65 72 76 65 64 20  s .** preserved 
125fa 62 65 63 61 75 73 65 20 6f 66 20 4a 4f 55 52 4e  because of JOURN
125fb 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 20 6f  ALMODE_PERSIST o
125fc 72 20 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52  r JOURNALMODE_TR
125fd 55 4e 43 41 54 45 2e 0a 2a 2a 20 49 66 20 74 68  UNCATE..** If th
125fe 65 20 6a 6f 75 72 6e 61 6c 20 72 65 61 6c 6c 79  e journal really
125ff 20 69 73 20 68 6f 74 2c 20 72 65 73 65 74 20 74   is hot, reset t
12600 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20 70  he pager cache p
12601 72 69 6f 72 20 72 6f 6c 6c 69 6e 67 0a 2a 2a 20  rior rolling.** 
12602 62 61 63 6b 20 61 6e 79 20 63 6f 6e 74 65 6e 74  back any content
12603 2e 20 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61  .  If the journa
12604 6c 20 69 73 20 6d 65 72 65 6c 79 20 70 65 72 73  l is merely pers
12605 69 73 74 65 6e 74 2c 20 6e 6f 20 72 65 73 65 74  istent, no reset
12606 20 69 73 0a 2a 2a 20 6e 65 65 64 65 64 2e 0a 2a   is.** needed..*
12607 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
12608 65 72 5f 70 6c 61 79 62 61 63 6b 28 50 61 67 65  er_playback(Page
12609 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 69  r *pPager, int i
1260a 73 48 6f 74 29 7b 0a 20 20 73 71 6c 69 74 65 33  sHot){.  sqlite3
1260b 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70 50 61  _vfs *pVfs = pPa
1260c 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 36 34  ger->pVfs;.  i64
1260d 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20 20   szJ;           
1260e 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
1260f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
12610 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20  e in bytes */.  
12611 75 33 32 20 6e 52 65 63 3b 20 20 20 20 20 20 20  u32 nRec;       
12612 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
12613 65 72 20 6f 66 20 52 65 63 6f 72 64 73 20 69 6e  er of Records in
12614 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a   the journal */.
12615 20 20 75 33 32 20 75 3b 20 20 20 20 20 20 20 20    u32 u;        
12616 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e             /* Un
12617 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75 6e  signed loop coun
12618 74 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d 78  ter */.  Pgno mx
12619 50 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  Pg = 0;         
1261a 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65    /* Size of the
1261b 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 20 69   original file i
1261c 6e 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e 74  n pages */.  int
1261d 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
1261e 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20        /* Result 
1261f 63 6f 64 65 20 6f 66 20 61 20 73 75 62 72 6f 75  code of a subrou
12620 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65  tine */.  int re
12621 73 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20  s = 1;          
12622 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75     /* Value retu
12623 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 4f  rned by sqlite3O
12624 73 41 63 63 65 73 73 28 29 20 2a 2f 0a 20 20 63  sAccess() */.  c
12625 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20 30  har *zMaster = 0
12626 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  ;       /* Name 
12627 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  of master journa
12628 6c 20 66 69 6c 65 20 69 66 20 61 6e 79 20 2a 2f  l file if any */
12629 0a 20 20 69 6e 74 20 6e 65 65 64 50 61 67 65 72  .  int needPager
1262a 52 65 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 54  Reset;      /* T
1262b 72 75 65 20 74 6f 20 72 65 73 65 74 20 70 61 67  rue to reset pag
1262c 65 20 70 72 69 6f 72 20 74 6f 20 66 69 72 73 74  e prior to first
1262d 20 70 61 67 65 20 72 6f 6c 6c 62 61 63 6b 20 2a   page rollback *
1262e 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  /..  /* Figure o
1262f 75 74 20 68 6f 77 20 6d 61 6e 79 20 72 65 63 6f  ut how many reco
12630 72 64 73 20 61 72 65 20 69 6e 20 74 68 65 20 6a  rds are in the j
12631 6f 75 72 6e 61 6c 2e 20 20 41 62 6f 72 74 20 65  ournal.  Abort e
12632 61 72 6c 79 20 69 66 0a 20 20 2a 2a 20 74 68 65  arly if.  ** the
12633 20 6a 6f 75 72 6e 61 6c 20 69 73 20 65 6d 70 74   journal is empt
12634 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  y..  */.  assert
12635 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
12636 3e 6a 66 64 29 20 29 3b 0a 20 20 72 63 20 3d 20  >jfd) );.  rc = 
12637 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a  sqlite3OsFileSiz
12638 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26  e(pPager->jfd, &
12639 73 7a 4a 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  szJ);.  if( rc!=
1263a 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 7a 4a  SQLITE_OK || szJ
1263b 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ==0 ){.    goto 
1263c 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20  end_playback;.  
1263d 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65  }..  /* Read the
1263e 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1263f 6e 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 6a 6f  name from the jo
12640 75 72 6e 61 6c 2c 20 69 66 20 69 74 20 69 73 20  urnal, if it is 
12641 70 72 65 73 65 6e 74 2e 0a 20 20 2a 2a 20 49 66  present..  ** If
12642 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   a master journa
12643 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 73  l file name is s
12644 70 65 63 69 66 69 65 64 2c 20 62 75 74 20 74 68  pecified, but th
12645 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 0a 20 20  e file is not.  
12646 2a 2a 20 70 72 65 73 65 6e 74 20 6f 6e 20 64 69  ** present on di
12647 73 6b 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75  sk, then the jou
12648 72 6e 61 6c 20 69 73 20 6e 6f 74 20 68 6f 74 20  rnal is not hot 
12649 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65  and does not nee
1264a 64 20 74 6f 20 62 65 0a 20 20 2a 2a 20 70 6c 61  d to be.  ** pla
1264b 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a 2a 0a 20  yed back..  **. 
1264c 20 2a 2a 20 54 4f 44 4f 3a 20 54 65 63 68 6e 69   ** TODO: Techni
1264d 63 61 6c 6c 79 20 74 68 65 20 66 6f 6c 6c 6f 77  cally the follow
1264e 69 6e 67 20 69 73 20 61 6e 20 65 72 72 6f 72 20  ing is an error 
1264f 62 65 63 61 75 73 65 20 69 74 20 61 73 73 75 6d  because it assum
12650 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 62 75 66  es that.  ** buf
12651 66 65 72 20 50 61 67 65 72 2e 70 54 6d 70 53 70  fer Pager.pTmpSp
12652 61 63 65 20 69 73 20 28 6d 78 50 61 74 68 6e 61  ace is (mxPathna
12653 6d 65 2b 31 29 20 62 79 74 65 73 20 6f 72 20 6c  me+1) bytes or l
12654 61 72 67 65 72 2e 20 69 2e 65 2e 20 74 68 61 74  arger. i.e. that
12655 0a 20 20 2a 2a 20 28 70 50 61 67 65 72 2d 3e 70  .  ** (pPager->p
12656 61 67 65 53 69 7a 65 20 3e 3d 20 70 50 61 67 65  ageSize >= pPage
12657 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e  r->pVfs->mxPathn
12658 61 6d 65 2b 31 29 2e 20 55 73 69 6e 67 20 6f 73  ame+1). Using os
12659 5f 75 6e 69 78 2e 63 2c 0a 20 20 2a 2a 20 20 6d  _unix.c,.  **  m
1265a 78 50 61 74 68 6e 61 6d 65 20 69 73 20 35 31 32  xPathname is 512
1265b 2c 20 77 68 69 63 68 20 69 73 20 74 68 65 20 73  , which is the s
1265c 61 6d 65 20 61 73 20 74 68 65 20 6d 69 6e 69 6d  ame as the minim
1265d 75 6d 20 61 6c 6c 6f 77 61 62 6c 65 20 76 61 6c  um allowable val
1265e 75 65 0a 20 20 2a 2a 20 66 6f 72 20 70 61 67 65  ue.  ** for page
1265f 53 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 7a 4d 61  Size..  */.  zMa
12660 73 74 65 72 20 3d 20 70 50 61 67 65 72 2d 3e 70  ster = pPager->p
12661 54 6d 70 53 70 61 63 65 3b 0a 20 20 72 63 20 3d  TmpSpace;.  rc =
12662 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75 72 6e   readMasterJourn
12663 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  al(pPager->jfd, 
12664 7a 4d 61 73 74 65 72 2c 20 70 50 61 67 65 72 2d  zMaster, pPager-
12665 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d  >pVfs->mxPathnam
12666 65 2b 31 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  e+1);.  if( rc==
12667 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 7a 4d 61  SQLITE_OK && zMa
12668 73 74 65 72 5b 30 5d 20 29 7b 0a 20 20 20 20 72  ster[0] ){.    r
12669 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63  c = sqlite3OsAcc
1266a 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73 74 65  ess(pVfs, zMaste
1266b 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  r, SQLITE_ACCESS
1266c 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29 3b 0a  _EXISTS, &res);.
1266d 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 20 3d 20    }.  zMaster = 
1266e 30 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  0;.  if( rc!=SQL
1266f 49 54 45 5f 4f 4b 20 7c 7c 20 21 72 65 73 20 29  ITE_OK || !res )
12670 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70  {.    goto end_p
12671 6c 61 79 62 61 63 6b 3b 0a 20 20 7d 0a 20 20 70  layback;.  }.  p
12672 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
12673 66 20 3d 20 30 3b 0a 20 20 6e 65 65 64 50 61 67  f = 0;.  needPag
12674 65 72 52 65 73 65 74 20 3d 20 69 73 48 6f 74 3b  erReset = isHot;
12675 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f 70  ..  /* This loop
12676 20 74 65 72 6d 69 6e 61 74 65 73 20 65 69 74 68   terminates eith
12677 65 72 20 77 68 65 6e 20 61 20 72 65 61 64 4a 6f  er when a readJo
12678 75 72 6e 61 6c 48 64 72 28 29 20 6f 72 20 0a 20  urnalHdr() or . 
12679 20 2a 2a 20 70 61 67 65 72 5f 70 6c 61 79 62 61   ** pager_playba
1267a 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 29 20 63 61  ck_one_page() ca
1267b 6c 6c 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  ll returns SQLIT
1267c 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 49 4f 20  E_DONE or an IO 
1267d 65 72 72 6f 72 20 0a 20 20 2a 2a 20 6f 63 63 75  error .  ** occu
1267e 72 73 2e 20 0a 20 20 2a 2f 0a 20 20 77 68 69 6c  rs. .  */.  whil
1267f 65 28 20 31 20 29 7b 0a 20 20 20 20 69 6e 74 20  e( 1 ){.    int 
12680 69 73 55 6e 73 79 6e 63 20 3d 20 30 3b 0a 0a 20  isUnsync = 0;.. 
12681 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6e     /* Read the n
12682 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  ext journal head
12683 65 72 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72  er from the jour
12684 6e 61 6c 20 66 69 6c 65 2e 20 20 49 66 20 74 68  nal file.  If th
12685 65 72 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e  ere are.    ** n
12686 6f 74 20 65 6e 6f 75 67 68 20 62 79 74 65 73 20  ot enough bytes 
12687 6c 65 66 74 20 69 6e 20 74 68 65 20 6a 6f 75 72  left in the jour
12688 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 61 20 63  nal file for a c
12689 6f 6d 70 6c 65 74 65 20 68 65 61 64 65 72 2c 20  omplete header, 
1268a 6f 72 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20  or.    ** it is 
1268b 63 6f 72 72 75 70 74 65 64 2c 20 74 68 65 6e 20  corrupted, then 
1268c 61 20 70 72 6f 63 65 73 73 20 6d 75 73 74 20 6f  a process must o
1268d 66 20 66 61 69 6c 65 64 20 77 68 69 6c 65 20 77  f failed while w
1268e 72 69 74 69 6e 67 20 69 74 2e 0a 20 20 20 20 2a  riting it..    *
1268f 2a 20 54 68 69 73 20 69 6e 64 69 63 61 74 65 73  * This indicates
12690 20 6e 6f 74 68 69 6e 67 20 6d 6f 72 65 20 6e 65   nothing more ne
12691 65 64 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64  eds to be rolled
12692 20 62 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20   back..    */.  
12693 20 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e    rc = readJourn
12694 61 6c 48 64 72 28 70 50 61 67 65 72 2c 20 69 73  alHdr(pPager, is
12695 48 6f 74 2c 20 73 7a 4a 2c 20 26 6e 52 65 63 2c  Hot, szJ, &nRec,
12696 20 26 6d 78 50 67 29 3b 0a 20 20 20 20 69 66 28   &mxPg);.    if(
12697 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
12698 7b 20 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  { .      if( rc=
12699 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a  =SQLITE_DONE ){.
1269a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
1269b 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a  ITE_OK;.      }.
1269c 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 70        goto end_p
1269d 6c 61 79 62 61 63 6b 3b 0a 20 20 20 20 7d 0a 0a  layback;.    }..
1269e 20 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69      /* If nRec i
1269f 73 20 30 78 66 66 66 66 66 66 66 66 2c 20 74 68  s 0xffffffff, th
126a0 65 6e 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20  en this journal 
126a1 77 61 73 20 63 72 65 61 74 65 64 20 62 79 20 61  was created by a
126a2 20 70 72 6f 63 65 73 73 0a 20 20 20 20 2a 2a 20   process.    ** 
126a3 77 6f 72 6b 69 6e 67 20 69 6e 20 6e 6f 2d 73 79  working in no-sy
126a4 6e 63 20 6d 6f 64 65 2e 20 54 68 69 73 20 6d 65  nc mode. This me
126a5 61 6e 73 20 74 68 61 74 20 74 68 65 20 72 65 73  ans that the res
126a6 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  t of the journal
126a7 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 63 6f 6e  .    ** file con
126a8 73 69 73 74 73 20 6f 66 20 70 61 67 65 73 2c 20  sists of pages, 
126a9 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72  there are no mor
126aa 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  e journal header
126ab 73 2e 20 43 6f 6d 70 75 74 65 0a 20 20 20 20 2a  s. Compute.    *
126ac 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6e  * the value of n
126ad 52 65 63 20 62 61 73 65 64 20 6f 6e 20 74 68 69  Rec based on thi
126ae 73 20 61 73 73 75 6d 70 74 69 6f 6e 2e 0a 20 20  s assumption..  
126af 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52 65    */.    if( nRe
126b0 63 3d 3d 30 78 66 66 66 66 66 66 66 66 20 29 7b  c==0xffffffff ){
126b1 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
126b2 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
126b3 66 3d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53  f==JOURNAL_HDR_S
126b4 5a 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20  Z(pPager) );.   
126b5 20 20 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 28     nRec = (int)(
126b6 28 73 7a 4a 20 2d 20 4a 4f 55 52 4e 41 4c 5f 48  (szJ - JOURNAL_H
126b7 44 52 5f 53 5a 28 70 50 61 67 65 72 29 29 2f 4a  DR_SZ(pPager))/J
126b8 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61  OURNAL_PG_SZ(pPa
126b9 67 65 72 29 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  ger));.    }..  
126ba 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73 20    /* If nRec is 
126bb 30 20 61 6e 64 20 74 68 69 73 20 72 6f 6c 6c 62  0 and this rollb
126bc 61 63 6b 20 69 73 20 6f 66 20 61 20 74 72 61 6e  ack is of a tran
126bd 73 61 63 74 69 6f 6e 20 63 72 65 61 74 65 64 20  saction created 
126be 62 79 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 70  by this.    ** p
126bf 72 6f 63 65 73 73 20 61 6e 64 20 69 66 20 74 68  rocess and if th
126c0 69 73 20 69 73 20 74 68 65 20 66 69 6e 61 6c 20  is is the final 
126c1 68 65 61 64 65 72 20 69 6e 20 74 68 65 20 6a 6f  header in the jo
126c2 75 72 6e 61 6c 2c 20 74 68 65 6e 20 69 74 20 6d  urnal, then it m
126c3 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 61 74  eans.    ** that
126c4 20 74 68 69 73 20 70 61 72 74 20 6f 66 20 74 68   this part of th
126c5 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65  e journal was be
126c6 69 6e 67 20 66 69 6c 6c 65 64 20 62 75 74 20 68  ing filled but h
126c7 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 0a  as not yet been.
126c8 20 20 20 20 2a 2a 20 73 79 6e 63 65 64 20 74 6f      ** synced to
126c9 20 64 69 73 6b 2e 20 20 43 6f 6d 70 75 74 65 20   disk.  Compute 
126ca 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
126cb 67 65 73 20 62 61 73 65 64 20 6f 6e 20 74 68 65  ges based on the
126cc 20 72 65 6d 61 69 6e 69 6e 67 0a 20 20 20 20 2a   remaining.    *
126cd 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69  * size of the fi
126ce 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  le..    **.    *
126cf 2a 20 54 68 65 20 74 68 69 72 64 20 74 65 72 6d  * The third term
126d0 20 6f 66 20 74 68 65 20 74 65 73 74 20 77 61 73   of the test was
126d1 20 61 64 64 65 64 20 74 6f 20 66 69 78 20 74 69   added to fix ti
126d2 63 6b 65 74 20 23 32 35 36 35 2e 0a 20 20 20 20  cket #2565..    
126d3 2a 2a 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 20  ** When rolling 
126d4 62 61 63 6b 20 61 20 68 6f 74 20 6a 6f 75 72 6e  back a hot journ
126d5 61 6c 2c 20 6e 52 65 63 3d 3d 30 20 61 6c 77 61  al, nRec==0 alwa
126d6 79 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  ys means that th
126d7 65 20 6e 65 78 74 0a 20 20 20 20 2a 2a 20 63 68  e next.    ** ch
126d8 75 6e 6b 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  unk of the journ
126d9 61 6c 20 63 6f 6e 74 61 69 6e 73 20 7a 65 72 6f  al contains zero
126da 20 70 61 67 65 73 20 74 6f 20 62 65 20 72 6f 6c   pages to be rol
126db 6c 65 64 20 62 61 63 6b 2e 20 20 42 75 74 0a 20  led back.  But. 
126dc 20 20 20 2a 2a 20 77 68 65 6e 20 64 6f 69 6e 67     ** when doing
126dd 20 61 20 52 4f 4c 4c 42 41 43 4b 20 61 6e 64 20   a ROLLBACK and 
126de 74 68 65 20 6e 52 65 63 3d 3d 30 20 63 68 75 6e  the nRec==0 chun
126df 6b 20 69 73 20 74 68 65 20 6c 61 73 74 20 63 68  k is the last ch
126e0 75 6e 6b 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68  unk in.    ** th
126e1 65 20 6a 6f 75 72 6e 61 6c 2c 20 69 74 20 6d 65  e journal, it me
126e2 61 6e 73 20 74 68 61 74 20 74 68 65 20 6a 6f 75  ans that the jou
126e3 72 6e 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61  rnal might conta
126e4 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 0a 20 20  in additional.  
126e5 20 20 2a 2a 20 70 61 67 65 73 20 74 68 61 74 20    ** pages that 
126e6 6e 65 65 64 20 74 6f 20 62 65 20 72 6f 6c 6c 65  need to be rolle
126e7 64 20 62 61 63 6b 20 61 6e 64 20 74 68 61 74 20  d back and that 
126e8 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
126e9 67 65 73 20 0a 20 20 20 20 2a 2a 20 73 68 6f 75  ges .    ** shou
126ea 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64 20 62  ld be computed b
126eb 61 73 65 64 20 6f 6e 20 74 68 65 20 6a 6f 75 72  ased on the jour
126ec 6e 61 6c 20 66 69 6c 65 20 73 69 7a 65 2e 0a 20  nal file size.. 
126ed 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52     */.    if( nR
126ee 65 63 3d 3d 30 20 26 26 20 21 69 73 48 6f 74 20  ec==0 && !isHot 
126ef 26 26 0a 20 20 20 20 20 20 20 20 70 50 61 67 65  &&.        pPage
126f0 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f  r->journalHdr+JO
126f1 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61  URNAL_HDR_SZ(pPa
126f2 67 65 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f  ger)==pPager->jo
126f3 75 72 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20  urnalOff ){.    
126f4 20 20 6e 52 65 63 20 3d 20 28 69 6e 74 29 28 28    nRec = (int)((
126f5 73 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a 6f  szJ - pPager->jo
126f6 75 72 6e 61 6c 4f 66 66 29 20 2f 20 4a 4f 55 52  urnalOff) / JOUR
126f7 4e 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72  NAL_PG_SZ(pPager
126f8 29 29 3b 0a 20 20 20 20 20 20 69 73 55 6e 73 79  ));.      isUnsy
126f9 6e 63 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 0a 20  nc = 1;.    }.. 
126fa 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73     /* If this is
126fb 20 74 68 65 20 66 69 72 73 74 20 68 65 61 64 65   the first heade
126fc 72 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65 20  r read from the 
126fd 6a 6f 75 72 6e 61 6c 2c 20 74 72 75 6e 63 61 74  journal, truncat
126fe 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74  e the.    ** dat
126ff 61 62 61 73 65 20 66 69 6c 65 20 62 61 63 6b 20  abase file back 
12700 74 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20  to its original 
12701 73 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  size..    */.   
12702 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75   if( pPager->jou
12703 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e 41 4c  rnalOff==JOURNAL
12704 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20  _HDR_SZ(pPager) 
12705 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61  ){.      rc = pa
12706 67 65 72 5f 74 72 75 6e 63 61 74 65 28 70 50 61  ger_truncate(pPa
12707 67 65 72 2c 20 6d 78 50 67 29 3b 0a 20 20 20 20  ger, mxPg);.    
12708 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
12709 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67  _OK ){.        g
1270a 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b  oto end_playback
1270b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1270c 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d  pPager->dbSize =
1270d 20 6d 78 50 67 3b 0a 20 20 20 20 7d 0a 0a 20 20   mxPg;.    }..  
1270e 20 20 2f 2a 20 43 6f 70 79 20 6f 72 69 67 69 6e    /* Copy origin
1270f 61 6c 20 70 61 67 65 73 20 6f 75 74 20 6f 66 20  al pages out of 
12710 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20  the journal and 
12711 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 0a 20  back into the . 
12712 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66     ** database f
12713 69 6c 65 20 61 6e 64 2f 6f 72 20 70 61 67 65 20  ile and/or page 
12714 63 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  cache..    */.  
12715 20 20 66 6f 72 28 75 3d 30 3b 20 75 3c 6e 52 65    for(u=0; u<nRe
12716 63 3b 20 75 2b 2b 29 7b 0a 20 20 20 20 20 20 69  c; u++){.      i
12717 66 28 20 6e 65 65 64 50 61 67 65 72 52 65 73 65  f( needPagerRese
12718 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 61 67  t ){.        pag
12719 65 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29  er_reset(pPager)
1271a 3b 0a 20 20 20 20 20 20 20 20 6e 65 65 64 50 61  ;.        needPa
1271b 67 65 72 52 65 73 65 74 20 3d 20 30 3b 0a 20 20  gerReset = 0;.  
1271c 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d      }.      rc =
1271d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f   pager_playback_
1271e 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c  one_page(pPager,
1271f 31 2c 69 73 55 6e 73 79 6e 63 2c 26 70 50 61 67  1,isUnsync,&pPag
12720 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 30  er->journalOff,0
12721 2c 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ,0);.      if( r
12722 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
12723 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
12724 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20  SQLITE_DONE ){. 
12725 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
12726 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20  LITE_OK;.       
12727 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
12728 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 20  alOff = szJ;.   
12729 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1272a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1272b 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20         /* If we 
1272c 61 72 65 20 75 6e 61 62 6c 65 20 74 6f 20 72 6f  are unable to ro
1272d 6c 6c 62 61 63 6b 2c 20 71 75 69 74 20 61 6e 64  llback, quit and
1272e 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f   return the erro
1272f 72 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63  r.          ** c
12730 6f 64 65 2e 20 20 54 68 69 73 20 77 69 6c 6c 20  ode.  This will 
12731 63 61 75 73 65 20 74 68 65 20 70 61 67 65 72 20  cause the pager 
12732 74 6f 20 65 6e 74 65 72 20 74 68 65 20 65 72 72  to enter the err
12733 6f 72 20 73 74 61 74 65 0a 20 20 20 20 20 20 20  or state.       
12734 20 20 20 2a 2a 20 73 6f 20 74 68 61 74 20 6e 6f     ** so that no
12735 20 66 75 72 74 68 65 72 20 68 61 72 6d 20 77 69   further harm wi
12736 6c 6c 20 62 65 20 64 6f 6e 65 2e 20 20 50 65 72  ll be done.  Per
12737 68 61 70 73 20 74 68 65 20 6e 65 78 74 0a 20 20  haps the next.  
12738 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65          ** proce
12739 73 73 20 74 6f 20 63 6f 6d 65 20 61 6c 6f 6e 67  ss to come along
1273a 20 77 69 6c 6c 20 62 65 20 61 62 6c 65 20 74 6f   will be able to
1273b 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 64 61   rollback the da
1273c 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 20 20  tabase..        
1273d 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 67    */.          g
1273e 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b  oto end_playback
1273f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
12740 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
12741 2f 2a 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a 20  /*NOTREACHED*/. 
12742 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a 0a 65   assert( 0 );..e
12743 6e 64 5f 70 6c 61 79 62 61 63 6b 3a 0a 20 20 2f  nd_playback:.  /
12744 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20 61 20 72 6f  * Following a ro
12745 6c 6c 62 61 63 6b 2c 20 74 68 65 20 64 61 74 61  llback, the data
12746 62 61 73 65 20 66 69 6c 65 20 73 68 6f 75 6c 64  base file should
12747 20 62 65 20 62 61 63 6b 20 69 6e 20 69 74 73 20   be back in its 
12748 6f 72 69 67 69 6e 61 6c 0a 20 20 2a 2a 20 73 74  original.  ** st
12749 61 74 65 20 70 72 69 6f 72 20 74 6f 20 74 68 65  ate prior to the
1274a 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 74 72   start of the tr
1274b 61 6e 73 61 63 74 69 6f 6e 2c 20 73 6f 20 69 6e  ansaction, so in
1274c 76 6f 6b 65 20 74 68 65 0a 20 20 2a 2a 20 53 51  voke the.  ** SQ
1274d 4c 49 54 45 5f 46 43 4e 54 4c 5f 44 42 5f 55 4e  LITE_FCNTL_DB_UN
1274e 43 48 41 4e 47 45 44 20 66 69 6c 65 2d 63 6f 6e  CHANGED file-con
1274f 74 72 6f 6c 20 6d 65 74 68 6f 64 20 74 6f 20 64  trol method to d
12750 69 73 61 62 6c 65 20 74 68 65 0a 20 20 2a 2a 20  isable the.  ** 
12751 61 73 73 65 72 74 69 6f 6e 20 74 68 61 74 20 74  assertion that t
12752 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 63  he transaction c
12753 6f 75 6e 74 65 72 20 77 61 73 20 6d 6f 64 69 66  ounter was modif
12754 69 65 64 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ied..  */.  asse
12755 72 74 28 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  rt(.    pPager->
12756 66 64 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 30 20  fd->pMethods==0 
12757 7c 7c 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73  ||.    sqlite3Os
12758 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 70 50 61 67  FileControl(pPag
12759 65 72 2d 3e 66 64 2c 53 51 4c 49 54 45 5f 46 43  er->fd,SQLITE_FC
1275a 4e 54 4c 5f 44 42 5f 55 4e 43 48 41 4e 47 45 44  NTL_DB_UNCHANGED
1275b 2c 30 29 3e 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20  ,0)>=SQLITE_OK. 
1275c 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69   );..  /* If thi
1275d 73 20 70 6c 61 79 62 61 63 6b 20 69 73 20 68 61  s playback is ha
1275e 70 70 65 6e 69 6e 67 20 61 75 74 6f 6d 61 74 69  ppening automati
1275f 63 61 6c 6c 79 20 61 73 20 61 20 72 65 73 75 6c  cally as a resul
12760 74 20 6f 66 20 61 6e 20 49 4f 20 6f 72 20 0a 20  t of an IO or . 
12761 20 2a 2a 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72   ** malloc error
12762 20 74 68 61 74 20 6f 63 63 75 72 72 65 64 20 61   that occurred a
12763 66 74 65 72 20 74 68 65 20 63 68 61 6e 67 65 2d  fter the change-
12764 63 6f 75 6e 74 65 72 20 77 61 73 20 75 70 64 61  counter was upda
12765 74 65 64 20 62 75 74 20 0a 20 20 2a 2a 20 62 65  ted but .  ** be
12766 66 6f 72 65 20 74 68 65 20 74 72 61 6e 73 61 63  fore the transac
12767 74 69 6f 6e 20 77 61 73 20 63 6f 6d 6d 69 74 74  tion was committ
12768 65 64 2c 20 74 68 65 6e 20 74 68 65 20 63 68 61  ed, then the cha
12769 6e 67 65 2d 63 6f 75 6e 74 65 72 20 0a 20 20 2a  nge-counter .  *
1276a 2a 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 6d  * modification m
1276b 61 79 20 6a 75 73 74 20 68 61 76 65 20 62 65 65  ay just have bee
1276c 6e 20 72 65 76 65 72 74 65 64 2e 20 49 66 20 74  n reverted. If t
1276d 68 69 73 20 68 61 70 70 65 6e 73 20 69 6e 20 65  his happens in e
1276e 78 63 6c 75 73 69 76 65 20 0a 20 20 2a 2a 20 6d  xclusive .  ** m
1276f 6f 64 65 2c 20 74 68 65 6e 20 73 75 62 73 65 71  ode, then subseq
12770 75 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  uent transaction
12771 73 20 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74  s performed by t
12772 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 69  he connection wi
12773 6c 6c 20 6e 6f 74 0a 20 20 2a 2a 20 75 70 64 61  ll not.  ** upda
12774 74 65 20 74 68 65 20 63 68 61 6e 67 65 2d 63 6f  te the change-co
12775 75 6e 74 65 72 20 61 74 20 61 6c 6c 2e 20 54 68  unter at all. Th
12776 69 73 20 6d 61 79 20 6c 65 61 64 20 74 6f 20 63  is may lead to c
12777 61 63 68 65 20 69 6e 63 6f 6e 73 69 73 74 65 6e  ache inconsisten
12778 63 79 0a 20 20 2a 2a 20 70 72 6f 62 6c 65 6d 73  cy.  ** problems
12779 20 66 6f 72 20 6f 74 68 65 72 20 70 72 6f 63 65   for other proce
1277a 73 73 65 73 20 61 74 20 73 6f 6d 65 20 70 6f 69  sses at some poi
1277b 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 72 65  nt in the future
1277c 2e 20 53 6f 2c 20 6a 75 73 74 0a 20 20 2a 2a 20  . So, just.  ** 
1277d 69 6e 20 63 61 73 65 20 74 68 69 73 20 68 61 73  in case this has
1277e 20 68 61 70 70 65 6e 65 64 2c 20 63 6c 65 61 72   happened, clear
1277f 20 74 68 65 20 63 68 61 6e 67 65 43 6f 75 6e 74   the changeCount
12780 44 6f 6e 65 20 66 6c 61 67 20 6e 6f 77 2e 0a 20  Done flag now.. 
12781 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 63 68   */.  pPager->ch
12782 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20  angeCountDone = 
12783 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65  pPager->tempFile
12784 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
12785 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 7a 4d  ITE_OK ){.    zM
12786 61 73 74 65 72 20 3d 20 70 50 61 67 65 72 2d 3e  aster = pPager->
12787 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 20 20 72  pTmpSpace;.    r
12788 63 20 3d 20 72 65 61 64 4d 61 73 74 65 72 4a 6f  c = readMasterJo
12789 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a 66  urnal(pPager->jf
1278a 64 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 61 67  d, zMaster, pPag
1278b 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74 68  er->pVfs->mxPath
1278c 6e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 74 65 73  name+1);.    tes
1278d 74 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54  tcase( rc!=SQLIT
1278e 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 20 20 69 66  E_OK );.  }.  if
1278f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
12790 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65  ){.    rc = page
12791 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  r_end_transactio
12792 6e 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65  n(pPager, zMaste
12793 72 5b 30 5d 21 3d 27 5c 30 27 29 3b 0a 20 20 20  r[0]!='\0');.   
12794 20 74 65 73 74 63 61 73 65 28 20 72 63 21 3d 53   testcase( rc!=S
12795 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 7d 0a  QLITE_OK );.  }.
12796 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
12797 5f 4f 4b 20 26 26 20 7a 4d 61 73 74 65 72 5b 30  _OK && zMaster[0
12798 5d 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20  ] && res ){.    
12799 2f 2a 20 49 66 20 74 68 65 72 65 20 77 61 73 20  /* If there was 
1279a 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  a master journal
1279b 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e   and this routin
1279c 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 73 75  e will return su
1279d 63 63 65 73 73 2c 0a 20 20 20 20 2a 2a 20 73 65  ccess,.    ** se
1279e 65 20 69 66 20 69 74 20 69 73 20 70 6f 73 73 69  e if it is possi
1279f 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 74 68  ble to delete th
127a0 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
127a1 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20  ..    */.    rc 
127a2 3d 20 70 61 67 65 72 5f 64 65 6c 6d 61 73 74 65  = pager_delmaste
127a3 72 28 70 50 61 67 65 72 2c 20 7a 4d 61 73 74 65  r(pPager, zMaste
127a4 72 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65  r);.    testcase
127a5 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
127a6 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65  );.  }..  /* The
127a7 20 50 61 67 65 72 2e 73 65 63 74 6f 72 53 69 7a   Pager.sectorSiz
127a8 65 20 76 61 72 69 61 62 6c 65 20 6d 61 79 20 68  e variable may h
127a9 61 76 65 20 62 65 65 6e 20 75 70 64 61 74 65 64  ave been updated
127aa 20 77 68 69 6c 65 20 72 6f 6c 6c 69 6e 67 0a 20   while rolling. 
127ab 20 2a 2a 20 62 61 63 6b 20 61 20 6a 6f 75 72 6e   ** back a journ
127ac 61 6c 20 63 72 65 61 74 65 64 20 62 79 20 61 20  al created by a 
127ad 70 72 6f 63 65 73 73 20 77 69 74 68 20 61 20 64  process with a d
127ae 69 66 66 65 72 65 6e 74 20 73 65 63 74 6f 72 20  ifferent sector 
127af 73 69 7a 65 0a 20 20 2a 2a 20 76 61 6c 75 65 2e  size.  ** value.
127b0 20 52 65 73 65 74 20 69 74 20 74 6f 20 74 68 65   Reset it to the
127b1 20 63 6f 72 72 65 63 74 20 76 61 6c 75 65 20 66   correct value f
127b2 6f 72 20 74 68 69 73 20 70 72 6f 63 65 73 73 2e  or this process.
127b3 0a 20 20 2a 2f 0a 20 20 73 65 74 53 65 63 74 6f  .  */.  setSecto
127b4 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20  rSize(pPager);. 
127b5 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
127b6 2a 0a 2a 2a 20 50 6c 61 79 62 61 63 6b 20 73 61  *.** Playback sa
127b7 76 65 70 6f 69 6e 74 20 70 53 61 76 65 70 6f 69  vepoint pSavepoi
127b8 6e 74 2e 20 4f 72 2c 20 69 66 20 70 53 61 76 65  nt. Or, if pSave
127b9 70 6f 69 6e 74 3d 3d 4e 55 4c 4c 2c 20 74 68 65  point==NULL, the
127ba 6e 20 70 6c 61 79 62 61 63 6b 0a 2a 2a 20 74 68  n playback.** th
127bb 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65 72 20  e entire master 
127bc 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68  journal file. Th
127bd 65 20 63 61 73 65 20 70 53 61 76 65 70 6f 69 6e  e case pSavepoin
127be 74 3d 3d 4e 55 4c 4c 20 6f 63 63 75 72 73 20 77  t==NULL occurs w
127bf 68 65 6e 20 0a 2a 2a 20 61 20 52 4f 4c 4c 42 41  hen .** a ROLLBA
127c0 43 4b 20 54 4f 20 63 6f 6d 6d 61 6e 64 20 69 73  CK TO command is
127c1 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 61 20 53 41   invoked on a SA
127c2 56 45 50 4f 49 4e 54 20 74 68 61 74 20 69 73 20  VEPOINT that is 
127c3 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a  a transaction .*
127c4 2a 20 73 61 76 65 70 6f 69 6e 74 2e 0a 2a 2a 0a  * savepoint..**.
127c5 2a 2a 20 57 68 65 6e 20 70 53 61 76 65 70 6f 69  ** When pSavepoi
127c6 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 28  nt is not NULL (
127c7 6d 65 61 6e 69 6e 67 20 61 20 6e 6f 6e 2d 74 72  meaning a non-tr
127c8 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f  ansaction savepo
127c9 69 6e 74 20 69 73 20 0a 2a 2a 20 62 65 69 6e 67  int is .** being
127ca 20 72 6f 6c 6c 65 64 20 62 61 63 6b 29 2c 20 74   rolled back), t
127cb 68 65 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b  hen the rollback
127cc 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 75 70 20   consists of up 
127cd 74 6f 20 74 68 72 65 65 20 73 74 61 67 65 73 2c  to three stages,
127ce 0a 2a 2a 20 70 65 72 66 6f 72 6d 65 64 20 69 6e  .** performed in
127cf 20 74 68 65 20 6f 72 64 65 72 20 73 70 65 63 69   the order speci
127d0 66 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  fied:.**.**   * 
127d1 50 61 67 65 73 20 61 72 65 20 70 6c 61 79 65 64  Pages are played
127d2 20 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 6d   back from the m
127d3 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 73 74 61 72  ain journal star
127d4 74 69 6e 67 20 61 74 20 62 79 74 65 0a 2a 2a 20  ting at byte.** 
127d5 20 20 20 20 6f 66 66 73 65 74 20 50 61 67 65 72      offset Pager
127d6 53 61 76 65 70 6f 69 6e 74 2e 69 4f 66 66 73 65  Savepoint.iOffse
127d7 74 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67  t and continuing
127d8 20 74 6f 20 0a 2a 2a 20 20 20 20 20 50 61 67 65   to .**     Page
127d9 72 53 61 76 65 70 6f 69 6e 74 2e 69 48 64 72 4f  rSavepoint.iHdrO
127da 66 66 73 65 74 2c 20 6f 72 20 74 6f 20 74 68 65  ffset, or to the
127db 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 69 6e   end of the main
127dc 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20 20   journal.**     
127dd 66 69 6c 65 20 69 66 20 50 61 67 65 72 53 61 76  file if PagerSav
127de 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65  epoint.iHdrOffse
127df 74 20 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a  t is zero..**.**
127e0 20 20 20 2a 20 49 66 20 50 61 67 65 72 53 61 76     * If PagerSav
127e1 65 70 6f 69 6e 74 2e 69 48 64 72 4f 66 66 73 65  epoint.iHdrOffse
127e2 74 20 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74  t is not zero, t
127e3 68 65 6e 20 70 61 67 65 73 20 61 72 65 20 70 6c  hen pages are pl
127e4 61 79 65 64 0a 2a 2a 20 20 20 20 20 62 61 63 6b  ayed.**     back
127e5 20 73 74 61 72 74 69 6e 67 20 66 72 6f 6d 20 74   starting from t
127e6 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  he journal heade
127e7 72 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f  r immediately fo
127e8 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 20 20 20 20  llowing .**     
127e9 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 2e 69  PagerSavepoint.i
127ea 48 64 72 4f 66 66 73 65 74 20 74 6f 20 74 68 65  HdrOffset to the
127eb 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 69 6e   end of the main
127ec 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a   journal file..*
127ed 2a 0a 2a 2a 20 20 20 2a 20 50 61 67 65 73 20 61  *.**   * Pages a
127ee 72 65 20 74 68 65 6e 20 70 6c 61 79 65 64 20 62  re then played b
127ef 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 73 75 62  ack from the sub
127f0 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 73  -journal file, s
127f1 74 61 72 74 69 6e 67 0a 2a 2a 20 20 20 20 20 77  tarting.**     w
127f2 69 74 68 20 74 68 65 20 50 61 67 65 72 53 61 76  ith the PagerSav
127f3 65 70 6f 69 6e 74 2e 69 53 75 62 52 65 63 20 61  epoint.iSubRec a
127f4 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f  nd continuing to
127f5 20 74 68 65 20 65 6e 64 20 6f 66 0a 2a 2a 20 20   the end of.**  
127f6 20 20 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66     the journal f
127f7 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 6f 75  ile..**.** Throu
127f8 67 68 6f 75 74 20 74 68 65 20 72 6f 6c 6c 62 61  ghout the rollba
127f9 63 6b 20 70 72 6f 63 65 73 73 2c 20 65 61 63 68  ck process, each
127fa 20 74 69 6d 65 20 61 20 70 61 67 65 20 69 73 20   time a page is 
127fb 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 74 68 65  rolled back, the
127fc 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  .** correspondin
127fd 67 20 62 69 74 20 69 73 20 73 65 74 20 69 6e 20  g bit is set in 
127fe 61 20 62 69 74 76 65 63 20 73 74 72 75 63 74 75  a bitvec structu
127ff 72 65 20 28 76 61 72 69 61 62 6c 65 20 70 44 6f  re (variable pDo
12800 6e 65 20 69 6e 20 74 68 65 0a 2a 2a 20 69 6d 70  ne in the.** imp
12801 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 62 65 6c 6f  lementation belo
12802 77 29 2e 20 54 68 69 73 20 69 73 20 75 73 65 64  w). This is used
12803 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20   to ensure that 
12804 61 20 70 61 67 65 20 69 73 20 6f 6e 6c 79 0a 2a  a page is only.*
12805 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 74 68  * rolled back th
12806 65 20 66 69 72 73 74 20 74 69 6d 65 20 69 74 20  e first time it 
12807 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 69  is encountered i
12808 6e 20 65 69 74 68 65 72 20 6a 6f 75 72 6e 61 6c  n either journal
12809 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 53 61 76 65  ..**.** If pSave
1280a 70 6f 69 6e 74 20 69 73 20 4e 55 4c 4c 2c 20 74  point is NULL, t
1280b 68 65 6e 20 70 61 67 65 73 20 61 72 65 20 6f 6e  hen pages are on
1280c 6c 79 20 70 6c 61 79 65 64 20 62 61 63 6b 20 66  ly played back f
1280d 72 6f 6d 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20  rom the main.** 
1280e 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68  journal file. Th
1280f 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 66  ere is no need f
12810 6f 72 20 61 20 62 69 74 76 65 63 20 69 6e 20 74  or a bitvec in t
12811 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20  his case..**.** 
12812 49 6e 20 65 69 74 68 65 72 20 63 61 73 65 2c 20  In either case, 
12813 62 65 66 6f 72 65 20 70 6c 61 79 62 61 63 6b 20  before playback 
12814 63 6f 6d 6d 65 6e 63 65 73 20 74 68 65 20 50 61  commences the Pa
12815 67 65 72 2e 64 62 53 69 7a 65 20 76 61 72 69 61  ger.dbSize varia
12816 62 6c 65 0a 2a 2a 20 69 73 20 72 65 73 65 74 20  ble.** is reset 
12817 74 6f 20 74 68 65 20 76 61 6c 75 65 20 74 68 61  to the value tha
12818 74 20 69 74 20 68 65 6c 64 20 61 74 20 74 68 65  t it held at the
12819 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 73 61   start of the sa
1281a 76 65 70 6f 69 6e 74 20 0a 2a 2a 20 28 6f 72 20  vepoint .** (or 
1281b 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20 4e 6f  transaction). No
1281c 20 70 61 67 65 20 77 69 74 68 20 61 20 70 61 67   page with a pag
1281d 65 2d 6e 75 6d 62 65 72 20 67 72 65 61 74 65 72  e-number greater
1281e 20 74 68 61 6e 20 74 68 69 73 20 76 61 6c 75 65   than this value
1281f 0a 2a 2a 20 69 73 20 70 6c 61 79 65 64 20 62 61  .** is played ba
12820 63 6b 2e 20 49 66 20 6f 6e 65 20 69 73 20 65 6e  ck. If one is en
12821 63 6f 75 6e 74 65 72 65 64 20 69 74 20 69 73 20  countered it is 
12822 73 69 6d 70 6c 79 20 73 6b 69 70 70 65 64 2e 0a  simply skipped..
12823 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
12824 67 65 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70  gerPlaybackSavep
12825 6f 69 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67  oint(Pager *pPag
12826 65 72 2c 20 50 61 67 65 72 53 61 76 65 70 6f 69  er, PagerSavepoi
12827 6e 74 20 2a 70 53 61 76 65 70 6f 69 6e 74 29 7b  nt *pSavepoint){
12828 0a 20 20 69 36 34 20 73 7a 4a 3b 20 20 20 20 20  .  i64 szJ;     
12829 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
1282a 66 66 65 63 74 69 76 65 20 73 69 7a 65 20 6f 66  ffective size of
1282b 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61   the main journa
1282c 6c 20 2a 2f 0a 20 20 69 36 34 20 69 48 64 72 4f  l */.  i64 iHdrO
1282d 66 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ff;             
1282e 2f 2a 20 45 6e 64 20 6f 66 20 66 69 72 73 74 20  /* End of first 
1282f 73 65 67 6d 65 6e 74 20 6f 66 20 6d 61 69 6e 2d  segment of main-
12830 6a 6f 75 72 6e 61 6c 20 72 65 63 6f 72 64 73 20  journal records 
12831 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  */.  int rc = SQ
12832 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 2f 2a  LITE_OK;      /*
12833 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
12834 20 20 42 69 74 76 65 63 20 2a 70 44 6f 6e 65 20    Bitvec *pDone 
12835 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 42 69  = 0;       /* Bi
12836 74 76 65 63 20 74 6f 20 65 6e 73 75 72 65 20 70  tvec to ensure p
12837 61 67 65 73 20 70 6c 61 79 65 64 20 62 61 63 6b  ages played back
12838 20 6f 6e 6c 79 20 6f 6e 63 65 20 2a 2f 0a 0a 20   only once */.. 
12839 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
1283a 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48  >state>=PAGER_SH
1283b 41 52 45 44 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c  ARED );..  /* Al
1283c 6c 6f 63 61 74 65 20 61 20 62 69 74 76 65 63 20  locate a bitvec 
1283d 74 6f 20 75 73 65 20 74 6f 20 73 74 6f 72 65 20  to use to store 
1283e 74 68 65 20 73 65 74 20 6f 66 20 70 61 67 65 73  the set of pages
1283f 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 2a 2f 0a   rolled back */.
12840 20 20 69 66 28 20 70 53 61 76 65 70 6f 69 6e 74    if( pSavepoint
12841 20 29 7b 0a 20 20 20 20 70 44 6f 6e 65 20 3d 20   ){.    pDone = 
12842 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65  sqlite3BitvecCre
12843 61 74 65 28 70 53 61 76 65 70 6f 69 6e 74 2d 3e  ate(pSavepoint->
12844 6e 4f 72 69 67 29 3b 0a 20 20 20 20 69 66 28 20  nOrig);.    if( 
12845 21 70 44 6f 6e 65 20 29 7b 0a 20 20 20 20 20 20  !pDone ){.      
12846 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
12847 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  MEM;.    }.  }..
12848 20 20 2f 2a 20 53 65 74 20 74 68 65 20 64 61 74    /* Set the dat
12849 61 62 61 73 65 20 73 69 7a 65 20 62 61 63 6b 20  abase size back 
1284a 74 6f 20 74 68 65 20 76 61 6c 75 65 20 69 74 20  to the value it 
1284b 77 61 73 20 62 65 66 6f 72 65 20 74 68 65 20 73  was before the s
1284c 61 76 65 70 6f 69 6e 74 20 0a 20 20 2a 2a 20 62  avepoint .  ** b
1284d 65 69 6e 67 20 72 65 76 65 72 74 65 64 20 77 61  eing reverted wa
1284e 73 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20  s opened..  */. 
1284f 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20   pPager->dbSize 
12850 3d 20 70 53 61 76 65 70 6f 69 6e 74 20 3f 20 70  = pSavepoint ? p
12851 53 61 76 65 70 6f 69 6e 74 2d 3e 6e 4f 72 69 67  Savepoint->nOrig
12852 20 3a 20 70 50 61 67 65 72 2d 3e 64 62 4f 72 69   : pPager->dbOri
12853 67 53 69 7a 65 3b 0a 0a 20 20 2f 2a 20 55 73 65  gSize;..  /* Use
12854 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
12855 4f 66 66 20 61 73 20 74 68 65 20 65 66 66 65 63  Off as the effec
12856 74 69 76 65 20 73 69 7a 65 20 6f 66 20 74 68 65  tive size of the
12857 20 6d 61 69 6e 20 72 6f 6c 6c 62 61 63 6b 0a 20   main rollback. 
12858 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68   ** journal.  Th
12859 65 20 61 63 74 75 61 6c 20 66 69 6c 65 20 6d 69  e actual file mi
1285a 67 68 74 20 62 65 20 6c 61 72 67 65 72 20 74 68  ght be larger th
1285b 61 6e 20 74 68 69 73 20 69 6e 0a 20 20 2a 2a 20  an this in.  ** 
1285c 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
1285d 45 5f 54 52 55 4e 43 41 54 45 20 6f 72 20 50 41  E_TRUNCATE or PA
1285e 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
1285f 50 45 52 53 49 53 54 2e 20 20 42 75 74 20 61 6e  PERSIST.  But an
12860 79 74 68 69 6e 67 0a 20 20 2a 2a 20 70 61 73 74  ything.  ** past
12861 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
12862 4f 66 66 20 69 73 20 6f 66 66 2d 6c 69 6d 69 74  Off is off-limit
12863 73 20 74 6f 20 75 73 2e 0a 20 20 2a 2f 0a 20 20  s to us..  */.  
12864 73 7a 4a 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f  szJ = pPager->jo
12865 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f 2a 20  urnalOff;..  /* 
12866 42 65 67 69 6e 20 62 79 20 72 6f 6c 6c 69 6e 67  Begin by rolling
12867 20 62 61 63 6b 20 72 65 63 6f 72 64 73 20 66 72   back records fr
12868 6f 6d 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72  om the main jour
12869 6e 61 6c 20 73 74 61 72 74 69 6e 67 20 61 74 0a  nal starting at.
1286a 20 20 2a 2a 20 50 61 67 65 72 53 61 76 65 70 6f    ** PagerSavepo
1286b 69 6e 74 2e 69 4f 66 66 73 65 74 20 61 6e 64 20  int.iOffset and 
1286c 63 6f 6e 74 69 6e 75 69 6e 67 20 74 6f 20 74 68  continuing to th
1286d 65 20 6e 65 78 74 20 6a 6f 75 72 6e 61 6c 20 68  e next journal h
1286e 65 61 64 65 72 2e 0a 20 20 2a 2a 20 54 68 65 72  eader..  ** Ther
1286f 65 20 6d 69 67 68 74 20 62 65 20 72 65 63 6f 72  e might be recor
12870 64 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 6a  ds in the main j
12871 6f 75 72 6e 61 6c 20 74 68 61 74 20 68 61 76 65  ournal that have
12872 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20   a page number. 
12873 20 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e   ** greater than
12874 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74   the current dat
12875 61 62 61 73 65 20 73 69 7a 65 20 28 70 50 61 67  abase size (pPag
12876 65 72 2d 3e 64 62 53 69 7a 65 29 20 62 75 74 20  er->dbSize) but 
12877 74 68 6f 73 65 0a 20 20 2a 2a 20 77 69 6c 6c 20  those.  ** will 
12878 62 65 20 73 6b 69 70 70 65 64 20 61 75 74 6f 6d  be skipped autom
12879 61 74 69 63 61 6c 6c 79 2e 20 20 50 61 67 65 73  atically.  Pages
1287a 20 61 72 65 20 61 64 64 65 64 20 74 6f 20 70 44   are added to pD
1287b 6f 6e 65 20 61 73 20 74 68 65 79 0a 20 20 2a 2a  one as they.  **
1287c 20 61 72 65 20 70 6c 61 79 65 64 20 62 61 63 6b   are played back
1287d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61  ..  */.  if( pSa
1287e 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 69  vepoint ){.    i
1287f 48 64 72 4f 66 66 20 3d 20 70 53 61 76 65 70 6f  HdrOff = pSavepo
12880 69 6e 74 2d 3e 69 48 64 72 4f 66 66 73 65 74 20  int->iHdrOffset 
12881 3f 20 70 53 61 76 65 70 6f 69 6e 74 2d 3e 69 48  ? pSavepoint->iH
12882 64 72 4f 66 66 73 65 74 20 3a 20 73 7a 4a 3b 0a  drOffset : szJ;.
12883 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
12884 6e 61 6c 4f 66 66 20 3d 20 70 53 61 76 65 70 6f  nalOff = pSavepo
12885 69 6e 74 2d 3e 69 4f 66 66 73 65 74 3b 0a 20 20  int->iOffset;.  
12886 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c    while( rc==SQL
12887 49 54 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72  ITE_OK && pPager
12888 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3c 69 48 64  ->journalOff<iHd
12889 72 4f 66 66 20 29 7b 0a 20 20 20 20 20 20 72 63  rOff ){.      rc
1288a 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63   = pager_playbac
1288b 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50 61 67 65  k_one_page(pPage
1288c 72 2c 20 31 2c 20 30 2c 20 26 70 50 61 67 65 72  r, 1, 0, &pPager
1288d 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 2c  ->journalOff, 1,
1288e 20 70 44 6f 6e 65 29 3b 0a 20 20 20 20 7d 0a 20   pDone);.    }. 
1288f 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
12890 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20  QLITE_DONE );.  
12891 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 61 67 65  }else{.    pPage
12892 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20  r->journalOff = 
12893 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e  0;.  }..  /* Con
12894 74 69 6e 75 65 20 72 6f 6c 6c 69 6e 67 20 62 61  tinue rolling ba
12895 63 6b 20 72 65 63 6f 72 64 73 20 6f 75 74 20 6f  ck records out o
12896 66 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e  f the main journ
12897 61 6c 20 73 74 61 72 74 69 6e 67 20 61 74 0a 20  al starting at. 
12898 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 6a 6f   ** the first jo
12899 75 72 6e 61 6c 20 68 65 61 64 65 72 20 73 65 65  urnal header see
1289a 6e 20 61 6e 64 20 63 6f 6e 74 69 6e 75 69 6e 67  n and continuing
1289b 20 75 6e 74 69 6c 20 74 68 65 20 65 66 66 65 63   until the effec
1289c 74 69 76 65 20 65 6e 64 0a 20 20 2a 2a 20 6f 66  tive end.  ** of
1289d 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61   the main journa
1289e 6c 20 66 69 6c 65 2e 20 20 43 6f 6e 74 69 6e 75  l file.  Continu
1289f 65 20 74 6f 20 73 6b 69 70 20 6f 75 74 2d 6f 66  e to skip out-of
128a0 2d 72 61 6e 67 65 20 70 61 67 65 73 20 61 6e 64  -range pages and
128a1 0a 20 20 2a 2a 20 63 6f 6e 74 69 6e 75 65 20 61  .  ** continue a
128a2 64 64 69 6e 67 20 70 61 67 65 73 20 72 6f 6c 6c  dding pages roll
128a3 65 64 20 62 61 63 6b 20 74 6f 20 70 44 6f 6e 65  ed back to pDone
128a4 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20  ..  */.  while( 
128a5 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
128a6 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
128a7 4f 66 66 3c 73 7a 4a 20 29 7b 0a 20 20 20 20 75  Off<szJ ){.    u
128a8 33 32 20 69 69 3b 20 20 20 20 20 20 20 20 20 20  32 ii;          
128a9 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
128aa 72 20 2a 2f 0a 20 20 20 20 75 33 32 20 6e 4a 52  r */.    u32 nJR
128ab 65 63 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e  ec = 0;     /* N
128ac 75 6d 62 65 72 20 6f 66 20 4a 6f 75 72 6e 61 6c  umber of Journal
128ad 20 52 65 63 6f 72 64 73 20 2a 2f 0a 20 20 20 20   Records */.    
128ae 75 33 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 72  u32 dummy;.    r
128af 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48  c = readJournalH
128b0 64 72 28 70 50 61 67 65 72 2c 20 30 2c 20 73 7a  dr(pPager, 0, sz
128b1 4a 2c 20 26 6e 4a 52 65 63 2c 20 26 64 75 6d 6d  J, &nJRec, &dumm
128b2 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  y);.    assert( 
128b3 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc!=SQLITE_DONE 
128b4 29 3b 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a  );..    /*.    *
128b5 2a 20 54 68 65 20 22 70 50 61 67 65 72 2d 3e 6a  * The "pPager->j
128b6 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41  ournalHdr+JOURNA
128b7 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29  L_HDR_SZ(pPager)
128b8 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ==pPager->journa
128b9 6c 4f 66 66 22 0a 20 20 20 20 2a 2a 20 74 65 73  lOff".    ** tes
128ba 74 20 69 73 20 72 65 6c 61 74 65 64 20 74 6f 20  t is related to 
128bb 74 69 63 6b 65 74 20 23 32 35 36 35 2e 20 20 53  ticket #2565.  S
128bc 65 65 20 74 68 65 20 64 69 73 63 75 73 73 69 6f  ee the discussio
128bd 6e 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20  n in the.    ** 
128be 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29  pager_playback()
128bf 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 64   function for ad
128c0 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
128c1 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tion..    */.   
128c2 20 69 66 28 20 6e 4a 52 65 63 3d 3d 30 20 0a 20   if( nJRec==0 . 
128c3 20 20 20 20 26 26 20 70 50 61 67 65 72 2d 3e 6a      && pPager->j
128c4 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52 4e 41  ournalHdr+JOURNA
128c5 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29  L_HDR_SZ(pPager)
128c6 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ==pPager->journa
128c7 6c 4f 66 66 0a 20 20 20 20 29 7b 0a 20 20 20 20  lOff.    ){.    
128c8 20 20 6e 4a 52 65 63 20 3d 20 28 75 33 32 29 28    nJRec = (u32)(
128c9 28 73 7a 4a 20 2d 20 70 50 61 67 65 72 2d 3e 6a  (szJ - pPager->j
128ca 6f 75 72 6e 61 6c 4f 66 66 29 2f 4a 4f 55 52 4e  ournalOff)/JOURN
128cb 41 4c 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29  AL_PG_SZ(pPager)
128cc 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  );.    }.    for
128cd 28 69 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54  (ii=0; rc==SQLIT
128ce 45 5f 4f 4b 20 26 26 20 69 69 3c 6e 4a 52 65 63  E_OK && ii<nJRec
128cf 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72   && pPager->jour
128d0 6e 61 6c 4f 66 66 3c 73 7a 4a 3b 20 69 69 2b 2b  nalOff<szJ; ii++
128d1 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61  ){.      rc = pa
128d2 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65  ger_playback_one
128d3 5f 70 61 67 65 28 70 50 61 67 65 72 2c 20 31 2c  _page(pPager, 1,
128d4 20 30 2c 20 26 70 50 61 67 65 72 2d 3e 6a 6f 75   0, &pPager->jou
128d5 72 6e 61 6c 4f 66 66 2c 20 31 2c 20 70 44 6f 6e  rnalOff, 1, pDon
128d6 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  e);.    }.    as
128d7 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
128d8 5f 44 4f 4e 45 20 29 3b 0a 20 20 7d 0a 20 20 61  _DONE );.  }.  a
128d9 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
128da 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 72 2d 3e  E_OK || pPager->
128db 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 73 7a 4a 20  journalOff==szJ 
128dc 29 3b 0a 0a 20 20 2f 2a 20 46 69 6e 61 6c 6c 79  );..  /* Finally
128dd 2c 20 20 72 6f 6c 6c 62 61 63 6b 20 70 61 67 65  ,  rollback page
128de 73 20 66 72 6f 6d 20 74 68 65 20 73 75 62 2d 6a  s from the sub-j
128df 6f 75 72 6e 61 6c 2e 20 20 50 61 67 65 20 74 68  ournal.  Page th
128e0 61 74 20 77 65 72 65 0a 20 20 2a 2a 20 70 72 65  at were.  ** pre
128e1 76 69 6f 75 73 6c 79 20 72 6f 6c 6c 65 64 20 62  viously rolled b
128e2 61 63 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6d  ack out of the m
128e3 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 28 61 6e 64  ain journal (and
128e4 20 61 72 65 20 68 65 6e 63 65 20 69 6e 20 70 44   are hence in pD
128e5 6f 6e 65 29 0a 20 20 2a 2a 20 77 69 6c 6c 20 62  one).  ** will b
128e6 65 20 73 6b 69 70 70 65 64 2e 20 20 4f 75 74 2d  e skipped.  Out-
128e7 6f 66 2d 72 61 6e 67 65 20 70 61 67 65 73 20 61  of-range pages a
128e8 72 65 20 61 6c 73 6f 20 73 6b 69 70 70 65 64 2e  re also skipped.
128e9 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53 61 76  .  */.  if( pSav
128ea 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 75 33  epoint ){.    u3
128eb 32 20 69 69 3b 20 20 20 20 20 20 20 20 20 20 20  2 ii;           
128ec 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
128ed 20 2a 2f 0a 20 20 20 20 69 36 34 20 6f 66 66 73   */.    i64 offs
128ee 65 74 20 3d 20 70 53 61 76 65 70 6f 69 6e 74 2d  et = pSavepoint-
128ef 3e 69 53 75 62 52 65 63 2a 28 34 2b 70 50 61 67  >iSubRec*(4+pPag
128f0 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20  er->pageSize);. 
128f1 20 20 20 66 6f 72 28 69 69 3d 70 53 61 76 65 70     for(ii=pSavep
128f2 6f 69 6e 74 2d 3e 69 53 75 62 52 65 63 3b 20 72  oint->iSubRec; r
128f3 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
128f4 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53 75 62 52  ii<pPager->nSubR
128f5 65 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20  ec; ii++){.     
128f6 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 3d   assert( offset=
128f7 3d 69 69 2a 28 34 2b 70 50 61 67 65 72 2d 3e 70  =ii*(4+pPager->p
128f8 61 67 65 53 69 7a 65 29 20 29 3b 0a 20 20 20 20  ageSize) );.    
128f9 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61    rc = pager_pla
128fa 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70  yback_one_page(p
128fb 50 61 67 65 72 2c 20 30 2c 20 30 2c 20 26 6f 66  Pager, 0, 0, &of
128fc 66 73 65 74 2c 20 31 2c 20 70 44 6f 6e 65 29 3b  fset, 1, pDone);
128fd 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
128fe 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f  t( rc!=SQLITE_DO
128ff 4e 45 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c  NE );.  }..  sql
12900 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f  ite3BitvecDestro
12901 79 28 70 44 6f 6e 65 29 3b 0a 20 20 69 66 28 20  y(pDone);.  if( 
12902 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
12903 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75  .    pPager->jou
12904 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20  rnalOff = szJ;. 
12905 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
12906 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  }../*.** Change 
12907 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62  the maximum numb
12908 65 72 20 6f 66 20 69 6e 2d 6d 65 6d 6f 72 79 20  er of in-memory 
12909 70 61 67 65 73 20 74 68 61 74 20 61 72 65 20 61  pages that are a
1290a 6c 6c 6f 77 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  llowed..*/.SQLIT
1290b 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1290c 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 61  qlite3PagerSetCa
1290d 63 68 65 73 69 7a 65 28 50 61 67 65 72 20 2a 70  chesize(Pager *p
1290e 50 61 67 65 72 2c 20 69 6e 74 20 6d 78 50 61 67  Pager, int mxPag
1290f 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 63 61  e){.  sqlite3Pca
12910 63 68 65 53 65 74 43 61 63 68 65 73 69 7a 65 28  cheSetCachesize(
12911 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c  pPager->pPCache,
12912 20 6d 78 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a   mxPage);.}../*.
12913 2a 2a 20 41 64 6a 75 73 74 20 74 68 65 20 72 6f  ** Adjust the ro
12914 62 75 73 74 6e 65 73 73 20 6f 66 20 74 68 65 20  bustness of the 
12915 64 61 74 61 62 61 73 65 20 74 6f 20 64 61 6d 61  database to dama
12916 67 65 20 64 75 65 20 74 6f 20 4f 53 20 63 72 61  ge due to OS cra
12917 73 68 65 73 0a 2a 2a 20 6f 72 20 70 6f 77 65 72  shes.** or power
12918 20 66 61 69 6c 75 72 65 73 20 62 79 20 63 68 61   failures by cha
12919 6e 67 69 6e 67 20 74 68 65 20 6e 75 6d 62 65 72  nging the number
1291a 20 6f 66 20 73 79 6e 63 73 28 29 73 20 77 68 65   of syncs()s whe
1291b 6e 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68 65  n writing.** the
1291c 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
1291d 6c 2e 20 20 54 68 65 72 65 20 61 72 65 20 74 68  l.  There are th
1291e 72 65 65 20 6c 65 76 65 6c 73 3a 0a 2a 2a 0a 2a  ree levels:.**.*
1291f 2a 20 20 20 20 4f 46 46 20 20 20 20 20 20 20 73  *    OFF       s
12920 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 29 20 69  qlite3OsSync() i
12921 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 2e 20  s never called. 
12922 20 54 68 69 73 20 69 73 20 74 68 65 20 64 65 66   This is the def
12923 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  ault.**         
12924 20 20 20 20 20 66 6f 72 20 74 65 6d 70 6f 72 61       for tempora
12925 72 79 20 61 6e 64 20 74 72 61 6e 73 69 65 6e 74  ry and transient
12926 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20   files..**.**   
12927 20 4e 4f 52 4d 41 4c 20 20 20 20 54 68 65 20 6a   NORMAL    The j
12928 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64  ournal is synced
12929 20 6f 6e 63 65 20 62 65 66 6f 72 65 20 77 72 69   once before wri
1292a 74 65 73 20 62 65 67 69 6e 20 6f 6e 20 74 68 65  tes begin on the
1292b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1292c 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73   database.  This
1292d 20 69 73 20 6e 6f 72 6d 61 6c 6c 79 20 61 64 65   is normally ade
1292e 71 75 61 74 65 20 70 72 6f 74 65 63 74 69 6f 6e  quate protection
1292f 2c 20 62 75 74 0a 2a 2a 20 20 20 20 20 20 20 20  , but.**        
12930 20 20 20 20 20 20 69 74 20 69 73 20 74 68 65 6f        it is theo
12931 72 65 74 69 63 61 6c 6c 79 20 70 6f 73 73 69 62  retically possib
12932 6c 65 2c 20 74 68 6f 75 67 68 20 76 65 72 79 20  le, though very 
12933 75 6e 6c 69 6b 65 6c 79 2c 0a 2a 2a 20 20 20 20  unlikely,.**    
12934 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20 61            that a
12935 6e 20 69 6e 6f 70 65 72 74 75 6e 65 20 70 6f 77  n inopertune pow
12936 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64  er failure could
12937 20 6c 65 61 76 65 20 74 68 65 20 6a 6f 75 72 6e   leave the journ
12938 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  al.**           
12939 20 20 20 69 6e 20 61 20 73 74 61 74 65 20 77 68     in a state wh
1293a 69 63 68 20 77 6f 75 6c 64 20 63 61 75 73 65 20  ich would cause 
1293b 64 61 6d 61 67 65 20 74 6f 20 74 68 65 20 64 61  damage to the da
1293c 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20  tabase.**       
1293d 20 20 20 20 20 20 20 77 68 65 6e 20 69 74 20 69         when it i
1293e 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a  s rolled back..*
1293f 2a 0a 2a 2a 20 20 20 20 46 55 4c 4c 20 20 20 20  *.**    FULL    
12940 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73    The journal is
12941 20 73 79 6e 63 65 64 20 74 77 69 63 65 20 62 65   synced twice be
12942 66 6f 72 65 20 77 72 69 74 65 73 20 62 65 67 69  fore writes begi
12943 6e 20 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20  n on the.**     
12944 20 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73           databas
12945 65 20 28 77 69 74 68 20 73 6f 6d 65 20 61 64 64  e (with some add
12946 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
12947 69 6f 6e 20 2d 20 74 68 65 20 6e 52 65 63 20 66  ion - the nRec f
12948 69 65 6c 64 0a 2a 2a 20 20 20 20 20 20 20 20 20  ield.**         
12949 20 20 20 20 20 6f 66 20 74 68 65 20 6a 6f 75 72       of the jour
1294a 6e 61 6c 20 68 65 61 64 65 72 20 2d 20 62 65 69  nal header - bei
1294b 6e 67 20 77 72 69 74 74 65 6e 20 69 6e 20 62 65  ng written in be
1294c 74 77 65 65 6e 20 74 68 65 20 74 77 6f 0a 2a 2a  tween the two.**
1294d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 79                sy
1294e 6e 63 73 29 2e 20 20 49 66 20 77 65 20 61 73 73  ncs).  If we ass
1294f 75 6d 65 20 74 68 61 74 20 77 72 69 74 69 6e 67  ume that writing
12950 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20   a.**           
12951 20 20 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73     single disk s
12952 65 63 74 6f 72 20 69 73 20 61 74 6f 6d 69 63 2c  ector is atomic,
12953 20 74 68 65 6e 20 74 68 69 73 20 6d 6f 64 65 20   then this mode 
12954 70 72 6f 76 69 64 65 73 0a 2a 2a 20 20 20 20 20  provides.**     
12955 20 20 20 20 20 20 20 20 20 61 73 73 75 72 61 6e           assuran
12956 63 65 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72  ce that the jour
12957 6e 61 6c 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  nal will not be 
12958 63 6f 72 72 75 70 74 65 64 20 74 6f 20 74 68 65  corrupted to the
12959 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1295a 20 70 6f 69 6e 74 20 6f 66 20 63 61 75 73 69 6e   point of causin
1295b 67 20 64 61 6d 61 67 65 20 74 6f 20 74 68 65 20  g damage to the 
1295c 64 61 74 61 62 61 73 65 20 64 75 72 69 6e 67 20  database during 
1295d 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20  rollback..**.** 
1295e 4e 75 6d 65 72 69 63 20 76 61 6c 75 65 73 20 61  Numeric values a
1295f 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
12960 68 65 73 65 20 73 74 61 74 65 73 20 61 72 65 20  hese states are 
12961 4f 46 46 3d 3d 31 2c 20 4e 4f 52 4d 41 4c 3d 32  OFF==1, NORMAL=2
12962 2c 0a 2a 2a 20 61 6e 64 20 46 55 4c 4c 3d 33 2e  ,.** and FULL=3.
12963 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
12964 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52  TE_OMIT_PAGER_PR
12965 41 47 4d 41 53 0a 53 51 4c 49 54 45 5f 50 52 49  AGMAS.SQLITE_PRI
12966 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
12967 33 50 61 67 65 72 53 65 74 53 61 66 65 74 79 4c  3PagerSetSafetyL
12968 65 76 65 6c 28 50 61 67 65 72 20 2a 70 50 61 67  evel(Pager *pPag
12969 65 72 2c 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69  er, int level, i
1296a 6e 74 20 62 46 75 6c 6c 46 73 79 6e 63 29 7b 0a  nt bFullFsync){.
1296b 20 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63    pPager->noSync
1296c 20 3d 20 20 28 6c 65 76 65 6c 3d 3d 31 20 7c 7c   =  (level==1 ||
1296d 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c   pPager->tempFil
1296e 65 29 20 3f 31 3a 30 3b 0a 20 20 70 50 61 67 65  e) ?1:0;.  pPage
1296f 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 28 6c  r->fullSync = (l
12970 65 76 65 6c 3d 3d 33 20 26 26 20 21 70 50 61 67  evel==3 && !pPag
12971 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 20 3f 31  er->tempFile) ?1
12972 3a 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 79  :0;.  pPager->sy
12973 6e 63 5f 66 6c 61 67 73 20 3d 20 28 62 46 75 6c  nc_flags = (bFul
12974 6c 46 73 79 6e 63 3f 53 51 4c 49 54 45 5f 53 59  lFsync?SQLITE_SY
12975 4e 43 5f 46 55 4c 4c 3a 53 51 4c 49 54 45 5f 53  NC_FULL:SQLITE_S
12976 59 4e 43 5f 4e 4f 52 4d 41 4c 29 3b 0a 20 20 69  YNC_NORMAL);.  i
12977 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  f( pPager->noSyn
12978 63 20 29 20 70 50 61 67 65 72 2d 3e 6e 65 65 64  c ) pPager->need
12979 53 79 6e 63 20 3d 20 30 3b 0a 7d 0a 23 65 6e 64  Sync = 0;.}.#end
1297a 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  if../*.** The fo
1297b 6c 6c 6f 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76  llowing global v
1297c 61 72 69 61 62 6c 65 20 69 73 20 69 6e 63 72 65  ariable is incre
1297d 6d 65 6e 74 65 64 20 77 68 65 6e 65 76 65 72 20  mented whenever 
1297e 74 68 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 61  the library.** a
1297f 74 74 65 6d 70 74 73 20 74 6f 20 6f 70 65 6e 20  ttempts to open 
12980 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  a temporary file
12981 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74  .  This informat
12982 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72 0a  ion is used for.
12983 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61  ** testing and a
12984 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 20 20 0a  nalysis only.  .
12985 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
12986 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49  _TEST.SQLITE_API
12987 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65   int sqlite3_ope
12988 6e 74 65 6d 70 5f 63 6f 75 6e 74 20 3d 20 30 3b  ntemp_count = 0;
12989 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f  .#endif../*.** O
1298a 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79 20  pen a temporary 
1298b 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74  file..**.** Writ
1298c 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72  e the file descr
1298d 69 70 74 6f 72 20 69 6e 74 6f 20 2a 70 46 69 6c  iptor into *pFil
1298e 65 2e 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  e. Return SQLITE
1298f 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 0a  _OK on success .
12990 2a 2a 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72  ** or some other
12991 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 77   error code if w
12992 65 20 66 61 69 6c 2e 20 54 68 65 20 4f 53 20 77  e fail. The OS w
12993 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ill automaticall
12994 79 20 0a 2a 2a 20 64 65 6c 65 74 65 20 74 68 65  y .** delete the
12995 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20   temporary file 
12996 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 65  when it is close
12997 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61  d..**.** The fla
12998 67 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 65  gs passed to the
12999 20 56 46 53 20 6c 61 79 65 72 20 78 4f 70 65 6e   VFS layer xOpen
1299a 28 29 20 63 61 6c 6c 20 61 72 65 20 74 68 6f 73  () call are thos
1299b 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 62  e specified.** b
1299c 79 20 70 61 72 61 6d 65 74 65 72 20 76 66 73 46  y parameter vfsF
1299d 6c 61 67 73 20 4f 52 65 64 20 77 69 74 68 20 74  lags ORed with t
1299e 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a  he following:.**
1299f 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4f  .**     SQLITE_O
129a0 50 45 4e 5f 52 45 41 44 57 52 49 54 45 0a 2a 2a  PEN_READWRITE.**
129a1 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
129a2 5f 43 52 45 41 54 45 0a 2a 2a 20 20 20 20 20 53  _CREATE.**     S
129a3 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55  QLITE_OPEN_EXCLU
129a4 53 49 56 45 0a 2a 2a 20 20 20 20 20 53 51 4c 49  SIVE.**     SQLI
129a5 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e  TE_OPEN_DELETEON
129a6 43 4c 4f 53 45 0a 2a 2f 0a 73 74 61 74 69 63 20  CLOSE.*/.static 
129a7 69 6e 74 20 70 61 67 65 72 4f 70 65 6e 74 65 6d  int pagerOpentem
129a8 70 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  p(.  Pager *pPag
129a9 65 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68  er,        /* Th
129aa 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 2a  e pager object *
129ab 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  /.  sqlite3_file
129ac 20 2a 70 46 69 6c 65 2c 20 20 2f 2a 20 57 72 69   *pFile,  /* Wri
129ad 74 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  te the file desc
129ae 72 69 70 74 6f 72 20 68 65 72 65 20 2a 2f 0a 20  riptor here */. 
129af 20 69 6e 74 20 76 66 73 46 6c 61 67 73 20 20 20   int vfsFlags   
129b0 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
129b1 70 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74  passed through t
129b2 6f 20 74 68 65 20 56 46 53 20 2a 2f 0a 29 7b 0a  o the VFS */.){.
129b3 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
129b4 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
129b5 6e 20 63 6f 64 65 20 2a 2f 0a 0a 23 69 66 64 65  n code */..#ifde
129b6 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
129b7 73 71 6c 69 74 65 33 5f 6f 70 65 6e 74 65 6d 70  sqlite3_opentemp
129b8 5f 63 6f 75 6e 74 2b 2b 3b 20 20 2f 2a 20 55 73  _count++;  /* Us
129b9 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61  ed for testing a
129ba 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79  nd analysis only
129bb 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 76 66   */.#endif..  vf
129bc 73 46 6c 61 67 73 20 7c 3d 20 20 53 51 4c 49 54  sFlags |=  SQLIT
129bd 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
129be 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43   | SQLITE_OPEN_C
129bf 52 45 41 54 45 20 7c 0a 20 20 20 20 20 20 20 20  REATE |.        
129c0 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
129c1 45 58 43 4c 55 53 49 56 45 20 7c 20 53 51 4c 49  EXCLUSIVE | SQLI
129c2 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e  TE_OPEN_DELETEON
129c3 43 4c 4f 53 45 3b 0a 20 20 72 63 20 3d 20 73 71  CLOSE;.  rc = sq
129c4 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 50 61 67  lite3OsOpen(pPag
129c5 65 72 2d 3e 70 56 66 73 2c 20 30 2c 20 70 46 69  er->pVfs, 0, pFi
129c6 6c 65 2c 20 76 66 73 46 6c 61 67 73 2c 20 30 29  le, vfsFlags, 0)
129c7 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d  ;.  assert( rc!=
129c8 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f  SQLITE_OK || isO
129c9 70 65 6e 28 70 46 69 6c 65 29 20 29 3b 0a 20 20  pen(pFile) );.  
129ca 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
129cb 0a 2a 2a 20 53 65 74 20 74 68 65 20 62 75 73 79  .** Set the busy
129cc 20 68 61 6e 64 6c 65 72 20 66 75 6e 63 74 69 6f   handler functio
129cd 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67  n..**.** The pag
129ce 65 72 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 62  er invokes the b
129cf 75 73 79 2d 68 61 6e 64 6c 65 72 20 69 66 20 73  usy-handler if s
129d0 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 20 72  qlite3OsLock() r
129d1 65 74 75 72 6e 73 20 0a 2a 2a 20 53 51 4c 49 54  eturns .** SQLIT
129d2 45 5f 42 55 53 59 20 77 68 65 6e 20 74 72 79 69  E_BUSY when tryi
129d3 6e 67 20 74 6f 20 75 70 67 72 61 64 65 20 66 72  ng to upgrade fr
129d4 6f 6d 20 6e 6f 2d 6c 6f 63 6b 20 74 6f 20 61 20  om no-lock to a 
129d5 53 48 41 52 45 44 20 6c 6f 63 6b 2c 0a 2a 2a 20  SHARED lock,.** 
129d6 6f 72 20 77 68 65 6e 20 74 72 79 69 6e 67 20 74  or when trying t
129d7 6f 20 75 70 67 72 61 64 65 20 66 72 6f 6d 20 61  o upgrade from a
129d8 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 74   RESERVED lock t
129d9 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 0a  o an EXCLUSIVE .
129da 2a 2a 20 6c 6f 63 6b 2e 20 49 74 20 64 6f 65 73  ** lock. It does
129db 20 2a 6e 6f 74 2a 20 69 6e 76 6f 6b 65 20 74 68   *not* invoke th
129dc 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 77  e busy handler w
129dd 68 65 6e 20 75 70 67 72 61 64 69 6e 67 20 66 72  hen upgrading fr
129de 6f 6d 0a 2a 2a 20 53 48 41 52 45 44 20 74 6f 20  om.** SHARED to 
129df 52 45 53 45 52 56 45 44 2c 20 6f 72 20 77 68 65  RESERVED, or whe
129e0 6e 20 75 70 67 72 61 64 69 6e 67 20 66 72 6f 6d  n upgrading from
129e1 20 53 48 41 52 45 44 20 74 6f 20 45 58 43 4c 55   SHARED to EXCLU
129e2 53 49 56 45 0a 2a 2a 20 28 77 68 69 63 68 20 6f  SIVE.** (which o
129e3 63 63 75 72 73 20 64 75 72 69 6e 67 20 68 6f 74  ccurs during hot
129e4 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63  -journal rollbac
129e5 6b 29 2e 20 53 75 6d 6d 61 72 79 3a 0a 2a 2a 0a  k). Summary:.**.
129e6 2a 2a 20 20 20 54 72 61 6e 73 69 74 69 6f 6e 20  **   Transition 
129e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
129e8 20 20 20 20 20 20 20 7c 20 49 6e 76 6f 6b 65 73         | Invokes
129e9 20 78 42 75 73 79 48 61 6e 64 6c 65 72 0a 2a 2a   xBusyHandler.**
129ea 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
129eb 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
129ec 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
129ed 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
129ee 20 4e 4f 5f 4c 4f 43 4b 20 20 20 20 20 20 20 2d   NO_LOCK       -
129ef 3e 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 20 20  > SHARED_LOCK   
129f0 20 20 20 7c 20 59 65 73 0a 2a 2a 20 20 20 53 48     | Yes.**   SH
129f1 41 52 45 44 5f 4c 4f 43 4b 20 20 20 2d 3e 20 52  ARED_LOCK   -> R
129f2 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20 20  ESERVED_LOCK    
129f3 7c 20 4e 6f 0a 2a 2a 20 20 20 53 48 41 52 45 44  | No.**   SHARED
129f4 5f 4c 4f 43 4b 20 20 20 2d 3e 20 45 58 43 4c 55  _LOCK   -> EXCLU
129f5 53 49 56 45 5f 4c 4f 43 4b 20 20 20 7c 20 4e 6f  SIVE_LOCK   | No
129f6 0a 2a 2a 20 20 20 52 45 53 45 52 56 45 44 5f 4c  .**   RESERVED_L
129f7 4f 43 4b 20 2d 3e 20 45 58 43 4c 55 53 49 56 45  OCK -> EXCLUSIVE
129f8 5f 4c 4f 43 4b 20 20 20 7c 20 59 65 73 0a 2a 2a  _LOCK   | Yes.**
129f9 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 73 79 2d  .** If the busy-
129fa 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63 6b  handler callback
129fb 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
129fc 6f 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 0a  o, the lock is .
129fd 2a 2a 20 72 65 74 72 69 65 64 2e 20 49 66 20 69  ** retried. If i
129fe 74 20 72 65 74 75 72 6e 73 20 7a 65 72 6f 2c 20  t returns zero, 
129ff 74 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f  then the SQLITE_
12a00 42 55 53 59 20 65 72 72 6f 72 20 69 73 0a 2a 2a  BUSY error is.**
12a01 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65   returned to the
12a02 20 63 61 6c 6c 65 72 20 6f 66 20 74 68 65 20 70   caller of the p
12a03 61 67 65 72 20 41 50 49 20 66 75 6e 63 74 69 6f  ager API functio
12a04 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
12a05 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
12a06 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e  3PagerSetBusyhan
12a07 64 6c 65 72 28 0a 20 20 50 61 67 65 72 20 2a 70  dler(.  Pager *p
12a08 50 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 20  Pager,          
12a09 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
12a0a 50 61 67 65 72 20 6f 62 6a 65 63 74 20 2a 2f 0a  Pager object */.
12a0b 20 20 69 6e 74 20 28 2a 78 42 75 73 79 48 61 6e    int (*xBusyHan
12a0c 64 6c 65 72 29 28 76 6f 69 64 20 2a 29 2c 20 20  dler)(void *),  
12a0d 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
12a0e 72 20 74 6f 20 62 75 73 79 2d 68 61 6e 64 6c 65  r to busy-handle
12a0f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  r function */.  
12a10 76 6f 69 64 20 2a 70 42 75 73 79 48 61 6e 64 6c  void *pBusyHandl
12a11 65 72 41 72 67 20 20 20 20 20 20 20 20 20 20 20  erArg           
12a12 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
12a13 20 74 6f 20 70 61 73 73 20 74 6f 20 78 42 75 73   to pass to xBus
12a14 79 48 61 6e 64 6c 65 72 20 2a 2f 0a 29 7b 20 20  yHandler */.){  
12a15 0a 20 20 70 50 61 67 65 72 2d 3e 78 42 75 73 79  .  pPager->xBusy
12a16 48 61 6e 64 6c 65 72 20 3d 20 78 42 75 73 79 48  Handler = xBusyH
12a17 61 6e 64 6c 65 72 3b 0a 20 20 70 50 61 67 65 72  andler;.  pPager
12a18 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72  ->pBusyHandlerAr
12a19 67 20 3d 20 70 42 75 73 79 48 61 6e 64 6c 65 72  g = pBusyHandler
12a1a 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  Arg;.}../*.** Re
12a1b 70 6f 72 74 20 74 68 65 20 63 75 72 72 65 6e 74  port the current
12a1c 20 70 61 67 65 20 73 69 7a 65 20 61 6e 64 20 6e   page size and n
12a1d 75 6d 62 65 72 20 6f 66 20 72 65 73 65 72 76 65  umber of reserve
12a1e 64 20 62 79 74 65 73 20 62 61 63 6b 0a 2a 2a 20  d bytes back.** 
12a1f 74 6f 20 74 68 65 20 63 6f 64 65 63 2e 0a 2a 2f  to the codec..*/
12a20 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48  .#ifdef SQLITE_H
12a21 41 53 5f 43 4f 44 45 43 0a 73 74 61 74 69 63 20  AS_CODEC.static 
12a22 76 6f 69 64 20 70 61 67 65 72 52 65 70 6f 72 74  void pagerReport
12a23 53 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61 67  Size(Pager *pPag
12a24 65 72 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65  er){.  if( pPage
12a25 72 2d 3e 78 43 6f 64 65 63 53 69 7a 65 43 68 6e  r->xCodecSizeChn
12a26 67 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d  g ){.    pPager-
12a27 3e 78 43 6f 64 65 63 53 69 7a 65 43 68 6e 67 28  >xCodecSizeChng(
12a28 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 2c 20  pPager->pCodec, 
12a29 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
12a2a 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
12a2b 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69 6e               (in
12a2c 74 29 70 50 61 67 65 72 2d 3e 6e 52 65 73 65 72  t)pPager->nReser
12a2d 76 65 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65  ve);.  }.}.#else
12a2e 0a 23 20 64 65 66 69 6e 65 20 70 61 67 65 72 52  .# define pagerR
12a2f 65 70 6f 72 74 53 69 7a 65 28 58 29 20 20 20 20  eportSize(X)    
12a30 20 2f 2a 20 4e 6f 2d 6f 70 20 69 66 20 77 65 20   /* No-op if we 
12a31 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72 74 20 61  do not support a
12a32 20 63 6f 64 65 63 20 2a 2f 0a 23 65 6e 64 69 66   codec */.#endif
12a33 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
12a34 68 65 20 70 61 67 65 20 73 69 7a 65 20 75 73 65  he page size use
12a35 64 20 62 79 20 74 68 65 20 50 61 67 65 72 20 6f  d by the Pager o
12a36 62 6a 65 63 74 2e 20 54 68 65 20 6e 65 77 20 70  bject. The new p
12a37 61 67 65 20 73 69 7a 65 20 0a 2a 2a 20 69 73 20  age size .** is 
12a38 70 61 73 73 65 64 20 69 6e 20 2a 70 50 61 67 65  passed in *pPage
12a39 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  Size..**.** If t
12a3a 68 65 20 70 61 67 65 72 20 69 73 20 69 6e 20 74  he pager is in t
12a3b 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20 77  he error state w
12a3c 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
12a3d 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 0a  n is called, it.
12a3e 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 54  ** is a no-op. T
12a3f 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
12a40 64 20 69 73 20 74 68 65 20 65 72 72 6f 72 20 73  d is the error s
12a41 74 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20  tate error code 
12a42 28 69 2e 65 2e 20 0a 2a 2a 20 6f 6e 65 20 6f 66  (i.e. .** one of
12a43 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 53   SQLITE_IOERR, S
12a44 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 6f 72  QLITE_CORRUPT or
12a45 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29 2e 0a 2a   SQLITE_FULL)..*
12a46 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20  *.** Otherwise, 
12a47 69 66 20 61 6c 6c 20 6f 66 20 74 68 65 20 66 6f  if all of the fo
12a48 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75 65  llowing are true
12a49 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65 20  :.**.**   * the 
12a4a 6e 65 77 20 70 61 67 65 20 73 69 7a 65 20 28 76  new page size (v
12a4b 61 6c 75 65 20 6f 66 20 2a 70 50 61 67 65 53 69  alue of *pPageSi
12a4c 7a 65 29 20 69 73 20 76 61 6c 69 64 20 28 61 20  ze) is valid (a 
12a4d 70 6f 77 65 72 20 0a 2a 2a 20 20 20 20 20 6f 66  power .**     of
12a4e 20 74 77 6f 20 62 65 74 77 65 65 6e 20 35 31 32   two between 512
12a4f 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 41 58 5f   and SQLITE_MAX_
12a50 50 41 47 45 5f 53 49 5a 45 2c 20 69 6e 63 6c 75  PAGE_SIZE, inclu
12a51 73 69 76 65 29 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a  sive), and.**.**
12a52 20 20 20 2a 20 74 68 65 72 65 20 61 72 65 20 6e     * there are n
12a53 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61  o outstanding pa
12a54 67 65 20 72 65 66 65 72 65 6e 63 65 73 2c 20 61  ge references, a
12a55 6e 64 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 74 68 65  nd.**.**   * the
12a56 20 64 61 74 61 62 61 73 65 20 69 73 20 65 69 74   database is eit
12a57 68 65 72 20 6e 6f 74 20 61 6e 20 69 6e 2d 6d 65  her not an in-me
12a58 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 6f 72  mory database or
12a59 20 69 74 20 69 73 0a 2a 2a 20 20 20 20 20 61 6e   it is.**     an
12a5a 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62   in-memory datab
12a5b 61 73 65 20 74 68 61 74 20 63 75 72 72 65 6e 74  ase that current
12a5c 6c 79 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 7a  ly consists of z
12a5d 65 72 6f 20 70 61 67 65 73 2e 0a 2a 2a 0a 2a 2a  ero pages..**.**
12a5e 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 72 20   then the pager 
12a5f 6f 62 6a 65 63 74 20 70 61 67 65 20 73 69 7a 65  object page size
12a60 20 69 73 20 73 65 74 20 74 6f 20 2a 70 50 61 67   is set to *pPag
12a61 65 53 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  eSize..**.** If 
12a62 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 69 73  the page size is
12a63 20 63 68 61 6e 67 65 64 2c 20 74 68 65 6e 20 74   changed, then t
12a64 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 75 73 65  his function use
12a65 73 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 61  s sqlite3PagerMa
12a66 6c 6c 6f 63 28 29 20 0a 2a 2a 20 74 6f 20 6f 62  lloc() .** to ob
12a67 74 61 69 6e 20 61 20 6e 65 77 20 50 61 67 65 72  tain a new Pager
12a68 2e 70 54 6d 70 53 70 61 63 65 20 62 75 66 66 65  .pTmpSpace buffe
12a69 72 2e 20 49 66 20 74 68 69 73 20 61 6c 6c 6f 63  r. If this alloc
12a6a 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 20 0a 2a  ation attempt .*
12a6b 2a 20 66 61 69 6c 73 2c 20 53 51 4c 49 54 45 5f  * fails, SQLITE_
12a6c 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65  NOMEM is returne
12a6d 64 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 73  d and the page s
12a6e 69 7a 65 20 72 65 6d 61 69 6e 73 20 75 6e 63 68  ize remains unch
12a6f 61 6e 67 65 64 2e 20 0a 2a 2a 20 49 6e 20 61 6c  anged. .** In al
12a70 6c 20 6f 74 68 65 72 20 63 61 73 65 73 2c 20 53  l other cases, S
12a71 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
12a72 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  rned..**.** If t
12a73 68 65 20 70 61 67 65 20 73 69 7a 65 20 69 73 20  he page size is 
12a74 6e 6f 74 20 63 68 61 6e 67 65 64 2c 20 65 69 74  not changed, eit
12a75 68 65 72 20 62 65 63 61 75 73 65 20 6f 6e 65 20  her because one 
12a76 6f 66 20 74 68 65 20 65 6e 75 6d 65 72 61 74 65  of the enumerate
12a77 64 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 73 20  d.** conditions 
12a78 61 62 6f 76 65 20 69 73 20 6e 6f 74 20 74 72 75  above is not tru
12a79 65 2c 20 74 68 65 20 70 61 67 65 72 20 77 61 73  e, the pager was
12a7a 20 69 6e 20 65 72 72 6f 72 20 73 74 61 74 65 20   in error state 
12a7b 77 68 65 6e 20 74 68 69 73 0a 2a 2a 20 66 75 6e  when this.** fun
12a7c 63 74 69 6f 6e 20 77 61 73 20 63 61 6c 6c 65 64  ction was called
12a7d 2c 20 6f 72 20 62 65 63 61 75 73 65 20 74 68 65  , or because the
12a7e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
12a7f 6f 6e 20 61 74 74 65 6d 70 74 20 66 61 69 6c 65  on attempt faile
12a80 64 2c 20 0a 2a 2a 20 74 68 65 6e 20 2a 70 50 61  d, .** then *pPa
12a81 67 65 53 69 7a 65 20 69 73 20 73 65 74 20 74 6f  geSize is set to
12a82 20 74 68 65 20 6f 6c 64 2c 20 72 65 74 61 69 6e   the old, retain
12a83 65 64 20 70 61 67 65 20 73 69 7a 65 20 62 65 66  ed page size bef
12a84 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
12a85 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
12a86 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
12a87 72 53 65 74 50 61 67 65 73 69 7a 65 28 50 61 67  rSetPagesize(Pag
12a88 65 72 20 2a 70 50 61 67 65 72 2c 20 75 31 36 20  er *pPager, u16 
12a89 2a 70 50 61 67 65 53 69 7a 65 2c 20 69 6e 74 20  *pPageSize, int 
12a8a 6e 52 65 73 65 72 76 65 29 7b 0a 20 20 69 6e 74  nReserve){.  int
12a8b 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72   rc = pPager->er
12a8c 72 43 6f 64 65 3b 0a 0a 20 20 69 66 28 20 72 63  rCode;..  if( rc
12a8d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
12a8e 20 20 20 75 31 36 20 70 61 67 65 53 69 7a 65 20     u16 pageSize 
12a8f 3d 20 2a 70 50 61 67 65 53 69 7a 65 3b 0a 20 20  = *pPageSize;.  
12a90 20 20 61 73 73 65 72 74 28 20 70 61 67 65 53 69    assert( pageSi
12a91 7a 65 3d 3d 30 20 7c 7c 20 28 70 61 67 65 53 69  ze==0 || (pageSi
12a92 7a 65 3e 3d 35 31 32 20 26 26 20 70 61 67 65 53  ze>=512 && pageS
12a93 69 7a 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f  ize<=SQLITE_MAX_
12a94 50 41 47 45 5f 53 49 5a 45 29 20 29 3b 0a 20 20  PAGE_SIZE) );.  
12a95 20 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e 6d    if( (pPager->m
12a96 65 6d 44 62 3d 3d 30 20 7c 7c 20 70 50 61 67 65  emDb==0 || pPage
12a97 72 2d 3e 64 62 53 69 7a 65 3d 3d 30 29 0a 20 20  r->dbSize==0).  
12a98 20 20 20 26 26 20 73 71 6c 69 74 65 33 50 63 61     && sqlite3Pca
12a99 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67  cheRefCount(pPag
12a9a 65 72 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20  er->pPCache)==0 
12a9b 0a 20 20 20 20 20 26 26 20 70 61 67 65 53 69 7a  .     && pageSiz
12a9c 65 20 26 26 20 70 61 67 65 53 69 7a 65 21 3d 70  e && pageSize!=p
12a9d 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20  Pager->pageSize 
12a9e 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 63 68  .    ){.      ch
12a9f 61 72 20 2a 70 4e 65 77 20 3d 20 28 63 68 61 72  ar *pNew = (char
12aa0 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65 4d 61   *)sqlite3PageMa
12aa1 6c 6c 6f 63 28 70 61 67 65 53 69 7a 65 29 3b 0a  lloc(pageSize);.
12aa2 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 77 20        if( !pNew 
12aa3 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
12aa4 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
12aa5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
12aa6 20 20 20 70 61 67 65 72 5f 72 65 73 65 74 28 70     pager_reset(p
12aa7 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20  Pager);.        
12aa8 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
12aa9 20 3d 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20   = pageSize;.   
12aaa 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
12aab 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70 54 6d  Free(pPager->pTm
12aac 70 53 70 61 63 65 29 3b 0a 20 20 20 20 20 20 20  pSpace);.       
12aad 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61   pPager->pTmpSpa
12aae 63 65 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20  ce = pNew;.     
12aaf 20 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65     sqlite3Pcache
12ab0 53 65 74 50 61 67 65 53 69 7a 65 28 70 50 61 67  SetPageSize(pPag
12ab1 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67  er->pPCache, pag
12ab2 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 7d 0a  eSize);.      }.
12ab3 20 20 20 20 7d 0a 20 20 20 20 2a 70 50 61 67 65      }.    *pPage
12ab4 53 69 7a 65 20 3d 20 28 75 31 36 29 70 50 61 67  Size = (u16)pPag
12ab5 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20  er->pageSize;.  
12ab6 20 20 69 66 28 20 6e 52 65 73 65 72 76 65 3c 30    if( nReserve<0
12ab7 20 29 20 6e 52 65 73 65 72 76 65 20 3d 20 70 50   ) nReserve = pP
12ab8 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65 3b 0a  ager->nReserve;.
12ab9 20 20 20 20 61 73 73 65 72 74 28 20 6e 52 65 73      assert( nRes
12aba 65 72 76 65 3e 3d 30 20 26 26 20 6e 52 65 73 65  erve>=0 && nRese
12abb 72 76 65 3c 31 30 30 30 20 29 3b 0a 20 20 20 20  rve<1000 );.    
12abc 70 50 61 67 65 72 2d 3e 6e 52 65 73 65 72 76 65  pPager->nReserve
12abd 20 3d 20 28 69 31 36 29 6e 52 65 73 65 72 76 65   = (i16)nReserve
12abe 3b 0a 20 20 20 20 70 61 67 65 72 52 65 70 6f 72  ;.    pagerRepor
12abf 74 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20  tSize(pPager);. 
12ac0 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
12ac1 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
12ac2 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
12ac3 20 22 74 65 6d 70 6f 72 61 72 79 20 70 61 67 65   "temporary page
12ac4 22 20 62 75 66 66 65 72 20 68 65 6c 64 20 69 6e  " buffer held in
12ac5 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20 62 79 20 74  ternally.** by t
12ac6 68 65 20 70 61 67 65 72 2e 20 20 54 68 69 73 20  he pager.  This 
12ac7 69 73 20 61 20 62 75 66 66 65 72 20 74 68 61 74  is a buffer that
12ac8 20 69 73 20 62 69 67 20 65 6e 6f 75 67 68 20 74   is big enough t
12ac9 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a 20 65 6e  o hold the.** en
12aca 74 69 72 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20  tire content of 
12acb 61 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2e  a database page.
12acc 20 20 54 68 69 73 20 62 75 66 66 65 72 20 69 73    This buffer is
12acd 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79   used internally
12ace 0a 2a 2a 20 64 75 72 69 6e 67 20 72 6f 6c 6c 62  .** during rollb
12acf 61 63 6b 20 61 6e 64 20 77 69 6c 6c 20 62 65 20  ack and will be 
12ad0 6f 76 65 72 77 72 69 74 74 65 6e 20 77 68 65 6e  overwritten when
12ad1 65 76 65 72 20 61 20 72 6f 6c 6c 62 61 63 6b 0a  ever a rollback.
12ad2 2a 2a 20 6f 63 63 75 72 73 2e 20 20 42 75 74 20  ** occurs.  But 
12ad3 6f 74 68 65 72 20 6d 6f 64 75 6c 65 73 20 61 72  other modules ar
12ad4 65 20 66 72 65 65 20 74 6f 20 75 73 65 20 69 74  e free to use it
12ad5 20 74 6f 6f 2c 20 61 73 20 6c 6f 6e 67 20 61 73   too, as long as
12ad6 0a 2a 2a 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 73  .** no rollbacks
12ad7 20 61 72 65 20 68 61 70 70 65 6e 69 6e 67 2e 0a   are happening..
12ad8 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
12ad9 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50  E void *sqlite3P
12ada 61 67 65 72 54 65 6d 70 53 70 61 63 65 28 50 61  agerTempSpace(Pa
12adb 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
12adc 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 70  return pPager->p
12add 54 6d 70 53 70 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a  TmpSpace;.}../*.
12ade 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 73 65  ** Attempt to se
12adf 74 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 64 61  t the maximum da
12ae0 74 61 62 61 73 65 20 70 61 67 65 20 63 6f 75 6e  tabase page coun
12ae1 74 20 69 66 20 6d 78 50 61 67 65 20 69 73 20 70  t if mxPage is p
12ae2 6f 73 69 74 69 76 65 2e 20 0a 2a 2a 20 4d 61 6b  ositive. .** Mak
12ae3 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 69 66 20  e no changes if 
12ae4 6d 78 50 61 67 65 20 69 73 20 7a 65 72 6f 20 6f  mxPage is zero o
12ae5 72 20 6e 65 67 61 74 69 76 65 2e 20 20 41 6e 64  r negative.  And
12ae6 20 6e 65 76 65 72 20 72 65 64 75 63 65 20 74 68   never reduce th
12ae7 65 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20 70 61 67  e.** maximum pag
12ae8 65 20 63 6f 75 6e 74 20 62 65 6c 6f 77 20 74 68  e count below th
12ae9 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65 20 6f  e current size o
12aea 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a  f the database..
12aeb 2a 2a 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73  **.** Regardless
12aec 20 6f 66 20 6d 78 50 61 67 65 2c 20 72 65 74 75   of mxPage, retu
12aed 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d  rn the current m
12aee 61 78 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e  aximum page coun
12aef 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
12af0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
12af1 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e  PagerMaxPageCoun
12af2 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  t(Pager *pPager,
12af3 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20   int mxPage){.  
12af4 69 66 28 20 6d 78 50 61 67 65 3e 30 20 29 7b 0a  if( mxPage>0 ){.
12af5 20 20 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67      pPager->mxPg
12af6 6e 6f 20 3d 20 6d 78 50 61 67 65 3b 0a 20 20 7d  no = mxPage;.  }
12af7 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50  .  sqlite3PagerP
12af8 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c  agecount(pPager,
12af9 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 50   0);.  return pP
12afa 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 3b 0a 7d 0a  ager->mxPgno;.}.
12afb 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
12afc 77 69 6e 67 20 73 65 74 20 6f 66 20 72 6f 75 74  wing set of rout
12afd 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f  ines are used to
12afe 20 64 69 73 61 62 6c 65 20 74 68 65 20 73 69 6d   disable the sim
12aff 75 6c 61 74 65 64 0a 2a 2a 20 49 2f 4f 20 65 72  ulated.** I/O er
12b00 72 6f 72 20 6d 65 63 68 61 6e 69 73 6d 2e 20 20  ror mechanism.  
12b01 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  These routines a
12b02 72 65 20 75 73 65 64 20 74 6f 20 61 76 6f 69 64  re used to avoid
12b03 20 73 69 6d 75 6c 61 74 65 64 0a 2a 2a 20 65 72   simulated.** er
12b04 72 6f 72 73 20 69 6e 20 70 6c 61 63 65 73 20 77  rors in places w
12b05 68 65 72 65 20 77 65 20 64 6f 20 6e 6f 74 20 63  here we do not c
12b06 61 72 65 20 61 62 6f 75 74 20 65 72 72 6f 72 73  are about errors
12b07 2e 0a 2a 2a 0a 2a 2a 20 55 6e 6c 65 73 73 20 2d  ..**.** Unless -
12b08 44 53 51 4c 49 54 45 5f 54 45 53 54 3d 31 20 69  DSQLITE_TEST=1 i
12b09 73 20 75 73 65 64 2c 20 74 68 65 73 65 20 72 6f  s used, these ro
12b0a 75 74 69 6e 65 73 20 61 72 65 20 61 6c 6c 20 6e  utines are all n
12b0b 6f 2d 6f 70 73 0a 2a 2a 20 61 6e 64 20 67 65 6e  o-ops.** and gen
12b0c 65 72 61 74 65 20 6e 6f 20 63 6f 64 65 2e 0a 2a  erate no code..*
12b0d 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
12b0e 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20  TEST.SQLITE_API 
12b0f 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74  extern int sqlit
12b10 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64  e3_io_error_pend
12b11 69 6e 67 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  ing;.SQLITE_API 
12b12 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69 74  extern int sqlit
12b13 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 69 74 3b  e3_io_error_hit;
12b14 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61 76 65  .static int save
12b15 64 5f 63 6e 74 3b 0a 76 6f 69 64 20 64 69 73 61  d_cnt;.void disa
12b16 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f  ble_simulated_io
12b17 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a 20  _errors(void){. 
12b18 20 73 61 76 65 64 5f 63 6e 74 20 3d 20 73 71 6c   saved_cnt = sql
12b19 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65  ite3_io_error_pe
12b1a 6e 64 69 6e 67 3b 0a 20 20 73 71 6c 69 74 65 33  nding;.  sqlite3
12b1b 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e  _io_error_pendin
12b1c 67 20 3d 20 2d 31 3b 0a 7d 0a 76 6f 69 64 20 65  g = -1;.}.void e
12b1d 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f  nable_simulated_
12b1e 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64 29 7b  io_errors(void){
12b1f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  .  sqlite3_io_er
12b20 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20 73 61  ror_pending = sa
12b21 76 65 64 5f 63 6e 74 3b 0a 7d 0a 23 65 6c 73 65  ved_cnt;.}.#else
12b22 0a 23 20 64 65 66 69 6e 65 20 64 69 73 61 62 6c  .# define disabl
12b23 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65  e_simulated_io_e
12b24 72 72 6f 72 73 28 29 0a 23 20 64 65 66 69 6e 65  rrors().# define
12b25 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65   enable_simulate
12b26 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a 23 65  d_io_errors().#e
12b27 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  ndif../*.** Read
12b28 20 74 68 65 20 66 69 72 73 74 20 4e 20 62 79 74   the first N byt
12b29 65 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69  es from the begi
12b2a 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 66 69 6c  nning of the fil
12b2b 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 0a 2a 2a  e into memory.**
12b2c 20 74 68 61 74 20 70 44 65 73 74 20 70 6f 69 6e   that pDest poin
12b2d 74 73 20 74 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66  ts to. .**.** If
12b2e 20 74 68 65 20 70 61 67 65 72 20 77 61 73 20 6f   the pager was o
12b2f 70 65 6e 65 64 20 6f 6e 20 61 20 74 72 61 6e 73  pened on a trans
12b30 69 65 6e 74 20 66 69 6c 65 20 28 7a 46 69 6c 65  ient file (zFile
12b31 6e 61 6d 65 3d 3d 22 22 29 2c 20 6f 72 0a 2a 2a  name==""), or.**
12b32 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 66 69 6c   opened on a fil
12b33 65 20 6c 65 73 73 20 74 68 61 6e 20 4e 20 62 79  e less than N by
12b34 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 74 68 65  tes in size, the
12b35 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69   output buffer i
12b36 73 0a 2a 2a 20 7a 65 72 6f 65 64 20 61 6e 64 20  s.** zeroed and 
12b37 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e  SQLITE_OK return
12b38 65 64 2e 20 54 68 65 20 72 61 74 69 6f 6e 61 6c  ed. The rational
12b39 65 20 66 6f 72 20 74 68 69 73 20 69 73 20 74 68  e for this is th
12b3a 61 74 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63  at this .** func
12b3b 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
12b3c 72 65 61 64 20 64 61 74 61 62 61 73 65 20 68 65  read database he
12b3d 61 64 65 72 73 2c 20 61 6e 64 20 61 20 6e 65 77  aders, and a new
12b3e 20 74 72 61 6e 73 69 65 6e 74 20 6f 72 0a 2a 2a   transient or.**
12b3f 20 7a 65 72 6f 20 73 69 7a 65 64 20 64 61 74 61   zero sized data
12b40 62 61 73 65 20 68 61 73 20 61 20 68 65 61 64 65  base has a heade
12b41 72 20 74 68 61 6e 20 63 6f 6e 73 69 73 74 73 20  r than consists 
12b42 65 6e 74 69 72 65 6c 79 20 6f 66 20 7a 65 72 6f  entirely of zero
12b43 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79  es..**.** If any
12b44 20 49 4f 20 65 72 72 6f 72 20 61 70 61 72 74 20   IO error apart 
12b45 66 72 6f 6d 20 53 51 4c 49 54 45 5f 49 4f 45 52  from SQLITE_IOER
12b46 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 69 73 20  R_SHORT_READ is 
12b47 65 6e 63 6f 75 6e 74 65 72 65 64 2c 0a 2a 2a 20  encountered,.** 
12b48 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69  the error code i
12b49 73 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68  s returned to th
12b4a 65 20 63 61 6c 6c 65 72 20 61 6e 64 20 74 68 65  e caller and the
12b4b 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
12b4c 0a 2a 2a 20 6f 75 74 70 75 74 20 62 75 66 66 65  .** output buffe
12b4d 72 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  r undefined..*/.
12b4e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
12b4f 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52  nt sqlite3PagerR
12b50 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 50 61  eadFileheader(Pa
12b51 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
12b52 20 4e 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61   N, unsigned cha
12b53 72 20 2a 70 44 65 73 74 29 7b 0a 20 20 69 6e 74  r *pDest){.  int
12b54 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
12b55 0a 20 20 6d 65 6d 73 65 74 28 70 44 65 73 74 2c  .  memset(pDest,
12b56 20 30 2c 20 4e 29 3b 0a 20 20 61 73 73 65 72 74   0, N);.  assert
12b57 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
12b58 3e 66 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e  >fd) || pPager->
12b59 74 65 6d 70 46 69 6c 65 20 29 3b 0a 20 20 69 66  tempFile );.  if
12b5a 28 20 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d  ( isOpen(pPager-
12b5b 3e 66 64 29 20 29 7b 0a 20 20 20 20 49 4f 54 52  >fd) ){.    IOTR
12b5c 41 43 45 28 28 22 44 42 48 44 52 20 25 70 20 30  ACE(("DBHDR %p 0
12b5d 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20   %d\n", pPager, 
12b5e 4e 29 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  N)).    rc = sql
12b5f 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65  ite3OsRead(pPage
12b60 72 2d 3e 66 64 2c 20 70 44 65 73 74 2c 20 4e 2c  r->fd, pDest, N,
12b61 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d   0);.    if( rc=
12b62 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48  =SQLITE_IOERR_SH
12b63 4f 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20  ORT_READ ){.    
12b64 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
12b65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
12b66 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
12b67 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74  * Return the tot
12b68 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  al number of pag
12b69 65 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  es in the databa
12b6a 73 65 20 66 69 6c 65 20 61 73 73 6f 63 69 61 74  se file associat
12b6b 65 64 20 0a 2a 2a 20 77 69 74 68 20 70 50 61 67  ed .** with pPag
12b6c 65 72 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 74 68  er. Normally, th
12b6d 69 73 20 69 73 20 63 61 6c 63 75 6c 61 74 65 64  is is calculated
12b6e 20 61 73 20 28 3c 64 62 20 66 69 6c 65 20 73 69   as (<db file si
12b6f 7a 65 3e 2f 3c 70 61 67 65 2d 73 69 7a 65 3e 29  ze>/<page-size>)
12b70 2e 0a 2a 2a 20 48 6f 77 65 76 65 72 2c 20 69 66  ..** However, if
12b71 20 74 68 65 20 66 69 6c 65 20 69 73 20 62 65 74   the file is bet
12b72 77 65 65 6e 20 31 20 61 6e 64 20 3c 70 61 67 65  ween 1 and <page
12b73 2d 73 69 7a 65 3e 20 62 79 74 65 73 20 69 6e 20  -size> bytes in 
12b74 73 69 7a 65 2c 20 74 68 65 6e 20 0a 2a 2a 20 74  size, then .** t
12b75 68 69 73 20 69 73 20 63 6f 6e 73 69 64 65 72 65  his is considere
12b76 64 20 61 20 31 20 70 61 67 65 20 66 69 6c 65 2e  d a 1 page file.
12b77 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 61  .**.** If the pa
12b78 67 65 72 20 69 73 20 69 6e 20 65 72 72 6f 72 20  ger is in error 
12b79 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 20  state when this 
12b7a 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
12b7b 65 64 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20  ed, then the.** 
12b7c 65 72 72 6f 72 20 73 74 61 74 65 20 65 72 72 6f  error state erro
12b7d 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e  r code is return
12b7e 65 64 20 61 6e 64 20 2a 70 6e 50 61 67 65 20 6c  ed and *pnPage l
12b7f 65 66 74 20 75 6e 63 68 61 6e 67 65 64 2e 20 4f  eft unchanged. O
12b80 72 2c 0a 2a 2a 20 69 66 20 74 68 65 20 66 69 6c  r,.** if the fil
12b81 65 20 73 79 73 74 65 6d 20 68 61 73 20 74 6f 20  e system has to 
12b82 62 65 20 71 75 65 72 69 65 64 20 66 6f 72 20 74  be queried for t
12b83 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66  he size of the f
12b84 69 6c 65 20 61 6e 64 0a 2a 2a 20 74 68 65 20 71  ile and.** the q
12b85 75 65 72 79 20 61 74 74 65 6d 70 74 20 72 65 74  uery attempt ret
12b86 75 72 6e 73 20 61 6e 20 49 4f 20 65 72 72 6f 72  urns an IO error
12b87 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63  , the IO error c
12b88 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 0a  ode is returned.
12b89 2a 2a 20 61 6e 64 20 2a 70 6e 50 61 67 65 20 69  ** and *pnPage i
12b8a 73 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 65 64  s left unchanged
12b8b 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ..**.** Otherwis
12b8c 65 2c 20 69 66 20 65 76 65 72 79 74 68 69 6e 67  e, if everything
12b8d 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20   is successful, 
12b8e 74 68 65 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69  then SQLITE_OK i
12b8f 73 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 6e  s returned.** an
12b90 64 20 2a 70 6e 50 61 67 65 20 69 73 20 73 65 74  d *pnPage is set
12b91 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   to the number o
12b92 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64  f pages in the d
12b93 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49  atabase..*/.SQLI
12b94 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
12b95 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63  qlite3PagerPagec
12b96 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50 61 67  ount(Pager *pPag
12b97 65 72 2c 20 69 6e 74 20 2a 70 6e 50 61 67 65 29  er, int *pnPage)
12b98 7b 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 20  {.  Pgno nPage; 
12b99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
12b9a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e   Value to return
12b9b 20 76 69 61 20 2a 70 6e 50 61 67 65 20 2a 2f 0a   via *pnPage */.
12b9c 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67  .  /* If the pag
12b9d 65 72 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e  er is already in
12b9e 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65   the error state
12b9f 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72  , return the err
12ba0 6f 72 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 69 66  or code. */.  if
12ba1 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  ( pPager->errCod
12ba2 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
12ba3 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b  pPager->errCode;
12ba4 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 74 65 72  .  }..  /* Deter
12ba5 6d 69 6e 65 20 74 68 65 20 6e 75 6d 62 65 72 20  mine the number 
12ba6 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
12ba7 66 69 6c 65 2e 20 53 74 6f 72 65 20 74 68 69 73  file. Store this
12ba8 20 69 6e 20 6e 50 61 67 65 2e 20 2a 2f 0a 20 20   in nPage. */.  
12ba9 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69  if( pPager->dbSi
12baa 7a 65 56 61 6c 69 64 20 29 7b 0a 20 20 20 20 6e  zeValid ){.    n
12bab 50 61 67 65 20 3d 20 70 50 61 67 65 72 2d 3e 64  Page = pPager->d
12bac 62 53 69 7a 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  bSize;.  }else{.
12bad 20 20 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20      int rc;     
12bae 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
12baf 72 72 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79  rror returned by
12bb0 20 4f 73 46 69 6c 65 53 69 7a 65 28 29 20 2a 2f   OsFileSize() */
12bb1 0a 20 20 20 20 69 36 34 20 6e 20 3d 20 30 3b 20  .    i64 n = 0; 
12bb2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
12bb3 46 69 6c 65 20 73 69 7a 65 20 69 6e 20 62 79 74  File size in byt
12bb4 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 4f  es returned by O
12bb5 73 46 69 6c 65 53 69 7a 65 28 29 20 2a 2f 0a 0a  sFileSize() */..
12bb6 20 20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70      assert( isOp
12bb7 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 20 7c  en(pPager->fd) |
12bb8 7c 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69  | pPager->tempFi
12bb9 6c 65 20 29 3b 0a 20 20 20 20 69 66 28 20 69 73  le );.    if( is
12bba 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29  Open(pPager->fd)
12bbb 20 26 26 20 28 30 20 21 3d 20 28 72 63 20 3d 20   && (0 != (rc = 
12bbc 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a  sqlite3OsFileSiz
12bbd 65 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 6e  e(pPager->fd, &n
12bbe 29 29 29 20 29 7b 0a 20 20 20 20 20 20 70 61 67  ))) ){.      pag
12bbf 65 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c  er_error(pPager,
12bc0 20 72 63 29 3b 0a 20 20 20 20 20 20 72 65 74 75   rc);.      retu
12bc1 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
12bc2 20 69 66 28 20 6e 3e 30 20 26 26 20 6e 3c 70 50   if( n>0 && n<pP
12bc3 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 29  ager->pageSize )
12bc4 7b 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20  {.      nPage = 
12bc5 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  1;.    }else{.  
12bc6 20 20 20 20 6e 50 61 67 65 20 3d 20 28 50 67 6e      nPage = (Pgn
12bc7 6f 29 28 6e 20 2f 20 70 50 61 67 65 72 2d 3e 70  o)(n / pPager->p
12bc8 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a  ageSize);.    }.
12bc9 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
12bca 73 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c  state!=PAGER_UNL
12bcb 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61  OCK ){.      pPa
12bcc 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 6e 50  ger->dbSize = nP
12bcd 61 67 65 3b 0a 20 20 20 20 20 20 70 50 61 67 65  age;.      pPage
12bce 72 2d 3e 64 62 46 69 6c 65 53 69 7a 65 20 3d 20  r->dbFileSize = 
12bcf 6e 50 61 67 65 3b 0a 20 20 20 20 20 20 70 50 61  nPage;.      pPa
12bd0 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64  ger->dbSizeValid
12bd1 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 1;.    }.  }.
12bd2 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75 72  .  /* If the cur
12bd3 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 70  rent number of p
12bd4 61 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65  ages in the file
12bd5 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
12bd6 20 74 68 65 20 0a 20 20 2a 2a 20 63 6f 6e 66 69   the .  ** confi
12bd7 67 75 72 65 64 20 6d 61 78 69 6d 75 6d 20 70 61  gured maximum pa
12bd8 67 65 72 20 6e 75 6d 62 65 72 2c 20 69 6e 63 72  ger number, incr
12bd9 65 61 73 65 20 74 68 65 20 61 6c 6c 6f 77 65 64  ease the allowed
12bda 20 6c 69 6d 69 74 20 73 6f 0a 20 20 2a 2a 20 74   limit so.  ** t
12bdb 68 61 74 20 74 68 65 20 66 69 6c 65 20 63 61 6e  hat the file can
12bdc 20 62 65 20 72 65 61 64 2e 0a 20 20 2a 2f 0a 20   be read..  */. 
12bdd 20 69 66 28 20 6e 50 61 67 65 3e 70 50 61 67 65   if( nPage>pPage
12bde 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20 20 20  r->mxPgno ){.   
12bdf 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20   pPager->mxPgno 
12be0 3d 20 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a 20  = (Pgno)nPage;. 
12be1 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65   }..  /* Set the
12be2 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65   output variable
12be3 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49   and return SQLI
12be4 54 45 5f 4f 4b 20 2a 2f 0a 20 20 69 66 28 20 70  TE_OK */.  if( p
12be5 6e 50 61 67 65 20 29 7b 0a 20 20 20 20 2a 70 6e  nPage ){.    *pn
12be6 50 61 67 65 20 3d 20 6e 50 61 67 65 3b 0a 20 20  Page = nPage;.  
12be7 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
12be8 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  E_OK;.}.../*.** 
12be9 54 72 79 20 74 6f 20 6f 62 74 61 69 6e 20 61 20  Try to obtain a 
12bea 6c 6f 63 6b 20 6f 66 20 74 79 70 65 20 6c 6f 63  lock of type loc
12beb 6b 74 79 70 65 20 6f 6e 20 74 68 65 20 64 61 74  ktype on the dat
12bec 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 0a 2a  abase file. If.*
12bed 2a 20 61 20 73 69 6d 69 6c 61 72 20 6f 72 20 67  * a similar or g
12bee 72 65 61 74 65 72 20 6c 6f 63 6b 20 69 73 20 61  reater lock is a
12bef 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 74 68 69  lready held, thi
12bf0 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20  s function is a 
12bf1 6e 6f 2d 6f 70 0a 2a 2a 20 28 72 65 74 75 72 6e  no-op.** (return
12bf2 69 6e 67 20 53 51 4c 49 54 45 5f 4f 4b 20 69 6d  ing SQLITE_OK im
12bf3 6d 65 64 69 61 74 65 6c 79 29 2e 0a 2a 2a 0a 2a  mediately)..**.*
12bf4 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 61 74 74  * Otherwise, att
12bf5 65 6d 70 74 20 74 6f 20 6f 62 74 61 69 6e 20 74  empt to obtain t
12bf6 68 65 20 6c 6f 63 6b 20 75 73 69 6e 67 20 73 71  he lock using sq
12bf7 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 49  lite3OsLock(). I
12bf8 6e 76 6f 6b 65 20 0a 2a 2a 20 74 68 65 20 62 75  nvoke .** the bu
12bf9 73 79 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 74  sy callback if t
12bfa 68 65 20 6c 6f 63 6b 20 69 73 20 63 75 72 72 65  he lock is curre
12bfb 6e 74 6c 79 20 6e 6f 74 20 61 76 61 69 6c 61 62  ntly not availab
12bfc 6c 65 2e 20 52 65 70 65 61 74 20 0a 2a 2a 20 75  le. Repeat .** u
12bfd 6e 74 69 6c 20 74 68 65 20 62 75 73 79 20 63 61  ntil the busy ca
12bfe 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 66  llback returns f
12bff 61 6c 73 65 20 6f 72 20 75 6e 74 69 6c 20 74 68  alse or until th
12c00 65 20 61 74 74 65 6d 70 74 20 74 6f 20 0a 2a 2a  e attempt to .**
12c01 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 6f 63 6b   obtain the lock
12c02 20 73 75 63 63 65 65 64 73 2e 0a 2a 2a 0a 2a 2a   succeeds..**.**
12c03 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
12c04 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64  K on success and
12c05 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   an error code i
12c06 66 20 77 65 20 63 61 6e 6e 6f 74 20 6f 62 74 61  f we cannot obta
12c07 69 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 2e 20  in.** the lock. 
12c08 49 66 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 6f  If the lock is o
12c09 62 74 61 69 6e 65 64 20 73 75 63 63 65 73 73 66  btained successf
12c0a 75 6c 6c 79 2c 20 73 65 74 20 74 68 65 20 50 61  ully, set the Pa
12c0b 67 65 72 2e 73 74 61 74 65 20 0a 2a 2a 20 76 61  ger.state .** va
12c0c 72 69 61 62 6c 65 20 74 6f 20 6c 6f 63 6b 74 79  riable to lockty
12c0d 70 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  pe before return
12c0e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ing..*/.static i
12c0f 6e 74 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e  nt pager_wait_on
12c10 5f 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70 50 61  _lock(Pager *pPa
12c11 67 65 72 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70  ger, int locktyp
12c12 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20 20 20  e){.  int rc;   
12c13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12c14 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
12c15 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20 20  turn code */..  
12c16 2f 2a 20 54 68 65 20 4f 53 20 6c 6f 63 6b 20 76  /* The OS lock v
12c17 61 6c 75 65 73 20 6d 75 73 74 20 62 65 20 74 68  alues must be th
12c18 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 50 61  e same as the Pa
12c19 67 65 72 20 6c 6f 63 6b 20 76 61 6c 75 65 73 20  ger lock values 
12c1a 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 50 41 47  */.  assert( PAG
12c1b 45 52 5f 53 48 41 52 45 44 3d 3d 53 48 41 52 45  ER_SHARED==SHARE
12c1c 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65  D_LOCK );.  asse
12c1d 72 74 28 20 50 41 47 45 52 5f 52 45 53 45 52 56  rt( PAGER_RESERV
12c1e 45 44 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43  ED==RESERVED_LOC
12c1f 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50  K );.  assert( P
12c20 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 3d 3d  AGER_EXCLUSIVE==
12c21 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29  EXCLUSIVE_LOCK )
12c22 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66  ;..  /* If the f
12c23 69 6c 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ile is currently
12c24 20 75 6e 6c 6f 63 6b 65 64 20 74 68 65 6e 20 74   unlocked then t
12c25 68 65 20 73 69 7a 65 20 6d 75 73 74 20 62 65 20  he size must be 
12c26 75 6e 6b 6e 6f 77 6e 20 2a 2f 0a 20 20 61 73 73  unknown */.  ass
12c27 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61  ert( pPager->sta
12c28 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44  te>=PAGER_SHARED
12c29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64 62 53 69   || pPager->dbSi
12c2a 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 0a 20  zeValid==0 );.. 
12c2b 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74   /* Check that t
12c2c 68 69 73 20 69 73 20 65 69 74 68 65 72 20 61 20  his is either a 
12c2d 6e 6f 2d 6f 70 20 28 62 65 63 61 75 73 65 20 74  no-op (because t
12c2e 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63  he requested loc
12c2f 6b 20 69 73 20 0a 20 20 2a 2a 20 61 6c 72 65 61  k is .  ** alrea
12c30 64 79 20 68 65 6c 64 2c 20 6f 72 20 6f 6e 65 20  dy held, or one 
12c31 6f 66 20 74 68 65 20 74 72 61 6e 73 69 73 74 69  of the transisti
12c32 6f 6e 73 20 74 68 61 74 20 74 68 65 20 62 75 73  ons that the bus
12c33 79 2d 68 61 6e 64 6c 65 72 0a 20 20 2a 2a 20 6d  y-handler.  ** m
12c34 61 79 20 62 65 20 69 6e 76 6f 6b 65 64 20 64 75  ay be invoked du
12c35 72 69 6e 67 2c 20 61 63 63 6f 72 64 69 6e 67 20  ring, according 
12c36 74 6f 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 61  to the comment a
12c37 62 6f 76 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65  bove.  ** sqlite
12c38 33 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e  3PagerSetBusyhan
12c39 64 6c 65 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 61  dler()..  */.  a
12c3a 73 73 65 72 74 28 20 28 70 50 61 67 65 72 2d 3e  ssert( (pPager->
12c3b 73 74 61 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 29  state>=locktype)
12c3c 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 50 61 67  .       || (pPag
12c3d 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52  er->state==PAGER
12c3e 5f 55 4e 4c 4f 43 4b 20 26 26 20 6c 6f 63 6b 74  _UNLOCK && lockt
12c3f 79 70 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 45  ype==PAGER_SHARE
12c40 44 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 50  D).       || (pP
12c41 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47  ager->state==PAG
12c42 45 52 5f 52 45 53 45 52 56 45 44 20 26 26 20 6c  ER_RESERVED && l
12c43 6f 63 6b 74 79 70 65 3d 3d 50 41 47 45 52 5f 45  ocktype==PAGER_E
12c44 58 43 4c 55 53 49 56 45 29 0a 20 20 29 3b 0a 0a  XCLUSIVE).  );..
12c45 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74    if( pPager->st
12c46 61 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b  ate>=locktype ){
12c47 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
12c48 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  _OK;.  }else{.  
12c49 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 72 63 20    do {.      rc 
12c4a 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28  = sqlite3OsLock(
12c4b 70 50 61 67 65 72 2d 3e 66 64 2c 20 6c 6f 63 6b  pPager->fd, lock
12c4c 74 79 70 65 29 3b 0a 20 20 20 20 7d 77 68 69 6c  type);.    }whil
12c4d 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55  e( rc==SQLITE_BU
12c4e 53 59 20 26 26 20 70 50 61 67 65 72 2d 3e 78 42  SY && pPager->xB
12c4f 75 73 79 48 61 6e 64 6c 65 72 28 70 50 61 67 65  usyHandler(pPage
12c50 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72 41  r->pBusyHandlerA
12c51 72 67 29 20 29 3b 0a 20 20 20 20 69 66 28 20 72  rg) );.    if( r
12c52 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
12c53 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74        pPager->st
12c54 61 74 65 20 3d 20 28 75 38 29 6c 6f 63 6b 74 79  ate = (u8)lockty
12c55 70 65 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43  pe;.      IOTRAC
12c56 45 28 28 22 4c 4f 43 4b 20 25 70 20 25 64 5c 6e  E(("LOCK %p %d\n
12c57 22 2c 20 70 50 61 67 65 72 2c 20 6c 6f 63 6b 74  ", pPager, lockt
12c58 79 70 65 29 29 0a 20 20 20 20 7d 0a 20 20 7d 0a  ype)).    }.  }.
12c59 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
12c5a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 61  /*.** Function a
12c5b 73 73 65 72 74 54 72 75 6e 63 61 74 65 43 6f 6e  ssertTruncateCon
12c5c 73 74 72 61 69 6e 74 28 70 50 61 67 65 72 29 20  straint(pPager) 
12c5d 63 68 65 63 6b 73 20 74 68 61 74 20 6f 6e 65 20  checks that one 
12c5e 6f 66 20 74 68 65 20 0a 2a 2a 20 66 6f 6c 6c 6f  of the .** follo
12c5f 77 69 6e 67 20 69 73 20 74 72 75 65 20 66 6f 72  wing is true for
12c60 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73   all dirty pages
12c61 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68   currently in th
12c62 65 20 70 61 67 65 2d 63 61 63 68 65 3a 0a 2a 2a  e page-cache:.**
12c63 0a 2a 2a 20 20 20 61 29 20 54 68 65 20 70 61 67  .**   a) The pag
12c64 65 20 6e 75 6d 62 65 72 20 69 73 20 6c 65 73 73  e number is less
12c65 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
12c66 6f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68  o the size of th
12c67 65 20 0a 2a 2a 20 20 20 20 20 20 63 75 72 72 65  e .**      curre
12c68 6e 74 20 64 61 74 61 62 61 73 65 20 69 6d 61 67  nt database imag
12c69 65 2c 20 69 6e 20 70 61 67 65 73 2c 20 4f 52 0a  e, in pages, OR.
12c6a 2a 2a 0a 2a 2a 20 20 20 62 29 20 69 66 20 74 68  **.**   b) if th
12c6b 65 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 20 77  e page content w
12c6c 65 72 65 20 77 72 69 74 74 65 6e 20 61 74 20 74  ere written at t
12c6d 68 69 73 20 74 69 6d 65 2c 20 69 74 20 77 6f 75  his time, it wou
12c6e 6c 64 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 62  ld not.**      b
12c6f 65 20 6e 65 63 65 73 73 61 72 79 20 74 6f 20 77  e necessary to w
12c70 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74  rite the current
12c71 20 63 6f 6e 74 65 6e 74 20 6f 75 74 20 74 6f 20   content out to 
12c72 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a  the sub-journal.
12c73 2a 2a 20 20 20 20 20 20 28 61 73 20 64 65 74 65  **      (as dete
12c74 72 6d 69 6e 65 64 20 62 79 20 66 75 6e 63 74 69  rmined by functi
12c75 6f 6e 20 73 75 62 6a 52 65 71 75 69 72 65 73 50  on subjRequiresP
12c76 61 67 65 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 49 66  age())..**.** If
12c77 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 61   the condition a
12c78 73 73 65 72 74 65 64 20 62 79 20 74 68 69 73 20  sserted by this 
12c79 66 75 6e 63 74 69 6f 6e 20 77 65 72 65 20 6e 6f  function were no
12c7a 74 20 74 72 75 65 2c 20 61 6e 64 20 74 68 65 0a  t true, and the.
12c7b 2a 2a 20 64 69 72 74 79 20 70 61 67 65 20 77 65  ** dirty page we
12c7c 72 65 20 74 6f 20 62 65 20 64 69 73 63 61 72 64  re to be discard
12c7d 65 64 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68  ed from the cach
12c7e 65 20 76 69 61 20 74 68 65 20 70 61 67 65 72 53  e via the pagerS
12c7f 74 72 65 73 73 28 29 0a 2a 2a 20 72 6f 75 74 69  tress().** routi
12c80 6e 65 2c 20 70 61 67 65 72 53 74 72 65 73 73 28  ne, pagerStress(
12c81 29 20 77 6f 75 6c 64 20 6e 6f 74 20 77 72 69 74  ) would not writ
12c82 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 70 61  e the current pa
12c83 67 65 20 63 6f 6e 74 65 6e 74 20 74 6f 0a 2a 2a  ge content to.**
12c84 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
12c85 6c 65 2e 20 49 66 20 61 20 73 61 76 65 70 6f 69  le. If a savepoi
12c86 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  nt transaction w
12c87 65 72 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20  ere rolled back 
12c88 61 66 74 65 72 0a 2a 2a 20 74 68 69 73 20 68 61  after.** this ha
12c89 70 70 65 6e 65 64 2c 20 74 68 65 20 63 6f 72 72  ppened, the corr
12c8a 65 63 74 20 62 65 68 61 76 69 6f 75 72 20 77 6f  ect behaviour wo
12c8b 75 6c 64 20 62 65 20 74 6f 20 72 65 73 74 6f 72  uld be to restor
12c8c 65 20 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a  e the current.**
12c8d 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20   content of the 
12c8e 70 61 67 65 2e 20 48 6f 77 65 76 65 72 2c 20 73  page. However, s
12c8f 69 6e 63 65 20 74 68 69 73 20 63 6f 6e 74 65 6e  ince this conten
12c90 74 20 69 73 20 6e 6f 74 20 70 72 65 73 65 6e 74  t is not present
12c91 20 69 6e 20 65 69 74 68 65 72 0a 2a 2a 20 74 68   in either.** th
12c92 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
12c93 6f 72 20 74 68 65 20 70 6f 72 74 69 6f 6e 20 6f  or the portion o
12c94 66 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a  f the rollback j
12c95 6f 75 72 6e 61 6c 20 61 6e 64 20 0a 2a 2a 20 73  ournal and .** s
12c96 75 62 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 65  ub-journal rolle
12c97 64 20 62 61 63 6b 20 74 68 65 20 63 6f 6e 74 65  d back the conte
12c98 6e 74 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  nt could not be 
12c99 72 65 73 74 6f 72 65 64 20 61 6e 64 20 74 68 65  restored and the
12c9a 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 69 6d 61  .** database ima
12c9b 67 65 20 77 6f 75 6c 64 20 62 65 63 6f 6d 65 20  ge would become 
12c9c 63 6f 72 72 75 70 74 2e 20 49 74 20 69 73 20 74  corrupt. It is t
12c9d 68 65 72 65 66 6f 72 65 20 66 6f 72 74 75 6e 61  herefore fortuna
12c9e 74 65 20 74 68 61 74 20 0a 2a 2a 20 74 68 69 73  te that .** this
12c9f 20 63 69 72 63 75 6d 73 74 61 6e 63 65 20 63 61   circumstance ca
12ca0 6e 6e 6f 74 20 61 72 69 73 65 2e 0a 2a 2f 0a 23  nnot arise..*/.#
12ca1 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
12ca2 45 5f 44 45 42 55 47 29 0a 73 74 61 74 69 63 20  E_DEBUG).static 
12ca3 76 6f 69 64 20 61 73 73 65 72 74 54 72 75 6e 63  void assertTrunc
12ca4 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 62 28  ateConstraintCb(
12ca5 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 61  PgHdr *pPg){.  a
12ca6 73 73 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67  ssert( pPg->flag
12ca7 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b  s&PGHDR_DIRTY );
12ca8 0a 20 20 61 73 73 65 72 74 28 20 21 73 75 62 6a  .  assert( !subj
12ca9 52 65 71 75 69 72 65 73 50 61 67 65 28 70 50 67  RequiresPage(pPg
12caa 29 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d  ) || pPg->pgno<=
12cab 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 64 62 53  pPg->pPager->dbS
12cac 69 7a 65 20 29 3b 0a 7d 0a 73 74 61 74 69 63 20  ize );.}.static 
12cad 76 6f 69 64 20 61 73 73 65 72 74 54 72 75 6e 63  void assertTrunc
12cae 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 50 61  ateConstraint(Pa
12caf 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
12cb0 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 74 65  sqlite3PcacheIte
12cb1 72 61 74 65 44 69 72 74 79 28 70 50 61 67 65 72  rateDirty(pPager
12cb2 2d 3e 70 50 43 61 63 68 65 2c 20 61 73 73 65 72  ->pPCache, asser
12cb3 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61  tTruncateConstra
12cb4 69 6e 74 43 62 29 3b 0a 7d 0a 23 65 6c 73 65 0a  intCb);.}.#else.
12cb5 23 20 64 65 66 69 6e 65 20 61 73 73 65 72 74 54  # define assertT
12cb6 72 75 6e 63 61 74 65 43 6f 6e 73 74 72 61 69 6e  runcateConstrain
12cb7 74 28 70 50 61 67 65 72 29 0a 23 65 6e 64 69 66  t(pPager).#endif
12cb8 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65  ../*.** Truncate
12cb9 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 64   the in-memory d
12cba 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6d 61  atabase file ima
12cbb 67 65 20 74 6f 20 6e 50 61 67 65 20 70 61 67 65  ge to nPage page
12cbc 73 2e 20 54 68 69 73 20 0a 2a 2a 20 66 75 6e 63  s. This .** func
12cbd 74 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 61 63  tion does not ac
12cbe 74 75 61 6c 6c 79 20 6d 6f 64 69 66 79 20 74 68  tually modify th
12cbf 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
12cc0 6f 6e 20 64 69 73 6b 2e 20 49 74 20 0a 2a 2a 20  on disk. It .** 
12cc1 6a 75 73 74 20 73 65 74 73 20 74 68 65 20 69 6e  just sets the in
12cc2 74 65 72 6e 61 6c 20 73 74 61 74 65 20 6f 66 20  ternal state of 
12cc3 74 68 65 20 70 61 67 65 72 20 6f 62 6a 65 63 74  the pager object
12cc4 20 73 6f 20 74 68 61 74 20 74 68 65 20 0a 2a 2a   so that the .**
12cc5 20 74 72 75 6e 63 61 74 69 6f 6e 20 77 69 6c 6c   truncation will
12cc6 20 62 65 20 64 6f 6e 65 20 77 68 65 6e 20 74 68   be done when th
12cc7 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61  e current transa
12cc8 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74  ction is committ
12cc9 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
12cca 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
12ccb 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49  e3PagerTruncateI
12ccc 6d 61 67 65 28 50 61 67 65 72 20 2a 70 50 61 67  mage(Pager *pPag
12ccd 65 72 2c 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b  er, Pgno nPage){
12cce 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
12ccf 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 29  r->dbSizeValid )
12cd0 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
12cd1 65 72 2d 3e 64 62 53 69 7a 65 3e 3d 6e 50 61 67  er->dbSize>=nPag
12cd2 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  e );.  assert( p
12cd3 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41  Pager->state>=PA
12cd4 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a  GER_RESERVED );.
12cd5 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
12cd6 20 3d 20 6e 50 61 67 65 3b 0a 20 20 61 73 73 65   = nPage;.  asse
12cd7 72 74 54 72 75 6e 63 61 74 65 43 6f 6e 73 74 72  rtTruncateConstr
12cd8 61 69 6e 74 28 70 50 61 67 65 72 29 3b 0a 7d 0a  aint(pPager);.}.
12cd9 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20  ./*.** Shutdown 
12cda 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2e 20  the page cache. 
12cdb 20 46 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79   Free all memory
12cdc 20 61 6e 64 20 63 6c 6f 73 65 20 61 6c 6c 20 66   and close all f
12cdd 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  iles..**.** If a
12cde 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73   transaction was
12cdf 20 69 6e 20 70 72 6f 67 72 65 73 73 20 77 68 65   in progress whe
12ce0 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
12ce1 73 20 63 61 6c 6c 65 64 2c 20 74 68 61 74 0a 2a  s called, that.*
12ce2 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  * transaction is
12ce3 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41   rolled back.  A
12ce4 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70  ll outstanding p
12ce5 61 67 65 73 20 61 72 65 20 69 6e 76 61 6c 69 64  ages are invalid
12ce6 61 74 65 64 0a 2a 2a 20 61 6e 64 20 74 68 65 69  ated.** and thei
12ce7 72 20 6d 65 6d 6f 72 79 20 69 73 20 66 72 65 65  r memory is free
12ce8 64 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20  d.  Any attempt 
12ce9 74 6f 20 75 73 65 20 61 20 70 61 67 65 20 61 73  to use a page as
12cea 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68  sociated.** with
12ceb 20 74 68 69 73 20 70 61 67 65 20 63 61 63 68 65   this page cache
12cec 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63   after this func
12ced 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 77 69 6c  tion returns wil
12cee 6c 20 6c 69 6b 65 6c 79 0a 2a 2a 20 72 65 73 75  l likely.** resu
12cef 6c 74 20 69 6e 20 61 20 63 6f 72 65 64 75 6d 70  lt in a coredump
12cf0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
12cf1 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 73 75 63  ction always suc
12cf2 63 65 65 64 73 2e 20 49 66 20 61 20 74 72 61 6e  ceeds. If a tran
12cf3 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76  saction is activ
12cf4 65 20 61 6e 20 61 74 74 65 6d 70 74 0a 2a 2a 20  e an attempt.** 
12cf5 69 73 20 6d 61 64 65 20 74 6f 20 72 6f 6c 6c 20  is made to roll 
12cf6 69 74 20 62 61 63 6b 2e 20 49 66 20 61 6e 20 65  it back. If an e
12cf7 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69  rror occurs duri
12cf8 6e 67 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  ng the rollback 
12cf9 0a 2a 2a 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61  .** a hot journa
12cfa 6c 20 6d 61 79 20 62 65 20 6c 65 66 74 20 69 6e  l may be left in
12cfb 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d 20   the filesystem 
12cfc 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 69 73 20  but no error is 
12cfd 72 65 74 75 72 6e 65 64 0a 2a 2a 20 74 6f 20 74  returned.** to t
12cfe 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 53 51  he caller..*/.SQ
12cff 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
12d00 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f   sqlite3PagerClo
12d01 73 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  se(Pager *pPager
12d02 29 7b 0a 20 20 64 69 73 61 62 6c 65 5f 73 69 6d  ){.  disable_sim
12d03 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73  ulated_io_errors
12d04 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65 67  ();.  sqlite3Beg
12d05 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  inBenignMalloc()
12d06 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 72 72 43  ;.  pPager->errC
12d07 6f 64 65 20 3d 20 30 3b 0a 20 20 70 50 61 67 65  ode = 0;.  pPage
12d08 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  r->exclusiveMode
12d09 20 3d 20 30 3b 0a 20 20 70 61 67 65 72 5f 72 65   = 0;.  pager_re
12d0a 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 69  set(pPager);.  i
12d0b 66 28 20 4d 45 4d 44 42 20 29 7b 0a 20 20 20 20  f( MEMDB ){.    
12d0c 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 50 61  pager_unlock(pPa
12d0d 67 65 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ger);.  }else{. 
12d0e 20 20 20 2f 2a 20 53 65 74 20 50 61 67 65 72 2e     /* Set Pager.
12d0f 6a 6f 75 72 6e 61 6c 48 64 72 20 74 6f 20 2d 31  journalHdr to -1
12d10 20 66 6f 72 20 74 68 65 20 62 65 6e 65 66 69 74   for the benefit
12d11 20 6f 66 20 74 68 65 20 70 61 67 65 72 5f 70 6c   of the pager_pl
12d12 61 79 62 61 63 6b 28 29 20 0a 20 20 20 20 2a 2a  ayback() .    **
12d13 20 63 61 6c 6c 20 77 68 69 63 68 20 6d 61 79 20   call which may 
12d14 62 65 20 6d 61 64 65 20 66 72 6f 6d 20 77 69 74  be made from wit
12d15 68 69 6e 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41  hin pagerUnlockA
12d16 6e 64 52 6f 6c 6c 62 61 63 6b 28 29 2e 20 49 66  ndRollback(). If
12d17 20 69 74 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f   it.    ** is no
12d18 74 20 2d 31 2c 20 74 68 65 6e 20 74 68 65 20 75  t -1, then the u
12d19 6e 73 79 6e 63 65 64 20 70 6f 72 74 69 6f 6e 20  nsynced portion 
12d1a 6f 66 20 61 6e 20 6f 70 65 6e 20 6a 6f 75 72 6e  of an open journ
12d1b 61 6c 20 66 69 6c 65 20 6d 61 79 0a 20 20 20 20  al file may.    
12d1c 2a 2a 20 62 65 20 70 6c 61 79 65 64 20 62 61 63  ** be played bac
12d1d 6b 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62  k into the datab
12d1e 61 73 65 2e 20 49 66 20 61 20 70 6f 77 65 72 20  ase. If a power 
12d1f 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 20 77  failure occurs w
12d20 68 69 6c 65 0a 20 20 20 20 2a 2a 20 74 68 69 73  hile.    ** this
12d21 20 69 73 20 68 61 70 70 65 6e 69 6e 67 2c 20 74   is happening, t
12d22 68 65 20 64 61 74 61 62 61 73 65 20 6d 61 79 20  he database may 
12d23 62 65 63 6f 6d 65 20 63 6f 72 72 75 70 74 2e 0a  become corrupt..
12d24 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 61 67 65      */.    pPage
12d25 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20  r->journalHdr = 
12d26 2d 31 3b 0a 20 20 20 20 70 61 67 65 72 55 6e 6c  -1;.    pagerUnl
12d27 6f 63 6b 41 6e 64 52 6f 6c 6c 62 61 63 6b 28 70  ockAndRollback(p
12d28 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 73 71  Pager);.  }.  sq
12d29 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61  lite3EndBenignMa
12d2a 6c 6c 6f 63 28 29 3b 0a 20 20 65 6e 61 62 6c 65  lloc();.  enable
12d2b 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72  _simulated_io_er
12d2c 72 6f 72 73 28 29 3b 0a 20 20 50 41 47 45 52 54  rors();.  PAGERT
12d2d 52 41 43 45 28 28 22 43 4c 4f 53 45 20 25 64 5c  RACE(("CLOSE %d\
12d2e 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67  n", PAGERID(pPag
12d2f 65 72 29 29 29 3b 0a 20 20 49 4f 54 52 41 43 45  er)));.  IOTRACE
12d30 28 28 22 43 4c 4f 53 45 20 25 70 5c 6e 22 2c 20  (("CLOSE %p\n", 
12d31 70 50 61 67 65 72 29 29 0a 20 20 73 71 6c 69 74  pPager)).  sqlit
12d32 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72  e3OsClose(pPager
12d33 2d 3e 66 64 29 3b 0a 20 20 73 71 6c 69 74 65 33  ->fd);.  sqlite3
12d34 50 61 67 65 46 72 65 65 28 70 50 61 67 65 72 2d  PageFree(pPager-
12d35 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 73  >pTmpSpace);.  s
12d36 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73  qlite3PcacheClos
12d37 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  e(pPager->pPCach
12d38 65 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  e);..#ifdef SQLI
12d39 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20 69  TE_HAS_CODEC.  i
12d3a 66 28 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65  f( pPager->xCode
12d3b 63 46 72 65 65 20 29 20 70 50 61 67 65 72 2d 3e  cFree ) pPager->
12d3c 78 43 6f 64 65 63 46 72 65 65 28 70 50 61 67 65  xCodecFree(pPage
12d3d 72 2d 3e 70 43 6f 64 65 63 29 3b 0a 23 65 6e 64  r->pCodec);.#end
12d3e 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70  if..  assert( !p
12d3f 50 61 67 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e  Pager->aSavepoin
12d40 74 20 26 26 20 21 70 50 61 67 65 72 2d 3e 70 49  t && !pPager->pI
12d41 6e 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73  nJournal );.  as
12d42 73 65 72 74 28 20 21 69 73 4f 70 65 6e 28 70 50  sert( !isOpen(pP
12d43 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20 21 69  ager->jfd) && !i
12d44 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a  sOpen(pPager->sj
12d45 66 64 29 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65  fd) );..  sqlite
12d46 33 5f 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a  3_free(pPager);.
12d47 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
12d48 4f 4b 3b 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69  OK;.}..#if !defi
12d49 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64  ned(NDEBUG) || d
12d4a 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45  efined(SQLITE_TE
12d4b 53 54 29 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  ST)./*.** Return
12d4c 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
12d4d 20 66 6f 72 20 70 61 67 65 20 70 50 67 2e 0a 2a   for page pPg..*
12d4e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
12d4f 20 50 67 6e 6f 20 73 71 6c 69 74 65 33 50 61 67   Pgno sqlite3Pag
12d50 65 72 50 61 67 65 6e 75 6d 62 65 72 28 44 62 50  erPagenumber(DbP
12d51 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 72 65 74  age *pPg){.  ret
12d52 75 72 6e 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 7d  urn pPg->pgno;.}
12d53 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49  .#endif../*.** I
12d54 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66  ncrement the ref
12d55 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72  erence count for
12d56 20 70 61 67 65 20 70 50 67 2e 0a 2a 2f 0a 53 51   page pPg..*/.SQ
12d57 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
12d58 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65  d sqlite3PagerRe
12d59 66 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a  f(DbPage *pPg){.
12d5a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52    sqlite3PcacheR
12d5b 65 66 28 70 50 67 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ef(pPg);.}../*.*
12d5c 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e  * Sync the journ
12d5d 61 6c 2e 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  al. In other wor
12d5e 64 73 2c 20 6d 61 6b 65 20 73 75 72 65 20 61 6c  ds, make sure al
12d5f 6c 20 74 68 65 20 70 61 67 65 73 20 74 68 61 74  l the pages that
12d60 20 68 61 76 65 0a 2a 2a 20 62 65 65 6e 20 77 72   have.** been wr
12d61 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75  itten to the jou
12d62 72 6e 61 6c 20 68 61 76 65 20 61 63 74 75 61 6c  rnal have actual
12d63 6c 79 20 72 65 61 63 68 65 64 20 74 68 65 20 73  ly reached the s
12d64 75 72 66 61 63 65 20 6f 66 20 74 68 65 0a 2a 2a  urface of the.**
12d65 20 64 69 73 6b 20 61 6e 64 20 63 61 6e 20 62 65   disk and can be
12d66 20 72 65 73 74 6f 72 65 64 20 69 6e 20 74 68 65   restored in the
12d67 20 65 76 65 6e 74 20 6f 66 20 61 20 68 6f 74 2d   event of a hot-
12d68 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b  journal rollback
12d69 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50  ..**.** If the P
12d6a 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c  ager.needSync fl
12d6b 61 67 20 69 73 20 6e 6f 74 20 73 65 74 2c 20 74  ag is not set, t
12d6c 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
12d6d 6e 20 69 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 2e  n is a.** no-op.
12d6e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20   Otherwise, the 
12d6f 61 63 74 69 6f 6e 73 20 72 65 71 75 69 72 65 64  actions required
12d70 20 64 65 70 65 6e 64 20 6f 6e 20 74 68 65 20 6a   depend on the j
12d71 6f 75 72 6e 61 6c 2d 6d 6f 64 65 0a 2a 2a 20 61  ournal-mode.** a
12d72 6e 64 20 74 68 65 20 64 65 76 69 63 65 20 63 68  nd the device ch
12d73 61 72 61 63 74 65 72 69 73 74 69 63 73 20 6f 66  aracteristics of
12d74 20 74 68 65 20 74 68 65 20 66 69 6c 65 2d 73 79   the the file-sy
12d75 73 74 65 6d 2c 20 61 73 20 66 6f 6c 6c 6f 77 73  stem, as follows
12d76 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 49 66 20 74  :.**.**   * If t
12d77 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
12d78 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  is an in-memory 
12d79 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20 6e 6f  journal file, no
12d7a 20 61 63 74 69 6f 6e 20 6e 65 65 64 0a 2a 2a 20   action need.** 
12d7b 20 20 20 20 62 65 20 74 61 6b 65 6e 2e 0a 2a 2a      be taken..**
12d7c 0a 2a 2a 20 20 20 2a 20 4f 74 68 65 72 77 69 73  .**   * Otherwis
12d7d 65 2c 20 69 66 20 74 68 65 20 64 65 76 69 63 65  e, if the device
12d7e 20 64 6f 65 73 20 6e 6f 74 20 73 75 70 70 6f 72   does not suppor
12d7f 74 20 74 68 65 20 53 41 46 45 5f 41 50 50 45 4e  t the SAFE_APPEN
12d80 44 20 70 72 6f 70 65 72 74 79 2c 0a 2a 2a 20 20  D property,.**  
12d81 20 20 20 74 68 65 6e 20 74 68 65 20 6e 52 65 63     then the nRec
12d82 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 6d 6f   field of the mo
12d83 73 74 20 72 65 63 65 6e 74 6c 79 20 77 72 69 74  st recently writ
12d84 74 65 6e 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  ten journal head
12d85 65 72 0a 2a 2a 20 20 20 20 20 69 73 20 75 70 64  er.**     is upd
12d86 61 74 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20  ated to contain 
12d87 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6a 6f  the number of jo
12d88 75 72 6e 61 6c 20 72 65 63 6f 72 64 73 20 74 68  urnal records th
12d89 61 74 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 62  at have.**     b
12d8a 65 65 6e 20 77 72 69 74 74 65 6e 20 66 6f 6c 6c  een written foll
12d8b 6f 77 69 6e 67 20 69 74 2e 20 49 66 20 74 68 65  owing it. If the
12d8c 20 70 61 67 65 72 20 69 73 20 6f 70 65 72 61 74   pager is operat
12d8d 69 6e 67 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63  ing in full-sync
12d8e 0a 2a 2a 20 20 20 20 20 6d 6f 64 65 2c 20 74 68  .**     mode, th
12d8f 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  en the journal f
12d90 69 6c 65 20 69 73 20 73 79 6e 63 65 64 20 62 65  ile is synced be
12d91 66 6f 72 65 20 74 68 69 73 20 66 69 65 6c 64 20  fore this field 
12d92 69 73 20 75 70 64 61 74 65 64 2e 0a 2a 2a 0a 2a  is updated..**.*
12d93 2a 20 20 20 2a 20 49 66 20 74 68 65 20 64 65 76  *   * If the dev
12d94 69 63 65 20 64 6f 65 73 20 6e 6f 74 20 73 75 70  ice does not sup
12d95 70 6f 72 74 20 74 68 65 20 53 45 51 55 45 4e 54  port the SEQUENT
12d96 49 41 4c 20 70 72 6f 70 65 72 74 79 2c 20 74 68  IAL property, th
12d97 65 6e 20 0a 2a 2a 20 20 20 20 20 6a 6f 75 72 6e  en .**     journ
12d98 61 6c 20 66 69 6c 65 20 69 73 20 73 79 6e 63 65  al file is synce
12d99 64 2e 0a 2a 2a 0a 2a 2a 20 4f 72 2c 20 69 6e 20  d..**.** Or, in 
12d9a 70 73 65 75 64 6f 2d 63 6f 64 65 3a 0a 2a 2a 0a  pseudo-code:.**.
12d9b 2a 2a 20 20 20 69 66 28 20 4e 4f 54 20 3c 69 6e  **   if( NOT <in
12d9c 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61 6c 3e  -memory journal>
12d9d 20 29 7b 0a 2a 2a 20 20 20 20 20 69 66 28 20 4e   ){.**     if( N
12d9e 4f 54 20 53 41 46 45 5f 41 50 50 45 4e 44 20 29  OT SAFE_APPEND )
12d9f 7b 0a 2a 2a 20 20 20 20 20 20 20 69 66 28 20 3c  {.**       if( <
12da0 66 75 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 3e 20  full-sync mode> 
12da1 29 20 78 53 79 6e 63 28 3c 6a 6f 75 72 6e 61 6c  ) xSync(<journal
12da2 20 66 69 6c 65 3e 29 3b 0a 2a 2a 20 20 20 20 20   file>);.**     
12da3 20 20 3c 75 70 64 61 74 65 20 6e 52 65 63 20 66    <update nRec f
12da4 69 65 6c 64 3e 0a 2a 2a 20 20 20 20 20 7d 20 0a  ield>.**     } .
12da5 2a 2a 20 20 20 20 20 69 66 28 20 4e 4f 54 20 53  **     if( NOT S
12da6 45 51 55 45 4e 54 49 41 4c 20 29 20 78 53 79 6e  EQUENTIAL ) xSyn
12da7 63 28 3c 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 3e  c(<journal file>
12da8 29 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20  );.**   }.**.** 
12da9 54 68 65 20 50 61 67 65 72 2e 6e 65 65 64 53 79  The Pager.needSy
12daa 6e 63 20 66 6c 61 67 20 69 73 20 6e 65 76 65 72  nc flag is never
12dab 20 62 65 20 73 65 74 20 66 6f 72 20 74 65 6d 70   be set for temp
12dac 6f 72 61 72 79 20 66 69 6c 65 73 2c 20 6f 72 20  orary files, or 
12dad 61 6e 79 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 72  any.** file oper
12dae 61 74 69 6e 67 20 69 6e 20 6e 6f 2d 73 79 6e 63  ating in no-sync
12daf 20 6d 6f 64 65 20 28 50 61 67 65 72 2e 6e 6f 53   mode (Pager.noS
12db0 79 6e 63 20 73 65 74 20 74 6f 20 6e 6f 6e 2d 7a  ync set to non-z
12db1 65 72 6f 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73  ero)..**.** If s
12db2 75 63 63 65 73 73 66 75 6c 2c 20 74 68 69 73 20  uccessful, this 
12db3 72 6f 75 74 69 6e 65 20 63 6c 65 61 72 73 20 74  routine clears t
12db4 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  he PGHDR_NEED_SY
12db5 4e 43 20 66 6c 61 67 20 6f 66 20 65 76 65 72 79  NC flag of every
12db6 20 0a 2a 2a 20 70 61 67 65 20 63 75 72 72 65 6e   .** page curren
12db7 74 6c 79 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f  tly held in memo
12db8 72 79 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  ry before return
12db9 69 6e 67 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 49  ing SQLITE_OK. I
12dba 66 20 61 6e 20 49 4f 0a 2a 2a 20 65 72 72 6f 72  f an IO.** error
12dbb 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c   is encountered,
12dbc 20 74 68 65 6e 20 74 68 65 20 49 4f 20 65 72 72   then the IO err
12dbd 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
12dbe 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65  ned to the calle
12dbf 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
12dc0 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 61 67   syncJournal(Pag
12dc1 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69  er *pPager){.  i
12dc2 66 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53  f( pPager->needS
12dc3 79 6e 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ync ){.    asser
12dc4 74 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70  t( !pPager->temp
12dc5 46 69 6c 65 20 29 3b 0a 20 20 20 20 69 66 28 20  File );.    if( 
12dc6 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
12dc7 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode!=PAGER_JOURN
12dc8 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b  ALMODE_MEMORY ){
12dc9 0a 20 20 20 20 20 20 69 6e 74 20 72 63 3b 20 20  .      int rc;  
12dca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12dcb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
12dcc 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
12dcd 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 44      const int iD
12dce 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76  c = sqlite3OsDev
12dcf 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
12dd0 63 73 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a  cs(pPager->fd);.
12dd1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 73        assert( is
12dd2 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
12dd3 29 20 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20  ) );..      if( 
12dd4 30 3d 3d 28 69 44 63 26 53 51 4c 49 54 45 5f 49  0==(iDc&SQLITE_I
12dd5 4f 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44  OCAP_SAFE_APPEND
12dd6 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ) ){.        /* 
12dd7 54 68 69 73 20 62 6c 6f 63 6b 20 64 65 61 6c 73  This block deals
12dd8 20 77 69 74 68 20 61 6e 20 6f 62 73 63 75 72 65   with an obscure
12dd9 20 70 72 6f 62 6c 65 6d 2e 20 49 66 20 74 68 65   problem. If the
12dda 20 6c 61 73 74 20 63 6f 6e 6e 65 63 74 69 6f 6e   last connection
12ddb 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74  .        ** that
12ddc 20 77 72 6f 74 65 20 74 6f 20 74 68 69 73 20 64   wrote to this d
12ddd 61 74 61 62 61 73 65 20 77 61 73 20 6f 70 65 72  atabase was oper
12dde 61 74 69 6e 67 20 69 6e 20 70 65 72 73 69 73 74  ating in persist
12ddf 65 6e 74 2d 6a 6f 75 72 6e 61 6c 0a 20 20 20 20  ent-journal.    
12de0 20 20 20 20 2a 2a 20 6d 6f 64 65 2c 20 74 68 65      ** mode, the
12de1 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  n the journal fi
12de2 6c 65 20 6d 61 79 20 61 74 20 74 68 69 73 20 70  le may at this p
12de3 6f 69 6e 74 20 61 63 74 75 61 6c 6c 79 20 62 65  oint actually be
12de4 20 6c 61 72 67 65 72 0a 20 20 20 20 20 20 20 20   larger.        
12de5 2a 2a 20 74 68 61 6e 20 50 61 67 65 72 2e 6a 6f  ** than Pager.jo
12de6 75 72 6e 61 6c 4f 66 66 20 62 79 74 65 73 2e 20  urnalOff bytes. 
12de7 49 66 20 74 68 65 20 6e 65 78 74 20 74 68 69 6e  If the next thin
12de8 67 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  g in the journal
12de9 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 69 6c 65  .        ** file
12dea 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20 61   happens to be a
12deb 20 6a 6f 75 72 6e 61 6c 2d 68 65 61 64 65 72 20   journal-header 
12dec 28 77 72 69 74 74 65 6e 20 61 73 20 70 61 72 74  (written as part
12ded 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20 20   of the.        
12dee 2a 2a 20 70 72 65 76 69 6f 75 73 20 63 6f 6e 6e  ** previous conn
12def 65 63 74 69 6f 6e 73 20 74 72 61 6e 73 61 63 74  ections transact
12df0 69 6f 6e 29 2c 20 61 6e 64 20 61 20 63 72 61 73  ion), and a cras
12df1 68 20 6f 72 20 70 6f 77 65 72 2d 66 61 69 6c 75  h or power-failu
12df2 72 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f  re .        ** o
12df3 63 63 75 72 73 20 61 66 74 65 72 20 6e 52 65 63  ccurs after nRec
12df4 20 69 73 20 75 70 64 61 74 65 64 20 62 75 74 20   is updated but 
12df5 62 65 66 6f 72 65 20 74 68 69 73 20 63 6f 6e 6e  before this conn
12df6 65 63 74 69 6f 6e 20 77 72 69 74 65 73 20 0a 20  ection writes . 
12df7 20 20 20 20 20 20 20 2a 2a 20 61 6e 79 74 68 69         ** anythi
12df8 6e 67 20 65 6c 73 65 20 74 6f 20 74 68 65 20 6a  ng else to the j
12df9 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f 72 20  ournal file (or 
12dfa 63 6f 6d 6d 69 74 73 2f 72 6f 6c 6c 73 20 62 61  commits/rolls ba
12dfb 63 6b 20 69 74 73 20 0a 20 20 20 20 20 20 20 20  ck its .        
12dfc 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c  ** transaction),
12dfd 20 74 68 65 6e 20 53 51 4c 69 74 65 20 6d 61 79   then SQLite may
12dfe 20 62 65 63 6f 6d 65 20 63 6f 6e 66 75 73 65 64   become confused
12dff 20 77 68 65 6e 20 64 6f 69 6e 67 20 74 68 65 20   when doing the 
12e00 0a 20 20 20 20 20 20 20 20 2a 2a 20 68 6f 74 2d  .        ** hot-
12e01 6a 6f 75 72 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b  journal rollback
12e02 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65 63 6f 76   following recov
12e03 65 72 79 2e 20 49 74 20 6d 61 79 20 72 6f 6c 6c  ery. It may roll
12e04 20 62 61 63 6b 20 61 6c 6c 0a 20 20 20 20 20 20   back all.      
12e05 20 20 2a 2a 20 6f 66 20 74 68 69 73 20 63 6f 6e    ** of this con
12e06 6e 65 63 74 69 6f 6e 73 20 64 61 74 61 2c 20 74  nections data, t
12e07 68 65 6e 20 70 72 6f 63 65 65 64 20 74 6f 20 72  hen proceed to r
12e08 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 74 68 65 20  olling back the 
12e09 6f 6c 64 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20  old,.        ** 
12e0a 6f 75 74 2d 6f 66 2d 64 61 74 65 20 64 61 74 61  out-of-date data
12e0b 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 74   that follows it
12e0c 2e 20 44 61 74 61 62 61 73 65 20 63 6f 72 72 75  . Database corru
12e0d 70 74 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20 2a  ption..        *
12e0e 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 6f 20  *.        ** To 
12e0f 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74 68 69 73  work around this
12e10 2c 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  , if the journal
12e11 20 66 69 6c 65 20 64 6f 65 73 20 61 70 70 65 61   file does appea
12e12 72 20 74 6f 20 63 6f 6e 74 61 69 6e 0a 20 20 20  r to contain.   
12e13 20 20 20 20 20 2a 2a 20 61 20 76 61 6c 69 64 20       ** a valid 
12e14 68 65 61 64 65 72 20 66 6f 6c 6c 6f 77 69 6e 67  header following
12e15 20 50 61 67 65 72 2e 6a 6f 75 72 6e 61 6c 4f 66   Pager.journalOf
12e16 66 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61 20  f, then write a 
12e17 30 78 30 30 0a 20 20 20 20 20 20 20 20 2a 2a 20  0x00.        ** 
12e18 62 79 74 65 20 74 6f 20 74 68 65 20 73 74 61 72  byte to the star
12e19 74 20 6f 66 20 69 74 20 74 6f 20 70 72 65 76 65  t of it to preve
12e1a 6e 74 20 69 74 20 66 72 6f 6d 20 62 65 69 6e 67  nt it from being
12e1b 20 72 65 63 6f 67 6e 69 7a 65 64 2e 0a 20 20 20   recognized..   
12e1c 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
12e1d 2a 2a 20 56 61 72 69 61 62 6c 65 20 69 4e 65 78  ** Variable iNex
12e1e 74 48 64 72 4f 66 66 73 65 74 20 69 73 20 73 65  tHdrOffset is se
12e1f 74 20 74 6f 20 74 68 65 20 6f 66 66 73 65 74 20  t to the offset 
12e20 61 74 20 77 68 69 63 68 20 74 68 69 73 0a 20 20  at which this.  
12e21 20 20 20 20 20 20 2a 2a 20 70 72 6f 62 6c 65 6d        ** problem
12e22 61 74 69 63 20 68 65 61 64 65 72 20 77 69 6c 6c  atic header will
12e23 20 6f 63 63 75 72 2c 20 69 66 20 69 74 20 65 78   occur, if it ex
12e24 69 73 74 73 2e 20 61 4d 61 67 69 63 20 69 73 20  ists. aMagic is 
12e25 75 73 65 64 20 0a 20 20 20 20 20 20 20 20 2a 2a  used .        **
12e26 20 61 73 20 61 20 74 65 6d 70 6f 72 61 72 79 20   as a temporary 
12e27 62 75 66 66 65 72 20 74 6f 20 69 6e 73 70 65 63  buffer to inspec
12e28 74 20 74 68 65 20 66 69 72 73 74 20 63 6f 75 70  t the first coup
12e29 6c 65 20 6f 66 20 62 79 74 65 73 20 6f 66 0a 20  le of bytes of. 
12e2a 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 70 6f         ** the po
12e2b 74 65 6e 74 69 61 6c 20 6a 6f 75 72 6e 61 6c 20  tential journal 
12e2c 68 65 61 64 65 72 2e 0a 20 20 20 20 20 20 20 20  header..        
12e2d 2a 2f 0a 20 20 20 20 20 20 20 20 69 36 34 20 69  */.        i64 i
12e2e 4e 65 78 74 48 64 72 4f 66 66 73 65 74 3b 0a 20  NextHdrOffset;. 
12e2f 20 20 20 20 20 20 20 75 38 20 61 4d 61 67 69 63         u8 aMagic
12e30 5b 38 5d 3b 0a 09 75 38 20 7a 48 65 61 64 65 72  [8];..u8 zHeader
12e31 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c  [sizeof(aJournal
12e32 4d 61 67 69 63 29 2b 34 5d 3b 0a 0a 09 6d 65 6d  Magic)+4];...mem
12e33 63 70 79 28 7a 48 65 61 64 65 72 2c 20 61 4a 6f  cpy(zHeader, aJo
12e34 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65  urnalMagic, size
12e35 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  of(aJournalMagic
12e36 29 29 3b 0a 09 70 75 74 33 32 62 69 74 73 28 26  ));..put32bits(&
12e37 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61  zHeader[sizeof(a
12e38 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20  JournalMagic)], 
12e39 70 50 61 67 65 72 2d 3e 6e 52 65 63 29 3b 0a 0a  pPager->nRec);..
12e3a 20 20 20 20 20 20 20 20 69 4e 65 78 74 48 64 72          iNextHdr
12e3b 4f 66 66 73 65 74 20 3d 20 6a 6f 75 72 6e 61 6c  Offset = journal
12e3c 48 64 72 4f 66 66 73 65 74 28 70 50 61 67 65 72  HdrOffset(pPager
12e3d 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
12e3e 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 70 50  sqlite3OsRead(pP
12e3f 61 67 65 72 2d 3e 6a 66 64 2c 20 61 4d 61 67 69  ager->jfd, aMagi
12e40 63 2c 20 38 2c 20 69 4e 65 78 74 48 64 72 4f 66  c, 8, iNextHdrOf
12e41 66 73 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69  fset);.        i
12e42 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
12e43 20 26 26 20 30 3d 3d 6d 65 6d 63 6d 70 28 61 4d   && 0==memcmp(aM
12e44 61 67 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61  agic, aJournalMa
12e45 67 69 63 2c 20 38 29 20 29 7b 0a 20 20 20 20 20  gic, 8) ){.     
12e46 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73       static cons
12e47 74 20 75 38 20 7a 65 72 6f 62 79 74 65 20 3d 20  t u8 zerobyte = 
12e48 30 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  0;.          rc 
12e49 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  = sqlite3OsWrite
12e4a 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26 7a  (pPager->jfd, &z
12e4b 65 72 6f 62 79 74 65 2c 20 31 2c 20 69 4e 65 78  erobyte, 1, iNex
12e4c 74 48 64 72 4f 66 66 73 65 74 29 3b 0a 20 20 20  tHdrOffset);.   
12e4d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
12e4e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
12e4f 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49   && rc!=SQLITE_I
12e50 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20  OERR_SHORT_READ 
12e51 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  ){.          ret
12e52 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20  urn rc;.        
12e53 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 72  }..        /* Wr
12e54 69 74 65 20 74 68 65 20 6e 52 65 63 20 76 61 6c  ite the nRec val
12e55 75 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72  ue into the jour
12e56 6e 61 6c 20 66 69 6c 65 20 68 65 61 64 65 72 2e  nal file header.
12e57 20 49 66 20 69 6e 0a 20 20 20 20 20 20 20 20 2a   If in.        *
12e58 2a 20 66 75 6c 6c 2d 73 79 6e 63 68 72 6f 6e 6f  * full-synchrono
12e59 75 73 20 6d 6f 64 65 2c 20 73 79 6e 63 20 74 68  us mode, sync th
12e5a 65 20 6a 6f 75 72 6e 61 6c 20 66 69 72 73 74 2e  e journal first.
12e5b 20 54 68 69 73 20 65 6e 73 75 72 65 73 20 74 68   This ensures th
12e5c 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c  at.        ** al
12e5d 6c 20 64 61 74 61 20 68 61 73 20 72 65 61 6c 6c  l data has reall
12e5e 79 20 68 69 74 20 74 68 65 20 64 69 73 6b 20 62  y hit the disk b
12e5f 65 66 6f 72 65 20 6e 52 65 63 20 69 73 20 75 70  efore nRec is up
12e60 64 61 74 65 64 20 74 6f 20 6d 61 72 6b 0a 20 20  dated to mark.  
12e61 20 20 20 20 20 20 2a 2a 20 69 74 20 61 73 20 61        ** it as a
12e62 20 63 61 6e 64 69 64 61 74 65 20 66 6f 72 20 72   candidate for r
12e63 6f 6c 6c 62 61 63 6b 2e 0a 20 20 20 20 20 20 20  ollback..       
12e64 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54   **.        ** T
12e65 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69  his is not requi
12e66 72 65 64 20 69 66 20 74 68 65 20 70 65 72 73 69  red if the persi
12e67 73 74 65 6e 74 20 6d 65 64 69 61 20 73 75 70 70  stent media supp
12e68 6f 72 74 73 20 74 68 65 0a 20 20 20 20 20 20 20  orts the.       
12e69 20 2a 2a 20 53 41 46 45 5f 41 50 50 45 4e 44 20   ** SAFE_APPEND 
12e6a 70 72 6f 70 65 72 74 79 2e 20 42 65 63 61 75 73  property. Becaus
12e6b 65 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 69  e in this case i
12e6c 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
12e6d 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 6f  e .        ** fo
12e6e 72 20 67 61 72 62 61 67 65 20 64 61 74 61 20 74  r garbage data t
12e6f 6f 20 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f  o be appended to
12e70 20 74 68 65 20 66 69 6c 65 2c 20 74 68 65 20 6e   the file, the n
12e71 52 65 63 20 66 69 65 6c 64 0a 20 20 20 20 20 20  Rec field.      
12e72 20 20 2a 2a 20 69 73 20 70 6f 70 75 6c 61 74 65    ** is populate
12e73 64 20 77 69 74 68 20 30 78 46 46 46 46 46 46 46  d with 0xFFFFFFF
12e74 46 20 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e  F when the journ
12e75 61 6c 20 68 65 61 64 65 72 20 69 73 20 77 72 69  al header is wri
12e76 74 74 65 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  tten.        ** 
12e77 61 6e 64 20 6e 65 76 65 72 20 6e 65 65 64 73 20  and never needs 
12e78 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20  to be updated.. 
12e79 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
12e7a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 66 75    if( pPager->fu
12e7b 6c 6c 53 79 6e 63 20 26 26 20 30 3d 3d 28 69 44  llSync && 0==(iD
12e7c 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  c&SQLITE_IOCAP_S
12e7d 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20  EQUENTIAL) ){.  
12e7e 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41          PAGERTRA
12e7f 43 45 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61  CE(("SYNC journa
12e80 6c 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45  l of %d\n", PAGE
12e81 52 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20  RID(pPager)));. 
12e82 20 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45           IOTRACE
12e83 28 28 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20  (("JSYNC %p\n", 
12e84 70 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 20  pPager)).       
12e85 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
12e86 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66  sSync(pPager->jf
12e87 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f  d, pPager->sync_
12e88 66 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20  flags);.        
12e89 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
12e8a 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
12e8b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
12e8c 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 48 44     IOTRACE(("JHD
12e8d 52 20 25 70 20 25 6c 6c 64 5c 6e 22 2c 20 70 50  R %p %lld\n", pP
12e8e 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f  ager, pPager->jo
12e8f 75 72 6e 61 6c 48 64 72 29 29 3b 0a 20 20 20 20  urnalHdr));.    
12e90 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
12e91 4f 73 57 72 69 74 65 28 0a 20 20 20 20 20 20 20  OsWrite(.       
12e92 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 66 64       pPager->jfd
12e93 2c 20 7a 48 65 61 64 65 72 2c 20 73 69 7a 65 6f  , zHeader, sizeo
12e94 66 28 7a 48 65 61 64 65 72 29 2c 20 70 50 61 67  f(zHeader), pPag
12e95 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 0a 09  er->journalHdr..
12e96 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
12e97 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
12e98 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
12e99 7d 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28  }.      if( 0==(
12e9a 69 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50  iDc&SQLITE_IOCAP
12e9b 5f 53 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a  _SEQUENTIAL) ){.
12e9c 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52 41          PAGERTRA
12e9d 43 45 28 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61  CE(("SYNC journa
12e9e 6c 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45  l of %d\n", PAGE
12e9f 52 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a 20  RID(pPager)));. 
12ea0 20 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28         IOTRACE((
12ea1 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50  "JSYNC %p\n", pP
12ea2 61 67 65 72 29 29 0a 20 20 20 20 20 20 20 20 72  ager)).        r
12ea3 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e  c = sqlite3OsSyn
12ea4 63 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70  c(pPager->jfd, p
12ea5 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67  Pager->sync_flag
12ea6 73 7c 20 0a 20 20 20 20 20 20 20 20 20 20 28 70  s| .          (p
12ea7 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67  Pager->sync_flag
12ea8 73 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46  s==SQLITE_SYNC_F
12ea9 55 4c 4c 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f  ULL?SQLITE_SYNC_
12eaa 44 41 54 41 4f 4e 4c 59 3a 30 29 0a 20 20 20 20  DATAONLY:0).    
12eab 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 69      );.        i
12eac 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
12ead 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
12eae 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
12eaf 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20   /* The journal 
12eb0 66 69 6c 65 20 77 61 73 20 6a 75 73 74 20 73 75  file was just su
12eb1 63 63 65 73 73 66 75 6c 6c 79 20 73 79 6e 63 65  ccessfully synce
12eb2 64 2e 20 53 65 74 20 50 61 67 65 72 2e 6e 65 65  d. Set Pager.nee
12eb3 64 53 79 6e 63 20 0a 20 20 20 20 2a 2a 20 74 6f  dSync .    ** to
12eb4 20 7a 65 72 6f 20 61 6e 64 20 63 6c 65 61 72 20   zero and clear 
12eb5 74 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53  the PGHDR_NEED_S
12eb6 59 4e 43 20 66 6c 61 67 20 6f 6e 20 61 6c 6c 20  YNC flag on all 
12eb7 70 61 67 65 73 73 2e 0a 20 20 20 20 2a 2f 0a 20  pagess..    */. 
12eb8 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53     pPager->needS
12eb9 79 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61  ync = 0;.    pPa
12eba 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72  ger->journalStar
12ebb 74 65 64 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c  ted = 1;.    sql
12ebc 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72 53  ite3PcacheClearS
12ebd 79 6e 63 46 6c 61 67 73 28 70 50 61 67 65 72 2d  yncFlags(pPager-
12ebe 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 7d 0a 0a  >pPCache);.  }..
12ebf 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
12ec0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  OK;.}../*.** The
12ec1 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
12ec2 20 66 69 72 73 74 20 69 6e 20 61 20 6c 69 6e 6b   first in a link
12ec3 65 64 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79  ed list of dirty
12ec4 20 70 61 67 65 73 20 63 6f 6e 6e 65 63 74 65 64   pages connected
12ec5 0a 2a 2a 20 62 79 20 74 68 65 20 50 67 48 64 72  .** by the PgHdr
12ec6 2e 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 2e  .pDirty pointer.
12ec7 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77   This function w
12ec8 72 69 74 65 73 20 65 61 63 68 20 6f 6e 65 20 6f  rites each one o
12ec9 66 20 74 68 65 0a 2a 2a 20 69 6e 2d 6d 65 6d 6f  f the.** in-memo
12eca 72 79 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  ry pages in the 
12ecb 6c 69 73 74 20 74 6f 20 74 68 65 20 64 61 74 61  list to the data
12ecc 62 61 73 65 20 66 69 6c 65 2e 20 54 68 65 20 61  base file. The a
12ecd 72 67 75 6d 65 6e 74 20 6d 61 79 0a 2a 2a 20 62  rgument may.** b
12ece 65 20 4e 55 4c 4c 2c 20 72 65 70 72 65 73 65 6e  e NULL, represen
12ecf 74 69 6e 67 20 61 6e 20 65 6d 70 74 79 20 6c 69  ting an empty li
12ed0 73 74 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  st. In this case
12ed1 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
12ed2 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  s.** a no-op..**
12ed3 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 6d 75  .** The pager mu
12ed4 73 74 20 68 6f 6c 64 20 61 74 20 6c 65 61 73 74  st hold at least
12ed5 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
12ed6 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   when this funct
12ed7 69 6f 6e 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64  ion.** is called
12ed8 2e 20 42 65 66 6f 72 65 20 77 72 69 74 69 6e 67  . Before writing
12ed9 20 61 6e 79 74 68 69 6e 67 20 74 6f 20 74 68 65   anything to the
12eda 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
12edb 74 68 69 73 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20  this lock.** is 
12edc 75 70 67 72 61 64 65 64 20 74 6f 20 61 6e 20 45  upgraded to an E
12edd 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49  XCLUSIVE lock. I
12ede 66 20 74 68 65 20 6c 6f 63 6b 20 63 61 6e 6e 6f  f the lock canno
12edf 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 0a 2a  t be obtained,.*
12ee0 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73  * SQLITE_BUSY is
12ee1 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f   returned and no
12ee2 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e   data is written
12ee3 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
12ee4 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a 20 49 66   file..** .** If
12ee5 20 74 68 65 20 70 61 67 65 72 20 69 73 20 61 20   the pager is a 
12ee6 74 65 6d 70 2d 66 69 6c 65 20 70 61 67 65 72 20  temp-file pager 
12ee7 61 6e 64 20 74 68 65 20 61 63 74 75 61 6c 20 66  and the actual f
12ee8 69 6c 65 2d 73 79 73 74 65 6d 20 66 69 6c 65 0a  ile-system file.
12ee9 2a 2a 20 69 73 20 6e 6f 74 20 79 65 74 20 6f 70  ** is not yet op
12eea 65 6e 2c 20 69 74 20 69 73 20 63 72 65 61 74 65  en, it is create
12eeb 64 20 61 6e 64 20 6f 70 65 6e 65 64 20 62 65 66  d and opened bef
12eec 6f 72 65 20 61 6e 79 20 64 61 74 61 20 69 73 20  ore any data is 
12eed 0a 2a 2a 20 77 72 69 74 74 65 6e 20 6f 75 74 2e  .** written out.
12eee 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 65 20  .**.** Once the 
12eef 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 75 70  lock has been up
12ef0 67 72 61 64 65 64 20 61 6e 64 2c 20 69 66 20 6e  graded and, if n
12ef1 65 63 65 73 73 61 72 79 2c 20 74 68 65 20 66 69  ecessary, the fi
12ef2 6c 65 20 6f 70 65 6e 65 64 2c 0a 2a 2a 20 74 68  le opened,.** th
12ef3 65 20 70 61 67 65 73 20 61 72 65 20 77 72 69 74  e pages are writ
12ef4 74 65 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 64  ten out to the d
12ef5 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20  atabase file in 
12ef6 6c 69 73 74 20 6f 72 64 65 72 2e 20 57 72 69 74  list order. Writ
12ef7 69 6e 67 0a 2a 2a 20 61 20 70 61 67 65 20 69 73  ing.** a page is
12ef8 20 73 6b 69 70 70 65 64 20 69 66 20 69 74 20 6d   skipped if it m
12ef9 65 65 74 73 20 65 69 74 68 65 72 20 6f 66 20 74  eets either of t
12efa 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 72 69  he following cri
12efb 74 65 72 69 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a  teria:.**.**   *
12efc 20 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   The page number
12efd 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
12efe 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20 6f   Pager.dbSize, o
12eff 72 0a 2a 2a 20 20 20 2a 20 54 68 65 20 50 47 48  r.**   * The PGH
12f00 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20 66 6c  DR_DONT_WRITE fl
12f01 61 67 20 69 73 20 73 65 74 20 6f 6e 20 74 68 65  ag is set on the
12f02 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   page..**.** If 
12f03 77 72 69 74 69 6e 67 20 6f 75 74 20 61 20 70 61  writing out a pa
12f04 67 65 20 63 61 75 73 65 73 20 74 68 65 20 64 61  ge causes the da
12f05 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20 67  tabase file to g
12f06 72 6f 77 2c 20 50 61 67 65 72 2e 64 62 46 69 6c  row, Pager.dbFil
12f07 65 53 69 7a 65 0a 2a 2a 20 69 73 20 75 70 64 61  eSize.** is upda
12f08 74 65 64 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e  ted accordingly.
12f09 20 49 66 20 70 61 67 65 20 31 20 69 73 20 77 72   If page 1 is wr
12f0a 69 74 74 65 6e 20 6f 75 74 2c 20 74 68 65 6e 20  itten out, then 
12f0b 74 68 65 20 76 61 6c 75 65 20 63 61 63 68 65 64  the value cached
12f0c 0a 2a 2a 20 69 6e 20 50 61 67 65 72 2e 64 62 46  .** in Pager.dbF
12f0d 69 6c 65 56 65 72 73 5b 5d 20 69 73 20 75 70 64  ileVers[] is upd
12f0e 61 74 65 64 20 74 6f 20 6d 61 74 63 68 20 74 68  ated to match th
12f0f 65 20 6e 65 77 20 76 61 6c 75 65 20 73 74 6f 72  e new value stor
12f10 65 64 20 69 6e 0a 2a 2a 20 74 68 65 20 64 61 74  ed in.** the dat
12f11 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  abase file..**.*
12f12 2a 20 49 66 20 65 76 65 72 79 74 68 69 6e 67 20  * If everything 
12f13 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 53  is successful, S
12f14 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
12f15 72 6e 65 64 2e 20 49 66 20 61 6e 20 49 4f 20 65  rned. If an IO e
12f16 72 72 6f 72 20 0a 2a 2a 20 6f 63 63 75 72 73 2c  rror .** occurs,
12f17 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64   an IO error cod
12f18 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f  e is returned. O
12f19 72 2c 20 69 66 20 74 68 65 20 45 58 43 4c 55 53  r, if the EXCLUS
12f1a 49 56 45 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 0a  IVE lock cannot.
12f1b 2a 2a 20 62 65 20 6f 62 74 61 69 6e 65 64 2c 20  ** be obtained, 
12f1c 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73 20 72  SQLITE_BUSY is r
12f1d 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74  eturned..*/.stat
12f1e 69 63 20 69 6e 74 20 70 61 67 65 72 5f 77 72 69  ic int pager_wri
12f1f 74 65 5f 70 61 67 65 6c 69 73 74 28 50 67 48 64  te_pagelist(PgHd
12f20 72 20 2a 70 4c 69 73 74 29 7b 0a 20 20 50 61 67  r *pList){.  Pag
12f21 65 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20  er *pPager;     
12f22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12f23 20 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63    /* Pager objec
12f24 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  t */.  int rc;  
12f25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12f26 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
12f27 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20  eturn code */.. 
12f28 20 69 66 28 20 4e 45 56 45 52 28 70 4c 69 73 74   if( NEVER(pList
12f29 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 20 53 51  ==0) ) return SQ
12f2a 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 67 65  LITE_OK;.  pPage
12f2b 72 20 3d 20 70 4c 69 73 74 2d 3e 70 50 61 67 65  r = pList->pPage
12f2c 72 3b 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73  r;..  /* At this
12f2d 20 70 6f 69 6e 74 20 74 68 65 72 65 20 6d 61 79   point there may
12f2e 20 62 65 20 65 69 74 68 65 72 20 61 20 52 45 53   be either a RES
12f2f 45 52 56 45 44 20 6f 72 20 45 58 43 4c 55 53 49  ERVED or EXCLUSI
12f30 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20  VE lock on the. 
12f31 20 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c   ** database fil
12f32 65 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61  e. If there is a
12f33 6c 72 65 61 64 79 20 61 6e 20 45 58 43 4c 55 53  lready an EXCLUS
12f34 49 56 45 20 6c 6f 63 6b 2c 20 74 68 65 20 66 6f  IVE lock, the fo
12f35 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 63 61 6c  llowing.  ** cal
12f36 6c 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20  l is a no-op..  
12f37 2a 2a 0a 20 20 2a 2a 20 4d 6f 76 69 6e 67 20 74  **.  ** Moving t
12f38 68 65 20 6c 6f 63 6b 20 66 72 6f 6d 20 52 45 53  he lock from RES
12f39 45 52 56 45 44 20 74 6f 20 45 58 43 4c 55 53 49  ERVED to EXCLUSI
12f3a 56 45 20 61 63 74 75 61 6c 6c 79 20 69 6e 76 6f  VE actually invo
12f3b 6c 76 65 73 20 67 6f 69 6e 67 0a 20 20 2a 2a 20  lves going.  ** 
12f3c 74 68 72 6f 75 67 68 20 61 6e 20 69 6e 74 65 72  through an inter
12f3d 6d 65 64 69 61 74 65 20 73 74 61 74 65 20 50 45  mediate state PE
12f3e 4e 44 49 4e 47 2e 20 20 20 41 20 50 45 4e 44 49  NDING.   A PENDI
12f3f 4e 47 20 6c 6f 63 6b 20 70 72 65 76 65 6e 74 73  NG lock prevents
12f40 20 6e 65 77 0a 20 20 2a 2a 20 72 65 61 64 65 72   new.  ** reader
12f41 73 20 66 72 6f 6d 20 61 74 74 61 63 68 69 6e 67  s from attaching
12f42 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
12f43 20 62 75 74 20 69 73 20 75 6e 73 75 66 66 69 63   but is unsuffic
12f44 69 65 6e 74 20 66 6f 72 20 75 73 20 74 6f 0a 20  ient for us to. 
12f45 20 2a 2a 20 77 72 69 74 65 2e 20 20 54 68 65 20   ** write.  The 
12f46 69 64 65 61 20 6f 66 20 61 20 50 45 4e 44 49 4e  idea of a PENDIN
12f47 47 20 6c 6f 63 6b 20 69 73 20 74 6f 20 70 72 65  G lock is to pre
12f48 76 65 6e 74 20 6e 65 77 20 72 65 61 64 65 72 73  vent new readers
12f49 20 66 72 6f 6d 0a 20 20 2a 2a 20 63 6f 6d 69 6e   from.  ** comin
12f4a 67 20 69 6e 20 77 68 69 6c 65 20 77 65 20 77 61  g in while we wa
12f4b 69 74 20 66 6f 72 20 65 78 69 73 74 69 6e 67 20  it for existing 
12f4c 72 65 61 64 65 72 73 20 74 6f 20 63 6c 65 61 72  readers to clear
12f4d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 69 6c  ..  **.  ** Whil
12f4e 65 20 74 68 65 20 70 61 67 65 72 20 69 73 20 69  e the pager is i
12f4f 6e 20 74 68 65 20 52 45 53 45 52 56 45 44 20 73  n the RESERVED s
12f50 74 61 74 65 2c 20 74 68 65 20 6f 72 69 67 69 6e  tate, the origin
12f51 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  al database file
12f52 0a 20 20 2a 2a 20 69 73 20 75 6e 63 68 61 6e 67  .  ** is unchang
12f53 65 64 20 61 6e 64 20 77 65 20 63 61 6e 20 72 6f  ed and we can ro
12f54 6c 6c 62 61 63 6b 20 77 69 74 68 6f 75 74 20 68  llback without h
12f55 61 76 69 6e 67 20 74 6f 20 70 6c 61 79 62 61 63  aving to playbac
12f56 6b 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 72 6e  k the.  ** journ
12f57 61 6c 20 69 6e 74 6f 20 74 68 65 20 6f 72 69 67  al into the orig
12f58 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 69  inal database fi
12f59 6c 65 2e 20 20 4f 6e 63 65 20 77 65 20 74 72 61  le.  Once we tra
12f5a 6e 73 69 74 69 6f 6e 20 74 6f 0a 20 20 2a 2a 20  nsition to.  ** 
12f5b 45 58 43 4c 55 53 49 56 45 2c 20 69 74 20 6d 65  EXCLUSIVE, it me
12f5c 61 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65  ans the database
12f5d 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 63   file has been c
12f5e 68 61 6e 67 65 64 20 61 6e 64 20 61 6e 79 20 72  hanged and any r
12f5f 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 77 69 6c  ollback.  ** wil
12f60 6c 20 72 65 71 75 69 72 65 20 61 20 6a 6f 75 72  l require a jour
12f61 6e 61 6c 20 70 6c 61 79 62 61 63 6b 2e 0a 20 20  nal playback..  
12f62 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  */.  assert( pPa
12f63 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
12f64 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20  R_RESERVED );.  
12f65 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74 5f  rc = pager_wait_
12f66 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c 20  on_lock(pPager, 
12f67 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 29 3b  EXCLUSIVE_LOCK);
12f68 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69  ..  /* If the fi
12f69 6c 65 20 69 73 20 61 20 74 65 6d 70 2d 66 69 6c  le is a temp-fil
12f6a 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65  e has not yet be
12f6b 65 6e 20 6f 70 65 6e 65 64 2c 20 6f 70 65 6e 20  en opened, open 
12f6c 69 74 20 6e 6f 77 2e 20 49 74 0a 20 20 2a 2a 20  it now. It.  ** 
12f6d 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
12f6e 66 6f 72 20 72 63 20 74 6f 20 62 65 20 6f 74 68  for rc to be oth
12f6f 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f  er than SQLITE_O
12f70 4b 20 69 66 20 74 68 69 73 20 62 72 61 6e 63 68  K if this branch
12f71 0a 20 20 2a 2a 20 69 73 20 74 61 6b 65 6e 2c 20  .  ** is taken, 
12f72 61 73 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e  as pager_wait_on
12f73 5f 6c 6f 63 6b 28 29 20 69 73 20 61 20 6e 6f 2d  _lock() is a no-
12f74 6f 70 20 66 6f 72 20 74 65 6d 70 2d 66 69 6c 65  op for temp-file
12f75 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 69  s..  */.  if( !i
12f76 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 66 64  sOpen(pPager->fd
12f77 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ) ){.    assert(
12f78 20 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c   pPager->tempFil
12f79 65 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f  e && rc==SQLITE_
12f7a 4f 4b 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 70  OK );.    rc = p
12f7b 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61  agerOpentemp(pPa
12f7c 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 66 64 2c  ger, pPager->fd,
12f7d 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c 61 67   pPager->vfsFlag
12f7e 73 29 3b 0a 20 20 7d 0a 0a 20 20 77 68 69 6c 65  s);.  }..  while
12f7f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
12f80 26 26 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20  && pList ){.    
12f81 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70 4c 69 73  Pgno pgno = pLis
12f82 74 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 20 20 2f 2a  t->pgno;..    /*
12f83 20 49 66 20 74 68 65 72 65 20 61 72 65 20 64 69   If there are di
12f84 72 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 65  rty pages in the
12f85 20 70 61 67 65 20 63 61 63 68 65 20 77 69 74 68   page cache with
12f86 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 67 72   page numbers gr
12f87 65 61 74 65 72 0a 20 20 20 20 2a 2a 20 74 68 61  eater.    ** tha
12f88 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20  n Pager.dbSize, 
12f89 74 68 69 73 20 6d 65 61 6e 73 20 73 71 6c 69 74  this means sqlit
12f8a 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 49  e3PagerTruncateI
12f8b 6d 61 67 65 28 29 20 77 61 73 20 63 61 6c 6c 65  mage() was calle
12f8c 64 20 74 6f 0a 20 20 20 20 2a 2a 20 6d 61 6b 65  d to.    ** make
12f8d 20 74 68 65 20 66 69 6c 65 20 73 6d 61 6c 6c 65   the file smalle
12f8e 72 20 28 70 72 65 73 75 6d 61 62 6c 79 20 62 79  r (presumably by
12f8f 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 63 6f 64   auto-vacuum cod
12f90 65 29 2e 20 44 6f 20 6e 6f 74 20 77 72 69 74 65  e). Do not write
12f91 0a 20 20 20 20 2a 2a 20 61 6e 79 20 73 75 63 68  .    ** any such
12f92 20 70 61 67 65 73 20 74 6f 20 74 68 65 20 66 69   pages to the fi
12f93 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  le..    **.    *
12f94 2a 20 41 6c 73 6f 2c 20 64 6f 20 6e 6f 74 20 77  * Also, do not w
12f95 72 69 74 65 20 6f 75 74 20 61 6e 79 20 70 61 67  rite out any pag
12f96 65 20 74 68 61 74 20 68 61 73 20 74 68 65 20 50  e that has the P
12f97 47 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 20  GHDR_DONT_WRITE 
12f98 66 6c 61 67 0a 20 20 20 20 2a 2a 20 73 65 74 20  flag.    ** set 
12f99 28 73 65 74 20 62 79 20 73 71 6c 69 74 65 33 50  (set by sqlite3P
12f9a 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28 29 29  agerDontWrite())
12f9b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
12f9c 20 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64   pgno<=pPager->d
12f9d 62 53 69 7a 65 20 26 26 20 30 3d 3d 28 70 4c 69  bSize && 0==(pLi
12f9e 73 74 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  st->flags&PGHDR_
12f9f 44 4f 4e 54 5f 57 52 49 54 45 29 20 29 7b 0a 20  DONT_WRITE) ){. 
12fa0 20 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 20       i64 offset 
12fa1 3d 20 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29  = (pgno-1)*(i64)
12fa2 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
12fa3 3b 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f  ;   /* Offset to
12fa4 20 77 72 69 74 65 20 2a 2f 0a 20 20 20 20 20 20   write */.      
12fa5 63 68 61 72 20 2a 70 44 61 74 61 3b 20 20 20 20  char *pData;    
12fa6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12fa7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
12fa8 2a 20 44 61 74 61 20 74 6f 20 77 72 69 74 65 20  * Data to write 
12fa9 2a 2f 20 20 20 20 0a 0a 20 20 20 20 20 20 2f 2a  */    ..      /*
12faa 20 45 6e 63 6f 64 65 20 74 68 65 20 64 61 74 61   Encode the data
12fab 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 43 4f  base */.      CO
12fac 44 45 43 32 28 70 50 61 67 65 72 2c 20 70 4c 69  DEC2(pPager, pLi
12fad 73 74 2d 3e 70 44 61 74 61 2c 20 70 67 6e 6f 2c  st->pData, pgno,
12fae 20 36 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54   6, return SQLIT
12faf 45 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61 29 3b  E_NOMEM, pData);
12fb0 0a 0a 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65  ..      /* Write
12fb1 20 6f 75 74 20 74 68 65 20 70 61 67 65 20 64 61   out the page da
12fb2 74 61 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20  ta. */.      rc 
12fb3 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  = sqlite3OsWrite
12fb4 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 70 44 61  (pPager->fd, pDa
12fb5 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65  ta, pPager->page
12fb6 53 69 7a 65 2c 20 6f 66 66 73 65 74 29 3b 0a 0a  Size, offset);..
12fb7 20 20 20 20 20 20 2f 2a 20 49 66 20 70 61 67 65        /* If page
12fb8 20 31 20 77 61 73 20 6a 75 73 74 20 77 72 69 74   1 was just writ
12fb9 74 65 6e 2c 20 75 70 64 61 74 65 20 50 61 67 65  ten, update Page
12fba 72 2e 64 62 46 69 6c 65 56 65 72 73 20 74 6f 20  r.dbFileVers to 
12fbb 6d 61 74 63 68 0a 20 20 20 20 20 20 2a 2a 20 74  match.      ** t
12fbc 68 65 20 76 61 6c 75 65 20 6e 6f 77 20 73 74 6f  he value now sto
12fbd 72 65 64 20 69 6e 20 74 68 65 20 64 61 74 61 62  red in the datab
12fbe 61 73 65 20 66 69 6c 65 2e 20 49 66 20 77 72 69  ase file. If wri
12fbf 74 69 6e 67 20 74 68 69 73 20 0a 20 20 20 20 20  ting this .     
12fc0 20 2a 2a 20 70 61 67 65 20 63 61 75 73 65 64 20   ** page caused 
12fc1 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
12fc2 65 20 74 6f 20 67 72 6f 77 2c 20 75 70 64 61 74  e to grow, updat
12fc3 65 20 64 62 46 69 6c 65 53 69 7a 65 2e 20 0a 20  e dbFileSize. . 
12fc4 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
12fc5 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20  ( pgno==1 ){.   
12fc6 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61       memcpy(&pPa
12fc7 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c  ger->dbFileVers,
12fc8 20 26 70 44 61 74 61 5b 32 34 5d 2c 20 73 69 7a   &pData[24], siz
12fc9 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62 46 69  eof(pPager->dbFi
12fca 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 20 20 20  leVers));.      
12fcb 7d 0a 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f  }.      if( pgno
12fcc 3e 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53  >pPager->dbFileS
12fcd 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ize ){.        p
12fce 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 53 69 7a  Pager->dbFileSiz
12fcf 65 20 3d 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20  e = pgno;.      
12fd0 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 55 70 64 61  }..      /* Upda
12fd1 74 65 20 61 6e 79 20 62 61 63 6b 75 70 20 6f 62  te any backup ob
12fd2 6a 65 63 74 73 20 63 6f 70 79 69 6e 67 20 74 68  jects copying th
12fd3 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
12fd4 69 73 20 70 61 67 65 72 2e 20 2a 2f 0a 20 20 20  is pager. */.   
12fd5 20 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70     sqlite3Backup
12fd6 55 70 64 61 74 65 28 70 50 61 67 65 72 2d 3e 70  Update(pPager->p
12fd7 42 61 63 6b 75 70 2c 20 70 67 6e 6f 2c 20 28 75  Backup, pgno, (u
12fd8 38 2a 29 70 4c 69 73 74 2d 3e 70 44 61 74 61 29  8*)pList->pData)
12fd9 3b 0a 0a 20 20 20 20 20 20 50 41 47 45 52 54 52  ;..      PAGERTR
12fda 41 43 45 28 28 22 53 54 4f 52 45 20 25 64 20 70  ACE(("STORE %d p
12fdb 61 67 65 20 25 64 20 68 61 73 68 28 25 30 38 78  age %d hash(%08x
12fdc 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  )\n",.          
12fdd 20 20 20 20 20 20 20 20 20 50 41 47 45 52 49 44           PAGERID
12fde 28 70 50 61 67 65 72 29 2c 20 70 67 6e 6f 2c 20  (pPager), pgno, 
12fdf 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70  pager_pagehash(p
12fe0 4c 69 73 74 29 29 29 3b 0a 20 20 20 20 20 20 49  List)));.      I
12fe1 4f 54 52 41 43 45 28 28 22 50 47 4f 55 54 20 25  OTRACE(("PGOUT %
12fe2 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c  p %d\n", pPager,
12fe3 20 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 50   pgno));.      P
12fe4 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74 65  AGER_INCR(sqlite
12fe5 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62 5f  3_pager_writedb_
12fe6 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 50 41  count);.      PA
12fe7 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d  GER_INCR(pPager-
12fe8 3e 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 7d 65  >nWrite);.    }e
12fe9 6c 73 65 7b 0a 20 20 20 20 20 20 50 41 47 45 52  lse{.      PAGER
12fea 54 52 41 43 45 28 28 22 4e 4f 53 54 4f 52 45 20  TRACE(("NOSTORE 
12feb 25 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50  %d page %d\n", P
12fec 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20  AGERID(pPager), 
12fed 70 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 23 69  pgno));.    }.#i
12fee 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43  fdef SQLITE_CHEC
12fef 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 4c 69 73  K_PAGES.    pLis
12ff0 74 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61  t->pageHash = pa
12ff1 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 4c 69  ger_pagehash(pLi
12ff2 73 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20  st);.#endif.    
12ff3 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 2d 3e 70  pList = pList->p
12ff4 44 69 72 74 79 3b 0a 20 20 7d 0a 0a 20 20 72 65  Dirty;.  }..  re
12ff5 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
12ff6 2a 20 41 70 70 65 6e 64 20 61 20 72 65 63 6f 72  * Append a recor
12ff7 64 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74  d of the current
12ff8 20 73 74 61 74 65 20 6f 66 20 70 61 67 65 20 70   state of page p
12ff9 50 67 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f  Pg to the sub-jo
12ffa 75 72 6e 61 6c 2e 20 0a 2a 2a 20 49 74 20 69 73  urnal. .** It is
12ffb 20 74 68 65 20 63 61 6c 6c 65 72 73 20 72 65 73   the callers res
12ffc 70 6f 6e 73 69 62 69 6c 69 74 79 20 74 6f 20 75  ponsibility to u
12ffd 73 65 20 73 75 62 6a 52 65 71 75 69 72 65 73 50  se subjRequiresP
12ffe 61 67 65 28 29 20 74 6f 20 63 68 65 63 6b 20 0a  age() to check .
12fff 2a 2a 20 74 68 61 74 20 69 74 20 69 73 20 72 65  ** that it is re
13000 61 6c 6c 79 20 72 65 71 75 69 72 65 64 20 62 65  ally required be
13001 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 74 68 69  fore calling thi
13002 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  s function..**.*
13003 2a 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  * If successful,
13004 20 73 65 74 20 74 68 65 20 62 69 74 20 63 6f 72   set the bit cor
13005 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 70 50  responding to pP
13006 67 2d 3e 70 67 6e 6f 20 69 6e 20 74 68 65 20 62  g->pgno in the b
13007 69 74 76 65 63 73 0a 2a 2a 20 66 6f 72 20 61 6c  itvecs.** for al
13008 6c 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74  l open savepoint
13009 73 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  s before returni
1300a 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ng..**.** This f
1300b 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
1300c 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65  SQLITE_OK if eve
1300d 72 79 74 68 69 6e 67 20 69 73 20 73 75 63 63 65  rything is succe
1300e 73 73 66 75 6c 2c 20 61 6e 20 49 4f 0a 2a 2a 20  ssful, an IO.** 
1300f 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 74 68  error code if th
13010 65 20 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69  e attempt to wri
13011 74 65 20 74 6f 20 74 68 65 20 73 75 62 2d 6a 6f  te to the sub-jo
13012 75 72 6e 61 6c 20 66 61 69 6c 73 2c 20 6f 72 20  urnal fails, or 
13013 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  .** SQLITE_NOMEM
13014 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69   if a malloc fai
13015 6c 73 20 77 68 69 6c 65 20 73 65 74 74 69 6e 67  ls while setting
13016 20 61 20 62 69 74 20 69 6e 20 61 20 73 61 76 65   a bit in a save
13017 70 6f 69 6e 74 0a 2a 2a 20 62 69 74 76 65 63 2e  point.** bitvec.
13018 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
13019 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 50 67  ubjournalPage(Pg
1301a 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 69 6e 74  Hdr *pPg){.  int
1301b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1301c 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
1301d 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a   = pPg->pPager;.
1301e 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70 50 61    if( isOpen(pPa
1301f 67 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20  ger->sjfd) ){.  
13020 20 20 76 6f 69 64 20 2a 70 44 61 74 61 20 3d 20    void *pData = 
13021 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20 20 20  pPg->pData;.    
13022 69 36 34 20 6f 66 66 73 65 74 20 3d 20 70 50 61  i64 offset = pPa
13023 67 65 72 2d 3e 6e 53 75 62 52 65 63 2a 28 34 2b  ger->nSubRec*(4+
13024 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
13025 29 3b 0a 20 20 20 20 63 68 61 72 20 2a 70 44 61  );.    char *pDa
13026 74 61 32 3b 0a 0a 20 20 20 20 43 4f 44 45 43 32  ta2;..    CODEC2
13027 28 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20  (pPager, pData, 
13028 70 50 67 2d 3e 70 67 6e 6f 2c 20 37 2c 20 72 65  pPg->pgno, 7, re
13029 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
1302a 4d 2c 20 70 44 61 74 61 32 29 3b 0a 20 20 20 20  M, pData2);.    
1302b 50 41 47 45 52 54 52 41 43 45 28 28 22 53 54 4d  PAGERTRACE(("STM
1302c 54 2d 4a 4f 55 52 4e 41 4c 20 25 64 20 70 61 67  T-JOURNAL %d pag
1302d 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44  e %d\n", PAGERID
1302e 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d 3e 70  (pPager), pPg->p
1302f 67 6e 6f 29 29 3b 0a 20 20 0a 20 20 20 20 61 73  gno));.  .    as
13030 73 65 72 74 28 20 70 61 67 65 49 6e 4a 6f 75 72  sert( pageInJour
13031 6e 61 6c 28 70 50 67 29 20 7c 7c 20 70 50 67 2d  nal(pPg) || pPg-
13032 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e 64 62  >pgno>pPager->db
13033 4f 72 69 67 53 69 7a 65 20 29 3b 0a 20 20 20 20  OrigSize );.    
13034 72 63 20 3d 20 77 72 69 74 65 33 32 62 69 74 73  rc = write32bits
13035 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c 20 6f  (pPager->sjfd, o
13036 66 66 73 65 74 2c 20 70 50 67 2d 3e 70 67 6e 6f  ffset, pPg->pgno
13037 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
13038 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
13039 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
1303a 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 73 6a  Write(pPager->sj
1303b 66 64 2c 20 70 44 61 74 61 32 2c 20 70 50 61 67  fd, pData2, pPag
1303c 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f 66  er->pageSize, of
1303d 66 73 65 74 2b 34 29 3b 0a 20 20 20 20 7d 0a 20  fset+4);.    }. 
1303e 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c   }.  if( rc==SQL
1303f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50  ITE_OK ){.    pP
13040 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 2b 2b 3b  ager->nSubRec++;
13041 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
13042 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3e  ger->nSavepoint>
13043 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 61 64  0 );.    rc = ad
13044 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74 76  dToSavepointBitv
13045 65 63 73 28 70 50 61 67 65 72 2c 20 70 50 67 2d  ecs(pPager, pPg-
13046 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65  >pgno);.  }.  re
13047 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
13048 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
13049 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
1304a 65 20 70 63 61 63 68 65 20 6c 61 79 65 72 20 77  e pcache layer w
1304b 68 65 6e 20 69 74 20 68 61 73 20 72 65 61 63 68  hen it has reach
1304c 65 64 20 73 6f 6d 65 0a 2a 2a 20 73 6f 66 74 20  ed some.** soft 
1304d 6d 65 6d 6f 72 79 20 6c 69 6d 69 74 2e 20 54 68  memory limit. Th
1304e 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
1304f 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
13050 20 61 20 50 61 67 65 72 20 6f 62 6a 65 63 74 0a   a Pager object.
13051 2a 2a 20 28 63 61 73 74 20 61 73 20 61 20 76 6f  ** (cast as a vo
13052 69 64 2a 29 2e 20 54 68 65 20 70 61 67 65 72 20  id*). The pager 
13053 69 73 20 61 6c 77 61 79 73 20 27 70 75 72 67 65  is always 'purge
13054 61 62 6c 65 27 20 28 6e 6f 74 20 61 6e 20 69 6e  able' (not an in
13055 2d 6d 65 6d 6f 72 79 0a 2a 2a 20 64 61 74 61 62  -memory.** datab
13056 61 73 65 29 2e 20 54 68 65 20 73 65 63 6f 6e 64  ase). The second
13057 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20 72   argument is a r
13058 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 70 61  eference to a pa
13059 67 65 20 74 68 61 74 20 69 73 20 0a 2a 2a 20 63  ge that is .** c
1305a 75 72 72 65 6e 74 6c 79 20 64 69 72 74 79 20 62  urrently dirty b
1305b 75 74 20 68 61 73 20 6e 6f 20 6f 75 74 73 74 61  ut has no outsta
1305c 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73  nding references
1305d 2e 20 54 68 65 20 70 61 67 65 0a 2a 2a 20 69 73  . The page.** is
1305e 20 61 6c 77 61 79 73 20 61 73 73 6f 63 69 61 74   always associat
1305f 65 64 20 77 69 74 68 20 74 68 65 20 50 61 67 65  ed with the Page
13060 72 20 6f 62 6a 65 63 74 20 70 61 73 73 65 64 20  r object passed 
13061 61 73 20 74 68 65 20 66 69 72 73 74 20 0a 2a 2a  as the first .**
13062 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   argument..**.**
13063 20 54 68 65 20 6a 6f 62 20 6f 66 20 74 68 69 73   The job of this
13064 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 6f 20   function is to 
13065 6d 61 6b 65 20 70 50 67 20 63 6c 65 61 6e 20 62  make pPg clean b
13066 79 20 77 72 69 74 69 6e 67 20 69 74 73 20 63 6f  y writing its co
13067 6e 74 65 6e 74 73 0a 2a 2a 20 6f 75 74 20 74 6f  ntents.** out to
13068 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
13069 6c 65 2c 20 69 66 20 70 6f 73 73 69 62 6c 65 2e  le, if possible.
1306a 20 54 68 69 73 20 6d 61 79 20 69 6e 76 6f 6c 76   This may involv
1306b 65 20 73 79 6e 63 69 6e 67 20 74 68 65 0a 2a 2a  e syncing the.**
1306c 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 0a   journal file. .
1306d 2a 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73  **.** If success
1306e 66 75 6c 2c 20 73 71 6c 69 74 65 33 50 63 61 63  ful, sqlite3Pcac
1306f 68 65 4d 61 6b 65 43 6c 65 61 6e 28 29 20 69 73  heMakeClean() is
13070 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 70   called on the p
13071 61 67 65 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54  age and.** SQLIT
13072 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 49  E_OK returned. I
13073 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f 63  f an IO error oc
13074 63 75 72 73 20 77 68 69 6c 65 20 74 72 79 69 6e  curs while tryin
13075 67 20 74 6f 20 6d 61 6b 65 20 74 68 65 0a 2a 2a  g to make the.**
13076 20 70 61 67 65 20 63 6c 65 61 6e 2c 20 74 68 65   page clean, the
13077 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20 69   IO error code i
13078 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 74  s returned. If t
13079 68 65 20 70 61 67 65 20 63 61 6e 6e 6f 74 20 62  he page cannot b
1307a 65 0a 2a 2a 20 6d 61 64 65 20 63 6c 65 61 6e 20  e.** made clean 
1307b 66 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 72  for some other r
1307c 65 61 73 6f 6e 2c 20 62 75 74 20 6e 6f 20 65 72  eason, but no er
1307d 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65 6e  ror occurs, then
1307e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 69 73   SQLITE_OK.** is
1307f 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c   returned by sql
13080 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c  ite3PcacheMakeCl
13081 65 61 6e 28 29 20 69 73 20 6e 6f 74 20 63 61 6c  ean() is not cal
13082 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  led..*/.static i
13083 6e 74 20 70 61 67 65 72 53 74 72 65 73 73 28 76  nt pagerStress(v
13084 6f 69 64 20 2a 70 2c 20 50 67 48 64 72 20 2a 70  oid *p, PgHdr *p
13085 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50  Pg){.  Pager *pP
13086 61 67 65 72 20 3d 20 28 50 61 67 65 72 20 2a 29  ager = (Pager *)
13087 70 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  p;.  int rc = SQ
13088 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65  LITE_OK;..  asse
13089 72 74 28 20 70 50 67 2d 3e 70 50 61 67 65 72 3d  rt( pPg->pPager=
1308a 3d 70 50 61 67 65 72 20 29 3b 0a 20 20 61 73 73  =pPager );.  ass
1308b 65 72 74 28 20 70 50 67 2d 3e 66 6c 61 67 73 26  ert( pPg->flags&
1308c 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b 0a 0a  PGHDR_DIRTY );..
1308d 20 20 2f 2a 20 54 68 65 20 64 6f 4e 6f 74 53 79    /* The doNotSy
1308e 6e 63 20 66 6c 61 67 20 69 73 20 73 65 74 20 62  nc flag is set b
1308f 79 20 74 68 65 20 73 71 6c 69 74 65 33 50 61 67  y the sqlite3Pag
13090 65 72 57 72 69 74 65 28 29 20 66 75 6e 63 74 69  erWrite() functi
13091 6f 6e 20 77 68 69 6c 65 20 69 74 0a 20 20 2a 2a  on while it.  **
13092 20 69 73 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20   is journalling 
13093 61 20 73 65 74 20 6f 66 20 74 77 6f 20 6f 72 20  a set of two or 
13094 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20 70 61  more database pa
13095 67 65 73 20 74 68 61 74 20 61 72 65 20 73 74 6f  ges that are sto
13096 72 65 64 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20  red.  ** on the 
13097 73 61 6d 65 20 64 69 73 6b 20 73 65 63 74 6f 72  same disk sector
13098 2e 20 53 79 6e 63 69 6e 67 20 74 68 65 20 6a 6f  . Syncing the jo
13099 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 61 6c 6c  urnal is not all
1309a 6f 77 65 64 20 77 68 69 6c 65 0a 20 20 2a 2a 20  owed while.  ** 
1309b 74 68 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e  this is happenin
1309c 67 20 61 73 20 69 74 20 69 73 20 69 6d 70 6f 72  g as it is impor
1309d 74 61 6e 74 20 74 68 61 74 20 61 6c 6c 20 6d 65  tant that all me
1309e 6d 62 65 72 73 20 6f 66 20 73 75 63 68 20 61 0a  mbers of such a.
1309f 20 20 2a 2a 20 73 65 74 20 6f 66 20 70 61 67 65    ** set of page
130a0 73 20 61 72 65 20 73 79 6e 63 65 64 20 74 6f 20  s are synced to 
130a1 64 69 73 6b 20 74 6f 67 65 74 68 65 72 2e 20 53  disk together. S
130a2 6f 2c 20 69 66 20 74 68 65 20 70 61 67 65 20 74  o, if the page t
130a3 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 20 20 2a  his function.  *
130a4 2a 20 69 73 20 74 72 79 69 6e 67 20 74 6f 20 6d  * is trying to m
130a5 61 6b 65 20 63 6c 65 61 6e 20 77 69 6c 6c 20 72  ake clean will r
130a6 65 71 75 69 72 65 20 61 20 6a 6f 75 72 6e 61 6c  equire a journal
130a7 20 73 79 6e 63 20 61 6e 64 20 74 68 65 20 64 6f   sync and the do
130a8 4e 6f 74 53 79 6e 63 0a 20 20 2a 2a 20 66 6c 61  NotSync.  ** fla
130a9 67 20 69 73 20 73 65 74 2c 20 72 65 74 75 72 6e  g is set, return
130aa 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61   without doing a
130ab 6e 79 74 68 69 6e 67 2e 20 54 68 65 20 70 63 61  nything. The pca
130ac 63 68 65 20 6c 61 79 65 72 20 77 69 6c 6c 0a 20  che layer will. 
130ad 20 2a 2a 20 6a 75 73 74 20 68 61 76 65 20 74 6f   ** just have to
130ae 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 61 6c   go ahead and al
130af 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61 67  locate a new pag
130b0 65 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 64  e buffer instead
130b1 20 6f 66 0a 20 20 2a 2a 20 72 65 75 73 69 6e 67   of.  ** reusing
130b2 20 70 50 67 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20   pPg..  **.  ** 
130b3 53 69 6d 69 6c 61 72 6c 79 2c 20 69 66 20 74 68  Similarly, if th
130b4 65 20 70 61 67 65 72 20 68 61 73 20 61 6c 72 65  e pager has alre
130b5 61 64 79 20 65 6e 74 65 72 65 64 20 74 68 65 20  ady entered the 
130b6 65 72 72 6f 72 20 73 74 61 74 65 2c 20 64 6f 20  error state, do 
130b7 6e 6f 74 0a 20 20 2a 2a 20 74 72 79 20 74 6f 20  not.  ** try to 
130b8 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e  write the conten
130b9 74 73 20 6f 66 20 70 50 67 20 74 6f 20 64 69 73  ts of pPg to dis
130ba 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45  k..  */.  if( NE
130bb 56 45 52 28 70 50 61 67 65 72 2d 3e 65 72 72 43  VER(pPager->errC
130bc 6f 64 65 29 0a 20 20 20 7c 7c 20 28 70 50 61 67  ode).   || (pPag
130bd 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 26 26  er->doNotSync &&
130be 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44   pPg->flags&PGHD
130bf 52 5f 4e 45 45 44 5f 53 59 4e 43 29 0a 20 20 29  R_NEED_SYNC).  )
130c0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
130c1 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f  ITE_OK;.  }..  /
130c2 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 72 6e  * Sync the journ
130c3 61 6c 20 66 69 6c 65 20 69 66 20 72 65 71 75 69  al file if requi
130c4 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50  red. */.  if( pP
130c5 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  g->flags&PGHDR_N
130c6 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20  EED_SYNC ){.    
130c7 72 63 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c  rc = syncJournal
130c8 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66  (pPager);.    if
130c9 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
130ca 26 26 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53  && pPager->fullS
130cb 79 6e 63 20 26 26 20 0a 20 20 20 20 20 20 21 28  ync && .      !(
130cc 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
130cd 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
130ce 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 20 26  ALMODE_MEMORY) &
130cf 26 0a 20 20 20 20 20 20 21 28 73 71 6c 69 74 65  &.      !(sqlite
130d0 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74  3OsDeviceCharact
130d1 65 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d  eristics(pPager-
130d2 3e 66 64 29 26 53 51 4c 49 54 45 5f 49 4f 43 41  >fd)&SQLITE_IOCA
130d3 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29 0a 20  P_SAFE_APPEND). 
130d4 20 20 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67     ){.      pPag
130d5 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20 20  er->nRec = 0;.  
130d6 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 4a 6f      rc = writeJo
130d7 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29  urnalHdr(pPager)
130d8 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
130d9 2a 20 49 66 20 74 68 65 20 70 61 67 65 20 6e 75  * If the page nu
130da 6d 62 65 72 20 6f 66 20 74 68 69 73 20 70 61 67  mber of this pag
130db 65 20 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e  e is larger than
130dc 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a   the current siz
130dd 65 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 64 61  e of.  ** the da
130de 74 61 62 61 73 65 20 69 6d 61 67 65 2c 20 69 74  tabase image, it
130df 20 6d 61 79 20 6e 65 65 64 20 74 6f 20 62 65 20   may need to be 
130e0 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73  written to the s
130e1 75 62 2d 6a 6f 75 72 6e 61 6c 2e 0a 20 20 2a 2a  ub-journal..  **
130e2 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65   This is because
130e3 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 70 61 67   the call to pag
130e4 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73  er_write_pagelis
130e5 74 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c 20 6e  t() below will n
130e6 6f 74 0a 20 20 2a 2a 20 61 63 74 75 61 6c 6c 79  ot.  ** actually
130e7 20 77 72 69 74 65 20 64 61 74 61 20 74 6f 20 74   write data to t
130e8 68 65 20 66 69 6c 65 20 69 6e 20 74 68 69 73 20  he file in this 
130e9 63 61 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  case..  **.  ** 
130ea 43 6f 6e 73 69 64 65 72 20 74 68 65 20 66 6f 6c  Consider the fol
130eb 6c 6f 77 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lowing sequence 
130ec 6f 66 20 65 76 65 6e 74 73 3a 0a 20 20 2a 2a 0a  of events:.  **.
130ed 20 20 2a 2a 20 20 20 42 45 47 49 4e 3b 0a 20 20    **   BEGIN;.  
130ee 2a 2a 20 20 20 20 20 3c 6a 6f 75 72 6e 61 6c 20  **     <journal 
130ef 70 61 67 65 20 58 3e 0a 20 20 2a 2a 20 20 20 20  page X>.  **    
130f0 20 3c 6d 6f 64 69 66 79 20 70 61 67 65 20 58 3e   <modify page X>
130f1 0a 20 20 2a 2a 20 20 20 20 20 53 41 56 45 50 4f  .  **     SAVEPO
130f2 49 4e 54 20 73 70 3b 0a 20 20 2a 2a 20 20 20 20  INT sp;.  **    
130f3 20 20 20 3c 73 68 72 69 6e 6b 20 64 61 74 61 62     <shrink datab
130f4 61 73 65 20 66 69 6c 65 20 74 6f 20 59 20 70 61  ase file to Y pa
130f5 67 65 73 3e 0a 20 20 2a 2a 20 20 20 20 20 20 20  ges>.  **       
130f6 70 61 67 65 72 53 74 72 65 73 73 28 70 61 67 65  pagerStress(page
130f7 20 58 29 0a 20 20 2a 2a 20 20 20 20 20 52 4f 4c   X).  **     ROL
130f8 4c 42 41 43 4b 20 54 4f 20 73 70 3b 0a 20 20 2a  LBACK TO sp;.  *
130f9 2a 0a 20 20 2a 2a 20 49 66 20 28 58 3e 59 29 2c  *.  ** If (X>Y),
130fa 20 74 68 65 6e 20 77 68 65 6e 20 70 61 67 65 72   then when pager
130fb 53 74 72 65 73 73 20 69 73 20 63 61 6c 6c 65 64  Stress is called
130fc 20 70 61 67 65 20 58 20 77 69 6c 6c 20 6e 6f 74   page X will not
130fd 20 62 65 20 77 72 69 74 74 65 6e 0a 20 20 2a 2a   be written.  **
130fe 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74 61   out to the data
130ff 62 61 73 65 20 66 69 6c 65 2c 20 62 75 74 20 77  base file, but w
13100 69 6c 6c 20 62 65 20 64 72 6f 70 70 65 64 20 66  ill be dropped f
13101 72 6f 6d 20 74 68 65 20 63 61 63 68 65 2e 20 54  rom the cache. T
13102 68 65 6e 2c 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77  hen,.  ** follow
13103 69 6e 67 20 74 68 65 20 22 52 4f 4c 4c 42 41 43  ing the "ROLLBAC
13104 4b 20 54 4f 20 73 70 22 20 73 74 61 74 65 6d 65  K TO sp" stateme
13105 6e 74 2c 20 72 65 61 64 69 6e 67 20 70 61 67 65  nt, reading page
13106 20 58 20 77 69 6c 6c 20 72 65 61 64 0a 20 20 2a   X will read.  *
13107 2a 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20  * data from the 
13108 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 54  database file. T
13109 68 69 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20  his will be the 
1310a 63 6f 70 79 20 6f 66 20 70 61 67 65 20 58 20 61  copy of page X a
1310b 73 20 69 74 0a 20 20 2a 2a 20 77 61 73 20 77 68  s it.  ** was wh
1310c 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  en the transacti
1310d 6f 6e 20 73 74 61 72 74 65 64 2c 20 6e 6f 74 20  on started, not 
1310e 61 73 20 69 74 20 77 61 73 20 77 68 65 6e 20 22  as it was when "
1310f 53 41 56 45 50 4f 49 4e 54 20 73 70 22 0a 20 20  SAVEPOINT sp".  
13110 2a 2a 20 77 61 73 20 65 78 65 63 75 74 65 64 2e  ** was executed.
13111 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 73  .  **.  ** The s
13112 6f 6c 75 74 69 6f 6e 20 69 73 20 74 6f 20 77 72  olution is to wr
13113 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ite the current 
13114 64 61 74 61 20 66 6f 72 20 70 61 67 65 20 58 20  data for page X 
13115 69 6e 74 6f 20 74 68 65 20 0a 20 20 2a 2a 20 73  into the .  ** s
13116 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ub-journal file 
13117 6e 6f 77 20 28 69 66 20 69 74 20 69 73 20 6e 6f  now (if it is no
13118 74 20 61 6c 72 65 61 64 79 20 74 68 65 72 65 29  t already there)
13119 2c 20 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c  , so that it wil
1311a 6c 0a 20 20 2a 2a 20 62 65 20 72 65 73 74 6f 72  l.  ** be restor
1311b 65 64 20 74 6f 20 69 74 73 20 63 75 72 72 65 6e  ed to its curren
1311c 74 20 76 61 6c 75 65 20 77 68 65 6e 20 74 68 65  t value when the
1311d 20 22 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 73 70   "ROLLBACK TO sp
1311e 22 20 69 73 20 0a 20 20 2a 2a 20 65 78 65 63 75  " is .  ** execu
1311f 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ted..  */.  if( 
13120 4e 45 56 45 52 28 0a 20 20 20 20 20 20 72 63 3d  NEVER(.      rc=
13121 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50  =SQLITE_OK && pP
13122 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d 3e  g->pgno>pPager->
13123 64 62 53 69 7a 65 20 26 26 20 73 75 62 6a 52 65  dbSize && subjRe
13124 71 75 69 72 65 73 50 61 67 65 28 70 50 67 29 0a  quiresPage(pPg).
13125 20 20 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20    ) ){.    rc = 
13126 73 75 62 6a 6f 75 72 6e 61 6c 50 61 67 65 28 70  subjournalPage(p
13127 50 67 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57  Pg);.  }..  /* W
13128 72 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74  rite the content
13129 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 6f 75  s of the page ou
1312a 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  t to the databas
1312b 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 69 66 28  e file. */.  if(
1312c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1312d 7b 0a 20 20 20 20 70 50 67 2d 3e 70 44 69 72 74  {.    pPg->pDirt
1312e 79 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20  y = 0;.    rc = 
1312f 70 61 67 65 72 5f 77 72 69 74 65 5f 70 61 67 65  pager_write_page
13130 6c 69 73 74 28 70 50 67 29 3b 0a 20 20 7d 0a 0a  list(pPg);.  }..
13131 20 20 2f 2a 20 4d 61 72 6b 20 74 68 65 20 70 61    /* Mark the pa
13132 67 65 20 61 73 20 63 6c 65 61 6e 2e 20 2a 2f 0a  ge as clean. */.
13133 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
13134 5f 4f 4b 20 29 7b 0a 20 20 20 20 50 41 47 45 52  _OK ){.    PAGER
13135 54 52 41 43 45 28 28 22 53 54 52 45 53 53 20 25  TRACE(("STRESS %
13136 64 20 70 61 67 65 20 25 64 5c 6e 22 2c 20 50 41  d page %d\n", PA
13137 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20 70  GERID(pPager), p
13138 50 67 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20  Pg->pgno));.    
13139 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b  sqlite3PcacheMak
1313a 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20 20 7d  eClean(pPg);.  }
1313b 0a 0a 20 20 72 65 74 75 72 6e 20 70 61 67 65 72  ..  return pager
1313c 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72  _error(pPager, r
1313d 63 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c  c);.}.../*.** Al
1313e 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e 69 74 69  locate and initi
1313f 61 6c 69 7a 65 20 61 20 6e 65 77 20 50 61 67 65  alize a new Page
13140 72 20 6f 62 6a 65 63 74 20 61 6e 64 20 70 75 74  r object and put
13141 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74   a pointer to it
13142 0a 2a 2a 20 69 6e 20 2a 70 70 50 61 67 65 72 2e  .** in *ppPager.
13143 20 54 68 65 20 70 61 67 65 72 20 73 68 6f 75 6c   The pager shoul
13144 64 20 65 76 65 6e 74 75 61 6c 6c 79 20 62 65 20  d eventually be 
13145 66 72 65 65 64 20 62 79 20 70 61 73 73 69 6e 67  freed by passing
13146 20 69 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65   it.** to sqlite
13147 33 50 61 67 65 72 43 6c 6f 73 65 28 29 2e 0a 2a  3PagerClose()..*
13148 2a 0a 2a 2a 20 54 68 65 20 7a 46 69 6c 65 6e 61  *.** The zFilena
13149 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74  me argument is t
1314a 68 65 20 70 61 74 68 20 74 6f 20 74 68 65 20 64  he path to the d
1314b 61 74 61 62 61 73 65 20 66 69 6c 65 20 74 6f 20  atabase file to 
1314c 6f 70 65 6e 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c  open..** If zFil
1314d 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 20 74 68  ename is NULL th
1314e 65 6e 20 61 20 72 61 6e 64 6f 6d 6c 79 2d 6e 61  en a randomly-na
1314f 6d 65 64 20 74 65 6d 70 6f 72 61 72 79 20 66 69  med temporary fi
13150 6c 65 20 69 73 20 63 72 65 61 74 65 64 0a 2a 2a  le is created.**
13151 20 61 6e 64 20 75 73 65 64 20 61 73 20 74 68 65   and used as the
13152 20 66 69 6c 65 20 74 6f 20 62 65 20 63 61 63 68   file to be cach
13153 65 64 2e 20 54 65 6d 70 6f 72 61 72 79 20 66 69  ed. Temporary fi
13154 6c 65 73 20 61 72 65 20 62 65 20 64 65 6c 65 74  les are be delet
13155 65 64 0a 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61  ed.** automatica
13156 6c 6c 79 20 77 68 65 6e 20 74 68 65 79 20 61 72  lly when they ar
13157 65 20 63 6c 6f 73 65 64 2e 20 49 66 20 7a 46 69  e closed. If zFi
13158 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f  lename is ":memo
13159 72 79 3a 22 20 74 68 65 6e 20 0a 2a 2a 20 61 6c  ry:" then .** al
1315a 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73  l information is
1315b 20 68 65 6c 64 20 69 6e 20 63 61 63 68 65 2e 20   held in cache. 
1315c 49 74 20 69 73 20 6e 65 76 65 72 20 77 72 69 74  It is never writ
1315d 74 65 6e 20 74 6f 20 64 69 73 6b 2e 20 0a 2a 2a  ten to disk. .**
1315e 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65   This can be use
1315f 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61  d to implement a
13160 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  n in-memory data
13161 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  base..**.** The 
13162 6e 45 78 74 72 61 20 70 61 72 61 6d 65 74 65 72  nExtra parameter
13163 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20 6e   specifies the n
13164 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f  umber of bytes o
13165 66 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65  f space allocate
13166 64 0a 2a 2a 20 61 6c 6f 6e 67 20 77 69 74 68 20  d.** along with 
13167 65 61 63 68 20 70 61 67 65 20 72 65 66 65 72 65  each page refere
13168 6e 63 65 2e 20 54 68 69 73 20 73 70 61 63 65 20  nce. This space 
13169 69 73 20 61 76 61 69 6c 61 62 6c 65 20 74 6f 20  is available to 
1316a 74 68 65 20 75 73 65 72 0a 2a 2a 20 76 69 61 20  the user.** via 
1316b 74 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72  the sqlite3Pager
1316c 47 65 74 45 78 74 72 61 28 29 20 41 50 49 2e 0a  GetExtra() API..
1316d 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20  **.** The flags 
1316e 61 72 67 75 6d 65 6e 74 20 69 73 20 75 73 65 64  argument is used
1316f 20 74 6f 20 73 70 65 63 69 66 79 20 70 72 6f 70   to specify prop
13170 65 72 74 69 65 73 20 74 68 61 74 20 61 66 66 65  erties that affe
13171 63 74 20 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74  ct the.** operat
13172 69 6f 6e 20 6f 66 20 74 68 65 20 70 61 67 65 72  ion of the pager
13173 2e 20 49 74 20 73 68 6f 75 6c 64 20 62 65 20 70  . It should be p
13174 61 73 73 65 64 20 73 6f 6d 65 20 62 69 74 77 69  assed some bitwi
13175 73 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 0a 2a  se combination.*
13176 2a 20 6f 66 20 74 68 65 20 50 41 47 45 52 5f 4f  * of the PAGER_O
13177 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 61 6e 64 20  MIT_JOURNAL and 
13178 50 41 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f 43  PAGER_NO_READLOC
13179 4b 20 66 6c 61 67 73 2e 0a 2a 2a 0a 2a 2a 20 54  K flags..**.** T
1317a 68 65 20 76 66 73 46 6c 61 67 73 20 70 61 72 61  he vfsFlags para
1317b 6d 65 74 65 72 20 69 73 20 61 20 62 69 74 6d 61  meter is a bitma
1317c 73 6b 20 74 6f 20 70 61 73 73 20 74 6f 20 74 68  sk to pass to th
1317d 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65  e flags paramete
1317e 72 0a 2a 2a 20 6f 66 20 74 68 65 20 78 4f 70 65  r.** of the xOpe
1317f 6e 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  n() method of th
13180 65 20 73 75 70 70 6c 69 65 64 20 56 46 53 20 77  e supplied VFS w
13181 68 65 6e 20 6f 70 65 6e 69 6e 67 20 66 69 6c 65  hen opening file
13182 73 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  s. .**.** If the
13183 20 70 61 67 65 72 20 6f 62 6a 65 63 74 20 69 73   pager object is
13184 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74   allocated and t
13185 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 6c  he specified fil
13186 65 20 6f 70 65 6e 65 64 20 0a 2a 2a 20 73 75 63  e opened .** suc
13187 63 65 73 73 66 75 6c 6c 79 2c 20 53 51 4c 49 54  cessfully, SQLIT
13188 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
13189 20 61 6e 64 20 2a 70 70 50 61 67 65 72 20 73 65   and *ppPager se
1318a 74 20 74 6f 20 70 6f 69 6e 74 20 74 6f 0a 2a 2a  t to point to.**
1318b 20 74 68 65 20 6e 65 77 20 70 61 67 65 72 20 6f   the new pager o
1318c 62 6a 65 63 74 2e 20 49 66 20 61 6e 20 65 72 72  bject. If an err
1318d 6f 72 20 6f 63 63 75 72 73 2c 20 2a 70 70 50 61  or occurs, *ppPa
1318e 67 65 72 20 69 73 20 73 65 74 20 74 6f 20 4e 55  ger is set to NU
1318f 4c 4c 0a 2a 2a 20 61 6e 64 20 65 72 72 6f 72 20  LL.** and error 
13190 63 6f 64 65 20 72 65 74 75 72 6e 65 64 2e 20 54  code returned. T
13191 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79  his function may
13192 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
13193 4f 4d 45 4d 0a 2a 2a 20 28 73 71 6c 69 74 65 33  OMEM.** (sqlite3
13194 4d 61 6c 6c 6f 63 28 29 20 69 73 20 75 73 65 64  Malloc() is used
13195 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d   to allocate mem
13196 6f 72 79 29 2c 20 53 51 4c 49 54 45 5f 43 41 4e  ory), SQLITE_CAN
13197 54 4f 50 45 4e 20 6f 72 20 0a 2a 2a 20 76 61 72  TOPEN or .** var
13198 69 6f 75 73 20 53 51 4c 49 54 45 5f 49 4f 5f 58  ious SQLITE_IO_X
13199 58 58 20 65 72 72 6f 72 73 2e 0a 2a 2f 0a 53 51  XX errors..*/.SQ
1319a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1319b 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65   sqlite3PagerOpe
1319c 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  n(.  sqlite3_vfs
1319d 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 2f 2a   *pVfs,       /*
1319e 20 54 68 65 20 76 69 72 74 75 61 6c 20 66 69 6c   The virtual fil
1319f 65 20 73 79 73 74 65 6d 20 74 6f 20 75 73 65 20  e system to use 
131a0 2a 2f 0a 20 20 50 61 67 65 72 20 2a 2a 70 70 50  */.  Pager **ppP
131a1 61 67 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a  ager,         /*
131a2 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 74 68 65   OUT: Return the
131a3 20 50 61 67 65 72 20 73 74 72 75 63 74 75 72 65   Pager structure
131a4 20 68 65 72 65 20 2a 2f 0a 20 20 63 6f 6e 73 74   here */.  const
131a5 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65   char *zFilename
131a6 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74  ,   /* Name of t
131a7 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
131a8 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e   to open */.  in
131a9 74 20 6e 45 78 74 72 61 2c 20 20 20 20 20 20 20  t nExtra,       
131aa 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20         /* Extra 
131ab 62 79 74 65 73 20 61 70 70 65 6e 64 20 74 6f 20  bytes append to 
131ac 65 61 63 68 20 69 6e 2d 6d 65 6d 6f 72 79 20 70  each in-memory p
131ad 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61  age */.  int fla
131ae 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gs,             
131af 20 20 2f 2a 20 66 6c 61 67 73 20 63 6f 6e 74 72    /* flags contr
131b0 6f 6c 6c 69 6e 67 20 74 68 69 73 20 66 69 6c 65  olling this file
131b1 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61   */.  int vfsFla
131b2 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  gs,            /
131b3 2a 20 66 6c 61 67 73 20 70 61 73 73 65 64 20 74  * flags passed t
131b4 68 72 6f 75 67 68 20 74 6f 20 73 71 6c 69 74 65  hrough to sqlite
131b5 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f  3_vfs.xOpen() */
131b6 0a 20 20 76 6f 69 64 20 28 2a 78 52 65 69 6e 69  .  void (*xReini
131b7 74 29 28 44 62 50 61 67 65 2a 29 20 2f 2a 20 46  t)(DbPage*) /* F
131b8 75 6e 63 74 69 6f 6e 20 74 6f 20 72 65 69 6e 69  unction to reini
131b9 74 69 61 6c 69 7a 65 20 70 61 67 65 73 20 2a 2f  tialize pages */
131ba 0a 29 7b 0a 20 20 75 38 20 2a 70 50 74 72 3b 0a  .){.  u8 *pPtr;.
131bb 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20    Pager *pPager 
131bc 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 50 61  = 0;       /* Pa
131bd 67 65 72 20 6f 62 6a 65 63 74 20 74 6f 20 61 6c  ger object to al
131be 6c 6f 63 61 74 65 20 61 6e 64 20 72 65 74 75 72  locate and retur
131bf 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20  n */.  int rc = 
131c0 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
131c1 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
131c2 2f 0a 20 20 69 6e 74 20 74 65 6d 70 46 69 6c 65  /.  int tempFile
131c3 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
131c4 54 72 75 65 20 66 6f 72 20 74 65 6d 70 20 66 69  True for temp fi
131c5 6c 65 73 20 28 69 6e 63 6c 2e 20 69 6e 2d 6d 65  les (incl. in-me
131c6 6d 6f 72 79 20 66 69 6c 65 73 29 20 2a 2f 0a 20  mory files) */. 
131c7 20 69 6e 74 20 6d 65 6d 44 62 20 3d 20 30 3b 20   int memDb = 0; 
131c8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
131c9 65 20 69 66 20 74 68 69 73 20 69 73 20 61 6e 20  e if this is an 
131ca 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 20 2a  in-memory file *
131cb 2f 0a 20 20 69 6e 74 20 72 65 61 64 4f 6e 6c 79  /.  int readOnly
131cc 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
131cd 54 72 75 65 20 69 66 20 74 68 69 73 20 69 73 20  True if this is 
131ce 61 20 72 65 61 64 2d 6f 6e 6c 79 20 66 69 6c 65  a read-only file
131cf 20 2a 2f 0a 20 20 69 6e 74 20 6a 6f 75 72 6e 61   */.  int journa
131d0 6c 46 69 6c 65 53 69 7a 65 3b 20 20 20 20 20 2f  lFileSize;     /
131d1 2a 20 42 79 74 65 73 20 74 6f 20 61 6c 6c 6f 63  * Bytes to alloc
131d2 61 74 65 20 66 6f 72 20 65 61 63 68 20 6a 6f 75  ate for each jou
131d3 72 6e 61 6c 20 66 64 20 2a 2f 0a 20 20 63 68 61  rnal fd */.  cha
131d4 72 20 2a 7a 50 61 74 68 6e 61 6d 65 20 3d 20 30  r *zPathname = 0
131d5 3b 20 20 20 20 20 2f 2a 20 46 75 6c 6c 20 70 61  ;     /* Full pa
131d6 74 68 20 74 6f 20 64 61 74 61 62 61 73 65 20 66  th to database f
131d7 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 61  ile */.  int nPa
131d8 74 68 6e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20  thname = 0;     
131d9 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
131da 79 74 65 73 20 69 6e 20 7a 50 61 74 68 6e 61 6d  ytes in zPathnam
131db 65 20 2a 2f 0a 20 20 69 6e 74 20 75 73 65 4a 6f  e */.  int useJo
131dc 75 72 6e 61 6c 20 3d 20 28 66 6c 61 67 73 20 26  urnal = (flags &
131dd 20 50 41 47 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52   PAGER_OMIT_JOUR
131de 4e 41 4c 29 3d 3d 30 3b 20 2f 2a 20 46 61 6c 73  NAL)==0; /* Fals
131df 65 20 74 6f 20 6f 6d 69 74 20 6a 6f 75 72 6e 61  e to omit journa
131e0 6c 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 52 65 61  l */.  int noRea
131e1 64 6c 6f 63 6b 20 3d 20 28 66 6c 61 67 73 20 26  dlock = (flags &
131e2 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f   PAGER_NO_READLO
131e3 43 4b 29 21 3d 30 3b 20 20 2f 2a 20 54 72 75 65  CK)!=0;  /* True
131e4 20 74 6f 20 6f 6d 69 74 20 72 65 61 64 2d 6c 6f   to omit read-lo
131e5 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 70 63 61 63  ck */.  int pcac
131e6 68 65 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33  heSize = sqlite3
131e7 50 63 61 63 68 65 53 69 7a 65 28 29 3b 20 20 20  PcacheSize();   
131e8 20 20 20 20 2f 2a 20 42 79 74 65 73 20 74 6f 20      /* Bytes to 
131e9 61 6c 6c 6f 63 61 74 65 20 66 6f 72 20 50 43 61  allocate for PCa
131ea 63 68 65 20 2a 2f 0a 20 20 75 31 36 20 73 7a 50  che */.  u16 szP
131eb 61 67 65 44 66 6c 74 20 3d 20 53 51 4c 49 54 45  ageDflt = SQLITE
131ec 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49  _DEFAULT_PAGE_SI
131ed 5a 45 3b 20 20 2f 2a 20 44 65 66 61 75 6c 74 20  ZE;  /* Default 
131ee 70 61 67 65 20 73 69 7a 65 20 2a 2f 0a 0a 20 20  page size */..  
131ef 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f  /* Figure out ho
131f0 77 20 6d 75 63 68 20 73 70 61 63 65 20 69 73 20  w much space is 
131f1 72 65 71 75 69 72 65 64 20 66 6f 72 20 65 61 63  required for eac
131f2 68 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2d 68  h journal file-h
131f3 61 6e 64 6c 65 0a 20 20 2a 2a 20 28 74 68 65 72  andle.  ** (ther
131f4 65 20 61 72 65 20 74 77 6f 20 6f 66 20 74 68 65  e are two of the
131f5 6d 2c 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72  m, the main jour
131f6 6e 61 6c 20 61 6e 64 20 74 68 65 20 73 75 62 2d  nal and the sub-
131f7 6a 6f 75 72 6e 61 6c 29 2e 20 54 68 69 73 0a 20  journal). This. 
131f8 20 2a 2a 20 69 73 20 74 68 65 20 6d 61 78 69 6d   ** is the maxim
131f9 75 6d 20 73 70 61 63 65 20 72 65 71 75 69 72 65  um space require
131fa 64 20 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f  d for an in-memo
131fb 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ry journal file 
131fc 68 61 6e 64 6c 65 20 0a 20 20 2a 2a 20 61 6e 64  handle .  ** and
131fd 20 61 20 72 65 67 75 6c 61 72 20 6a 6f 75 72 6e   a regular journ
131fe 61 6c 20 66 69 6c 65 2d 68 61 6e 64 6c 65 2e 20  al file-handle. 
131ff 4e 6f 74 65 20 74 68 61 74 20 61 20 22 72 65 67  Note that a "reg
13200 75 6c 61 72 20 6a 6f 75 72 6e 61 6c 2d 68 61 6e  ular journal-han
13201 64 6c 65 22 0a 20 20 2a 2a 20 6d 61 79 20 62 65  dle".  ** may be
13202 20 61 20 77 72 61 70 70 65 72 20 63 61 70 61 62   a wrapper capab
13203 6c 65 20 6f 66 20 63 61 63 68 69 6e 67 20 74 68  le of caching th
13204 65 20 66 69 72 73 74 20 70 6f 72 74 69 6f 6e 20  e first portion 
13205 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20  of the journal. 
13206 20 2a 2a 20 66 69 6c 65 20 69 6e 20 6d 65 6d 6f   ** file in memo
13207 72 79 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ry to implement 
13208 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65  the atomic-write
13209 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 28 73   optimization (s
1320a 65 65 20 0a 20 20 2a 2a 20 73 6f 75 72 63 65 20  ee .  ** source 
1320b 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63 29 2e  file journal.c).
1320c 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
1320d 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 70  te3JournalSize(p
1320e 56 66 73 29 3e 73 71 6c 69 74 65 33 4d 65 6d 4a  Vfs)>sqlite3MemJ
1320f 6f 75 72 6e 61 6c 53 69 7a 65 28 29 20 29 7b 0a  ournalSize() ){.
13210 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53      journalFileS
13211 69 7a 65 20 3d 20 52 4f 55 4e 44 38 28 73 71 6c  ize = ROUND8(sql
13212 69 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28  ite3JournalSize(
13213 70 56 66 73 29 29 3b 0a 20 20 7d 65 6c 73 65 7b  pVfs));.  }else{
13214 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65  .    journalFile
13215 53 69 7a 65 20 3d 20 52 4f 55 4e 44 38 28 73 71  Size = ROUND8(sq
13216 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53  lite3MemJournalS
13217 69 7a 65 28 29 29 3b 0a 20 20 7d 0a 0a 20 20 2f  ize());.  }..  /
13218 2a 20 53 65 74 20 74 68 65 20 6f 75 74 70 75 74  * Set the output
13219 20 76 61 72 69 61 62 6c 65 20 74 6f 20 4e 55 4c   variable to NUL
1321a 4c 20 69 6e 20 63 61 73 65 20 61 6e 20 65 72 72  L in case an err
1321b 6f 72 20 6f 63 63 75 72 73 2e 20 2a 2f 0a 20 20  or occurs. */.  
1321c 2a 70 70 50 61 67 65 72 20 3d 20 30 3b 0a 0a 20  *ppPager = 0;.. 
1321d 20 2f 2a 20 43 6f 6d 70 75 74 65 20 61 6e 64 20   /* Compute and 
1321e 73 74 6f 72 65 20 74 68 65 20 66 75 6c 6c 20 70  store the full p
1321f 61 74 68 6e 61 6d 65 20 69 6e 20 61 6e 20 61 6c  athname in an al
13220 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72 20 70  located buffer p
13221 6f 69 6e 74 65 64 0a 20 20 2a 2a 20 74 6f 20 62  ointed.  ** to b
13222 79 20 7a 50 61 74 68 6e 61 6d 65 2c 20 6c 65 6e  y zPathname, len
13223 67 74 68 20 6e 50 61 74 68 6e 61 6d 65 2e 20 4f  gth nPathname. O
13224 72 2c 20 69 66 20 74 68 69 73 20 69 73 20 61 20  r, if this is a 
13225 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2c 0a  temporary file,.
13226 20 20 2a 2a 20 6c 65 61 76 65 20 62 6f 74 68 20    ** leave both 
13227 6e 50 61 74 68 6e 61 6d 65 20 61 6e 64 20 7a 50  nPathname and zP
13228 61 74 68 6e 61 6d 65 20 73 65 74 20 74 6f 20 30  athname set to 0
13229 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 46 69  ..  */.  if( zFi
1322a 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c 65 6e  lename && zFilen
1322b 61 6d 65 5b 30 5d 20 29 7b 0a 20 20 20 20 6e 50  ame[0] ){.    nP
1322c 61 74 68 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e  athname = pVfs->
1322d 6d 78 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20  mxPathname+1;.  
1322e 20 20 7a 50 61 74 68 6e 61 6d 65 20 3d 20 73 71    zPathname = sq
1322f 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 50 61 74  lite3Malloc(nPat
13230 68 6e 61 6d 65 2a 32 29 3b 0a 20 20 20 20 69 66  hname*2);.    if
13231 28 20 7a 50 61 74 68 6e 61 6d 65 3d 3d 30 20 29  ( zPathname==0 )
13232 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
13233 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
13234 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
13235 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a  E_OMIT_MEMORYDB.
13236 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a      if( strcmp(z
13237 46 69 6c 65 6e 61 6d 65 2c 22 3a 6d 65 6d 6f 72  Filename,":memor
13238 79 3a 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  y:")==0 ){.     
13239 20 6d 65 6d 44 62 20 3d 20 31 3b 0a 20 20 20 20   memDb = 1;.    
1323a 20 20 7a 50 61 74 68 6e 61 6d 65 5b 30 5d 20 3d    zPathname[0] =
1323b 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65   0;.    }else.#e
1323c 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20 20 20 20  ndif.    {.     
1323d 20 7a 50 61 74 68 6e 61 6d 65 5b 30 5d 20 3d 20   zPathname[0] = 
1323e 30 3b 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20  0; /* Make sure 
1323f 69 6e 69 74 69 61 6c 69 7a 65 64 20 65 76 65 6e  initialized even
13240 20 69 66 20 46 75 6c 6c 50 61 74 68 6e 61 6d 65   if FullPathname
13241 28 29 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20  () fails */.    
13242 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
13243 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 70 56 66  FullPathname(pVf
13244 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 50  s, zFilename, nP
13245 61 74 68 6e 61 6d 65 2c 20 7a 50 61 74 68 6e 61  athname, zPathna
13246 6d 65 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  me);.    }..    
13247 6e 50 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69  nPathname = sqli
13248 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 50 61 74  te3Strlen30(zPat
13249 68 6e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  hname);.    if( 
1324a 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1324b 20 6e 50 61 74 68 6e 61 6d 65 2b 38 3e 70 56 66   nPathname+8>pVf
1324c 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20 29 7b  s->mxPathname ){
1324d 0a 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62  .      /* This b
1324e 72 61 6e 63 68 20 69 73 20 74 61 6b 65 6e 20 77  ranch is taken w
1324f 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  hen the journal 
13250 70 61 74 68 20 72 65 71 75 69 72 65 64 20 62 79  path required by
13251 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 61  .      ** the da
13252 74 61 62 61 73 65 20 62 65 69 6e 67 20 6f 70 65  tabase being ope
13253 6e 65 64 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65  ned will be more
13254 20 74 68 61 6e 20 70 56 66 73 2d 3e 6d 78 50 61   than pVfs->mxPa
13255 74 68 6e 61 6d 65 0a 20 20 20 20 20 20 2a 2a 20  thname.      ** 
13256 62 79 74 65 73 20 69 6e 20 6c 65 6e 67 74 68 2e  bytes in length.
13257 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 65 20   This means the 
13258 64 61 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20  database cannot 
13259 62 65 20 6f 70 65 6e 65 64 2c 0a 20 20 20 20 20  be opened,.     
1325a 20 2a 2a 20 61 73 20 69 74 20 77 69 6c 6c 20 6e   ** as it will n
1325b 6f 74 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74  ot be possible t
1325c 6f 20 6f 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e  o open the journ
1325d 61 6c 20 66 69 6c 65 20 6f 72 20 65 76 65 6e 0a  al file or even.
1325e 20 20 20 20 20 20 2a 2a 20 63 68 65 63 6b 20 66        ** check f
1325f 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c  or a hot-journal
13260 20 62 65 66 6f 72 65 20 72 65 61 64 69 6e 67 2e   before reading.
13261 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
13262 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54  rc = SQLITE_CANT
13263 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  OPEN;.    }.    
13264 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
13265 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  K ){.      sqlit
13266 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e 61 6d  e3_free(zPathnam
13267 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  e);.      return
13268 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a   rc;.    }.  }..
13269 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65    /* Allocate me
1326a 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 50 61 67  mory for the Pag
1326b 65 72 20 73 74 72 75 63 74 75 72 65 2c 20 50 43  er structure, PC
1326c 61 63 68 65 20 6f 62 6a 65 63 74 2c 20 74 68 65  ache object, the
1326d 0a 20 20 2a 2a 20 74 68 72 65 65 20 66 69 6c 65  .  ** three file
1326e 20 64 65 73 63 72 69 70 74 6f 72 73 2c 20 74 68   descriptors, th
1326f 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
13270 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 6a 6f 75  name and the jou
13271 72 6e 61 6c 20 0a 20 20 2a 2a 20 66 69 6c 65 20  rnal .  ** file 
13272 6e 61 6d 65 2e 20 54 68 65 20 6c 61 79 6f 75 74  name. The layout
13273 20 69 6e 20 6d 65 6d 6f 72 79 20 69 73 20 61 73   in memory is as
13274 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20   follows:.  **. 
13275 20 2a 2a 20 20 20 20 20 50 61 67 65 72 20 6f 62   **     Pager ob
13276 6a 65 63 74 20 20 20 20 20 20 20 20 20 20 20 20  ject            
13277 20 20 20 20 20 20 20 20 28 73 69 7a 65 6f 66 28          (sizeof(
13278 50 61 67 65 72 29 20 62 79 74 65 73 29 0a 20 20  Pager) bytes).  
13279 2a 2a 20 20 20 20 20 50 43 61 63 68 65 20 6f 62  **     PCache ob
1327a 6a 65 63 74 20 20 20 20 20 20 20 20 20 20 20 20  ject            
1327b 20 20 20 20 20 20 20 28 73 71 6c 69 74 65 33 50         (sqlite3P
1327c 63 61 63 68 65 53 69 7a 65 28 29 20 62 79 74 65  cacheSize() byte
1327d 73 29 0a 20 20 2a 2a 20 20 20 20 20 44 61 74 61  s).  **     Data
1327e 62 61 73 65 20 66 69 6c 65 20 68 61 6e 64 6c 65  base file handle
1327f 20 20 20 20 20 20 20 20 20 20 20 20 28 70 56 66              (pVf
13280 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 62 79 74 65  s->szOsFile byte
13281 73 29 0a 20 20 2a 2a 20 20 20 20 20 53 75 62 2d  s).  **     Sub-
13282 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 6e  journal file han
13283 64 6c 65 20 20 20 20 20 20 20 20 20 28 6a 6f 75  dle         (jou
13284 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 62 79 74  rnalFileSize byt
13285 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 4d 61 69  es).  **     Mai
13286 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68  n journal file h
13287 61 6e 64 6c 65 20 20 20 20 20 20 20 20 28 6a 6f  andle        (jo
13288 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 62 79  urnalFileSize by
13289 74 65 73 29 0a 20 20 2a 2a 20 20 20 20 20 44 61  tes).  **     Da
1328a 74 61 62 61 73 65 20 66 69 6c 65 20 6e 61 6d 65  tabase file name
1328b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 6e                (n
1328c 50 61 74 68 6e 61 6d 65 2b 31 20 62 79 74 65 73  Pathname+1 bytes
1328d 29 0a 20 20 2a 2a 20 20 20 20 20 4a 6f 75 72 6e  ).  **     Journ
1328e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20 20 20 20  al file name    
1328f 20 20 20 20 20 20 20 20 20 20 20 28 6e 50 61 74             (nPat
13290 68 6e 61 6d 65 2b 38 2b 31 20 62 79 74 65 73 29  hname+8+1 bytes)
13291 0a 20 20 2a 2f 0a 20 20 70 50 74 72 20 3d 20 28  .  */.  pPtr = (
13292 75 38 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c  u8 *)sqlite3Mall
13293 6f 63 5a 65 72 6f 28 0a 20 20 20 20 52 4f 55 4e  ocZero(.    ROUN
13294 44 38 28 73 69 7a 65 6f 66 28 2a 70 50 61 67 65  D8(sizeof(*pPage
13295 72 29 29 20 2b 20 20 20 20 20 20 2f 2a 20 50 61  r)) +      /* Pa
13296 67 65 72 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ger structure */
13297 0a 20 20 20 20 52 4f 55 4e 44 38 28 70 63 61 63  .    ROUND8(pcac
13298 68 65 53 69 7a 65 29 20 2b 20 20 20 20 20 20 20  heSize) +       
13299 20 20 20 20 2f 2a 20 50 43 61 63 68 65 20 6f 62      /* PCache ob
1329a 6a 65 63 74 20 2a 2f 0a 20 20 20 20 52 4f 55 4e  ject */.    ROUN
1329b 44 38 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c  D8(pVfs->szOsFil
1329c 65 29 20 2b 20 20 20 20 20 20 20 2f 2a 20 54 68  e) +       /* Th
1329d 65 20 6d 61 69 6e 20 64 62 20 66 69 6c 65 20 2a  e main db file *
1329e 2f 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c  /.    journalFil
1329f 65 53 69 7a 65 20 2a 20 32 20 2b 20 20 20 20 20  eSize * 2 +     
132a0 20 20 20 20 20 2f 2a 20 54 68 65 20 74 77 6f 20       /* The two 
132a1 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f  journal files */
132a2 20 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20   .    nPathname 
132a3 2b 20 31 20 2b 20 20 20 20 20 20 20 20 20 20 20  + 1 +           
132a4 20 20 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d       /* zFilenam
132a5 65 20 2a 2f 0a 20 20 20 20 6e 50 61 74 68 6e 61  e */.    nPathna
132a6 6d 65 20 2b 20 38 20 2b 20 31 20 20 20 20 20 20  me + 8 + 1      
132a7 20 20 20 20 20 20 20 20 2f 2a 20 7a 4a 6f 75 72          /* zJour
132a8 6e 61 6c 20 2a 2f 0a 20 20 29 3b 0a 20 20 61 73  nal */.  );.  as
132a9 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45  sert( EIGHT_BYTE
132aa 5f 41 4c 49 47 4e 4d 45 4e 54 28 53 51 4c 49 54  _ALIGNMENT(SQLIT
132ab 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 6a 6f 75  E_INT_TO_PTR(jou
132ac 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 29 20 29  rnalFileSize)) )
132ad 3b 0a 20 20 69 66 28 20 21 70 50 74 72 20 29 7b  ;.  if( !pPtr ){
132ae 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
132af 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20  e(zPathname);.  
132b0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
132b1 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 50 61  NOMEM;.  }.  pPa
132b2 67 65 72 20 3d 20 20 20 20 20 20 20 20 20 20 20  ger =           
132b3 20 20 20 28 50 61 67 65 72 2a 29 28 70 50 74 72     (Pager*)(pPtr
132b4 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 50 43  );.  pPager->pPC
132b5 61 63 68 65 20 3d 20 20 20 20 28 50 43 61 63 68  ache =    (PCach
132b6 65 2a 29 28 70 50 74 72 20 2b 3d 20 52 4f 55 4e  e*)(pPtr += ROUN
132b7 44 38 28 73 69 7a 65 6f 66 28 2a 70 50 61 67 65  D8(sizeof(*pPage
132b8 72 29 29 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  r)));.  pPager->
132b9 66 64 20 3d 20 20 20 28 73 71 6c 69 74 65 33 5f  fd =   (sqlite3_
132ba 66 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 52  file*)(pPtr += R
132bb 4f 55 4e 44 38 28 70 63 61 63 68 65 53 69 7a 65  OUND8(pcacheSize
132bc 29 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 6a  ));.  pPager->sj
132bd 66 64 20 3d 20 28 73 71 6c 69 74 65 33 5f 66 69  fd = (sqlite3_fi
132be 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 52 4f 55  le*)(pPtr += ROU
132bf 4e 44 38 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69  ND8(pVfs->szOsFi
132c0 6c 65 29 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  le));.  pPager->
132c1 6a 66 64 20 3d 20 20 28 73 71 6c 69 74 65 33 5f  jfd =  (sqlite3_
132c2 66 69 6c 65 2a 29 28 70 50 74 72 20 2b 3d 20 6a  file*)(pPtr += j
132c3 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 29 3b  ournalFileSize);
132c4 0a 20 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65  .  pPager->zFile
132c5 6e 61 6d 65 20 3d 20 20 20 20 28 63 68 61 72 2a  name =    (char*
132c6 29 28 70 50 74 72 20 2b 3d 20 6a 6f 75 72 6e 61  )(pPtr += journa
132c7 6c 46 69 6c 65 53 69 7a 65 29 3b 0a 20 20 61 73  lFileSize);.  as
132c8 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54 45  sert( EIGHT_BYTE
132c9 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 50 61 67 65  _ALIGNMENT(pPage
132ca 72 2d 3e 6a 66 64 29 20 29 3b 0a 0a 20 20 2f 2a  r->jfd) );..  /*
132cb 20 46 69 6c 6c 20 69 6e 20 74 68 65 20 50 61 67   Fill in the Pag
132cc 65 72 2e 7a 46 69 6c 65 6e 61 6d 65 20 61 6e 64  er.zFilename and
132cd 20 50 61 67 65 72 2e 7a 4a 6f 75 72 6e 61 6c 20   Pager.zJournal 
132ce 62 75 66 66 65 72 73 2c 20 69 66 20 72 65 71 75  buffers, if requ
132cf 69 72 65 64 2e 20 2a 2f 0a 20 20 69 66 28 20 7a  ired. */.  if( z
132d0 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20  Pathname ){.    
132d1 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c  pPager->zJournal
132d2 20 3d 20 20 20 28 63 68 61 72 2a 29 28 70 50 74   =   (char*)(pPt
132d3 72 20 2b 3d 20 6e 50 61 74 68 6e 61 6d 65 20 2b  r += nPathname +
132d4 20 31 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28   1);.    memcpy(
132d5 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d  pPager->zFilenam
132d6 65 2c 20 7a 50 61 74 68 6e 61 6d 65 2c 20 6e 50  e, zPathname, nP
132d7 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d 65  athname);.    me
132d8 6d 63 70 79 28 70 50 61 67 65 72 2d 3e 7a 4a 6f  mcpy(pPager->zJo
132d9 75 72 6e 61 6c 2c 20 7a 50 61 74 68 6e 61 6d 65  urnal, zPathname
132da 2c 20 6e 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20  , nPathname);.  
132db 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72    memcpy(&pPager
132dc 2d 3e 7a 4a 6f 75 72 6e 61 6c 5b 6e 50 61 74 68  ->zJournal[nPath
132dd 6e 61 6d 65 5d 2c 20 22 2d 6a 6f 75 72 6e 61 6c  name], "-journal
132de 22 2c 20 38 29 3b 0a 20 20 20 20 69 66 28 20 70  ", 8);.    if( p
132df 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65  Pager->zFilename
132e0 5b 30 5d 3d 3d 30 20 29 20 70 50 61 67 65 72 2d  [0]==0 ) pPager-
132e1 3e 7a 4a 6f 75 72 6e 61 6c 5b 30 5d 20 3d 20 30  >zJournal[0] = 0
132e2 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
132e3 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20  ee(zPathname);. 
132e4 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 70 56 66   }.  pPager->pVf
132e5 73 20 3d 20 70 56 66 73 3b 0a 20 20 70 50 61 67  s = pVfs;.  pPag
132e6 65 72 2d 3e 76 66 73 46 6c 61 67 73 20 3d 20 76  er->vfsFlags = v
132e7 66 73 46 6c 61 67 73 3b 0a 0a 20 20 2f 2a 20 4f  fsFlags;..  /* O
132e8 70 65 6e 20 74 68 65 20 70 61 67 65 72 20 66 69  pen the pager fi
132e9 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a  le..  */.  if( z
132ea 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69 6c  Filename && zFil
132eb 65 6e 61 6d 65 5b 30 5d 20 26 26 20 21 6d 65 6d  ename[0] && !mem
132ec 44 62 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 6f  Db ){.    int fo
132ed 75 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ut = 0;         
132ee 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 46             /* VF
132ef 53 20 66 6c 61 67 73 20 72 65 74 75 72 6e 65 64  S flags returned
132f0 20 62 79 20 78 4f 70 65 6e 28 29 20 2a 2f 0a 20   by xOpen() */. 
132f1 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
132f2 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67  sOpen(pVfs, pPag
132f3 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 70  er->zFilename, p
132f4 50 61 67 65 72 2d 3e 66 64 2c 20 76 66 73 46 6c  Pager->fd, vfsFl
132f5 61 67 73 2c 20 26 66 6f 75 74 29 3b 0a 20 20 20  ags, &fout);.   
132f6 20 72 65 61 64 4f 6e 6c 79 20 3d 20 28 66 6f 75   readOnly = (fou
132f7 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  t&SQLITE_OPEN_RE
132f8 41 44 4f 4e 4c 59 29 3b 0a 0a 20 20 20 20 2f 2a  ADONLY);..    /*
132f9 20 49 66 20 74 68 65 20 66 69 6c 65 20 77 61 73   If the file was
132fa 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70   successfully op
132fb 65 6e 65 64 20 66 6f 72 20 72 65 61 64 2f 77 72  ened for read/wr
132fc 69 74 65 20 61 63 63 65 73 73 2c 0a 20 20 20 20  ite access,.    
132fd 2a 2a 20 63 68 6f 6f 73 65 20 61 20 64 65 66 61  ** choose a defa
132fe 75 6c 74 20 70 61 67 65 20 73 69 7a 65 20 69 6e  ult page size in
132ff 20 63 61 73 65 20 77 65 20 68 61 76 65 20 74 6f   case we have to
13300 20 63 72 65 61 74 65 20 74 68 65 0a 20 20 20 20   create the.    
13301 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
13302 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20 70 61  . The default pa
13303 67 65 20 73 69 7a 65 20 69 73 20 74 68 65 20 6d  ge size is the m
13304 61 78 69 6d 75 6d 20 6f 66 3a 0a 20 20 20 20 2a  aximum of:.    *
13305 2a 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 53 51  *.    **    + SQ
13306 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47  LITE_DEFAULT_PAG
13307 45 5f 53 49 5a 45 2c 0a 20 20 20 20 2a 2a 20 20  E_SIZE,.    **  
13308 20 20 2b 20 54 68 65 20 76 61 6c 75 65 20 72 65    + The value re
13309 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65  turned by sqlite
1330a 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a  3OsSectorSize().
1330b 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20      **    + The 
1330c 6c 61 72 67 65 73 74 20 70 61 67 65 20 73 69 7a  largest page siz
1330d 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 77 72  e that can be wr
1330e 69 74 74 65 6e 20 61 74 6f 6d 69 63 61 6c 6c 79  itten atomically
1330f 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
13310 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
13311 26 20 21 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20  & !readOnly ){. 
13312 20 20 20 20 20 73 65 74 53 65 63 74 6f 72 53 69       setSectorSi
13313 7a 65 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  ze(pPager);.    
13314 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f    assert(SQLITE_
13315 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a  DEFAULT_PAGE_SIZ
13316 45 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45  E<=SQLITE_MAX_DE
13317 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 29  FAULT_PAGE_SIZE)
13318 3b 0a 20 20 20 20 20 20 69 66 28 20 73 7a 50 61  ;.      if( szPa
13319 67 65 44 66 6c 74 3c 70 50 61 67 65 72 2d 3e 73  geDflt<pPager->s
1331a 65 63 74 6f 72 53 69 7a 65 20 29 7b 0a 20 20 20  ectorSize ){.   
1331b 20 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d       if( pPager-
1331c 3e 73 65 63 74 6f 72 53 69 7a 65 3e 53 51 4c 49  >sectorSize>SQLI
1331d 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50  TE_MAX_DEFAULT_P
1331e 41 47 45 5f 53 49 5a 45 20 29 7b 0a 20 20 20 20  AGE_SIZE ){.    
1331f 20 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74        szPageDflt
13320 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45   = SQLITE_MAX_DE
13321 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b  FAULT_PAGE_SIZE;
13322 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
13323 20 20 20 20 20 20 20 20 20 20 73 7a 50 61 67 65            szPage
13324 44 66 6c 74 20 3d 20 28 75 31 36 29 70 50 61 67  Dflt = (u16)pPag
13325 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 3b 0a  er->sectorSize;.
13326 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
13327 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  }.#ifdef SQLITE_
13328 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52  ENABLE_ATOMIC_WR
13329 49 54 45 0a 20 20 20 20 20 20 7b 0a 20 20 20 20  ITE.      {.    
1332a 20 20 20 20 69 6e 74 20 69 44 63 20 3d 20 73 71      int iDc = sq
1332b 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61  lite3OsDeviceCha
1332c 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61  racteristics(pPa
1332d 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 20 20  ger->fd);.      
1332e 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20    int ii;.      
1332f 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f    assert(SQLITE_
13330 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d  IOCAP_ATOMIC512=
13331 3d 28 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20  =(512>>8));.    
13332 20 20 20 20 61 73 73 65 72 74 28 53 51 4c 49 54      assert(SQLIT
13333 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34  E_IOCAP_ATOMIC64
13334 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29 3b 0a  K==(65536>>8));.
13335 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 53          assert(S
13336 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46 41 55 4c  QLITE_MAX_DEFAUL
13337 54 5f 50 41 47 45 5f 53 49 5a 45 3c 3d 36 35 35  T_PAGE_SIZE<=655
13338 33 36 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  36);.        for
13339 28 69 69 3d 73 7a 50 61 67 65 44 66 6c 74 3b 20  (ii=szPageDflt; 
1333a 69 69 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 44  ii<=SQLITE_MAX_D
1333b 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
1333c 3b 20 69 69 3d 69 69 2a 32 29 7b 0a 20 20 20 20  ; ii=ii*2){.    
1333d 20 20 20 20 20 20 69 66 28 20 69 44 63 26 28 53        if( iDc&(S
1333e 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
1333f 49 43 7c 28 69 69 3e 3e 38 29 29 20 29 7b 0a 20  IC|(ii>>8)) ){. 
13340 20 20 20 20 20 20 20 20 20 20 20 73 7a 50 61 67             szPag
13341 65 44 66 6c 74 20 3d 20 69 69 3b 0a 20 20 20 20  eDflt = ii;.    
13342 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
13343 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  }.      }.#endif
13344 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
13345 20 20 20 20 2f 2a 20 49 66 20 61 20 74 65 6d 70      /* If a temp
13346 6f 72 61 72 79 20 66 69 6c 65 20 69 73 20 72 65  orary file is re
13347 71 75 65 73 74 65 64 2c 20 69 74 20 69 73 20 6e  quested, it is n
13348 6f 74 20 6f 70 65 6e 65 64 20 69 6d 6d 65 64 69  ot opened immedi
13349 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 49 6e  ately..    ** In
1334a 20 74 68 69 73 20 63 61 73 65 20 77 65 20 61 63   this case we ac
1334b 63 65 70 74 20 74 68 65 20 64 65 66 61 75 6c 74  cept the default
1334c 20 70 61 67 65 20 73 69 7a 65 20 61 6e 64 20 64   page size and d
1334d 65 6c 61 79 20 61 63 74 75 61 6c 6c 79 0a 20 20  elay actually.  
1334e 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 74 68 65    ** opening the
1334f 20 66 69 6c 65 20 75 6e 74 69 6c 20 74 68 65 20   file until the 
13350 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 20 4f 73  first call to Os
13351 57 72 69 74 65 28 29 2e 0a 20 20 20 20 2a 2a 0a  Write()..    **.
13352 20 20 20 20 2a 2a 20 54 68 69 73 20 62 72 61 6e      ** This bran
13353 63 68 20 69 73 20 61 6c 73 6f 20 72 75 6e 20 66  ch is also run f
13354 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  or an in-memory 
13355 64 61 74 61 62 61 73 65 2e 20 41 6e 20 69 6e 2d  database. An in-
13356 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 64 61  memory.    ** da
13357 74 61 62 61 73 65 20 69 73 20 74 68 65 20 73 61  tabase is the sa
13358 6d 65 20 61 73 20 61 20 74 65 6d 70 2d 66 69 6c  me as a temp-fil
13359 65 20 74 68 61 74 20 69 73 20 6e 65 76 65 72 20  e that is never 
1335a 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 0a 20  written out to. 
1335b 20 20 20 2a 2a 20 64 69 73 6b 20 61 6e 64 20 75     ** disk and u
1335c 73 65 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  ses an in-memory
1335d 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61   rollback journa
1335e 6c 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20 74  l..    */ .    t
1335f 65 6d 70 46 69 6c 65 20 3d 20 31 3b 0a 20 20 20  empFile = 1;.   
13360 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20 3d   pPager->state =
13361 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45   PAGER_EXCLUSIVE
13362 3b 0a 20 20 20 20 72 65 61 64 4f 6e 6c 79 20 3d  ;.    readOnly =
13363 20 28 76 66 73 46 6c 61 67 73 26 53 51 4c 49 54   (vfsFlags&SQLIT
13364 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29  E_OPEN_READONLY)
13365 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  ;.  }..  /* The 
13366 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 74  following call t
13367 6f 20 50 61 67 65 72 53 65 74 50 61 67 65 73 69  o PagerSetPagesi
13368 7a 65 28 29 20 73 65 72 76 65 73 20 74 6f 20 73  ze() serves to s
13369 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  et the value of 
1336a 0a 20 20 2a 2a 20 50 61 67 65 72 2e 70 61 67 65  .  ** Pager.page
1336b 53 69 7a 65 20 61 6e 64 20 74 6f 20 61 6c 6c 6f  Size and to allo
1336c 63 61 74 65 20 74 68 65 20 50 61 67 65 72 2e 70  cate the Pager.p
1336d 54 6d 70 53 70 61 63 65 20 62 75 66 66 65 72 2e  TmpSpace buffer.
1336e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d  .  */.  if( rc==
1336f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
13370 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
13371 3e 6d 65 6d 44 62 3d 3d 30 20 29 3b 0a 20 20 20  >memDb==0 );.   
13372 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
13373 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70 50  erSetPagesize(pP
13374 61 67 65 72 2c 20 26 73 7a 50 61 67 65 44 66 6c  ager, &szPageDfl
13375 74 2c 20 2d 31 29 3b 0a 20 20 20 20 74 65 73 74  t, -1);.    test
13376 63 61 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45  case( rc!=SQLITE
13377 5f 4f 4b 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  _OK );.  }..  /*
13378 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
13379 75 72 72 65 64 20 69 6e 20 65 69 74 68 65 72 20  urred in either 
1337a 6f 66 20 74 68 65 20 62 6c 6f 63 6b 73 20 61 62  of the blocks ab
1337b 6f 76 65 2c 20 66 72 65 65 20 74 68 65 20 0a 20  ove, free the . 
1337c 20 2a 2a 20 50 61 67 65 72 20 73 74 72 75 63 74   ** Pager struct
1337d 75 72 65 20 61 6e 64 20 63 6c 6f 73 65 20 74 68  ure and close th
1337e 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69  e file..  */.  i
1337f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
13380 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
13381 21 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61  !pPager->pTmpSpa
13382 63 65 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ce );.    sqlite
13383 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d  3OsClose(pPager-
13384 3e 66 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  >fd);.    sqlite
13385 33 5f 66 72 65 65 28 70 50 61 67 65 72 29 3b 0a  3_free(pPager);.
13386 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
13387 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c   }..  /* Initial
13388 69 7a 65 20 74 68 65 20 50 43 61 63 68 65 20 6f  ize the PCache o
13389 62 6a 65 63 74 2e 20 2a 2f 0a 20 20 61 73 73 65  bject. */.  asse
1338a 72 74 28 20 6e 45 78 74 72 61 3c 31 30 30 30 20  rt( nExtra<1000 
1338b 29 3b 0a 20 20 6e 45 78 74 72 61 20 3d 20 52 4f  );.  nExtra = RO
1338c 55 4e 44 38 28 6e 45 78 74 72 61 29 3b 0a 20 20  UND8(nExtra);.  
1338d 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65  sqlite3PcacheOpe
1338e 6e 28 73 7a 50 61 67 65 44 66 6c 74 2c 20 6e 45  n(szPageDflt, nE
1338f 78 74 72 61 2c 20 21 6d 65 6d 44 62 2c 0a 20 20  xtra, !memDb,.  
13390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13391 20 20 21 6d 65 6d 44 62 3f 70 61 67 65 72 53 74    !memDb?pagerSt
13392 72 65 73 73 3a 30 2c 20 28 76 6f 69 64 20 2a 29  ress:0, (void *)
13393 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e  pPager, pPager->
13394 70 50 43 61 63 68 65 29 3b 0a 0a 20 20 50 41 47  pPCache);..  PAG
13395 45 52 54 52 41 43 45 28 28 22 4f 50 45 4e 20 25  ERTRACE(("OPEN %
13396 64 20 25 73 5c 6e 22 2c 20 46 49 4c 45 48 41 4e  d %s\n", FILEHAN
13397 44 4c 45 49 44 28 70 50 61 67 65 72 2d 3e 66 64  DLEID(pPager->fd
13398 29 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65  ), pPager->zFile
13399 6e 61 6d 65 29 29 3b 0a 20 20 49 4f 54 52 41 43  name));.  IOTRAC
1339a 45 28 28 22 4f 50 45 4e 20 25 70 20 25 73 5c 6e  E(("OPEN %p %s\n
1339b 22 2c 20 70 50 61 67 65 72 2c 20 70 50 61 67 65  ", pPager, pPage
1339c 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 29 29 0a 0a  r->zFilename))..
1339d 20 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75    pPager->useJou
1339e 72 6e 61 6c 20 3d 20 28 75 38 29 75 73 65 4a 6f  rnal = (u8)useJo
1339f 75 72 6e 61 6c 3b 0a 20 20 70 50 61 67 65 72 2d  urnal;.  pPager-
133a0 3e 6e 6f 52 65 61 64 6c 6f 63 6b 20 3d 20 28 6e  >noReadlock = (n
133a1 6f 52 65 61 64 6c 6f 63 6b 20 26 26 20 72 65 61  oReadlock && rea
133a2 64 4f 6e 6c 79 29 20 3f 31 3a 30 3b 0a 20 20 2f  dOnly) ?1:0;.  /
133a3 2a 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4f 70  * pPager->stmtOp
133a4 65 6e 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20  en = 0; */.  /* 
133a5 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73  pPager->stmtInUs
133a6 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70  e = 0; */.  /* p
133a7 50 61 67 65 72 2d 3e 6e 52 65 66 20 3d 20 30 3b  Pager->nRef = 0;
133a8 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62   */.  pPager->db
133a9 53 69 7a 65 56 61 6c 69 64 20 3d 20 28 75 38 29  SizeValid = (u8)
133aa 6d 65 6d 44 62 3b 0a 20 20 2f 2a 20 70 50 61 67  memDb;.  /* pPag
133ab 65 72 2d 3e 73 74 6d 74 53 69 7a 65 20 3d 20 30  er->stmtSize = 0
133ac 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72  ; */.  /* pPager
133ad 2d 3e 73 74 6d 74 4a 53 69 7a 65 20 3d 20 30 3b  ->stmtJSize = 0;
133ae 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d   */.  /* pPager-
133af 3e 6e 50 61 67 65 20 3d 20 30 3b 20 2a 2f 0a 20  >nPage = 0; */. 
133b0 20 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20   pPager->mxPgno 
133b1 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47  = SQLITE_MAX_PAG
133b2 45 5f 43 4f 55 4e 54 3b 0a 20 20 2f 2a 20 70 50  E_COUNT;.  /* pP
133b3 61 67 65 72 2d 3e 73 74 61 74 65 20 3d 20 50 41  ager->state = PA
133b4 47 45 52 5f 55 4e 4c 4f 43 4b 3b 20 2a 2f 0a 20  GER_UNLOCK; */. 
133b5 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
133b6 3e 73 74 61 74 65 20 3d 3d 20 28 74 65 6d 70 46  >state == (tempF
133b7 69 6c 65 20 3f 20 50 41 47 45 52 5f 45 58 43 4c  ile ? PAGER_EXCL
133b8 55 53 49 56 45 20 3a 20 50 41 47 45 52 5f 55 4e  USIVE : PAGER_UN
133b9 4c 4f 43 4b 29 20 29 3b 0a 20 20 2f 2a 20 70 50  LOCK) );.  /* pP
133ba 61 67 65 72 2d 3e 65 72 72 4d 61 73 6b 20 3d 20  ager->errMask = 
133bb 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e  0; */.  pPager->
133bc 74 65 6d 70 46 69 6c 65 20 3d 20 28 75 38 29 74  tempFile = (u8)t
133bd 65 6d 70 46 69 6c 65 3b 0a 20 20 61 73 73 65 72  empFile;.  asser
133be 74 28 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47  t( tempFile==PAG
133bf 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e  ER_LOCKINGMODE_N
133c0 4f 52 4d 41 4c 20 0a 20 20 20 20 20 20 20 20 20  ORMAL .         
133c1 20 7c 7c 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41   || tempFile==PA
133c2 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
133c3 45 58 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 61  EXCLUSIVE );.  a
133c4 73 73 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43  ssert( PAGER_LOC
133c5 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49  KINGMODE_EXCLUSI
133c6 56 45 3d 3d 31 20 29 3b 0a 20 20 70 50 61 67 65  VE==1 );.  pPage
133c7 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  r->exclusiveMode
133c8 20 3d 20 28 75 38 29 74 65 6d 70 46 69 6c 65 3b   = (u8)tempFile;
133c9 20 0a 20 20 70 50 61 67 65 72 2d 3e 63 68 61 6e   .  pPager->chan
133ca 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 70 50  geCountDone = pP
133cb 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a  ager->tempFile;.
133cc 20 20 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20    pPager->memDb 
133cd 3d 20 28 75 38 29 6d 65 6d 44 62 3b 0a 20 20 70  = (u8)memDb;.  p
133ce 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 20  Pager->readOnly 
133cf 3d 20 28 75 38 29 72 65 61 64 4f 6e 6c 79 3b 0a  = (u8)readOnly;.
133d0 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 65 65    /* pPager->nee
133d1 64 53 79 6e 63 20 3d 20 30 3b 20 2a 2f 0a 20 20  dSync = 0; */.  
133d2 61 73 73 65 72 74 28 20 75 73 65 4a 6f 75 72 6e  assert( useJourn
133d3 61 6c 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74 65  al || pPager->te
133d4 6d 70 46 69 6c 65 20 29 3b 0a 20 20 70 50 61 67  mpFile );.  pPag
133d5 65 72 2d 3e 6e 6f 53 79 6e 63 20 3d 20 70 50 61  er->noSync = pPa
133d6 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20  ger->tempFile;. 
133d7 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e   pPager->fullSyn
133d8 63 20 3d 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79  c = pPager->noSy
133d9 6e 63 20 3f 30 3a 31 3b 0a 20 20 70 50 61 67 65  nc ?0:1;.  pPage
133da 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 20 3d 20  r->sync_flags = 
133db 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d  SQLITE_SYNC_NORM
133dc 41 4c 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d  AL;.  /* pPager-
133dd 3e 70 46 69 72 73 74 20 3d 20 30 3b 20 2a 2f 0a  >pFirst = 0; */.
133de 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46 69    /* pPager->pFi
133df 72 73 74 53 79 6e 63 65 64 20 3d 20 30 3b 20 2a  rstSynced = 0; *
133e0 2f 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 70  /.  /* pPager->p
133e1 4c 61 73 74 20 3d 20 30 3b 20 2a 2f 0a 20 20 70  Last = 0; */.  p
133e2 50 61 67 65 72 2d 3e 6e 45 78 74 72 61 20 3d 20  Pager->nExtra = 
133e3 28 75 31 36 29 6e 45 78 74 72 61 3b 0a 20 20 70  (u16)nExtra;.  p
133e4 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 69  Pager->journalSi
133e5 7a 65 4c 69 6d 69 74 20 3d 20 53 51 4c 49 54 45  zeLimit = SQLITE
133e6 5f 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c  _DEFAULT_JOURNAL
133e7 5f 53 49 5a 45 5f 4c 49 4d 49 54 3b 0a 20 20 61  _SIZE_LIMIT;.  a
133e8 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70 50  ssert( isOpen(pP
133e9 61 67 65 72 2d 3e 66 64 29 20 7c 7c 20 74 65 6d  ager->fd) || tem
133ea 70 46 69 6c 65 20 29 3b 0a 20 20 73 65 74 53 65  pFile );.  setSe
133eb 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 29  ctorSize(pPager)
133ec 3b 0a 20 20 69 66 28 20 21 75 73 65 4a 6f 75 72  ;.  if( !useJour
133ed 6e 61 6c 20 29 7b 0a 20 20 20 20 70 50 61 67 65  nal ){.    pPage
133ee 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d  r->journalMode =
133ef 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f   PAGER_JOURNALMO
133f0 44 45 5f 4f 46 46 3b 0a 20 20 7d 65 6c 73 65 20  DE_OFF;.  }else 
133f1 69 66 28 20 6d 65 6d 44 62 20 29 7b 0a 20 20 20  if( memDb ){.   
133f2 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
133f3 4d 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55  Mode = PAGER_JOU
133f4 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 3b  RNALMODE_MEMORY;
133f5 0a 20 20 7d 0a 20 20 2f 2a 20 70 50 61 67 65 72  .  }.  /* pPager
133f6 2d 3e 78 42 75 73 79 48 61 6e 64 6c 65 72 20 3d  ->xBusyHandler =
133f7 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50 61 67   0; */.  /* pPag
133f8 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65 72  er->pBusyHandler
133f9 41 72 67 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50  Arg = 0; */.  pP
133fa 61 67 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 20  ager->xReiniter 
133fb 3d 20 78 52 65 69 6e 69 74 3b 0a 20 20 2f 2a 20  = xReinit;.  /* 
133fc 6d 65 6d 73 65 74 28 70 50 61 67 65 72 2d 3e 61  memset(pPager->a
133fd 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Hash, 0, sizeof(
133fe 70 50 61 67 65 72 2d 3e 61 48 61 73 68 29 29 3b  pPager->aHash));
133ff 20 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20 3d   */.  *ppPager =
13400 20 70 50 61 67 65 72 3b 0a 20 20 72 65 74 75 72   pPager;.  retur
13401 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
13402 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
13403 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
13404 61 66 74 65 72 20 74 72 61 6e 73 69 74 69 6f 6e  after transition
13405 69 6e 67 20 66 72 6f 6d 20 50 41 47 45 52 5f 55  ing from PAGER_U
13406 4e 4c 4f 43 4b 20 74 6f 0a 2a 2a 20 50 41 47 45  NLOCK to.** PAGE
13407 52 5f 53 48 41 52 45 44 20 73 74 61 74 65 2e 20  R_SHARED state. 
13408 49 74 20 74 65 73 74 73 20 69 66 20 74 68 65 72  It tests if ther
13409 65 20 69 73 20 61 20 68 6f 74 20 6a 6f 75 72 6e  e is a hot journ
1340a 61 6c 20 70 72 65 73 65 6e 74 20 69 6e 0a 2a 2a  al present in.**
1340b 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d   the file-system
1340c 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 70   for the given p
1340d 61 67 65 72 2e 20 41 20 68 6f 74 20 6a 6f 75 72  ager. A hot jour
1340e 6e 61 6c 20 69 73 20 6f 6e 65 20 74 68 61 74 20  nal is one that 
1340f 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20  .** needs to be 
13410 70 6c 61 79 65 64 20 62 61 63 6b 2e 20 41 63 63  played back. Acc
13411 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73 20 66  ording to this f
13412 75 6e 63 74 69 6f 6e 2c 20 61 20 68 6f 74 2d 6a  unction, a hot-j
13413 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 20 65  ournal.** file e
13414 78 69 73 74 73 20 69 66 20 74 68 65 20 66 6f 6c  xists if the fol
13415 6c 6f 77 69 6e 67 20 63 72 69 74 65 72 69 61 20  lowing criteria 
13416 61 72 65 20 6d 65 74 3a 0a 2a 2a 0a 2a 2a 20 20  are met:.**.**  
13417 20 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66   * The journal f
13418 69 6c 65 20 65 78 69 73 74 73 20 69 6e 20 74 68  ile exists in th
13419 65 20 66 69 6c 65 20 73 79 73 74 65 6d 2c 20 61  e file system, a
1341a 6e 64 0a 2a 2a 20 20 20 2a 20 4e 6f 20 70 72 6f  nd.**   * No pro
1341b 63 65 73 73 20 68 6f 6c 64 73 20 61 20 52 45 53  cess holds a RES
1341c 45 52 56 45 44 20 6f 72 20 67 72 65 61 74 65 72  ERVED or greater
1341d 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
1341e 61 62 61 73 65 20 66 69 6c 65 2c 20 61 6e 64 0a  abase file, and.
1341f 2a 2a 20 20 20 2a 20 54 68 65 20 64 61 74 61 62  **   * The datab
13420 61 73 65 20 66 69 6c 65 20 69 74 73 65 6c 66 20  ase file itself 
13421 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
13422 30 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2c  0 bytes in size,
13423 20 61 6e 64 0a 2a 2a 20 20 20 2a 20 54 68 65 20   and.**   * The 
13424 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68  first byte of th
13425 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65  e journal file e
13426 78 69 73 74 73 20 61 6e 64 20 69 73 20 6e 6f 74  xists and is not
13427 20 30 78 30 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   0x00..**.** If 
13428 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65  the current size
13429 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1342a 20 66 69 6c 65 20 69 73 20 30 20 62 75 74 20 61   file is 0 but a
1342b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a   journal file.**
1342c 20 65 78 69 73 74 73 2c 20 74 68 61 74 20 69 73   exists, that is
1342d 20 70 72 6f 62 61 62 6c 79 20 61 6e 20 6f 6c 64   probably an old
1342e 20 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20 6f 76   journal left ov
1342f 65 72 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 0a  er from a prior.
13430 2a 2a 20 64 61 74 61 62 61 73 65 20 77 69 74 68  ** database with
13431 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 20   the same name. 
13432 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
13433 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
13434 0a 2a 2a 20 6a 75 73 74 20 64 65 6c 65 74 65 64  .** just deleted
13435 20 75 73 69 6e 67 20 4f 73 44 65 6c 65 74 65 2c   using OsDelete,
13436 20 2a 70 45 78 69 73 74 73 20 69 73 20 73 65 74   *pExists is set
13437 20 74 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45   to 0 and SQLITE
13438 5f 4f 4b 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e  _OK.** is return
13439 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ed..**.** This r
1343a 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20  outine does not 
1343b 63 68 65 63 6b 20 69 66 20 74 68 65 72 65 20 69  check if there i
1343c 73 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  s a master journ
1343d 61 6c 20 66 69 6c 65 6e 61 6d 65 0a 2a 2a 20 61  al filename.** a
1343e 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
1343f 20 66 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20   file. If there 
13440 69 73 2c 20 61 6e 64 20 74 68 61 74 20 6d 61 73  is, and that mas
13441 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
13442 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  .** does not exi
13443 73 74 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75  st, then the jou
13444 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e 6f 74  rnal file is not
13445 20 72 65 61 6c 6c 79 20 68 6f 74 2e 20 49 6e 20   really hot. In 
13446 74 68 69 73 0a 2a 2a 20 63 61 73 65 20 74 68 69  this.** case thi
13447 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72  s routine will r
13448 65 74 75 72 6e 20 61 20 66 61 6c 73 65 2d 70 6f  eturn a false-po
13449 73 69 74 69 76 65 2e 20 54 68 65 20 70 61 67 65  sitive. The page
1344a 72 5f 70 6c 61 79 62 61 63 6b 28 29 0a 2a 2a 20  r_playback().** 
1344b 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 64 69 73  routine will dis
1344c 63 6f 76 65 72 20 74 68 61 74 20 74 68 65 20 6a  cover that the j
1344d 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6e  ournal file is n
1344e 6f 74 20 72 65 61 6c 6c 79 20 68 6f 74 20 61 6e  ot really hot an
1344f 64 20 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72  d .** will not r
13450 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 0a 2a 2a  oll it back. .**
13451 0a 2a 2a 20 49 66 20 61 20 68 6f 74 2d 6a 6f 75  .** If a hot-jou
13452 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 66 6f 75  rnal file is fou
13453 6e 64 20 74 6f 20 65 78 69 73 74 2c 20 2a 70 45  nd to exist, *pE
13454 78 69 73 74 73 20 69 73 20 73 65 74 20 74 6f 20  xists is set to 
13455 31 20 61 6e 64 20 0a 2a 2a 20 53 51 4c 49 54 45  1 and .** SQLITE
13456 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 49 66  _OK returned. If
13457 20 6e 6f 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20   no hot-journal 
13458 66 69 6c 65 20 69 73 20 70 72 65 73 65 6e 74 2c  file is present,
13459 20 2a 70 45 78 69 73 74 73 20 69 73 0a 2a 2a 20   *pExists is.** 
1345a 73 65 74 20 74 6f 20 30 20 61 6e 64 20 53 51 4c  set to 0 and SQL
1345b 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64 2e  ITE_OK returned.
1345c 20 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20   If an IO error 
1345d 6f 63 63 75 72 73 20 77 68 69 6c 65 20 74 72 79  occurs while try
1345e 69 6e 67 0a 2a 2a 20 74 6f 20 64 65 74 65 72 6d  ing.** to determ
1345f 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e  ine whether or n
13460 6f 74 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c  ot a hot-journal
13461 20 66 69 6c 65 20 65 78 69 73 74 73 2c 20 74 68   file exists, th
13462 65 20 49 4f 20 65 72 72 6f 72 0a 2a 2a 20 63 6f  e IO error.** co
13463 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  de is returned a
13464 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  nd the value of 
13465 2a 70 45 78 69 73 74 73 20 69 73 20 75 6e 64 65  *pExists is unde
13466 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  fined..*/.static
13467 20 69 6e 74 20 68 61 73 48 6f 74 4a 6f 75 72 6e   int hasHotJourn
13468 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  al(Pager *pPager
13469 2c 20 69 6e 74 20 2a 70 45 78 69 73 74 73 29 7b  , int *pExists){
1346a 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
1346b 20 63 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50   const pVfs = pP
1346c 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 6e  ager->pVfs;.  in
1346d 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
1346e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
1346f 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
13470 69 6e 74 20 65 78 69 73 74 73 3b 20 20 20 20 20  int exists;     
13471 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
13472 20 54 72 75 65 20 69 66 20 61 20 6a 6f 75 72 6e   True if a journ
13473 61 6c 20 66 69 6c 65 20 69 73 20 70 72 65 73 65  al file is prese
13474 6e 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  nt */..  assert(
13475 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20   pPager!=0 );.  
13476 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
13477 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20  useJournal );.  
13478 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70  assert( isOpen(p
13479 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a 20 20  Pager->fd) );.  
1347a 61 73 73 65 72 74 28 20 21 69 73 4f 70 65 6e 28  assert( !isOpen(
1347b 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a  pPager->jfd) );.
1347c 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
1347d 2d 3e 73 74 61 74 65 20 3c 3d 20 50 41 47 45 52  ->state <= PAGER
1347e 5f 53 48 41 52 45 44 20 29 3b 0a 0a 20 20 2a 70  _SHARED );..  *p
1347f 45 78 69 73 74 73 20 3d 20 30 3b 0a 20 20 72 63  Exists = 0;.  rc
13480 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65   = sqlite3OsAcce
13481 73 73 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d  ss(pVfs, pPager-
13482 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 53 51 4c 49 54  >zJournal, SQLIT
13483 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c  E_ACCESS_EXISTS,
13484 20 26 65 78 69 73 74 73 29 3b 0a 20 20 69 66 28   &exists);.  if(
13485 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
13486 26 20 65 78 69 73 74 73 20 29 7b 0a 20 20 20 20  & exists ){.    
13487 69 6e 74 20 6c 6f 63 6b 65 64 3b 20 20 20 20 20  int locked;     
13488 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
13489 72 75 65 20 69 66 20 73 6f 6d 65 20 70 72 6f 63  rue if some proc
1348a 65 73 73 20 68 6f 6c 64 73 20 61 20 52 45 53 45  ess holds a RESE
1348b 52 56 45 44 20 6c 6f 63 6b 20 2a 2f 0a 0a 20 20  RVED lock */..  
1348c 20 20 2f 2a 20 52 61 63 65 20 63 6f 6e 64 69 74    /* Race condit
1348d 69 6f 6e 20 68 65 72 65 3a 20 20 41 6e 6f 74 68  ion here:  Anoth
1348e 65 72 20 70 72 6f 63 65 73 73 20 6d 69 67 68 74  er process might
1348f 20 68 61 76 65 20 62 65 65 6e 20 68 6f 6c 64 69   have been holdi
13490 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 74 68  ng the.    ** th
13491 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  e RESERVED lock 
13492 61 6e 64 20 68 61 76 65 20 61 20 6a 6f 75 72 6e  and have a journ
13493 61 6c 20 6f 70 65 6e 20 61 74 20 74 68 65 20 73  al open at the s
13494 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 29  qlite3OsAccess()
13495 20 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 61 62   .    ** call ab
13496 6f 76 65 2c 20 62 75 74 20 74 68 65 6e 20 64 65  ove, but then de
13497 6c 65 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c  lete the journal
13498 20 61 6e 64 20 64 72 6f 70 20 74 68 65 20 6c 6f   and drop the lo
13499 63 6b 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a  ck before.    **
1349a 20 77 65 20 67 65 74 20 74 6f 20 74 68 65 20 66   we get to the f
1349b 6f 6c 6c 6f 77 69 6e 67 20 73 71 6c 69 74 65 33  ollowing sqlite3
1349c 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  OsCheckReservedL
1349d 6f 63 6b 28 29 20 63 61 6c 6c 2e 20 20 49 66 20  ock() call.  If 
1349e 74 68 61 74 0a 20 20 20 20 2a 2a 20 69 73 20 74  that.    ** is t
1349f 68 65 20 63 61 73 65 2c 20 74 68 69 73 20 72 6f  he case, this ro
134a0 75 74 69 6e 65 20 6d 69 67 68 74 20 74 68 69 6e  utine might thin
134a1 6b 20 74 68 65 72 65 20 69 73 20 61 20 68 6f 74  k there is a hot
134a2 20 6a 6f 75 72 6e 61 6c 20 77 68 65 6e 0a 20 20   journal when.  
134a3 20 20 2a 2a 20 69 6e 20 66 61 63 74 20 74 68 65    ** in fact the
134a4 72 65 20 69 73 20 6e 6f 6e 65 2e 20 20 54 68 69  re is none.  Thi
134a5 73 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 66  s results in a f
134a6 61 6c 73 65 2d 70 6f 73 69 74 69 76 65 20 77 68  alse-positive wh
134a7 69 63 68 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20  ich will.    ** 
134a8 62 65 20 64 65 61 6c 74 20 77 69 74 68 20 62 79  be dealt with by
134a9 20 74 68 65 20 70 6c 61 79 62 61 63 6b 20 72 6f   the playback ro
134aa 75 74 69 6e 65 2e 20 20 54 69 63 6b 65 74 20 23  utine.  Ticket #
134ab 33 38 38 33 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  3883..    */.   
134ac 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 43   rc = sqlite3OsC
134ad 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
134ae 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 6c 6f  (pPager->fd, &lo
134af 63 6b 65 64 29 3b 0a 20 20 20 20 69 66 28 20 72  cked);.    if( r
134b0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
134b1 21 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 20  !locked ){.     
134b2 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20 20 20   int nPage;..   
134b3 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 65 20     /* Check the 
134b4 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61  size of the data
134b5 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20 69 74  base file. If it
134b6 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 30 20 70   consists of 0 p
134b7 61 67 65 73 2c 0a 20 20 20 20 20 20 2a 2a 20 74  ages,.      ** t
134b8 68 65 6e 20 64 65 6c 65 74 65 20 74 68 65 20 6a  hen delete the j
134b9 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 53 65 65  ournal file. See
134ba 20 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d 6d   the header comm
134bb 65 6e 74 20 61 62 6f 76 65 20 66 6f 72 20 0a 20  ent above for . 
134bc 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65 61 73       ** the reas
134bd 6f 6e 69 6e 67 20 68 65 72 65 2e 20 20 44 65 6c  oning here.  Del
134be 65 74 65 20 74 68 65 20 6f 62 73 6f 6c 65 74 65  ete the obsolete
134bf 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 75 6e   journal file un
134c0 64 65 72 0a 20 20 20 20 20 20 2a 2a 20 61 20 52  der.      ** a R
134c1 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 74 6f 20  ESERVED lock to 
134c2 61 76 6f 69 64 20 72 61 63 65 20 63 6f 6e 64 69  avoid race condi
134c3 74 69 6f 6e 73 20 61 6e 64 20 74 6f 20 61 76 6f  tions and to avo
134c4 69 64 20 76 69 6f 6c 61 74 69 6e 67 0a 20 20 20  id violating.   
134c5 20 20 20 2a 2a 20 5b 48 33 33 30 32 30 5d 2e 0a     ** [H33020]..
134c6 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72        */.      r
134c7 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
134c8 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72  Pagecount(pPager
134c9 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 20  , &nPage);.     
134ca 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
134cb 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  OK ){.        if
134cc 28 20 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20  ( nPage==0 ){.  
134cd 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42          sqlite3B
134ce 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  eginBenignMalloc
134cf 28 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  ();.          if
134d0 28 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28  ( sqlite3OsLock(
134d1 70 50 61 67 65 72 2d 3e 66 64 2c 20 52 45 53 45  pPager->fd, RESE
134d2 52 56 45 44 5f 4c 4f 43 4b 29 3d 3d 53 51 4c 49  RVED_LOCK)==SQLI
134d3 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
134d4 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65       sqlite3OsDe
134d5 6c 65 74 65 28 70 56 66 73 2c 20 70 50 61 67 65  lete(pVfs, pPage
134d6 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 30 29 3b  r->zJournal, 0);
134d7 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
134d8 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 70 50 61  ite3OsUnlock(pPa
134d9 67 65 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f  ger->fd, SHARED_
134da 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 20  LOCK);.         
134db 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c   }.          sql
134dc 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
134dd 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 20 20 7d  loc();.        }
134de 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
134df 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66  /* The journal f
134e0 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64 20 6e  ile exists and n
134e1 6f 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69  o other connecti
134e2 6f 6e 20 68 61 73 20 61 20 72 65 73 65 72 76 65  on has a reserve
134e3 64 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f  d.          ** o
134e4 72 20 67 72 65 61 74 65 72 20 6c 6f 63 6b 20 6f  r greater lock o
134e5 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
134e6 69 6c 65 2e 20 4e 6f 77 20 63 68 65 63 6b 20 74  ile. Now check t
134e7 68 61 74 20 74 68 65 72 65 20 69 73 0a 20 20 20  hat there is.   
134e8 20 20 20 20 20 20 20 2a 2a 20 61 74 20 6c 65 61         ** at lea
134e9 73 74 20 6f 6e 65 20 6e 6f 6e 2d 7a 65 72 6f 20  st one non-zero 
134ea 62 79 74 65 73 20 61 74 20 74 68 65 20 73 74 61  bytes at the sta
134eb 72 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  rt of the journa
134ec 6c 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 20 20  l file..        
134ed 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73    ** If there is
134ee 2c 20 74 68 65 6e 20 77 65 20 63 6f 6e 73 69 64  , then we consid
134ef 65 72 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20  er this journal 
134f0 74 6f 20 62 65 20 68 6f 74 2e 20 49 66 20 6e 6f  to be hot. If no
134f1 74 2c 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  t, .          **
134f2 20 69 74 20 63 61 6e 20 62 65 20 69 67 6e 6f 72   it can be ignor
134f3 65 64 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  ed..          */
134f4 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 66  .          int f
134f5 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   = SQLITE_OPEN_R
134f6 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f 4f  EADONLY|SQLITE_O
134f7 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c  PEN_MAIN_JOURNAL
134f8 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  ;.          rc =
134f9 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70   sqlite3OsOpen(p
134fa 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f  Vfs, pPager->zJo
134fb 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d 3e 6a  urnal, pPager->j
134fc 66 64 2c 20 66 2c 20 26 66 29 3b 0a 20 20 20 20  fd, f, &f);.    
134fd 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
134fe 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
134ff 20 20 20 20 20 20 20 75 38 20 66 69 72 73 74 20         u8 first 
13500 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 0;.           
13501 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52   rc = sqlite3OsR
13502 65 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ead(pPager->jfd,
13503 20 28 76 6f 69 64 20 2a 29 26 66 69 72 73 74 2c   (void *)&first,
13504 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   1, 0);.        
13505 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
13506 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52  TE_IOERR_SHORT_R
13507 45 41 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20  EAD ){.         
13508 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
13509 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  _OK;.           
1350a 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 73   }.            s
1350b 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50  qlite3OsClose(pP
1350c 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20  ager->jfd);.    
1350d 20 20 20 20 20 20 20 20 2a 70 45 78 69 73 74 73          *pExists
1350e 20 3d 20 28 66 69 72 73 74 21 3d 30 29 3b 0a 20   = (first!=0);. 
1350f 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69           }else i
13510 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43 41  f( rc==SQLITE_CA
13511 4e 54 4f 50 45 4e 20 29 7b 0a 20 20 20 20 20 20  NTOPEN ){.      
13512 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20 63        /* If we c
13513 61 6e 6e 6f 74 20 6f 70 65 6e 20 74 68 65 20 72  annot open the r
13514 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
13515 66 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 74 6f  file in order to
13516 20 73 65 65 20 69 66 0a 20 20 20 20 20 20 20 20   see if.        
13517 20 20 20 20 2a 2a 20 69 74 73 20 68 61 73 20 61      ** its has a
13518 20 7a 65 72 6f 20 68 65 61 64 65 72 2c 20 74 68   zero header, th
13519 61 74 20 6d 69 67 68 74 20 62 65 20 64 75 65 20  at might be due 
1351a 74 6f 20 61 6e 20 49 2f 4f 20 65 72 72 6f 72 2c  to an I/O error,
1351b 20 6f 72 0a 20 20 20 20 20 20 20 20 20 20 20 20   or.            
1351c 2a 2a 20 69 74 20 6d 69 67 68 74 20 62 65 20 64  ** it might be d
1351d 75 65 20 74 6f 20 74 68 65 20 72 61 63 65 20 63  ue to the race c
1351e 6f 6e 64 69 74 69 6f 6e 20 64 65 73 63 72 69 62  ondition describ
1351f 65 64 20 61 62 6f 76 65 20 61 6e 64 20 69 6e 0a  ed above and in.
13520 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74              ** t
13521 69 63 6b 65 74 20 23 33 38 38 33 2e 20 20 45 69  icket #3883.  Ei
13522 74 68 65 72 20 77 61 79 2c 20 61 73 73 75 6d 65  ther way, assume
13523 20 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61   that the journa
13524 6c 20 69 73 20 68 6f 74 2e 0a 20 20 20 20 20 20  l is hot..      
13525 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 6d 69        ** This mi
13526 67 68 74 20 62 65 20 61 20 66 61 6c 73 65 20 70  ght be a false p
13527 6f 73 69 74 69 76 65 2e 20 20 42 75 74 20 69 66  ositive.  But if
13528 20 69 74 20 69 73 2c 20 74 68 65 6e 20 74 68 65   it is, then the
13529 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
1352a 61 75 74 6f 6d 61 74 69 63 20 6a 6f 75 72 6e 61  automatic journa
1352b 6c 20 70 6c 61 79 62 61 63 6b 20 61 6e 64 20 72  l playback and r
1352c 65 63 6f 76 65 72 79 20 6d 65 63 68 61 6e 69 73  ecovery mechanis
1352d 6d 20 77 69 6c 6c 20 64 65 61 6c 0a 20 20 20 20  m will deal.    
1352e 20 20 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20          ** with 
1352f 69 74 20 75 6e 64 65 72 20 61 6e 20 45 58 43 4c  it under an EXCL
13530 55 53 49 56 45 20 6c 6f 63 6b 20 77 68 65 72 65  USIVE lock where
13531 20 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20   we do not need 
13532 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  to.            *
13533 2a 20 77 6f 72 72 79 20 73 6f 20 6d 75 63 68 20  * worry so much 
13534 77 69 74 68 20 72 61 63 65 20 63 6f 6e 64 69 74  with race condit
13535 69 6f 6e 73 2e 0a 20 20 20 20 20 20 20 20 20 20  ions..          
13536 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20    */.           
13537 20 2a 70 45 78 69 73 74 73 20 3d 20 31 3b 0a 20   *pExists = 1;. 
13538 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
13539 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20  SQLITE_OK;.     
1353a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
1353b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1353c 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
1353d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74  .}../*.** Read t
1353e 68 65 20 63 6f 6e 74 65 6e 74 20 66 6f 72 20 70  he content for p
1353f 61 67 65 20 70 50 67 20 6f 75 74 20 6f 66 20 74  age pPg out of t
13540 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
13541 20 61 6e 64 20 69 6e 74 6f 20 0a 2a 2a 20 70 50   and into .** pP
13542 67 2d 3e 70 44 61 74 61 2e 20 41 20 73 68 61 72  g->pData. A shar
13543 65 64 20 6c 6f 63 6b 20 6f 72 20 67 72 65 61 74  ed lock or great
13544 65 72 20 6d 75 73 74 20 62 65 20 68 65 6c 64 20  er must be held 
13545 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  on the database.
13546 2a 2a 20 66 69 6c 65 20 62 65 66 6f 72 65 20 74  ** file before t
13547 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
13548 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  called..**.** If
13549 20 70 61 67 65 20 31 20 69 73 20 72 65 61 64 2c   page 1 is read,
1354a 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20   then the value 
1354b 6f 66 20 50 61 67 65 72 2e 64 62 46 69 6c 65 56  of Pager.dbFileV
1354c 65 72 73 5b 5d 20 69 73 20 73 65 74 20 74 6f 0a  ers[] is set to.
1354d 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 72 65 61  ** the value rea
1354e 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62  d from the datab
1354f 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20  ase file..**.** 
13550 49 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f  If an IO error o
13551 63 63 75 72 73 2c 20 74 68 65 6e 20 74 68 65 20  ccurs, then the 
13552 49 4f 20 65 72 72 6f 72 20 69 73 20 72 65 74 75  IO error is retu
13553 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c  rned to the call
13554 65 72 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  er..** Otherwise
13555 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  , SQLITE_OK is r
13556 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74  eturned..*/.stat
13557 69 63 20 69 6e 74 20 72 65 61 64 44 62 50 61 67  ic int readDbPag
13558 65 28 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20  e(PgHdr *pPg){. 
13559 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d   Pager *pPager =
1355a 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 20 2f 2a   pPg->pPager; /*
1355b 20 50 61 67 65 72 20 6f 62 6a 65 63 74 20 61 73   Pager object as
1355c 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 70 61  sociated with pa
1355d 67 65 20 70 50 67 20 2a 2f 0a 20 20 50 67 6e 6f  ge pPg */.  Pgno
1355e 20 70 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e   pgno = pPg->pgn
1355f 6f 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65  o;       /* Page
13560 20 6e 75 6d 62 65 72 20 74 6f 20 72 65 61 64 20   number to read 
13561 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
13562 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13563 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65    /* Return code
13564 20 2a 2f 0a 20 20 69 36 34 20 69 4f 66 66 73 65   */.  i64 iOffse
13565 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
13566 20 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65     /* Byte offse
13567 74 20 6f 66 20 66 69 6c 65 20 74 6f 20 72 65 61  t of file to rea
13568 64 20 66 72 6f 6d 20 2a 2f 0a 0a 20 20 61 73 73  d from */..  ass
13569 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61  ert( pPager->sta
1356a 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44  te>=PAGER_SHARED
1356b 20 26 26 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20   && !MEMDB );.  
1356c 61 73 73 65 72 74 28 20 69 73 4f 70 65 6e 28 70  assert( isOpen(p
1356d 50 61 67 65 72 2d 3e 66 64 29 20 29 3b 0a 0a 20  Pager->fd) );.. 
1356e 20 69 66 28 20 4e 45 56 45 52 28 21 69 73 4f 70   if( NEVER(!isOp
1356f 65 6e 28 70 50 61 67 65 72 2d 3e 66 64 29 29 20  en(pPager->fd)) 
13570 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
13571 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20  Pager->tempFile 
13572 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50  );.    memset(pP
13573 67 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70 50 61  g->pData, 0, pPa
13574 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a  ger->pageSize);.
13575 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
13576 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 4f 66 66  E_OK;.  }.  iOff
13577 73 65 74 20 3d 20 28 70 67 6e 6f 2d 31 29 2a 28  set = (pgno-1)*(
13578 69 36 34 29 70 50 61 67 65 72 2d 3e 70 61 67 65  i64)pPager->page
13579 53 69 7a 65 3b 0a 20 20 72 63 20 3d 20 73 71 6c  Size;.  rc = sql
1357a 69 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65  ite3OsRead(pPage
1357b 72 2d 3e 66 64 2c 20 70 50 67 2d 3e 70 44 61 74  r->fd, pPg->pDat
1357c 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53  a, pPager->pageS
1357d 69 7a 65 2c 20 69 4f 66 66 73 65 74 29 3b 0a 20  ize, iOffset);. 
1357e 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1357f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44  IOERR_SHORT_READ
13580 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
13581 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66  ITE_OK;.  }.  if
13582 28 20 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20  ( pgno==1 ){.   
13583 20 75 38 20 2a 64 62 46 69 6c 65 56 65 72 73 20   u8 *dbFileVers 
13584 3d 20 26 28 28 75 38 2a 29 70 50 67 2d 3e 70 44  = &((u8*)pPg->pD
13585 61 74 61 29 5b 32 34 5d 3b 0a 20 20 20 20 6d 65  ata)[24];.    me
13586 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64 62  mcpy(&pPager->db
13587 46 69 6c 65 56 65 72 73 2c 20 64 62 46 69 6c 65  FileVers, dbFile
13588 56 65 72 73 2c 20 73 69 7a 65 6f 66 28 70 50 61  Vers, sizeof(pPa
13589 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73 29  ger->dbFileVers)
1358a 29 3b 0a 20 20 7d 0a 20 20 43 4f 44 45 43 31 28  );.  }.  CODEC1(
1358b 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 44 61  pPager, pPg->pDa
1358c 74 61 2c 20 70 67 6e 6f 2c 20 33 2c 20 72 63 20  ta, pgno, 3, rc 
1358d 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b  = SQLITE_NOMEM);
1358e 0a 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 28 73  ..  PAGER_INCR(s
1358f 71 6c 69 74 65 33 5f 70 61 67 65 72 5f 72 65 61  qlite3_pager_rea
13590 64 64 62 5f 63 6f 75 6e 74 29 3b 0a 20 20 50 41  ddb_count);.  PA
13591 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d  GER_INCR(pPager-
13592 3e 6e 52 65 61 64 29 3b 0a 20 20 49 4f 54 52 41  >nRead);.  IOTRA
13593 43 45 28 28 22 50 47 49 4e 20 25 70 20 25 64 5c  CE(("PGIN %p %d\
13594 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f  n", pPager, pgno
13595 29 29 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45  ));.  PAGERTRACE
13596 28 28 22 46 45 54 43 48 20 25 64 20 70 61 67 65  (("FETCH %d page
13597 20 25 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e   %d hash(%08x)\n
13598 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
13599 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72    PAGERID(pPager
1359a 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f 70  ), pgno, pager_p
1359b 61 67 65 68 61 73 68 28 70 50 67 29 29 29 3b 0a  agehash(pPg)));.
1359c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1359d 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
1359e 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74  tion is called t
1359f 6f 20 6f 62 74 61 69 6e 20 61 20 73 68 61 72 65  o obtain a share
135a0 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61  d lock on the da
135a1 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20  tabase file..** 
135a2 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f  It is illegal to
135a3 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67   call sqlite3Pag
135a4 65 72 41 63 71 75 69 72 65 28 29 20 75 6e 74 69  erAcquire() unti
135a5 6c 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e  l after this fun
135a6 63 74 69 6f 6e 0a 2a 2a 20 68 61 73 20 62 65 65  ction.** has bee
135a7 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63  n successfully c
135a8 61 6c 6c 65 64 2e 20 49 66 20 61 20 73 68 61 72  alled. If a shar
135a9 65 64 2d 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61  ed-lock is alrea
135aa 64 79 20 68 65 6c 64 20 77 68 65 6e 0a 2a 2a 20  dy held when.** 
135ab 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
135ac 20 63 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 61   called, it is a
135ad 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 68   no-op..**.** Th
135ae 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 70 65 72  e following oper
135af 61 74 69 6f 6e 73 20 61 72 65 20 61 6c 73 6f 20  ations are also 
135b0 70 65 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69  performed by thi
135b1 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  s function..**.*
135b2 2a 20 20 20 31 29 20 49 66 20 74 68 65 20 70 61  *   1) If the pa
135b3 67 65 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79  ger is currently
135b4 20 69 6e 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b   in PAGER_UNLOCK
135b5 20 73 74 61 74 65 20 28 6e 6f 20 6c 6f 63 6b 20   state (no lock 
135b6 68 65 6c 64 0a 2a 2a 20 20 20 20 20 20 6f 6e 20  held.**      on 
135b7 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
135b8 65 29 2c 20 74 68 65 6e 20 61 6e 20 61 74 74 65  e), then an atte
135b9 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f 20 6f  mpt is made to o
135ba 62 74 61 69 6e 20 61 0a 2a 2a 20 20 20 20 20 20  btain a.**      
135bb 53 48 41 52 45 44 20 6c 6f 63 6b 20 6f 6e 20 74  SHARED lock on t
135bc 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
135bd 2e 20 49 6d 6d 65 64 69 61 74 65 6c 79 20 61 66  . Immediately af
135be 74 65 72 20 6f 62 74 61 69 6e 69 6e 67 0a 2a 2a  ter obtaining.**
135bf 20 20 20 20 20 20 74 68 65 20 53 48 41 52 45 44        the SHARED
135c0 20 6c 6f 63 6b 2c 20 74 68 65 20 66 69 6c 65 2d   lock, the file-
135c1 73 79 73 74 65 6d 20 69 73 20 63 68 65 63 6b 65  system is checke
135c2 64 20 66 6f 72 20 61 20 68 6f 74 2d 6a 6f 75 72  d for a hot-jour
135c3 6e 61 6c 2c 0a 2a 2a 20 20 20 20 20 20 77 68 69  nal,.**      whi
135c4 63 68 20 69 73 20 70 6c 61 79 65 64 20 62 61 63  ch is played bac
135c5 6b 20 69 66 20 70 72 65 73 65 6e 74 2e 20 46 6f  k if present. Fo
135c6 6c 6c 6f 77 69 6e 67 20 61 6e 79 20 68 6f 74 2d  llowing any hot-
135c7 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 20 20 20 20  journal .**     
135c8 20 72 6f 6c 6c 62 61 63 6b 2c 20 74 68 65 20 63   rollback, the c
135c9 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63  ontents of the c
135ca 61 63 68 65 20 61 72 65 20 76 61 6c 69 64 61 74  ache are validat
135cb 65 64 20 62 79 20 63 68 65 63 6b 69 6e 67 0a 2a  ed by checking.*
135cc 2a 20 20 20 20 20 20 74 68 65 20 27 63 68 61 6e  *      the 'chan
135cd 67 65 2d 63 6f 75 6e 74 65 72 27 20 66 69 65 6c  ge-counter' fiel
135ce 64 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  d of the databas
135cf 65 20 66 69 6c 65 20 68 65 61 64 65 72 20 61 6e  e file header an
135d0 64 0a 2a 2a 20 20 20 20 20 20 64 69 73 63 61 72  d.**      discar
135d1 64 65 64 20 69 66 20 74 68 65 79 20 61 72 65 20  ded if they are 
135d2 66 6f 75 6e 64 20 74 6f 20 62 65 20 69 6e 76 61  found to be inva
135d3 6c 69 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20  lid..**.**   2) 
135d4 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20  If the pager is 
135d5 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63 6c 75  running in exclu
135d6 73 69 76 65 2d 6d 6f 64 65 2c 20 61 6e 64 20 74  sive-mode, and t
135d7 68 65 72 65 20 61 72 65 20 63 75 72 72 65 6e 74  here are current
135d8 6c 79 0a 2a 2a 20 20 20 20 20 20 6e 6f 20 6f 75  ly.**      no ou
135d9 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65  tstanding refere
135da 6e 63 65 73 20 74 6f 20 61 6e 79 20 70 61 67 65  nces to any page
135db 73 2c 20 61 6e 64 20 69 73 20 69 6e 20 74 68 65  s, and is in the
135dc 20 65 72 72 6f 72 20 73 74 61 74 65 2c 0a 2a 2a   error state,.**
135dd 20 20 20 20 20 20 74 68 65 6e 20 61 6e 20 61 74        then an at
135de 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74 6f  tempt is made to
135df 20 63 6c 65 61 72 20 74 68 65 20 65 72 72 6f 72   clear the error
135e0 20 73 74 61 74 65 20 62 79 20 64 69 73 63 61 72   state by discar
135e1 64 69 6e 67 0a 2a 2a 20 20 20 20 20 20 74 68 65  ding.**      the
135e2 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
135e3 20 70 61 67 65 20 63 61 63 68 65 20 61 6e 64 20   page cache and 
135e4 72 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 6e 79  rolling back any
135e5 20 6f 70 65 6e 20 6a 6f 75 72 6e 61 6c 0a 2a 2a   open journal.**
135e6 20 20 20 20 20 20 66 69 6c 65 2e 0a 2a 2a 0a 2a        file..**.*
135e7 2a 20 49 66 20 74 68 65 20 6f 70 65 72 61 74 69  * If the operati
135e8 6f 6e 20 64 65 73 63 72 69 62 65 64 20 62 79 20  on described by 
135e9 28 32 29 20 61 62 6f 76 65 20 69 73 20 6e 6f 74  (2) above is not
135ea 20 61 74 74 65 6d 70 74 65 64 2c 20 61 6e 64 20   attempted, and 
135eb 69 66 20 74 68 65 0a 2a 2a 20 70 61 67 65 72 20  if the.** pager 
135ec 69 73 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73  is in an error s
135ed 74 61 74 65 20 6f 74 68 65 72 20 74 68 61 6e 20  tate other than 
135ee 53 51 4c 49 54 45 5f 46 55 4c 4c 20 77 68 65 6e  SQLITE_FULL when
135ef 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c   this is called,
135f0 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 73 74  .** the error st
135f1 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69  ate error code i
135f2 73 20 72 65 74 75 72 6e 65 64 2e 20 49 74 20 69  s returned. It i
135f3 73 20 70 65 72 6d 69 74 74 65 64 20 74 6f 20 72  s permitted to r
135f4 65 61 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62  ead the.** datab
135f5 61 73 65 20 77 68 65 6e 20 69 6e 20 53 51 4c 49  ase when in SQLI
135f6 54 45 5f 46 55 4c 4c 20 65 72 72 6f 72 20 73 74  TE_FULL error st
135f7 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72  ate..**.** Other
135f8 77 69 73 65 2c 20 69 66 20 65 76 65 72 79 74 68  wise, if everyth
135f9 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 66 75  ing is successfu
135fa 6c 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  l, SQLITE_OK is 
135fb 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 0a  returned. If an.
135fc 2a 2a 20 49 4f 20 65 72 72 6f 72 20 6f 63 63 75  ** IO error occu
135fd 72 73 20 77 68 69 6c 65 20 6c 6f 63 6b 69 6e 67  rs while locking
135fe 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 63   the database, c
135ff 68 65 63 6b 69 6e 67 20 66 6f 72 20 61 20 68 6f  hecking for a ho
13600 74 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c  t-journal.** fil
13601 65 20 6f 72 20 72 6f 6c 6c 69 6e 67 20 62 61 63  e or rolling bac
13602 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  k a journal file
13603 2c 20 74 68 65 20 49 4f 20 65 72 72 6f 72 20 63  , the IO error c
13604 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
13605 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
13606 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
13607 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 50 61  gerSharedLock(Pa
13608 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
13609 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1360a 4f 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  OK;             
1360b 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
1360c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 45 72 72  e */.  int isErr
1360d 6f 72 52 65 73 65 74 20 3d 20 30 3b 20 20 20 20  orReset = 0;    
1360e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
1360f 65 20 69 66 20 72 65 63 6f 76 65 72 69 6e 67 20  e if recovering 
13610 66 72 6f 6d 20 65 72 72 6f 72 20 73 74 61 74 65  from error state
13611 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 72   */..  /* This r
13612 6f 75 74 69 6e 65 20 69 73 20 6f 6e 6c 79 20 63  outine is only c
13613 61 6c 6c 65 64 20 66 72 6f 6d 20 62 2d 74 72 65  alled from b-tre
13614 65 20 61 6e 64 20 6f 6e 6c 79 20 77 68 65 6e 20  e and only when 
13615 74 68 65 72 65 20 61 72 65 20 6e 6f 0a 20 20 2a  there are no.  *
13616 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61  * outstanding pa
13617 67 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ges */.  assert(
13618 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65   sqlite3PcacheRe
13619 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70  fCount(pPager->p
1361a 50 43 61 63 68 65 29 3d 3d 30 20 29 3b 0a 20 20  PCache)==0 );.  
1361b 69 66 28 20 4e 45 56 45 52 28 4d 45 4d 44 42 20  if( NEVER(MEMDB 
1361c 26 26 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  && pPager->errCo
1361d 64 65 29 20 29 7b 20 72 65 74 75 72 6e 20 70 50  de) ){ return pP
1361e 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 20 7d  ager->errCode; }
1361f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 64  ..  /* If this d
13620 61 74 61 62 61 73 65 20 69 73 20 69 6e 20 61 6e  atabase is in an
13621 20 65 72 72 6f 72 2d 73 74 61 74 65 2c 20 6e 6f   error-state, no
13622 77 20 69 73 20 61 20 63 68 61 6e 63 65 20 74 6f  w is a chance to
13623 20 63 6c 65 61 72 0a 20 20 2a 2a 20 74 68 65 20   clear.  ** the 
13624 65 72 72 6f 72 2e 20 44 69 73 63 61 72 64 20 74  error. Discard t
13625 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
13626 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20 61  he pager-cache a
13627 6e 64 20 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a  nd rollback.  **
13628 20 61 6e 79 20 68 6f 74 20 6a 6f 75 72 6e 61 6c   any hot journal
13629 20 69 6e 20 74 68 65 20 66 69 6c 65 2d 73 79 73   in the file-sys
1362a 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  tem..  */.  if( 
1362b 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
1362c 29 7b 0a 20 20 20 20 69 66 28 20 69 73 4f 70 65  ){.    if( isOpe
1362d 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c  n(pPager->jfd) |
1362e 7c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e  | pPager->zJourn
1362f 61 6c 20 29 7b 0a 20 20 20 20 20 20 69 73 45 72  al ){.      isEr
13630 72 6f 72 52 65 73 65 74 20 3d 20 31 3b 0a 20 20  rorReset = 1;.  
13631 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e    }.    pPager->
13632 65 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45  errCode = SQLITE
13633 5f 4f 4b 3b 0a 20 20 20 20 70 61 67 65 72 5f 72  _OK;.    pager_r
13634 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20  eset(pPager);.  
13635 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  }..  if( pPager-
13636 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e  >state==PAGER_UN
13637 4c 4f 43 4b 20 7c 7c 20 69 73 45 72 72 6f 72 52  LOCK || isErrorR
13638 65 73 65 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  eset ){.    sqli
13639 74 65 33 5f 76 66 73 20 2a 20 63 6f 6e 73 74 20  te3_vfs * const 
1363a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e 70  pVfs = pPager->p
1363b 56 66 73 3b 0a 20 20 20 20 69 6e 74 20 69 73 48  Vfs;.    int isH
1363c 6f 74 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20  otJournal = 0;. 
1363d 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44     assert( !MEMD
1363e 42 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  B );.    assert(
1363f 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65   sqlite3PcacheRe
13640 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70  fCount(pPager->p
13641 50 43 61 63 68 65 29 3d 3d 30 20 29 3b 0a 20 20  PCache)==0 );.  
13642 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f    if( pPager->no
13643 52 65 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20  Readlock ){.    
13644 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
13645 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20  ->readOnly );.  
13646 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74      pPager->stat
13647 65 20 3d 20 50 41 47 45 52 5f 53 48 41 52 45 44  e = PAGER_SHARED
13648 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
13649 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61     rc = pager_wa
1364a 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65  it_on_lock(pPage
1364b 72 2c 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b  r, SHARED_LOCK);
1364c 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
1364d 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1364e 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
1364f 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52  er->state==PAGER
13650 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20  _UNLOCK );.     
13651 20 20 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f     return pager_
13652 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63  error(pPager, rc
13653 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
13654 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
13655 67 65 72 2d 3e 73 74 61 74 65 3e 3d 53 48 41 52  ger->state>=SHAR
13656 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20 20 20 20  ED_LOCK );..    
13657 2f 2a 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 20  /* If a journal 
13658 66 69 6c 65 20 65 78 69 73 74 73 2c 20 61 6e 64  file exists, and
13659 20 74 68 65 72 65 20 69 73 20 6e 6f 20 52 45 53   there is no RES
1365a 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68  ERVED lock on th
1365b 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73  e.    ** databas
1365c 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20  e file, then it 
1365d 65 69 74 68 65 72 20 6e 65 65 64 73 20 74 6f 20  either needs to 
1365e 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b 20 6f  be played back o
1365f 72 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a  r deleted..    *
13660 2f 0a 20 20 20 20 69 66 28 20 21 69 73 45 72 72  /.    if( !isErr
13661 6f 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20 20  orReset ){.     
13662 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
13663 3e 73 74 61 74 65 20 3c 3d 20 50 41 47 45 52 5f  >state <= PAGER_
13664 53 48 41 52 45 44 20 29 3b 0a 20 20 20 20 20 20  SHARED );.      
13665 72 63 20 3d 20 68 61 73 48 6f 74 4a 6f 75 72 6e  rc = hasHotJourn
13666 61 6c 28 70 50 61 67 65 72 2c 20 26 69 73 48 6f  al(pPager, &isHo
13667 74 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20 20  tJournal);.     
13668 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
13669 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  OK ){.        go
1366a 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20  to failed;.     
1366b 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
1366c 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 7c 7c   isErrorReset ||
1366d 20 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 29 7b   isHotJournal ){
1366e 0a 20 20 20 20 20 20 2f 2a 20 47 65 74 20 61 6e  .      /* Get an
1366f 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20   EXCLUSIVE lock 
13670 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
13671 66 69 6c 65 2e 20 41 74 20 74 68 69 73 20 70 6f  file. At this po
13672 69 6e 74 20 69 74 20 69 73 0a 20 20 20 20 20 20  int it is.      
13673 2a 2a 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61  ** important tha
13674 74 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63  t a RESERVED loc
13675 6b 20 69 73 20 6e 6f 74 20 6f 62 74 61 69 6e 65  k is not obtaine
13676 64 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20  d on the way to 
13677 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 45 58 43  the.      ** EXC
13678 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66 20  LUSIVE lock. If 
13679 69 74 20 77 65 72 65 2c 20 61 6e 6f 74 68 65 72  it were, another
1367a 20 70 72 6f 63 65 73 73 20 6d 69 67 68 74 20 6f   process might o
1367b 70 65 6e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a  pen the.      **
1367c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20   database file, 
1367d 64 65 74 65 63 74 20 74 68 65 20 52 45 53 45 52  detect the RESER
1367e 56 45 44 20 6c 6f 63 6b 2c 20 61 6e 64 20 63 6f  VED lock, and co
1367f 6e 63 6c 75 64 65 20 74 68 61 74 20 74 68 65 0a  nclude that the.
13680 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73        ** databas
13681 65 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 61  e is safe to rea
13682 64 20 77 68 69 6c 65 20 74 68 69 73 20 70 72 6f  d while this pro
13683 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 72 6f  cess is still ro
13684 6c 6c 69 6e 67 20 74 68 65 20 0a 20 20 20 20 20  lling the .     
13685 20 2a 2a 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20   ** hot-journal 
13686 62 61 63 6b 2e 0a 20 20 20 20 20 20 2a 2a 20 0a  back..      ** .
13687 20 20 20 20 20 20 2a 2a 20 42 65 63 61 75 73 65        ** Because
13688 20 74 68 65 20 69 6e 74 65 72 6d 65 64 69 61 74   the intermediat
13689 65 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20  e RESERVED lock 
1368a 69 73 20 6e 6f 74 20 72 65 71 75 65 73 74 65 64  is not requested
1368b 2c 20 61 6e 79 0a 20 20 20 20 20 20 2a 2a 20 6f  , any.      ** o
1368c 74 68 65 72 20 70 72 6f 63 65 73 73 20 61 74 74  ther process att
1368d 65 6d 70 74 69 6e 67 20 74 6f 20 61 63 63 65 73  empting to acces
1368e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  s the database f
1368f 69 6c 65 20 77 69 6c 6c 20 67 65 74 20 74 6f 20  ile will get to 
13690 0a 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20 70  .      ** this p
13691 6f 69 6e 74 20 69 6e 20 74 68 65 20 63 6f 64 65  oint in the code
13692 20 61 6e 64 20 66 61 69 6c 20 74 6f 20 6f 62 74   and fail to obt
13693 61 69 6e 20 69 74 73 20 6f 77 6e 20 45 58 43 4c  ain its own EXCL
13694 55 53 49 56 45 20 6c 6f 63 6b 20 0a 20 20 20 20  USIVE lock .    
13695 20 20 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 61    ** on the data
13696 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 20  base file..     
13697 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 50   */.      if( pP
13698 61 67 65 72 2d 3e 73 74 61 74 65 3c 45 58 43 4c  ager->state<EXCL
13699 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20  USIVE_LOCK ){.  
1369a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1369b 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72 2d  e3OsLock(pPager-
1369c 3e 66 64 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c  >fd, EXCLUSIVE_L
1369d 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66  OCK);.        if
1369e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1369f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
136a0 3d 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50  = pager_error(pP
136a1 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20  ager, rc);.     
136a2 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64       goto failed
136a3 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
136a4 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74      pPager->stat
136a5 65 20 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53  e = PAGER_EXCLUS
136a6 49 56 45 3b 0a 20 20 20 20 20 20 7d 0a 20 0a 20  IVE;.      }. . 
136a7 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65       /* Open the
136a8 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20 72 65 61   journal for rea
136a9 64 2f 77 72 69 74 65 20 61 63 63 65 73 73 2e 20  d/write access. 
136aa 54 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20  This is because 
136ab 69 6e 20 0a 20 20 20 20 20 20 2a 2a 20 65 78 63  in .      ** exc
136ac 6c 75 73 69 76 65 2d 61 63 63 65 73 73 20 6d 6f  lusive-access mo
136ad 64 65 20 74 68 65 20 66 69 6c 65 20 64 65 73 63  de the file desc
136ae 72 69 70 74 6f 72 20 77 69 6c 6c 20 62 65 20 6b  riptor will be k
136af 65 70 74 20 6f 70 65 6e 20 61 6e 64 0a 20 20 20  ept open and.   
136b0 20 20 20 2a 2a 20 70 6f 73 73 69 62 6c 79 20 75     ** possibly u
136b1 73 65 64 20 66 6f 72 20 61 20 74 72 61 6e 73 61  sed for a transa
136b2 63 74 69 6f 6e 20 6c 61 74 65 72 20 6f 6e 2e 20  ction later on. 
136b3 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2c  On some systems,
136b4 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 4f 73   the.      ** Os
136b5 54 72 75 6e 63 61 74 65 28 29 20 63 61 6c 6c 20  Truncate() call 
136b6 75 73 65 64 20 69 6e 20 65 78 63 6c 75 73 69 76  used in exclusiv
136b7 65 2d 61 63 63 65 73 73 20 6d 6f 64 65 20 61 6c  e-access mode al
136b8 73 6f 20 72 65 71 75 69 72 65 73 0a 20 20 20 20  so requires.    
136b9 20 20 2a 2a 20 61 20 72 65 61 64 2f 77 72 69 74    ** a read/writ
136ba 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e 0a 20  e file handle.. 
136bb 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
136bc 28 20 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72  ( !isOpen(pPager
136bd 2d 3e 6a 66 64 29 20 29 7b 0a 20 20 20 20 20 20  ->jfd) ){.      
136be 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20    int res;.     
136bf 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
136c0 73 41 63 63 65 73 73 28 70 56 66 73 2c 70 50 61  sAccess(pVfs,pPa
136c1 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 53 51  ger->zJournal,SQ
136c2 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53  LITE_ACCESS_EXIS
136c3 54 53 2c 26 72 65 73 29 3b 0a 20 20 20 20 20 20  TS,&res);.      
136c4 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
136c5 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
136c6 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20 20   if( res ){.    
136c7 20 20 20 20 20 20 20 20 69 6e 74 20 66 6f 75 74          int fout
136c8 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
136c9 20 20 69 6e 74 20 66 20 3d 20 53 51 4c 49 54 45    int f = SQLITE
136ca 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 7c  _OPEN_READWRITE|
136cb 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
136cc 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 20 20 20 20  _JOURNAL;.      
136cd 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
136ce 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20  Pager->tempFile 
136cf 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  );.            r
136d0 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  c = sqlite3OsOpe
136d1 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e  n(pVfs, pPager->
136d2 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72  zJournal, pPager
136d3 2d 3e 6a 66 64 2c 20 66 2c 20 26 66 6f 75 74 29  ->jfd, f, &fout)
136d4 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73  ;.            as
136d5 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
136d6 5f 4f 4b 20 7c 7c 20 69 73 4f 70 65 6e 28 70 50  _OK || isOpen(pP
136d7 61 67 65 72 2d 3e 6a 66 64 29 20 29 3b 0a 20 20  ager->jfd) );.  
136d8 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
136d9 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 66  ==SQLITE_OK && f
136da 6f 75 74 26 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  out&SQLITE_OPEN_
136db 52 45 41 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20  READONLY ){.    
136dc 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
136dd 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a  QLITE_CANTOPEN;.
136de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
136df 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50 61  lite3OsClose(pPa
136e0 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20  ger->jfd);.     
136e1 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
136e2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
136e3 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
136e4 6a 6f 75 72 6e 61 6c 20 64 6f 65 73 20 6e 6f 74  journal does not
136e5 20 65 78 69 73 74 2c 20 69 74 20 75 73 75 61 6c   exist, it usual
136e6 6c 79 20 6d 65 61 6e 73 20 74 68 61 74 20 73 6f  ly means that so
136e7 6d 65 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  me .            
136e8 2a 2a 20 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74  ** other connect
136e9 69 6f 6e 20 6d 61 6e 61 67 65 64 20 74 6f 20 67  ion managed to g
136ea 65 74 20 69 6e 20 61 6e 64 20 72 6f 6c 6c 20 69  et in and roll i
136eb 74 20 62 61 63 6b 20 62 65 66 6f 72 65 20 0a 20  t back before . 
136ec 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68             ** th
136ed 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 62  is connection ob
136ee 74 61 69 6e 65 64 20 74 68 65 20 65 78 63 6c 75  tained the exclu
136ef 73 69 76 65 20 6c 6f 63 6b 20 61 62 6f 76 65 2e  sive lock above.
136f0 20 4f 72 2c 20 69 74 20 0a 20 20 20 20 20 20 20   Or, it .       
136f1 20 20 20 20 20 2a 2a 20 6d 61 79 20 6d 65 61 6e       ** may mean
136f2 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72 20   that the pager 
136f3 77 61 73 20 69 6e 20 74 68 65 20 65 72 72 6f 72  was in the error
136f4 2d 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73  -state when this
136f5 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
136f6 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 63 61 6c  function was cal
136f7 6c 65 64 20 61 6e 64 20 74 68 65 20 6a 6f 75 72  led and the jour
136f8 6e 61 6c 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f  nal file does no
136f9 74 20 65 78 69 73 74 2e 20 20 2a 2f 0a 20 20 20  t exist.  */.   
136fa 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61           rc = pa
136fb 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74  ger_end_transact
136fc 69 6f 6e 28 70 50 61 67 65 72 2c 20 30 29 3b 0a  ion(pPager, 0);.
136fd 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
136fe 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
136ff 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
13700 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
13701 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20   goto failed;.  
13702 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
13703 54 4f 44 4f 3a 20 57 68 79 20 61 72 65 20 74 68  TODO: Why are th
13704 65 73 65 20 63 6c 65 61 72 65 64 20 68 65 72 65  ese cleared here
13705 3f 20 49 73 20 69 74 20 6e 65 63 65 73 73 61 72  ? Is it necessar
13706 79 3f 20 2a 2f 0a 20 20 20 20 20 20 70 50 61 67  y? */.      pPag
13707 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74  er->journalStart
13708 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50  ed = 0;.      pP
13709 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
1370a 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 50 61 67   = 0;.      pPag
1370b 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20  er->setMaster = 
1370c 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  0;.      pPager-
1370d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30 3b  >journalHdr = 0;
1370e 0a 20 0a 20 20 20 20 20 20 2f 2a 20 50 6c 61 79  . .      /* Play
1370f 62 61 63 6b 20 61 6e 64 20 64 65 6c 65 74 65 20  back and delete 
13710 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 44 72  the journal.  Dr
13711 6f 70 20 74 68 65 20 64 61 74 61 62 61 73 65 20  op the database 
13712 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 6c  write.      ** l
13713 6f 63 6b 20 61 6e 64 20 72 65 61 63 71 75 69 72  ock and reacquir
13714 65 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 2e  e the read lock.
13715 20 50 75 72 67 65 20 74 68 65 20 63 61 63 68 65   Purge the cache
13716 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20 2a 2a   before.      **
13717 20 70 6c 61 79 69 6e 67 20 62 61 63 6b 20 74 68   playing back th
13718 65 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 73 6f  e hot-journal so
13719 20 74 68 61 74 20 77 65 20 64 6f 6e 27 74 20 65   that we don't e
1371a 6e 64 20 75 70 20 77 69 74 68 0a 20 20 20 20 20  nd up with.     
1371b 20 2a 2a 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74   ** an inconsist
1371c 65 6e 74 20 63 61 63 68 65 2e 0a 20 20 20 20 20  ent cache..     
1371d 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 73   */.      if( is
1371e 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
1371f 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  ) ){.        rc 
13720 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b  = pager_playback
13721 28 70 50 61 67 65 72 2c 20 31 29 3b 0a 20 20 20  (pPager, 1);.   
13722 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
13723 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
13724 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65      rc = pager_e
13725 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29  rror(pPager, rc)
13726 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
13727 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20   failed;.       
13728 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
13729 20 61 73 73 65 72 74 28 20 28 70 50 61 67 65 72   assert( (pPager
1372a 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53  ->state==PAGER_S
1372b 48 41 52 45 44 29 0a 20 20 20 20 20 20 20 20 20  HARED).         
1372c 20 20 7c 7c 20 28 70 50 61 67 65 72 2d 3e 65 78    || (pPager->ex
1372d 63 6c 75 73 69 76 65 4d 6f 64 65 20 26 26 20 70  clusiveMode && p
1372e 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 50 41 47  Pager->state>PAG
1372f 45 52 5f 53 48 41 52 45 44 29 0a 20 20 20 20 20  ER_SHARED).     
13730 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69   );.    }..    i
13731 66 28 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b  f( pPager->pBack
13732 75 70 20 7c 7c 20 73 71 6c 69 74 65 33 50 63 61  up || sqlite3Pca
13733 63 68 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61  chePagecount(pPa
13734 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3e 30 20  ger->pPCache)>0 
13735 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ){.      /* The 
13736 73 68 61 72 65 64 2d 6c 6f 63 6b 20 68 61 73 20  shared-lock has 
13737 6a 75 73 74 20 62 65 65 6e 20 61 63 71 75 69 72  just been acquir
13738 65 64 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ed on the databa
13739 73 65 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a  se file.      **
1373a 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 61   and there are a
1373b 6c 72 65 61 64 79 20 70 61 67 65 73 20 69 6e 20  lready pages in 
1373c 74 68 65 20 63 61 63 68 65 20 28 66 72 6f 6d 20  the cache (from 
1373d 61 20 70 72 65 76 69 6f 75 73 0a 20 20 20 20 20  a previous.     
1373e 20 2a 2a 20 72 65 61 64 20 6f 72 20 77 72 69 74   ** read or writ
1373f 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2e 20  e transaction). 
13740 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
13741 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 20 20   the database.  
13742 20 20 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20      ** has been 
13743 6d 6f 64 69 66 69 65 64 2e 20 20 49 66 20 74 68  modified.  If th
13744 65 20 64 61 74 61 62 61 73 65 20 68 61 73 20 63  e database has c
13745 68 61 6e 67 65 64 2c 20 66 6c 75 73 68 20 74 68  hanged, flush th
13746 65 0a 20 20 20 20 20 20 2a 2a 20 63 61 63 68 65  e.      ** cache
13747 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ..      **.     
13748 20 2a 2a 20 44 61 74 61 62 61 73 65 20 63 68 61   ** Database cha
13749 6e 67 65 73 20 69 73 20 64 65 74 65 63 74 65 64  nges is detected
1374a 20 62 79 20 6c 6f 6f 6b 69 6e 67 20 61 74 20 31   by looking at 1
1374b 35 20 62 79 74 65 73 20 62 65 67 69 6e 6e 69 6e  5 bytes beginnin
1374c 67 0a 20 20 20 20 20 20 2a 2a 20 61 74 20 6f 66  g.      ** at of
1374d 66 73 65 74 20 32 34 20 69 6e 74 6f 20 74 68 65  fset 24 into the
1374e 20 66 69 6c 65 2e 20 20 54 68 65 20 66 69 72 73   file.  The firs
1374f 74 20 34 20 6f 66 20 74 68 65 73 65 20 31 36 20  t 4 of these 16 
13750 62 79 74 65 73 20 61 72 65 0a 20 20 20 20 20 20  bytes are.      
13751 2a 2a 20 61 20 33 32 2d 62 69 74 20 63 6f 75 6e  ** a 32-bit coun
13752 74 65 72 20 74 68 61 74 20 69 73 20 69 6e 63 72  ter that is incr
13753 65 6d 65 6e 74 65 64 20 77 69 74 68 20 65 61 63  emented with eac
13754 68 20 63 68 61 6e 67 65 2e 20 20 54 68 65 0a 20  h change.  The. 
13755 20 20 20 20 20 2a 2a 20 6f 74 68 65 72 20 62 79       ** other by
13756 74 65 73 20 63 68 61 6e 67 65 20 72 61 6e 64 6f  tes change rando
13757 6d 6c 79 20 77 69 74 68 20 65 61 63 68 20 66 69  mly with each fi
13758 6c 65 20 63 68 61 6e 67 65 20 77 68 65 6e 0a 20  le change when. 
13759 20 20 20 20 20 2a 2a 20 61 20 63 6f 64 65 63 20       ** a codec 
1375a 69 73 20 69 6e 20 75 73 65 2e 0a 20 20 20 20 20  is in use..     
1375b 20 2a 2a 20 0a 20 20 20 20 20 20 2a 2a 20 54 68   ** .      ** Th
1375c 65 72 65 20 69 73 20 61 20 76 61 6e 69 73 68 69  ere is a vanishi
1375d 6e 67 6c 79 20 73 6d 61 6c 6c 20 63 68 61 6e 63  ngly small chanc
1375e 65 20 74 68 61 74 20 61 20 63 68 61 6e 67 65 20  e that a change 
1375f 77 69 6c 6c 20 6e 6f 74 20 62 65 20 0a 20 20 20  will not be .   
13760 20 20 20 2a 2a 20 64 65 74 65 63 74 65 64 2e 20     ** detected. 
13761 20 54 68 65 20 63 68 61 6e 63 65 20 6f 66 20 61   The chance of a
13762 6e 20 75 6e 64 65 74 65 63 74 65 64 20 63 68 61  n undetected cha
13763 6e 67 65 20 69 73 20 73 6f 20 73 6d 61 6c 6c 20  nge is so small 
13764 74 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 69 74  that.      ** it
13765 20 63 61 6e 20 62 65 20 6e 65 67 6c 65 63 74 65   can be neglecte
13766 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  d..      */.    
13767 20 20 63 68 61 72 20 64 62 46 69 6c 65 56 65 72    char dbFileVer
13768 73 5b 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d  s[sizeof(pPager-
13769 3e 64 62 46 69 6c 65 56 65 72 73 29 5d 3b 0a 20  >dbFileVers)];. 
1376a 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
1376b 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65  rPagecount(pPage
1376c 72 2c 20 30 29 3b 0a 0a 20 20 20 20 20 20 69 66  r, 0);..      if
1376d 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  ( pPager->errCod
1376e 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  e ){.        rc 
1376f 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  = pPager->errCod
13770 65 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  e;.        goto 
13771 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a  failed;.      }.
13772 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
13773 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c  Pager->dbSizeVal
13774 69 64 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  id );.      if( 
13775 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3e 30  pPager->dbSize>0
13776 20 29 7b 0a 20 20 20 20 20 20 20 20 49 4f 54 52   ){.        IOTR
13777 41 43 45 28 28 22 43 4b 56 45 52 53 20 25 70 20  ACE(("CKVERS %p 
13778 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 73  %d\n", pPager, s
13779 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73  izeof(dbFileVers
1377a 29 29 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  )));.        rc 
1377b 3d 20 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28  = sqlite3OsRead(
1377c 70 50 61 67 65 72 2d 3e 66 64 2c 20 26 64 62 46  pPager->fd, &dbF
1377d 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f 66 28  ileVers, sizeof(
1377e 64 62 46 69 6c 65 56 65 72 73 29 2c 20 32 34 29  dbFileVers), 24)
1377f 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
13780 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
13781 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61           goto fa
13782 69 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a  iled;.        }.
13783 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
13784 20 20 20 20 20 6d 65 6d 73 65 74 28 64 62 46 69       memset(dbFi
13785 6c 65 56 65 72 73 2c 20 30 2c 20 73 69 7a 65 6f  leVers, 0, sizeo
13786 66 28 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a  f(dbFileVers));.
13787 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 69        }..      i
13788 66 28 20 6d 65 6d 63 6d 70 28 70 50 61 67 65 72  f( memcmp(pPager
13789 2d 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 64 62  ->dbFileVers, db
1378a 46 69 6c 65 56 65 72 73 2c 20 73 69 7a 65 6f 66  FileVers, sizeof
1378b 28 64 62 46 69 6c 65 56 65 72 73 29 29 21 3d 30  (dbFileVers))!=0
1378c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 61 67 65   ){.        page
1378d 72 5f 72 65 73 65 74 28 70 50 61 67 65 72 29 3b  r_reset(pPager);
1378e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1378f 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
13790 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  r->exclusiveMode
13791 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 74 61 74   || pPager->stat
13792 65 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20  e==PAGER_SHARED 
13793 29 3b 0a 20 20 7d 0a 0a 20 66 61 69 6c 65 64 3a  );.  }.. failed:
13794 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
13795 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 70  E_OK ){.    /* p
13796 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 29 20 69 73  ager_unlock() is
13797 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 65 78 63   a no-op for exc
13798 6c 75 73 69 76 65 20 6d 6f 64 65 20 61 6e 64 20  lusive mode and 
13799 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
1379a 73 65 73 2e 20 2a 2f 0a 20 20 20 20 70 61 67 65  ses. */.    page
1379b 72 5f 75 6e 6c 6f 63 6b 28 70 50 61 67 65 72 29  r_unlock(pPager)
1379c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1379d 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  c;.}../*.** If t
1379e 68 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  he reference cou
1379f 6e 74 20 68 61 73 20 72 65 61 63 68 65 64 20 7a  nt has reached z
137a0 65 72 6f 2c 20 72 6f 6c 6c 62 61 63 6b 20 61 6e  ero, rollback an
137a1 79 20 61 63 74 69 76 65 0a 2a 2a 20 74 72 61 6e  y active.** tran
137a2 73 61 63 74 69 6f 6e 20 61 6e 64 20 75 6e 6c 6f  saction and unlo
137a3 63 6b 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2a  ck the pager..**
137a4 0a 2a 2a 20 45 78 63 65 70 74 2c 20 69 6e 20 6c  .** Except, in l
137a5 6f 63 6b 69 6e 67 5f 6d 6f 64 65 3d 45 58 43 4c  ocking_mode=EXCL
137a6 55 53 49 56 45 20 77 68 65 6e 20 74 68 65 72 65  USIVE when there
137a7 20 69 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20 69   is nothing to i
137a8 6e 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63  n.** the rollbac
137a9 6b 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 65 20 75  k journal, the u
137aa 6e 6c 6f 63 6b 20 69 73 20 6e 6f 74 20 70 65 72  nlock is not per
137ab 66 6f 72 6d 65 64 20 61 6e 64 20 74 68 65 72 65  formed and there
137ac 20 69 73 0a 2a 2a 20 6e 6f 74 68 69 6e 67 20 74   is.** nothing t
137ad 6f 20 72 6f 6c 6c 62 61 63 6b 2c 20 73 6f 20 74  o rollback, so t
137ae 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
137af 20 6e 6f 2d 6f 70 2e 0a 2a 2f 20 0a 73 74 61 74   no-op..*/ .stat
137b0 69 63 20 76 6f 69 64 20 70 61 67 65 72 55 6e 6c  ic void pagerUnl
137b1 6f 63 6b 49 66 55 6e 75 73 65 64 28 50 61 67 65  ockIfUnused(Page
137b2 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66  r *pPager){.  if
137b3 28 20 28 73 71 6c 69 74 65 33 50 63 61 63 68 65  ( (sqlite3Pcache
137b4 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d  RefCount(pPager-
137b5 3e 70 50 43 61 63 68 65 29 3d 3d 30 29 0a 20 20  >pPCache)==0).  
137b6 20 26 26 20 28 21 70 50 61 67 65 72 2d 3e 65 78   && (!pPager->ex
137b7 63 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 70  clusiveMode || p
137b8 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
137b9 66 3e 30 29 20 0a 20 20 29 7b 0a 20 20 20 20 70  f>0) .  ){.    p
137ba 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c  agerUnlockAndRol
137bb 6c 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20  lback(pPager);. 
137bc 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75   }.}../*.** Acqu
137bd 69 72 65 20 61 20 72 65 66 65 72 65 6e 63 65 20  ire a reference 
137be 74 6f 20 70 61 67 65 20 6e 75 6d 62 65 72 20 70  to page number p
137bf 67 6e 6f 20 69 6e 20 70 61 67 65 72 20 70 50 61  gno in pager pPa
137c0 67 65 72 20 28 61 20 70 61 67 65 0a 2a 2a 20 72  ger (a page.** r
137c1 65 66 65 72 65 6e 63 65 20 68 61 73 20 74 79 70  eference has typ
137c2 65 20 44 62 50 61 67 65 2a 29 2e 20 49 66 20 74  e DbPage*). If t
137c3 68 65 20 72 65 71 75 65 73 74 65 64 20 72 65 66  he requested ref
137c4 65 72 65 6e 63 65 20 69 73 20 0a 2a 2a 20 73 75  erence is .** su
137c5 63 63 65 73 73 66 75 6c 6c 79 20 6f 62 74 61 69  ccessfully obtai
137c6 6e 65 64 2c 20 69 74 20 69 73 20 63 6f 70 69 65  ned, it is copie
137c7 64 20 74 6f 20 2a 70 70 50 61 67 65 20 61 6e 64  d to *ppPage and
137c8 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72   SQLITE_OK retur
137c9 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ned..**.** If th
137ca 65 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65  e requested page
137cb 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74   is already in t
137cc 68 65 20 63 61 63 68 65 2c 20 69 74 20 69 73 20  he cache, it is 
137cd 72 65 74 75 72 6e 65 64 2e 20 0a 2a 2a 20 4f 74  returned. .** Ot
137ce 68 65 72 77 69 73 65 2c 20 61 20 6e 65 77 20 70  herwise, a new p
137cf 61 67 65 20 6f 62 6a 65 63 74 20 69 73 20 61 6c  age object is al
137d0 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f 70 75  located and popu
137d1 6c 61 74 65 64 20 77 69 74 68 20 64 61 74 61 0a  lated with data.
137d2 2a 2a 20 72 65 61 64 20 66 72 6f 6d 20 74 68 65  ** read from the
137d3 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
137d4 49 6e 20 73 6f 6d 65 20 63 61 73 65 73 2c 20 74  In some cases, t
137d5 68 65 20 70 63 61 63 68 65 20 6d 6f 64 75 6c 65  he pcache module
137d6 20 6d 61 79 0a 2a 2a 20 63 68 6f 6f 73 65 20 6e   may.** choose n
137d7 6f 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61  ot to allocate a
137d8 20 6e 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74   new page object
137d9 20 61 6e 64 20 6d 61 79 20 72 65 75 73 65 20 61   and may reuse a
137da 6e 20 65 78 69 73 74 69 6e 67 0a 2a 2a 20 6f 62  n existing.** ob
137db 6a 65 63 74 20 77 69 74 68 20 6e 6f 20 6f 75 74  ject with no out
137dc 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e  standing referen
137dd 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65  ces..**.** The e
137de 78 74 72 61 20 64 61 74 61 20 61 70 70 65 6e 64  xtra data append
137df 65 64 20 74 6f 20 61 20 70 61 67 65 20 69 73 20  ed to a page is 
137e0 61 6c 77 61 79 73 20 69 6e 69 74 69 61 6c 69 7a  always initializ
137e1 65 64 20 74 6f 20 7a 65 72 6f 73 20 74 68 65 20  ed to zeros the 
137e2 0a 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 20 61  .** first time a
137e3 20 70 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20   page is loaded 
137e4 69 6e 74 6f 20 6d 65 6d 6f 72 79 2e 20 49 66 20  into memory. If 
137e5 74 68 65 20 70 61 67 65 20 72 65 71 75 65 73 74  the page request
137e6 65 64 20 69 73 20 0a 2a 2a 20 61 6c 72 65 61 64  ed is .** alread
137e7 79 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 77  y in the cache w
137e8 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f  hen this functio
137e9 6e 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65  n is called, the
137ea 6e 20 74 68 65 20 65 78 74 72 61 0a 2a 2a 20 64  n the extra.** d
137eb 61 74 61 20 69 73 20 6c 65 66 74 20 61 73 20 69  ata is left as i
137ec 74 20 77 61 73 20 77 68 65 6e 20 74 68 65 20 70  t was when the p
137ed 61 67 65 20 6f 62 6a 65 63 74 20 77 61 73 20 6c  age object was l
137ee 61 73 74 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20  ast used..**.** 
137ef 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
137f0 69 6d 61 67 65 20 69 73 20 73 6d 61 6c 6c 65 72  image is smaller
137f1 20 74 68 61 6e 20 74 68 65 20 72 65 71 75 65 73   than the reques
137f2 74 65 64 20 70 61 67 65 20 6f 72 20 69 66 20 61  ted page or if a
137f3 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61   .** non-zero va
137f4 6c 75 65 20 69 73 20 70 61 73 73 65 64 20 61 73  lue is passed as
137f5 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 20 70   the noContent p
137f6 61 72 61 6d 65 74 65 72 20 61 6e 64 20 74 68 65  arameter and the
137f7 20 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 20 70   .** requested p
137f8 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61  age is not alrea
137f9 64 79 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  dy stored in the
137fa 20 63 61 63 68 65 2c 20 74 68 65 6e 20 6e 6f 20   cache, then no 
137fb 0a 2a 2a 20 61 63 74 75 61 6c 20 64 69 73 6b 20  .** actual disk 
137fc 72 65 61 64 20 6f 63 63 75 72 73 2e 20 49 6e 20  read occurs. In 
137fd 74 68 69 73 20 63 61 73 65 20 74 68 65 20 6d 65  this case the me
137fe 6d 6f 72 79 20 69 6d 61 67 65 20 6f 66 20 74 68  mory image of th
137ff 65 20 0a 2a 2a 20 70 61 67 65 20 69 73 20 69 6e  e .** page is in
13800 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 6c 6c  itialized to all
13801 20 7a 65 72 6f 73 2e 20 0a 2a 2a 0a 2a 2a 20 49   zeros. .**.** I
13802 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73 20 74  f noContent is t
13803 72 75 65 2c 20 69 74 20 6d 65 61 6e 73 20 74 68  rue, it means th
13804 61 74 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72  at we do not car
13805 65 20 61 62 6f 75 74 20 74 68 65 20 63 6f 6e 74  e about the cont
13806 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70  ents.** of the p
13807 61 67 65 2e 20 54 68 69 73 20 6f 63 63 75 72 73  age. This occurs
13808 20 69 6e 20 74 77 6f 20 73 65 70 65 72 61 74 65   in two seperate
13809 20 73 63 65 6e 61 72 69 6f 73 3a 0a 2a 2a 0a 2a   scenarios:.**.*
1380a 2a 20 20 20 61 29 20 57 68 65 6e 20 72 65 61 64  *   a) When read
1380b 69 6e 67 20 61 20 66 72 65 65 2d 6c 69 73 74 20  ing a free-list 
1380c 6c 65 61 66 20 70 61 67 65 20 66 72 6f 6d 20 74  leaf page from t
1380d 68 65 20 64 61 74 61 62 61 73 65 2c 20 61 6e 64  he database, and
1380e 0a 2a 2a 0a 2a 2a 20 20 20 62 29 20 57 68 65 6e  .**.**   b) When
1380f 20 61 20 73 61 76 65 70 6f 69 6e 74 20 69 73 20   a savepoint is 
13810 62 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63  being rolled bac
13811 6b 20 61 6e 64 20 77 65 20 6e 65 65 64 20 74 6f  k and we need to
13812 20 6c 6f 61 64 0a 2a 2a 20 20 20 20 20 20 61 20   load.**      a 
13813 6e 65 77 20 70 61 67 65 20 69 6e 74 6f 20 74 68  new page into th
13814 65 20 63 61 63 68 65 20 74 6f 20 70 6f 70 75 6c  e cache to popul
13815 61 74 65 20 77 69 74 68 20 74 68 65 20 64 61 74  ate with the dat
13816 61 20 72 65 61 64 0a 2a 2a 20 20 20 20 20 20 66  a read.**      f
13817 72 6f 6d 20 74 68 65 20 73 61 76 65 70 6f 69 6e  rom the savepoin
13818 74 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a  t journal..**.**
13819 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74 20 69 73   If noContent is
1381a 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20   true, then the 
1381b 64 61 74 61 20 72 65 74 75 72 6e 65 64 20 69 73  data returned is
1381c 20 7a 65 72 6f 65 64 20 69 6e 73 74 65 61 64 20   zeroed instead 
1381d 6f 66 0a 2a 2a 20 62 65 69 6e 67 20 72 65 61 64  of.** being read
1381e 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
1381f 73 65 2e 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79  se. Additionally
13820 2c 20 74 68 65 20 62 69 74 73 20 63 6f 72 72 65  , the bits corre
13821 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 70  sponding.** to p
13822 67 6e 6f 20 69 6e 20 50 61 67 65 72 2e 70 49 6e  gno in Pager.pIn
13823 4a 6f 75 72 6e 61 6c 20 28 62 69 74 76 65 63 20  Journal (bitvec 
13824 6f 66 20 70 61 67 65 73 20 61 6c 72 65 61 64 79  of pages already
13825 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a   written to the.
13826 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29  ** journal file)
13827 20 61 6e 64 20 74 68 65 20 50 61 67 65 72 53 61   and the PagerSa
13828 76 65 70 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70  vepoint.pInSavep
13829 6f 69 6e 74 20 62 69 74 76 65 63 73 20 6f 66 20  oint bitvecs of 
1382a 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 73 61 76 65  any open.** save
1382b 70 6f 69 6e 74 73 20 61 72 65 20 73 65 74 2e 20  points are set. 
1382c 54 68 69 73 20 6d 65 61 6e 73 20 69 66 20 74 68  This means if th
1382d 65 20 70 61 67 65 20 69 73 20 6d 61 64 65 20 77  e page is made w
1382e 72 69 74 61 62 6c 65 20 61 74 20 61 6e 79 0a 2a  ritable at any.*
1382f 2a 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66  * point in the f
13830 75 74 75 72 65 2c 20 75 73 69 6e 67 20 61 20 63  uture, using a c
13831 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 50 61  all to sqlite3Pa
13832 67 65 72 57 72 69 74 65 28 29 2c 20 69 74 73 20  gerWrite(), its 
13833 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 77 69 6c 6c  contents.** will
13834 20 6e 6f 74 20 62 65 20 6a 6f 75 72 6e 61 6c 65   not be journale
13835 64 2e 20 54 68 69 73 20 73 61 76 65 73 20 49 4f  d. This saves IO
13836 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 63 71 75  ..**.** The acqu
13837 69 73 69 74 69 6f 6e 20 6d 69 67 68 74 20 66 61  isition might fa
13838 69 6c 20 66 6f 72 20 73 65 76 65 72 61 6c 20 72  il for several r
13839 65 61 73 6f 6e 73 2e 20 20 49 6e 20 61 6c 6c 20  easons.  In all 
1383a 63 61 73 65 73 2c 0a 2a 2a 20 61 6e 20 61 70 70  cases,.** an app
1383b 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 63  ropriate error c
1383c 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20  ode is returned 
1383d 61 6e 64 20 2a 70 70 50 61 67 65 20 69 73 20 73  and *ppPage is s
1383e 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  et to NULL..**.*
1383f 2a 20 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74  * See also sqlit
13840 65 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 29 2e  e3PagerLookup().
13841 20 20 42 6f 74 68 20 74 68 69 73 20 72 6f 75 74    Both this rout
13842 69 6e 65 20 61 6e 64 20 4c 6f 6f 6b 75 70 28 29  ine and Lookup()
13843 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 66   attempt.** to f
13844 69 6e 64 20 61 20 70 61 67 65 20 69 6e 20 74 68  ind a page in th
13845 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68  e in-memory cach
13846 65 20 66 69 72 73 74 2e 20 20 49 66 20 74 68 65  e first.  If the
13847 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61 6c 72   page is not alr
13848 65 61 64 79 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72  eady.** in memor
13849 79 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  y, this routine 
1384a 67 6f 65 73 20 74 6f 20 64 69 73 6b 20 74 6f 20  goes to disk to 
1384b 72 65 61 64 20 69 74 20 69 6e 20 77 68 65 72 65  read it in where
1384c 61 73 20 4c 6f 6f 6b 75 70 28 29 0a 2a 2a 20 6a  as Lookup().** j
1384d 75 73 74 20 72 65 74 75 72 6e 73 20 30 2e 20 20  ust returns 0.  
1384e 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 71  This routine acq
1384f 75 69 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63  uires a read-loc
13850 6b 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65  k the first time
13851 20 69 74 0a 2a 2a 20 68 61 73 20 74 6f 20 67 6f   it.** has to go
13852 20 74 6f 20 64 69 73 6b 2c 20 61 6e 64 20 63 6f   to disk, and co
13853 75 6c 64 20 61 6c 73 6f 20 70 6c 61 79 62 61 63  uld also playbac
13854 6b 20 61 6e 20 6f 6c 64 20 6a 6f 75 72 6e 61 6c  k an old journal
13855 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   if necessary..*
13856 2a 20 53 69 6e 63 65 20 4c 6f 6f 6b 75 70 28 29  * Since Lookup()
13857 20 6e 65 76 65 72 20 67 6f 65 73 20 74 6f 20 64   never goes to d
13858 69 73 6b 2c 20 69 74 20 6e 65 76 65 72 20 68 61  isk, it never ha
13859 73 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 6c  s to deal with l
1385a 6f 63 6b 73 0a 2a 2a 20 6f 72 20 6a 6f 75 72 6e  ocks.** or journ
1385b 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c  al files..*/.SQL
1385c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1385d 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75  sqlite3PagerAcqu
1385e 69 72 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50  ire(.  Pager *pP
1385f 61 67 65 72 2c 20 20 20 20 20 20 2f 2a 20 54 68  ager,      /* Th
13860 65 20 70 61 67 65 72 20 6f 70 65 6e 20 6f 6e 20  e pager open on 
13861 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
13862 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  e */.  Pgno pgno
13863 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61  ,          /* Pa
13864 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 66 65 74  ge number to fet
13865 63 68 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a  ch */.  DbPage *
13866 2a 70 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 57  *ppPage,    /* W
13867 72 69 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74  rite a pointer t
13868 6f 20 74 68 65 20 70 61 67 65 20 68 65 72 65 20  o the page here 
13869 2a 2f 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65  */.  int noConte
1386a 6e 74 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e  nt       /* Do n
1386b 6f 74 20 62 6f 74 68 65 72 20 72 65 61 64 69 6e  ot bother readin
1386c 67 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 64  g content from d
1386d 69 73 6b 20 69 66 20 74 72 75 65 20 2a 2f 0a 29  isk if true */.)
1386e 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 67  {.  int rc;.  Pg
1386f 48 64 72 20 2a 70 50 67 3b 0a 0a 20 20 61 73 73  Hdr *pPg;..  ass
13870 65 72 74 28 20 61 73 73 65 72 74 5f 70 61 67 65  ert( assert_page
13871 72 5f 73 74 61 74 65 28 70 50 61 67 65 72 29 20  r_state(pPager) 
13872 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
13873 67 65 72 2d 3e 73 74 61 74 65 3e 50 41 47 45 52  ger->state>PAGER
13874 5f 55 4e 4c 4f 43 4b 20 29 3b 0a 0a 20 20 69 66  _UNLOCK );..  if
13875 28 20 70 67 6e 6f 3d 3d 30 20 29 7b 0a 20 20 20  ( pgno==0 ){.   
13876 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
13877 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d  ORRUPT_BKPT;.  }
13878 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61  ..  /* If the pa
13879 67 65 72 20 69 73 20 69 6e 20 74 68 65 20 65 72  ger is in the er
1387a 72 6f 72 20 73 74 61 74 65 2c 20 72 65 74 75 72  ror state, retur
1387b 6e 20 61 6e 20 65 72 72 6f 72 20 69 6d 6d 65 64  n an error immed
1387c 69 61 74 65 6c 79 2e 20 0a 20 20 2a 2a 20 4f 74  iately. .  ** Ot
1387d 68 65 72 77 69 73 65 2c 20 72 65 71 75 65 73 74  herwise, request
1387e 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 74   the page from t
1387f 68 65 20 50 43 61 63 68 65 20 6c 61 79 65 72 2e  he PCache layer.
13880 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72   */.  if( pPager
13881 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 54  ->errCode!=SQLIT
13882 45 5f 4f 4b 20 26 26 20 70 50 61 67 65 72 2d 3e  E_OK && pPager->
13883 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 54 45 5f  errCode!=SQLITE_
13884 46 55 4c 4c 20 29 7b 0a 20 20 20 20 72 63 20 3d  FULL ){.    rc =
13885 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
13886 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
13887 63 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68  c = sqlite3Pcach
13888 65 46 65 74 63 68 28 70 50 61 67 65 72 2d 3e 70  eFetch(pPager->p
13889 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c 20 31 2c  PCache, pgno, 1,
1388a 20 70 70 50 61 67 65 29 3b 0a 20 20 7d 0a 0a 20   ppPage);.  }.. 
1388b 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1388c 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 45 69 74  OK ){.    /* Eit
1388d 68 65 72 20 74 68 65 20 63 61 6c 6c 20 74 6f 20  her the call to 
1388e 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74  sqlite3PcacheFet
1388f 63 68 28 29 20 72 65 74 75 72 6e 65 64 20 61 6e  ch() returned an
13890 20 65 72 72 6f 72 20 6f 72 20 74 68 65 0a 20 20   error or the.  
13891 20 20 2a 2a 20 70 61 67 65 72 20 77 61 73 20 61    ** pager was a
13892 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 65 72  lready in the er
13893 72 6f 72 2d 73 74 61 74 65 20 77 68 65 6e 20 74  ror-state when t
13894 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 61 73  his function was
13895 20 63 61 6c 6c 65 64 2e 0a 20 20 20 20 2a 2a 20   called..    ** 
13896 53 65 74 20 70 50 67 20 74 6f 20 30 20 61 6e 64  Set pPg to 0 and
13897 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 65 78 63   jump to the exc
13898 65 70 74 69 6f 6e 20 68 61 6e 64 6c 65 72 2e 20  eption handler. 
13899 20 2a 2f 0a 20 20 20 20 70 50 67 20 3d 20 30 3b   */.    pPg = 0;
1389a 0a 20 20 20 20 67 6f 74 6f 20 70 61 67 65 72 5f  .    goto pager_
1389b 61 63 71 75 69 72 65 5f 65 72 72 3b 0a 20 20 7d  acquire_err;.  }
1389c 0a 20 20 61 73 73 65 72 74 28 20 28 2a 70 70 50  .  assert( (*ppP
1389d 61 67 65 29 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f  age)->pgno==pgno
1389e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 2a   );.  assert( (*
1389f 70 70 50 61 67 65 29 2d 3e 70 50 61 67 65 72 3d  ppPage)->pPager=
138a0 3d 70 50 61 67 65 72 20 7c 7c 20 28 2a 70 70 50  =pPager || (*ppP
138a1 61 67 65 29 2d 3e 70 50 61 67 65 72 3d 3d 30 20  age)->pPager==0 
138a2 29 3b 0a 0a 20 20 69 66 28 20 28 2a 70 70 50 61  );..  if( (*ppPa
138a3 67 65 29 2d 3e 70 50 61 67 65 72 20 29 7b 0a 20  ge)->pPager ){. 
138a4 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 63 61     /* In this ca
138a5 73 65 20 74 68 65 20 70 63 61 63 68 65 20 61 6c  se the pcache al
138a6 72 65 61 64 79 20 63 6f 6e 74 61 69 6e 73 20 61  ready contains a
138a7 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 63 6f  n initialized co
138a8 70 79 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65  py of.    ** the
138a9 20 70 61 67 65 2e 20 52 65 74 75 72 6e 20 77 69   page. Return wi
138aa 74 68 6f 75 74 20 66 75 72 74 68 65 72 20 61 64  thout further ad
138ab 6f 2e 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  o.  */.    asser
138ac 74 28 20 70 67 6e 6f 3c 3d 50 41 47 45 52 5f 4d  t( pgno<=PAGER_M
138ad 41 58 5f 50 47 4e 4f 20 26 26 20 70 67 6e 6f 21  AX_PGNO && pgno!
138ae 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70  =PAGER_MJ_PGNO(p
138af 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 50 41  Pager) );.    PA
138b0 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72 2d  GER_INCR(pPager-
138b1 3e 6e 48 69 74 29 3b 0a 20 20 20 20 72 65 74 75  >nHit);.    retu
138b2 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20  rn SQLITE_OK;.. 
138b3 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54   }else{.    /* T
138b4 68 65 20 70 61 67 65 72 20 63 61 63 68 65 20 68  he pager cache h
138b5 61 73 20 63 72 65 61 74 65 64 20 61 20 6e 65 77  as created a new
138b6 20 70 61 67 65 2e 20 49 74 73 20 63 6f 6e 74 65   page. Its conte
138b7 6e 74 20 6e 65 65 64 73 20 74 6f 20 0a 20 20 20  nt needs to .   
138b8 20 2a 2a 20 62 65 20 69 6e 69 74 69 61 6c 69 7a   ** be initializ
138b9 65 64 2e 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ed.  */.    int 
138ba 6e 4d 61 78 3b 0a 0a 20 20 20 20 50 41 47 45 52  nMax;..    PAGER
138bb 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 4d  _INCR(pPager->nM
138bc 69 73 73 29 3b 0a 20 20 20 20 70 50 67 20 3d 20  iss);.    pPg = 
138bd 2a 70 70 50 61 67 65 3b 0a 20 20 20 20 70 50 67  *ppPage;.    pPg
138be 2d 3e 70 50 61 67 65 72 20 3d 20 70 50 61 67 65  ->pPager = pPage
138bf 72 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 6d  r;..    /* The m
138c0 61 78 69 6d 75 6d 20 70 61 67 65 20 6e 75 6d 62  aximum page numb
138c1 65 72 20 69 73 20 32 5e 33 31 2e 20 52 65 74 75  er is 2^31. Retu
138c2 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
138c3 54 20 69 66 20 61 20 70 61 67 65 0a 20 20 20 20  T if a page.    
138c4 2a 2a 20 6e 75 6d 62 65 72 20 67 72 65 61 74 65  ** number greate
138c5 72 20 74 68 61 6e 20 74 68 69 73 2c 20 6f 72 20  r than this, or 
138c6 74 68 65 20 75 6e 75 73 65 64 20 6c 6f 63 6b 69  the unused locki
138c7 6e 67 2d 70 61 67 65 2c 20 69 73 20 72 65 71 75  ng-page, is requ
138c8 65 73 74 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66  ested. */.    if
138c9 28 20 70 67 6e 6f 3e 50 41 47 45 52 5f 4d 41 58  ( pgno>PAGER_MAX
138ca 5f 50 47 4e 4f 20 7c 7c 20 70 67 6e 6f 3d 3d 50  _PGNO || pgno==P
138cb 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61  AGER_MJ_PGNO(pPa
138cc 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 72 63  ger) ){.      rc
138cd 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50   = SQLITE_CORRUP
138ce 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f  T_BKPT;.      go
138cf 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69 72 65  to pager_acquire
138d0 5f 65 72 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  _err;.    }..   
138d1 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
138d2 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67  erPagecount(pPag
138d3 65 72 2c 20 26 6e 4d 61 78 29 3b 0a 20 20 20 20  er, &nMax);.    
138d4 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
138d5 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  K ){.      goto 
138d6 70 61 67 65 72 5f 61 63 71 75 69 72 65 5f 65 72  pager_acquire_er
138d7 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  r;.    }..    if
138d8 28 20 6e 4d 61 78 3c 28 69 6e 74 29 70 67 6e 6f  ( nMax<(int)pgno
138d9 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20 6e 6f 43   || MEMDB || noC
138da 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20  ontent ){.      
138db 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d  if( pgno>pPager-
138dc 3e 6d 78 50 67 6e 6f 20 29 7b 0a 09 72 63 20 3d  >mxPgno ){..rc =
138dd 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 09 67   SQLITE_FULL;..g
138de 6f 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69 72  oto pager_acquir
138df 65 5f 65 72 72 3b 0a 20 20 20 20 20 20 7d 0a 20  e_err;.      }. 
138e0 20 20 20 20 20 69 66 28 20 6e 6f 43 6f 6e 74 65       if( noConte
138e1 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  nt ){.        /*
138e2 20 46 61 69 6c 75 72 65 20 74 6f 20 73 65 74 20   Failure to set 
138e3 74 68 65 20 62 69 74 73 20 69 6e 20 74 68 65 20  the bits in the 
138e4 49 6e 4a 6f 75 72 6e 61 6c 20 62 69 74 2d 76 65  InJournal bit-ve
138e5 63 74 6f 72 73 20 69 73 20 62 65 6e 69 67 6e 2e  ctors is benign.
138e6 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 74 20 6d  .        ** It m
138e7 65 72 65 6c 79 20 6d 65 61 6e 73 20 74 68 61 74  erely means that
138e8 20 77 65 20 6d 69 67 68 74 20 64 6f 20 73 6f 6d   we might do som
138e9 65 20 65 78 74 72 61 20 77 6f 72 6b 20 74 6f 20  e extra work to 
138ea 6a 6f 75 72 6e 61 6c 20 61 20 0a 20 20 20 20 20  journal a .     
138eb 20 20 20 2a 2a 20 70 61 67 65 20 74 68 61 74 20     ** page that 
138ec 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f  does not need to
138ed 20 62 65 20 6a 6f 75 72 6e 61 6c 65 64 2e 20 20   be journaled.  
138ee 4e 65 76 65 72 74 68 65 6c 65 73 73 2c 20 62 65  Nevertheless, be
138ef 20 73 75 72 65 20 0a 20 20 20 20 20 20 20 20 2a   sure .        *
138f0 2a 20 74 6f 20 74 65 73 74 20 74 68 65 20 63 61  * to test the ca
138f1 73 65 20 77 68 65 72 65 20 61 20 6d 61 6c 6c 6f  se where a mallo
138f2 63 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77  c error occurs w
138f3 68 69 6c 65 20 74 72 79 69 6e 67 20 74 6f 20 73  hile trying to s
138f4 65 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  et .        ** a
138f5 20 62 69 74 20 69 6e 20 61 20 62 69 74 20 76 65   bit in a bit ve
138f6 63 74 6f 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f  ctor..        */
138f7 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
138f8 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f  BeginBenignMallo
138f9 63 28 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  c();.        if(
138fa 20 70 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 64   pgno<=pPager->d
138fb 62 4f 72 69 67 53 69 7a 65 20 29 7b 0a 20 20 20  bOrigSize ){.   
138fc 20 20 20 20 20 20 20 54 45 53 54 4f 4e 4c 59 28         TESTONLY(
138fd 20 72 63 20 3d 20 29 20 73 71 6c 69 74 65 33 42   rc = ) sqlite3B
138fe 69 74 76 65 63 53 65 74 28 70 50 61 67 65 72 2d  itvecSet(pPager-
138ff 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 67 6e  >pInJournal, pgn
13900 6f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65  o);.          te
13901 73 74 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49  stcase( rc==SQLI
13902 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20  TE_NOMEM );.    
13903 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 54 45      }.        TE
13904 53 54 4f 4e 4c 59 28 20 72 63 20 3d 20 29 20 61  STONLY( rc = ) a
13905 64 64 54 6f 53 61 76 65 70 6f 69 6e 74 42 69 74  ddToSavepointBit
13906 76 65 63 73 28 70 50 61 67 65 72 2c 20 70 67 6e  vecs(pPager, pgn
13907 6f 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74  o);.        test
13908 63 61 73 65 28 20 72 63 3d 3d 53 51 4c 49 54 45  case( rc==SQLITE
13909 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 20 20  _NOMEM );.      
1390a 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69    sqlite3EndBeni
1390b 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20  gnMalloc();.    
1390c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1390d 20 6d 65 6d 73 65 74 28 70 50 67 2d 3e 70 44 61   memset(pPg->pDa
1390e 74 61 2c 20 30 2c 20 70 50 61 67 65 72 2d 3e 70  ta, 0, pPager->p
1390f 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20  ageSize);.      
13910 7d 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45 28  }.      IOTRACE(
13911 28 22 5a 45 52 4f 20 25 70 20 25 64 5c 6e 22 2c  ("ZERO %p %d\n",
13912 20 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 29 3b   pPager, pgno));
13913 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
13914 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 70    assert( pPg->p
13915 50 61 67 65 72 3d 3d 70 50 61 67 65 72 20 29 3b  Pager==pPager );
13916 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65 61 64  .      rc = read
13917 44 62 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20  DbPage(pPg);.   
13918 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
13919 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1391a 67 6f 74 6f 20 70 61 67 65 72 5f 61 63 71 75 69  goto pager_acqui
1391b 72 65 5f 65 72 72 3b 0a 20 20 20 20 20 20 7d 0a  re_err;.      }.
1391c 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c      }.#ifdef SQL
1391d 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a  ITE_CHECK_PAGES.
1391e 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73      pPg->pageHas
1391f 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61  h = pager_pageha
13920 73 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a  sh(pPg);.#endif.
13921 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51    }..  return SQ
13922 4c 49 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 72 5f  LITE_OK;..pager_
13923 61 63 71 75 69 72 65 5f 65 72 72 3a 0a 20 20 61  acquire_err:.  a
13924 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
13925 45 5f 4f 4b 20 29 3b 0a 20 20 69 66 28 20 70 50  E_OK );.  if( pP
13926 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  g ){.    sqlite3
13927 50 63 61 63 68 65 44 72 6f 70 28 70 50 67 29 3b  PcacheDrop(pPg);
13928 0a 20 20 7d 0a 20 20 70 61 67 65 72 55 6e 6c 6f  .  }.  pagerUnlo
13929 63 6b 49 66 55 6e 75 73 65 64 28 70 50 61 67 65  ckIfUnused(pPage
1392a 72 29 3b 0a 0a 20 20 2a 70 70 50 61 67 65 20 3d  r);..  *ppPage =
1392b 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   0;.  return rc;
1392c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72  .}../*.** Acquir
1392d 65 20 61 20 70 61 67 65 20 69 66 20 69 74 20 69  e a page if it i
1392e 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65  s already in the
1392f 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65   in-memory cache
13930 2e 20 20 44 6f 0a 2a 2a 20 6e 6f 74 20 72 65 61  .  Do.** not rea
13931 64 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20  d the page from 
13932 64 69 73 6b 2e 20 20 52 65 74 75 72 6e 20 61 20  disk.  Return a 
13933 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70  pointer to the p
13934 61 67 65 2c 0a 2a 2a 20 6f 72 20 30 20 69 66 20  age,.** or 0 if 
13935 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20  the page is not 
13936 69 6e 20 63 61 63 68 65 2e 20 41 6c 73 6f 2c 20  in cache. Also, 
13937 72 65 74 75 72 6e 20 30 20 69 66 20 74 68 65 20  return 0 if the 
13938 0a 2a 2a 20 70 61 67 65 72 20 69 73 20 69 6e 20  .** pager is in 
13939 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 73 74 61  PAGER_UNLOCK sta
1393a 74 65 20 77 68 65 6e 20 74 68 69 73 20 66 75 6e  te when this fun
1393b 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2c  ction is called,
1393c 0a 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 70 61  .** or if the pa
1393d 67 65 72 20 69 73 20 69 6e 20 61 6e 20 65 72 72  ger is in an err
1393e 6f 72 20 73 74 61 74 65 20 6f 74 68 65 72 20 74  or state other t
1393f 68 61 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 2e  han SQLITE_FULL.
13940 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20  .**.** See also 
13941 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
13942 29 2e 20 20 54 68 65 20 64 69 66 66 65 72 65 6e  ).  The differen
13943 63 65 20 62 65 74 77 65 65 6e 20 74 68 69 73 20  ce between this 
13944 72 6f 75 74 69 6e 65 0a 2a 2a 20 61 6e 64 20 73  routine.** and s
13945 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29  qlite3PagerGet()
13946 20 69 73 20 74 68 61 74 20 5f 67 65 74 28 29 20   is that _get() 
13947 77 69 6c 6c 20 67 6f 20 74 6f 20 74 68 65 20 64  will go to the d
13948 69 73 6b 20 61 6e 64 20 72 65 61 64 0a 2a 2a 20  isk and read.** 
13949 69 6e 20 74 68 65 20 70 61 67 65 20 69 66 20 74  in the page if t
1394a 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 61  he page is not a
1394b 6c 72 65 61 64 79 20 69 6e 20 63 61 63 68 65 2e  lready in cache.
1394c 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a    This routine.*
1394d 2a 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69  * returns NULL i
1394e 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f  f the page is no
1394f 74 20 69 6e 20 63 61 63 68 65 20 6f 72 20 69 66  t in cache or if
13950 20 61 20 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f   a disk I/O erro
13951 72 20 0a 2a 2a 20 68 61 73 20 65 76 65 72 20 68  r .** has ever h
13952 61 70 70 65 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  appened..*/.SQLI
13953 54 45 5f 50 52 49 56 41 54 45 20 44 62 50 61 67  TE_PRIVATE DbPag
13954 65 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 4c  e *sqlite3PagerL
13955 6f 6f 6b 75 70 28 50 61 67 65 72 20 2a 70 50 61  ookup(Pager *pPa
13956 67 65 72 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b  ger, Pgno pgno){
13957 0a 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20  .  PgHdr *pPg = 
13958 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  0;.  assert( pPa
13959 67 65 72 21 3d 30 20 29 3b 0a 20 20 61 73 73 65  ger!=0 );.  asse
1395a 72 74 28 20 70 67 6e 6f 21 3d 30 20 29 3b 0a 20  rt( pgno!=0 );. 
1395b 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
1395c 3e 70 50 43 61 63 68 65 21 3d 30 20 29 3b 0a 20  >pPCache!=0 );. 
1395d 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
1395e 3e 73 74 61 74 65 20 3e 20 50 41 47 45 52 5f 55  >state > PAGER_U
1395f 4e 4c 4f 43 4b 20 29 3b 0a 20 20 73 71 6c 69 74  NLOCK );.  sqlit
13960 65 33 50 63 61 63 68 65 46 65 74 63 68 28 70 50  e3PcacheFetch(pP
13961 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70  ager->pPCache, p
13962 67 6e 6f 2c 20 30 2c 20 26 70 50 67 29 3b 0a 20  gno, 0, &pPg);. 
13963 20 72 65 74 75 72 6e 20 70 50 67 3b 0a 7d 0a 0a   return pPg;.}..
13964 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20  /*.** Release a 
13965 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 2e 0a  page reference..
13966 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 75 6d  **.** If the num
13967 62 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65  ber of reference
13968 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 64 72  s to the page dr
13969 6f 70 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65 6e  op to zero, then
1396a 20 74 68 65 0a 2a 2a 20 70 61 67 65 20 69 73 20   the.** page is 
1396b 61 64 64 65 64 20 74 6f 20 74 68 65 20 4c 52 55  added to the LRU
1396c 20 6c 69 73 74 2e 20 20 57 68 65 6e 20 61 6c 6c   list.  When all
1396d 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 61   references to a
1396e 6c 6c 20 70 61 67 65 73 0a 2a 2a 20 61 72 65 20  ll pages.** are 
1396f 72 65 6c 65 61 73 65 64 2c 20 61 20 72 6f 6c 6c  released, a roll
13970 62 61 63 6b 20 6f 63 63 75 72 73 20 61 6e 64 20  back occurs and 
13971 74 68 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  the lock on the 
13972 64 61 74 61 62 61 73 65 20 69 73 0a 2a 2a 20 72  database is.** r
13973 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  emoved..*/.SQLIT
13974 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
13975 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
13976 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20  (DbPage *pPg){. 
13977 20 69 66 28 20 70 50 67 20 29 7b 0a 20 20 20 20   if( pPg ){.    
13978 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20  Pager *pPager = 
13979 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 20  pPg->pPager;.   
1397a 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65   sqlite3PcacheRe
1397b 6c 65 61 73 65 28 70 50 67 29 3b 0a 20 20 20 20  lease(pPg);.    
1397c 70 61 67 65 72 55 6e 6c 6f 63 6b 49 66 55 6e 75  pagerUnlockIfUnu
1397d 73 65 64 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  sed(pPager);.  }
1397e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
1397f 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 66 69   main journal fi
13980 6c 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  le has already b
13981 65 65 6e 20 6f 70 65 6e 65 64 2c 20 65 6e 73 75  een opened, ensu
13982 72 65 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 73  re that the.** s
13983 75 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ub-journal file 
13984 69 73 20 6f 70 65 6e 20 74 6f 6f 2e 20 49 66 20  is open too. If 
13985 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c  the main journal
13986 20 69 73 20 6e 6f 74 20 6f 70 65 6e 2c 0a 2a 2a   is not open,.**
13987 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
13988 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a  s a no-op..**.**
13989 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
1398a 74 75 72 6e 65 64 20 69 66 20 65 76 65 72 79 74  turned if everyt
1398b 68 69 6e 67 20 67 6f 65 73 20 61 63 63 6f 72 64  hing goes accord
1398c 69 6e 67 20 74 6f 20 70 6c 61 6e 2e 20 0a 2a 2a  ing to plan. .**
1398d 20 41 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52   An SQLITE_IOERR
1398e 5f 58 58 58 20 65 72 72 6f 72 20 63 6f 64 65 20  _XXX error code 
1398f 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 61  is returned if a
13990 20 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 73 71 6c   call to .** sql
13991 69 74 65 33 4f 73 4f 70 65 6e 28 29 20 66 61 69  ite3OsOpen() fai
13992 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ls..*/.static in
13993 74 20 6f 70 65 6e 53 75 62 4a 6f 75 72 6e 61 6c  t openSubJournal
13994 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
13995 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
13996 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 69 73 4f  TE_OK;.  if( isO
13997 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
13998 20 26 26 20 21 69 73 4f 70 65 6e 28 70 50 61 67   && !isOpen(pPag
13999 65 72 2d 3e 73 6a 66 64 29 20 29 7b 0a 20 20 20  er->sjfd) ){.   
1399a 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75   if( pPager->jou
1399b 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  rnalMode==PAGER_
1399c 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f  JOURNALMODE_MEMO
1399d 52 59 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73 75  RY || pPager->su
1399e 62 6a 49 6e 4d 65 6d 6f 72 79 20 29 7b 0a 20 20  bjInMemory ){.  
1399f 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f      sqlite3MemJo
139a0 75 72 6e 61 6c 4f 70 65 6e 28 70 50 61 67 65 72  urnalOpen(pPager
139a1 2d 3e 73 6a 66 64 29 3b 0a 20 20 20 20 7d 65 6c  ->sjfd);.    }el
139a2 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 70  se{.      rc = p
139a3 61 67 65 72 4f 70 65 6e 74 65 6d 70 28 70 50 61  agerOpentemp(pPa
139a4 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 6a 66  ger, pPager->sjf
139a5 64 2c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53  d, SQLITE_OPEN_S
139a6 55 42 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20 20 20  UBJOURNAL);.    
139a7 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
139a8 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
139a9 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
139aa 6c 65 64 20 61 74 20 74 68 65 20 73 74 61 72 74  led at the start
139ab 20 6f 66 20 65 76 65 72 79 20 77 72 69 74 65 20   of every write 
139ac 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20  transaction..** 
139ad 54 68 65 72 65 20 6d 75 73 74 20 61 6c 72 65 61  There must alrea
139ae 64 79 20 62 65 20 61 20 52 45 53 45 52 56 45 44  dy be a RESERVED
139af 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   or EXCLUSIVE lo
139b0 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ck on the databa
139b1 73 65 20 0a 2a 2a 20 66 69 6c 65 20 77 68 65 6e  se .** file when
139b2 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
139b3 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 4f   called..**.** O
139b4 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  pen the journal 
139b5 66 69 6c 65 20 66 6f 72 20 70 61 67 65 72 20 70  file for pager p
139b6 50 61 67 65 72 20 61 6e 64 20 77 72 69 74 65 20  Pager and write 
139b7 61 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  a journal header
139b8 0a 2a 2a 20 74 6f 20 74 68 65 20 73 74 61 72 74  .** to the start
139b9 20 6f 66 20 69 74 2e 20 49 66 20 74 68 65 72 65   of it. If there
139ba 20 61 72 65 20 61 63 74 69 76 65 20 73 61 76 65   are active save
139bb 70 6f 69 6e 74 73 2c 20 6f 70 65 6e 20 74 68 65  points, open the
139bc 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 0a 2a 2a 20   sub-journal.** 
139bd 61 73 20 77 65 6c 6c 2e 20 54 68 69 73 20 66 75  as well. This fu
139be 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75  nction is only u
139bf 73 65 64 20 77 68 65 6e 20 74 68 65 20 6a 6f 75  sed when the jou
139c0 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 62 65 69  rnal file is bei
139c1 6e 67 20 0a 2a 2a 20 6f 70 65 6e 65 64 20 74 6f  ng .** opened to
139c2 20 77 72 69 74 65 20 61 20 72 6f 6c 6c 62 61 63   write a rollbac
139c3 6b 20 6c 6f 67 20 66 6f 72 20 61 20 74 72 61 6e  k log for a tran
139c4 73 61 63 74 69 6f 6e 2e 20 49 74 20 69 73 20 6e  saction. It is n
139c5 6f 74 20 75 73 65 64 20 0a 2a 2a 20 77 68 65 6e  ot used .** when
139c6 20 6f 70 65 6e 69 6e 67 20 61 20 68 6f 74 20 6a   opening a hot j
139c7 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74 6f 20 72  ournal file to r
139c8 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 0a 2a 2a 0a  oll it back..**.
139c9 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61  ** If the journa
139ca 6c 20 66 69 6c 65 20 69 73 20 61 6c 72 65 61 64  l file is alread
139cb 79 20 6f 70 65 6e 20 28 61 73 20 69 74 20 6d 61  y open (as it ma
139cc 79 20 62 65 20 69 6e 20 65 78 63 6c 75 73 69 76  y be in exclusiv
139cd 65 20 6d 6f 64 65 29 2c 0a 2a 2a 20 74 68 65 6e  e mode),.** then
139ce 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6a   this function j
139cf 75 73 74 20 77 72 69 74 65 73 20 61 20 6a 6f 75  ust writes a jou
139d0 72 6e 61 6c 20 68 65 61 64 65 72 20 74 6f 20 74  rnal header to t
139d1 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 0a  he start of the.
139d2 2a 2a 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 20  ** already open 
139d3 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 57 68 65  file. .**.** Whe
139d4 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
139d5 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
139d6 6f 70 65 6e 65 64 20 62 79 20 74 68 69 73 20 66  opened by this f
139d7 75 6e 63 74 69 6f 6e 2c 20 74 68 65 0a 2a 2a 20  unction, the.** 
139d8 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e 61 6c  Pager.pInJournal
139d9 20 62 69 74 76 65 63 20 73 74 72 75 63 74 75 72   bitvec structur
139da 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2e 0a  e is allocated..
139db 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c  **.** Return SQL
139dc 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74  ITE_OK if everyt
139dd 68 69 6e 67 20 69 73 20 73 75 63 63 65 73 73 66  hing is successf
139de 75 6c 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 72  ul. Otherwise, r
139df 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54 45  eturn .** SQLITE
139e0 5f 4e 4f 4d 45 4d 20 69 66 20 74 68 65 20 61 74  _NOMEM if the at
139e1 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74  tempt to allocat
139e2 65 20 50 61 67 65 72 2e 70 49 6e 4a 6f 75 72 6e  e Pager.pInJourn
139e3 61 6c 20 66 61 69 6c 73 2c 20 6f 72 20 0a 2a 2a  al fails, or .**
139e4 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64   an IO error cod
139e5 65 20 69 66 20 6f 70 65 6e 69 6e 67 20 6f 72 20  e if opening or 
139e6 77 72 69 74 69 6e 67 20 74 68 65 20 6a 6f 75 72  writing the jour
139e7 6e 61 6c 20 66 69 6c 65 20 66 61 69 6c 73 2e 0a  nal file fails..
139e8 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
139e9 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c  ger_open_journal
139ea 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
139eb 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
139ec 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 20  TE_OK;          
139ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
139ee 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
139ef 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 20    sqlite3_vfs * 
139f0 63 6f 6e 73 74 20 70 56 66 73 20 3d 20 70 50 61  const pVfs = pPa
139f1 67 65 72 2d 3e 70 56 66 73 3b 20 20 20 2f 2a 20  ger->pVfs;   /* 
139f2 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 76  Local cache of v
139f3 66 73 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 0a 20  fs pointer */.. 
139f4 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
139f5 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52 45  >state>=PAGER_RE
139f6 53 45 52 56 45 44 20 29 3b 0a 20 20 61 73 73 65  SERVED );.  asse
139f7 72 74 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a  rt( pPager->useJ
139f8 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 61 73 73 65  ournal );.  asse
139f9 72 74 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  rt( pPager->jour
139fa 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a  nalMode!=PAGER_J
139fb 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29  OURNALMODE_OFF )
139fc 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
139fd 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d  er->pInJournal==
139fe 30 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 49 66 20  0 );.  .  /* If 
139ff 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 65  already in the e
13a00 72 72 6f 72 20 73 74 61 74 65 2c 20 74 68 69 73  rror state, this
13a01 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e   function is a n
13a02 6f 2d 6f 70 2e 20 20 42 75 74 20 6f 6e 0a 20 20  o-op.  But on.  
13a03 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e  ** the other han
13a04 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  d, this routine 
13a05 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65 64 20  is never called 
13a06 69 66 20 77 65 20 61 72 65 20 61 6c 72 65 61 64  if we are alread
13a07 79 20 69 6e 0a 20 20 2a 2a 20 61 6e 20 65 72 72  y in.  ** an err
13a08 6f 72 20 73 74 61 74 65 2e 20 2a 2f 0a 20 20 69  or state. */.  i
13a09 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d  f( NEVER(pPager-
13a0a 3e 65 72 72 43 6f 64 65 29 20 29 20 72 65 74 75  >errCode) ) retu
13a0b 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  rn pPager->errCo
13a0c 64 65 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20  de;..  /* TODO: 
13a0d 49 73 20 69 74 20 72 65 61 6c 6c 79 20 70 6f 73  Is it really pos
13a0e 73 69 62 6c 65 20 74 6f 20 67 65 74 20 68 65 72  sible to get her
13a0f 65 20 77 69 74 68 20 64 62 53 69 7a 65 56 61 6c  e with dbSizeVal
13a10 69 64 3d 3d 30 3f 20 49 66 20 6e 6f 74 2c 0a 20  id==0? If not,. 
13a11 20 2a 2a 20 74 68 65 20 63 61 6c 6c 20 74 6f 20   ** the call to 
13a12 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 29  PagerPagecount()
13a13 20 63 61 6e 20 62 65 20 72 65 6d 6f 76 65 64 2e   can be removed.
13a14 0a 20 20 2a 2f 0a 20 20 74 65 73 74 63 61 73 65  .  */.  testcase
13a15 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  ( pPager->dbSize
13a16 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 20 73 71  Valid==0 );.  sq
13a17 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f  lite3PagerPageco
13a18 75 6e 74 28 70 50 61 67 65 72 2c 20 30 29 3b 0a  unt(pPager, 0);.
13a19 0a 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f  .  pPager->pInJo
13a1a 75 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 42  urnal = sqlite3B
13a1b 69 74 76 65 63 43 72 65 61 74 65 28 70 50 61 67  itvecCreate(pPag
13a1c 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20 69  er->dbSize);.  i
13a1d 66 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f  f( pPager->pInJo
13a1e 75 72 6e 61 6c 3d 3d 30 20 29 7b 0a 20 20 20 20  urnal==0 ){.    
13a1f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
13a20 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f  MEM;.  }..  /* O
13a21 70 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  pen the journal 
13a22 66 69 6c 65 20 69 66 20 69 74 20 69 73 20 6e 6f  file if it is no
13a23 74 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 2e 20  t already open. 
13a24 2a 2f 0a 20 20 69 66 28 20 21 69 73 4f 70 65 6e  */.  if( !isOpen
13a25 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b  (pPager->jfd) ){
13a26 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
13a27 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41  >journalMode==PA
13a28 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
13a29 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20 20  MEMORY ){.      
13a2a 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61  sqlite3MemJourna
13a2b 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  lOpen(pPager->jf
13a2c 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  d);.    }else{. 
13a2d 20 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 66       const int f
13a2e 6c 61 67 73 20 3d 20 20 20 20 20 20 20 20 20 20  lags =          
13a2f 20 20 20 20 20 20 20 20 20 2f 2a 20 56 46 53 20           /* VFS 
13a30 66 6c 61 67 73 20 74 6f 20 6f 70 65 6e 20 6a 6f  flags to open jo
13a31 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20  urnal file */.  
13a32 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
13a33 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49  N_READWRITE|SQLI
13a34 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 0a  TE_OPEN_CREATE|.
13a35 20 20 20 20 20 20 20 20 28 70 50 61 67 65 72 2d          (pPager-
13a36 3e 74 65 6d 70 46 69 6c 65 20 3f 20 0a 20 20 20  >tempFile ? .   
13a37 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f         (SQLITE_O
13a38 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53  PEN_DELETEONCLOS
13a39 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45  E|SQLITE_OPEN_TE
13a3a 4d 50 5f 4a 4f 55 52 4e 41 4c 29 3a 0a 20 20 20  MP_JOURNAL):.   
13a3b 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4f         (SQLITE_O
13a3c 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c  PEN_MAIN_JOURNAL
13a3d 29 0a 20 20 20 20 20 20 20 20 29 3b 0a 23 69 66  ).        );.#if
13a3e 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
13a3f 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20  E_ATOMIC_WRITE. 
13a40 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
13a41 33 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a 20 20  3JournalOpen(.  
13a42 20 20 20 20 20 20 20 20 70 56 66 73 2c 20 70 50          pVfs, pP
13a43 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20  ager->zJournal, 
13a44 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c 61  pPager->jfd, fla
13a45 67 73 2c 20 6a 72 6e 6c 42 75 66 66 65 72 53 69  gs, jrnlBufferSi
13a46 7a 65 28 70 50 61 67 65 72 29 0a 20 20 20 20 20  ze(pPager).     
13a47 20 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20   );.#else.      
13a48 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f 70  rc = sqlite3OsOp
13a49 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72 2d  en(pVfs, pPager-
13a4a 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65  >zJournal, pPage
13a4b 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20 30  r->jfd, flags, 0
13a4c 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a  );.#endif.    }.
13a4d 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d      assert( rc!=
13a4e 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 69 73 4f  SQLITE_OK || isO
13a4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
13a50 20 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 57   );.  }...  /* W
13a51 72 69 74 65 20 74 68 65 20 66 69 72 73 74 20 6a  rite the first j
13a52 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20 74 6f  ournal header to
13a53 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
13a54 65 20 61 6e 64 20 6f 70 65 6e 20 0a 20 20 2a 2a  e and open .  **
13a55 20 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c   the sub-journal
13a56 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 20   if necessary.. 
13a57 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
13a58 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f  LITE_OK ){.    /
13a59 2a 20 54 4f 44 4f 3a 20 43 68 65 63 6b 20 69 66  * TODO: Check if
13a5a 20 61 6c 6c 20 6f 66 20 74 68 65 73 65 20 61 72   all of these ar
13a5b 65 20 72 65 61 6c 6c 79 20 72 65 71 75 69 72 65  e really require
13a5c 64 2e 20 2a 2f 0a 20 20 20 20 70 50 61 67 65 72  d. */.    pPager
13a5d 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 3d 20 70  ->dbOrigSize = p
13a5e 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20  Pager->dbSize;. 
13a5f 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
13a60 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a 20  alStarted = 0;. 
13a61 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53     pPager->needS
13a62 79 6e 63 20 3d 20 30 3b 0a 20 20 20 20 70 50 61  ync = 0;.    pPa
13a63 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20  ger->nRec = 0;. 
13a64 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
13a65 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 70  alOff = 0;.    p
13a66 50 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72  Pager->setMaster
13a67 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72   = 0;.    pPager
13a68 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 20 3d 20 30  ->journalHdr = 0
13a69 3b 0a 20 20 20 20 72 63 20 3d 20 77 72 69 74 65  ;.    rc = write
13a6a 4a 6f 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65  JournalHdr(pPage
13a6b 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  r);.  }.  if( rc
13a6c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
13a6d 50 61 67 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e  Pager->nSavepoin
13a6e 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6f 70  t ){.    rc = op
13a6f 65 6e 53 75 62 4a 6f 75 72 6e 61 6c 28 70 50 61  enSubJournal(pPa
13a70 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  ger);.  }..  if(
13a71 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
13a72 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69 74  {.    sqlite3Bit
13a73 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65  vecDestroy(pPage
13a74 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a  r->pInJournal);.
13a75 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a      pPager->pInJ
13a76 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 7d 0a  ournal = 0;.  }.
13a77 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
13a78 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 61 20 77 72  /*.** Begin a wr
13a79 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20  ite-transaction 
13a7a 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69 65 64  on the specified
13a7b 20 70 61 67 65 72 20 6f 62 6a 65 63 74 2e 20 49   pager object. I
13a7c 66 20 61 20 0a 2a 2a 20 77 72 69 74 65 2d 74 72  f a .** write-tr
13a7d 61 6e 73 61 63 74 69 6f 6e 20 68 61 73 20 61 6c  ansaction has al
13a7e 72 65 61 64 79 20 62 65 65 6e 20 6f 70 65 6e 65  ready been opene
13a7f 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  d, this function
13a80 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a   is a no-op..**.
13a81 2a 2a 20 49 66 20 74 68 65 20 65 78 46 6c 61 67  ** If the exFlag
13a82 20 61 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c   argument is fal
13a83 73 65 2c 20 74 68 65 6e 20 61 63 71 75 69 72 65  se, then acquire
13a84 20 61 74 20 6c 65 61 73 74 20 61 20 52 45 53 45   at least a RESE
13a85 52 56 45 44 0a 2a 2a 20 6c 6f 63 6b 20 6f 6e 20  RVED.** lock on 
13a86 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
13a87 65 2e 20 49 66 20 65 78 46 6c 61 67 20 69 73 20  e. If exFlag is 
13a88 74 72 75 65 2c 20 74 68 65 6e 20 61 63 71 75 69  true, then acqui
13a89 72 65 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 61  re at least.** a
13a8a 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  n EXCLUSIVE lock
13a8b 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b  . If such a lock
13a8c 20 69 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64   is already held
13a8d 2c 20 6e 6f 20 6c 6f 63 6b 69 6e 67 20 0a 2a 2a  , no locking .**
13a8e 20 66 75 6e 63 74 69 6f 6e 73 20 6e 65 65 64 20   functions need 
13a8f 62 65 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a  be called..**.**
13a90 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20   If this is not 
13a91 61 20 74 65 6d 70 6f 72 61 72 79 20 6f 72 20 69  a temporary or i
13a92 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 20 61 6e  n-memory file an
13a93 64 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  d, the journal f
13a94 69 6c 65 20 69 73 20 0a 2a 2a 20 6f 70 65 6e 65  ile is .** opene
13a95 64 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20  d if it has not 
13a96 62 65 65 6e 20 61 6c 72 65 61 64 79 2e 20 46 6f  been already. Fo
13a97 72 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69  r a temporary fi
13a98 6c 65 2c 20 74 68 65 20 6f 70 65 6e 69 6e 67 20  le, the opening 
13a99 0a 2a 2a 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  .** of the journ
13a9a 61 6c 20 66 69 6c 65 20 69 73 20 64 65 66 65 72  al file is defer
13a9b 72 65 64 20 75 6e 74 69 6c 20 74 68 65 72 65 20  red until there 
13a9c 69 73 20 61 6e 20 61 63 74 75 61 6c 20 6e 65 65  is an actual nee
13a9d 64 20 74 6f 20 0a 2a 2a 20 77 72 69 74 65 20 74  d to .** write t
13a9e 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 54  o the journal. T
13a9f 4f 44 4f 3a 20 57 68 79 20 68 61 6e 64 6c 65 20  ODO: Why handle 
13aa0 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 20  temporary files 
13aa1 64 69 66 66 65 72 65 6e 74 6c 79 3f 0a 2a 2a 0a  differently?.**.
13aa2 2a 2a 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61  ** If the journa
13aa3 6c 20 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64  l file is opened
13aa4 20 28 6f 72 20 69 66 20 69 74 20 69 73 20 61 6c   (or if it is al
13aa5 72 65 61 64 79 20 6f 70 65 6e 29 2c 20 74 68 65  ready open), the
13aa6 6e 20 61 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 68  n a.** journal-h
13aa7 65 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e  eader is written
13aa8 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f 66   to the start of
13aa9 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68   it..**.** If th
13aaa 65 20 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 61  e subjInMemory a
13aab 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 6e 2d 7a  rgument is non-z
13aac 65 72 6f 2c 20 74 68 65 6e 20 61 6e 79 20 73 75  ero, then any su
13aad 62 2d 6a 6f 75 72 6e 61 6c 20 6f 70 65 6e 65 64  b-journal opened
13aae 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 69 73 20  .** within this 
13aaf 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c  transaction will
13ab0 20 62 65 20 6f 70 65 6e 65 64 20 61 73 20 61 6e   be opened as an
13ab1 20 69 6e 2d 6d 65 6d 6f 72 79 20 66 69 6c 65 2e   in-memory file.
13ab2 20 54 68 69 73 0a 2a 2a 20 68 61 73 20 6e 6f 20   This.** has no 
13ab3 65 66 66 65 63 74 20 69 66 20 74 68 65 20 73 75  effect if the su
13ab4 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c 72  b-journal is alr
13ab5 65 61 64 79 20 6f 70 65 6e 65 64 20 28 61 73 20  eady opened (as 
13ab6 69 74 20 6d 61 79 20 62 65 20 77 68 65 6e 0a 2a  it may be when.*
13ab7 2a 20 72 75 6e 6e 69 6e 67 20 69 6e 20 65 78 63  * running in exc
13ab8 6c 75 73 69 76 65 20 6d 6f 64 65 29 20 6f 72 20  lusive mode) or 
13ab9 69 66 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  if the transacti
13aba 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 72 65 71 75  on does not requ
13abb 69 72 65 20 61 0a 2a 2a 20 73 75 62 2d 6a 6f 75  ire a.** sub-jou
13abc 72 6e 61 6c 2e 20 49 66 20 74 68 65 20 73 75 62  rnal. If the sub
13abd 6a 49 6e 4d 65 6d 6f 72 79 20 61 72 67 75 6d 65  jInMemory argume
13abe 6e 74 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e  nt is zero, then
13abf 20 61 6e 79 20 72 65 71 75 69 72 65 64 0a 2a 2a   any required.**
13ac0 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20   sub-journal is 
13ac1 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e 2d 6d  implemented in-m
13ac2 65 6d 6f 72 79 20 69 66 20 70 50 61 67 65 72 20  emory if pPager 
13ac3 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  is an in-memory 
13ac4 64 61 74 61 62 61 73 65 2c 20 0a 2a 2a 20 6f 72  database, .** or
13ac5 20 75 73 69 6e 67 20 61 20 74 65 6d 70 6f 72 61   using a tempora
13ac6 72 79 20 66 69 6c 65 20 6f 74 68 65 72 77 69 73  ry file otherwis
13ac7 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
13ac8 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
13ac9 50 61 67 65 72 42 65 67 69 6e 28 50 61 67 65 72  PagerBegin(Pager
13aca 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 65 78   *pPager, int ex
13acb 46 6c 61 67 2c 20 69 6e 74 20 73 75 62 6a 49 6e  Flag, int subjIn
13acc 4d 65 6d 6f 72 79 29 7b 0a 20 20 69 6e 74 20 72  Memory){.  int r
13acd 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
13ace 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
13acf 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e  >state!=PAGER_UN
13ad0 4c 4f 43 4b 20 29 3b 0a 20 20 70 50 61 67 65 72  LOCK );.  pPager
13ad1 2d 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 20 3d  ->subjInMemory =
13ad2 20 28 75 38 29 73 75 62 6a 49 6e 4d 65 6d 6f 72   (u8)subjInMemor
13ad3 79 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  y;.  if( pPager-
13ad4 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 53 48  >state==PAGER_SH
13ad5 41 52 45 44 20 29 7b 0a 20 20 20 20 61 73 73 65  ARED ){.    asse
13ad6 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a  rt( pPager->pInJ
13ad7 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a 20 20 20  ournal==0 );.   
13ad8 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20   assert( !MEMDB 
13ad9 26 26 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70  && !pPager->temp
13ada 46 69 6c 65 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  File );..    /* 
13adb 4f 62 74 61 69 6e 20 61 20 52 45 53 45 52 56 45  Obtain a RESERVE
13adc 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61  D lock on the da
13add 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49 66 20  tabase file. If 
13ade 74 68 65 20 65 78 46 6c 61 67 20 70 61 72 61 6d  the exFlag param
13adf 65 74 65 72 0a 20 20 20 20 2a 2a 20 69 73 20 74  eter.    ** is t
13ae0 72 75 65 2c 20 74 68 65 6e 20 69 6d 6d 65 64 69  rue, then immedi
13ae1 61 74 65 6c 79 20 75 70 67 72 61 64 65 20 74 68  ately upgrade th
13ae2 69 73 20 74 6f 20 61 6e 20 45 58 43 4c 55 53 49  is to an EXCLUSI
13ae3 56 45 20 6c 6f 63 6b 2e 20 54 68 65 0a 20 20 20  VE lock. The.   
13ae4 20 2a 2a 20 62 75 73 79 2d 68 61 6e 64 6c 65 72   ** busy-handler
13ae5 20 63 61 6c 6c 62 61 63 6b 20 63 61 6e 20 62 65   callback can be
13ae6 20 75 73 65 64 20 77 68 65 6e 20 75 70 67 72 61   used when upgra
13ae7 64 69 6e 67 20 74 6f 20 74 68 65 20 45 58 43 4c  ding to the EXCL
13ae8 55 53 49 56 45 0a 20 20 20 20 2a 2a 20 6c 6f 63  USIVE.    ** loc
13ae9 6b 2c 20 62 75 74 20 6e 6f 74 20 77 68 65 6e 20  k, but not when 
13aea 6f 62 74 61 69 6e 69 6e 67 20 74 68 65 20 52 45  obtaining the RE
13aeb 53 45 52 56 45 44 20 6c 6f 63 6b 2e 0a 20 20 20  SERVED lock..   
13aec 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   */.    rc = sql
13aed 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65  ite3OsLock(pPage
13aee 72 2d 3e 66 64 2c 20 52 45 53 45 52 56 45 44 5f  r->fd, RESERVED_
13aef 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28 20 72  LOCK);.    if( r
13af0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
13af1 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74        pPager->st
13af2 61 74 65 20 3d 20 50 41 47 45 52 5f 52 45 53 45  ate = PAGER_RESE
13af3 52 56 45 44 3b 0a 20 20 20 20 20 20 69 66 28 20  RVED;.      if( 
13af4 65 78 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20  exFlag ){.      
13af5 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69    rc = pager_wai
13af6 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72  t_on_lock(pPager
13af7 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  , EXCLUSIVE_LOCK
13af8 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
13af9 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  ..    /* If the 
13afa 72 65 71 75 69 72 65 64 20 6c 6f 63 6b 73 20 77  required locks w
13afb 65 72 65 20 73 75 63 63 65 73 73 66 75 6c 6c 79  ere successfully
13afc 20 6f 62 74 61 69 6e 65 64 2c 20 6f 70 65 6e 20   obtained, open 
13afd 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20  the journal.    
13afe 2a 2a 20 66 69 6c 65 20 61 6e 64 20 77 72 69 74  ** file and writ
13aff 65 20 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72  e the first jour
13b00 6e 61 6c 2d 68 65 61 64 65 72 20 74 6f 20 69 74  nal-header to it
13b01 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
13b02 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
13b03 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  & pPager->journa
13b04 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55  lMode!=PAGER_JOU
13b05 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b 0a  RNALMODE_OFF ){.
13b06 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
13b07 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50  _open_journal(pP
13b08 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ager);.    }.  }
13b09 65 6c 73 65 20 69 66 28 20 69 73 4f 70 65 6e 28  else if( isOpen(
13b0a 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20  pPager->jfd) && 
13b0b 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
13b0c 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  ff==0 ){.    /* 
13b0d 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 65  This happens whe
13b0e 6e 20 74 68 65 20 70 61 67 65 72 20 77 61 73 20  n the pager was 
13b0f 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 61 63 63  in exclusive-acc
13b10 65 73 73 20 6d 6f 64 65 20 74 68 65 20 6c 61 73  ess mode the las
13b11 74 0a 20 20 20 20 2a 2a 20 74 69 6d 65 20 61 20  t.    ** time a 
13b12 28 72 65 61 64 20 6f 72 20 77 72 69 74 65 29 20  (read or write) 
13b13 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20  transaction was 
13b14 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 6e  successfully con
13b15 63 6c 75 64 65 64 0a 20 20 20 20 2a 2a 20 62 79  cluded.    ** by
13b16 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e   this connection
13b17 2e 20 49 6e 73 74 65 61 64 20 6f 66 20 64 65 6c  . Instead of del
13b18 65 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61  eting the journa
13b19 6c 20 66 69 6c 65 20 69 74 20 77 61 73 20 0a 20  l file it was . 
13b1a 20 20 20 2a 2a 20 6b 65 70 74 20 6f 70 65 6e 20     ** kept open 
13b1b 61 6e 64 20 65 69 74 68 65 72 20 77 61 73 20 74  and either was t
13b1c 72 75 6e 63 61 74 65 64 20 74 6f 20 30 20 62 79  runcated to 0 by
13b1d 74 65 73 20 6f 72 20 69 74 73 20 68 65 61 64 65  tes or its heade
13b1e 72 20 77 61 73 0a 20 20 20 20 2a 2a 20 6f 76 65  r was.    ** ove
13b1f 72 77 72 69 74 74 65 6e 20 77 69 74 68 20 7a 65  rwritten with ze
13b20 72 6f 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ros..    */.    
13b21 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
13b22 6e 52 65 63 3d 3d 30 20 29 3b 0a 20 20 20 20 61  nRec==0 );.    a
13b23 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64  ssert( pPager->d
13b24 62 4f 72 69 67 53 69 7a 65 3d 3d 30 20 29 3b 0a  bOrigSize==0 );.
13b25 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
13b26 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d  er->pInJournal==
13b27 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 61  0 );.    rc = pa
13b28 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c  ger_open_journal
13b29 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20  (pPager);.  }.. 
13b2a 20 50 41 47 45 52 54 52 41 43 45 28 28 22 54 52   PAGERTRACE(("TR
13b2b 41 4e 53 41 43 54 49 4f 4e 20 25 64 5c 6e 22 2c  ANSACTION %d\n",
13b2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
13b2d 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 69  ));.  assert( !i
13b2e 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
13b2f 64 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f  d) || pPager->jo
13b30 75 72 6e 61 6c 4f 66 66 3e 30 20 7c 7c 20 72 63  urnalOff>0 || rc
13b31 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20  !=SQLITE_OK );. 
13b32 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
13b33 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  OK ){.    assert
13b34 28 20 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64  ( !pPager->dbMod
13b35 69 66 69 65 64 20 29 3b 0a 20 20 20 20 2f 2a 20  ified );.    /* 
13b36 49 67 6e 6f 72 65 20 61 6e 79 20 49 4f 20 65 72  Ignore any IO er
13b37 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72 73 20  ror that occurs 
13b38 77 69 74 68 69 6e 20 70 61 67 65 72 5f 65 6e 64  within pager_end
13b39 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 2e 20  _transaction(). 
13b3a 54 68 65 0a 20 20 20 20 2a 2a 20 70 75 72 70 6f  The.    ** purpo
13b3b 73 65 20 6f 66 20 74 68 69 73 20 63 61 6c 6c 20  se of this call 
13b3c 69 73 20 74 6f 20 72 65 73 65 74 20 74 68 65 20  is to reset the 
13b3d 69 6e 74 65 72 6e 61 6c 20 73 74 61 74 65 20 6f  internal state o
13b3e 66 20 74 68 65 20 70 61 67 65 72 0a 20 20 20 20  f the pager.    
13b3f 2a 2a 20 73 75 62 2d 73 79 73 74 65 6d 2e 20 49  ** sub-system. I
13b40 74 20 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72  t doesn't matter
13b41 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d   if the journal-
13b42 66 69 6c 65 20 69 73 20 6e 6f 74 20 70 72 6f 70  file is not prop
13b43 65 72 6c 79 0a 20 20 20 20 2a 2a 20 66 69 6e 61  erly.    ** fina
13b44 6c 69 7a 65 64 20 61 74 20 74 68 69 73 20 70 6f  lized at this po
13b45 69 6e 74 20 28 73 69 6e 63 65 20 69 74 20 69 73  int (since it is
13b46 20 6e 6f 74 20 61 20 76 61 6c 69 64 20 6a 6f 75   not a valid jou
13b47 72 6e 61 6c 20 66 69 6c 65 20 61 6e 79 77 61 79  rnal file anyway
13b48 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 61  )..    */.    pa
13b49 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74  ger_end_transact
13b4a 69 6f 6e 28 70 50 61 67 65 72 2c 20 30 29 3b 0a  ion(pPager, 0);.
13b4b 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
13b4c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61  .}../*.** Mark a
13b4d 20 73 69 6e 67 6c 65 20 64 61 74 61 20 70 61 67   single data pag
13b4e 65 20 61 73 20 77 72 69 74 65 61 62 6c 65 2e 20  e as writeable. 
13b4f 54 68 65 20 70 61 67 65 20 69 73 20 77 72 69 74  The page is writ
13b50 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 0a 2a 2a  ten into the .**
13b51 20 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72   main journal or
13b52 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 20 61 73 20   sub-journal as 
13b53 72 65 71 75 69 72 65 64 2e 20 49 66 20 74 68 65  required. If the
13b54 20 70 61 67 65 20 69 73 20 77 72 69 74 74 65 6e   page is written
13b55 20 69 6e 74 6f 0a 2a 2a 20 6f 6e 65 20 6f 66 20   into.** one of 
13b56 74 68 65 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68  the journals, th
13b57 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
13b58 62 69 74 20 69 73 20 73 65 74 20 69 6e 20 74 68  bit is set in th
13b59 65 20 0a 2a 2a 20 50 61 67 65 72 2e 70 49 6e 4a  e .** Pager.pInJ
13b5a 6f 75 72 6e 61 6c 20 62 69 74 76 65 63 20 61 6e  ournal bitvec an
13b5b 64 20 74 68 65 20 50 61 67 65 72 53 61 76 65 70  d the PagerSavep
13b5c 6f 69 6e 74 2e 70 49 6e 53 61 76 65 70 6f 69 6e  oint.pInSavepoin
13b5d 74 20 62 69 74 76 65 63 73 0a 2a 2a 20 6f 66 20  t bitvecs.** of 
13b5e 61 6e 79 20 6f 70 65 6e 20 73 61 76 65 70 6f 69  any open savepoi
13b5f 6e 74 73 20 61 73 20 61 70 70 72 6f 70 72 69 61  nts as appropria
13b60 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  te..*/.static in
13b61 74 20 70 61 67 65 72 5f 77 72 69 74 65 28 50 67  t pager_write(Pg
13b62 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 76 6f 69  Hdr *pPg){.  voi
13b63 64 20 2a 70 44 61 74 61 20 3d 20 70 50 67 2d 3e  d *pData = pPg->
13b64 70 44 61 74 61 3b 0a 20 20 50 61 67 65 72 20 2a  pData;.  Pager *
13b65 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50  pPager = pPg->pP
13b66 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d  ager;.  int rc =
13b67 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f   SQLITE_OK;..  /
13b68 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
13b69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 20 75 6e 6c  s not called unl
13b6a 65 73 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ess a transactio
13b6b 6e 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  n has already be
13b6c 65 6e 0a 20 20 2a 2a 20 73 74 61 72 74 65 64 2e  en.  ** started.
13b6d 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
13b6e 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50  pPager->state>=P
13b6f 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b  AGER_RESERVED );
13b70 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72  ..  /* If an err
13b71 6f 72 20 68 61 73 20 62 65 65 6e 20 70 72 65 76  or has been prev
13b72 69 6f 75 73 6c 79 20 64 65 74 65 63 74 65 64 2c  iously detected,
13b73 20 77 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62   we should not b
13b74 65 0a 20 20 2a 2a 20 63 61 6c 6c 69 6e 67 20 74  e.  ** calling t
13b75 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 52 65  his routine.  Re
13b76 70 65 61 74 20 74 68 65 20 65 72 72 6f 72 20 66  peat the error f
13b77 6f 72 20 72 6f 62 75 73 74 6e 65 73 73 2e 0a 20  or robustness.. 
13b78 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45 52 28   */.  if( NEVER(
13b79 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 29  pPager->errCode)
13b7a 20 29 20 20 72 65 74 75 72 6e 20 70 50 61 67 65   )  return pPage
13b7b 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 0a 20 20 2f  r->errCode;..  /
13b7c 2a 20 48 69 67 68 65 72 2d 6c 65 76 65 6c 20 72  * Higher-level r
13b7d 6f 75 74 69 6e 65 73 20 6e 65 76 65 72 20 63 61  outines never ca
13b7e 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ll this function
13b7f 20 69 66 20 64 61 74 61 62 61 73 65 20 69 73 20   if database is 
13b80 6e 6f 74 0a 20 20 2a 2a 20 77 72 69 74 61 62 6c  not.  ** writabl
13b81 65 2e 20 20 42 75 74 20 63 68 65 63 6b 20 61 6e  e.  But check an
13b82 79 77 61 79 2c 20 6a 75 73 74 20 66 6f 72 20 72  yway, just for r
13b83 6f 62 75 73 74 6e 65 73 73 2e 20 2a 2f 0a 20 20  obustness. */.  
13b84 69 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72  if( NEVER(pPager
13b85 2d 3e 72 65 61 64 4f 6e 6c 79 29 20 29 20 72 65  ->readOnly) ) re
13b86 74 75 72 6e 20 53 51 4c 49 54 45 5f 50 45 52 4d  turn SQLITE_PERM
13b87 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 21 70 50  ;..  assert( !pP
13b88 61 67 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20  ager->setMaster 
13b89 29 3b 0a 0a 20 20 43 48 45 43 4b 5f 50 41 47 45  );..  CHECK_PAGE
13b8a 28 70 50 67 29 3b 0a 0a 20 20 2f 2a 20 4d 61 72  (pPg);..  /* Mar
13b8b 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 64 69  k the page as di
13b8c 72 74 79 2e 20 20 49 66 20 74 68 65 20 70 61 67  rty.  If the pag
13b8d 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  e has already be
13b8e 65 6e 20 77 72 69 74 74 65 6e 0a 20 20 2a 2a 20  en written.  ** 
13b8f 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74  to the journal t
13b90 68 65 6e 20 77 65 20 63 61 6e 20 72 65 74 75 72  hen we can retur
13b91 6e 20 72 69 67 68 74 20 61 77 61 79 2e 0a 20 20  n right away..  
13b92 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63  */.  sqlite3Pcac
13b93 68 65 4d 61 6b 65 44 69 72 74 79 28 70 50 67 29  heMakeDirty(pPg)
13b94 3b 0a 20 20 69 66 28 20 70 61 67 65 49 6e 4a 6f  ;.  if( pageInJo
13b95 75 72 6e 61 6c 28 70 50 67 29 20 26 26 20 21 73  urnal(pPg) && !s
13b96 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 65 28  ubjRequiresPage(
13b97 70 50 67 29 20 29 7b 0a 20 20 20 20 70 50 61 67  pPg) ){.    pPag
13b98 65 72 2d 3e 64 62 4d 6f 64 69 66 69 65 64 20 3d  er->dbModified =
13b99 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 0a 20 20   1;.  }else{..  
13b9a 20 20 2f 2a 20 49 66 20 77 65 20 67 65 74 20 74    /* If we get t
13b9b 68 69 73 20 66 61 72 2c 20 69 74 20 6d 65 61 6e  his far, it mean
13b9c 73 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20  s that the page 
13b9d 6e 65 65 64 73 20 74 6f 20 62 65 0a 20 20 20 20  needs to be.    
13b9e 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68  ** written to th
13b9f 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f  e transaction jo
13ba0 75 72 6e 61 6c 20 6f 72 20 74 68 65 20 63 6b 65  urnal or the cke
13ba1 63 6b 70 6f 69 6e 74 20 6a 6f 75 72 6e 61 6c 0a  ckpoint journal.
13ba2 20 20 20 20 2a 2a 20 6f 72 20 62 6f 74 68 2e 0a      ** or both..
13ba3 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 48 69      **.    ** Hi
13ba4 67 68 65 72 20 6c 65 76 65 6c 20 72 6f 75 74 69  gher level routi
13ba5 6e 65 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20  nes should have 
13ba6 61 6c 72 65 61 64 79 20 73 74 61 72 74 65 64 20  already started 
13ba7 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 20  a transaction,. 
13ba8 20 20 20 2a 2a 20 77 68 69 63 68 20 6d 65 61 6e     ** which mean
13ba9 73 20 74 68 65 79 20 68 61 76 65 20 61 63 71 75  s they have acqu
13baa 69 72 65 64 20 74 68 65 20 6e 65 63 65 73 73 61  ired the necessa
13bab 72 79 20 6c 6f 63 6b 73 20 61 6e 64 20 6f 70 65  ry locks and ope
13bac 6e 65 64 0a 20 20 20 20 2a 2a 20 61 20 72 6f 6c  ned.    ** a rol
13bad 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20  lback journal.  
13bae 44 6f 75 62 6c 65 2d 63 68 65 63 6b 20 74 6f 20  Double-check to 
13baf 6d 61 6b 65 73 20 73 75 72 65 20 74 68 69 73 20  makes sure this 
13bb0 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20 20 20  is the case..   
13bb1 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   */.    rc = sql
13bb2 69 74 65 33 50 61 67 65 72 42 65 67 69 6e 28 70  ite3PagerBegin(p
13bb3 50 61 67 65 72 2c 20 30 2c 20 70 50 61 67 65 72  Pager, 0, pPager
13bb4 2d 3e 73 75 62 6a 49 6e 4d 65 6d 6f 72 79 29 3b  ->subjInMemory);
13bb5 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72  .    if( NEVER(r
13bb6 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b  c!=SQLITE_OK) ){
13bb7 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
13bb8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
13bb9 21 69 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e  !isOpen(pPager->
13bba 6a 66 64 29 20 26 26 20 70 50 61 67 65 72 2d 3e  jfd) && pPager->
13bbb 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47  journalMode!=PAG
13bbc 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f  ER_JOURNALMODE_O
13bbd 46 46 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65  FF ){.      asse
13bbe 72 74 28 20 70 50 61 67 65 72 2d 3e 75 73 65 4a  rt( pPager->useJ
13bbf 6f 75 72 6e 61 6c 20 29 3b 0a 20 20 20 20 20 20  ournal );.      
13bc0 72 63 20 3d 20 70 61 67 65 72 5f 6f 70 65 6e 5f  rc = pager_open_
13bc1 6a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29 3b  journal(pPager);
13bc2 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
13bc3 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
13bc4 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
13bc5 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69  pPager->dbModifi
13bc6 65 64 20 3d 20 31 3b 0a 20 20 0a 20 20 20 20 2f  ed = 1;.  .    /
13bc7 2a 20 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f  * The transactio
13bc8 6e 20 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65 78  n journal now ex
13bc9 69 73 74 73 20 61 6e 64 20 77 65 20 68 61 76 65  ists and we have
13bca 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20 61   a RESERVED or a
13bcb 6e 0a 20 20 20 20 2a 2a 20 45 58 43 4c 55 53 49  n.    ** EXCLUSI
13bcc 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 6d  VE lock on the m
13bcd 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c  ain database fil
13bce 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 63 75  e.  Write the cu
13bcf 72 72 65 6e 74 20 70 61 67 65 20 74 6f 0a 20 20  rrent page to.  
13bd0 20 20 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63    ** the transac
13bd1 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 66 20  tion journal if 
13bd2 69 74 20 69 73 20 6e 6f 74 20 74 68 65 72 65 20  it is not there 
13bd3 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a  already..    */.
13bd4 20 20 20 20 69 66 28 20 21 70 61 67 65 49 6e 4a      if( !pageInJ
13bd5 6f 75 72 6e 61 6c 28 70 50 67 29 20 26 26 20 69  ournal(pPg) && i
13bd6 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66  sOpen(pPager->jf
13bd7 64 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  d) ){.      if( 
13bd8 70 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65  pPg->pgno<=pPage
13bd9 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 20 29 7b  r->dbOrigSize ){
13bda 0a 20 20 20 20 20 20 20 20 75 33 32 20 63 6b 73  .        u32 cks
13bdb 75 6d 3b 0a 20 20 20 20 20 20 20 20 63 68 61 72  um;.        char
13bdc 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 20 20   *pData2;..     
13bdd 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20     /* We should 
13bde 6e 65 76 65 72 20 77 72 69 74 65 20 74 6f 20 74  never write to t
13bdf 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
13be0 74 68 65 20 70 61 67 65 20 74 68 61 74 0a 20 20  the page that.  
13be1 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e        ** contain
13be2 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c  s the database l
13be3 6f 63 6b 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  ocks.  The follo
13be4 77 69 6e 67 20 61 73 73 65 72 74 20 76 65 72 69  wing assert veri
13be5 66 69 65 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  fies.        ** 
13be6 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 2e 20  that we do not. 
13be7 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  */.        asser
13be8 74 28 20 70 50 67 2d 3e 70 67 6e 6f 21 3d 50 41  t( pPg->pgno!=PA
13be9 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67  GER_MJ_PGNO(pPag
13bea 65 72 29 20 29 3b 0a 20 20 20 20 20 20 20 20 43  er) );.        C
13beb 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20 70 44  ODEC2(pPager, pD
13bec 61 74 61 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20  ata, pPg->pgno, 
13bed 37 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  7, return SQLITE
13bee 5f 4e 4f 4d 45 4d 2c 20 70 44 61 74 61 32 29 3b  _NOMEM, pData2);
13bef 0a 20 20 20 20 20 20 20 20 63 6b 73 75 6d 20 3d  .        cksum =
13bf0 20 70 61 67 65 72 5f 63 6b 73 75 6d 28 70 50 61   pager_cksum(pPa
13bf1 67 65 72 2c 20 28 75 38 2a 29 70 44 61 74 61 32  ger, (u8*)pData2
13bf2 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
13bf3 77 72 69 74 65 33 32 62 69 74 73 28 70 50 61 67  write32bits(pPag
13bf4 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d  er->jfd, pPager-
13bf5 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 67  >journalOff, pPg
13bf6 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20  ->pgno);.       
13bf7 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
13bf8 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
13bf9 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72  rc = sqlite3OsWr
13bfa 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ite(pPager->jfd,
13bfb 20 70 44 61 74 61 32 2c 20 70 50 61 67 65 72 2d   pData2, pPager-
13bfc 3e 70 61 67 65 53 69 7a 65 2c 0a 20 20 20 20 20  >pageSize,.     
13bfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13bfe 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d           pPager-
13bff 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 20 34 29  >journalOff + 4)
13c00 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67  ;.          pPag
13c01 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b  er->journalOff +
13c02 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  = pPager->pageSi
13c03 7a 65 2b 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ze+4;.        }.
13c04 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
13c05 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
13c06 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74         rc = writ
13c07 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e  e32bits(pPager->
13c08 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75  jfd, pPager->jou
13c09 72 6e 61 6c 4f 66 66 2c 20 63 6b 73 75 6d 29 3b  rnalOff, cksum);
13c0a 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65  .          pPage
13c0b 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d  r->journalOff +=
13c0c 20 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   4;.        }.  
13c0d 20 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22        IOTRACE(("
13c0e 4a 4f 55 54 20 25 70 20 25 64 20 25 6c 6c 64 20  JOUT %p %d %lld 
13c0f 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70  %d\n", pPager, p
13c10 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 20  Pg->pgno, .     
13c11 20 20 20 20 20 20 20 20 20 20 20 20 70 50 61 67              pPag
13c12 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20  er->journalOff, 
13c13 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
13c14 29 29 3b 0a 20 20 20 20 20 20 20 20 50 41 47 45  ));.        PAGE
13c15 52 5f 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70  R_INCR(sqlite3_p
13c16 61 67 65 72 5f 77 72 69 74 65 6a 5f 63 6f 75 6e  ager_writej_coun
13c17 74 29 3b 0a 20 20 20 20 20 20 20 20 50 41 47 45  t);.        PAGE
13c18 52 54 52 41 43 45 28 28 22 4a 4f 55 52 4e 41 4c  RTRACE(("JOURNAL
13c19 20 25 64 20 70 61 67 65 20 25 64 20 6e 65 65 64   %d page %d need
13c1a 53 79 6e 63 3d 25 64 20 68 61 73 68 28 25 30 38  Sync=%d hash(%08
13c1b 78 29 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  x)\n",.         
13c1c 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61 67      PAGERID(pPag
13c1d 65 72 29 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20  er), pPg->pgno, 
13c1e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 28 28  .             ((
13c1f 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  pPg->flags&PGHDR
13c20 5f 4e 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 29  _NEED_SYNC)?1:0)
13c21 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61 73 68  , pager_pagehash
13c22 28 70 50 67 29 29 29 3b 0a 0a 20 20 20 20 20 20  (pPg)));..      
13c23 20 20 2f 2a 20 45 76 65 6e 20 69 66 20 61 6e 20    /* Even if an 
13c24 49 4f 20 6f 72 20 64 69 73 6b 66 75 6c 6c 20 65  IO or diskfull e
13c25 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 77 68  rror occurred wh
13c26 69 6c 65 20 6a 6f 75 72 6e 61 6c 6c 69 6e 67 20  ile journalling 
13c27 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 70  the.        ** p
13c28 61 67 65 20 69 6e 20 74 68 65 20 62 6c 6f 63 6b  age in the block
13c29 20 61 62 6f 76 65 2c 20 73 65 74 20 74 68 65 20   above, set the 
13c2a 6e 65 65 64 2d 73 79 6e 63 20 66 6c 61 67 20 66  need-sync flag f
13c2b 6f 72 20 74 68 65 20 70 61 67 65 2e 0a 20 20 20  or the page..   
13c2c 20 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73       ** Otherwis
13c2d 65 2c 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e  e, when the tran
13c2e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65  saction is rolle
13c2f 64 20 62 61 63 6b 2c 20 74 68 65 20 6c 6f 67 69  d back, the logi
13c30 63 20 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20  c in.        ** 
13c31 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67  playback_one_pag
13c32 65 28 29 20 77 69 6c 6c 20 74 68 69 6e 6b 20 74  e() will think t
13c33 68 61 74 20 74 68 65 20 70 61 67 65 20 6e 65 65  hat the page nee
13c34 64 73 20 74 6f 20 62 65 20 72 65 73 74 6f 72 65  ds to be restore
13c35 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20  d.        ** in 
13c36 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
13c37 65 2e 20 41 6e 64 20 69 66 20 61 6e 20 49 4f 20  e. And if an IO 
13c38 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77 68 69  error occurs whi
13c39 6c 65 20 64 6f 69 6e 67 20 73 6f 2c 0a 20 20 20  le doing so,.   
13c3a 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 63 6f 72       ** then cor
13c3b 72 75 70 74 69 6f 6e 20 6d 61 79 20 66 6f 6c 6c  ruption may foll
13c3c 6f 77 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ow..        */. 
13c3d 20 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67         if( !pPag
13c3e 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 7b 0a 20 20  er->noSync ){.  
13c3f 20 20 20 20 20 20 20 20 70 50 67 2d 3e 66 6c 61          pPg->fla
13c40 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44  gs |= PGHDR_NEED
13c41 5f 53 59 4e 43 3b 0a 20 20 20 20 20 20 20 20 20  _SYNC;.         
13c42 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
13c43 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  c = 1;.        }
13c44 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20  ..        /* An 
13c45 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72  error has occurr
13c46 65 64 20 77 72 69 74 69 6e 67 20 74 6f 20 74 68  ed writing to th
13c47 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  e journal file. 
13c48 54 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  The .        ** 
13c49 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c  transaction will
13c4a 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20   be rolled back 
13c4b 62 79 20 74 68 65 20 6c 61 79 65 72 20 61 62 6f  by the layer abo
13c4c 76 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ve..        */. 
13c4d 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
13c4e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
13c4f 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
13c50 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  .        }..    
13c51 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63      pPager->nRec
13c52 2b 2b 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  ++;.        asse
13c53 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a  rt( pPager->pInJ
13c54 6f 75 72 6e 61 6c 21 3d 30 20 29 3b 0a 20 20 20  ournal!=0 );.   
13c55 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
13c56 33 42 69 74 76 65 63 53 65 74 28 70 50 61 67 65  3BitvecSet(pPage
13c57 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70  r->pInJournal, p
13c58 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20  Pg->pgno);.     
13c59 20 20 20 74 65 73 74 63 61 73 65 28 20 72 63 3d     testcase( rc=
13c5a 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b  =SQLITE_NOMEM );
13c5b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
13c5c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc==SQLITE_OK |
13c5d 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  | rc==SQLITE_NOM
13c5e 45 4d 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63  EM );.        rc
13c5f 20 7c 3d 20 61 64 64 54 6f 53 61 76 65 70 6f 69   |= addToSavepoi
13c60 6e 74 42 69 74 76 65 63 73 28 70 50 61 67 65 72  ntBitvecs(pPager
13c61 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20  , pPg->pgno);.  
13c62 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
13c63 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
13c64 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d       assert( rc=
13c65 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b  =SQLITE_NOMEM );
13c66 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
13c67 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
13c68 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
13c69 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72       if( !pPager
13c6a 2d 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64  ->journalStarted
13c6b 20 26 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53   && !pPager->noS
13c6c 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ync ){.         
13c6d 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50   pPg->flags |= P
13c6e 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a  GHDR_NEED_SYNC;.
13c6f 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72            pPager
13c70 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a  ->needSync = 1;.
13c71 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
13c72 20 20 50 41 47 45 52 54 52 41 43 45 28 28 22 41    PAGERTRACE(("A
13c73 50 50 45 4e 44 20 25 64 20 70 61 67 65 20 25 64  PPEND %d page %d
13c74 20 6e 65 65 64 53 79 6e 63 3d 25 64 5c 6e 22 2c   needSync=%d\n",
13c75 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
13c76 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
13c77 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 0a 20 20 20  , pPg->pgno,.   
13c78 20 20 20 20 20 20 20 20 20 20 20 20 28 28 70 50              ((pP
13c79 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  g->flags&PGHDR_N
13c7a 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 29 29 29  EED_SYNC)?1:0)))
13c7b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
13c7c 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65    .    /* If the
13c7d 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e   statement journ
13c7e 61 6c 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 74  al is open and t
13c7f 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69  he page is not i
13c80 6e 20 69 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65  n it,.    ** the
13c81 6e 20 77 72 69 74 65 20 74 68 65 20 63 75 72 72  n write the curr
13c82 65 6e 74 20 70 61 67 65 20 74 6f 20 74 68 65 20  ent page to the 
13c83 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61  statement journa
13c84 6c 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a 20 20  l.  Note that.  
13c85 20 20 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65    ** the stateme
13c86 6e 74 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 6d 61  nt journal forma
13c87 74 20 64 69 66 66 65 72 73 20 66 72 6f 6d 20 74  t differs from t
13c88 68 65 20 73 74 61 6e 64 61 72 64 20 6a 6f 75 72  he standard jour
13c89 6e 61 6c 20 66 6f 72 6d 61 74 0a 20 20 20 20 2a  nal format.    *
13c8a 2a 20 69 6e 20 74 68 61 74 20 69 74 20 6f 6d 69  * in that it omi
13c8b 74 73 20 74 68 65 20 63 68 65 63 6b 73 75 6d 73  ts the checksums
13c8c 20 61 6e 64 20 74 68 65 20 68 65 61 64 65 72 2e   and the header.
13c8d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
13c8e 73 75 62 6a 52 65 71 75 69 72 65 73 50 61 67 65  subjRequiresPage
13c8f 28 70 50 67 29 20 29 7b 0a 20 20 20 20 20 20 72  (pPg) ){.      r
13c90 63 20 3d 20 73 75 62 6a 6f 75 72 6e 61 6c 50 61  c = subjournalPa
13c91 67 65 28 70 50 67 29 3b 0a 20 20 20 20 7d 0a 20  ge(pPg);.    }. 
13c92 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20   }..  /* Update 
13c93 74 68 65 20 64 61 74 61 62 61 73 65 20 73 69 7a  the database siz
13c94 65 20 61 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20  e and return..  
13c95 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  */.  assert( pPa
13c96 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
13c97 52 5f 53 48 41 52 45 44 20 29 3b 0a 20 20 69 66  R_SHARED );.  if
13c98 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  ( pPager->dbSize
13c99 3c 70 50 67 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20  <pPg->pgno ){.  
13c9a 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
13c9b 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20 20   = pPg->pgno;.  
13c9c 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
13c9d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 64  ../*.** Mark a d
13c9e 61 74 61 20 70 61 67 65 20 61 73 20 77 72 69 74  ata page as writ
13c9f 65 61 62 6c 65 2e 20 54 68 69 73 20 72 6f 75 74  eable. This rout
13ca0 69 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c  ine must be call
13ca1 65 64 20 62 65 66 6f 72 65 20 0a 2a 2a 20 6d 61  ed before .** ma
13ca2 6b 69 6e 67 20 63 68 61 6e 67 65 73 20 74 6f 20  king changes to 
13ca3 61 20 70 61 67 65 2e 20 54 68 65 20 63 61 6c 6c  a page. The call
13ca4 65 72 20 6d 75 73 74 20 63 68 65 63 6b 20 74 68  er must check th
13ca5 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 0a  e return value .
13ca6 2a 2a 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74  ** of this funct
13ca7 69 6f 6e 20 61 6e 64 20 62 65 20 63 61 72 65 66  ion and be caref
13ca8 75 6c 20 6e 6f 74 20 74 6f 20 63 68 61 6e 67 65  ul not to change
13ca9 20 61 6e 79 20 70 61 67 65 20 64 61 74 61 20 75   any page data u
13caa 6e 6c 65 73 73 20 0a 2a 2a 20 74 68 69 73 20 72  nless .** this r
13cab 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53  outine returns S
13cac 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20  QLITE_OK..**.** 
13cad 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62  The difference b
13cae 65 74 77 65 65 6e 20 74 68 69 73 20 66 75 6e 63  etween this func
13caf 74 69 6f 6e 20 61 6e 64 20 70 61 67 65 72 5f 77  tion and pager_w
13cb0 72 69 74 65 28 29 20 69 73 20 74 68 61 74 20 74  rite() is that t
13cb1 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  his.** function 
13cb2 61 6c 73 6f 20 64 65 61 6c 73 20 77 69 74 68 20  also deals with 
13cb3 74 68 65 20 73 70 65 63 69 61 6c 20 63 61 73 65  the special case
13cb4 20 77 68 65 72 65 20 32 20 6f 72 20 6d 6f 72 65   where 2 or more
13cb5 20 70 61 67 65 73 0a 2a 2a 20 66 69 74 20 6f 6e   pages.** fit on
13cb6 20 61 20 73 69 6e 67 6c 65 20 64 69 73 6b 20 73   a single disk s
13cb7 65 63 74 6f 72 2e 20 49 6e 20 74 68 69 73 20 63  ector. In this c
13cb8 61 73 65 20 61 6c 6c 20 63 6f 2d 72 65 73 69 64  ase all co-resid
13cb9 65 6e 74 20 70 61 67 65 73 0a 2a 2a 20 6d 75 73  ent pages.** mus
13cba 74 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74  t have been writ
13cbb 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ten to the journ
13cbc 61 6c 20 66 69 6c 65 20 62 65 66 6f 72 65 20 72  al file before r
13cbd 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  eturning..**.** 
13cbe 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
13cbf 72 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rs, SQLITE_NOMEM
13cc0 20 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20   or an IO error 
13cc1 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
13cc2 0a 2a 2a 20 61 73 20 61 70 70 72 6f 70 72 69 61  .** as appropria
13cc3 74 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53  te. Otherwise, S
13cc4 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c  QLITE_OK..*/.SQL
13cc5 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
13cc6 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
13cc7 65 28 44 62 50 61 67 65 20 2a 70 44 62 50 61 67  e(DbPage *pDbPag
13cc8 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  e){.  int rc = S
13cc9 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 50 67 48  QLITE_OK;..  PgH
13cca 64 72 20 2a 70 50 67 20 3d 20 70 44 62 50 61 67  dr *pPg = pDbPag
13ccb 65 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  e;.  Pager *pPag
13ccc 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72  er = pPg->pPager
13ccd 3b 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 50 65  ;.  Pgno nPagePe
13cce 72 53 65 63 74 6f 72 20 3d 20 28 70 50 61 67 65  rSector = (pPage
13ccf 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 2f 70 50  r->sectorSize/pP
13cd0 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b  ager->pageSize);
13cd1 0a 0a 20 20 69 66 28 20 6e 50 61 67 65 50 65 72  ..  if( nPagePer
13cd2 53 65 63 74 6f 72 3e 31 20 29 7b 0a 20 20 20 20  Sector>1 ){.    
13cd3 50 67 6e 6f 20 6e 50 61 67 65 43 6f 75 6e 74 3b  Pgno nPageCount;
13cd4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74            /* Tot
13cd5 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  al number of pag
13cd6 65 73 20 69 6e 20 64 61 74 61 62 61 73 65 20 66  es in database f
13cd7 69 6c 65 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20  ile */.    Pgno 
13cd8 70 67 31 3b 20 20 20 20 20 20 20 20 20 20 20 20  pg1;            
13cd9 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61       /* First pa
13cda 67 65 20 6f 66 20 74 68 65 20 73 65 63 74 6f 72  ge of the sector
13cdb 20 70 50 67 20 69 73 20 6c 6f 63 61 74 65 64 20   pPg is located 
13cdc 6f 6e 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  on. */.    int n
13cdd 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  Page;           
13cde 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
13cdf 66 20 70 61 67 65 73 20 73 74 61 72 74 69 6e 67  f pages starting
13ce0 20 61 74 20 70 67 31 20 74 6f 20 6a 6f 75 72 6e   at pg1 to journ
13ce1 61 6c 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 69  al */.    int ii
13ce2 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
13ce3 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
13ce4 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  ter */.    int n
13ce5 65 65 64 53 79 6e 63 20 3d 20 30 3b 20 20 20 20  eedSync = 0;    
13ce6 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
13ce7 61 6e 79 20 70 61 67 65 20 68 61 73 20 50 47 48  any page has PGH
13ce8 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 2a 2f 0a  DR_NEED_SYNC */.
13ce9 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20  .    /* Set the 
13cea 64 6f 4e 6f 74 53 79 6e 63 20 66 6c 61 67 20 74  doNotSync flag t
13ceb 6f 20 31 2e 20 54 68 69 73 20 69 73 20 62 65 63  o 1. This is bec
13cec 61 75 73 65 20 77 65 20 63 61 6e 6e 6f 74 20 61  ause we cannot a
13ced 6c 6c 6f 77 20 61 20 6a 6f 75 72 6e 61 6c 0a 20  llow a journal. 
13cee 20 20 20 2a 2a 20 68 65 61 64 65 72 20 74 6f 20     ** header to 
13cef 62 65 20 77 72 69 74 74 65 6e 20 62 65 74 77 65  be written betwe
13cf0 65 6e 20 74 68 65 20 70 61 67 65 73 20 6a 6f 75  en the pages jou
13cf1 72 6e 61 6c 65 64 20 62 79 20 74 68 69 73 20 66  rnaled by this f
13cf2 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a  unction..    */.
13cf3 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d      assert( !MEM
13cf4 44 42 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  DB );.    assert
13cf5 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53  ( pPager->doNotS
13cf6 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20 70 50  ync==0 );.    pP
13cf7 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20  ager->doNotSync 
13cf8 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69  = 1;..    /* Thi
13cf9 73 20 74 72 69 63 6b 20 61 73 73 75 6d 65 73 20  s trick assumes 
13cfa 74 68 61 74 20 62 6f 74 68 20 74 68 65 20 70 61  that both the pa
13cfb 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 65 63 74  ge-size and sect
13cfc 6f 72 2d 73 69 7a 65 20 61 72 65 0a 20 20 20 20  or-size are.    
13cfd 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 20 70 6f  ** an integer po
13cfe 77 65 72 20 6f 66 20 32 2e 20 49 74 20 73 65 74  wer of 2. It set
13cff 73 20 76 61 72 69 61 62 6c 65 20 70 67 31 20 74  s variable pg1 t
13d00 6f 20 74 68 65 20 69 64 65 6e 74 69 66 69 65 72  o the identifier
13d01 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 66  .    ** of the f
13d02 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65  irst page of the
13d03 20 73 65 63 74 6f 72 20 70 50 67 20 69 73 20 6c   sector pPg is l
13d04 6f 63 61 74 65 64 20 6f 6e 2e 0a 20 20 20 20 2a  ocated on..    *
13d05 2f 0a 20 20 20 20 70 67 31 20 3d 20 28 28 70 50  /.    pg1 = ((pP
13d06 67 2d 3e 70 67 6e 6f 2d 31 29 20 26 20 7e 28 6e  g->pgno-1) & ~(n
13d07 50 61 67 65 50 65 72 53 65 63 74 6f 72 2d 31 29  PagePerSector-1)
13d08 29 20 2b 20 31 3b 0a 0a 20 20 20 20 73 71 6c 69  ) + 1;..    sqli
13d09 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e  te3PagerPagecoun
13d0a 74 28 70 50 61 67 65 72 2c 20 28 69 6e 74 20 2a  t(pPager, (int *
13d0b 29 26 6e 50 61 67 65 43 6f 75 6e 74 29 3b 0a 20  )&nPageCount);. 
13d0c 20 20 20 69 66 28 20 70 50 67 2d 3e 70 67 6e 6f     if( pPg->pgno
13d0d 3e 6e 50 61 67 65 43 6f 75 6e 74 20 29 7b 0a 20  >nPageCount ){. 
13d0e 20 20 20 20 20 6e 50 61 67 65 20 3d 20 28 70 50       nPage = (pP
13d0f 67 2d 3e 70 67 6e 6f 20 2d 20 70 67 31 29 2b 31  g->pgno - pg1)+1
13d10 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
13d11 28 70 67 31 2b 6e 50 61 67 65 50 65 72 53 65 63  (pg1+nPagePerSec
13d12 74 6f 72 2d 31 29 3e 6e 50 61 67 65 43 6f 75 6e  tor-1)>nPageCoun
13d13 74 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67 65  t ){.      nPage
13d14 20 3d 20 6e 50 61 67 65 43 6f 75 6e 74 2b 31 2d   = nPageCount+1-
13d15 70 67 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  pg1;.    }else{.
13d16 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 6e 50        nPage = nP
13d17 61 67 65 50 65 72 53 65 63 74 6f 72 3b 0a 20 20  agePerSector;.  
13d18 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 6e    }.    assert(n
13d19 50 61 67 65 3e 30 29 3b 0a 20 20 20 20 61 73 73  Page>0);.    ass
13d1a 65 72 74 28 70 67 31 3c 3d 70 50 67 2d 3e 70 67  ert(pg1<=pPg->pg
13d1b 6e 6f 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  no);.    assert(
13d1c 28 70 67 31 2b 6e 50 61 67 65 29 3e 70 50 67 2d  (pg1+nPage)>pPg-
13d1d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20 20 66 6f 72  >pgno);..    for
13d1e 28 69 69 3d 30 3b 20 69 69 3c 6e 50 61 67 65 20  (ii=0; ii<nPage 
13d1f 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
13d20 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 50  ; ii++){.      P
13d21 67 6e 6f 20 70 67 20 3d 20 70 67 31 2b 69 69 3b  gno pg = pg1+ii;
13d22 0a 20 20 20 20 20 20 50 67 48 64 72 20 2a 70 50  .      PgHdr *pP
13d23 61 67 65 3b 0a 20 20 20 20 20 20 69 66 28 20 70  age;.      if( p
13d24 67 3d 3d 70 50 67 2d 3e 70 67 6e 6f 20 7c 7c 20  g==pPg->pgno || 
13d25 21 73 71 6c 69 74 65 33 42 69 74 76 65 63 54 65  !sqlite3BitvecTe
13d26 73 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f  st(pPager->pInJo
13d27 75 72 6e 61 6c 2c 20 70 67 29 20 29 7b 0a 20 20  urnal, pg) ){.  
13d28 20 20 20 20 20 20 69 66 28 20 70 67 21 3d 50 41        if( pg!=PA
13d29 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67  GER_MJ_PGNO(pPag
13d2a 65 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  er) ){.         
13d2b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
13d2c 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 70 67  erGet(pPager, pg
13d2d 2c 20 26 70 50 61 67 65 29 3b 0a 20 20 20 20 20  , &pPage);.     
13d2e 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
13d2f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
13d30 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
13d31 5f 77 72 69 74 65 28 70 50 61 67 65 29 3b 0a 20  _write(pPage);. 
13d32 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70             if( p
13d33 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 44  Page->flags&PGHD
13d34 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20  R_NEED_SYNC ){. 
13d35 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 65 65               nee
13d36 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20  dSync = 1;.     
13d37 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
13d38 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63  pPager->needSync
13d39 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  );.            }
13d3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
13d3b 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
13d3c 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Page);.         
13d3d 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
13d3e 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 70 50     }else if( (pP
13d3f 61 67 65 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b  age = pager_look
13d40 75 70 28 70 50 61 67 65 72 2c 20 70 67 29 29 21  up(pPager, pg))!
13d41 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  =0 ){.        if
13d42 28 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 50  ( pPage->flags&P
13d43 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29  GHDR_NEED_SYNC )
13d44 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 65 64  {.          need
13d45 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20  Sync = 1;.      
13d46 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69    }.        sqli
13d47 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50  te3PagerUnref(pP
13d48 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  age);.      }.  
13d49 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
13d4a 68 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  he PGHDR_NEED_SY
13d4b 4e 43 20 66 6c 61 67 20 69 73 20 73 65 74 20 66  NC flag is set f
13d4c 6f 72 20 61 6e 79 20 6f 66 20 74 68 65 20 6e 50  or any of the nP
13d4d 61 67 65 20 70 61 67 65 73 20 0a 20 20 20 20 2a  age pages .    *
13d4e 2a 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 67  * starting at pg
13d4f 31 2c 20 74 68 65 6e 20 69 74 20 6e 65 65 64 73  1, then it needs
13d50 20 74 6f 20 62 65 20 73 65 74 20 66 6f 72 20 61   to be set for a
13d51 6c 6c 20 6f 66 20 74 68 65 6d 2e 20 42 65 63 61  ll of them. Beca
13d52 75 73 65 0a 20 20 20 20 2a 2a 20 77 72 69 74 69  use.    ** writi
13d53 6e 67 20 74 6f 20 61 6e 79 20 6f 66 20 74 68 65  ng to any of the
13d54 73 65 20 6e 50 61 67 65 20 70 61 67 65 73 20 6d  se nPage pages m
13d55 61 79 20 64 61 6d 61 67 65 20 74 68 65 20 6f 74  ay damage the ot
13d56 68 65 72 73 2c 20 74 68 65 0a 20 20 20 20 2a 2a  hers, the.    **
13d57 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6d 75   journal file mu
13d58 73 74 20 63 6f 6e 74 61 69 6e 20 73 79 6e 63 28  st contain sync(
13d59 29 65 64 20 63 6f 70 69 65 73 20 6f 66 20 61 6c  )ed copies of al
13d5a 6c 20 6f 66 20 74 68 65 6d 0a 20 20 20 20 2a 2a  l of them.    **
13d5b 20 62 65 66 6f 72 65 20 61 6e 79 20 6f 66 20 74   before any of t
13d5c 68 65 6d 20 63 61 6e 20 62 65 20 77 72 69 74 74  hem can be writt
13d5d 65 6e 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61  en out to the da
13d5e 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20  tabase file..   
13d5f 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   */.    if( rc==
13d60 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 65 65  SQLITE_OK && nee
13d61 64 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 61  dSync ){.      a
13d62 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20 26 26  ssert( !MEMDB &&
13d63 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d   pPager->noSync=
13d64 3d 30 20 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  =0 );.      for(
13d65 69 69 3d 30 3b 20 69 69 3c 6e 50 61 67 65 3b 20  ii=0; ii<nPage; 
13d66 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 50  ii++){.        P
13d67 67 48 64 72 20 2a 70 50 61 67 65 20 3d 20 70 61  gHdr *pPage = pa
13d68 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65  ger_lookup(pPage
13d69 72 2c 20 70 67 31 2b 69 69 29 3b 0a 20 20 20 20  r, pg1+ii);.    
13d6a 20 20 20 20 69 66 28 20 70 50 61 67 65 20 29 7b      if( pPage ){
13d6b 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65  .          pPage
13d6c 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52  ->flags |= PGHDR
13d6d 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20  _NEED_SYNC;.    
13d6e 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
13d6f 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a  erUnref(pPage);.
13d70 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
13d71 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70  }.      assert(p
13d72 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 29  Pager->needSync)
13d73 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73  ;.    }..    ass
13d74 65 72 74 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e  ert( pPager->doN
13d75 6f 74 53 79 6e 63 3d 3d 31 20 29 3b 0a 20 20 20  otSync==1 );.   
13d76 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79   pPager->doNotSy
13d77 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  nc = 0;.  }else{
13d78 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f  .    rc = pager_
13d79 77 72 69 74 65 28 70 44 62 50 61 67 65 29 3b 0a  write(pDbPage);.
13d7a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
13d7b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
13d7c 20 54 52 55 45 20 69 66 20 74 68 65 20 70 61 67   TRUE if the pag
13d7d 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61  e given in the a
13d7e 72 67 75 6d 65 6e 74 20 77 61 73 20 70 72 65 76  rgument was prev
13d7f 69 6f 75 73 6c 79 20 70 61 73 73 65 64 0a 2a 2a  iously passed.**
13d80 20 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72   to sqlite3Pager
13d81 57 72 69 74 65 28 29 2e 20 20 49 6e 20 6f 74 68  Write().  In oth
13d82 65 72 20 77 6f 72 64 73 2c 20 72 65 74 75 72 6e  er words, return
13d83 20 54 52 55 45 20 69 66 20 69 74 20 69 73 20 6f   TRUE if it is o
13d84 6b 0a 2a 2a 20 74 6f 20 63 68 61 6e 67 65 20 74  k.** to change t
13d85 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  he content of th
13d86 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 6e 64  e page..*/.#ifnd
13d87 65 66 20 4e 44 45 42 55 47 0a 53 51 4c 49 54 45  ef NDEBUG.SQLITE
13d88 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
13d89 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
13d8a 61 62 6c 65 28 44 62 50 61 67 65 20 2a 70 50 67  able(DbPage *pPg
13d8b 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d  ){.  return pPg-
13d8c 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52  >flags&PGHDR_DIR
13d8d 54 59 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  TY;.}.#endif../*
13d8e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74 68  .** A call to th
13d8f 69 73 20 72 6f 75 74 69 6e 65 20 74 65 6c 6c 73  is routine tells
13d90 20 74 68 65 20 70 61 67 65 72 20 74 68 61 74 20   the pager that 
13d91 69 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73  it is not necess
13d92 61 72 79 20 74 6f 0a 2a 2a 20 77 72 69 74 65 20  ary to.** write 
13d93 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  the information 
13d94 6f 6e 20 70 61 67 65 20 70 50 67 20 62 61 63 6b  on page pPg back
13d95 20 74 6f 20 74 68 65 20 64 69 73 6b 2c 20 65 76   to the disk, ev
13d96 65 6e 20 74 68 6f 75 67 68 0a 2a 2a 20 74 68 61  en though.** tha
13d97 74 20 70 61 67 65 20 6d 69 67 68 74 20 62 65 20  t page might be 
13d98 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74 79 2e  marked as dirty.
13d99 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20    This happens, 
13d9a 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65  for example, whe
13d9b 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 68 61  n.** the page ha
13d9c 73 20 62 65 65 6e 20 61 64 64 65 64 20 61 73 20  s been added as 
13d9d 61 20 6c 65 61 66 20 6f 66 20 74 68 65 20 66 72  a leaf of the fr
13d9e 65 65 6c 69 73 74 20 61 6e 64 20 73 6f 20 69 74  eelist and so it
13d9f 73 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 6e 6f 20  s.** content no 
13da0 6c 6f 6e 67 65 72 20 6d 61 74 74 65 72 73 2e 0a  longer matters..
13da1 2a 2a 0a 2a 2a 20 54 68 65 20 6f 76 65 72 6c 79  **.** The overly
13da2 69 6e 67 20 73 6f 66 74 77 61 72 65 20 6c 61 79  ing software lay
13da3 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f  er calls this ro
13da4 75 74 69 6e 65 20 77 68 65 6e 20 61 6c 6c 20 6f  utine when all o
13da5 66 20 74 68 65 20 64 61 74 61 0a 2a 2a 20 6f 6e  f the data.** on
13da6 20 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 20   the given page 
13da7 69 73 20 75 6e 75 73 65 64 2e 20 54 68 65 20 70  is unused. The p
13da8 61 67 65 72 20 6d 61 72 6b 73 20 74 68 65 20 70  ager marks the p
13da9 61 67 65 20 61 73 20 63 6c 65 61 6e 20 73 6f 0a  age as clean so.
13daa 2a 2a 20 74 68 61 74 20 69 74 20 64 6f 65 73 20  ** that it does 
13dab 6e 6f 74 20 67 65 74 20 77 72 69 74 74 65 6e 20  not get written 
13dac 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 54  to disk..**.** T
13dad 65 73 74 73 20 73 68 6f 77 20 74 68 61 74 20 74  ests show that t
13dae 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  his optimization
13daf 20 63 61 6e 20 71 75 61 64 72 75 70 6c 65 20 74   can quadruple t
13db0 68 65 20 73 70 65 65 64 20 6f 66 20 6c 61 72 67  he speed of larg
13db1 65 20 0a 2a 2a 20 44 45 4c 45 54 45 20 6f 70 65  e .** DELETE ope
13db2 72 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49  rations..*/.SQLI
13db3 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
13db4 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74  sqlite3PagerDont
13db5 57 72 69 74 65 28 50 67 48 64 72 20 2a 70 50 67  Write(PgHdr *pPg
13db6 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  ){.  Pager *pPag
13db7 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72  er = pPg->pPager
13db8 3b 0a 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c  ;.  if( (pPg->fl
13db9 61 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 29  ags&PGHDR_DIRTY)
13dba 20 26 26 20 70 50 61 67 65 72 2d 3e 6e 53 61 76   && pPager->nSav
13dbb 65 70 6f 69 6e 74 3d 3d 30 20 29 7b 0a 20 20 20  epoint==0 ){.   
13dbc 20 50 41 47 45 52 54 52 41 43 45 28 28 22 44 4f   PAGERTRACE(("DO
13dbd 4e 54 5f 57 52 49 54 45 20 70 61 67 65 20 25 64  NT_WRITE page %d
13dbe 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 67 2d 3e   of %d\n", pPg->
13dbf 70 67 6e 6f 2c 20 50 41 47 45 52 49 44 28 70 50  pgno, PAGERID(pP
13dc0 61 67 65 72 29 29 29 3b 0a 20 20 20 20 49 4f 54  ager)));.    IOT
13dc1 52 41 43 45 28 28 22 43 4c 45 41 4e 20 25 70 20  RACE(("CLEAN %p 
13dc2 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70  %d\n", pPager, p
13dc3 50 67 2d 3e 70 67 6e 6f 29 29 0a 20 20 20 20 70  Pg->pgno)).    p
13dc4 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48  Pg->flags |= PGH
13dc5 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 3b 0a 23  DR_DONT_WRITE;.#
13dc6 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45  ifdef SQLITE_CHE
13dc7 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 50 67  CK_PAGES.    pPg
13dc8 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61 67  ->pageHash = pag
13dc9 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67 29  er_pagehash(pPg)
13dca 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a  ;.#endif.  }.}..
13dcb 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
13dcc 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  ne is called to 
13dcd 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61  increment the va
13dce 6c 75 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  lue of the datab
13dcf 61 73 65 20 66 69 6c 65 20 0a 2a 2a 20 63 68 61  ase file .** cha
13dd0 6e 67 65 2d 63 6f 75 6e 74 65 72 2c 20 73 74 6f  nge-counter, sto
13dd1 72 65 64 20 61 73 20 61 20 34 2d 62 79 74 65 20  red as a 4-byte 
13dd2 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67  big-endian integ
13dd3 65 72 20 73 74 61 72 74 69 6e 67 20 61 74 20 0a  er starting at .
13dd4 2a 2a 20 62 79 74 65 20 6f 66 66 73 65 74 20 32  ** byte offset 2
13dd5 34 20 6f 66 20 74 68 65 20 70 61 67 65 72 20 66  4 of the pager f
13dd6 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ile..**.** If th
13dd7 65 20 69 73 44 69 72 65 63 74 4d 6f 64 65 20 66  e isDirectMode f
13dd8 6c 61 67 20 69 73 20 7a 65 72 6f 2c 20 74 68 65  lag is zero, the
13dd9 6e 20 74 68 69 73 20 69 73 20 64 6f 6e 65 20 62  n this is done b
13dda 79 20 63 61 6c 6c 69 6e 67 20 0a 2a 2a 20 73 71  y calling .** sq
13ddb 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
13ddc 29 20 6f 6e 20 70 61 67 65 20 31 2c 20 74 68 65  ) on page 1, the
13ddd 6e 20 6d 6f 64 69 66 79 69 6e 67 20 74 68 65 20  n modifying the 
13dde 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 0a  contents of the.
13ddf 2a 2a 20 70 61 67 65 20 64 61 74 61 2e 20 49 6e  ** page data. In
13de0 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 66   this case the f
13de1 69 6c 65 20 77 69 6c 6c 20 62 65 20 75 70 64 61  ile will be upda
13de2 74 65 64 20 77 68 65 6e 20 74 68 65 20 63 75 72  ted when the cur
13de3 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74  rent.** transact
13de4 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64  ion is committed
13de5 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 44 69  ..**.** The isDi
13de6 72 65 63 74 4d 6f 64 65 20 66 6c 61 67 20 6d 61  rectMode flag ma
13de7 79 20 6f 6e 6c 79 20 62 65 20 6e 6f 6e 2d 7a 65  y only be non-ze
13de8 72 6f 20 69 66 20 74 68 65 20 6c 69 62 72 61 72  ro if the librar
13de9 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 0a 2a  y was compiled.*
13dea 2a 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54  * with the SQLIT
13deb 45 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f  E_ENABLE_ATOMIC_
13dec 57 52 49 54 45 20 6d 61 63 72 6f 20 64 65 66 69  WRITE macro defi
13ded 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ned. In this cas
13dee 65 2c 0a 2a 2a 20 69 66 20 69 73 44 69 72 65 63  e,.** if isDirec
13def 74 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  t is non-zero, t
13df0 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
13df1 20 66 69 6c 65 20 69 73 20 75 70 64 61 74 65 64   file is updated
13df2 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 62 79 20   directly.** by 
13df3 77 72 69 74 69 6e 67 20 61 6e 20 75 70 64 61 74  writing an updat
13df4 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 61  ed version of pa
13df5 67 65 20 31 20 75 73 69 6e 67 20 61 20 63 61 6c  ge 1 using a cal
13df6 6c 20 74 6f 20 74 68 65 20 0a 2a 2a 20 73 71 6c  l to the .** sql
13df7 69 74 65 33 4f 73 57 72 69 74 65 28 29 20 66 75  ite3OsWrite() fu
13df8 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  nction..*/.stati
13df9 63 20 69 6e 74 20 70 61 67 65 72 5f 69 6e 63 72  c int pager_incr
13dfa 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 50  _changecounter(P
13dfb 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
13dfc 74 20 69 73 44 69 72 65 63 74 4d 6f 64 65 29 7b  t isDirectMode){
13dfd 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
13dfe 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 44 65 63  TE_OK;..  /* Dec
13dff 6c 61 72 65 20 61 6e 64 20 69 6e 69 74 69 61 6c  lare and initial
13e00 69 7a 65 20 63 6f 6e 73 74 61 6e 74 20 69 6e 74  ize constant int
13e01 65 67 65 72 20 27 69 73 44 69 72 65 63 74 27 2e  eger 'isDirect'.
13e02 20 49 66 20 74 68 65 0a 20 20 2a 2a 20 61 74 6f   If the.  ** ato
13e03 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d 69  mic-write optimi
13e04 7a 61 74 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65  zation is enable
13e05 64 20 69 6e 20 74 68 69 73 20 62 75 69 6c 64 2c  d in this build,
13e06 20 74 68 65 6e 20 69 73 44 69 72 65 63 74 0a 20   then isDirect. 
13e07 20 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c 69 7a   ** is initializ
13e08 65 64 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  ed to the value 
13e09 70 61 73 73 65 64 20 61 73 20 74 68 65 20 69 73  passed as the is
13e0a 44 69 72 65 63 74 4d 6f 64 65 20 70 61 72 61 6d  DirectMode param
13e0b 65 74 65 72 0a 20 20 2a 2a 20 74 6f 20 74 68 69  eter.  ** to thi
13e0c 73 20 66 75 6e 63 74 69 6f 6e 2e 20 4f 74 68 65  s function. Othe
13e0d 72 77 69 73 65 2c 20 69 74 20 69 73 20 61 6c 77  rwise, it is alw
13e0e 61 79 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e  ays set to zero.
13e0f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 69  .  **.  ** The i
13e10 64 65 61 20 69 73 20 74 68 61 74 20 69 66 20 74  dea is that if t
13e11 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20  he atomic-write 
13e12 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20  optimization is 
13e13 6e 6f 74 0a 20 20 2a 2a 20 65 6e 61 62 6c 65 64  not.  ** enabled
13e14 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65   at compile time
13e15 2c 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63  , the compiler c
13e16 61 6e 20 6f 6d 69 74 20 74 68 65 20 74 65 73 74  an omit the test
13e17 73 20 6f 66 0a 20 20 2a 2a 20 27 69 73 44 69 72  s of.  ** 'isDir
13e18 65 63 74 27 20 62 65 6c 6f 77 2c 20 61 73 20 77  ect' below, as w
13e19 65 6c 6c 20 61 73 20 74 68 65 20 62 6c 6f 63 6b  ell as the block
13e1a 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 74 68 65   enclosed in the
13e1b 0a 20 20 2a 2a 20 22 69 66 28 20 69 73 44 69 72  .  ** "if( isDir
13e1c 65 63 74 20 29 22 20 63 6f 6e 64 69 74 69 6f 6e  ect )" condition
13e1d 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ..  */.#ifndef S
13e1e 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54 4f  QLITE_ENABLE_ATO
13e1f 4d 49 43 5f 57 52 49 54 45 0a 23 20 64 65 66 69  MIC_WRITE.# defi
13e20 6e 65 20 44 49 52 45 43 54 5f 4d 4f 44 45 20 30  ne DIRECT_MODE 0
13e21 0a 20 20 61 73 73 65 72 74 28 20 69 73 44 69 72  .  assert( isDir
13e22 65 63 74 4d 6f 64 65 3d 3d 30 20 29 3b 0a 20 20  ectMode==0 );.  
13e23 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
13e24 28 69 73 44 69 72 65 63 74 4d 6f 64 65 29 3b 0a  (isDirectMode);.
13e25 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 44  #else.# define D
13e26 49 52 45 43 54 5f 4d 4f 44 45 20 69 73 44 69 72  IRECT_MODE isDir
13e27 65 63 74 4d 6f 64 65 0a 23 65 6e 64 69 66 0a 0a  ectMode.#endif..
13e28 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
13e29 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 52  ->state>=PAGER_R
13e2a 45 53 45 52 56 45 44 20 29 3b 0a 20 20 69 66 28  ESERVED );.  if(
13e2b 20 21 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65   !pPager->change
13e2c 43 6f 75 6e 74 44 6f 6e 65 20 26 26 20 41 4c 57  CountDone && ALW
13e2d 41 59 53 28 70 50 61 67 65 72 2d 3e 64 62 53 69  AYS(pPager->dbSi
13e2e 7a 65 3e 30 29 20 29 7b 0a 20 20 20 20 50 67 48  ze>0) ){.    PgH
13e2f 64 72 20 2a 70 50 67 48 64 72 3b 20 20 20 20 20  dr *pPgHdr;     
13e30 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
13e31 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20  ference to page 
13e32 31 20 2a 2f 0a 20 20 20 20 75 33 32 20 63 68 61  1 */.    u32 cha
13e33 6e 67 65 5f 63 6f 75 6e 74 65 72 3b 20 20 20 20  nge_counter;    
13e34 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61         /* Initia
13e35 6c 20 76 61 6c 75 65 20 6f 66 20 63 68 61 6e 67  l value of chang
13e36 65 2d 63 6f 75 6e 74 65 72 20 66 69 65 6c 64 20  e-counter field 
13e37 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20  */..    assert( 
13e38 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c  !pPager->tempFil
13e39 65 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67  e && isOpen(pPag
13e3a 65 72 2d 3e 66 64 29 20 29 3b 0a 0a 20 20 20 20  er->fd) );..    
13e3b 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 31 20 6f  /* Open page 1 o
13e3c 66 20 74 68 65 20 66 69 6c 65 20 66 6f 72 20 77  f the file for w
13e3d 72 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72  riting. */.    r
13e3e 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
13e3f 47 65 74 28 70 50 61 67 65 72 2c 20 31 2c 20 26  Get(pPager, 1, &
13e40 70 50 67 48 64 72 29 3b 0a 20 20 20 20 61 73 73  pPgHdr);.    ass
13e41 65 72 74 28 20 70 50 67 48 64 72 3d 3d 30 20 7c  ert( pPgHdr==0 |
13e42 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  | rc==SQLITE_OK 
13e43 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70 61  );..    /* If pa
13e44 67 65 20 6f 6e 65 20 77 61 73 20 66 65 74 63 68  ge one was fetch
13e45 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c  ed successfully,
13e46 20 61 6e 64 20 74 68 69 73 20 66 75 6e 63 74 69   and this functi
13e47 6f 6e 20 69 73 20 6e 6f 74 0a 20 20 20 20 2a 2a  on is not.    **
13e48 20 6f 70 65 72 61 74 69 6e 67 20 69 6e 20 64 69   operating in di
13e49 72 65 63 74 2d 6d 6f 64 65 2c 20 6d 61 6b 65 20  rect-mode, make 
13e4a 70 61 67 65 20 31 20 77 72 69 74 61 62 6c 65 2e  page 1 writable.
13e4b 20 20 57 68 65 6e 20 6e 6f 74 20 69 6e 20 0a 20    When not in . 
13e4c 20 20 20 2a 2a 20 64 69 72 65 63 74 20 6d 6f 64     ** direct mod
13e4d 65 2c 20 70 61 67 65 20 31 20 69 73 20 61 6c 77  e, page 1 is alw
13e4e 61 79 73 20 68 65 6c 64 20 69 6e 20 63 61 63 68  ays held in cach
13e4f 65 20 61 6e 64 20 68 65 6e 63 65 20 74 68 65 20  e and hence the 
13e50 50 61 67 65 72 47 65 74 28 29 0a 20 20 20 20 2a  PagerGet().    *
13e51 2a 20 61 62 6f 76 65 20 69 73 20 61 6c 77 61 79  * above is alway
13e52 73 20 73 75 63 63 65 73 73 66 75 6c 20 2d 20 68  s successful - h
13e53 65 6e 63 65 20 74 68 65 20 41 4c 57 41 59 53 20  ence the ALWAYS 
13e54 6f 6e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  on rc==SQLITE_OK
13e55 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
13e56 20 21 44 49 52 45 43 54 5f 4d 4f 44 45 20 26 26   !DIRECT_MODE &&
13e57 20 41 4c 57 41 59 53 28 72 63 3d 3d 53 51 4c 49   ALWAYS(rc==SQLI
13e58 54 45 5f 4f 4b 29 20 29 7b 0a 20 20 20 20 20 20  TE_OK) ){.      
13e59 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
13e5a 72 57 72 69 74 65 28 70 50 67 48 64 72 29 3b 0a  rWrite(pPgHdr);.
13e5b 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72      }..    if( r
13e5c 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
13e5d 20 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65        /* Increme
13e5e 6e 74 20 74 68 65 20 76 61 6c 75 65 20 6a 75 73  nt the value jus
13e5f 74 20 72 65 61 64 20 61 6e 64 20 77 72 69 74 65  t read and write
13e60 20 69 74 20 62 61 63 6b 20 74 6f 20 62 79 74 65   it back to byte
13e61 20 32 34 2e 20 2a 2f 0a 20 20 20 20 20 20 63 68   24. */.      ch
13e62 61 6e 67 65 5f 63 6f 75 6e 74 65 72 20 3d 20 73  ange_counter = s
13e63 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28 28  qlite3Get4byte((
13e64 75 38 2a 29 70 50 61 67 65 72 2d 3e 64 62 46 69  u8*)pPager->dbFi
13e65 6c 65 56 65 72 73 29 3b 0a 20 20 20 20 20 20 63  leVers);.      c
13e66 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72 2b 2b 3b  hange_counter++;
13e67 0a 20 20 20 20 20 20 70 75 74 33 32 62 69 74 73  .      put32bits
13e68 28 28 28 63 68 61 72 2a 29 70 50 67 48 64 72 2d  (((char*)pPgHdr-
13e69 3e 70 44 61 74 61 29 2b 32 34 2c 20 63 68 61 6e  >pData)+24, chan
13e6a 67 65 5f 63 6f 75 6e 74 65 72 29 3b 0a 0a 20 20  ge_counter);..  
13e6b 20 20 20 20 2f 2a 20 49 66 20 72 75 6e 6e 69 6e      /* If runnin
13e6c 67 20 69 6e 20 64 69 72 65 63 74 20 6d 6f 64 65  g in direct mode
13e6d 2c 20 77 72 69 74 65 20 74 68 65 20 63 6f 6e 74  , write the cont
13e6e 65 6e 74 73 20 6f 66 20 70 61 67 65 20 31 20 74  ents of page 1 t
13e6f 6f 20 74 68 65 20 66 69 6c 65 2e 20 2a 2f 0a 20  o the file. */. 
13e70 20 20 20 20 20 69 66 28 20 44 49 52 45 43 54 5f       if( DIRECT_
13e71 4d 4f 44 45 20 29 7b 0a 20 20 20 20 20 20 20 20  MODE ){.        
13e72 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 42 75 66  const void *zBuf
13e73 20 3d 20 70 50 67 48 64 72 2d 3e 70 44 61 74 61   = pPgHdr->pData
13e74 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
13e75 28 20 70 50 61 67 65 72 2d 3e 64 62 46 69 6c 65  ( pPager->dbFile
13e76 53 69 7a 65 3e 30 20 29 3b 0a 20 20 20 20 20 20  Size>0 );.      
13e77 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
13e78 57 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64  Write(pPager->fd
13e79 2c 20 7a 42 75 66 2c 20 70 50 61 67 65 72 2d 3e  , zBuf, pPager->
13e7a 70 61 67 65 53 69 7a 65 2c 20 30 29 3b 0a 20 20  pageSize, 0);.  
13e7b 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
13e7c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
13e7d 20 20 20 20 20 70 50 61 67 65 72 2d 3e 63 68 61       pPager->cha
13e7e 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d 20 31  ngeCountDone = 1
13e7f 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
13e80 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
13e81 20 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65 43   pPager->changeC
13e82 6f 75 6e 74 44 6f 6e 65 20 3d 20 31 3b 0a 20 20  ountDone = 1;.  
13e83 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
13e84 20 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20   /* Release the 
13e85 70 61 67 65 20 72 65 66 65 72 65 6e 63 65 2e 20  page reference. 
13e86 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61  */.    sqlite3Pa
13e87 67 65 72 55 6e 72 65 66 28 70 50 67 48 64 72 29  gerUnref(pPgHdr)
13e88 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
13e89 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63  c;.}../*.** Sync
13e8a 20 74 68 65 20 70 61 67 65 72 20 66 69 6c 65 20   the pager file 
13e8b 74 6f 20 64 69 73 6b 2e 20 54 68 69 73 20 69 73  to disk. This is
13e8c 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 69 6e 2d   a no-op for in-
13e8d 6d 65 6d 6f 72 79 20 66 69 6c 65 73 0a 2a 2a 20  memory files.** 
13e8e 6f 72 20 70 61 67 65 73 20 77 69 74 68 20 74 68  or pages with th
13e8f 65 20 50 61 67 65 72 2e 6e 6f 53 79 6e 63 20 66  e Pager.noSync f
13e90 6c 61 67 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 49  lag set..**.** I
13e91 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72  f successful, or
13e92 20 63 61 6c 6c 65 64 20 6f 6e 20 61 20 70 61 67   called on a pag
13e93 65 72 20 66 6f 72 20 77 68 69 63 68 20 69 74 20  er for which it 
13e94 69 73 20 61 20 6e 6f 2d 6f 70 2c 20 74 68 69 73  is a no-op, this
13e95 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  .** function ret
13e96 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 20  urns SQLITE_OK. 
13e97 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 20 49 4f  Otherwise, an IO
13e98 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
13e99 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  eturned..*/.SQLI
13e9a 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
13e9b 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63 28  qlite3PagerSync(
13e9c 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
13e9d 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
13e9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13e9f 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
13ea0 20 63 6f 64 65 20 2a 2f 0a 20 20 61 73 73 65 72   code */.  asser
13ea1 74 28 20 21 4d 45 4d 44 42 20 29 3b 0a 20 20 69  t( !MEMDB );.  i
13ea2 66 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  f( pPager->noSyn
13ea3 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  c ){.    rc = SQ
13ea4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65  LITE_OK;.  }else
13ea5 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
13ea6 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d  e3OsSync(pPager-
13ea7 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e  >fd, pPager->syn
13ea8 63 5f 66 6c 61 67 73 29 3b 0a 20 20 7d 0a 20 20  c_flags);.  }.  
13ea9 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
13eaa 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 64 61 74  .** Sync the dat
13eab 61 62 61 73 65 20 66 69 6c 65 20 66 6f 72 20 74  abase file for t
13eac 68 65 20 70 61 67 65 72 20 70 50 61 67 65 72 2e  he pager pPager.
13ead 20 7a 4d 61 73 74 65 72 20 70 6f 69 6e 74 73 20   zMaster points 
13eae 74 6f 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f  to the name.** o
13eaf 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  f a master journ
13eb0 61 6c 20 66 69 6c 65 20 74 68 61 74 20 73 68 6f  al file that sho
13eb1 75 6c 64 20 62 65 20 77 72 69 74 74 65 6e 20 69  uld be written i
13eb2 6e 74 6f 20 74 68 65 20 69 6e 64 69 76 69 64 75  nto the individu
13eb3 61 6c 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69  al.** journal fi
13eb4 6c 65 2e 20 7a 4d 61 73 74 65 72 20 6d 61 79 20  le. zMaster may 
13eb5 62 65 20 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69  be NULL, which i
13eb6 73 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73  s interpreted as
13eb7 20 6e 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f   no master.** jo
13eb8 75 72 6e 61 6c 20 28 61 20 73 69 6e 67 6c 65 20  urnal (a single 
13eb9 64 61 74 61 62 61 73 65 20 74 72 61 6e 73 61 63  database transac
13eba 74 69 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  tion)..**.** Thi
13ebb 73 20 72 6f 75 74 69 6e 65 20 65 6e 73 75 72 65  s routine ensure
13ebc 73 20 74 68 61 74 3a 0a 2a 2a 0a 2a 2a 20 20 20  s that:.**.**   
13ebd 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66  * The database f
13ebe 69 6c 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  ile change-count
13ebf 65 72 20 69 73 20 75 70 64 61 74 65 64 2c 0a 2a  er is updated,.*
13ec0 2a 20 20 20 2a 20 74 68 65 20 6a 6f 75 72 6e 61  *   * the journa
13ec1 6c 20 69 73 20 73 79 6e 63 65 64 20 28 75 6e 6c  l is synced (unl
13ec2 65 73 73 20 74 68 65 20 61 74 6f 6d 69 63 2d 77  ess the atomic-w
13ec3 72 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f  rite optimizatio
13ec4 6e 20 69 73 20 75 73 65 64 29 2c 0a 2a 2a 20 20  n is used),.**  
13ec5 20 2a 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67   * all dirty pag
13ec6 65 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 74  es are written t
13ec7 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
13ec8 69 6c 65 2c 20 0a 2a 2a 20 20 20 2a 20 74 68 65  ile, .**   * the
13ec9 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
13eca 73 20 74 72 75 6e 63 61 74 65 64 20 28 69 66 20  s truncated (if 
13ecb 72 65 71 75 69 72 65 64 29 2c 20 61 6e 64 0a 2a  required), and.*
13ecc 2a 20 20 20 2a 20 74 68 65 20 64 61 74 61 62 61  *   * the databa
13ecd 73 65 20 66 69 6c 65 20 73 79 6e 63 65 64 2e 20  se file synced. 
13ece 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20  .**.** The only 
13ecf 74 68 69 6e 67 20 74 68 61 74 20 72 65 6d 61 69  thing that remai
13ed0 6e 73 20 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65  ns to commit the
13ed1 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
13ed2 74 6f 20 66 69 6e 61 6c 69 7a 65 20 0a 2a 2a 20  to finalize .** 
13ed3 28 64 65 6c 65 74 65 2c 20 74 72 75 6e 63 61 74  (delete, truncat
13ed4 65 20 6f 72 20 7a 65 72 6f 20 74 68 65 20 66 69  e or zero the fi
13ed5 72 73 74 20 70 61 72 74 20 6f 66 29 20 74 68 65  rst part of) the
13ed6 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 28 6f   journal file (o
13ed7 72 20 0a 2a 2a 20 64 65 6c 65 74 65 20 74 68 65  r .** delete the
13ed8 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
13ed9 66 69 6c 65 20 69 66 20 73 70 65 63 69 66 69 65  file if specifie
13eda 64 29 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74  d)..**.** Note t
13edb 68 61 74 20 69 66 20 7a 4d 61 73 74 65 72 3d 3d  hat if zMaster==
13edc 4e 55 4c 4c 2c 20 74 68 69 73 20 64 6f 65 73 20  NULL, this does 
13edd 6e 6f 74 20 6f 76 65 72 77 72 69 74 65 20 61 20  not overwrite a 
13ede 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 0a 2a  previous value.*
13edf 2a 20 70 61 73 73 65 64 20 74 6f 20 61 6e 20 73  * passed to an s
13ee0 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69  qlite3PagerCommi
13ee1 74 50 68 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c  tPhaseOne() call
13ee2 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66  ..**.** If the f
13ee3 69 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 20 2d  inal parameter -
13ee4 20 6e 6f 53 79 6e 63 20 2d 20 69 73 20 74 72 75   noSync - is tru
13ee5 65 2c 20 74 68 65 6e 20 74 68 65 20 64 61 74 61  e, then the data
13ee6 62 61 73 65 20 66 69 6c 65 20 69 74 73 65 6c 66  base file itself
13ee7 0a 2a 2a 20 69 73 20 6e 6f 74 20 73 79 6e 63 65  .** is not synce
13ee8 64 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75  d. The caller mu
13ee9 73 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50  st call sqlite3P
13eea 61 67 65 72 53 79 6e 63 28 29 20 64 69 72 65 63  agerSync() direc
13eeb 74 6c 79 20 74 6f 0a 2a 2a 20 73 79 6e 63 20 74  tly to.** sync t
13eec 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
13eed 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20   before calling 
13eee 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29  CommitPhaseTwo()
13eef 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 0a 2a   to delete the.*
13ef0 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69  * journal file i
13ef1 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2f 0a  n this case..*/.
13ef2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
13ef3 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43  nt sqlite3PagerC
13ef4 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 0a 20  ommitPhaseOne(. 
13ef5 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20   Pager *pPager, 
13ef6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13ef7 20 2f 2a 20 50 61 67 65 72 20 6f 62 6a 65 63 74   /* Pager object
13ef8 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
13ef9 20 2a 7a 4d 61 73 74 65 72 2c 20 20 20 20 20 20   *zMaster,      
13efa 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f 74 20        /* If not 
13efb 4e 55 4c 4c 2c 20 74 68 65 20 6d 61 73 74 65 72  NULL, the master
13efc 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 2a 2f   journal name */
13efd 0a 20 20 69 6e 74 20 6e 6f 53 79 6e 63 20 20 20  .  int noSync   
13efe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13eff 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 6f 6d     /* True to om
13f00 69 74 20 74 68 65 20 78 53 79 6e 63 20 6f 6e 20  it the xSync on 
13f01 74 68 65 20 64 62 20 66 69 6c 65 20 2a 2f 0a 29  the db file */.)
13f02 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
13f03 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20  ITE_OK;         
13f04 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
13f05 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20  de */..  /* The 
13f06 64 62 4f 72 69 67 53 69 7a 65 20 69 73 20 6e 65  dbOrigSize is ne
13f07 76 65 72 20 73 65 74 20 69 66 20 6a 6f 75 72 6e  ver set if journ
13f08 61 6c 5f 6d 6f 64 65 3d 4f 46 46 20 2a 2f 0a 20  al_mode=OFF */. 
13f09 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
13f0a 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41  >journalMode!=PA
13f0b 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
13f0c 4f 46 46 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64  OFF || pPager->d
13f0d 62 4f 72 69 67 53 69 7a 65 3d 3d 30 20 29 3b 0a  bOrigSize==0 );.
13f0e 0a 20 20 2f 2a 20 49 66 20 61 20 70 72 69 6f 72  .  /* If a prior
13f0f 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c   error occurred,
13f10 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68   this routine sh
13f11 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 61 6c 6c  ould not be call
13f12 65 64 2e 20 20 52 4f 4c 4c 42 41 43 4b 0a 20 20  ed.  ROLLBACK.  
13f13 2a 2a 20 69 73 20 74 68 65 20 61 70 70 72 6f 70  ** is the approp
13f14 72 69 61 74 65 20 72 65 73 70 6f 6e 73 65 20 74  riate response t
13f15 6f 20 61 6e 20 65 72 72 6f 72 2c 20 6e 6f 74 20  o an error, not 
13f16 43 4f 4d 4d 49 54 2e 20 20 47 75 61 72 64 20 61  COMMIT.  Guard a
13f17 67 61 69 6e 73 74 0a 20 20 2a 2a 20 63 6f 64 69  gainst.  ** codi
13f18 6e 67 20 65 72 72 6f 72 73 20 62 79 20 72 65 70  ng errors by rep
13f19 65 61 74 69 6e 67 20 74 68 65 20 70 72 69 6f 72  eating the prior
13f1a 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 69 66 28   error. */.  if(
13f1b 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d 3e 65   NEVER(pPager->e
13f1c 72 72 43 6f 64 65 29 20 29 20 72 65 74 75 72 6e  rrCode) ) return
13f1d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
13f1e 3b 0a 0a 20 20 50 41 47 45 52 54 52 41 43 45 28  ;..  PAGERTRACE(
13f1f 28 22 44 41 54 41 42 41 53 45 20 53 59 4e 43 3a  ("DATABASE SYNC:
13f20 20 46 69 6c 65 3d 25 73 20 7a 4d 61 73 74 65 72   File=%s zMaster
13f21 3d 25 73 20 6e 53 69 7a 65 3d 25 64 5c 6e 22 2c  =%s nSize=%d\n",
13f22 20 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e   .      pPager->
13f23 7a 46 69 6c 65 6e 61 6d 65 2c 20 7a 4d 61 73 74  zFilename, zMast
13f24 65 72 2c 20 70 50 61 67 65 72 2d 3e 64 62 53 69  er, pPager->dbSi
13f25 7a 65 29 29 3b 0a 0a 20 20 69 66 28 20 4d 45 4d  ze));..  if( MEM
13f26 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62  DB && pPager->db
13f27 4d 6f 64 69 66 69 65 64 20 29 7b 0a 20 20 20 20  Modified ){.    
13f28 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e  /* If this is an
13f29 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 62 2c 20 6f   in-memory db, o
13f2a 72 20 6e 6f 20 70 61 67 65 73 20 68 61 76 65 20  r no pages have 
13f2b 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 2c  been written to,
13f2c 20 6f 72 20 74 68 69 73 0a 20 20 20 20 2a 2a 20   or this.    ** 
13f2d 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 61 6c 72  function has alr
13f2e 65 61 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64  eady been called
13f2f 2c 20 69 74 20 69 73 20 6d 6f 73 74 6c 79 20 61  , it is mostly a
13f30 20 6e 6f 2d 6f 70 2e 20 20 48 6f 77 65 76 65 72   no-op.  However
13f31 2c 20 61 6e 79 0a 20 20 20 20 2a 2a 20 62 61 63  , any.    ** bac
13f32 6b 75 70 20 69 6e 20 70 72 6f 67 72 65 73 73 20  kup in progress 
13f33 6e 65 65 64 73 20 74 6f 20 62 65 20 72 65 73 74  needs to be rest
13f34 61 72 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  arted..    */.  
13f35 20 20 73 71 6c 69 74 65 33 42 61 63 6b 75 70 52    sqlite3BackupR
13f36 65 73 74 61 72 74 28 70 50 61 67 65 72 2d 3e 70  estart(pPager->p
13f37 42 61 63 6b 75 70 29 3b 0a 20 20 7d 65 6c 73 65  Backup);.  }else
13f38 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61   if( pPager->sta
13f39 74 65 21 3d 50 41 47 45 52 5f 53 59 4e 43 45 44  te!=PAGER_SYNCED
13f3a 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f   && pPager->dbMo
13f3b 64 69 66 69 65 64 20 29 7b 0a 0a 20 20 20 20 2f  dified ){..    /
13f3c 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
13f3d 62 6c 6f 63 6b 20 75 70 64 61 74 65 73 20 74 68  block updates th
13f3e 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74 65 72  e change-counter
13f3f 2e 20 45 78 61 63 74 6c 79 20 68 6f 77 20 69 74  . Exactly how it
13f40 0a 20 20 20 20 2a 2a 20 64 6f 65 73 20 74 68 69  .    ** does thi
13f41 73 20 64 65 70 65 6e 64 73 20 6f 6e 20 77 68 65  s depends on whe
13f42 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
13f43 61 74 6f 6d 69 63 2d 75 70 64 61 74 65 20 6f 70  atomic-update op
13f44 74 69 6d 69 7a 61 74 69 6f 6e 0a 20 20 20 20 2a  timization.    *
13f45 2a 20 77 61 73 20 65 6e 61 62 6c 65 64 20 61 74  * was enabled at
13f46 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 61   compile time, a
13f47 6e 64 20 69 66 20 74 68 69 73 20 74 72 61 6e 73  nd if this trans
13f48 61 63 74 69 6f 6e 20 6d 65 65 74 73 20 74 68 65  action meets the
13f49 20 0a 20 20 20 20 2a 2a 20 72 75 6e 74 69 6d 65   .    ** runtime
13f4a 20 63 72 69 74 65 72 69 61 20 74 6f 20 75 73 65   criteria to use
13f4b 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 3a 20   the operation: 
13f4c 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
13f4d 20 20 2a 20 54 68 65 20 66 69 6c 65 2d 73 79 73    * The file-sys
13f4e 74 65 6d 20 73 75 70 70 6f 72 74 73 20 74 68 65  tem supports the
13f4f 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 70 72   atomic-write pr
13f50 6f 70 65 72 74 79 20 66 6f 72 0a 20 20 20 20 2a  operty for.    *
13f51 2a 20 20 20 20 20 20 62 6c 6f 63 6b 73 20 6f 66  *      blocks of
13f52 20 73 69 7a 65 20 70 61 67 65 2d 73 69 7a 65 2c   size page-size,
13f53 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 20 20 20   and .    **    
13f54 2a 20 54 68 69 73 20 63 6f 6d 6d 69 74 20 69 73  * This commit is
13f55 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 20 6d   not part of a m
13f56 75 6c 74 69 2d 66 69 6c 65 20 74 72 61 6e 73 61  ulti-file transa
13f57 63 74 69 6f 6e 2c 20 61 6e 64 0a 20 20 20 20 2a  ction, and.    *
13f58 2a 20 20 20 20 2a 20 45 78 61 63 74 6c 79 20 6f  *    * Exactly o
13f59 6e 65 20 70 61 67 65 20 68 61 73 20 62 65 65 6e  ne page has been
13f5a 20 6d 6f 64 69 66 69 65 64 20 61 6e 64 20 73 74   modified and st
13f5b 6f 72 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  ore in the journ
13f5c 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a  al file..    **.
13f5d 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 6f 70      ** If the op
13f5e 74 69 6d 69 7a 61 74 69 6f 6e 20 77 61 73 20 6e  timization was n
13f5f 6f 74 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f  ot enabled at co
13f60 6d 70 69 6c 65 20 74 69 6d 65 2c 20 74 68 65 6e  mpile time, then
13f61 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65   the.    ** page
13f62 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75  r_incr_changecou
13f63 6e 74 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 20  nter() function 
13f64 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 75 70 64  is called to upd
13f65 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 0a 20  ate the change. 
13f66 20 20 20 2a 2a 20 63 6f 75 6e 74 65 72 20 69 6e     ** counter in
13f67 20 27 69 6e 64 69 72 65 63 74 2d 6d 6f 64 65 27   'indirect-mode'
13f68 2e 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a  . If the optimiz
13f69 61 74 69 6f 6e 20 69 73 20 63 6f 6d 70 69 6c 65  ation is compile
13f6a 64 20 69 6e 20 62 75 74 0a 20 20 20 20 2a 2a 20  d in but.    ** 
13f6b 69 73 20 6e 6f 74 20 61 70 70 6c 69 63 61 62 6c  is not applicabl
13f6c 65 20 74 6f 20 74 68 69 73 20 74 72 61 6e 73 61  e to this transa
13f6d 63 74 69 6f 6e 2c 20 63 61 6c 6c 20 73 71 6c 69  ction, call sqli
13f6e 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65  te3JournalCreate
13f6f 28 29 0a 20 20 20 20 2a 2a 20 74 6f 20 6d 61 6b  ().    ** to mak
13f70 65 20 73 75 72 65 20 74 68 65 20 6a 6f 75 72 6e  e sure the journ
13f71 61 6c 20 66 69 6c 65 20 68 61 73 20 61 63 74 75  al file has actu
13f72 61 6c 6c 79 20 62 65 65 6e 20 63 72 65 61 74 65  ally been create
13f73 64 2c 20 74 68 65 6e 20 63 61 6c 6c 0a 20 20 20  d, then call.   
13f74 20 2a 2a 20 70 61 67 65 72 5f 69 6e 63 72 5f 63   ** pager_incr_c
13f75 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 74  hangecounter() t
13f76 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61  o update the cha
13f77 6e 67 65 2d 63 6f 75 6e 74 65 72 20 69 6e 20 69  nge-counter in i
13f78 6e 64 69 72 65 63 74 0a 20 20 20 20 2a 2a 20 6d  ndirect.    ** m
13f79 6f 64 65 2e 20 0a 20 20 20 20 2a 2a 0a 20 20 20  ode. .    **.   
13f7a 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69   ** Otherwise, i
13f7b 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69  f the optimizati
13f7c 6f 6e 20 69 73 20 62 6f 74 68 20 65 6e 61 62 6c  on is both enabl
13f7d 65 64 20 61 6e 64 20 61 70 70 6c 69 63 61 62 6c  ed and applicabl
13f7e 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 63  e,.    ** then c
13f7f 61 6c 6c 20 70 61 67 65 72 5f 69 6e 63 72 5f 63  all pager_incr_c
13f80 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 29 20 74  hangecounter() t
13f81 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61  o update the cha
13f82 6e 67 65 2d 63 6f 75 6e 74 65 72 0a 20 20 20 20  nge-counter.    
13f83 2a 2a 20 69 6e 20 27 64 69 72 65 63 74 27 20 6d  ** in 'direct' m
13f84 6f 64 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ode. In this cas
13f85 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  e the journal fi
13f86 6c 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65  le will never be
13f87 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20  .    ** created 
13f88 66 6f 72 20 74 68 69 73 20 74 72 61 6e 73 61 63  for this transac
13f89 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 23 69 66  tion..    */.#if
13f8a 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
13f8b 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20  E_ATOMIC_WRITE. 
13f8c 20 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a 20     PgHdr *pPg;. 
13f8d 20 20 20 61 73 73 65 72 74 28 20 69 73 4f 70 65     assert( isOpe
13f8e 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 7c  n(pPager->jfd) |
13f8f 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  | pPager->journa
13f90 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  lMode==PAGER_JOU
13f91 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b 0a  RNALMODE_OFF );.
13f92 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74 65 72      if( !zMaster
13f93 20 26 26 20 69 73 4f 70 65 6e 28 70 50 61 67 65   && isOpen(pPage
13f94 72 2d 3e 6a 66 64 29 20 0a 20 20 20 20 20 26 26  r->jfd) .     &&
13f95 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
13f96 4f 66 66 3d 3d 6a 72 6e 6c 42 75 66 66 65 72 53  Off==jrnlBufferS
13f97 69 7a 65 28 70 50 61 67 65 72 29 20 0a 20 20 20  ize(pPager) .   
13f98 20 20 26 26 20 70 50 61 67 65 72 2d 3e 64 62 53    && pPager->dbS
13f99 69 7a 65 3e 3d 70 50 61 67 65 72 2d 3e 64 62 46  ize>=pPager->dbF
13f9a 69 6c 65 53 69 7a 65 0a 20 20 20 20 20 26 26 20  ileSize.     && 
13f9b 28 30 3d 3d 28 70 50 67 20 3d 20 73 71 6c 69 74  (0==(pPg = sqlit
13f9c 65 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 73  e3PcacheDirtyLis
13f9d 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  t(pPager->pPCach
13f9e 65 29 29 20 7c 7c 20 30 3d 3d 70 50 67 2d 3e 70  e)) || 0==pPg->p
13f9f 44 69 72 74 79 29 0a 20 20 20 20 29 7b 0a 20 20  Dirty).    ){.  
13fa0 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68      /* Update th
13fa1 65 20 64 62 20 66 69 6c 65 20 63 68 61 6e 67 65  e db file change
13fa2 20 63 6f 75 6e 74 65 72 20 76 69 61 20 74 68 65   counter via the
13fa3 20 64 69 72 65 63 74 2d 77 72 69 74 65 20 6d 65   direct-write me
13fa4 74 68 6f 64 2e 20 54 68 65 20 0a 20 20 20 20 20  thod. The .     
13fa5 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61   ** following ca
13fa6 6c 6c 20 77 69 6c 6c 20 6d 6f 64 69 66 79 20 74  ll will modify t
13fa7 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70  he in-memory rep
13fa8 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 70  resentation of p
13fa9 61 67 65 20 31 20 0a 20 20 20 20 20 20 2a 2a 20  age 1 .      ** 
13faa 74 6f 20 69 6e 63 6c 75 64 65 20 74 68 65 20 75  to include the u
13fab 70 64 61 74 65 64 20 63 68 61 6e 67 65 20 63 6f  pdated change co
13fac 75 6e 74 65 72 20 61 6e 64 20 74 68 65 6e 20 77  unter and then w
13fad 72 69 74 65 20 70 61 67 65 20 31 20 0a 20 20 20  rite page 1 .   
13fae 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79 20 74     ** directly t
13faf 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
13fb0 69 6c 65 2e 20 42 65 63 61 75 73 65 20 6f 66 20  ile. Because of 
13fb1 74 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65  the atomic-write
13fb2 20 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f 70 65   .      ** prope
13fb3 72 74 79 20 6f 66 20 74 68 65 20 68 6f 73 74 20  rty of the host 
13fb4 66 69 6c 65 2d 73 79 73 74 65 6d 2c 20 74 68 69  file-system, thi
13fb5 73 20 69 73 20 73 61 66 65 2e 0a 20 20 20 20 20  s is safe..     
13fb6 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 70   */.      rc = p
13fb7 61 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65  ager_incr_change
13fb8 63 6f 75 6e 74 65 72 28 70 50 61 67 65 72 2c 20  counter(pPager, 
13fb9 31 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  1);.    }else{. 
13fba 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
13fbb 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65 28 70  3JournalCreate(p
13fbc 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20  Pager->jfd);.   
13fbd 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
13fbe 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
13fbf 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f  rc = pager_incr_
13fc0 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50  changecounter(pP
13fc1 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20  ager, 0);.      
13fc2 7d 0a 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20  }.    }.#else.  
13fc3 20 20 72 63 20 3d 20 70 61 67 65 72 5f 69 6e 63    rc = pager_inc
13fc4 72 5f 63 68 61 6e 67 65 63 6f 75 6e 74 65 72 28  r_changecounter(
13fc5 70 50 61 67 65 72 2c 20 30 29 3b 0a 23 65 6e 64  pPager, 0);.#end
13fc6 69 66 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  if.    if( rc!=S
13fc7 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
13fc8 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65  commit_phase_one
13fc9 5f 65 78 69 74 3b 0a 0a 20 20 20 20 2f 2a 20 49  _exit;..    /* I
13fca 66 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69  f this transacti
13fcb 6f 6e 20 68 61 73 20 6d 61 64 65 20 74 68 65 20  on has made the 
13fcc 64 61 74 61 62 61 73 65 20 73 6d 61 6c 6c 65 72  database smaller
13fcd 2c 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 65 73  , then all pages
13fce 0a 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 64 69  .    ** being di
13fcf 73 63 61 72 64 65 64 20 62 79 20 74 68 65 20 74  scarded by the t
13fd0 72 75 6e 63 61 74 69 6f 6e 20 6d 75 73 74 20 62  runcation must b
13fd1 65 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65  e written to the
13fd2 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20   journal.    ** 
13fd3 66 69 6c 65 2e 20 54 68 69 73 20 63 61 6e 20 6f  file. This can o
13fd4 6e 6c 79 20 68 61 70 70 65 6e 20 69 6e 20 61 75  nly happen in au
13fd5 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65 2e 0a  to-vacuum mode..
13fd6 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 42 65      **.    ** Be
13fd7 66 6f 72 65 20 72 65 61 64 69 6e 67 20 74 68 65  fore reading the
13fd8 20 70 61 67 65 73 20 77 69 74 68 20 70 61 67 65   pages with page
13fd9 20 6e 75 6d 62 65 72 73 20 6c 61 72 67 65 72 20   numbers larger 
13fda 74 68 61 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a  than the .    **
13fdb 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f   current value o
13fdc 66 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20  f Pager.dbSize, 
13fdd 73 65 74 20 64 62 53 69 7a 65 20 62 61 63 6b 20  set dbSize back 
13fde 74 6f 20 74 68 65 20 76 61 6c 75 65 0a 20 20 20  to the value.   
13fdf 20 2a 2a 20 74 68 61 74 20 69 74 20 74 6f 6f 6b   ** that it took
13fe0 20 61 74 20 74 68 65 20 73 74 61 72 74 20 6f 66   at the start of
13fe1 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
13fe2 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65  . Otherwise, the
13fe3 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 73 20 74 6f  .    ** calls to
13fe4 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
13fe5 28 29 20 72 65 74 75 72 6e 20 7a 65 72 6f 65 64  () return zeroed
13fe6 20 70 61 67 65 73 20 69 6e 73 74 65 61 64 20 6f   pages instead o
13fe7 66 20 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e  f .    ** readin
13fe8 67 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20  g data from the 
13fe9 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20  database file.. 
13fea 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 68 65     **.    ** Whe
13feb 6e 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3d 3d  n journal_mode==
13fec 4f 46 46 20 74 68 65 20 64 62 4f 72 69 67 53 69  OFF the dbOrigSi
13fed 7a 65 20 69 73 20 61 6c 77 61 79 73 20 7a 65 72  ze is always zer
13fee 6f 2c 20 73 6f 20 74 68 69 73 0a 20 20 20 20 2a  o, so this.    *
13fef 2a 20 62 6c 6f 63 6b 20 6e 65 76 65 72 20 72 75  * block never ru
13ff0 6e 73 20 69 66 20 6a 6f 75 72 6e 61 6c 5f 6d 6f  ns if journal_mo
13ff1 64 65 3d 4f 46 46 2e 0a 20 20 20 20 2a 2f 0a 23  de=OFF..    */.#
13ff2 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
13ff3 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
13ff4 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62    if( pPager->db
13ff5 53 69 7a 65 3c 70 50 61 67 65 72 2d 3e 64 62 4f  Size<pPager->dbO
13ff6 72 69 67 53 69 7a 65 20 0a 20 20 20 20 20 26 26  rigSize .     &&
13ff7 20 41 4c 57 41 59 53 28 70 50 61 67 65 72 2d 3e   ALWAYS(pPager->
13ff8 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47  journalMode!=PAG
13ff9 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f  ER_JOURNALMODE_O
13ffa 46 46 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  FF).    ){.     
13ffb 20 50 67 6e 6f 20 69 3b 20 20 20 20 20 20 20 20   Pgno i;        
13ffc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13ffd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74             /* It
13ffe 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20  erator variable 
13fff 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 50  */.      const P
14000 67 6e 6f 20 69 53 6b 69 70 20 3d 20 50 41 47 45  gno iSkip = PAGE
14001 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72  R_MJ_PGNO(pPager
14002 29 3b 20 2f 2a 20 50 65 6e 64 69 6e 67 20 6c 6f  ); /* Pending lo
14003 63 6b 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 20  ck page */.     
14004 20 63 6f 6e 73 74 20 50 67 6e 6f 20 64 62 53 69   const Pgno dbSi
14005 7a 65 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53  ze = pPager->dbS
14006 69 7a 65 3b 20 20 20 20 20 20 20 2f 2a 20 44 61  ize;       /* Da
14007 74 61 62 61 73 65 20 69 6d 61 67 65 20 73 69 7a  tabase image siz
14008 65 20 2a 2f 20 0a 20 20 20 20 20 20 70 50 61 67  e */ .      pPag
14009 65 72 2d 3e 64 62 53 69 7a 65 20 3d 20 70 50 61  er->dbSize = pPa
1400a 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3b  ger->dbOrigSize;
1400b 0a 20 20 20 20 20 20 66 6f 72 28 20 69 3d 64 62  .      for( i=db
1400c 53 69 7a 65 2b 31 3b 20 69 3c 3d 70 50 61 67 65  Size+1; i<=pPage
1400d 72 2d 3e 64 62 4f 72 69 67 53 69 7a 65 3b 20 69  r->dbOrigSize; i
1400e 2b 2b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ++ ){.        if
1400f 28 20 21 73 71 6c 69 74 65 33 42 69 74 76 65 63  ( !sqlite3Bitvec
14010 54 65 73 74 28 70 50 61 67 65 72 2d 3e 70 49 6e  Test(pPager->pIn
14011 4a 6f 75 72 6e 61 6c 2c 20 69 29 20 26 26 20 69  Journal, i) && i
14012 21 3d 69 53 6b 69 70 20 29 7b 0a 20 20 20 20 20  !=iSkip ){.     
14013 20 20 20 20 20 50 67 48 64 72 20 2a 70 50 61 67       PgHdr *pPag
14014 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  e;             /
14015 2a 20 50 61 67 65 20 74 6f 20 6a 6f 75 72 6e 61  * Page to journa
14016 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 72  l */.          r
14017 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
14018 47 65 74 28 70 50 61 67 65 72 2c 20 69 2c 20 26  Get(pPager, i, &
14019 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  pPage);.        
1401a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1401b 5f 4f 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69  _OK ) goto commi
1401c 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74  t_phase_one_exit
1401d 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  ;.          rc =
1401e 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
1401f 74 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20  te(pPage);.     
14020 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
14021 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a 20  rUnref(pPage);. 
14022 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21           if( rc!
14023 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
14024 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f  o commit_phase_o
14025 6e 65 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20  ne_exit;.       
14026 20 7d 0a 20 20 20 20 20 20 7d 20 0a 20 20 20 20   }.      } .    
14027 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65    pPager->dbSize
14028 20 3d 20 64 62 53 69 7a 65 3b 0a 20 20 20 20 7d   = dbSize;.    }
14029 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20  .#endif..    /* 
1402a 57 72 69 74 65 20 74 68 65 20 6d 61 73 74 65 72  Write the master
1402b 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 69 6e   journal name in
1402c 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  to the journal f
1402d 69 6c 65 2e 20 49 66 20 61 20 6d 61 73 74 65 72  ile. If a master
1402e 20 0a 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c   .    ** journal
1402f 20 66 69 6c 65 20 6e 61 6d 65 20 68 61 73 20 61   file name has a
14030 6c 72 65 61 64 79 20 62 65 65 6e 20 77 72 69 74  lready been writ
14031 74 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ten to the journ
14032 61 6c 20 66 69 6c 65 2c 20 0a 20 20 20 20 2a 2a  al file, .    **
14033 20 6f 72 20 69 66 20 7a 4d 61 73 74 65 72 20 69   or if zMaster i
14034 73 20 4e 55 4c 4c 20 28 6e 6f 20 6d 61 73 74 65  s NULL (no maste
14035 72 20 6a 6f 75 72 6e 61 6c 29 2c 20 74 68 65 6e  r journal), then
14036 20 74 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20   this call is a 
14037 6e 6f 2d 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20  no-op..    */.  
14038 20 20 72 63 20 3d 20 77 72 69 74 65 4d 61 73 74    rc = writeMast
14039 65 72 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72  erJournal(pPager
1403a 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  , zMaster);.    
1403b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1403c 4b 20 29 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f  K ) goto commit_
1403d 70 68 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a  phase_one_exit;.
1403e 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20 74 68 65  .    /* Sync the
1403f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49   journal file. I
14040 66 20 74 68 65 20 61 74 6f 6d 69 63 2d 75 70 64  f the atomic-upd
14041 61 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ate optimization
14042 20 69 73 20 62 65 69 6e 67 0a 20 20 20 20 2a 2a   is being.    **
14043 20 75 73 65 64 2c 20 74 68 69 73 20 63 61 6c 6c   used, this call
14044 20 77 69 6c 6c 20 6e 6f 74 20 63 72 65 61 74 65   will not create
14045 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
14046 65 20 6f 72 20 70 65 72 66 6f 72 6d 20 61 6e 79  e or perform any
14047 0a 20 20 20 20 2a 2a 20 72 65 61 6c 20 49 4f 2e  .    ** real IO.
14048 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d  .    */.    rc =
14049 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61   syncJournal(pPa
1404a 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ger);.    if( rc
1404b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
1404c 74 6f 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f  to commit_phase_
1404d 6f 6e 65 5f 65 78 69 74 3b 0a 0a 20 20 20 20 2f  one_exit;..    /
1404e 2a 20 57 72 69 74 65 20 61 6c 6c 20 64 69 72 74  * Write all dirt
1404f 79 20 70 61 67 65 73 20 74 6f 20 74 68 65 20 64  y pages to the d
14050 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 2a 2f  atabase file. */
14051 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f  .    rc = pager_
14052 77 72 69 74 65 5f 70 61 67 65 6c 69 73 74 28 73  write_pagelist(s
14053 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 72 74  qlite3PcacheDirt
14054 79 4c 69 73 74 28 70 50 61 67 65 72 2d 3e 70 50  yList(pPager->pP
14055 43 61 63 68 65 29 29 3b 0a 20 20 20 20 69 66 28  Cache));.    if(
14056 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
14057 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
14058 72 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc!=SQLITE_IOERR
14059 5f 42 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 20 20  _BLOCKED );.    
1405a 20 20 67 6f 74 6f 20 63 6f 6d 6d 69 74 5f 70 68    goto commit_ph
1405b 61 73 65 5f 6f 6e 65 5f 65 78 69 74 3b 0a 20 20  ase_one_exit;.  
1405c 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50    }.    sqlite3P
1405d 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 70 50  cacheCleanAll(pP
1405e 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a  ager->pPCache);.
1405f 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66  .    /* If the f
14060 69 6c 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 6e  ile on disk is n
14061 6f 74 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65  ot the same size
14062 20 61 73 20 74 68 65 20 64 61 74 61 62 61 73 65   as the database
14063 20 69 6d 61 67 65 2c 0a 20 20 20 20 2a 2a 20 74   image,.    ** t
14064 68 65 6e 20 75 73 65 20 70 61 67 65 72 5f 74 72  hen use pager_tr
14065 75 6e 63 61 74 65 20 74 6f 20 67 72 6f 77 20 6f  uncate to grow o
14066 72 20 73 68 72 69 6e 6b 20 74 68 65 20 66 69 6c  r shrink the fil
14067 65 20 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20  e here..    */. 
14068 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64     if( pPager->d
14069 62 53 69 7a 65 21 3d 70 50 61 67 65 72 2d 3e 64  bSize!=pPager->d
1406a 62 46 69 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20  bFileSize ){.   
1406b 20 20 20 50 67 6e 6f 20 6e 4e 65 77 20 3d 20 70     Pgno nNew = p
1406c 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 2d 20  Pager->dbSize - 
1406d 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3d  (pPager->dbSize=
1406e 3d 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70  =PAGER_MJ_PGNO(p
1406f 50 61 67 65 72 29 29 3b 0a 20 20 20 20 20 20 61  Pager));.      a
14070 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
14071 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c  tate>=PAGER_EXCL
14072 55 53 49 56 45 20 29 3b 0a 20 20 20 20 20 20 72  USIVE );.      r
14073 63 20 3d 20 70 61 67 65 72 5f 74 72 75 6e 63 61  c = pager_trunca
14074 74 65 28 70 50 61 67 65 72 2c 20 6e 4e 65 77 29  te(pPager, nNew)
14075 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
14076 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
14077 20 63 6f 6d 6d 69 74 5f 70 68 61 73 65 5f 6f 6e   commit_phase_on
14078 65 5f 65 78 69 74 3b 0a 20 20 20 20 7d 0a 0a 20  e_exit;.    }.. 
14079 20 20 20 2f 2a 20 46 69 6e 61 6c 6c 79 2c 20 73     /* Finally, s
1407a 79 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65  ync the database
1407b 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66   file. */.    if
1407c 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  ( !pPager->noSyn
1407d 63 20 26 26 20 21 6e 6f 53 79 6e 63 20 29 7b 0a  c && !noSync ){.
1407e 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1407f 65 33 4f 73 53 79 6e 63 28 70 50 61 67 65 72 2d  e3OsSync(pPager-
14080 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e  >fd, pPager->syn
14081 63 5f 66 6c 61 67 73 29 3b 0a 20 20 20 20 7d 0a  c_flags);.    }.
14082 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 44 42      IOTRACE(("DB
14083 53 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67  SYNC %p\n", pPag
14084 65 72 29 29 0a 0a 20 20 20 20 70 50 61 67 65 72  er))..    pPager
14085 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f  ->state = PAGER_
14086 53 59 4e 43 45 44 3b 0a 20 20 7d 0a 0a 63 6f 6d  SYNCED;.  }..com
14087 6d 69 74 5f 70 68 61 73 65 5f 6f 6e 65 5f 65 78  mit_phase_one_ex
14088 69 74 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  it:.  return rc;
14089 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20  .}.../*.** When 
1408a 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
1408b 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 64 61 74   called, the dat
1408c 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 62  abase file has b
1408d 65 65 6e 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a  een completely.*
1408e 2a 20 75 70 64 61 74 65 64 20 74 6f 20 72 65 66  * updated to ref
1408f 6c 65 63 74 20 74 68 65 20 63 68 61 6e 67 65 73  lect the changes
14090 20 6d 61 64 65 20 62 79 20 74 68 65 20 63 75 72   made by the cur
14091 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  rent transaction
14092 20 61 6e 64 0a 2a 2a 20 73 79 6e 63 65 64 20 74   and.** synced t
14093 6f 20 64 69 73 6b 2e 20 54 68 65 20 6a 6f 75 72  o disk. The jour
14094 6e 61 6c 20 66 69 6c 65 20 73 74 69 6c 6c 20 65  nal file still e
14095 78 69 73 74 73 20 69 6e 20 74 68 65 20 66 69 6c  xists in the fil
14096 65 2d 73 79 73 74 65 6d 20 0a 2a 2a 20 74 68 6f  e-system .** tho
14097 75 67 68 2c 20 61 6e 64 20 69 66 20 61 20 66 61  ugh, and if a fa
14098 69 6c 75 72 65 20 6f 63 63 75 72 73 20 61 74 20  ilure occurs at 
14099 74 68 69 73 20 70 6f 69 6e 74 20 69 74 20 77 69  this point it wi
1409a 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a  ll eventually.**
1409b 20 62 65 20 75 73 65 64 20 61 73 20 61 20 68 6f   be used as a ho
1409c 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 74 68  t-journal and th
1409d 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61  e current transa
1409e 63 74 69 6f 6e 20 72 6f 6c 6c 65 64 20 62 61 63  ction rolled bac
1409f 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75  k..**.** This fu
140a0 6e 63 74 69 6f 6e 20 66 69 6e 61 6c 69 7a 65 73  nction finalizes
140a1 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
140a2 65 2c 20 65 69 74 68 65 72 20 62 79 20 64 65 6c  e, either by del
140a3 65 74 69 6e 67 2c 20 0a 2a 2a 20 74 72 75 6e 63  eting, .** trunc
140a4 61 74 69 6e 67 20 6f 72 20 70 61 72 74 69 61 6c  ating or partial
140a5 6c 79 20 7a 65 72 6f 69 6e 67 20 69 74 2c 20 73  ly zeroing it, s
140a6 6f 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f 74  o that it cannot
140a7 20 62 65 20 75 73 65 64 20 0a 2a 2a 20 66 6f 72   be used .** for
140a8 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72 6f 6c   hot-journal rol
140a9 6c 62 61 63 6b 2e 20 4f 6e 63 65 20 74 68 69 73  lback. Once this
140aa 20 69 73 20 64 6f 6e 65 20 74 68 65 20 74 72 61   is done the tra
140ab 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 69  nsaction is.** i
140ac 72 72 65 76 6f 63 61 62 6c 79 20 63 6f 6d 6d 69  rrevocably commi
140ad 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  tted..**.** If a
140ae 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
140af 61 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65  an IO error code
140b0 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
140b1 20 74 68 65 20 70 61 67 65 72 0a 2a 2a 20 6d 6f   the pager.** mo
140b2 76 65 73 20 69 6e 74 6f 20 74 68 65 20 65 72 72  ves into the err
140b3 6f 72 20 73 74 61 74 65 2e 20 4f 74 68 65 72 77  or state. Otherw
140b4 69 73 65 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 69  ise, SQLITE_OK i
140b5 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53  s returned..*/.S
140b6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
140b7 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  t sqlite3PagerCo
140b8 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 50 61 67  mmitPhaseTwo(Pag
140b9 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69  er *pPager){.  i
140ba 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
140bb 4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  K;              
140bc 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
140bd 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 69 73  de */..  /* This
140be 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20   routine should 
140bf 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 69 66  not be called if
140c0 20 61 20 70 72 69 6f 72 20 65 72 72 6f 72 20 68   a prior error h
140c1 61 73 20 6f 63 63 75 72 72 65 64 2e 0a 20 20 2a  as occurred..  *
140c2 2a 20 42 75 74 20 69 66 20 28 64 75 65 20 74 6f  * But if (due to
140c3 20 61 20 63 6f 64 69 6e 67 20 65 72 72 6f 72 20   a coding error 
140c4 65 6c 73 65 77 68 65 72 65 20 69 6e 20 74 68 65  elsewhere in the
140c5 20 73 79 73 74 65 6d 29 20 69 74 20 64 6f 65 73   system) it does
140c6 20 67 65 74 0a 20 20 2a 2a 20 63 61 6c 6c 65 64   get.  ** called
140c7 2c 20 6a 75 73 74 20 72 65 74 75 72 6e 20 74 68  , just return th
140c8 65 20 73 61 6d 65 20 65 72 72 6f 72 20 63 6f 64  e same error cod
140c9 65 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20  e without doing 
140ca 61 6e 79 74 68 69 6e 67 2e 20 2a 2f 0a 20 20 69  anything. */.  i
140cb 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d  f( NEVER(pPager-
140cc 3e 65 72 72 43 6f 64 65 29 20 29 20 72 65 74 75  >errCode) ) retu
140cd 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f  rn pPager->errCo
140ce 64 65 3b 0a 0a 20 20 2f 2a 20 54 68 69 73 20 66  de;..  /* This f
140cf 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e  unction should n
140d0 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 69 66 20  ot be called if 
140d1 74 68 65 20 70 61 67 65 72 20 69 73 20 6e 6f 74  the pager is not
140d2 20 69 6e 20 61 74 20 6c 65 61 73 74 0a 20 20 2a   in at least.  *
140d3 2a 20 50 41 47 45 52 5f 52 45 53 45 52 56 45 44  * PAGER_RESERVED
140d4 20 73 74 61 74 65 2e 20 41 6e 64 20 69 6e 64 65   state. And inde
140d5 65 64 20 53 51 4c 69 74 65 20 6e 65 76 65 72 20  ed SQLite never 
140d6 64 6f 65 73 20 74 68 69 73 2e 20 42 75 74 20 69  does this. But i
140d7 74 20 69 73 0a 20 20 2a 2a 20 6e 69 63 65 20 74  t is.  ** nice t
140d8 6f 20 68 61 76 65 20 74 68 69 73 20 64 65 66 65  o have this defe
140d9 6e 73 69 76 65 20 74 65 73 74 20 68 65 72 65 20  nsive test here 
140da 61 6e 79 77 61 79 2e 0a 20 20 2a 2f 0a 20 20 69  anyway..  */.  i
140db 66 28 20 4e 45 56 45 52 28 70 50 61 67 65 72 2d  f( NEVER(pPager-
140dc 3e 73 74 61 74 65 3c 50 41 47 45 52 5f 52 45 53  >state<PAGER_RES
140dd 45 52 56 45 44 29 20 29 20 72 65 74 75 72 6e 20  ERVED) ) return 
140de 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 0a 20  SQLITE_ERROR;.. 
140df 20 2f 2a 20 41 6e 20 6f 70 74 69 6d 69 7a 61 74   /* An optimizat
140e0 69 6f 6e 2e 20 49 66 20 74 68 65 20 64 61 74 61  ion. If the data
140e1 62 61 73 65 20 77 61 73 20 6e 6f 74 20 61 63 74  base was not act
140e2 75 61 6c 6c 79 20 6d 6f 64 69 66 69 65 64 20 64  ually modified d
140e3 75 72 69 6e 67 0a 20 20 2a 2a 20 74 68 69 73 20  uring.  ** this 
140e4 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65  transaction, the
140e5 20 70 61 67 65 72 20 69 73 20 72 75 6e 6e 69 6e   pager is runnin
140e6 67 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 6d  g in exclusive-m
140e7 6f 64 65 20 61 6e 64 20 69 73 0a 20 20 2a 2a 20  ode and is.  ** 
140e8 75 73 69 6e 67 20 70 65 72 73 69 73 74 65 6e 74  using persistent
140e9 20 6a 6f 75 72 6e 61 6c 73 2c 20 74 68 65 6e 20   journals, then 
140ea 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
140eb 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2a 0a 20   a no-op..  **. 
140ec 20 2a 2a 20 54 68 65 20 73 74 61 72 74 20 6f 66   ** The start of
140ed 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
140ee 65 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74  e currently cont
140ef 61 69 6e 73 20 61 20 73 69 6e 67 6c 65 20 6a 6f  ains a single jo
140f0 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 65 61 64  urnal .  ** head
140f1 65 72 20 77 69 74 68 20 74 68 65 20 6e 52 65 63  er with the nRec
140f2 20 66 69 65 6c 64 20 73 65 74 20 74 6f 20 30 2e   field set to 0.
140f3 20 49 66 20 73 75 63 68 20 61 20 6a 6f 75 72 6e   If such a journ
140f4 61 6c 20 69 73 20 75 73 65 64 20 61 73 0a 20 20  al is used as.  
140f5 2a 2a 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c  ** a hot-journal
140f6 20 64 75 72 69 6e 67 20 68 6f 74 2d 6a 6f 75 72   during hot-jour
140f7 6e 61 6c 20 72 6f 6c 6c 62 61 63 6b 2c 20 30 20  nal rollback, 0 
140f8 63 68 61 6e 67 65 73 20 77 69 6c 6c 20 62 65 20  changes will be 
140f9 6d 61 64 65 0a 20 20 2a 2a 20 74 6f 20 74 68 65  made.  ** to the
140fa 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
140fb 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e  So there is no n
140fc 65 65 64 20 74 6f 20 7a 65 72 6f 20 74 68 65 20  eed to zero the 
140fd 6a 6f 75 72 6e 61 6c 20 0a 20 20 2a 2a 20 68 65  journal .  ** he
140fe 61 64 65 72 2e 20 53 69 6e 63 65 20 74 68 65 20  ader. Since the 
140ff 70 61 67 65 72 20 69 73 20 69 6e 20 65 78 63 6c  pager is in excl
14100 75 73 69 76 65 20 6d 6f 64 65 2c 20 74 68 65 72  usive mode, ther
14101 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 2a  e is no need.  *
14102 2a 20 74 6f 20 64 72 6f 70 20 61 6e 79 20 6c 6f  * to drop any lo
14103 63 6b 73 20 65 69 74 68 65 72 2e 0a 20 20 2a 2f  cks either..  */
14104 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64  .  if( pPager->d
14105 62 4d 6f 64 69 66 69 65 64 3d 3d 30 20 26 26 20  bModified==0 && 
14106 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
14107 65 4d 6f 64 65 20 0a 20 20 20 26 26 20 70 50 61  eMode .   && pPa
14108 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
14109 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
1410a 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 20 29 7b  ODE_PERSIST.  ){
1410b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
1410c 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3d  ger->journalOff=
1410d 3d 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28  =JOURNAL_HDR_SZ(
1410e 70 50 61 67 65 72 29 20 29 3b 0a 20 20 20 20 72  pPager) );.    r
1410f 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
14110 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54 52 41  .  }..  PAGERTRA
14111 43 45 28 28 22 43 4f 4d 4d 49 54 20 25 64 5c 6e  CE(("COMMIT %d\n
14112 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65  ", PAGERID(pPage
14113 72 29 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r)));.  assert( 
14114 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50  pPager->state==P
14115 41 47 45 52 5f 53 59 4e 43 45 44 20 7c 7c 20 4d  AGER_SYNCED || M
14116 45 4d 44 42 20 7c 7c 20 21 70 50 61 67 65 72 2d  EMDB || !pPager-
14117 3e 64 62 4d 6f 64 69 66 69 65 64 20 29 3b 0a 20  >dbModified );. 
14118 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f   rc = pager_end_
14119 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67  transaction(pPag
1411a 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 74 4d  er, pPager->setM
1411b 61 73 74 65 72 29 3b 0a 20 20 72 65 74 75 72 6e  aster);.  return
1411c 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61   pager_error(pPa
1411d 67 65 72 2c 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a  ger, rc);.}../*.
1411e 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6c 6c 20  ** Rollback all 
1411f 63 68 61 6e 67 65 73 2e 20 54 68 65 20 64 61 74  changes. The dat
14120 61 62 61 73 65 20 66 61 6c 6c 73 20 62 61 63 6b  abase falls back
14121 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52 45 44   to PAGER_SHARED
14122 20 6d 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   mode..**.** Thi
14123 73 20 66 75 6e 63 74 69 6f 6e 20 70 65 72 66 6f  s function perfo
14124 72 6d 73 20 74 77 6f 20 74 61 73 6b 73 3a 0a 2a  rms two tasks:.*
14125 2a 0a 2a 2a 20 20 20 31 29 20 49 74 20 72 6f 6c  *.**   1) It rol
14126 6c 73 20 62 61 63 6b 20 74 68 65 20 6a 6f 75 72  ls back the jour
14127 6e 61 6c 20 66 69 6c 65 2c 20 72 65 73 74 6f 72  nal file, restor
14128 69 6e 67 20 61 6c 6c 20 64 61 74 61 62 61 73 65  ing all database
14129 20 66 69 6c 65 20 61 6e 64 20 0a 2a 2a 20 20 20   file and .**   
1412a 20 20 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63     in-memory cac
1412b 68 65 20 70 61 67 65 73 20 74 6f 20 74 68 65 20  he pages to the 
1412c 73 74 61 74 65 20 74 68 65 79 20 77 65 72 65 20  state they were 
1412d 69 6e 20 77 68 65 6e 20 74 68 65 20 74 72 61 6e  in when the tran
1412e 73 61 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20  saction.**      
1412f 77 61 73 20 6f 70 65 6e 65 64 2c 20 61 6e 64 0a  was opened, and.
14130 2a 2a 20 20 20 32 29 20 49 74 20 66 69 6e 61 6c  **   2) It final
14131 69 7a 65 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c  izes the journal
14132 20 66 69 6c 65 2c 20 73 6f 20 74 68 61 74 20 69   file, so that i
14133 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20 66 6f  t is not used fo
14134 72 20 68 6f 74 0a 2a 2a 20 20 20 20 20 20 72 6f  r hot.**      ro
14135 6c 6c 62 61 63 6b 20 61 74 20 61 6e 79 20 70 6f  llback at any po
14136 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 72  int in the futur
14137 65 2e 0a 2a 2a 0a 2a 2a 20 73 75 62 6a 65 63 74  e..**.** subject
14138 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   to the followin
14139 67 20 71 75 61 6c 69 66 69 63 61 74 69 6f 6e 73  g qualifications
1413a 3a 0a 2a 2a 0a 2a 2a 20 2a 20 49 66 20 74 68 65  :.**.** * If the
1413b 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
1413c 20 6e 6f 74 20 79 65 74 20 6f 70 65 6e 20 77 68   not yet open wh
1413d 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
1413e 20 69 73 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20 20   is called,.**  
1413f 20 74 68 65 6e 20 6f 6e 6c 79 20 28 32 29 20 69   then only (2) i
14140 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 49 6e 20  s performed. In 
14141 74 68 69 73 20 63 61 73 65 20 74 68 65 72 65 20  this case there 
14142 69 73 20 6e 6f 20 6a 6f 75 72 6e 61 6c 20 66 69  is no journal fi
14143 6c 65 0a 2a 2a 20 20 20 74 6f 20 72 6f 6c 6c 20  le.**   to roll 
14144 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 2a 20 49 66  back..**.** * If
14145 20 69 6e 20 61 6e 20 65 72 72 6f 72 20 73 74 61   in an error sta
14146 74 65 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51  te other than SQ
14147 4c 49 54 45 5f 46 55 4c 4c 2c 20 74 68 65 6e 20  LITE_FULL, then 
14148 74 61 73 6b 20 28 31 29 20 69 73 20 0a 2a 2a 20  task (1) is .** 
14149 20 20 70 65 72 66 6f 72 6d 65 64 2e 20 49 66 20    performed. If 
1414a 73 75 63 63 65 73 73 66 75 6c 2c 20 74 61 73 6b  successful, task
1414b 20 28 32 29 2e 20 52 65 67 61 72 64 6c 65 73 73   (2). Regardless
1414c 20 6f 66 20 74 68 65 20 6f 75 74 63 6f 6d 65 0a   of the outcome.
1414d 2a 2a 20 20 20 6f 66 20 65 69 74 68 65 72 2c 20  **   of either, 
1414e 74 68 65 20 65 72 72 6f 72 20 73 74 61 74 65 20  the error state 
1414f 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
14150 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61  turned to the ca
14151 6c 6c 65 72 0a 2a 2a 20 20 20 28 69 2e 65 2e 20  ller.**   (i.e. 
14152 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f 49 4f  either SQLITE_IO
14153 45 52 52 20 6f 72 20 53 51 4c 49 54 45 5f 43 4f  ERR or SQLITE_CO
14154 52 52 55 50 54 29 2e 0a 2a 2a 0a 2a 2a 20 2a 20  RRUPT)..**.** * 
14155 49 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20  If the pager is 
14156 69 6e 20 50 41 47 45 52 5f 52 45 53 45 52 56 45  in PAGER_RESERVE
14157 44 20 73 74 61 74 65 2c 20 74 68 65 6e 20 61 74  D state, then at
14158 74 65 6d 70 74 20 28 31 29 2e 20 57 68 65 74 68  tempt (1). Wheth
14159 65 72 0a 2a 2a 20 20 20 6f 72 20 6e 6f 74 20 28  er.**   or not (
1415a 31 29 20 69 73 20 73 75 63 63 75 73 73 66 75 6c  1) is succussful
1415b 2c 20 61 6c 73 6f 20 61 74 74 65 6d 70 74 20 28  , also attempt (
1415c 32 29 2e 20 49 66 20 73 75 63 63 65 73 73 66 75  2). If successfu
1415d 6c 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 53  l, return.**   S
1415e 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77  QLITE_OK. Otherw
1415f 69 73 65 2c 20 65 6e 74 65 72 20 74 68 65 20 65  ise, enter the e
14160 72 72 6f 72 20 73 74 61 74 65 20 61 6e 64 20 72  rror state and r
14161 65 74 75 72 6e 20 74 68 65 20 66 69 72 73 74 20  eturn the first 
14162 0a 2a 2a 20 20 20 65 72 72 6f 72 20 63 6f 64 65  .**   error code
14163 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 20 0a 2a   encountered. .*
14164 2a 0a 2a 2a 20 20 20 49 6e 20 74 68 69 73 20 63  *.**   In this c
14165 61 73 65 20 74 68 65 72 65 20 69 73 20 6e 6f 20  ase there is no 
14166 63 68 61 6e 63 65 20 74 68 61 74 20 74 68 65 20  chance that the 
14167 64 61 74 61 62 61 73 65 20 77 61 73 20 77 72 69  database was wri
14168 74 74 65 6e 20 74 6f 2e 20 0a 2a 2a 20 20 20 53  tten to. .**   S
14169 6f 20 69 73 20 73 61 66 65 20 74 6f 20 66 69 6e  o is safe to fin
1416a 61 6c 69 7a 65 20 74 68 65 20 6a 6f 75 72 6e 61  alize the journa
1416b 6c 20 66 69 6c 65 20 65 76 65 6e 20 69 66 20 74  l file even if t
1416c 68 65 20 70 6c 61 79 62 61 63 6b 20 0a 2a 2a 20  he playback .** 
1416d 20 20 28 6f 70 65 72 61 74 69 6f 6e 20 31 29 20    (operation 1) 
1416e 66 61 69 6c 65 64 2e 20 48 6f 77 65 76 65 72 20  failed. However 
1416f 74 68 65 20 70 61 67 65 72 20 6d 75 73 74 20 65  the pager must e
14170 6e 74 65 72 20 74 68 65 20 65 72 72 6f 72 20 73  nter the error s
14171 74 61 74 65 0a 2a 2a 20 20 20 61 73 20 74 68 65  tate.**   as the
14172 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
14173 20 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65   in-memory cache
14174 20 61 72 65 20 6e 6f 77 20 73 75 73 70 65 63 74   are now suspect
14175 2e 0a 2a 2a 0a 2a 2a 20 2a 20 46 69 6e 61 6c 6c  ..**.** * Finall
14176 79 2c 20 69 66 20 69 6e 20 50 41 47 45 52 5f 45  y, if in PAGER_E
14177 58 43 4c 55 53 49 56 45 20 73 74 61 74 65 2c 20  XCLUSIVE state, 
14178 74 68 65 6e 20 61 74 74 65 6d 70 74 20 28 31 29  then attempt (1)
14179 2e 20 4f 6e 6c 79 0a 2a 2a 20 20 20 61 74 74 65  . Only.**   atte
1417a 6d 70 74 20 28 32 29 20 69 66 20 28 31 29 20 69  mpt (2) if (1) i
1417b 73 20 73 75 63 63 65 73 73 66 75 6c 2e 20 52 65  s successful. Re
1417c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69  turn SQLITE_OK i
1417d 66 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a  f successful,.**
1417e 20 20 20 6f 74 68 65 72 77 69 73 65 20 65 6e 74     otherwise ent
1417f 65 72 20 74 68 65 20 65 72 72 6f 72 20 73 74 61  er the error sta
14180 74 65 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68  te and return th
14181 65 20 65 72 72 6f 72 20 63 6f 64 65 20 66 72 6f  e error code fro
14182 6d 20 74 68 65 20 0a 2a 2a 20 20 20 66 61 69 6c  m the .**   fail
14183 69 6e 67 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a  ing operation..*
14184 2a 0a 2a 2a 20 20 20 49 6e 20 74 68 69 73 20 63  *.**   In this c
14185 61 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ase the database
14186 20 66 69 6c 65 20 6d 61 79 20 68 61 76 65 20 62   file may have b
14187 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 2e 20  een written to. 
14188 53 6f 20 69 66 20 74 68 65 0a 2a 2a 20 20 20 70  So if the.**   p
14189 6c 61 79 62 61 63 6b 20 6f 70 65 72 61 74 69 6f  layback operatio
1418a 6e 20 64 69 64 20 6e 6f 74 20 73 75 63 63 65 65  n did not succee
1418b 64 20 69 74 20 77 6f 75 6c 64 20 6e 6f 74 20 62  d it would not b
1418c 65 20 73 61 66 65 20 74 6f 20 66 69 6e 61 6c 69  e safe to finali
1418d 7a 65 0a 2a 2a 20 20 20 74 68 65 20 6a 6f 75 72  ze.**   the jour
1418e 6e 61 6c 20 66 69 6c 65 2e 20 49 74 20 6e 65 65  nal file. It nee
1418f 64 73 20 74 6f 20 62 65 20 6c 65 66 74 20 69 6e  ds to be left in
14190 20 74 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d   the file-system
14191 20 73 6f 20 74 68 61 74 0a 2a 2a 20 20 20 73 6f   so that.**   so
14192 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  me other process
14193 20 63 61 6e 20 75 73 65 20 69 74 20 74 6f 20 72   can use it to r
14194 65 73 74 6f 72 65 20 74 68 65 20 64 61 74 61 62  estore the datab
14195 61 73 65 20 73 74 61 74 65 20 28 62 79 0a 2a 2a  ase state (by.**
14196 20 20 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 20 72     hot-journal r
14197 6f 6c 6c 62 61 63 6b 29 2e 0a 2a 2f 0a 53 51 4c  ollback)..*/.SQL
14198 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
14199 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c  sqlite3PagerRoll
1419a 62 61 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67  back(Pager *pPag
1419b 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  er){.  int rc = 
1419c 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20  SQLITE_OK;      
1419d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
1419e 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
1419f 50 41 47 45 52 54 52 41 43 45 28 28 22 52 4f 4c  PAGERTRACE(("ROL
141a0 4c 42 41 43 4b 20 25 64 5c 6e 22 2c 20 50 41 47  LBACK %d\n", PAG
141a1 45 52 49 44 28 70 50 61 67 65 72 29 29 29 3b 0a  ERID(pPager)));.
141a2 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 64    if( !pPager->d
141a3 62 4d 6f 64 69 66 69 65 64 20 7c 7c 20 21 69 73  bModified || !is
141a4 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
141a5 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61  ) ){.    rc = pa
141a6 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74  ger_end_transact
141a7 69 6f 6e 28 70 50 61 67 65 72 2c 20 70 50 61 67  ion(pPager, pPag
141a8 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a  er->setMaster);.
141a9 20 20 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67    }else if( pPag
141aa 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 26 20 70  er->errCode && p
141ab 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 21 3d  Pager->errCode!=
141ac 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29 7b 0a 20  SQLITE_FULL ){. 
141ad 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73     if( pPager->s
141ae 74 61 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c  tate>=PAGER_EXCL
141af 55 53 49 56 45 20 29 7b 0a 20 20 20 20 20 20 70  USIVE ){.      p
141b0 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50  ager_playback(pP
141b1 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  ager, 0);.    }.
141b2 20 20 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d      rc = pPager-
141b3 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d 65 6c 73  >errCode;.  }els
141b4 65 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  e{.    if( pPage
141b5 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f  r->state==PAGER_
141b6 52 45 53 45 52 56 45 44 20 29 7b 0a 20 20 20 20  RESERVED ){.    
141b7 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20    int rc2;.     
141b8 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79   rc = pager_play
141b9 62 61 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b  back(pPager, 0);
141ba 0a 20 20 20 20 20 20 72 63 32 20 3d 20 70 61 67  .      rc2 = pag
141bb 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69  er_end_transacti
141bc 6f 6e 28 70 50 61 67 65 72 2c 20 70 50 61 67 65  on(pPager, pPage
141bd 72 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a 20  r->setMaster);. 
141be 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
141bf 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
141c0 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20    rc = rc2;.    
141c1 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
141c2 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
141c3 70 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c  playback(pPager,
141c4 20 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20   0);.    }..    
141c5 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20  if( !MEMDB ){.  
141c6 20 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69      pPager->dbSi
141c7 7a 65 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20  zeValid = 0;.   
141c8 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e   }..    /* If an
141c9 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75   error occurs du
141ca 72 69 6e 67 20 61 20 52 4f 4c 4c 42 41 43 4b 2c  ring a ROLLBACK,
141cb 20 77 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65   we can no longe
141cc 72 20 74 72 75 73 74 20 74 68 65 20 70 61 67 65  r trust the page
141cd 72 0a 20 20 20 20 2a 2a 20 63 61 63 68 65 2e 20  r.    ** cache. 
141ce 53 6f 20 63 61 6c 6c 20 70 61 67 65 72 5f 65 72  So call pager_er
141cf 72 6f 72 28 29 20 6f 6e 20 74 68 65 20 77 61 79  ror() on the way
141d0 20 6f 75 74 20 74 6f 20 6d 61 6b 65 20 61 6e 79   out to make any
141d1 20 65 72 72 6f 72 20 0a 20 20 20 20 2a 2a 20 70   error .    ** p
141d2 65 72 73 69 73 74 65 6e 74 2e 0a 20 20 20 20 2a  ersistent..    *
141d3 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 67 65 72  /.    rc = pager
141d4 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20 72  _error(pPager, r
141d5 63 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  c);.  }.  return
141d6 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
141d7 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65  turn TRUE if the
141d8 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
141d9 73 20 6f 70 65 6e 65 64 20 72 65 61 64 2d 6f 6e  s opened read-on
141da 6c 79 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53  ly.  Return FALS
141db 45 0a 2a 2a 20 69 66 20 74 68 65 20 64 61 74 61  E.** if the data
141dc 62 61 73 65 20 69 73 20 28 69 6e 20 74 68 65 6f  base is (in theo
141dd 72 79 29 20 77 72 69 74 61 62 6c 65 2e 0a 2a 2f  ry) writable..*/
141de 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
141df 75 38 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  u8 sqlite3PagerI
141e0 73 72 65 61 64 6f 6e 6c 79 28 50 61 67 65 72 20  sreadonly(Pager 
141e1 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75  *pPager){.  retu
141e2 72 6e 20 70 50 61 67 65 72 2d 3e 72 65 61 64 4f  rn pPager->readO
141e3 6e 6c 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  nly;.}../*.** Re
141e4 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
141e5 6f 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f  of references to
141e6 20 74 68 65 20 70 61 67 65 72 2e 0a 2a 2f 0a 53   the pager..*/.S
141e7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
141e8 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65  t sqlite3PagerRe
141e9 66 63 6f 75 6e 74 28 50 61 67 65 72 20 2a 70 50  fcount(Pager *pP
141ea 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ager){.  return 
141eb 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66  sqlite3PcacheRef
141ec 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50  Count(pPager->pP
141ed 43 61 63 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Cache);.}../*.**
141ee 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
141ef 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73  er of references
141f0 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 69 65   to the specifie
141f1 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49 54  d page..*/.SQLIT
141f2 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
141f3 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65  lite3PagerPageRe
141f4 66 63 6f 75 6e 74 28 44 62 50 61 67 65 20 2a 70  fcount(DbPage *p
141f5 50 61 67 65 29 7b 0a 20 20 72 65 74 75 72 6e 20  Page){.  return 
141f6 73 71 6c 69 74 65 33 50 63 61 63 68 65 50 61 67  sqlite3PcachePag
141f7 65 52 65 66 63 6f 75 6e 74 28 70 50 61 67 65 29  eRefcount(pPage)
141f8 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
141f9 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68  TE_TEST./*.** Th
141fa 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
141fb 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61  ed for testing a
141fc 6e 64 20 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79  nd analysis only
141fd 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
141fe 41 54 45 20 69 6e 74 20 2a 73 71 6c 69 74 65 33  ATE int *sqlite3
141ff 50 61 67 65 72 53 74 61 74 73 28 50 61 67 65 72  PagerStats(Pager
14200 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 73 74 61   *pPager){.  sta
14201 74 69 63 20 69 6e 74 20 61 5b 31 31 5d 3b 0a 20  tic int a[11];. 
14202 20 61 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 50   a[0] = sqlite3P
14203 63 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50  cacheRefCount(pP
14204 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a  ager->pPCache);.
14205 20 20 61 5b 31 5d 20 3d 20 73 71 6c 69 74 65 33    a[1] = sqlite3
14206 50 63 61 63 68 65 50 61 67 65 63 6f 75 6e 74 28  PcachePagecount(
14207 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
14208 3b 0a 20 20 61 5b 32 5d 20 3d 20 73 71 6c 69 74  ;.  a[2] = sqlit
14209 65 33 50 63 61 63 68 65 47 65 74 43 61 63 68 65  e3PcacheGetCache
1420a 73 69 7a 65 28 70 50 61 67 65 72 2d 3e 70 50 43  size(pPager->pPC
1420b 61 63 68 65 29 3b 0a 20 20 61 5b 33 5d 20 3d 20  ache);.  a[3] = 
1420c 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61  pPager->dbSizeVa
1420d 6c 69 64 20 3f 20 28 69 6e 74 29 20 70 50 61 67  lid ? (int) pPag
1420e 65 72 2d 3e 64 62 53 69 7a 65 20 3a 20 2d 31 3b  er->dbSize : -1;
1420f 0a 20 20 61 5b 34 5d 20 3d 20 70 50 61 67 65 72  .  a[4] = pPager
14210 2d 3e 73 74 61 74 65 3b 0a 20 20 61 5b 35 5d 20  ->state;.  a[5] 
14211 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  = pPager->errCod
14212 65 3b 0a 20 20 61 5b 36 5d 20 3d 20 70 50 61 67  e;.  a[6] = pPag
14213 65 72 2d 3e 6e 48 69 74 3b 0a 20 20 61 5b 37 5d  er->nHit;.  a[7]
14214 20 3d 20 70 50 61 67 65 72 2d 3e 6e 4d 69 73 73   = pPager->nMiss
14215 3b 0a 20 20 61 5b 38 5d 20 3d 20 30 3b 20 20 2f  ;.  a[8] = 0;  /
14216 2a 20 55 73 65 64 20 74 6f 20 62 65 20 70 50 61  * Used to be pPa
14217 67 65 72 2d 3e 6e 4f 76 66 6c 20 2a 2f 0a 20 20  ger->nOvfl */.  
14218 61 5b 39 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e  a[9] = pPager->n
14219 52 65 61 64 3b 0a 20 20 61 5b 31 30 5d 20 3d 20  Read;.  a[10] = 
1421a 70 50 61 67 65 72 2d 3e 6e 57 72 69 74 65 3b 0a  pPager->nWrite;.
1421b 20 20 72 65 74 75 72 6e 20 61 3b 0a 7d 0a 23 65    return a;.}.#e
1421c 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  ndif../*.** Retu
1421d 72 6e 20 74 72 75 65 20 69 66 20 74 68 69 73 20  rn true if this 
1421e 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  is an in-memory 
1421f 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  pager..*/.SQLITE
14220 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
14221 69 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62  ite3PagerIsMemdb
14222 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
14223 0a 20 20 72 65 74 75 72 6e 20 4d 45 4d 44 42 3b  .  return MEMDB;
14224 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  .}../*.** Check 
14225 74 68 61 74 20 74 68 65 72 65 20 61 72 65 20 61  that there are a
14226 74 20 6c 65 61 73 74 20 6e 53 61 76 65 70 6f 69  t least nSavepoi
14227 6e 74 20 73 61 76 65 70 6f 69 6e 74 73 20 6f 70  nt savepoints op
14228 65 6e 2e 20 49 66 20 74 68 65 72 65 20 61 72 65  en. If there are
14229 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 6c 65  .** currently le
1422a 73 73 20 74 68 61 6e 20 6e 53 61 76 65 70 6f 69  ss than nSavepoi
1422b 6e 74 73 20 6f 70 65 6e 2c 20 74 68 65 6e 20 6f  nts open, then o
1422c 70 65 6e 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20  pen one or more 
1422d 73 61 76 65 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f  savepoints.** to
1422e 20 6d 61 6b 65 20 75 70 20 74 68 65 20 64 69 66   make up the dif
1422f 66 65 72 65 6e 63 65 2e 20 49 66 20 74 68 65 20  ference. If the 
14230 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70 6f  number of savepo
14231 69 6e 74 73 20 69 73 20 61 6c 72 65 61 64 79 0a  ints is already.
14232 2a 2a 20 65 71 75 61 6c 20 74 6f 20 6e 53 61 76  ** equal to nSav
14233 65 70 6f 69 6e 74 2c 20 74 68 65 6e 20 74 68 69  epoint, then thi
14234 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20  s function is a 
14235 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  no-op..**.** If 
14236 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
14237 69 6f 6e 20 66 61 69 6c 73 2c 20 53 51 4c 49 54  ion fails, SQLIT
14238 45 5f 4e 4f 4d 45 4d 20 69 73 20 72 65 74 75 72  E_NOMEM is retur
14239 6e 65 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72  ned. If an error
1423a 20 0a 2a 2a 20 6f 63 63 75 72 73 20 77 68 69 6c   .** occurs whil
1423b 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 73 75  e opening the su
1423c 62 2d 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2c 20  b-journal file, 
1423d 74 68 65 6e 20 61 6e 20 49 4f 20 65 72 72 6f 72  then an IO error
1423e 20 63 6f 64 65 20 69 73 0a 2a 2a 20 72 65 74 75   code is.** retu
1423f 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c  rned. Otherwise,
14240 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53   SQLITE_OK..*/.S
14241 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
14242 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70  t sqlite3PagerOp
14243 65 6e 53 61 76 65 70 6f 69 6e 74 28 50 61 67 65  enSavepoint(Page
14244 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6e  r *pPager, int n
14245 53 61 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 6e  Savepoint){.  in
14246 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
14247 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
14248 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72          /* Retur
14249 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20  n code */.  int 
1424a 6e 43 75 72 72 65 6e 74 20 3d 20 70 50 61 67 65  nCurrent = pPage
1424b 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3b 20 20  r->nSavepoint;  
1424c 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74        /* Current
1424d 20 6e 75 6d 62 65 72 20 6f 66 20 73 61 76 65 70   number of savep
1424e 6f 69 6e 74 73 20 2a 2f 0a 0a 20 20 69 66 28 20  oints */..  if( 
1424f 6e 53 61 76 65 70 6f 69 6e 74 3e 6e 43 75 72 72  nSavepoint>nCurr
14250 65 6e 74 20 26 26 20 70 50 61 67 65 72 2d 3e 75  ent && pPager->u
14251 73 65 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20  seJournal ){.   
14252 20 69 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20   int ii;        
14253 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14254 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72           /* Iter
14255 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f  ator variable */
14256 0a 20 20 20 20 50 61 67 65 72 53 61 76 65 70 6f  .    PagerSavepo
14257 69 6e 74 20 2a 61 4e 65 77 3b 20 20 20 20 20 20  int *aNew;      
14258 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
14259 4e 65 77 20 50 61 67 65 72 2e 61 53 61 76 65 70  New Pager.aSavep
1425a 6f 69 6e 74 20 61 72 72 61 79 20 2a 2f 0a 0a 20  oint array */.. 
1425b 20 20 20 2f 2a 20 45 69 74 68 65 72 20 74 68 65     /* Either the
1425c 72 65 20 69 73 20 6e 6f 20 61 63 74 69 76 65 20  re is no active 
1425d 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65 20 73  journal or the s
1425e 75 62 2d 6a 6f 75 72 6e 61 6c 20 69 73 20 6f 70  ub-journal is op
1425f 65 6e 20 6f 72 20 0a 20 20 20 20 2a 2a 20 74 68  en or .    ** th
14260 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c 77  e journal is alw
14261 61 79 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65  ays stored in me
14262 6d 6f 72 79 20 2a 2f 0a 20 20 20 20 61 73 73 65  mory */.    asse
14263 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 53 61 76  rt( pPager->nSav
14264 65 70 6f 69 6e 74 3d 3d 30 20 7c 7c 20 69 73 4f  epoint==0 || isO
14265 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a 66 64  pen(pPager->sjfd
14266 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20  ) ||.           
14267 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
14268 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
14269 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29  NALMODE_MEMORY )
1426a 3b 0a 0a 20 20 20 20 2f 2a 20 47 72 6f 77 20 74  ;..    /* Grow t
1426b 68 65 20 50 61 67 65 72 2e 61 53 61 76 65 70 6f  he Pager.aSavepo
1426c 69 6e 74 20 61 72 72 61 79 20 75 73 69 6e 67 20  int array using 
1426d 72 65 61 6c 6c 6f 63 28 29 2e 20 52 65 74 75 72  realloc(). Retur
1426e 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20  n SQLITE_NOMEM. 
1426f 20 20 20 2a 2a 20 69 66 20 74 68 65 20 61 6c 6c     ** if the all
14270 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2e 20 4f  ocation fails. O
14271 74 68 65 72 77 69 73 65 2c 20 7a 65 72 6f 20 74  therwise, zero t
14272 68 65 20 6e 65 77 20 70 6f 72 74 69 6f 6e 20 69  he new portion i
14273 6e 20 63 61 73 65 20 61 20 0a 20 20 20 20 2a 2a  n case a .    **
14274 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20   malloc failure 
14275 6f 63 63 75 72 73 20 77 68 69 6c 65 20 70 6f 70  occurs while pop
14276 75 6c 61 74 69 6e 67 20 69 74 20 69 6e 20 74 68  ulating it in th
14277 65 20 66 6f 72 28 2e 2e 2e 29 20 6c 6f 6f 70 20  e for(...) loop 
14278 62 65 6c 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20 20  below..    */.  
14279 20 20 61 4e 65 77 20 3d 20 28 50 61 67 65 72 53    aNew = (PagerS
1427a 61 76 65 70 6f 69 6e 74 20 2a 29 73 71 6c 69 74  avepoint *)sqlit
1427b 65 33 52 65 61 6c 6c 6f 63 28 0a 20 20 20 20 20  e3Realloc(.     
1427c 20 20 20 70 50 61 67 65 72 2d 3e 61 53 61 76 65     pPager->aSave
1427d 70 6f 69 6e 74 2c 20 73 69 7a 65 6f 66 28 50 61  point, sizeof(Pa
1427e 67 65 72 53 61 76 65 70 6f 69 6e 74 29 2a 6e 53  gerSavepoint)*nS
1427f 61 76 65 70 6f 69 6e 74 0a 20 20 20 20 29 3b 0a  avepoint.    );.
14280 20 20 20 20 69 66 28 20 21 61 4e 65 77 20 29 7b      if( !aNew ){
14281 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
14282 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
14283 7d 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 61 4e  }.    memset(&aN
14284 65 77 5b 6e 43 75 72 72 65 6e 74 5d 2c 20 30 2c  ew[nCurrent], 0,
14285 20 28 6e 53 61 76 65 70 6f 69 6e 74 2d 6e 43 75   (nSavepoint-nCu
14286 72 72 65 6e 74 29 20 2a 20 73 69 7a 65 6f 66 28  rrent) * sizeof(
14287 50 61 67 65 72 53 61 76 65 70 6f 69 6e 74 29 29  PagerSavepoint))
14288 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 61 53  ;.    pPager->aS
14289 61 76 65 70 6f 69 6e 74 20 3d 20 61 4e 65 77 3b  avepoint = aNew;
1428a 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 53 61  .    pPager->nSa
1428b 76 65 70 6f 69 6e 74 20 3d 20 6e 53 61 76 65 70  vepoint = nSavep
1428c 6f 69 6e 74 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f  oint;..    /* Po
1428d 70 75 6c 61 74 65 20 74 68 65 20 50 61 67 65 72  pulate the Pager
1428e 53 61 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74  Savepoint struct
1428f 75 72 65 73 20 6a 75 73 74 20 61 6c 6c 6f 63 61  ures just alloca
14290 74 65 64 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28  ted. */.    for(
14291 69 69 3d 6e 43 75 72 72 65 6e 74 3b 20 69 69 3c  ii=nCurrent; ii<
14292 6e 53 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b  nSavepoint; ii++
14293 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
14294 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56   pPager->dbSizeV
14295 61 6c 69 64 20 29 3b 0a 20 20 20 20 20 20 61 4e  alid );.      aN
14296 65 77 5b 69 69 5d 2e 6e 4f 72 69 67 20 3d 20 70  ew[ii].nOrig = p
14297 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 20  Pager->dbSize;. 
14298 20 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28       if( isOpen(
14299 70 50 61 67 65 72 2d 3e 6a 66 64 29 20 26 26 20  pPager->jfd) && 
1429a 41 4c 57 41 59 53 28 70 50 61 67 65 72 2d 3e 6a  ALWAYS(pPager->j
1429b 6f 75 72 6e 61 6c 4f 66 66 3e 30 29 20 29 7b 0a  ournalOff>0) ){.
1429c 20 20 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d          aNew[ii]
1429d 2e 69 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65  .iOffset = pPage
1429e 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20  r->journalOff;. 
1429f 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
142a0 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e 69 4f 66      aNew[ii].iOf
142a1 66 73 65 74 20 3d 20 4a 4f 55 52 4e 41 4c 5f 48  fset = JOURNAL_H
142a2 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20  DR_SZ(pPager);. 
142a3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 4e 65       }.      aNe
142a4 77 5b 69 69 5d 2e 69 53 75 62 52 65 63 20 3d 20  w[ii].iSubRec = 
142a5 70 50 61 67 65 72 2d 3e 6e 53 75 62 52 65 63 3b  pPager->nSubRec;
142a6 0a 20 20 20 20 20 20 61 4e 65 77 5b 69 69 5d 2e  .      aNew[ii].
142a7 70 49 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 73  pInSavepoint = s
142a8 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61  qlite3BitvecCrea
142a9 74 65 28 70 50 61 67 65 72 2d 3e 64 62 53 69 7a  te(pPager->dbSiz
142aa 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 61  e);.      if( !a
142ab 4e 65 77 5b 69 69 5d 2e 70 49 6e 53 61 76 65 70  New[ii].pInSavep
142ac 6f 69 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20  oint ){.        
142ad 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
142ae 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  MEM;.      }.   
142af 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20   }..    /* Open 
142b0 74 68 65 20 73 75 62 2d 6a 6f 75 72 6e 61 6c 2c  the sub-journal,
142b1 20 69 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c   if it is not al
142b2 72 65 61 64 79 20 6f 70 65 6e 65 64 2e 20 2a 2f  ready opened. */
142b3 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e 53 75  .    rc = openSu
142b4 62 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 29  bJournal(pPager)
142b5 3b 0a 20 20 20 20 61 73 73 65 72 74 54 72 75 6e  ;.    assertTrun
142b6 63 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 28 70  cateConstraint(p
142b7 50 61 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 72  Pager);.  }..  r
142b8 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
142b9 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
142ba 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72 6f   is called to ro
142bb 6c 6c 62 61 63 6b 20 6f 72 20 72 65 6c 65 61 73  llback or releas
142bc 65 20 28 63 6f 6d 6d 69 74 29 20 61 20 73 61 76  e (commit) a sav
142bd 65 70 6f 69 6e 74 2e 0a 2a 2a 20 54 68 65 20 73  epoint..** The s
142be 61 76 65 70 6f 69 6e 74 20 74 6f 20 72 65 6c 65  avepoint to rele
142bf 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20  ase or rollback 
142c0 6e 65 65 64 20 6e 6f 74 20 62 65 20 74 68 65 20  need not be the 
142c1 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 0a 2a  most recently .*
142c2 2a 20 63 72 65 61 74 65 64 20 73 61 76 65 70 6f  * created savepo
142c3 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d  int..**.** Param
142c4 65 74 65 72 20 6f 70 20 69 73 20 61 6c 77 61 79  eter op is alway
142c5 73 20 65 69 74 68 65 72 20 53 41 56 45 50 4f 49  s either SAVEPOI
142c6 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 6f 72 20 53  NT_ROLLBACK or S
142c7 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45  AVEPOINT_RELEASE
142c8 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20 53 41  ..** If it is SA
142c9 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c  VEPOINT_RELEASE,
142ca 20 74 68 65 6e 20 72 65 6c 65 61 73 65 20 61 6e   then release an
142cb 64 20 64 65 73 74 72 6f 79 20 74 68 65 20 73 61  d destroy the sa
142cc 76 65 70 6f 69 6e 74 20 77 69 74 68 0a 2a 2a 20  vepoint with.** 
142cd 69 6e 64 65 78 20 69 53 61 76 65 70 6f 69 6e 74  index iSavepoint
142ce 2e 20 49 66 20 69 74 20 69 73 20 53 41 56 45 50  . If it is SAVEP
142cf 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 74  OINT_ROLLBACK, t
142d0 68 65 6e 20 72 6f 6c 6c 62 61 63 6b 20 61 6c 6c  hen rollback all
142d1 20 63 68 61 6e 67 65 73 0a 2a 2a 20 74 68 61 74   changes.** that
142d2 20 68 61 76 65 20 6f 63 63 75 72 72 65 64 20 73   have occurred s
142d3 69 6e 63 65 20 74 68 65 20 73 70 65 63 69 66 69  ince the specifi
142d4 65 64 20 73 61 76 65 70 6f 69 6e 74 20 77 61 73  ed savepoint was
142d5 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20   created..**.** 
142d6 54 68 65 20 73 61 76 65 70 6f 69 6e 74 20 74 6f  The savepoint to
142d7 20 72 6f 6c 6c 62 61 63 6b 20 6f 72 20 72 65 6c   rollback or rel
142d8 65 61 73 65 20 69 73 20 69 64 65 6e 74 69 66 69  ease is identifi
142d9 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72 20  ed by parameter 
142da 0a 2a 2a 20 69 53 61 76 65 70 6f 69 6e 74 2e 20  .** iSavepoint. 
142db 41 20 76 61 6c 75 65 20 6f 66 20 30 20 6d 65 61  A value of 0 mea
142dc 6e 73 20 74 6f 20 6f 70 65 72 61 74 65 20 6f 6e  ns to operate on
142dd 20 74 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 73   the outermost s
142de 61 76 65 70 6f 69 6e 74 0a 2a 2a 20 28 74 68 65  avepoint.** (the
142df 20 66 69 72 73 74 20 63 72 65 61 74 65 64 29 2e   first created).
142e0 20 41 20 76 61 6c 75 65 20 6f 66 20 28 50 61 67   A value of (Pag
142e1 65 72 2e 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29  er.nSavepoint-1)
142e2 20 6d 65 61 6e 73 20 6f 70 65 72 61 74 65 0a 2a   means operate.*
142e3 2a 20 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65  * on the most re
142e4 63 65 6e 74 6c 79 20 63 72 65 61 74 65 64 20 73  cently created s
142e5 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 69 53 61  avepoint. If iSa
142e6 76 65 70 6f 69 6e 74 20 69 73 20 67 72 65 61 74  vepoint is great
142e7 65 72 20 74 68 61 6e 0a 2a 2a 20 28 50 61 67 65  er than.** (Page
142e8 72 2e 6e 53 61 76 65 70 6f 69 6e 74 2d 31 29 2c  r.nSavepoint-1),
142e9 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   then this funct
142ea 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ion is a no-op..
142eb 2a 2a 0a 2a 2a 20 49 66 20 61 20 6e 65 67 61 74  **.** If a negat
142ec 69 76 65 20 76 61 6c 75 65 20 69 73 20 70 61 73  ive value is pas
142ed 73 65 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63  sed to this func
142ee 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 63  tion, then the c
142ef 75 72 72 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61  urrent.** transa
142f0 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65 64 20  ction is rolled 
142f1 62 61 63 6b 2e 20 54 68 69 73 20 69 73 20 64 69  back. This is di
142f2 66 66 65 72 65 6e 74 20 74 6f 20 63 61 6c 6c 69  fferent to calli
142f3 6e 67 20 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61  ng .** sqlite3Pa
142f4 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 20 62 65  gerRollback() be
142f5 63 61 75 73 65 20 74 68 69 73 20 66 75 6e 63 74  cause this funct
142f6 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 74 65 72  ion does not ter
142f7 6d 69 6e 61 74 65 0a 2a 2a 20 74 68 65 20 74 72  minate.** the tr
142f8 61 6e 73 61 63 74 69 6f 6e 20 6f 72 20 75 6e 6c  ansaction or unl
142f9 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73 65  ock the database
142fa 2c 20 69 74 20 6a 75 73 74 20 72 65 73 74 6f 72  , it just restor
142fb 65 73 20 74 68 65 20 0a 2a 2a 20 63 6f 6e 74 65  es the .** conte
142fc 6e 74 73 20 6f 66 20 74 68 65 20 64 61 74 61 62  nts of the datab
142fd 61 73 65 20 74 6f 20 69 74 73 20 6f 72 69 67 69  ase to its origi
142fe 6e 61 6c 20 73 74 61 74 65 2e 20 0a 2a 2a 0a 2a  nal state. .**.*
142ff 2a 20 49 6e 20 61 6e 79 20 63 61 73 65 2c 20 61  * In any case, a
14300 6c 6c 20 73 61 76 65 70 6f 69 6e 74 73 20 77 69  ll savepoints wi
14301 74 68 20 61 6e 20 69 6e 64 65 78 20 67 72 65 61  th an index grea
14302 74 65 72 20 74 68 61 6e 20 69 53 61 76 65 70 6f  ter than iSavepo
14303 69 6e 74 20 0a 2a 2a 20 61 72 65 20 64 65 73 74  int .** are dest
14304 72 6f 79 65 64 2e 20 49 66 20 74 68 69 73 20 69  royed. If this i
14305 73 20 61 20 72 65 6c 65 61 73 65 20 6f 70 65 72  s a release oper
14306 61 74 69 6f 6e 20 28 6f 70 3d 3d 53 41 56 45 50  ation (op==SAVEP
14307 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 2c 0a 2a  OINT_RELEASE),.*
14308 2a 20 74 68 65 6e 20 73 61 76 65 70 6f 69 6e 74  * then savepoint
14309 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73 20 61   iSavepoint is a
1430a 6c 73 6f 20 64 65 73 74 72 6f 79 65 64 2e 0a 2a  lso destroyed..*
1430b 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
1430c 6f 6e 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51  on may return SQ
1430d 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61 20  LITE_NOMEM if a 
1430e 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
1430f 6e 20 66 61 69 6c 73 2c 0a 2a 2a 20 6f 72 20 61  n fails,.** or a
14310 6e 20 49 4f 20 65 72 72 6f 72 20 63 6f 64 65 20  n IO error code 
14311 69 66 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 6f  if an IO error o
14312 63 63 75 72 73 20 77 68 69 6c 65 20 72 6f 6c 6c  ccurs while roll
14313 69 6e 67 20 62 61 63 6b 20 61 20 0a 2a 2a 20 73  ing back a .** s
14314 61 76 65 70 6f 69 6e 74 2e 20 49 66 20 6e 6f 20  avepoint. If no 
14315 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 53 51  errors occur, SQ
14316 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
14317 6e 65 64 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f  ned..*/ .SQLITE_
14318 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
14319 74 65 33 50 61 67 65 72 53 61 76 65 70 6f 69 6e  te3PagerSavepoin
1431a 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  t(Pager *pPager,
1431b 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 69 53 61   int op, int iSa
1431c 76 65 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20  vepoint){.  int 
1431d 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1431e 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d 53  .  assert( op==S
1431f 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45  AVEPOINT_RELEASE
14320 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e   || op==SAVEPOIN
14321 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20 20  T_ROLLBACK );.  
14322 61 73 73 65 72 74 28 20 69 53 61 76 65 70 6f 69  assert( iSavepoi
14323 6e 74 3e 3d 30 20 7c 7c 20 6f 70 3d 3d 53 41 56  nt>=0 || op==SAV
14324 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20  EPOINT_ROLLBACK 
14325 29 3b 0a 0a 20 20 69 66 28 20 69 53 61 76 65 70  );..  if( iSavep
14326 6f 69 6e 74 3c 70 50 61 67 65 72 2d 3e 6e 53 61  oint<pPager->nSa
14327 76 65 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 69  vepoint ){.    i
14328 6e 74 20 69 69 3b 20 20 20 20 20 20 20 20 20 20  nt ii;          
14329 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61    /* Iterator va
1432a 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e  riable */.    in
1432b 74 20 6e 4e 65 77 3b 20 20 20 20 20 20 20 20 20  t nNew;         
1432c 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65   /* Number of re
1432d 6d 61 69 6e 69 6e 67 20 73 61 76 65 70 6f 69 6e  maining savepoin
1432e 74 73 20 61 66 74 65 72 20 74 68 69 73 20 6f 70  ts after this op
1432f 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 46 69 67  . */..    /* Fig
14330 75 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79  ure out how many
14331 20 73 61 76 65 70 6f 69 6e 74 73 20 77 69 6c 6c   savepoints will
14332 20 73 74 69 6c 6c 20 62 65 20 61 63 74 69 76 65   still be active
14333 20 61 66 74 65 72 20 74 68 69 73 0a 20 20 20 20   after this.    
14334 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e 20 53 74  ** operation. St
14335 6f 72 65 20 74 68 69 73 20 76 61 6c 75 65 20 69  ore this value i
14336 6e 20 6e 4e 65 77 2e 20 54 68 65 6e 20 66 72 65  n nNew. Then fre
14337 65 20 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f  e resources asso
14338 63 69 61 74 65 64 20 0a 20 20 20 20 2a 2a 20 77  ciated .    ** w
14339 69 74 68 20 61 6e 79 20 73 61 76 65 70 6f 69 6e  ith any savepoin
1433a 74 73 20 74 68 61 74 20 61 72 65 20 64 65 73 74  ts that are dest
1433b 72 6f 79 65 64 20 62 79 20 74 68 69 73 20 6f 70  royed by this op
1433c 65 72 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a  eration..    */.
1433d 20 20 20 20 6e 4e 65 77 20 3d 20 69 53 61 76 65      nNew = iSave
1433e 70 6f 69 6e 74 20 2b 20 28 6f 70 3d 3d 53 41 56  point + (op==SAV
1433f 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 29  EPOINT_ROLLBACK)
14340 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 6e 4e 65  ;.    for(ii=nNe
14341 77 3b 20 69 69 3c 70 50 61 67 65 72 2d 3e 6e 53  w; ii<pPager->nS
14342 61 76 65 70 6f 69 6e 74 3b 20 69 69 2b 2b 29 7b  avepoint; ii++){
14343 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69  .      sqlite3Bi
14344 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67  tvecDestroy(pPag
14345 65 72 2d 3e 61 53 61 76 65 70 6f 69 6e 74 5b 69  er->aSavepoint[i
14346 69 5d 2e 70 49 6e 53 61 76 65 70 6f 69 6e 74 29  i].pInSavepoint)
14347 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67  ;.    }.    pPag
14348 65 72 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d  er->nSavepoint =
14349 20 6e 4e 65 77 3b 0a 0a 20 20 20 20 2f 2a 20 49   nNew;..    /* I
1434a 66 20 74 68 69 73 20 69 73 20 61 20 72 6f 6c 6c  f this is a roll
1434b 62 61 63 6b 20 6f 70 65 72 61 74 69 6f 6e 2c 20  back operation, 
1434c 70 6c 61 79 62 61 63 6b 20 74 68 65 20 73 70 65  playback the spe
1434d 63 69 66 69 65 64 20 73 61 76 65 70 6f 69 6e 74  cified savepoint
1434e 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73  ..    ** If this
1434f 20 69 73 20 61 20 74 65 6d 70 2d 66 69 6c 65 2c   is a temp-file,
14350 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20   it is possible 
14351 74 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c  that the journal
14352 20 66 69 6c 65 20 68 61 73 0a 20 20 20 20 2a 2a   file has.    **
14353 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 6f 70   not yet been op
14354 65 6e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61  ened. In this ca
14355 73 65 20 74 68 65 72 65 20 68 61 76 65 20 62 65  se there have be
14356 65 6e 20 6e 6f 20 63 68 61 6e 67 65 73 20 74 6f  en no changes to
14357 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74 61  .    ** the data
14358 62 61 73 65 20 66 69 6c 65 2c 20 73 6f 20 74 68  base file, so th
14359 65 20 70 6c 61 79 62 61 63 6b 20 6f 70 65 72 61  e playback opera
1435a 74 69 6f 6e 20 63 61 6e 20 62 65 20 73 6b 69 70  tion can be skip
1435b 70 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ped..    */.    
1435c 69 66 28 20 6f 70 3d 3d 53 41 56 45 50 4f 49 4e  if( op==SAVEPOIN
1435d 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20 69 73  T_ROLLBACK && is
1435e 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64  Open(pPager->jfd
1435f 29 20 29 7b 0a 20 20 20 20 20 20 50 61 67 65 72  ) ){.      Pager
14360 53 61 76 65 70 6f 69 6e 74 20 2a 70 53 61 76 65  Savepoint *pSave
14361 70 6f 69 6e 74 20 3d 20 28 6e 4e 65 77 3d 3d 30  point = (nNew==0
14362 29 3f 30 3a 26 70 50 61 67 65 72 2d 3e 61 53 61  )?0:&pPager->aSa
14363 76 65 70 6f 69 6e 74 5b 6e 4e 65 77 2d 31 5d 3b  vepoint[nNew-1];
14364 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
14365 72 50 6c 61 79 62 61 63 6b 53 61 76 65 70 6f 69  rPlaybackSavepoi
14366 6e 74 28 70 50 61 67 65 72 2c 20 70 53 61 76 65  nt(pPager, pSave
14367 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20 61 73  point);.      as
14368 73 65 72 74 28 72 63 21 3d 53 51 4c 49 54 45 5f  sert(rc!=SQLITE_
14369 44 4f 4e 45 29 3b 0a 20 20 20 20 7d 0a 20 20 0a  DONE);.    }.  .
1436a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
1436b 73 20 61 20 72 65 6c 65 61 73 65 20 6f 66 20 74  s a release of t
1436c 68 65 20 6f 75 74 65 72 6d 6f 73 74 20 73 61 76  he outermost sav
1436d 65 70 6f 69 6e 74 2c 20 74 72 75 6e 63 61 74 65  epoint, truncate
1436e 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 75 62   .    ** the sub
1436f 2d 6a 6f 75 72 6e 61 6c 20 74 6f 20 7a 65 72 6f  -journal to zero
14370 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20   bytes in size. 
14371 2a 2f 0a 20 20 20 20 69 66 28 20 6e 4e 65 77 3d  */.    if( nNew=
14372 3d 30 20 26 26 20 6f 70 3d 3d 53 41 56 45 50 4f  =0 && op==SAVEPO
14373 49 4e 54 5f 52 45 4c 45 41 53 45 20 26 26 20 69  INT_RELEASE && i
14374 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 6a  sOpen(pPager->sj
14375 66 64 29 20 29 7b 0a 20 20 20 20 20 20 61 73 73  fd) ){.      ass
14376 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
14377 4f 4b 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  OK );.      rc =
14378 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61   sqlite3OsTrunca
14379 74 65 28 70 50 61 67 65 72 2d 3e 73 6a 66 64 2c  te(pPager->sjfd,
1437a 20 30 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65   0);.      pPage
1437b 72 2d 3e 6e 53 75 62 52 65 63 20 3d 20 30 3b 0a  r->nSubRec = 0;.
1437c 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1437d 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1437e 52 65 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20  Return the full 
1437f 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20  pathname of the 
14380 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a  database file..*
14381 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
14382 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c   const char *sql
14383 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e 61 6d  ite3PagerFilenam
14384 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  e(Pager *pPager)
14385 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65  {.  return pPage
14386 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 3b 0a 7d 0a  r->zFilename;.}.
14387 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
14388 65 20 56 46 53 20 73 74 72 75 63 74 75 72 65 20  e VFS structure 
14389 66 6f 72 20 74 68 65 20 70 61 67 65 72 2e 0a 2a  for the pager..*
1438a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1438b 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76   const sqlite3_v
1438c 66 73 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  fs *sqlite3Pager
1438d 56 66 73 28 50 61 67 65 72 20 2a 70 50 61 67 65  Vfs(Pager *pPage
1438e 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61  r){.  return pPa
1438f 67 65 72 2d 3e 70 56 66 73 3b 0a 7d 0a 0a 2f 2a  ger->pVfs;.}../*
14390 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66  .** Return the f
14391 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f 72 20 74  ile handle for t
14392 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
14393 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77   associated.** w
14394 69 74 68 20 74 68 65 20 70 61 67 65 72 2e 20 20  ith the pager.  
14395 54 68 69 73 20 6d 69 67 68 74 20 72 65 74 75 72  This might retur
14396 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 66 69  n NULL if the fi
14397 6c 65 20 68 61 73 0a 2a 2a 20 6e 6f 74 20 79 65  le has.** not ye
14398 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 2e 0a 2a  t been opened..*
14399 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1439a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73   sqlite3_file *s
1439b 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28  qlite3PagerFile(
1439c 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
1439d 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d    return pPager-
1439e 3e 66 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  >fd;.}../*.** Re
1439f 74 75 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61  turn the full pa
143a0 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f  thname of the jo
143a1 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53  urnal file..*/.S
143a2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
143a3 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
143a4 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d  3PagerJournalnam
143a5 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  e(Pager *pPager)
143a6 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65  {.  return pPage
143a7 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 3b 0a 7d 0a 0a  r->zJournal;.}..
143a8 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  /*.** Return tru
143a9 65 20 69 66 20 66 73 79 6e 63 28 29 20 63 61 6c  e if fsync() cal
143aa 6c 73 20 61 72 65 20 64 69 73 61 62 6c 65 64 20  ls are disabled 
143ab 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e 20  for this pager. 
143ac 20 52 65 74 75 72 6e 20 46 41 4c 53 45 0a 2a 2a   Return FALSE.**
143ad 20 69 66 20 66 73 79 6e 63 28 29 73 20 61 72 65   if fsync()s are
143ae 20 65 78 65 63 75 74 65 64 20 6e 6f 72 6d 61 6c   executed normal
143af 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ly..*/.SQLITE_PR
143b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
143b1 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 50 61 67  3PagerNosync(Pag
143b2 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 72  er *pPager){.  r
143b3 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 6e 6f  eturn pPager->no
143b4 53 79 6e 63 3b 0a 7d 0a 0a 23 69 66 64 65 66 20  Sync;.}..#ifdef 
143b5 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43  SQLITE_HAS_CODEC
143b6 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f 72 20 72 65  ./*.** Set or re
143b7 74 72 69 65 76 65 20 74 68 65 20 63 6f 64 65 63  trieve the codec
143b8 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 0a   for this pager.
143b9 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
143ba 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43 6f  qlite3PagerSetCo
143bb 64 65 63 28 0a 20 20 50 61 67 65 72 20 2a 70 50  dec(.  Pager *pP
143bc 61 67 65 72 2c 0a 20 20 76 6f 69 64 20 2a 28 2a  ager,.  void *(*
143bd 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c 76 6f  xCodec)(void*,vo
143be 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c 0a 20  id*,Pgno,int),. 
143bf 20 76 6f 69 64 20 28 2a 78 43 6f 64 65 63 53 69   void (*xCodecSi
143c0 7a 65 43 68 6e 67 29 28 76 6f 69 64 2a 2c 69 6e  zeChng)(void*,in
143c1 74 2c 69 6e 74 29 2c 0a 20 20 76 6f 69 64 20 28  t,int),.  void (
143c2 2a 78 43 6f 64 65 63 46 72 65 65 29 28 76 6f 69  *xCodecFree)(voi
143c3 64 2a 29 2c 0a 20 20 76 6f 69 64 20 2a 70 43 6f  d*),.  void *pCo
143c4 64 65 63 0a 29 7b 0a 20 20 69 66 28 20 70 50 61  dec.){.  if( pPa
143c5 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65 20  ger->xCodecFree 
143c6 29 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65 63  ) pPager->xCodec
143c7 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70 43 6f  Free(pPager->pCo
143c8 64 65 63 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e  dec);.  pPager->
143c9 78 43 6f 64 65 63 20 3d 20 78 43 6f 64 65 63 3b  xCodec = xCodec;
143ca 0a 20 20 70 50 61 67 65 72 2d 3e 78 43 6f 64 65  .  pPager->xCode
143cb 63 53 69 7a 65 43 68 6e 67 20 3d 20 78 43 6f 64  cSizeChng = xCod
143cc 65 63 53 69 7a 65 43 68 6e 67 3b 0a 20 20 70 50  ecSizeChng;.  pP
143cd 61 67 65 72 2d 3e 78 43 6f 64 65 63 46 72 65 65  ager->xCodecFree
143ce 20 3d 20 78 43 6f 64 65 63 46 72 65 65 3b 0a 20   = xCodecFree;. 
143cf 20 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63 20   pPager->pCodec 
143d0 3d 20 70 43 6f 64 65 63 3b 0a 20 20 70 61 67 65  = pCodec;.  page
143d1 72 52 65 70 6f 72 74 53 69 7a 65 28 70 50 61 67  rReportSize(pPag
143d2 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  er);.}.static vo
143d3 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  id *sqlite3Pager
143d4 47 65 74 43 6f 64 65 63 28 50 61 67 65 72 20 2a  GetCodec(Pager *
143d5 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72  pPager){.  retur
143d6 6e 20 70 50 61 67 65 72 2d 3e 70 43 6f 64 65 63  n pPager->pCodec
143d7 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  ;.}.#endif..#ifn
143d8 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
143d9 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a  AUTOVACUUM./*.**
143da 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65 20 70   Move the page p
143db 50 67 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 70  Pg to location p
143dc 67 6e 6f 20 69 6e 20 74 68 65 20 66 69 6c 65 2e  gno in the file.
143dd 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73  .**.** There mus
143de 74 20 62 65 20 6e 6f 20 72 65 66 65 72 65 6e 63  t be no referenc
143df 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 70  es to the page p
143e0 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65  reviously locate
143e1 64 20 61 74 0a 2a 2a 20 70 67 6e 6f 20 28 77 68  d at.** pgno (wh
143e2 69 63 68 20 77 65 20 63 61 6c 6c 20 70 50 67 4f  ich we call pPgO
143e3 6c 64 29 20 74 68 6f 75 67 68 20 74 68 61 74 20  ld) though that 
143e4 70 61 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 20  page is allowed 
143e5 74 6f 20 62 65 0a 2a 2a 20 69 6e 20 63 61 63 68  to be.** in cach
143e6 65 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20  e.  If the page 
143e7 70 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74  previously locat
143e8 65 64 20 61 74 20 70 67 6e 6f 20 69 73 20 6e 6f  ed at pgno is no
143e9 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20  t already.** in 
143ea 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  the rollback jou
143eb 72 6e 61 6c 2c 20 69 74 20 69 73 20 6e 6f 74 20  rnal, it is not 
143ec 70 75 74 20 74 68 65 72 65 20 62 79 20 62 79 20  put there by by 
143ed 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a  this routine..**
143ee 0a 2a 2a 20 52 65 66 65 72 65 6e 63 65 73 20 74  .** References t
143ef 6f 20 74 68 65 20 70 61 67 65 20 70 50 67 20 72  o the page pPg r
143f0 65 6d 61 69 6e 20 76 61 6c 69 64 2e 20 55 70 64  emain valid. Upd
143f1 61 74 69 6e 67 20 61 6e 79 0a 2a 2a 20 6d 65 74  ating any.** met
143f2 61 2d 64 61 74 61 20 61 73 73 6f 63 69 61 74 65  a-data associate
143f3 64 20 77 69 74 68 20 70 50 67 20 28 69 2e 65 2e  d with pPg (i.e.
143f4 20 64 61 74 61 20 73 74 6f 72 65 64 20 69 6e 20   data stored in 
143f5 74 68 65 20 6e 45 78 74 72 61 20 62 79 74 65 73  the nExtra bytes
143f6 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c  .** allocated al
143f7 6f 6e 67 20 77 69 74 68 20 74 68 65 20 70 61 67  ong with the pag
143f8 65 29 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e  e) is the respon
143f9 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20  sibility of the 
143fa 63 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20  caller..**.** A 
143fb 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74  transaction must
143fc 20 62 65 20 61 63 74 69 76 65 20 77 68 65 6e 20   be active when 
143fd 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
143fe 63 61 6c 6c 65 64 2e 20 49 74 20 75 73 65 64 20  called. It used 
143ff 74 6f 20 62 65 0a 2a 2a 20 72 65 71 75 69 72 65  to be.** require
14400 64 20 74 68 61 74 20 61 20 73 74 61 74 65 6d 65  d that a stateme
14401 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  nt transaction w
14402 61 73 20 6e 6f 74 20 61 63 74 69 76 65 2c 20 62  as not active, b
14403 75 74 20 74 68 69 73 20 72 65 73 74 72 69 63 74  ut this restrict
14404 69 6f 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20  ion.** has been 
14405 72 65 6d 6f 76 65 64 20 28 43 52 45 41 54 45 20  removed (CREATE 
14406 49 4e 44 45 58 20 6e 65 65 64 73 20 74 6f 20 6d  INDEX needs to m
14407 6f 76 65 20 61 20 70 61 67 65 20 77 68 65 6e 20  ove a page when 
14408 61 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74  a statement.** t
14409 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63  ransaction is ac
1440a 74 69 76 65 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  tive)..**.** If 
1440b 74 68 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d  the fourth argum
1440c 65 6e 74 2c 20 69 73 43 6f 6d 6d 69 74 2c 20 69  ent, isCommit, i
1440d 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
1440e 20 74 68 69 73 20 70 61 67 65 20 69 73 20 62 65   this page is be
1440f 69 6e 67 0a 2a 2a 20 6d 6f 76 65 64 20 61 73 20  ing.** moved as 
14410 70 61 72 74 20 6f 66 20 61 20 64 61 74 61 62 61  part of a databa
14411 73 65 20 72 65 6f 72 67 61 6e 69 7a 61 74 69 6f  se reorganizatio
14412 6e 20 6a 75 73 74 20 62 65 66 6f 72 65 20 74 68  n just before th
14413 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a  e transaction .*
14414 2a 20 69 73 20 62 65 69 6e 67 20 63 6f 6d 6d 69  * is being commi
14415 74 74 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61  tted. In this ca
14416 73 65 2c 20 69 74 20 69 73 20 67 75 61 72 61 6e  se, it is guaran
14417 74 65 65 64 20 74 68 61 74 20 74 68 65 20 64 61  teed that the da
14418 74 61 62 61 73 65 20 70 61 67 65 20 0a 2a 2a 20  tabase page .** 
14419 70 50 67 20 72 65 66 65 72 73 20 74 6f 20 77 69  pPg refers to wi
1441a 6c 6c 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65  ll not be writte
1441b 6e 20 74 6f 20 61 67 61 69 6e 20 77 69 74 68 69  n to again withi
1441c 6e 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69  n this transacti
1441d 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  on..**.** This f
1441e 75 6e 63 74 69 6f 6e 20 6d 61 79 20 72 65 74 75  unction may retu
1441f 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  rn SQLITE_NOMEM 
14420 6f 72 20 61 6e 20 49 4f 20 65 72 72 6f 72 20 63  or an IO error c
14421 6f 64 65 20 69 66 20 61 6e 20 65 72 72 6f 72 0a  ode if an error.
14422 2a 2a 20 6f 63 63 75 72 73 2e 20 4f 74 68 65 72  ** occurs. Other
14423 77 69 73 65 2c 20 69 74 20 72 65 74 75 72 6e 73  wise, it returns
14424 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53   SQLITE_OK..*/.S
14425 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
14426 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f  t sqlite3PagerMo
14427 76 65 70 61 67 65 28 50 61 67 65 72 20 2a 70 50  vepage(Pager *pP
14428 61 67 65 72 2c 20 44 62 50 61 67 65 20 2a 70 50  ager, DbPage *pP
14429 67 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 69 6e  g, Pgno pgno, in
1442a 74 20 69 73 43 6f 6d 6d 69 74 29 7b 0a 20 20 50  t isCommit){.  P
1442b 67 48 64 72 20 2a 70 50 67 4f 6c 64 3b 20 20 20  gHdr *pPgOld;   
1442c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1442d 68 65 20 70 61 67 65 20 62 65 69 6e 67 20 6f 76  he page being ov
1442e 65 72 77 72 69 74 74 65 6e 2e 20 2a 2f 0a 20 20  erwritten. */.  
1442f 50 67 6e 6f 20 6e 65 65 64 53 79 6e 63 50 67 6e  Pgno needSyncPgn
14430 6f 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20  o = 0;       /* 
14431 4f 6c 64 20 76 61 6c 75 65 20 6f 66 20 70 50 67  Old value of pPg
14432 2d 3e 70 67 6e 6f 2c 20 69 66 20 73 79 6e 63 20  ->pgno, if sync 
14433 69 73 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20  is required */. 
14434 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
14435 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
14436 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
14437 20 20 50 67 6e 6f 20 6f 72 69 67 50 67 6e 6f 3b    Pgno origPgno;
14438 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
14439 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 70  * The original p
1443a 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 0a 20  age number */.. 
1443b 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 52   assert( pPg->nR
1443c 65 66 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 49 66  ef>0 );..  /* If
1443d 20 74 68 65 20 70 61 67 65 20 62 65 69 6e 67 20   the page being 
1443e 6d 6f 76 65 64 20 69 73 20 64 69 72 74 79 20 61  moved is dirty a
1443f 6e 64 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20  nd has not been 
14440 73 61 76 65 64 20 62 79 20 74 68 65 20 6c 61 74  saved by the lat
14441 65 73 74 0a 20 20 2a 2a 20 73 61 76 65 70 6f 69  est.  ** savepoi
14442 6e 74 2c 20 74 68 65 6e 20 73 61 76 65 20 74 68  nt, then save th
14443 65 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e  e current conten
14444 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 69  ts of the page i
14445 6e 74 6f 20 74 68 65 20 0a 20 20 2a 2a 20 73 75  nto the .  ** su
14446 62 2d 6a 6f 75 72 6e 61 6c 20 6e 6f 77 2e 20 54  b-journal now. T
14447 68 69 73 20 69 73 20 72 65 71 75 69 72 65 64 20  his is required 
14448 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 20 66 6f  to handle the fo
14449 6c 6c 6f 77 69 6e 67 20 73 63 65 6e 61 72 69 6f  llowing scenario
1444a 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 42 45  :.  **.  **   BE
1444b 47 49 4e 3b 0a 20 20 2a 2a 20 20 20 20 20 3c 6a  GIN;.  **     <j
1444c 6f 75 72 6e 61 6c 20 70 61 67 65 20 58 2c 20 74  ournal page X, t
1444d 68 65 6e 20 6d 6f 64 69 66 79 20 69 74 20 69 6e  hen modify it in
1444e 20 6d 65 6d 6f 72 79 3e 0a 20 20 2a 2a 20 20 20   memory>.  **   
1444f 20 20 53 41 56 45 50 4f 49 4e 54 20 6f 6e 65 3b    SAVEPOINT one;
14450 0a 20 20 2a 2a 20 20 20 20 20 20 20 3c 4d 6f 76  .  **       <Mov
14451 65 20 70 61 67 65 20 58 20 74 6f 20 6c 6f 63 61  e page X to loca
14452 74 69 6f 6e 20 59 3e 0a 20 20 2a 2a 20 20 20 20  tion Y>.  **    
14453 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65   ROLLBACK TO one
14454 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70  ;.  **.  ** If p
14455 61 67 65 20 58 20 77 65 72 65 20 6e 6f 74 20 77  age X were not w
14456 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73 75  ritten to the su
14457 62 2d 6a 6f 75 72 6e 61 6c 20 68 65 72 65 2c 20  b-journal here, 
14458 69 74 20 77 6f 75 6c 64 20 6e 6f 74 0a 20 20 2a  it would not.  *
14459 2a 20 62 65 20 70 6f 73 73 69 62 6c 65 20 74 6f  * be possible to
1445a 20 72 65 73 74 6f 72 65 20 69 74 73 20 63 6f 6e   restore its con
1445b 74 65 6e 74 73 20 77 68 65 6e 20 74 68 65 20 22  tents when the "
1445c 52 4f 4c 4c 42 41 43 4b 20 54 4f 20 6f 6e 65 22  ROLLBACK TO one"
1445d 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20  .  ** statement 
1445e 77 65 72 65 20 69 73 20 70 72 6f 63 65 73 73 65  were is processe
1445f 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 73 75 62  d..  **.  ** sub
14460 6a 6f 75 72 6e 61 6c 50 61 67 65 28 29 20 6d 61  journalPage() ma
14461 79 20 6e 65 65 64 20 74 6f 20 61 6c 6c 6f 63 61  y need to alloca
14462 74 65 20 73 70 61 63 65 20 74 6f 20 73 74 6f 72  te space to stor
14463 65 20 70 50 67 2d 3e 70 67 6e 6f 20 69 6e 74 6f  e pPg->pgno into
14464 0a 20 20 2a 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72  .  ** one or mor
14465 65 20 73 61 76 65 70 6f 69 6e 74 20 62 69 74 76  e savepoint bitv
14466 65 63 73 2e 20 54 68 69 73 20 69 73 20 74 68 65  ecs. This is the
14467 20 72 65 61 73 6f 6e 20 74 68 69 73 20 66 75 6e   reason this fun
14468 63 74 69 6f 6e 0a 20 20 2a 2a 20 6d 61 79 20 72  ction.  ** may r
14469 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
1446a 45 4d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  EM..  */.  if( p
1446b 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  Pg->flags&PGHDR_
1446c 44 49 52 54 59 20 0a 20 20 20 26 26 20 73 75 62  DIRTY .   && sub
1446d 6a 52 65 71 75 69 72 65 73 50 61 67 65 28 70 50  jRequiresPage(pP
1446e 67 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f  g).   && SQLITE_
1446f 4f 4b 21 3d 28 72 63 20 3d 20 73 75 62 6a 6f 75  OK!=(rc = subjou
14470 72 6e 61 6c 50 61 67 65 28 70 50 67 29 29 0a 20  rnalPage(pPg)). 
14471 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
14472 63 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52 54  c;.  }..  PAGERT
14473 52 41 43 45 28 28 22 4d 4f 56 45 20 25 64 20 70  RACE(("MOVE %d p
14474 61 67 65 20 25 64 20 28 6e 65 65 64 53 79 6e 63  age %d (needSync
14475 3d 25 64 29 20 6d 6f 76 65 73 20 74 6f 20 25 64  =%d) moves to %d
14476 5c 6e 22 2c 20 0a 20 20 20 20 20 20 50 41 47 45  \n", .      PAGE
14477 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67  RID(pPager), pPg
14478 2d 3e 70 67 6e 6f 2c 20 28 70 50 67 2d 3e 66 6c  ->pgno, (pPg->fl
14479 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53  ags&PGHDR_NEED_S
1447a 59 4e 43 29 3f 31 3a 30 2c 20 70 67 6e 6f 29 29  YNC)?1:0, pgno))
1447b 3b 0a 20 20 49 4f 54 52 41 43 45 28 28 22 4d 4f  ;.  IOTRACE(("MO
1447c 56 45 20 25 70 20 25 64 20 25 64 5c 6e 22 2c 20  VE %p %d %d\n", 
1447d 70 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e  pPager, pPg->pgn
1447e 6f 2c 20 70 67 6e 6f 29 29 0a 0a 20 20 2f 2a 20  o, pgno))..  /* 
1447f 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e  If the journal n
14480 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 28  eeds to be sync(
14481 29 65 64 20 62 65 66 6f 72 65 20 70 61 67 65 20  )ed before page 
14482 70 50 67 2d 3e 70 67 6e 6f 20 63 61 6e 0a 20 20  pPg->pgno can.  
14483 2a 2a 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f  ** be written to
14484 2c 20 73 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e  , store pPg->pgn
14485 6f 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61  o in local varia
14486 62 6c 65 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f  ble needSyncPgno
14487 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74  ..  **.  ** If t
14488 68 65 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67  he isCommit flag
14489 20 69 73 20 73 65 74 2c 20 74 68 65 72 65 20 69   is set, there i
1448a 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d  s no need to rem
1448b 65 6d 62 65 72 20 74 68 61 74 0a 20 20 2a 2a 20  ember that.  ** 
1448c 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64  the journal need
1448d 73 20 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64  s to be sync()ed
1448e 20 62 65 66 6f 72 65 20 64 61 74 61 62 61 73 65   before database
1448f 20 70 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20   page pPg->pgno 
14490 0a 20 20 2a 2a 20 63 61 6e 20 62 65 20 77 72 69  .  ** can be wri
14491 74 74 65 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c  tten to. The cal
14492 6c 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20  ler has already 
14493 70 72 6f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20  promised not to 
14494 77 72 69 74 65 20 74 6f 20 69 74 2e 0a 20 20 2a  write to it..  *
14495 2f 0a 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c  /.  if( (pPg->fl
14496 61 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53  ags&PGHDR_NEED_S
14497 59 4e 43 29 20 26 26 20 21 69 73 43 6f 6d 6d 69  YNC) && !isCommi
14498 74 20 29 7b 0a 20 20 20 20 6e 65 65 64 53 79 6e  t ){.    needSyn
14499 63 50 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e  cPgno = pPg->pgn
1449a 6f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  o;.    assert( p
1449b 61 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67  ageInJournal(pPg
1449c 29 20 7c 7c 20 70 50 67 2d 3e 70 67 6e 6f 3e 70  ) || pPg->pgno>p
1449d 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53 69 7a  Pager->dbOrigSiz
1449e 65 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  e );.    assert(
1449f 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44   pPg->flags&PGHD
144a0 52 5f 44 49 52 54 59 20 29 3b 0a 20 20 20 20 61  R_DIRTY );.    a
144a1 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e  ssert( pPager->n
144a2 65 65 64 53 79 6e 63 20 29 3b 0a 20 20 7d 0a 0a  eedSync );.  }..
144a3 20 20 2f 2a 20 49 66 20 74 68 65 20 63 61 63 68    /* If the cach
144a4 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 61 67  e contains a pag
144a5 65 20 77 69 74 68 20 70 61 67 65 2d 6e 75 6d 62  e with page-numb
144a6 65 72 20 70 67 6e 6f 2c 20 72 65 6d 6f 76 65 20  er pgno, remove 
144a7 69 74 0a 20 20 2a 2a 20 66 72 6f 6d 20 69 74 73  it.  ** from its
144a8 20 68 61 73 68 20 63 68 61 69 6e 2e 20 41 6c 73   hash chain. Als
144a9 6f 2c 20 69 66 20 74 68 65 20 50 67 48 64 72 2e  o, if the PgHdr.
144aa 6e 65 65 64 53 79 6e 63 20 77 61 73 20 73 65 74  needSync was set
144ab 20 66 6f 72 20 0a 20 20 2a 2a 20 70 61 67 65 20   for .  ** page 
144ac 70 67 6e 6f 20 62 65 66 6f 72 65 20 74 68 65 20  pgno before the 
144ad 27 6d 6f 76 65 27 20 6f 70 65 72 61 74 69 6f 6e  'move' operation
144ae 2c 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65  , it needs to be
144af 20 72 65 74 61 69 6e 65 64 20 0a 20 20 2a 2a 20   retained .  ** 
144b0 66 6f 72 20 74 68 65 20 70 61 67 65 20 6d 6f 76  for the page mov
144b1 65 64 20 74 68 65 72 65 2e 0a 20 20 2a 2f 0a 20  ed there..  */. 
144b2 20 70 50 67 2d 3e 66 6c 61 67 73 20 26 3d 20 7e   pPg->flags &= ~
144b3 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b  PGHDR_NEED_SYNC;
144b4 0a 20 20 70 50 67 4f 6c 64 20 3d 20 70 61 67 65  .  pPgOld = page
144b5 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67 65 72 2c  r_lookup(pPager,
144b6 20 70 67 6e 6f 29 3b 0a 20 20 61 73 73 65 72 74   pgno);.  assert
144b7 28 20 21 70 50 67 4f 6c 64 20 7c 7c 20 70 50 67  ( !pPgOld || pPg
144b8 4f 6c 64 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a  Old->nRef==1 );.
144b9 20 20 69 66 28 20 70 50 67 4f 6c 64 20 29 7b 0a    if( pPgOld ){.
144ba 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c      pPg->flags |
144bb 3d 20 28 70 50 67 4f 6c 64 2d 3e 66 6c 61 67 73  = (pPgOld->flags
144bc 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43  &PGHDR_NEED_SYNC
144bd 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63  );.    sqlite3Pc
144be 61 63 68 65 44 72 6f 70 28 70 50 67 4f 6c 64 29  acheDrop(pPgOld)
144bf 3b 0a 20 20 7d 0a 0a 20 20 6f 72 69 67 50 67 6e  ;.  }..  origPgn
144c0 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f 3b 0a 20  o = pPg->pgno;. 
144c1 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f   sqlite3PcacheMo
144c2 76 65 28 70 50 67 2c 20 70 67 6e 6f 29 3b 0a 20  ve(pPg, pgno);. 
144c3 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61   sqlite3PcacheMa
144c4 6b 65 44 69 72 74 79 28 70 50 67 29 3b 0a 20 20  keDirty(pPg);.  
144c5 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69  pPager->dbModifi
144c6 65 64 20 3d 20 31 3b 0a 0a 20 20 69 66 28 20 6e  ed = 1;..  if( n
144c7 65 65 64 53 79 6e 63 50 67 6e 6f 20 29 7b 0a 20  eedSyncPgno ){. 
144c8 20 20 20 2f 2a 20 49 66 20 6e 65 65 64 53 79 6e     /* If needSyn
144c9 63 50 67 6e 6f 20 69 73 20 6e 6f 6e 2d 7a 65 72  cPgno is non-zer
144ca 6f 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72  o, then the jour
144cb 6e 61 6c 20 66 69 6c 65 20 6e 65 65 64 73 20 74  nal file needs t
144cc 6f 20 62 65 20 0a 20 20 20 20 2a 2a 20 73 79 6e  o be .    ** syn
144cd 63 28 29 65 64 20 62 65 66 6f 72 65 20 61 6e 79  c()ed before any
144ce 20 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e   data is written
144cf 20 74 6f 20 64 61 74 61 62 61 73 65 20 66 69 6c   to database fil
144d0 65 20 70 61 67 65 20 6e 65 65 64 53 79 6e 63 50  e page needSyncP
144d1 67 6e 6f 2e 0a 20 20 20 20 2a 2a 20 43 75 72 72  gno..    ** Curr
144d2 65 6e 74 6c 79 2c 20 6e 6f 20 73 75 63 68 20 70  ently, no such p
144d3 61 67 65 20 65 78 69 73 74 73 20 69 6e 20 74 68  age exists in th
144d4 65 20 70 61 67 65 2d 63 61 63 68 65 20 61 6e 64  e page-cache and
144d5 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 22 69 73   the .    ** "is
144d6 20 6a 6f 75 72 6e 61 6c 65 64 22 20 62 69 74 76   journaled" bitv
144d7 65 63 20 66 6c 61 67 20 68 61 73 20 62 65 65 6e  ec flag has been
144d8 20 73 65 74 2e 20 54 68 69 73 20 6e 65 65 64 73   set. This needs
144d9 20 74 6f 20 62 65 20 72 65 6d 65 64 69 65 64 20   to be remedied 
144da 62 79 0a 20 20 20 20 2a 2a 20 6c 6f 61 64 69 6e  by.    ** loadin
144db 67 20 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20  g the page into 
144dc 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20  the pager-cache 
144dd 61 6e 64 20 73 65 74 74 69 6e 67 20 74 68 65 20  and setting the 
144de 50 67 48 64 72 2e 6e 65 65 64 53 79 6e 63 20 0a  PgHdr.needSync .
144df 20 20 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 20      ** flag..   
144e0 20 2a 2a 0a 20 20 20 20 2a 2a 20 49 66 20 74 68   **.    ** If th
144e1 65 20 61 74 74 65 6d 70 74 20 74 6f 20 6c 6f 61  e attempt to loa
144e2 64 20 74 68 65 20 70 61 67 65 20 69 6e 74 6f 20  d the page into 
144e3 74 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 66  the page-cache f
144e4 61 69 6c 73 2c 20 28 64 75 65 0a 20 20 20 20 2a  ails, (due.    *
144e5 2a 20 74 6f 20 61 20 6d 61 6c 6c 6f 63 28 29 20  * to a malloc() 
144e6 6f 72 20 49 4f 20 66 61 69 6c 75 72 65 29 2c 20  or IO failure), 
144e7 63 6c 65 61 72 20 74 68 65 20 62 69 74 20 69 6e  clear the bit in
144e8 20 74 68 65 20 70 49 6e 4a 6f 75 72 6e 61 6c 5b   the pInJournal[
144e9 5d 0a 20 20 20 20 2a 2a 20 61 72 72 61 79 2e 20  ].    ** array. 
144ea 4f 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68  Otherwise, if th
144eb 65 20 70 61 67 65 20 69 73 20 6c 6f 61 64 65 64  e page is loaded
144ec 20 61 6e 64 20 77 72 69 74 74 65 6e 20 61 67 61   and written aga
144ed 69 6e 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68 69  in in.    ** thi
144ee 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69  s transaction, i
144ef 74 20 6d 61 79 20 62 65 20 77 72 69 74 74 65 6e  t may be written
144f0 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65   to the database
144f1 20 66 69 6c 65 20 62 65 66 6f 72 65 0a 20 20 20   file before.   
144f2 20 2a 2a 20 69 74 20 69 73 20 73 79 6e 63 65 64   ** it is synced
144f3 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61   into the journa
144f4 6c 20 66 69 6c 65 2e 20 54 68 69 73 20 77 61 79  l file. This way
144f5 2c 20 69 74 20 6d 61 79 20 65 6e 64 20 75 70 20  , it may end up 
144f6 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 6a 6f  in.    ** the jo
144f7 75 72 6e 61 6c 20 66 69 6c 65 20 74 77 69 63 65  urnal file twice
144f8 2c 20 62 75 74 20 74 68 61 74 20 69 73 20 6e 6f  , but that is no
144f9 74 20 61 20 70 72 6f 62 6c 65 6d 2e 0a 20 20 20  t a problem..   
144fa 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73   **.    ** The s
144fb 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29  qlite3PagerGet()
144fc 20 63 61 6c 6c 20 6d 61 79 20 63 61 75 73 65 20   call may cause 
144fd 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 6f 20 73  the journal to s
144fe 79 6e 63 2e 20 53 6f 20 6d 61 6b 65 0a 20 20 20  ync. So make.   
144ff 20 2a 2a 20 73 75 72 65 20 74 68 65 20 50 61 67   ** sure the Pag
14500 65 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67  er.needSync flag
14501 20 69 73 20 73 65 74 20 74 6f 6f 2e 0a 20 20 20   is set too..   
14502 20 2a 2f 0a 20 20 20 20 50 67 48 64 72 20 2a 70   */.    PgHdr *p
14503 50 67 48 64 72 3b 0a 20 20 20 20 61 73 73 65 72  PgHdr;.    asser
14504 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53  t( pPager->needS
14505 79 6e 63 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  ync );.    rc = 
14506 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
14507 70 50 61 67 65 72 2c 20 6e 65 65 64 53 79 6e 63  pPager, needSync
14508 50 67 6e 6f 2c 20 26 70 50 67 48 64 72 29 3b 0a  Pgno, &pPgHdr);.
14509 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1450a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69  TE_OK ){.      i
1450b 66 28 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 3c  f( needSyncPgno<
1450c 3d 70 50 61 67 65 72 2d 3e 64 62 4f 72 69 67 53  =pPager->dbOrigS
1450d 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 61  ize ){.        a
1450e 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70  ssert( pPager->p
1450f 54 6d 70 53 70 61 63 65 21 3d 30 20 29 3b 0a 20  TmpSpace!=0 );. 
14510 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69         sqlite3Bi
14511 74 76 65 63 43 6c 65 61 72 28 70 50 61 67 65 72  tvecClear(pPager
14512 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 6e 65  ->pInJournal, ne
14513 65 64 53 79 6e 63 50 67 6e 6f 2c 20 70 50 61 67  edSyncPgno, pPag
14514 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a  er->pTmpSpace);.
14515 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
14516 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
14517 20 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53     pPager->needS
14518 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20 61 73 73  ync = 1;.    ass
14519 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6e 6f 53  ert( pPager->noS
1451a 79 6e 63 3d 3d 30 20 26 26 20 21 4d 45 4d 44 42  ync==0 && !MEMDB
1451b 20 29 3b 0a 20 20 20 20 70 50 67 48 64 72 2d 3e   );.    pPgHdr->
1451c 66 6c 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 4e  flags |= PGHDR_N
1451d 45 45 44 5f 53 59 4e 43 3b 0a 20 20 20 20 73 71  EED_SYNC;.    sq
1451e 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44  lite3PcacheMakeD
1451f 69 72 74 79 28 70 50 67 48 64 72 29 3b 0a 20 20  irty(pPgHdr);.  
14520 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
14521 72 65 66 28 70 50 67 48 64 72 29 3b 0a 20 20 7d  ref(pPgHdr);.  }
14522 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 46 6f 72 20  ..  /*.  ** For 
14523 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  an in-memory dat
14524 61 62 61 73 65 2c 20 6d 61 6b 65 20 73 75 72 65  abase, make sure
14525 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61   the original pa
14526 67 65 20 63 6f 6e 74 69 6e 75 65 73 0a 20 20 2a  ge continues.  *
14527 2a 20 74 6f 20 65 78 69 73 74 2c 20 69 6e 20 63  * to exist, in c
14528 61 73 65 20 74 68 65 20 74 72 61 6e 73 61 63 74  ase the transact
14529 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 72 6f 6c  ion needs to rol
1452a 6c 20 62 61 63 6b 2e 20 20 57 65 20 61 6c 6c 6f  l back.  We allo
1452b 63 61 74 65 0a 20 20 2a 2a 20 74 68 65 20 70 61  cate.  ** the pa
1452c 67 65 20 6e 6f 77 2c 20 69 6e 73 74 65 61 64 20  ge now, instead 
1452d 6f 66 20 61 74 20 72 6f 6c 6c 62 61 63 6b 2c 20  of at rollback, 
1452e 62 65 63 61 75 73 65 20 77 65 20 63 61 6e 20 62  because we can b
1452f 65 74 74 65 72 20 64 65 61 6c 0a 20 20 2a 2a 20  etter deal.  ** 
14530 77 69 74 68 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d  with an out-of-m
14531 65 6d 6f 72 79 20 65 72 72 6f 72 20 6e 6f 77 2e  emory error now.
14532 20 20 54 69 63 6b 65 74 20 23 33 37 36 31 2e 0a    Ticket #3761..
14533 20 20 2a 2f 0a 20 20 69 66 28 20 4d 45 4d 44 42    */.  if( MEMDB
14534 20 29 7b 0a 20 20 20 20 44 62 50 61 67 65 20 2a   ){.    DbPage *
14535 70 4e 65 77 3b 0a 20 20 20 20 72 63 20 3d 20 73  pNew;.    rc = s
14536 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69  qlite3PagerAcqui
14537 72 65 28 70 50 61 67 65 72 2c 20 6f 72 69 67 50  re(pPager, origP
14538 67 6e 6f 2c 20 26 70 4e 65 77 2c 20 31 29 3b 0a  gno, &pNew, 1);.
14539 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1453a 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73  TE_OK ){.      s
1453b 71 6c 69 74 65 33 50 63 61 63 68 65 4d 6f 76 65  qlite3PcacheMove
1453c 28 70 50 67 2c 20 6f 72 69 67 50 67 6e 6f 29 3b  (pPg, origPgno);
1453d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
1453e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
1453f 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 4e  te3PagerUnref(pN
14540 65 77 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75  ew);.  }..  retu
14541 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
14542 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65  #endif../*.** Re
14543 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
14544 6f 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74  o the data for t
14545 68 65 20 73 70 65 63 69 66 69 65 64 20 70 61 67  he specified pag
14546 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
14547 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
14548 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 44  e3PagerGetData(D
14549 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20 61  bPage *pPg){.  a
1454a 73 73 65 72 74 28 20 70 50 67 2d 3e 6e 52 65 66  ssert( pPg->nRef
1454b 3e 30 20 7c 7c 20 70 50 67 2d 3e 70 50 61 67 65  >0 || pPg->pPage
1454c 72 2d 3e 6d 65 6d 44 62 20 29 3b 0a 20 20 72 65  r->memDb );.  re
1454d 74 75 72 6e 20 70 50 67 2d 3e 70 44 61 74 61 3b  turn pPg->pData;
1454e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1454f 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
14550 65 20 50 61 67 65 72 2e 6e 45 78 74 72 61 20 62  e Pager.nExtra b
14551 79 74 65 73 20 6f 66 20 22 65 78 74 72 61 22 20  ytes of "extra" 
14552 73 70 61 63 65 20 0a 2a 2a 20 61 6c 6c 6f 63 61  space .** alloca
14553 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 74  ted along with t
14554 68 65 20 73 70 65 63 69 66 69 65 64 20 70 61 67  he specified pag
14555 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
14556 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
14557 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28  e3PagerGetExtra(
14558 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20  DbPage *pPg){.  
14559 72 65 74 75 72 6e 20 70 50 67 2d 3e 70 45 78 74  return pPg->pExt
1455a 72 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74  ra;.}../*.** Get
1455b 2f 73 65 74 20 74 68 65 20 6c 6f 63 6b 69 6e 67  /set the locking
1455c 2d 6d 6f 64 65 20 66 6f 72 20 74 68 69 73 20 70  -mode for this p
1455d 61 67 65 72 2e 20 50 61 72 61 6d 65 74 65 72 20  ager. Parameter 
1455e 65 4d 6f 64 65 20 6d 75 73 74 20 62 65 20 6f 6e  eMode must be on
1455f 65 0a 2a 2a 20 6f 66 20 50 41 47 45 52 5f 4c 4f  e.** of PAGER_LO
14560 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 2c  CKINGMODE_QUERY,
14561 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f   PAGER_LOCKINGMO
14562 44 45 5f 4e 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a  DE_NORMAL or .**
14563 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f   PAGER_LOCKINGMO
14564 44 45 5f 45 58 43 4c 55 53 49 56 45 2e 20 49 66  DE_EXCLUSIVE. If
14565 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69   the parameter i
14566 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c 20 74 68  s not _QUERY, th
14567 65 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e  en.** the lockin
14568 67 2d 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f  g-mode is set to
14569 20 74 68 65 20 76 61 6c 75 65 20 73 70 65 63 69   the value speci
1456a 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  fied..**.** The 
1456b 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69  returned value i
1456c 73 20 65 69 74 68 65 72 20 50 41 47 45 52 5f 4c  s either PAGER_L
1456d 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41  OCKINGMODE_NORMA
1456e 4c 20 6f 72 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f  L or.** PAGER_LO
1456f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53  CKINGMODE_EXCLUS
14570 49 56 45 2c 20 69 6e 64 69 63 61 74 69 6e 67 20  IVE, indicating 
14571 74 68 65 20 63 75 72 72 65 6e 74 20 28 70 6f 73  the current (pos
14572 73 69 62 6c 79 20 75 70 64 61 74 65 64 29 0a 2a  sibly updated).*
14573 2a 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a  * locking-mode..
14574 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14575 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
14576 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61  erLockingMode(Pa
14577 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
14578 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 72   eMode){.  asser
14579 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  t( eMode==PAGER_
1457a 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52  LOCKINGMODE_QUER
1457b 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c  Y.            ||
1457c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f   eMode==PAGER_LO
1457d 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c  CKINGMODE_NORMAL
1457e 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
1457f 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43  eMode==PAGER_LOC
14580 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49  KINGMODE_EXCLUSI
14581 56 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  VE );.  assert( 
14582 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
14583 45 5f 51 55 45 52 59 3c 30 20 29 3b 0a 20 20 61  E_QUERY<0 );.  a
14584 73 73 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43  ssert( PAGER_LOC
14585 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e  KINGMODE_NORMAL>
14586 3d 30 20 26 26 20 50 41 47 45 52 5f 4c 4f 43 4b  =0 && PAGER_LOCK
14587 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56  INGMODE_EXCLUSIV
14588 45 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 65 4d  E>=0 );.  if( eM
14589 6f 64 65 3e 3d 30 20 26 26 20 21 70 50 61 67 65  ode>=0 && !pPage
1458a 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20  r->tempFile ){. 
1458b 20 20 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75     pPager->exclu
1458c 73 69 76 65 4d 6f 64 65 20 3d 20 28 75 38 29 65  siveMode = (u8)e
1458d 4d 6f 64 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Mode;.  }.  retu
1458e 72 6e 20 28 69 6e 74 29 70 50 61 67 65 72 2d 3e  rn (int)pPager->
1458f 65 78 63 6c 75 73 69 76 65 4d 6f 64 65 3b 0a 7d  exclusiveMode;.}
14590 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 2f 73 65 74 20  ../*.** Get/set 
14591 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65  the journal-mode
14592 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e   for this pager.
14593 20 50 61 72 61 6d 65 74 65 72 20 65 4d 6f 64 65   Parameter eMode
14594 20 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 3a   must be one of:
14595 0a 2a 2a 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f  .**.**    PAGER_
14596 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51 55 45 52  JOURNALMODE_QUER
14597 59 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f  Y.**    PAGER_JO
14598 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45 54 45  URNALMODE_DELETE
14599 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55  .**    PAGER_JOU
1459a 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54  RNALMODE_TRUNCAT
1459b 45 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f  E.**    PAGER_JO
1459c 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53  URNALMODE_PERSIS
1459d 54 0a 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f  T.**    PAGER_JO
1459e 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a 2a 2a  URNALMODE_OFF.**
1459f 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41      PAGER_JOURNA
145a0 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 0a 2a 2a 0a  LMODE_MEMORY.**.
145a1 2a 2a 20 49 66 20 74 68 65 20 70 61 72 61 6d 65  ** If the parame
145a2 74 65 72 20 69 73 20 6e 6f 74 20 5f 51 55 45 52  ter is not _QUER
145a3 59 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72  Y, then the jour
145a4 6e 61 6c 5f 6d 6f 64 65 20 69 73 20 73 65 74 20  nal_mode is set 
145a5 74 6f 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20  to the.** value 
145a6 73 70 65 63 69 66 69 65 64 20 69 66 20 74 68 65  specified if the
145a7 20 63 68 61 6e 67 65 20 69 73 20 61 6c 6c 6f 77   change is allow
145a8 65 64 2e 20 20 54 68 65 20 63 68 61 6e 67 65 20  ed.  The change 
145a9 69 73 20 64 69 73 61 6c 6c 6f 77 65 64 0a 2a 2a  is disallowed.**
145aa 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69   for the followi
145ab 6e 67 20 72 65 61 73 6f 6e 73 3a 0a 2a 2a 0a 2a  ng reasons:.**.*
145ac 2a 20 20 20 2a 20 20 41 6e 20 69 6e 2d 6d 65 6d  *   *  An in-mem
145ad 6f 72 79 20 64 61 74 61 62 61 73 65 20 63 61 6e  ory database can
145ae 20 6f 6e 6c 79 20 68 61 76 65 20 69 74 73 20 6a   only have its j
145af 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 73 65 74 20  ournal_mode set 
145b0 74 6f 20 5f 4f 46 46 0a 2a 2a 20 20 20 20 20 20  to _OFF.**      
145b1 6f 72 20 5f 4d 45 4d 4f 52 59 2e 0a 2a 2a 0a 2a  or _MEMORY..**.*
145b2 2a 20 20 20 2a 20 20 54 68 65 20 6a 6f 75 72 6e  *   *  The journ
145b3 61 6c 20 6d 6f 64 65 20 6d 61 79 20 6e 6f 74 20  al mode may not 
145b4 62 65 20 63 68 61 6e 67 65 64 20 77 68 69 6c 65  be changed while
145b5 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
145b6 73 20 61 63 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20  s active..**.** 
145b7 54 68 65 20 72 65 74 75 72 6e 65 64 20 69 6e 64  The returned ind
145b8 69 63 61 74 65 20 74 68 65 20 63 75 72 72 65 6e  icate the curren
145b9 74 20 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61  t (possibly upda
145ba 74 65 64 29 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64  ted) journal-mod
145bb 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
145bc 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
145bd 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65  PagerJournalMode
145be 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20  (Pager *pPager, 
145bf 69 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73  int eMode){.  as
145c0 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47  sert( eMode==PAG
145c1 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51  ER_JOURNALMODE_Q
145c2 55 45 52 59 0a 20 20 20 20 20 20 20 20 20 20 20  UERY.           
145c3 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52   || eMode==PAGER
145c4 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c  _JOURNALMODE_DEL
145c5 45 54 45 0a 20 20 20 20 20 20 20 20 20 20 20 20  ETE.            
145c6 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  || eMode==PAGER_
145c7 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e  JOURNALMODE_TRUN
145c8 43 41 54 45 0a 20 20 20 20 20 20 20 20 20 20 20  CATE.           
145c9 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52   || eMode==PAGER
145ca 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52  _JOURNALMODE_PER
145cb 53 49 53 54 0a 20 20 20 20 20 20 20 20 20 20 20  SIST.           
145cc 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52   || eMode==PAGER
145cd 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46  _JOURNALMODE_OFF
145ce 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c   .            ||
145cf 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f   eMode==PAGER_JO
145d0 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59  URNALMODE_MEMORY
145d1 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 50 41   );.  assert( PA
145d2 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
145d3 51 55 45 52 59 3c 30 20 29 3b 0a 20 20 69 66 28  QUERY<0 );.  if(
145d4 20 65 4d 6f 64 65 3e 3d 30 0a 20 20 20 26 26 20   eMode>=0.   && 
145d5 28 21 4d 45 4d 44 42 20 7c 7c 20 65 4d 6f 64 65  (!MEMDB || eMode
145d6 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  ==PAGER_JOURNALM
145d7 4f 44 45 5f 4d 45 4d 4f 52 59 20 0a 20 20 20 20  ODE_MEMORY .    
145d8 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f            || eMo
145d9 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
145da 4c 4d 4f 44 45 5f 4f 46 46 29 0a 20 20 20 26 26  LMODE_OFF).   &&
145db 20 21 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69   !pPager->dbModi
145dc 66 69 65 64 0a 20 20 20 26 26 20 28 21 69 73 4f  fied.   && (!isO
145dd 70 65 6e 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  pen(pPager->jfd)
145de 20 7c 7c 20 30 3d 3d 70 50 61 67 65 72 2d 3e 6a   || 0==pPager->j
145df 6f 75 72 6e 61 6c 4f 66 66 29 0a 20 20 29 7b 0a  ournalOff).  ){.
145e0 20 20 20 20 69 66 28 20 69 73 4f 70 65 6e 28 70      if( isOpen(p
145e1 50 61 67 65 72 2d 3e 6a 66 64 29 20 29 7b 0a 20  Pager->jfd) ){. 
145e2 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c       sqlite3OsCl
145e3 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29  ose(pPager->jfd)
145e4 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67  ;.    }.    pPag
145e5 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20  er->journalMode 
145e6 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20 20 7d  = (u8)eMode;.  }
145e7 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29 70  .  return (int)p
145e8 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f  Pager->journalMo
145e9 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74  de;.}../*.** Get
145ea 2f 73 65 74 20 74 68 65 20 73 69 7a 65 2d 6c 69  /set the size-li
145eb 6d 69 74 20 75 73 65 64 20 66 6f 72 20 70 65 72  mit used for per
145ec 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c 20  sistent journal 
145ed 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 74  files..**.** Set
145ee 74 69 6e 67 20 74 68 65 20 73 69 7a 65 20 6c 69  ting the size li
145ef 6d 69 74 20 74 6f 20 2d 31 20 6d 65 61 6e 73 20  mit to -1 means 
145f0 6e 6f 20 6c 69 6d 69 74 20 69 73 20 65 6e 66 6f  no limit is enfo
145f1 72 63 65 64 2e 0a 2a 2a 20 41 6e 20 61 74 74 65  rced..** An atte
145f2 6d 70 74 20 74 6f 20 73 65 74 20 61 20 6c 69 6d  mpt to set a lim
145f3 69 74 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  it smaller than 
145f4 2d 31 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  -1 is a no-op..*
145f5 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
145f6 20 69 36 34 20 73 71 6c 69 74 65 33 50 61 67 65   i64 sqlite3Page
145f7 72 4a 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69  rJournalSizeLimi
145f8 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  t(Pager *pPager,
145f9 20 69 36 34 20 69 4c 69 6d 69 74 29 7b 0a 20 20   i64 iLimit){.  
145fa 69 66 28 20 69 4c 69 6d 69 74 3e 3d 2d 31 20 29  if( iLimit>=-1 )
145fb 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f  {.    pPager->jo
145fc 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d  urnalSizeLimit =
145fd 20 69 4c 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72   iLimit;.  }.  r
145fe 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 6a 6f  eturn pPager->jo
145ff 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a  urnalSizeLimit;.
14600 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
14601 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
14602 20 70 50 61 67 65 72 2d 3e 70 42 61 63 6b 75 70   pPager->pBackup
14603 20 76 61 72 69 61 62 6c 65 2e 20 54 68 65 20 62   variable. The b
14604 61 63 6b 75 70 20 6d 6f 64 75 6c 65 0a 2a 2a 20  ackup module.** 
14605 69 6e 20 62 61 63 6b 75 70 2e 63 20 6d 61 69 6e  in backup.c main
14606 74 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 6e  tains the conten
14607 74 20 6f 66 20 74 68 69 73 20 76 61 72 69 61 62  t of this variab
14608 6c 65 2e 20 54 68 69 73 20 6d 6f 64 75 6c 65 0a  le. This module.
14609 2a 2a 20 75 73 65 73 20 69 74 20 6f 70 61 71 75  ** uses it opaqu
1460a 65 6c 79 20 61 73 20 61 6e 20 61 72 67 75 6d 65  ely as an argume
1460b 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 42 61 63  nt to sqlite3Bac
1460c 6b 75 70 52 65 73 74 61 72 74 28 29 20 61 6e 64  kupRestart() and
1460d 0a 2a 2a 20 73 71 6c 69 74 65 33 42 61 63 6b 75  .** sqlite3Backu
1460e 70 55 70 64 61 74 65 28 29 20 6f 6e 6c 79 2e 0a  pUpdate() only..
1460f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14610 45 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  E sqlite3_backup
14611 20 2a 2a 73 71 6c 69 74 65 33 50 61 67 65 72 42   **sqlite3PagerB
14612 61 63 6b 75 70 50 74 72 28 50 61 67 65 72 20 2a  ackupPtr(Pager *
14613 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72  pPager){.  retur
14614 6e 20 26 70 50 61 67 65 72 2d 3e 70 42 61 63 6b  n &pPager->pBack
14615 75 70 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a  up;.}..#endif /*
14616 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53   SQLITE_OMIT_DIS
14617 4b 49 4f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  KIO */../*******
14618 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70  ******* End of p
14619 61 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  ager.c *********
1461a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1461b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1461c 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
1461d 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
1461e 6c 65 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a  le btmutex.c ***
1461f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14621 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
14622 30 37 20 41 75 67 75 73 74 20 32 37 0a 2a 2a 0a  07 August 27.**.
14623 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
14624 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
14625 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
14626 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
14627 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
14628 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
14629 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
1462a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
1462b 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
1462c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1462d 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
1462e 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
1462f 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
14630 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
14631 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
14632 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
14633 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
14634 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
14635 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14636 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14637 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14638 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
14639 2a 20 24 49 64 3a 20 62 74 6d 75 74 65 78 2e 63  * $Id: btmutex.c
1463a 2c 76 20 31 2e 31 37 20 32 30 30 39 2f 30 37 2f  ,v 1.17 2009/07/
1463b 32 30 20 31 32 3a 33 33 3a 33 33 20 64 72 68 20  20 12:33:33 drh 
1463c 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73  Exp $.**.** This
1463d 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
1463e 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c  ode used to impl
1463f 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 6f 6e  ement mutexes on
14640 20 42 74 72 65 65 20 6f 62 6a 65 63 74 73 2e 0a   Btree objects..
14641 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 72 65 61  ** This code rea
14642 6c 6c 79 20 62 65 6c 6f 6e 67 73 20 69 6e 20 62  lly belongs in b
14643 74 72 65 65 2e 63 2e 20 20 42 75 74 20 62 74 72  tree.c.  But btr
14644 65 65 2e 63 20 69 73 20 67 65 74 74 69 6e 67 20  ee.c is getting 
14645 74 6f 6f 0a 2a 2a 20 62 69 67 20 61 6e 64 20 77  too.** big and w
14646 65 20 77 61 6e 74 20 74 6f 20 62 72 65 61 6b 20  e want to break 
14647 69 74 20 64 6f 77 6e 20 73 6f 6d 65 2e 20 20 54  it down some.  T
14648 68 69 73 20 70 61 63 6b 61 67 65 64 20 73 65 65  his packaged see
14649 6d 65 64 20 6c 69 6b 65 0a 2a 2a 20 61 20 67 6f  med like.** a go
1464a 6f 64 20 62 72 65 61 6b 6f 75 74 2e 0a 2a 2f 0a  od breakout..*/.
1464b 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1464c 49 6e 63 6c 75 64 65 20 62 74 72 65 65 49 6e 74  Include btreeInt
1464d 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
1464e 20 6f 66 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a   of btmutex.c **
1464f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
14650 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
14651 42 65 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65  Begin file btree
14652 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
14653 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14654 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
14655 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c  /*.** 2004 April
14656 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74   6.**.** The aut
14657 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
14658 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
14659 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
1465a 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
1465b 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
1465c 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
1465d 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
1465e 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
1465f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
14660 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
14661 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
14662 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
14663 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
14664 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
14665 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
14666 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
14667 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
14668 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14669 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1466a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1466b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1466c 2a 0a 2a 2a 20 24 49 64 3a 20 62 74 72 65 65 49  *.** $Id: btreeI
1466d 6e 74 2e 68 2c 76 20 31 2e 35 32 20 32 30 30 39  nt.h,v 1.52 2009
1466e 2f 30 37 2f 31 35 20 31 37 3a 32 35 3a 34 36 20  /07/15 17:25:46 
1466f 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20  drh Exp $.**.** 
14670 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d  This file implem
14671 65 6e 74 73 20 61 20 65 78 74 65 72 6e 61 6c 20  ents a external 
14672 28 64 69 73 6b 2d 62 61 73 65 64 29 20 64 61 74  (disk-based) dat
14673 61 62 61 73 65 20 75 73 69 6e 67 20 42 54 72 65  abase using BTre
14674 65 73 2e 0a 2a 2a 20 46 6f 72 20 61 20 64 65 74  es..** For a det
14675 61 69 6c 65 64 20 64 69 73 63 75 73 73 69 6f 6e  ailed discussion
14676 20 6f 66 20 42 54 72 65 65 73 2c 20 72 65 66 65   of BTrees, refe
14677 72 20 74 6f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44  r to.**.**     D
14678 6f 6e 61 6c 64 20 45 2e 20 4b 6e 75 74 68 2c 20  onald E. Knuth, 
14679 54 48 45 20 41 52 54 20 4f 46 20 43 4f 4d 50 55  THE ART OF COMPU
1467a 54 45 52 20 50 52 4f 47 52 41 4d 4d 49 4e 47 2c  TER PROGRAMMING,
1467b 20 56 6f 6c 75 6d 65 20 33 3a 0a 2a 2a 20 20 20   Volume 3:.**   
1467c 20 20 22 53 6f 72 74 69 6e 67 20 41 6e 64 20 53    "Sorting And S
1467d 65 61 72 63 68 69 6e 67 22 2c 20 70 61 67 65 73  earching", pages
1467e 20 34 37 33 2d 34 38 30 2e 20 41 64 64 69 73 6f   473-480. Addiso
1467f 6e 2d 57 65 73 6c 65 79 0a 2a 2a 20 20 20 20 20  n-Wesley.**     
14680 50 75 62 6c 69 73 68 69 6e 67 20 43 6f 6d 70 61  Publishing Compa
14681 6e 79 2c 20 52 65 61 64 69 6e 67 2c 20 4d 61 73  ny, Reading, Mas
14682 73 61 63 68 75 73 65 74 74 73 2e 0a 2a 2a 0a 2a  sachusetts..**.*
14683 2a 20 54 68 65 20 62 61 73 69 63 20 69 64 65 61  * The basic idea
14684 20 69 73 20 74 68 61 74 20 65 61 63 68 20 70 61   is that each pa
14685 67 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 63  ge of the file c
14686 6f 6e 74 61 69 6e 73 20 4e 20 64 61 74 61 62 61  ontains N databa
14687 73 65 0a 2a 2a 20 65 6e 74 72 69 65 73 20 61 6e  se.** entries an
14688 64 20 4e 2b 31 20 70 6f 69 6e 74 65 72 73 20 74  d N+1 pointers t
14689 6f 20 73 75 62 70 61 67 65 73 2e 0a 2a 2a 0a 2a  o subpages..**.*
1468a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *   ------------
1468b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1468c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1468d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1468e 2d 2d 2d 2d 0a 2a 2a 20 20 20 7c 20 20 50 74 72  ----.**   |  Ptr
1468f 28 30 29 20 7c 20 4b 65 79 28 30 29 20 7c 20 50  (0) | Key(0) | P
14690 74 72 28 31 29 20 7c 20 4b 65 79 28 31 29 20 7c  tr(1) | Key(1) |
14691 20 2e 2e 2e 20 7c 20 4b 65 79 28 4e 2d 31 29 20   ... | Key(N-1) 
14692 7c 20 50 74 72 28 4e 29 20 7c 0a 2a 2a 20 20 20  | Ptr(N) |.**   
14693 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14694 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14695 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14696 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
14697 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68  .**.** All of th
14698 65 20 6b 65 79 73 20 6f 6e 20 74 68 65 20 70 61  e keys on the pa
14699 67 65 20 74 68 61 74 20 50 74 72 28 30 29 20 70  ge that Ptr(0) p
1469a 6f 69 6e 74 73 20 74 6f 20 68 61 76 65 20 76 61  oints to have va
1469b 6c 75 65 73 20 6c 65 73 73 0a 2a 2a 20 74 68 61  lues less.** tha
1469c 6e 20 4b 65 79 28 30 29 2e 20 20 41 6c 6c 20 6f  n Key(0).  All o
1469d 66 20 74 68 65 20 6b 65 79 73 20 6f 6e 20 70 61  f the keys on pa
1469e 67 65 20 50 74 72 28 31 29 20 61 6e 64 20 69 74  ge Ptr(1) and it
1469f 73 20 73 75 62 70 61 67 65 73 20 68 61 76 65 0a  s subpages have.
146a0 2a 2a 20 76 61 6c 75 65 73 20 67 72 65 61 74 65  ** values greate
146a1 72 20 74 68 61 6e 20 4b 65 79 28 30 29 20 61 6e  r than Key(0) an
146a2 64 20 6c 65 73 73 20 74 68 61 6e 20 4b 65 79 28  d less than Key(
146a3 31 29 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20  1).  All of the 
146a4 6b 65 79 73 0a 2a 2a 20 6f 6e 20 50 74 72 28 4e  keys.** on Ptr(N
146a5 29 20 61 6e 64 20 69 74 73 20 73 75 62 70 61 67  ) and its subpag
146a6 65 73 20 68 61 76 65 20 76 61 6c 75 65 73 20 67  es have values g
146a7 72 65 61 74 65 72 20 74 68 61 6e 20 4b 65 79 28  reater than Key(
146a8 4e 2d 31 29 2e 20 20 41 6e 64 0a 2a 2a 20 73 6f  N-1).  And.** so
146a9 20 66 6f 72 74 68 2e 0a 2a 2a 0a 2a 2a 20 46 69   forth..**.** Fi
146aa 6e 64 69 6e 67 20 61 20 70 61 72 74 69 63 75 6c  nding a particul
146ab 61 72 20 6b 65 79 20 72 65 71 75 69 72 65 73 20  ar key requires 
146ac 72 65 61 64 69 6e 67 20 4f 28 6c 6f 67 28 4d 29  reading O(log(M)
146ad 29 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65  ) pages from the
146ae 20 0a 2a 2a 20 64 69 73 6b 20 77 68 65 72 65 20   .** disk where 
146af 4d 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  M is the number 
146b0 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  of entries in th
146b1 65 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e  e tree..**.** In
146b2 20 74 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61   this implementa
146b3 74 69 6f 6e 2c 20 61 20 73 69 6e 67 6c 65 20 66  tion, a single f
146b4 69 6c 65 20 63 61 6e 20 68 6f 6c 64 20 6f 6e 65  ile can hold one
146b5 20 6f 72 20 6d 6f 72 65 20 73 65 70 61 72 61 74   or more separat
146b6 65 20 0a 2a 2a 20 42 54 72 65 65 73 2e 20 20 45  e .** BTrees.  E
146b7 61 63 68 20 42 54 72 65 65 20 69 73 20 69 64 65  ach BTree is ide
146b8 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 69  ntified by the i
146b9 6e 64 65 78 20 6f 66 20 69 74 73 20 72 6f 6f 74  ndex of its root
146ba 20 70 61 67 65 2e 20 20 54 68 65 0a 2a 2a 20 6b   page.  The.** k
146bb 65 79 20 61 6e 64 20 64 61 74 61 20 66 6f 72 20  ey and data for 
146bc 61 6e 79 20 65 6e 74 72 79 20 61 72 65 20 63 6f  any entry are co
146bd 6d 62 69 6e 65 64 20 74 6f 20 66 6f 72 6d 20 74  mbined to form t
146be 68 65 20 22 70 61 79 6c 6f 61 64 22 2e 20 20 41  he "payload".  A
146bf 0a 2a 2a 20 66 69 78 65 64 20 61 6d 6f 75 6e 74  .** fixed amount
146c0 20 6f 66 20 70 61 79 6c 6f 61 64 20 63 61 6e 20   of payload can 
146c1 62 65 20 63 61 72 72 69 65 64 20 64 69 72 65 63  be carried direc
146c2 74 6c 79 20 6f 6e 20 74 68 65 20 64 61 74 61 62  tly on the datab
146c3 61 73 65 0a 2a 2a 20 70 61 67 65 2e 20 20 49 66  ase.** page.  If
146c4 20 74 68 65 20 70 61 79 6c 6f 61 64 20 69 73 20   the payload is 
146c5 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20  larger than the 
146c6 70 72 65 73 65 74 20 61 6d 6f 75 6e 74 20 74 68  preset amount th
146c7 65 6e 20 73 75 72 70 6c 75 73 0a 2a 2a 20 62 79  en surplus.** by
146c8 74 65 73 20 61 72 65 20 73 74 6f 72 65 64 20 6f  tes are stored o
146c9 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  n overflow pages
146ca 2e 20 20 54 68 65 20 70 61 79 6c 6f 61 64 20 66  .  The payload f
146cb 6f 72 20 61 6e 20 65 6e 74 72 79 0a 2a 2a 20 61  or an entry.** a
146cc 6e 64 20 74 68 65 20 70 72 65 63 65 64 69 6e 67  nd the preceding
146cd 20 70 6f 69 6e 74 65 72 20 61 72 65 20 63 6f 6d   pointer are com
146ce 62 69 6e 65 64 20 74 6f 20 66 6f 72 6d 20 61 20  bined to form a 
146cf 22 43 65 6c 6c 22 2e 20 20 45 61 63 68 20 0a 2a  "Cell".  Each .*
146d0 2a 20 70 61 67 65 20 68 61 73 20 61 20 73 6d 61  * page has a sma
146d1 6c 6c 20 68 65 61 64 65 72 20 77 68 69 63 68 20  ll header which 
146d2 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 50 74 72  contains the Ptr
146d3 28 4e 29 20 70 6f 69 6e 74 65 72 20 61 6e 64 20  (N) pointer and 
146d4 6f 74 68 65 72 0a 2a 2a 20 69 6e 66 6f 72 6d 61  other.** informa
146d5 74 69 6f 6e 20 73 75 63 68 20 61 73 20 74 68 65  tion such as the
146d6 20 73 69 7a 65 20 6f 66 20 6b 65 79 20 61 6e 64   size of key and
146d7 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 46 4f 52   data..**.** FOR
146d8 4d 41 54 20 44 45 54 41 49 4c 53 0a 2a 2a 0a 2a  MAT DETAILS.**.*
146d9 2a 20 54 68 65 20 66 69 6c 65 20 69 73 20 64 69  * The file is di
146da 76 69 64 65 64 20 69 6e 74 6f 20 70 61 67 65 73  vided into pages
146db 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61 67  .  The first pag
146dc 65 20 69 73 20 63 61 6c 6c 65 64 20 70 61 67 65  e is called page
146dd 20 31 2c 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e   1,.** the secon
146de 64 20 69 73 20 70 61 67 65 20 32 2c 20 61 6e 64  d is page 2, and
146df 20 73 6f 20 66 6f 72 74 68 2e 20 20 41 20 70 61   so forth.  A pa
146e0 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 7a 65 72  ge number of zer
146e1 6f 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 22  o indicates.** "
146e2 6e 6f 20 73 75 63 68 20 70 61 67 65 22 2e 20 20  no such page".  
146e3 54 68 65 20 70 61 67 65 20 73 69 7a 65 20 63 61  The page size ca
146e4 6e 20 62 65 20 61 6e 79 20 70 6f 77 65 72 20 6f  n be any power o
146e5 66 20 32 20 62 65 74 77 65 65 6e 20 35 31 32 20  f 2 between 512 
146e6 61 6e 64 20 33 32 37 36 38 2e 0a 2a 2a 20 45 61  and 32768..** Ea
146e7 63 68 20 70 61 67 65 20 63 61 6e 20 62 65 20 65  ch page can be e
146e8 69 74 68 65 72 20 61 20 62 74 72 65 65 20 70 61  ither a btree pa
146e9 67 65 2c 20 61 20 66 72 65 65 6c 69 73 74 20 70  ge, a freelist p
146ea 61 67 65 2c 20 61 6e 20 6f 76 65 72 66 6c 6f 77  age, an overflow
146eb 0a 2a 2a 20 70 61 67 65 2c 20 6f 72 20 61 20 70  .** page, or a p
146ec 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2e  ointer-map page.
146ed 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
146ee 20 70 61 67 65 20 69 73 20 61 6c 77 61 79 73 20   page is always 
146ef 61 20 62 74 72 65 65 20 70 61 67 65 2e 20 20 54  a btree page.  T
146f0 68 65 20 66 69 72 73 74 20 31 30 30 20 62 79 74  he first 100 byt
146f1 65 73 20 6f 66 20 74 68 65 20 66 69 72 73 74 0a  es of the first.
146f2 2a 2a 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 20  ** page contain 
146f3 61 20 73 70 65 63 69 61 6c 20 68 65 61 64 65 72  a special header
146f4 20 28 74 68 65 20 22 66 69 6c 65 20 68 65 61 64   (the "file head
146f5 65 72 22 29 20 74 68 61 74 20 64 65 73 63 72 69  er") that descri
146f6 62 65 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a  bes the file..**
146f7 20 54 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74   The format of t
146f8 68 65 20 66 69 6c 65 20 68 65 61 64 65 72 20 69  he file header i
146f9 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  s as follows:.**
146fa 0a 2a 2a 20 20 20 4f 46 46 53 45 54 20 20 20 53  .**   OFFSET   S
146fb 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49  IZE    DESCRIPTI
146fc 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20  ON.**      0    
146fd 20 20 31 36 20 20 20 20 20 48 65 61 64 65 72 20    16     Header 
146fe 73 74 72 69 6e 67 3a 20 22 53 51 4c 69 74 65 20  string: "SQLite 
146ff 66 6f 72 6d 61 74 20 33 5c 30 30 30 22 0a 2a 2a  format 3\000".**
14700 20 20 20 20 20 31 36 20 20 20 20 20 20 20 32 20       16       2 
14701 20 20 20 20 50 61 67 65 20 73 69 7a 65 20 69 6e      Page size in
14702 20 62 79 74 65 73 2e 20 20 0a 2a 2a 20 20 20 20   bytes.  .**    
14703 20 31 38 20 20 20 20 20 20 20 31 20 20 20 20 20   18       1     
14704 46 69 6c 65 20 66 6f 72 6d 61 74 20 77 72 69 74  File format writ
14705 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20  e version.**    
14706 20 31 39 20 20 20 20 20 20 20 31 20 20 20 20 20   19       1     
14707 46 69 6c 65 20 66 6f 72 6d 61 74 20 72 65 61 64  File format read
14708 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20   version.**     
14709 32 30 20 20 20 20 20 20 20 31 20 20 20 20 20 42  20       1     B
1470a 79 74 65 73 20 6f 66 20 75 6e 75 73 65 64 20 73  ytes of unused s
1470b 70 61 63 65 20 61 74 20 74 68 65 20 65 6e 64 20  pace at the end 
1470c 6f 66 20 65 61 63 68 20 70 61 67 65 0a 2a 2a 20  of each page.** 
1470d 20 20 20 20 32 31 20 20 20 20 20 20 20 31 20 20      21       1  
1470e 20 20 20 4d 61 78 20 65 6d 62 65 64 64 65 64 20     Max embedded 
1470f 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e  payload fraction
14710 0a 2a 2a 20 20 20 20 20 32 32 20 20 20 20 20 20  .**     22      
14711 20 31 20 20 20 20 20 4d 69 6e 20 65 6d 62 65 64   1     Min embed
14712 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63  ded payload frac
14713 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 33 20 20  tion.**     23  
14714 20 20 20 20 20 31 20 20 20 20 20 4d 69 6e 20 6c       1     Min l
14715 65 61 66 20 70 61 79 6c 6f 61 64 20 66 72 61 63  eaf payload frac
14716 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 34 20 20  tion.**     24  
14717 20 20 20 20 20 34 20 20 20 20 20 46 69 6c 65 20       4     File 
14718 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 0a 2a  change counter.*
14719 2a 20 20 20 20 20 32 38 20 20 20 20 20 20 20 34  *     28       4
1471a 20 20 20 20 20 52 65 73 65 72 76 65 64 20 66 6f       Reserved fo
1471b 72 20 66 75 74 75 72 65 20 75 73 65 0a 2a 2a 20  r future use.** 
1471c 20 20 20 20 33 32 20 20 20 20 20 20 20 34 20 20      32       4  
1471d 20 20 20 46 69 72 73 74 20 66 72 65 65 6c 69 73     First freelis
1471e 74 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 33 36  t page.**     36
1471f 20 20 20 20 20 20 20 34 20 20 20 20 20 4e 75 6d         4     Num
14720 62 65 72 20 6f 66 20 66 72 65 65 6c 69 73 74 20  ber of freelist 
14721 70 61 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c  pages in the fil
14722 65 0a 2a 2a 20 20 20 20 20 34 30 20 20 20 20 20  e.**     40     
14723 20 36 30 20 20 20 20 20 31 35 20 34 2d 62 79 74   60     15 4-byt
14724 65 20 6d 65 74 61 20 76 61 6c 75 65 73 20 70 61  e meta values pa
14725 73 73 65 64 20 74 6f 20 68 69 67 68 65 72 20 6c  ssed to higher l
14726 61 79 65 72 73 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ayers.**.**     
14727 34 30 20 20 20 20 20 20 20 34 20 20 20 20 20 53  40       4     S
14728 63 68 65 6d 61 20 63 6f 6f 6b 69 65 0a 2a 2a 20  chema cookie.** 
14729 20 20 20 20 34 34 20 20 20 20 20 20 20 34 20 20      44       4  
1472a 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 6f     File format o
1472b 66 20 73 63 68 65 6d 61 20 6c 61 79 65 72 0a 2a  f schema layer.*
1472c 2a 20 20 20 20 20 34 38 20 20 20 20 20 20 20 34  *     48       4
1472d 20 20 20 20 20 53 69 7a 65 20 6f 66 20 70 61 67       Size of pag
1472e 65 20 63 61 63 68 65 0a 2a 2a 20 20 20 20 20 35  e cache.**     5
1472f 32 20 20 20 20 20 20 20 34 20 20 20 20 20 4c 61  2       4     La
14730 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65 20  rgest root-page 
14731 28 61 75 74 6f 2f 69 6e 63 72 5f 76 61 63 75 75  (auto/incr_vacuu
14732 6d 29 0a 2a 2a 20 20 20 20 20 35 36 20 20 20 20  m).**     56    
14733 20 20 20 34 20 20 20 20 20 31 3d 55 54 46 2d 38     4     1=UTF-8
14734 20 32 3d 55 54 46 31 36 6c 65 20 33 3d 55 54 46   2=UTF16le 3=UTF
14735 31 36 62 65 0a 2a 2a 20 20 20 20 20 36 30 20 20  16be.**     60  
14736 20 20 20 20 20 34 20 20 20 20 20 55 73 65 72 20       4     User 
14737 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 36  version.**     6
14738 34 20 20 20 20 20 20 20 34 20 20 20 20 20 49 6e  4       4     In
14739 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d  cremental vacuum
1473a 20 6d 6f 64 65 0a 2a 2a 20 20 20 20 20 36 38 20   mode.**     68 
1473b 20 20 20 20 20 20 34 20 20 20 20 20 75 6e 75 73        4     unus
1473c 65 64 0a 2a 2a 20 20 20 20 20 37 32 20 20 20 20  ed.**     72    
1473d 20 20 20 34 20 20 20 20 20 75 6e 75 73 65 64 0a     4     unused.
1473e 2a 2a 20 20 20 20 20 37 36 20 20 20 20 20 20 20  **     76       
1473f 34 20 20 20 20 20 75 6e 75 73 65 64 0a 2a 2a 0a  4     unused.**.
14740 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 69 6e  ** All of the in
14741 74 65 67 65 72 20 76 61 6c 75 65 73 20 61 72 65  teger values are
14742 20 62 69 67 2d 65 6e 64 69 61 6e 20 28 6d 6f 73   big-endian (mos
14743 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20 62 79  t significant by
14744 74 65 20 66 69 72 73 74 29 2e 0a 2a 2a 0a 2a 2a  te first)..**.**
14745 20 54 68 65 20 66 69 6c 65 20 63 68 61 6e 67 65   The file change
14746 20 63 6f 75 6e 74 65 72 20 69 73 20 69 6e 63 72   counter is incr
14747 65 6d 65 6e 74 65 64 20 77 68 65 6e 20 74 68 65  emented when the
14748 20 64 61 74 61 62 61 73 65 20 69 73 20 63 68 61   database is cha
14749 6e 67 65 64 0a 2a 2a 20 54 68 69 73 20 63 6f 75  nged.** This cou
1474a 6e 74 65 72 20 61 6c 6c 6f 77 73 20 6f 74 68 65  nter allows othe
1474b 72 20 70 72 6f 63 65 73 73 65 73 20 74 6f 20 6b  r processes to k
1474c 6e 6f 77 20 77 68 65 6e 20 74 68 65 20 66 69 6c  now when the fil
1474d 65 20 68 61 73 20 63 68 61 6e 67 65 64 0a 2a 2a  e has changed.**
1474e 20 61 6e 64 20 74 68 75 73 20 77 68 65 6e 20 74   and thus when t
1474f 68 65 79 20 6e 65 65 64 20 74 6f 20 66 6c 75 73  hey need to flus
14750 68 20 74 68 65 69 72 20 63 61 63 68 65 2e 0a 2a  h their cache..*
14751 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 20 65 6d 62  *.** The max emb
14752 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72  edded payload fr
14753 61 63 74 69 6f 6e 20 69 73 20 74 68 65 20 61 6d  action is the am
14754 6f 75 6e 74 20 6f 66 20 74 68 65 20 74 6f 74 61  ount of the tota
14755 6c 20 75 73 61 62 6c 65 0a 2a 2a 20 73 70 61 63  l usable.** spac
14756 65 20 69 6e 20 61 20 70 61 67 65 20 74 68 61 74  e in a page that
14757 20 63 61 6e 20 62 65 20 63 6f 6e 73 75 6d 65 64   can be consumed
14758 20 62 79 20 61 20 73 69 6e 67 6c 65 20 63 65 6c   by a single cel
14759 6c 20 66 6f 72 20 73 74 61 6e 64 61 72 64 0a 2a  l for standard.*
1475a 2a 20 42 2d 74 72 65 65 20 28 6e 6f 6e 2d 4c 45  * B-tree (non-LE
1475b 41 46 44 41 54 41 29 20 74 61 62 6c 65 73 2e 20  AFDATA) tables. 
1475c 20 41 20 76 61 6c 75 65 20 6f 66 20 32 35 35 20   A value of 255 
1475d 6d 65 61 6e 73 20 31 30 30 25 2e 20 20 54 68 65  means 100%.  The
1475e 20 64 65 66 61 75 6c 74 0a 2a 2a 20 69 73 20 74   default.** is t
1475f 6f 20 6c 69 6d 69 74 20 74 68 65 20 6d 61 78 69  o limit the maxi
14760 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 73 6f  mum cell size so
14761 20 74 68 61 74 20 61 74 20 6c 65 61 73 74 20 34   that at least 4
14762 20 63 65 6c 6c 73 20 77 69 6c 6c 20 66 69 74 0a   cells will fit.
14763 2a 2a 20 6f 6e 20 6f 6e 65 20 70 61 67 65 2e 20  ** on one page. 
14764 20 54 68 75 73 20 74 68 65 20 64 65 66 61 75 6c   Thus the defaul
14765 74 20 6d 61 78 20 65 6d 62 65 64 64 65 64 20 70  t max embedded p
14766 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20  ayload fraction 
14767 69 73 20 36 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  is 64..**.** If 
14768 74 68 65 20 70 61 79 6c 6f 61 64 20 66 6f 72 20  the payload for 
14769 61 20 63 65 6c 6c 20 69 73 20 6c 61 72 67 65 72  a cell is larger
1476a 20 74 68 61 6e 20 74 68 65 20 6d 61 78 20 70 61   than the max pa
1476b 79 6c 6f 61 64 2c 20 74 68 65 6e 20 65 78 74 72  yload, then extr
1476c 61 0a 2a 2a 20 70 61 79 6c 6f 61 64 20 69 73 20  a.** payload is 
1476d 73 70 69 6c 6c 65 64 20 74 6f 20 6f 76 65 72 66  spilled to overf
1476e 6c 6f 77 20 70 61 67 65 73 2e 20 20 4f 6e 63 65  low pages.  Once
1476f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   an overflow pag
14770 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c 0a  e is allocated,.
14771 2a 2a 20 61 73 20 6d 61 6e 79 20 62 79 74 65 73  ** as many bytes
14772 20 61 73 20 70 6f 73 73 69 62 6c 65 20 61 72 65   as possible are
14773 20 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68 65 20   moved into the 
14774 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 77  overflow pages w
14775 69 74 68 6f 75 74 20 6c 65 74 74 69 6e 67 0a 2a  ithout letting.*
14776 2a 20 74 68 65 20 63 65 6c 6c 20 73 69 7a 65 20  * the cell size 
14777 64 72 6f 70 20 62 65 6c 6f 77 20 74 68 65 20 6d  drop below the m
14778 69 6e 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c  in embedded payl
14779 6f 61 64 20 66 72 61 63 74 69 6f 6e 2e 0a 2a 2a  oad fraction..**
1477a 0a 2a 2a 20 54 68 65 20 6d 69 6e 20 6c 65 61 66  .** The min leaf
1477b 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f   payload fractio
1477c 6e 20 69 73 20 6c 69 6b 65 20 74 68 65 20 6d 69  n is like the mi
1477d 6e 20 65 6d 62 65 64 64 65 64 20 70 61 79 6c 6f  n embedded paylo
1477e 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a 2a 20 65  ad fraction.** e
1477f 78 63 65 70 74 20 74 68 61 74 20 69 74 20 61 70  xcept that it ap
14780 70 6c 69 65 73 20 74 6f 20 6c 65 61 66 20 6e 6f  plies to leaf no
14781 64 65 73 20 69 6e 20 61 20 4c 45 41 46 44 41 54  des in a LEAFDAT
14782 41 20 74 72 65 65 2e 20 20 54 68 65 20 6d 61 78  A tree.  The max
14783 69 6d 75 6d 0a 2a 2a 20 70 61 79 6c 6f 61 64 20  imum.** payload 
14784 66 72 61 63 74 69 6f 6e 20 66 6f 72 20 61 20 4c  fraction for a L
14785 45 41 46 44 41 54 41 20 74 72 65 65 20 69 73 20  EAFDATA tree is 
14786 61 6c 77 61 79 73 20 31 30 30 25 20 28 6f 72 20  always 100% (or 
14787 32 35 35 29 20 61 6e 64 20 69 74 0a 2a 2a 20 6e  255) and it.** n
14788 6f 74 20 73 70 65 63 69 66 69 65 64 20 69 6e 20  ot specified in 
14789 74 68 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a  the header..**.*
1478a 2a 20 45 61 63 68 20 62 74 72 65 65 20 70 61 67  * Each btree pag
1478b 65 73 20 69 73 20 64 69 76 69 64 65 64 20 69 6e  es is divided in
1478c 74 6f 20 74 68 72 65 65 20 73 65 63 74 69 6f 6e  to three section
1478d 73 3a 20 20 54 68 65 20 68 65 61 64 65 72 2c 20  s:  The header, 
1478e 74 68 65 0a 2a 2a 20 63 65 6c 6c 20 70 6f 69 6e  the.** cell poin
1478f 74 65 72 20 61 72 72 61 79 2c 20 61 6e 64 20 74  ter array, and t
14790 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  he cell content 
14791 61 72 65 61 2e 20 20 50 61 67 65 20 31 20 61 6c  area.  Page 1 al
14792 73 6f 20 68 61 73 20 61 20 31 30 30 2d 62 79 74  so has a 100-byt
14793 65 0a 2a 2a 20 66 69 6c 65 20 68 65 61 64 65 72  e.** file header
14794 20 74 68 61 74 20 6f 63 63 75 72 73 20 62 65 66   that occurs bef
14795 6f 72 65 20 74 68 65 20 70 61 67 65 20 68 65 61  ore the page hea
14796 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  der..**.**      
14797 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  |---------------
14798 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 66 69 6c  -|.**      | fil
14799 65 20 68 65 61 64 65 72 20 20 20 20 7c 20 20 20  e header    |   
1479a 31 30 30 20 62 79 74 65 73 2e 20 20 50 61 67 65  100 bytes.  Page
1479b 20 31 20 6f 6e 6c 79 2e 0a 2a 2a 20 20 20 20 20   1 only..**     
1479c 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   |--------------
1479d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 70 61  --|.**      | pa
1479e 67 65 20 68 65 61 64 65 72 20 20 20 20 7c 20 20  ge header    |  
1479f 20 38 20 62 79 74 65 73 20 66 6f 72 20 6c 65 61   8 bytes for lea
147a0 76 65 73 2e 20 20 31 32 20 62 79 74 65 73 20 66  ves.  12 bytes f
147a1 6f 72 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  or interior node
147a2 73 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d  s.**      |-----
147a3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20  -----------|.** 
147a4 20 20 20 20 20 7c 20 63 65 6c 6c 20 70 6f 69 6e       | cell poin
147a5 74 65 72 20 20 20 7c 20 20 20 7c 20 20 32 20 62  ter   |   |  2 b
147a6 79 74 65 73 20 70 65 72 20 63 65 6c 6c 2e 20 20  ytes per cell.  
147a7 53 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a 2a 2a  Sorted order..**
147a8 20 20 20 20 20 20 7c 20 61 72 72 61 79 20 20 20        | array   
147a9 20 20 20 20 20 20 20 7c 20 20 20 7c 20 20 47 72         |   |  Gr
147aa 6f 77 73 20 64 6f 77 6e 77 61 72 64 0a 2a 2a 20  ows downward.** 
147ab 20 20 20 20 20 7c 20 20 20 20 20 20 20 20 20 20       |          
147ac 20 20 20 20 20 20 7c 20 20 20 76 0a 2a 2a 20 20        |   v.**  
147ad 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d      |-----------
147ae 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c  -----|.**      |
147af 20 75 6e 61 6c 6c 6f 63 61 74 65 64 20 20 20 20   unallocated    
147b0 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 73 70 61 63  |.**      | spac
147b1 65 20 20 20 20 20 20 20 20 20 20 7c 0a 2a 2a 20  e          |.** 
147b2 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d       |----------
147b3 2d 2d 2d 2d 2d 2d 7c 20 20 20 5e 20 20 47 72 6f  ------|   ^  Gro
147b4 77 73 20 75 70 77 61 72 64 73 0a 2a 2a 20 20 20  ws upwards.**   
147b5 20 20 20 7c 20 63 65 6c 6c 20 63 6f 6e 74 65 6e     | cell conten
147b6 74 20 20 20 7c 20 20 20 7c 20 20 41 72 62 69 74  t   |   |  Arbit
147b7 72 61 72 79 20 6f 72 64 65 72 20 69 6e 74 65 72  rary order inter
147b8 73 70 65 72 73 65 64 20 77 69 74 68 20 66 72 65  spersed with fre
147b9 65 62 6c 6f 63 6b 73 2e 0a 2a 2a 20 20 20 20 20  eblocks..**     
147ba 20 7c 20 61 72 65 61 20 20 20 20 20 20 20 20 20   | area         
147bb 20 20 7c 20 20 20 7c 20 20 61 6e 64 20 66 72 65    |   |  and fre
147bc 65 20 73 70 61 63 65 20 66 72 61 67 6d 65 6e 74  e space fragment
147bd 73 2e 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d  s..**      |----
147be 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a  ------------|.**
147bf 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 68 65 61  .** The page hea
147c0 64 65 72 73 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20  ders looks like 
147c1 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 46  this:.**.**   OF
147c2 46 53 45 54 20 20 20 53 49 5a 45 20 20 20 20 20  FSET   SIZE     
147c3 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20  DESCRIPTION.**  
147c4 20 20 20 20 30 20 20 20 20 20 20 20 31 20 20 20      0       1   
147c5 20 20 20 46 6c 61 67 73 2e 20 31 3a 20 69 6e 74     Flags. 1: int
147c6 6b 65 79 2c 20 32 3a 20 7a 65 72 6f 64 61 74 61  key, 2: zerodata
147c7 2c 20 34 3a 20 6c 65 61 66 64 61 74 61 2c 20 38  , 4: leafdata, 8
147c8 3a 20 6c 65 61 66 0a 2a 2a 20 20 20 20 20 20 31  : leaf.**      1
147c9 20 20 20 20 20 20 20 32 20 20 20 20 20 20 62 79         2      by
147ca 74 65 20 6f 66 66 73 65 74 20 74 6f 20 74 68 65  te offset to the
147cb 20 66 69 72 73 74 20 66 72 65 65 62 6c 6f 63 6b   first freeblock
147cc 0a 2a 2a 20 20 20 20 20 20 33 20 20 20 20 20 20  .**      3      
147cd 20 32 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f   2      number o
147ce 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 69 73 20  f cells on this 
147cf 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 35 20 20  page.**      5  
147d0 20 20 20 20 20 32 20 20 20 20 20 20 66 69 72 73       2      firs
147d1 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 63 65  t byte of the ce
147d2 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 0a  ll content area.
147d3 2a 2a 20 20 20 20 20 20 37 20 20 20 20 20 20 20  **      7       
147d4 31 20 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66  1      number of
147d5 20 66 72 61 67 6d 65 6e 74 65 64 20 66 72 65 65   fragmented free
147d6 20 62 79 74 65 73 0a 2a 2a 20 20 20 20 20 20 38   bytes.**      8
147d7 20 20 20 20 20 20 20 34 20 20 20 20 20 20 52 69         4      Ri
147d8 67 68 74 20 63 68 69 6c 64 20 28 74 68 65 20 50  ght child (the P
147d9 74 72 28 4e 29 20 76 61 6c 75 65 29 2e 20 20 4f  tr(N) value).  O
147da 6d 69 74 74 65 64 20 6f 6e 20 6c 65 61 76 65 73  mitted on leaves
147db 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67  ..**.** The flag
147dc 73 20 64 65 66 69 6e 65 20 74 68 65 20 66 6f 72  s define the for
147dd 6d 61 74 20 6f 66 20 74 68 69 73 20 62 74 72 65  mat of this btre
147de 65 20 70 61 67 65 2e 20 20 54 68 65 20 6c 65 61  e page.  The lea
147df 66 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 61  f flag means tha
147e0 74 0a 2a 2a 20 74 68 69 73 20 70 61 67 65 20 68  t.** this page h
147e1 61 73 20 6e 6f 20 63 68 69 6c 64 72 65 6e 2e 20  as no children. 
147e2 20 54 68 65 20 7a 65 72 6f 64 61 74 61 20 66 6c   The zerodata fl
147e3 61 67 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  ag means that th
147e4 69 73 20 70 61 67 65 20 63 61 72 72 69 65 73 0a  is page carries.
147e5 2a 2a 20 6f 6e 6c 79 20 6b 65 79 73 20 61 6e 64  ** only keys and
147e6 20 6e 6f 20 64 61 74 61 2e 20 20 54 68 65 20 69   no data.  The i
147e7 6e 74 6b 65 79 20 66 6c 61 67 20 6d 65 61 6e 73  ntkey flag means
147e8 20 74 68 61 74 20 74 68 65 20 6b 65 79 20 69 73   that the key is
147e9 20 61 20 69 6e 74 65 67 65 72 0a 2a 2a 20 77 68   a integer.** wh
147ea 69 63 68 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ich is stored in
147eb 20 74 68 65 20 6b 65 79 20 73 69 7a 65 20 65 6e   the key size en
147ec 74 72 79 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  try of the cell 
147ed 68 65 61 64 65 72 20 72 61 74 68 65 72 20 74 68  header rather th
147ee 61 6e 20 69 6e 0a 2a 2a 20 74 68 65 20 70 61 79  an in.** the pay
147ef 6c 6f 61 64 20 61 72 65 61 2e 0a 2a 2a 0a 2a 2a  load area..**.**
147f0 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65   The cell pointe
147f1 72 20 61 72 72 61 79 20 62 65 67 69 6e 73 20 6f  r array begins o
147f2 6e 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65  n the first byte
147f3 20 61 66 74 65 72 20 74 68 65 20 70 61 67 65 20   after the page 
147f4 68 65 61 64 65 72 2e 0a 2a 2a 20 54 68 65 20 63  header..** The c
147f5 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61  ell pointer arra
147f6 79 20 63 6f 6e 74 61 69 6e 73 20 7a 65 72 6f 20  y contains zero 
147f7 6f 72 20 6d 6f 72 65 20 32 2d 62 79 74 65 20 6e  or more 2-byte n
147f8 75 6d 62 65 72 73 20 77 68 69 63 68 20 61 72 65  umbers which are
147f9 0a 2a 2a 20 6f 66 66 73 65 74 73 20 66 72 6f 6d  .** offsets from
147fa 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
147fb 66 20 74 68 65 20 70 61 67 65 20 74 6f 20 74 68  f the page to th
147fc 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69  e cell content i
147fd 6e 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 63 6f  n the cell.** co
147fe 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 54 68 65  ntent area.  The
147ff 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 73 20 6f   cell pointers o
14800 63 63 75 72 20 69 6e 20 73 6f 72 74 65 64 20 6f  ccur in sorted o
14801 72 64 65 72 2e 20 20 54 68 65 20 73 79 73 74 65  rder.  The syste
14802 6d 20 73 74 72 69 76 65 73 0a 2a 2a 20 74 6f 20  m strives.** to 
14803 6b 65 65 70 20 66 72 65 65 20 73 70 61 63 65 20  keep free space 
14804 61 66 74 65 72 20 74 68 65 20 6c 61 73 74 20 63  after the last c
14805 65 6c 6c 20 70 6f 69 6e 74 65 72 20 73 6f 20 74  ell pointer so t
14806 68 61 74 20 6e 65 77 20 63 65 6c 6c 73 20 63 61  hat new cells ca
14807 6e 0a 2a 2a 20 62 65 20 65 61 73 69 6c 79 20 61  n.** be easily a
14808 64 64 65 64 20 77 69 74 68 6f 75 74 20 68 61 76  dded without hav
14809 69 6e 67 20 74 6f 20 64 65 66 72 61 67 6d 65 6e  ing to defragmen
1480a 74 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a  t the page..**.*
1480b 2a 20 43 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69  * Cell content i
1480c 73 20 73 74 6f 72 65 64 20 61 74 20 74 68 65 20  s stored at the 
1480d 76 65 72 79 20 65 6e 64 20 6f 66 20 74 68 65 20  very end of the 
1480e 70 61 67 65 20 61 6e 64 20 67 72 6f 77 73 20 74  page and grows t
1480f 6f 77 61 72 64 20 74 68 65 0a 2a 2a 20 62 65 67  oward the.** beg
14810 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61  inning of the pa
14811 67 65 2e 0a 2a 2a 0a 2a 2a 20 55 6e 75 73 65 64  ge..**.** Unused
14812 20 73 70 61 63 65 20 77 69 74 68 69 6e 20 74 68   space within th
14813 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 61  e cell content a
14814 72 65 61 20 69 73 20 63 6f 6c 6c 65 63 74 65 64  rea is collected
14815 20 69 6e 74 6f 20 61 20 6c 69 6e 6b 65 64 20 6c   into a linked l
14816 69 73 74 20 6f 66 0a 2a 2a 20 66 72 65 65 62 6c  ist of.** freebl
14817 6f 63 6b 73 2e 20 20 45 61 63 68 20 66 72 65 65  ocks.  Each free
14818 62 6c 6f 63 6b 20 69 73 20 61 74 20 6c 65 61 73  block is at leas
14819 74 20 34 20 62 79 74 65 73 20 69 6e 20 73 69 7a  t 4 bytes in siz
1481a 65 2e 20 20 54 68 65 20 62 79 74 65 20 6f 66 66  e.  The byte off
1481b 73 65 74 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69  set.** to the fi
1481c 72 73 74 20 66 72 65 65 62 6c 6f 63 6b 20 69 73  rst freeblock is
1481d 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 68 65   given in the he
1481e 61 64 65 72 2e 20 20 46 72 65 65 62 6c 6f 63 6b  ader.  Freeblock
1481f 73 20 6f 63 63 75 72 20 69 6e 0a 2a 2a 20 69 6e  s occur in.** in
14820 63 72 65 61 73 69 6e 67 20 6f 72 64 65 72 2e 20  creasing order. 
14821 20 42 65 63 61 75 73 65 20 61 20 66 72 65 65 62   Because a freeb
14822 6c 6f 63 6b 20 6d 75 73 74 20 62 65 20 61 74 20  lock must be at 
14823 6c 65 61 73 74 20 34 20 62 79 74 65 73 20 69 6e  least 4 bytes in
14824 20 73 69 7a 65 2c 0a 2a 2a 20 61 6e 79 20 67 72   size,.** any gr
14825 6f 75 70 20 6f 66 20 33 20 6f 72 20 66 65 77 65  oup of 3 or fewe
14826 72 20 75 6e 75 73 65 64 20 62 79 74 65 73 20 69  r unused bytes i
14827 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65  n the cell conte
14828 6e 74 20 61 72 65 61 20 63 61 6e 6e 6f 74 0a 2a  nt area cannot.*
14829 2a 20 65 78 69 73 74 20 6f 6e 20 74 68 65 20 66  * exist on the f
1482a 72 65 65 62 6c 6f 63 6b 20 63 68 61 69 6e 2e 20  reeblock chain. 
1482b 20 41 20 67 72 6f 75 70 20 6f 66 20 33 20 6f 72   A group of 3 or
1482c 20 66 65 77 65 72 20 66 72 65 65 20 62 79 74 65   fewer free byte
1482d 73 20 69 73 20 63 61 6c 6c 65 64 0a 2a 2a 20 61  s is called.** a
1482e 20 66 72 61 67 6d 65 6e 74 2e 20 20 54 68 65 20   fragment.  The 
1482f 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
14830 62 79 74 65 73 20 69 6e 20 61 6c 6c 20 66 72 61  bytes in all fra
14831 67 6d 65 6e 74 73 20 69 73 20 72 65 63 6f 72 64  gments is record
14832 65 64 2e 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61  ed..** in the pa
14833 67 65 20 68 65 61 64 65 72 20 61 74 20 6f 66 66  ge header at off
14834 73 65 74 20 37 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  set 7..**.**    
14835 53 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54  SIZE    DESCRIPT
14836 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 32 20 20 20  ION.**      2   
14837 20 20 42 79 74 65 20 6f 66 66 73 65 74 20 6f 66    Byte offset of
14838 20 74 68 65 20 6e 65 78 74 20 66 72 65 65 62 6c   the next freebl
14839 6f 63 6b 0a 2a 2a 20 20 20 20 20 20 32 20 20 20  ock.**      2   
1483a 20 20 42 79 74 65 73 20 69 6e 20 74 68 69 73 20    Bytes in this 
1483b 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 0a 2a 2a 20  freeblock.**.** 
1483c 43 65 6c 6c 73 20 61 72 65 20 6f 66 20 76 61 72  Cells are of var
1483d 69 61 62 6c 65 20 6c 65 6e 67 74 68 2e 20 20 43  iable length.  C
1483e 65 6c 6c 73 20 61 72 65 20 73 74 6f 72 65 64 20  ells are stored 
1483f 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  in the cell cont
14840 65 6e 74 20 61 72 65 61 20 61 74 0a 2a 2a 20 74  ent area at.** t
14841 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 61  he end of the pa
14842 67 65 2e 20 20 50 6f 69 6e 74 65 72 73 20 74 6f  ge.  Pointers to
14843 20 74 68 65 20 63 65 6c 6c 73 20 61 72 65 20 69   the cells are i
14844 6e 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74  n the cell point
14845 65 72 20 61 72 72 61 79 0a 2a 2a 20 74 68 61 74  er array.** that
14846 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c   immediately fol
14847 6c 6f 77 73 20 74 68 65 20 70 61 67 65 20 68 65  lows the page he
14848 61 64 65 72 2e 20 20 43 65 6c 6c 73 20 69 73 20  ader.  Cells is 
14849 6e 6f 74 20 6e 65 63 65 73 73 61 72 69 6c 79 0a  not necessarily.
1484a 2a 2a 20 63 6f 6e 74 69 67 75 6f 75 73 20 6f 72  ** contiguous or
1484b 20 69 6e 20 6f 72 64 65 72 2c 20 62 75 74 20 63   in order, but c
1484c 65 6c 6c 20 70 6f 69 6e 74 65 72 73 20 61 72 65  ell pointers are
1484d 20 63 6f 6e 74 69 67 75 6f 75 73 20 61 6e 64 20   contiguous and 
1484e 69 6e 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a 20  in order..**.** 
1484f 43 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 6d 61 6b  Cell content mak
14850 65 73 20 75 73 65 20 6f 66 20 76 61 72 69 61 62  es use of variab
14851 6c 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65  le length intege
14852 72 73 2e 20 20 41 20 76 61 72 69 61 62 6c 65 0a  rs.  A variable.
14853 2a 2a 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65  ** length intege
14854 72 20 69 73 20 31 20 74 6f 20 39 20 62 79 74 65  r is 1 to 9 byte
14855 73 20 77 68 65 72 65 20 74 68 65 20 6c 6f 77 65  s where the lowe
14856 72 20 37 20 62 69 74 73 20 6f 66 20 65 61 63 68  r 7 bits of each
14857 20 0a 2a 2a 20 62 79 74 65 20 61 72 65 20 75 73   .** byte are us
14858 65 64 2e 20 20 54 68 65 20 69 6e 74 65 67 65 72  ed.  The integer
14859 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 6c 6c   consists of all
1485a 20 62 79 74 65 73 20 74 68 61 74 20 68 61 76 65   bytes that have
1485b 20 62 69 74 20 38 20 73 65 74 20 61 6e 64 0a 2a   bit 8 set and.*
1485c 2a 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65  * the first byte
1485d 20 77 69 74 68 20 62 69 74 20 38 20 63 6c 65 61   with bit 8 clea
1485e 72 2e 20 20 54 68 65 20 6d 6f 73 74 20 73 69 67  r.  The most sig
1485f 6e 69 66 69 63 61 6e 74 20 62 79 74 65 20 6f 66  nificant byte of
14860 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20   the integer.** 
14861 61 70 70 65 61 72 73 20 66 69 72 73 74 2e 20 20  appears first.  
14862 41 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  A variable-lengt
14863 68 20 69 6e 74 65 67 65 72 20 6d 61 79 20 6e 6f  h integer may no
14864 74 20 62 65 20 6d 6f 72 65 20 74 68 61 6e 20 39  t be more than 9
14865 20 62 79 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2a 20   bytes long..** 
14866 41 73 20 61 20 73 70 65 63 69 61 6c 20 63 61 73  As a special cas
14867 65 2c 20 61 6c 6c 20 38 20 62 79 74 65 73 20 6f  e, all 8 bytes o
14868 66 20 74 68 65 20 39 74 68 20 62 79 74 65 20 61  f the 9th byte a
14869 72 65 20 75 73 65 64 20 61 73 20 64 61 74 61 2e  re used as data.
1486a 20 20 54 68 69 73 0a 2a 2a 20 61 6c 6c 6f 77 73    This.** allows
1486b 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65   a 64-bit intege
1486c 72 20 74 6f 20 62 65 20 65 6e 63 6f 64 65 64 20  r to be encoded 
1486d 69 6e 20 39 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a  in 9 bytes..**.*
1486e 2a 20 20 20 20 30 78 30 30 20 20 20 20 20 20 20  *    0x00       
1486f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
14870 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 30  ecomes  0x000000
14871 30 30 0a 2a 2a 20 20 20 20 30 78 37 66 20 20 20  00.**    0x7f   
14872 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14873 20 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30     becomes  0x00
14874 30 30 30 30 37 66 0a 2a 2a 20 20 20 20 30 78 38  00007f.**    0x8
14875 31 20 30 78 30 30 20 20 20 20 20 20 20 20 20 20  1 0x00          
14876 20 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20         becomes  
14877 30 78 30 30 30 30 30 30 38 30 0a 2a 2a 20 20 20  0x00000080.**   
14878 20 30 78 38 32 20 30 78 30 30 20 20 20 20 20 20   0x82 0x00      
14879 20 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d             becom
1487a 65 73 20 20 30 78 30 30 30 30 30 31 30 30 0a 2a  es  0x00000100.*
1487b 2a 20 20 20 20 30 78 38 30 20 30 78 37 66 20 20  *    0x80 0x7f  
1487c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
1487d 65 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 30  ecomes  0x000000
1487e 37 66 0a 2a 2a 20 20 20 20 30 78 38 61 20 30 78  7f.**    0x8a 0x
1487f 39 31 20 30 78 64 31 20 30 78 61 63 20 30 78 37  91 0xd1 0xac 0x7
14880 38 20 20 62 65 63 6f 6d 65 73 20 20 30 78 31 32  8  becomes  0x12
14881 33 34 35 36 37 38 0a 2a 2a 20 20 20 20 30 78 38  345678.**    0x8
14882 31 20 30 78 38 31 20 30 78 38 31 20 30 78 38 31  1 0x81 0x81 0x81
14883 20 30 78 30 31 20 20 62 65 63 6f 6d 65 73 20 20   0x01  becomes  
14884 30 78 31 30 32 30 34 30 38 31 0a 2a 2a 0a 2a 2a  0x10204081.**.**
14885 20 56 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68   Variable length
14886 20 69 6e 74 65 67 65 72 73 20 61 72 65 20 75 73   integers are us
14887 65 64 20 66 6f 72 20 72 6f 77 69 64 73 20 61 6e  ed for rowids an
14888 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6e 75  d to hold the nu
14889 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73  mber of.** bytes
1488a 20 6f 66 20 6b 65 79 20 61 6e 64 20 64 61 74 61   of key and data
1488b 20 69 6e 20 61 20 62 74 72 65 65 20 63 65 6c 6c   in a btree cell
1488c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74  ..**.** The cont
1488d 65 6e 74 20 6f 66 20 61 20 63 65 6c 6c 20 6c 6f  ent of a cell lo
1488e 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a  oks like this:.*
1488f 2a 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 20  *.**    SIZE    
14890 44 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20  DESCRIPTION.**  
14891 20 20 20 20 34 20 20 20 20 20 50 61 67 65 20 6e      4     Page n
14892 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6c 65 66  umber of the lef
14893 74 20 63 68 69 6c 64 2e 20 4f 6d 69 74 74 65 64  t child. Omitted
14894 20 69 66 20 6c 65 61 66 20 66 6c 61 67 20 69 73   if leaf flag is
14895 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 76 61 72   set..**     var
14896 20 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 62 79      Number of by
14897 74 65 73 20 6f 66 20 64 61 74 61 2e 20 4f 6d 69  tes of data. Omi
14898 74 74 65 64 20 69 66 20 74 68 65 20 7a 65 72 6f  tted if the zero
14899 64 61 74 61 20 66 6c 61 67 20 69 73 20 73 65 74  data flag is set
1489a 2e 0a 2a 2a 20 20 20 20 20 76 61 72 20 20 20 20  ..**     var    
1489b 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
1489c 6f 66 20 6b 65 79 2e 20 4f 72 20 74 68 65 20 6b  of key. Or the k
1489d 65 79 20 69 74 73 65 6c 66 20 69 66 20 69 6e 74  ey itself if int
1489e 6b 65 79 20 66 6c 61 67 20 69 73 20 73 65 74 2e  key flag is set.
1489f 0a 2a 2a 20 20 20 20 20 20 2a 20 20 20 20 20 50  .**      *     P
148a0 61 79 6c 6f 61 64 0a 2a 2a 20 20 20 20 20 20 34  ayload.**      4
148a1 20 20 20 20 20 46 69 72 73 74 20 70 61 67 65 20       First page 
148a2 6f 66 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  of the overflow 
148a3 63 68 61 69 6e 2e 20 20 4f 6d 69 74 74 65 64 20  chain.  Omitted 
148a4 69 66 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a 2a  if no overflow.*
148a5 2a 0a 2a 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61  *.** Overflow pa
148a6 67 65 73 20 66 6f 72 6d 20 61 20 6c 69 6e 6b 65  ges form a linke
148a7 64 20 6c 69 73 74 2e 20 20 45 61 63 68 20 70 61  d list.  Each pa
148a8 67 65 20 65 78 63 65 70 74 20 74 68 65 20 6c 61  ge except the la
148a9 73 74 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79  st is completely
148aa 0a 2a 2a 20 66 69 6c 6c 65 64 20 77 69 74 68 20  .** filled with 
148ab 64 61 74 61 20 28 70 61 67 65 73 69 7a 65 20 2d  data (pagesize -
148ac 20 34 20 62 79 74 65 73 29 2e 20 20 54 68 65 20   4 bytes).  The 
148ad 6c 61 73 74 20 70 61 67 65 20 63 61 6e 20 68 61  last page can ha
148ae 76 65 20 61 73 20 6c 69 74 74 6c 65 0a 2a 2a 20  ve as little.** 
148af 61 73 20 31 20 62 79 74 65 20 6f 66 20 64 61 74  as 1 byte of dat
148b0 61 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45  a..**.**    SIZE
148b1 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a      DESCRIPTION.
148b2 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 50 61  **      4     Pa
148b3 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 78  ge number of nex
148b4 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 0a  t overflow page.
148b5 2a 2a 20 20 20 20 20 20 2a 20 20 20 20 20 44 61  **      *     Da
148b6 74 61 0a 2a 2a 0a 2a 2a 20 46 72 65 65 6c 69 73  ta.**.** Freelis
148b7 74 20 70 61 67 65 73 20 63 6f 6d 65 20 69 6e 20  t pages come in 
148b8 74 77 6f 20 73 75 62 74 79 70 65 73 3a 20 74 72  two subtypes: tr
148b9 75 6e 6b 20 70 61 67 65 73 20 61 6e 64 20 6c 65  unk pages and le
148ba 61 66 20 70 61 67 65 73 2e 20 20 54 68 65 0a 2a  af pages.  The.*
148bb 2a 20 66 69 6c 65 20 68 65 61 64 65 72 20 70 6f  * file header po
148bc 69 6e 74 73 20 74 6f 20 74 68 65 20 66 69 72 73  ints to the firs
148bd 74 20 69 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69  t in a linked li
148be 73 74 20 6f 66 20 74 72 75 6e 6b 20 70 61 67 65  st of trunk page
148bf 2e 20 20 45 61 63 68 20 74 72 75 6e 6b 0a 2a 2a  .  Each trunk.**
148c0 20 70 61 67 65 20 70 6f 69 6e 74 73 20 74 6f 20   page points to 
148c1 6d 75 6c 74 69 70 6c 65 20 6c 65 61 66 20 70 61  multiple leaf pa
148c2 67 65 73 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e  ges.  The conten
148c3 74 20 6f 66 20 61 20 6c 65 61 66 20 70 61 67 65  t of a leaf page
148c4 20 69 73 0a 2a 2a 20 75 6e 73 70 65 63 69 66 69   is.** unspecifi
148c5 65 64 2e 20 20 41 20 74 72 75 6e 6b 20 70 61 67  ed.  A trunk pag
148c6 65 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69  e looks like thi
148c7 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 49 5a 45  s:.**.**    SIZE
148c8 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e 0a      DESCRIPTION.
148c9 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20 50 61  **      4     Pa
148ca 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 6e 65 78  ge number of nex
148cb 74 20 74 72 75 6e 6b 20 70 61 67 65 0a 2a 2a 20  t trunk page.** 
148cc 20 20 20 20 20 34 20 20 20 20 20 4e 75 6d 62 65       4     Numbe
148cd 72 20 6f 66 20 6c 65 61 66 20 70 6f 69 6e 74 65  r of leaf pointe
148ce 72 73 20 6f 6e 20 74 68 69 73 20 70 61 67 65 0a  rs on this page.
148cf 2a 2a 20 20 20 20 20 20 2a 20 20 20 20 20 7a 65  **      *     ze
148d0 72 6f 20 6f 72 20 6d 6f 72 65 20 70 61 67 65 73  ro or more pages
148d1 20 6e 75 6d 62 65 72 73 20 6f 66 20 6c 65 61 76   numbers of leav
148d2 65 73 0a 2a 2f 0a 0a 0a 2f 2a 20 54 68 65 20 66  es.*/.../* The f
148d3 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 20 69  ollowing value i
148d4 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 63 65  s the maximum ce
148d5 6c 6c 20 73 69 7a 65 20 61 73 73 75 6d 69 6e 67  ll size assuming
148d6 20 61 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 0a   a maximum page.
148d7 2a 2a 20 73 69 7a 65 20 67 69 76 65 20 61 62 6f  ** size give abo
148d8 76 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  ve..*/.#define M
148d9 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29  X_CELL_SIZE(pBt)
148da 20 20 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65    (pBt->pageSize
148db 2d 38 29 0a 0a 2f 2a 20 54 68 65 20 6d 61 78 69  -8)../* The maxi
148dc 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 65  mum number of ce
148dd 6c 6c 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20  lls on a single 
148de 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61  page of the data
148df 62 61 73 65 2e 20 20 54 68 69 73 0a 2a 2a 20 61  base.  This.** a
148e0 73 73 75 6d 65 73 20 61 20 6d 69 6e 69 6d 75 6d  ssumes a minimum
148e1 20 63 65 6c 6c 20 73 69 7a 65 20 6f 66 20 36 20   cell size of 6 
148e2 62 79 74 65 73 20 20 28 34 20 62 79 74 65 73 20  bytes  (4 bytes 
148e3 66 6f 72 20 74 68 65 20 63 65 6c 6c 20 69 74 73  for the cell its
148e4 65 6c 66 0a 2a 2a 20 70 6c 75 73 20 32 20 62 79  elf.** plus 2 by
148e5 74 65 73 20 66 6f 72 20 74 68 65 20 69 6e 64 65  tes for the inde
148e6 78 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 69 6e  x to the cell in
148e7 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72   the page header
148e8 29 2e 20 20 53 75 63 68 0a 2a 2a 20 73 6d 61 6c  ).  Such.** smal
148e9 6c 20 63 65 6c 6c 73 20 77 69 6c 6c 20 62 65 20  l cells will be 
148ea 72 61 72 65 2c 20 62 75 74 20 74 68 65 79 20 61  rare, but they a
148eb 72 65 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a  re possible..*/.
148ec 23 64 65 66 69 6e 65 20 4d 58 5f 43 45 4c 4c 28  #define MX_CELL(
148ed 70 42 74 29 20 28 28 70 42 74 2d 3e 70 61 67 65  pBt) ((pBt->page
148ee 53 69 7a 65 2d 38 29 2f 36 29 0a 0a 2f 2a 20 46  Size-8)/6)../* F
148ef 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
148f0 6f 6e 73 20 2a 2f 0a 74 79 70 65 64 65 66 20 73  ons */.typedef s
148f1 74 72 75 63 74 20 4d 65 6d 50 61 67 65 20 4d 65  truct MemPage Me
148f2 6d 50 61 67 65 3b 0a 74 79 70 65 64 65 66 20 73  mPage;.typedef s
148f3 74 72 75 63 74 20 42 74 4c 6f 63 6b 20 42 74 4c  truct BtLock BtL
148f4 6f 63 6b 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ock;../*.** This
148f5 20 69 73 20 61 20 6d 61 67 69 63 20 73 74 72 69   is a magic stri
148f6 6e 67 20 74 68 61 74 20 61 70 70 65 61 72 73 20  ng that appears 
148f7 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
148f8 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20 53 51 4c   of every.** SQL
148f9 69 74 65 20 64 61 74 61 62 61 73 65 20 69 6e 20  ite database in 
148fa 6f 72 64 65 72 20 74 6f 20 69 64 65 6e 74 69 66  order to identif
148fb 79 20 74 68 65 20 66 69 6c 65 20 61 73 20 61 20  y the file as a 
148fc 72 65 61 6c 20 64 61 74 61 62 61 73 65 2e 0a 2a  real database..*
148fd 2a 0a 2a 2a 20 59 6f 75 20 63 61 6e 20 63 68 61  *.** You can cha
148fe 6e 67 65 20 74 68 69 73 20 76 61 6c 75 65 20 61  nge this value a
148ff 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 62  t compile-time b
14900 79 20 73 70 65 63 69 66 79 69 6e 67 20 61 0a 2a  y specifying a.*
14901 2a 20 2d 44 53 51 4c 49 54 45 5f 46 49 4c 45 5f  * -DSQLITE_FILE_
14902 48 45 41 44 45 52 3d 22 2e 2e 2e 22 20 6f 6e 20  HEADER="..." on 
14903 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d  the compiler com
14904 6d 61 6e 64 2d 6c 69 6e 65 2e 20 20 54 68 65 0a  mand-line.  The.
14905 2a 2a 20 68 65 61 64 65 72 20 6d 75 73 74 20 62  ** header must b
14906 65 20 65 78 61 63 74 6c 79 20 31 36 20 62 79 74  e exactly 16 byt
14907 65 73 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65  es including the
14908 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 6f 72   zero-terminator
14909 20 73 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e   so.** the strin
1490a 67 20 69 74 73 65 6c 66 20 73 68 6f 75 6c 64 20  g itself should 
1490b 62 65 20 31 35 20 63 68 61 72 61 63 74 65 72 73  be 15 characters
1490c 20 6c 6f 6e 67 2e 20 20 49 66 20 79 6f 75 20 63   long.  If you c
1490d 68 61 6e 67 65 0a 2a 2a 20 74 68 65 20 68 65 61  hange.** the hea
1490e 64 65 72 2c 20 74 68 65 6e 20 79 6f 75 72 20 63  der, then your c
1490f 75 73 74 6f 6d 20 6c 69 62 72 61 72 79 20 77 69  ustom library wi
14910 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74  ll not be able t
14911 6f 20 72 65 61 64 20 0a 2a 2a 20 64 61 74 61 62  o read .** datab
14912 61 73 65 73 20 67 65 6e 65 72 61 74 65 64 20 62  ases generated b
14913 79 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 74  y the standard t
14914 6f 6f 6c 73 20 61 6e 64 20 74 68 65 20 73 74 61  ools and the sta
14915 6e 64 61 72 64 20 74 6f 6f 6c 73 0a 2a 2a 20 77  ndard tools.** w
14916 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20  ill not be able 
14917 74 6f 20 72 65 61 64 20 64 61 74 61 62 61 73 65  to read database
14918 73 20 63 72 65 61 74 65 64 20 62 79 20 79 6f 75  s created by you
14919 72 20 63 75 73 74 6f 6d 20 6c 69 62 72 61 72 79  r custom library
1491a 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
1491b 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 20  ITE_FILE_HEADER 
1491c 2f 2a 20 31 32 33 34 35 36 37 38 39 20 31 32 33  /* 123456789 123
1491d 34 35 36 20 2a 2f 0a 23 20 20 64 65 66 69 6e 65  456 */.#  define
1491e 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41   SQLITE_FILE_HEA
1491f 44 45 52 20 22 53 51 4c 69 74 65 20 66 6f 72 6d  DER "SQLite form
14920 61 74 20 33 22 0a 23 65 6e 64 69 66 0a 0a 2f 2a  at 3".#endif../*
14921 0a 2a 2a 20 50 61 67 65 20 74 79 70 65 20 66 6c  .** Page type fl
14922 61 67 73 2e 20 20 41 6e 20 4f 52 65 64 20 63 6f  ags.  An ORed co
14923 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65  mbination of the
14924 73 65 20 66 6c 61 67 73 20 61 70 70 65 61 72 20  se flags appear 
14925 61 73 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20  as the.** first 
14926 62 79 74 65 20 6f 66 20 6f 6e 2d 64 69 73 6b 20  byte of on-disk 
14927 69 6d 61 67 65 20 6f 66 20 65 76 65 72 79 20 42  image of every B
14928 54 72 65 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 64  Tree page..*/.#d
14929 65 66 69 6e 65 20 50 54 46 5f 49 4e 54 4b 45 59  efine PTF_INTKEY
1492a 20 20 20 20 30 78 30 31 0a 23 64 65 66 69 6e 65      0x01.#define
1492b 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 20 20 30   PTF_ZERODATA  0
1492c 78 30 32 0a 23 64 65 66 69 6e 65 20 50 54 46 5f  x02.#define PTF_
1492d 4c 45 41 46 44 41 54 41 20 20 30 78 30 34 0a 23  LEAFDATA  0x04.#
1492e 64 65 66 69 6e 65 20 50 54 46 5f 4c 45 41 46 20  define PTF_LEAF 
1492f 20 20 20 20 20 30 78 30 38 0a 0a 2f 2a 0a 2a 2a       0x08../*.**
14930 20 41 73 20 65 61 63 68 20 70 61 67 65 20 6f 66   As each page of
14931 20 74 68 65 20 66 69 6c 65 20 69 73 20 6c 6f 61   the file is loa
14932 64 65 64 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 2c  ded into memory,
14933 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
14934 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a  the following.**
14935 20 73 74 72 75 63 74 75 72 65 20 69 73 20 61 70   structure is ap
14936 70 65 6e 64 65 64 20 61 6e 64 20 69 6e 69 74 69  pended and initi
14937 61 6c 69 7a 65 64 20 74 6f 20 7a 65 72 6f 2e 20  alized to zero. 
14938 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
14939 73 74 6f 72 65 73 0a 2a 2a 20 69 6e 66 6f 72 6d  stores.** inform
1493a 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20  ation about the 
1493b 70 61 67 65 20 74 68 61 74 20 69 73 20 64 65 63  page that is dec
1493c 6f 64 65 64 20 66 72 6f 6d 20 74 68 65 20 72 61  oded from the ra
1493d 77 20 66 69 6c 65 20 70 61 67 65 2e 0a 2a 2a 0a  w file page..**.
1493e 2a 2a 20 54 68 65 20 70 50 61 72 65 6e 74 20 66  ** The pParent f
1493f 69 65 6c 64 20 70 6f 69 6e 74 73 20 62 61 63 6b  ield points back
14940 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20 70   to the parent p
14941 61 67 65 2e 20 20 54 68 69 73 20 61 6c 6c 6f 77  age.  This allow
14942 73 20 75 73 20 74 6f 0a 2a 2a 20 77 61 6c 6b 20  s us to.** walk 
14943 75 70 20 74 68 65 20 42 54 72 65 65 20 66 72 6f  up the BTree fro
14944 6d 20 61 6e 79 20 6c 65 61 66 20 74 6f 20 74 68  m any leaf to th
14945 65 20 72 6f 6f 74 2e 20 20 43 61 72 65 20 6d 75  e root.  Care mu
14946 73 74 20 62 65 20 74 61 6b 65 6e 20 74 6f 0a 2a  st be taken to.*
14947 2a 20 75 6e 72 65 66 28 29 20 74 68 65 20 70 61  * unref() the pa
14948 72 65 6e 74 20 70 61 67 65 20 70 6f 69 6e 74 65  rent page pointe
14949 72 20 77 68 65 6e 20 74 68 69 73 20 70 61 67 65  r when this page
1494a 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 72 65   is no longer re
1494b 66 65 72 65 6e 63 65 64 2e 0a 2a 2a 20 54 68 65  ferenced..** The
1494c 20 70 61 67 65 44 65 73 74 72 75 63 74 6f 72 28   pageDestructor(
1494d 29 20 72 6f 75 74 69 6e 65 20 68 61 6e 64 6c 65  ) routine handle
1494e 73 20 74 68 61 74 20 63 68 6f 72 65 2e 0a 2a 2a  s that chore..**
1494f 0a 2a 2a 20 41 63 63 65 73 73 20 74 6f 20 61 6c  .** Access to al
14950 6c 20 66 69 65 6c 64 73 20 6f 66 20 74 68 69 73  l fields of this
14951 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63 6f   structure is co
14952 6e 74 72 6f 6c 6c 65 64 20 62 79 20 74 68 65 20  ntrolled by the 
14953 6d 75 74 65 78 0a 2a 2a 20 73 74 6f 72 65 64 20  mutex.** stored 
14954 69 6e 20 4d 65 6d 50 61 67 65 2e 70 42 74 2d 3e  in MemPage.pBt->
14955 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74  mutex..*/.struct
14956 20 4d 65 6d 50 61 67 65 20 7b 0a 20 20 75 38 20   MemPage {.  u8 
14957 69 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20 20  isInit;         
14958 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70 72 65    /* True if pre
14959 76 69 6f 75 73 6c 79 20 69 6e 69 74 69 61 6c 69  viously initiali
1495a 7a 65 64 2e 20 4d 55 53 54 20 42 45 20 46 49 52  zed. MUST BE FIR
1495b 53 54 21 20 2a 2f 0a 20 20 75 38 20 6e 4f 76 65  ST! */.  u8 nOve
1495c 72 66 6c 6f 77 3b 20 20 20 20 20 20 20 20 2f 2a  rflow;        /*
1495d 20 4e 75 6d 62 65 72 20 6f 66 20 6f 76 65 72 66   Number of overf
1495e 6c 6f 77 20 63 65 6c 6c 20 62 6f 64 69 65 73 20  low cell bodies 
1495f 69 6e 20 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20  in aCell[] */.  
14960 75 38 20 69 6e 74 4b 65 79 3b 20 20 20 20 20 20  u8 intKey;      
14961 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
14962 69 6e 74 6b 65 79 20 66 6c 61 67 20 69 73 20 73  intkey flag is s
14963 65 74 20 2a 2f 0a 20 20 75 38 20 6c 65 61 66 3b  et */.  u8 leaf;
14964 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
14965 54 72 75 65 20 69 66 20 6c 65 61 66 20 66 6c 61  True if leaf fla
14966 67 20 69 73 20 73 65 74 20 2a 2f 0a 20 20 75 38  g is set */.  u8
14967 20 68 61 73 44 61 74 61 3b 20 20 20 20 20 20 20   hasData;       
14968 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
14969 69 73 20 70 61 67 65 20 73 74 6f 72 65 73 20 64  is page stores d
1496a 61 74 61 20 2a 2f 0a 20 20 75 38 20 68 64 72 4f  ata */.  u8 hdrO
1496b 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 2f 2a  ffset;        /*
1496c 20 31 30 30 20 66 6f 72 20 70 61 67 65 20 31 2e   100 for page 1.
1496d 20 20 30 20 6f 74 68 65 72 77 69 73 65 20 2a 2f    0 otherwise */
1496e 0a 20 20 75 38 20 63 68 69 6c 64 50 74 72 53 69  .  u8 childPtrSi
1496f 7a 65 3b 20 20 20 20 20 2f 2a 20 30 20 69 66 20  ze;     /* 0 if 
14970 6c 65 61 66 3d 3d 31 2e 20 20 34 20 69 66 20 6c  leaf==1.  4 if l
14971 65 61 66 3d 3d 30 20 2a 2f 0a 20 20 75 31 36 20  eaf==0 */.  u16 
14972 6d 61 78 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20  maxLocal;       
14973 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42 74 53 68   /* Copy of BtSh
14974 61 72 65 64 2e 6d 61 78 4c 6f 63 61 6c 20 6f 72  ared.maxLocal or
14975 20 42 74 53 68 61 72 65 64 2e 6d 61 78 4c 65 61   BtShared.maxLea
14976 66 20 2a 2f 0a 20 20 75 31 36 20 6d 69 6e 4c 6f  f */.  u16 minLo
14977 63 61 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 43  cal;        /* C
14978 6f 70 79 20 6f 66 20 42 74 53 68 61 72 65 64 2e  opy of BtShared.
14979 6d 69 6e 4c 6f 63 61 6c 20 6f 72 20 42 74 53 68  minLocal or BtSh
1497a 61 72 65 64 2e 6d 69 6e 4c 65 61 66 20 2a 2f 0a  ared.minLeaf */.
1497b 20 20 75 31 36 20 63 65 6c 6c 4f 66 66 73 65 74    u16 cellOffset
1497c 3b 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20  ;      /* Index 
1497d 69 6e 20 61 44 61 74 61 20 6f 66 20 66 69 72 73  in aData of firs
1497e 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a  t cell pointer *
1497f 2f 0a 20 20 75 31 36 20 6e 46 72 65 65 3b 20 20  /.  u16 nFree;  
14980 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
14981 65 72 20 6f 66 20 66 72 65 65 20 62 79 74 65 73  er of free bytes
14982 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a   on the page */.
14983 20 20 75 31 36 20 6e 43 65 6c 6c 3b 20 20 20 20    u16 nCell;    
14984 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
14985 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 69   of cells on thi
14986 73 20 70 61 67 65 2c 20 6c 6f 63 61 6c 20 61 6e  s page, local an
14987 64 20 6f 76 66 6c 20 2a 2f 0a 20 20 75 31 36 20  d ovfl */.  u16 
14988 6d 61 73 6b 50 61 67 65 3b 20 20 20 20 20 20 20  maskPage;       
14989 20 2f 2a 20 4d 61 73 6b 20 66 6f 72 20 70 61 67   /* Mask for pag
1498a 65 20 6f 66 66 73 65 74 20 2a 2f 0a 20 20 73 74  e offset */.  st
1498b 72 75 63 74 20 5f 4f 76 66 6c 43 65 6c 6c 20 7b  ruct _OvflCell {
1498c 20 20 20 2f 2a 20 43 65 6c 6c 73 20 74 68 61 74     /* Cells that
1498d 20 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 6f 6e   will not fit on
1498e 20 61 44 61 74 61 5b 5d 20 2a 2f 0a 20 20 20 20   aData[] */.    
1498f 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20 20  u8 *pCell;      
14990 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 73 20      /* Pointers 
14991 74 6f 20 74 68 65 20 62 6f 64 79 20 6f 66 20 74  to the body of t
14992 68 65 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c  he overflow cell
14993 20 2a 2f 0a 20 20 20 20 75 31 36 20 69 64 78 3b   */.    u16 idx;
14994 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
14995 6e 73 65 72 74 20 74 68 69 73 20 63 65 6c 6c 20  nsert this cell 
14996 62 65 66 6f 72 65 20 69 64 78 2d 74 68 20 6e 6f  before idx-th no
14997 6e 2d 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20  n-overflow cell 
14998 2a 2f 0a 20 20 7d 20 61 4f 76 66 6c 5b 35 5d 3b  */.  } aOvfl[5];
14999 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
1499a 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74  ;       /* Point
1499b 65 72 20 74 6f 20 42 74 53 68 61 72 65 64 20 74  er to BtShared t
1499c 68 61 74 20 74 68 69 73 20 70 61 67 65 20 69 73  hat this page is
1499d 20 70 61 72 74 20 6f 66 20 2a 2f 0a 20 20 75 38   part of */.  u8
1499e 20 2a 61 44 61 74 61 3b 20 20 20 20 20 20 20 20   *aData;        
1499f 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
149a0 20 64 69 73 6b 20 69 6d 61 67 65 20 6f 66 20 74   disk image of t
149a1 68 65 20 70 61 67 65 20 64 61 74 61 20 2a 2f 0a  he page data */.
149a2 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67    DbPage *pDbPag
149a3 65 3b 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20  e;     /* Pager 
149a4 70 61 67 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20  page handle */. 
149a5 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20 20 20   Pgno pgno;     
149a6 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75        /* Page nu
149a7 6d 62 65 72 20 66 6f 72 20 74 68 69 73 20 70 61  mber for this pa
149a8 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ge */.};../*.** 
149a9 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 69 6d  The in-memory im
149aa 61 67 65 20 6f 66 20 61 20 64 69 73 6b 20 70 61  age of a disk pa
149ab 67 65 20 68 61 73 20 74 68 65 20 61 75 78 69 6c  ge has the auxil
149ac 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  iary information
149ad 20 61 70 70 65 6e 64 65 64 0a 2a 2a 20 74 6f 20   appended.** to 
149ae 74 68 65 20 65 6e 64 2e 20 20 45 58 54 52 41 5f  the end.  EXTRA_
149af 53 49 5a 45 20 69 73 20 74 68 65 20 6e 75 6d 62  SIZE is the numb
149b0 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 73  er of bytes of s
149b1 70 61 63 65 20 6e 65 65 64 65 64 20 74 6f 20 68  pace needed to h
149b2 6f 6c 64 0a 2a 2a 20 74 68 61 74 20 65 78 74 72  old.** that extr
149b3 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a  a information..*
149b4 2f 0a 23 64 65 66 69 6e 65 20 45 58 54 52 41 5f  /.#define EXTRA_
149b5 53 49 5a 45 20 73 69 7a 65 6f 66 28 4d 65 6d 50  SIZE sizeof(MemP
149b6 61 67 65 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69  age)../*.** A li
149b7 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65  nked list of the
149b8 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
149b9 74 75 72 65 73 20 69 73 20 73 74 6f 72 65 64 20  tures is stored 
149ba 61 74 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63  at BtShared.pLoc
149bb 6b 2e 0a 2a 2a 20 4c 6f 63 6b 73 20 61 72 65 20  k..** Locks are 
149bc 61 64 64 65 64 20 28 6f 72 20 75 70 67 72 61 64  added (or upgrad
149bd 65 64 20 66 72 6f 6d 20 52 45 41 44 5f 4c 4f 43  ed from READ_LOC
149be 4b 20 74 6f 20 57 52 49 54 45 5f 4c 4f 43 4b 29  K to WRITE_LOCK)
149bf 20 77 68 65 6e 20 61 20 63 75 72 73 6f 72 20 0a   when a cursor .
149c0 2a 2a 20 69 73 20 6f 70 65 6e 65 64 20 6f 6e 20  ** is opened on 
149c1 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72  the table with r
149c2 6f 6f 74 20 70 61 67 65 20 42 74 53 68 61 72 65  oot page BtShare
149c3 64 2e 69 54 61 62 6c 65 2e 20 4c 6f 63 6b 73 20  d.iTable. Locks 
149c4 61 72 65 20 72 65 6d 6f 76 65 64 0a 2a 2a 20 66  are removed.** f
149c5 72 6f 6d 20 74 68 69 73 20 6c 69 73 74 20 77 68  rom this list wh
149c6 65 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  en a transaction
149c7 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72   is committed or
149c8 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 6f 72   rolled back, or
149c9 20 77 68 65 6e 0a 2a 2a 20 61 20 62 74 72 65 65   when.** a btree
149ca 20 68 61 6e 64 6c 65 20 69 73 20 63 6c 6f 73 65   handle is close
149cb 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 4c  d..*/.struct BtL
149cc 6f 63 6b 20 7b 0a 20 20 42 74 72 65 65 20 2a 70  ock {.  Btree *p
149cd 42 74 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a  Btree;        /*
149ce 20 42 74 72 65 65 20 68 61 6e 64 6c 65 20 68 6f   Btree handle ho
149cf 6c 64 69 6e 67 20 74 68 69 73 20 6c 6f 63 6b 20  lding this lock 
149d0 2a 2f 0a 20 20 50 67 6e 6f 20 69 54 61 62 6c 65  */.  Pgno iTable
149d1 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f  ;          /* Ro
149d2 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65  ot page of table
149d3 20 2a 2f 0a 20 20 75 38 20 65 4c 6f 63 6b 3b 20   */.  u8 eLock; 
149d4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
149d5 45 41 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 54  EAD_LOCK or WRIT
149d6 45 5f 4c 4f 43 4b 20 2a 2f 0a 20 20 42 74 4c 6f  E_LOCK */.  BtLo
149d7 63 6b 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20  ck *pNext;      
149d8 20 20 2f 2a 20 4e 65 78 74 20 69 6e 20 42 74 53    /* Next in BtS
149d9 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 73 74  hared.pLock list
149da 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 43 61 6e 64 69   */.};../* Candi
149db 64 61 74 65 20 76 61 6c 75 65 73 20 66 6f 72 20  date values for 
149dc 42 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 2a 2f 0a  BtLock.eLock */.
149dd 23 64 65 66 69 6e 65 20 52 45 41 44 5f 4c 4f 43  #define READ_LOC
149de 4b 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20  K     1.#define 
149df 57 52 49 54 45 5f 4c 4f 43 4b 20 20 20 20 32 0a  WRITE_LOCK    2.
149e0 0a 2f 2a 20 41 20 42 74 72 65 65 20 68 61 6e 64  ./* A Btree hand
149e1 6c 65 0a 2a 2a 0a 2a 2a 20 41 20 64 61 74 61 62  le.**.** A datab
149e2 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63  ase connection c
149e3 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65  ontains a pointe
149e4 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65  r to an instance
149e5 20 6f 66 0a 2a 2a 20 74 68 69 73 20 6f 62 6a 65   of.** this obje
149e6 63 74 20 66 6f 72 20 65 76 65 72 79 20 64 61 74  ct for every dat
149e7 61 62 61 73 65 20 66 69 6c 65 20 74 68 61 74 20  abase file that 
149e8 69 74 20 68 61 73 20 6f 70 65 6e 2e 20 20 54 68  it has open.  Th
149e9 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  is structure.** 
149ea 69 73 20 6f 70 61 71 75 65 20 74 6f 20 74 68 65  is opaque to the
149eb 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
149ec 74 69 6f 6e 2e 20 20 54 68 65 20 64 61 74 61 62  tion.  The datab
149ed 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63  ase connection c
149ee 61 6e 6e 6f 74 0a 2a 2a 20 73 65 65 20 74 68 65  annot.** see the
149ef 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66 20 74 68   internals of th
149f0 69 73 20 73 74 72 75 63 74 75 72 65 20 61 6e 64  is structure and
149f1 20 6f 6e 6c 79 20 64 65 61 6c 73 20 77 69 74 68   only deals with
149f2 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a 2a 20   pointers to.** 
149f3 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e 0a  this structure..
149f4 2a 2a 0a 2a 2a 20 46 6f 72 20 73 6f 6d 65 20 64  **.** For some d
149f5 61 74 61 62 61 73 65 20 66 69 6c 65 73 2c 20 74  atabase files, t
149f6 68 65 20 73 61 6d 65 20 75 6e 64 65 72 6c 79 69  he same underlyi
149f7 6e 67 20 64 61 74 61 62 61 73 65 20 63 61 63 68  ng database cach
149f8 65 20 6d 69 67 68 74 20 62 65 20 0a 2a 2a 20 73  e might be .** s
149f9 68 61 72 65 64 20 62 65 74 77 65 65 6e 20 6d 75  hared between mu
149fa 6c 74 69 70 6c 65 20 63 6f 6e 6e 65 63 74 69 6f  ltiple connectio
149fb 6e 73 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73  ns.  In that cas
149fc 65 2c 20 65 61 63 68 20 63 6f 6e 6e 65 63 74 69  e, each connecti
149fd 6f 6e 0a 2a 2a 20 68 61 73 20 69 74 20 6f 77 6e  on.** has it own
149fe 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
149ff 73 20 6f 62 6a 65 63 74 2e 20 20 42 75 74 20 65  s object.  But e
14a00 61 63 68 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  ach instance of 
14a01 74 68 69 73 20 6f 62 6a 65 63 74 0a 2a 2a 20 70  this object.** p
14a02 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73 61 6d  oints to the sam
14a03 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63  e BtShared objec
14a04 74 2e 20 20 54 68 65 20 64 61 74 61 62 61 73 65  t.  The database
14a05 20 63 61 63 68 65 20 61 6e 64 20 74 68 65 0a 2a   cache and the.*
14a06 2a 20 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61  * schema associa
14a07 74 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74  ted with the dat
14a08 61 62 61 73 65 20 66 69 6c 65 20 61 72 65 20 61  abase file are a
14a09 6c 6c 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74  ll contained wit
14a0a 68 69 6e 0a 2a 2a 20 74 68 65 20 42 74 53 68 61  hin.** the BtSha
14a0b 72 65 64 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a  red object..**.*
14a0c 2a 20 41 6c 6c 20 66 69 65 6c 64 73 20 69 6e 20  * All fields in 
14a0d 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61  this structure a
14a0e 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64 65  re accessed unde
14a0f 72 20 73 71 6c 69 74 65 33 2e 6d 75 74 65 78 2e  r sqlite3.mutex.
14a10 0a 2a 2a 20 54 68 65 20 70 42 74 20 70 6f 69 6e  .** The pBt poin
14a11 74 65 72 20 69 74 73 65 6c 66 20 6d 61 79 20 6e  ter itself may n
14a12 6f 74 20 62 65 20 63 68 61 6e 67 65 64 20 77 68  ot be changed wh
14a13 69 6c 65 20 74 68 65 72 65 20 65 78 69 73 74 73  ile there exists
14a14 20 63 75 72 73 6f 72 73 20 0a 2a 2a 20 69 6e 20   cursors .** in 
14a15 74 68 65 20 72 65 66 65 72 65 6e 63 65 64 20 42  the referenced B
14a16 74 53 68 61 72 65 64 20 74 68 61 74 20 70 6f 69  tShared that poi
14a17 6e 74 20 62 61 63 6b 20 74 6f 20 74 68 69 73 20  nt back to this 
14a18 42 74 72 65 65 20 73 69 6e 63 65 20 74 68 6f 73  Btree since thos
14a19 65 0a 2a 2a 20 63 75 72 73 6f 72 73 20 68 61 76  e.** cursors hav
14a1a 65 20 74 6f 20 64 6f 20 67 6f 20 74 68 72 6f 75  e to do go throu
14a1b 67 68 20 74 68 69 73 20 42 74 72 65 65 20 74 6f  gh this Btree to
14a1c 20 66 69 6e 64 20 74 68 65 69 72 20 42 74 53 68   find their BtSh
14a1d 61 72 65 64 20 61 6e 64 0a 2a 2a 20 74 68 65 79  ared and.** they
14a1e 20 6f 66 74 65 6e 20 64 6f 20 73 6f 20 77 69 74   often do so wit
14a1f 68 6f 75 74 20 68 6f 6c 64 69 6e 67 20 73 71 6c  hout holding sql
14a20 69 74 65 33 2e 6d 75 74 65 78 2e 0a 2a 2f 0a 73  ite3.mutex..*/.s
14a21 74 72 75 63 74 20 42 74 72 65 65 20 7b 0a 20 20  truct Btree {.  
14a22 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
14a23 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
14a24 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 6f  se connection ho
14a25 6c 64 69 6e 67 20 74 68 69 73 20 62 74 72 65 65  lding this btree
14a26 20 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a   */.  BtShared *
14a27 70 42 74 3b 20 20 20 20 20 2f 2a 20 53 68 61 72  pBt;     /* Shar
14a28 61 62 6c 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20  able content of 
14a29 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20  this btree */.  
14a2a 75 38 20 69 6e 54 72 61 6e 73 3b 20 20 20 20 20  u8 inTrans;     
14a2b 20 20 20 2f 2a 20 54 52 41 4e 53 5f 4e 4f 4e 45     /* TRANS_NONE
14a2c 2c 20 54 52 41 4e 53 5f 52 45 41 44 20 6f 72 20  , TRANS_READ or 
14a2d 54 52 41 4e 53 5f 57 52 49 54 45 20 2a 2f 0a 20  TRANS_WRITE */. 
14a2e 20 75 38 20 73 68 61 72 61 62 6c 65 3b 20 20 20   u8 sharable;   
14a2f 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77      /* True if w
14a30 65 20 63 61 6e 20 73 68 61 72 65 20 70 42 74 20  e can share pBt 
14a31 77 69 74 68 20 61 6e 6f 74 68 65 72 20 64 62 20  with another db 
14a32 2a 2f 0a 20 20 75 38 20 6c 6f 63 6b 65 64 3b 20  */.  u8 locked; 
14a33 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
14a34 69 66 20 64 62 20 63 75 72 72 65 6e 74 6c 79 20  if db currently 
14a35 68 61 73 20 70 42 74 20 6c 6f 63 6b 65 64 20 2a  has pBt locked *
14a36 2f 0a 20 20 69 6e 74 20 77 61 6e 74 54 6f 4c 6f  /.  int wantToLo
14a37 63 6b 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  ck;    /* Number
14a38 20 6f 66 20 6e 65 73 74 65 64 20 63 61 6c 6c 73   of nested calls
14a39 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65   to sqlite3Btree
14a3a 45 6e 74 65 72 28 29 20 2a 2f 0a 20 20 69 6e 74  Enter() */.  int
14a3b 20 6e 42 61 63 6b 75 70 3b 20 20 20 20 20 20 20   nBackup;       
14a3c 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61 63  /* Number of bac
14a3d 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e 73 20 72  kup operations r
14a3e 65 61 64 69 6e 67 20 74 68 69 73 20 62 74 72 65  eading this btre
14a3f 65 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 4e  e */.  Btree *pN
14a40 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4c 69 73  ext;      /* Lis
14a41 74 20 6f 66 20 6f 74 68 65 72 20 73 68 61 72 61  t of other shara
14a42 62 6c 65 20 42 74 72 65 65 73 20 66 72 6f 6d 20  ble Btrees from 
14a43 74 68 65 20 73 61 6d 65 20 64 62 20 2a 2f 0a 20  the same db */. 
14a44 20 42 74 72 65 65 20 2a 70 50 72 65 76 3b 20 20   Btree *pPrev;  
14a45 20 20 20 20 2f 2a 20 42 61 63 6b 20 70 6f 69 6e      /* Back poin
14a46 74 65 72 20 6f 66 20 74 68 65 20 73 61 6d 65 20  ter of the same 
14a47 6c 69 73 74 20 2a 2f 0a 23 69 66 6e 64 65 66 20  list */.#ifndef 
14a48 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
14a49 45 44 5f 43 41 43 48 45 0a 20 20 42 74 4c 6f 63  ED_CACHE.  BtLoc
14a4a 6b 20 6c 6f 63 6b 3b 20 20 20 20 20 20 20 2f 2a  k lock;       /*
14a4b 20 4f 62 6a 65 63 74 20 75 73 65 64 20 74 6f 20   Object used to 
14a4c 6c 6f 63 6b 20 70 61 67 65 20 31 20 2a 2f 0a 23  lock page 1 */.#
14a4d 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  endif.};../*.** 
14a4e 42 74 72 65 65 2e 69 6e 54 72 61 6e 73 20 6d 61  Btree.inTrans ma
14a4f 79 20 74 61 6b 65 20 6f 6e 65 20 6f 66 20 74 68  y take one of th
14a50 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75  e following valu
14a51 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  es..**.** If the
14a52 20 73 68 61 72 65 64 2d 64 61 74 61 20 65 78 74   shared-data ext
14a53 65 6e 73 69 6f 6e 20 69 73 20 65 6e 61 62 6c 65  ension is enable
14a54 64 2c 20 74 68 65 72 65 20 6d 61 79 20 62 65 20  d, there may be 
14a55 6d 75 6c 74 69 70 6c 65 20 75 73 65 72 73 0a 2a  multiple users.*
14a56 2a 20 6f 66 20 74 68 65 20 42 74 72 65 65 20 73  * of the Btree s
14a57 74 72 75 63 74 75 72 65 2e 20 41 74 20 6d 6f 73  tructure. At mos
14a58 74 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 6d  t one of these m
14a59 61 79 20 6f 70 65 6e 20 61 20 77 72 69 74 65 20  ay open a write 
14a5a 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20  transaction,.** 
14a5b 62 75 74 20 61 6e 79 20 6e 75 6d 62 65 72 20 6d  but any number m
14a5c 61 79 20 68 61 76 65 20 61 63 74 69 76 65 20 72  ay have active r
14a5d 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73  ead transactions
14a5e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 52 41  ..*/.#define TRA
14a5f 4e 53 5f 4e 4f 4e 45 20 20 30 0a 23 64 65 66 69  NS_NONE  0.#defi
14a60 6e 65 20 54 52 41 4e 53 5f 52 45 41 44 20 20 31  ne TRANS_READ  1
14a61 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f 57  .#define TRANS_W
14a62 52 49 54 45 20 32 0a 0a 2f 2a 0a 2a 2a 20 41 6e  RITE 2../*.** An
14a63 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69   instance of thi
14a64 73 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73 65  s object represe
14a65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20 64 61 74  nts a single dat
14a66 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a  abase file..** .
14a67 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64 61 74 61  ** A single data
14a68 62 61 73 65 20 66 69 6c 65 20 63 61 6e 20 62 65  base file can be
14a69 20 69 6e 20 75 73 65 20 61 73 20 74 68 65 20 73   in use as the s
14a6a 61 6d 65 20 74 69 6d 65 20 62 79 20 74 77 6f 0a  ame time by two.
14a6b 2a 2a 20 6f 72 20 6d 6f 72 65 20 64 61 74 61 62  ** or more datab
14a6c 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e  ase connections.
14a6d 20 20 57 68 65 6e 20 74 77 6f 20 6f 72 20 6d 6f    When two or mo
14a6e 72 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61  re connections a
14a6f 72 65 0a 2a 2a 20 73 68 61 72 69 6e 67 20 74 68  re.** sharing th
14a70 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20  e same database 
14a71 66 69 6c 65 2c 20 65 61 63 68 20 63 6f 6e 6e 65  file, each conne
14a72 63 74 69 6f 6e 20 68 61 73 20 69 74 20 6f 77 6e  ction has it own
14a73 0a 2a 2a 20 70 72 69 76 61 74 65 20 42 74 72 65  .** private Btre
14a74 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65  e object for the
14a75 20 66 69 6c 65 20 61 6e 64 20 65 61 63 68 20 6f   file and each o
14a76 66 20 74 68 6f 73 65 20 42 74 72 65 65 73 20 70  f those Btrees p
14a77 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 74 68 69 73  oints.** to this
14a78 20 6f 6e 65 20 42 74 53 68 61 72 65 64 20 6f 62   one BtShared ob
14a79 6a 65 63 74 2e 20 20 42 74 53 68 61 72 65 64 2e  ject.  BtShared.
14a7a 6e 52 65 66 20 69 73 20 74 68 65 20 6e 75 6d 62  nRef is the numb
14a7b 65 72 20 6f 66 0a 2a 2a 20 63 6f 6e 6e 65 63 74  er of.** connect
14a7c 69 6f 6e 73 20 63 75 72 72 65 6e 74 6c 79 20 73  ions currently s
14a7d 68 61 72 69 6e 67 20 74 68 69 73 20 64 61 74 61  haring this data
14a7e 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  base file..**.**
14a7f 20 46 69 65 6c 64 73 20 69 6e 20 74 68 69 73 20   Fields in this 
14a80 73 74 72 75 63 74 75 72 65 20 61 72 65 20 61 63  structure are ac
14a81 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 68 65  cessed under the
14a82 20 42 74 53 68 61 72 65 64 2e 6d 75 74 65 78 0a   BtShared.mutex.
14a83 2a 2a 20 6d 75 74 65 78 2c 20 65 78 63 65 70 74  ** mutex, except
14a84 20 66 6f 72 20 6e 52 65 66 20 61 6e 64 20 70 4e   for nRef and pN
14a85 65 78 74 20 77 68 69 63 68 20 61 72 65 20 61 63  ext which are ac
14a86 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 68 65  cessed under the
14a87 0a 2a 2a 20 67 6c 6f 62 61 6c 20 53 51 4c 49 54  .** global SQLIT
14a88 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
14a89 41 53 54 45 52 20 6d 75 74 65 78 2e 20 20 54 68  ASTER mutex.  Th
14a8a 65 20 70 50 61 67 65 72 20 66 69 65 6c 64 0a 2a  e pPager field.*
14a8b 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f 64  * may not be mod
14a8c 69 66 69 65 64 20 6f 6e 63 65 20 69 74 20 69 73  ified once it is
14a8d 20 69 6e 69 74 69 61 6c 6c 79 20 73 65 74 20 61   initially set a
14a8e 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65 66 3e 30  s long as nRef>0
14a8f 2e 0a 2a 2a 20 54 68 65 20 70 53 63 68 65 6d 61  ..** The pSchema
14a90 20 66 69 65 6c 64 20 6d 61 79 20 62 65 20 73 65   field may be se
14a91 74 20 6f 6e 63 65 20 75 6e 64 65 72 20 42 74 53  t once under BtS
14a92 68 61 72 65 64 2e 6d 75 74 65 78 20 61 6e 64 0a  hared.mutex and.
14a93 2a 2a 20 74 68 65 72 65 61 66 74 65 72 20 69 73  ** thereafter is
14a94 20 75 6e 63 68 61 6e 67 65 64 20 61 73 20 6c 6f   unchanged as lo
14a95 6e 67 20 61 73 20 6e 52 65 66 3e 30 2e 0a 2a 2a  ng as nRef>0..**
14a96 0a 2a 2a 20 69 73 50 65 6e 64 69 6e 67 3a 0a 2a  .** isPending:.*
14a97 2a 0a 2a 2a 20 20 20 49 66 20 61 20 42 74 53 68  *.**   If a BtSh
14a98 61 72 65 64 20 63 6c 69 65 6e 74 20 66 61 69 6c  ared client fail
14a99 73 20 74 6f 20 6f 62 74 61 69 6e 20 61 20 77 72  s to obtain a wr
14a9a 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 61 20 64 61  ite-lock on a da
14a9b 74 61 62 61 73 65 0a 2a 2a 20 20 20 74 61 62 6c  tabase.**   tabl
14a9c 65 20 28 62 65 63 61 75 73 65 20 74 68 65 72 65  e (because there
14a9d 20 65 78 69 73 74 73 20 6f 6e 65 20 6f 72 20 6d   exists one or m
14a9e 6f 72 65 20 72 65 61 64 2d 6c 6f 63 6b 73 20 6f  ore read-locks o
14a9f 6e 20 74 68 65 20 74 61 62 6c 65 29 2c 0a 2a 2a  n the table),.**
14aa0 20 20 20 74 68 65 20 73 68 61 72 65 64 2d 63 61     the shared-ca
14aa1 63 68 65 20 65 6e 74 65 72 73 20 27 70 65 6e 64  che enters 'pend
14aa2 69 6e 67 2d 6c 6f 63 6b 27 20 73 74 61 74 65 20  ing-lock' state 
14aa3 61 6e 64 20 69 73 50 65 6e 64 69 6e 67 20 69 73  and isPending is
14aa4 0a 2a 2a 20 20 20 73 65 74 20 74 6f 20 74 72 75  .**   set to tru
14aa5 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65 20 73  e..**.**   The s
14aa6 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65 61 76  hared-cache leav
14aa7 65 73 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20  es the 'pending 
14aa8 6c 6f 63 6b 27 20 73 74 61 74 65 20 77 68 65 6e  lock' state when
14aa9 20 65 69 74 68 65 72 20 6f 66 0a 2a 2a 20 20 20   either of.**   
14aaa 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6f 63  the following oc
14aab 63 75 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 31  cur:.**.**     1
14aac 29 20 54 68 65 20 63 75 72 72 65 6e 74 20 77 72  ) The current wr
14aad 69 74 65 72 20 28 42 74 53 68 61 72 65 64 2e 70  iter (BtShared.p
14aae 57 72 69 74 65 72 29 20 63 6f 6e 63 6c 75 64 65  Writer) conclude
14aaf 73 20 69 74 73 20 74 72 61 6e 73 61 63 74 69 6f  s its transactio
14ab0 6e 2c 20 4f 52 0a 2a 2a 20 20 20 20 20 32 29 20  n, OR.**     2) 
14ab1 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f  The number of lo
14ab2 63 6b 73 20 68 65 6c 64 20 62 79 20 6f 74 68 65  cks held by othe
14ab3 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 64 72  r connections dr
14ab4 6f 70 73 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 0a  ops to zero..**.
14ab5 2a 2a 20 20 20 77 68 69 6c 65 20 69 6e 20 74 68  **   while in th
14ab6 65 20 27 70 65 6e 64 69 6e 67 2d 6c 6f 63 6b 27  e 'pending-lock'
14ab7 20 73 74 61 74 65 2c 20 6e 6f 20 63 6f 6e 6e 65   state, no conne
14ab8 63 74 69 6f 6e 20 6d 61 79 20 73 74 61 72 74 20  ction may start 
14ab9 61 20 6e 65 77 0a 2a 2a 20 20 20 74 72 61 6e 73  a new.**   trans
14aba 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20  action..**.**   
14abb 54 68 69 73 20 66 65 61 74 75 72 65 20 69 73 20  This feature is 
14abc 69 6e 63 6c 75 64 65 64 20 74 6f 20 68 65 6c 70  included to help
14abd 20 70 72 65 76 65 6e 74 20 77 72 69 74 65 72 2d   prevent writer-
14abe 73 74 61 72 76 61 74 69 6f 6e 2e 0a 2a 2f 0a 73  starvation..*/.s
14abf 74 72 75 63 74 20 42 74 53 68 61 72 65 64 20 7b  truct BtShared {
14ac0 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
14ac1 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
14ac2 70 61 67 65 20 63 61 63 68 65 20 2a 2f 0a 20 20  page cache */.  
14ac3 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
14ac4 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
14ac5 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 63 75 72  e connection cur
14ac6 72 65 6e 74 6c 79 20 75 73 69 6e 67 20 74 68 69  rently using thi
14ac7 73 20 42 74 72 65 65 20 2a 2f 0a 20 20 42 74 43  s Btree */.  BtC
14ac8 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 20  ursor *pCursor; 
14ac9 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20     /* A list of 
14aca 61 6c 6c 20 6f 70 65 6e 20 63 75 72 73 6f 72 73  all open cursors
14acb 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70   */.  MemPage *p
14acc 50 61 67 65 31 3b 20 20 20 20 20 20 2f 2a 20 46  Page1;      /* F
14acd 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65  irst page of the
14ace 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 75   database */.  u
14acf 38 20 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20  8 readOnly;     
14ad0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
14ad1 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 66  the underlying f
14ad2 69 6c 65 20 69 73 20 72 65 61 64 6f 6e 6c 79 20  ile is readonly 
14ad3 2a 2f 0a 20 20 75 38 20 70 61 67 65 53 69 7a 65  */.  u8 pageSize
14ad4 46 69 78 65 64 3b 20 20 20 20 20 2f 2a 20 54 72  Fixed;     /* Tr
14ad5 75 65 20 69 66 20 74 68 65 20 70 61 67 65 20 73  ue if the page s
14ad6 69 7a 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65  ize can no longe
14ad7 72 20 62 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a  r be changed */.
14ad8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
14ad9 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
14ada 20 75 38 20 61 75 74 6f 56 61 63 75 75 6d 3b 20   u8 autoVacuum; 
14adb 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
14adc 66 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 73  f auto-vacuum is
14add 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 75 38   enabled */.  u8
14ade 20 69 6e 63 72 56 61 63 75 75 6d 3b 20 20 20 20   incrVacuum;    
14adf 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69      /* True if i
14ae0 6e 63 72 2d 76 61 63 75 75 6d 20 69 73 20 65 6e  ncr-vacuum is en
14ae1 61 62 6c 65 64 20 2a 2f 0a 23 65 6e 64 69 66 0a  abled */.#endif.
14ae2 20 20 75 31 36 20 70 61 67 65 53 69 7a 65 3b 20    u16 pageSize; 
14ae3 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c          /* Total
14ae4 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
14ae5 20 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 20   on a page */.  
14ae6 75 31 36 20 75 73 61 62 6c 65 53 69 7a 65 3b 20  u16 usableSize; 
14ae7 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
14ae8 6f 66 20 75 73 61 62 6c 65 20 62 79 74 65 73 20  of usable bytes 
14ae9 6f 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a  on each page */.
14aea 20 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c 3b 20    u16 maxLocal; 
14aeb 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
14aec 75 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64  um local payload
14aed 20 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41 54 41   in non-LEAFDATA
14aee 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 75 31 36   tables */.  u16
14aef 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 20   minLocal;      
14af0 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f     /* Minimum lo
14af1 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 6e  cal payload in n
14af2 6f 6e 2d 4c 45 41 46 44 41 54 41 20 74 61 62 6c  on-LEAFDATA tabl
14af3 65 73 20 2a 2f 0a 20 20 75 31 36 20 6d 61 78 4c  es */.  u16 maxL
14af4 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  eaf;          /*
14af5 20 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 6c 20 70   Maximum local p
14af6 61 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45 41 46  ayload in a LEAF
14af7 44 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a 20 20  DATA table */.  
14af8 75 31 36 20 6d 69 6e 4c 65 61 66 3b 20 20 20 20  u16 minLeaf;    
14af9 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d        /* Minimum
14afa 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69   local payload i
14afb 6e 20 61 20 4c 45 41 46 44 41 54 41 20 74 61 62  n a LEAFDATA tab
14afc 6c 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72 61  le */.  u8 inTra
14afd 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 20 2f 2a  nsaction;     /*
14afe 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 61   Transaction sta
14aff 74 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 72 61  te */.  int nTra
14b00 6e 73 61 63 74 69 6f 6e 3b 20 20 20 20 20 2f 2a  nsaction;     /*
14b01 20 4e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20   Number of open 
14b02 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 28 72 65  transactions (re
14b03 61 64 20 2b 20 77 72 69 74 65 29 20 2a 2f 0a 20  ad + write) */. 
14b04 20 76 6f 69 64 20 2a 70 53 63 68 65 6d 61 3b 20   void *pSchema; 
14b05 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
14b06 72 20 74 6f 20 73 70 61 63 65 20 61 6c 6c 6f 63  r to space alloc
14b07 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 42  ated by sqlite3B
14b08 74 72 65 65 53 63 68 65 6d 61 28 29 20 2a 2f 0a  treeSchema() */.
14b09 20 20 76 6f 69 64 20 28 2a 78 46 72 65 65 53 63    void (*xFreeSc
14b0a 68 65 6d 61 29 28 76 6f 69 64 2a 29 3b 20 20 2f  hema)(void*);  /
14b0b 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72  * Destructor for
14b0c 20 42 74 53 68 61 72 65 64 2e 70 53 63 68 65 6d   BtShared.pSchem
14b0d 61 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d  a */.  sqlite3_m
14b0e 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 2f 2a 20  utex *mutex; /* 
14b0f 4e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75  Non-recursive mu
14b10 74 65 78 20 72 65 71 75 69 72 65 64 20 74 6f 20  tex required to 
14b11 61 63 63 65 73 73 20 74 68 69 73 20 73 74 72 75  access this stru
14b12 63 74 20 2a 2f 0a 20 20 42 69 74 76 65 63 20 2a  ct */.  Bitvec *
14b13 70 48 61 73 43 6f 6e 74 65 6e 74 3b 20 20 2f 2a  pHasContent;  /*
14b14 20 53 65 74 20 6f 66 20 70 61 67 65 73 20 6d 6f   Set of pages mo
14b15 76 65 64 20 74 6f 20 66 72 65 65 2d 6c 69 73 74  ved to free-list
14b16 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f   this transactio
14b17 6e 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  n */.#ifndef SQL
14b18 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
14b19 43 41 43 48 45 0a 20 20 69 6e 74 20 6e 52 65 66  CACHE.  int nRef
14b1a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
14b1b 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72   Number of refer
14b1c 65 6e 63 65 73 20 74 6f 20 74 68 69 73 20 73 74  ences to this st
14b1d 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 42 74 53  ructure */.  BtS
14b1e 68 61 72 65 64 20 2a 70 4e 65 78 74 3b 20 20 20  hared *pNext;   
14b1f 20 20 20 2f 2a 20 4e 65 78 74 20 6f 6e 20 61 20     /* Next on a 
14b20 6c 69 73 74 20 6f 66 20 73 68 61 72 61 62 6c 65  list of sharable
14b21 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74   BtShared struct
14b22 73 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20 2a 70  s */.  BtLock *p
14b23 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20  Lock;        /* 
14b24 4c 69 73 74 20 6f 66 20 6c 6f 63 6b 73 20 68 65  List of locks he
14b25 6c 64 20 6f 6e 20 74 68 69 73 20 73 68 61 72 65  ld on this share
14b26 64 2d 62 74 72 65 65 20 73 74 72 75 63 74 20 2a  d-btree struct *
14b27 2f 0a 20 20 42 74 72 65 65 20 2a 70 57 72 69 74  /.  Btree *pWrit
14b28 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 42 74 72  er;       /* Btr
14b29 65 65 20 77 69 74 68 20 63 75 72 72 65 6e 74 6c  ee with currentl
14b2a 79 20 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61  y open write tra
14b2b 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38  nsaction */.  u8
14b2c 20 69 73 45 78 63 6c 75 73 69 76 65 3b 20 20 20   isExclusive;   
14b2d 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 70      /* True if p
14b2e 57 72 69 74 65 72 20 68 61 73 20 61 6e 20 45 58  Writer has an EX
14b2f 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20  CLUSIVE lock on 
14b30 74 68 65 20 64 62 20 2a 2f 0a 20 20 75 38 20 69  the db */.  u8 i
14b31 73 50 65 6e 64 69 6e 67 3b 20 20 20 20 20 20 20  sPending;       
14b32 20 20 2f 2a 20 49 66 20 77 61 69 74 69 6e 67 20    /* If waiting 
14b33 66 6f 72 20 72 65 61 64 2d 6c 6f 63 6b 73 20 74  for read-locks t
14b34 6f 20 63 6c 65 61 72 20 2a 2f 0a 23 65 6e 64 69  o clear */.#endi
14b35 66 0a 20 20 75 38 20 2a 70 54 6d 70 53 70 61 63  f.  u8 *pTmpSpac
14b36 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42 74 53  e;        /* BtS
14b37 68 61 72 65 64 2e 70 61 67 65 53 69 7a 65 20 62  hared.pageSize b
14b38 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 66 6f  ytes of space fo
14b39 72 20 74 6d 70 20 75 73 65 20 2a 2f 0a 7d 3b 0a  r tmp use */.};.
14b3a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
14b3b 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
14b3c 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73  ing structure is
14b3d 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 69 6e   used to hold in
14b3e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61 62 6f  formation.** abo
14b3f 75 74 20 61 20 63 65 6c 6c 2e 20 20 54 68 65 20  ut a cell.  The 
14b40 70 61 72 73 65 43 65 6c 6c 50 74 72 28 29 20 66  parseCellPtr() f
14b41 75 6e 63 74 69 6f 6e 20 66 69 6c 6c 73 20 69 6e  unction fills in
14b42 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a   this structure.
14b43 2a 2a 20 62 61 73 65 64 20 6f 6e 20 69 6e 66 6f  ** based on info
14b44 72 6d 61 74 69 6f 6e 20 65 78 74 72 61 63 74 20  rmation extract 
14b45 66 72 6f 6d 20 74 68 65 20 72 61 77 20 64 69 73  from the raw dis
14b46 6b 20 70 61 67 65 2e 0a 2a 2f 0a 74 79 70 65 64  k page..*/.typed
14b47 65 66 20 73 74 72 75 63 74 20 43 65 6c 6c 49 6e  ef struct CellIn
14b48 66 6f 20 43 65 6c 6c 49 6e 66 6f 3b 0a 73 74 72  fo CellInfo;.str
14b49 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 7b 0a 20  uct CellInfo {. 
14b4a 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20 20 20   u8 *pCell;     
14b4b 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68  /* Pointer to th
14b4c 65 20 73 74 61 72 74 20 6f 66 20 63 65 6c 6c 20  e start of cell 
14b4d 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 36 34  content */.  i64
14b4e 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 54   nKey;      /* T
14b4f 68 65 20 6b 65 79 20 66 6f 72 20 49 4e 54 4b 45  he key for INTKE
14b50 59 20 74 61 62 6c 65 73 2c 20 6f 72 20 6e 75 6d  Y tables, or num
14b51 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
14b52 6b 65 79 20 2a 2f 0a 20 20 75 33 32 20 6e 44 61  key */.  u32 nDa
14b53 74 61 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ta;     /* Numbe
14b54 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61  r of bytes of da
14b55 74 61 20 2a 2f 0a 20 20 75 33 32 20 6e 50 61 79  ta */.  u32 nPay
14b56 6c 6f 61 64 3b 20 20 2f 2a 20 54 6f 74 61 6c 20  load;  /* Total 
14b57 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61  amount of payloa
14b58 64 20 2a 2f 0a 20 20 75 31 36 20 6e 48 65 61 64  d */.  u16 nHead
14b59 65 72 3b 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66  er;   /* Size of
14b5a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
14b5b 74 20 68 65 61 64 65 72 20 69 6e 20 62 79 74 65  t header in byte
14b5c 73 20 2a 2f 0a 20 20 75 31 36 20 6e 4c 6f 63 61  s */.  u16 nLoca
14b5d 6c 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20  l;    /* Amount 
14b5e 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 20  of payload held 
14b5f 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 75 31 36  locally */.  u16
14b60 20 69 4f 76 65 72 66 6c 6f 77 3b 20 2f 2a 20 4f   iOverflow; /* O
14b61 66 66 73 65 74 20 74 6f 20 6f 76 65 72 66 6c 6f  ffset to overflo
14b62 77 20 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 20  w page number.  
14b63 5a 65 72 6f 20 69 66 20 6e 6f 20 6f 76 65 72 66  Zero if no overf
14b64 6c 6f 77 20 2a 2f 0a 20 20 75 31 36 20 6e 53 69  low */.  u16 nSi
14b65 7a 65 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20  ze;     /* Size 
14b66 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  of the cell cont
14b67 65 6e 74 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20  ent on the main 
14b68 62 2d 74 72 65 65 20 70 61 67 65 20 2a 2f 0a 7d  b-tree page */.}
14b69 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d  ;../*.** Maximum
14b6a 20 64 65 70 74 68 20 6f 66 20 61 6e 20 53 51 4c   depth of an SQL
14b6b 69 74 65 20 42 2d 54 72 65 65 20 73 74 72 75 63  ite B-Tree struc
14b6c 74 75 72 65 2e 20 41 6e 79 20 42 2d 54 72 65 65  ture. Any B-Tree
14b6d 20 64 65 65 70 65 72 20 74 68 61 6e 0a 2a 2a 20   deeper than.** 
14b6e 74 68 69 73 20 77 69 6c 6c 20 62 65 20 64 65 63  this will be dec
14b6f 6c 61 72 65 64 20 63 6f 72 72 75 70 74 2e 20 54  lared corrupt. T
14b70 68 69 73 20 76 61 6c 75 65 20 69 73 20 63 61 6c  his value is cal
14b71 63 75 6c 61 74 65 64 20 62 61 73 65 64 20 6f 6e  culated based on
14b72 20 61 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20 64 61   a.** maximum da
14b73 74 61 62 61 73 65 20 73 69 7a 65 20 6f 66 20 32  tabase size of 2
14b74 5e 33 31 20 70 61 67 65 73 20 61 20 6d 69 6e 69  ^31 pages a mini
14b75 6d 75 6d 20 66 61 6e 6f 75 74 20 6f 66 20 32 20  mum fanout of 2 
14b76 66 6f 72 20 61 0a 2a 2a 20 72 6f 6f 74 2d 6e 6f  for a.** root-no
14b77 64 65 20 61 6e 64 20 33 20 66 6f 72 20 61 6c 6c  de and 3 for all
14b78 20 6f 74 68 65 72 20 69 6e 74 65 72 6e 61 6c 20   other internal 
14b79 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  nodes..**.** If 
14b7a 61 20 74 72 65 65 20 74 68 61 74 20 61 70 70 65  a tree that appe
14b7b 61 72 73 20 74 6f 20 62 65 20 74 61 6c 6c 65 72  ars to be taller
14b7c 20 74 68 61 6e 20 74 68 69 73 20 69 73 20 65 6e   than this is en
14b7d 63 6f 75 6e 74 65 72 65 64 2c 20 69 74 20 69 73  countered, it is
14b7e 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74 68 61 74  .** assumed that
14b7f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
14b80 20 63 6f 72 72 75 70 74 2e 0a 2a 2f 0a 23 64 65   corrupt..*/.#de
14b81 66 69 6e 65 20 42 54 43 55 52 53 4f 52 5f 4d 41  fine BTCURSOR_MA
14b82 58 5f 44 45 50 54 48 20 32 30 0a 0a 2f 2a 0a 2a  X_DEPTH 20../*.*
14b83 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20 61 20  * A cursor is a 
14b84 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 61 72  pointer to a par
14b85 74 69 63 75 6c 61 72 20 65 6e 74 72 79 20 77 69  ticular entry wi
14b86 74 68 69 6e 20 61 20 70 61 72 74 69 63 75 6c 61  thin a particula
14b87 72 0a 2a 2a 20 62 2d 74 72 65 65 20 77 69 74 68  r.** b-tree with
14b88 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 66 69  in a database fi
14b89 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 6e  le..**.** The en
14b8a 74 72 79 20 69 73 20 69 64 65 6e 74 69 66 69 65  try is identifie
14b8b 64 20 62 79 20 69 74 73 20 4d 65 6d 50 61 67 65  d by its MemPage
14b8c 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78 20 69   and the index i
14b8d 6e 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 43 65  n.** MemPage.aCe
14b8e 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 65 6e 74 72  ll[] of the entr
14b8f 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c  y..**.** A singl
14b90 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
14b91 63 61 6e 20 73 68 61 72 65 64 20 62 79 20 74 77  can shared by tw
14b92 6f 20 6d 6f 72 65 20 64 61 74 61 62 61 73 65 20  o more database 
14b93 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a 20  connections,.** 
14b94 62 75 74 20 63 75 72 73 6f 72 73 20 63 61 6e 6e  but cursors cann
14b95 6f 74 20 62 65 20 73 68 61 72 65 64 2e 20 20 45  ot be shared.  E
14b96 61 63 68 20 63 75 72 73 6f 72 20 69 73 20 61 73  ach cursor is as
14b97 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 0a  sociated with a.
14b98 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20 64 61  ** particular da
14b99 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
14b9a 6e 20 69 64 65 6e 74 69 66 69 65 64 20 42 74 43  n identified BtC
14b9b 75 72 73 6f 72 2e 70 42 74 72 65 65 2e 64 62 2e  ursor.pBtree.db.
14b9c 0a 2a 2a 0a 2a 2a 20 46 69 65 6c 64 73 20 69 6e  .**.** Fields in
14b9d 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
14b9e 61 72 65 20 61 63 63 65 73 73 65 64 20 75 6e 64  are accessed und
14b9f 65 72 20 74 68 65 20 42 74 53 68 61 72 65 64 2e  er the BtShared.
14ba0 6d 75 74 65 78 0a 2a 2a 20 66 6f 75 6e 64 20 61  mutex.** found a
14ba1 74 20 73 65 6c 66 2d 3e 70 42 74 2d 3e 6d 75 74  t self->pBt->mut
14ba2 65 78 2e 20 0a 2a 2f 0a 73 74 72 75 63 74 20 42  ex. .*/.struct B
14ba3 74 43 75 72 73 6f 72 20 7b 0a 20 20 42 74 72 65  tCursor {.  Btre
14ba4 65 20 2a 70 42 74 72 65 65 3b 20 20 20 20 20 20  e *pBtree;      
14ba5 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74 72        /* The Btr
14ba6 65 65 20 74 6f 20 77 68 69 63 68 20 74 68 69 73  ee to which this
14ba7 20 63 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20   cursor belongs 
14ba8 2a 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  */.  BtShared *p
14ba9 42 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  Bt;            /
14baa 2a 20 54 68 65 20 42 74 53 68 61 72 65 64 20 74  * The BtShared t
14bab 68 69 73 20 63 75 72 73 6f 72 20 70 6f 69 6e 74  his cursor point
14bac 73 20 74 6f 20 2a 2f 0a 20 20 42 74 43 75 72 73  s to */.  BtCurs
14bad 6f 72 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65  or *pNext, *pPre
14bae 76 3b 20 20 2f 2a 20 46 6f 72 6d 73 20 61 20 6c  v;  /* Forms a l
14baf 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61 6c  inked list of al
14bb0 6c 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 73  l cursors */.  s
14bb1 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70  truct KeyInfo *p
14bb2 4b 65 79 49 6e 66 6f 3b 20 2f 2a 20 41 72 67 75  KeyInfo; /* Argu
14bb3 6d 65 6e 74 20 70 61 73 73 65 64 20 74 6f 20 63  ment passed to c
14bb4 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69  omparison functi
14bb5 6f 6e 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e  on */.  Pgno pgn
14bb6 6f 52 6f 6f 74 3b 20 20 20 20 20 20 20 20 20 20  oRoot;          
14bb7 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20 70 61    /* The root pa
14bb8 67 65 20 6f 66 20 74 68 69 73 20 74 72 65 65 20  ge of this tree 
14bb9 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
14bba 36 34 20 63 61 63 68 65 64 52 6f 77 69 64 3b 20  64 cachedRowid; 
14bbb 2f 2a 20 4e 65 78 74 20 72 6f 77 69 64 20 63 61  /* Next rowid ca
14bbc 63 68 65 2e 20 20 30 20 6d 65 61 6e 73 20 6e 6f  che.  0 means no
14bbd 74 20 76 61 6c 69 64 20 2a 2f 0a 20 20 43 65 6c  t valid */.  Cel
14bbe 6c 49 6e 66 6f 20 69 6e 66 6f 3b 20 20 20 20 20  lInfo info;     
14bbf 20 20 20 20 20 20 20 2f 2a 20 41 20 70 61 72 73         /* A pars
14bc0 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 77 65  e of the cell we
14bc1 20 61 72 65 20 70 6f 69 6e 74 69 6e 67 20 61 74   are pointing at
14bc2 20 2a 2f 0a 20 20 75 38 20 77 72 46 6c 61 67 3b   */.  u8 wrFlag;
14bc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14bc4 2f 2a 20 54 72 75 65 20 69 66 20 77 72 69 74 61  /* True if writa
14bc5 62 6c 65 20 2a 2f 0a 20 20 75 38 20 61 74 4c 61  ble */.  u8 atLa
14bc6 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  st;             
14bc7 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69     /* Cursor poi
14bc8 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73  nting to the las
14bc9 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 75 38 20  t entry */.  u8 
14bca 76 61 6c 69 64 4e 4b 65 79 3b 20 20 20 20 20 20  validNKey;      
14bcb 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
14bcc 66 20 69 6e 66 6f 2e 6e 4b 65 79 20 69 73 20 76  f info.nKey is v
14bcd 61 6c 69 64 20 2a 2f 0a 20 20 75 38 20 65 53 74  alid */.  u8 eSt
14bce 61 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ate;            
14bcf 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68      /* One of th
14bd0 65 20 43 55 52 53 4f 52 5f 58 58 58 20 63 6f 6e  e CURSOR_XXX con
14bd1 73 74 61 6e 74 73 20 28 73 65 65 20 62 65 6c 6f  stants (see belo
14bd2 77 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4b  w) */.  void *pK
14bd3 65 79 3b 20 20 20 20 20 20 2f 2a 20 53 61 76 65  ey;      /* Save
14bd4 64 20 6b 65 79 20 74 68 61 74 20 77 61 73 20 63  d key that was c
14bd5 75 72 73 6f 72 27 73 20 6c 61 73 74 20 6b 6e 6f  ursor's last kno
14bd6 77 6e 20 70 6f 73 69 74 69 6f 6e 20 2a 2f 0a 20  wn position */. 
14bd7 20 69 36 34 20 6e 4b 65 79 3b 20 20 20 20 20 20   i64 nKey;      
14bd8 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 4b 65    /* Size of pKe
14bd9 79 2c 20 6f 72 20 6c 61 73 74 20 69 6e 74 65 67  y, or last integ
14bda 65 72 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20  er key */.  int 
14bdb 73 6b 69 70 4e 65 78 74 3b 20 20 20 20 2f 2a 20  skipNext;    /* 
14bdc 50 72 65 76 28 29 20 69 73 20 6e 6f 6f 70 20 69  Prev() is noop i
14bdd 66 20 6e 65 67 61 74 69 76 65 2e 20 4e 65 78 74  f negative. Next
14bde 28 29 20 69 73 20 6e 6f 6f 70 20 69 66 20 70 6f  () is noop if po
14bdf 73 69 74 69 76 65 20 2a 2f 0a 23 69 66 6e 64 65  sitive */.#ifnde
14be0 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
14be1 43 52 42 4c 4f 42 0a 20 20 75 38 20 69 73 49 6e  CRBLOB.  u8 isIn
14be2 63 72 62 6c 6f 62 48 61 6e 64 6c 65 3b 20 20 20  crblobHandle;   
14be3 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
14be4 69 73 20 63 75 72 73 6f 72 20 69 73 20 61 6e 20  is cursor is an 
14be5 69 6e 63 72 2e 20 69 6f 20 68 61 6e 64 6c 65 20  incr. io handle 
14be6 2a 2f 0a 20 20 50 67 6e 6f 20 2a 61 4f 76 65 72  */.  Pgno *aOver
14be7 66 6c 6f 77 3b 20 20 20 20 20 20 20 20 20 20 2f  flow;          /
14be8 2a 20 43 61 63 68 65 20 6f 66 20 6f 76 65 72 66  * Cache of overf
14be9 6c 6f 77 20 70 61 67 65 20 6c 6f 63 61 74 69 6f  low page locatio
14bea 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69  ns */.#endif.  i
14beb 31 36 20 69 50 61 67 65 3b 20 20 20 20 20 20 20  16 iPage;       
14bec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14bed 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
14bee 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 69 6e   current page in
14bef 20 61 70 50 61 67 65 20 2a 2f 0a 20 20 4d 65 6d   apPage */.  Mem
14bf0 50 61 67 65 20 2a 61 70 50 61 67 65 5b 42 54 43  Page *apPage[BTC
14bf1 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 5d  URSOR_MAX_DEPTH]
14bf2 3b 20 20 2f 2a 20 50 61 67 65 73 20 66 72 6f 6d  ;  /* Pages from
14bf3 20 72 6f 6f 74 20 74 6f 20 63 75 72 72 65 6e 74   root to current
14bf4 20 70 61 67 65 20 2a 2f 0a 20 20 75 31 36 20 61   page */.  u16 a
14bf5 69 49 64 78 5b 42 54 43 55 52 53 4f 52 5f 4d 41  iIdx[BTCURSOR_MA
14bf6 58 5f 44 45 50 54 48 5d 3b 20 20 20 20 20 20 20  X_DEPTH];       
14bf7 20 2f 2a 20 43 75 72 72 65 6e 74 20 69 6e 64 65   /* Current inde
14bf8 78 20 69 6e 20 61 70 50 61 67 65 5b 69 5d 20 2a  x in apPage[i] *
14bf9 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 74 65  /.};../*.** Pote
14bfa 6e 74 69 61 6c 20 76 61 6c 75 65 73 20 66 6f 72  ntial values for
14bfb 20 42 74 43 75 72 73 6f 72 2e 65 53 74 61 74 65   BtCursor.eState
14bfc 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 56  ..**.** CURSOR_V
14bfd 41 4c 49 44 3a 0a 2a 2a 20 20 20 43 75 72 73 6f  ALID:.**   Curso
14bfe 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 76 61  r points to a va
14bff 6c 69 64 20 65 6e 74 72 79 2e 20 67 65 74 50 61  lid entry. getPa
14c00 79 6c 6f 61 64 28 29 20 65 74 63 2e 20 6d 61 79  yload() etc. may
14c01 20 62 65 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a   be called..**.*
14c02 2a 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  * CURSOR_INVALID
14c03 3a 0a 2a 2a 20 20 20 43 75 72 73 6f 72 20 64 6f  :.**   Cursor do
14c04 65 73 20 6e 6f 74 20 70 6f 69 6e 74 20 74 6f 20  es not point to 
14c05 61 20 76 61 6c 69 64 20 65 6e 74 72 79 2e 20 54  a valid entry. T
14c06 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 20 28  his can happen (
14c07 66 6f 72 20 65 78 61 6d 70 6c 65 29 20 0a 2a 2a  for example) .**
14c08 20 20 20 62 65 63 61 75 73 65 20 74 68 65 20 74     because the t
14c09 61 62 6c 65 20 69 73 20 65 6d 70 74 79 20 6f 72  able is empty or
14c0a 20 62 65 63 61 75 73 65 20 42 74 72 65 65 43 75   because BtreeCu
14c0b 72 73 6f 72 46 69 72 73 74 28 29 20 68 61 73 20  rsorFirst() has 
14c0c 6e 6f 74 20 62 65 65 6e 0a 2a 2a 20 20 20 63 61  not been.**   ca
14c0d 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53  lled..**.** CURS
14c0e 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 3a 0a  OR_REQUIRESEEK:.
14c0f 2a 2a 20 20 20 54 68 65 20 74 61 62 6c 65 20 74  **   The table t
14c10 68 61 74 20 74 68 69 73 20 63 75 72 73 6f 72 20  hat this cursor 
14c11 77 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 73 74  was opened on st
14c12 69 6c 6c 20 65 78 69 73 74 73 2c 20 62 75 74 20  ill exists, but 
14c13 68 61 73 20 62 65 65 6e 20 0a 2a 2a 20 20 20 6d  has been .**   m
14c14 6f 64 69 66 69 65 64 20 73 69 6e 63 65 20 74 68  odified since th
14c15 65 20 63 75 72 73 6f 72 20 77 61 73 20 6c 61 73  e cursor was las
14c16 74 20 75 73 65 64 2e 20 54 68 65 20 63 75 72 73  t used. The curs
14c17 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69 73 20 73  or position is s
14c18 61 76 65 64 0a 2a 2a 20 20 20 69 6e 20 76 61 72  aved.**   in var
14c19 69 61 62 6c 65 73 20 42 74 43 75 72 73 6f 72 2e  iables BtCursor.
14c1a 70 4b 65 79 20 61 6e 64 20 42 74 43 75 72 73 6f  pKey and BtCurso
14c1b 72 2e 6e 4b 65 79 2e 20 57 68 65 6e 20 61 20 63  r.nKey. When a c
14c1c 75 72 73 6f 72 20 69 73 20 69 6e 20 0a 2a 2a 20  ursor is in .** 
14c1d 20 20 74 68 69 73 20 73 74 61 74 65 2c 20 72 65    this state, re
14c1e 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74  storeCursorPosit
14c1f 69 6f 6e 28 29 20 63 61 6e 20 62 65 20 63 61 6c  ion() can be cal
14c20 6c 65 64 20 74 6f 20 61 74 74 65 6d 70 74 20 74  led to attempt t
14c21 6f 0a 2a 2a 20 20 20 73 65 65 6b 20 74 68 65 20  o.**   seek the 
14c22 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 73 61  cursor to the sa
14c23 76 65 64 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2a  ved position..**
14c24 0a 2a 2a 20 43 55 52 53 4f 52 5f 46 41 55 4c 54  .** CURSOR_FAULT
14c25 3a 0a 2a 2a 20 20 20 41 20 75 6e 72 65 63 6f 76  :.**   A unrecov
14c26 65 72 61 62 6c 65 20 65 72 72 6f 72 20 28 61 6e  erable error (an
14c27 20 49 2f 4f 20 65 72 72 6f 72 20 6f 72 20 61 20   I/O error or a 
14c28 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 29 20  malloc failure) 
14c29 68 61 73 20 6f 63 63 75 72 72 65 64 0a 2a 2a 20  has occurred.** 
14c2a 20 20 6f 6e 20 61 20 64 69 66 66 65 72 65 6e 74    on a different
14c2b 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74   connection that
14c2c 20 73 68 61 72 65 73 20 74 68 65 20 42 74 53 68   shares the BtSh
14c2d 61 72 65 64 20 63 61 63 68 65 20 77 69 74 68 20  ared cache with 
14c2e 74 68 69 73 0a 2a 2a 20 20 20 63 75 72 73 6f 72  this.**   cursor
14c2f 2e 20 20 54 68 65 20 65 72 72 6f 72 20 68 61 73  .  The error has
14c30 20 6c 65 66 74 20 74 68 65 20 63 61 63 68 65 20   left the cache 
14c31 69 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65  in an inconsiste
14c32 6e 74 20 73 74 61 74 65 2e 0a 2a 2a 20 20 20 44  nt state..**   D
14c33 6f 20 6e 6f 74 68 69 6e 67 20 65 6c 73 65 20 77  o nothing else w
14c34 69 74 68 20 74 68 69 73 20 63 75 72 73 6f 72 2e  ith this cursor.
14c35 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f    Any attempt to
14c36 20 75 73 65 20 74 68 65 20 63 75 72 73 6f 72 0a   use the cursor.
14c37 2a 2a 20 20 20 73 68 6f 75 6c 64 20 72 65 74 75  **   should retu
14c38 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  rn the error cod
14c39 65 20 73 74 6f 72 65 64 20 69 6e 20 42 74 43 75  e stored in BtCu
14c3a 72 73 6f 72 2e 73 6b 69 70 0a 2a 2f 0a 23 64 65  rsor.skip.*/.#de
14c3b 66 69 6e 65 20 43 55 52 53 4f 52 5f 49 4e 56 41  fine CURSOR_INVA
14c3c 4c 49 44 20 20 20 20 20 20 20 20 20 20 20 30 0a  LID           0.
14c3d 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f 56  #define CURSOR_V
14c3e 41 4c 49 44 20 20 20 20 20 20 20 20 20 20 20 20  ALID            
14c3f 20 31 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f   1.#define CURSO
14c40 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 20 20  R_REQUIRESEEK   
14c41 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 43 55      2.#define CU
14c42 52 53 4f 52 5f 46 41 55 4c 54 20 20 20 20 20 20  RSOR_FAULT      
14c43 20 20 20 20 20 20 20 33 0a 0a 2f 2a 20 0a 2a 2a         3../* .**
14c44 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61   The database pa
14c45 67 65 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 42  ge the PENDING_B
14c46 59 54 45 20 6f 63 63 75 70 69 65 73 2e 20 54 68  YTE occupies. Th
14c47 69 73 20 70 61 67 65 20 69 73 20 6e 65 76 65 72  is page is never
14c48 20 75 73 65 64 2e 0a 2a 2f 0a 23 20 64 65 66 69   used..*/.# defi
14c49 6e 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  ne PENDING_BYTE_
14c4a 50 41 47 45 28 70 42 74 29 20 50 41 47 45 52 5f  PAGE(pBt) PAGER_
14c4b 4d 4a 5f 50 47 4e 4f 28 70 42 74 29 0a 0a 2f 2a  MJ_PGNO(pBt)../*
14c4c 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73  .** These macros
14c4d 20 64 65 66 69 6e 65 20 74 68 65 20 6c 6f 63 61   define the loca
14c4e 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 6f 69 6e  tion of the poin
14c4f 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f  ter-map entry fo
14c50 72 20 61 20 0a 2a 2a 20 64 61 74 61 62 61 73 65  r a .** database
14c51 20 70 61 67 65 2e 20 54 68 65 20 66 69 72 73 74   page. The first
14c52 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 65 61 63   argument to eac
14c53 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  h is the number 
14c54 6f 66 20 75 73 61 62 6c 65 0a 2a 2a 20 62 79 74  of usable.** byt
14c55 65 73 20 6f 6e 20 65 61 63 68 20 70 61 67 65 20  es on each page 
14c56 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
14c57 28 6f 66 74 65 6e 20 31 30 32 34 29 2e 20 54 68  (often 1024). Th
14c58 65 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65 0a  e second is the.
14c59 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74  ** page number t
14c5a 6f 20 6c 6f 6f 6b 20 75 70 20 69 6e 20 74 68 65  o look up in the
14c5b 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a 2a   pointer map..**
14c5c 0a 2a 2a 20 50 54 52 4d 41 50 5f 50 41 47 45 4e  .** PTRMAP_PAGEN
14c5d 4f 20 72 65 74 75 72 6e 73 20 74 68 65 20 64 61  O returns the da
14c5e 74 61 62 61 73 65 20 70 61 67 65 20 6e 75 6d 62  tabase page numb
14c5f 65 72 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65  er of the pointe
14c60 72 2d 6d 61 70 0a 2a 2a 20 70 61 67 65 20 74 68  r-map.** page th
14c61 61 74 20 73 74 6f 72 65 73 20 74 68 65 20 72 65  at stores the re
14c62 71 75 69 72 65 64 20 70 6f 69 6e 74 65 72 2e 20  quired pointer. 
14c63 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54  PTRMAP_PTROFFSET
14c64 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68 65 20   returns.** the 
14c65 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20 72 65  offset of the re
14c66 71 75 65 73 74 65 64 20 6d 61 70 20 65 6e 74 72  quested map entr
14c67 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  y..**.** If the 
14c68 70 67 6e 6f 20 61 72 67 75 6d 65 6e 74 20 70 61  pgno argument pa
14c69 73 73 65 64 20 74 6f 20 50 54 52 4d 41 50 5f 50  ssed to PTRMAP_P
14c6a 41 47 45 4e 4f 20 69 73 20 61 20 70 6f 69 6e 74  AGENO is a point
14c6b 65 72 2d 6d 61 70 20 70 61 67 65 2c 0a 2a 2a 20  er-map page,.** 
14c6c 74 68 65 6e 20 70 67 6e 6f 20 69 73 20 72 65 74  then pgno is ret
14c6d 75 72 6e 65 64 2e 20 53 6f 20 28 70 67 6e 6f 3d  urned. So (pgno=
14c6e 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70  =PTRMAP_PAGENO(p
14c6f 67 73 7a 2c 20 70 67 6e 6f 29 29 20 63 61 6e 20  gsz, pgno)) can 
14c70 62 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 74 65  be.** used to te
14c71 73 74 20 69 66 20 70 67 6e 6f 20 69 73 20 61 20  st if pgno is a 
14c72 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65  pointer-map page
14c73 2e 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 20  . PTRMAP_ISPAGE 
14c74 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74 68  implements.** th
14c75 69 73 20 74 65 73 74 2e 0a 2a 2f 0a 23 64 65 66  is test..*/.#def
14c76 69 6e 65 20 50 54 52 4d 41 50 5f 50 41 47 45 4e  ine PTRMAP_PAGEN
14c77 4f 28 70 42 74 2c 20 70 67 6e 6f 29 20 70 74 72  O(pBt, pgno) ptr
14c78 6d 61 70 50 61 67 65 6e 6f 28 70 42 74 2c 20 70  mapPageno(pBt, p
14c79 67 6e 6f 29 0a 23 64 65 66 69 6e 65 20 50 54 52  gno).#define PTR
14c7a 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28 70 67  MAP_PTROFFSET(pg
14c7b 70 74 72 6d 61 70 2c 20 70 67 6e 6f 29 20 28 35  ptrmap, pgno) (5
14c7c 2a 28 70 67 6e 6f 2d 70 67 70 74 72 6d 61 70 2d  *(pgno-pgptrmap-
14c7d 31 29 29 0a 23 64 65 66 69 6e 65 20 50 54 52 4d  1)).#define PTRM
14c7e 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20 70  AP_ISPAGE(pBt, p
14c7f 67 6e 6f 29 20 28 50 54 52 4d 41 50 5f 50 41 47  gno) (PTRMAP_PAG
14c80 45 4e 4f 28 28 70 42 74 29 2c 28 70 67 6e 6f 29  ENO((pBt),(pgno)
14c81 29 3d 3d 28 70 67 6e 6f 29 29 0a 0a 2f 2a 0a 2a  )==(pgno))../*.*
14c82 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  * The pointer ma
14c83 70 20 69 73 20 61 20 6c 6f 6f 6b 75 70 20 74 61  p is a lookup ta
14c84 62 6c 65 20 74 68 61 74 20 69 64 65 6e 74 69 66  ble that identif
14c85 69 65 73 20 74 68 65 20 70 61 72 65 6e 74 20 70  ies the parent p
14c86 61 67 65 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20  age for.** each 
14c87 63 68 69 6c 64 20 70 61 67 65 20 69 6e 20 74 68  child page in th
14c88 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
14c89 20 20 54 68 65 20 70 61 72 65 6e 74 20 70 61 67    The parent pag
14c8a 65 20 69 73 20 74 68 65 20 70 61 67 65 20 74 68  e is the page th
14c8b 61 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 61  at.** contains a
14c8c 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
14c8d 63 68 69 6c 64 2e 20 20 45 76 65 72 79 20 70 61  child.  Every pa
14c8e 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ge in the databa
14c8f 73 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 30  se contains.** 0
14c90 20 6f 72 20 31 20 70 61 72 65 6e 74 20 70 61 67   or 1 parent pag
14c91 65 73 2e 20 20 28 49 6e 20 74 68 69 73 20 63 6f  es.  (In this co
14c92 6e 74 65 78 74 20 27 64 61 74 61 62 61 73 65 20  ntext 'database 
14c93 70 61 67 65 27 20 72 65 66 65 72 73 0a 2a 2a 20  page' refers.** 
14c94 74 6f 20 61 6e 79 20 70 61 67 65 20 74 68 61 74  to any page that
14c95 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20   is not part of 
14c96 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  the pointer map 
14c97 69 74 73 65 6c 66 2e 29 20 20 45 61 63 68 20 70  itself.)  Each p
14c98 6f 69 6e 74 65 72 20 6d 61 70 0a 2a 2a 20 65 6e  ointer map.** en
14c99 74 72 79 20 63 6f 6e 73 69 73 74 73 20 6f 66 20  try consists of 
14c9a 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 27 74  a single byte 't
14c9b 79 70 65 27 20 61 6e 64 20 61 20 34 20 62 79 74  ype' and a 4 byt
14c9c 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75  e parent page nu
14c9d 6d 62 65 72 2e 0a 2a 2a 20 54 68 65 20 50 54 52  mber..** The PTR
14c9e 4d 41 50 5f 58 58 58 20 69 64 65 6e 74 69 66 69  MAP_XXX identifi
14c9f 65 72 73 20 62 65 6c 6f 77 20 61 72 65 20 74 68  ers below are th
14ca0 65 20 76 61 6c 69 64 20 74 79 70 65 73 2e 0a 2a  e valid types..*
14ca1 2a 0a 2a 2a 20 54 68 65 20 70 75 72 70 6f 73 65  *.** The purpose
14ca2 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72 20   of the pointer 
14ca3 6d 61 70 20 69 73 20 74 6f 20 66 61 63 69 6c 69  map is to facili
14ca4 74 79 20 6d 6f 76 69 6e 67 20 70 61 67 65 73 20  ty moving pages 
14ca5 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 70 6f 73 69  from one.** posi
14ca6 74 69 6f 6e 20 69 6e 20 74 68 65 20 66 69 6c 65  tion in the file
14ca7 20 74 6f 20 61 6e 6f 74 68 65 72 20 61 73 20 70   to another as p
14ca8 61 72 74 20 6f 66 20 61 75 74 6f 76 61 63 75 75  art of autovacuu
14ca9 6d 2e 20 20 57 68 65 6e 20 61 20 70 61 67 65 0a  m.  When a page.
14caa 2a 2a 20 69 73 20 6d 6f 76 65 64 2c 20 74 68 65  ** is moved, the
14cab 20 70 6f 69 6e 74 65 72 20 69 6e 20 69 74 73 20   pointer in its 
14cac 70 61 72 65 6e 74 20 6d 75 73 74 20 62 65 20 75  parent must be u
14cad 70 64 61 74 65 64 20 74 6f 20 70 6f 69 6e 74 20  pdated to point 
14cae 74 6f 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 6f  to the.** new lo
14caf 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 70 6f 69  cation.  The poi
14cb0 6e 74 65 72 20 6d 61 70 20 69 73 20 75 73 65 64  nter map is used
14cb1 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 70   to locate the p
14cb2 61 72 65 6e 74 20 70 61 67 65 20 71 75 69 63 6b  arent page quick
14cb3 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50  ly..**.** PTRMAP
14cb4 5f 52 4f 4f 54 50 41 47 45 3a 20 54 68 65 20 64  _ROOTPAGE: The d
14cb5 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20  atabase page is 
14cb6 61 20 72 6f 6f 74 2d 70 61 67 65 2e 20 54 68 65  a root-page. The
14cb7 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73 20   page-number is 
14cb8 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  not.**          
14cb9 20 20 20 20 20 20 20 20 75 73 65 64 20 69 6e 20          used in 
14cba 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a  this case..**.**
14cbb 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45   PTRMAP_FREEPAGE
14cbc 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70  : The database p
14cbd 61 67 65 20 69 73 20 61 6e 20 75 6e 75 73 65 64  age is an unused
14cbe 20 28 66 72 65 65 29 20 70 61 67 65 2e 20 54 68   (free) page. Th
14cbf 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 0a 2a  e page-number .*
14cc0 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
14cc1 20 20 20 69 73 20 6e 6f 74 20 75 73 65 64 20 69     is not used i
14cc2 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a  n this case..**.
14cc3 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  ** PTRMAP_OVERFL
14cc4 4f 57 31 3a 20 54 68 65 20 64 61 74 61 62 61 73  OW1: The databas
14cc5 65 20 70 61 67 65 20 69 73 20 74 68 65 20 66 69  e page is the fi
14cc6 72 73 74 20 70 61 67 65 20 69 6e 20 61 20 6c 69  rst page in a li
14cc7 73 74 20 6f 66 20 0a 2a 2a 20 20 20 20 20 20 20  st of .**       
14cc8 20 20 20 20 20 20 20 20 20 20 20 20 6f 76 65 72              over
14cc9 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65 20  flow pages. The 
14cca 70 61 67 65 20 6e 75 6d 62 65 72 20 69 64 65 6e  page number iden
14ccb 74 69 66 69 65 73 20 74 68 65 20 70 61 67 65 20  tifies the page 
14ccc 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  that.**         
14ccd 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 61 69            contai
14cce 6e 73 20 74 68 65 20 63 65 6c 6c 20 77 69 74 68  ns the cell with
14ccf 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
14cd0 69 73 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  is overflow page
14cd1 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f  ..**.** PTRMAP_O
14cd2 56 45 52 46 4c 4f 57 32 3a 20 54 68 65 20 64 61  VERFLOW2: The da
14cd3 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20 74  tabase page is t
14cd4 68 65 20 73 65 63 6f 6e 64 20 6f 72 20 6c 61 74  he second or lat
14cd5 65 72 20 70 61 67 65 20 69 6e 20 61 20 6c 69 73  er page in a lis
14cd6 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20  t of.**         
14cd7 20 20 20 20 20 20 20 20 20 20 6f 76 65 72 66 6c            overfl
14cd8 6f 77 20 70 61 67 65 73 2e 20 54 68 65 20 70 61  ow pages. The pa
14cd9 67 65 2d 6e 75 6d 62 65 72 20 69 64 65 6e 74 69  ge-number identi
14cda 66 69 65 73 20 74 68 65 20 70 72 65 76 69 6f 75  fies the previou
14cdb 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  s.**            
14cdc 20 20 20 20 20 20 20 70 61 67 65 20 69 6e 20 74         page in t
14cdd 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  he overflow page
14cde 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 50 54 52   list..**.** PTR
14cdf 4d 41 50 5f 42 54 52 45 45 3a 20 54 68 65 20 64  MAP_BTREE: The d
14ce0 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20  atabase page is 
14ce1 61 20 6e 6f 6e 2d 72 6f 6f 74 20 62 74 72 65 65  a non-root btree
14ce2 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 20   page. The page 
14ce3 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 20 20 20 20  number.**       
14ce4 20 20 20 20 20 20 20 20 69 64 65 6e 74 69 66 69          identifi
14ce5 65 73 20 74 68 65 20 70 61 72 65 6e 74 20 70 61  es the parent pa
14ce6 67 65 20 69 6e 20 74 68 65 20 62 74 72 65 65 2e  ge in the btree.
14ce7 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 52 4d  .*/.#define PTRM
14ce8 41 50 5f 52 4f 4f 54 50 41 47 45 20 31 0a 23 64  AP_ROOTPAGE 1.#d
14ce9 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 46 52 45  efine PTRMAP_FRE
14cea 45 50 41 47 45 20 32 0a 23 64 65 66 69 6e 65 20  EPAGE 2.#define 
14ceb 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31  PTRMAP_OVERFLOW1
14cec 20 33 0a 23 64 65 66 69 6e 65 20 50 54 52 4d 41   3.#define PTRMA
14ced 50 5f 4f 56 45 52 46 4c 4f 57 32 20 34 0a 23 64  P_OVERFLOW2 4.#d
14cee 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 42 54 52  efine PTRMAP_BTR
14cef 45 45 20 35 0a 0a 2f 2a 20 41 20 62 75 6e 63 68  EE 5../* A bunch
14cf0 20 6f 66 20 61 73 73 65 72 74 28 29 20 73 74 61   of assert() sta
14cf1 74 65 6d 65 6e 74 73 20 74 6f 20 63 68 65 63 6b  tements to check
14cf2 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
14cf3 20 73 74 61 74 65 20 76 61 72 69 61 62 6c 65 73   state variables
14cf4 0a 2a 2a 20 6f 66 20 68 61 6e 64 6c 65 20 70 20  .** of handle p 
14cf5 28 74 79 70 65 20 42 74 72 65 65 2a 29 20 61 72  (type Btree*) ar
14cf6 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 63 6f 6e  e internally con
14cf7 73 69 73 74 65 6e 74 2e 0a 2a 2f 0a 23 64 65 66  sistent..*/.#def
14cf8 69 6e 65 20 62 74 72 65 65 49 6e 74 65 67 72 69  ine btreeIntegri
14cf9 74 79 28 70 29 20 5c 0a 20 20 61 73 73 65 72 74  ty(p) \.  assert
14cfa 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e  ( p->pBt->inTran
14cfb 73 61 63 74 69 6f 6e 21 3d 54 52 41 4e 53 5f 4e  saction!=TRANS_N
14cfc 4f 4e 45 20 7c 7c 20 70 2d 3e 70 42 74 2d 3e 6e  ONE || p->pBt->n
14cfd 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 30 20 29  Transaction==0 )
14cfe 3b 20 5c 0a 20 20 61 73 73 65 72 74 28 20 70 2d  ; \.  assert( p-
14cff 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74  >pBt->inTransact
14d00 69 6f 6e 3e 3d 70 2d 3e 69 6e 54 72 61 6e 73 20  ion>=p->inTrans 
14d01 29 3b 20 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ); .../*.** The 
14d02 49 53 41 55 54 4f 56 41 43 55 55 4d 20 6d 61 63  ISAUTOVACUUM mac
14d03 72 6f 20 69 73 20 75 73 65 64 20 77 69 74 68 69  ro is used withi
14d04 6e 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f  n balance_nonroo
14d05 74 28 29 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  t() to determine
14d06 0a 2a 2a 20 69 66 20 74 68 65 20 64 61 74 61 62  .** if the datab
14d07 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74  ase supports aut
14d08 6f 2d 76 61 63 75 75 6d 20 6f 72 20 6e 6f 74 2e  o-vacuum or not.
14d09 20 42 65 63 61 75 73 65 20 69 74 20 69 73 20 75   Because it is u
14d0a 73 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 61 6e  sed.** within an
14d0b 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74   expression that
14d0c 20 69 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20   is an argument 
14d0d 74 6f 20 61 6e 6f 74 68 65 72 20 6d 61 63 72 6f  to another macro
14d0e 20 0a 2a 2a 20 28 73 71 6c 69 74 65 4d 61 6c 6c   .** (sqliteMall
14d0f 6f 63 52 61 77 29 2c 20 69 74 20 69 73 20 6e 6f  ocRaw), it is no
14d10 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75 73  t possible to us
14d11 65 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 63 6f  e conditional co
14d12 6d 70 69 6c 61 74 69 6f 6e 2e 0a 2a 2a 20 53 6f  mpilation..** So
14d13 2c 20 74 68 69 73 20 6d 61 63 72 6f 20 69 73 20  , this macro is 
14d14 64 65 66 69 6e 65 64 20 69 6e 73 74 65 61 64 2e  defined instead.
14d15 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
14d16 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
14d17 55 4d 0a 23 64 65 66 69 6e 65 20 49 53 41 55 54  UM.#define ISAUT
14d18 4f 56 41 43 55 55 4d 20 28 70 42 74 2d 3e 61 75  OVACUUM (pBt->au
14d19 74 6f 56 61 63 75 75 6d 29 0a 23 65 6c 73 65 0a  toVacuum).#else.
14d1a 23 64 65 66 69 6e 65 20 49 53 41 55 54 4f 56 41  #define ISAUTOVA
14d1b 43 55 55 4d 20 30 0a 23 65 6e 64 69 66 0a 0a 0a  CUUM 0.#endif...
14d1c 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75 63  /*.** This struc
14d1d 74 75 72 65 20 69 73 20 70 61 73 73 65 64 20 61  ture is passed a
14d1e 72 6f 75 6e 64 20 74 68 72 6f 75 67 68 20 61 6c  round through al
14d1f 6c 20 74 68 65 20 73 61 6e 69 74 79 20 63 68 65  l the sanity che
14d20 63 6b 69 6e 67 20 72 6f 75 74 69 6e 65 73 0a 2a  cking routines.*
14d21 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65  * in order to ke
14d22 65 70 20 74 72 61 63 6b 20 6f 66 20 73 6f 6d 65  ep track of some
14d23 20 67 6c 6f 62 61 6c 20 73 74 61 74 65 20 69 6e   global state in
14d24 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79  formation..*/.ty
14d25 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e 74  pedef struct Int
14d26 65 67 72 69 74 79 43 6b 20 49 6e 74 65 67 72 69  egrityCk Integri
14d27 74 79 43 6b 3b 0a 73 74 72 75 63 74 20 49 6e 74  tyCk;.struct Int
14d28 65 67 72 69 74 79 43 6b 20 7b 0a 20 20 42 74 53  egrityCk {.  BtS
14d29 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 2f  hared *pBt;    /
14d2a 2a 20 54 68 65 20 74 72 65 65 20 62 65 69 6e 67  * The tree being
14d2b 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a   checked out */.
14d2c 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b    Pager *pPager;
14d2d 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73 6f 63      /* The assoc
14d2e 69 61 74 65 64 20 70 61 67 65 72 2e 20 20 41 6c  iated pager.  Al
14d2f 73 6f 20 61 63 63 65 73 73 69 62 6c 65 20 62 79  so accessible by
14d30 20 70 42 74 2d 3e 70 50 61 67 65 72 20 2a 2f 0a   pBt->pPager */.
14d31 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 20 20 20    Pgno nPage;   
14d32 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
14d33 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64 61   pages in the da
14d34 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20  tabase */.  int 
14d35 2a 61 6e 52 65 66 3b 20 20 20 20 20 20 20 2f 2a  *anRef;       /*
14d36 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73   Number of times
14d37 20 65 61 63 68 20 70 61 67 65 20 69 73 20 72 65   each page is re
14d38 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 69 6e  ferenced */.  in
14d39 74 20 6d 78 45 72 72 3b 20 20 20 20 20 20 20 20  t mxErr;        
14d3a 2f 2a 20 53 74 6f 70 20 61 63 63 75 6d 75 6c 61  /* Stop accumula
14d3b 74 69 6e 67 20 65 72 72 6f 72 73 20 77 68 65 6e  ting errors when
14d3c 20 74 68 69 73 20 72 65 61 63 68 65 73 20 7a 65   this reaches ze
14d3d 72 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72  ro */.  int nErr
14d3e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  ;         /* Num
14d3f 62 65 72 20 6f 66 20 6d 65 73 73 61 67 65 73 20  ber of messages 
14d40 77 72 69 74 74 65 6e 20 74 6f 20 7a 45 72 72 4d  written to zErrM
14d41 73 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69  sg so far */.  i
14d42 6e 74 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b  nt mallocFailed;
14d43 20 2f 2a 20 41 20 6d 65 6d 6f 72 79 20 61 6c 6c   /* A memory all
14d44 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 68 61  ocation error ha
14d45 73 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 20 20  s occurred */.  
14d46 53 74 72 41 63 63 75 6d 20 65 72 72 4d 73 67 3b  StrAccum errMsg;
14d47 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 20    /* Accumulate 
14d48 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67  the error messag
14d49 65 20 74 65 78 74 20 68 65 72 65 20 2a 2f 0a 7d  e text here */.}
14d4a 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f 72  ;../*.** Read or
14d4b 20 77 72 69 74 65 20 61 20 74 77 6f 2d 20 61 6e   write a two- an
14d4c 64 20 66 6f 75 72 2d 62 79 74 65 20 62 69 67 2d  d four-byte big-
14d4d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20 76  endian integer v
14d4e 61 6c 75 65 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  alues..*/.#defin
14d4f 65 20 67 65 74 32 62 79 74 65 28 78 29 20 20 20  e get2byte(x)   
14d50 28 28 78 29 5b 30 5d 3c 3c 38 20 7c 20 28 78 29  ((x)[0]<<8 | (x)
14d51 5b 31 5d 29 0a 23 64 65 66 69 6e 65 20 70 75 74  [1]).#define put
14d52 32 62 79 74 65 28 70 2c 76 29 20 28 28 70 29 5b  2byte(p,v) ((p)[
14d53 30 5d 20 3d 20 28 75 38 29 28 28 76 29 3e 3e 38  0] = (u8)((v)>>8
14d54 29 2c 20 28 70 29 5b 31 5d 20 3d 20 28 75 38 29  ), (p)[1] = (u8)
14d55 28 76 29 29 0a 23 64 65 66 69 6e 65 20 67 65 74  (v)).#define get
14d56 34 62 79 74 65 20 73 71 6c 69 74 65 33 47 65 74  4byte sqlite3Get
14d57 34 62 79 74 65 0a 23 64 65 66 69 6e 65 20 70 75  4byte.#define pu
14d58 74 34 62 79 74 65 20 73 71 6c 69 74 65 33 50 75  t4byte sqlite3Pu
14d59 74 34 62 79 74 65 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  t4byte../*******
14d5a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62  ******* End of b
14d5b 74 72 65 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  treeInt.h ******
14d5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14d5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14d5e 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
14d5f 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
14d60 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
14d61 20 6f 66 66 20 69 6e 20 62 74 6d 75 74 65 78 2e   off in btmutex.
14d62 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
14d63 2a 2a 2a 2a 2a 2a 2f 0a 23 69 66 6e 64 65 66 20  ******/.#ifndef 
14d64 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
14d65 45 44 5f 43 41 43 48 45 0a 23 69 66 20 53 51 4c  ED_CACHE.#if SQL
14d66 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 0a  ITE_THREADSAFE..
14d67 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65  /*.** Obtain the
14d68 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 20   BtShared mutex 
14d69 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
14d6a 42 2d 54 72 65 65 20 68 61 6e 64 6c 65 20 70 2e  B-Tree handle p.
14d6b 20 41 6c 73 6f 2c 0a 2a 2a 20 73 65 74 20 42 74   Also,.** set Bt
14d6c 53 68 61 72 65 64 2e 64 62 20 74 6f 20 74 68 65  Shared.db to the
14d6d 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
14d6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
14d6f 20 70 20 61 6e 64 20 74 68 65 0a 2a 2a 20 70 2d   p and the.** p-
14d70 3e 6c 6f 63 6b 65 64 20 62 6f 6f 6c 65 61 6e 20  >locked boolean 
14d71 74 6f 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74  to true..*/.stat
14d72 69 63 20 76 6f 69 64 20 6c 6f 63 6b 42 74 72 65  ic void lockBtre
14d73 65 4d 75 74 65 78 28 42 74 72 65 65 20 2a 70 29  eMutex(Btree *p)
14d74 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c  {.  assert( p->l
14d75 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 20 61 73  ocked==0 );.  as
14d76 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
14d77 74 65 78 5f 6e 6f 74 68 65 6c 64 28 70 2d 3e 70  tex_notheld(p->p
14d78 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
14d79 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
14d7a 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62  mutex_held(p->db
14d7b 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 73  ->mutex) );..  s
14d7c 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
14d7d 65 72 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78  er(p->pBt->mutex
14d7e 29 3b 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20  );.  p->pBt->db 
14d7f 3d 20 70 2d 3e 64 62 3b 0a 20 20 70 2d 3e 6c 6f  = p->db;.  p->lo
14d80 63 6b 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a  cked = 1;.}../*.
14d81 2a 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 42  ** Release the B
14d82 74 53 68 61 72 65 64 20 6d 75 74 65 78 20 61 73  tShared mutex as
14d83 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 42 2d  sociated with B-
14d84 54 72 65 65 20 68 61 6e 64 6c 65 20 70 20 61 6e  Tree handle p an
14d85 64 0a 2a 2a 20 63 6c 65 61 72 20 74 68 65 20 70  d.** clear the p
14d86 2d 3e 6c 6f 63 6b 65 64 20 62 6f 6f 6c 65 61 6e  ->locked boolean
14d87 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
14d88 20 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65   unlockBtreeMute
14d89 78 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 61  x(Btree *p){.  a
14d8a 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64  ssert( p->locked
14d8b 3d 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==1 );.  assert(
14d8c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
14d8d 65 6c 64 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65  eld(p->pBt->mute
14d8e 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
14d8f 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
14d90 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ld(p->db->mutex)
14d91 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
14d92 3e 64 62 3d 3d 70 2d 3e 70 42 74 2d 3e 64 62 20  >db==p->pBt->db 
14d93 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  );..  sqlite3_mu
14d94 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 42 74  tex_leave(p->pBt
14d95 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6c  ->mutex);.  p->l
14d96 6f 63 6b 65 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  ocked = 0;.}../*
14d97 0a 2a 2a 20 45 6e 74 65 72 20 61 20 6d 75 74 65  .** Enter a mute
14d98 78 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 42  x on the given B
14d99 54 72 65 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a  Tree object..**.
14d9a 2a 2a 20 49 66 20 74 68 65 20 6f 62 6a 65 63 74  ** If the object
14d9b 20 69 73 20 6e 6f 74 20 73 68 61 72 61 62 6c 65   is not sharable
14d9c 2c 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 20  , then no mutex 
14d9d 69 73 20 65 76 65 72 20 72 65 71 75 69 72 65 64  is ever required
14d9e 0a 2a 2a 20 61 6e 64 20 74 68 69 73 20 72 6f 75  .** and this rou
14d9f 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tine is a no-op.
14da0 20 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67    The underlying
14da1 20 6d 75 74 65 78 20 69 73 20 6e 6f 6e 2d 72 65   mutex is non-re
14da2 63 75 72 73 69 76 65 2e 0a 2a 2a 20 42 75 74 20  cursive..** But 
14da3 77 65 20 6b 65 65 70 20 61 20 72 65 66 65 72 65  we keep a refere
14da4 6e 63 65 20 63 6f 75 6e 74 20 69 6e 20 42 74 72  nce count in Btr
14da5 65 65 2e 77 61 6e 74 54 6f 4c 6f 63 6b 20 73 6f  ee.wantToLock so
14da6 20 74 68 65 20 62 65 68 61 76 69 6f 72 0a 2a 2a   the behavior.**
14da7 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 61   of this interfa
14da8 63 65 20 69 73 20 72 65 63 75 72 73 69 76 65 2e  ce is recursive.
14da9 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69 64 20  .**.** To avoid 
14daa 64 65 61 64 6c 6f 63 6b 73 2c 20 6d 75 6c 74 69  deadlocks, multi
14dab 70 6c 65 20 42 74 72 65 65 73 20 61 72 65 20 6c  ple Btrees are l
14dac 6f 63 6b 65 64 20 69 6e 20 74 68 65 20 73 61 6d  ocked in the sam
14dad 65 20 6f 72 64 65 72 0a 2a 2a 20 62 79 20 61 6c  e order.** by al
14dae 6c 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  l database conne
14daf 63 74 69 6f 6e 73 2e 20 20 54 68 65 20 70 2d 3e  ctions.  The p->
14db0 70 4e 65 78 74 20 69 73 20 61 20 6c 69 73 74 20  pNext is a list 
14db1 6f 66 20 6f 74 68 65 72 0a 2a 2a 20 42 74 72 65  of other.** Btre
14db2 65 73 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20  es belonging to 
14db3 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
14db4 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 73 20  e connection as 
14db5 74 68 65 20 70 20 42 74 72 65 65 0a 2a 2a 20 77  the p Btree.** w
14db6 68 69 63 68 20 6e 65 65 64 20 74 6f 20 62 65 20  hich need to be 
14db7 6c 6f 63 6b 65 64 20 61 66 74 65 72 20 70 2e 20  locked after p. 
14db8 20 49 66 20 77 65 20 63 61 6e 6e 6f 74 20 67 65   If we cannot ge
14db9 74 20 61 20 6c 6f 63 6b 20 6f 6e 0a 2a 2a 20 70  t a lock on.** p
14dba 2c 20 74 68 65 6e 20 66 69 72 73 74 20 75 6e 6c  , then first unl
14dbb 6f 63 6b 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f  ock all of the o
14dbc 74 68 65 72 73 20 6f 6e 20 70 2d 3e 70 4e 65 78  thers on p->pNex
14dbd 74 2c 20 74 68 65 6e 20 77 61 69 74 0a 2a 2a 20  t, then wait.** 
14dbe 66 6f 72 20 74 68 65 20 6c 6f 63 6b 20 74 6f 20  for the lock to 
14dbf 62 65 63 6f 6d 65 20 61 76 61 69 6c 61 62 6c 65  become available
14dc0 20 6f 6e 20 70 2c 20 74 68 65 6e 20 72 65 6c 6f   on p, then relo
14dc1 63 6b 20 61 6c 6c 20 6f 66 20 74 68 65 0a 2a 2a  ck all of the.**
14dc2 20 73 75 62 73 65 71 75 65 6e 74 20 42 74 72 65   subsequent Btre
14dc3 65 73 20 74 68 61 74 20 64 65 73 69 72 65 20 61  es that desire a
14dc4 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45   lock..*/.SQLITE
14dc5 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
14dc6 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
14dc7 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 72  Btree *p){.  Btr
14dc8 65 65 20 2a 70 4c 61 74 65 72 3b 0a 0a 20 20 2f  ee *pLater;..  /
14dc9 2a 20 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e  * Some basic san
14dca 69 74 79 20 63 68 65 63 6b 69 6e 67 20 6f 6e 20  ity checking on 
14dcb 74 68 65 20 42 74 72 65 65 2e 20 20 54 68 65 20  the Btree.  The 
14dcc 6c 69 73 74 20 6f 66 20 42 74 72 65 65 73 0a 20  list of Btrees. 
14dcd 20 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79   ** connected by
14dce 20 70 4e 65 78 74 20 61 6e 64 20 70 50 72 65 76   pNext and pPrev
14dcf 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 20 73 6f   should be in so
14dd0 72 74 65 64 20 6f 72 64 65 72 20 62 79 0a 20 20  rted order by.  
14dd1 2a 2a 20 42 74 72 65 65 2e 70 42 74 20 76 61 6c  ** Btree.pBt val
14dd2 75 65 2e 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73  ue. All elements
14dd3 20 6f 66 20 74 68 65 20 6c 69 73 74 20 73 68 6f   of the list sho
14dd4 75 6c 64 20 62 65 6c 6f 6e 67 20 74 6f 0a 20 20  uld belong to.  
14dd5 2a 2a 20 74 68 65 20 73 61 6d 65 20 63 6f 6e 6e  ** the same conn
14dd6 65 63 74 69 6f 6e 2e 20 4f 6e 6c 79 20 73 68 61  ection. Only sha
14dd7 72 65 64 20 42 74 72 65 65 73 20 61 72 65 20 6f  red Btrees are o
14dd8 6e 20 74 68 65 20 6c 69 73 74 2e 20 2a 2f 0a 20  n the list. */. 
14dd9 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4e 65 78   assert( p->pNex
14dda 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65 78 74  t==0 || p->pNext
14ddb 2d 3e 70 42 74 3e 70 2d 3e 70 42 74 20 29 3b 0a  ->pBt>p->pBt );.
14ddc 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50 72    assert( p->pPr
14ddd 65 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72 65  ev==0 || p->pPre
14dde 76 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 3b  v->pBt<p->pBt );
14ddf 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4e  .  assert( p->pN
14de0 65 78 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 4e 65  ext==0 || p->pNe
14de1 78 74 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b  xt->db==p->db );
14de2 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 50  .  assert( p->pP
14de3 72 65 76 3d 3d 30 20 7c 7c 20 70 2d 3e 70 50 72  rev==0 || p->pPr
14de4 65 76 2d 3e 64 62 3d 3d 70 2d 3e 64 62 20 29 3b  ev->db==p->db );
14de5 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68  .  assert( p->sh
14de6 61 72 61 62 6c 65 20 7c 7c 20 28 70 2d 3e 70 4e  arable || (p->pN
14de7 65 78 74 3d 3d 30 20 26 26 20 70 2d 3e 70 50 72  ext==0 && p->pPr
14de8 65 76 3d 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20  ev==0) );..  /* 
14de9 43 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 69 6e  Check for lockin
14dea 67 20 63 6f 6e 73 69 73 74 65 6e 63 79 20 2a 2f  g consistency */
14deb 0a 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e 6c  .  assert( !p->l
14dec 6f 63 6b 65 64 20 7c 7c 20 70 2d 3e 77 61 6e 74  ocked || p->want
14ded 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20 20 61 73  ToLock>0 );.  as
14dee 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c  sert( p->sharabl
14def 65 20 7c 7c 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f  e || p->wantToLo
14df0 63 6b 3d 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 57  ck==0 );..  /* W
14df1 65 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64 79  e should already
14df2 20 68 6f 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20   hold a lock on 
14df3 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
14df4 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73  nection */.  ass
14df5 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
14df6 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d  ex_held(p->db->m
14df7 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 55  utex) );..  /* U
14df8 6e 6c 65 73 73 20 74 68 65 20 64 61 74 61 62 61  nless the databa
14df9 73 65 20 69 73 20 73 68 61 72 61 62 6c 65 20 61  se is sharable a
14dfa 6e 64 20 75 6e 6c 6f 63 6b 65 64 2c 20 74 68 65  nd unlocked, the
14dfb 6e 20 42 74 53 68 61 72 65 64 2e 64 62 0a 20 20  n BtShared.db.  
14dfc 2a 2a 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64  ** should alread
14dfd 79 20 62 65 20 73 65 74 20 63 6f 72 72 65 63 74  y be set correct
14dfe 6c 79 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ly. */.  assert(
14dff 20 28 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 26   (p->locked==0 &
14e00 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 29 20 7c  & p->sharable) |
14e01 7c 20 70 2d 3e 70 42 74 2d 3e 64 62 3d 3d 70 2d  | p->pBt->db==p-
14e02 3e 64 62 20 29 3b 0a 0a 20 20 69 66 28 20 21 70  >db );..  if( !p
14e03 2d 3e 73 68 61 72 61 62 6c 65 20 29 20 72 65 74  ->sharable ) ret
14e04 75 72 6e 3b 0a 20 20 70 2d 3e 77 61 6e 74 54 6f  urn;.  p->wantTo
14e05 4c 6f 63 6b 2b 2b 3b 0a 20 20 69 66 28 20 70 2d  Lock++;.  if( p-
14e06 3e 6c 6f 63 6b 65 64 20 29 20 72 65 74 75 72 6e  >locked ) return
14e07 3b 0a 0a 20 20 2f 2a 20 49 6e 20 6d 6f 73 74 20  ;..  /* In most 
14e08 63 61 73 65 73 2c 20 77 65 20 73 68 6f 75 6c 64  cases, we should
14e09 20 62 65 20 61 62 6c 65 20 74 6f 20 61 63 71 75   be able to acqu
14e0a 69 72 65 20 74 68 65 20 6c 6f 63 6b 20 77 65 0a  ire the lock we.
14e0b 20 20 2a 2a 20 77 61 6e 74 20 77 69 74 68 6f 75    ** want withou
14e0c 74 20 68 61 76 69 6e 67 20 74 6f 20 67 6f 20 74  t having to go t
14e0d 68 72 6f 75 67 68 74 20 74 68 65 20 61 73 63 65  hrought the asce
14e0e 6e 64 69 6e 67 20 6c 6f 63 6b 0a 20 20 2a 2a 20  nding lock.  ** 
14e0f 70 72 6f 63 65 64 75 72 65 20 74 68 61 74 20 66  procedure that f
14e10 6f 6c 6c 6f 77 73 2e 20 20 4a 75 73 74 20 62 65  ollows.  Just be
14e11 20 73 75 72 65 20 6e 6f 74 20 74 6f 20 62 6c 6f   sure not to blo
14e12 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  ck..  */.  if( s
14e13 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
14e14 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3d  (p->pBt->mutex)=
14e15 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
14e16 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70    p->pBt->db = p
14e17 2d 3e 64 62 3b 0a 20 20 20 20 70 2d 3e 6c 6f 63  ->db;.    p->loc
14e18 6b 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74  ked = 1;.    ret
14e19 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54  urn;.  }..  /* T
14e1a 6f 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b  o avoid deadlock
14e1b 2c 20 66 69 72 73 74 20 72 65 6c 65 61 73 65 20  , first release 
14e1c 61 6c 6c 20 6c 6f 63 6b 73 20 77 69 74 68 20 61  all locks with a
14e1d 20 6c 61 72 67 65 72 0a 20 20 2a 2a 20 42 74 53   larger.  ** BtS
14e1e 68 61 72 65 64 20 61 64 64 72 65 73 73 2e 20 20  hared address.  
14e1f 54 68 65 6e 20 61 63 71 75 69 72 65 20 6f 75 72  Then acquire our
14e20 20 6c 6f 63 6b 2e 20 20 54 68 65 6e 20 72 65 61   lock.  Then rea
14e21 63 71 75 69 72 65 0a 20 20 2a 2a 20 74 68 65 20  cquire.  ** the 
14e22 6f 74 68 65 72 20 42 74 53 68 61 72 65 64 20 6c  other BtShared l
14e23 6f 63 6b 73 20 74 68 61 74 20 77 65 20 75 73 65  ocks that we use
14e24 64 20 74 6f 20 68 6f 6c 64 20 69 6e 20 61 73 63  d to hold in asc
14e25 65 6e 64 69 6e 67 0a 20 20 2a 2a 20 6f 72 64 65  ending.  ** orde
14e26 72 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 4c  r..  */.  for(pL
14e27 61 74 65 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70  ater=p->pNext; p
14e28 4c 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c  Later; pLater=pL
14e29 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  ater->pNext){.  
14e2a 20 20 61 73 73 65 72 74 28 20 70 4c 61 74 65 72    assert( pLater
14e2b 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a 20 20  ->sharable );.  
14e2c 20 20 61 73 73 65 72 74 28 20 70 4c 61 74 65 72    assert( pLater
14e2d 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 4c  ->pNext==0 || pL
14e2e 61 74 65 72 2d 3e 70 4e 65 78 74 2d 3e 70 42 74  ater->pNext->pBt
14e2f 3e 70 4c 61 74 65 72 2d 3e 70 42 74 20 29 3b 0a  >pLater->pBt );.
14e30 20 20 20 20 61 73 73 65 72 74 28 20 21 70 4c 61      assert( !pLa
14e31 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70  ter->locked || p
14e32 4c 61 74 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63  Later->wantToLoc
14e33 6b 3e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70  k>0 );.    if( p
14e34 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b  Later->locked ){
14e35 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72  .      unlockBtr
14e36 65 65 4d 75 74 65 78 28 70 4c 61 74 65 72 29 3b  eeMutex(pLater);
14e37 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 6c 6f 63  .    }.  }.  loc
14e38 6b 42 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a  kBtreeMutex(p);.
14e39 20 20 66 6f 72 28 70 4c 61 74 65 72 3d 70 2d 3e    for(pLater=p->
14e3a 70 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b 20 70  pNext; pLater; p
14e3b 4c 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e 70 4e  Later=pLater->pN
14e3c 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 4c  ext){.    if( pL
14e3d 61 74 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b  ater->wantToLock
14e3e 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63 6b 42 74   ){.      lockBt
14e3f 72 65 65 4d 75 74 65 78 28 70 4c 61 74 65 72 29  reeMutex(pLater)
14e40 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
14e41 2a 0a 2a 2a 20 45 78 69 74 20 74 68 65 20 72 65  *.** Exit the re
14e42 63 75 72 73 69 76 65 20 6d 75 74 65 78 20 6f 6e  cursive mutex on
14e43 20 61 20 42 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c   a Btree..*/.SQL
14e44 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
14e45 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
14e46 76 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20  ve(Btree *p){.  
14e47 69 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20  if( p->sharable 
14e48 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
14e49 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29  ->wantToLock>0 )
14e4a 3b 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c  ;.    p->wantToL
14e4b 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70  ock--;.    if( p
14e4c 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20  ->wantToLock==0 
14e4d 29 7b 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42  ){.      unlockB
14e4e 74 72 65 65 4d 75 74 65 78 28 70 29 3b 0a 20 20  treeMutex(p);.  
14e4f 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64    }.  }.}..#ifnd
14e50 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20  ef NDEBUG./*.** 
14e51 52 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74  Return true if t
14e52 68 65 20 42 74 53 68 61 72 65 64 20 6d 75 74 65  he BtShared mute
14e53 78 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65  x is held on the
14e54 20 62 74 72 65 65 2c 20 6f 72 20 69 66 20 74 68   btree, or if th
14e55 65 0a 2a 2a 20 42 2d 54 72 65 65 20 69 73 20 6e  e.** B-Tree is n
14e56 6f 74 20 6d 61 72 6b 65 64 20 61 73 20 73 68 61  ot marked as sha
14e57 72 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  rable..**.** Thi
14e58 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65  s routine is use
14e59 64 20 6f 6e 6c 79 20 66 72 6f 6d 20 77 69 74 68  d only from with
14e5a 69 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74  in assert() stat
14e5b 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ements..*/.SQLIT
14e5c 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
14e5d 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d  lite3BtreeHoldsM
14e5e 75 74 65 78 28 42 74 72 65 65 20 2a 70 29 7b 0a  utex(Btree *p){.
14e5f 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61    assert( p->sha
14e60 72 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c  rable==0 || p->l
14e61 6f 63 6b 65 64 3d 3d 30 20 7c 7c 20 70 2d 3e 77  ocked==0 || p->w
14e62 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 20  antToLock>0 );. 
14e63 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72   assert( p->shar
14e64 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c 6f  able==0 || p->lo
14e65 63 6b 65 64 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62  cked==0 || p->db
14e66 3d 3d 70 2d 3e 70 42 74 2d 3e 64 62 20 29 3b 0a  ==p->pBt->db );.
14e67 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68 61    assert( p->sha
14e68 72 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e 6c  rable==0 || p->l
14e69 6f 63 6b 65 64 3d 3d 30 20 7c 7c 20 73 71 6c 69  ocked==0 || sqli
14e6a 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
14e6b 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
14e6c 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68  .  assert( p->sh
14e6d 61 72 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d 3e  arable==0 || p->
14e6e 6c 6f 63 6b 65 64 3d 3d 30 20 7c 7c 20 73 71 6c  locked==0 || sql
14e6f 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
14e70 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
14e71 0a 0a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 73  ..  return (p->s
14e72 68 61 72 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 2d  harable==0 || p-
14e73 3e 6c 6f 63 6b 65 64 29 3b 0a 7d 0a 23 65 6e 64  >locked);.}.#end
14e74 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  if...#ifndef SQL
14e75 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
14e76 42 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 61 6e  B./*.** Enter an
14e77 64 20 6c 65 61 76 65 20 61 20 6d 75 74 65 78 20  d leave a mutex 
14e78 6f 6e 20 61 20 42 74 72 65 65 20 67 69 76 65 6e  on a Btree given
14e79 20 61 20 63 75 72 73 6f 72 20 6f 77 6e 65 64 20   a cursor owned 
14e7a 62 79 20 74 68 61 74 0a 2a 2a 20 42 74 72 65 65  by that.** Btree
14e7b 2e 20 20 54 68 65 73 65 20 65 6e 74 72 79 20 70  .  These entry p
14e7c 6f 69 6e 74 73 20 61 72 65 20 75 73 65 64 20 62  oints are used b
14e7d 79 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 49 2f  y incremental I/
14e7e 4f 20 61 6e 64 20 63 61 6e 20 62 65 0a 2a 2a 20  O and can be.** 
14e7f 6f 6d 69 74 74 65 64 20 69 66 20 74 68 61 74 20  omitted if that 
14e80 6d 6f 64 75 6c 65 20 69 73 20 6e 6f 74 20 75 73  module is not us
14e81 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
14e82 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
14e83 65 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73  e3BtreeEnterCurs
14e84 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  or(BtCursor *pCu
14e85 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  r){.  sqlite3Btr
14e86 65 65 45 6e 74 65 72 28 70 43 75 72 2d 3e 70 42  eeEnter(pCur->pB
14e87 74 72 65 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  tree);.}.SQLITE_
14e88 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
14e89 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75  ite3BtreeLeaveCu
14e8a 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70  rsor(BtCursor *p
14e8b 43 75 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 42  Cur){.  sqlite3B
14e8c 74 72 65 65 4c 65 61 76 65 28 70 43 75 72 2d 3e  treeLeave(pCur->
14e8d 70 42 74 72 65 65 29 3b 0a 7d 0a 23 65 6e 64 69  pBtree);.}.#endi
14e8e 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
14e8f 5f 49 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 0a 2f  _INCRBLOB */.../
14e90 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d  *.** Enter the m
14e91 75 74 65 78 20 6f 6e 20 65 76 65 72 79 20 42 74  utex on every Bt
14e92 72 65 65 20 61 73 73 6f 63 69 61 74 65 64 20 77  ree associated w
14e93 69 74 68 20 61 20 64 61 74 61 62 61 73 65 0a 2a  ith a database.*
14e94 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 54  * connection.  T
14e95 68 69 73 20 69 73 20 6e 65 65 64 65 64 20 28 66  his is needed (f
14e96 6f 72 20 65 78 61 6d 70 6c 65 29 20 70 72 69 6f  or example) prio
14e97 72 20 74 6f 20 70 61 72 73 69 6e 67 0a 2a 2a 20  r to parsing.** 
14e98 61 20 73 74 61 74 65 6d 65 6e 74 20 73 69 6e 63  a statement sinc
14e99 65 20 77 65 20 77 69 6c 6c 20 62 65 20 63 6f 6d  e we will be com
14e9a 70 61 72 69 6e 67 20 74 61 62 6c 65 20 61 6e 64  paring table and
14e9b 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 0a 2a 2a   column names.**
14e9c 20 61 67 61 69 6e 73 74 20 61 6c 6c 20 73 63 68   against all sch
14e9d 65 6d 61 73 20 61 6e 64 20 77 65 20 64 6f 20 6e  emas and we do n
14e9e 6f 74 20 77 61 6e 74 20 74 68 6f 73 65 20 73 63  ot want those sc
14e9f 68 65 6d 61 73 20 62 65 69 6e 67 0a 2a 2a 20 72  hemas being.** r
14ea0 65 73 65 74 20 6f 75 74 20 66 72 6f 6d 20 75 6e  eset out from un
14ea1 64 65 72 20 75 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  der us..**.** Th
14ea2 65 72 65 20 69 73 20 61 20 63 6f 72 72 65 73 70  ere is a corresp
14ea3 6f 6e 64 69 6e 67 20 6c 65 61 76 65 2d 61 6c 6c  onding leave-all
14ea4 20 70 72 6f 63 65 64 75 72 65 73 2e 0a 2a 2a 0a   procedures..**.
14ea5 2a 2a 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74  ** Enter the mut
14ea6 65 78 65 73 20 69 6e 20 61 63 63 65 6e 64 69 6e  exes in accendin
14ea7 67 20 6f 72 64 65 72 20 62 79 20 42 74 53 68 61  g order by BtSha
14ea8 72 65 64 20 70 6f 69 6e 74 65 72 20 61 64 64 72  red pointer addr
14ea9 65 73 73 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20  ess.** to avoid 
14eaa 74 68 65 20 70 6f 73 73 69 62 69 6c 69 74 79 20  the possibility 
14eab 6f 66 20 64 65 61 64 6c 6f 63 6b 20 77 68 65 6e  of deadlock when
14eac 20 74 77 6f 20 74 68 72 65 61 64 73 20 77 69 74   two threads wit
14ead 68 0a 2a 2a 20 74 77 6f 20 6f 72 20 6d 6f 72 65  h.** two or more
14eae 20 62 74 72 65 65 73 20 69 6e 20 63 6f 6d 6d 6f   btrees in commo
14eaf 6e 20 62 6f 74 68 20 74 72 79 20 74 6f 20 6c 6f  n both try to lo
14eb0 63 6b 20 61 6c 6c 20 74 68 65 69 72 20 62 74 72  ck all their btr
14eb1 65 65 73 0a 2a 2a 20 61 74 20 74 68 65 20 73 61  ees.** at the sa
14eb2 6d 65 20 69 6e 73 74 61 6e 74 2e 0a 2a 2f 0a 53  me instant..*/.S
14eb3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
14eb4 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  id sqlite3BtreeE
14eb5 6e 74 65 72 41 6c 6c 28 73 71 6c 69 74 65 33 20  nterAll(sqlite3 
14eb6 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  *db){.  int i;. 
14eb7 20 42 74 72 65 65 20 2a 70 2c 20 2a 70 4c 61 74   Btree *p, *pLat
14eb8 65 72 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  er;.  assert( sq
14eb9 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
14eba 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
14ebb 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
14ebc 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70  nDb; i++){.    p
14ebd 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
14ebe 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  t;.    assert( !
14ebf 70 20 7c 7c 20 28 70 2d 3e 6c 6f 63 6b 65 64 3d  p || (p->locked=
14ec0 3d 30 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c  =0 && p->sharabl
14ec1 65 29 20 7c 7c 20 70 2d 3e 70 42 74 2d 3e 64 62  e) || p->pBt->db
14ec2 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 20 20 69  ==p->db );.    i
14ec3 66 28 20 70 20 26 26 20 70 2d 3e 73 68 61 72 61  f( p && p->shara
14ec4 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  ble ){.      p->
14ec5 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20  wantToLock++;.  
14ec6 20 20 20 20 69 66 28 20 21 70 2d 3e 6c 6f 63 6b      if( !p->lock
14ec7 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  ed ){.        as
14ec8 73 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c  sert( p->wantToL
14ec9 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20  ock==1 );.      
14eca 20 20 77 68 69 6c 65 28 20 70 2d 3e 70 50 72 65    while( p->pPre
14ecb 76 20 29 20 70 20 3d 20 70 2d 3e 70 50 72 65 76  v ) p = p->pPrev
14ecc 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61  ;.        /* Rea
14ecd 73 6f 6e 20 66 6f 72 20 41 4c 57 41 59 53 3a 20  son for ALWAYS: 
14ece 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 61   There must be a
14ecf 74 20 6c 65 61 73 74 20 6f 6e 20 75 6e 6c 6f 63  t least on unloc
14ed0 6b 65 64 20 42 74 72 65 65 20 69 6e 0a 20 20 20  ked Btree in.   
14ed1 20 20 20 20 20 2a 2a 20 74 68 65 20 63 68 61 69       ** the chai
14ed2 6e 2e 20 20 4f 74 68 65 72 77 69 73 65 20 74 68  n.  Otherwise th
14ed3 65 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 74 65 73  e !p->locked tes
14ed4 74 20 61 62 6f 76 65 20 77 6f 75 6c 64 20 68 61  t above would ha
14ed5 76 65 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20 20  ve failed */.   
14ed6 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e 6c       while( p->l
14ed7 6f 63 6b 65 64 20 26 26 20 41 4c 57 41 59 53 28  ocked && ALWAYS(
14ed8 70 2d 3e 70 4e 65 78 74 29 20 29 20 70 20 3d 20  p->pNext) ) p = 
14ed9 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  p->pNext;.      
14eda 20 20 66 6f 72 28 70 4c 61 74 65 72 20 3d 20 70    for(pLater = p
14edb 2d 3e 70 4e 65 78 74 3b 20 70 4c 61 74 65 72 3b  ->pNext; pLater;
14edc 20 70 4c 61 74 65 72 3d 70 4c 61 74 65 72 2d 3e   pLater=pLater->
14edd 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20  pNext){.        
14ede 20 20 69 66 28 20 70 4c 61 74 65 72 2d 3e 6c 6f    if( pLater->lo
14edf 63 6b 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20  cked ){.        
14ee0 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 4d      unlockBtreeM
14ee1 75 74 65 78 28 70 4c 61 74 65 72 29 3b 0a 20 20  utex(pLater);.  
14ee2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
14ee3 20 20 7d 0a 20 20 20 20 20 20 20 20 77 68 69 6c    }.        whil
14ee4 65 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20  e( p ){.        
14ee5 20 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78    lockBtreeMutex
14ee6 28 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70  (p);.          p
14ee7 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20   = p->pNext;.   
14ee8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
14ee9 20 20 20 7d 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54     }.  }.}.SQLIT
14eea 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
14eeb 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
14eec 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 29  All(sqlite3 *db)
14eed 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42 74 72  {.  int i;.  Btr
14eee 65 65 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28  ee *p;.  assert(
14eef 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
14ef0 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
14ef1 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64  ;.  for(i=0; i<d
14ef2 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
14ef3 20 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d    p = db->aDb[i]
14ef4 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 20  .pBt;.    if( p 
14ef5 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29  && p->sharable )
14ef6 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
14ef7 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20  p->wantToLock>0 
14ef8 29 3b 0a 20 20 20 20 20 20 70 2d 3e 77 61 6e 74  );.      p->want
14ef9 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20  ToLock--;.      
14efa 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63  if( p->wantToLoc
14efb 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  k==0 ){.        
14efc 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78  unlockBtreeMutex
14efd 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  (p);.      }.   
14efe 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65   }.  }.}..#ifnde
14eff 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52  f NDEBUG./*.** R
14f00 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68  eturn true if th
14f01 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64  e current thread
14f02 20 68 6f 6c 64 73 20 74 68 65 20 64 61 74 61 62   holds the datab
14f03 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a  ase connection.*
14f04 2a 20 6d 75 74 65 78 20 61 6e 64 20 61 6c 6c 20  * mutex and all 
14f05 72 65 71 75 69 72 65 64 20 42 74 53 68 61 72 65  required BtShare
14f06 64 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a  d mutexes..**.**
14f07 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
14f08 20 75 73 65 64 20 69 6e 73 69 64 65 20 61 73 73   used inside ass
14f09 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73  ert() statements
14f0a 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45   only..*/.SQLITE
14f0b 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
14f0c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c  ite3BtreeHoldsAl
14f0d 6c 4d 75 74 65 78 65 73 28 73 71 6c 69 74 65 33  lMutexes(sqlite3
14f0e 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   *db){.  int i;.
14f0f 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f 6d    if( !sqlite3_m
14f10 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
14f11 74 65 78 29 20 29 7b 0a 20 20 20 20 72 65 74 75  tex) ){.    retu
14f12 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  rn 0;.  }.  for(
14f13 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  i=0; i<db->nDb; 
14f14 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20  i++){.    Btree 
14f15 2a 70 3b 0a 20 20 20 20 70 20 3d 20 64 62 2d 3e  *p;.    p = db->
14f16 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20  aDb[i].pBt;.    
14f17 69 66 28 20 70 20 26 26 20 70 2d 3e 73 68 61 72  if( p && p->shar
14f18 61 62 6c 65 20 26 26 0a 20 20 20 20 20 20 20 20  able &&.        
14f19 20 28 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d   (p->wantToLock=
14f1a 3d 30 20 7c 7c 20 21 73 71 6c 69 74 65 33 5f 6d  =0 || !sqlite3_m
14f1b 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42 74  utex_held(p->pBt
14f1c 2d 3e 6d 75 74 65 78 29 29 20 29 7b 0a 20 20 20  ->mutex)) ){.   
14f1d 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
14f1e 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
14f1f 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e  1;.}.#endif /* N
14f20 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  DEBUG */../*.** 
14f21 41 64 64 20 61 20 6e 65 77 20 42 74 72 65 65 20  Add a new Btree 
14f22 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 42 74 72  pointer to a Btr
14f23 65 65 4d 75 74 65 78 41 72 72 61 79 2e 20 0a 2a  eeMutexArray. .*
14f24 2a 20 69 66 20 74 68 65 20 70 6f 69 6e 74 65 72  * if the pointer
14f25 20 63 61 6e 20 70 6f 73 73 69 62 6c 79 20 62 65   can possibly be
14f26 20 73 68 61 72 65 64 20 77 69 74 68 0a 2a 2a 20   shared with.** 
14f27 61 6e 6f 74 68 65 72 20 64 61 74 61 62 61 73 65  another database
14f28 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a   connection..**.
14f29 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 73 20  ** The pointers 
14f2a 61 72 65 20 6b 65 70 74 20 69 6e 20 73 6f 72 74  are kept in sort
14f2b 65 64 20 6f 72 64 65 72 20 62 79 20 70 42 74 72  ed order by pBtr
14f2c 65 65 2d 3e 70 42 74 2e 20 20 54 68 61 74 0a 2a  ee->pBt.  That.*
14f2d 2a 20 77 61 79 20 77 68 65 6e 20 77 65 20 67 6f  * way when we go
14f2e 20 74 6f 20 65 6e 74 65 72 20 61 6c 6c 20 74 68   to enter all th
14f2f 65 20 6d 75 74 65 78 65 73 2c 20 77 65 20 63 61  e mutexes, we ca
14f30 6e 20 65 6e 74 65 72 20 74 68 65 6d 0a 2a 2a 20  n enter them.** 
14f31 69 6e 20 6f 72 64 65 72 20 77 69 74 68 6f 75 74  in order without
14f32 20 65 76 65 72 79 20 68 61 76 69 6e 67 20 74 6f   every having to
14f33 20 62 61 63 6b 75 70 20 61 6e 64 20 72 65 74 72   backup and retr
14f34 79 20 61 6e 64 20 77 69 74 68 6f 75 74 0a 2a 2a  y and without.**
14f35 20 77 6f 72 72 79 69 6e 67 20 61 62 6f 75 74 20   worrying about 
14f36 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20  deadlock..**.** 
14f37 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 68  The number of sh
14f38 61 72 65 64 20 62 74 72 65 65 73 20 77 69 6c 6c  ared btrees will
14f39 20 61 6c 77 61 79 73 20 62 65 20 73 6d 61 6c 6c   always be small
14f3a 20 28 75 73 75 61 6c 6c 79 20 30 20 6f 72 20 31   (usually 0 or 1
14f3b 29 0a 2a 2a 20 73 6f 20 61 6e 20 69 6e 73 65 72  ).** so an inser
14f3c 74 69 6f 6e 20 73 6f 72 74 20 69 73 20 61 6e 20  tion sort is an 
14f3d 61 64 65 71 75 61 74 65 20 61 6c 67 6f 72 69 74  adequate algorit
14f3e 68 6d 20 68 65 72 65 2e 0a 2a 2f 0a 53 51 4c 49  hm here..*/.SQLI
14f3f 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
14f40 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65  sqlite3BtreeMute
14f41 78 41 72 72 61 79 49 6e 73 65 72 74 28 42 74 72  xArrayInsert(Btr
14f42 65 65 4d 75 74 65 78 41 72 72 61 79 20 2a 70 41  eeMutexArray *pA
14f43 72 72 61 79 2c 20 42 74 72 65 65 20 2a 70 42 74  rray, Btree *pBt
14f44 72 65 65 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a  ree){.  int i, j
14f45 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
14f46 74 3b 0a 20 20 69 66 28 20 70 42 74 72 65 65 3d  t;.  if( pBtree=
14f47 3d 30 20 7c 7c 20 70 42 74 72 65 65 2d 3e 73 68  =0 || pBtree->sh
14f48 61 72 61 62 6c 65 3d 3d 30 20 29 20 72 65 74 75  arable==0 ) retu
14f49 72 6e 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  rn;.#ifndef NDEB
14f4a 55 47 0a 20 20 7b 0a 20 20 20 20 66 6f 72 28 69  UG.  {.    for(i
14f4b 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d  =0; i<pArray->nM
14f4c 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  utex; i++){.    
14f4d 20 20 61 73 73 65 72 74 28 20 70 41 72 72 61 79    assert( pArray
14f4e 2d 3e 61 42 74 72 65 65 5b 69 5d 21 3d 70 42 74  ->aBtree[i]!=pBt
14f4f 72 65 65 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ree );.    }.  }
14f50 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 65 72 74  .#endif.  assert
14f51 28 20 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78  ( pArray->nMutex
14f52 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  >=0 );.  assert(
14f53 20 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3c   pArray->nMutex<
14f54 41 72 72 61 79 53 69 7a 65 28 70 41 72 72 61 79  ArraySize(pArray
14f55 2d 3e 61 42 74 72 65 65 29 2d 31 20 29 3b 0a 20  ->aBtree)-1 );. 
14f56 20 70 42 74 20 3d 20 70 42 74 72 65 65 2d 3e 70   pBt = pBtree->p
14f57 42 74 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  Bt;.  for(i=0; i
14f58 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b  <pArray->nMutex;
14f59 20 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72   i++){.    asser
14f5a 74 28 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65  t( pArray->aBtre
14f5b 65 5b 69 5d 21 3d 70 42 74 72 65 65 20 29 3b 0a  e[i]!=pBtree );.
14f5c 20 20 20 20 69 66 28 20 70 41 72 72 61 79 2d 3e      if( pArray->
14f5d 61 42 74 72 65 65 5b 69 5d 2d 3e 70 42 74 3e 70  aBtree[i]->pBt>p
14f5e 42 74 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28  Bt ){.      for(
14f5f 6a 3d 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78  j=pArray->nMutex
14f60 3b 20 6a 3e 69 3b 20 6a 2d 2d 29 7b 0a 20 20 20  ; j>i; j--){.   
14f61 20 20 20 20 20 70 41 72 72 61 79 2d 3e 61 42 74       pArray->aBt
14f62 72 65 65 5b 6a 5d 20 3d 20 70 41 72 72 61 79 2d  ree[j] = pArray-
14f63 3e 61 42 74 72 65 65 5b 6a 2d 31 5d 3b 0a 20 20  >aBtree[j-1];.  
14f64 20 20 20 20 7d 0a 20 20 20 20 20 20 70 41 72 72      }.      pArr
14f65 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 20 3d 20  ay->aBtree[i] = 
14f66 70 42 74 72 65 65 3b 0a 20 20 20 20 20 20 70 41  pBtree;.      pA
14f67 72 72 61 79 2d 3e 6e 4d 75 74 65 78 2b 2b 3b 0a  rray->nMutex++;.
14f68 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
14f69 20 20 7d 0a 20 20 7d 0a 20 20 70 41 72 72 61 79    }.  }.  pArray
14f6a 2d 3e 61 42 74 72 65 65 5b 70 41 72 72 61 79 2d  ->aBtree[pArray-
14f6b 3e 6e 4d 75 74 65 78 2b 2b 5d 20 3d 20 70 42 74  >nMutex++] = pBt
14f6c 72 65 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e  ree;.}../*.** En
14f6d 74 65 72 20 74 68 65 20 6d 75 74 65 78 20 6f 66  ter the mutex of
14f6e 20 65 76 65 72 79 20 62 74 72 65 65 20 69 6e 20   every btree in 
14f6f 74 68 65 20 61 72 72 61 79 2e 20 20 54 68 69 73  the array.  This
14f70 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a 2a 20 63   routine is.** c
14f71 61 6c 6c 65 64 20 61 74 20 74 68 65 20 62 65 67  alled at the beg
14f72 69 6e 6e 69 6e 67 20 6f 66 20 73 71 6c 69 74 65  inning of sqlite
14f73 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 54 68  3VdbeExec().  Th
14f74 65 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a 2a  e mutexes are.**
14f75 20 65 78 69 74 65 64 20 61 74 20 74 68 65 20 65   exited at the e
14f76 6e 64 20 6f 66 20 74 68 65 20 73 61 6d 65 20 66  nd of the same f
14f77 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  unction..*/.SQLI
14f78 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
14f79 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65  sqlite3BtreeMute
14f7a 78 41 72 72 61 79 45 6e 74 65 72 28 42 74 72 65  xArrayEnter(Btre
14f7b 65 4d 75 74 65 78 41 72 72 61 79 20 2a 70 41 72  eMutexArray *pAr
14f7c 72 61 79 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ray){.  int i;. 
14f7d 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 72 72   for(i=0; i<pArr
14f7e 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29  ay->nMutex; i++)
14f7f 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 20 3d  {.    Btree *p =
14f80 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b   pArray->aBtree[
14f81 69 5d 3b 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20  i];.    /* Some 
14f82 62 61 73 69 63 20 73 61 6e 69 74 79 20 63 68 65  basic sanity che
14f83 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20 61 73 73  cking */.    ass
14f84 65 72 74 28 20 69 3d 3d 30 20 7c 7c 20 70 41 72  ert( i==0 || pAr
14f85 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 2d 31 5d  ray->aBtree[i-1]
14f86 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29 3b 0a  ->pBt<p->pBt );.
14f87 20 20 20 20 61 73 73 65 72 74 28 20 21 70 2d 3e      assert( !p->
14f88 6c 6f 63 6b 65 64 20 7c 7c 20 70 2d 3e 77 61 6e  locked || p->wan
14f89 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20  tToLock>0 );..  
14f8a 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 61    /* We should a
14f8b 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f  lready hold a lo
14f8c 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ck on the databa
14f8d 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
14f8e 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
14f8f 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
14f90 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
14f91 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 42 74 72  ..    /* The Btr
14f92 65 65 20 69 73 20 73 68 61 72 61 62 6c 65 20 62  ee is sharable b
14f93 65 63 61 75 73 65 20 6f 6e 6c 79 20 73 68 61 72  ecause only shar
14f94 61 62 6c 65 20 42 74 72 65 65 73 20 61 72 65 20  able Btrees are 
14f95 65 6e 74 65 72 65 64 0a 20 20 20 20 2a 2a 20 69  entered.    ** i
14f96 6e 74 6f 20 74 68 65 20 61 72 72 61 79 20 69 6e  nto the array in
14f97 20 74 68 65 20 66 69 72 73 74 20 70 6c 61 63 65   the first place
14f98 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  . */.    assert(
14f99 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a   p->sharable );.
14f9a 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f  .    p->wantToLo
14f9b 63 6b 2b 2b 3b 0a 20 20 20 20 69 66 28 20 21 70  ck++;.    if( !p
14f9c 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20  ->locked ){.    
14f9d 20 20 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78    lockBtreeMutex
14f9e 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  (p);.    }.  }.}
14f9f 0a 0a 2f 2a 0a 2a 2a 20 4c 65 61 76 65 20 74 68  ../*.** Leave th
14fa0 65 20 6d 75 74 65 78 20 6f 66 20 65 76 65 72 79  e mutex of every
14fa1 20 62 74 72 65 65 20 69 6e 20 74 68 65 20 67 72   btree in the gr
14fa2 6f 75 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  oup..*/.SQLITE_P
14fa3 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
14fa4 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72  te3BtreeMutexArr
14fa5 61 79 4c 65 61 76 65 28 42 74 72 65 65 4d 75 74  ayLeave(BtreeMut
14fa6 65 78 41 72 72 61 79 20 2a 70 41 72 72 61 79 29  exArray *pArray)
14fa7 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72  {.  int i;.  for
14fa8 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d 3e  (i=0; i<pArray->
14fa9 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20 20  nMutex; i++){.  
14faa 20 20 42 74 72 65 65 20 2a 70 20 3d 20 70 41 72    Btree *p = pAr
14fab 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 3b 0a  ray->aBtree[i];.
14fac 20 20 20 20 2f 2a 20 53 6f 6d 65 20 62 61 73 69      /* Some basi
14fad 63 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e  c sanity checkin
14fae 67 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  g */.    assert(
14faf 20 69 3d 3d 30 20 7c 7c 20 70 41 72 72 61 79 2d   i==0 || pArray-
14fb0 3e 61 42 74 72 65 65 5b 69 2d 31 5d 2d 3e 70 42  >aBtree[i-1]->pB
14fb1 74 3c 70 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20  t<p->pBt );.    
14fb2 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65  assert( p->locke
14fb3 64 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  d );.    assert(
14fb4 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30   p->wantToLock>0
14fb5 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 57 65 20 73   );..    /* We s
14fb6 68 6f 75 6c 64 20 61 6c 72 65 61 64 79 20 68 6f  hould already ho
14fb7 6c 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  ld a lock on the
14fb8 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
14fb9 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65  tion */.    asse
14fba 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
14fbb 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75  x_held(p->db->mu
14fbc 74 65 78 29 20 29 3b 0a 0a 20 20 20 20 70 2d 3e  tex) );..    p->
14fbd 77 61 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20  wantToLock--;.  
14fbe 20 20 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c    if( p->wantToL
14fbf 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ock==0 ){.      
14fc0 75 6e 6c 6f 63 6b 42 74 72 65 65 4d 75 74 65 78  unlockBtreeMutex
14fc1 28 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  (p);.    }.  }.}
14fc2 0a 0a 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50  ..#else.SQLITE_P
14fc3 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
14fc4 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 42 74  te3BtreeEnter(Bt
14fc5 72 65 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 70 42  ree *p){.  p->pB
14fc6 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 7d  t->db = p->db;.}
14fc7 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14fc8 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
14fc9 65 45 6e 74 65 72 41 6c 6c 28 73 71 6c 69 74 65  eEnterAll(sqlite
14fca 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b  3 *db){.  int i;
14fcb 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62  .  for(i=0; i<db
14fcc 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
14fcd 20 42 74 72 65 65 20 2a 70 20 3d 20 64 62 2d 3e   Btree *p = db->
14fce 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20  aDb[i].pBt;.    
14fcf 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 70  if( p ){.      p
14fd0 2d 3e 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64  ->pBt->db = p->d
14fd1 62 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23  b;.    }.  }.}.#
14fd2 65 6e 64 69 66 20 2f 2a 20 69 66 20 53 51 4c 49  endif /* if SQLI
14fd3 54 45 5f 54 48 52 45 41 44 53 41 46 45 20 2a 2f  TE_THREADSAFE */
14fd4 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66 6e 64 65  .#endif /* ifnde
14fd5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
14fd6 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a 0a 2f  ARED_CACHE */../
14fd7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
14fd8 6e 64 20 6f 66 20 62 74 6d 75 74 65 78 2e 63 20  nd of btmutex.c 
14fd9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14fda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14fdb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
14fdc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
14fdd 65 67 69 6e 20 66 69 6c 65 20 62 74 72 65 65 2e  egin file btree.
14fde 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
14fdf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
14fe1 2a 0a 2a 2a 20 32 30 30 34 20 41 70 72 69 6c 20  *.** 2004 April 
14fe2 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  6.**.** The auth
14fe3 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
14fe4 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
14fe5 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
14fe6 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
14fe7 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
14fe8 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
14fe9 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
14fea 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
14feb 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
14fec 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
14fed 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
14fee 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
14fef 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
14ff0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
14ff1 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
14ff2 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
14ff3 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
14ff4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14ff5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14ff6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14ff7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
14ff8 0a 2a 2a 20 24 49 64 3a 20 62 74 72 65 65 2e 63  .** $Id: btree.c
14ff9 2c 76 20 31 2e 37 30 35 20 32 30 30 39 2f 30 38  ,v 1.705 2009/08
14ffa 2f 31 30 20 30 33 3a 35 37 3a 35 38 20 73 68 61  /10 03:57:58 sha
14ffb 6e 65 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54  ne Exp $.**.** T
14ffc 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65  his file impleme
14ffd 6e 74 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28  nts a external (
14ffe 64 69 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61  disk-based) data
14fff 62 61 73 65 20 75 73 69 6e 67 20 42 54 72 65 65  base using BTree
15000 73 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 68 65  s..** See the he
15001 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20  ader comment on 
15002 22 62 74 72 65 65 49 6e 74 2e 68 22 20 66 6f 72  "btreeInt.h" for
15003 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
15004 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 49 6e 63 6c  rmation..** Incl
15005 75 64 69 6e 67 20 61 20 64 65 73 63 72 69 70 74  uding a descript
15006 69 6f 6e 20 6f 66 20 66 69 6c 65 20 66 6f 72 6d  ion of file form
15007 61 74 20 61 6e 64 20 61 6e 20 6f 76 65 72 76 69  at and an overvi
15008 65 77 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 2e  ew of operation.
15009 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68  .*/../*.** The h
1500a 65 61 64 65 72 20 73 74 72 69 6e 67 20 74 68 61  eader string tha
1500b 74 20 61 70 70 65 61 72 73 20 61 74 20 74 68 65  t appears at the
1500c 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76   beginning of ev
1500d 65 72 79 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61  ery.** SQLite da
1500e 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69  tabase..*/.stati
1500f 63 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 4d 61  c const char zMa
15010 67 69 63 48 65 61 64 65 72 5b 5d 20 3d 20 53 51  gicHeader[] = SQ
15011 4c 49 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52  LITE_FILE_HEADER
15012 3b 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 69  ;../*.** Set thi
15013 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  s global variabl
15014 65 20 74 6f 20 31 20 74 6f 20 65 6e 61 62 6c 65  e to 1 to enable
15015 20 74 72 61 63 69 6e 67 20 75 73 69 6e 67 20 74   tracing using t
15016 68 65 20 54 52 41 43 45 0a 2a 2a 20 6d 61 63 72  he TRACE.** macr
15017 6f 2e 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 20  o..*/.#if 0.int 
15018 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 61 63  sqlite3BtreeTrac
15019 65 3d 31 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f  e=1;  /* True to
1501a 20 65 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20   enable tracing 
1501b 2a 2f 0a 23 20 64 65 66 69 6e 65 20 54 52 41 43  */.# define TRAC
1501c 45 28 58 29 20 20 69 66 28 73 71 6c 69 74 65 33  E(X)  if(sqlite3
1501d 42 74 72 65 65 54 72 61 63 65 29 7b 70 72 69 6e  BtreeTrace){prin
1501e 74 66 20 58 3b 66 66 6c 75 73 68 28 73 74 64 6f  tf X;fflush(stdo
1501f 75 74 29 3b 7d 0a 23 65 6c 73 65 0a 23 20 64 65  ut);}.#else.# de
15020 66 69 6e 65 20 54 52 41 43 45 28 58 29 0a 23 65  fine TRACE(X).#e
15021 6e 64 69 66 0a 0a 0a 0a 23 69 66 6e 64 65 66 20  ndif....#ifndef 
15022 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
15023 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41  ED_CACHE./*.** A
15024 20 6c 69 73 74 20 6f 66 20 42 74 53 68 61 72 65   list of BtShare
15025 64 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20 61  d objects that a
15026 72 65 20 65 6c 69 67 69 62 6c 65 20 66 6f 72 20  re eligible for 
15027 70 61 72 74 69 63 69 70 61 74 69 6f 6e 0a 2a 2a  participation.**
15028 20 69 6e 20 73 68 61 72 65 64 20 63 61 63 68 65   in shared cache
15029 2e 20 20 54 68 69 73 20 76 61 72 69 61 62 6c 65  .  This variable
1502a 20 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65 20   has file scope 
1502b 64 75 72 69 6e 67 20 6e 6f 72 6d 61 6c 20 62 75  during normal bu
1502c 69 6c 64 73 2c 0a 2a 2a 20 62 75 74 20 74 68 65  ilds,.** but the
1502d 20 74 65 73 74 20 68 61 72 6e 65 73 73 20 6e 65   test harness ne
1502e 65 64 73 20 74 6f 20 61 63 63 65 73 73 20 69 74  eds to access it
1502f 20 73 6f 20 77 65 20 6d 61 6b 65 20 69 74 20 67   so we make it g
15030 6c 6f 62 61 6c 20 66 6f 72 20 0a 2a 2a 20 74 65  lobal for .** te
15031 73 74 20 62 75 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a  st builds..**.**
15032 20 41 63 63 65 73 73 20 74 6f 20 74 68 69 73 20   Access to this 
15033 76 61 72 69 61 62 6c 65 20 69 73 20 70 72 6f 74  variable is prot
15034 65 63 74 65 64 20 62 79 20 53 51 4c 49 54 45 5f  ected by SQLITE_
15035 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
15036 54 45 52 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  TER..*/.#ifdef S
15037 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
15038 45 5f 50 52 49 56 41 54 45 20 42 74 53 68 61 72  E_PRIVATE BtShar
15039 65 64 20 2a 53 51 4c 49 54 45 5f 57 53 44 20 73  ed *SQLITE_WSD s
1503a 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68  qlite3SharedCach
1503b 65 4c 69 73 74 20 3d 20 30 3b 0a 23 65 6c 73 65  eList = 0;.#else
1503c 0a 73 74 61 74 69 63 20 42 74 53 68 61 72 65 64  .static BtShared
1503d 20 2a 53 51 4c 49 54 45 5f 57 53 44 20 73 71 6c   *SQLITE_WSD sql
1503e 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c  ite3SharedCacheL
1503f 69 73 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  ist = 0;.#endif.
15040 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
15041 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
15042 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  HE */..#ifndef S
15043 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
15044 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 45 6e  D_CACHE./*.** En
15045 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c 65 20  able or disable 
15046 74 68 65 20 73 68 61 72 65 64 20 70 61 67 65 72  the shared pager
15047 20 61 6e 64 20 73 63 68 65 6d 61 20 66 65 61 74   and schema feat
15048 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ures..**.** This
15049 20 72 6f 75 74 69 6e 65 20 68 61 73 20 6e 6f 20   routine has no 
1504a 65 66 66 65 63 74 20 6f 6e 20 65 78 69 73 74 69  effect on existi
1504b 6e 67 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ng database conn
1504c 65 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20  ections..** The 
1504d 73 68 61 72 65 64 20 63 61 63 68 65 20 73 65 74  shared cache set
1504e 74 69 6e 67 20 65 66 66 65 63 74 73 20 6f 6e 6c  ting effects onl
1504f 79 20 66 75 74 75 72 65 20 63 61 6c 6c 73 20 74  y future calls t
15050 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65  o.** sqlite3_ope
15051 6e 28 29 2c 20 73 71 6c 69 74 65 33 5f 6f 70 65  n(), sqlite3_ope
15052 6e 31 36 28 29 2c 20 6f 72 20 73 71 6c 69 74 65  n16(), or sqlite
15053 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a 2f 0a  3_open_v2()..*/.
15054 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
15055 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68  qlite3_enable_sh
15056 61 72 65 64 5f 63 61 63 68 65 28 69 6e 74 20 65  ared_cache(int e
15057 6e 61 62 6c 65 29 7b 0a 20 20 73 71 6c 69 74 65  nable){.  sqlite
15058 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 68  3GlobalConfig.sh
15059 61 72 65 64 43 61 63 68 65 45 6e 61 62 6c 65 64  aredCacheEnabled
1505a 20 3d 20 65 6e 61 62 6c 65 3b 0a 20 20 72 65 74   = enable;.  ret
1505b 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1505c 0a 23 65 6e 64 69 66 0a 0a 0a 0a 23 69 66 64 65  .#endif....#ifde
1505d 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
1505e 41 52 45 44 5f 43 41 43 48 45 0a 20 20 2f 2a 0a  ARED_CACHE.  /*.
1505f 20 20 2a 2a 20 54 68 65 20 66 75 6e 63 74 69 6f    ** The functio
15060 6e 73 20 71 75 65 72 79 53 68 61 72 65 64 43 61  ns querySharedCa
15061 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 20  cheTableLock(), 
15062 73 65 74 53 68 61 72 65 64 43 61 63 68 65 54 61  setSharedCacheTa
15063 62 6c 65 4c 6f 63 6b 28 29 2c 0a 20 20 2a 2a 20  bleLock(),.  ** 
15064 61 6e 64 20 63 6c 65 61 72 41 6c 6c 53 68 61 72  and clearAllShar
15065 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b  edCacheTableLock
15066 73 28 29 0a 20 20 2a 2a 20 6d 61 6e 69 70 75 6c  s().  ** manipul
15067 61 74 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74  ate entries in t
15068 68 65 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63  he BtShared.pLoc
15069 6b 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 75 73  k linked list us
1506a 65 64 20 74 6f 20 73 74 6f 72 65 0a 20 20 2a 2a  ed to store.  **
1506b 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 74 61   shared-cache ta
1506c 62 6c 65 20 6c 65 76 65 6c 20 6c 6f 63 6b 73 2e  ble level locks.
1506d 20 49 66 20 74 68 65 20 6c 69 62 72 61 72 79 20   If the library 
1506e 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  is compiled with
1506f 20 74 68 65 0a 20 20 2a 2a 20 73 68 61 72 65 64   the.  ** shared
15070 2d 63 61 63 68 65 20 66 65 61 74 75 72 65 20 64  -cache feature d
15071 69 73 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68  isabled, then th
15072 65 72 65 20 69 73 20 6f 6e 6c 79 20 65 76 65 72  ere is only ever
15073 20 6f 6e 65 20 75 73 65 72 0a 20 20 2a 2a 20 6f   one user.  ** o
15074 66 20 65 61 63 68 20 42 74 53 68 61 72 65 64 20  f each BtShared 
15075 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 73 6f  structure and so
15076 20 74 68 69 73 20 6c 6f 63 6b 69 6e 67 20 69 73   this locking is
15077 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 2e 20   not necessary. 
15078 0a 20 20 2a 2a 20 53 6f 20 64 65 66 69 6e 65 20  .  ** So define 
15079 74 68 65 20 6c 6f 63 6b 20 72 65 6c 61 74 65 64  the lock related
1507a 20 66 75 6e 63 74 69 6f 6e 73 20 61 73 20 6e 6f   functions as no
1507b 2d 6f 70 73 2e 0a 20 20 2a 2f 0a 20 20 23 64 65  -ops..  */.  #de
1507c 66 69 6e 65 20 71 75 65 72 79 53 68 61 72 65 64  fine queryShared
1507d 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 61  CacheTableLock(a
1507e 2c 62 2c 63 29 20 53 51 4c 49 54 45 5f 4f 4b 0a  ,b,c) SQLITE_OK.
1507f 20 20 23 64 65 66 69 6e 65 20 73 65 74 53 68 61    #define setSha
15080 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
15081 6b 28 61 2c 62 2c 63 29 20 53 51 4c 49 54 45 5f  k(a,b,c) SQLITE_
15082 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20 63 6c 65  OK.  #define cle
15083 61 72 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65  arAllSharedCache
15084 54 61 62 6c 65 4c 6f 63 6b 73 28 61 29 0a 20 20  TableLocks(a).  
15085 23 64 65 66 69 6e 65 20 64 6f 77 6e 67 72 61 64  #define downgrad
15086 65 41 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54  eAllSharedCacheT
15087 61 62 6c 65 4c 6f 63 6b 73 28 61 29 0a 20 20 23  ableLocks(a).  #
15088 64 65 66 69 6e 65 20 68 61 73 53 68 61 72 65 64  define hasShared
15089 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 61  CacheTableLock(a
1508a 2c 62 2c 63 2c 64 29 20 31 0a 20 20 23 64 65 66  ,b,c,d) 1.  #def
1508b 69 6e 65 20 68 61 73 52 65 61 64 43 6f 6e 66 6c  ine hasReadConfl
1508c 69 63 74 73 28 61 2c 20 62 29 20 30 0a 23 65 6e  icts(a, b) 0.#en
1508d 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
1508e 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
1508f 43 41 43 48 45 0a 0a 23 69 66 64 65 66 20 53 51  CACHE..#ifdef SQ
15090 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a  LITE_DEBUG./*.**
15091 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
15092 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 61 73   is only used as
15093 20 70 61 72 74 20 6f 66 20 61 6e 20 61 73 73 65   part of an asse
15094 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 2e 20  rt() statement. 
15095 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20  ***.**.** Check 
15096 74 6f 20 73 65 65 20 69 66 20 70 42 74 72 65 65  to see if pBtree
15097 20 68 6f 6c 64 73 20 74 68 65 20 72 65 71 75 69   holds the requi
15098 72 65 64 20 6c 6f 63 6b 73 20 74 6f 20 72 65 61  red locks to rea
15099 64 20 6f 72 20 77 72 69 74 65 20 74 6f 20 74 68  d or write to th
1509a 65 20 0a 2a 2a 20 74 61 62 6c 65 20 77 69 74 68  e .** table with
1509b 20 72 6f 6f 74 20 70 61 67 65 20 69 52 6f 6f 74   root page iRoot
1509c 2e 20 20 20 52 65 74 75 72 6e 20 31 20 69 66 20  .   Return 1 if 
1509d 69 74 20 64 6f 65 73 20 61 6e 64 20 30 20 69 66  it does and 0 if
1509e 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20   not..**.** For 
1509f 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 20 77 72  example, when wr
150a0 69 74 69 6e 67 20 74 6f 20 61 20 74 61 62 6c 65  iting to a table
150a1 20 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20   with root-page 
150a2 69 52 6f 6f 74 20 76 69 61 20 0a 2a 2a 20 42 74  iRoot via .** Bt
150a3 72 65 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70  ree connection p
150a4 42 74 72 65 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  Btree:.**.**    
150a5 61 73 73 65 72 74 28 20 68 61 73 53 68 61 72 65  assert( hasShare
150a6 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28  dCacheTableLock(
150a7 70 42 74 72 65 65 2c 20 69 52 6f 6f 74 2c 20 30  pBtree, iRoot, 0
150a8 2c 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 29 3b  , WRITE_LOCK) );
150a9 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 72 69 74  .**.** When writ
150aa 69 6e 67 20 74 6f 20 61 6e 20 69 6e 64 65 78 20  ing to an index 
150ab 74 68 61 74 20 72 65 73 69 64 65 73 20 69 6e 20  that resides in 
150ac 61 20 73 68 61 72 61 62 6c 65 20 64 61 74 61 62  a sharable datab
150ad 61 73 65 2c 20 74 68 65 20 0a 2a 2a 20 63 61 6c  ase, the .** cal
150ae 6c 65 72 20 73 68 6f 75 6c 64 20 68 61 76 65 20  ler should have 
150af 66 69 72 73 74 20 6f 62 74 61 69 6e 65 64 20 61  first obtained a
150b0 20 6c 6f 63 6b 20 73 70 65 63 69 66 79 69 6e 67   lock specifying
150b1 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f   the root page o
150b2 66 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65 73 70  f.** the corresp
150b3 6f 6e 64 69 6e 67 20 74 61 62 6c 65 2e 20 54 68  onding table. Th
150b4 69 73 20 6d 61 6b 65 73 20 74 68 69 6e 67 73 20  is makes things 
150b5 61 20 62 69 74 20 6d 6f 72 65 20 63 6f 6d 70 6c  a bit more compl
150b6 69 63 61 74 65 64 2c 0a 2a 2a 20 61 73 20 74 68  icated,.** as th
150b7 69 73 20 6d 6f 64 75 6c 65 20 74 72 65 61 74 73  is module treats
150b8 20 65 61 63 68 20 74 61 62 6c 65 20 61 73 20 61   each table as a
150b9 20 73 65 70 61 72 61 74 65 20 73 74 72 75 63 74   separate struct
150ba 75 72 65 2e 20 54 6f 20 64 65 74 65 72 6d 69 6e  ure. To determin
150bb 65 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 63  e.** the table c
150bc 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
150bd 74 68 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20  the index being 
150be 77 72 69 74 74 65 6e 2c 20 74 68 69 73 0a 2a 2a  written, this.**
150bf 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 74 6f   function has to
150c0 20 73 65 61 72 63 68 20 74 68 72 6f 75 67 68 20   search through 
150c1 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
150c2 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74 65  ema..**.** Inste
150c3 61 64 20 6f 66 20 61 20 6c 6f 63 6b 20 6f 6e 20  ad of a lock on 
150c4 74 68 65 20 74 61 62 6c 65 2f 69 6e 64 65 78 20  the table/index 
150c5 72 6f 6f 74 65 64 20 61 74 20 70 61 67 65 20 69  rooted at page i
150c6 52 6f 6f 74 2c 20 74 68 65 20 63 61 6c 6c 65 72  Root, the caller
150c7 20 6d 61 79 0a 2a 2a 20 68 6f 6c 64 20 61 20 77   may.** hold a w
150c8 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65  rite-lock on the
150c9 20 73 63 68 65 6d 61 20 74 61 62 6c 65 20 28 72   schema table (r
150ca 6f 6f 74 20 70 61 67 65 20 31 29 2e 20 54 68 69  oot page 1). Thi
150cb 73 20 69 73 20 61 6c 73 6f 0a 2a 2a 20 61 63 63  s is also.** acc
150cc 65 70 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74  eptable..*/.stat
150cd 69 63 20 69 6e 74 20 68 61 73 53 68 61 72 65 64  ic int hasShared
150ce 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 0a  CacheTableLock(.
150cf 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c    Btree *pBtree,
150d0 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64           /* Hand
150d1 6c 65 20 74 68 61 74 20 6d 75 73 74 20 68 6f 6c  le that must hol
150d2 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 50 67 6e 6f  d lock */.  Pgno
150d3 20 69 52 6f 6f 74 2c 20 20 20 20 20 20 20 20 20   iRoot,         
150d4 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20     /* Root page 
150d5 6f 66 20 62 2d 74 72 65 65 20 2a 2f 0a 20 20 69  of b-tree */.  i
150d6 6e 74 20 69 73 49 6e 64 65 78 2c 20 20 20 20 20  nt isIndex,     
150d7 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
150d8 20 69 52 6f 6f 74 20 69 73 20 74 68 65 20 72 6f   iRoot is the ro
150d9 6f 74 20 6f 66 20 61 6e 20 69 6e 64 65 78 20 62  ot of an index b
150da 2d 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 65  -tree */.  int e
150db 4c 6f 63 6b 54 79 70 65 20 20 20 20 20 20 20 20  LockType        
150dc 20 20 2f 2a 20 52 65 71 75 69 72 65 64 20 6c 6f    /* Required lo
150dd 63 6b 20 74 79 70 65 20 28 52 45 41 44 5f 4c 4f  ck type (READ_LO
150de 43 4b 20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b  CK or WRITE_LOCK
150df 29 20 2a 2f 0a 29 7b 0a 20 20 53 63 68 65 6d 61  ) */.){.  Schema
150e0 20 2a 70 53 63 68 65 6d 61 20 3d 20 28 53 63 68   *pSchema = (Sch
150e1 65 6d 61 20 2a 29 70 42 74 72 65 65 2d 3e 70 42  ema *)pBtree->pB
150e2 74 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 50 67  t->pSchema;.  Pg
150e3 6e 6f 20 69 54 61 62 20 3d 20 30 3b 0a 20 20 42  no iTab = 0;.  B
150e4 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 3b 0a 0a 20  tLock *pLock;.. 
150e5 20 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 61   /* If this data
150e6 62 61 73 65 20 69 73 20 6e 6f 74 20 73 68 61 72  base is not shar
150e7 65 61 62 6c 65 2c 20 6f 72 20 69 66 20 74 68 65  eable, or if the
150e8 20 63 6c 69 65 6e 74 20 69 73 20 72 65 61 64 69   client is readi
150e9 6e 67 0a 20 20 2a 2a 20 61 6e 64 20 68 61 73 20  ng.  ** and has 
150ea 74 68 65 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69  the read-uncommi
150eb 74 74 65 64 20 66 6c 61 67 20 73 65 74 2c 20 74  tted flag set, t
150ec 68 65 6e 20 6e 6f 20 6c 6f 63 6b 20 69 73 20 72  hen no lock is r
150ed 65 71 75 69 72 65 64 2e 20 0a 20 20 2a 2a 20 52  equired. .  ** R
150ee 65 74 75 72 6e 20 74 72 75 65 20 69 6d 6d 65 64  eturn true immed
150ef 69 61 74 65 6c 79 2e 0a 20 20 2a 2f 0a 20 20 69  iately..  */.  i
150f0 66 28 20 28 70 42 74 72 65 65 2d 3e 73 68 61 72  f( (pBtree->shar
150f1 61 62 6c 65 3d 3d 30 29 0a 20 20 20 7c 7c 20 28  able==0).   || (
150f2 65 4c 6f 63 6b 54 79 70 65 3d 3d 52 45 41 44 5f  eLockType==READ_
150f3 4c 4f 43 4b 20 26 26 20 28 70 42 74 72 65 65 2d  LOCK && (pBtree-
150f4 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  >db->flags & SQL
150f5 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74  ITE_ReadUncommit
150f6 74 65 64 29 29 0a 20 20 29 7b 0a 20 20 20 20 72  ted)).  ){.    r
150f7 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20  eturn 1;.  }..  
150f8 2f 2a 20 49 66 20 74 68 65 20 63 6c 69 65 6e 74  /* If the client
150f9 20 69 73 20 72 65 61 64 69 6e 67 20 20 6f 72 20   is reading  or 
150fa 77 72 69 74 69 6e 67 20 61 6e 20 69 6e 64 65 78  writing an index
150fb 20 61 6e 64 20 74 68 65 20 73 63 68 65 6d 61 20   and the schema 
150fc 69 73 0a 20 20 2a 2a 20 6e 6f 74 20 6c 6f 61 64  is.  ** not load
150fd 65 64 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74  ed, then it is t
150fe 6f 6f 20 64 69 66 66 69 63 75 6c 74 20 74 6f 20  oo difficult to 
150ff 61 63 74 75 61 6c 6c 79 20 63 68 65 63 6b 20 74  actually check t
15100 6f 20 73 65 65 20 69 66 0a 20 20 2a 2a 20 74 68  o see if.  ** th
15101 65 20 63 6f 72 72 65 63 74 20 6c 6f 63 6b 73 20  e correct locks 
15102 61 72 65 20 68 65 6c 64 2e 20 20 53 6f 20 64 6f  are held.  So do
15103 20 6e 6f 74 20 62 6f 74 68 65 72 20 2d 20 6a 75   not bother - ju
15104 73 74 20 72 65 74 75 72 6e 20 74 72 75 65 2e 0a  st return true..
15105 20 20 2a 2a 20 54 68 69 73 20 63 61 73 65 20 64    ** This case d
15106 6f 65 73 20 6e 6f 74 20 63 6f 6d 65 20 75 70 20  oes not come up 
15107 76 65 72 79 20 6f 66 74 65 6e 20 61 6e 79 68 6f  very often anyho
15108 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73  w..  */.  if( is
15109 49 6e 64 65 78 20 26 26 20 28 21 70 53 63 68 65  Index && (!pSche
1510a 6d 61 20 7c 7c 20 28 70 53 63 68 65 6d 61 2d 3e  ma || (pSchema->
1510b 66 6c 61 67 73 26 44 42 5f 53 63 68 65 6d 61 4c  flags&DB_SchemaL
1510c 6f 61 64 65 64 29 3d 3d 30 29 20 29 7b 0a 20 20  oaded)==0) ){.  
1510d 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a    return 1;.  }.
1510e 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74  .  /* Figure out
1510f 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 74   the root-page t
15110 68 61 74 20 74 68 65 20 6c 6f 63 6b 20 73 68 6f  hat the lock sho
15111 75 6c 64 20 62 65 20 68 65 6c 64 20 6f 6e 2e 20  uld be held on. 
15112 46 6f 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20 62  For table.  ** b
15113 2d 74 72 65 65 73 2c 20 74 68 69 73 20 69 73 20  -trees, this is 
15114 6a 75 73 74 20 74 68 65 20 72 6f 6f 74 20 70 61  just the root pa
15115 67 65 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65  ge of the b-tree
15116 20 62 65 69 6e 67 20 72 65 61 64 20 6f 72 0a 20   being read or. 
15117 20 2a 2a 20 77 72 69 74 74 65 6e 2e 20 46 6f 72   ** written. For
15118 20 69 6e 64 65 78 20 62 2d 74 72 65 65 73 2c 20   index b-trees, 
15119 69 74 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70  it is the root p
1511a 61 67 65 20 6f 66 20 74 68 65 20 61 73 73 6f 63  age of the assoc
1511b 69 61 74 65 64 0a 20 20 2a 2a 20 74 61 62 6c 65  iated.  ** table
1511c 2e 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 49 6e  .  */.  if( isIn
1511d 64 65 78 20 29 7b 0a 20 20 20 20 48 61 73 68 45  dex ){.    HashE
1511e 6c 65 6d 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28  lem *p;.    for(
1511f 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  p=sqliteHashFirs
15120 74 28 26 70 53 63 68 65 6d 61 2d 3e 69 64 78 48  t(&pSchema->idxH
15121 61 73 68 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74  ash); p; p=sqlit
15122 65 48 61 73 68 4e 65 78 74 28 70 29 29 7b 0a 20  eHashNext(p)){. 
15123 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78       Index *pIdx
15124 20 3d 20 28 49 6e 64 65 78 20 2a 29 73 71 6c 69   = (Index *)sqli
15125 74 65 48 61 73 68 44 61 74 61 28 70 29 3b 0a 20  teHashData(p);. 
15126 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 74       if( pIdx->t
15127 6e 75 6d 3d 3d 28 69 6e 74 29 69 52 6f 6f 74 20  num==(int)iRoot 
15128 29 7b 0a 20 20 20 20 20 20 20 20 69 54 61 62 20  ){.        iTab 
15129 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e  = pIdx->pTable->
1512a 74 6e 75 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  tnum;.      }.  
1512b 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
1512c 20 69 54 61 62 20 3d 20 69 52 6f 6f 74 3b 0a 20   iTab = iRoot;. 
1512d 20 7d 0a 0a 20 20 2f 2a 20 53 65 61 72 63 68 20   }..  /* Search 
1512e 66 6f 72 20 74 68 65 20 72 65 71 75 69 72 65 64  for the required
1512f 20 6c 6f 63 6b 2e 20 45 69 74 68 65 72 20 61 20   lock. Either a 
15130 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 72 6f  write-lock on ro
15131 6f 74 2d 70 61 67 65 20 69 54 61 62 2c 20 61 20  ot-page iTab, a 
15132 0a 20 20 2a 2a 20 77 72 69 74 65 2d 6c 6f 63 6b  .  ** write-lock
15133 20 6f 6e 20 74 68 65 20 73 63 68 65 6d 61 20 74   on the schema t
15134 61 62 6c 65 2c 20 6f 72 20 28 69 66 20 74 68 65  able, or (if the
15135 20 63 6c 69 65 6e 74 20 69 73 20 72 65 61 64 69   client is readi
15136 6e 67 29 20 61 0a 20 20 2a 2a 20 72 65 61 64 2d  ng) a.  ** read-
15137 6c 6f 63 6b 20 6f 6e 20 69 54 61 62 20 77 69 6c  lock on iTab wil
15138 6c 20 73 75 66 66 69 63 65 2e 20 52 65 74 75 72  l suffice. Retur
15139 6e 20 31 20 69 66 20 61 6e 79 20 6f 66 20 74 68  n 1 if any of th
1513a 65 73 65 20 61 72 65 20 66 6f 75 6e 64 2e 20 20  ese are found.  
1513b 2a 2f 0a 20 20 66 6f 72 28 70 4c 6f 63 6b 3d 70  */.  for(pLock=p
1513c 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 4c 6f 63  Btree->pBt->pLoc
1513d 6b 3b 20 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b 3d  k; pLock; pLock=
1513e 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 29 7b 0a 20  pLock->pNext){. 
1513f 20 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 70 42     if( pLock->pB
15140 74 72 65 65 3d 3d 70 42 74 72 65 65 20 0a 20 20  tree==pBtree .  
15141 20 20 20 26 26 20 28 70 4c 6f 63 6b 2d 3e 69 54     && (pLock->iT
15142 61 62 6c 65 3d 3d 69 54 61 62 20 7c 7c 20 28 70  able==iTab || (p
15143 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 3d 3d 57 52 49  Lock->eLock==WRI
15144 54 45 5f 4c 4f 43 4b 20 26 26 20 70 4c 6f 63 6b  TE_LOCK && pLock
15145 2d 3e 69 54 61 62 6c 65 3d 3d 31 29 29 0a 20 20  ->iTable==1)).  
15146 20 20 20 26 26 20 70 4c 6f 63 6b 2d 3e 65 4c 6f     && pLock->eLo
15147 63 6b 3e 3d 65 4c 6f 63 6b 54 79 70 65 20 0a 20  ck>=eLockType . 
15148 20 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75     ){.      retu
15149 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn 1;.    }.  }.
1514a 0a 20 20 2f 2a 20 46 61 69 6c 65 64 20 74 6f 20  .  /* Failed to 
1514b 66 69 6e 64 20 74 68 65 20 72 65 71 75 69 72 65  find the require
1514c 64 20 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 72 65 74  d lock. */.  ret
1514d 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20  urn 0;.}.#endif 
1514e 2f 2a 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  /* SQLITE_DEBUG 
1514f 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
15150 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 2a 2a 20  E_DEBUG./*.**** 
15151 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61  This function ma
15152 79 20 62 65 20 75 73 65 64 20 61 73 20 70 61 72  y be used as par
15153 74 20 6f 66 20 61 73 73 65 72 74 28 29 20 73 74  t of assert() st
15154 61 74 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a  atements only. *
15155 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  ***.**.** Return
15156 20 74 72 75 65 20 69 66 20 69 74 20 77 6f 75 6c   true if it woul
15157 64 20 62 65 20 69 6c 6c 65 67 61 6c 20 66 6f 72  d be illegal for
15158 20 70 42 74 72 65 65 20 74 6f 20 77 72 69 74 65   pBtree to write
15159 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 74 61 62   into the.** tab
1515a 6c 65 20 6f 72 20 69 6e 64 65 78 20 72 6f 6f 74  le or index root
1515b 65 64 20 61 74 20 69 52 6f 6f 74 20 62 65 63 61  ed at iRoot beca
1515c 75 73 65 20 6f 74 68 65 72 20 73 68 61 72 65 64  use other shared
1515d 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 61 72 65   connections are
1515e 0a 2a 2a 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73  .** simultaneous
1515f 6c 79 20 72 65 61 64 69 6e 67 20 74 68 61 74 20  ly reading that 
15160 73 61 6d 65 20 74 61 62 6c 65 20 6f 72 20 69 6e  same table or in
15161 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73  dex..**.** It is
15162 20 69 6c 6c 65 67 61 6c 20 66 6f 72 20 70 42 74   illegal for pBt
15163 72 65 65 20 74 6f 20 77 72 69 74 65 20 69 66 20  ree to write if 
15164 73 6f 6d 65 20 6f 74 68 65 72 20 42 74 72 65 65  some other Btree
15165 20 6f 62 6a 65 63 74 20 74 68 61 74 0a 2a 2a 20   object that.** 
15166 73 68 61 72 65 73 20 74 68 65 20 73 61 6d 65 20  shares the same 
15167 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 20  BtShared object 
15168 69 73 20 63 75 72 72 65 6e 74 6c 79 20 72 65 61  is currently rea
15169 64 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 0a  ding or writing.
1516a 2a 2a 20 74 68 65 20 69 52 6f 6f 74 20 74 61 62  ** the iRoot tab
1516b 6c 65 2e 20 20 45 78 63 65 70 74 2c 20 69 66 20  le.  Except, if 
1516c 74 68 65 20 6f 74 68 65 72 20 42 74 72 65 65 20  the other Btree 
1516d 6f 62 6a 65 63 74 20 68 61 73 20 74 68 65 0a 2a  object has the.*
1516e 2a 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74  * read-uncommitt
1516f 65 64 20 66 6c 61 67 20 73 65 74 2c 20 74 68 65  ed flag set, the
15170 6e 20 69 74 20 69 73 20 4f 4b 20 66 6f 72 20 74  n it is OK for t
15171 68 65 20 6f 74 68 65 72 20 6f 62 6a 65 63 74 20  he other object 
15172 74 6f 0a 2a 2a 20 68 61 76 65 20 61 20 72 65 61  to.** have a rea
15173 64 20 63 75 72 73 6f 72 2e 0a 2a 2a 0a 2a 2a 20  d cursor..**.** 
15174 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 62 65 66  For example, bef
15175 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f 20 61  ore writing to a
15176 6e 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 74  ny part of the t
15177 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 0a 2a 2a  able or index.**
15178 20 72 6f 6f 74 65 64 20 61 74 20 70 61 67 65 20   rooted at page 
15179 69 52 6f 6f 74 2c 20 6f 6e 65 20 73 68 6f 75 6c  iRoot, one shoul
1517a 64 20 63 61 6c 6c 3a 0a 2a 2a 0a 2a 2a 20 20 20  d call:.**.**   
1517b 20 61 73 73 65 72 74 28 20 21 68 61 73 52 65 61   assert( !hasRea
1517c 64 43 6f 6e 66 6c 69 63 74 73 28 70 42 74 72 65  dConflicts(pBtre
1517d 65 2c 20 69 52 6f 6f 74 29 20 29 3b 0a 2a 2f 0a  e, iRoot) );.*/.
1517e 73 74 61 74 69 63 20 69 6e 74 20 68 61 73 52 65  static int hasRe
1517f 61 64 43 6f 6e 66 6c 69 63 74 73 28 42 74 72 65  adConflicts(Btre
15180 65 20 2a 70 42 74 72 65 65 2c 20 50 67 6e 6f 20  e *pBtree, Pgno 
15181 69 52 6f 6f 74 29 7b 0a 20 20 42 74 43 75 72 73  iRoot){.  BtCurs
15182 6f 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d 70  or *p;.  for(p=p
15183 42 74 72 65 65 2d 3e 70 42 74 2d 3e 70 43 75 72  Btree->pBt->pCur
15184 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65  sor; p; p=p->pNe
15185 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  xt){.    if( p->
15186 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74 20  pgnoRoot==iRoot 
15187 0a 20 20 20 20 20 26 26 20 70 2d 3e 70 42 74 72  .     && p->pBtr
15188 65 65 21 3d 70 42 74 72 65 65 0a 20 20 20 20 20  ee!=pBtree.     
15189 26 26 20 30 3d 3d 28 70 2d 3e 70 42 74 72 65 65  && 0==(p->pBtree
1518a 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ->db->flags & SQ
1518b 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69  LITE_ReadUncommi
1518c 74 74 65 64 29 0a 20 20 20 20 29 7b 0a 20 20 20  tted).    ){.   
1518d 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
1518e 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
1518f 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 20 20 20 2f  0;.}.#endif    /
15190 2a 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  * #ifdef SQLITE_
15191 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  DEBUG */../*.** 
15192 51 75 65 72 79 20 74 6f 20 73 65 65 20 69 66 20  Query to see if 
15193 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 20 6d  Btree handle p m
15194 61 79 20 6f 62 74 61 69 6e 20 61 20 6c 6f 63 6b  ay obtain a lock
15195 20 6f 66 20 74 79 70 65 20 65 4c 6f 63 6b 20 0a   of type eLock .
15196 2a 2a 20 28 52 45 41 44 5f 4c 4f 43 4b 20 6f 72  ** (READ_LOCK or
15197 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 6f 6e 20   WRITE_LOCK) on 
15198 74 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72  the table with r
15199 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 2e 20 52  oot-page iTab. R
1519a 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f  eturn.** SQLITE_
1519b 4f 4b 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 6d  OK if the lock m
1519c 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64 20 28  ay be obtained (
1519d 62 79 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 65  by calling.** se
1519e 74 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  tSharedCacheTabl
1519f 65 4c 6f 63 6b 28 29 29 2c 20 6f 72 20 53 51 4c  eLock()), or SQL
151a0 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20 6e 6f  ITE_LOCKED if no
151a1 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
151a2 20 71 75 65 72 79 53 68 61 72 65 64 43 61 63 68   querySharedCach
151a3 65 54 61 62 6c 65 4c 6f 63 6b 28 42 74 72 65 65  eTableLock(Btree
151a4 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 62 2c 20   *p, Pgno iTab, 
151a5 75 38 20 65 4c 6f 63 6b 29 7b 0a 20 20 42 74 53  u8 eLock){.  BtS
151a6 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
151a7 70 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70  pBt;.  BtLock *p
151a8 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28  Iter;..  assert(
151a9 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c   sqlite3BtreeHol
151aa 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20  dsMutex(p) );.  
151ab 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52  assert( eLock==R
151ac 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f 63  EAD_LOCK || eLoc
151ad 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b  k==WRITE_LOCK );
151ae 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62  .  assert( p->db
151af 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
151b0 20 21 28 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 26   !(p->db->flags&
151b1 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d  SQLITE_ReadUncom
151b2 6d 69 74 74 65 64 29 7c 7c 65 4c 6f 63 6b 3d 3d  mitted)||eLock==
151b3 57 52 49 54 45 5f 4c 4f 43 4b 7c 7c 69 54 61 62  WRITE_LOCK||iTab
151b4 3d 3d 31 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 49  ==1 );.  .  /* I
151b5 66 20 72 65 71 75 65 73 74 69 6e 67 20 61 20 77  f requesting a w
151b6 72 69 74 65 2d 6c 6f 63 6b 2c 20 74 68 65 6e 20  rite-lock, then 
151b7 74 68 65 20 42 74 72 65 65 20 6d 75 73 74 20 68  the Btree must h
151b8 61 76 65 20 61 6e 20 6f 70 65 6e 20 77 72 69 74  ave an open writ
151b9 65 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69  e.  ** transacti
151ba 6f 6e 20 6f 6e 20 74 68 69 73 20 66 69 6c 65 2e  on on this file.
151bb 20 41 6e 64 2c 20 6f 62 76 69 6f 75 73 6c 79 2c   And, obviously,
151bc 20 66 6f 72 20 74 68 69 73 20 74 6f 20 62 65 20   for this to be 
151bd 73 6f 20 74 68 65 72 65 20 0a 20 20 2a 2a 20 6d  so there .  ** m
151be 75 73 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 77  ust be an open w
151bf 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
151c0 20 6f 6e 20 74 68 65 20 66 69 6c 65 20 69 74 73   on the file its
151c1 65 6c 66 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  elf..  */.  asse
151c2 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f  rt( eLock==READ_
151c3 4c 4f 43 4b 20 7c 7c 20 28 70 3d 3d 70 42 74 2d  LOCK || (p==pBt-
151c4 3e 70 57 72 69 74 65 72 20 26 26 20 70 2d 3e 69  >pWriter && p->i
151c5 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52  nTrans==TRANS_WR
151c6 49 54 45 29 20 29 3b 0a 20 20 61 73 73 65 72 74  ITE) );.  assert
151c7 28 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f  ( eLock==READ_LO
151c8 43 4b 20 7c 7c 20 70 42 74 2d 3e 69 6e 54 72 61  CK || pBt->inTra
151c9 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f  nsaction==TRANS_
151ca 57 52 49 54 45 20 29 3b 0a 20 20 0a 20 20 2f 2a  WRITE );.  .  /*
151cb 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
151cc 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20   a no-op if the 
151cd 73 68 61 72 65 64 2d 63 61 63 68 65 20 69 73 20  shared-cache is 
151ce 6e 6f 74 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20  not enabled */. 
151cf 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c   if( !p->sharabl
151d0 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  e ){.    return 
151d1 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
151d2 20 20 2f 2a 20 49 66 20 73 6f 6d 65 20 6f 74 68    /* If some oth
151d3 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  er connection is
151d4 20 68 6f 6c 64 69 6e 67 20 61 6e 20 65 78 63 6c   holding an excl
151d5 75 73 69 76 65 20 6c 6f 63 6b 2c 20 74 68 65 0a  usive lock, the.
151d6 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64 20 6c    ** requested l
151d7 6f 63 6b 20 6d 61 79 20 6e 6f 74 20 62 65 20 6f  ock may not be o
151d8 62 74 61 69 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20  btained..  */.  
151d9 69 66 28 20 70 42 74 2d 3e 70 57 72 69 74 65 72  if( pBt->pWriter
151da 21 3d 70 20 26 26 20 70 42 74 2d 3e 69 73 45 78  !=p && pBt->isEx
151db 63 6c 75 73 69 76 65 20 29 7b 0a 20 20 20 20 73  clusive ){.    s
151dc 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e  qlite3Connection
151dd 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20 70  Blocked(p->db, p
151de 42 74 2d 3e 70 57 72 69 74 65 72 2d 3e 64 62 29  Bt->pWriter->db)
151df 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
151e0 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45  ITE_LOCKED_SHARE
151e1 44 43 41 43 48 45 3b 0a 20 20 7d 0a 0a 20 20 66  DCACHE;.  }..  f
151e2 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c  or(pIter=pBt->pL
151e3 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65  ock; pIter; pIte
151e4 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b  r=pIter->pNext){
151e5 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 64  .    /* The cond
151e6 69 74 69 6f 6e 20 28 70 49 74 65 72 2d 3e 65 4c  ition (pIter->eL
151e7 6f 63 6b 21 3d 65 4c 6f 63 6b 29 20 69 6e 20 74  ock!=eLock) in t
151e8 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 66 28  he following if(
151e9 2e 2e 2e 29 20 0a 20 20 20 20 2a 2a 20 73 74 61  ...) .    ** sta
151ea 74 65 6d 65 6e 74 20 69 73 20 61 20 73 69 6d 70  tement is a simp
151eb 6c 69 66 69 63 61 74 69 6f 6e 20 6f 66 3a 0a 20  lification of:. 
151ec 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 28     **.    **   (
151ed 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43  eLock==WRITE_LOC
151ee 4b 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f 63  K || pIter->eLoc
151ef 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 29 0a 20  k==WRITE_LOCK). 
151f0 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 73 69 6e     **.    ** sin
151f1 63 65 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20  ce we know that 
151f2 69 66 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f  if eLock==WRITE_
151f3 4c 4f 43 4b 2c 20 74 68 65 6e 20 6e 6f 20 6f 74  LOCK, then no ot
151f4 68 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20  her connection. 
151f5 20 20 20 2a 2a 20 6d 61 79 20 68 6f 6c 64 20 61     ** may hold a
151f6 20 57 52 49 54 45 5f 4c 4f 43 4b 20 6f 6e 20 61   WRITE_LOCK on a
151f7 6e 79 20 74 61 62 6c 65 20 69 6e 20 74 68 69 73  ny table in this
151f8 20 66 69 6c 65 20 28 73 69 6e 63 65 20 74 68 65   file (since the
151f9 72 65 20 63 61 6e 0a 20 20 20 20 2a 2a 20 6f 6e  re can.    ** on
151fa 6c 79 20 62 65 20 61 20 73 69 6e 67 6c 65 20 77  ly be a single w
151fb 72 69 74 65 72 29 2e 0a 20 20 20 20 2a 2f 0a 20  riter)..    */. 
151fc 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 72     assert( pIter
151fd 2d 3e 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f  ->eLock==READ_LO
151fe 43 4b 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f  CK || pIter->eLo
151ff 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29  ck==WRITE_LOCK )
15200 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 65 4c  ;.    assert( eL
15201 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c  ock==READ_LOCK |
15202 7c 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 3d  | pIter->pBtree=
15203 3d 70 20 7c 7c 20 70 49 74 65 72 2d 3e 65 4c 6f  =p || pIter->eLo
15204 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a  ck==READ_LOCK);.
15205 20 20 20 20 69 66 28 20 70 49 74 65 72 2d 3e 70      if( pIter->p
15206 42 74 72 65 65 21 3d 70 20 26 26 20 70 49 74 65  Btree!=p && pIte
15207 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20  r->iTable==iTab 
15208 26 26 20 70 49 74 65 72 2d 3e 65 4c 6f 63 6b 21  && pIter->eLock!
15209 3d 65 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20  =eLock ){.      
1520a 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f  sqlite3Connectio
1520b 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64 62 2c 20  nBlocked(p->db, 
1520c 70 49 74 65 72 2d 3e 70 42 74 72 65 65 2d 3e 64  pIter->pBtree->d
1520d 62 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65 4c  b);.      if( eL
1520e 6f 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20  ock==WRITE_LOCK 
1520f 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
15210 74 28 20 70 3d 3d 70 42 74 2d 3e 70 57 72 69 74  t( p==pBt->pWrit
15211 65 72 20 29 3b 0a 20 20 20 20 20 20 20 20 70 42  er );.        pB
15212 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 31  t->isPending = 1
15213 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
15214 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f  return SQLITE_LO
15215 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 45  CKED_SHAREDCACHE
15216 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
15217 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
15218 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c  }.#endif /* !SQL
15219 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
1521a 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65  CACHE */..#ifnde
1521b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
1521c 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a  ARED_CACHE./*.**
1521d 20 41 64 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74   Add a lock on t
1521e 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f  he table with ro
1521f 6f 74 2d 70 61 67 65 20 69 54 61 62 6c 65 20 74  ot-page iTable t
15220 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72  o the shared-btr
15221 65 65 20 75 73 65 64 0a 2a 2a 20 62 79 20 42 74  ee used.** by Bt
15222 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 20 50 61  ree handle p. Pa
15223 72 61 6d 65 74 65 72 20 65 4c 6f 63 6b 20 6d 75  rameter eLock mu
15224 73 74 20 62 65 20 65 69 74 68 65 72 20 52 45 41  st be either REA
15225 44 5f 4c 4f 43 4b 20 6f 72 20 0a 2a 2a 20 57 52  D_LOCK or .** WR
15226 49 54 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20  ITE_LOCK..**.** 
15227 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73  This function as
15228 73 75 6d 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77  sumes the follow
15229 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 61 29  ing:.**.**   (a)
1522a 20 54 68 65 20 73 70 65 63 69 66 69 65 64 20 42   The specified B
1522b 74 72 65 65 20 6f 62 6a 65 63 74 20 70 20 69 73  tree object p is
1522c 20 63 6f 6e 6e 65 63 74 65 64 20 74 6f 20 61 20   connected to a 
1522d 73 68 61 72 61 62 6c 65 0a 2a 2a 20 20 20 20 20  sharable.**     
1522e 20 20 64 61 74 61 62 61 73 65 20 28 6f 6e 65 20    database (one 
1522f 77 69 74 68 20 74 68 65 20 42 74 53 68 61 72 65  with the BtShare
15230 64 2e 73 68 61 72 61 62 6c 65 20 66 6c 61 67 20  d.sharable flag 
15231 73 65 74 29 2c 20 61 6e 64 0a 2a 2a 0a 2a 2a 20  set), and.**.** 
15232 20 20 28 62 29 20 4e 6f 20 6f 74 68 65 72 20 42    (b) No other B
15233 74 72 65 65 20 6f 62 6a 65 63 74 73 20 68 6f 6c  tree objects hol
15234 64 20 61 20 6c 6f 63 6b 20 74 68 61 74 20 63 6f  d a lock that co
15235 6e 66 6c 69 63 74 73 0a 2a 2a 20 20 20 20 20 20  nflicts.**      
15236 20 77 69 74 68 20 74 68 65 20 72 65 71 75 65 73   with the reques
15237 74 65 64 20 6c 6f 63 6b 20 28 69 2e 65 2e 20 71  ted lock (i.e. q
15238 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54  uerySharedCacheT
15239 61 62 6c 65 4c 6f 63 6b 28 29 20 68 61 73 0a 2a  ableLock() has.*
1523a 2a 20 20 20 20 20 20 20 61 6c 72 65 61 64 79 20  *       already 
1523b 62 65 65 6e 20 63 61 6c 6c 65 64 20 61 6e 64 20  been called and 
1523c 72 65 74 75 72 6e 65 64 20 53 51 4c 49 54 45 5f  returned SQLITE_
1523d 4f 4b 29 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54  OK)..**.** SQLIT
1523e 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64  E_OK is returned
1523f 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 69 73 20   if the lock is 
15240 61 64 64 65 64 20 73 75 63 63 65 73 73 66 75 6c  added successful
15241 6c 79 2e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  ly. SQLITE_NOMEM
15242 20 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64   .** is returned
15243 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 61 74 74   if a malloc att
15244 65 6d 70 74 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73  empt fails..*/.s
15245 74 61 74 69 63 20 69 6e 74 20 73 65 74 53 68 61  tatic int setSha
15246 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
15247 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 67 6e 6f  k(Btree *p, Pgno
15248 20 69 54 61 62 6c 65 2c 20 75 38 20 65 4c 6f 63   iTable, u8 eLoc
15249 6b 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  k){.  BtShared *
1524a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
1524b 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20  BtLock *pLock = 
1524c 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 49 74  0;.  BtLock *pIt
1524d 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  er;..  assert( s
1524e 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
1524f 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73  Mutex(p) );.  as
15250 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d 52 45 41  sert( eLock==REA
15251 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f 63 6b 3d  D_LOCK || eLock=
15252 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b 0a 20  =WRITE_LOCK );. 
15253 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 21 3d   assert( p->db!=
15254 30 20 29 3b 0a 0a 20 20 2f 2a 20 41 20 63 6f 6e  0 );..  /* A con
15255 6e 65 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65  nection with the
15256 20 72 65 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65   read-uncommitte
15257 64 20 66 6c 61 67 20 73 65 74 20 77 69 6c 6c 20  d flag set will 
15258 6e 65 76 65 72 20 74 72 79 20 74 6f 0a 20 20 2a  never try to.  *
15259 2a 20 6f 62 74 61 69 6e 20 61 20 72 65 61 64 2d  * obtain a read-
1525a 6c 6f 63 6b 20 75 73 69 6e 67 20 74 68 69 73 20  lock using this 
1525b 66 75 6e 63 74 69 6f 6e 2e 20 54 68 65 20 6f 6e  function. The on
1525c 6c 79 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 62 74  ly read-lock obt
1525d 61 69 6e 65 64 0a 20 20 2a 2a 20 62 79 20 61 20  ained.  ** by a 
1525e 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 6e 20 72 65  connection in re
1525f 61 64 2d 75 6e 63 6f 6d 6d 69 74 74 65 64 20 6d  ad-uncommitted m
15260 6f 64 65 20 69 73 20 6f 6e 20 74 68 65 20 73 71  ode is on the sq
15261 6c 69 74 65 5f 6d 61 73 74 65 72 20 0a 20 20 2a  lite_master .  *
15262 2a 20 74 61 62 6c 65 2c 20 61 6e 64 20 74 68 61  * table, and tha
15263 74 20 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e  t lock is obtain
15264 65 64 20 69 6e 20 42 74 72 65 65 42 65 67 69 6e  ed in BtreeBegin
15265 54 72 61 6e 73 28 29 2e 20 20 2a 2f 0a 20 20 61  Trans().  */.  a
15266 73 73 65 72 74 28 20 30 3d 3d 28 70 2d 3e 64 62  ssert( 0==(p->db
15267 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52  ->flags&SQLITE_R
15268 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 20  eadUncommitted) 
15269 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54 45 5f  || eLock==WRITE_
1526a 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68  LOCK );..  /* Th
1526b 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75  is function shou
1526c 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65  ld only be calle
1526d 64 20 6f 6e 20 61 20 73 68 61 72 61 62 6c 65 20  d on a sharable 
1526e 62 2d 74 72 65 65 20 61 66 74 65 72 20 69 74 20  b-tree after it 
1526f 0a 20 20 2a 2a 20 68 61 73 20 62 65 65 6e 20 64  .  ** has been d
15270 65 74 65 72 6d 69 6e 65 64 20 74 68 61 74 20 6e  etermined that n
15271 6f 20 6f 74 68 65 72 20 62 2d 74 72 65 65 20 68  o other b-tree h
15272 6f 6c 64 73 20 61 20 63 6f 6e 66 6c 69 63 74 69  olds a conflicti
15273 6e 67 20 6c 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61  ng lock.  */.  a
15274 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62  ssert( p->sharab
15275 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  le );.  assert( 
15276 53 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79  SQLITE_OK==query
15277 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
15278 4c 6f 63 6b 28 70 2c 20 69 54 61 62 6c 65 2c 20  Lock(p, iTable, 
15279 65 4c 6f 63 6b 29 20 29 3b 0a 0a 20 20 2f 2a 20  eLock) );..  /* 
1527a 46 69 72 73 74 20 73 65 61 72 63 68 20 74 68 65  First search the
1527b 20 6c 69 73 74 20 66 6f 72 20 61 6e 20 65 78 69   list for an exi
1527c 73 74 69 6e 67 20 6c 6f 63 6b 20 6f 6e 20 74 68  sting lock on th
1527d 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 66  is table. */.  f
1527e 6f 72 28 70 49 74 65 72 3d 70 42 74 2d 3e 70 4c  or(pIter=pBt->pL
1527f 6f 63 6b 3b 20 70 49 74 65 72 3b 20 70 49 74 65  ock; pIter; pIte
15280 72 3d 70 49 74 65 72 2d 3e 70 4e 65 78 74 29 7b  r=pIter->pNext){
15281 0a 20 20 20 20 69 66 28 20 70 49 74 65 72 2d 3e  .    if( pIter->
15282 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20 26  iTable==iTable &
15283 26 20 70 49 74 65 72 2d 3e 70 42 74 72 65 65 3d  & pIter->pBtree=
15284 3d 70 20 29 7b 0a 20 20 20 20 20 20 70 4c 6f 63  =p ){.      pLoc
15285 6b 20 3d 20 70 49 74 65 72 3b 0a 20 20 20 20 20  k = pIter;.     
15286 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
15287 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 61  }..  /* If the a
15288 62 6f 76 65 20 73 65 61 72 63 68 20 64 69 64 20  bove search did 
15289 6e 6f 74 20 66 69 6e 64 20 61 20 42 74 4c 6f 63  not find a BtLoc
1528a 6b 20 73 74 72 75 63 74 20 61 73 73 6f 63 69 61  k struct associa
1528b 74 69 6e 67 20 42 74 72 65 65 20 70 0a 20 20 2a  ting Btree p.  *
1528c 2a 20 77 69 74 68 20 74 61 62 6c 65 20 69 54 61  * with table iTa
1528d 62 6c 65 2c 20 61 6c 6c 6f 63 61 74 65 20 6f 6e  ble, allocate on
1528e 65 20 61 6e 64 20 6c 69 6e 6b 20 69 74 20 69 6e  e and link it in
1528f 74 6f 20 74 68 65 20 6c 69 73 74 2e 0a 20 20 2a  to the list..  *
15290 2f 0a 20 20 69 66 28 20 21 70 4c 6f 63 6b 20 29  /.  if( !pLock )
15291 7b 0a 20 20 20 20 70 4c 6f 63 6b 20 3d 20 28 42  {.    pLock = (B
15292 74 4c 6f 63 6b 20 2a 29 73 71 6c 69 74 65 33 4d  tLock *)sqlite3M
15293 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f 66  allocZero(sizeof
15294 28 42 74 4c 6f 63 6b 29 29 3b 0a 20 20 20 20 69  (BtLock));.    i
15295 66 28 20 21 70 4c 6f 63 6b 20 29 7b 0a 20 20 20  f( !pLock ){.   
15296 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
15297 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
15298 20 20 70 4c 6f 63 6b 2d 3e 69 54 61 62 6c 65 20    pLock->iTable 
15299 3d 20 69 54 61 62 6c 65 3b 0a 20 20 20 20 70 4c  = iTable;.    pL
1529a 6f 63 6b 2d 3e 70 42 74 72 65 65 20 3d 20 70 3b  ock->pBtree = p;
1529b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78  .    pLock->pNex
1529c 74 20 3d 20 70 42 74 2d 3e 70 4c 6f 63 6b 3b 0a  t = pBt->pLock;.
1529d 20 20 20 20 70 42 74 2d 3e 70 4c 6f 63 6b 20 3d      pBt->pLock =
1529e 20 70 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 2f   pLock;.  }..  /
1529f 2a 20 53 65 74 20 74 68 65 20 42 74 4c 6f 63 6b  * Set the BtLock
152a0 2e 65 4c 6f 63 6b 20 76 61 72 69 61 62 6c 65 20  .eLock variable 
152a1 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f  to the maximum o
152a2 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f  f the current lo
152a3 63 6b 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 20  ck.  ** and the 
152a4 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b 2e 20  requested lock. 
152a5 54 68 69 73 20 6d 65 61 6e 73 20 69 66 20 61 20  This means if a 
152a6 77 72 69 74 65 2d 6c 6f 63 6b 20 77 61 73 20 61  write-lock was a
152a7 6c 72 65 61 64 79 20 68 65 6c 64 0a 20 20 2a 2a  lready held.  **
152a8 20 61 6e 64 20 61 20 72 65 61 64 2d 6c 6f 63 6b   and a read-lock
152a9 20 72 65 71 75 65 73 74 65 64 2c 20 77 65 20 64   requested, we d
152aa 6f 6e 27 74 20 69 6e 63 6f 72 72 65 63 74 6c 79  on't incorrectly
152ab 20 64 6f 77 6e 67 72 61 64 65 20 74 68 65 20 6c   downgrade the l
152ac 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ock..  */.  asse
152ad 72 74 28 20 57 52 49 54 45 5f 4c 4f 43 4b 3e 52  rt( WRITE_LOCK>R
152ae 45 41 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66  EAD_LOCK );.  if
152af 28 20 65 4c 6f 63 6b 3e 70 4c 6f 63 6b 2d 3e 65  ( eLock>pLock->e
152b0 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63  Lock ){.    pLoc
152b1 6b 2d 3e 65 4c 6f 63 6b 20 3d 20 65 4c 6f 63 6b  k->eLock = eLock
152b2 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
152b3 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e  SQLITE_OK;.}.#en
152b4 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f  dif /* !SQLITE_O
152b5 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
152b6 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
152b7 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
152b8 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 52 65 6c 65  CACHE./*.** Rele
152b9 61 73 65 20 61 6c 6c 20 74 68 65 20 74 61 62 6c  ase all the tabl
152ba 65 20 6c 6f 63 6b 73 20 28 6c 6f 63 6b 73 20 6f  e locks (locks o
152bb 62 74 61 69 6e 65 64 20 76 69 61 20 63 61 6c 6c  btained via call
152bc 73 20 74 6f 0a 2a 2a 20 74 68 65 20 73 65 74 53  s to.** the setS
152bd 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c  haredCacheTableL
152be 6f 63 6b 28 29 20 70 72 6f 63 65 64 75 72 65 29  ock() procedure)
152bf 20 68 65 6c 64 20 62 79 20 42 74 72 65 65 20 6f   held by Btree o
152c0 62 6a 65 63 74 20 70 2e 0a 2a 2a 0a 2a 2a 20 54  bject p..**.** T
152c1 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73  his function ass
152c2 75 6d 65 73 20 74 68 61 74 20 42 74 72 65 65 20  umes that Btree 
152c3 70 20 68 61 73 20 61 6e 20 6f 70 65 6e 20 72 65  p has an open re
152c4 61 64 20 6f 72 20 77 72 69 74 65 20 0a 2a 2a 20  ad or write .** 
152c5 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20  transaction. If 
152c6 69 74 20 64 6f 65 73 20 6e 6f 74 2c 20 74 68 65  it does not, the
152c7 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 2e 69  n the BtShared.i
152c8 73 50 65 6e 64 69 6e 67 20 76 61 72 69 61 62 6c  sPending variabl
152c9 65 0a 2a 2a 20 6d 61 79 20 62 65 20 69 6e 63 6f  e.** may be inco
152ca 72 72 65 63 74 6c 79 20 63 6c 65 61 72 65 64 2e  rrectly cleared.
152cb 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
152cc 63 6c 65 61 72 41 6c 6c 53 68 61 72 65 64 43 61  clearAllSharedCa
152cd 63 68 65 54 61 62 6c 65 4c 6f 63 6b 73 28 42 74  cheTableLocks(Bt
152ce 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61  ree *p){.  BtSha
152cf 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
152d0 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 2a 70 70  t;.  BtLock **pp
152d1 49 74 65 72 20 3d 20 26 70 42 74 2d 3e 70 4c 6f  Iter = &pBt->pLo
152d2 63 6b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  ck;..  assert( s
152d3 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
152d4 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61 73  Mutex(p) );.  as
152d5 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62 6c  sert( p->sharabl
152d6 65 20 7c 7c 20 30 3d 3d 2a 70 70 49 74 65 72 20  e || 0==*ppIter 
152d7 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  );.  assert( p->
152d8 69 6e 54 72 61 6e 73 3e 30 20 29 3b 0a 0a 20 20  inTrans>0 );..  
152d9 77 68 69 6c 65 28 20 2a 70 70 49 74 65 72 20 29  while( *ppIter )
152da 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 4c  {.    BtLock *pL
152db 6f 63 6b 20 3d 20 2a 70 70 49 74 65 72 3b 0a 20  ock = *ppIter;. 
152dc 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e     assert( pBt->
152dd 69 73 45 78 63 6c 75 73 69 76 65 3d 3d 30 20 7c  isExclusive==0 |
152de 7c 20 70 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d  | pBt->pWriter==
152df 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 20 29 3b  pLock->pBtree );
152e0 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f  .    assert( pLo
152e1 63 6b 2d 3e 70 42 74 72 65 65 2d 3e 69 6e 54 72  ck->pBtree->inTr
152e2 61 6e 73 3e 3d 70 4c 6f 63 6b 2d 3e 65 4c 6f 63  ans>=pLock->eLoc
152e3 6b 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 6f  k );.    if( pLo
152e4 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70 20 29 7b  ck->pBtree==p ){
152e5 0a 20 20 20 20 20 20 2a 70 70 49 74 65 72 20 3d  .      *ppIter =
152e6 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20   pLock->pNext;. 
152e7 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f       assert( pLo
152e8 63 6b 2d 3e 69 54 61 62 6c 65 21 3d 31 20 7c 7c  ck->iTable!=1 ||
152e9 20 70 4c 6f 63 6b 3d 3d 26 70 2d 3e 6c 6f 63 6b   pLock==&p->lock
152ea 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4c   );.      if( pL
152eb 6f 63 6b 2d 3e 69 54 61 62 6c 65 21 3d 31 20 29  ock->iTable!=1 )
152ec 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
152ed 33 5f 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20  3_free(pLock);. 
152ee 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
152ef 7b 0a 20 20 20 20 20 20 70 70 49 74 65 72 20 3d  {.      ppIter =
152f0 20 26 70 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a   &pLock->pNext;.
152f1 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 73 73      }.  }..  ass
152f2 65 72 74 28 20 70 42 74 2d 3e 69 73 50 65 6e 64  ert( pBt->isPend
152f3 69 6e 67 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70  ing==0 || pBt->p
152f4 57 72 69 74 65 72 20 29 3b 0a 20 20 69 66 28 20  Writer );.  if( 
152f5 70 42 74 2d 3e 70 57 72 69 74 65 72 3d 3d 70 20  pBt->pWriter==p 
152f6 29 7b 0a 20 20 20 20 70 42 74 2d 3e 70 57 72 69  ){.    pBt->pWri
152f7 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 70 42 74  ter = 0;.    pBt
152f8 2d 3e 69 73 45 78 63 6c 75 73 69 76 65 20 3d 20  ->isExclusive = 
152f9 30 3b 0a 20 20 20 20 70 42 74 2d 3e 69 73 50 65  0;.    pBt->isPe
152fa 6e 64 69 6e 67 20 3d 20 30 3b 0a 20 20 7d 65 6c  nding = 0;.  }el
152fb 73 65 20 69 66 28 20 70 42 74 2d 3e 6e 54 72 61  se if( pBt->nTra
152fc 6e 73 61 63 74 69 6f 6e 3d 3d 32 20 29 7b 0a 20  nsaction==2 ){. 
152fd 20 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74     /* This funct
152fe 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68  ion is called wh
152ff 65 6e 20 42 74 72 65 65 20 70 20 69 73 20 63 6f  en Btree p is co
15300 6e 63 6c 75 64 69 6e 67 20 69 74 73 20 0a 20 20  ncluding its .  
15301 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e    ** transaction
15302 2e 20 49 66 20 74 68 65 72 65 20 63 75 72 72 65  . If there curre
15303 6e 74 6c 79 20 65 78 69 73 74 73 20 61 20 77 72  ntly exists a wr
15304 69 74 65 72 2c 20 61 6e 64 20 70 20 69 73 20 6e  iter, and p is n
15305 6f 74 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 77  ot.    ** that w
15306 72 69 74 65 72 2c 20 74 68 65 6e 20 74 68 65 20  riter, then the 
15307 6e 75 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20  number of locks 
15308 68 65 6c 64 20 62 79 20 63 6f 6e 6e 65 63 74 69  held by connecti
15309 6f 6e 73 20 6f 74 68 65 72 0a 20 20 20 20 2a 2a  ons other.    **
1530a 20 74 68 61 6e 20 74 68 65 20 77 72 69 74 65 72   than the writer
1530b 20 6d 75 73 74 20 62 65 20 61 62 6f 75 74 20 74   must be about t
1530c 6f 20 64 72 6f 70 20 74 6f 20 7a 65 72 6f 2e 20  o drop to zero. 
1530d 49 6e 20 74 68 69 73 20 63 61 73 65 0a 20 20 20  In this case.   
1530e 20 2a 2a 20 73 65 74 20 74 68 65 20 69 73 50 65   ** set the isPe
1530f 6e 64 69 6e 67 20 66 6c 61 67 20 74 6f 20 30 2e  nding flag to 0.
15310 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49  .    **.    ** I
15311 66 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 63  f there is not c
15312 75 72 72 65 6e 74 6c 79 20 61 20 77 72 69 74 65  urrently a write
15313 72 2c 20 74 68 65 6e 20 42 74 53 68 61 72 65 64  r, then BtShared
15314 2e 69 73 50 65 6e 64 69 6e 67 20 6d 75 73 74 0a  .isPending must.
15315 20 20 20 20 2a 2a 20 62 65 20 7a 65 72 6f 20 61      ** be zero a
15316 6c 72 65 61 64 79 2e 20 53 6f 20 74 68 69 73 20  lready. So this 
15317 6e 65 78 74 20 6c 69 6e 65 20 69 73 20 68 61 72  next line is har
15318 6d 6c 65 73 73 20 69 6e 20 74 68 61 74 20 63 61  mless in that ca
15319 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  se..    */.    p
1531a 42 74 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20  Bt->isPending = 
1531b 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0;.  }.}../*.** 
1531c 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63 68  This function ch
1531d 61 6e 67 65 73 20 61 6c 6c 20 77 72 69 74 65 2d  anges all write-
1531e 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 20 42 74  locks held by Bt
1531f 72 65 65 20 70 20 69 6e 74 6f 20 72 65 61 64 2d  ree p into read-
15320 6c 6f 63 6b 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  locks..*/.static
15321 20 76 6f 69 64 20 64 6f 77 6e 67 72 61 64 65 41   void downgradeA
15322 6c 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62  llSharedCacheTab
15323 6c 65 4c 6f 63 6b 73 28 42 74 72 65 65 20 2a 70  leLocks(Btree *p
15324 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  ){.  BtShared *p
15325 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 69  Bt = p->pBt;.  i
15326 66 28 20 70 42 74 2d 3e 70 57 72 69 74 65 72 3d  f( pBt->pWriter=
15327 3d 70 20 29 7b 0a 20 20 20 20 42 74 4c 6f 63 6b  =p ){.    BtLock
15328 20 2a 70 4c 6f 63 6b 3b 0a 20 20 20 20 70 42 74   *pLock;.    pBt
15329 2d 3e 70 57 72 69 74 65 72 20 3d 20 30 3b 0a 20  ->pWriter = 0;. 
1532a 20 20 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73     pBt->isExclus
1532b 69 76 65 20 3d 20 30 3b 0a 20 20 20 20 70 42 74  ive = 0;.    pBt
1532c 2d 3e 69 73 50 65 6e 64 69 6e 67 20 3d 20 30 3b  ->isPending = 0;
1532d 0a 20 20 20 20 66 6f 72 28 70 4c 6f 63 6b 3d 70  .    for(pLock=p
1532e 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70 4c 6f 63 6b  Bt->pLock; pLock
1532f 3b 20 70 4c 6f 63 6b 3d 70 4c 6f 63 6b 2d 3e 70  ; pLock=pLock->p
15330 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73 73  Next){.      ass
15331 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63  ert( pLock->eLoc
15332 6b 3d 3d 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20  k==READ_LOCK || 
15333 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d 3d 70  pLock->pBtree==p
15334 20 29 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d   );.      pLock-
15335 3e 65 4c 6f 63 6b 20 3d 20 52 45 41 44 5f 4c 4f  >eLock = READ_LO
15336 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  CK;.    }.  }.}.
15337 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
15338 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
15339 43 48 45 20 2a 2f 0a 0a 73 74 61 74 69 63 20 76  CHE */..static v
1533a 6f 69 64 20 72 65 6c 65 61 73 65 50 61 67 65 28  oid releasePage(
1533b 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 3b  MemPage *pPage);
1533c 20 20 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66    /* Forward ref
1533d 65 72 65 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  erence */../*.**
1533e 2a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  *** This routine
1533f 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 65 20   is used inside 
15340 6f 66 20 61 73 73 65 72 74 28 29 20 6f 6e 6c 79  of assert() only
15341 20 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 56 65 72 69   ****.**.** Veri
15342 66 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73  fy that the curs
15343 6f 72 20 68 6f 6c 64 73 20 74 68 65 20 6d 75 74  or holds the mut
15344 65 78 20 6f 6e 20 69 74 73 20 42 74 53 68 61 72  ex on its BtShar
15345 65 64 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  ed.*/.#ifdef SQL
15346 49 54 45 5f 44 45 42 55 47 0a 73 74 61 74 69 63  ITE_DEBUG.static
15347 20 69 6e 74 20 63 75 72 73 6f 72 48 6f 6c 64 73   int cursorHolds
15348 4d 75 74 65 78 28 42 74 43 75 72 73 6f 72 20 2a  Mutex(BtCursor *
15349 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  p){.  return sql
1534a 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1534b 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a  p->pBt->mutex);.
1534c 7d 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64  }.#endif...#ifnd
1534d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
1534e 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 49 6e  NCRBLOB./*.** In
1534f 76 61 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65  validate the ove
15350 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20  rflow page-list 
15351 63 61 63 68 65 20 66 6f 72 20 63 75 72 73 6f 72  cache for cursor
15352 20 70 43 75 72 2c 20 69 66 20 61 6e 79 2e 0a 2a   pCur, if any..*
15353 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e  /.static void in
15354 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77  validateOverflow
15355 43 61 63 68 65 28 42 74 43 75 72 73 6f 72 20 2a  Cache(BtCursor *
15356 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28  pCur){.  assert(
15357 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
15358 78 28 70 43 75 72 29 20 29 3b 0a 20 20 73 71 6c  x(pCur) );.  sql
15359 69 74 65 33 5f 66 72 65 65 28 70 43 75 72 2d 3e  ite3_free(pCur->
1535a 61 4f 76 65 72 66 6c 6f 77 29 3b 0a 20 20 70 43  aOverflow);.  pC
1535b 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d 20  ur->aOverflow = 
1535c 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 61  0;.}../*.** Inva
1535d 6c 69 64 61 74 65 20 74 68 65 20 6f 76 65 72 66  lidate the overf
1535e 6c 6f 77 20 70 61 67 65 2d 6c 69 73 74 20 63 61  low page-list ca
1535f 63 68 65 20 66 6f 72 20 61 6c 6c 20 63 75 72 73  che for all curs
15360 6f 72 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 6f 6e  ors opened.** on
15361 20 74 68 65 20 73 68 61 72 65 64 20 62 74 72 65   the shared btre
15362 65 20 73 74 72 75 63 74 75 72 65 20 70 42 74 2e  e structure pBt.
15363 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
15364 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65  invalidateAllOve
15365 72 66 6c 6f 77 43 61 63 68 65 28 42 74 53 68 61  rflowCache(BtSha
15366 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 42 74 43  red *pBt){.  BtC
15367 75 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65  ursor *p;.  asse
15368 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
15369 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
1536a 78 29 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42  x) );.  for(p=pB
1536b 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70  t->pCursor; p; p
1536c 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  =p->pNext){.    
1536d 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c  invalidateOverfl
1536e 6f 77 43 61 63 68 65 28 70 29 3b 0a 20 20 7d 0a  owCache(p);.  }.
1536f 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
15370 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
15371 20 62 65 66 6f 72 65 20 6d 6f 64 69 66 79 69 6e   before modifyin
15372 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  g the contents o
15373 66 20 61 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20  f a table.** to 
15374 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 69  invalidate any i
15375 6e 63 72 62 6c 6f 62 20 63 75 72 73 6f 72 73 20  ncrblob cursors 
15376 74 68 61 74 20 61 72 65 20 6f 70 65 6e 20 6f 6e  that are open on
15377 20 74 68 65 0a 2a 2a 20 72 6f 77 20 6f 72 20 6f   the.** row or o
15378 6e 65 20 6f 66 20 74 68 65 20 72 6f 77 73 20 62  ne of the rows b
15379 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e 0a 2a  eing modified..*
1537a 2a 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65 6e 74  *.** If argument
1537b 20 69 73 43 6c 65 61 72 54 61 62 6c 65 20 69 73   isClearTable is
1537c 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20   true, then the 
1537d 65 6e 74 69 72 65 20 63 6f 6e 74 65 6e 74 73 20  entire contents 
1537e 6f 66 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20  of the.** table 
1537f 69 73 20 61 62 6f 75 74 20 74 6f 20 62 65 20 64  is about to be d
15380 65 6c 65 74 65 64 2e 20 49 6e 20 74 68 69 73 20  eleted. In this 
15381 63 61 73 65 20 69 6e 76 61 6c 69 64 61 74 65 20  case invalidate 
15382 61 6c 6c 20 69 6e 63 72 62 6c 6f 62 0a 2a 2a 20  all incrblob.** 
15383 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20  cursors open on 
15384 61 6e 79 20 72 6f 77 20 77 69 74 68 69 6e 20 74  any row within t
15385 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f  he table with ro
15386 6f 74 2d 70 61 67 65 20 70 67 6e 6f 52 6f 6f 74  ot-page pgnoRoot
15387 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ..**.** Otherwis
15388 65 2c 20 69 66 20 61 72 67 75 6d 65 6e 74 20 69  e, if argument i
15389 73 43 6c 65 61 72 54 61 62 6c 65 20 69 73 20 66  sClearTable is f
1538a 61 6c 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72  alse, then the r
1538b 6f 77 20 77 69 74 68 0a 2a 2a 20 72 6f 77 69 64  ow with.** rowid
1538c 20 69 52 6f 77 20 69 73 20 62 65 69 6e 67 20 72   iRow is being r
1538d 65 70 6c 61 63 65 64 20 6f 72 20 64 65 6c 65 74  eplaced or delet
1538e 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  ed. In this case
1538f 20 69 6e 76 61 6c 69 64 61 74 65 0a 2a 2a 20 6f   invalidate.** o
15390 6e 6c 79 20 74 68 6f 73 65 20 69 6e 63 72 62 6c  nly those incrbl
15391 6f 62 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20  ob cursors open 
15392 6f 6e 20 74 68 61 74 20 73 70 65 63 69 66 69 63  on that specific
15393 20 72 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   row..*/.static 
15394 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 49  void invalidateI
15395 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 0a  ncrblobCursors(.
15396 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 2c    Btree *pBtree,
15397 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
15398 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 74   database file t
15399 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20 69 36 34  o check */.  i64
1539a 20 69 52 6f 77 2c 20 20 20 20 20 20 20 20 20 20   iRow,          
1539b 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 77 69       /* The rowi
1539c 64 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 20  d that might be 
1539d 63 68 61 6e 67 69 6e 67 20 2a 2f 0a 20 20 69 6e  changing */.  in
1539e 74 20 69 73 43 6c 65 61 72 54 61 62 6c 65 20 20  t isClearTable  
1539f 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
153a0 20 61 6c 6c 20 72 6f 77 73 20 61 72 65 20 62 65   all rows are be
153a1 69 6e 67 20 64 65 6c 65 74 65 64 20 2a 2f 0a 29  ing deleted */.)
153a2 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b  {.  BtCursor *p;
153a3 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
153a4 20 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 3b 0a   = pBtree->pBt;.
153a5 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
153a6 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78  3BtreeHoldsMutex
153a7 28 70 42 74 72 65 65 29 20 29 3b 0a 20 20 66 6f  (pBtree) );.  fo
153a8 72 28 70 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72  r(p=pBt->pCursor
153a9 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29  ; p; p=p->pNext)
153aa 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 69 73 49  {.    if( p->isI
153ab 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 26 26  ncrblobHandle &&
153ac 20 28 69 73 43 6c 65 61 72 54 61 62 6c 65 20 7c   (isClearTable |
153ad 7c 20 70 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d  | p->info.nKey==
153ae 69 52 6f 77 29 20 29 7b 0a 20 20 20 20 20 20 70  iRow) ){.      p
153af 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f  ->eState = CURSO
153b0 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 7d  R_INVALID;.    }
153b1 0a 20 20 7d 0a 7d 0a 0a 23 65 6c 73 65 0a 20 20  .  }.}..#else.  
153b2 2f 2a 20 53 74 75 62 20 66 75 6e 63 74 69 6f 6e  /* Stub function
153b3 73 20 77 68 65 6e 20 49 4e 43 52 42 4c 4f 42 20  s when INCRBLOB 
153b4 69 73 20 6f 6d 69 74 74 65 64 20 2a 2f 0a 20 20  is omitted */.  
153b5 23 64 65 66 69 6e 65 20 69 6e 76 61 6c 69 64 61  #define invalida
153b6 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28  teOverflowCache(
153b7 78 29 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76  x).  #define inv
153b8 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c  alidateAllOverfl
153b9 6f 77 43 61 63 68 65 28 78 29 0a 20 20 23 64 65  owCache(x).  #de
153ba 66 69 6e 65 20 69 6e 76 61 6c 69 64 61 74 65 49  fine invalidateI
153bb 6e 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 78  ncrblobCursors(x
153bc 2c 79 2c 7a 29 0a 23 65 6e 64 69 66 20 2f 2a 20  ,y,z).#endif /* 
153bd 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
153be 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53  BLOB */../*.** S
153bf 65 74 20 62 69 74 20 70 67 6e 6f 20 6f 66 20 74  et bit pgno of t
153c0 68 65 20 42 74 53 68 61 72 65 64 2e 70 48 61 73  he BtShared.pHas
153c1 43 6f 6e 74 65 6e 74 20 62 69 74 76 65 63 2e 20  Content bitvec. 
153c2 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 0a  This is called .
153c3 2a 2a 20 77 68 65 6e 20 61 20 70 61 67 65 20 74  ** when a page t
153c4 68 61 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63  hat previously c
153c5 6f 6e 74 61 69 6e 65 64 20 64 61 74 61 20 62 65  ontained data be
153c6 63 6f 6d 65 73 20 61 20 66 72 65 65 2d 6c 69 73  comes a free-lis
153c7 74 20 6c 65 61 66 20 0a 2a 2a 20 70 61 67 65 2e  t leaf .** page.
153c8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 42 74 53 68 61  .**.** The BtSha
153c9 72 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20  red.pHasContent 
153ca 62 69 74 76 65 63 20 65 78 69 73 74 73 20 74 6f  bitvec exists to
153cb 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 61 6e 20   work around an 
153cc 6f 62 73 63 75 72 65 0a 2a 2a 20 62 75 67 20 63  obscure.** bug c
153cd 61 75 73 65 64 20 62 79 20 74 68 65 20 69 6e 74  aused by the int
153ce 65 72 61 63 74 69 6f 6e 20 6f 66 20 74 77 6f 20  eraction of two 
153cf 75 73 65 66 75 6c 20 49 4f 20 6f 70 74 69 6d 69  useful IO optimi
153d0 7a 61 74 69 6f 6e 73 20 73 75 72 72 6f 75 6e 64  zations surround
153d1 69 6e 67 0a 2a 2a 20 66 72 65 65 2d 6c 69 73 74  ing.** free-list
153d2 20 6c 65 61 66 20 70 61 67 65 73 3a 0a 2a 2a 0a   leaf pages:.**.
153d3 2a 2a 20 20 20 31 29 20 57 68 65 6e 20 61 6c 6c  **   1) When all
153d4 20 64 61 74 61 20 69 73 20 64 65 6c 65 74 65 64   data is deleted
153d5 20 66 72 6f 6d 20 61 20 70 61 67 65 20 61 6e 64   from a page and
153d6 20 74 68 65 20 70 61 67 65 20 62 65 63 6f 6d 65   the page become
153d7 73 0a 2a 2a 20 20 20 20 20 20 61 20 66 72 65 65  s.**      a free
153d8 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67 65 2c  -list leaf page,
153d9 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
153da 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20   written to the 
153db 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20 20 20  database.**     
153dc 20 28 61 73 20 66 72 65 65 2d 6c 69 73 74 20 6c   (as free-list l
153dd 65 61 66 20 70 61 67 65 73 20 63 6f 6e 74 61 69  eaf pages contai
153de 6e 20 6e 6f 20 6d 65 61 6e 69 6e 67 66 75 6c 20  n no meaningful 
153df 64 61 74 61 29 2e 20 53 6f 6d 65 74 69 6d 65 73  data). Sometimes
153e0 0a 2a 2a 20 20 20 20 20 20 73 75 63 68 20 61 20  .**      such a 
153e1 70 61 67 65 20 69 73 20 6e 6f 74 20 65 76 65 6e  page is not even
153e2 20 6a 6f 75 72 6e 61 6c 6c 65 64 20 28 61 73 20   journalled (as 
153e3 69 74 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 6d  it will not be m
153e4 6f 64 69 66 69 65 64 2c 0a 2a 2a 20 20 20 20 20  odified,.**     
153e5 20 77 68 79 20 62 6f 74 68 65 72 20 6a 6f 75 72   why bother jour
153e6 6e 61 6c 6c 69 6e 67 20 69 74 3f 29 2e 0a 2a 2a  nalling it?)..**
153e7 0a 2a 2a 20 20 20 32 29 20 57 68 65 6e 20 61 20  .**   2) When a 
153e8 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70  free-list leaf p
153e9 61 67 65 20 69 73 20 72 65 75 73 65 64 2c 20 69  age is reused, i
153ea 74 73 20 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f  ts content is no
153eb 74 20 72 65 61 64 0a 2a 2a 20 20 20 20 20 20 66  t read.**      f
153ec 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
153ed 20 6f 72 20 77 72 69 74 74 65 6e 20 74 6f 20 74   or written to t
153ee 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
153ef 28 77 68 79 20 73 68 6f 75 6c 64 20 69 74 0a 2a  (why should it.*
153f0 2a 20 20 20 20 20 20 62 65 2c 20 69 66 20 69 74  *      be, if it
153f1 20 69 73 20 6e 6f 74 20 61 74 20 61 6c 6c 20 6d   is not at all m
153f2 65 61 6e 69 6e 67 66 75 6c 3f 29 2e 0a 2a 2a 0a  eaningful?)..**.
153f3 2a 2a 20 42 79 20 74 68 65 6d 73 65 6c 76 65 73  ** By themselves
153f4 2c 20 74 68 65 73 65 20 6f 70 74 69 6d 69 7a 61  , these optimiza
153f5 74 69 6f 6e 73 20 77 6f 72 6b 20 66 69 6e 65 20  tions work fine 
153f6 61 6e 64 20 70 72 6f 76 69 64 65 20 61 20 68 61  and provide a ha
153f7 6e 64 79 0a 2a 2a 20 70 65 72 66 6f 72 6d 61 6e  ndy.** performan
153f8 63 65 20 62 6f 6f 73 74 20 74 6f 20 62 75 6c 6b  ce boost to bulk
153f9 20 64 65 6c 65 74 65 20 6f 72 20 69 6e 73 65 72   delete or inser
153fa 74 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 48 6f  t operations. Ho
153fb 77 65 76 65 72 2c 20 69 66 0a 2a 2a 20 61 20 70  wever, if.** a p
153fc 61 67 65 20 69 73 20 6d 6f 76 65 64 20 74 6f 20  age is moved to 
153fd 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 61 6e  the free-list an
153fe 64 20 74 68 65 6e 20 72 65 75 73 65 64 20 77 69  d then reused wi
153ff 74 68 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a  thin the same.**
15400 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 20   transaction, a 
15401 70 72 6f 62 6c 65 6d 20 63 6f 6d 65 73 20 75 70  problem comes up
15402 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 69 73  . If the page is
15403 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 20   not journalled 
15404 77 68 65 6e 0a 2a 2a 20 69 74 20 69 73 20 6d 6f  when.** it is mo
15405 76 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 2d  ved to the free-
15406 6c 69 73 74 20 61 6e 64 20 69 74 20 69 73 20 61  list and it is a
15407 6c 73 6f 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c 6c  lso not journall
15408 65 64 20 77 68 65 6e 20 69 74 0a 2a 2a 20 69 73  ed when it.** is
15409 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20   extracted from 
1540a 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 61 6e  the free-list an
1540b 64 20 72 65 75 73 65 64 2c 20 74 68 65 6e 20 74  d reused, then t
1540c 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61  he original data
1540d 0a 2a 2a 20 6d 61 79 20 62 65 20 6c 6f 73 74 2e  .** may be lost.
1540e 20 49 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66   In the event of
1540f 20 61 20 72 6f 6c 6c 62 61 63 6b 2c 20 69 74 20   a rollback, it 
15410 6d 61 79 20 6e 6f 74 20 62 65 20 70 6f 73 73 69  may not be possi
15411 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 73 74 6f 72  ble.** to restor
15412 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  e the database t
15413 6f 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 63  o its original c
15414 6f 6e 66 69 67 75 72 61 74 69 6f 6e 2e 0a 2a 2a  onfiguration..**
15415 0a 2a 2a 20 54 68 65 20 73 6f 6c 75 74 69 6f 6e  .** The solution
15416 20 69 73 20 74 68 65 20 42 74 53 68 61 72 65 64   is the BtShared
15417 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62 69 74  .pHasContent bit
15418 76 65 63 2e 20 57 68 65 6e 65 76 65 72 20 61 20  vec. Whenever a 
15419 70 61 67 65 20 69 73 20 0a 2a 2a 20 6d 6f 76 65  page is .** move
1541a 64 20 74 6f 20 62 65 63 6f 6d 65 20 61 20 66 72  d to become a fr
1541b 65 65 2d 6c 69 73 74 20 6c 65 61 66 20 70 61 67  ee-list leaf pag
1541c 65 2c 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  e, the correspon
1541d 64 69 6e 67 20 62 69 74 20 69 73 0a 2a 2a 20 73  ding bit is.** s
1541e 65 74 20 69 6e 20 74 68 65 20 62 69 74 76 65 63  et in the bitvec
1541f 2e 20 57 68 65 6e 65 76 65 72 20 61 20 6c 65 61  . Whenever a lea
15420 66 20 70 61 67 65 20 69 73 20 65 78 74 72 61 63  f page is extrac
15421 74 65 64 20 66 72 6f 6d 20 74 68 65 20 66 72 65  ted from the fre
15422 65 2d 6c 69 73 74 2c 0a 2a 2a 20 6f 70 74 69 6d  e-list,.** optim
15423 69 7a 61 74 69 6f 6e 20 32 20 61 62 6f 76 65 20  ization 2 above 
15424 69 73 20 6f 6d 69 74 74 65 64 20 69 66 20 74 68  is omitted if th
15425 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
15426 62 69 74 20 69 73 20 61 6c 72 65 61 64 79 0a 2a  bit is already.*
15427 2a 20 73 65 74 20 69 6e 20 42 74 53 68 61 72 65  * set in BtShare
15428 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 2e 20 54  d.pHasContent. T
15429 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
1542a 68 65 20 62 69 74 76 65 63 20 61 72 65 20 63 6c  he bitvec are cl
1542b 65 61 72 65 64 0a 2a 2a 20 61 74 20 74 68 65 20  eared.** at the 
1542c 65 6e 64 20 6f 66 20 65 76 65 72 79 20 74 72 61  end of every tra
1542d 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  nsaction..*/.sta
1542e 74 69 63 20 69 6e 74 20 62 74 72 65 65 53 65 74  tic int btreeSet
1542f 48 61 73 43 6f 6e 74 65 6e 74 28 42 74 53 68 61  HasContent(BtSha
15430 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 70  red *pBt, Pgno p
15431 67 6e 6f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  gno){.  int rc =
15432 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
15433 28 20 21 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74  ( !pBt->pHasCont
15434 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  ent ){.    int n
15435 50 61 67 65 20 3d 20 31 30 30 3b 0a 20 20 20 20  Page = 100;.    
15436 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
15437 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65  count(pBt->pPage
15438 72 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 20  r, &nPage);.    
15439 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 50 61 67  /* If sqlite3Pag
1543a 65 72 50 61 67 65 63 6f 75 6e 74 28 29 20 66 61  erPagecount() fa
1543b 69 6c 73 20 74 68 65 72 65 20 69 73 20 6e 6f 20  ils there is no 
1543c 68 61 72 6d 20 62 65 63 61 75 73 65 20 74 68 65  harm because the
1543d 0a 20 20 20 20 2a 2a 20 6e 50 61 67 65 20 76 61  .    ** nPage va
1543e 72 69 61 62 6c 65 20 69 73 20 75 6e 63 68 61 6e  riable is unchan
1543f 67 65 64 20 66 72 6f 6d 20 69 74 73 20 64 65 66  ged from its def
15440 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 31 30  ault value of 10
15441 30 20 2a 2f 0a 20 20 20 20 70 42 74 2d 3e 70 48  0 */.    pBt->pH
15442 61 73 43 6f 6e 74 65 6e 74 20 3d 20 73 71 6c 69  asContent = sqli
15443 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28  te3BitvecCreate(
15444 28 75 33 32 29 6e 50 61 67 65 29 3b 0a 20 20 20  (u32)nPage);.   
15445 20 69 66 28 20 21 70 42 74 2d 3e 70 48 61 73 43   if( !pBt->pHasC
15446 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20 20  ontent ){.      
15447 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
15448 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  M;.    }.  }.  i
15449 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1544a 20 26 26 20 70 67 6e 6f 3c 3d 73 71 6c 69 74 65   && pgno<=sqlite
1544b 33 42 69 74 76 65 63 53 69 7a 65 28 70 42 74 2d  3BitvecSize(pBt-
1544c 3e 70 48 61 73 43 6f 6e 74 65 6e 74 29 20 29 7b  >pHasContent) ){
1544d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1544e 33 42 69 74 76 65 63 53 65 74 28 70 42 74 2d 3e  3BitvecSet(pBt->
1544f 70 48 61 73 43 6f 6e 74 65 6e 74 2c 20 70 67 6e  pHasContent, pgn
15450 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  o);.  }.  return
15451 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75   rc;.}../*.** Qu
15452 65 72 79 20 74 68 65 20 42 74 53 68 61 72 65 64  ery the BtShared
15453 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 76 65 63  .pHasContent vec
15454 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  tor..**.** This 
15455 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
15456 65 64 20 77 68 65 6e 20 61 20 66 72 65 65 2d 6c  ed when a free-l
15457 69 73 74 20 6c 65 61 66 20 70 61 67 65 20 69 73  ist leaf page is
15458 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
15459 65 0a 2a 2a 20 66 72 65 65 2d 6c 69 73 74 20 66  e.** free-list f
1545a 6f 72 20 72 65 75 73 65 2e 20 49 74 20 72 65 74  or reuse. It ret
1545b 75 72 6e 73 20 66 61 6c 73 65 20 69 66 20 69 74  urns false if it
1545c 20 69 73 20 73 61 66 65 20 74 6f 20 72 65 74 72   is safe to retr
1545d 69 65 76 65 20 74 68 65 0a 2a 2a 20 70 61 67 65  ieve the.** page
1545e 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20   from the pager 
1545f 6c 61 79 65 72 20 77 69 74 68 20 74 68 65 20 27  layer with the '
15460 6e 6f 2d 63 6f 6e 74 65 6e 74 27 20 66 6c 61 67  no-content' flag
15461 20 73 65 74 2e 20 54 72 75 65 20 6f 74 68 65 72   set. True other
15462 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  wise..*/.static 
15463 69 6e 74 20 62 74 72 65 65 47 65 74 48 61 73 43  int btreeGetHasC
15464 6f 6e 74 65 6e 74 28 42 74 53 68 61 72 65 64 20  ontent(BtShared 
15465 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29  *pBt, Pgno pgno)
15466 7b 0a 20 20 42 69 74 76 65 63 20 2a 70 20 3d 20  {.  Bitvec *p = 
15467 70 42 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74  pBt->pHasContent
15468 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 20 26 26  ;.  return (p &&
15469 20 28 70 67 6e 6f 3e 73 71 6c 69 74 65 33 42 69   (pgno>sqlite3Bi
1546a 74 76 65 63 53 69 7a 65 28 70 29 20 7c 7c 20 73  tvecSize(p) || s
1546b 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74  qlite3BitvecTest
1546c 28 70 2c 20 70 67 6e 6f 29 29 29 3b 0a 7d 0a 0a  (p, pgno)));.}..
1546d 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 28 64 65 73  /*.** Clear (des
1546e 74 72 6f 79 29 20 74 68 65 20 42 74 53 68 61 72  troy) the BtShar
1546f 65 64 2e 70 48 61 73 43 6f 6e 74 65 6e 74 20 62  ed.pHasContent b
15470 69 74 76 65 63 2e 20 54 68 69 73 20 73 68 6f 75  itvec. This shou
15471 6c 64 20 62 65 0a 2a 2a 20 69 6e 76 6f 6b 65 64  ld be.** invoked
15472 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69   at the conclusi
15473 6f 6e 20 6f 66 20 65 61 63 68 20 77 72 69 74 65  on of each write
15474 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f  -transaction..*/
15475 0a 73 74 61 74 69 63 20 76 6f 69 64 20 62 74 72  .static void btr
15476 65 65 43 6c 65 61 72 48 61 73 43 6f 6e 74 65 6e  eeClearHasConten
15477 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29  t(BtShared *pBt)
15478 7b 0a 20 20 73 71 6c 69 74 65 33 42 69 74 76 65  {.  sqlite3Bitve
15479 63 44 65 73 74 72 6f 79 28 70 42 74 2d 3e 70 48  cDestroy(pBt->pH
1547a 61 73 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 70 42  asContent);.  pB
1547b 74 2d 3e 70 48 61 73 43 6f 6e 74 65 6e 74 20 3d  t->pHasContent =
1547c 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76   0;.}../*.** Sav
1547d 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 75  e the current cu
1547e 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69 6e  rsor position in
1547f 20 74 68 65 20 76 61 72 69 61 62 6c 65 73 20 42   the variables B
15480 74 43 75 72 73 6f 72 2e 6e 4b 65 79 20 0a 2a 2a  tCursor.nKey .**
15481 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 70 4b   and BtCursor.pK
15482 65 79 2e 20 54 68 65 20 63 75 72 73 6f 72 27 73  ey. The cursor's
15483 20 73 74 61 74 65 20 69 73 20 73 65 74 20 74 6f   state is set to
15484 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53   CURSOR_REQUIRES
15485 45 45 4b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  EEK..**.** The c
15486 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72  aller must ensur
15487 65 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f  e that the curso
15488 72 20 69 73 20 76 61 6c 69 64 20 28 68 61 73 20  r is valid (has 
15489 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56  eState==CURSOR_V
1548a 41 4c 49 44 29 0a 2a 2a 20 70 72 69 6f 72 20 74  ALID).** prior t
1548b 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72  o calling this r
1548c 6f 75 74 69 6e 65 2e 20 20 0a 2a 2f 0a 73 74 61  outine.  .*/.sta
1548d 74 69 63 20 69 6e 74 20 73 61 76 65 43 75 72 73  tic int saveCurs
1548e 6f 72 50 6f 73 69 74 69 6f 6e 28 42 74 43 75 72  orPosition(BtCur
1548f 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e  sor *pCur){.  in
15490 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
15491 20 43 55 52 53 4f 52 5f 56 41 4c 49 44 3d 3d 70   CURSOR_VALID==p
15492 43 75 72 2d 3e 65 53 74 61 74 65 20 29 3b 0a 20  Cur->eState );. 
15493 20 61 73 73 65 72 74 28 20 30 3d 3d 70 43 75 72   assert( 0==pCur
15494 2d 3e 70 4b 65 79 20 29 3b 0a 20 20 61 73 73 65  ->pKey );.  asse
15495 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
15496 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 0a 20  utex(pCur) );.. 
15497 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
15498 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72 2c 20  eeKeySize(pCur, 
15499 26 70 43 75 72 2d 3e 6e 4b 65 79 29 3b 0a 20 20  &pCur->nKey);.  
1549a 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
1549b 54 45 5f 4f 4b 20 29 3b 20 20 2f 2a 20 4b 65 79  TE_OK );  /* Key
1549c 53 69 7a 65 28 29 20 63 61 6e 6e 6f 74 20 66 61  Size() cannot fa
1549d 69 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74  il */..  /* If t
1549e 68 69 73 20 69 73 20 61 6e 20 69 6e 74 4b 65 79  his is an intKey
1549f 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 74 68 65   table, then the
154a0 20 61 62 6f 76 65 20 63 61 6c 6c 20 74 6f 20 42   above call to B
154a1 74 72 65 65 4b 65 79 53 69 7a 65 28 29 0a 20 20  treeKeySize().  
154a2 2a 2a 20 73 74 6f 72 65 73 20 74 68 65 20 69 6e  ** stores the in
154a3 74 65 67 65 72 20 6b 65 79 20 69 6e 20 70 43 75  teger key in pCu
154a4 72 2d 3e 6e 4b 65 79 2e 20 49 6e 20 74 68 69 73  r->nKey. In this
154a5 20 63 61 73 65 20 74 68 69 73 20 76 61 6c 75 65   case this value
154a6 20 69 73 0a 20 20 2a 2a 20 61 6c 6c 20 74 68 61   is.  ** all tha
154a7 74 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 4f  t is required. O
154a8 74 68 65 72 77 69 73 65 2c 20 69 66 20 70 43 75  therwise, if pCu
154a9 72 20 69 73 20 6e 6f 74 20 6f 70 65 6e 20 6f 6e  r is not open on
154aa 20 61 6e 20 69 6e 74 4b 65 79 0a 20 20 2a 2a 20   an intKey.  ** 
154ab 74 61 62 6c 65 2c 20 74 68 65 6e 20 6d 61 6c 6c  table, then mall
154ac 6f 63 20 73 70 61 63 65 20 66 6f 72 20 61 6e 64  oc space for and
154ad 20 73 74 6f 72 65 20 74 68 65 20 70 43 75 72 2d   store the pCur-
154ae 3e 6e 4b 65 79 20 62 79 74 65 73 20 6f 66 20 6b  >nKey bytes of k
154af 65 79 20 0a 20 20 2a 2a 20 64 61 74 61 2e 0a 20  ey .  ** data.. 
154b0 20 2a 2f 0a 20 20 69 66 28 20 30 3d 3d 70 43 75   */.  if( 0==pCu
154b1 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e  r->apPage[0]->in
154b2 74 4b 65 79 20 29 7b 0a 20 20 20 20 76 6f 69 64  tKey ){.    void
154b3 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33   *pKey = sqlite3
154b4 4d 61 6c 6c 6f 63 28 20 28 69 6e 74 29 70 43 75  Malloc( (int)pCu
154b5 72 2d 3e 6e 4b 65 79 20 29 3b 0a 20 20 20 20 69  r->nKey );.    i
154b6 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 20  f( pKey ){.     
154b7 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
154b8 65 65 4b 65 79 28 70 43 75 72 2c 20 30 2c 20 28  eeKey(pCur, 0, (
154b9 69 6e 74 29 70 43 75 72 2d 3e 6e 4b 65 79 2c 20  int)pCur->nKey, 
154ba 70 4b 65 79 29 3b 0a 20 20 20 20 20 20 69 66 28  pKey);.      if(
154bb 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
154bc 7b 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e  {.        pCur->
154bd 70 4b 65 79 20 3d 20 70 4b 65 79 3b 0a 20 20 20  pKey = pKey;.   
154be 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
154bf 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
154c0 4b 65 79 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Key);.      }.  
154c1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
154c2 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
154c3 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73  ;.    }.  }.  as
154c4 73 65 72 74 28 20 21 70 43 75 72 2d 3e 61 70 50  sert( !pCur->apP
154c5 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 7c  age[0]->intKey |
154c6 7c 20 21 70 43 75 72 2d 3e 70 4b 65 79 20 29 3b  | !pCur->pKey );
154c7 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ..  if( rc==SQLI
154c8 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74  TE_OK ){.    int
154c9 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   i;.    for(i=0;
154ca 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61 67 65 3b   i<=pCur->iPage;
154cb 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 72 65 6c   i++){.      rel
154cc 65 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61  easePage(pCur->a
154cd 70 50 61 67 65 5b 69 5d 29 3b 0a 20 20 20 20 20  pPage[i]);.     
154ce 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d   pCur->apPage[i]
154cf 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
154d0 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 2d 31  pCur->iPage = -1
154d1 3b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61  ;.    pCur->eSta
154d2 74 65 20 3d 20 43 55 52 53 4f 52 5f 52 45 51 55  te = CURSOR_REQU
154d3 49 52 45 53 45 45 4b 3b 0a 20 20 7d 0a 0a 20 20  IRESEEK;.  }..  
154d4 69 6e 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c  invalidateOverfl
154d5 6f 77 43 61 63 68 65 28 70 43 75 72 29 3b 0a 20  owCache(pCur);. 
154d6 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
154d7 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 70 6f  *.** Save the po
154d8 73 69 74 69 6f 6e 73 20 6f 66 20 61 6c 6c 20 63  sitions of all c
154d9 75 72 73 6f 72 73 20 28 65 78 63 65 70 74 20 70  ursors (except p
154da 45 78 63 65 70 74 29 20 74 68 61 74 20 61 72 65  Except) that are
154db 20 6f 70 65 6e 20 6f 6e 0a 2a 2a 20 74 68 65 20   open on.** the 
154dc 74 61 62 6c 65 20 20 77 69 74 68 20 72 6f 6f 74  table  with root
154dd 2d 70 61 67 65 20 69 52 6f 6f 74 2e 20 55 73 75  -page iRoot. Usu
154de 61 6c 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61  ally, this is ca
154df 6c 6c 65 64 20 6a 75 73 74 20 62 65 66 6f 72 65  lled just before
154e0 20 63 75 72 73 6f 72 0a 2a 2a 20 70 45 78 63 65   cursor.** pExce
154e1 70 74 20 69 73 20 75 73 65 64 20 74 6f 20 6d 6f  pt is used to mo
154e2 64 69 66 79 20 74 68 65 20 74 61 62 6c 65 20 28  dify the table (
154e3 42 74 72 65 65 44 65 6c 65 74 65 28 29 20 6f 72  BtreeDelete() or
154e4 20 42 74 72 65 65 49 6e 73 65 72 74 28 29 29 2e   BtreeInsert()).
154e5 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
154e6 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 42 74  aveAllCursors(Bt
154e7 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e  Shared *pBt, Pgn
154e8 6f 20 69 52 6f 6f 74 2c 20 42 74 43 75 72 73 6f  o iRoot, BtCurso
154e9 72 20 2a 70 45 78 63 65 70 74 29 7b 0a 20 20 42  r *pExcept){.  B
154ea 74 43 75 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73  tCursor *p;.  as
154eb 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
154ec 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
154ed 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
154ee 28 20 70 45 78 63 65 70 74 3d 3d 30 20 7c 7c 20  ( pExcept==0 || 
154ef 70 45 78 63 65 70 74 2d 3e 70 42 74 3d 3d 70 42  pExcept->pBt==pB
154f0 74 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74  t );.  for(p=pBt
154f1 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d  ->pCursor; p; p=
154f2 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69  p->pNext){.    i
154f3 66 28 20 70 21 3d 70 45 78 63 65 70 74 20 26 26  f( p!=pExcept &&
154f4 20 28 30 3d 3d 69 52 6f 6f 74 20 7c 7c 20 70 2d   (0==iRoot || p-
154f5 3e 70 67 6e 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74  >pgnoRoot==iRoot
154f6 29 20 26 26 20 0a 20 20 20 20 20 20 20 20 70 2d  ) && .        p-
154f7 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
154f8 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 20 69  VALID ){.      i
154f9 6e 74 20 72 63 20 3d 20 73 61 76 65 43 75 72 73  nt rc = saveCurs
154fa 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 3b 0a 20  orPosition(p);. 
154fb 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f       if( SQLITE_
154fc 4f 4b 21 3d 72 63 20 29 7b 0a 20 20 20 20 20 20  OK!=rc ){.      
154fd 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
154fe 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
154ff 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
15500 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61  K;.}../*.** Clea
15501 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 75  r the current cu
15502 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a  rsor position..*
15503 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
15504 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
15505 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 42 74  eeClearCursor(Bt
15506 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
15507 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
15508 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
15509 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
1550a 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20  e(pCur->pKey);. 
1550b 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b   pCur->pKey = 0;
1550c 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20  .  pCur->eState 
1550d 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  = CURSOR_INVALID
1550e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68  ;.}../*.** In th
1550f 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 42 74  is version of Bt
15510 72 65 65 4d 6f 76 65 74 6f 2c 20 70 4b 65 79 20  reeMoveto, pKey 
15511 69 73 20 61 20 70 61 63 6b 65 64 20 69 6e 64 65  is a packed inde
15512 78 20 72 65 63 6f 72 64 0a 2a 2a 20 73 75 63 68  x record.** such
15513 20 61 73 20 69 73 20 67 65 6e 65 72 61 74 65 64   as is generated
15514 20 62 79 20 74 68 65 20 4f 50 5f 4d 61 6b 65 52   by the OP_MakeR
15515 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 20 20 55  ecord opcode.  U
15516 6e 70 61 63 6b 20 74 68 65 0a 2a 2a 20 72 65 63  npack the.** rec
15517 6f 72 64 20 61 6e 64 20 74 68 65 6e 20 63 61 6c  ord and then cal
15518 6c 20 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70  l BtreeMovetoUnp
15519 61 63 6b 65 64 28 29 20 74 6f 20 64 6f 20 74 68  acked() to do th
1551a 65 20 77 6f 72 6b 2e 0a 2a 2f 0a 73 74 61 74 69  e work..*/.stati
1551b 63 20 69 6e 74 20 62 74 72 65 65 4d 6f 76 65 74  c int btreeMovet
1551c 6f 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  o(.  BtCursor *p
1551d 43 75 72 2c 20 20 20 20 20 2f 2a 20 43 75 72 73  Cur,     /* Curs
1551e 6f 72 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 62  or open on the b
1551f 74 72 65 65 20 74 6f 20 62 65 20 73 65 61 72 63  tree to be searc
15520 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  hed */.  const v
15521 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20  oid *pKey,   /* 
15522 50 61 63 6b 65 64 20 6b 65 79 20 69 66 20 74 68  Packed key if th
15523 65 20 62 74 72 65 65 20 69 73 20 61 6e 20 69 6e  e btree is an in
15524 64 65 78 20 2a 2f 0a 20 20 69 36 34 20 6e 4b 65  dex */.  i64 nKe
15525 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  y,           /* 
15526 49 6e 74 65 67 65 72 20 6b 65 79 20 66 6f 72 20  Integer key for 
15527 74 61 62 6c 65 73 2e 20 20 53 69 7a 65 20 6f 66  tables.  Size of
15528 20 70 4b 65 79 20 66 6f 72 20 69 6e 64 69 63 65   pKey for indice
15529 73 20 2a 2f 0a 20 20 69 6e 74 20 62 69 61 73 2c  s */.  int bias,
1552a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69             /* Bi
1552b 61 73 20 73 65 61 72 63 68 20 74 6f 20 74 68 65  as search to the
1552c 20 68 69 67 68 20 65 6e 64 20 2a 2f 0a 20 20 69   high end */.  i
1552d 6e 74 20 2a 70 52 65 73 20 20 20 20 20 20 20 20  nt *pRes        
1552e 20 20 20 2f 2a 20 57 72 69 74 65 20 73 65 61 72     /* Write sear
1552f 63 68 20 72 65 73 75 6c 74 73 20 68 65 72 65 20  ch results here 
15530 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20  */.){.  int rc; 
15531 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15532 20 20 20 2f 2a 20 53 74 61 74 75 73 20 63 6f 64     /* Status cod
15533 65 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52  e */.  UnpackedR
15534 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 20  ecord *pIdxKey; 
15535 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 69 6e    /* Unpacked in
15536 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 63 68 61  dex key */.  cha
15537 72 20 61 53 70 61 63 65 5b 31 35 30 5d 3b 20 20  r aSpace[150];  
15538 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20          /* Temp 
15539 73 70 61 63 65 20 66 6f 72 20 70 49 64 78 4b 65  space for pIdxKe
1553a 79 20 2d 20 74 6f 20 61 76 6f 69 64 20 61 20 6d  y - to avoid a m
1553b 61 6c 6c 6f 63 20 2a 2f 0a 0a 20 20 69 66 28 20  alloc */..  if( 
1553c 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 73 73 65  pKey ){.    asse
1553d 72 74 28 20 6e 4b 65 79 3d 3d 28 69 36 34 29 28  rt( nKey==(i64)(
1553e 69 6e 74 29 6e 4b 65 79 20 29 3b 0a 20 20 20 20  int)nKey );.    
1553f 70 49 64 78 4b 65 79 20 3d 20 73 71 6c 69 74 65  pIdxKey = sqlite
15540 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63  3VdbeRecordUnpac
15541 6b 28 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f  k(pCur->pKeyInfo
15542 2c 20 28 69 6e 74 29 6e 4b 65 79 2c 20 70 4b 65  , (int)nKey, pKe
15543 79 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  y,.             
15544 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15545 20 20 20 20 20 20 20 20 20 61 53 70 61 63 65 2c           aSpace,
15546 20 73 69 7a 65 6f 66 28 61 53 70 61 63 65 29 29   sizeof(aSpace))
15547 3b 0a 20 20 20 20 69 66 28 20 70 49 64 78 4b 65  ;.    if( pIdxKe
15548 79 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51  y==0 ) return SQ
15549 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65  LITE_NOMEM;.  }e
1554a 6c 73 65 7b 0a 20 20 20 20 70 49 64 78 4b 65 79  lse{.    pIdxKey
1554b 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 63 20 3d   = 0;.  }.  rc =
1554c 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
1554d 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 75 72  etoUnpacked(pCur
1554e 2c 20 70 49 64 78 4b 65 79 2c 20 6e 4b 65 79 2c  , pIdxKey, nKey,
1554f 20 62 69 61 73 2c 20 70 52 65 73 29 3b 0a 20 20   bias, pRes);.  
15550 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20  if( pKey ){.    
15551 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74  sqlite3VdbeDelet
15552 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28  eUnpackedRecord(
15553 70 49 64 78 4b 65 79 29 3b 0a 20 20 7d 0a 20 20  pIdxKey);.  }.  
15554 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
15555 0a 2a 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20  .** Restore the 
15556 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 70 6f  cursor to the po
15557 73 69 74 69 6f 6e 20 69 74 20 77 61 73 20 69 6e  sition it was in
15558 20 28 6f 72 20 61 73 20 63 6c 6f 73 65 20 74 6f   (or as close to
15559 20 61 73 20 70 6f 73 73 69 62 6c 65 29 0a 2a 2a   as possible).**
1555a 20 77 68 65 6e 20 73 61 76 65 43 75 72 73 6f 72   when saveCursor
1555b 50 6f 73 69 74 69 6f 6e 28 29 20 77 61 73 20 63  Position() was c
1555c 61 6c 6c 65 64 2e 20 4e 6f 74 65 20 74 68 61 74  alled. Note that
1555d 20 74 68 69 73 20 63 61 6c 6c 20 64 65 6c 65 74   this call delet
1555e 65 73 20 74 68 65 20 0a 2a 2a 20 73 61 76 65 64  es the .** saved
1555f 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66 6f 20 73   position info s
15560 74 6f 72 65 64 20 62 79 20 73 61 76 65 43 75 72  tored by saveCur
15561 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2c 20 73  sorPosition(), s
15562 6f 20 74 68 65 72 65 20 63 61 6e 20 62 65 0a 2a  o there can be.*
15563 2a 20 61 74 20 6d 6f 73 74 20 6f 6e 65 20 65 66  * at most one ef
15564 66 65 63 74 69 76 65 20 72 65 73 74 6f 72 65 43  fective restoreC
15565 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20  ursorPosition() 
15566 63 61 6c 6c 20 61 66 74 65 72 20 65 61 63 68 20  call after each 
15567 0a 2a 2a 20 73 61 76 65 43 75 72 73 6f 72 50 6f  .** saveCursorPo
15568 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f 0a 73 74 61  sition()..*/.sta
15569 74 69 63 20 69 6e 74 20 62 74 72 65 65 52 65 73  tic int btreeRes
1556a 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69  toreCursorPositi
1556b 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  on(BtCursor *pCu
1556c 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  r){.  int rc;.  
1556d 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
1556e 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29  ldsMutex(pCur) )
1556f 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
15570 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 4f 52  ->eState>=CURSOR
15571 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a  _REQUIRESEEK );.
15572 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61    if( pCur->eSta
15573 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54  te==CURSOR_FAULT
15574 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70   ){.    return p
15575 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 3b 0a 20  Cur->skipNext;. 
15576 20 7d 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74   }.  pCur->eStat
15577 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c  e = CURSOR_INVAL
15578 49 44 3b 0a 20 20 72 63 20 3d 20 62 74 72 65 65  ID;.  rc = btree
15579 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20 70 43 75  Moveto(pCur, pCu
1557a 72 2d 3e 70 4b 65 79 2c 20 70 43 75 72 2d 3e 6e  r->pKey, pCur->n
1557b 4b 65 79 2c 20 30 2c 20 26 70 43 75 72 2d 3e 73  Key, 0, &pCur->s
1557c 6b 69 70 4e 65 78 74 29 3b 0a 20 20 69 66 28 20  kipNext);.  if( 
1557d 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1557e 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
1557f 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20  e(pCur->pKey);. 
15580 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20     pCur->pKey = 
15581 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  0;.    assert( p
15582 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
15583 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70 43 75  SOR_VALID || pCu
15584 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
15585 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20 7d  R_INVALID );.  }
15586 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
15587 0a 23 64 65 66 69 6e 65 20 72 65 73 74 6f 72 65  .#define restore
15588 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70  CursorPosition(p
15589 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 61 74 65  ) \.  (p->eState
1558a 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  >=CURSOR_REQUIRE
1558b 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 20 20 20  SEEK ? \.       
1558c 20 20 62 74 72 65 65 52 65 73 74 6f 72 65 43 75    btreeRestoreCu
1558d 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 29 20  rsorPosition(p) 
1558e 3a 20 5c 0a 20 20 20 20 20 20 20 20 20 53 51 4c  : \.         SQL
1558f 49 54 45 5f 4f 4b 29 0a 0a 2f 2a 0a 2a 2a 20 44  ITE_OK)../*.** D
15590 65 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72  etermine whether
15591 20 6f 72 20 6e 6f 74 20 61 20 63 75 72 73 6f 72   or not a cursor
15592 20 68 61 73 20 6d 6f 76 65 64 20 66 72 6f 6d 20   has moved from 
15593 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 69 74 0a  the position it.
15594 2a 2a 20 77 61 73 20 6c 61 73 74 20 70 6c 61 63  ** was last plac
15595 65 64 20 61 74 2e 20 20 43 75 72 73 6f 72 73 20  ed at.  Cursors 
15596 63 61 6e 20 6d 6f 76 65 20 77 68 65 6e 20 74 68  can move when th
15597 65 20 72 6f 77 20 74 68 65 79 20 61 72 65 20 70  e row they are p
15598 6f 69 6e 74 69 6e 67 0a 2a 2a 20 61 74 20 69 73  ointing.** at is
15599 20 64 65 6c 65 74 65 64 20 6f 75 74 20 66 72 6f   deleted out fro
1559a 6d 20 75 6e 64 65 72 20 74 68 65 6d 2e 0a 2a 2a  m under them..**
1559b 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1559c 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f   returns an erro
1559d 72 20 63 6f 64 65 20 69 66 20 73 6f 6d 65 74 68  r code if someth
1559e 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 20  ing goes wrong. 
1559f 20 54 68 65 0a 2a 2a 20 69 6e 74 65 67 65 72 20   The.** integer 
155a0 2a 70 48 61 73 4d 6f 76 65 64 20 69 73 20 73 65  *pHasMoved is se
155a1 74 20 74 6f 20 6f 6e 65 20 69 66 20 74 68 65 20  t to one if the 
155a2 63 75 72 73 6f 72 20 68 61 73 20 6d 6f 76 65 64  cursor has moved
155a3 20 61 6e 64 20 30 20 69 66 20 6e 6f 74 2e 0a 2a   and 0 if not..*
155a4 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
155a5 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
155a6 65 43 75 72 73 6f 72 48 61 73 4d 6f 76 65 64 28  eCursorHasMoved(
155a7 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
155a8 69 6e 74 20 2a 70 48 61 73 4d 6f 76 65 64 29 7b  int *pHasMoved){
155a9 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63  .  int rc;..  rc
155aa 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72   = restoreCursor
155ab 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a  Position(pCur);.
155ac 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
155ad 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31 3b 0a  *pHasMoved = 1;.
155ae 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
155af 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65   }.  if( pCur->e
155b0 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41  State!=CURSOR_VA
155b1 4c 49 44 20 7c 7c 20 70 43 75 72 2d 3e 73 6b 69  LID || pCur->ski
155b2 70 4e 65 78 74 21 3d 30 20 29 7b 0a 20 20 20 20  pNext!=0 ){.    
155b3 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 31 3b 0a  *pHasMoved = 1;.
155b4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70 48    }else{.    *pH
155b5 61 73 4d 6f 76 65 64 20 3d 20 30 3b 0a 20 20 7d  asMoved = 0;.  }
155b6 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
155b7 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  _OK;.}..#ifndef 
155b8 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
155b9 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 47 69 76  VACUUM./*.** Giv
155ba 65 6e 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72  en a page number
155bb 20 6f 66 20 61 20 72 65 67 75 6c 61 72 20 64 61   of a regular da
155bc 74 61 62 61 73 65 20 70 61 67 65 2c 20 72 65 74  tabase page, ret
155bd 75 72 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20  urn the page.** 
155be 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 70  number for the p
155bf 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 20  ointer-map page 
155c0 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
155c1 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 0a  e entry for the.
155c2 2a 2a 20 69 6e 70 75 74 20 70 61 67 65 20 6e 75  ** input page nu
155c3 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  mber..*/.static 
155c4 50 67 6e 6f 20 70 74 72 6d 61 70 50 61 67 65 6e  Pgno ptrmapPagen
155c5 6f 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c  o(BtShared *pBt,
155c6 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 69   Pgno pgno){.  i
155c7 6e 74 20 6e 50 61 67 65 73 50 65 72 4d 61 70 50  nt nPagesPerMapP
155c8 61 67 65 3b 0a 20 20 50 67 6e 6f 20 69 50 74 72  age;.  Pgno iPtr
155c9 4d 61 70 2c 20 72 65 74 3b 0a 20 20 61 73 73 65  Map, ret;.  asse
155ca 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
155cb 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65  x_held(pBt->mute
155cc 78 29 20 29 3b 0a 20 20 6e 50 61 67 65 73 50 65  x) );.  nPagesPe
155cd 72 4d 61 70 50 61 67 65 20 3d 20 28 70 42 74 2d  rMapPage = (pBt-
155ce 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 29 2b 31  >usableSize/5)+1
155cf 3b 0a 20 20 69 50 74 72 4d 61 70 20 3d 20 28 70  ;.  iPtrMap = (p
155d0 67 6e 6f 2d 32 29 2f 6e 50 61 67 65 73 50 65 72  gno-2)/nPagesPer
155d1 4d 61 70 50 61 67 65 3b 0a 20 20 72 65 74 20 3d  MapPage;.  ret =
155d2 20 28 69 50 74 72 4d 61 70 2a 6e 50 61 67 65 73   (iPtrMap*nPages
155d3 50 65 72 4d 61 70 50 61 67 65 29 20 2b 20 32 3b  PerMapPage) + 2;
155d4 20 0a 20 20 69 66 28 20 72 65 74 3d 3d 50 45 4e   .  if( ret==PEN
155d5 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
155d6 42 74 29 20 29 7b 0a 20 20 20 20 72 65 74 2b 2b  Bt) ){.    ret++
155d7 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
155d8 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69  et;.}../*.** Wri
155d9 74 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f  te an entry into
155da 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   the pointer map
155db 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
155dc 74 69 6e 65 20 75 70 64 61 74 65 73 20 74 68 65  tine updates the
155dd 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74   pointer map ent
155de 72 79 20 66 6f 72 20 70 61 67 65 20 6e 75 6d 62  ry for page numb
155df 65 72 20 27 6b 65 79 27 0a 2a 2a 20 73 6f 20 74  er 'key'.** so t
155e0 68 61 74 20 69 74 20 6d 61 70 73 20 74 6f 20 74  hat it maps to t
155e1 79 70 65 20 27 65 54 79 70 65 27 20 61 6e 64 20  ype 'eType' and 
155e2 70 61 72 65 6e 74 20 70 61 67 65 20 6e 75 6d 62  parent page numb
155e3 65 72 20 27 70 67 6e 6f 27 2e 0a 2a 2a 0a 2a 2a  er 'pgno'..**.**
155e4 20 49 66 20 2a 70 52 43 20 69 73 20 69 6e 69 74   If *pRC is init
155e5 69 61 6c 6c 79 20 6e 6f 6e 2d 7a 65 72 6f 20 28  ially non-zero (
155e6 6e 6f 6e 2d 53 51 4c 49 54 45 5f 4f 4b 29 20 74  non-SQLITE_OK) t
155e7 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
155e8 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d 6f 70 2e 20   is.** a no-op. 
155e9 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
155ea 75 72 73 2c 20 74 68 65 20 61 70 70 72 6f 70 72  urs, the appropr
155eb 69 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20  iate error code 
155ec 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e  is written.** in
155ed 74 6f 20 2a 70 52 43 2e 0a 2a 2f 0a 73 74 61 74  to *pRC..*/.stat
155ee 69 63 20 76 6f 69 64 20 70 74 72 6d 61 70 50 75  ic void ptrmapPu
155ef 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c  t(BtShared *pBt,
155f0 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 65 54   Pgno key, u8 eT
155f1 79 70 65 2c 20 50 67 6e 6f 20 70 61 72 65 6e 74  ype, Pgno parent
155f2 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 44  , int *pRC){.  D
155f3 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 20  bPage *pDbPage; 
155f4 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20   /* The pointer 
155f5 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 75 38  map page */.  u8
155f6 20 2a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20   *pPtrmap;      
155f7 2f 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d  /* The pointer m
155f8 61 70 20 64 61 74 61 20 2a 2f 0a 20 20 50 67 6e  ap data */.  Pgn
155f9 6f 20 69 50 74 72 6d 61 70 3b 20 20 20 20 20 2f  o iPtrmap;     /
155fa 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  * The pointer ma
155fb 70 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f  p page number */
155fc 0a 20 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 20  .  int offset;  
155fd 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69       /* Offset i
155fe 6e 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61  n pointer map pa
155ff 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20  ge */.  int rc; 
15600 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
15601 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75  urn code from su
15602 62 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 0a 20  bfunctions */.. 
15603 20 69 66 28 20 2a 70 52 43 20 29 20 72 65 74 75   if( *pRC ) retu
15604 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  rn;..  assert( s
15605 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
15606 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
15607 0a 20 20 2f 2a 20 54 68 65 20 6d 61 73 74 65 72  .  /* The master
15608 2d 6a 6f 75 72 6e 61 6c 20 70 61 67 65 20 6e 75  -journal page nu
15609 6d 62 65 72 20 6d 75 73 74 20 6e 65 76 65 72 20  mber must never 
1560a 62 65 20 75 73 65 64 20 61 73 20 61 20 70 6f 69  be used as a poi
1560b 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f  nter map page */
1560c 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d 50 54  .  assert( 0==PT
1560d 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c  RMAP_ISPAGE(pBt,
1560e 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41   PENDING_BYTE_PA
1560f 47 45 28 70 42 74 29 29 20 29 3b 0a 0a 20 20 61  GE(pBt)) );..  a
15610 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f  ssert( pBt->auto
15611 56 61 63 75 75 6d 20 29 3b 0a 20 20 69 66 28 20  Vacuum );.  if( 
15612 6b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70  key==0 ){.    *p
15613 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52  RC = SQLITE_CORR
15614 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 72 65  UPT_BKPT;.    re
15615 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 50 74 72  turn;.  }.  iPtr
15616 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f 50 41 47  map = PTRMAP_PAG
15617 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29 3b 0a 20  ENO(pBt, key);. 
15618 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
15619 65 72 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65  erGet(pBt->pPage
1561a 72 2c 20 69 50 74 72 6d 61 70 2c 20 26 70 44 62  r, iPtrmap, &pDb
1561b 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21  Page);.  if( rc!
1561c 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1561d 20 20 2a 70 52 43 20 3d 20 72 63 3b 0a 20 20 20    *pRC = rc;.   
1561e 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6f   return;.  }.  o
1561f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 5f 50  ffset = PTRMAP_P
15620 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d 61 70  TROFFSET(iPtrmap
15621 2c 20 6b 65 79 29 3b 0a 20 20 69 66 28 20 6f 66  , key);.  if( of
15622 66 73 65 74 3c 30 20 29 7b 0a 20 20 20 20 2a 70  fset<0 ){.    *p
15623 52 43 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52  RC = SQLITE_CORR
15624 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 67 6f  UPT_BKPT;.    go
15625 74 6f 20 70 74 72 6d 61 70 5f 65 78 69 74 3b 0a  to ptrmap_exit;.
15626 20 20 7d 0a 20 20 70 50 74 72 6d 61 70 20 3d 20    }.  pPtrmap = 
15627 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61 67  (u8 *)sqlite3Pag
15628 65 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67  erGetData(pDbPag
15629 65 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 65  e);..  if( eType
1562a 21 3d 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74  !=pPtrmap[offset
1562b 5d 20 7c 7c 20 67 65 74 34 62 79 74 65 28 26 70  ] || get4byte(&p
1562c 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d  Ptrmap[offset+1]
1562d 29 21 3d 70 61 72 65 6e 74 20 29 7b 0a 20 20 20  )!=parent ){.   
1562e 20 54 52 41 43 45 28 28 22 50 54 52 4d 41 50 5f   TRACE(("PTRMAP_
1562f 55 50 44 41 54 45 3a 20 25 64 2d 3e 28 25 64 2c  UPDATE: %d->(%d,
15630 25 64 29 5c 6e 22 2c 20 6b 65 79 2c 20 65 54 79  %d)\n", key, eTy
15631 70 65 2c 20 70 61 72 65 6e 74 29 29 3b 0a 20 20  pe, parent));.  
15632 20 20 2a 70 52 43 3d 20 72 63 20 3d 20 73 71 6c    *pRC= rc = sql
15633 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
15634 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28  DbPage);.    if(
15635 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
15636 7b 0a 20 20 20 20 20 20 70 50 74 72 6d 61 70 5b  {.      pPtrmap[
15637 6f 66 66 73 65 74 5d 20 3d 20 65 54 79 70 65 3b  offset] = eType;
15638 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28  .      put4byte(
15639 26 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b  &pPtrmap[offset+
1563a 31 5d 2c 20 70 61 72 65 6e 74 29 3b 0a 20 20 20  1], parent);.   
1563b 20 7d 0a 20 20 7d 0a 0a 70 74 72 6d 61 70 5f 65   }.  }..ptrmap_e
1563c 78 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 50 61  xit:.  sqlite3Pa
1563d 67 65 72 55 6e 72 65 66 28 70 44 62 50 61 67 65  gerUnref(pDbPage
1563e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  );.}../*.** Read
1563f 20 61 6e 20 65 6e 74 72 79 20 66 72 6f 6d 20 74   an entry from t
15640 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a  he pointer map..
15641 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
15642 6e 65 20 72 65 74 72 69 65 76 65 73 20 74 68 65  ne retrieves the
15643 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74   pointer map ent
15644 72 79 20 66 6f 72 20 70 61 67 65 20 27 6b 65 79  ry for page 'key
15645 27 2c 20 77 72 69 74 69 6e 67 0a 2a 2a 20 74 68  ', writing.** th
15646 65 20 74 79 70 65 20 61 6e 64 20 70 61 72 65 6e  e type and paren
15647 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 74 6f  t page number to
15648 20 2a 70 45 54 79 70 65 20 61 6e 64 20 2a 70 50   *pEType and *pP
15649 67 6e 6f 20 72 65 73 70 65 63 74 69 76 65 6c 79  gno respectively
1564a 2e 0a 2a 2a 20 41 6e 20 65 72 72 6f 72 20 63 6f  ..** An error co
1564b 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  de is returned i
1564c 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73  f something goes
1564d 20 77 72 6f 6e 67 2c 20 6f 74 68 65 72 77 69 73   wrong, otherwis
1564e 65 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a  e SQLITE_OK..*/.
1564f 73 74 61 74 69 63 20 69 6e 74 20 70 74 72 6d 61  static int ptrma
15650 70 47 65 74 28 42 74 53 68 61 72 65 64 20 2a 70  pGet(BtShared *p
15651 42 74 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38  Bt, Pgno key, u8
15652 20 2a 70 45 54 79 70 65 2c 20 50 67 6e 6f 20 2a   *pEType, Pgno *
15653 70 50 67 6e 6f 29 7b 0a 20 20 44 62 50 61 67 65  pPgno){.  DbPage
15654 20 2a 70 44 62 50 61 67 65 3b 20 20 20 2f 2a 20   *pDbPage;   /* 
15655 54 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 20  The pointer map 
15656 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69 50  page */.  int iP
15657 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f 2a 20  trmap;       /* 
15658 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65  Pointer map page
15659 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a   index */.  u8 *
1565a 70 50 74 72 6d 61 70 3b 20 20 20 20 20 20 20 2f  pPtrmap;       /
1565b 2a 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 70 61  * Pointer map pa
1565c 67 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74  ge data */.  int
1565d 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20 20 20   offset;        
1565e 2f 2a 20 4f 66 66 73 65 74 20 6f 66 20 65 6e 74  /* Offset of ent
1565f 72 79 20 69 6e 20 70 6f 69 6e 74 65 72 20 6d 61  ry in pointer ma
15660 70 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  p */.  int rc;..
15661 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
15662 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
15663 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 69  ->mutex) );..  i
15664 50 74 72 6d 61 70 20 3d 20 50 54 52 4d 41 50 5f  Ptrmap = PTRMAP_
15665 50 41 47 45 4e 4f 28 70 42 74 2c 20 6b 65 79 29  PAGENO(pBt, key)
15666 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
15667 50 61 67 65 72 47 65 74 28 70 42 74 2d 3e 70 50  PagerGet(pBt->pP
15668 61 67 65 72 2c 20 69 50 74 72 6d 61 70 2c 20 26  ager, iPtrmap, &
15669 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20  pDbPage);.  if( 
1566a 72 63 21 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  rc!=0 ){.    ret
1566b 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50  urn rc;.  }.  pP
1566c 74 72 6d 61 70 20 3d 20 28 75 38 20 2a 29 73 71  trmap = (u8 *)sq
1566d 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74  lite3PagerGetDat
1566e 61 28 70 44 62 50 61 67 65 29 3b 0a 0a 20 20 6f  a(pDbPage);..  o
1566f 66 66 73 65 74 20 3d 20 50 54 52 4d 41 50 5f 50  ffset = PTRMAP_P
15670 54 52 4f 46 46 53 45 54 28 69 50 74 72 6d 61 70  TROFFSET(iPtrmap
15671 2c 20 6b 65 79 29 3b 0a 20 20 61 73 73 65 72 74  , key);.  assert
15672 28 20 70 45 54 79 70 65 21 3d 30 20 29 3b 0a 20  ( pEType!=0 );. 
15673 20 2a 70 45 54 79 70 65 20 3d 20 70 50 74 72 6d   *pEType = pPtrm
15674 61 70 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 69 66  ap[offset];.  if
15675 28 20 70 50 67 6e 6f 20 29 20 2a 70 50 67 6e 6f  ( pPgno ) *pPgno
15676 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 74   = get4byte(&pPt
15677 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29 3b  rmap[offset+1]);
15678 0a 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72  ..  sqlite3Pager
15679 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a  Unref(pDbPage);.
1567a 20 20 69 66 28 20 2a 70 45 54 79 70 65 3c 31 20    if( *pEType<1 
1567b 7c 7c 20 2a 70 45 54 79 70 65 3e 35 20 29 20 72  || *pEType>5 ) r
1567c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
1567d 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 72 65 74  RUPT_BKPT;.  ret
1567e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1567f 0a 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20 64 65  ..#else /* if de
15680 66 69 6e 65 64 20 53 51 4c 49 54 45 5f 4f 4d 49  fined SQLITE_OMI
15681 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a  T_AUTOVACUUM */.
15682 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70    #define ptrmap
15683 50 75 74 28 77 2c 78 2c 79 2c 7a 2c 72 63 29 0a  Put(w,x,y,z,rc).
15684 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70    #define ptrmap
15685 47 65 74 28 77 2c 78 2c 79 2c 7a 29 20 53 51 4c  Get(w,x,y,z) SQL
15686 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65  ITE_OK.  #define
15687 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74   ptrmapPutOvflPt
15688 72 28 78 2c 20 79 2c 20 72 63 29 0a 23 65 6e 64  r(x, y, rc).#end
15689 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20  if../*.** Given 
1568a 61 20 62 74 72 65 65 20 70 61 67 65 20 61 6e 64  a btree page and
1568b 20 61 20 63 65 6c 6c 20 69 6e 64 65 78 20 28 30   a cell index (0
1568c 20 6d 65 61 6e 73 20 74 68 65 20 66 69 72 73 74   means the first
1568d 20 63 65 6c 6c 20 6f 6e 0a 2a 2a 20 74 68 65 20   cell on.** the 
1568e 70 61 67 65 2c 20 31 20 6d 65 61 6e 73 20 74 68  page, 1 means th
1568f 65 20 73 65 63 6f 6e 64 20 63 65 6c 6c 2c 20 61  e second cell, a
15690 6e 64 20 73 6f 20 66 6f 72 74 68 29 20 72 65 74  nd so forth) ret
15691 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a  urn a pointer.**
15692 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e   to the cell con
15693 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tent..**.** This
15694 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73 20 6f   routine works o
15695 6e 6c 79 20 66 6f 72 20 70 61 67 65 73 20 74 68  nly for pages th
15696 61 74 20 64 6f 20 6e 6f 74 20 63 6f 6e 74 61 69  at do not contai
15697 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73  n overflow cells
15698 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 69 6e  ..*/.#define fin
15699 64 43 65 6c 6c 28 50 2c 49 29 20 5c 0a 20 20 28  dCell(P,I) \.  (
1569a 28 50 29 2d 3e 61 44 61 74 61 20 2b 20 28 28 50  (P)->aData + ((P
1569b 29 2d 3e 6d 61 73 6b 50 61 67 65 20 26 20 67 65  )->maskPage & ge
1569c 74 32 62 79 74 65 28 26 28 50 29 2d 3e 61 44 61  t2byte(&(P)->aDa
1569d 74 61 5b 28 50 29 2d 3e 63 65 6c 6c 4f 66 66 73  ta[(P)->cellOffs
1569e 65 74 2b 32 2a 28 49 29 5d 29 29 29 0a 0a 2f 2a  et+2*(I)])))../*
1569f 0a 2a 2a 20 54 68 69 73 20 61 20 6d 6f 72 65 20  .** This a more 
156a0 63 6f 6d 70 6c 65 78 20 76 65 72 73 69 6f 6e 20  complex version 
156a1 6f 66 20 66 69 6e 64 43 65 6c 6c 28 29 20 74 68  of findCell() th
156a2 61 74 20 77 6f 72 6b 73 20 66 6f 72 0a 2a 2a 20  at works for.** 
156a3 70 61 67 65 73 20 74 68 61 74 20 64 6f 20 63 6f  pages that do co
156a4 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63  ntain overflow c
156a5 65 6c 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ells..*/.static 
156a6 75 38 20 2a 66 69 6e 64 4f 76 65 72 66 6c 6f 77  u8 *findOverflow
156a7 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50  Cell(MemPage *pP
156a8 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b  age, int iCell){
156a9 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65  .  int i;.  asse
156aa 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
156ab 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42  x_held(pPage->pB
156ac 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66  t->mutex) );.  f
156ad 6f 72 28 69 3d 70 50 61 67 65 2d 3e 6e 4f 76 65  or(i=pPage->nOve
156ae 72 66 6c 6f 77 2d 31 3b 20 69 3e 3d 30 3b 20 69  rflow-1; i>=0; i
156af 2d 2d 29 7b 0a 20 20 20 20 69 6e 74 20 6b 3b 0a  --){.    int k;.
156b0 20 20 20 20 73 74 72 75 63 74 20 5f 4f 76 66 6c      struct _Ovfl
156b1 43 65 6c 6c 20 2a 70 4f 76 66 6c 3b 0a 20 20 20  Cell *pOvfl;.   
156b2 20 70 4f 76 66 6c 20 3d 20 26 70 50 61 67 65 2d   pOvfl = &pPage-
156b3 3e 61 4f 76 66 6c 5b 69 5d 3b 0a 20 20 20 20 6b  >aOvfl[i];.    k
156b4 20 3d 20 70 4f 76 66 6c 2d 3e 69 64 78 3b 0a 20   = pOvfl->idx;. 
156b5 20 20 20 69 66 28 20 6b 3c 3d 69 43 65 6c 6c 20     if( k<=iCell 
156b6 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d  ){.      if( k==
156b7 69 43 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20  iCell ){.       
156b8 20 72 65 74 75 72 6e 20 70 4f 76 66 6c 2d 3e 70   return pOvfl->p
156b9 43 65 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Cell;.      }.  
156ba 20 20 20 20 69 43 65 6c 6c 2d 2d 3b 0a 20 20 20      iCell--;.   
156bb 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
156bc 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20  findCell(pPage, 
156bd 69 43 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  iCell);.}../*.**
156be 20 50 61 72 73 65 20 61 20 63 65 6c 6c 20 63 6f   Parse a cell co
156bf 6e 74 65 6e 74 20 62 6c 6f 63 6b 20 61 6e 64 20  ntent block and 
156c0 66 69 6c 6c 20 69 6e 20 74 68 65 20 43 65 6c 6c  fill in the Cell
156c1 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20  Info structure. 
156c2 20 54 68 65 72 65 0a 2a 2a 20 61 72 65 20 74 77   There.** are tw
156c3 6f 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68  o versions of th
156c4 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 62 74  is function.  bt
156c5 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29 20 74  reeParseCell() t
156c6 61 6b 65 73 20 61 20 0a 2a 2a 20 63 65 6c 6c 20  akes a .** cell 
156c7 69 6e 64 65 78 20 61 73 20 74 68 65 20 73 65 63  index as the sec
156c8 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 61 6e 64  ond argument and
156c9 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50   btreeParseCellP
156ca 74 72 28 29 20 0a 2a 2a 20 74 61 6b 65 73 20 61  tr() .** takes a
156cb 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
156cc 62 6f 64 79 20 6f 66 20 74 68 65 20 63 65 6c 6c  body of the cell
156cd 20 61 73 20 69 74 73 20 73 65 63 6f 6e 64 20 61   as its second a
156ce 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57  rgument..**.** W
156cf 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c 65 2c  ithin this file,
156d0 20 74 68 65 20 70 61 72 73 65 43 65 6c 6c 28 29   the parseCell()
156d1 20 6d 61 63 72 6f 20 63 61 6e 20 62 65 20 63 61   macro can be ca
156d2 6c 6c 65 64 20 69 6e 73 74 65 61 64 20 6f 66 0a  lled instead of.
156d3 2a 2a 20 62 74 72 65 65 50 61 72 73 65 43 65 6c  ** btreeParseCel
156d4 6c 50 74 72 28 29 2e 20 55 73 69 6e 67 20 73 6f  lPtr(). Using so
156d5 6d 65 20 63 6f 6d 70 69 6c 65 72 73 2c 20 74 68  me compilers, th
156d6 69 73 20 77 69 6c 6c 20 62 65 20 66 61 73 74 65  is will be faste
156d7 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  r..*/.static voi
156d8 64 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c  d btreeParseCell
156d9 50 74 72 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a  Ptr(.  MemPage *
156da 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 2f  pPage,         /
156db 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e  * Page containin
156dc 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20  g the cell */.  
156dd 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20 20  u8 *pCell,      
156de 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
156df 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 74  er to the cell t
156e0 65 78 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e  ext. */.  CellIn
156e1 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20 20 20  fo *pInfo       
156e2 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 69    /* Fill in thi
156e3 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29  s structure */.)
156e4 7b 0a 20 20 75 31 36 20 6e 3b 20 20 20 20 20 20  {.  u16 n;      
156e5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
156e6 75 6d 62 65 72 20 62 79 74 65 73 20 69 6e 20 63  umber bytes in c
156e7 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65 61 64  ell content head
156e8 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 50 61 79  er */.  u32 nPay
156e9 6c 6f 61 64 3b 20 20 20 20 20 20 20 20 20 20 20  load;           
156ea 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
156eb 65 73 20 6f 66 20 63 65 6c 6c 20 70 61 79 6c 6f  es of cell paylo
156ec 61 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  ad */..  assert(
156ed 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
156ee 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
156ef 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70 49 6e  mutex) );..  pIn
156f0 66 6f 2d 3e 70 43 65 6c 6c 20 3d 20 70 43 65 6c  fo->pCell = pCel
156f1 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  l;.  assert( pPa
156f2 67 65 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c 20 70  ge->leaf==0 || p
156f3 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 31 20 29 3b  Page->leaf==1 );
156f4 0a 20 20 6e 20 3d 20 70 50 61 67 65 2d 3e 63 68  .  n = pPage->ch
156f5 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 61 73  ildPtrSize;.  as
156f6 73 65 72 74 28 20 6e 3d 3d 34 2d 34 2a 70 50 61  sert( n==4-4*pPa
156f7 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 69 66  ge->leaf );.  if
156f8 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20  ( pPage->intKey 
156f9 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  ){.    if( pPage
156fa 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20  ->hasData ){.   
156fb 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e     n += getVarin
156fc 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 6e  t32(&pCell[n], n
156fd 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 7d 65  Payload);.    }e
156fe 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61 79 6c  lse{.      nPayl
156ff 6f 61 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  oad = 0;.    }. 
15700 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e     n += getVarin
15701 74 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 28 75 36  t(&pCell[n], (u6
15702 34 2a 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65 79 29  4*)&pInfo->nKey)
15703 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 44 61  ;.    pInfo->nDa
15704 74 61 20 3d 20 6e 50 61 79 6c 6f 61 64 3b 0a 20  ta = nPayload;. 
15705 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 66   }else{.    pInf
15706 6f 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20  o->nData = 0;.  
15707 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69 6e 74    n += getVarint
15708 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 6e 50  32(&pCell[n], nP
15709 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 70 49 6e  ayload);.    pIn
1570a 66 6f 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61 79 6c  fo->nKey = nPayl
1570b 6f 61 64 3b 0a 20 20 7d 0a 20 20 70 49 6e 66 6f  oad;.  }.  pInfo
1570c 2d 3e 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 50 61  ->nPayload = nPa
1570d 79 6c 6f 61 64 3b 0a 20 20 70 49 6e 66 6f 2d 3e  yload;.  pInfo->
1570e 6e 48 65 61 64 65 72 20 3d 20 6e 3b 0a 20 20 74  nHeader = n;.  t
1570f 65 73 74 63 61 73 65 28 20 6e 50 61 79 6c 6f 61  estcase( nPayloa
15710 64 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63  d==pPage->maxLoc
15711 61 6c 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65  al );.  testcase
15712 28 20 6e 50 61 79 6c 6f 61 64 3d 3d 70 50 61 67  ( nPayload==pPag
15713 65 2d 3e 6d 61 78 4c 6f 63 61 6c 2b 31 20 29 3b  e->maxLocal+1 );
15714 0a 20 20 69 66 28 20 6c 69 6b 65 6c 79 28 6e 50  .  if( likely(nP
15715 61 79 6c 6f 61 64 3c 3d 70 50 61 67 65 2d 3e 6d  ayload<=pPage->m
15716 61 78 4c 6f 63 61 6c 29 20 29 7b 0a 20 20 20 20  axLocal) ){.    
15717 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 28  /* This is the (
15718 65 61 73 79 29 20 63 6f 6d 6d 6f 6e 20 63 61 73  easy) common cas
15719 65 20 77 68 65 72 65 20 74 68 65 20 65 6e 74 69  e where the enti
1571a 72 65 20 70 61 79 6c 6f 61 64 20 66 69 74 73 0a  re payload fits.
1571b 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 6f      ** on the lo
1571c 63 61 6c 20 70 61 67 65 2e 20 20 4e 6f 20 6f 76  cal page.  No ov
1571d 65 72 66 6c 6f 77 20 69 73 20 72 65 71 75 69 72  erflow is requir
1571e 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ed..    */.    i
1571f 6e 74 20 6e 53 69 7a 65 3b 20 20 20 20 20 20 20  nt nSize;       
15720 20 20 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65     /* Total size
15721 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74   of cell content
15722 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 20   in bytes */.   
15723 20 6e 53 69 7a 65 20 3d 20 6e 50 61 79 6c 6f 61   nSize = nPayloa
15724 64 20 2b 20 6e 3b 0a 20 20 20 20 70 49 6e 66 6f  d + n;.    pInfo
15725 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29  ->nLocal = (u16)
15726 6e 50 61 79 6c 6f 61 64 3b 0a 20 20 20 20 70 49  nPayload;.    pI
15727 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 3d  nfo->iOverflow =
15728 20 30 3b 0a 20 20 20 20 69 66 28 20 28 6e 53 69   0;.    if( (nSi
15729 7a 65 20 26 20 7e 33 29 3d 3d 30 20 29 7b 0a 20  ze & ~3)==0 ){. 
1572a 20 20 20 20 20 6e 53 69 7a 65 20 3d 20 34 3b 20       nSize = 4; 
1572b 20 20 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75         /* Minimu
1572c 6d 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 34  m cell size is 4
1572d 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 70 49   */.    }.    pI
1572e 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 28 75 31  nfo->nSize = (u1
1572f 36 29 6e 53 69 7a 65 3b 0a 20 20 7d 65 6c 73 65  6)nSize;.  }else
15730 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  {.    /* If the 
15731 70 61 79 6c 6f 61 64 20 77 69 6c 6c 20 6e 6f 74  payload will not
15732 20 66 69 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20   fit completely 
15733 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67  on the local pag
15734 65 2c 20 77 65 20 68 61 76 65 0a 20 20 20 20 2a  e, we have.    *
15735 2a 20 74 6f 20 64 65 63 69 64 65 20 68 6f 77 20  * to decide how 
15736 6d 75 63 68 20 74 6f 20 73 74 6f 72 65 20 6c 6f  much to store lo
15737 63 61 6c 6c 79 20 61 6e 64 20 68 6f 77 20 6d 75  cally and how mu
15738 63 68 20 74 6f 20 73 70 69 6c 6c 20 6f 6e 74 6f  ch to spill onto
15739 0a 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77  .    ** overflow
1573a 20 70 61 67 65 73 2e 20 20 54 68 65 20 73 74 72   pages.  The str
1573b 61 74 65 67 79 20 69 73 20 74 6f 20 6d 69 6e 69  ategy is to mini
1573c 6d 69 7a 65 20 74 68 65 20 61 6d 6f 75 6e 74 20  mize the amount 
1573d 6f 66 20 75 6e 75 73 65 64 0a 20 20 20 20 2a 2a  of unused.    **
1573e 20 73 70 61 63 65 20 6f 6e 20 6f 76 65 72 66 6c   space on overfl
1573f 6f 77 20 70 61 67 65 73 20 77 68 69 6c 65 20 6b  ow pages while k
15740 65 65 70 69 6e 67 20 74 68 65 20 61 6d 6f 75 6e  eeping the amoun
15741 74 20 6f 66 20 6c 6f 63 61 6c 20 73 74 6f 72 61  t of local stora
15742 67 65 0a 20 20 20 20 2a 2a 20 69 6e 20 62 65 74  ge.    ** in bet
15743 77 65 65 6e 20 6d 69 6e 4c 6f 63 61 6c 20 61 6e  ween minLocal an
15744 64 20 6d 61 78 4c 6f 63 61 6c 2e 0a 20 20 20 20  d maxLocal..    
15745 2a 2a 0a 20 20 20 20 2a 2a 20 57 61 72 6e 69 6e  **.    ** Warnin
15746 67 3a 20 20 63 68 61 6e 67 69 6e 67 20 74 68 65  g:  changing the
15747 20 77 61 79 20 6f 76 65 72 66 6c 6f 77 20 70 61   way overflow pa
15748 79 6c 6f 61 64 20 69 73 20 64 69 73 74 72 69 62  yload is distrib
15749 75 74 65 64 20 69 6e 20 61 6e 79 0a 20 20 20 20  uted in any.    
1574a 2a 2a 20 77 61 79 20 77 69 6c 6c 20 72 65 73 75  ** way will resu
1574b 6c 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6d 70 61  lt in an incompa
1574c 74 69 62 6c 65 20 66 69 6c 65 20 66 6f 72 6d 61  tible file forma
1574d 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e  t..    */.    in
1574e 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20 20 2f 2a 20  t minLocal;  /* 
1574f 4d 69 6e 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f  Minimum amount o
15750 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c  f payload held l
15751 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 69 6e  ocally */.    in
15752 74 20 6d 61 78 4c 6f 63 61 6c 3b 20 20 2f 2a 20  t maxLocal;  /* 
15753 4d 61 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f  Maximum amount o
15754 66 20 70 61 79 6c 6f 61 64 20 68 65 6c 64 20 6c  f payload held l
15755 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 69 6e  ocally */.    in
15756 74 20 73 75 72 70 6c 75 73 3b 20 20 20 2f 2a 20  t surplus;   /* 
15757 4f 76 65 72 66 6c 6f 77 20 70 61 79 6c 6f 61 64  Overflow payload
15758 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 6c   available for l
15759 6f 63 61 6c 20 73 74 6f 72 61 67 65 20 2a 2f 0a  ocal storage */.
1575a 0a 20 20 20 20 6d 69 6e 4c 6f 63 61 6c 20 3d 20  .    minLocal = 
1575b 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b  pPage->minLocal;
1575c 0a 20 20 20 20 6d 61 78 4c 6f 63 61 6c 20 3d 20  .    maxLocal = 
1575d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 3b  pPage->maxLocal;
1575e 0a 20 20 20 20 73 75 72 70 6c 75 73 20 3d 20 6d  .    surplus = m
1575f 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e 50 61 79 6c  inLocal + (nPayl
15760 6f 61 64 20 2d 20 6d 69 6e 4c 6f 63 61 6c 29 25  oad - minLocal)%
15761 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61  (pPage->pBt->usa
15762 62 6c 65 53 69 7a 65 20 2d 20 34 29 3b 0a 20 20  bleSize - 4);.  
15763 20 20 74 65 73 74 63 61 73 65 28 20 73 75 72 70    testcase( surp
15764 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c 20 29 3b  lus==maxLocal );
15765 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 73  .    testcase( s
15766 75 72 70 6c 75 73 3d 3d 6d 61 78 4c 6f 63 61 6c  urplus==maxLocal
15767 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20 73 75  +1 );.    if( su
15768 72 70 6c 75 73 20 3c 3d 20 6d 61 78 4c 6f 63 61  rplus <= maxLoca
15769 6c 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 66 6f  l ){.      pInfo
1576a 2d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29  ->nLocal = (u16)
1576b 73 75 72 70 6c 75 73 3b 0a 20 20 20 20 7d 65 6c  surplus;.    }el
1576c 73 65 7b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d  se{.      pInfo-
1576d 3e 6e 4c 6f 63 61 6c 20 3d 20 28 75 31 36 29 6d  >nLocal = (u16)m
1576e 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d 0a 20  inLocal;.    }. 
1576f 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66     pInfo->iOverf
15770 6c 6f 77 20 3d 20 28 75 31 36 29 28 70 49 6e 66  low = (u16)(pInf
15771 6f 2d 3e 6e 4c 6f 63 61 6c 20 2b 20 6e 29 3b 0a  o->nLocal + n);.
15772 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53 69 7a 65      pInfo->nSize
15773 20 3d 20 70 49 6e 66 6f 2d 3e 69 4f 76 65 72 66   = pInfo->iOverf
15774 6c 6f 77 20 2b 20 34 3b 0a 20 20 7d 0a 7d 0a 23  low + 4;.  }.}.#
15775 64 65 66 69 6e 65 20 70 61 72 73 65 43 65 6c 6c  define parseCell
15776 28 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c 20 70  (pPage, iCell, p
15777 49 6e 66 6f 29 20 5c 0a 20 20 62 74 72 65 65 50  Info) \.  btreeP
15778 61 72 73 65 43 65 6c 6c 50 74 72 28 28 70 50 61  arseCellPtr((pPa
15779 67 65 29 2c 20 66 69 6e 64 43 65 6c 6c 28 28 70  ge), findCell((p
1577a 50 61 67 65 29 2c 20 28 69 43 65 6c 6c 29 29 2c  Page), (iCell)),
1577b 20 28 70 49 6e 66 6f 29 29 0a 73 74 61 74 69 63   (pInfo)).static
1577c 20 76 6f 69 64 20 62 74 72 65 65 50 61 72 73 65   void btreeParse
1577d 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20  Cell(.  MemPage 
1577e 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20  *pPage,         
1577f 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69  /* Page containi
15780 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20  ng the cell */. 
15781 20 69 6e 74 20 69 43 65 6c 6c 2c 20 20 20 20 20   int iCell,     
15782 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
15783 63 65 6c 6c 20 69 6e 64 65 78 2e 20 20 46 69 72  cell index.  Fir
15784 73 74 20 63 65 6c 6c 20 69 73 20 30 20 2a 2f 0a  st cell is 0 */.
15785 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e 66    CellInfo *pInf
15786 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c  o         /* Fil
15787 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75 63 74  l in this struct
15788 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 70 61 72 73  ure */.){.  pars
15789 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65  eCell(pPage, iCe
1578a 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a 0a 2f  ll, pInfo);.}../
1578b 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65  *.** Compute the
1578c 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
1578d 20 62 79 74 65 73 20 74 68 61 74 20 61 20 43 65   bytes that a Ce
1578e 6c 6c 20 6e 65 65 64 73 20 69 6e 20 74 68 65 20  ll needs in the 
1578f 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 61 72 65  cell.** data are
15790 61 20 6f 66 20 74 68 65 20 62 74 72 65 65 2d 70  a of the btree-p
15791 61 67 65 2e 20 20 54 68 65 20 72 65 74 75 72 6e  age.  The return
15792 20 6e 75 6d 62 65 72 20 69 6e 63 6c 75 64 65 73   number includes
15793 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 61 74   the cell.** dat
15794 61 20 68 65 61 64 65 72 20 61 6e 64 20 74 68 65  a header and the
15795 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 2c 20   local payload, 
15796 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 76 65 72  but not any over
15797 66 6c 6f 77 20 70 61 67 65 20 6f 72 0a 2a 2a 20  flow page or.** 
15798 74 68 65 20 73 70 61 63 65 20 75 73 65 64 20 62  the space used b
15799 79 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74  y the cell point
1579a 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 31  er..*/.static u1
1579b 36 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 4d 65  6 cellSizePtr(Me
1579c 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 75 38  mPage *pPage, u8
1579d 20 2a 70 43 65 6c 6c 29 7b 0a 20 20 75 38 20 2a   *pCell){.  u8 *
1579e 70 49 74 65 72 20 3d 20 26 70 43 65 6c 6c 5b 70  pIter = &pCell[p
1579f 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53 69  Page->childPtrSi
157a0 7a 65 5d 3b 0a 20 20 75 33 32 20 6e 53 69 7a 65  ze];.  u32 nSize
157a1 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
157a2 5f 44 45 42 55 47 0a 20 20 2f 2a 20 54 68 65 20  _DEBUG.  /* The 
157a3 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62  value returned b
157a4 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  y this function 
157a5 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65  should always be
157a6 20 74 68 65 20 73 61 6d 65 20 61 73 0a 20 20 2a   the same as.  *
157a7 2a 20 74 68 65 20 28 43 65 6c 6c 49 6e 66 6f 2e  * the (CellInfo.
157a8 6e 53 69 7a 65 29 20 76 61 6c 75 65 20 66 6f 75  nSize) value fou
157a9 6e 64 20 62 79 20 64 6f 69 6e 67 20 61 20 66 75  nd by doing a fu
157aa 6c 6c 20 70 61 72 73 65 20 6f 66 20 74 68 65 0a  ll parse of the.
157ab 20 20 2a 2a 20 63 65 6c 6c 2e 20 49 66 20 53 51    ** cell. If SQ
157ac 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 64 65  LITE_DEBUG is de
157ad 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74  fined, an assert
157ae 28 29 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d  () at the bottom
157af 20 6f 66 0a 20 20 2a 2a 20 74 68 69 73 20 66 75   of.  ** this fu
157b0 6e 63 74 69 6f 6e 20 76 65 72 69 66 69 65 73 20  nction verifies 
157b1 74 68 61 74 20 74 68 69 73 20 69 6e 76 61 72 69  that this invari
157b2 61 6e 74 20 69 73 20 6e 6f 74 20 76 69 6f 6c 61  ant is not viola
157b3 74 65 64 2e 20 2a 2f 0a 20 20 43 65 6c 6c 49 6e  ted. */.  CellIn
157b4 66 6f 20 64 65 62 75 67 69 6e 66 6f 3b 0a 20 20  fo debuginfo;.  
157b5 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74  btreeParseCellPt
157b6 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20  r(pPage, pCell, 
157b7 26 64 65 62 75 67 69 6e 66 6f 29 3b 0a 23 65 6e  &debuginfo);.#en
157b8 64 69 66 0a 0a 20 20 69 66 28 20 70 50 61 67 65  dif..  if( pPage
157b9 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20  ->intKey ){.    
157ba 75 38 20 2a 70 45 6e 64 3b 0a 20 20 20 20 69 66  u8 *pEnd;.    if
157bb 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61  ( pPage->hasData
157bc 20 29 7b 0a 20 20 20 20 20 20 70 49 74 65 72 20   ){.      pIter 
157bd 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 70  += getVarint32(p
157be 49 74 65 72 2c 20 6e 53 69 7a 65 29 3b 0a 20 20  Iter, nSize);.  
157bf 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e    }else{.      n
157c0 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  Size = 0;.    }.
157c1 0a 20 20 20 20 2f 2a 20 70 49 74 65 72 20 6e 6f  .    /* pIter no
157c2 77 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20  w points at the 
157c3 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20 6b  64-bit integer k
157c4 65 79 20 76 61 6c 75 65 2c 20 61 20 76 61 72 69  ey value, a vari
157c5 61 62 6c 65 20 6c 65 6e 67 74 68 20 0a 20 20 20  able length .   
157c6 20 2a 2a 20 69 6e 74 65 67 65 72 2e 20 54 68 65   ** integer. The
157c7 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b   following block
157c8 20 6d 6f 76 65 73 20 70 49 74 65 72 20 74 6f 20   moves pIter to 
157c9 70 6f 69 6e 74 20 61 74 20 74 68 65 20 66 69 72  point at the fir
157ca 73 74 20 62 79 74 65 0a 20 20 20 20 2a 2a 20 70  st byte.    ** p
157cb 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74  ast the end of t
157cc 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 2a 2f  he key value. */
157cd 0a 20 20 20 20 70 45 6e 64 20 3d 20 26 70 49 74  .    pEnd = &pIt
157ce 65 72 5b 39 5d 3b 0a 20 20 20 20 77 68 69 6c 65  er[9];.    while
157cf 28 20 28 2a 70 49 74 65 72 2b 2b 29 26 30 78 38  ( (*pIter++)&0x8
157d0 30 20 26 26 20 70 49 74 65 72 3c 70 45 6e 64 20  0 && pIter<pEnd 
157d1 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
157d2 70 49 74 65 72 20 2b 3d 20 67 65 74 56 61 72 69  pIter += getVari
157d3 6e 74 33 32 28 70 49 74 65 72 2c 20 6e 53 69 7a  nt32(pIter, nSiz
157d4 65 29 3b 0a 20 20 7d 0a 0a 20 20 74 65 73 74 63  e);.  }..  testc
157d5 61 73 65 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67  ase( nSize==pPag
157d6 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20  e->maxLocal );. 
157d7 20 74 65 73 74 63 61 73 65 28 20 6e 53 69 7a 65   testcase( nSize
157d8 3d 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61  ==pPage->maxLoca
157d9 6c 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e 53 69  l+1 );.  if( nSi
157da 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63  ze>pPage->maxLoc
157db 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 69  al ){.    int mi
157dc 6e 4c 6f 63 61 6c 20 3d 20 70 50 61 67 65 2d 3e  nLocal = pPage->
157dd 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 6e 53  minLocal;.    nS
157de 69 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c 20 2b  ize = minLocal +
157df 20 28 6e 53 69 7a 65 20 2d 20 6d 69 6e 4c 6f 63   (nSize - minLoc
157e0 61 6c 29 20 25 20 28 70 50 61 67 65 2d 3e 70 42  al) % (pPage->pB
157e1 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20  t->usableSize - 
157e2 34 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65  4);.    testcase
157e3 28 20 6e 53 69 7a 65 3d 3d 70 50 61 67 65 2d 3e  ( nSize==pPage->
157e4 6d 61 78 4c 6f 63 61 6c 20 29 3b 0a 20 20 20 20  maxLocal );.    
157e5 74 65 73 74 63 61 73 65 28 20 6e 53 69 7a 65 3d  testcase( nSize=
157e6 3d 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c  =pPage->maxLocal
157e7 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 53  +1 );.    if( nS
157e8 69 7a 65 3e 70 50 61 67 65 2d 3e 6d 61 78 4c 6f  ize>pPage->maxLo
157e9 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 6e 53 69  cal ){.      nSi
157ea 7a 65 20 3d 20 6d 69 6e 4c 6f 63 61 6c 3b 0a 20  ze = minLocal;. 
157eb 20 20 20 7d 0a 20 20 20 20 6e 53 69 7a 65 20 2b     }.    nSize +
157ec 3d 20 34 3b 0a 20 20 7d 0a 20 20 6e 53 69 7a 65  = 4;.  }.  nSize
157ed 20 2b 3d 20 28 75 33 32 29 28 70 49 74 65 72 20   += (u32)(pIter 
157ee 2d 20 70 43 65 6c 6c 29 3b 0a 0a 20 20 2f 2a 20  - pCell);..  /* 
157ef 54 68 65 20 6d 69 6e 69 6d 75 6d 20 73 69 7a 65  The minimum size
157f0 20 6f 66 20 61 6e 79 20 63 65 6c 6c 20 69 73 20   of any cell is 
157f1 34 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 69 66  4 bytes. */.  if
157f2 28 20 6e 53 69 7a 65 3c 34 20 29 7b 0a 20 20 20  ( nSize<4 ){.   
157f3 20 6e 53 69 7a 65 20 3d 20 34 3b 0a 20 20 7d 0a   nSize = 4;.  }.
157f4 0a 20 20 61 73 73 65 72 74 28 20 6e 53 69 7a 65  .  assert( nSize
157f5 3d 3d 64 65 62 75 67 69 6e 66 6f 2e 6e 53 69 7a  ==debuginfo.nSiz
157f6 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 75  e );.  return (u
157f7 31 36 29 6e 53 69 7a 65 3b 0a 7d 0a 0a 23 69 66  16)nSize;.}..#if
157f8 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
157f9 0a 2f 2a 20 54 68 69 73 20 76 61 72 69 61 74 69  ./* This variati
157fa 6f 6e 20 6f 6e 20 63 65 6c 6c 53 69 7a 65 50 74  on on cellSizePt
157fb 72 28 29 20 69 73 20 75 73 65 64 20 69 6e 73 69  r() is used insi
157fc 64 65 20 6f 66 20 61 73 73 65 72 74 28 29 20 73  de of assert() s
157fd 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 6f 6e 6c  tatements.** onl
157fe 79 2e 20 2a 2f 0a 73 74 61 74 69 63 20 75 31 36  y. */.static u16
157ff 20 63 65 6c 6c 53 69 7a 65 28 4d 65 6d 50 61 67   cellSize(MemPag
15800 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69 43  e *pPage, int iC
15801 65 6c 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 63  ell){.  return c
15802 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65  ellSizePtr(pPage
15803 2c 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  , findCell(pPage
15804 2c 20 69 43 65 6c 6c 29 29 3b 0a 7d 0a 23 65 6e  , iCell));.}.#en
15805 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
15806 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
15807 55 55 4d 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  UUM./*.** If the
15808 20 63 65 6c 6c 20 70 43 65 6c 6c 2c 20 70 61 72   cell pCell, par
15809 74 20 6f 66 20 70 61 67 65 20 70 50 61 67 65 20  t of page pPage 
1580a 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74  contains a point
1580b 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 6f 76 65 72  er.** to an over
1580c 66 6c 6f 77 20 70 61 67 65 2c 20 69 6e 73 65 72  flow page, inser
1580d 74 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20  t an entry into 
1580e 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a  the pointer-map.
1580f 2a 2a 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66  ** for the overf
15810 6c 6f 77 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61  low page..*/.sta
15811 74 69 63 20 76 6f 69 64 20 70 74 72 6d 61 70 50  tic void ptrmapP
15812 75 74 4f 76 66 6c 50 74 72 28 4d 65 6d 50 61 67  utOvflPtr(MemPag
15813 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a 70 43  e *pPage, u8 *pC
15814 65 6c 6c 2c 20 69 6e 74 20 2a 70 52 43 29 7b 0a  ell, int *pRC){.
15815 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b    CellInfo info;
15816 0a 20 20 69 66 28 20 2a 70 52 43 20 29 20 72 65  .  if( *pRC ) re
15817 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  turn;.  assert( 
15818 70 43 65 6c 6c 21 3d 30 20 29 3b 0a 20 20 62 74  pCell!=0 );.  bt
15819 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28  reeParseCellPtr(
1581a 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69  pPage, pCell, &i
1581b 6e 66 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20  nfo);.  assert( 
1581c 28 69 6e 66 6f 2e 6e 44 61 74 61 2b 28 70 50 61  (info.nData+(pPa
1581d 67 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a 69 6e 66  ge->intKey?0:inf
1581e 6f 2e 6e 4b 65 79 29 29 3d 3d 69 6e 66 6f 2e 6e  o.nKey))==info.n
1581f 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20 69 66 28  Payload );.  if(
15820 20 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20   info.iOverflow 
15821 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6f 76 66 6c  ){.    Pgno ovfl
15822 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65   = get4byte(&pCe
15823 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  ll[info.iOverflo
15824 77 5d 29 3b 0a 20 20 20 20 70 74 72 6d 61 70 50  w]);.    ptrmapP
15825 75 74 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 6f  ut(pPage->pBt, o
15826 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52  vfl, PTRMAP_OVER
15827 46 4c 4f 57 31 2c 20 70 50 61 67 65 2d 3e 70 67  FLOW1, pPage->pg
15828 6e 6f 2c 20 70 52 43 29 3b 0a 20 20 7d 0a 7d 0a  no, pRC);.  }.}.
15829 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44  #endif.../*.** D
1582a 65 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61  efragment the pa
1582b 67 65 20 67 69 76 65 6e 2e 20 20 41 6c 6c 20 43  ge given.  All C
1582c 65 6c 6c 73 20 61 72 65 20 6d 6f 76 65 64 20 74  ells are moved t
1582d 6f 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20  o the.** end of 
1582e 74 68 65 20 70 61 67 65 20 61 6e 64 20 61 6c 6c  the page and all
1582f 20 66 72 65 65 20 73 70 61 63 65 20 69 73 20 63   free space is c
15830 6f 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e  ollected into on
15831 65 0a 2a 2a 20 62 69 67 20 46 72 65 65 42 6c 6b  e.** big FreeBlk
15832 20 74 68 61 74 20 6f 63 63 75 72 73 20 69 6e 20   that occurs in 
15833 62 65 74 77 65 65 6e 20 74 68 65 20 68 65 61 64  between the head
15834 65 72 20 61 6e 64 20 63 65 6c 6c 0a 2a 2a 20 70  er and cell.** p
15835 6f 69 6e 74 65 72 20 61 72 72 61 79 20 61 6e 64  ointer array and
15836 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
15837 74 20 61 72 65 61 2e 0a 2a 2f 0a 73 74 61 74 69  t area..*/.stati
15838 63 20 69 6e 74 20 64 65 66 72 61 67 6d 65 6e 74  c int defragment
15839 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50  Page(MemPage *pP
1583a 61 67 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20  age){.  int i;  
1583b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1583c 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
1583d 65 72 20 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20  er */.  int pc; 
1583e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1583f 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
15840 20 61 20 69 2d 74 68 20 63 65 6c 6c 20 2a 2f 0a   a i-th cell */.
15841 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20    int hdr;      
15842 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
15843 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 70 61  Offset to the pa
15844 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 69  ge header */.  i
15845 6e 74 20 73 69 7a 65 3b 20 20 20 20 20 20 20 20  nt size;        
15846 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
15847 65 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f 0a 20  e of a cell */. 
15848 20 69 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b   int usableSize;
15849 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1584a 75 6d 62 65 72 20 6f 66 20 75 73 61 62 6c 65 20  umber of usable 
1584b 62 79 74 65 73 20 6f 6e 20 61 20 70 61 67 65 20  bytes on a page 
1584c 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66  */.  int cellOff
1584d 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  set;            
1584e 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 74 68 65  /* Offset to the
1584f 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72   cell pointer ar
15850 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 63 62 72  ray */.  int cbr
15851 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  k;              
15852 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f      /* Offset to
15853 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
15854 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e 74 20  t area */.  int 
15855 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  nCell;          
15856 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
15857 20 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65   of cells on the
15858 20 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69 67   page */.  unsig
15859 6e 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b 20  ned char *data; 
1585a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67        /* The pag
1585b 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 6e 73 69  e data */.  unsi
1585c 67 6e 65 64 20 63 68 61 72 20 2a 74 65 6d 70 3b  gned char *temp;
1585d 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20 61         /* Temp a
1585e 72 65 61 20 66 6f 72 20 63 65 6c 6c 20 63 6f 6e  rea for cell con
1585f 74 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 69 43  tent */.  int iC
15860 65 6c 6c 46 69 72 73 74 3b 20 20 20 20 20 20 20  ellFirst;       
15861 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 6c       /* First al
15862 6c 6f 77 61 62 6c 65 20 63 65 6c 6c 20 69 6e 64  lowable cell ind
15863 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c  ex */.  int iCel
15864 6c 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 20  lLast;          
15865 20 20 20 2f 2a 20 4c 61 73 74 20 70 6f 73 73 69     /* Last possi
15866 62 6c 65 20 63 65 6c 6c 20 69 6e 64 65 78 20 2a  ble cell index *
15867 2f 0a 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  /...  assert( sq
15868 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
15869 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62  eable(pPage->pDb
1586a 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72  Page) );.  asser
1586b 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30  t( pPage->pBt!=0
1586c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
1586d 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
1586e 53 69 7a 65 20 3c 3d 20 53 51 4c 49 54 45 5f 4d  Size <= SQLITE_M
1586f 41 58 5f 50 41 47 45 5f 53 49 5a 45 20 29 3b 0a  AX_PAGE_SIZE );.
15870 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
15871 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b  >nOverflow==0 );
15872 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
15873 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
15874 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
15875 20 29 3b 0a 20 20 74 65 6d 70 20 3d 20 73 71 6c   );.  temp = sql
15876 69 74 65 33 50 61 67 65 72 54 65 6d 70 53 70 61  ite3PagerTempSpa
15877 63 65 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70  ce(pPage->pBt->p
15878 50 61 67 65 72 29 3b 0a 20 20 64 61 74 61 20 3d  Pager);.  data =
15879 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20   pPage->aData;. 
1587a 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64   hdr = pPage->hd
1587b 72 4f 66 66 73 65 74 3b 0a 20 20 63 65 6c 6c 4f  rOffset;.  cellO
1587c 66 66 73 65 74 20 3d 20 70 50 61 67 65 2d 3e 63  ffset = pPage->c
1587d 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20 6e 43 65  ellOffset;.  nCe
1587e 6c 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c  ll = pPage->nCel
1587f 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 43 65  l;.  assert( nCe
15880 6c 6c 3d 3d 67 65 74 32 62 79 74 65 28 26 64 61  ll==get2byte(&da
15881 74 61 5b 68 64 72 2b 33 5d 29 20 29 3b 0a 20 20  ta[hdr+3]) );.  
15882 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70 50 61  usableSize = pPa
15883 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53  ge->pBt->usableS
15884 69 7a 65 3b 0a 20 20 63 62 72 6b 20 3d 20 67 65  ize;.  cbrk = ge
15885 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72  t2byte(&data[hdr
15886 2b 35 5d 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26  +5]);.  memcpy(&
15887 74 65 6d 70 5b 63 62 72 6b 5d 2c 20 26 64 61 74  temp[cbrk], &dat
15888 61 5b 63 62 72 6b 5d 2c 20 75 73 61 62 6c 65 53  a[cbrk], usableS
15889 69 7a 65 20 2d 20 63 62 72 6b 29 3b 0a 20 20 63  ize - cbrk);.  c
1588a 62 72 6b 20 3d 20 75 73 61 62 6c 65 53 69 7a 65  brk = usableSize
1588b 3b 0a 20 20 69 43 65 6c 6c 46 69 72 73 74 20 3d  ;.  iCellFirst =
1588c 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a   cellOffset + 2*
1588d 6e 43 65 6c 6c 3b 0a 20 20 69 43 65 6c 6c 4c 61  nCell;.  iCellLa
1588e 73 74 20 3d 20 75 73 61 62 6c 65 53 69 7a 65 20  st = usableSize 
1588f 2d 20 34 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  - 4;.  for(i=0; 
15890 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20  i<nCell; i++){. 
15891 20 20 20 75 38 20 2a 70 41 64 64 72 3b 20 20 20     u8 *pAddr;   
15892 20 20 2f 2a 20 54 68 65 20 69 2d 74 68 20 63 65    /* The i-th ce
15893 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20  ll pointer */.  
15894 20 20 70 41 64 64 72 20 3d 20 26 64 61 74 61 5b    pAddr = &data[
15895 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 69 2a 32  cellOffset + i*2
15896 5d 3b 0a 20 20 20 20 70 63 20 3d 20 67 65 74 32  ];.    pc = get2
15897 62 79 74 65 28 70 41 64 64 72 29 3b 0a 20 20 20  byte(pAddr);.   
15898 20 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69   testcase( pc==i
15899 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 20  CellFirst );.   
1589a 20 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69   testcase( pc==i
1589b 43 65 6c 6c 4c 61 73 74 20 29 3b 0a 23 69 66 20  CellLast );.#if 
1589c 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
1589d 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f  ENABLE_OVERSIZE_
1589e 43 45 4c 4c 5f 43 48 45 43 4b 29 0a 20 20 20 20  CELL_CHECK).    
1589f 2f 2a 20 54 68 65 73 65 20 63 6f 6e 64 69 74 69  /* These conditi
158a0 6f 6e 73 20 68 61 76 65 20 61 6c 72 65 61 64 79  ons have already
158a1 20 62 65 65 6e 20 76 65 72 69 66 69 65 64 20 69   been verified i
158a2 6e 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28  n btreeInitPage(
158a3 29 0a 20 20 20 20 2a 2a 20 69 66 20 53 51 4c 49  ).    ** if SQLI
158a4 54 45 5f 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49  TE_ENABLE_OVERSI
158a5 5a 45 5f 43 45 4c 4c 5f 43 48 45 43 4b 20 69 73  ZE_CELL_CHECK is
158a6 20 64 65 66 69 6e 65 64 20 0a 20 20 20 20 2a 2f   defined .    */
158a7 0a 20 20 20 20 69 66 28 20 70 63 3c 69 43 65 6c  .    if( pc<iCel
158a8 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e 69 43 65  lFirst || pc>iCe
158a9 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20 20 20 20  llLast ){.      
158aa 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
158ab 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
158ac 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 61 73 73  }.#endif.    ass
158ad 65 72 74 28 20 70 63 3e 3d 69 43 65 6c 6c 46 69  ert( pc>=iCellFi
158ae 72 73 74 20 26 26 20 70 63 3c 3d 69 43 65 6c 6c  rst && pc<=iCell
158af 4c 61 73 74 20 29 3b 0a 20 20 20 20 73 69 7a 65  Last );.    size
158b0 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70   = cellSizePtr(p
158b1 50 61 67 65 2c 20 26 74 65 6d 70 5b 70 63 5d 29  Page, &temp[pc])
158b2 3b 0a 20 20 20 20 63 62 72 6b 20 2d 3d 20 73 69  ;.    cbrk -= si
158b3 7a 65 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28  ze;.#if defined(
158b4 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4f 56  SQLITE_ENABLE_OV
158b5 45 52 53 49 5a 45 5f 43 45 4c 4c 5f 43 48 45 43  ERSIZE_CELL_CHEC
158b6 4b 29 0a 20 20 20 20 69 66 28 20 63 62 72 6b 3c  K).    if( cbrk<
158b7 69 43 65 6c 6c 46 69 72 73 74 20 29 7b 0a 20 20  iCellFirst ){.  
158b8 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
158b9 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
158ba 20 20 20 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20      }.#else.    
158bb 69 66 28 20 63 62 72 6b 3c 69 43 65 6c 6c 46 69  if( cbrk<iCellFi
158bc 72 73 74 20 7c 7c 20 70 63 2b 73 69 7a 65 3e 75  rst || pc+size>u
158bd 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20  sableSize ){.   
158be 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
158bf 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
158c0 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
158c1 61 73 73 65 72 74 28 20 63 62 72 6b 2b 73 69 7a  assert( cbrk+siz
158c2 65 3c 3d 75 73 61 62 6c 65 53 69 7a 65 20 26 26  e<=usableSize &&
158c3 20 63 62 72 6b 3e 3d 69 43 65 6c 6c 46 69 72 73   cbrk>=iCellFirs
158c4 74 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  t );.    testcas
158c5 65 28 20 63 62 72 6b 2b 73 69 7a 65 3d 3d 75 73  e( cbrk+size==us
158c6 61 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 20 20  ableSize );.    
158c7 74 65 73 74 63 61 73 65 28 20 70 63 2b 73 69 7a  testcase( pc+siz
158c8 65 3d 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 3b  e==usableSize );
158c9 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74  .    memcpy(&dat
158ca 61 5b 63 62 72 6b 5d 2c 20 26 74 65 6d 70 5b 70  a[cbrk], &temp[p
158cb 63 5d 2c 20 73 69 7a 65 29 3b 0a 20 20 20 20 70  c], size);.    p
158cc 75 74 32 62 79 74 65 28 70 41 64 64 72 2c 20 63  ut2byte(pAddr, c
158cd 62 72 6b 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  brk);.  }.  asse
158ce 72 74 28 20 63 62 72 6b 3e 3d 69 43 65 6c 6c 46  rt( cbrk>=iCellF
158cf 69 72 73 74 20 29 3b 0a 20 20 70 75 74 32 62 79  irst );.  put2by
158d0 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c  te(&data[hdr+5],
158d1 20 63 62 72 6b 29 3b 0a 20 20 64 61 74 61 5b 68   cbrk);.  data[h
158d2 64 72 2b 31 5d 20 3d 20 30 3b 0a 20 20 64 61 74  dr+1] = 0;.  dat
158d3 61 5b 68 64 72 2b 32 5d 20 3d 20 30 3b 0a 20 20  a[hdr+2] = 0;.  
158d4 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d 20 30 3b  data[hdr+7] = 0;
158d5 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b  .  memset(&data[
158d6 69 43 65 6c 6c 46 69 72 73 74 5d 2c 20 30 2c 20  iCellFirst], 0, 
158d7 63 62 72 6b 2d 69 43 65 6c 6c 46 69 72 73 74 29  cbrk-iCellFirst)
158d8 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
158d9 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61  te3PagerIswritea
158da 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ble(pPage->pDbPa
158db 67 65 29 20 29 3b 0a 20 20 69 66 28 20 63 62 72  ge) );.  if( cbr
158dc 6b 2d 69 43 65 6c 6c 46 69 72 73 74 21 3d 70 50  k-iCellFirst!=pP
158dd 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b 0a 20 20  age->nFree ){.  
158de 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
158df 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
158e0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
158e1 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  E_OK;.}../*.** A
158e2 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 20 62 79  llocate nByte by
158e3 74 65 73 20 6f 66 20 73 70 61 63 65 20 66 72 6f  tes of space fro
158e4 6d 20 77 69 74 68 69 6e 20 74 68 65 20 42 2d 54  m within the B-T
158e5 72 65 65 20 70 61 67 65 20 70 61 73 73 65 64 0a  ree page passed.
158e6 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ** as the first 
158e7 61 72 67 75 6d 65 6e 74 2e 20 57 72 69 74 65 20  argument. Write 
158e8 69 6e 74 6f 20 2a 70 49 64 78 20 74 68 65 20 69  into *pIdx the i
158e9 6e 64 65 78 20 69 6e 74 6f 20 70 50 61 67 65 2d  ndex into pPage-
158ea 3e 61 44 61 74 61 5b 5d 0a 2a 2a 20 6f 66 20 74  >aData[].** of t
158eb 68 65 20 66 69 72 73 74 20 62 79 74 65 20 6f 66  he first byte of
158ec 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65   allocated space
158ed 2e 20 52 65 74 75 72 6e 20 65 69 74 68 65 72 20  . Return either 
158ee 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 0a 2a 2a 20  SQLITE_OK or.** 
158ef 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 28 75  an error code (u
158f0 73 75 61 6c 6c 79 20 53 51 4c 49 54 45 5f 43 4f  sually SQLITE_CO
158f1 52 52 55 50 54 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  RRUPT)..**.** Th
158f2 65 20 63 61 6c 6c 65 72 20 67 75 61 72 61 6e 74  e caller guarant
158f3 65 65 73 20 74 68 61 74 20 74 68 65 72 65 20 69  ees that there i
158f4 73 20 73 75 66 66 69 63 69 65 6e 74 20 73 70 61  s sufficient spa
158f5 63 65 20 74 6f 20 6d 61 6b 65 20 74 68 65 0a 2a  ce to make the.*
158f6 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 54  * allocation.  T
158f7 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68  his routine migh
158f8 74 20 6e 65 65 64 20 74 6f 20 64 65 66 72 61 67  t need to defrag
158f9 6d 65 6e 74 20 69 6e 20 6f 72 64 65 72 20 74 6f  ment in order to
158fa 20 62 72 69 6e 67 0a 2a 2a 20 61 6c 6c 20 74 68   bring.** all th
158fb 65 20 73 70 61 63 65 20 74 6f 67 65 74 68 65 72  e space together
158fc 2c 20 68 6f 77 65 76 65 72 2e 20 20 54 68 69 73  , however.  This
158fd 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 61 76   routine will av
158fe 6f 69 64 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65  oid using.** the
158ff 20 66 69 72 73 74 20 74 77 6f 20 62 79 74 65 73   first two bytes
15900 20 70 61 73 74 20 74 68 65 20 63 65 6c 6c 20 70   past the cell p
15901 6f 69 6e 74 65 72 20 61 72 65 61 20 73 69 6e 63  ointer area sinc
15902 65 20 70 72 65 73 75 6d 61 62 6c 79 20 74 68 69  e presumably thi
15903 73 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  s.** allocation 
15904 69 73 20 62 65 69 6e 67 20 6d 61 64 65 20 69 6e  is being made in
15905 20 6f 72 64 65 72 20 74 6f 20 69 6e 73 65 72 74   order to insert
15906 20 61 20 6e 65 77 20 63 65 6c 6c 2c 20 73 6f 20   a new cell, so 
15907 77 65 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f 20  we will.** also 
15908 65 6e 64 20 75 70 20 6e 65 65 64 69 6e 67 20 61  end up needing a
15909 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74 65   new cell pointe
1590a 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
1590b 20 61 6c 6c 6f 63 61 74 65 53 70 61 63 65 28 4d   allocateSpace(M
1590c 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69  emPage *pPage, i
1590d 6e 74 20 6e 42 79 74 65 2c 20 69 6e 74 20 2a 70  nt nByte, int *p
1590e 49 64 78 29 7b 0a 20 20 63 6f 6e 73 74 20 69 6e  Idx){.  const in
1590f 74 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68  t hdr = pPage->h
15910 64 72 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a 20  drOffset;    /* 
15911 4c 6f 63 61 6c 20 63 61 63 68 65 20 6f 66 20 70  Local cache of p
15912 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 20  Page->hdrOffset 
15913 2a 2f 0a 20 20 75 38 20 2a 20 63 6f 6e 73 74 20  */.  u8 * const 
15914 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44  data = pPage->aD
15915 61 74 61 3b 20 20 20 20 20 20 2f 2a 20 4c 6f 63  ata;      /* Loc
15916 61 6c 20 63 61 63 68 65 20 6f 66 20 70 50 61 67  al cache of pPag
15917 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20 69 6e  e->aData */.  in
15918 74 20 6e 46 72 61 67 3b 20 20 20 20 20 20 20 20  t nFrag;        
15919 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1591a 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1591b 66 72 61 67 6d 65 6e 74 65 64 20 62 79 74 65 73  fragmented bytes
1591c 20 6f 6e 20 70 50 61 67 65 20 2a 2f 0a 20 20 69   on pPage */.  i
1591d 6e 74 20 74 6f 70 3b 20 20 20 20 20 20 20 20 20  nt top;         
1591e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1591f 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79 74      /* First byt
15920 65 20 6f 66 20 63 65 6c 6c 20 63 6f 6e 74 65 6e  e of cell conten
15921 74 20 61 72 65 61 20 2a 2f 0a 20 20 69 6e 74 20  t area */.  int 
15922 67 61 70 3b 20 20 20 20 20 20 20 20 2f 2a 20 46  gap;        /* F
15923 69 72 73 74 20 62 79 74 65 20 6f 66 20 67 61 70  irst byte of gap
15924 20 62 65 74 77 65 65 6e 20 63 65 6c 6c 20 70 6f   between cell po
15925 69 6e 74 65 72 73 20 61 6e 64 20 63 65 6c 6c 20  inters and cell 
15926 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  content */.  int
15927 20 72 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20   rc;         /* 
15928 49 6e 74 65 67 65 72 20 72 65 74 75 72 6e 20 63  Integer return c
15929 6f 64 65 20 2a 2f 0a 20 20 0a 20 20 61 73 73 65  ode */.  .  asse
1592a 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
1592b 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67  Iswriteable(pPag
1592c 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
1592d 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
1592e 70 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28  pBt );.  assert(
1592f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15930 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
15931 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
15932 72 74 28 20 6e 42 79 74 65 3e 3d 30 20 29 3b 20  rt( nByte>=0 ); 
15933 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c   /* Minimum cell
15934 20 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 20 20   size is 4 */.  
15935 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e  assert( pPage->n
15936 46 72 65 65 3e 3d 6e 42 79 74 65 20 29 3b 0a 20  Free>=nByte );. 
15937 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
15938 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a  nOverflow==0 );.
15939 20 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 3c    assert( nByte<
1593a 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62  pPage->pBt->usab
1593b 6c 65 53 69 7a 65 2d 38 20 29 3b 0a 0a 20 20 6e  leSize-8 );..  n
1593c 46 72 61 67 20 3d 20 64 61 74 61 5b 68 64 72 2b  Frag = data[hdr+
1593d 37 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  7];.  assert( pP
1593e 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20  age->cellOffset 
1593f 3d 3d 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a  == hdr + 12 - 4*
15940 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20  pPage->leaf );. 
15941 20 67 61 70 20 3d 20 70 50 61 67 65 2d 3e 63 65   gap = pPage->ce
15942 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70 50 61  llOffset + 2*pPa
15943 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 74 6f 70  ge->nCell;.  top
15944 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
15945 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 69 66 28  a[hdr+5]);.  if(
15946 20 67 61 70 3e 74 6f 70 20 29 20 72 65 74 75 72   gap>top ) retur
15947 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  n SQLITE_CORRUPT
15948 5f 42 4b 50 54 3b 0a 20 20 74 65 73 74 63 61 73  _BKPT;.  testcas
15949 65 28 20 67 61 70 2b 32 3d 3d 74 6f 70 20 29 3b  e( gap+2==top );
1594a 0a 20 20 74 65 73 74 63 61 73 65 28 20 67 61 70  .  testcase( gap
1594b 2b 31 3d 3d 74 6f 70 20 29 3b 0a 20 20 74 65 73  +1==top );.  tes
1594c 74 63 61 73 65 28 20 67 61 70 3d 3d 74 6f 70 20  tcase( gap==top 
1594d 29 3b 0a 0a 20 20 69 66 28 20 6e 46 72 61 67 3e  );..  if( nFrag>
1594e 3d 36 30 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6c  =60 ){.    /* Al
1594f 77 61 79 73 20 64 65 66 72 61 67 6d 65 6e 74 20  ways defragment 
15950 68 69 67 68 6c 79 20 66 72 61 67 6d 65 6e 74 65  highly fragmente
15951 64 20 70 61 67 65 73 20 2a 2f 0a 20 20 20 20 72  d pages */.    r
15952 63 20 3d 20 64 65 66 72 61 67 6d 65 6e 74 50 61  c = defragmentPa
15953 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 69  ge(pPage);.    i
15954 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
15955 63 3b 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74  c;.    top = get
15956 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b  2byte(&data[hdr+
15957 35 5d 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  5]);.  }else if(
15958 20 67 61 70 2b 32 3c 3d 74 6f 70 20 29 7b 0a 20   gap+2<=top ){. 
15959 20 20 20 2f 2a 20 53 65 61 72 63 68 20 74 68 65     /* Search the
1595a 20 66 72 65 65 6c 69 73 74 20 6c 6f 6f 6b 69 6e   freelist lookin
1595b 67 20 66 6f 72 20 61 20 66 72 65 65 20 73 6c 6f  g for a free slo
1595c 74 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20  t big enough to 
1595d 73 61 74 69 73 66 79 20 0a 20 20 20 20 2a 2a 20  satisfy .    ** 
1595e 74 68 65 20 72 65 71 75 65 73 74 2e 20 54 68 65  the request. The
1595f 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 6d   allocation is m
15960 61 64 65 20 66 72 6f 6d 20 74 68 65 20 66 69 72  ade from the fir
15961 73 74 20 66 72 65 65 20 73 6c 6f 74 20 69 6e 20  st free slot in 
15962 0a 20 20 20 20 2a 2a 20 74 68 65 20 6c 69 73 74  .    ** the list
15963 20 74 68 61 74 20 69 73 20 6c 61 72 67 65 20 65   that is large e
15964 6e 6f 75 67 68 20 74 6f 20 61 63 63 6f 6d 61 64  nough to accomad
15965 61 74 65 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20  ate it..    */. 
15966 20 20 20 69 6e 74 20 70 63 2c 20 61 64 64 72 3b     int pc, addr;
15967 0a 20 20 20 20 66 6f 72 28 61 64 64 72 3d 68 64  .    for(addr=hd
15968 72 2b 31 3b 20 28 70 63 20 3d 20 67 65 74 32 62  r+1; (pc = get2b
15969 79 74 65 28 26 64 61 74 61 5b 61 64 64 72 5d 29  yte(&data[addr])
1596a 29 3e 30 3b 20 61 64 64 72 3d 70 63 29 7b 0a 20  )>0; addr=pc){. 
1596b 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20       int size = 
1596c 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  get2byte(&data[p
1596d 63 2b 32 5d 29 3b 20 20 20 20 20 2f 2a 20 53 69  c+2]);     /* Si
1596e 7a 65 20 6f 66 20 66 72 65 65 20 73 6c 6f 74 20  ze of free slot 
1596f 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 69 7a  */.      if( siz
15970 65 3e 3d 6e 42 79 74 65 20 29 7b 0a 20 20 20 20  e>=nByte ){.    
15971 20 20 20 20 69 6e 74 20 78 20 3d 20 73 69 7a 65      int x = size
15972 20 2d 20 6e 42 79 74 65 3b 0a 20 20 20 20 20 20   - nByte;.      
15973 20 20 74 65 73 74 63 61 73 65 28 20 78 3d 3d 34    testcase( x==4
15974 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74   );.        test
15975 63 61 73 65 28 20 78 3d 3d 33 20 29 3b 0a 20 20  case( x==3 );.  
15976 20 20 20 20 20 20 69 66 28 20 78 3c 34 20 29 7b        if( x<4 ){
15977 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  .          /* Re
15978 6d 6f 76 65 20 74 68 65 20 73 6c 6f 74 20 66 72  move the slot fr
15979 6f 6d 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  om the free-list
1597a 2e 20 55 70 64 61 74 65 20 74 68 65 20 6e 75 6d  . Update the num
1597b 62 65 72 20 6f 66 0a 20 20 20 20 20 20 20 20 20  ber of.         
1597c 20 2a 2a 20 66 72 61 67 6d 65 6e 74 65 64 20 62   ** fragmented b
1597d 79 74 65 73 20 77 69 74 68 69 6e 20 74 68 65 20  ytes within the 
1597e 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20 20  page. */.       
1597f 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b     memcpy(&data[
15980 61 64 64 72 5d 2c 20 26 64 61 74 61 5b 70 63 5d  addr], &data[pc]
15981 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 2);.          
15982 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d 20 28 75  data[hdr+7] = (u
15983 38 29 28 6e 46 72 61 67 20 2b 20 78 29 3b 0a 20  8)(nFrag + x);. 
15984 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
15985 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73          /* The s
15986 6c 6f 74 20 72 65 6d 61 69 6e 73 20 6f 6e 20 74  lot remains on t
15987 68 65 20 66 72 65 65 2d 6c 69 73 74 2e 20 52 65  he free-list. Re
15988 64 75 63 65 20 69 74 73 20 73 69 7a 65 20 74 6f  duce its size to
15989 20 61 63 63 6f 75 6e 74 0a 20 20 20 20 20 20 20   account.       
1598a 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 70 6f     ** for the po
1598b 72 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68  rtion used by th
1598c 65 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e  e new allocation
1598d 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70  . */.          p
1598e 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63  ut2byte(&data[pc
1598f 2b 32 5d 2c 20 78 29 3b 0a 20 20 20 20 20 20 20  +2], x);.       
15990 20 7d 0a 20 20 20 20 20 20 20 20 2a 70 49 64 78   }.        *pIdx
15991 20 3d 20 70 63 20 2b 20 78 3b 0a 20 20 20 20 20   = pc + x;.     
15992 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
15993 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  _OK;.      }.   
15994 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65   }.  }..  /* Che
15995 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ck to make sure 
15996 74 68 65 72 65 20 69 73 20 65 6e 6f 75 67 68 20  there is enough 
15997 73 70 61 63 65 20 69 6e 20 74 68 65 20 67 61 70  space in the gap
15998 20 74 6f 20 73 61 74 69 73 66 79 0a 20 20 2a 2a   to satisfy.  **
15999 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e   the allocation.
1599a 20 20 49 66 20 6e 6f 74 2c 20 64 65 66 72 61 67    If not, defrag
1599b 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 74 65 73  ment..  */.  tes
1599c 74 63 61 73 65 28 20 67 61 70 2b 32 2b 6e 42 79  tcase( gap+2+nBy
1599d 74 65 3d 3d 74 6f 70 20 29 3b 0a 20 20 69 66 28  te==top );.  if(
1599e 20 67 61 70 2b 32 2b 6e 42 79 74 65 3e 74 6f 70   gap+2+nByte>top
1599f 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 64 65 66   ){.    rc = def
159a0 72 61 67 6d 65 6e 74 50 61 67 65 28 70 50 61 67  ragmentPage(pPag
159a1 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  e);.    if( rc )
159a2 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
159a3 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65 28 26  top = get2byte(&
159a4 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20  data[hdr+5]);.  
159a5 20 20 61 73 73 65 72 74 28 20 67 61 70 2b 6e 42    assert( gap+nB
159a6 79 74 65 3c 3d 74 6f 70 20 29 3b 0a 20 20 7d 0a  yte<=top );.  }.
159a7 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
159a8 6d 65 6d 6f 72 79 20 66 72 6f 6d 20 74 68 65 20  memory from the 
159a9 67 61 70 20 69 6e 20 62 65 74 77 65 65 6e 20 74  gap in between t
159aa 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20  he cell pointer 
159ab 61 72 72 61 79 0a 20 20 2a 2a 20 61 6e 64 20 74  array.  ** and t
159ac 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  he cell content 
159ad 61 72 65 61 2e 20 20 54 68 65 20 62 74 72 65 65  area.  The btree
159ae 49 6e 69 74 50 61 67 65 28 29 20 63 61 6c 6c 20  InitPage() call 
159af 68 61 73 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a  has already.  **
159b0 20 76 61 6c 69 64 61 74 65 64 20 74 68 65 20 66   validated the f
159b1 72 65 65 6c 69 73 74 2e 20 20 47 69 76 65 6e 20  reelist.  Given 
159b2 74 68 61 74 20 74 68 65 20 66 72 65 65 6c 69 73  that the freelis
159b3 74 20 69 73 20 76 61 6c 69 64 2c 20 74 68 65 72  t is valid, ther
159b4 65 0a 20 20 2a 2a 20 69 73 20 6e 6f 20 77 61 79  e.  ** is no way
159b5 20 74 68 61 74 20 74 68 65 20 61 6c 6c 6f 63 61   that the alloca
159b6 74 69 6f 6e 20 63 61 6e 20 65 78 74 65 6e 64 20  tion can extend 
159b7 6f 66 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74  off the end of t
159b8 68 65 20 70 61 67 65 2e 0a 20 20 2a 2a 20 54 68  he page..  ** Th
159b9 65 20 61 73 73 65 72 74 28 29 20 62 65 6c 6f 77  e assert() below
159ba 20 76 65 72 69 66 69 65 73 20 74 68 65 20 70 72   verifies the pr
159bb 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 2e  evious sentence.
159bc 0a 20 20 2a 2f 0a 20 20 74 6f 70 20 2d 3d 20 6e  .  */.  top -= n
159bd 42 79 74 65 3b 0a 20 20 70 75 74 32 62 79 74 65  Byte;.  put2byte
159be 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 74  (&data[hdr+5], t
159bf 6f 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 74  op);.  assert( t
159c0 6f 70 2b 6e 42 79 74 65 20 3c 3d 20 70 50 61 67  op+nByte <= pPag
159c1 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  e->pBt->usableSi
159c2 7a 65 20 29 3b 0a 20 20 2a 70 49 64 78 20 3d 20  ze );.  *pIdx = 
159c3 74 6f 70 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  top;.  return SQ
159c4 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
159c5 2a 20 52 65 74 75 72 6e 20 61 20 73 65 63 74 69  * Return a secti
159c6 6f 6e 20 6f 66 20 74 68 65 20 70 50 61 67 65 2d  on of the pPage-
159c7 3e 61 44 61 74 61 20 74 6f 20 74 68 65 20 66 72  >aData to the fr
159c8 65 65 6c 69 73 74 2e 0a 2a 2a 20 54 68 65 20 66  eelist..** The f
159c9 69 72 73 74 20 62 79 74 65 20 6f 66 20 74 68 65  irst byte of the
159ca 20 6e 65 77 20 66 72 65 65 20 62 6c 6f 63 6b 20   new free block 
159cb 69 73 20 70 50 61 67 65 2d 3e 61 44 69 73 6b 5b  is pPage->aDisk[
159cc 73 74 61 72 74 5d 0a 2a 2a 20 61 6e 64 20 74 68  start].** and th
159cd 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 6c  e size of the bl
159ce 6f 63 6b 20 69 73 20 22 73 69 7a 65 22 20 62 79  ock is "size" by
159cf 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20  tes..**.** Most 
159d0 6f 66 20 74 68 65 20 65 66 66 6f 72 74 20 68 65  of the effort he
159d1 72 65 20 69 73 20 69 6e 76 6f 6c 76 65 64 20 69  re is involved i
159d2 6e 20 63 6f 61 6c 65 73 69 6e 67 20 61 64 6a 61  n coalesing adja
159d3 63 65 6e 74 0a 2a 2a 20 66 72 65 65 20 62 6c 6f  cent.** free blo
159d4 63 6b 73 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c  cks into a singl
159d5 65 20 62 69 67 20 66 72 65 65 20 62 6c 6f 63 6b  e big free block
159d6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
159d7 66 72 65 65 53 70 61 63 65 28 4d 65 6d 50 61 67  freeSpace(MemPag
159d8 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 73 74  e *pPage, int st
159d9 61 72 74 2c 20 69 6e 74 20 73 69 7a 65 29 7b 0a  art, int size){.
159da 20 20 69 6e 74 20 61 64 64 72 2c 20 70 62 65 67    int addr, pbeg
159db 69 6e 2c 20 68 64 72 3b 0a 20 20 69 6e 74 20 69  in, hdr;.  int i
159dc 4c 61 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  Last;           
159dd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
159de 4c 61 72 67 65 73 74 20 70 6f 73 73 69 62 6c 65  Largest possible
159df 20 66 72 65 65 62 6c 6f 63 6b 20 6f 66 66 73 65   freeblock offse
159e0 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  t */.  unsigned 
159e1 63 68 61 72 20 2a 64 61 74 61 20 3d 20 70 50 61  char *data = pPa
159e2 67 65 2d 3e 61 44 61 74 61 3b 0a 0a 20 20 61 73  ge->aData;..  as
159e3 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74  sert( pPage->pBt
159e4 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
159e5 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
159e6 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e  riteable(pPage->
159e7 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73  pDbPage) );.  as
159e8 73 65 72 74 28 20 73 74 61 72 74 3e 3d 70 50 61  sert( start>=pPa
159e9 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 36 2b  ge->hdrOffset+6+
159ea 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72 53  pPage->childPtrS
159eb 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ize );.  assert(
159ec 20 28 73 74 61 72 74 20 2b 20 73 69 7a 65 29 3c   (start + size)<
159ed 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61  =pPage->pBt->usa
159ee 62 6c 65 53 69 7a 65 20 29 3b 0a 20 20 61 73 73  bleSize );.  ass
159ef 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
159f0 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
159f1 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
159f2 61 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 30 20  assert( size>=0 
159f3 29 3b 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20  );   /* Minimum 
159f4 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a  cell size is 4 *
159f5 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
159f6 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20  _SECURE_DELETE. 
159f7 20 2f 2a 20 4f 76 65 72 77 72 69 74 65 20 64 65   /* Overwrite de
159f8 6c 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f  leted informatio
159f9 6e 20 77 69 74 68 20 7a 65 72 6f 73 20 77 68 65  n with zeros whe
159fa 6e 20 74 68 65 20 53 45 43 55 52 45 5f 44 45 4c  n the SECURE_DEL
159fb 45 54 45 20 0a 20 20 2a 2a 20 6f 70 74 69 6f 6e  ETE .  ** option
159fc 20 69 73 20 65 6e 61 62 6c 65 64 20 61 74 20 63   is enabled at c
159fd 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 2a 2f 0a 20  ompile-time */. 
159fe 20 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 73 74   memset(&data[st
159ff 61 72 74 5d 2c 20 30 2c 20 73 69 7a 65 29 3b 0a  art], 0, size);.
15a00 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 64 64  #endif..  /* Add
15a01 20 74 68 65 20 73 70 61 63 65 20 62 61 63 6b 20   the space back 
15a02 69 6e 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20  into the linked 
15a03 6c 69 73 74 20 6f 66 20 66 72 65 65 62 6c 6f 63  list of freebloc
15a04 6b 73 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a 20  ks.  Note that. 
15a05 20 2a 2a 20 65 76 65 6e 20 74 68 6f 75 67 68 20   ** even though 
15a06 74 68 65 20 66 72 65 65 62 6c 6f 63 6b 20 6c 69  the freeblock li
15a07 73 74 20 77 61 73 20 63 68 65 63 6b 65 64 20 62  st was checked b
15a08 79 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28  y btreeInitPage(
15a09 29 2c 0a 20 20 2a 2a 20 62 74 72 65 65 49 6e 69  ),.  ** btreeIni
15a0a 74 50 61 67 65 28 29 20 64 69 64 20 6e 6f 74 20  tPage() did not 
15a0b 64 65 74 65 63 74 20 6f 76 65 72 6c 61 70 70 69  detect overlappi
15a0c 6e 67 20 63 65 6c 6c 73 20 6f 72 0a 20 20 2a 2a  ng cells or.  **
15a0d 20 66 72 65 65 62 6c 6f 63 6b 73 20 74 68 61 74   freeblocks that
15a0e 20 6f 76 65 72 6c 61 70 70 65 64 20 63 65 6c 6c   overlapped cell
15a0f 73 2e 20 20 20 4e 6f 72 20 64 6f 65 73 20 69 74  s.   Nor does it
15a10 20 64 65 74 65 63 74 20 77 68 65 6e 20 74 68 65   detect when the
15a11 0a 20 20 2a 2a 20 63 65 6c 6c 20 63 6f 6e 74 65  .  ** cell conte
15a12 6e 74 20 61 72 65 61 20 65 78 63 65 65 64 73 20  nt area exceeds 
15a13 74 68 65 20 76 61 6c 75 65 20 69 6e 20 74 68 65  the value in the
15a14 20 70 61 67 65 20 68 65 61 64 65 72 2e 20 20 49   page header.  I
15a15 66 20 74 68 65 73 65 0a 20 20 2a 2a 20 73 69 74  f these.  ** sit
15a16 75 61 74 69 6f 6e 73 20 61 72 69 73 65 2c 20 74  uations arise, t
15a17 68 65 6e 20 73 75 62 73 65 71 75 65 6e 74 20 69  hen subsequent i
15a18 6e 73 65 72 74 20 6f 70 65 72 61 74 69 6f 6e 73  nsert operations
15a19 20 6d 69 67 68 74 20 63 6f 72 72 75 70 74 0a 20   might corrupt. 
15a1a 20 2a 2a 20 74 68 65 20 66 72 65 65 6c 69 73 74   ** the freelist
15a1b 2e 20 20 53 6f 20 77 65 20 64 6f 20 6e 65 65 64  .  So we do need
15a1c 20 74 6f 20 63 68 65 63 6b 20 66 6f 72 20 63 6f   to check for co
15a1d 72 72 75 70 74 69 6f 6e 20 77 68 69 6c 65 20 73  rruption while s
15a1e 63 61 6e 6e 69 6e 67 0a 20 20 2a 2a 20 74 68 65  canning.  ** the
15a1f 20 66 72 65 65 6c 69 73 74 2e 0a 20 20 2a 2f 0a   freelist..  */.
15a20 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68    hdr = pPage->h
15a21 64 72 4f 66 66 73 65 74 3b 0a 20 20 61 64 64 72  drOffset;.  addr
15a22 20 3d 20 68 64 72 20 2b 20 31 3b 0a 20 20 69 4c   = hdr + 1;.  iL
15a23 61 73 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74  ast = pPage->pBt
15a24 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34  ->usableSize - 4
15a25 3b 0a 20 20 61 73 73 65 72 74 28 20 73 74 61 72  ;.  assert( star
15a26 74 3c 3d 69 4c 61 73 74 20 29 3b 0a 20 20 77 68  t<=iLast );.  wh
15a27 69 6c 65 28 20 28 70 62 65 67 69 6e 20 3d 20 67  ile( (pbegin = g
15a28 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 61 64  et2byte(&data[ad
15a29 64 72 5d 29 29 3c 73 74 61 72 74 20 26 26 20 70  dr]))<start && p
15a2a 62 65 67 69 6e 3e 30 20 29 7b 0a 20 20 20 20 69  begin>0 ){.    i
15a2b 66 28 20 70 62 65 67 69 6e 3c 61 64 64 72 2b 34  f( pbegin<addr+4
15a2c 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
15a2d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
15a2e 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20  BKPT;.    }.    
15a2f 61 64 64 72 20 3d 20 70 62 65 67 69 6e 3b 0a 20  addr = pbegin;. 
15a30 20 7d 0a 20 20 69 66 28 20 70 62 65 67 69 6e 3e   }.  if( pbegin>
15a31 69 4c 61 73 74 20 29 7b 0a 20 20 20 20 72 65 74  iLast ){.    ret
15a32 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
15a33 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 61  PT_BKPT;.  }.  a
15a34 73 73 65 72 74 28 20 70 62 65 67 69 6e 3e 61 64  ssert( pbegin>ad
15a35 64 72 20 7c 7c 20 70 62 65 67 69 6e 3d 3d 30 20  dr || pbegin==0 
15a36 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64  );.  put2byte(&d
15a37 61 74 61 5b 61 64 64 72 5d 2c 20 73 74 61 72 74  ata[addr], start
15a38 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64  );.  put2byte(&d
15a39 61 74 61 5b 73 74 61 72 74 5d 2c 20 70 62 65 67  ata[start], pbeg
15a3a 69 6e 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28  in);.  put2byte(
15a3b 26 64 61 74 61 5b 73 74 61 72 74 2b 32 5d 2c 20  &data[start+2], 
15a3c 73 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 2d 3e  size);.  pPage->
15a3d 6e 46 72 65 65 20 3d 20 70 50 61 67 65 2d 3e 6e  nFree = pPage->n
15a3e 46 72 65 65 20 2b 20 28 75 31 36 29 73 69 7a 65  Free + (u16)size
15a3f 3b 0a 0a 20 20 2f 2a 20 43 6f 61 6c 65 73 63 65  ;..  /* Coalesce
15a40 20 61 64 6a 61 63 65 6e 74 20 66 72 65 65 20 62   adjacent free b
15a41 6c 6f 63 6b 73 20 2a 2f 0a 20 20 61 64 64 72 20  locks */.  addr 
15a42 3d 20 68 64 72 20 2b 20 31 3b 0a 20 20 77 68 69  = hdr + 1;.  whi
15a43 6c 65 28 20 28 70 62 65 67 69 6e 20 3d 20 67 65  le( (pbegin = ge
15a44 74 32 62 79 74 65 28 26 64 61 74 61 5b 61 64 64  t2byte(&data[add
15a45 72 5d 29 29 3e 30 20 29 7b 0a 20 20 20 20 69 6e  r]))>0 ){.    in
15a46 74 20 70 6e 65 78 74 2c 20 70 73 69 7a 65 2c 20  t pnext, psize, 
15a47 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  x;.    assert( p
15a48 62 65 67 69 6e 3e 61 64 64 72 20 29 3b 0a 20 20  begin>addr );.  
15a49 20 20 61 73 73 65 72 74 28 20 70 62 65 67 69 6e    assert( pbegin
15a4a 3c 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73  <=pPage->pBt->us
15a4b 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b 0a 20 20  ableSize-4 );.  
15a4c 20 20 70 6e 65 78 74 20 3d 20 67 65 74 32 62 79    pnext = get2by
15a4d 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 5d  te(&data[pbegin]
15a4e 29 3b 0a 20 20 20 20 70 73 69 7a 65 20 3d 20 67  );.    psize = g
15a4f 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 62  et2byte(&data[pb
15a50 65 67 69 6e 2b 32 5d 29 3b 0a 20 20 20 20 69 66  egin+2]);.    if
15a51 28 20 70 62 65 67 69 6e 20 2b 20 70 73 69 7a 65  ( pbegin + psize
15a52 20 2b 20 33 20 3e 3d 20 70 6e 65 78 74 20 26 26   + 3 >= pnext &&
15a53 20 70 6e 65 78 74 3e 30 20 29 7b 0a 20 20 20 20   pnext>0 ){.    
15a54 20 20 69 6e 74 20 66 72 61 67 20 3d 20 70 6e 65    int frag = pne
15a55 78 74 20 2d 20 28 70 62 65 67 69 6e 2b 70 73 69  xt - (pbegin+psi
15a56 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28  ze);.      if( (
15a57 66 72 61 67 3c 30 29 20 7c 7c 20 28 66 72 61 67  frag<0) || (frag
15a58 3e 28 69 6e 74 29 64 61 74 61 5b 68 64 72 2b 37  >(int)data[hdr+7
15a59 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  ]) ){.        re
15a5a 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
15a5b 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20  UPT_BKPT;.      
15a5c 7d 0a 20 20 20 20 20 20 64 61 74 61 5b 68 64 72  }.      data[hdr
15a5d 2b 37 5d 20 2d 3d 20 28 75 38 29 66 72 61 67 3b  +7] -= (u8)frag;
15a5e 0a 20 20 20 20 20 20 78 20 3d 20 67 65 74 32 62  .      x = get2b
15a5f 79 74 65 28 26 64 61 74 61 5b 70 6e 65 78 74 5d  yte(&data[pnext]
15a60 29 3b 0a 20 20 20 20 20 20 70 75 74 32 62 79 74  );.      put2byt
15a61 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 5d 2c  e(&data[pbegin],
15a62 20 78 29 3b 0a 20 20 20 20 20 20 78 20 3d 20 70   x);.      x = p
15a63 6e 65 78 74 20 2b 20 67 65 74 32 62 79 74 65 28  next + get2byte(
15a64 26 64 61 74 61 5b 70 6e 65 78 74 2b 32 5d 29 20  &data[pnext+2]) 
15a65 2d 20 70 62 65 67 69 6e 3b 0a 20 20 20 20 20 20  - pbegin;.      
15a66 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  put2byte(&data[p
15a67 62 65 67 69 6e 2b 32 5d 2c 20 78 29 3b 0a 20 20  begin+2], x);.  
15a68 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
15a69 64 64 72 20 3d 20 70 62 65 67 69 6e 3b 0a 20 20  ddr = pbegin;.  
15a6a 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66    }.  }..  /* If
15a6b 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
15a6c 74 20 61 72 65 61 20 62 65 67 69 6e 73 20 77 69  t area begins wi
15a6d 74 68 20 61 20 66 72 65 65 62 6c 6f 63 6b 2c 20  th a freeblock, 
15a6e 72 65 6d 6f 76 65 20 69 74 2e 20 2a 2f 0a 20 20  remove it. */.  
15a6f 69 66 28 20 64 61 74 61 5b 68 64 72 2b 31 5d 3d  if( data[hdr+1]=
15a70 3d 64 61 74 61 5b 68 64 72 2b 35 5d 20 26 26 20  =data[hdr+5] && 
15a71 64 61 74 61 5b 68 64 72 2b 32 5d 3d 3d 64 61 74  data[hdr+2]==dat
15a72 61 5b 68 64 72 2b 36 5d 20 29 7b 0a 20 20 20 20  a[hdr+6] ){.    
15a73 69 6e 74 20 74 6f 70 3b 0a 20 20 20 20 70 62 65  int top;.    pbe
15a74 67 69 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26  gin = get2byte(&
15a75 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20 20  data[hdr+1]);.  
15a76 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 68    memcpy(&data[h
15a77 64 72 2b 31 5d 2c 20 26 64 61 74 61 5b 70 62 65  dr+1], &data[pbe
15a78 67 69 6e 5d 2c 20 32 29 3b 0a 20 20 20 20 74 6f  gin], 2);.    to
15a79 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  p = get2byte(&da
15a7a 74 61 5b 68 64 72 2b 35 5d 29 20 2b 20 67 65 74  ta[hdr+5]) + get
15a7b 32 62 79 74 65 28 26 64 61 74 61 5b 70 62 65 67  2byte(&data[pbeg
15a7c 69 6e 2b 32 5d 29 3b 0a 20 20 20 20 70 75 74 32  in+2]);.    put2
15a7d 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
15a7e 5d 2c 20 74 6f 70 29 3b 0a 20 20 7d 0a 20 20 61  ], top);.  }.  a
15a7f 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
15a80 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
15a81 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
15a82 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
15a83 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  E_OK;.}../*.** D
15a84 65 63 6f 64 65 20 74 68 65 20 66 6c 61 67 73 20  ecode the flags 
15a85 62 79 74 65 20 28 74 68 65 20 66 69 72 73 74 20  byte (the first 
15a86 62 79 74 65 20 6f 66 20 74 68 65 20 68 65 61 64  byte of the head
15a87 65 72 29 20 66 6f 72 20 61 20 70 61 67 65 0a 2a  er) for a page.*
15a88 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65  * and initialize
15a89 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 4d   fields of the M
15a8a 65 6d 50 61 67 65 20 73 74 72 75 63 74 75 72 65  emPage structure
15a8b 20 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a   accordingly..**
15a8c 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 66 6f 6c  .** Only the fol
15a8d 6c 6f 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69  lowing combinati
15a8e 6f 6e 73 20 61 72 65 20 73 75 70 70 6f 72 74 65  ons are supporte
15a8f 64 2e 20 20 41 6e 79 74 68 69 6e 67 20 64 69 66  d.  Anything dif
15a90 66 65 72 65 6e 74 0a 2a 2a 20 69 6e 64 69 63 61  ferent.** indica
15a91 74 65 73 20 61 20 63 6f 72 72 75 70 74 20 64 61  tes a corrupt da
15a92 74 61 62 61 73 65 20 66 69 6c 65 73 3a 0a 2a 2a  tabase files:.**
15a93 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f  .**         PTF_
15a94 5a 45 52 4f 44 41 54 41 0a 2a 2a 20 20 20 20 20  ZERODATA.**     
15a95 20 20 20 20 50 54 46 5f 5a 45 52 4f 44 41 54 41      PTF_ZERODATA
15a96 20 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2a 20 20   | PTF_LEAF.**  
15a97 20 20 20 20 20 20 20 50 54 46 5f 4c 45 41 46 44         PTF_LEAFD
15a98 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59  ATA | PTF_INTKEY
15a99 0a 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f  .**         PTF_
15a9a 4c 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49  LEAFDATA | PTF_I
15a9b 4e 54 4b 45 59 20 7c 20 50 54 46 5f 4c 45 41 46  NTKEY | PTF_LEAF
15a9c 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
15a9d 65 63 6f 64 65 46 6c 61 67 73 28 4d 65 6d 50 61  ecodeFlags(MemPa
15a9e 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66  ge *pPage, int f
15a9f 6c 61 67 42 79 74 65 29 7b 0a 20 20 42 74 53 68  lagByte){.  BtSh
15aa0 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 2f  ared *pBt;     /
15aa1 2a 20 41 20 63 6f 70 79 20 6f 66 20 70 50 61 67  * A copy of pPag
15aa2 65 2d 3e 70 42 74 20 2a 2f 0a 0a 20 20 61 73 73  e->pBt */..  ass
15aa3 65 72 74 28 20 70 50 61 67 65 2d 3e 68 64 72 4f  ert( pPage->hdrO
15aa4 66 66 73 65 74 3d 3d 28 70 50 61 67 65 2d 3e 70  ffset==(pPage->p
15aa5 67 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30  gno==1 ? 100 : 0
15aa6 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
15aa7 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
15aa8 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
15aa9 74 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65 2d  tex) );.  pPage-
15aaa 3e 6c 65 61 66 20 3d 20 28 75 38 29 28 66 6c 61  >leaf = (u8)(fla
15aab 67 42 79 74 65 3e 3e 33 29 3b 20 20 61 73 73 65  gByte>>3);  asse
15aac 72 74 28 20 50 54 46 5f 4c 45 41 46 20 3d 3d 20  rt( PTF_LEAF == 
15aad 31 3c 3c 33 20 29 3b 0a 20 20 66 6c 61 67 42 79  1<<3 );.  flagBy
15aae 74 65 20 26 3d 20 7e 50 54 46 5f 4c 45 41 46 3b  te &= ~PTF_LEAF;
15aaf 0a 20 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50  .  pPage->childP
15ab0 74 72 53 69 7a 65 20 3d 20 34 2d 34 2a 70 50 61  trSize = 4-4*pPa
15ab1 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 70 42 74 20  ge->leaf;.  pBt 
15ab2 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20  = pPage->pBt;.  
15ab3 69 66 28 20 66 6c 61 67 42 79 74 65 3d 3d 28 50  if( flagByte==(P
15ab4 54 46 5f 4c 45 41 46 44 41 54 41 20 7c 20 50 54  TF_LEAFDATA | PT
15ab5 46 5f 49 4e 54 4b 45 59 29 20 29 7b 0a 20 20 20  F_INTKEY) ){.   
15ab6 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d   pPage->intKey =
15ab7 20 31 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 68   1;.    pPage->h
15ab8 61 73 44 61 74 61 20 3d 20 70 50 61 67 65 2d 3e  asData = pPage->
15ab9 6c 65 61 66 3b 0a 20 20 20 20 70 50 61 67 65 2d  leaf;.    pPage-
15aba 3e 6d 61 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d  >maxLocal = pBt-
15abb 3e 6d 61 78 4c 65 61 66 3b 0a 20 20 20 20 70 50  >maxLeaf;.    pP
15abc 61 67 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20  age->minLocal = 
15abd 70 42 74 2d 3e 6d 69 6e 4c 65 61 66 3b 0a 20 20  pBt->minLeaf;.  
15abe 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 42 79  }else if( flagBy
15abf 74 65 3d 3d 50 54 46 5f 5a 45 52 4f 44 41 54 41  te==PTF_ZERODATA
15ac0 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69   ){.    pPage->i
15ac1 6e 74 4b 65 79 20 3d 20 30 3b 0a 20 20 20 20 70  ntKey = 0;.    p
15ac2 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 3d 20  Page->hasData = 
15ac3 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 61  0;.    pPage->ma
15ac4 78 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 61  xLocal = pBt->ma
15ac5 78 4c 6f 63 61 6c 3b 0a 20 20 20 20 70 50 61 67  xLocal;.    pPag
15ac6 65 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 70 42  e->minLocal = pB
15ac7 74 2d 3e 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 7d  t->minLocal;.  }
15ac8 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
15ac9 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
15aca 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75  BKPT;.  }.  retu
15acb 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
15acc 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  ./*.** Initializ
15acd 65 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20  e the auxiliary 
15ace 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20  information for 
15acf 61 20 64 69 73 6b 20 62 6c 6f 63 6b 2e 0a 2a 2a  a disk block..**
15ad0 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
15ad1 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e  E_OK on success.
15ad2 20 20 49 66 20 77 65 20 73 65 65 20 74 68 61 74    If we see that
15ad3 20 74 68 65 20 70 61 67 65 20 64 6f 65 73 0a 2a   the page does.*
15ad4 2a 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20  * not contain a 
15ad5 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64 61 74 61  well-formed data
15ad6 62 61 73 65 20 70 61 67 65 2c 20 74 68 65 6e 20  base page, then 
15ad7 72 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54  return .** SQLIT
15ad8 45 5f 43 4f 52 52 55 50 54 2e 20 20 4e 6f 74 65  E_CORRUPT.  Note
15ad9 20 74 68 61 74 20 61 20 72 65 74 75 72 6e 20 6f   that a return o
15ada 66 20 53 51 4c 49 54 45 5f 4f 4b 20 64 6f 65 73  f SQLITE_OK does
15adb 20 6e 6f 74 0a 2a 2a 20 67 75 61 72 61 6e 74 65   not.** guarante
15adc 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20  e that the page 
15add 69 73 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 2e 20  is well-formed. 
15ade 20 49 74 20 6f 6e 6c 79 20 73 68 6f 77 73 20 74   It only shows t
15adf 68 61 74 0a 2a 2a 20 77 65 20 66 61 69 6c 65 64  hat.** we failed
15ae0 20 74 6f 20 64 65 74 65 63 74 20 61 6e 79 20 63   to detect any c
15ae1 6f 72 72 75 70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74  orruption..*/.st
15ae2 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 49 6e  atic int btreeIn
15ae3 69 74 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a  itPage(MemPage *
15ae4 70 50 61 67 65 29 7b 0a 0a 20 20 61 73 73 65 72  pPage){..  asser
15ae5 74 28 20 70 50 61 67 65 2d 3e 70 42 74 21 3d 30  t( pPage->pBt!=0
15ae6 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
15ae7 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
15ae8 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74  (pPage->pBt->mut
15ae9 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
15aea 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d 73 71   pPage->pgno==sq
15aeb 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 6e 75  lite3PagerPagenu
15aec 6d 62 65 72 28 70 50 61 67 65 2d 3e 70 44 62 50  mber(pPage->pDbP
15aed 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74  age) );.  assert
15aee 28 20 70 50 61 67 65 20 3d 3d 20 73 71 6c 69 74  ( pPage == sqlit
15aef 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28  e3PagerGetExtra(
15af0 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
15af1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
15af2 67 65 2d 3e 61 44 61 74 61 20 3d 3d 20 73 71 6c  ge->aData == sql
15af3 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61  ite3PagerGetData
15af4 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
15af5 20 29 3b 0a 0a 20 20 69 66 28 20 21 70 50 61 67   );..  if( !pPag
15af6 65 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20 20  e->isInit ){.   
15af7 20 75 31 36 20 70 63 3b 20 20 20 20 20 20 20 20   u16 pc;        
15af8 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
15af9 66 20 61 20 66 72 65 65 62 6c 6f 63 6b 20 77 69  f a freeblock wi
15afa 74 68 69 6e 20 70 50 61 67 65 2d 3e 61 44 61 74  thin pPage->aDat
15afb 61 5b 5d 20 2a 2f 0a 20 20 20 20 75 38 20 68 64  a[] */.    u8 hd
15afc 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  r;            /*
15afd 20 4f 66 66 73 65 74 20 74 6f 20 62 65 67 69 6e   Offset to begin
15afe 6e 69 6e 67 20 6f 66 20 70 61 67 65 20 68 65 61  ning of page hea
15aff 64 65 72 20 2a 2f 0a 20 20 20 20 75 38 20 2a 64  der */.    u8 *d
15b00 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ata;          /*
15b01 20 45 71 75 61 6c 20 74 6f 20 70 50 61 67 65 2d   Equal to pPage-
15b02 3e 61 44 61 74 61 20 2a 2f 0a 20 20 20 20 42 74  >aData */.    Bt
15b03 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20  Shared *pBt;    
15b04 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20      /* The main 
15b05 62 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20  btree structure 
15b06 2a 2f 0a 20 20 20 20 75 31 36 20 75 73 61 62 6c  */.    u16 usabl
15b07 65 53 69 7a 65 3b 20 20 20 20 2f 2a 20 41 6d 6f  eSize;    /* Amo
15b08 75 6e 74 20 6f 66 20 75 73 61 62 6c 65 20 73 70  unt of usable sp
15b09 61 63 65 20 6f 6e 20 65 61 63 68 20 70 61 67 65  ace on each page
15b0a 20 2a 2f 0a 20 20 20 20 75 31 36 20 63 65 6c 6c   */.    u16 cell
15b0b 4f 66 66 73 65 74 3b 20 20 20 20 2f 2a 20 4f 66  Offset;    /* Of
15b0c 66 73 65 74 20 66 72 6f 6d 20 73 74 61 72 74 20  fset from start 
15b0d 6f 66 20 70 61 67 65 20 74 6f 20 66 69 72 73 74  of page to first
15b0e 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 2a 2f   cell pointer */
15b0f 0a 20 20 20 20 75 31 36 20 6e 46 72 65 65 3b 20  .    u16 nFree; 
15b10 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
15b11 72 20 6f 66 20 75 6e 75 73 65 64 20 62 79 74 65  r of unused byte
15b12 73 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f  s on the page */
15b13 0a 20 20 20 20 75 31 36 20 74 6f 70 3b 20 20 20  .    u16 top;   
15b14 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
15b15 20 62 79 74 65 20 6f 66 20 74 68 65 20 63 65 6c   byte of the cel
15b16 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a  l content area *
15b17 2f 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 46  /.    int iCellF
15b18 69 72 73 74 3b 20 20 20 20 2f 2a 20 46 69 72 73  irst;    /* Firs
15b19 74 20 61 6c 6c 6f 77 61 62 6c 65 20 63 65 6c 6c  t allowable cell
15b1a 20 6f 72 20 66 72 65 65 62 6c 6f 63 6b 20 6f 66   or freeblock of
15b1b 66 73 65 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20  fset */.    int 
15b1c 69 43 65 6c 6c 4c 61 73 74 3b 20 20 20 20 20 2f  iCellLast;     /
15b1d 2a 20 4c 61 73 74 20 70 6f 73 73 69 62 6c 65 20  * Last possible 
15b1e 63 65 6c 6c 20 6f 72 20 66 72 65 65 62 6c 6f 63  cell or freebloc
15b1f 6b 20 6f 66 66 73 65 74 20 2a 2f 0a 0a 20 20 20  k offset */..   
15b20 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42   pBt = pPage->pB
15b21 74 3b 0a 0a 20 20 20 20 68 64 72 20 3d 20 70 50  t;..    hdr = pP
15b22 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a  age->hdrOffset;.
15b23 20 20 20 20 64 61 74 61 20 3d 20 70 50 61 67 65      data = pPage
15b24 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 69 66 28  ->aData;.    if(
15b25 20 64 65 63 6f 64 65 46 6c 61 67 73 28 70 50 61   decodeFlags(pPa
15b26 67 65 2c 20 64 61 74 61 5b 68 64 72 5d 29 20 29  ge, data[hdr]) )
15b27 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
15b28 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
15b29 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 61   assert( pBt->pa
15b2a 67 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70  geSize>=512 && p
15b2b 42 74 2d 3e 70 61 67 65 53 69 7a 65 3c 3d 33 32  Bt->pageSize<=32
15b2c 37 36 38 20 29 3b 0a 20 20 20 20 70 50 61 67 65  768 );.    pPage
15b2d 2d 3e 6d 61 73 6b 50 61 67 65 20 3d 20 70 42 74  ->maskPage = pBt
15b2e 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 31 3b 0a  ->pageSize - 1;.
15b2f 20 20 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72      pPage->nOver
15b30 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20 75 73  flow = 0;.    us
15b31 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e  ableSize = pBt->
15b32 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20  usableSize;.    
15b33 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65  pPage->cellOffse
15b34 74 20 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d  t = cellOffset =
15b35 20 68 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50   hdr + 12 - 4*pP
15b36 61 67 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 74  age->leaf;.    t
15b37 6f 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  op = get2byte(&d
15b38 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20  ata[hdr+5]);.   
15b39 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20   pPage->nCell = 
15b3a 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 68  get2byte(&data[h
15b3b 64 72 2b 33 5d 29 3b 0a 20 20 20 20 69 66 28 20  dr+3]);.    if( 
15b3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f  pPage->nCell>MX_
15b3d 43 45 4c 4c 28 70 42 74 29 20 29 7b 0a 20 20 20  CELL(pBt) ){.   
15b3e 20 20 20 2f 2a 20 54 6f 20 6d 61 6e 79 20 63 65     /* To many ce
15b3f 6c 6c 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65  lls for a single
15b40 20 70 61 67 65 2e 20 20 54 68 65 20 70 61 67 65   page.  The page
15b41 20 6d 75 73 74 20 62 65 20 63 6f 72 72 75 70 74   must be corrupt
15b42 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e   */.      return
15b43 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
15b44 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20  BKPT;.    }.    
15b45 74 65 73 74 63 61 73 65 28 20 70 50 61 67 65 2d  testcase( pPage-
15b46 3e 6e 43 65 6c 6c 3d 3d 4d 58 5f 43 45 4c 4c 28  >nCell==MX_CELL(
15b47 70 42 74 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20  pBt) );..    /* 
15b48 41 20 6d 61 6c 66 6f 72 6d 65 64 20 64 61 74 61  A malformed data
15b49 62 61 73 65 20 70 61 67 65 20 6d 69 67 68 74 20  base page might 
15b4a 63 61 75 73 65 20 75 73 20 74 6f 20 72 65 61 64  cause us to read
15b4b 20 70 61 73 74 20 74 68 65 20 65 6e 64 0a 20 20   past the end.  
15b4c 20 20 2a 2a 20 6f 66 20 70 61 67 65 20 77 68 65    ** of page whe
15b4d 6e 20 70 61 72 73 69 6e 67 20 61 20 63 65 6c 6c  n parsing a cell
15b4e 2e 20 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  .  .    **.    *
15b4f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
15b50 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 63 68  block of code ch
15b51 65 63 6b 73 20 65 61 72 6c 79 20 74 6f 20 73 65  ecks early to se
15b52 65 20 69 66 20 61 20 63 65 6c 6c 20 65 78 74 65  e if a cell exte
15b53 6e 64 73 0a 20 20 20 20 2a 2a 20 70 61 73 74 20  nds.    ** past 
15b54 74 68 65 20 65 6e 64 20 6f 66 20 61 20 70 61 67  the end of a pag
15b55 65 20 62 6f 75 6e 64 61 72 79 20 61 6e 64 20 63  e boundary and c
15b56 61 75 73 65 73 20 53 51 4c 49 54 45 5f 43 4f 52  auses SQLITE_COR
15b57 52 55 50 54 20 74 6f 20 62 65 20 0a 20 20 20 20  RUPT to be .    
15b58 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 66 20 69  ** returned if i
15b59 74 20 64 6f 65 73 2e 0a 20 20 20 20 2a 2f 0a 20  t does..    */. 
15b5a 20 20 20 69 43 65 6c 6c 46 69 72 73 74 20 3d 20     iCellFirst = 
15b5b 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 70  cellOffset + 2*p
15b5c 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20  Page->nCell;.   
15b5d 20 69 43 65 6c 6c 4c 61 73 74 20 3d 20 75 73 61   iCellLast = usa
15b5e 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a 23 69 66  bleSize - 4;.#if
15b5f 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
15b60 45 4e 41 42 4c 45 5f 4f 56 45 52 53 49 5a 45 5f  ENABLE_OVERSIZE_
15b61 43 45 4c 4c 5f 43 48 45 43 4b 29 0a 20 20 20 20  CELL_CHECK).    
15b62 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20  {.      int i;  
15b63 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
15b64 65 78 20 69 6e 74 6f 20 74 68 65 20 63 65 6c 6c  ex into the cell
15b65 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 2a   pointer array *
15b66 2f 0a 20 20 20 20 20 20 69 6e 74 20 73 7a 3b 20  /.      int sz; 
15b67 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
15b68 65 20 6f 66 20 61 20 63 65 6c 6c 20 2a 2f 0a 0a  e of a cell */..
15b69 20 20 20 20 20 20 69 66 28 20 21 70 50 61 67 65        if( !pPage
15b6a 2d 3e 6c 65 61 66 20 29 20 69 43 65 6c 6c 4c 61  ->leaf ) iCellLa
15b6b 73 74 2d 2d 3b 0a 20 20 20 20 20 20 66 6f 72 28  st--;.      for(
15b6c 69 3d 30 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43  i=0; i<pPage->nC
15b6d 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  ell; i++){.     
15b6e 20 20 20 70 63 20 3d 20 67 65 74 32 62 79 74 65     pc = get2byte
15b6f 28 26 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65  (&data[cellOffse
15b70 74 2b 69 2a 32 5d 29 3b 0a 20 20 20 20 20 20 20  t+i*2]);.       
15b71 20 74 65 73 74 63 61 73 65 28 20 70 63 3d 3d 69   testcase( pc==i
15b72 43 65 6c 6c 46 69 72 73 74 20 29 3b 0a 20 20 20  CellFirst );.   
15b73 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
15b74 63 3d 3d 69 43 65 6c 6c 4c 61 73 74 20 29 3b 0a  c==iCellLast );.
15b75 20 20 20 20 20 20 20 20 69 66 28 20 70 63 3c 69          if( pc<i
15b76 43 65 6c 6c 46 69 72 73 74 20 7c 7c 20 70 63 3e  CellFirst || pc>
15b77 69 43 65 6c 6c 4c 61 73 74 20 29 7b 0a 20 20 20  iCellLast ){.   
15b78 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
15b79 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
15b7a 54 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  T;.        }.   
15b7b 20 20 20 20 20 73 7a 20 3d 20 63 65 6c 6c 53 69       sz = cellSi
15b7c 7a 65 50 74 72 28 70 50 61 67 65 2c 20 26 64 61  zePtr(pPage, &da
15b7d 74 61 5b 70 63 5d 29 3b 0a 20 20 20 20 20 20 20  ta[pc]);.       
15b7e 20 74 65 73 74 63 61 73 65 28 20 70 63 2b 73 7a   testcase( pc+sz
15b7f 3d 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 3b 0a  ==usableSize );.
15b80 20 20 20 20 20 20 20 20 69 66 28 20 70 63 2b 73          if( pc+s
15b81 7a 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a  z>usableSize ){.
15b82 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
15b83 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
15b84 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 7d 0a  BKPT;.        }.
15b85 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
15b86 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  ( !pPage->leaf )
15b87 20 69 43 65 6c 6c 4c 61 73 74 2b 2b 3b 0a 20 20   iCellLast++;.  
15b88 20 20 7d 20 20 0a 23 65 6e 64 69 66 0a 0a 20 20    }  .#endif..  
15b89 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65    /* Compute the
15b8a 20 74 6f 74 61 6c 20 66 72 65 65 20 73 70 61 63   total free spac
15b8b 65 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f  e on the page */
15b8c 0a 20 20 20 20 70 63 20 3d 20 67 65 74 32 62 79  .    pc = get2by
15b8d 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 29  te(&data[hdr+1])
15b8e 3b 0a 20 20 20 20 6e 46 72 65 65 20 3d 20 64 61  ;.    nFree = da
15b8f 74 61 5b 68 64 72 2b 37 5d 20 2b 20 74 6f 70 3b  ta[hdr+7] + top;
15b90 0a 20 20 20 20 77 68 69 6c 65 28 20 70 63 3e 30  .    while( pc>0
15b91 20 29 7b 0a 20 20 20 20 20 20 75 31 36 20 6e 65   ){.      u16 ne
15b92 78 74 2c 20 73 69 7a 65 3b 0a 20 20 20 20 20 20  xt, size;.      
15b93 69 66 28 20 70 63 3c 69 43 65 6c 6c 46 69 72 73  if( pc<iCellFirs
15b94 74 20 7c 7c 20 70 63 3e 69 43 65 6c 6c 4c 61 73  t || pc>iCellLas
15b95 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  t ){.        /* 
15b96 53 74 61 72 74 20 6f 66 20 66 72 65 65 20 62 6c  Start of free bl
15b97 6f 63 6b 20 69 73 20 6f 66 66 20 74 68 65 20 70  ock is off the p
15b98 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 72  age */.        r
15b99 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
15b9a 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20  RUPT_BKPT; .    
15b9b 20 20 7d 0a 20 20 20 20 20 20 6e 65 78 74 20 3d    }.      next =
15b9c 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
15b9d 70 63 5d 29 3b 0a 20 20 20 20 20 20 73 69 7a 65  pc]);.      size
15b9e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
15b9f 61 5b 70 63 2b 32 5d 29 3b 0a 20 20 20 20 20 20  a[pc+2]);.      
15ba0 69 66 28 20 28 6e 65 78 74 3e 30 20 26 26 20 6e  if( (next>0 && n
15ba1 65 78 74 3c 3d 70 63 2b 73 69 7a 65 2b 33 29 20  ext<=pc+size+3) 
15ba2 7c 7c 20 70 63 2b 73 69 7a 65 3e 75 73 61 62 6c  || pc+size>usabl
15ba3 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20  eSize ){.       
15ba4 20 2f 2a 20 46 72 65 65 20 62 6c 6f 63 6b 73 20   /* Free blocks 
15ba5 6d 75 73 74 20 62 65 20 69 6e 20 61 73 63 65 6e  must be in ascen
15ba6 64 69 6e 67 20 6f 72 64 65 72 2e 20 41 6e 64 20  ding order. And 
15ba7 74 68 65 20 6c 61 73 74 20 62 79 74 65 20 6f 66  the last byte of
15ba8 0a 09 2a 2a 20 74 68 65 20 66 72 65 65 2d 62 6c  ..** the free-bl
15ba9 6f 63 6b 20 6d 75 73 74 20 6c 69 65 20 6f 6e 20  ock must lie on 
15baa 74 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67  the database pag
15bab 65 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72  e.  */.        r
15bac 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
15bad 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20 20 20  RUPT_BKPT; .    
15bae 20 20 7d 0a 20 20 20 20 20 20 6e 46 72 65 65 20    }.      nFree 
15baf 3d 20 6e 46 72 65 65 20 2b 20 73 69 7a 65 3b 0a  = nFree + size;.
15bb0 20 20 20 20 20 20 70 63 20 3d 20 6e 65 78 74 3b        pc = next;
15bb1 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41  .    }..    /* A
15bb2 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 6e 46  t this point, nF
15bb3 72 65 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ree contains the
15bb4 20 73 75 6d 20 6f 66 20 74 68 65 20 6f 66 66 73   sum of the offs
15bb5 65 74 20 74 6f 20 74 68 65 20 73 74 61 72 74 0a  et to the start.
15bb6 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 63 65      ** of the ce
15bb7 6c 6c 2d 63 6f 6e 74 65 6e 74 20 61 72 65 61 20  ll-content area 
15bb8 70 6c 75 73 20 74 68 65 20 6e 75 6d 62 65 72 20  plus the number 
15bb9 6f 66 20 66 72 65 65 20 62 79 74 65 73 20 77 69  of free bytes wi
15bba 74 68 69 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20  thin.    ** the 
15bbb 63 65 6c 6c 2d 63 6f 6e 74 65 6e 74 20 61 72 65  cell-content are
15bbc 61 2e 20 49 66 20 74 68 69 73 20 69 73 20 67 72  a. If this is gr
15bbd 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 75  eater than the u
15bbe 73 61 62 6c 65 2d 73 69 7a 65 0a 20 20 20 20 2a  sable-size.    *
15bbf 2a 20 6f 66 20 74 68 65 20 70 61 67 65 2c 20 74  * of the page, t
15bc0 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d 75 73  hen the page mus
15bc1 74 20 62 65 20 63 6f 72 72 75 70 74 65 64 2e 20  t be corrupted. 
15bc2 54 68 69 73 20 63 68 65 63 6b 20 61 6c 73 6f 0a  This check also.
15bc3 20 20 20 20 2a 2a 20 73 65 72 76 65 73 20 74 6f      ** serves to
15bc4 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68 65   verify that the
15bc5 20 6f 66 66 73 65 74 20 74 6f 20 74 68 65 20 73   offset to the s
15bc6 74 61 72 74 20 6f 66 20 74 68 65 20 63 65 6c 6c  tart of the cell
15bc7 2d 63 6f 6e 74 65 6e 74 0a 20 20 20 20 2a 2a 20  -content.    ** 
15bc8 61 72 65 61 2c 20 61 63 63 6f 72 64 69 6e 67 20  area, according 
15bc9 74 6f 20 74 68 65 20 70 61 67 65 20 68 65 61 64  to the page head
15bca 65 72 2c 20 6c 69 65 73 20 77 69 74 68 69 6e 20  er, lies within 
15bcb 74 68 65 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f  the page..    */
15bcc 0a 20 20 20 20 69 66 28 20 6e 46 72 65 65 3e 75  .    if( nFree>u
15bcd 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20 20  sableSize ){.   
15bce 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
15bcf 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a  _CORRUPT_BKPT; .
15bd0 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 2d      }.    pPage-
15bd1 3e 6e 46 72 65 65 20 3d 20 28 75 31 36 29 28 6e  >nFree = (u16)(n
15bd2 46 72 65 65 20 2d 20 69 43 65 6c 6c 46 69 72 73  Free - iCellFirs
15bd3 74 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69  t);.    pPage->i
15bd4 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 20  sInit = 1;.  }. 
15bd5 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
15bd6 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  K;.}../*.** Set 
15bd7 75 70 20 61 20 72 61 77 20 70 61 67 65 20 73 6f  up a raw page so
15bd8 20 74 68 61 74 20 69 74 20 6c 6f 6f 6b 73 20 6c   that it looks l
15bd9 69 6b 65 20 61 20 64 61 74 61 62 61 73 65 20 70  ike a database p
15bda 61 67 65 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20 6e  age holding.** n
15bdb 6f 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 74  o entries..*/.st
15bdc 61 74 69 63 20 76 6f 69 64 20 7a 65 72 6f 50 61  atic void zeroPa
15bdd 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  ge(MemPage *pPag
15bde 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20  e, int flags){. 
15bdf 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
15be0 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44  data = pPage->aD
15be1 61 74 61 3b 0a 20 20 42 74 53 68 61 72 65 64 20  ata;.  BtShared 
15be2 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42  *pBt = pPage->pB
15be3 74 3b 0a 20 20 75 38 20 68 64 72 20 3d 20 70 50  t;.  u8 hdr = pP
15be4 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a  age->hdrOffset;.
15be5 20 20 75 31 36 20 66 69 72 73 74 3b 0a 0a 20 20    u16 first;..  
15be6 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
15be7 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72 28 70  agerPagenumber(p
15be8 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3d 3d  Page->pDbPage)==
15be9 70 50 61 67 65 2d 3e 70 67 6e 6f 20 29 3b 0a 20  pPage->pgno );. 
15bea 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15beb 50 61 67 65 72 47 65 74 45 78 74 72 61 28 70 50  PagerGetExtra(pP
15bec 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 3d 3d  age->pDbPage) ==
15bed 20 28 76 6f 69 64 2a 29 70 50 61 67 65 20 29 3b   (void*)pPage );
15bee 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
15bef 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70  e3PagerGetData(p
15bf0 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 3d  Page->pDbPage) =
15bf1 3d 20 64 61 74 61 20 29 3b 0a 20 20 61 73 73 65  = data );.  asse
15bf2 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
15bf3 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67  Iswriteable(pPag
15bf4 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
15bf5 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15bf6 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
15bf7 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 2f 2a 6d  >mutex) );.  /*m
15bf8 65 6d 73 65 74 28 26 64 61 74 61 5b 68 64 72 5d  emset(&data[hdr]
15bf9 2c 20 30 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65  , 0, pBt->usable
15bfa 53 69 7a 65 20 2d 20 68 64 72 29 3b 2a 2f 0a 20  Size - hdr);*/. 
15bfb 20 64 61 74 61 5b 68 64 72 5d 20 3d 20 28 63 68   data[hdr] = (ch
15bfc 61 72 29 66 6c 61 67 73 3b 0a 20 20 66 69 72 73  ar)flags;.  firs
15bfd 74 20 3d 20 68 64 72 20 2b 20 38 20 2b 20 34 2a  t = hdr + 8 + 4*
15bfe 28 28 66 6c 61 67 73 26 50 54 46 5f 4c 45 41 46  ((flags&PTF_LEAF
15bff 29 3d 3d 30 20 3f 31 3a 30 29 3b 0a 20 20 6d 65  )==0 ?1:0);.  me
15c00 6d 73 65 74 28 26 64 61 74 61 5b 68 64 72 2b 31  mset(&data[hdr+1
15c01 5d 2c 20 30 2c 20 34 29 3b 0a 20 20 64 61 74 61  ], 0, 4);.  data
15c02 5b 68 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 20 70  [hdr+7] = 0;.  p
15c03 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64  ut2byte(&data[hd
15c04 72 2b 35 5d 2c 20 70 42 74 2d 3e 75 73 61 62 6c  r+5], pBt->usabl
15c05 65 53 69 7a 65 29 3b 0a 20 20 70 50 61 67 65 2d  eSize);.  pPage-
15c06 3e 6e 46 72 65 65 20 3d 20 70 42 74 2d 3e 75 73  >nFree = pBt->us
15c07 61 62 6c 65 53 69 7a 65 20 2d 20 66 69 72 73 74  ableSize - first
15c08 3b 0a 20 20 64 65 63 6f 64 65 46 6c 61 67 73 28  ;.  decodeFlags(
15c09 70 50 61 67 65 2c 20 66 6c 61 67 73 29 3b 0a 20  pPage, flags);. 
15c0a 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65   pPage->hdrOffse
15c0b 74 20 3d 20 68 64 72 3b 0a 20 20 70 50 61 67 65  t = hdr;.  pPage
15c0c 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 66  ->cellOffset = f
15c0d 69 72 73 74 3b 0a 20 20 70 50 61 67 65 2d 3e 6e  irst;.  pPage->n
15c0e 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20  Overflow = 0;.  
15c0f 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 61 67  assert( pBt->pag
15c10 65 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 42  eSize>=512 && pB
15c11 74 2d 3e 70 61 67 65 53 69 7a 65 3c 3d 33 32 37  t->pageSize<=327
15c12 36 38 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6d  68 );.  pPage->m
15c13 61 73 6b 50 61 67 65 20 3d 20 70 42 74 2d 3e 70  askPage = pBt->p
15c14 61 67 65 53 69 7a 65 20 2d 20 31 3b 0a 20 20 70  ageSize - 1;.  p
15c15 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 30 3b  Page->nCell = 0;
15c16 0a 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74  .  pPage->isInit
15c17 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20   = 1;.}.../*.** 
15c18 43 6f 6e 76 65 72 74 20 61 20 44 62 50 61 67 65  Convert a DbPage
15c19 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 74   obtained from t
15c1a 68 65 20 70 61 67 65 72 20 69 6e 74 6f 20 61 20  he pager into a 
15c1b 4d 65 6d 50 61 67 65 20 75 73 65 64 20 62 79 0a  MemPage used by.
15c1c 2a 2a 20 74 68 65 20 62 74 72 65 65 20 6c 61 79  ** the btree lay
15c1d 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4d 65  er..*/.static Me
15c1e 6d 50 61 67 65 20 2a 62 74 72 65 65 50 61 67 65  mPage *btreePage
15c1f 46 72 6f 6d 44 62 50 61 67 65 28 44 62 50 61 67  FromDbPage(DbPag
15c20 65 20 2a 70 44 62 50 61 67 65 2c 20 50 67 6e 6f  e *pDbPage, Pgno
15c21 20 70 67 6e 6f 2c 20 42 74 53 68 61 72 65 64 20   pgno, BtShared 
15c22 2a 70 42 74 29 7b 0a 20 20 4d 65 6d 50 61 67 65  *pBt){.  MemPage
15c23 20 2a 70 50 61 67 65 20 3d 20 28 4d 65 6d 50 61   *pPage = (MemPa
15c24 67 65 2a 29 73 71 6c 69 74 65 33 50 61 67 65 72  ge*)sqlite3Pager
15c25 47 65 74 45 78 74 72 61 28 70 44 62 50 61 67 65  GetExtra(pDbPage
15c26 29 3b 0a 20 20 70 50 61 67 65 2d 3e 61 44 61 74  );.  pPage->aDat
15c27 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  a = sqlite3Pager
15c28 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65 29  GetData(pDbPage)
15c29 3b 0a 20 20 70 50 61 67 65 2d 3e 70 44 62 50 61  ;.  pPage->pDbPa
15c2a 67 65 20 3d 20 70 44 62 50 61 67 65 3b 0a 20 20  ge = pDbPage;.  
15c2b 70 50 61 67 65 2d 3e 70 42 74 20 3d 20 70 42 74  pPage->pBt = pBt
15c2c 3b 0a 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20  ;.  pPage->pgno 
15c2d 3d 20 70 67 6e 6f 3b 0a 20 20 70 50 61 67 65 2d  = pgno;.  pPage-
15c2e 3e 68 64 72 4f 66 66 73 65 74 20 3d 20 70 50 61  >hdrOffset = pPa
15c2f 67 65 2d 3e 70 67 6e 6f 3d 3d 31 20 3f 20 31 30  ge->pgno==1 ? 10
15c30 30 20 3a 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  0 : 0;.  return 
15c31 70 50 61 67 65 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a  pPage; .}../*.**
15c32 20 47 65 74 20 61 20 70 61 67 65 20 66 72 6f 6d   Get a page from
15c33 20 74 68 65 20 70 61 67 65 72 2e 20 20 49 6e 69   the pager.  Ini
15c34 74 69 61 6c 69 7a 65 20 74 68 65 20 4d 65 6d 50  tialize the MemP
15c35 61 67 65 2e 70 42 74 20 61 6e 64 0a 2a 2a 20 4d  age.pBt and.** M
15c36 65 6d 50 61 67 65 2e 61 44 61 74 61 20 65 6c 65  emPage.aData ele
15c37 6d 65 6e 74 73 20 69 66 20 6e 65 65 64 65 64 2e  ments if needed.
15c38 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 6f  .**.** If the no
15c39 43 6f 6e 74 65 6e 74 20 66 6c 61 67 20 69 73 20  Content flag is 
15c3a 73 65 74 2c 20 69 74 20 6d 65 61 6e 73 20 74 68  set, it means th
15c3b 61 74 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72  at we do not car
15c3c 65 20 61 62 6f 75 74 0a 2a 2a 20 74 68 65 20 63  e about.** the c
15c3d 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61  ontent of the pa
15c3e 67 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e  ge at this time.
15c3f 20 20 53 6f 20 64 6f 20 6e 6f 74 20 67 6f 20 74    So do not go t
15c40 6f 20 74 68 65 20 64 69 73 6b 0a 2a 2a 20 74 6f  o the disk.** to
15c41 20 66 65 74 63 68 20 74 68 65 20 63 6f 6e 74 65   fetch the conte
15c42 6e 74 2e 20 20 4a 75 73 74 20 66 69 6c 6c 20 69  nt.  Just fill i
15c43 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 77 69  n the content wi
15c44 74 68 20 7a 65 72 6f 73 20 66 6f 72 20 6e 6f 77  th zeros for now
15c45 2e 0a 2a 2a 20 49 66 20 69 6e 20 74 68 65 20 66  ..** If in the f
15c46 75 74 75 72 65 20 77 65 20 63 61 6c 6c 20 73 71  uture we call sq
15c47 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
15c48 29 20 6f 6e 20 74 68 69 73 20 70 61 67 65 2c 20  ) on this page, 
15c49 74 68 61 74 0a 2a 2a 20 6d 65 61 6e 73 20 77 65  that.** means we
15c4a 20 68 61 76 65 20 73 74 61 72 74 65 64 20 74 6f   have started to
15c4b 20 62 65 20 63 6f 6e 63 65 72 6e 65 64 20 61 62   be concerned ab
15c4c 6f 75 74 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20  out content and 
15c4d 74 68 65 20 64 69 73 6b 0a 2a 2a 20 72 65 61 64  the disk.** read
15c4e 20 73 68 6f 75 6c 64 20 6f 63 63 75 72 20 61 74   should occur at
15c4f 20 74 68 61 74 20 70 6f 69 6e 74 2e 0a 2a 2f 0a   that point..*/.
15c50 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65  static int btree
15c51 47 65 74 50 61 67 65 28 0a 20 20 42 74 53 68 61  GetPage(.  BtSha
15c52 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20  red *pBt,       
15c53 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f 0a  /* The btree */.
15c54 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20    Pgno pgno,    
15c55 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
15c56 20 6f 66 20 74 68 65 20 70 61 67 65 20 74 6f 20   of the page to 
15c57 66 65 74 63 68 20 2a 2f 0a 20 20 4d 65 6d 50 61  fetch */.  MemPa
15c58 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 20 20 20  ge **ppPage,    
15c59 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 61  /* Return the pa
15c5a 67 65 20 69 6e 20 74 68 69 73 20 70 61 72 61 6d  ge in this param
15c5b 65 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f  eter */.  int no
15c5c 43 6f 6e 74 65 6e 74 20 20 20 20 20 20 20 20 2f  Content        /
15c5d 2a 20 44 6f 20 6e 6f 74 20 6c 6f 61 64 20 70 61  * Do not load pa
15c5e 67 65 20 63 6f 6e 74 65 6e 74 20 69 66 20 74 72  ge content if tr
15c5f 75 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  ue */.){.  int r
15c60 63 3b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62  c;.  DbPage *pDb
15c61 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28  Page;..  assert(
15c62 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
15c63 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
15c64 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
15c65 33 50 61 67 65 72 41 63 71 75 69 72 65 28 70 42  3PagerAcquire(pB
15c66 74 2d 3e 70 50 61 67 65 72 2c 20 70 67 6e 6f 2c  t->pPager, pgno,
15c67 20 28 44 62 50 61 67 65 2a 2a 29 26 70 44 62 50   (DbPage**)&pDbP
15c68 61 67 65 2c 20 6e 6f 43 6f 6e 74 65 6e 74 29 3b  age, noContent);
15c69 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75  .  if( rc ) retu
15c6a 72 6e 20 72 63 3b 0a 20 20 2a 70 70 50 61 67 65  rn rc;.  *ppPage
15c6b 20 3d 20 62 74 72 65 65 50 61 67 65 46 72 6f 6d   = btreePageFrom
15c6c 44 62 50 61 67 65 28 70 44 62 50 61 67 65 2c 20  DbPage(pDbPage, 
15c6d 70 67 6e 6f 2c 20 70 42 74 29 3b 0a 20 20 72 65  pgno, pBt);.  re
15c6e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
15c6f 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 72 69 65 76  }../*.** Retriev
15c70 65 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68  e a page from th
15c71 65 20 70 61 67 65 72 20 63 61 63 68 65 2e 20 49  e pager cache. I
15c72 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  f the requested 
15c73 70 61 67 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 61  page is not.** a
15c74 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 70 61  lready in the pa
15c75 67 65 72 20 63 61 63 68 65 20 72 65 74 75 72 6e  ger cache return
15c76 20 4e 55 4c 4c 2e 20 49 6e 69 74 69 61 6c 69 7a   NULL. Initializ
15c77 65 20 74 68 65 20 4d 65 6d 50 61 67 65 2e 70 42  e the MemPage.pB
15c78 74 20 61 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65  t and.** MemPage
15c79 2e 61 44 61 74 61 20 65 6c 65 6d 65 6e 74 73 20  .aData elements 
15c7a 69 66 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73 74  if needed..*/.st
15c7b 61 74 69 63 20 4d 65 6d 50 61 67 65 20 2a 62 74  atic MemPage *bt
15c7c 72 65 65 50 61 67 65 4c 6f 6f 6b 75 70 28 42 74  reePageLookup(Bt
15c7d 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e  Shared *pBt, Pgn
15c7e 6f 20 70 67 6e 6f 29 7b 0a 20 20 44 62 50 61 67  o pgno){.  DbPag
15c7f 65 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 61 73  e *pDbPage;.  as
15c80 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
15c81 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
15c82 74 65 78 29 20 29 3b 0a 20 20 70 44 62 50 61 67  tex) );.  pDbPag
15c83 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  e = sqlite3Pager
15c84 4c 6f 6f 6b 75 70 28 70 42 74 2d 3e 70 50 61 67  Lookup(pBt->pPag
15c85 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 69 66 28  er, pgno);.  if(
15c86 20 70 44 62 50 61 67 65 20 29 7b 0a 20 20 20 20   pDbPage ){.    
15c87 72 65 74 75 72 6e 20 62 74 72 65 65 50 61 67 65  return btreePage
15c88 46 72 6f 6d 44 62 50 61 67 65 28 70 44 62 50 61  FromDbPage(pDbPa
15c89 67 65 2c 20 70 67 6e 6f 2c 20 70 42 74 29 3b 0a  ge, pgno, pBt);.
15c8a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
15c8b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
15c8c 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
15c8d 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e  database file in
15c8e 20 70 61 67 65 73 2e 20 49 66 20 74 68 65 72 65   pages. If there
15c8f 20 69 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66 0a   is any kind of.
15c90 2a 2a 20 65 72 72 6f 72 2c 20 72 65 74 75 72 6e  ** error, return
15c91 20 28 28 75 6e 73 69 67 6e 65 64 20 69 6e 74 29   ((unsigned int)
15c92 2d 31 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50  -1)..*/.static P
15c93 67 6e 6f 20 70 61 67 65 72 50 61 67 65 63 6f 75  gno pagerPagecou
15c94 6e 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  nt(BtShared *pBt
15c95 29 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65 20 3d  ){.  int nPage =
15c96 20 2d 31 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20   -1;.  int rc;. 
15c97 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50   assert( pBt->pP
15c98 61 67 65 31 20 29 3b 0a 20 20 72 63 20 3d 20 73  age1 );.  rc = s
15c99 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63  qlite3PagerPagec
15c9a 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72  ount(pBt->pPager
15c9b 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 61 73 73  , &nPage);.  ass
15c9c 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
15c9d 4f 4b 20 7c 7c 20 6e 50 61 67 65 3d 3d 2d 31 20  OK || nPage==-1 
15c9e 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 50 67 6e  );.  return (Pgn
15c9f 6f 29 6e 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  o)nPage;.}../*.*
15ca0 2a 20 47 65 74 20 61 20 70 61 67 65 20 66 72 6f  * Get a page fro
15ca1 6d 20 74 68 65 20 70 61 67 65 72 20 61 6e 64 20  m the pager and 
15ca2 69 6e 69 74 69 61 6c 69 7a 65 20 69 74 2e 20 20  initialize it.  
15ca3 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
15ca4 6a 75 73 74 20 61 0a 2a 2a 20 63 6f 6e 76 65 6e  just a.** conven
15ca5 69 65 6e 63 65 20 77 72 61 70 70 65 72 20 61 72  ience wrapper ar
15ca6 6f 75 6e 64 20 73 65 70 61 72 61 74 65 20 63 61  ound separate ca
15ca7 6c 6c 73 20 74 6f 20 62 74 72 65 65 47 65 74 50  lls to btreeGetP
15ca8 61 67 65 28 29 20 61 6e 64 20 0a 2a 2a 20 62 74  age() and .** bt
15ca9 72 65 65 49 6e 69 74 50 61 67 65 28 29 2e 0a 2a  reeInitPage()..*
15caa 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  *.** If an error
15cab 20 6f 63 63 75 72 73 2c 20 74 68 65 6e 20 74 68   occurs, then th
15cac 65 20 76 61 6c 75 65 20 2a 70 70 50 61 67 65 20  e value *ppPage 
15cad 69 73 20 73 65 74 20 74 6f 20 69 73 20 75 6e 64  is set to is und
15cae 65 66 69 6e 65 64 2e 20 49 74 0a 2a 2a 20 6d 61  efined. It.** ma
15caf 79 20 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67  y remain unchang
15cb0 65 64 2c 20 6f 72 20 69 74 20 6d 61 79 20 62 65  ed, or it may be
15cb1 20 73 65 74 20 74 6f 20 61 6e 20 69 6e 76 61 6c   set to an inval
15cb2 69 64 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61  id value..*/.sta
15cb3 74 69 63 20 69 6e 74 20 67 65 74 41 6e 64 49 6e  tic int getAndIn
15cb4 69 74 50 61 67 65 28 0a 20 20 42 74 53 68 61 72  itPage(.  BtShar
15cb5 65 64 20 2a 70 42 74 2c 20 20 20 20 20 20 20 20  ed *pBt,        
15cb6 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
15cb7 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f  e file */.  Pgno
15cb8 20 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20   pgno,          
15cb9 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68   /* Number of th
15cba 65 20 70 61 67 65 20 74 6f 20 67 65 74 20 2a 2f  e page to get */
15cbb 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50  .  MemPage **ppP
15cbc 61 67 65 20 20 20 20 20 2f 2a 20 57 72 69 74 65  age     /* Write
15cbd 20 74 68 65 20 70 61 67 65 20 70 6f 69 6e 74 65   the page pointe
15cbe 72 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  r here */.){.  i
15cbf 6e 74 20 72 63 3b 0a 20 20 54 45 53 54 4f 4e 4c  nt rc;.  TESTONL
15cc0 59 28 20 50 67 6e 6f 20 69 4c 61 73 74 50 67 20  Y( Pgno iLastPg 
15cc1 3d 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74  = pagerPagecount
15cc2 28 70 42 74 29 3b 20 29 0a 20 20 61 73 73 65 72  (pBt); ).  asser
15cc3 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
15cc4 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
15cc5 29 20 29 3b 0a 0a 20 20 72 63 20 3d 20 62 74 72  ) );..  rc = btr
15cc6 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 70  eeGetPage(pBt, p
15cc7 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20 30 29 3b  gno, ppPage, 0);
15cc8 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
15cc9 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d  E_OK ){.    rc =
15cca 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 2a   btreeInitPage(*
15ccb 70 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28  ppPage);.    if(
15ccc 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
15ccd 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50  {.      releaseP
15cce 61 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20  age(*ppPage);.  
15ccf 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66    }.  }..  /* If
15cd0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 70   the requested p
15cd1 61 67 65 20 6e 75 6d 62 65 72 20 77 61 73 20 65  age number was e
15cd2 69 74 68 65 72 20 30 20 6f 72 20 67 72 65 61 74  ither 0 or great
15cd3 65 72 20 74 68 61 6e 20 74 68 65 20 70 61 67 65  er than the page
15cd4 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  .  ** number of 
15cd5 74 68 65 20 6c 61 73 74 20 70 61 67 65 20 69 6e  the last page in
15cd6 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 74   the database, t
15cd7 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f  his function sho
15cd8 75 6c 64 20 72 65 74 75 72 6e 0a 20 20 2a 2a 20  uld return.  ** 
15cd9 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20 6f  SQLITE_CORRUPT o
15cda 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 65 72 72  r some other err
15cdb 6f 72 20 28 69 2e 65 2e 20 53 51 4c 49 54 45 5f  or (i.e. SQLITE_
15cdc 46 55 4c 4c 29 2e 20 43 68 65 63 6b 20 74 68 61  FULL). Check tha
15cdd 74 20 74 68 69 73 0a 20 20 2a 2a 20 69 73 20 74  t this.  ** is t
15cde 68 65 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 61  he case.  */.  a
15cdf 73 73 65 72 74 28 20 28 70 67 6e 6f 3e 30 20 26  ssert( (pgno>0 &
15ce0 26 20 70 67 6e 6f 3c 3d 69 4c 61 73 74 50 67 29  & pgno<=iLastPg)
15ce1 20 7c 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f   || rc!=SQLITE_O
15ce2 4b 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  K );.  testcase(
15ce3 20 70 67 6e 6f 3d 3d 30 20 29 3b 0a 20 20 74 65   pgno==0 );.  te
15ce4 73 74 63 61 73 65 28 20 70 67 6e 6f 3d 3d 69 4c  stcase( pgno==iL
15ce5 61 73 74 50 67 20 29 3b 0a 0a 20 20 72 65 74 75  astPg );..  retu
15ce6 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
15ce7 52 65 6c 65 61 73 65 20 61 20 4d 65 6d 50 61 67  Release a MemPag
15ce8 65 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20  e.  This should 
15ce9 62 65 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20 66  be called once f
15cea 6f 72 20 65 61 63 68 20 70 72 69 6f 72 0a 2a 2a  or each prior.**
15ceb 20 63 61 6c 6c 20 74 6f 20 62 74 72 65 65 47 65   call to btreeGe
15cec 74 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  tPage..*/.static
15ced 20 76 6f 69 64 20 72 65 6c 65 61 73 65 50 61 67   void releasePag
15cee 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  e(MemPage *pPage
15cef 29 7b 0a 20 20 69 66 28 20 70 50 61 67 65 20 29  ){.  if( pPage )
15cf0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
15cf1 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d  age->nOverflow==
15cf2 30 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65  0 || sqlite3Page
15cf3 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 70 50  rPageRefcount(pP
15cf4 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3e 31 20  age->pDbPage)>1 
15cf5 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
15cf6 50 61 67 65 2d 3e 61 44 61 74 61 20 29 3b 0a 20  Page->aData );. 
15cf7 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
15cf8 2d 3e 70 42 74 20 29 3b 0a 20 20 20 20 61 73 73  ->pBt );.    ass
15cf9 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
15cfa 72 47 65 74 45 78 74 72 61 28 70 50 61 67 65 2d  rGetExtra(pPage-
15cfb 3e 70 44 62 50 61 67 65 29 20 3d 3d 20 28 76 6f  >pDbPage) == (vo
15cfc 69 64 2a 29 70 50 61 67 65 20 29 3b 0a 20 20 20  id*)pPage );.   
15cfd 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15cfe 50 61 67 65 72 47 65 74 44 61 74 61 28 70 50 61  PagerGetData(pPa
15cff 67 65 2d 3e 70 44 62 50 61 67 65 29 3d 3d 70 50  ge->pDbPage)==pP
15d00 61 67 65 2d 3e 61 44 61 74 61 20 29 3b 0a 20 20  age->aData );.  
15d01 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
15d02 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
15d03 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
15d04 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61  );.    sqlite3Pa
15d05 67 65 72 55 6e 72 65 66 28 70 50 61 67 65 2d 3e  gerUnref(pPage->
15d06 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a 7d 0a  pDbPage);.  }.}.
15d07 0a 2f 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 61 20  ./*.** During a 
15d08 72 6f 6c 6c 62 61 63 6b 2c 20 77 68 65 6e 20 74  rollback, when t
15d09 68 65 20 70 61 67 65 72 20 72 65 6c 6f 61 64 73  he pager reloads
15d0a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74   information int
15d0b 6f 20 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 73  o the cache.** s
15d0c 6f 20 74 68 61 74 20 74 68 65 20 63 61 63 68 65  o that the cache
15d0d 20 69 73 20 72 65 73 74 6f 72 65 64 20 74 6f 20   is restored to 
15d0e 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61  its original sta
15d0f 74 65 20 61 74 20 74 68 65 20 73 74 61 72 74 20  te at the start 
15d10 6f 66 0a 2a 2a 20 74 68 65 20 74 72 61 6e 73 61  of.** the transa
15d11 63 74 69 6f 6e 2c 20 66 6f 72 20 65 61 63 68 20  ction, for each 
15d12 70 61 67 65 20 72 65 73 74 6f 72 65 64 20 74 68  page restored th
15d13 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
15d14 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  lled..**.** This
15d15 20 72 6f 75 74 69 6e 65 20 6e 65 65 64 73 20 74   routine needs t
15d16 6f 20 72 65 73 65 74 20 74 68 65 20 65 78 74 72  o reset the extr
15d17 61 20 64 61 74 61 20 73 65 63 74 69 6f 6e 20 61  a data section a
15d18 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
15d19 0a 2a 2a 20 70 61 67 65 20 74 6f 20 61 67 72 65  .** page to agre
15d1a 65 20 77 69 74 68 20 74 68 65 20 72 65 73 74 6f  e with the resto
15d1b 72 65 64 20 64 61 74 61 2e 0a 2a 2f 0a 73 74 61  red data..*/.sta
15d1c 74 69 63 20 76 6f 69 64 20 70 61 67 65 52 65 69  tic void pageRei
15d1d 6e 69 74 28 44 62 50 61 67 65 20 2a 70 44 61 74  nit(DbPage *pDat
15d1e 61 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  a){.  MemPage *p
15d1f 50 61 67 65 3b 0a 20 20 70 50 61 67 65 20 3d 20  Page;.  pPage = 
15d20 28 4d 65 6d 50 61 67 65 20 2a 29 73 71 6c 69 74  (MemPage *)sqlit
15d21 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28  e3PagerGetExtra(
15d22 70 44 61 74 61 29 3b 0a 20 20 61 73 73 65 72 74  pData);.  assert
15d23 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61  ( sqlite3PagerPa
15d24 67 65 52 65 66 63 6f 75 6e 74 28 70 44 61 74 61  geRefcount(pData
15d25 29 3e 30 20 29 3b 0a 20 20 69 66 28 20 70 50 61  )>0 );.  if( pPa
15d26 67 65 2d 3e 69 73 49 6e 69 74 20 29 7b 0a 20 20  ge->isInit ){.  
15d27 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
15d28 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
15d29 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
15d2a 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73  );.    pPage->is
15d2b 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20 69 66  Init = 0;.    if
15d2c 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61  ( sqlite3PagerPa
15d2d 67 65 52 65 66 63 6f 75 6e 74 28 70 44 61 74 61  geRefcount(pData
15d2e 29 3e 31 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  )>1 ){.      /* 
15d2f 70 50 61 67 65 20 6d 69 67 68 74 20 6e 6f 74 20  pPage might not 
15d30 62 65 20 61 20 62 74 72 65 65 20 70 61 67 65 3b  be a btree page;
15d31 20 20 69 74 20 6d 69 67 68 74 20 62 65 20 61 6e    it might be an
15d32 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 0a 20   overflow page. 
15d33 20 20 20 20 20 2a 2a 20 6f 72 20 70 74 72 6d 61       ** or ptrma
15d34 70 20 70 61 67 65 20 6f 72 20 61 20 66 72 65 65  p page or a free
15d35 20 70 61 67 65 2e 20 20 49 6e 20 74 68 6f 73 65   page.  In those
15d36 20 63 61 73 65 73 2c 20 74 68 65 20 66 6f 6c 6c   cases, the foll
15d37 6f 77 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 63  owing.      ** c
15d38 61 6c 6c 20 74 6f 20 62 74 72 65 65 49 6e 69 74  all to btreeInit
15d39 50 61 67 65 28 29 20 77 69 6c 6c 20 6c 69 6b 65  Page() will like
15d3a 6c 79 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ly return SQLITE
15d3b 5f 43 4f 52 52 55 50 54 2e 0a 20 20 20 20 20 20  _CORRUPT..      
15d3c 2a 2a 20 42 75 74 20 6e 6f 20 68 61 72 6d 20 69  ** But no harm i
15d3d 73 20 64 6f 6e 65 20 62 79 20 74 68 69 73 2e 20  s done by this. 
15d3e 20 41 6e 64 20 69 74 20 69 73 20 76 65 72 79 20   And it is very 
15d3f 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 0a 20  important that. 
15d40 20 20 20 20 20 2a 2a 20 62 74 72 65 65 49 6e 69       ** btreeIni
15d41 74 50 61 67 65 28 29 20 62 65 20 63 61 6c 6c 65  tPage() be calle
15d42 64 20 6f 6e 20 65 76 65 72 79 20 62 74 72 65 65  d on every btree
15d43 20 70 61 67 65 20 73 6f 20 77 65 20 6d 61 6b 65   page so we make
15d44 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63 61  .      ** the ca
15d45 6c 6c 20 66 6f 72 20 65 76 65 72 79 20 70 61 67  ll for every pag
15d46 65 20 74 68 61 74 20 63 6f 6d 65 73 20 69 6e 20  e that comes in 
15d47 66 6f 72 20 72 65 2d 69 6e 69 74 69 6e 67 2e 20  for re-initing. 
15d48 2a 2f 0a 20 20 20 20 20 20 62 74 72 65 65 49 6e  */.      btreeIn
15d49 69 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  itPage(pPage);. 
15d4a 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
15d4b 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 62 75 73  * Invoke the bus
15d4c 79 20 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 20  y handler for a 
15d4d 62 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  btree..*/.static
15d4e 20 69 6e 74 20 62 74 72 65 65 49 6e 76 6f 6b 65   int btreeInvoke
15d4f 42 75 73 79 48 61 6e 64 6c 65 72 28 76 6f 69 64  BusyHandler(void
15d50 20 2a 70 41 72 67 29 7b 0a 20 20 42 74 53 68 61   *pArg){.  BtSha
15d51 72 65 64 20 2a 70 42 74 20 3d 20 28 42 74 53 68  red *pBt = (BtSh
15d52 61 72 65 64 2a 29 70 41 72 67 3b 0a 20 20 61 73  ared*)pArg;.  as
15d53 73 65 72 74 28 20 70 42 74 2d 3e 64 62 20 29 3b  sert( pBt->db );
15d54 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
15d55 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
15d56 74 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  t->db->mutex) );
15d57 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
15d58 33 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c  3InvokeBusyHandl
15d59 65 72 28 26 70 42 74 2d 3e 64 62 2d 3e 62 75 73  er(&pBt->db->bus
15d5a 79 48 61 6e 64 6c 65 72 29 3b 0a 7d 0a 0a 2f 2a  yHandler);.}../*
15d5b 0a 2a 2a 20 4f 70 65 6e 20 61 20 64 61 74 61 62  .** Open a datab
15d5c 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20 0a 2a 2a  ase file..** .**
15d5d 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 74 68   zFilename is th
15d5e 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  e name of the da
15d5f 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49 66  tabase file.  If
15d60 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 4e 55   zFilename is NU
15d61 4c 4c 0a 2a 2a 20 61 20 6e 65 77 20 64 61 74 61  LL.** a new data
15d62 62 61 73 65 20 77 69 74 68 20 61 20 72 61 6e 64  base with a rand
15d63 6f 6d 20 6e 61 6d 65 20 69 73 20 63 72 65 61 74  om name is creat
15d64 65 64 2e 20 20 54 68 69 73 20 72 61 6e 64 6f 6d  ed.  This random
15d65 6c 79 20 6e 61 6d 65 64 0a 2a 2a 20 64 61 74 61  ly named.** data
15d66 62 61 73 65 20 66 69 6c 65 20 77 69 6c 6c 20 62  base file will b
15d67 65 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 73  e deleted when s
15d68 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65  qlite3BtreeClose
15d69 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a  () is called..**
15d6a 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73   If zFilename is
15d6b 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e   ":memory:" then
15d6c 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
15d6d 74 61 62 61 73 65 20 69 73 20 63 72 65 61 74 65  tabase is create
15d6e 64 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 75 74  d.** that is aut
15d6f 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65 73 74 72  omatically destr
15d70 6f 79 65 64 20 77 68 65 6e 20 69 74 20 69 73 20  oyed when it is 
15d71 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  closed..**.** If
15d72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
15d73 20 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 20   already opened 
15d74 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  in the same data
15d75 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  base connection.
15d76 2a 2a 20 61 6e 64 20 77 65 20 61 72 65 20 69 6e  ** and we are in
15d77 20 73 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f   shared cache mo
15d78 64 65 2c 20 74 68 65 6e 20 74 68 65 20 6f 70 65  de, then the ope
15d79 6e 20 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68  n will fail with
15d7a 20 61 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f   an.** SQLITE_CO
15d7b 4e 53 54 52 41 49 4e 54 20 65 72 72 6f 72 2e 20  NSTRAINT error. 
15d7c 20 57 65 20 63 61 6e 6e 6f 74 20 61 6c 6c 6f 77   We cannot allow
15d7d 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 42 74 53   two or more BtS
15d7e 68 61 72 65 64 0a 2a 2a 20 6f 62 6a 65 63 74 73  hared.** objects
15d7f 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74   in the same dat
15d80 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
15d81 20 73 69 6e 63 65 20 64 6f 69 6e 67 20 73 6f 20   since doing so 
15d82 77 69 6c 6c 20 6c 65 61 64 0a 2a 2a 20 74 6f 20  will lead.** to 
15d83 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 20 6c 6f  problems with lo
15d84 63 6b 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45  cking..*/.SQLITE
15d85 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
15d86 69 74 65 33 42 74 72 65 65 4f 70 65 6e 28 0a 20  ite3BtreeOpen(. 
15d87 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
15d88 6c 65 6e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65  lename,  /* Name
15d89 20 6f 66 20 74 68 65 20 66 69 6c 65 20 63 6f 6e   of the file con
15d8a 74 61 69 6e 69 6e 67 20 74 68 65 20 42 54 72 65  taining the BTre
15d8b 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  e database */.  
15d8c 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
15d8d 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 6f 63          /* Assoc
15d8e 69 61 74 65 64 20 64 61 74 61 62 61 73 65 20 68  iated database h
15d8f 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65 65  andle */.  Btree
15d90 20 2a 2a 70 70 42 74 72 65 65 2c 20 20 20 20 20   **ppBtree,     
15d91 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
15d92 20 6e 65 77 20 42 74 72 65 65 20 6f 62 6a 65 63   new Btree objec
15d93 74 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a  t written here *
15d94 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
15d95 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
15d96 70 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20  ptions */.  int 
15d97 76 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20  vfsFlags        
15d98 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 70 61 73      /* Flags pas
15d99 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73  sed through to s
15d9a 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e  qlite3_vfs.xOpen
15d9b 28 29 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  () */.){.  sqlit
15d9c 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 20 20 20  e3_vfs *pVfs;   
15d9d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
15d9e 20 56 46 53 20 74 6f 20 75 73 65 20 66 6f 72 20   VFS to use for 
15d9f 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20  this btree */.  
15da0 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
15da1 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0;             /
15da2 2a 20 53 68 61 72 65 64 20 70 61 72 74 20 6f 66  * Shared part of
15da3 20 62 74 72 65 65 20 73 74 72 75 63 74 75 72 65   btree structure
15da4 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 3b 20   */.  Btree *p; 
15da5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15da6 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 74       /* Handle t
15da7 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71  o return */.  sq
15da8 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
15da9 65 78 4f 70 65 6e 20 3d 20 30 3b 20 20 2f 2a 20  exOpen = 0;  /* 
15daa 50 72 65 76 65 6e 74 73 20 61 20 72 61 63 65 20  Prevents a race 
15dab 63 6f 6e 64 69 74 69 6f 6e 2e 20 54 69 63 6b 65  condition. Ticke
15dac 74 20 23 33 35 33 37 20 2a 2f 0a 20 20 69 6e 74  t #3537 */.  int
15dad 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
15dae 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
15daf 65 73 75 6c 74 20 63 6f 64 65 20 66 72 6f 6d 20  esult code from 
15db0 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  this function */
15db1 0a 20 20 75 38 20 6e 52 65 73 65 72 76 65 3b 20  .  u8 nReserve; 
15db2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15db3 20 20 2f 2a 20 42 79 74 65 20 6f 66 20 75 6e 75    /* Byte of unu
15db4 73 65 64 20 73 70 61 63 65 20 6f 6e 20 65 61 63  sed space on eac
15db5 68 20 70 61 67 65 20 2a 2f 0a 20 20 75 6e 73 69  h page */.  unsi
15db6 67 6e 65 64 20 63 68 61 72 20 7a 44 62 48 65 61  gned char zDbHea
15db7 64 65 72 5b 31 30 30 5d 3b 20 20 2f 2a 20 44 61  der[100];  /* Da
15db8 74 61 62 61 73 65 20 68 65 61 64 65 72 20 63 6f  tabase header co
15db9 6e 74 65 6e 74 20 2a 2f 0a 0a 20 20 2f 2a 20 53  ntent */..  /* S
15dba 65 74 20 74 68 65 20 76 61 72 69 61 62 6c 65 20  et the variable 
15dbb 69 73 4d 65 6d 64 62 20 74 6f 20 74 72 75 65 20  isMemdb to true 
15dbc 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  for an in-memory
15dbd 20 64 61 74 61 62 61 73 65 2c 20 6f 72 20 0a 20   database, or . 
15dbe 20 2a 2a 20 66 61 6c 73 65 20 66 6f 72 20 61 20   ** false for a 
15dbf 66 69 6c 65 2d 62 61 73 65 64 20 64 61 74 61 62  file-based datab
15dc0 61 73 65 2e 20 54 68 69 73 20 73 79 6d 62 6f 6c  ase. This symbol
15dc1 20 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65   is only require
15dc2 64 20 69 66 0a 20 20 2a 2a 20 65 69 74 68 65 72  d if.  ** either
15dc3 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 2d 64   of the shared-d
15dc4 61 74 61 20 6f 72 20 61 75 74 6f 76 61 63 75 75  ata or autovacuu
15dc5 6d 20 66 65 61 74 75 72 65 73 20 61 72 65 20 63  m features are c
15dc6 6f 6d 70 69 6c 65 64 20 0a 20 20 2a 2a 20 69 6e  ompiled .  ** in
15dc7 74 6f 20 74 68 65 20 6c 69 62 72 61 72 79 2e 0a  to the library..
15dc8 20 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65    */.#if !define
15dc9 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  d(SQLITE_OMIT_SH
15dca 41 52 45 44 5f 43 41 43 48 45 29 20 7c 7c 20 21  ARED_CACHE) || !
15dcb 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
15dcc 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 29 0a  MIT_AUTOVACUUM).
15dcd 20 20 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f    #ifdef SQLITE_
15dce 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20  OMIT_MEMORYDB.  
15dcf 20 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65    const int isMe
15dd0 6d 64 62 20 3d 20 30 3b 0a 20 20 23 65 6c 73 65  mdb = 0;.  #else
15dd1 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69  .    const int i
15dd2 73 4d 65 6d 64 62 20 3d 20 7a 46 69 6c 65 6e 61  sMemdb = zFilena
15dd3 6d 65 20 26 26 20 21 73 74 72 63 6d 70 28 7a 46  me && !strcmp(zF
15dd4 69 6c 65 6e 61 6d 65 2c 20 22 3a 6d 65 6d 6f 72  ilename, ":memor
15dd5 79 3a 22 29 3b 0a 20 20 23 65 6e 64 69 66 0a 23  y:");.  #endif.#
15dd6 65 6e 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28  endif..  assert(
15dd7 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65   db!=0 );.  asse
15dd8 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
15dd9 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
15dda 29 20 29 3b 0a 0a 20 20 70 56 66 73 20 3d 20 64  ) );..  pVfs = d
15ddb 62 2d 3e 70 56 66 73 3b 0a 20 20 70 20 3d 20 73  b->pVfs;.  p = s
15ddc 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f  qlite3MallocZero
15ddd 28 73 69 7a 65 6f 66 28 42 74 72 65 65 29 29 3b  (sizeof(Btree));
15dde 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20  .  if( !p ){.   
15ddf 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
15de0 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 2d 3e 69  OMEM;.  }.  p->i
15de1 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e  nTrans = TRANS_N
15de2 4f 4e 45 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64  ONE;.  p->db = d
15de3 62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  b;.#ifndef SQLIT
15de4 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
15de5 43 48 45 0a 20 20 70 2d 3e 6c 6f 63 6b 2e 70 42  CHE.  p->lock.pB
15de6 74 72 65 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6c  tree = p;.  p->l
15de7 6f 63 6b 2e 69 54 61 62 6c 65 20 3d 20 31 3b 0a  ock.iTable = 1;.
15de8 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66  #endif..#if !def
15de9 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
15dea 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 26  _SHARED_CACHE) &
15deb 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
15dec 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20  E_OMIT_DISKIO). 
15ded 20 2f 2a 0a 20 20 2a 2a 20 49 66 20 74 68 69 73   /*.  ** If this
15dee 20 42 74 72 65 65 20 69 73 20 61 20 63 61 6e 64   Btree is a cand
15def 69 64 61 74 65 20 66 6f 72 20 73 68 61 72 65 64  idate for shared
15df0 20 63 61 63 68 65 2c 20 74 72 79 20 74 6f 20 66   cache, try to f
15df1 69 6e 64 20 61 6e 0a 20 20 2a 2a 20 65 78 69 73  ind an.  ** exis
15df2 74 69 6e 67 20 42 74 53 68 61 72 65 64 20 6f 62  ting BtShared ob
15df3 6a 65 63 74 20 74 68 61 74 20 77 65 20 63 61 6e  ject that we can
15df4 20 73 68 61 72 65 20 77 69 74 68 0a 20 20 2a 2f   share with.  */
15df5 0a 20 20 69 66 28 20 69 73 4d 65 6d 64 62 3d 3d  .  if( isMemdb==
15df6 30 20 26 26 20 7a 46 69 6c 65 6e 61 6d 65 20 26  0 && zFilename &
15df7 26 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 29  & zFilename[0] )
15df8 7b 0a 20 20 20 20 69 66 28 20 76 66 73 46 6c 61  {.    if( vfsFla
15df9 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
15dfa 5f 53 48 41 52 45 44 43 41 43 48 45 20 29 7b 0a  _SHAREDCACHE ){.
15dfb 20 20 20 20 20 20 69 6e 74 20 6e 46 75 6c 6c 50        int nFullP
15dfc 61 74 68 6e 61 6d 65 20 3d 20 70 56 66 73 2d 3e  athname = pVfs->
15dfd 6d 78 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 20 20  mxPathname+1;.  
15dfe 20 20 20 20 63 68 61 72 20 2a 7a 46 75 6c 6c 50      char *zFullP
15dff 61 74 68 6e 61 6d 65 20 3d 20 73 71 6c 69 74 65  athname = sqlite
15e00 33 4d 61 6c 6c 6f 63 28 6e 46 75 6c 6c 50 61 74  3Malloc(nFullPat
15e01 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 73 71  hname);.      sq
15e02 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
15e03 65 78 53 68 61 72 65 64 3b 0a 20 20 20 20 20 20  exShared;.      
15e04 70 2d 3e 73 68 61 72 61 62 6c 65 20 3d 20 31 3b  p->sharable = 1;
15e05 0a 20 20 20 20 20 20 69 66 28 20 21 7a 46 75 6c  .      if( !zFul
15e06 6c 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20  lPathname ){.   
15e07 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
15e08 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 65  e(p);.        re
15e09 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
15e0a 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  M;.      }.     
15e0b 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61   sqlite3OsFullPa
15e0c 74 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 46 69  thname(pVfs, zFi
15e0d 6c 65 6e 61 6d 65 2c 20 6e 46 75 6c 6c 50 61 74  lename, nFullPat
15e0e 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 50 61 74 68  hname, zFullPath
15e0f 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 6d 75 74  name);.      mut
15e10 65 78 4f 70 65 6e 20 3d 20 73 71 6c 69 74 65 33  exOpen = sqlite3
15e11 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
15e12 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4f  E_MUTEX_STATIC_O
15e13 50 45 4e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  PEN);.      sqli
15e14 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
15e15 6d 75 74 65 78 4f 70 65 6e 29 3b 0a 20 20 20 20  mutexOpen);.    
15e16 20 20 6d 75 74 65 78 53 68 61 72 65 64 20 3d 20    mutexShared = 
15e17 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
15e18 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
15e19 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20  TATIC_MASTER);. 
15e1a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
15e1b 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 53 68  ex_enter(mutexSh
15e1c 61 72 65 64 29 3b 0a 20 20 20 20 20 20 66 6f 72  ared);.      for
15e1d 28 70 42 74 3d 47 4c 4f 42 41 4c 28 42 74 53 68  (pBt=GLOBAL(BtSh
15e1e 61 72 65 64 2a 2c 73 71 6c 69 74 65 33 53 68 61  ared*,sqlite3Sha
15e1f 72 65 64 43 61 63 68 65 4c 69 73 74 29 3b 20 70  redCacheList); p
15e20 42 74 3b 20 70 42 74 3d 70 42 74 2d 3e 70 4e 65  Bt; pBt=pBt->pNe
15e21 78 74 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  xt){.        ass
15e22 65 72 74 28 20 70 42 74 2d 3e 6e 52 65 66 3e 30  ert( pBt->nRef>0
15e23 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
15e24 30 3d 3d 73 74 72 63 6d 70 28 7a 46 75 6c 6c 50  0==strcmp(zFullP
15e25 61 74 68 6e 61 6d 65 2c 20 73 71 6c 69 74 65 33  athname, sqlite3
15e26 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 70 42  PagerFilename(pB
15e27 74 2d 3e 70 50 61 67 65 72 29 29 0a 20 20 20 20  t->pPager)).    
15e28 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
15e29 73 71 6c 69 74 65 33 50 61 67 65 72 56 66 73 28  sqlite3PagerVfs(
15e2a 70 42 74 2d 3e 70 50 61 67 65 72 29 3d 3d 70 56  pBt->pPager)==pV
15e2b 66 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  fs ){.          
15e2c 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20 20 20  int iDb;.       
15e2d 20 20 20 66 6f 72 28 69 44 62 3d 64 62 2d 3e 6e     for(iDb=db->n
15e2e 44 62 2d 31 3b 20 69 44 62 3e 3d 30 3b 20 69 44  Db-1; iDb>=0; iD
15e2f 62 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20  b--){.          
15e30 20 20 42 74 72 65 65 20 2a 70 45 78 69 73 74 69    Btree *pExisti
15e31 6e 67 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  ng = db->aDb[iDb
15e32 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 20 20 20  ].pBt;.         
15e33 20 20 20 69 66 28 20 70 45 78 69 73 74 69 6e 67     if( pExisting
15e34 20 26 26 20 70 45 78 69 73 74 69 6e 67 2d 3e 70   && pExisting->p
15e35 42 74 3d 3d 70 42 74 20 29 7b 0a 20 20 20 20 20  Bt==pBt ){.     
15e36 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
15e37 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74  _mutex_leave(mut
15e38 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20  exShared);.     
15e39 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
15e3a 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74  _mutex_leave(mut
15e3b 65 78 4f 70 65 6e 29 3b 0a 20 20 20 20 20 20 20  exOpen);.       
15e3c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66         sqlite3_f
15e3d 72 65 65 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d  ree(zFullPathnam
15e3e 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  e);.            
15e3f 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
15e40 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
15e41 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
15e42 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 20  ONSTRAINT;.     
15e43 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
15e44 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70     }.          p
15e45 2d 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 20  ->pBt = pBt;.   
15e46 20 20 20 20 20 20 20 70 42 74 2d 3e 6e 52 65 66         pBt->nRef
15e47 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  ++;.          br
15e48 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
15e49 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
15e4a 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
15e4b 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20  (mutexShared);. 
15e4c 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
15e4d 65 28 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29  e(zFullPathname)
15e4e 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53  ;.    }.#ifdef S
15e4f 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20  QLITE_DEBUG.    
15e50 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49  else{.      /* I
15e51 6e 20 64 65 62 75 67 20 6d 6f 64 65 2c 20 77 65  n debug mode, we
15e52 20 6d 61 72 6b 20 61 6c 6c 20 70 65 72 73 69 73   mark all persis
15e53 74 65 6e 74 20 64 61 74 61 62 61 73 65 73 20 61  tent databases a
15e54 73 20 73 68 61 72 61 62 6c 65 0a 20 20 20 20 20  s sharable.     
15e55 20 2a 2a 20 65 76 65 6e 20 77 68 65 6e 20 74 68   ** even when th
15e56 65 79 20 61 72 65 20 6e 6f 74 2e 20 20 54 68 69  ey are not.  Thi
15e57 73 20 65 78 65 72 63 69 73 65 73 20 74 68 65 20  s exercises the 
15e58 6c 6f 63 6b 69 6e 67 20 63 6f 64 65 20 61 6e 64  locking code and
15e59 0a 20 20 20 20 20 20 2a 2a 20 67 69 76 65 73 20  .      ** gives 
15e5a 6d 6f 72 65 20 6f 70 70 6f 72 74 75 6e 69 74 79  more opportunity
15e5b 20 66 6f 72 20 61 73 73 65 72 74 73 28 73 71 6c   for asserts(sql
15e5c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
15e5d 29 29 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74  )).      ** stat
15e5e 65 6d 65 6e 74 73 20 74 6f 20 66 69 6e 64 20 6c  ements to find l
15e5f 6f 63 6b 69 6e 67 20 70 72 6f 62 6c 65 6d 73 2e  ocking problems.
15e60 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
15e61 70 2d 3e 73 68 61 72 61 62 6c 65 20 3d 20 31 3b  p->sharable = 1;
15e62 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
15e63 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 70  }.#endif.  if( p
15e64 42 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 0a  Bt==0 ){.    /*.
15e65 20 20 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f      ** The follo
15e66 77 69 6e 67 20 61 73 73 65 72 74 73 20 6d 61 6b  wing asserts mak
15e67 65 20 73 75 72 65 20 74 68 61 74 20 73 74 72 75  e sure that stru
15e68 63 74 75 72 65 73 20 75 73 65 64 20 62 79 20 74  ctures used by t
15e69 68 65 20 62 74 72 65 65 20 61 72 65 0a 20 20 20  he btree are.   
15e6a 20 2a 2a 20 74 68 65 20 72 69 67 68 74 20 73 69   ** the right si
15e6b 7a 65 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20  ze.  This is to 
15e6c 67 75 61 72 64 20 61 67 61 69 6e 73 74 20 73 69  guard against si
15e6d 7a 65 20 63 68 61 6e 67 65 73 20 74 68 61 74 20  ze changes that 
15e6e 72 65 73 75 6c 74 0a 20 20 20 20 2a 2a 20 77 68  result.    ** wh
15e6f 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 6f 6e 20  en compiling on 
15e70 61 20 64 69 66 66 65 72 65 6e 74 20 61 72 63 68  a different arch
15e71 69 74 65 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f  itecture..    */
15e72 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a  .    assert( siz
15e73 65 6f 66 28 69 36 34 29 3d 3d 38 20 7c 7c 20 73  eof(i64)==8 || s
15e74 69 7a 65 6f 66 28 69 36 34 29 3d 3d 34 20 29 3b  izeof(i64)==4 );
15e75 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a  .    assert( siz
15e76 65 6f 66 28 75 36 34 29 3d 3d 38 20 7c 7c 20 73  eof(u64)==8 || s
15e77 69 7a 65 6f 66 28 75 36 34 29 3d 3d 34 20 29 3b  izeof(u64)==4 );
15e78 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a  .    assert( siz
15e79 65 6f 66 28 75 33 32 29 3d 3d 34 20 29 3b 0a 20  eof(u32)==4 );. 
15e7a 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f     assert( sizeo
15e7b 66 28 75 31 36 29 3d 3d 32 20 29 3b 0a 20 20 20  f(u16)==2 );.   
15e7c 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28   assert( sizeof(
15e7d 50 67 6e 6f 29 3d 3d 34 20 29 3b 0a 20 20 0a 20  Pgno)==4 );.  . 
15e7e 20 20 20 70 42 74 20 3d 20 73 71 6c 69 74 65 33     pBt = sqlite3
15e7f 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65  MallocZero( size
15e80 6f 66 28 2a 70 42 74 29 20 29 3b 0a 20 20 20 20  of(*pBt) );.    
15e81 69 66 28 20 70 42 74 3d 3d 30 20 29 7b 0a 20 20  if( pBt==0 ){.  
15e82 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
15e83 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67 6f 74  NOMEM;.      got
15e84 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74  o btree_open_out
15e85 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
15e86 20 73 71 6c 69 74 65 33 50 61 67 65 72 4f 70 65   sqlite3PagerOpe
15e87 6e 28 70 56 66 73 2c 20 26 70 42 74 2d 3e 70 50  n(pVfs, &pBt->pP
15e88 61 67 65 72 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c  ager, zFilename,
15e89 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
15e8a 20 20 20 20 20 20 20 20 20 20 20 45 58 54 52 41             EXTRA
15e8b 5f 53 49 5a 45 2c 20 66 6c 61 67 73 2c 20 76 66  _SIZE, flags, vf
15e8c 73 46 6c 61 67 73 2c 20 70 61 67 65 52 65 69 6e  sFlags, pageRein
15e8d 69 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  it);.    if( rc=
15e8e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
15e8f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
15e90 50 61 67 65 72 52 65 61 64 46 69 6c 65 68 65 61  PagerReadFilehea
15e91 64 65 72 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  der(pBt->pPager,
15e92 73 69 7a 65 6f 66 28 7a 44 62 48 65 61 64 65 72  sizeof(zDbHeader
15e93 29 2c 7a 44 62 48 65 61 64 65 72 29 3b 0a 20 20  ),zDbHeader);.  
15e94 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d    }.    if( rc!=
15e95 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
15e96 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70     goto btree_op
15e97 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  en_out;.    }.  
15e98 20 20 70 42 74 2d 3e 64 62 20 3d 20 64 62 3b 0a    pBt->db = db;.
15e99 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
15e9a 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 70  SetBusyhandler(p
15e9b 42 74 2d 3e 70 50 61 67 65 72 2c 20 62 74 72 65  Bt->pPager, btre
15e9c 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c  eInvokeBusyHandl
15e9d 65 72 2c 20 70 42 74 29 3b 0a 20 20 20 20 70 2d  er, pBt);.    p-
15e9e 3e 70 42 74 20 3d 20 70 42 74 3b 0a 20 20 0a 20  >pBt = pBt;.  . 
15e9f 20 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 20     pBt->pCursor 
15ea0 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 70 50  = 0;.    pBt->pP
15ea1 61 67 65 31 20 3d 20 30 3b 0a 20 20 20 20 70 42  age1 = 0;.    pB
15ea2 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 73 71  t->readOnly = sq
15ea3 6c 69 74 65 33 50 61 67 65 72 49 73 72 65 61 64  lite3PagerIsread
15ea4 6f 6e 6c 79 28 70 42 74 2d 3e 70 50 61 67 65 72  only(pBt->pPager
15ea5 29 3b 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 65  );.    pBt->page
15ea6 53 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28  Size = get2byte(
15ea7 26 7a 44 62 48 65 61 64 65 72 5b 31 36 5d 29 3b  &zDbHeader[16]);
15ea8 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 70 61  .    if( pBt->pa
15ea9 67 65 53 69 7a 65 3c 35 31 32 20 7c 7c 20 70 42  geSize<512 || pB
15eaa 74 2d 3e 70 61 67 65 53 69 7a 65 3e 53 51 4c 49  t->pageSize>SQLI
15eab 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45  TE_MAX_PAGE_SIZE
15eac 0a 20 20 20 20 20 20 20 20 20 7c 7c 20 28 28 70  .         || ((p
15ead 42 74 2d 3e 70 61 67 65 53 69 7a 65 2d 31 29 26  Bt->pageSize-1)&
15eae 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 21 3d  pBt->pageSize)!=
15eaf 30 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e  0 ){.      pBt->
15eb0 70 61 67 65 53 69 7a 65 20 3d 20 30 3b 0a 23 69  pageSize = 0;.#i
15eb1 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
15eb2 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
15eb3 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6d 61 67     /* If the mag
15eb4 69 63 20 6e 61 6d 65 20 22 3a 6d 65 6d 6f 72 79  ic name ":memory
15eb5 3a 22 20 77 69 6c 6c 20 63 72 65 61 74 65 20 61  :" will create a
15eb6 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  n in-memory data
15eb7 62 61 73 65 2c 20 74 68 65 6e 0a 20 20 20 20 20  base, then.     
15eb8 20 2a 2a 20 6c 65 61 76 65 20 74 68 65 20 61 75   ** leave the au
15eb9 74 6f 56 61 63 75 75 6d 20 6d 6f 64 65 20 61 74  toVacuum mode at
15eba 20 30 20 28 64 6f 20 6e 6f 74 20 61 75 74 6f 2d   0 (do not auto-
15ebb 76 61 63 75 75 6d 29 2c 20 65 76 65 6e 20 69 66  vacuum), even if
15ebc 0a 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45  .      ** SQLITE
15ebd 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43  _DEFAULT_AUTOVAC
15ebe 55 55 4d 20 69 73 20 74 72 75 65 2e 20 4f 6e 20  UUM is true. On 
15ebf 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c 20  the other hand, 
15ec0 69 66 0a 20 20 20 20 20 20 2a 2a 20 53 51 4c 49  if.      ** SQLI
15ec1 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59 44 42  TE_OMIT_MEMORYDB
15ec2 20 68 61 73 20 62 65 65 6e 20 64 65 66 69 6e 65   has been define
15ec3 64 2c 20 74 68 65 6e 20 22 3a 6d 65 6d 6f 72 79  d, then ":memory
15ec4 3a 22 20 69 73 20 6a 75 73 74 20 61 0a 20 20 20  :" is just a.   
15ec5 20 20 20 2a 2a 20 72 65 67 75 6c 61 72 20 66 69     ** regular fi
15ec6 6c 65 2d 6e 61 6d 65 2e 20 49 6e 20 74 68 69 73  le-name. In this
15ec7 20 63 61 73 65 20 74 68 65 20 61 75 74 6f 2d 76   case the auto-v
15ec8 61 63 75 75 6d 20 61 70 70 6c 69 65 73 20 61 73  acuum applies as
15ec9 20 70 65 72 20 6e 6f 72 6d 61 6c 2e 0a 20 20 20   per normal..   
15eca 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
15ecb 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 21 69 73  zFilename && !is
15ecc 4d 65 6d 64 62 20 29 7b 0a 20 20 20 20 20 20 20  Memdb ){.       
15ecd 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
15ece 20 3d 20 28 53 51 4c 49 54 45 5f 44 45 46 41 55   = (SQLITE_DEFAU
15ecf 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 3f 20  LT_AUTOVACUUM ? 
15ed0 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 20 20  1 : 0);.        
15ed1 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20  pBt->incrVacuum 
15ed2 3d 20 28 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  = (SQLITE_DEFAUL
15ed3 54 5f 41 55 54 4f 56 41 43 55 55 4d 3d 3d 32 20  T_AUTOVACUUM==2 
15ed4 3f 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 20  ? 1 : 0);.      
15ed5 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 6e  }.#endif.      n
15ed6 52 65 73 65 72 76 65 20 3d 20 30 3b 0a 20 20 20  Reserve = 0;.   
15ed7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 52   }else{.      nR
15ed8 65 73 65 72 76 65 20 3d 20 7a 44 62 48 65 61 64  eserve = zDbHead
15ed9 65 72 5b 32 30 5d 3b 0a 20 20 20 20 20 20 70 42  er[20];.      pB
15eda 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64  t->pageSizeFixed
15edb 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51   = 1;.#ifndef SQ
15edc 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
15edd 43 55 55 4d 0a 20 20 20 20 20 20 70 42 74 2d 3e  CUUM.      pBt->
15ede 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 67 65  autoVacuum = (ge
15edf 74 34 62 79 74 65 28 26 7a 44 62 48 65 61 64 65  t4byte(&zDbHeade
15ee0 72 5b 33 36 20 2b 20 34 2a 34 5d 29 3f 31 3a 30  r[36 + 4*4])?1:0
15ee1 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e  );.      pBt->in
15ee2 63 72 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34  crVacuum = (get4
15ee3 62 79 74 65 28 26 7a 44 62 48 65 61 64 65 72 5b  byte(&zDbHeader[
15ee4 33 36 20 2b 20 37 2a 34 5d 29 3f 31 3a 30 29 3b  36 + 7*4])?1:0);
15ee5 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20  .#endif.    }.  
15ee6 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
15ee7 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70  gerSetPagesize(p
15ee8 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74  Bt->pPager, &pBt
15ee9 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6e 52 65 73  ->pageSize, nRes
15eea 65 72 76 65 29 3b 0a 20 20 20 20 69 66 28 20 72  erve);.    if( r
15eeb 63 20 29 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f  c ) goto btree_o
15eec 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 70 42 74  pen_out;.    pBt
15eed 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70  ->usableSize = p
15eee 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 6e  Bt->pageSize - n
15eef 52 65 73 65 72 76 65 3b 0a 20 20 20 20 61 73 73  Reserve;.    ass
15ef0 65 72 74 28 20 28 70 42 74 2d 3e 70 61 67 65 53  ert( (pBt->pageS
15ef1 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 20 20  ize & 7)==0 );  
15ef2 2f 2a 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 6d  /* 8-byte alignm
15ef3 65 6e 74 20 6f 66 20 70 61 67 65 53 69 7a 65 20  ent of pageSize 
15ef4 2a 2f 0a 20 20 20 0a 23 69 66 20 21 64 65 66 69  */.   .#if !defi
15ef5 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
15ef6 53 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26  SHARED_CACHE) &&
15ef7 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
15ef8 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20  _OMIT_DISKIO).  
15ef9 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6e 65 77    /* Add the new
15efa 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74   BtShared object
15efb 20 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c   to the linked l
15efc 69 73 74 20 73 68 61 72 61 62 6c 65 20 42 74 53  ist sharable BtS
15efd 68 61 72 65 64 73 2e 0a 20 20 20 20 2a 2f 0a 20  hareds..    */. 
15efe 20 20 20 69 66 28 20 70 2d 3e 73 68 61 72 61 62     if( p->sharab
15eff 6c 65 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  le ){.      sqli
15f00 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
15f01 53 68 61 72 65 64 3b 0a 20 20 20 20 20 20 70 42  Shared;.      pB
15f02 74 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20  t->nRef = 1;.   
15f03 20 20 20 6d 75 74 65 78 53 68 61 72 65 64 20 3d     mutexShared =
15f04 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
15f05 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
15f06 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a  STATIC_MASTER);.
15f07 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45        if( SQLITE
15f08 5f 54 48 52 45 41 44 53 41 46 45 20 26 26 20 73  _THREADSAFE && s
15f09 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
15f0a 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29 7b  ig.bCoreMutex ){
15f0b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e 6d 75  .        pBt->mu
15f0c 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  tex = sqlite3Mut
15f0d 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
15f0e 55 54 45 58 5f 46 41 53 54 29 3b 0a 20 20 20 20  UTEX_FAST);.    
15f0f 20 20 20 20 69 66 28 20 70 42 74 2d 3e 6d 75 74      if( pBt->mut
15f10 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ex==0 ){.       
15f11 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
15f12 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20  OMEM;.          
15f13 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
15f14 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
15f15 67 6f 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f  goto btree_open_
15f16 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  out;.        }. 
15f17 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
15f18 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
15f19 28 6d 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20  (mutexShared);. 
15f1a 20 20 20 20 20 70 42 74 2d 3e 70 4e 65 78 74 20       pBt->pNext 
15f1b 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65  = GLOBAL(BtShare
15f1c 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64  d*,sqlite3Shared
15f1d 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 20 20 20  CacheList);.    
15f1e 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65    GLOBAL(BtShare
15f1f 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64  d*,sqlite3Shared
15f20 43 61 63 68 65 4c 69 73 74 29 20 3d 20 70 42 74  CacheList) = pBt
15f21 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
15f22 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65  mutex_leave(mute
15f23 78 53 68 61 72 65 64 29 3b 0a 20 20 20 20 7d 0a  xShared);.    }.
15f24 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 23 69 66 20  #endif.  }..#if 
15f25 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
15f26 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
15f27 45 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  E) && !defined(S
15f28 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49  QLITE_OMIT_DISKI
15f29 4f 29 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6e  O).  /* If the n
15f2a 65 77 20 42 74 72 65 65 20 75 73 65 73 20 61 20  ew Btree uses a 
15f2b 73 68 61 72 61 62 6c 65 20 70 42 74 53 68 61 72  sharable pBtShar
15f2c 65 64 2c 20 74 68 65 6e 20 6c 69 6e 6b 20 74 68  ed, then link th
15f2d 65 20 6e 65 77 0a 20 20 2a 2a 20 42 74 72 65 65  e new.  ** Btree
15f2e 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74 20 6f   into the list o
15f2f 66 20 61 6c 6c 20 73 68 61 72 61 62 6c 65 20 42  f all sharable B
15f30 74 72 65 65 73 20 66 6f 72 20 74 68 65 20 73 61  trees for the sa
15f31 6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 20  me connection.. 
15f32 20 2a 2a 20 54 68 65 20 6c 69 73 74 20 69 73 20   ** The list is 
15f33 6b 65 70 74 20 69 6e 20 61 73 63 65 6e 64 69 6e  kept in ascendin
15f34 67 20 6f 72 64 65 72 20 62 79 20 70 42 74 20 61  g order by pBt a
15f35 64 64 72 65 73 73 2e 0a 20 20 2a 2f 0a 20 20 69  ddress..  */.  i
15f36 66 28 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29  f( p->sharable )
15f37 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  {.    int i;.   
15f38 20 42 74 72 65 65 20 2a 70 53 69 62 3b 0a 20 20   Btree *pSib;.  
15f39 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
15f3a 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
15f3b 20 20 69 66 28 20 28 70 53 69 62 20 3d 20 64 62    if( (pSib = db
15f3c 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 21 3d 30  ->aDb[i].pBt)!=0
15f3d 20 26 26 20 70 53 69 62 2d 3e 73 68 61 72 61 62   && pSib->sharab
15f3e 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 77 68  le ){.        wh
15f3f 69 6c 65 28 20 70 53 69 62 2d 3e 70 50 72 65 76  ile( pSib->pPrev
15f40 20 29 7b 20 70 53 69 62 20 3d 20 70 53 69 62 2d   ){ pSib = pSib-
15f41 3e 70 50 72 65 76 3b 20 7d 0a 20 20 20 20 20 20  >pPrev; }.      
15f42 20 20 69 66 28 20 70 2d 3e 70 42 74 3c 70 53 69    if( p->pBt<pSi
15f43 62 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20  b->pBt ){.      
15f44 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70      p->pNext = p
15f45 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20 20 70  Sib;.          p
15f46 2d 3e 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20  ->pPrev = 0;.   
15f47 20 20 20 20 20 20 20 70 53 69 62 2d 3e 70 50 72         pSib->pPr
15f48 65 76 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20  ev = p;.        
15f49 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
15f4a 20 77 68 69 6c 65 28 20 70 53 69 62 2d 3e 70 4e   while( pSib->pN
15f4b 65 78 74 20 26 26 20 70 53 69 62 2d 3e 70 4e 65  ext && pSib->pNe
15f4c 78 74 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20 29  xt->pBt<p->pBt )
15f4d 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 53  {.            pS
15f4e 69 62 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 74  ib = pSib->pNext
15f4f 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
15f50 20 20 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74          p->pNext
15f51 20 3d 20 70 53 69 62 2d 3e 70 4e 65 78 74 3b 0a   = pSib->pNext;.
15f52 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50 72            p->pPr
15f53 65 76 20 3d 20 70 53 69 62 3b 0a 20 20 20 20 20  ev = pSib;.     
15f54 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4e 65 78       if( p->pNex
15f55 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  t ){.           
15f56 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76   p->pNext->pPrev
15f57 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20   = p;.          
15f58 7d 0a 20 20 20 20 20 20 20 20 20 20 70 53 69 62  }.          pSib
15f59 2d 3e 70 4e 65 78 74 20 3d 20 70 3b 0a 20 20 20  ->pNext = p;.   
15f5a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62       }.        b
15f5b 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
15f5c 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20    }.  }.#endif. 
15f5d 20 2a 70 70 42 74 72 65 65 20 3d 20 70 3b 0a 0a   *ppBtree = p;..
15f5e 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3a 0a  btree_open_out:.
15f5f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
15f60 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70  _OK ){.    if( p
15f61 42 74 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65  Bt && pBt->pPage
15f62 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  r ){.      sqlit
15f63 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70 42 74  e3PagerClose(pBt
15f64 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d  ->pPager);.    }
15f65 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
15f66 65 28 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69  e(pBt);.    sqli
15f67 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 20  te3_free(p);.   
15f68 20 2a 70 70 42 74 72 65 65 20 3d 20 30 3b 0a 20   *ppBtree = 0;. 
15f69 20 7d 0a 20 20 69 66 28 20 6d 75 74 65 78 4f 70   }.  if( mutexOp
15f6a 65 6e 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  en ){.    assert
15f6b 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
15f6c 68 65 6c 64 28 6d 75 74 65 78 4f 70 65 6e 29 20  held(mutexOpen) 
15f6d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
15f6e 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78  utex_leave(mutex
15f6f 4f 70 65 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74  Open);.  }.  ret
15f70 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
15f71 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65 20 42   Decrement the B
15f72 74 53 68 61 72 65 64 2e 6e 52 65 66 20 63 6f 75  tShared.nRef cou
15f73 6e 74 65 72 2e 20 20 57 68 65 6e 20 69 74 20 72  nter.  When it r
15f74 65 61 63 68 65 73 20 7a 65 72 6f 2c 0a 2a 2a 20  eaches zero,.** 
15f75 72 65 6d 6f 76 65 20 74 68 65 20 42 74 53 68 61  remove the BtSha
15f76 72 65 64 20 73 74 72 75 63 74 75 72 65 20 66 72  red structure fr
15f77 6f 6d 20 74 68 65 20 73 68 61 72 69 6e 67 20 6c  om the sharing l
15f78 69 73 74 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20  ist.  Return.** 
15f79 74 72 75 65 20 69 66 20 74 68 65 20 42 74 53 68  true if the BtSh
15f7a 61 72 65 64 2e 6e 52 65 66 20 63 6f 75 6e 74 65  ared.nRef counte
15f7b 72 20 72 65 61 63 68 65 73 20 7a 65 72 6f 20 61  r reaches zero a
15f7c 6e 64 20 72 65 74 75 72 6e 0a 2a 2a 20 66 61 6c  nd return.** fal
15f7d 73 65 20 69 66 20 69 74 20 69 73 20 73 74 69 6c  se if it is stil
15f7e 6c 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2f 0a 73  l positive..*/.s
15f7f 74 61 74 69 63 20 69 6e 74 20 72 65 6d 6f 76 65  tatic int remove
15f80 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69 73 74 28  FromSharingList(
15f81 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a  BtShared *pBt){.
15f82 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
15f83 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
15f84 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
15f85 20 2a 70 4d 61 73 74 65 72 3b 0a 20 20 42 74 53   *pMaster;.  BtS
15f86 68 61 72 65 64 20 2a 70 4c 69 73 74 3b 0a 20 20  hared *pList;.  
15f87 69 6e 74 20 72 65 6d 6f 76 65 64 20 3d 20 30 3b  int removed = 0;
15f88 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
15f89 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c  te3_mutex_nothel
15f8a 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
15f8b 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 73 71 6c  .  pMaster = sql
15f8c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
15f8d 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
15f8e 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73 71  IC_MASTER);.  sq
15f8f 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
15f90 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 70 42  r(pMaster);.  pB
15f91 74 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28  t->nRef--;.  if(
15f92 20 70 42 74 2d 3e 6e 52 65 66 3c 3d 30 20 29 7b   pBt->nRef<=0 ){
15f93 0a 20 20 20 20 69 66 28 20 47 4c 4f 42 41 4c 28  .    if( GLOBAL(
15f94 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69 74 65  BtShared*,sqlite
15f95 33 53 68 61 72 65 64 43 61 63 68 65 4c 69 73 74  3SharedCacheList
15f96 29 3d 3d 70 42 74 20 29 7b 0a 20 20 20 20 20 20  )==pBt ){.      
15f97 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a  GLOBAL(BtShared*
15f98 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61  ,sqlite3SharedCa
15f99 63 68 65 4c 69 73 74 29 20 3d 20 70 42 74 2d 3e  cheList) = pBt->
15f9a 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65  pNext;.    }else
15f9b 7b 0a 20 20 20 20 20 20 70 4c 69 73 74 20 3d 20  {.      pList = 
15f9c 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65 64 2a  GLOBAL(BtShared*
15f9d 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61  ,sqlite3SharedCa
15f9e 63 68 65 4c 69 73 74 29 3b 0a 20 20 20 20 20 20  cheList);.      
15f9f 77 68 69 6c 65 28 20 41 4c 57 41 59 53 28 70 4c  while( ALWAYS(pL
15fa0 69 73 74 29 20 26 26 20 70 4c 69 73 74 2d 3e 70  ist) && pList->p
15fa1 4e 65 78 74 21 3d 70 42 74 20 29 7b 0a 20 20 20  Next!=pBt ){.   
15fa2 20 20 20 20 20 70 4c 69 73 74 3d 70 4c 69 73 74       pList=pList
15fa3 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d  ->pNext;.      }
15fa4 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57 41 59  .      if( ALWAY
15fa5 53 28 70 4c 69 73 74 29 20 29 7b 0a 20 20 20 20  S(pList) ){.    
15fa6 20 20 20 20 70 4c 69 73 74 2d 3e 70 4e 65 78 74      pList->pNext
15fa7 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b 0a 20   = pBt->pNext;. 
15fa8 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
15fa9 20 69 66 28 20 53 51 4c 49 54 45 5f 54 48 52 45   if( SQLITE_THRE
15faa 41 44 53 41 46 45 20 29 7b 0a 20 20 20 20 20 20  ADSAFE ){.      
15fab 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72  sqlite3_mutex_fr
15fac 65 65 28 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a  ee(pBt->mutex);.
15fad 20 20 20 20 7d 0a 20 20 20 20 72 65 6d 6f 76 65      }.    remove
15fae 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73 71 6c  d = 1;.  }.  sql
15faf 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
15fb0 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 74  (pMaster);.  ret
15fb1 75 72 6e 20 72 65 6d 6f 76 65 64 3b 0a 23 65 6c  urn removed;.#el
15fb2 73 65 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 23  se.  return 1;.#
15fb3 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  endif.}../*.** M
15fb4 61 6b 65 20 73 75 72 65 20 70 42 74 2d 3e 70 54  ake sure pBt->pT
15fb5 6d 70 53 70 61 63 65 20 70 6f 69 6e 74 73 20 74  mpSpace points t
15fb6 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  o an allocation 
15fb7 6f 66 20 0a 2a 2a 20 4d 58 5f 43 45 4c 4c 5f 53  of .** MX_CELL_S
15fb8 49 5a 45 28 70 42 74 29 20 62 79 74 65 73 2e 0a  IZE(pBt) bytes..
15fb9 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
15fba 6c 6c 6f 63 61 74 65 54 65 6d 70 53 70 61 63 65  llocateTempSpace
15fbb 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b  (BtShared *pBt){
15fbc 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70 54 6d  .  if( !pBt->pTm
15fbd 70 53 70 61 63 65 20 29 7b 0a 20 20 20 20 70 42  pSpace ){.    pB
15fbe 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d 20 73  t->pTmpSpace = s
15fbf 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63  qlite3PageMalloc
15fc0 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20  ( pBt->pageSize 
15fc1 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
15fc2 46 72 65 65 20 74 68 65 20 70 42 74 2d 3e 70 54  Free the pBt->pT
15fc3 6d 70 53 70 61 63 65 20 61 6c 6c 6f 63 61 74 69  mpSpace allocati
15fc4 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  on.*/.static voi
15fc5 64 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 28  d freeTempSpace(
15fc6 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a  BtShared *pBt){.
15fc7 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65    sqlite3PageFre
15fc8 65 28 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63  e( pBt->pTmpSpac
15fc9 65 29 3b 0a 20 20 70 42 74 2d 3e 70 54 6d 70 53  e);.  pBt->pTmpS
15fca 70 61 63 65 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  pace = 0;.}../*.
15fcb 2a 2a 20 43 6c 6f 73 65 20 61 6e 20 6f 70 65 6e  ** Close an open
15fcc 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69 6e   database and in
15fcd 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 63 75 72  validate all cur
15fce 73 6f 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  sors..*/.SQLITE_
15fcf 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
15fd0 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 42 74  te3BtreeClose(Bt
15fd1 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61  ree *p){.  BtSha
15fd2 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
15fd3 74 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  t;.  BtCursor *p
15fd4 43 75 72 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65  Cur;..  /* Close
15fd5 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 6f 70 65   all cursors ope
15fd6 6e 65 64 20 76 69 61 20 74 68 69 73 20 68 61 6e  ned via this han
15fd7 64 6c 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72  dle.  */.  asser
15fd8 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
15fd9 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74  _held(p->db->mut
15fda 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ex) );.  sqlite3
15fdb 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20  BtreeEnter(p);. 
15fdc 20 70 43 75 72 20 3d 20 70 42 74 2d 3e 70 43 75   pCur = pBt->pCu
15fdd 72 73 6f 72 3b 0a 20 20 77 68 69 6c 65 28 20 70  rsor;.  while( p
15fde 43 75 72 20 29 7b 0a 20 20 20 20 42 74 43 75 72  Cur ){.    BtCur
15fdf 73 6f 72 20 2a 70 54 6d 70 20 3d 20 70 43 75 72  sor *pTmp = pCur
15fe0 3b 0a 20 20 20 20 70 43 75 72 20 3d 20 70 43 75  ;.    pCur = pCu
15fe1 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 69 66  r->pNext;.    if
15fe2 28 20 70 54 6d 70 2d 3e 70 42 74 72 65 65 3d 3d  ( pTmp->pBtree==
15fe3 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  p ){.      sqlit
15fe4 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72 73  e3BtreeCloseCurs
15fe5 6f 72 28 70 54 6d 70 29 3b 0a 20 20 20 20 7d 0a  or(pTmp);.    }.
15fe6 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f 6c 6c 62 61    }..  /* Rollba
15fe7 63 6b 20 61 6e 79 20 61 63 74 69 76 65 20 74 72  ck any active tr
15fe8 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64 20 66 72  ansaction and fr
15fe9 65 65 20 74 68 65 20 68 61 6e 64 6c 65 20 73 74  ee the handle st
15fea 72 75 63 74 75 72 65 2e 0a 20 20 2a 2a 20 54 68  ructure..  ** Th
15feb 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  e call to sqlite
15fec 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 29  3BtreeRollback()
15fed 20 64 72 6f 70 73 20 61 6e 79 20 74 61 62 6c 65   drops any table
15fee 2d 6c 6f 63 6b 73 20 68 65 6c 64 20 62 79 0a 20  -locks held by. 
15fef 20 2a 2a 20 74 68 69 73 20 68 61 6e 64 6c 65 2e   ** this handle.
15ff0 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42  .  */.  sqlite3B
15ff1 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70 29 3b  treeRollback(p);
15ff2 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
15ff3 65 61 76 65 28 70 29 3b 0a 0a 20 20 2f 2a 20 49  eave(p);..  /* I
15ff4 66 20 74 68 65 72 65 20 61 72 65 20 73 74 69 6c  f there are stil
15ff5 6c 20 6f 74 68 65 72 20 6f 75 74 73 74 61 6e 64  l other outstand
15ff6 69 6e 67 20 72 65 66 65 72 65 6e 63 65 73 20 74  ing references t
15ff7 6f 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72  o the shared-btr
15ff8 65 65 0a 20 20 2a 2a 20 73 74 72 75 63 74 75 72  ee.  ** structur
15ff9 65 2c 20 72 65 74 75 72 6e 20 6e 6f 77 2e 20 54  e, return now. T
15ffa 68 65 20 72 65 6d 61 69 6e 64 65 72 20 6f 66 20  he remainder of 
15ffb 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 20 63  this procedure c
15ffc 6c 65 61 6e 73 20 0a 20 20 2a 2a 20 75 70 20 74  leans .  ** up t
15ffd 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65 2e  he shared-btree.
15ffe 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
15fff 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30  p->wantToLock==0
16000 20 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30   && p->locked==0
16001 20 29 3b 0a 20 20 69 66 28 20 21 70 2d 3e 73 68   );.  if( !p->sh
16002 61 72 61 62 6c 65 20 7c 7c 20 72 65 6d 6f 76 65  arable || remove
16003 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69 73 74 28  FromSharingList(
16004 70 42 74 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54  pBt) ){.    /* T
16005 68 65 20 70 42 74 20 69 73 20 6e 6f 20 6c 6f 6e  he pBt is no lon
16006 67 65 72 20 6f 6e 20 74 68 65 20 73 68 61 72 69  ger on the shari
16007 6e 67 20 6c 69 73 74 2c 20 73 6f 20 77 65 20 63  ng list, so we c
16008 61 6e 20 61 63 63 65 73 73 0a 20 20 20 20 2a 2a  an access.    **
16009 20 69 74 20 77 69 74 68 6f 75 74 20 68 61 76 69   it without havi
1600a 6e 67 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d  ng to hold the m
1600b 75 74 65 78 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  utex..    **.   
1600c 20 2a 2a 20 43 6c 65 61 6e 20 6f 75 74 20 61 6e   ** Clean out an
1600d 64 20 64 65 6c 65 74 65 20 74 68 65 20 42 74 53  d delete the BtS
1600e 68 61 72 65 64 20 6f 62 6a 65 63 74 2e 0a 20 20  hared object..  
1600f 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
16010 20 21 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 29   !pBt->pCursor )
16011 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67  ;.    sqlite3Pag
16012 65 72 43 6c 6f 73 65 28 70 42 74 2d 3e 70 50 61  erClose(pBt->pPa
16013 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 70 42  ger);.    if( pB
16014 74 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 20 26  t->xFreeSchema &
16015 26 20 70 42 74 2d 3e 70 53 63 68 65 6d 61 20 29  & pBt->pSchema )
16016 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 78 46 72  {.      pBt->xFr
16017 65 65 53 63 68 65 6d 61 28 70 42 74 2d 3e 70 53  eeSchema(pBt->pS
16018 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 20 20  chema);.    }.  
16019 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
1601a 42 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  Bt->pSchema);.  
1601b 20 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 28    freeTempSpace(
1601c 70 42 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  pBt);.    sqlite
1601d 33 5f 66 72 65 65 28 70 42 74 29 3b 0a 20 20 7d  3_free(pBt);.  }
1601e 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1601f 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
16020 48 45 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  HE.  assert( p->
16021 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 3b  wantToLock==0 );
16022 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f  .  assert( p->lo
16023 63 6b 65 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28  cked==0 );.  if(
16024 20 70 2d 3e 70 50 72 65 76 20 29 20 70 2d 3e 70   p->pPrev ) p->p
16025 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 2d  Prev->pNext = p-
16026 3e 70 4e 65 78 74 3b 0a 20 20 69 66 28 20 70 2d  >pNext;.  if( p-
16027 3e 70 4e 65 78 74 20 29 20 70 2d 3e 70 4e 65 78  >pNext ) p->pNex
16028 74 2d 3e 70 50 72 65 76 20 3d 20 70 2d 3e 70 50  t->pPrev = p->pP
16029 72 65 76 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73  rev;.#endif..  s
1602a 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a  qlite3_free(p);.
1602b 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1602c 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  OK;.}../*.** Cha
1602d 6e 67 65 20 74 68 65 20 6c 69 6d 69 74 20 6f 6e  nge the limit on
1602e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
1602f 61 67 65 73 20 61 6c 6c 6f 77 65 64 20 69 6e 20  ages allowed in 
16030 74 68 65 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a  the cache..**.**
16031 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   The maximum num
16032 62 65 72 20 6f 66 20 63 61 63 68 65 20 70 61 67  ber of cache pag
16033 65 73 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  es is set to the
16034 20 61 62 73 6f 6c 75 74 65 0a 2a 2a 20 76 61 6c   absolute.** val
16035 75 65 20 6f 66 20 6d 78 50 61 67 65 2e 20 20 49  ue of mxPage.  I
16036 66 20 6d 78 50 61 67 65 20 69 73 20 6e 65 67 61  f mxPage is nega
16037 74 69 76 65 2c 20 74 68 65 20 70 61 67 65 72 20  tive, the pager 
16038 77 69 6c 6c 0a 2a 2a 20 6f 70 65 72 61 74 65 20  will.** operate 
16039 61 73 79 6e 63 68 72 6f 6e 6f 75 73 6c 79 20 2d  asynchronously -
1603a 20 69 74 20 77 69 6c 6c 20 6e 6f 74 20 73 74 6f   it will not sto
1603b 70 20 74 6f 20 64 6f 20 66 73 79 6e 63 28 29 73  p to do fsync()s
1603c 0a 2a 2a 20 74 6f 20 69 6e 73 75 72 65 20 64 61  .** to insure da
1603d 74 61 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f  ta is written to
1603e 20 74 68 65 20 64 69 73 6b 20 73 75 72 66 61 63   the disk surfac
1603f 65 20 62 65 66 6f 72 65 0a 2a 2a 20 63 6f 6e 74  e before.** cont
16040 69 6e 75 69 6e 67 2e 20 20 54 72 61 6e 73 61 63  inuing.  Transac
16041 74 69 6f 6e 73 20 73 74 69 6c 6c 20 77 6f 72 6b  tions still work
16042 20 69 66 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20   if synchronous 
16043 69 73 20 6f 66 66 2c 0a 2a 2a 20 61 6e 64 20 74  is off,.** and t
16044 68 65 20 64 61 74 61 62 61 73 65 20 63 61 6e 6e  he database cann
16045 6f 74 20 62 65 20 63 6f 72 72 75 70 74 65 64 20  ot be corrupted 
16046 69 66 20 74 68 69 73 20 70 72 6f 67 72 61 6d 0a  if this program.
16047 2a 2a 20 63 72 61 73 68 65 73 2e 20 20 42 75 74  ** crashes.  But
16048 20 69 66 20 74 68 65 20 6f 70 65 72 61 74 69 6e   if the operatin
16049 67 20 73 79 73 74 65 6d 20 63 72 61 73 68 65 73  g system crashes
1604a 20 6f 72 20 74 68 65 72 65 20 69 73 0a 2a 2a 20   or there is.** 
1604b 61 6e 20 61 62 72 75 70 74 20 70 6f 77 65 72 20  an abrupt power 
1604c 66 61 69 6c 75 72 65 20 77 68 65 6e 20 73 79 6e  failure when syn
1604d 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 66 66 2c  chronous is off,
1604e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
1604f 20 63 6f 75 6c 64 20 62 65 20 6c 65 66 74 20 69   could be left i
16050 6e 20 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e  n an inconsisten
16051 74 20 61 6e 64 20 75 6e 72 65 63 6f 76 65 72 61  t and unrecovera
16052 62 6c 65 20 73 74 61 74 65 2e 0a 2a 2a 20 53 79  ble state..** Sy
16053 6e 63 68 72 6f 6e 6f 75 73 20 69 73 20 6f 6e 20  nchronous is on 
16054 62 79 20 64 65 66 61 75 6c 74 20 73 6f 20 64 61  by default so da
16055 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f  tabase corruptio
16056 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20 6e 6f 72 6d  n is not.** norm
16057 61 6c 6c 79 20 61 20 77 6f 72 72 79 2e 0a 2a 2f  ally a worry..*/
16058 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16059 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
1605a 53 65 74 43 61 63 68 65 53 69 7a 65 28 42 74 72  SetCacheSize(Btr
1605b 65 65 20 2a 70 2c 20 69 6e 74 20 6d 78 50 61 67  ee *p, int mxPag
1605c 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  e){.  BtShared *
1605d 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
1605e 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1605f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62  mutex_held(p->db
16060 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71  ->mutex) );.  sq
16061 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
16062 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61 67  p);.  sqlite3Pag
16063 65 72 53 65 74 43 61 63 68 65 73 69 7a 65 28 70  erSetCachesize(p
16064 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d 78 50 61  Bt->pPager, mxPa
16065 67 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  ge);.  sqlite3Bt
16066 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
16067 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
16068 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  .}../*.** Change
16069 20 74 68 65 20 77 61 79 20 64 61 74 61 20 69 73   the way data is
1606a 20 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 20   synced to disk 
1606b 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 6e 63 72  in order to incr
1606c 65 61 73 65 20 6f 72 20 64 65 63 72 65 61 73 65  ease or decrease
1606d 0a 2a 2a 20 68 6f 77 20 77 65 6c 6c 20 74 68 65  .** how well the
1606e 20 64 61 74 61 62 61 73 65 20 72 65 73 69 73 74   database resist
1606f 73 20 64 61 6d 61 67 65 20 64 75 65 20 74 6f 20  s damage due to 
16070 4f 53 20 63 72 61 73 68 65 73 20 61 6e 64 20 70  OS crashes and p
16071 6f 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 73  ower.** failures
16072 2e 20 20 4c 65 76 65 6c 20 31 20 69 73 20 74 68  .  Level 1 is th
16073 65 20 73 61 6d 65 20 61 73 20 61 73 79 6e 63 68  e same as asynch
16074 72 6f 6e 6f 75 73 20 28 6e 6f 20 73 79 6e 63 73  ronous (no syncs
16075 28 29 20 6f 63 63 75 72 20 61 6e 64 0a 2a 2a 20  () occur and.** 
16076 74 68 65 72 65 20 69 73 20 61 20 68 69 67 68 20  there is a high 
16077 70 72 6f 62 61 62 69 6c 69 74 79 20 6f 66 20 64  probability of d
16078 61 6d 61 67 65 29 20 20 4c 65 76 65 6c 20 32 20  amage)  Level 2 
16079 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 2e 20  is the default. 
1607a 20 54 68 65 72 65 0a 2a 2a 20 69 73 20 61 20 76   There.** is a v
1607b 65 72 79 20 6c 6f 77 20 62 75 74 20 6e 6f 6e 2d  ery low but non-
1607c 7a 65 72 6f 20 70 72 6f 62 61 62 69 6c 69 74 79  zero probability
1607d 20 6f 66 20 64 61 6d 61 67 65 2e 20 20 4c 65 76   of damage.  Lev
1607e 65 6c 20 33 20 72 65 64 75 63 65 73 20 74 68 65  el 3 reduces the
1607f 0a 2a 2a 20 70 72 6f 62 61 62 69 6c 69 74 79 20  .** probability 
16080 6f 66 20 64 61 6d 61 67 65 20 74 6f 20 6e 65 61  of damage to nea
16081 72 20 7a 65 72 6f 20 62 75 74 20 77 69 74 68 20  r zero but with 
16082 61 20 77 72 69 74 65 20 70 65 72 66 6f 72 6d 61  a write performa
16083 6e 63 65 20 72 65 64 75 63 74 69 6f 6e 2e 0a 2a  nce reduction..*
16084 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
16085 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47  _OMIT_PAGER_PRAG
16086 4d 41 53 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  MAS.SQLITE_PRIVA
16087 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
16088 72 65 65 53 65 74 53 61 66 65 74 79 4c 65 76 65  reeSetSafetyLeve
16089 6c 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20  l(Btree *p, int 
1608a 6c 65 76 65 6c 2c 20 69 6e 74 20 66 75 6c 6c 53  level, int fullS
1608b 79 6e 63 29 7b 0a 20 20 42 74 53 68 61 72 65 64  ync){.  BtShared
1608c 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
1608d 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1608e 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
1608f 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
16090 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
16091 72 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 50  r(p);.  sqlite3P
16092 61 67 65 72 53 65 74 53 61 66 65 74 79 4c 65 76  agerSetSafetyLev
16093 65 6c 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  el(pBt->pPager, 
16094 6c 65 76 65 6c 2c 20 66 75 6c 6c 53 79 6e 63 29  level, fullSync)
16095 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
16096 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
16097 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
16098 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65  #endif../*.** Re
16099 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65  turn TRUE if the
1609a 20 67 69 76 65 6e 20 62 74 72 65 65 20 69 73 20   given btree is 
1609b 73 65 74 20 74 6f 20 73 61 66 65 74 79 20 6c 65  set to safety le
1609c 76 65 6c 20 31 2e 20 20 49 6e 20 6f 74 68 65 72  vel 1.  In other
1609d 0a 2a 2a 20 77 6f 72 64 73 2c 20 72 65 74 75 72  .** words, retur
1609e 6e 20 54 52 55 45 20 69 66 20 6e 6f 20 73 79 6e  n TRUE if no syn
1609f 63 28 29 20 6f 63 63 75 72 73 20 6f 6e 20 74 68  c() occurs on th
160a0 65 20 64 69 73 6b 20 66 69 6c 65 73 2e 0a 2a 2f  e disk files..*/
160a1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
160a2 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
160a3 53 79 6e 63 44 69 73 61 62 6c 65 64 28 42 74 72  SyncDisabled(Btr
160a4 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72  ee *p){.  BtShar
160a5 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
160a6 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73  ;.  int rc;.  as
160a7 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
160a8 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e  tex_held(p->db->
160a9 6d 75 74 65 78 29 20 29 3b 20 20 0a 20 20 73 71  mutex) );  .  sq
160aa 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
160ab 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42  p);.  assert( pB
160ac 74 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65 72  t && pBt->pPager
160ad 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   );.  rc = sqlit
160ae 65 33 50 61 67 65 72 4e 6f 73 79 6e 63 28 70 42  e3PagerNosync(pB
160af 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 73 71  t->pPager);.  sq
160b0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
160b1 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p);.  return rc;
160b2 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .}..#if !defined
160b3 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47  (SQLITE_OMIT_PAG
160b4 45 52 5f 50 52 41 47 4d 41 53 29 20 7c 7c 20 21  ER_PRAGMAS) || !
160b5 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
160b6 4d 49 54 5f 56 41 43 55 55 4d 29 0a 2f 2a 0a 2a  MIT_VACUUM)./*.*
160b7 2a 20 43 68 61 6e 67 65 20 74 68 65 20 64 65 66  * Change the def
160b8 61 75 6c 74 20 70 61 67 65 73 20 73 69 7a 65 20  ault pages size 
160b9 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  and the number o
160ba 66 20 72 65 73 65 72 76 65 64 20 62 79 74 65 73  f reserved bytes
160bb 20 70 65 72 20 70 61 67 65 2e 0a 2a 2a 20 4f 72   per page..** Or
160bc 2c 20 69 66 20 74 68 65 20 70 61 67 65 20 73 69  , if the page si
160bd 7a 65 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  ze has already b
160be 65 65 6e 20 66 69 78 65 64 2c 20 72 65 74 75 72  een fixed, retur
160bf 6e 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c  n SQLITE_READONL
160c0 59 20 0a 2a 2a 20 77 69 74 68 6f 75 74 20 63 68  Y .** without ch
160c1 61 6e 67 69 6e 67 20 61 6e 79 74 68 69 6e 67 2e  anging anything.
160c2 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20  .**.** The page 
160c3 73 69 7a 65 20 6d 75 73 74 20 62 65 20 61 20 70  size must be a p
160c4 6f 77 65 72 20 6f 66 20 32 20 62 65 74 77 65 65  ower of 2 betwee
160c5 6e 20 35 31 32 20 61 6e 64 20 36 35 35 33 36 2e  n 512 and 65536.
160c6 20 20 49 66 20 74 68 65 20 70 61 67 65 0a 2a 2a    If the page.**
160c7 20 73 69 7a 65 20 73 75 70 70 6c 69 65 64 20 64   size supplied d
160c8 6f 65 73 20 6e 6f 74 20 6d 65 65 74 20 74 68 69  oes not meet thi
160c9 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 68 65  s constraint the
160ca 6e 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20  n the page size 
160cb 69 73 20 6e 6f 74 0a 2a 2a 20 63 68 61 6e 67 65  is not.** change
160cc 64 2e 0a 2a 2a 0a 2a 2a 20 50 61 67 65 20 73 69  d..**.** Page si
160cd 7a 65 73 20 61 72 65 20 63 6f 6e 73 74 72 61 69  zes are constrai
160ce 6e 65 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65  ned to be a powe
160cf 72 20 6f 66 20 74 77 6f 20 73 6f 20 74 68 61 74  r of two so that
160d0 20 74 68 65 20 72 65 67 69 6f 6e 0a 2a 2a 20 6f   the region.** o
160d1 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
160d2 69 6c 65 20 75 73 65 64 20 66 6f 72 20 6c 6f 63  ile used for loc
160d3 6b 69 6e 67 20 28 62 65 67 69 6e 6e 69 6e 67 20  king (beginning 
160d4 61 74 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c  at PENDING_BYTE,
160d5 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 62 79  .** the first by
160d6 74 65 20 70 61 73 74 20 74 68 65 20 31 47 42 20  te past the 1GB 
160d7 62 6f 75 6e 64 61 72 79 2c 20 30 78 34 30 30 30  boundary, 0x4000
160d8 30 30 30 30 29 20 6e 65 65 64 73 20 74 6f 20 6f  0000) needs to o
160d9 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 20 62  ccur.** at the b
160da 65 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20 70 61  eginning of a pa
160db 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72  ge..**.** If par
160dc 61 6d 65 74 65 72 20 6e 52 65 73 65 72 76 65 20  ameter nReserve 
160dd 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72  is less than zer
160de 6f 2c 20 74 68 65 6e 20 74 68 65 20 6e 75 6d 62  o, then the numb
160df 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 0a 2a  er of reserved.*
160e0 2a 20 62 79 74 65 73 20 70 65 72 20 70 61 67 65  * bytes per page
160e1 20 69 73 20 6c 65 66 74 20 75 6e 63 68 61 6e 67   is left unchang
160e2 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
160e3 20 69 46 69 78 21 3d 30 20 74 68 65 6e 20 74 68   iFix!=0 then th
160e4 65 20 70 61 67 65 53 69 7a 65 46 69 78 65 64 20  e pageSizeFixed 
160e5 66 6c 61 67 20 69 73 20 73 65 74 20 73 6f 20 74  flag is set so t
160e6 68 61 74 20 74 68 65 20 70 61 67 65 20 73 69 7a  hat the page siz
160e7 65 0a 2a 2a 20 61 6e 64 20 61 75 74 6f 76 61 63  e.** and autovac
160e8 75 75 6d 20 6d 6f 64 65 20 63 61 6e 20 6e 6f 20  uum mode can no 
160e9 6c 6f 6e 67 65 72 20 62 65 20 63 68 61 6e 67 65  longer be change
160ea 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
160eb 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
160ec 42 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65  BtreeSetPageSize
160ed 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 70  (Btree *p, int p
160ee 61 67 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52 65  ageSize, int nRe
160ef 73 65 72 76 65 2c 20 69 6e 74 20 69 46 69 78 29  serve, int iFix)
160f0 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
160f1 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 53 68 61 72  ITE_OK;.  BtShar
160f2 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
160f3 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65 73  ;.  assert( nRes
160f4 65 72 76 65 3e 3d 2d 31 20 26 26 20 6e 52 65 73  erve>=-1 && nRes
160f5 65 72 76 65 3c 3d 32 35 35 20 29 3b 0a 20 20 73  erve<=255 );.  s
160f6 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
160f7 28 70 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e  (p);.  if( pBt->
160f8 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 29 7b  pageSizeFixed ){
160f9 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
160fa 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 72  eLeave(p);.    r
160fb 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41  eturn SQLITE_REA
160fc 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69 66 28  DONLY;.  }.  if(
160fd 20 6e 52 65 73 65 72 76 65 3c 30 20 29 7b 0a 20   nReserve<0 ){. 
160fe 20 20 20 6e 52 65 73 65 72 76 65 20 3d 20 70 42     nReserve = pB
160ff 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 42  t->pageSize - pB
16100 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20  t->usableSize;. 
16101 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65   }.  assert( nRe
16102 73 65 72 76 65 3e 3d 30 20 26 26 20 6e 52 65 73  serve>=0 && nRes
16103 65 72 76 65 3c 3d 32 35 35 20 29 3b 0a 20 20 69  erve<=255 );.  i
16104 66 28 20 70 61 67 65 53 69 7a 65 3e 3d 35 31 32  f( pageSize>=512
16105 20 26 26 20 70 61 67 65 53 69 7a 65 3c 3d 53 51   && pageSize<=SQ
16106 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49  LITE_MAX_PAGE_SI
16107 5a 45 20 26 26 0a 20 20 20 20 20 20 20 20 28 28  ZE &&.        ((
16108 70 61 67 65 53 69 7a 65 2d 31 29 26 70 61 67 65  pageSize-1)&page
16109 53 69 7a 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20  Size)==0 ){.    
1610a 61 73 73 65 72 74 28 20 28 70 61 67 65 53 69 7a  assert( (pageSiz
1610b 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20 20  e & 7)==0 );.   
1610c 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 70   assert( !pBt->p
1610d 50 61 67 65 31 20 26 26 20 21 70 42 74 2d 3e 70  Page1 && !pBt->p
1610e 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 70 42  Cursor );.    pB
1610f 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 28 75  t->pageSize = (u
16110 31 36 29 70 61 67 65 53 69 7a 65 3b 0a 20 20 20  16)pageSize;.   
16111 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 28 70   freeTempSpace(p
16112 42 74 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  Bt);.  }.  rc = 
16113 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50  sqlite3PagerSetP
16114 61 67 65 73 69 7a 65 28 70 42 74 2d 3e 70 50 61  agesize(pBt->pPa
16115 67 65 72 2c 20 26 70 42 74 2d 3e 70 61 67 65 53  ger, &pBt->pageS
16116 69 7a 65 2c 20 6e 52 65 73 65 72 76 65 29 3b 0a  ize, nReserve);.
16117 20 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a    pBt->usableSiz
16118 65 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  e = pBt->pageSiz
16119 65 20 2d 20 28 75 31 36 29 6e 52 65 73 65 72 76  e - (u16)nReserv
1611a 65 3b 0a 20 20 69 66 28 20 69 46 69 78 20 29 20  e;.  if( iFix ) 
1611b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78  pBt->pageSizeFix
1611c 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65  ed = 1;.  sqlite
1611d 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
1611e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1611f 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
16120 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e   currently defin
16121 65 64 20 70 61 67 65 20 73 69 7a 65 0a 2a 2f 0a  ed page size.*/.
16122 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
16123 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  nt sqlite3BtreeG
16124 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65  etPageSize(Btree
16125 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70   *p){.  return p
16126 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3b  ->pBt->pageSize;
16127 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
16128 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
16129 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 61 74  ytes of space at
1612a 20 74 68 65 20 65 6e 64 20 6f 66 20 65 76 65 72   the end of ever
1612b 79 20 70 61 67 65 20 74 68 61 74 0a 2a 2a 20 61  y page that.** a
1612c 72 65 20 69 6e 74 65 6e 74 75 61 6c 6c 79 20 6c  re intentually l
1612d 65 66 74 20 75 6e 75 73 65 64 2e 20 20 54 68 69  eft unused.  Thi
1612e 73 20 69 73 20 74 68 65 20 22 72 65 73 65 72 76  s is the "reserv
1612f 65 64 22 20 73 70 61 63 65 20 74 68 61 74 20 69  ed" space that i
16130 73 0a 2a 2a 20 73 6f 6d 65 74 69 6d 65 73 20 75  s.** sometimes u
16131 73 65 64 20 62 79 20 65 78 74 65 6e 73 69 6f 6e  sed by extension
16132 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
16133 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
16134 42 74 72 65 65 47 65 74 52 65 73 65 72 76 65 28  BtreeGetReserve(
16135 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74  Btree *p){.  int
16136 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72   n;.  sqlite3Btr
16137 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 6e 20  eeEnter(p);.  n 
16138 3d 20 70 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69  = p->pBt->pageSi
16139 7a 65 20 2d 20 70 2d 3e 70 42 74 2d 3e 75 73 61  ze - p->pBt->usa
1613a 62 6c 65 53 69 7a 65 3b 0a 20 20 73 71 6c 69 74  bleSize;.  sqlit
1613b 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
1613c 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a  .  return n;.}..
1613d 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 6d 61  /*.** Set the ma
1613e 78 69 6d 75 6d 20 70 61 67 65 20 63 6f 75 6e 74  ximum page count
1613f 20 66 6f 72 20 61 20 64 61 74 61 62 61 73 65 20   for a database 
16140 69 66 20 6d 78 50 61 67 65 20 69 73 20 70 6f 73  if mxPage is pos
16141 69 74 69 76 65 2e 0a 2a 2a 20 4e 6f 20 63 68 61  itive..** No cha
16142 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20 69 66  nges are made if
16143 20 6d 78 50 61 67 65 20 69 73 20 30 20 6f 72 20   mxPage is 0 or 
16144 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 20 52 65 67  negative..** Reg
16145 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20 76  ardless of the v
16146 61 6c 75 65 20 6f 66 20 6d 78 50 61 67 65 2c 20  alue of mxPage, 
16147 72 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d  return the maxim
16148 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a  um page count..*
16149 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1614a 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
1614b 65 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 42 74  eMaxPageCount(Bt
1614c 72 65 65 20 2a 70 2c 20 69 6e 74 20 6d 78 50 61  ree *p, int mxPa
1614d 67 65 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20  ge){.  int n;.  
1614e 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1614f 72 28 70 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69  r(p);.  n = sqli
16150 74 65 33 50 61 67 65 72 4d 61 78 50 61 67 65 43  te3PagerMaxPageC
16151 6f 75 6e 74 28 70 2d 3e 70 42 74 2d 3e 70 50 61  ount(p->pBt->pPa
16152 67 65 72 2c 20 6d 78 50 61 67 65 29 3b 0a 20 20  ger, mxPage);.  
16153 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
16154 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e  e(p);.  return n
16155 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  ;.}.#endif /* !d
16156 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
16157 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53  IT_PAGER_PRAGMAS
16158 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  ) || !defined(SQ
16159 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d  LITE_OMIT_VACUUM
1615a 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  ) */../*.** Chan
1615b 67 65 20 74 68 65 20 27 61 75 74 6f 2d 76 61 63  ge the 'auto-vac
1615c 75 75 6d 27 20 70 72 6f 70 65 72 74 79 20 6f 66  uum' property of
1615d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 49   the database. I
1615e 66 20 74 68 65 20 27 61 75 74 6f 56 61 63 75 75  f the 'autoVacuu
1615f 6d 27 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20  m'.** parameter 
16160 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
16161 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f  n auto-vacuum mo
16162 64 65 20 69 73 20 65 6e 61 62 6c 65 64 2e 20 49  de is enabled. I
16163 66 20 7a 65 72 6f 2c 20 69 74 0a 2a 2a 20 69 73  f zero, it.** is
16164 20 64 69 73 61 62 6c 65 64 2e 20 54 68 65 20 64   disabled. The d
16165 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72  efault value for
16166 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d   the auto-vacuum
16167 20 70 72 6f 70 65 72 74 79 20 69 73 20 0a 2a 2a   property is .**
16168 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74   determined by t
16169 68 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  he SQLITE_DEFAUL
1616a 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 6d 61 63  T_AUTOVACUUM mac
1616b 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ro..*/.SQLITE_PR
1616c 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1616d 33 42 74 72 65 65 53 65 74 41 75 74 6f 56 61 63  3BtreeSetAutoVac
1616e 75 75 6d 28 42 74 72 65 65 20 2a 70 2c 20 69 6e  uum(Btree *p, in
1616f 74 20 61 75 74 6f 56 61 63 75 75 6d 29 7b 0a 23  t autoVacuum){.#
16170 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
16171 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 72  T_AUTOVACUUM.  r
16172 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41  eturn SQLITE_REA
16173 44 4f 4e 4c 59 3b 0a 23 65 6c 73 65 0a 20 20 42  DONLY;.#else.  B
16174 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
16175 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 20  ->pBt;.  int rc 
16176 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75  = SQLITE_OK;.  u
16177 38 20 61 76 20 3d 20 28 75 38 29 61 75 74 6f 56  8 av = (u8)autoV
16178 61 63 75 75 6d 3b 0a 0a 20 20 73 71 6c 69 74 65  acuum;..  sqlite
16179 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
1617a 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53    if( pBt->pageS
1617b 69 7a 65 46 69 78 65 64 20 26 26 20 28 61 76 20  izeFixed && (av 
1617c 3f 31 3a 30 29 21 3d 70 42 74 2d 3e 61 75 74 6f  ?1:0)!=pBt->auto
1617d 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 72 63  Vacuum ){.    rc
1617e 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e   = SQLITE_READON
1617f 4c 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  LY;.  }else{.   
16180 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
16181 20 3d 20 61 76 20 3f 31 3a 30 3b 0a 20 20 20 20   = av ?1:0;.    
16182 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 20  pBt->incrVacuum 
16183 3d 20 61 76 3d 3d 32 20 3f 31 3a 30 3b 0a 20 20  = av==2 ?1:0;.  
16184 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  }.  sqlite3Btree
16185 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
16186 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a  rn rc;.#endif.}.
16187 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
16188 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 27  e value of the '
16189 61 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f  auto-vacuum' pro
1618a 70 65 72 74 79 2e 20 49 66 20 61 75 74 6f 2d 76  perty. If auto-v
1618b 61 63 75 75 6d 20 69 73 20 0a 2a 2a 20 65 6e 61  acuum is .** ena
1618c 62 6c 65 64 20 31 20 69 73 20 72 65 74 75 72 6e  bled 1 is return
1618d 65 64 2e 20 4f 74 68 65 72 77 69 73 65 20 30 2e  ed. Otherwise 0.
1618e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1618f 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
16190 72 65 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d  reeGetAutoVacuum
16191 28 42 74 72 65 65 20 2a 70 29 7b 0a 23 69 66 64  (Btree *p){.#ifd
16192 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
16193 55 54 4f 56 41 43 55 55 4d 0a 20 20 72 65 74 75  UTOVACUUM.  retu
16194 72 6e 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43  rn BTREE_AUTOVAC
16195 55 55 4d 5f 4e 4f 4e 45 3b 0a 23 65 6c 73 65 0a  UUM_NONE;.#else.
16196 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69    int rc;.  sqli
16197 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
16198 3b 0a 20 20 72 63 20 3d 20 28 0a 20 20 20 20 28  ;.  rc = (.    (
16199 21 70 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63  !p->pBt->autoVac
1619a 75 75 6d 29 3f 42 54 52 45 45 5f 41 55 54 4f 56  uum)?BTREE_AUTOV
1619b 41 43 55 55 4d 5f 4e 4f 4e 45 3a 0a 20 20 20 20  ACUUM_NONE:.    
1619c 28 21 70 2d 3e 70 42 74 2d 3e 69 6e 63 72 56 61  (!p->pBt->incrVa
1619d 63 75 75 6d 29 3f 42 54 52 45 45 5f 41 55 54 4f  cuum)?BTREE_AUTO
1619e 56 41 43 55 55 4d 5f 46 55 4c 4c 3a 0a 20 20 20  VACUUM_FULL:.   
1619f 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55   BTREE_AUTOVACUU
161a0 4d 5f 49 4e 43 52 0a 20 20 29 3b 0a 20 20 73 71  M_INCR.  );.  sq
161a1 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
161a2 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p);.  return rc;
161a3 0a 23 65 6e 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a  .#endif.}.../*.*
161a4 2a 20 47 65 74 20 61 20 72 65 66 65 72 65 6e 63  * Get a referenc
161a5 65 20 74 6f 20 70 50 61 67 65 31 20 6f 66 20 74  e to pPage1 of t
161a6 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
161a7 2e 20 20 54 68 69 73 20 77 69 6c 6c 0a 2a 2a 20  .  This will.** 
161a8 61 6c 73 6f 20 61 63 71 75 69 72 65 20 61 20 72  also acquire a r
161a9 65 61 64 6c 6f 63 6b 20 6f 6e 20 74 68 61 74 20  eadlock on that 
161aa 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49  file..**.** SQLI
161ab 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
161ac 64 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49  d on success.  I
161ad 66 20 74 68 65 20 66 69 6c 65 20 69 73 20 6e 6f  f the file is no
161ae 74 20 61 0a 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d  t a.** well-form
161af 65 64 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ed database file
161b0 2c 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 43 4f  , then SQLITE_CO
161b1 52 52 55 50 54 20 69 73 20 72 65 74 75 72 6e 65  RRUPT is returne
161b2 64 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53  d..** SQLITE_BUS
161b3 59 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  Y is returned if
161b4 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
161b5 20 6c 6f 63 6b 65 64 2e 20 20 53 51 4c 49 54 45   locked.  SQLITE
161b6 5f 4e 4f 4d 45 4d 0a 2a 2a 20 69 73 20 72 65 74  _NOMEM.** is ret
161b7 75 72 6e 65 64 20 69 66 20 77 65 20 72 75 6e 20  urned if we run 
161b8 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 0a  out of memory. .
161b9 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f  */.static int lo
161ba 63 6b 42 74 72 65 65 28 42 74 53 68 61 72 65 64  ckBtree(BtShared
161bb 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 72 63   *pBt){.  int rc
161bc 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
161bd 67 65 31 3b 0a 20 20 69 6e 74 20 6e 50 61 67 65  ge1;.  int nPage
161be 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ;..  assert( sql
161bf 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
161c0 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
161c1 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50   assert( pBt->pP
161c2 61 67 65 31 3d 3d 30 20 29 3b 0a 20 20 72 63 20  age1==0 );.  rc 
161c3 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 68  = sqlite3PagerSh
161c4 61 72 65 64 4c 6f 63 6b 28 70 42 74 2d 3e 70 50  aredLock(pBt->pP
161c5 61 67 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21  ager);.  if( rc!
161c6 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
161c7 75 72 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 62  urn rc;.  rc = b
161c8 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
161c9 20 31 2c 20 26 70 50 61 67 65 31 2c 20 30 29 3b   1, &pPage1, 0);
161ca 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
161cb 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
161cc 3b 0a 0a 20 20 2f 2a 20 44 6f 20 73 6f 6d 65 20  ;..  /* Do some 
161cd 63 68 65 63 6b 69 6e 67 20 74 6f 20 68 65 6c 70  checking to help
161ce 20 69 6e 73 75 72 65 20 74 68 65 20 66 69 6c 65   insure the file
161cf 20 77 65 20 6f 70 65 6e 65 64 20 72 65 61 6c 6c   we opened reall
161d0 79 20 69 73 0a 20 20 2a 2a 20 61 20 76 61 6c 69  y is.  ** a vali
161d1 64 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  d database file.
161d2 20 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71   .  */.  rc = sq
161d3 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f  lite3PagerPageco
161d4 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  unt(pBt->pPager,
161d5 20 26 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 20   &nPage);.  if( 
161d6 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
161d7 0a 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f  .    goto page1_
161d8 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 7d  init_failed;.  }
161d9 65 6c 73 65 20 69 66 28 20 6e 50 61 67 65 3e 30  else if( nPage>0
161da 20 29 7b 0a 20 20 20 20 69 6e 74 20 70 61 67 65   ){.    int page
161db 53 69 7a 65 3b 0a 20 20 20 20 69 6e 74 20 75 73  Size;.    int us
161dc 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20 75 38  ableSize;.    u8
161dd 20 2a 70 61 67 65 31 20 3d 20 70 50 61 67 65 31   *page1 = pPage1
161de 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 72 63 20  ->aData;.    rc 
161df 3d 20 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 3b  = SQLITE_NOTADB;
161e0 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28  .    if( memcmp(
161e1 70 61 67 65 31 2c 20 7a 4d 61 67 69 63 48 65 61  page1, zMagicHea
161e2 64 65 72 2c 20 31 36 29 21 3d 30 20 29 7b 0a 20  der, 16)!=0 ){. 
161e3 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f       goto page1_
161e4 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20  init_failed;.   
161e5 20 7d 0a 20 20 20 20 69 66 28 20 70 61 67 65 31   }.    if( page1
161e6 5b 31 38 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20  [18]>1 ){.      
161e7 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20  pBt->readOnly = 
161e8 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  1;.    }.    if(
161e9 20 70 61 67 65 31 5b 31 39 5d 3e 31 20 29 7b 0a   page1[19]>1 ){.
161ea 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31        goto page1
161eb 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20  _init_failed;.  
161ec 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20    }..    /* The 
161ed 6d 61 78 69 6d 75 6d 20 65 6d 62 65 64 64 65 64  maximum embedded
161ee 20 66 72 61 63 74 69 6f 6e 20 6d 75 73 74 20 62   fraction must b
161ef 65 20 65 78 61 63 74 6c 79 20 32 35 25 2e 20 20  e exactly 25%.  
161f0 41 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a  And the minimum.
161f1 20 20 20 20 2a 2a 20 65 6d 62 65 64 64 65 64 20      ** embedded 
161f2 66 72 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65  fraction must be
161f3 20 31 32 2e 35 25 20 66 6f 72 20 62 6f 74 68 20   12.5% for both 
161f4 6c 65 61 66 2d 64 61 74 61 20 61 6e 64 20 6e 6f  leaf-data and no
161f5 6e 2d 6c 65 61 66 2d 64 61 74 61 2e 0a 20 20 20  n-leaf-data..   
161f6 20 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c   ** The original
161f7 20 64 65 73 69 67 6e 20 61 6c 6c 6f 77 65 64 20   design allowed 
161f8 74 68 65 73 65 20 61 6d 6f 75 6e 74 73 20 74 6f  these amounts to
161f9 20 76 61 72 79 2c 20 62 75 74 20 61 73 20 6f 66   vary, but as of
161fa 0a 20 20 20 20 2a 2a 20 76 65 72 73 69 6f 6e 20  .    ** version 
161fb 33 2e 36 2e 30 2c 20 77 65 20 72 65 71 75 69 72  3.6.0, we requir
161fc 65 20 74 68 65 6d 20 74 6f 20 62 65 20 66 69 78  e them to be fix
161fd 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ed..    */.    i
161fe 66 28 20 6d 65 6d 63 6d 70 28 26 70 61 67 65 31  f( memcmp(&page1
161ff 5b 32 31 5d 2c 20 22 5c 31 30 30 5c 30 34 30 5c  [21], "\100\040\
16200 30 34 30 22 2c 33 29 21 3d 30 20 29 7b 0a 20 20  040",3)!=0 ){.  
16201 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69      goto page1_i
16202 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20  nit_failed;.    
16203 7d 0a 20 20 20 20 70 61 67 65 53 69 7a 65 20 3d  }.    pageSize =
16204 20 67 65 74 32 62 79 74 65 28 26 70 61 67 65 31   get2byte(&page1
16205 5b 31 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 28  [16]);.    if( (
16206 28 70 61 67 65 53 69 7a 65 2d 31 29 26 70 61 67  (pageSize-1)&pag
16207 65 53 69 7a 65 29 21 3d 30 20 7c 7c 20 70 61 67  eSize)!=0 || pag
16208 65 53 69 7a 65 3c 35 31 32 20 7c 7c 0a 20 20 20  eSize<512 ||.   
16209 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4d 41 58       (SQLITE_MAX
1620a 5f 50 41 47 45 5f 53 49 5a 45 3c 33 32 37 36 38  _PAGE_SIZE<32768
1620b 20 26 26 20 70 61 67 65 53 69 7a 65 3e 53 51 4c   && pageSize>SQL
1620c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a  ITE_MAX_PAGE_SIZ
1620d 45 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  E).    ){.      
1620e 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f  goto page1_init_
1620f 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20  failed;.    }.  
16210 20 20 61 73 73 65 72 74 28 20 28 70 61 67 65 53    assert( (pageS
16211 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20  ize & 7)==0 );. 
16212 20 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20     usableSize = 
16213 70 61 67 65 53 69 7a 65 20 2d 20 70 61 67 65 31  pageSize - page1
16214 5b 32 30 5d 3b 0a 20 20 20 20 69 66 28 20 70 61  [20];.    if( pa
16215 67 65 53 69 7a 65 21 3d 70 42 74 2d 3e 70 61 67  geSize!=pBt->pag
16216 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 2f  eSize ){.      /
16217 2a 20 41 66 74 65 72 20 72 65 61 64 69 6e 67 20  * After reading 
16218 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f  the first page o
16219 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61  f the database a
1621a 73 73 75 6d 69 6e 67 20 61 20 70 61 67 65 20 73  ssuming a page s
1621b 69 7a 65 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20  ize.      ** of 
1621c 42 74 53 68 61 72 65 64 2e 70 61 67 65 53 69 7a  BtShared.pageSiz
1621d 65 2c 20 77 65 20 68 61 76 65 20 64 69 73 63 6f  e, we have disco
1621e 76 65 72 65 64 20 74 68 61 74 20 74 68 65 20 70  vered that the p
1621f 61 67 65 2d 73 69 7a 65 20 69 73 0a 20 20 20 20  age-size is.    
16220 20 20 2a 2a 20 61 63 74 75 61 6c 6c 79 20 70 61    ** actually pa
16221 67 65 53 69 7a 65 2e 20 55 6e 6c 6f 63 6b 20 74  geSize. Unlock t
16222 68 65 20 64 61 74 61 62 61 73 65 2c 20 6c 65 61  he database, lea
16223 76 65 20 70 42 74 2d 3e 70 50 61 67 65 31 20 61  ve pBt->pPage1 a
16224 74 0a 20 20 20 20 20 20 2a 2a 20 7a 65 72 6f 20  t.      ** zero 
16225 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54  and return SQLIT
16226 45 5f 4f 4b 2e 20 54 68 65 20 63 61 6c 6c 65 72  E_OK. The caller
16227 20 77 69 6c 6c 20 63 61 6c 6c 20 74 68 69 73 20   will call this 
16228 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20 20 20 2a  function.      *
16229 2a 20 61 67 61 69 6e 20 77 69 74 68 20 74 68 65  * again with the
1622a 20 63 6f 72 72 65 63 74 20 70 61 67 65 2d 73 69   correct page-si
1622b 7a 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ze..      */.   
1622c 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
1622d 50 61 67 65 31 29 3b 0a 20 20 20 20 20 20 70 42  Page1);.      pB
1622e 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20  t->usableSize = 
1622f 28 75 31 36 29 75 73 61 62 6c 65 53 69 7a 65 3b  (u16)usableSize;
16230 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61 67 65  .      pBt->page
16231 53 69 7a 65 20 3d 20 28 75 31 36 29 70 61 67 65  Size = (u16)page
16232 53 69 7a 65 3b 0a 20 20 20 20 20 20 66 72 65 65  Size;.      free
16233 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a  TempSpace(pBt);.
16234 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
16235 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69  e3PagerSetPagesi
16236 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  ze(pBt->pPager, 
16237 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2c 0a  &pBt->pageSize,.
16238 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16239 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1623a 20 20 20 70 61 67 65 53 69 7a 65 2d 75 73 61 62     pageSize-usab
1623b 6c 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 72  leSize);.      r
1623c 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
1623d 20 20 20 20 69 66 28 20 75 73 61 62 6c 65 53 69      if( usableSi
1623e 7a 65 3c 34 38 30 20 29 7b 0a 20 20 20 20 20 20  ze<480 ){.      
1623f 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f  goto page1_init_
16240 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20  failed;.    }.  
16241 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20    pBt->pageSize 
16242 3d 20 28 75 31 36 29 70 61 67 65 53 69 7a 65 3b  = (u16)pageSize;
16243 0a 20 20 20 20 70 42 74 2d 3e 75 73 61 62 6c 65  .    pBt->usable
16244 53 69 7a 65 20 3d 20 28 75 31 36 29 75 73 61 62  Size = (u16)usab
16245 6c 65 53 69 7a 65 3b 0a 23 69 66 6e 64 65 66 20  leSize;.#ifndef 
16246 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
16247 56 41 43 55 55 4d 0a 20 20 20 20 70 42 74 2d 3e  VACUUM.    pBt->
16248 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 67 65  autoVacuum = (ge
16249 74 34 62 79 74 65 28 26 70 61 67 65 31 5b 33 36  t4byte(&page1[36
1624a 20 2b 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20   + 4*4])?1:0);. 
1624b 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63 75     pBt->incrVacu
1624c 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28 26  um = (get4byte(&
1624d 70 61 67 65 31 5b 33 36 20 2b 20 37 2a 34 5d 29  page1[36 + 7*4])
1624e 3f 31 3a 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20  ?1:0);.#endif.  
1624f 7d 0a 0a 20 20 2f 2a 20 6d 61 78 4c 6f 63 61 6c  }..  /* maxLocal
16250 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20   is the maximum 
16251 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c 6f 61  amount of payloa
16252 64 20 74 6f 20 73 74 6f 72 65 20 6c 6f 63 61 6c  d to store local
16253 6c 79 20 66 6f 72 0a 20 20 2a 2a 20 61 20 63 65  ly for.  ** a ce
16254 6c 6c 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 69  ll.  Make sure i
16255 74 20 69 73 20 73 6d 61 6c 6c 20 65 6e 6f 75 67  t is small enoug
16256 68 20 73 6f 20 74 68 61 74 20 61 74 20 6c 65 61  h so that at lea
16257 73 74 20 6d 69 6e 46 61 6e 6f 75 74 0a 20 20 2a  st minFanout.  *
16258 2a 20 63 65 6c 6c 73 20 63 61 6e 20 77 69 6c 6c  * cells can will
16259 20 66 69 74 20 6f 6e 20 6f 6e 65 20 70 61 67 65   fit on one page
1625a 2e 20 20 57 65 20 61 73 73 75 6d 65 20 61 20 31  .  We assume a 1
1625b 30 2d 62 79 74 65 20 70 61 67 65 20 68 65 61 64  0-byte page head
1625c 65 72 2e 0a 20 20 2a 2a 20 42 65 73 69 64 65 73  er..  ** Besides
1625d 20 74 68 65 20 70 61 79 6c 6f 61 64 2c 20 74 68   the payload, th
1625e 65 20 63 65 6c 6c 20 6d 75 73 74 20 73 74 6f 72  e cell must stor
1625f 65 3a 0a 20 20 2a 2a 20 20 20 20 20 32 2d 62 79  e:.  **     2-by
16260 74 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68  te pointer to th
16261 65 20 63 65 6c 6c 0a 20 20 2a 2a 20 20 20 20 20  e cell.  **     
16262 34 2d 62 79 74 65 20 63 68 69 6c 64 20 70 6f 69  4-byte child poi
16263 6e 74 65 72 0a 20 20 2a 2a 20 20 20 20 20 39 2d  nter.  **     9-
16264 62 79 74 65 20 6e 4b 65 79 20 76 61 6c 75 65 0a  byte nKey value.
16265 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 74 65 20    **     4-byte 
16266 6e 44 61 74 61 20 76 61 6c 75 65 0a 20 20 2a 2a  nData value.  **
16267 20 20 20 20 20 34 2d 62 79 74 65 20 6f 76 65 72       4-byte over
16268 66 6c 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65  flow page pointe
16269 72 0a 20 20 2a 2a 20 53 6f 20 61 20 63 65 6c 6c  r.  ** So a cell
1626a 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 32   consists of a 2
1626b 2d 62 79 74 65 20 70 6f 69 6e 65 72 2c 20 61 20  -byte poiner, a 
1626c 68 65 61 64 65 72 20 77 68 69 63 68 20 69 73 20  header which is 
1626d 61 73 20 6d 75 63 68 20 61 73 0a 20 20 2a 2a 20  as much as.  ** 
1626e 31 37 20 62 79 74 65 73 20 6c 6f 6e 67 2c 20 30  17 bytes long, 0
1626f 20 74 6f 20 4e 20 62 79 74 65 73 20 6f 66 20 70   to N bytes of p
16270 61 79 6c 6f 61 64 2c 20 61 6e 64 20 61 6e 20 6f  ayload, and an o
16271 70 74 69 6f 6e 61 6c 20 34 20 62 79 74 65 20 6f  ptional 4 byte o
16272 76 65 72 66 6c 6f 77 0a 20 20 2a 2a 20 70 61 67  verflow.  ** pag
16273 65 20 70 6f 69 6e 74 65 72 2e 0a 20 20 2a 2f 0a  e pointer..  */.
16274 20 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 20    pBt->maxLocal 
16275 3d 20 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  = (pBt->usableSi
16276 7a 65 2d 31 32 29 2a 36 34 2f 32 35 35 20 2d 20  ze-12)*64/255 - 
16277 32 33 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c 6f  23;.  pBt->minLo
16278 63 61 6c 20 3d 20 28 70 42 74 2d 3e 75 73 61 62  cal = (pBt->usab
16279 6c 65 53 69 7a 65 2d 31 32 29 2a 33 32 2f 32 35  leSize-12)*32/25
1627a 35 20 2d 20 32 33 3b 0a 20 20 70 42 74 2d 3e 6d  5 - 23;.  pBt->m
1627b 61 78 4c 65 61 66 20 3d 20 70 42 74 2d 3e 75 73  axLeaf = pBt->us
1627c 61 62 6c 65 53 69 7a 65 20 2d 20 33 35 3b 0a 20  ableSize - 35;. 
1627d 20 70 42 74 2d 3e 6d 69 6e 4c 65 61 66 20 3d 20   pBt->minLeaf = 
1627e 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  (pBt->usableSize
1627f 2d 31 32 29 2a 33 32 2f 32 35 35 20 2d 20 32 33  -12)*32/255 - 23
16280 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74 2d  ;.  assert( pBt-
16281 3e 6d 61 78 4c 65 61 66 20 2b 20 32 33 20 3c 3d  >maxLeaf + 23 <=
16282 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42   MX_CELL_SIZE(pB
16283 74 29 20 29 3b 0a 20 20 70 42 74 2d 3e 70 50 61  t) );.  pBt->pPa
16284 67 65 31 20 3d 20 70 50 61 67 65 31 3b 0a 20 20  ge1 = pPage1;.  
16285 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
16286 3b 0a 0a 70 61 67 65 31 5f 69 6e 69 74 5f 66 61  ;..page1_init_fa
16287 69 6c 65 64 3a 0a 20 20 72 65 6c 65 61 73 65 50  iled:.  releaseP
16288 61 67 65 28 70 50 61 67 65 31 29 3b 0a 20 20 70  age(pPage1);.  p
16289 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30 3b 0a  Bt->pPage1 = 0;.
1628a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1628b 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 61  /*.** If there a
1628c 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e 64 69 6e  re no outstandin
1628d 67 20 63 75 72 73 6f 72 73 20 61 6e 64 20 77 65  g cursors and we
1628e 20 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 65 20   are not in the 
1628f 6d 69 64 64 6c 65 0a 2a 2a 20 6f 66 20 61 20 74  middle.** of a t
16290 72 61 6e 73 61 63 74 69 6f 6e 20 62 75 74 20 74  ransaction but t
16291 68 65 72 65 20 69 73 20 61 20 72 65 61 64 20 6c  here is a read l
16292 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
16293 61 73 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69  ase, then.** thi
16294 73 20 72 6f 75 74 69 6e 65 20 75 6e 72 65 66 73  s routine unrefs
16295 20 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20   the first page 
16296 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
16297 66 69 6c 65 20 77 68 69 63 68 20 0a 2a 2a 20 68  file which .** h
16298 61 73 20 74 68 65 20 65 66 66 65 63 74 20 6f 66  as the effect of
16299 20 72 65 6c 65 61 73 69 6e 67 20 74 68 65 20 72   releasing the r
1629a 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20  ead lock..**.** 
1629b 49 66 20 74 68 65 72 65 20 69 73 20 61 20 74 72  If there is a tr
1629c 61 6e 73 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f  ansaction in pro
1629d 67 72 65 73 73 2c 20 74 68 69 73 20 72 6f 75 74  gress, this rout
1629e 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ine is a no-op..
1629f 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75  */.static void u
162a0 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73  nlockBtreeIfUnus
162a1 65 64 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  ed(BtShared *pBt
162a2 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
162a3 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
162a4 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
162a5 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 43   assert( pBt->pC
162a6 75 72 73 6f 72 3d 3d 30 20 7c 7c 20 70 42 74 2d  ursor==0 || pBt-
162a7 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 54  >inTransaction>T
162a8 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 69  RANS_NONE );.  i
162a9 66 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61  f( pBt->inTransa
162aa 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f 4e  ction==TRANS_NON
162ab 45 20 26 26 20 70 42 74 2d 3e 70 50 61 67 65 31  E && pBt->pPage1
162ac 21 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  !=0 ){.    asser
162ad 74 28 20 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e  t( pBt->pPage1->
162ae 61 44 61 74 61 20 29 3b 0a 20 20 20 20 61 73 73  aData );.    ass
162af 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
162b0 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70  rRefcount(pBt->p
162b1 50 61 67 65 72 29 3d 3d 31 20 29 3b 0a 20 20 20  Pager)==1 );.   
162b2 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50   assert( pBt->pP
162b3 61 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a 20  age1->aData );. 
162b4 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
162b5 42 74 2d 3e 70 50 61 67 65 31 29 3b 0a 20 20 20  Bt->pPage1);.   
162b6 20 70 42 74 2d 3e 70 50 61 67 65 31 20 3d 20 30   pBt->pPage1 = 0
162b7 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ;.  }.}../*.** I
162b8 66 20 70 42 74 20 70 6f 69 6e 74 73 20 74 6f 20  f pBt points to 
162b9 61 6e 20 65 6d 70 74 79 20 66 69 6c 65 20 74 68  an empty file th
162ba 65 6e 20 63 6f 6e 76 65 72 74 20 74 68 61 74 20  en convert that 
162bb 65 6d 70 74 79 20 66 69 6c 65 0a 2a 2a 20 69 6e  empty file.** in
162bc 74 6f 20 61 20 6e 65 77 20 65 6d 70 74 79 20 64  to a new empty d
162bd 61 74 61 62 61 73 65 20 62 79 20 69 6e 69 74 69  atabase by initi
162be 61 6c 69 7a 69 6e 67 20 74 68 65 20 66 69 72 73  alizing the firs
162bf 74 20 70 61 67 65 20 6f 66 0a 2a 2a 20 74 68 65  t page of.** the
162c0 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73 74   database..*/.st
162c1 61 74 69 63 20 69 6e 74 20 6e 65 77 44 61 74 61  atic int newData
162c2 62 61 73 65 28 42 74 53 68 61 72 65 64 20 2a 70  base(BtShared *p
162c3 42 74 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  Bt){.  MemPage *
162c4 70 50 31 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  pP1;.  unsigned 
162c5 63 68 61 72 20 2a 64 61 74 61 3b 0a 20 20 69 6e  char *data;.  in
162c6 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e 50 61 67  t rc;.  int nPag
162c7 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  e;..  assert( sq
162c8 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
162c9 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  (pBt->mutex) );.
162ca 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
162cb 65 20 73 69 7a 65 20 68 61 73 20 61 6c 72 65 61  e size has alrea
162cc 64 79 20 62 65 65 6e 20 6d 65 61 73 75 72 65 64  dy been measured
162cd 20 61 6e 64 20 63 61 63 68 65 64 2c 20 73 6f 20   and cached, so 
162ce 66 61 69 6c 75 72 65 0a 20 20 2a 2a 20 69 73 20  failure.  ** is 
162cf 69 6d 70 6f 73 73 69 62 6c 65 20 68 65 72 65 2e  impossible here.
162d0 20 20 49 66 20 74 68 65 20 6f 72 69 67 69 6e 61    If the origina
162d1 6c 20 73 69 7a 65 20 6d 65 61 73 75 72 65 6d 65  l size measureme
162d2 6e 74 20 66 61 69 6c 65 64 2c 20 74 68 65 6e 0a  nt failed, then.
162d3 20 20 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67 20    ** processing 
162d4 61 62 6f 72 74 73 20 62 65 66 6f 72 65 20 65 6e  aborts before en
162d5 74 65 72 69 6e 67 20 74 68 69 73 20 72 6f 75 74  tering this rout
162d6 69 6e 65 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73  ine. */.  rc = s
162d7 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63  qlite3PagerPagec
162d8 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72  ount(pBt->pPager
162d9 2c 20 26 6e 50 61 67 65 29 3b 0a 20 20 69 66 28  , &nPage);.  if(
162da 20 4e 45 56 45 52 28 72 63 21 3d 53 51 4c 49 54   NEVER(rc!=SQLIT
162db 45 5f 4f 4b 29 20 7c 7c 20 6e 50 61 67 65 3e 30  E_OK) || nPage>0
162dc 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
162dd 63 3b 0a 20 20 7d 0a 20 20 70 50 31 20 3d 20 70  c;.  }.  pP1 = p
162de 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20 20 61 73  Bt->pPage1;.  as
162df 73 65 72 74 28 20 70 50 31 21 3d 30 20 29 3b 0a  sert( pP1!=0 );.
162e0 20 20 64 61 74 61 20 3d 20 70 50 31 2d 3e 61 44    data = pP1->aD
162e1 61 74 61 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  ata;.  rc = sqli
162e2 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
162e3 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 69  1->pDbPage);.  i
162e4 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
162e5 63 3b 0a 20 20 6d 65 6d 63 70 79 28 64 61 74 61  c;.  memcpy(data
162e6 2c 20 7a 4d 61 67 69 63 48 65 61 64 65 72 2c 20  , zMagicHeader, 
162e7 73 69 7a 65 6f 66 28 7a 4d 61 67 69 63 48 65 61  sizeof(zMagicHea
162e8 64 65 72 29 29 3b 0a 20 20 61 73 73 65 72 74 28  der));.  assert(
162e9 20 73 69 7a 65 6f 66 28 7a 4d 61 67 69 63 48 65   sizeof(zMagicHe
162ea 61 64 65 72 29 3d 3d 31 36 20 29 3b 0a 20 20 70  ader)==16 );.  p
162eb 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 31 36  ut2byte(&data[16
162ec 5d 2c 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  ], pBt->pageSize
162ed 29 3b 0a 20 20 64 61 74 61 5b 31 38 5d 20 3d 20  );.  data[18] = 
162ee 31 3b 0a 20 20 64 61 74 61 5b 31 39 5d 20 3d 20  1;.  data[19] = 
162ef 31 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  1;.  assert( pBt
162f0 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3c 3d 70 42  ->usableSize<=pB
162f1 74 2d 3e 70 61 67 65 53 69 7a 65 20 26 26 20 70  t->pageSize && p
162f2 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2b 32  Bt->usableSize+2
162f3 35 35 3e 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a  55>=pBt->pageSiz
162f4 65 29 3b 0a 20 20 64 61 74 61 5b 32 30 5d 20 3d  e);.  data[20] =
162f5 20 28 75 38 29 28 70 42 74 2d 3e 70 61 67 65 53   (u8)(pBt->pageS
162f6 69 7a 65 20 2d 20 70 42 74 2d 3e 75 73 61 62 6c  ize - pBt->usabl
162f7 65 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 5b 32  eSize);.  data[2
162f8 31 5d 20 3d 20 36 34 3b 0a 20 20 64 61 74 61 5b  1] = 64;.  data[
162f9 32 32 5d 20 3d 20 33 32 3b 0a 20 20 64 61 74 61  22] = 32;.  data
162fa 5b 32 33 5d 20 3d 20 33 32 3b 0a 20 20 6d 65 6d  [23] = 32;.  mem
162fb 73 65 74 28 26 64 61 74 61 5b 32 34 5d 2c 20 30  set(&data[24], 0
162fc 2c 20 31 30 30 2d 32 34 29 3b 0a 20 20 7a 65 72  , 100-24);.  zer
162fd 6f 50 61 67 65 28 70 50 31 2c 20 50 54 46 5f 49  oPage(pP1, PTF_I
162fe 4e 54 4b 45 59 7c 50 54 46 5f 4c 45 41 46 7c 50  NTKEY|PTF_LEAF|P
162ff 54 46 5f 4c 45 41 46 44 41 54 41 20 29 3b 0a 20  TF_LEAFDATA );. 
16300 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69   pBt->pageSizeFi
16301 78 65 64 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66  xed = 1;.#ifndef
16302 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
16303 4f 56 41 43 55 55 4d 0a 20 20 61 73 73 65 72 74  OVACUUM.  assert
16304 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
16305 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e 61 75 74  m==1 || pBt->aut
16306 6f 56 61 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 20  oVacuum==0 );.  
16307 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 63  assert( pBt->inc
16308 72 56 61 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 42  rVacuum==1 || pB
16309 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 3d 3d 30  t->incrVacuum==0
1630a 20 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26   );.  put4byte(&
1630b 64 61 74 61 5b 33 36 20 2b 20 34 2a 34 5d 2c 20  data[36 + 4*4], 
1630c 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29  pBt->autoVacuum)
1630d 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26 64 61  ;.  put4byte(&da
1630e 74 61 5b 33 36 20 2b 20 37 2a 34 5d 2c 20 70 42  ta[36 + 7*4], pB
1630f 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d 29 3b 0a  t->incrVacuum);.
16310 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
16311 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
16312 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 73  .** Attempt to s
16313 74 61 72 74 20 61 20 6e 65 77 20 74 72 61 6e 73  tart a new trans
16314 61 63 74 69 6f 6e 2e 20 41 20 77 72 69 74 65 2d  action. A write-
16315 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69  transaction.** i
16316 73 20 73 74 61 72 74 65 64 20 69 66 20 74 68 65  s started if the
16317 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
16318 20 69 73 20 6e 6f 6e 7a 65 72 6f 2c 20 6f 74 68   is nonzero, oth
16319 65 72 77 69 73 65 20 61 20 72 65 61 64 2d 0a 2a  erwise a read-.*
1631a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20  * transaction.  
1631b 49 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72  If the second ar
1631c 67 75 6d 65 6e 74 20 69 73 20 32 20 6f 72 20 6d  gument is 2 or m
1631d 6f 72 65 20 61 6e 64 20 65 78 63 6c 75 73 69 76  ore and exclusiv
1631e 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e  e.** transaction
1631f 20 69 73 20 73 74 61 72 74 65 64 2c 20 6d 65 61   is started, mea
16320 6e 69 6e 67 20 74 68 61 74 20 6e 6f 20 6f 74 68  ning that no oth
16321 65 72 20 70 72 6f 63 65 73 73 20 69 73 20 61 6c  er process is al
16322 6c 6f 77 65 64 0a 2a 2a 20 74 6f 20 61 63 63 65  lowed.** to acce
16323 73 73 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  ss the database.
16324 20 20 41 20 70 72 65 65 78 69 73 74 69 6e 67 20    A preexisting 
16325 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20  transaction may 
16326 6e 6f 74 20 62 65 0a 2a 2a 20 75 70 67 72 61 64  not be.** upgrad
16327 65 64 20 74 6f 20 65 78 63 6c 75 73 69 76 65 20  ed to exclusive 
16328 62 79 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20  by calling this 
16329 72 6f 75 74 69 6e 65 20 61 20 73 65 63 6f 6e 64  routine a second
1632a 20 74 69 6d 65 20 2d 20 74 68 65 0a 2a 2a 20 65   time - the.** e
1632b 78 63 6c 75 73 69 76 69 74 79 20 66 6c 61 67 20  xclusivity flag 
1632c 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 61  only works for a
1632d 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e   new transaction
1632e 2e 0a 2a 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d  ..**.** A write-
1632f 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74  transaction must
16330 20 62 65 20 73 74 61 72 74 65 64 20 62 65 66 6f   be started befo
16331 72 65 20 61 74 74 65 6d 70 74 69 6e 67 20 61 6e  re attempting an
16332 79 20 0a 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f  y .** changes to
16333 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
16334 4e 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  None of the foll
16335 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 0a  owing routines .
16336 2a 2a 20 77 69 6c 6c 20 77 6f 72 6b 20 75 6e 6c  ** will work unl
16337 65 73 73 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ess a transactio
16338 6e 20 69 73 20 73 74 61 72 74 65 64 20 66 69 72  n is started fir
16339 73 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 73  st:.**.**      s
1633a 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74  qlite3BtreeCreat
1633b 65 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20  eTable().**     
1633c 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72 65   sqlite3BtreeCre
1633d 61 74 65 49 6e 64 65 78 28 29 0a 2a 2a 20 20 20  ateIndex().**   
1633e 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
1633f 6c 65 61 72 54 61 62 6c 65 28 29 0a 2a 2a 20 20  learTable().**  
16340 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
16341 44 72 6f 70 54 61 62 6c 65 28 29 0a 2a 2a 20 20  DropTable().**  
16342 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
16343 49 6e 73 65 72 74 28 29 0a 2a 2a 20 20 20 20 20  Insert().**     
16344 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c   sqlite3BtreeDel
16345 65 74 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71  ete().**      sq
16346 6c 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65  lite3BtreeUpdate
16347 4d 65 74 61 28 29 0a 2a 2a 0a 2a 2a 20 49 66 20  Meta().**.** If 
16348 61 6e 20 69 6e 69 74 69 61 6c 20 61 74 74 65 6d  an initial attem
16349 70 74 20 74 6f 20 61 63 71 75 69 72 65 20 74 68  pt to acquire th
1634a 65 20 6c 6f 63 6b 20 66 61 69 6c 73 20 62 65 63  e lock fails bec
1634b 61 75 73 65 20 6f 66 20 6c 6f 63 6b 20 63 6f 6e  ause of lock con
1634c 74 65 6e 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 74  tention.** and t
1634d 68 65 20 64 61 74 61 62 61 73 65 20 77 61 73 20  he database was 
1634e 70 72 65 76 69 6f 75 73 6c 79 20 75 6e 6c 6f 63  previously unloc
1634f 6b 65 64 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65  ked, then invoke
16350 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65   the busy handle
16351 72 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 69 73  r.** if there is
16352 20 6f 6e 65 2e 20 20 42 75 74 20 69 66 20 74 68   one.  But if th
16353 65 72 65 20 77 61 73 20 70 72 65 76 69 6f 75 73  ere was previous
16354 6c 79 20 61 20 72 65 61 64 2d 6c 6f 63 6b 2c 20  ly a read-lock, 
16355 64 6f 20 6e 6f 74 0a 2a 2a 20 69 6e 76 6f 6b 65  do not.** invoke
16356 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65   the busy handle
16357 72 20 2d 20 6a 75 73 74 20 72 65 74 75 72 6e 20  r - just return 
16358 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 53 51  SQLITE_BUSY.  SQ
16359 4c 49 54 45 5f 42 55 53 59 20 69 73 20 0a 2a 2a  LITE_BUSY is .**
1635a 20 72 65 74 75 72 6e 65 64 20 77 68 65 6e 20 74   returned when t
1635b 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20  here is already 
1635c 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 6e 20 6f  a read-lock in o
1635d 72 64 65 72 20 74 6f 20 61 76 6f 69 64 20 61 20  rder to avoid a 
1635e 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20  deadlock..**.** 
1635f 53 75 70 70 6f 73 65 20 74 68 65 72 65 20 61 72  Suppose there ar
16360 65 20 74 77 6f 20 70 72 6f 63 65 73 73 65 73 20  e two processes 
16361 41 20 61 6e 64 20 42 2e 20 20 41 20 68 61 73 20  A and B.  A has 
16362 61 20 72 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20  a read lock and 
16363 42 20 68 61 73 0a 2a 2a 20 61 20 72 65 73 65 72  B has.** a reser
16364 76 65 64 20 6c 6f 63 6b 2e 20 20 42 20 74 72 69  ved lock.  B tri
16365 65 73 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f  es to promote to
16366 20 65 78 63 6c 75 73 69 76 65 20 62 75 74 20 69   exclusive but i
16367 73 20 62 6c 6f 63 6b 65 64 20 62 65 63 61 75 73  s blocked becaus
16368 65 0a 2a 2a 20 6f 66 20 41 27 73 20 72 65 61 64  e.** of A's read
16369 20 6c 6f 63 6b 2e 20 20 41 20 74 72 69 65 73 20   lock.  A tries 
1636a 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 72 65  to promote to re
1636b 73 65 72 76 65 64 20 62 75 74 20 69 73 20 62 6c  served but is bl
1636c 6f 63 6b 65 64 20 62 79 20 42 2e 0a 2a 2a 20 4f  ocked by B..** O
1636d 6e 65 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20  ne or the other 
1636e 6f 66 20 74 68 65 20 74 77 6f 20 70 72 6f 63 65  of the two proce
1636f 73 73 65 73 20 6d 75 73 74 20 67 69 76 65 20 77  sses must give w
16370 61 79 20 6f 72 20 74 68 65 72 65 20 63 61 6e 20  ay or there can 
16371 62 65 0a 2a 2a 20 6e 6f 20 70 72 6f 67 72 65 73  be.** no progres
16372 73 2e 20 20 42 79 20 72 65 74 75 72 6e 69 6e 67  s.  By returning
16373 20 53 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64   SQLITE_BUSY and
16374 20 6e 6f 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68   not invoking th
16375 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a  e busy callback.
16376 2a 2a 20 77 68 65 6e 20 41 20 61 6c 72 65 61 64  ** when A alread
16377 79 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63  y has a read loc
16378 6b 2c 20 77 65 20 65 6e 63 6f 75 72 61 67 65 20  k, we encourage 
16379 41 20 74 6f 20 67 69 76 65 20 75 70 20 61 6e 64  A to give up and
1637a 20 6c 65 74 20 42 0a 2a 2a 20 70 72 6f 63 65 65   let B.** procee
1637b 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
1637c 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1637d 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28  BtreeBeginTrans(
1637e 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 77 72  Btree *p, int wr
1637f 66 6c 61 67 29 7b 0a 20 20 73 71 6c 69 74 65 33  flag){.  sqlite3
16380 20 2a 70 42 6c 6f 63 6b 20 3d 20 30 3b 0a 20 20   *pBlock = 0;.  
16381 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
16382 70 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63  p->pBt;.  int rc
16383 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20   = SQLITE_OK;.. 
16384 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
16385 65 72 28 70 29 3b 0a 20 20 62 74 72 65 65 49 6e  er(p);.  btreeIn
16386 74 65 67 72 69 74 79 28 70 29 3b 0a 0a 20 20 2f  tegrity(p);..  /
16387 2a 20 49 66 20 74 68 65 20 62 74 72 65 65 20 69  * If the btree i
16388 73 20 61 6c 72 65 61 64 79 20 69 6e 20 61 20 77  s already in a w
16389 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e  rite-transaction
1638a 2c 20 6f 72 20 69 74 0a 20 20 2a 2a 20 69 73 20  , or it.  ** is 
1638b 61 6c 72 65 61 64 79 20 69 6e 20 61 20 72 65 61  already in a rea
1638c 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e  d-transaction an
1638d 64 20 61 20 72 65 61 64 2d 74 72 61 6e 73 61 63  d a read-transac
1638e 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 72 65 71  tion.  ** is req
1638f 75 65 73 74 65 64 2c 20 74 68 69 73 20 69 73 20  uested, this is 
16390 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2f 0a 20 20  a no-op..  */.  
16391 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d  if( p->inTrans==
16392 54 52 41 4e 53 5f 57 52 49 54 45 20 7c 7c 20 28  TRANS_WRITE || (
16393 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e  p->inTrans==TRAN
16394 53 5f 52 45 41 44 20 26 26 20 21 77 72 66 6c 61  S_READ && !wrfla
16395 67 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74  g) ){.    goto t
16396 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a  rans_begun;.  }.
16397 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 72 61 6e  .  /* Write tran
16398 73 61 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74  sactions are not
16399 20 70 6f 73 73 69 62 6c 65 20 6f 6e 20 61 20 72   possible on a r
1639a 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73  ead-only databas
1639b 65 20 2a 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e  e */.  if( pBt->
1639c 72 65 61 64 4f 6e 6c 79 20 26 26 20 77 72 66 6c  readOnly && wrfl
1639d 61 67 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  ag ){.    rc = S
1639e 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a  QLITE_READONLY;.
1639f 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62      goto trans_b
163a0 65 67 75 6e 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64  egun;.  }..#ifnd
163a1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
163a2 48 41 52 45 44 5f 43 41 43 48 45 0a 20 20 2f 2a  HARED_CACHE.  /*
163a3 20 49 66 20 61 6e 6f 74 68 65 72 20 64 61 74 61   If another data
163a4 62 61 73 65 20 68 61 6e 64 6c 65 20 68 61 73 20  base handle has 
163a5 61 6c 72 65 61 64 79 20 6f 70 65 6e 65 64 20 61  already opened a
163a6 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
163a7 6f 6e 20 0a 20 20 2a 2a 20 6f 6e 20 74 68 69 73  on .  ** on this
163a8 20 73 68 61 72 65 64 2d 62 74 72 65 65 20 73 74   shared-btree st
163a9 72 75 63 74 75 72 65 20 61 6e 64 20 61 20 73 65  ructure and a se
163aa 63 6f 6e 64 20 77 72 69 74 65 20 74 72 61 6e 73  cond write trans
163ab 61 63 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 72  action is.  ** r
163ac 65 71 75 65 73 74 65 64 2c 20 72 65 74 75 72 6e  equested, return
163ad 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a   SQLITE_LOCKED..
163ae 20 20 2a 2f 0a 20 20 69 66 28 20 28 77 72 66 6c    */.  if( (wrfl
163af 61 67 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61  ag && pBt->inTra
163b0 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f  nsaction==TRANS_
163b1 57 52 49 54 45 29 20 7c 7c 20 70 42 74 2d 3e 69  WRITE) || pBt->i
163b2 73 50 65 6e 64 69 6e 67 20 29 7b 0a 20 20 20 20  sPending ){.    
163b3 70 42 6c 6f 63 6b 20 3d 20 70 42 74 2d 3e 70 57  pBlock = pBt->pW
163b4 72 69 74 65 72 2d 3e 64 62 3b 0a 20 20 7d 65 6c  riter->db;.  }el
163b5 73 65 20 69 66 28 20 77 72 66 6c 61 67 3e 31 20  se if( wrflag>1 
163b6 29 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70  ){.    BtLock *p
163b7 49 74 65 72 3b 0a 20 20 20 20 66 6f 72 28 70 49  Iter;.    for(pI
163b8 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20  ter=pBt->pLock; 
163b9 70 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74  pIter; pIter=pIt
163ba 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  er->pNext){.    
163bb 20 20 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74    if( pIter->pBt
163bc 72 65 65 21 3d 70 20 29 7b 0a 20 20 20 20 20 20  ree!=p ){.      
163bd 20 20 70 42 6c 6f 63 6b 20 3d 20 70 49 74 65 72    pBlock = pIter
163be 2d 3e 70 42 74 72 65 65 2d 3e 64 62 3b 0a 20 20  ->pBtree->db;.  
163bf 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
163c0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
163c1 20 69 66 28 20 70 42 6c 6f 63 6b 20 29 7b 0a 20   if( pBlock ){. 
163c2 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63     sqlite3Connec
163c3 74 69 6f 6e 42 6c 6f 63 6b 65 64 28 70 2d 3e 64  tionBlocked(p->d
163c4 62 2c 20 70 42 6c 6f 63 6b 29 3b 0a 20 20 20 20  b, pBlock);.    
163c5 72 63 20 3d 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  rc = SQLITE_LOCK
163c6 45 44 5f 53 48 41 52 45 44 43 41 43 48 45 3b 0a  ED_SHAREDCACHE;.
163c7 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62      goto trans_b
163c8 65 67 75 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  egun;.  }.#endif
163c9 0a 0a 20 20 2f 2a 20 41 6e 79 20 72 65 61 64 2d  ..  /* Any read-
163ca 6f 6e 6c 79 20 6f 72 20 72 65 61 64 2d 77 72 69  only or read-wri
163cb 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  te transaction i
163cc 6d 70 6c 69 65 73 20 61 20 72 65 61 64 2d 6c 6f  mplies a read-lo
163cd 63 6b 20 6f 6e 20 0a 20 20 2a 2a 20 70 61 67 65  ck on .  ** page
163ce 20 31 2e 20 53 6f 20 69 66 20 73 6f 6d 65 20 6f   1. So if some o
163cf 74 68 65 72 20 73 68 61 72 65 64 2d 63 61 63 68  ther shared-cach
163d0 65 20 63 6c 69 65 6e 74 20 61 6c 72 65 61 64 79  e client already
163d1 20 68 61 73 20 61 20 77 72 69 74 65 2d 6c 6f 63   has a write-loc
163d2 6b 20 0a 20 20 2a 2a 20 6f 6e 20 70 61 67 65 20  k .  ** on page 
163d3 31 2c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  1, the transacti
163d4 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 6f 70 65  on cannot be ope
163d5 6e 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 71  ned. */.  rc = q
163d6 75 65 72 79 53 68 61 72 65 64 43 61 63 68 65 54  uerySharedCacheT
163d7 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 4d 41 53 54  ableLock(p, MAST
163d8 45 52 5f 52 4f 4f 54 2c 20 52 45 41 44 5f 4c 4f  ER_ROOT, READ_LO
163d9 43 4b 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54  CK);.  if( SQLIT
163da 45 5f 4f 4b 21 3d 72 63 20 29 20 67 6f 74 6f 20  E_OK!=rc ) goto 
163db 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 0a 20 20  trans_begun;..  
163dc 64 6f 20 7b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c  do {.    /* Call
163dd 20 6c 6f 63 6b 42 74 72 65 65 28 29 20 75 6e 74   lockBtree() unt
163de 69 6c 20 65 69 74 68 65 72 20 70 42 74 2d 3e 70  il either pBt->p
163df 50 61 67 65 31 20 69 73 20 70 6f 70 75 6c 61 74  Page1 is populat
163e0 65 64 20 6f 72 0a 20 20 20 20 2a 2a 20 6c 6f 63  ed or.    ** loc
163e1 6b 42 74 72 65 65 28 29 20 72 65 74 75 72 6e 73  kBtree() returns
163e2 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72   something other
163e3 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e   than SQLITE_OK.
163e4 20 6c 6f 63 6b 42 74 72 65 65 28 29 0a 20 20 20   lockBtree().   
163e5 20 2a 2a 20 6d 61 79 20 72 65 74 75 72 6e 20 53   ** may return S
163e6 51 4c 49 54 45 5f 4f 4b 20 62 75 74 20 6c 65 61  QLITE_OK but lea
163e7 76 65 20 70 42 74 2d 3e 70 50 61 67 65 31 20 73  ve pBt->pPage1 s
163e8 65 74 20 74 6f 20 30 20 69 66 20 61 66 74 65 72  et to 0 if after
163e9 0a 20 20 20 20 2a 2a 20 72 65 61 64 69 6e 67 20  .    ** reading 
163ea 70 61 67 65 20 31 20 69 74 20 64 69 73 63 6f 76  page 1 it discov
163eb 65 72 73 20 74 68 61 74 20 74 68 65 20 70 61 67  ers that the pag
163ec 65 2d 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61  e-size of the da
163ed 74 61 62 61 73 65 20 0a 20 20 20 20 2a 2a 20 66  tabase .    ** f
163ee 69 6c 65 20 69 73 20 6e 6f 74 20 70 42 74 2d 3e  ile is not pBt->
163ef 70 61 67 65 53 69 7a 65 2e 20 49 6e 20 74 68 69  pageSize. In thi
163f0 73 20 63 61 73 65 20 6c 6f 63 6b 42 74 72 65 65  s case lockBtree
163f1 28 29 20 77 69 6c 6c 20 75 70 64 61 74 65 0a 20  () will update. 
163f2 20 20 20 2a 2a 20 70 42 74 2d 3e 70 61 67 65 53     ** pBt->pageS
163f3 69 7a 65 20 74 6f 20 74 68 65 20 70 61 67 65 2d  ize to the page-
163f4 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69 6c 65  size of the file
163f5 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 20 20 2a 2f   on disk..    */
163f6 0a 20 20 20 20 77 68 69 6c 65 28 20 70 42 74 2d  .    while( pBt-
163f7 3e 70 50 61 67 65 31 3d 3d 30 20 26 26 20 53 51  >pPage1==0 && SQ
163f8 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 6c  LITE_OK==(rc = l
163f9 6f 63 6b 42 74 72 65 65 28 70 42 74 29 29 20 29  ockBtree(pBt)) )
163fa 3b 0a 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  ;..    if( rc==S
163fb 51 4c 49 54 45 5f 4f 4b 20 26 26 20 77 72 66 6c  QLITE_OK && wrfl
163fc 61 67 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ag ){.      if( 
163fd 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b  pBt->readOnly ){
163fe 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
163ff 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20  LITE_READONLY;. 
16400 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
16401 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
16402 50 61 67 65 72 42 65 67 69 6e 28 70 42 74 2d 3e  PagerBegin(pBt->
16403 70 50 61 67 65 72 2c 77 72 66 6c 61 67 3e 31 2c  pPager,wrflag>1,
16404 73 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65 6d  sqlite3TempInMem
16405 6f 72 79 28 70 2d 3e 64 62 29 29 3b 0a 20 20 20  ory(p->db));.   
16406 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
16407 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
16408 20 20 20 20 72 63 20 3d 20 6e 65 77 44 61 74 61      rc = newData
16409 62 61 73 65 28 70 42 74 29 3b 0a 20 20 20 20 20  base(pBt);.     
1640a 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1640b 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 72 63   }.  .    if( rc
1640c 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1640d 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65       unlockBtree
1640e 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20  IfUnused(pBt);. 
1640f 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 72     }.  }while( r
16410 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26  c==SQLITE_BUSY &
16411 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63  & pBt->inTransac
16412 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45  tion==TRANS_NONE
16413 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 62 74   &&.          bt
16414 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e  reeInvokeBusyHan
16415 64 6c 65 72 28 70 42 74 29 20 29 3b 0a 0a 20 20  dler(pBt) );..  
16416 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
16417 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  K ){.    if( p->
16418 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 4e  inTrans==TRANS_N
16419 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 70 42 74  ONE ){.      pBt
1641a 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2b 2b  ->nTransaction++
1641b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
1641c 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
1641d 48 45 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  HE.      if( p->
1641e 73 68 61 72 61 62 6c 65 20 29 7b 0a 09 61 73 73  sharable ){..ass
1641f 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 2e 70 42 74  ert( p->lock.pBt
16420 72 65 65 3d 3d 70 20 26 26 20 70 2d 3e 6c 6f 63  ree==p && p->loc
16421 6b 2e 69 54 61 62 6c 65 3d 3d 31 20 29 3b 0a 20  k.iTable==1 );. 
16422 20 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 2e 65         p->lock.e
16423 4c 6f 63 6b 20 3d 20 52 45 41 44 5f 4c 4f 43 4b  Lock = READ_LOCK
16424 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c 6f 63  ;.        p->loc
16425 6b 2e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70  k.pNext = pBt->p
16426 4c 6f 63 6b 3b 0a 20 20 20 20 20 20 20 20 70 42  Lock;.        pB
16427 74 2d 3e 70 4c 6f 63 6b 20 3d 20 26 70 2d 3e 6c  t->pLock = &p->l
16428 6f 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  ock;.      }.#en
16429 64 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d  dif.    }.    p-
1642a 3e 69 6e 54 72 61 6e 73 20 3d 20 28 77 72 66 6c  >inTrans = (wrfl
1642b 61 67 3f 54 52 41 4e 53 5f 57 52 49 54 45 3a 54  ag?TRANS_WRITE:T
1642c 52 41 4e 53 5f 52 45 41 44 29 3b 0a 20 20 20 20  RANS_READ);.    
1642d 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 70  if( p->inTrans>p
1642e 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
1642f 6e 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e  n ){.      pBt->
16430 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20  inTransaction = 
16431 70 2d 3e 69 6e 54 72 61 6e 73 3b 0a 20 20 20 20  p->inTrans;.    
16432 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
16433 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
16434 48 45 0a 20 20 20 20 69 66 28 20 77 72 66 6c 61  HE.    if( wrfla
16435 67 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  g ){.      asser
16436 74 28 20 21 70 42 74 2d 3e 70 57 72 69 74 65 72  t( !pBt->pWriter
16437 20 29 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70   );.      pBt->p
16438 57 72 69 74 65 72 20 3d 20 70 3b 0a 20 20 20 20  Writer = p;.    
16439 20 20 70 42 74 2d 3e 69 73 45 78 63 6c 75 73 69    pBt->isExclusi
1643a 76 65 20 3d 20 28 75 38 29 28 77 72 66 6c 61 67  ve = (u8)(wrflag
1643b 3e 31 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  >1);.    }.#endi
1643c 66 0a 20 20 7d 0a 0a 0a 74 72 61 6e 73 5f 62 65  f.  }...trans_be
1643d 67 75 6e 3a 0a 20 20 69 66 28 20 72 63 3d 3d 53  gun:.  if( rc==S
1643e 51 4c 49 54 45 5f 4f 4b 20 26 26 20 77 72 66 6c  QLITE_OK && wrfl
1643f 61 67 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69  ag ){.    /* Thi
16440 73 20 63 61 6c 6c 20 6d 61 6b 65 73 20 73 75 72  s call makes sur
16441 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72  e that the pager
16442 20 68 61 73 20 74 68 65 20 63 6f 72 72 65 63 74   has the correct
16443 20 6e 75 6d 62 65 72 20 6f 66 0a 20 20 20 20 2a   number of.    *
16444 2a 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e 74  * open savepoint
16445 73 2e 20 49 66 20 74 68 65 20 73 65 63 6f 6e 64  s. If the second
16446 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 67 72   parameter is gr
16447 65 61 74 65 72 20 74 68 61 6e 20 30 20 61 6e 64  eater than 0 and
16448 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 75 62 2d  .    ** the sub-
16449 6a 6f 75 72 6e 61 6c 20 69 73 20 6e 6f 74 20 61  journal is not a
1644a 6c 72 65 61 64 79 20 6f 70 65 6e 2c 20 74 68 65  lready open, the
1644b 6e 20 69 74 20 77 69 6c 6c 20 62 65 20 6f 70 65  n it will be ope
1644c 6e 65 64 20 68 65 72 65 2e 0a 20 20 20 20 2a 2f  ned here..    */
1644d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1644e 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f  3PagerOpenSavepo
1644f 69 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  int(pBt->pPager,
16450 20 70 2d 3e 64 62 2d 3e 6e 53 61 76 65 70 6f 69   p->db->nSavepoi
16451 6e 74 29 3b 0a 20 20 7d 0a 0a 20 20 62 74 72 65  nt);.  }..  btre
16452 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a 20  eIntegrity(p);. 
16453 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
16454 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ve(p);.  return 
16455 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  rc;.}..#ifndef S
16456 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
16457 41 43 55 55 4d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  ACUUM../*.** Set
16458 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70   the pointer-map
16459 20 65 6e 74 72 69 65 73 20 66 6f 72 20 61 6c 6c   entries for all
1645a 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 70 61 67   children of pag
1645b 65 20 70 50 61 67 65 2e 20 41 6c 73 6f 2c 20 69  e pPage. Also, i
1645c 66 0a 2a 2a 20 70 50 61 67 65 20 63 6f 6e 74 61  f.** pPage conta
1645d 69 6e 73 20 63 65 6c 6c 73 20 74 68 61 74 20 70  ins cells that p
1645e 6f 69 6e 74 20 74 6f 20 6f 76 65 72 66 6c 6f 77  oint to overflow
1645f 20 70 61 67 65 73 2c 20 73 65 74 20 74 68 65 20   pages, set the 
16460 70 6f 69 6e 74 65 72 0a 2a 2a 20 6d 61 70 20 65  pointer.** map e
16461 6e 74 72 69 65 73 20 66 6f 72 20 74 68 65 20 6f  ntries for the o
16462 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 73  verflow pages as
16463 20 77 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63   well..*/.static
16464 20 69 6e 74 20 73 65 74 43 68 69 6c 64 50 74 72   int setChildPtr
16465 6d 61 70 73 28 4d 65 6d 50 61 67 65 20 2a 70 50  maps(MemPage *pP
16466 61 67 65 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20  age){.  int i;  
16467 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16468 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
16469 75 6e 74 65 72 20 76 61 72 69 61 62 6c 65 20 2a  unter variable *
1646a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20  /.  int nCell;  
1646b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1646c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1646d 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 70 61 67   of cells in pag
1646e 65 20 70 50 61 67 65 20 2a 2f 0a 20 20 69 6e 74  e pPage */.  int
1646f 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
16470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16471 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a  /* Return code *
16472 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  /.  BtShared *pB
16473 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a  t = pPage->pBt;.
16474 20 20 75 38 20 69 73 49 6e 69 74 4f 72 69 67 20    u8 isInitOrig 
16475 3d 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 3b  = pPage->isInit;
16476 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 20 3d 20 70  .  Pgno pgno = p
16477 50 61 67 65 2d 3e 70 67 6e 6f 3b 0a 0a 20 20 61  Page->pgno;..  a
16478 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
16479 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d  utex_held(pPage-
1647a 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  >pBt->mutex) );.
1647b 20 20 72 63 20 3d 20 62 74 72 65 65 49 6e 69 74    rc = btreeInit
1647c 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 69  Page(pPage);.  i
1647d 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1647e 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 65 74   ){.    goto set
1647f 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f  _child_ptrmaps_o
16480 75 74 3b 0a 20 20 7d 0a 20 20 6e 43 65 6c 6c 20  ut;.  }.  nCell 
16481 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a  = pPage->nCell;.
16482 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43  .  for(i=0; i<nC
16483 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75  ell; i++){.    u
16484 38 20 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43  8 *pCell = findC
16485 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a 0a  ell(pPage, i);..
16486 20 20 20 20 70 74 72 6d 61 70 50 75 74 4f 76 66      ptrmapPutOvf
16487 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c  lPtr(pPage, pCel
16488 6c 2c 20 26 72 63 29 3b 0a 0a 20 20 20 20 69 66  l, &rc);..    if
16489 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  ( !pPage->leaf )
1648a 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 63 68 69  {.      Pgno chi
1648b 6c 64 50 67 6e 6f 20 3d 20 67 65 74 34 62 79 74  ldPgno = get4byt
1648c 65 28 70 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20  e(pCell);.      
1648d 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 63  ptrmapPut(pBt, c
1648e 68 69 6c 64 50 67 6e 6f 2c 20 50 54 52 4d 41 50  hildPgno, PTRMAP
1648f 5f 42 54 52 45 45 2c 20 70 67 6e 6f 2c 20 26 72  _BTREE, pgno, &r
16490 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  c);.    }.  }.. 
16491 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
16492 66 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 63 68  f ){.    Pgno ch
16493 69 6c 64 50 67 6e 6f 20 3d 20 67 65 74 34 62 79  ildPgno = get4by
16494 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61  te(&pPage->aData
16495 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65  [pPage->hdrOffse
16496 74 2b 38 5d 29 3b 0a 20 20 20 20 70 74 72 6d 61  t+8]);.    ptrma
16497 70 50 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50  pPut(pBt, childP
16498 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45  gno, PTRMAP_BTRE
16499 45 2c 20 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20  E, pgno, &rc);. 
1649a 20 7d 0a 0a 73 65 74 5f 63 68 69 6c 64 5f 70 74   }..set_child_pt
1649b 72 6d 61 70 73 5f 6f 75 74 3a 0a 20 20 70 50 61  rmaps_out:.  pPa
1649c 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 69 73 49  ge->isInit = isI
1649d 6e 69 74 4f 72 69 67 3b 0a 20 20 72 65 74 75 72  nitOrig;.  retur
1649e 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  n rc;.}../*.** S
1649f 6f 6d 65 77 68 65 72 65 20 6f 6e 20 70 50 61 67  omewhere on pPag
164a0 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  e is a pointer t
164a1 6f 20 70 61 67 65 20 69 46 72 6f 6d 2e 20 20 4d  o page iFrom.  M
164a2 6f 64 69 66 79 20 74 68 69 73 20 70 6f 69 6e 74  odify this point
164a3 65 72 20 73 6f 0a 2a 2a 20 74 68 61 74 20 69 74  er so.** that it
164a4 20 70 6f 69 6e 74 73 20 74 6f 20 69 54 6f 2e 20   points to iTo. 
164a5 50 61 72 61 6d 65 74 65 72 20 65 54 79 70 65 20  Parameter eType 
164a6 64 65 73 63 72 69 62 65 73 20 74 68 65 20 74 79  describes the ty
164a7 70 65 20 6f 66 20 70 6f 69 6e 74 65 72 20 74 6f  pe of pointer to
164a8 0a 2a 2a 20 62 65 20 6d 6f 64 69 66 69 65 64 2c  .** be modified,
164a9 20 61 73 20 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a   as  follows:.**
164aa 0a 2a 2a 20 50 54 52 4d 41 50 5f 42 54 52 45 45  .** PTRMAP_BTREE
164ab 3a 20 20 20 20 20 70 50 61 67 65 20 69 73 20 61  :     pPage is a
164ac 20 62 74 72 65 65 2d 70 61 67 65 2e 20 54 68 65   btree-page. The
164ad 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74 73 20   pointer points 
164ae 61 74 20 61 20 63 68 69 6c 64 20 0a 2a 2a 20 20  at a child .**  
164af 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
164b0 20 70 61 67 65 20 6f 66 20 70 50 61 67 65 2e 0a   page of pPage..
164b1 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45  **.** PTRMAP_OVE
164b2 52 46 4c 4f 57 31 3a 20 70 50 61 67 65 20 69 73  RFLOW1: pPage is
164b3 20 61 20 62 74 72 65 65 2d 70 61 67 65 2e 20 54   a btree-page. T
164b4 68 65 20 70 6f 69 6e 74 65 72 20 70 6f 69 6e 74  he pointer point
164b5 73 20 61 74 20 61 6e 20 6f 76 65 72 66 6c 6f 77  s at an overflow
164b6 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
164b7 20 20 20 20 20 20 70 61 67 65 20 70 6f 69 6e 74        page point
164b8 65 64 20 74 6f 20 62 79 20 6f 6e 65 20 6f 66 20  ed to by one of 
164b9 74 68 65 20 63 65 6c 6c 73 20 6f 6e 20 70 50 61  the cells on pPa
164ba 67 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50  ge..**.** PTRMAP
164bb 5f 4f 56 45 52 46 4c 4f 57 32 3a 20 70 50 61 67  _OVERFLOW2: pPag
164bc 65 20 69 73 20 61 6e 20 6f 76 65 72 66 6c 6f 77  e is an overflow
164bd 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74  -page. The point
164be 65 72 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65  er points at the
164bf 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20 20 20 20   next.**        
164c0 20 20 20 20 20 20 20 20 20 20 20 6f 76 65 72 66             overf
164c1 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 20  low page in the 
164c2 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  list..*/.static 
164c3 69 6e 74 20 6d 6f 64 69 66 79 50 61 67 65 50 6f  int modifyPagePo
164c4 69 6e 74 65 72 28 4d 65 6d 50 61 67 65 20 2a 70  inter(MemPage *p
164c5 50 61 67 65 2c 20 50 67 6e 6f 20 69 46 72 6f 6d  Page, Pgno iFrom
164c6 2c 20 50 67 6e 6f 20 69 54 6f 2c 20 75 38 20 65  , Pgno iTo, u8 e
164c7 54 79 70 65 29 7b 0a 20 20 61 73 73 65 72 74 28  Type){.  assert(
164c8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
164c9 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e  eld(pPage->pBt->
164ca 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
164cb 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
164cc 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67  Iswriteable(pPag
164cd 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20  e->pDbPage) );. 
164ce 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d   if( eType==PTRM
164cf 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 29 7b 0a  AP_OVERFLOW2 ){.
164d0 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74      /* The point
164d1 65 72 20 69 73 20 61 6c 77 61 79 73 20 74 68 65  er is always the
164d2 20 66 69 72 73 74 20 34 20 62 79 74 65 73 20 6f   first 4 bytes o
164d3 66 20 74 68 65 20 70 61 67 65 20 69 6e 20 74 68  f the page in th
164d4 69 73 20 63 61 73 65 2e 20 20 2a 2f 0a 20 20 20  is case.  */.   
164d5 20 69 66 28 20 67 65 74 34 62 79 74 65 28 70 50   if( get4byte(pP
164d6 61 67 65 2d 3e 61 44 61 74 61 29 21 3d 69 46 72  age->aData)!=iFr
164d7 6f 6d 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  om ){.      retu
164d8 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
164d9 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20  T_BKPT;.    }.  
164da 20 20 70 75 74 34 62 79 74 65 28 70 50 61 67 65    put4byte(pPage
164db 2d 3e 61 44 61 74 61 2c 20 69 54 6f 29 3b 0a 20  ->aData, iTo);. 
164dc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 38 20 69   }else{.    u8 i
164dd 73 49 6e 69 74 4f 72 69 67 20 3d 20 70 50 61 67  sInitOrig = pPag
164de 65 2d 3e 69 73 49 6e 69 74 3b 0a 20 20 20 20 69  e->isInit;.    i
164df 6e 74 20 69 3b 0a 20 20 20 20 69 6e 74 20 6e 43  nt i;.    int nC
164e0 65 6c 6c 3b 0a 0a 20 20 20 20 62 74 72 65 65 49  ell;..    btreeI
164e1 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a  nitPage(pPage);.
164e2 20 20 20 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67      nCell = pPag
164e3 65 2d 3e 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 66  e->nCell;..    f
164e4 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b  or(i=0; i<nCell;
164e5 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20   i++){.      u8 
164e6 2a 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c  *pCell = findCel
164e7 6c 28 70 50 61 67 65 2c 20 69 29 3b 0a 20 20 20  l(pPage, i);.   
164e8 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54     if( eType==PT
164e9 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 29  RMAP_OVERFLOW1 )
164ea 7b 0a 20 20 20 20 20 20 20 20 43 65 6c 6c 49 6e  {.        CellIn
164eb 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20  fo info;.       
164ec 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50   btreeParseCellP
164ed 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c  tr(pPage, pCell,
164ee 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20   &info);.       
164ef 20 69 66 28 20 69 6e 66 6f 2e 69 4f 76 65 72 66   if( info.iOverf
164f0 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 20  low ){.         
164f1 20 69 66 28 20 69 46 72 6f 6d 3d 3d 67 65 74 34   if( iFrom==get4
164f2 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f  byte(&pCell[info
164f3 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 20 29 7b 0a  .iOverflow]) ){.
164f4 20 20 20 20 20 20 20 20 20 20 20 20 70 75 74 34              put4
164f5 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f  byte(&pCell[info
164f6 2e 69 4f 76 65 72 66 6c 6f 77 5d 2c 20 69 54 6f  .iOverflow], iTo
164f7 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  );.            b
164f8 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
164f9 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
164fa 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
164fb 20 69 66 28 20 67 65 74 34 62 79 74 65 28 70 43   if( get4byte(pC
164fc 65 6c 6c 29 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20  ell)==iFrom ){. 
164fd 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74           put4byt
164fe 65 28 70 43 65 6c 6c 2c 20 69 54 6f 29 3b 0a 20  e(pCell, iTo);. 
164ff 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
16500 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
16501 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 69  }.    }.  .    i
16502 66 28 20 69 3d 3d 6e 43 65 6c 6c 20 29 7b 0a 20  f( i==nCell ){. 
16503 20 20 20 20 20 69 66 28 20 65 54 79 70 65 21 3d       if( eType!=
16504 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20  PTRMAP_BTREE || 
16505 0a 20 20 20 20 20 20 20 20 20 20 67 65 74 34 62  .          get4b
16506 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74  yte(&pPage->aDat
16507 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  a[pPage->hdrOffs
16508 65 74 2b 38 5d 29 21 3d 69 46 72 6f 6d 20 29 7b  et+8])!=iFrom ){
16509 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
1650a 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
1650b 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  KPT;.      }.   
1650c 20 20 20 70 75 74 34 62 79 74 65 28 26 70 50 61     put4byte(&pPa
1650d 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d  ge->aData[pPage-
1650e 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 2c 20 69  >hdrOffset+8], i
1650f 54 6f 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  To);.    }..    
16510 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20  pPage->isInit = 
16511 69 73 49 6e 69 74 4f 72 69 67 3b 0a 20 20 7d 0a  isInitOrig;.  }.
16512 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
16513 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f  OK;.}.../*.** Mo
16514 76 65 20 74 68 65 20 6f 70 65 6e 20 64 61 74 61  ve the open data
16515 62 61 73 65 20 70 61 67 65 20 70 44 62 50 61 67  base page pDbPag
16516 65 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 69 46  e to location iF
16517 72 65 65 50 61 67 65 20 69 6e 20 74 68 65 20 0a  reePage in the .
16518 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54 68 65  ** database. The
16519 20 70 44 62 50 61 67 65 20 72 65 66 65 72 65 6e   pDbPage referen
1651a 63 65 20 72 65 6d 61 69 6e 73 20 76 61 6c 69 64  ce remains valid
1651b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 73 43 6f  ..**.** The isCo
1651c 6d 6d 69 74 20 66 6c 61 67 20 69 6e 64 69 63 61  mmit flag indica
1651d 74 65 73 20 74 68 61 74 20 74 68 65 72 65 20 69  tes that there i
1651e 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d  s no need to rem
1651f 65 6d 62 65 72 20 74 68 61 74 0a 2a 2a 20 74 68  ember that.** th
16520 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73 20  e journal needs 
16521 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64 20 62  to be sync()ed b
16522 65 66 6f 72 65 20 64 61 74 61 62 61 73 65 20 70  efore database p
16523 61 67 65 20 70 44 62 50 61 67 65 2d 3e 70 67 6e  age pDbPage->pgn
16524 6f 20 0a 2a 2a 20 63 61 6e 20 62 65 20 77 72 69  o .** can be wri
16525 74 74 65 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c  tten to. The cal
16526 6c 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20  ler has already 
16527 70 72 6f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20  promised not to 
16528 77 72 69 74 65 20 74 6f 20 74 68 61 74 0a 2a 2a  write to that.**
16529 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   page..*/.static
1652a 20 69 6e 74 20 72 65 6c 6f 63 61 74 65 50 61 67   int relocatePag
1652b 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  e(.  BtShared *p
1652c 42 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  Bt,           /*
1652d 20 42 74 72 65 65 20 2a 2f 0a 20 20 4d 65 6d 50   Btree */.  MemP
1652e 61 67 65 20 2a 70 44 62 50 61 67 65 2c 20 20 20  age *pDbPage,   
1652f 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67       /* Open pag
16530 65 20 74 6f 20 6d 6f 76 65 20 2a 2f 0a 20 20 75  e to move */.  u
16531 38 20 65 54 79 70 65 2c 20 20 20 20 20 20 20 20  8 eType,        
16532 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
16533 65 72 20 6d 61 70 20 27 74 79 70 65 27 20 65 6e  er map 'type' en
16534 74 72 79 20 66 6f 72 20 70 44 62 50 61 67 65 20  try for pDbPage 
16535 2a 2f 0a 20 20 50 67 6e 6f 20 69 50 74 72 50 61  */.  Pgno iPtrPa
16536 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ge,           /*
16537 20 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 70 61   Pointer map 'pa
16538 67 65 2d 6e 6f 27 20 65 6e 74 72 79 20 66 6f 72  ge-no' entry for
16539 20 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67   pDbPage */.  Pg
1653a 6e 6f 20 69 46 72 65 65 50 61 67 65 2c 20 20 20  no iFreePage,   
1653b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f         /* The lo
1653c 63 61 74 69 6f 6e 20 74 6f 20 6d 6f 76 65 20 70  cation to move p
1653d 44 62 50 61 67 65 20 74 6f 20 2a 2f 0a 20 20 69  DbPage to */.  i
1653e 6e 74 20 69 73 43 6f 6d 6d 69 74 20 20 20 20 20  nt isCommit     
1653f 20 20 20 20 20 20 20 20 2f 2a 20 69 73 43 6f 6d          /* isCom
16540 6d 69 74 20 66 6c 61 67 20 70 61 73 73 65 64 20  mit flag passed 
16541 74 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d  to sqlite3PagerM
16542 6f 76 65 70 61 67 65 20 2a 2f 0a 29 7b 0a 20 20  ovepage */.){.  
16543 4d 65 6d 50 61 67 65 20 2a 70 50 74 72 50 61 67  MemPage *pPtrPag
16544 65 3b 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65  e;   /* The page
16545 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61   that contains a
16546 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 44 62 50   pointer to pDbP
16547 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 44  age */.  Pgno iD
16548 62 50 61 67 65 20 3d 20 70 44 62 50 61 67 65 2d  bPage = pDbPage-
16549 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20 2a  >pgno;.  Pager *
1654a 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50  pPager = pBt->pP
1654b 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  ager;.  int rc;.
1654c 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65  .  assert( eType
1654d 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  ==PTRMAP_OVERFLO
1654e 57 32 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52  W2 || eType==PTR
1654f 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 7c 7c  MAP_OVERFLOW1 ||
16550 20 0a 20 20 20 20 20 20 65 54 79 70 65 3d 3d 50   .      eType==P
16551 54 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65  TRMAP_BTREE || e
16552 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f  Type==PTRMAP_ROO
16553 54 50 41 47 45 20 29 3b 0a 20 20 61 73 73 65 72  TPAGE );.  asser
16554 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
16555 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
16556 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
16557 44 62 50 61 67 65 2d 3e 70 42 74 3d 3d 70 42 74  DbPage->pBt==pBt
16558 20 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20 70   );..  /* Move p
16559 61 67 65 20 69 44 62 50 61 67 65 20 66 72 6f 6d  age iDbPage from
1655a 20 69 74 73 20 63 75 72 72 65 6e 74 20 6c 6f 63   its current loc
1655b 61 74 69 6f 6e 20 74 6f 20 70 61 67 65 20 6e 75  ation to page nu
1655c 6d 62 65 72 20 69 46 72 65 65 50 61 67 65 20 2a  mber iFreePage *
1655d 2f 0a 20 20 54 52 41 43 45 28 28 22 41 55 54 4f  /.  TRACE(("AUTO
1655e 56 41 43 55 55 4d 3a 20 4d 6f 76 69 6e 67 20 25  VACUUM: Moving %
1655f 64 20 74 6f 20 66 72 65 65 20 70 61 67 65 20 25  d to free page %
16560 64 20 28 70 74 72 20 70 61 67 65 20 25 64 20 74  d (ptr page %d t
16561 79 70 65 20 25 64 29 5c 6e 22 2c 20 0a 20 20 20  ype %d)\n", .   
16562 20 20 20 69 44 62 50 61 67 65 2c 20 69 46 72 65     iDbPage, iFre
16563 65 50 61 67 65 2c 20 69 50 74 72 50 61 67 65 2c  ePage, iPtrPage,
16564 20 65 54 79 70 65 29 29 3b 0a 20 20 72 63 20 3d   eType));.  rc =
16565 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76   sqlite3PagerMov
16566 65 70 61 67 65 28 70 50 61 67 65 72 2c 20 70 44  epage(pPager, pD
16567 62 50 61 67 65 2d 3e 70 44 62 50 61 67 65 2c 20  bPage->pDbPage, 
16568 69 46 72 65 65 50 61 67 65 2c 20 69 73 43 6f 6d  iFreePage, isCom
16569 6d 69 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  mit);.  if( rc!=
1656a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1656b 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
1656c 20 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20    pDbPage->pgno 
1656d 3d 20 69 46 72 65 65 50 61 67 65 3b 0a 0a 20 20  = iFreePage;..  
1656e 2f 2a 20 49 66 20 70 44 62 50 61 67 65 20 77 61  /* If pDbPage wa
1656f 73 20 61 20 62 74 72 65 65 2d 70 61 67 65 2c 20  s a btree-page, 
16570 74 68 65 6e 20 69 74 20 6d 61 79 20 68 61 76 65  then it may have
16571 20 63 68 69 6c 64 20 70 61 67 65 73 20 61 6e 64   child pages and
16572 2f 6f 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 74  /or cells.  ** t
16573 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76 65  hat point to ove
16574 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65  rflow pages. The
16575 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74   pointer map ent
16576 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 74 68 65  ries for all the
16577 73 65 0a 20 20 2a 2a 20 70 61 67 65 73 20 6e 65  se.  ** pages ne
16578 65 64 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64  ed to be changed
16579 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70  ..  **.  ** If p
1657a 44 62 50 61 67 65 20 69 73 20 61 6e 20 6f 76 65  DbPage is an ove
1657b 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e  rflow page, then
1657c 20 74 68 65 20 66 69 72 73 74 20 34 20 62 79 74   the first 4 byt
1657d 65 73 20 6d 61 79 20 73 74 6f 72 65 20 61 0a 20  es may store a. 
1657e 20 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61   ** pointer to a
1657f 20 73 75 62 73 65 71 75 65 6e 74 20 6f 76 65 72   subsequent over
16580 66 6c 6f 77 20 70 61 67 65 2e 20 49 66 20 74 68  flow page. If th
16581 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20  is is the case, 
16582 74 68 65 6e 0a 20 20 2a 2a 20 74 68 65 20 70 6f  then.  ** the po
16583 69 6e 74 65 72 20 6d 61 70 20 6e 65 65 64 73 20  inter map needs 
16584 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 66 6f  to be updated fo
16585 72 20 74 68 65 20 73 75 62 73 65 71 75 65 6e 74  r the subsequent
16586 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a   overflow page..
16587 20 20 2a 2f 0a 20 20 69 66 28 20 65 54 79 70 65    */.  if( eType
16588 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c  ==PTRMAP_BTREE |
16589 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f  | eType==PTRMAP_
1658a 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20  ROOTPAGE ){.    
1658b 72 63 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72  rc = setChildPtr
1658c 6d 61 70 73 28 70 44 62 50 61 67 65 29 3b 0a 20  maps(pDbPage);. 
1658d 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1658e 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65  E_OK ){.      re
1658f 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
16590 20 7d 65 6c 73 65 7b 0a 20 20 20 20 50 67 6e 6f   }else{.    Pgno
16591 20 6e 65 78 74 4f 76 66 6c 20 3d 20 67 65 74 34   nextOvfl = get4
16592 62 79 74 65 28 70 44 62 50 61 67 65 2d 3e 61 44  byte(pDbPage->aD
16593 61 74 61 29 3b 0a 20 20 20 20 69 66 28 20 6e 65  ata);.    if( ne
16594 78 74 4f 76 66 6c 21 3d 30 20 29 7b 0a 20 20 20  xtOvfl!=0 ){.   
16595 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74     ptrmapPut(pBt
16596 2c 20 6e 65 78 74 4f 76 66 6c 2c 20 50 54 52 4d  , nextOvfl, PTRM
16597 41 50 5f 4f 56 45 52 46 4c 4f 57 32 2c 20 69 46  AP_OVERFLOW2, iF
16598 72 65 65 50 61 67 65 2c 20 26 72 63 29 3b 0a 20  reePage, &rc);. 
16599 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1659a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1659b 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
1659c 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
1659d 20 20 2f 2a 20 46 69 78 20 74 68 65 20 64 61 74    /* Fix the dat
1659e 61 62 61 73 65 20 70 6f 69 6e 74 65 72 20 6f 6e  abase pointer on
1659f 20 70 61 67 65 20 69 50 74 72 50 61 67 65 20 74   page iPtrPage t
165a0 68 61 74 20 70 6f 69 6e 74 65 64 20 61 74 20 69  hat pointed at i
165a1 44 62 50 61 67 65 20 73 6f 0a 20 20 2a 2a 20 74  DbPage so.  ** t
165a2 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 61 74  hat it points at
165a3 20 69 46 72 65 65 50 61 67 65 2e 20 41 6c 73 6f   iFreePage. Also
165a4 20 66 69 78 20 74 68 65 20 70 6f 69 6e 74 65 72   fix the pointer
165a5 20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20   map entry for. 
165a6 20 2a 2a 20 69 50 74 72 50 61 67 65 2e 0a 20 20   ** iPtrPage..  
165a7 2a 2f 0a 20 20 69 66 28 20 65 54 79 70 65 21 3d  */.  if( eType!=
165a8 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20  PTRMAP_ROOTPAGE 
165a9 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65  ){.    rc = btre
165aa 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69 50  eGetPage(pBt, iP
165ab 74 72 50 61 67 65 2c 20 26 70 50 74 72 50 61 67  trPage, &pPtrPag
165ac 65 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72  e, 0);.    if( r
165ad 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
165ae 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
165af 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
165b0 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
165b1 65 28 70 50 74 72 50 61 67 65 2d 3e 70 44 62 50  e(pPtrPage->pDbP
165b2 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63  age);.    if( rc
165b3 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
165b4 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
165b5 28 70 50 74 72 50 61 67 65 29 3b 0a 20 20 20 20  (pPtrPage);.    
165b6 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
165b7 20 7d 0a 20 20 20 20 72 63 20 3d 20 6d 6f 64 69   }.    rc = modi
165b8 66 79 50 61 67 65 50 6f 69 6e 74 65 72 28 70 50  fyPagePointer(pP
165b9 74 72 50 61 67 65 2c 20 69 44 62 50 61 67 65 2c  trPage, iDbPage,
165ba 20 69 46 72 65 65 50 61 67 65 2c 20 65 54 79 70   iFreePage, eTyp
165bb 65 29 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 50  e);.    releaseP
165bc 61 67 65 28 70 50 74 72 50 61 67 65 29 3b 0a 20  age(pPtrPage);. 
165bd 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
165be 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 74  E_OK ){.      pt
165bf 72 6d 61 70 50 75 74 28 70 42 74 2c 20 69 46 72  rmapPut(pBt, iFr
165c0 65 65 50 61 67 65 2c 20 65 54 79 70 65 2c 20 69  eePage, eType, i
165c1 50 74 72 50 61 67 65 2c 20 26 72 63 29 3b 0a 20  PtrPage, &rc);. 
165c2 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
165c3 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77  n rc;.}../* Forw
165c4 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  ard declaration 
165c5 72 65 71 75 69 72 65 64 20 62 79 20 69 6e 63 72  required by incr
165c6 56 61 63 75 75 6d 53 74 65 70 28 29 2e 20 2a 2f  VacuumStep(). */
165c7 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f  .static int allo
165c8 63 61 74 65 42 74 72 65 65 50 61 67 65 28 42 74  cateBtreePage(Bt
165c9 53 68 61 72 65 64 20 2a 2c 20 4d 65 6d 50 61 67  Shared *, MemPag
165ca 65 20 2a 2a 2c 20 50 67 6e 6f 20 2a 2c 20 50 67  e **, Pgno *, Pg
165cb 6e 6f 2c 20 75 38 29 3b 0a 0a 2f 2a 0a 2a 2a 20  no, u8);../*.** 
165cc 50 65 72 66 6f 72 6d 20 61 20 73 69 6e 67 6c 65  Perform a single
165cd 20 73 74 65 70 20 6f 66 20 61 6e 20 69 6e 63 72   step of an incr
165ce 65 6d 65 6e 74 61 6c 2d 76 61 63 75 75 6d 2e 20  emental-vacuum. 
165cf 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a  If successful,.*
165d0 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  * return SQLITE_
165d1 4f 4b 2e 20 49 66 20 74 68 65 72 65 20 69 73 20  OK. If there is 
165d2 6e 6f 20 77 6f 72 6b 20 74 6f 20 64 6f 20 28 61  no work to do (a
165d3 6e 64 20 74 68 65 72 65 66 6f 72 65 20 6e 6f 0a  nd therefore no.
165d4 2a 2a 20 70 6f 69 6e 74 20 69 6e 20 63 61 6c 6c  ** point in call
165d5 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ing this functio
165d6 6e 20 61 67 61 69 6e 29 2c 20 72 65 74 75 72 6e  n again), return
165d7 20 53 51 4c 49 54 45 5f 44 4f 4e 45 2e 0a 2a 2a   SQLITE_DONE..**
165d8 0a 2a 2a 20 4d 6f 72 65 20 73 70 65 63 69 66 69  .** More specifi
165d9 63 6c 79 2c 20 74 68 69 73 20 66 75 6e 63 74 69  cly, this functi
165da 6f 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72  on attempts to r
165db 65 2d 6f 72 67 61 6e 69 7a 65 20 74 68 65 20 0a  e-organize the .
165dc 2a 2a 20 64 61 74 61 62 61 73 65 20 73 6f 20 74  ** database so t
165dd 68 61 74 20 74 68 65 20 6c 61 73 74 20 70 61 67  hat the last pag
165de 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 63 75  e of the file cu
165df 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 0a 2a  rrently in use.*
165e0 2a 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20 69  * is no longer i
165e1 6e 20 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  n use..**.** If 
165e2 74 68 65 20 6e 46 69 6e 20 70 61 72 61 6d 65 74  the nFin paramet
165e3 65 72 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  er is non-zero, 
165e4 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 73  this function as
165e5 73 75 6d 65 73 0a 2a 2a 20 74 68 61 74 20 74 68  sumes.** that th
165e6 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c 20 6b 65  e caller will ke
165e7 65 70 20 63 61 6c 6c 69 6e 67 20 69 6e 63 72 56  ep calling incrV
165e8 61 63 75 75 6d 53 74 65 70 28 29 20 75 6e 74 69  acuumStep() unti
165e9 6c 0a 2a 2a 20 69 74 20 72 65 74 75 72 6e 73 20  l.** it returns 
165ea 53 51 4c 49 54 45 5f 44 4f 4e 45 20 6f 72 20 61  SQLITE_DONE or a
165eb 6e 20 65 72 72 6f 72 2c 20 61 6e 64 20 74 68 61  n error, and tha
165ec 74 20 6e 46 69 6e 20 69 73 20 74 68 65 0a 2a 2a  t nFin is the.**
165ed 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
165ee 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
165ef 6c 65 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20  le will contain 
165f0 61 66 74 65 72 20 74 68 69 73 20 0a 2a 2a 20 70  after this .** p
165f1 72 6f 63 65 73 73 20 69 73 20 63 6f 6d 70 6c 65  rocess is comple
165f2 74 65 2e 20 20 49 66 20 6e 46 69 6e 20 69 73 20  te.  If nFin is 
165f3 7a 65 72 6f 2c 20 69 74 20 69 73 20 61 73 73 75  zero, it is assu
165f4 6d 65 64 20 74 68 61 74 0a 2a 2a 20 69 6e 63 72  med that.** incr
165f5 56 61 63 75 75 6d 53 74 65 70 28 29 20 77 69 6c  VacuumStep() wil
165f6 6c 20 62 65 20 63 61 6c 6c 65 64 20 61 20 66 69  l be called a fi
165f7 6e 69 74 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74  nite amount of t
165f8 69 6d 65 73 0a 2a 2a 20 77 68 69 63 68 20 6d 61  imes.** which ma
165f9 79 20 6f 72 20 6d 61 79 20 6e 6f 74 20 65 6d 70  y or may not emp
165fa 74 79 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e  ty the freelist.
165fb 20 20 41 20 66 75 6c 6c 20 61 75 74 6f 76 61 63    A full autovac
165fc 75 75 6d 0a 2a 2a 20 68 61 73 20 6e 46 69 6e 3e  uum.** has nFin>
165fd 30 2e 20 20 41 20 22 50 52 41 47 4d 41 20 69 6e  0.  A "PRAGMA in
165fe 63 72 65 6d 65 6e 74 61 6c 5f 76 61 63 75 75 6d  cremental_vacuum
165ff 22 20 68 61 73 20 6e 46 69 6e 3d 3d 30 2e 0a 2a  " has nFin==0..*
16600 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 63  /.static int inc
16601 72 56 61 63 75 75 6d 53 74 65 70 28 42 74 53 68  rVacuumStep(BtSh
16602 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20  ared *pBt, Pgno 
16603 6e 46 69 6e 2c 20 50 67 6e 6f 20 69 4c 61 73 74  nFin, Pgno iLast
16604 50 67 29 7b 0a 20 20 50 67 6e 6f 20 6e 46 72 65  Pg){.  Pgno nFre
16605 65 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20  eList;          
16606 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
16607 67 65 73 20 73 74 69 6c 6c 20 6f 6e 20 74 68 65  ges still on the
16608 20 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20   free-list */.. 
16609 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1660a 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
1660b 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73  >mutex) );.  ass
1660c 65 72 74 28 20 69 4c 61 73 74 50 67 3e 6e 46 69  ert( iLastPg>nFi
1660d 6e 20 29 3b 0a 0a 20 20 69 66 28 20 21 50 54 52  n );..  if( !PTR
1660e 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20  MAP_ISPAGE(pBt, 
1660f 69 4c 61 73 74 50 67 29 20 26 26 20 69 4c 61 73  iLastPg) && iLas
16610 74 50 67 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54  tPg!=PENDING_BYT
16611 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20  E_PAGE(pBt) ){. 
16612 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 75     int rc;.    u
16613 38 20 65 54 79 70 65 3b 0a 20 20 20 20 50 67 6e  8 eType;.    Pgn
16614 6f 20 69 50 74 72 50 61 67 65 3b 0a 0a 20 20 20  o iPtrPage;..   
16615 20 6e 46 72 65 65 4c 69 73 74 20 3d 20 67 65 74   nFreeList = get
16616 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67  4byte(&pBt->pPag
16617 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a  e1->aData[36]);.
16618 20 20 20 20 69 66 28 20 6e 46 72 65 65 4c 69 73      if( nFreeLis
16619 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65  t==0 ){.      re
1661a 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
1661b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20  ;.    }..    rc 
1661c 3d 20 70 74 72 6d 61 70 47 65 74 28 70 42 74 2c  = ptrmapGet(pBt,
1661d 20 69 4c 61 73 74 50 67 2c 20 26 65 54 79 70 65   iLastPg, &eType
1661e 2c 20 26 69 50 74 72 50 61 67 65 29 3b 0a 20 20  , &iPtrPage);.  
1661f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
16620 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74  _OK ){.      ret
16621 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
16622 20 20 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52    if( eType==PTR
16623 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 29 7b 0a  MAP_ROOTPAGE ){.
16624 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
16625 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
16626 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
16627 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46   eType==PTRMAP_F
16628 52 45 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20  REEPAGE ){.     
16629 20 69 66 28 20 6e 46 69 6e 3d 3d 30 20 29 7b 0a   if( nFin==0 ){.
1662a 20 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76          /* Remov
1662b 65 20 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20  e the page from 
1662c 74 68 65 20 66 69 6c 65 73 20 66 72 65 65 2d 6c  the files free-l
1662d 69 73 74 2e 20 54 68 69 73 20 69 73 20 6e 6f 74  ist. This is not
1662e 20 72 65 71 75 69 72 65 64 0a 20 20 20 20 20 20   required.      
1662f 20 20 2a 2a 20 69 66 20 6e 46 69 6e 20 69 73 20    ** if nFin is 
16630 6e 6f 6e 2d 7a 65 72 6f 2e 20 49 6e 20 74 68 61  non-zero. In tha
16631 74 20 63 61 73 65 2c 20 74 68 65 20 66 72 65 65  t case, the free
16632 2d 6c 69 73 74 20 77 69 6c 6c 20 62 65 0a 20 20  -list will be.  
16633 20 20 20 20 20 20 2a 2a 20 74 72 75 6e 63 61 74        ** truncat
16634 65 64 20 74 6f 20 7a 65 72 6f 20 61 66 74 65 72  ed to zero after
16635 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   this function r
16636 65 74 75 72 6e 73 2c 20 73 6f 20 69 74 20 64 6f  eturns, so it do
16637 65 73 6e 27 74 20 0a 20 20 20 20 20 20 20 20 2a  esn't .        *
16638 2a 20 6d 61 74 74 65 72 20 69 66 20 69 74 20 73  * matter if it s
16639 74 69 6c 6c 20 63 6f 6e 74 61 69 6e 73 20 73 6f  till contains so
1663a 6d 65 20 67 61 72 62 61 67 65 20 65 6e 74 72 69  me garbage entri
1663b 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  es..        */. 
1663c 20 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65         Pgno iFre
1663d 65 50 67 3b 0a 20 20 20 20 20 20 20 20 4d 65 6d  ePg;.        Mem
1663e 50 61 67 65 20 2a 70 46 72 65 65 50 67 3b 0a 20  Page *pFreePg;. 
1663f 20 20 20 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f         rc = allo
16640 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42  cateBtreePage(pB
16641 74 2c 20 26 70 46 72 65 65 50 67 2c 20 26 69 46  t, &pFreePg, &iF
16642 72 65 65 50 67 2c 20 69 4c 61 73 74 50 67 2c 20  reePg, iLastPg, 
16643 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  1);.        if( 
16644 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
16645 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
16646 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
16647 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
16648 69 46 72 65 65 50 67 3d 3d 69 4c 61 73 74 50 67  iFreePg==iLastPg
16649 20 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65   );.        rele
1664a 61 73 65 50 61 67 65 28 70 46 72 65 65 50 67 29  asePage(pFreePg)
1664b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20  ;.      }.    } 
1664c 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 50 67 6e  else {.      Pgn
1664d 6f 20 69 46 72 65 65 50 67 3b 20 20 20 20 20 20  o iFreePg;      
1664e 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
1664f 6f 66 20 66 72 65 65 20 70 61 67 65 20 74 6f 20  of free page to 
16650 6d 6f 76 65 20 70 4c 61 73 74 50 67 20 74 6f 20  move pLastPg to 
16651 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65  */.      MemPage
16652 20 2a 70 4c 61 73 74 50 67 3b 0a 0a 20 20 20 20   *pLastPg;..    
16653 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50    rc = btreeGetP
16654 61 67 65 28 70 42 74 2c 20 69 4c 61 73 74 50 67  age(pBt, iLastPg
16655 2c 20 26 70 4c 61 73 74 50 67 2c 20 30 29 3b 0a  , &pLastPg, 0);.
16656 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
16657 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
16658 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
16659 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20      }..      /* 
1665a 49 66 20 6e 46 69 6e 20 69 73 20 7a 65 72 6f 2c  If nFin is zero,
1665b 20 74 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20   this loop runs 
1665c 65 78 61 63 74 6c 79 20 6f 6e 63 65 20 61 6e 64  exactly once and
1665d 20 70 61 67 65 20 70 4c 61 73 74 50 67 0a 20 20   page pLastPg.  
1665e 20 20 20 20 2a 2a 20 69 73 20 73 77 61 70 70 65      ** is swappe
1665f 64 20 77 69 74 68 20 74 68 65 20 66 69 72 73 74  d with the first
16660 20 66 72 65 65 20 70 61 67 65 20 70 75 6c 6c 65   free page pulle
16661 64 20 6f 66 66 20 74 68 65 20 66 72 65 65 20 6c  d off the free l
16662 69 73 74 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20  ist..      **.  
16663 20 20 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f 74      ** On the ot
16664 68 65 72 20 68 61 6e 64 2c 20 69 66 20 6e 46 69  her hand, if nFi
16665 6e 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61  n is greater tha
16666 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6b 65 65  n zero, then kee
16667 70 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 6f 70 69  p.      ** loopi
16668 6e 67 20 75 6e 74 69 6c 20 61 20 66 72 65 65 2d  ng until a free-
16669 70 61 67 65 20 6c 6f 63 61 74 65 64 20 77 69 74  page located wit
1666a 68 69 6e 20 74 68 65 20 66 69 72 73 74 20 6e 46  hin the first nF
1666b 69 6e 20 70 61 67 65 73 0a 20 20 20 20 20 20 2a  in pages.      *
1666c 2a 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 73  * of the file is
1666d 20 66 6f 75 6e 64 2e 0a 20 20 20 20 20 20 2a 2f   found..      */
1666e 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20  .      do {.    
1666f 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 46 72      MemPage *pFr
16670 65 65 50 67 3b 0a 20 20 20 20 20 20 20 20 72 63  eePg;.        rc
16671 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65   = allocateBtree
16672 50 61 67 65 28 70 42 74 2c 20 26 70 46 72 65 65  Page(pBt, &pFree
16673 50 67 2c 20 26 69 46 72 65 65 50 67 2c 20 30 2c  Pg, &iFreePg, 0,
16674 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   0);.        if(
16675 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
16676 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 6c 65  {.          rele
16677 61 73 65 50 61 67 65 28 70 4c 61 73 74 50 67 29  asePage(pLastPg)
16678 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ;.          retu
16679 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d  rn rc;.        }
1667a 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
1667b 50 61 67 65 28 70 46 72 65 65 50 67 29 3b 0a 20  Page(pFreePg);. 
1667c 20 20 20 20 20 7d 77 68 69 6c 65 28 20 6e 46 69       }while( nFi
1667d 6e 21 3d 30 20 26 26 20 69 46 72 65 65 50 67 3e  n!=0 && iFreePg>
1667e 6e 46 69 6e 20 29 3b 0a 20 20 20 20 20 20 61 73  nFin );.      as
1667f 73 65 72 74 28 20 69 46 72 65 65 50 67 3c 69 4c  sert( iFreePg<iL
16680 61 73 74 50 67 20 29 3b 0a 20 20 20 20 20 20 0a  astPg );.      .
16681 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
16682 65 33 50 61 67 65 72 57 72 69 74 65 28 70 4c 61  e3PagerWrite(pLa
16683 73 74 50 67 2d 3e 70 44 62 50 61 67 65 29 3b 0a  stPg->pDbPage);.
16684 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
16685 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
16686 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65     rc = relocate
16687 50 61 67 65 28 70 42 74 2c 20 70 4c 61 73 74 50  Page(pBt, pLastP
16688 67 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 61  g, eType, iPtrPa
16689 67 65 2c 20 69 46 72 65 65 50 67 2c 20 6e 46 69  ge, iFreePg, nFi
1668a 6e 21 3d 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20  n!=0);.      }. 
1668b 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
1668c 28 70 4c 61 73 74 50 67 29 3b 0a 20 20 20 20 20  (pLastPg);.     
1668d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1668e 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  OK ){.        re
1668f 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d  turn rc;.      }
16690 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66  .    }.  }..  if
16691 28 20 6e 46 69 6e 3d 3d 30 20 29 7b 0a 20 20 20  ( nFin==0 ){.   
16692 20 69 4c 61 73 74 50 67 2d 2d 3b 0a 20 20 20 20   iLastPg--;.    
16693 77 68 69 6c 65 28 20 69 4c 61 73 74 50 67 3d 3d  while( iLastPg==
16694 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
16695 45 28 70 42 74 29 7c 7c 50 54 52 4d 41 50 5f 49  E(pBt)||PTRMAP_I
16696 53 50 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74  SPAGE(pBt, iLast
16697 50 67 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28  Pg) ){.      if(
16698 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70   PTRMAP_ISPAGE(p
16699 42 74 2c 20 69 4c 61 73 74 50 67 29 20 29 7b 0a  Bt, iLastPg) ){.
1669a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20          MemPage 
1669b 2a 70 50 67 3b 0a 20 20 20 20 20 20 20 20 69 6e  *pPg;.        in
1669c 74 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50  t rc = btreeGetP
1669d 61 67 65 28 70 42 74 2c 20 69 4c 61 73 74 50 67  age(pBt, iLastPg
1669e 2c 20 26 70 50 67 2c 20 30 29 3b 0a 20 20 20 20  , &pPg, 0);.    
1669f 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
166a0 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
166a1 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
166a2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
166a3 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
166a4 72 57 72 69 74 65 28 70 50 67 2d 3e 70 44 62 50  rWrite(pPg->pDbP
166a5 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65  age);.        re
166a6 6c 65 61 73 65 50 61 67 65 28 70 50 67 29 3b 0a  leasePage(pPg);.
166a7 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
166a8 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
166a9 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
166aa 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
166ab 20 20 7d 0a 20 20 20 20 20 20 69 4c 61 73 74 50    }.      iLastP
166ac 67 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  g--;.    }.    s
166ad 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63  qlite3PagerTrunc
166ae 61 74 65 49 6d 61 67 65 28 70 42 74 2d 3e 70 50  ateImage(pBt->pP
166af 61 67 65 72 2c 20 69 4c 61 73 74 50 67 29 3b 0a  ager, iLastPg);.
166b0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
166b1 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
166b2 20 41 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63   A write-transac
166b3 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 6f 70 65  tion must be ope
166b4 6e 65 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69  ned before calli
166b5 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ng this function
166b6 2e 0a 2a 2a 20 49 74 20 70 65 72 66 6f 72 6d 73  ..** It performs
166b7 20 61 20 73 69 6e 67 6c 65 20 75 6e 69 74 20 6f   a single unit o
166b8 66 20 77 6f 72 6b 20 74 6f 77 61 72 64 73 20 61  f work towards a
166b9 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61  n incremental va
166ba 63 75 75 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  cuum..**.** If t
166bb 68 65 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76  he incremental v
166bc 61 63 75 75 6d 20 69 73 20 66 69 6e 69 73 68 65  acuum is finishe
166bd 64 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e  d after this fun
166be 63 74 69 6f 6e 20 68 61 73 20 72 75 6e 2c 0a 2a  ction has run,.*
166bf 2a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73  * SQLITE_DONE is
166c0 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 69 74   returned. If it
166c1 20 69 73 20 6e 6f 74 20 66 69 6e 69 73 68 65 64   is not finished
166c2 2c 20 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f  , but no error o
166c3 63 63 75 72 72 65 64 2c 0a 2a 2a 20 53 51 4c 49  ccurred,.** SQLI
166c4 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
166c5 64 2e 20 4f 74 68 65 72 77 69 73 65 20 61 6e 20  d. Otherwise an 
166c6 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64  SQLite error cod
166c7 65 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  e. .*/.SQLITE_PR
166c8 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
166c9 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d  3BtreeIncrVacuum
166ca 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e  (Btree *p){.  in
166cb 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64  t rc;.  BtShared
166cc 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
166cd 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
166ce 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72  nter(p);.  asser
166cf 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61  t( pBt->inTransa
166d0 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49  ction==TRANS_WRI
166d1 54 45 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73  TE && p->inTrans
166d2 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b  ==TRANS_WRITE );
166d3 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 61 75 74  .  if( !pBt->aut
166d4 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 72  oVacuum ){.    r
166d5 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  c = SQLITE_DONE;
166d6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
166d7 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66  validateAllOverf
166d8 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a 20  lowCache(pBt);. 
166d9 20 20 20 72 63 20 3d 20 69 6e 63 72 56 61 63 75     rc = incrVacu
166da 75 6d 53 74 65 70 28 70 42 74 2c 20 30 2c 20 70  umStep(pBt, 0, p
166db 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42  agerPagecount(pB
166dc 74 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  t));.  }.  sqlit
166dd 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
166de 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
166df 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
166e0 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 70 72  ine is called pr
166e1 69 6f 72 20 74 6f 20 73 71 6c 69 74 65 33 50 61  ior to sqlite3Pa
166e2 67 65 72 43 6f 6d 6d 69 74 20 77 68 65 6e 20 61  gerCommit when a
166e3 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20   transaction.** 
166e4 69 73 20 63 6f 6d 6d 69 74 65 64 20 66 6f 72 20  is commited for 
166e5 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 64  an auto-vacuum d
166e6 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49  atabase..**.** I
166e7 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  f SQLITE_OK is r
166e8 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 2a 70  eturned, then *p
166e9 6e 54 72 75 6e 63 20 69 73 20 73 65 74 20 74 6f  nTrunc is set to
166ea 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
166eb 61 67 65 73 0a 2a 2a 20 74 68 65 20 64 61 74 61  ages.** the data
166ec 62 61 73 65 20 66 69 6c 65 20 73 68 6f 75 6c 64  base file should
166ed 20 62 65 20 74 72 75 6e 63 61 74 65 64 20 74 6f   be truncated to
166ee 20 64 75 72 69 6e 67 20 74 68 65 20 63 6f 6d 6d   during the comm
166ef 69 74 20 70 72 6f 63 65 73 73 2e 20 0a 2a 2a 20  it process. .** 
166f0 69 2e 65 2e 20 74 68 65 20 64 61 74 61 62 61 73  i.e. the databas
166f1 65 20 68 61 73 20 62 65 65 6e 20 72 65 6f 72 67  e has been reorg
166f2 61 6e 69 7a 65 64 20 73 6f 20 74 68 61 74 20 6f  anized so that o
166f3 6e 6c 79 20 74 68 65 20 66 69 72 73 74 20 2a 70  nly the first *p
166f4 6e 54 72 75 6e 63 0a 2a 2a 20 70 61 67 65 73 20  nTrunc.** pages 
166f5 61 72 65 20 69 6e 20 75 73 65 2e 0a 2a 2f 0a 73  are in use..*/.s
166f6 74 61 74 69 63 20 69 6e 74 20 61 75 74 6f 56 61  tatic int autoVa
166f7 63 75 75 6d 43 6f 6d 6d 69 74 28 42 74 53 68 61  cuumCommit(BtSha
166f8 72 65 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74  red *pBt){.  int
166f9 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
166fa 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
166fb 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a   = pBt->pPager;.
166fc 20 20 56 56 41 5f 4f 4e 4c 59 28 20 69 6e 74 20    VVA_ONLY( int 
166fd 6e 52 65 66 20 3d 20 73 71 6c 69 74 65 33 50 61  nRef = sqlite3Pa
166fe 67 65 72 52 65 66 63 6f 75 6e 74 28 70 50 61 67  gerRefcount(pPag
166ff 65 72 29 20 29 3b 0a 0a 20 20 61 73 73 65 72 74  er) );..  assert
16700 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
16701 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
16702 20 29 3b 0a 20 20 69 6e 76 61 6c 69 64 61 74 65   );.  invalidate
16703 41 6c 6c 4f 76 65 72 66 6c 6f 77 43 61 63 68 65  AllOverflowCache
16704 28 70 42 74 29 3b 0a 20 20 61 73 73 65 72 74 28  (pBt);.  assert(
16705 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29  pBt->autoVacuum)
16706 3b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 69 6e  ;.  if( !pBt->in
16707 63 72 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20  crVacuum ){.    
16708 50 67 6e 6f 20 6e 46 69 6e 3b 20 20 20 20 20 20  Pgno nFin;      
16709 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1670a 70 61 67 65 73 20 69 6e 20 64 61 74 61 62 61 73  pages in databas
1670b 65 20 61 66 74 65 72 20 61 75 74 6f 76 61 63 75  e after autovacu
1670c 75 6d 69 6e 67 20 2a 2f 0a 20 20 20 20 50 67 6e  uming */.    Pgn
1670d 6f 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20  o nFree;        
1670e 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67  /* Number of pag
1670f 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69  es on the freeli
16710 73 74 20 69 6e 69 74 69 61 6c 6c 79 20 2a 2f 0a  st initially */.
16711 20 20 20 20 50 67 6e 6f 20 6e 50 74 72 6d 61 70      Pgno nPtrmap
16712 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  ;      /* Number
16713 20 6f 66 20 50 74 72 4d 61 70 20 70 61 67 65 73   of PtrMap pages
16714 20 74 6f 20 62 65 20 66 72 65 65 64 20 2a 2f 0a   to be freed */.
16715 20 20 20 20 50 67 6e 6f 20 69 46 72 65 65 3b 20      Pgno iFree; 
16716 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65         /* The ne
16717 78 74 20 70 61 67 65 20 74 6f 20 62 65 20 66 72  xt page to be fr
16718 65 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  eed */.    int n
16719 45 6e 74 72 79 3b 20 20 20 20 20 20 20 20 2f 2a  Entry;        /*
1671a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
1671b 65 73 20 6f 6e 20 6f 6e 65 20 70 74 72 6d 61 70  es on one ptrmap
1671c 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 50 67 6e   page */.    Pgn
1671d 6f 20 6e 4f 72 69 67 3b 20 20 20 20 20 20 20 20  o nOrig;        
1671e 2f 2a 20 44 61 74 61 62 61 73 65 20 73 69 7a 65  /* Database size
1671f 20 62 65 66 6f 72 65 20 66 72 65 65 69 6e 67 20   before freeing 
16720 2a 2f 0a 0a 20 20 20 20 6e 4f 72 69 67 20 3d 20  */..    nOrig = 
16721 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  pagerPagecount(p
16722 42 74 29 3b 0a 20 20 20 20 69 66 28 20 50 54 52  Bt);.    if( PTR
16723 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20  MAP_ISPAGE(pBt, 
16724 6e 4f 72 69 67 29 20 7c 7c 20 6e 4f 72 69 67 3d  nOrig) || nOrig=
16725 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  =PENDING_BYTE_PA
16726 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20  GE(pBt) ){.     
16727 20 2f 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f   /* It is not po
16728 73 73 69 62 6c 65 20 74 6f 20 63 72 65 61 74 65  ssible to create
16729 20 61 20 64 61 74 61 62 61 73 65 20 66 6f 72 20   a database for 
1672a 77 68 69 63 68 20 74 68 65 20 66 69 6e 61 6c 20  which the final 
1672b 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 69 73  page.      ** is
1672c 20 65 69 74 68 65 72 20 61 20 70 6f 69 6e 74 65   either a pointe
1672d 72 2d 6d 61 70 20 70 61 67 65 20 6f 72 20 74 68  r-map page or th
1672e 65 20 70 65 6e 64 69 6e 67 2d 62 79 74 65 20 70  e pending-byte p
1672f 61 67 65 2e 20 49 66 20 6f 6e 65 0a 20 20 20 20  age. If one.    
16730 20 20 2a 2a 20 69 73 20 65 6e 63 6f 75 6e 74 65    ** is encounte
16731 72 65 64 2c 20 74 68 69 73 20 69 6e 64 69 63 61  red, this indica
16732 74 65 73 20 63 6f 72 72 75 70 74 69 6f 6e 2e 0a  tes corruption..
16733 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 72        */.      r
16734 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
16735 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d  RUPT_BKPT;.    }
16736 0a 0a 20 20 20 20 6e 46 72 65 65 20 3d 20 67 65  ..    nFree = ge
16737 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61  t4byte(&pBt->pPa
16738 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b  ge1->aData[36]);
16739 0a 20 20 20 20 6e 45 6e 74 72 79 20 3d 20 70 42  .    nEntry = pB
1673a 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 35 3b  t->usableSize/5;
1673b 0a 20 20 20 20 6e 50 74 72 6d 61 70 20 3d 20 28  .    nPtrmap = (
1673c 6e 46 72 65 65 2d 6e 4f 72 69 67 2b 50 54 52 4d  nFree-nOrig+PTRM
1673d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20 6e  AP_PAGENO(pBt, n
1673e 4f 72 69 67 29 2b 6e 45 6e 74 72 79 29 2f 6e 45  Orig)+nEntry)/nE
1673f 6e 74 72 79 3b 0a 20 20 20 20 6e 46 69 6e 20 3d  ntry;.    nFin =
16740 20 6e 4f 72 69 67 20 2d 20 6e 46 72 65 65 20 2d   nOrig - nFree -
16741 20 6e 50 74 72 6d 61 70 3b 0a 20 20 20 20 69 66   nPtrmap;.    if
16742 28 20 6e 4f 72 69 67 3e 50 45 4e 44 49 4e 47 5f  ( nOrig>PENDING_
16743 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 26  BYTE_PAGE(pBt) &
16744 26 20 6e 46 69 6e 3c 50 45 4e 44 49 4e 47 5f 42  & nFin<PENDING_B
16745 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b  YTE_PAGE(pBt) ){
16746 0a 20 20 20 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20  .      nFin--;. 
16747 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20     }.    while( 
16748 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42  PTRMAP_ISPAGE(pB
16749 74 2c 20 6e 46 69 6e 29 20 7c 7c 20 6e 46 69 6e  t, nFin) || nFin
1674a 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  ==PENDING_BYTE_P
1674b 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20  AGE(pBt) ){.    
1674c 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20 7d 0a    nFin--;.    }.
1674d 20 20 20 20 69 66 28 20 6e 46 69 6e 3e 6e 4f 72      if( nFin>nOr
1674e 69 67 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  ig ) return SQLI
1674f 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
16750 0a 0a 20 20 20 20 66 6f 72 28 69 46 72 65 65 3d  ..    for(iFree=
16751 6e 4f 72 69 67 3b 20 69 46 72 65 65 3e 6e 46 69  nOrig; iFree>nFi
16752 6e 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f  n && rc==SQLITE_
16753 4f 4b 3b 20 69 46 72 65 65 2d 2d 29 7b 0a 20 20  OK; iFree--){.  
16754 20 20 20 20 72 63 20 3d 20 69 6e 63 72 56 61 63      rc = incrVac
16755 75 75 6d 53 74 65 70 28 70 42 74 2c 20 6e 46 69  uumStep(pBt, nFi
16756 6e 2c 20 69 46 72 65 65 29 3b 0a 20 20 20 20 7d  n, iFree);.    }
16757 0a 20 20 20 20 69 66 28 20 28 72 63 3d 3d 53 51  .    if( (rc==SQ
16758 4c 49 54 45 5f 44 4f 4e 45 20 7c 7c 20 72 63 3d  LITE_DONE || rc=
16759 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 26 26 20 6e  =SQLITE_OK) && n
1675a 46 72 65 65 3e 30 20 29 7b 0a 20 20 20 20 20 20  Free>0 ){.      
1675b 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1675c 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1675d 65 33 50 61 67 65 72 57 72 69 74 65 28 70 42 74  e3PagerWrite(pBt
1675e 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67  ->pPage1->pDbPag
1675f 65 29 3b 0a 20 20 20 20 20 20 70 75 74 34 62 79  e);.      put4by
16760 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d  te(&pBt->pPage1-
16761 3e 61 44 61 74 61 5b 33 32 5d 2c 20 30 29 3b 0a  >aData[32], 0);.
16762 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26        put4byte(&
16763 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61  pBt->pPage1->aDa
16764 74 61 5b 33 36 5d 2c 20 30 29 3b 0a 20 20 20 20  ta[36], 0);.    
16765 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72    sqlite3PagerTr
16766 75 6e 63 61 74 65 49 6d 61 67 65 28 70 42 74 2d  uncateImage(pBt-
16767 3e 70 50 61 67 65 72 2c 20 6e 46 69 6e 29 3b 0a  >pPager, nFin);.
16768 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
16769 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1676a 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
1676b 72 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72  rRollback(pPager
1676c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
1676d 61 73 73 65 72 74 28 20 6e 52 65 66 3d 3d 73 71  assert( nRef==sq
1676e 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75  lite3PagerRefcou
1676f 6e 74 28 70 50 61 67 65 72 29 20 29 3b 0a 20 20  nt(pPager) );.  
16770 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65  return rc;.}..#e
16771 6c 73 65 20 2f 2a 20 69 66 6e 64 65 66 20 53 51  lse /* ifndef SQ
16772 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
16773 43 55 55 4d 20 2a 2f 0a 23 20 64 65 66 69 6e 65  CUUM */.# define
16774 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73   setChildPtrmaps
16775 28 78 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65  (x) SQLITE_OK.#e
16776 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ndif../*.** This
16777 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68   routine does th
16778 65 20 66 69 72 73 74 20 70 68 61 73 65 20 6f 66  e first phase of
16779 20 61 20 74 77 6f 2d 70 68 61 73 65 20 63 6f 6d   a two-phase com
1677a 6d 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69  mit.  This routi
1677b 6e 65 0a 2a 2a 20 63 61 75 73 65 73 20 61 20 72  ne.** causes a r
1677c 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
1677d 74 6f 20 62 65 20 63 72 65 61 74 65 64 20 28 69  to be created (i
1677e 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c  f it does not al
1677f 72 65 61 64 79 20 65 78 69 73 74 29 0a 2a 2a 20  ready exist).** 
16780 61 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 77 69  and populated wi
16781 74 68 20 65 6e 6f 75 67 68 20 69 6e 66 6f 72 6d  th enough inform
16782 61 74 69 6f 6e 20 73 6f 20 74 68 61 74 20 69 66  ation so that if
16783 20 61 20 70 6f 77 65 72 20 6c 6f 73 73 20 6f 63   a power loss oc
16784 63 75 72 73 0a 2a 2a 20 74 68 65 20 64 61 74 61  curs.** the data
16785 62 61 73 65 20 63 61 6e 20 62 65 20 72 65 73 74  base can be rest
16786 6f 72 65 64 20 74 6f 20 69 74 73 20 6f 72 69 67  ored to its orig
16787 69 6e 61 6c 20 73 74 61 74 65 20 62 79 20 70 6c  inal state by pl
16788 61 79 69 6e 67 20 62 61 63 6b 0a 2a 2a 20 74 68  aying back.** th
16789 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 6e  e journal.  Then
1678a 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
1678b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61 72 65   the journal are
1678c 20 66 6c 75 73 68 65 64 20 6f 75 74 20 74 6f 0a   flushed out to.
1678d 2a 2a 20 74 68 65 20 64 69 73 6b 2e 20 20 41 66  ** the disk.  Af
1678e 74 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ter the journal 
1678f 69 73 20 73 61 66 65 6c 79 20 6f 6e 20 6f 78 69  is safely on oxi
16790 64 65 2c 20 74 68 65 20 63 68 61 6e 67 65 73 20  de, the changes 
16791 74 6f 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  to the.** databa
16792 73 65 20 61 72 65 20 77 72 69 74 74 65 6e 20 69  se are written i
16793 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65  nto the database
16794 20 66 69 6c 65 20 61 6e 64 20 66 6c 75 73 68 65   file and flushe
16795 64 20 74 6f 20 6f 78 69 64 65 2e 0a 2a 2a 20 41  d to oxide..** A
16796 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 69  t the end of thi
16797 73 20 63 61 6c 6c 2c 20 74 68 65 20 72 6f 6c 6c  s call, the roll
16798 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 73 74 69  back journal sti
16799 6c 6c 20 65 78 69 73 74 73 20 6f 6e 20 74 68 65  ll exists on the
1679a 0a 2a 2a 20 64 69 73 6b 20 61 6e 64 20 77 65 20  .** disk and we 
1679b 61 72 65 20 73 74 69 6c 6c 20 68 6f 6c 64 69 6e  are still holdin
1679c 67 20 61 6c 6c 20 6c 6f 63 6b 73 2c 20 73 6f 20  g all locks, so 
1679d 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
1679e 68 61 73 20 6e 6f 74 0a 2a 2a 20 63 6f 6d 6d 69  has not.** commi
1679f 74 74 65 64 2e 20 20 53 65 65 20 73 71 6c 69 74  tted.  See sqlit
167a0 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
167a1 73 65 54 77 6f 28 29 20 66 6f 72 20 74 68 65 20  seTwo() for the 
167a2 73 65 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20  second phase of 
167a3 74 68 65 0a 2a 2a 20 63 6f 6d 6d 69 74 20 70 72  the.** commit pr
167a4 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ocess..**.** Thi
167a5 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f  s call is a no-o
167a6 70 20 69 66 20 6e 6f 20 77 72 69 74 65 2d 74 72  p if no write-tr
167a7 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 75 72  ansaction is cur
167a8 72 65 6e 74 6c 79 20 61 63 74 69 76 65 20 6f 6e  rently active on
167a9 20 70 42 74 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65   pBt..**.** Othe
167aa 72 77 69 73 65 2c 20 73 79 6e 63 20 74 68 65 20  rwise, sync the 
167ab 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f  database file fo
167ac 72 20 74 68 65 20 62 74 72 65 65 20 70 42 74 2e  r the btree pBt.
167ad 20 7a 4d 61 73 74 65 72 20 70 6f 69 6e 74 73 20   zMaster points 
167ae 74 6f 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 6f  to.** the name o
167af 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  f a master journ
167b0 61 6c 20 66 69 6c 65 20 74 68 61 74 20 73 68 6f  al file that sho
167b1 75 6c 64 20 62 65 20 77 72 69 74 74 65 6e 20 69  uld be written i
167b2 6e 74 6f 20 74 68 65 0a 2a 2a 20 69 6e 64 69 76  nto the.** indiv
167b3 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 69  idual journal fi
167b4 6c 65 2c 20 6f 72 20 69 73 20 4e 55 4c 4c 2c 20  le, or is NULL, 
167b5 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20 6d 61  indicating no ma
167b6 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
167b7 65 20 0a 2a 2a 20 28 73 69 6e 67 6c 65 20 64 61  e .** (single da
167b8 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 69  tabase transacti
167b9 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  on)..**.** When 
167ba 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 20  this is called, 
167bb 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
167bc 61 6c 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64  al should alread
167bd 79 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a 20 63  y have been.** c
167be 72 65 61 74 65 64 2c 20 70 6f 70 75 6c 61 74 65  reated, populate
167bf 64 20 77 69 74 68 20 74 68 69 73 20 6a 6f 75 72  d with this jour
167c0 6e 61 6c 20 70 6f 69 6e 74 65 72 20 61 6e 64 20  nal pointer and 
167c1 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 0a  synced to disk..
167c2 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 69 73 20  **.** Once this 
167c3 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 72  is routine has r
167c4 65 74 75 72 6e 65 64 2c 20 74 68 65 20 6f 6e 6c  eturned, the onl
167c5 79 20 74 68 69 6e 67 20 72 65 71 75 69 72 65 64  y thing required
167c6 20 74 6f 20 63 6f 6d 6d 69 74 0a 2a 2a 20 74 68   to commit.** th
167c7 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74  e write-transact
167c8 69 6f 6e 20 66 6f 72 20 74 68 69 73 20 64 61 74  ion for this dat
167c9 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 74 6f  abase file is to
167ca 20 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72   delete the jour
167cb 6e 61 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nal..*/.SQLITE_P
167cc 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
167cd 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
167ce 73 65 4f 6e 65 28 42 74 72 65 65 20 2a 70 2c 20  seOne(Btree *p, 
167cf 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73  const char *zMas
167d0 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ter){.  int rc =
167d1 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
167d2 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  ( p->inTrans==TR
167d3 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20  ANS_WRITE ){.   
167d4 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
167d5 20 70 2d 3e 70 42 74 3b 0a 20 20 20 20 73 71 6c   p->pBt;.    sql
167d6 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
167d7 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
167d8 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
167d9 4d 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61  M.    if( pBt->a
167da 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20  utoVacuum ){.   
167db 20 20 20 72 63 20 3d 20 61 75 74 6f 56 61 63 75     rc = autoVacu
167dc 75 6d 43 6f 6d 6d 69 74 28 70 42 74 29 3b 0a 20  umCommit(pBt);. 
167dd 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
167de 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
167df 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
167e0 61 76 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20  ave(p);.        
167e1 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
167e2 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a   }.    }.#endif.
167e3 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
167e4 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65  PagerCommitPhase
167e5 4f 6e 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  One(pBt->pPager,
167e6 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20   zMaster, 0);.  
167e7 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
167e8 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65  ave(p);.  }.  re
167e9 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
167ea 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
167eb 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 62  is called from b
167ec 6f 74 68 20 42 74 72 65 65 43 6f 6d 6d 69 74 50  oth BtreeCommitP
167ed 68 61 73 65 54 77 6f 28 29 20 61 6e 64 20 42 74  haseTwo() and Bt
167ee 72 65 65 52 6f 6c 6c 62 61 63 6b 28 29 0a 2a 2a  reeRollback().**
167ef 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69   at the conclusi
167f0 6f 6e 20 6f 66 20 61 20 74 72 61 6e 73 61 63 74  on of a transact
167f1 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
167f2 6f 69 64 20 62 74 72 65 65 45 6e 64 54 72 61 6e  oid btreeEndTran
167f3 73 61 63 74 69 6f 6e 28 42 74 72 65 65 20 2a 70  saction(Btree *p
167f4 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  ){.  BtShared *p
167f5 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 61  Bt = p->pBt;.  a
167f6 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
167f7 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29  reeHoldsMutex(p)
167f8 20 29 3b 0a 0a 20 20 62 74 72 65 65 43 6c 65 61   );..  btreeClea
167f9 72 48 61 73 43 6f 6e 74 65 6e 74 28 70 42 74 29  rHasContent(pBt)
167fa 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61  ;.  if( p->inTra
167fb 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26  ns>TRANS_NONE &&
167fc 20 70 2d 3e 64 62 2d 3e 61 63 74 69 76 65 56 64   p->db->activeVd
167fd 62 65 43 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f  beCnt>1 ){.    /
167fe 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 6f  * If there are o
167ff 74 68 65 72 20 61 63 74 69 76 65 20 73 74 61 74  ther active stat
16800 65 6d 65 6e 74 73 20 74 68 61 74 20 62 65 6c 6f  ements that belo
16801 6e 67 20 74 6f 20 74 68 69 73 20 64 61 74 61 62  ng to this datab
16802 61 73 65 0a 20 20 20 20 2a 2a 20 68 61 6e 64 6c  ase.    ** handl
16803 65 2c 20 64 6f 77 6e 67 72 61 64 65 20 74 6f 20  e, downgrade to 
16804 61 20 72 65 61 64 2d 6f 6e 6c 79 20 74 72 61 6e  a read-only tran
16805 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 6f 74 68  saction. The oth
16806 65 72 20 73 74 61 74 65 6d 65 6e 74 73 0a 20 20  er statements.  
16807 20 20 2a 2a 20 6d 61 79 20 73 74 69 6c 6c 20 62    ** may still b
16808 65 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20 74  e reading from t
16809 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 2a 2f  he database.  */
1680a 0a 20 20 20 20 64 6f 77 6e 67 72 61 64 65 41 6c  .    downgradeAl
1680b 6c 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c  lSharedCacheTabl
1680c 65 4c 6f 63 6b 73 28 70 29 3b 0a 20 20 20 20 70  eLocks(p);.    p
1680d 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e  ->inTrans = TRAN
1680e 53 5f 52 45 41 44 3b 0a 20 20 7d 65 6c 73 65 7b  S_READ;.  }else{
1680f 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 68  .    /* If the h
16810 61 6e 64 6c 65 20 68 61 64 20 61 6e 79 20 6b 69  andle had any ki
16811 6e 64 20 6f 66 20 74 72 61 6e 73 61 63 74 69 6f  nd of transactio
16812 6e 20 6f 70 65 6e 2c 20 64 65 63 72 65 6d 65 6e  n open, decremen
16813 74 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 74 72  t the .    ** tr
16814 61 6e 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 20  ansaction count 
16815 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 62 74  of the shared bt
16816 72 65 65 2e 20 49 66 20 74 68 65 20 74 72 61 6e  ree. If the tran
16817 73 61 63 74 69 6f 6e 20 63 6f 75 6e 74 20 0a 20  saction count . 
16818 20 20 20 2a 2a 20 72 65 61 63 68 65 73 20 30 2c     ** reaches 0,
16819 20 73 65 74 20 74 68 65 20 73 68 61 72 65 64 20   set the shared 
1681a 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 4e  state to TRANS_N
1681b 4f 4e 45 2e 20 54 68 65 20 75 6e 6c 6f 63 6b 42  ONE. The unlockB
1681c 74 72 65 65 49 66 55 6e 75 73 65 64 28 29 0a 20  treeIfUnused(). 
1681d 20 20 20 2a 2a 20 63 61 6c 6c 20 62 65 6c 6f 77     ** call below
1681e 20 77 69 6c 6c 20 75 6e 6c 6f 63 6b 20 74 68 65   will unlock the
1681f 20 70 61 67 65 72 2e 20 20 2a 2f 0a 20 20 20 20   pager.  */.    
16820 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d  if( p->inTrans!=
16821 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a 20 20  TRANS_NONE ){.  
16822 20 20 20 20 63 6c 65 61 72 41 6c 6c 53 68 61 72      clearAllShar
16823 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b  edCacheTableLock
16824 73 28 70 29 3b 0a 20 20 20 20 20 20 70 42 74 2d  s(p);.      pBt-
16825 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b  >nTransaction--;
16826 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 70 42  .      if( 0==pB
16827 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20  t->nTransaction 
16828 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e  ){.        pBt->
16829 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20  inTransaction = 
1682a 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20  TRANS_NONE;.    
1682b 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
1682c 2a 20 53 65 74 20 74 68 65 20 63 75 72 72 65 6e  * Set the curren
1682d 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 74  t transaction st
1682e 61 74 65 20 74 6f 20 54 52 41 4e 53 5f 4e 4f 4e  ate to TRANS_NON
1682f 45 20 61 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65  E and unlock the
16830 20 0a 20 20 20 20 2a 2a 20 70 61 67 65 72 20 69   .    ** pager i
16831 66 20 74 68 69 73 20 63 61 6c 6c 20 63 6c 6f 73  f this call clos
16832 65 64 20 74 68 65 20 6f 6e 6c 79 20 72 65 61 64  ed the only read
16833 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73 61   or write transa
16834 63 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20 20 70  ction.  */.    p
16835 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52 41 4e  ->inTrans = TRAN
16836 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 75 6e 6c 6f  S_NONE;.    unlo
16837 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28  ckBtreeIfUnused(
16838 70 42 74 29 3b 0a 20 20 7d 0a 0a 20 20 62 74 72  pBt);.  }..  btr
16839 65 65 49 6e 74 65 67 72 69 74 79 28 70 29 3b 0a  eeIntegrity(p);.
1683a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20  }../*.** Commit 
1683b 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
1683c 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 70 72 6f  currently in pro
1683d 67 72 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  gress..**.** Thi
1683e 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d  s routine implem
1683f 65 6e 74 73 20 74 68 65 20 73 65 63 6f 6e 64 20  ents the second 
16840 70 68 61 73 65 20 6f 66 20 61 20 32 2d 70 68 61  phase of a 2-pha
16841 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68 65 0a  se commit.  The.
16842 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  ** sqlite3BtreeC
16843 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20  ommitPhaseOne() 
16844 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68 65  routine does the
16845 20 66 69 72 73 74 20 70 68 61 73 65 20 61 6e 64   first phase and
16846 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 69 6e   should.** be in
16847 76 6f 6b 65 64 20 70 72 69 6f 72 20 74 6f 20 63  voked prior to c
16848 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75 74  alling this rout
16849 69 6e 65 2e 20 20 54 68 65 20 73 71 6c 69 74 65  ine.  The sqlite
1684a 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
1684b 65 4f 6e 65 28 29 0a 2a 2a 20 72 6f 75 74 69 6e  eOne().** routin
1684c 65 20 64 69 64 20 61 6c 6c 20 74 68 65 20 77 6f  e did all the wo
1684d 72 6b 20 6f 66 20 77 72 69 74 69 6e 67 20 69 6e  rk of writing in
1684e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 74 6f  formation out to
1684f 20 64 69 73 6b 20 61 6e 64 20 66 6c 75 73 68 69   disk and flushi
16850 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e  ng the.** conten
16851 74 73 20 73 6f 20 74 68 61 74 20 74 68 65 79 20  ts so that they 
16852 61 72 65 20 77 72 69 74 74 65 6e 20 6f 6e 74 6f  are written onto
16853 20 74 68 65 20 64 69 73 6b 20 70 6c 61 74 74 65   the disk platte
16854 72 2e 20 20 41 6c 6c 20 74 68 69 73 0a 2a 2a 20  r.  All this.** 
16855 72 6f 75 74 69 6e 65 20 68 61 73 20 74 6f 20 64  routine has to d
16856 6f 20 69 73 20 64 65 6c 65 74 65 20 6f 72 20 74  o is delete or t
16857 72 75 6e 63 61 74 65 20 6f 72 20 7a 65 72 6f 20  runcate or zero 
16858 74 68 65 20 68 65 61 64 65 72 20 69 6e 20 74 68  the header in th
16859 65 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63  e.** the rollbac
1685a 6b 20 6a 6f 75 72 6e 61 6c 20 28 77 68 69 63 68  k journal (which
1685b 20 63 61 75 73 65 73 20 74 68 65 20 74 72 61 6e   causes the tran
1685c 73 61 63 74 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69  saction to commi
1685d 74 29 20 61 6e 64 0a 2a 2a 20 64 72 6f 70 20 6c  t) and.** drop l
1685e 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ocks..**.** This
1685f 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 20 74 68   will release th
16860 65 20 77 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20  e write lock on 
16861 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
16862 65 2e 20 20 49 66 20 74 68 65 72 65 0a 2a 2a 20  e.  If there.** 
16863 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 63 75  are no active cu
16864 72 73 6f 72 73 2c 20 69 74 20 61 6c 73 6f 20 72  rsors, it also r
16865 65 6c 65 61 73 65 73 20 74 68 65 20 72 65 61 64  eleases the read
16866 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45   lock..*/.SQLITE
16867 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
16868 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50  ite3BtreeCommitP
16869 68 61 73 65 54 77 6f 28 42 74 72 65 65 20 2a 70  haseTwo(Btree *p
1686a 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  ){.  BtShared *p
1686b 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20  Bt = p->pBt;..  
1686c 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
1686d 72 28 70 29 3b 0a 20 20 62 74 72 65 65 49 6e 74  r(p);.  btreeInt
1686e 65 67 72 69 74 79 28 70 29 3b 0a 0a 20 20 2f 2a  egrity(p);..  /*
1686f 20 49 66 20 74 68 65 20 68 61 6e 64 6c 65 20 68   If the handle h
16870 61 73 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73  as a write-trans
16871 61 63 74 69 6f 6e 20 6f 70 65 6e 2c 20 63 6f 6d  action open, com
16872 6d 69 74 20 74 68 65 20 73 68 61 72 65 64 2d 62  mit the shared-b
16873 74 72 65 65 73 20 0a 20 20 2a 2a 20 74 72 61 6e  trees .  ** tran
16874 73 61 63 74 69 6f 6e 20 61 6e 64 20 73 65 74 20  saction and set 
16875 74 68 65 20 73 68 61 72 65 64 20 73 74 61 74 65  the shared state
16876 20 74 6f 20 54 52 41 4e 53 5f 52 45 41 44 2e 0a   to TRANS_READ..
16877 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 6e    */.  if( p->in
16878 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49  Trans==TRANS_WRI
16879 54 45 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63  TE ){.    int rc
1687a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42  ;.    assert( pB
1687b 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
1687c 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b  ==TRANS_WRITE );
1687d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74  .    assert( pBt
1687e 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3e 30  ->nTransaction>0
1687f 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   );.    rc = sql
16880 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50  ite3PagerCommitP
16881 68 61 73 65 54 77 6f 28 70 42 74 2d 3e 70 50 61  haseTwo(pBt->pPa
16882 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ger);.    if( rc
16883 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
16884 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
16885 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20 20  eLeave(p);.     
16886 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
16887 7d 0a 20 20 20 20 70 42 74 2d 3e 69 6e 54 72 61  }.    pBt->inTra
16888 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e 53  nsaction = TRANS
16889 5f 52 45 41 44 3b 0a 20 20 7d 0a 0a 20 20 62 74  _READ;.  }..  bt
1688a 72 65 65 45 6e 64 54 72 61 6e 73 61 63 74 69 6f  reeEndTransactio
1688b 6e 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  n(p);.  sqlite3B
1688c 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20  treeLeave(p);.  
1688d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1688e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 62 6f  ;.}../*.** Do bo
1688f 74 68 20 70 68 61 73 65 73 20 6f 66 20 61 20 63  th phases of a c
16890 6f 6d 6d 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ommit..*/.SQLITE
16891 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
16892 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28  ite3BtreeCommit(
16893 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74  Btree *p){.  int
16894 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74   rc;.  sqlite3Bt
16895 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72  reeEnter(p);.  r
16896 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
16897 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70  CommitPhaseOne(p
16898 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  , 0);.  if( rc==
16899 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1689a 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1689b 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  eeCommitPhaseTwo
1689c 28 70 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  (p);.  }.  sqlit
1689d 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
1689e 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1689f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
168a0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
168a1 20 6e 75 6d 62 65 72 20 6f 66 20 77 72 69 74 65   number of write
168a2 2d 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e  -cursors open on
168a3 20 74 68 69 73 20 68 61 6e 64 6c 65 2e 20 54 68   this handle. Th
168a4 69 73 20 69 73 20 66 6f 72 20 75 73 65 0a 2a 2a  is is for use.**
168a5 20 69 6e 20 61 73 73 65 72 74 28 29 20 65 78 70   in assert() exp
168a6 72 65 73 73 69 6f 6e 73 2c 20 73 6f 20 69 74 20  ressions, so it 
168a7 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64  is only compiled
168a8 20 69 66 20 4e 44 45 42 55 47 20 69 73 20 6e 6f   if NDEBUG is no
168a9 74 0a 2a 2a 20 64 65 66 69 6e 65 64 2e 0a 2a 2a  t.** defined..**
168aa 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72 70  .** For the purp
168ab 6f 73 65 73 20 6f 66 20 74 68 69 73 20 72 6f 75  oses of this rou
168ac 74 69 6e 65 2c 20 61 20 77 72 69 74 65 2d 63 75  tine, a write-cu
168ad 72 73 6f 72 20 69 73 20 61 6e 79 20 63 75 72 73  rsor is any curs
168ae 6f 72 20 74 68 61 74 0a 2a 2a 20 69 73 20 63 61  or that.** is ca
168af 70 61 62 6c 65 20 6f 66 20 77 72 69 74 69 6e 67  pable of writing
168b0 20 74 6f 20 74 68 65 20 64 61 74 61 62 73 65 2e   to the databse.
168b1 20 20 54 68 61 74 20 6d 65 61 6e 73 20 74 68 65    That means the
168b2 20 63 75 72 73 6f 72 20 77 61 73 0a 2a 2a 20 6f   cursor was.** o
168b3 72 69 67 69 6e 61 6c 6c 79 20 6f 70 65 6e 65 64  riginally opened
168b4 20 66 6f 72 20 77 72 69 74 69 6e 67 20 61 6e 64   for writing and
168b5 20 74 68 65 20 63 75 72 73 6f 72 20 68 61 73 20   the cursor has 
168b6 6e 6f 74 20 62 65 20 64 69 73 61 62 6c 65 64 0a  not be disabled.
168b7 2a 2a 20 62 79 20 68 61 76 69 6e 67 20 69 74 73  ** by having its
168b8 20 73 74 61 74 65 20 63 68 61 6e 67 65 64 20 74   state changed t
168b9 6f 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 2e 0a  o CURSOR_FAULT..
168ba 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  */.static int co
168bb 75 6e 74 57 72 69 74 65 43 75 72 73 6f 72 73 28  untWriteCursors(
168bc 42 74 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a  BtShared *pBt){.
168bd 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72    BtCursor *pCur
168be 3b 0a 20 20 69 6e 74 20 72 20 3d 20 30 3b 0a 20  ;.  int r = 0;. 
168bf 20 66 6f 72 28 70 43 75 72 3d 70 42 74 2d 3e 70   for(pCur=pBt->p
168c0 43 75 72 73 6f 72 3b 20 70 43 75 72 3b 20 70 43  Cursor; pCur; pC
168c1 75 72 3d 70 43 75 72 2d 3e 70 4e 65 78 74 29 7b  ur=pCur->pNext){
168c2 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 77  .    if( pCur->w
168c3 72 46 6c 61 67 20 26 26 20 70 43 75 72 2d 3e 65  rFlag && pCur->e
168c4 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 46 41  State!=CURSOR_FA
168c5 55 4c 54 20 29 20 72 2b 2b 3b 20 0a 20 20 7d 0a  ULT ) r++; .  }.
168c6 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a 23 65    return r;.}.#e
168c7 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ndif../*.** This
168c8 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68   routine sets th
168c9 65 20 73 74 61 74 65 20 74 6f 20 43 55 52 53 4f  e state to CURSO
168ca 52 5f 46 41 55 4c 54 20 61 6e 64 20 74 68 65 20  R_FAULT and the 
168cb 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 74 6f  error.** code to
168cc 20 65 72 72 43 6f 64 65 20 66 6f 72 20 65 76 65   errCode for eve
168cd 72 79 20 63 75 72 73 6f 72 20 6f 6e 20 42 74 53  ry cursor on BtS
168ce 68 61 72 65 64 20 74 68 61 74 20 70 42 74 72 65  hared that pBtre
168cf 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 73 2e  e.** references.
168d0 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20 63 75 72  .**.** Every cur
168d1 73 6f 72 20 69 73 20 74 72 69 70 70 65 64 2c 20  sor is tripped, 
168d2 69 6e 63 6c 75 64 69 6e 67 20 63 75 72 73 6f 72  including cursor
168d3 73 20 74 68 61 74 20 62 65 6c 6f 6e 67 0a 2a 2a  s that belong.**
168d4 20 74 6f 20 6f 74 68 65 72 20 64 61 74 61 62 61   to other databa
168d5 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74  se connections t
168d6 68 61 74 20 68 61 70 70 65 6e 20 74 6f 20 62 65  hat happen to be
168d7 20 73 68 61 72 69 6e 67 0a 2a 2a 20 74 68 65 20   sharing.** the 
168d8 63 61 63 68 65 20 77 69 74 68 20 70 42 74 72 65  cache with pBtre
168d9 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  e..**.** This ro
168da 75 74 69 6e 65 20 67 65 74 73 20 63 61 6c 6c 65  utine gets calle
168db 64 20 77 68 65 6e 20 61 20 72 6f 6c 6c 62 61 63  d when a rollbac
168dc 6b 20 6f 63 63 75 72 73 2e 0a 2a 2a 20 41 6c 6c  k occurs..** All
168dd 20 63 75 72 73 6f 72 73 20 75 73 69 6e 67 20 74   cursors using t
168de 68 65 20 73 61 6d 65 20 63 61 63 68 65 20 6d 75  he same cache mu
168df 73 74 20 62 65 20 74 72 69 70 70 65 64 0a 2a 2a  st be tripped.**
168e0 20 74 6f 20 70 72 65 76 65 6e 74 20 74 68 65 6d   to prevent them
168e1 20 66 72 6f 6d 20 74 72 79 69 6e 67 20 74 6f 20   from trying to 
168e2 75 73 65 20 74 68 65 20 62 74 72 65 65 20 61 66  use the btree af
168e3 74 65 72 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62  ter.** the rollb
168e4 61 63 6b 2e 20 20 54 68 65 20 72 6f 6c 6c 62 61  ack.  The rollba
168e5 63 6b 20 6d 61 79 20 68 61 76 65 20 64 65 6c 65  ck may have dele
168e6 74 65 64 20 74 61 62 6c 65 73 0a 2a 2a 20 6f 72  ted tables.** or
168e7 20 6d 6f 76 65 64 20 72 6f 6f 74 20 70 61 67 65   moved root page
168e8 73 2c 20 73 6f 20 69 74 20 69 73 20 6e 6f 74 20  s, so it is not 
168e9 73 75 66 66 69 63 69 65 6e 74 20 74 6f 0a 2a 2a  sufficient to.**
168ea 20 73 61 76 65 20 74 68 65 20 73 74 61 74 65 20   save the state 
168eb 6f 66 20 74 68 65 20 63 75 72 73 6f 72 2e 20 20  of the cursor.  
168ec 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73 74 20  The cursor must 
168ed 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 61 74 65  be.** invalidate
168ee 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
168ef 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
168f0 33 42 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72  3BtreeTripAllCur
168f1 73 6f 72 73 28 42 74 72 65 65 20 2a 70 42 74 72  sors(Btree *pBtr
168f2 65 65 2c 20 69 6e 74 20 65 72 72 43 6f 64 65 29  ee, int errCode)
168f3 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 3b  {.  BtCursor *p;
168f4 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
168f5 6e 74 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20  nter(pBtree);.  
168f6 66 6f 72 28 70 3d 70 42 74 72 65 65 2d 3e 70 42  for(p=pBtree->pB
168f7 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 3b 20 70  t->pCursor; p; p
168f8 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  =p->pNext){.    
168f9 69 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74  int i;.    sqlit
168fa 65 33 42 74 72 65 65 43 6c 65 61 72 43 75 72 73  e3BtreeClearCurs
168fb 6f 72 28 70 29 3b 0a 20 20 20 20 70 2d 3e 65 53  or(p);.    p->eS
168fc 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 46 41  tate = CURSOR_FA
168fd 55 4c 54 3b 0a 20 20 20 20 70 2d 3e 73 6b 69 70  ULT;.    p->skip
168fe 4e 65 78 74 20 3d 20 65 72 72 43 6f 64 65 3b 0a  Next = errCode;.
168ff 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d      for(i=0; i<=
16900 70 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a  p->iPage; i++){.
16901 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
16902 65 28 70 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b  e(p->apPage[i]);
16903 0a 20 20 20 20 20 20 70 2d 3e 61 70 50 61 67 65  .      p->apPage
16904 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  [i] = 0;.    }. 
16905 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65   }.  sqlite3Btre
16906 65 4c 65 61 76 65 28 70 42 74 72 65 65 29 3b 0a  eLeave(pBtree);.
16907 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63  }../*.** Rollbac
16908 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  k the transactio
16909 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 2e 20 20  n in progress.  
1690a 41 6c 6c 20 63 75 72 73 6f 72 73 20 77 69 6c 6c  All cursors will
1690b 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64 65 64   be.** invalided
1690c 20 62 79 20 74 68 69 73 20 6f 70 65 72 61 74 69   by this operati
1690d 6f 6e 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74  on.  Any attempt
1690e 20 74 6f 20 75 73 65 20 61 20 63 75 72 73 6f 72   to use a cursor
1690f 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 6f 70 65  .** that was ope
16910 6e 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69  n at the beginni
16911 6e 67 20 6f 66 20 74 68 69 73 20 6f 70 65 72 61  ng of this opera
16912 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73 75 6c 74  tion will result
16913 0a 2a 2a 20 69 6e 20 61 6e 20 65 72 72 6f 72 2e  .** in an error.
16914 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 69 6c 6c  .**.** This will
16915 20 72 65 6c 65 61 73 65 20 74 68 65 20 77 72 69   release the wri
16916 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  te lock on the d
16917 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 49  atabase file.  I
16918 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e  f there.** are n
16919 6f 20 61 63 74 69 76 65 20 63 75 72 73 6f 72 73  o active cursors
1691a 2c 20 69 74 20 61 6c 73 6f 20 72 65 6c 65 61 73  , it also releas
1691b 65 73 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b  es the read lock
1691c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1691d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
1691e 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 42 74 72  treeRollback(Btr
1691f 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63  ee *p){.  int rc
16920 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
16921 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 4d 65  t = p->pBt;.  Me
16922 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b 0a 0a  mPage *pPage1;..
16923 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
16924 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 73  ter(p);.  rc = s
16925 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42  aveAllCursors(pB
16926 74 2c 20 30 2c 20 30 29 3b 0a 23 69 66 6e 64 65  t, 0, 0);.#ifnde
16927 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
16928 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69 66 28  ARED_CACHE.  if(
16929 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1692a 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73  {.    /* This is
1692b 20 61 20 68 6f 72 72 69 62 6c 65 20 73 69 74 75   a horrible situ
1692c 61 74 69 6f 6e 2e 20 41 6e 20 49 4f 20 6f 72 20  ation. An IO or 
1692d 6d 61 6c 6c 6f 63 28 29 20 65 72 72 6f 72 20 6f  malloc() error o
1692e 63 63 75 72 72 65 64 20 77 68 69 6c 73 74 0a 20  ccurred whilst. 
1692f 20 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f 20     ** trying to 
16930 73 61 76 65 20 63 75 72 73 6f 72 20 70 6f 73 69  save cursor posi
16931 74 69 6f 6e 73 2e 20 49 66 20 74 68 69 73 20 69  tions. If this i
16932 73 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72  s an automatic r
16933 6f 6c 6c 62 61 63 6b 20 28 61 73 0a 20 20 20 20  ollback (as.    
16934 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  ** the result of
16935 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 6d   a constraint, m
16936 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 20  alloc() failure 
16937 6f 72 20 49 4f 20 65 72 72 6f 72 29 20 74 68 65  or IO error) the
16938 6e 20 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 61  n .    ** the ca
16939 63 68 65 20 6d 61 79 20 62 65 20 69 6e 74 65 72  che may be inter
1693a 6e 61 6c 6c 79 20 69 6e 63 6f 6e 73 69 73 74 65  nally inconsiste
1693b 6e 74 20 28 6e 6f 74 20 63 6f 6e 74 61 69 6e 20  nt (not contain 
1693c 76 61 6c 69 64 20 74 72 65 65 73 29 20 73 6f 0a  valid trees) so.
1693d 20 20 20 20 2a 2a 20 77 65 20 63 61 6e 6e 6f 74      ** we cannot
1693e 20 73 69 6d 70 6c 79 20 72 65 74 75 72 6e 20 74   simply return t
1693f 68 65 20 65 72 72 6f 72 20 74 6f 20 74 68 65 20  he error to the 
16940 63 61 6c 6c 65 72 2e 20 49 6e 73 74 65 61 64 2c  caller. Instead,
16941 20 61 62 6f 72 74 20 0a 20 20 20 20 2a 2a 20 61   abort .    ** a
16942 6c 6c 20 71 75 65 72 69 65 73 20 74 68 61 74 20  ll queries that 
16943 6d 61 79 20 62 65 20 75 73 69 6e 67 20 61 6e 79  may be using any
16944 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 73 20   of the cursors 
16945 74 68 61 74 20 66 61 69 6c 65 64 20 74 6f 20 73  that failed to s
16946 61 76 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ave..    */.    
16947 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 69 70  sqlite3BtreeTrip
16948 41 6c 6c 43 75 72 73 6f 72 73 28 70 2c 20 72 63  AllCursors(p, rc
16949 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
1694a 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70  btreeIntegrity(p
1694b 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54  );..  if( p->inT
1694c 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54  rans==TRANS_WRIT
1694d 45 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 32  E ){.    int rc2
1694e 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 54  ;..    assert( T
1694f 52 41 4e 53 5f 57 52 49 54 45 3d 3d 70 42 74 2d  RANS_WRITE==pBt-
16950 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29  >inTransaction )
16951 3b 0a 20 20 20 20 72 63 32 20 3d 20 73 71 6c 69  ;.    rc2 = sqli
16952 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63 6b  te3PagerRollback
16953 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20  (pBt->pPager);. 
16954 20 20 20 69 66 28 20 72 63 32 21 3d 53 51 4c 49     if( rc2!=SQLI
16955 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
16956 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 7d 0a 0a  c = rc2;.    }..
16957 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6c 6c 62      /* The rollb
16958 61 63 6b 20 6d 61 79 20 68 61 76 65 20 64 65 73  ack may have des
16959 74 72 6f 79 65 64 20 74 68 65 20 70 50 61 67 65  troyed the pPage
1695a 31 2d 3e 61 44 61 74 61 20 76 61 6c 75 65 2e 20  1->aData value. 
1695b 20 53 6f 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20   So.    ** call 
1695c 62 74 72 65 65 47 65 74 50 61 67 65 28 29 20 6f  btreeGetPage() o
1695d 6e 20 70 61 67 65 20 31 20 61 67 61 69 6e 20 74  n page 1 again t
1695e 6f 20 6d 61 6b 65 0a 20 20 20 20 2a 2a 20 73 75  o make.    ** su
1695f 72 65 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61  re pPage1->aData
16960 20 69 73 20 73 65 74 20 63 6f 72 72 65 63 74 6c   is set correctl
16961 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 74  y. */.    if( bt
16962 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
16963 31 2c 20 26 70 50 61 67 65 31 2c 20 30 29 3d 3d  1, &pPage1, 0)==
16964 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
16965 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
16966 50 61 67 65 31 29 3b 0a 20 20 20 20 7d 0a 20 20  Page1);.    }.  
16967 20 20 61 73 73 65 72 74 28 20 63 6f 75 6e 74 57    assert( countW
16968 72 69 74 65 43 75 72 73 6f 72 73 28 70 42 74 29  riteCursors(pBt)
16969 3d 3d 30 20 29 3b 0a 20 20 20 20 70 42 74 2d 3e  ==0 );.    pBt->
1696a 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20  inTransaction = 
1696b 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20 7d 0a  TRANS_READ;.  }.
1696c 0a 20 20 62 74 72 65 65 45 6e 64 54 72 61 6e 73  .  btreeEndTrans
1696d 61 63 74 69 6f 6e 28 70 29 3b 0a 20 20 73 71 6c  action(p);.  sql
1696e 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1696f 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
16970 7d 0a 0a 2f 2a 0a 2a 2a 20 53 74 61 72 74 20 61  }../*.** Start a
16971 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72   statement subtr
16972 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 73  ansaction. The s
16973 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 20 63 61  ubtransaction ca
16974 6e 20 63 61 6e 20 62 65 20 72 6f 6c 6c 65 64 0a  n can be rolled.
16975 2a 2a 20 62 61 63 6b 20 69 6e 64 65 70 65 6e 64  ** back independ
16976 65 6e 74 6c 79 20 6f 66 20 74 68 65 20 6d 61 69  ently of the mai
16977 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 59  n transaction. Y
16978 6f 75 20 6d 75 73 74 20 73 74 61 72 74 20 61 20  ou must start a 
16979 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20  transaction .** 
1697a 62 65 66 6f 72 65 20 73 74 61 72 74 69 6e 67 20  before starting 
1697b 61 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e  a subtransaction
1697c 2e 20 54 68 65 20 73 75 62 74 72 61 6e 73 61 63  . The subtransac
1697d 74 69 6f 6e 20 69 73 20 65 6e 64 65 64 20 61 75  tion is ended au
1697e 74 6f 6d 61 74 69 63 61 6c 6c 79 20 0a 2a 2a 20  tomatically .** 
1697f 69 66 20 74 68 65 20 6d 61 69 6e 20 74 72 61 6e  if the main tran
16980 73 61 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 20  saction commits 
16981 6f 72 20 72 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a  or rolls back..*
16982 2a 0a 2a 2a 20 53 74 61 74 65 6d 65 6e 74 20 73  *.** Statement s
16983 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 61  ubtransactions a
16984 72 65 20 75 73 65 64 20 61 72 6f 75 6e 64 20 69  re used around i
16985 6e 64 69 76 69 64 75 61 6c 20 53 51 4c 20 73 74  ndividual SQL st
16986 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74  atements.** that
16987 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20 77   are contained w
16988 69 74 68 69 6e 20 61 20 42 45 47 49 4e 2e 2e 2e  ithin a BEGIN...
16989 43 4f 4d 4d 49 54 20 62 6c 6f 63 6b 2e 20 20 49  COMMIT block.  I
1698a 66 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a  f a constraint.*
1698b 2a 20 65 72 72 6f 72 20 6f 63 63 75 72 73 20 77  * error occurs w
1698c 69 74 68 69 6e 20 74 68 65 20 73 74 61 74 65 6d  ithin the statem
1698d 65 6e 74 2c 20 74 68 65 20 65 66 66 65 63 74 20  ent, the effect 
1698e 6f 66 20 74 68 61 74 20 6f 6e 65 20 73 74 61 74  of that one stat
1698f 65 6d 65 6e 74 0a 2a 2a 20 63 61 6e 20 62 65 20  ement.** can be 
16990 72 6f 6c 6c 65 64 20 62 61 63 6b 20 77 69 74 68  rolled back with
16991 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 72 6f  out having to ro
16992 6c 6c 62 61 63 6b 20 74 68 65 20 65 6e 74 69 72  llback the entir
16993 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a  e transaction..*
16994 2a 0a 2a 2a 20 41 20 73 74 61 74 65 6d 65 6e 74  *.** A statement
16995 20 73 75 62 2d 74 72 61 6e 73 61 63 74 69 6f 6e   sub-transaction
16996 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   is implemented 
16997 61 73 20 61 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20  as an anonymous 
16998 73 61 76 65 70 6f 69 6e 74 2e 20 54 68 65 0a 2a  savepoint. The.*
16999 2a 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61  * value passed a
1699a 73 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72  s the second par
1699b 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 74 6f  ameter is the to
1699c 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 61  tal number of sa
1699d 76 65 70 6f 69 6e 74 73 2c 0a 2a 2a 20 69 6e 63  vepoints,.** inc
1699e 6c 75 64 69 6e 67 20 74 68 65 20 6e 65 77 20 61  luding the new a
1699f 6e 6f 6e 79 6d 6f 75 73 20 73 61 76 65 70 6f 69  nonymous savepoi
169a0 6e 74 2c 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20  nt, open on the 
169a1 42 2d 54 72 65 65 2e 20 69 2e 65 2e 20 69 66 20  B-Tree. i.e. if 
169a2 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e 6f 20  there.** are no 
169a3 61 63 74 69 76 65 20 73 61 76 65 70 6f 69 6e 74  active savepoint
169a4 73 20 61 6e 64 20 6e 6f 20 6f 74 68 65 72 20 73  s and no other s
169a5 74 61 74 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63  tatement-transac
169a6 74 69 6f 6e 73 20 6f 70 65 6e 2c 0a 2a 2a 20 69  tions open,.** i
169a7 53 74 61 74 65 6d 65 6e 74 20 69 73 20 31 2e 20  Statement is 1. 
169a8 54 68 69 73 20 61 6e 6f 6e 79 6d 6f 75 73 20 73  This anonymous s
169a9 61 76 65 70 6f 69 6e 74 20 63 61 6e 20 62 65 20  avepoint can be 
169aa 72 65 6c 65 61 73 65 64 20 6f 72 20 72 6f 6c 6c  released or roll
169ab 65 64 20 62 61 63 6b 0a 2a 2a 20 75 73 69 6e 67  ed back.** using
169ac 20 74 68 65 20 73 71 6c 69 74 65 33 42 74 72 65   the sqlite3Btre
169ad 65 53 61 76 65 70 6f 69 6e 74 28 29 20 66 75 6e  eSavepoint() fun
169ae 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
169af 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
169b0 69 74 65 33 42 74 72 65 65 42 65 67 69 6e 53 74  ite3BtreeBeginSt
169b1 6d 74 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74  mt(Btree *p, int
169b2 20 69 53 74 61 74 65 6d 65 6e 74 29 7b 0a 20 20   iStatement){.  
169b3 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72  int rc;.  BtShar
169b4 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
169b5 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
169b6 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65  Enter(p);.  asse
169b7 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d  rt( p->inTrans==
169b8 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20  TRANS_WRITE );. 
169b9 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 72 65   assert( pBt->re
169ba 61 64 4f 6e 6c 79 3d 3d 30 20 29 3b 0a 20 20 61  adOnly==0 );.  a
169bb 73 73 65 72 74 28 20 69 53 74 61 74 65 6d 65 6e  ssert( iStatemen
169bc 74 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  t>0 );.  assert(
169bd 20 69 53 74 61 74 65 6d 65 6e 74 3e 70 2d 3e 64   iStatement>p->d
169be 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 29 3b  b->nSavepoint );
169bf 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 2d 3e  .  if( NEVER(p->
169c0 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57  inTrans!=TRANS_W
169c1 52 49 54 45 20 7c 7c 20 70 42 74 2d 3e 72 65 61  RITE || pBt->rea
169c2 64 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 72 63  dOnly) ){.    rc
169c3 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e   = SQLITE_INTERN
169c4 41 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  AL;.  }else{.   
169c5 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e   assert( pBt->in
169c6 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41  Transaction==TRA
169c7 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20 20 20  NS_WRITE );.    
169c8 2f 2a 20 41 74 20 74 68 65 20 70 61 67 65 72 20  /* At the pager 
169c9 6c 65 76 65 6c 2c 20 61 20 73 74 61 74 65 6d 65  level, a stateme
169ca 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  nt transaction i
169cb 73 20 61 20 73 61 76 65 70 6f 69 6e 74 20 77 69  s a savepoint wi
169cc 74 68 0a 20 20 20 20 2a 2a 20 61 6e 20 69 6e 64  th.    ** an ind
169cd 65 78 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  ex greater than 
169ce 61 6c 6c 20 73 61 76 65 70 6f 69 6e 74 73 20 63  all savepoints c
169cf 72 65 61 74 65 64 20 65 78 70 6c 69 63 69 74 6c  reated explicitl
169d0 79 20 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20 53  y using.    ** S
169d1 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49  QL statements. I
169d2 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20  t is illegal to 
169d3 6f 70 65 6e 2c 20 72 65 6c 65 61 73 65 20 6f 72  open, release or
169d4 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 79 0a 20 20   rollback any.  
169d5 20 20 2a 2a 20 73 75 63 68 20 73 61 76 65 70 6f    ** such savepo
169d6 69 6e 74 73 20 77 68 69 6c 65 20 74 68 65 20 73  ints while the s
169d7 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
169d8 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e 74 20 69  tion savepoint i
169d9 73 20 61 63 74 69 76 65 2e 0a 20 20 20 20 2a 2f  s active..    */
169da 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
169db 33 50 61 67 65 72 4f 70 65 6e 53 61 76 65 70 6f  3PagerOpenSavepo
169dc 69 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  int(pBt->pPager,
169dd 20 69 53 74 61 74 65 6d 65 6e 74 29 3b 0a 20 20   iStatement);.  
169de 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  }.  sqlite3Btree
169df 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
169e0 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
169e1 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
169e2 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e 63  ent to this func
169e3 74 69 6f 6e 2c 20 6f 70 2c 20 69 73 20 61 6c 77  tion, op, is alw
169e4 61 79 73 20 53 41 56 45 50 4f 49 4e 54 5f 52 4f  ays SAVEPOINT_RO
169e5 4c 4c 42 41 43 4b 0a 2a 2a 20 6f 72 20 53 41 56  LLBACK.** or SAV
169e6 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2e 20  EPOINT_RELEASE. 
169e7 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 65 69  This function ei
169e8 74 68 65 72 20 72 65 6c 65 61 73 65 73 20 6f 72  ther releases or
169e9 20 72 6f 6c 6c 73 20 62 61 63 6b 20 74 68 65 0a   rolls back the.
169ea 2a 2a 20 73 61 76 65 70 6f 69 6e 74 20 69 64 65  ** savepoint ide
169eb 6e 74 69 66 69 65 64 20 62 79 20 70 61 72 61 6d  ntified by param
169ec 65 74 65 72 20 69 53 61 76 65 70 6f 69 6e 74 2c  eter iSavepoint,
169ed 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74 68   depending on th
169ee 65 20 76 61 6c 75 65 20 0a 2a 2a 20 6f 66 20 6f  e value .** of o
169ef 70 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c  p..**.** Normall
169f0 79 2c 20 69 53 61 76 65 70 6f 69 6e 74 20 69 73  y, iSavepoint is
169f1 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f 72   greater than or
169f2 20 65 71 75 61 6c 20 74 6f 20 7a 65 72 6f 2e 20   equal to zero. 
169f3 48 6f 77 65 76 65 72 2c 20 69 66 20 6f 70 20 69  However, if op i
169f4 73 0a 2a 2a 20 53 41 56 45 50 4f 49 4e 54 5f 52  s.** SAVEPOINT_R
169f5 4f 4c 4c 42 41 43 4b 2c 20 74 68 65 6e 20 69 53  OLLBACK, then iS
169f6 61 76 65 70 6f 69 6e 74 20 6d 61 79 20 61 6c 73  avepoint may als
169f7 6f 20 62 65 20 2d 31 2e 20 49 6e 20 74 68 69 73  o be -1. In this
169f8 20 63 61 73 65 20 74 68 65 20 0a 2a 2a 20 63 6f   case the .** co
169f9 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 65 6e  ntents of the en
169fa 74 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  tire transaction
169fb 20 61 72 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b   are rolled back
169fc 2e 20 54 68 69 73 20 69 73 20 64 69 66 66 65 72  . This is differ
169fd 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 61 20 6e 6f  ent.** from a no
169fe 72 6d 61 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e  rmal transaction
169ff 20 72 6f 6c 6c 62 61 63 6b 2c 20 61 73 20 6e 6f   rollback, as no
16a00 20 6c 6f 63 6b 73 20 61 72 65 20 72 65 6c 65 61   locks are relea
16a01 73 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20 74  sed and the.** t
16a02 72 61 6e 73 61 63 74 69 6f 6e 20 72 65 6d 61 69  ransaction remai
16a03 6e 73 20 6f 70 65 6e 2e 0a 2a 2f 0a 53 51 4c 49  ns open..*/.SQLI
16a04 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
16a05 71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 70  qlite3BtreeSavep
16a06 6f 69 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69  oint(Btree *p, i
16a07 6e 74 20 6f 70 2c 20 69 6e 74 20 69 53 61 76 65  nt op, int iSave
16a08 70 6f 69 6e 74 29 7b 0a 20 20 69 6e 74 20 72 63  point){.  int rc
16a09 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
16a0a 69 66 28 20 70 20 26 26 20 70 2d 3e 69 6e 54 72  if( p && p->inTr
16a0b 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ans==TRANS_WRITE
16a0c 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72 65 64   ){.    BtShared
16a0d 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
16a0e 20 20 20 20 61 73 73 65 72 74 28 20 6f 70 3d 3d      assert( op==
16a0f 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53  SAVEPOINT_RELEAS
16a10 45 20 7c 7c 20 6f 70 3d 3d 53 41 56 45 50 4f 49  E || op==SAVEPOI
16a11 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 3b 0a 20  NT_ROLLBACK );. 
16a12 20 20 20 61 73 73 65 72 74 28 20 69 53 61 76 65     assert( iSave
16a13 70 6f 69 6e 74 3e 3d 30 20 7c 7c 20 28 69 53 61  point>=0 || (iSa
16a14 76 65 70 6f 69 6e 74 3d 3d 2d 31 20 26 26 20 6f  vepoint==-1 && o
16a15 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c  p==SAVEPOINT_ROL
16a16 4c 42 41 43 4b 29 20 29 3b 0a 20 20 20 20 73 71  LBACK) );.    sq
16a17 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
16a18 70 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  p);.    rc = sql
16a19 69 74 65 33 50 61 67 65 72 53 61 76 65 70 6f 69  ite3PagerSavepoi
16a1a 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20  nt(pBt->pPager, 
16a1b 6f 70 2c 20 69 53 61 76 65 70 6f 69 6e 74 29 3b  op, iSavepoint);
16a1c 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
16a1d 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
16a1e 72 63 20 3d 20 6e 65 77 44 61 74 61 62 61 73 65  rc = newDatabase
16a1f 28 70 42 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20  (pBt);.    }.   
16a20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
16a21 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ve(p);.  }.  ret
16a22 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
16a23 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 63 75   Create a new cu
16a24 72 73 6f 72 20 66 6f 72 20 74 68 65 20 42 54 72  rsor for the BTr
16a25 65 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 69 73  ee whose root is
16a26 20 6f 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20   on the page.** 
16a27 69 54 61 62 6c 65 2e 20 49 66 20 61 20 72 65 61  iTable. If a rea
16a28 64 2d 6f 6e 6c 79 20 63 75 72 73 6f 72 20 69 73  d-only cursor is
16a29 20 72 65 71 75 65 73 74 65 64 2c 20 69 74 20 69   requested, it i
16a2a 73 20 61 73 73 75 6d 65 64 20 74 68 61 74 0a 2a  s assumed that.*
16a2b 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 61 6c 72  * the caller alr
16a2c 65 61 64 79 20 68 61 73 20 61 74 20 6c 65 61 73  eady has at leas
16a2d 74 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 74 72  t a read-only tr
16a2e 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65 6e 0a 2a  ansaction open.*
16a2f 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  * on the databas
16a30 65 20 61 6c 72 65 61 64 79 2e 20 49 66 20 61 20  e already. If a 
16a31 77 72 69 74 65 2d 63 75 72 73 6f 72 20 69 73 20  write-cursor is 
16a32 72 65 71 75 65 73 74 65 64 2c 20 74 68 65 6e 0a  requested, then.
16a33 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73  ** the caller is
16a34 20 61 73 73 75 6d 65 64 20 74 6f 20 68 61 76 65   assumed to have
16a35 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65 20 74   an open write t
16a36 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  ransaction..**.*
16a37 2a 20 49 66 20 77 72 46 6c 61 67 3d 3d 30 2c 20  * If wrFlag==0, 
16a38 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20  then the cursor 
16a39 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 64  can only be used
16a3a 20 66 6f 72 20 72 65 61 64 69 6e 67 2e 0a 2a 2a   for reading..**
16a3b 20 49 66 20 77 72 46 6c 61 67 3d 3d 31 2c 20 74   If wrFlag==1, t
16a3c 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 63  hen the cursor c
16a3d 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 20 72  an be used for r
16a3e 65 61 64 69 6e 67 20 6f 72 20 66 6f 72 0a 2a 2a  eading or for.**
16a3f 20 77 72 69 74 69 6e 67 20 69 66 20 6f 74 68 65   writing if othe
16a40 72 20 63 6f 6e 64 69 74 69 6f 6e 73 20 66 6f 72  r conditions for
16a41 20 77 72 69 74 69 6e 67 20 61 72 65 20 61 6c 73   writing are als
16a42 6f 20 6d 65 74 2e 20 20 54 68 65 73 65 0a 2a 2a  o met.  These.**
16a43 20 61 72 65 20 74 68 65 20 63 6f 6e 64 69 74 69   are the conditi
16a44 6f 6e 73 20 74 68 61 74 20 6d 75 73 74 20 62 65  ons that must be
16a45 20 6d 65 74 20 69 6e 20 6f 72 64 65 72 20 66 6f   met in order fo
16a46 72 20 77 72 69 74 69 6e 67 20 74 6f 0a 2a 2a 20  r writing to.** 
16a47 62 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a  be allowed:.**.*
16a48 2a 20 31 3a 20 20 54 68 65 20 63 75 72 73 6f 72  * 1:  The cursor
16a49 20 6d 75 73 74 20 68 61 76 65 20 62 65 65 6e 20   must have been 
16a4a 6f 70 65 6e 65 64 20 77 69 74 68 20 77 72 46 6c  opened with wrFl
16a4b 61 67 3d 3d 31 0a 2a 2a 0a 2a 2a 20 32 3a 20 20  ag==1.**.** 2:  
16a4c 4f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63  Other database c
16a4d 6f 6e 6e 65 63 74 69 6f 6e 73 20 74 68 61 74 20  onnections that 
16a4e 73 68 61 72 65 20 74 68 65 20 73 61 6d 65 20 70  share the same p
16a4f 61 67 65 72 20 63 61 63 68 65 0a 2a 2a 20 20 20  ager cache.**   
16a50 20 20 62 75 74 20 77 68 69 63 68 20 61 72 65 20    but which are 
16a51 6e 6f 74 20 69 6e 20 74 68 65 20 52 45 41 44 5f  not in the READ_
16a52 55 4e 43 4f 4d 4d 49 54 54 45 44 20 73 74 61 74  UNCOMMITTED stat
16a53 65 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 0a 2a  e may not have.*
16a54 2a 20 20 20 20 20 63 75 72 73 6f 72 73 20 6f 70  *     cursors op
16a55 65 6e 20 77 69 74 68 20 77 72 46 6c 61 67 3d 3d  en with wrFlag==
16a56 30 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 74 61  0 on the same ta
16a57 62 6c 65 2e 20 20 4f 74 68 65 72 77 69 73 65 0a  ble.  Otherwise.
16a58 2a 2a 20 20 20 20 20 74 68 65 20 63 68 61 6e 67  **     the chang
16a59 65 73 20 6d 61 64 65 20 62 79 20 74 68 69 73 20  es made by this 
16a5a 77 72 69 74 65 20 63 75 72 73 6f 72 20 77 6f 75  write cursor wou
16a5b 6c 64 20 62 65 20 76 69 73 69 62 6c 65 20 74 6f  ld be visible to
16a5c 0a 2a 2a 20 20 20 20 20 74 68 65 20 72 65 61 64  .**     the read
16a5d 20 63 75 72 73 6f 72 73 20 69 6e 20 74 68 65 20   cursors in the 
16a5e 6f 74 68 65 72 20 64 61 74 61 62 61 73 65 20 63  other database c
16a5f 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  onnection..**.**
16a60 20 33 3a 20 20 54 68 65 20 64 61 74 61 62 61 73   3:  The databas
16a61 65 20 6d 75 73 74 20 62 65 20 77 72 69 74 61 62  e must be writab
16a62 6c 65 20 28 6e 6f 74 20 6f 6e 20 72 65 61 64 2d  le (not on read-
16a63 6f 6e 6c 79 20 6d 65 64 69 61 29 0a 2a 2a 0a 2a  only media).**.*
16a64 2a 20 34 3a 20 20 54 68 65 72 65 20 6d 75 73 74  * 4:  There must
16a65 20 62 65 20 61 6e 20 61 63 74 69 76 65 20 74 72   be an active tr
16a66 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  ansaction..**.**
16a67 20 4e 6f 20 63 68 65 63 6b 69 6e 67 20 69 73 20   No checking is 
16a68 64 6f 6e 65 20 74 6f 20 6d 61 6b 65 20 73 75 72  done to make sur
16a69 65 20 74 68 61 74 20 70 61 67 65 20 69 54 61 62  e that page iTab
16a6a 6c 65 20 72 65 61 6c 6c 79 20 69 73 20 74 68 65  le really is the
16a6b 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66  .** root page of
16a6c 20 61 20 62 2d 74 72 65 65 2e 20 20 49 66 20 69   a b-tree.  If i
16a6d 74 20 69 73 20 6e 6f 74 2c 20 74 68 65 6e 20 74  t is not, then t
16a6e 68 65 20 63 75 72 73 6f 72 20 61 63 71 75 69 72  he cursor acquir
16a6f 65 64 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 77  ed.** will not w
16a70 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a  ork correctly..*
16a71 2a 0a 2a 2a 20 49 74 20 69 73 20 61 73 73 75 6d  *.** It is assum
16a72 65 64 20 74 68 61 74 20 74 68 65 20 73 71 6c 69  ed that the sqli
16a73 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69  te3BtreeCursorSi
16a74 7a 65 28 29 20 62 79 74 65 73 20 6f 66 20 6d 65  ze() bytes of me
16a75 6d 6f 72 79 20 0a 2a 2a 20 70 6f 69 6e 74 65 64  mory .** pointed
16a76 20 74 6f 20 62 79 20 70 43 75 72 20 68 61 76 65   to by pCur have
16a77 20 62 65 65 6e 20 7a 65 72 6f 65 64 20 62 79 20   been zeroed by 
16a78 74 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 73  the caller..*/.s
16a79 74 61 74 69 63 20 69 6e 74 20 62 74 72 65 65 43  tatic int btreeC
16a7a 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20 2a  ursor(.  Btree *
16a7b 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
16a7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a7d 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f 0a  /* The btree */.
16a7e 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20    int iTable,   
16a7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a80 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74           /* Root
16a81 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 74   page of table t
16a82 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20  o open */.  int 
16a83 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20  wrFlag,         
16a84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16a85 20 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 65     /* 1 to write
16a86 2e 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f  . 0 read-only */
16a87 0a 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66  .  struct KeyInf
16a88 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20  o *pKeyInfo,    
16a89 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
16a8a 73 74 20 61 72 67 20 74 6f 20 63 6f 6d 70 61 72  st arg to compar
16a8b 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  ison function */
16a8c 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75  .  BtCursor *pCu
16a8d 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r               
16a8e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61            /* Spa
16a8f 63 65 20 66 6f 72 20 6e 65 77 20 63 75 72 73 6f  ce for new curso
16a90 72 20 2a 2f 0a 29 7b 0a 20 20 42 74 53 68 61 72  r */.){.  BtShar
16a91 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
16a92 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
16a93 20 2f 2a 20 53 68 61 72 65 64 20 62 2d 74 72 65   /* Shared b-tre
16a94 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 0a 20 20 61  e handle */..  a
16a95 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
16a96 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29  reeHoldsMutex(p)
16a97 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 77 72   );.  assert( wr
16a98 46 6c 61 67 3d 3d 30 20 7c 7c 20 77 72 46 6c 61  Flag==0 || wrFla
16a99 67 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 54 68  g==1 );..  /* Th
16a9a 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65  e following asse
16a9b 72 74 20 73 74 61 74 65 6d 65 6e 74 73 20 76 65  rt statements ve
16a9c 72 69 66 79 20 74 68 61 74 20 69 66 20 74 68 69  rify that if thi
16a9d 73 20 69 73 20 61 20 73 68 61 72 61 62 6c 65 20  s is a sharable 
16a9e 0a 20 20 2a 2a 20 62 2d 74 72 65 65 20 64 61 74  .  ** b-tree dat
16a9f 61 62 61 73 65 2c 20 74 68 65 20 63 6f 6e 6e 65  abase, the conne
16aa0 63 74 69 6f 6e 20 69 73 20 68 6f 6c 64 69 6e 67  ction is holding
16aa1 20 74 68 65 20 72 65 71 75 69 72 65 64 20 74 61   the required ta
16aa2 62 6c 65 20 6c 6f 63 6b 73 2c 20 0a 20 20 2a 2a  ble locks, .  **
16aa3 20 61 6e 64 20 74 68 61 74 20 6e 6f 20 6f 74 68   and that no oth
16aa4 65 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61  er connection ha
16aa5 73 20 61 6e 79 20 6f 70 65 6e 20 63 75 72 73 6f  s any open curso
16aa6 72 20 74 68 61 74 20 63 6f 6e 66 6c 69 63 74 73  r that conflicts
16aa7 20 77 69 74 68 20 0a 20 20 2a 2a 20 74 68 69 73   with .  ** this
16aa8 20 6c 6f 63 6b 2e 20 20 2a 2f 0a 20 20 61 73 73   lock.  */.  ass
16aa9 65 72 74 28 20 68 61 73 53 68 61 72 65 64 43 61  ert( hasSharedCa
16aaa 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20  cheTableLock(p, 
16aab 69 54 61 62 6c 65 2c 20 70 4b 65 79 49 6e 66 6f  iTable, pKeyInfo
16aac 21 3d 30 2c 20 77 72 46 6c 61 67 2b 31 29 20 29  !=0, wrFlag+1) )
16aad 3b 0a 20 20 61 73 73 65 72 74 28 20 77 72 46 6c  ;.  assert( wrFl
16aae 61 67 3d 3d 30 20 7c 7c 20 21 68 61 73 52 65 61  ag==0 || !hasRea
16aaf 64 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 69 54  dConflicts(p, iT
16ab0 61 62 6c 65 29 20 29 3b 0a 0a 20 20 2f 2a 20 41  able) );..  /* A
16ab1 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20 63  ssert that the c
16ab2 61 6c 6c 65 72 20 68 61 73 20 6f 70 65 6e 65 64  aller has opened
16ab3 20 74 68 65 20 72 65 71 75 69 72 65 64 20 74 72   the required tr
16ab4 61 6e 73 61 63 74 69 6f 6e 2e 20 2a 2f 0a 20 20  ansaction. */.  
16ab5 61 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61  assert( p->inTra
16ab6 6e 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b  ns>TRANS_NONE );
16ab7 0a 20 20 61 73 73 65 72 74 28 20 77 72 46 6c 61  .  assert( wrFla
16ab8 67 3d 3d 30 20 7c 7c 20 70 2d 3e 69 6e 54 72 61  g==0 || p->inTra
16ab9 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20  ns==TRANS_WRITE 
16aba 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  );.  assert( pBt
16abb 2d 3e 70 50 61 67 65 31 20 26 26 20 70 42 74 2d  ->pPage1 && pBt-
16abc 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 29  >pPage1->aData )
16abd 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 77  ;..  if( NEVER(w
16abe 72 46 6c 61 67 20 26 26 20 70 42 74 2d 3e 72 65  rFlag && pBt->re
16abf 61 64 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 72  adOnly) ){.    r
16ac0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41  eturn SQLITE_REA
16ac1 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 69 66 28  DONLY;.  }.  if(
16ac2 20 69 54 61 62 6c 65 3d 3d 31 20 26 26 20 70 61   iTable==1 && pa
16ac3 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74  gerPagecount(pBt
16ac4 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )==0 ){.    retu
16ac5 72 6e 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 3b  rn SQLITE_EMPTY;
16ac6 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 74  .  }..  /* Now t
16ac7 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 65 72 72  hat no other err
16ac8 6f 72 73 20 63 61 6e 20 6f 63 63 75 72 2c 20 66  ors can occur, f
16ac9 69 6e 69 73 68 20 66 69 6c 6c 69 6e 67 20 69 6e  inish filling in
16aca 20 74 68 65 20 42 74 43 75 72 73 6f 72 0a 20 20   the BtCursor.  
16acb 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 61 6e 64  ** variables and
16acc 20 6c 69 6e 6b 20 74 68 65 20 63 75 72 73 6f 72   link the cursor
16acd 20 69 6e 74 6f 20 74 68 65 20 42 74 53 68 61 72   into the BtShar
16ace 65 64 20 6c 69 73 74 2e 20 20 2a 2f 0a 20 20 70  ed list.  */.  p
16acf 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 3d 20  Cur->pgnoRoot = 
16ad0 28 50 67 6e 6f 29 69 54 61 62 6c 65 3b 0a 20 20  (Pgno)iTable;.  
16ad1 70 43 75 72 2d 3e 69 50 61 67 65 20 3d 20 2d 31  pCur->iPage = -1
16ad2 3b 0a 20 20 70 43 75 72 2d 3e 70 4b 65 79 49 6e  ;.  pCur->pKeyIn
16ad3 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a 20  fo = pKeyInfo;. 
16ad4 20 70 43 75 72 2d 3e 70 42 74 72 65 65 20 3d 20   pCur->pBtree = 
16ad5 70 3b 0a 20 20 70 43 75 72 2d 3e 70 42 74 20 3d  p;.  pCur->pBt =
16ad6 20 70 42 74 3b 0a 20 20 70 43 75 72 2d 3e 77 72   pBt;.  pCur->wr
16ad7 46 6c 61 67 20 3d 20 28 75 38 29 77 72 46 6c 61  Flag = (u8)wrFla
16ad8 67 3b 0a 20 20 70 43 75 72 2d 3e 70 4e 65 78 74  g;.  pCur->pNext
16ad9 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b   = pBt->pCursor;
16ada 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 70 4e 65  .  if( pCur->pNe
16adb 78 74 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e  xt ){.    pCur->
16adc 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70  pNext->pPrev = p
16add 43 75 72 3b 0a 20 20 7d 0a 20 20 70 42 74 2d 3e  Cur;.  }.  pBt->
16ade 70 43 75 72 73 6f 72 20 3d 20 70 43 75 72 3b 0a  pCursor = pCur;.
16adf 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d    pCur->eState =
16ae0 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b   CURSOR_INVALID;
16ae1 0a 20 20 70 43 75 72 2d 3e 63 61 63 68 65 64 52  .  pCur->cachedR
16ae2 6f 77 69 64 20 3d 20 30 3b 0a 20 20 72 65 74 75  owid = 0;.  retu
16ae3 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
16ae4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
16ae5 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
16ae6 75 72 73 6f 72 28 0a 20 20 42 74 72 65 65 20 2a  ursor(.  Btree *
16ae7 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
16ae8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16ae9 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65       /* The btre
16aea 65 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c  e */.  int iTabl
16aeb 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
16aec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16aed 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20     /* Root page 
16aee 6f 66 20 74 61 62 6c 65 20 74 6f 20 6f 70 65 6e  of table to open
16aef 20 2a 2f 0a 20 20 69 6e 74 20 77 72 46 6c 61 67   */.  int wrFlag
16af0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
16af1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16af2 20 20 2f 2a 20 31 20 74 6f 20 77 72 69 74 65 2e    /* 1 to write.
16af3 20 30 20 72 65 61 64 2d 6f 6e 6c 79 20 2a 2f 0a   0 read-only */.
16af4 20 20 73 74 72 75 63 74 20 4b 65 79 49 6e 66 6f    struct KeyInfo
16af5 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20   *pKeyInfo,     
16af6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16af7 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 78 43   First arg to xC
16af8 6f 6d 70 61 72 65 28 29 20 2a 2f 0a 20 20 42 74  ompare() */.  Bt
16af9 43 75 72 73 6f 72 20 2a 70 43 75 72 20 20 20 20  Cursor *pCur    
16afa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16afb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
16afc 74 65 20 6e 65 77 20 63 75 72 73 6f 72 20 68 65  te new cursor he
16afd 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  re */.){.  int r
16afe 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  c;.  sqlite3Btre
16aff 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20  eEnter(p);.  rc 
16b00 3d 20 62 74 72 65 65 43 75 72 73 6f 72 28 70 2c  = btreeCursor(p,
16b01 20 69 54 61 62 6c 65 2c 20 77 72 46 6c 61 67 2c   iTable, wrFlag,
16b02 20 70 4b 65 79 49 6e 66 6f 2c 20 70 43 75 72 29   pKeyInfo, pCur)
16b03 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
16b04 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
16b05 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
16b06 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20  Return the size 
16b07 6f 66 20 61 20 42 74 43 75 72 73 6f 72 20 6f 62  of a BtCursor ob
16b08 6a 65 63 74 20 69 6e 20 62 79 74 65 73 2e 0a 2a  ject in bytes..*
16b09 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66  *.** This interf
16b0a 61 63 65 73 20 69 73 20 6e 65 65 64 65 64 20 73  aces is needed s
16b0b 6f 20 74 68 61 74 20 75 73 65 72 73 20 6f 66 20  o that users of 
16b0c 63 75 72 73 6f 72 73 20 63 61 6e 20 70 72 65 61  cursors can prea
16b0d 6c 6c 6f 63 61 74 65 0a 2a 2a 20 73 75 66 66 69  llocate.** suffi
16b0e 63 69 65 6e 74 20 73 74 6f 72 61 67 65 20 74 6f  cient storage to
16b0f 20 68 6f 6c 64 20 61 20 63 75 72 73 6f 72 2e 20   hold a cursor. 
16b10 20 54 68 65 20 42 74 43 75 72 73 6f 72 20 6f 62   The BtCursor ob
16b11 6a 65 63 74 20 69 73 20 6f 70 61 71 75 65 0a 2a  ject is opaque.*
16b12 2a 20 74 6f 20 75 73 65 72 73 20 73 6f 20 74 68  * to users so th
16b13 65 79 20 63 61 6e 6e 6f 74 20 64 6f 20 74 68 65  ey cannot do the
16b14 20 73 69 7a 65 6f 66 28 29 20 74 68 65 6d 73 65   sizeof() themse
16b15 6c 76 65 73 20 2d 20 74 68 65 79 20 6d 75 73 74  lves - they must
16b16 20 63 61 6c 6c 0a 2a 2a 20 74 68 69 73 20 72 6f   call.** this ro
16b17 75 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  utine..*/.SQLITE
16b18 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
16b19 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 53  ite3BtreeCursorS
16b1a 69 7a 65 28 76 6f 69 64 29 7b 0a 20 20 72 65 74  ize(void){.  ret
16b1b 75 72 6e 20 73 69 7a 65 6f 66 28 42 74 43 75 72  urn sizeof(BtCur
16b1c 73 6f 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  sor);.}../*.** S
16b1d 65 74 20 74 68 65 20 63 61 63 68 65 64 20 72 6f  et the cached ro
16b1e 77 69 64 20 76 61 6c 75 65 20 6f 66 20 65 76 65  wid value of eve
16b1f 72 79 20 63 75 72 73 6f 72 20 69 6e 20 74 68 65  ry cursor in the
16b20 20 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 66   same database f
16b21 69 6c 65 0a 2a 2a 20 61 73 20 70 43 75 72 20 61  ile.** as pCur a
16b22 6e 64 20 68 61 76 69 6e 67 20 74 68 65 20 73 61  nd having the sa
16b23 6d 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d  me root page num
16b24 62 65 72 20 61 73 20 70 43 75 72 2e 20 20 54 68  ber as pCur.  Th
16b25 65 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 73 65  e value is.** se
16b26 74 20 74 6f 20 69 52 6f 77 69 64 2e 0a 2a 2a 0a  t to iRowid..**.
16b27 2a 2a 20 4f 6e 6c 79 20 70 6f 73 69 74 69 76 65  ** Only positive
16b28 20 72 6f 77 69 64 20 76 61 6c 75 65 73 20 61 72   rowid values ar
16b29 65 20 63 6f 6e 73 69 64 65 72 65 64 20 76 61 6c  e considered val
16b2a 69 64 20 66 6f 72 20 74 68 69 73 20 63 61 63 68  id for this cach
16b2b 65 2e 0a 2a 2a 20 54 68 65 20 63 61 63 68 65 20  e..** The cache 
16b2c 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 74  is initialized t
16b2d 6f 20 7a 65 72 6f 2c 20 69 6e 64 69 63 61 74 69  o zero, indicati
16b2e 6e 67 20 61 6e 20 69 6e 76 61 6c 69 64 20 63 61  ng an invalid ca
16b2f 63 68 65 2e 0a 2a 2a 20 41 20 62 74 72 65 65 20  che..** A btree 
16b30 77 69 6c 6c 20 77 6f 72 6b 20 66 69 6e 65 20 77  will work fine w
16b31 69 74 68 20 7a 65 72 6f 20 6f 72 20 6e 65 67 61  ith zero or nega
16b32 74 69 76 65 20 72 6f 77 69 64 73 2e 20 20 57 65  tive rowids.  We
16b33 20 6a 75 73 74 20 63 61 6e 6e 6f 74 0a 2a 2a 20   just cannot.** 
16b34 63 61 63 68 65 20 7a 65 72 6f 20 6f 72 20 6e 65  cache zero or ne
16b35 67 61 74 69 76 65 20 72 6f 77 69 64 73 2c 20 77  gative rowids, w
16b36 68 69 63 68 20 6d 65 61 6e 73 20 74 61 62 6c 65  hich means table
16b37 73 20 74 68 61 74 20 75 73 65 20 7a 65 72 6f 20  s that use zero 
16b38 6f 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 72  or.** negative r
16b39 6f 77 69 64 73 20 6d 69 67 68 74 20 72 75 6e 20  owids might run 
16b3a 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72 2e  a little slower.
16b3b 20 20 42 75 74 20 69 6e 20 70 72 61 63 74 69 63    But in practic
16b3c 65 2c 20 7a 65 72 6f 0a 2a 2a 20 6f 72 20 6e 65  e, zero.** or ne
16b3d 67 61 74 69 76 65 20 72 6f 77 69 64 73 20 61 72  gative rowids ar
16b3e 65 20 76 65 72 79 20 75 6e 63 6f 6d 6d 6f 6e 20  e very uncommon 
16b3f 73 6f 20 74 68 69 73 20 73 68 6f 75 6c 64 20 6e  so this should n
16b40 6f 74 20 62 65 20 61 20 70 72 6f 62 6c 65 6d 2e  ot be a problem.
16b41 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
16b42 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
16b43 74 72 65 65 53 65 74 43 61 63 68 65 64 52 6f 77  treeSetCachedRow
16b44 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  id(BtCursor *pCu
16b45 72 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  r, sqlite3_int64
16b46 20 69 52 6f 77 69 64 29 7b 0a 20 20 42 74 43 75   iRowid){.  BtCu
16b47 72 73 6f 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70  rsor *p;.  for(p
16b48 3d 70 43 75 72 2d 3e 70 42 74 2d 3e 70 43 75 72  =pCur->pBt->pCur
16b49 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65  sor; p; p=p->pNe
16b4a 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  xt){.    if( p->
16b4b 70 67 6e 6f 52 6f 6f 74 3d 3d 70 43 75 72 2d 3e  pgnoRoot==pCur->
16b4c 70 67 6e 6f 52 6f 6f 74 20 29 20 70 2d 3e 63 61  pgnoRoot ) p->ca
16b4d 63 68 65 64 52 6f 77 69 64 20 3d 20 69 52 6f 77  chedRowid = iRow
16b4e 69 64 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  id;.  }.  assert
16b4f 28 20 70 43 75 72 2d 3e 63 61 63 68 65 64 52 6f  ( pCur->cachedRo
16b50 77 69 64 3d 3d 69 52 6f 77 69 64 20 29 3b 0a 7d  wid==iRowid );.}
16b51 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
16b52 68 65 20 63 61 63 68 65 64 20 72 6f 77 69 64 20  he cached rowid 
16b53 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 63 75  for the given cu
16b54 72 73 6f 72 2e 20 20 41 20 6e 65 67 61 74 69 76  rsor.  A negativ
16b55 65 20 6f 72 20 7a 65 72 6f 0a 2a 2a 20 72 65 74  e or zero.** ret
16b56 75 72 6e 20 76 61 6c 75 65 20 69 6e 64 69 63 61  urn value indica
16b57 74 65 73 20 74 68 61 74 20 74 68 65 20 72 6f 77  tes that the row
16b58 69 64 20 63 61 63 68 65 20 69 73 20 69 6e 76 61  id cache is inva
16b59 6c 69 64 20 61 6e 64 20 73 68 6f 75 6c 64 20 62  lid and should b
16b5a 65 0a 2a 2a 20 69 67 6e 6f 72 65 64 2e 20 20 49  e.** ignored.  I
16b5b 66 20 74 68 65 20 72 6f 77 69 64 20 63 61 63 68  f the rowid cach
16b5c 65 20 68 61 73 20 6e 65 76 65 72 20 62 65 66 6f  e has never befo
16b5d 72 65 20 62 65 65 6e 20 73 65 74 2c 20 74 68 65  re been set, the
16b5e 6e 20 61 0a 2a 2a 20 7a 65 72 6f 20 69 73 20 72  n a.** zero is r
16b5f 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  eturned..*/.SQLI
16b60 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74  TE_PRIVATE sqlit
16b61 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e3_int64 sqlite3
16b62 42 74 72 65 65 47 65 74 43 61 63 68 65 64 52 6f  BtreeGetCachedRo
16b63 77 69 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43  wid(BtCursor *pC
16b64 75 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 43  ur){.  return pC
16b65 75 72 2d 3e 63 61 63 68 65 64 52 6f 77 69 64 3b  ur->cachedRowid;
16b66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  .}../*.** Close 
16b67 61 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 72  a cursor.  The r
16b68 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ead lock on the 
16b69 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73  database file is
16b6a 20 72 65 6c 65 61 73 65 64 0a 2a 2a 20 77 68 65   released.** whe
16b6b 6e 20 74 68 65 20 6c 61 73 74 20 63 75 72 73 6f  n the last curso
16b6c 72 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a  r is closed..*/.
16b6d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
16b6e 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
16b6f 6c 6f 73 65 43 75 72 73 6f 72 28 42 74 43 75 72  loseCursor(BtCur
16b70 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 42 74  sor *pCur){.  Bt
16b71 72 65 65 20 2a 70 42 74 72 65 65 20 3d 20 70 43  ree *pBtree = pC
16b72 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20 20 69 66  ur->pBtree;.  if
16b73 28 20 70 42 74 72 65 65 20 29 7b 0a 20 20 20 20  ( pBtree ){.    
16b74 69 6e 74 20 69 3b 0a 20 20 20 20 42 74 53 68 61  int i;.    BtSha
16b75 72 65 64 20 2a 70 42 74 20 3d 20 70 43 75 72 2d  red *pBt = pCur-
16b76 3e 70 42 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  >pBt;.    sqlite
16b77 33 42 74 72 65 65 45 6e 74 65 72 28 70 42 74 72  3BtreeEnter(pBtr
16b78 65 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ee);.    sqlite3
16b79 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72  BtreeClearCursor
16b7a 28 70 43 75 72 29 3b 0a 20 20 20 20 69 66 28 20  (pCur);.    if( 
16b7b 70 43 75 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20  pCur->pPrev ){. 
16b7c 20 20 20 20 20 70 43 75 72 2d 3e 70 50 72 65 76       pCur->pPrev
16b7d 2d 3e 70 4e 65 78 74 20 3d 20 70 43 75 72 2d 3e  ->pNext = pCur->
16b7e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65  pNext;.    }else
16b7f 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 43 75  {.      pBt->pCu
16b80 72 73 6f 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65  rsor = pCur->pNe
16b81 78 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  xt;.    }.    if
16b82 28 20 70 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b  ( pCur->pNext ){
16b83 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 70 4e 65  .      pCur->pNe
16b84 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 43 75 72  xt->pPrev = pCur
16b85 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20  ->pPrev;.    }. 
16b86 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70     for(i=0; i<=p
16b87 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29  Cur->iPage; i++)
16b88 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50  {.      releaseP
16b89 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67 65  age(pCur->apPage
16b8a 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  [i]);.    }.    
16b8b 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75  unlockBtreeIfUnu
16b8c 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 69 6e  sed(pBt);.    in
16b8d 76 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77  validateOverflow
16b8e 43 61 63 68 65 28 70 43 75 72 29 3b 0a 20 20 20  Cache(pCur);.   
16b8f 20 2f 2a 20 73 71 6c 69 74 65 33 5f 66 72 65 65   /* sqlite3_free
16b90 28 70 43 75 72 29 3b 20 2a 2f 0a 20 20 20 20 73  (pCur); */.    s
16b91 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
16b92 28 70 42 74 72 65 65 29 3b 0a 20 20 7d 0a 20 20  (pBtree);.  }.  
16b93 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
16b94 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  ;.}../*.** Make 
16b95 73 75 72 65 20 74 68 65 20 42 74 43 75 72 73 6f  sure the BtCurso
16b96 72 2a 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20  r* given in the 
16b97 61 72 67 75 6d 65 6e 74 20 68 61 73 20 61 20 76  argument has a v
16b98 61 6c 69 64 0a 2a 2a 20 42 74 43 75 72 73 6f 72  alid.** BtCursor
16b99 2e 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e  .info structure.
16b9a 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20 61    If it is not a
16b9b 6c 72 65 61 64 79 20 76 61 6c 69 64 2c 20 63 61  lready valid, ca
16b9c 6c 6c 0a 2a 2a 20 62 74 72 65 65 50 61 72 73 65  ll.** btreeParse
16b9d 43 65 6c 6c 28 29 20 74 6f 20 66 69 6c 6c 20 69  Cell() to fill i
16b9e 74 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 42 74 43 75  t in..**.** BtCu
16b9f 72 73 6f 72 2e 69 6e 66 6f 20 69 73 20 61 20 63  rsor.info is a c
16ba0 61 63 68 65 20 6f 66 20 74 68 65 20 69 6e 66 6f  ache of the info
16ba1 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 20 63  rmation in the c
16ba2 75 72 72 65 6e 74 20 63 65 6c 6c 2e 0a 2a 2a 20  urrent cell..** 
16ba3 55 73 69 6e 67 20 74 68 69 73 20 63 61 63 68 65  Using this cache
16ba4 20 72 65 64 75 63 65 73 20 74 68 65 20 6e 75 6d   reduces the num
16ba5 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 74 6f 20  ber of calls to 
16ba6 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 29  btreeParseCell()
16ba7 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 37 2d 30 36 2d  ..**.** 2007-06-
16ba8 32 35 3a 20 20 54 68 65 72 65 20 69 73 20 61 20  25:  There is a 
16ba9 62 75 67 20 69 6e 20 73 6f 6d 65 20 76 65 72 73  bug in some vers
16baa 69 6f 6e 73 20 6f 66 20 4d 53 56 43 20 74 68 61  ions of MSVC tha
16bab 74 20 63 61 75 73 65 20 74 68 65 0a 2a 2a 20 63  t cause the.** c
16bac 6f 6d 70 69 6c 65 72 20 74 6f 20 63 72 61 73 68  ompiler to crash
16bad 20 77 68 65 6e 20 67 65 74 43 65 6c 6c 49 6e 66   when getCellInf
16bae 6f 28 29 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74  o() is implement
16baf 65 64 20 61 73 20 61 20 6d 61 63 72 6f 2e 0a 2a  ed as a macro..*
16bb0 2a 20 42 75 74 20 74 68 65 72 65 20 69 73 20 61  * But there is a
16bb1 20 6d 65 61 73 75 72 65 61 62 6c 65 20 73 70 65   measureable spe
16bb2 65 64 20 61 64 76 61 6e 74 61 67 65 20 74 6f 20  ed advantage to 
16bb3 75 73 69 6e 67 20 74 68 65 20 6d 61 63 72 6f 20  using the macro 
16bb4 6f 6e 20 67 63 63 0a 2a 2a 20 28 77 68 65 6e 20  on gcc.** (when 
16bb5 6c 65 73 73 20 63 6f 6d 70 69 6c 65 72 20 6f 70  less compiler op
16bb6 74 69 6d 69 7a 61 74 69 6f 6e 73 20 6c 69 6b 65  timizations like
16bb7 20 2d 4f 73 20 6f 72 20 2d 4f 30 20 61 72 65 20   -Os or -O0 are 
16bb8 75 73 65 64 20 61 6e 64 20 74 68 65 0a 2a 2a 20  used and the.** 
16bb9 63 6f 6d 70 69 6c 65 72 20 69 73 20 6e 6f 74 20  compiler is not 
16bba 64 6f 69 6e 67 20 61 67 72 65 73 73 69 76 65 20  doing agressive 
16bbb 69 6e 6c 69 6e 69 6e 67 2e 29 20 20 53 6f 20 77  inlining.)  So w
16bbc 65 20 75 73 65 20 61 20 72 65 61 6c 20 66 75 6e  e use a real fun
16bbd 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 4d 53 56  ction.** for MSV
16bbe 43 20 61 6e 64 20 61 20 6d 61 63 72 6f 20 66 6f  C and a macro fo
16bbf 72 20 65 76 65 72 79 74 68 69 6e 67 20 65 6c 73  r everything els
16bc0 65 2e 20 20 54 69 63 6b 65 74 20 23 32 34 35 37  e.  Ticket #2457
16bc1 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45  ..*/.#ifndef NDE
16bc2 42 55 47 0a 20 20 73 74 61 74 69 63 20 76 6f 69  BUG.  static voi
16bc3 64 20 61 73 73 65 72 74 43 65 6c 6c 49 6e 66 6f  d assertCellInfo
16bc4 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
16bc5 7b 0a 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69  {.    CellInfo i
16bc6 6e 66 6f 3b 0a 20 20 20 20 69 6e 74 20 69 50 61  nfo;.    int iPa
16bc7 67 65 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65  ge = pCur->iPage
16bc8 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 69 6e  ;.    memset(&in
16bc9 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 69 6e  fo, 0, sizeof(in
16bca 66 6f 29 29 3b 0a 20 20 20 20 62 74 72 65 65 50  fo));.    btreeP
16bcb 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e 61  arseCell(pCur->a
16bcc 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 20 70 43  pPage[iPage], pC
16bcd 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 5d  ur->aiIdx[iPage]
16bce 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20 61 73  , &info);.    as
16bcf 73 65 72 74 28 20 6d 65 6d 63 6d 70 28 26 69 6e  sert( memcmp(&in
16bd0 66 6f 2c 20 26 70 43 75 72 2d 3e 69 6e 66 6f 2c  fo, &pCur->info,
16bd1 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3d 3d   sizeof(info))==
16bd2 30 20 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20  0 );.  }.#else. 
16bd3 20 23 64 65 66 69 6e 65 20 61 73 73 65 72 74 43   #define assertC
16bd4 65 6c 6c 49 6e 66 6f 28 78 29 0a 23 65 6e 64 69  ellInfo(x).#endi
16bd5 66 0a 23 69 66 64 65 66 20 5f 4d 53 43 5f 56 45  f.#ifdef _MSC_VE
16bd6 52 0a 20 20 2f 2a 20 55 73 65 20 61 20 72 65 61  R.  /* Use a rea
16bd7 6c 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20 4d 53  l function in MS
16bd8 56 43 20 74 6f 20 77 6f 72 6b 20 61 72 6f 75 6e  VC to work aroun
16bd9 64 20 62 75 67 73 20 69 6e 20 74 68 61 74 20 63  d bugs in that c
16bda 6f 6d 70 69 6c 65 72 2e 20 2a 2f 0a 20 20 73 74  ompiler. */.  st
16bdb 61 74 69 63 20 76 6f 69 64 20 67 65 74 43 65 6c  atic void getCel
16bdc 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f 72 20 2a  lInfo(BtCursor *
16bdd 70 43 75 72 29 7b 0a 20 20 20 20 69 66 28 20 70  pCur){.    if( p
16bde 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 3d  Cur->info.nSize=
16bdf 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  =0 ){.      int 
16be0 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 69 50  iPage = pCur->iP
16be1 61 67 65 3b 0a 20 20 20 20 20 20 62 74 72 65 65  age;.      btree
16be2 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e  ParseCell(pCur->
16be3 61 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 70 43  apPage[iPage],pC
16be4 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 5d  ur->aiIdx[iPage]
16be5 2c 26 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20  ,&pCur->info);. 
16be6 20 20 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64       pCur->valid
16be7 4e 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20 7d 65  NKey = 1;.    }e
16be8 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72  lse{.      asser
16be9 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b  tCellInfo(pCur);
16bea 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6c 73 65  .    }.  }.#else
16beb 20 2f 2a 20 69 66 20 6e 6f 74 20 5f 4d 53 43 5f   /* if not _MSC_
16bec 56 45 52 20 2a 2f 0a 20 20 2f 2a 20 55 73 65 20  VER */.  /* Use 
16bed 61 20 6d 61 63 72 6f 20 69 6e 20 61 6c 6c 20 6f  a macro in all o
16bee 74 68 65 72 20 63 6f 6d 70 69 6c 65 72 73 20 73  ther compilers s
16bef 6f 20 74 68 61 74 20 74 68 65 20 66 75 6e 63 74  o that the funct
16bf0 69 6f 6e 20 69 73 20 69 6e 6c 69 6e 65 64 20 2a  ion is inlined *
16bf1 2f 0a 23 64 65 66 69 6e 65 20 67 65 74 43 65 6c  /.#define getCel
16bf2 6c 49 6e 66 6f 28 70 43 75 72 29 20 20 20 20 20  lInfo(pCur)     
16bf3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bf5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bf6 20 5c 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69   \.  if( pCur->i
16bf7 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 20 29 7b 20  nfo.nSize==0 ){ 
16bf8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bf9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bfa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bfb 20 20 5c 0a 20 20 20 20 69 6e 74 20 69 50 61 67    \.    int iPag
16bfc 65 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b  e = pCur->iPage;
16bfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bfe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16bff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c00 20 20 20 5c 0a 20 20 20 20 62 74 72 65 65 50 61     \.    btreePa
16c01 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70  rseCell(pCur->ap
16c02 50 61 67 65 5b 69 50 61 67 65 5d 2c 70 43 75 72  Page[iPage],pCur
16c03 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 5d 2c 26  ->aiIdx[iPage],&
16c04 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 20 5c 0a 20  pCur->info); \. 
16c05 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b     pCur->validNK
16c06 65 79 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20  ey = 1;         
16c07 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
16c0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20    }else{        
16c0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
16c0f 0a 20 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49  .    assertCellI
16c10 6e 66 6f 28 70 43 75 72 29 3b 20 20 20 20 20 20  nfo(pCur);      
16c11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c12 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16c14 5c 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  \.  }.#endif /* 
16c15 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 0a 23 69 66  _MSC_VER */..#if
16c16 6e 64 65 66 20 4e 44 45 42 55 47 20 20 2f 2a 20  ndef NDEBUG  /* 
16c17 54 68 65 20 6e 65 78 74 20 72 6f 75 74 69 6e 65  The next routine
16c18 20 75 73 65 64 20 6f 6e 6c 79 20 77 69 74 68 69   used only withi
16c19 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65  n assert() state
16c1a 6d 65 6e 74 73 20 2a 2f 0a 2f 2a 0a 2a 2a 20 52  ments */./*.** R
16c1b 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68  eturn true if th
16c1c 65 20 67 69 76 65 6e 20 42 74 43 75 72 73 6f 72  e given BtCursor
16c1d 20 69 73 20 76 61 6c 69 64 2e 20 20 41 20 76 61   is valid.  A va
16c1e 6c 69 64 20 63 75 72 73 6f 72 20 69 73 20 6f 6e  lid cursor is on
16c1f 65 0a 2a 2a 20 74 68 61 74 20 69 73 20 63 75 72  e.** that is cur
16c20 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20  rently pointing 
16c21 74 6f 20 61 20 72 6f 77 20 69 6e 20 61 20 28 6e  to a row in a (n
16c22 6f 6e 2d 65 6d 70 74 79 29 20 74 61 62 6c 65 2e  on-empty) table.
16c23 0a 2a 2a 20 54 68 69 73 20 69 73 20 61 20 76 65  .** This is a ve
16c24 72 69 66 69 63 61 74 69 6f 6e 20 72 6f 75 74 69  rification routi
16c25 6e 65 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20  ne is used only 
16c26 77 69 74 68 69 6e 20 61 73 73 65 72 74 28 29 20  within assert() 
16c27 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53  statements..*/.S
16c28 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
16c29 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  t sqlite3BtreeCu
16c2a 72 73 6f 72 49 73 56 61 6c 69 64 28 42 74 43 75  rsorIsValid(BtCu
16c2b 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 72  rsor *pCur){.  r
16c2c 65 74 75 72 6e 20 70 43 75 72 20 26 26 20 70 43  eturn pCur && pC
16c2d 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
16c2e 4f 52 5f 56 41 4c 49 44 3b 0a 7d 0a 23 65 6e 64  OR_VALID;.}.#end
16c2f 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a  if /* NDEBUG */.
16c30 0a 2f 2a 0a 2a 2a 20 53 65 74 20 2a 70 53 69 7a  ./*.** Set *pSiz
16c31 65 20 74 6f 20 74 68 65 20 73 69 7a 65 20 6f 66  e to the size of
16c32 20 74 68 65 20 62 75 66 66 65 72 20 6e 65 65 64   the buffer need
16c33 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 76  ed to hold the v
16c34 61 6c 75 65 20 6f 66 0a 2a 2a 20 74 68 65 20 6b  alue of.** the k
16c35 65 79 20 66 6f 72 20 74 68 65 20 63 75 72 72 65  ey for the curre
16c36 6e 74 20 65 6e 74 72 79 2e 20 20 49 66 20 74 68  nt entry.  If th
16c37 65 20 63 75 72 73 6f 72 20 69 73 20 6e 6f 74 20  e cursor is not 
16c38 70 6f 69 6e 74 69 6e 67 0a 2a 2a 20 74 6f 20 61  pointing.** to a
16c39 20 76 61 6c 69 64 20 65 6e 74 72 79 2c 20 2a 70   valid entry, *p
16c3a 53 69 7a 65 20 69 73 20 73 65 74 20 74 6f 20 30  Size is set to 0
16c3b 2e 20 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 74  . .**.** For a t
16c3c 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 49 4e  able with the IN
16c3d 54 4b 45 59 20 66 6c 61 67 20 73 65 74 2c 20 74  TKEY flag set, t
16c3e 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
16c3f 72 6e 73 20 74 68 65 20 6b 65 79 0a 2a 2a 20 69  rns the key.** i
16c40 74 73 65 6c 66 2c 20 6e 6f 74 20 74 68 65 20 6e  tself, not the n
16c41 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
16c42 6e 20 74 68 65 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a  n the key..**.**
16c43 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74   The caller must
16c44 20 70 6f 73 69 74 69 6f 6e 20 74 68 65 20 63 75   position the cu
16c45 72 73 6f 72 20 70 72 69 6f 72 20 74 6f 20 69 6e  rsor prior to in
16c46 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 74  voking this rout
16c47 69 6e 65 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 69 73  ine..** .** This
16c48 20 72 6f 75 74 69 6e 65 20 63 61 6e 6e 6f 74 20   routine cannot 
16c49 66 61 69 6c 2e 20 20 49 74 20 61 6c 77 61 79 73  fail.  It always
16c4a 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
16c4b 4f 4b 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f  OK.  .*/.SQLITE_
16c4c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
16c4d 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28  te3BtreeKeySize(
16c4e 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20  BtCursor *pCur, 
16c4f 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20 61  i64 *pSize){.  a
16c50 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
16c51 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
16c52 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
16c53 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
16c54 49 4e 56 41 4c 49 44 20 7c 7c 20 70 43 75 72 2d  INVALID || pCur-
16c55 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
16c56 56 41 4c 49 44 20 29 3b 0a 20 20 69 66 28 20 70  VALID );.  if( p
16c57 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52  Cur->eState!=CUR
16c58 53 4f 52 5f 56 41 4c 49 44 20 29 7b 0a 20 20 20  SOR_VALID ){.   
16c59 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 20 20 7d   *pSize = 0;.  }
16c5a 65 6c 73 65 7b 0a 20 20 20 20 67 65 74 43 65 6c  else{.    getCel
16c5b 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a 20 20 20  lInfo(pCur);.   
16c5c 20 2a 70 53 69 7a 65 20 3d 20 70 43 75 72 2d 3e   *pSize = pCur->
16c5d 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 7d 0a 20  info.nKey;.  }. 
16c5e 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
16c5f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  K;.}../*.** Set 
16c60 2a 70 53 69 7a 65 20 74 6f 20 74 68 65 20 6e 75  *pSize to the nu
16c61 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
16c62 20 64 61 74 61 20 69 6e 20 74 68 65 20 65 6e 74   data in the ent
16c63 72 79 20 74 68 65 0a 2a 2a 20 63 75 72 73 6f 72  ry the.** cursor
16c64 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
16c65 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  s to..**.** The 
16c66 63 61 6c 6c 65 72 20 6d 75 73 74 20 67 75 61 72  caller must guar
16c67 61 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 63  antee that the c
16c68 75 72 73 6f 72 20 69 73 20 70 6f 69 6e 74 69 6e  ursor is pointin
16c69 67 20 74 6f 20 61 20 6e 6f 6e 2d 4e 55 4c 4c 0a  g to a non-NULL.
16c6a 2a 2a 20 76 61 6c 69 64 20 65 6e 74 72 79 2e 20  ** valid entry. 
16c6b 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
16c6c 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f   the calling pro
16c6d 63 65 64 75 72 65 20 6d 75 73 74 20 67 75 61 72  cedure must guar
16c6e 61 6e 74 65 65 0a 2a 2a 20 74 68 61 74 20 74 68  antee.** that th
16c6f 65 20 63 75 72 73 6f 72 20 68 61 73 20 43 75 72  e cursor has Cur
16c70 73 6f 72 2e 65 53 74 61 74 65 3d 3d 43 55 52 53  sor.eState==CURS
16c71 4f 52 5f 56 41 4c 49 44 2e 0a 2a 2a 0a 2a 2a 20  OR_VALID..**.** 
16c72 46 61 69 6c 75 72 65 20 69 73 20 6e 6f 74 20 70  Failure is not p
16c73 6f 73 73 69 62 6c 65 2e 20 20 54 68 69 73 20 66  ossible.  This f
16c74 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 72  unction always r
16c75 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4f 4b  eturns SQLITE_OK
16c76 2e 0a 2a 2a 20 49 74 20 6d 69 67 68 74 20 6a 75  ..** It might ju
16c77 73 74 20 61 73 20 77 65 6c 6c 20 62 65 20 61 20  st as well be a 
16c78 70 72 6f 63 65 64 75 72 65 20 28 72 65 74 75 72  procedure (retur
16c79 6e 69 6e 67 20 76 6f 69 64 29 20 62 75 74 20 77  ning void) but w
16c7a 65 20 63 6f 6e 74 69 6e 75 65 0a 2a 2a 20 74 6f  e continue.** to
16c7b 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67   return an integ
16c7c 65 72 20 72 65 73 75 6c 74 20 63 6f 64 65 20 66  er result code f
16c7d 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65  or historical re
16c7e 61 73 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  asons..*/.SQLITE
16c7f 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
16c80 69 74 65 33 42 74 72 65 65 44 61 74 61 53 69 7a  ite3BtreeDataSiz
16c81 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  e(BtCursor *pCur
16c82 2c 20 75 33 32 20 2a 70 53 69 7a 65 29 7b 0a 20  , u32 *pSize){. 
16c83 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
16c84 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
16c85 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  );.  assert( pCu
16c86 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
16c87 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 67 65 74  R_VALID );.  get
16c88 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a  CellInfo(pCur);.
16c89 20 20 2a 70 53 69 7a 65 20 3d 20 70 43 75 72 2d    *pSize = pCur-
16c8a 3e 69 6e 66 6f 2e 6e 44 61 74 61 3b 0a 20 20 72  >info.nData;.  r
16c8b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
16c8c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20  .}../*.** Given 
16c8d 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20  the page number 
16c8e 6f 66 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70  of an overflow p
16c8f 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  age in the datab
16c90 61 73 65 20 28 70 61 72 61 6d 65 74 65 72 0a 2a  ase (parameter.*
16c91 2a 20 6f 76 66 6c 29 2c 20 74 68 69 73 20 66 75  * ovfl), this fu
16c92 6e 63 74 69 6f 6e 20 66 69 6e 64 73 20 74 68 65  nction finds the
16c93 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   page number of 
16c94 74 68 65 20 6e 65 78 74 20 70 61 67 65 20 69 6e  the next page in
16c95 20 74 68 65 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20   the .** linked 
16c96 6c 69 73 74 20 6f 66 20 6f 76 65 72 66 6c 6f 77  list of overflow
16c97 20 70 61 67 65 73 2e 20 49 66 20 70 6f 73 73 69   pages. If possi
16c98 62 6c 65 2c 20 69 74 20 75 73 65 73 20 74 68 65  ble, it uses the
16c99 20 61 75 74 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20   auto-vacuum.** 
16c9a 70 6f 69 6e 74 65 72 2d 6d 61 70 20 64 61 74 61  pointer-map data
16c9b 20 69 6e 73 74 65 61 64 20 6f 66 20 72 65 61 64   instead of read
16c9c 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ing the content 
16c9d 6f 66 20 70 61 67 65 20 6f 76 66 6c 20 74 6f 20  of page ovfl to 
16c9e 64 6f 20 73 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66  do so. .**.** If
16c9f 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
16ca0 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
16ca1 20 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65   code is returne
16ca2 64 2e 20 4f 74 68 65 72 77 69 73 65 3a 0a 2a 2a  d. Otherwise:.**
16ca3 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 6e 75 6d  .** The page num
16ca4 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74 20  ber of the next 
16ca5 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e  overflow page in
16ca6 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74   the linked list
16ca7 20 69 73 20 0a 2a 2a 20 77 72 69 74 74 65 6e 20   is .** written 
16ca8 74 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 2e 20 49  to *pPgnoNext. I
16ca9 66 20 70 61 67 65 20 6f 76 66 6c 20 69 73 20 74  f page ovfl is t
16caa 68 65 20 6c 61 73 74 20 70 61 67 65 20 69 6e 20  he last page in 
16cab 69 74 73 20 6c 69 6e 6b 65 64 20 0a 2a 2a 20 6c  its linked .** l
16cac 69 73 74 2c 20 2a 70 50 67 6e 6f 4e 65 78 74 20  ist, *pPgnoNext 
16cad 69 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 2e 20  is set to zero. 
16cae 0a 2a 2a 0a 2a 2a 20 49 66 20 70 70 50 61 67 65  .**.** If ppPage
16caf 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 61 6e   is not NULL, an
16cb0 64 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  d a reference to
16cb1 20 74 68 65 20 4d 65 6d 50 61 67 65 20 6f 62 6a   the MemPage obj
16cb2 65 63 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  ect correspondin
16cb3 67 0a 2a 2a 20 74 6f 20 70 61 67 65 20 6e 75 6d  g.** to page num
16cb4 62 65 72 20 70 4f 76 66 6c 20 77 61 73 20 6f 62  ber pOvfl was ob
16cb5 74 61 69 6e 65 64 2c 20 74 68 65 6e 20 2a 70 70  tained, then *pp
16cb6 50 61 67 65 20 69 73 20 73 65 74 20 74 6f 20 70  Page is set to p
16cb7 6f 69 6e 74 20 74 6f 20 74 68 61 74 0a 2a 2a 20  oint to that.** 
16cb8 72 65 66 65 72 65 6e 63 65 2e 20 49 74 20 69 73  reference. It is
16cb9 20 74 68 65 20 72 65 73 70 6f 6e 73 69 62 69 6c   the responsibil
16cba 69 74 79 20 6f 66 20 74 68 65 20 63 61 6c 6c 65  ity of the calle
16cbb 72 20 74 6f 20 63 61 6c 6c 20 72 65 6c 65 61 73  r to call releas
16cbc 65 50 61 67 65 28 29 0a 2a 2a 20 6f 6e 20 2a 70  ePage().** on *p
16cbd 70 50 61 67 65 20 74 6f 20 66 72 65 65 20 74 68  pPage to free th
16cbe 65 20 72 65 66 65 72 65 6e 63 65 2e 20 49 6e 20  e reference. In 
16cbf 6e 6f 20 72 65 66 65 72 65 6e 63 65 20 77 61 73  no reference was
16cc0 20 6f 62 74 61 69 6e 65 64 20 28 62 65 63 61 75   obtained (becau
16cc1 73 65 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65  se.** the pointe
16cc2 72 2d 6d 61 70 20 77 61 73 20 75 73 65 64 20 74  r-map was used t
16cc3 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 76 61 6c  o obtain the val
16cc4 75 65 20 66 6f 72 20 2a 70 50 67 6e 6f 4e 65 78  ue for *pPgnoNex
16cc5 74 29 2c 20 74 68 65 6e 0a 2a 2a 20 2a 70 70 50  t), then.** *ppP
16cc6 61 67 65 20 69 73 20 73 65 74 20 74 6f 20 7a 65  age is set to ze
16cc7 72 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ro..*/.static in
16cc8 74 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67  t getOverflowPag
16cc9 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  e(.  BtShared *p
16cca 42 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  Bt,             
16ccb 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
16ccc 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f  e file */.  Pgno
16ccd 20 6f 76 66 6c 2c 20 20 20 20 20 20 20 20 20 20   ovfl,          
16cce 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72           /* Curr
16ccf 65 6e 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  ent overflow pag
16cd0 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 4d 65  e number */.  Me
16cd1 6d 50 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20  mPage **ppPage, 
16cd2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
16cd3 54 3a 20 4d 65 6d 50 61 67 65 20 68 61 6e 64 6c  T: MemPage handl
16cd4 65 20 28 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20  e (may be NULL) 
16cd5 2a 2f 0a 20 20 50 67 6e 6f 20 2a 70 50 67 6e 6f  */.  Pgno *pPgno
16cd6 4e 65 78 74 20 20 20 20 20 20 20 20 20 20 20 20  Next            
16cd7 20 20 2f 2a 20 4f 55 54 3a 20 4e 65 78 74 20 6f    /* OUT: Next o
16cd8 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6e 75 6d  verflow page num
16cd9 62 65 72 20 2a 2f 0a 29 7b 0a 20 20 50 67 6e 6f  ber */.){.  Pgno
16cda 20 6e 65 78 74 20 3d 20 30 3b 0a 20 20 4d 65 6d   next = 0;.  Mem
16cdb 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30 3b  Page *pPage = 0;
16cdc 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
16cdd 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65 72 74  TE_OK;..  assert
16cde 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
16cdf 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
16ce0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 70 50 67   );.  assert(pPg
16ce1 6e 6f 4e 65 78 74 29 3b 0a 0a 23 69 66 6e 64 65  noNext);..#ifnde
16ce2 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
16ce3 54 4f 56 41 43 55 55 4d 0a 20 20 2f 2a 20 54 72  TOVACUUM.  /* Tr
16ce4 79 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 65  y to find the ne
16ce5 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f  xt page in the o
16ce6 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 75 73 69  verflow list usi
16ce7 6e 67 20 74 68 65 0a 20 20 2a 2a 20 61 75 74 6f  ng the.  ** auto
16ce8 76 61 63 75 75 6d 20 70 6f 69 6e 74 65 72 2d 6d  vacuum pointer-m
16ce9 61 70 20 70 61 67 65 73 2e 20 47 75 65 73 73 20  ap pages. Guess 
16cea 74 68 61 74 20 74 68 65 20 6e 65 78 74 20 70 61  that the next pa
16ceb 67 65 20 69 6e 20 0a 20 20 2a 2a 20 74 68 65 20  ge in .  ** the 
16cec 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 69 73  overflow list is
16ced 20 70 61 67 65 20 6e 75 6d 62 65 72 20 28 6f 76   page number (ov
16cee 66 6c 2b 31 29 2e 20 49 66 20 74 68 61 74 20 67  fl+1). If that g
16cef 75 65 73 73 20 74 75 72 6e 73 20 0a 20 20 2a 2a  uess turns .  **
16cf0 20 6f 75 74 20 74 6f 20 62 65 20 77 72 6f 6e 67   out to be wrong
16cf1 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 6c  , fall back to l
16cf2 6f 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 20  oading the data 
16cf3 6f 66 20 70 61 67 65 20 0a 20 20 2a 2a 20 6e 75  of page .  ** nu
16cf4 6d 62 65 72 20 6f 76 66 6c 20 74 6f 20 64 65 74  mber ovfl to det
16cf5 65 72 6d 69 6e 65 20 74 68 65 20 6e 65 78 74 20  ermine the next 
16cf6 70 61 67 65 20 6e 75 6d 62 65 72 2e 0a 20 20 2a  page number..  *
16cf7 2f 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74  /.  if( pBt->aut
16cf8 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50  oVacuum ){.    P
16cf9 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 20 20 50 67  gno pgno;.    Pg
16cfa 6e 6f 20 69 47 75 65 73 73 20 3d 20 6f 76 66 6c  no iGuess = ovfl
16cfb 2b 31 3b 0a 20 20 20 20 75 38 20 65 54 79 70 65  +1;.    u8 eType
16cfc 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 50 54  ;..    while( PT
16cfd 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c  RMAP_ISPAGE(pBt,
16cfe 20 69 47 75 65 73 73 29 20 7c 7c 20 69 47 75 65   iGuess) || iGue
16cff 73 73 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45  ss==PENDING_BYTE
16d00 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20  _PAGE(pBt) ){.  
16d01 20 20 20 20 69 47 75 65 73 73 2b 2b 3b 0a 20 20      iGuess++;.  
16d02 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 69 47 75    }..    if( iGu
16d03 65 73 73 3c 3d 70 61 67 65 72 50 61 67 65 63 6f  ess<=pagerPageco
16d04 75 6e 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20  unt(pBt) ){.    
16d05 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74    rc = ptrmapGet
16d06 28 70 42 74 2c 20 69 47 75 65 73 73 2c 20 26 65  (pBt, iGuess, &e
16d07 54 79 70 65 2c 20 26 70 67 6e 6f 29 3b 0a 20 20  Type, &pgno);.  
16d08 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
16d09 54 45 5f 4f 4b 20 26 26 20 65 54 79 70 65 3d 3d  TE_OK && eType==
16d0a 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 32  PTRMAP_OVERFLOW2
16d0b 20 26 26 20 70 67 6e 6f 3d 3d 6f 76 66 6c 20 29   && pgno==ovfl )
16d0c 7b 0a 20 20 20 20 20 20 20 20 6e 65 78 74 20 3d  {.        next =
16d0d 20 69 47 75 65 73 73 3b 0a 20 20 20 20 20 20 20   iGuess;.       
16d0e 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e   rc = SQLITE_DON
16d0f 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  E;.      }.    }
16d10 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 61  .  }.#endif..  a
16d11 73 73 65 72 74 28 20 6e 65 78 74 3d 3d 30 20 7c  ssert( next==0 |
16d12 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  | rc==SQLITE_DON
16d13 45 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  E );.  if( rc==S
16d14 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
16d15 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67  rc = btreeGetPag
16d16 65 28 70 42 74 2c 20 6f 76 66 6c 2c 20 26 70 50  e(pBt, ovfl, &pP
16d17 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 61 73 73  age, 0);.    ass
16d18 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
16d19 4f 4b 20 7c 7c 20 70 50 61 67 65 3d 3d 30 20 29  OK || pPage==0 )
16d1a 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
16d1b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
16d1c 20 6e 65 78 74 20 3d 20 67 65 74 34 62 79 74 65   next = get4byte
16d1d 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29 3b 0a  (pPage->aData);.
16d1e 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 50      }.  }..  *pP
16d1f 67 6e 6f 4e 65 78 74 20 3d 20 6e 65 78 74 3b 0a  gnoNext = next;.
16d20 20 20 69 66 28 20 70 70 50 61 67 65 20 29 7b 0a    if( ppPage ){.
16d21 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50      *ppPage = pP
16d22 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  age;.  }else{.  
16d23 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
16d24 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  age);.  }.  retu
16d25 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 44  rn (rc==SQLITE_D
16d26 4f 4e 45 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20  ONE ? SQLITE_OK 
16d27 3a 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  : rc);.}../*.** 
16d28 43 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 61  Copy data from a
16d29 20 62 75 66 66 65 72 20 74 6f 20 61 20 70 61 67   buffer to a pag
16d2a 65 2c 20 6f 72 20 66 72 6f 6d 20 61 20 70 61 67  e, or from a pag
16d2b 65 20 74 6f 20 61 20 62 75 66 66 65 72 2e 0a 2a  e to a buffer..*
16d2c 2a 0a 2a 2a 20 70 50 61 79 6c 6f 61 64 20 69 73  *.** pPayload is
16d2d 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 64 61   a pointer to da
16d2e 74 61 20 73 74 6f 72 65 64 20 6f 6e 20 64 61 74  ta stored on dat
16d2f 61 62 61 73 65 20 70 61 67 65 20 70 44 62 50 61  abase page pDbPa
16d30 67 65 2e 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65  ge..** If argume
16d31 6e 74 20 65 4f 70 20 69 73 20 66 61 6c 73 65 2c  nt eOp is false,
16d32 20 74 68 65 6e 20 6e 42 79 74 65 20 62 79 74 65   then nByte byte
16d33 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f  s of data are co
16d34 70 69 65 64 0a 2a 2a 20 66 72 6f 6d 20 70 50 61  pied.** from pPa
16d35 79 6c 6f 61 64 20 74 6f 20 74 68 65 20 62 75 66  yload to the buf
16d36 66 65 72 20 70 6f 69 6e 74 65 64 20 61 74 20 62  fer pointed at b
16d37 79 20 70 42 75 66 2e 20 49 66 20 65 4f 70 20 69  y pBuf. If eOp i
16d38 73 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20  s true,.** then 
16d39 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
16d3a 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e  e() is called on
16d3b 20 70 44 62 50 61 67 65 20 61 6e 64 20 6e 42 79   pDbPage and nBy
16d3c 74 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 64  te bytes.** of d
16d3d 61 74 61 20 61 72 65 20 63 6f 70 69 65 64 20 66  ata are copied f
16d3e 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 70  rom the buffer p
16d3f 42 75 66 20 74 6f 20 70 50 61 79 6c 6f 61 64 2e  Buf to pPayload.
16d40 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  .**.** SQLITE_OK
16d41 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20   is returned on 
16d42 73 75 63 63 65 73 73 2c 20 6f 74 68 65 72 77 69  success, otherwi
16d43 73 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  se an error code
16d44 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
16d45 63 6f 70 79 50 61 79 6c 6f 61 64 28 0a 20 20 76  copyPayload(.  v
16d46 6f 69 64 20 2a 70 50 61 79 6c 6f 61 64 2c 20 20  oid *pPayload,  
16d47 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
16d48 74 65 72 20 74 6f 20 70 61 67 65 20 64 61 74 61  ter to page data
16d49 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66   */.  void *pBuf
16d4a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
16d4b 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75  /* Pointer to bu
16d4c 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  ffer */.  int nB
16d4d 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  yte,            
16d4e 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
16d4f 20 62 79 74 65 73 20 74 6f 20 63 6f 70 79 20 2a   bytes to copy *
16d50 2f 0a 20 20 69 6e 74 20 65 4f 70 2c 20 20 20 20  /.  int eOp,    
16d51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16d52 20 30 20 2d 3e 20 63 6f 70 79 20 66 72 6f 6d 20   0 -> copy from 
16d53 70 61 67 65 2c 20 31 20 2d 3e 20 63 6f 70 79 20  page, 1 -> copy 
16d54 74 6f 20 70 61 67 65 20 2a 2f 0a 20 20 44 62 50  to page */.  DbP
16d55 61 67 65 20 2a 70 44 62 50 61 67 65 20 20 20 20  age *pDbPage    
16d56 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63         /* Page c
16d57 6f 6e 74 61 69 6e 69 6e 67 20 70 50 61 79 6c 6f  ontaining pPaylo
16d58 61 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 65  ad */.){.  if( e
16d59 4f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 70  Op ){.    /* Cop
16d5a 79 20 64 61 74 61 20 66 72 6f 6d 20 62 75 66 66  y data from buff
16d5b 65 72 20 74 6f 20 70 61 67 65 20 28 61 20 77 72  er to page (a wr
16d5c 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a  ite operation) *
16d5d 2f 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73  /.    int rc = s
16d5e 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
16d5f 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69  (pDbPage);.    i
16d60 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
16d61 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
16d62 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d   rc;.    }.    m
16d63 65 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c 20  emcpy(pPayload, 
16d64 70 42 75 66 2c 20 6e 42 79 74 65 29 3b 0a 20 20  pBuf, nByte);.  
16d65 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f  }else{.    /* Co
16d66 70 79 20 64 61 74 61 20 66 72 6f 6d 20 70 61 67  py data from pag
16d67 65 20 74 6f 20 62 75 66 66 65 72 20 28 61 20 72  e to buffer (a r
16d68 65 61 64 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a  ead operation) *
16d69 2f 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 42 75  /.    memcpy(pBu
16d6a 66 2c 20 70 50 61 79 6c 6f 61 64 2c 20 6e 42 79  f, pPayload, nBy
16d6b 74 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  te);.  }.  retur
16d6c 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
16d6d 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
16d6e 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72  ion is used to r
16d6f 65 61 64 20 6f 72 20 6f 76 65 72 77 72 69 74 65  ead or overwrite
16d70 20 70 61 79 6c 6f 61 64 20 69 6e 66 6f 72 6d 61   payload informa
16d71 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20  tion.** for the 
16d72 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 70  entry that the p
16d73 43 75 72 20 63 75 72 73 6f 72 20 69 73 20 70 6f  Cur cursor is po
16d74 69 6e 74 69 6e 67 20 74 6f 2e 20 49 66 20 74 68  inting to. If th
16d75 65 20 65 4f 70 0a 2a 2a 20 70 61 72 61 6d 65 74  e eOp.** paramet
16d76 65 72 20 69 73 20 30 2c 20 74 68 69 73 20 69 73  er is 0, this is
16d77 20 61 20 72 65 61 64 20 6f 70 65 72 61 74 69 6f   a read operatio
16d78 6e 20 28 64 61 74 61 20 63 6f 70 69 65 64 20 69  n (data copied i
16d79 6e 74 6f 0a 2a 2a 20 62 75 66 66 65 72 20 70 42  nto.** buffer pB
16d7a 75 66 29 2e 20 49 66 20 69 74 20 69 73 20 6e 6f  uf). If it is no
16d7b 6e 2d 7a 65 72 6f 2c 20 61 20 77 72 69 74 65 20  n-zero, a write 
16d7c 28 64 61 74 61 20 63 6f 70 69 65 64 20 66 72 6f  (data copied fro
16d7d 6d 0a 2a 2a 20 62 75 66 66 65 72 20 70 42 75 66  m.** buffer pBuf
16d7e 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 6f 74 61 6c  )..**.** A total
16d7f 20 6f 66 20 22 61 6d 74 22 20 62 79 74 65 73 20   of "amt" bytes 
16d80 61 72 65 20 72 65 61 64 20 6f 72 20 77 72 69 74  are read or writ
16d81 74 65 6e 20 62 65 67 69 6e 6e 69 6e 67 20 61 74  ten beginning at
16d82 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 20 44 61   "offset"..** Da
16d83 74 61 20 69 73 20 72 65 61 64 20 74 6f 20 6f 72  ta is read to or
16d84 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72   from the buffer
16d85 20 70 42 75 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   pBuf..**.** The
16d86 20 63 6f 6e 74 65 6e 74 20 62 65 69 6e 67 20 72   content being r
16d87 65 61 64 20 6f 72 20 77 72 69 74 74 65 6e 20 6d  ead or written m
16d88 69 67 68 74 20 61 70 70 65 61 72 20 6f 6e 20 74  ight appear on t
16d89 68 65 20 6d 61 69 6e 20 70 61 67 65 0a 2a 2a 20  he main page.** 
16d8a 6f 72 20 62 65 20 73 63 61 74 74 65 72 65 64 20  or be scattered 
16d8b 6f 75 74 20 6f 6e 20 6d 75 6c 74 69 70 6c 65 20  out on multiple 
16d8c 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 0a  overflow pages..
16d8d 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 42 74 43  **.** If the BtC
16d8e 75 72 73 6f 72 2e 69 73 49 6e 63 72 62 6c 6f 62  ursor.isIncrblob
16d8f 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 20 73  Handle flag is s
16d90 65 74 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72  et, and the curr
16d91 65 6e 74 0a 2a 2a 20 63 75 72 73 6f 72 20 65 6e  ent.** cursor en
16d92 74 72 79 20 75 73 65 73 20 6f 6e 65 20 6f 72 20  try uses one or 
16d93 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61  more overflow pa
16d94 67 65 73 2c 20 74 68 69 73 20 66 75 6e 63 74 69  ges, this functi
16d95 6f 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 73 20  on.** allocates 
16d96 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20 6c 61  space for and la
16d97 7a 69 6c 79 20 70 6f 70 6c 75 61 74 65 73 20 74  zily popluates t
16d98 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  he overflow page
16d99 2d 6c 69 73 74 20 0a 2a 2a 20 63 61 63 68 65 20  -list .** cache 
16d9a 61 72 72 61 79 20 28 42 74 43 75 72 73 6f 72 2e  array (BtCursor.
16d9b 61 4f 76 65 72 66 6c 6f 77 29 2e 20 53 75 62 73  aOverflow). Subs
16d9c 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 75 73 65  equent calls use
16d9d 20 74 68 69 73 0a 2a 2a 20 63 61 63 68 65 20 74   this.** cache t
16d9e 6f 20 6d 61 6b 65 20 73 65 65 6b 69 6e 67 20 74  o make seeking t
16d9f 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 6f  o the supplied o
16da0 66 66 73 65 74 20 6d 6f 72 65 20 65 66 66 69 63  ffset more effic
16da1 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65  ient..**.** Once
16da2 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   an overflow pag
16da3 65 2d 6c 69 73 74 20 63 61 63 68 65 20 68 61 73  e-list cache has
16da4 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c   been allocated,
16da5 20 69 74 20 6d 61 79 20 62 65 0a 2a 2a 20 69 6e   it may be.** in
16da6 76 61 6c 69 64 61 74 65 64 20 69 66 20 73 6f 6d  validated if som
16da7 65 20 6f 74 68 65 72 20 63 75 72 73 6f 72 20 77  e other cursor w
16da8 72 69 74 65 73 20 74 6f 20 74 68 65 20 73 61 6d  rites to the sam
16da9 65 20 74 61 62 6c 65 2c 20 6f 72 20 69 66 0a 2a  e table, or if.*
16daa 2a 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  * the cursor is 
16dab 6d 6f 76 65 64 20 74 6f 20 61 20 64 69 66 66 65  moved to a diffe
16dac 72 65 6e 74 20 72 6f 77 2e 20 41 64 64 69 74 69  rent row. Additi
16dad 6f 6e 61 6c 6c 79 2c 20 69 6e 20 61 75 74 6f 2d  onally, in auto-
16dae 76 61 63 75 75 6d 0a 2a 2a 20 6d 6f 64 65 2c 20  vacuum.** mode, 
16daf 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 76  the following ev
16db0 65 6e 74 73 20 6d 61 79 20 69 6e 76 61 6c 69 64  ents may invalid
16db1 61 74 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20  ate an overflow 
16db2 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2e  page-list cache.
16db3 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6e  .**.**   * An in
16db4 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d  cremental vacuum
16db5 2c 0a 2a 2a 20 20 20 2a 20 41 20 63 6f 6d 6d 69  ,.**   * A commi
16db6 74 20 69 6e 20 61 75 74 6f 5f 76 61 63 75 75 6d  t in auto_vacuum
16db7 3d 22 66 75 6c 6c 22 20 6d 6f 64 65 2c 0a 2a 2a  ="full" mode,.**
16db8 20 20 20 2a 20 43 72 65 61 74 69 6e 67 20 61 20     * Creating a 
16db9 74 61 62 6c 65 20 28 6d 61 79 20 72 65 71 75 69  table (may requi
16dba 72 65 20 6d 6f 76 69 6e 67 20 61 6e 20 6f 76 65  re moving an ove
16dbb 72 66 6c 6f 77 20 70 61 67 65 29 2e 0a 2a 2f 0a  rflow page)..*/.
16dbc 73 74 61 74 69 63 20 69 6e 74 20 61 63 63 65 73  static int acces
16dbd 73 50 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 75  sPayload(.  BtCu
16dbe 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20  rsor *pCur,     
16dbf 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74   /* Cursor point
16dc0 69 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 6f 20  ing to entry to 
16dc1 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 75  read from */.  u
16dc2 33 32 20 6f 66 66 73 65 74 2c 20 20 20 20 20 20  32 offset,      
16dc3 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61      /* Begin rea
16dc4 64 69 6e 67 20 74 68 69 73 20 66 61 72 20 69 6e  ding this far in
16dc5 74 6f 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20  to payload */.  
16dc6 75 33 32 20 61 6d 74 2c 20 20 20 20 20 20 20 20  u32 amt,        
16dc7 20 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 69       /* Read thi
16dc8 73 20 6d 61 6e 79 20 62 79 74 65 73 20 2a 2f 0a  s many bytes */.
16dc9 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
16dca 2a 70 42 75 66 2c 20 2f 2a 20 57 72 69 74 65 20  *pBuf, /* Write 
16dcb 74 68 65 20 62 79 74 65 73 20 69 6e 74 6f 20 74  the bytes into t
16dcc 68 69 73 20 62 75 66 66 65 72 20 2a 2f 20 0a 20  his buffer */ . 
16dcd 20 69 6e 74 20 65 4f 70 20 20 20 20 20 20 20 20   int eOp        
16dce 20 20 20 20 20 20 2f 2a 20 7a 65 72 6f 20 74 6f        /* zero to
16dcf 20 72 65 61 64 2e 20 6e 6f 6e 2d 7a 65 72 6f 20   read. non-zero 
16dd0 74 6f 20 77 72 69 74 65 2e 20 2a 2f 0a 29 7b 0a  to write. */.){.
16dd1 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
16dd2 2a 61 50 61 79 6c 6f 61 64 3b 0a 20 20 69 6e 74  *aPayload;.  int
16dd3 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
16dd4 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20 20 69  .  u32 nKey;.  i
16dd5 6e 74 20 69 49 64 78 20 3d 20 30 3b 0a 20 20 4d  nt iIdx = 0;.  M
16dd6 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20  emPage *pPage = 
16dd7 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
16dd8 72 2d 3e 69 50 61 67 65 5d 3b 20 2f 2a 20 42 74  r->iPage]; /* Bt
16dd9 72 65 65 20 70 61 67 65 20 6f 66 20 63 75 72 72  ree page of curr
16dda 65 6e 74 20 65 6e 74 72 79 20 2a 2f 0a 20 20 42  ent entry */.  B
16ddb 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
16ddc 43 75 72 2d 3e 70 42 74 3b 20 20 20 20 20 20 20  Cur->pBt;       
16ddd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 74             /* Bt
16dde 72 65 65 20 74 68 69 73 20 63 75 72 73 6f 72 20  ree this cursor 
16ddf 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f 0a 0a 20  belongs to */.. 
16de0 20 61 73 73 65 72 74 28 20 70 50 61 67 65 20 29   assert( pPage )
16de1 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
16de2 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
16de3 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65  _VALID );.  asse
16de4 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  rt( pCur->aiIdx[
16de5 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50 61  pCur->iPage]<pPa
16de6 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61  ge->nCell );.  a
16de7 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
16de8 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
16de9 0a 0a 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28  ..  getCellInfo(
16dea 70 43 75 72 29 3b 0a 20 20 61 50 61 79 6c 6f 61  pCur);.  aPayloa
16deb 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 70  d = pCur->info.p
16dec 43 65 6c 6c 20 2b 20 70 43 75 72 2d 3e 69 6e 66  Cell + pCur->inf
16ded 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 6e 4b 65  o.nHeader;.  nKe
16dee 79 20 3d 20 28 70 50 61 67 65 2d 3e 69 6e 74 4b  y = (pPage->intK
16def 65 79 20 3f 20 30 20 3a 20 28 69 6e 74 29 70 43  ey ? 0 : (int)pC
16df0 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 29 3b 0a  ur->info.nKey);.
16df1 0a 20 20 69 66 28 20 4e 45 56 45 52 28 6f 66 66  .  if( NEVER(off
16df2 73 65 74 2b 61 6d 74 20 3e 20 6e 4b 65 79 2b 70  set+amt > nKey+p
16df3 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 74 61 29  Cur->info.nData)
16df4 20 0a 20 20 20 7c 7c 20 26 61 50 61 79 6c 6f 61   .   || &aPayloa
16df5 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f  d[pCur->info.nLo
16df6 63 61 6c 5d 20 3e 20 26 70 50 61 67 65 2d 3e 61  cal] > &pPage->a
16df7 44 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c 65  Data[pBt->usable
16df8 53 69 7a 65 5d 0a 20 20 29 7b 0a 20 20 20 20 2f  Size].  ){.    /
16df9 2a 20 54 72 79 69 6e 67 20 74 6f 20 72 65 61 64  * Trying to read
16dfa 20 6f 72 20 77 72 69 74 65 20 70 61 73 74 20 74   or write past t
16dfb 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 64 61  he end of the da
16dfc 74 61 20 69 73 20 61 6e 20 65 72 72 6f 72 20 2a  ta is an error *
16dfd 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  /.    return SQL
16dfe 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
16dff 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63  ;.  }..  /* Chec
16e00 6b 20 69 66 20 64 61 74 61 20 6d 75 73 74 20 62  k if data must b
16e01 65 20 72 65 61 64 2f 77 72 69 74 74 65 6e 20 74  e read/written t
16e02 6f 2f 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65  o/from the btree
16e03 20 70 61 67 65 20 69 74 73 65 6c 66 2e 20 2a 2f   page itself. */
16e04 0a 20 20 69 66 28 20 6f 66 66 73 65 74 3c 70 43  .  if( offset<pC
16e05 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20  ur->info.nLocal 
16e06 29 7b 0a 20 20 20 20 69 6e 74 20 61 20 3d 20 61  ){.    int a = a
16e07 6d 74 3b 0a 20 20 20 20 69 66 28 20 61 2b 6f 66  mt;.    if( a+of
16e08 66 73 65 74 3e 70 43 75 72 2d 3e 69 6e 66 6f 2e  fset>pCur->info.
16e09 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20  nLocal ){.      
16e0a 61 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  a = pCur->info.n
16e0b 4c 6f 63 61 6c 20 2d 20 6f 66 66 73 65 74 3b 0a  Local - offset;.
16e0c 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 63      }.    rc = c
16e0d 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50 61 79  opyPayload(&aPay
16e0e 6c 6f 61 64 5b 6f 66 66 73 65 74 5d 2c 20 70 42  load[offset], pB
16e0f 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 50 61 67  uf, a, eOp, pPag
16e10 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  e->pDbPage);.   
16e11 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20   offset = 0;.   
16e12 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20 20   pBuf += a;.    
16e13 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 7d 65 6c 73  amt -= a;.  }els
16e14 65 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 2d 3d  e{.    offset -=
16e15 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63   pCur->info.nLoc
16e16 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72  al;.  }..  if( r
16e17 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
16e18 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 63 6f 6e  amt>0 ){.    con
16e19 73 74 20 75 33 32 20 6f 76 66 6c 53 69 7a 65 20  st u32 ovflSize 
16e1a 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  = pBt->usableSiz
16e1b 65 20 2d 20 34 3b 20 20 2f 2a 20 42 79 74 65 73  e - 4;  /* Bytes
16e1c 20 63 6f 6e 74 65 6e 74 20 70 65 72 20 6f 76 66   content per ovf
16e1d 6c 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 50 67  l page */.    Pg
16e1e 6e 6f 20 6e 65 78 74 50 61 67 65 3b 0a 0a 20 20  no nextPage;..  
16e1f 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67 65 74    nextPage = get
16e20 34 62 79 74 65 28 26 61 50 61 79 6c 6f 61 64 5b  4byte(&aPayload[
16e21 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61  pCur->info.nLoca
16e22 6c 5d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  l]);..#ifndef SQ
16e23 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
16e24 4f 42 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  OB.    /* If the
16e25 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c   isIncrblobHandl
16e26 65 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e  e flag is set an
16e27 64 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 61  d the BtCursor.a
16e28 4f 76 65 72 66 6c 6f 77 5b 5d 0a 20 20 20 20 2a  Overflow[].    *
16e29 2a 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 61  * has not been a
16e2a 6c 6c 6f 63 61 74 65 64 2c 20 61 6c 6c 6f 63 61  llocated, alloca
16e2b 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 61  te it now. The a
16e2c 72 72 61 79 20 69 73 20 73 69 7a 65 64 20 61 74  rray is sized at
16e2d 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6e 74 72  .    ** one entr
16e2e 79 20 66 6f 72 20 65 61 63 68 20 6f 76 65 72 66  y for each overf
16e2f 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 20  low page in the 
16e30 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20  overflow chain. 
16e31 54 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 20  The.    ** page 
16e32 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 66 69  number of the fi
16e33 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  rst overflow pag
16e34 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61  e is stored in a
16e35 4f 76 65 72 66 6c 6f 77 5b 30 5d 2c 0a 20 20 20  Overflow[0],.   
16e36 20 2a 2a 20 65 74 63 2e 20 41 20 76 61 6c 75 65   ** etc. A value
16e37 20 6f 66 20 30 20 69 6e 20 74 68 65 20 61 4f 76   of 0 in the aOv
16e38 65 72 66 6c 6f 77 5b 5d 20 61 72 72 61 79 20 6d  erflow[] array m
16e39 65 61 6e 73 20 22 6e 6f 74 20 79 65 74 20 6b 6e  eans "not yet kn
16e3a 6f 77 6e 22 0a 20 20 20 20 2a 2a 20 28 74 68 65  own".    ** (the
16e3b 20 63 61 63 68 65 20 69 73 20 6c 61 7a 69 6c 79   cache is lazily
16e3c 20 70 6f 70 75 6c 61 74 65 64 29 2e 0a 20 20 20   populated)..   
16e3d 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43 75 72   */.    if( pCur
16e3e 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64  ->isIncrblobHand
16e3f 6c 65 20 26 26 20 21 70 43 75 72 2d 3e 61 4f 76  le && !pCur->aOv
16e40 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20  erflow ){.      
16e41 69 6e 74 20 6e 4f 76 66 6c 20 3d 20 28 70 43 75  int nOvfl = (pCu
16e42 72 2d 3e 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64  r->info.nPayload
16e43 2d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63  -pCur->info.nLoc
16e44 61 6c 2b 6f 76 66 6c 53 69 7a 65 2d 31 29 2f 6f  al+ovflSize-1)/o
16e45 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20 20 70  vflSize;.      p
16e46 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d  Cur->aOverflow =
16e47 20 28 50 67 6e 6f 20 2a 29 73 71 6c 69 74 65 33   (Pgno *)sqlite3
16e48 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f  MallocZero(sizeo
16e49 66 28 50 67 6e 6f 29 2a 6e 4f 76 66 6c 29 3b 0a  f(Pgno)*nOvfl);.
16e4a 20 20 20 20 20 20 2f 2a 20 6e 4f 76 66 6c 20 69        /* nOvfl i
16e4b 73 20 61 6c 77 61 79 73 20 70 6f 73 69 74 69 76  s always positiv
16e4c 65 2e 20 20 49 66 20 69 74 20 77 65 72 65 20 7a  e.  If it were z
16e4d 65 72 6f 2c 20 66 65 74 63 68 50 61 79 6c 6f 61  ero, fetchPayloa
16e4e 64 20 77 6f 75 6c 64 20 68 61 76 65 0a 20 20 20  d would have.   
16e4f 20 20 20 2a 2a 20 62 65 65 6e 20 75 73 65 64 20     ** been used 
16e50 69 6e 73 74 65 61 64 20 6f 66 20 74 68 69 73 20  instead of this 
16e51 72 6f 75 74 69 6e 65 2e 20 2a 2f 0a 20 20 20 20  routine. */.    
16e52 20 20 69 66 28 20 41 4c 57 41 59 53 28 6e 4f 76    if( ALWAYS(nOv
16e53 66 6c 29 20 26 26 20 21 70 43 75 72 2d 3e 61 4f  fl) && !pCur->aO
16e54 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20  verflow ){.     
16e55 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
16e56 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  OMEM;.      }.  
16e57 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74    }..    /* If t
16e58 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  he overflow page
16e59 2d 6c 69 73 74 20 63 61 63 68 65 20 68 61 73 20  -list cache has 
16e5a 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 61  been allocated a
16e5b 6e 64 20 74 68 65 0a 20 20 20 20 2a 2a 20 65 6e  nd the.    ** en
16e5c 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 72 73  try for the firs
16e5d 74 20 72 65 71 75 69 72 65 64 20 6f 76 65 72 66  t required overf
16e5e 6c 6f 77 20 70 61 67 65 20 69 73 20 76 61 6c 69  low page is vali
16e5f 64 2c 20 73 6b 69 70 0a 20 20 20 20 2a 2a 20 64  d, skip.    ** d
16e60 69 72 65 63 74 6c 79 20 74 6f 20 69 74 2e 0a 20  irectly to it.. 
16e61 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43     */.    if( pC
16e62 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 26 26  ur->aOverflow &&
16e63 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77   pCur->aOverflow
16e64 5b 6f 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65  [offset/ovflSize
16e65 5d 20 29 7b 0a 20 20 20 20 20 20 69 49 64 78 20  ] ){.      iIdx 
16e66 3d 20 28 6f 66 66 73 65 74 2f 6f 76 66 6c 53 69  = (offset/ovflSi
16e67 7a 65 29 3b 0a 20 20 20 20 20 20 6e 65 78 74 50  ze);.      nextP
16e68 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65  age = pCur->aOve
16e69 72 66 6c 6f 77 5b 69 49 64 78 5d 3b 0a 20 20 20  rflow[iIdx];.   
16e6a 20 20 20 6f 66 66 73 65 74 20 3d 20 28 6f 66 66     offset = (off
16e6b 73 65 74 25 6f 76 66 6c 53 69 7a 65 29 3b 0a 20  set%ovflSize);. 
16e6c 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
16e6d 20 66 6f 72 28 20 3b 20 72 63 3d 3d 53 51 4c 49   for( ; rc==SQLI
16e6e 54 45 5f 4f 4b 20 26 26 20 61 6d 74 3e 30 20 26  TE_OK && amt>0 &
16e6f 26 20 6e 65 78 74 50 61 67 65 3b 20 69 49 64 78  & nextPage; iIdx
16e70 2b 2b 29 7b 0a 0a 23 69 66 6e 64 65 66 20 53 51  ++){..#ifndef SQ
16e71 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
16e72 4f 42 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 72  OB.      /* If r
16e73 65 71 75 69 72 65 64 2c 20 70 6f 70 75 6c 61 74  equired, populat
16e74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70  e the overflow p
16e75 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2e 20  age-list cache. 
16e76 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 43 75  */.      if( pCu
16e77 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 29 7b 0a  r->aOverflow ){.
16e78 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 21          assert(!
16e79 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b  pCur->aOverflow[
16e7a 69 49 64 78 5d 20 7c 7c 20 70 43 75 72 2d 3e 61  iIdx] || pCur->a
16e7b 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78 5d 3d 3d  Overflow[iIdx]==
16e7c 6e 65 78 74 50 61 67 65 29 3b 0a 20 20 20 20 20  nextPage);.     
16e7d 20 20 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c     pCur->aOverfl
16e7e 6f 77 5b 69 49 64 78 5d 20 3d 20 6e 65 78 74 50  ow[iIdx] = nextP
16e7f 61 67 65 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  age;.      }.#en
16e80 64 69 66 0a 0a 20 20 20 20 20 20 69 66 28 20 6f  dif..      if( o
16e81 66 66 73 65 74 3e 3d 6f 76 66 6c 53 69 7a 65 20  ffset>=ovflSize 
16e82 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
16e83 65 20 6f 6e 6c 79 20 72 65 61 73 6f 6e 20 74 6f  e only reason to
16e84 20 72 65 61 64 20 74 68 69 73 20 70 61 67 65 20   read this page 
16e85 69 73 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65  is to obtain the
16e86 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a   page.        **
16e87 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
16e88 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65  next page in the
16e89 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e   overflow chain.
16e8a 20 54 68 65 20 70 61 67 65 0a 20 20 20 20 20 20   The page.      
16e8b 20 20 2a 2a 20 64 61 74 61 20 69 73 20 6e 6f 74    ** data is not
16e8c 20 72 65 71 75 69 72 65 64 2e 20 53 6f 20 66 69   required. So fi
16e8d 72 73 74 20 74 72 79 20 74 6f 20 6c 6f 6f 6b 75  rst try to looku
16e8e 70 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 0a 20  p the overflow. 
16e8f 20 20 20 20 20 20 20 2a 2a 20 70 61 67 65 2d 6c         ** page-l
16e90 69 73 74 20 63 61 63 68 65 2c 20 69 66 20 61 6e  ist cache, if an
16e91 79 2c 20 74 68 65 6e 20 66 61 6c 6c 20 62 61 63  y, then fall bac
16e92 6b 20 74 6f 20 74 68 65 20 67 65 74 4f 76 65 72  k to the getOver
16e93 66 6c 6f 77 50 61 67 65 28 29 0a 20 20 20 20 20  flowPage().     
16e94 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a     ** function..
16e95 20 20 20 20 20 20 20 20 2a 2f 0a 23 69 66 6e 64          */.#ifnd
16e96 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
16e97 4e 43 52 42 4c 4f 42 0a 20 20 20 20 20 20 20 20  NCRBLOB.        
16e98 69 66 28 20 70 43 75 72 2d 3e 61 4f 76 65 72 66  if( pCur->aOverf
16e99 6c 6f 77 20 26 26 20 70 43 75 72 2d 3e 61 4f 76  low && pCur->aOv
16e9a 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 20 29  erflow[iIdx+1] )
16e9b 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78 74  {.          next
16e9c 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 4f 76  Page = pCur->aOv
16e9d 65 72 66 6c 6f 77 5b 69 49 64 78 2b 31 5d 3b 0a  erflow[iIdx+1];.
16e9e 20 20 20 20 20 20 20 20 7d 20 65 6c 73 65 20 0a          } else .
16e9f 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 20  #endif.         
16ea0 20 72 63 20 3d 20 67 65 74 4f 76 65 72 66 6c 6f   rc = getOverflo
16ea1 77 50 61 67 65 28 70 42 74 2c 20 6e 65 78 74 50  wPage(pBt, nextP
16ea2 61 67 65 2c 20 30 2c 20 26 6e 65 78 74 50 61 67  age, 0, &nextPag
16ea3 65 29 3b 0a 20 20 20 20 20 20 20 20 6f 66 66 73  e);.        offs
16ea4 65 74 20 2d 3d 20 6f 76 66 6c 53 69 7a 65 3b 0a  et -= ovflSize;.
16ea5 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
16ea6 20 20 20 20 20 2f 2a 20 4e 65 65 64 20 74 6f 20       /* Need to 
16ea7 72 65 61 64 20 74 68 69 73 20 70 61 67 65 20 70  read this page p
16ea8 72 6f 70 65 72 6c 79 2e 20 49 74 20 63 6f 6e 74  roperly. It cont
16ea9 61 69 6e 73 20 73 6f 6d 65 20 6f 66 20 74 68 65  ains some of the
16eaa 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 61 6e 67  .        ** rang
16eab 65 20 6f 66 20 64 61 74 61 20 74 68 61 74 20 69  e of data that i
16eac 73 20 62 65 69 6e 67 20 72 65 61 64 20 28 65 4f  s being read (eO
16ead 70 3d 3d 30 29 20 6f 72 20 77 72 69 74 74 65 6e  p==0) or written
16eae 20 28 65 4f 70 21 3d 30 29 2e 0a 20 20 20 20 20   (eOp!=0)..     
16eaf 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 44 62     */.        Db
16eb0 50 61 67 65 20 2a 70 44 62 50 61 67 65 3b 0a 20  Page *pDbPage;. 
16eb1 20 20 20 20 20 20 20 69 6e 74 20 61 20 3d 20 61         int a = a
16eb2 6d 74 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  mt;.        rc =
16eb3 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
16eb4 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 6e 65  (pBt->pPager, ne
16eb5 78 74 50 61 67 65 2c 20 26 70 44 62 50 61 67 65  xtPage, &pDbPage
16eb6 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
16eb7 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
16eb8 20 20 20 20 20 20 20 20 20 20 61 50 61 79 6c 6f            aPaylo
16eb9 61 64 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  ad = sqlite3Page
16eba 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65  rGetData(pDbPage
16ebb 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 65 78  );.          nex
16ebc 74 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65  tPage = get4byte
16ebd 28 61 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20  (aPayload);.    
16ebe 20 20 20 20 20 20 69 66 28 20 61 20 2b 20 6f 66        if( a + of
16ebf 66 73 65 74 20 3e 20 6f 76 66 6c 53 69 7a 65 20  fset > ovflSize 
16ec0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  ){.            a
16ec1 20 3d 20 6f 76 66 6c 53 69 7a 65 20 2d 20 6f 66   = ovflSize - of
16ec2 66 73 65 74 3b 0a 20 20 20 20 20 20 20 20 20 20  fset;.          
16ec3 7d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  }.          rc =
16ec4 20 63 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50   copyPayload(&aP
16ec5 61 79 6c 6f 61 64 5b 6f 66 66 73 65 74 2b 34 5d  ayload[offset+4]
16ec6 2c 20 70 42 75 66 2c 20 61 2c 20 65 4f 70 2c 20  , pBuf, a, eOp, 
16ec7 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
16ec8 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
16ec9 55 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a  Unref(pDbPage);.
16eca 20 20 20 20 20 20 20 20 20 20 6f 66 66 73 65 74            offset
16ecb 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
16ecc 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 20 20 20 20  amt -= a;.      
16ecd 20 20 20 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20      pBuf += a;. 
16ece 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
16ecf 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66  .    }.  }..  if
16ed0 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
16ed1 26 26 20 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20  && amt>0 ){.    
16ed2 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
16ed3 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a  RRUPT_BKPT;.  }.
16ed4 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
16ed5 2f 2a 0a 2a 2a 20 52 65 61 64 20 70 61 72 74 20  /*.** Read part 
16ed6 6f 66 20 74 68 65 20 6b 65 79 20 61 73 73 6f 63  of the key assoc
16ed7 69 61 74 65 64 20 77 69 74 68 20 63 75 72 73 6f  iated with curso
16ed8 72 20 70 43 75 72 2e 20 20 45 78 61 63 74 6c 79  r pCur.  Exactly
16ed9 0a 2a 2a 20 22 61 6d 74 22 20 62 79 74 65 73 20  .** "amt" bytes 
16eda 77 69 6c 6c 20 62 65 20 74 72 61 6e 73 66 65 72  will be transfer
16edb 65 64 20 69 6e 74 6f 20 70 42 75 66 5b 5d 2e 20  ed into pBuf[]. 
16edc 20 54 68 65 20 74 72 61 6e 73 66 65 72 0a 2a 2a   The transfer.**
16edd 20 62 65 67 69 6e 73 20 61 74 20 22 6f 66 66 73   begins at "offs
16ede 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  et"..**.** The c
16edf 61 6c 6c 65 72 20 6d 75 73 74 20 65 6e 73 75 72  aller must ensur
16ee0 65 20 74 68 61 74 20 70 43 75 72 20 69 73 20 70  e that pCur is p
16ee1 6f 69 6e 74 69 6e 67 20 74 6f 20 61 20 76 61 6c  ointing to a val
16ee2 69 64 20 72 6f 77 0a 2a 2a 20 69 6e 20 74 68 65  id row.** in the
16ee3 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 52 65   table..**.** Re
16ee4 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f  turn SQLITE_OK o
16ee5 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20  n success or an 
16ee6 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e  error code if an
16ee7 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77  ything goes.** w
16ee8 72 6f 6e 67 2e 20 20 41 6e 20 65 72 72 6f 72 20  rong.  An error 
16ee9 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 22  is returned if "
16eea 6f 66 66 73 65 74 2b 61 6d 74 22 20 69 73 20 6c  offset+amt" is l
16eeb 61 72 67 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68  arger than.** th
16eec 65 20 61 76 61 69 6c 61 62 6c 65 20 70 61 79 6c  e available payl
16eed 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  oad..*/.SQLITE_P
16eee 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
16eef 65 33 42 74 72 65 65 4b 65 79 28 42 74 43 75 72  e3BtreeKey(BtCur
16ef0 73 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 6f  sor *pCur, u32 o
16ef1 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20  ffset, u32 amt, 
16ef2 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20 20 61  void *pBuf){.  a
16ef3 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
16ef4 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
16ef5 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
16ef6 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
16ef7 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72  VALID );.  asser
16ef8 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d  t( pCur->iPage>=
16ef9 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67  0 && pCur->apPag
16efa 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 29  e[pCur->iPage] )
16efb 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
16efc 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
16efd 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50 61 67  age]<pCur->apPag
16efe 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e  e[pCur->iPage]->
16eff 6e 43 65 6c 6c 20 29 3b 0a 20 20 72 65 74 75 72  nCell );.  retur
16f00 6e 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28  n accessPayload(
16f01 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d  pCur, offset, am
16f02 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61  t, (unsigned cha
16f03 72 2a 29 70 42 75 66 2c 20 30 29 3b 0a 7d 0a 0a  r*)pBuf, 0);.}..
16f04 2f 2a 0a 2a 2a 20 52 65 61 64 20 70 61 72 74 20  /*.** Read part 
16f05 6f 66 20 74 68 65 20 64 61 74 61 20 61 73 73 6f  of the data asso
16f06 63 69 61 74 65 64 20 77 69 74 68 20 63 75 72 73  ciated with curs
16f07 6f 72 20 70 43 75 72 2e 20 20 45 78 61 63 74 6c  or pCur.  Exactl
16f08 79 0a 2a 2a 20 22 61 6d 74 22 20 62 79 74 65 73  y.** "amt" bytes
16f09 20 77 69 6c 6c 20 62 65 20 74 72 61 6e 73 66 65   will be transfe
16f0a 72 65 64 20 69 6e 74 6f 20 70 42 75 66 5b 5d 2e  red into pBuf[].
16f0b 20 20 54 68 65 20 74 72 61 6e 73 66 65 72 0a 2a    The transfer.*
16f0c 2a 20 62 65 67 69 6e 73 20 61 74 20 22 6f 66 66  * begins at "off
16f0d 73 65 74 22 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  set"..**.** Retu
16f0e 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20  rn SQLITE_OK on 
16f0f 73 75 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72  success or an er
16f10 72 6f 72 20 63 6f 64 65 20 69 66 20 61 6e 79 74  ror code if anyt
16f11 68 69 6e 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f  hing goes.** wro
16f12 6e 67 2e 20 20 41 6e 20 65 72 72 6f 72 20 69 73  ng.  An error is
16f13 20 72 65 74 75 72 6e 65 64 20 69 66 20 22 6f 66   returned if "of
16f14 66 73 65 74 2b 61 6d 74 22 20 69 73 20 6c 61 72  fset+amt" is lar
16f15 67 65 72 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20  ger than.** the 
16f16 61 76 61 69 6c 61 62 6c 65 20 70 61 79 6c 6f 61  available payloa
16f17 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
16f18 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
16f19 42 74 72 65 65 44 61 74 61 28 42 74 43 75 72 73  BtreeData(BtCurs
16f1a 6f 72 20 2a 70 43 75 72 2c 20 75 33 32 20 6f 66  or *pCur, u32 of
16f1b 66 73 65 74 2c 20 75 33 32 20 61 6d 74 2c 20 76  fset, u32 amt, v
16f1c 6f 69 64 20 2a 70 42 75 66 29 7b 0a 20 20 69 6e  oid *pBuf){.  in
16f1d 74 20 72 63 3b 0a 0a 23 69 66 6e 64 65 66 20 53  t rc;..#ifndef S
16f1e 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
16f1f 4c 4f 42 0a 20 20 69 66 20 28 20 70 43 75 72 2d  LOB.  if ( pCur-
16f20 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
16f21 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 72  INVALID ){.    r
16f22 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f  eturn SQLITE_ABO
16f23 52 54 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  RT;.  }.#endif..
16f24 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
16f25 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
16f26 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f   );.  rc = resto
16f27 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  reCursorPosition
16f28 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63  (pCur);.  if( rc
16f29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
16f2a 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
16f2b 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
16f2c 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 61 73 73  VALID );.    ass
16f2d 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67 65  ert( pCur->iPage
16f2e 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50  >=0 && pCur->apP
16f2f 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
16f30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
16f31 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
16f32 2d 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e 61  ->iPage]<pCur->a
16f33 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
16f34 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20  e]->nCell );.   
16f35 20 72 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c   rc = accessPayl
16f36 6f 61 64 28 70 43 75 72 2c 20 6f 66 66 73 65 74  oad(pCur, offset
16f37 2c 20 61 6d 74 2c 20 70 42 75 66 2c 20 30 29 3b  , amt, pBuf, 0);
16f38 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
16f39 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
16f3a 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70  n a pointer to p
16f3b 61 79 6c 6f 61 64 20 69 6e 66 6f 72 6d 61 74 69  ayload informati
16f3c 6f 6e 20 66 72 6f 6d 20 74 68 65 20 65 6e 74 72  on from the entr
16f3d 79 20 74 68 61 74 20 74 68 65 20 0a 2a 2a 20 70  y that the .** p
16f3e 43 75 72 20 63 75 72 73 6f 72 20 69 73 20 70 6f  Cur cursor is po
16f3f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20  inting to.  The 
16f40 70 6f 69 6e 74 65 72 20 69 73 20 74 6f 20 74 68  pointer is to th
16f41 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 0a 2a  e beginning of.*
16f42 2a 20 74 68 65 20 6b 65 79 20 69 66 20 73 6b 69  * the key if ski
16f43 70 4b 65 79 3d 3d 30 20 61 6e 64 20 69 74 20 70  pKey==0 and it p
16f44 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 62 65 67  oints to the beg
16f45 69 6e 6e 69 6e 67 20 6f 66 20 64 61 74 61 20 69  inning of data i
16f46 66 0a 2a 2a 20 73 6b 69 70 4b 65 79 3d 3d 31 2e  f.** skipKey==1.
16f47 20 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20    The number of 
16f48 62 79 74 65 73 20 6f 66 20 61 76 61 69 6c 61 62  bytes of availab
16f49 6c 65 20 6b 65 79 2f 64 61 74 61 20 69 73 20 77  le key/data is w
16f4a 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f 20 2a  ritten.** into *
16f4b 70 41 6d 74 2e 20 20 49 66 20 2a 70 41 6d 74 3d  pAmt.  If *pAmt=
16f4c 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c  =0, then the val
16f4d 75 65 20 72 65 74 75 72 6e 65 64 20 77 69 6c 6c  ue returned will
16f4e 20 6e 6f 74 20 62 65 0a 2a 2a 20 61 20 76 61 6c   not be.** a val
16f4f 69 64 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a  id pointer..**.*
16f50 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
16f51 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  s an optimizatio
16f52 6e 2e 20 20 49 74 20 69 73 20 63 6f 6d 6d 6f 6e  n.  It is common
16f53 20 66 6f 72 20 74 68 65 20 65 6e 74 69 72 65 20   for the entire 
16f54 6b 65 79 0a 2a 2a 20 61 6e 64 20 64 61 74 61 20  key.** and data 
16f55 74 6f 20 66 69 74 20 6f 6e 20 74 68 65 20 6c 6f  to fit on the lo
16f56 63 61 6c 20 70 61 67 65 20 61 6e 64 20 66 6f 72  cal page and for
16f57 20 74 68 65 72 65 20 74 6f 20 62 65 20 6e 6f 20   there to be no 
16f58 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 65  overflow.** page
16f59 73 2e 20 20 57 68 65 6e 20 74 68 61 74 20 69 73  s.  When that is
16f5a 20 73 6f 2c 20 74 68 69 73 20 72 6f 75 74 69 6e   so, this routin
16f5b 65 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  e can be used to
16f5c 20 61 63 63 65 73 73 20 74 68 65 0a 2a 2a 20 6b   access the.** k
16f5d 65 79 20 61 6e 64 20 64 61 74 61 20 77 69 74 68  ey and data with
16f5e 6f 75 74 20 6d 61 6b 69 6e 67 20 61 20 63 6f 70  out making a cop
16f5f 79 2e 20 20 49 66 20 74 68 65 20 6b 65 79 20 61  y.  If the key a
16f60 6e 64 2f 6f 72 20 64 61 74 61 20 73 70 69 6c 6c  nd/or data spill
16f61 73 0a 2a 2a 20 6f 6e 74 6f 20 6f 76 65 72 66 6c  s.** onto overfl
16f62 6f 77 20 70 61 67 65 73 2c 20 74 68 65 6e 20 61  ow pages, then a
16f63 63 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20 6d  ccessPayload() m
16f64 75 73 74 20 62 65 20 75 73 65 64 20 74 6f 20 72  ust be used to r
16f65 65 61 73 73 65 6d 62 6c 65 0a 2a 2a 20 74 68 65  eassemble.** the
16f66 20 6b 65 79 2f 64 61 74 61 20 61 6e 64 20 63 6f   key/data and co
16f67 70 79 20 69 74 20 69 6e 74 6f 20 61 20 70 72 65  py it into a pre
16f68 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65 72  allocated buffer
16f69 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e  ..**.** The poin
16f6a 74 65 72 20 72 65 74 75 72 6e 65 64 20 62 79 20  ter returned by 
16f6b 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6c 6f 6f  this routine loo
16f6c 6b 73 20 64 69 72 65 63 74 6c 79 20 69 6e 74 6f  ks directly into
16f6d 20 74 68 65 20 63 61 63 68 65 64 0a 2a 2a 20 70   the cached.** p
16f6e 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  age of the datab
16f6f 61 73 65 2e 20 20 54 68 65 20 64 61 74 61 20 6d  ase.  The data m
16f70 69 67 68 74 20 63 68 61 6e 67 65 20 6f 72 20 6d  ight change or m
16f71 6f 76 65 20 74 68 65 20 6e 65 78 74 20 74 69 6d  ove the next tim
16f72 65 0a 2a 2a 20 61 6e 79 20 62 74 72 65 65 20 72  e.** any btree r
16f73 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
16f74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
16f75 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
16f76 2a 66 65 74 63 68 50 61 79 6c 6f 61 64 28 0a 20  *fetchPayload(. 
16f77 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c   BtCursor *pCur,
16f78 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
16f79 70 6f 69 6e 74 69 6e 67 20 74 6f 20 65 6e 74 72  pointing to entr
16f7a 79 20 74 6f 20 72 65 61 64 20 66 72 6f 6d 20 2a  y to read from *
16f7b 2f 0a 20 20 69 6e 74 20 2a 70 41 6d 74 2c 20 20  /.  int *pAmt,  
16f7c 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74           /* Writ
16f7d 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
16f7e 61 76 61 69 6c 61 62 6c 65 20 62 79 74 65 73 20  available bytes 
16f7f 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 73 6b  here */.  int sk
16f80 69 70 4b 65 79 20 20 20 20 20 20 20 20 20 20 2f  ipKey          /
16f81 2a 20 72 65 61 64 20 62 65 67 69 6e 6e 69 6e 67  * read beginning
16f82 20 61 74 20 64 61 74 61 20 69 66 20 74 68 69 73   at data if this
16f83 20 69 73 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20   is true */.){. 
16f84 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
16f85 61 50 61 79 6c 6f 61 64 3b 0a 20 20 4d 65 6d 50  aPayload;.  MemP
16f86 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 75 33  age *pPage;.  u3
16f87 32 20 6e 4b 65 79 3b 0a 20 20 75 33 32 20 6e 4c  2 nKey;.  u32 nL
16f88 6f 63 61 6c 3b 0a 0a 20 20 61 73 73 65 72 74 28  ocal;..  assert(
16f89 20 70 43 75 72 21 3d 30 20 26 26 20 70 43 75 72   pCur!=0 && pCur
16f8a 2d 3e 69 50 61 67 65 3e 3d 30 20 26 26 20 70 43  ->iPage>=0 && pC
16f8b 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
16f8c 3e 69 50 61 67 65 5d 29 3b 0a 20 20 61 73 73 65  >iPage]);.  asse
16f8d 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  rt( pCur->eState
16f8e 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
16f8f 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  ;.  assert( curs
16f90 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
16f91 72 29 20 29 3b 0a 20 20 70 50 61 67 65 20 3d 20  r) );.  pPage = 
16f92 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
16f93 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73  r->iPage];.  ass
16f94 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78  ert( pCur->aiIdx
16f95 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 50  [pCur->iPage]<pP
16f96 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20  age->nCell );.  
16f97 69 66 28 20 4e 45 56 45 52 28 70 43 75 72 2d 3e  if( NEVER(pCur->
16f98 69 6e 66 6f 2e 6e 53 69 7a 65 3d 3d 30 29 20 29  info.nSize==0) )
16f99 7b 0a 20 20 20 20 62 74 72 65 65 50 61 72 73 65  {.    btreeParse
16f9a 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 67  Cell(pCur->apPag
16f9b 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2c 20  e[pCur->iPage], 
16f9c 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
16f9d 2d 3e 69 50 61 67 65 5d 2c 0a 20 20 20 20 20 20  ->iPage],.      
16f9e 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70 43               &pC
16f9f 75 72 2d 3e 69 6e 66 6f 29 3b 0a 20 20 7d 0a 20  ur->info);.  }. 
16fa0 20 61 50 61 79 6c 6f 61 64 20 3d 20 70 43 75 72   aPayload = pCur
16fa1 2d 3e 69 6e 66 6f 2e 70 43 65 6c 6c 3b 0a 20 20  ->info.pCell;.  
16fa2 61 50 61 79 6c 6f 61 64 20 2b 3d 20 70 43 75 72  aPayload += pCur
16fa3 2d 3e 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a  ->info.nHeader;.
16fa4 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74    if( pPage->int
16fa5 4b 65 79 20 29 7b 0a 20 20 20 20 6e 4b 65 79 20  Key ){.    nKey 
16fa6 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 0;.  }else{.  
16fa7 20 20 6e 4b 65 79 20 3d 20 28 69 6e 74 29 70 43    nKey = (int)pC
16fa8 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a 20  ur->info.nKey;. 
16fa9 20 7d 0a 20 20 69 66 28 20 73 6b 69 70 4b 65 79   }.  if( skipKey
16faa 20 29 7b 0a 20 20 20 20 61 50 61 79 6c 6f 61 64   ){.    aPayload
16fab 20 2b 3d 20 6e 4b 65 79 3b 0a 20 20 20 20 6e 4c   += nKey;.    nL
16fac 6f 63 61 6c 20 3d 20 70 43 75 72 2d 3e 69 6e 66  ocal = pCur->inf
16fad 6f 2e 6e 4c 6f 63 61 6c 20 2d 20 6e 4b 65 79 3b  o.nLocal - nKey;
16fae 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 4c  .  }else{.    nL
16faf 6f 63 61 6c 20 3d 20 70 43 75 72 2d 3e 69 6e 66  ocal = pCur->inf
16fb0 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 61 73  o.nLocal;.    as
16fb1 73 65 72 74 28 20 6e 4c 6f 63 61 6c 3c 3d 6e 4b  sert( nLocal<=nK
16fb2 65 79 20 29 3b 0a 20 20 7d 0a 20 20 2a 70 41 6d  ey );.  }.  *pAm
16fb3 74 20 3d 20 6e 4c 6f 63 61 6c 3b 0a 20 20 72 65  t = nLocal;.  re
16fb4 74 75 72 6e 20 61 50 61 79 6c 6f 61 64 3b 0a 7d  turn aPayload;.}
16fb5 0a 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 74 68 65  .../*.** For the
16fb6 20 65 6e 74 72 79 20 74 68 61 74 20 63 75 72 73   entry that curs
16fb7 6f 72 20 70 43 75 72 20 69 73 20 70 6f 69 6e 74  or pCur is point
16fb8 20 74 6f 2c 20 72 65 74 75 72 6e 20 61 73 0a 2a   to, return as.*
16fb9 2a 20 6d 61 6e 79 20 62 79 74 65 73 20 6f 66 20  * many bytes of 
16fba 74 68 65 20 6b 65 79 20 6f 72 20 64 61 74 61 20  the key or data 
16fbb 61 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c 65  as are available
16fbc 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c 0a 2a 2a   on the local.**
16fbd 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20 20 57   b-tree page.  W
16fbe 72 69 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20  rite the number 
16fbf 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 62 79 74  of available byt
16fc0 65 73 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 0a 2a  es into *pAmt..*
16fc1 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72  *.** The pointer
16fc2 20 72 65 74 75 72 6e 65 64 20 69 73 20 65 70 68   returned is eph
16fc3 65 6d 65 72 61 6c 2e 20 20 54 68 65 20 6b 65 79  emeral.  The key
16fc4 2f 64 61 74 61 20 6d 61 79 20 6d 6f 76 65 0a 2a  /data may move.*
16fc5 2a 20 6f 72 20 62 65 20 64 65 73 74 72 6f 79 65  * or be destroye
16fc6 64 20 6f 6e 20 74 68 65 20 6e 65 78 74 20 63 61  d on the next ca
16fc7 6c 6c 20 74 6f 20 61 6e 79 20 42 74 72 65 65 20  ll to any Btree 
16fc8 72 6f 75 74 69 6e 65 2c 0a 2a 2a 20 69 6e 63 6c  routine,.** incl
16fc9 75 64 69 6e 67 20 63 61 6c 6c 73 20 66 72 6f 6d  uding calls from
16fca 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 20 61   other threads a
16fcb 67 61 69 6e 73 74 20 74 68 65 20 73 61 6d 65 20  gainst the same 
16fcc 63 61 63 68 65 2e 0a 2a 2a 20 48 65 6e 63 65 2c  cache..** Hence,
16fcd 20 61 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20   a mutex on the 
16fce 42 74 53 68 61 72 65 64 20 73 68 6f 75 6c 64 20  BtShared should 
16fcf 62 65 20 68 65 6c 64 20 70 72 69 6f 72 20 74 6f  be held prior to
16fd0 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 69 73   calling.** this
16fd1 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20   routine..**.** 
16fd2 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 69  These routines i
16fd3 73 20 75 73 65 64 20 74 6f 20 67 65 74 20 71 75  s used to get qu
16fd4 69 63 6b 20 61 63 63 65 73 73 20 74 6f 20 6b 65  ick access to ke
16fd5 79 20 61 6e 64 20 64 61 74 61 0a 2a 2a 20 69 6e  y and data.** in
16fd6 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73 65   the common case
16fd7 20 77 68 65 72 65 20 6e 6f 20 6f 76 65 72 66 6c   where no overfl
16fd8 6f 77 20 70 61 67 65 73 20 61 72 65 20 75 73 65  ow pages are use
16fd9 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
16fda 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20  VATE const void 
16fdb 2a 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65 79  *sqlite3BtreeKey
16fdc 46 65 74 63 68 28 42 74 43 75 72 73 6f 72 20 2a  Fetch(BtCursor *
16fdd 70 43 75 72 2c 20 69 6e 74 20 2a 70 41 6d 74 29  pCur, int *pAmt)
16fde 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  {.  const void *
16fdf 70 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28  p = 0;.  assert(
16fe0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
16fe1 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65  eld(pCur->pBtree
16fe2 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
16fe3 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
16fe4 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
16fe5 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53   );.  if( ALWAYS
16fe6 28 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43  (pCur->eState==C
16fe7 55 52 53 4f 52 5f 56 41 4c 49 44 29 20 29 7b 0a  URSOR_VALID) ){.
16fe8 20 20 20 20 70 20 3d 20 28 63 6f 6e 73 74 20 76      p = (const v
16fe9 6f 69 64 2a 29 66 65 74 63 68 50 61 79 6c 6f 61  oid*)fetchPayloa
16fea 64 28 70 43 75 72 2c 20 70 41 6d 74 2c 20 30 29  d(pCur, pAmt, 0)
16feb 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
16fec 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ;.}.SQLITE_PRIVA
16fed 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  TE const void *s
16fee 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 46  qlite3BtreeDataF
16fef 65 74 63 68 28 42 74 43 75 72 73 6f 72 20 2a 70  etch(BtCursor *p
16ff0 43 75 72 2c 20 69 6e 74 20 2a 70 41 6d 74 29 7b  Cur, int *pAmt){
16ff1 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  .  const void *p
16ff2 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
16ff3 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
16ff4 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65 2d  ld(pCur->pBtree-
16ff5 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
16ff6 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
16ff7 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
16ff8 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28  );.  if( ALWAYS(
16ff9 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55  pCur->eState==CU
16ffa 52 53 4f 52 5f 56 41 4c 49 44 29 20 29 7b 0a 20  RSOR_VALID) ){. 
16ffb 20 20 20 70 20 3d 20 28 63 6f 6e 73 74 20 76 6f     p = (const vo
16ffc 69 64 2a 29 66 65 74 63 68 50 61 79 6c 6f 61 64  id*)fetchPayload
16ffd 28 70 43 75 72 2c 20 70 41 6d 74 2c 20 31 29 3b  (pCur, pAmt, 1);
16ffe 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b  .  }.  return p;
16fff 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 6f 76 65 20  .}.../*.** Move 
17000 74 68 65 20 63 75 72 73 6f 72 20 64 6f 77 6e 20  the cursor down 
17001 74 6f 20 61 20 6e 65 77 20 63 68 69 6c 64 20 70  to a new child p
17002 61 67 65 2e 20 20 54 68 65 20 6e 65 77 50 67 6e  age.  The newPgn
17003 6f 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  o argument is th
17004 65 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72  e.** page number
17005 20 6f 66 20 74 68 65 20 63 68 69 6c 64 20 70 61   of the child pa
17006 67 65 20 74 6f 20 6d 6f 76 65 20 74 6f 2e 0a 2a  ge to move to..*
17007 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
17008 6f 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  on returns SQLIT
17009 45 5f 43 4f 52 52 55 50 54 20 69 66 20 74 68 65  E_CORRUPT if the
1700a 20 70 61 67 65 2d 68 65 61 64 65 72 20 66 6c 61   page-header fla
1700b 67 73 20 66 69 65 6c 64 20 6f 66 0a 2a 2a 20 74  gs field of.** t
1700c 68 65 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67  he new child pag
1700d 65 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68  e does not match
1700e 20 74 68 65 20 66 6c 61 67 73 20 66 69 65 6c 64   the flags field
1700f 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20 28   of the parent (
17010 69 2e 65 2e 0a 2a 2a 20 69 66 20 61 6e 20 69 6e  i.e..** if an in
17011 74 6b 65 79 20 70 61 67 65 20 61 70 70 65 61 72  tkey page appear
17012 73 20 74 6f 20 62 65 20 74 68 65 20 70 61 72 65  s to be the pare
17013 6e 74 20 6f 66 20 61 20 6e 6f 6e 2d 69 6e 74 6b  nt of a non-intk
17014 65 79 20 70 61 67 65 2c 20 6f 72 0a 2a 2a 20 76  ey page, or.** v
17015 69 63 65 2d 76 65 72 73 61 29 2e 0a 2a 2f 0a 73  ice-versa)..*/.s
17016 74 61 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f  tatic int moveTo
17017 43 68 69 6c 64 28 42 74 43 75 72 73 6f 72 20 2a  Child(BtCursor *
17018 70 43 75 72 2c 20 75 33 32 20 6e 65 77 50 67 6e  pCur, u32 newPgn
17019 6f 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  o){.  int rc;.  
1701a 69 6e 74 20 69 20 3d 20 70 43 75 72 2d 3e 69 50  int i = pCur->iP
1701b 61 67 65 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  age;.  MemPage *
1701c 70 4e 65 77 50 61 67 65 3b 0a 20 20 42 74 53 68  pNewPage;.  BtSh
1701d 61 72 65 64 20 2a 70 42 74 20 3d 20 70 43 75 72  ared *pBt = pCur
1701e 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74  ->pBt;..  assert
1701f 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
17020 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73  ex(pCur) );.  as
17021 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61  sert( pCur->eSta
17022 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te==CURSOR_VALID
17023 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
17024 75 72 2d 3e 69 50 61 67 65 3c 42 54 43 55 52 53  ur->iPage<BTCURS
17025 4f 52 5f 4d 41 58 5f 44 45 50 54 48 20 29 3b 0a  OR_MAX_DEPTH );.
17026 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50 61 67    if( pCur->iPag
17027 65 3e 3d 28 42 54 43 55 52 53 4f 52 5f 4d 41 58  e>=(BTCURSOR_MAX
17028 5f 44 45 50 54 48 2d 31 29 20 29 7b 0a 20 20 20  _DEPTH-1) ){.   
17029 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
1702a 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d  ORRUPT_BKPT;.  }
1702b 0a 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e  .  rc = getAndIn
1702c 69 74 50 61 67 65 28 70 42 74 2c 20 6e 65 77 50  itPage(pBt, newP
1702d 67 6e 6f 2c 20 26 70 4e 65 77 50 61 67 65 29 3b  gno, &pNewPage);
1702e 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75  .  if( rc ) retu
1702f 72 6e 20 72 63 3b 0a 20 20 70 43 75 72 2d 3e 61  rn rc;.  pCur->a
17030 70 50 61 67 65 5b 69 2b 31 5d 20 3d 20 70 4e 65  pPage[i+1] = pNe
17031 77 50 61 67 65 3b 0a 20 20 70 43 75 72 2d 3e 61  wPage;.  pCur->a
17032 69 49 64 78 5b 69 2b 31 5d 20 3d 20 30 3b 0a 20  iIdx[i+1] = 0;. 
17033 20 70 43 75 72 2d 3e 69 50 61 67 65 2b 2b 3b 0a   pCur->iPage++;.
17034 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53  .  pCur->info.nS
17035 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d  ize = 0;.  pCur-
17036 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a  >validNKey = 0;.
17037 20 20 69 66 28 20 70 4e 65 77 50 61 67 65 2d 3e    if( pNewPage->
17038 6e 43 65 6c 6c 3c 31 20 7c 7c 20 70 4e 65 77 50  nCell<1 || pNewP
17039 61 67 65 2d 3e 69 6e 74 4b 65 79 21 3d 70 43 75  age->intKey!=pCu
1703a 72 2d 3e 61 70 50 61 67 65 5b 69 5d 2d 3e 69 6e  r->apPage[i]->in
1703b 74 4b 65 79 20 29 7b 0a 20 20 20 20 72 65 74 75  tKey ){.    retu
1703c 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
1703d 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65  T_BKPT;.  }.  re
1703e 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1703f 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  }..#ifndef NDEBU
17040 47 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 70 50 61  G./*.** Page pPa
17041 72 65 6e 74 20 69 73 20 61 6e 20 69 6e 74 65 72  rent is an inter
17042 6e 61 6c 20 28 6e 6f 6e 2d 6c 65 61 66 29 20 74  nal (non-leaf) t
17043 72 65 65 20 70 61 67 65 2e 20 54 68 69 73 20 66  ree page. This f
17044 75 6e 63 74 69 6f 6e 20 0a 2a 2a 20 61 73 73 65  unction .** asse
17045 72 74 73 20 74 68 61 74 20 70 61 67 65 20 6e 75  rts that page nu
17046 6d 62 65 72 20 69 43 68 69 6c 64 20 69 73 20 74  mber iChild is t
17047 68 65 20 6c 65 66 74 2d 63 68 69 6c 64 20 69 66  he left-child if
17048 20 74 68 65 20 69 49 64 78 27 74 68 0a 2a 2a 20   the iIdx'th.** 
17049 63 65 6c 6c 20 69 6e 20 70 61 67 65 20 70 50 61  cell in page pPa
1704a 72 65 6e 74 2e 20 4f 72 2c 20 69 66 20 69 49 64  rent. Or, if iId
1704b 78 20 69 73 20 65 71 75 61 6c 20 74 6f 20 74 68  x is equal to th
1704c 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
1704d 66 0a 2a 2a 20 63 65 6c 6c 73 20 69 6e 20 70 50  f.** cells in pP
1704e 61 72 65 6e 74 2c 20 74 68 61 74 20 70 61 67 65  arent, that page
1704f 20 6e 75 6d 62 65 72 20 69 43 68 69 6c 64 20 69   number iChild i
17050 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 69 6c  s the right-chil
17051 64 20 6f 66 0a 2a 2a 20 74 68 65 20 70 61 67 65  d of.** the page
17052 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
17053 20 61 73 73 65 72 74 50 61 72 65 6e 74 49 6e 64   assertParentInd
17054 65 78 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 72  ex(MemPage *pPar
17055 65 6e 74 2c 20 69 6e 74 20 69 49 64 78 2c 20 50  ent, int iIdx, P
17056 67 6e 6f 20 69 43 68 69 6c 64 29 7b 0a 20 20 61  gno iChild){.  a
17057 73 73 65 72 74 28 20 69 49 64 78 3c 3d 70 50 61  ssert( iIdx<=pPa
17058 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20  rent->nCell );. 
17059 20 69 66 28 20 69 49 64 78 3d 3d 70 50 61 72 65   if( iIdx==pPare
1705a 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20 20  nt->nCell ){.   
1705b 20 61 73 73 65 72 74 28 20 67 65 74 34 62 79 74   assert( get4byt
1705c 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 61 74  e(&pParent->aDat
1705d 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66  a[pParent->hdrOf
1705e 66 73 65 74 2b 38 5d 29 3d 3d 69 43 68 69 6c 64  fset+8])==iChild
1705f 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   );.  }else{.   
17060 20 61 73 73 65 72 74 28 20 67 65 74 34 62 79 74   assert( get4byt
17061 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 65  e(findCell(pPare
17062 6e 74 2c 20 69 49 64 78 29 29 3d 3d 69 43 68 69  nt, iIdx))==iChi
17063 6c 64 20 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73  ld );.  }.}.#els
17064 65 0a 23 20 20 64 65 66 69 6e 65 20 61 73 73 65  e.#  define asse
17065 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 78 2c  rtParentIndex(x,
17066 79 2c 7a 29 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a  y,z) .#endif../*
17067 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72  .** Move the cur
17068 73 6f 72 20 75 70 20 74 6f 20 74 68 65 20 70 61  sor up to the pa
17069 72 65 6e 74 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a  rent page..**.**
1706a 20 70 43 75 72 2d 3e 69 64 78 20 69 73 20 73 65   pCur->idx is se
1706b 74 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 69 6e  t to the cell in
1706c 64 65 78 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  dex that contain
1706d 73 20 74 68 65 20 70 6f 69 6e 74 65 72 0a 2a 2a  s the pointer.**
1706e 20 74 6f 20 74 68 65 20 70 61 67 65 20 77 65 20   to the page we 
1706f 61 72 65 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 2e  are coming from.
17070 20 20 49 66 20 77 65 20 61 72 65 20 63 6f 6d 69    If we are comi
17071 6e 67 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 72  ng from the.** r
17072 69 67 68 74 2d 6d 6f 73 74 20 63 68 69 6c 64 20  ight-most child 
17073 70 61 67 65 20 74 68 65 6e 20 70 43 75 72 2d 3e  page then pCur->
17074 69 64 78 20 69 73 20 73 65 74 20 74 6f 20 6f 6e  idx is set to on
17075 65 20 6d 6f 72 65 20 74 68 61 6e 0a 2a 2a 20 74  e more than.** t
17076 68 65 20 6c 61 72 67 65 73 74 20 63 65 6c 6c 20  he largest cell 
17077 69 6e 64 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63  index..*/.static
17078 20 76 6f 69 64 20 6d 6f 76 65 54 6f 50 61 72 65   void moveToPare
17079 6e 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  nt(BtCursor *pCu
1707a 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75  r){.  assert( cu
1707b 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
1707c 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Cur) );.  assert
1707d 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
1707e 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a  CURSOR_VALID );.
1707f 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
17080 69 50 61 67 65 3e 30 20 29 3b 0a 20 20 61 73 73  iPage>0 );.  ass
17081 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67  ert( pCur->apPag
17082 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 29  e[pCur->iPage] )
17083 3b 0a 20 20 61 73 73 65 72 74 50 61 72 65 6e 74  ;.  assertParent
17084 49 6e 64 65 78 28 0a 20 20 20 20 70 43 75 72 2d  Index(.    pCur-
17085 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
17086 61 67 65 2d 31 5d 2c 20 0a 20 20 20 20 70 43 75  age-1], .    pCu
17087 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
17088 50 61 67 65 2d 31 5d 2c 20 0a 20 20 20 20 70 43  Page-1], .    pC
17089 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
1708a 3e 69 50 61 67 65 5d 2d 3e 70 67 6e 6f 0a 20 20  >iPage]->pgno.  
1708b 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65  );.  releasePage
1708c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43  (pCur->apPage[pC
1708d 75 72 2d 3e 69 50 61 67 65 5d 29 3b 0a 20 20 70  ur->iPage]);.  p
1708e 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a 20 20  Cur->iPage--;.  
1708f 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
17090 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61   = 0;.  pCur->va
17091 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 7d 0a 0a  lidNKey = 0;.}..
17092 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63  /*.** Move the c
17093 75 72 73 6f 72 20 74 6f 20 70 6f 69 6e 74 20 74  ursor to point t
17094 6f 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  o the root page 
17095 6f 66 20 69 74 73 20 62 2d 74 72 65 65 20 73 74  of its b-tree st
17096 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 49  ructure..**.** I
17097 66 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20  f the table has 
17098 61 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70  a virtual root p
17099 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 63 75  age, then the cu
1709a 72 73 6f 72 20 69 73 20 6d 6f 76 65 64 20 74 6f  rsor is moved to
1709b 20 70 6f 69 6e 74 0a 2a 2a 20 74 6f 20 74 68 65   point.** to the
1709c 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70 61   virtual root pa
1709d 67 65 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68  ge instead of th
1709e 65 20 61 63 74 75 61 6c 20 72 6f 6f 74 20 70 61  e actual root pa
1709f 67 65 2e 20 41 20 74 61 62 6c 65 20 68 61 73 20  ge. A table has 
170a0 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20 72 6f 6f  a.** virtual roo
170a1 74 20 70 61 67 65 20 77 68 65 6e 20 74 68 65 20  t page when the 
170a2 61 63 74 75 61 6c 20 72 6f 6f 74 20 70 61 67 65  actual root page
170a3 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 63 65 6c   contains no cel
170a4 6c 73 20 61 6e 64 20 61 20 0a 2a 2a 20 73 69 6e  ls and a .** sin
170a5 67 6c 65 20 63 68 69 6c 64 20 70 61 67 65 2e 20  gle child page. 
170a6 54 68 69 73 20 63 61 6e 20 6f 6e 6c 79 20 68 61  This can only ha
170a7 70 70 65 6e 20 77 69 74 68 20 74 68 65 20 74 61  ppen with the ta
170a8 62 6c 65 20 72 6f 6f 74 65 64 20 61 74 20 70 61  ble rooted at pa
170a9 67 65 20 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ge 1..**.** If t
170aa 68 65 20 62 2d 74 72 65 65 20 73 74 72 75 63 74  he b-tree struct
170ab 75 72 65 20 69 73 20 65 6d 70 74 79 2c 20 74 68  ure is empty, th
170ac 65 20 63 75 72 73 6f 72 20 73 74 61 74 65 20 69  e cursor state i
170ad 73 20 73 65 74 20 74 6f 20 0a 2a 2a 20 43 55 52  s set to .** CUR
170ae 53 4f 52 5f 49 4e 56 41 4c 49 44 2e 20 4f 74 68  SOR_INVALID. Oth
170af 65 72 77 69 73 65 2c 20 74 68 65 20 63 75 72 73  erwise, the curs
170b0 6f 72 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69  or is set to poi
170b1 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 0a  nt to the first.
170b2 2a 2a 20 63 65 6c 6c 20 6c 6f 63 61 74 65 64 20  ** cell located 
170b3 6f 6e 20 74 68 65 20 72 6f 6f 74 20 28 6f 72 20  on the root (or 
170b4 76 69 72 74 75 61 6c 20 72 6f 6f 74 29 20 70 61  virtual root) pa
170b5 67 65 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f  ge and the curso
170b6 72 20 73 74 61 74 65 0a 2a 2a 20 69 73 20 73 65  r state.** is se
170b7 74 20 74 6f 20 43 55 52 53 4f 52 5f 56 41 4c 49  t to CURSOR_VALI
170b8 44 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73  D..**.** If this
170b9 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
170ba 73 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20  s successfully, 
170bb 69 74 20 6d 61 79 20 62 65 20 61 73 73 75 6d 65  it may be assume
170bc 64 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 70 61  d that the.** pa
170bd 67 65 2d 68 65 61 64 65 72 20 66 6c 61 67 73 20  ge-header flags 
170be 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68  indicate that th
170bf 65 20 5b 76 69 72 74 75 61 6c 5d 20 72 6f 6f 74  e [virtual] root
170c0 2d 70 61 67 65 20 69 73 20 74 68 65 20 65 78 70  -page is the exp
170c1 65 63 74 65 64 20 0a 2a 2a 20 6b 69 6e 64 20 6f  ected .** kind o
170c2 66 20 62 2d 74 72 65 65 20 70 61 67 65 20 28 69  f b-tree page (i
170c3 2e 65 2e 20 69 66 20 77 68 65 6e 20 6f 70 65 6e  .e. if when open
170c4 69 6e 67 20 74 68 65 20 63 75 72 73 6f 72 20 74  ing the cursor t
170c5 68 65 20 63 61 6c 6c 65 72 20 64 69 64 20 6e 6f  he caller did no
170c6 74 0a 2a 2a 20 73 70 65 63 69 66 79 20 61 20 4b  t.** specify a K
170c7 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
170c8 20 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 20   the flags byte 
170c9 69 73 20 73 65 74 20 74 6f 20 30 78 30 35 20 6f  is set to 0x05 o
170ca 72 20 30 78 30 44 2c 0a 2a 2a 20 69 6e 64 69 63  r 0x0D,.** indic
170cb 61 74 69 6e 67 20 61 20 74 61 62 6c 65 20 62 2d  ating a table b-
170cc 74 72 65 65 2c 20 6f 72 20 69 66 20 74 68 65 20  tree, or if the 
170cd 63 61 6c 6c 65 72 20 64 69 64 20 73 70 65 63 69  caller did speci
170ce 66 79 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a 2a  fy a KeyInfo .**
170cf 20 73 74 72 75 63 74 75 72 65 20 74 68 65 20 66   structure the f
170d0 6c 61 67 73 20 62 79 74 65 20 69 73 20 73 65 74  lags byte is set
170d1 20 74 6f 20 30 78 30 32 20 6f 72 20 30 78 30 41   to 0x02 or 0x0A
170d2 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 61 6e 20  , indicating an 
170d3 69 6e 64 65 78 0a 2a 2a 20 62 2d 74 72 65 65 29  index.** b-tree)
170d4 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
170d5 6d 6f 76 65 54 6f 52 6f 6f 74 28 42 74 43 75 72  moveToRoot(BtCur
170d6 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 4d 65  sor *pCur){.  Me
170d7 6d 50 61 67 65 20 2a 70 52 6f 6f 74 3b 0a 20 20  mPage *pRoot;.  
170d8 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
170d9 4f 4b 3b 0a 20 20 42 74 72 65 65 20 2a 70 20 3d  OK;.  Btree *p =
170da 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20   pCur->pBtree;. 
170db 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
170dc 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 61 73 73 65   p->pBt;..  asse
170dd 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
170de 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
170df 61 73 73 65 72 74 28 20 43 55 52 53 4f 52 5f 49  assert( CURSOR_I
170e0 4e 56 41 4c 49 44 20 3c 20 43 55 52 53 4f 52 5f  NVALID < CURSOR_
170e1 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20  REQUIRESEEK );. 
170e2 20 61 73 73 65 72 74 28 20 43 55 52 53 4f 52 5f   assert( CURSOR_
170e3 56 41 4c 49 44 20 20 20 3c 20 43 55 52 53 4f 52  VALID   < CURSOR
170e4 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a  _REQUIRESEEK );.
170e5 20 20 61 73 73 65 72 74 28 20 43 55 52 53 4f 52    assert( CURSOR
170e6 5f 46 41 55 4c 54 20 20 20 3e 20 43 55 52 53 4f  _FAULT   > CURSO
170e7 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b  R_REQUIRESEEK );
170e8 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74  .  if( pCur->eSt
170e9 61 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55  ate>=CURSOR_REQU
170ea 49 52 45 53 45 45 4b 20 29 7b 0a 20 20 20 20 69  IRESEEK ){.    i
170eb 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  f( pCur->eState=
170ec 3d 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 29 7b  =CURSOR_FAULT ){
170ed 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
170ee 43 75 72 2d 3e 73 6b 69 70 4e 65 78 74 21 3d 53  Cur->skipNext!=S
170ef 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20 20  QLITE_OK );.    
170f0 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d 3e 73    return pCur->s
170f1 6b 69 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20  kipNext;.    }. 
170f2 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
170f3 6c 65 61 72 43 75 72 73 6f 72 28 70 43 75 72 29  learCursor(pCur)
170f4 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 43 75  ;.  }..  if( pCu
170f5 72 2d 3e 69 50 61 67 65 3e 3d 30 20 29 7b 0a 20  r->iPage>=0 ){. 
170f6 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f     int i;.    fo
170f7 72 28 69 3d 31 3b 20 69 3c 3d 70 43 75 72 2d 3e  r(i=1; i<=pCur->
170f8 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20  iPage; i++){.   
170f9 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
170fa 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 29 3b  Cur->apPage[i]);
170fb 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75 72 2d  .    }.    pCur-
170fc 3e 69 50 61 67 65 20 3d 20 30 3b 0a 20 20 7d 65  >iPage = 0;.  }e
170fd 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 67 65  lse{.    rc = ge
170fe 74 41 6e 64 49 6e 69 74 50 61 67 65 28 70 42 74  tAndInitPage(pBt
170ff 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74  , pCur->pgnoRoot
17100 2c 20 26 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  , &pCur->apPage[
17101 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  0]);.    if( rc!
17102 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
17103 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65      pCur->eState
17104 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49   = CURSOR_INVALI
17105 44 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  D;.      return 
17106 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43  rc;.    }.    pC
17107 75 72 2d 3e 69 50 61 67 65 20 3d 20 30 3b 0a 0a  ur->iPage = 0;..
17108 20 20 20 20 2f 2a 20 49 66 20 70 43 75 72 2d 3e      /* If pCur->
17109 70 4b 65 79 49 6e 66 6f 20 69 73 20 6e 6f 74 20  pKeyInfo is not 
1710a 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 63  NULL, then the c
1710b 61 6c 6c 65 72 20 74 68 61 74 20 6f 70 65 6e 65  aller that opene
1710c 64 20 74 68 69 73 20 63 75 72 73 6f 72 0a 20 20  d this cursor.  
1710d 20 20 2a 2a 20 65 78 70 65 63 74 65 64 20 74 6f    ** expected to
1710e 20 6f 70 65 6e 20 69 74 20 6f 6e 20 61 6e 20 69   open it on an i
1710f 6e 64 65 78 20 62 2d 74 72 65 65 2e 20 4f 74 68  ndex b-tree. Oth
17110 65 72 77 69 73 65 2c 20 69 66 20 70 4b 65 79 49  erwise, if pKeyI
17111 6e 66 6f 20 69 73 0a 20 20 20 20 2a 2a 20 4e 55  nfo is.    ** NU
17112 4c 4c 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 65  LL, the caller e
17113 78 70 65 63 74 73 20 61 20 74 61 62 6c 65 20 62  xpects a table b
17114 2d 74 72 65 65 2e 20 49 66 20 74 68 69 73 20 69  -tree. If this i
17115 73 20 6e 6f 74 20 74 68 65 20 63 61 73 65 2c 0a  s not the case,.
17116 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 61 6e      ** return an
17117 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20   SQLITE_CORRUPT 
17118 65 72 72 6f 72 2e 20 20 2a 2f 0a 20 20 20 20 61  error.  */.    a
17119 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50  ssert( pCur->apP
1711a 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 3d 3d  age[0]->intKey==
1711b 31 20 7c 7c 20 70 43 75 72 2d 3e 61 70 50 61 67  1 || pCur->apPag
1711c 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 3d 3d 30 20  e[0]->intKey==0 
1711d 29 3b 0a 20 20 20 20 69 66 28 20 28 70 43 75 72  );.    if( (pCur
1711e 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 29 21 3d  ->pKeyInfo==0)!=
1711f 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d  pCur->apPage[0]-
17120 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20  >intKey ){.     
17121 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
17122 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
17123 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 73 73   }.  }..  /* Ass
17124 65 72 74 20 74 68 61 74 20 74 68 65 20 72 6f 6f  ert that the roo
17125 74 20 70 61 67 65 20 69 73 20 6f 66 20 74 68 65  t page is of the
17126 20 63 6f 72 72 65 63 74 20 74 79 70 65 2e 20 54   correct type. T
17127 68 69 73 20 6d 75 73 74 20 62 65 20 74 68 65 0a  his must be the.
17128 20 20 2a 2a 20 63 61 73 65 20 61 73 20 74 68 65    ** case as the
17129 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 66 75   call to this fu
1712a 6e 63 74 69 6f 6e 20 74 68 61 74 20 6c 6f 61 64  nction that load
1712b 65 64 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65  ed the root-page
1712c 20 28 65 69 74 68 65 72 0a 20 20 2a 2a 20 74 68   (either.  ** th
1712d 69 73 20 63 61 6c 6c 20 6f 72 20 61 20 70 72 65  is call or a pre
1712e 76 69 6f 75 73 20 69 6e 76 6f 63 61 74 69 6f 6e  vious invocation
1712f 29 20 77 6f 75 6c 64 20 68 61 76 65 20 64 65 74  ) would have det
17130 65 63 74 65 64 20 63 6f 72 72 75 70 74 69 6f 6e  ected corruption
17131 20 0a 20 20 2a 2a 20 69 66 20 74 68 65 20 61 73   .  ** if the as
17132 73 75 6d 70 74 69 6f 6e 20 77 65 72 65 20 6e 6f  sumption were no
17133 74 20 74 72 75 65 2c 20 61 6e 64 20 69 74 20 69  t true, and it i
17134 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66  s not possible f
17135 6f 72 20 74 68 65 20 66 6c 61 67 73 20 0a 20 20  or the flags .  
17136 2a 2a 20 62 79 74 65 20 74 6f 20 68 61 76 65 20  ** byte to have 
17137 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 77 68  been modified wh
17138 69 6c 65 20 74 68 69 73 20 63 75 72 73 6f 72 20  ile this cursor 
17139 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 66  is holding a ref
1713a 65 72 65 6e 63 65 0a 20 20 2a 2a 20 74 6f 20 74  erence.  ** to t
1713b 68 65 20 70 61 67 65 2e 20 20 2a 2f 0a 20 20 70  he page.  */.  p
1713c 52 6f 6f 74 20 3d 20 70 43 75 72 2d 3e 61 70 50  Root = pCur->apP
1713d 61 67 65 5b 30 5d 3b 0a 20 20 61 73 73 65 72 74  age[0];.  assert
1713e 28 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 3d 3d 70  ( pRoot->pgno==p
1713f 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74 20 29 3b  Cur->pgnoRoot );
17140 0a 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74  .  assert( pRoot
17141 2d 3e 69 73 49 6e 69 74 20 26 26 20 28 70 43 75  ->isInit && (pCu
17142 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 29 3d  r->pKeyInfo==0)=
17143 3d 70 52 6f 6f 74 2d 3e 69 6e 74 4b 65 79 20 29  =pRoot->intKey )
17144 3b 0a 0a 20 20 70 43 75 72 2d 3e 61 69 49 64 78  ;..  pCur->aiIdx
17145 5b 30 5d 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d  [0] = 0;.  pCur-
17146 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b  >info.nSize = 0;
17147 0a 20 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20  .  pCur->atLast 
17148 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c  = 0;.  pCur->val
17149 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 0a 20 20 69  idNKey = 0;..  i
1714a 66 28 20 70 52 6f 6f 74 2d 3e 6e 43 65 6c 6c 3d  f( pRoot->nCell=
1714b 3d 30 20 26 26 20 21 70 52 6f 6f 74 2d 3e 6c 65  =0 && !pRoot->le
1714c 61 66 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 73  af ){.    Pgno s
1714d 75 62 70 61 67 65 3b 0a 20 20 20 20 69 66 28 20  ubpage;.    if( 
1714e 70 52 6f 6f 74 2d 3e 70 67 6e 6f 21 3d 31 20 29  pRoot->pgno!=1 )
1714f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
17150 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
17151 20 73 75 62 70 61 67 65 20 3d 20 67 65 74 34 62   subpage = get4b
17152 79 74 65 28 26 70 52 6f 6f 74 2d 3e 61 44 61 74  yte(&pRoot->aDat
17153 61 5b 70 52 6f 6f 74 2d 3e 68 64 72 4f 66 66 73  a[pRoot->hdrOffs
17154 65 74 2b 38 5d 29 3b 0a 20 20 20 20 70 43 75 72  et+8]);.    pCur
17155 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f  ->eState = CURSO
17156 52 5f 56 41 4c 49 44 3b 0a 20 20 20 20 72 63 20  R_VALID;.    rc 
17157 3d 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43  = moveToChild(pC
17158 75 72 2c 20 73 75 62 70 61 67 65 29 3b 0a 20 20  ur, subpage);.  
17159 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 75 72 2d  }else{.    pCur-
1715a 3e 65 53 74 61 74 65 20 3d 20 28 28 70 52 6f 6f  >eState = ((pRoo
1715b 74 2d 3e 6e 43 65 6c 6c 3e 30 29 3f 43 55 52 53  t->nCell>0)?CURS
1715c 4f 52 5f 56 41 4c 49 44 3a 43 55 52 53 4f 52 5f  OR_VALID:CURSOR_
1715d 49 4e 56 41 4c 49 44 29 3b 0a 20 20 7d 0a 20 20  INVALID);.  }.  
1715e 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1715f 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72  .** Move the cur
17160 73 6f 72 20 64 6f 77 6e 20 74 6f 20 74 68 65 20  sor down to the 
17161 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 61 66 20 65  left-most leaf e
17162 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65  ntry beneath the
17163 0a 2a 2a 20 65 6e 74 72 79 20 74 6f 20 77 68 69  .** entry to whi
17164 63 68 20 69 74 20 69 73 20 63 75 72 72 65 6e 74  ch it is current
17165 6c 79 20 70 6f 69 6e 74 69 6e 67 2e 0a 2a 2a 0a  ly pointing..**.
17166 2a 2a 20 54 68 65 20 6c 65 66 74 2d 6d 6f 73 74  ** The left-most
17167 20 6c 65 61 66 20 69 73 20 74 68 65 20 6f 6e 65   leaf is the one
17168 20 77 69 74 68 20 74 68 65 20 73 6d 61 6c 6c 65   with the smalle
17169 73 74 20 6b 65 79 20 2d 20 74 68 65 20 66 69 72  st key - the fir
1716a 73 74 0a 2a 2a 20 69 6e 20 61 73 63 65 6e 64 69  st.** in ascendi
1716b 6e 67 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61  ng order..*/.sta
1716c 74 69 63 20 69 6e 74 20 6d 6f 76 65 54 6f 4c 65  tic int moveToLe
1716d 66 74 6d 6f 73 74 28 42 74 43 75 72 73 6f 72 20  ftmost(BtCursor 
1716e 2a 70 43 75 72 29 7b 0a 20 20 50 67 6e 6f 20 70  *pCur){.  Pgno p
1716f 67 6e 6f 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  gno;.  int rc = 
17170 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d  SQLITE_OK;.  Mem
17171 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20  Page *pPage;..  
17172 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
17173 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29  ldsMutex(pCur) )
17174 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
17175 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
17176 5f 56 41 4c 49 44 20 29 3b 0a 20 20 77 68 69 6c  _VALID );.  whil
17177 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  e( rc==SQLITE_OK
17178 20 26 26 20 21 28 70 50 61 67 65 20 3d 20 70 43   && !(pPage = pC
17179 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
1717a 3e 69 50 61 67 65 5d 29 2d 3e 6c 65 61 66 20 29  >iPage])->leaf )
1717b 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  {.    assert( pC
1717c 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e  ur->aiIdx[pCur->
1717d 69 50 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43  iPage]<pPage->nC
1717e 65 6c 6c 20 29 3b 0a 20 20 20 20 70 67 6e 6f 20  ell );.    pgno 
1717f 3d 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43  = get4byte(findC
17180 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 75 72 2d  ell(pPage, pCur-
17181 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
17182 67 65 5d 29 29 3b 0a 20 20 20 20 72 63 20 3d 20  ge]));.    rc = 
17183 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72  moveToChild(pCur
17184 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72  , pgno);.  }.  r
17185 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
17186 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73  ** Move the curs
17187 6f 72 20 64 6f 77 6e 20 74 6f 20 74 68 65 20 72  or down to the r
17188 69 67 68 74 2d 6d 6f 73 74 20 6c 65 61 66 20 65  ight-most leaf e
17189 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65  ntry beneath the
1718a 0a 2a 2a 20 70 61 67 65 20 74 6f 20 77 68 69 63  .** page to whic
1718b 68 20 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c  h it is currentl
1718c 79 20 70 6f 69 6e 74 69 6e 67 2e 20 20 4e 6f 74  y pointing.  Not
1718d 69 63 65 20 74 68 65 20 64 69 66 66 65 72 65 6e  ice the differen
1718e 63 65 0a 2a 2a 20 62 65 74 77 65 65 6e 20 6d 6f  ce.** between mo
1718f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 29 20 61  veToLeftmost() a
17190 6e 64 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f  nd moveToRightmo
17191 73 74 28 29 2e 20 20 6d 6f 76 65 54 6f 4c 65 66  st().  moveToLef
17192 74 6d 6f 73 74 28 29 0a 2a 2a 20 66 69 6e 64 73  tmost().** finds
17193 20 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 65   the left-most e
17194 6e 74 72 79 20 62 65 6e 65 61 74 68 20 74 68 65  ntry beneath the
17195 20 2a 65 6e 74 72 79 2a 20 77 68 65 72 65 61 73   *entry* whereas
17196 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74   moveToRightmost
17197 28 29 0a 2a 2a 20 66 69 6e 64 73 20 74 68 65 20  ().** finds the 
17198 72 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79  right-most entry
17199 20 62 65 6e 65 61 74 68 20 74 68 65 20 2a 70 61   beneath the *pa
1719a 67 65 2a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  ge*..**.** The r
1719b 69 67 68 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20  ight-most entry 
1719c 69 73 20 74 68 65 20 6f 6e 65 20 77 69 74 68 20  is the one with 
1719d 74 68 65 20 6c 61 72 67 65 73 74 20 6b 65 79 20  the largest key 
1719e 2d 20 74 68 65 20 6c 61 73 74 0a 2a 2a 20 6b 65  - the last.** ke
1719f 79 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f  y in ascending o
171a0 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rder..*/.static 
171a1 69 6e 74 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d  int moveToRightm
171a2 6f 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43  ost(BtCursor *pC
171a3 75 72 29 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f  ur){.  Pgno pgno
171a4 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
171a5 49 54 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67  ITE_OK;.  MemPag
171a6 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20  e *pPage = 0;.. 
171a7 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
171a8 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
171a9 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75  );.  assert( pCu
171aa 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f  r->eState==CURSO
171ab 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 77 68 69  R_VALID );.  whi
171ac 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  le( rc==SQLITE_O
171ad 4b 20 26 26 20 21 28 70 50 61 67 65 20 3d 20 70  K && !(pPage = p
171ae 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
171af 2d 3e 69 50 61 67 65 5d 29 2d 3e 6c 65 61 66 20  ->iPage])->leaf 
171b0 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65  ){.    pgno = ge
171b1 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61  t4byte(&pPage->a
171b2 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f  Data[pPage->hdrO
171b3 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 70  ffset+8]);.    p
171b4 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
171b5 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67 65 2d  >iPage] = pPage-
171b6 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20 72 63 20 3d  >nCell;.    rc =
171b7 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75   moveToChild(pCu
171b8 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 20 20  r, pgno);.  }.  
171b9 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
171ba 4b 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 61  K ){.    pCur->a
171bb 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
171bc 5d 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  ] = pPage->nCell
171bd 2d 31 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e  -1;.    pCur->in
171be 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20  fo.nSize = 0;.  
171bf 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65    pCur->validNKe
171c0 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  y = 0;.  }.  ret
171c1 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f  urn rc;.}../* Mo
171c2 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f  ve the cursor to
171c3 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79   the first entry
171c4 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20 20   in the table.  
171c5 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
171c6 0a 2a 2a 20 6f 6e 20 73 75 63 63 65 73 73 2e 20  .** on success. 
171c7 20 53 65 74 20 2a 70 52 65 73 20 74 6f 20 30 20   Set *pRes to 0 
171c8 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 63  if the cursor ac
171c9 74 75 61 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f  tually points to
171ca 20 73 6f 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72   something.** or
171cb 20 73 65 74 20 2a 70 52 65 73 20 74 6f 20 31 20   set *pRes to 1 
171cc 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  if the table is 
171cd 65 6d 70 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  empty..*/.SQLITE
171ce 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
171cf 69 74 65 33 42 74 72 65 65 46 69 72 73 74 28 42  ite3BtreeFirst(B
171d0 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69  tCursor *pCur, i
171d1 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74  nt *pRes){.  int
171d2 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   rc;..  assert( 
171d3 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
171d4 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
171d5 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
171d6 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42 74  x_held(pCur->pBt
171d7 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  ree->db->mutex) 
171d8 29 3b 0a 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f  );.  rc = moveTo
171d9 52 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 69 66  Root(pCur);.  if
171da 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
171db 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d  ){.    if( pCur-
171dc 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
171dd 49 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20  INVALID ){.     
171de 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61   assert( pCur->a
171df 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
171e0 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a  e]->nCell==0 );.
171e1 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 31 3b        *pRes = 1;
171e2 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
171e3 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 65 6c 73 65  TE_OK;.    }else
171e4 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
171e5 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
171e6 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c  r->iPage]->nCell
171e7 3e 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65  >0 );.      *pRe
171e8 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20  s = 0;.      rc 
171e9 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74  = moveToLeftmost
171ea 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20  (pCur);.    }.  
171eb 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
171ec 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75  ../* Move the cu
171ed 72 73 6f 72 20 74 6f 20 74 68 65 20 6c 61 73 74  rsor to the last
171ee 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61   entry in the ta
171ef 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51 4c  ble.  Return SQL
171f0 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63  ITE_OK.** on suc
171f1 63 65 73 73 2e 20 20 53 65 74 20 2a 70 52 65 73  cess.  Set *pRes
171f2 20 74 6f 20 30 20 69 66 20 74 68 65 20 63 75 72   to 0 if the cur
171f3 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 70 6f 69  sor actually poi
171f4 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e 67  nts to something
171f5 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52 65 73  .** or set *pRes
171f6 20 74 6f 20 31 20 69 66 20 74 68 65 20 74 61 62   to 1 if the tab
171f7 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f 0a  le is empty..*/.
171f8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
171f9 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  nt sqlite3BtreeL
171fa 61 73 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43  ast(BtCursor *pC
171fb 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a  ur, int *pRes){.
171fc 20 20 69 6e 74 20 72 63 3b 0a 20 0a 20 20 61 73    int rc;. .  as
171fd 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
171fe 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
171ff 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
17200 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75  3_mutex_held(pCu
17201 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d  r->pBtree->db->m
17202 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 49  utex) );..  /* I
17203 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 6c 72  f the cursor alr
17204 65 61 64 79 20 70 6f 69 6e 74 73 20 74 6f 20 74  eady points to t
17205 68 65 20 6c 61 73 74 20 65 6e 74 72 79 2c 20 74  he last entry, t
17206 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20  his is a no-op. 
17207 2a 2f 0a 20 20 69 66 28 20 43 55 52 53 4f 52 5f  */.  if( CURSOR_
17208 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74  VALID==pCur->eSt
17209 61 74 65 20 26 26 20 70 43 75 72 2d 3e 61 74 4c  ate && pCur->atL
1720a 61 73 74 20 29 7b 0a 23 69 66 64 65 66 20 53 51  ast ){.#ifdef SQ
1720b 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 2f  LITE_DEBUG.    /
1720c 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 73 65 72  * This block ser
1720d 76 65 73 20 74 6f 20 61 73 73 65 72 74 28 29 20  ves to assert() 
1720e 74 68 61 74 20 74 68 65 20 63 75 72 73 6f 72 20  that the cursor 
1720f 72 65 61 6c 6c 79 20 64 6f 65 73 20 70 6f 69 6e  really does poin
17210 74 20 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65  t .    ** to the
17211 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e 20 74   last entry in t
17212 68 65 20 62 2d 74 72 65 65 2e 20 2a 2f 0a 20 20  he b-tree. */.  
17213 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 66 6f    int ii;.    fo
17214 72 28 69 69 3d 30 3b 20 69 69 3c 70 43 75 72 2d  r(ii=0; ii<pCur-
17215 3e 69 50 61 67 65 3b 20 69 69 2b 2b 29 7b 0a 20  >iPage; ii++){. 
17216 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75       assert( pCu
17217 72 2d 3e 61 69 49 64 78 5b 69 69 5d 3d 3d 70 43  r->aiIdx[ii]==pC
17218 75 72 2d 3e 61 70 50 61 67 65 5b 69 69 5d 2d 3e  ur->apPage[ii]->
17219 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 7d 0a 20  nCell );.    }. 
1721a 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d     assert( pCur-
1721b 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
1721c 67 65 5d 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67  ge]==pCur->apPag
1721d 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e  e[pCur->iPage]->
1721e 6e 43 65 6c 6c 2d 31 20 29 3b 0a 20 20 20 20 61  nCell-1 );.    a
1721f 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50  ssert( pCur->apP
17220 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
17221 2d 3e 6c 65 61 66 20 29 3b 0a 23 65 6e 64 69 66  ->leaf );.#endif
17222 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
17223 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 72 63  TE_OK;.  }..  rc
17224 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43   = moveToRoot(pC
17225 75 72 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  ur);.  if( rc==S
17226 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
17227 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c  if( CURSOR_INVAL
17228 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65  ID==pCur->eState
17229 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
1722a 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  ( pCur->apPage[p
1722b 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65  Cur->iPage]->nCe
1722c 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a  ll==0 );.      *
1722d 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 7d 65  pRes = 1;.    }e
1722e 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72  lse{.      asser
1722f 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
17230 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b  =CURSOR_VALID );
17231 0a 20 20 20 20 20 20 2a 70 52 65 73 20 3d 20 30  .      *pRes = 0
17232 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76  ;.      rc = mov
17233 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 70 43 75  eToRightmost(pCu
17234 72 29 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e  r);.      pCur->
17235 61 74 4c 61 73 74 20 3d 20 72 63 3d 3d 53 51 4c  atLast = rc==SQL
17236 49 54 45 5f 4f 4b 20 3f 31 3a 30 3b 0a 20 20 20  ITE_OK ?1:0;.   
17237 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
17238 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65 20 74  rc;.}../* Move t
17239 68 65 20 63 75 72 73 6f 72 20 73 6f 20 74 68 61  he cursor so tha
1723a 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 61  t it points to a
1723b 6e 20 65 6e 74 72 79 20 6e 65 61 72 20 74 68 65  n entry near the
1723c 20 6b 65 79 20 0a 2a 2a 20 73 70 65 63 69 66 69   key .** specifi
1723d 65 64 20 62 79 20 70 49 64 78 4b 65 79 20 6f 72  ed by pIdxKey or
1723e 20 69 6e 74 4b 65 79 2e 20 20 20 52 65 74 75 72   intKey.   Retur
1723f 6e 20 61 20 73 75 63 63 65 73 73 20 63 6f 64 65  n a success code
17240 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 49 4e 54 4b  ..**.** For INTK
17241 45 59 20 74 61 62 6c 65 73 2c 20 74 68 65 20 69  EY tables, the i
17242 6e 74 4b 65 79 20 70 61 72 61 6d 65 74 65 72 20  ntKey parameter 
17243 69 73 20 75 73 65 64 2e 20 20 70 49 64 78 4b 65  is used.  pIdxKe
17244 79 20 0a 2a 2a 20 6d 75 73 74 20 62 65 20 4e 55  y .** must be NU
17245 4c 4c 2e 20 20 46 6f 72 20 69 6e 64 65 78 20 74  LL.  For index t
17246 61 62 6c 65 73 2c 20 70 49 64 78 4b 65 79 20 69  ables, pIdxKey i
17247 73 20 75 73 65 64 20 61 6e 64 20 69 6e 74 4b 65  s used and intKe
17248 79 0a 2a 2a 20 69 73 20 69 67 6e 6f 72 65 64 2e  y.** is ignored.
17249 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 78 61  .**.** If an exa
1724a 63 74 20 6d 61 74 63 68 20 69 73 20 6e 6f 74 20  ct match is not 
1724b 66 6f 75 6e 64 2c 20 74 68 65 6e 20 74 68 65 20  found, then the 
1724c 63 75 72 73 6f 72 20 69 73 20 61 6c 77 61 79 73  cursor is always
1724d 0a 2a 2a 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e  .** left pointin
1724e 67 20 61 74 20 61 20 6c 65 61 66 20 70 61 67 65  g at a leaf page
1724f 20 77 68 69 63 68 20 77 6f 75 6c 64 20 68 6f 6c   which would hol
17250 64 20 74 68 65 20 65 6e 74 72 79 20 69 66 20 69  d the entry if i
17251 74 0a 2a 2a 20 77 65 72 65 20 70 72 65 73 65 6e  t.** were presen
17252 74 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d  t.  The cursor m
17253 69 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e  ight point to an
17254 20 65 6e 74 72 79 20 74 68 61 74 20 63 6f 6d 65   entry that come
17255 73 0a 2a 2a 20 62 65 66 6f 72 65 20 6f 72 20 61  s.** before or a
17256 66 74 65 72 20 74 68 65 20 6b 65 79 2e 0a 2a 2a  fter the key..**
17257 0a 2a 2a 20 41 6e 20 69 6e 74 65 67 65 72 20 69  .** An integer i
17258 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a  s written into *
17259 70 52 65 73 20 77 68 69 63 68 20 69 73 20 74 68  pRes which is th
1725a 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 63  e result of.** c
1725b 6f 6d 70 61 72 69 6e 67 20 74 68 65 20 6b 65 79  omparing the key
1725c 20 77 69 74 68 20 74 68 65 20 65 6e 74 72 79 20   with the entry 
1725d 74 6f 20 77 68 69 63 68 20 74 68 65 20 63 75 72  to which the cur
1725e 73 6f 72 20 69 73 20 0a 2a 2a 20 70 6f 69 6e 74  sor is .** point
1725f 69 6e 67 2e 20 20 54 68 65 20 6d 65 61 6e 69 6e  ing.  The meanin
17260 67 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72  g of the integer
17261 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a   written into.**
17262 20 2a 70 52 65 73 20 69 73 20 61 73 20 66 6f 6c   *pRes is as fol
17263 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  lows:.**.**     
17264 2a 70 52 65 73 3c 30 20 20 20 20 20 20 54 68 65  *pRes<0      The
17265 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20   cursor is left 
17266 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65  pointing at an e
17267 6e 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20  ntry that.**    
17268 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73                is
17269 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 69 6e   smaller than in
1726a 74 4b 65 79 2f 70 49 64 78 4b 65 79 20 6f 72 20  tKey/pIdxKey or 
1726b 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  if the table is 
1726c 65 6d 70 74 79 0a 2a 2a 20 20 20 20 20 20 20 20  empty.**        
1726d 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 74 68            and th
1726e 65 20 63 75 72 73 6f 72 20 69 73 20 74 68 65 72  e cursor is ther
1726f 65 66 6f 72 65 20 6c 65 66 74 20 70 6f 69 6e 74  efore left point
17270 20 74 6f 20 6e 6f 74 68 69 6e 67 2e 0a 2a 2a 0a   to nothing..**.
17271 2a 2a 20 20 20 20 20 2a 70 52 65 73 3d 3d 30 20  **     *pRes==0 
17272 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69      The cursor i
17273 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20  s left pointing 
17274 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74  at an entry that
17275 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
17276 20 20 20 20 20 65 78 61 63 74 6c 79 20 6d 61 74       exactly mat
17277 63 68 65 73 20 69 6e 74 4b 65 79 2f 70 49 64 78  ches intKey/pIdx
17278 4b 65 79 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a  Key..**.**     *
17279 70 52 65 73 3e 30 20 20 20 20 20 20 54 68 65 20  pRes>0      The 
1727a 63 75 72 73 6f 72 20 69 73 20 6c 65 66 74 20 70  cursor is left p
1727b 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e  ointing at an en
1727c 74 72 79 20 74 68 61 74 0a 2a 2a 20 20 20 20 20  try that.**     
1727d 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20               is 
1727e 6c 61 72 67 65 72 20 74 68 61 6e 20 69 6e 74 4b  larger than intK
1727f 65 79 2f 70 49 64 78 4b 65 79 2e 0a 2a 2a 0a 2a  ey/pIdxKey..**.*
17280 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
17281 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
17282 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28  eMovetoUnpacked(
17283 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75  .  BtCursor *pCu
17284 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  r,          /* T
17285 68 65 20 63 75 72 73 6f 72 20 74 6f 20 62 65 20  he cursor to be 
17286 6d 6f 76 65 64 20 2a 2f 0a 20 20 55 6e 70 61 63  moved */.  Unpac
17287 6b 65 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b  kedRecord *pIdxK
17288 65 79 2c 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20  ey, /* Unpacked 
17289 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 69  index key */.  i
1728a 36 34 20 69 6e 74 4b 65 79 2c 20 20 20 20 20 20  64 intKey,      
1728b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
1728c 61 62 6c 65 20 6b 65 79 20 2a 2f 0a 20 20 69 6e  able key */.  in
1728d 74 20 62 69 61 73 52 69 67 68 74 2c 20 20 20 20  t biasRight,    
1728e 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75         /* If tru
1728f 65 2c 20 62 69 61 73 20 74 68 65 20 73 65 61 72  e, bias the sear
17290 63 68 20 74 6f 20 74 68 65 20 68 69 67 68 20 65  ch to the high e
17291 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65  nd */.  int *pRe
17292 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s               
17293 20 2f 2a 20 57 72 69 74 65 20 73 65 61 72 63 68   /* Write search
17294 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f   results here */
17295 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  .){.  int rc;.. 
17296 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
17297 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
17298 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
17299 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
1729a 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62  pCur->pBtree->db
1729b 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
1729c 73 65 72 74 28 20 70 52 65 73 20 29 3b 0a 20 20  sert( pRes );.  
1729d 61 73 73 65 72 74 28 20 28 70 49 64 78 4b 65 79  assert( (pIdxKey
1729e 3d 3d 30 29 3d 3d 28 70 43 75 72 2d 3e 70 4b 65  ==0)==(pCur->pKe
1729f 79 49 6e 66 6f 3d 3d 30 29 20 29 3b 0a 0a 20 20  yInfo==0) );..  
172a0 2f 2a 20 49 66 20 74 68 65 20 63 75 72 73 6f 72  /* If the cursor
172a1 20 69 73 20 61 6c 72 65 61 64 79 20 70 6f 73 69   is already posi
172a2 74 69 6f 6e 65 64 20 61 74 20 74 68 65 20 70 6f  tioned at the po
172a3 69 6e 74 20 77 65 20 61 72 65 20 74 72 79 69 6e  int we are tryin
172a4 67 0a 20 20 2a 2a 20 74 6f 20 6d 6f 76 65 20 74  g.  ** to move t
172a5 6f 2c 20 74 68 65 6e 20 6a 75 73 74 20 72 65 74  o, then just ret
172a6 75 72 6e 20 77 69 74 68 6f 75 74 20 64 6f 69 6e  urn without doin
172a7 67 20 61 6e 79 20 77 6f 72 6b 20 2a 2f 0a 20 20  g any work */.  
172a8 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  if( pCur->eState
172a9 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 26  ==CURSOR_VALID &
172aa 26 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65  & pCur->validNKe
172ab 79 20 0a 20 20 20 26 26 20 70 43 75 72 2d 3e 61  y .   && pCur->a
172ac 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79  pPage[0]->intKey
172ad 20 0a 20 20 29 7b 0a 20 20 20 20 69 66 28 20 70   .  ){.    if( p
172ae 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d  Cur->info.nKey==
172af 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20  intKey ){.      
172b0 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20 20  *pRes = 0;.     
172b1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
172b2 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  K;.    }.    if(
172b3 20 70 43 75 72 2d 3e 61 74 4c 61 73 74 20 26 26   pCur->atLast &&
172b4 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79   pCur->info.nKey
172b5 3c 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20  <intKey ){.     
172b6 20 2a 70 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20   *pRes = -1;.   
172b7 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
172b8 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  _OK;.    }.  }..
172b9 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f    rc = moveToRoo
172ba 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72  t(pCur);.  if( r
172bb 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  c ){.    return 
172bc 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  rc;.  }.  assert
172bd 28 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  ( pCur->apPage[p
172be 43 75 72 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20  Cur->iPage] );. 
172bf 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61   assert( pCur->a
172c0 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
172c1 65 5d 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20  e]->isInit );.  
172c2 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70  assert( pCur->ap
172c3 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
172c4 5d 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 70 43  ]->nCell>0 || pC
172c5 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53  ur->eState==CURS
172c6 4f 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20 20  OR_INVALID );.  
172c7 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  if( pCur->eState
172c8 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  ==CURSOR_INVALID
172c9 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20   ){.    *pRes = 
172ca 2d 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  -1;.    assert( 
172cb 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
172cc 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c  r->iPage]->nCell
172cd 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72  ==0 );.    retur
172ce 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
172cf 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
172d0 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b  >apPage[0]->intK
172d1 65 79 20 7c 7c 20 70 49 64 78 4b 65 79 20 29 3b  ey || pIdxKey );
172d2 0a 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20  .  for(;;){.    
172d3 69 6e 74 20 6c 77 72 2c 20 75 70 72 3b 0a 20 20  int lwr, upr;.  
172d4 20 20 50 67 6e 6f 20 63 68 6c 64 50 67 3b 0a 20    Pgno chldPg;. 
172d5 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67     MemPage *pPag
172d6 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65  e = pCur->apPage
172d7 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20  [pCur->iPage];. 
172d8 20 20 20 69 6e 74 20 63 3b 0a 0a 20 20 20 20 2f     int c;..    /
172d9 2a 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 6d  * pPage->nCell m
172da 75 73 74 20 62 65 20 67 72 65 61 74 65 72 20 74  ust be greater t
172db 68 61 6e 20 7a 65 72 6f 2e 20 49 66 20 74 68 69  han zero. If thi
172dc 73 20 69 73 20 74 68 65 20 72 6f 6f 74 2d 70 61  s is the root-pa
172dd 67 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 75  ge.    ** the cu
172de 72 73 6f 72 20 77 6f 75 6c 64 20 68 61 76 65 20  rsor would have 
172df 62 65 65 6e 20 49 4e 56 41 4c 49 44 20 61 62 6f  been INVALID abo
172e0 76 65 20 61 6e 64 20 74 68 69 73 20 66 6f 72 28  ve and this for(
172e1 3b 3b 29 20 6c 6f 6f 70 0a 20 20 20 20 2a 2a 20  ;;) loop.    ** 
172e2 6e 6f 74 20 72 75 6e 2e 20 49 66 20 74 68 69 73  not run. If this
172e3 20 69 73 20 6e 6f 74 20 74 68 65 20 72 6f 6f 74   is not the root
172e4 2d 70 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20  -page, then the 
172e5 6d 6f 76 65 54 6f 43 68 69 6c 64 28 29 20 72 6f  moveToChild() ro
172e6 75 74 69 6e 65 0a 20 20 20 20 2a 2a 20 77 6f 75  utine.    ** wou
172e7 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 20  ld have already 
172e8 64 65 74 65 63 74 65 64 20 64 62 20 63 6f 72 72  detected db corr
172e9 75 70 74 69 6f 6e 2e 20 53 69 6d 69 6c 61 72 6c  uption. Similarl
172ea 79 2c 20 70 50 61 67 65 20 6d 75 73 74 0a 20 20  y, pPage must.  
172eb 20 20 2a 2a 20 62 65 20 74 68 65 20 72 69 67 68    ** be the righ
172ec 74 20 6b 69 6e 64 20 28 69 6e 64 65 78 20 6f 72  t kind (index or
172ed 20 74 61 62 6c 65 29 20 6f 66 20 62 2d 74 72 65   table) of b-tre
172ee 65 20 70 61 67 65 2e 20 4f 74 68 65 72 77 69 73  e page. Otherwis
172ef 65 0a 20 20 20 20 2a 2a 20 61 20 6d 6f 76 65 54  e.    ** a moveT
172f0 6f 43 68 69 6c 64 28 29 20 6f 72 20 6d 6f 76 65  oChild() or move
172f1 54 6f 52 6f 6f 74 28 29 20 63 61 6c 6c 20 77 6f  ToRoot() call wo
172f2 75 6c 64 20 68 61 76 65 20 64 65 74 65 63 74 65  uld have detecte
172f3 64 20 63 6f 72 72 75 70 74 69 6f 6e 2e 20 20 2a  d corruption.  *
172f4 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  /.    assert( pP
172f5 61 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a  age->nCell>0 );.
172f6 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
172f7 65 2d 3e 69 6e 74 4b 65 79 3d 3d 28 70 49 64 78  e->intKey==(pIdx
172f8 4b 65 79 3d 3d 30 29 20 29 3b 0a 20 20 20 20 6c  Key==0) );.    l
172f9 77 72 20 3d 20 30 3b 0a 20 20 20 20 75 70 72 20  wr = 0;.    upr 
172fa 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31  = pPage->nCell-1
172fb 3b 0a 20 20 20 20 69 66 28 20 62 69 61 73 52 69  ;.    if( biasRi
172fc 67 68 74 20 29 7b 0a 20 20 20 20 20 20 70 43 75  ght ){.      pCu
172fd 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
172fe 50 61 67 65 5d 20 3d 20 28 75 31 36 29 75 70 72  Page] = (u16)upr
172ff 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
17300 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70     pCur->aiIdx[p
17301 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 28 75  Cur->iPage] = (u
17302 31 36 29 28 28 75 70 72 2b 6c 77 72 29 2f 32 29  16)((upr+lwr)/2)
17303 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  ;.    }.    for(
17304 3b 3b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69  ;;){.      int i
17305 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78  dx = pCur->aiIdx
17306 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 20 2f  [pCur->iPage]; /
17307 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72 72 65  * Index of curre
17308 6e 74 20 63 65 6c 6c 20 69 6e 20 70 50 61 67 65  nt cell in pPage
17309 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 2a 70 43   */.      u8 *pC
1730a 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  ell;            
1730b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1730c 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 75 72 72   Pointer to curr
1730d 65 6e 74 20 63 65 6c 6c 20 69 6e 20 70 50 61 67  ent cell in pPag
1730e 65 20 2a 2f 0a 0a 20 20 20 20 20 20 70 43 75 72  e */..      pCur
1730f 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30  ->info.nSize = 0
17310 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d 20  ;.      pCell = 
17311 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20  findCell(pPage, 
17312 69 64 78 29 20 2b 20 70 50 61 67 65 2d 3e 63 68  idx) + pPage->ch
17313 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 20 20  ildPtrSize;.    
17314 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74    if( pPage->int
17315 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 69  Key ){.        i
17316 36 34 20 6e 43 65 6c 6c 4b 65 79 3b 0a 20 20 20  64 nCellKey;.   
17317 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e       if( pPage->
17318 68 61 73 44 61 74 61 20 29 7b 0a 20 20 20 20 20  hasData ){.     
17319 20 20 20 20 20 75 33 32 20 64 75 6d 6d 79 3b 0a       u32 dummy;.
1731a 20 20 20 20 20 20 20 20 20 20 70 43 65 6c 6c 20            pCell 
1731b 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 70  += getVarint32(p
1731c 43 65 6c 6c 2c 20 64 75 6d 6d 79 29 3b 0a 20 20  Cell, dummy);.  
1731d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1731e 67 65 74 56 61 72 69 6e 74 28 70 43 65 6c 6c 2c  getVarint(pCell,
1731f 20 28 75 36 34 2a 29 26 6e 43 65 6c 6c 4b 65 79   (u64*)&nCellKey
17320 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e  );.        if( n
17321 43 65 6c 6c 4b 65 79 3d 3d 69 6e 74 4b 65 79 20  CellKey==intKey 
17322 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d  ){.          c =
17323 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73   0;.        }els
17324 65 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 69  e if( nCellKey<i
17325 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20  ntKey ){.       
17326 20 20 20 63 20 3d 20 2d 31 3b 0a 20 20 20 20 20     c = -1;.     
17327 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
17328 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c      assert( nCel
17329 6c 4b 65 79 3e 69 6e 74 4b 65 79 20 29 3b 0a 20  lKey>intKey );. 
1732a 20 20 20 20 20 20 20 20 20 63 20 3d 20 2b 31 3b           c = +1;
1732b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1732c 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b     pCur->validNK
1732d 65 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ey = 1;.        
1732e 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 20  pCur->info.nKey 
1732f 3d 20 6e 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 20  = nCellKey;.    
17330 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
17331 20 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20   /* The maximum 
17332 73 75 70 70 6f 72 74 65 64 20 70 61 67 65 2d 73  supported page-s
17333 69 7a 65 20 69 73 20 33 32 37 36 38 20 62 79 74  ize is 32768 byt
17334 65 73 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 74  es. This means t
17335 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  hat.        ** t
17336 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
17337 72 20 6f 66 20 72 65 63 6f 72 64 20 62 79 74 65  r of record byte
17338 73 20 73 74 6f 72 65 64 20 6f 6e 20 61 6e 20 69  s stored on an i
17339 6e 64 65 78 20 42 2d 54 72 65 65 0a 20 20 20 20  ndex B-Tree.    
1733a 20 20 20 20 2a 2a 20 70 61 67 65 20 69 73 20 61      ** page is a
1733b 74 20 6d 6f 73 74 20 38 31 39 38 20 62 79 74 65  t most 8198 byte
1733c 73 2c 20 77 68 69 63 68 20 6d 61 79 20 62 65 20  s, which may be 
1733d 73 74 6f 72 65 64 20 61 73 20 61 20 32 2d 62 79  stored as a 2-by
1733e 74 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 76 61  te.        ** va
1733f 72 69 6e 74 2e 20 54 68 69 73 20 69 6e 66 6f 72  rint. This infor
17340 6d 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  mation is used t
17341 6f 20 61 74 74 65 6d 70 74 20 74 6f 20 61 76 6f  o attempt to avo
17342 69 64 20 70 61 72 73 69 6e 67 20 0a 20 20 20 20  id parsing .    
17343 20 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 69 72      ** the entir
17344 65 20 63 65 6c 6c 20 62 79 20 63 68 65 63 6b 69  e cell by checki
17345 6e 67 20 66 6f 72 20 74 68 65 20 63 61 73 65 73  ng for the cases
17346 20 77 68 65 72 65 20 74 68 65 20 72 65 63 6f 72   where the recor
17347 64 20 69 73 20 0a 20 20 20 20 20 20 20 20 2a 2a  d is .        **
17348 20 73 74 6f 72 65 64 20 65 6e 74 69 72 65 6c 79   stored entirely
17349 20 77 69 74 68 69 6e 20 74 68 65 20 62 2d 74 72   within the b-tr
1734a 65 65 20 70 61 67 65 20 62 79 20 69 6e 73 70 65  ee page by inspe
1734b 63 74 69 6e 67 20 74 68 65 20 66 69 72 73 74 20  cting the first 
1734c 0a 20 20 20 20 20 20 20 20 2a 2a 20 32 20 62 79  .        ** 2 by
1734d 74 65 73 20 6f 66 20 74 68 65 20 63 65 6c 6c 2e  tes of the cell.
1734e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
1734f 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20      int nCell = 
17350 70 43 65 6c 6c 5b 30 5d 3b 0a 20 20 20 20 20 20  pCell[0];.      
17351 20 20 69 66 28 20 21 28 6e 43 65 6c 6c 20 26 20    if( !(nCell & 
17352 30 78 38 30 29 20 26 26 20 6e 43 65 6c 6c 3c 3d  0x80) && nCell<=
17353 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20  pPage->maxLocal 
17354 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
17355 54 68 69 73 20 62 72 61 6e 63 68 20 72 75 6e 73  This branch runs
17356 20 69 66 20 74 68 65 20 72 65 63 6f 72 64 2d 73   if the record-s
17357 69 7a 65 20 66 69 65 6c 64 20 6f 66 20 74 68 65  ize field of the
17358 20 63 65 6c 6c 20 69 73 20 61 0a 20 20 20 20 20   cell is a.     
17359 20 20 20 20 20 2a 2a 20 73 69 6e 67 6c 65 20 62       ** single b
1735a 79 74 65 20 76 61 72 69 6e 74 20 61 6e 64 20 74  yte varint and t
1735b 68 65 20 72 65 63 6f 72 64 20 66 69 74 73 20 65  he record fits e
1735c 6e 74 69 72 65 6c 79 20 6f 6e 20 74 68 65 20 6d  ntirely on the m
1735d 61 69 6e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  ain.          **
1735e 20 62 2d 74 72 65 65 20 70 61 67 65 2e 20 20 2a   b-tree page.  *
1735f 2f 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20  /.          c = 
17360 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72  sqlite3VdbeRecor
17361 64 43 6f 6d 70 61 72 65 28 6e 43 65 6c 6c 2c 20  dCompare(nCell, 
17362 28 76 6f 69 64 2a 29 26 70 43 65 6c 6c 5b 31 5d  (void*)&pCell[1]
17363 2c 20 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20  , pIdxKey);.    
17364 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 28      }else if( !(
17365 70 43 65 6c 6c 5b 31 5d 20 26 20 30 78 38 30 29  pCell[1] & 0x80)
17366 20 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 28   .          && (
17367 6e 43 65 6c 6c 20 3d 20 28 28 6e 43 65 6c 6c 26  nCell = ((nCell&
17368 30 78 37 66 29 3c 3c 37 29 20 2b 20 70 43 65 6c  0x7f)<<7) + pCel
17369 6c 5b 31 5d 29 3c 3d 70 50 61 67 65 2d 3e 6d 61  l[1])<=pPage->ma
1736a 78 4c 6f 63 61 6c 0a 20 20 20 20 20 20 20 20 29  xLocal.        )
1736b 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  {.          /* T
1736c 68 65 20 72 65 63 6f 72 64 2d 73 69 7a 65 20 66  he record-size f
1736d 69 65 6c 64 20 69 73 20 61 20 32 20 62 79 74 65  ield is a 2 byte
1736e 20 76 61 72 69 6e 74 20 61 6e 64 20 74 68 65 20   varint and the 
1736f 72 65 63 6f 72 64 20 0a 20 20 20 20 20 20 20 20  record .        
17370 20 20 2a 2a 20 66 69 74 73 20 65 6e 74 69 72 65    ** fits entire
17371 6c 79 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20 62  ly on the main b
17372 2d 74 72 65 65 20 70 61 67 65 2e 20 20 2a 2f 0a  -tree page.  */.
17373 20 20 20 20 20 20 20 20 20 20 63 20 3d 20 73 71            c = sq
17374 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43  lite3VdbeRecordC
17375 6f 6d 70 61 72 65 28 6e 43 65 6c 6c 2c 20 28 76  ompare(nCell, (v
17376 6f 69 64 2a 29 26 70 43 65 6c 6c 5b 32 5d 2c 20  oid*)&pCell[2], 
17377 70 49 64 78 4b 65 79 29 3b 0a 20 20 20 20 20 20  pIdxKey);.      
17378 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
17379 20 20 20 2f 2a 20 54 68 65 20 72 65 63 6f 72 64     /* The record
1737a 20 66 6c 6f 77 73 20 6f 76 65 72 20 6f 6e 74 6f   flows over onto
1737b 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65   one or more ove
1737c 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 49 6e 0a  rflow pages. In.
1737d 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68 69            ** thi
1737e 73 20 63 61 73 65 20 74 68 65 20 77 68 6f 6c 65  s case the whole
1737f 20 63 65 6c 6c 20 6e 65 65 64 73 20 74 6f 20 62   cell needs to b
17380 65 20 70 61 72 73 65 64 2c 20 61 20 62 75 66 66  e parsed, a buff
17381 65 72 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 20  er allocated.   
17382 20 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 61 63         ** and ac
17383 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20 75 73  cessPayload() us
17384 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20 74  ed to retrieve t
17385 68 65 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74  he record into t
17386 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  he.          ** 
17387 62 75 66 66 65 72 20 62 65 66 6f 72 65 20 56 64  buffer before Vd
17388 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65 28  beRecordCompare(
17389 29 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 2e  ) can be called.
1738a 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 76 6f   */.          vo
1738b 69 64 20 2a 70 43 65 6c 6c 4b 65 79 3b 0a 20 20  id *pCellKey;.  
1738c 20 20 20 20 20 20 20 20 75 38 20 2a 20 63 6f 6e          u8 * con
1738d 73 74 20 70 43 65 6c 6c 42 6f 64 79 20 3d 20 70  st pCellBody = p
1738e 43 65 6c 6c 20 2d 20 70 50 61 67 65 2d 3e 63 68  Cell - pPage->ch
1738f 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20 20 20  ildPtrSize;.    
17390 20 20 20 20 20 20 62 74 72 65 65 50 61 72 73 65        btreeParse
17391 43 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70  CellPtr(pPage, p
17392 43 65 6c 6c 42 6f 64 79 2c 20 26 70 43 75 72 2d  CellBody, &pCur-
17393 3e 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20  >info);.        
17394 20 20 6e 43 65 6c 6c 20 3d 20 28 69 6e 74 29 70    nCell = (int)p
17395 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a  Cur->info.nKey;.
17396 20 20 20 20 20 20 20 20 20 20 70 43 65 6c 6c 4b            pCellK
17397 65 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  ey = sqlite3Mall
17398 6f 63 28 20 6e 43 65 6c 6c 20 29 3b 0a 20 20 20  oc( nCell );.   
17399 20 20 20 20 20 20 20 69 66 28 20 70 43 65 6c 6c         if( pCell
1739a 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Key==0 ){.      
1739b 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1739c 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 20  E_NOMEM;.       
1739d 20 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f       goto moveto
1739e 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20  _finish;.       
1739f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72     }.          r
173a0 63 20 3d 20 61 63 63 65 73 73 50 61 79 6c 6f 61  c = accessPayloa
173a1 64 28 70 43 75 72 2c 20 30 2c 20 6e 43 65 6c 6c  d(pCur, 0, nCell
173a2 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  , (unsigned char
173a3 2a 29 70 43 65 6c 6c 4b 65 79 2c 20 30 29 3b 0a  *)pCellKey, 0);.
173a4 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72 63            if( rc
173a5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
173a6 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 65  sqlite3_free(pCe
173a7 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20  llKey);.        
173a8 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f      goto moveto_
173a9 66 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 20  finish;.        
173aa 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 63 20    }.          c 
173ab 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63  = sqlite3VdbeRec
173ac 6f 72 64 43 6f 6d 70 61 72 65 28 6e 43 65 6c 6c  ordCompare(nCell
173ad 2c 20 70 43 65 6c 6c 4b 65 79 2c 20 70 49 64 78  , pCellKey, pIdx
173ae 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Key);.          
173af 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43 65  sqlite3_free(pCe
173b0 6c 6c 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20  llKey);.        
173b1 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
173b2 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20  if( c==0 ){.    
173b3 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69      if( pPage->i
173b4 6e 74 4b 65 79 20 26 26 20 21 70 50 61 67 65 2d  ntKey && !pPage-
173b5 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 20  >leaf ){.       
173b6 20 20 20 6c 77 72 20 3d 20 69 64 78 3b 0a 20 20     lwr = idx;.  
173b7 20 20 20 20 20 20 20 20 75 70 72 20 3d 20 6c 77          upr = lw
173b8 72 20 2d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  r - 1;.         
173b9 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
173ba 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
173bb 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20 20   *pRes = 0;.    
173bc 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
173bd 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20  E_OK;.          
173be 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69  goto moveto_fini
173bf 73 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  sh;.        }.  
173c0 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
173c1 63 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6c  c<0 ){.        l
173c2 77 72 20 3d 20 69 64 78 2b 31 3b 0a 20 20 20 20  wr = idx+1;.    
173c3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
173c4 20 75 70 72 20 3d 20 69 64 78 2d 31 3b 0a 20 20   upr = idx-1;.  
173c5 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
173c6 6c 77 72 3e 75 70 72 20 29 7b 0a 20 20 20 20 20  lwr>upr ){.     
173c7 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
173c8 7d 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69  }.      pCur->ai
173c9 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
173ca 20 3d 20 28 75 31 36 29 28 28 6c 77 72 2b 75 70   = (u16)((lwr+up
173cb 72 29 2f 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20  r)/2);.    }.   
173cc 20 61 73 73 65 72 74 28 20 6c 77 72 3d 3d 75 70   assert( lwr==up
173cd 72 2b 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72  r+1 );.    asser
173ce 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74  t( pPage->isInit
173cf 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67   );.    if( pPag
173d0 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20  e->leaf ){.     
173d1 20 63 68 6c 64 50 67 20 3d 20 30 3b 0a 20 20 20   chldPg = 0;.   
173d2 20 7d 65 6c 73 65 20 69 66 28 20 6c 77 72 3e 3d   }else if( lwr>=
173d3 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a  pPage->nCell ){.
173d4 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 67        chldPg = g
173d5 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e  et4byte(&pPage->
173d6 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72  aData[pPage->hdr
173d7 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20  Offset+8]);.    
173d8 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 68 6c  }else{.      chl
173d9 64 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 66  dPg = get4byte(f
173da 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 6c  indCell(pPage, l
173db 77 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  wr));.    }.    
173dc 69 66 28 20 63 68 6c 64 50 67 3d 3d 30 20 29 7b  if( chldPg==0 ){
173dd 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
173de 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
173df 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70  >iPage]<pCur->ap
173e0 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
173e1 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20  ]->nCell );.    
173e2 20 20 2a 70 52 65 73 20 3d 20 63 3b 0a 20 20 20    *pRes = c;.   
173e3 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
173e4 4b 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f  K;.      goto mo
173e5 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20  veto_finish;.   
173e6 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49   }.    pCur->aiI
173e7 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20  dx[pCur->iPage] 
173e8 3d 20 28 75 31 36 29 6c 77 72 3b 0a 20 20 20 20  = (u16)lwr;.    
173e9 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
173ea 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e   = 0;.    pCur->
173eb 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20  validNKey = 0;. 
173ec 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68     rc = moveToCh
173ed 69 6c 64 28 70 43 75 72 2c 20 63 68 6c 64 50 67  ild(pCur, chldPg
173ee 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20  );.    if( rc ) 
173ef 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e 69  goto moveto_fini
173f0 73 68 3b 0a 20 20 7d 0a 6d 6f 76 65 74 6f 5f 66  sh;.  }.moveto_f
173f1 69 6e 69 73 68 3a 0a 20 20 72 65 74 75 72 6e 20  inish:.  return 
173f2 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65  rc;.}.../*.** Re
173f3 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65  turn TRUE if the
173f4 20 63 75 72 73 6f 72 20 69 73 20 6e 6f 74 20 70   cursor is not p
173f5 6f 69 6e 74 69 6e 67 20 61 74 20 61 6e 20 65 6e  ointing at an en
173f6 74 72 79 20 6f 66 20 74 68 65 20 74 61 62 6c 65  try of the table
173f7 2e 0a 2a 2a 0a 2a 2a 20 54 52 55 45 20 77 69 6c  ..**.** TRUE wil
173f8 6c 20 62 65 20 72 65 74 75 72 6e 65 64 20 61 66  l be returned af
173f9 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  ter a call to sq
173fa 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28 29  lite3BtreeNext()
173fb 20 6d 6f 76 65 73 0a 2a 2a 20 70 61 73 74 20 74   moves.** past t
173fc 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e  he last entry in
173fd 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 73 71   the table or sq
173fe 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 28 29  lite3BtreePrev()
173ff 20 6d 6f 76 65 73 20 70 61 73 74 0a 2a 2a 20 74   moves past.** t
17400 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 2e 20  he first entry. 
17401 20 54 52 55 45 20 69 73 20 61 6c 73 6f 20 72 65   TRUE is also re
17402 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 74 61  turned if the ta
17403 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f  ble is empty..*/
17404 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
17405 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
17406 45 6f 66 28 42 74 43 75 72 73 6f 72 20 2a 70 43  Eof(BtCursor *pC
17407 75 72 29 7b 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20  ur){.  /* TODO: 
17408 57 68 61 74 20 69 66 20 74 68 65 20 63 75 72 73  What if the curs
17409 6f 72 20 69 73 20 69 6e 20 43 55 52 53 4f 52 5f  or is in CURSOR_
1740a 52 45 51 55 49 52 45 53 45 45 4b 20 62 75 74 20  REQUIRESEEK but 
1740b 61 6c 6c 20 74 61 62 6c 65 20 65 6e 74 72 69 65  all table entrie
1740c 73 0a 20 20 2a 2a 20 68 61 76 65 20 62 65 65 6e  s.  ** have been
1740d 20 64 65 6c 65 74 65 64 3f 20 54 68 69 73 20 41   deleted? This A
1740e 50 49 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20  PI will need to 
1740f 63 68 61 6e 67 65 20 74 6f 20 72 65 74 75 72 6e  change to return
17410 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 0a 20   an error code. 
17411 20 2a 2a 20 61 73 20 77 65 6c 6c 20 61 73 20 74   ** as well as t
17412 68 65 20 62 6f 6f 6c 65 61 6e 20 72 65 73 75 6c  he boolean resul
17413 74 20 76 61 6c 75 65 2e 0a 20 20 2a 2f 0a 20 20  t value..  */.  
17414 72 65 74 75 72 6e 20 28 43 55 52 53 4f 52 5f 56  return (CURSOR_V
17415 41 4c 49 44 21 3d 70 43 75 72 2d 3e 65 53 74 61  ALID!=pCur->eSta
17416 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  te);.}../*.** Ad
17417 76 61 6e 63 65 20 74 68 65 20 63 75 72 73 6f 72  vance the cursor
17418 20 74 6f 20 74 68 65 20 6e 65 78 74 20 65 6e 74   to the next ent
17419 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ry in the databa
1741a 73 65 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63 65  se.  If.** succe
1741b 73 73 66 75 6c 20 74 68 65 6e 20 73 65 74 20 2a  ssful then set *
1741c 70 52 65 73 3d 30 2e 20 20 49 66 20 74 68 65 20  pRes=0.  If the 
1741d 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61 6c  cursor.** was al
1741e 72 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20 74  ready pointing t
1741f 6f 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79  o the last entry
17420 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
17421 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69 73 20   before.** this 
17422 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c  routine was call
17423 65 64 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 52  ed, then set *pR
17424 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  es=1..*/.SQLITE_
17425 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
17426 74 65 33 42 74 72 65 65 4e 65 78 74 28 42 74 43  te3BtreeNext(BtC
17427 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74  ursor *pCur, int
17428 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72   *pRes){.  int r
17429 63 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 20  c;.  int idx;.  
1742a 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a  MemPage *pPage;.
1742b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
1742c 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
1742d 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74  ) );.  rc = rest
1742e 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  oreCursorPositio
1742f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72  n(pCur);.  if( r
17430 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
17431 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
17432 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 52 65   }.  assert( pRe
17433 73 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 43 55  s!=0 );.  if( CU
17434 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43  RSOR_INVALID==pC
17435 75 72 2d 3e 65 53 74 61 74 65 20 29 7b 0a 20 20  ur->eState ){.  
17436 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20    *pRes = 1;.   
17437 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
17438 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 75  K;.  }.  if( pCu
17439 72 2d 3e 73 6b 69 70 4e 65 78 74 3e 30 20 29 7b  r->skipNext>0 ){
1743a 0a 20 20 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e  .    pCur->skipN
1743b 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 2a 70 52  ext = 0;.    *pR
1743c 65 73 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75  es = 0;.    retu
1743d 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
1743e 7d 0a 20 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65  }.  pCur->skipNe
1743f 78 74 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67 65  xt = 0;..  pPage
17440 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
17441 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20  pCur->iPage];.  
17442 69 64 78 20 3d 20 2b 2b 70 43 75 72 2d 3e 61 69  idx = ++pCur->ai
17443 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
17444 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
17445 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 61  e->isInit );.  a
17446 73 73 65 72 74 28 20 69 64 78 3c 3d 70 50 61 67  ssert( idx<=pPag
17447 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20 70  e->nCell );..  p
17448 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20  Cur->info.nSize 
17449 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c  = 0;.  pCur->val
1744a 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66  idNKey = 0;.  if
1744b 28 20 69 64 78 3e 3d 70 50 61 67 65 2d 3e 6e 43  ( idx>=pPage->nC
1744c 65 6c 6c 20 29 7b 0a 20 20 20 20 69 66 28 20 21  ell ){.    if( !
1744d 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20  pPage->leaf ){. 
1744e 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f       rc = moveTo
1744f 43 68 69 6c 64 28 70 43 75 72 2c 20 67 65 74 34  Child(pCur, get4
17450 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61  byte(&pPage->aDa
17451 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66  ta[pPage->hdrOff
17452 73 65 74 2b 38 5d 29 29 3b 0a 20 20 20 20 20 20  set+8]));.      
17453 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
17454 72 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d  rc;.      rc = m
17455 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70 43  oveToLeftmost(pC
17456 75 72 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73  ur);.      *pRes
17457 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75   = 0;.      retu
17458 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
17459 20 64 6f 7b 0a 20 20 20 20 20 20 69 66 28 20 70   do{.      if( p
1745a 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b  Cur->iPage==0 ){
1745b 0a 20 20 20 20 20 20 20 20 2a 70 52 65 73 20 3d  .        *pRes =
1745c 20 31 3b 0a 20 20 20 20 20 20 20 20 70 43 75 72   1;.        pCur
1745d 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f  ->eState = CURSO
1745e 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20  R_INVALID;.     
1745f 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
17460 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  _OK;.      }.   
17461 20 20 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28     moveToParent(
17462 70 43 75 72 29 3b 0a 20 20 20 20 20 20 70 50 61  pCur);.      pPa
17463 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  ge = pCur->apPag
17464 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a  e[pCur->iPage];.
17465 20 20 20 20 7d 77 68 69 6c 65 28 20 70 43 75 72      }while( pCur
17466 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
17467 61 67 65 5d 3e 3d 70 50 61 67 65 2d 3e 6e 43 65  age]>=pPage->nCe
17468 6c 6c 20 29 3b 0a 20 20 20 20 2a 70 52 65 73 20  ll );.    *pRes 
17469 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 70 50 61  = 0;.    if( pPa
1746a 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20  ge->intKey ){.  
1746b 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1746c 42 74 72 65 65 4e 65 78 74 28 70 43 75 72 2c 20  BtreeNext(pCur, 
1746d 70 52 65 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65  pRes);.    }else
1746e 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
1746f 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20  ITE_OK;.    }.  
17470 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
17471 0a 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20  .  *pRes = 0;.  
17472 69 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20  if( pPage->leaf 
17473 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
17474 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72  LITE_OK;.  }.  r
17475 63 20 3d 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f  c = moveToLeftmo
17476 73 74 28 70 43 75 72 29 3b 0a 20 20 72 65 74 75  st(pCur);.  retu
17477 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  rn rc;.}.../*.**
17478 20 53 74 65 70 20 74 68 65 20 63 75 72 73 6f 72   Step the cursor
17479 20 74 6f 20 74 68 65 20 62 61 63 6b 20 74 6f 20   to the back to 
1747a 74 68 65 20 70 72 65 76 69 6f 75 73 20 65 6e 74  the previous ent
1747b 72 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ry in the databa
1747c 73 65 2e 20 20 49 66 0a 2a 2a 20 73 75 63 63 65  se.  If.** succe
1747d 73 73 66 75 6c 20 74 68 65 6e 20 73 65 74 20 2a  ssful then set *
1747e 70 52 65 73 3d 30 2e 20 20 49 66 20 74 68 65 20  pRes=0.  If the 
1747f 63 75 72 73 6f 72 0a 2a 2a 20 77 61 73 20 61 6c  cursor.** was al
17480 72 65 61 64 79 20 70 6f 69 6e 74 69 6e 67 20 74  ready pointing t
17481 6f 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72  o the first entr
17482 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  y in the databas
17483 65 20 62 65 66 6f 72 65 0a 2a 2a 20 74 68 69 73  e before.** this
17484 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c   routine was cal
17485 6c 65 64 2c 20 74 68 65 6e 20 73 65 74 20 2a 70  led, then set *p
17486 52 65 73 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45  Res=1..*/.SQLITE
17487 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
17488 69 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75  ite3BtreePreviou
17489 73 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  s(BtCursor *pCur
1748a 2c 20 69 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20  , int *pRes){.  
1748b 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67  int rc;.  MemPag
1748c 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73  e *pPage;..  ass
1748d 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
1748e 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
1748f 20 72 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72   rc = restoreCur
17490 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72  sorPosition(pCur
17491 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
17492 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
17493 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70  turn rc;.  }.  p
17494 43 75 72 2d 3e 61 74 4c 61 73 74 20 3d 20 30 3b  Cur->atLast = 0;
17495 0a 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49 4e  .  if( CURSOR_IN
17496 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74  VALID==pCur->eSt
17497 61 74 65 20 29 7b 0a 20 20 20 20 2a 70 52 65 73  ate ){.    *pRes
17498 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 1;.    return
17499 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
1749a 20 20 69 66 28 20 70 43 75 72 2d 3e 73 6b 69 70    if( pCur->skip
1749b 4e 65 78 74 3c 30 20 29 7b 0a 20 20 20 20 70 43  Next<0 ){.    pC
1749c 75 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30  ur->skipNext = 0
1749d 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30 3b  ;.    *pRes = 0;
1749e 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1749f 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 43 75  TE_OK;.  }.  pCu
174a0 72 2d 3e 73 6b 69 70 4e 65 78 74 20 3d 20 30 3b  r->skipNext = 0;
174a1 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75 72  ..  pPage = pCur
174a2 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
174a3 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74 28  Page];.  assert(
174a4 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29   pPage->isInit )
174a5 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d 3e  ;.  if( !pPage->
174a6 6c 65 61 66 20 29 7b 0a 20 20 20 20 69 6e 74 20  leaf ){.    int 
174a7 69 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64  idx = pCur->aiId
174a8 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a  x[pCur->iPage];.
174a9 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43      rc = moveToC
174aa 68 69 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62  hild(pCur, get4b
174ab 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50 61  yte(findCell(pPa
174ac 67 65 2c 20 69 64 78 29 29 29 3b 0a 20 20 20 20  ge, idx)));.    
174ad 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
174ae 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
174af 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f  .    rc = moveTo
174b0 52 69 67 68 74 6d 6f 73 74 28 70 43 75 72 29 3b  Rightmost(pCur);
174b1 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68  .  }else{.    wh
174b2 69 6c 65 28 20 70 43 75 72 2d 3e 61 69 49 64 78  ile( pCur->aiIdx
174b3 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3d 3d 30  [pCur->iPage]==0
174b4 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43   ){.      if( pC
174b5 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b 0a  ur->iPage==0 ){.
174b6 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 65 53          pCur->eS
174b7 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49 4e  tate = CURSOR_IN
174b8 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 20 20 2a  VALID;.        *
174b9 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20  pRes = 1;.      
174ba 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
174bb 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  OK;.      }.    
174bc 20 20 6d 6f 76 65 54 6f 50 61 72 65 6e 74 28 70    moveToParent(p
174bd 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Cur);.    }.    
174be 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
174bf 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e   = 0;.    pCur->
174c0 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 0a  validNKey = 0;..
174c1 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b      pCur->aiIdx[
174c2 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 2d 3b 0a  pCur->iPage]--;.
174c3 20 20 20 20 70 50 61 67 65 20 3d 20 70 43 75 72      pPage = pCur
174c4 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69  ->apPage[pCur->i
174c5 50 61 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 70  Page];.    if( p
174c6 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20  Page->intKey && 
174c7 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
174c8 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
174c9 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28  e3BtreePrevious(
174ca 70 43 75 72 2c 20 70 52 65 73 29 3b 0a 20 20 20  pCur, pRes);.   
174cb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
174cc 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
174cd 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 52 65 73 20    }.  }.  *pRes 
174ce 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  = 0;.  return rc
174cf 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  ;.}../*.** Alloc
174d0 61 74 65 20 61 20 6e 65 77 20 70 61 67 65 20 66  ate a new page f
174d1 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65  rom the database
174d2 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   file..**.** The
174d3 20 6e 65 77 20 70 61 67 65 20 69 73 20 6d 61 72   new page is mar
174d4 6b 65 64 20 61 73 20 64 69 72 74 79 2e 20 20 28  ked as dirty.  (
174d5 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
174d6 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
174d7 65 28 29 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61  e().** has alrea
174d8 64 79 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 6f  dy been called o
174d9 6e 20 74 68 65 20 6e 65 77 20 70 61 67 65 2e 29  n the new page.)
174da 20 20 54 68 65 20 6e 65 77 20 70 61 67 65 20 68    The new page h
174db 61 73 20 61 6c 73 6f 0a 2a 2a 20 62 65 65 6e 20  as also.** been 
174dc 72 65 66 65 72 65 6e 63 65 64 20 61 6e 64 20 74  referenced and t
174dd 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69  he calling routi
174de 6e 65 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  ne is responsibl
174df 65 20 66 6f 72 20 63 61 6c 6c 69 6e 67 0a 2a 2a  e for calling.**
174e0 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
174e1 65 66 28 29 20 6f 6e 20 74 68 65 20 6e 65 77 20  ef() on the new 
174e2 70 61 67 65 20 77 68 65 6e 20 69 74 20 69 73 20  page when it is 
174e3 64 6f 6e 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49  done..**.** SQLI
174e4 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
174e5 64 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 41  d on success.  A
174e6 6e 79 20 6f 74 68 65 72 20 72 65 74 75 72 6e 20  ny other return 
174e7 76 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 0a  value indicates.
174e8 2a 2a 20 61 6e 20 65 72 72 6f 72 2e 20 20 2a 70  ** an error.  *p
174e9 70 50 61 67 65 20 61 6e 64 20 2a 70 50 67 6e 6f  pPage and *pPgno
174ea 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20 69   are undefined i
174eb 6e 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61  n the event of a
174ec 6e 20 65 72 72 6f 72 2e 0a 2a 2a 20 44 6f 20 6e  n error..** Do n
174ed 6f 74 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65  ot invoke sqlite
174ee 33 50 61 67 65 72 55 6e 72 65 66 28 29 20 6f 6e  3PagerUnref() on
174ef 20 2a 70 70 50 61 67 65 20 69 66 20 61 6e 20 65   *ppPage if an e
174f0 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65 64  rror is returned
174f1 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22  ..**.** If the "
174f2 6e 65 61 72 62 79 22 20 70 61 72 61 6d 65 74 65  nearby" paramete
174f3 72 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e  r is not 0, then
174f4 20 61 20 28 66 65 65 62 6c 65 29 20 65 66 66 6f   a (feeble) effo
174f5 72 74 20 69 73 20 6d 61 64 65 20 74 6f 20 0a 2a  rt is made to .*
174f6 2a 20 6c 6f 63 61 74 65 20 61 20 70 61 67 65 20  * locate a page 
174f7 63 6c 6f 73 65 20 74 6f 20 74 68 65 20 70 61 67  close to the pag
174f8 65 20 6e 75 6d 62 65 72 20 22 6e 65 61 72 62 79  e number "nearby
174f9 22 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20  ".  This can be 
174fa 75 73 65 64 20 69 6e 20 61 6e 0a 2a 2a 20 61 74  used in an.** at
174fb 74 65 6d 70 74 20 74 6f 20 6b 65 65 70 20 72 65  tempt to keep re
174fc 6c 61 74 65 64 20 70 61 67 65 73 20 63 6c 6f 73  lated pages clos
174fd 65 20 74 6f 20 65 61 63 68 20 6f 74 68 65 72 20  e to each other 
174fe 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
174ff 66 69 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20 69  file,.** which i
17500 6e 20 74 75 72 6e 20 63 61 6e 20 6d 61 6b 65 20  n turn can make 
17501 64 61 74 61 62 61 73 65 20 61 63 63 65 73 73 20  database access 
17502 66 61 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66  faster..**.** If
17503 20 74 68 65 20 22 65 78 61 63 74 22 20 70 61 72   the "exact" par
17504 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 30 2c  ameter is not 0,
17505 20 61 6e 64 20 74 68 65 20 70 61 67 65 2d 6e 75   and the page-nu
17506 6d 62 65 72 20 6e 65 61 72 62 79 20 65 78 69 73  mber nearby exis
17507 74 73 20 0a 2a 2a 20 61 6e 79 77 68 65 72 65 20  ts .** anywhere 
17508 6f 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74  on the free-list
17509 2c 20 74 68 65 6e 20 69 74 20 69 73 20 67 75 61  , then it is gua
1750a 72 65 6e 74 65 65 64 20 74 6f 20 62 65 20 72 65  renteed to be re
1750b 74 75 72 6e 65 64 2e 20 54 68 69 73 0a 2a 2a 20  turned. This.** 
1750c 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79 20  is only used by 
1750d 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61  auto-vacuum data
1750e 62 61 73 65 73 20 77 68 65 6e 20 61 6c 6c 6f 63  bases when alloc
1750f 61 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62 6c  ating a new tabl
17510 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
17511 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61   allocateBtreePa
17512 67 65 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a  ge(.  BtShared *
17513 70 42 74 2c 20 0a 20 20 4d 65 6d 50 61 67 65 20  pBt, .  MemPage 
17514 2a 2a 70 70 50 61 67 65 2c 20 0a 20 20 50 67 6e  **ppPage, .  Pgn
17515 6f 20 2a 70 50 67 6e 6f 2c 20 0a 20 20 50 67 6e  o *pPgno, .  Pgn
17516 6f 20 6e 65 61 72 62 79 2c 0a 20 20 75 38 20 65  o nearby,.  u8 e
17517 78 61 63 74 0a 29 7b 0a 20 20 4d 65 6d 50 61 67  xact.){.  MemPag
17518 65 20 2a 70 50 61 67 65 31 3b 0a 20 20 69 6e 74  e *pPage1;.  int
17519 20 72 63 3b 0a 20 20 75 33 32 20 6e 3b 20 20 20   rc;.  u32 n;   
1751a 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70    /* Number of p
1751b 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65  ages on the free
1751c 6c 69 73 74 20 2a 2f 0a 20 20 75 33 32 20 6b 3b  list */.  u32 k;
1751d 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1751e 66 20 6c 65 61 76 65 73 20 6f 6e 20 74 68 65 20  f leaves on the 
1751f 74 72 75 6e 6b 20 6f 66 20 74 68 65 20 66 72 65  trunk of the fre
17520 65 6c 69 73 74 20 2a 2f 0a 20 20 4d 65 6d 50 61  elist */.  MemPa
17521 67 65 20 2a 70 54 72 75 6e 6b 20 3d 20 30 3b 0a  ge *pTrunk = 0;.
17522 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 72 65 76    MemPage *pPrev
17523 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 50 67 6e  Trunk = 0;.  Pgn
17524 6f 20 6d 78 50 61 67 65 3b 20 20 20 20 20 2f 2a  o mxPage;     /*
17525 20 54 6f 74 61 6c 20 73 69 7a 65 20 6f 66 20 74   Total size of t
17526 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
17527 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73   */..  assert( s
17528 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
17529 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
1752a 0a 20 20 70 50 61 67 65 31 20 3d 20 70 42 74 2d  .  pPage1 = pBt-
1752b 3e 70 50 61 67 65 31 3b 0a 20 20 6d 78 50 61 67  >pPage1;.  mxPag
1752c 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f 75  e = pagerPagecou
1752d 6e 74 28 70 42 74 29 3b 0a 20 20 6e 20 3d 20 67  nt(pBt);.  n = g
1752e 65 74 34 62 79 74 65 28 26 70 50 61 67 65 31 2d  et4byte(&pPage1-
1752f 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20 20 74  >aData[36]);.  t
17530 65 73 74 63 61 73 65 28 20 6e 3d 3d 6d 78 50 61  estcase( n==mxPa
17531 67 65 2d 31 20 29 3b 0a 20 20 69 66 28 20 6e 3e  ge-1 );.  if( n>
17532 3d 6d 78 50 61 67 65 20 29 7b 0a 20 20 20 20 72  =mxPage ){.    r
17533 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
17534 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20  RUPT_BKPT;.  }. 
17535 20 69 66 28 20 6e 3e 30 20 29 7b 0a 20 20 20 20   if( n>0 ){.    
17536 2f 2a 20 54 68 65 72 65 20 61 72 65 20 70 61 67  /* There are pag
17537 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69  es on the freeli
17538 73 74 2e 20 20 52 65 75 73 65 20 6f 6e 65 20 6f  st.  Reuse one o
17539 66 20 74 68 6f 73 65 20 70 61 67 65 73 2e 20 2a  f those pages. *
1753a 2f 0a 20 20 20 20 50 67 6e 6f 20 69 54 72 75 6e  /.    Pgno iTrun
1753b 6b 3b 0a 20 20 20 20 75 38 20 73 65 61 72 63 68  k;.    u8 search
1753c 4c 69 73 74 20 3d 20 30 3b 20 2f 2a 20 49 66 20  List = 0; /* If 
1753d 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 6d 75  the free-list mu
1753e 73 74 20 62 65 20 73 65 61 72 63 68 65 64 20 66  st be searched f
1753f 6f 72 20 27 6e 65 61 72 62 79 27 20 2a 2f 0a 20  or 'nearby' */. 
17540 20 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68     .    /* If th
17541 65 20 27 65 78 61 63 74 27 20 70 61 72 61 6d 65  e 'exact' parame
17542 74 65 72 20 77 61 73 20 74 72 75 65 20 61 6e 64  ter was true and
17543 20 61 20 71 75 65 72 79 20 6f 66 20 74 68 65 20   a query of the 
17544 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20  pointer-map.    
17545 2a 2a 20 73 68 6f 77 73 20 74 68 61 74 20 74 68  ** shows that th
17546 65 20 70 61 67 65 20 27 6e 65 61 72 62 79 27 20  e page 'nearby' 
17547 69 73 20 73 6f 6d 65 77 68 65 72 65 20 6f 6e 20  is somewhere on 
17548 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2c 20 74  the free-list, t
17549 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 65  hen.    ** the e
1754a 6e 74 69 72 65 2d 6c 69 73 74 20 77 69 6c 6c 20  ntire-list will 
1754b 62 65 20 73 65 61 72 63 68 65 64 20 66 6f 72 20  be searched for 
1754c 74 68 61 74 20 70 61 67 65 2e 0a 20 20 20 20 2a  that page..    *
1754d 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1754e 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
1754f 0a 20 20 20 20 69 66 28 20 65 78 61 63 74 20 26  .    if( exact &
17550 26 20 6e 65 61 72 62 79 3c 3d 6d 78 50 61 67 65  & nearby<=mxPage
17551 20 29 7b 0a 20 20 20 20 20 20 75 38 20 65 54 79   ){.      u8 eTy
17552 70 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  pe;.      assert
17553 28 20 6e 65 61 72 62 79 3e 30 20 29 3b 0a 20 20  ( nearby>0 );.  
17554 20 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d      assert( pBt-
17555 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20  >autoVacuum );. 
17556 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70       rc = ptrmap
17557 47 65 74 28 70 42 74 2c 20 6e 65 61 72 62 79 2c  Get(pBt, nearby,
17558 20 26 65 54 79 70 65 2c 20 30 29 3b 0a 20 20 20   &eType, 0);.   
17559 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
1755a 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 69 66 28  rn rc;.      if(
1755b 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46   eType==PTRMAP_F
1755c 52 45 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20  REEPAGE ){.     
1755d 20 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20     searchList = 
1755e 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  1;.      }.     
1755f 20 2a 70 50 67 6e 6f 20 3d 20 6e 65 61 72 62 79   *pPgno = nearby
17560 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  ;.    }.#endif..
17561 20 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74      /* Decrement
17562 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 63   the free-list c
17563 6f 75 6e 74 20 62 79 20 31 2e 20 53 65 74 20 69  ount by 1. Set i
17564 54 72 75 6e 6b 20 74 6f 20 74 68 65 20 69 6e 64  Trunk to the ind
17565 65 78 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a  ex of the.    **
17566 20 66 69 72 73 74 20 66 72 65 65 2d 6c 69 73 74   first free-list
17567 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 69 50 72   trunk page. iPr
17568 65 76 54 72 75 6e 6b 20 69 73 20 69 6e 69 74 69  evTrunk is initi
17569 61 6c 6c 79 20 31 2e 0a 20 20 20 20 2a 2f 0a 20  ally 1..    */. 
1756a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
1756b 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65 31  agerWrite(pPage1
1756c 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
1756d 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
1756e 72 63 3b 0a 20 20 20 20 70 75 74 34 62 79 74 65  rc;.    put4byte
1756f 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b  (&pPage1->aData[
17570 33 36 5d 2c 20 6e 2d 31 29 3b 0a 0a 20 20 20 20  36], n-1);..    
17571 2f 2a 20 54 68 65 20 63 6f 64 65 20 77 69 74 68  /* The code with
17572 69 6e 20 74 68 69 73 20 6c 6f 6f 70 20 69 73 20  in this loop is 
17573 72 75 6e 20 6f 6e 6c 79 20 6f 6e 63 65 20 69 66  run only once if
17574 20 74 68 65 20 27 73 65 61 72 63 68 4c 69 73 74   the 'searchList
17575 27 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20 2a  ' variable.    *
17576 2a 20 69 73 20 6e 6f 74 20 74 72 75 65 2e 20 4f  * is not true. O
17577 74 68 65 72 77 69 73 65 2c 20 69 74 20 72 75 6e  therwise, it run
17578 73 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20  s once for each 
17579 74 72 75 6e 6b 2d 70 61 67 65 20 6f 6e 20 74 68  trunk-page on th
1757a 65 0a 20 20 20 20 2a 2a 20 66 72 65 65 2d 6c 69  e.    ** free-li
1757b 73 74 20 75 6e 74 69 6c 20 74 68 65 20 70 61 67  st until the pag
1757c 65 20 27 6e 65 61 72 62 79 27 20 69 73 20 6c 6f  e 'nearby' is lo
1757d 63 61 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  cated..    */.  
1757e 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 70 50 72    do {.      pPr
1757f 65 76 54 72 75 6e 6b 20 3d 20 70 54 72 75 6e 6b  evTrunk = pTrunk
17580 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 72 65  ;.      if( pPre
17581 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20  vTrunk ){.      
17582 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 62    iTrunk = get4b
17583 79 74 65 28 26 70 50 72 65 76 54 72 75 6e 6b 2d  yte(&pPrevTrunk-
17584 3e 61 44 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20  >aData[0]);.    
17585 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
17586 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79   iTrunk = get4by
17587 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74  te(&pPage1->aDat
17588 61 5b 33 32 5d 29 3b 0a 20 20 20 20 20 20 7d 0a  a[32]);.      }.
17589 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1758a 69 54 72 75 6e 6b 3d 3d 6d 78 50 61 67 65 20 29  iTrunk==mxPage )
1758b 3b 0a 20 20 20 20 20 20 69 66 28 20 69 54 72 75  ;.      if( iTru
1758c 6e 6b 3e 6d 78 50 61 67 65 20 29 7b 0a 20 20 20  nk>mxPage ){.   
1758d 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1758e 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
1758f 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
17590 20 20 20 20 72 63 20 3d 20 62 74 72 65 65 47 65      rc = btreeGe
17591 74 50 61 67 65 28 70 42 74 2c 20 69 54 72 75 6e  tPage(pBt, iTrun
17592 6b 2c 20 26 70 54 72 75 6e 6b 2c 20 30 29 3b 0a  k, &pTrunk, 0);.
17593 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
17594 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  ( rc ){.        
17595 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20  pTrunk = 0;.    
17596 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c      goto end_all
17597 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20  ocate_page;.    
17598 20 20 7d 0a 0a 20 20 20 20 20 20 6b 20 3d 20 67    }..      k = g
17599 65 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d  et4byte(&pTrunk-
1759a 3e 61 44 61 74 61 5b 34 5d 29 3b 0a 20 20 20 20  >aData[4]);.    
1759b 20 20 69 66 28 20 6b 3d 3d 30 20 26 26 20 21 73    if( k==0 && !s
1759c 65 61 72 63 68 4c 69 73 74 20 29 7b 0a 20 20 20  earchList ){.   
1759d 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72 75 6e       /* The trun
1759e 6b 20 68 61 73 20 6e 6f 20 6c 65 61 76 65 73 20  k has no leaves 
1759f 61 6e 64 20 74 68 65 20 6c 69 73 74 20 69 73 20  and the list is 
175a0 6e 6f 74 20 62 65 69 6e 67 20 73 65 61 72 63 68  not being search
175a1 65 64 2e 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  ed. .        ** 
175a2 53 6f 20 65 78 74 72 61 63 74 20 74 68 65 20 74  So extract the t
175a3 72 75 6e 6b 20 70 61 67 65 20 69 74 73 65 6c 66  runk page itself
175a4 20 61 6e 64 20 75 73 65 20 69 74 20 61 73 20 74   and use it as t
175a5 68 65 20 6e 65 77 6c 79 20 0a 20 20 20 20 20 20  he newly .      
175a6 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 70    ** allocated p
175a7 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 61  age */.        a
175a8 73 73 65 72 74 28 20 70 50 72 65 76 54 72 75 6e  ssert( pPrevTrun
175a9 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  k==0 );.        
175aa 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
175ab 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70  rWrite(pTrunk->p
175ac 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  DbPage);.       
175ad 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
175ae 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c       goto end_al
175af 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20  locate_page;.   
175b0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a       }.        *
175b1 70 50 67 6e 6f 20 3d 20 69 54 72 75 6e 6b 3b 0a  pPgno = iTrunk;.
175b2 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
175b3 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32  pPage1->aData[32
175b4 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74  ], &pTrunk->aDat
175b5 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20  a[0], 4);.      
175b6 20 20 2a 70 70 50 61 67 65 20 3d 20 70 54 72 75    *ppPage = pTru
175b7 6e 6b 3b 0a 20 20 20 20 20 20 20 20 70 54 72 75  nk;.        pTru
175b8 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  nk = 0;.        
175b9 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45  TRACE(("ALLOCATE
175ba 3a 20 25 64 20 74 72 75 6e 6b 20 2d 20 25 64 20  : %d trunk - %d 
175bb 66 72 65 65 20 70 61 67 65 73 20 6c 65 66 74 5c  free pages left\
175bc 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29  n", *pPgno, n-1)
175bd 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  );.      }else i
175be 66 28 20 6b 3e 28 75 33 32 29 28 70 42 74 2d 3e  f( k>(u32)(pBt->
175bf 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32  usableSize/4 - 2
175c0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ) ){.        /* 
175c1 56 61 6c 75 65 20 6f 66 20 6b 20 69 73 20 6f 75  Value of k is ou
175c2 74 20 6f 66 20 72 61 6e 67 65 2e 20 20 44 61 74  t of range.  Dat
175c3 61 62 61 73 65 20 63 6f 72 72 75 70 74 69 6f 6e  abase corruption
175c4 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20 3d   */.        rc =
175c5 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
175c6 42 4b 50 54 3b 0a 20 20 20 20 20 20 20 20 67 6f  BKPT;.        go
175c7 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f  to end_allocate_
175c8 70 61 67 65 3b 0a 23 69 66 6e 64 65 66 20 53 51  page;.#ifndef SQ
175c9 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
175ca 43 55 55 4d 0a 20 20 20 20 20 20 7d 65 6c 73 65  CUUM.      }else
175cb 20 69 66 28 20 73 65 61 72 63 68 4c 69 73 74 20   if( searchList 
175cc 26 26 20 6e 65 61 72 62 79 3d 3d 69 54 72 75 6e  && nearby==iTrun
175cd 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  k ){.        /* 
175ce 54 68 65 20 6c 69 73 74 20 69 73 20 62 65 69 6e  The list is bein
175cf 67 20 73 65 61 72 63 68 65 64 20 61 6e 64 20 74  g searched and t
175d0 68 69 73 20 74 72 75 6e 6b 20 70 61 67 65 20 69  his trunk page i
175d1 73 20 74 68 65 20 70 61 67 65 0a 20 20 20 20 20  s the page.     
175d2 20 20 20 2a 2a 20 74 6f 20 61 6c 6c 6f 63 61 74     ** to allocat
175d3 65 2c 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66  e, regardless of
175d4 20 77 68 65 74 68 65 72 20 69 74 20 68 61 73 20   whether it has 
175d5 6c 65 61 76 65 73 2e 0a 20 20 20 20 20 20 20 20  leaves..        
175d6 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  */.        asser
175d7 74 28 20 2a 70 50 67 6e 6f 3d 3d 69 54 72 75 6e  t( *pPgno==iTrun
175d8 6b 20 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 70  k );.        *pp
175d9 50 61 67 65 20 3d 20 70 54 72 75 6e 6b 3b 0a 20  Page = pTrunk;. 
175da 20 20 20 20 20 20 20 73 65 61 72 63 68 4c 69 73         searchLis
175db 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 72  t = 0;.        r
175dc 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
175dd 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44  Write(pTrunk->pD
175de 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  bPage);.        
175df 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
175e0 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c      goto end_all
175e1 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20  ocate_page;.    
175e2 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
175e3 28 20 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ( k==0 ){.      
175e4 20 20 20 20 69 66 28 20 21 70 50 72 65 76 54 72      if( !pPrevTr
175e5 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  unk ){.         
175e6 20 20 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65     memcpy(&pPage
175e7 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 26 70  1->aData[32], &p
175e8 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c  Trunk->aData[0],
175e9 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d   4);.          }
175ea 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
175eb 20 20 6d 65 6d 63 70 79 28 26 70 50 72 65 76 54    memcpy(&pPrevT
175ec 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20  runk->aData[0], 
175ed 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30  &pTrunk->aData[0
175ee 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20 20  ], 4);.         
175ef 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65   }.        }else
175f0 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  {.          /* T
175f1 68 65 20 74 72 75 6e 6b 20 70 61 67 65 20 69 73  he trunk page is
175f2 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65   required by the
175f3 20 63 61 6c 6c 65 72 20 62 75 74 20 69 74 20 63   caller but it c
175f4 6f 6e 74 61 69 6e 73 20 0a 20 20 20 20 20 20 20  ontains .       
175f5 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72 73 20 74     ** pointers t
175f6 6f 20 66 72 65 65 2d 6c 69 73 74 20 6c 65 61 76  o free-list leav
175f7 65 73 2e 20 54 68 65 20 66 69 72 73 74 20 6c 65  es. The first le
175f8 61 66 20 62 65 63 6f 6d 65 73 20 61 20 74 72 75  af becomes a tru
175f9 6e 6b 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  nk.          ** 
175fa 70 61 67 65 20 69 6e 20 74 68 69 73 20 63 61 73  page in this cas
175fb 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  e..          */.
175fc 20 20 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67            MemPag
175fd 65 20 2a 70 4e 65 77 54 72 75 6e 6b 3b 0a 20 20  e *pNewTrunk;.  
175fe 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69 4e 65          Pgno iNe
175ff 77 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74  wTrunk = get4byt
17600 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61  e(&pTrunk->aData
17601 5b 38 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20  [8]);.          
17602 69 66 28 20 69 4e 65 77 54 72 75 6e 6b 3e 6d 78  if( iNewTrunk>mx
17603 50 61 67 65 20 29 7b 20 0a 20 20 20 20 20 20 20  Page ){ .       
17604 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
17605 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
17606 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20             goto 
17607 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67  end_allocate_pag
17608 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  e;.          }. 
17609 20 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73           testcas
1760a 65 28 20 69 4e 65 77 54 72 75 6e 6b 3d 3d 6d 78  e( iNewTrunk==mx
1760b 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 20 20  Page );.        
1760c 20 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50    rc = btreeGetP
1760d 61 67 65 28 70 42 74 2c 20 69 4e 65 77 54 72 75  age(pBt, iNewTru
1760e 6e 6b 2c 20 26 70 4e 65 77 54 72 75 6e 6b 2c 20  nk, &pNewTrunk, 
1760f 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  0);.          if
17610 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
17611 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67  ){.            g
17612 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65  oto end_allocate
17613 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20  _page;.         
17614 20 7d 0a 20 20 20 20 20 20 20 20 20 20 72 63 20   }.          rc 
17615 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
17616 69 74 65 28 70 4e 65 77 54 72 75 6e 6b 2d 3e 70  ite(pNewTrunk->p
17617 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  DbPage);.       
17618 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
17619 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
1761a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
1761b 70 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20  pNewTrunk);.    
1761c 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64          goto end
1761d 5f 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a  _allocate_page;.
1761e 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1761f 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e        memcpy(&pN
17620 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30  ewTrunk->aData[0
17621 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74  ], &pTrunk->aDat
17622 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20  a[0], 4);.      
17623 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70 4e      put4byte(&pN
17624 65 77 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34  ewTrunk->aData[4
17625 5d 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 20 20 20  ], k-1);.       
17626 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 54     memcpy(&pNewT
17627 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d 2c 20  runk->aData[8], 
17628 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 31  &pTrunk->aData[1
17629 32 5d 2c 20 28 6b 2d 31 29 2a 34 29 3b 0a 20 20  2], (k-1)*4);.  
1762a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65 50          releaseP
1762b 61 67 65 28 70 4e 65 77 54 72 75 6e 6b 29 3b 0a  age(pNewTrunk);.
1762c 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21 70            if( !p
1762d 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20  PrevTrunk ){.   
1762e 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
1762f 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
17630 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 31 2d  riteable(pPage1-
17631 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20  >pDbPage) );.   
17632 20 20 20 20 20 20 20 20 20 70 75 74 34 62 79 74           put4byt
17633 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  e(&pPage1->aData
17634 5b 33 32 5d 2c 20 69 4e 65 77 54 72 75 6e 6b 29  [32], iNewTrunk)
17635 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
17636 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  e{.            r
17637 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
17638 57 72 69 74 65 28 70 50 72 65 76 54 72 75 6e 6b  Write(pPrevTrunk
17639 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
1763a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29          if( rc )
1763b 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
1763c 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74  goto end_allocat
1763d 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20  e_page;.        
1763e 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1763f 20 20 70 75 74 34 62 79 74 65 28 26 70 50 72 65    put4byte(&pPre
17640 76 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d  vTrunk->aData[0]
17641 2c 20 69 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20  , iNewTrunk);.  
17642 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
17643 20 20 7d 0a 20 20 20 20 20 20 20 20 70 54 72 75    }.        pTru
17644 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  nk = 0;.        
17645 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45  TRACE(("ALLOCATE
17646 3a 20 25 64 20 74 72 75 6e 6b 20 2d 20 25 64 20  : %d trunk - %d 
17647 66 72 65 65 20 70 61 67 65 73 20 6c 65 66 74 5c  free pages left\
17648 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29  n", *pPgno, n-1)
17649 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  );.#endif.      
1764a 7d 65 6c 73 65 20 69 66 28 20 6b 3e 30 20 29 7b  }else if( k>0 ){
1764b 0a 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72  .        /* Extr
1764c 61 63 74 20 61 20 6c 65 61 66 20 66 72 6f 6d 20  act a leaf from 
1764d 74 68 65 20 74 72 75 6e 6b 20 2a 2f 0a 20 20 20  the trunk */.   
1764e 20 20 20 20 20 75 33 32 20 63 6c 6f 73 65 73 74       u32 closest
1764f 3b 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69  ;.        Pgno i
17650 50 61 67 65 3b 0a 20 20 20 20 20 20 20 20 75 6e  Page;.        un
17651 73 69 67 6e 65 64 20 63 68 61 72 20 2a 61 44 61  signed char *aDa
17652 74 61 20 3d 20 70 54 72 75 6e 6b 2d 3e 61 44 61  ta = pTrunk->aDa
17653 74 61 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  ta;.        rc =
17654 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
17655 74 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61  te(pTrunk->pDbPa
17656 67 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ge);.        if(
17657 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20   rc ){.         
17658 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61   goto end_alloca
17659 74 65 5f 70 61 67 65 3b 0a 20 20 20 20 20 20 20  te_page;.       
1765a 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e   }.        if( n
1765b 65 61 72 62 79 3e 30 20 29 7b 0a 20 20 20 20 20  earby>0 ){.     
1765c 20 20 20 20 20 75 33 32 20 69 3b 0a 20 20 20 20       u32 i;.    
1765d 20 20 20 20 20 20 69 6e 74 20 64 69 73 74 3b 0a        int dist;.
1765e 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73            closes
1765f 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  t = 0;.         
17660 20 64 69 73 74 20 3d 20 67 65 74 34 62 79 74 65   dist = get4byte
17661 28 26 61 44 61 74 61 5b 38 5d 29 20 2d 20 6e 65  (&aData[8]) - ne
17662 61 72 62 79 3b 0a 20 20 20 20 20 20 20 20 20 20  arby;.          
17663 69 66 28 20 64 69 73 74 3c 30 20 29 20 64 69 73  if( dist<0 ) dis
17664 74 20 3d 20 2d 64 69 73 74 3b 0a 20 20 20 20 20  t = -dist;.     
17665 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c       for(i=1; i<
17666 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  k; i++){.       
17667 20 20 20 20 20 69 6e 74 20 64 32 20 3d 20 67 65       int d2 = ge
17668 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 38 2b  t4byte(&aData[8+
17669 69 2a 34 5d 29 20 2d 20 6e 65 61 72 62 79 3b 0a  i*4]) - nearby;.
1766a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
1766b 64 32 3c 30 20 29 20 64 32 20 3d 20 2d 64 32 3b  d2<0 ) d2 = -d2;
1766c 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
1766d 20 64 32 3c 64 69 73 74 20 29 7b 0a 20 20 20 20   d2<dist ){.    
1766e 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73            closes
1766f 74 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 20  t = i;.         
17670 20 20 20 20 20 64 69 73 74 20 3d 20 64 32 3b 0a       dist = d2;.
17671 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
17672 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
17673 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
17674 20 20 20 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a     closest = 0;.
17675 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20          }..     
17676 20 20 20 69 50 61 67 65 20 3d 20 67 65 74 34 62     iPage = get4b
17677 79 74 65 28 26 61 44 61 74 61 5b 38 2b 63 6c 6f  yte(&aData[8+clo
17678 73 65 73 74 2a 34 5d 29 3b 0a 20 20 20 20 20 20  sest*4]);.      
17679 20 20 74 65 73 74 63 61 73 65 28 20 69 50 61 67    testcase( iPag
1767a 65 3d 3d 6d 78 50 61 67 65 20 29 3b 0a 20 20 20  e==mxPage );.   
1767b 20 20 20 20 20 69 66 28 20 69 50 61 67 65 3e 6d       if( iPage>m
1767c 78 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20  xPage ){.       
1767d 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
1767e 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
1767f 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
17680 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20  allocate_page;. 
17681 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
17682 20 74 65 73 74 63 61 73 65 28 20 69 50 61 67 65   testcase( iPage
17683 3d 3d 6d 78 50 61 67 65 20 29 3b 0a 20 20 20 20  ==mxPage );.    
17684 20 20 20 20 69 66 28 20 21 73 65 61 72 63 68 4c      if( !searchL
17685 69 73 74 20 7c 7c 20 69 50 61 67 65 3d 3d 6e 65  ist || iPage==ne
17686 61 72 62 79 20 29 7b 0a 20 20 20 20 20 20 20 20  arby ){.        
17687 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 3b    int noContent;
17688 0a 20 20 20 20 20 20 20 20 20 20 2a 70 50 67 6e  .          *pPgn
17689 6f 20 3d 20 69 50 61 67 65 3b 0a 20 20 20 20 20  o = iPage;.     
1768a 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c       TRACE(("ALL
1768b 4f 43 41 54 45 3a 20 25 64 20 77 61 73 20 6c 65  OCATE: %d was le
1768c 61 66 20 25 64 20 6f 66 20 25 64 20 6f 6e 20 74  af %d of %d on t
1768d 72 75 6e 6b 20 25 64 22 0a 20 20 20 20 20 20 20  runk %d".       
1768e 20 20 20 20 20 20 20 20 20 20 22 3a 20 25 64 20            ": %d 
1768f 6d 6f 72 65 20 66 72 65 65 20 70 61 67 65 73 5c  more free pages\
17690 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
17691 20 20 20 20 20 2a 70 50 67 6e 6f 2c 20 63 6c 6f       *pPgno, clo
17692 73 65 73 74 2b 31 2c 20 6b 2c 20 70 54 72 75 6e  sest+1, k, pTrun
17693 6b 2d 3e 70 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a  k->pgno, n-1));.
17694 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 6c            if( cl
17695 6f 73 65 73 74 3c 6b 2d 31 20 29 7b 0a 20 20 20  osest<k-1 ){.   
17696 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28           memcpy(
17697 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74  &aData[8+closest
17698 2a 34 5d 2c 20 26 61 44 61 74 61 5b 34 2b 6b 2a  *4], &aData[4+k*
17699 34 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20  4], 4);.        
1769a 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 75    }.          pu
1769b 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 34 5d  t4byte(&aData[4]
1769c 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 20 20 20 20  , k-1);.        
1769d 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1769e 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
1769f 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67  e(pTrunk->pDbPag
176a0 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  e) );.          
176a1 6e 6f 43 6f 6e 74 65 6e 74 20 3d 20 21 62 74 72  noContent = !btr
176a2 65 65 47 65 74 48 61 73 43 6f 6e 74 65 6e 74 28  eeGetHasContent(
176a3 70 42 74 2c 20 2a 70 50 67 6e 6f 29 3b 0a 20 20  pBt, *pPgno);.  
176a4 20 20 20 20 20 20 20 20 72 63 20 3d 20 62 74 72          rc = btr
176a5 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 2a  eeGetPage(pBt, *
176a6 70 50 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20 6e  pPgno, ppPage, n
176a7 6f 43 6f 6e 74 65 6e 74 29 3b 0a 20 20 20 20 20  oContent);.     
176a8 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
176a9 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
176aa 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
176ab 65 33 50 61 67 65 72 57 72 69 74 65 28 28 2a 70  e3PagerWrite((*p
176ac 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67 65 29  pPage)->pDbPage)
176ad 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ;.            if
176ae 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
176af 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
176b0 20 72 65 6c 65 61 73 65 50 61 67 65 28 2a 70 70   releasePage(*pp
176b1 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Page);.         
176b2 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
176b3 0a 20 20 20 20 20 20 20 20 20 20 73 65 61 72 63  .          searc
176b4 68 4c 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 20  hList = 0;.     
176b5 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
176b6 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
176b7 50 72 65 76 54 72 75 6e 6b 29 3b 0a 20 20 20 20  PrevTrunk);.    
176b8 20 20 70 50 72 65 76 54 72 75 6e 6b 20 3d 20 30    pPrevTrunk = 0
176b9 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 73 65  ;.    }while( se
176ba 61 72 63 68 4c 69 73 74 20 29 3b 0a 20 20 7d 65  archList );.  }e
176bb 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 72  lse{.    /* Ther
176bc 65 20 61 72 65 20 6e 6f 20 70 61 67 65 73 20 6f  e are no pages o
176bd 6e 20 74 68 65 20 66 72 65 65 6c 69 73 74 2c 20  n the freelist, 
176be 73 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20  so create a new 
176bf 70 61 67 65 20 61 74 20 74 68 65 0a 20 20 20 20  page at the.    
176c0 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69  ** end of the fi
176c1 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 50  le */.    int nP
176c2 61 67 65 20 3d 20 70 61 67 65 72 50 61 67 65 63  age = pagerPagec
176c3 6f 75 6e 74 28 70 42 74 29 3b 0a 20 20 20 20 2a  ount(pBt);.    *
176c4 70 50 67 6e 6f 20 3d 20 6e 50 61 67 65 20 2b 20  pPgno = nPage + 
176c5 31 3b 0a 0a 20 20 20 20 69 66 28 20 2a 70 50 67  1;..    if( *pPg
176c6 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45  no==PENDING_BYTE
176c7 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20  _PAGE(pBt) ){.  
176c8 20 20 20 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b 0a      (*pPgno)++;.
176c9 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53      }..#ifndef S
176ca 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
176cb 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42  ACUUM.    if( pB
176cc 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26 26  t->autoVacuum &&
176cd 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70   PTRMAP_ISPAGE(p
176ce 42 74 2c 20 2a 70 50 67 6e 6f 29 20 29 7b 0a 20  Bt, *pPgno) ){. 
176cf 20 20 20 20 20 2f 2a 20 49 66 20 2a 70 50 67 6e       /* If *pPgn
176d0 6f 20 72 65 66 65 72 73 20 74 6f 20 61 20 70 6f  o refers to a po
176d1 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20  inter-map page, 
176d2 61 6c 6c 6f 63 61 74 65 20 74 77 6f 20 6e 65 77  allocate two new
176d3 20 70 61 67 65 73 0a 20 20 20 20 20 20 2a 2a 20   pages.      ** 
176d4 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  at the end of th
176d5 65 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f  e file instead o
176d6 66 20 6f 6e 65 2e 20 54 68 65 20 66 69 72 73 74  f one. The first
176d7 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 0a   allocated page.
176d8 20 20 20 20 20 20 2a 2a 20 62 65 63 6f 6d 65 73        ** becomes
176d9 20 61 20 6e 65 77 20 70 6f 69 6e 74 65 72 2d 6d   a new pointer-m
176da 61 70 20 70 61 67 65 2c 20 74 68 65 20 73 65 63  ap page, the sec
176db 6f 6e 64 20 69 73 20 75 73 65 64 20 62 79 20 74  ond is used by t
176dc 68 65 20 63 61 6c 6c 65 72 2e 0a 20 20 20 20 20  he caller..     
176dd 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 50 61 67   */.      MemPag
176de 65 20 2a 70 50 67 20 3d 20 30 3b 0a 20 20 20 20  e *pPg = 0;.    
176df 20 20 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41    TRACE(("ALLOCA
176e0 54 45 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64 20  TE: %d from end 
176e1 6f 66 20 66 69 6c 65 20 28 70 6f 69 6e 74 65 72  of file (pointer
176e2 2d 6d 61 70 20 70 61 67 65 29 5c 6e 22 2c 20 2a  -map page)\n", *
176e3 70 50 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 61  pPgno));.      a
176e4 73 73 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50  ssert( *pPgno!=P
176e5 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
176e6 28 70 42 74 29 20 29 3b 0a 20 20 20 20 20 20 72  (pBt) );.      r
176e7 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65  c = btreeGetPage
176e8 28 70 42 74 2c 20 2a 70 50 67 6e 6f 2c 20 26 70  (pBt, *pPgno, &p
176e9 50 67 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66  Pg, 0);.      if
176ea 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
176eb 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
176ec 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
176ed 65 28 70 50 67 2d 3e 70 44 62 50 61 67 65 29 3b  e(pPg->pDbPage);
176ee 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
176ef 50 61 67 65 28 70 50 67 29 3b 0a 20 20 20 20 20  Page(pPg);.     
176f0 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 20   }.      if( rc 
176f1 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
176f2 20 20 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b 0a 20     (*pPgno)++;. 
176f3 20 20 20 20 20 69 66 28 20 2a 70 50 67 6e 6f 3d       if( *pPgno=
176f4 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  =PENDING_BYTE_PA
176f5 47 45 28 70 42 74 29 20 29 7b 20 28 2a 70 50 67  GE(pBt) ){ (*pPg
176f6 6e 6f 29 2b 2b 3b 20 7d 0a 20 20 20 20 7d 0a 23  no)++; }.    }.#
176f7 65 6e 64 69 66 0a 0a 20 20 20 20 61 73 73 65 72  endif..    asser
176f8 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e 44 49  t( *pPgno!=PENDI
176f9 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
176fa 29 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 62 74  ) );.    rc = bt
176fb 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
176fc 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20  *pPgno, ppPage, 
176fd 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  0);.    if( rc )
176fe 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
176ff 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
17700 72 57 72 69 74 65 28 28 2a 70 70 50 61 67 65 29  rWrite((*ppPage)
17701 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
17702 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
17703 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61  K ){.      relea
17704 73 65 50 61 67 65 28 2a 70 70 50 61 67 65 29 3b  sePage(*ppPage);
17705 0a 20 20 20 20 7d 0a 20 20 20 20 54 52 41 43 45  .    }.    TRACE
17706 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20 25 64 20  (("ALLOCATE: %d 
17707 66 72 6f 6d 20 65 6e 64 20 6f 66 20 66 69 6c 65  from end of file
17708 5c 6e 22 2c 20 2a 70 50 67 6e 6f 29 29 3b 0a 20  \n", *pPgno));. 
17709 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 2a 70   }..  assert( *p
1770a 50 67 6e 6f 21 3d 50 45 4e 44 49 4e 47 5f 42 59  Pgno!=PENDING_BY
1770b 54 45 5f 50 41 47 45 28 70 42 74 29 20 29 3b 0a  TE_PAGE(pBt) );.
1770c 0a 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70 61  .end_allocate_pa
1770d 67 65 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67  ge:.  releasePag
1770e 65 28 70 54 72 75 6e 6b 29 3b 0a 20 20 72 65 6c  e(pTrunk);.  rel
1770f 65 61 73 65 50 61 67 65 28 70 50 72 65 76 54 72  easePage(pPrevTr
17710 75 6e 6b 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  unk);.  if( rc==
17711 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
17712 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 67 65   if( sqlite3Page
17713 72 50 61 67 65 52 65 66 63 6f 75 6e 74 28 28 2a  rPageRefcount((*
17714 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61 67 65  ppPage)->pDbPage
17715 29 3e 31 20 29 7b 0a 20 20 20 20 20 20 72 65 6c  )>1 ){.      rel
17716 65 61 73 65 50 61 67 65 28 2a 70 70 50 61 67 65  easePage(*ppPage
17717 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
17718 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
17719 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 28  KPT;.    }.    (
1771a 2a 70 70 50 61 67 65 29 2d 3e 69 73 49 6e 69 74  *ppPage)->isInit
1771b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
1771c 20 20 20 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a     *ppPage = 0;.
1771d 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1771e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
1771f 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
17720 74 6f 20 61 64 64 20 70 61 67 65 20 69 50 61 67  to add page iPag
17721 65 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  e to the databas
17722 65 20 66 69 6c 65 20 66 72 65 65 2d 6c 69 73 74  e file free-list
17723 2e 20 0a 2a 2a 20 49 74 20 69 73 20 61 73 73 75  . .** It is assu
17724 6d 65 64 20 74 68 61 74 20 74 68 65 20 70 61 67  med that the pag
17725 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  e is not already
17726 20 61 20 70 61 72 74 20 6f 66 20 74 68 65 20 66   a part of the f
17727 72 65 65 2d 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  ree-list..**.** 
17728 54 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64  The value passed
17729 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61   as the second a
1772a 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 20  rgument to this 
1772b 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 70 74 69  function is opti
1772c 6f 6e 61 6c 2e 0a 2a 2a 20 49 66 20 74 68 65 20  onal..** If the 
1772d 63 61 6c 6c 65 72 20 68 61 70 70 65 6e 73 20 74  caller happens t
1772e 6f 20 68 61 76 65 20 61 20 70 6f 69 6e 74 65 72  o have a pointer
1772f 20 74 6f 20 74 68 65 20 4d 65 6d 50 61 67 65 20   to the MemPage 
17730 6f 62 6a 65 63 74 20 0a 2a 2a 20 63 6f 72 72 65  object .** corre
17731 73 70 6f 6e 64 69 6e 67 20 74 6f 20 70 61 67 65  sponding to page
17732 20 69 50 61 67 65 20 68 61 6e 64 79 2c 20 69 74   iPage handy, it
17733 20 6d 61 79 20 70 61 73 73 20 69 74 20 61 73 20   may pass it as 
17734 74 68 65 20 73 65 63 6f 6e 64 20 76 61 6c 75 65  the second value
17735 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  . .** Otherwise,
17736 20 69 74 20 6d 61 79 20 70 61 73 73 20 4e 55 4c   it may pass NUL
17737 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 70 6f  L..**.** If a po
17738 69 6e 74 65 72 20 74 6f 20 61 20 4d 65 6d 50 61  inter to a MemPa
17739 67 65 20 6f 62 6a 65 63 74 20 69 73 20 70 61 73  ge object is pas
1773a 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
1773b 64 20 61 72 67 75 6d 65 6e 74 2c 0a 2a 2a 20 69  d argument,.** i
1773c 74 73 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  ts reference cou
1773d 6e 74 20 69 73 20 6e 6f 74 20 61 6c 74 65 72 65  nt is not altere
1773e 64 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69  d by this functi
1773f 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  on..*/.static in
17740 74 20 66 72 65 65 50 61 67 65 32 28 42 74 53 68  t freePage2(BtSh
17741 61 72 65 64 20 2a 70 42 74 2c 20 4d 65 6d 50 61  ared *pBt, MemPa
17742 67 65 20 2a 70 4d 65 6d 50 61 67 65 2c 20 50 67  ge *pMemPage, Pg
17743 6e 6f 20 69 50 61 67 65 29 7b 0a 20 20 4d 65 6d  no iPage){.  Mem
17744 50 61 67 65 20 2a 70 54 72 75 6e 6b 20 3d 20 30  Page *pTrunk = 0
17745 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
17746 20 2f 2a 20 46 72 65 65 2d 6c 69 73 74 20 74 72   /* Free-list tr
17747 75 6e 6b 20 70 61 67 65 20 2a 2f 0a 20 20 50 67  unk page */.  Pg
17748 6e 6f 20 69 54 72 75 6e 6b 20 3d 20 30 3b 20 20  no iTrunk = 0;  
17749 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1774a 20 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72    /* Page number
1774b 20 6f 66 20 66 72 65 65 2d 6c 69 73 74 20 74 72   of free-list tr
1774c 75 6e 6b 20 70 61 67 65 20 2a 2f 20 0a 20 20 4d  unk page */ .  M
1774d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 20 3d  emPage *pPage1 =
1774e 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 20 20 20   pBt->pPage1;   
1774f 20 20 20 2f 2a 20 4c 6f 63 61 6c 20 72 65 66 65     /* Local refe
17750 72 65 6e 63 65 20 74 6f 20 70 61 67 65 20 31 20  rence to page 1 
17751 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  */.  MemPage *pP
17752 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
17753 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
17754 20 62 65 69 6e 67 20 66 72 65 65 64 2e 20 4d 61   being freed. Ma
17755 79 20 62 65 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20  y be NULL. */.  
17756 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
17757 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17758 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 43 6f      /* Return Co
17759 64 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 72 65  de */.  int nFre
1775a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1775b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
1775c 6e 69 74 69 61 6c 20 6e 75 6d 62 65 72 20 6f 66  nitial number of
1775d 20 70 61 67 65 73 20 6f 6e 20 66 72 65 65 2d 6c   pages on free-l
1775e 69 73 74 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ist */..  assert
1775f 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
17760 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
17761 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 50   );.  assert( iP
17762 61 67 65 3e 31 20 29 3b 0a 20 20 61 73 73 65 72  age>1 );.  asser
17763 74 28 20 21 70 4d 65 6d 50 61 67 65 20 7c 7c 20  t( !pMemPage || 
17764 70 4d 65 6d 50 61 67 65 2d 3e 70 67 6e 6f 3d 3d  pMemPage->pgno==
17765 69 50 61 67 65 20 29 3b 0a 0a 20 20 69 66 28 20  iPage );..  if( 
17766 70 4d 65 6d 50 61 67 65 20 29 7b 0a 20 20 20 20  pMemPage ){.    
17767 70 50 61 67 65 20 3d 20 70 4d 65 6d 50 61 67 65  pPage = pMemPage
17768 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67  ;.    sqlite3Pag
17769 65 72 52 65 66 28 70 50 61 67 65 2d 3e 70 44 62  erRef(pPage->pDb
1776a 50 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Page);.  }else{.
1776b 20 20 20 20 70 50 61 67 65 20 3d 20 62 74 72 65      pPage = btre
1776c 65 50 61 67 65 4c 6f 6f 6b 75 70 28 70 42 74 2c  ePageLookup(pBt,
1776d 20 69 50 61 67 65 29 3b 0a 20 20 7d 0a 0a 20 20   iPage);.  }..  
1776e 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65  /* Increment the
1776f 20 66 72 65 65 20 70 61 67 65 20 63 6f 75 6e 74   free page count
17770 20 6f 6e 20 70 50 61 67 65 31 20 2a 2f 0a 20 20   on pPage1 */.  
17771 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
17772 72 57 72 69 74 65 28 70 50 61 67 65 31 2d 3e 70  rWrite(pPage1->p
17773 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72  DbPage);.  if( r
17774 63 20 29 20 67 6f 74 6f 20 66 72 65 65 70 61 67  c ) goto freepag
17775 65 5f 6f 75 74 3b 0a 20 20 6e 46 72 65 65 20 3d  e_out;.  nFree =
17776 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65   get4byte(&pPage
17777 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b 0a 20  1->aData[36]);. 
17778 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65   put4byte(&pPage
17779 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20 6e 46  1->aData[36], nF
1777a 72 65 65 2b 31 29 3b 0a 0a 23 69 66 64 65 66 20  ree+1);..#ifdef 
1777b 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45  SQLITE_SECURE_DE
1777c 4c 45 54 45 0a 20 20 2f 2a 20 49 66 20 74 68 65  LETE.  /* If the
1777d 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44   SQLITE_SECURE_D
1777e 45 4c 45 54 45 20 63 6f 6d 70 69 6c 65 2d 74 69  ELETE compile-ti
1777f 6d 65 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e 61  me option is ena
17780 62 6c 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a 20  bled, then.  ** 
17781 61 6c 77 61 79 73 20 66 75 6c 6c 79 20 6f 76 65  always fully ove
17782 72 77 72 69 74 65 20 64 65 6c 65 74 65 64 20 69  rwrite deleted i
17783 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68 20  nformation with 
17784 7a 65 72 6f 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  zeros..  */.  if
17785 28 20 28 21 70 50 61 67 65 20 26 26 20 28 72 63  ( (!pPage && (rc
17786 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28   = btreeGetPage(
17787 70 42 74 2c 20 69 50 61 67 65 2c 20 26 70 50 61  pBt, iPage, &pPa
17788 67 65 2c 20 30 29 29 29 0a 20 20 20 7c 7c 20 20  ge, 0))).   ||  
17789 20 20 20 20 20 20 20 20 20 20 28 72 63 20 3d 20            (rc = 
1778a 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
1778b 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
1778c 29 29 0a 20 20 29 7b 0a 20 20 20 20 67 6f 74 6f  )).  ){.    goto
1778d 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20   freepage_out;. 
1778e 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 50 61 67   }.  memset(pPag
1778f 65 2d 3e 61 44 61 74 61 2c 20 30 2c 20 70 50 61  e->aData, 0, pPa
17790 67 65 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a  ge->pBt->pageSiz
17791 65 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a  e);.#endif..  /*
17792 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65   If the database
17793 20 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76   supports auto-v
17794 61 63 75 75 6d 2c 20 77 72 69 74 65 20 61 6e 20  acuum, write an 
17795 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70 6f 69  entry in the poi
17796 6e 74 65 72 2d 6d 61 70 0a 20 20 2a 2a 20 74 6f  nter-map.  ** to
17797 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 74   indicate that t
17798 68 65 20 70 61 67 65 20 69 73 20 66 72 65 65 2e  he page is free.
17799 0a 20 20 2a 2f 0a 20 20 69 66 28 20 49 53 41 55  .  */.  if( ISAU
1779a 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20  TOVACUUM ){.    
1779b 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 69  ptrmapPut(pBt, i
1779c 50 61 67 65 2c 20 50 54 52 4d 41 50 5f 46 52 45  Page, PTRMAP_FRE
1779d 45 50 41 47 45 2c 20 30 2c 20 26 72 63 29 3b 0a  EPAGE, 0, &rc);.
1779e 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74      if( rc ) got
1779f 6f 20 66 72 65 65 70 61 67 65 5f 6f 75 74 3b 0a  o freepage_out;.
177a0 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 6d 61    }..  /* Now ma
177a1 6e 69 70 75 6c 61 74 65 20 74 68 65 20 61 63 74  nipulate the act
177a2 75 61 6c 20 64 61 74 61 62 61 73 65 20 66 72 65  ual database fre
177a3 65 2d 6c 69 73 74 20 73 74 72 75 63 74 75 72 65  e-list structure
177a4 2e 20 54 68 65 72 65 20 61 72 65 20 74 77 6f 0a  . There are two.
177a5 20 20 2a 2a 20 70 6f 73 73 69 62 69 6c 69 74 69    ** possibiliti
177a6 65 73 2e 20 49 66 20 74 68 65 20 66 72 65 65 2d  es. If the free-
177a7 6c 69 73 74 20 69 73 20 63 75 72 72 65 6e 74 6c  list is currentl
177a8 79 20 65 6d 70 74 79 2c 20 6f 72 20 69 66 20 74  y empty, or if t
177a9 68 65 20 66 69 72 73 74 0a 20 20 2a 2a 20 74 72  he first.  ** tr
177aa 75 6e 6b 20 70 61 67 65 20 69 6e 20 74 68 65 20  unk page in the 
177ab 66 72 65 65 2d 6c 69 73 74 20 69 73 20 66 75 6c  free-list is ful
177ac 6c 2c 20 74 68 65 6e 20 74 68 69 73 20 70 61 67  l, then this pag
177ad 65 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 61 0a  e will become a.
177ae 20 20 2a 2a 20 6e 65 77 20 66 72 65 65 2d 6c 69    ** new free-li
177af 73 74 20 74 72 75 6e 6b 20 70 61 67 65 2e 20 4f  st trunk page. O
177b0 74 68 65 72 77 69 73 65 2c 20 69 74 20 77 69 6c  therwise, it wil
177b1 6c 20 62 65 63 6f 6d 65 20 61 20 6c 65 61 66 20  l become a leaf 
177b2 6f 66 20 74 68 65 0a 20 20 2a 2a 20 66 69 72 73  of the.  ** firs
177b3 74 20 74 72 75 6e 6b 20 70 61 67 65 20 69 6e 20  t trunk page in 
177b4 74 68 65 20 63 75 72 72 65 6e 74 20 66 72 65 65  the current free
177b5 2d 6c 69 73 74 2e 20 54 68 69 73 20 62 6c 6f 63  -list. This bloc
177b6 6b 20 74 65 73 74 73 20 69 66 20 69 74 0a 20 20  k tests if it.  
177b7 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74  ** is possible t
177b8 6f 20 61 64 64 20 74 68 65 20 70 61 67 65 20 61  o add the page a
177b9 73 20 61 20 6e 65 77 20 66 72 65 65 2d 6c 69 73  s a new free-lis
177ba 74 20 6c 65 61 66 2e 0a 20 20 2a 2f 0a 20 20 69  t leaf..  */.  i
177bb 66 28 20 6e 46 72 65 65 21 3d 30 20 29 7b 0a 20  f( nFree!=0 ){. 
177bc 20 20 20 75 33 32 20 6e 4c 65 61 66 3b 20 20 20     u32 nLeaf;   
177bd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
177be 49 6e 69 74 69 61 6c 20 6e 75 6d 62 65 72 20 6f  Initial number o
177bf 66 20 6c 65 61 66 20 63 65 6c 6c 73 20 6f 6e 20  f leaf cells on 
177c0 74 72 75 6e 6b 20 70 61 67 65 20 2a 2f 0a 0a 20  trunk page */.. 
177c1 20 20 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34     iTrunk = get4
177c2 62 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44  byte(&pPage1->aD
177c3 61 74 61 5b 33 32 5d 29 3b 0a 20 20 20 20 72 63  ata[32]);.    rc
177c4 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65 28   = btreeGetPage(
177c5 70 42 74 2c 20 69 54 72 75 6e 6b 2c 20 26 70 54  pBt, iTrunk, &pT
177c6 72 75 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 69 66  runk, 0);.    if
177c7 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
177c8 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 72  ){.      goto fr
177c9 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 20 20  eepage_out;.    
177ca 7d 0a 0a 20 20 20 20 6e 4c 65 61 66 20 3d 20 67  }..    nLeaf = g
177cb 65 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d  et4byte(&pTrunk-
177cc 3e 61 44 61 74 61 5b 34 5d 29 3b 0a 20 20 20 20  >aData[4]);.    
177cd 61 73 73 65 72 74 28 20 70 42 74 2d 3e 75 73 61  assert( pBt->usa
177ce 62 6c 65 53 69 7a 65 3e 33 32 20 29 3b 0a 20 20  bleSize>32 );.  
177cf 20 20 69 66 28 20 6e 4c 65 61 66 20 3e 20 28 75    if( nLeaf > (u
177d0 33 32 29 70 42 74 2d 3e 75 73 61 62 6c 65 53 69  32)pBt->usableSi
177d1 7a 65 2f 34 20 2d 20 32 20 29 7b 0a 20 20 20 20  ze/4 - 2 ){.    
177d2 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f    rc = SQLITE_CO
177d3 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
177d4 20 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f    goto freepage_
177d5 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  out;.    }.    i
177d6 66 28 20 6e 4c 65 61 66 20 3c 20 28 75 33 32 29  f( nLeaf < (u32)
177d7 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f  pBt->usableSize/
177d8 34 20 2d 20 38 20 29 7b 0a 20 20 20 20 20 20 2f  4 - 8 ){.      /
177d9 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  * In this case t
177da 68 65 72 65 20 69 73 20 72 6f 6f 6d 20 6f 6e 20  here is room on 
177db 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65 20 74  the trunk page t
177dc 6f 20 69 6e 73 65 72 74 20 74 68 65 20 70 61 67  o insert the pag
177dd 65 0a 20 20 20 20 20 20 2a 2a 20 62 65 69 6e 67  e.      ** being
177de 20 66 72 65 65 64 20 61 73 20 61 20 6e 65 77 20   freed as a new 
177df 6c 65 61 66 2e 0a 20 20 20 20 20 20 2a 2a 0a 20  leaf..      **. 
177e0 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61       ** Note tha
177e1 74 20 74 68 65 20 74 72 75 6e 6b 20 70 61 67 65  t the trunk page
177e2 20 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 66   is not really f
177e3 75 6c 6c 20 75 6e 74 69 6c 20 69 74 20 63 6f 6e  ull until it con
177e4 74 61 69 6e 73 0a 20 20 20 20 20 20 2a 2a 20 75  tains.      ** u
177e5 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20 32 20  sableSize/4 - 2 
177e6 65 6e 74 72 69 65 73 2c 20 6e 6f 74 20 75 73 61  entries, not usa
177e7 62 6c 65 53 69 7a 65 2f 34 20 2d 20 38 20 65 6e  bleSize/4 - 8 en
177e8 74 72 69 65 73 20 61 73 20 77 65 20 68 61 76 65  tries as we have
177e9 0a 20 20 20 20 20 20 2a 2a 20 63 6f 64 65 64 2e  .      ** coded.
177ea 20 20 42 75 74 20 64 75 65 20 74 6f 20 61 20 63    But due to a c
177eb 6f 64 69 6e 67 20 65 72 72 6f 72 20 69 6e 20 76  oding error in v
177ec 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  ersions of SQLit
177ed 65 20 70 72 69 6f 72 20 74 6f 0a 20 20 20 20 20  e prior to.     
177ee 20 2a 2a 20 33 2e 36 2e 30 2c 20 64 61 74 61 62   ** 3.6.0, datab
177ef 61 73 65 73 20 77 69 74 68 20 66 72 65 65 6c 69  ases with freeli
177f0 73 74 20 74 72 75 6e 6b 20 70 61 67 65 73 20 68  st trunk pages h
177f1 6f 6c 64 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  olding more than
177f2 0a 20 20 20 20 20 20 2a 2a 20 75 73 61 62 6c 65  .      ** usable
177f3 53 69 7a 65 2f 34 20 2d 20 38 20 65 6e 74 72 69  Size/4 - 8 entri
177f4 65 73 20 77 69 6c 6c 20 62 65 20 72 65 70 6f 72  es will be repor
177f5 74 65 64 20 61 73 20 63 6f 72 72 75 70 74 2e 20  ted as corrupt. 
177f6 20 49 6e 20 6f 72 64 65 72 0a 20 20 20 20 20 20   In order.      
177f7 2a 2a 20 74 6f 20 6d 61 69 6e 74 61 69 6e 20 62  ** to maintain b
177f8 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69  ackwards compati
177f9 62 69 6c 69 74 79 20 77 69 74 68 20 6f 6c 64 65  bility with olde
177fa 72 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  r versions of SQ
177fb 4c 69 74 65 2c 0a 20 20 20 20 20 20 2a 2a 20 77  Lite,.      ** w
177fc 65 20 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20  e will continue 
177fd 74 6f 20 72 65 73 74 72 69 63 74 20 74 68 65 20  to restrict the 
177fe 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  number of entrie
177ff 73 20 74 6f 20 75 73 61 62 6c 65 53 69 7a 65 2f  s to usableSize/
17800 34 20 2d 20 38 0a 20 20 20 20 20 20 2a 2a 20 66  4 - 8.      ** f
17801 6f 72 20 6e 6f 77 2e 20 20 41 74 20 73 6f 6d 65  or now.  At some
17802 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 66 75   point in the fu
17803 74 75 72 65 20 28 6f 6e 63 65 20 65 76 65 72 79  ture (once every
17804 6f 6e 65 20 68 61 73 20 75 70 67 72 61 64 65 64  one has upgraded
17805 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 33 2e 36  .      ** to 3.6
17806 2e 30 20 6f 72 20 6c 61 74 65 72 29 20 77 65 20  .0 or later) we 
17807 73 68 6f 75 6c 64 20 63 6f 6e 73 69 64 65 72 20  should consider 
17808 66 69 78 69 6e 67 20 74 68 65 20 63 6f 6e 64 69  fixing the condi
17809 74 69 6f 6e 61 6c 20 61 62 6f 76 65 0a 20 20 20  tional above.   
1780a 20 20 20 2a 2a 20 74 6f 20 72 65 61 64 20 22 75     ** to read "u
1780b 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32 22 20 69  sableSize/4-2" i
1780c 6e 73 74 65 61 64 20 6f 66 20 22 75 73 61 62 6c  nstead of "usabl
1780d 65 53 69 7a 65 2f 34 2d 38 22 2e 0a 20 20 20 20  eSize/4-8"..    
1780e 20 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20    */.      rc = 
1780f 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
17810 65 28 70 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67  e(pTrunk->pDbPag
17811 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  e);.      if( rc
17812 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
17813 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28         put4byte(
17814 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34  &pTrunk->aData[4
17815 5d 2c 20 6e 4c 65 61 66 2b 31 29 3b 0a 20 20 20  ], nLeaf+1);.   
17816 20 20 20 20 20 70 75 74 34 62 79 74 65 28 26 70       put4byte(&p
17817 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 38 2b 6e  Trunk->aData[8+n
17818 4c 65 61 66 2a 34 5d 2c 20 69 50 61 67 65 29 3b  Leaf*4], iPage);
17819 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1781a 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20  SECURE_DELETE.  
1781b 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65 20        if( pPage 
1781c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
1781d 69 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72 69  ite3PagerDontWri
1781e 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  te(pPage->pDbPag
1781f 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65  e);.        }.#e
17820 6e 64 69 66 0a 20 20 20 20 20 20 20 20 72 63 20  ndif.        rc 
17821 3d 20 62 74 72 65 65 53 65 74 48 61 73 43 6f 6e  = btreeSetHasCon
17822 74 65 6e 74 28 70 42 74 2c 20 69 50 61 67 65 29  tent(pBt, iPage)
17823 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
17824 54 52 41 43 45 28 28 22 46 52 45 45 2d 50 41 47  TRACE(("FREE-PAG
17825 45 3a 20 25 64 20 6c 65 61 66 20 6f 6e 20 74 72  E: %d leaf on tr
17826 75 6e 6b 20 70 61 67 65 20 25 64 5c 6e 22 2c 70  unk page %d\n",p
17827 50 61 67 65 2d 3e 70 67 6e 6f 2c 70 54 72 75 6e  Page->pgno,pTrun
17828 6b 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20  k->pgno));.     
17829 20 67 6f 74 6f 20 66 72 65 65 70 61 67 65 5f 6f   goto freepage_o
1782a 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ut;.    }.  }.. 
1782b 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 66   /* If control f
1782c 6c 6f 77 73 20 74 6f 20 74 68 69 73 20 70 6f 69  lows to this poi
1782d 6e 74 2c 20 74 68 65 6e 20 69 74 20 77 61 73 20  nt, then it was 
1782e 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20  not possible to 
1782f 61 64 64 20 74 68 65 0a 20 20 2a 2a 20 74 68 65  add the.  ** the
17830 20 70 61 67 65 20 62 65 69 6e 67 20 66 72 65 65   page being free
17831 64 20 61 73 20 61 20 6c 65 61 66 20 70 61 67 65  d as a leaf page
17832 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 74 72   of the first tr
17833 75 6e 6b 20 69 6e 20 74 68 65 20 66 72 65 65 2d  unk in the free-
17834 6c 69 73 74 2e 0a 20 20 2a 2a 20 50 6f 73 73 69  list..  ** Possi
17835 62 6c 79 20 62 65 63 61 75 73 65 20 74 68 65 20  bly because the 
17836 66 72 65 65 2d 6c 69 73 74 20 69 73 20 65 6d 70  free-list is emp
17837 74 79 2c 20 6f 72 20 70 6f 73 73 69 62 6c 79 20  ty, or possibly 
17838 62 65 63 61 75 73 65 20 74 68 65 20 0a 20 20 2a  because the .  *
17839 2a 20 66 69 72 73 74 20 74 72 75 6e 6b 20 69 6e  * first trunk in
1783a 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 69   the free-list i
1783b 73 20 66 75 6c 6c 2e 20 45 69 74 68 65 72 20 77  s full. Either w
1783c 61 79 2c 20 74 68 65 20 70 61 67 65 20 62 65 69  ay, the page bei
1783d 6e 67 20 66 72 65 65 64 0a 20 20 2a 2a 20 77 69  ng freed.  ** wi
1783e 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6e 65  ll become the ne
1783f 77 20 66 69 72 73 74 20 74 72 75 6e 6b 20 70 61  w first trunk pa
17840 67 65 20 69 6e 20 74 68 65 20 66 72 65 65 2d 6c  ge in the free-l
17841 69 73 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ist..  */.  if( 
17842 70 50 61 67 65 3d 3d 30 20 26 26 20 53 51 4c 49  pPage==0 && SQLI
17843 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 62 74 72  TE_OK!=(rc = btr
17844 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 69  eeGetPage(pBt, i
17845 50 61 67 65 2c 20 26 70 50 61 67 65 2c 20 30 29  Page, &pPage, 0)
17846 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 72  ) ){.    goto fr
17847 65 65 70 61 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a  eepage_out;.  }.
17848 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
17849 67 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e  gerWrite(pPage->
1784a 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20  pDbPage);.  if( 
1784b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1784c 0a 20 20 20 20 67 6f 74 6f 20 66 72 65 65 70 61  .    goto freepa
1784d 67 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 70 75  ge_out;.  }.  pu
1784e 74 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44  t4byte(pPage->aD
1784f 61 74 61 2c 20 69 54 72 75 6e 6b 29 3b 0a 20 20  ata, iTrunk);.  
17850 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65 2d  put4byte(&pPage-
17851 3e 61 44 61 74 61 5b 34 5d 2c 20 30 29 3b 0a 20  >aData[4], 0);. 
17852 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65   put4byte(&pPage
17853 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 69 50  1->aData[32], iP
17854 61 67 65 29 3b 0a 20 20 54 52 41 43 45 28 28 22  age);.  TRACE(("
17855 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20 6e 65  FREE-PAGE: %d ne
17856 77 20 74 72 75 6e 6b 20 70 61 67 65 20 72 65 70  w trunk page rep
17857 6c 61 63 69 6e 67 20 25 64 5c 6e 22 2c 20 70 50  lacing %d\n", pP
17858 61 67 65 2d 3e 70 67 6e 6f 2c 20 69 54 72 75 6e  age->pgno, iTrun
17859 6b 29 29 3b 0a 0a 66 72 65 65 70 61 67 65 5f 6f  k));..freepage_o
1785a 75 74 3a 0a 20 20 69 66 28 20 70 50 61 67 65 20  ut:.  if( pPage 
1785b 29 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 73  ){.    pPage->is
1785c 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Init = 0;.  }.  
1785d 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67  releasePage(pPag
1785e 65 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 67  e);.  releasePag
1785f 65 28 70 54 72 75 6e 6b 29 3b 0a 20 20 72 65 74  e(pTrunk);.  ret
17860 75 72 6e 20 72 63 3b 0a 7d 0a 73 74 61 74 69 63  urn rc;.}.static
17861 20 76 6f 69 64 20 66 72 65 65 50 61 67 65 28 4d   void freePage(M
17862 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69  emPage *pPage, i
17863 6e 74 20 2a 70 52 43 29 7b 0a 20 20 69 66 28 20  nt *pRC){.  if( 
17864 28 2a 70 52 43 29 3d 3d 53 51 4c 49 54 45 5f 4f  (*pRC)==SQLITE_O
17865 4b 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d 20  K ){.    *pRC = 
17866 66 72 65 65 50 61 67 65 32 28 70 50 61 67 65 2d  freePage2(pPage-
17867 3e 70 42 74 2c 20 70 50 61 67 65 2c 20 70 50 61  >pBt, pPage, pPa
17868 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a 7d  ge->pgno);.  }.}
17869 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 79  ../*.** Free any
1786a 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20   overflow pages 
1786b 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
1786c 74 68 65 20 67 69 76 65 6e 20 43 65 6c 6c 2e 0a  the given Cell..
1786d 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c  */.static int cl
1786e 65 61 72 43 65 6c 6c 28 4d 65 6d 50 61 67 65 20  earCell(MemPage 
1786f 2a 70 50 61 67 65 2c 20 75 6e 73 69 67 6e 65 64  *pPage, unsigned
17870 20 63 68 61 72 20 2a 70 43 65 6c 6c 29 7b 0a 20   char *pCell){. 
17871 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
17872 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20 43   pPage->pBt;.  C
17873 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20  ellInfo info;.  
17874 50 67 6e 6f 20 6f 76 66 6c 50 67 6e 6f 3b 0a 20  Pgno ovflPgno;. 
17875 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e   int rc;.  int n
17876 4f 76 66 6c 3b 0a 20 20 75 31 36 20 6f 76 66 6c  Ovfl;.  u16 ovfl
17877 50 61 67 65 53 69 7a 65 3b 0a 0a 20 20 61 73 73  PageSize;..  ass
17878 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
17879 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
1787a 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
1787b 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74  btreeParseCellPt
1787c 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20  r(pPage, pCell, 
1787d 26 69 6e 66 6f 29 3b 0a 20 20 69 66 28 20 69 6e  &info);.  if( in
1787e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 3d 3d 30 20  fo.iOverflow==0 
1787f 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
17880 4c 49 54 45 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f 20  LITE_OK;  /* No 
17881 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 20  overflow pages. 
17882 52 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20 64  Return without d
17883 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 20 2a 2f  oing anything */
17884 0a 20 20 7d 0a 20 20 6f 76 66 6c 50 67 6e 6f 20  .  }.  ovflPgno 
17885 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65 6c  = get4byte(&pCel
17886 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77  l[info.iOverflow
17887 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42  ]);.  assert( pB
17888 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3e 20  t->usableSize > 
17889 34 20 29 3b 0a 20 20 6f 76 66 6c 50 61 67 65 53  4 );.  ovflPageS
1788a 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c  ize = pBt->usabl
1788b 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 6e 4f 76  eSize - 4;.  nOv
1788c 66 6c 20 3d 20 28 69 6e 66 6f 2e 6e 50 61 79 6c  fl = (info.nPayl
1788d 6f 61 64 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61  oad - info.nLoca
1788e 6c 20 2b 20 6f 76 66 6c 50 61 67 65 53 69 7a 65  l + ovflPageSize
1788f 20 2d 20 31 29 2f 6f 76 66 6c 50 61 67 65 53 69   - 1)/ovflPageSi
17890 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 76  ze;.  assert( ov
17891 66 6c 50 67 6e 6f 3d 3d 30 20 7c 7c 20 6e 4f 76  flPgno==0 || nOv
17892 66 6c 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28  fl>0 );.  while(
17893 20 6e 4f 76 66 6c 2d 2d 20 29 7b 0a 20 20 20 20   nOvfl-- ){.    
17894 50 67 6e 6f 20 69 4e 65 78 74 20 3d 20 30 3b 0a  Pgno iNext = 0;.
17895 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f 76      MemPage *pOv
17896 66 6c 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20  fl = 0;.    if( 
17897 6f 76 66 6c 50 67 6e 6f 3c 32 20 7c 7c 20 6f 76  ovflPgno<2 || ov
17898 66 6c 50 67 6e 6f 3e 70 61 67 65 72 50 61 67 65  flPgno>pagerPage
17899 63 6f 75 6e 74 28 70 42 74 29 20 29 7b 0a 20 20  count(pBt) ){.  
1789a 20 20 20 20 2f 2a 20 30 20 69 73 20 6e 6f 74 20      /* 0 is not 
1789b 61 20 6c 65 67 61 6c 20 70 61 67 65 20 6e 75 6d  a legal page num
1789c 62 65 72 20 61 6e 64 20 70 61 67 65 20 31 20 63  ber and page 1 c
1789d 61 6e 6e 6f 74 20 62 65 20 61 6e 20 0a 20 20 20  annot be an .   
1789e 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 70     ** overflow p
1789f 61 67 65 2e 20 54 68 65 72 65 66 6f 72 65 20 69  age. Therefore i
178a0 66 20 6f 76 66 6c 50 67 6e 6f 3c 32 20 6f 72 20  f ovflPgno<2 or 
178a1 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20  past the end of 
178a2 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 66 69  the .      ** fi
178a3 6c 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  le the database 
178a4 6d 75 73 74 20 62 65 20 63 6f 72 72 75 70 74 2e  must be corrupt.
178a5 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e   */.      return
178a6 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
178a7 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20  BKPT;.    }.    
178a8 69 66 28 20 6e 4f 76 66 6c 20 29 7b 0a 20 20 20  if( nOvfl ){.   
178a9 20 20 20 72 63 20 3d 20 67 65 74 4f 76 65 72 66     rc = getOverf
178aa 6c 6f 77 50 61 67 65 28 70 42 74 2c 20 6f 76 66  lowPage(pBt, ovf
178ab 6c 50 67 6e 6f 2c 20 26 70 4f 76 66 6c 2c 20 26  lPgno, &pOvfl, &
178ac 69 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 69 66  iNext);.      if
178ad 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
178ae 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
178af 20 66 72 65 65 50 61 67 65 32 28 70 42 74 2c 20   freePage2(pBt, 
178b0 70 4f 76 66 6c 2c 20 6f 76 66 6c 50 67 6e 6f 29  pOvfl, ovflPgno)
178b1 3b 0a 20 20 20 20 69 66 28 20 70 4f 76 66 6c 20  ;.    if( pOvfl 
178b2 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
178b3 50 61 67 65 72 55 6e 72 65 66 28 70 4f 76 66 6c  PagerUnref(pOvfl
178b4 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
178b5 7d 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72  }.    if( rc ) r
178b6 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 6f 76  eturn rc;.    ov
178b7 66 6c 50 67 6e 6f 20 3d 20 69 4e 65 78 74 3b 0a  flPgno = iNext;.
178b8 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
178b9 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
178ba 20 43 72 65 61 74 65 20 74 68 65 20 62 79 74 65   Create the byte
178bb 20 73 65 71 75 65 6e 63 65 20 75 73 65 64 20 74   sequence used t
178bc 6f 20 72 65 70 72 65 73 65 6e 74 20 61 20 63 65  o represent a ce
178bd 6c 6c 20 6f 6e 20 70 61 67 65 20 70 50 61 67 65  ll on page pPage
178be 0a 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 74 68  .** and write th
178bf 61 74 20 62 79 74 65 20 73 65 71 75 65 6e 63 65  at byte sequence
178c0 20 69 6e 74 6f 20 70 43 65 6c 6c 5b 5d 2e 20 20   into pCell[].  
178c1 4f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61  Overflow pages a
178c2 72 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  re.** allocated 
178c3 61 6e 64 20 66 69 6c 6c 65 64 20 69 6e 20 61 73  and filled in as
178c4 20 6e 65 63 65 73 73 61 72 79 2e 20 20 54 68 65   necessary.  The
178c5 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75   calling procedu
178c6 72 65 0a 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73  re.** is respons
178c7 69 62 6c 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20  ible for making 
178c8 73 75 72 65 20 73 75 66 66 69 63 69 65 6e 74 20  sure sufficient 
178c9 73 70 61 63 65 20 68 61 73 20 62 65 65 6e 20 61  space has been a
178ca 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 66 6f 72 20  llocated.** for 
178cb 70 43 65 6c 6c 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 4e  pCell[]..**.** N
178cc 6f 74 65 20 74 68 61 74 20 70 43 65 6c 6c 20 64  ote that pCell d
178cd 6f 65 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72  oes not necessar
178ce 79 20 6e 65 65 64 20 74 6f 20 70 6f 69 6e 74 20  y need to point 
178cf 74 6f 20 74 68 65 20 70 50 61 67 65 2d 3e 61 44  to the pPage->aD
178d0 61 74 61 0a 2a 2a 20 61 72 65 61 2e 20 20 70 43  ata.** area.  pC
178d1 65 6c 6c 20 6d 69 67 68 74 20 70 6f 69 6e 74 20  ell might point 
178d2 74 6f 20 73 6f 6d 65 20 74 65 6d 70 6f 72 61 72  to some temporar
178d3 79 20 73 74 6f 72 61 67 65 2e 20 20 54 68 65 20  y storage.  The 
178d4 63 65 6c 6c 20 77 69 6c 6c 0a 2a 2a 20 62 65 20  cell will.** be 
178d5 63 6f 6e 73 74 72 75 63 74 65 64 20 69 6e 20 74  constructed in t
178d6 68 69 73 20 74 65 6d 70 6f 72 61 72 79 20 61 72  his temporary ar
178d7 65 61 20 74 68 65 6e 20 63 6f 70 69 65 64 20 69  ea then copied i
178d8 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61  nto pPage->aData
178d9 0a 2a 2a 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74  .** later..*/.st
178da 61 74 69 63 20 69 6e 74 20 66 69 6c 6c 49 6e 43  atic int fillInC
178db 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a  ell(.  MemPage *
178dc 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20 20  pPage,          
178dd 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67        /* The pag
178de 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  e that contains 
178df 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 6e  the cell */.  un
178e0 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65  signed char *pCe
178e1 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ll,          /* 
178e2 43 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66  Complete text of
178e3 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a 20 20 63   the cell */.  c
178e4 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c  onst void *pKey,
178e5 20 69 36 34 20 6e 4b 65 79 2c 20 20 20 20 2f 2a   i64 nKey,    /*
178e6 20 54 68 65 20 6b 65 79 20 2a 2f 0a 20 20 63 6f   The key */.  co
178e7 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61 2c  nst void *pData,
178e8 69 6e 74 20 6e 44 61 74 61 2c 20 20 20 2f 2a 20  int nData,   /* 
178e9 54 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e  The data */.  in
178ea 74 20 6e 5a 65 72 6f 2c 20 20 20 20 20 20 20 20  t nZero,        
178eb 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
178ec 45 78 74 72 61 20 7a 65 72 6f 20 62 79 74 65 73  Extra zero bytes
178ed 20 74 6f 20 61 70 70 65 6e 64 20 74 6f 20 70 44   to append to pD
178ee 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  ata */.  int *pn
178ef 53 69 7a 65 20 20 20 20 20 20 20 20 20 20 20 20  Size            
178f0 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
178f1 20 63 65 6c 6c 20 73 69 7a 65 20 68 65 72 65 20   cell size here 
178f2 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 50 61 79  */.){.  int nPay
178f3 6c 6f 61 64 3b 0a 20 20 63 6f 6e 73 74 20 75 38  load;.  const u8
178f4 20 2a 70 53 72 63 3b 0a 20 20 69 6e 74 20 6e 53   *pSrc;.  int nS
178f5 72 63 2c 20 6e 2c 20 72 63 3b 0a 20 20 69 6e 74  rc, n, rc;.  int
178f6 20 73 70 61 63 65 4c 65 66 74 3b 0a 20 20 4d 65   spaceLeft;.  Me
178f7 6d 50 61 67 65 20 2a 70 4f 76 66 6c 20 3d 20 30  mPage *pOvfl = 0
178f8 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 54 6f  ;.  MemPage *pTo
178f9 52 65 6c 65 61 73 65 20 3d 20 30 3b 0a 20 20 75  Release = 0;.  u
178fa 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 50  nsigned char *pP
178fb 72 69 6f 72 3b 0a 20 20 75 6e 73 69 67 6e 65 64  rior;.  unsigned
178fc 20 63 68 61 72 20 2a 70 50 61 79 6c 6f 61 64 3b   char *pPayload;
178fd 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
178fe 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20   = pPage->pBt;. 
178ff 20 50 67 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d   Pgno pgnoOvfl =
17900 20 30 3b 0a 20 20 69 6e 74 20 6e 48 65 61 64 65   0;.  int nHeade
17901 72 3b 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e  r;.  CellInfo in
17902 66 6f 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  fo;..  assert( s
17903 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
17904 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
17905 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 70 50  tex) );..  /* pP
17906 61 67 65 20 69 73 20 6e 6f 74 20 6e 65 63 65 73  age is not neces
17907 73 61 72 69 6c 79 20 77 72 69 74 65 61 62 6c 65  sarily writeable
17908 20 73 69 6e 63 65 20 70 43 65 6c 6c 20 6d 69 67   since pCell mig
17909 68 74 20 62 65 20 61 75 78 69 6c 69 61 72 79 0a  ht be auxiliary.
1790a 20 20 2a 2a 20 62 75 66 66 65 72 20 73 70 61 63    ** buffer spac
1790b 65 20 74 68 61 74 20 69 73 20 73 65 70 61 72 61  e that is separa
1790c 74 65 20 66 72 6f 6d 20 74 68 65 20 70 50 61 67  te from the pPag
1790d 65 20 62 75 66 66 65 72 20 61 72 65 61 20 2a 2f  e buffer area */
1790e 0a 20 20 61 73 73 65 72 74 28 20 70 43 65 6c 6c  .  assert( pCell
1790f 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 20 7c 7c  <pPage->aData ||
17910 20 70 43 65 6c 6c 3e 3d 26 70 50 61 67 65 2d 3e   pCell>=&pPage->
17911 61 44 61 74 61 5b 70 42 74 2d 3e 70 61 67 65 53  aData[pBt->pageS
17912 69 7a 65 5d 0a 20 20 20 20 20 20 20 20 20 20 20  ize].           
17913 20 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72   || sqlite3Pager
17914 49 73 77 72 69 74 65 61 62 6c 65 28 70 50 61 67  Iswriteable(pPag
17915 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a  e->pDbPage) );..
17916 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65    /* Fill in the
17917 20 68 65 61 64 65 72 2e 20 2a 2f 0a 20 20 6e 48   header. */.  nH
17918 65 61 64 65 72 20 3d 20 30 3b 0a 20 20 69 66 28  eader = 0;.  if(
17919 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
1791a 0a 20 20 20 20 6e 48 65 61 64 65 72 20 2b 3d 20  .    nHeader += 
1791b 34 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  4;.  }.  if( pPa
1791c 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a 20  ge->hasData ){. 
1791d 20 20 20 6e 48 65 61 64 65 72 20 2b 3d 20 70 75     nHeader += pu
1791e 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e  tVarint(&pCell[n
1791f 48 65 61 64 65 72 5d 2c 20 6e 44 61 74 61 2b 6e  Header], nData+n
17920 5a 65 72 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Zero);.  }else{.
17921 20 20 20 20 6e 44 61 74 61 20 3d 20 6e 5a 65 72      nData = nZer
17922 6f 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 6e 48 65  o = 0;.  }.  nHe
17923 61 64 65 72 20 2b 3d 20 70 75 74 56 61 72 69 6e  ader += putVarin
17924 74 28 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72  t(&pCell[nHeader
17925 5d 2c 20 2a 28 75 36 34 2a 29 26 6e 4b 65 79 29  ], *(u64*)&nKey)
17926 3b 0a 20 20 62 74 72 65 65 50 61 72 73 65 43 65  ;.  btreeParseCe
17927 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65  llPtr(pPage, pCe
17928 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 61 73  ll, &info);.  as
17929 73 65 72 74 28 20 69 6e 66 6f 2e 6e 48 65 61 64  sert( info.nHead
1792a 65 72 3d 3d 6e 48 65 61 64 65 72 20 29 3b 0a 20  er==nHeader );. 
1792b 20 61 73 73 65 72 74 28 20 69 6e 66 6f 2e 6e 4b   assert( info.nK
1792c 65 79 3d 3d 6e 4b 65 79 20 29 3b 0a 20 20 61 73  ey==nKey );.  as
1792d 73 65 72 74 28 20 69 6e 66 6f 2e 6e 44 61 74 61  sert( info.nData
1792e 3d 3d 28 75 33 32 29 28 6e 44 61 74 61 2b 6e 5a  ==(u32)(nData+nZ
1792f 65 72 6f 29 20 29 3b 0a 20 20 0a 20 20 2f 2a 20  ero) );.  .  /* 
17930 46 69 6c 6c 20 69 6e 20 74 68 65 20 70 61 79 6c  Fill in the payl
17931 6f 61 64 20 2a 2f 0a 20 20 6e 50 61 79 6c 6f 61  oad */.  nPayloa
17932 64 20 3d 20 6e 44 61 74 61 20 2b 20 6e 5a 65 72  d = nData + nZer
17933 6f 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e  o;.  if( pPage->
17934 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 70 53  intKey ){.    pS
17935 72 63 20 3d 20 70 44 61 74 61 3b 0a 20 20 20 20  rc = pData;.    
17936 6e 53 72 63 20 3d 20 6e 44 61 74 61 3b 0a 20 20  nSrc = nData;.  
17937 20 20 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 7d    nData = 0;.  }
17938 65 6c 73 65 7b 20 0a 20 20 20 20 69 66 28 20 4e  else{ .    if( N
17939 45 56 45 52 28 6e 4b 65 79 3e 30 78 37 66 66 66  EVER(nKey>0x7fff
1793a 66 66 66 66 20 7c 7c 20 70 4b 65 79 3d 3d 30 29  ffff || pKey==0)
1793b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1793c 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
1793d 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20  BKPT;.    }.    
1793e 6e 50 61 79 6c 6f 61 64 20 2b 3d 20 28 69 6e 74  nPayload += (int
1793f 29 6e 4b 65 79 3b 0a 20 20 20 20 70 53 72 63 20  )nKey;.    pSrc 
17940 3d 20 70 4b 65 79 3b 0a 20 20 20 20 6e 53 72 63  = pKey;.    nSrc
17941 20 3d 20 28 69 6e 74 29 6e 4b 65 79 3b 0a 20 20   = (int)nKey;.  
17942 7d 0a 20 20 2a 70 6e 53 69 7a 65 20 3d 20 69 6e  }.  *pnSize = in
17943 66 6f 2e 6e 53 69 7a 65 3b 0a 20 20 73 70 61 63  fo.nSize;.  spac
17944 65 4c 65 66 74 20 3d 20 69 6e 66 6f 2e 6e 4c 6f  eLeft = info.nLo
17945 63 61 6c 3b 0a 20 20 70 50 61 79 6c 6f 61 64 20  cal;.  pPayload 
17946 3d 20 26 70 43 65 6c 6c 5b 6e 48 65 61 64 65 72  = &pCell[nHeader
17947 5d 3b 0a 20 20 70 50 72 69 6f 72 20 3d 20 26 70  ];.  pPrior = &p
17948 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66  Cell[info.iOverf
17949 6c 6f 77 5d 3b 0a 0a 20 20 77 68 69 6c 65 28 20  low];..  while( 
1794a 6e 50 61 79 6c 6f 61 64 3e 30 20 29 7b 0a 20 20  nPayload>0 ){.  
1794b 20 20 69 66 28 20 73 70 61 63 65 4c 65 66 74 3d    if( spaceLeft=
1794c 3d 30 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51  =0 ){.#ifndef SQ
1794d 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
1794e 43 55 55 4d 0a 20 20 20 20 20 20 50 67 6e 6f 20  CUUM.      Pgno 
1794f 70 67 6e 6f 50 74 72 6d 61 70 20 3d 20 70 67 6e  pgnoPtrmap = pgn
17950 6f 4f 76 66 6c 3b 20 2f 2a 20 4f 76 65 72 66 6c  oOvfl; /* Overfl
17951 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72 2d  ow page pointer-
17952 6d 61 70 20 65 6e 74 72 79 20 70 61 67 65 20 2a  map entry page *
17953 2f 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d  /.      if( pBt-
17954 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20  >autoVacuum ){. 
17955 20 20 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 20         do{.     
17956 20 20 20 20 20 70 67 6e 6f 4f 76 66 6c 2b 2b 3b       pgnoOvfl++;
17957 0a 20 20 20 20 20 20 20 20 7d 20 77 68 69 6c 65  .        } while
17958 28 20 0a 20 20 20 20 20 20 20 20 20 20 50 54 52  ( .          PTR
17959 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20  MAP_ISPAGE(pBt, 
1795a 70 67 6e 6f 4f 76 66 6c 29 20 7c 7c 20 70 67 6e  pgnoOvfl) || pgn
1795b 6f 4f 76 66 6c 3d 3d 50 45 4e 44 49 4e 47 5f 42  oOvfl==PENDING_B
1795c 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 0a 20  YTE_PAGE(pBt) . 
1795d 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20         );.      
1795e 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72  }.#endif.      r
1795f 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65  c = allocateBtre
17960 65 50 61 67 65 28 70 42 74 2c 20 26 70 4f 76 66  ePage(pBt, &pOvf
17961 6c 2c 20 26 70 67 6e 6f 4f 76 66 6c 2c 20 70 67  l, &pgnoOvfl, pg
17962 6e 6f 4f 76 66 6c 2c 20 30 29 3b 0a 23 69 66 6e  noOvfl, 0);.#ifn
17963 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
17964 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20  AUTOVACUUM.     
17965 20 2f 2a 20 49 66 20 74 68 65 20 64 61 74 61 62   /* If the datab
17966 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75 74  ase supports aut
17967 6f 2d 76 61 63 75 75 6d 2c 20 61 6e 64 20 74 68  o-vacuum, and th
17968 65 20 73 65 63 6f 6e 64 20 6f 72 20 73 75 62 73  e second or subs
17969 65 71 75 65 6e 74 0a 20 20 20 20 20 20 2a 2a 20  equent.      ** 
1796a 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69 73  overflow page is
1796b 20 62 65 69 6e 67 20 61 6c 6c 6f 63 61 74 65 64   being allocated
1796c 2c 20 61 64 64 20 61 6e 20 65 6e 74 72 79 20 74  , add an entry t
1796d 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  o the pointer-ma
1796e 70 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 74  p.      ** for t
1796f 68 61 74 20 70 61 67 65 20 6e 6f 77 2e 20 0a 20  hat page now. . 
17970 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
17971 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
17972 66 69 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70  first overflow p
17973 61 67 65 2c 20 74 68 65 6e 20 77 72 69 74 65 20  age, then write 
17974 61 20 70 61 72 74 69 61 6c 20 65 6e 74 72 79 20  a partial entry 
17975 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65  .      ** to the
17976 20 70 6f 69 6e 74 65 72 2d 6d 61 70 2e 20 49 66   pointer-map. If
17977 20 77 65 20 77 72 69 74 65 20 6e 6f 74 68 69 6e   we write nothin
17978 67 20 74 6f 20 74 68 69 73 20 70 6f 69 6e 74 65  g to this pointe
17979 72 2d 6d 61 70 20 73 6c 6f 74 2c 0a 20 20 20 20  r-map slot,.    
1797a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 6f 70    ** then the op
1797b 74 69 6d 69 73 74 69 63 20 6f 76 65 72 66 6c 6f  timistic overflo
1797c 77 20 63 68 61 69 6e 20 70 72 6f 63 65 73 73 69  w chain processi
1797d 6e 67 20 69 6e 20 63 6c 65 61 72 43 65 6c 6c 28  ng in clearCell(
1797e 29 0a 20 20 20 20 20 20 2a 2a 20 6d 61 79 20 6d  ).      ** may m
1797f 69 73 69 6e 74 65 72 70 72 65 74 20 74 68 65 20  isinterpret the 
17980 75 6e 69 6e 69 74 69 61 6c 69 73 65 64 20 76 61  uninitialised va
17981 6c 75 65 73 20 61 6e 64 20 64 65 6c 65 74 65 20  lues and delete 
17982 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 77 72 6f  the.      ** wro
17983 6e 67 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68  ng pages from th
17984 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20  e database..    
17985 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70    */.      if( p
17986 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26  Bt->autoVacuum &
17987 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  & rc==SQLITE_OK 
17988 29 7b 0a 20 20 20 20 20 20 20 20 75 38 20 65 54  ){.        u8 eT
17989 79 70 65 20 3d 20 28 70 67 6e 6f 50 74 72 6d 61  ype = (pgnoPtrma
1798a 70 3f 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  p?PTRMAP_OVERFLO
1798b 57 32 3a 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c  W2:PTRMAP_OVERFL
1798c 4f 57 31 29 3b 0a 20 20 20 20 20 20 20 20 70 74  OW1);.        pt
1798d 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 67 6e  rmapPut(pBt, pgn
1798e 6f 4f 76 66 6c 2c 20 65 54 79 70 65 2c 20 70 67  oOvfl, eType, pg
1798f 6e 6f 50 74 72 6d 61 70 2c 20 26 72 63 29 3b 0a  noPtrmap, &rc);.
17990 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20 29          if( rc )
17991 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 6c 65  {.          rele
17992 61 73 65 50 61 67 65 28 70 4f 76 66 6c 29 3b 0a  asePage(pOvfl);.
17993 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
17994 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69  }.#endif.      i
17995 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20  f( rc ){.       
17996 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 6f   releasePage(pTo
17997 52 65 6c 65 61 73 65 29 3b 0a 20 20 20 20 20 20  Release);.      
17998 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
17999 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49     }..      /* I
1799a 66 20 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20  f pToRelease is 
1799b 6e 6f 74 20 7a 65 72 6f 20 74 68 61 6e 20 70 50  not zero than pP
1799c 72 69 6f 72 20 70 6f 69 6e 74 73 20 69 6e 74 6f  rior points into
1799d 20 74 68 65 20 64 61 74 61 20 61 72 65 61 0a 20   the data area. 
1799e 20 20 20 20 20 2a 2a 20 6f 66 20 70 54 6f 52 65       ** of pToRe
1799f 6c 65 61 73 65 2e 20 20 4d 61 6b 65 20 73 75 72  lease.  Make sur
179a0 65 20 70 54 6f 52 65 6c 65 61 73 65 20 69 73 20  e pToRelease is 
179a1 73 74 69 6c 6c 20 77 72 69 74 65 61 62 6c 65 2e  still writeable.
179a2 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
179a3 28 20 70 54 6f 52 65 6c 65 61 73 65 3d 3d 30 20  ( pToRelease==0 
179a4 7c 7c 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  || sqlite3PagerI
179a5 73 77 72 69 74 65 61 62 6c 65 28 70 54 6f 52 65  swriteable(pToRe
179a6 6c 65 61 73 65 2d 3e 70 44 62 50 61 67 65 29 20  lease->pDbPage) 
179a7 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  );..      /* If 
179a8 70 50 72 69 6f 72 20 69 73 20 70 61 72 74 20 6f  pPrior is part o
179a9 66 20 74 68 65 20 64 61 74 61 20 61 72 65 61 20  f the data area 
179aa 6f 66 20 70 50 61 67 65 2c 20 74 68 65 6e 20 6d  of pPage, then m
179ab 61 6b 65 20 73 75 72 65 20 70 50 61 67 65 0a 20  ake sure pPage. 
179ac 20 20 20 20 20 2a 2a 20 69 73 20 73 74 69 6c 6c       ** is still
179ad 20 77 72 69 74 65 61 62 6c 65 20 2a 2f 0a 20 20   writeable */.  
179ae 20 20 20 20 61 73 73 65 72 74 28 20 70 50 72 69      assert( pPri
179af 6f 72 3c 70 50 61 67 65 2d 3e 61 44 61 74 61 20  or<pPage->aData 
179b0 7c 7c 20 70 50 72 69 6f 72 3e 3d 26 70 50 61 67  || pPrior>=&pPag
179b1 65 2d 3e 61 44 61 74 61 5b 70 42 74 2d 3e 70 61  e->aData[pBt->pa
179b2 67 65 53 69 7a 65 5d 0a 20 20 20 20 20 20 20 20  geSize].        
179b3 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 50 61      || sqlite3Pa
179b4 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
179b5 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29  Page->pDbPage) )
179b6 3b 0a 0a 20 20 20 20 20 20 70 75 74 34 62 79 74  ;..      put4byt
179b7 65 28 70 50 72 69 6f 72 2c 20 70 67 6e 6f 4f 76  e(pPrior, pgnoOv
179b8 66 6c 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61  fl);.      relea
179b9 73 65 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73  sePage(pToReleas
179ba 65 29 3b 0a 20 20 20 20 20 20 70 54 6f 52 65 6c  e);.      pToRel
179bb 65 61 73 65 20 3d 20 70 4f 76 66 6c 3b 0a 20 20  ease = pOvfl;.  
179bc 20 20 20 20 70 50 72 69 6f 72 20 3d 20 70 4f 76      pPrior = pOv
179bd 66 6c 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 20  fl->aData;.     
179be 20 70 75 74 34 62 79 74 65 28 70 50 72 69 6f 72   put4byte(pPrior
179bf 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 50 61 79  , 0);.      pPay
179c0 6c 6f 61 64 20 3d 20 26 70 4f 76 66 6c 2d 3e 61  load = &pOvfl->a
179c1 44 61 74 61 5b 34 5d 3b 0a 20 20 20 20 20 20 73  Data[4];.      s
179c2 70 61 63 65 4c 65 66 74 20 3d 20 70 42 74 2d 3e  paceLeft = pBt->
179c3 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 3b 0a  usableSize - 4;.
179c4 20 20 20 20 7d 0a 20 20 20 20 6e 20 3d 20 6e 50      }.    n = nP
179c5 61 79 6c 6f 61 64 3b 0a 20 20 20 20 69 66 28 20  ayload;.    if( 
179c6 6e 3e 73 70 61 63 65 4c 65 66 74 20 29 20 6e 20  n>spaceLeft ) n 
179c7 3d 20 73 70 61 63 65 4c 65 66 74 3b 0a 0a 20 20  = spaceLeft;..  
179c8 20 20 2f 2a 20 49 66 20 70 54 6f 52 65 6c 65 61    /* If pToRelea
179c9 73 65 20 69 73 20 6e 6f 74 20 7a 65 72 6f 20 74  se is not zero t
179ca 68 61 6e 20 70 50 61 79 6c 6f 61 64 20 70 6f 69  han pPayload poi
179cb 6e 74 73 20 69 6e 74 6f 20 74 68 65 20 64 61 74  nts into the dat
179cc 61 20 61 72 65 61 0a 20 20 20 20 2a 2a 20 6f 66  a area.    ** of
179cd 20 70 54 6f 52 65 6c 65 61 73 65 2e 20 20 4d 61   pToRelease.  Ma
179ce 6b 65 20 73 75 72 65 20 70 54 6f 52 65 6c 65 61  ke sure pToRelea
179cf 73 65 20 69 73 20 73 74 69 6c 6c 20 77 72 69 74  se is still writ
179d0 65 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 61 73  eable. */.    as
179d1 73 65 72 74 28 20 70 54 6f 52 65 6c 65 61 73 65  sert( pToRelease
179d2 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 50 61  ==0 || sqlite3Pa
179d3 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
179d4 54 6f 52 65 6c 65 61 73 65 2d 3e 70 44 62 50 61  ToRelease->pDbPa
179d5 67 65 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 49  ge) );..    /* I
179d6 66 20 70 50 61 79 6c 6f 61 64 20 69 73 20 70 61  f pPayload is pa
179d7 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 61  rt of the data a
179d8 72 65 61 20 6f 66 20 70 50 61 67 65 2c 20 74 68  rea of pPage, th
179d9 65 6e 20 6d 61 6b 65 20 73 75 72 65 20 70 50 61  en make sure pPa
179da 67 65 0a 20 20 20 20 2a 2a 20 69 73 20 73 74 69  ge.    ** is sti
179db 6c 6c 20 77 72 69 74 65 61 62 6c 65 20 2a 2f 0a  ll writeable */.
179dc 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 79      assert( pPay
179dd 6c 6f 61 64 3c 70 50 61 67 65 2d 3e 61 44 61 74  load<pPage->aDat
179de 61 20 7c 7c 20 70 50 61 79 6c 6f 61 64 3e 3d 26  a || pPayload>=&
179df 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 42 74  pPage->aData[pBt
179e0 2d 3e 70 61 67 65 53 69 7a 65 5d 0a 20 20 20 20  ->pageSize].    
179e1 20 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74          || sqlit
179e2 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
179e3 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
179e4 65 29 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 6e  e) );..    if( n
179e5 53 72 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 69  Src>0 ){.      i
179e6 66 28 20 6e 3e 6e 53 72 63 20 29 20 6e 20 3d 20  f( n>nSrc ) n = 
179e7 6e 53 72 63 3b 0a 20 20 20 20 20 20 61 73 73 65  nSrc;.      asse
179e8 72 74 28 20 70 53 72 63 20 29 3b 0a 20 20 20 20  rt( pSrc );.    
179e9 20 20 6d 65 6d 63 70 79 28 70 50 61 79 6c 6f 61    memcpy(pPayloa
179ea 64 2c 20 70 53 72 63 2c 20 6e 29 3b 0a 20 20 20  d, pSrc, n);.   
179eb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65   }else{.      me
179ec 6d 73 65 74 28 70 50 61 79 6c 6f 61 64 2c 20 30  mset(pPayload, 0
179ed 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , n);.    }.    
179ee 6e 50 61 79 6c 6f 61 64 20 2d 3d 20 6e 3b 0a 20  nPayload -= n;. 
179ef 20 20 20 70 50 61 79 6c 6f 61 64 20 2b 3d 20 6e     pPayload += n
179f0 3b 0a 20 20 20 20 70 53 72 63 20 2b 3d 20 6e 3b  ;.    pSrc += n;
179f1 0a 20 20 20 20 6e 53 72 63 20 2d 3d 20 6e 3b 0a  .    nSrc -= n;.
179f2 20 20 20 20 73 70 61 63 65 4c 65 66 74 20 2d 3d      spaceLeft -=
179f3 20 6e 3b 0a 20 20 20 20 69 66 28 20 6e 53 72 63   n;.    if( nSrc
179f4 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 53 72  ==0 ){.      nSr
179f5 63 20 3d 20 6e 44 61 74 61 3b 0a 20 20 20 20 20  c = nData;.     
179f6 20 70 53 72 63 20 3d 20 70 44 61 74 61 3b 0a 20   pSrc = pData;. 
179f7 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 6c 65 61     }.  }.  relea
179f8 73 65 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73  sePage(pToReleas
179f9 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  e);.  return SQL
179fa 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
179fb 20 52 65 6d 6f 76 65 20 74 68 65 20 69 2d 74 68   Remove the i-th
179fc 20 63 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65   cell from pPage
179fd 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
179fe 65 66 66 65 63 74 73 20 70 50 61 67 65 20 6f 6e  effects pPage on
179ff 6c 79 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20  ly..** The cell 
17a00 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 66  content is not f
17a01 72 65 65 64 20 6f 72 20 64 65 61 6c 6c 6f 63 61  reed or dealloca
17a02 74 65 64 2e 20 20 49 74 20 69 73 20 61 73 73 75  ted.  It is assu
17a03 6d 65 64 20 74 68 61 74 0a 2a 2a 20 74 68 65 20  med that.** the 
17a04 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 61 73  cell content has
17a05 20 62 65 65 6e 20 63 6f 70 69 65 64 20 73 6f 6d   been copied som
17a06 65 70 6c 61 63 65 20 65 6c 73 65 2e 20 20 54 68  eplace else.  Th
17a07 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 0a  is routine just.
17a08 2a 2a 20 72 65 6d 6f 76 65 73 20 74 68 65 20 72  ** removes the r
17a09 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20  eference to the 
17a0a 63 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e  cell from pPage.
17a0b 0a 2a 2a 0a 2a 2a 20 22 73 7a 22 20 6d 75 73 74  .**.** "sz" must
17a0c 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   be the number o
17a0d 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 63  f bytes in the c
17a0e 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ell..*/.static v
17a0f 6f 69 64 20 64 72 6f 70 43 65 6c 6c 28 4d 65 6d  oid dropCell(Mem
17a10 50 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74  Page *pPage, int
17a11 20 69 64 78 2c 20 69 6e 74 20 73 7a 2c 20 69 6e   idx, int sz, in
17a12 74 20 2a 70 52 43 29 7b 0a 20 20 69 6e 74 20 69  t *pRC){.  int i
17a13 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f  ;          /* Lo
17a14 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
17a15 69 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20  int pc;         
17a16 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 63 65 6c  /* Offset to cel
17a17 6c 20 63 6f 6e 74 65 6e 74 20 6f 66 20 63 65 6c  l content of cel
17a18 6c 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 20  l being deleted 
17a19 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20  */.  u8 *data;  
17a1a 20 20 20 20 20 2f 2a 20 70 50 61 67 65 2d 3e 61       /* pPage->a
17a1b 44 61 74 61 20 2a 2f 0a 20 20 75 38 20 2a 70 74  Data */.  u8 *pt
17a1c 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65  r;        /* Use
17a1d 64 20 74 6f 20 6d 6f 76 65 20 62 79 74 65 73 20  d to move bytes 
17a1e 61 72 6f 75 6e 64 20 77 69 74 68 69 6e 20 64 61  around within da
17a1f 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 72 63  ta[] */.  int rc
17a20 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ;         /* The
17a21 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   return code */.
17a22 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20    int hdr;      
17a23 20 20 2f 2a 20 42 65 67 69 6e 6e 69 6e 67 20 6f    /* Beginning o
17a24 66 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 30  f the header.  0
17a25 20 6d 6f 73 74 20 70 61 67 65 73 2e 20 20 31 30   most pages.  10
17a26 30 20 70 61 67 65 20 31 20 2a 2f 0a 0a 20 20 69  0 page 1 */..  i
17a27 66 28 20 2a 70 52 43 20 29 20 72 65 74 75 72 6e  f( *pRC ) return
17a28 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64 78  ;..  assert( idx
17a29 3e 3d 30 20 26 26 20 69 64 78 3c 70 50 61 67 65  >=0 && idx<pPage
17a2a 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 61 73 73  ->nCell );.  ass
17a2b 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53 69 7a  ert( sz==cellSiz
17a2c 65 28 70 50 61 67 65 2c 20 69 64 78 29 20 29 3b  e(pPage, idx) );
17a2d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
17a2e 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
17a2f 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
17a30 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  e) );.  assert( 
17a31 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
17a32 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d  ld(pPage->pBt->m
17a33 75 74 65 78 29 20 29 3b 0a 20 20 64 61 74 61 20  utex) );.  data 
17a34 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a  = pPage->aData;.
17a35 20 20 70 74 72 20 3d 20 26 64 61 74 61 5b 70 50    ptr = &data[pP
17a36 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20  age->cellOffset 
17a37 2b 20 32 2a 69 64 78 5d 3b 0a 20 20 70 63 20 3d  + 2*idx];.  pc =
17a38 20 67 65 74 32 62 79 74 65 28 70 74 72 29 3b 0a   get2byte(ptr);.
17a39 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68    hdr = pPage->h
17a3a 64 72 4f 66 66 73 65 74 3b 0a 20 20 74 65 73 74  drOffset;.  test
17a3b 63 61 73 65 28 20 70 63 3d 3d 67 65 74 32 62 79  case( pc==get2by
17a3c 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29  te(&data[hdr+5])
17a3d 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20   );.  testcase( 
17a3e 70 63 2b 73 7a 3d 3d 70 50 61 67 65 2d 3e 70 42  pc+sz==pPage->pB
17a3f 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 29 3b  t->usableSize );
17a40 0a 20 20 69 66 28 20 70 63 20 3c 20 67 65 74 32  .  if( pc < get2
17a41 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
17a42 5d 29 20 7c 7c 20 70 63 2b 73 7a 20 3e 20 70 50  ]) || pc+sz > pP
17a43 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
17a44 53 69 7a 65 20 29 7b 0a 20 20 20 20 2a 70 52 43  Size ){.    *pRC
17a45 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50   = SQLITE_CORRUP
17a46 54 5f 42 4b 50 54 3b 0a 20 20 20 20 72 65 74 75  T_BKPT;.    retu
17a47 72 6e 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 66  rn;.  }.  rc = f
17a48 72 65 65 53 70 61 63 65 28 70 50 61 67 65 2c 20  reeSpace(pPage, 
17a49 70 63 2c 20 73 7a 29 3b 0a 20 20 69 66 28 20 72  pc, sz);.  if( r
17a4a 63 20 29 7b 0a 20 20 20 20 2a 70 52 43 20 3d 20  c ){.    *pRC = 
17a4b 72 63 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  rc;.    return;.
17a4c 20 20 7d 0a 20 20 66 6f 72 28 69 3d 69 64 78 2b    }.  for(i=idx+
17a4d 31 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c  1; i<pPage->nCel
17a4e 6c 3b 20 69 2b 2b 2c 20 70 74 72 2b 3d 32 29 7b  l; i++, ptr+=2){
17a4f 0a 20 20 20 20 70 74 72 5b 30 5d 20 3d 20 70 74  .    ptr[0] = pt
17a50 72 5b 32 5d 3b 0a 20 20 20 20 70 74 72 5b 31 5d  r[2];.    ptr[1]
17a51 20 3d 20 70 74 72 5b 33 5d 3b 0a 20 20 7d 0a 20   = ptr[3];.  }. 
17a52 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 2d 3b   pPage->nCell--;
17a53 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74  .  put2byte(&dat
17a54 61 5b 68 64 72 2b 33 5d 2c 20 70 50 61 67 65 2d  a[hdr+3], pPage-
17a55 3e 6e 43 65 6c 6c 29 3b 0a 20 20 70 50 61 67 65  >nCell);.  pPage
17a56 2d 3e 6e 46 72 65 65 20 2b 3d 20 32 3b 0a 7d 0a  ->nFree += 2;.}.
17a57 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20  ./*.** Insert a 
17a58 6e 65 77 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67  new cell on pPag
17a59 65 20 61 74 20 63 65 6c 6c 20 69 6e 64 65 78 20  e at cell index 
17a5a 22 69 22 2e 20 20 70 43 65 6c 6c 20 70 6f 69 6e  "i".  pCell poin
17a5b 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 63 6f 6e  ts to the.** con
17a5c 74 65 6e 74 20 6f 66 20 74 68 65 20 63 65 6c 6c  tent of the cell
17a5d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63  ..**.** If the c
17a5e 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 77 69 6c 6c  ell content will
17a5f 20 66 69 74 20 6f 6e 20 74 68 65 20 70 61 67 65   fit on the page
17a60 2c 20 74 68 65 6e 20 70 75 74 20 69 74 20 74 68  , then put it th
17a61 65 72 65 2e 20 20 49 66 20 69 74 0a 2a 2a 20 77  ere.  If it.** w
17a62 69 6c 6c 20 6e 6f 74 20 66 69 74 2c 20 74 68 65  ill not fit, the
17a63 6e 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f 66  n make a copy of
17a64 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
17a65 74 20 69 6e 74 6f 20 70 54 65 6d 70 20 69 66 0a  t into pTemp if.
17a66 2a 2a 20 70 54 65 6d 70 20 69 73 20 6e 6f 74 20  ** pTemp is not 
17a67 6e 75 6c 6c 2e 20 20 52 65 67 61 72 64 6c 65 73  null.  Regardles
17a68 73 20 6f 66 20 70 54 65 6d 70 2c 20 61 6c 6c 6f  s of pTemp, allo
17a69 63 61 74 65 20 61 20 6e 65 77 20 65 6e 74 72 79  cate a new entry
17a6a 0a 2a 2a 20 69 6e 20 70 50 61 67 65 2d 3e 61 4f  .** in pPage->aO
17a6b 76 66 6c 5b 5d 20 61 6e 64 20 6d 61 6b 65 20 69  vfl[] and make i
17a6c 74 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 63  t point to the c
17a6d 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 28 65 69 74  ell content (eit
17a6e 68 65 72 0a 2a 2a 20 69 6e 20 70 54 65 6d 70 20  her.** in pTemp 
17a6f 6f 72 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  or the original 
17a70 70 43 65 6c 6c 29 20 61 6e 64 20 61 6c 73 6f 20  pCell) and also 
17a71 72 65 63 6f 72 64 20 69 74 73 20 69 6e 64 65 78  record its index
17a72 2e 20 0a 2a 2a 20 41 6c 6c 6f 63 61 74 69 6e 67  . .** Allocating
17a73 20 61 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20   a new entry in 
17a74 70 50 61 67 65 2d 3e 61 43 65 6c 6c 5b 5d 20 69  pPage->aCell[] i
17a75 6d 70 6c 69 65 73 20 74 68 61 74 20 0a 2a 2a 20  mplies that .** 
17a76 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
17a77 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e   is incremented.
17a78 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 53 6b 69 70 20  .**.** If nSkip 
17a79 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
17a7a 6e 20 64 6f 20 6e 6f 74 20 63 6f 70 79 20 74 68  n do not copy th
17a7b 65 20 66 69 72 73 74 20 6e 53 6b 69 70 20 62 79  e first nSkip by
17a7c 74 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 63 65  tes of the.** ce
17a7d 6c 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77  ll. The caller w
17a7e 69 6c 6c 20 6f 76 65 72 77 72 69 74 65 20 74 68  ill overwrite th
17a7f 65 6d 20 61 66 74 65 72 20 74 68 69 73 20 66 75  em after this fu
17a80 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 20  nction returns. 
17a81 49 66 0a 2a 2a 20 6e 53 6b 69 70 20 69 73 20 6e  If.** nSkip is n
17a82 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 70 43  on-zero, then pC
17a83 65 6c 6c 20 6d 61 79 20 6e 6f 74 20 70 6f 69 6e  ell may not poin
17a84 74 20 74 6f 20 61 6e 20 69 6e 76 61 6c 69 64 20  t to an invalid 
17a85 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20  memory location 
17a86 0a 2a 2a 20 28 62 75 74 20 70 43 65 6c 6c 2b 6e  .** (but pCell+n
17a87 53 6b 69 70 20 69 73 20 61 6c 77 61 79 73 20 76  Skip is always v
17a88 61 6c 69 64 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  alid)..*/.static
17a89 20 76 6f 69 64 20 69 6e 73 65 72 74 43 65 6c 6c   void insertCell
17a8a 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  (.  MemPage *pPa
17a8b 67 65 2c 20 20 20 2f 2a 20 50 61 67 65 20 69 6e  ge,   /* Page in
17a8c 74 6f 20 77 68 69 63 68 20 77 65 20 61 72 65 20  to which we are 
17a8d 63 6f 70 79 69 6e 67 20 2a 2f 0a 20 20 69 6e 74  copying */.  int
17a8e 20 69 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f   i,            /
17a8f 2a 20 4e 65 77 20 63 65 6c 6c 20 62 65 63 6f 6d  * New cell becom
17a90 65 73 20 74 68 65 20 69 2d 74 68 20 63 65 6c 6c  es the i-th cell
17a91 20 6f 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0a   of the page */.
17a92 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20    u8 *pCell,    
17a93 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f      /* Content o
17a94 66 20 74 68 65 20 6e 65 77 20 63 65 6c 6c 20 2a  f the new cell *
17a95 2f 0a 20 20 69 6e 74 20 73 7a 2c 20 20 20 20 20  /.  int sz,     
17a96 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f        /* Bytes o
17a97 66 20 63 6f 6e 74 65 6e 74 20 69 6e 20 70 43 65  f content in pCe
17a98 6c 6c 20 2a 2f 0a 20 20 75 38 20 2a 70 54 65 6d  ll */.  u8 *pTem
17a99 70 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d  p,        /* Tem
17a9a 70 20 73 74 6f 72 61 67 65 20 73 70 61 63 65 20  p storage space 
17a9b 66 6f 72 20 70 43 65 6c 6c 2c 20 69 66 20 6e 65  for pCell, if ne
17a9c 65 64 65 64 20 2a 2f 0a 20 20 50 67 6e 6f 20 69  eded */.  Pgno i
17a9d 43 68 69 6c 64 2c 20 20 20 20 20 20 2f 2a 20 49  Child,      /* I
17a9e 66 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 72 65 70 6c  f non-zero, repl
17a9f 61 63 65 20 66 69 72 73 74 20 34 20 62 79 74 65  ace first 4 byte
17aa0 73 20 77 69 74 68 20 74 68 69 73 20 76 61 6c 75  s with this valu
17aa1 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 43 20  e */.  int *pRC 
17aa2 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 61 64           /* Read
17aa3 20 61 6e 64 20 77 72 69 74 65 20 72 65 74 75 72   and write retur
17aa4 6e 20 63 6f 64 65 20 66 72 6f 6d 20 68 65 72 65  n code from here
17aa5 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 64 78   */.){.  int idx
17aa6 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68  ;          /* Wh
17aa7 65 72 65 20 74 6f 20 77 72 69 74 65 20 6e 65 77  ere to write new
17aa8 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69 6e   cell content in
17aa9 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74   data[] */.  int
17aaa 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   j;            /
17aab 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
17aac 2f 0a 20 20 69 6e 74 20 65 6e 64 3b 20 20 20 20  /.  int end;    
17aad 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62        /* First b
17aae 79 74 65 20 70 61 73 74 20 74 68 65 20 6c 61 73  yte past the las
17aaf 74 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 69  t cell pointer i
17ab0 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e  n data[] */.  in
17ab1 74 20 69 6e 73 3b 20 20 20 20 20 20 20 20 20 20  t ins;          
17ab2 2f 2a 20 49 6e 64 65 78 20 69 6e 20 64 61 74 61  /* Index in data
17ab3 5b 5d 20 77 68 65 72 65 20 6e 65 77 20 63 65 6c  [] where new cel
17ab4 6c 20 70 6f 69 6e 74 65 72 20 69 73 20 69 6e 73  l pointer is ins
17ab5 65 72 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 63  erted */.  int c
17ab6 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20  ellOffset;   /* 
17ab7 41 64 64 72 65 73 73 20 6f 66 20 66 69 72 73 74  Address of first
17ab8 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 69 6e   cell pointer in
17ab9 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 75 38 20   data[] */.  u8 
17aba 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20 20 2f  *data;         /
17abb 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  * The content of
17abc 20 74 68 65 20 77 68 6f 6c 65 20 70 61 67 65 20   the whole page 
17abd 2a 2f 0a 20 20 75 38 20 2a 70 74 72 3b 20 20 20  */.  u8 *ptr;   
17abe 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 66         /* Used f
17abf 6f 72 20 6d 6f 76 69 6e 67 20 69 6e 66 6f 72 6d  or moving inform
17ac0 61 74 69 6f 6e 20 61 72 6f 75 6e 64 20 69 6e 20  ation around in 
17ac1 64 61 74 61 5b 5d 20 2a 2f 0a 0a 20 20 69 6e 74  data[] */..  int
17ac2 20 6e 53 6b 69 70 20 3d 20 28 69 43 68 69 6c 64   nSkip = (iChild
17ac3 20 3f 20 34 20 3a 20 30 29 3b 0a 0a 20 20 69 66   ? 4 : 0);..  if
17ac4 28 20 2a 70 52 43 20 29 20 72 65 74 75 72 6e 3b  ( *pRC ) return;
17ac5 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30  ..  assert( i>=0
17ac6 20 26 26 20 69 3c 3d 70 50 61 67 65 2d 3e 6e 43   && i<=pPage->nC
17ac7 65 6c 6c 2b 70 50 61 67 65 2d 3e 6e 4f 76 65 72  ell+pPage->nOver
17ac8 66 6c 6f 77 20 29 3b 0a 20 20 61 73 73 65 72 74  flow );.  assert
17ac9 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 3d  ( pPage->nCell<=
17aca 4d 58 5f 43 45 4c 4c 28 70 50 61 67 65 2d 3e 70  MX_CELL(pPage->p
17acb 42 74 29 20 26 26 20 4d 58 5f 43 45 4c 4c 28 70  Bt) && MX_CELL(p
17acc 50 61 67 65 2d 3e 70 42 74 29 3c 3d 35 34 36 30  Page->pBt)<=5460
17acd 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
17ace 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3c 3d  age->nOverflow<=
17acf 41 72 72 61 79 53 69 7a 65 28 70 50 61 67 65 2d  ArraySize(pPage-
17ad0 3e 61 4f 76 66 6c 29 20 29 3b 0a 20 20 61 73 73  >aOvfl) );.  ass
17ad1 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53 69 7a  ert( sz==cellSiz
17ad2 65 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c  ePtr(pPage, pCel
17ad3 6c 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  l) );.  assert( 
17ad4 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
17ad5 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d  ld(pPage->pBt->m
17ad6 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70  utex) );.  if( p
17ad7 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20  Page->nOverflow 
17ad8 7c 7c 20 73 7a 2b 32 3e 70 50 61 67 65 2d 3e 6e  || sz+2>pPage->n
17ad9 46 72 65 65 20 29 7b 0a 20 20 20 20 69 66 28 20  Free ){.    if( 
17ada 70 54 65 6d 70 20 29 7b 0a 20 20 20 20 20 20 6d  pTemp ){.      m
17adb 65 6d 63 70 79 28 70 54 65 6d 70 2b 6e 53 6b 69  emcpy(pTemp+nSki
17adc 70 2c 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20  p, pCell+nSkip, 
17add 73 7a 2d 6e 53 6b 69 70 29 3b 0a 20 20 20 20 20  sz-nSkip);.     
17ade 20 70 43 65 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a   pCell = pTemp;.
17adf 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 43      }.    if( iC
17ae0 68 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 70 75  hild ){.      pu
17ae1 74 34 62 79 74 65 28 70 43 65 6c 6c 2c 20 69 43  t4byte(pCell, iC
17ae2 68 69 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20  hild);.    }.   
17ae3 20 6a 20 3d 20 70 50 61 67 65 2d 3e 6e 4f 76 65   j = pPage->nOve
17ae4 72 66 6c 6f 77 2b 2b 3b 0a 20 20 20 20 61 73 73  rflow++;.    ass
17ae5 65 72 74 28 20 6a 3c 28 69 6e 74 29 28 73 69 7a  ert( j<(int)(siz
17ae6 65 6f 66 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c  eof(pPage->aOvfl
17ae7 29 2f 73 69 7a 65 6f 66 28 70 50 61 67 65 2d 3e  )/sizeof(pPage->
17ae8 61 4f 76 66 6c 5b 30 5d 29 29 20 29 3b 0a 20 20  aOvfl[0])) );.  
17ae9 20 20 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 6a    pPage->aOvfl[j
17aea 5d 2e 70 43 65 6c 6c 20 3d 20 70 43 65 6c 6c 3b  ].pCell = pCell;
17aeb 0a 20 20 20 20 70 50 61 67 65 2d 3e 61 4f 76 66  .    pPage->aOvf
17aec 6c 5b 6a 5d 2e 69 64 78 20 3d 20 28 75 31 36 29  l[j].idx = (u16)
17aed 69 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  i;.  }else{.    
17aee 69 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33  int rc = sqlite3
17aef 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67 65  PagerWrite(pPage
17af0 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  ->pDbPage);.    
17af1 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
17af2 4b 20 29 7b 0a 20 20 20 20 20 20 2a 70 52 43 20  K ){.      *pRC 
17af3 3d 20 72 63 3b 0a 20 20 20 20 20 20 72 65 74 75  = rc;.      retu
17af4 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  rn;.    }.    as
17af5 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
17af6 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50  erIswriteable(pP
17af7 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  age->pDbPage) );
17af8 0a 20 20 20 20 64 61 74 61 20 3d 20 70 50 61 67  .    data = pPag
17af9 65 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 63 65  e->aData;.    ce
17afa 6c 6c 4f 66 66 73 65 74 20 3d 20 70 50 61 67 65  llOffset = pPage
17afb 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20  ->cellOffset;.  
17afc 20 20 65 6e 64 20 3d 20 63 65 6c 6c 4f 66 66 73    end = cellOffs
17afd 65 74 20 2b 20 32 2a 70 50 61 67 65 2d 3e 6e 43  et + 2*pPage->nC
17afe 65 6c 6c 3b 0a 20 20 20 20 69 6e 73 20 3d 20 63  ell;.    ins = c
17aff 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a 69 3b  ellOffset + 2*i;
17b00 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61  .    rc = alloca
17b01 74 65 53 70 61 63 65 28 70 50 61 67 65 2c 20 73  teSpace(pPage, s
17b02 7a 2c 20 26 69 64 78 29 3b 0a 20 20 20 20 69 66  z, &idx);.    if
17b03 28 20 72 63 20 29 7b 20 2a 70 52 43 20 3d 20 72  ( rc ){ *pRC = r
17b04 63 3b 20 72 65 74 75 72 6e 3b 20 7d 0a 20 20 20  c; return; }.   
17b05 20 2f 2a 20 54 68 65 20 61 6c 6c 6f 63 61 74 65   /* The allocate
17b06 53 70 61 63 65 28 29 20 72 6f 75 74 69 6e 65 20  Space() routine 
17b07 67 75 61 72 61 6e 74 65 65 73 20 74 68 65 20 66  guarantees the f
17b08 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 70 72 6f  ollowing two pro
17b09 70 65 72 74 69 65 73 0a 20 20 20 20 2a 2a 20 69  perties.    ** i
17b0a 66 20 69 74 20 72 65 74 75 72 6e 73 20 73 75 63  f it returns suc
17b0b 63 65 73 73 20 2a 2f 0a 20 20 20 20 61 73 73 65  cess */.    asse
17b0c 72 74 28 20 69 64 78 20 3e 3d 20 65 6e 64 2b 32  rt( idx >= end+2
17b0d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
17b0e 69 64 78 2b 73 7a 20 3c 3d 20 70 50 61 67 65 2d  idx+sz <= pPage-
17b0f 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  >pBt->usableSize
17b10 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e   );.    pPage->n
17b11 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 70 50 61 67  Cell++;.    pPag
17b12 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 28 75 31 36  e->nFree -= (u16
17b13 29 28 32 20 2b 20 73 7a 29 3b 0a 20 20 20 20 6d  )(2 + sz);.    m
17b14 65 6d 63 70 79 28 26 64 61 74 61 5b 69 64 78 2b  emcpy(&data[idx+
17b15 6e 53 6b 69 70 5d 2c 20 70 43 65 6c 6c 2b 6e 53  nSkip], pCell+nS
17b16 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69 70 29 3b 0a  kip, sz-nSkip);.
17b17 20 20 20 20 69 66 28 20 69 43 68 69 6c 64 20 29      if( iChild )
17b18 7b 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65  {.      put4byte
17b19 28 26 64 61 74 61 5b 69 64 78 5d 2c 20 69 43 68  (&data[idx], iCh
17b1a 69 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ild);.    }.    
17b1b 66 6f 72 28 6a 3d 65 6e 64 2c 20 70 74 72 3d 26  for(j=end, ptr=&
17b1c 64 61 74 61 5b 6a 5d 3b 20 6a 3e 69 6e 73 3b 20  data[j]; j>ins; 
17b1d 6a 2d 3d 32 2c 20 70 74 72 2d 3d 32 29 7b 0a 20  j-=2, ptr-=2){. 
17b1e 20 20 20 20 20 70 74 72 5b 30 5d 20 3d 20 70 74       ptr[0] = pt
17b1f 72 5b 2d 32 5d 3b 0a 20 20 20 20 20 20 70 74 72  r[-2];.      ptr
17b20 5b 31 5d 20 3d 20 70 74 72 5b 2d 31 5d 3b 0a 20  [1] = ptr[-1];. 
17b21 20 20 20 7d 0a 20 20 20 20 70 75 74 32 62 79 74     }.    put2byt
17b22 65 28 26 64 61 74 61 5b 69 6e 73 5d 2c 20 69 64  e(&data[ins], id
17b23 78 29 3b 0a 20 20 20 20 70 75 74 32 62 79 74 65  x);.    put2byte
17b24 28 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 68 64  (&data[pPage->hd
17b25 72 4f 66 66 73 65 74 2b 33 5d 2c 20 70 50 61 67  rOffset+3], pPag
17b26 65 2d 3e 6e 43 65 6c 6c 29 3b 0a 23 69 66 6e 64  e->nCell);.#ifnd
17b27 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
17b28 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66  UTOVACUUM.    if
17b29 28 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 61 75  ( pPage->pBt->au
17b2a 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20  toVacuum ){.    
17b2b 20 20 2f 2a 20 54 68 65 20 63 65 6c 6c 20 6d 61    /* The cell ma
17b2c 79 20 63 6f 6e 74 61 69 6e 20 61 20 70 6f 69 6e  y contain a poin
17b2d 74 65 72 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c  ter to an overfl
17b2e 6f 77 20 70 61 67 65 2e 20 49 66 20 73 6f 2c 20  ow page. If so, 
17b2f 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 74  write.      ** t
17b30 68 65 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65  he entry for the
17b31 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 69   overflow page i
17b32 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72 20  nto the pointer 
17b33 6d 61 70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  map..      */.  
17b34 20 20 20 20 70 74 72 6d 61 70 50 75 74 4f 76 66      ptrmapPutOvf
17b35 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c  lPtr(pPage, pCel
17b36 6c 2c 20 70 52 43 29 3b 0a 20 20 20 20 7d 0a 23  l, pRC);.    }.#
17b37 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  endif.  }.}../*.
17b38 2a 2a 20 41 64 64 20 61 20 6c 69 73 74 20 6f 66  ** Add a list of
17b39 20 63 65 6c 6c 73 20 74 6f 20 61 20 70 61 67 65   cells to a page
17b3a 2e 20 20 54 68 65 20 70 61 67 65 20 73 68 6f 75  .  The page shou
17b3b 6c 64 20 62 65 20 69 6e 69 74 69 61 6c 6c 79 20  ld be initially 
17b3c 65 6d 70 74 79 2e 0a 2a 2a 20 54 68 65 20 63 65  empty..** The ce
17b3d 6c 6c 73 20 61 72 65 20 67 75 61 72 61 6e 74 65  lls are guarante
17b3e 65 64 20 74 6f 20 66 69 74 20 6f 6e 20 74 68 65  ed to fit on the
17b3f 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   page..*/.static
17b40 20 76 6f 69 64 20 61 73 73 65 6d 62 6c 65 50 61   void assemblePa
17b41 67 65 28 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  ge(.  MemPage *p
17b42 50 61 67 65 2c 20 20 20 2f 2a 20 54 68 65 20 70  Page,   /* The p
17b43 61 67 65 20 74 6f 20 62 65 20 61 73 73 65 6d 62  age to be assemb
17b44 6c 69 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  lied */.  int nC
17b45 65 6c 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 54  ell,        /* T
17b46 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c  he number of cel
17b47 6c 73 20 74 6f 20 61 64 64 20 74 6f 20 74 68 69  ls to add to thi
17b48 73 20 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a  s page */.  u8 *
17b49 2a 61 70 43 65 6c 6c 2c 20 20 20 20 20 20 2f 2a  *apCell,      /*
17b4a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 63 65 6c   Pointers to cel
17b4b 6c 20 62 6f 64 69 65 73 20 2a 2f 0a 20 20 75 31  l bodies */.  u1
17b4c 36 20 2a 61 53 69 7a 65 20 20 20 20 20 20 20 20  6 *aSize        
17b4d 2f 2a 20 53 69 7a 65 73 20 6f 66 20 74 68 65 20  /* Sizes of the 
17b4e 63 65 6c 6c 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e  cells */.){.  in
17b4f 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
17b50 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
17b51 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c 70 74  */.  u8 *pCellpt
17b52 72 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73  r;     /* Addres
17b53 73 20 6f 66 20 6e 65 78 74 20 63 65 6c 6c 20 70  s of next cell p
17b54 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ointer */.  int 
17b55 63 65 6c 6c 62 6f 64 79 3b 20 20 20 20 20 2f 2a  cellbody;     /*
17b56 20 41 64 64 72 65 73 73 20 6f 66 20 6e 65 78 74   Address of next
17b57 20 63 65 6c 6c 20 62 6f 64 79 20 2a 2f 0a 20 20   cell body */.  
17b58 75 38 20 2a 20 63 6f 6e 73 74 20 64 61 74 61 20  u8 * const data 
17b59 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 20  = pPage->aData; 
17b5a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
17b5b 6f 69 6e 74 65 72 20 74 6f 20 64 61 74 61 20 66  ointer to data f
17b5c 6f 72 20 70 50 61 67 65 20 2a 2f 0a 20 20 63 6f  or pPage */.  co
17b5d 6e 73 74 20 69 6e 74 20 68 64 72 20 3d 20 70 50  nst int hdr = pP
17b5e 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 20  age->hdrOffset; 
17b5f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66            /* Off
17b60 73 65 74 20 6f 66 20 68 65 61 64 65 72 20 6f 6e  set of header on
17b61 20 70 50 61 67 65 20 2a 2f 0a 20 20 63 6f 6e 73   pPage */.  cons
17b62 74 20 69 6e 74 20 6e 55 73 61 62 6c 65 20 3d 20  t int nUsable = 
17b63 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62  pPage->pBt->usab
17b64 6c 65 53 69 7a 65 3b 20 2f 2a 20 55 73 61 62 6c  leSize; /* Usabl
17b65 65 20 73 69 7a 65 20 6f 66 20 70 61 67 65 20 2a  e size of page *
17b66 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  /..  assert( pPa
17b67 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30  ge->nOverflow==0
17b68 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
17b69 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
17b6a 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74  (pPage->pBt->mut
17b6b 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
17b6c 20 6e 43 65 6c 6c 3e 3d 30 20 26 26 20 6e 43 65   nCell>=0 && nCe
17b6d 6c 6c 3c 3d 4d 58 5f 43 45 4c 4c 28 70 50 61 67  ll<=MX_CELL(pPag
17b6e 65 2d 3e 70 42 74 29 20 26 26 20 4d 58 5f 43 45  e->pBt) && MX_CE
17b6f 4c 4c 28 70 50 61 67 65 2d 3e 70 42 74 29 3c 3d  LL(pPage->pBt)<=
17b70 35 34 36 30 20 29 3b 0a 20 20 61 73 73 65 72 74  5460 );.  assert
17b71 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
17b72 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d  writeable(pPage-
17b73 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20  >pDbPage) );..  
17b74 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68  /* Check that th
17b75 65 20 70 61 67 65 20 68 61 73 20 6a 75 73 74 20  e page has just 
17b76 62 65 65 6e 20 7a 65 72 6f 65 64 20 62 79 20 7a  been zeroed by z
17b77 65 72 6f 50 61 67 65 28 29 20 2a 2f 0a 20 20 61  eroPage() */.  a
17b78 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 43  ssert( pPage->nC
17b79 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  ell==0 );.  asse
17b7a 72 74 28 20 67 65 74 32 62 79 74 65 28 26 64 61  rt( get2byte(&da
17b7b 74 61 5b 68 64 72 2b 35 5d 29 3d 3d 6e 55 73 61  ta[hdr+5])==nUsa
17b7c 62 6c 65 20 29 3b 0a 0a 20 20 70 43 65 6c 6c 70  ble );..  pCellp
17b7d 74 72 20 3d 20 26 64 61 74 61 5b 70 50 61 67 65  tr = &data[pPage
17b7e 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 6e  ->cellOffset + n
17b7f 43 65 6c 6c 2a 32 5d 3b 0a 20 20 63 65 6c 6c 62  Cell*2];.  cellb
17b80 6f 64 79 20 3d 20 6e 55 73 61 62 6c 65 3b 0a 20  ody = nUsable;. 
17b81 20 66 6f 72 28 69 3d 6e 43 65 6c 6c 2d 31 3b 20   for(i=nCell-1; 
17b82 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20  i>=0; i--){.    
17b83 70 43 65 6c 6c 70 74 72 20 2d 3d 20 32 3b 0a 20  pCellptr -= 2;. 
17b84 20 20 20 63 65 6c 6c 62 6f 64 79 20 2d 3d 20 61     cellbody -= a
17b85 53 69 7a 65 5b 69 5d 3b 0a 20 20 20 20 70 75 74  Size[i];.    put
17b86 32 62 79 74 65 28 70 43 65 6c 6c 70 74 72 2c 20  2byte(pCellptr, 
17b87 63 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 20 20 6d  cellbody);.    m
17b88 65 6d 63 70 79 28 26 64 61 74 61 5b 63 65 6c 6c  emcpy(&data[cell
17b89 62 6f 64 79 5d 2c 20 61 70 43 65 6c 6c 5b 69 5d  body], apCell[i]
17b8a 2c 20 61 53 69 7a 65 5b 69 5d 29 3b 0a 20 20 7d  , aSize[i]);.  }
17b8b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74  .  put2byte(&dat
17b8c 61 5b 68 64 72 2b 33 5d 2c 20 6e 43 65 6c 6c 29  a[hdr+3], nCell)
17b8d 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61  ;.  put2byte(&da
17b8e 74 61 5b 68 64 72 2b 35 5d 2c 20 63 65 6c 6c 62  ta[hdr+5], cellb
17b8f 6f 64 79 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e  ody);.  pPage->n
17b90 46 72 65 65 20 2d 3d 20 28 6e 43 65 6c 6c 2a 32  Free -= (nCell*2
17b91 20 2b 20 6e 55 73 61 62 6c 65 20 2d 20 63 65 6c   + nUsable - cel
17b92 6c 62 6f 64 79 29 3b 0a 20 20 70 50 61 67 65 2d  lbody);.  pPage-
17b93 3e 6e 43 65 6c 6c 20 3d 20 28 75 31 36 29 6e 43  >nCell = (u16)nC
17b94 65 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  ell;.}../*.** Th
17b95 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 61 72 61  e following para
17b96 6d 65 74 65 72 73 20 64 65 74 65 72 6d 69 6e 65  meters determine
17b97 20 68 6f 77 20 6d 61 6e 79 20 61 64 6a 61 63 65   how many adjace
17b98 6e 74 20 70 61 67 65 73 20 67 65 74 20 69 6e 76  nt pages get inv
17b99 6f 6c 76 65 64 0a 2a 2a 20 69 6e 20 61 20 62 61  olved.** in a ba
17b9a 6c 61 6e 63 69 6e 67 20 6f 70 65 72 61 74 69 6f  lancing operatio
17b9b 6e 2e 20 20 4e 4e 20 69 73 20 74 68 65 20 6e 75  n.  NN is the nu
17b9c 6d 62 65 72 20 6f 66 20 6e 65 69 67 68 62 6f 72  mber of neighbor
17b9d 73 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65  s on either side
17b9e 0a 2a 2a 20 6f 66 20 74 68 65 20 70 61 67 65 20  .** of the page 
17b9f 74 68 61 74 20 70 61 72 74 69 63 69 70 61 74 65  that participate
17ba0 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63 69 6e   in the balancin
17ba1 67 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4e 42  g operation.  NB
17ba2 20 69 73 20 74 68 65 0a 2a 2a 20 74 6f 74 61 6c   is the.** total
17ba3 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
17ba4 20 74 68 61 74 20 70 61 72 74 69 63 69 70 61 74   that participat
17ba5 65 2c 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65  e, including the
17ba6 20 74 61 72 67 65 74 20 70 61 67 65 20 61 6e 64   target page and
17ba7 0a 2a 2a 20 4e 4e 20 6e 65 69 67 68 62 6f 72 73  .** NN neighbors
17ba8 20 6f 6e 20 65 69 74 68 65 72 20 73 69 64 65 2e   on either side.
17ba9 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 69 6e 69 6d  .**.** The minim
17baa 75 6d 20 76 61 6c 75 65 20 6f 66 20 4e 4e 20 69  um value of NN i
17bab 73 20 31 20 28 6f 66 20 63 6f 75 72 73 65 29 2e  s 1 (of course).
17bac 20 20 49 6e 63 72 65 61 73 69 6e 67 20 4e 4e 20    Increasing NN 
17bad 61 62 6f 76 65 20 31 0a 2a 2a 20 28 74 6f 20 32  above 1.** (to 2
17bae 20 6f 72 20 33 29 20 67 69 76 65 73 20 61 20 6d   or 3) gives a m
17baf 6f 64 65 73 74 20 69 6d 70 72 6f 76 65 6d 65 6e  odest improvemen
17bb0 74 20 69 6e 20 53 45 4c 45 43 54 20 61 6e 64 20  t in SELECT and 
17bb1 44 45 4c 45 54 45 20 70 65 72 66 6f 72 6d 61 6e  DELETE performan
17bb2 63 65 0a 2a 2a 20 69 6e 20 65 78 63 68 61 6e 67  ce.** in exchang
17bb3 65 20 66 6f 72 20 61 20 6c 61 72 67 65 72 20 64  e for a larger d
17bb4 65 67 72 61 64 61 74 69 6f 6e 20 69 6e 20 49 4e  egradation in IN
17bb5 53 45 52 54 20 61 6e 64 20 55 50 44 41 54 45 20  SERT and UPDATE 
17bb6 70 65 72 66 6f 72 6d 61 6e 63 65 2e 0a 2a 2a 20  performance..** 
17bb7 54 68 65 20 76 61 6c 75 65 20 6f 66 20 4e 4e 20  The value of NN 
17bb8 61 70 70 65 61 72 73 20 74 6f 20 67 69 76 65 20  appears to give 
17bb9 74 68 65 20 62 65 73 74 20 72 65 73 75 6c 74 73  the best results
17bba 20 6f 76 65 72 61 6c 6c 2e 0a 2a 2f 0a 23 64 65   overall..*/.#de
17bbb 66 69 6e 65 20 4e 4e 20 31 20 20 20 20 20 20 20  fine NN 1       
17bbc 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
17bbd 6f 66 20 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20  of neighbors on 
17bbe 65 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 70  either side of p
17bbf 50 61 67 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  Page */.#define 
17bc0 4e 42 20 28 4e 4e 2a 32 2b 31 29 20 20 20 20 20  NB (NN*2+1)     
17bc1 20 2f 2a 20 54 6f 74 61 6c 20 70 61 67 65 73 20   /* Total pages 
17bc2 69 6e 76 6f 6c 76 65 64 20 69 6e 20 74 68 65 20  involved in the 
17bc3 62 61 6c 61 6e 63 65 20 2a 2f 0a 0a 0a 23 69 66  balance */...#if
17bc4 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
17bc5 5f 51 55 49 43 4b 42 41 4c 41 4e 43 45 0a 2f 2a  _QUICKBALANCE./*
17bc6 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e  .** This version
17bc7 20 6f 66 20 62 61 6c 61 6e 63 65 28 29 20 68 61   of balance() ha
17bc8 6e 64 6c 65 73 20 74 68 65 20 63 6f 6d 6d 6f 6e  ndles the common
17bc9 20 73 70 65 63 69 61 6c 20 63 61 73 65 20 77 68   special case wh
17bca 65 72 65 0a 2a 2a 20 61 20 6e 65 77 20 65 6e 74  ere.** a new ent
17bcb 72 79 20 69 73 20 62 65 69 6e 67 20 69 6e 73 65  ry is being inse
17bcc 72 74 65 64 20 6f 6e 20 74 68 65 20 65 78 74 72  rted on the extr
17bcd 65 6d 65 20 72 69 67 68 74 2d 65 6e 64 20 6f 66  eme right-end of
17bce 20 74 68 65 0a 2a 2a 20 74 72 65 65 2c 20 69 6e   the.** tree, in
17bcf 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 77 68   other words, wh
17bd0 65 6e 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79  en the new entry
17bd1 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65   will become the
17bd2 20 6c 61 72 67 65 73 74 0a 2a 2a 20 65 6e 74 72   largest.** entr
17bd3 79 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a  y in the tree..*
17bd4 2a 0a 2a 2a 20 49 6e 73 74 65 61 64 20 6f 66 20  *.** Instead of 
17bd5 74 72 79 69 6e 67 20 74 6f 20 62 61 6c 61 6e 63  trying to balanc
17bd6 65 20 74 68 65 20 33 20 72 69 67 68 74 2d 6d 6f  e the 3 right-mo
17bd7 73 74 20 6c 65 61 66 20 70 61 67 65 73 2c 20 6a  st leaf pages, j
17bd8 75 73 74 20 61 64 64 0a 2a 2a 20 61 20 6e 65 77  ust add.** a new
17bd9 20 70 61 67 65 20 74 6f 20 74 68 65 20 72 69 67   page to the rig
17bda 68 74 2d 68 61 6e 64 20 73 69 64 65 20 61 6e 64  ht-hand side and
17bdb 20 70 75 74 20 74 68 65 20 6f 6e 65 20 6e 65 77   put the one new
17bdc 20 65 6e 74 72 79 20 69 6e 0a 2a 2a 20 74 68 61   entry in.** tha
17bdd 74 20 70 61 67 65 2e 20 20 54 68 69 73 20 6c 65  t page.  This le
17bde 61 76 65 73 20 74 68 65 20 72 69 67 68 74 20 73  aves the right s
17bdf 69 64 65 20 6f 66 20 74 68 65 20 74 72 65 65 20  ide of the tree 
17be0 73 6f 6d 65 77 68 61 74 0a 2a 2a 20 75 6e 62 61  somewhat.** unba
17be1 6c 61 6e 63 65 64 2e 20 20 42 75 74 20 6f 64 64  lanced.  But odd
17be2 73 20 61 72 65 20 74 68 61 74 20 77 65 20 77 69  s are that we wi
17be3 6c 6c 20 62 65 20 69 6e 73 65 72 74 69 6e 67 20  ll be inserting 
17be4 6e 65 77 20 65 6e 74 72 69 65 73 0a 2a 2a 20 61  new entries.** a
17be5 74 20 74 68 65 20 65 6e 64 20 73 6f 6f 6e 20 61  t the end soon a
17be6 66 74 65 72 77 61 72 64 73 20 73 6f 20 74 68 65  fterwards so the
17be7 20 6e 65 61 72 6c 79 20 65 6d 70 74 79 20 70 61   nearly empty pa
17be8 67 65 20 77 69 6c 6c 20 71 75 69 63 6b 6c 79 0a  ge will quickly.
17be9 2a 2a 20 66 69 6c 6c 20 75 70 2e 20 20 4f 6e 20  ** fill up.  On 
17bea 61 76 65 72 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70  average..**.** p
17beb 50 61 67 65 20 69 73 20 74 68 65 20 6c 65 61 66  Page is the leaf
17bec 20 70 61 67 65 20 77 68 69 63 68 20 69 73 20 74   page which is t
17bed 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 70 61  he right-most pa
17bee 67 65 20 69 6e 20 74 68 65 20 74 72 65 65 2e 0a  ge in the tree..
17bef 2a 2a 20 70 50 61 72 65 6e 74 20 69 73 20 69 74  ** pParent is it
17bf0 73 20 70 61 72 65 6e 74 2e 20 20 70 50 61 67 65  s parent.  pPage
17bf1 20 6d 75 73 74 20 68 61 76 65 20 61 20 73 69 6e   must have a sin
17bf2 67 6c 65 20 6f 76 65 72 66 6c 6f 77 20 65 6e 74  gle overflow ent
17bf3 72 79 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 61  ry.** which is a
17bf4 6c 73 6f 20 74 68 65 20 72 69 67 68 74 2d 6d 6f  lso the right-mo
17bf5 73 74 20 65 6e 74 72 79 20 6f 6e 20 74 68 65 20  st entry on the 
17bf6 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  page..**.** The 
17bf7 70 53 70 61 63 65 20 62 75 66 66 65 72 20 69 73  pSpace buffer is
17bf8 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 61   used to store a
17bf9 20 74 65 6d 70 6f 72 61 72 79 20 63 6f 70 79 20   temporary copy 
17bfa 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 0a 2a  of the divider.*
17bfb 2a 20 63 65 6c 6c 20 74 68 61 74 20 77 69 6c 6c  * cell that will
17bfc 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74   be inserted int
17bfd 6f 20 70 50 61 72 65 6e 74 2e 20 53 75 63 68 20  o pParent. Such 
17bfe 61 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74 73 20  a cell consists 
17bff 6f 66 20 61 20 34 0a 2a 2a 20 62 79 74 65 20 70  of a 4.** byte p
17c00 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 6c 6c 6f  age number follo
17c01 77 65 64 20 62 79 20 61 20 76 61 72 69 61 62 6c  wed by a variabl
17c02 65 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e length integer
17c03 2e 20 49 6e 20 6f 74 68 65 72 0a 2a 2a 20 77 6f  . In other.** wo
17c04 72 64 73 2c 20 61 74 20 6d 6f 73 74 20 31 33 20  rds, at most 13 
17c05 62 79 74 65 73 2e 20 48 65 6e 63 65 20 74 68 65  bytes. Hence the
17c06 20 70 53 70 61 63 65 20 62 75 66 66 65 72 20 6d   pSpace buffer m
17c07 75 73 74 20 62 65 20 61 74 0a 2a 2a 20 6c 65 61  ust be at.** lea
17c08 73 74 20 31 33 20 62 79 74 65 73 20 69 6e 20 73  st 13 bytes in s
17c09 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ize..*/.static i
17c0a 6e 74 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b  nt balance_quick
17c0b 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e  (MemPage *pParen
17c0c 74 2c 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  t, MemPage *pPag
17c0d 65 2c 20 75 38 20 2a 70 53 70 61 63 65 29 7b 0a  e, u8 *pSpace){.
17c0e 20 20 42 74 53 68 61 72 65 64 20 2a 63 6f 6e 73    BtShared *cons
17c0f 74 20 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70  t pBt = pPage->p
17c10 42 74 3b 20 20 20 20 2f 2a 20 42 2d 54 72 65 65  Bt;    /* B-Tree
17c11 20 44 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 4d   Database */.  M
17c12 65 6d 50 61 67 65 20 2a 70 4e 65 77 3b 20 20 20  emPage *pNew;   
17c13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17c14 20 20 20 20 2f 2a 20 4e 65 77 6c 79 20 61 6c 6c      /* Newly all
17c15 6f 63 61 74 65 64 20 70 61 67 65 20 2a 2f 0a 20  ocated page */. 
17c16 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
17c17 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17c18 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20        /* Return 
17c19 43 6f 64 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70  Code */.  Pgno p
17c1a 67 6e 6f 4e 65 77 3b 20 20 20 20 20 20 20 20 20  gnoNew;         
17c1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
17c1c 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  * Page number of
17c1d 20 70 4e 65 77 20 2a 2f 0a 0a 20 20 61 73 73 65   pNew */..  asse
17c1e 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
17c1f 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42  x_held(pPage->pB
17c20 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  t->mutex) );.  a
17c21 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61  ssert( sqlite3Pa
17c22 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70  gerIswriteable(p
17c23 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29  Parent->pDbPage)
17c24 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
17c25 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d  age->nOverflow==
17c26 31 20 29 3b 0a 0a 20 20 69 66 28 20 70 50 61 67  1 );..  if( pPag
17c27 65 2d 3e 6e 43 65 6c 6c 3c 3d 30 20 29 20 72 65  e->nCell<=0 ) re
17c28 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
17c29 55 50 54 5f 42 4b 50 54 3b 0a 0a 20 20 2f 2a 20  UPT_BKPT;..  /* 
17c2a 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70  Allocate a new p
17c2b 61 67 65 2e 20 54 68 69 73 20 70 61 67 65 20 77  age. This page w
17c2c 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 72  ill become the r
17c2d 69 67 68 74 2d 73 69 62 6c 69 6e 67 20 6f 66 20  ight-sibling of 
17c2e 0a 20 20 2a 2a 20 70 50 61 67 65 2e 20 4d 61 6b  .  ** pPage. Mak
17c2f 65 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  e the parent pag
17c30 65 20 77 72 69 74 61 62 6c 65 2c 20 73 6f 20 74  e writable, so t
17c31 68 61 74 20 74 68 65 20 6e 65 77 20 64 69 76 69  hat the new divi
17c32 64 65 72 20 63 65 6c 6c 0a 20 20 2a 2a 20 6d 61  der cell.  ** ma
17c33 79 20 62 65 20 69 6e 73 65 72 74 65 64 2e 20 49  y be inserted. I
17c34 66 20 62 6f 74 68 20 74 68 65 73 65 20 6f 70 65  f both these ope
17c35 72 61 74 69 6f 6e 73 20 61 72 65 20 73 75 63 63  rations are succ
17c36 65 73 73 66 75 6c 2c 20 70 72 6f 63 65 65 64 2e  essful, proceed.
17c37 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 61 6c 6c  .  */.  rc = all
17c38 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70  ocateBtreePage(p
17c39 42 74 2c 20 26 70 4e 65 77 2c 20 26 70 67 6e 6f  Bt, &pNew, &pgno
17c3a 4e 65 77 2c 20 30 2c 20 30 29 3b 0a 0a 20 20 69  New, 0, 0);..  i
17c3b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
17c3c 20 29 7b 0a 0a 20 20 20 20 75 38 20 2a 70 4f 75   ){..    u8 *pOu
17c3d 74 20 3d 20 26 70 53 70 61 63 65 5b 34 5d 3b 0a  t = &pSpace[4];.
17c3e 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20      u8 *pCell = 
17c3f 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d 2e  pPage->aOvfl[0].
17c40 70 43 65 6c 6c 3b 0a 20 20 20 20 75 31 36 20 73  pCell;.    u16 s
17c41 7a 43 65 6c 6c 20 3d 20 63 65 6c 6c 53 69 7a 65  zCell = cellSize
17c42 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c  Ptr(pPage, pCell
17c43 29 3b 0a 20 20 20 20 75 38 20 2a 70 53 74 6f 70  );.    u8 *pStop
17c44 3b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  ;..    assert( s
17c45 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
17c46 74 65 61 62 6c 65 28 70 4e 65 77 2d 3e 70 44 62  teable(pNew->pDb
17c47 50 61 67 65 29 20 29 3b 0a 20 20 20 20 61 73 73  Page) );.    ass
17c48 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44 61 74  ert( pPage->aDat
17c49 61 5b 30 5d 3d 3d 28 50 54 46 5f 49 4e 54 4b 45  a[0]==(PTF_INTKE
17c4a 59 7c 50 54 46 5f 4c 45 41 46 44 41 54 41 7c 50  Y|PTF_LEAFDATA|P
17c4b 54 46 5f 4c 45 41 46 29 20 29 3b 0a 20 20 20 20  TF_LEAF) );.    
17c4c 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 2c 20 50  zeroPage(pNew, P
17c4d 54 46 5f 49 4e 54 4b 45 59 7c 50 54 46 5f 4c 45  TF_INTKEY|PTF_LE
17c4e 41 46 44 41 54 41 7c 50 54 46 5f 4c 45 41 46 29  AFDATA|PTF_LEAF)
17c4f 3b 0a 20 20 20 20 61 73 73 65 6d 62 6c 65 50 61  ;.    assemblePa
17c50 67 65 28 70 4e 65 77 2c 20 31 2c 20 26 70 43 65  ge(pNew, 1, &pCe
17c51 6c 6c 2c 20 26 73 7a 43 65 6c 6c 29 3b 0a 0a 20  ll, &szCell);.. 
17c52 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73     /* If this is
17c53 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   an auto-vacuum 
17c54 64 61 74 61 62 61 73 65 2c 20 75 70 64 61 74 65  database, update
17c55 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   the pointer map
17c56 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 65 6e 74  .    ** with ent
17c57 72 69 65 73 20 66 6f 72 20 74 68 65 20 6e 65 77  ries for the new
17c58 20 70 61 67 65 2c 20 61 6e 64 20 61 6e 79 20 70   page, and any p
17c59 6f 69 6e 74 65 72 20 66 72 6f 6d 20 74 68 65 20  ointer from the 
17c5a 0a 20 20 20 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20  .    ** cell on 
17c5b 74 68 65 20 70 61 67 65 20 74 6f 20 61 6e 20 6f  the page to an o
17c5c 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 20 49 66  verflow page. If
17c5d 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65   either of these
17c5e 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f  .    ** operatio
17c5f 6e 73 20 66 61 69 6c 73 2c 20 74 68 65 20 72 65  ns fails, the re
17c60 74 75 72 6e 20 63 6f 64 65 20 69 73 20 73 65 74  turn code is set
17c61 2c 20 62 75 74 20 74 68 65 20 63 6f 6e 74 65 6e  , but the conten
17c62 74 73 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65  ts.    ** of the
17c63 20 70 61 72 65 6e 74 20 70 61 67 65 20 61 72 65   parent page are
17c64 20 73 74 69 6c 6c 20 6d 61 6e 69 70 75 6c 61 74   still manipulat
17c65 65 64 20 62 79 20 74 68 68 20 63 6f 64 65 20 62  ed by thh code b
17c66 65 6c 6f 77 2e 0a 20 20 20 20 2a 2a 20 54 68 61  elow..    ** Tha
17c67 74 20 69 73 20 4f 6b 2c 20 61 74 20 74 68 69 73  t is Ok, at this
17c68 20 70 6f 69 6e 74 20 74 68 65 20 70 61 72 65 6e   point the paren
17c69 74 20 70 61 67 65 20 69 73 20 67 75 61 72 61 6e  t page is guaran
17c6a 74 65 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20 62  teed to.    ** b
17c6b 65 20 6d 61 72 6b 65 64 20 61 73 20 64 69 72 74  e marked as dirt
17c6c 79 2e 20 52 65 74 75 72 6e 69 6e 67 20 61 6e 20  y. Returning an 
17c6d 65 72 72 6f 72 20 63 6f 64 65 20 77 69 6c 6c 20  error code will 
17c6e 63 61 75 73 65 20 61 0a 20 20 20 20 2a 2a 20 72  cause a.    ** r
17c6f 6f 6c 6c 62 61 63 6b 2c 20 75 6e 64 6f 69 6e 67  ollback, undoing
17c70 20 61 6e 79 20 63 68 61 6e 67 65 73 20 6d 61 64   any changes mad
17c71 65 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20  e to the parent 
17c72 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  page..    */.   
17c73 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55   if( ISAUTOVACUU
17c74 4d 20 29 7b 0a 20 20 20 20 20 20 70 74 72 6d 61  M ){.      ptrma
17c75 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 4e 65  pPut(pBt, pgnoNe
17c76 77 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c  w, PTRMAP_BTREE,
17c77 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e 6f 2c 20   pParent->pgno, 
17c78 26 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20  &rc);.      if( 
17c79 73 7a 43 65 6c 6c 3e 70 4e 65 77 2d 3e 6d 69 6e  szCell>pNew->min
17c7a 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20 20  Local ){.       
17c7b 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 50 74   ptrmapPutOvflPt
17c7c 72 28 70 4e 65 77 2c 20 70 43 65 6c 6c 2c 20 26  r(pNew, pCell, &
17c7d 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rc);.      }.   
17c7e 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 43 72 65   }.  .    /* Cre
17c7f 61 74 65 20 61 20 64 69 76 69 64 65 72 20 63 65  ate a divider ce
17c80 6c 6c 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74  ll to insert int
17c81 6f 20 70 50 61 72 65 6e 74 2e 20 54 68 65 20 64  o pParent. The d
17c82 69 76 69 64 65 72 20 63 65 6c 6c 0a 20 20 20 20  ivider cell.    
17c83 2a 2a 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61  ** consists of a
17c84 20 34 2d 62 79 74 65 20 70 61 67 65 20 6e 75 6d   4-byte page num
17c85 62 65 72 20 28 74 68 65 20 70 61 67 65 20 6e 75  ber (the page nu
17c86 6d 62 65 72 20 6f 66 20 70 50 61 67 65 29 20 61  mber of pPage) a
17c87 6e 64 0a 20 20 20 20 2a 2a 20 61 20 76 61 72 69  nd.    ** a vari
17c88 61 62 6c 65 20 6c 65 6e 67 74 68 20 6b 65 79 20  able length key 
17c89 76 61 6c 75 65 20 28 77 68 69 63 68 20 6d 75 73  value (which mus
17c8a 74 20 62 65 20 74 68 65 20 73 61 6d 65 20 76 61  t be the same va
17c8b 6c 75 65 20 61 73 20 74 68 65 0a 20 20 20 20 2a  lue as the.    *
17c8c 2a 20 6c 61 72 67 65 73 74 20 6b 65 79 20 6f 6e  * largest key on
17c8d 20 70 50 61 67 65 29 2e 0a 20 20 20 20 2a 2a 0a   pPage)..    **.
17c8e 20 20 20 20 2a 2a 20 54 6f 20 66 69 6e 64 20 74      ** To find t
17c8f 68 65 20 6c 61 72 67 65 73 74 20 6b 65 79 20 76  he largest key v
17c90 61 6c 75 65 20 6f 6e 20 70 50 61 67 65 2c 20 66  alue on pPage, f
17c91 69 72 73 74 20 66 69 6e 64 20 74 68 65 20 72 69  irst find the ri
17c92 67 68 74 2d 6d 6f 73 74 20 0a 20 20 20 20 2a 2a  ght-most .    **
17c93 20 63 65 6c 6c 20 6f 6e 20 70 50 61 67 65 2e 20   cell on pPage. 
17c94 54 68 65 20 66 69 72 73 74 20 74 77 6f 20 66 69  The first two fi
17c95 65 6c 64 73 20 6f 66 20 74 68 69 73 20 63 65 6c  elds of this cel
17c96 6c 20 61 72 65 20 74 68 65 20 0a 20 20 20 20 2a  l are the .    *
17c97 2a 20 72 65 63 6f 72 64 2d 6c 65 6e 67 74 68 20  * record-length 
17c98 28 61 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67  (a variable leng
17c99 74 68 20 69 6e 74 65 67 65 72 20 61 74 20 6d 6f  th integer at mo
17c9a 73 74 20 33 32 2d 62 69 74 73 20 69 6e 20 73 69  st 32-bits in si
17c9b 7a 65 29 0a 20 20 20 20 2a 2a 20 61 6e 64 20 74  ze).    ** and t
17c9c 68 65 20 6b 65 79 20 76 61 6c 75 65 20 28 61 20  he key value (a 
17c9d 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68 20  variable length 
17c9e 69 6e 74 65 67 65 72 2c 20 6d 61 79 20 68 61 76  integer, may hav
17c9f 65 20 61 6e 79 20 76 61 6c 75 65 29 2e 0a 20 20  e any value)..  
17ca0 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 6f    ** The first o
17ca1 66 20 74 68 65 20 77 68 69 6c 65 28 2e 2e 2e 29  f the while(...)
17ca2 20 6c 6f 6f 70 73 20 62 65 6c 6f 77 20 73 6b 69   loops below ski
17ca3 70 73 20 6f 76 65 72 20 74 68 65 20 72 65 63 6f  ps over the reco
17ca4 72 64 2d 6c 65 6e 67 74 68 0a 20 20 20 20 2a 2a  rd-length.    **
17ca5 20 66 69 65 6c 64 2e 20 54 68 65 20 73 65 63 6f   field. The seco
17ca6 6e 64 20 77 68 69 6c 65 28 2e 2e 2e 29 20 6c 6f  nd while(...) lo
17ca7 6f 70 20 63 6f 70 69 65 73 20 74 68 65 20 6b 65  op copies the ke
17ca8 79 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65  y value from the
17ca9 0a 20 20 20 20 2a 2a 20 63 65 6c 6c 20 6f 6e 20  .    ** cell on 
17caa 70 50 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70  pPage into the p
17cab 53 70 61 63 65 20 62 75 66 66 65 72 2e 0a 20 20  Space buffer..  
17cac 20 20 2a 2f 0a 20 20 20 20 70 43 65 6c 6c 20 3d    */.    pCell =
17cad 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c   findCell(pPage,
17cae 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31 29   pPage->nCell-1)
17caf 3b 0a 20 20 20 20 70 53 74 6f 70 20 3d 20 26 70  ;.    pStop = &p
17cb0 43 65 6c 6c 5b 39 5d 3b 0a 20 20 20 20 77 68 69  Cell[9];.    whi
17cb1 6c 65 28 20 28 2a 28 70 43 65 6c 6c 2b 2b 29 26  le( (*(pCell++)&
17cb2 30 78 38 30 29 20 26 26 20 70 43 65 6c 6c 3c 70  0x80) && pCell<p
17cb3 53 74 6f 70 20 29 3b 0a 20 20 20 20 70 53 74 6f  Stop );.    pSto
17cb4 70 20 3d 20 26 70 43 65 6c 6c 5b 39 5d 3b 0a 20  p = &pCell[9];. 
17cb5 20 20 20 77 68 69 6c 65 28 20 28 28 2a 28 70 4f     while( ((*(pO
17cb6 75 74 2b 2b 29 20 3d 20 2a 28 70 43 65 6c 6c 2b  ut++) = *(pCell+
17cb7 2b 29 29 26 30 78 38 30 29 20 26 26 20 70 43 65  +))&0x80) && pCe
17cb8 6c 6c 3c 70 53 74 6f 70 20 29 3b 0a 0a 20 20 20  ll<pStop );..   
17cb9 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 65 20 6e   /* Insert the n
17cba 65 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20  ew divider cell 
17cbb 69 6e 74 6f 20 70 50 61 72 65 6e 74 2e 20 2a 2f  into pParent. */
17cbc 0a 20 20 20 20 69 6e 73 65 72 74 43 65 6c 6c 28  .    insertCell(
17cbd 70 50 61 72 65 6e 74 2c 20 70 50 61 72 65 6e 74  pParent, pParent
17cbe 2d 3e 6e 43 65 6c 6c 2c 20 70 53 70 61 63 65 2c  ->nCell, pSpace,
17cbf 20 28 69 6e 74 29 28 70 4f 75 74 2d 70 53 70 61   (int)(pOut-pSpa
17cc0 63 65 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ce),.           
17cc1 20 20 20 20 30 2c 20 70 50 61 67 65 2d 3e 70 67      0, pPage->pg
17cc2 6e 6f 2c 20 26 72 63 29 3b 0a 0a 20 20 20 20 2f  no, &rc);..    /
17cc3 2a 20 53 65 74 20 74 68 65 20 72 69 67 68 74 2d  * Set the right-
17cc4 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 20 6f 66  child pointer of
17cc5 20 70 50 61 72 65 6e 74 20 74 6f 20 70 6f 69 6e   pParent to poin
17cc6 74 20 74 6f 20 74 68 65 20 6e 65 77 20 70 61 67  t to the new pag
17cc7 65 2e 20 2a 2f 0a 20 20 20 20 70 75 74 34 62 79  e. */.    put4by
17cc8 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44 61  te(&pParent->aDa
17cc9 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72 4f  ta[pParent->hdrO
17cca 66 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 4e 65  ffset+8], pgnoNe
17ccb 77 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65  w);.  .    /* Re
17ccc 6c 65 61 73 65 20 74 68 65 20 72 65 66 65 72 65  lease the refere
17ccd 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 77 20 70  nce to the new p
17cce 61 67 65 2e 20 2a 2f 0a 20 20 20 20 72 65 6c 65  age. */.    rele
17ccf 61 73 65 50 61 67 65 28 70 4e 65 77 29 3b 0a 20  asePage(pNew);. 
17cd0 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
17cd1 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
17cd2 49 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41  ITE_OMIT_QUICKBA
17cd3 4c 41 4e 43 45 20 2a 2f 0a 0a 23 69 66 20 30 0a  LANCE */..#if 0.
17cd4 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
17cd5 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 63 6f 6e  ion does not con
17cd6 74 72 69 62 75 74 65 20 61 6e 79 74 68 69 6e 67  tribute anything
17cd7 20 74 6f 20 74 68 65 20 6f 70 65 72 61 74 69 6f   to the operatio
17cd8 6e 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20  n of SQLite..** 
17cd9 69 74 20 69 73 20 73 6f 6d 65 74 69 6d 65 73 20  it is sometimes 
17cda 61 63 74 69 76 61 74 65 64 20 74 65 6d 70 6f 72  activated tempor
17cdb 61 72 69 6c 79 20 77 68 69 6c 65 20 64 65 62 75  arily while debu
17cdc 67 67 69 6e 67 20 63 6f 64 65 20 72 65 73 70 6f  gging code respo
17cdd 6e 73 69 62 6c 65 20 0a 2a 2a 20 66 6f 72 20 73  nsible .** for s
17cde 65 74 74 69 6e 67 20 70 6f 69 6e 74 65 72 2d 6d  etting pointer-m
17cdf 61 70 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73  ap entries..*/.s
17ce0 74 61 74 69 63 20 69 6e 74 20 70 74 72 6d 61 70  tatic int ptrmap
17ce1 43 68 65 63 6b 50 61 67 65 73 28 4d 65 6d 50 61  CheckPages(MemPa
17ce2 67 65 20 2a 2a 61 70 50 61 67 65 2c 20 69 6e 74  ge **apPage, int
17ce3 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 69   nPage){.  int i
17ce4 2c 20 6a 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  , j;.  for(i=0; 
17ce5 69 3c 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20  i<nPage; i++){. 
17ce6 20 20 20 50 67 6e 6f 20 6e 3b 0a 20 20 20 20 75     Pgno n;.    u
17ce7 38 20 65 3b 0a 20 20 20 20 4d 65 6d 50 61 67 65  8 e;.    MemPage
17ce8 20 2a 70 50 61 67 65 20 3d 20 61 70 50 61 67 65   *pPage = apPage
17ce9 5b 69 5d 3b 0a 20 20 20 20 42 74 53 68 61 72 65  [i];.    BtShare
17cea 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e  d *pBt = pPage->
17ceb 70 42 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28  pBt;.    assert(
17cec 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29   pPage->isInit )
17ced 3b 0a 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20  ;..    for(j=0; 
17cee 6a 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20  j<pPage->nCell; 
17cef 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 43 65 6c 6c  j++){.      Cell
17cf0 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20 20 20 20 20  Info info;.     
17cf1 20 75 38 20 2a 7a 3b 0a 20 20 20 20 20 0a 20 20   u8 *z;.     .  
17cf2 20 20 20 20 7a 20 3d 20 66 69 6e 64 43 65 6c 6c      z = findCell
17cf3 28 70 50 61 67 65 2c 20 6a 29 3b 0a 20 20 20 20  (pPage, j);.    
17cf4 20 20 62 74 72 65 65 50 61 72 73 65 43 65 6c 6c    btreeParseCell
17cf5 50 74 72 28 70 50 61 67 65 2c 20 7a 2c 20 26 69  Ptr(pPage, z, &i
17cf6 6e 66 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20  nfo);.      if( 
17cf7 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 20 29  info.iOverflow )
17cf8 7b 0a 20 20 20 20 20 20 20 20 50 67 6e 6f 20 6f  {.        Pgno o
17cf9 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65 28 26  vfl = get4byte(&
17cfa 7a 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77  z[info.iOverflow
17cfb 5d 29 3b 0a 20 20 20 20 20 20 20 20 70 74 72 6d  ]);.        ptrm
17cfc 61 70 47 65 74 28 70 42 74 2c 20 6f 76 66 6c 2c  apGet(pBt, ovfl,
17cfd 20 26 65 2c 20 26 6e 29 3b 0a 20 20 20 20 20 20   &e, &n);.      
17cfe 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 70 50 61    assert( n==pPa
17cff 67 65 2d 3e 70 67 6e 6f 20 26 26 20 65 3d 3d 50  ge->pgno && e==P
17d00 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20  TRMAP_OVERFLOW1 
17d01 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
17d02 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
17d03 66 20 29 7b 0a 20 20 20 20 20 20 20 20 50 67 6e  f ){.        Pgn
17d04 6f 20 63 68 69 6c 64 20 3d 20 67 65 74 34 62 79  o child = get4by
17d05 74 65 28 7a 29 3b 0a 20 20 20 20 20 20 20 20 70  te(z);.        p
17d06 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 63 68  trmapGet(pBt, ch
17d07 69 6c 64 2c 20 26 65 2c 20 26 6e 29 3b 0a 20 20  ild, &e, &n);.  
17d08 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3d        assert( n=
17d09 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 26 26 20  =pPage->pgno && 
17d0a 65 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20  e==PTRMAP_BTREE 
17d0b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
17d0c 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d  .    if( !pPage-
17d0d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 50  >leaf ){.      P
17d0e 67 6e 6f 20 63 68 69 6c 64 20 3d 20 67 65 74 34  gno child = get4
17d0f 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61  byte(&pPage->aDa
17d10 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66  ta[pPage->hdrOff
17d11 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 20 20 70  set+8]);.      p
17d12 74 72 6d 61 70 47 65 74 28 70 42 74 2c 20 63 68  trmapGet(pBt, ch
17d13 69 6c 64 2c 20 26 65 2c 20 26 6e 29 3b 0a 20 20  ild, &e, &n);.  
17d14 20 20 20 20 61 73 73 65 72 74 28 20 6e 3d 3d 70      assert( n==p
17d15 50 61 67 65 2d 3e 70 67 6e 6f 20 26 26 20 65 3d  Page->pgno && e=
17d16 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20 29 3b  =PTRMAP_BTREE );
17d17 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
17d18 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a  urn 1;.}.#endif.
17d19 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
17d1a 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
17d1b 63 6f 70 79 20 74 68 65 20 63 6f 6e 74 65 6e 74  copy the content
17d1c 73 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65 20  s of the b-tree 
17d1d 6e 6f 64 65 20 73 74 6f 72 65 64 20 0a 2a 2a 20  node stored .** 
17d1e 6f 6e 20 70 61 67 65 20 70 46 72 6f 6d 20 74 6f  on page pFrom to
17d1f 20 70 61 67 65 20 70 54 6f 2e 20 49 66 20 70 61   page pTo. If pa
17d20 67 65 20 70 46 72 6f 6d 20 77 61 73 20 6e 6f 74  ge pFrom was not
17d21 20 61 20 6c 65 61 66 20 70 61 67 65 2c 20 74 68   a leaf page, th
17d22 65 6e 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65  en.** the pointe
17d23 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f  r-map entries fo
17d24 72 20 65 61 63 68 20 63 68 69 6c 64 20 70 61 67  r each child pag
17d25 65 20 61 72 65 20 75 70 64 61 74 65 64 20 73 6f  e are updated so
17d26 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 70 61 72   that the.** par
17d27 65 6e 74 20 70 61 67 65 20 73 74 6f 72 65 64 20  ent page stored 
17d28 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d  in the pointer m
17d29 61 70 20 69 73 20 70 61 67 65 20 70 54 6f 2e 20  ap is page pTo. 
17d2a 49 66 20 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e  If pFrom contain
17d2b 65 64 0a 2a 2a 20 61 6e 79 20 63 65 6c 6c 73 20  ed.** any cells 
17d2c 77 69 74 68 20 6f 76 65 72 66 6c 6f 77 20 70 61  with overflow pa
17d2d 67 65 20 70 6f 69 6e 74 65 72 73 2c 20 74 68 65  ge pointers, the
17d2e 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  n the correspond
17d2f 69 6e 67 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 6d  ing pointer.** m
17d30 61 70 20 65 6e 74 72 69 65 73 20 61 72 65 20 61  ap entries are a
17d31 6c 73 6f 20 75 70 64 61 74 65 64 20 73 6f 20 74  lso updated so t
17d32 68 61 74 20 74 68 65 20 70 61 72 65 6e 74 20 70  hat the parent p
17d33 61 67 65 20 69 73 20 70 61 67 65 20 70 54 6f 2e  age is page pTo.
17d34 0a 2a 2a 0a 2a 2a 20 49 66 20 70 46 72 6f 6d 20  .**.** If pFrom 
17d35 69 73 20 63 75 72 72 65 6e 74 6c 79 20 63 61 72  is currently car
17d36 72 79 69 6e 67 20 61 6e 79 20 6f 76 65 72 66 6c  rying any overfl
17d37 6f 77 20 63 65 6c 6c 73 20 28 65 6e 74 72 69 65  ow cells (entrie
17d38 73 20 69 6e 20 74 68 65 0a 2a 2a 20 4d 65 6d 50  s in the.** MemP
17d39 61 67 65 2e 61 4f 76 66 6c 5b 5d 20 61 72 72 61  age.aOvfl[] arra
17d3a 79 29 2c 20 74 68 65 79 20 61 72 65 20 6e 6f 74  y), they are not
17d3b 20 63 6f 70 69 65 64 20 74 6f 20 70 54 6f 2e 20   copied to pTo. 
17d3c 0a 2a 2a 0a 2a 2a 20 42 65 66 6f 72 65 20 72 65  .**.** Before re
17d3d 74 75 72 6e 69 6e 67 2c 20 70 61 67 65 20 70 54  turning, page pT
17d3e 6f 20 69 73 20 72 65 69 6e 69 74 69 61 6c 69 7a  o is reinitializ
17d3f 65 64 20 75 73 69 6e 67 20 62 74 72 65 65 49 6e  ed using btreeIn
17d40 69 74 50 61 67 65 28 29 2e 0a 2a 2a 0a 2a 2a 20  itPage()..**.** 
17d41 54 68 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20  The performance 
17d42 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  of this function
17d43 20 69 73 20 6e 6f 74 20 63 72 69 74 69 63 61 6c   is not critical
17d44 2e 20 49 74 20 69 73 20 6f 6e 6c 79 20 75 73 65  . It is only use
17d45 64 20 62 79 20 0a 2a 2a 20 74 68 65 20 62 61 6c  d by .** the bal
17d46 61 6e 63 65 5f 73 68 61 6c 6c 6f 77 65 72 28 29  ance_shallower()
17d47 20 61 6e 64 20 62 61 6c 61 6e 63 65 5f 64 65 65   and balance_dee
17d48 70 65 72 28 29 20 70 72 6f 63 65 64 75 72 65 73  per() procedures
17d49 2c 20 6e 65 69 74 68 65 72 20 6f 66 0a 2a 2a 20  , neither of.** 
17d4a 77 68 69 63 68 20 61 72 65 20 63 61 6c 6c 65 64  which are called
17d4b 20 6f 66 74 65 6e 20 75 6e 64 65 72 20 6e 6f 72   often under nor
17d4c 6d 61 6c 20 63 69 72 63 75 6d 73 74 61 6e 63 65  mal circumstance
17d4d 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
17d4e 64 20 63 6f 70 79 4e 6f 64 65 43 6f 6e 74 65 6e  d copyNodeConten
17d4f 74 28 4d 65 6d 50 61 67 65 20 2a 70 46 72 6f 6d  t(MemPage *pFrom
17d50 2c 20 4d 65 6d 50 61 67 65 20 2a 70 54 6f 2c 20  , MemPage *pTo, 
17d51 69 6e 74 20 2a 70 52 43 29 7b 0a 20 20 69 66 28  int *pRC){.  if(
17d52 20 28 2a 70 52 43 29 3d 3d 53 51 4c 49 54 45 5f   (*pRC)==SQLITE_
17d53 4f 4b 20 29 7b 0a 20 20 20 20 42 74 53 68 61 72  OK ){.    BtShar
17d54 65 64 20 2a 20 63 6f 6e 73 74 20 70 42 74 20 3d  ed * const pBt =
17d55 20 70 46 72 6f 6d 2d 3e 70 42 74 3b 0a 20 20 20   pFrom->pBt;.   
17d56 20 75 38 20 2a 20 63 6f 6e 73 74 20 61 46 72 6f   u8 * const aFro
17d57 6d 20 3d 20 70 46 72 6f 6d 2d 3e 61 44 61 74 61  m = pFrom->aData
17d58 3b 0a 20 20 20 20 75 38 20 2a 20 63 6f 6e 73 74  ;.    u8 * const
17d59 20 61 54 6f 20 3d 20 70 54 6f 2d 3e 61 44 61 74   aTo = pTo->aDat
17d5a 61 3b 0a 20 20 20 20 69 6e 74 20 63 6f 6e 73 74  a;.    int const
17d5b 20 69 46 72 6f 6d 48 64 72 20 3d 20 70 46 72 6f   iFromHdr = pFro
17d5c 6d 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20  m->hdrOffset;.  
17d5d 20 20 69 6e 74 20 63 6f 6e 73 74 20 69 54 6f 48    int const iToH
17d5e 64 72 20 3d 20 28 28 70 54 6f 2d 3e 70 67 6e 6f  dr = ((pTo->pgno
17d5f 3d 3d 31 29 20 3f 20 31 30 30 20 3a 20 30 29 3b  ==1) ? 100 : 0);
17d60 0a 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 69 6e  .    TESTONLY(in
17d61 74 20 72 63 3b 29 0a 20 20 20 20 69 6e 74 20 69  t rc;).    int i
17d62 44 61 74 61 3b 0a 20 20 0a 20 20 0a 20 20 20 20  Data;.  .  .    
17d63 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 69  assert( pFrom->i
17d64 73 49 6e 69 74 20 29 3b 0a 20 20 20 20 61 73 73  sInit );.    ass
17d65 65 72 74 28 20 70 46 72 6f 6d 2d 3e 6e 46 72 65  ert( pFrom->nFre
17d66 65 3e 3d 69 54 6f 48 64 72 20 29 3b 0a 20 20 20  e>=iToHdr );.   
17d67 20 61 73 73 65 72 74 28 20 67 65 74 32 62 79 74   assert( get2byt
17d68 65 28 26 61 46 72 6f 6d 5b 69 46 72 6f 6d 48 64  e(&aFrom[iFromHd
17d69 72 2b 35 5d 29 3c 3d 70 42 74 2d 3e 75 73 61 62  r+5])<=pBt->usab
17d6a 6c 65 53 69 7a 65 20 29 3b 0a 20 20 0a 20 20 20  leSize );.  .   
17d6b 20 2f 2a 20 43 6f 70 79 20 74 68 65 20 62 2d 74   /* Copy the b-t
17d6c 72 65 65 20 6e 6f 64 65 20 63 6f 6e 74 65 6e 74  ree node content
17d6d 20 66 72 6f 6d 20 70 61 67 65 20 70 46 72 6f 6d   from page pFrom
17d6e 20 74 6f 20 70 61 67 65 20 70 54 6f 2e 20 2a 2f   to page pTo. */
17d6f 0a 20 20 20 20 69 44 61 74 61 20 3d 20 67 65 74  .    iData = get
17d70 32 62 79 74 65 28 26 61 46 72 6f 6d 5b 69 46 72  2byte(&aFrom[iFr
17d71 6f 6d 48 64 72 2b 35 5d 29 3b 0a 20 20 20 20 6d  omHdr+5]);.    m
17d72 65 6d 63 70 79 28 26 61 54 6f 5b 69 44 61 74 61  emcpy(&aTo[iData
17d73 5d 2c 20 26 61 46 72 6f 6d 5b 69 44 61 74 61 5d  ], &aFrom[iData]
17d74 2c 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  , pBt->usableSiz
17d75 65 2d 69 44 61 74 61 29 3b 0a 20 20 20 20 6d 65  e-iData);.    me
17d76 6d 63 70 79 28 26 61 54 6f 5b 69 54 6f 48 64 72  mcpy(&aTo[iToHdr
17d77 5d 2c 20 26 61 46 72 6f 6d 5b 69 46 72 6f 6d 48  ], &aFrom[iFromH
17d78 64 72 5d 2c 20 70 46 72 6f 6d 2d 3e 63 65 6c 6c  dr], pFrom->cell
17d79 4f 66 66 73 65 74 20 2b 20 32 2a 70 46 72 6f 6d  Offset + 2*pFrom
17d7a 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 20 0a 20 20 20  ->nCell);.  .   
17d7b 20 2f 2a 20 52 65 69 6e 69 74 69 61 6c 69 7a 65   /* Reinitialize
17d7c 20 70 61 67 65 20 70 54 6f 20 73 6f 20 74 68 61   page pTo so tha
17d7d 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  t the contents o
17d7e 66 20 74 68 65 20 4d 65 6d 50 61 67 65 20 73 74  f the MemPage st
17d7f 72 75 63 74 75 72 65 0a 20 20 20 20 2a 2a 20 6d  ructure.    ** m
17d80 61 74 63 68 20 74 68 65 20 6e 65 77 20 64 61 74  atch the new dat
17d81 61 2e 20 54 68 65 20 69 6e 69 74 69 61 6c 69 7a  a. The initializ
17d82 61 74 69 6f 6e 20 6f 66 20 70 54 6f 20 22 63 61  ation of pTo "ca
17d83 6e 6e 6f 74 22 20 66 61 69 6c 2c 20 61 73 20 74  nnot" fail, as t
17d84 68 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 20 63  he.    ** data c
17d85 6f 70 69 65 64 20 66 72 6f 6d 20 70 46 72 6f 6d  opied from pFrom
17d86 20 69 73 20 6b 6e 6f 77 6e 20 74 6f 20 62 65 20   is known to be 
17d87 76 61 6c 69 64 2e 20 20 2a 2f 0a 20 20 20 20 70  valid.  */.    p
17d88 54 6f 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a  To->isInit = 0;.
17d89 20 20 20 20 54 45 53 54 4f 4e 4c 59 28 72 63 20      TESTONLY(rc 
17d8a 3d 20 29 20 62 74 72 65 65 49 6e 69 74 50 61 67  = ) btreeInitPag
17d8b 65 28 70 54 6f 29 3b 0a 20 20 20 20 61 73 73 65  e(pTo);.    asse
17d8c 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
17d8d 4b 20 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49  K );.  .    /* I
17d8e 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74  f this is an aut
17d8f 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73  o-vacuum databas
17d90 65 2c 20 75 70 64 61 74 65 20 74 68 65 20 70 6f  e, update the po
17d91 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65  inter-map entrie
17d92 73 0a 20 20 20 20 2a 2a 20 66 6f 72 20 61 6e 79  s.    ** for any
17d93 20 62 2d 74 72 65 65 20 6f 72 20 6f 76 65 72 66   b-tree or overf
17d94 6c 6f 77 20 70 61 67 65 73 20 74 68 61 74 20 70  low pages that p
17d95 54 6f 20 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 20  To now contains 
17d96 74 68 65 20 70 6f 69 6e 74 65 72 73 20 74 6f 2e  the pointers to.
17d97 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
17d98 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a  ISAUTOVACUUM ){.
17d99 20 20 20 20 20 20 2a 70 52 43 20 3d 20 73 65 74        *pRC = set
17d9a 43 68 69 6c 64 50 74 72 6d 61 70 73 28 70 54 6f  ChildPtrmaps(pTo
17d9b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
17d9c 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
17d9d 6e 65 20 72 65 64 69 73 74 72 69 62 75 74 65 73  ne redistributes
17d9e 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65 20 69 50   cells on the iP
17d9f 61 72 65 6e 74 49 64 78 27 74 68 20 63 68 69 6c  arentIdx'th chil
17da0 64 20 6f 66 20 70 50 61 72 65 6e 74 0a 2a 2a 20  d of pParent.** 
17da1 28 68 65 72 65 61 66 74 65 72 20 22 74 68 65 20  (hereafter "the 
17da2 70 61 67 65 22 29 20 61 6e 64 20 75 70 20 74 6f  page") and up to
17da3 20 32 20 73 69 62 6c 69 6e 67 73 20 73 6f 20 74   2 siblings so t
17da4 68 61 74 20 61 6c 6c 20 70 61 67 65 73 20 68 61  hat all pages ha
17da5 76 65 20 61 62 6f 75 74 20 74 68 65 0a 2a 2a 20  ve about the.** 
17da6 73 61 6d 65 20 61 6d 6f 75 6e 74 20 6f 66 20 66  same amount of f
17da7 72 65 65 20 73 70 61 63 65 2e 20 55 73 75 61 6c  ree space. Usual
17da8 6c 79 20 61 20 73 69 6e 67 6c 65 20 73 69 62 6c  ly a single sibl
17da9 69 6e 67 20 6f 6e 20 65 69 74 68 65 72 20 73 69  ing on either si
17daa 64 65 20 6f 66 20 74 68 65 0a 2a 2a 20 70 61 67  de of the.** pag
17dab 65 20 61 72 65 20 75 73 65 64 20 69 6e 20 74 68  e are used in th
17dac 65 20 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68 6f  e balancing, tho
17dad 75 67 68 20 62 6f 74 68 20 73 69 62 6c 69 6e 67  ugh both sibling
17dae 73 20 6d 69 67 68 74 20 63 6f 6d 65 20 66 72 6f  s might come fro
17daf 6d 20 6f 6e 65 0a 2a 2a 20 73 69 64 65 20 69 66  m one.** side if
17db0 20 74 68 65 20 70 61 67 65 20 69 73 20 74 68 65   the page is the
17db1 20 66 69 72 73 74 20 6f 72 20 6c 61 73 74 20 63   first or last c
17db2 68 69 6c 64 20 6f 66 20 69 74 73 20 70 61 72 65  hild of its pare
17db3 6e 74 2e 20 49 66 20 74 68 65 20 70 61 67 65 20  nt. If the page 
17db4 0a 2a 2a 20 68 61 73 20 66 65 77 65 72 20 74 68  .** has fewer th
17db5 61 6e 20 32 20 73 69 62 6c 69 6e 67 73 20 28 73  an 2 siblings (s
17db6 6f 6d 65 74 68 69 6e 67 20 77 68 69 63 68 20 63  omething which c
17db7 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69  an only happen i
17db8 66 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 69 73  f the page.** is
17db9 20 61 20 72 6f 6f 74 20 70 61 67 65 20 6f 72 20   a root page or 
17dba 61 20 63 68 69 6c 64 20 6f 66 20 61 20 72 6f 6f  a child of a roo
17dbb 74 20 70 61 67 65 29 20 74 68 65 6e 20 61 6c 6c  t page) then all
17dbc 20 61 76 61 69 6c 61 62 6c 65 20 73 69 62 6c 69   available sibli
17dbd 6e 67 73 0a 2a 2a 20 70 61 72 74 69 63 69 70 61  ngs.** participa
17dbe 74 65 20 69 6e 20 74 68 65 20 62 61 6c 61 6e 63  te in the balanc
17dbf 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e  ing..**.** The n
17dc0 75 6d 62 65 72 20 6f 66 20 73 69 62 6c 69 6e 67  umber of sibling
17dc1 73 20 6f 66 20 74 68 65 20 70 61 67 65 20 6d 69  s of the page mi
17dc2 67 68 74 20 62 65 20 69 6e 63 72 65 61 73 65 64  ght be increased
17dc3 20 6f 72 20 64 65 63 72 65 61 73 65 64 20 62 79   or decreased by
17dc4 20 0a 2a 2a 20 6f 6e 65 20 6f 72 20 74 77 6f 20   .** one or two 
17dc5 69 6e 20 61 6e 20 65 66 66 6f 72 74 20 74 6f 20  in an effort to 
17dc6 6b 65 65 70 20 70 61 67 65 73 20 6e 65 61 72 6c  keep pages nearl
17dc7 79 20 66 75 6c 6c 20 62 75 74 20 6e 6f 74 20 6f  y full but not o
17dc8 76 65 72 20 66 75 6c 6c 2e 20 0a 2a 2a 0a 2a 2a  ver full. .**.**
17dc9 20 4e 6f 74 65 20 74 68 61 74 20 77 68 65 6e 20   Note that when 
17dca 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
17dcb 63 61 6c 6c 65 64 2c 20 73 6f 6d 65 20 6f 66 20  called, some of 
17dcc 74 68 65 20 63 65 6c 6c 73 20 6f 6e 20 74 68 65  the cells on the
17dcd 20 70 61 67 65 0a 2a 2a 20 6d 69 67 68 74 20 6e   page.** might n
17dce 6f 74 20 61 63 74 75 61 6c 6c 79 20 62 65 20 73  ot actually be s
17dcf 74 6f 72 65 64 20 69 6e 20 4d 65 6d 50 61 67 65  tored in MemPage
17dd0 2e 61 44 61 74 61 5b 5d 2e 20 54 68 69 73 20 63  .aData[]. This c
17dd1 61 6e 20 68 61 70 70 65 6e 0a 2a 2a 20 69 66 20  an happen.** if 
17dd2 74 68 65 20 70 61 67 65 20 69 73 20 6f 76 65 72  the page is over
17dd3 66 75 6c 6c 2e 20 54 68 69 73 20 72 6f 75 74 69  full. This routi
17dd4 6e 65 20 65 6e 73 75 72 65 73 20 74 68 61 74 20  ne ensures that 
17dd5 61 6c 6c 20 63 65 6c 6c 73 20 61 6c 6c 6f 63 61  all cells alloca
17dd6 74 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 70 61  ted.** to the pa
17dd7 67 65 20 61 6e 64 20 69 74 73 20 73 69 62 6c 69  ge and its sibli
17dd8 6e 67 73 20 66 69 74 20 69 6e 74 6f 20 4d 65 6d  ngs fit into Mem
17dd9 50 61 67 65 2e 61 44 61 74 61 5b 5d 20 62 65 66  Page.aData[] bef
17dda 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
17ddb 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 63 6f 75 72  *.** In the cour
17ddc 73 65 20 6f 66 20 62 61 6c 61 6e 63 69 6e 67 20  se of balancing 
17ddd 74 68 65 20 70 61 67 65 20 61 6e 64 20 69 74 73  the page and its
17dde 20 73 69 62 6c 69 6e 67 73 2c 20 63 65 6c 6c 73   siblings, cells
17ddf 20 6d 61 79 20 62 65 0a 2a 2a 20 69 6e 73 65 72   may be.** inser
17de0 74 65 64 20 69 6e 74 6f 20 6f 72 20 72 65 6d 6f  ted into or remo
17de1 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70 61 72  ved from the par
17de2 65 6e 74 20 70 61 67 65 20 28 70 50 61 72 65 6e  ent page (pParen
17de3 74 29 2e 20 44 6f 69 6e 67 20 73 6f 0a 2a 2a 20  t). Doing so.** 
17de4 6d 61 79 20 63 61 75 73 65 20 74 68 65 20 70 61  may cause the pa
17de5 72 65 6e 74 20 70 61 67 65 20 74 6f 20 62 65 63  rent page to bec
17de6 6f 6d 65 20 6f 76 65 72 66 75 6c 6c 20 6f 72 20  ome overfull or 
17de7 75 6e 64 65 72 66 75 6c 6c 2e 20 49 66 20 74 68  underfull. If th
17de8 69 73 0a 2a 2a 20 68 61 70 70 65 6e 73 2c 20 69  is.** happens, i
17de9 74 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73  t is the respons
17dea 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20 63  ibility of the c
17deb 61 6c 6c 65 72 20 74 6f 20 69 6e 76 6f 6b 65 20  aller to invoke 
17dec 74 68 65 20 63 6f 72 72 65 63 74 0a 2a 2a 20 62  the correct.** b
17ded 61 6c 61 6e 63 69 6e 67 20 72 6f 75 74 69 6e 65  alancing routine
17dee 20 74 6f 20 66 69 78 20 74 68 69 73 20 70 72 6f   to fix this pro
17def 62 6c 65 6d 20 28 73 65 65 20 74 68 65 20 62 61  blem (see the ba
17df0 6c 61 6e 63 65 28 29 20 72 6f 75 74 69 6e 65 29  lance() routine)
17df1 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73  . .**.** If this
17df2 20 72 6f 75 74 69 6e 65 20 66 61 69 6c 73 20 66   routine fails f
17df3 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e 2c 20 69  or any reason, i
17df4 74 20 6d 69 67 68 74 20 6c 65 61 76 65 20 74 68  t might leave th
17df5 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 6e  e database.** in
17df6 20 61 20 63 6f 72 72 75 70 74 65 64 20 73 74 61   a corrupted sta
17df7 74 65 2e 20 53 6f 20 69 66 20 74 68 69 73 20 72  te. So if this r
17df8 6f 75 74 69 6e 65 20 66 61 69 6c 73 2c 20 74 68  outine fails, th
17df9 65 20 64 61 74 61 62 61 73 65 20 73 68 6f 75 6c  e database shoul
17dfa 64 0a 2a 2a 20 62 65 20 72 6f 6c 6c 65 64 20 62  d.** be rolled b
17dfb 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74  ack..**.** The t
17dfc 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  hird argument to
17dfd 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20   this function, 
17dfe 61 4f 76 66 6c 53 70 61 63 65 2c 20 69 73 20 61  aOvflSpace, is a
17dff 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 0a 2a 2a   pointer to a.**
17e00 20 62 75 66 66 65 72 20 62 69 67 20 65 6e 6f 75   buffer big enou
17e01 67 68 20 74 6f 20 68 6f 6c 64 20 6f 6e 65 20 70  gh to hold one p
17e02 61 67 65 2e 20 49 66 20 77 68 69 6c 65 20 69 6e  age. If while in
17e03 73 65 72 74 69 6e 67 20 63 65 6c 6c 73 20 69 6e  serting cells in
17e04 74 6f 20 74 68 65 20 70 61 72 65 6e 74 0a 2a 2a  to the parent.**
17e05 20 70 61 67 65 20 28 70 50 61 72 65 6e 74 29 20   page (pParent) 
17e06 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20  the parent page 
17e07 62 65 63 6f 6d 65 73 20 6f 76 65 72 66 75 6c 6c  becomes overfull
17e08 2c 20 74 68 69 73 20 62 75 66 66 65 72 20 69 73  , this buffer is
17e09 0a 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72  .** used to stor
17e0a 65 20 74 68 65 20 70 61 72 65 6e 74 27 73 20 6f  e the parent's o
17e0b 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 2e 20 42  verflow cells. B
17e0c 65 63 61 75 73 65 20 74 68 69 73 20 66 75 6e 63  ecause this func
17e0d 74 69 6f 6e 20 69 6e 73 65 72 74 73 0a 2a 2a 20  tion inserts.** 
17e0e 61 20 6d 61 78 69 6d 75 6d 20 6f 66 20 66 6f 75  a maximum of fou
17e0f 72 20 64 69 76 69 64 65 72 20 63 65 6c 6c 73 20  r divider cells 
17e10 69 6e 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20  into the parent 
17e11 70 61 67 65 2c 20 61 6e 64 20 74 68 65 20 6d 61  page, and the ma
17e12 78 69 6d 75 6d 0a 2a 2a 20 73 69 7a 65 20 6f 66  ximum.** size of
17e13 20 61 20 63 65 6c 6c 20 73 74 6f 72 65 64 20 77   a cell stored w
17e14 69 74 68 69 6e 20 61 6e 20 69 6e 74 65 72 6e 61  ithin an interna
17e15 6c 20 6e 6f 64 65 20 69 73 20 61 6c 77 61 79 73  l node is always
17e16 20 6c 65 73 73 20 74 68 61 6e 20 31 2f 34 0a 2a   less than 1/4.*
17e17 2a 20 6f 66 20 74 68 65 20 70 61 67 65 2d 73 69  * of the page-si
17e18 7a 65 2c 20 74 68 65 20 61 4f 76 66 6c 53 70 61  ze, the aOvflSpa
17e19 63 65 5b 5d 20 62 75 66 66 65 72 20 69 73 20 67  ce[] buffer is g
17e1a 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65 20  uaranteed to be 
17e1b 6c 61 72 67 65 0a 2a 2a 20 65 6e 6f 75 67 68 20  large.** enough 
17e1c 66 6f 72 20 61 6c 6c 20 6f 76 65 72 66 6c 6f 77  for all overflow
17e1d 20 63 65 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 49 66   cells..**.** If
17e1e 20 61 4f 76 66 6c 53 70 61 63 65 20 69 73 20 73   aOvflSpace is s
17e1f 65 74 20 74 6f 20 61 20 6e 75 6c 6c 20 70 6f 69  et to a null poi
17e20 6e 74 65 72 2c 20 74 68 69 73 20 66 75 6e 63 74  nter, this funct
17e21 69 6f 6e 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20  ion returns .** 
17e22 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f  SQLITE_NOMEM..*/
17e23 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61  .static int bala
17e24 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 0a 20 20 4d  nce_nonroot(.  M
17e25 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 2c  emPage *pParent,
17e26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
17e27 2a 20 50 61 72 65 6e 74 20 70 61 67 65 20 6f 66  * Parent page of
17e28 20 73 69 62 6c 69 6e 67 73 20 62 65 69 6e 67 20   siblings being 
17e29 62 61 6c 61 6e 63 65 64 20 2a 2f 0a 20 20 69 6e  balanced */.  in
17e2a 74 20 69 50 61 72 65 6e 74 49 64 78 2c 20 20 20  t iParentIdx,   
17e2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17e2c 20 49 6e 64 65 78 20 6f 66 20 22 74 68 65 20 70   Index of "the p
17e2d 61 67 65 22 20 69 6e 20 70 50 61 72 65 6e 74 20  age" in pParent 
17e2e 2a 2f 0a 20 20 75 38 20 2a 61 4f 76 66 6c 53 70  */.  u8 *aOvflSp
17e2f 61 63 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ace,            
17e30 20 20 20 20 20 2f 2a 20 70 61 67 65 2d 73 69 7a       /* page-siz
17e31 65 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65  e bytes of space
17e32 20 66 6f 72 20 70 61 72 65 6e 74 20 6f 76 66 6c   for parent ovfl
17e33 20 2a 2f 0a 20 20 69 6e 74 20 69 73 52 6f 6f 74   */.  int isRoot
17e34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17e35 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
17e36 20 70 50 61 72 65 6e 74 20 69 73 20 61 20 72 6f   pParent is a ro
17e37 6f 74 2d 70 61 67 65 20 2a 2f 0a 29 7b 0a 20 20  ot-page */.){.  
17e38 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20  BtShared *pBt;  
17e39 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17e3a 54 68 65 20 77 68 6f 6c 65 20 64 61 74 61 62 61  The whole databa
17e3b 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 65 6c  se */.  int nCel
17e3c 6c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  l = 0;          
17e3d 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
17e3e 66 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c  f cells in apCel
17e3f 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61  l[] */.  int nMa
17e40 78 43 65 6c 6c 73 20 3d 20 30 3b 20 20 20 20 20  xCells = 0;     
17e41 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74        /* Allocat
17e42 65 64 20 73 69 7a 65 20 6f 66 20 61 70 43 65 6c  ed size of apCel
17e43 6c 2c 20 73 7a 43 65 6c 6c 2c 20 61 46 72 6f 6d  l, szCell, aFrom
17e44 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 65 77 20  . */.  int nNew 
17e45 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
17e46 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
17e47 20 70 61 67 65 73 20 69 6e 20 61 70 4e 65 77 5b   pages in apNew[
17e48 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b  ] */.  int nOld;
17e49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17e4a 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
17e4b 20 70 61 67 65 73 20 69 6e 20 61 70 4f 6c 64 5b   pages in apOld[
17e4c 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 2c  ] */.  int i, j,
17e4d 20 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   k;             
17e4e 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
17e4f 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 78  ters */.  int nx
17e50 44 69 76 3b 20 20 20 20 20 20 20 20 20 20 20 20  Div;            
17e51 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 64         /* Next d
17e52 69 76 69 64 65 72 20 73 6c 6f 74 20 69 6e 20 70  ivider slot in p
17e53 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c 5b 5d 20  Parent->aCell[] 
17e54 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  */.  int rc = SQ
17e55 4c 49 54 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20  LITE_OK;        
17e56 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72 6e 20    /* The return 
17e57 63 6f 64 65 20 2a 2f 0a 20 20 75 31 36 20 6c 65  code */.  u16 le
17e58 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 20 20 20  afCorrection;   
17e59 20 20 20 20 20 20 20 2f 2a 20 34 20 69 66 20 70         /* 4 if p
17e5a 50 61 67 65 20 69 73 20 61 20 6c 65 61 66 2e 20  Page is a leaf. 
17e5b 20 30 20 69 66 20 6e 6f 74 20 2a 2f 0a 20 20 69   0 if not */.  i
17e5c 6e 74 20 6c 65 61 66 44 61 74 61 3b 20 20 20 20  nt leafData;    
17e5d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
17e5e 72 75 65 20 69 66 20 70 50 61 67 65 20 69 73 20  rue if pPage is 
17e5f 61 20 6c 65 61 66 20 6f 66 20 61 20 4c 45 41 46  a leaf of a LEAF
17e60 44 41 54 41 20 74 72 65 65 20 2a 2f 0a 20 20 69  DATA tree */.  i
17e61 6e 74 20 75 73 61 62 6c 65 53 70 61 63 65 3b 20  nt usableSpace; 
17e62 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
17e63 79 74 65 73 20 69 6e 20 70 50 61 67 65 20 62 65  ytes in pPage be
17e64 79 6f 6e 64 20 74 68 65 20 68 65 61 64 65 72 20  yond the header 
17e65 2a 2f 0a 20 20 69 6e 74 20 70 61 67 65 46 6c 61  */.  int pageFla
17e66 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  gs;             
17e67 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 70 50    /* Value of pP
17e68 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 20 2a 2f  age->aData[0] */
17e69 0a 20 20 69 6e 74 20 73 75 62 74 6f 74 61 6c 3b  .  int subtotal;
17e6a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17e6b 2f 2a 20 53 75 62 74 6f 74 61 6c 20 6f 66 20 62  /* Subtotal of b
17e6c 79 74 65 73 20 69 6e 20 63 65 6c 6c 73 20 6f 6e  ytes in cells on
17e6d 20 6f 6e 65 20 70 61 67 65 20 2a 2f 0a 20 20 69   one page */.  i
17e6e 6e 74 20 69 53 70 61 63 65 31 20 3d 20 30 3b 20  nt iSpace1 = 0; 
17e6f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
17e70 69 72 73 74 20 75 6e 75 73 65 64 20 62 79 74 65  irst unused byte
17e71 20 6f 66 20 61 53 70 61 63 65 31 5b 5d 20 2a 2f   of aSpace1[] */
17e72 0a 20 20 69 6e 74 20 69 4f 76 66 6c 53 70 61 63  .  int iOvflSpac
17e73 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  e = 0;          
17e74 2f 2a 20 46 69 72 73 74 20 75 6e 75 73 65 64 20  /* First unused 
17e75 62 79 74 65 20 6f 66 20 61 4f 76 66 6c 53 70 61  byte of aOvflSpa
17e76 63 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  ce[] */.  int sz
17e77 53 63 72 61 74 63 68 3b 20 20 20 20 20 20 20 20  Scratch;        
17e78 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
17e79 66 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79  f scratch memory
17e7a 20 72 65 71 75 65 73 74 65 64 20 2a 2f 0a 20 20   requested */.  
17e7b 4d 65 6d 50 61 67 65 20 2a 61 70 4f 6c 64 5b 4e  MemPage *apOld[N
17e7c 42 5d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  B];          /* 
17e7d 70 50 61 67 65 20 61 6e 64 20 75 70 20 74 6f 20  pPage and up to 
17e7e 74 77 6f 20 73 69 62 6c 69 6e 67 73 20 2a 2f 0a  two siblings */.
17e7f 20 20 4d 65 6d 50 61 67 65 20 2a 61 70 43 6f 70    MemPage *apCop
17e80 79 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 2f  y[NB];         /
17e81 2a 20 50 72 69 76 61 74 65 20 63 6f 70 69 65 73  * Private copies
17e82 20 6f 66 20 61 70 4f 6c 64 5b 5d 20 70 61 67 65   of apOld[] page
17e83 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a  s */.  MemPage *
17e84 61 70 4e 65 77 5b 4e 42 2b 32 5d 3b 20 20 20 20  apNew[NB+2];    
17e85 20 20 20 20 2f 2a 20 70 50 61 67 65 20 61 6e 64      /* pPage and
17e86 20 75 70 20 74 6f 20 4e 42 20 73 69 62 6c 69 6e   up to NB siblin
17e87 67 73 20 61 66 74 65 72 20 62 61 6c 61 6e 63 69  gs after balanci
17e88 6e 67 20 2a 2f 0a 20 20 75 38 20 2a 70 52 69 67  ng */.  u8 *pRig
17e89 68 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ht;             
17e8a 20 20 20 20 20 2f 2a 20 4c 6f 63 61 74 69 6f 6e       /* Location
17e8b 20 69 6e 20 70 61 72 65 6e 74 20 6f 66 20 72 69   in parent of ri
17e8c 67 68 74 2d 73 69 62 6c 69 6e 67 20 70 6f 69 6e  ght-sibling poin
17e8d 74 65 72 20 2a 2f 0a 20 20 75 38 20 2a 61 70 44  ter */.  u8 *apD
17e8e 69 76 5b 4e 42 2d 31 5d 3b 20 20 20 20 20 20 20  iv[NB-1];       
17e8f 20 20 20 20 20 20 2f 2a 20 44 69 76 69 64 65 72        /* Divider
17e90 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e   cells in pParen
17e91 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 4e 65  t */.  int cntNe
17e92 77 5b 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20  w[NB+2];        
17e93 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20      /* Index in 
17e94 61 43 65 6c 6c 5b 5d 20 6f 66 20 63 65 6c 6c 20  aCell[] of cell 
17e95 61 66 74 65 72 20 69 2d 74 68 20 70 61 67 65 20  after i-th page 
17e96 2a 2f 0a 20 20 69 6e 74 20 73 7a 4e 65 77 5b 4e  */.  int szNew[N
17e97 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20  B+2];           
17e98 20 20 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 73 69    /* Combined si
17e99 7a 65 20 6f 66 20 63 65 6c 6c 73 20 70 6c 61 63  ze of cells plac
17e9a 65 20 6f 6e 20 69 2d 74 68 20 70 61 67 65 20 2a  e on i-th page *
17e9b 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 20  /.  u8 **apCell 
17e9c 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
17e9d 20 2f 2a 20 41 6c 6c 20 63 65 6c 6c 73 20 62 65   /* All cells be
17e9e 67 69 6e 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a  gin balanced */.
17e9f 20 20 75 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 20    u16 *szCell;  
17ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
17ea1 2a 20 4c 6f 63 61 6c 20 73 69 7a 65 20 6f 66 20  * Local size of 
17ea2 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43  all cells in apC
17ea3 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61  ell[] */.  u8 *a
17ea4 53 70 61 63 65 31 3b 20 20 20 20 20 20 20 20 20  Space1;         
17ea5 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65          /* Space
17ea6 20 66 6f 72 20 63 6f 70 69 65 73 20 6f 66 20 64   for copies of d
17ea7 69 76 69 64 65 72 73 20 63 65 6c 6c 73 20 2a 2f  ividers cells */
17ea8 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 20 20 20  .  Pgno pgno;   
17ea9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17eaa 2f 2a 20 54 65 6d 70 20 76 61 72 20 74 6f 20 73  /* Temp var to s
17eab 74 6f 72 65 20 61 20 70 61 67 65 20 6e 75 6d 62  tore a page numb
17eac 65 72 20 69 6e 20 2a 2f 0a 0a 20 20 70 42 74 20  er in */..  pBt 
17ead 3d 20 70 50 61 72 65 6e 74 2d 3e 70 42 74 3b 0a  = pParent->pBt;.
17eae 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
17eaf 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
17eb0 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
17eb1 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
17eb2 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50  erIswriteable(pP
17eb3 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 20  arent->pDbPage) 
17eb4 29 3b 0a 0a 23 69 66 20 30 0a 20 20 54 52 41 43  );..#if 0.  TRAC
17eb5 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 62 65 67  E(("BALANCE: beg
17eb6 69 6e 20 70 61 67 65 20 25 64 20 63 68 69 6c 64  in page %d child
17eb7 20 6f 66 20 25 64 5c 6e 22 2c 20 70 50 61 67 65   of %d\n", pPage
17eb8 2d 3e 70 67 6e 6f 2c 20 70 50 61 72 65 6e 74 2d  ->pgno, pParent-
17eb9 3e 70 67 6e 6f 29 29 3b 0a 23 65 6e 64 69 66 0a  >pgno));.#endif.
17eba 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f  .  /* At this po
17ebb 69 6e 74 20 70 50 61 72 65 6e 74 20 6d 61 79 20  int pParent may 
17ebc 68 61 76 65 20 61 74 20 6d 6f 73 74 20 6f 6e 65  have at most one
17ebd 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 2e 20   overflow cell. 
17ebe 41 6e 64 20 69 66 0a 20 20 2a 2a 20 74 68 69 73  And if.  ** this
17ebf 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 69   overflow cell i
17ec0 73 20 70 72 65 73 65 6e 74 2c 20 69 74 20 6d 75  s present, it mu
17ec1 73 74 20 62 65 20 74 68 65 20 63 65 6c 6c 20 77  st be the cell w
17ec2 69 74 68 20 0a 20 20 2a 2a 20 69 6e 64 65 78 20  ith .  ** index 
17ec3 69 50 61 72 65 6e 74 49 64 78 2e 20 54 68 69 73  iParentIdx. This
17ec4 20 73 63 65 6e 61 72 69 6f 20 63 6f 6d 65 73 20   scenario comes 
17ec5 61 62 6f 75 74 20 77 68 65 6e 20 74 68 69 73 20  about when this 
17ec6 66 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73  function.  ** is
17ec7 20 63 61 6c 6c 65 64 20 28 69 6e 64 69 72 65 63   called (indirec
17ec8 74 6c 79 29 20 66 72 6f 6d 20 73 71 6c 69 74 65  tly) from sqlite
17ec9 33 42 74 72 65 65 44 65 6c 65 74 65 28 29 2e 0a  3BtreeDelete()..
17eca 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
17ecb 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f  Parent->nOverflo
17ecc 77 3d 3d 30 20 7c 7c 20 70 50 61 72 65 6e 74 2d  w==0 || pParent-
17ecd 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20 29 3b  >nOverflow==1 );
17ece 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65  .  assert( pPare
17ecf 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30  nt->nOverflow==0
17ed0 20 7c 7c 20 70 50 61 72 65 6e 74 2d 3e 61 4f 76   || pParent->aOv
17ed1 66 6c 5b 30 5d 2e 69 64 78 3d 3d 69 50 61 72 65  fl[0].idx==iPare
17ed2 6e 74 49 64 78 20 29 3b 0a 0a 20 20 69 66 28 20  ntIdx );..  if( 
17ed3 21 61 4f 76 66 6c 53 70 61 63 65 20 29 7b 0a 20  !aOvflSpace ){. 
17ed4 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
17ed5 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f  _NOMEM;.  }..  /
17ed6 2a 20 46 69 6e 64 20 74 68 65 20 73 69 62 6c 69  * Find the sibli
17ed7 6e 67 20 70 61 67 65 73 20 74 6f 20 62 61 6c 61  ng pages to bala
17ed8 6e 63 65 2e 20 41 6c 73 6f 20 6c 6f 63 61 74 65  nce. Also locate
17ed9 20 74 68 65 20 63 65 6c 6c 73 20 69 6e 20 70 50   the cells in pP
17eda 61 72 65 6e 74 20 0a 20 20 2a 2a 20 74 68 61 74  arent .  ** that
17edb 20 64 69 76 69 64 65 20 74 68 65 20 73 69 62 6c   divide the sibl
17edc 69 6e 67 73 2e 20 41 6e 20 61 74 74 65 6d 70 74  ings. An attempt
17edd 20 69 73 20 6d 61 64 65 20 74 6f 20 66 69 6e 64   is made to find
17ede 20 4e 4e 20 73 69 62 6c 69 6e 67 73 20 6f 6e 20   NN siblings on 
17edf 0a 20 20 2a 2a 20 65 69 74 68 65 72 20 73 69 64  .  ** either sid
17ee0 65 20 6f 66 20 70 50 61 67 65 2e 20 4d 6f 72 65  e of pPage. More
17ee1 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 74 61   siblings are ta
17ee2 6b 65 6e 20 66 72 6f 6d 20 6f 6e 65 20 73 69 64  ken from one sid
17ee3 65 2c 20 68 6f 77 65 76 65 72 2c 20 0a 20 20 2a  e, however, .  *
17ee4 2a 20 69 66 20 74 68 65 72 65 20 61 72 65 20 66  * if there are f
17ee5 65 77 65 72 20 74 68 61 6e 20 4e 4e 20 73 69 62  ewer than NN sib
17ee6 6c 69 6e 67 73 20 6f 6e 20 74 68 65 20 6f 74 68  lings on the oth
17ee7 65 72 20 73 69 64 65 2e 20 49 66 20 70 50 61 72  er side. If pPar
17ee8 65 6e 74 0a 20 20 2a 2a 20 68 61 73 20 4e 42 20  ent.  ** has NB 
17ee9 6f 72 20 66 65 77 65 72 20 63 68 69 6c 64 72 65  or fewer childre
17eea 6e 20 74 68 65 6e 20 61 6c 6c 20 63 68 69 6c 64  n then all child
17eeb 72 65 6e 20 6f 66 20 70 50 61 72 65 6e 74 20 61  ren of pParent a
17eec 72 65 20 74 61 6b 65 6e 2e 20 20 0a 20 20 2a 2a  re taken.  .  **
17eed 0a 20 20 2a 2a 20 54 68 69 73 20 6c 6f 6f 70 20  .  ** This loop 
17eee 61 6c 73 6f 20 64 72 6f 70 73 20 74 68 65 20 64  also drops the d
17eef 69 76 69 64 65 72 20 63 65 6c 6c 73 20 66 72 6f  ivider cells fro
17ef0 6d 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67  m the parent pag
17ef1 65 2e 20 54 68 69 73 0a 20 20 2a 2a 20 77 61 79  e. This.  ** way
17ef2 2c 20 74 68 65 20 72 65 6d 61 69 6e 64 65 72 20  , the remainder 
17ef3 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  of the function 
17ef4 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 74 6f  does not have to
17ef5 20 64 65 61 6c 20 77 69 74 68 20 61 6e 79 0a 20   deal with any. 
17ef6 20 2a 2a 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c   ** overflow cel
17ef7 6c 73 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74  ls in the parent
17ef8 20 70 61 67 65 2c 20 73 69 6e 63 65 20 69 66 20   page, since if 
17ef9 61 6e 79 20 65 78 69 73 74 65 64 20 74 68 65 79  any existed they
17efa 20 77 69 6c 6c 0a 20 20 2a 2a 20 68 61 76 65 20   will.  ** have 
17efb 61 6c 72 65 61 64 79 20 62 65 65 6e 20 72 65 6d  already been rem
17efc 6f 76 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 20 3d  oved..  */.  i =
17efd 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66   pParent->nOverf
17efe 6c 6f 77 20 2b 20 70 50 61 72 65 6e 74 2d 3e 6e  low + pParent->n
17eff 43 65 6c 6c 3b 0a 20 20 69 66 28 20 69 3c 32 20  Cell;.  if( i<2 
17f00 29 7b 0a 20 20 20 20 6e 78 44 69 76 20 3d 20 30  ){.    nxDiv = 0
17f01 3b 0a 20 20 20 20 6e 4f 6c 64 20 3d 20 69 2b 31  ;.    nOld = i+1
17f02 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e  ;.  }else{.    n
17f03 4f 6c 64 20 3d 20 33 3b 0a 20 20 20 20 69 66 28  Old = 3;.    if(
17f04 20 69 50 61 72 65 6e 74 49 64 78 3d 3d 30 20 29   iParentIdx==0 )
17f05 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
17f06 20 20 0a 20 20 20 20 20 20 6e 78 44 69 76 20 3d    .      nxDiv =
17f07 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   0;.    }else if
17f08 28 20 69 50 61 72 65 6e 74 49 64 78 3d 3d 69 20  ( iParentIdx==i 
17f09 29 7b 0a 20 20 20 20 20 20 6e 78 44 69 76 20 3d  ){.      nxDiv =
17f0a 20 69 2d 32 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   i-2;.    }else{
17f0b 0a 20 20 20 20 20 20 6e 78 44 69 76 20 3d 20 69  .      nxDiv = i
17f0c 50 61 72 65 6e 74 49 64 78 2d 31 3b 0a 20 20 20  ParentIdx-1;.   
17f0d 20 7d 0a 20 20 20 20 69 20 3d 20 32 3b 0a 20 20   }.    i = 2;.  
17f0e 7d 0a 20 20 69 66 28 20 28 69 2b 6e 78 44 69 76  }.  if( (i+nxDiv
17f0f 2d 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66  -pParent->nOverf
17f10 6c 6f 77 29 3d 3d 70 50 61 72 65 6e 74 2d 3e 6e  low)==pParent->n
17f11 43 65 6c 6c 20 29 7b 0a 20 20 20 20 70 52 69 67  Cell ){.    pRig
17f12 68 74 20 3d 20 26 70 50 61 72 65 6e 74 2d 3e 61  ht = &pParent->a
17f13 44 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64  Data[pParent->hd
17f14 72 4f 66 66 73 65 74 2b 38 5d 3b 0a 20 20 7d 65  rOffset+8];.  }e
17f15 6c 73 65 7b 0a 20 20 20 20 70 52 69 67 68 74 20  lse{.    pRight 
17f16 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 65  = findCell(pPare
17f17 6e 74 2c 20 69 2b 6e 78 44 69 76 2d 70 50 61 72  nt, i+nxDiv-pPar
17f18 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 29 3b  ent->nOverflow);
17f19 0a 20 20 7d 0a 20 20 70 67 6e 6f 20 3d 20 67 65  .  }.  pgno = ge
17f1a 74 34 62 79 74 65 28 70 52 69 67 68 74 29 3b 0a  t4byte(pRight);.
17f1b 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20    while( 1 ){.  
17f1c 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69    rc = getAndIni
17f1d 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c  tPage(pBt, pgno,
17f1e 20 26 61 70 4f 6c 64 5b 69 5d 29 3b 0a 20 20 20   &apOld[i]);.   
17f1f 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
17f20 20 6d 65 6d 73 65 74 28 61 70 4f 6c 64 2c 20 30   memset(apOld, 0
17f21 2c 20 28 69 2b 31 29 2a 73 69 7a 65 6f 66 28 4d  , (i+1)*sizeof(M
17f22 65 6d 50 61 67 65 2a 29 29 3b 0a 20 20 20 20 20  emPage*));.     
17f23 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
17f24 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 20  eanup;.    }.   
17f25 20 6e 4d 61 78 43 65 6c 6c 73 20 2b 3d 20 31 2b   nMaxCells += 1+
17f26 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 43 65 6c 6c 2b  apOld[i]->nCell+
17f27 61 70 4f 6c 64 5b 69 5d 2d 3e 6e 4f 76 65 72 66  apOld[i]->nOverf
17f28 6c 6f 77 3b 0a 20 20 20 20 69 66 28 20 28 69 2d  low;.    if( (i-
17f29 2d 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 0a  -)==0 ) break;..
17f2a 20 20 20 20 69 66 28 20 69 2b 6e 78 44 69 76 3d      if( i+nxDiv=
17f2b 3d 70 50 61 72 65 6e 74 2d 3e 61 4f 76 66 6c 5b  =pParent->aOvfl[
17f2c 30 5d 2e 69 64 78 20 26 26 20 70 50 61 72 65 6e  0].idx && pParen
17f2d 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0a  t->nOverflow ){.
17f2e 20 20 20 20 20 20 61 70 44 69 76 5b 69 5d 20 3d        apDiv[i] =
17f2f 20 70 50 61 72 65 6e 74 2d 3e 61 4f 76 66 6c 5b   pParent->aOvfl[
17f30 30 5d 2e 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20  0].pCell;.      
17f31 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28  pgno = get4byte(
17f32 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20 20  apDiv[i]);.     
17f33 20 73 7a 4e 65 77 5b 69 5d 20 3d 20 63 65 6c 6c   szNew[i] = cell
17f34 53 69 7a 65 50 74 72 28 70 50 61 72 65 6e 74 2c  SizePtr(pParent,
17f35 20 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20   apDiv[i]);.    
17f36 20 20 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72    pParent->nOver
17f37 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20 7d 65  flow = 0;.    }e
17f38 6c 73 65 7b 0a 20 20 20 20 20 20 61 70 44 69 76  lse{.      apDiv
17f39 5b 69 5d 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70  [i] = findCell(p
17f3a 50 61 72 65 6e 74 2c 20 69 2b 6e 78 44 69 76 2d  Parent, i+nxDiv-
17f3b 70 50 61 72 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c  pParent->nOverfl
17f3c 6f 77 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 20  ow);.      pgno 
17f3d 3d 20 67 65 74 34 62 79 74 65 28 61 70 44 69 76  = get4byte(apDiv
17f3e 5b 69 5d 29 3b 0a 20 20 20 20 20 20 73 7a 4e 65  [i]);.      szNe
17f3f 77 5b 69 5d 20 3d 20 63 65 6c 6c 53 69 7a 65 50  w[i] = cellSizeP
17f40 74 72 28 70 50 61 72 65 6e 74 2c 20 61 70 44 69  tr(pParent, apDi
17f41 76 5b 69 5d 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  v[i]);..      /*
17f42 20 44 72 6f 70 20 74 68 65 20 63 65 6c 6c 20 66   Drop the cell f
17f43 72 6f 6d 20 74 68 65 20 70 61 72 65 6e 74 20 70  rom the parent p
17f44 61 67 65 2e 20 61 70 44 69 76 5b 69 5d 20 73 74  age. apDiv[i] st
17f45 69 6c 6c 20 70 6f 69 6e 74 73 20 74 6f 0a 20 20  ill points to.  
17f46 20 20 20 20 2a 2a 20 74 68 65 20 63 65 6c 6c 20      ** the cell 
17f47 77 69 74 68 69 6e 20 74 68 65 20 70 61 72 65 6e  within the paren
17f48 74 2c 20 65 76 65 6e 20 74 68 6f 75 67 68 20 69  t, even though i
17f49 74 20 68 61 73 20 62 65 65 6e 20 64 72 6f 70 70  t has been dropp
17f4a 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 69  ed..      ** Thi
17f4b 73 20 69 73 20 73 61 66 65 20 62 65 63 61 75 73  s is safe becaus
17f4c 65 20 64 72 6f 70 70 69 6e 67 20 61 20 63 65 6c  e dropping a cel
17f4d 6c 20 6f 6e 6c 79 20 6f 76 65 72 77 72 69 74 65  l only overwrite
17f4e 73 20 74 68 65 20 66 69 72 73 74 0a 20 20 20 20  s the first.    
17f4f 20 20 2a 2a 20 66 6f 75 72 20 62 79 74 65 73 20    ** four bytes 
17f50 6f 66 20 69 74 2c 20 61 6e 64 20 74 68 69 73 20  of it, and this 
17f51 66 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 6e 6f  function does no
17f52 74 20 6e 65 65 64 20 74 68 65 20 66 69 72 73 74  t need the first
17f53 0a 20 20 20 20 20 20 2a 2a 20 66 6f 75 72 20 62  .      ** four b
17f54 79 74 65 73 20 6f 66 20 74 68 65 20 64 69 76 69  ytes of the divi
17f55 64 65 72 20 63 65 6c 6c 2e 20 53 6f 20 74 68 65  der cell. So the
17f56 20 70 6f 69 6e 74 65 72 20 69 73 20 73 61 66 65   pointer is safe
17f57 20 74 6f 20 75 73 65 0a 20 20 20 20 20 20 2a 2a   to use.      **
17f58 20 6c 61 74 65 72 20 6f 6e 2e 20 20 0a 20 20 20   later on.  .   
17f59 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 55     **.      ** U
17f5a 6e 6c 65 73 73 20 53 51 4c 69 74 65 20 69 73 20  nless SQLite is 
17f5b 63 6f 6d 70 69 6c 65 64 20 69 6e 20 73 65 63 75  compiled in secu
17f5c 72 65 2d 64 65 6c 65 74 65 20 6d 6f 64 65 2e 20  re-delete mode. 
17f5d 49 6e 20 74 68 69 73 20 63 61 73 65 2c 0a 20 20  In this case,.  
17f5e 20 20 20 20 2a 2a 20 74 68 65 20 64 72 6f 70 43      ** the dropC
17f5f 65 6c 6c 28 29 20 72 6f 75 74 69 6e 65 20 77 69  ell() routine wi
17f60 6c 6c 20 6f 76 65 72 77 72 69 74 65 20 74 68 65  ll overwrite the
17f61 20 65 6e 74 69 72 65 20 63 65 6c 6c 20 77 69 74   entire cell wit
17f62 68 20 7a 65 72 6f 65 73 2e 0a 20 20 20 20 20 20  h zeroes..      
17f63 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c  ** In this case,
17f64 20 74 65 6d 70 6f 72 61 72 69 6c 79 20 63 6f 70   temporarily cop
17f65 79 20 74 68 65 20 63 65 6c 6c 20 69 6e 74 6f 20  y the cell into 
17f66 74 68 65 20 61 4f 76 66 6c 53 70 61 63 65 5b 5d  the aOvflSpace[]
17f67 0a 20 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72  .      ** buffer
17f68 2e 20 49 74 20 77 69 6c 6c 20 62 65 20 63 6f 70  . It will be cop
17f69 69 65 64 20 6f 75 74 20 61 67 61 69 6e 20 61 73  ied out again as
17f6a 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 61 53 70   soon as the aSp
17f6b 61 63 65 5b 5d 20 62 75 66 66 65 72 0a 20 20 20  ace[] buffer.   
17f6c 20 20 20 2a 2a 20 69 73 20 61 6c 6c 6f 63 61 74     ** is allocat
17f6d 65 64 2e 20 20 2a 2f 0a 23 69 66 64 65 66 20 53  ed.  */.#ifdef S
17f6e 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45 4c  QLITE_SECURE_DEL
17f6f 45 54 45 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  ETE.      memcpy
17f70 28 26 61 4f 76 66 6c 53 70 61 63 65 5b 61 70 44  (&aOvflSpace[apD
17f71 69 76 5b 69 5d 2d 70 50 61 72 65 6e 74 2d 3e 61  iv[i]-pParent->a
17f72 44 61 74 61 5d 2c 20 61 70 44 69 76 5b 69 5d 2c  Data], apDiv[i],
17f73 20 73 7a 4e 65 77 5b 69 5d 29 3b 0a 20 20 20 20   szNew[i]);.    
17f74 20 20 61 70 44 69 76 5b 69 5d 20 3d 20 26 61 4f    apDiv[i] = &aO
17f75 76 66 6c 53 70 61 63 65 5b 61 70 44 69 76 5b 69  vflSpace[apDiv[i
17f76 5d 2d 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61  ]-pParent->aData
17f77 5d 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  ];.#endif.      
17f78 64 72 6f 70 43 65 6c 6c 28 70 50 61 72 65 6e 74  dropCell(pParent
17f79 2c 20 69 2b 6e 78 44 69 76 2d 70 50 61 72 65 6e  , i+nxDiv-pParen
17f7a 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2c 20 73 7a  t->nOverflow, sz
17f7b 4e 65 77 5b 69 5d 2c 20 26 72 63 29 3b 0a 20 20  New[i], &rc);.  
17f7c 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61    }.  }..  /* Ma
17f7d 6b 65 20 6e 4d 61 78 43 65 6c 6c 73 20 61 20 6d  ke nMaxCells a m
17f7e 75 6c 74 69 70 6c 65 20 6f 66 20 34 20 69 6e 20  ultiple of 4 in 
17f7f 6f 72 64 65 72 20 74 6f 20 70 72 65 73 65 72 76  order to preserv
17f80 65 20 38 2d 62 79 74 65 0a 20 20 2a 2a 20 61 6c  e 8-byte.  ** al
17f81 69 67 6e 6d 65 6e 74 20 2a 2f 0a 20 20 6e 4d 61  ignment */.  nMa
17f82 78 43 65 6c 6c 73 20 3d 20 28 6e 4d 61 78 43 65  xCells = (nMaxCe
17f83 6c 6c 73 20 2b 20 33 29 26 7e 33 3b 0a 0a 20 20  lls + 3)&~3;..  
17f84 2f 2a 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74 65  /*.  ** Allocate
17f85 20 73 70 61 63 65 20 66 6f 72 20 6d 65 6d 6f 72   space for memor
17f86 79 20 73 74 72 75 63 74 75 72 65 73 0a 20 20 2a  y structures.  *
17f87 2f 0a 20 20 6b 20 3d 20 70 42 74 2d 3e 70 61 67  /.  k = pBt->pag
17f88 65 53 69 7a 65 20 2b 20 52 4f 55 4e 44 38 28 73  eSize + ROUND8(s
17f89 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65 29 29 3b  izeof(MemPage));
17f8a 0a 20 20 73 7a 53 63 72 61 74 63 68 20 3d 0a 20  .  szScratch =. 
17f8b 20 20 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 2a        nMaxCells*
17f8c 73 69 7a 65 6f 66 28 75 38 2a 29 20 20 20 20 20  sizeof(u8*)     
17f8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17f8e 20 20 2f 2a 20 61 70 43 65 6c 6c 20 2a 2f 0a 20    /* apCell */. 
17f8f 20 20 20 20 2b 20 6e 4d 61 78 43 65 6c 6c 73 2a      + nMaxCells*
17f90 73 69 7a 65 6f 66 28 75 31 36 29 20 20 20 20 20  sizeof(u16)     
17f91 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17f92 20 20 2f 2a 20 73 7a 43 65 6c 6c 20 2a 2f 0a 20    /* szCell */. 
17f93 20 20 20 20 2b 20 70 42 74 2d 3e 70 61 67 65 53      + pBt->pageS
17f94 69 7a 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ize             
17f95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17f96 20 20 2f 2a 20 61 53 70 61 63 65 31 20 2a 2f 0a    /* aSpace1 */.
17f97 20 20 20 20 20 2b 20 6b 2a 6e 4f 6c 64 3b 20 20       + k*nOld;  
17f98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17f99 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17f9a 20 20 20 2f 2a 20 50 61 67 65 20 63 6f 70 69 65     /* Page copie
17f9b 73 20 28 61 70 43 6f 70 79 29 20 2a 2f 0a 20 20  s (apCopy) */.  
17f9c 61 70 43 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33  apCell = sqlite3
17f9d 53 63 72 61 74 63 68 4d 61 6c 6c 6f 63 28 20 73  ScratchMalloc( s
17f9e 7a 53 63 72 61 74 63 68 20 29 3b 20 0a 20 20 69  zScratch ); .  i
17f9f 66 28 20 61 70 43 65 6c 6c 3d 3d 30 20 29 7b 0a  f( apCell==0 ){.
17fa0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
17fa1 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20  NOMEM;.    goto 
17fa2 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b  balance_cleanup;
17fa3 0a 20 20 7d 0a 20 20 73 7a 43 65 6c 6c 20 3d 20  .  }.  szCell = 
17fa4 28 75 31 36 2a 29 26 61 70 43 65 6c 6c 5b 6e 4d  (u16*)&apCell[nM
17fa5 61 78 43 65 6c 6c 73 5d 3b 0a 20 20 61 53 70 61  axCells];.  aSpa
17fa6 63 65 31 20 3d 20 28 75 38 2a 29 26 73 7a 43 65  ce1 = (u8*)&szCe
17fa7 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b 0a 20  ll[nMaxCells];. 
17fa8 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42   assert( EIGHT_B
17fa9 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 61 53  YTE_ALIGNMENT(aS
17faa 70 61 63 65 31 29 20 29 3b 0a 0a 20 20 2f 2a 0a  pace1) );..  /*.
17fab 20 20 2a 2a 20 4c 6f 61 64 20 70 6f 69 6e 74 65    ** Load pointe
17fac 72 73 20 74 6f 20 61 6c 6c 20 63 65 6c 6c 73 20  rs to all cells 
17fad 6f 6e 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73  on sibling pages
17fae 20 61 6e 64 20 74 68 65 20 64 69 76 69 64 65 72   and the divider
17faf 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74 6f   cells.  ** into
17fb0 20 74 68 65 20 6c 6f 63 61 6c 20 61 70 43 65 6c   the local apCel
17fb1 6c 5b 5d 20 61 72 72 61 79 2e 20 20 4d 61 6b 65  l[] array.  Make
17fb2 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65 20 64   copies of the d
17fb3 69 76 69 64 65 72 20 63 65 6c 6c 73 0a 20 20 2a  ivider cells.  *
17fb4 2a 20 69 6e 74 6f 20 73 70 61 63 65 20 6f 62 74  * into space obt
17fb5 61 69 6e 65 64 20 66 72 6f 6d 20 61 53 70 61 63  ained from aSpac
17fb6 65 31 5b 5d 20 61 6e 64 20 72 65 6d 6f 76 65 20  e1[] and remove 
17fb7 74 68 65 20 74 68 65 20 64 69 76 69 64 65 72 20  the the divider 
17fb8 43 65 6c 6c 73 0a 20 20 2a 2a 20 66 72 6f 6d 20  Cells.  ** from 
17fb9 70 50 61 72 65 6e 74 2e 0a 20 20 2a 2a 0a 20 20  pParent..  **.  
17fba 2a 2a 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e  ** If the siblin
17fbb 67 73 20 61 72 65 20 6f 6e 20 6c 65 61 66 20 70  gs are on leaf p
17fbc 61 67 65 73 2c 20 74 68 65 6e 20 74 68 65 20 63  ages, then the c
17fbd 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 20 6f 66  hild pointers of
17fbe 20 74 68 65 0a 20 20 2a 2a 20 64 69 76 69 64 65   the.  ** divide
17fbf 72 20 63 65 6c 6c 73 20 61 72 65 20 73 74 72 69  r cells are stri
17fc0 70 70 65 64 20 66 72 6f 6d 20 74 68 65 20 63 65  pped from the ce
17fc1 6c 6c 73 20 62 65 66 6f 72 65 20 74 68 65 79 20  lls before they 
17fc2 61 72 65 20 63 6f 70 69 65 64 0a 20 20 2a 2a 20  are copied.  ** 
17fc3 69 6e 74 6f 20 61 53 70 61 63 65 31 5b 5d 2e 20  into aSpace1[]. 
17fc4 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 61 6c   In this way, al
17fc5 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c  l cells in apCel
17fc6 6c 5b 5d 20 61 72 65 20 77 69 74 68 6f 75 74 0a  l[] are without.
17fc7 20 20 2a 2a 20 63 68 69 6c 64 20 70 6f 69 6e 74    ** child point
17fc8 65 72 73 2e 20 20 49 66 20 73 69 62 6c 69 6e 67  ers.  If sibling
17fc9 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 73  s are not leaves
17fca 2c 20 74 68 65 6e 20 61 6c 6c 20 63 65 6c 6c 20  , then all cell 
17fcb 69 6e 0a 20 20 2a 2a 20 61 70 43 65 6c 6c 5b 5d  in.  ** apCell[]
17fcc 20 69 6e 63 6c 75 64 65 20 63 68 69 6c 64 20 70   include child p
17fcd 6f 69 6e 74 65 72 73 2e 20 20 45 69 74 68 65 72  ointers.  Either
17fce 20 77 61 79 2c 20 61 6c 6c 20 63 65 6c 6c 73 20   way, all cells 
17fcf 69 6e 20 61 70 43 65 6c 6c 5b 5d 0a 20 20 2a 2a  in apCell[].  **
17fd0 20 61 72 65 20 61 6c 69 6b 65 2e 0a 20 20 2a 2a   are alike..  **
17fd1 0a 20 20 2a 2a 20 6c 65 61 66 43 6f 72 72 65 63  .  ** leafCorrec
17fd2 74 69 6f 6e 3a 20 20 34 20 69 66 20 70 50 61 67  tion:  4 if pPag
17fd3 65 20 69 73 20 61 20 6c 65 61 66 2e 20 20 30 20  e is a leaf.  0 
17fd4 69 66 20 70 50 61 67 65 20 69 73 20 6e 6f 74 20  if pPage is not 
17fd5 61 20 6c 65 61 66 2e 0a 20 20 2a 2a 20 20 20 20  a leaf..  **    
17fd6 20 20 20 6c 65 61 66 44 61 74 61 3a 20 20 31 20     leafData:  1 
17fd7 69 66 20 70 50 61 67 65 20 68 6f 6c 64 73 20 6b  if pPage holds k
17fd8 65 79 2b 64 61 74 61 20 61 6e 64 20 70 50 61 72  ey+data and pPar
17fd9 65 6e 74 20 68 6f 6c 64 73 20 6f 6e 6c 79 20 6b  ent holds only k
17fda 65 79 73 2e 0a 20 20 2a 2f 0a 20 20 6c 65 61 66  eys..  */.  leaf
17fdb 43 6f 72 72 65 63 74 69 6f 6e 20 3d 20 61 70 4f  Correction = apO
17fdc 6c 64 5b 30 5d 2d 3e 6c 65 61 66 2a 34 3b 0a 20  ld[0]->leaf*4;. 
17fdd 20 6c 65 61 66 44 61 74 61 20 3d 20 61 70 4f 6c   leafData = apOl
17fde 64 5b 30 5d 2d 3e 68 61 73 44 61 74 61 3b 0a 20  d[0]->hasData;. 
17fdf 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64   for(i=0; i<nOld
17fe0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20  ; i++){.    int 
17fe1 6c 69 6d 69 74 3b 0a 20 20 20 20 0a 20 20 20 20  limit;.    .    
17fe2 2f 2a 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20  /* Before doing 
17fe3 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 2c 20 74  anything else, t
17fe4 61 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68  ake a copy of th
17fe5 65 20 69 27 74 68 20 6f 72 69 67 69 6e 61 6c 20  e i'th original 
17fe6 73 69 62 6c 69 6e 67 0a 20 20 20 20 2a 2a 20 54  sibling.    ** T
17fe7 68 65 20 72 65 73 74 20 6f 66 20 74 68 69 73 20  he rest of this 
17fe8 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 75 73  function will us
17fe9 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20  e data from the 
17fea 63 6f 70 69 65 73 20 72 61 74 68 65 72 0a 20 20  copies rather.  
17feb 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 6f 72    ** that the or
17fec 69 67 69 6e 61 6c 20 70 61 67 65 73 20 73 69 6e  iginal pages sin
17fed 63 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  ce the original 
17fee 70 61 67 65 73 20 77 69 6c 6c 20 62 65 20 69 6e  pages will be in
17fef 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 72 6f 63   the.    ** proc
17ff0 65 73 73 20 6f 66 20 62 65 69 6e 67 20 6f 76 65  ess of being ove
17ff1 72 77 72 69 74 74 65 6e 2e 20 20 2a 2f 0a 20 20  rwritten.  */.  
17ff2 20 20 4d 65 6d 50 61 67 65 20 2a 70 4f 6c 64 20    MemPage *pOld 
17ff3 3d 20 61 70 43 6f 70 79 5b 69 5d 20 3d 20 28 4d  = apCopy[i] = (M
17ff4 65 6d 50 61 67 65 2a 29 26 61 53 70 61 63 65 31  emPage*)&aSpace1
17ff5 5b 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2b  [pBt->pageSize +
17ff6 20 6b 2a 69 5d 3b 0a 20 20 20 20 6d 65 6d 63 70   k*i];.    memcp
17ff7 79 28 70 4f 6c 64 2c 20 61 70 4f 6c 64 5b 69 5d  y(pOld, apOld[i]
17ff8 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67 65  , sizeof(MemPage
17ff9 29 29 3b 0a 20 20 20 20 70 4f 6c 64 2d 3e 61 44  ));.    pOld->aD
17ffa 61 74 61 20 3d 20 28 76 6f 69 64 2a 29 26 70 4f  ata = (void*)&pO
17ffb 6c 64 5b 31 5d 3b 0a 20 20 20 20 6d 65 6d 63 70  ld[1];.    memcp
17ffc 79 28 70 4f 6c 64 2d 3e 61 44 61 74 61 2c 20 61  y(pOld->aData, a
17ffd 70 4f 6c 64 5b 69 5d 2d 3e 61 44 61 74 61 2c 20  pOld[i]->aData, 
17ffe 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a  pBt->pageSize);.
17fff 0a 20 20 20 20 6c 69 6d 69 74 20 3d 20 70 4f 6c  .    limit = pOl
18000 64 2d 3e 6e 43 65 6c 6c 2b 70 4f 6c 64 2d 3e 6e  d->nCell+pOld->n
18001 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 66 6f  Overflow;.    fo
18002 72 28 6a 3d 30 3b 20 6a 3c 6c 69 6d 69 74 3b 20  r(j=0; j<limit; 
18003 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73 65  j++){.      asse
18004 72 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65  rt( nCell<nMaxCe
18005 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20 61 70 43  lls );.      apC
18006 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e  ell[nCell] = fin
18007 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 4f  dOverflowCell(pO
18008 6c 64 2c 20 6a 29 3b 0a 20 20 20 20 20 20 73 7a  ld, j);.      sz
18009 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65  Cell[nCell] = ce
1800a 6c 6c 53 69 7a 65 50 74 72 28 70 4f 6c 64 2c 20  llSizePtr(pOld, 
1800b 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0a  apCell[nCell]);.
1800c 20 20 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 20        nCell++;. 
1800d 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3c 6e     }.    if( i<n
1800e 4f 6c 64 2d 31 20 26 26 20 21 6c 65 61 66 44 61  Old-1 && !leafDa
1800f 74 61 29 7b 0a 20 20 20 20 20 20 75 31 36 20 73  ta){.      u16 s
18010 7a 20 3d 20 28 75 31 36 29 73 7a 4e 65 77 5b 69  z = (u16)szNew[i
18011 5d 3b 0a 20 20 20 20 20 20 75 38 20 2a 70 54 65  ];.      u8 *pTe
18012 6d 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  mp;.      assert
18013 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c  ( nCell<nMaxCell
18014 73 20 29 3b 0a 20 20 20 20 20 20 73 7a 43 65 6c  s );.      szCel
18015 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73 7a 3b 0a 20  l[nCell] = sz;. 
18016 20 20 20 20 20 70 54 65 6d 70 20 3d 20 26 61 53       pTemp = &aS
18017 70 61 63 65 31 5b 69 53 70 61 63 65 31 5d 3b 0a  pace1[iSpace1];.
18018 20 20 20 20 20 20 69 53 70 61 63 65 31 20 2b 3d        iSpace1 +=
18019 20 73 7a 3b 0a 20 20 20 20 20 20 61 73 73 65 72   sz;.      asser
1801a 74 28 20 73 7a 3c 3d 70 42 74 2d 3e 70 61 67 65  t( sz<=pBt->page
1801b 53 69 7a 65 2f 34 20 29 3b 0a 20 20 20 20 20 20  Size/4 );.      
1801c 61 73 73 65 72 74 28 20 69 53 70 61 63 65 31 3c  assert( iSpace1<
1801d 3d 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 29  =pBt->pageSize )
1801e 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70  ;.      memcpy(p
1801f 54 65 6d 70 2c 20 61 70 44 69 76 5b 69 5d 2c 20  Temp, apDiv[i], 
18020 73 7a 29 3b 0a 20 20 20 20 20 20 61 70 43 65 6c  sz);.      apCel
18021 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 70 54 65 6d 70  l[nCell] = pTemp
18022 2b 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b  +leafCorrection;
18023 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c  .      assert( l
18024 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 30  eafCorrection==0
18025 20 7c 7c 20 6c 65 61 66 43 6f 72 72 65 63 74 69   || leafCorrecti
18026 6f 6e 3d 3d 34 20 29 3b 0a 20 20 20 20 20 20 73  on==4 );.      s
18027 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73  zCell[nCell] = s
18028 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 2d 20 6c  zCell[nCell] - l
18029 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20  eafCorrection;. 
1802a 20 20 20 20 20 69 66 28 20 21 70 4f 6c 64 2d 3e       if( !pOld->
1802b 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 20 20  leaf ){.        
1802c 61 73 73 65 72 74 28 20 6c 65 61 66 43 6f 72 72  assert( leafCorr
1802d 65 63 74 69 6f 6e 3d 3d 30 20 29 3b 0a 20 20 20  ection==0 );.   
1802e 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 6c       assert( pOl
1802f 64 2d 3e 68 64 72 4f 66 66 73 65 74 3d 3d 30 20  d->hdrOffset==0 
18030 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  );.        /* Th
18031 65 20 72 69 67 68 74 20 70 6f 69 6e 74 65 72 20  e right pointer 
18032 6f 66 20 74 68 65 20 63 68 69 6c 64 20 70 61 67  of the child pag
18033 65 20 70 4f 6c 64 20 62 65 63 6f 6d 65 73 20 74  e pOld becomes t
18034 68 65 20 6c 65 66 74 0a 20 20 20 20 20 20 20 20  he left.        
18035 2a 2a 20 70 6f 69 6e 74 65 72 20 6f 66 20 74 68  ** pointer of th
18036 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20 2a  e divider cell *
18037 2f 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  /.        memcpy
18038 28 61 70 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c 20  (apCell[nCell], 
18039 26 70 4f 6c 64 2d 3e 61 44 61 74 61 5b 38 5d 2c  &pOld->aData[8],
1803a 20 34 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65   4);.      }else
1803b 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
1803c 28 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e  ( leafCorrection
1803d 3d 3d 34 20 29 3b 0a 20 20 20 20 20 20 20 20 69  ==4 );.        i
1803e 66 28 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d  f( szCell[nCell]
1803f 3c 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  <4 ){.          
18040 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20  /* Do not allow 
18041 61 6e 79 20 63 65 6c 6c 73 20 73 6d 61 6c 6c 65  any cells smalle
18042 72 20 74 68 61 6e 20 34 20 62 79 74 65 73 2e 20  r than 4 bytes. 
18043 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73 7a 43  */.          szC
18044 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 34 3b 0a  ell[nCell] = 4;.
18045 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
18046 7d 0a 20 20 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b  }.      nCell++;
18047 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
18048 0a 20 20 2a 2a 20 46 69 67 75 72 65 20 6f 75 74  .  ** Figure out
18049 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
1804a 61 67 65 73 20 6e 65 65 64 65 64 20 74 6f 20 68  ages needed to h
1804b 6f 6c 64 20 61 6c 6c 20 6e 43 65 6c 6c 20 63 65  old all nCell ce
1804c 6c 6c 73 2e 0a 20 20 2a 2a 20 53 74 6f 72 65 20  lls..  ** Store 
1804d 74 68 69 73 20 6e 75 6d 62 65 72 20 69 6e 20 22  this number in "
1804e 6b 22 2e 20 20 41 6c 73 6f 20 63 6f 6d 70 75 74  k".  Also comput
1804f 65 20 73 7a 4e 65 77 5b 5d 20 77 68 69 63 68 20  e szNew[] which 
18050 69 73 20 74 68 65 20 74 6f 74 61 6c 0a 20 20 2a  is the total.  *
18051 2a 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65  * size of all ce
18052 6c 6c 73 20 6f 6e 20 74 68 65 20 69 2d 74 68 20  lls on the i-th 
18053 70 61 67 65 20 61 6e 64 20 63 6e 74 4e 65 77 5b  page and cntNew[
18054 5d 20 77 68 69 63 68 20 69 73 20 74 68 65 20 69  ] which is the i
18055 6e 64 65 78 0a 20 20 2a 2a 20 69 6e 20 61 70 43  ndex.  ** in apC
18056 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 63 65 6c  ell[] of the cel
18057 6c 20 74 68 61 74 20 64 69 76 69 64 65 73 20 70  l that divides p
18058 61 67 65 20 69 20 66 72 6f 6d 20 70 61 67 65 20  age i from page 
18059 69 2b 31 2e 20 20 0a 20 20 2a 2a 20 63 6e 74 4e  i+1.  .  ** cntN
1805a 65 77 5b 6b 5d 20 73 68 6f 75 6c 64 20 65 71 75  ew[k] should equ
1805b 61 6c 20 6e 43 65 6c 6c 2e 0a 20 20 2a 2a 0a 20  al nCell..  **. 
1805c 20 2a 2a 20 56 61 6c 75 65 73 20 63 6f 6d 70 75   ** Values compu
1805d 74 65 64 20 62 79 20 74 68 69 73 20 62 6c 6f 63  ted by this bloc
1805e 6b 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  k:.  **.  **    
1805f 20 20 20 20 20 20 20 6b 3a 20 54 68 65 20 74 6f         k: The to
18060 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 69  tal number of si
18061 62 6c 69 6e 67 20 70 61 67 65 73 0a 20 20 2a 2a  bling pages.  **
18062 20 20 20 20 73 7a 4e 65 77 5b 69 5d 3a 20 53 70      szNew[i]: Sp
18063 61 63 65 64 20 75 73 65 64 20 6f 6e 20 74 68 65  aced used on the
18064 20 69 2d 74 68 20 73 69 62 6c 69 6e 67 20 70 61   i-th sibling pa
18065 67 65 2e 0a 20 20 2a 2a 20 20 20 63 6e 74 4e 65  ge..  **   cntNe
18066 77 5b 69 5d 3a 20 49 6e 64 65 78 20 69 6e 20 61  w[i]: Index in a
18067 70 43 65 6c 6c 5b 5d 20 61 6e 64 20 73 7a 43 65  pCell[] and szCe
18068 6c 6c 5b 5d 20 66 6f 72 20 74 68 65 20 66 69 72  ll[] for the fir
18069 73 74 20 63 65 6c 6c 20 74 6f 0a 20 20 2a 2a 20  st cell to.  ** 
1806a 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65               the
1806b 20 72 69 67 68 74 20 6f 66 20 74 68 65 20 69 2d   right of the i-
1806c 74 68 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e  th sibling page.
1806d 0a 20 20 2a 2a 20 75 73 61 62 6c 65 53 70 61 63  .  ** usableSpac
1806e 65 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  e: Number of byt
1806f 65 73 20 6f 66 20 73 70 61 63 65 20 61 76 61 69  es of space avai
18070 6c 61 62 6c 65 20 6f 6e 20 65 61 63 68 20 73 69  lable on each si
18071 62 6c 69 6e 67 2e 0a 20 20 2a 2a 20 0a 20 20 2a  bling..  ** .  *
18072 2f 0a 20 20 75 73 61 62 6c 65 53 70 61 63 65 20  /.  usableSpace 
18073 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  = pBt->usableSiz
18074 65 20 2d 20 31 32 20 2b 20 6c 65 61 66 43 6f 72  e - 12 + leafCor
18075 72 65 63 74 69 6f 6e 3b 0a 20 20 66 6f 72 28 73  rection;.  for(s
18076 75 62 74 6f 74 61 6c 3d 6b 3d 69 3d 30 3b 20 69  ubtotal=k=i=0; i
18077 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20  <nCell; i++){.  
18078 20 20 61 73 73 65 72 74 28 20 69 3c 6e 4d 61 78    assert( i<nMax
18079 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 73 75 62  Cells );.    sub
1807a 74 6f 74 61 6c 20 2b 3d 20 73 7a 43 65 6c 6c 5b  total += szCell[
1807b 69 5d 20 2b 20 32 3b 0a 20 20 20 20 69 66 28 20  i] + 2;.    if( 
1807c 73 75 62 74 6f 74 61 6c 20 3e 20 75 73 61 62 6c  subtotal > usabl
1807d 65 53 70 61 63 65 20 29 7b 0a 20 20 20 20 20 20  eSpace ){.      
1807e 73 7a 4e 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f  szNew[k] = subto
1807f 74 61 6c 20 2d 20 73 7a 43 65 6c 6c 5b 69 5d 3b  tal - szCell[i];
18080 0a 20 20 20 20 20 20 63 6e 74 4e 65 77 5b 6b 5d  .      cntNew[k]
18081 20 3d 20 69 3b 0a 20 20 20 20 20 20 69 66 28 20   = i;.      if( 
18082 6c 65 61 66 44 61 74 61 20 29 7b 20 69 2d 2d 3b  leafData ){ i--;
18083 20 7d 0a 20 20 20 20 20 20 73 75 62 74 6f 74 61   }.      subtota
18084 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 6b 2b 2b  l = 0;.      k++
18085 3b 0a 20 20 20 20 20 20 69 66 28 20 6b 3e 4e 42  ;.      if( k>NB
18086 2b 31 20 29 7b 20 72 63 20 3d 20 53 51 4c 49 54  +1 ){ rc = SQLIT
18087 45 5f 43 4f 52 52 55 50 54 3b 20 67 6f 74 6f 20  E_CORRUPT; goto 
18088 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b  balance_cleanup;
18089 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73   }.    }.  }.  s
1808a 7a 4e 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74  zNew[k] = subtot
1808b 61 6c 3b 0a 20 20 63 6e 74 4e 65 77 5b 6b 5d 20  al;.  cntNew[k] 
1808c 3d 20 6e 43 65 6c 6c 3b 0a 20 20 6b 2b 2b 3b 0a  = nCell;.  k++;.
1808d 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 70  .  /*.  ** The p
1808e 61 63 6b 69 6e 67 20 63 6f 6d 70 75 74 65 64 20  acking computed 
1808f 62 79 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  by the previous 
18090 62 6c 6f 63 6b 20 69 73 20 62 69 61 73 65 64 20  block is biased 
18091 74 6f 77 61 72 64 20 74 68 65 20 73 69 62 6c 69  toward the sibli
18092 6e 67 73 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20  ngs.  ** on the 
18093 6c 65 66 74 20 73 69 64 65 2e 20 20 54 68 65 20  left side.  The 
18094 6c 65 66 74 20 73 69 62 6c 69 6e 67 73 20 61 72  left siblings ar
18095 65 20 61 6c 77 61 79 73 20 6e 65 61 72 6c 79 20  e always nearly 
18096 66 75 6c 6c 2c 20 77 68 69 6c 65 20 74 68 65 0a  full, while the.
18097 20 20 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20    ** right-most 
18098 73 69 62 6c 69 6e 67 20 6d 69 67 68 74 20 62 65  sibling might be
18099 20 6e 65 61 72 6c 79 20 65 6d 70 74 79 2e 20 20   nearly empty.  
1809a 54 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f  This block of co
1809b 64 65 20 61 74 74 65 6d 70 74 73 0a 20 20 2a 2a  de attempts.  **
1809c 20 74 6f 20 61 64 6a 75 73 74 20 74 68 65 20 70   to adjust the p
1809d 61 63 6b 69 6e 67 20 6f 66 20 73 69 62 6c 69 6e  acking of siblin
1809e 67 73 20 74 6f 20 67 65 74 20 61 20 62 65 74 74  gs to get a bett
1809f 65 72 20 62 61 6c 61 6e 63 65 2e 0a 20 20 2a 2a  er balance..  **
180a0 0a 20 20 2a 2a 20 54 68 69 73 20 61 64 6a 75 73  .  ** This adjus
180a1 74 6d 65 6e 74 20 69 73 20 6d 6f 72 65 20 74 68  tment is more th
180a2 61 6e 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69  an an optimizati
180a3 6f 6e 2e 20 20 54 68 65 20 70 61 63 6b 69 6e 67  on.  The packing
180a4 20 61 62 6f 76 65 20 6d 69 67 68 74 0a 20 20 2a   above might.  *
180a5 2a 20 62 65 20 73 6f 20 6f 75 74 20 6f 66 20 62  * be so out of b
180a6 61 6c 61 6e 63 65 20 61 73 20 74 6f 20 62 65 20  alance as to be 
180a7 69 6c 6c 65 67 61 6c 2e 20 20 46 6f 72 20 65 78  illegal.  For ex
180a8 61 6d 70 6c 65 2c 20 74 68 65 20 72 69 67 68 74  ample, the right
180a9 2d 6d 6f 73 74 0a 20 20 2a 2a 20 73 69 62 6c 69  -most.  ** sibli
180aa 6e 67 20 6d 69 67 68 74 20 62 65 20 63 6f 6d 70  ng might be comp
180ab 6c 65 74 65 6c 79 20 65 6d 70 74 79 2e 20 20 54  letely empty.  T
180ac 68 69 73 20 61 64 6a 75 73 74 6d 65 6e 74 20 69  his adjustment i
180ad 73 20 6e 6f 74 20 6f 70 74 69 6f 6e 61 6c 2e 0a  s not optional..
180ae 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6b 2d 31    */.  for(i=k-1
180af 3b 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20  ; i>0; i--){.   
180b0 20 69 6e 74 20 73 7a 52 69 67 68 74 20 3d 20 73   int szRight = s
180b1 7a 4e 65 77 5b 69 5d 3b 20 20 2f 2a 20 53 69 7a  zNew[i];  /* Siz
180b2 65 20 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20  e of sibling on 
180b3 74 68 65 20 72 69 67 68 74 20 2a 2f 0a 20 20 20  the right */.   
180b4 20 69 6e 74 20 73 7a 4c 65 66 74 20 3d 20 73 7a   int szLeft = sz
180b5 4e 65 77 5b 69 2d 31 5d 3b 20 2f 2a 20 53 69 7a  New[i-1]; /* Siz
180b6 65 20 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20  e of sibling on 
180b7 74 68 65 20 6c 65 66 74 20 2a 2f 0a 20 20 20 20  the left */.    
180b8 69 6e 74 20 72 3b 20 20 20 20 20 20 20 20 20 20  int r;          
180b9 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
180ba 72 69 67 68 74 2d 6d 6f 73 74 20 63 65 6c 6c 20  right-most cell 
180bb 69 6e 20 6c 65 66 74 20 73 69 62 6c 69 6e 67 20  in left sibling 
180bc 2a 2f 0a 20 20 20 20 69 6e 74 20 64 3b 20 20 20  */.    int d;   
180bd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
180be 64 65 78 20 6f 66 20 66 69 72 73 74 20 63 65 6c  dex of first cel
180bf 6c 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66  l to the left of
180c0 20 72 69 67 68 74 20 73 69 62 6c 69 6e 67 20 2a   right sibling *
180c1 2f 0a 0a 20 20 20 20 72 20 3d 20 63 6e 74 4e 65  /..    r = cntNe
180c2 77 5b 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20  w[i-1] - 1;.    
180c3 64 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66  d = r + 1 - leaf
180c4 44 61 74 61 3b 0a 20 20 20 20 61 73 73 65 72 74  Data;.    assert
180c5 28 20 64 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b  ( d<nMaxCells );
180c6 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 3c 6e  .    assert( r<n
180c7 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20  MaxCells );.    
180c8 77 68 69 6c 65 28 20 73 7a 52 69 67 68 74 3d 3d  while( szRight==
180c9 30 20 7c 7c 20 73 7a 52 69 67 68 74 2b 73 7a 43  0 || szRight+szC
180ca 65 6c 6c 5b 64 5d 2b 32 3c 3d 73 7a 4c 65 66 74  ell[d]+2<=szLeft
180cb 2d 28 73 7a 43 65 6c 6c 5b 72 5d 2b 32 29 20 29  -(szCell[r]+2) )
180cc 7b 0a 20 20 20 20 20 20 73 7a 52 69 67 68 74 20  {.      szRight 
180cd 2b 3d 20 73 7a 43 65 6c 6c 5b 64 5d 20 2b 20 32  += szCell[d] + 2
180ce 3b 0a 20 20 20 20 20 20 73 7a 4c 65 66 74 20 2d  ;.      szLeft -
180cf 3d 20 73 7a 43 65 6c 6c 5b 72 5d 20 2b 20 32 3b  = szCell[r] + 2;
180d0 0a 20 20 20 20 20 20 63 6e 74 4e 65 77 5b 69 2d  .      cntNew[i-
180d1 31 5d 2d 2d 3b 0a 20 20 20 20 20 20 72 20 3d 20  1]--;.      r = 
180d2 63 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d 20 31 3b  cntNew[i-1] - 1;
180d3 0a 20 20 20 20 20 20 64 20 3d 20 72 20 2b 20 31  .      d = r + 1
180d4 20 2d 20 6c 65 61 66 44 61 74 61 3b 0a 20 20 20   - leafData;.   
180d5 20 7d 0a 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20   }.    szNew[i] 
180d6 3d 20 73 7a 52 69 67 68 74 3b 0a 20 20 20 20 73  = szRight;.    s
180d7 7a 4e 65 77 5b 69 2d 31 5d 20 3d 20 73 7a 4c 65  zNew[i-1] = szLe
180d8 66 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 69  ft;.  }..  /* Ei
180d9 74 68 65 72 20 77 65 20 66 6f 75 6e 64 20 6f 6e  ther we found on
180da 65 20 6f 72 20 6d 6f 72 65 20 63 65 6c 6c 73 20  e or more cells 
180db 28 63 6e 74 6e 65 77 5b 30 5d 29 3e 30 29 20 6f  (cntnew[0])>0) o
180dc 72 20 70 50 61 67 65 20 69 73 0a 20 20 2a 2a 20  r pPage is.  ** 
180dd 61 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20 70  a virtual root p
180de 61 67 65 2e 20 20 41 20 76 69 72 74 75 61 6c 20  age.  A virtual 
180df 72 6f 6f 74 20 70 61 67 65 20 69 73 20 77 68 65  root page is whe
180e0 6e 20 74 68 65 20 72 65 61 6c 20 72 6f 6f 74 0a  n the real root.
180e1 20 20 2a 2a 20 70 61 67 65 20 69 73 20 70 61 67    ** page is pag
180e2 65 20 31 20 61 6e 64 20 77 65 20 61 72 65 20 74  e 1 and we are t
180e3 68 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20 6f 66  he only child of
180e4 20 74 68 61 74 20 70 61 67 65 2e 0a 20 20 2a 2f   that page..  */
180e5 0a 20 20 61 73 73 65 72 74 28 20 63 6e 74 4e 65  .  assert( cntNe
180e6 77 5b 30 5d 3e 30 20 7c 7c 20 28 70 50 61 72 65  w[0]>0 || (pPare
180e7 6e 74 2d 3e 70 67 6e 6f 3d 3d 31 20 26 26 20 70  nt->pgno==1 && p
180e8 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d 30  Parent->nCell==0
180e9 29 20 29 3b 0a 0a 20 20 54 52 41 43 45 28 28 22  ) );..  TRACE(("
180ea 42 41 4c 41 4e 43 45 3a 20 6f 6c 64 3a 20 25 64  BALANCE: old: %d
180eb 20 25 64 20 25 64 20 20 22 2c 0a 20 20 20 20 61   %d %d  ",.    a
180ec 70 4f 6c 64 5b 30 5d 2d 3e 70 67 6e 6f 2c 20 0a  pOld[0]->pgno, .
180ed 20 20 20 20 6e 4f 6c 64 3e 3d 32 20 3f 20 61 70      nOld>=2 ? ap
180ee 4f 6c 64 5b 31 5d 2d 3e 70 67 6e 6f 20 3a 20 30  Old[1]->pgno : 0
180ef 2c 0a 20 20 20 20 6e 4f 6c 64 3e 3d 33 20 3f 20  ,.    nOld>=3 ? 
180f0 61 70 4f 6c 64 5b 32 5d 2d 3e 70 67 6e 6f 20 3a  apOld[2]->pgno :
180f1 20 30 0a 20 20 29 29 3b 0a 0a 20 20 2f 2a 0a 20   0.  ));..  /*. 
180f2 20 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 6b 20 6e   ** Allocate k n
180f3 65 77 20 70 61 67 65 73 2e 20 20 52 65 75 73 65  ew pages.  Reuse
180f4 20 6f 6c 64 20 70 61 67 65 73 20 77 68 65 72 65   old pages where
180f5 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a   possible..  */.
180f6 20 20 69 66 28 20 61 70 4f 6c 64 5b 30 5d 2d 3e    if( apOld[0]->
180f7 70 67 6e 6f 3c 3d 31 20 29 7b 0a 20 20 20 20 72  pgno<=1 ){.    r
180f8 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  c = SQLITE_CORRU
180f9 50 54 3b 0a 20 20 20 20 67 6f 74 6f 20 62 61 6c  PT;.    goto bal
180fa 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  ance_cleanup;.  
180fb 7d 0a 20 20 70 61 67 65 46 6c 61 67 73 20 3d 20  }.  pageFlags = 
180fc 61 70 4f 6c 64 5b 30 5d 2d 3e 61 44 61 74 61 5b  apOld[0]->aData[
180fd 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  0];.  for(i=0; i
180fe 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65  <k; i++){.    Me
180ff 6d 50 61 67 65 20 2a 70 4e 65 77 3b 0a 20 20 20  mPage *pNew;.   
18100 20 69 66 28 20 69 3c 6e 4f 6c 64 20 29 7b 0a 20   if( i<nOld ){. 
18101 20 20 20 20 20 70 4e 65 77 20 3d 20 61 70 4e 65       pNew = apNe
18102 77 5b 69 5d 20 3d 20 61 70 4f 6c 64 5b 69 5d 3b  w[i] = apOld[i];
18103 0a 20 20 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20  .      apOld[i] 
18104 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  = 0;.      rc = 
18105 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
18106 65 28 70 4e 65 77 2d 3e 70 44 62 50 61 67 65 29  e(pNew->pDbPage)
18107 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b 3b 0a  ;.      nNew++;.
18108 20 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67        if( rc ) g
18109 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61  oto balance_clea
1810a 6e 75 70 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  nup;.    }else{.
1810b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 3e        assert( i>
1810c 30 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 );.      rc = 
1810d 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67  allocateBtreePag
1810e 65 28 70 42 74 2c 20 26 70 4e 65 77 2c 20 26 70  e(pBt, &pNew, &p
1810f 67 6e 6f 2c 20 70 67 6e 6f 2c 20 30 29 3b 0a 20  gno, pgno, 0);. 
18110 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f       if( rc ) go
18111 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e  to balance_clean
18112 75 70 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b  up;.      apNew[
18113 69 5d 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20  i] = pNew;.     
18114 20 6e 4e 65 77 2b 2b 3b 0a 0a 20 20 20 20 20 20   nNew++;..      
18115 2f 2a 20 53 65 74 20 74 68 65 20 70 6f 69 6e 74  /* Set the point
18116 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f 72  er-map entry for
18117 20 74 68 65 20 6e 65 77 20 73 69 62 6c 69 6e 67   the new sibling
18118 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20 20 20 20   page. */.      
18119 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d  if( ISAUTOVACUUM
1811a 20 29 7b 0a 20 20 20 20 20 20 20 20 70 74 72 6d   ){.        ptrm
1811b 61 70 50 75 74 28 70 42 74 2c 20 70 4e 65 77 2d  apPut(pBt, pNew-
1811c 3e 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54  >pgno, PTRMAP_BT
1811d 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67  REE, pParent->pg
1811e 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20  no, &rc);.      
1811f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
18120 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
18121 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
18122 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 7d  eanup;.        }
18123 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
18124 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e   }..  /* Free an
18125 79 20 6f 6c 64 20 70 61 67 65 73 20 74 68 61 74  y old pages that
18126 20 77 65 72 65 20 6e 6f 74 20 72 65 75 73 65 64   were not reused
18127 20 61 73 20 6e 65 77 20 70 61 67 65 73 2e 0a 20   as new pages.. 
18128 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69 3c 6e   */.  while( i<n
18129 4f 6c 64 20 29 7b 0a 20 20 20 20 66 72 65 65 50  Old ){.    freeP
1812a 61 67 65 28 61 70 4f 6c 64 5b 69 5d 2c 20 26 72  age(apOld[i], &r
1812b 63 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  c);.    if( rc )
1812c 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
1812d 65 61 6e 75 70 3b 0a 20 20 20 20 72 65 6c 65 61  eanup;.    relea
1812e 73 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29  sePage(apOld[i])
1812f 3b 0a 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d  ;.    apOld[i] =
18130 20 30 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d   0;.    i++;.  }
18131 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 75 74 20  ..  /*.  ** Put 
18132 74 68 65 20 6e 65 77 20 70 61 67 65 73 20 69 6e  the new pages in
18133 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72   accending order
18134 2e 20 20 54 68 69 73 20 68 65 6c 70 73 20 74 6f  .  This helps to
18135 0a 20 20 2a 2a 20 6b 65 65 70 20 65 6e 74 72 69  .  ** keep entri
18136 65 73 20 69 6e 20 74 68 65 20 64 69 73 6b 20 66  es in the disk f
18137 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 73 6f 20  ile in order so 
18138 74 68 61 74 20 61 20 73 63 61 6e 0a 20 20 2a 2a  that a scan.  **
18139 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 73   of the table is
1813a 20 61 20 6c 69 6e 65 61 72 20 73 63 61 6e 20 74   a linear scan t
1813b 68 72 6f 75 67 68 20 74 68 65 20 66 69 6c 65 2e  hrough the file.
1813c 20 20 54 68 61 74 0a 20 20 2a 2a 20 69 6e 20 74    That.  ** in t
1813d 75 72 6e 20 68 65 6c 70 73 20 74 68 65 20 6f 70  urn helps the op
1813e 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 74  erating system t
1813f 6f 20 64 65 6c 69 76 65 72 20 70 61 67 65 73 0a  o deliver pages.
18140 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 64 69    ** from the di
18141 73 6b 20 6d 6f 72 65 20 72 61 70 69 64 6c 79 2e  sk more rapidly.
18142 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 4f 28  .  **.  ** An O(
18143 6e 5e 32 29 20 69 6e 73 65 72 74 69 6f 6e 20 73  n^2) insertion s
18144 6f 72 74 20 61 6c 67 6f 72 69 74 68 6d 20 69 73  ort algorithm is
18145 20 75 73 65 64 2c 20 62 75 74 20 73 69 6e 63 65   used, but since
18146 0a 20 20 2a 2a 20 6e 20 69 73 20 6e 65 76 65 72  .  ** n is never
18147 20 6d 6f 72 65 20 74 68 61 6e 20 4e 42 20 28 61   more than NB (a
18148 20 73 6d 61 6c 6c 20 63 6f 6e 73 74 61 6e 74 29   small constant)
18149 2c 20 74 68 61 74 20 73 68 6f 75 6c 64 0a 20 20  , that should.  
1814a 2a 2a 20 6e 6f 74 20 62 65 20 61 20 70 72 6f 62  ** not be a prob
1814b 6c 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57  lem..  **.  ** W
1814c 68 65 6e 20 4e 42 3d 3d 33 2c 20 74 68 69 73 20  hen NB==3, this 
1814d 6f 6e 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  one optimization
1814e 20 6d 61 6b 65 73 20 74 68 65 20 64 61 74 61 62   makes the datab
1814f 61 73 65 0a 20 20 2a 2a 20 61 62 6f 75 74 20 32  ase.  ** about 2
18150 35 25 20 66 61 73 74 65 72 20 66 6f 72 20 6c 61  5% faster for la
18151 72 67 65 20 69 6e 73 65 72 74 69 6f 6e 73 20 61  rge insertions a
18152 6e 64 20 64 65 6c 65 74 69 6f 6e 73 2e 0a 20 20  nd deletions..  
18153 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  */.  for(i=0; i<
18154 6b 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  k-1; i++){.    i
18155 6e 74 20 6d 69 6e 56 20 3d 20 61 70 4e 65 77 5b  nt minV = apNew[
18156 69 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 69 6e  i]->pgno;.    in
18157 74 20 6d 69 6e 49 20 3d 20 69 3b 0a 20 20 20 20  t minI = i;.    
18158 66 6f 72 28 6a 3d 69 2b 31 3b 20 6a 3c 6b 3b 20  for(j=i+1; j<k; 
18159 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  j++){.      if( 
1815a 61 70 4e 65 77 5b 6a 5d 2d 3e 70 67 6e 6f 3c 28  apNew[j]->pgno<(
1815b 75 6e 73 69 67 6e 65 64 29 6d 69 6e 56 20 29 7b  unsigned)minV ){
1815c 0a 20 20 20 20 20 20 20 20 6d 69 6e 49 20 3d 20  .        minI = 
1815d 6a 3b 0a 20 20 20 20 20 20 20 20 6d 69 6e 56 20  j;.        minV 
1815e 3d 20 61 70 4e 65 77 5b 6a 5d 2d 3e 70 67 6e 6f  = apNew[j]->pgno
1815f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
18160 20 20 20 20 69 66 28 20 6d 69 6e 49 3e 69 20 29      if( minI>i )
18161 7b 0a 20 20 20 20 20 20 69 6e 74 20 74 3b 0a 20  {.      int t;. 
18162 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 54       MemPage *pT
18163 3b 0a 20 20 20 20 20 20 74 20 3d 20 61 70 4e 65  ;.      t = apNe
18164 77 5b 69 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20  w[i]->pgno;.    
18165 20 20 70 54 20 3d 20 61 70 4e 65 77 5b 69 5d 3b    pT = apNew[i];
18166 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 69 5d 20  .      apNew[i] 
18167 3d 20 61 70 4e 65 77 5b 6d 69 6e 49 5d 3b 0a 20  = apNew[minI];. 
18168 20 20 20 20 20 61 70 4e 65 77 5b 6d 69 6e 49 5d       apNew[minI]
18169 20 3d 20 70 54 3b 0a 20 20 20 20 7d 0a 20 20 7d   = pT;.    }.  }
1816a 0a 20 20 54 52 41 43 45 28 28 22 6e 65 77 3a 20  .  TRACE(("new: 
1816b 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25 64  %d(%d) %d(%d) %d
1816c 28 25 64 29 20 25 64 28 25 64 29 20 25 64 28 25  (%d) %d(%d) %d(%
1816d 64 29 5c 6e 22 2c 0a 20 20 20 20 61 70 4e 65 77  d)\n",.    apNew
1816e 5b 30 5d 2d 3e 70 67 6e 6f 2c 20 73 7a 4e 65 77  [0]->pgno, szNew
1816f 5b 30 5d 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 32  [0],.    nNew>=2
18170 20 3f 20 61 70 4e 65 77 5b 31 5d 2d 3e 70 67 6e   ? apNew[1]->pgn
18171 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e 3d 32 20 3f  o : 0, nNew>=2 ?
18172 20 73 7a 4e 65 77 5b 31 5d 20 3a 20 30 2c 0a 20   szNew[1] : 0,. 
18173 20 20 20 6e 4e 65 77 3e 3d 33 20 3f 20 61 70 4e     nNew>=3 ? apN
18174 65 77 5b 32 5d 2d 3e 70 67 6e 6f 20 3a 20 30 2c  ew[2]->pgno : 0,
18175 20 6e 4e 65 77 3e 3d 33 20 3f 20 73 7a 4e 65 77   nNew>=3 ? szNew
18176 5b 32 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65  [2] : 0,.    nNe
18177 77 3e 3d 34 20 3f 20 61 70 4e 65 77 5b 33 5d 2d  w>=4 ? apNew[3]-
18178 3e 70 67 6e 6f 20 3a 20 30 2c 20 6e 4e 65 77 3e  >pgno : 0, nNew>
18179 3d 34 20 3f 20 73 7a 4e 65 77 5b 33 5d 20 3a 20  =4 ? szNew[3] : 
1817a 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 35 20 3f  0,.    nNew>=5 ?
1817b 20 61 70 4e 65 77 5b 34 5d 2d 3e 70 67 6e 6f 20   apNew[4]->pgno 
1817c 3a 20 30 2c 20 6e 4e 65 77 3e 3d 35 20 3f 20 73  : 0, nNew>=5 ? s
1817d 7a 4e 65 77 5b 34 5d 20 3a 20 30 29 29 3b 0a 0a  zNew[4] : 0));..
1817e 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1817f 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c  3PagerIswriteabl
18180 65 28 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61  e(pParent->pDbPa
18181 67 65 29 20 29 3b 0a 20 20 70 75 74 34 62 79 74  ge) );.  put4byt
18182 65 28 70 52 69 67 68 74 2c 20 61 70 4e 65 77 5b  e(pRight, apNew[
18183 6e 4e 65 77 2d 31 5d 2d 3e 70 67 6e 6f 29 3b 0a  nNew-1]->pgno);.
18184 0a 20 20 2f 2a 0a 20 20 2a 2a 20 45 76 65 6e 6c  .  /*.  ** Evenl
18185 79 20 64 69 73 74 72 69 62 75 74 65 20 74 68 65  y distribute the
18186 20 64 61 74 61 20 69 6e 20 61 70 43 65 6c 6c 5b   data in apCell[
18187 5d 20 61 63 72 6f 73 73 20 74 68 65 20 6e 65 77  ] across the new
18188 20 70 61 67 65 73 2e 0a 20 20 2a 2a 20 49 6e 73   pages..  ** Ins
18189 65 72 74 20 64 69 76 69 64 65 72 20 63 65 6c 6c  ert divider cell
1818a 73 20 69 6e 74 6f 20 70 50 61 72 65 6e 74 20 61  s into pParent a
1818b 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 20 2a  s necessary..  *
1818c 2f 0a 20 20 6a 20 3d 20 30 3b 0a 20 20 66 6f 72  /.  j = 0;.  for
1818d 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b  (i=0; i<nNew; i+
1818e 2b 29 7b 0a 20 20 20 20 2f 2a 20 41 73 73 65 6d  +){.    /* Assem
1818f 62 6c 65 20 74 68 65 20 6e 65 77 20 73 69 62 6c  ble the new sibl
18190 69 6e 67 20 70 61 67 65 2e 20 2a 2f 0a 20 20 20  ing page. */.   
18191 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 20 3d   MemPage *pNew =
18192 20 61 70 4e 65 77 5b 69 5d 3b 0a 20 20 20 20 61   apNew[i];.    a
18193 73 73 65 72 74 28 20 6a 3c 6e 4d 61 78 43 65 6c  ssert( j<nMaxCel
18194 6c 73 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61  ls );.    zeroPa
18195 67 65 28 70 4e 65 77 2c 20 70 61 67 65 46 6c 61  ge(pNew, pageFla
18196 67 73 29 3b 0a 20 20 20 20 61 73 73 65 6d 62 6c  gs);.    assembl
18197 65 50 61 67 65 28 70 4e 65 77 2c 20 63 6e 74 4e  ePage(pNew, cntN
18198 65 77 5b 69 5d 2d 6a 2c 20 26 61 70 43 65 6c 6c  ew[i]-j, &apCell
18199 5b 6a 5d 2c 20 26 73 7a 43 65 6c 6c 5b 6a 5d 29  [j], &szCell[j])
1819a 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e  ;.    assert( pN
1819b 65 77 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20 28  ew->nCell>0 || (
1819c 6e 4e 65 77 3d 3d 31 20 26 26 20 63 6e 74 4e 65  nNew==1 && cntNe
1819d 77 5b 30 5d 3d 3d 30 29 20 29 3b 0a 20 20 20 20  w[0]==0) );.    
1819e 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 4f  assert( pNew->nO
1819f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a 20  verflow==0 );.. 
181a0 20 20 20 6a 20 3d 20 63 6e 74 4e 65 77 5b 69 5d     j = cntNew[i]
181a1 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  ;..    /* If the
181a2 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20 61 73   sibling page as
181a3 73 65 6d 62 6c 65 64 20 61 62 6f 76 65 20 77 61  sembled above wa
181a4 73 20 6e 6f 74 20 74 68 65 20 72 69 67 68 74 2d  s not the right-
181a5 6d 6f 73 74 20 73 69 62 6c 69 6e 67 2c 0a 20 20  most sibling,.  
181a6 20 20 2a 2a 20 69 6e 73 65 72 74 20 61 20 64 69    ** insert a di
181a7 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 74 6f 20  vider cell into 
181a8 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e  the parent page.
181a9 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
181aa 72 74 28 20 69 3c 6e 4e 65 77 2d 31 20 7c 7c 20  rt( i<nNew-1 || 
181ab 6a 3d 3d 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20  j==nCell );.    
181ac 69 66 28 20 6a 3c 6e 43 65 6c 6c 20 29 7b 0a 20  if( j<nCell ){. 
181ad 20 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a       u8 *pCell;.
181ae 20 20 20 20 20 20 75 38 20 2a 70 54 65 6d 70 3b        u8 *pTemp;
181af 0a 20 20 20 20 20 20 69 6e 74 20 73 7a 3b 0a 0a  .      int sz;..
181b0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c        assert( j<
181b1 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20  nMaxCells );.   
181b2 20 20 20 70 43 65 6c 6c 20 3d 20 61 70 43 65 6c     pCell = apCel
181b3 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 73 7a 20 3d  l[j];.      sz =
181b4 20 73 7a 43 65 6c 6c 5b 6a 5d 20 2b 20 6c 65 61   szCell[j] + lea
181b5 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a 20 20 20  fCorrection;.   
181b6 20 20 20 70 54 65 6d 70 20 3d 20 26 61 4f 76 66     pTemp = &aOvf
181b7 6c 53 70 61 63 65 5b 69 4f 76 66 6c 53 70 61 63  lSpace[iOvflSpac
181b8 65 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70  e];.      if( !p
181b9 4e 65 77 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  New->leaf ){.   
181ba 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65       memcpy(&pNe
181bb 77 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 70 43 65  w->aData[8], pCe
181bc 6c 6c 2c 20 34 29 3b 0a 20 20 20 20 20 20 7d 65  ll, 4);.      }e
181bd 6c 73 65 20 69 66 28 20 6c 65 61 66 44 61 74 61  lse if( leafData
181be 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49   ){.        /* I
181bf 66 20 74 68 65 20 74 72 65 65 20 69 73 20 61 20  f the tree is a 
181c0 6c 65 61 66 2d 64 61 74 61 20 74 72 65 65 2c 20  leaf-data tree, 
181c1 61 6e 64 20 74 68 65 20 73 69 62 6c 69 6e 67 73  and the siblings
181c2 20 61 72 65 20 6c 65 61 76 65 73 2c 20 0a 20 20   are leaves, .  
181c3 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68        ** then th
181c4 65 72 65 20 69 73 20 6e 6f 20 64 69 76 69 64 65  ere is no divide
181c5 72 20 63 65 6c 6c 20 69 6e 20 61 70 43 65 6c 6c  r cell in apCell
181c6 5b 5d 2e 20 49 6e 73 74 65 61 64 2c 20 74 68 65  []. Instead, the
181c7 20 64 69 76 69 64 65 72 20 0a 20 20 20 20 20 20   divider .      
181c8 20 20 2a 2a 20 63 65 6c 6c 20 63 6f 6e 73 69 73    ** cell consis
181c9 74 73 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65  ts of the intege
181ca 72 20 6b 65 79 20 66 6f 72 20 74 68 65 20 72 69  r key for the ri
181cb 67 68 74 2d 6d 6f 73 74 20 63 65 6c 6c 20 6f 66  ght-most cell of
181cc 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65   .        ** the
181cd 20 73 69 62 6c 69 6e 67 2d 70 61 67 65 20 61 73   sibling-page as
181ce 73 65 6d 62 6c 65 64 20 61 62 6f 76 65 20 6f 6e  sembled above on
181cf 6c 79 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ly..        */. 
181d0 20 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20         CellInfo 
181d1 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 6a 2d  info;.        j-
181d2 2d 3b 0a 20 20 20 20 20 20 20 20 62 74 72 65 65  -;.        btree
181d3 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 4e 65  ParseCellPtr(pNe
181d4 77 2c 20 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 69  w, apCell[j], &i
181d5 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 70 43  nfo);.        pC
181d6 65 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20  ell = pTemp;.   
181d7 20 20 20 20 20 73 7a 20 3d 20 34 20 2b 20 70 75       sz = 4 + pu
181d8 74 56 61 72 69 6e 74 28 26 70 43 65 6c 6c 5b 34  tVarint(&pCell[4
181d9 5d 2c 20 69 6e 66 6f 2e 6e 4b 65 79 29 3b 0a 20  ], info.nKey);. 
181da 20 20 20 20 20 20 20 70 54 65 6d 70 20 3d 20 30         pTemp = 0
181db 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
181dc 20 20 20 20 20 20 20 70 43 65 6c 6c 20 2d 3d 20         pCell -= 
181dd 34 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f 62  4;.        /* Ob
181de 73 63 75 72 65 20 63 61 73 65 20 66 6f 72 20 6e  scure case for n
181df 6f 6e 2d 6c 65 61 66 2d 64 61 74 61 20 74 72 65  on-leaf-data tre
181e0 65 73 3a 20 49 66 20 74 68 65 20 63 65 6c 6c 20  es: If the cell 
181e1 61 74 20 70 43 65 6c 6c 20 77 61 73 0a 20 20 20  at pCell was.   
181e2 20 20 20 20 20 2a 2a 20 70 72 65 76 69 6f 75 73       ** previous
181e3 6c 79 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 6c  ly stored on a l
181e4 65 61 66 20 6e 6f 64 65 2c 20 61 6e 64 20 69 74  eaf node, and it
181e5 73 20 72 65 70 6f 72 74 65 64 20 73 69 7a 65 20  s reported size 
181e6 77 61 73 20 34 0a 20 20 20 20 20 20 20 20 2a 2a  was 4.        **
181e7 20 62 79 74 65 73 2c 20 74 68 65 6e 20 69 74 20   bytes, then it 
181e8 6d 61 79 20 61 63 74 75 61 6c 6c 79 20 62 65 20  may actually be 
181e9 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 69  smaller than thi
181ea 73 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 28 73  s .        ** (s
181eb 65 65 20 62 74 72 65 65 50 61 72 73 65 43 65 6c  ee btreeParseCel
181ec 6c 50 74 72 28 29 2c 20 34 20 62 79 74 65 73 20  lPtr(), 4 bytes 
181ed 69 73 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 73  is the minimum s
181ee 69 7a 65 20 6f 66 0a 20 20 20 20 20 20 20 20 2a  ize of.        *
181ef 2a 20 61 6e 79 20 63 65 6c 6c 29 2e 20 42 75 74  * any cell). But
181f0 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74   it is important
181f1 20 74 6f 20 70 61 73 73 20 74 68 65 20 63 6f 72   to pass the cor
181f2 72 65 63 74 20 73 69 7a 65 20 74 6f 20 0a 20 20  rect size to .  
181f3 20 20 20 20 20 20 2a 2a 20 69 6e 73 65 72 74 43        ** insertC
181f4 65 6c 6c 28 29 2c 20 73 6f 20 72 65 70 61 72 73  ell(), so repars
181f5 65 20 74 68 65 20 63 65 6c 6c 20 6e 6f 77 2e 0a  e the cell now..
181f6 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
181f7 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20     ** Note that 
181f8 74 68 69 73 20 63 61 6e 20 6e 65 76 65 72 20 68  this can never h
181f9 61 70 70 65 6e 20 69 6e 20 61 6e 20 53 51 4c 69  appen in an SQLi
181fa 74 65 20 64 61 74 61 20 66 69 6c 65 2c 20 61 73  te data file, as
181fb 20 61 6c 6c 0a 20 20 20 20 20 20 20 20 2a 2a 20   all.        ** 
181fc 63 65 6c 6c 73 20 61 72 65 20 61 74 20 6c 65 61  cells are at lea
181fd 73 74 20 34 20 62 79 74 65 73 2e 20 49 74 20 6f  st 4 bytes. It o
181fe 6e 6c 79 20 68 61 70 70 65 6e 73 20 69 6e 20 62  nly happens in b
181ff 2d 74 72 65 65 73 20 75 73 65 64 0a 20 20 20 20  -trees used.    
18200 20 20 20 20 2a 2a 20 74 6f 20 65 76 61 6c 75 61      ** to evalua
18201 74 65 20 22 49 4e 20 28 53 45 4c 45 43 54 20 2e  te "IN (SELECT .
18202 2e 2e 29 22 20 61 6e 64 20 73 69 6d 69 6c 61 72  ..)" and similar
18203 20 63 6c 61 75 73 65 73 2e 0a 20 20 20 20 20 20   clauses..      
18204 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28    */.        if(
18205 20 73 7a 43 65 6c 6c 5b 6a 5d 3d 3d 34 20 29 7b   szCell[j]==4 ){
18206 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
18207 74 28 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e  t(leafCorrection
18208 3d 3d 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ==4);.          
18209 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72  sz = cellSizePtr
1820a 28 70 50 61 72 65 6e 74 2c 20 70 43 65 6c 6c 29  (pParent, pCell)
1820b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1820c 20 20 7d 0a 20 20 20 20 20 20 69 4f 76 66 6c 53    }.      iOvflS
1820d 70 61 63 65 20 2b 3d 20 73 7a 3b 0a 20 20 20 20  pace += sz;.    
1820e 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d 70 42    assert( sz<=pB
1820f 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20 29 3b  t->pageSize/4 );
18210 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69  .      assert( i
18211 4f 76 66 6c 53 70 61 63 65 3c 3d 70 42 74 2d 3e  OvflSpace<=pBt->
18212 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 20  pageSize );.    
18213 20 20 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61    insertCell(pPa
18214 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 70 43 65  rent, nxDiv, pCe
18215 6c 6c 2c 20 73 7a 2c 20 70 54 65 6d 70 2c 20 70  ll, sz, pTemp, p
18216 4e 65 77 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b  New->pgno, &rc);
18217 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
18218 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20  QLITE_OK ) goto 
18219 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b  balance_cleanup;
1821a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73  .      assert( s
1821b 71 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69  qlite3PagerIswri
1821c 74 65 61 62 6c 65 28 70 50 61 72 65 6e 74 2d 3e  teable(pParent->
1821d 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 20  pDbPage) );..   
1821e 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 6e 78     j++;.      nx
1821f 44 69 76 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  Div++;.    }.  }
18220 0a 20 20 61 73 73 65 72 74 28 20 6a 3d 3d 6e 43  .  assert( j==nC
18221 65 6c 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ell );.  assert(
18222 20 6e 4f 6c 64 3e 30 20 29 3b 0a 20 20 61 73 73   nOld>0 );.  ass
18223 65 72 74 28 20 6e 4e 65 77 3e 30 20 29 3b 0a 20  ert( nNew>0 );. 
18224 20 69 66 28 20 28 70 61 67 65 46 6c 61 67 73 20   if( (pageFlags 
18225 26 20 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20 29  & PTF_LEAF)==0 )
18226 7b 0a 20 20 20 20 75 38 20 2a 7a 43 68 69 6c 64  {.    u8 *zChild
18227 20 3d 20 26 61 70 43 6f 70 79 5b 6e 4f 6c 64 2d   = &apCopy[nOld-
18228 31 5d 2d 3e 61 44 61 74 61 5b 38 5d 3b 0a 20 20  1]->aData[8];.  
18229 20 20 6d 65 6d 63 70 79 28 26 61 70 4e 65 77 5b    memcpy(&apNew[
1822a 6e 4e 65 77 2d 31 5d 2d 3e 61 44 61 74 61 5b 38  nNew-1]->aData[8
1822b 5d 2c 20 7a 43 68 69 6c 64 2c 20 34 29 3b 0a 20  ], zChild, 4);. 
1822c 20 7d 0a 0a 20 20 69 66 28 20 69 73 52 6f 6f 74   }..  if( isRoot
1822d 20 26 26 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65   && pParent->nCe
1822e 6c 6c 3d 3d 30 20 26 26 20 70 50 61 72 65 6e 74  ll==0 && pParent
1822f 2d 3e 68 64 72 4f 66 66 73 65 74 3c 3d 61 70 4e  ->hdrOffset<=apN
18230 65 77 5b 30 5d 2d 3e 6e 46 72 65 65 20 29 7b 0a  ew[0]->nFree ){.
18231 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20      /* The root 
18232 70 61 67 65 20 6f 66 20 74 68 65 20 62 2d 74 72  page of the b-tr
18233 65 65 20 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 20  ee now contains 
18234 6e 6f 20 63 65 6c 6c 73 2e 20 54 68 65 20 6f 6e  no cells. The on
18235 6c 79 20 73 69 62 6c 69 6e 67 0a 20 20 20 20 2a  ly sibling.    *
18236 2a 20 70 61 67 65 20 69 73 20 74 68 65 20 72 69  * page is the ri
18237 67 68 74 2d 63 68 69 6c 64 20 6f 66 20 74 68 65  ght-child of the
18238 20 70 61 72 65 6e 74 2e 20 43 6f 70 79 20 74 68   parent. Copy th
18239 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
1823a 65 0a 20 20 20 20 2a 2a 20 63 68 69 6c 64 20 70  e.    ** child p
1823b 61 67 65 20 69 6e 74 6f 20 74 68 65 20 70 61 72  age into the par
1823c 65 6e 74 2c 20 64 65 63 72 65 61 73 69 6e 67 20  ent, decreasing 
1823d 74 68 65 20 6f 76 65 72 61 6c 6c 20 68 65 69 67  the overall heig
1823e 68 74 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a  ht of the.    **
1823f 20 62 2d 74 72 65 65 20 73 74 72 75 63 74 75 72   b-tree structur
18240 65 20 62 79 20 6f 6e 65 2e 20 54 68 69 73 20 69  e by one. This i
18241 73 20 64 65 73 63 72 69 62 65 64 20 61 73 20 74  s described as t
18242 68 65 20 22 62 61 6c 61 6e 63 65 2d 73 68 61 6c  he "balance-shal
18243 6c 6f 77 65 72 22 0a 20 20 20 20 2a 2a 20 73 75  lower".    ** su
18244 62 2d 61 6c 67 6f 72 69 74 68 6d 20 69 6e 20 73  b-algorithm in s
18245 6f 6d 65 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f  ome documentatio
18246 6e 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  n..    **.    **
18247 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 61   If this is an a
18248 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62  uto-vacuum datab
18249 61 73 65 2c 20 74 68 65 20 63 61 6c 6c 20 74 6f  ase, the call to
1824a 20 63 6f 70 79 4e 6f 64 65 43 6f 6e 74 65 6e 74   copyNodeContent
1824b 28 29 20 0a 20 20 20 20 2a 2a 20 73 65 74 73 20  () .    ** sets 
1824c 61 6c 6c 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  all pointer-map 
1824d 65 6e 74 72 69 65 73 20 63 6f 72 72 65 73 70 6f  entries correspo
1824e 6e 64 69 6e 67 20 74 6f 20 64 61 74 61 62 61 73  nding to databas
1824f 65 20 69 6d 61 67 65 20 70 61 67 65 73 20 0a 20  e image pages . 
18250 20 20 20 2a 2a 20 66 6f 72 20 77 68 69 63 68 20     ** for which 
18251 74 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20 73  the pointer is s
18252 74 6f 72 65 64 20 77 69 74 68 69 6e 20 74 68 65  tored within the
18253 20 63 6f 6e 74 65 6e 74 20 62 65 69 6e 67 20 63   content being c
18254 6f 70 69 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20  opied..    **.  
18255 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20    ** The second 
18256 61 73 73 65 72 74 20 62 65 6c 6f 77 20 76 65 72  assert below ver
18257 69 66 69 65 73 20 74 68 61 74 20 74 68 65 20 63  ifies that the c
18258 68 69 6c 64 20 70 61 67 65 20 69 73 20 64 65 66  hild page is def
18259 72 61 67 6d 65 6e 74 65 64 0a 20 20 20 20 2a 2a  ragmented.    **
1825a 20 28 69 74 20 6d 75 73 74 20 62 65 2c 20 61 73   (it must be, as
1825b 20 69 74 20 77 61 73 20 6a 75 73 74 20 72 65 63   it was just rec
1825c 6f 6e 73 74 72 75 63 74 65 64 20 75 73 69 6e 67  onstructed using
1825d 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 29 29   assemblePage())
1825e 2e 20 54 68 69 73 0a 20 20 20 20 2a 2a 20 69 73  . This.    ** is
1825f 20 69 6d 70 6f 72 74 61 6e 74 20 69 66 20 74 68   important if th
18260 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 68 61  e parent page ha
18261 70 70 65 6e 73 20 74 6f 20 62 65 20 70 61 67 65  ppens to be page
18262 20 31 20 6f 66 20 74 68 65 20 64 61 74 61 62 61   1 of the databa
18263 73 65 0a 20 20 20 20 2a 2a 20 69 6d 61 67 65 2e  se.    ** image.
18264 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
18265 20 6e 4e 65 77 3d 3d 31 20 29 3b 0a 20 20 20 20   nNew==1 );.    
18266 61 73 73 65 72 74 28 20 61 70 4e 65 77 5b 30 5d  assert( apNew[0]
18267 2d 3e 6e 46 72 65 65 20 3d 3d 20 0a 20 20 20 20  ->nFree == .    
18268 20 20 20 20 28 67 65 74 32 62 79 74 65 28 26 61      (get2byte(&a
18269 70 4e 65 77 5b 30 5d 2d 3e 61 44 61 74 61 5b 35  pNew[0]->aData[5
1826a 5d 29 2d 61 70 4e 65 77 5b 30 5d 2d 3e 63 65 6c  ])-apNew[0]->cel
1826b 6c 4f 66 66 73 65 74 2d 61 70 4e 65 77 5b 30 5d  lOffset-apNew[0]
1826c 2d 3e 6e 43 65 6c 6c 2a 32 29 20 0a 20 20 20 20  ->nCell*2) .    
1826d 29 3b 0a 20 20 20 20 63 6f 70 79 4e 6f 64 65 43  );.    copyNodeC
1826e 6f 6e 74 65 6e 74 28 61 70 4e 65 77 5b 30 5d 2c  ontent(apNew[0],
1826f 20 70 50 61 72 65 6e 74 2c 20 26 72 63 29 3b 0a   pParent, &rc);.
18270 20 20 20 20 66 72 65 65 50 61 67 65 28 61 70 4e      freePage(apN
18271 65 77 5b 30 5d 2c 20 26 72 63 29 3b 0a 20 20 7d  ew[0], &rc);.  }
18272 65 6c 73 65 20 69 66 28 20 49 53 41 55 54 4f 56  else if( ISAUTOV
18273 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 2f 2a 20  ACUUM ){.    /* 
18274 46 69 78 20 74 68 65 20 70 6f 69 6e 74 65 72 2d  Fix the pointer-
18275 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72 20  map entries for 
18276 61 6c 6c 20 74 68 65 20 63 65 6c 6c 73 20 74 68  all the cells th
18277 61 74 20 77 65 72 65 20 73 68 69 66 74 65 64 20  at were shifted 
18278 61 72 6f 75 6e 64 2e 20 0a 20 20 20 20 2a 2a 20  around. .    ** 
18279 54 68 65 72 65 20 61 72 65 20 73 65 76 65 72 61  There are severa
1827a 6c 20 64 69 66 66 65 72 65 6e 74 20 74 79 70 65  l different type
1827b 73 20 6f 66 20 70 6f 69 6e 74 65 72 2d 6d 61 70  s of pointer-map
1827c 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 6e 65   entries that ne
1827d 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20  ed to.    ** be 
1827e 64 65 61 6c 74 20 77 69 74 68 20 62 79 20 74 68  dealt with by th
1827f 69 73 20 72 6f 75 74 69 6e 65 2e 20 53 6f 6d 65  is routine. Some
18280 20 6f 66 20 74 68 65 73 65 20 68 61 76 65 20 62   of these have b
18281 65 65 6e 20 73 65 74 20 61 6c 72 65 61 64 79 2c  een set already,
18282 20 62 75 74 0a 20 20 20 20 2a 2a 20 6d 61 6e 79   but.    ** many
18283 20 68 61 76 65 20 6e 6f 74 2e 20 54 68 65 20 66   have not. The f
18284 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 61 20 73 75  ollowing is a su
18285 6d 6d 61 72 79 3a 0a 20 20 20 20 2a 2a 0a 20 20  mmary:.    **.  
18286 20 20 2a 2a 20 20 20 31 29 20 54 68 65 20 65 6e    **   1) The en
18287 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 64  tries associated
18288 20 77 69 74 68 20 6e 65 77 20 73 69 62 6c 69 6e   with new siblin
18289 67 20 70 61 67 65 73 20 74 68 61 74 20 77 65 72  g pages that wer
1828a 65 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 20 20 20  e not.    **    
1828b 20 20 73 69 62 6c 69 6e 67 73 20 77 68 65 6e 20    siblings when 
1828c 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 61  this function wa
1828d 73 20 63 61 6c 6c 65 64 2e 20 54 68 65 73 65 20  s called. These 
1828e 68 61 76 65 20 61 6c 72 65 61 64 79 0a 20 20 20  have already.   
1828f 20 2a 2a 20 20 20 20 20 20 62 65 65 6e 20 73 65   **      been se
18290 74 2e 20 57 65 20 64 6f 6e 27 74 20 6e 65 65 64  t. We don't need
18291 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74 20   to worry about 
18292 6f 6c 64 20 73 69 62 6c 69 6e 67 73 20 74 68 61  old siblings tha
18293 74 20 77 65 72 65 0a 20 20 20 20 2a 2a 20 20 20  t were.    **   
18294 20 20 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20     moved to the 
18295 66 72 65 65 2d 6c 69 73 74 20 2d 20 74 68 65 20  free-list - the 
18296 66 72 65 65 50 61 67 65 28 29 20 63 6f 64 65 20  freePage() code 
18297 68 61 73 20 74 61 6b 65 6e 20 63 61 72 65 0a 20  has taken care. 
18298 20 20 20 2a 2a 20 20 20 20 20 20 6f 66 20 74 68     **      of th
18299 6f 73 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ose..    **.    
1829a 2a 2a 20 20 20 32 29 20 54 68 65 20 70 6f 69 6e  **   2) The poin
1829b 74 65 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20  ter-map entries 
1829c 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
1829d 74 68 65 20 66 69 72 73 74 20 6f 76 65 72 66 6c  the first overfl
1829e 6f 77 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 70  ow.    **      p
1829f 61 67 65 20 69 6e 20 61 6e 79 20 6f 76 65 72 66  age in any overf
182a0 6c 6f 77 20 63 68 61 69 6e 73 20 75 73 65 64 20  low chains used 
182a1 62 79 20 6e 65 77 20 64 69 76 69 64 65 72 20 63  by new divider c
182a2 65 6c 6c 73 2e 20 54 68 65 73 65 20 0a 20 20 20  ells. These .   
182a3 20 2a 2a 20 20 20 20 20 20 68 61 76 65 20 61 6c   **      have al
182a4 73 6f 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  so already been 
182a5 74 61 6b 65 6e 20 63 61 72 65 20 6f 66 20 62 79  taken care of by
182a6 20 74 68 65 20 69 6e 73 65 72 74 43 65 6c 6c 28   the insertCell(
182a7 29 20 63 6f 64 65 2e 0a 20 20 20 20 2a 2a 0a 20  ) code..    **. 
182a8 20 20 20 2a 2a 20 20 20 33 29 20 49 66 20 74 68     **   3) If th
182a9 65 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 20  e sibling pages 
182aa 61 72 65 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20  are not leaves, 
182ab 74 68 65 6e 20 74 68 65 20 63 68 69 6c 64 20 70  then the child p
182ac 61 67 65 73 20 6f 66 0a 20 20 20 20 2a 2a 20 20  ages of.    **  
182ad 20 20 20 20 63 65 6c 6c 73 20 73 74 6f 72 65 64      cells stored
182ae 20 6f 6e 20 74 68 65 20 73 69 62 6c 69 6e 67 20   on the sibling 
182af 70 61 67 65 73 20 6d 61 79 20 6e 65 65 64 20 74  pages may need t
182b0 6f 20 62 65 20 75 70 64 61 74 65 64 2e 0a 20 20  o be updated..  
182b1 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 34 29    **.    **   4)
182b2 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e 67 20   If the sibling 
182b3 70 61 67 65 73 20 61 72 65 20 6e 6f 74 20 69 6e  pages are not in
182b4 74 65 72 6e 61 6c 20 69 6e 74 6b 65 79 20 6e 6f  ternal intkey no
182b5 64 65 73 2c 20 74 68 65 6e 20 61 6e 79 0a 20 20  des, then any.  
182b6 20 20 2a 2a 20 20 20 20 20 20 6f 76 65 72 66 6c    **      overfl
182b7 6f 77 20 70 61 67 65 73 20 75 73 65 64 20 62 79  ow pages used by
182b8 20 74 68 65 73 65 20 63 65 6c 6c 73 20 6d 61 79   these cells may
182b9 20 6e 65 65 64 20 74 6f 20 62 65 20 75 70 64 61   need to be upda
182ba 74 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  ted.    **      
182bb 28 69 6e 74 65 72 6e 61 6c 20 69 6e 74 6b 65 79  (internal intkey
182bc 20 6e 6f 64 65 73 20 6e 65 76 65 72 20 63 6f 6e   nodes never con
182bd 74 61 69 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f  tain pointers to
182be 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 29   overflow pages)
182bf 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
182c0 20 20 35 29 20 49 66 20 74 68 65 20 73 69 62 6c    5) If the sibl
182c1 69 6e 67 20 70 61 67 65 73 20 61 72 65 20 6e 6f  ing pages are no
182c2 74 20 6c 65 61 76 65 73 2c 20 74 68 65 6e 20 74  t leaves, then t
182c3 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20  he pointer-map. 
182c4 20 20 20 2a 2a 20 20 20 20 20 20 65 6e 74 72 69     **      entri
182c5 65 73 20 66 6f 72 20 74 68 65 20 72 69 67 68 74  es for the right
182c6 2d 63 68 69 6c 64 20 70 61 67 65 73 20 6f 66 20  -child pages of 
182c7 65 61 63 68 20 73 69 62 6c 69 6e 67 20 6d 61 79  each sibling may
182c8 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20 20 20 20   need.    **    
182c9 20 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 2e    to be updated.
182ca 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43  .    **.    ** C
182cb 61 73 65 73 20 31 20 61 6e 64 20 32 20 61 72 65  ases 1 and 2 are
182cc 20 64 65 61 6c 74 20 77 69 74 68 20 61 62 6f 76   dealt with abov
182cd 65 20 62 79 20 6f 74 68 65 72 20 63 6f 64 65 2e  e by other code.
182ce 20 54 68 65 20 6e 65 78 74 0a 20 20 20 20 2a 2a   The next.    **
182cf 20 62 6c 6f 63 6b 20 64 65 61 6c 73 20 77 69 74   block deals wit
182d0 68 20 63 61 73 65 73 20 33 20 61 6e 64 20 34 20  h cases 3 and 4 
182d1 61 6e 64 20 74 68 65 20 6f 6e 65 20 61 66 74 65  and the one afte
182d2 72 20 74 68 61 74 2c 20 63 61 73 65 20 35 2e 20  r that, case 5. 
182d3 53 69 6e 63 65 0a 20 20 20 20 2a 2a 20 73 65 74  Since.    ** set
182d4 74 69 6e 67 20 61 20 70 6f 69 6e 74 65 72 20 6d  ting a pointer m
182d5 61 70 20 65 6e 74 72 79 20 69 73 20 61 20 72 65  ap entry is a re
182d6 6c 61 74 69 76 65 6c 79 20 65 78 70 65 6e 73 69  latively expensi
182d7 76 65 20 6f 70 65 72 61 74 69 6f 6e 2c 20 74 68  ve operation, th
182d8 69 73 0a 20 20 20 20 2a 2a 20 63 6f 64 65 20 6f  is.    ** code o
182d9 6e 6c 79 20 73 65 74 73 20 70 6f 69 6e 74 65 72  nly sets pointer
182da 20 6d 61 70 20 65 6e 74 72 69 65 73 20 66 6f 72   map entries for
182db 20 63 68 69 6c 64 20 6f 72 20 6f 76 65 72 66 6c   child or overfl
182dc 6f 77 20 70 61 67 65 73 20 74 68 61 74 20 68 61  ow pages that ha
182dd 76 65 0a 20 20 20 20 2a 2a 20 61 63 74 75 61 6c  ve.    ** actual
182de 6c 79 20 6d 6f 76 65 64 20 62 65 74 77 65 65 6e  ly moved between
182df 20 70 61 67 65 73 2e 20 20 2a 2f 0a 20 20 20 20   pages.  */.    
182e0 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 20 3d 20  MemPage *pNew = 
182e1 61 70 4e 65 77 5b 30 5d 3b 0a 20 20 20 20 4d 65  apNew[0];.    Me
182e2 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d 20 61 70  mPage *pOld = ap
182e3 43 6f 70 79 5b 30 5d 3b 0a 20 20 20 20 69 6e 74  Copy[0];.    int
182e4 20 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 70 4f 6c   nOverflow = pOl
182e5 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20  d->nOverflow;.  
182e6 20 20 69 6e 74 20 69 4e 65 78 74 4f 6c 64 20 3d    int iNextOld =
182e7 20 70 4f 6c 64 2d 3e 6e 43 65 6c 6c 20 2b 20 6e   pOld->nCell + n
182e8 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 20 20 69 6e  Overflow;.    in
182e9 74 20 69 4f 76 65 72 66 6c 6f 77 20 3d 20 28 6e  t iOverflow = (n
182ea 4f 76 65 72 66 6c 6f 77 20 3f 20 70 4f 6c 64 2d  Overflow ? pOld-
182eb 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 20 3a 20  >aOvfl[0].idx : 
182ec 2d 31 29 3b 0a 20 20 20 20 6a 20 3d 20 30 3b 20  -1);.    j = 0; 
182ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
182ee 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
182ef 75 72 72 65 6e 74 20 27 6f 6c 64 27 20 73 69 62  urrent 'old' sib
182f0 6c 69 6e 67 20 70 61 67 65 20 2a 2f 0a 20 20 20  ling page */.   
182f1 20 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20   k = 0;         
182f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
182f3 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 27      /* Current '
182f4 6e 65 77 27 20 73 69 62 6c 69 6e 67 20 70 61 67  new' sibling pag
182f5 65 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30  e */.    for(i=0
182f6 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b  ; i<nCell; i++){
182f7 0a 20 20 20 20 20 20 69 6e 74 20 69 73 44 69 76  .      int isDiv
182f8 69 64 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20  ider = 0;.      
182f9 77 68 69 6c 65 28 20 69 3d 3d 69 4e 65 78 74 4f  while( i==iNextO
182fa 6c 64 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  ld ){.        /*
182fb 20 43 65 6c 6c 20 69 20 69 73 20 74 68 65 20 63   Cell i is the c
182fc 65 6c 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  ell immediately 
182fd 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6c 61  following the la
182fe 73 74 20 63 65 6c 6c 20 6f 6e 20 6f 6c 64 0a 20  st cell on old. 
182ff 20 20 20 20 20 20 20 2a 2a 20 73 69 62 6c 69 6e         ** siblin
18300 67 20 70 61 67 65 20 6a 2e 20 49 66 20 74 68 65  g page j. If the
18301 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6e 6f   siblings are no
18302 74 20 6c 65 61 66 20 70 61 67 65 73 20 6f 66 20  t leaf pages of 
18303 61 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e  an.        ** in
18304 74 6b 65 79 20 62 2d 74 72 65 65 2c 20 74 68 65  tkey b-tree, the
18305 6e 20 63 65 6c 6c 20 69 20 77 61 73 20 61 20 64  n cell i was a d
18306 69 76 69 64 65 72 20 63 65 6c 6c 2e 20 2a 2f 0a  ivider cell. */.
18307 20 20 20 20 20 20 20 20 70 4f 6c 64 20 3d 20 61          pOld = a
18308 70 43 6f 70 79 5b 2b 2b 6a 5d 3b 0a 20 20 20 20  pCopy[++j];.    
18309 20 20 20 20 69 4e 65 78 74 4f 6c 64 20 3d 20 69      iNextOld = i
1830a 20 2b 20 21 6c 65 61 66 44 61 74 61 20 2b 20 70   + !leafData + p
1830b 4f 6c 64 2d 3e 6e 43 65 6c 6c 20 2b 20 70 4f 6c  Old->nCell + pOl
1830c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20  d->nOverflow;.  
1830d 20 20 20 20 20 20 69 66 28 20 70 4f 6c 64 2d 3e        if( pOld->
1830e 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20  nOverflow ){.   
1830f 20 20 20 20 20 20 20 6e 4f 76 65 72 66 6c 6f 77         nOverflow
18310 20 3d 20 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c   = pOld->nOverfl
18311 6f 77 3b 0a 20 20 20 20 20 20 20 20 20 20 69 4f  ow;.          iO
18312 76 65 72 66 6c 6f 77 20 3d 20 69 20 2b 20 21 6c  verflow = i + !l
18313 65 61 66 44 61 74 61 20 2b 20 70 4f 6c 64 2d 3e  eafData + pOld->
18314 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3b 0a 20 20  aOvfl[0].idx;.  
18315 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
18316 69 73 44 69 76 69 64 65 72 20 3d 20 21 6c 65 61  isDivider = !lea
18317 66 44 61 74 61 3b 20 20 0a 20 20 20 20 20 20 7d  fData;  .      }
18318 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 6e  ..      assert(n
18319 4f 76 65 72 66 6c 6f 77 3e 30 20 7c 7c 20 69 4f  Overflow>0 || iO
1831a 76 65 72 66 6c 6f 77 3c 69 20 29 3b 0a 20 20 20  verflow<i );.   
1831b 20 20 20 61 73 73 65 72 74 28 6e 4f 76 65 72 66     assert(nOverf
1831c 6c 6f 77 3c 32 20 7c 7c 20 70 4f 6c 64 2d 3e 61  low<2 || pOld->a
1831d 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d 3d 70 4f 6c  Ovfl[0].idx==pOl
1831e 64 2d 3e 61 4f 76 66 6c 5b 31 5d 2e 69 64 78 2d  d->aOvfl[1].idx-
1831f 31 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  1);.      assert
18320 28 6e 4f 76 65 72 66 6c 6f 77 3c 33 20 7c 7c 20  (nOverflow<3 || 
18321 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b 31 5d 2e 69  pOld->aOvfl[1].i
18322 64 78 3d 3d 70 4f 6c 64 2d 3e 61 4f 76 66 6c 5b  dx==pOld->aOvfl[
18323 32 5d 2e 69 64 78 2d 31 29 3b 0a 20 20 20 20 20  2].idx-1);.     
18324 20 69 66 28 20 69 3d 3d 69 4f 76 65 72 66 6c 6f   if( i==iOverflo
18325 77 20 29 7b 0a 20 20 20 20 20 20 20 20 69 73 44  w ){.        isD
18326 69 76 69 64 65 72 20 3d 20 31 3b 0a 20 20 20 20  ivider = 1;.    
18327 20 20 20 20 69 66 28 20 28 2d 2d 6e 4f 76 65 72      if( (--nOver
18328 66 6c 6f 77 29 3e 30 20 29 7b 0a 20 20 20 20 20  flow)>0 ){.     
18329 20 20 20 20 20 69 4f 76 65 72 66 6c 6f 77 2b 2b       iOverflow++
1832a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1832b 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 69    }..      if( i
1832c 3d 3d 63 6e 74 4e 65 77 5b 6b 5d 20 29 7b 0a 20  ==cntNew[k] ){. 
1832d 20 20 20 20 20 20 20 2f 2a 20 43 65 6c 6c 20 69         /* Cell i
1832e 20 69 73 20 74 68 65 20 63 65 6c 6c 20 69 6d 6d   is the cell imm
1832f 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69  ediately followi
18330 6e 67 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c  ng the last cell
18331 20 6f 6e 20 6e 65 77 0a 20 20 20 20 20 20 20 20   on new.        
18332 2a 2a 20 73 69 62 6c 69 6e 67 20 70 61 67 65 20  ** sibling page 
18333 6b 2e 20 49 66 20 74 68 65 20 73 69 62 6c 69 6e  k. If the siblin
18334 67 73 20 61 72 65 20 6e 6f 74 20 6c 65 61 66 20  gs are not leaf 
18335 70 61 67 65 73 20 6f 66 20 61 6e 0a 20 20 20 20  pages of an.    
18336 20 20 20 20 2a 2a 20 69 6e 74 6b 65 79 20 62 2d      ** intkey b-
18337 74 72 65 65 2c 20 74 68 65 6e 20 63 65 6c 6c 20  tree, then cell 
18338 69 20 69 73 20 61 20 64 69 76 69 64 65 72 20 63  i is a divider c
18339 65 6c 6c 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20  ell.  */.       
1833a 20 70 4e 65 77 20 3d 20 61 70 4e 65 77 5b 2b 2b   pNew = apNew[++
1833b 6b 5d 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  k];.        if( 
1833c 21 6c 65 61 66 44 61 74 61 20 29 20 63 6f 6e 74  !leafData ) cont
1833d 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  inue;.      }.  
1833e 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 6e 4f      assert( j<nO
1833f 6c 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ld );.      asse
18340 72 74 28 20 6b 3c 6e 4e 65 77 20 29 3b 0a 0a 20  rt( k<nNew );.. 
18341 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63       /* If the c
18342 65 6c 6c 20 77 61 73 20 6f 72 69 67 69 6e 61 6c  ell was original
18343 6c 79 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20  ly divider cell 
18344 28 61 6e 64 20 69 73 20 6e 6f 74 20 6e 6f 77 29  (and is not now)
18345 20 6f 72 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20   or.      ** an 
18346 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 2c 20 6f  overflow cell, o
18347 72 20 69 66 20 74 68 65 20 63 65 6c 6c 20 77 61  r if the cell wa
18348 73 20 6c 6f 63 61 74 65 64 20 6f 6e 20 61 20 64  s located on a d
18349 69 66 66 65 72 65 6e 74 20 73 69 62 6c 69 6e 67  ifferent sibling
1834a 0a 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 62  .      ** page b
1834b 65 66 6f 72 65 20 74 68 65 20 62 61 6c 61 6e 63  efore the balanc
1834c 69 6e 67 2c 20 74 68 65 6e 20 74 68 65 20 70 6f  ing, then the po
1834d 69 6e 74 65 72 20 6d 61 70 20 65 6e 74 72 69 65  inter map entrie
1834e 73 20 61 73 73 6f 63 69 61 74 65 64 0a 20 20 20  s associated.   
1834f 20 20 20 2a 2a 20 77 69 74 68 20 61 6e 79 20 63     ** with any c
18350 68 69 6c 64 20 6f 72 20 6f 76 65 72 66 6c 6f 77  hild or overflow
18351 20 70 61 67 65 73 20 6e 65 65 64 20 74 6f 20 62   pages need to b
18352 65 20 75 70 64 61 74 65 64 2e 20 20 2a 2f 0a 20  e updated.  */. 
18353 20 20 20 20 20 69 66 28 20 69 73 44 69 76 69 64       if( isDivid
18354 65 72 20 7c 7c 20 70 4f 6c 64 2d 3e 70 67 6e 6f  er || pOld->pgno
18355 21 3d 70 4e 65 77 2d 3e 70 67 6e 6f 20 29 7b 0a  !=pNew->pgno ){.
18356 20 20 20 20 20 20 20 20 69 66 28 20 21 6c 65 61          if( !lea
18357 66 43 6f 72 72 65 63 74 69 6f 6e 20 29 7b 0a 20  fCorrection ){. 
18358 20 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 50           ptrmapP
18359 75 74 28 70 42 74 2c 20 67 65 74 34 62 79 74 65  ut(pBt, get4byte
1835a 28 61 70 43 65 6c 6c 5b 69 5d 29 2c 20 50 54 52  (apCell[i]), PTR
1835b 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e 65 77 2d  MAP_BTREE, pNew-
1835c 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20 20 20  >pgno, &rc);.   
1835d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
1835e 66 28 20 73 7a 43 65 6c 6c 5b 69 5d 3e 70 4e 65  f( szCell[i]>pNe
1835f 77 2d 3e 6d 69 6e 4c 6f 63 61 6c 20 29 7b 0a 20  w->minLocal ){. 
18360 20 20 20 20 20 20 20 20 20 70 74 72 6d 61 70 50           ptrmapP
18361 75 74 4f 76 66 6c 50 74 72 28 70 4e 65 77 2c 20  utOvflPtr(pNew, 
18362 61 70 43 65 6c 6c 5b 69 5d 2c 20 26 72 63 29 3b  apCell[i], &rc);
18363 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
18364 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66   }.    }..    if
18365 28 20 21 6c 65 61 66 43 6f 72 72 65 63 74 69 6f  ( !leafCorrectio
18366 6e 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69  n ){.      for(i
18367 3d 30 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29  =0; i<nNew; i++)
18368 7b 0a 20 20 20 20 20 20 20 20 75 33 32 20 6b 65  {.        u32 ke
18369 79 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 70  y = get4byte(&ap
1836a 4e 65 77 5b 69 5d 2d 3e 61 44 61 74 61 5b 38 5d  New[i]->aData[8]
1836b 29 3b 0a 20 20 20 20 20 20 20 20 70 74 72 6d 61  );.        ptrma
1836c 70 50 75 74 28 70 42 74 2c 20 6b 65 79 2c 20 50  pPut(pBt, key, P
1836d 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 61 70 4e  TRMAP_BTREE, apN
1836e 65 77 5b 69 5d 2d 3e 70 67 6e 6f 2c 20 26 72 63  ew[i]->pgno, &rc
1836f 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
18370 0a 0a 23 69 66 20 30 0a 20 20 20 20 2f 2a 20 54  ..#if 0.    /* T
18371 68 65 20 70 74 72 6d 61 70 43 68 65 63 6b 50 61  he ptrmapCheckPa
18372 67 65 73 28 29 20 63 6f 6e 74 61 69 6e 73 20 61  ges() contains a
18373 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e  ssert() statemen
18374 74 73 20 74 68 61 74 20 76 65 72 69 66 79 20 74  ts that verify t
18375 68 61 74 0a 20 20 20 20 2a 2a 20 61 6c 6c 20 70  hat.    ** all p
18376 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 73  ointer map pages
18377 20 61 72 65 20 73 65 74 20 63 6f 72 72 65 63 74   are set correct
18378 6c 79 2e 20 54 68 69 73 20 69 73 20 68 65 6c 70  ly. This is help
18379 66 75 6c 20 77 68 69 6c 65 20 0a 20 20 20 20 2a  ful while .    *
1837a 2a 20 64 65 62 75 67 67 69 6e 67 2e 20 54 68 69  * debugging. Thi
1837b 73 20 69 73 20 75 73 75 61 6c 6c 79 20 64 69 73  s is usually dis
1837c 61 62 6c 65 64 20 62 65 63 61 75 73 65 20 61 20  abled because a 
1837d 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65  corrupt database
1837e 20 6d 61 79 0a 20 20 20 20 2a 2a 20 63 61 75 73   may.    ** caus
1837f 65 20 61 6e 20 61 73 73 65 72 74 28 29 20 73 74  e an assert() st
18380 61 74 65 6d 65 6e 74 20 74 6f 20 66 61 69 6c 2e  atement to fail.
18381 20 20 2a 2f 0a 20 20 20 20 70 74 72 6d 61 70 43    */.    ptrmapC
18382 68 65 63 6b 50 61 67 65 73 28 61 70 4e 65 77 2c  heckPages(apNew,
18383 20 6e 4e 65 77 29 3b 0a 20 20 20 20 70 74 72 6d   nNew);.    ptrm
18384 61 70 43 68 65 63 6b 50 61 67 65 73 28 26 70 50  apCheckPages(&pP
18385 61 72 65 6e 74 2c 20 31 29 3b 0a 23 65 6e 64 69  arent, 1);.#endi
18386 66 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  f.  }..  assert(
18387 20 70 50 61 72 65 6e 74 2d 3e 69 73 49 6e 69 74   pParent->isInit
18388 20 29 3b 0a 20 20 54 52 41 43 45 28 28 22 42 41   );.  TRACE(("BA
18389 4c 41 4e 43 45 3a 20 66 69 6e 69 73 68 65 64 3a  LANCE: finished:
1838a 20 6f 6c 64 3d 25 64 20 6e 65 77 3d 25 64 20 63   old=%d new=%d c
1838b 65 6c 6c 73 3d 25 64 5c 6e 22 2c 0a 20 20 20 20  ells=%d\n",.    
1838c 20 20 20 20 20 20 6e 4f 6c 64 2c 20 6e 4e 65 77        nOld, nNew
1838d 2c 20 6e 43 65 6c 6c 29 29 3b 0a 0a 20 20 2f 2a  , nCell));..  /*
1838e 0a 20 20 2a 2a 20 43 6c 65 61 6e 75 70 20 62 65  .  ** Cleanup be
1838f 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a  fore returning..
18390 20 20 2a 2f 0a 62 61 6c 61 6e 63 65 5f 63 6c 65    */.balance_cle
18391 61 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 53  anup:.  sqlite3S
18392 63 72 61 74 63 68 46 72 65 65 28 61 70 43 65 6c  cratchFree(apCel
18393 6c 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  l);.  for(i=0; i
18394 3c 6e 4f 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nOld; i++){.   
18395 20 72 65 6c 65 61 73 65 50 61 67 65 28 61 70 4f   releasePage(apO
18396 6c 64 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 66 6f  ld[i]);.  }.  fo
18397 72 28 69 3d 30 3b 20 69 3c 6e 4e 65 77 3b 20 69  r(i=0; i<nNew; i
18398 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65  ++){.    release
18399 50 61 67 65 28 61 70 4e 65 77 5b 69 5d 29 3b 0a  Page(apNew[i]);.
1839a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
1839b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ;.}.../*.** This
1839c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
1839d 6c 65 64 20 77 68 65 6e 20 74 68 65 20 72 6f 6f  led when the roo
1839e 74 20 70 61 67 65 20 6f 66 20 61 20 62 2d 74 72  t page of a b-tr
1839f 65 65 20 73 74 72 75 63 74 75 72 65 20 69 73 0a  ee structure is.
183a0 2a 2a 20 6f 76 65 72 66 75 6c 6c 20 28 68 61 73  ** overfull (has
183a1 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 76 65   one or more ove
183a2 72 66 6c 6f 77 20 70 61 67 65 73 29 2e 0a 2a 2a  rflow pages)..**
183a3 0a 2a 2a 20 41 20 6e 65 77 20 63 68 69 6c 64 20  .** A new child 
183a4 70 61 67 65 20 69 73 20 61 6c 6c 6f 63 61 74 65  page is allocate
183a5 64 20 61 6e 64 20 74 68 65 20 63 6f 6e 74 65 6e  d and the conten
183a6 74 73 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e  ts of the curren
183a7 74 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 2c 20  t root.** page, 
183a8 69 6e 63 6c 75 64 69 6e 67 20 6f 76 65 72 66 6c  including overfl
183a9 6f 77 20 63 65 6c 6c 73 2c 20 61 72 65 20 63 6f  ow cells, are co
183aa 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 63 68  pied into the ch
183ab 69 6c 64 2e 20 54 68 65 20 72 6f 6f 74 0a 2a 2a  ild. The root.**
183ac 20 70 61 67 65 20 69 73 20 74 68 65 6e 20 6f 76   page is then ov
183ad 65 72 77 72 69 74 74 65 6e 20 74 6f 20 6d 61 6b  erwritten to mak
183ae 65 20 69 74 20 61 6e 20 65 6d 70 74 79 20 70 61  e it an empty pa
183af 67 65 20 77 69 74 68 20 74 68 65 20 72 69 67 68  ge with the righ
183b0 74 2d 63 68 69 6c 64 20 0a 2a 2a 20 70 6f 69 6e  t-child .** poin
183b1 74 65 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  ter pointing to 
183b2 74 68 65 20 6e 65 77 20 70 61 67 65 2e 0a 2a 2a  the new page..**
183b3 0a 2a 2a 20 42 65 66 6f 72 65 20 72 65 74 75 72  .** Before retur
183b4 6e 69 6e 67 2c 20 61 6c 6c 20 70 6f 69 6e 74 65  ning, all pointe
183b5 72 2d 6d 61 70 20 65 6e 74 72 69 65 73 20 63 6f  r-map entries co
183b6 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 70  rresponding to p
183b7 61 67 65 73 20 0a 2a 2a 20 74 68 61 74 20 74 68  ages .** that th
183b8 65 20 6e 65 77 20 63 68 69 6c 64 2d 70 61 67 65  e new child-page
183b9 20 6e 6f 77 20 63 6f 6e 74 61 69 6e 73 20 70 6f   now contains po
183ba 69 6e 74 65 72 73 20 74 6f 20 61 72 65 20 75 70  inters to are up
183bb 64 61 74 65 64 2e 20 54 68 65 0a 2a 2a 20 65 6e  dated. The.** en
183bc 74 72 79 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  try correspondin
183bd 67 20 74 6f 20 74 68 65 20 6e 65 77 20 72 69 67  g to the new rig
183be 68 74 2d 63 68 69 6c 64 20 70 6f 69 6e 74 65 72  ht-child pointer
183bf 20 6f 66 20 74 68 65 20 72 6f 6f 74 0a 2a 2a 20   of the root.** 
183c0 70 61 67 65 20 69 73 20 61 6c 73 6f 20 75 70 64  page is also upd
183c1 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73  ated..**.** If s
183c2 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 70 43 68  uccessful, *ppCh
183c3 69 6c 64 20 69 73 20 73 65 74 20 74 6f 20 63 6f  ild is set to co
183c4 6e 74 61 69 6e 20 61 20 72 65 66 65 72 65 6e 63  ntain a referenc
183c5 65 20 74 6f 20 74 68 65 20 63 68 69 6c 64 20 0a  e to the child .
183c6 2a 2a 20 70 61 67 65 20 61 6e 64 20 53 51 4c 49  ** page and SQLI
183c7 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65  TE_OK is returne
183c8 64 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  d. In this case 
183c9 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20 72 65  the caller is re
183ca 71 75 69 72 65 64 0a 2a 2a 20 74 6f 20 63 61 6c  quired.** to cal
183cb 6c 20 72 65 6c 65 61 73 65 50 61 67 65 28 29 20  l releasePage() 
183cc 6f 6e 20 2a 70 70 43 68 69 6c 64 20 65 78 61 63  on *ppChild exac
183cd 74 6c 79 20 6f 6e 63 65 2e 20 49 66 20 61 6e 20  tly once. If an 
183ce 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a  error occurs,.**
183cf 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   an error code i
183d0 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a  s returned and *
183d1 70 70 43 68 69 6c 64 20 69 73 20 73 65 74 20 74  ppChild is set t
183d2 6f 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  o 0..*/.static i
183d3 6e 74 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65  nt balance_deepe
183d4 72 28 4d 65 6d 50 61 67 65 20 2a 70 52 6f 6f 74  r(MemPage *pRoot
183d5 2c 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 43 68  , MemPage **ppCh
183d6 69 6c 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 20  ild){.  int rc; 
183d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
183d8 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
183d9 20 76 61 6c 75 65 20 66 72 6f 6d 20 73 75 62 70   value from subp
183da 72 6f 63 65 64 75 72 65 73 20 2a 2f 0a 20 20 4d  rocedures */.  M
183db 65 6d 50 61 67 65 20 2a 70 43 68 69 6c 64 20 3d  emPage *pChild =
183dc 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a   0;           /*
183dd 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20 6e 65   Pointer to a ne
183de 77 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0a  w child page */.
183df 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 6c 64    Pgno pgnoChild
183e0 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
183e1 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20   /* Page number 
183e2 6f 66 20 74 68 65 20 6e 65 77 20 63 68 69 6c 64  of the new child
183e3 20 70 61 67 65 20 2a 2f 0a 20 20 42 74 53 68 61   page */.  BtSha
183e4 72 65 64 20 2a 70 42 74 20 3d 20 70 52 6f 6f 74  red *pBt = pRoot
183e5 2d 3e 70 42 74 3b 20 20 20 20 2f 2a 20 54 68 65  ->pBt;    /* The
183e6 20 42 54 72 65 65 20 2a 2f 0a 0a 20 20 61 73 73   BTree */..  ass
183e7 65 72 74 28 20 70 52 6f 6f 74 2d 3e 6e 4f 76 65  ert( pRoot->nOve
183e8 72 66 6c 6f 77 3e 30 20 29 3b 0a 20 20 61 73 73  rflow>0 );.  ass
183e9 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
183ea 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74  ex_held(pBt->mut
183eb 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b  ex) );..  /* Mak
183ec 65 20 70 52 6f 6f 74 2c 20 74 68 65 20 72 6f 6f  e pRoot, the roo
183ed 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 62 2d  t page of the b-
183ee 74 72 65 65 2c 20 77 72 69 74 61 62 6c 65 2e 20  tree, writable. 
183ef 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 0a  Allocate a new .
183f0 20 20 2a 2a 20 70 61 67 65 20 74 68 61 74 20 77    ** page that w
183f1 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20 6e  ill become the n
183f2 65 77 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f  ew right-child o
183f3 66 20 70 50 61 67 65 2e 20 43 6f 70 79 20 74 68  f pPage. Copy th
183f4 65 20 63 6f 6e 74 65 6e 74 73 0a 20 20 2a 2a 20  e contents.  ** 
183f5 6f 66 20 74 68 65 20 6e 6f 64 65 20 73 74 6f 72  of the node stor
183f6 65 64 20 6f 6e 20 70 52 6f 6f 74 20 69 6e 74 6f  ed on pRoot into
183f7 20 74 68 65 20 6e 65 77 20 63 68 69 6c 64 20 70   the new child p
183f8 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d  age..  */.  rc =
183f9 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69   sqlite3PagerWri
183fa 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67  te(pRoot->pDbPag
183fb 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  e);.  if( rc==SQ
183fc 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
183fd 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65  c = allocateBtre
183fe 65 50 61 67 65 28 70 42 74 2c 26 70 43 68 69 6c  ePage(pBt,&pChil
183ff 64 2c 26 70 67 6e 6f 43 68 69 6c 64 2c 70 52 6f  d,&pgnoChild,pRo
18400 6f 74 2d 3e 70 67 6e 6f 2c 30 29 3b 0a 20 20 20  ot->pgno,0);.   
18401 20 63 6f 70 79 4e 6f 64 65 43 6f 6e 74 65 6e 74   copyNodeContent
18402 28 70 52 6f 6f 74 2c 20 70 43 68 69 6c 64 2c 20  (pRoot, pChild, 
18403 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20 49 53  &rc);.    if( IS
18404 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20  AUTOVACUUM ){.  
18405 20 20 20 20 70 74 72 6d 61 70 50 75 74 28 70 42      ptrmapPut(pB
18406 74 2c 20 70 67 6e 6f 43 68 69 6c 64 2c 20 50 54  t, pgnoChild, PT
18407 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 52 6f 6f  RMAP_BTREE, pRoo
18408 74 2d 3e 70 67 6e 6f 2c 20 26 72 63 29 3b 0a 20  t->pgno, &rc);. 
18409 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 72     }.  }.  if( r
1840a 63 20 29 7b 0a 20 20 20 20 2a 70 70 43 68 69 6c  c ){.    *ppChil
1840b 64 20 3d 20 30 3b 0a 20 20 20 20 72 65 6c 65 61  d = 0;.    relea
1840c 73 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a  sePage(pChild);.
1840d 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1840e 20 7d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c   }.  assert( sql
1840f 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74 65  ite3PagerIswrite
18410 61 62 6c 65 28 70 43 68 69 6c 64 2d 3e 70 44 62  able(pChild->pDb
18411 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72  Page) );.  asser
18412 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49  t( sqlite3PagerI
18413 73 77 72 69 74 65 61 62 6c 65 28 70 52 6f 6f 74  swriteable(pRoot
18414 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20  ->pDbPage) );.  
18415 61 73 73 65 72 74 28 20 70 43 68 69 6c 64 2d 3e  assert( pChild->
18416 6e 43 65 6c 6c 3d 3d 70 52 6f 6f 74 2d 3e 6e 43  nCell==pRoot->nC
18417 65 6c 6c 20 29 3b 0a 0a 20 20 54 52 41 43 45 28  ell );..  TRACE(
18418 28 22 42 41 4c 41 4e 43 45 3a 20 63 6f 70 79 20  ("BALANCE: copy 
18419 72 6f 6f 74 20 25 64 20 69 6e 74 6f 20 25 64 5c  root %d into %d\
1841a 6e 22 2c 20 70 52 6f 6f 74 2d 3e 70 67 6e 6f 2c  n", pRoot->pgno,
1841b 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b   pChild->pgno));
1841c 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 68 65 20  ..  /* Copy the 
1841d 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 73 20 66  overflow cells f
1841e 72 6f 6d 20 70 52 6f 6f 74 20 74 6f 20 70 43 68  rom pRoot to pCh
1841f 69 6c 64 20 2a 2f 0a 20 20 6d 65 6d 63 70 79 28  ild */.  memcpy(
18420 70 43 68 69 6c 64 2d 3e 61 4f 76 66 6c 2c 20 70  pChild->aOvfl, p
18421 52 6f 6f 74 2d 3e 61 4f 76 66 6c 2c 20 70 52 6f  Root->aOvfl, pRo
18422 6f 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2a 73 69  ot->nOverflow*si
18423 7a 65 6f 66 28 70 52 6f 6f 74 2d 3e 61 4f 76 66  zeof(pRoot->aOvf
18424 6c 5b 30 5d 29 29 3b 0a 20 20 70 43 68 69 6c 64  l[0]));.  pChild
18425 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 70 52  ->nOverflow = pR
18426 6f 6f 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a  oot->nOverflow;.
18427 0a 20 20 2f 2a 20 5a 65 72 6f 20 74 68 65 20 63  .  /* Zero the c
18428 6f 6e 74 65 6e 74 73 20 6f 66 20 70 52 6f 6f 74  ontents of pRoot
18429 2e 20 54 68 65 6e 20 69 6e 73 74 61 6c 6c 20 70  . Then install p
1842a 43 68 69 6c 64 20 61 73 20 74 68 65 20 72 69 67  Child as the rig
1842b 68 74 2d 63 68 69 6c 64 2e 20 2a 2f 0a 20 20 7a  ht-child. */.  z
1842c 65 72 6f 50 61 67 65 28 70 52 6f 6f 74 2c 20 70  eroPage(pRoot, p
1842d 43 68 69 6c 64 2d 3e 61 44 61 74 61 5b 30 5d 20  Child->aData[0] 
1842e 26 20 7e 50 54 46 5f 4c 45 41 46 29 3b 0a 20 20  & ~PTF_LEAF);.  
1842f 70 75 74 34 62 79 74 65 28 26 70 52 6f 6f 74 2d  put4byte(&pRoot-
18430 3e 61 44 61 74 61 5b 70 52 6f 6f 74 2d 3e 68 64  >aData[pRoot->hd
18431 72 4f 66 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f  rOffset+8], pgno
18432 43 68 69 6c 64 29 3b 0a 0a 20 20 2a 70 70 43 68  Child);..  *ppCh
18433 69 6c 64 20 3d 20 70 43 68 69 6c 64 3b 0a 20 20  ild = pChild;.  
18434 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
18435 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70  ;.}../*.** The p
18436 61 67 65 20 74 68 61 74 20 70 43 75 72 20 63 75  age that pCur cu
18437 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 73 20 74  rrently points t
18438 6f 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20  o has just been 
18439 6d 6f 64 69 66 69 65 64 20 69 6e 0a 2a 2a 20 73  modified in.** s
1843a 6f 6d 65 20 77 61 79 2e 20 54 68 69 73 20 66 75  ome way. This fu
1843b 6e 63 74 69 6f 6e 20 66 69 67 75 72 65 73 20 6f  nction figures o
1843c 75 74 20 69 66 20 74 68 69 73 20 6d 6f 64 69 66  ut if this modif
1843d 69 63 61 74 69 6f 6e 20 6d 65 61 6e 73 20 74 68  ication means th
1843e 65 0a 2a 2a 20 74 72 65 65 20 6e 65 65 64 73 20  e.** tree needs 
1843f 74 6f 20 62 65 20 62 61 6c 61 6e 63 65 64 2c 20  to be balanced, 
18440 61 6e 64 20 69 66 20 73 6f 20 63 61 6c 6c 73 20  and if so calls 
18441 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
18442 62 61 6c 61 6e 63 69 6e 67 20 0a 2a 2a 20 72 6f  balancing .** ro
18443 75 74 69 6e 65 2e 20 42 61 6c 61 6e 63 69 6e 67  utine. Balancing
18444 20 72 6f 75 74 69 6e 65 73 20 61 72 65 3a 0a 2a   routines are:.*
18445 2a 0a 2a 2a 20 20 20 62 61 6c 61 6e 63 65 5f 71  *.**   balance_q
18446 75 69 63 6b 28 29 0a 2a 2a 20 20 20 62 61 6c 61  uick().**   bala
18447 6e 63 65 5f 64 65 65 70 65 72 28 29 0a 2a 2a 20  nce_deeper().** 
18448 20 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f    balance_nonroo
18449 74 28 29 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  t().*/.static in
1844a 74 20 62 61 6c 61 6e 63 65 28 42 74 43 75 72 73  t balance(BtCurs
1844b 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74  or *pCur){.  int
1844c 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1844d 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 4d 69  .  const int nMi
1844e 6e 20 3d 20 70 43 75 72 2d 3e 70 42 74 2d 3e 75  n = pCur->pBt->u
1844f 73 61 62 6c 65 53 69 7a 65 20 2a 20 32 20 2f 20  sableSize * 2 / 
18450 33 3b 0a 20 20 75 38 20 61 42 61 6c 61 6e 63 65  3;.  u8 aBalance
18451 51 75 69 63 6b 53 70 61 63 65 5b 31 33 5d 3b 0a  QuickSpace[13];.
18452 20 20 75 38 20 2a 70 46 72 65 65 20 3d 20 30 3b    u8 *pFree = 0;
18453 0a 0a 20 20 54 45 53 54 4f 4e 4c 59 28 20 69 6e  ..  TESTONLY( in
18454 74 20 62 61 6c 61 6e 63 65 5f 71 75 69 63 6b 5f  t balance_quick_
18455 63 61 6c 6c 65 64 20 3d 20 30 20 29 3b 0a 20 20  called = 0 );.  
18456 54 45 53 54 4f 4e 4c 59 28 20 69 6e 74 20 62 61  TESTONLY( int ba
18457 6c 61 6e 63 65 5f 64 65 65 70 65 72 5f 63 61 6c  lance_deeper_cal
18458 6c 65 64 20 3d 20 30 20 29 3b 0a 0a 20 20 64 6f  led = 0 );..  do
18459 20 7b 0a 20 20 20 20 69 6e 74 20 69 50 61 67 65   {.    int iPage
1845a 20 3d 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a   = pCur->iPage;.
1845b 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61      MemPage *pPa
1845c 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  ge = pCur->apPag
1845d 65 5b 69 50 61 67 65 5d 3b 0a 0a 20 20 20 20 69  e[iPage];..    i
1845e 66 28 20 69 50 61 67 65 3d 3d 30 20 29 7b 0a 20  f( iPage==0 ){. 
1845f 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e       if( pPage->
18460 6e 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20  nOverflow ){.   
18461 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74       /* The root
18462 20 70 61 67 65 20 6f 66 20 74 68 65 20 62 2d 74   page of the b-t
18463 72 65 65 20 69 73 20 6f 76 65 72 66 75 6c 6c 2e  ree is overfull.
18464 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 63 61   In this case ca
18465 6c 6c 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  ll the.        *
18466 2a 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72  * balance_deeper
18467 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63  () function to c
18468 72 65 61 74 65 20 61 20 6e 65 77 20 63 68 69 6c  reate a new chil
18469 64 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70  d for the root-p
1846a 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  age.        ** a
1846b 6e 64 20 63 6f 70 79 20 74 68 65 20 63 75 72 72  nd copy the curr
1846c 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  ent contents of 
1846d 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 74 6f  the root-page to
1846e 20 69 74 2e 20 54 68 65 0a 20 20 20 20 20 20 20   it. The.       
1846f 20 2a 2a 20 6e 65 78 74 20 69 74 65 72 61 74 69   ** next iterati
18470 6f 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f  on of the do-loo
18471 70 20 77 69 6c 6c 20 62 61 6c 61 6e 63 65 20 74  p will balance t
18472 68 65 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 20  he child page.. 
18473 20 20 20 20 20 20 20 2a 2f 20 0a 20 20 20 20 20         */ .     
18474 20 20 20 61 73 73 65 72 74 28 20 28 62 61 6c 61     assert( (bala
18475 6e 63 65 5f 64 65 65 70 65 72 5f 63 61 6c 6c 65  nce_deeper_calle
18476 64 2b 2b 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20  d++)==0 );.     
18477 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f     rc = balance_
18478 64 65 65 70 65 72 28 70 50 61 67 65 2c 20 26 70  deeper(pPage, &p
18479 43 75 72 2d 3e 61 70 50 61 67 65 5b 31 5d 29 3b  Cur->apPage[1]);
1847a 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
1847b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1847c 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 69 50          pCur->iP
1847d 61 67 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  age = 1;.       
1847e 20 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 30     pCur->aiIdx[0
1847f 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  ] = 0;.         
18480 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 31 5d 20   pCur->aiIdx[1] 
18481 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 61  = 0;.          a
18482 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50  ssert( pCur->apP
18483 61 67 65 5b 31 5d 2d 3e 6e 4f 76 65 72 66 6c 6f  age[1]->nOverflo
18484 77 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  w );.        }. 
18485 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
18486 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
18487 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
18488 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f   pPage->nOverflo
18489 77 3d 3d 30 20 26 26 20 70 50 61 67 65 2d 3e 6e  w==0 && pPage->n
1848a 46 72 65 65 3c 3d 6e 4d 69 6e 20 29 7b 0a 20 20  Free<=nMin ){.  
1848b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1848c 65 6c 73 65 7b 0a 20 20 20 20 20 20 4d 65 6d 50  else{.      MemP
1848d 61 67 65 20 2a 20 63 6f 6e 73 74 20 70 50 61 72  age * const pPar
1848e 65 6e 74 20 3d 20 70 43 75 72 2d 3e 61 70 50 61  ent = pCur->apPa
1848f 67 65 5b 69 50 61 67 65 2d 31 5d 3b 0a 20 20 20  ge[iPage-1];.   
18490 20 20 20 69 6e 74 20 63 6f 6e 73 74 20 69 49 64     int const iId
18491 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  x = pCur->aiIdx[
18492 69 50 61 67 65 2d 31 5d 3b 0a 0a 20 20 20 20 20  iPage-1];..     
18493 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
18494 65 72 57 72 69 74 65 28 70 50 61 72 65 6e 74 2d  erWrite(pParent-
18495 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20  >pDbPage);.     
18496 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
18497 4f 4b 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51  OK ){.#ifndef SQ
18498 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42  LITE_OMIT_QUICKB
18499 41 4c 41 4e 43 45 0a 20 20 20 20 20 20 20 20 69  ALANCE.        i
1849a 66 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74  f( pPage->hasDat
1849b 61 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 50  a.         && pP
1849c 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d  age->nOverflow==
1849d 31 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 50  1.         && pP
1849e 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64  age->aOvfl[0].id
1849f 78 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 0a  x==pPage->nCell.
184a0 20 20 20 20 20 20 20 20 20 26 26 20 70 50 61 72           && pPar
184a1 65 6e 74 2d 3e 70 67 6e 6f 21 3d 31 0a 20 20 20  ent->pgno!=1.   
184a2 20 20 20 20 20 20 26 26 20 70 50 61 72 65 6e 74        && pParent
184a3 2d 3e 6e 43 65 6c 6c 3d 3d 69 49 64 78 0a 20 20  ->nCell==iIdx.  
184a4 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
184a5 20 20 20 2f 2a 20 43 61 6c 6c 20 62 61 6c 61 6e     /* Call balan
184a6 63 65 5f 71 75 69 63 6b 28 29 20 74 6f 20 63 72  ce_quick() to cr
184a7 65 61 74 65 20 61 20 6e 65 77 20 73 69 62 6c 69  eate a new sibli
184a8 6e 67 20 6f 66 20 70 50 61 67 65 20 6f 6e 20 77  ng of pPage on w
184a9 68 69 63 68 0a 20 20 20 20 20 20 20 20 20 20 2a  hich.          *
184aa 2a 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 6f  * to store the o
184ab 76 65 72 66 6c 6f 77 20 63 65 6c 6c 2e 20 62 61  verflow cell. ba
184ac 6c 61 6e 63 65 5f 71 75 69 63 6b 28 29 20 69 6e  lance_quick() in
184ad 73 65 72 74 73 20 61 20 6e 65 77 20 63 65 6c 6c  serts a new cell
184ae 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e  .          ** in
184af 74 6f 20 70 50 61 72 65 6e 74 2c 20 77 68 69 63  to pParent, whic
184b0 68 20 6d 61 79 20 63 61 75 73 65 20 70 50 61 72  h may cause pPar
184b1 65 6e 74 20 6f 76 65 72 66 6c 6f 77 2e 20 49 66  ent overflow. If
184b2 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 20 20   this.          
184b3 2a 2a 20 68 61 70 70 65 6e 73 2c 20 74 68 65 20  ** happens, the 
184b4 6e 65 78 74 20 69 6e 74 65 72 61 74 69 6f 6e 20  next interation 
184b5 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70 20 77  of the do-loop w
184b6 69 6c 6c 20 62 61 6c 61 6e 63 65 20 70 50 61 72  ill balance pPar
184b7 65 6e 74 20 0a 20 20 20 20 20 20 20 20 20 20 2a  ent .          *
184b8 2a 20 75 73 65 20 65 69 74 68 65 72 20 62 61 6c  * use either bal
184b9 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 6f  ance_nonroot() o
184ba 72 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72  r balance_deeper
184bb 28 29 2e 20 55 6e 74 69 6c 20 74 68 69 73 0a 20  (). Until this. 
184bc 20 20 20 20 20 20 20 20 20 2a 2a 20 68 61 70 70           ** happ
184bd 65 6e 73 2c 20 74 68 65 20 6f 76 65 72 66 6c 6f  ens, the overflo
184be 77 20 63 65 6c 6c 20 69 73 20 73 74 6f 72 65 64  w cell is stored
184bf 20 69 6e 20 74 68 65 20 61 42 61 6c 61 6e 63 65   in the aBalance
184c0 51 75 69 63 6b 53 70 61 63 65 5b 5d 0a 20 20 20  QuickSpace[].   
184c1 20 20 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72         ** buffer
184c2 2e 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 0a  . .          **.
184c3 20 20 20 20 20 20 20 20 20 20 2a 2a 20 54 68 65            ** The
184c4 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 65 20   purpose of the 
184c5 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74  following assert
184c6 28 29 20 69 73 20 74 6f 20 63 68 65 63 6b 20 74  () is to check t
184c7 68 61 74 20 6f 6e 6c 79 20 61 0a 20 20 20 20 20  hat only a.     
184c8 20 20 20 20 20 2a 2a 20 73 69 6e 67 6c 65 20 63       ** single c
184c9 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 65 5f 71  all to balance_q
184ca 75 69 63 6b 28 29 20 69 73 20 6d 61 64 65 20 66  uick() is made f
184cb 6f 72 20 65 61 63 68 20 63 61 6c 6c 20 74 6f 20  or each call to 
184cc 74 68 69 73 0a 20 20 20 20 20 20 20 20 20 20 2a  this.          *
184cd 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 20 74  * function. If t
184ce 68 69 73 20 77 65 72 65 20 6e 6f 74 20 76 65 72  his were not ver
184cf 69 66 69 65 64 2c 20 61 20 73 75 62 74 6c 65 20  ified, a subtle 
184d0 62 75 67 20 69 6e 76 6f 6c 76 69 6e 67 20 72 65  bug involving re
184d1 75 73 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  use.          **
184d2 20 6f 66 20 74 68 65 20 61 42 61 6c 61 6e 63 65   of the aBalance
184d3 51 75 69 63 6b 53 70 61 63 65 5b 5d 20 6d 69 67  QuickSpace[] mig
184d4 68 74 20 73 6e 65 61 6b 20 69 6e 2e 0a 20 20 20  ht sneak in..   
184d5 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
184d6 20 20 20 20 61 73 73 65 72 74 28 20 28 62 61 6c      assert( (bal
184d7 61 6e 63 65 5f 71 75 69 63 6b 5f 63 61 6c 6c 65  ance_quick_calle
184d8 64 2b 2b 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20  d++)==0 );.     
184d9 20 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63       rc = balanc
184da 65 5f 71 75 69 63 6b 28 70 50 61 72 65 6e 74 2c  e_quick(pParent,
184db 20 70 50 61 67 65 2c 20 61 42 61 6c 61 6e 63 65   pPage, aBalance
184dc 51 75 69 63 6b 53 70 61 63 65 29 3b 0a 20 20 20  QuickSpace);.   
184dd 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69       }else.#endi
184de 66 0a 20 20 20 20 20 20 20 20 7b 0a 20 20 20 20  f.        {.    
184df 20 20 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73        /* In this
184e0 20 63 61 73 65 2c 20 63 61 6c 6c 20 62 61 6c 61   case, call bala
184e1 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 20 74 6f  nce_nonroot() to
184e2 20 72 65 64 69 73 74 72 69 62 75 74 65 20 63 65   redistribute ce
184e3 6c 6c 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  lls.          **
184e4 20 62 65 74 77 65 65 6e 20 70 50 61 67 65 20 61   between pPage a
184e5 6e 64 20 75 70 20 74 6f 20 32 20 6f 66 20 69 74  nd up to 2 of it
184e6 73 20 73 69 62 6c 69 6e 67 20 70 61 67 65 73 2e  s sibling pages.
184e7 20 54 68 69 73 20 69 6e 76 6f 6c 76 65 73 0a 20   This involves. 
184e8 20 20 20 20 20 20 20 20 20 2a 2a 20 6d 6f 64 69           ** modi
184e9 66 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e  fying the conten
184ea 74 73 20 6f 66 20 70 50 61 72 65 6e 74 2c 20 77  ts of pParent, w
184eb 68 69 63 68 20 6d 61 79 20 63 61 75 73 65 20 70  hich may cause p
184ec 50 61 72 65 6e 74 20 74 6f 0a 20 20 20 20 20 20  Parent to.      
184ed 20 20 20 20 2a 2a 20 62 65 63 6f 6d 65 20 6f 76      ** become ov
184ee 65 72 66 75 6c 6c 20 6f 72 20 75 6e 64 65 72 66  erfull or underf
184ef 75 6c 6c 2e 20 54 68 65 20 6e 65 78 74 20 69 74  ull. The next it
184f0 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 64  eration of the d
184f1 6f 2d 6c 6f 6f 70 0a 20 20 20 20 20 20 20 20 20  o-loop.         
184f2 20 2a 2a 20 77 69 6c 6c 20 62 61 6c 61 6e 63 65   ** will balance
184f3 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
184f4 20 74 6f 20 63 6f 72 72 65 63 74 20 74 68 69 73   to correct this
184f5 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 0a  ..          ** .
184f6 20 20 20 20 20 20 20 20 20 20 2a 2a 20 49 66 20            ** If 
184f7 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20  the parent page 
184f8 62 65 63 6f 6d 65 73 20 6f 76 65 72 66 75 6c 6c  becomes overfull
184f9 2c 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63  , the overflow c
184fa 65 6c 6c 20 6f 72 20 63 65 6c 6c 73 0a 20 20 20  ell or cells.   
184fb 20 20 20 20 20 20 20 2a 2a 20 61 72 65 20 73 74         ** are st
184fc 6f 72 65 64 20 69 6e 20 74 68 65 20 70 53 70 61  ored in the pSpa
184fd 63 65 20 62 75 66 66 65 72 20 61 6c 6c 6f 63 61  ce buffer alloca
184fe 74 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  ted immediately 
184ff 62 65 6c 6f 77 2e 20 0a 20 20 20 20 20 20 20 20  below. .        
18500 20 20 2a 2a 20 41 20 73 75 62 73 65 71 75 65 6e    ** A subsequen
18501 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74  t iteration of t
18502 68 65 20 64 6f 2d 6c 6f 6f 70 20 77 69 6c 6c 20  he do-loop will 
18503 64 65 61 6c 20 77 69 74 68 20 74 68 69 73 20 62  deal with this b
18504 79 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63  y.          ** c
18505 61 6c 6c 69 6e 67 20 62 61 6c 61 6e 63 65 5f 6e  alling balance_n
18506 6f 6e 72 6f 6f 74 28 29 20 28 62 61 6c 61 6e 63  onroot() (balanc
18507 65 5f 64 65 65 70 65 72 28 29 20 6d 61 79 20 62  e_deeper() may b
18508 65 20 63 61 6c 6c 65 64 20 66 69 72 73 74 2c 0a  e called first,.
18509 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 75 74            ** but
1850a 20 69 74 20 64 6f 65 73 6e 27 74 20 64 65 61 6c   it doesn't deal
1850b 20 77 69 74 68 20 6f 76 65 72 66 6c 6f 77 20 63   with overflow c
1850c 65 6c 6c 73 20 2d 20 6a 75 73 74 20 6d 6f 76 65  ells - just move
1850d 73 20 74 68 65 6d 20 74 6f 20 61 0a 20 20 20 20  s them to a.    
1850e 20 20 20 20 20 20 2a 2a 20 64 69 66 66 65 72 65        ** differe
1850f 6e 74 20 70 61 67 65 29 2e 20 4f 6e 63 65 20 74  nt page). Once t
18510 68 69 73 20 73 75 62 73 65 71 75 65 6e 74 20 63  his subsequent c
18511 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63 65 5f 6e  all to balance_n
18512 6f 6e 72 6f 6f 74 28 29 20 0a 20 20 20 20 20 20  onroot() .      
18513 20 20 20 20 2a 2a 20 68 61 73 20 63 6f 6d 70 6c      ** has compl
18514 65 74 65 64 2c 20 69 74 20 69 73 20 73 61 66 65  eted, it is safe
18515 20 74 6f 20 72 65 6c 65 61 73 65 20 74 68 65 20   to release the 
18516 70 53 70 61 63 65 20 62 75 66 66 65 72 20 75 73  pSpace buffer us
18517 65 64 20 62 79 0a 20 20 20 20 20 20 20 20 20 20  ed by.          
18518 2a 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  ** the previous 
18519 63 61 6c 6c 2c 20 61 73 20 74 68 65 20 6f 76 65  call, as the ove
1851a 72 66 6c 6f 77 20 63 65 6c 6c 20 64 61 74 61 20  rflow cell data 
1851b 77 69 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 0a  will have been .
1851c 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 70            ** cop
1851d 69 65 64 20 65 69 74 68 65 72 20 69 6e 74 6f 20  ied either into 
1851e 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 64 61  the body of a da
1851f 74 61 62 61 73 65 20 70 61 67 65 20 6f 72 20 69  tabase page or i
18520 6e 74 6f 20 74 68 65 20 6e 65 77 0a 20 20 20 20  nto the new.    
18521 20 20 20 20 20 20 2a 2a 20 70 53 70 61 63 65 20        ** pSpace 
18522 62 75 66 66 65 72 20 70 61 73 73 65 64 20 74 6f  buffer passed to
18523 20 74 68 65 20 6c 61 74 74 65 72 20 63 61 6c 6c   the latter call
18524 20 74 6f 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72   to balance_nonr
18525 6f 6f 74 28 29 2e 0a 20 20 20 20 20 20 20 20 20  oot()..         
18526 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 75 38   */.          u8
18527 20 2a 70 53 70 61 63 65 20 3d 20 73 71 6c 69 74   *pSpace = sqlit
18528 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 70 43 75  e3PageMalloc(pCu
18529 72 2d 3e 70 42 74 2d 3e 70 61 67 65 53 69 7a 65  r->pBt->pageSize
1852a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  );.          rc 
1852b 3d 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f  = balance_nonroo
1852c 74 28 70 50 61 72 65 6e 74 2c 20 69 49 64 78 2c  t(pParent, iIdx,
1852d 20 70 53 70 61 63 65 2c 20 69 50 61 67 65 3d 3d   pSpace, iPage==
1852e 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  1);.          if
1852f 28 20 70 46 72 65 65 20 29 7b 0a 20 20 20 20 20  ( pFree ){.     
18530 20 20 20 20 20 20 20 2f 2a 20 49 66 20 70 46 72         /* If pFr
18531 65 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  ee is not NULL, 
18532 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  it points to the
18533 20 70 53 70 61 63 65 20 62 75 66 66 65 72 20 75   pSpace buffer u
18534 73 65 64 20 0a 20 20 20 20 20 20 20 20 20 20 20  sed .           
18535 20 2a 2a 20 62 79 20 61 20 70 72 65 76 69 6f 75   ** by a previou
18536 73 20 63 61 6c 6c 20 74 6f 20 62 61 6c 61 6e 63  s call to balanc
18537 65 5f 6e 6f 6e 72 6f 6f 74 28 29 2e 20 49 74 73  e_nonroot(). Its
18538 20 63 6f 6e 74 65 6e 74 73 20 61 72 65 0a 20 20   contents are.  
18539 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6e 6f 77            ** now
1853a 20 73 74 6f 72 65 64 20 65 69 74 68 65 72 20 6f   stored either o
1853b 6e 20 72 65 61 6c 20 64 61 74 61 62 61 73 65 20  n real database 
1853c 70 61 67 65 73 20 6f 72 20 77 69 74 68 69 6e 20  pages or within 
1853d 74 68 65 20 0a 20 20 20 20 20 20 20 20 20 20 20  the .           
1853e 20 2a 2a 20 6e 65 77 20 70 53 70 61 63 65 20 62   ** new pSpace b
1853f 75 66 66 65 72 2c 20 73 6f 20 69 74 20 6d 61 79  uffer, so it may
18540 20 62 65 20 73 61 66 65 6c 79 20 66 72 65 65 64   be safely freed
18541 20 68 65 72 65 2e 20 2a 2f 0a 20 20 20 20 20 20   here. */.      
18542 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
18543 65 46 72 65 65 28 70 46 72 65 65 29 3b 0a 20 20  eFree(pFree);.  
18544 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20          }..     
18545 20 20 20 20 20 2f 2a 20 54 68 65 20 70 53 70 61       /* The pSpa
18546 63 65 20 62 75 66 66 65 72 20 77 69 6c 6c 20 62  ce buffer will b
18547 65 20 66 72 65 65 64 20 61 66 74 65 72 20 74 68  e freed after th
18548 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f 0a 20  e next call to. 
18549 20 20 20 20 20 20 20 20 20 2a 2a 20 62 61 6c 61           ** bala
1854a 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 29 2c 20 6f  nce_nonroot(), o
1854b 72 20 6a 75 73 74 20 62 65 66 6f 72 65 20 74 68  r just before th
1854c 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
1854d 72 6e 73 2c 20 77 68 69 63 68 65 76 65 72 0a 20  rns, whichever. 
1854e 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d 65           ** come
1854f 73 20 66 69 72 73 74 2e 20 2a 2f 0a 20 20 20 20  s first. */.    
18550 20 20 20 20 20 20 70 46 72 65 65 20 3d 20 70 53        pFree = pS
18551 70 61 63 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  pace;.        }.
18552 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 70        }..      p
18553 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20  Page->nOverflow 
18554 3d 20 30 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54  = 0;..      /* T
18555 68 65 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f  he next iteratio
18556 6e 20 6f 66 20 74 68 65 20 64 6f 2d 6c 6f 6f 70  n of the do-loop
18557 20 62 61 6c 61 6e 63 65 73 20 74 68 65 20 70 61   balances the pa
18558 72 65 6e 74 20 70 61 67 65 2e 20 2a 2f 0a 20 20  rent page. */.  
18559 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
1855a 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 70 43  pPage);.      pC
1855b 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a 20 20 20  ur->iPage--;.   
1855c 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 72 63 3d   }.  }while( rc=
1855d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 0a 20  =SQLITE_OK );.. 
1855e 20 69 66 28 20 70 46 72 65 65 20 29 7b 0a 20 20   if( pFree ){.  
1855f 20 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65    sqlite3PageFre
18560 65 28 70 46 72 65 65 29 3b 0a 20 20 7d 0a 20 20  e(pFree);.  }.  
18561 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
18562 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 6e 65  *.** Insert a ne
18563 77 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74 68  w record into th
18564 65 20 42 54 72 65 65 2e 20 20 54 68 65 20 6b 65  e BTree.  The ke
18565 79 20 69 73 20 67 69 76 65 6e 20 62 79 20 28 70  y is given by (p
18566 4b 65 79 2c 6e 4b 65 79 29 0a 2a 2a 20 61 6e 64  Key,nKey).** and
18567 20 74 68 65 20 64 61 74 61 20 69 73 20 67 69 76   the data is giv
18568 65 6e 20 62 79 20 28 70 44 61 74 61 2c 6e 44 61  en by (pData,nDa
18569 74 61 29 2e 20 20 54 68 65 20 63 75 72 73 6f 72  ta).  The cursor
1856a 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 74 6f   is used only to
1856b 0a 2a 2a 20 64 65 66 69 6e 65 20 77 68 61 74 20  .** define what 
1856c 74 61 62 6c 65 20 74 68 65 20 72 65 63 6f 72 64  table the record
1856d 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65 72   should be inser
1856e 74 65 64 20 69 6e 74 6f 2e 20 20 54 68 65 20 63  ted into.  The c
1856f 75 72 73 6f 72 0a 2a 2a 20 69 73 20 6c 65 66 74  ursor.** is left
18570 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 72   pointing at a r
18571 61 6e 64 6f 6d 20 6c 6f 63 61 74 69 6f 6e 2e 0a  andom location..
18572 2a 2a 0a 2a 2a 20 46 6f 72 20 61 6e 20 49 4e 54  **.** For an INT
18573 4b 45 59 20 74 61 62 6c 65 2c 20 6f 6e 6c 79 20  KEY table, only 
18574 74 68 65 20 6e 4b 65 79 20 76 61 6c 75 65 20 6f  the nKey value o
18575 66 20 74 68 65 20 6b 65 79 20 69 73 20 75 73 65  f the key is use
18576 64 2e 20 20 70 4b 65 79 20 69 73 0a 2a 2a 20 69  d.  pKey is.** i
18577 67 6e 6f 72 65 64 2e 20 20 46 6f 72 20 61 20 5a  gnored.  For a Z
18578 45 52 4f 44 41 54 41 20 74 61 62 6c 65 2c 20 74  ERODATA table, t
18579 68 65 20 70 44 61 74 61 20 61 6e 64 20 6e 44 61  he pData and nDa
1857a 74 61 20 61 72 65 20 62 6f 74 68 20 69 67 6e 6f  ta are both igno
1857b 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  red..**.** If th
1857c 65 20 73 65 65 6b 52 65 73 75 6c 74 20 70 61 72  e seekResult par
1857d 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65  ameter is non-ze
1857e 72 6f 2c 20 74 68 65 6e 20 61 20 73 75 63 63 65  ro, then a succe
1857f 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 0a 2a 2a  ssful call to.**
18580 20 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28   MovetoUnpacked(
18581 29 20 74 6f 20 73 65 65 6b 20 63 75 72 73 6f 72  ) to seek cursor
18582 20 70 43 75 72 20 74 6f 20 28 70 4b 65 79 2c 20   pCur to (pKey, 
18583 6e 4b 65 79 29 20 68 61 73 20 61 6c 72 65 61 64  nKey) has alread
18584 79 0a 2a 2a 20 62 65 65 6e 20 70 65 72 66 6f 72  y.** been perfor
18585 6d 65 64 2e 20 73 65 65 6b 52 65 73 75 6c 74 20  med. seekResult 
18586 69 73 20 74 68 65 20 73 65 61 72 63 68 20 72 65  is the search re
18587 73 75 6c 74 20 72 65 74 75 72 6e 65 64 20 28 61  sult returned (a
18588 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6e 75 6d   negative.** num
18589 62 65 72 20 69 66 20 70 43 75 72 20 70 6f 69 6e  ber if pCur poin
1858a 74 73 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74  ts at an entry t
1858b 68 61 74 20 69 73 20 73 6d 61 6c 6c 65 72 20 74  hat is smaller t
1858c 68 61 6e 20 28 70 4b 65 79 2c 20 6e 4b 65 79 29  han (pKey, nKey)
1858d 2c 20 6f 72 0a 2a 2a 20 61 20 70 6f 73 69 74 69  , or.** a positi
1858e 76 65 20 76 61 6c 75 65 20 69 66 20 70 43 75 72  ve value if pCur
1858f 20 70 6f 69 6e 74 73 20 61 74 20 61 6e 20 65 74   points at an et
18590 72 79 20 74 68 61 74 20 69 73 20 6c 61 72 67 65  ry that is large
18591 72 20 74 68 61 6e 20 0a 2a 2a 20 28 70 4b 65 79  r than .** (pKey
18592 2c 20 6e 4b 65 79 29 29 2e 20 0a 2a 2a 0a 2a 2a  , nKey)). .**.**
18593 20 49 66 20 74 68 65 20 73 65 65 6b 52 65 73 75   If the seekResu
18594 6c 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  lt parameter is 
18595 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74  non-zero, then t
18596 68 65 20 63 61 6c 6c 65 72 20 67 75 61 72 61 6e  he caller guaran
18597 74 65 65 73 20 74 68 61 74 0a 2a 2a 20 63 75 72  tees that.** cur
18598 73 6f 72 20 70 43 75 72 20 69 73 20 70 6f 69 6e  sor pCur is poin
18599 74 69 6e 67 20 61 74 20 74 68 65 20 65 78 69 73  ting at the exis
1859a 74 69 6e 67 20 63 6f 70 79 20 6f 66 20 61 20 72  ting copy of a r
1859b 6f 77 20 74 68 61 74 20 69 73 20 74 6f 20 62 65  ow that is to be
1859c 0a 2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 2e  .** overwritten.
1859d 20 20 49 66 20 74 68 65 20 73 65 65 6b 52 65 73    If the seekRes
1859e 75 6c 74 20 70 61 72 61 6d 65 74 65 72 20 69 73  ult parameter is
1859f 20 30 2c 20 74 68 65 6e 20 63 75 72 73 6f 72 20   0, then cursor 
185a0 70 43 75 72 20 6d 61 79 0a 2a 2a 20 70 6f 69 6e  pCur may.** poin
185a1 74 20 74 6f 20 61 6e 79 20 65 6e 74 72 79 20 6f  t to any entry o
185a2 72 20 74 6f 20 6e 6f 20 65 6e 74 72 79 20 61 74  r to no entry at
185a3 20 61 6c 6c 20 61 6e 64 20 73 6f 20 74 68 69 73   all and so this
185a4 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 74 6f   function has to
185a5 20 73 65 65 6b 0a 2a 2a 20 74 68 65 20 63 75 72   seek.** the cur
185a6 73 6f 72 20 62 65 66 6f 72 65 20 74 68 65 20 6e  sor before the n
185a7 65 77 20 6b 65 79 20 63 61 6e 20 62 65 20 69 6e  ew key can be in
185a8 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  serted..*/.SQLIT
185a9 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
185aa 6c 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74  lite3BtreeInsert
185ab 28 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  (.  BtCursor *pC
185ac 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ur,             
185ad 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 64 61 74     /* Insert dat
185ae 61 20 69 6e 74 6f 20 74 68 65 20 74 61 62 6c 65  a into the table
185af 20 6f 66 20 74 68 69 73 20 63 75 72 73 6f 72 20   of this cursor 
185b0 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  */.  const void 
185b1 2a 70 4b 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c  *pKey, i64 nKey,
185b2 20 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 6f      /* The key o
185b3 66 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64  f the new record
185b4 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
185b5 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61   *pData, int nDa
185b6 74 61 2c 20 20 2f 2a 20 54 68 65 20 64 61 74 61  ta,  /* The data
185b7 20 6f 66 20 74 68 65 20 6e 65 77 20 72 65 63 6f   of the new reco
185b8 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72  rd */.  int nZer
185b9 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o,              
185ba 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
185bb 20 6f 66 20 65 78 74 72 61 20 30 20 62 79 74 65   of extra 0 byte
185bc 73 20 74 6f 20 61 70 70 65 6e 64 20 74 6f 20 64  s to append to d
185bd 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20 61 70 70  ata */.  int app
185be 65 6e 64 42 69 61 73 2c 20 20 20 20 20 20 20 20  endBias,        
185bf 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
185c0 69 66 20 74 68 69 73 20 69 73 20 6c 69 6b 65 6c  if this is likel
185c1 79 20 61 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 20  y an append */. 
185c2 20 69 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 20   int seekResult 
185c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
185c4 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 70 72 69  /* Result of pri
185c5 6f 72 20 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65  or MovetoUnpacke
185c6 64 28 29 20 63 61 6c 6c 20 2a 2f 0a 29 7b 0a 20  d() call */.){. 
185c7 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 6c   int rc;.  int l
185c8 6f 63 20 3d 20 73 65 65 6b 52 65 73 75 6c 74 3b  oc = seekResult;
185c9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 2d 31 3a            /* -1:
185ca 20 62 65 66 6f 72 65 20 64 65 73 69 72 65 64 20   before desired 
185cb 6c 6f 63 61 74 69 6f 6e 20 20 2b 31 3a 20 61 66  location  +1: af
185cc 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4e  ter */.  int szN
185cd 65 77 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 64  ew = 0;.  int id
185ce 78 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  x;.  MemPage *pP
185cf 61 67 65 3b 0a 20 20 42 74 72 65 65 20 2a 70 20  age;.  Btree *p 
185d0 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a  = pCur->pBtree;.
185d1 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
185d2 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 75 6e 73 69  = p->pBt;.  unsi
185d3 67 6e 65 64 20 63 68 61 72 20 2a 6f 6c 64 43 65  gned char *oldCe
185d4 6c 6c 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  ll;.  unsigned c
185d5 68 61 72 20 2a 6e 65 77 43 65 6c 6c 20 3d 20 30  har *newCell = 0
185d6 3b 0a 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65  ;..  if( pCur->e
185d7 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41  State==CURSOR_FA
185d8 55 4c 54 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ULT ){.    asser
185d9 74 28 20 70 43 75 72 2d 3e 73 6b 69 70 4e 65 78  t( pCur->skipNex
185da 74 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  t!=SQLITE_OK );.
185db 20 20 20 20 72 65 74 75 72 6e 20 70 43 75 72 2d      return pCur-
185dc 3e 73 6b 69 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a  >skipNext;.  }..
185dd 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
185de 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
185df 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43   );.  assert( pC
185e0 75 72 2d 3e 77 72 46 6c 61 67 20 26 26 20 70 42  ur->wrFlag && pB
185e1 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
185e2 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 26 26  ==TRANS_WRITE &&
185e3 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20   !pBt->readOnly 
185e4 29 3b 0a 20 20 61 73 73 65 72 74 28 20 68 61 73  );.  assert( has
185e5 53 68 61 72 65 64 43 61 63 68 65 54 61 62 6c 65  SharedCacheTable
185e6 4c 6f 63 6b 28 70 2c 20 70 43 75 72 2d 3e 70 67  Lock(p, pCur->pg
185e7 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 2d 3e 70 4b  noRoot, pCur->pK
185e8 65 79 49 6e 66 6f 21 3d 30 2c 20 32 29 20 29 3b  eyInfo!=0, 2) );
185e9 0a 0a 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68  ..  /* Assert th
185ea 61 74 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61  at the caller ha
185eb 73 20 62 65 65 6e 20 63 6f 6e 73 69 73 74 65 6e  s been consisten
185ec 74 2e 20 49 66 20 74 68 69 73 20 63 75 72 73 6f  t. If this curso
185ed 72 20 77 61 73 20 6f 70 65 6e 65 64 0a 20 20 2a  r was opened.  *
185ee 2a 20 65 78 70 65 63 74 69 6e 67 20 61 6e 20 69  * expecting an i
185ef 6e 64 65 78 20 62 2d 74 72 65 65 2c 20 74 68 65  ndex b-tree, the
185f0 6e 20 74 68 65 20 63 61 6c 6c 65 72 20 73 68 6f  n the caller sho
185f1 75 6c 64 20 62 65 20 69 6e 73 65 72 74 69 6e 67  uld be inserting
185f2 20 62 6c 6f 62 0a 20 20 2a 2a 20 6b 65 79 73 20   blob.  ** keys 
185f3 77 69 74 68 20 6e 6f 20 61 73 73 6f 63 69 61 74  with no associat
185f4 65 64 20 64 61 74 61 2e 20 49 66 20 74 68 65 20  ed data. If the 
185f5 63 75 72 73 6f 72 20 77 61 73 20 6f 70 65 6e 65  cursor was opene
185f6 64 20 65 78 70 65 63 74 69 6e 67 20 61 6e 0a 20  d expecting an. 
185f7 20 2a 2a 20 69 6e 74 6b 65 79 20 74 61 62 6c 65   ** intkey table
185f8 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 73 68 6f  , the caller sho
185f9 75 6c 64 20 62 65 20 69 6e 73 65 72 74 69 6e 67  uld be inserting
185fa 20 69 6e 74 65 67 65 72 20 6b 65 79 73 20 77 69   integer keys wi
185fb 74 68 20 61 0a 20 20 2a 2a 20 62 6c 6f 62 20 6f  th a.  ** blob o
185fc 66 20 61 73 73 6f 63 69 61 74 65 64 20 64 61 74  f associated dat
185fd 61 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  a.  */.  assert(
185fe 20 28 70 4b 65 79 3d 3d 30 29 3d 3d 28 70 43 75   (pKey==0)==(pCu
185ff 72 2d 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 29 20  r->pKeyInfo==0) 
18600 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73  );..  /* If this
18601 20 69 73 20 61 6e 20 69 6e 73 65 72 74 20 69 6e   is an insert in
18602 74 6f 20 61 20 74 61 62 6c 65 20 62 2d 74 72 65  to a table b-tre
18603 65 2c 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e  e, invalidate an
18604 79 20 69 6e 63 72 62 6c 6f 62 20 0a 20 20 2a 2a  y incrblob .  **
18605 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e   cursors open on
18606 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67 20 72   the row being r
18607 65 70 6c 61 63 65 64 20 28 61 73 73 75 6d 69 6e  eplaced (assumin
18608 67 20 74 68 69 73 20 69 73 20 61 20 72 65 70 6c  g this is a repl
18609 61 63 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69  ace.  ** operati
1860a 6f 6e 20 2d 20 69 66 20 69 74 20 69 73 20 6e 6f  on - if it is no
1860b 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  t, the following
1860c 20 69 73 20 61 20 6e 6f 2d 6f 70 29 2e 20 20 2a   is a no-op).  *
1860d 2f 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 70 4b  /.  if( pCur->pK
1860e 65 79 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20  eyInfo==0 ){.   
1860f 20 69 6e 76 61 6c 69 64 61 74 65 49 6e 63 72 62   invalidateIncrb
18610 6c 6f 62 43 75 72 73 6f 72 73 28 70 2c 20 6e 4b  lobCursors(p, nK
18611 65 79 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f  ey, 0);.  }..  /
18612 2a 20 53 61 76 65 20 74 68 65 20 70 6f 73 69 74  * Save the posit
18613 69 6f 6e 73 20 6f 66 20 61 6e 79 20 6f 74 68 65  ions of any othe
18614 72 20 63 75 72 73 6f 72 73 20 6f 70 65 6e 20 6f  r cursors open o
18615 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 0a 20 20  n this table..  
18616 2a 2a 0a 20 20 2a 2a 20 49 6e 20 73 6f 6d 65 20  **.  ** In some 
18617 63 61 73 65 73 2c 20 74 68 65 20 63 61 6c 6c 20  cases, the call 
18618 74 6f 20 62 74 72 65 65 4d 6f 76 65 74 6f 28 29  to btreeMoveto()
18619 20 62 65 6c 6f 77 20 69 73 20 61 20 6e 6f 2d 6f   below is a no-o
1861a 70 2e 20 46 6f 72 0a 20 20 2a 2a 20 65 78 61 6d  p. For.  ** exam
1861b 70 6c 65 2c 20 77 68 65 6e 20 69 6e 73 65 72 74  ple, when insert
1861c 69 6e 67 20 64 61 74 61 20 69 6e 74 6f 20 61 20  ing data into a 
1861d 74 61 62 6c 65 20 77 69 74 68 20 61 75 74 6f 2d  table with auto-
1861e 67 65 6e 65 72 61 74 65 64 20 69 6e 74 65 67 65  generated intege
1861f 72 0a 20 20 2a 2a 20 6b 65 79 73 2c 20 74 68 65  r.  ** keys, the
18620 20 56 44 42 45 20 6c 61 79 65 72 20 69 6e 76 6f   VDBE layer invo
18621 6b 65 73 20 73 71 6c 69 74 65 33 42 74 72 65 65  kes sqlite3Btree
18622 4c 61 73 74 28 29 20 74 6f 20 66 69 67 75 72 65  Last() to figure
18623 20 6f 75 74 20 74 68 65 20 0a 20 20 2a 2a 20 69   out the .  ** i
18624 6e 74 65 67 65 72 20 6b 65 79 20 74 6f 20 75 73  nteger key to us
18625 65 2e 20 49 74 20 74 68 65 6e 20 63 61 6c 6c 73  e. It then calls
18626 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74   this function t
18627 6f 20 61 63 74 75 61 6c 6c 79 20 69 6e 73 65 72  o actually inser
18628 74 20 74 68 65 20 0a 20 20 2a 2a 20 64 61 74 61  t the .  ** data
18629 20 69 6e 74 6f 20 74 68 65 20 69 6e 74 6b 65 79   into the intkey
1862a 20 42 2d 54 72 65 65 2e 20 49 6e 20 74 68 69 73   B-Tree. In this
1862b 20 63 61 73 65 20 62 74 72 65 65 4d 6f 76 65 74   case btreeMovet
1862c 6f 28 29 20 72 65 63 6f 67 6e 69 7a 65 73 0a 20  o() recognizes. 
1862d 20 2a 2a 20 74 68 61 74 20 74 68 65 20 63 75 72   ** that the cur
1862e 73 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 77  sor is already w
1862f 68 65 72 65 20 69 74 20 6e 65 65 64 73 20 74 6f  here it needs to
18630 20 62 65 20 61 6e 64 20 72 65 74 75 72 6e 73 20   be and returns 
18631 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20 64 6f 69  without.  ** doi
18632 6e 67 20 61 6e 79 20 77 6f 72 6b 2e 20 54 6f 20  ng any work. To 
18633 61 76 6f 69 64 20 74 68 77 61 72 74 69 6e 67 20  avoid thwarting 
18634 74 68 65 73 65 20 6f 70 74 69 6d 69 7a 61 74 69  these optimizati
18635 6f 6e 73 2c 20 69 74 20 69 73 20 69 6d 70 6f 72  ons, it is impor
18636 74 61 6e 74 0a 20 20 2a 2a 20 6e 6f 74 20 74 6f  tant.  ** not to
18637 20 63 6c 65 61 72 20 74 68 65 20 63 75 72 73 6f   clear the curso
18638 72 20 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 72  r here..  */.  r
18639 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 6f  c = saveAllCurso
1863a 72 73 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 67  rs(pBt, pCur->pg
1863b 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29 3b 0a 20  noRoot, pCur);. 
1863c 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
1863d 20 72 63 3b 0a 20 20 69 66 28 20 21 6c 6f 63 20   rc;.  if( !loc 
1863e 29 7b 0a 20 20 20 20 72 63 20 3d 20 62 74 72 65  ){.    rc = btre
1863f 65 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20 70 4b  eMoveto(pCur, pK
18640 65 79 2c 20 6e 4b 65 79 2c 20 61 70 70 65 6e 64  ey, nKey, append
18641 42 69 61 73 2c 20 26 6c 6f 63 29 3b 0a 20 20 20  Bias, &loc);.   
18642 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
18643 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72   rc;.  }.  asser
18644 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d  t( pCur->eState=
18645 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c  =CURSOR_VALID ||
18646 20 28 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d   (pCur->eState==
18647 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 26  CURSOR_INVALID &
18648 26 20 6c 6f 63 29 20 29 3b 0a 0a 20 20 70 50 61  & loc) );..  pPa
18649 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  ge = pCur->apPag
1864a 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a  e[pCur->iPage];.
1864b 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
1864c 3e 69 6e 74 4b 65 79 20 7c 7c 20 6e 4b 65 79 3e  >intKey || nKey>
1864d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1864e 70 50 61 67 65 2d 3e 6c 65 61 66 20 7c 7c 20 21  pPage->leaf || !
1864f 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b  pPage->intKey );
18650 0a 0a 20 20 54 52 41 43 45 28 28 22 49 4e 53 45  ..  TRACE(("INSE
18651 52 54 3a 20 74 61 62 6c 65 3d 25 64 20 6e 6b 65  RT: table=%d nke
18652 79 3d 25 6c 6c 64 20 6e 64 61 74 61 3d 25 64 20  y=%lld ndata=%d 
18653 70 61 67 65 3d 25 64 20 25 73 5c 6e 22 2c 0a 20  page=%d %s\n",. 
18654 20 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 70           pCur->p
18655 67 6e 6f 52 6f 6f 74 2c 20 6e 4b 65 79 2c 20 6e  gnoRoot, nKey, n
18656 44 61 74 61 2c 20 70 50 61 67 65 2d 3e 70 67 6e  Data, pPage->pgn
18657 6f 2c 0a 20 20 20 20 20 20 20 20 20 20 6c 6f 63  o,.          loc
18658 3d 3d 30 20 3f 20 22 6f 76 65 72 77 72 69 74 65  ==0 ? "overwrite
18659 22 20 3a 20 22 6e 65 77 20 65 6e 74 72 79 22 29  " : "new entry")
1865a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
1865b 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20  ge->isInit );.  
1865c 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53 70 61 63  allocateTempSpac
1865d 65 28 70 42 74 29 3b 0a 20 20 6e 65 77 43 65 6c  e(pBt);.  newCel
1865e 6c 20 3d 20 70 42 74 2d 3e 70 54 6d 70 53 70 61  l = pBt->pTmpSpa
1865f 63 65 3b 0a 20 20 69 66 28 20 6e 65 77 43 65 6c  ce;.  if( newCel
18660 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51  l==0 ) return SQ
18661 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 72 63  LITE_NOMEM;.  rc
18662 20 3d 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 70 50   = fillInCell(pP
18663 61 67 65 2c 20 6e 65 77 43 65 6c 6c 2c 20 70 4b  age, newCell, pK
18664 65 79 2c 20 6e 4b 65 79 2c 20 70 44 61 74 61 2c  ey, nKey, pData,
18665 20 6e 44 61 74 61 2c 20 6e 5a 65 72 6f 2c 20 26   nData, nZero, &
18666 73 7a 4e 65 77 29 3b 0a 20 20 69 66 28 20 72 63  szNew);.  if( rc
18667 20 29 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65   ) goto end_inse
18668 72 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a  rt;.  assert( sz
18669 4e 65 77 3d 3d 63 65 6c 6c 53 69 7a 65 50 74 72  New==cellSizePtr
1866a 28 70 50 61 67 65 2c 20 6e 65 77 43 65 6c 6c 29  (pPage, newCell)
1866b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 7a   );.  assert( sz
1866c 4e 65 77 3c 3d 4d 58 5f 43 45 4c 4c 5f 53 49 5a  New<=MX_CELL_SIZ
1866d 45 28 70 42 74 29 20 29 3b 0a 20 20 69 64 78 20  E(pBt) );.  idx 
1866e 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43  = pCur->aiIdx[pC
1866f 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 69 66  ur->iPage];.  if
18670 28 20 6c 6f 63 3d 3d 30 20 29 7b 0a 20 20 20 20  ( loc==0 ){.    
18671 75 31 36 20 73 7a 4f 6c 64 3b 0a 20 20 20 20 61  u16 szOld;.    a
18672 73 73 65 72 74 28 20 69 64 78 3c 70 50 61 67 65  ssert( idx<pPage
18673 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72  ->nCell );.    r
18674 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
18675 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62  Write(pPage->pDb
18676 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Page);.    if( r
18677 63 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  c ){.      goto 
18678 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 20 20  end_insert;.    
18679 7d 0a 20 20 20 20 6f 6c 64 43 65 6c 6c 20 3d 20  }.    oldCell = 
1867a 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20  findCell(pPage, 
1867b 69 64 78 29 3b 0a 20 20 20 20 69 66 28 20 21 70  idx);.    if( !p
1867c 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Page->leaf ){.  
1867d 20 20 20 20 6d 65 6d 63 70 79 28 6e 65 77 43 65      memcpy(newCe
1867e 6c 6c 2c 20 6f 6c 64 43 65 6c 6c 2c 20 34 29 3b  ll, oldCell, 4);
1867f 0a 20 20 20 20 7d 0a 20 20 20 20 73 7a 4f 6c 64  .    }.    szOld
18680 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70   = cellSizePtr(p
18681 50 61 67 65 2c 20 6f 6c 64 43 65 6c 6c 29 3b 0a  Page, oldCell);.
18682 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 43 65      rc = clearCe
18683 6c 6c 28 70 50 61 67 65 2c 20 6f 6c 64 43 65 6c  ll(pPage, oldCel
18684 6c 29 3b 0a 20 20 20 20 64 72 6f 70 43 65 6c 6c  l);.    dropCell
18685 28 70 50 61 67 65 2c 20 69 64 78 2c 20 73 7a 4f  (pPage, idx, szO
18686 6c 64 2c 20 26 72 63 29 3b 0a 20 20 20 20 69 66  ld, &rc);.    if
18687 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f  ( rc ) goto end_
18688 69 6e 73 65 72 74 3b 0a 20 20 7d 65 6c 73 65 20  insert;.  }else 
18689 69 66 28 20 6c 6f 63 3c 30 20 26 26 20 70 50 61  if( loc<0 && pPa
1868a 67 65 2d 3e 6e 43 65 6c 6c 3e 30 20 29 7b 0a 20  ge->nCell>0 ){. 
1868b 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
1868c 2d 3e 6c 65 61 66 20 29 3b 0a 20 20 20 20 69 64  ->leaf );.    id
1868d 78 20 3d 20 2b 2b 70 43 75 72 2d 3e 61 69 49 64  x = ++pCur->aiId
1868e 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a  x[pCur->iPage];.
1868f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
18690 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61 66  ert( pPage->leaf
18691 20 29 3b 0a 20 20 7d 0a 20 20 69 6e 73 65 72 74   );.  }.  insert
18692 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c  Cell(pPage, idx,
18693 20 6e 65 77 43 65 6c 6c 2c 20 73 7a 4e 65 77 2c   newCell, szNew,
18694 20 30 2c 20 30 2c 20 26 72 63 29 3b 0a 20 20 61   0, 0, &rc);.  a
18695 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
18696 45 5f 4f 4b 20 7c 7c 20 70 50 61 67 65 2d 3e 6e  E_OK || pPage->n
18697 43 65 6c 6c 3e 30 20 7c 7c 20 70 50 61 67 65 2d  Cell>0 || pPage-
18698 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 29 3b 0a  >nOverflow>0 );.
18699 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 65 72 72 6f  .  /* If no erro
1869a 72 20 68 61 73 20 6f 63 63 75 72 65 64 20 61 6e  r has occured an
1869b 64 20 70 50 61 67 65 20 68 61 73 20 61 6e 20 6f  d pPage has an o
1869c 76 65 72 66 6c 6f 77 20 63 65 6c 6c 2c 20 63 61  verflow cell, ca
1869d 6c 6c 20 62 61 6c 61 6e 63 65 28 29 20 0a 20 20  ll balance() .  
1869e 2a 2a 20 74 6f 20 72 65 64 69 73 74 72 69 62 75  ** to redistribu
1869f 74 65 20 74 68 65 20 63 65 6c 6c 73 20 77 69 74  te the cells wit
186a0 68 69 6e 20 74 68 65 20 74 72 65 65 2e 20 53 69  hin the tree. Si
186a1 6e 63 65 20 62 61 6c 61 6e 63 65 28 29 20 6d 61  nce balance() ma
186a2 79 20 6d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20  y move.  ** the 
186a3 63 75 72 73 6f 72 2c 20 7a 65 72 6f 20 74 68 65  cursor, zero the
186a4 20 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 2e 6e   BtCursor.info.n
186a5 53 69 7a 65 20 61 6e 64 20 42 74 43 75 72 73 6f  Size and BtCurso
186a6 72 2e 76 61 6c 69 64 4e 4b 65 79 0a 20 20 2a 2a  r.validNKey.  **
186a7 20 76 61 72 69 61 62 6c 65 73 2e 0a 20 20 2a 2a   variables..  **
186a8 0a 20 20 2a 2a 20 50 72 65 76 69 6f 75 73 20 76  .  ** Previous v
186a9 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  ersions of SQLit
186aa 65 20 63 61 6c 6c 65 64 20 6d 6f 76 65 54 6f 52  e called moveToR
186ab 6f 6f 74 28 29 20 74 6f 20 6d 6f 76 65 20 74 68  oot() to move th
186ac 65 20 63 75 72 73 6f 72 0a 20 20 2a 2a 20 62 61  e cursor.  ** ba
186ad 63 6b 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 70  ck to the root p
186ae 61 67 65 20 61 73 20 62 61 6c 61 6e 63 65 28 29  age as balance()
186af 20 75 73 65 64 20 74 6f 20 69 6e 76 61 6c 69 64   used to invalid
186b0 61 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ate the contents
186b1 0a 20 20 2a 2a 20 6f 66 20 42 74 43 75 72 73 6f  .  ** of BtCurso
186b2 72 2e 61 70 50 61 67 65 5b 5d 20 61 6e 64 20 42  r.apPage[] and B
186b3 74 43 75 72 73 6f 72 2e 61 69 49 64 78 5b 5d 2e  tCursor.aiIdx[].
186b4 20 49 6e 73 74 65 61 64 20 6f 66 20 64 6f 69 6e   Instead of doin
186b5 67 20 74 68 61 74 2c 0a 20 20 2a 2a 20 73 65 74  g that,.  ** set
186b6 20 74 68 65 20 63 75 72 73 6f 72 20 73 74 61 74   the cursor stat
186b7 65 20 74 6f 20 22 69 6e 76 61 6c 69 64 22 2e 20  e to "invalid". 
186b8 54 68 69 73 20 6d 61 6b 65 73 20 63 6f 6d 6d 6f  This makes commo
186b9 6e 20 69 6e 73 65 72 74 20 6f 70 65 72 61 74 69  n insert operati
186ba 6f 6e 73 0a 20 20 2a 2a 20 73 6c 69 67 68 74 6c  ons.  ** slightl
186bb 79 20 66 61 73 74 65 72 2e 0a 20 20 2a 2a 0a 20  y faster..  **. 
186bc 20 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 73   ** There is a s
186bd 75 62 74 6c 65 20 62 75 74 20 69 6d 70 6f 72 74  ubtle but import
186be 61 6e 74 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ant optimization
186bf 20 68 65 72 65 20 74 6f 6f 2e 20 57 68 65 6e 20   here too. When 
186c0 69 6e 73 65 72 74 69 6e 67 0a 20 20 2a 2a 20 6d  inserting.  ** m
186c1 75 6c 74 69 70 6c 65 20 72 65 63 6f 72 64 73 20  ultiple records 
186c2 69 6e 74 6f 20 61 6e 20 69 6e 74 6b 65 79 20 62  into an intkey b
186c3 2d 74 72 65 65 20 75 73 69 6e 67 20 61 20 73 69  -tree using a si
186c4 6e 67 6c 65 20 63 75 72 73 6f 72 20 28 61 73 20  ngle cursor (as 
186c5 63 61 6e 0a 20 20 2a 2a 20 68 61 70 70 65 6e 20  can.  ** happen 
186c6 77 68 69 6c 65 20 70 72 6f 63 65 73 73 69 6e 67  while processing
186c7 20 61 6e 20 22 49 4e 53 45 52 54 20 49 4e 54 4f   an "INSERT INTO
186c8 20 2e 2e 2e 20 53 45 4c 45 43 54 22 20 73 74 61   ... SELECT" sta
186c9 74 65 6d 65 6e 74 29 2c 20 69 74 0a 20 20 2a 2a  tement), it.  **
186ca 20 69 73 20 61 64 76 61 6e 74 61 67 65 6f 75 73   is advantageous
186cb 20 74 6f 20 6c 65 61 76 65 20 74 68 65 20 63 75   to leave the cu
186cc 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f  rsor pointing to
186cd 20 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20   the last entry 
186ce 69 6e 0a 20 20 2a 2a 20 74 68 65 20 62 2d 74 72  in.  ** the b-tr
186cf 65 65 20 69 66 20 70 6f 73 73 69 62 6c 65 2e 20  ee if possible. 
186d0 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  If the cursor is
186d1 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 74   left pointing t
186d2 6f 20 74 68 65 20 6c 61 73 74 0a 20 20 2a 2a 20  o the last.  ** 
186d3 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74 61 62  entry in the tab
186d4 6c 65 2c 20 61 6e 64 20 74 68 65 20 6e 65 78 74  le, and the next
186d5 20 72 6f 77 20 69 6e 73 65 72 74 65 64 20 68 61   row inserted ha
186d6 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6b 65 79  s an integer key
186d7 0a 20 20 2a 2a 20 6c 61 72 67 65 72 20 74 68 61  .  ** larger tha
186d8 6e 20 74 68 65 20 6c 61 72 67 65 73 74 20 65 78  n the largest ex
186d9 69 73 74 69 6e 67 20 6b 65 79 2c 20 69 74 20 69  isting key, it i
186da 73 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 69 6e  s possible to in
186db 73 65 72 74 20 74 68 65 0a 20 20 2a 2a 20 72 6f  sert the.  ** ro
186dc 77 20 77 69 74 68 6f 75 74 20 73 65 65 6b 69 6e  w without seekin
186dd 67 20 74 68 65 20 63 75 72 73 6f 72 2e 20 54 68  g the cursor. Th
186de 69 73 20 63 61 6e 20 62 65 20 61 20 62 69 67 20  is can be a big 
186df 70 65 72 66 6f 72 6d 61 6e 63 65 20 62 6f 6f 73  performance boos
186e0 74 2e 0a 20 20 2a 2f 0a 20 20 70 43 75 72 2d 3e  t..  */.  pCur->
186e1 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a  info.nSize = 0;.
186e2 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65    pCur->validNKe
186e3 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 72 63 3d  y = 0;.  if( rc=
186e4 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50  =SQLITE_OK && pP
186e5 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29  age->nOverflow )
186e6 7b 0a 20 20 20 20 72 63 20 3d 20 62 61 6c 61 6e  {.    rc = balan
186e7 63 65 28 70 43 75 72 29 3b 0a 0a 20 20 20 20 2f  ce(pCur);..    /
186e8 2a 20 4d 75 73 74 20 6d 61 6b 65 20 73 75 72 65  * Must make sure
186e9 20 6e 4f 76 65 72 66 6c 6f 77 20 69 73 20 72 65   nOverflow is re
186ea 73 65 74 20 74 6f 20 7a 65 72 6f 20 65 76 65 6e  set to zero even
186eb 20 69 66 20 74 68 65 20 62 61 6c 61 6e 63 65 28   if the balance(
186ec 29 0a 20 20 20 20 2a 2a 20 66 61 69 6c 73 2e 20  ).    ** fails. 
186ed 49 6e 74 65 72 6e 61 6c 20 64 61 74 61 20 73 74  Internal data st
186ee 72 75 63 74 75 72 65 20 63 6f 72 72 75 70 74 69  ructure corrupti
186ef 6f 6e 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 6f  on will result o
186f0 74 68 65 72 77 69 73 65 2e 20 0a 20 20 20 20 2a  therwise. .    *
186f1 2a 20 41 6c 73 6f 2c 20 73 65 74 20 74 68 65 20  * Also, set the 
186f2 63 75 72 73 6f 72 20 73 74 61 74 65 20 74 6f 20  cursor state to 
186f3 69 6e 76 61 6c 69 64 2e 20 54 68 69 73 20 73 74  invalid. This st
186f4 6f 70 73 20 73 61 76 65 43 75 72 73 6f 72 50 6f  ops saveCursorPo
186f5 73 69 74 69 6f 6e 28 29 0a 20 20 20 20 2a 2a 20  sition().    ** 
186f6 66 72 6f 6d 20 74 72 79 69 6e 67 20 74 6f 20 73  from trying to s
186f7 61 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ave the current 
186f8 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74 68 65 20  position of the 
186f9 63 75 72 73 6f 72 2e 20 20 2a 2f 0a 20 20 20 20  cursor.  */.    
186fa 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
186fb 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 4f 76 65 72  r->iPage]->nOver
186fc 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20 20 70 43  flow = 0;.    pC
186fd 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52  ur->eState = CUR
186fe 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 7d  SOR_INVALID;.  }
186ff 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
18700 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
18701 61 67 65 5d 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d  age]->nOverflow=
18702 3d 30 20 29 3b 0a 0a 65 6e 64 5f 69 6e 73 65 72  =0 );..end_inser
18703 74 3a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  t:.  return rc;.
18704 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
18705 74 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74  the entry that t
18706 68 65 20 63 75 72 73 6f 72 20 69 73 20 70 6f 69  he cursor is poi
18707 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20 63  nting to.  The c
18708 75 72 73 6f 72 0a 2a 2a 20 69 73 20 6c 65 66 74  ursor.** is left
18709 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 61   pointing at a a
1870a 72 62 69 74 72 61 72 79 20 6c 6f 63 61 74 69 6f  rbitrary locatio
1870b 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
1870c 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1870d 42 74 72 65 65 44 65 6c 65 74 65 28 42 74 43 75  BtreeDelete(BtCu
1870e 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 42  rsor *pCur){.  B
1870f 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d 3e  tree *p = pCur->
18710 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61 72  pBtree;.  BtShar
18711 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
18712 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 0a  ;              .
18713 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
18714 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18715 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
18716 20 63 6f 64 65 20 2a 2f 0a 20 20 4d 65 6d 50 61   code */.  MemPa
18717 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20 20 20  ge *pPage;      
18718 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18719 2f 2a 20 50 61 67 65 20 74 6f 20 64 65 6c 65 74  /* Page to delet
1871a 65 20 63 65 6c 6c 20 66 72 6f 6d 20 2a 2f 0a 20  e cell from */. 
1871b 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
1871c 70 43 65 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  pCell;          
1871d 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
1871e 20 74 6f 20 63 65 6c 6c 20 74 6f 20 64 65 6c 65   to cell to dele
1871f 74 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43 65 6c  te */.  int iCel
18720 6c 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20  lIdx;           
18721 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
18722 49 6e 64 65 78 20 6f 66 20 63 65 6c 6c 20 74 6f  Index of cell to
18723 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 69 6e 74   delete */.  int
18724 20 69 43 65 6c 6c 44 65 70 74 68 3b 20 20 20 20   iCellDepth;    
18725 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18726 20 20 2f 2a 20 44 65 70 74 68 20 6f 66 20 6e 6f    /* Depth of no
18727 64 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 43  de containing pC
18728 65 6c 6c 20 2a 2f 20 0a 0a 20 20 61 73 73 65 72  ell */ ..  asser
18729 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
1872a 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61  tex(pCur) );.  a
1872b 73 73 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72  ssert( pBt->inTr
1872c 61 6e 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53  ansaction==TRANS
1872d 5f 57 52 49 54 45 20 29 3b 0a 20 20 61 73 73 65  _WRITE );.  asse
1872e 72 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e  rt( !pBt->readOn
1872f 6c 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ly );.  assert( 
18730 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 29 3b 0a  pCur->wrFlag );.
18731 20 20 61 73 73 65 72 74 28 20 68 61 73 53 68 61    assert( hasSha
18732 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
18733 6b 28 70 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52  k(p, pCur->pgnoR
18734 6f 6f 74 2c 20 70 43 75 72 2d 3e 70 4b 65 79 49  oot, pCur->pKeyI
18735 6e 66 6f 21 3d 30 2c 20 32 29 20 29 3b 0a 20 20  nfo!=0, 2) );.  
18736 61 73 73 65 72 74 28 20 21 68 61 73 52 65 61 64  assert( !hasRead
18737 43 6f 6e 66 6c 69 63 74 73 28 70 2c 20 70 43 75  Conflicts(p, pCu
18738 72 2d 3e 70 67 6e 6f 52 6f 6f 74 29 20 29 3b 0a  r->pgnoRoot) );.
18739 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70 43 75  .  if( NEVER(pCu
1873a 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
1873b 50 61 67 65 5d 3e 3d 70 43 75 72 2d 3e 61 70 50  Page]>=pCur->apP
1873c 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
1873d 2d 3e 6e 43 65 6c 6c 29 20 0a 20 20 20 7c 7c 20  ->nCell) .   || 
1873e 4e 45 56 45 52 28 70 43 75 72 2d 3e 65 53 74 61  NEVER(pCur->eSta
1873f 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44  te!=CURSOR_VALID
18740 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ).  ){.    retur
18741 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 20  n SQLITE_ERROR; 
18742 20 2f 2a 20 53 6f 6d 65 74 68 69 6e 67 20 68 61   /* Something ha
18743 73 20 67 6f 6e 65 20 61 77 72 79 2e 20 2a 2f 0a  s gone awry. */.
18744 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69    }..  /* If thi
18745 73 20 69 73 20 61 20 64 65 6c 65 74 65 20 6f 70  s is a delete op
18746 65 72 61 74 69 6f 6e 20 74 6f 20 72 65 6d 6f 76  eration to remov
18747 65 20 61 20 72 6f 77 20 66 72 6f 6d 20 61 20 74  e a row from a t
18748 61 62 6c 65 20 62 2d 74 72 65 65 2c 0a 20 20 2a  able b-tree,.  *
18749 2a 20 69 6e 76 61 6c 69 64 61 74 65 20 61 6e 79  * invalidate any
1874a 20 69 6e 63 72 62 6c 6f 62 20 63 75 72 73 6f 72   incrblob cursor
1874b 73 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 72 6f  s open on the ro
1874c 77 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2e  w being deleted.
1874d 20 20 2a 2f 0a 20 20 69 66 28 20 70 43 75 72 2d    */.  if( pCur-
1874e 3e 70 4b 65 79 49 6e 66 6f 3d 3d 30 20 29 7b 0a  >pKeyInfo==0 ){.
1874f 20 20 20 20 69 6e 76 61 6c 69 64 61 74 65 49 6e      invalidateIn
18750 63 72 62 6c 6f 62 43 75 72 73 6f 72 73 28 70 2c  crblobCursors(p,
18751 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79   pCur->info.nKey
18752 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 69 43 65  , 0);.  }..  iCe
18753 6c 6c 44 65 70 74 68 20 3d 20 70 43 75 72 2d 3e  llDepth = pCur->
18754 69 50 61 67 65 3b 0a 20 20 69 43 65 6c 6c 49 64  iPage;.  iCellId
18755 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  x = pCur->aiIdx[
18756 69 43 65 6c 6c 44 65 70 74 68 5d 3b 0a 20 20 70  iCellDepth];.  p
18757 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50  Page = pCur->apP
18758 61 67 65 5b 69 43 65 6c 6c 44 65 70 74 68 5d 3b  age[iCellDepth];
18759 0a 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43  .  pCell = findC
1875a 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c  ell(pPage, iCell
1875b 49 64 78 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  Idx);..  /* If t
1875c 68 65 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 69  he page containi
1875d 6e 67 20 74 68 65 20 65 6e 74 72 79 20 74 6f 20  ng the entry to 
1875e 64 65 6c 65 74 65 20 69 73 20 6e 6f 74 20 61 20  delete is not a 
1875f 6c 65 61 66 20 70 61 67 65 2c 20 6d 6f 76 65 0a  leaf page, move.
18760 20 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72 20    ** the cursor 
18761 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 65  to the largest e
18762 6e 74 72 79 20 69 6e 20 74 68 65 20 74 72 65 65  ntry in the tree
18763 20 74 68 61 74 20 69 73 20 73 6d 61 6c 6c 65 72   that is smaller
18764 20 74 68 61 6e 0a 20 20 2a 2a 20 74 68 65 20 65   than.  ** the e
18765 6e 74 72 79 20 62 65 69 6e 67 20 64 65 6c 65 74  ntry being delet
18766 65 64 2e 20 54 68 69 73 20 63 65 6c 6c 20 77 69  ed. This cell wi
18767 6c 6c 20 72 65 70 6c 61 63 65 20 74 68 65 20 63  ll replace the c
18768 65 6c 6c 20 62 65 69 6e 67 20 64 65 6c 65 74 65  ell being delete
18769 64 0a 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20  d.  ** from the 
1876a 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 2e 20 54  internal node. T
1876b 68 65 20 27 70 72 65 76 69 6f 75 73 27 20 65 6e  he 'previous' en
1876c 74 72 79 20 69 73 20 75 73 65 64 20 66 6f 72 20  try is used for 
1876d 74 68 69 73 20 69 6e 73 74 65 61 64 0a 20 20 2a  this instead.  *
1876e 2a 20 6f 66 20 74 68 65 20 27 6e 65 78 74 27 20  * of the 'next' 
1876f 65 6e 74 72 79 2c 20 61 73 20 74 68 65 20 70 72  entry, as the pr
18770 65 76 69 6f 75 73 20 65 6e 74 72 79 20 69 73 20  evious entry is 
18771 61 6c 77 61 79 73 20 61 20 70 61 72 74 20 6f 66  always a part of
18772 20 74 68 65 0a 20 20 2a 2a 20 73 75 62 2d 74 72   the.  ** sub-tr
18773 65 65 20 68 65 61 64 65 64 20 62 79 20 74 68 65  ee headed by the
18774 20 63 68 69 6c 64 20 70 61 67 65 20 6f 66 20 74   child page of t
18775 68 65 20 63 65 6c 6c 20 62 65 69 6e 67 20 64 65  he cell being de
18776 6c 65 74 65 64 2e 20 54 68 69 73 20 6d 61 6b 65  leted. This make
18777 73 0a 20 20 2a 2a 20 62 61 6c 61 6e 63 69 6e 67  s.  ** balancing
18778 20 74 68 65 20 74 72 65 65 20 66 6f 6c 6c 6f 77   the tree follow
18779 69 6e 67 20 74 68 65 20 64 65 6c 65 74 65 20 6f  ing the delete o
1877a 70 65 72 61 74 69 6f 6e 20 65 61 73 69 65 72 2e  peration easier.
1877b 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 50 61 67    */.  if( !pPag
1877c 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 69  e->leaf ){.    i
1877d 6e 74 20 6e 6f 74 55 73 65 64 3b 0a 20 20 20 20  nt notUsed;.    
1877e 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1877f 65 50 72 65 76 69 6f 75 73 28 70 43 75 72 2c 20  ePrevious(pCur, 
18780 26 6e 6f 74 55 73 65 64 29 3b 0a 20 20 20 20 69  &notUsed);.    i
18781 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
18782 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 61 76  c;.  }..  /* Sav
18783 65 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20  e the positions 
18784 6f 66 20 61 6e 79 20 6f 74 68 65 72 20 63 75 72  of any other cur
18785 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 68 69  sors open on thi
18786 73 20 74 61 62 6c 65 20 62 65 66 6f 72 65 0a 20  s table before. 
18787 20 2a 2a 20 6d 61 6b 69 6e 67 20 61 6e 79 20 6d   ** making any m
18788 6f 64 69 66 69 63 61 74 69 6f 6e 73 2e 20 4d 61  odifications. Ma
18789 6b 65 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74  ke the page cont
1878a 61 69 6e 69 6e 67 20 74 68 65 20 65 6e 74 72 79  aining the entry
1878b 20 74 6f 20 62 65 20 0a 20 20 2a 2a 20 64 65 6c   to be .  ** del
1878c 65 74 65 64 20 77 72 69 74 61 62 6c 65 2e 20 54  eted writable. T
1878d 68 65 6e 20 66 72 65 65 20 61 6e 79 20 6f 76 65  hen free any ove
1878e 72 66 6c 6f 77 20 70 61 67 65 73 20 61 73 73 6f  rflow pages asso
1878f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
18790 0a 20 20 2a 2a 20 65 6e 74 72 79 20 61 6e 64 20  .  ** entry and 
18791 66 69 6e 61 6c 6c 79 20 72 65 6d 6f 76 65 20 74  finally remove t
18792 68 65 20 63 65 6c 6c 20 69 74 73 65 6c 66 20 66  he cell itself f
18793 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20 70  rom within the p
18794 61 67 65 2e 20 20 0a 20 20 2a 2f 0a 20 20 72 63  age.  .  */.  rc
18795 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72   = saveAllCursor
18796 73 28 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e  s(pBt, pCur->pgn
18797 6f 52 6f 6f 74 2c 20 70 43 75 72 29 3b 0a 20 20  oRoot, pCur);.  
18798 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
18799 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  rc;.  rc = sqlit
1879a 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61  e3PagerWrite(pPa
1879b 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  ge->pDbPage);.  
1879c 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
1879d 72 63 3b 0a 20 20 72 63 20 3d 20 63 6c 65 61 72  rc;.  rc = clear
1879e 43 65 6c 6c 28 70 50 61 67 65 2c 20 70 43 65 6c  Cell(pPage, pCel
1879f 6c 29 3b 0a 20 20 64 72 6f 70 43 65 6c 6c 28 70  l);.  dropCell(p
187a0 50 61 67 65 2c 20 69 43 65 6c 6c 49 64 78 2c 20  Page, iCellIdx, 
187a1 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67  cellSizePtr(pPag
187a2 65 2c 20 70 43 65 6c 6c 29 2c 20 26 72 63 29 3b  e, pCell), &rc);
187a3 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75  .  if( rc ) retu
187a4 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 49 66 20  rn rc;..  /* If 
187a5 74 68 65 20 63 65 6c 6c 20 64 65 6c 65 74 65 64  the cell deleted
187a6 20 77 61 73 20 6e 6f 74 20 6c 6f 63 61 74 65 64   was not located
187a7 20 6f 6e 20 61 20 6c 65 61 66 20 70 61 67 65 2c   on a leaf page,
187a8 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72   then the cursor
187a9 0a 20 20 2a 2a 20 69 73 20 63 75 72 72 65 6e 74  .  ** is current
187aa 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74  ly pointing to t
187ab 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 79  he largest entry
187ac 20 69 6e 20 74 68 65 20 73 75 62 2d 74 72 65 65   in the sub-tree
187ad 20 68 65 61 64 65 64 0a 20 20 2a 2a 20 62 79 20   headed.  ** by 
187ae 74 68 65 20 63 68 69 6c 64 2d 70 61 67 65 20 6f  the child-page o
187af 66 20 74 68 65 20 63 65 6c 6c 20 74 68 61 74 20  f the cell that 
187b0 77 61 73 20 6a 75 73 74 20 64 65 6c 65 74 65 64  was just deleted
187b1 20 66 72 6f 6d 20 61 6e 20 69 6e 74 65 72 6e 61   from an interna
187b2 6c 0a 20 20 2a 2a 20 6e 6f 64 65 2e 20 54 68 65  l.  ** node. The
187b3 20 63 65 6c 6c 20 66 72 6f 6d 20 74 68 65 20 6c   cell from the l
187b4 65 61 66 20 6e 6f 64 65 20 6e 65 65 64 73 20 74  eaf node needs t
187b5 6f 20 62 65 20 6d 6f 76 65 64 20 74 6f 20 74 68  o be moved to th
187b6 65 20 69 6e 74 65 72 6e 61 6c 0a 20 20 2a 2a 20  e internal.  ** 
187b7 6e 6f 64 65 20 74 6f 20 72 65 70 6c 61 63 65 20  node to replace 
187b8 74 68 65 20 64 65 6c 65 74 65 64 20 63 65 6c 6c  the deleted cell
187b9 2e 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 50 61  .  */.  if( !pPa
187ba 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20  ge->leaf ){.    
187bb 4d 65 6d 50 61 67 65 20 2a 70 4c 65 61 66 20 3d  MemPage *pLeaf =
187bc 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
187bd 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20  ur->iPage];.    
187be 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 20 20 50  int nCell;.    P
187bf 67 6e 6f 20 6e 20 3d 20 70 43 75 72 2d 3e 61 70  gno n = pCur->ap
187c0 50 61 67 65 5b 69 43 65 6c 6c 44 65 70 74 68 2b  Page[iCellDepth+
187c1 31 5d 2d 3e 70 67 6e 6f 3b 0a 20 20 20 20 75 6e  1]->pgno;.    un
187c2 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 54 6d  signed char *pTm
187c3 70 3b 0a 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20  p;..    pCell = 
187c4 66 69 6e 64 43 65 6c 6c 28 70 4c 65 61 66 2c 20  findCell(pLeaf, 
187c5 70 4c 65 61 66 2d 3e 6e 43 65 6c 6c 2d 31 29 3b  pLeaf->nCell-1);
187c6 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20 63 65 6c  .    nCell = cel
187c7 6c 53 69 7a 65 50 74 72 28 70 4c 65 61 66 2c 20  lSizePtr(pLeaf, 
187c8 70 43 65 6c 6c 29 3b 0a 20 20 20 20 61 73 73 65  pCell);.    asse
187c9 72 74 28 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45  rt( MX_CELL_SIZE
187ca 28 70 42 74 29 3e 3d 6e 43 65 6c 6c 20 29 3b 0a  (pBt)>=nCell );.
187cb 0a 20 20 20 20 61 6c 6c 6f 63 61 74 65 54 65 6d  .    allocateTem
187cc 70 53 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20  pSpace(pBt);.   
187cd 20 70 54 6d 70 20 3d 20 70 42 74 2d 3e 70 54 6d   pTmp = pBt->pTm
187ce 70 53 70 61 63 65 3b 0a 0a 20 20 20 20 72 63 20  pSpace;..    rc 
187cf 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
187d0 69 74 65 28 70 4c 65 61 66 2d 3e 70 44 62 50 61  ite(pLeaf->pDbPa
187d1 67 65 29 3b 0a 20 20 20 20 69 6e 73 65 72 74 43  ge);.    insertC
187d2 65 6c 6c 28 70 50 61 67 65 2c 20 69 43 65 6c 6c  ell(pPage, iCell
187d3 49 64 78 2c 20 70 43 65 6c 6c 2d 34 2c 20 6e 43  Idx, pCell-4, nC
187d4 65 6c 6c 2b 34 2c 20 70 54 6d 70 2c 20 6e 2c 20  ell+4, pTmp, n, 
187d5 26 72 63 29 3b 0a 20 20 20 20 64 72 6f 70 43 65  &rc);.    dropCe
187d6 6c 6c 28 70 4c 65 61 66 2c 20 70 4c 65 61 66 2d  ll(pLeaf, pLeaf-
187d7 3e 6e 43 65 6c 6c 2d 31 2c 20 6e 43 65 6c 6c 2c  >nCell-1, nCell,
187d8 20 26 72 63 29 3b 0a 20 20 20 20 69 66 28 20 72   &rc);.    if( r
187d9 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
187da 20 7d 0a 0a 20 20 2f 2a 20 42 61 6c 61 6e 63 65   }..  /* Balance
187db 20 74 68 65 20 74 72 65 65 2e 20 49 66 20 74 68   the tree. If th
187dc 65 20 65 6e 74 72 79 20 64 65 6c 65 74 65 64 20  e entry deleted 
187dd 77 61 73 20 6c 6f 63 61 74 65 64 20 6f 6e 20 61  was located on a
187de 20 6c 65 61 66 20 70 61 67 65 2c 0a 20 20 2a 2a   leaf page,.  **
187df 20 74 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72   then the cursor
187e0 20 73 74 69 6c 6c 20 70 6f 69 6e 74 73 20 74 6f   still points to
187e1 20 74 68 61 74 20 70 61 67 65 2e 20 49 6e 20 74   that page. In t
187e2 68 69 73 20 63 61 73 65 20 74 68 65 20 66 69 72  his case the fir
187e3 73 74 0a 20 20 2a 2a 20 63 61 6c 6c 20 74 6f 20  st.  ** call to 
187e4 62 61 6c 61 6e 63 65 28 29 20 72 65 70 61 69 72  balance() repair
187e5 73 20 74 68 65 20 74 72 65 65 2c 20 61 6e 64 20  s the tree, and 
187e6 74 68 65 20 69 66 28 2e 2e 2e 29 20 63 6f 6e 64  the if(...) cond
187e7 69 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 6e 65  ition is.  ** ne
187e8 76 65 72 20 74 72 75 65 2e 0a 20 20 2a 2a 0a 20  ver true..  **. 
187e9 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 69   ** Otherwise, i
187ea 66 20 74 68 65 20 65 6e 74 72 79 20 64 65 6c 65  f the entry dele
187eb 74 65 64 20 77 61 73 20 6f 6e 20 61 6e 20 69 6e  ted was on an in
187ec 74 65 72 6e 61 6c 20 6e 6f 64 65 20 70 61 67 65  ternal node page
187ed 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 70 43 75 72  , then.  ** pCur
187ee 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20   is pointing to 
187ef 74 68 65 20 6c 65 61 66 20 70 61 67 65 20 66 72  the leaf page fr
187f0 6f 6d 20 77 68 69 63 68 20 61 20 63 65 6c 6c 20  om which a cell 
187f1 77 61 73 20 72 65 6d 6f 76 65 64 20 74 6f 0a 20  was removed to. 
187f2 20 2a 2a 20 72 65 70 6c 61 63 65 20 74 68 65 20   ** replace the 
187f3 63 65 6c 6c 20 64 65 6c 65 74 65 64 20 66 72 6f  cell deleted fro
187f4 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6e  m the internal n
187f5 6f 64 65 2e 20 54 68 69 73 20 69 73 20 73 6c 69  ode. This is sli
187f6 67 68 74 6c 79 0a 20 20 2a 2a 20 74 72 69 63 6b  ghtly.  ** trick
187f7 79 20 61 73 20 74 68 65 20 6c 65 61 66 20 6e 6f  y as the leaf no
187f8 64 65 20 6d 61 79 20 62 65 20 75 6e 64 65 72 66  de may be underf
187f9 75 6c 6c 2c 20 61 6e 64 20 74 68 65 20 69 6e 74  ull, and the int
187fa 65 72 6e 61 6c 20 6e 6f 64 65 20 6d 61 79 0a 20  ernal node may. 
187fb 20 2a 2a 20 62 65 20 65 69 74 68 65 72 20 75 6e   ** be either un
187fc 64 65 72 20 6f 72 20 6f 76 65 72 66 75 6c 6c 2e  der or overfull.
187fd 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 72 75   In this case ru
187fe 6e 20 74 68 65 20 62 61 6c 61 6e 63 69 6e 67 20  n the balancing 
187ff 61 6c 67 6f 72 69 74 68 6d 0a 20 20 2a 2a 20 6f  algorithm.  ** o
18800 6e 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65 20  n the leaf node 
18801 66 69 72 73 74 2e 20 49 66 20 74 68 65 20 62 61  first. If the ba
18802 6c 61 6e 63 65 20 70 72 6f 63 65 65 64 73 20 66  lance proceeds f
18803 61 72 20 65 6e 6f 75 67 68 20 75 70 20 74 68 65  ar enough up the
18804 0a 20 20 2a 2a 20 74 72 65 65 20 74 68 61 74 20  .  ** tree that 
18805 77 65 20 63 61 6e 20 62 65 20 73 75 72 65 20 74  we can be sure t
18806 68 61 74 20 61 6e 79 20 70 72 6f 62 6c 65 6d 20  hat any problem 
18807 69 6e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  in the internal 
18808 6e 6f 64 65 20 68 61 73 0a 20 20 2a 2a 20 62 65  node has.  ** be
18809 65 6e 20 63 6f 72 72 65 63 74 65 64 2c 20 73 6f  en corrected, so
1880a 20 62 65 20 69 74 2e 20 4f 74 68 65 72 77 69 73   be it. Otherwis
1880b 65 2c 20 61 66 74 65 72 20 62 61 6c 61 6e 63 69  e, after balanci
1880c 6e 67 20 74 68 65 20 6c 65 61 66 20 6e 6f 64 65  ng the leaf node
1880d 2c 0a 20 20 2a 2a 20 77 61 6c 6b 20 74 68 65 20  ,.  ** walk the 
1880e 63 75 72 73 6f 72 20 75 70 20 74 68 65 20 74 72  cursor up the tr
1880f 65 65 20 74 6f 20 74 68 65 20 69 6e 74 65 72 6e  ee to the intern
18810 61 6c 20 6e 6f 64 65 20 61 6e 64 20 62 61 6c 61  al node and bala
18811 6e 63 65 20 69 74 20 61 73 20 0a 20 20 2a 2a 20  nce it as .  ** 
18812 77 65 6c 6c 2e 20 20 2a 2f 0a 20 20 72 63 20 3d  well.  */.  rc =
18813 20 62 61 6c 61 6e 63 65 28 70 43 75 72 29 3b 0a   balance(pCur);.
18814 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
18815 5f 4f 4b 20 26 26 20 70 43 75 72 2d 3e 69 50 61  _OK && pCur->iPa
18816 67 65 3e 69 43 65 6c 6c 44 65 70 74 68 20 29 7b  ge>iCellDepth ){
18817 0a 20 20 20 20 77 68 69 6c 65 28 20 70 43 75 72  .    while( pCur
18818 2d 3e 69 50 61 67 65 3e 69 43 65 6c 6c 44 65 70  ->iPage>iCellDep
18819 74 68 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65  th ){.      rele
1881a 61 73 65 50 61 67 65 28 70 43 75 72 2d 3e 61 70  asePage(pCur->ap
1881b 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
1881c 2d 2d 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  --]);.    }.    
1881d 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75  rc = balance(pCu
1881e 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72  r);.  }..  if( r
1881f 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
18820 20 20 20 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70      moveToRoot(p
18821 43 75 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Cur);.  }.  retu
18822 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
18823 43 72 65 61 74 65 20 61 20 6e 65 77 20 42 54 72  Create a new BTr
18824 65 65 20 74 61 62 6c 65 2e 20 20 57 72 69 74 65  ee table.  Write
18825 20 69 6e 74 6f 20 2a 70 69 54 61 62 6c 65 20 74   into *piTable t
18826 68 65 20 70 61 67 65 0a 2a 2a 20 6e 75 6d 62 65  he page.** numbe
18827 72 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 20 70  r for the root p
18828 61 67 65 20 6f 66 20 74 68 65 20 6e 65 77 20 74  age of the new t
18829 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  able..**.** The 
1882a 74 79 70 65 20 6f 66 20 74 79 70 65 20 69 73 20  type of type is 
1882b 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68  determined by th
1882c 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65  e flags paramete
1882d 72 2e 20 20 4f 6e 6c 79 20 74 68 65 0a 2a 2a 20  r.  Only the.** 
1882e 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73  following values
1882f 20 6f 66 20 66 6c 61 67 73 20 61 72 65 20 63 75   of flags are cu
18830 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e 20  rrently in use. 
18831 20 4f 74 68 65 72 20 76 61 6c 75 65 73 20 66 6f   Other values fo
18832 72 0a 2a 2a 20 66 6c 61 67 73 20 6d 69 67 68 74  r.** flags might
18833 20 6e 6f 74 20 77 6f 72 6b 3a 0a 2a 2a 0a 2a 2a   not work:.**.**
18834 20 20 20 20 20 42 54 52 45 45 5f 49 4e 54 4b 45       BTREE_INTKE
18835 59 7c 42 54 52 45 45 5f 4c 45 41 46 44 41 54 41  Y|BTREE_LEAFDATA
18836 20 20 20 20 20 55 73 65 64 20 66 6f 72 20 53 51       Used for SQ
18837 4c 20 74 61 62 6c 65 73 20 77 69 74 68 20 72 6f  L tables with ro
18838 77 69 64 20 6b 65 79 73 0a 2a 2a 20 20 20 20 20  wid keys.**     
18839 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 20 20  BTREE_ZERODATA  
1883a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1883b 55 73 65 64 20 66 6f 72 20 53 51 4c 20 69 6e 64  Used for SQL ind
1883c 69 63 65 73 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ices.*/.static i
1883d 6e 74 20 62 74 72 65 65 43 72 65 61 74 65 54 61  nt btreeCreateTa
1883e 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e  ble(Btree *p, in
1883f 74 20 2a 70 69 54 61 62 6c 65 2c 20 69 6e 74 20  t *piTable, int 
18840 66 6c 61 67 73 29 7b 0a 20 20 42 74 53 68 61 72  flags){.  BtShar
18841 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
18842 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 52 6f  ;.  MemPage *pRo
18843 6f 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 52  ot;.  Pgno pgnoR
18844 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  oot;.  int rc;..
18845 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
18846 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78  3BtreeHoldsMutex
18847 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  (p) );.  assert(
18848 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74   pBt->inTransact
18849 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ion==TRANS_WRITE
1884a 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70   );.  assert( !p
1884b 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 3b 0a  Bt->readOnly );.
1884c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
1884d 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
1884e 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74   rc = allocateBt
1884f 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 52  reePage(pBt, &pR
18850 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f 6f 74 2c 20  oot, &pgnoRoot, 
18851 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20  1, 0);.  if( rc 
18852 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
18853 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69 66  ;.  }.#else.  if
18854 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
18855 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 70 67  m ){.    Pgno pg
18856 6e 6f 4d 6f 76 65 3b 20 20 20 20 20 20 2f 2a 20  noMove;      /* 
18857 4d 6f 76 65 20 61 20 70 61 67 65 20 68 65 72 65  Move a page here
18858 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f   to make room fo
18859 72 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20  r the root-page 
1885a 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a  */.    MemPage *
1885b 70 50 61 67 65 4d 6f 76 65 3b 20 2f 2a 20 54 68  pPageMove; /* Th
1885c 65 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 74  e page to move t
1885d 6f 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 43 72  o. */..    /* Cr
1885e 65 61 74 69 6e 67 20 61 20 6e 65 77 20 74 61 62  eating a new tab
1885f 6c 65 20 6d 61 79 20 70 72 6f 62 61 62 6c 79 20  le may probably 
18860 72 65 71 75 69 72 65 20 6d 6f 76 69 6e 67 20 61  require moving a
18861 6e 20 65 78 69 73 74 69 6e 67 20 64 61 74 61 62  n existing datab
18862 61 73 65 0a 20 20 20 20 2a 2a 20 74 6f 20 6d 61  ase.    ** to ma
18863 6b 65 20 72 6f 6f 6d 20 66 6f 72 20 74 68 65 20  ke room for the 
18864 6e 65 77 20 74 61 62 6c 65 73 20 72 6f 6f 74 20  new tables root 
18865 70 61 67 65 2e 20 49 6e 20 63 61 73 65 20 74 68  page. In case th
18866 69 73 20 70 61 67 65 20 74 75 72 6e 73 0a 20 20  is page turns.  
18867 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62 65 20 61    ** out to be a
18868 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2c  n overflow page,
18869 20 64 65 6c 65 74 65 20 61 6c 6c 20 6f 76 65 72   delete all over
1886a 66 6c 6f 77 20 70 61 67 65 2d 6d 61 70 20 63 61  flow page-map ca
1886b 63 68 65 73 0a 20 20 20 20 2a 2a 20 68 65 6c 64  ches.    ** held
1886c 20 62 79 20 6f 70 65 6e 20 63 75 72 73 6f 72 73   by open cursors
1886d 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 76  ..    */.    inv
1886e 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c  alidateAllOverfl
1886f 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a 0a 20  owCache(pBt);.. 
18870 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 76     /* Read the v
18871 61 6c 75 65 20 6f 66 20 6d 65 74 61 5b 33 5d 20  alue of meta[3] 
18872 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
18873 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77  e to determine w
18874 68 65 72 65 20 74 68 65 0a 20 20 20 20 2a 2a 20  here the.    ** 
18875 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 65  root page of the
18876 20 6e 65 77 20 74 61 62 6c 65 20 73 68 6f 75 6c   new table shoul
18877 64 20 67 6f 2e 20 6d 65 74 61 5b 33 5d 20 69 73  d go. meta[3] is
18878 20 74 68 65 20 6c 61 72 67 65 73 74 20 72 6f 6f   the largest roo
18879 74 2d 70 61 67 65 0a 20 20 20 20 2a 2a 20 63 72  t-page.    ** cr
1887a 65 61 74 65 64 20 73 6f 20 66 61 72 2c 20 73 6f  eated so far, so
1887b 20 74 68 65 20 6e 65 77 20 72 6f 6f 74 2d 70 61   the new root-pa
1887c 67 65 20 69 73 20 28 6d 65 74 61 5b 33 5d 2b 31  ge is (meta[3]+1
1887d 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  )..    */.    sq
1887e 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74  lite3BtreeGetMet
1887f 61 28 70 2c 20 42 54 52 45 45 5f 4c 41 52 47 45  a(p, BTREE_LARGE
18880 53 54 5f 52 4f 4f 54 5f 50 41 47 45 2c 20 26 70  ST_ROOT_PAGE, &p
18881 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 70 67  gnoRoot);.    pg
18882 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a 20 20 20 20 2f  noRoot++;..    /
18883 2a 20 54 68 65 20 6e 65 77 20 72 6f 6f 74 2d 70  * The new root-p
18884 61 67 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 61  age may not be a
18885 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 61 20 70 6f  llocated on a po
18886 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20  inter-map page, 
18887 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 50 45  or the.    ** PE
18888 4e 44 49 4e 47 5f 42 59 54 45 20 70 61 67 65 2e  NDING_BYTE page.
18889 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68 69 6c  .    */.    whil
1888a 65 28 20 70 67 6e 6f 52 6f 6f 74 3d 3d 50 54 52  e( pgnoRoot==PTR
1888b 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20  MAP_PAGENO(pBt, 
1888c 70 67 6e 6f 52 6f 6f 74 29 20 7c 7c 0a 20 20 20  pgnoRoot) ||.   
1888d 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 3d 3d 50       pgnoRoot==P
1888e 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
1888f 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 70  (pBt) ){.      p
18890 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 20 20 20 20 7d  gnoRoot++;.    }
18891 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 67 6e  .    assert( pgn
18892 6f 52 6f 6f 74 3e 3d 33 20 29 3b 0a 0a 20 20 20  oRoot>=3 );..   
18893 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 70   /* Allocate a p
18894 61 67 65 2e 20 54 68 65 20 70 61 67 65 20 74 68  age. The page th
18895 61 74 20 63 75 72 72 65 6e 74 6c 79 20 72 65 73  at currently res
18896 69 64 65 73 20 61 74 20 70 67 6e 6f 52 6f 6f 74  ides at pgnoRoot
18897 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 62 65 20   will.    ** be 
18898 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 61 6c 6c  moved to the all
18899 6f 63 61 74 65 64 20 70 61 67 65 20 28 75 6e 6c  ocated page (unl
1889a 65 73 73 20 74 68 65 20 61 6c 6c 6f 63 61 74 65  ess the allocate
1889b 64 20 70 61 67 65 20 68 61 70 70 65 6e 73 0a 20  d page happens. 
1889c 20 20 20 2a 2a 20 74 6f 20 72 65 73 69 64 65 20     ** to reside 
1889d 61 74 20 70 67 6e 6f 52 6f 6f 74 29 2e 0a 20 20  at pgnoRoot)..  
1889e 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 61 6c    */.    rc = al
1889f 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28  locateBtreePage(
188a0 70 42 74 2c 20 26 70 50 61 67 65 4d 6f 76 65 2c  pBt, &pPageMove,
188a1 20 26 70 67 6e 6f 4d 6f 76 65 2c 20 70 67 6e 6f   &pgnoMove, pgno
188a2 52 6f 6f 74 2c 20 31 29 3b 0a 20 20 20 20 69 66  Root, 1);.    if
188a3 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
188a4 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
188a5 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  rc;.    }..    i
188a6 66 28 20 70 67 6e 6f 4d 6f 76 65 21 3d 70 67 6e  f( pgnoMove!=pgn
188a7 6f 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20 20 2f  oRoot ){.      /
188a8 2a 20 70 67 6e 6f 52 6f 6f 74 20 69 73 20 74 68  * pgnoRoot is th
188a9 65 20 70 61 67 65 20 74 68 61 74 20 77 69 6c 6c  e page that will
188aa 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65   be used for the
188ab 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 0a 20 20   root-page of.  
188ac 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 77 20 74      ** the new t
188ad 61 62 6c 65 20 28 61 73 73 75 6d 69 6e 67 20 61  able (assuming a
188ae 6e 20 65 72 72 6f 72 20 64 69 64 20 6e 6f 74 20  n error did not 
188af 6f 63 63 75 72 29 2e 20 42 75 74 20 77 65 20 77  occur). But we w
188b0 65 72 65 0a 20 20 20 20 20 20 2a 2a 20 61 6c 6c  ere.      ** all
188b1 6f 63 61 74 65 64 20 70 67 6e 6f 4d 6f 76 65 2e  ocated pgnoMove.
188b2 20 49 66 20 72 65 71 75 69 72 65 64 20 28 69 2e   If required (i.
188b3 65 2e 20 69 66 20 69 74 20 77 61 73 20 6e 6f 74  e. if it was not
188b4 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 20 20 20   allocated.     
188b5 20 2a 2a 20 62 79 20 65 78 74 65 6e 64 69 6e 67   ** by extending
188b6 20 74 68 65 20 66 69 6c 65 29 2c 20 74 68 65 20   the file), the 
188b7 63 75 72 72 65 6e 74 20 70 61 67 65 20 61 74 20  current page at 
188b8 70 6f 73 69 74 69 6f 6e 20 70 67 6e 6f 4d 6f 76  position pgnoMov
188b9 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 61 6c  e.      ** is al
188ba 72 65 61 64 79 20 6a 6f 75 72 6e 61 6c 65 64 2e  ready journaled.
188bb 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
188bc 75 38 20 65 54 79 70 65 20 3d 20 30 3b 0a 20 20  u8 eType = 0;.  
188bd 20 20 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67      Pgno iPtrPag
188be 65 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 72 65  e = 0;..      re
188bf 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 4d  leasePage(pPageM
188c0 6f 76 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  ove);..      /* 
188c1 4d 6f 76 65 20 74 68 65 20 70 61 67 65 20 63 75  Move the page cu
188c2 72 72 65 6e 74 6c 79 20 61 74 20 70 67 6e 6f 52  rrently at pgnoR
188c3 6f 6f 74 20 74 6f 20 70 67 6e 6f 4d 6f 76 65 2e  oot to pgnoMove.
188c4 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 62   */.      rc = b
188c5 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
188c6 20 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f 6f   pgnoRoot, &pRoo
188c7 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  t, 0);.      if(
188c8 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
188c9 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
188ca 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   rc;.      }.   
188cb 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65     rc = ptrmapGe
188cc 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74 2c  t(pBt, pgnoRoot,
188cd 20 26 65 54 79 70 65 2c 20 26 69 50 74 72 50 61   &eType, &iPtrPa
188ce 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 65  ge);.      if( e
188cf 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f  Type==PTRMAP_ROO
188d0 54 50 41 47 45 20 7c 7c 20 65 54 79 70 65 3d 3d  TPAGE || eType==
188d1 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47 45 20  PTRMAP_FREEPAGE 
188d2 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
188d3 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
188d4 4b 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  KPT;.      }.   
188d5 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
188d6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
188d7 72 65 6c 65 61 73 65 50 61 67 65 28 70 52 6f 6f  releasePage(pRoo
188d8 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  t);.        retu
188d9 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20  rn rc;.      }. 
188da 20 20 20 20 20 61 73 73 65 72 74 28 20 65 54 79       assert( eTy
188db 70 65 21 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50  pe!=PTRMAP_ROOTP
188dc 41 47 45 20 29 3b 0a 20 20 20 20 20 20 61 73 73  AGE );.      ass
188dd 65 72 74 28 20 65 54 79 70 65 21 3d 50 54 52 4d  ert( eType!=PTRM
188de 41 50 5f 46 52 45 45 50 41 47 45 20 29 3b 0a 20  AP_FREEPAGE );. 
188df 20 20 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61       rc = reloca
188e0 74 65 50 61 67 65 28 70 42 74 2c 20 70 52 6f 6f  tePage(pBt, pRoo
188e1 74 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50 61  t, eType, iPtrPa
188e2 67 65 2c 20 70 67 6e 6f 4d 6f 76 65 2c 20 30 29  ge, pgnoMove, 0)
188e3 3b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50  ;.      releaseP
188e4 61 67 65 28 70 52 6f 6f 74 29 3b 0a 0a 20 20 20  age(pRoot);..   
188e5 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68 65     /* Obtain the
188e6 20 70 61 67 65 20 61 74 20 70 67 6e 6f 52 6f 6f   page at pgnoRoo
188e7 74 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72  t */.      if( r
188e8 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
188e9 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
188ea 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  c;.      }.     
188eb 20 72 63 20 3d 20 62 74 72 65 65 47 65 74 50 61   rc = btreeGetPa
188ec 67 65 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74  ge(pBt, pgnoRoot
188ed 2c 20 26 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20  , &pRoot, 0);.  
188ee 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
188ef 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
188f0 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
188f1 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 73    }.      rc = s
188f2 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
188f3 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29  (pRoot->pDbPage)
188f4 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
188f5 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
188f6 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
188f7 28 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 20  (pRoot);.       
188f8 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
188f9 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
188fa 20 20 20 20 20 70 52 6f 6f 74 20 3d 20 70 50 61       pRoot = pPa
188fb 67 65 4d 6f 76 65 3b 0a 20 20 20 20 7d 20 0a 0a  geMove;.    } ..
188fc 20 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68      /* Update th
188fd 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 61 6e  e pointer-map an
188fe 64 20 6d 65 74 61 2d 64 61 74 61 20 77 69 74 68  d meta-data with
188ff 20 74 68 65 20 6e 65 77 20 72 6f 6f 74 2d 70 61   the new root-pa
18900 67 65 20 6e 75 6d 62 65 72 2e 20 2a 2f 0a 20 20  ge number. */.  
18901 20 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c    ptrmapPut(pBt,
18902 20 70 67 6e 6f 52 6f 6f 74 2c 20 50 54 52 4d 41   pgnoRoot, PTRMA
18903 50 5f 52 4f 4f 54 50 41 47 45 2c 20 30 2c 20 26  P_ROOTPAGE, 0, &
18904 72 63 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  rc);.    if( rc 
18905 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  ){.      release
18906 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20  Page(pRoot);.   
18907 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
18908 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c    }.    rc = sql
18909 69 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d  ite3BtreeUpdateM
1890a 65 74 61 28 70 2c 20 34 2c 20 70 67 6e 6f 52 6f  eta(p, 4, pgnoRo
1890b 6f 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  ot);.    if( rc 
1890c 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  ){.      release
1890d 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20  Page(pRoot);.   
1890e 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
1890f 20 20 7d 0a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20    }..  }else{.  
18910 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42    rc = allocateB
18911 74 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70  treePage(pBt, &p
18912 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f 6f 74 2c  Root, &pgnoRoot,
18913 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20   1, 0);.    if( 
18914 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
18915 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 61 73 73    }.#endif.  ass
18916 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65  ert( sqlite3Page
18917 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 52 6f  rIswriteable(pRo
18918 6f 74 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  ot->pDbPage) );.
18919 20 20 7a 65 72 6f 50 61 67 65 28 70 52 6f 6f 74    zeroPage(pRoot
1891a 2c 20 66 6c 61 67 73 20 7c 20 50 54 46 5f 4c 45  , flags | PTF_LE
1891b 41 46 29 3b 0a 20 20 73 71 6c 69 74 65 33 50 61  AF);.  sqlite3Pa
1891c 67 65 72 55 6e 72 65 66 28 70 52 6f 6f 74 2d 3e  gerUnref(pRoot->
1891d 70 44 62 50 61 67 65 29 3b 0a 20 20 2a 70 69 54  pDbPage);.  *piT
1891e 61 62 6c 65 20 3d 20 28 69 6e 74 29 70 67 6e 6f  able = (int)pgno
1891f 52 6f 6f 74 3b 0a 20 20 72 65 74 75 72 6e 20 53  Root;.  return S
18920 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49  QLITE_OK;.}.SQLI
18921 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
18922 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74  qlite3BtreeCreat
18923 65 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c  eTable(Btree *p,
18924 20 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c 20 69   int *piTable, i
18925 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74  nt flags){.  int
18926 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74   rc;.  sqlite3Bt
18927 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72  reeEnter(p);.  r
18928 63 20 3d 20 62 74 72 65 65 43 72 65 61 74 65 54  c = btreeCreateT
18929 61 62 6c 65 28 70 2c 20 70 69 54 61 62 6c 65 2c  able(p, piTable,
1892a 20 66 6c 61 67 73 29 3b 0a 20 20 73 71 6c 69 74   flags);.  sqlit
1892b 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
1892c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1892d 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20 74 68 65  ./*.** Erase the
1892e 20 67 69 76 65 6e 20 64 61 74 61 62 61 73 65 20   given database 
1892f 70 61 67 65 20 61 6e 64 20 61 6c 6c 20 69 74 73  page and all its
18930 20 63 68 69 6c 64 72 65 6e 2e 20 20 52 65 74 75   children.  Retu
18931 72 6e 0a 2a 2a 20 74 68 65 20 70 61 67 65 20 74  rn.** the page t
18932 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 0a  o the freelist..
18933 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6c  */.static int cl
18934 65 61 72 44 61 74 61 62 61 73 65 50 61 67 65 28  earDatabasePage(
18935 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
18936 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ,           /* T
18937 68 65 20 42 54 72 65 65 20 74 68 61 74 20 63 6f  he BTree that co
18938 6e 74 61 69 6e 73 20 74 68 65 20 74 61 62 6c 65  ntains the table
18939 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c   */.  Pgno pgno,
1893a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
1893b 61 67 65 20 6e 75 6d 62 65 72 20 74 6f 20 63 6c  age number to cl
1893c 65 61 72 20 2a 2f 0a 20 20 69 6e 74 20 66 72 65  ear */.  int fre
1893d 65 50 61 67 65 46 6c 61 67 2c 20 20 20 20 20 2f  ePageFlag,     /
1893e 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 70 61 67  * Deallocate pag
1893f 65 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 69  e if true */.  i
18940 6e 74 20 2a 70 6e 43 68 61 6e 67 65 0a 29 7b 0a  nt *pnChange.){.
18941 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
18942 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e  ;.  int rc;.  un
18943 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65  signed char *pCe
18944 6c 6c 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20  ll;.  int i;..  
18945 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
18946 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
18947 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
18948 70 67 6e 6f 3e 70 61 67 65 72 50 61 67 65 63 6f  pgno>pagerPageco
18949 75 6e 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20  unt(pBt) ){.    
1894a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
1894b 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a  RRUPT_BKPT;.  }.
1894c 0a 20 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e  .  rc = getAndIn
1894d 69 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f  itPage(pBt, pgno
1894e 2c 20 26 70 50 61 67 65 29 3b 0a 20 20 69 66 28  , &pPage);.  if(
1894f 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
18950 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50  .  for(i=0; i<pP
18951 61 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29  age->nCell; i++)
18952 7b 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69  {.    pCell = fi
18953 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29  ndCell(pPage, i)
18954 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65  ;.    if( !pPage
18955 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20  ->leaf ){.      
18956 72 63 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61  rc = clearDataba
18957 73 65 50 61 67 65 28 70 42 74 2c 20 67 65 74 34  sePage(pBt, get4
18958 62 79 74 65 28 70 43 65 6c 6c 29 2c 20 31 2c 20  byte(pCell), 1, 
18959 70 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 20  pnChange);.     
1895a 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63   if( rc ) goto c
1895b 6c 65 61 72 64 61 74 61 62 61 73 65 70 61 67 65  leardatabasepage
1895c 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _out;.    }.    
1895d 72 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70  rc = clearCell(p
1895e 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20  Page, pCell);.  
1895f 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20    if( rc ) goto 
18960 63 6c 65 61 72 64 61 74 61 62 61 73 65 70 61 67  cleardatabasepag
18961 65 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28  e_out;.  }.  if(
18962 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
18963 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44  .    rc = clearD
18964 61 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c  atabasePage(pBt,
18965 20 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65   get4byte(&pPage
18966 2d 3e 61 44 61 74 61 5b 38 5d 29 2c 20 31 2c 20  ->aData[8]), 1, 
18967 70 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 69  pnChange);.    i
18968 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65  f( rc ) goto cle
18969 61 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f  ardatabasepage_o
1896a 75 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ut;.  }else if( 
1896b 70 6e 43 68 61 6e 67 65 20 29 7b 0a 20 20 20 20  pnChange ){.    
1896c 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69  assert( pPage->i
1896d 6e 74 4b 65 79 20 29 3b 0a 20 20 20 20 2a 70 6e  ntKey );.    *pn
1896e 43 68 61 6e 67 65 20 2b 3d 20 70 50 61 67 65 2d  Change += pPage-
1896f 3e 6e 43 65 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66  >nCell;.  }.  if
18970 28 20 66 72 65 65 50 61 67 65 46 6c 61 67 20 29  ( freePageFlag )
18971 7b 0a 20 20 20 20 66 72 65 65 50 61 67 65 28 70  {.    freePage(p
18972 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 7d 65  Page, &rc);.  }e
18973 6c 73 65 20 69 66 28 20 28 72 63 20 3d 20 73 71  lse if( (rc = sq
18974 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
18975 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 29  pPage->pDbPage))
18976 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 65 72 6f 50  ==0 ){.    zeroP
18977 61 67 65 28 70 50 61 67 65 2c 20 70 50 61 67 65  age(pPage, pPage
18978 2d 3e 61 44 61 74 61 5b 30 5d 20 7c 20 50 54 46  ->aData[0] | PTF
18979 5f 4c 45 41 46 29 3b 0a 20 20 7d 0a 0a 63 6c 65  _LEAF);.  }..cle
1897a 61 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f  ardatabasepage_o
1897b 75 74 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67  ut:.  releasePag
1897c 65 28 70 50 61 67 65 29 3b 0a 20 20 72 65 74 75  e(pPage);.  retu
1897d 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1897e 44 65 6c 65 74 65 20 61 6c 6c 20 69 6e 66 6f 72  Delete all infor
1897f 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 73 69  mation from a si
18980 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 74 68  ngle table in th
18981 65 20 64 61 74 61 62 61 73 65 2e 20 20 69 54 61  e database.  iTa
18982 62 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20 70 61  ble is.** the pa
18983 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
18984 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62   root of the tab
18985 6c 65 2e 20 20 41 66 74 65 72 20 74 68 69 73 20  le.  After this 
18986 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2c  routine returns,
18987 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70 61 67  .** the root pag
18988 65 20 69 73 20 65 6d 70 74 79 2c 20 62 75 74 20  e is empty, but 
18989 73 74 69 6c 6c 20 65 78 69 73 74 73 2e 0a 2a 2a  still exists..**
1898a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1898b 20 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20   will fail with 
1898c 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66  SQLITE_LOCKED if
1898d 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f   there are any o
1898e 70 65 6e 0a 2a 2a 20 72 65 61 64 20 63 75 72 73  pen.** read curs
1898f 6f 72 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  ors on the table
18990 2e 20 20 4f 70 65 6e 20 77 72 69 74 65 20 63 75  .  Open write cu
18991 72 73 6f 72 73 20 61 72 65 20 6d 6f 76 65 64 20  rsors are moved 
18992 74 6f 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 6f  to the.** root o
18993 66 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a  f the table..**.
18994 2a 2a 20 49 66 20 70 6e 43 68 61 6e 67 65 20 69  ** If pnChange i
18995 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
18996 20 74 61 62 6c 65 20 69 54 61 62 6c 65 20 6d 75   table iTable mu
18997 73 74 20 62 65 20 61 6e 20 69 6e 74 6b 65 79 20  st be an intkey 
18998 74 61 62 6c 65 2e 20 54 68 65 0a 2a 2a 20 69 6e  table. The.** in
18999 74 65 67 65 72 20 76 61 6c 75 65 20 70 6f 69 6e  teger value poin
1899a 74 65 64 20 74 6f 20 62 79 20 70 6e 43 68 61 6e  ted to by pnChan
1899b 67 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65  ge is incremente
1899c 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20  d by the number 
1899d 6f 66 0a 2a 2a 20 65 6e 74 72 69 65 73 20 69 6e  of.** entries in
1899e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53   the table..*/.S
1899f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
189a0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c  t sqlite3BtreeCl
189a1 65 61 72 54 61 62 6c 65 28 42 74 72 65 65 20 2a  earTable(Btree *
189a2 70 2c 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69  p, int iTable, i
189a3 6e 74 20 2a 70 6e 43 68 61 6e 67 65 29 7b 0a 20  nt *pnChange){. 
189a4 20 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61   int rc;.  BtSha
189a5 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
189a6 74 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  t;.  sqlite3Btre
189a7 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73  eEnter(p);.  ass
189a8 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d  ert( p->inTrans=
189a9 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a  =TRANS_WRITE );.
189aa 0a 20 20 2f 2a 20 49 6e 76 61 6c 69 64 61 74 65  .  /* Invalidate
189ab 20 61 6c 6c 20 69 6e 63 72 62 6c 6f 62 20 63 75   all incrblob cu
189ac 72 73 6f 72 73 20 6f 70 65 6e 20 6f 6e 20 74 61  rsors open on ta
189ad 62 6c 65 20 69 54 61 62 6c 65 20 28 61 73 73 75  ble iTable (assu
189ae 6d 69 6e 67 20 69 54 61 62 6c 65 0a 20 20 2a 2a  ming iTable.  **
189af 20 69 73 20 74 68 65 20 72 6f 6f 74 20 6f 66 20   is the root of 
189b0 61 20 74 61 62 6c 65 20 62 2d 74 72 65 65 20 2d  a table b-tree -
189b1 20 69 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 74   if it is not, t
189b2 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c  he following cal
189b3 6c 20 69 73 0a 20 20 2a 2a 20 61 20 6e 6f 2d 6f  l is.  ** a no-o
189b4 70 29 2e 20 20 2a 2f 0a 20 20 69 6e 76 61 6c 69  p).  */.  invali
189b5 64 61 74 65 49 6e 63 72 62 6c 6f 62 43 75 72 73  dateIncrblobCurs
189b6 6f 72 73 28 70 2c 20 30 2c 20 31 29 3b 0a 0a 20  ors(p, 0, 1);.. 
189b7 20 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72   rc = saveAllCur
189b8 73 6f 72 73 28 70 42 74 2c 20 28 50 67 6e 6f 29  sors(pBt, (Pgno)
189b9 69 54 61 62 6c 65 2c 20 30 29 3b 0a 20 20 69 66  iTable, 0);.  if
189ba 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20  ( SQLITE_OK==rc 
189bb 29 7b 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61  ){.    rc = clea
189bc 72 44 61 74 61 62 61 73 65 50 61 67 65 28 70 42  rDatabasePage(pB
189bd 74 2c 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c  t, (Pgno)iTable,
189be 20 30 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a 20   0, pnChange);. 
189bf 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65   }.  sqlite3Btre
189c0 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
189c1 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
189c2 20 45 72 61 73 65 20 61 6c 6c 20 69 6e 66 6f 72   Erase all infor
189c3 6d 61 74 69 6f 6e 20 69 6e 20 61 20 74 61 62 6c  mation in a tabl
189c4 65 20 61 6e 64 20 61 64 64 20 74 68 65 20 72 6f  e and add the ro
189c5 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ot of the table 
189c6 74 6f 0a 2a 2a 20 74 68 65 20 66 72 65 65 6c 69  to.** the freeli
189c7 73 74 2e 20 20 45 78 63 65 70 74 2c 20 74 68 65  st.  Except, the
189c8 20 72 6f 6f 74 20 6f 66 20 74 68 65 20 70 72 69   root of the pri
189c9 6e 63 69 70 6c 65 20 74 61 62 6c 65 20 28 74 68  nciple table (th
189ca 65 20 6f 6e 65 20 6f 6e 0a 2a 2a 20 70 61 67 65  e one on.** page
189cb 20 31 29 20 69 73 20 6e 65 76 65 72 20 61 64 64   1) is never add
189cc 65 64 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69  ed to the freeli
189cd 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  st..**.** This r
189ce 6f 75 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 6c  outine will fail
189cf 20 77 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f 43   with SQLITE_LOC
189d0 4b 45 44 20 69 66 20 74 68 65 72 65 20 61 72 65  KED if there are
189d1 20 61 6e 79 20 6f 70 65 6e 0a 2a 2a 20 63 75 72   any open.** cur
189d2 73 6f 72 73 20 6f 6e 20 74 68 65 20 74 61 62 6c  sors on the tabl
189d3 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55 54 4f  e..**.** If AUTO
189d4 56 41 43 55 55 4d 20 69 73 20 65 6e 61 62 6c 65  VACUUM is enable
189d5 64 20 61 6e 64 20 74 68 65 20 70 61 67 65 20 61  d and the page a
189d6 74 20 69 54 61 62 6c 65 20 69 73 20 6e 6f 74 20  t iTable is not 
189d7 74 68 65 20 6c 61 73 74 0a 2a 2a 20 72 6f 6f 74  the last.** root
189d8 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61 74   page in the dat
189d9 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e  abase file, then
189da 20 74 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70   the last root p
189db 61 67 65 20 0a 2a 2a 20 69 6e 20 74 68 65 20 64  age .** in the d
189dc 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73 20  atabase file is 
189dd 6d 6f 76 65 64 20 69 6e 74 6f 20 74 68 65 20 73  moved into the s
189de 6c 6f 74 20 66 6f 72 6d 65 72 6c 79 20 6f 63 63  lot formerly occ
189df 75 70 69 65 64 20 62 79 0a 2a 2a 20 69 54 61 62  upied by.** iTab
189e0 6c 65 20 61 6e 64 20 74 68 61 74 20 6c 61 73 74  le and that last
189e1 20 73 6c 6f 74 20 66 6f 72 6d 65 72 6c 79 20 6f   slot formerly o
189e2 63 63 75 70 69 65 64 20 62 79 20 74 68 65 20 6c  ccupied by the l
189e3 61 73 74 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a  ast root page.**
189e4 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65   is added to the
189e5 20 66 72 65 65 6c 69 73 74 20 69 6e 73 74 65 61   freelist instea
189e6 64 20 6f 66 20 69 54 61 62 6c 65 2e 20 20 49 6e  d of iTable.  In
189e7 20 74 68 69 73 20 73 61 79 2c 20 61 6c 6c 0a 2a   this say, all.*
189e8 2a 20 72 6f 6f 74 20 70 61 67 65 73 20 61 72 65  * root pages are
189e9 20 6b 65 70 74 20 61 74 20 74 68 65 20 62 65 67   kept at the beg
189ea 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 61  inning of the da
189eb 74 61 62 61 73 65 20 66 69 6c 65 2c 20 77 68 69  tabase file, whi
189ec 63 68 0a 2a 2a 20 69 73 20 6e 65 63 65 73 73 61  ch.** is necessa
189ed 72 79 20 66 6f 72 20 41 55 54 4f 56 41 43 55 55  ry for AUTOVACUU
189ee 4d 20 74 6f 20 77 6f 72 6b 20 72 69 67 68 74 2e  M to work right.
189ef 20 20 2a 70 69 4d 6f 76 65 64 20 69 73 20 73 65    *piMoved is se
189f0 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 70 61 67  t to the .** pag
189f1 65 20 6e 75 6d 62 65 72 20 74 68 61 74 20 75 73  e number that us
189f2 65 64 20 74 6f 20 62 65 20 74 68 65 20 6c 61 73  ed to be the las
189f3 74 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74  t root page in t
189f4 68 65 20 66 69 6c 65 20 62 65 66 6f 72 65 0a 2a  he file before.*
189f5 2a 20 74 68 65 20 6d 6f 76 65 2e 20 20 49 66 20  * the move.  If 
189f6 6e 6f 20 70 61 67 65 20 67 65 74 73 20 6d 6f 76  no page gets mov
189f7 65 64 2c 20 2a 70 69 4d 6f 76 65 64 20 69 73 20  ed, *piMoved is 
189f8 73 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 54 68 65  set to 0..** The
189f9 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20   last root page 
189fa 69 73 20 72 65 63 6f 72 64 65 64 20 69 6e 20 6d  is recorded in m
189fb 65 74 61 5b 33 5d 20 61 6e 64 20 74 68 65 20 76  eta[3] and the v
189fc 61 6c 75 65 20 6f 66 0a 2a 2a 20 6d 65 74 61 5b  alue of.** meta[
189fd 33 5d 20 69 73 20 75 70 64 61 74 65 64 20 62 79  3] is updated by
189fe 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65 2e   this procedure.
189ff 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
18a00 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74  treeDropTable(Bt
18a01 72 65 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61  ree *p, Pgno iTa
18a02 62 6c 65 2c 20 69 6e 74 20 2a 70 69 4d 6f 76 65  ble, int *piMove
18a03 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  d){.  int rc;.  
18a04 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d  MemPage *pPage =
18a05 20 30 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a   0;.  BtShared *
18a06 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20  pBt = p->pBt;.. 
18a07 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
18a08 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28  BtreeHoldsMutex(
18a09 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  p) );.  assert( 
18a0a 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e  p->inTrans==TRAN
18a0b 53 5f 57 52 49 54 45 20 29 3b 0a 0a 20 20 2f 2a  S_WRITE );..  /*
18a0c 20 49 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 74   It is illegal t
18a0d 6f 20 64 72 6f 70 20 61 20 74 61 62 6c 65 20 69  o drop a table i
18a0e 66 20 61 6e 79 20 63 75 72 73 6f 72 73 20 61 72  f any cursors ar
18a0f 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65 0a 20 20  e open on the.  
18a10 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 54 68 69  ** database. Thi
18a11 73 20 69 73 20 62 65 63 61 75 73 65 20 69 6e 20  s is because in 
18a12 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64 65  auto-vacuum mode
18a13 20 74 68 65 20 62 61 63 6b 65 6e 64 20 6d 61 79   the backend may
18a14 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f 20 6d 6f  .  ** need to mo
18a15 76 65 20 61 6e 6f 74 68 65 72 20 72 6f 6f 74 2d  ve another root-
18a16 70 61 67 65 20 74 6f 20 66 69 6c 6c 20 61 20 67  page to fill a g
18a17 61 70 20 6c 65 66 74 20 62 79 20 74 68 65 20 64  ap left by the d
18a18 65 6c 65 74 65 64 0a 20 20 2a 2a 20 72 6f 6f 74  eleted.  ** root
18a19 20 70 61 67 65 2e 20 49 66 20 61 6e 20 6f 70 65   page. If an ope
18a1a 6e 20 63 75 72 73 6f 72 20 77 61 73 20 75 73 69  n cursor was usi
18a1b 6e 67 20 74 68 69 73 20 70 61 67 65 20 61 20 70  ng this page a p
18a1c 72 6f 62 6c 65 6d 20 77 6f 75 6c 64 20 0a 20 20  roblem would .  
18a1d 2a 2a 20 6f 63 63 75 72 2e 0a 20 20 2a 2a 0a 20  ** occur..  **. 
18a1e 20 2a 2a 20 54 68 69 73 20 65 72 72 6f 72 20 69   ** This error i
18a1f 73 20 63 61 75 67 68 74 20 6c 6f 6e 67 20 62 65  s caught long be
18a20 66 6f 72 65 20 63 6f 6e 74 72 6f 6c 20 72 65 61  fore control rea
18a21 63 68 65 73 20 74 68 69 73 20 70 6f 69 6e 74 2e  ches this point.
18a22 0a 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45  .  */.  if( NEVE
18a23 52 28 70 42 74 2d 3e 70 43 75 72 73 6f 72 29 20  R(pBt->pCursor) 
18a24 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f  ){.    sqlite3Co
18a25 6e 6e 65 63 74 69 6f 6e 42 6c 6f 63 6b 65 64 28  nnectionBlocked(
18a26 70 2d 3e 64 62 2c 20 70 42 74 2d 3e 70 43 75 72  p->db, pBt->pCur
18a27 73 6f 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 29  sor->pBtree->db)
18a28 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
18a29 49 54 45 5f 4c 4f 43 4b 45 44 5f 53 48 41 52 45  ITE_LOCKED_SHARE
18a2a 44 43 41 43 48 45 3b 0a 20 20 7d 0a 0a 20 20 72  DCACHE;.  }..  r
18a2b 63 20 3d 20 62 74 72 65 65 47 65 74 50 61 67 65  c = btreeGetPage
18a2c 28 70 42 74 2c 20 28 50 67 6e 6f 29 69 54 61 62  (pBt, (Pgno)iTab
18a2d 6c 65 2c 20 26 70 50 61 67 65 2c 20 30 29 3b 0a  le, &pPage, 0);.
18a2e 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
18a2f 6e 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c  n rc;.  rc = sql
18a30 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54 61  ite3BtreeClearTa
18a31 62 6c 65 28 70 2c 20 69 54 61 62 6c 65 2c 20 30  ble(p, iTable, 0
18a32 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20  );.  if( rc ){. 
18a33 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
18a34 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72  Page);.    retur
18a35 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70 69  n rc;.  }..  *pi
18a36 4d 6f 76 65 64 20 3d 20 30 3b 0a 0a 20 20 69 66  Moved = 0;..  if
18a37 28 20 69 54 61 62 6c 65 3e 31 20 29 7b 0a 23 69  ( iTable>1 ){.#i
18a38 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
18a39 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
18a3a 66 72 65 65 50 61 67 65 28 70 50 61 67 65 2c 20  freePage(pPage, 
18a3b 26 72 63 29 3b 0a 20 20 20 20 72 65 6c 65 61 73  &rc);.    releas
18a3c 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 23 65  ePage(pPage);.#e
18a3d 6c 73 65 0a 20 20 20 20 69 66 28 20 70 42 74 2d  lse.    if( pBt-
18a3e 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20  >autoVacuum ){. 
18a3f 20 20 20 20 20 50 67 6e 6f 20 6d 61 78 52 6f 6f       Pgno maxRoo
18a40 74 50 67 6e 6f 3b 0a 20 20 20 20 20 20 73 71 6c  tPgno;.      sql
18a41 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
18a42 28 70 2c 20 42 54 52 45 45 5f 4c 41 52 47 45 53  (p, BTREE_LARGES
18a43 54 5f 52 4f 4f 54 5f 50 41 47 45 2c 20 26 6d 61  T_ROOT_PAGE, &ma
18a44 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a 0a 20 20 20  xRootPgno);..   
18a45 20 20 20 69 66 28 20 69 54 61 62 6c 65 3d 3d 6d     if( iTable==m
18a46 61 78 52 6f 6f 74 50 67 6e 6f 20 29 7b 0a 20 20  axRootPgno ){.  
18a47 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
18a48 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70  table being drop
18a49 70 65 64 20 69 73 20 74 68 65 20 74 61 62 6c 65  ped is the table
18a4a 20 77 69 74 68 20 74 68 65 20 6c 61 72 67 65 73   with the larges
18a4b 74 20 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20  t root-page.    
18a4c 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 69 6e      ** number in
18a4d 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 70   the database, p
18a4e 75 74 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  ut the root page
18a4f 20 6f 6e 20 74 68 65 20 66 72 65 65 20 6c 69 73   on the free lis
18a50 74 2e 20 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  t. .        */. 
18a51 20 20 20 20 20 20 20 66 72 65 65 50 61 67 65 28         freePage(
18a52 70 50 61 67 65 2c 20 26 72 63 29 3b 0a 20 20 20  pPage, &rc);.   
18a53 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
18a54 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  (pPage);.       
18a55 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
18a56 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
18a57 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
18a58 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
18a59 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  {.        /* The
18a5a 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64 72 6f   table being dro
18a5b 70 70 65 64 20 64 6f 65 73 20 6e 6f 74 20 68 61  pped does not ha
18a5c 76 65 20 74 68 65 20 6c 61 72 67 65 73 74 20 72  ve the largest r
18a5d 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 20 20 20  oot-page.       
18a5e 20 2a 2a 20 6e 75 6d 62 65 72 20 69 6e 20 74 68   ** number in th
18a5f 65 20 64 61 74 61 62 61 73 65 2e 20 53 6f 20 6d  e database. So m
18a60 6f 76 65 20 74 68 65 20 70 61 67 65 20 74 68 61  ove the page tha
18a61 74 20 64 6f 65 73 20 69 6e 74 6f 20 74 68 65 20  t does into the 
18a62 0a 20 20 20 20 20 20 20 20 2a 2a 20 67 61 70 20  .        ** gap 
18a63 6c 65 66 74 20 62 79 20 74 68 65 20 64 65 6c 65  left by the dele
18a64 74 65 64 20 72 6f 6f 74 2d 70 61 67 65 2e 0a 20  ted root-page.. 
18a65 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
18a66 20 20 4d 65 6d 50 61 67 65 20 2a 70 4d 6f 76 65    MemPage *pMove
18a67 3b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73  ;.        releas
18a68 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
18a69 20 20 20 20 20 20 72 63 20 3d 20 62 74 72 65 65        rc = btree
18a6a 47 65 74 50 61 67 65 28 70 42 74 2c 20 6d 61 78  GetPage(pBt, max
18a6b 52 6f 6f 74 50 67 6e 6f 2c 20 26 70 4d 6f 76 65  RootPgno, &pMove
18a6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , 0);.        if
18a6d 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
18a6e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  ){.          ret
18a6f 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20  urn rc;.        
18a70 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72  }.        rc = r
18a71 65 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c  elocatePage(pBt,
18a72 20 70 4d 6f 76 65 2c 20 50 54 52 4d 41 50 5f 52   pMove, PTRMAP_R
18a73 4f 4f 54 50 41 47 45 2c 20 30 2c 20 69 54 61 62  OOTPAGE, 0, iTab
18a74 6c 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  le, 0);.        
18a75 72 65 6c 65 61 73 65 50 61 67 65 28 70 4d 6f 76  releasePage(pMov
18a76 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  e);.        if( 
18a77 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
18a78 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
18a79 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
18a7a 20 20 20 20 20 20 20 20 70 4d 6f 76 65 20 3d 20          pMove = 
18a7b 30 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  0;.        rc = 
18a7c 62 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74  btreeGetPage(pBt
18a7d 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 2c 20 26  , maxRootPgno, &
18a7e 70 4d 6f 76 65 2c 20 30 29 3b 0a 20 20 20 20 20  pMove, 0);.     
18a7f 20 20 20 66 72 65 65 50 61 67 65 28 70 4d 6f 76     freePage(pMov
18a80 65 2c 20 26 72 63 29 3b 0a 20 20 20 20 20 20 20  e, &rc);.       
18a81 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4d 6f   releasePage(pMo
18a82 76 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ve);.        if(
18a83 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
18a84 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
18a85 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d  rn rc;.        }
18a86 0a 20 20 20 20 20 20 20 20 2a 70 69 4d 6f 76 65  .        *piMove
18a87 64 20 3d 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3b  d = maxRootPgno;
18a88 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
18a89 2f 2a 20 53 65 74 20 74 68 65 20 6e 65 77 20 27  /* Set the new '
18a8a 6d 61 78 2d 72 6f 6f 74 2d 70 61 67 65 27 20 76  max-root-page' v
18a8b 61 6c 75 65 20 69 6e 20 74 68 65 20 64 61 74 61  alue in the data
18a8c 62 61 73 65 20 68 65 61 64 65 72 2e 20 54 68 69  base header. Thi
18a8d 73 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 74 68  s.      ** is th
18a8e 65 20 6f 6c 64 20 76 61 6c 75 65 20 6c 65 73 73  e old value less
18a8f 20 6f 6e 65 2c 20 6c 65 73 73 20 6f 6e 65 20 6d   one, less one m
18a90 6f 72 65 20 69 66 20 74 68 61 74 20 68 61 70 70  ore if that happ
18a91 65 6e 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20  ens to.      ** 
18a92 62 65 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 6e  be a root-page n
18a93 75 6d 62 65 72 2c 20 6c 65 73 73 20 6f 6e 65 20  umber, less one 
18a94 61 67 61 69 6e 20 69 66 20 74 68 61 74 20 69 73  again if that is
18a95 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 50 45   the.      ** PE
18a96 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 2e  NDING_BYTE_PAGE.
18a97 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
18a98 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20  maxRootPgno--;. 
18a99 20 20 20 20 20 77 68 69 6c 65 28 20 6d 61 78 52       while( maxR
18a9a 6f 6f 74 50 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47  ootPgno==PENDING
18a9b 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 0a  _BYTE_PAGE(pBt).
18a9c 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
18a9d 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42  PTRMAP_ISPAGE(pB
18a9e 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 20  t, maxRootPgno) 
18a9f 29 7b 0a 20 20 20 20 20 20 20 20 6d 61 78 52 6f  ){.        maxRo
18aa0 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20  otPgno--;.      
18aa1 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  }.      assert( 
18aa2 6d 61 78 52 6f 6f 74 50 67 6e 6f 21 3d 50 45 4e  maxRootPgno!=PEN
18aa3 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
18aa4 42 74 29 20 29 3b 0a 0a 20 20 20 20 20 20 72 63  Bt) );..      rc
18aa5 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 55   = sqlite3BtreeU
18aa6 70 64 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20  pdateMeta(p, 4, 
18aa7 6d 61 78 52 6f 6f 74 50 67 6e 6f 29 3b 0a 20 20  maxRootPgno);.  
18aa8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66    }else{.      f
18aa9 72 65 65 50 61 67 65 28 70 50 61 67 65 2c 20 26  reePage(pPage, &
18aaa 72 63 29 3b 0a 20 20 20 20 20 20 72 65 6c 65 61  rc);.      relea
18aab 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  sePage(pPage);. 
18aac 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 65     }.#endif.  }e
18aad 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20 73  lse{.    /* If s
18aae 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54  qlite3BtreeDropT
18aaf 61 62 6c 65 20 77 61 73 20 63 61 6c 6c 65 64 20  able was called 
18ab0 6f 6e 20 70 61 67 65 20 31 2e 0a 20 20 20 20 2a  on page 1..    *
18ab1 2a 20 54 68 69 73 20 72 65 61 6c 6c 79 20 6e 65  * This really ne
18ab2 76 65 72 20 73 68 6f 75 6c 64 20 68 61 70 70 65  ver should happe
18ab3 6e 20 65 78 63 65 70 74 20 69 6e 20 61 20 63 6f  n except in a co
18ab4 72 72 75 70 74 0a 20 20 20 20 2a 2a 20 64 61 74  rrupt.    ** dat
18ab5 61 62 61 73 65 2e 20 0a 20 20 20 20 2a 2f 0a 20  abase. .    */. 
18ab6 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50 61 67     zeroPage(pPag
18ab7 65 2c 20 50 54 46 5f 49 4e 54 4b 45 59 7c 50 54  e, PTF_INTKEY|PT
18ab8 46 5f 4c 45 41 46 20 29 3b 0a 20 20 20 20 72 65  F_LEAF );.    re
18ab9 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29  leasePage(pPage)
18aba 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
18abb 63 3b 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  c;  .}.SQLITE_PR
18abc 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
18abd 33 42 74 72 65 65 44 72 6f 70 54 61 62 6c 65 28  3BtreeDropTable(
18abe 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54  Btree *p, int iT
18abf 61 62 6c 65 2c 20 69 6e 74 20 2a 70 69 4d 6f 76  able, int *piMov
18ac0 65 64 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ed){.  int rc;. 
18ac1 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
18ac2 65 72 28 70 29 3b 0a 20 20 72 63 20 3d 20 62 74  er(p);.  rc = bt
18ac3 72 65 65 44 72 6f 70 54 61 62 6c 65 28 70 2c 20  reeDropTable(p, 
18ac4 69 54 61 62 6c 65 2c 20 70 69 4d 6f 76 65 64 29  iTable, piMoved)
18ac5 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
18ac6 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
18ac7 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  rn rc;.}.../*.**
18ac8 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d   This function m
18ac9 61 79 20 6f 6e 6c 79 20 62 65 20 63 61 6c 6c 65  ay only be calle
18aca 64 20 69 66 20 74 68 65 20 62 2d 74 72 65 65 20  d if the b-tree 
18acb 63 6f 6e 6e 65 63 74 69 6f 6e 20 61 6c 72 65 61  connection alrea
18acc 64 79 0a 2a 2a 20 68 61 73 20 61 20 72 65 61 64  dy.** has a read
18acd 20 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73 61   or write transa
18ace 63 74 69 6f 6e 20 6f 70 65 6e 20 6f 6e 20 74 68  ction open on th
18acf 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a  e database..**.*
18ad0 2a 20 52 65 61 64 20 74 68 65 20 6d 65 74 61 2d  * Read the meta-
18ad1 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20  information out 
18ad2 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 66 69  of a database fi
18ad3 6c 65 2e 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a 20  le.  Meta[0].** 
18ad4 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
18ad5 20 66 72 65 65 20 70 61 67 65 73 20 63 75 72 72   free pages curr
18ad6 65 6e 74 6c 79 20 69 6e 20 74 68 65 20 64 61 74  ently in the dat
18ad7 61 62 61 73 65 2e 20 20 4d 65 74 61 5b 31 5d 0a  abase.  Meta[1].
18ad8 2a 2a 20 74 68 72 6f 75 67 68 20 6d 65 74 61 5b  ** through meta[
18ad9 31 35 5d 20 61 72 65 20 61 76 61 69 6c 61 62 6c  15] are availabl
18ada 65 20 66 6f 72 20 75 73 65 20 62 79 20 68 69 67  e for use by hig
18adb 68 65 72 20 6c 61 79 65 72 73 2e 20 20 4d 65 74  her layers.  Met
18adc 61 5b 30 5d 0a 2a 2a 20 69 73 20 72 65 61 64 2d  a[0].** is read-
18add 6f 6e 6c 79 2c 20 74 68 65 20 6f 74 68 65 72 73  only, the others
18ade 20 61 72 65 20 72 65 61 64 2f 77 72 69 74 65 2e   are read/write.
18adf 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20 73 63 68 65  .** .** The sche
18ae0 6d 61 20 6c 61 79 65 72 20 6e 75 6d 62 65 72 73  ma layer numbers
18ae1 20 6d 65 74 61 20 76 61 6c 75 65 73 20 64 69 66   meta values dif
18ae2 66 65 72 65 6e 74 6c 79 2e 20 20 41 74 20 74 68  ferently.  At th
18ae3 65 20 73 63 68 65 6d 61 0a 2a 2a 20 6c 61 79 65  e schema.** laye
18ae4 72 20 28 61 6e 64 20 74 68 65 20 53 65 74 43 6f  r (and the SetCo
18ae5 6f 6b 69 65 20 61 6e 64 20 52 65 61 64 43 6f 6f  okie and ReadCoo
18ae6 6b 69 65 20 6f 70 63 6f 64 65 73 29 20 74 68 65  kie opcodes) the
18ae7 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 66 72   number of.** fr
18ae8 65 65 20 70 61 67 65 73 20 69 73 20 6e 6f 74 20  ee pages is not 
18ae9 76 69 73 69 62 6c 65 2e 20 20 53 6f 20 43 6f 6f  visible.  So Coo
18aea 6b 69 65 5b 30 5d 20 69 73 20 74 68 65 20 73 61  kie[0] is the sa
18aeb 6d 65 20 61 73 20 4d 65 74 61 5b 31 5d 2e 0a 2a  me as Meta[1]..*
18aec 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18aed 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
18aee 65 65 47 65 74 4d 65 74 61 28 42 74 72 65 65 20  eeGetMeta(Btree 
18aef 2a 70 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32  *p, int idx, u32
18af0 20 2a 70 4d 65 74 61 29 7b 0a 20 20 42 74 53 68   *pMeta){.  BtSh
18af1 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
18af2 42 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74  Bt;..  sqlite3Bt
18af3 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 61  reeEnter(p);.  a
18af4 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e  ssert( p->inTran
18af5 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b 0a  s>TRANS_NONE );.
18af6 20 20 61 73 73 65 72 74 28 20 53 51 4c 49 54 45    assert( SQLITE
18af7 5f 4f 4b 3d 3d 71 75 65 72 79 53 68 61 72 65 64  _OK==queryShared
18af8 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70  CacheTableLock(p
18af9 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 52  , MASTER_ROOT, R
18afa 45 41 44 5f 4c 4f 43 4b 29 20 29 3b 0a 20 20 61  EAD_LOCK) );.  a
18afb 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50 61 67  ssert( pBt->pPag
18afc 65 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  e1 );.  assert( 
18afd 69 64 78 3e 3d 30 20 26 26 20 69 64 78 3c 3d 31  idx>=0 && idx<=1
18afe 35 20 29 3b 0a 0a 20 20 2a 70 4d 65 74 61 20 3d  5 );..  *pMeta =
18aff 20 67 65 74 34 62 79 74 65 28 26 70 42 74 2d 3e   get4byte(&pBt->
18b00 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36  pPage1->aData[36
18b01 20 2b 20 69 64 78 2a 34 5d 29 3b 0a 0a 20 20 2f   + idx*4]);..  /
18b02 2a 20 49 66 20 61 75 74 6f 2d 76 61 63 75 75 6d  * If auto-vacuum
18b03 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e 20   is disabled in 
18b04 74 68 69 73 20 62 75 69 6c 64 20 61 6e 64 20 74  this build and t
18b05 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f 2d 76  his is an auto-v
18b06 61 63 75 75 6d 0a 20 20 2a 2a 20 64 61 74 61 62  acuum.  ** datab
18b07 61 73 65 2c 20 6d 61 72 6b 20 74 68 65 20 64 61  ase, mark the da
18b08 74 61 62 61 73 65 20 61 73 20 72 65 61 64 2d 6f  tabase as read-o
18b09 6e 6c 79 2e 20 20 2a 2f 0a 23 69 66 64 65 66 20  nly.  */.#ifdef 
18b0a 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
18b0b 56 41 43 55 55 4d 0a 20 20 69 66 28 20 69 64 78  VACUUM.  if( idx
18b0c 3d 3d 42 54 52 45 45 5f 4c 41 52 47 45 53 54 5f  ==BTREE_LARGEST_
18b0d 52 4f 4f 54 5f 50 41 47 45 20 26 26 20 2a 70 4d  ROOT_PAGE && *pM
18b0e 65 74 61 3e 30 20 29 20 70 42 74 2d 3e 72 65 61  eta>0 ) pBt->rea
18b0f 64 4f 6e 6c 79 20 3d 20 31 3b 0a 23 65 6e 64 69  dOnly = 1;.#endi
18b10 66 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  f..  sqlite3Btre
18b11 65 4c 65 61 76 65 28 70 29 3b 0a 7d 0a 0a 2f 2a  eLeave(p);.}../*
18b12 0a 2a 2a 20 57 72 69 74 65 20 6d 65 74 61 2d 69  .** Write meta-i
18b13 6e 66 6f 72 6d 61 74 69 6f 6e 20 62 61 63 6b 20  nformation back 
18b14 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
18b15 65 2e 20 20 4d 65 74 61 5b 30 5d 20 69 73 0a 2a  e.  Meta[0] is.*
18b16 2a 20 72 65 61 64 2d 6f 6e 6c 79 20 61 6e 64 20  * read-only and 
18b17 6d 61 79 20 6e 6f 74 20 62 65 20 77 72 69 74 74  may not be writt
18b18 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  en..*/.SQLITE_PR
18b19 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
18b1a 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74 61  3BtreeUpdateMeta
18b1b 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69  (Btree *p, int i
18b1c 64 78 2c 20 75 33 32 20 69 4d 65 74 61 29 7b 0a  dx, u32 iMeta){.
18b1d 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
18b1e 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 75 6e 73 69  = p->pBt;.  unsi
18b1f 67 6e 65 64 20 63 68 61 72 20 2a 70 50 31 3b 0a  gned char *pP1;.
18b20 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65    int rc;.  asse
18b21 72 74 28 20 69 64 78 3e 3d 31 20 26 26 20 69 64  rt( idx>=1 && id
18b22 78 3c 3d 31 35 20 29 3b 0a 20 20 73 71 6c 69 74  x<=15 );.  sqlit
18b23 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
18b24 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 6e  .  assert( p->in
18b25 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57 52 49  Trans==TRANS_WRI
18b26 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  TE );.  assert( 
18b27 70 42 74 2d 3e 70 50 61 67 65 31 21 3d 30 20 29  pBt->pPage1!=0 )
18b28 3b 0a 20 20 70 50 31 20 3d 20 70 42 74 2d 3e 70  ;.  pP1 = pBt->p
18b29 50 61 67 65 31 2d 3e 61 44 61 74 61 3b 0a 20 20  Page1->aData;.  
18b2a 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
18b2b 72 57 72 69 74 65 28 70 42 74 2d 3e 70 50 61 67  rWrite(pBt->pPag
18b2c 65 31 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  e1->pDbPage);.  
18b2d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
18b2e 4b 20 29 7b 0a 20 20 20 20 70 75 74 34 62 79 74  K ){.    put4byt
18b2f 65 28 26 70 50 31 5b 33 36 20 2b 20 69 64 78 2a  e(&pP1[36 + idx*
18b30 34 5d 2c 20 69 4d 65 74 61 29 3b 0a 23 69 66 6e  4], iMeta);.#ifn
18b31 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
18b32 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69  AUTOVACUUM.    i
18b33 66 28 20 69 64 78 3d 3d 42 54 52 45 45 5f 49 4e  f( idx==BTREE_IN
18b34 43 52 5f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20  CR_VACUUM ){.   
18b35 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e     assert( pBt->
18b36 61 75 74 6f 56 61 63 75 75 6d 20 7c 7c 20 69 4d  autoVacuum || iM
18b37 65 74 61 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  eta==0 );.      
18b38 61 73 73 65 72 74 28 20 69 4d 65 74 61 3d 3d 30  assert( iMeta==0
18b39 20 7c 7c 20 69 4d 65 74 61 3d 3d 31 20 29 3b 0a   || iMeta==1 );.
18b3a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56        pBt->incrV
18b3b 61 63 75 75 6d 20 3d 20 28 75 38 29 69 4d 65 74  acuum = (u8)iMet
18b3c 61 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  a;.    }.#endif.
18b3d 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72    }.  sqlite3Btr
18b3e 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65  eeLeave(p);.  re
18b3f 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e  turn rc;.}..#ifn
18b40 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
18b41 42 54 52 45 45 43 4f 55 4e 54 0a 2f 2a 0a 2a 2a  BTREECOUNT./*.**
18b42 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
18b43 65 6e 74 2c 20 70 43 75 72 2c 20 69 73 20 61 20  ent, pCur, is a 
18b44 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 6f 6e  cursor opened on
18b45 20 73 6f 6d 65 20 62 2d 74 72 65 65 2e 20 43 6f   some b-tree. Co
18b46 75 6e 74 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65  unt the.** numbe
18b47 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
18b48 74 68 65 20 62 2d 74 72 65 65 20 61 6e 64 20 77  the b-tree and w
18b49 72 69 74 65 20 74 68 65 20 72 65 73 75 6c 74 20  rite the result 
18b4a 74 6f 20 2a 70 6e 45 6e 74 72 79 2e 0a 2a 2a 0a  to *pnEntry..**.
18b4b 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20  ** SQLITE_OK is 
18b4c 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65 20  returned if the 
18b4d 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 73 75 63  operation is suc
18b4e 63 65 73 73 66 75 6c 6c 79 20 65 78 65 63 75 74  cessfully execut
18b4f 65 64 2e 20 0a 2a 2a 20 4f 74 68 65 72 77 69 73  ed. .** Otherwis
18b50 65 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 69  e, if an error i
18b51 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 28 69  s encountered (i
18b52 2e 65 2e 20 61 6e 20 49 4f 20 65 72 72 6f 72 20  .e. an IO error 
18b53 6f 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63  or database.** c
18b54 6f 72 72 75 70 74 69 6f 6e 29 20 61 6e 20 53 51  orruption) an SQ
18b55 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20  Lite error code 
18b56 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
18b57 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
18b58 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
18b59 6f 75 6e 74 28 42 74 43 75 72 73 6f 72 20 2a 70  ount(BtCursor *p
18b5a 43 75 72 2c 20 69 36 34 20 2a 70 6e 45 6e 74 72  Cur, i64 *pnEntr
18b5b 79 29 7b 0a 20 20 69 36 34 20 6e 45 6e 74 72 79  y){.  i64 nEntry
18b5c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
18b5d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
18b5e 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20 69 6e  lue to return in
18b5f 20 2a 70 6e 45 6e 74 72 79 20 2a 2f 0a 20 20 69   *pnEntry */.  i
18b60 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
18b61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18b62 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
18b63 64 65 20 2a 2f 0a 20 20 72 63 20 3d 20 6d 6f 76  de */.  rc = mov
18b64 65 54 6f 52 6f 6f 74 28 70 43 75 72 29 3b 0a 0a  eToRoot(pCur);..
18b65 20 20 2f 2a 20 55 6e 6c 65 73 73 20 61 6e 20 65    /* Unless an e
18b66 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 65  rror occurs, the
18b67 20 66 6f 6c 6c 6f 77 69 6e 67 20 6c 6f 6f 70 20   following loop 
18b68 72 75 6e 73 20 6f 6e 65 20 69 74 65 72 61 74 69  runs one iterati
18b69 6f 6e 20 66 6f 72 20 65 61 63 68 0a 20 20 2a 2a  on for each.  **
18b6a 20 70 61 67 65 20 69 6e 20 74 68 65 20 42 2d 54   page in the B-T
18b6b 72 65 65 20 73 74 72 75 63 74 75 72 65 20 28 6e  ree structure (n
18b6c 6f 74 20 69 6e 63 6c 75 64 69 6e 67 20 6f 76 65  ot including ove
18b6d 72 66 6c 6f 77 20 70 61 67 65 73 29 2e 20 0a 20  rflow pages). . 
18b6e 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 72 63 3d   */.  while( rc=
18b6f 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
18b70 20 20 69 6e 74 20 69 49 64 78 3b 20 20 20 20 20    int iIdx;     
18b71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18b72 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
18b73 20 63 68 69 6c 64 20 6e 6f 64 65 20 69 6e 20 70   child node in p
18b74 61 72 65 6e 74 20 2a 2f 0a 20 20 20 20 4d 65 6d  arent */.    Mem
18b75 50 61 67 65 20 2a 70 50 61 67 65 3b 20 20 20 20  Page *pPage;    
18b76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18b77 2f 2a 20 43 75 72 72 65 6e 74 20 70 61 67 65 20  /* Current page 
18b78 6f 66 20 74 68 65 20 62 2d 74 72 65 65 20 2a 2f  of the b-tree */
18b79 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73  ..    /* If this
18b7a 20 69 73 20 61 20 6c 65 61 66 20 70 61 67 65 20   is a leaf page 
18b7b 6f 72 20 74 68 65 20 74 72 65 65 20 69 73 20 6e  or the tree is n
18b7c 6f 74 20 61 6e 20 69 6e 74 2d 6b 65 79 20 74 72  ot an int-key tr
18b7d 65 65 2c 20 74 68 65 6e 20 0a 20 20 20 20 2a 2a  ee, then .    **
18b7e 20 74 68 69 73 20 70 61 67 65 20 63 6f 6e 74 61   this page conta
18b7f 69 6e 73 20 63 6f 75 6e 74 61 62 6c 65 20 65 6e  ins countable en
18b80 74 72 69 65 73 2e 20 49 6e 63 72 65 6d 65 6e 74  tries. Increment
18b81 20 74 68 65 20 65 6e 74 72 79 20 63 6f 75 6e 74   the entry count
18b82 65 72 0a 20 20 20 20 2a 2a 20 61 63 63 6f 72 64  er.    ** accord
18b83 69 6e 67 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20  ingly..    */.  
18b84 20 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e    pPage = pCur->
18b85 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61  apPage[pCur->iPa
18b86 67 65 5d 3b 0a 20 20 20 20 69 66 28 20 70 50 61  ge];.    if( pPa
18b87 67 65 2d 3e 6c 65 61 66 20 7c 7c 20 21 70 50 61  ge->leaf || !pPa
18b88 67 65 2d 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20  ge->intKey ){.  
18b89 20 20 20 20 6e 45 6e 74 72 79 20 2b 3d 20 70 50      nEntry += pP
18b8a 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20 20 20 20  age->nCell;.    
18b8b 7d 0a 0a 20 20 20 20 2f 2a 20 70 50 61 67 65 20  }..    /* pPage 
18b8c 69 73 20 61 20 6c 65 61 66 20 6e 6f 64 65 2e 20  is a leaf node. 
18b8d 54 68 69 73 20 6c 6f 6f 70 20 6e 61 76 69 67 61  This loop naviga
18b8e 74 65 73 20 74 68 65 20 63 75 72 73 6f 72 20 73  tes the cursor s
18b8f 6f 20 74 68 61 74 20 69 74 20 0a 20 20 20 20 2a  o that it .    *
18b90 2a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  * points to the 
18b91 66 69 72 73 74 20 69 6e 74 65 72 69 6f 72 20 63  first interior c
18b92 65 6c 6c 20 74 68 61 74 20 69 74 20 70 6f 69 6e  ell that it poin
18b93 74 73 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74  ts to the parent
18b94 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 20 6e   of.    ** the n
18b95 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65 20  ext page in the 
18b96 74 72 65 65 20 74 68 61 74 20 68 61 73 20 6e 6f  tree that has no
18b97 74 20 79 65 74 20 62 65 65 6e 20 76 69 73 69 74  t yet been visit
18b98 65 64 2e 20 54 68 65 0a 20 20 20 20 2a 2a 20 70  ed. The.    ** p
18b99 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
18b9a 3e 69 50 61 67 65 5d 20 76 61 6c 75 65 20 69 73  >iPage] value is
18b9b 20 73 65 74 20 74 6f 20 74 68 65 20 69 6e 64 65   set to the inde
18b9c 78 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20  x of the parent 
18b9d 63 65 6c 6c 0a 20 20 20 20 2a 2a 20 6f 66 20 74  cell.    ** of t
18b9e 68 65 20 70 61 67 65 2c 20 6f 72 20 74 6f 20 74  he page, or to t
18b9f 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c  he number of cel
18ba0 6c 73 20 69 6e 20 74 68 65 20 70 61 67 65 20 69  ls in the page i
18ba1 66 20 74 68 65 20 6e 65 78 74 20 70 61 67 65 0a  f the next page.
18ba2 20 20 20 20 2a 2a 20 74 6f 20 76 69 73 69 74 20      ** to visit 
18ba3 69 73 20 74 68 65 20 72 69 67 68 74 2d 63 68 69  is the right-chi
18ba4 6c 64 20 6f 66 20 69 74 73 20 70 61 72 65 6e 74  ld of its parent
18ba5 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
18ba6 49 66 20 61 6c 6c 20 70 61 67 65 73 20 69 6e 20  If all pages in 
18ba7 74 68 65 20 74 72 65 65 20 68 61 76 65 20 62 65  the tree have be
18ba8 65 6e 20 76 69 73 69 74 65 64 2c 20 72 65 74 75  en visited, retu
18ba9 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 74 6f 20  rn SQLITE_OK to 
18baa 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 65  the.    ** calle
18bab 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  r..    */.    if
18bac 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b  ( pPage->leaf ){
18bad 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 20 20  .      do {.    
18bae 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 69 50      if( pCur->iP
18baf 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  age==0 ){.      
18bb0 20 20 20 20 2f 2a 20 41 6c 6c 20 70 61 67 65 73      /* All pages
18bb1 20 6f 66 20 74 68 65 20 62 2d 74 72 65 65 20 68   of the b-tree h
18bb2 61 76 65 20 62 65 65 6e 20 76 69 73 69 74 65 64  ave been visited
18bb3 2e 20 52 65 74 75 72 6e 20 73 75 63 63 65 73 73  . Return success
18bb4 66 75 6c 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20  fully. */.      
18bb5 20 20 20 20 2a 70 6e 45 6e 74 72 79 20 3d 20 6e      *pnEntry = n
18bb6 45 6e 74 72 79 3b 0a 20 20 20 20 20 20 20 20 20  Entry;.         
18bb7 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
18bb8 4b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  K;.        }.   
18bb9 20 20 20 20 20 6d 6f 76 65 54 6f 50 61 72 65 6e       moveToParen
18bba 74 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 7d  t(pCur);.      }
18bbb 77 68 69 6c 65 20 28 20 70 43 75 72 2d 3e 61 69  while ( pCur->ai
18bbc 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  Idx[pCur->iPage]
18bbd 3e 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  >=pCur->apPage[p
18bbe 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65  Cur->iPage]->nCe
18bbf 6c 6c 20 29 3b 0a 0a 20 20 20 20 20 20 70 43 75  ll );..      pCu
18bc0 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
18bc1 50 61 67 65 5d 2b 2b 3b 0a 20 20 20 20 20 20 70  Page]++;.      p
18bc2 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50  Page = pCur->apP
18bc3 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
18bc4 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
18bc5 44 65 73 63 65 6e 64 20 74 6f 20 74 68 65 20 63  Descend to the c
18bc6 68 69 6c 64 20 6e 6f 64 65 20 6f 66 20 74 68 65  hild node of the
18bc7 20 63 65 6c 6c 20 74 68 61 74 20 74 68 65 20 63   cell that the c
18bc8 75 72 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20  ursor currently 
18bc9 0a 20 20 20 20 2a 2a 20 70 6f 69 6e 74 73 20 61  .    ** points a
18bca 74 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 72  t. This is the r
18bcb 69 67 68 74 2d 63 68 69 6c 64 20 69 66 20 28 69  ight-child if (i
18bcc 49 64 78 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c  Idx==pPage->nCel
18bcd 6c 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  l)..    */.    i
18bce 49 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49 64  Idx = pCur->aiId
18bcf 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a  x[pCur->iPage];.
18bd0 20 20 20 20 69 66 28 20 69 49 64 78 3d 3d 70 50      if( iIdx==pP
18bd1 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20 20  age->nCell ){.  
18bd2 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43      rc = moveToC
18bd3 68 69 6c 64 28 70 43 75 72 2c 20 67 65 74 34 62  hild(pCur, get4b
18bd4 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74  yte(&pPage->aDat
18bd5 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  a[pPage->hdrOffs
18bd6 65 74 2b 38 5d 29 29 3b 0a 20 20 20 20 7d 65 6c  et+8]));.    }el
18bd7 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6d  se{.      rc = m
18bd8 6f 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c  oveToChild(pCur,
18bd9 20 67 65 74 34 62 79 74 65 28 66 69 6e 64 43 65   get4byte(findCe
18bda 6c 6c 28 70 50 61 67 65 2c 20 69 49 64 78 29 29  ll(pPage, iIdx))
18bdb 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
18bdc 2f 2a 20 41 6e 20 65 72 72 6f 72 20 68 61 73 20  /* An error has 
18bdd 6f 63 63 75 72 72 65 64 2e 20 52 65 74 75 72 6e  occurred. Return
18bde 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20   an error code. 
18bdf 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  */.  return rc;.
18be0 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
18be1 52 65 74 75 72 6e 20 74 68 65 20 70 61 67 65 72  Return the pager
18be2 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
18be3 20 61 20 42 54 72 65 65 2e 20 20 54 68 69 73 20   a BTree.  This 
18be4 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
18be5 66 6f 72 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61  for.** testing a
18be6 6e 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c  nd debugging onl
18be7 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
18be8 56 41 54 45 20 50 61 67 65 72 20 2a 73 71 6c 69  VATE Pager *sqli
18be9 74 65 33 42 74 72 65 65 50 61 67 65 72 28 42 74  te3BtreePager(Bt
18bea 72 65 65 20 2a 70 29 7b 0a 20 20 72 65 74 75 72  ree *p){.  retur
18beb 6e 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72  n p->pBt->pPager
18bec 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
18bed 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49  ITE_OMIT_INTEGRI
18bee 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 41  TY_CHECK./*.** A
18bef 70 70 65 6e 64 20 61 20 6d 65 73 73 61 67 65 20  ppend a message 
18bf0 74 6f 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73  to the error mes
18bf1 73 61 67 65 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a  sage string..*/.
18bf2 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 63  static void chec
18bf3 6b 41 70 70 65 6e 64 4d 73 67 28 0a 20 20 49 6e  kAppendMsg(.  In
18bf4 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63  tegrityCk *pChec
18bf5 6b 2c 0a 20 20 63 68 61 72 20 2a 7a 4d 73 67 31  k,.  char *zMsg1
18bf6 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
18bf7 7a 46 6f 72 6d 61 74 2c 0a 20 20 2e 2e 2e 0a 29  zFormat,.  ....)
18bf8 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a  {.  va_list ap;.
18bf9 20 20 69 66 28 20 21 70 43 68 65 63 6b 2d 3e 6d    if( !pCheck->m
18bfa 78 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 20  xErr ) return;. 
18bfb 20 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72 2d 2d   pCheck->mxErr--
18bfc 3b 0a 20 20 70 43 68 65 63 6b 2d 3e 6e 45 72 72  ;.  pCheck->nErr
18bfd 2b 2b 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61  ++;.  va_start(a
18bfe 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 69  p, zFormat);.  i
18bff 66 28 20 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73  f( pCheck->errMs
18c00 67 2e 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 73  g.nChar ){.    s
18c01 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70  qlite3StrAccumAp
18c02 70 65 6e 64 28 26 70 43 68 65 63 6b 2d 3e 65 72  pend(&pCheck->er
18c03 72 4d 73 67 2c 20 22 5c 6e 22 2c 20 31 29 3b 0a  rMsg, "\n", 1);.
18c04 20 20 7d 0a 20 20 69 66 28 20 7a 4d 73 67 31 20    }.  if( zMsg1 
18c05 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74  ){.    sqlite3St
18c06 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26 70 43  rAccumAppend(&pC
18c07 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2c 20 7a 4d  heck->errMsg, zM
18c08 73 67 31 2c 20 2d 31 29 3b 0a 20 20 7d 0a 20 20  sg1, -1);.  }.  
18c09 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74 66 28  sqlite3VXPrintf(
18c0a 26 70 43 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2c  &pCheck->errMsg,
18c0b 20 31 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29   1, zFormat, ap)
18c0c 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  ;.  va_end(ap);.
18c0d 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 65 72    if( pCheck->er
18c0e 72 4d 73 67 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65  rMsg.mallocFaile
18c0f 64 20 29 7b 0a 20 20 20 20 70 43 68 65 63 6b 2d  d ){.    pCheck-
18c10 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
18c11 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20  1;.  }.}.#endif 
18c12 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  /* SQLITE_OMIT_I
18c13 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 20 2a  NTEGRITY_CHECK *
18c14 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
18c15 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59  E_OMIT_INTEGRITY
18c16 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 41 64 64  _CHECK./*.** Add
18c17 20 31 20 74 6f 20 74 68 65 20 72 65 66 65 72 65   1 to the refere
18c18 6e 63 65 20 63 6f 75 6e 74 20 66 6f 72 20 70 61  nce count for pa
18c19 67 65 20 69 50 61 67 65 2e 20 20 49 66 20 74 68  ge iPage.  If th
18c1a 69 73 20 69 73 20 74 68 65 20 73 65 63 6f 6e 64  is is the second
18c1b 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 20 74 6f  .** reference to
18c1c 20 74 68 65 20 70 61 67 65 2c 20 61 64 64 20 61   the page, add a
18c1d 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
18c1e 74 6f 20 70 43 68 65 63 6b 2d 3e 7a 45 72 72 4d  to pCheck->zErrM
18c1f 73 67 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20  sg..** Return 1 
18c20 69 66 20 74 68 65 72 65 20 61 72 65 20 32 20 6f  if there are 2 o
18c21 72 65 20 6d 6f 72 65 20 72 65 66 65 72 65 6e 63  re more referenc
18c22 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 61  es to the page a
18c23 6e 64 20 30 20 69 66 0a 2a 2a 20 69 66 20 74 68  nd 0 if.** if th
18c24 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20  is is the first 
18c25 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65  reference to the
18c26 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73   page..**.** Als
18c27 6f 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 65  o check that the
18c28 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20   page number is 
18c29 69 6e 20 62 6f 75 6e 64 73 2e 0a 2a 2f 0a 73 74  in bounds..*/.st
18c2a 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 52 65  atic int checkRe
18c2b 66 28 49 6e 74 65 67 72 69 74 79 43 6b 20 2a 70  f(IntegrityCk *p
18c2c 43 68 65 63 6b 2c 20 50 67 6e 6f 20 69 50 61 67  Check, Pgno iPag
18c2d 65 2c 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78  e, char *zContex
18c2e 74 29 7b 0a 20 20 69 66 28 20 69 50 61 67 65 3d  t){.  if( iPage=
18c2f 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  =0 ) return 1;. 
18c30 20 69 66 28 20 69 50 61 67 65 3e 70 43 68 65 63   if( iPage>pChec
18c31 6b 2d 3e 6e 50 61 67 65 20 29 7b 0a 20 20 20 20  k->nPage ){.    
18c32 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70  checkAppendMsg(p
18c33 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c  Check, zContext,
18c34 20 22 69 6e 76 61 6c 69 64 20 70 61 67 65 20 6e   "invalid page n
18c35 75 6d 62 65 72 20 25 64 22 2c 20 69 50 61 67 65  umber %d", iPage
18c36 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  );.    return 1;
18c37 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 68 65 63  .  }.  if( pChec
18c38 6b 2d 3e 61 6e 52 65 66 5b 69 50 61 67 65 5d 3d  k->anRef[iPage]=
18c39 3d 31 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41  =1 ){.    checkA
18c3a 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c  ppendMsg(pCheck,
18c3b 20 7a 43 6f 6e 74 65 78 74 2c 20 22 32 6e 64 20   zContext, "2nd 
18c3c 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67  reference to pag
18c3d 65 20 25 64 22 2c 20 69 50 61 67 65 29 3b 0a 20  e %d", iPage);. 
18c3e 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
18c3f 0a 20 20 72 65 74 75 72 6e 20 20 28 70 43 68 65  .  return  (pChe
18c40 63 6b 2d 3e 61 6e 52 65 66 5b 69 50 61 67 65 5d  ck->anRef[iPage]
18c41 2b 2b 29 3e 31 3b 0a 7d 0a 0a 23 69 66 6e 64 65  ++)>1;.}..#ifnde
18c42 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
18c43 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 43  TOVACUUM./*.** C
18c44 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 65 6e  heck that the en
18c45 74 72 79 20 69 6e 20 74 68 65 20 70 6f 69 6e 74  try in the point
18c46 65 72 2d 6d 61 70 20 66 6f 72 20 70 61 67 65 20  er-map for page 
18c47 69 43 68 69 6c 64 20 6d 61 70 73 20 74 6f 20 0a  iChild maps to .
18c48 2a 2a 20 70 61 67 65 20 69 50 61 72 65 6e 74 2c  ** page iParent,
18c49 20 70 6f 69 6e 74 65 72 20 74 79 70 65 20 70 74   pointer type pt
18c4a 72 54 79 70 65 2e 20 49 66 20 6e 6f 74 2c 20 61  rType. If not, a
18c4b 70 70 65 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d  ppend an error m
18c4c 65 73 73 61 67 65 0a 2a 2a 20 74 6f 20 70 43 68  essage.** to pCh
18c4d 65 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  eck..*/.static v
18c4e 6f 69 64 20 63 68 65 63 6b 50 74 72 6d 61 70 28  oid checkPtrmap(
18c4f 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20 2a  .  IntegrityCk *
18c50 70 43 68 65 63 6b 2c 20 20 20 2f 2a 20 49 6e 74  pCheck,   /* Int
18c51 65 67 72 69 74 79 20 63 68 65 63 6b 20 63 6f 6e  egrity check con
18c52 74 65 78 74 20 2a 2f 0a 20 20 50 67 6e 6f 20 69  text */.  Pgno i
18c53 43 68 69 6c 64 2c 20 20 20 20 20 20 20 20 20 20  Child,          
18c54 20 2f 2a 20 43 68 69 6c 64 20 70 61 67 65 20 6e   /* Child page n
18c55 75 6d 62 65 72 20 2a 2f 0a 20 20 75 38 20 65 54  umber */.  u8 eT
18c56 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ype,            
18c57 20 20 2f 2a 20 45 78 70 65 63 74 65 64 20 70 6f    /* Expected po
18c58 69 6e 74 65 72 20 6d 61 70 20 74 79 70 65 20 2a  inter map type *
18c59 2f 0a 20 20 50 67 6e 6f 20 69 50 61 72 65 6e 74  /.  Pgno iParent
18c5a 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78  ,          /* Ex
18c5b 70 65 63 74 65 64 20 70 6f 69 6e 74 65 72 20 6d  pected pointer m
18c5c 61 70 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e  ap parent page n
18c5d 75 6d 62 65 72 20 2a 2f 0a 20 20 63 68 61 72 20  umber */.  char 
18c5e 2a 7a 43 6f 6e 74 65 78 74 20 20 20 20 20 20 20  *zContext       
18c5f 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 64 65 73    /* Context des
18c60 63 72 69 70 74 69 6f 6e 20 28 75 73 65 64 20 66  cription (used f
18c61 6f 72 20 65 72 72 6f 72 20 6d 73 67 29 20 2a 2f  or error msg) */
18c62 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
18c63 75 38 20 65 50 74 72 6d 61 70 54 79 70 65 3b 0a  u8 ePtrmapType;.
18c64 20 20 50 67 6e 6f 20 69 50 74 72 6d 61 70 50 61    Pgno iPtrmapPa
18c65 72 65 6e 74 3b 0a 0a 20 20 72 63 20 3d 20 70 74  rent;..  rc = pt
18c66 72 6d 61 70 47 65 74 28 70 43 68 65 63 6b 2d 3e  rmapGet(pCheck->
18c67 70 42 74 2c 20 69 43 68 69 6c 64 2c 20 26 65 50  pBt, iChild, &eP
18c68 74 72 6d 61 70 54 79 70 65 2c 20 26 69 50 74 72  trmapType, &iPtr
18c69 6d 61 70 50 61 72 65 6e 74 29 3b 0a 20 20 69 66  mapParent);.  if
18c6a 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
18c6b 29 7b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  ){.    if( rc==S
18c6c 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72  QLITE_NOMEM || r
18c6d 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  c==SQLITE_IOERR_
18c6e 4e 4f 4d 45 4d 20 29 20 70 43 68 65 63 6b 2d 3e  NOMEM ) pCheck->
18c6f 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
18c70 3b 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e  ;.    checkAppen
18c71 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f  dMsg(pCheck, zCo
18c72 6e 74 65 78 74 2c 20 22 46 61 69 6c 65 64 20 74  ntext, "Failed t
18c73 6f 20 72 65 61 64 20 70 74 72 6d 61 70 20 6b 65  o read ptrmap ke
18c74 79 3d 25 64 22 2c 20 69 43 68 69 6c 64 29 3b 0a  y=%d", iChild);.
18c75 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
18c76 0a 20 20 69 66 28 20 65 50 74 72 6d 61 70 54 79  .  if( ePtrmapTy
18c77 70 65 21 3d 65 54 79 70 65 20 7c 7c 20 69 50 74  pe!=eType || iPt
18c78 72 6d 61 70 50 61 72 65 6e 74 21 3d 69 50 61 72  rmapParent!=iPar
18c79 65 6e 74 20 29 7b 0a 20 20 20 20 63 68 65 63 6b  ent ){.    check
18c7a 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b  AppendMsg(pCheck
18c7b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20 20  , zContext, .   
18c7c 20 20 20 22 42 61 64 20 70 74 72 20 6d 61 70 20     "Bad ptr map 
18c7d 65 6e 74 72 79 20 6b 65 79 3d 25 64 20 65 78 70  entry key=%d exp
18c7e 65 63 74 65 64 3d 28 25 64 2c 25 64 29 20 67 6f  ected=(%d,%d) go
18c7f 74 3d 28 25 64 2c 25 64 29 22 2c 20 0a 20 20 20  t=(%d,%d)", .   
18c80 20 20 20 69 43 68 69 6c 64 2c 20 65 54 79 70 65     iChild, eType
18c81 2c 20 69 50 61 72 65 6e 74 2c 20 65 50 74 72 6d  , iParent, ePtrm
18c82 61 70 54 79 70 65 2c 20 69 50 74 72 6d 61 70 50  apType, iPtrmapP
18c83 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 7d 0a 23 65  arent);.  }.}.#e
18c84 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  ndif../*.** Chec
18c85 6b 20 74 68 65 20 69 6e 74 65 67 72 69 74 79 20  k the integrity 
18c86 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73 74 20  of the freelist 
18c87 6f 72 20 6f 66 20 61 6e 20 6f 76 65 72 66 6c 6f  or of an overflo
18c88 77 20 70 61 67 65 20 6c 69 73 74 2e 0a 2a 2a 20  w page list..** 
18c89 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20  Verify that the 
18c8a 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
18c8b 6f 6e 20 74 68 65 20 6c 69 73 74 20 69 73 20 4e  on the list is N
18c8c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
18c8d 20 63 68 65 63 6b 4c 69 73 74 28 0a 20 20 49 6e   checkList(.  In
18c8e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65 63  tegrityCk *pChec
18c8f 6b 2c 20 20 2f 2a 20 49 6e 74 65 67 72 69 74 79  k,  /* Integrity
18c90 20 63 68 65 63 6b 69 6e 67 20 63 6f 6e 74 65 78   checking contex
18c91 74 20 2a 2f 0a 20 20 69 6e 74 20 69 73 46 72 65  t */.  int isFre
18c92 65 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a 20  eList,       /* 
18c93 54 72 75 65 20 66 6f 72 20 61 20 66 72 65 65 6c  True for a freel
18c94 69 73 74 2e 20 20 46 61 6c 73 65 20 66 6f 72 20  ist.  False for 
18c95 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 6c 69  overflow page li
18c96 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 67  st */.  int iPag
18c97 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e,            /*
18c98 20 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72   Page number for
18c99 20 66 69 72 73 74 20 70 61 67 65 20 69 6e 20 74   first page in t
18c9a 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74  he list */.  int
18c9b 20 4e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   N,             
18c9c 20 20 20 2f 2a 20 45 78 70 65 63 74 65 64 20 6e     /* Expected n
18c9d 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69  umber of pages i
18c9e 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20  n the list */.  
18c9f 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74 20 20  char *zContext  
18ca0 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74        /* Context
18ca1 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61   for error messa
18ca2 67 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ges */.){.  int 
18ca3 69 3b 0a 20 20 69 6e 74 20 65 78 70 65 63 74 65  i;.  int expecte
18ca4 64 20 3d 20 4e 3b 0a 20 20 69 6e 74 20 69 46 69  d = N;.  int iFi
18ca5 72 73 74 20 3d 20 69 50 61 67 65 3b 0a 20 20 77  rst = iPage;.  w
18ca6 68 69 6c 65 28 20 4e 2d 2d 20 3e 20 30 20 26 26  hile( N-- > 0 &&
18ca7 20 70 43 68 65 63 6b 2d 3e 6d 78 45 72 72 20 29   pCheck->mxErr )
18ca8 7b 0a 20 20 20 20 44 62 50 61 67 65 20 2a 70 4f  {.    DbPage *pO
18ca9 76 66 6c 50 61 67 65 3b 0a 20 20 20 20 75 6e 73  vflPage;.    uns
18caa 69 67 6e 65 64 20 63 68 61 72 20 2a 70 4f 76 66  igned char *pOvf
18cab 6c 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20 69  lData;.    if( i
18cac 50 61 67 65 3c 31 20 29 7b 0a 20 20 20 20 20 20  Page<1 ){.      
18cad 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70  checkAppendMsg(p
18cae 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c  Check, zContext,
18caf 0a 20 20 20 20 20 20 20 20 20 22 25 64 20 6f 66  .         "%d of
18cb0 20 25 64 20 70 61 67 65 73 20 6d 69 73 73 69 6e   %d pages missin
18cb1 67 20 66 72 6f 6d 20 6f 76 65 72 66 6c 6f 77 20  g from overflow 
18cb2 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20 61 74  list starting at
18cb3 20 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20   %d",.          
18cb4 4e 2b 31 2c 20 65 78 70 65 63 74 65 64 2c 20 69  N+1, expected, i
18cb5 46 69 72 73 74 29 3b 0a 20 20 20 20 20 20 62 72  First);.      br
18cb6 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  eak;.    }.    i
18cb7 66 28 20 63 68 65 63 6b 52 65 66 28 70 43 68 65  f( checkRef(pChe
18cb8 63 6b 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74  ck, iPage, zCont
18cb9 65 78 74 29 20 29 20 62 72 65 61 6b 3b 0a 20 20  ext) ) break;.  
18cba 20 20 69 66 28 20 73 71 6c 69 74 65 33 50 61 67    if( sqlite3Pag
18cbb 65 72 47 65 74 28 70 43 68 65 63 6b 2d 3e 70 50  erGet(pCheck->pP
18cbc 61 67 65 72 2c 20 28 50 67 6e 6f 29 69 50 61 67  ager, (Pgno)iPag
18cbd 65 2c 20 26 70 4f 76 66 6c 50 61 67 65 29 20 29  e, &pOvflPage) )
18cbe 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70  {.      checkApp
18cbf 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a  endMsg(pCheck, z
18cc0 43 6f 6e 74 65 78 74 2c 20 22 66 61 69 6c 65 64  Context, "failed
18cc1 20 74 6f 20 67 65 74 20 70 61 67 65 20 25 64 22   to get page %d"
18cc2 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20  , iPage);.      
18cc3 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
18cc4 20 70 4f 76 66 6c 44 61 74 61 20 3d 20 28 75 6e   pOvflData = (un
18cc5 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 73 71  signed char *)sq
18cc6 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74  lite3PagerGetDat
18cc7 61 28 70 4f 76 66 6c 50 61 67 65 29 3b 0a 20 20  a(pOvflPage);.  
18cc8 20 20 69 66 28 20 69 73 46 72 65 65 4c 69 73 74    if( isFreeList
18cc9 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20   ){.      int n 
18cca 3d 20 67 65 74 34 62 79 74 65 28 26 70 4f 76 66  = get4byte(&pOvf
18ccb 6c 44 61 74 61 5b 34 5d 29 3b 0a 23 69 66 6e 64  lData[4]);.#ifnd
18ccc 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
18ccd 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20  UTOVACUUM.      
18cce 69 66 28 20 70 43 68 65 63 6b 2d 3e 70 42 74 2d  if( pCheck->pBt-
18ccf 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20  >autoVacuum ){. 
18cd0 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d         checkPtrm
18cd1 61 70 28 70 43 68 65 63 6b 2c 20 69 50 61 67 65  ap(pCheck, iPage
18cd2 2c 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47  , PTRMAP_FREEPAG
18cd3 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 78 74 29 3b  E, 0, zContext);
18cd4 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
18cd5 20 20 20 20 20 20 69 66 28 20 6e 3e 70 43 68 65        if( n>pChe
18cd6 63 6b 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53  ck->pBt->usableS
18cd7 69 7a 65 2f 34 2d 32 20 29 7b 0a 20 20 20 20 20  ize/4-2 ){.     
18cd8 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
18cd9 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65  g(pCheck, zConte
18cda 78 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 22  xt,.           "
18cdb 66 72 65 65 6c 69 73 74 20 6c 65 61 66 20 63 6f  freelist leaf co
18cdc 75 6e 74 20 74 6f 6f 20 62 69 67 20 6f 6e 20 70  unt too big on p
18cdd 61 67 65 20 25 64 22 2c 20 69 50 61 67 65 29 3b  age %d", iPage);
18cde 0a 20 20 20 20 20 20 20 20 4e 2d 2d 3b 0a 20 20  .        N--;.  
18cdf 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
18ce0 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b     for(i=0; i<n;
18ce1 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
18ce2 20 50 67 6e 6f 20 69 46 72 65 65 50 61 67 65 20   Pgno iFreePage 
18ce3 3d 20 67 65 74 34 62 79 74 65 28 26 70 4f 76 66  = get4byte(&pOvf
18ce4 6c 44 61 74 61 5b 38 2b 69 2a 34 5d 29 3b 0a 23  lData[8+i*4]);.#
18ce5 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
18ce6 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
18ce7 20 20 20 20 20 20 20 20 69 66 28 20 70 43 68 65          if( pChe
18ce8 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61 63  ck->pBt->autoVac
18ce9 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20  uum ){.         
18cea 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70     checkPtrmap(p
18ceb 43 68 65 63 6b 2c 20 69 46 72 65 65 50 61 67 65  Check, iFreePage
18cec 2c 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47  , PTRMAP_FREEPAG
18ced 45 2c 20 30 2c 20 7a 43 6f 6e 74 65 78 74 29 3b  E, 0, zContext);
18cee 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 23 65 6e  .          }.#en
18cef 64 69 66 0a 20 20 20 20 20 20 20 20 20 20 63 68  dif.          ch
18cf0 65 63 6b 52 65 66 28 70 43 68 65 63 6b 2c 20 69  eckRef(pCheck, i
18cf1 46 72 65 65 50 61 67 65 2c 20 7a 43 6f 6e 74 65  FreePage, zConte
18cf2 78 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  xt);.        }. 
18cf3 20 20 20 20 20 20 20 4e 20 2d 3d 20 6e 3b 0a 20         N -= n;. 
18cf4 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66       }.    }.#if
18cf5 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
18cf6 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
18cf7 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49  else{.      /* I
18cf8 66 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  f this database 
18cf9 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61  supports auto-va
18cfa 63 75 75 6d 20 61 6e 64 20 69 50 61 67 65 20 69  cuum and iPage i
18cfb 73 20 6e 6f 74 20 74 68 65 20 6c 61 73 74 0a 20  s not the last. 
18cfc 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e 20       ** page in 
18cfd 74 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 6c 69  this overflow li
18cfe 73 74 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74  st, check that t
18cff 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 65  he pointer-map e
18d00 6e 74 72 79 20 66 6f 72 0a 20 20 20 20 20 20 2a  ntry for.      *
18d01 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * the following 
18d02 70 61 67 65 20 6d 61 74 63 68 65 73 20 69 50 61  page matches iPa
18d03 67 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ge..      */.   
18d04 20 20 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 70     if( pCheck->p
18d05 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 26  Bt->autoVacuum &
18d06 26 20 4e 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  & N>0 ){.       
18d07 20 69 20 3d 20 67 65 74 34 62 79 74 65 28 70 4f   i = get4byte(pO
18d08 76 66 6c 44 61 74 61 29 3b 0a 20 20 20 20 20 20  vflData);.      
18d09 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43    checkPtrmap(pC
18d0a 68 65 63 6b 2c 20 69 2c 20 50 54 52 4d 41 50 5f  heck, i, PTRMAP_
18d0b 4f 56 45 52 46 4c 4f 57 32 2c 20 69 50 61 67 65  OVERFLOW2, iPage
18d0c 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20  , zContext);.   
18d0d 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69     }.    }.#endi
18d0e 66 0a 20 20 20 20 69 50 61 67 65 20 3d 20 67 65  f.    iPage = ge
18d0f 74 34 62 79 74 65 28 70 4f 76 66 6c 44 61 74 61  t4byte(pOvflData
18d10 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61  );.    sqlite3Pa
18d11 67 65 72 55 6e 72 65 66 28 70 4f 76 66 6c 50 61  gerUnref(pOvflPa
18d12 67 65 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69  ge);.  }.}.#endi
18d13 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
18d14 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b  _INTEGRITY_CHECK
18d15 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
18d16 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49  ITE_OMIT_INTEGRI
18d17 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20 44  TY_CHECK./*.** D
18d18 6f 20 76 61 72 69 6f 75 73 20 73 61 6e 69 74 79  o various sanity
18d19 20 63 68 65 63 6b 73 20 6f 6e 20 61 20 73 69 6e   checks on a sin
18d1a 67 6c 65 20 70 61 67 65 20 6f 66 20 61 20 74 72  gle page of a tr
18d1b 65 65 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74  ee.  Return.** t
18d1c 68 65 20 74 72 65 65 20 64 65 70 74 68 2e 20 20  he tree depth.  
18d1d 52 6f 6f 74 20 70 61 67 65 73 20 72 65 74 75 72  Root pages retur
18d1e 6e 20 30 2e 20 20 50 61 72 65 6e 74 73 20 6f 66  n 0.  Parents of
18d1f 20 72 6f 6f 74 20 70 61 67 65 73 0a 2a 2a 20 72   root pages.** r
18d20 65 74 75 72 6e 20 31 2c 20 61 6e 64 20 73 6f 20  eturn 1, and so 
18d21 66 6f 72 74 68 2e 0a 2a 2a 20 0a 2a 2a 20 54 68  forth..** .** Th
18d22 65 73 65 20 63 68 65 63 6b 73 20 61 72 65 20 64  ese checks are d
18d23 6f 6e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  one:.**.**      
18d24 31 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68  1.  Make sure th
18d25 61 74 20 63 65 6c 6c 73 20 61 6e 64 20 66 72 65  at cells and fre
18d26 65 62 6c 6f 63 6b 73 20 64 6f 20 6e 6f 74 20 6f  eblocks do not o
18d27 76 65 72 6c 61 70 0a 2a 2a 20 20 20 20 20 20 20  verlap.**       
18d28 20 20 20 62 75 74 20 63 6f 6d 62 69 6e 65 20 74     but combine t
18d29 6f 20 63 6f 6d 70 6c 65 74 65 6c 79 20 63 6f 76  o completely cov
18d2a 65 72 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 20  er the page..** 
18d2b 20 4e 4f 20 20 32 2e 20 20 4d 61 6b 65 20 73 75   NO  2.  Make su
18d2c 72 65 20 63 65 6c 6c 20 6b 65 79 73 20 61 72 65  re cell keys are
18d2d 20 69 6e 20 6f 72 64 65 72 2e 0a 2a 2a 20 20 4e   in order..**  N
18d2e 4f 20 20 33 2e 20 20 4d 61 6b 65 20 73 75 72 65  O  3.  Make sure
18d2f 20 6e 6f 20 6b 65 79 20 69 73 20 6c 65 73 73 20   no key is less 
18d30 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
18d31 20 7a 4c 6f 77 65 72 42 6f 75 6e 64 2e 0a 2a 2a   zLowerBound..**
18d32 20 20 4e 4f 20 20 34 2e 20 20 4d 61 6b 65 20 73    NO  4.  Make s
18d33 75 72 65 20 6e 6f 20 6b 65 79 20 69 73 20 67 72  ure no key is gr
18d34 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71  eater than or eq
18d35 75 61 6c 20 74 6f 20 7a 55 70 70 65 72 42 6f 75  ual to zUpperBou
18d36 6e 64 2e 0a 2a 2a 20 20 20 20 20 20 35 2e 20 20  nd..**      5.  
18d37 43 68 65 63 6b 20 74 68 65 20 69 6e 74 65 67 72  Check the integr
18d38 69 74 79 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20  ity of overflow 
18d39 70 61 67 65 73 2e 0a 2a 2a 20 20 20 20 20 20 36  pages..**      6
18d3a 2e 20 20 52 65 63 75 72 73 69 76 65 6c 79 20 63  .  Recursively c
18d3b 61 6c 6c 20 63 68 65 63 6b 54 72 65 65 50 61 67  all checkTreePag
18d3c 65 20 6f 6e 20 61 6c 6c 20 63 68 69 6c 64 72 65  e on all childre
18d3d 6e 2e 0a 2a 2a 20 20 20 20 20 20 37 2e 20 20 56  n..**      7.  V
18d3e 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20 64  erify that the d
18d3f 65 70 74 68 20 6f 66 20 61 6c 6c 20 63 68 69 6c  epth of all chil
18d40 64 72 65 6e 20 69 73 20 74 68 65 20 73 61 6d 65  dren is the same
18d41 2e 0a 2a 2a 20 20 20 20 20 20 38 2e 20 20 4d 61  ..**      8.  Ma
18d42 6b 65 20 73 75 72 65 20 74 68 69 73 20 70 61 67  ke sure this pag
18d43 65 20 69 73 20 61 74 20 6c 65 61 73 74 20 33 33  e is at least 33
18d44 25 20 66 75 6c 6c 20 6f 72 20 65 6c 73 65 20 69  % full or else i
18d45 74 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  t is.**         
18d46 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68 65   the root of the
18d47 20 74 72 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   tree..*/.static
18d48 20 69 6e 74 20 63 68 65 63 6b 54 72 65 65 50 61   int checkTreePa
18d49 67 65 28 0a 20 20 49 6e 74 65 67 72 69 74 79 43  ge(.  IntegrityC
18d4a 6b 20 2a 70 43 68 65 63 6b 2c 20 20 2f 2a 20 43  k *pCheck,  /* C
18d4b 6f 6e 74 65 78 74 20 66 6f 72 20 74 68 65 20 73  ontext for the s
18d4c 61 6e 69 74 79 20 63 68 65 63 6b 20 2a 2f 0a 20  anity check */. 
18d4d 20 69 6e 74 20 69 50 61 67 65 2c 20 20 20 20 20   int iPage,     
18d4e 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e         /* Page n
18d4f 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61 67  umber of the pag
18d50 65 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 20 20  e to check */.  
18d51 63 68 61 72 20 2a 7a 50 61 72 65 6e 74 43 6f 6e  char *zParentCon
18d52 74 65 78 74 20 20 2f 2a 20 50 61 72 65 6e 74 20  text  /* Parent 
18d53 63 6f 6e 74 65 78 74 20 2a 2f 0a 29 7b 0a 20 20  context */.){.  
18d54 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a  MemPage *pPage;.
18d55 20 20 69 6e 74 20 69 2c 20 72 63 2c 20 64 65 70    int i, rc, dep
18d56 74 68 2c 20 64 32 2c 20 70 67 6e 6f 2c 20 63 6e  th, d2, pgno, cn
18d57 74 3b 0a 20 20 69 6e 74 20 68 64 72 2c 20 63 65  t;.  int hdr, ce
18d58 6c 6c 53 74 61 72 74 3b 0a 20 20 69 6e 74 20 6e  llStart;.  int n
18d59 43 65 6c 6c 3b 0a 20 20 75 38 20 2a 64 61 74 61  Cell;.  u8 *data
18d5a 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
18d5b 74 3b 0a 20 20 69 6e 74 20 75 73 61 62 6c 65 53  t;.  int usableS
18d5c 69 7a 65 3b 0a 20 20 63 68 61 72 20 7a 43 6f 6e  ize;.  char zCon
18d5d 74 65 78 74 5b 31 30 30 5d 3b 0a 20 20 63 68 61  text[100];.  cha
18d5e 72 20 2a 68 69 74 20 3d 20 30 3b 0a 0a 20 20 73  r *hit = 0;..  s
18d5f 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
18d60 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29  sizeof(zContext)
18d61 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 50 61 67  , zContext, "Pag
18d62 65 20 25 64 3a 20 22 2c 20 69 50 61 67 65 29 3b  e %d: ", iPage);
18d63 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61  ..  /* Check tha
18d64 74 20 74 68 65 20 70 61 67 65 20 65 78 69 73 74  t the page exist
18d65 73 0a 20 20 2a 2f 0a 20 20 70 42 74 20 3d 20 70  s.  */.  pBt = p
18d66 43 68 65 63 6b 2d 3e 70 42 74 3b 0a 20 20 75 73  Check->pBt;.  us
18d67 61 62 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e  ableSize = pBt->
18d68 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 69 66  usableSize;.  if
18d69 28 20 69 50 61 67 65 3d 3d 30 20 29 20 72 65 74  ( iPage==0 ) ret
18d6a 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 63 68 65  urn 0;.  if( che
18d6b 63 6b 52 65 66 28 70 43 68 65 63 6b 2c 20 69 50  ckRef(pCheck, iP
18d6c 61 67 65 2c 20 7a 50 61 72 65 6e 74 43 6f 6e 74  age, zParentCont
18d6d 65 78 74 29 20 29 20 72 65 74 75 72 6e 20 30 3b  ext) ) return 0;
18d6e 0a 20 20 69 66 28 20 28 72 63 20 3d 20 62 74 72  .  if( (rc = btr
18d6f 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 28  eeGetPage(pBt, (
18d70 50 67 6e 6f 29 69 50 61 67 65 2c 20 26 70 50 61  Pgno)iPage, &pPa
18d71 67 65 2c 20 30 29 29 21 3d 30 20 29 7b 0a 20 20  ge, 0))!=0 ){.  
18d72 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67    checkAppendMsg
18d73 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78  (pCheck, zContex
18d74 74 2c 0a 20 20 20 20 20 20 20 22 75 6e 61 62 6c  t,.       "unabl
18d75 65 20 74 6f 20 67 65 74 20 74 68 65 20 70 61 67  e to get the pag
18d76 65 2e 20 65 72 72 6f 72 20 63 6f 64 65 3d 25 64  e. error code=%d
18d77 22 2c 20 72 63 29 3b 0a 20 20 20 20 72 65 74 75  ", rc);.    retu
18d78 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rn 0;.  }..  /* 
18d79 43 6c 65 61 72 20 4d 65 6d 50 61 67 65 2e 69 73  Clear MemPage.is
18d7a 49 6e 69 74 20 74 6f 20 6d 61 6b 65 20 73 75 72  Init to make sur
18d7b 65 20 74 68 65 20 63 6f 72 72 75 70 74 69 6f 6e  e the corruption
18d7c 20 64 65 74 65 63 74 69 6f 6e 20 63 6f 64 65 20   detection code 
18d7d 69 6e 0a 20 20 2a 2a 20 62 74 72 65 65 49 6e 69  in.  ** btreeIni
18d7e 74 50 61 67 65 28 29 20 69 73 20 65 78 65 63 75  tPage() is execu
18d7f 74 65 64 2e 20 20 2a 2f 0a 20 20 70 50 61 67 65  ted.  */.  pPage
18d80 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20  ->isInit = 0;.  
18d81 69 66 28 20 28 72 63 20 3d 20 62 74 72 65 65 49  if( (rc = btreeI
18d82 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 29 21  nitPage(pPage))!
18d83 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  =0 ){.    assert
18d84 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 52  ( rc==SQLITE_COR
18d85 52 55 50 54 20 29 3b 20 20 2f 2a 20 54 68 65 20  RUPT );  /* The 
18d86 6f 6e 6c 79 20 70 6f 73 73 69 62 6c 65 20 65 72  only possible er
18d87 72 6f 72 20 66 72 6f 6d 20 49 6e 69 74 50 61 67  ror from InitPag
18d88 65 20 2a 2f 0a 20 20 20 20 63 68 65 63 6b 41 70  e */.    checkAp
18d89 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
18d8a 7a 43 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20  zContext, .     
18d8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 62                "b
18d8c 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20 72  treeInitPage() r
18d8d 65 74 75 72 6e 73 20 65 72 72 6f 72 20 63 6f 64  eturns error cod
18d8e 65 20 25 64 22 2c 20 72 63 29 3b 0a 20 20 20 20  e %d", rc);.    
18d8f 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67  releasePage(pPag
18d90 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  e);.    return 0
18d91 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63  ;.  }..  /* Chec
18d92 6b 20 6f 75 74 20 61 6c 6c 20 74 68 65 20 63 65  k out all the ce
18d93 6c 6c 73 2e 0a 20 20 2a 2f 0a 20 20 64 65 70 74  lls..  */.  dept
18d94 68 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30  h = 0;.  for(i=0
18d95 3b 20 69 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  ; i<pPage->nCell
18d96 20 26 26 20 70 43 68 65 63 6b 2d 3e 6d 78 45 72   && pCheck->mxEr
18d97 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20  r; i++){.    u8 
18d98 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 75 33 32 20  *pCell;.    u32 
18d99 73 7a 3b 0a 20 20 20 20 43 65 6c 6c 49 6e 66 6f  sz;.    CellInfo
18d9a 20 69 6e 66 6f 3b 0a 0a 20 20 20 20 2f 2a 20 43   info;..    /* C
18d9b 68 65 63 6b 20 70 61 79 6c 6f 61 64 20 6f 76 65  heck payload ove
18d9c 72 66 6c 6f 77 20 70 61 67 65 73 0a 20 20 20 20  rflow pages.    
18d9d 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  */.    sqlite3_s
18d9e 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
18d9f 43 6f 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74 65  Context), zConte
18da0 78 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  xt,.            
18da1 20 22 4f 6e 20 74 72 65 65 20 70 61 67 65 20 25   "On tree page %
18da2 64 20 63 65 6c 6c 20 25 64 3a 20 22 2c 20 69 50  d cell %d: ", iP
18da3 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 70 43 65  age, i);.    pCe
18da4 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50  ll = findCell(pP
18da5 61 67 65 2c 69 29 3b 0a 20 20 20 20 62 74 72 65  age,i);.    btre
18da6 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 50  eParseCellPtr(pP
18da7 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e 66  age, pCell, &inf
18da8 6f 29 3b 0a 20 20 20 20 73 7a 20 3d 20 69 6e 66  o);.    sz = inf
18da9 6f 2e 6e 44 61 74 61 3b 0a 20 20 20 20 69 66 28  o.nData;.    if(
18daa 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20   !pPage->intKey 
18dab 29 20 73 7a 20 2b 3d 20 28 69 6e 74 29 69 6e 66  ) sz += (int)inf
18dac 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 61 73 73 65  o.nKey;.    asse
18dad 72 74 28 20 73 7a 3d 3d 69 6e 66 6f 2e 6e 50 61  rt( sz==info.nPa
18dae 79 6c 6f 61 64 20 29 3b 0a 20 20 20 20 69 66 28  yload );.    if(
18daf 20 28 73 7a 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c   (sz>info.nLocal
18db0 29 20 0a 20 20 20 20 20 26 26 20 28 26 70 43 65  ) .     && (&pCe
18db1 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  ll[info.iOverflo
18db2 77 5d 3c 3d 26 70 50 61 67 65 2d 3e 61 44 61 74  w]<=&pPage->aDat
18db3 61 5b 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  a[pBt->usableSiz
18db4 65 5d 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20  e]).    ){.     
18db5 20 69 6e 74 20 6e 50 61 67 65 20 3d 20 28 73 7a   int nPage = (sz
18db6 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2b   - info.nLocal +
18db7 20 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 35 29   usableSize - 5)
18db8 2f 28 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34  /(usableSize - 4
18db9 29 3b 0a 20 20 20 20 20 20 50 67 6e 6f 20 70 67  );.      Pgno pg
18dba 6e 6f 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 74  noOvfl = get4byt
18dbb 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f  e(&pCell[info.iO
18dbc 76 65 72 66 6c 6f 77 5d 29 3b 0a 23 69 66 6e 64  verflow]);.#ifnd
18dbd 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
18dbe 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20  UTOVACUUM.      
18dbf 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
18dc0 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 63  uum ){.        c
18dc1 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65 63  heckPtrmap(pChec
18dc2 6b 2c 20 70 67 6e 6f 4f 76 66 6c 2c 20 50 54 52  k, pgnoOvfl, PTR
18dc3 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 2c 20 69  MAP_OVERFLOW1, i
18dc4 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b  Page, zContext);
18dc5 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
18dc6 20 20 20 20 20 20 63 68 65 63 6b 4c 69 73 74 28        checkList(
18dc7 70 43 68 65 63 6b 2c 20 30 2c 20 70 67 6e 6f 4f  pCheck, 0, pgnoO
18dc8 76 66 6c 2c 20 6e 50 61 67 65 2c 20 7a 43 6f 6e  vfl, nPage, zCon
18dc9 74 65 78 74 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  text);.    }..  
18dca 20 20 2f 2a 20 43 68 65 63 6b 20 73 61 6e 69 74    /* Check sanit
18dcb 79 20 6f 66 20 6c 65 66 74 20 63 68 69 6c 64 20  y of left child 
18dcc 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  page..    */.   
18dcd 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
18dce 66 20 29 7b 0a 20 20 20 20 20 20 70 67 6e 6f 20  f ){.      pgno 
18dcf 3d 20 67 65 74 34 62 79 74 65 28 70 43 65 6c 6c  = get4byte(pCell
18dd0 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
18dd1 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
18dd2 4d 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d  M.      if( pBt-
18dd3 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20  >autoVacuum ){. 
18dd4 20 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d         checkPtrm
18dd5 61 70 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c  ap(pCheck, pgno,
18dd6 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 69   PTRMAP_BTREE, i
18dd7 50 61 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b  Page, zContext);
18dd8 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
18dd9 20 20 20 20 20 20 64 32 20 3d 20 63 68 65 63 6b        d2 = check
18dda 54 72 65 65 50 61 67 65 28 70 43 68 65 63 6b 2c  TreePage(pCheck,
18ddb 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74 65 78 74 29   pgno, zContext)
18ddc 3b 0a 20 20 20 20 20 20 69 66 28 20 69 3e 30 20  ;.      if( i>0 
18ddd 26 26 20 64 32 21 3d 64 65 70 74 68 20 29 7b 0a  && d2!=depth ){.
18dde 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70          checkApp
18ddf 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a  endMsg(pCheck, z
18de0 43 6f 6e 74 65 78 74 2c 20 22 43 68 69 6c 64 20  Context, "Child 
18de1 70 61 67 65 20 64 65 70 74 68 20 64 69 66 66 65  page depth diffe
18de2 72 73 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rs");.      }.  
18de3 20 20 20 20 64 65 70 74 68 20 3d 20 64 32 3b 0a      depth = d2;.
18de4 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
18de5 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
18de6 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74 34 62      pgno = get4b
18de7 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74  yte(&pPage->aDat
18de8 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73  a[pPage->hdrOffs
18de9 65 74 2b 38 5d 29 3b 0a 20 20 20 20 73 71 6c 69  et+8]);.    sqli
18dea 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
18deb 65 6f 66 28 7a 43 6f 6e 74 65 78 74 29 2c 20 7a  eof(zContext), z
18dec 43 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20  Context, .      
18ded 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
18dee 4f 6e 20 70 61 67 65 20 25 64 20 61 74 20 72 69  On page %d at ri
18def 67 68 74 20 63 68 69 6c 64 3a 20 22 2c 20 69 50  ght child: ", iP
18df0 61 67 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51  age);.#ifndef SQ
18df1 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
18df2 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42 74  CUUM.    if( pBt
18df3 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a  ->autoVacuum ){.
18df4 20 20 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61        checkPtrma
18df5 70 28 70 43 68 65 63 6b 2c 20 70 67 6e 6f 2c 20  p(pCheck, pgno, 
18df6 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 69 50  PTRMAP_BTREE, iP
18df7 61 67 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 23  age, 0);.    }.#
18df8 65 6e 64 69 66 0a 20 20 20 20 63 68 65 63 6b 54  endif.    checkT
18df9 72 65 65 50 61 67 65 28 70 43 68 65 63 6b 2c 20  reePage(pCheck, 
18dfa 70 67 6e 6f 2c 20 7a 43 6f 6e 74 65 78 74 29 3b  pgno, zContext);
18dfb 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 43 68 65 63  .  }. .  /* Chec
18dfc 6b 20 66 6f 72 20 63 6f 6d 70 6c 65 74 65 20 63  k for complete c
18dfd 6f 76 65 72 61 67 65 20 6f 66 20 74 68 65 20 70  overage of the p
18dfe 61 67 65 0a 20 20 2a 2f 0a 20 20 64 61 74 61 20  age.  */.  data 
18dff 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a  = pPage->aData;.
18e00 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68    hdr = pPage->h
18e01 64 72 4f 66 66 73 65 74 3b 0a 20 20 68 69 74 20  drOffset;.  hit 
18e02 3d 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c  = sqlite3PageMal
18e03 6c 6f 63 28 20 70 42 74 2d 3e 70 61 67 65 53 69  loc( pBt->pageSi
18e04 7a 65 20 29 3b 0a 20 20 69 66 28 20 68 69 74 3d  ze );.  if( hit=
18e05 3d 30 20 29 7b 0a 20 20 20 20 70 43 68 65 63 6b  =0 ){.    pCheck
18e06 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
18e07 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   1;.  }else{.   
18e08 20 75 31 36 20 63 6f 6e 74 65 6e 74 4f 66 66 73   u16 contentOffs
18e09 65 74 20 3d 20 67 65 74 32 62 79 74 65 28 26 64  et = get2byte(&d
18e0a 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20  ata[hdr+5]);.   
18e0b 20 61 73 73 65 72 74 28 20 63 6f 6e 74 65 6e 74   assert( content
18e0c 4f 66 66 73 65 74 3c 3d 75 73 61 62 6c 65 53 69  Offset<=usableSi
18e0d 7a 65 20 29 3b 20 20 2f 2a 20 45 6e 66 6f 72 63  ze );  /* Enforc
18e0e 65 64 20 62 79 20 62 74 72 65 65 49 6e 69 74 50  ed by btreeInitP
18e0f 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 6d 65 6d  age() */.    mem
18e10 73 65 74 28 68 69 74 2b 63 6f 6e 74 65 6e 74 4f  set(hit+contentO
18e11 66 66 73 65 74 2c 20 30 2c 20 75 73 61 62 6c 65  ffset, 0, usable
18e12 53 69 7a 65 2d 63 6f 6e 74 65 6e 74 4f 66 66 73  Size-contentOffs
18e13 65 74 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  et);.    memset(
18e14 68 69 74 2c 20 31 2c 20 63 6f 6e 74 65 6e 74 4f  hit, 1, contentO
18e15 66 66 73 65 74 29 3b 0a 20 20 20 20 6e 43 65 6c  ffset);.    nCel
18e16 6c 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  l = get2byte(&da
18e17 74 61 5b 68 64 72 2b 33 5d 29 3b 0a 20 20 20 20  ta[hdr+3]);.    
18e18 63 65 6c 6c 53 74 61 72 74 20 3d 20 68 64 72 20  cellStart = hdr 
18e19 2b 20 31 32 20 2d 20 34 2a 70 50 61 67 65 2d 3e  + 12 - 4*pPage->
18e1a 6c 65 61 66 3b 0a 20 20 20 20 66 6f 72 28 69 3d  leaf;.    for(i=
18e1b 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29  0; i<nCell; i++)
18e1c 7b 0a 20 20 20 20 20 20 69 6e 74 20 70 63 20 3d  {.      int pc =
18e1d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
18e1e 63 65 6c 6c 53 74 61 72 74 2b 69 2a 32 5d 29 3b  cellStart+i*2]);
18e1f 0a 20 20 20 20 20 20 75 31 36 20 73 69 7a 65 20  .      u16 size 
18e20 3d 20 31 30 32 34 3b 0a 20 20 20 20 20 20 69 6e  = 1024;.      in
18e21 74 20 6a 3b 0a 20 20 20 20 20 20 69 66 28 20 70  t j;.      if( p
18e22 63 3c 3d 75 73 61 62 6c 65 53 69 7a 65 2d 34 20  c<=usableSize-4 
18e23 29 7b 0a 20 20 20 20 20 20 20 20 73 69 7a 65 20  ){.        size 
18e24 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50  = cellSizePtr(pP
18e25 61 67 65 2c 20 26 64 61 74 61 5b 70 63 5d 29 3b  age, &data[pc]);
18e26 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
18e27 66 28 20 28 70 63 2b 73 69 7a 65 2d 31 29 3e 3d  f( (pc+size-1)>=
18e28 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20  usableSize ){.  
18e29 20 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e        checkAppen
18e2a 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 30 2c 20  dMsg(pCheck, 0, 
18e2b 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 43 6f  .            "Co
18e2c 72 72 75 70 74 69 6f 6e 20 64 65 74 65 63 74 65  rruption detecte
18e2d 64 20 69 6e 20 63 65 6c 6c 20 25 64 20 6f 6e 20  d in cell %d on 
18e2e 70 61 67 65 20 25 64 22 2c 69 2c 69 50 61 67 65  page %d",i,iPage
18e2f 2c 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ,0);.      }else
18e30 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  {.        for(j=
18e31 70 63 2b 73 69 7a 65 2d 31 3b 20 6a 3e 3d 70 63  pc+size-1; j>=pc
18e32 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d 2b 2b 3b  ; j--) hit[j]++;
18e33 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
18e34 20 20 20 69 20 3d 20 67 65 74 32 62 79 74 65 28     i = get2byte(
18e35 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b 0a 20  &data[hdr+1]);. 
18e36 20 20 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b     while( i>0 ){
18e37 0a 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65 2c  .      int size,
18e38 20 6a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   j;.      assert
18e39 28 20 69 3c 3d 75 73 61 62 6c 65 53 69 7a 65 2d  ( i<=usableSize-
18e3a 34 20 29 3b 20 20 20 20 20 2f 2a 20 45 6e 66 6f  4 );     /* Enfo
18e3b 72 63 65 64 20 62 79 20 62 74 72 65 65 49 6e 69  rced by btreeIni
18e3c 74 50 61 67 65 28 29 20 2a 2f 0a 20 20 20 20 20  tPage() */.     
18e3d 20 73 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65   size = get2byte
18e3e 28 26 64 61 74 61 5b 69 2b 32 5d 29 3b 0a 20 20  (&data[i+2]);.  
18e3f 20 20 20 20 61 73 73 65 72 74 28 20 69 2b 73 69      assert( i+si
18e40 7a 65 3c 3d 75 73 61 62 6c 65 53 69 7a 65 20 29  ze<=usableSize )
18e41 3b 20 20 2f 2a 20 45 6e 66 6f 72 63 65 64 20 62  ;  /* Enforced b
18e42 79 20 62 74 72 65 65 49 6e 69 74 50 61 67 65 28  y btreeInitPage(
18e43 29 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 6a  ) */.      for(j
18e44 3d 69 2b 73 69 7a 65 2d 31 3b 20 6a 3e 3d 69 3b  =i+size-1; j>=i;
18e45 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d 2b 2b 3b 0a   j--) hit[j]++;.
18e46 20 20 20 20 20 20 6a 20 3d 20 67 65 74 32 62 79        j = get2by
18e47 74 65 28 26 64 61 74 61 5b 69 5d 29 3b 0a 20 20  te(&data[i]);.  
18e48 20 20 20 20 61 73 73 65 72 74 28 20 6a 3d 3d 30      assert( j==0
18e49 20 7c 7c 20 6a 3e 69 2b 73 69 7a 65 20 29 3b 20   || j>i+size ); 
18e4a 20 2f 2a 20 45 6e 66 6f 72 63 65 64 20 62 79 20   /* Enforced by 
18e4b 62 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20  btreeInitPage() 
18e4c 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
18e4d 20 6a 3c 3d 75 73 61 62 6c 65 53 69 7a 65 2d 34   j<=usableSize-4
18e4e 20 29 3b 20 20 20 2f 2a 20 45 6e 66 6f 72 63 65   );   /* Enforce
18e4f 64 20 62 79 20 62 74 72 65 65 49 6e 69 74 50 61  d by btreeInitPa
18e50 67 65 28 29 20 2a 2f 0a 20 20 20 20 20 20 69 20  ge() */.      i 
18e51 3d 20 6a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  = j;.    }.    f
18e52 6f 72 28 69 3d 63 6e 74 3d 30 3b 20 69 3c 75 73  or(i=cnt=0; i<us
18e53 61 62 6c 65 53 69 7a 65 3b 20 69 2b 2b 29 7b 0a  ableSize; i++){.
18e54 20 20 20 20 20 20 69 66 28 20 68 69 74 5b 69 5d        if( hit[i]
18e55 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63  ==0 ){.        c
18e56 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73  nt++;.      }els
18e57 65 20 69 66 28 20 68 69 74 5b 69 5d 3e 31 20 29  e if( hit[i]>1 )
18e58 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41  {.        checkA
18e59 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c  ppendMsg(pCheck,
18e5a 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 22 4d   0,.          "M
18e5b 75 6c 74 69 70 6c 65 20 75 73 65 73 20 66 6f 72  ultiple uses for
18e5c 20 62 79 74 65 20 25 64 20 6f 66 20 70 61 67 65   byte %d of page
18e5d 20 25 64 22 2c 20 69 2c 20 69 50 61 67 65 29 3b   %d", i, iPage);
18e5e 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
18e5f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
18e60 20 20 69 66 28 20 63 6e 74 21 3d 64 61 74 61 5b    if( cnt!=data[
18e61 68 64 72 2b 37 5d 20 29 7b 0a 20 20 20 20 20 20  hdr+7] ){.      
18e62 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70  checkAppendMsg(p
18e63 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20  Check, 0, .     
18e64 20 20 20 20 20 22 46 72 61 67 6d 65 6e 74 61 74       "Fragmentat
18e65 69 6f 6e 20 6f 66 20 25 64 20 62 79 74 65 73 20  ion of %d bytes 
18e66 72 65 70 6f 72 74 65 64 20 61 73 20 25 64 20 6f  reported as %d o
18e67 6e 20 70 61 67 65 20 25 64 22 2c 0a 20 20 20 20  n page %d",.    
18e68 20 20 20 20 20 20 63 6e 74 2c 20 64 61 74 61 5b        cnt, data[
18e69 68 64 72 2b 37 5d 2c 20 69 50 61 67 65 29 3b 0a  hdr+7], iPage);.
18e6a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69      }.  }.  sqli
18e6b 74 65 33 50 61 67 65 46 72 65 65 28 68 69 74 29  te3PageFree(hit)
18e6c 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65 28  ;.  releasePage(
18e6d 70 50 61 67 65 29 3b 0a 20 20 72 65 74 75 72 6e  pPage);.  return
18e6e 20 64 65 70 74 68 2b 31 3b 0a 7d 0a 23 65 6e 64   depth+1;.}.#end
18e6f 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
18e70 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  T_INTEGRITY_CHEC
18e71 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  K */..#ifndef SQ
18e72 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52  LITE_OMIT_INTEGR
18e73 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a 20  ITY_CHECK./*.** 
18e74 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65  This routine doe
18e75 73 20 61 20 63 6f 6d 70 6c 65 74 65 20 63 68 65  s a complete che
18e76 63 6b 20 6f 66 20 74 68 65 20 67 69 76 65 6e 20  ck of the given 
18e77 42 54 72 65 65 20 66 69 6c 65 2e 20 20 61 52 6f  BTree file.  aRo
18e78 6f 74 5b 5d 20 69 73 0a 2a 2a 20 61 6e 20 61 72  ot[] is.** an ar
18e79 72 61 79 20 6f 66 20 70 61 67 65 73 20 6e 75 6d  ray of pages num
18e7a 62 65 72 73 20 77 65 72 65 20 65 61 63 68 20 70  bers were each p
18e7b 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 74 68  age number is th
18e7c 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 0a 2a  e root page of.*
18e7d 2a 20 61 20 74 61 62 6c 65 2e 20 20 6e 52 6f 6f  * a table.  nRoo
18e7e 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  t is the number 
18e7f 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 52  of entries in aR
18e80 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 61  oot..**.** A rea
18e81 64 2d 6f 6e 6c 79 20 6f 72 20 72 65 61 64 2d 77  d-only or read-w
18e82 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
18e83 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 64 20   must be opened 
18e84 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 0a 2a  before calling.*
18e85 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  * this function.
18e86 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65  .**.** Write the
18e87 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   number of error
18e88 20 73 65 65 6e 20 69 6e 20 2a 70 6e 45 72 72 2e   seen in *pnErr.
18e89 20 20 45 78 63 65 70 74 20 66 6f 72 20 73 6f 6d    Except for som
18e8a 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f  e memory.** allo
18e8b 63 61 74 69 6f 6e 20 65 72 72 6f 72 73 2c 20 20  cation errors,  
18e8c 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
18e8d 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20   held in memory 
18e8e 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a  obtained from.**
18e8f 20 6d 61 6c 6c 6f 63 20 69 73 20 72 65 74 75 72   malloc is retur
18e90 6e 65 64 20 69 66 20 2a 70 6e 45 72 72 20 69 73  ned if *pnErr is
18e91 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 2a   non-zero.  If *
18e92 70 6e 45 72 72 3d 3d 30 20 74 68 65 6e 20 4e 55  pnErr==0 then NU
18e93 4c 4c 20 69 73 0a 2a 2a 20 72 65 74 75 72 6e 65  LL is.** returne
18e94 64 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72 79 20  d.  If a memory 
18e95 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72  allocation error
18e96 20 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73   occurs, NULL is
18e97 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51   returned..*/.SQ
18e98 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
18e99 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 49  r *sqlite3BtreeI
18e9a 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28 0a 20  ntegrityCheck(. 
18e9b 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20 2f   Btree *p,     /
18e9c 2a 20 54 68 65 20 62 74 72 65 65 20 74 6f 20 62  * The btree to b
18e9d 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a 20 20 69  e checked */.  i
18e9e 6e 74 20 2a 61 52 6f 6f 74 2c 20 20 20 2f 2a 20  nt *aRoot,   /* 
18e9f 41 6e 20 61 72 72 61 79 20 6f 66 20 72 6f 6f 74  An array of root
18ea0 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20 66   pages numbers f
18ea1 6f 72 20 69 6e 64 69 76 69 64 75 61 6c 20 74 72  or individual tr
18ea2 65 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 6f  ees */.  int nRo
18ea3 6f 74 2c 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  ot,    /* Number
18ea4 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
18ea5 52 6f 6f 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  Root[] */.  int 
18ea6 6d 78 45 72 72 2c 20 20 20 20 2f 2a 20 53 74 6f  mxErr,    /* Sto
18ea7 70 20 72 65 70 6f 72 74 69 6e 67 20 65 72 72 6f  p reporting erro
18ea8 72 73 20 61 66 74 65 72 20 74 68 69 73 20 6d 61  rs after this ma
18ea9 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 45  ny */.  int *pnE
18eaa 72 72 20 20 20 20 2f 2a 20 57 72 69 74 65 20 6e  rr    /* Write n
18eab 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20  umber of errors 
18eac 73 65 65 6e 20 74 6f 20 74 68 69 73 20 76 61 72  seen to this var
18ead 69 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 50 67  iable */.){.  Pg
18eae 6e 6f 20 69 3b 0a 20 20 69 6e 74 20 6e 52 65 66  no i;.  int nRef
18eaf 3b 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20  ;.  IntegrityCk 
18eb0 73 43 68 65 63 6b 3b 0a 20 20 42 74 53 68 61 72  sCheck;.  BtShar
18eb1 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
18eb2 3b 0a 20 20 63 68 61 72 20 7a 45 72 72 5b 31 30  ;.  char zErr[10
18eb3 30 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74  0];..  sqlite3Bt
18eb4 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 61  reeEnter(p);.  a
18eb5 73 73 65 72 74 28 20 70 2d 3e 69 6e 54 72 61 6e  ssert( p->inTran
18eb6 73 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 26 26 20  s>TRANS_NONE && 
18eb7 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  pBt->inTransacti
18eb8 6f 6e 3e 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 3b  on>TRANS_NONE );
18eb9 0a 20 20 6e 52 65 66 20 3d 20 73 71 6c 69 74 65  .  nRef = sqlite
18eba 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70  3PagerRefcount(p
18ebb 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 73  Bt->pPager);.  s
18ebc 43 68 65 63 6b 2e 70 42 74 20 3d 20 70 42 74 3b  Check.pBt = pBt;
18ebd 0a 20 20 73 43 68 65 63 6b 2e 70 50 61 67 65 72  .  sCheck.pPager
18ebe 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 72 3b 0a   = pBt->pPager;.
18ebf 20 20 73 43 68 65 63 6b 2e 6e 50 61 67 65 20 3d    sCheck.nPage =
18ec0 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28   pagerPagecount(
18ec1 73 43 68 65 63 6b 2e 70 42 74 29 3b 0a 20 20 73  sCheck.pBt);.  s
18ec2 43 68 65 63 6b 2e 6d 78 45 72 72 20 3d 20 6d 78  Check.mxErr = mx
18ec3 45 72 72 3b 0a 20 20 73 43 68 65 63 6b 2e 6e 45  Err;.  sCheck.nE
18ec4 72 72 20 3d 20 30 3b 0a 20 20 73 43 68 65 63 6b  rr = 0;.  sCheck
18ec5 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  .mallocFailed = 
18ec6 30 3b 0a 20 20 2a 70 6e 45 72 72 20 3d 20 30 3b  0;.  *pnErr = 0;
18ec7 0a 20 20 69 66 28 20 73 43 68 65 63 6b 2e 6e 50  .  if( sCheck.nP
18ec8 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  age==0 ){.    sq
18ec9 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
18eca 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  p);.    return 0
18ecb 3b 0a 20 20 7d 0a 20 20 73 43 68 65 63 6b 2e 61  ;.  }.  sCheck.a
18ecc 6e 52 65 66 20 3d 20 73 71 6c 69 74 65 33 4d 61  nRef = sqlite3Ma
18ecd 6c 6c 6f 63 28 20 28 73 43 68 65 63 6b 2e 6e 50  lloc( (sCheck.nP
18ece 61 67 65 2b 31 29 2a 73 69 7a 65 6f 66 28 73 43  age+1)*sizeof(sC
18ecf 68 65 63 6b 2e 61 6e 52 65 66 5b 30 5d 29 20 29  heck.anRef[0]) )
18ed0 3b 0a 20 20 69 66 28 20 21 73 43 68 65 63 6b 2e  ;.  if( !sCheck.
18ed1 61 6e 52 65 66 20 29 7b 0a 20 20 20 20 2a 70 6e  anRef ){.    *pn
18ed2 45 72 72 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c  Err = 1;.    sql
18ed3 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
18ed4 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
18ed5 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
18ed6 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67 65 3b  i<=sCheck.nPage;
18ed7 20 69 2b 2b 29 7b 20 73 43 68 65 63 6b 2e 61 6e   i++){ sCheck.an
18ed8 52 65 66 5b 69 5d 20 3d 20 30 3b 20 7d 0a 20 20  Ref[i] = 0; }.  
18ed9 69 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  i = PENDING_BYTE
18eda 5f 50 41 47 45 28 70 42 74 29 3b 0a 20 20 69 66  _PAGE(pBt);.  if
18edb 28 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67  ( i<=sCheck.nPag
18edc 65 20 29 7b 0a 20 20 20 20 73 43 68 65 63 6b 2e  e ){.    sCheck.
18edd 61 6e 52 65 66 5b 69 5d 20 3d 20 31 3b 0a 20 20  anRef[i] = 1;.  
18ede 7d 0a 20 20 73 71 6c 69 74 65 33 53 74 72 41 63  }.  sqlite3StrAc
18edf 63 75 6d 49 6e 69 74 28 26 73 43 68 65 63 6b 2e  cumInit(&sCheck.
18ee0 65 72 72 4d 73 67 2c 20 7a 45 72 72 2c 20 73 69  errMsg, zErr, si
18ee1 7a 65 6f 66 28 7a 45 72 72 29 2c 20 32 30 30 30  zeof(zErr), 2000
18ee2 30 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20  0);..  /* Check 
18ee3 74 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66  the integrity of
18ee4 20 74 68 65 20 66 72 65 65 6c 69 73 74 0a 20 20   the freelist.  
18ee5 2a 2f 0a 20 20 63 68 65 63 6b 4c 69 73 74 28 26  */.  checkList(&
18ee6 73 43 68 65 63 6b 2c 20 31 2c 20 67 65 74 34 62  sCheck, 1, get4b
18ee7 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31  yte(&pBt->pPage1
18ee8 2d 3e 61 44 61 74 61 5b 33 32 5d 29 2c 0a 20 20  ->aData[32]),.  
18ee9 20 20 20 20 20 20 20 20 20 20 67 65 74 34 62 79            get4by
18eea 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65 31 2d  te(&pBt->pPage1-
18eeb 3e 61 44 61 74 61 5b 33 36 5d 29 2c 20 22 4d 61  >aData[36]), "Ma
18eec 69 6e 20 66 72 65 65 6c 69 73 74 3a 20 22 29 3b  in freelist: ");
18eed 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 61 6c 6c  ..  /* Check all
18eee 20 74 68 65 20 74 61 62 6c 65 73 2e 0a 20 20 2a   the tables..  *
18eef 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 28 69 6e  /.  for(i=0; (in
18ef0 74 29 69 3c 6e 52 6f 6f 74 20 26 26 20 73 43 68  t)i<nRoot && sCh
18ef1 65 63 6b 2e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b  eck.mxErr; i++){
18ef2 0a 20 20 20 20 69 66 28 20 61 52 6f 6f 74 5b 69  .    if( aRoot[i
18ef3 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  ]==0 ) continue;
18ef4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
18ef5 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
18ef6 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74      if( pBt->aut
18ef7 6f 56 61 63 75 75 6d 20 26 26 20 61 52 6f 6f 74  oVacuum && aRoot
18ef8 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20 63  [i]>1 ){.      c
18ef9 68 65 63 6b 50 74 72 6d 61 70 28 26 73 43 68 65  heckPtrmap(&sChe
18efa 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 50 54  ck, aRoot[i], PT
18efb 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c 20 30  RMAP_ROOTPAGE, 0
18efc 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  , 0);.    }.#end
18efd 69 66 0a 20 20 20 20 63 68 65 63 6b 54 72 65 65  if.    checkTree
18efe 50 61 67 65 28 26 73 43 68 65 63 6b 2c 20 61 52  Page(&sCheck, aR
18eff 6f 6f 74 5b 69 5d 2c 20 22 4c 69 73 74 20 6f 66  oot[i], "List of
18f00 20 74 72 65 65 20 72 6f 6f 74 73 3a 20 22 29 3b   tree roots: ");
18f01 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  .  }..  /* Make 
18f02 73 75 72 65 20 65 76 65 72 79 20 70 61 67 65 20  sure every page 
18f03 69 6e 20 74 68 65 20 66 69 6c 65 20 69 73 20 72  in the file is r
18f04 65 66 65 72 65 6e 63 65 64 0a 20 20 2a 2f 0a 20  eferenced.  */. 
18f05 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 73 43 68   for(i=1; i<=sCh
18f06 65 63 6b 2e 6e 50 61 67 65 20 26 26 20 73 43 68  eck.nPage && sCh
18f07 65 63 6b 2e 6d 78 45 72 72 3b 20 69 2b 2b 29 7b  eck.mxErr; i++){
18f08 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
18f09 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
18f0a 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e 61 6e     if( sCheck.an
18f0b 52 65 66 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20  Ref[i]==0 ){.   
18f0c 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
18f0d 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20 22 50  g(&sCheck, 0, "P
18f0e 61 67 65 20 25 64 20 69 73 20 6e 65 76 65 72 20  age %d is never 
18f0f 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 20 20 7d  used", i);.    }
18f10 0a 23 65 6c 73 65 0a 20 20 20 20 2f 2a 20 49 66  .#else.    /* If
18f11 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 75   the database su
18f12 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75  pports auto-vacu
18f13 75 6d 2c 20 6d 61 6b 65 20 73 75 72 65 20 6e 6f  um, make sure no
18f14 20 74 61 62 6c 65 73 20 63 6f 6e 74 61 69 6e 0a   tables contain.
18f15 20 20 20 20 2a 2a 20 72 65 66 65 72 65 6e 63 65      ** reference
18f16 73 20 74 6f 20 70 6f 69 6e 74 65 72 2d 6d 61 70  s to pointer-map
18f17 20 70 61 67 65 73 2e 0a 20 20 20 20 2a 2f 0a 20   pages..    */. 
18f18 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e 61 6e     if( sCheck.an
18f19 52 65 66 5b 69 5d 3d 3d 30 20 26 26 20 0a 20 20  Ref[i]==0 && .  
18f1a 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50 41 47       (PTRMAP_PAG
18f1b 45 4e 4f 28 70 42 74 2c 20 69 29 21 3d 69 20 7c  ENO(pBt, i)!=i |
18f1c 7c 20 21 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  | !pBt->autoVacu
18f1d 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 63 68 65  um) ){.      che
18f1e 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68  ckAppendMsg(&sCh
18f1f 65 63 6b 2c 20 30 2c 20 22 50 61 67 65 20 25 64  eck, 0, "Page %d
18f20 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 22 2c   is never used",
18f21 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69   i);.    }.    i
18f22 66 28 20 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b  f( sCheck.anRef[
18f23 69 5d 21 3d 30 20 26 26 20 0a 20 20 20 20 20 20  i]!=0 && .      
18f24 20 28 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28   (PTRMAP_PAGENO(
18f25 70 42 74 2c 20 69 29 3d 3d 69 20 26 26 20 70 42  pBt, i)==i && pB
18f26 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 20 29  t->autoVacuum) )
18f27 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70 70  {.      checkApp
18f28 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b 2c 20  endMsg(&sCheck, 
18f29 30 2c 20 22 50 6f 69 6e 74 65 72 20 6d 61 70 20  0, "Pointer map 
18f2a 70 61 67 65 20 25 64 20 69 73 20 72 65 66 65 72  page %d is refer
18f2b 65 6e 63 65 64 22 2c 20 69 29 3b 0a 20 20 20 20  enced", i);.    
18f2c 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20  }.#endif.  }..  
18f2d 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69  /* Make sure thi
18f2e 73 20 61 6e 61 6c 79 73 69 73 20 64 69 64 20 6e  s analysis did n
18f2f 6f 74 20 6c 65 61 76 65 20 61 6e 79 20 75 6e 72  ot leave any unr
18f30 65 66 28 29 20 70 61 67 65 73 2e 0a 20 20 2a 2a  ef() pages..  **
18f31 20 54 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65   This is an inte
18f32 72 6e 61 6c 20 63 6f 6e 73 69 73 74 65 6e 63 79  rnal consistency
18f33 20 63 68 65 63 6b 3b 20 61 6e 20 69 6e 74 65 67   check; an integ
18f34 72 69 74 79 20 63 68 65 63 6b 0a 20 20 2a 2a 20  rity check.  ** 
18f35 6f 66 20 74 68 65 20 69 6e 74 65 67 72 69 74 79  of the integrity
18f36 20 63 68 65 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69   check..  */.  i
18f37 66 28 20 4e 45 56 45 52 28 6e 52 65 66 20 21 3d  f( NEVER(nRef !=
18f38 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66   sqlite3PagerRef
18f39 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65  count(pBt->pPage
18f3a 72 29 29 20 29 7b 0a 20 20 20 20 63 68 65 63 6b  r)) ){.    check
18f3b 41 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63  AppendMsg(&sChec
18f3c 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20 22 4f 75  k, 0, .      "Ou
18f3d 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 63  tstanding page c
18f3e 6f 75 6e 74 20 67 6f 65 73 20 66 72 6f 6d 20 25  ount goes from %
18f3f 64 20 74 6f 20 25 64 20 64 75 72 69 6e 67 20 74  d to %d during t
18f40 68 69 73 20 61 6e 61 6c 79 73 69 73 22 2c 0a 20  his analysis",. 
18f41 20 20 20 20 20 6e 52 65 66 2c 20 73 71 6c 69 74       nRef, sqlit
18f42 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28  e3PagerRefcount(
18f43 70 42 74 2d 3e 70 50 61 67 65 72 29 0a 20 20 20  pBt->pPager).   
18f44 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c   );.  }..  /* Cl
18f45 65 61 6e 20 20 75 70 20 61 6e 64 20 72 65 70 6f  ean  up and repo
18f46 72 74 20 65 72 72 6f 72 73 2e 0a 20 20 2a 2f 0a  rt errors..  */.
18f47 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
18f48 61 76 65 28 70 29 3b 0a 20 20 73 71 6c 69 74 65  ave(p);.  sqlite
18f49 33 5f 66 72 65 65 28 73 43 68 65 63 6b 2e 61 6e  3_free(sCheck.an
18f4a 52 65 66 29 3b 0a 20 20 69 66 28 20 73 43 68 65  Ref);.  if( sChe
18f4b 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  ck.mallocFailed 
18f4c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74  ){.    sqlite3St
18f4d 72 41 63 63 75 6d 52 65 73 65 74 28 26 73 43 68  rAccumReset(&sCh
18f4e 65 63 6b 2e 65 72 72 4d 73 67 29 3b 0a 20 20 20  eck.errMsg);.   
18f4f 20 2a 70 6e 45 72 72 20 3d 20 73 43 68 65 63 6b   *pnErr = sCheck
18f50 2e 6e 45 72 72 2b 31 3b 0a 20 20 20 20 72 65 74  .nErr+1;.    ret
18f51 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 2a 70 6e  urn 0;.  }.  *pn
18f52 45 72 72 20 3d 20 73 43 68 65 63 6b 2e 6e 45 72  Err = sCheck.nEr
18f53 72 3b 0a 20 20 69 66 28 20 73 43 68 65 63 6b 2e  r;.  if( sCheck.
18f54 6e 45 72 72 3d 3d 30 20 29 20 73 71 6c 69 74 65  nErr==0 ) sqlite
18f55 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28 26  3StrAccumReset(&
18f56 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29 3b 0a  sCheck.errMsg);.
18f57 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
18f58 53 74 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26  StrAccumFinish(&
18f59 73 43 68 65 63 6b 2e 65 72 72 4d 73 67 29 3b 0a  sCheck.errMsg);.
18f5a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
18f5b 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54  TE_OMIT_INTEGRIT
18f5c 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 2f 2a 0a 2a  Y_CHECK */../*.*
18f5d 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 75 6c  * Return the ful
18f5e 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74 68  l pathname of th
18f5f 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74  e underlying dat
18f60 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  abase file..**.*
18f61 2a 20 54 68 65 20 70 61 67 65 72 20 66 69 6c 65  * The pager file
18f62 6e 61 6d 65 20 69 73 20 69 6e 76 61 72 69 61 6e  name is invarian
18f63 74 20 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65  t as long as the
18f64 20 70 61 67 65 72 20 69 73 0a 2a 2a 20 6f 70 65   pager is.** ope
18f65 6e 20 73 6f 20 69 74 20 69 73 20 73 61 66 65 20  n so it is safe 
18f66 74 6f 20 61 63 63 65 73 73 20 77 69 74 68 6f 75  to access withou
18f67 74 20 74 68 65 20 42 74 53 68 61 72 65 64 20 6d  t the BtShared m
18f68 75 74 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  utex..*/.SQLITE_
18f69 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
18f6a 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  ar *sqlite3Btree
18f6b 47 65 74 46 69 6c 65 6e 61 6d 65 28 42 74 72 65  GetFilename(Btre
18f6c 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  e *p){.  assert(
18f6d 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72 21   p->pBt->pPager!
18f6e 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 73  =0 );.  return s
18f6f 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e  qlite3PagerFilen
18f70 61 6d 65 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67  ame(p->pBt->pPag
18f71 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  er);.}../*.** Re
18f72 74 75 72 6e 20 74 68 65 20 70 61 74 68 6e 61 6d  turn the pathnam
18f73 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e of the journal
18f74 20 66 69 6c 65 20 66 6f 72 20 74 68 69 73 20 64   file for this d
18f75 61 74 61 62 61 73 65 2e 20 54 68 65 20 72 65 74  atabase. The ret
18f76 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20  urn.** value of 
18f77 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
18f78 74 68 65 20 73 61 6d 65 20 72 65 67 61 72 64 6c  the same regardl
18f79 65 73 73 20 6f 66 20 77 68 65 74 68 65 72 20 74  ess of whether t
18f7a 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a  he journal file.
18f7b 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 72 65 61  ** has been crea
18f7c 74 65 64 20 6f 72 20 6e 6f 74 2e 0a 2a 2a 0a 2a  ted or not..**.*
18f7d 2a 20 54 68 65 20 70 61 67 65 72 20 6a 6f 75 72  * The pager jour
18f7e 6e 61 6c 20 66 69 6c 65 6e 61 6d 65 20 69 73 20  nal filename is 
18f7f 69 6e 76 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e  invariant as lon
18f80 67 20 61 73 20 74 68 65 20 70 61 67 65 72 20 69  g as the pager i
18f81 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20  s.** open so it 
18f82 69 73 20 73 61 66 65 20 74 6f 20 61 63 63 65 73  is safe to acces
18f83 73 20 77 69 74 68 6f 75 74 20 74 68 65 20 42 74  s without the Bt
18f84 53 68 61 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f  Shared mutex..*/
18f85 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18f86 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
18f87 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 72 6e  te3BtreeGetJourn
18f88 61 6c 6e 61 6d 65 28 42 74 72 65 65 20 2a 70 29  alname(Btree *p)
18f89 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70  {.  assert( p->p
18f8a 42 74 2d 3e 70 50 61 67 65 72 21 3d 30 20 29 3b  Bt->pPager!=0 );
18f8b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
18f8c 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61 6d  3PagerJournalnam
18f8d 65 28 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72  e(p->pBt->pPager
18f8e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
18f8f 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61  rn non-zero if a
18f90 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
18f91 61 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54  active..*/.SQLIT
18f92 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
18f93 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72  lite3BtreeIsInTr
18f94 61 6e 73 28 42 74 72 65 65 20 2a 70 29 7b 0a 20  ans(Btree *p){. 
18f95 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c   assert( p==0 ||
18f96 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
18f97 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78  eld(p->db->mutex
18f98 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 70  ) );.  return (p
18f99 20 26 26 20 28 70 2d 3e 69 6e 54 72 61 6e 73 3d   && (p->inTrans=
18f9a 3d 54 52 41 4e 53 5f 57 52 49 54 45 29 29 3b 0a  =TRANS_WRITE));.
18f9b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
18f9c 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 72 65  non-zero if a re
18f9d 61 64 20 28 6f 72 20 77 72 69 74 65 29 20 74 72  ad (or write) tr
18f9e 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
18f9f 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ive..*/.SQLITE_P
18fa0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
18fa1 65 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 54  e3BtreeIsInReadT
18fa2 72 61 6e 73 28 42 74 72 65 65 20 2a 70 29 7b 0a  rans(Btree *p){.
18fa3 20 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20    assert( p );. 
18fa4 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
18fa5 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64  _mutex_held(p->d
18fa6 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72  b->mutex) );.  r
18fa7 65 74 75 72 6e 20 70 2d 3e 69 6e 54 72 61 6e 73  eturn p->inTrans
18fa8 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 7d 0a  !=TRANS_NONE;.}.
18fa9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18faa 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
18fab 49 73 49 6e 42 61 63 6b 75 70 28 42 74 72 65 65  IsInBackup(Btree
18fac 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20   *p){.  assert( 
18fad 70 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  p );.  assert( s
18fae 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
18faf 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  d(p->db->mutex) 
18fb0 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 6e  );.  return p->n
18fb1 42 61 63 6b 75 70 21 3d 30 3b 0a 7d 0a 0a 2f 2a  Backup!=0;.}../*
18fb2 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
18fb3 6e 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  n returns a poin
18fb4 74 65 72 20 74 6f 20 61 20 62 6c 6f 62 20 6f 66  ter to a blob of
18fb5 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74   memory associat
18fb6 65 64 20 77 69 74 68 0a 2a 2a 20 61 20 73 69 6e  ed with.** a sin
18fb7 67 6c 65 20 73 68 61 72 65 64 2d 62 74 72 65 65  gle shared-btree
18fb8 2e 20 54 68 65 20 6d 65 6d 6f 72 79 20 69 73 20  . The memory is 
18fb9 75 73 65 64 20 62 79 20 63 6c 69 65 6e 74 20 63  used by client c
18fba 6f 64 65 20 66 6f 72 20 69 74 73 20 6f 77 6e 0a  ode for its own.
18fbb 2a 2a 20 70 75 72 70 6f 73 65 73 20 28 66 6f 72  ** purposes (for
18fbc 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 73 74 6f   example, to sto
18fbd 72 65 20 61 20 68 69 67 68 2d 6c 65 76 65 6c 20  re a high-level 
18fbe 73 63 68 65 6d 61 20 61 73 73 6f 63 69 61 74 65  schema associate
18fbf 64 20 77 69 74 68 20 0a 2a 2a 20 74 68 65 20 73  d with .** the s
18fc0 68 61 72 65 64 2d 62 74 72 65 65 29 2e 20 54 68  hared-btree). Th
18fc1 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d 61  e btree layer ma
18fc2 6e 61 67 65 73 20 72 65 66 65 72 65 6e 63 65 20  nages reference 
18fc3 63 6f 75 6e 74 69 6e 67 20 69 73 73 75 65 73 2e  counting issues.
18fc4 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
18fc5 20 74 69 6d 65 20 74 68 69 73 20 69 73 20 63 61   time this is ca
18fc6 6c 6c 65 64 20 6f 6e 20 61 20 73 68 61 72 65 64  lled on a shared
18fc7 2d 62 74 72 65 65 2c 20 6e 42 79 74 65 73 20 62  -btree, nBytes b
18fc8 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a  ytes of memory.*
18fc9 2a 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64 2c  * are allocated,
18fca 20 7a 65 72 6f 65 64 2c 20 61 6e 64 20 72 65 74   zeroed, and ret
18fcb 75 72 6e 65 64 20 74 6f 20 74 68 65 20 63 61 6c  urned to the cal
18fcc 6c 65 72 2e 20 46 6f 72 20 65 61 63 68 20 73 75  ler. For each su
18fcd 62 73 65 71 75 65 6e 74 20 0a 2a 2a 20 63 61 6c  bsequent .** cal
18fce 6c 20 74 68 65 20 6e 42 79 74 65 73 20 70 61 72  l the nBytes par
18fcf 61 6d 65 74 65 72 20 69 73 20 69 67 6e 6f 72 65  ameter is ignore
18fd0 64 20 61 6e 64 20 61 20 70 6f 69 6e 74 65 72 20  d and a pointer 
18fd1 74 6f 20 74 68 65 20 73 61 6d 65 20 62 6c 6f 62  to the same blob
18fd2 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65  .** of memory re
18fd3 74 75 72 6e 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 49  turned. .**.** I
18fd4 66 20 74 68 65 20 6e 42 79 74 65 73 20 70 61 72  f the nBytes par
18fd5 61 6d 65 74 65 72 20 69 73 20 30 20 61 6e 64 20  ameter is 0 and 
18fd6 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f  the blob of memo
18fd7 72 79 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62  ry has not yet b
18fd8 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64  een.** allocated
18fd9 2c 20 61 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72  , a null pointer
18fda 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49 66   is returned. If
18fdb 20 74 68 65 20 62 6c 6f 62 20 68 61 73 20 61 6c   the blob has al
18fdc 72 65 61 64 79 20 62 65 65 6e 0a 2a 2a 20 61 6c  ready been.** al
18fdd 6c 6f 63 61 74 65 64 2c 20 69 74 20 69 73 20 72  located, it is r
18fde 65 74 75 72 6e 65 64 20 61 73 20 6e 6f 72 6d 61  eturned as norma
18fdf 6c 2e 0a 2a 2a 0a 2a 2a 20 4a 75 73 74 20 62 65  l..**.** Just be
18fe0 66 6f 72 65 20 74 68 65 20 73 68 61 72 65 64 2d  fore the shared-
18fe1 62 74 72 65 65 20 69 73 20 63 6c 6f 73 65 64 2c  btree is closed,
18fe2 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 61   the function pa
18fe3 73 73 65 64 20 61 73 20 74 68 65 20 0a 2a 2a 20  ssed as the .** 
18fe4 78 46 72 65 65 20 61 72 67 75 6d 65 6e 74 20 77  xFree argument w
18fe5 68 65 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  hen the memory a
18fe6 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 20 6d 61  llocation was ma
18fe7 64 65 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e  de is invoked on
18fe8 20 74 68 65 20 0a 2a 2a 20 62 6c 6f 62 20 6f 66   the .** blob of
18fe9 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72   allocated memor
18fea 79 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  y. This function
18feb 20 73 68 6f 75 6c 64 20 6e 6f 74 20 63 61 6c 6c   should not call
18fec 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 0a   sqlite3_free().
18fed 2a 2a 20 6f 6e 20 74 68 65 20 6d 65 6d 6f 72 79  ** on the memory
18fee 2c 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65  , the btree laye
18fef 72 20 64 6f 65 73 20 74 68 61 74 2e 0a 2a 2f 0a  r does that..*/.
18ff0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
18ff1 6f 69 64 20 2a 73 71 6c 69 74 65 33 42 74 72 65  oid *sqlite3Btre
18ff2 65 53 63 68 65 6d 61 28 42 74 72 65 65 20 2a 70  eSchema(Btree *p
18ff3 2c 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 76 6f  , int nBytes, vo
18ff4 69 64 28 2a 78 46 72 65 65 29 28 76 6f 69 64 20  id(*xFree)(void 
18ff5 2a 29 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20  *)){.  BtShared 
18ff6 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20  *pBt = p->pBt;. 
18ff7 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
18ff8 65 72 28 70 29 3b 0a 20 20 69 66 28 20 21 70 42  er(p);.  if( !pB
18ff9 74 2d 3e 70 53 63 68 65 6d 61 20 26 26 20 6e 42  t->pSchema && nB
18ffa 79 74 65 73 20 29 7b 0a 20 20 20 20 70 42 74 2d  ytes ){.    pBt-
18ffb 3e 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74  >pSchema = sqlit
18ffc 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 79  e3MallocZero(nBy
18ffd 74 65 73 29 3b 0a 20 20 20 20 70 42 74 2d 3e 78  tes);.    pBt->x
18ffe 46 72 65 65 53 63 68 65 6d 61 20 3d 20 78 46 72  FreeSchema = xFr
18fff 65 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ee;.  }.  sqlite
19000 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
19001 20 20 72 65 74 75 72 6e 20 70 42 74 2d 3e 70 53    return pBt->pS
19002 63 68 65 6d 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  chema;.}../*.** 
19003 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f  Return SQLITE_LO
19004 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48 45  CKED_SHAREDCACHE
19005 20 69 66 20 61 6e 6f 74 68 65 72 20 75 73 65 72   if another user
19006 20 6f 66 20 74 68 65 20 73 61 6d 65 20 73 68 61   of the same sha
19007 72 65 64 20 0a 2a 2a 20 62 74 72 65 65 20 61 73  red .** btree as
19008 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 68 61   the argument ha
19009 6e 64 6c 65 20 68 6f 6c 64 73 20 61 6e 20 65 78  ndle holds an ex
1900a 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20  clusive lock on 
1900b 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 5f 6d  the .** sqlite_m
1900c 61 73 74 65 72 20 74 61 62 6c 65 2e 20 4f 74 68  aster table. Oth
1900d 65 72 77 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b  erwise SQLITE_OK
1900e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1900f 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
19010 74 72 65 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64  treeSchemaLocked
19011 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e  (Btree *p){.  in
19012 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20  t rc;.  assert( 
19013 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
19014 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ld(p->db->mutex)
19015 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72   );.  sqlite3Btr
19016 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63  eeEnter(p);.  rc
19017 20 3d 20 71 75 65 72 79 53 68 61 72 65 64 43 61   = querySharedCa
19018 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20  cheTableLock(p, 
19019 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 52 45 41  MASTER_ROOT, REA
1901a 44 5f 4c 4f 43 4b 29 3b 0a 20 20 61 73 73 65 72  D_LOCK);.  asser
1901b 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
1901c 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4c   || rc==SQLITE_L
1901d 4f 43 4b 45 44 5f 53 48 41 52 45 44 43 41 43 48  OCKED_SHAREDCACH
1901e 45 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  E );.  sqlite3Bt
1901f 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
19020 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 69  eturn rc;.}...#i
19021 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
19022 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a 2f  T_SHARED_CACHE./
19023 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 61 20 6c 6f  *.** Obtain a lo
19024 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20  ck on the table 
19025 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 20  whose root page 
19026 69 73 20 69 54 61 62 2e 20 20 54 68 65 0a 2a 2a  is iTab.  The.**
19027 20 6c 6f 63 6b 20 69 73 20 61 20 77 72 69 74 65   lock is a write
19028 20 6c 6f 63 6b 20 69 66 20 69 73 57 72 69 74 65   lock if isWrite
19029 6c 6f 63 6b 20 69 73 20 74 72 75 65 20 6f 72 20  lock is true or 
1902a 61 20 72 65 61 64 20 6c 6f 63 6b 0a 2a 2a 20 69  a read lock.** i
1902b 66 20 69 74 20 69 73 20 66 61 6c 73 65 2e 0a 2a  f it is false..*
1902c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1902d 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
1902e 65 4c 6f 63 6b 54 61 62 6c 65 28 42 74 72 65 65  eLockTable(Btree
1902f 20 2a 70 2c 20 69 6e 74 20 69 54 61 62 2c 20 75   *p, int iTab, u
19030 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 7b 0a  8 isWriteLock){.
19031 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
19032 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20  E_OK;.  assert( 
19033 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e  p->inTrans!=TRAN
19034 53 5f 4e 4f 4e 45 20 29 3b 0a 20 20 69 66 28 20  S_NONE );.  if( 
19035 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20  p->sharable ){. 
19036 20 20 20 75 38 20 6c 6f 63 6b 54 79 70 65 20 3d     u8 lockType =
19037 20 52 45 41 44 5f 4c 4f 43 4b 20 2b 20 69 73 57   READ_LOCK + isW
19038 72 69 74 65 4c 6f 63 6b 3b 0a 20 20 20 20 61 73  riteLock;.    as
19039 73 65 72 74 28 20 52 45 41 44 5f 4c 4f 43 4b 2b  sert( READ_LOCK+
1903a 31 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b  1==WRITE_LOCK );
1903b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73 57  .    assert( isW
1903c 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 69  riteLock==0 || i
1903d 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 31 20 29 3b  sWriteLock==1 );
1903e 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ..    sqlite3Btr
1903f 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20  eeEnter(p);.    
19040 72 63 20 3d 20 71 75 65 72 79 53 68 61 72 65 64  rc = queryShared
19041 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70  CacheTableLock(p
19042 2c 20 69 54 61 62 2c 20 6c 6f 63 6b 54 79 70 65  , iTab, lockType
19043 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
19044 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
19045 20 20 72 63 20 3d 20 73 65 74 53 68 61 72 65 64    rc = setShared
19046 43 61 63 68 65 54 61 62 6c 65 4c 6f 63 6b 28 70  CacheTableLock(p
19047 2c 20 69 54 61 62 2c 20 6c 6f 63 6b 54 79 70 65  , iTab, lockType
19048 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
19049 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70  ite3BtreeLeave(p
1904a 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1904b 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  rc;.}.#endif..#i
1904c 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1904d 54 5f 49 4e 43 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a  T_INCRBLOB./*.**
1904e 20 41 72 67 75 6d 65 6e 74 20 70 43 73 72 20 6d   Argument pCsr m
1904f 75 73 74 20 62 65 20 61 20 63 75 72 73 6f 72 20  ust be a cursor 
19050 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69  opened for writi
19051 6e 67 20 6f 6e 20 61 6e 20 0a 2a 2a 20 49 4e 54  ng on an .** INT
19052 4b 45 59 20 74 61 62 6c 65 20 63 75 72 72 65 6e  KEY table curren
19053 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  tly pointing at 
19054 61 20 76 61 6c 69 64 20 74 61 62 6c 65 20 65 6e  a valid table en
19055 74 72 79 2e 20 0a 2a 2a 20 54 68 69 73 20 66 75  try. .** This fu
19056 6e 63 74 69 6f 6e 20 6d 6f 64 69 66 69 65 73 20  nction modifies 
19057 74 68 65 20 64 61 74 61 20 73 74 6f 72 65 64 20  the data stored 
19058 61 73 20 70 61 72 74 20 6f 66 20 74 68 61 74 20  as part of that 
19059 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c  entry..**.** Onl
1905a 79 20 74 68 65 20 64 61 74 61 20 63 6f 6e 74 65  y the data conte
1905b 6e 74 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 6d  nt may only be m
1905c 6f 64 69 66 69 65 64 2c 20 69 74 20 69 73 20 6e  odified, it is n
1905d 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 0a  ot possible to .
1905e 2a 2a 20 63 68 61 6e 67 65 20 74 68 65 20 6c 65  ** change the le
1905f 6e 67 74 68 20 6f 66 20 74 68 65 20 64 61 74 61  ngth of the data
19060 20 73 74 6f 72 65 64 2e 20 49 66 20 74 68 69 73   stored. If this
19061 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
19062 6c 65 64 20 77 69 74 68 0a 2a 2a 20 70 61 72 61  led with.** para
19063 6d 65 74 65 72 73 20 74 68 61 74 20 61 74 74 65  meters that atte
19064 6d 70 74 20 74 6f 20 77 72 69 74 65 20 70 61 73  mpt to write pas
19065 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
19066 20 65 78 69 73 74 69 6e 67 20 64 61 74 61 2c 0a   existing data,.
19067 2a 2a 20 6e 6f 20 6d 6f 64 69 66 69 63 61 74 69  ** no modificati
19068 6f 6e 73 20 61 72 65 20 6d 61 64 65 20 61 6e 64  ons are made and
19069 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 20   SQLITE_CORRUPT 
1906a 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
1906b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1906c 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  nt sqlite3BtreeP
1906d 75 74 44 61 74 61 28 42 74 43 75 72 73 6f 72 20  utData(BtCursor 
1906e 2a 70 43 73 72 2c 20 75 33 32 20 6f 66 66 73 65  *pCsr, u32 offse
1906f 74 2c 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64  t, u32 amt, void
19070 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a   *z){.  int rc;.
19071 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
19072 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 73 72 29  HoldsMutex(pCsr)
19073 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
19074 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
19075 28 70 43 73 72 2d 3e 70 42 74 72 65 65 2d 3e 64  (pCsr->pBtree->d
19076 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  b->mutex) );.  a
19077 73 73 65 72 74 28 20 70 43 73 72 2d 3e 69 73 49  ssert( pCsr->isI
19078 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20 29 3b  ncrblobHandle );
19079 0a 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65  ..  rc = restore
1907a 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70  CursorPosition(p
1907b 43 73 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  Csr);.  if( rc!=
1907c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1907d 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
1907e 20 20 61 73 73 65 72 74 28 20 70 43 73 72 2d 3e    assert( pCsr->
1907f 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 52  eState!=CURSOR_R
19080 45 51 55 49 52 45 53 45 45 4b 20 29 3b 0a 20 20  EQUIRESEEK );.  
19081 69 66 28 20 70 43 73 72 2d 3e 65 53 74 61 74 65  if( pCsr->eState
19082 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  !=CURSOR_VALID )
19083 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
19084 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 0a  ITE_ABORT;.  }..
19085 20 20 2f 2a 20 43 68 65 63 6b 20 73 6f 6d 65 20    /* Check some 
19086 61 73 73 75 6d 70 74 69 6f 6e 73 3a 20 0a 20 20  assumptions: .  
19087 2a 2a 20 20 20 28 61 29 20 74 68 65 20 63 75 72  **   (a) the cur
19088 73 6f 72 20 69 73 20 6f 70 65 6e 20 66 6f 72 20  sor is open for 
19089 77 72 69 74 69 6e 67 2c 0a 20 20 2a 2a 20 20 20  writing,.  **   
1908a 28 62 29 20 74 68 65 72 65 20 69 73 20 61 20 72  (b) there is a r
1908b 65 61 64 2f 77 72 69 74 65 20 74 72 61 6e 73 61  ead/write transa
1908c 63 74 69 6f 6e 20 6f 70 65 6e 2c 0a 20 20 2a 2a  ction open,.  **
1908d 20 20 20 28 63 29 20 74 68 65 20 63 6f 6e 6e 65     (c) the conne
1908e 63 74 69 6f 6e 20 68 6f 6c 64 73 20 61 20 77 72  ction holds a wr
1908f 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ite-lock on the 
19090 74 61 62 6c 65 20 28 69 66 20 72 65 71 75 69 72  table (if requir
19091 65 64 29 2c 0a 20 20 2a 2a 20 20 20 28 64 29 20  ed),.  **   (d) 
19092 74 68 65 72 65 20 61 72 65 20 6e 6f 20 63 6f 6e  there are no con
19093 66 6c 69 63 74 69 6e 67 20 72 65 61 64 2d 6c 6f  flicting read-lo
19094 63 6b 73 2c 20 61 6e 64 0a 20 20 2a 2a 20 20 20  cks, and.  **   
19095 28 65 29 20 74 68 65 20 63 75 72 73 6f 72 20 70  (e) the cursor p
19096 6f 69 6e 74 73 20 61 74 20 61 20 76 61 6c 69 64  oints at a valid
19097 20 72 6f 77 20 6f 66 20 61 6e 20 69 6e 74 4b 65   row of an intKe
19098 79 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  y table..  */.  
19099 69 66 28 20 21 70 43 73 72 2d 3e 77 72 46 6c 61  if( !pCsr->wrFla
1909a 67 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  g ){.    return 
1909b 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b  SQLITE_READONLY;
1909c 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21  .  }.  assert( !
1909d 70 43 73 72 2d 3e 70 42 74 2d 3e 72 65 61 64 4f  pCsr->pBt->readO
1909e 6e 6c 79 20 26 26 20 70 43 73 72 2d 3e 70 42 74  nly && pCsr->pBt
1909f 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d  ->inTransaction=
190a0 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a  =TRANS_WRITE );.
190a1 20 20 61 73 73 65 72 74 28 20 68 61 73 53 68 61    assert( hasSha
190a2 72 65 64 43 61 63 68 65 54 61 62 6c 65 4c 6f 63  redCacheTableLoc
190a3 6b 28 70 43 73 72 2d 3e 70 42 74 72 65 65 2c 20  k(pCsr->pBtree, 
190a4 70 43 73 72 2d 3e 70 67 6e 6f 52 6f 6f 74 2c 20  pCsr->pgnoRoot, 
190a5 30 2c 20 32 29 20 29 3b 0a 20 20 61 73 73 65 72  0, 2) );.  asser
190a6 74 28 20 21 68 61 73 52 65 61 64 43 6f 6e 66 6c  t( !hasReadConfl
190a7 69 63 74 73 28 70 43 73 72 2d 3e 70 42 74 72 65  icts(pCsr->pBtre
190a8 65 2c 20 70 43 73 72 2d 3e 70 67 6e 6f 52 6f 6f  e, pCsr->pgnoRoo
190a9 74 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  t) );.  assert( 
190aa 70 43 73 72 2d 3e 61 70 50 61 67 65 5b 70 43 73  pCsr->apPage[pCs
190ab 72 2d 3e 69 50 61 67 65 5d 2d 3e 69 6e 74 4b 65  r->iPage]->intKe
190ac 79 20 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 61  y );..  return a
190ad 63 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43 73  ccessPayload(pCs
190ae 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20  r, offset, amt, 
190af 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
190b0 29 7a 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a  )z, 1);.}../* .*
190b1 2a 20 53 65 74 20 61 20 66 6c 61 67 20 6f 6e 20  * Set a flag on 
190b2 74 68 69 73 20 63 75 72 73 6f 72 20 74 6f 20 63  this cursor to c
190b3 61 63 68 65 20 74 68 65 20 6c 6f 63 61 74 69 6f  ache the locatio
190b4 6e 73 20 6f 66 20 70 61 67 65 73 20 66 72 6f 6d  ns of pages from
190b5 20 74 68 65 20 0a 2a 2a 20 6f 76 65 72 66 6c 6f   the .** overflo
190b6 77 20 6c 69 73 74 20 66 6f 72 20 74 68 65 20 63  w list for the c
190b7 75 72 72 65 6e 74 20 72 6f 77 2e 20 54 68 69 73  urrent row. This
190b8 20 69 73 20 75 73 65 64 20 62 79 20 63 75 72 73   is used by curs
190b9 6f 72 73 20 6f 70 65 6e 65 64 0a 2a 2a 20 66 6f  ors opened.** fo
190ba 72 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c  r incremental bl
190bb 6f 62 20 49 4f 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a  ob IO only..**.*
190bc 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
190bd 73 65 74 73 20 61 20 66 6c 61 67 20 6f 6e 6c 79  sets a flag only
190be 2e 20 54 68 65 20 61 63 74 75 61 6c 20 70 61 67  . The actual pag
190bf 65 20 6c 6f 63 61 74 69 6f 6e 20 63 61 63 68 65  e location cache
190c0 0a 2a 2a 20 28 73 74 6f 72 65 64 20 69 6e 20 42  .** (stored in B
190c1 74 43 75 72 73 6f 72 2e 61 4f 76 65 72 66 6c 6f  tCursor.aOverflo
190c2 77 5b 5d 29 20 69 73 20 61 6c 6c 6f 63 61 74 65  w[]) is allocate
190c3 64 20 61 6e 64 20 75 73 65 64 20 62 79 20 66 75  d and used by fu
190c4 6e 63 74 69 6f 6e 0a 2a 2a 20 61 63 63 65 73 73  nction.** access
190c5 50 61 79 6c 6f 61 64 28 29 20 28 74 68 65 20 77  Payload() (the w
190c6 6f 72 6b 65 72 20 66 75 6e 63 74 69 6f 6e 20 66  orker function f
190c7 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65 65 44  or sqlite3BtreeD
190c8 61 74 61 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c  ata() and.** sql
190c9 69 74 65 33 42 74 72 65 65 50 75 74 44 61 74 61  ite3BtreePutData
190ca 28 29 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ())..*/.SQLITE_P
190cb 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
190cc 74 65 33 42 74 72 65 65 43 61 63 68 65 4f 76 65  te3BtreeCacheOve
190cd 72 66 6c 6f 77 28 42 74 43 75 72 73 6f 72 20 2a  rflow(BtCursor *
190ce 70 43 75 72 29 7b 0a 20 20 61 73 73 65 72 74 28  pCur){.  assert(
190cf 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65   cursorHoldsMute
190d0 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73  x(pCur) );.  ass
190d1 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
190d2 65 78 5f 68 65 6c 64 28 70 43 75 72 2d 3e 70 42  ex_held(pCur->pB
190d3 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  tree->db->mutex)
190d4 20 29 3b 0a 20 20 61 73 73 65 72 74 28 21 70 43   );.  assert(!pC
190d5 75 72 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61  ur->isIncrblobHa
190d6 6e 64 6c 65 29 3b 0a 20 20 61 73 73 65 72 74 28  ndle);.  assert(
190d7 21 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77  !pCur->aOverflow
190d8 29 3b 0a 20 20 70 43 75 72 2d 3e 69 73 49 6e 63  );.  pCur->isInc
190d9 72 62 6c 6f 62 48 61 6e 64 6c 65 20 3d 20 31 3b  rblobHandle = 1;
190da 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a  .}.#endif../****
190db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
190dc 66 20 62 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a  f btree.c ******
190dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
190de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
190df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
190e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
190e1 20 66 69 6c 65 20 62 61 63 6b 75 70 2e 63 20 2a   file backup.c *
190e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
190e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
190e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
190e5 20 32 30 30 39 20 4a 61 6e 75 61 72 79 20 32 38   2009 January 28
190e6 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
190e7 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
190e8 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
190e9 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
190ea 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
190eb 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
190ec 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
190ed 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
190ee 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
190ef 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
190f0 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
190f1 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
190f2 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
190f3 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
190f4 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
190f5 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
190f6 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
190f7 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
190f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
190f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
190fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
190fb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
190fc 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
190fd 74 61 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d  tains the implem
190fe 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
190ff 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 58  sqlite3_backup_X
19100 58 58 28 29 20 0a 2a 2a 20 41 50 49 20 66 75 6e  XX() .** API fun
19101 63 74 69 6f 6e 73 20 61 6e 64 20 74 68 65 20 72  ctions and the r
19102 65 6c 61 74 65 64 20 66 65 61 74 75 72 65 73 2e  elated features.
19103 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 61 63 6b  .**.** $Id: back
19104 75 70 2e 63 2c 76 20 31 2e 31 39 20 32 30 30 39  up.c,v 1.19 2009
19105 2f 30 37 2f 30 36 20 31 39 3a 30 33 3a 31 33 20  /07/06 19:03:13 
19106 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  drh Exp $.*/../*
19107 20 4d 61 63 72 6f 20 74 6f 20 66 69 6e 64 20 74   Macro to find t
19108 68 65 20 6d 69 6e 69 6d 75 6d 20 6f 66 20 74 77  he minimum of tw
19109 6f 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 73  o numeric values
1910a 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4d 49 4e  ..*/.#ifndef MIN
1910b 0a 23 20 64 65 66 69 6e 65 20 4d 49 4e 28 78 2c  .# define MIN(x,
1910c 79 29 20 28 28 78 29 3c 28 79 29 3f 28 78 29 3a  y) ((x)<(y)?(x):
1910d 28 79 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  (y)).#endif../*.
1910e 2a 2a 20 53 74 72 75 63 74 75 72 65 20 61 6c 6c  ** Structure all
1910f 6f 63 61 74 65 64 20 66 6f 72 20 65 61 63 68 20  ocated for each 
19110 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f 6e  backup operation
19111 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  ..*/.struct sqli
19112 74 65 33 5f 62 61 63 6b 75 70 20 7b 0a 20 20 73  te3_backup {.  s
19113 71 6c 69 74 65 33 2a 20 70 44 65 73 74 44 62 3b  qlite3* pDestDb;
19114 20 20 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69          /* Desti
19115 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 20  nation database 
19116 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 42 74 72 65  handle */.  Btre
19117 65 20 2a 70 44 65 73 74 3b 20 20 20 20 20 20 20  e *pDest;       
19118 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74       /* Destinat
19119 69 6f 6e 20 62 2d 74 72 65 65 20 66 69 6c 65 20  ion b-tree file 
1911a 2a 2f 0a 20 20 75 33 32 20 69 44 65 73 74 53 63  */.  u32 iDestSc
1911b 68 65 6d 61 3b 20 20 20 20 20 20 20 20 20 2f 2a  hema;         /*
1911c 20 4f 72 69 67 69 6e 61 6c 20 73 63 68 65 6d 61   Original schema
1911d 20 63 6f 6f 6b 69 65 20 69 6e 20 64 65 73 74 69   cookie in desti
1911e 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  nation */.  int 
1911f 62 44 65 73 74 4c 6f 63 6b 65 64 3b 20 20 20 20  bDestLocked;    
19120 20 20 20 20 20 2f 2a 20 54 72 75 65 20 6f 6e 63       /* True onc
19121 65 20 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61  e a write-transa
19122 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 20 6f 6e  ction is open on
19123 20 70 44 65 73 74 20 2a 2f 0a 0a 20 20 50 67 6e   pDest */..  Pgn
19124 6f 20 69 4e 65 78 74 3b 20 20 20 20 20 20 20 20  o iNext;        
19125 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75        /* Page nu
19126 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65 78 74  mber of the next
19127 20 73 6f 75 72 63 65 20 70 61 67 65 20 74 6f 20   source page to 
19128 63 6f 70 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65  copy */.  sqlite
19129 33 2a 20 70 53 72 63 44 62 3b 20 20 20 20 20 20  3* pSrcDb;      
1912a 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 64 61 74     /* Source dat
1912b 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
1912c 20 20 42 74 72 65 65 20 2a 70 53 72 63 3b 20 20    Btree *pSrc;  
1912d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6f             /* So
1912e 75 72 63 65 20 62 2d 74 72 65 65 20 66 69 6c 65  urce b-tree file
1912f 20 2a 2f 0a 0a 20 20 69 6e 74 20 72 63 3b 20 20   */..  int rc;  
19130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19131 2f 2a 20 42 61 63 6b 75 70 20 70 72 6f 63 65 73  /* Backup proces
19132 73 20 65 72 72 6f 72 20 63 6f 64 65 20 2a 2f 0a  s error code */.
19133 0a 20 20 2f 2a 20 54 68 65 73 65 20 74 77 6f 20  .  /* These two 
19134 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 73 65  variables are se
19135 74 20 62 79 20 65 76 65 72 79 20 63 61 6c 6c 20  t by every call 
19136 74 6f 20 62 61 63 6b 75 70 5f 73 74 65 70 28 29  to backup_step()
19137 2e 20 54 68 65 79 20 61 72 65 0a 20 20 2a 2a 20  . They are.  ** 
19138 72 65 61 64 20 62 79 20 63 61 6c 6c 73 20 74 6f  read by calls to
19139 20 62 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e   backup_remainin
1913a 67 28 29 20 61 6e 64 20 62 61 63 6b 75 70 5f 70  g() and backup_p
1913b 61 67 65 63 6f 75 6e 74 28 29 2e 0a 20 20 2a 2f  agecount()..  */
1913c 0a 20 20 50 67 6e 6f 20 6e 52 65 6d 61 69 6e 69  .  Pgno nRemaini
1913d 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ng;         /* N
1913e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 6c  umber of pages l
1913f 65 66 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20  eft to copy */. 
19140 20 50 67 6e 6f 20 6e 50 61 67 65 63 6f 75 6e 74   Pgno nPagecount
19141 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74  ;         /* Tot
19142 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  al number of pag
19143 65 73 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 0a 20  es to copy */.. 
19144 20 69 6e 74 20 69 73 41 74 74 61 63 68 65 64 3b   int isAttached;
19145 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
19146 65 20 6f 6e 63 65 20 62 61 63 6b 75 70 20 68 61  e once backup ha
19147 73 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65  s been registere
19148 64 20 77 69 74 68 20 70 61 67 65 72 20 2a 2f 0a  d with pager */.
19149 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70    sqlite3_backup
1914a 20 2a 70 4e 65 78 74 3b 20 20 20 2f 2a 20 4e 65   *pNext;   /* Ne
1914b 78 74 20 62 61 63 6b 75 70 20 61 73 73 6f 63 69  xt backup associ
1914c 61 74 65 64 20 77 69 74 68 20 73 6f 75 72 63 65  ated with source
1914d 20 70 61 67 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   pager */.};../*
1914e 0a 2a 2a 20 54 48 52 45 41 44 20 53 41 46 45 54  .** THREAD SAFET
1914f 59 20 4e 4f 54 45 53 3a 0a 2a 2a 0a 2a 2a 20 20  Y NOTES:.**.**  
19150 20 4f 6e 63 65 20 69 74 20 68 61 73 20 62 65 65   Once it has bee
19151 6e 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20  n created using 
19152 62 61 63 6b 75 70 5f 69 6e 69 74 28 29 2c 20 61  backup_init(), a
19153 20 73 69 6e 67 6c 65 20 73 71 6c 69 74 65 33 5f   single sqlite3_
19154 62 61 63 6b 75 70 0a 2a 2a 20 20 20 73 74 72 75  backup.**   stru
19155 63 74 75 72 65 20 6d 61 79 20 62 65 20 61 63 63  cture may be acc
19156 65 73 73 65 64 20 76 69 61 20 74 77 6f 20 67 72  essed via two gr
19157 6f 75 70 73 20 6f 66 20 74 68 72 65 61 64 2d 73  oups of thread-s
19158 61 66 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 73  afe entry points
19159 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 56 69  :.**.**     * Vi
1915a 61 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62 61  a the sqlite3_ba
1915b 63 6b 75 70 5f 58 58 58 28 29 20 41 50 49 20 66  ckup_XXX() API f
1915c 75 6e 63 74 69 6f 6e 20 62 61 63 6b 75 70 5f 73  unction backup_s
1915d 74 65 70 28 29 20 61 6e 64 20 0a 2a 2a 20 20 20  tep() and .**   
1915e 20 20 20 20 62 61 63 6b 75 70 5f 66 69 6e 69 73      backup_finis
1915f 68 28 29 2e 20 42 6f 74 68 20 74 68 65 73 65 20  h(). Both these 
19160 66 75 6e 63 74 69 6f 6e 73 20 6f 62 74 61 69 6e  functions obtain
19161 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
19162 62 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 68 61  base.**       ha
19163 6e 64 6c 65 20 6d 75 74 65 78 20 61 6e 64 20 74  ndle mutex and t
19164 68 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61  he mutex associa
19165 74 65 64 20 77 69 74 68 20 74 68 65 20 73 6f 75  ted with the sou
19166 72 63 65 20 42 74 53 68 61 72 65 64 20 0a 2a 2a  rce BtShared .**
19167 20 20 20 20 20 20 20 73 74 72 75 63 74 75 72 65         structure
19168 2c 20 69 6e 20 74 68 61 74 20 6f 72 64 65 72 2e  , in that order.
19169 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 56 69 61  .**.**     * Via
1916a 20 74 68 65 20 42 61 63 6b 75 70 55 70 64 61 74   the BackupUpdat
1916b 65 28 29 20 61 6e 64 20 42 61 63 6b 75 70 52 65  e() and BackupRe
1916c 73 74 61 72 74 28 29 20 66 75 6e 63 74 69 6f 6e  start() function
1916d 73 2c 20 77 68 69 63 68 20 61 72 65 0a 2a 2a 20  s, which are.** 
1916e 20 20 20 20 20 20 69 6e 76 6f 6b 65 64 20 62 79        invoked by
1916f 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72   the pager layer
19170 20 74 6f 20 72 65 70 6f 72 74 20 76 61 72 69 6f   to report vario
19171 75 73 20 73 74 61 74 65 20 63 68 61 6e 67 65 73  us state changes
19172 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 74 68 65   in.**       the
19173 20 70 61 67 65 20 63 61 63 68 65 20 61 73 73 6f   page cache asso
19174 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
19175 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65 2e  source database.
19176 20 54 68 65 20 6d 75 74 65 78 0a 2a 2a 20 20 20   The mutex.**   
19177 20 20 20 20 61 73 73 6f 63 69 61 74 65 64 20 77      associated w
19178 69 74 68 20 74 68 65 20 73 6f 75 72 63 65 20 64  ith the source d
19179 61 74 61 62 61 73 65 20 42 74 53 68 61 72 65 64  atabase BtShared
1917a 20 73 74 72 75 63 74 75 72 65 20 77 69 6c 6c 20   structure will 
1917b 61 6c 77 61 79 73 20 0a 2a 2a 20 20 20 20 20 20  always .**      
1917c 20 62 65 20 68 65 6c 64 20 77 68 65 6e 20 65 69   be held when ei
1917d 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 66 75  ther of these fu
1917e 6e 63 74 69 6f 6e 73 20 61 72 65 20 69 6e 76 6f  nctions are invo
1917f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 54 68 65  ked..**.**   The
19180 20 6f 74 68 65 72 20 73 71 6c 69 74 65 33 5f 62   other sqlite3_b
19181 61 63 6b 75 70 5f 58 58 58 28 29 20 41 50 49 20  ackup_XXX() API 
19182 66 75 6e 63 74 69 6f 6e 73 2c 20 62 61 63 6b 75  functions, backu
19183 70 5f 72 65 6d 61 69 6e 69 6e 67 28 29 20 61 6e  p_remaining() an
19184 64 0a 2a 2a 20 20 20 62 61 63 6b 75 70 5f 70 61  d.**   backup_pa
19185 67 65 63 6f 75 6e 74 28 29 20 61 72 65 20 6e 6f  gecount() are no
19186 74 20 74 68 72 65 61 64 2d 73 61 66 65 20 66 75  t thread-safe fu
19187 6e 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 79  nctions. If they
19188 20 61 72 65 20 63 61 6c 6c 65 64 0a 2a 2a 20 20   are called.**  
19189 20 77 68 69 6c 65 20 73 6f 6d 65 20 6f 74 68 65   while some othe
1918a 72 20 74 68 72 65 61 64 20 69 73 20 63 61 6c 6c  r thread is call
1918b 69 6e 67 20 62 61 63 6b 75 70 5f 73 74 65 70 28  ing backup_step(
1918c 29 20 6f 72 20 62 61 63 6b 75 70 5f 66 69 6e 69  ) or backup_fini
1918d 73 68 28 29 2c 0a 2a 2a 20 20 20 74 68 65 20 76  sh(),.**   the v
1918e 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64 20 6d  alues returned m
1918f 61 79 20 62 65 20 69 6e 76 61 6c 69 64 2e 20 54  ay be invalid. T
19190 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 66  here is no way f
19191 6f 72 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20  or a call to.** 
19192 20 20 42 61 63 6b 75 70 55 70 64 61 74 65 28 29    BackupUpdate()
19193 20 6f 72 20 42 61 63 6b 75 70 52 65 73 74 61 72   or BackupRestar
19194 74 28 29 20 74 6f 20 69 6e 74 65 72 66 65 72 65  t() to interfere
19195 20 77 69 74 68 20 62 61 63 6b 75 70 5f 72 65 6d   with backup_rem
19196 61 69 6e 69 6e 67 28 29 0a 2a 2a 20 20 20 6f 72  aining().**   or
19197 20 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e   backup_pagecoun
19198 74 28 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 65 70  t()..**.**   Dep
19199 65 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 53 51  ending on the SQ
1919a 4c 69 74 65 20 63 6f 6e 66 69 67 75 72 61 74 69  Lite configurati
1919b 6f 6e 2c 20 74 68 65 20 64 61 74 61 62 61 73 65  on, the database
1919c 20 68 61 6e 64 6c 65 73 20 61 6e 64 2f 6f 72 0a   handles and/or.
1919d 2a 2a 20 20 20 74 68 65 20 42 74 72 65 65 20 6f  **   the Btree o
1919e 62 6a 65 63 74 73 20 6d 61 79 20 68 61 76 65 20  bjects may have 
1919f 74 68 65 69 72 20 6f 77 6e 20 6d 75 74 65 78 65  their own mutexe
191a0 73 20 74 68 61 74 20 72 65 71 75 69 72 65 20 6c  s that require l
191a1 6f 63 6b 69 6e 67 2e 0a 2a 2a 20 20 20 4e 6f 6e  ocking..**   Non
191a2 2d 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73  -sharable Btrees
191a3 20 28 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61   (in-memory data
191a4 62 61 73 65 73 20 66 6f 72 20 65 78 61 6d 70 6c  bases for exampl
191a5 65 29 2c 20 64 6f 20 6e 6f 74 20 68 61 76 65 0a  e), do not have.
191a6 2a 2a 20 20 20 61 73 73 6f 63 69 61 74 65 64 20  **   associated 
191a7 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a  mutexes..*/../*.
191a8 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
191a9 74 65 72 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  ter correspondin
191aa 67 20 74 6f 20 64 61 74 61 62 61 73 65 20 7a 44  g to database zD
191ab 62 20 28 69 2e 65 2e 20 22 6d 61 69 6e 22 2c 20  b (i.e. "main", 
191ac 22 74 65 6d 70 22 29 0a 2a 2a 20 69 6e 20 63 6f  "temp").** in co
191ad 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20  nnection handle 
191ae 70 44 62 2e 20 49 66 20 73 75 63 68 20 61 20 64  pDb. If such a d
191af 61 74 61 62 61 73 65 20 63 61 6e 6e 6f 74 20 62  atabase cannot b
191b0 65 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 0a  e found, return.
191b1 2a 2a 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  ** a NULL pointe
191b2 72 20 61 6e 64 20 77 72 69 74 65 20 61 6e 20 65  r and write an e
191b3 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20  rror message to 
191b4 70 45 72 72 6f 72 44 62 2e 0a 2a 2a 0a 2a 2a 20  pErrorDb..**.** 
191b5 49 66 20 74 68 65 20 22 74 65 6d 70 22 20 64 61  If the "temp" da
191b6 74 61 62 61 73 65 20 69 73 20 72 65 71 75 65 73  tabase is reques
191b7 74 65 64 2c 20 69 74 20 6d 61 79 20 6e 65 65 64  ted, it may need
191b8 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20 62 79   to be opened by
191b9 20 74 68 69 73 20 0a 2a 2a 20 66 75 6e 63 74 69   this .** functi
191ba 6f 6e 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  on. If an error 
191bb 6f 63 63 75 72 73 20 77 68 69 6c 65 20 64 6f 69  occurs while doi
191bc 6e 67 20 73 6f 2c 20 72 65 74 75 72 6e 20 30 20  ng so, return 0 
191bd 61 6e 64 20 77 72 69 74 65 20 61 6e 20 0a 2a 2a  and write an .**
191be 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74   error message t
191bf 6f 20 70 45 72 72 6f 72 44 62 2e 0a 2a 2f 0a 73  o pErrorDb..*/.s
191c0 74 61 74 69 63 20 42 74 72 65 65 20 2a 66 69 6e  tatic Btree *fin
191c1 64 42 74 72 65 65 28 73 71 6c 69 74 65 33 20 2a  dBtree(sqlite3 *
191c2 70 45 72 72 6f 72 44 62 2c 20 73 71 6c 69 74 65  pErrorDb, sqlite
191c3 33 20 2a 70 44 62 2c 20 63 6f 6e 73 74 20 63 68  3 *pDb, const ch
191c4 61 72 20 2a 7a 44 62 29 7b 0a 20 20 69 6e 74 20  ar *zDb){.  int 
191c5 69 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44  i = sqlite3FindD
191c6 62 4e 61 6d 65 28 70 44 62 2c 20 7a 44 62 29 3b  bName(pDb, zDb);
191c7 0a 0a 20 20 69 66 28 20 69 3d 3d 31 20 29 7b 0a  ..  if( i==1 ){.
191c8 20 20 20 20 50 61 72 73 65 20 2a 70 50 61 72 73      Parse *pPars
191c9 65 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20  e;.    int rc = 
191ca 30 3b 0a 20 20 20 20 70 50 61 72 73 65 20 3d 20  0;.    pParse = 
191cb 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f  sqlite3StackAllo
191cc 63 5a 65 72 6f 28 70 45 72 72 6f 72 44 62 2c 20  cZero(pErrorDb, 
191cd 73 69 7a 65 6f 66 28 2a 70 50 61 72 73 65 29 29  sizeof(*pParse))
191ce 3b 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65  ;.    if( pParse
191cf 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ==0 ){.      sql
191d0 69 74 65 33 45 72 72 6f 72 28 70 45 72 72 6f 72  ite3Error(pError
191d1 44 62 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  Db, SQLITE_NOMEM
191d2 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79  , "out of memory
191d3 22 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  ");.      rc = S
191d4 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
191d5 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 50   }else{.      pP
191d6 61 72 73 65 2d 3e 64 62 20 3d 20 70 44 62 3b 0a  arse->db = pDb;.
191d7 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
191d8 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73  3OpenTempDatabas
191d9 65 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20  e(pParse) ){.   
191da 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
191db 72 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a  rClear(pParse);.
191dc 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
191dd 72 72 6f 72 28 70 45 72 72 6f 72 44 62 2c 20 70  rror(pErrorDb, p
191de 50 61 72 73 65 2d 3e 72 63 2c 20 22 25 73 22 2c  Parse->rc, "%s",
191df 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
191e0 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
191e1 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
191e2 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
191e3 74 65 33 53 74 61 63 6b 46 72 65 65 28 70 45 72  te3StackFree(pEr
191e4 72 6f 72 44 62 2c 20 70 50 61 72 73 65 29 3b 0a  rorDb, pParse);.
191e5 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
191e6 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
191e7 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   0;.    }.  }.. 
191e8 20 69 66 28 20 69 3c 30 20 29 7b 0a 20 20 20 20   if( i<0 ){.    
191e9 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 45 72  sqlite3Error(pEr
191ea 72 6f 72 44 62 2c 20 53 51 4c 49 54 45 5f 45 52  rorDb, SQLITE_ER
191eb 52 4f 52 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 61  ROR, "unknown da
191ec 74 61 62 61 73 65 20 25 73 22 2c 20 7a 44 62 29  tabase %s", zDb)
191ed 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  ;.    return 0;.
191ee 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 44    }..  return pD
191ef 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 7d  b->aDb[i].pBt;.}
191f0 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
191f1 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  n sqlite3_backup
191f2 20 70 72 6f 63 65 73 73 20 74 6f 20 63 6f 70 79   process to copy
191f3 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
191f4 20 7a 53 72 63 44 62 20 66 72 6f 6d 0a 2a 2a 20   zSrcDb from.** 
191f5 63 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c  connection handl
191f6 65 20 70 53 72 63 44 62 20 74 6f 20 7a 44 65 73  e pSrcDb to zDes
191f7 74 44 62 20 69 6e 20 70 44 65 73 74 44 62 2e 20  tDb in pDestDb. 
191f8 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72  If successful, r
191f9 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74  eturn.** a point
191fa 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 73 71  er to the new sq
191fb 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 6f 62 6a  lite3_backup obj
191fc 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ect..**.** If an
191fd 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e   error occurs, N
191fe 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20  ULL is returned 
191ff 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  and an error cod
19200 65 20 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73  e and error mess
19201 61 67 65 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e  age.** stored in
19202 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
19203 20 70 44 65 73 74 44 62 2e 0a 2a 2f 0a 53 51 4c   pDestDb..*/.SQL
19204 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
19205 62 61 63 6b 75 70 20 2a 73 71 6c 69 74 65 33 5f  backup *sqlite3_
19206 62 61 63 6b 75 70 5f 69 6e 69 74 28 0a 20 20 73  backup_init(.  s
19207 71 6c 69 74 65 33 2a 20 70 44 65 73 74 44 62 2c  qlite3* pDestDb,
19208 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19209 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
1920a 20 74 6f 20 77 72 69 74 65 20 74 6f 20 2a 2f 0a   to write to */.
1920b 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
1920c 65 73 74 44 62 2c 20 20 20 20 20 20 20 20 20 20  estDb,          
1920d 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
1920e 6f 66 20 64 61 74 61 62 61 73 65 20 77 69 74 68  of database with
1920f 69 6e 20 70 44 65 73 74 44 62 20 2a 2f 0a 20 20  in pDestDb */.  
19210 73 71 6c 69 74 65 33 2a 20 70 53 72 63 44 62 2c  sqlite3* pSrcDb,
19211 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19212 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
19213 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20  e connection to 
19214 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 63  read from */.  c
19215 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 72 63 44  onst char *zSrcD
19216 62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b               
19217 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
19218 64 61 74 61 62 61 73 65 20 77 69 74 68 69 6e 20  database within 
19219 70 53 72 63 44 62 20 2a 2f 0a 29 7b 0a 20 20 73  pSrcDb */.){.  s
1921a 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70  qlite3_backup *p
1921b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1921c 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f       /* Value to
1921d 20 72 65 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a   return */..  /*
1921e 20 4c 6f 63 6b 20 74 68 65 20 73 6f 75 72 63 65   Lock the source
1921f 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
19220 2e 20 54 68 65 20 64 65 73 74 69 6e 61 74 69 6f  . The destinatio
19221 6e 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20  n database.  ** 
19222 68 61 6e 64 6c 65 20 69 73 20 6e 6f 74 20 6c 6f  handle is not lo
19223 63 6b 65 64 20 69 6e 20 74 68 69 73 20 72 6f 75  cked in this rou
19224 74 69 6e 65 2c 20 62 75 74 20 69 74 20 69 73 20  tine, but it is 
19225 6c 6f 63 6b 65 64 20 69 6e 0a 20 20 2a 2a 20 73  locked in.  ** s
19226 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
19227 65 70 28 29 2e 20 54 68 65 20 75 73 65 72 20 69  ep(). The user i
19228 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 65 6e  s required to en
19229 73 75 72 65 20 74 68 61 74 20 6e 6f 0a 20 20 2a  sure that no.  *
1922a 2a 20 6f 74 68 65 72 20 74 68 72 65 61 64 20 61  * other thread a
1922b 63 63 65 73 73 65 73 20 74 68 65 20 64 65 73 74  ccesses the dest
1922c 69 6e 61 74 69 6f 6e 20 68 61 6e 64 6c 65 20 66  ination handle f
1922d 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 0a  or the duration.
1922e 20 20 2a 2a 20 6f 66 20 74 68 65 20 62 61 63 6b    ** of the back
1922f 75 70 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 41  up operation.  A
19230 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73  ny attempt to us
19231 65 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  e the destinatio
19232 6e 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20  n.  ** database 
19233 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65  connection while
19234 20 61 20 62 61 63 6b 75 70 20 69 73 20 69 6e 20   a backup is in 
19235 70 72 6f 67 72 65 73 73 20 6d 61 79 20 63 61 75  progress may cau
19236 73 65 0a 20 20 2a 2a 20 61 20 6d 61 6c 66 75 6e  se.  ** a malfun
19237 63 74 69 6f 6e 20 6f 72 20 61 20 64 65 61 64 6c  ction or a deadl
19238 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ock..  */.  sqli
19239 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
1923a 70 53 72 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a  pSrcDb->mutex);.
1923b 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1923c 65 6e 74 65 72 28 70 44 65 73 74 44 62 2d 3e 6d  enter(pDestDb->m
1923d 75 74 65 78 29 3b 0a 0a 20 20 69 66 28 20 70 53  utex);..  if( pS
1923e 72 63 44 62 3d 3d 70 44 65 73 74 44 62 20 29 7b  rcDb==pDestDb ){
1923f 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
19240 72 28 0a 20 20 20 20 20 20 20 20 70 44 65 73 74  r(.        pDest
19241 44 62 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  Db, SQLITE_ERROR
19242 2c 20 22 73 6f 75 72 63 65 20 61 6e 64 20 64 65  , "source and de
19243 73 74 69 6e 61 74 69 6f 6e 20 6d 75 73 74 20 62  stination must b
19244 65 20 64 69 73 74 69 6e 63 74 22 0a 20 20 20 20  e distinct".    
19245 29 3b 0a 20 20 20 20 70 20 3d 20 30 3b 0a 20 20  );.    p = 0;.  
19246 7d 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 41  }else {.    /* A
19247 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f  llocate space fo
19248 72 20 61 20 6e 65 77 20 73 71 6c 69 74 65 33 5f  r a new sqlite3_
19249 62 61 63 6b 75 70 20 6f 62 6a 65 63 74 20 2a 2f  backup object */
1924a 0a 20 20 20 20 70 20 3d 20 28 73 71 6c 69 74 65  .    p = (sqlite
1924b 33 5f 62 61 63 6b 75 70 20 2a 29 73 71 6c 69 74  3_backup *)sqlit
1924c 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66  e3_malloc(sizeof
1924d 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 29  (sqlite3_backup)
1924e 29 3b 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b  );.    if( !p ){
1924f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
19250 72 6f 72 28 70 44 65 73 74 44 62 2c 20 53 51 4c  ror(pDestDb, SQL
19251 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20  ITE_NOMEM, 0);. 
19252 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
19253 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e  f the allocation
19254 20 73 75 63 63 65 65 64 65 64 2c 20 70 6f 70 75   succeeded, popu
19255 6c 61 74 65 20 74 68 65 20 6e 65 77 20 6f 62 6a  late the new obj
19256 65 63 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 20  ect. */.  if( p 
19257 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c  ){.    memset(p,
19258 20 30 2c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74   0, sizeof(sqlit
19259 65 33 5f 62 61 63 6b 75 70 29 29 3b 0a 20 20 20  e3_backup));.   
1925a 20 70 2d 3e 70 53 72 63 20 3d 20 66 69 6e 64 42   p->pSrc = findB
1925b 74 72 65 65 28 70 44 65 73 74 44 62 2c 20 70 53  tree(pDestDb, pS
1925c 72 63 44 62 2c 20 7a 53 72 63 44 62 29 3b 0a 20  rcDb, zSrcDb);. 
1925d 20 20 20 70 2d 3e 70 44 65 73 74 20 3d 20 66 69     p->pDest = fi
1925e 6e 64 42 74 72 65 65 28 70 44 65 73 74 44 62 2c  ndBtree(pDestDb,
1925f 20 70 44 65 73 74 44 62 2c 20 7a 44 65 73 74 44   pDestDb, zDestD
19260 62 29 3b 0a 20 20 20 20 70 2d 3e 70 44 65 73 74  b);.    p->pDest
19261 44 62 20 3d 20 70 44 65 73 74 44 62 3b 0a 20 20  Db = pDestDb;.  
19262 20 20 70 2d 3e 70 53 72 63 44 62 20 3d 20 70 53    p->pSrcDb = pS
19263 72 63 44 62 3b 0a 20 20 20 20 70 2d 3e 69 4e 65  rcDb;.    p->iNe
19264 78 74 20 3d 20 31 3b 0a 20 20 20 20 70 2d 3e 69  xt = 1;.    p->i
19265 73 41 74 74 61 63 68 65 64 20 3d 20 30 3b 0a 0a  sAttached = 0;..
19266 20 20 20 20 69 66 28 20 30 3d 3d 70 2d 3e 70 53      if( 0==p->pS
19267 72 63 20 7c 7c 20 30 3d 3d 70 2d 3e 70 44 65 73  rc || 0==p->pDes
19268 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 6e  t ){.      /* On
19269 65 20 28 6f 72 20 62 6f 74 68 29 20 6f 66 20 74  e (or both) of t
1926a 68 65 20 6e 61 6d 65 64 20 64 61 74 61 62 61 73  he named databas
1926b 65 73 20 64 69 64 20 6e 6f 74 20 65 78 69 73 74  es did not exist
1926c 2e 20 41 6e 20 65 72 72 6f 72 20 68 61 73 0a 20  . An error has. 
1926d 20 20 20 20 20 2a 2a 20 61 6c 72 65 61 64 79 20       ** already 
1926e 62 65 65 6e 20 77 72 69 74 74 65 6e 20 69 6e 74  been written int
1926f 6f 20 74 68 65 20 70 44 65 73 74 44 62 20 68 61  o the pDestDb ha
19270 6e 64 6c 65 2e 20 41 6c 6c 20 74 68 61 74 20 69  ndle. All that i
19271 73 20 6c 65 66 74 0a 20 20 20 20 20 20 2a 2a 20  s left.      ** 
19272 74 6f 20 64 6f 20 68 65 72 65 20 69 73 20 66 72  to do here is fr
19273 65 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f 62  ee the sqlite3_b
19274 61 63 6b 75 70 20 73 74 72 75 63 74 75 72 65 2e  ackup structure.
19275 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
19276 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
19277 0a 20 20 20 20 20 20 70 20 3d 20 30 3b 0a 20 20  .      p = 0;.  
19278 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 20    }.  }.  if( p 
19279 29 7b 0a 20 20 20 20 70 2d 3e 70 53 72 63 2d 3e  ){.    p->pSrc->
1927a 6e 42 61 63 6b 75 70 2b 2b 3b 0a 20 20 7d 0a 0a  nBackup++;.  }..
1927b 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1927c 6c 65 61 76 65 28 70 44 65 73 74 44 62 2d 3e 6d  leave(pDestDb->m
1927d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65 33  utex);.  sqlite3
1927e 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 53 72  _mutex_leave(pSr
1927f 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  cDb->mutex);.  r
19280 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
19281 2a 20 41 72 67 75 6d 65 6e 74 20 72 63 20 69 73  * Argument rc is
19282 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
19283 20 63 6f 64 65 2e 20 52 65 74 75 72 6e 20 74 72   code. Return tr
19284 75 65 20 69 66 20 74 68 69 73 20 65 72 72 6f 72  ue if this error
19285 20 69 73 20 0a 2a 2a 20 63 6f 6e 73 69 64 65 72   is .** consider
19286 65 64 20 66 61 74 61 6c 20 69 66 20 65 6e 63 6f  ed fatal if enco
19287 75 6e 74 65 72 65 64 20 64 75 72 69 6e 67 20 61  untered during a
19288 20 62 61 63 6b 75 70 20 6f 70 65 72 61 74 69 6f   backup operatio
19289 6e 2e 20 41 6c 6c 20 65 72 72 6f 72 73 0a 2a 2a  n. All errors.**
1928a 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20   are considered 
1928b 66 61 74 61 6c 20 65 78 63 65 70 74 20 66 6f 72  fatal except for
1928c 20 53 51 4c 49 54 45 5f 42 55 53 59 20 61 6e 64   SQLITE_BUSY and
1928d 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a   SQLITE_LOCKED..
1928e 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73  */.static int is
1928f 46 61 74 61 6c 45 72 72 6f 72 28 69 6e 74 20 72  FatalError(int r
19290 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 72 63  c){.  return (rc
19291 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72  !=SQLITE_OK && r
19292 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 26  c!=SQLITE_BUSY &
19293 26 20 41 4c 57 41 59 53 28 72 63 21 3d 53 51 4c  & ALWAYS(rc!=SQL
19294 49 54 45 5f 4c 4f 43 4b 45 44 29 29 3b 0a 7d 0a  ITE_LOCKED));.}.
19295 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72  ./*.** Parameter
19296 20 7a 53 72 63 44 61 74 61 20 70 6f 69 6e 74 73   zSrcData points
19297 20 74 6f 20 61 20 62 75 66 66 65 72 20 63 6f 6e   to a buffer con
19298 74 61 69 6e 69 6e 67 20 74 68 65 20 64 61 74 61  taining the data
19299 20 66 6f 72 20 0a 2a 2a 20 70 61 67 65 20 69 53   for .** page iS
1929a 72 63 50 67 20 66 72 6f 6d 20 74 68 65 20 73 6f  rcPg from the so
1929b 75 72 63 65 20 64 61 74 61 62 61 73 65 2e 20 43  urce database. C
1929c 6f 70 79 20 74 68 69 73 20 64 61 74 61 20 69 6e  opy this data in
1929d 74 6f 20 74 68 65 20 0a 2a 2a 20 64 65 73 74 69  to the .** desti
1929e 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2e  nation database.
1929f 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62  .*/.static int b
192a0 61 63 6b 75 70 4f 6e 65 50 61 67 65 28 73 71 6c  ackupOnePage(sql
192a1 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 2c 20  ite3_backup *p, 
192a2 50 67 6e 6f 20 69 53 72 63 50 67 2c 20 63 6f 6e  Pgno iSrcPg, con
192a3 73 74 20 75 38 20 2a 7a 53 72 63 44 61 74 61 29  st u8 *zSrcData)
192a4 7b 0a 20 20 50 61 67 65 72 20 2a 20 63 6f 6e 73  {.  Pager * cons
192a5 74 20 70 44 65 73 74 50 61 67 65 72 20 3d 20 73  t pDestPager = s
192a6 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
192a7 28 70 2d 3e 70 44 65 73 74 29 3b 0a 20 20 63 6f  (p->pDest);.  co
192a8 6e 73 74 20 69 6e 74 20 6e 53 72 63 50 67 73 7a  nst int nSrcPgsz
192a9 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
192aa 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 53  etPageSize(p->pS
192ab 72 63 29 3b 0a 20 20 69 6e 74 20 6e 44 65 73 74  rc);.  int nDest
192ac 50 67 73 7a 20 3d 20 73 71 6c 69 74 65 33 42 74  Pgsz = sqlite3Bt
192ad 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70  reeGetPageSize(p
192ae 2d 3e 70 44 65 73 74 29 3b 0a 20 20 63 6f 6e 73  ->pDest);.  cons
192af 74 20 69 6e 74 20 6e 43 6f 70 79 20 3d 20 4d 49  t int nCopy = MI
192b0 4e 28 6e 53 72 63 50 67 73 7a 2c 20 6e 44 65 73  N(nSrcPgsz, nDes
192b1 74 50 67 73 7a 29 3b 0a 20 20 63 6f 6e 73 74 20  tPgsz);.  const 
192b2 69 36 34 20 69 45 6e 64 20 3d 20 28 69 36 34 29  i64 iEnd = (i64)
192b3 69 53 72 63 50 67 2a 28 69 36 34 29 6e 53 72 63  iSrcPg*(i64)nSrc
192b4 50 67 73 7a 3b 0a 0a 20 20 69 6e 74 20 72 63 20  Pgsz;..  int rc 
192b5 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
192b6 36 34 20 69 4f 66 66 3b 0a 0a 20 20 61 73 73 65  64 iOff;..  asse
192b7 72 74 28 20 70 2d 3e 62 44 65 73 74 4c 6f 63 6b  rt( p->bDestLock
192b8 65 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ed );.  assert( 
192b9 21 69 73 46 61 74 61 6c 45 72 72 6f 72 28 70 2d  !isFatalError(p-
192ba 3e 72 63 29 20 29 3b 0a 20 20 61 73 73 65 72 74  >rc) );.  assert
192bb 28 20 69 53 72 63 50 67 21 3d 50 45 4e 44 49 4e  ( iSrcPg!=PENDIN
192bc 47 5f 42 59 54 45 5f 50 41 47 45 28 70 2d 3e 70  G_BYTE_PAGE(p->p
192bd 53 72 63 2d 3e 70 42 74 29 20 29 3b 0a 20 20 61  Src->pBt) );.  a
192be 73 73 65 72 74 28 20 7a 53 72 63 44 61 74 61 20  ssert( zSrcData 
192bf 29 3b 0a 0a 20 20 2f 2a 20 43 61 74 63 68 20 74  );..  /* Catch t
192c0 68 65 20 63 61 73 65 20 77 68 65 72 65 20 74 68  he case where th
192c1 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 69 73  e destination is
192c2 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61   an in-memory da
192c3 74 61 62 61 73 65 20 61 6e 64 20 74 68 65 0a 20  tabase and the. 
192c4 20 2a 2a 20 70 61 67 65 20 73 69 7a 65 73 20 6f   ** page sizes o
192c5 66 20 74 68 65 20 73 6f 75 72 63 65 20 61 6e 64  f the source and
192c6 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 69 66   destination dif
192c7 66 65 72 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28  fer. .  */.  if(
192c8 20 6e 53 72 63 50 67 73 7a 21 3d 6e 44 65 73 74   nSrcPgsz!=nDest
192c9 50 67 73 7a 20 26 26 20 73 71 6c 69 74 65 33 50  Pgsz && sqlite3P
192ca 61 67 65 72 49 73 4d 65 6d 64 62 28 73 71 6c 69  agerIsMemdb(sqli
192cb 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70 2d  te3BtreePager(p-
192cc 3e 70 44 65 73 74 29 29 20 29 7b 0a 20 20 20 20  >pDest)) ){.    
192cd 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41 44  rc = SQLITE_READ
192ce 4f 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ONLY;.  }..  /* 
192cf 54 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73 20 6f  This loop runs o
192d0 6e 63 65 20 66 6f 72 20 65 61 63 68 20 64 65 73  nce for each des
192d1 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 20 73 70  tination page sp
192d2 61 6e 6e 65 64 20 62 79 20 74 68 65 20 73 6f 75  anned by the sou
192d3 72 63 65 20 0a 20 20 2a 2a 20 70 61 67 65 2e 20  rce .  ** page. 
192d4 46 6f 72 20 65 61 63 68 20 69 74 65 72 61 74 69  For each iterati
192d5 6f 6e 2c 20 76 61 72 69 61 62 6c 65 20 69 4f 66  on, variable iOf
192d6 66 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  f is set to the 
192d7 62 79 74 65 20 6f 66 66 73 65 74 0a 20 20 2a 2a  byte offset.  **
192d8 20 6f 66 20 74 68 65 20 64 65 73 74 69 6e 61 74   of the destinat
192d9 69 6f 6e 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20  ion page..  */. 
192da 20 66 6f 72 28 69 4f 66 66 3d 69 45 6e 64 2d 28   for(iOff=iEnd-(
192db 69 36 34 29 6e 53 72 63 50 67 73 7a 3b 20 72 63  i64)nSrcPgsz; rc
192dc 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
192dd 4f 66 66 3c 69 45 6e 64 3b 20 69 4f 66 66 2b 3d  Off<iEnd; iOff+=
192de 6e 44 65 73 74 50 67 73 7a 29 7b 0a 20 20 20 20  nDestPgsz){.    
192df 44 62 50 61 67 65 20 2a 70 44 65 73 74 50 67 20  DbPage *pDestPg 
192e0 3d 20 30 3b 0a 20 20 20 20 50 67 6e 6f 20 69 44  = 0;.    Pgno iD
192e1 65 73 74 20 3d 20 28 50 67 6e 6f 29 28 69 4f 66  est = (Pgno)(iOf
192e2 66 2f 6e 44 65 73 74 50 67 73 7a 29 2b 31 3b 0a  f/nDestPgsz)+1;.
192e3 20 20 20 20 69 66 28 20 69 44 65 73 74 3d 3d 50      if( iDest==P
192e4 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45  ENDING_BYTE_PAGE
192e5 28 70 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 20  (p->pDest->pBt) 
192e6 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
192e7 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28  if( SQLITE_OK==(
192e8 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
192e9 72 47 65 74 28 70 44 65 73 74 50 61 67 65 72 2c  rGet(pDestPager,
192ea 20 69 44 65 73 74 2c 20 26 70 44 65 73 74 50 67   iDest, &pDestPg
192eb 29 29 0a 20 20 20 20 20 26 26 20 53 51 4c 49 54  )).     && SQLIT
192ec 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK==(rc = sqli
192ed 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 44  te3PagerWrite(pD
192ee 65 73 74 50 67 29 29 0a 20 20 20 20 29 7b 0a 20  estPg)).    ){. 
192ef 20 20 20 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a       const u8 *z
192f0 49 6e 20 3d 20 26 7a 53 72 63 44 61 74 61 5b 69  In = &zSrcData[i
192f1 4f 66 66 25 6e 53 72 63 50 67 73 7a 5d 3b 0a 20  Off%nSrcPgsz];. 
192f2 20 20 20 20 20 75 38 20 2a 7a 44 65 73 74 44 61       u8 *zDestDa
192f3 74 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  ta = sqlite3Page
192f4 72 47 65 74 44 61 74 61 28 70 44 65 73 74 50 67  rGetData(pDestPg
192f5 29 3b 0a 20 20 20 20 20 20 75 38 20 2a 7a 4f 75  );.      u8 *zOu
192f6 74 20 3d 20 26 7a 44 65 73 74 44 61 74 61 5b 69  t = &zDestData[i
192f7 4f 66 66 25 6e 44 65 73 74 50 67 73 7a 5d 3b 0a  Off%nDestPgsz];.
192f8 0a 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 74  .      /* Copy t
192f9 68 65 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65  he data from the
192fa 20 73 6f 75 72 63 65 20 70 61 67 65 20 69 6e 74   source page int
192fb 6f 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  o the destinatio
192fc 6e 20 70 61 67 65 2e 0a 20 20 20 20 20 20 2a 2a  n page..      **
192fd 20 54 68 65 6e 20 63 6c 65 61 72 20 74 68 65 20   Then clear the 
192fe 42 74 72 65 65 20 6c 61 79 65 72 20 4d 65 6d 50  Btree layer MemP
192ff 61 67 65 2e 69 73 49 6e 69 74 20 66 6c 61 67 2e  age.isInit flag.
19300 20 42 6f 74 68 20 74 68 69 73 20 6d 6f 64 75 6c   Both this modul
19301 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 74  e.      ** and t
19302 68 65 20 70 61 67 65 72 20 63 6f 64 65 20 75 73  he pager code us
19303 65 20 74 68 69 73 20 74 72 69 63 6b 20 28 63 6c  e this trick (cl
19304 65 61 72 69 6e 67 20 74 68 65 20 66 69 72 73 74  earing the first
19305 20 62 79 74 65 0a 20 20 20 20 20 20 2a 2a 20 6f   byte.      ** o
19306 66 20 74 68 65 20 70 61 67 65 20 27 65 78 74 72  f the page 'extr
19307 61 27 20 73 70 61 63 65 20 74 6f 20 69 6e 76 61  a' space to inva
19308 6c 69 64 61 74 65 20 74 68 65 20 42 74 72 65 65  lidate the Btree
19309 20 6c 61 79 65 72 73 0a 20 20 20 20 20 20 2a 2a   layers.      **
1930a 20 63 61 63 68 65 64 20 70 61 72 73 65 20 6f 66   cached parse of
1930b 20 74 68 65 20 70 61 67 65 29 2e 20 4d 65 6d 50   the page). MemP
1930c 61 67 65 2e 69 73 49 6e 69 74 20 69 73 20 6d 61  age.isInit is ma
1930d 72 6b 65 64 20 0a 20 20 20 20 20 20 2a 2a 20 22  rked .      ** "
1930e 4d 55 53 54 20 42 45 20 46 49 52 53 54 22 20 66  MUST BE FIRST" f
1930f 6f 72 20 74 68 69 73 20 70 75 72 70 6f 73 65 2e  or this purpose.
19310 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
19311 6d 65 6d 63 70 79 28 7a 4f 75 74 2c 20 7a 49 6e  memcpy(zOut, zIn
19312 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20 20 20  , nCopy);.      
19313 28 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61  ((u8 *)sqlite3Pa
19314 67 65 72 47 65 74 45 78 74 72 61 28 70 44 65 73  gerGetExtra(pDes
19315 74 50 67 29 29 5b 30 5d 20 3d 20 30 3b 0a 20 20  tPg))[0] = 0;.  
19316 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50    }.    sqlite3P
19317 61 67 65 72 55 6e 72 65 66 28 70 44 65 73 74 50  agerUnref(pDestP
19318 67 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  g);.  }..  retur
19319 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  n rc;.}../*.** I
1931a 66 20 70 46 69 6c 65 20 69 73 20 63 75 72 72 65  f pFile is curre
1931b 6e 74 6c 79 20 6c 61 72 67 65 72 20 74 68 61 6e  ntly larger than
1931c 20 69 53 69 7a 65 20 62 79 74 65 73 2c 20 74 68   iSize bytes, th
1931d 65 6e 20 74 72 75 6e 63 61 74 65 20 69 74 20 74  en truncate it t
1931e 6f 0a 2a 2a 20 65 78 61 63 74 6c 79 20 69 53 69  o.** exactly iSi
1931f 7a 65 20 62 79 74 65 73 2e 20 49 66 20 70 46 69  ze bytes. If pFi
19320 6c 65 20 69 73 20 6e 6f 74 20 6c 61 72 67 65 72  le is not larger
19321 20 74 68 61 6e 20 69 53 69 7a 65 20 62 79 74 65   than iSize byte
19322 73 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20  s, then.** this 
19323 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f  function is a no
19324 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  -op..**.** Retur
19325 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65  n SQLITE_OK if e
19326 76 65 72 79 74 68 69 6e 67 20 69 73 20 73 75 63  verything is suc
19327 63 65 73 73 66 75 6c 2c 20 6f 72 20 61 6e 20 53  cessful, or an S
19328 51 4c 69 74 65 20 65 72 72 6f 72 20 0a 2a 2a 20  QLite error .** 
19329 63 6f 64 65 20 69 66 20 61 6e 20 65 72 72 6f 72  code if an error
1932a 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74   occurs..*/.stat
1932b 69 63 20 69 6e 74 20 62 61 63 6b 75 70 54 72 75  ic int backupTru
1932c 6e 63 61 74 65 46 69 6c 65 28 73 71 6c 69 74 65  ncateFile(sqlite
1932d 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 20 69  3_file *pFile, i
1932e 36 34 20 69 53 69 7a 65 29 7b 0a 20 20 69 36 34  64 iSize){.  i64
1932f 20 69 43 75 72 72 65 6e 74 3b 0a 20 20 69 6e 74   iCurrent;.  int
19330 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46   rc = sqlite3OsF
19331 69 6c 65 53 69 7a 65 28 70 46 69 6c 65 2c 20 26  ileSize(pFile, &
19332 69 43 75 72 72 65 6e 74 29 3b 0a 20 20 69 66 28  iCurrent);.  if(
19333 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
19334 26 20 69 43 75 72 72 65 6e 74 3e 69 53 69 7a 65  & iCurrent>iSize
19335 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
19336 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70  ite3OsTruncate(p
19337 46 69 6c 65 2c 20 69 53 69 7a 65 29 3b 0a 20 20  File, iSize);.  
19338 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
19339 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ../*.** Register
1933a 20 74 68 69 73 20 62 61 63 6b 75 70 20 6f 62 6a   this backup obj
1933b 65 63 74 20 77 69 74 68 20 74 68 65 20 61 73 73  ect with the ass
1933c 6f 63 69 61 74 65 64 20 73 6f 75 72 63 65 20 70  ociated source p
1933d 61 67 65 72 20 66 6f 72 0a 2a 2a 20 63 61 6c 6c  ager for.** call
1933e 62 61 63 6b 73 20 77 68 65 6e 20 70 61 67 65 73  backs when pages
1933f 20 61 72 65 20 63 68 61 6e 67 65 64 20 6f 72 20   are changed or 
19340 74 68 65 20 63 61 63 68 65 20 69 6e 76 61 6c 69  the cache invali
19341 64 61 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  dated..*/.static
19342 20 76 6f 69 64 20 61 74 74 61 63 68 42 61 63 6b   void attachBack
19343 75 70 4f 62 6a 65 63 74 28 73 71 6c 69 74 65 33  upObject(sqlite3
19344 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a 20 20 73  _backup *p){.  s
19345 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a 2a  qlite3_backup **
19346 70 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  pp;.  assert( sq
19347 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d  lite3BtreeHoldsM
19348 75 74 65 78 28 70 2d 3e 70 53 72 63 29 20 29 3b  utex(p->pSrc) );
19349 0a 20 20 70 70 20 3d 20 73 71 6c 69 74 65 33 50  .  pp = sqlite3P
1934a 61 67 65 72 42 61 63 6b 75 70 50 74 72 28 73 71  agerBackupPtr(sq
1934b 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
1934c 70 2d 3e 70 53 72 63 29 29 3b 0a 20 20 70 2d 3e  p->pSrc));.  p->
1934d 70 4e 65 78 74 20 3d 20 2a 70 70 3b 0a 20 20 2a  pNext = *pp;.  *
1934e 70 70 20 3d 20 70 3b 0a 20 20 70 2d 3e 69 73 41  pp = p;.  p->isA
1934f 74 74 61 63 68 65 64 20 3d 20 31 3b 0a 7d 0a 0a  ttached = 1;.}..
19350 2f 2a 0a 2a 2a 20 43 6f 70 79 20 6e 50 61 67 65  /*.** Copy nPage
19351 20 70 61 67 65 73 20 66 72 6f 6d 20 74 68 65 20   pages from the 
19352 73 6f 75 72 63 65 20 62 2d 74 72 65 65 20 74 6f  source b-tree to
19353 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
19354 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
19355 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61 63 6b  int sqlite3_back
19356 75 70 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f  up_step(sqlite3_
19357 62 61 63 6b 75 70 20 2a 70 2c 20 69 6e 74 20 6e  backup *p, int n
19358 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b  Page){.  int rc;
19359 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
1935a 78 5f 65 6e 74 65 72 28 70 2d 3e 70 53 72 63 44  x_enter(p->pSrcD
1935b 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c  b->mutex);.  sql
1935c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
1935d 2d 3e 70 53 72 63 29 3b 0a 20 20 69 66 28 20 70  ->pSrc);.  if( p
1935e 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20  ->pDestDb ){.   
1935f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
19360 6e 74 65 72 28 70 2d 3e 70 44 65 73 74 44 62 2d  nter(p->pDestDb-
19361 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20  >mutex);.  }..  
19362 72 63 20 3d 20 70 2d 3e 72 63 3b 0a 20 20 69 66  rc = p->rc;.  if
19363 28 20 21 69 73 46 61 74 61 6c 45 72 72 6f 72 28  ( !isFatalError(
19364 72 63 29 20 29 7b 0a 20 20 20 20 50 61 67 65 72  rc) ){.    Pager
19365 20 2a 20 63 6f 6e 73 74 20 70 53 72 63 50 61 67   * const pSrcPag
19366 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  er = sqlite3Btre
19367 65 50 61 67 65 72 28 70 2d 3e 70 53 72 63 29 3b  ePager(p->pSrc);
19368 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70       /* Source p
19369 61 67 65 72 20 2a 2f 0a 20 20 20 20 50 61 67 65  ager */.    Page
1936a 72 20 2a 20 63 6f 6e 73 74 20 70 44 65 73 74 50  r * const pDestP
1936b 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74  ager = sqlite3Bt
1936c 72 65 65 50 61 67 65 72 28 70 2d 3e 70 44 65 73  reePager(p->pDes
1936d 74 29 3b 20 20 20 2f 2a 20 44 65 73 74 20 70 61  t);   /* Dest pa
1936e 67 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ger */.    int i
1936f 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
19370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19371 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62   Iterator variab
19372 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 53  le */.    int nS
19373 72 63 50 61 67 65 20 3d 20 2d 31 3b 20 20 20 20  rcPage = -1;    
19374 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
19375 53 69 7a 65 20 6f 66 20 73 6f 75 72 63 65 20 64  Size of source d
19376 62 20 69 6e 20 70 61 67 65 73 20 2a 2f 0a 20 20  b in pages */.  
19377 20 20 69 6e 74 20 62 43 6c 6f 73 65 54 72 61 6e    int bCloseTran
19378 73 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  s = 0;          
19379 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
1937a 73 72 63 20 64 62 20 72 65 71 75 69 72 65 73 20  src db requires 
1937b 75 6e 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 0a 20 20  unlocking */..  
1937c 20 20 2f 2a 20 49 66 20 74 68 65 20 73 6f 75 72    /* If the sour
1937d 63 65 20 70 61 67 65 72 20 69 73 20 63 75 72 72  ce pager is curr
1937e 65 6e 74 6c 79 20 69 6e 20 61 20 77 72 69 74 65  ently in a write
1937f 2d 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 72 65  -transaction, re
19380 74 75 72 6e 0a 20 20 20 20 2a 2a 20 53 51 4c 49  turn.    ** SQLI
19381 54 45 5f 42 55 53 59 20 69 6d 6d 65 64 69 61 74  TE_BUSY immediat
19382 65 6c 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ely..    */.    
19383 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 26  if( p->pDestDb &
19384 26 20 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e  & p->pSrc->pBt->
19385 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54  inTransaction==T
19386 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20  RANS_WRITE ){.  
19387 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
19388 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  BUSY;.    }else{
19389 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1938a 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 0a 20 20  TE_OK;.    }..  
1938b 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 64 65    /* Lock the de
1938c 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61  stination databa
1938d 73 65 2c 20 69 66 20 69 74 20 69 73 20 6e 6f 74  se, if it is not
1938e 20 6c 6f 63 6b 65 64 20 61 6c 72 65 61 64 79 2e   locked already.
1938f 20 2a 2f 0a 20 20 20 20 69 66 28 20 53 51 4c 49   */.    if( SQLI
19390 54 45 5f 4f 4b 3d 3d 72 63 20 26 26 20 70 2d 3e  TE_OK==rc && p->
19391 62 44 65 73 74 4c 6f 63 6b 65 64 3d 3d 30 0a 20  bDestLocked==0. 
19392 20 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b      && SQLITE_OK
19393 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42  ==(rc = sqlite3B
19394 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70  treeBeginTrans(p
19395 2d 3e 70 44 65 73 74 2c 20 32 29 29 20 0a 20 20  ->pDest, 2)) .  
19396 20 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 62 44    ){.      p->bD
19397 65 73 74 4c 6f 63 6b 65 64 20 3d 20 31 3b 0a 20  estLocked = 1;. 
19398 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
19399 65 47 65 74 4d 65 74 61 28 70 2d 3e 70 44 65 73  eGetMeta(p->pDes
1939a 74 2c 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f  t, BTREE_SCHEMA_
1939b 56 45 52 53 49 4f 4e 2c 20 26 70 2d 3e 69 44 65  VERSION, &p->iDe
1939c 73 74 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 7d  stSchema);.    }
1939d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72  ..    /* If ther
1939e 65 20 69 73 20 6e 6f 20 6f 70 65 6e 20 72 65 61  e is no open rea
1939f 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e  d-transaction on
193a0 20 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61   the source data
193a1 62 61 73 65 2c 20 6f 70 65 6e 0a 20 20 20 20 2a  base, open.    *
193a2 2a 20 6f 6e 65 20 6e 6f 77 2e 20 49 66 20 61 20  * one now. If a 
193a3 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f  transaction is o
193a4 70 65 6e 65 64 20 68 65 72 65 2c 20 74 68 65 6e  pened here, then
193a5 20 69 74 20 77 69 6c 6c 20 62 65 20 63 6c 6f 73   it will be clos
193a6 65 64 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65  ed.    ** before
193a7 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 65   this function e
193a8 78 69 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  xits..    */.   
193a9 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
193aa 4f 4b 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33  OK && 0==sqlite3
193ab 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72 61  BtreeIsInReadTra
193ac 6e 73 28 70 2d 3e 70 53 72 63 29 20 29 7b 0a 20  ns(p->pSrc) ){. 
193ad 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
193ae 33 42 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73  3BtreeBeginTrans
193af 28 70 2d 3e 70 53 72 63 2c 20 30 29 3b 0a 20 20  (p->pSrc, 0);.  
193b0 20 20 20 20 62 43 6c 6f 73 65 54 72 61 6e 73 20      bCloseTrans 
193b1 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20  = 1;.    }.  .  
193b2 20 20 2f 2a 20 4e 6f 77 20 74 68 61 74 20 74 68    /* Now that th
193b3 65 72 65 20 69 73 20 61 20 72 65 61 64 2d 6c 6f  ere is a read-lo
193b4 63 6b 20 6f 6e 20 74 68 65 20 73 6f 75 72 63 65  ck on the source
193b5 20 64 61 74 61 62 61 73 65 2c 20 71 75 65 72 79   database, query
193b6 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 6f 75 72   the.    ** sour
193b7 63 65 20 70 61 67 65 72 20 66 6f 72 20 74 68 65  ce pager for the
193b8 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
193b9 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
193ba 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
193bb 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
193bc 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
193bd 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75  ite3PagerPagecou
193be 6e 74 28 70 53 72 63 50 61 67 65 72 2c 20 26 6e  nt(pSrcPager, &n
193bf 53 72 63 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a  SrcPage);.    }.
193c0 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 28 6e      for(ii=0; (n
193c1 50 61 67 65 3c 30 20 7c 7c 20 69 69 3c 6e 50 61  Page<0 || ii<nPa
193c2 67 65 29 20 26 26 20 70 2d 3e 69 4e 65 78 74 3c  ge) && p->iNext<
193c3 3d 28 50 67 6e 6f 29 6e 53 72 63 50 61 67 65 20  =(Pgno)nSrcPage 
193c4 26 26 20 21 72 63 3b 20 69 69 2b 2b 29 7b 0a 20  && !rc; ii++){. 
193c5 20 20 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20       const Pgno 
193c6 69 53 72 63 50 67 20 3d 20 70 2d 3e 69 4e 65 78  iSrcPg = p->iNex
193c7 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
193c8 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70 61 67     /* Source pag
193c9 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20  e number */.    
193ca 20 20 69 66 28 20 69 53 72 63 50 67 21 3d 50 45    if( iSrcPg!=PE
193cb 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
193cc 70 2d 3e 70 53 72 63 2d 3e 70 42 74 29 20 29 7b  p->pSrc->pBt) ){
193cd 0a 20 20 20 20 20 20 20 20 44 62 50 61 67 65 20  .        DbPage 
193ce 2a 70 53 72 63 50 67 3b 20 20 20 20 20 20 20 20  *pSrcPg;        
193cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
193d0 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 70       /* Source p
193d1 61 67 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  age object */.  
193d2 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
193d3 65 33 50 61 67 65 72 47 65 74 28 70 53 72 63 50  e3PagerGet(pSrcP
193d4 61 67 65 72 2c 20 69 53 72 63 50 67 2c 20 26 70  ager, iSrcPg, &p
193d5 53 72 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20  SrcPg);.        
193d6 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
193d7 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  K ){.          r
193d8 63 20 3d 20 62 61 63 6b 75 70 4f 6e 65 50 61 67  c = backupOnePag
193d9 65 28 70 2c 20 69 53 72 63 50 67 2c 20 73 71 6c  e(p, iSrcPg, sql
193da 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61  ite3PagerGetData
193db 28 70 53 72 63 50 67 29 29 3b 0a 20 20 20 20 20  (pSrcPg));.     
193dc 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
193dd 72 55 6e 72 65 66 28 70 53 72 63 50 67 29 3b 0a  rUnref(pSrcPg);.
193de 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
193df 7d 0a 20 20 20 20 20 20 70 2d 3e 69 4e 65 78 74  }.      p->iNext
193e0 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ++;.    }.    if
193e1 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
193e2 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 50 61 67  ){.      p->nPag
193e3 65 63 6f 75 6e 74 20 3d 20 6e 53 72 63 50 61 67  ecount = nSrcPag
193e4 65 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 6d  e;.      p->nRem
193e5 61 69 6e 69 6e 67 20 3d 20 6e 53 72 63 50 61 67  aining = nSrcPag
193e6 65 2b 31 2d 70 2d 3e 69 4e 65 78 74 3b 0a 20 20  e+1-p->iNext;.  
193e7 20 20 20 20 69 66 28 20 70 2d 3e 69 4e 65 78 74      if( p->iNext
193e8 3e 28 50 67 6e 6f 29 6e 53 72 63 50 61 67 65 20  >(Pgno)nSrcPage 
193e9 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
193ea 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20  SQLITE_DONE;.   
193eb 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70 2d     }else if( !p-
193ec 3e 69 73 41 74 74 61 63 68 65 64 20 29 7b 0a 20  >isAttached ){. 
193ed 20 20 20 20 20 20 20 61 74 74 61 63 68 42 61 63         attachBac
193ee 6b 75 70 4f 62 6a 65 63 74 28 70 29 3b 0a 20 20  kupObject(p);.  
193ef 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20      }.    }.  . 
193f0 20 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65     /* Update the
193f1 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 20   schema version 
193f2 66 69 65 6c 64 20 69 6e 20 74 68 65 20 64 65 73  field in the des
193f3 74 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73  tination databas
193f4 65 2e 20 54 68 69 73 0a 20 20 20 20 2a 2a 20 69  e. This.    ** i
193f5 73 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  s to make sure t
193f6 68 61 74 20 74 68 65 20 73 63 68 65 6d 61 2d 76  hat the schema-v
193f7 65 72 73 69 6f 6e 20 72 65 61 6c 6c 79 20 64 6f  ersion really do
193f8 65 73 20 63 68 61 6e 67 65 20 69 6e 0a 20 20 20  es change in.   
193f9 20 2a 2a 20 74 68 65 20 63 61 73 65 20 77 68 65   ** the case whe
193fa 72 65 20 74 68 65 20 73 6f 75 72 63 65 20 61 6e  re the source an
193fb 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61  d destination da
193fc 74 61 62 61 73 65 73 20 68 61 76 65 20 74 68 65  tabases have the
193fd 0a 20 20 20 20 2a 2a 20 73 61 6d 65 20 73 63 68  .    ** same sch
193fe 65 6d 61 20 76 65 72 73 69 6f 6e 2e 0a 20 20 20  ema version..   
193ff 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   */.    if( rc==
19400 53 51 4c 49 54 45 5f 44 4f 4e 45 20 0a 20 20 20  SQLITE_DONE .   
19401 20 20 26 26 20 28 72 63 20 3d 20 73 71 6c 69 74    && (rc = sqlit
19402 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74  e3BtreeUpdateMet
19403 61 28 70 2d 3e 70 44 65 73 74 2c 31 2c 70 2d 3e  a(p->pDest,1,p->
19404 69 44 65 73 74 53 63 68 65 6d 61 2b 31 29 29 3d  iDestSchema+1))=
19405 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 20 20 29  =SQLITE_OK.    )
19406 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69 6e  {.      const in
19407 74 20 6e 53 72 63 50 61 67 65 73 69 7a 65 20 3d  t nSrcPagesize =
19408 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
19409 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 53 72 63  PageSize(p->pSrc
1940a 29 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 69  );.      const i
1940b 6e 74 20 6e 44 65 73 74 50 61 67 65 73 69 7a 65  nt nDestPagesize
1940c 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
1940d 65 74 50 61 67 65 53 69 7a 65 28 70 2d 3e 70 44  etPageSize(p->pD
1940e 65 73 74 29 3b 0a 20 20 20 20 20 20 69 6e 74 20  est);.      int 
1940f 6e 44 65 73 74 54 72 75 6e 63 61 74 65 3b 0a 20  nDestTruncate;. 
19410 20 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70   .      if( p->p
19411 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 20 20  DestDb ){.      
19412 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
19413 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 70 2d 3e  ternalSchema(p->
19414 70 44 65 73 74 44 62 2c 20 30 29 3b 0a 20 20 20  pDestDb, 0);.   
19415 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 53     }..      /* S
19416 65 74 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65  et nDestTruncate
19417 20 74 6f 20 74 68 65 20 66 69 6e 61 6c 20 6e 75   to the final nu
19418 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
19419 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
1941a 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61  .      ** databa
1941b 73 65 2e 20 54 68 65 20 63 6f 6d 70 6c 69 63 61  se. The complica
1941c 74 69 6f 6e 20 68 65 72 65 20 69 73 20 74 68 61  tion here is tha
1941d 74 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f  t the destinatio
1941e 6e 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20  n page.      ** 
1941f 73 69 7a 65 20 6d 61 79 20 62 65 20 64 69 66 66  size may be diff
19420 65 72 65 6e 74 20 74 6f 20 74 68 65 20 73 6f 75  erent to the sou
19421 72 63 65 20 70 61 67 65 20 73 69 7a 65 2e 20 0a  rce page size. .
19422 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
19423 2a 20 49 66 20 74 68 65 20 73 6f 75 72 63 65 20  * If the source 
19424 70 61 67 65 20 73 69 7a 65 20 69 73 20 73 6d 61  page size is sma
19425 6c 6c 65 72 20 74 68 61 6e 20 74 68 65 20 64 65  ller than the de
19426 73 74 69 6e 61 74 69 6f 6e 20 70 61 67 65 20 73  stination page s
19427 69 7a 65 2c 20 0a 20 20 20 20 20 20 2a 2a 20 72  ize, .      ** r
19428 6f 75 6e 64 20 75 70 2e 20 49 6e 20 74 68 69 73  ound up. In this
19429 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c 20 74   case the call t
1942a 6f 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63  o sqlite3OsTrunc
1942b 61 74 65 28 29 20 62 65 6c 6f 77 20 77 69 6c 6c  ate() below will
1942c 0a 20 20 20 20 20 20 2a 2a 20 66 69 78 20 74 68  .      ** fix th
1942d 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69  e size of the fi
1942e 6c 65 2e 20 48 6f 77 65 76 65 72 20 69 74 20 69  le. However it i
1942f 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 63  s important to c
19430 61 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c  all.      ** sql
19431 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61 74  ite3PagerTruncat
19432 65 49 6d 61 67 65 28 29 20 68 65 72 65 20 73 6f  eImage() here so
19433 20 74 68 61 74 20 61 6e 79 20 70 61 67 65 73 20   that any pages 
19434 69 6e 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a  in the .      **
19435 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 66 69 6c   destination fil
19436 65 20 74 68 61 74 20 6c 69 65 20 62 65 79 6f 6e  e that lie beyon
19437 64 20 74 68 65 20 6e 44 65 73 74 54 72 75 6e 63  d the nDestTrunc
19438 61 74 65 20 70 61 67 65 20 6d 61 72 6b 20 61 72  ate page mark ar
19439 65 0a 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e  e.      ** journ
1943a 61 6c 6c 65 64 20 62 79 20 50 61 67 65 72 43 6f  alled by PagerCo
1943b 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29 20 62  mmitPhaseOne() b
1943c 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20 64  efore they are d
1943d 65 73 74 72 6f 79 65 64 0a 20 20 20 20 20 20 2a  estroyed.      *
1943e 2a 20 62 79 20 74 68 65 20 66 69 6c 65 20 74 72  * by the file tr
1943f 75 6e 63 61 74 69 6f 6e 2e 0a 20 20 20 20 20 20  uncation..      
19440 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 53 72  */.      if( nSr
19441 63 50 61 67 65 73 69 7a 65 3c 6e 44 65 73 74 50  cPagesize<nDestP
19442 61 67 65 73 69 7a 65 20 29 7b 0a 20 20 20 20 20  agesize ){.     
19443 20 20 20 69 6e 74 20 72 61 74 69 6f 20 3d 20 6e     int ratio = n
19444 44 65 73 74 50 61 67 65 73 69 7a 65 2f 6e 53 72  DestPagesize/nSr
19445 63 50 61 67 65 73 69 7a 65 3b 0a 20 20 20 20 20  cPagesize;.     
19446 20 20 20 6e 44 65 73 74 54 72 75 6e 63 61 74 65     nDestTruncate
19447 20 3d 20 28 6e 53 72 63 50 61 67 65 2b 72 61 74   = (nSrcPage+rat
19448 69 6f 2d 31 29 2f 72 61 74 69 6f 3b 0a 20 20 20  io-1)/ratio;.   
19449 20 20 20 20 20 69 66 28 20 6e 44 65 73 74 54 72       if( nDestTr
1944a 75 6e 63 61 74 65 3d 3d 28 69 6e 74 29 50 45 4e  uncate==(int)PEN
1944b 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
1944c 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 20 29 7b  ->pDest->pBt) ){
1944d 0a 20 20 20 20 20 20 20 20 20 20 6e 44 65 73 74  .          nDest
1944e 54 72 75 6e 63 61 74 65 2d 2d 3b 0a 20 20 20 20  Truncate--;.    
1944f 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
19450 65 7b 0a 20 20 20 20 20 20 20 20 6e 44 65 73 74  e{.        nDest
19451 54 72 75 6e 63 61 74 65 20 3d 20 6e 53 72 63 50  Truncate = nSrcP
19452 61 67 65 20 2a 20 28 6e 53 72 63 50 61 67 65 73  age * (nSrcPages
19453 69 7a 65 2f 6e 44 65 73 74 50 61 67 65 73 69 7a  ize/nDestPagesiz
19454 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  e);.      }.    
19455 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 54 72    sqlite3PagerTr
19456 75 6e 63 61 74 65 49 6d 61 67 65 28 70 44 65 73  uncateImage(pDes
19457 74 50 61 67 65 72 2c 20 6e 44 65 73 74 54 72 75  tPager, nDestTru
19458 6e 63 61 74 65 29 3b 0a 0a 20 20 20 20 20 20 69  ncate);..      i
19459 66 28 20 6e 53 72 63 50 61 67 65 73 69 7a 65 3c  f( nSrcPagesize<
1945a 6e 44 65 73 74 50 61 67 65 73 69 7a 65 20 29 7b  nDestPagesize ){
1945b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74  .        /* If t
1945c 68 65 20 73 6f 75 72 63 65 20 70 61 67 65 2d 73  he source page-s
1945d 69 7a 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 74  ize is smaller t
1945e 68 61 6e 20 74 68 65 20 64 65 73 74 69 6e 61 74  han the destinat
1945f 69 6f 6e 20 70 61 67 65 2d 73 69 7a 65 2c 0a 20  ion page-size,. 
19460 20 20 20 20 20 20 20 2a 2a 20 74 77 6f 20 65 78         ** two ex
19461 74 72 61 20 74 68 69 6e 67 73 20 6d 61 79 20 6e  tra things may n
19462 65 65 64 20 74 6f 20 68 61 70 70 65 6e 3a 0a 20  eed to happen:. 
19463 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
19464 20 20 2a 2a 20 20 20 2a 20 54 68 65 20 64 65 73    **   * The des
19465 74 69 6e 61 74 69 6f 6e 20 6d 61 79 20 6e 65 65  tination may nee
19466 64 20 74 6f 20 62 65 20 74 72 75 6e 63 61 74 65  d to be truncate
19467 64 2c 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a  d, and.        *
19468 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 2a  *.        **   *
19469 20 44 61 74 61 20 73 74 6f 72 65 64 20 6f 6e 20   Data stored on 
1946a 74 68 65 20 70 61 67 65 73 20 69 6d 6d 65 64 69  the pages immedi
1946b 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20  ately following 
1946c 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  the .        ** 
1946d 20 20 20 20 70 65 6e 64 69 6e 67 2d 62 79 74 65      pending-byte
1946e 20 70 61 67 65 20 69 6e 20 74 68 65 20 73 6f 75   page in the sou
1946f 72 63 65 20 64 61 74 61 62 61 73 65 20 6d 61 79  rce database may
19470 20 6e 65 65 64 20 74 6f 20 62 65 0a 20 20 20 20   need to be.    
19471 20 20 20 20 2a 2a 20 20 20 20 20 63 6f 70 69 65      **     copie
19472 64 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74 69  d into the desti
19473 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65 2e  nation database.
19474 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
19475 20 20 20 20 63 6f 6e 73 74 20 69 36 34 20 69 53      const i64 iS
19476 69 7a 65 20 3d 20 28 69 36 34 29 6e 53 72 63 50  ize = (i64)nSrcP
19477 61 67 65 73 69 7a 65 20 2a 20 28 69 36 34 29 6e  agesize * (i64)n
19478 53 72 63 50 61 67 65 3b 0a 20 20 20 20 20 20 20  SrcPage;.       
19479 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 20   sqlite3_file * 
1947a 63 6f 6e 73 74 20 70 46 69 6c 65 20 3d 20 73 71  const pFile = sq
1947b 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 70  lite3PagerFile(p
1947c 44 65 73 74 50 61 67 65 72 29 3b 0a 0a 20 20 20  DestPager);..   
1947d 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69       assert( pFi
1947e 6c 65 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  le );.        as
1947f 73 65 72 74 28 20 28 69 36 34 29 6e 44 65 73 74  sert( (i64)nDest
19480 54 72 75 6e 63 61 74 65 2a 28 69 36 34 29 6e 44  Truncate*(i64)nD
19481 65 73 74 50 61 67 65 73 69 7a 65 20 3e 3d 20 69  estPagesize >= i
19482 53 69 7a 65 20 7c 7c 20 28 0a 20 20 20 20 20 20  Size || (.      
19483 20 20 20 20 20 20 20 20 6e 44 65 73 74 54 72 75          nDestTru
19484 6e 63 61 74 65 3d 3d 28 69 6e 74 29 28 50 45 4e  ncate==(int)(PEN
19485 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
19486 2d 3e 70 44 65 73 74 2d 3e 70 42 74 29 2d 31 29  ->pDest->pBt)-1)
19487 0a 20 20 20 20 20 20 20 20 20 20 20 26 26 20 69  .           && i
19488 53 69 7a 65 3e 3d 50 45 4e 44 49 4e 47 5f 42 59  Size>=PENDING_BY
19489 54 45 20 26 26 20 69 53 69 7a 65 3c 3d 50 45 4e  TE && iSize<=PEN
1948a 44 49 4e 47 5f 42 59 54 45 2b 6e 44 65 73 74 50  DING_BYTE+nDestP
1948b 61 67 65 73 69 7a 65 0a 20 20 20 20 20 20 20 20  agesize.        
1948c 29 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ));.        if( 
1948d 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
1948e 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d   sqlite3PagerCom
1948f 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 44 65 73  mitPhaseOne(pDes
19490 74 50 61 67 65 72 2c 20 30 2c 20 31 29 29 0a 20  tPager, 0, 1)). 
19491 20 20 20 20 20 20 20 20 26 26 20 53 51 4c 49 54          && SQLIT
19492 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 62 61 63 6b  E_OK==(rc = back
19493 75 70 54 72 75 6e 63 61 74 65 46 69 6c 65 28 70  upTruncateFile(p
19494 46 69 6c 65 2c 20 69 53 69 7a 65 29 29 0a 20 20  File, iSize)).  
19495 20 20 20 20 20 20 20 26 26 20 53 51 4c 49 54 45         && SQLITE
19496 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK==(rc = sqlit
19497 65 33 50 61 67 65 72 53 79 6e 63 28 70 44 65 73  e3PagerSync(pDes
19498 74 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 20  tPager)).       
19499 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 36   ){.          i6
1949a 34 20 69 4f 66 66 3b 0a 20 20 20 20 20 20 20 20  4 iOff;.        
1949b 20 20 69 36 34 20 69 45 6e 64 20 3d 20 4d 49 4e    i64 iEnd = MIN
1949c 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 2b 20  (PENDING_BYTE + 
1949d 6e 44 65 73 74 50 61 67 65 73 69 7a 65 2c 20 69  nDestPagesize, i
1949e 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Size);.         
1949f 20 66 6f 72 28 0a 20 20 20 20 20 20 20 20 20 20   for(.          
194a0 20 20 69 4f 66 66 3d 50 45 4e 44 49 4e 47 5f 42    iOff=PENDING_B
194a1 59 54 45 2b 6e 53 72 63 50 61 67 65 73 69 7a 65  YTE+nSrcPagesize
194a2 3b 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  ; .            r
194a3 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
194a4 69 4f 66 66 3c 69 45 6e 64 3b 20 0a 20 20 20 20  iOff<iEnd; .    
194a5 20 20 20 20 20 20 20 20 69 4f 66 66 2b 3d 6e 53          iOff+=nS
194a6 72 63 50 61 67 65 73 69 7a 65 0a 20 20 20 20 20  rcPagesize.     
194a7 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
194a8 20 20 20 20 50 67 48 64 72 20 2a 70 53 72 63 50      PgHdr *pSrcP
194a9 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  g = 0;.         
194aa 20 20 20 63 6f 6e 73 74 20 50 67 6e 6f 20 69 53     const Pgno iS
194ab 72 63 50 67 20 3d 20 28 50 67 6e 6f 29 28 28 69  rcPg = (Pgno)((i
194ac 4f 66 66 2f 6e 53 72 63 50 61 67 65 73 69 7a 65  Off/nSrcPagesize
194ad 29 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20  )+1);.          
194ae 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
194af 67 65 72 47 65 74 28 70 53 72 63 50 61 67 65 72  gerGet(pSrcPager
194b0 2c 20 69 53 72 63 50 67 2c 20 26 70 53 72 63 50  , iSrcPg, &pSrcP
194b1 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  g);.            
194b2 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
194b3 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  K ){.           
194b4 20 20 20 75 38 20 2a 7a 44 61 74 61 20 3d 20 73     u8 *zData = s
194b5 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61  qlite3PagerGetDa
194b6 74 61 28 70 53 72 63 50 67 29 3b 0a 20 20 20 20  ta(pSrcPg);.    
194b7 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73            rc = s
194b8 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 46  qlite3OsWrite(pF
194b9 69 6c 65 2c 20 7a 44 61 74 61 2c 20 6e 53 72 63  ile, zData, nSrc
194ba 50 61 67 65 73 69 7a 65 2c 20 69 4f 66 66 29 3b  Pagesize, iOff);
194bb 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
194bc 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
194bd 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 53 72  e3PagerUnref(pSr
194be 63 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20  cPg);.          
194bf 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
194c0 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
194c1 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
194c2 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  erCommitPhaseOne
194c3 28 70 44 65 73 74 50 61 67 65 72 2c 20 30 2c 20  (pDestPager, 0, 
194c4 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20  0);.      }.  . 
194c5 20 20 20 20 20 2f 2a 20 46 69 6e 69 73 68 20 63       /* Finish c
194c6 6f 6d 6d 69 74 74 69 6e 67 20 74 68 65 20 74 72  ommitting the tr
194c7 61 6e 73 61 63 74 69 6f 6e 20 74 6f 20 74 68 65  ansaction to the
194c8 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74   destination dat
194c9 61 62 61 73 65 2e 20 2a 2f 0a 20 20 20 20 20 20  abase. */.      
194ca 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72  if( SQLITE_OK==r
194cb 63 0a 20 20 20 20 20 20 20 26 26 20 53 51 4c 49  c.       && SQLI
194cc 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c  TE_OK==(rc = sql
194cd 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50  ite3BtreeCommitP
194ce 68 61 73 65 54 77 6f 28 70 2d 3e 70 44 65 73 74  haseTwo(p->pDest
194cf 29 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  )).      ){.    
194d0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
194d1 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20  DONE;.      }.  
194d2 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 49 66    }.  .    /* If
194d3 20 62 43 6c 6f 73 65 54 72 61 6e 73 20 69 73 20   bCloseTrans is 
194d4 74 72 75 65 2c 20 74 68 65 6e 20 74 68 69 73 20  true, then this 
194d5 66 75 6e 63 74 69 6f 6e 20 6f 70 65 6e 65 64 20  function opened 
194d6 61 20 72 65 61 64 20 74 72 61 6e 73 61 63 74 69  a read transacti
194d7 6f 6e 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68 65  on.    ** on the
194d8 20 73 6f 75 72 63 65 20 64 61 74 61 62 61 73 65   source database
194d9 2e 20 43 6c 6f 73 65 20 74 68 65 20 72 65 61 64  . Close the read
194da 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 68 65 72   transaction her
194db 65 2e 20 54 68 65 72 65 20 69 73 0a 20 20 20 20  e. There is.    
194dc 2a 2a 20 6e 6f 20 6e 65 65 64 20 74 6f 20 63 68  ** no need to ch
194dd 65 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 76  eck the return v
194de 61 6c 75 65 73 20 6f 66 20 74 68 65 20 62 74 72  alues of the btr
194df 65 65 20 6d 65 74 68 6f 64 73 20 68 65 72 65 2c  ee methods here,
194e0 20 61 73 0a 20 20 20 20 2a 2a 20 22 63 6f 6d 6d   as.    ** "comm
194e1 69 74 74 69 6e 67 22 20 61 20 72 65 61 64 2d 6f  itting" a read-o
194e2 6e 6c 79 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  nly transaction 
194e3 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 0a 20 20 20  cannot fail..   
194e4 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 43 6c 6f   */.    if( bClo
194e5 73 65 54 72 61 6e 73 20 29 7b 0a 20 20 20 20 20  seTrans ){.     
194e6 20 54 45 53 54 4f 4e 4c 59 28 20 69 6e 74 20 72   TESTONLY( int r
194e7 63 32 20 29 3b 0a 20 20 20 20 20 20 54 45 53 54  c2 );.      TEST
194e8 4f 4e 4c 59 28 20 72 63 32 20 20 3d 20 29 20 73  ONLY( rc2  = ) s
194e9 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69  qlite3BtreeCommi
194ea 74 50 68 61 73 65 4f 6e 65 28 70 2d 3e 70 53 72  tPhaseOne(p->pSr
194eb 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 54 45 53  c, 0);.      TES
194ec 54 4f 4e 4c 59 28 20 72 63 32 20 7c 3d 20 29 20  TONLY( rc2 |= ) 
194ed 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
194ee 69 74 50 68 61 73 65 54 77 6f 28 70 2d 3e 70 53  itPhaseTwo(p->pS
194ef 72 63 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  rc);.      asser
194f0 74 28 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 4f  t( rc2==SQLITE_O
194f1 4b 20 29 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20  K );.    }.  .  
194f2 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20    p->rc = rc;.  
194f3 7d 0a 20 20 69 66 28 20 70 2d 3e 70 44 65 73 74  }.  if( p->pDest
194f4 44 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  Db ){.    sqlite
194f5 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d  3_mutex_leave(p-
194f6 3e 70 44 65 73 74 44 62 2d 3e 6d 75 74 65 78 29  >pDestDb->mutex)
194f7 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42  ;.  }.  sqlite3B
194f8 74 72 65 65 4c 65 61 76 65 28 70 2d 3e 70 53 72  treeLeave(p->pSr
194f9 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  c);.  sqlite3_mu
194fa 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 53 72  tex_leave(p->pSr
194fb 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  cDb->mutex);.  r
194fc 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
194fd 2a 2a 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 72  ** Release all r
194fe 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69 61  esources associa
194ff 74 65 64 20 77 69 74 68 20 61 6e 20 73 71 6c 69  ted with an sqli
19500 74 65 33 5f 62 61 63 6b 75 70 2a 20 68 61 6e 64  te3_backup* hand
19501 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  le..*/.SQLITE_AP
19502 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 61  I int sqlite3_ba
19503 63 6b 75 70 5f 66 69 6e 69 73 68 28 73 71 6c 69  ckup_finish(sqli
19504 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 29 7b 0a  te3_backup *p){.
19505 20 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70    sqlite3_backup
19506 20 2a 2a 70 70 3b 20 20 20 20 20 20 20 20 20 20   **pp;          
19507 20 20 20 20 20 20 20 2f 2a 20 50 74 72 20 74 6f         /* Ptr to
19508 20 68 65 61 64 20 6f 66 20 70 61 67 65 72 73 20   head of pagers 
19509 62 61 63 6b 75 70 20 6c 69 73 74 20 2a 2f 0a 20  backup list */. 
1950a 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
1950b 6d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20 20  mutex;          
1950c 20 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74        /* Mutex t
1950d 6f 20 70 72 6f 74 65 63 74 20 73 6f 75 72 63 65  o protect source
1950e 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69   database */.  i
1950f 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
19510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19511 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20      /* Value to 
19512 72 65 74 75 72 6e 20 2a 2f 0a 0a 20 20 2f 2a 20  return */..  /* 
19513 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78 65  Enter the mutexe
19514 73 20 2a 2f 0a 20 20 69 66 28 20 70 3d 3d 30 20  s */.  if( p==0 
19515 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
19516 4f 4b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  OK;.  sqlite3_mu
19517 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 53 72  tex_enter(p->pSr
19518 63 44 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73  cDb->mutex);.  s
19519 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1951a 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 6d 75 74  (p->pSrc);.  mut
1951b 65 78 20 3d 20 70 2d 3e 70 53 72 63 44 62 2d 3e  ex = p->pSrcDb->
1951c 6d 75 74 65 78 3b 0a 20 20 69 66 28 20 70 2d 3e  mutex;.  if( p->
1951d 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20 20 73  pDestDb ){.    s
1951e 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
1951f 65 72 28 70 2d 3e 70 44 65 73 74 44 62 2d 3e 6d  er(p->pDestDb->m
19520 75 74 65 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  utex);.  }..  /*
19521 20 44 65 74 61 63 68 20 74 68 69 73 20 62 61 63   Detach this bac
19522 6b 75 70 20 66 72 6f 6d 20 74 68 65 20 73 6f 75  kup from the sou
19523 72 63 65 20 70 61 67 65 72 2e 20 2a 2f 0a 20 20  rce pager. */.  
19524 69 66 28 20 70 2d 3e 70 44 65 73 74 44 62 20 29  if( p->pDestDb )
19525 7b 0a 20 20 20 20 70 2d 3e 70 53 72 63 2d 3e 6e  {.    p->pSrc->n
19526 42 61 63 6b 75 70 2d 2d 3b 0a 20 20 7d 0a 20 20  Backup--;.  }.  
19527 69 66 28 20 70 2d 3e 69 73 41 74 74 61 63 68 65  if( p->isAttache
19528 64 20 29 7b 0a 20 20 20 20 70 70 20 3d 20 73 71  d ){.    pp = sq
19529 6c 69 74 65 33 50 61 67 65 72 42 61 63 6b 75 70  lite3PagerBackup
1952a 50 74 72 28 73 71 6c 69 74 65 33 42 74 72 65 65  Ptr(sqlite3Btree
1952b 50 61 67 65 72 28 70 2d 3e 70 53 72 63 29 29 3b  Pager(p->pSrc));
1952c 0a 20 20 20 20 77 68 69 6c 65 28 20 2a 70 70 21  .    while( *pp!
1952d 3d 70 20 29 7b 0a 20 20 20 20 20 20 70 70 20 3d  =p ){.      pp =
1952e 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 3b 0a   &(*pp)->pNext;.
1952f 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 20 3d 20      }.    *pp = 
19530 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20  p->pNext;.  }.. 
19531 20 2f 2a 20 49 66 20 61 20 74 72 61 6e 73 61 63   /* If a transac
19532 74 69 6f 6e 20 69 73 20 73 74 69 6c 6c 20 6f 70  tion is still op
19533 65 6e 20 6f 6e 20 74 68 65 20 42 74 72 65 65 2c  en on the Btree,
19534 20 72 6f 6c 6c 20 69 74 20 62 61 63 6b 2e 20 2a   roll it back. *
19535 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  /.  sqlite3Btree
19536 52 6f 6c 6c 62 61 63 6b 28 70 2d 3e 70 44 65 73  Rollback(p->pDes
19537 74 29 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68  t);..  /* Set th
19538 65 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20  e error code of 
19539 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
1953a 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e  database handle.
1953b 20 2a 2f 0a 20 20 72 63 20 3d 20 28 70 2d 3e 72   */.  rc = (p->r
1953c 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 29 20  c==SQLITE_DONE) 
1953d 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 70 2d  ? SQLITE_OK : p-
1953e 3e 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 45 72  >rc;.  sqlite3Er
1953f 72 6f 72 28 70 2d 3e 70 44 65 73 74 44 62 2c 20  ror(p->pDestDb, 
19540 72 63 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 45 78  rc, 0);..  /* Ex
19541 69 74 20 74 68 65 20 6d 75 74 65 78 65 73 20 61  it the mutexes a
19542 6e 64 20 66 72 65 65 20 74 68 65 20 62 61 63 6b  nd free the back
19543 75 70 20 63 6f 6e 74 65 78 74 20 73 74 72 75 63  up context struc
19544 74 75 72 65 2e 20 2a 2f 0a 20 20 69 66 28 20 70  ture. */.  if( p
19545 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20 20 20  ->pDestDb ){.   
19546 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
19547 65 61 76 65 28 70 2d 3e 70 44 65 73 74 44 62 2d  eave(p->pDestDb-
19548 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 73  >mutex);.  }.  s
19549 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
1954a 28 70 2d 3e 70 53 72 63 29 3b 0a 20 20 69 66 28  (p->pSrc);.  if(
1954b 20 70 2d 3e 70 44 65 73 74 44 62 20 29 7b 0a 20   p->pDestDb ){. 
1954c 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
1954d 70 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  p);.  }.  sqlite
1954e 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75  3_mutex_leave(mu
1954f 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
19550 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  c;.}../*.** Retu
19551 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
19552 20 70 61 67 65 73 20 73 74 69 6c 6c 20 74 6f 20   pages still to 
19553 62 65 20 62 61 63 6b 65 64 20 75 70 20 61 73 20  be backed up as 
19554 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  of the most rece
19555 6e 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71  nt.** call to sq
19556 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
19557 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  p()..*/.SQLITE_A
19558 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
19559 61 63 6b 75 70 5f 72 65 6d 61 69 6e 69 6e 67 28  ackup_remaining(
1955a 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20 2a  sqlite3_backup *
1955b 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e  p){.  return p->
1955c 6e 52 65 6d 61 69 6e 69 6e 67 3b 0a 7d 0a 0a 2f  nRemaining;.}../
1955d 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1955e 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
1955f 70 61 67 65 73 20 69 6e 20 74 68 65 20 73 6f 75  pages in the sou
19560 72 63 65 20 64 61 74 61 62 61 73 65 20 61 73 20  rce database as 
19561 6f 66 20 74 68 65 20 6d 6f 73 74 20 0a 2a 2a 20  of the most .** 
19562 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73  recent call to s
19563 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74  qlite3_backup_st
19564 65 70 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ep()..*/.SQLITE_
19565 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
19566 62 61 63 6b 75 70 5f 70 61 67 65 63 6f 75 6e 74  backup_pagecount
19567 28 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 20  (sqlite3_backup 
19568 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d  *p){.  return p-
19569 3e 6e 50 61 67 65 63 6f 75 6e 74 3b 0a 7d 0a 0a  >nPagecount;.}..
1956a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
1956b 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66  ion is called af
1956c 74 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ter the contents
1956d 20 6f 66 20 70 61 67 65 20 69 50 61 67 65 20 6f   of page iPage o
1956e 66 20 74 68 65 0a 2a 2a 20 73 6f 75 72 63 65 20  f the.** source 
1956f 64 61 74 61 62 61 73 65 20 68 61 76 65 20 62 65  database have be
19570 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 49 66 20  en modified. If 
19571 70 61 67 65 20 69 50 61 67 65 20 68 61 73 20 61  page iPage has a
19572 6c 72 65 61 64 79 20 62 65 65 6e 20 0a 2a 2a 20  lready been .** 
19573 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20  copied into the 
19574 64 65 73 74 69 6e 61 74 69 6f 6e 20 64 61 74 61  destination data
19575 62 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 64  base, then the d
19576 61 74 61 20 77 72 69 74 74 65 6e 20 74 6f 20 74  ata written to t
19577 68 65 0a 2a 2a 20 64 65 73 74 69 6e 61 74 69 6f  he.** destinatio
19578 6e 20 69 73 20 6e 6f 77 20 69 6e 76 61 6c 69 64  n is now invalid
19579 61 74 65 64 2e 20 54 68 65 20 64 65 73 74 69 6e  ated. The destin
1957a 61 74 69 6f 6e 20 63 6f 70 79 20 6f 66 20 69 50  ation copy of iP
1957b 61 67 65 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20  age needs.** to 
1957c 62 65 20 75 70 64 61 74 65 64 20 77 69 74 68 20  be updated with 
1957d 74 68 65 20 6e 65 77 20 64 61 74 61 20 62 65 66  the new data bef
1957e 6f 72 65 20 74 68 65 20 62 61 63 6b 75 70 20 6f  ore the backup o
1957f 70 65 72 61 74 69 6f 6e 20 69 73 0a 2a 2a 20 63  peration is.** c
19580 6f 6d 70 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 49  omplete..**.** I
19581 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61  t is assumed tha
19582 74 20 74 68 65 20 6d 75 74 65 78 20 61 73 73 6f  t the mutex asso
19583 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
19584 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63 74 0a  BtShared object.
19585 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ** corresponding
19586 20 74 6f 20 74 68 65 20 73 6f 75 72 63 65 20 64   to the source d
19587 61 74 61 62 61 73 65 20 69 73 20 68 65 6c 64 20  atabase is held 
19588 77 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  when this functi
19589 6f 6e 20 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e  on is.** called.
1958a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1958b 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42  TE void sqlite3B
1958c 61 63 6b 75 70 55 70 64 61 74 65 28 73 71 6c 69  ackupUpdate(sqli
1958d 74 65 33 5f 62 61 63 6b 75 70 20 2a 70 42 61 63  te3_backup *pBac
1958e 6b 75 70 2c 20 50 67 6e 6f 20 69 50 61 67 65 2c  kup, Pgno iPage,
1958f 20 63 6f 6e 73 74 20 75 38 20 2a 61 44 61 74 61   const u8 *aData
19590 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63  ){.  sqlite3_bac
19591 6b 75 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20  kup *p;         
19592 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
19593 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a  rator variable *
19594 2f 0a 20 20 66 6f 72 28 70 3d 70 42 61 63 6b 75  /.  for(p=pBacku
19595 70 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  p; p; p=p->pNext
19596 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  ){.    assert( s
19597 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
19598 64 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e  d(p->pSrc->pBt->
19599 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 69 66  mutex) );.    if
1959a 28 20 21 69 73 46 61 74 61 6c 45 72 72 6f 72 28  ( !isFatalError(
1959b 70 2d 3e 72 63 29 20 26 26 20 69 50 61 67 65 3c  p->rc) && iPage<
1959c 70 2d 3e 69 4e 65 78 74 20 29 7b 0a 20 20 20 20  p->iNext ){.    
1959d 20 20 2f 2a 20 54 68 65 20 62 61 63 6b 75 70 20    /* The backup 
1959e 70 72 6f 63 65 73 73 20 70 20 68 61 73 20 61 6c  process p has al
1959f 72 65 61 64 79 20 63 6f 70 69 65 64 20 70 61 67  ready copied pag
195a0 65 20 69 50 61 67 65 2e 20 42 75 74 20 6e 6f 77  e iPage. But now
195a1 20 69 74 0a 20 20 20 20 20 20 2a 2a 20 68 61 73   it.      ** has
195a2 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20 62   been modified b
195a3 79 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  y a transaction 
195a4 6f 6e 20 74 68 65 20 73 6f 75 72 63 65 20 70 61  on the source pa
195a5 67 65 72 2e 20 43 6f 70 79 0a 20 20 20 20 20 20  ger. Copy.      
195a6 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61 20  ** the new data 
195a7 69 6e 74 6f 20 74 68 65 20 62 61 63 6b 75 70 2e  into the backup.
195a8 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
195a9 69 6e 74 20 72 63 20 3d 20 62 61 63 6b 75 70 4f  int rc = backupO
195aa 6e 65 50 61 67 65 28 70 2c 20 69 50 61 67 65 2c  nePage(p, iPage,
195ab 20 61 44 61 74 61 29 3b 0a 20 20 20 20 20 20 61   aData);.      a
195ac 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54  ssert( rc!=SQLIT
195ad 45 5f 42 55 53 59 20 26 26 20 72 63 21 3d 53 51  E_BUSY && rc!=SQ
195ae 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29 3b 0a 20  LITE_LOCKED );. 
195af 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
195b0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
195b1 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20    p->rc = rc;.  
195b2 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
195b3 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 74 61 72 74  }../*.** Restart
195b4 20 74 68 65 20 62 61 63 6b 75 70 20 70 72 6f 63   the backup proc
195b5 65 73 73 2e 20 54 68 69 73 20 69 73 20 63 61 6c  ess. This is cal
195b6 6c 65 64 20 77 68 65 6e 20 74 68 65 20 70 61 67  led when the pag
195b7 65 72 20 6c 61 79 65 72 0a 2a 2a 20 64 65 74 65  er layer.** dete
195b8 63 74 73 20 74 68 61 74 20 74 68 65 20 64 61 74  cts that the dat
195b9 61 62 61 73 65 20 68 61 73 20 62 65 65 6e 20 6d  abase has been m
195ba 6f 64 69 66 69 65 64 20 62 79 20 61 6e 20 65 78  odified by an ex
195bb 74 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 0a  ternal database.
195bc 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 49  ** connection. I
195bd 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 72  n this case ther
195be 65 20 69 73 20 6e 6f 20 77 61 79 20 6f 66 20 6b  e is no way of k
195bf 6e 6f 77 69 6e 67 20 77 68 69 63 68 20 6f 66 20  nowing which of 
195c0 74 68 65 0a 2a 2a 20 70 61 67 65 73 20 74 68 61  the.** pages tha
195c1 74 20 68 61 76 65 20 62 65 65 6e 20 63 6f 70 69  t have been copi
195c2 65 64 20 69 6e 74 6f 20 74 68 65 20 64 65 73 74  ed into the dest
195c3 69 6e 61 74 69 6f 6e 20 64 61 74 61 62 61 73 65  ination database
195c4 20 61 72 65 20 73 74 69 6c 6c 20 0a 2a 2a 20 76   are still .** v
195c5 61 6c 69 64 20 61 6e 64 20 77 68 69 63 68 20 61  alid and which a
195c6 72 65 20 6e 6f 74 2c 20 73 6f 20 74 68 65 20 65  re not, so the e
195c7 6e 74 69 72 65 20 70 72 6f 63 65 73 73 20 6e 65  ntire process ne
195c8 65 64 73 20 74 6f 20 62 65 20 72 65 73 74 61 72  eds to be restar
195c9 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73  ted..**.** It is
195ca 20 61 73 73 75 6d 65 64 20 74 68 61 74 20 74 68   assumed that th
195cb 65 20 6d 75 74 65 78 20 61 73 73 6f 63 69 61 74  e mutex associat
195cc 65 64 20 77 69 74 68 20 74 68 65 20 42 74 53 68  ed with the BtSh
195cd 61 72 65 64 20 6f 62 6a 65 63 74 0a 2a 2a 20 63  ared object.** c
195ce 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20  orresponding to 
195cf 74 68 65 20 73 6f 75 72 63 65 20 64 61 74 61 62  the source datab
195d0 61 73 65 20 69 73 20 68 65 6c 64 20 77 68 65 6e  ase is held when
195d1 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
195d2 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a  s.** called..*/.
195d3 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
195d4 6f 69 64 20 73 71 6c 69 74 65 33 42 61 63 6b 75  oid sqlite3Backu
195d5 70 52 65 73 74 61 72 74 28 73 71 6c 69 74 65 33  pRestart(sqlite3
195d6 5f 62 61 63 6b 75 70 20 2a 70 42 61 63 6b 75 70  _backup *pBackup
195d7 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 61 63  ){.  sqlite3_bac
195d8 6b 75 70 20 2a 70 3b 20 20 20 20 20 20 20 20 20  kup *p;         
195d9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
195da 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a  rator variable *
195db 2f 0a 20 20 66 6f 72 28 70 3d 70 42 61 63 6b 75  /.  for(p=pBacku
195dc 70 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  p; p; p=p->pNext
195dd 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  ){.    assert( s
195de 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
195df 64 28 70 2d 3e 70 53 72 63 2d 3e 70 42 74 2d 3e  d(p->pSrc->pBt->
195e0 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 70 2d  mutex) );.    p-
195e1 3e 69 4e 65 78 74 20 3d 20 31 3b 0a 20 20 7d 0a  >iNext = 1;.  }.
195e2 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
195e3 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 0a 2f 2a  E_OMIT_VACUUM./*
195e4 0a 2a 2a 20 43 6f 70 79 20 74 68 65 20 63 6f 6d  .** Copy the com
195e5 70 6c 65 74 65 20 63 6f 6e 74 65 6e 74 20 6f 66  plete content of
195e6 20 70 42 74 46 72 6f 6d 20 69 6e 74 6f 20 70 42   pBtFrom into pB
195e7 74 54 6f 2e 20 20 41 20 74 72 61 6e 73 61 63 74  tTo.  A transact
195e8 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 61  ion.** must be a
195e9 63 74 69 76 65 20 66 6f 72 20 62 6f 74 68 20 66  ctive for both f
195ea 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  iles..**.** The 
195eb 73 69 7a 65 20 6f 66 20 66 69 6c 65 20 70 54 6f  size of file pTo
195ec 20 6d 61 79 20 62 65 20 72 65 64 75 63 65 64 20   may be reduced 
195ed 62 79 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f  by this operatio
195ee 6e 2e 20 49 66 20 61 6e 79 74 68 69 6e 67 20 0a  n. If anything .
195ef 2a 2a 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 74  ** goes wrong, t
195f0 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  he transaction o
195f1 6e 20 70 54 6f 20 69 73 20 72 6f 6c 6c 65 64 20  n pTo is rolled 
195f2 62 61 63 6b 2e 20 49 66 20 73 75 63 63 65 73 73  back. If success
195f3 66 75 6c 2c 20 74 68 65 20 0a 2a 2a 20 74 72 61  ful, the .** tra
195f4 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d  nsaction is comm
195f5 69 74 74 65 64 20 62 65 66 6f 72 65 20 72 65 74  itted before ret
195f6 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54  urning..*/.SQLIT
195f7 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
195f8 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79 46 69  lite3BtreeCopyFi
195f9 6c 65 28 42 74 72 65 65 20 2a 70 54 6f 2c 20 42  le(Btree *pTo, B
195fa 74 72 65 65 20 2a 70 46 72 6f 6d 29 7b 0a 20 20  tree *pFrom){.  
195fb 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
195fc 33 5f 62 61 63 6b 75 70 20 62 3b 0a 20 20 73 71  3_backup b;.  sq
195fd 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
195fe 70 54 6f 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  pTo);.  sqlite3B
195ff 74 72 65 65 45 6e 74 65 72 28 70 46 72 6f 6d 29  treeEnter(pFrom)
19600 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 75 70 20 61  ;..  /* Set up a
19601 6e 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70  n sqlite3_backup
19602 20 6f 62 6a 65 63 74 2e 20 73 71 6c 69 74 65 33   object. sqlite3
19603 5f 62 61 63 6b 75 70 2e 70 44 65 73 74 44 62 20  _backup.pDestDb 
19604 6d 75 73 74 20 62 65 20 73 65 74 0a 20 20 2a 2a  must be set.  **
19605 20 74 6f 20 30 2e 20 54 68 69 73 20 69 73 20 75   to 0. This is u
19606 73 65 64 20 62 79 20 74 68 65 20 69 6d 70 6c 65  sed by the imple
19607 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 73 71  mentations of sq
19608 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 73 74 65  lite3_backup_ste
19609 70 28 29 0a 20 20 2a 2a 20 61 6e 64 20 73 71 6c  p().  ** and sql
1960a 69 74 65 33 5f 62 61 63 6b 75 70 5f 66 69 6e 69  ite3_backup_fini
1960b 73 68 28 29 20 74 6f 20 64 65 74 65 63 74 20 74  sh() to detect t
1960c 68 61 74 20 74 68 65 79 20 61 72 65 20 62 65 69  hat they are bei
1960d 6e 67 20 63 61 6c 6c 65 64 0a 20 20 2a 2a 20 66  ng called.  ** f
1960e 72 6f 6d 20 74 68 69 73 20 66 75 6e 63 74 69 6f  rom this functio
1960f 6e 2c 20 6e 6f 74 20 64 69 72 65 63 74 6c 79 20  n, not directly 
19610 62 79 20 74 68 65 20 75 73 65 72 2e 0a 20 20 2a  by the user..  *
19611 2f 0a 20 20 6d 65 6d 73 65 74 28 26 62 2c 20 30  /.  memset(&b, 0
19612 2c 20 73 69 7a 65 6f 66 28 62 29 29 3b 0a 20 20  , sizeof(b));.  
19613 62 2e 70 53 72 63 44 62 20 3d 20 70 46 72 6f 6d  b.pSrcDb = pFrom
19614 2d 3e 64 62 3b 0a 20 20 62 2e 70 53 72 63 20 3d  ->db;.  b.pSrc =
19615 20 70 46 72 6f 6d 3b 0a 20 20 62 2e 70 44 65 73   pFrom;.  b.pDes
19616 74 20 3d 20 70 54 6f 3b 0a 20 20 62 2e 69 4e 65  t = pTo;.  b.iNe
19617 78 74 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 30 78  xt = 1;..  /* 0x
19618 37 46 46 46 46 46 46 46 20 69 73 20 74 68 65 20  7FFFFFFF is the 
19619 68 61 72 64 20 6c 69 6d 69 74 20 66 6f 72 20 74  hard limit for t
1961a 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
1961b 65 73 20 69 6e 20 61 20 64 61 74 61 62 61 73 65  es in a database
1961c 0a 20 20 2a 2a 20 66 69 6c 65 2e 20 42 79 20 70  .  ** file. By p
1961d 61 73 73 69 6e 67 20 74 68 69 73 20 61 73 20 74  assing this as t
1961e 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
1961f 65 73 20 74 6f 20 63 6f 70 79 20 74 6f 0a 20 20  es to copy to.  
19620 2a 2a 20 73 71 6c 69 74 65 33 5f 62 61 63 6b 75  ** sqlite3_backu
19621 70 5f 73 74 65 70 28 29 2c 20 77 65 20 63 61 6e  p_step(), we can
19622 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20   guarantee that 
19623 74 68 65 20 63 6f 70 79 20 66 69 6e 69 73 68 65  the copy finishe
19624 73 20 0a 20 20 2a 2a 20 77 69 74 68 69 6e 20 61  s .  ** within a
19625 20 73 69 6e 67 6c 65 20 63 61 6c 6c 20 28 75 6e   single call (un
19626 6c 65 73 73 20 61 6e 20 65 72 72 6f 72 20 6f 63  less an error oc
19627 63 75 72 73 29 2e 20 54 68 65 20 61 73 73 65 72  curs). The asser
19628 74 28 29 20 73 74 61 74 65 6d 65 6e 74 0a 20 20  t() statement.  
19629 2a 2a 20 63 68 65 63 6b 73 20 74 68 69 73 20 61  ** checks this a
1962a 73 73 75 6d 70 74 69 6f 6e 20 2d 20 28 70 2d 3e  ssumption - (p->
1962b 72 63 29 20 73 68 6f 75 6c 64 20 62 65 20 73 65  rc) should be se
1962c 74 20 74 6f 20 65 69 74 68 65 72 20 53 51 4c 49  t to either SQLI
1962d 54 45 5f 44 4f 4e 45 20 0a 20 20 2a 2a 20 6f 72  TE_DONE .  ** or
1962e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a   an error code..
1962f 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62    */.  sqlite3_b
19630 61 63 6b 75 70 5f 73 74 65 70 28 26 62 2c 20 30  ackup_step(&b, 0
19631 78 37 46 46 46 46 46 46 46 29 3b 0a 20 20 61 73  x7FFFFFFF);.  as
19632 73 65 72 74 28 20 62 2e 72 63 21 3d 53 51 4c 49  sert( b.rc!=SQLI
19633 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d 20  TE_OK );.  rc = 
19634 73 71 6c 69 74 65 33 5f 62 61 63 6b 75 70 5f 66  sqlite3_backup_f
19635 69 6e 69 73 68 28 26 62 29 3b 0a 20 20 69 66 28  inish(&b);.  if(
19636 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
19637 7b 0a 20 20 20 20 70 54 6f 2d 3e 70 42 74 2d 3e  {.    pTo->pBt->
19638 70 61 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20  pageSizeFixed = 
19639 30 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  0;.  }..  sqlite
1963a 33 42 74 72 65 65 4c 65 61 76 65 28 70 46 72 6f  3BtreeLeave(pFro
1963b 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  m);.  sqlite3Btr
1963c 65 65 4c 65 61 76 65 28 70 54 6f 29 3b 0a 20 20  eeLeave(pTo);.  
1963d 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e  return rc;.}.#en
1963e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1963f 49 54 5f 56 41 43 55 55 4d 20 2a 2f 0a 0a 2f 2a  IT_VACUUM */../*
19640 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
19641 64 20 6f 66 20 62 61 63 6b 75 70 2e 63 20 2a 2a  d of backup.c **
19642 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19643 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19644 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
19645 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
19646 67 69 6e 20 66 69 6c 65 20 76 64 62 65 6d 65 6d  gin file vdbemem
19647 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
19648 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19649 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1964a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 36 0a  .** 2004 May 26.
1964b 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
1964c 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
1964d 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
1964e 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
1964f 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
19650 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
19651 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
19652 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
19653 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
19654 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
19655 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
19656 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
19657 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
19658 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
19659 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
1965a 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
1965b 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
1965c 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
1965d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1965e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1965f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
19661 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
19662 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65  ontains code use
19663 20 74 6f 20 6d 61 6e 69 70 75 6c 61 74 65 20 22   to manipulate "
19664 4d 65 6d 22 20 73 74 72 75 63 74 75 72 65 2e 20  Mem" structure. 
19665 20 41 20 22 4d 65 6d 22 0a 2a 2a 20 73 74 6f 72   A "Mem".** stor
19666 65 73 20 61 20 73 69 6e 67 6c 65 20 76 61 6c 75  es a single valu
19667 65 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20  e in the VDBE.  
19668 4d 65 6d 20 69 73 20 61 6e 20 6f 70 61 71 75 65  Mem is an opaque
19669 20 73 74 72 75 63 74 75 72 65 20 76 69 73 69 62   structure visib
1966a 6c 65 0a 2a 2a 20 6f 6e 6c 79 20 77 69 74 68 69  le.** only withi
1966b 6e 20 74 68 65 20 56 44 42 45 2e 20 20 49 6e 74  n the VDBE.  Int
1966c 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65 73 20  erface routines 
1966d 72 65 66 65 72 20 74 6f 20 61 20 4d 65 6d 20 75  refer to a Mem u
1966e 73 69 6e 67 20 74 68 65 0a 2a 2a 20 6e 61 6d 65  sing the.** name
1966f 20 73 71 6c 69 74 65 5f 76 61 6c 75 65 0a 2a 2a   sqlite_value.**
19670 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 6d 65 6d  .** $Id: vdbemem
19671 2e 63 2c 76 20 31 2e 31 35 32 20 32 30 30 39 2f  .c,v 1.152 2009/
19672 30 37 2f 32 32 20 31 38 3a 30 37 3a 34 31 20 64  07/22 18:07:41 d
19673 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a  rh Exp $.*/../*.
19674 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 56  ** Call sqlite3V
19675 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62  dbeMemExpandBlob
19676 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70 6c 69  () on the suppli
19677 65 64 20 76 61 6c 75 65 20 28 74 79 70 65 20 4d  ed value (type M
19678 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65 71  em*).** P if req
19679 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e  uired..*/.#defin
1967a 65 20 65 78 70 61 6e 64 42 6c 6f 62 28 50 29 20  e expandBlob(P) 
1967b 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45 4d  (((P)->flags&MEM
1967c 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33 56 64  _Zero)?sqlite3Vd
1967d 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28  beMemExpandBlob(
1967e 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  P):0)../*.** If 
1967f 70 4d 65 6d 20 69 73 20 61 6e 20 6f 62 6a 65 63  pMem is an objec
19680 74 20 77 69 74 68 20 61 20 76 61 6c 69 64 20 73  t with a valid s
19681 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61  tring representa
19682 74 69 6f 6e 2c 20 74 68 69 73 20 72 6f 75 74 69  tion, this routi
19683 6e 65 0a 2a 2a 20 65 6e 73 75 72 65 73 20 74 68  ne.** ensures th
19684 65 20 69 6e 74 65 72 6e 61 6c 20 65 6e 63 6f 64  e internal encod
19685 69 6e 67 20 66 6f 72 20 74 68 65 20 73 74 72 69  ing for the stri
19686 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ng representatio
19687 6e 20 69 73 0a 2a 2a 20 27 64 65 73 69 72 65 64  n is.** 'desired
19688 45 6e 63 27 2c 20 6f 6e 65 20 6f 66 20 53 51 4c  Enc', one of SQL
19689 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45  ITE_UTF8, SQLITE
1968a 5f 55 54 46 31 36 4c 45 20 6f 72 20 53 51 4c 49  _UTF16LE or SQLI
1968b 54 45 5f 55 54 46 31 36 42 45 2e 0a 2a 2a 0a 2a  TE_UTF16BE..**.*
1968c 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 6e 6f 74  * If pMem is not
1968d 20 61 20 73 74 72 69 6e 67 20 6f 62 6a 65 63 74   a string object
1968e 2c 20 6f 72 20 74 68 65 20 65 6e 63 6f 64 69 6e  , or the encodin
1968f 67 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 0a  g of the string.
19690 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ** representatio
19691 6e 20 69 73 20 61 6c 72 65 61 64 79 20 73 74 6f  n is already sto
19692 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 72 65  red using the re
19693 71 75 65 73 74 65 64 20 65 6e 63 6f 64 69 6e 67  quested encoding
19694 2c 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72  , then this.** r
19695 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f  outine is a no-o
19696 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f  p..**.** SQLITE_
19697 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  OK is returned i
19698 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  f the conversion
19699 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 20 28   is successful (
1969a 6f 72 20 6e 6f 74 20 72 65 71 75 69 72 65 64 29  or not required)
1969b 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  ..** SQLITE_NOME
1969c 4d 20 6d 61 79 20 62 65 20 72 65 74 75 72 6e 65  M may be returne
1969d 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20  d if a malloc() 
1969e 66 61 69 6c 73 20 64 75 72 69 6e 67 20 63 6f 6e  fails during con
1969f 76 65 72 73 69 6f 6e 0a 2a 2a 20 62 65 74 77 65  version.** betwe
196a0 65 6e 20 66 6f 72 6d 61 74 73 2e 0a 2a 2f 0a 53  en formats..*/.S
196a1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
196a2 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61  t sqlite3VdbeCha
196a3 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65 6d 20  ngeEncoding(Mem 
196a4 2a 70 4d 65 6d 2c 20 69 6e 74 20 64 65 73 69 72  *pMem, int desir
196a5 65 64 45 6e 63 29 7b 0a 20 20 69 6e 74 20 72 63  edEnc){.  int rc
196a6 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65  ;.  assert( (pMe
196a7 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77  m->flags&MEM_Row
196a8 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73  Set)==0 );.  ass
196a9 65 72 74 28 20 64 65 73 69 72 65 64 45 6e 63 3d  ert( desiredEnc=
196aa 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 7c 7c 20  =SQLITE_UTF8 || 
196ab 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49  desiredEnc==SQLI
196ac 54 45 5f 55 54 46 31 36 4c 45 0a 20 20 20 20 20  TE_UTF16LE.     
196ad 20 20 20 20 20 20 7c 7c 20 64 65 73 69 72 65 64        || desired
196ae 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31  Enc==SQLITE_UTF1
196af 36 42 45 20 29 3b 0a 20 20 69 66 28 20 21 28 70  6BE );.  if( !(p
196b0 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53  Mem->flags&MEM_S
196b1 74 72 29 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63  tr) || pMem->enc
196b2 3d 3d 64 65 73 69 72 65 64 45 6e 63 20 29 7b 0a  ==desiredEnc ){.
196b3 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
196b4 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 61 73 73 65  E_OK;.  }.  asse
196b5 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20  rt( pMem->db==0 
196b6 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
196b7 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e  _held(pMem->db->
196b8 6d 75 74 65 78 29 20 29 3b 0a 23 69 66 64 65 66  mutex) );.#ifdef
196b9 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
196ba 31 36 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  16.  return SQLI
196bb 54 45 5f 45 52 52 4f 52 3b 0a 23 65 6c 73 65 0a  TE_ERROR;.#else.
196bc 0a 20 20 2f 2a 20 4d 65 6d 54 72 61 6e 73 6c 61  .  /* MemTransla
196bd 74 65 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20  te() may return 
196be 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20 53 51 4c  SQLITE_OK or SQL
196bf 49 54 45 5f 4e 4f 4d 45 4d 2e 20 49 66 20 4e 4f  ITE_NOMEM. If NO
196c0 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 2c  MEM is returned,
196c1 0a 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 65  .  ** then the e
196c2 6e 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 76  ncoding of the v
196c3 61 6c 75 65 20 6d 61 79 20 6e 6f 74 20 68 61 76  alue may not hav
196c4 65 20 63 68 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a  e changed..  */.
196c5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
196c6 62 65 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 70  beMemTranslate(p
196c7 4d 65 6d 2c 20 28 75 38 29 64 65 73 69 72 65 64  Mem, (u8)desired
196c8 45 6e 63 29 3b 0a 20 20 61 73 73 65 72 74 28 72  Enc);.  assert(r
196c9 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20  c==SQLITE_OK    
196ca 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  || rc==SQLITE_NO
196cb 4d 45 4d 29 3b 0a 20 20 61 73 73 65 72 74 28 72  MEM);.  assert(r
196cc 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20  c==SQLITE_OK    
196cd 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d 64 65  || pMem->enc!=de
196ce 73 69 72 65 64 45 6e 63 29 3b 0a 20 20 61 73 73  siredEnc);.  ass
196cf 65 72 74 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  ert(rc==SQLITE_N
196d0 4f 4d 45 4d 20 7c 7c 20 70 4d 65 6d 2d 3e 65 6e  OMEM || pMem->en
196d1 63 3d 3d 64 65 73 69 72 65 64 45 6e 63 29 3b 0a  c==desiredEnc);.
196d2 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e    return rc;.#en
196d3 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  dif.}../*.** Mak
196d4 65 20 73 75 72 65 20 70 4d 65 6d 2d 3e 7a 20 70  e sure pMem->z p
196d5 6f 69 6e 74 73 20 74 6f 20 61 20 77 72 69 74 61  oints to a writa
196d6 62 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f  ble allocation o
196d7 66 20 61 74 20 6c 65 61 73 74 20 0a 2a 2a 20 6e  f at least .** n
196d8 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66   bytes..**.** If
196d9 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c   the memory cell
196da 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61   currently conta
196db 69 6e 73 20 73 74 72 69 6e 67 20 6f 72 20 62 6c  ins string or bl
196dc 6f 62 20 64 61 74 61 0a 2a 2a 20 61 6e 64 20 74  ob data.** and t
196dd 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
196de 74 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73  t passed to this
196df 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74 72 75   function is tru
196e0 65 2c 20 74 68 65 20 0a 2a 2a 20 63 75 72 72 65  e, the .** curre
196e1 6e 74 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  nt content of th
196e2 65 20 63 65 6c 6c 20 69 73 20 70 72 65 73 65 72  e cell is preser
196e3 76 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ved. Otherwise, 
196e4 69 74 20 6d 61 79 0a 2a 2a 20 62 65 20 64 69 73  it may.** be dis
196e5 63 61 72 64 65 64 2e 20 20 0a 2a 2a 0a 2a 2a 20  carded.  .**.** 
196e6 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65  This function se
196e7 74 73 20 74 68 65 20 4d 45 4d 5f 44 79 6e 20 66  ts the MEM_Dyn f
196e8 6c 61 67 20 61 6e 64 20 63 6c 65 61 72 73 20 61  lag and clears a
196e9 6e 79 20 78 44 65 6c 20 63 61 6c 6c 62 61 63 6b  ny xDel callback
196ea 2e 0a 2a 2a 20 49 74 20 61 6c 73 6f 20 63 6c 65  ..** It also cle
196eb 61 72 73 20 4d 45 4d 5f 45 70 68 65 6d 20 61 6e  ars MEM_Ephem an
196ec 64 20 4d 45 4d 5f 53 74 61 74 69 63 2e 20 49 66  d MEM_Static. If
196ed 20 74 68 65 20 70 72 65 73 65 72 76 65 20 66 6c   the preserve fl
196ee 61 67 20 69 73 20 0a 2a 2a 20 6e 6f 74 20 73 65  ag is .** not se
196ef 74 2c 20 4d 65 6d 2e 6e 20 69 73 20 7a 65 72 6f  t, Mem.n is zero
196f0 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
196f1 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
196f2 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 4d 65 6d  3VdbeMemGrow(Mem
196f3 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 2c 20 69   *pMem, int n, i
196f4 6e 74 20 70 72 65 73 65 72 76 65 29 7b 0a 20 20  nt preserve){.  
196f5 61 73 73 65 72 74 28 20 31 20 3e 3d 0a 20 20 20  assert( 1 >=.   
196f6 20 28 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63   ((pMem->zMalloc
196f7 20 26 26 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f   && pMem->zMallo
196f8 63 3d 3d 70 4d 65 6d 2d 3e 7a 29 20 3f 20 31 20  c==pMem->z) ? 1 
196f9 3a 20 30 29 20 2b 0a 20 20 20 20 28 28 28 70 4d  : 0) +.    (((pM
196fa 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79  em->flags&MEM_Dy
196fb 6e 29 26 26 70 4d 65 6d 2d 3e 78 44 65 6c 29 20  n)&&pMem->xDel) 
196fc 3f 20 31 20 3a 20 30 29 20 2b 20 0a 20 20 20 20  ? 1 : 0) + .    
196fd 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45  ((pMem->flags&ME
196fe 4d 5f 45 70 68 65 6d 29 20 3f 20 31 20 3a 20 30  M_Ephem) ? 1 : 0
196ff 29 20 2b 20 0a 20 20 20 20 28 28 70 4d 65 6d 2d  ) + .    ((pMem-
19700 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61 74 69  >flags&MEM_Stati
19701 63 29 20 3f 20 31 20 3a 20 30 29 0a 20 20 29 3b  c) ? 1 : 0).  );
19702 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d  .  assert( (pMem
19703 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53  ->flags&MEM_RowS
19704 65 74 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 66 28  et)==0 );..  if(
19705 20 6e 3c 33 32 20 29 20 6e 20 3d 20 33 32 3b 0a   n<32 ) n = 32;.
19706 20 20 69 66 28 20 73 71 6c 69 74 65 33 44 62 4d    if( sqlite3DbM
19707 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 6d 2d 3e  allocSize(pMem->
19708 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f  db, pMem->zMallo
19709 63 29 3c 6e 20 29 7b 0a 20 20 20 20 69 66 28 20  c)<n ){.    if( 
1970a 70 72 65 73 65 72 76 65 20 26 26 20 70 4d 65 6d  preserve && pMem
1970b 2d 3e 7a 3d 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c  ->z==pMem->zMall
1970c 6f 63 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d  oc ){.      pMem
1970d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 4d 61 6c  ->z = pMem->zMal
1970e 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 52  loc = sqlite3DbR
1970f 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 4d 65  eallocOrFree(pMe
19710 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 2c 20  m->db, pMem->z, 
19711 6e 29 3b 0a 20 20 20 20 20 20 70 72 65 73 65 72  n);.      preser
19712 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  ve = 0;.    }els
19713 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
19714 44 62 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c  DbFree(pMem->db,
19715 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b   pMem->zMalloc);
19716 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61  .      pMem->zMa
19717 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62  lloc = sqlite3Db
19718 4d 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d 3e  MallocRaw(pMem->
19719 64 62 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20  db, n);.    }.  
1971a 7d 0a 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a  }..  if( pMem->z
1971b 20 26 26 20 70 72 65 73 65 72 76 65 20 26 26 20   && preserve && 
1971c 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 26 26  pMem->zMalloc &&
1971d 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e   pMem->z!=pMem->
1971e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 6d  zMalloc ){.    m
1971f 65 6d 63 70 79 28 70 4d 65 6d 2d 3e 7a 4d 61 6c  emcpy(pMem->zMal
19720 6c 6f 63 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 70 4d  loc, pMem->z, pM
19721 65 6d 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20 69 66  em->n);.  }.  if
19722 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45  ( pMem->flags&ME
19723 4d 5f 44 79 6e 20 26 26 20 70 4d 65 6d 2d 3e 78  M_Dyn && pMem->x
19724 44 65 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d  Del ){.    pMem-
19725 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a 29 28 70  >xDel((void *)(p
19726 4d 65 6d 2d 3e 7a 29 29 3b 0a 20 20 7d 0a 0a 20  Mem->z));.  }.. 
19727 20 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d   pMem->z = pMem-
19728 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69 66 28 20  >zMalloc;.  if( 
19729 70 4d 65 6d 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20  pMem->z==0 ){.  
1972a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
1972b 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73  MEM_Null;.  }els
1972c 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  e{.    pMem->fla
1972d 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65  gs &= ~(MEM_Ephe
1972e 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20  m|MEM_Static);. 
1972f 20 7d 0a 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20   }.  pMem->xDel 
19730 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 70  = 0;.  return (p
19731 4d 65 6d 2d 3e 7a 20 3f 20 53 51 4c 49 54 45 5f  Mem->z ? SQLITE_
19732 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  OK : SQLITE_NOME
19733 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b  M);.}../*.** Mak
19734 65 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 20  e the given Mem 
19735 6f 62 6a 65 63 74 20 4d 45 4d 5f 44 79 6e 2e 20  object MEM_Dyn. 
19736 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
19737 20 6d 61 6b 65 20 69 74 20 73 6f 0a 2a 2a 20 74   make it so.** t
19738 68 61 74 20 61 6e 79 20 54 45 58 54 20 6f 72 20  hat any TEXT or 
19739 42 4c 4f 42 20 63 6f 6e 74 65 6e 74 20 69 73 20  BLOB content is 
1973a 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79  stored in memory
1973b 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a   obtained from.*
1973c 2a 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 49 6e 20  * malloc().  In 
1973d 74 68 69 73 20 77 61 79 2c 20 77 65 20 6b 6e 6f  this way, we kno
1973e 77 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72  w that the memor
1973f 79 20 69 73 20 73 61 66 65 20 74 6f 20 62 65 0a  y is safe to be.
19740 2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20 6f  ** overwritten o
19741 72 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a  r altered..**.**
19742 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   Return SQLITE_O
19743 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20  K on success or 
19744 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20  SQLITE_NOMEM if 
19745 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f  malloc fails..*/
19746 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
19747 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
19748 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28  emMakeWriteable(
19749 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e  Mem *pMem){.  in
1974a 74 20 66 3b 0a 20 20 61 73 73 65 72 74 28 20 70  t f;.  assert( p
1974b 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
1974c 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1974d 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
1974e 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  ) );.  assert( (
1974f 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pMem->flags&MEM_
19750 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20  RowSet)==0 );.  
19751 65 78 70 61 6e 64 42 6c 6f 62 28 70 4d 65 6d 29  expandBlob(pMem)
19752 3b 0a 20 20 66 20 3d 20 70 4d 65 6d 2d 3e 66 6c  ;.  f = pMem->fl
19753 61 67 73 3b 0a 20 20 69 66 28 20 28 66 26 28 4d  ags;.  if( (f&(M
19754 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29  EM_Str|MEM_Blob)
19755 29 20 26 26 20 70 4d 65 6d 2d 3e 7a 21 3d 70 4d  ) && pMem->z!=pM
19756 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20  em->zMalloc ){. 
19757 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64     if( sqlite3Vd
19758 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20  beMemGrow(pMem, 
19759 70 4d 65 6d 2d 3e 6e 20 2b 20 32 2c 20 31 29 20  pMem->n + 2, 1) 
1975a 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1975b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
1975c 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b    }.    pMem->z[
1975d 70 4d 65 6d 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20  pMem->n] = 0;.  
1975e 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e    pMem->z[pMem->
1975f 6e 2b 31 5d 20 3d 20 30 3b 0a 20 20 20 20 70 4d  n+1] = 0;.    pM
19760 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d  em->flags |= MEM
19761 5f 54 65 72 6d 3b 0a 20 20 7d 0a 0a 20 20 72 65  _Term;.  }..  re
19762 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
19763 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  }../*.** If the 
19764 67 69 76 65 6e 20 4d 65 6d 2a 20 68 61 73 20 61  given Mem* has a
19765 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74 61 69   zero-filled tai
19766 6c 2c 20 74 75 72 6e 20 69 74 20 69 6e 74 6f 20  l, turn it into 
19767 61 6e 20 6f 72 64 69 6e 61 72 79 0a 2a 2a 20 62  an ordinary.** b
19768 6c 6f 62 20 73 74 6f 72 65 64 20 69 6e 20 64 79  lob stored in dy
19769 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
1976a 74 65 64 20 73 70 61 63 65 2e 0a 2a 2f 0a 23 69  ted space..*/.#i
1976b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1976c 54 5f 49 4e 43 52 42 4c 4f 42 0a 53 51 4c 49 54  T_INCRBLOB.SQLIT
1976d 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1976e 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61  lite3VdbeMemExpa
1976f 6e 64 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d 65 6d  ndBlob(Mem *pMem
19770 29 7b 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66  ){.  if( pMem->f
19771 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20  lags & MEM_Zero 
19772 29 7b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65  ){.    int nByte
19773 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d  ;.    assert( pM
19774 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c  em->flags&MEM_Bl
19775 6f 62 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ob );.    assert
19776 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d  ( (pMem->flags&M
19777 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b  EM_RowSet)==0 );
19778 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65  .    assert( pMe
19779 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  m->db==0 || sqli
1977a 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1977b 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  Mem->db->mutex) 
1977c 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 6e  );..    /* Set n
1977d 42 79 74 65 20 74 6f 20 74 68 65 20 6e 75 6d 62  Byte to the numb
1977e 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75  er of bytes requ
1977f 69 72 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68  ired to store th
19780 65 20 65 78 70 61 6e 64 65 64 20 62 6c 6f 62 2e  e expanded blob.
19781 20 2a 2f 0a 20 20 20 20 6e 42 79 74 65 20 3d 20   */.    nByte = 
19782 70 4d 65 6d 2d 3e 6e 20 2b 20 70 4d 65 6d 2d 3e  pMem->n + pMem->
19783 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 69 66 28  u.nZero;.    if(
19784 20 6e 42 79 74 65 3c 3d 30 20 29 7b 0a 20 20 20   nByte<=0 ){.   
19785 20 20 20 6e 42 79 74 65 20 3d 20 31 3b 0a 20 20     nByte = 1;.  
19786 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69    }.    if( sqli
19787 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
19788 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 31 29 20 29  Mem, nByte, 1) )
19789 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
1978a 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
1978b 20 7d 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28 26   }..    memset(&
1978c 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d  pMem->z[pMem->n]
1978d 2c 20 30 2c 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65  , 0, pMem->u.nZe
1978e 72 6f 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e  ro);.    pMem->n
1978f 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72   += pMem->u.nZer
19790 6f 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  o;.    pMem->fla
19791 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 5a 65 72 6f  gs &= ~(MEM_Zero
19792 7c 4d 45 4d 5f 54 65 72 6d 29 3b 0a 20 20 7d 0a  |MEM_Term);.  }.
19793 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
19794 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f  OK;.}.#endif.../
19795 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74  *.** Make sure t
19796 68 65 20 67 69 76 65 6e 20 4d 65 6d 20 69 73 20  he given Mem is 
19797 5c 75 30 30 30 30 20 74 65 72 6d 69 6e 61 74 65  \u0000 terminate
19798 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
19799 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1979a 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e  VdbeMemNulTermin
1979b 61 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a  ate(Mem *pMem){.
1979c 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
1979d 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
1979e 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d  _mutex_held(pMem
1979f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
197a0 20 20 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61    if( (pMem->fla
197a1 67 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 21 3d  gs & MEM_Term)!=
197a2 30 20 7c 7c 20 28 70 4d 65 6d 2d 3e 66 6c 61 67  0 || (pMem->flag
197a3 73 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20  s & MEM_Str)==0 
197a4 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
197a5 4c 49 54 45 5f 4f 4b 3b 20 20 20 2f 2a 20 4e 6f  LITE_OK;   /* No
197a6 74 68 69 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a 20  thing to do */. 
197a7 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
197a8 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d  VdbeMemGrow(pMem
197a9 2c 20 70 4d 65 6d 2d 3e 6e 2b 32 2c 20 31 29 20  , pMem->n+2, 1) 
197aa 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
197ab 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
197ac 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e    pMem->z[pMem->
197ad 6e 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e  n] = 0;.  pMem->
197ae 7a 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30  z[pMem->n+1] = 0
197af 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  ;.  pMem->flags 
197b0 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 72  |= MEM_Term;.  r
197b1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
197b2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4d 45  .}../*.** Add ME
197b3 4d 5f 53 74 72 20 74 6f 20 74 68 65 20 73 65 74  M_Str to the set
197b4 20 6f 66 20 72 65 70 72 65 73 65 6e 74 61 74 69   of representati
197b5 6f 6e 73 20 66 6f 72 20 74 68 65 20 67 69 76 65  ons for the give
197b6 6e 20 4d 65 6d 2e 20 20 4e 75 6d 62 65 72 73 0a  n Mem.  Numbers.
197b7 2a 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64  ** are converted
197b8 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 73   using sqlite3_s
197b9 6e 70 72 69 6e 74 66 28 29 2e 20 20 43 6f 6e 76  nprintf().  Conv
197ba 65 72 74 69 6e 67 20 61 20 42 4c 4f 42 20 74 6f  erting a BLOB to
197bb 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20   a string.** is 
197bc 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 45  a no-op..**.** E
197bd 78 69 73 74 69 6e 67 20 72 65 70 72 65 73 65 6e  xisting represen
197be 74 61 74 69 6f 6e 73 20 4d 45 4d 5f 49 6e 74 20  tations MEM_Int 
197bf 61 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 61 72 65  and MEM_Real are
197c0 20 2a 6e 6f 74 2a 20 69 6e 76 61 6c 69 64 61 74   *not* invalidat
197c1 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 4d 45 4d 5f  ed..**.** A MEM_
197c2 4e 75 6c 6c 20 76 61 6c 75 65 20 77 69 6c 6c 20  Null value will 
197c3 6e 65 76 65 72 20 62 65 20 70 61 73 73 65 64 20  never be passed 
197c4 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
197c5 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
197c6 69 73 0a 2a 2a 20 75 73 65 64 20 66 6f 72 20 63  is.** used for c
197c7 6f 6e 76 65 72 74 69 6e 67 20 76 61 6c 75 65 73  onverting values
197c8 20 74 6f 20 74 65 78 74 20 66 6f 72 20 72 65 74   to text for ret
197c9 75 72 6e 69 6e 67 20 74 6f 20 74 68 65 20 75 73  urning to the us
197ca 65 72 20 28 69 2e 65 2e 20 76 69 61 0a 2a 2a 20  er (i.e. via.** 
197cb 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
197cc 78 74 28 29 29 2c 20 6f 72 20 66 6f 72 20 65 6e  xt()), or for en
197cd 73 75 72 69 6e 67 20 74 68 61 74 20 76 61 6c 75  suring that valu
197ce 65 73 20 74 6f 20 62 65 20 75 73 65 64 20 61 73  es to be used as
197cf 20 62 74 72 65 65 0a 2a 2a 20 6b 65 79 73 20 61   btree.** keys a
197d0 72 65 20 73 74 72 69 6e 67 73 2e 20 49 6e 20 74  re strings. In t
197d1 68 65 20 66 6f 72 6d 65 72 20 63 61 73 65 20 61  he former case a
197d2 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73   NULL pointer is
197d3 20 72 65 74 75 72 6e 65 64 20 74 68 65 0a 2a 2a   returned the.**
197d4 20 75 73 65 72 20 61 6e 64 20 74 68 65 20 6c 61   user and the la
197d5 74 65 72 20 69 73 20 61 6e 20 69 6e 74 65 72 6e  ter is an intern
197d6 61 6c 20 70 72 6f 67 72 61 6d 6d 69 6e 67 20 65  al programming e
197d7 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rror..*/.SQLITE_
197d8 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
197d9 74 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67  te3VdbeMemString
197da 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69  ify(Mem *pMem, i
197db 6e 74 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 72  nt enc){.  int r
197dc 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
197dd 20 69 6e 74 20 66 67 20 3d 20 70 4d 65 6d 2d 3e   int fg = pMem->
197de 66 6c 61 67 73 3b 0a 20 20 63 6f 6e 73 74 20 69  flags;.  const i
197df 6e 74 20 6e 42 79 74 65 20 3d 20 33 32 3b 0a 0a  nt nByte = 32;..
197e0 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
197e1 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
197e2 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d  _mutex_held(pMem
197e3 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
197e4 20 20 61 73 73 65 72 74 28 20 21 28 66 67 26 4d    assert( !(fg&M
197e5 45 4d 5f 5a 65 72 6f 29 20 29 3b 0a 20 20 61 73  EM_Zero) );.  as
197e6 73 65 72 74 28 20 21 28 66 67 26 28 4d 45 4d 5f  sert( !(fg&(MEM_
197e7 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 20 29  Str|MEM_Blob)) )
197e8 3b 0a 20 20 61 73 73 65 72 74 28 20 66 67 26 28  ;.  assert( fg&(
197e9 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c  MEM_Int|MEM_Real
197ea 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  ) );.  assert( (
197eb 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pMem->flags&MEM_
197ec 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20 20  RowSet)==0 );.  
197ed 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59  assert( EIGHT_BY
197ee 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65  TE_ALIGNMENT(pMe
197ef 6d 29 20 29 3b 0a 0a 0a 20 20 69 66 28 20 73 71  m) );...  if( sq
197f0 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77  lite3VdbeMemGrow
197f1 28 70 4d 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29  (pMem, nByte, 0)
197f2 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
197f3 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
197f4 0a 0a 20 20 2f 2a 20 46 6f 72 20 61 20 52 65 61  ..  /* For a Rea
197f5 6c 20 6f 72 20 49 6e 74 65 67 65 72 2c 20 75 73  l or Integer, us
197f6 65 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  e sqlite3_mprint
197f7 66 28 29 20 74 6f 20 70 72 6f 64 75 63 65 20 74  f() to produce t
197f8 68 65 20 55 54 46 2d 38 0a 20 20 2a 2a 20 73 74  he UTF-8.  ** st
197f9 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74  ring representat
197fa 69 6f 6e 20 6f 66 20 74 68 65 20 76 61 6c 75 65  ion of the value
197fb 2e 20 54 68 65 6e 2c 20 69 66 20 74 68 65 20 72  . Then, if the r
197fc 65 71 75 69 72 65 64 20 65 6e 63 6f 64 69 6e 67  equired encoding
197fd 0a 20 20 2a 2a 20 69 73 20 55 54 46 2d 31 36 6c  .  ** is UTF-16l
197fe 65 20 6f 72 20 55 54 46 2d 31 36 62 65 20 64 6f  e or UTF-16be do
197ff 20 61 20 74 72 61 6e 73 6c 61 74 69 6f 6e 2e 0a   a translation..
19800 20 20 2a 2a 20 0a 20 20 2a 2a 20 46 49 58 20 4d    ** .  ** FIX M
19801 45 3a 20 49 74 20 77 6f 75 6c 64 20 62 65 20 62  E: It would be b
19802 65 74 74 65 72 20 69 66 20 73 71 6c 69 74 65 33  etter if sqlite3
19803 5f 73 6e 70 72 69 6e 74 66 28 29 20 63 6f 75 6c  _snprintf() coul
19804 64 20 64 6f 20 55 54 46 2d 31 36 2e 0a 20 20 2a  d do UTF-16..  *
19805 2f 0a 20 20 69 66 28 20 66 67 20 26 20 4d 45 4d  /.  if( fg & MEM
19806 5f 49 6e 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  _Int ){.    sqli
19807 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 79  te3_snprintf(nBy
19808 74 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 6c  te, pMem->z, "%l
19809 6c 64 22 2c 20 70 4d 65 6d 2d 3e 75 2e 69 29 3b  ld", pMem->u.i);
1980a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
1980b 73 65 72 74 28 20 66 67 20 26 20 4d 45 4d 5f 52  sert( fg & MEM_R
1980c 65 61 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  eal );.    sqlit
1980d 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 42 79 74  e3_snprintf(nByt
1980e 65 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 21 2e  e, pMem->z, "%!.
1980f 31 35 67 22 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0a  15g", pMem->r);.
19810 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20    }.  pMem->n = 
19811 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
19812 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 70 4d 65 6d  pMem->z);.  pMem
19813 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55  ->enc = SQLITE_U
19814 54 46 38 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61  TF8;.  pMem->fla
19815 67 73 20 7c 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45  gs |= MEM_Str|ME
19816 4d 5f 54 65 72 6d 3b 0a 20 20 73 71 6c 69 74 65  M_Term;.  sqlite
19817 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64  3VdbeChangeEncod
19818 69 6e 67 28 70 4d 65 6d 2c 20 65 6e 63 29 3b 0a  ing(pMem, enc);.
19819 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1981a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 63 65 6c  /*.** Memory cel
1981b 6c 20 70 4d 65 6d 20 63 6f 6e 74 61 69 6e 73 20  l pMem contains 
1981c 74 68 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 61  the context of a
1981d 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  n aggregate func
1981e 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 72 6f  tion..** This ro
1981f 75 74 69 6e 65 20 63 61 6c 6c 73 20 74 68 65 20  utine calls the 
19820 66 69 6e 61 6c 69 7a 65 20 6d 65 74 68 6f 64 20  finalize method 
19821 66 6f 72 20 74 68 61 74 20 66 75 6e 63 74 69 6f  for that functio
19822 6e 2e 20 20 54 68 65 0a 2a 2a 20 72 65 73 75 6c  n.  The.** resul
19823 74 20 6f 66 20 74 68 65 20 61 67 67 72 65 67 61  t of the aggrega
19824 74 65 20 69 73 20 73 74 6f 72 65 64 20 62 61 63  te is stored bac
19825 6b 20 69 6e 74 6f 20 70 4d 65 6d 2e 0a 2a 2a 0a  k into pMem..**.
19826 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
19827 5f 45 52 52 4f 52 20 69 66 20 74 68 65 20 66 69  _ERROR if the fi
19828 6e 61 6c 69 7a 65 72 20 72 65 70 6f 72 74 73 20  nalizer reports 
19829 61 6e 20 65 72 72 6f 72 2e 20 20 53 51 4c 49 54  an error.  SQLIT
1982a 45 5f 4f 4b 0a 2a 2a 20 6f 74 68 65 72 77 69 73  E_OK.** otherwis
1982b 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1982c 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1982d 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28  VdbeMemFinalize(
1982e 4d 65 6d 20 2a 70 4d 65 6d 2c 20 46 75 6e 63 44  Mem *pMem, FuncD
1982f 65 66 20 2a 70 46 75 6e 63 29 7b 0a 20 20 69 6e  ef *pFunc){.  in
19830 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
19831 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70  ;.  if( ALWAYS(p
19832 46 75 6e 63 20 26 26 20 70 46 75 6e 63 2d 3e 78  Func && pFunc->x
19833 46 69 6e 61 6c 69 7a 65 29 20 29 7b 0a 20 20 20  Finalize) ){.   
19834 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
19835 20 63 74 78 3b 0a 20 20 20 20 61 73 73 65 72 74   ctx;.    assert
19836 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  ( (pMem->flags &
19837 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 7c 7c   MEM_Null)!=0 ||
19838 20 70 46 75 6e 63 3d 3d 70 4d 65 6d 2d 3e 75 2e   pFunc==pMem->u.
19839 70 44 65 66 20 29 3b 0a 20 20 20 20 61 73 73 65  pDef );.    asse
1983a 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20  rt( pMem->db==0 
1983b 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
1983c 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e  _held(pMem->db->
1983d 6d 75 74 65 78 29 20 29 3b 0a 20 20 20 20 6d 65  mutex) );.    me
1983e 6d 73 65 74 28 26 63 74 78 2c 20 30 2c 20 73 69  mset(&ctx, 0, si
1983f 7a 65 6f 66 28 63 74 78 29 29 3b 0a 20 20 20 20  zeof(ctx));.    
19840 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45  ctx.s.flags = ME
19841 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 63 74 78 2e  M_Null;.    ctx.
19842 73 2e 64 62 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b  s.db = pMem->db;
19843 0a 20 20 20 20 63 74 78 2e 70 4d 65 6d 20 3d 20  .    ctx.pMem = 
19844 70 4d 65 6d 3b 0a 20 20 20 20 63 74 78 2e 70 46  pMem;.    ctx.pF
19845 75 6e 63 20 3d 20 70 46 75 6e 63 3b 0a 20 20 20  unc = pFunc;.   
19846 20 70 46 75 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a   pFunc->xFinaliz
19847 65 28 26 63 74 78 29 3b 0a 20 20 20 20 61 73 73  e(&ctx);.    ass
19848 65 72 74 28 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66  ert( 0==(pMem->f
19849 6c 61 67 73 26 4d 45 4d 5f 44 79 6e 29 20 26 26  lags&MEM_Dyn) &&
1984a 20 21 70 4d 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a   !pMem->xDel );.
1984b 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1984c 65 28 70 4d 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d  e(pMem->db, pMem
1984d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20  ->zMalloc);.    
1984e 6d 65 6d 63 70 79 28 70 4d 65 6d 2c 20 26 63 74  memcpy(pMem, &ct
1984f 78 2e 73 2c 20 73 69 7a 65 6f 66 28 63 74 78 2e  x.s, sizeof(ctx.
19850 73 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 74  s));.    rc = ct
19851 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 20  x.isError;.  }. 
19852 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
19853 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f  *.** If the memo
19854 72 79 20 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 73  ry cell contains
19855 20 61 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20   a string value 
19856 74 68 61 74 20 6d 75 73 74 20 62 65 20 66 72 65  that must be fre
19857 65 64 20 62 79 0a 2a 2a 20 69 6e 76 6f 6b 69 6e  ed by.** invokin
19858 67 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 63 61  g an external ca
19859 6c 6c 62 61 63 6b 2c 20 66 72 65 65 20 69 74 20  llback, free it 
1985a 6e 6f 77 2e 20 43 61 6c 6c 69 6e 67 20 74 68 69  now. Calling thi
1985b 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 64 6f  s function.** do
1985c 65 73 20 6e 6f 74 20 66 72 65 65 20 61 6e 79 20  es not free any 
1985d 4d 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 62 75 66 66  Mem.zMalloc buff
1985e 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
1985f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
19860 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
19861 45 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a 70 29  External(Mem *p)
19862 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64  {.  assert( p->d
19863 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
19864 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62  mutex_held(p->db
19865 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 74 65  ->mutex) );.  te
19866 73 74 63 61 73 65 28 20 70 2d 3e 66 6c 61 67 73  stcase( p->flags
19867 20 26 20 4d 45 4d 5f 41 67 67 20 29 3b 0a 20 20   & MEM_Agg );.  
19868 74 65 73 74 63 61 73 65 28 20 70 2d 3e 66 6c 61  testcase( p->fla
19869 67 73 20 26 20 4d 45 4d 5f 44 79 6e 20 29 3b 0a  gs & MEM_Dyn );.
1986a 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 66    testcase( p->f
1986b 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65  lags & MEM_RowSe
1986c 74 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28  t );.  testcase(
1986d 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f   p->flags & MEM_
1986e 46 72 61 6d 65 20 29 3b 0a 20 20 69 66 28 20 70  Frame );.  if( p
1986f 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 41 67 67  ->flags&(MEM_Agg
19870 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 52 6f 77  |MEM_Dyn|MEM_Row
19871 53 65 74 7c 4d 45 4d 5f 46 72 61 6d 65 29 20 29  Set|MEM_Frame) )
19872 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61  {.    if( p->fla
19873 67 73 26 4d 45 4d 5f 41 67 67 20 29 7b 0a 20 20  gs&MEM_Agg ){.  
19874 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
19875 65 6d 46 69 6e 61 6c 69 7a 65 28 70 2c 20 70 2d  emFinalize(p, p-
19876 3e 75 2e 70 44 65 66 29 3b 0a 20 20 20 20 20 20  >u.pDef);.      
19877 61 73 73 65 72 74 28 20 28 70 2d 3e 66 6c 61 67  assert( (p->flag
19878 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d 30 20  s & MEM_Agg)==0 
19879 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1987a 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
1987b 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  );.    }else if(
1987c 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79   p->flags&MEM_Dy
1987d 6e 20 26 26 20 70 2d 3e 78 44 65 6c 20 29 7b 0a  n && p->xDel ){.
1987e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70        assert( (p
1987f 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 52 6f 77 53  ->flags&MEM_RowS
19880 65 74 29 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  et)==0 );.      
19881 70 2d 3e 78 44 65 6c 28 28 76 6f 69 64 20 2a 29  p->xDel((void *)
19882 70 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 2d 3e  p->z);.      p->
19883 78 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 7d 65  xDel = 0;.    }e
19884 6c 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73  lse if( p->flags
19885 26 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a 20  &MEM_RowSet ){. 
19886 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77 53       sqlite3RowS
19887 65 74 43 6c 65 61 72 28 70 2d 3e 75 2e 70 52 6f  etClear(p->u.pRo
19888 77 53 65 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  wSet);.    }else
19889 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 4d 45   if( p->flags&ME
1988a 4d 5f 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 20  M_Frame ){.     
1988b 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1988c 65 74 4e 75 6c 6c 28 70 29 3b 0a 20 20 20 20 7d  etNull(p);.    }
1988d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
1988e 6c 65 61 73 65 20 61 6e 79 20 6d 65 6d 6f 72 79  lease any memory
1988f 20 68 65 6c 64 20 62 79 20 74 68 65 20 4d 65 6d   held by the Mem
19890 2e 20 54 68 69 73 20 6d 61 79 20 6c 65 61 76 65  . This may leave
19891 20 74 68 65 20 4d 65 6d 20 69 6e 20 61 6e 0a 2a   the Mem in an.*
19892 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20 73  * inconsistent s
19893 74 61 74 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c  tate, for exampl
19894 65 20 77 69 74 68 20 28 4d 65 6d 2e 7a 3d 3d 30  e with (Mem.z==0
19895 29 20 61 6e 64 0a 2a 2a 20 28 4d 65 6d 2e 74 79  ) and.** (Mem.ty
19896 70 65 3d 3d 53 51 4c 49 54 45 5f 54 45 58 54 29  pe==SQLITE_TEXT)
19897 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19898 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
19899 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 4d  VdbeMemRelease(M
1989a 65 6d 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65  em *p){.  sqlite
1989b 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45  3VdbeMemReleaseE
1989c 78 74 65 72 6e 61 6c 28 70 29 3b 0a 20 20 73 71  xternal(p);.  sq
1989d 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64  lite3DbFree(p->d
1989e 62 2c 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a  b, p->zMalloc);.
1989f 20 20 70 2d 3e 7a 20 3d 20 30 3b 0a 20 20 70 2d    p->z = 0;.  p-
198a0 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20  >zMalloc = 0;.  
198a1 70 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 7d 0a 0a  p->xDel = 0;.}..
198a2 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20  /*.** Convert a 
198a3 36 34 2d 62 69 74 20 49 45 45 45 20 64 6f 75 62  64-bit IEEE doub
198a4 6c 65 20 69 6e 74 6f 20 61 20 36 34 2d 62 69 74  le into a 64-bit
198a5 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e   signed integer.
198a6 0a 2a 2a 20 49 66 20 74 68 65 20 64 6f 75 62 6c  .** If the doubl
198a7 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 2c 20  e is too large, 
198a8 72 65 74 75 72 6e 20 30 78 38 30 30 30 30 30 30  return 0x8000000
198a9 30 30 30 30 30 30 30 30 30 2e 0a 2a 2a 0a 2a 2a  000000000..**.**
198aa 20 4d 6f 73 74 20 73 79 73 74 65 6d 73 20 61 70   Most systems ap
198ab 70 65 61 72 20 74 6f 20 64 6f 20 74 68 69 73 20  pear to do this 
198ac 73 69 6d 70 6c 79 20 62 79 20 61 73 73 69 67 6e  simply by assign
198ad 69 6e 67 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73  ing.** variables
198ae 20 61 6e 64 20 77 69 74 68 6f 75 74 20 74 68 65   and without the
198af 20 65 78 74 72 61 20 72 61 6e 67 65 20 74 65 73   extra range tes
198b0 74 73 2e 20 20 42 75 74 0a 2a 2a 20 74 68 65 72  ts.  But.** ther
198b1 65 20 61 72 65 20 72 65 70 6f 72 74 73 20 74 68  e are reports th
198b2 61 74 20 77 69 6e 64 6f 77 73 20 74 68 72 6f 77  at windows throw
198b3 73 20 61 6e 20 65 78 70 65 63 74 69 6f 6e 0a 2a  s an expection.*
198b4 2a 20 69 66 20 74 68 65 20 66 6c 6f 61 74 69 6e  * if the floatin
198b5 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 73  g point value is
198b6 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 28   out of range. (
198b7 53 65 65 20 74 69 63 6b 65 74 20 23 32 38 38 30  See ticket #2880
198b8 2e 29 0a 2a 2a 20 42 65 63 61 75 73 65 20 77 65  .).** Because we
198b9 20 64 6f 20 6e 6f 74 20 63 6f 6d 70 6c 65 74 65   do not complete
198ba 6c 79 20 75 6e 64 65 72 73 74 61 6e 64 20 74 68  ly understand th
198bb 65 20 70 72 6f 62 6c 65 6d 2c 20 77 65 20 77 69  e problem, we wi
198bc 6c 6c 0a 2a 2a 20 74 61 6b 65 20 74 68 65 20 63  ll.** take the c
198bd 6f 6e 73 65 72 76 61 74 69 76 65 20 61 70 70 72  onservative appr
198be 6f 61 63 68 20 61 6e 64 20 61 6c 77 61 79 73 20  oach and always 
198bf 64 6f 20 72 61 6e 67 65 20 74 65 73 74 73 0a 2a  do range tests.*
198c0 2a 20 62 65 66 6f 72 65 20 61 74 74 65 6d 70 74  * before attempt
198c1 69 6e 67 20 74 68 65 20 63 6f 6e 76 65 72 73 69  ing the conversi
198c2 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36  on..*/.static i6
198c3 34 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28  4 doubleToInt64(
198c4 64 6f 75 62 6c 65 20 72 29 7b 0a 20 20 2f 2a 0a  double r){.  /*.
198c5 20 20 2a 2a 20 4d 61 6e 79 20 63 6f 6d 70 69 6c    ** Many compil
198c6 65 72 73 20 77 65 20 65 6e 63 6f 75 6e 74 65 72  ers we encounter
198c7 20 64 6f 20 6e 6f 74 20 64 65 66 69 6e 65 20 63   do not define c
198c8 6f 6e 73 74 61 6e 74 73 20 66 6f 72 20 74 68 65  onstants for the
198c9 0a 20 20 2a 2a 20 6d 69 6e 69 6d 75 6d 20 61 6e  .  ** minimum an
198ca 64 20 6d 61 78 69 6d 75 6d 20 36 34 2d 62 69 74  d maximum 64-bit
198cb 20 69 6e 74 65 67 65 72 73 2c 20 6f 72 20 74 68   integers, or th
198cc 65 79 20 64 65 66 69 6e 65 20 74 68 65 6d 0a 20  ey define them. 
198cd 20 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74   ** inconsistent
198ce 6c 79 2e 20 20 41 6e 64 20 6d 61 6e 79 20 64 6f  ly.  And many do
198cf 20 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64 20   not understand 
198d0 74 68 65 20 22 4c 4c 22 20 6e 6f 74 61 74 69 6f  the "LL" notatio
198d1 6e 2e 0a 20 20 2a 2a 20 53 6f 20 77 65 20 64 65  n..  ** So we de
198d2 66 69 6e 65 20 6f 75 72 20 6f 77 6e 20 73 74 61  fine our own sta
198d3 74 69 63 20 63 6f 6e 73 74 61 6e 74 73 20 68 65  tic constants he
198d4 72 65 20 75 73 69 6e 67 20 6e 6f 74 68 69 6e 67  re using nothing
198d5 0a 20 20 2a 2a 20 6c 61 72 67 65 72 20 74 68 61  .  ** larger tha
198d6 6e 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67  n a 32-bit integ
198d7 65 72 20 63 6f 6e 73 74 61 6e 74 2e 0a 20 20 2a  er constant..  *
198d8 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  /.  static const
198d9 20 69 36 34 20 6d 61 78 49 6e 74 20 3d 20 4c 41   i64 maxInt = LA
198da 52 47 45 53 54 5f 49 4e 54 36 34 3b 0a 20 20 73  RGEST_INT64;.  s
198db 74 61 74 69 63 20 63 6f 6e 73 74 20 69 36 34 20  tatic const i64 
198dc 6d 69 6e 49 6e 74 20 3d 20 53 4d 41 4c 4c 45 53  minInt = SMALLES
198dd 54 5f 49 4e 54 36 34 3b 0a 0a 20 20 69 66 28 20  T_INT64;..  if( 
198de 72 3c 28 64 6f 75 62 6c 65 29 6d 69 6e 49 6e 74  r<(double)minInt
198df 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d   ){.    return m
198e0 69 6e 49 6e 74 3b 0a 20 20 7d 65 6c 73 65 20 69  inInt;.  }else i
198e1 66 28 20 72 3e 28 64 6f 75 62 6c 65 29 6d 61 78  f( r>(double)max
198e2 49 6e 74 20 29 7b 0a 20 20 20 20 2f 2a 20 6d 69  Int ){.    /* mi
198e3 6e 49 6e 74 20 69 73 20 63 6f 72 72 65 63 74 20  nInt is correct 
198e4 68 65 72 65 20 2d 20 6e 6f 74 20 6d 61 78 49 6e  here - not maxIn
198e5 74 2e 20 20 49 74 20 74 75 72 6e 73 20 6f 75 74  t.  It turns out
198e6 20 74 68 61 74 20 61 73 73 69 67 6e 69 6e 67 0a   that assigning.
198e7 20 20 20 20 2a 2a 20 61 20 76 65 72 79 20 6c 61      ** a very la
198e8 72 67 65 20 70 6f 73 69 74 69 76 65 20 6e 75 6d  rge positive num
198e9 62 65 72 20 74 6f 20 61 6e 20 69 6e 74 65 67 65  ber to an intege
198ea 72 20 72 65 73 75 6c 74 73 20 69 6e 20 61 20 76  r results in a v
198eb 65 72 79 20 6c 61 72 67 65 0a 20 20 20 20 2a 2a  ery large.    **
198ec 20 6e 65 67 61 74 69 76 65 20 69 6e 74 65 67 65   negative intege
198ed 72 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20 6e  r.  This makes n
198ee 6f 20 73 65 6e 73 65 2c 20 62 75 74 20 69 74 20  o sense, but it 
198ef 69 73 20 77 68 61 74 20 78 38 36 20 68 61 72 64  is what x86 hard
198f0 77 61 72 65 0a 20 20 20 20 2a 2a 20 64 6f 65 73  ware.    ** does
198f1 20 73 6f 20 66 6f 72 20 63 6f 6d 70 61 74 69 62   so for compatib
198f2 69 6c 69 74 79 20 77 65 20 77 69 6c 6c 20 64 6f  ility we will do
198f3 20 74 68 65 20 73 61 6d 65 20 69 6e 20 73 6f 66   the same in sof
198f4 74 77 61 72 65 2e 20 2a 2f 0a 20 20 20 20 72 65  tware. */.    re
198f5 74 75 72 6e 20 6d 69 6e 49 6e 74 3b 0a 20 20 7d  turn minInt;.  }
198f6 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
198f7 20 28 69 36 34 29 72 3b 0a 20 20 7d 0a 7d 0a 0a   (i64)r;.  }.}..
198f8 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 73 6f 6d  /*.** Return som
198f9 65 20 6b 69 6e 64 20 6f 66 20 69 6e 74 65 67 65  e kind of intege
198fa 72 20 76 61 6c 75 65 20 77 68 69 63 68 20 69 73  r value which is
198fb 20 74 68 65 20 62 65 73 74 20 77 65 20 63 61 6e   the best we can
198fc 20 64 6f 0a 2a 2a 20 61 74 20 72 65 70 72 65 73   do.** at repres
198fd 65 6e 74 69 6e 67 20 74 68 65 20 76 61 6c 75 65  enting the value
198fe 20 74 68 61 74 20 2a 70 4d 65 6d 20 64 65 73 63   that *pMem desc
198ff 72 69 62 65 73 20 61 73 20 61 6e 20 69 6e 74 65  ribes as an inte
19900 67 65 72 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20  ger..** If pMem 
19901 69 73 20 61 6e 20 69 6e 74 65 67 65 72 2c 20 74  is an integer, t
19902 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 69 73  hen the value is
19903 20 65 78 61 63 74 2e 20 20 49 66 20 70 4d 65 6d   exact.  If pMem
19904 20 69 73 0a 2a 2a 20 61 20 66 6c 6f 61 74 69 6e   is.** a floatin
19905 67 2d 70 6f 69 6e 74 20 74 68 65 6e 20 74 68 65  g-point then the
19906 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
19907 69 73 20 74 68 65 20 69 6e 74 65 67 65 72 20 70  is the integer p
19908 61 72 74 2e 0a 2a 2a 20 49 66 20 70 4d 65 6d 20  art..** If pMem 
19909 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20 62  is a string or b
1990a 6c 6f 62 2c 20 74 68 65 6e 20 77 65 20 6d 61 6b  lob, then we mak
1990b 65 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20  e an attempt to 
1990c 63 6f 6e 76 65 72 74 0a 2a 2a 20 69 74 20 69 6e  convert.** it in
1990d 74 6f 20 61 20 69 6e 74 65 67 65 72 20 61 6e 64  to a integer and
1990e 20 72 65 74 75 72 6e 20 74 68 61 74 2e 20 20 49   return that.  I
1990f 66 20 70 4d 65 6d 20 72 65 70 72 65 73 65 6e 74  f pMem represent
19910 73 20 61 6e 0a 2a 2a 20 61 6e 20 53 51 4c 2d 4e  s an.** an SQL-N
19911 55 4c 4c 20 76 61 6c 75 65 2c 20 72 65 74 75 72  ULL value, retur
19912 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d  n 0..**.** If pM
19913 65 6d 20 72 65 70 72 65 73 65 6e 74 73 20 61 20  em represents a 
19914 73 74 72 69 6e 67 20 76 61 6c 75 65 2c 20 69 74  string value, it
19915 73 20 65 6e 63 6f 64 69 6e 67 20 6d 69 67 68 74  s encoding might
19916 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a   be changed..*/.
19917 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
19918 36 34 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e  64 sqlite3VdbeIn
19919 74 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d  tValue(Mem *pMem
1991a 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a  ){.  int flags;.
1991b 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
1991c 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33  db==0 || sqlite3
1991d 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d  _mutex_held(pMem
1991e 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
1991f 20 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f    assert( EIGHT_
19920 42 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70  BYTE_ALIGNMENT(p
19921 4d 65 6d 29 20 29 3b 0a 20 20 66 6c 61 67 73 20  Mem) );.  flags 
19922 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20  = pMem->flags;. 
19923 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d   if( flags & MEM
19924 5f 49 6e 74 20 29 7b 0a 20 20 20 20 72 65 74 75  _Int ){.    retu
19925 72 6e 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20  rn pMem->u.i;.  
19926 7d 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20  }else if( flags 
19927 26 20 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20  & MEM_Real ){.  
19928 20 20 72 65 74 75 72 6e 20 64 6f 75 62 6c 65 54    return doubleT
19929 6f 49 6e 74 36 34 28 70 4d 65 6d 2d 3e 72 29 3b  oInt64(pMem->r);
1992a 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 66 6c 61  .  }else if( fla
1992b 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45  gs & (MEM_Str|ME
1992c 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20 69  M_Blob) ){.    i
1992d 36 34 20 76 61 6c 75 65 3b 0a 20 20 20 20 70 4d  64 value;.    pM
1992e 65 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d  em->flags |= MEM
1992f 5f 53 74 72 3b 0a 20 20 20 20 69 66 28 20 73 71  _Str;.    if( sq
19930 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
19931 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51  ncoding(pMem, SQ
19932 4c 49 54 45 5f 55 54 46 38 29 0a 20 20 20 20 20  LITE_UTF8).     
19933 20 20 7c 7c 20 73 71 6c 69 74 65 33 56 64 62 65    || sqlite3Vdbe
19934 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28  MemNulTerminate(
19935 70 4d 65 6d 29 20 29 7b 0a 20 20 20 20 20 20 72  pMem) ){.      r
19936 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20  eturn 0;.    }. 
19937 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d     assert( pMem-
19938 3e 7a 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  >z );.    sqlite
19939 33 41 74 6f 69 36 34 28 70 4d 65 6d 2d 3e 7a 2c  3Atoi64(pMem->z,
1993a 20 26 76 61 6c 75 65 29 3b 0a 20 20 20 20 72 65   &value);.    re
1993b 74 75 72 6e 20 76 61 6c 75 65 3b 0a 20 20 7d 65  turn value;.  }e
1993c 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
1993d 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0;.  }.}../*.** 
1993e 52 65 74 75 72 6e 20 74 68 65 20 62 65 73 74 20  Return the best 
1993f 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f  representation o
19940 66 20 70 4d 65 6d 20 74 68 61 74 20 77 65 20 63  f pMem that we c
19941 61 6e 20 67 65 74 20 69 6e 74 6f 20 61 0a 2a 2a  an get into a.**
19942 20 64 6f 75 62 6c 65 2e 20 20 49 66 20 70 4d 65   double.  If pMe
19943 6d 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 64  m is already a d
19944 6f 75 62 6c 65 20 6f 72 20 61 6e 20 69 6e 74 65  ouble or an inte
19945 67 65 72 2c 20 72 65 74 75 72 6e 20 69 74 73 0a  ger, return its.
19946 2a 2a 20 76 61 6c 75 65 2e 20 20 49 66 20 69 74  ** value.  If it
19947 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 72 20   is a string or 
19948 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f 6e  blob, try to con
19949 76 65 72 74 20 69 74 20 74 6f 20 61 20 64 6f 75  vert it to a dou
1994a 62 6c 65 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73  ble..** If it is
1994b 20 61 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20   a NULL, return 
1994c 30 2e 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  0.0..*/.SQLITE_P
1994d 52 49 56 41 54 45 20 64 6f 75 62 6c 65 20 73 71  RIVATE double sq
1994e 6c 69 74 65 33 56 64 62 65 52 65 61 6c 56 61 6c  lite3VdbeRealVal
1994f 75 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20  ue(Mem *pMem){. 
19950 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64   assert( pMem->d
19951 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
19952 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d  mutex_held(pMem-
19953 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
19954 20 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42   assert( EIGHT_B
19955 59 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d  YTE_ALIGNMENT(pM
19956 65 6d 29 20 29 3b 0a 20 20 69 66 28 20 70 4d 65  em) );.  if( pMe
19957 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52  m->flags & MEM_R
19958 65 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72  eal ){.    retur
19959 6e 20 70 4d 65 6d 2d 3e 72 3b 0a 20 20 7d 65 6c  n pMem->r;.  }el
1995a 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61  se if( pMem->fla
1995b 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a  gs & MEM_Int ){.
1995c 20 20 20 20 72 65 74 75 72 6e 20 28 64 6f 75 62      return (doub
1995d 6c 65 29 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20  le)pMem->u.i;.  
1995e 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e  }else if( pMem->
1995f 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72  flags & (MEM_Str
19960 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20  |MEM_Blob) ){.  
19961 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30 20 49    /* (double)0 I
19962 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54 45  n case of SQLITE
19963 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
19964 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20 64  OINT... */.    d
19965 6f 75 62 6c 65 20 76 61 6c 20 3d 20 28 64 6f 75  ouble val = (dou
19966 62 6c 65 29 30 3b 0a 20 20 20 20 70 4d 65 6d 2d  ble)0;.    pMem-
19967 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74  >flags |= MEM_St
19968 72 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  r;.    if( sqlit
19969 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
1996a 64 69 6e 67 28 70 4d 65 6d 2c 20 53 51 4c 49 54  ding(pMem, SQLIT
1996b 45 5f 55 54 46 38 29 0a 20 20 20 20 20 20 20 7c  E_UTF8).       |
1996c 7c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  | sqlite3VdbeMem
1996d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 4d 65  NulTerminate(pMe
1996e 6d 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 28  m) ){.      /* (
1996f 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61 73 65  double)0 In case
19970 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   of SQLITE_OMIT_
19971 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 2e 2e  FLOATING_POINT..
19972 2e 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72  . */.      retur
19973 6e 20 28 64 6f 75 62 6c 65 29 30 3b 0a 20 20 20  n (double)0;.   
19974 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
19975 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20 73 71  Mem->z );.    sq
19976 6c 69 74 65 33 41 74 6f 46 28 70 4d 65 6d 2d 3e  lite3AtoF(pMem->
19977 7a 2c 20 26 76 61 6c 29 3b 0a 20 20 20 20 72 65  z, &val);.    re
19978 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d 65 6c 73  turn val;.  }els
19979 65 7b 0a 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c  e{.    /* (doubl
1997a 65 29 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53  e)0 In case of S
1997b 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
1997c 49 4e 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a  ING_POINT... */.
1997d 20 20 20 20 72 65 74 75 72 6e 20 28 64 6f 75 62      return (doub
1997e 6c 65 29 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  le)0;.  }.}../*.
1997f 2a 2a 20 54 68 65 20 4d 45 4d 20 73 74 72 75 63  ** The MEM struc
19980 74 75 72 65 20 69 73 20 61 6c 72 65 61 64 79 20  ture is already 
19981 61 20 4d 45 4d 5f 52 65 61 6c 2e 20 20 54 72 79  a MEM_Real.  Try
19982 20 74 6f 20 61 6c 73 6f 20 6d 61 6b 65 20 69 74   to also make it
19983 20 61 0a 2a 2a 20 4d 45 4d 5f 49 6e 74 20 69 66   a.** MEM_Int if
19984 20 77 65 20 63 61 6e 2e 0a 2a 2f 0a 53 51 4c 49   we can..*/.SQLI
19985 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
19986 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67  sqlite3VdbeInteg
19987 65 72 41 66 66 69 6e 69 74 79 28 4d 65 6d 20 2a  erAffinity(Mem *
19988 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28  pMem){.  assert(
19989 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d   pMem->flags & M
1998a 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 61 73 73  EM_Real );.  ass
1998b 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67  ert( (pMem->flag
1998c 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d  s & MEM_RowSet)=
1998d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1998e 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pMem->db==0 || s
1998f 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
19990 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65  d(pMem->db->mute
19991 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  x) );.  assert( 
19992 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e  EIGHT_BYTE_ALIGN
19993 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 20  MENT(pMem) );.. 
19994 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 64 6f 75   pMem->u.i = dou
19995 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d 2d  bleToInt64(pMem-
19996 3e 72 29 3b 0a 0a 20 20 2f 2a 20 4f 6e 6c 79 20  >r);..  /* Only 
19997 6d 61 72 6b 20 74 68 65 20 76 61 6c 75 65 20 61  mark the value a
19998 73 20 61 6e 20 69 6e 74 65 67 65 72 20 69 66 0a  s an integer if.
19999 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 28 31 29    **.  **    (1)
1999a 20 74 68 65 20 72 6f 75 6e 64 2d 74 72 69 70 20   the round-trip 
1999b 63 6f 6e 76 65 72 73 69 6f 6e 20 72 65 61 6c 2d  conversion real-
1999c 3e 69 6e 74 2d 3e 72 65 61 6c 20 69 73 20 61 20  >int->real is a 
1999d 6e 6f 2d 6f 70 2c 20 61 6e 64 0a 20 20 2a 2a 20  no-op, and.  ** 
1999e 20 20 20 28 32 29 20 54 68 65 20 69 6e 74 65 67     (2) The integ
1999f 65 72 20 69 73 20 6e 65 69 74 68 65 72 20 74 68  er is neither th
199a0 65 20 6c 61 72 67 65 73 74 20 6e 6f 72 20 74 68  e largest nor th
199a1 65 20 73 6d 61 6c 6c 65 73 74 0a 20 20 2a 2a 20  e smallest.  ** 
199a2 20 20 20 20 20 20 20 70 6f 73 73 69 62 6c 65 20         possible 
199a3 69 6e 74 65 67 65 72 20 28 74 69 63 6b 65 74 20  integer (ticket 
199a4 23 33 39 32 32 29 0a 20 20 2a 2a 0a 20 20 2a 2a  #3922).  **.  **
199a5 20 54 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20   The second and 
199a6 74 68 69 72 64 20 74 65 72 6d 73 20 69 6e 20 74  third terms in t
199a7 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e  he following con
199a8 64 69 74 69 6f 6e 61 6c 20 65 6e 66 6f 72 63 65  ditional enforce
199a9 73 0a 20 20 2a 2a 20 74 68 65 20 73 65 63 6f 6e  s.  ** the secon
199aa 64 20 63 6f 6e 64 69 74 69 6f 6e 20 75 6e 64 65  d condition unde
199ab 72 20 74 68 65 20 61 73 73 75 6d 70 74 69 6f 6e  r the assumption
199ac 20 74 68 61 74 20 61 64 64 69 74 69 6f 6e 20 6f   that addition o
199ad 76 65 72 66 6c 6f 77 20 63 61 75 73 65 73 0a 20  verflow causes. 
199ae 20 2a 2a 20 76 61 6c 75 65 73 20 74 6f 20 77 72   ** values to wr
199af 61 70 20 61 72 6f 75 6e 64 2e 20 20 4f 6e 20 78  ap around.  On x
199b0 38 36 20 68 61 72 64 77 61 72 65 2c 20 74 68 65  86 hardware, the
199b1 20 74 68 69 72 64 20 74 65 72 6d 20 69 73 20 61   third term is a
199b2 6c 77 61 79 73 0a 20 20 2a 2a 20 74 72 75 65 20  lways.  ** true 
199b3 61 6e 64 20 63 6f 75 6c 64 20 62 65 20 6f 6d 69  and could be omi
199b4 74 74 65 64 2e 20 20 42 75 74 20 77 65 20 6c 65  tted.  But we le
199b5 61 76 65 20 69 74 20 69 6e 20 62 65 63 61 75 73  ave it in becaus
199b6 65 20 6f 74 68 65 72 0a 20 20 2a 2a 20 61 72 63  e other.  ** arc
199b7 68 69 74 65 63 74 75 72 65 73 20 6d 69 67 68 74  hitectures might
199b8 20 62 65 68 61 76 65 20 64 69 66 66 65 72 65 6e   behave differen
199b9 74 6c 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  tly..  */.  if( 
199ba 70 4d 65 6d 2d 3e 72 3d 3d 28 64 6f 75 62 6c 65  pMem->r==(double
199bb 29 70 4d 65 6d 2d 3e 75 2e 69 20 26 26 20 70 4d  )pMem->u.i && pM
199bc 65 6d 2d 3e 75 2e 69 3e 53 4d 41 4c 4c 45 53 54  em->u.i>SMALLEST
199bd 5f 49 4e 54 36 34 0a 20 20 20 20 20 20 26 26 20  _INT64.      && 
199be 41 4c 57 41 59 53 28 70 4d 65 6d 2d 3e 75 2e 69  ALWAYS(pMem->u.i
199bf 3c 4c 41 52 47 45 53 54 5f 49 4e 54 36 34 29 20  <LARGEST_INT64) 
199c0 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  ){.    pMem->fla
199c1 67 73 20 7c 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20  gs |= MEM_Int;. 
199c2 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76   }.}../*.** Conv
199c3 65 72 74 20 70 4d 65 6d 20 74 6f 20 74 79 70 65  ert pMem to type
199c4 20 69 6e 74 65 67 65 72 2e 20 20 49 6e 76 61 6c   integer.  Inval
199c5 69 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20  idate any prior 
199c6 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e  representations.
199c7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
199c8 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
199c9 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28  beMemIntegerify(
199ca 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73  Mem *pMem){.  as
199cb 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d  sert( pMem->db==
199cc 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74  0 || sqlite3_mut
199cd 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62  ex_held(pMem->db
199ce 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
199cf 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
199d0 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29  gs & MEM_RowSet)
199d1 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
199d2 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47   EIGHT_BYTE_ALIG
199d3 4e 4d 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a  NMENT(pMem) );..
199d4 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 73 71    pMem->u.i = sq
199d5 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
199d6 65 28 70 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65  e(pMem);.  MemSe
199d7 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20  tTypeFlag(pMem, 
199d8 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 72 65 74 75  MEM_Int);.  retu
199d9 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
199da 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70  ./*.** Convert p
199db 4d 65 6d 20 73 6f 20 74 68 61 74 20 69 74 20 69  Mem so that it i
199dc 73 20 6f 66 20 74 79 70 65 20 4d 45 4d 5f 52 65  s of type MEM_Re
199dd 61 6c 2e 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74  al..** Invalidat
199de 65 20 61 6e 79 20 70 72 69 6f 72 20 72 65 70 72  e any prior repr
199df 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a  esentations..*/.
199e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
199e1 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
199e2 6d 52 65 61 6c 69 66 79 28 4d 65 6d 20 2a 70 4d  mRealify(Mem *pM
199e3 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  em){.  assert( p
199e4 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
199e5 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
199e6 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
199e7 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 45  ) );.  assert( E
199e8 49 47 48 54 5f 42 59 54 45 5f 41 4c 49 47 4e 4d  IGHT_BYTE_ALIGNM
199e9 45 4e 54 28 70 4d 65 6d 29 20 29 3b 0a 0a 20 20  ENT(pMem) );..  
199ea 70 4d 65 6d 2d 3e 72 20 3d 20 73 71 6c 69 74 65  pMem->r = sqlite
199eb 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70  3VdbeRealValue(p
199ec 4d 65 6d 29 3b 0a 20 20 4d 65 6d 53 65 74 54 79  Mem);.  MemSetTy
199ed 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d  peFlag(pMem, MEM
199ee 5f 52 65 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e  _Real);.  return
199ef 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
199f0 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 70 4d 65  *.** Convert pMe
199f1 6d 20 73 6f 20 74 68 61 74 20 69 74 20 68 61 73  m so that it has
199f2 20 74 79 70 65 73 20 4d 45 4d 5f 52 65 61 6c 20   types MEM_Real 
199f3 6f 72 20 4d 45 4d 5f 49 6e 74 20 6f 72 20 62 6f  or MEM_Int or bo
199f4 74 68 2e 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74  th..** Invalidat
199f5 65 20 61 6e 79 20 70 72 69 6f 72 20 72 65 70 72  e any prior repr
199f6 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a  esentations..*/.
199f7 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
199f8 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
199f9 6d 4e 75 6d 65 72 69 66 79 28 4d 65 6d 20 2a 70  mNumerify(Mem *p
199fa 4d 65 6d 29 7b 0a 20 20 64 6f 75 62 6c 65 20 72  Mem){.  double r
199fb 31 2c 20 72 32 3b 0a 20 20 69 36 34 20 69 3b 0a  1, r2;.  i64 i;.
199fc 20 20 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d    assert( (pMem-
199fd 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e  >flags & (MEM_In
199fe 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 4e  t|MEM_Real|MEM_N
199ff 75 6c 6c 29 29 3d 3d 30 20 29 3b 0a 20 20 61 73  ull))==0 );.  as
19a00 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
19a01 67 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d  gs & (MEM_Blob|M
19a02 45 4d 5f 53 74 72 29 29 21 3d 30 20 29 3b 0a 20  EM_Str))!=0 );. 
19a03 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64   assert( pMem->d
19a04 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
19a05 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d  mutex_held(pMem-
19a06 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
19a07 20 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62   r1 = sqlite3Vdb
19a08 65 52 65 61 6c 56 61 6c 75 65 28 70 4d 65 6d 29  eRealValue(pMem)
19a09 3b 0a 20 20 69 20 3d 20 64 6f 75 62 6c 65 54 6f  ;.  i = doubleTo
19a0a 49 6e 74 36 34 28 72 31 29 3b 0a 20 20 72 32 20  Int64(r1);.  r2 
19a0b 3d 20 28 64 6f 75 62 6c 65 29 69 3b 0a 20 20 69  = (double)i;.  i
19a0c 66 28 20 72 31 3d 3d 72 32 20 29 7b 0a 20 20 20  f( r1==r2 ){.   
19a0d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49   sqlite3VdbeMemI
19a0e 6e 74 65 67 65 72 69 66 79 28 70 4d 65 6d 29 3b  ntegerify(pMem);
19a0f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4d  .  }else{.    pM
19a10 65 6d 2d 3e 72 20 3d 20 72 31 3b 0a 20 20 20 20  em->r = r1;.    
19a11 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
19a12 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a  Mem, MEM_Real);.
19a13 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
19a14 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
19a15 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76   Delete any prev
19a16 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73  ious value and s
19a17 65 74 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f  et the value sto
19a18 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f 20  red in *pMem to 
19a19 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  NULL..*/.SQLITE_
19a1a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
19a1b 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
19a1c 6c 6c 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20  ll(Mem *pMem){. 
19a1d 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   if( pMem->flags
19a1e 20 26 20 4d 45 4d 5f 46 72 61 6d 65 20 29 7b 0a   & MEM_Frame ){.
19a1f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46      sqlite3VdbeF
19a20 72 61 6d 65 44 65 6c 65 74 65 28 70 4d 65 6d 2d  rameDelete(pMem-
19a21 3e 75 2e 70 46 72 61 6d 65 29 3b 0a 20 20 7d 0a  >u.pFrame);.  }.
19a22 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67    if( pMem->flag
19a23 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 20 29  s & MEM_RowSet )
19a24 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 6f 77  {.    sqlite3Row
19a25 53 65 74 43 6c 65 61 72 28 70 4d 65 6d 2d 3e 75  SetClear(pMem->u
19a26 2e 70 52 6f 77 53 65 74 29 3b 0a 20 20 7d 0a 20  .pRowSet);.  }. 
19a27 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28   MemSetTypeFlag(
19a28 70 4d 65 6d 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b  pMem, MEM_Null);
19a29 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20  .  pMem->type = 
19a2a 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 7d 0a 0a  SQLITE_NULL;.}..
19a2b 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79  /*.** Delete any
19a2c 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20   previous value 
19a2d 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75  and set the valu
19a2e 65 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20 6f  e to be a BLOB o
19a2f 66 20 6c 65 6e 67 74 68 0a 2a 2a 20 6e 20 63 6f  f length.** n co
19a30 6e 74 61 69 6e 69 6e 67 20 61 6c 6c 20 7a 65 72  ntaining all zer
19a31 6f 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  os..*/.SQLITE_PR
19a32 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
19a33 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f  e3VdbeMemSetZero
19a34 42 6c 6f 62 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20  Blob(Mem *pMem, 
19a35 69 6e 74 20 6e 29 7b 0a 20 20 73 71 6c 69 74 65  int n){.  sqlite
19a36 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
19a37 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e 66  pMem);.  pMem->f
19a38 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c  lags = MEM_Blob|
19a39 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 70 4d 65 6d  MEM_Zero;.  pMem
19a3a 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
19a3b 42 4c 4f 42 3b 0a 20 20 70 4d 65 6d 2d 3e 6e 20  BLOB;.  pMem->n 
19a3c 3d 20 30 3b 0a 20 20 69 66 28 20 6e 3c 30 20 29  = 0;.  if( n<0 )
19a3d 20 6e 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e   n = 0;.  pMem->
19a3e 75 2e 6e 5a 65 72 6f 20 3d 20 6e 3b 0a 20 20 70  u.nZero = n;.  p
19a3f 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54  Mem->enc = SQLIT
19a40 45 5f 55 54 46 38 3b 0a 0a 23 69 66 64 65 66 20  E_UTF8;..#ifdef 
19a41 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
19a42 42 4c 4f 42 0a 20 20 73 71 6c 69 74 65 33 56 64  BLOB.  sqlite3Vd
19a43 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20  beMemGrow(pMem, 
19a44 6e 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 4d 65  n, 0);.  if( pMe
19a45 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 70 4d 65 6d  m->z ){.    pMem
19a46 2d 3e 6e 20 3d 20 6e 3b 0a 20 20 20 20 6d 65 6d  ->n = n;.    mem
19a47 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30 2c 20  set(pMem->z, 0, 
19a48 6e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d  n);.  }.#endif.}
19a49 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61  ../*.** Delete a
19a4a 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75  ny previous valu
19a4b 65 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61  e and set the va
19a4c 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70  lue stored in *p
19a4d 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d  Mem to val,.** m
19a4e 61 6e 69 66 65 73 74 20 74 79 70 65 20 49 4e 54  anifest type INT
19a4f 45 47 45 52 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  EGER..*/.SQLITE_
19a50 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
19a51 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e  ite3VdbeMemSetIn
19a52 74 36 34 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69  t64(Mem *pMem, i
19a53 36 34 20 76 61 6c 29 7b 0a 20 20 73 71 6c 69 74  64 val){.  sqlit
19a54 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
19a55 28 70 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e  (pMem);.  pMem->
19a56 75 2e 69 20 3d 20 76 61 6c 3b 0a 20 20 70 4d 65  u.i = val;.  pMe
19a57 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49  m->flags = MEM_I
19a58 6e 74 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65  nt;.  pMem->type
19a59 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45   = SQLITE_INTEGE
19a5a 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65  R;.}../*.** Dele
19a5b 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20  te any previous 
19a5c 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68  value and set th
19a5d 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69  e value stored i
19a5e 6e 20 2a 70 4d 65 6d 20 74 6f 20 76 61 6c 2c 0a  n *pMem to val,.
19a5f 2a 2a 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65  ** manifest type
19a60 20 52 45 41 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45   REAL..*/.SQLITE
19a61 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
19a62 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44  lite3VdbeMemSetD
19a63 6f 75 62 6c 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c  ouble(Mem *pMem,
19a64 20 64 6f 75 62 6c 65 20 76 61 6c 29 7b 0a 20 20   double val){.  
19a65 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e  if( sqlite3IsNaN
19a66 28 76 61 6c 29 20 29 7b 0a 20 20 20 20 73 71 6c  (val) ){.    sql
19a67 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
19a68 6c 6c 28 70 4d 65 6d 29 3b 0a 20 20 7d 65 6c 73  ll(pMem);.  }els
19a69 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  e{.    sqlite3Vd
19a6a 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65  beMemRelease(pMe
19a6b 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 72 20  m);.    pMem->r 
19a6c 3d 20 76 61 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d  = val;.    pMem-
19a6d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 52 65 61  >flags = MEM_Rea
19a6e 6c 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70  l;.    pMem->typ
19a6f 65 20 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54  e = SQLITE_FLOAT
19a70 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  ;.  }.}../*.** D
19a71 65 6c 65 74 65 20 61 6e 79 20 70 72 65 76 69 6f  elete any previo
19a72 75 73 20 76 61 6c 75 65 20 61 6e 64 20 73 65 74  us value and set
19a73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 70 4d   the value of pM
19a74 65 6d 20 74 6f 20 62 65 20 61 6e 0a 2a 2a 20 65  em to be an.** e
19a75 6d 70 74 79 20 62 6f 6f 6c 65 61 6e 20 69 6e 64  mpty boolean ind
19a76 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ex..*/.SQLITE_PR
19a77 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
19a78 65 33 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53  e3VdbeMemSetRowS
19a79 65 74 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20  et(Mem *pMem){. 
19a7a 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
19a7b 4d 65 6d 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72  Mem->db;.  asser
19a7c 74 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73  t( db!=0 );.  as
19a7d 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
19a7e 67 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29  gs & MEM_RowSet)
19a7f 3d 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ==0 );.  sqlite3
19a80 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
19a81 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e 7a 4d  Mem);.  pMem->zM
19a82 61 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44  alloc = sqlite3D
19a83 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 36  bMallocRaw(db, 6
19a84 34 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  4);.  if( db->ma
19a85 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
19a86 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
19a87 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73  MEM_Null;.  }els
19a88 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
19a89 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a  Mem->zMalloc );.
19a8a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 70 52 6f 77      pMem->u.pRow
19a8b 53 65 74 20 3d 20 73 71 6c 69 74 65 33 52 6f 77  Set = sqlite3Row
19a8c 53 65 74 49 6e 69 74 28 64 62 2c 20 70 4d 65 6d  SetInit(db, pMem
19a8d 2d 3e 7a 4d 61 6c 6c 6f 63 2c 20 0a 20 20 20 20  ->zMalloc, .    
19a8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19a8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19a90 20 20 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c     sqlite3DbMall
19a91 6f 63 53 69 7a 65 28 64 62 2c 20 70 4d 65 6d 2d  ocSize(db, pMem-
19a92 3e 7a 4d 61 6c 6c 6f 63 29 29 3b 0a 20 20 20 20  >zMalloc));.    
19a93 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 75 2e  assert( pMem->u.
19a94 70 52 6f 77 53 65 74 21 3d 30 20 29 3b 0a 20 20  pRowSet!=0 );.  
19a95 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
19a96 4d 45 4d 5f 52 6f 77 53 65 74 3b 0a 20 20 7d 0a  MEM_RowSet;.  }.
19a97 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
19a98 74 72 75 65 20 69 66 20 74 68 65 20 4d 65 6d 20  true if the Mem 
19a99 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e 73 20  object contains 
19a9a 61 20 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 74  a TEXT or BLOB t
19a9b 68 61 74 20 69 73 0a 2a 2a 20 74 6f 6f 20 6c 61  hat is.** too la
19a9c 72 67 65 20 2d 20 77 68 6f 73 65 20 73 69 7a 65  rge - whose size
19a9d 20 65 78 63 65 65 64 73 20 53 51 4c 49 54 45 5f   exceeds SQLITE_
19a9e 4d 41 58 5f 4c 45 4e 47 54 48 2e 0a 2a 2f 0a 53  MAX_LENGTH..*/.S
19a9f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
19aa0 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
19aa1 54 6f 6f 42 69 67 28 4d 65 6d 20 2a 70 29 7b 0a  TooBig(Mem *p){.
19aa2 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 21    assert( p->db!
19aa3 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66  =0 );.  if( p->f
19aa4 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c  lags & (MEM_Str|
19aa5 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20  MEM_Blob) ){.   
19aa6 20 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e 3b 0a 20   int n = p->n;. 
19aa7 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20     if( p->flags 
19aa8 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20  & MEM_Zero ){.  
19aa9 20 20 20 20 6e 20 2b 3d 20 70 2d 3e 75 2e 6e 5a      n += p->u.nZ
19aaa 65 72 6f 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  ero;.    }.    r
19aab 65 74 75 72 6e 20 6e 3e 70 2d 3e 64 62 2d 3e 61  eturn n>p->db->a
19aac 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
19aad 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a  IT_LENGTH];.  }.
19aae 20 20 72 65 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a    return 0; .}..
19aaf 2f 2a 0a 2a 2a 20 53 69 7a 65 20 6f 66 20 73 74  /*.** Size of st
19ab0 72 75 63 74 20 4d 65 6d 20 6e 6f 74 20 69 6e 63  ruct Mem not inc
19ab1 6c 75 64 69 6e 67 20 74 68 65 20 4d 65 6d 2e 7a  luding the Mem.z
19ab2 4d 61 6c 6c 6f 63 20 6d 65 6d 62 65 72 2e 0a 2a  Malloc member..*
19ab3 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 43 45 4c  /.#define MEMCEL
19ab4 4c 53 49 5a 45 20 28 73 69 7a 65 5f 74 29 28 26  LSIZE (size_t)(&
19ab5 28 28 28 4d 65 6d 20 2a 29 30 29 2d 3e 7a 4d 61  (((Mem *)0)->zMa
19ab6 6c 6c 6f 63 29 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61  lloc))../*.** Ma
19ab7 6b 65 20 61 6e 20 73 68 61 6c 6c 6f 77 20 63 6f  ke an shallow co
19ab8 70 79 20 6f 66 20 70 46 72 6f 6d 20 69 6e 74 6f  py of pFrom into
19ab9 20 70 54 6f 2e 20 20 50 72 69 6f 72 20 63 6f 6e   pTo.  Prior con
19aba 74 65 6e 74 73 20 6f 66 0a 2a 2a 20 70 54 6f 20  tents of.** pTo 
19abb 61 72 65 20 66 72 65 65 64 2e 20 20 54 68 65 20  are freed.  The 
19abc 70 46 72 6f 6d 2d 3e 7a 20 66 69 65 6c 64 20 69  pFrom->z field i
19abd 73 20 6e 6f 74 20 64 75 70 6c 69 63 61 74 65 64  s not duplicated
19abe 2e 20 20 49 66 0a 2a 2a 20 70 46 72 6f 6d 2d 3e  .  If.** pFrom->
19abf 7a 20 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20  z is used, then 
19ac0 70 54 6f 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f  pTo->z points to
19ac1 20 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 20   the same thing 
19ac2 61 73 20 70 46 72 6f 6d 2d 3e 7a 0a 2a 2a 20 61  as pFrom->z.** a
19ac3 6e 64 20 66 6c 61 67 73 20 67 65 74 73 20 73 72  nd flags gets sr
19ac4 63 54 79 70 65 20 28 65 69 74 68 65 72 20 4d 45  cType (either ME
19ac5 4d 5f 45 70 68 65 6d 20 6f 72 20 4d 45 4d 5f 53  M_Ephem or MEM_S
19ac6 74 61 74 69 63 29 2e 0a 2a 2f 0a 53 51 4c 49 54  tatic)..*/.SQLIT
19ac7 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
19ac8 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61  qlite3VdbeMemSha
19ac9 6c 6c 6f 77 43 6f 70 79 28 4d 65 6d 20 2a 70 54  llowCopy(Mem *pT
19aca 6f 2c 20 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46  o, const Mem *pF
19acb 72 6f 6d 2c 20 69 6e 74 20 73 72 63 54 79 70 65  rom, int srcType
19acc 29 7b 0a 20 20 61 73 73 65 72 74 28 20 28 70 46  ){.  assert( (pF
19acd 72 6f 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  rom->flags & MEM
19ace 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a 20  _RowSet)==0 );. 
19acf 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
19ad0 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70  eleaseExternal(p
19ad1 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54  To);.  memcpy(pT
19ad2 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c  o, pFrom, MEMCEL
19ad3 4c 53 49 5a 45 29 3b 0a 20 20 70 54 6f 2d 3e 78  LSIZE);.  pTo->x
19ad4 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 28  Del = 0;.  if( (
19ad5 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d  pFrom->flags&MEM
19ad6 5f 44 79 6e 29 21 3d 30 20 7c 7c 20 70 46 72 6f  _Dyn)!=0 || pFro
19ad7 6d 2d 3e 7a 3d 3d 70 46 72 6f 6d 2d 3e 7a 4d 61  m->z==pFrom->zMa
19ad8 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 70 54 6f 2d  lloc ){.    pTo-
19ad9 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f  >flags &= ~(MEM_
19ada 44 79 6e 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d  Dyn|MEM_Static|M
19adb 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 61  EM_Ephem);.    a
19adc 73 73 65 72 74 28 20 73 72 63 54 79 70 65 3d 3d  ssert( srcType==
19add 4d 45 4d 5f 45 70 68 65 6d 20 7c 7c 20 73 72 63  MEM_Ephem || src
19ade 54 79 70 65 3d 3d 4d 45 4d 5f 53 74 61 74 69 63  Type==MEM_Static
19adf 20 29 3b 0a 20 20 20 20 70 54 6f 2d 3e 66 6c 61   );.    pTo->fla
19ae0 67 73 20 7c 3d 20 73 72 63 54 79 70 65 3b 0a 20  gs |= srcType;. 
19ae1 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65   }.}../*.** Make
19ae2 20 61 20 66 75 6c 6c 20 63 6f 70 79 20 6f 66 20   a full copy of 
19ae3 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e 20  pFrom into pTo. 
19ae4 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73 20   Prior contents 
19ae5 6f 66 20 70 54 6f 20 61 72 65 0a 2a 2a 20 66 72  of pTo are.** fr
19ae6 65 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 63  eed before the c
19ae7 6f 70 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2f 0a  opy is made..*/.
19ae8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
19ae9 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
19aea 6d 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20  mCopy(Mem *pTo, 
19aeb 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d  const Mem *pFrom
19aec 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
19aed 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 61 73 73 65  LITE_OK;..  asse
19aee 72 74 28 20 28 70 46 72 6f 6d 2d 3e 66 6c 61 67  rt( (pFrom->flag
19aef 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d  s & MEM_RowSet)=
19af0 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  =0 );.  sqlite3V
19af1 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74  dbeMemReleaseExt
19af2 65 72 6e 61 6c 28 70 54 6f 29 3b 0a 20 20 6d 65  ernal(pTo);.  me
19af3 6d 63 70 79 28 70 54 6f 2c 20 70 46 72 6f 6d 2c  mcpy(pTo, pFrom,
19af4 20 4d 45 4d 43 45 4c 4c 53 49 5a 45 29 3b 0a 20   MEMCELLSIZE);. 
19af5 20 70 54 6f 2d 3e 66 6c 61 67 73 20 26 3d 20 7e   pTo->flags &= ~
19af6 4d 45 4d 5f 44 79 6e 3b 0a 0a 20 20 69 66 28 20  MEM_Dyn;..  if( 
19af7 70 54 6f 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f  pTo->flags&(MEM_
19af8 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b  Str|MEM_Blob) ){
19af9 0a 20 20 20 20 69 66 28 20 30 3d 3d 28 70 46 72  .    if( 0==(pFr
19afa 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74  om->flags&MEM_St
19afb 61 74 69 63 29 20 29 7b 0a 20 20 20 20 20 20 70  atic) ){.      p
19afc 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d  To->flags |= MEM
19afd 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20 20 72 63  _Ephem;.      rc
19afe 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65   = sqlite3VdbeMe
19aff 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70  mMakeWriteable(p
19b00 54 6f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  To);.    }.  }..
19b01 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
19b02 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 74  /*.** Transfer t
19b03 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70  he contents of p
19b04 46 72 6f 6d 20 74 6f 20 70 54 6f 2e 20 41 6e 79  From to pTo. Any
19b05 20 65 78 69 73 74 69 6e 67 20 76 61 6c 75 65 20   existing value 
19b06 69 6e 20 70 54 6f 20 69 73 0a 2a 2a 20 66 72 65  in pTo is.** fre
19b07 65 64 2e 20 49 66 20 70 46 72 6f 6d 20 63 6f 6e  ed. If pFrom con
19b08 74 61 69 6e 73 20 65 70 68 65 6d 65 72 61 6c 20  tains ephemeral 
19b09 64 61 74 61 2c 20 61 20 63 6f 70 79 20 69 73 20  data, a copy is 
19b0a 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a 20 70 46 72 6f  made..**.** pFro
19b0b 6d 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 53 51  m contains an SQ
19b0c 4c 20 4e 55 4c 4c 20 77 68 65 6e 20 74 68 69 73  L NULL when this
19b0d 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
19b0e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19b0f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
19b10 56 64 62 65 4d 65 6d 4d 6f 76 65 28 4d 65 6d 20  VdbeMemMove(Mem 
19b11 2a 70 54 6f 2c 20 4d 65 6d 20 2a 70 46 72 6f 6d  *pTo, Mem *pFrom
19b12 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 46 72  ){.  assert( pFr
19b13 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  om->db==0 || sql
19b14 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
19b15 70 46 72 6f 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  pFrom->db->mutex
19b16 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
19b17 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  To->db==0 || sql
19b18 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
19b19 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  pTo->db->mutex) 
19b1a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 72  );.  assert( pFr
19b1b 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 54 6f  om->db==0 || pTo
19b1c 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70 46 72 6f 6d  ->db==0 || pFrom
19b1d 2d 3e 64 62 3d 3d 70 54 6f 2d 3e 64 62 20 29 3b  ->db==pTo->db );
19b1e 0a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ..  sqlite3VdbeM
19b1f 65 6d 52 65 6c 65 61 73 65 28 70 54 6f 29 3b 0a  emRelease(pTo);.
19b20 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46    memcpy(pTo, pF
19b21 72 6f 6d 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29  rom, sizeof(Mem)
19b22 29 3b 0a 20 20 70 46 72 6f 6d 2d 3e 66 6c 61 67  );.  pFrom->flag
19b23 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
19b24 70 46 72 6f 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b  pFrom->xDel = 0;
19b25 0a 20 20 70 46 72 6f 6d 2d 3e 7a 4d 61 6c 6c 6f  .  pFrom->zMallo
19b26 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  c = 0;.}../*.** 
19b27 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65  Change the value
19b28 20 6f 66 20 61 20 4d 65 6d 20 74 6f 20 62 65 20   of a Mem to be 
19b29 61 20 73 74 72 69 6e 67 20 6f 72 20 61 20 42 4c  a string or a BL
19b2a 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65  OB..**.** The me
19b2b 6d 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 20  mory management 
19b2c 73 74 72 61 74 65 67 79 20 64 65 70 65 6e 64 73  strategy depends
19b2d 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66   on the value of
19b2e 20 74 68 65 20 78 44 65 6c 0a 2a 2a 20 70 61 72   the xDel.** par
19b2f 61 6d 65 74 65 72 2e 20 49 66 20 74 68 65 20 76  ameter. If the v
19b30 61 6c 75 65 20 70 61 73 73 65 64 20 69 73 20 53  alue passed is S
19b31 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2c  QLITE_TRANSIENT,
19b32 20 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 73 74   then the .** st
19b33 72 69 6e 67 20 69 73 20 63 6f 70 69 65 64 20 69  ring is copied i
19b34 6e 74 6f 20 61 20 28 70 6f 73 73 69 62 6c 79 20  nto a (possibly 
19b35 65 78 69 73 74 69 6e 67 29 20 62 75 66 66 65 72  existing) buffer
19b36 20 6d 61 6e 61 67 65 64 20 62 79 20 74 68 65 20   managed by the 
19b37 0a 2a 2a 20 4d 65 6d 20 73 74 72 75 63 74 75 72  .** Mem structur
19b38 65 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e  e. Otherwise, an
19b39 79 20 65 78 69 73 74 69 6e 67 20 62 75 66 66 65  y existing buffe
19b3a 72 20 69 73 20 66 72 65 65 64 20 61 6e 64 20 74  r is freed and t
19b3b 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 63 6f  he.** pointer co
19b3c 70 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  pied..**.** If t
19b3d 68 65 20 73 74 72 69 6e 67 20 69 73 20 74 6f 6f  he string is too
19b3e 20 6c 61 72 67 65 20 28 69 66 20 69 74 20 65 78   large (if it ex
19b3f 63 65 65 64 73 20 74 68 65 20 53 51 4c 49 54 45  ceeds the SQLITE
19b40 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 0a 2a 2a  _LIMIT_LENGTH.**
19b41 20 73 69 7a 65 20 6c 69 6d 69 74 29 20 74 68 65   size limit) the
19b42 6e 20 6e 6f 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  n no memory allo
19b43 63 61 74 69 6f 6e 20 6f 63 63 75 72 73 2e 20 20  cation occurs.  
19b44 49 66 20 74 68 65 20 73 74 72 69 6e 67 20 63 61  If the string ca
19b45 6e 20 62 65 0a 2a 2a 20 73 74 6f 72 65 64 20 77  n be.** stored w
19b46 69 74 68 6f 75 74 20 61 6c 6c 6f 63 61 74 69 6e  ithout allocatin
19b47 67 20 6d 65 6d 6f 72 79 2c 20 74 68 65 6e 20 69  g memory, then i
19b48 74 20 69 73 2e 20 20 49 66 20 61 20 6d 65 6d 6f  t is.  If a memo
19b49 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a  ry allocation.**
19b4a 20 69 73 20 72 65 71 75 69 72 65 64 20 74 6f 20   is required to 
19b4b 73 74 6f 72 65 20 74 68 65 20 73 74 72 69 6e 67  store the string
19b4c 2c 20 74 68 65 6e 20 76 61 6c 75 65 20 6f 66 20  , then value of 
19b4d 70 4d 65 6d 20 69 73 20 75 6e 63 68 61 6e 67 65  pMem is unchange
19b4e 64 2e 20 20 49 6e 0a 2a 2a 20 65 69 74 68 65 72  d.  In.** either
19b4f 20 63 61 73 65 2c 20 53 51 4c 49 54 45 5f 54 4f   case, SQLITE_TO
19b50 4f 42 49 47 20 69 73 20 72 65 74 75 72 6e 65 64  OBIG is returned
19b51 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19b52 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
19b53 64 62 65 4d 65 6d 53 65 74 53 74 72 28 0a 20 20  dbeMemSetStr(.  
19b54 4d 65 6d 20 2a 70 4d 65 6d 2c 20 20 20 20 20 20  Mem *pMem,      
19b55 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65      /* Memory ce
19b56 6c 6c 20 74 6f 20 73 65 74 20 74 6f 20 73 74 72  ll to set to str
19b57 69 6e 67 20 76 61 6c 75 65 20 2a 2f 0a 20 20 63  ing value */.  c
19b58 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 20 20  onst char *z,   
19b59 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 70 6f 69     /* String poi
19b5a 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c  nter */.  int n,
19b5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
19b5c 20 42 79 74 65 73 20 69 6e 20 73 74 72 69 6e 67   Bytes in string
19b5d 2c 20 6f 72 20 6e 65 67 61 74 69 76 65 20 2a 2f  , or negative */
19b5e 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20  .  u8 enc,      
19b5f 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69         /* Encodi
19b60 6e 67 20 6f 66 20 7a 2e 20 20 30 20 66 6f 72 20  ng of z.  0 for 
19b61 42 4c 4f 42 73 20 2a 2f 0a 20 20 76 6f 69 64 20  BLOBs */.  void 
19b62 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20 2f  (*xDel)(void*) /
19b63 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 75 6e  * Destructor fun
19b64 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 69 6e  ction */.){.  in
19b65 74 20 6e 42 79 74 65 20 3d 20 6e 3b 20 20 20 20  t nByte = n;    
19b66 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65 20 66    /* New value f
19b67 6f 72 20 70 4d 65 6d 2d 3e 6e 20 2a 2f 0a 20 20  or pMem->n */.  
19b68 69 6e 74 20 69 4c 69 6d 69 74 3b 20 20 20 20 20  int iLimit;     
19b69 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61      /* Maximum a
19b6a 6c 6c 6f 77 65 64 20 73 74 72 69 6e 67 20 6f 72  llowed string or
19b6b 20 62 6c 6f 62 20 73 69 7a 65 20 2a 2f 0a 20 20   blob size */.  
19b6c 75 31 36 20 66 6c 61 67 73 20 3d 20 30 3b 20 20  u16 flags = 0;  
19b6d 20 20 20 20 2f 2a 20 4e 65 77 20 76 61 6c 75 65      /* New value
19b6e 20 66 6f 72 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   for pMem->flags
19b6f 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
19b70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
19b71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
19b72 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
19b73 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  ) );.  assert( (
19b74 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pMem->flags & ME
19b75 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a  M_RowSet)==0 );.
19b76 0a 20 20 2f 2a 20 49 66 20 7a 20 69 73 20 61 20  .  /* If z is a 
19b77 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20 73 65  NULL pointer, se
19b78 74 20 70 4d 65 6d 20 74 6f 20 63 6f 6e 74 61 69  t pMem to contai
19b79 6e 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 2a  n an SQL NULL. *
19b7a 2f 0a 20 20 69 66 28 20 21 7a 20 29 7b 0a 20 20  /.  if( !z ){.  
19b7b 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
19b7c 53 65 74 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20  SetNull(pMem);. 
19b7d 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
19b7e 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  _OK;.  }..  if( 
19b7f 70 4d 65 6d 2d 3e 64 62 20 29 7b 0a 20 20 20 20  pMem->db ){.    
19b80 69 4c 69 6d 69 74 20 3d 20 70 4d 65 6d 2d 3e 64  iLimit = pMem->d
19b81 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
19b82 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a  _LIMIT_LENGTH];.
19b83 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c 69    }else{.    iLi
19b84 6d 69 74 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58  mit = SQLITE_MAX
19b85 5f 4c 45 4e 47 54 48 3b 0a 20 20 7d 0a 20 20 66  _LENGTH;.  }.  f
19b86 6c 61 67 73 20 3d 20 28 65 6e 63 3d 3d 30 3f 4d  lags = (enc==0?M
19b87 45 4d 5f 42 6c 6f 62 3a 4d 45 4d 5f 53 74 72 29  EM_Blob:MEM_Str)
19b88 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 30 20  ;.  if( nByte<0 
19b89 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 65  ){.    assert( e
19b8a 6e 63 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  nc!=0 );.    if(
19b8b 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46   enc==SQLITE_UTF
19b8c 38 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 6e  8 ){.      for(n
19b8d 42 79 74 65 3d 30 3b 20 6e 42 79 74 65 3c 3d 69  Byte=0; nByte<=i
19b8e 4c 69 6d 69 74 20 26 26 20 7a 5b 6e 42 79 74 65  Limit && z[nByte
19b8f 5d 3b 20 6e 42 79 74 65 2b 2b 29 7b 7d 0a 20 20  ]; nByte++){}.  
19b90 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66    }else{.      f
19b91 6f 72 28 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74  or(nByte=0; nByt
19b92 65 3c 3d 69 4c 69 6d 69 74 20 26 26 20 28 7a 5b  e<=iLimit && (z[
19b93 6e 42 79 74 65 5d 20 7c 20 7a 5b 6e 42 79 74 65  nByte] | z[nByte
19b94 2b 31 5d 29 3b 20 6e 42 79 74 65 2b 3d 32 29 7b  +1]); nByte+=2){
19b95 7d 0a 20 20 20 20 7d 0a 20 20 20 20 66 6c 61 67  }.    }.    flag
19b96 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20  s |= MEM_Term;. 
19b97 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c   }..  /* The fol
19b98 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 73 65 74  lowing block set
19b99 73 20 74 68 65 20 6e 65 77 20 76 61 6c 75 65 73  s the new values
19b9a 20 6f 66 20 4d 65 6d 2e 7a 20 61 6e 64 20 4d 65   of Mem.z and Me
19b9b 6d 2e 78 44 65 6c 2e 20 49 74 0a 20 20 2a 2a 20  m.xDel. It.  ** 
19b9c 61 6c 73 6f 20 73 65 74 73 20 61 20 66 6c 61 67  also sets a flag
19b9d 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   in local variab
19b9e 6c 65 20 22 66 6c 61 67 73 22 20 74 6f 20 69 6e  le "flags" to in
19b9f 64 69 63 61 74 65 20 74 68 65 20 6d 65 6d 6f 72  dicate the memor
19ba0 79 0a 20 20 2a 2a 20 6d 61 6e 61 67 65 6d 65 6e  y.  ** managemen
19ba1 74 20 28 6f 6e 65 20 6f 66 20 4d 45 4d 5f 44 79  t (one of MEM_Dy
19ba2 6e 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69 63 29  n or MEM_Static)
19ba3 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 78 44 65  ..  */.  if( xDe
19ba4 6c 3d 3d 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  l==SQLITE_TRANSI
19ba5 45 4e 54 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  ENT ){.    int n
19ba6 41 6c 6c 6f 63 20 3d 20 6e 42 79 74 65 3b 0a 20  Alloc = nByte;. 
19ba7 20 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45 4d     if( flags&MEM
19ba8 5f 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 6e  _Term ){.      n
19ba9 41 6c 6c 6f 63 20 2b 3d 20 28 65 6e 63 3d 3d 53  Alloc += (enc==S
19baa 51 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 3b  QLITE_UTF8?1:2);
19bab 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e  .    }.    if( n
19bac 42 79 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20  Byte>iLimit ){. 
19bad 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
19bae 54 45 5f 54 4f 4f 42 49 47 3b 0a 20 20 20 20 7d  TE_TOOBIG;.    }
19baf 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
19bb0 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d  VdbeMemGrow(pMem
19bb1 2c 20 6e 41 6c 6c 6f 63 2c 20 30 29 20 29 7b 0a  , nAlloc, 0) ){.
19bb2 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
19bb3 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
19bb4 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d  .    memcpy(pMem
19bb5 2d 3e 7a 2c 20 7a 2c 20 6e 41 6c 6c 6f 63 29 3b  ->z, z, nAlloc);
19bb6 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 78 44 65  .  }else if( xDe
19bb7 6c 3d 3d 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  l==SQLITE_DYNAMI
19bb8 43 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  C ){.    sqlite3
19bb9 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
19bba 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e  Mem);.    pMem->
19bbb 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e  zMalloc = pMem->
19bbc 7a 20 3d 20 28 63 68 61 72 20 2a 29 7a 3b 0a 20  z = (char *)z;. 
19bbd 20 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20     pMem->xDel = 
19bbe 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  0;.  }else{.    
19bbf 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
19bc0 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20  lease(pMem);.   
19bc1 20 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72   pMem->z = (char
19bc2 20 2a 29 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e   *)z;.    pMem->
19bc3 78 44 65 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 20  xDel = xDel;.   
19bc4 20 66 6c 61 67 73 20 7c 3d 20 28 28 78 44 65 6c   flags |= ((xDel
19bc5 3d 3d 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  ==SQLITE_STATIC)
19bc6 3f 4d 45 4d 5f 53 74 61 74 69 63 3a 4d 45 4d 5f  ?MEM_Static:MEM_
19bc7 44 79 6e 29 3b 0a 20 20 7d 0a 0a 20 20 70 4d 65  Dyn);.  }..  pMe
19bc8 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b 0a 20 20  m->n = nByte;.  
19bc9 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 66 6c  pMem->flags = fl
19bca 61 67 73 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e 63  ags;.  pMem->enc
19bcb 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51 4c   = (enc==0 ? SQL
19bcc 49 54 45 5f 55 54 46 38 20 3a 20 65 6e 63 29 3b  ITE_UTF8 : enc);
19bcd 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20  .  pMem->type = 
19bce 28 65 6e 63 3d 3d 30 20 3f 20 53 51 4c 49 54 45  (enc==0 ? SQLITE
19bcf 5f 42 4c 4f 42 20 3a 20 53 51 4c 49 54 45 5f 54  _BLOB : SQLITE_T
19bd0 45 58 54 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  EXT);..#ifndef S
19bd1 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
19bd2 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e 63  .  if( pMem->enc
19bd3 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26 26  !=SQLITE_UTF8 &&
19bd4 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 48   sqlite3VdbeMemH
19bd5 61 6e 64 6c 65 42 6f 6d 28 70 4d 65 6d 29 20 29  andleBom(pMem) )
19bd6 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
19bd7 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23  ITE_NOMEM;.  }.#
19bd8 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e 42 79  endif..  if( nBy
19bd9 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20  te>iLimit ){.   
19bda 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 54   return SQLITE_T
19bdb 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a 20 20 72 65  OOBIG;.  }..  re
19bdc 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
19bdd 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65  }../*.** Compare
19bde 20 74 68 65 20 76 61 6c 75 65 73 20 63 6f 6e 74   the values cont
19bdf 61 69 6e 65 64 20 62 79 20 74 68 65 20 74 77 6f  ained by the two
19be0 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 2c 20 72   memory cells, r
19be1 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 6e 65 67 61  eturning.** nega
19be2 74 69 76 65 2c 20 7a 65 72 6f 20 6f 72 20 70 6f  tive, zero or po
19be3 73 69 74 69 76 65 20 69 66 20 70 4d 65 6d 31 20  sitive if pMem1 
19be4 69 73 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71  is less than, eq
19be5 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74  ual to, or great
19be6 65 72 0a 2a 2a 20 74 68 61 6e 20 70 4d 65 6d 32  er.** than pMem2
19be7 2e 20 53 6f 72 74 69 6e 67 20 6f 72 64 65 72 20  . Sorting order 
19be8 69 73 20 4e 55 4c 4c 27 73 20 66 69 72 73 74 2c  is NULL's first,
19be9 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 6e 75 6d   followed by num
19bea 62 65 72 73 20 28 69 6e 74 65 67 65 72 73 0a 2a  bers (integers.*
19beb 2a 20 61 6e 64 20 72 65 61 6c 73 29 20 73 6f 72  * and reals) sor
19bec 74 65 64 20 6e 75 6d 65 72 69 63 61 6c 6c 79 2c  ted numerically,
19bed 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 74 65 78   followed by tex
19bee 74 20 6f 72 64 65 72 65 64 20 62 79 20 74 68 65  t ordered by the
19bef 20 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65   collating.** se
19bf0 71 75 65 6e 63 65 20 70 43 6f 6c 6c 20 61 6e 64  quence pColl and
19bf1 20 66 69 6e 61 6c 6c 79 20 62 6c 6f 62 27 73 20   finally blob's 
19bf2 6f 72 64 65 72 65 64 20 62 79 20 6d 65 6d 63 6d  ordered by memcm
19bf3 70 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 77 6f 20 4e  p()..**.** Two N
19bf4 55 4c 4c 20 76 61 6c 75 65 73 20 61 72 65 20 63  ULL values are c
19bf5 6f 6e 73 69 64 65 72 65 64 20 65 71 75 61 6c 20  onsidered equal 
19bf6 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  by this function
19bf7 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
19bf8 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  ATE int sqlite3M
19bf9 65 6d 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20  emCompare(const 
19bfa 4d 65 6d 20 2a 70 4d 65 6d 31 2c 20 63 6f 6e 73  Mem *pMem1, cons
19bfb 74 20 4d 65 6d 20 2a 70 4d 65 6d 32 2c 20 63 6f  t Mem *pMem2, co
19bfc 6e 73 74 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  nst CollSeq *pCo
19bfd 6c 6c 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ll){.  int rc;. 
19bfe 20 69 6e 74 20 66 31 2c 20 66 32 3b 0a 20 20 69   int f1, f2;.  i
19bff 6e 74 20 63 6f 6d 62 69 6e 65 64 5f 66 6c 61 67  nt combined_flag
19c00 73 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65 72 63 68  s;..  /* Interch
19c01 61 6e 67 65 20 70 4d 65 6d 31 20 61 6e 64 20 70  ange pMem1 and p
19c02 4d 65 6d 32 20 69 66 20 74 68 65 20 63 6f 6c 6c  Mem2 if the coll
19c03 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 73  ating sequence s
19c04 70 65 63 69 66 69 65 73 0a 20 20 2a 2a 20 44 45  pecifies.  ** DE
19c05 53 43 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20  SC order..  */. 
19c06 20 66 31 20 3d 20 70 4d 65 6d 31 2d 3e 66 6c 61   f1 = pMem1->fla
19c07 67 73 3b 0a 20 20 66 32 20 3d 20 70 4d 65 6d 32  gs;.  f2 = pMem2
19c08 2d 3e 66 6c 61 67 73 3b 0a 20 20 63 6f 6d 62 69  ->flags;.  combi
19c09 6e 65 64 5f 66 6c 61 67 73 20 3d 20 66 31 7c 66  ned_flags = f1|f
19c0a 32 3b 0a 20 20 61 73 73 65 72 74 28 20 28 63 6f  2;.  assert( (co
19c0b 6d 62 69 6e 65 64 5f 66 6c 61 67 73 20 26 20 4d  mbined_flags & M
19c0c 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b  EM_RowSet)==0 );
19c0d 0a 20 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76  . .  /* If one v
19c0e 61 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20 69 74  alue is NULL, it
19c0f 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68   is less than th
19c10 65 20 6f 74 68 65 72 2e 20 49 66 20 62 6f 74 68  e other. If both
19c11 20 76 61 6c 75 65 73 0a 20 20 2a 2a 20 61 72 65   values.  ** are
19c12 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e   NULL, return 0.
19c13 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62  .  */.  if( comb
19c14 69 6e 65 64 5f 66 6c 61 67 73 26 4d 45 4d 5f 4e  ined_flags&MEM_N
19c15 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ull ){.    retur
19c16 6e 20 28 66 32 26 4d 45 4d 5f 4e 75 6c 6c 29 20  n (f2&MEM_Null) 
19c17 2d 20 28 66 31 26 4d 45 4d 5f 4e 75 6c 6c 29 3b  - (f1&MEM_Null);
19c18 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e  .  }..  /* If on
19c19 65 20 76 61 6c 75 65 20 69 73 20 61 20 6e 75 6d  e value is a num
19c1a 62 65 72 20 61 6e 64 20 74 68 65 20 6f 74 68 65  ber and the othe
19c1b 72 20 69 73 20 6e 6f 74 2c 20 74 68 65 20 6e 75  r is not, the nu
19c1c 6d 62 65 72 20 69 73 20 6c 65 73 73 2e 0a 20 20  mber is less..  
19c1d 2a 2a 20 49 66 20 62 6f 74 68 20 61 72 65 20 6e  ** If both are n
19c1e 75 6d 62 65 72 73 2c 20 63 6f 6d 70 61 72 65 20  umbers, compare 
19c1f 61 73 20 72 65 61 6c 73 20 69 66 20 6f 6e 65 20  as reals if one 
19c20 69 73 20 61 20 72 65 61 6c 2c 20 6f 72 20 61 73  is a real, or as
19c21 20 69 6e 74 65 67 65 72 73 0a 20 20 2a 2a 20 69   integers.  ** i
19c22 66 20 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72  f both values ar
19c23 65 20 69 6e 74 65 67 65 72 73 2e 0a 20 20 2a 2f  e integers..  */
19c24 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f  .  if( combined_
19c25 66 6c 61 67 73 26 28 4d 45 4d 5f 49 6e 74 7c 4d  flags&(MEM_Int|M
19c26 45 4d 5f 52 65 61 6c 29 20 29 7b 0a 20 20 20 20  EM_Real) ){.    
19c27 69 66 28 20 21 28 66 31 26 28 4d 45 4d 5f 49 6e  if( !(f1&(MEM_In
19c28 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a  t|MEM_Real)) ){.
19c29 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
19c2a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 28      }.    if( !(
19c2b 66 32 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f  f2&(MEM_Int|MEM_
19c2c 52 65 61 6c 29 29 20 29 7b 0a 20 20 20 20 20 20  Real)) ){.      
19c2d 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d  return -1;.    }
19c2e 0a 20 20 20 20 69 66 28 20 28 66 31 20 26 20 66  .    if( (f1 & f
19c2f 32 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20  2 & MEM_Int)==0 
19c30 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20  ){.      double 
19c31 72 31 2c 20 72 32 3b 0a 20 20 20 20 20 20 69 66  r1, r2;.      if
19c32 28 20 28 66 31 26 4d 45 4d 5f 52 65 61 6c 29 3d  ( (f1&MEM_Real)=
19c33 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 31  =0 ){.        r1
19c34 20 3d 20 28 64 6f 75 62 6c 65 29 70 4d 65 6d 31   = (double)pMem1
19c35 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 7d 65 6c  ->u.i;.      }el
19c36 73 65 7b 0a 20 20 20 20 20 20 20 20 72 31 20 3d  se{.        r1 =
19c37 20 70 4d 65 6d 31 2d 3e 72 3b 0a 20 20 20 20 20   pMem1->r;.     
19c38 20 7d 0a 20 20 20 20 20 20 69 66 28 20 28 66 32   }.      if( (f2
19c39 26 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30 20 29 7b  &MEM_Real)==0 ){
19c3a 0a 20 20 20 20 20 20 20 20 72 32 20 3d 20 28 64  .        r2 = (d
19c3b 6f 75 62 6c 65 29 70 4d 65 6d 32 2d 3e 75 2e 69  ouble)pMem2->u.i
19c3c 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
19c3d 20 20 20 20 20 20 20 72 32 20 3d 20 70 4d 65 6d         r2 = pMem
19c3e 32 2d 3e 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20  2->r;.      }.  
19c3f 20 20 20 20 69 66 28 20 72 31 3c 72 32 20 29 20      if( r1<r2 ) 
19c40 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20  return -1;.     
19c41 20 69 66 28 20 72 31 3e 72 32 20 29 20 72 65 74   if( r1>r2 ) ret
19c42 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74  urn 1;.      ret
19c43 75 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  urn 0;.    }else
19c44 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
19c45 66 31 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20  f1&MEM_Int );.  
19c46 20 20 20 20 61 73 73 65 72 74 28 20 66 32 26 4d      assert( f2&M
19c47 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20  EM_Int );.      
19c48 69 66 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3c  if( pMem1->u.i <
19c49 20 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65   pMem2->u.i ) re
19c4a 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20 69  turn -1;.      i
19c4b 66 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3e 20  f( pMem1->u.i > 
19c4c 70 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74  pMem2->u.i ) ret
19c4d 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74  urn 1;.      ret
19c4e 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 0;.    }.  }
19c4f 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61  ..  /* If one va
19c50 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e 67 20  lue is a string 
19c51 61 6e 64 20 74 68 65 20 6f 74 68 65 72 20 69 73  and the other is
19c52 20 61 20 62 6c 6f 62 2c 20 74 68 65 20 73 74 72   a blob, the str
19c53 69 6e 67 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a  ing is less..  *
19c54 2a 20 49 66 20 62 6f 74 68 20 61 72 65 20 73 74  * If both are st
19c55 72 69 6e 67 73 2c 20 63 6f 6d 70 61 72 65 20 75  rings, compare u
19c56 73 69 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69  sing the collati
19c57 6e 67 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20  ng functions..  
19c58 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65  */.  if( combine
19c59 64 5f 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20  d_flags&MEM_Str 
19c5a 29 7b 0a 20 20 20 20 69 66 28 20 28 66 31 20 26  ){.    if( (f1 &
19c5b 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29 7b 0a   MEM_Str)==0 ){.
19c5c 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
19c5d 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66      }.    if( (f
19c5e 32 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20  2 & MEM_Str)==0 
19c5f 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
19c60 2d 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61  -1;.    }..    a
19c61 73 73 65 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e  ssert( pMem1->en
19c62 63 3d 3d 70 4d 65 6d 32 2d 3e 65 6e 63 20 29 3b  c==pMem2->enc );
19c63 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65  .    assert( pMe
19c64 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f  m1->enc==SQLITE_
19c65 55 54 46 38 20 7c 7c 20 0a 20 20 20 20 20 20 20  UTF8 || .       
19c66 20 20 20 20 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d       pMem1->enc=
19c67 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20  =SQLITE_UTF16LE 
19c68 7c 7c 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53  || pMem1->enc==S
19c69 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b  QLITE_UTF16BE );
19c6a 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c  ..    /* The col
19c6b 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
19c6c 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20  must be defined 
19c6d 61 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 65  at this point, e
19c6e 76 65 6e 20 69 66 0a 20 20 20 20 2a 2a 20 74 68  ven if.    ** th
19c6f 65 20 75 73 65 72 20 64 65 6c 65 74 65 73 20 74  e user deletes t
19c70 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
19c71 75 65 6e 63 65 20 61 66 74 65 72 20 74 68 65 20  uence after the 
19c72 76 64 62 65 20 70 72 6f 67 72 61 6d 20 69 73 0a  vdbe program is.
19c73 20 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64 20      ** compiled 
19c74 28 74 68 69 73 20 77 61 73 20 6e 6f 74 20 61 6c  (this was not al
19c75 77 61 79 73 20 74 68 65 20 63 61 73 65 29 2e 0a  ways the case)..
19c76 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
19c77 74 28 20 21 70 43 6f 6c 6c 20 7c 7c 20 70 43 6f  t( !pColl || pCo
19c78 6c 6c 2d 3e 78 43 6d 70 20 29 3b 0a 0a 20 20 20  ll->xCmp );..   
19c79 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20   if( pColl ){.  
19c7a 20 20 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 65      if( pMem1->e
19c7b 6e 63 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29  nc==pColl->enc )
19c7c 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  {.        /* The
19c7d 20 73 74 72 69 6e 67 73 20 61 72 65 20 61 6c 72   strings are alr
19c7e 65 61 64 79 20 69 6e 20 74 68 65 20 63 6f 72 72  eady in the corr
19c7f 65 63 74 20 65 6e 63 6f 64 69 6e 67 2e 20 20 43  ect encoding.  C
19c80 61 6c 6c 20 74 68 65 0a 20 20 20 20 20 20 20 20  all the.        
19c81 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75  ** comparison fu
19c82 6e 63 74 69 6f 6e 20 64 69 72 65 63 74 6c 79 20  nction directly 
19c83 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  */.        retur
19c84 6e 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43  n pColl->xCmp(pC
19c85 6f 6c 6c 2d 3e 70 55 73 65 72 2c 70 4d 65 6d 31  oll->pUser,pMem1
19c86 2d 3e 6e 2c 70 4d 65 6d 31 2d 3e 7a 2c 70 4d 65  ->n,pMem1->z,pMe
19c87 6d 32 2d 3e 6e 2c 70 4d 65 6d 32 2d 3e 7a 29 3b  m2->n,pMem2->z);
19c88 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
19c89 20 20 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64        const void
19c8a 20 2a 76 31 2c 20 2a 76 32 3b 0a 20 20 20 20 20   *v1, *v2;.     
19c8b 20 20 20 69 6e 74 20 6e 31 2c 20 6e 32 3b 0a 20     int n1, n2;. 
19c8c 20 20 20 20 20 20 20 4d 65 6d 20 63 31 3b 0a 20         Mem c1;. 
19c8d 20 20 20 20 20 20 20 4d 65 6d 20 63 32 3b 0a 20         Mem c2;. 
19c8e 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26 63         memset(&c
19c8f 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 63 31 29  1, 0, sizeof(c1)
19c90 29 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65  );.        memse
19c91 74 28 26 63 32 2c 20 30 2c 20 73 69 7a 65 6f 66  t(&c2, 0, sizeof
19c92 28 63 32 29 29 3b 0a 20 20 20 20 20 20 20 20 73  (c2));.        s
19c93 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61  qlite3VdbeMemSha
19c94 6c 6c 6f 77 43 6f 70 79 28 26 63 31 2c 20 70 4d  llowCopy(&c1, pM
19c95 65 6d 31 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b  em1, MEM_Ephem);
19c96 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
19c97 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f  VdbeMemShallowCo
19c98 70 79 28 26 63 32 2c 20 70 4d 65 6d 32 2c 20 4d  py(&c2, pMem2, M
19c99 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 20  EM_Ephem);.     
19c9a 20 20 20 76 31 20 3d 20 73 71 6c 69 74 65 33 56     v1 = sqlite3V
19c9b 61 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74 65  alueText((sqlite
19c9c 33 5f 76 61 6c 75 65 2a 29 26 63 31 2c 20 70 43  3_value*)&c1, pC
19c9d 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20  oll->enc);.     
19c9e 20 20 20 6e 31 20 3d 20 76 31 3d 3d 30 20 3f 20     n1 = v1==0 ? 
19c9f 30 20 3a 20 63 31 2e 6e 3b 0a 20 20 20 20 20 20  0 : c1.n;.      
19ca0 20 20 76 32 20 3d 20 73 71 6c 69 74 65 33 56 61    v2 = sqlite3Va
19ca1 6c 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33  lueText((sqlite3
19ca2 5f 76 61 6c 75 65 2a 29 26 63 32 2c 20 70 43 6f  _value*)&c2, pCo
19ca3 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20  ll->enc);.      
19ca4 20 20 6e 32 20 3d 20 76 32 3d 3d 30 20 3f 20 30    n2 = v2==0 ? 0
19ca5 20 3a 20 63 32 2e 6e 3b 0a 20 20 20 20 20 20 20   : c2.n;.       
19ca6 20 72 63 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43 6d   rc = pColl->xCm
19ca7 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 20  p(pColl->pUser, 
19ca8 6e 31 2c 20 76 31 2c 20 6e 32 2c 20 76 32 29 3b  n1, v1, n2, v2);
19ca9 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
19caa 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26  VdbeMemRelease(&
19cab 63 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  c1);.        sql
19cac 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
19cad 73 65 28 26 63 32 29 3b 0a 20 20 20 20 20 20 20  se(&c2);.       
19cae 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
19caf 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a    }.    }.    /*
19cb0 20 49 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74   If a NULL point
19cb1 65 72 20 77 61 73 20 70 61 73 73 65 64 20 61 73  er was passed as
19cb2 20 74 68 65 20 63 6f 6c 6c 61 74 65 20 66 75 6e   the collate fun
19cb3 63 74 69 6f 6e 2c 20 66 61 6c 6c 20 74 68 72 6f  ction, fall thro
19cb4 75 67 68 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68  ugh.    ** to th
19cb5 65 20 62 6c 6f 62 20 63 61 73 65 20 61 6e 64 20  e blob case and 
19cb6 75 73 65 20 6d 65 6d 63 6d 70 28 29 2e 20 20 2a  use memcmp().  *
19cb7 2f 0a 20 20 7d 0a 20 0a 20 20 2f 2a 20 42 6f 74  /.  }. .  /* Bot
19cb8 68 20 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65  h values must be
19cb9 20 62 6c 6f 62 73 2e 20 20 43 6f 6d 70 61 72 65   blobs.  Compare
19cba 20 75 73 69 6e 67 20 6d 65 6d 63 6d 70 28 29 2e   using memcmp().
19cbb 20 20 2a 2f 0a 20 20 72 63 20 3d 20 6d 65 6d 63    */.  rc = memc
19cbc 6d 70 28 70 4d 65 6d 31 2d 3e 7a 2c 20 70 4d 65  mp(pMem1->z, pMe
19cbd 6d 32 2d 3e 7a 2c 20 28 70 4d 65 6d 31 2d 3e 6e  m2->z, (pMem1->n
19cbe 3e 70 4d 65 6d 32 2d 3e 6e 29 3f 70 4d 65 6d 32  >pMem2->n)?pMem2
19cbf 2d 3e 6e 3a 70 4d 65 6d 31 2d 3e 6e 29 3b 0a 20  ->n:pMem1->n);. 
19cc0 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20   if( rc==0 ){.  
19cc1 20 20 72 63 20 3d 20 70 4d 65 6d 31 2d 3e 6e 20    rc = pMem1->n 
19cc2 2d 20 70 4d 65 6d 32 2d 3e 6e 3b 0a 20 20 7d 0a  - pMem2->n;.  }.
19cc3 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
19cc4 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 64 61 74 61 20  /*.** Move data 
19cc5 6f 75 74 20 6f 66 20 61 20 62 74 72 65 65 20 6b  out of a btree k
19cc6 65 79 20 6f 72 20 64 61 74 61 20 66 69 65 6c 64  ey or data field
19cc7 20 61 6e 64 20 69 6e 74 6f 20 61 20 4d 65 6d 20   and into a Mem 
19cc8 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68  structure..** Th
19cc9 65 20 64 61 74 61 20 6f 72 20 6b 65 79 20 69 73  e data or key is
19cca 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20   taken from the 
19ccb 65 6e 74 72 79 20 74 68 61 74 20 70 43 75 72 20  entry that pCur 
19ccc 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  is currently poi
19ccd 6e 74 69 6e 67 0a 2a 2a 20 74 6f 2e 20 20 6f 66  nting.** to.  of
19cce 66 73 65 74 20 61 6e 64 20 61 6d 74 20 64 65 74  fset and amt det
19ccf 65 72 6d 69 6e 65 20 77 68 61 74 20 70 6f 72 74  ermine what port
19cd0 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 20  ion of the data 
19cd1 6f 72 20 6b 65 79 20 74 6f 20 72 65 74 72 69 65  or key to retrie
19cd2 76 65 2e 0a 2a 2a 20 6b 65 79 20 69 73 20 74 72  ve..** key is tr
19cd3 75 65 20 74 6f 20 67 65 74 20 74 68 65 20 6b 65  ue to get the ke
19cd4 79 20 6f 72 20 66 61 6c 73 65 20 74 6f 20 67 65  y or false to ge
19cd5 74 20 64 61 74 61 2e 20 20 54 68 65 20 72 65 73  t data.  The res
19cd6 75 6c 74 20 69 73 20 77 72 69 74 74 65 6e 0a 2a  ult is written.*
19cd7 2a 20 69 6e 74 6f 20 74 68 65 20 70 4d 65 6d 20  * into the pMem 
19cd8 65 6c 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  element..**.** T
19cd9 68 65 20 70 4d 65 6d 20 73 74 72 75 63 74 75 72  he pMem structur
19cda 65 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20  e is assumed to 
19cdb 62 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64  be uninitialized
19cdc 2e 20 20 41 6e 79 20 70 72 69 6f 72 20 63 6f 6e  .  Any prior con
19cdd 74 65 6e 74 0a 2a 2a 20 69 73 20 6f 76 65 72 77  tent.** is overw
19cde 72 69 74 74 65 6e 20 77 69 74 68 6f 75 74 20 62  ritten without b
19cdf 65 69 6e 67 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a  eing freed..**.*
19ce0 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e  * If this routin
19ce1 65 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20  e fails for any 
19ce2 72 65 61 73 6f 6e 20 28 6d 61 6c 6c 6f 63 20 72  reason (malloc r
19ce3 65 74 75 72 6e 73 20 4e 55 4c 4c 20 6f 72 20 75  eturns NULL or u
19ce4 6e 61 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 61 64  nable.** to read
19ce5 20 66 72 6f 6d 20 74 68 65 20 64 69 73 6b 29 20   from the disk) 
19ce6 74 68 65 6e 20 74 68 65 20 70 4d 65 6d 20 69 73  then the pMem is
19ce7 20 6c 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f   left in an inco
19ce8 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a  nsistent state..
19ce9 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19cea 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
19ceb 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 0a 20  eMemFromBtree(. 
19cec 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c   BtCursor *pCur,
19ced 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69     /* Cursor poi
19cee 6e 74 69 6e 67 20 61 74 20 72 65 63 6f 72 64 20  nting at record 
19cef 74 6f 20 72 65 74 72 69 65 76 65 2e 20 2a 2f 0a  to retrieve. */.
19cf0 20 20 69 6e 74 20 6f 66 66 73 65 74 2c 20 20 20    int offset,   
19cf1 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72      /* Offset fr
19cf2 6f 6d 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  om the start of 
19cf3 64 61 74 61 20 74 6f 20 72 65 74 75 72 6e 20 62  data to return b
19cf4 79 74 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a 20 20  ytes from. */.  
19cf5 69 6e 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20  int amt,        
19cf6 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
19cf7 79 74 65 73 20 74 6f 20 72 65 74 75 72 6e 2e 20  ytes to return. 
19cf8 2a 2f 0a 20 20 69 6e 74 20 6b 65 79 2c 20 20 20  */.  int key,   
19cf9 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75         /* If tru
19cfa 65 2c 20 72 65 74 72 69 65 76 65 20 66 72 6f 6d  e, retrieve from
19cfb 20 74 68 65 20 62 74 72 65 65 20 6b 65 79 2c 20   the btree key, 
19cfc 6e 6f 74 20 64 61 74 61 2e 20 2a 2f 0a 20 20 4d  not data. */.  M
19cfd 65 6d 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20  em *pMem        
19cfe 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20   /* OUT: Return 
19cff 64 61 74 61 20 69 6e 20 74 68 69 73 20 4d 65 6d  data in this Mem
19d00 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 29   structure. */.)
19d01 7b 0a 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b  {.  char *zData;
19d02 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20          /* Data 
19d03 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65 20 6c  from the btree l
19d04 61 79 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 76  ayer */.  int av
19d05 61 69 6c 61 62 6c 65 20 3d 20 30 3b 20 20 2f 2a  ailable = 0;  /*
19d06 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
19d07 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68   available on th
19d08 65 20 6c 6f 63 61 6c 20 62 74 72 65 65 20 70 61  e local btree pa
19d09 67 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d  ge */.  int rc =
19d0a 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 2f 2a 20 52   SQLITE_OK; /* R
19d0b 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 0a 20  eturn code */.. 
19d0c 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
19d0d 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c  BtreeCursorIsVal
19d0e 69 64 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 2f  id(pCur) );..  /
19d0f 2a 20 4e 6f 74 65 3a 20 74 68 65 20 63 61 6c 6c  * Note: the call
19d10 73 20 74 6f 20 42 74 72 65 65 4b 65 79 46 65 74  s to BtreeKeyFet
19d11 63 68 28 29 20 61 6e 64 20 44 61 74 61 46 65 74  ch() and DataFet
19d12 63 68 28 29 20 62 65 6c 6f 77 20 61 73 73 65 72  ch() below asser
19d13 74 28 29 20 0a 20 20 2a 2a 20 74 68 61 74 20 62  t() .  ** that b
19d14 6f 74 68 20 74 68 65 20 42 74 53 68 61 72 65 64  oth the BtShared
19d15 20 61 6e 64 20 64 61 74 61 62 61 73 65 20 68 61   and database ha
19d16 6e 64 6c 65 20 6d 75 74 65 78 65 73 20 61 72 65  ndle mutexes are
19d17 20 68 65 6c 64 2e 20 2a 2f 0a 20 20 61 73 73 65   held. */.  asse
19d18 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73  rt( (pMem->flags
19d19 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d   & MEM_RowSet)==
19d1a 30 20 29 3b 0a 20 20 69 66 28 20 6b 65 79 20 29  0 );.  if( key )
19d1b 7b 0a 20 20 20 20 7a 44 61 74 61 20 3d 20 28 63  {.    zData = (c
19d1c 68 61 72 20 2a 29 73 71 6c 69 74 65 33 42 74 72  har *)sqlite3Btr
19d1d 65 65 4b 65 79 46 65 74 63 68 28 70 43 75 72 2c  eeKeyFetch(pCur,
19d1e 20 26 61 76 61 69 6c 61 62 6c 65 29 3b 0a 20 20   &available);.  
19d1f 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 44 61 74 61  }else{.    zData
19d20 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74   = (char *)sqlit
19d21 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68  e3BtreeDataFetch
19d22 28 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c  (pCur, &availabl
19d23 65 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  e);.  }.  assert
19d24 28 20 7a 44 61 74 61 21 3d 30 20 29 3b 0a 0a 20  ( zData!=0 );.. 
19d25 20 69 66 28 20 6f 66 66 73 65 74 2b 61 6d 74 3c   if( offset+amt<
19d26 3d 61 76 61 69 6c 61 62 6c 65 20 26 26 20 28 70  =available && (p
19d27 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44  Mem->flags&MEM_D
19d28 79 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  yn)==0 ){.    sq
19d29 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
19d2a 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70  ase(pMem);.    p
19d2b 4d 65 6d 2d 3e 7a 20 3d 20 26 7a 44 61 74 61 5b  Mem->z = &zData[
19d2c 6f 66 66 73 65 74 5d 3b 0a 20 20 20 20 70 4d 65  offset];.    pMe
19d2d 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42  m->flags = MEM_B
19d2e 6c 6f 62 7c 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20  lob|MEM_Ephem;. 
19d2f 20 7d 65 6c 73 65 20 69 66 28 20 53 51 4c 49 54   }else if( SQLIT
19d30 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK==(rc = sqli
19d31 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70  te3VdbeMemGrow(p
19d32 4d 65 6d 2c 20 61 6d 74 2b 32 2c 20 30 29 29 20  Mem, amt+2, 0)) 
19d33 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  ){.    pMem->fla
19d34 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d 45  gs = MEM_Blob|ME
19d35 4d 5f 44 79 6e 7c 4d 45 4d 5f 54 65 72 6d 3b 0a  M_Dyn|MEM_Term;.
19d36 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20      pMem->enc = 
19d37 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70  0;.    pMem->typ
19d38 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42 3b  e = SQLITE_BLOB;
19d39 0a 20 20 20 20 69 66 28 20 6b 65 79 20 29 7b 0a  .    if( key ){.
19d3a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
19d3b 65 33 42 74 72 65 65 4b 65 79 28 70 43 75 72 2c  e3BtreeKey(pCur,
19d3c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70 4d   offset, amt, pM
19d3d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c 73  em->z);.    }els
19d3e 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  e{.      rc = sq
19d3f 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28 70  lite3BtreeData(p
19d40 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74  Cur, offset, amt
19d41 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20  , pMem->z);.    
19d42 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 61 6d  }.    pMem->z[am
19d43 74 5d 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d  t] = 0;.    pMem
19d44 2d 3e 7a 5b 61 6d 74 2b 31 5d 20 3d 20 30 3b 0a  ->z[amt+1] = 0;.
19d45 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
19d46 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73  TE_OK ){.      s
19d47 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
19d48 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20  ease(pMem);.    
19d49 7d 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 6e 20  }.  }.  pMem->n 
19d4a 3d 20 61 6d 74 3b 0a 0a 20 20 72 65 74 75 72 6e  = amt;..  return
19d4b 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20   rc;.}../* This 
19d4c 66 75 6e 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79  function is only
19d4d 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 74 65 72   available inter
19d4e 6e 61 6c 6c 79 2c 20 69 74 20 69 73 20 6e 6f 74  nally, it is not
19d4f 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20   part of the.** 
19d50 65 78 74 65 72 6e 61 6c 20 41 50 49 2e 20 49 74  external API. It
19d51 20 77 6f 72 6b 73 20 69 6e 20 61 20 73 69 6d 69   works in a simi
19d52 6c 61 72 20 77 61 79 20 74 6f 20 73 71 6c 69 74  lar way to sqlit
19d53 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 2c  e3_value_text(),
19d54 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 65 20 64  .** except the d
19d55 61 74 61 20 72 65 74 75 72 6e 65 64 20 69 73 20  ata returned is 
19d56 69 6e 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  in the encoding 
19d57 73 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65  specified by the
19d58 20 73 65 63 6f 6e 64 0a 2a 2a 20 70 61 72 61 6d   second.** param
19d59 65 74 65 72 2c 20 77 68 69 63 68 20 6d 75 73 74  eter, which must
19d5a 20 62 65 20 6f 6e 65 20 6f 66 20 53 51 4c 49 54   be one of SQLIT
19d5b 45 5f 55 54 46 31 36 42 45 2c 20 53 51 4c 49 54  E_UTF16BE, SQLIT
19d5c 45 5f 55 54 46 31 36 4c 45 20 6f 72 0a 2a 2a 20  E_UTF16LE or.** 
19d5d 53 51 4c 49 54 45 5f 55 54 46 38 2e 0a 2a 2a 0a  SQLITE_UTF8..**.
19d5e 2a 2a 20 28 32 30 30 36 2d 30 32 2d 31 36 3a 29  ** (2006-02-16:)
19d5f 20 20 54 68 65 20 65 6e 63 20 76 61 6c 75 65 20    The enc value 
19d60 63 61 6e 20 62 65 20 6f 72 2d 65 64 20 77 69 74  can be or-ed wit
19d61 68 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41  h SQLITE_UTF16_A
19d62 4c 49 47 4e 45 44 2e 0a 2a 2a 20 49 66 20 74 68  LIGNED..** If th
19d63 61 74 20 69 73 20 74 68 65 20 63 61 73 65 2c 20  at is the case, 
19d64 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20  then the result 
19d65 6d 75 73 74 20 62 65 20 61 6c 69 67 6e 65 64 20  must be aligned 
19d66 6f 6e 20 61 6e 20 65 76 65 6e 20 62 79 74 65 0a  on an even byte.
19d67 2a 2a 20 62 6f 75 6e 64 61 72 79 2e 0a 2a 2f 0a  ** boundary..*/.
19d68 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
19d69 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
19d6a 65 33 56 61 6c 75 65 54 65 78 74 28 73 71 6c 69  e3ValueText(sqli
19d6b 74 65 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c 2c  te3_value* pVal,
19d6c 20 75 38 20 65 6e 63 29 7b 0a 20 20 69 66 28 20   u8 enc){.  if( 
19d6d 21 70 56 61 6c 20 29 20 72 65 74 75 72 6e 20 30  !pVal ) return 0
19d6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 56 61  ;..  assert( pVa
19d6f 6c 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c 69  l->db==0 || sqli
19d70 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
19d71 56 61 6c 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  Val->db->mutex) 
19d72 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 65 6e  );.  assert( (en
19d73 63 26 33 29 3d 3d 28 65 6e 63 26 7e 53 51 4c 49  c&3)==(enc&~SQLI
19d74 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44  TE_UTF16_ALIGNED
19d75 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  ) );.  assert( (
19d76 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pVal->flags & ME
19d77 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 3b 0a  M_RowSet)==0 );.
19d78 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 66 6c 61  .  if( pVal->fla
19d79 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20  gs&MEM_Null ){. 
19d7a 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
19d7b 0a 20 20 61 73 73 65 72 74 28 20 28 4d 45 4d 5f  .  assert( (MEM_
19d7c 42 6c 6f 62 3e 3e 33 29 20 3d 3d 20 4d 45 4d 5f  Blob>>3) == MEM_
19d7d 53 74 72 20 29 3b 0a 20 20 70 56 61 6c 2d 3e 66  Str );.  pVal->f
19d7e 6c 61 67 73 20 7c 3d 20 28 70 56 61 6c 2d 3e 66  lags |= (pVal->f
19d7f 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29  lags & MEM_Blob)
19d80 3e 3e 33 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f  >>3;.  expandBlo
19d81 62 28 70 56 61 6c 29 3b 0a 20 20 69 66 28 20 70  b(pVal);.  if( p
19d82 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53  Val->flags&MEM_S
19d83 74 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  tr ){.    sqlite
19d84 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64  3VdbeChangeEncod
19d85 69 6e 67 28 70 56 61 6c 2c 20 65 6e 63 20 26 20  ing(pVal, enc & 
19d86 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c  ~SQLITE_UTF16_AL
19d87 49 47 4e 45 44 29 3b 0a 20 20 20 20 69 66 28 20  IGNED);.    if( 
19d88 28 65 6e 63 20 26 20 53 51 4c 49 54 45 5f 55 54  (enc & SQLITE_UT
19d89 46 31 36 5f 41 4c 49 47 4e 45 44 29 21 3d 30 20  F16_ALIGNED)!=0 
19d8a 26 26 20 31 3d 3d 28 31 26 53 51 4c 49 54 45 5f  && 1==(1&SQLITE_
19d8b 50 54 52 5f 54 4f 5f 49 4e 54 28 70 56 61 6c 2d  PTR_TO_INT(pVal-
19d8c 3e 7a 29 29 20 29 7b 0a 20 20 20 20 20 20 61 73  >z)) ){.      as
19d8d 73 65 72 74 28 20 28 70 56 61 6c 2d 3e 66 6c 61  sert( (pVal->fla
19d8e 67 73 20 26 20 28 4d 45 4d 5f 45 70 68 65 6d 7c  gs & (MEM_Ephem|
19d8f 4d 45 4d 5f 53 74 61 74 69 63 29 29 21 3d 30 20  MEM_Static))!=0 
19d90 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  );.      if( sql
19d91 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57  ite3VdbeMemMakeW
19d92 72 69 74 65 61 62 6c 65 28 70 56 61 6c 29 21 3d  riteable(pVal)!=
19d93 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
19d94 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
19d95 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
19d96 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e   sqlite3VdbeMemN
19d97 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 56 61 6c  ulTerminate(pVal
19d98 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
19d99 61 73 73 65 72 74 28 20 28 70 56 61 6c 2d 3e 66  assert( (pVal->f
19d9a 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f 62 29 3d 3d  lags&MEM_Blob)==
19d9b 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 );.    sqlite3
19d9c 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79  VdbeMemStringify
19d9d 28 70 56 61 6c 2c 20 65 6e 63 29 3b 0a 20 20 20  (pVal, enc);.   
19d9e 20 61 73 73 65 72 74 28 20 30 3d 3d 28 31 26 53   assert( 0==(1&S
19d9f 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54  QLITE_PTR_TO_INT
19da0 28 70 56 61 6c 2d 3e 7a 29 29 20 29 3b 0a 20 20  (pVal->z)) );.  
19da1 7d 0a 20 20 61 73 73 65 72 74 28 70 56 61 6c 2d  }.  assert(pVal-
19da2 3e 65 6e 63 3d 3d 28 65 6e 63 20 26 20 7e 53 51  >enc==(enc & ~SQ
19da3 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e  LITE_UTF16_ALIGN
19da4 45 44 29 20 7c 7c 20 70 56 61 6c 2d 3e 64 62 3d  ED) || pVal->db=
19da5 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  =0.             
19da6 20 7c 7c 20 70 56 61 6c 2d 3e 64 62 2d 3e 6d 61   || pVal->db->ma
19da7 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
19da8 69 66 28 20 70 56 61 6c 2d 3e 65 6e 63 3d 3d 28  if( pVal->enc==(
19da9 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55 54  enc & ~SQLITE_UT
19daa 46 31 36 5f 41 4c 49 47 4e 45 44 29 20 29 7b 0a  F16_ALIGNED) ){.
19dab 20 20 20 20 72 65 74 75 72 6e 20 70 56 61 6c 2d      return pVal-
19dac 3e 7a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  >z;.  }else{.   
19dad 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d   return 0;.  }.}
19dae 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
19daf 20 6e 65 77 20 73 71 6c 69 74 65 33 5f 76 61 6c   new sqlite3_val
19db0 75 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51  ue object..*/.SQ
19db1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c  LITE_PRIVATE sql
19db2 69 74 65 33 5f 76 61 6c 75 65 20 2a 73 71 6c 69  ite3_value *sqli
19db3 74 65 33 56 61 6c 75 65 4e 65 77 28 73 71 6c 69  te3ValueNew(sqli
19db4 74 65 33 20 2a 64 62 29 7b 0a 20 20 4d 65 6d 20  te3 *db){.  Mem 
19db5 2a 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  *p = sqlite3DbMa
19db6 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
19db7 65 6f 66 28 2a 70 29 29 3b 0a 20 20 69 66 28 20  eof(*p));.  if( 
19db8 70 20 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61 67  p ){.    p->flag
19db9 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20  s = MEM_Null;.  
19dba 20 20 70 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49    p->type = SQLI
19dbb 54 45 5f 4e 55 4c 4c 3b 0a 20 20 20 20 70 2d 3e  TE_NULL;.    p->
19dbc 64 62 20 3d 20 64 62 3b 0a 20 20 7d 0a 20 20 72  db = db;.  }.  r
19dbd 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
19dbe 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 73  * Create a new s
19dbf 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a  qlite3_value obj
19dc0 65 63 74 2c 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ect, containing 
19dc1 74 68 65 20 76 61 6c 75 65 20 6f 66 20 70 45 78  the value of pEx
19dc2 70 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f  pr..**.** This o
19dc3 6e 6c 79 20 77 6f 72 6b 73 20 66 6f 72 20 76 65  nly works for ve
19dc4 72 79 20 73 69 6d 70 6c 65 20 65 78 70 72 65 73  ry simple expres
19dc5 73 69 6f 6e 73 20 74 68 61 74 20 63 6f 6e 73 69  sions that consi
19dc6 73 74 20 6f 66 20 6f 6e 65 20 63 6f 6e 73 74 61  st of one consta
19dc7 6e 74 0a 2a 2a 20 74 6f 6b 65 6e 20 28 69 2e 65  nt.** token (i.e
19dc8 2e 20 22 35 22 2c 20 22 35 2e 31 22 2c 20 22 27  . "5", "5.1", "'
19dc9 61 20 73 74 72 69 6e 67 27 22 29 2e 20 49 66 20  a string'"). If 
19dca 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 63  the expression c
19dcb 61 6e 0a 2a 2a 20 62 65 20 63 6f 6e 76 65 72 74  an.** be convert
19dcc 65 64 20 64 69 72 65 63 74 6c 79 20 69 6e 74 6f  ed directly into
19dcd 20 61 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 74   a value, then t
19dce 68 65 20 76 61 6c 75 65 20 69 73 20 61 6c 6c 6f  he value is allo
19dcf 63 61 74 65 64 20 61 6e 64 0a 2a 2a 20 61 20 70  cated and.** a p
19dd0 6f 69 6e 74 65 72 20 77 72 69 74 74 65 6e 20 74  ointer written t
19dd1 6f 20 2a 70 70 56 61 6c 2e 20 54 68 65 20 63 61  o *ppVal. The ca
19dd2 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69  ller is responsi
19dd3 62 6c 65 20 66 6f 72 20 64 65 61 6c 6c 6f 63 61  ble for dealloca
19dd4 74 69 6e 67 0a 2a 2a 20 74 68 65 20 76 61 6c 75  ting.** the valu
19dd5 65 20 62 79 20 70 61 73 73 69 6e 67 20 69 74 20  e by passing it 
19dd6 74 6f 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46  to sqlite3ValueF
19dd7 72 65 65 28 29 20 6c 61 74 65 72 20 6f 6e 2e 20  ree() later on. 
19dd8 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  If the expressio
19dd9 6e 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 63  n.** cannot be c
19dda 6f 6e 76 65 72 74 65 64 20 74 6f 20 61 20 76 61  onverted to a va
19ddb 6c 75 65 2c 20 74 68 65 6e 20 2a 70 70 56 61 6c  lue, then *ppVal
19ddc 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e   is set to NULL.
19ddd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19dde 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61  TE int sqlite3Va
19ddf 6c 75 65 46 72 6f 6d 45 78 70 72 28 0a 20 20 73  lueFromExpr(.  s
19de0 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
19de1 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
19de2 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
19de3 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ion */.  Expr *p
19de4 45 78 70 72 2c 20 20 20 20 20 20 20 20 20 20 20  Expr,           
19de5 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
19de6 73 69 6f 6e 20 74 6f 20 65 76 61 6c 75 61 74 65  sion to evaluate
19de7 20 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20   */.  u8 enc,   
19de8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19de9 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 74 6f 20 75  /* Encoding to u
19dea 73 65 20 2a 2f 0a 20 20 75 38 20 61 66 66 69 6e  se */.  u8 affin
19deb 69 74 79 2c 20 20 20 20 20 20 20 20 20 20 20 20  ity,            
19dec 20 20 2f 2a 20 41 66 66 69 6e 69 74 79 20 74 6f    /* Affinity to
19ded 20 75 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65   use */.  sqlite
19dee 33 5f 76 61 6c 75 65 20 2a 2a 70 70 56 61 6c 20  3_value **ppVal 
19def 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
19df0 20 6e 65 77 20 76 61 6c 75 65 20 68 65 72 65 20   new value here 
19df1 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6f 70 3b 0a  */.){.  int op;.
19df2 20 20 63 68 61 72 20 2a 7a 56 61 6c 20 3d 20 30    char *zVal = 0
19df3 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ;.  sqlite3_valu
19df4 65 20 2a 70 56 61 6c 20 3d 20 30 3b 0a 0a 20 20  e *pVal = 0;..  
19df5 69 66 28 20 21 70 45 78 70 72 20 29 7b 0a 20 20  if( !pExpr ){.  
19df6 20 20 2a 70 70 56 61 6c 20 3d 20 30 3b 0a 20 20    *ppVal = 0;.  
19df7 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
19df8 4f 4b 3b 0a 20 20 7d 0a 20 20 6f 70 20 3d 20 70  OK;.  }.  op = p
19df9 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 69 66 28 20  Expr->op;.  if( 
19dfa 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20  op==TK_REGISTER 
19dfb 29 7b 0a 20 20 20 20 6f 70 20 3d 20 70 45 78 70  ){.    op = pExp
19dfc 72 2d 3e 6f 70 32 3b 0a 20 20 7d 0a 0a 20 20 69  r->op2;.  }..  i
19dfd 66 28 20 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e 47  f( op==TK_STRING
19dfe 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54   || op==TK_FLOAT
19dff 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47   || op==TK_INTEG
19e00 45 52 20 29 7b 0a 20 20 20 20 70 56 61 6c 20 3d  ER ){.    pVal =
19e01 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77   sqlite3ValueNew
19e02 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 70 56  (db);.    if( pV
19e03 61 6c 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f  al==0 ) goto no_
19e04 6d 65 6d 3b 0a 20 20 20 20 69 66 28 20 45 78 70  mem;.    if( Exp
19e05 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78  rHasProperty(pEx
19e06 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29  pr, EP_IntValue)
19e07 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
19e08 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34  3VdbeMemSetInt64
19e09 28 70 56 61 6c 2c 20 28 69 36 34 29 70 45 78 70  (pVal, (i64)pExp
19e0a 72 2d 3e 75 2e 69 56 61 6c 75 65 29 3b 0a 20 20  r->u.iValue);.  
19e0b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a    }else{.      z
19e0c 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 53  Val = sqlite3DbS
19e0d 74 72 44 75 70 28 64 62 2c 20 70 45 78 70 72 2d  trDup(db, pExpr-
19e0e 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20  >u.zToken);.    
19e0f 20 20 69 66 28 20 7a 56 61 6c 3d 3d 30 20 29 20    if( zVal==0 ) 
19e10 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20  goto no_mem;.   
19e11 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53     sqlite3ValueS
19e12 65 74 53 74 72 28 70 56 61 6c 2c 20 2d 31 2c 20  etStr(pVal, -1, 
19e13 7a 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46  zVal, SQLITE_UTF
19e14 38 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  8, SQLITE_DYNAMI
19e15 43 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6f 70  C);.      if( op
19e16 3d 3d 54 4b 5f 46 4c 4f 41 54 20 29 20 70 56 61  ==TK_FLOAT ) pVa
19e17 6c 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  l->type = SQLITE
19e18 5f 46 4c 4f 41 54 3b 0a 20 20 20 20 7d 0a 20 20  _FLOAT;.    }.  
19e19 20 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 49 4e    if( (op==TK_IN
19e1a 54 45 47 45 52 20 7c 7c 20 6f 70 3d 3d 54 4b 5f  TEGER || op==TK_
19e1b 46 4c 4f 41 54 20 29 20 26 26 20 61 66 66 69 6e  FLOAT ) && affin
19e1c 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f  ity==SQLITE_AFF_
19e1d 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 73 71  NONE ){.      sq
19e1e 6c 69 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41  lite3ValueApplyA
19e1f 66 66 69 6e 69 74 79 28 70 56 61 6c 2c 20 53 51  ffinity(pVal, SQ
19e20 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
19e21 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a  , SQLITE_UTF8);.
19e22 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
19e23 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70 70   sqlite3ValueApp
19e24 6c 79 41 66 66 69 6e 69 74 79 28 70 56 61 6c 2c  lyAffinity(pVal,
19e25 20 61 66 66 69 6e 69 74 79 2c 20 53 51 4c 49 54   affinity, SQLIT
19e26 45 5f 55 54 46 38 29 3b 0a 20 20 20 20 7d 0a 20  E_UTF8);.    }. 
19e27 20 20 20 69 66 28 20 65 6e 63 21 3d 53 51 4c 49     if( enc!=SQLI
19e28 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 20  TE_UTF8 ){.     
19e29 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
19e2a 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 61 6c 2c  geEncoding(pVal,
19e2b 20 65 6e 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d   enc);.    }.  }
19e2c 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  else if( op==TK_
19e2d 55 4d 49 4e 55 53 20 29 20 7b 0a 20 20 20 20 69  UMINUS ) {.    i
19e2e 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71  f( SQLITE_OK==sq
19e2f 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78  lite3ValueFromEx
19e30 70 72 28 64 62 2c 70 45 78 70 72 2d 3e 70 4c 65  pr(db,pExpr->pLe
19e31 66 74 2c 65 6e 63 2c 61 66 66 69 6e 69 74 79 2c  ft,enc,affinity,
19e32 26 70 56 61 6c 29 20 29 7b 0a 20 20 20 20 20 20  &pVal) ){.      
19e33 70 56 61 6c 2d 3e 75 2e 69 20 3d 20 2d 31 20 2a  pVal->u.i = -1 *
19e34 20 70 56 61 6c 2d 3e 75 2e 69 3b 0a 20 20 20 20   pVal->u.i;.    
19e35 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 2d 31 20    /* (double)-1 
19e36 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54  In case of SQLIT
19e37 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
19e38 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20 20  POINT... */.    
19e39 20 20 70 56 61 6c 2d 3e 72 20 3d 20 28 64 6f 75    pVal->r = (dou
19e3a 62 6c 65 29 2d 31 20 2a 20 70 56 61 6c 2d 3e 72  ble)-1 * pVal->r
19e3b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 6e  ;.    }.  }.#ifn
19e3c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
19e3d 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 20 20 65  BLOB_LITERAL.  e
19e3e 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 42  lse if( op==TK_B
19e3f 4c 4f 42 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  LOB ){.    int n
19e40 56 61 6c 3b 0a 20 20 20 20 61 73 73 65 72 74 28  Val;.    assert(
19e41 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e   pExpr->u.zToken
19e42 5b 30 5d 3d 3d 27 78 27 20 7c 7c 20 70 45 78 70  [0]=='x' || pExp
19e43 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d  r->u.zToken[0]==
19e44 27 58 27 20 29 3b 0a 20 20 20 20 61 73 73 65 72  'X' );.    asser
19e45 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  t( pExpr->u.zTok
19e46 65 6e 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20  en[1]=='\'' );. 
19e47 20 20 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65     pVal = sqlite
19e48 33 56 61 6c 75 65 4e 65 77 28 64 62 29 3b 0a 20  3ValueNew(db);. 
19e49 20 20 20 69 66 28 20 21 70 56 61 6c 20 29 20 67     if( !pVal ) g
19e4a 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20  oto no_mem;.    
19e4b 7a 56 61 6c 20 3d 20 26 70 45 78 70 72 2d 3e 75  zVal = &pExpr->u
19e4c 2e 7a 54 6f 6b 65 6e 5b 32 5d 3b 0a 20 20 20 20  .zToken[2];.    
19e4d 6e 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 53 74  nVal = sqlite3St
19e4e 72 6c 65 6e 33 30 28 7a 56 61 6c 29 2d 31 3b 0a  rlen30(zVal)-1;.
19e4f 20 20 20 20 61 73 73 65 72 74 28 20 7a 56 61 6c      assert( zVal
19e50 5b 6e 56 61 6c 5d 3d 3d 27 5c 27 27 20 29 3b 0a  [nVal]=='\'' );.
19e51 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
19e52 65 6d 53 65 74 53 74 72 28 70 56 61 6c 2c 20 73  emSetStr(pVal, s
19e53 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28  qlite3HexToBlob(
19e54 64 62 2c 20 7a 56 61 6c 2c 20 6e 56 61 6c 29 2c  db, zVal, nVal),
19e55 20 6e 56 61 6c 2f 32 2c 0a 20 20 20 20 20 20 20   nVal/2,.       
19e56 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
19e57 20 20 30 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41    0, SQLITE_DYNA
19e58 4d 49 43 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  MIC);.  }.#endif
19e59 0a 0a 20 20 69 66 28 20 70 56 61 6c 20 29 7b 0a  ..  if( pVal ){.
19e5a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
19e5b 65 6d 53 74 6f 72 65 54 79 70 65 28 70 56 61 6c  emStoreType(pVal
19e5c 29 3b 0a 20 20 7d 0a 20 20 2a 70 70 56 61 6c 20  );.  }.  *ppVal 
19e5d 3d 20 70 56 61 6c 3b 0a 20 20 72 65 74 75 72 6e  = pVal;.  return
19e5e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 6e 6f 5f   SQLITE_OK;..no_
19e5f 6d 65 6d 3a 0a 20 20 64 62 2d 3e 6d 61 6c 6c 6f  mem:.  db->mallo
19e60 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 73  cFailed = 1;.  s
19e61 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
19e62 20 7a 56 61 6c 29 3b 0a 20 20 73 71 6c 69 74 65   zVal);.  sqlite
19e63 33 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29  3ValueFree(pVal)
19e64 3b 0a 20 20 2a 70 70 56 61 6c 20 3d 20 30 3b 0a  ;.  *ppVal = 0;.
19e65 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
19e66 4e 4f 4d 45 4d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  NOMEM;.}../*.** 
19e67 43 68 61 6e 67 65 20 74 68 65 20 73 74 72 69 6e  Change the strin
19e68 67 20 76 61 6c 75 65 20 6f 66 20 61 6e 20 73 71  g value of an sq
19e69 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
19e6a 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  ct.*/.SQLITE_PRI
19e6b 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
19e6c 33 56 61 6c 75 65 53 65 74 53 74 72 28 0a 20 20  3ValueSetStr(.  
19e6d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 76  sqlite3_value *v
19e6e 2c 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74  ,     /* Value t
19e6f 6f 20 62 65 20 73 65 74 20 2a 2f 0a 20 20 69 6e  o be set */.  in
19e70 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  t n,            
19e71 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
19e72 20 73 74 72 69 6e 67 20 7a 20 2a 2f 0a 20 20 63   string z */.  c
19e73 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 20 20  onst void *z,   
19e74 20 20 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20       /* Text of 
19e75 74 68 65 20 6e 65 77 20 73 74 72 69 6e 67 20 2a  the new string *
19e76 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20 20  /.  u8 enc,     
19e77 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63            /* Enc
19e78 6f 64 69 6e 67 20 74 6f 20 75 73 65 20 2a 2f 0a  oding to use */.
19e79 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76    void (*xDel)(v
19e7a 6f 69 64 2a 29 20 20 20 2f 2a 20 44 65 73 74 72  oid*)   /* Destr
19e7b 75 63 74 6f 72 20 66 6f 72 20 74 68 65 20 73 74  uctor for the st
19e7c 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 66 28  ring */.){.  if(
19e7d 20 76 20 29 20 73 71 6c 69 74 65 33 56 64 62 65   v ) sqlite3Vdbe
19e7e 4d 65 6d 53 65 74 53 74 72 28 28 4d 65 6d 20 2a  MemSetStr((Mem *
19e7f 29 76 2c 20 7a 2c 20 6e 2c 20 65 6e 63 2c 20 78  )v, z, n, enc, x
19e80 44 65 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46  Del);.}../*.** F
19e81 72 65 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 76  ree an sqlite3_v
19e82 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53  alue object.*/.S
19e83 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
19e84 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46  id sqlite3ValueF
19e85 72 65 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  ree(sqlite3_valu
19e86 65 20 2a 76 29 7b 0a 20 20 69 66 28 20 21 76 20  e *v){.  if( !v 
19e87 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69  ) return;.  sqli
19e88 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
19e89 65 28 28 4d 65 6d 20 2a 29 76 29 3b 0a 20 20 73  e((Mem *)v);.  s
19e8a 71 6c 69 74 65 33 44 62 46 72 65 65 28 28 28 4d  qlite3DbFree(((M
19e8b 65 6d 2a 29 76 29 2d 3e 64 62 2c 20 76 29 3b 0a  em*)v)->db, v);.
19e8c 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
19e8d 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
19e8e 74 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74  tes in the sqlit
19e8f 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 20  e3_value object 
19e90 61 73 73 75 6d 69 6e 67 0a 2a 2a 20 74 68 61 74  assuming.** that
19e91 20 69 74 20 75 73 65 73 20 74 68 65 20 65 6e 63   it uses the enc
19e92 6f 64 69 6e 67 20 22 65 6e 63 22 0a 2a 2f 0a 53  oding "enc".*/.S
19e93 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
19e94 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79  t sqlite3ValueBy
19e95 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  tes(sqlite3_valu
19e96 65 20 2a 70 56 61 6c 2c 20 75 38 20 65 6e 63 29  e *pVal, u8 enc)
19e97 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20 28 4d 65  {.  Mem *p = (Me
19e98 6d 2a 29 70 56 61 6c 3b 0a 20 20 69 66 28 20 28  m*)pVal;.  if( (
19e99 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42  p->flags & MEM_B
19e9a 6c 6f 62 29 21 3d 30 20 7c 7c 20 73 71 6c 69 74  lob)!=0 || sqlit
19e9b 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c  e3ValueText(pVal
19e9c 2c 20 65 6e 63 29 20 29 7b 0a 20 20 20 20 69 66  , enc) ){.    if
19e9d 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  ( p->flags & MEM
19e9e 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20 72  _Zero ){.      r
19e9f 65 74 75 72 6e 20 70 2d 3e 6e 20 2b 20 70 2d 3e  eturn p->n + p->
19ea0 75 2e 6e 5a 65 72 6f 3b 0a 20 20 20 20 7d 65 6c  u.nZero;.    }el
19ea1 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  se{.      return
19ea2 20 70 2d 3e 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d   p->n;.    }.  }
19ea3 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
19ea4 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
19ea5 45 6e 64 20 6f 66 20 76 64 62 65 6d 65 6d 2e 63  End of vdbemem.c
19ea6 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
19ea7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ea8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
19ea9 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
19eaa 42 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 61  Begin file vdbea
19eab 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ux.c ***********
19eac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ead 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
19eae 2f 2a 0a 2a 2a 20 32 30 30 33 20 53 65 70 74 65  /*.** 2003 Septe
19eaf 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65  mber 6.**.** The
19eb0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
19eb1 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
19eb2 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
19eb3 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
19eb4 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
19eb5 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
19eb6 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
19eb7 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
19eb8 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
19eb9 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
19eba 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
19ebb 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
19ebc 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
19ebd 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
19ebe 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
19ebf 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
19ec0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
19ec1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ec2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ec3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ec4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
19ec5 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
19ec6 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
19ec7 20 75 73 65 64 20 66 6f 72 20 63 72 65 61 74 69   used for creati
19ec8 6e 67 2c 20 64 65 73 74 72 6f 79 69 6e 67 2c 20  ng, destroying, 
19ec9 61 6e 64 20 70 6f 70 75 6c 61 74 69 6e 67 0a 2a  and populating.*
19eca 2a 20 61 20 56 44 42 45 20 28 6f 72 20 61 6e 20  * a VDBE (or an 
19ecb 22 73 71 6c 69 74 65 33 5f 73 74 6d 74 22 20 61  "sqlite3_stmt" a
19ecc 73 20 69 74 20 69 73 20 6b 6e 6f 77 6e 20 74 6f  s it is known to
19ecd 20 74 68 65 20 6f 75 74 73 69 64 65 20 77 6f 72   the outside wor
19ece 6c 64 2e 29 20 20 50 72 69 6f 72 0a 2a 2a 20 74  ld.)  Prior.** t
19ecf 6f 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 37 2c  o version 2.8.7,
19ed0 20 61 6c 6c 20 74 68 69 73 20 63 6f 64 65 20 77   all this code w
19ed1 61 73 20 63 6f 6d 62 69 6e 65 64 20 69 6e 74 6f  as combined into
19ed2 20 74 68 65 20 76 64 62 65 2e 63 20 73 6f 75 72   the vdbe.c sour
19ed3 63 65 20 66 69 6c 65 2e 0a 2a 2a 20 42 75 74 20  ce file..** But 
19ed4 74 68 61 74 20 66 69 6c 65 20 77 61 73 20 67 65  that file was ge
19ed5 74 74 69 6e 67 20 74 6f 6f 20 62 69 67 20 73 6f  tting too big so
19ed6 20 74 68 69 73 20 73 75 62 72 6f 75 74 69 6e 65   this subroutine
19ed7 73 20 77 65 72 65 20 73 70 6c 69 74 20 6f 75 74  s were split out
19ed8 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62  ..**.** $Id: vdb
19ed9 65 61 75 78 2e 63 2c 76 20 31 2e 34 38 30 20 32  eaux.c,v 1.480 2
19eda 30 30 39 2f 30 38 2f 30 38 20 31 38 3a 30 31 3a  009/08/08 18:01:
19edb 30 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  08 drh Exp $.*/.
19edc 0a 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 64 65  .../*.** When de
19edd 62 75 67 67 69 6e 67 20 74 68 65 20 63 6f 64 65  bugging the code
19ede 20 67 65 6e 65 72 61 74 6f 72 20 69 6e 20 61 20   generator in a 
19edf 73 79 6d 62 6f 6c 69 63 20 64 65 62 75 67 67 65  symbolic debugge
19ee0 72 2c 20 6f 6e 65 20 63 61 6e 0a 2a 2a 20 73 65  r, one can.** se
19ee1 74 20 74 68 65 20 73 71 6c 69 74 65 33 56 64 62  t the sqlite3Vdb
19ee2 65 41 64 64 6f 70 54 72 61 63 65 20 74 6f 20 31  eAddopTrace to 1
19ee3 20 61 6e 64 20 61 6c 6c 20 6f 70 63 6f 64 65 73   and all opcodes
19ee4 20 77 69 6c 6c 20 62 65 20 70 72 69 6e 74 65 64   will be printed
19ee5 0a 2a 2a 20 61 73 20 74 68 65 79 20 61 72 65 20  .** as they are 
19ee6 61 64 64 65 64 20 74 6f 20 74 68 65 20 69 6e 73  added to the ins
19ee7 74 72 75 63 74 69 6f 6e 20 73 74 72 65 61 6d 2e  truction stream.
19ee8 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
19ee9 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50  E_DEBUG.SQLITE_P
19eea 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
19eeb 65 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65  e3VdbeAddopTrace
19eec 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f   = 0;.#endif.../
19eed 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
19eee 77 20 76 69 72 74 75 61 6c 20 64 61 74 61 62 61  w virtual databa
19eef 73 65 20 65 6e 67 69 6e 65 2e 0a 2a 2f 0a 53 51  se engine..*/.SQ
19ef0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62  LITE_PRIVATE Vdb
19ef1 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72  e *sqlite3VdbeCr
19ef2 65 61 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62  eate(sqlite3 *db
19ef3 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20  ){.  Vdbe *p;.  
19ef4 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  p = sqlite3DbMal
19ef5 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
19ef6 6f 66 28 56 64 62 65 29 20 29 3b 0a 20 20 69 66  of(Vdbe) );.  if
19ef7 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
19ef8 30 3b 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b  0;.  p->db = db;
19ef9 0a 20 20 69 66 28 20 64 62 2d 3e 70 56 64 62 65  .  if( db->pVdbe
19efa 20 29 7b 0a 20 20 20 20 64 62 2d 3e 70 56 64 62   ){.    db->pVdb
19efb 65 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a 20 20  e->pPrev = p;.  
19efc 7d 0a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 64  }.  p->pNext = d
19efd 62 2d 3e 70 56 64 62 65 3b 0a 20 20 70 2d 3e 70  b->pVdbe;.  p->p
19efe 50 72 65 76 20 3d 20 30 3b 0a 20 20 64 62 2d 3e  Prev = 0;.  db->
19eff 70 56 64 62 65 20 3d 20 70 3b 0a 20 20 70 2d 3e  pVdbe = p;.  p->
19f00 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47  magic = VDBE_MAG
19f01 49 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 72  IC_INIT;.  retur
19f02 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  n p;.}../*.** Re
19f03 6d 65 6d 62 65 72 20 74 68 65 20 53 51 4c 20 73  member the SQL s
19f04 74 72 69 6e 67 20 66 6f 72 20 61 20 70 72 65 70  tring for a prep
19f05 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 2e 0a  ared statement..
19f06 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
19f07 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
19f08 62 65 53 65 74 53 71 6c 28 56 64 62 65 20 2a 70  beSetSql(Vdbe *p
19f09 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c  , const char *z,
19f0a 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 73 50 72   int n, int isPr
19f0b 65 70 61 72 65 56 32 29 7b 0a 20 20 61 73 73 65  epareV2){.  asse
19f0c 72 74 28 20 69 73 50 72 65 70 61 72 65 56 32 3d  rt( isPrepareV2=
19f0d 3d 31 20 7c 7c 20 69 73 50 72 65 70 61 72 65 56  =1 || isPrepareV
19f0e 32 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 3d  2==0 );.  if( p=
19f0f 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69 66  =0 ) return;.#if
19f10 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
19f11 54 52 41 43 45 0a 20 20 69 66 28 20 21 69 73 50  TRACE.  if( !isP
19f12 72 65 70 61 72 65 56 32 20 29 20 72 65 74 75 72  repareV2 ) retur
19f13 6e 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73 73 65  n;.#endif.  asse
19f14 72 74 28 20 70 2d 3e 7a 53 71 6c 3d 3d 30 20 29  rt( p->zSql==0 )
19f15 3b 0a 20 20 70 2d 3e 7a 53 71 6c 20 3d 20 73 71  ;.  p->zSql = sq
19f16 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 70  lite3DbStrNDup(p
19f17 2d 3e 64 62 2c 20 7a 2c 20 6e 29 3b 0a 20 20 70  ->db, z, n);.  p
19f18 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 3d 20  ->isPrepareV2 = 
19f19 69 73 50 72 65 70 61 72 65 56 32 3b 0a 7d 0a 0a  isPrepareV2;.}..
19f1a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
19f1b 20 53 51 4c 20 61 73 73 6f 63 69 61 74 65 64 20   SQL associated 
19f1c 77 69 74 68 20 61 20 70 72 65 70 61 72 65 64 20  with a prepared 
19f1d 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c  statement.*/.SQL
19f1e 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
19f1f 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28  ar *sqlite3_sql(
19f20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
19f21 74 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 20  tmt){.  Vdbe *p 
19f22 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b  = (Vdbe *)pStmt;
19f23 0a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 69 73  .  return (p->is
19f24 50 72 65 70 61 72 65 56 32 20 3f 20 70 2d 3e 7a  PrepareV2 ? p->z
19f25 53 71 6c 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a  Sql : 0);.}../*.
19f26 2a 2a 20 53 77 61 70 20 61 6c 6c 20 63 6f 6e 74  ** Swap all cont
19f27 65 6e 74 20 62 65 74 77 65 65 6e 20 74 77 6f 20  ent between two 
19f28 56 44 42 45 20 73 74 72 75 63 74 75 72 65 73 2e  VDBE structures.
19f29 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
19f2a 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
19f2b 64 62 65 53 77 61 70 28 56 64 62 65 20 2a 70 41  dbeSwap(Vdbe *pA
19f2c 2c 20 56 64 62 65 20 2a 70 42 29 7b 0a 20 20 56  , Vdbe *pB){.  V
19f2d 64 62 65 20 74 6d 70 2c 20 2a 70 54 6d 70 3b 0a  dbe tmp, *pTmp;.
19f2e 20 20 63 68 61 72 20 2a 7a 54 6d 70 3b 0a 20 20    char *zTmp;.  
19f2f 74 6d 70 20 3d 20 2a 70 41 3b 0a 20 20 2a 70 41  tmp = *pA;.  *pA
19f30 20 3d 20 2a 70 42 3b 0a 20 20 2a 70 42 20 3d 20   = *pB;.  *pB = 
19f31 74 6d 70 3b 0a 20 20 70 54 6d 70 20 3d 20 70 41  tmp;.  pTmp = pA
19f32 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 41 2d 3e 70  ->pNext;.  pA->p
19f33 4e 65 78 74 20 3d 20 70 42 2d 3e 70 4e 65 78 74  Next = pB->pNext
19f34 3b 0a 20 20 70 42 2d 3e 70 4e 65 78 74 20 3d 20  ;.  pB->pNext = 
19f35 70 54 6d 70 3b 0a 20 20 70 54 6d 70 20 3d 20 70  pTmp;.  pTmp = p
19f36 41 2d 3e 70 50 72 65 76 3b 0a 20 20 70 41 2d 3e  A->pPrev;.  pA->
19f37 70 50 72 65 76 20 3d 20 70 42 2d 3e 70 50 72 65  pPrev = pB->pPre
19f38 76 3b 0a 20 20 70 42 2d 3e 70 50 72 65 76 20 3d  v;.  pB->pPrev =
19f39 20 70 54 6d 70 3b 0a 20 20 7a 54 6d 70 20 3d 20   pTmp;.  zTmp = 
19f3a 70 41 2d 3e 7a 53 71 6c 3b 0a 20 20 70 41 2d 3e  pA->zSql;.  pA->
19f3b 7a 53 71 6c 20 3d 20 70 42 2d 3e 7a 53 71 6c 3b  zSql = pB->zSql;
19f3c 0a 20 20 70 42 2d 3e 7a 53 71 6c 20 3d 20 7a 54  .  pB->zSql = zT
19f3d 6d 70 3b 0a 20 20 70 42 2d 3e 69 73 50 72 65 70  mp;.  pB->isPrep
19f3e 61 72 65 56 32 20 3d 20 70 41 2d 3e 69 73 50 72  areV2 = pA->isPr
19f3f 65 70 61 72 65 56 32 3b 0a 7d 0a 0a 23 69 66 64  epareV2;.}..#ifd
19f40 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
19f41 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 72 61 63 69  /*.** Turn traci
19f42 6e 67 20 6f 6e 20 6f 72 20 6f 66 66 0a 2a 2f 0a  ng on or off.*/.
19f43 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
19f44 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 54  oid sqlite3VdbeT
19f45 72 61 63 65 28 56 64 62 65 20 2a 70 2c 20 46 49  race(Vdbe *p, FI
19f46 4c 45 20 2a 74 72 61 63 65 29 7b 0a 20 20 70 2d  LE *trace){.  p-
19f47 3e 74 72 61 63 65 20 3d 20 74 72 61 63 65 3b 0a  >trace = trace;.
19f48 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
19f49 52 65 73 69 7a 65 20 74 68 65 20 56 64 62 65 2e  Resize the Vdbe.
19f4a 61 4f 70 20 61 72 72 61 79 20 73 6f 20 74 68 61  aOp array so tha
19f4b 74 20 69 74 20 69 73 20 61 74 20 6c 65 61 73 74  t it is at least
19f4c 20 6f 6e 65 20 6f 70 20 6c 61 72 67 65 72 20 74   one op larger t
19f4d 68 61 6e 20 0a 2a 2a 20 69 74 20 77 61 73 2e 0a  han .** it was..
19f4e 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f 75 74 2d  **.** If an out-
19f4f 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f 72 20  of-memory error 
19f50 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 65 73  occurs while res
19f51 69 7a 69 6e 67 20 74 68 65 20 61 72 72 61 79 2c  izing the array,
19f52 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54   return.** SQLIT
19f53 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74 68 69 73  E_NOMEM. In this
19f54 20 63 61 73 65 20 56 64 62 65 2e 61 4f 70 20 61   case Vdbe.aOp a
19f55 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c 6c 6f 63  nd Vdbe.nOpAlloc
19f56 20 72 65 6d 61 69 6e 20 0a 2a 2a 20 75 6e 63 68   remain .** unch
19f57 61 6e 67 65 64 20 28 74 68 69 73 20 69 73 20 73  anged (this is s
19f58 6f 20 74 68 61 74 20 61 6e 79 20 6f 70 63 6f 64  o that any opcod
19f59 65 73 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63  es already alloc
19f5a 61 74 65 64 20 63 61 6e 20 62 65 20 0a 2a 2a 20  ated can be .** 
19f5b 63 6f 72 72 65 63 74 6c 79 20 64 65 61 6c 6c 6f  correctly deallo
19f5c 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68  cated along with
19f5d 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65   the rest of the
19f5e 20 56 64 62 65 29 2e 0a 2a 2f 0a 73 74 61 74 69   Vdbe)..*/.stati
19f5f 63 20 69 6e 74 20 67 72 6f 77 4f 70 41 72 72 61  c int growOpArra
19f60 79 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 56 64  y(Vdbe *p){.  Vd
19f61 62 65 4f 70 20 2a 70 4e 65 77 3b 0a 20 20 69 6e  beOp *pNew;.  in
19f62 74 20 6e 4e 65 77 20 3d 20 28 70 2d 3e 6e 4f 70  t nNew = (p->nOp
19f63 41 6c 6c 6f 63 20 3f 20 70 2d 3e 6e 4f 70 41 6c  Alloc ? p->nOpAl
19f64 6c 6f 63 2a 32 20 3a 20 28 69 6e 74 29 28 31 30  loc*2 : (int)(10
19f65 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29 29 29 3b  24/sizeof(Op)));
19f66 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
19f67 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d 3e 64 62  3DbRealloc(p->db
19f68 2c 20 70 2d 3e 61 4f 70 2c 20 6e 4e 65 77 2a 73  , p->aOp, nNew*s
19f69 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20 20 69 66  izeof(Op));.  if
19f6a 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 70 2d  ( pNew ){.    p-
19f6b 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 73 71 6c 69  >nOpAlloc = sqli
19f6c 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28  te3DbMallocSize(
19f6d 70 2d 3e 64 62 2c 20 70 4e 65 77 29 2f 73 69 7a  p->db, pNew)/siz
19f6e 65 6f 66 28 4f 70 29 3b 0a 20 20 20 20 70 2d 3e  eof(Op);.    p->
19f6f 61 4f 70 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a  aOp = pNew;.  }.
19f70 20 20 72 65 74 75 72 6e 20 28 70 4e 65 77 20 3f    return (pNew ?
19f71 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c   SQLITE_OK : SQL
19f72 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a 0a 2f  ITE_NOMEM);.}../
19f73 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 69  *.** Add a new i
19f74 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 74 68  nstruction to th
19f75 65 20 6c 69 73 74 20 6f 66 20 69 6e 73 74 72 75  e list of instru
19f76 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 20 69  ctions current i
19f77 6e 20 74 68 65 0a 2a 2a 20 56 44 42 45 2e 20 20  n the.** VDBE.  
19f78 52 65 74 75 72 6e 20 74 68 65 20 61 64 64 72 65  Return the addre
19f79 73 73 20 6f 66 20 74 68 65 20 6e 65 77 20 69 6e  ss of the new in
19f7a 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  struction..**.**
19f7b 20 50 61 72 61 6d 65 74 65 72 73 3a 0a 2a 2a 0a   Parameters:.**.
19f7c 2a 2a 20 20 20 20 70 20 20 20 20 20 20 20 20 20  **    p         
19f7d 20 20 20 20 20 20 50 6f 69 6e 74 65 72 20 74 6f        Pointer to
19f7e 20 74 68 65 20 56 44 42 45 0a 2a 2a 0a 2a 2a 20   the VDBE.**.** 
19f7f 20 20 20 6f 70 20 20 20 20 20 20 20 20 20 20 20     op           
19f80 20 20 20 54 68 65 20 6f 70 63 6f 64 65 20 66 6f     The opcode fo
19f81 72 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  r this instructi
19f82 6f 6e 0a 2a 2a 0a 2a 2a 20 20 20 20 70 31 2c 20  on.**.**    p1, 
19f83 70 32 2c 20 70 33 20 20 20 20 20 20 4f 70 65 72  p2, p3      Oper
19f84 61 6e 64 73 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74  ands.**.** Use t
19f85 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  he sqlite3VdbeRe
19f86 73 6f 6c 76 65 4c 61 62 65 6c 28 29 20 66 75 6e  solveLabel() fun
19f87 63 74 69 6f 6e 20 74 6f 20 66 69 78 20 61 6e 20  ction to fix an 
19f88 61 64 64 72 65 73 73 20 61 6e 64 0a 2a 2a 20 74  address and.** t
19f89 68 65 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  he sqlite3VdbeCh
19f8a 61 6e 67 65 50 34 28 29 20 66 75 6e 63 74 69 6f  angeP4() functio
19f8b 6e 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20  n to change the 
19f8c 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 34 0a  value of the P4.
19f8d 2a 2a 20 6f 70 65 72 61 6e 64 2e 0a 2a 2f 0a 53  ** operand..*/.S
19f8e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
19f8f 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  t sqlite3VdbeAdd
19f90 4f 70 33 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  Op3(Vdbe *p, int
19f91 20 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69 6e 74   op, int p1, int
19f92 20 70 32 2c 20 69 6e 74 20 70 33 29 7b 0a 20 20   p2, int p3){.  
19f93 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 4f 70 20  int i;.  VdbeOp 
19f94 2a 70 4f 70 3b 0a 0a 20 20 69 20 3d 20 70 2d 3e  *pOp;..  i = p->
19f95 6e 4f 70 3b 0a 20 20 61 73 73 65 72 74 28 20 70  nOp;.  assert( p
19f96 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
19f97 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 61 73  GIC_INIT );.  as
19f98 73 65 72 74 28 20 6f 70 3e 30 20 26 26 20 6f 70  sert( op>0 && op
19f99 3c 30 78 66 66 20 29 3b 0a 20 20 69 66 28 20 70  <0xff );.  if( p
19f9a 2d 3e 6e 4f 70 41 6c 6c 6f 63 3c 3d 69 20 29 7b  ->nOpAlloc<=i ){
19f9b 0a 20 20 20 20 69 66 28 20 67 72 6f 77 4f 70 41  .    if( growOpA
19f9c 72 72 61 79 28 70 29 20 29 7b 0a 20 20 20 20 20  rray(p) ){.     
19f9d 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
19f9e 0a 20 20 7d 0a 20 20 70 2d 3e 6e 4f 70 2b 2b 3b  .  }.  p->nOp++;
19f9f 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70  .  pOp = &p->aOp
19fa0 5b 69 5d 3b 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f  [i];.  pOp->opco
19fa1 64 65 20 3d 20 28 75 38 29 6f 70 3b 0a 20 20 70  de = (u8)op;.  p
19fa2 4f 70 2d 3e 70 35 20 3d 20 30 3b 0a 20 20 70 4f  Op->p5 = 0;.  pO
19fa3 70 2d 3e 70 31 20 3d 20 70 31 3b 0a 20 20 70 4f  p->p1 = p1;.  pO
19fa4 70 2d 3e 70 32 20 3d 20 70 32 3b 0a 20 20 70 4f  p->p2 = p2;.  pO
19fa5 70 2d 3e 70 33 20 3d 20 70 33 3b 0a 20 20 70 4f  p->p3 = p3;.  pO
19fa6 70 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 70  p->p4.p = 0;.  p
19fa7 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f  Op->p4type = P4_
19fa8 4e 4f 54 55 53 45 44 3b 0a 20 20 70 2d 3e 65 78  NOTUSED;.  p->ex
19fa9 70 69 72 65 64 20 3d 20 30 3b 0a 23 69 66 64 65  pired = 0;.#ifde
19faa 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
19fab 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d   pOp->zComment =
19fac 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65   0;.  if( sqlite
19fad 33 56 64 62 65 41 64 64 6f 70 54 72 61 63 65 20  3VdbeAddopTrace 
19fae 29 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69  ) sqlite3VdbePri
19faf 6e 74 4f 70 28 30 2c 20 69 2c 20 26 70 2d 3e 61  ntOp(0, i, &p->a
19fb0 4f 70 5b 69 5d 29 3b 0a 23 65 6e 64 69 66 0a 23  Op[i]);.#endif.#
19fb1 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49  ifdef VDBE_PROFI
19fb2 4c 45 0a 20 20 70 4f 70 2d 3e 63 79 63 6c 65 73  LE.  pOp->cycles
19fb3 20 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 63 6e 74   = 0;.  pOp->cnt
19fb4 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72   = 0;.#endif.  r
19fb5 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c 49 54  eturn i;.}.SQLIT
19fb6 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
19fb7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28  lite3VdbeAddOp0(
19fb8 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6f 70 29  Vdbe *p, int op)
19fb9 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
19fba 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 2c 20  e3VdbeAddOp3(p, 
19fbb 6f 70 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 7d 0a  op, 0, 0, 0);.}.
19fbc 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
19fbd 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
19fbe 64 4f 70 31 28 56 64 62 65 20 2a 70 2c 20 69 6e  dOp1(Vdbe *p, in
19fbf 74 20 6f 70 2c 20 69 6e 74 20 70 31 29 7b 0a 20  t op, int p1){. 
19fc0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56   return sqlite3V
19fc1 64 62 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c  dbeAddOp3(p, op,
19fc2 20 70 31 2c 20 30 2c 20 30 29 3b 0a 7d 0a 53 51   p1, 0, 0);.}.SQ
19fc3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
19fc4 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
19fc5 70 32 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20  p2(Vdbe *p, int 
19fc6 6f 70 2c 20 69 6e 74 20 70 31 2c 20 69 6e 74 20  op, int p1, int 
19fc7 70 32 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  p2){.  return sq
19fc8 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
19fc9 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 20 30  p, op, p1, p2, 0
19fca 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64 64  );.}.../*.** Add
19fcb 20 61 6e 20 6f 70 63 6f 64 65 20 74 68 61 74 20   an opcode that 
19fcc 69 6e 63 6c 75 64 65 73 20 74 68 65 20 70 34 20  includes the p4 
19fcd 76 61 6c 75 65 20 61 73 20 61 20 70 6f 69 6e 74  value as a point
19fce 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
19fcf 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
19fd0 33 56 64 62 65 41 64 64 4f 70 34 28 0a 20 20 56  3VdbeAddOp4(.  V
19fd1 64 62 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20  dbe *p,         
19fd2 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 6f 70     /* Add the op
19fd3 63 6f 64 65 20 74 6f 20 74 68 69 73 20 56 4d 20  code to this VM 
19fd4 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20  */.  int op,    
19fd5 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
19fd6 6e 65 77 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20  new opcode */.  
19fd7 69 6e 74 20 70 31 2c 20 20 20 20 20 20 20 20 20  int p1,         
19fd8 20 20 20 20 2f 2a 20 54 68 65 20 50 31 20 6f 70      /* The P1 op
19fd9 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 70  erand */.  int p
19fda 32 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  2,             /
19fdb 2a 20 54 68 65 20 50 32 20 6f 70 65 72 61 6e 64  * The P2 operand
19fdc 20 2a 2f 0a 20 20 69 6e 74 20 70 33 2c 20 20 20   */.  int p3,   
19fdd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
19fde 20 50 33 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20   P3 operand */. 
19fdf 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34   const char *zP4
19fe0 2c 20 20 20 20 2f 2a 20 54 68 65 20 50 34 20 6f  ,    /* The P4 o
19fe1 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20  perand */.  int 
19fe2 70 34 74 79 70 65 20 20 20 20 20 20 20 20 20 20  p4type          
19fe3 2f 2a 20 50 34 20 6f 70 65 72 61 6e 64 20 74 79  /* P4 operand ty
19fe4 70 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61  pe */.){.  int a
19fe5 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
19fe6 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70  eAddOp3(p, op, p
19fe7 31 2c 20 70 32 2c 20 70 33 29 3b 0a 20 20 73 71  1, p2, p3);.  sq
19fe8 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
19fe9 34 28 70 2c 20 61 64 64 72 2c 20 7a 50 34 2c 20  4(p, addr, zP4, 
19fea 70 34 74 79 70 65 29 3b 0a 20 20 72 65 74 75 72  p4type);.  retur
19feb 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n addr;.}../*.**
19fec 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 73 79   Create a new sy
19fed 6d 62 6f 6c 69 63 20 6c 61 62 65 6c 20 66 6f 72  mbolic label for
19fee 20 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20   an instruction 
19fef 74 68 61 74 20 68 61 73 20 79 65 74 20 74 6f 20  that has yet to 
19ff0 62 65 0a 2a 2a 20 63 6f 64 65 64 2e 20 20 54 68  be.** coded.  Th
19ff1 65 20 73 79 6d 62 6f 6c 69 63 20 6c 61 62 65 6c  e symbolic label
19ff2 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20   is really just 
19ff3 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d 62 65  a negative numbe
19ff4 72 2e 20 20 54 68 65 0a 2a 2a 20 6c 61 62 65 6c  r.  The.** label
19ff5 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20   can be used as 
19ff6 74 68 65 20 50 32 20 76 61 6c 75 65 20 6f 66 20  the P2 value of 
19ff7 61 6e 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4c  an operation.  L
19ff8 61 74 65 72 2c 20 77 68 65 6e 0a 2a 2a 20 74 68  ater, when.** th
19ff9 65 20 6c 61 62 65 6c 20 69 73 20 72 65 73 6f 6c  e label is resol
19ffa 76 65 64 20 74 6f 20 61 20 73 70 65 63 69 66 69  ved to a specifi
19ffb 63 20 61 64 64 72 65 73 73 2c 20 74 68 65 20 56  c address, the V
19ffc 44 42 45 20 77 69 6c 6c 20 73 63 61 6e 0a 2a 2a  DBE will scan.**
19ffd 20 74 68 72 6f 75 67 68 20 69 74 73 20 6f 70 65   through its ope
19ffe 72 61 74 69 6f 6e 20 6c 69 73 74 20 61 6e 64 20  ration list and 
19fff 63 68 61 6e 67 65 20 61 6c 6c 20 76 61 6c 75 65  change all value
1a000 73 20 6f 66 20 50 32 20 77 68 69 63 68 20 6d 61  s of P2 which ma
1a001 74 63 68 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c  tch.** the label
1a002 20 69 6e 74 6f 20 74 68 65 20 72 65 73 6f 6c 76   into the resolv
1a003 65 64 20 61 64 64 72 65 73 73 2e 0a 2a 2a 0a 2a  ed address..**.*
1a004 2a 20 54 68 65 20 56 44 42 45 20 6b 6e 6f 77 73  * The VDBE knows
1a005 20 74 68 61 74 20 61 20 50 32 20 76 61 6c 75 65   that a P2 value
1a006 20 69 73 20 61 20 6c 61 62 65 6c 20 62 65 63 61   is a label beca
1a007 75 73 65 20 6c 61 62 65 6c 73 20 61 72 65 0a 2a  use labels are.*
1a008 2a 20 61 6c 77 61 79 73 20 6e 65 67 61 74 69 76  * always negativ
1a009 65 20 61 6e 64 20 50 32 20 76 61 6c 75 65 73 20  e and P2 values 
1a00a 61 72 65 20 73 75 70 70 6f 73 65 20 74 6f 20 62  are suppose to b
1a00b 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a  e non-negative..
1a00c 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6e 65 67 61  ** Hence, a nega
1a00d 74 69 76 65 20 50 32 20 76 61 6c 75 65 20 69 73  tive P2 value is
1a00e 20 61 20 6c 61 62 65 6c 20 74 68 61 74 20 68 61   a label that ha
1a00f 73 20 79 65 74 20 74 6f 20 62 65 20 72 65 73 6f  s yet to be reso
1a010 6c 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 5a 65 72 6f  lved..**.** Zero
1a011 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
1a012 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73  a malloc() fails
1a013 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1a014 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
1a015 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64 62  dbeMakeLabel(Vdb
1a016 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  e *p){.  int i;.
1a017 20 20 69 20 3d 20 70 2d 3e 6e 4c 61 62 65 6c 2b    i = p->nLabel+
1a018 2b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  +;.  assert( p->
1a019 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49  magic==VDBE_MAGI
1a01a 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28 20  C_INIT );.  if( 
1a01b 69 3e 3d 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f  i>=p->nLabelAllo
1a01c 63 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 20 3d  c ){.    int n =
1a01d 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 2a   p->nLabelAlloc*
1a01e 32 20 2b 20 35 3b 0a 20 20 20 20 70 2d 3e 61 4c  2 + 5;.    p->aL
1a01f 61 62 65 6c 20 3d 20 73 71 6c 69 74 65 33 44 62  abel = sqlite3Db
1a020 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 2d  ReallocOrFree(p-
1a021 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c 2c 0a  >db, p->aLabel,.
1a022 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a023 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a024 20 20 20 20 20 20 20 6e 2a 73 69 7a 65 6f 66 28         n*sizeof(
1a025 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29 29 3b 0a  p->aLabel[0]));.
1a026 20 20 20 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c      p->nLabelAll
1a027 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  oc = sqlite3DbMa
1a028 6c 6c 6f 63 53 69 7a 65 28 70 2d 3e 64 62 2c 20  llocSize(p->db, 
1a029 70 2d 3e 61 4c 61 62 65 6c 29 2f 73 69 7a 65 6f  p->aLabel)/sizeo
1a02a 66 28 70 2d 3e 61 4c 61 62 65 6c 5b 30 5d 29 3b  f(p->aLabel[0]);
1a02b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 61 4c  .  }.  if( p->aL
1a02c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70 2d 3e 61  abel ){.    p->a
1a02d 4c 61 62 65 6c 5b 69 5d 20 3d 20 2d 31 3b 0a 20  Label[i] = -1;. 
1a02e 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31 2d 69   }.  return -1-i
1a02f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 6f 6c  ;.}../*.** Resol
1a030 76 65 20 6c 61 62 65 6c 20 22 78 22 20 74 6f 20  ve label "x" to 
1a031 62 65 20 74 68 65 20 61 64 64 72 65 73 73 20 6f  be the address o
1a032 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72  f the next instr
1a033 75 63 74 69 6f 6e 20 74 6f 0a 2a 2a 20 62 65 20  uction to.** be 
1a034 69 6e 73 65 72 74 65 64 2e 20 20 54 68 65 20 70  inserted.  The p
1a035 61 72 61 6d 65 74 65 72 20 22 78 22 20 6d 75 73  arameter "x" mus
1a036 74 20 68 61 76 65 20 62 65 65 6e 20 6f 62 74 61  t have been obta
1a037 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 61 20 70  ined from.** a p
1a038 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c  rior call to sql
1a039 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
1a03a 6c 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  l()..*/.SQLITE_P
1a03b 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1a03c 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
1a03d 62 65 6c 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  bel(Vdbe *p, int
1a03e 20 78 29 7b 0a 20 20 69 6e 74 20 6a 20 3d 20 2d   x){.  int j = -
1a03f 31 2d 78 3b 0a 20 20 61 73 73 65 72 74 28 20 70  1-x;.  assert( p
1a040 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
1a041 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 61 73  GIC_INIT );.  as
1a042 73 65 72 74 28 20 6a 3e 3d 30 20 26 26 20 6a 3c  sert( j>=0 && j<
1a043 70 2d 3e 6e 4c 61 62 65 6c 20 29 3b 0a 20 20 69  p->nLabel );.  i
1a044 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20 29 7b 0a  f( p->aLabel ){.
1a045 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c 5b 6a 5d      p->aLabel[j]
1a046 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 7d 0a 7d   = p->nOp;.  }.}
1a047 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
1a048 44 45 42 55 47 20 2f 2a 20 73 71 6c 69 74 65 33  DEBUG /* sqlite3
1a049 41 73 73 65 72 74 4d 61 79 41 62 6f 72 74 28 29  AssertMayAbort()
1a04a 20 6c 6f 67 69 63 20 2a 2f 0a 0a 2f 2a 0a 2a 2a   logic */../*.**
1a04b 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74   The following t
1a04c 79 70 65 20 61 6e 64 20 66 75 6e 63 74 69 6f 6e  ype and function
1a04d 20 61 72 65 20 75 73 65 64 20 74 6f 20 69 74 65   are used to ite
1a04e 72 61 74 65 20 74 68 72 6f 75 67 68 20 61 6c 6c  rate through all
1a04f 20 6f 70 63 6f 64 65 73 0a 2a 2a 20 69 6e 20 61   opcodes.** in a
1a050 20 56 64 62 65 20 6d 61 69 6e 20 70 72 6f 67 72   Vdbe main progr
1a051 61 6d 20 61 6e 64 20 65 61 63 68 20 6f 66 20 74  am and each of t
1a052 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 73 20  he sub-programs 
1a053 28 74 72 69 67 67 65 72 73 29 20 69 74 20 6d 61  (triggers) it ma
1a054 79 20 0a 2a 2a 20 69 6e 76 6f 6b 65 20 64 69 72  y .** invoke dir
1a055 65 63 74 6c 79 20 6f 72 20 69 6e 64 69 72 65 63  ectly or indirec
1a056 74 6c 79 2e 20 49 74 20 73 68 6f 75 6c 64 20 62  tly. It should b
1a057 65 20 75 73 65 64 20 61 73 20 66 6f 6c 6c 6f 77  e used as follow
1a058 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 4f 70 20 2a 70  s:.**.**   Op *p
1a059 4f 70 3b 0a 2a 2a 20 20 20 56 64 62 65 4f 70 49  Op;.**   VdbeOpI
1a05a 74 65 72 20 73 49 74 65 72 3b 0a 2a 2a 0a 2a 2a  ter sIter;.**.**
1a05b 20 20 20 6d 65 6d 73 65 74 28 26 73 49 74 65 72     memset(&sIter
1a05c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 49 74 65  , 0, sizeof(sIte
1a05d 72 29 29 3b 0a 2a 2a 20 20 20 73 49 74 65 72 2e  r));.**   sIter.
1a05e 76 20 3d 20 76 3b 20 20 20 20 20 20 20 20 20 20  v = v;          
1a05f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a060 20 20 2f 2f 20 76 20 69 73 20 6f 66 20 74 79 70    // v is of typ
1a061 65 20 56 64 62 65 2a 20 0a 2a 2a 20 20 20 77 68  e Vdbe* .**   wh
1a062 69 6c 65 28 20 28 70 4f 70 20 3d 20 6f 70 49 74  ile( (pOp = opIt
1a063 65 72 4e 65 78 74 28 26 73 49 74 65 72 29 29 20  erNext(&sIter)) 
1a064 29 7b 0a 2a 2a 20 20 20 20 20 2f 2f 20 44 6f 20  ){.**     // Do 
1a065 73 6f 6d 65 74 68 69 6e 67 20 77 69 74 68 20 70  something with p
1a066 4f 70 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 20 20 73  Op.**   }.**   s
1a067 71 6c 69 74 65 33 44 62 46 72 65 65 28 76 2d 3e  qlite3DbFree(v->
1a068 64 62 2c 20 73 49 74 65 72 2e 61 70 53 75 62 29  db, sIter.apSub)
1a069 3b 0a 2a 2a 20 0a 2a 2f 0a 74 79 70 65 64 65 66  ;.** .*/.typedef
1a06a 20 73 74 72 75 63 74 20 56 64 62 65 4f 70 49 74   struct VdbeOpIt
1a06b 65 72 20 56 64 62 65 4f 70 49 74 65 72 3b 0a 73  er VdbeOpIter;.s
1a06c 74 72 75 63 74 20 56 64 62 65 4f 70 49 74 65 72  truct VdbeOpIter
1a06d 20 7b 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20   {.  Vdbe *v;   
1a06e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a06f 2f 2a 20 56 64 62 65 20 74 6f 20 69 74 65 72 61  /* Vdbe to itera
1a070 74 65 20 74 68 72 6f 75 67 68 20 74 68 65 20 6f  te through the o
1a071 70 63 6f 64 65 73 20 6f 66 20 2a 2f 0a 20 20 53  pcodes of */.  S
1a072 75 62 50 72 6f 67 72 61 6d 20 2a 2a 61 70 53 75  ubProgram **apSu
1a073 62 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72  b;        /* Arr
1a074 61 79 20 6f 66 20 73 75 62 70 72 6f 67 72 61 6d  ay of subprogram
1a075 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 75 62 3b  s */.  int nSub;
1a076 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a077 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
1a078 6e 74 72 69 65 73 20 69 6e 20 61 70 53 75 62 20  ntries in apSub 
1a079 2a 2f 0a 20 20 69 6e 74 20 69 41 64 64 72 3b 20  */.  int iAddr; 
1a07a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a07b 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6e 65  /* Address of ne
1a07c 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74  xt instruction t
1a07d 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e  o return */.  in
1a07e 74 20 69 53 75 62 3b 20 20 20 20 20 20 20 20 20  t iSub;         
1a07f 20 20 20 20 20 20 20 20 20 2f 2a 20 30 20 3d 20           /* 0 = 
1a080 6d 61 69 6e 20 70 72 6f 67 72 61 6d 2c 20 31 20  main program, 1 
1a081 3d 20 66 69 72 73 74 20 73 75 62 2d 70 72 6f 67  = first sub-prog
1a082 72 61 6d 20 65 74 63 2e 20 2a 2f 0a 7d 3b 0a 73  ram etc. */.};.s
1a083 74 61 74 69 63 20 4f 70 20 2a 6f 70 49 74 65 72  tatic Op *opIter
1a084 4e 65 78 74 28 56 64 62 65 4f 70 49 74 65 72 20  Next(VdbeOpIter 
1a085 2a 70 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  *p){.  Vdbe *v =
1a086 20 70 2d 3e 76 3b 0a 20 20 4f 70 20 2a 70 52 65   p->v;.  Op *pRe
1a087 74 20 3d 20 30 3b 0a 20 20 4f 70 20 2a 61 4f 70  t = 0;.  Op *aOp
1a088 3b 0a 20 20 69 6e 74 20 6e 4f 70 3b 0a 0a 20 20  ;.  int nOp;..  
1a089 69 66 28 20 70 2d 3e 69 53 75 62 3c 3d 70 2d 3e  if( p->iSub<=p->
1a08a 6e 53 75 62 20 29 7b 0a 0a 20 20 20 20 69 66 28  nSub ){..    if(
1a08b 20 70 2d 3e 69 53 75 62 3d 3d 30 20 29 7b 0a 20   p->iSub==0 ){. 
1a08c 20 20 20 20 20 61 4f 70 20 3d 20 76 2d 3e 61 4f       aOp = v->aO
1a08d 70 3b 0a 20 20 20 20 20 20 6e 4f 70 20 3d 20 76  p;.      nOp = v
1a08e 2d 3e 6e 4f 70 3b 0a 20 20 20 20 7d 65 6c 73 65  ->nOp;.    }else
1a08f 7b 0a 20 20 20 20 20 20 61 4f 70 20 3d 20 70 2d  {.      aOp = p-
1a090 3e 61 70 53 75 62 5b 70 2d 3e 69 53 75 62 2d 31  >apSub[p->iSub-1
1a091 5d 2d 3e 61 4f 70 3b 0a 20 20 20 20 20 20 6e 4f  ]->aOp;.      nO
1a092 70 20 3d 20 70 2d 3e 61 70 53 75 62 5b 70 2d 3e  p = p->apSub[p->
1a093 69 53 75 62 2d 31 5d 2d 3e 6e 4f 70 3b 0a 20 20  iSub-1]->nOp;.  
1a094 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
1a095 70 2d 3e 69 41 64 64 72 3c 6e 4f 70 20 29 3b 0a  p->iAddr<nOp );.
1a096 0a 20 20 20 20 70 52 65 74 20 3d 20 26 61 4f 70  .    pRet = &aOp
1a097 5b 70 2d 3e 69 41 64 64 72 5d 3b 0a 20 20 20 20  [p->iAddr];.    
1a098 70 2d 3e 69 41 64 64 72 2b 2b 3b 0a 20 20 20 20  p->iAddr++;.    
1a099 69 66 28 20 70 2d 3e 69 41 64 64 72 3d 3d 6e 4f  if( p->iAddr==nO
1a09a 70 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 53  p ){.      p->iS
1a09b 75 62 2b 2b 3b 0a 20 20 20 20 20 20 70 2d 3e 69  ub++;.      p->i
1a09c 41 64 64 72 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  Addr = 0;.    }.
1a09d 20 20 0a 20 20 20 20 69 66 28 20 70 52 65 74 2d    .    if( pRet-
1a09e 3e 70 34 74 79 70 65 3d 3d 50 34 5f 53 55 42 50  >p4type==P4_SUBP
1a09f 52 4f 47 52 41 4d 20 29 7b 0a 20 20 20 20 20 20  ROGRAM ){.      
1a0a0 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 70 2d 3e  int nByte = (p->
1a0a1 6e 53 75 62 2b 31 29 2a 73 69 7a 65 6f 66 28 53  nSub+1)*sizeof(S
1a0a2 75 62 50 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20  ubProgram*);.   
1a0a3 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
1a0a4 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e 53  for(j=0; j<p->nS
1a0a5 75 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ub; j++){.      
1a0a6 20 20 69 66 28 20 70 2d 3e 61 70 53 75 62 5b 6a    if( p->apSub[j
1a0a7 5d 3d 3d 70 52 65 74 2d 3e 70 34 2e 70 50 72 6f  ]==pRet->p4.pPro
1a0a8 67 72 61 6d 20 29 20 62 72 65 61 6b 3b 0a 20 20  gram ) break;.  
1a0a9 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
1a0aa 6a 3d 3d 70 2d 3e 6e 53 75 62 20 29 7b 0a 20 20  j==p->nSub ){.  
1a0ab 20 20 20 20 20 20 70 2d 3e 61 70 53 75 62 20 3d        p->apSub =
1a0ac 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f   sqlite3DbReallo
1a0ad 63 4f 72 46 72 65 65 28 76 2d 3e 64 62 2c 20 70  cOrFree(v->db, p
1a0ae 2d 3e 61 70 53 75 62 2c 20 6e 42 79 74 65 29 3b  ->apSub, nByte);
1a0af 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 2d  .        if( !p-
1a0b0 3e 61 70 53 75 62 20 29 7b 0a 20 20 20 20 20 20  >apSub ){.      
1a0b1 20 20 20 20 70 52 65 74 20 3d 20 30 3b 0a 20 20      pRet = 0;.  
1a0b2 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1a0b3 20 20 20 20 20 20 20 70 2d 3e 61 70 53 75 62 5b         p->apSub[
1a0b4 70 2d 3e 6e 53 75 62 2b 2b 5d 20 3d 20 70 52 65  p->nSub++] = pRe
1a0b5 74 2d 3e 70 34 2e 70 50 72 6f 67 72 61 6d 3b 0a  t->p4.pProgram;.
1a0b6 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1a0b7 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  }.    }.  }..  r
1a0b8 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f  eturn pRet;.}../
1a0b9 2a 0a 2a 2a 20 43 68 65 63 6b 20 69 66 20 74 68  *.** Check if th
1a0ba 65 20 70 72 6f 67 72 61 6d 20 73 74 6f 72 65 64  e program stored
1a0bb 20 69 6e 20 74 68 65 20 56 4d 20 61 73 73 6f 63   in the VM assoc
1a0bc 69 61 74 65 64 20 77 69 74 68 20 70 50 61 72 73  iated with pPars
1a0bd 65 20 6d 61 79 0a 2a 2a 20 74 68 72 6f 77 20 61  e may.** throw a
1a0be 6e 20 41 42 4f 52 54 20 65 78 63 65 70 74 69 6f  n ABORT exceptio
1a0bf 6e 20 28 63 61 75 73 69 6e 67 20 74 68 65 20 73  n (causing the s
1a0c0 74 61 74 65 6d 65 6e 74 2c 20 62 75 74 20 6e 6f  tatement, but no
1a0c1 74 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63  t entire transac
1a0c2 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65 20 72 6f  tion.** to be ro
1a0c3 6c 6c 65 64 20 62 61 63 6b 29 2e 20 54 68 69 73  lled back). This
1a0c4 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 20 74 72   condition is tr
1a0c5 75 65 20 69 66 20 74 68 65 20 6d 61 69 6e 20 70  ue if the main p
1a0c6 72 6f 67 72 61 6d 20 6f 72 20 61 6e 79 0a 2a 2a  rogram or any.**
1a0c7 20 73 75 62 2d 70 72 6f 67 72 61 6d 73 20 63 6f   sub-programs co
1a0c8 6e 74 61 69 6e 73 20 61 6e 79 20 6f 66 20 74 68  ntains any of th
1a0c9 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a  e following:.**.
1a0ca 2a 2a 20 20 20 2a 20 20 4f 50 5f 48 61 6c 74 20  **   *  OP_Halt 
1a0cb 77 69 74 68 20 50 31 3d 53 51 4c 49 54 45 5f 43  with P1=SQLITE_C
1a0cc 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64 20 50 32  ONSTRAINT and P2
1a0cd 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20 20  =OE_Abort..**   
1a0ce 2a 20 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c  *  OP_HaltIfNull
1a0cf 20 77 69 74 68 20 50 31 3d 53 51 4c 49 54 45 5f   with P1=SQLITE_
1a0d0 43 4f 4e 53 54 52 41 49 4e 54 20 61 6e 64 20 50  CONSTRAINT and P
1a0d1 32 3d 4f 45 5f 41 62 6f 72 74 2e 0a 2a 2a 20 20  2=OE_Abort..**  
1a0d2 20 2a 20 20 4f 50 5f 44 65 73 74 72 6f 79 0a 2a   *  OP_Destroy.*
1a0d3 2a 20 20 20 2a 20 20 4f 50 5f 56 55 70 64 61 74  *   *  OP_VUpdat
1a0d4 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f 56 52 65  e.**   *  OP_VRe
1a0d5 6e 61 6d 65 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f  name.**   *  OP_
1a0d6 46 6b 43 6f 75 6e 74 65 72 20 77 69 74 68 20 50  FkCounter with P
1a0d7 32 3d 3d 30 20 28 69 6d 6d 65 64 69 61 74 65 20  2==0 (immediate 
1a0d8 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
1a0d9 74 72 61 69 6e 74 29 0a 2a 2a 0a 2a 2a 20 54 68  traint).**.** Th
1a0da 65 6e 20 63 68 65 63 6b 20 74 68 61 74 20 74 68  en check that th
1a0db 65 20 76 61 6c 75 65 20 6f 66 20 50 61 72 73 65  e value of Parse
1a0dc 2e 6d 61 79 41 62 6f 72 74 20 69 73 20 74 72 75  .mayAbort is tru
1a0dd 65 20 69 66 20 61 6e 0a 2a 2a 20 41 42 4f 52 54  e if an.** ABORT
1a0de 20 6d 61 79 20 62 65 20 74 68 72 6f 77 6e 2c 20   may be thrown, 
1a0df 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69  or false otherwi
1a0e0 73 65 2e 20 52 65 74 75 72 6e 20 74 72 75 65 20  se. Return true 
1a0e1 69 66 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6d 61  if it does.** ma
1a0e2 74 63 68 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74  tch, or false ot
1a0e3 68 65 72 77 69 73 65 2e 20 54 68 69 73 20 66 75  herwise. This fu
1a0e4 6e 63 74 69 6f 6e 20 69 73 20 69 6e 74 65 6e 64  nction is intend
1a0e5 65 64 20 74 6f 20 62 65 20 75 73 65 64 20 61 73  ed to be used as
1a0e6 0a 2a 2a 20 70 61 72 74 20 6f 66 20 61 6e 20 61  .** part of an a
1a0e7 73 73 65 72 74 20 73 74 61 74 65 6d 65 6e 74 20  ssert statement 
1a0e8 69 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 2e  in the compiler.
1a0e9 20 53 69 6d 69 6c 61 72 20 74 6f 3a 0a 2a 2a 0a   Similar to:.**.
1a0ea 2a 2a 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  **   assert( sql
1a0eb 69 74 65 33 56 64 62 65 41 73 73 65 72 74 4d 61  ite3VdbeAssertMa
1a0ec 79 41 62 6f 72 74 28 70 50 61 72 73 65 2d 3e 70  yAbort(pParse->p
1a0ed 56 64 62 65 2c 20 70 50 61 72 73 65 2d 3e 6d 61  Vdbe, pParse->ma
1a0ee 79 41 62 6f 72 74 29 20 29 3b 0a 2a 2f 0a 53 51  yAbort) );.*/.SQ
1a0ef 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1a0f0 20 73 71 6c 69 74 65 33 56 64 62 65 41 73 73 65   sqlite3VdbeAsse
1a0f1 72 74 4d 61 79 41 62 6f 72 74 28 56 64 62 65 20  rtMayAbort(Vdbe 
1a0f2 2a 76 2c 20 69 6e 74 20 6d 61 79 41 62 6f 72 74  *v, int mayAbort
1a0f3 29 7b 0a 20 20 69 6e 74 20 68 61 73 41 62 6f 72  ){.  int hasAbor
1a0f4 74 20 3d 20 30 3b 0a 20 20 4f 70 20 2a 70 4f 70  t = 0;.  Op *pOp
1a0f5 3b 0a 20 20 56 64 62 65 4f 70 49 74 65 72 20 73  ;.  VdbeOpIter s
1a0f6 49 74 65 72 3b 0a 20 20 6d 65 6d 73 65 74 28 26  Iter;.  memset(&
1a0f7 73 49 74 65 72 2c 20 30 2c 20 73 69 7a 65 6f 66  sIter, 0, sizeof
1a0f8 28 73 49 74 65 72 29 29 3b 0a 20 20 73 49 74 65  (sIter));.  sIte
1a0f9 72 2e 76 20 3d 20 76 3b 0a 0a 20 20 77 68 69 6c  r.v = v;..  whil
1a0fa 65 28 20 28 70 4f 70 20 3d 20 6f 70 49 74 65 72  e( (pOp = opIter
1a0fb 4e 65 78 74 28 26 73 49 74 65 72 29 29 21 3d 30  Next(&sIter))!=0
1a0fc 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70 63 6f   ){.    int opco
1a0fd 64 65 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  de = pOp->opcode
1a0fe 3b 0a 20 20 20 20 69 66 28 20 6f 70 63 6f 64 65  ;.    if( opcode
1a0ff 3d 3d 4f 50 5f 44 65 73 74 72 6f 79 20 7c 7c 20  ==OP_Destroy || 
1a100 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 55 70 64 61  opcode==OP_VUpda
1a101 74 65 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50  te || opcode==OP
1a102 5f 56 52 65 6e 61 6d 65 20 0a 23 69 66 6e 64 65  _VRename .#ifnde
1a103 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  f SQLITE_OMIT_FO
1a104 52 45 49 47 4e 5f 4b 45 59 0a 20 20 20 20 20 7c  REIGN_KEY.     |
1a105 7c 20 28 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6b  | (opcode==OP_Fk
1a106 43 6f 75 6e 74 65 72 20 26 26 20 70 4f 70 2d 3e  Counter && pOp->
1a107 70 31 3d 3d 30 20 26 26 20 70 4f 70 2d 3e 70 32  p1==0 && pOp->p2
1a108 3d 3d 31 29 20 0a 23 65 6e 64 69 66 0a 20 20 20  ==1) .#endif.   
1a109 20 20 7c 7c 20 28 28 6f 70 63 6f 64 65 3d 3d 4f    || ((opcode==O
1a10a 50 5f 48 61 6c 74 20 7c 7c 20 6f 70 63 6f 64 65  P_Halt || opcode
1a10b 3d 3d 4f 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 29  ==OP_HaltIfNull)
1a10c 20 0a 20 20 20 20 20 20 26 26 20 28 70 4f 70 2d   .      && (pOp-
1a10d 3e 70 31 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e 53  >p1==SQLITE_CONS
1a10e 54 52 41 49 4e 54 20 26 26 20 70 4f 70 2d 3e 70  TRAINT && pOp->p
1a10f 32 3d 3d 4f 45 5f 41 62 6f 72 74 29 29 0a 20 20  2==OE_Abort)).  
1a110 20 20 29 7b 0a 20 20 20 20 20 20 68 61 73 41 62    ){.      hasAb
1a111 6f 72 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 62  ort = 1;.      b
1a112 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
1a113 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1a114 76 2d 3e 64 62 2c 20 73 49 74 65 72 2e 61 70 53  v->db, sIter.apS
1a115 75 62 29 3b 0a 0a 20 20 2f 2a 20 52 65 74 75 72  ub);..  /* Retur
1a116 6e 20 74 72 75 65 20 69 66 20 68 61 73 41 62 6f  n true if hasAbo
1a117 72 74 3d 3d 6d 61 79 41 62 6f 72 74 2e 20 4f 72  rt==mayAbort. Or
1a118 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69   if a malloc fai
1a119 6c 75 72 65 20 6f 63 63 75 72 65 64 2e 0a 20 20  lure occured..  
1a11a 2a 2a 20 49 66 20 6d 61 6c 6c 6f 63 20 66 61 69  ** If malloc fai
1a11b 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 20 77 68  led, then the wh
1a11c 69 6c 65 28 29 20 6c 6f 6f 70 20 61 62 6f 76 65  ile() loop above
1a11d 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 69 74   may not have it
1a11e 65 72 61 74 65 64 0a 20 20 2a 2a 20 74 68 72 6f  erated.  ** thro
1a11f 75 67 68 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20  ugh all opcodes 
1a120 61 6e 64 20 68 61 73 41 62 6f 72 74 20 6d 61 79  and hasAbort may
1a121 20 62 65 20 73 65 74 20 69 6e 63 6f 72 72 65 63   be set incorrec
1a122 74 6c 79 2e 20 52 65 74 75 72 6e 0a 20 20 2a 2a  tly. Return.  **
1a123 20 74 72 75 65 20 66 6f 72 20 74 68 69 73 20 63   true for this c
1a124 61 73 65 20 74 6f 20 70 72 65 76 65 6e 74 20 74  ase to prevent t
1a125 68 65 20 61 73 73 65 72 74 28 29 20 69 6e 20 74  he assert() in t
1a126 68 65 20 63 61 6c 6c 65 72 73 20 66 72 61 6d 65  he callers frame
1a127 0a 20 20 2a 2a 20 66 72 6f 6d 20 66 61 69 6c 69  .  ** from faili
1a128 6e 67 2e 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e  ng.  */.  return
1a129 20 28 20 76 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63   ( v->db->malloc
1a12a 46 61 69 6c 65 64 20 7c 7c 20 68 61 73 41 62 6f  Failed || hasAbo
1a12b 72 74 3d 3d 6d 61 79 41 62 6f 72 74 20 29 3b 0a  rt==mayAbort );.
1a12c 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1a12d 54 45 5f 44 45 42 55 47 20 2d 20 74 68 65 20 73  TE_DEBUG - the s
1a12e 71 6c 69 74 65 33 41 73 73 65 72 74 4d 61 79 41  qlite3AssertMayA
1a12f 62 6f 72 74 28 29 20 66 75 6e 63 74 69 6f 6e 20  bort() function 
1a130 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 70 20 74  */../*.** Loop t
1a131 68 72 6f 75 67 68 20 74 68 65 20 70 72 6f 67 72  hrough the progr
1a132 61 6d 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 50  am looking for P
1a133 32 20 76 61 6c 75 65 73 20 74 68 61 74 20 61 72  2 values that ar
1a134 65 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 6f 6e  e negative.** on
1a135 20 6a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f   jump instructio
1a136 6e 73 2e 20 20 45 61 63 68 20 73 75 63 68 20 76  ns.  Each such v
1a137 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c 2e  alue is a label.
1a138 20 20 52 65 73 6f 6c 76 65 20 74 68 65 0a 2a 2a    Resolve the.**
1a139 20 6c 61 62 65 6c 20 62 79 20 73 65 74 74 69 6e   label by settin
1a13a 67 20 74 68 65 20 50 32 20 76 61 6c 75 65 20 74  g the P2 value t
1a13b 6f 20 69 74 73 20 63 6f 72 72 65 63 74 20 6e 6f  o its correct no
1a13c 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 2e 0a 2a 2a  n-zero value..**
1a13d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1a13e 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 63 65 20   is called once 
1a13f 61 66 74 65 72 20 61 6c 6c 20 6f 70 63 6f 64 65  after all opcode
1a140 73 20 68 61 76 65 20 62 65 65 6e 20 69 6e 73 65  s have been inse
1a141 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 69  rted..**.** Vari
1a142 61 62 6c 65 20 2a 70 4d 61 78 46 75 6e 63 41 72  able *pMaxFuncAr
1a143 67 73 20 69 73 20 73 65 74 20 74 6f 20 74 68 65  gs is set to the
1a144 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65 20 6f   maximum value o
1a145 66 20 61 6e 79 20 50 32 20 61 72 67 75 6d 65 6e  f any P2 argumen
1a146 74 20 0a 2a 2a 20 74 6f 20 61 6e 20 4f 50 5f 46  t .** to an OP_F
1a147 75 6e 63 74 69 6f 6e 2c 20 4f 50 5f 41 67 67 53  unction, OP_AggS
1a148 74 65 70 20 6f 72 20 4f 50 5f 56 46 69 6c 74 65  tep or OP_VFilte
1a149 72 20 6f 70 63 6f 64 65 2e 20 54 68 69 73 20 69  r opcode. This i
1a14a 73 20 75 73 65 64 20 62 79 20 0a 2a 2a 20 73 71  s used by .** sq
1a14b 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61  lite3VdbeMakeRea
1a14c 64 79 28 29 20 74 6f 20 73 69 7a 65 20 74 68 65  dy() to size the
1a14d 20 56 64 62 65 2e 61 70 41 72 67 5b 5d 20 61 72   Vdbe.apArg[] ar
1a14e 72 61 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ray..*/.static v
1a14f 6f 69 64 20 72 65 73 6f 6c 76 65 50 32 56 61 6c  oid resolveP2Val
1a150 75 65 73 28 56 64 62 65 20 2a 70 2c 20 69 6e 74  ues(Vdbe *p, int
1a151 20 2a 70 4d 61 78 46 75 6e 63 41 72 67 73 29 7b   *pMaxFuncArgs){
1a152 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
1a153 6e 4d 61 78 41 72 67 73 20 3d 20 2a 70 4d 61 78  nMaxArgs = *pMax
1a154 46 75 6e 63 41 72 67 73 3b 0a 20 20 4f 70 20 2a  FuncArgs;.  Op *
1a155 70 4f 70 3b 0a 20 20 69 6e 74 20 2a 61 4c 61 62  pOp;.  int *aLab
1a156 65 6c 20 3d 20 70 2d 3e 61 4c 61 62 65 6c 3b 0a  el = p->aLabel;.
1a157 20 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20    p->readOnly = 
1a158 31 3b 0a 20 20 66 6f 72 28 70 4f 70 3d 70 2d 3e  1;.  for(pOp=p->
1a159 61 4f 70 2c 20 69 3d 70 2d 3e 6e 4f 70 2d 31 3b  aOp, i=p->nOp-1;
1a15a 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 70 2b   i>=0; i--, pOp+
1a15b 2b 29 7b 0a 20 20 20 20 75 38 20 6f 70 63 6f 64  +){.    u8 opcod
1a15c 65 20 3d 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3b  e = pOp->opcode;
1a15d 0a 0a 20 20 20 20 69 66 28 20 6f 70 63 6f 64 65  ..    if( opcode
1a15e 3d 3d 4f 50 5f 46 75 6e 63 74 69 6f 6e 20 7c 7c  ==OP_Function ||
1a15f 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 67 67 53   opcode==OP_AggS
1a160 74 65 70 20 29 7b 0a 20 20 20 20 20 20 69 66 28  tep ){.      if(
1a161 20 70 4f 70 2d 3e 70 35 3e 6e 4d 61 78 41 72 67   pOp->p5>nMaxArg
1a162 73 20 29 20 6e 4d 61 78 41 72 67 73 20 3d 20 70  s ) nMaxArgs = p
1a163 4f 70 2d 3e 70 35 3b 0a 23 69 66 6e 64 65 66 20  Op->p5;.#ifndef 
1a164 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1a165 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c  UALTABLE.    }el
1a166 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f  se if( opcode==O
1a167 50 5f 56 55 70 64 61 74 65 20 29 7b 0a 20 20 20  P_VUpdate ){.   
1a168 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3e 6e     if( pOp->p2>n
1a169 4d 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41 72  MaxArgs ) nMaxAr
1a16a 67 73 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 23 65  gs = pOp->p2;.#e
1a16b 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 20 69  ndif.    }else i
1a16c 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54 72  f( opcode==OP_Tr
1a16d 61 6e 73 61 63 74 69 6f 6e 20 26 26 20 70 4f 70  ansaction && pOp
1a16e 2d 3e 70 32 21 3d 30 20 29 7b 0a 20 20 20 20 20  ->p2!=0 ){.     
1a16f 20 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 30   p->readOnly = 0
1a170 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
1a171 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1a172 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  LE.    }else if(
1a173 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 46 69 6c   opcode==OP_VFil
1a174 74 65 72 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  ter ){.      int
1a175 20 6e 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   n;.      assert
1a176 28 20 70 2d 3e 6e 4f 70 20 2d 20 69 20 3e 3d 20  ( p->nOp - i >= 
1a177 33 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  3 );.      asser
1a178 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64  t( pOp[-1].opcod
1a179 65 3d 3d 4f 50 5f 49 6e 74 65 67 65 72 20 29 3b  e==OP_Integer );
1a17a 0a 20 20 20 20 20 20 6e 20 3d 20 70 4f 70 5b 2d  .      n = pOp[-
1a17b 31 5d 2e 70 31 3b 0a 20 20 20 20 20 20 69 66 28  1].p1;.      if(
1a17c 20 6e 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d   n>nMaxArgs ) nM
1a17d 61 78 41 72 67 73 20 3d 20 6e 3b 0a 23 65 6e 64  axArgs = n;.#end
1a17e 69 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  if.    }..    if
1a17f 28 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63  ( sqlite3VdbeOpc
1a180 6f 64 65 48 61 73 50 72 6f 70 65 72 74 79 28 6f  odeHasProperty(o
1a181 70 63 6f 64 65 2c 20 4f 50 46 4c 47 5f 4a 55 4d  pcode, OPFLG_JUM
1a182 50 29 20 26 26 20 70 4f 70 2d 3e 70 32 3c 30 20  P) && pOp->p2<0 
1a183 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
1a184 20 2d 31 2d 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e   -1-pOp->p2<p->n
1a185 4c 61 62 65 6c 20 29 3b 0a 20 20 20 20 20 20 70  Label );.      p
1a186 4f 70 2d 3e 70 32 20 3d 20 61 4c 61 62 65 6c 5b  Op->p2 = aLabel[
1a187 2d 31 2d 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20  -1-pOp->p2];.   
1a188 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
1a189 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d  DbFree(p->db, p-
1a18a 3e 61 4c 61 62 65 6c 29 3b 0a 20 20 70 2d 3e 61  >aLabel);.  p->a
1a18b 4c 61 62 65 6c 20 3d 20 30 3b 0a 0a 20 20 2a 70  Label = 0;..  *p
1a18c 4d 61 78 46 75 6e 63 41 72 67 73 20 3d 20 6e 4d  MaxFuncArgs = nM
1a18d 61 78 41 72 67 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  axArgs;.}../*.**
1a18e 20 52 65 74 75 72 6e 20 74 68 65 20 61 64 64 72   Return the addr
1a18f 65 73 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20  ess of the next 
1a190 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 62  instruction to b
1a191 65 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 53  e inserted..*/.S
1a192 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1a193 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  t sqlite3VdbeCur
1a194 72 65 6e 74 41 64 64 72 28 56 64 62 65 20 2a 70  rentAddr(Vdbe *p
1a195 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  ){.  assert( p->
1a196 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49  magic==VDBE_MAGI
1a197 43 5f 49 4e 49 54 20 29 3b 0a 20 20 72 65 74 75  C_INIT );.  retu
1a198 72 6e 20 70 2d 3e 6e 4f 70 3b 0a 7d 0a 0a 2f 2a  rn p->nOp;.}../*
1a199 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
1a19a 6e 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  n returns a poin
1a19b 74 65 72 20 74 6f 20 74 68 65 20 61 72 72 61 79  ter to the array
1a19c 20 6f 66 20 6f 70 63 6f 64 65 73 20 61 73 73 6f   of opcodes asso
1a19d 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a 20 74  ciated with.** t
1a19e 68 65 20 56 64 62 65 20 70 61 73 73 65 64 20 61  he Vdbe passed a
1a19f 73 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  s the first argu
1a1a0 6d 65 6e 74 2e 20 49 74 20 69 73 20 74 68 65 20  ment. It is the 
1a1a1 63 61 6c 6c 65 72 73 20 72 65 73 70 6f 6e 73 69  callers responsi
1a1a2 62 69 6c 69 74 79 0a 2a 2a 20 74 6f 20 61 72 72  bility.** to arr
1a1a3 61 6e 67 65 20 66 6f 72 20 74 68 65 20 72 65 74  ange for the ret
1a1a4 75 72 6e 65 64 20 61 72 72 61 79 20 74 6f 20 62  urned array to b
1a1a5 65 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65  e eventually fre
1a1a6 65 64 20 75 73 69 6e 67 20 74 68 65 20 0a 2a 2a  ed using the .**
1a1a7 20 76 64 62 65 46 72 65 65 4f 70 41 72 72 61 79   vdbeFreeOpArray
1a1a8 28 29 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a  () function..**.
1a1a9 2a 2a 20 42 65 66 6f 72 65 20 72 65 74 75 72 6e  ** Before return
1a1aa 69 6e 67 2c 20 2a 70 6e 4f 70 20 69 73 20 73 65  ing, *pnOp is se
1a1ab 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20  t to the number 
1a1ac 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68  of entries in th
1a1ad 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 61 72  e returned.** ar
1a1ae 72 61 79 2e 20 41 6c 73 6f 2c 20 2a 70 6e 4d 61  ray. Also, *pnMa
1a1af 78 41 72 67 20 69 73 20 73 65 74 20 74 6f 20 74  xArg is set to t
1a1b0 68 65 20 6c 61 72 67 65 72 20 6f 66 20 69 74 73  he larger of its
1a1b1 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 61   current value a
1a1b2 6e 64 20 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65  nd .** the numbe
1a1b3 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
1a1b4 74 68 65 20 56 64 62 65 2e 61 70 41 72 67 5b 5d  the Vdbe.apArg[]
1a1b5 20 61 72 72 61 79 20 72 65 71 75 69 72 65 64 20   array required 
1a1b6 74 6f 20 65 78 65 63 75 74 65 20 74 68 65 20 0a  to execute the .
1a1b7 2a 2a 20 72 65 74 75 72 6e 65 64 20 70 72 6f 67  ** returned prog
1a1b8 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ram..*/.SQLITE_P
1a1b9 52 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a 73  RIVATE VdbeOp *s
1a1ba 71 6c 69 74 65 33 56 64 62 65 54 61 6b 65 4f 70  qlite3VdbeTakeOp
1a1bb 41 72 72 61 79 28 56 64 62 65 20 2a 70 2c 20 69  Array(Vdbe *p, i
1a1bc 6e 74 20 2a 70 6e 4f 70 2c 20 69 6e 74 20 2a 70  nt *pnOp, int *p
1a1bd 6e 4d 61 78 41 72 67 29 7b 0a 20 20 56 64 62 65  nMaxArg){.  Vdbe
1a1be 4f 70 20 2a 61 4f 70 20 3d 20 70 2d 3e 61 4f 70  Op *aOp = p->aOp
1a1bf 3b 0a 20 20 61 73 73 65 72 74 28 20 61 4f 70 20  ;.  assert( aOp 
1a1c0 26 26 20 21 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  && !p->db->mallo
1a1c1 63 46 61 69 6c 65 64 20 29 3b 0a 0a 20 20 2f 2a  cFailed );..  /*
1a1c2 20 43 68 65 63 6b 20 74 68 61 74 20 73 71 6c 69   Check that sqli
1a1c3 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65  te3VdbeUsesBtree
1a1c4 28 29 20 77 61 73 20 6e 6f 74 20 63 61 6c 6c 65  () was not calle
1a1c5 64 20 6f 6e 20 74 68 69 73 20 56 4d 20 2a 2f 0a  d on this VM */.
1a1c6 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4d 75    assert( p->aMu
1a1c7 74 65 78 2e 6e 4d 75 74 65 78 3d 3d 30 20 29 3b  tex.nMutex==0 );
1a1c8 0a 0a 20 20 72 65 73 6f 6c 76 65 50 32 56 61 6c  ..  resolveP2Val
1a1c9 75 65 73 28 70 2c 20 70 6e 4d 61 78 41 72 67 29  ues(p, pnMaxArg)
1a1ca 3b 0a 20 20 2a 70 6e 4f 70 20 3d 20 70 2d 3e 6e  ;.  *pnOp = p->n
1a1cb 4f 70 3b 0a 20 20 70 2d 3e 61 4f 70 20 3d 20 30  Op;.  p->aOp = 0
1a1cc 3b 0a 20 20 72 65 74 75 72 6e 20 61 4f 70 3b 0a  ;.  return aOp;.
1a1cd 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 77  }../*.** Add a w
1a1ce 68 6f 6c 65 20 6c 69 73 74 20 6f 66 20 6f 70 65  hole list of ope
1a1cf 72 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 6f  rations to the o
1a1d0 70 65 72 61 74 69 6f 6e 20 73 74 61 63 6b 2e 20  peration stack. 
1a1d1 20 52 65 74 75 72 6e 20 74 68 65 0a 2a 2a 20 61   Return the.** a
1a1d2 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 66 69  ddress of the fi
1a1d3 72 73 74 20 6f 70 65 72 61 74 69 6f 6e 20 61 64  rst operation ad
1a1d4 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ded..*/.SQLITE_P
1a1d5 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1a1d6 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28  e3VdbeAddOpList(
1a1d7 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 6e 4f 70  Vdbe *p, int nOp
1a1d8 2c 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6f 6e  , VdbeOpList con
1a1d9 73 74 20 2a 61 4f 70 29 7b 0a 20 20 69 6e 74 20  st *aOp){.  int 
1a1da 61 64 64 72 3b 0a 20 20 61 73 73 65 72 74 28 20  addr;.  assert( 
1a1db 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  p->magic==VDBE_M
1a1dc 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69  AGIC_INIT );.  i
1a1dd 66 28 20 70 2d 3e 6e 4f 70 20 2b 20 6e 4f 70 20  f( p->nOp + nOp 
1a1de 3e 20 70 2d 3e 6e 4f 70 41 6c 6c 6f 63 20 26 26  > p->nOpAlloc &&
1a1df 20 67 72 6f 77 4f 70 41 72 72 61 79 28 70 29 20   growOpArray(p) 
1a1e0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
1a1e1 0a 20 20 7d 0a 20 20 61 64 64 72 20 3d 20 70 2d  .  }.  addr = p-
1a1e2 3e 6e 4f 70 3b 0a 20 20 69 66 28 20 41 4c 57 41  >nOp;.  if( ALWA
1a1e3 59 53 28 6e 4f 70 3e 30 29 20 29 7b 0a 20 20 20  YS(nOp>0) ){.   
1a1e4 20 69 6e 74 20 69 3b 0a 20 20 20 20 56 64 62 65   int i;.    Vdbe
1a1e5 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 70 49  OpList const *pI
1a1e6 6e 20 3d 20 61 4f 70 3b 0a 20 20 20 20 66 6f 72  n = aOp;.    for
1a1e7 28 69 3d 30 3b 20 69 3c 6e 4f 70 3b 20 69 2b 2b  (i=0; i<nOp; i++
1a1e8 2c 20 70 49 6e 2b 2b 29 7b 0a 20 20 20 20 20 20  , pIn++){.      
1a1e9 69 6e 74 20 70 32 20 3d 20 70 49 6e 2d 3e 70 32  int p2 = pIn->p2
1a1ea 3b 0a 20 20 20 20 20 20 56 64 62 65 4f 70 20 2a  ;.      VdbeOp *
1a1eb 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4f 70 5b 69  pOut = &p->aOp[i
1a1ec 2b 61 64 64 72 5d 3b 0a 20 20 20 20 20 20 70 4f  +addr];.      pO
1a1ed 75 74 2d 3e 6f 70 63 6f 64 65 20 3d 20 70 49 6e  ut->opcode = pIn
1a1ee 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 20 20  ->opcode;.      
1a1ef 70 4f 75 74 2d 3e 70 31 20 3d 20 70 49 6e 2d 3e  pOut->p1 = pIn->
1a1f0 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 70 32  p1;.      if( p2
1a1f1 3c 30 20 26 26 20 73 71 6c 69 74 65 33 56 64 62  <0 && sqlite3Vdb
1a1f2 65 4f 70 63 6f 64 65 48 61 73 50 72 6f 70 65 72  eOpcodeHasProper
1a1f3 74 79 28 70 4f 75 74 2d 3e 6f 70 63 6f 64 65 2c  ty(pOut->opcode,
1a1f4 20 4f 50 46 4c 47 5f 4a 55 4d 50 29 20 29 7b 0a   OPFLG_JUMP) ){.
1a1f5 20 20 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 32          pOut->p2
1a1f6 20 3d 20 61 64 64 72 20 2b 20 41 44 44 52 28 70   = addr + ADDR(p
1a1f7 32 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  2);.      }else{
1a1f8 0a 20 20 20 20 20 20 20 20 70 4f 75 74 2d 3e 70  .        pOut->p
1a1f9 32 20 3d 20 70 32 3b 0a 20 20 20 20 20 20 7d 0a  2 = p2;.      }.
1a1fa 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 33 20 3d        pOut->p3 =
1a1fb 20 70 49 6e 2d 3e 70 33 3b 0a 20 20 20 20 20 20   pIn->p3;.      
1a1fc 70 4f 75 74 2d 3e 70 34 74 79 70 65 20 3d 20 50  pOut->p4type = P
1a1fd 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 20 20 20  4_NOTUSED;.     
1a1fe 20 70 4f 75 74 2d 3e 70 34 2e 70 20 3d 20 30 3b   pOut->p4.p = 0;
1a1ff 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 35 20  .      pOut->p5 
1a200 3d 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  = 0;.#ifdef SQLI
1a201 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 70  TE_DEBUG.      p
1a202 4f 75 74 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d 20  Out->zComment = 
1a203 30 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  0;.      if( sql
1a204 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 72 61  ite3VdbeAddopTra
1a205 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ce ){.        sq
1a206 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70  lite3VdbePrintOp
1a207 28 30 2c 20 69 2b 61 64 64 72 2c 20 26 70 2d 3e  (0, i+addr, &p->
1a208 61 4f 70 5b 69 2b 61 64 64 72 5d 29 3b 0a 20 20  aOp[i+addr]);.  
1a209 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
1a20a 20 7d 0a 20 20 20 20 70 2d 3e 6e 4f 70 20 2b 3d   }.    p->nOp +=
1a20b 20 6e 4f 70 3b 0a 20 20 7d 0a 20 20 72 65 74 75   nOp;.  }.  retu
1a20c 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn addr;.}../*.*
1a20d 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c  * Change the val
1a20e 75 65 20 6f 66 20 74 68 65 20 50 31 20 6f 70 65  ue of the P1 ope
1a20f 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69  rand for a speci
1a210 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  fic instruction.
1a211 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1a212 20 69 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20   is useful when 
1a213 61 20 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20  a large program 
1a214 69 73 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61  is loaded from a
1a215 0a 2a 2a 20 73 74 61 74 69 63 20 61 72 72 61 79  .** static array
1a216 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64   using sqlite3Vd
1a217 62 65 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20  beAddOpList but 
1a218 77 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20  we want to make 
1a219 61 0a 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63  a.** few minor c
1a21a 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72  hanges to the pr
1a21b 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ogram..*/.SQLITE
1a21c 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1a21d 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1a21e 31 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61  1(Vdbe *p, int a
1a21f 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20  ddr, int val){. 
1a220 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b   assert( p!=0 );
1a221 0a 20 20 61 73 73 65 72 74 28 20 61 64 64 72 3e  .  assert( addr>
1a222 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e  =0 );.  if( p->n
1a223 4f 70 3e 61 64 64 72 20 29 7b 0a 20 20 20 20 70  Op>addr ){.    p
1a224 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 31 20 3d  ->aOp[addr].p1 =
1a225 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   val;.  }.}../*.
1a226 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61  ** Change the va
1a227 6c 75 65 20 6f 66 20 74 68 65 20 50 32 20 6f 70  lue of the P2 op
1a228 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63  erand for a spec
1a229 69 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e  ific instruction
1a22a 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
1a22b 65 20 69 73 20 75 73 65 66 75 6c 20 66 6f 72 20  e is useful for 
1a22c 73 65 74 74 69 6e 67 20 61 20 6a 75 6d 70 20 64  setting a jump d
1a22d 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 53  estination..*/.S
1a22e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1a22f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  id sqlite3VdbeCh
1a230 61 6e 67 65 50 32 28 56 64 62 65 20 2a 70 2c 20  angeP2(Vdbe *p, 
1a231 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 76 61  int addr, int va
1a232 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 21  l){.  assert( p!
1a233 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1a234 61 64 64 72 3e 3d 30 20 29 3b 0a 20 20 69 66 28  addr>=0 );.  if(
1a235 20 70 2d 3e 6e 4f 70 3e 61 64 64 72 20 29 7b 0a   p->nOp>addr ){.
1a236 20 20 20 20 70 2d 3e 61 4f 70 5b 61 64 64 72 5d      p->aOp[addr]
1a237 2e 70 32 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d  .p2 = val;.  }.}
1a238 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
1a239 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  he value of the 
1a23a 50 33 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 61  P3 operand for a
1a23b 20 73 70 65 63 69 66 69 63 20 69 6e 73 74 72 75   specific instru
1a23c 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
1a23d 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1a23e 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1a23f 33 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61  3(Vdbe *p, int a
1a240 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a 20  ddr, int val){. 
1a241 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b   assert( p!=0 );
1a242 0a 20 20 61 73 73 65 72 74 28 20 61 64 64 72 3e  .  assert( addr>
1a243 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e  =0 );.  if( p->n
1a244 4f 70 3e 61 64 64 72 20 29 7b 0a 20 20 20 20 70  Op>addr ){.    p
1a245 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70 33 20 3d  ->aOp[addr].p3 =
1a246 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   val;.  }.}../*.
1a247 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61  ** Change the va
1a248 6c 75 65 20 6f 66 20 74 68 65 20 50 35 20 6f 70  lue of the P5 op
1a249 65 72 61 6e 64 20 66 6f 72 20 74 68 65 20 6d 6f  erand for the mo
1a24a 73 74 20 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 61  st recently.** a
1a24b 64 64 65 64 20 6f 70 65 72 61 74 69 6f 6e 2e 0a  dded operation..
1a24c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1a24d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1a24e 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 20  beChangeP5(Vdbe 
1a24f 2a 70 2c 20 75 38 20 76 61 6c 29 7b 0a 20 20 61  *p, u8 val){.  a
1a250 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
1a251 20 69 66 28 20 70 2d 3e 61 4f 70 20 29 7b 0a 20   if( p->aOp ){. 
1a252 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f     assert( p->nO
1a253 70 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e 61 4f  p>0 );.    p->aO
1a254 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 70 35 20 3d  p[p->nOp-1].p5 =
1a255 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   val;.  }.}../*.
1a256 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 50 32  ** Change the P2
1a257 20 6f 70 65 72 61 6e 64 20 6f 66 20 69 6e 73 74   operand of inst
1a258 72 75 63 74 69 6f 6e 20 61 64 64 72 20 73 6f 20  ruction addr so 
1a259 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74  that it points t
1a25a 6f 0a 2a 2a 20 74 68 65 20 61 64 64 72 65 73 73  o.** the address
1a25b 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e 73   of the next ins
1a25c 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20 63  truction to be c
1a25d 6f 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  oded..*/.SQLITE_
1a25e 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1a25f 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
1a260 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64  (Vdbe *p, int ad
1a261 64 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64  dr){.  sqlite3Vd
1a262 62 65 43 68 61 6e 67 65 50 32 28 70 2c 20 61 64  beChangeP2(p, ad
1a263 64 72 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 7d 0a 0a  dr, p->nOp);.}..
1a264 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e  ./*.** If the in
1a265 70 75 74 20 46 75 6e 63 44 65 66 20 73 74 72 75  put FuncDef stru
1a266 63 74 75 72 65 20 69 73 20 65 70 68 65 6d 65 72  cture is ephemer
1a267 61 6c 2c 20 74 68 65 6e 20 66 72 65 65 20 69 74  al, then free it
1a268 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 46 75 6e  .  If.** the Fun
1a269 63 44 65 66 20 69 73 20 6e 6f 74 20 65 70 68 65  cDef is not ephe
1a26a 72 6d 61 6c 2c 20 74 68 65 6e 20 64 6f 20 6e 6f  rmal, then do no
1a26b 74 68 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  thing..*/.static
1a26c 20 76 6f 69 64 20 66 72 65 65 45 70 68 65 6d 65   void freeEpheme
1a26d 72 61 6c 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69  ralFunction(sqli
1a26e 74 65 33 20 2a 64 62 2c 20 46 75 6e 63 44 65 66  te3 *db, FuncDef
1a26f 20 2a 70 44 65 66 29 7b 0a 20 20 69 66 28 20 41   *pDef){.  if( A
1a270 4c 57 41 59 53 28 70 44 65 66 29 20 26 26 20 28  LWAYS(pDef) && (
1a271 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51  pDef->flags & SQ
1a272 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d 29  LITE_FUNC_EPHEM)
1a273 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  !=0 ){.    sqlit
1a274 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 44 65  e3DbFree(db, pDe
1a275 66 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  f);.  }.}../*.**
1a276 20 44 65 6c 65 74 65 20 61 20 50 34 20 76 61 6c   Delete a P4 val
1a277 75 65 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e  ue if necessary.
1a278 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1a279 66 72 65 65 50 34 28 73 71 6c 69 74 65 33 20 2a  freeP4(sqlite3 *
1a27a 64 62 2c 20 69 6e 74 20 70 34 74 79 70 65 2c 20  db, int p4type, 
1a27b 76 6f 69 64 20 2a 70 34 29 7b 0a 20 20 69 66 28  void *p4){.  if(
1a27c 20 70 34 20 29 7b 0a 20 20 20 20 73 77 69 74 63   p4 ){.    switc
1a27d 68 28 20 70 34 74 79 70 65 20 29 7b 0a 20 20 20  h( p4type ){.   
1a27e 20 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c 3a     case P4_REAL:
1a27f 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f 49  .      case P4_I
1a280 4e 54 36 34 3a 0a 20 20 20 20 20 20 63 61 73 65  NT64:.      case
1a281 20 50 34 5f 4d 50 52 49 4e 54 46 3a 0a 20 20 20   P4_MPRINTF:.   
1a282 20 20 20 63 61 73 65 20 50 34 5f 44 59 4e 41 4d     case P4_DYNAM
1a283 49 43 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50  IC:.      case P
1a284 34 5f 4b 45 59 49 4e 46 4f 3a 0a 20 20 20 20 20  4_KEYINFO:.     
1a285 20 63 61 73 65 20 50 34 5f 49 4e 54 41 52 52 41   case P4_INTARRA
1a286 59 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 34  Y:.      case P4
1a287 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
1a288 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  : {.        sqli
1a289 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 34  te3DbFree(db, p4
1a28a 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
1a28b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1a28c 63 61 73 65 20 50 34 5f 56 44 42 45 46 55 4e 43  case P4_VDBEFUNC
1a28d 3a 20 7b 0a 20 20 20 20 20 20 20 20 56 64 62 65  : {.        Vdbe
1a28e 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 20  Func *pVdbeFunc 
1a28f 3d 20 28 56 64 62 65 46 75 6e 63 20 2a 29 70 34  = (VdbeFunc *)p4
1a290 3b 0a 20 20 20 20 20 20 20 20 66 72 65 65 45 70  ;.        freeEp
1a291 68 65 6d 65 72 61 6c 46 75 6e 63 74 69 6f 6e 28  hemeralFunction(
1a292 64 62 2c 20 70 56 64 62 65 46 75 6e 63 2d 3e 70  db, pVdbeFunc->p
1a293 46 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 73  Func);.        s
1a294 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65  qlite3VdbeDelete
1a295 41 75 78 44 61 74 61 28 70 56 64 62 65 46 75 6e  AuxData(pVdbeFun
1a296 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  c, 0);.        s
1a297 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1a298 20 70 56 64 62 65 46 75 6e 63 29 3b 0a 20 20 20   pVdbeFunc);.   
1a299 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1a29a 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 50    }.      case P
1a29b 34 5f 46 55 4e 43 44 45 46 3a 20 7b 0a 20 20 20  4_FUNCDEF: {.   
1a29c 20 20 20 20 20 66 72 65 65 45 70 68 65 6d 65 72       freeEphemer
1a29d 61 6c 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 28  alFunction(db, (
1a29e 46 75 6e 63 44 65 66 2a 29 70 34 29 3b 0a 20 20  FuncDef*)p4);.  
1a29f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1a2a0 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20     }.      case 
1a2a1 50 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 20 20  P4_MEM: {.      
1a2a2 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72    sqlite3ValueFr
1a2a3 65 65 28 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  ee((sqlite3_valu
1a2a4 65 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20 20  e*)p4);.        
1a2a5 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
1a2a6 20 20 20 20 20 63 61 73 65 20 50 34 5f 56 54 41       case P4_VTA
1a2a7 42 20 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71  B : {.        sq
1a2a8 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28  lite3VtabUnlock(
1a2a9 28 56 54 61 62 6c 65 20 2a 29 70 34 29 3b 0a 20  (VTable *)p4);. 
1a2aa 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1a2ab 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
1a2ac 20 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 3a   P4_SUBPROGRAM :
1a2ad 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   {.        sqlit
1a2ae 65 33 56 64 62 65 50 72 6f 67 72 61 6d 44 65 6c  e3VdbeProgramDel
1a2af 65 74 65 28 64 62 2c 20 28 53 75 62 50 72 6f 67  ete(db, (SubProg
1a2b0 72 61 6d 20 2a 29 70 34 2c 20 31 29 3b 0a 20 20  ram *)p4, 1);.  
1a2b1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1a2b2 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d     }.    }.  }.}
1a2b3 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65  ../*.** Free the
1a2b4 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64   space allocated
1a2b5 20 66 6f 72 20 61 4f 70 20 61 6e 64 20 61 6e 79   for aOp and any
1a2b6 20 70 34 20 76 61 6c 75 65 73 20 61 6c 6c 6f 63   p4 values alloc
1a2b7 61 74 65 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20  ated for the.** 
1a2b8 6f 70 63 6f 64 65 73 20 63 6f 6e 74 61 69 6e 65  opcodes containe
1a2b9 64 20 77 69 74 68 69 6e 2e 20 49 66 20 61 4f 70  d within. If aOp
1a2ba 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 69 74 20   is not NULL it 
1a2bb 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 63 6f  is assumed to co
1a2bc 6e 74 61 69 6e 20 0a 2a 2a 20 6e 4f 70 20 65 6e  ntain .** nOp en
1a2bd 74 72 69 65 73 2e 20 0a 2a 2f 0a 73 74 61 74 69  tries. .*/.stati
1a2be 63 20 76 6f 69 64 20 76 64 62 65 46 72 65 65 4f  c void vdbeFreeO
1a2bf 70 41 72 72 61 79 28 73 71 6c 69 74 65 33 20 2a  pArray(sqlite3 *
1a2c0 64 62 2c 20 4f 70 20 2a 61 4f 70 2c 20 69 6e 74  db, Op *aOp, int
1a2c1 20 6e 4f 70 29 7b 0a 20 20 69 66 28 20 61 4f 70   nOp){.  if( aOp
1a2c2 20 29 7b 0a 20 20 20 20 4f 70 20 2a 70 4f 70 3b   ){.    Op *pOp;
1a2c3 0a 20 20 20 20 66 6f 72 28 70 4f 70 3d 61 4f 70  .    for(pOp=aOp
1a2c4 3b 20 70 4f 70 3c 26 61 4f 70 5b 6e 4f 70 5d 3b  ; pOp<&aOp[nOp];
1a2c5 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20 66   pOp++){.      f
1a2c6 72 65 65 50 34 28 64 62 2c 20 70 4f 70 2d 3e 70  reeP4(db, pOp->p
1a2c7 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70  4type, pOp->p4.p
1a2c8 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  );.#ifdef SQLITE
1a2c9 5f 44 45 42 55 47 0a 20 20 20 20 20 20 73 71 6c  _DEBUG.      sql
1a2ca 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1a2cb 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 29 3b 0a 23  Op->zComment);.#
1a2cc 65 6e 64 69 66 20 20 20 20 20 0a 20 20 20 20 7d  endif     .    }
1a2cd 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 62  .  }.  sqlite3Db
1a2ce 46 72 65 65 28 64 62 2c 20 61 4f 70 29 3b 0a 7d  Free(db, aOp);.}
1a2cf 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65 6e  ../*.** Decremen
1a2d0 74 20 74 68 65 20 72 65 66 2d 63 6f 75 6e 74 20  t the ref-count 
1a2d1 6f 6e 20 74 68 65 20 53 75 62 50 72 6f 67 72 61  on the SubProgra
1a2d2 6d 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73  m structure pass
1a2d3 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 73 65 63  ed as the.** sec
1a2d4 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 20 49 66  ond argument. If
1a2d5 20 74 68 65 20 72 65 66 2d 63 6f 75 6e 74 20 72   the ref-count r
1a2d6 65 61 63 68 65 73 20 7a 65 72 6f 2c 20 66 72 65  eaches zero, fre
1a2d7 65 20 74 68 65 20 73 74 72 75 63 74 75 72 65 2e  e the structure.
1a2d8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 72 61 79  .**.** The array
1a2d9 20 6f 66 20 56 44 42 45 20 6f 70 63 6f 64 65 73   of VDBE opcodes
1a2da 20 73 74 6f 72 65 64 20 61 73 20 53 75 62 50 72   stored as SubPr
1a2db 6f 67 72 61 6d 2e 61 4f 70 20 69 73 20 66 72 65  ogram.aOp is fre
1a2dc 65 64 20 69 66 0a 2a 2a 20 65 69 74 68 65 72 20  ed if.** either 
1a2dd 74 68 65 20 72 65 66 2d 63 6f 75 6e 74 20 72 65  the ref-count re
1a2de 61 63 68 65 73 20 7a 65 72 6f 20 6f 72 20 70 61  aches zero or pa
1a2df 72 61 6d 65 74 65 72 20 66 72 65 65 6f 70 20 69  rameter freeop i
1a2e0 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a  s non-zero..**.*
1a2e1 2a 20 53 69 6e 63 65 20 74 68 65 20 61 72 72 61  * Since the arra
1a2e2 79 20 6f 66 20 6f 70 63 6f 64 65 73 20 70 6f 69  y of opcodes poi
1a2e3 6e 74 65 64 20 74 6f 20 62 79 20 53 75 62 50 72  nted to by SubPr
1a2e4 6f 67 72 61 6d 2e 61 4f 70 20 6d 61 79 20 64 69  ogram.aOp may di
1a2e5 72 65 63 74 6c 79 0a 2a 2a 20 6f 72 20 69 6e 64  rectly.** or ind
1a2e6 69 72 65 63 74 6c 79 20 63 6f 6e 74 61 69 6e 20  irectly contain 
1a2e7 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74  a reference to t
1a2e8 68 65 20 53 75 62 50 72 6f 67 72 61 6d 20 73 74  he SubProgram st
1a2e9 72 75 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0a  ructure itself..
1a2ea 2a 2a 20 42 79 20 70 61 73 73 69 6e 67 20 61 20  ** By passing a 
1a2eb 6e 6f 6e 2d 7a 65 72 6f 20 66 72 65 65 6f 70 20  non-zero freeop 
1a2ec 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65 20 63  parameter, the c
1a2ed 61 6c 6c 65 72 20 6d 61 79 20 65 6e 73 75 72 65  aller may ensure
1a2ee 20 74 68 61 74 20 61 6c 6c 0a 2a 2a 20 53 75 62   that all.** Sub
1a2ef 50 72 6f 67 72 61 6d 20 73 74 72 75 63 74 75 72  Program structur
1a2f0 65 73 20 61 6e 64 20 74 68 65 69 72 20 61 4f 70  es and their aOp
1a2f1 20 61 72 72 61 79 73 20 61 72 65 20 66 72 65 65   arrays are free
1a2f2 64 2c 20 65 76 65 6e 20 77 68 65 6e 20 74 68 65  d, even when the
1a2f3 72 65 0a 2a 2a 20 61 72 65 20 73 75 63 68 20 63  re.** are such c
1a2f4 69 72 63 75 6c 61 72 20 72 65 66 65 72 65 6e 63  ircular referenc
1a2f5 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es..*/.SQLITE_PR
1a2f6 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1a2f7 65 33 56 64 62 65 50 72 6f 67 72 61 6d 44 65 6c  e3VdbeProgramDel
1a2f8 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ete(sqlite3 *db,
1a2f9 20 53 75 62 50 72 6f 67 72 61 6d 20 2a 70 2c 20   SubProgram *p, 
1a2fa 69 6e 74 20 66 72 65 65 6f 70 29 7b 0a 20 20 69  int freeop){.  i
1a2fb 66 28 20 70 20 29 7b 0a 20 20 20 20 61 73 73 65  f( p ){.    asse
1a2fc 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b  rt( p->nRef>0 );
1a2fd 0a 20 20 20 20 69 66 28 20 66 72 65 65 6f 70 20  .    if( freeop 
1a2fe 7c 7c 20 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 7b  || p->nRef==1 ){
1a2ff 0a 20 20 20 20 20 20 4f 70 20 2a 61 4f 70 20 3d  .      Op *aOp =
1a300 20 70 2d 3e 61 4f 70 3b 0a 20 20 20 20 20 20 70   p->aOp;.      p
1a301 2d 3e 61 4f 70 20 3d 20 30 3b 0a 20 20 20 20 20  ->aOp = 0;.     
1a302 20 76 64 62 65 46 72 65 65 4f 70 41 72 72 61 79   vdbeFreeOpArray
1a303 28 64 62 2c 20 61 4f 70 2c 20 70 2d 3e 6e 4f 70  (db, aOp, p->nOp
1a304 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 4f 70 20  );.      p->nOp 
1a305 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  = 0;.    }.    p
1a306 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66  ->nRef--;.    if
1a307 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a  ( p->nRef==0 ){.
1a308 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1a309 72 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 20 20  ree(db, p);.    
1a30a 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  }.  }.}.../*.** 
1a30b 43 68 61 6e 67 65 20 4e 20 6f 70 63 6f 64 65 73  Change N opcodes
1a30c 20 73 74 61 72 74 69 6e 67 20 61 74 20 61 64 64   starting at add
1a30d 72 20 74 6f 20 4e 6f 2d 6f 70 73 2e 0a 2a 2f 0a  r to No-ops..*/.
1a30e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1a30f 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43  oid sqlite3VdbeC
1a310 68 61 6e 67 65 54 6f 4e 6f 6f 70 28 56 64 62 65  hangeToNoop(Vdbe
1a311 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69   *p, int addr, i
1a312 6e 74 20 4e 29 7b 0a 20 20 69 66 28 20 70 2d 3e  nt N){.  if( p->
1a313 61 4f 70 20 29 7b 0a 20 20 20 20 56 64 62 65 4f  aOp ){.    VdbeO
1a314 70 20 2a 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70  p *pOp = &p->aOp
1a315 5b 61 64 64 72 5d 3b 0a 20 20 20 20 73 71 6c 69  [addr];.    sqli
1a316 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b  te3 *db = p->db;
1a317 0a 20 20 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20  .    while( N-- 
1a318 29 7b 0a 20 20 20 20 20 20 66 72 65 65 50 34 28  ){.      freeP4(
1a319 64 62 2c 20 70 4f 70 2d 3e 70 34 74 79 70 65 2c  db, pOp->p4type,
1a31a 20 70 4f 70 2d 3e 70 34 2e 70 29 3b 0a 20 20 20   pOp->p4.p);.   
1a31b 20 20 20 6d 65 6d 73 65 74 28 70 4f 70 2c 20 30     memset(pOp, 0
1a31c 2c 20 73 69 7a 65 6f 66 28 70 4f 70 5b 30 5d 29  , sizeof(pOp[0])
1a31d 29 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70  );.      pOp->op
1a31e 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a  code = OP_Noop;.
1a31f 20 20 20 20 20 20 70 4f 70 2b 2b 3b 0a 20 20 20        pOp++;.   
1a320 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
1a321 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65  Change the value
1a322 20 6f 66 20 74 68 65 20 50 34 20 6f 70 65 72 61   of the P4 opera
1a323 6e 64 20 66 6f 72 20 61 20 73 70 65 63 69 66 69  nd for a specifi
1a324 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  c instruction..*
1a325 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
1a326 73 20 75 73 65 66 75 6c 20 77 68 65 6e 20 61 20  s useful when a 
1a327 6c 61 72 67 65 20 70 72 6f 67 72 61 6d 20 69 73  large program is
1a328 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20 61 0a 2a   loaded from a.*
1a329 2a 20 73 74 61 74 69 63 20 61 72 72 61 79 20 75  * static array u
1a32a 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62 65  sing sqlite3Vdbe
1a32b 41 64 64 4f 70 4c 69 73 74 20 62 75 74 20 77 65  AddOpList but we
1a32c 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 61 0a   want to make a.
1a32d 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20 63 68 61  ** few minor cha
1a32e 6e 67 65 73 20 74 6f 20 74 68 65 20 70 72 6f 67  nges to the prog
1a32f 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 3e  ram..**.** If n>
1a330 3d 30 20 74 68 65 6e 20 74 68 65 20 50 34 20 6f  =0 then the P4 o
1a331 70 65 72 61 6e 64 20 69 73 20 64 79 6e 61 6d 69  perand is dynami
1a332 63 2c 20 6d 65 61 6e 69 6e 67 20 74 68 61 74 20  c, meaning that 
1a333 61 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65  a copy of.** the
1a334 20 73 74 72 69 6e 67 20 69 73 20 6d 61 64 65 20   string is made 
1a335 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  into memory obta
1a336 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
1a337 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 41  3_malloc()..** A
1a338 20 76 61 6c 75 65 20 6f 66 20 6e 3d 3d 30 20 6d   value of n==0 m
1a339 65 61 6e 73 20 63 6f 70 79 20 62 79 74 65 73 20  eans copy bytes 
1a33a 6f 66 20 7a 50 34 20 75 70 20 74 6f 20 61 6e 64  of zP4 up to and
1a33b 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 0a 2a   including the.*
1a33c 2a 20 66 69 72 73 74 20 6e 75 6c 6c 20 62 79 74  * first null byt
1a33d 65 2e 20 20 49 66 20 6e 3e 30 20 74 68 65 6e 20  e.  If n>0 then 
1a33e 63 6f 70 79 20 6e 2b 31 20 62 79 74 65 73 20 6f  copy n+1 bytes o
1a33f 66 20 7a 50 34 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  f zP4..**.** If 
1a340 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 69 74  n==P4_KEYINFO it
1a341 20 6d 65 61 6e 73 20 74 68 61 74 20 7a 50 34 20   means that zP4 
1a342 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
1a343 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74  a KeyInfo struct
1a344 75 72 65 2e 0a 2a 2a 20 41 20 63 6f 70 79 20 69  ure..** A copy i
1a345 73 20 6d 61 64 65 20 6f 66 20 74 68 65 20 4b 65  s made of the Ke
1a346 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
1a347 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61  into memory obta
1a348 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c  ined from.** sql
1a349 69 74 65 33 5f 6d 61 6c 6c 6f 63 2c 20 74 6f 20  ite3_malloc, to 
1a34a 62 65 20 66 72 65 65 64 20 77 68 65 6e 20 74 68  be freed when th
1a34b 65 20 56 64 62 65 20 69 73 20 66 69 6e 61 6c 69  e Vdbe is finali
1a34c 7a 65 64 2e 0a 2a 2a 20 6e 3d 3d 50 34 5f 4b 45  zed..** n==P4_KE
1a34d 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 69 6e  YINFO_HANDOFF in
1a34e 64 69 63 61 74 65 73 20 74 68 61 74 20 7a 50 34  dicates that zP4
1a34f 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b 65 79   points to a Key
1a350 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a 2a  Info structure.*
1a351 2a 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f  * stored in memo
1a352 72 79 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c  ry that the call
1a353 65 72 20 68 61 73 20 6f 62 74 61 69 6e 65 64 20  er has obtained 
1a354 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  from sqlite3_mal
1a355 6c 6f 63 2e 20 54 68 65 20 0a 2a 2a 20 63 61 6c  loc. The .** cal
1a356 6c 65 72 20 73 68 6f 75 6c 64 20 6e 6f 74 20 66  ler should not f
1a357 72 65 65 20 74 68 65 20 61 6c 6c 6f 63 61 74 69  ree the allocati
1a358 6f 6e 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 66  on, it will be f
1a359 72 65 65 64 20 77 68 65 6e 20 74 68 65 20 56 64  reed when the Vd
1a35a 62 65 20 69 73 0a 2a 2a 20 66 69 6e 61 6c 69 7a  be is.** finaliz
1a35b 65 64 2e 0a 2a 2a 20 0a 2a 2a 20 4f 74 68 65 72  ed..** .** Other
1a35c 20 76 61 6c 75 65 73 20 6f 66 20 6e 20 28 50 34   values of n (P4
1a35d 5f 53 54 41 54 49 43 2c 20 50 34 5f 43 4f 4c 4c  _STATIC, P4_COLL
1a35e 53 45 51 20 65 74 63 2e 29 20 69 6e 64 69 63 61  SEQ etc.) indica
1a35f 74 65 20 74 68 61 74 20 7a 50 34 20 70 6f 69 6e  te that zP4 poin
1a360 74 73 0a 2a 2a 20 74 6f 20 61 20 73 74 72 69 6e  ts.** to a strin
1a361 67 20 6f 72 20 73 74 72 75 63 74 75 72 65 20 74  g or structure t
1a362 68 61 74 20 69 73 20 67 75 61 72 61 6e 74 65 65  hat is guarantee
1a363 64 20 74 6f 20 65 78 69 73 74 20 66 6f 72 20 74  d to exist for t
1a364 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 0a 2a  he lifetime of.*
1a365 2a 20 74 68 65 20 56 64 62 65 2e 20 49 6e 20 74  * the Vdbe. In t
1a366 68 65 73 65 20 63 61 73 65 73 20 77 65 20 63 61  hese cases we ca
1a367 6e 20 6a 75 73 74 20 63 6f 70 79 20 74 68 65 20  n just copy the 
1a368 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49  pointer..**.** I
1a369 66 20 61 64 64 72 3c 30 20 74 68 65 6e 20 63 68  f addr<0 then ch
1a36a 61 6e 67 65 20 50 34 20 6f 6e 20 74 68 65 20 6d  ange P4 on the m
1a36b 6f 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73  ost recently ins
1a36c 65 72 74 65 64 20 69 6e 73 74 72 75 63 74 69 6f  erted instructio
1a36d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
1a36e 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1a36f 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 56 64  3VdbeChangeP4(Vd
1a370 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64 72 2c  be *p, int addr,
1a371 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 34   const char *zP4
1a372 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 4f 70 20 2a  , int n){.  Op *
1a373 70 4f 70 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  pOp;.  sqlite3 *
1a374 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 21  db;.  assert( p!
1a375 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20 70 2d 3e  =0 );.  db = p->
1a376 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  db;.  assert( p-
1a377 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47  >magic==VDBE_MAG
1a378 49 43 5f 49 4e 49 54 20 29 3b 0a 20 20 69 66 28  IC_INIT );.  if(
1a379 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 64 62   p->aOp==0 || db
1a37a 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1a37b 7b 0a 20 20 20 20 69 66 20 28 20 6e 21 3d 50 34  {.    if ( n!=P4
1a37c 5f 4b 45 59 49 4e 46 4f 20 26 26 20 6e 21 3d 50  _KEYINFO && n!=P
1a37d 34 5f 56 54 41 42 20 29 20 7b 0a 20 20 20 20 20  4_VTAB ) {.     
1a37e 20 66 72 65 65 50 34 28 64 62 2c 20 6e 2c 20 28   freeP4(db, n, (
1a37f 76 6f 69 64 2a 29 2a 28 63 68 61 72 2a 2a 29 26  void*)*(char**)&
1a380 7a 50 34 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  zP4);.    }.    
1a381 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73  return;.  }.  as
1a382 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29  sert( p->nOp>0 )
1a383 3b 0a 20 20 61 73 73 65 72 74 28 20 61 64 64 72  ;.  assert( addr
1a384 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 69 66 28  <p->nOp );.  if(
1a385 20 61 64 64 72 3c 30 20 29 7b 0a 20 20 20 20 61   addr<0 ){.    a
1a386 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 20 2d 20 31  ddr = p->nOp - 1
1a387 3b 0a 20 20 7d 0a 20 20 70 4f 70 20 3d 20 26 70  ;.  }.  pOp = &p
1a388 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b 0a 20 20 66  ->aOp[addr];.  f
1a389 72 65 65 50 34 28 64 62 2c 20 70 4f 70 2d 3e 70  reeP4(db, pOp->p
1a38a 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70  4type, pOp->p4.p
1a38b 29 3b 0a 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d  );.  pOp->p4.p =
1a38c 20 30 3b 0a 20 20 69 66 28 20 6e 3d 3d 50 34 5f   0;.  if( n==P4_
1a38d 49 4e 54 33 32 20 29 7b 0a 20 20 20 20 2f 2a 20  INT32 ){.    /* 
1a38e 4e 6f 74 65 3a 20 74 68 69 73 20 63 61 73 74 20  Note: this cast 
1a38f 69 73 20 73 61 66 65 2c 20 62 65 63 61 75 73 65  is safe, because
1a390 20 74 68 65 20 6f 72 69 67 69 6e 20 64 61 74 61   the origin data
1a391 20 70 6f 69 6e 74 20 77 61 73 20 61 6e 20 69 6e   point was an in
1a392 74 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 77 61  t.    ** that wa
1a393 73 20 63 61 73 74 20 74 6f 20 61 20 28 63 6f 6e  s cast to a (con
1a394 73 74 20 63 68 61 72 20 2a 29 2e 20 2a 2f 0a 20  st char *). */. 
1a395 20 20 20 70 4f 70 2d 3e 70 34 2e 69 20 3d 20 53     pOp->p4.i = S
1a396 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54  QLITE_PTR_TO_INT
1a397 28 7a 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d 3e  (zP4);.    pOp->
1a398 70 34 74 79 70 65 20 3d 20 50 34 5f 49 4e 54 33  p4type = P4_INT3
1a399 32 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 7a  2;.  }else if( z
1a39a 50 34 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4f 70  P4==0 ){.    pOp
1a39b 2d 3e 70 34 2e 70 20 3d 20 30 3b 0a 20 20 20 20  ->p4.p = 0;.    
1a39c 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34  pOp->p4type = P4
1a39d 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 7d 65 6c 73  _NOTUSED;.  }els
1a39e 65 20 69 66 28 20 6e 3d 3d 50 34 5f 4b 45 59 49  e if( n==P4_KEYI
1a39f 4e 46 4f 20 29 7b 0a 20 20 20 20 4b 65 79 49 6e  NFO ){.    KeyIn
1a3a0 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20  fo *pKeyInfo;.  
1a3a1 20 20 69 6e 74 20 6e 46 69 65 6c 64 2c 20 6e 42    int nField, nB
1a3a2 79 74 65 3b 0a 0a 20 20 20 20 6e 46 69 65 6c 64  yte;..    nField
1a3a3 20 3d 20 28 28 4b 65 79 49 6e 66 6f 2a 29 7a 50   = ((KeyInfo*)zP
1a3a4 34 29 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 20 20  4)->nField;.    
1a3a5 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a  nByte = sizeof(*
1a3a6 70 4b 65 79 49 6e 66 6f 29 20 2b 20 28 6e 46 69  pKeyInfo) + (nFi
1a3a7 65 6c 64 2d 31 29 2a 73 69 7a 65 6f 66 28 70 4b  eld-1)*sizeof(pK
1a3a8 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 30 5d  eyInfo->aColl[0]
1a3a9 29 20 2b 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20  ) + nField;.    
1a3aa 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69 74  pKeyInfo = sqlit
1a3ab 65 33 4d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 20  e3Malloc( nByte 
1a3ac 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70  );.    pOp->p4.p
1a3ad 4b 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e  KeyInfo = pKeyIn
1a3ae 66 6f 3b 0a 20 20 20 20 69 66 28 20 70 4b 65 79  fo;.    if( pKey
1a3af 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 75 38  Info ){.      u8
1a3b0 20 2a 61 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20   *aSortOrder;.  
1a3b1 20 20 20 20 6d 65 6d 63 70 79 28 70 4b 65 79 49      memcpy(pKeyI
1a3b2 6e 66 6f 2c 20 7a 50 34 2c 20 6e 42 79 74 65 29  nfo, zP4, nByte)
1a3b3 3b 0a 20 20 20 20 20 20 61 53 6f 72 74 4f 72 64  ;.      aSortOrd
1a3b4 65 72 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61  er = pKeyInfo->a
1a3b5 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 20  SortOrder;.     
1a3b6 20 69 66 28 20 61 53 6f 72 74 4f 72 64 65 72 20   if( aSortOrder 
1a3b7 29 7b 0a 20 20 20 20 20 20 20 20 70 4b 65 79 49  ){.        pKeyI
1a3b8 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20  nfo->aSortOrder 
1a3b9 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
1a3ba 2a 29 26 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f  *)&pKeyInfo->aCo
1a3bb 6c 6c 5b 6e 46 69 65 6c 64 5d 3b 0a 20 20 20 20  ll[nField];.    
1a3bc 20 20 20 20 6d 65 6d 63 70 79 28 70 4b 65 79 49      memcpy(pKeyI
1a3bd 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 2c  nfo->aSortOrder,
1a3be 20 61 53 6f 72 74 4f 72 64 65 72 2c 20 6e 46 69   aSortOrder, nFi
1a3bf 65 6c 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  eld);.      }.  
1a3c0 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20      pOp->p4type 
1a3c1 3d 20 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20  = P4_KEYINFO;.  
1a3c2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
1a3c3 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
1a3c4 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f  ed = 1;.      pO
1a3c5 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 4e  p->p4type = P4_N
1a3c6 4f 54 55 53 45 44 3b 0a 20 20 20 20 7d 0a 20 20  OTUSED;.    }.  
1a3c7 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50 34 5f  }else if( n==P4_
1a3c8 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20  KEYINFO_HANDOFF 
1a3c9 29 7b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70  ){.    pOp->p4.p
1a3ca 20 3d 20 28 76 6f 69 64 2a 29 7a 50 34 3b 0a 20   = (void*)zP4;. 
1a3cb 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20 3d     pOp->p4type =
1a3cc 20 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20 7d   P4_KEYINFO;.  }
1a3cd 65 6c 73 65 20 69 66 28 20 6e 3d 3d 50 34 5f 56  else if( n==P4_V
1a3ce 54 41 42 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e  TAB ){.    pOp->
1a3cf 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50  p4.p = (void*)zP
1a3d0 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79  4;.    pOp->p4ty
1a3d1 70 65 20 3d 20 50 34 5f 56 54 41 42 3b 0a 20 20  pe = P4_VTAB;.  
1a3d2 20 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63    sqlite3VtabLoc
1a3d3 6b 28 28 56 54 61 62 6c 65 20 2a 29 7a 50 34 29  k((VTable *)zP4)
1a3d4 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 28  ;.    assert( ((
1a3d5 56 54 61 62 6c 65 20 2a 29 7a 50 34 29 2d 3e 64  VTable *)zP4)->d
1a3d6 62 3d 3d 70 2d 3e 64 62 20 29 3b 0a 20 20 7d 65  b==p->db );.  }e
1a3d7 6c 73 65 20 69 66 28 20 6e 3c 30 20 29 7b 0a 20  lse if( n<0 ){. 
1a3d8 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 28     pOp->p4.p = (
1a3d9 76 6f 69 64 2a 29 7a 50 34 3b 0a 20 20 20 20 70  void*)zP4;.    p
1a3da 4f 70 2d 3e 70 34 74 79 70 65 20 3d 20 28 73 69  Op->p4type = (si
1a3db 67 6e 65 64 20 63 68 61 72 29 6e 3b 0a 20 20 7d  gned char)n;.  }
1a3dc 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 6e 3d  else{.    if( n=
1a3dd 3d 30 20 29 20 6e 20 3d 20 73 71 6c 69 74 65 33  =0 ) n = sqlite3
1a3de 53 74 72 6c 65 6e 33 30 28 7a 50 34 29 3b 0a 20  Strlen30(zP4);. 
1a3df 20 20 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d 20 73     pOp->p4.z = s
1a3e0 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
1a3e1 70 2d 3e 64 62 2c 20 7a 50 34 2c 20 6e 29 3b 0a  p->db, zP4, n);.
1a3e2 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65 20      pOp->p4type 
1a3e3 3d 20 50 34 5f 44 59 4e 41 4d 49 43 3b 0a 20 20  = P4_DYNAMIC;.  
1a3e4 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45  }.}..#ifndef NDE
1a3e5 42 55 47 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  BUG./*.** Change
1a3e6 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20   the comment on 
1a3e7 74 68 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63  the the most rec
1a3e8 65 6e 74 6c 79 20 63 6f 64 65 64 20 69 6e 73 74  ently coded inst
1a3e9 72 75 63 74 69 6f 6e 2e 20 20 4f 72 0a 2a 2a 20  ruction.  Or.** 
1a3ea 69 6e 73 65 72 74 20 61 20 4e 6f 2d 6f 70 20 61  insert a No-op a
1a3eb 6e 64 20 61 64 64 20 74 68 65 20 63 6f 6d 6d 65  nd add the comme
1a3ec 6e 74 20 74 6f 20 74 68 61 74 20 6e 65 77 20 69  nt to that new i
1a3ed 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 54 68 69  nstruction.  Thi
1a3ee 73 0a 2a 2a 20 6d 61 6b 65 73 20 74 68 65 20 63  s.** makes the c
1a3ef 6f 64 65 20 65 61 73 69 65 72 20 74 6f 20 72 65  ode easier to re
1a3f0 61 64 20 64 75 72 69 6e 67 20 64 65 62 75 67 67  ad during debugg
1a3f1 69 6e 67 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68  ing.  None of th
1a3f2 69 73 20 68 61 70 70 65 6e 73 0a 2a 2a 20 69 6e  is happens.** in
1a3f3 20 61 20 70 72 6f 64 75 63 74 69 6f 6e 20 62 75   a production bu
1a3f4 69 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ild..*/.SQLITE_P
1a3f5 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1a3f6 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74 28 56  te3VdbeComment(V
1a3f7 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68  dbe *p, const ch
1a3f8 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e  ar *zFormat, ...
1a3f9 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  ){.  va_list ap;
1a3fa 0a 20 20 69 66 28 20 21 70 20 29 20 72 65 74 75  .  if( !p ) retu
1a3fb 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  rn;.  assert( p-
1a3fc 3e 6e 4f 70 3e 30 20 7c 7c 20 70 2d 3e 61 4f 70  >nOp>0 || p->aOp
1a3fd 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
1a3fe 20 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 70 2d   p->aOp==0 || p-
1a3ff 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a  >aOp[p->nOp-1].z
1a400 43 6f 6d 6d 65 6e 74 3d 3d 30 20 7c 7c 20 70 2d  Comment==0 || p-
1a401 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
1a402 64 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f  d );.  if( p->nO
1a403 70 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a  p ){.    char **
1a404 70 7a 20 3d 20 26 70 2d 3e 61 4f 70 5b 70 2d 3e  pz = &p->aOp[p->
1a405 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b  nOp-1].zComment;
1a406 0a 20 20 20 20 76 61 5f 73 74 61 72 74 28 61 70  .    va_start(ap
1a407 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20  , zFormat);.    
1a408 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d  sqlite3DbFree(p-
1a409 3e 64 62 2c 20 2a 70 7a 29 3b 0a 20 20 20 20 2a  >db, *pz);.    *
1a40a 70 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72  pz = sqlite3VMPr
1a40b 69 6e 74 66 28 70 2d 3e 64 62 2c 20 7a 46 6f 72  intf(p->db, zFor
1a40c 6d 61 74 2c 20 61 70 29 3b 0a 20 20 20 20 76 61  mat, ap);.    va
1a40d 5f 65 6e 64 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a  _end(ap);.  }.}.
1a40e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1a40f 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4e  oid sqlite3VdbeN
1a410 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20  oopComment(Vdbe 
1a411 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  *p, const char *
1a412 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20  zFormat, ...){. 
1a413 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69   va_list ap;.  i
1a414 66 28 20 21 70 20 29 20 72 65 74 75 72 6e 3b 0a  f( !p ) return;.
1a415 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1a416 4f 70 30 28 70 2c 20 4f 50 5f 4e 6f 6f 70 29 3b  Op0(p, OP_Noop);
1a417 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 4f  .  assert( p->nO
1a418 70 3e 30 20 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30  p>0 || p->aOp==0
1a419 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
1a41a 3e 61 4f 70 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f  >aOp==0 || p->aO
1a41b 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d  p[p->nOp-1].zCom
1a41c 6d 65 6e 74 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62  ment==0 || p->db
1a41d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1a41e 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70 20 29  ;.  if( p->nOp )
1a41f 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 70 7a 20  {.    char **pz 
1a420 3d 20 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70  = &p->aOp[p->nOp
1a421 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20  -1].zComment;.  
1a422 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 7a    va_start(ap, z
1a423 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 73 71 6c  Format);.    sql
1a424 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62  ite3DbFree(p->db
1a425 2c 20 2a 70 7a 29 3b 0a 20 20 20 20 2a 70 7a 20  , *pz);.    *pz 
1a426 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69 6e 74  = sqlite3VMPrint
1a427 66 28 70 2d 3e 64 62 2c 20 7a 46 6f 72 6d 61 74  f(p->db, zFormat
1a428 2c 20 61 70 29 3b 0a 20 20 20 20 76 61 5f 65 6e  , ap);.    va_en
1a429 64 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  d(ap);.  }.}.#en
1a42a 64 69 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a  dif  /* NDEBUG *
1a42b 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  /../*.** Return 
1a42c 74 68 65 20 6f 70 63 6f 64 65 20 66 6f 72 20 61  the opcode for a
1a42d 20 67 69 76 65 6e 20 61 64 64 72 65 73 73 2e 20   given address. 
1a42e 20 49 66 20 74 68 65 20 61 64 64 72 65 73 73 20   If the address 
1a42f 69 73 20 2d 31 2c 20 74 68 65 6e 0a 2a 2a 20 72  is -1, then.** r
1a430 65 74 75 72 6e 20 74 68 65 20 6d 6f 73 74 20 72  eturn the most r
1a431 65 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64  ecently inserted
1a432 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49   opcode..**.** I
1a433 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  f a memory alloc
1a434 61 74 69 6f 6e 20 65 72 72 6f 72 20 68 61 73 20  ation error has 
1a435 6f 63 63 75 72 72 65 64 20 70 72 69 6f 72 20 74  occurred prior t
1a436 6f 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 6f 66  o the calling of
1a437 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65   this.** routine
1a438 2c 20 74 68 65 6e 20 61 20 70 6f 69 6e 74 65 72  , then a pointer
1a439 20 74 6f 20 61 20 64 75 6d 6d 79 20 56 64 62 65   to a dummy Vdbe
1a43a 4f 70 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72  Op will be retur
1a43b 6e 65 64 2e 20 20 54 68 61 74 20 6f 70 63 6f 64  ned.  That opcod
1a43c 65 0a 2a 2a 20 69 73 20 72 65 61 64 61 62 6c 65  e.** is readable
1a43d 20 61 6e 64 20 77 72 69 74 61 62 6c 65 2c 20 62   and writable, b
1a43e 75 74 20 69 74 20 68 61 73 20 6e 6f 20 65 66 66  ut it has no eff
1a43f 65 63 74 2e 20 20 54 68 65 20 72 65 74 75 72 6e  ect.  The return
1a440 20 6f 66 20 61 20 64 75 6d 6d 79 0a 2a 2a 20 6f   of a dummy.** o
1a441 70 63 6f 64 65 20 61 6c 6c 6f 77 73 20 74 68 65  pcode allows the
1a442 20 63 61 6c 6c 20 74 6f 20 63 6f 6e 74 69 6e 75   call to continu
1a443 65 20 66 75 6e 63 74 69 6f 6e 69 6e 67 20 61 66  e functioning af
1a444 74 65 72 20 61 20 4f 4f 4d 20 66 61 75 6c 74 20  ter a OOM fault 
1a445 77 69 74 68 6f 75 74 0a 2a 2a 20 68 61 76 69 6e  without.** havin
1a446 67 20 74 6f 20 63 68 65 63 6b 20 74 6f 20 73 65  g to check to se
1a447 65 20 69 66 20 74 68 65 20 72 65 74 75 72 6e 20  e if the return 
1a448 66 72 6f 6d 20 74 68 69 73 20 72 6f 75 74 69 6e  from this routin
1a449 65 20 69 73 20 61 20 76 61 6c 69 64 20 70 6f 69  e is a valid poi
1a44a 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 62 6f 75  nter..**.** Abou
1a44b 74 20 74 68 65 20 23 69 66 64 65 66 20 53 51 4c  t the #ifdef SQL
1a44c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 3a 20  ITE_OMIT_TRACE: 
1a44d 20 4e 6f 72 6d 61 6c 6c 79 2c 20 74 68 69 73 20   Normally, this 
1a44e 72 6f 75 74 69 6e 65 20 69 73 20 6e 65 76 65 72  routine is never
1a44f 20 63 61 6c 6c 65 64 0a 2a 2a 20 75 6e 6c 65 73   called.** unles
1a450 73 20 70 2d 3e 6e 4f 70 3e 30 2e 20 20 54 68 69  s p->nOp>0.  Thi
1a451 73 20 69 73 20 62 65 63 61 75 73 65 20 69 6e 20  s is because in 
1a452 74 68 65 20 61 62 73 65 6e 73 65 20 6f 66 20 53  the absense of S
1a453 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45  QLITE_OMIT_TRACE
1a454 2c 0a 2a 2a 20 61 6e 20 4f 50 5f 54 72 61 63 65  ,.** an OP_Trace
1a455 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20   instruction is 
1a456 61 6c 77 61 79 73 20 69 6e 73 65 72 74 65 64 20  always inserted 
1a457 62 79 20 73 71 6c 69 74 65 33 56 64 62 65 47 65  by sqlite3VdbeGe
1a458 74 28 29 20 61 73 20 73 6f 6f 6e 20 61 73 0a 2a  t() as soon as.*
1a459 2a 20 61 20 6e 65 77 20 56 44 42 45 20 69 73 20  * a new VDBE is 
1a45a 63 72 65 61 74 65 64 2e 20 20 53 6f 20 77 65 20  created.  So we 
1a45b 61 72 65 20 66 72 65 65 20 74 6f 20 73 65 74 20  are free to set 
1a45c 61 64 64 72 20 74 6f 20 70 2d 3e 6e 4f 70 2d 31  addr to p->nOp-1
1a45d 20 77 69 74 68 6f 75 74 0a 2a 2a 20 68 61 76 69   without.** havi
1a45e 6e 67 20 74 6f 20 64 6f 75 62 6c 65 2d 63 68 65  ng to double-che
1a45f 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ck to make sure 
1a460 74 68 61 74 20 74 68 65 20 72 65 73 75 6c 74 20  that the result 
1a461 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e  is non-negative.
1a462 20 42 75 74 0a 2a 2a 20 69 66 20 53 51 4c 49 54   But.** if SQLIT
1a463 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 69 73 20  E_OMIT_TRACE is 
1a464 64 65 66 69 6e 65 64 2c 20 74 68 65 20 4f 50 5f  defined, the OP_
1a465 54 72 61 63 65 20 69 73 20 6f 6d 69 74 74 65 64  Trace is omitted
1a466 20 61 6e 64 20 77 65 20 64 6f 20 6e 65 65 64 20   and we do need 
1a467 74 6f 0a 2a 2a 20 63 68 65 63 6b 20 74 68 65 20  to.** check the 
1a468 76 61 6c 75 65 20 6f 66 20 70 2d 3e 6e 4f 70 2d  value of p->nOp-
1a469 31 20 62 65 66 6f 72 65 20 63 6f 6e 74 69 6e 75  1 before continu
1a46a 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ing..*/.SQLITE_P
1a46b 52 49 56 41 54 45 20 56 64 62 65 4f 70 20 2a 73  RIVATE VdbeOp *s
1a46c 71 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28  qlite3VdbeGetOp(
1a46d 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64  Vdbe *p, int add
1a46e 72 29 7b 0a 20 20 73 74 61 74 69 63 20 56 64 62  r){.  static Vdb
1a46f 65 4f 70 20 64 75 6d 6d 79 3b 0a 20 20 61 73 73  eOp dummy;.  ass
1a470 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56  ert( p->magic==V
1a471 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29  DBE_MAGIC_INIT )
1a472 3b 0a 20 20 69 66 28 20 61 64 64 72 3c 30 20 29  ;.  if( addr<0 )
1a473 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  {.#ifdef SQLITE_
1a474 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 20 20 69  OMIT_TRACE.    i
1a475 66 28 20 70 2d 3e 6e 4f 70 3d 3d 30 20 29 20 72  f( p->nOp==0 ) r
1a476 65 74 75 72 6e 20 26 64 75 6d 6d 79 3b 0a 23 65  eturn &dummy;.#e
1a477 6e 64 69 66 0a 20 20 20 20 61 64 64 72 20 3d 20  ndif.    addr = 
1a478 70 2d 3e 6e 4f 70 20 2d 20 31 3b 0a 20 20 7d 0a  p->nOp - 1;.  }.
1a479 20 20 61 73 73 65 72 74 28 20 28 61 64 64 72 3e    assert( (addr>
1a47a 3d 30 20 26 26 20 61 64 64 72 3c 70 2d 3e 6e 4f  =0 && addr<p->nO
1a47b 70 29 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61 6c  p) || p->db->mal
1a47c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69  locFailed );.  i
1a47d 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  f( p->db->malloc
1a47e 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65  Failed ){.    re
1a47f 74 75 72 6e 20 26 64 75 6d 6d 79 3b 0a 20 20 7d  turn &dummy;.  }
1a480 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
1a481 20 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b 0a   &p->aOp[addr];.
1a482 20 20 7d 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69    }.}..#if !defi
1a483 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
1a484 45 58 50 4c 41 49 4e 29 20 7c 7c 20 21 64 65 66  EXPLAIN) || !def
1a485 69 6e 65 64 28 4e 44 45 42 55 47 29 20 5c 0a 20  ined(NDEBUG) \. 
1a486 20 20 20 20 7c 7c 20 64 65 66 69 6e 65 64 28 56      || defined(V
1a487 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20  DBE_PROFILE) || 
1a488 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
1a489 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  EBUG)./*.** Comp
1a48a 75 74 65 20 61 20 73 74 72 69 6e 67 20 74 68 61  ute a string tha
1a48b 74 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20  t describes the 
1a48c 50 34 20 70 61 72 61 6d 65 74 65 72 20 66 6f 72  P4 parameter for
1a48d 20 61 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 55   an opcode..** U
1a48e 73 65 20 7a 54 65 6d 70 20 66 6f 72 20 61 6e 79  se zTemp for any
1a48f 20 72 65 71 75 69 72 65 64 20 74 65 6d 70 6f 72   required tempor
1a490 61 72 79 20 62 75 66 66 65 72 20 73 70 61 63 65  ary buffer space
1a491 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72  ..*/.static char
1a492 20 2a 64 69 73 70 6c 61 79 50 34 28 4f 70 20 2a   *displayP4(Op *
1a493 70 4f 70 2c 20 63 68 61 72 20 2a 7a 54 65 6d 70  pOp, char *zTemp
1a494 2c 20 69 6e 74 20 6e 54 65 6d 70 29 7b 0a 20 20  , int nTemp){.  
1a495 63 68 61 72 20 2a 7a 50 34 20 3d 20 7a 54 65 6d  char *zP4 = zTem
1a496 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 54 65  p;.  assert( nTe
1a497 6d 70 3e 3d 32 30 20 29 3b 0a 20 20 73 77 69 74  mp>=20 );.  swit
1a498 63 68 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 20  ch( pOp->p4type 
1a499 29 7b 0a 20 20 20 20 63 61 73 65 20 50 34 5f 4b  ){.    case P4_K
1a49a 45 59 49 4e 46 4f 5f 53 54 41 54 49 43 3a 0a 20  EYINFO_STATIC:. 
1a49b 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59 49 4e     case P4_KEYIN
1a49c 46 4f 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  FO: {.      int 
1a49d 69 2c 20 6a 3b 0a 20 20 20 20 20 20 4b 65 79 49  i, j;.      KeyI
1a49e 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d 20  nfo *pKeyInfo = 
1a49f 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f  pOp->p4.pKeyInfo
1a4a0 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
1a4a1 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20  snprintf(nTemp, 
1a4a2 7a 54 65 6d 70 2c 20 22 6b 65 79 69 6e 66 6f 28  zTemp, "keyinfo(
1a4a3 25 64 22 2c 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e  %d", pKeyInfo->n
1a4a4 46 69 65 6c 64 29 3b 0a 20 20 20 20 20 20 69 20  Field);.      i 
1a4a5 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
1a4a6 30 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 20 20  0(zTemp);.      
1a4a7 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4b 65 79 49  for(j=0; j<pKeyI
1a4a8 6e 66 6f 2d 3e 6e 46 69 65 6c 64 3b 20 6a 2b 2b  nfo->nField; j++
1a4a9 29 7b 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c 53  ){.        CollS
1a4aa 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 70 4b 65 79  eq *pColl = pKey
1a4ab 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6a 5d 3b 0a  Info->aColl[j];.
1a4ac 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c          if( pCol
1a4ad 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  l ){.          i
1a4ae 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74  nt n = sqlite3St
1a4af 72 6c 65 6e 33 30 28 70 43 6f 6c 6c 2d 3e 7a 4e  rlen30(pColl->zN
1a4b0 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ame);.          
1a4b1 69 66 28 20 69 2b 6e 3e 6e 54 65 6d 70 2d 36 20  if( i+n>nTemp-6 
1a4b2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d  ){.            m
1a4b3 65 6d 63 70 79 28 26 7a 54 65 6d 70 5b 69 5d 2c  emcpy(&zTemp[i],
1a4b4 22 2c 2e 2e 2e 22 2c 34 29 3b 0a 20 20 20 20 20  ",...",4);.     
1a4b5 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1a4b6 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1a4b7 20 20 20 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d      zTemp[i++] =
1a4b8 20 27 2c 27 3b 0a 20 20 20 20 20 20 20 20 20 20   ',';.          
1a4b9 69 66 28 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53  if( pKeyInfo->aS
1a4ba 6f 72 74 4f 72 64 65 72 20 26 26 20 70 4b 65 79  ortOrder && pKey
1a4bb 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72  Info->aSortOrder
1a4bc 5b 6a 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20  [j] ){.         
1a4bd 20 20 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20     zTemp[i++] = 
1a4be 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  '-';.          }
1a4bf 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70  .          memcp
1a4c0 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 20 70 43 6f  y(&zTemp[i], pCo
1a4c1 6c 6c 2d 3e 7a 4e 61 6d 65 2c 6e 2b 31 29 3b 0a  ll->zName,n+1);.
1a4c2 20 20 20 20 20 20 20 20 20 20 69 20 2b 3d 20 6e            i += n
1a4c3 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  ;.        }else 
1a4c4 69 66 28 20 69 2b 34 3c 6e 54 65 6d 70 2d 36 20  if( i+4<nTemp-6 
1a4c5 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d  ){.          mem
1a4c6 63 70 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c  cpy(&zTemp[i],",
1a4c7 6e 69 6c 22 2c 34 29 3b 0a 20 20 20 20 20 20 20  nil",4);.       
1a4c8 20 20 20 69 20 2b 3d 20 34 3b 0a 20 20 20 20 20     i += 4;.     
1a4c9 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1a4ca 20 20 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20     zTemp[i++] = 
1a4cb 27 29 27 3b 0a 20 20 20 20 20 20 7a 54 65 6d 70  ')';.      zTemp
1a4cc 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 61  [i] = 0;.      a
1a4cd 73 73 65 72 74 28 20 69 3c 6e 54 65 6d 70 20 29  ssert( i<nTemp )
1a4ce 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1a4cf 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34     }.    case P4
1a4d0 5f 43 4f 4c 4c 53 45 51 3a 20 7b 0a 20 20 20 20  _COLLSEQ: {.    
1a4d1 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
1a4d2 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c   = pOp->p4.pColl
1a4d3 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
1a4d4 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20  snprintf(nTemp, 
1a4d5 7a 54 65 6d 70 2c 20 22 63 6f 6c 6c 73 65 71 28  zTemp, "collseq(
1a4d6 25 2e 32 30 73 29 22 2c 20 70 43 6f 6c 6c 2d 3e  %.20s)", pColl->
1a4d7 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 62 72  zName);.      br
1a4d8 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
1a4d9 61 73 65 20 50 34 5f 46 55 4e 43 44 45 46 3a 20  ase P4_FUNCDEF: 
1a4da 7b 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66 20  {.      FuncDef 
1a4db 2a 70 44 65 66 20 3d 20 70 4f 70 2d 3e 70 34 2e  *pDef = pOp->p4.
1a4dc 70 46 75 6e 63 3b 0a 20 20 20 20 20 20 73 71 6c  pFunc;.      sql
1a4dd 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54  ite3_snprintf(nT
1a4de 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 73 28  emp, zTemp, "%s(
1a4df 25 64 29 22 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d  %d)", pDef->zNam
1a4e0 65 2c 20 70 44 65 66 2d 3e 6e 41 72 67 29 3b 0a  e, pDef->nArg);.
1a4e1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1a4e2 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49   }.    case P4_I
1a4e3 4e 54 36 34 3a 20 7b 0a 20 20 20 20 20 20 73 71  NT64: {.      sq
1a4e4 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
1a4e5 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 6c  Temp, zTemp, "%l
1a4e6 6c 64 22 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 49  ld", *pOp->p4.pI
1a4e7 36 34 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  64);.      break
1a4e8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
1a4e9 20 50 34 5f 49 4e 54 33 32 3a 20 7b 0a 20 20 20   P4_INT32: {.   
1a4ea 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1a4eb 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70  ntf(nTemp, zTemp
1a4ec 2c 20 22 25 64 22 2c 20 70 4f 70 2d 3e 70 34 2e  , "%d", pOp->p4.
1a4ed 69 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  i);.      break;
1a4ee 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1a4ef 50 34 5f 52 45 41 4c 3a 20 7b 0a 20 20 20 20 20  P4_REAL: {.     
1a4f0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
1a4f1 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20  f(nTemp, zTemp, 
1a4f2 22 25 2e 31 36 67 22 2c 20 2a 70 4f 70 2d 3e 70  "%.16g", *pOp->p
1a4f3 34 2e 70 52 65 61 6c 29 3b 0a 20 20 20 20 20 20  4.pReal);.      
1a4f4 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1a4f5 20 63 61 73 65 20 50 34 5f 4d 45 4d 3a 20 7b 0a   case P4_MEM: {.
1a4f6 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 20        Mem *pMem 
1a4f7 3d 20 70 4f 70 2d 3e 70 34 2e 70 4d 65 6d 3b 0a  = pOp->p4.pMem;.
1a4f8 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70        assert( (p
1a4f9 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
1a4fa 5f 4e 75 6c 6c 29 3d 3d 30 20 29 3b 0a 20 20 20  _Null)==0 );.   
1a4fb 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61     if( pMem->fla
1a4fc 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a  gs & MEM_Str ){.
1a4fd 20 20 20 20 20 20 20 20 7a 50 34 20 3d 20 70 4d          zP4 = pM
1a4fe 65 6d 2d 3e 7a 3b 0a 20 20 20 20 20 20 7d 65 6c  em->z;.      }el
1a4ff 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61  se if( pMem->fla
1a500 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a  gs & MEM_Int ){.
1a501 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
1a502 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20  snprintf(nTemp, 
1a503 7a 54 65 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 70  zTemp, "%lld", p
1a504 4d 65 6d 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 20  Mem->u.i);.     
1a505 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d   }else if( pMem-
1a506 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61  >flags & MEM_Rea
1a507 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  l ){.        sql
1a508 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54  ite3_snprintf(nT
1a509 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31  emp, zTemp, "%.1
1a50a 36 67 22 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0a 20  6g", pMem->r);. 
1a50b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1a50c 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
1a50d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c  ->flags & MEM_Bl
1a50e 6f 62 20 29 3b 0a 20 20 20 20 20 20 20 20 7a 50  ob );.        zP
1a50f 34 20 3d 20 22 28 62 6c 6f 62 29 22 3b 0a 20 20  4 = "(blob)";.  
1a510 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
1a511 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  k;.    }.#ifndef
1a512 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1a513 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 63 61  TUALTABLE.    ca
1a514 73 65 20 50 34 5f 56 54 41 42 3a 20 7b 0a 20 20  se P4_VTAB: {.  
1a515 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
1a516 20 2a 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70   *pVtab = pOp->p
1a517 34 2e 70 56 74 61 62 2d 3e 70 56 74 61 62 3b 0a  4.pVtab->pVtab;.
1a518 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
1a519 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54  printf(nTemp, zT
1a51a 65 6d 70 2c 20 22 76 74 61 62 3a 25 70 3a 25 70  emp, "vtab:%p:%p
1a51b 22 2c 20 70 56 74 61 62 2c 20 70 56 74 61 62 2d  ", pVtab, pVtab-
1a51c 3e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 20 20 20  >pModule);.     
1a51d 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
1a51e 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 50 34  ndif.    case P4
1a51f 5f 49 4e 54 41 52 52 41 59 3a 20 7b 0a 20 20 20  _INTARRAY: {.   
1a520 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1a521 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70  ntf(nTemp, zTemp
1a522 2c 20 22 69 6e 74 61 72 72 61 79 22 29 3b 0a 20  , "intarray");. 
1a523 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1a524 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 53 55  }.    case P4_SU
1a525 42 50 52 4f 47 52 41 4d 3a 20 7b 0a 20 20 20 20  BPROGRAM: {.    
1a526 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
1a527 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c  tf(nTemp, zTemp,
1a528 20 22 70 72 6f 67 72 61 6d 22 29 3b 0a 20 20 20   "program");.   
1a529 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1a52a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
1a52b 20 20 20 20 20 7a 50 34 20 3d 20 70 4f 70 2d 3e       zP4 = pOp->
1a52c 70 34 2e 7a 3b 0a 20 20 20 20 20 20 69 66 28 20  p4.z;.      if( 
1a52d 7a 50 34 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  zP4==0 ){.      
1a52e 20 20 7a 50 34 20 3d 20 7a 54 65 6d 70 3b 0a 20    zP4 = zTemp;. 
1a52f 20 20 20 20 20 20 20 7a 54 65 6d 70 5b 30 5d 20         zTemp[0] 
1a530 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
1a531 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28   }.  }.  assert(
1a532 20 7a 50 34 21 3d 30 20 29 3b 0a 20 20 72 65 74   zP4!=0 );.  ret
1a533 75 72 6e 20 7a 50 34 3b 0a 7d 0a 23 65 6e 64 69  urn zP4;.}.#endi
1a534 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6c 61 72 65  f../*.** Declare
1a535 20 74 6f 20 74 68 65 20 56 64 62 65 20 74 68 61   to the Vdbe tha
1a536 74 20 74 68 65 20 42 54 72 65 65 20 6f 62 6a 65  t the BTree obje
1a537 63 74 20 61 74 20 64 62 2d 3e 61 44 62 5b 69 5d  ct at db->aDb[i]
1a538 20 69 73 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c   is used..*/.SQL
1a539 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1a53a 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73   sqlite3VdbeUses
1a53b 42 74 72 65 65 28 56 64 62 65 20 2a 70 2c 20 69  Btree(Vdbe *p, i
1a53c 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 6d 61 73  nt i){.  int mas
1a53d 6b 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d  k;.  assert( i>=
1a53e 30 20 26 26 20 69 3c 70 2d 3e 64 62 2d 3e 6e 44  0 && i<p->db->nD
1a53f 62 20 26 26 20 69 3c 73 69 7a 65 6f 66 28 75 33  b && i<sizeof(u3
1a540 32 29 2a 38 20 29 3b 0a 20 20 61 73 73 65 72 74  2)*8 );.  assert
1a541 28 20 69 3c 28 69 6e 74 29 73 69 7a 65 6f 66 28  ( i<(int)sizeof(
1a542 70 2d 3e 62 74 72 65 65 4d 61 73 6b 29 2a 38 20  p->btreeMask)*8 
1a543 29 3b 0a 20 20 6d 61 73 6b 20 3d 20 28 28 75 33  );.  mask = ((u3
1a544 32 29 31 29 3c 3c 69 3b 0a 20 20 69 66 28 20 28  2)1)<<i;.  if( (
1a545 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 6d  p->btreeMask & m
1a546 61 73 6b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70  ask)==0 ){.    p
1a547 2d 3e 62 74 72 65 65 4d 61 73 6b 20 7c 3d 20 6d  ->btreeMask |= m
1a548 61 73 6b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ask;.    sqlite3
1a549 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 49  BtreeMutexArrayI
1a54a 6e 73 65 72 74 28 26 70 2d 3e 61 4d 75 74 65 78  nsert(&p->aMutex
1a54b 2c 20 70 2d 3e 64 62 2d 3e 61 44 62 5b 69 5d 2e  , p->db->aDb[i].
1a54c 70 42 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 23 69  pBt);.  }.}...#i
1a54d 66 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50  f defined(VDBE_P
1a54e 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e  ROFILE) || defin
1a54f 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
1a550 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 61 20 73  ./*.** Print a s
1a551 69 6e 67 6c 65 20 6f 70 63 6f 64 65 2e 20 20 54  ingle opcode.  T
1a552 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75  his routine is u
1a553 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e  sed for debuggin
1a554 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54  g only..*/.SQLIT
1a555 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1a556 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f  qlite3VdbePrintO
1a557 70 28 46 49 4c 45 20 2a 70 4f 75 74 2c 20 69 6e  p(FILE *pOut, in
1a558 74 20 70 63 2c 20 4f 70 20 2a 70 4f 70 29 7b 0a  t pc, Op *pOp){.
1a559 20 20 63 68 61 72 20 2a 7a 50 34 3b 0a 20 20 63    char *zP4;.  c
1a55a 68 61 72 20 7a 50 74 72 5b 35 30 5d 3b 0a 20 20  har zPtr[50];.  
1a55b 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
1a55c 72 20 2a 7a 46 6f 72 6d 61 74 31 20 3d 20 22 25  r *zFormat1 = "%
1a55d 34 64 20 25 2d 31 33 73 20 25 34 64 20 25 34 64  4d %-13s %4d %4d
1a55e 20 25 34 64 20 25 2d 34 73 20 25 2e 32 58 20 25   %4d %-4s %.2X %
1a55f 73 5c 6e 22 3b 0a 20 20 69 66 28 20 70 4f 75 74  s\n";.  if( pOut
1a560 3d 3d 30 20 29 20 70 4f 75 74 20 3d 20 73 74 64  ==0 ) pOut = std
1a561 6f 75 74 3b 0a 20 20 7a 50 34 20 3d 20 64 69 73  out;.  zP4 = dis
1a562 70 6c 61 79 50 34 28 70 4f 70 2c 20 7a 50 74 72  playP4(pOp, zPtr
1a563 2c 20 73 69 7a 65 6f 66 28 7a 50 74 72 29 29 3b  , sizeof(zPtr));
1a564 0a 20 20 66 70 72 69 6e 74 66 28 70 4f 75 74 2c  .  fprintf(pOut,
1a565 20 7a 46 6f 72 6d 61 74 31 2c 20 70 63 2c 20 0a   zFormat1, pc, .
1a566 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 70 63        sqlite3Opc
1a567 6f 64 65 4e 61 6d 65 28 70 4f 70 2d 3e 6f 70 63  odeName(pOp->opc
1a568 6f 64 65 29 2c 20 70 4f 70 2d 3e 70 31 2c 20 70  ode), pOp->p1, p
1a569 4f 70 2d 3e 70 32 2c 20 70 4f 70 2d 3e 70 33 2c  Op->p2, pOp->p3,
1a56a 20 7a 50 34 2c 20 70 4f 70 2d 3e 70 35 2c 0a 23   zP4, pOp->p5,.#
1a56b 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
1a56c 55 47 0a 20 20 20 20 20 20 70 4f 70 2d 3e 7a 43  UG.      pOp->zC
1a56d 6f 6d 6d 65 6e 74 20 3f 20 70 4f 70 2d 3e 7a 43  omment ? pOp->zC
1a56e 6f 6d 6d 65 6e 74 20 3a 20 22 22 0a 23 65 6c 73  omment : "".#els
1a56f 65 0a 20 20 20 20 20 20 22 22 0a 23 65 6e 64 69  e.      "".#endi
1a570 66 0a 20 20 29 3b 0a 20 20 66 66 6c 75 73 68 28  f.  );.  fflush(
1a571 70 4f 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  pOut);.}.#endif.
1a572 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61  ./*.** Release a
1a573 6e 20 61 72 72 61 79 20 6f 66 20 4e 20 4d 65 6d  n array of N Mem
1a574 20 65 6c 65 6d 65 6e 74 73 0a 2a 2f 0a 73 74 61   elements.*/.sta
1a575 74 69 63 20 76 6f 69 64 20 72 65 6c 65 61 73 65  tic void release
1a576 4d 65 6d 41 72 72 61 79 28 4d 65 6d 20 2a 70 2c  MemArray(Mem *p,
1a577 20 69 6e 74 20 4e 29 7b 0a 20 20 69 66 28 20 70   int N){.  if( p
1a578 20 26 26 20 4e 20 29 7b 0a 20 20 20 20 4d 65 6d   && N ){.    Mem
1a579 20 2a 70 45 6e 64 3b 0a 20 20 20 20 73 71 6c 69   *pEnd;.    sqli
1a57a 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b  te3 *db = p->db;
1a57b 0a 20 20 20 20 75 38 20 6d 61 6c 6c 6f 63 5f 66  .    u8 malloc_f
1a57c 61 69 6c 65 64 20 3d 20 64 62 2d 3e 6d 61 6c 6c  ailed = db->mall
1a57d 6f 63 46 61 69 6c 65 64 3b 0a 20 20 20 20 66 6f  ocFailed;.    fo
1a57e 72 28 70 45 6e 64 3d 26 70 5b 4e 5d 3b 20 70 3c  r(pEnd=&p[N]; p<
1a57f 70 45 6e 64 3b 20 70 2b 2b 29 7b 0a 20 20 20 20  pEnd; p++){.    
1a580 20 20 61 73 73 65 72 74 28 20 28 26 70 5b 31 5d    assert( (&p[1]
1a581 29 3d 3d 70 45 6e 64 20 7c 7c 20 70 5b 30 5d 2e  )==pEnd || p[0].
1a582 64 62 3d 3d 70 5b 31 5d 2e 64 62 20 29 3b 0a 0a  db==p[1].db );..
1a583 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c        /* This bl
1a584 6f 63 6b 20 69 73 20 72 65 61 6c 6c 79 20 61 6e  ock is really an
1a585 20 69 6e 6c 69 6e 65 64 20 76 65 72 73 69 6f 6e   inlined version
1a586 20 6f 66 20 73 71 6c 69 74 65 33 56 64 62 65 4d   of sqlite3VdbeM
1a587 65 6d 52 65 6c 65 61 73 65 28 29 0a 20 20 20 20  emRelease().    
1a588 20 20 2a 2a 20 74 68 61 74 20 74 61 6b 65 73 20    ** that takes 
1a589 61 64 76 61 6e 74 61 67 65 20 6f 66 20 74 68 65  advantage of the
1a58a 20 66 61 63 74 20 74 68 61 74 20 74 68 65 20 6d   fact that the m
1a58b 65 6d 6f 72 79 20 63 65 6c 6c 20 76 61 6c 75 65  emory cell value
1a58c 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20 62 65   is .      ** be
1a58d 69 6e 67 20 73 65 74 20 74 6f 20 4e 55 4c 4c 20  ing set to NULL 
1a58e 61 66 74 65 72 20 72 65 6c 65 61 73 69 6e 67 20  after releasing 
1a58f 61 6e 79 20 64 79 6e 61 6d 69 63 20 72 65 73 6f  any dynamic reso
1a590 75 72 63 65 73 2e 0a 20 20 20 20 20 20 2a 2a 0a  urces..      **.
1a591 20 20 20 20 20 20 2a 2a 20 54 68 65 20 6a 75 73        ** The jus
1a592 74 69 66 69 63 61 74 69 6f 6e 20 66 6f 72 20 64  tification for d
1a593 75 70 6c 69 63 61 74 69 6e 67 20 63 6f 64 65 20  uplicating code 
1a594 69 73 20 74 68 61 74 20 61 63 63 6f 72 64 69 6e  is that accordin
1a595 67 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 20 63  g to .      ** c
1a596 61 6c 6c 67 72 69 6e 64 2c 20 74 68 69 73 20 63  allgrind, this c
1a597 61 75 73 65 73 20 61 20 63 65 72 74 61 69 6e 20  auses a certain 
1a598 74 65 73 74 20 63 61 73 65 20 74 6f 20 68 69 74  test case to hit
1a599 20 74 68 65 20 43 50 55 20 34 2e 37 20 0a 20 20   the CPU 4.7 .  
1a59a 20 20 20 20 2a 2a 20 70 65 72 63 65 6e 74 20 6c      ** percent l
1a59b 65 73 73 20 28 78 38 36 20 6c 69 6e 75 78 2c 20  ess (x86 linux, 
1a59c 67 63 63 20 76 65 72 73 69 6f 6e 20 34 2e 31 2e  gcc version 4.1.
1a59d 32 2c 20 2d 4f 36 29 20 74 68 61 6e 20 69 66 20  2, -O6) than if 
1a59e 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  .      ** sqlite
1a59f 33 4d 65 6d 52 65 6c 65 61 73 65 28 29 20 77 65  3MemRelease() we
1a5a0 72 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 68  re called from h
1a5a1 65 72 65 2e 20 57 69 74 68 20 2d 4f 32 2c 20 74  ere. With -O2, t
1a5a2 68 69 73 20 6a 75 6d 70 73 0a 20 20 20 20 20 20  his jumps.      
1a5a3 2a 2a 20 74 6f 20 36 2e 36 20 70 65 72 63 65 6e  ** to 6.6 percen
1a5a4 74 2e 20 54 68 65 20 74 65 73 74 20 63 61 73 65  t. The test case
1a5a5 20 69 73 20 69 6e 73 65 72 74 69 6e 67 20 31 30   is inserting 10
1a5a6 30 30 20 72 6f 77 73 20 69 6e 74 6f 20 61 20 74  00 rows into a t
1a5a7 61 62 6c 65 20 0a 20 20 20 20 20 20 2a 2a 20 77  able .      ** w
1a5a8 69 74 68 20 6e 6f 20 69 6e 64 65 78 65 73 20 75  ith no indexes u
1a5a9 73 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 70 72  sing a single pr
1a5aa 65 70 61 72 65 64 20 49 4e 53 45 52 54 20 73 74  epared INSERT st
1a5ab 61 74 65 6d 65 6e 74 2c 20 62 69 6e 64 28 29 20  atement, bind() 
1a5ac 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 72 65  .      ** and re
1a5ad 73 65 74 28 29 2e 20 49 6e 73 65 72 74 73 20 61  set(). Inserts a
1a5ae 72 65 20 67 72 6f 75 70 65 64 20 69 6e 74 6f 20  re grouped into 
1a5af 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20  a transaction.. 
1a5b0 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
1a5b1 28 20 70 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f  ( p->flags&(MEM_
1a5b2 41 67 67 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f  Agg|MEM_Dyn|MEM_
1a5b3 46 72 61 6d 65 7c 4d 45 4d 5f 52 6f 77 53 65 74  Frame|MEM_RowSet
1a5b4 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  ) ){.        sql
1a5b5 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
1a5b6 73 65 28 70 29 3b 0a 20 20 20 20 20 20 7d 65 6c  se(p);.      }el
1a5b7 73 65 20 69 66 28 20 70 2d 3e 7a 4d 61 6c 6c 6f  se if( p->zMallo
1a5b8 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  c ){.        sql
1a5b9 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1a5ba 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20  ->zMalloc);.    
1a5bb 20 20 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d      p->zMalloc =
1a5bc 20 30 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20   0;.      }..   
1a5bd 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d 45     p->flags = ME
1a5be 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20  M_Null;.    }.  
1a5bf 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
1a5c0 65 64 20 3d 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c  ed = malloc_fail
1a5c1 65 64 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ed;.  }.}../*.**
1a5c2 20 44 65 6c 65 74 65 20 61 20 56 64 62 65 46 72   Delete a VdbeFr
1a5c3 61 6d 65 20 6f 62 6a 65 63 74 20 61 6e 64 20 69  ame object and i
1a5c4 74 73 20 63 6f 6e 74 65 6e 74 73 2e 20 56 64 62  ts contents. Vdb
1a5c5 65 46 72 61 6d 65 20 6f 62 6a 65 63 74 73 20 61  eFrame objects a
1a5c6 72 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20  re.** allocated 
1a5c7 62 79 20 74 68 65 20 4f 50 5f 50 72 6f 67 72 61  by the OP_Progra
1a5c8 6d 20 6f 70 63 6f 64 65 20 69 6e 20 73 71 6c 69  m opcode in sqli
1a5c9 74 65 33 56 64 62 65 45 78 65 63 28 29 2e 0a 2a  te3VdbeExec()..*
1a5ca 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1a5cb 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
1a5cc 65 46 72 61 6d 65 44 65 6c 65 74 65 28 56 64 62  eFrameDelete(Vdb
1a5cd 65 46 72 61 6d 65 20 2a 70 29 7b 0a 20 20 69 6e  eFrame *p){.  in
1a5ce 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d  t i;.  Mem *aMem
1a5cf 20 3d 20 56 64 62 65 46 72 61 6d 65 4d 65 6d 28   = VdbeFrameMem(
1a5d0 70 29 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72  p);.  VdbeCursor
1a5d1 20 2a 2a 61 70 43 73 72 20 3d 20 28 56 64 62 65   **apCsr = (Vdbe
1a5d2 43 75 72 73 6f 72 20 2a 2a 29 26 61 4d 65 6d 5b  Cursor **)&aMem[
1a5d3 70 2d 3e 6e 43 68 69 6c 64 4d 65 6d 5d 3b 0a 20  p->nChildMem];. 
1a5d4 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e   for(i=0; i<p->n
1a5d5 43 68 69 6c 64 43 73 72 3b 20 69 2b 2b 29 7b 0a  ChildCsr; i++){.
1a5d6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 46      sqlite3VdbeF
1a5d7 72 65 65 43 75 72 73 6f 72 28 70 2d 3e 76 2c 20  reeCursor(p->v, 
1a5d8 61 70 43 73 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20  apCsr[i]);.  }. 
1a5d9 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79   releaseMemArray
1a5da 28 61 4d 65 6d 2c 20 70 2d 3e 6e 43 68 69 6c 64  (aMem, p->nChild
1a5db 4d 65 6d 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  Mem);.  sqlite3D
1a5dc 62 46 72 65 65 28 70 2d 3e 76 2d 3e 64 62 2c 20  bFree(p->v->db, 
1a5dd 70 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  p);.}..#ifndef S
1a5de 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41  QLITE_OMIT_EXPLA
1a5df 49 4e 0a 2f 2a 0a 2a 2a 20 47 69 76 65 20 61 20  IN./*.** Give a 
1a5e0 6c 69 73 74 69 6e 67 20 6f 66 20 74 68 65 20 70  listing of the p
1a5e1 72 6f 67 72 61 6d 20 69 6e 20 74 68 65 20 76 69  rogram in the vi
1a5e2 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a  rtual machine..*
1a5e3 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61  *.** The interfa
1a5e4 63 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61  ce is the same a
1a5e5 73 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65  s sqlite3VdbeExe
1a5e6 63 28 29 2e 20 20 42 75 74 20 69 6e 73 74 65 61  c().  But instea
1a5e7 64 20 6f 66 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20  d of.** running 
1a5e8 74 68 65 20 63 6f 64 65 2c 20 69 74 20 69 6e 76  the code, it inv
1a5e9 6f 6b 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63  okes the callbac
1a5ea 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20  k once for each 
1a5eb 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20  instruction..** 
1a5ec 54 68 69 73 20 66 65 61 74 75 72 65 20 69 73 20  This feature is 
1a5ed 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
1a5ee 74 20 22 45 58 50 4c 41 49 4e 22 2e 0a 2a 2a 0a  t "EXPLAIN"..**.
1a5ef 2a 2a 20 57 68 65 6e 20 70 2d 3e 65 78 70 6c 61  ** When p->expla
1a5f0 69 6e 3d 3d 31 2c 20 65 61 63 68 20 69 6e 73 74  in==1, each inst
1a5f1 72 75 63 74 69 6f 6e 20 69 73 20 6c 69 73 74 65  ruction is liste
1a5f2 64 2e 20 20 57 68 65 6e 0a 2a 2a 20 70 2d 3e 65  d.  When.** p->e
1a5f3 78 70 6c 61 69 6e 3d 3d 32 2c 20 6f 6e 6c 79 20  xplain==2, only 
1a5f4 4f 50 5f 45 78 70 6c 61 69 6e 20 69 6e 73 74 72  OP_Explain instr
1a5f5 75 63 74 69 6f 6e 73 20 61 72 65 20 6c 69 73 74  uctions are list
1a5f6 65 64 20 61 6e 64 20 74 68 65 73 65 0a 2a 2a 20  ed and these.** 
1a5f7 61 72 65 20 73 68 6f 77 6e 20 69 6e 20 61 20 64  are shown in a d
1a5f8 69 66 66 65 72 65 6e 74 20 66 6f 72 6d 61 74 2e  ifferent format.
1a5f9 20 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20    p->explain==2 
1a5fa 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65  is used to imple
1a5fb 6d 65 6e 74 0a 2a 2a 20 45 58 50 4c 41 49 4e 20  ment.** EXPLAIN 
1a5fc 51 55 45 52 59 20 50 4c 41 4e 2e 0a 2a 2f 0a 53  QUERY PLAN..*/.S
1a5fd 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1a5fe 74 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73  t sqlite3VdbeLis
1a5ff 74 28 0a 20 20 56 64 62 65 20 2a 70 20 20 20 20  t(.  Vdbe *p    
1a600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a601 2a 20 54 68 65 20 56 44 42 45 20 2a 2f 0a 29 7b  * The VDBE */.){
1a602 0a 20 20 69 6e 74 20 6e 52 6f 77 3b 20 20 20 20  .  int nRow;    
1a603 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a604 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c          /* Total
1a605 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
1a606 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 69  to return */.  i
1a607 6e 74 20 6e 53 75 62 20 3d 20 30 3b 20 20 20 20  nt nSub = 0;    
1a608 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a609 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1a60a 20 73 75 62 2d 76 64 62 65 73 20 73 65 65 6e 20   sub-vdbes seen 
1a60b 73 6f 20 66 61 72 20 2a 2f 0a 20 20 53 75 62 50  so far */.  SubP
1a60c 72 6f 67 72 61 6d 20 2a 2a 61 70 53 75 62 20 3d  rogram **apSub =
1a60d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1a60e 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 73 75 62   /* Array of sub
1a60f 2d 76 64 62 65 73 20 2a 2f 0a 20 20 4d 65 6d 20  -vdbes */.  Mem 
1a610 2a 70 53 75 62 20 3d 20 30 3b 0a 20 20 73 71 6c  *pSub = 0;.  sql
1a611 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62  ite3 *db = p->db
1a612 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  ;.  int i;.  int
1a613 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1a614 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 70  .  Mem *pMem = p
1a615 2d 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 26  ->pResultSet = &
1a616 70 2d 3e 61 4d 65 6d 5b 31 5d 3b 0a 0a 20 20 61  p->aMem[1];..  a
1a617 73 73 65 72 74 28 20 70 2d 3e 65 78 70 6c 61 69  ssert( p->explai
1a618 6e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  n );.  assert( p
1a619 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41  ->magic==VDBE_MA
1a61a 47 49 43 5f 52 55 4e 20 29 3b 0a 20 20 61 73 73  GIC_RUN );.  ass
1a61b 65 72 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d  ert( db->magic==
1a61c 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53  SQLITE_MAGIC_BUS
1a61d 59 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  Y );.  assert( p
1a61e 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc==SQLITE_OK 
1a61f 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45  || p->rc==SQLITE
1a620 5f 42 55 53 59 20 7c 7c 20 70 2d 3e 72 63 3d 3d  _BUSY || p->rc==
1a621 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a  SQLITE_NOMEM );.
1a622 0a 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67  .  /* Even thoug
1a623 68 20 74 68 69 73 20 6f 70 63 6f 64 65 20 64 6f  h this opcode do
1a624 65 73 20 6e 6f 74 20 75 73 65 20 64 79 6e 61 6d  es not use dynam
1a625 69 63 20 73 74 72 69 6e 67 73 20 66 6f 72 0a 20  ic strings for. 
1a626 20 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2c 20   ** the result, 
1a627 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 6d  result columns m
1a628 61 79 20 62 65 63 6f 6d 65 20 64 79 6e 61 6d 69  ay become dynami
1a629 63 20 69 66 20 74 68 65 20 75 73 65 72 20 63 61  c if the user ca
1a62a 6c 6c 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  lls.  ** sqlite3
1a62b 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29  _column_text16()
1a62c 2c 20 63 61 75 73 69 6e 67 20 61 20 74 72 61 6e  , causing a tran
1a62d 73 6c 61 74 69 6f 6e 20 74 6f 20 55 54 46 2d 31  slation to UTF-1
1a62e 36 20 65 6e 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f  6 encoding..  */
1a62f 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72  .  releaseMemArr
1a630 61 79 28 70 4d 65 6d 2c 20 38 29 3b 0a 0a 20 20  ay(pMem, 8);..  
1a631 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54  if( p->rc==SQLIT
1a632 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 2f  E_NOMEM ){.    /
1a633 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 69  * This happens i
1a634 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 69 6e 73  f a malloc() ins
1a635 69 64 65 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  ide a call to sq
1a636 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
1a637 74 28 29 20 6f 72 0a 20 20 20 20 2a 2a 20 73 71  t() or.    ** sq
1a638 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
1a639 74 31 36 28 29 20 66 61 69 6c 65 64 2e 20 20 2a  t16() failed.  *
1a63a 2f 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63  /.    db->malloc
1a63b 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
1a63c 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
1a63d 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46  ROR;.  }..  /* F
1a63e 69 67 75 72 65 20 6f 75 74 20 74 6f 74 61 6c 20  igure out total 
1a63f 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74  number of rows t
1a640 68 61 74 20 77 69 6c 6c 20 62 65 20 72 65 74 75  hat will be retu
1a641 72 6e 65 64 20 62 79 20 74 68 69 73 20 0a 20 20  rned by this .  
1a642 2a 2a 20 45 58 50 4c 41 49 4e 20 70 72 6f 67 72  ** EXPLAIN progr
1a643 61 6d 2e 20 20 2a 2f 0a 20 20 6e 52 6f 77 20 3d  am.  */.  nRow =
1a644 20 70 2d 3e 6e 4f 70 3b 0a 20 20 69 66 28 20 70   p->nOp;.  if( p
1a645 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31 20 29 7b 0a  ->explain==1 ){.
1a646 20 20 20 20 70 53 75 62 20 3d 20 26 70 2d 3e 61      pSub = &p->a
1a647 4d 65 6d 5b 39 5d 3b 0a 20 20 20 20 69 66 28 20  Mem[9];.    if( 
1a648 70 53 75 62 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pSub->flags&MEM_
1a649 42 6c 6f 62 20 29 7b 0a 20 20 20 20 20 20 6e 53  Blob ){.      nS
1a64a 75 62 20 3d 20 70 53 75 62 2d 3e 6e 2f 73 69 7a  ub = pSub->n/siz
1a64b 65 6f 66 28 56 64 62 65 2a 29 3b 0a 20 20 20 20  eof(Vdbe*);.    
1a64c 20 20 61 70 53 75 62 20 3d 20 28 53 75 62 50 72    apSub = (SubPr
1a64d 6f 67 72 61 6d 20 2a 2a 29 70 53 75 62 2d 3e 7a  ogram **)pSub->z
1a64e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  ;.    }.    for(
1a64f 69 3d 30 3b 20 69 3c 6e 53 75 62 3b 20 69 2b 2b  i=0; i<nSub; i++
1a650 29 7b 0a 20 20 20 20 20 20 6e 52 6f 77 20 2b 3d  ){.      nRow +=
1a651 20 61 70 53 75 62 5b 69 5d 2d 3e 6e 4f 70 3b 0a   apSub[i]->nOp;.
1a652 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 64 6f 7b      }.  }..  do{
1a653 0a 20 20 20 20 69 20 3d 20 70 2d 3e 70 63 2b 2b  .    i = p->pc++
1a654 3b 0a 20 20 7d 77 68 69 6c 65 28 20 69 3c 6e 52  ;.  }while( i<nR
1a655 6f 77 20 26 26 20 70 2d 3e 65 78 70 6c 61 69 6e  ow && p->explain
1a656 3d 3d 32 20 26 26 20 70 2d 3e 61 4f 70 5b 69 5d  ==2 && p->aOp[i]
1a657 2e 6f 70 63 6f 64 65 21 3d 4f 50 5f 45 78 70 6c  .opcode!=OP_Expl
1a658 61 69 6e 20 29 3b 0a 20 20 69 66 28 20 69 3e 3d  ain );.  if( i>=
1a659 6e 52 6f 77 20 29 7b 0a 20 20 20 20 70 2d 3e 72  nRow ){.    p->r
1a65a 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1a65b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 44     rc = SQLITE_D
1a65c 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ONE;.  }else if(
1a65d 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72   db->u1.isInterr
1a65e 75 70 74 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e  upted ){.    p->
1a65f 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45  rc = SQLITE_INTE
1a660 52 52 55 50 54 3b 0a 20 20 20 20 72 63 20 3d 20  RRUPT;.    rc = 
1a661 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1a662 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
1a663 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
1a664 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65  db, "%s", sqlite
1a665 33 45 72 72 53 74 72 28 70 2d 3e 72 63 29 29 3b  3ErrStr(p->rc));
1a666 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68  .  }else{.    ch
1a667 61 72 20 2a 7a 3b 0a 20 20 20 20 4f 70 20 2a 70  ar *z;.    Op *p
1a668 4f 70 3b 0a 20 20 20 20 69 66 28 20 69 3c 70 2d  Op;.    if( i<p-
1a669 3e 6e 4f 70 20 29 7b 0a 20 20 20 20 20 20 70 4f  >nOp ){.      pO
1a66a 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a  p = &p->aOp[i];.
1a66b 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1a66c 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 69 20   int j;.      i 
1a66d 2d 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 20 20 20  -= p->nOp;.     
1a66e 20 66 6f 72 28 6a 3d 30 3b 20 69 3e 3d 61 70 53   for(j=0; i>=apS
1a66f 75 62 5b 6a 5d 2d 3e 6e 4f 70 3b 20 6a 2b 2b 29  ub[j]->nOp; j++)
1a670 7b 0a 20 20 20 20 20 20 20 20 69 20 2d 3d 20 61  {.        i -= a
1a671 70 53 75 62 5b 6a 5d 2d 3e 6e 4f 70 3b 0a 20 20  pSub[j]->nOp;.  
1a672 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 20      }.      pOp 
1a673 3d 20 26 61 70 53 75 62 5b 6a 5d 2d 3e 61 4f 70  = &apSub[j]->aOp
1a674 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  [i];.    }.    i
1a675 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 31  f( p->explain==1
1a676 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e   ){.      pMem->
1a677 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
1a678 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70  .      pMem->typ
1a679 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47  e = SQLITE_INTEG
1a67a 45 52 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  ER;.      pMem->
1a67b 75 2e 69 20 3d 20 69 3b 20 20 20 20 20 20 20 20  u.i = i;        
1a67c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a67d 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72          /* Progr
1a67e 61 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  am counter */.  
1a67f 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 0a 20      pMem++;.  . 
1a680 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
1a681 20 3d 20 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45   = MEM_Static|ME
1a682 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a  M_Str|MEM_Term;.
1a683 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20 3d 20        pMem->z = 
1a684 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33 4f 70  (char*)sqlite3Op
1a685 63 6f 64 65 4e 61 6d 65 28 70 4f 70 2d 3e 6f 70  codeName(pOp->op
1a686 63 6f 64 65 29 3b 20 20 2f 2a 20 4f 70 63 6f 64  code);  /* Opcod
1a687 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72  e */.      asser
1a688 74 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b  t( pMem->z!=0 );
1a689 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d  .      pMem->n =
1a68a 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
1a68b 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20  (pMem->z);.     
1a68c 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
1a68d 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20 20  LITE_TEXT;.     
1a68e 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c   pMem->enc = SQL
1a68f 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20  ITE_UTF8;.      
1a690 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 20 20 69  pMem++;..      i
1a691 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d  f( pOp->p4type==
1a692 50 34 5f 53 55 42 50 52 4f 47 52 41 4d 20 29 7b  P4_SUBPROGRAM ){
1a693 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 42 79  .        int nBy
1a694 74 65 20 3d 20 28 6e 53 75 62 2b 31 29 2a 73 69  te = (nSub+1)*si
1a695 7a 65 6f 66 28 53 75 62 50 72 6f 67 72 61 6d 2a  zeof(SubProgram*
1a696 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a  );.        int j
1a697 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  ;.        for(j=
1a698 30 3b 20 6a 3c 6e 53 75 62 3b 20 6a 2b 2b 29 7b  0; j<nSub; j++){
1a699 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61  .          if( a
1a69a 70 53 75 62 5b 6a 5d 3d 3d 70 4f 70 2d 3e 70 34  pSub[j]==pOp->p4
1a69b 2e 70 50 72 6f 67 72 61 6d 20 29 20 62 72 65 61  .pProgram ) brea
1a69c 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
1a69d 20 20 20 20 20 69 66 28 20 6a 3d 3d 6e 53 75 62       if( j==nSub
1a69e 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73   && SQLITE_OK==s
1a69f 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f  qlite3VdbeMemGro
1a6a0 77 28 70 53 75 62 2c 20 6e 42 79 74 65 2c 20 31  w(pSub, nByte, 1
1a6a1 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  ) ){.          a
1a6a2 70 53 75 62 20 3d 20 28 53 75 62 50 72 6f 67 72  pSub = (SubProgr
1a6a3 61 6d 20 2a 2a 29 70 53 75 62 2d 3e 7a 3b 0a 20  am **)pSub->z;. 
1a6a4 20 20 20 20 20 20 20 20 20 61 70 53 75 62 5b 6e           apSub[n
1a6a5 53 75 62 2b 2b 5d 20 3d 20 70 4f 70 2d 3e 70 34  Sub++] = pOp->p4
1a6a6 2e 70 50 72 6f 67 72 61 6d 3b 0a 20 20 20 20 20  .pProgram;.     
1a6a7 20 20 20 20 20 70 53 75 62 2d 3e 66 6c 61 67 73       pSub->flags
1a6a8 20 7c 3d 20 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20   |= MEM_Blob;.  
1a6a9 20 20 20 20 20 20 20 20 70 53 75 62 2d 3e 6e 20          pSub->n 
1a6aa 3d 20 6e 53 75 62 2a 73 69 7a 65 6f 66 28 53 75  = nSub*sizeof(Su
1a6ab 62 50 72 6f 67 72 61 6d 2a 29 3b 0a 20 20 20 20  bProgram*);.    
1a6ac 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
1a6ad 20 20 7d 0a 0a 20 20 20 20 70 4d 65 6d 2d 3e 66    }..    pMem->f
1a6ae 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
1a6af 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20      pMem->u.i = 
1a6b0 70 4f 70 2d 3e 70 31 3b 20 20 20 20 20 20 20 20  pOp->p1;        
1a6b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a6b2 20 20 2f 2a 20 50 31 20 2a 2f 0a 20 20 20 20 70    /* P1 */.    p
1a6b3 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
1a6b4 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20  TE_INTEGER;.    
1a6b5 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20 70 4d 65  pMem++;..    pMe
1a6b6 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49  m->flags = MEM_I
1a6b7 6e 74 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e  nt;.    pMem->u.
1a6b8 69 20 3d 20 70 4f 70 2d 3e 70 32 3b 20 20 20 20  i = pOp->p2;    
1a6b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a6ba 20 20 20 20 20 20 2f 2a 20 50 32 20 2a 2f 0a 20        /* P2 */. 
1a6bb 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
1a6bc 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a  SQLITE_INTEGER;.
1a6bd 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20      pMem++;..   
1a6be 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d   if( p->explain=
1a6bf 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d  =1 ){.      pMem
1a6c0 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
1a6c1 74 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75  t;.      pMem->u
1a6c2 2e 69 20 3d 20 70 4f 70 2d 3e 70 33 3b 20 20 20  .i = pOp->p3;   
1a6c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a6c4 20 20 20 20 20 20 20 2f 2a 20 50 33 20 2a 2f 0a         /* P3 */.
1a6c5 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65        pMem->type
1a6c6 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45   = SQLITE_INTEGE
1a6c7 52 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b  R;.      pMem++;
1a6c8 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
1a6c9 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72  sqlite3VdbeMemGr
1a6ca 6f 77 28 70 4d 65 6d 2c 20 33 32 2c 20 30 29 20  ow(pMem, 32, 0) 
1a6cb 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ){            /*
1a6cc 20 50 34 20 2a 2f 0a 20 20 20 20 20 20 61 73 73   P4 */.      ass
1a6cd 65 72 74 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c  ert( p->db->mall
1a6ce 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20  ocFailed );.    
1a6cf 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1a6d0 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20  ERROR;.    }.   
1a6d1 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
1a6d2 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d  EM_Dyn|MEM_Str|M
1a6d3 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 7a 20 3d  EM_Term;.    z =
1a6d4 20 64 69 73 70 6c 61 79 50 34 28 70 4f 70 2c 20   displayP4(pOp, 
1a6d5 70 4d 65 6d 2d 3e 7a 2c 20 33 32 29 3b 0a 20 20  pMem->z, 32);.  
1a6d6 20 20 69 66 28 20 7a 21 3d 70 4d 65 6d 2d 3e 7a    if( z!=pMem->z
1a6d7 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1a6d8 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70  3VdbeMemSetStr(p
1a6d9 4d 65 6d 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49  Mem, z, -1, SQLI
1a6da 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20 20  TE_UTF8, 0);.   
1a6db 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73   }else{.      as
1a6dc 73 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30  sert( pMem->z!=0
1a6dd 20 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e   );.      pMem->
1a6de 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  n = sqlite3Strle
1a6df 6e 33 30 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20  n30(pMem->z);.  
1a6e0 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20      pMem->enc = 
1a6e1 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20  SQLITE_UTF8;.   
1a6e2 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70   }.    pMem->typ
1a6e3 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b  e = SQLITE_TEXT;
1a6e4 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20  .    pMem++;..  
1a6e5 20 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e    if( p->explain
1a6e6 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ==1 ){.      if(
1a6e7 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 47   sqlite3VdbeMemG
1a6e8 72 6f 77 28 70 4d 65 6d 2c 20 34 2c 20 30 29 20  row(pMem, 4, 0) 
1a6e9 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ){.        asser
1a6ea 74 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  t( p->db->malloc
1a6eb 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20  Failed );.      
1a6ec 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1a6ed 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20  ERROR;.      }. 
1a6ee 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
1a6ef 20 3d 20 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53   = MEM_Dyn|MEM_S
1a6f0 74 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20  tr|MEM_Term;.   
1a6f1 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 32 3b 0a     pMem->n = 2;.
1a6f2 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
1a6f3 70 72 69 6e 74 66 28 33 2c 20 70 4d 65 6d 2d 3e  printf(3, pMem->
1a6f4 7a 2c 20 22 25 2e 32 78 22 2c 20 70 4f 70 2d 3e  z, "%.2x", pOp->
1a6f5 70 35 29 3b 20 20 20 2f 2a 20 50 35 20 2a 2f 0a  p5);   /* P5 */.
1a6f6 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65        pMem->type
1a6f7 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a   = SQLITE_TEXT;.
1a6f8 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20        pMem->enc 
1a6f9 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20  = SQLITE_UTF8;. 
1a6fa 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 0a       pMem++;.  .
1a6fb 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
1a6fc 42 55 47 0a 20 20 20 20 20 20 69 66 28 20 70 4f  BUG.      if( pO
1a6fd 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 29 7b 0a 20  p->zComment ){. 
1a6fe 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61         pMem->fla
1a6ff 67 73 20 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d  gs = MEM_Str|MEM
1a700 5f 54 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 70  _Term;.        p
1a701 4d 65 6d 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 7a 43  Mem->z = pOp->zC
1a702 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 20 20 20 20  omment;.        
1a703 70 4d 65 6d 2d 3e 6e 20 3d 20 73 71 6c 69 74 65  pMem->n = sqlite
1a704 33 53 74 72 6c 65 6e 33 30 28 70 4d 65 6d 2d 3e  3Strlen30(pMem->
1a705 7a 29 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d  z);.        pMem
1a706 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55  ->enc = SQLITE_U
1a707 54 46 38 3b 0a 20 20 20 20 20 20 20 20 70 4d 65  TF8;.        pMe
1a708 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45  m->type = SQLITE
1a709 5f 54 45 58 54 3b 0a 20 20 20 20 20 20 7d 65 6c  _TEXT;.      }el
1a70a 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  se.#endif.      
1a70b 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e  {.        pMem->
1a70c 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
1a70d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1a70e 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 6d 65          /* Comme
1a70f 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4d  nt */.        pM
1a710 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54  em->type = SQLIT
1a711 45 5f 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a  E_NULL;.      }.
1a712 20 20 20 20 7d 0a 0a 20 20 20 20 70 2d 3e 6e 52      }..    p->nR
1a713 65 73 43 6f 6c 75 6d 6e 20 3d 20 38 20 2d 20 35  esColumn = 8 - 5
1a714 2a 28 70 2d 3e 65 78 70 6c 61 69 6e 2d 31 29 3b  *(p->explain-1);
1a715 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c  .    p->rc = SQL
1a716 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72 63 20 3d  ITE_OK;.    rc =
1a717 20 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 7d   SQLITE_ROW;.  }
1a718 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1a719 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1a71a 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f  _OMIT_EXPLAIN */
1a71b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
1a71c 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e  DEBUG./*.** Prin
1a71d 74 20 74 68 65 20 53 51 4c 20 74 68 61 74 20 77  t the SQL that w
1a71e 61 73 20 75 73 65 64 20 74 6f 20 67 65 6e 65 72  as used to gener
1a71f 61 74 65 20 61 20 56 44 42 45 20 70 72 6f 67 72  ate a VDBE progr
1a720 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  am..*/.SQLITE_PR
1a721 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1a722 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 56  e3VdbePrintSql(V
1a723 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e  dbe *p){.  int n
1a724 4f 70 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 56  Op = p->nOp;.  V
1a725 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20 69 66  dbeOp *pOp;.  if
1a726 28 20 6e 4f 70 3c 31 20 29 20 72 65 74 75 72 6e  ( nOp<1 ) return
1a727 3b 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f  ;.  pOp = &p->aO
1a728 70 5b 30 5d 3b 0a 20 20 69 66 28 20 70 4f 70 2d  p[0];.  if( pOp-
1a729 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 63  >opcode==OP_Trac
1a72a 65 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d  e && pOp->p4.z!=
1a72b 30 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  0 ){.    const c
1a72c 68 61 72 20 2a 7a 20 3d 20 70 4f 70 2d 3e 70 34  har *z = pOp->p4
1a72d 2e 7a 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 73  .z;.    while( s
1a72e 71 6c 69 74 65 33 49 73 73 70 61 63 65 28 2a 7a  qlite3Isspace(*z
1a72f 29 20 29 20 7a 2b 2b 3b 0a 20 20 20 20 70 72 69  ) ) z++;.    pri
1a730 6e 74 66 28 22 53 51 4c 3a 20 5b 25 73 5d 5c 6e  ntf("SQL: [%s]\n
1a731 22 2c 20 7a 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  ", z);.  }.}.#en
1a732 64 69 66 0a 0a 23 69 66 20 21 64 65 66 69 6e 65  dif..#if !define
1a733 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
1a734 41 43 45 29 20 26 26 20 64 65 66 69 6e 65 64 28  ACE) && defined(
1a735 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f  SQLITE_ENABLE_IO
1a736 54 52 41 43 45 29 0a 2f 2a 0a 2a 2a 20 50 72 69  TRACE)./*.** Pri
1a737 6e 74 20 61 6e 20 49 4f 54 52 41 43 45 20 6d 65  nt an IOTRACE me
1a738 73 73 61 67 65 20 73 68 6f 77 69 6e 67 20 53 51  ssage showing SQ
1a739 4c 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2f 0a 53 51  L content..*/.SQ
1a73a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1a73b 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54  d sqlite3VdbeIOT
1a73c 72 61 63 65 53 71 6c 28 56 64 62 65 20 2a 70 29  raceSql(Vdbe *p)
1a73d 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d 20 70 2d  {.  int nOp = p-
1a73e 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f 70 20 2a  >nOp;.  VdbeOp *
1a73f 70 4f 70 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  pOp;.  if( sqlit
1a740 65 33 49 6f 54 72 61 63 65 3d 3d 30 20 29 20 72  e3IoTrace==0 ) r
1a741 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 6e 4f 70  eturn;.  if( nOp
1a742 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70  <1 ) return;.  p
1a743 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 30 5d 3b  Op = &p->aOp[0];
1a744 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f  .  if( pOp->opco
1a745 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26 20  de==OP_Trace && 
1a746 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20 29 7b 0a  pOp->p4.z!=0 ){.
1a747 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20      int i, j;.  
1a748 20 20 63 68 61 72 20 7a 5b 31 30 30 30 5d 3b 0a    char z[1000];.
1a749 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1a74a 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 29 2c 20  intf(sizeof(z), 
1a74b 7a 2c 20 22 25 73 22 2c 20 70 4f 70 2d 3e 70 34  z, "%s", pOp->p4
1a74c 2e 7a 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  .z);.    for(i=0
1a74d 3b 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65  ; sqlite3Isspace
1a74e 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20  (z[i]); i++){}. 
1a74f 20 20 20 66 6f 72 28 6a 3d 30 3b 20 7a 5b 69 5d     for(j=0; z[i]
1a750 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
1a751 28 20 73 71 6c 69 74 65 33 49 73 73 70 61 63 65  ( sqlite3Isspace
1a752 28 7a 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20  (z[i]) ){.      
1a753 20 20 69 66 28 20 7a 5b 69 2d 31 5d 21 3d 27 20    if( z[i-1]!=' 
1a754 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a  ' ){.          z
1a755 5b 6a 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20  [j++] = ' ';.   
1a756 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
1a757 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b  se{.        z[j+
1a758 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20 20  +] = z[i];.     
1a759 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 5b 6a   }.    }.    z[j
1a75a 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74  ] = 0;.    sqlit
1a75b 65 33 49 6f 54 72 61 63 65 28 22 53 51 4c 20 25  e3IoTrace("SQL %
1a75c 73 5c 6e 22 2c 20 7a 29 3b 0a 20 20 7d 0a 7d 0a  s\n", z);.  }.}.
1a75d 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54  #endif /* !SQLIT
1a75e 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20 26 26 20  E_OMIT_TRACE && 
1a75f 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f  SQLITE_ENABLE_IO
1a760 54 52 41 43 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  TRACE */../*.** 
1a761 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66  Allocate space f
1a762 72 6f 6d 20 61 20 66 69 78 65 64 20 73 69 7a 65  rom a fixed size
1a763 20 62 75 66 66 65 72 2e 20 20 4d 61 6b 65 20 2a   buffer.  Make *
1a764 70 70 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a  pp point to the.
1a765 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 73 70 61  ** allocated spa
1a766 63 65 2e 20 20 28 4e 6f 74 65 3a 20 20 70 70 20  ce.  (Note:  pp 
1a767 69 73 20 61 20 63 68 61 72 2a 20 72 61 74 68 65  is a char* rathe
1a768 72 20 74 68 61 6e 20 61 20 76 6f 69 64 2a 2a 20  r than a void** 
1a769 74 6f 0a 2a 2a 20 77 6f 72 6b 20 61 72 6f 75 6e  to.** work aroun
1a76a 64 20 74 68 65 20 70 6f 69 6e 74 65 72 20 61 6c  d the pointer al
1a76b 69 61 73 69 6e 67 20 72 75 6c 65 73 20 6f 66 20  iasing rules of 
1a76c 43 2e 29 20 20 2a 70 70 20 73 68 6f 75 6c 64 20  C.)  *pp should 
1a76d 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a 20 62 65 20  initially.** be 
1a76e 7a 65 72 6f 2e 20 20 49 66 20 2a 70 70 20 69 73  zero.  If *pp is
1a76f 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 61 74 20   not zero, that 
1a770 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 73  means that the s
1a771 70 61 63 65 20 68 61 73 20 61 6c 72 65 61 64 79  pace has already
1a772 0a 2a 2a 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74  .** been allocat
1a773 65 64 20 61 6e 64 20 74 68 69 73 20 72 6f 75 74  ed and this rout
1a774 69 6e 65 20 69 73 20 61 20 6e 6f 6f 70 2e 0a 2a  ine is a noop..*
1a775 2a 0a 2a 2a 20 6e 42 79 74 65 20 69 73 20 74 68  *.** nByte is th
1a776 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
1a777 73 20 6f 66 20 73 70 61 63 65 20 6e 65 65 64 65  s of space neede
1a778 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70 46 72 6f 6d  d..**.** *ppFrom
1a779 20 70 6f 69 6e 74 20 74 6f 20 61 76 61 69 6c 61   point to availa
1a77a 62 6c 65 20 73 70 61 63 65 20 61 6e 64 20 70 45  ble space and pE
1a77b 6e 64 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  nd points to the
1a77c 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 61   end of the.** a
1a77d 76 61 69 6c 61 62 6c 65 20 73 70 61 63 65 2e 0a  vailable space..
1a77e 2a 2a 0a 2a 2a 20 2a 70 6e 42 79 74 65 20 69 73  **.** *pnByte is
1a77f 20 61 20 63 6f 75 6e 74 65 72 20 6f 66 20 74 68   a counter of th
1a780 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
1a781 73 20 6f 66 20 73 70 61 63 65 20 74 68 61 74 20  s of space that 
1a782 68 61 76 65 20 66 61 69 6c 65 64 0a 2a 2a 20 74  have failed.** t
1a783 6f 20 61 6c 6c 6f 63 61 74 65 2e 20 20 49 66 20  o allocate.  If 
1a784 74 68 65 72 65 20 69 73 20 69 6e 73 75 66 66 69  there is insuffi
1a785 63 69 65 6e 74 20 73 70 61 63 65 20 69 6e 20 2a  cient space in *
1a786 70 70 46 72 6f 6d 20 74 6f 20 73 61 74 69 73 66  ppFrom to satisf
1a787 79 20 74 68 65 0a 2a 2a 20 72 65 71 75 65 73 74  y the.** request
1a788 2c 20 74 68 65 6e 20 69 6e 63 72 65 6d 65 6e 74  , then increment
1a789 20 2a 70 6e 42 79 74 65 20 62 79 20 74 68 65 20   *pnByte by the 
1a78a 61 6d 6f 75 6e 74 20 6f 66 20 74 68 65 20 72 65  amount of the re
1a78b 71 75 65 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  quest..*/.static
1a78c 20 76 6f 69 64 20 61 6c 6c 6f 63 53 70 61 63 65   void allocSpace
1a78d 28 0a 20 20 63 68 61 72 20 2a 70 70 2c 20 20 20  (.  char *pp,   
1a78e 20 20 20 20 20 20 20 20 20 2f 2a 20 49 4e 2f 4f           /* IN/O
1a78f 55 54 3a 20 53 65 74 20 2a 70 70 20 74 6f 20 70  UT: Set *pp to p
1a790 6f 69 6e 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65  oint to allocate
1a791 64 20 62 75 66 66 65 72 20 2a 2f 0a 20 20 69 6e  d buffer */.  in
1a792 74 20 6e 42 79 74 65 2c 20 20 20 20 20 20 20 20  t nByte,        
1a793 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1a794 62 79 74 65 73 20 74 6f 20 61 6c 6c 6f 63 61 74  bytes to allocat
1a795 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 70 70 46 72  e */.  u8 **ppFr
1a796 6f 6d 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 49  om,         /* I
1a797 4e 2f 4f 55 54 3a 20 41 6c 6c 6f 63 61 74 65 20  N/OUT: Allocate 
1a798 66 72 6f 6d 20 2a 70 70 46 72 6f 6d 20 2a 2f 0a  from *ppFrom */.
1a799 20 20 75 38 20 2a 70 45 6e 64 2c 20 20 20 20 20    u8 *pEnd,     
1a79a 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
1a79b 72 20 74 6f 20 31 20 62 79 74 65 20 70 61 73 74  r to 1 byte past
1a79c 20 74 68 65 20 65 6e 64 20 6f 66 20 2a 70 70 46   the end of *ppF
1a79d 72 6f 6d 20 62 75 66 66 65 72 20 2a 2f 0a 20 20  rom buffer */.  
1a79e 69 6e 74 20 2a 70 6e 42 79 74 65 20 20 20 20 20  int *pnByte     
1a79f 20 20 20 20 20 2f 2a 20 49 66 20 61 6c 6c 6f 63       /* If alloc
1a7a0 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20  ation cannot be 
1a7a1 6d 61 64 65 2c 20 69 6e 63 72 65 6d 65 6e 74 20  made, increment 
1a7a2 2a 70 6e 42 79 74 65 20 2a 2f 0a 29 7b 0a 20 20  *pnByte */.){.  
1a7a3 61 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59  assert( EIGHT_BY
1a7a4 54 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 2a 70 70  TE_ALIGNMENT(*pp
1a7a5 46 72 6f 6d 29 20 29 3b 0a 20 20 69 66 28 20 28  From) );.  if( (
1a7a6 2a 28 76 6f 69 64 2a 2a 29 70 70 29 3d 3d 30 20  *(void**)pp)==0 
1a7a7 29 7b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 52  ){.    nByte = R
1a7a8 4f 55 4e 44 38 28 6e 42 79 74 65 29 3b 0a 20 20  OUND8(nByte);.  
1a7a9 20 20 69 66 28 20 26 28 2a 70 70 46 72 6f 6d 29    if( &(*ppFrom)
1a7aa 5b 6e 42 79 74 65 5d 20 3c 3d 20 70 45 6e 64 20  [nByte] <= pEnd 
1a7ab 29 7b 0a 20 20 20 20 20 20 2a 28 76 6f 69 64 2a  ){.      *(void*
1a7ac 2a 29 70 70 20 3d 20 28 76 6f 69 64 20 2a 29 2a  *)pp = (void *)*
1a7ad 70 70 46 72 6f 6d 3b 0a 20 20 20 20 20 20 2a 70  ppFrom;.      *p
1a7ae 70 46 72 6f 6d 20 2b 3d 20 6e 42 79 74 65 3b 0a  pFrom += nByte;.
1a7af 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1a7b0 20 2a 70 6e 42 79 74 65 20 2b 3d 20 6e 42 79 74   *pnByte += nByt
1a7b1 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  e;.    }.  }.}..
1a7b2 2f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 61 20  /*.** Prepare a 
1a7b3 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  virtual machine 
1a7b4 66 6f 72 20 65 78 65 63 75 74 69 6f 6e 2e 20 20  for execution.  
1a7b5 54 68 69 73 20 69 6e 76 6f 6c 76 65 73 20 74 68  This involves th
1a7b6 69 6e 67 73 20 73 75 63 68 0a 2a 2a 20 61 73 20  ings such.** as 
1a7b7 61 6c 6c 6f 63 61 74 69 6e 67 20 73 74 61 63 6b  allocating stack
1a7b8 20 73 70 61 63 65 20 61 6e 64 20 69 6e 69 74 69   space and initi
1a7b9 61 6c 69 7a 69 6e 67 20 74 68 65 20 70 72 6f 67  alizing the prog
1a7ba 72 61 6d 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 20  ram counter..** 
1a7bb 41 66 74 65 72 20 74 68 65 20 56 44 42 45 20 68  After the VDBE h
1a7bc 61 73 20 62 65 20 70 72 65 70 70 65 64 2c 20 69  as be prepped, i
1a7bd 74 20 63 61 6e 20 62 65 20 65 78 65 63 75 74 65  t can be execute
1a7be 64 20 62 79 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  d by one or more
1a7bf 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c  .** calls to sql
1a7c0 69 74 65 33 56 64 62 65 45 78 65 63 28 29 2e 20  ite3VdbeExec(). 
1a7c1 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20   .**.** This is 
1a7c2 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20  the only way to 
1a7c3 6d 6f 76 65 20 61 20 56 44 42 45 20 66 72 6f 6d  move a VDBE from
1a7c4 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54   VDBE_MAGIC_INIT
1a7c5 20 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49   to.** VDBE_MAGI
1a7c6 43 5f 52 55 4e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  C_RUN..**.** Thi
1a7c7 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62  s function may b
1a7c8 65 20 63 61 6c 6c 65 64 20 6d 6f 72 65 20 74 68  e called more th
1a7c9 61 6e 20 6f 6e 63 65 20 6f 6e 20 61 20 73 69 6e  an once on a sin
1a7ca 67 6c 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68  gle virtual mach
1a7cb 69 6e 65 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73  ine..** The firs
1a7cc 74 20 63 61 6c 6c 20 69 73 20 6d 61 64 65 20 77  t call is made w
1a7cd 68 69 6c 65 20 63 6f 6d 70 69 6c 69 6e 67 20 74  hile compiling t
1a7ce 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
1a7cf 2e 20 53 75 62 73 65 71 75 65 6e 74 0a 2a 2a 20  . Subsequent.** 
1a7d0 63 61 6c 6c 73 20 61 72 65 20 6d 61 64 65 20 61  calls are made a
1a7d1 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 70 72  s part of the pr
1a7d2 6f 63 65 73 73 20 6f 66 20 72 65 73 65 74 74 69  ocess of resetti
1a7d3 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74  ng a statement t
1a7d4 6f 20 62 65 0a 2a 2a 20 72 65 2d 65 78 65 63 75  o be.** re-execu
1a7d5 74 65 64 20 28 66 72 6f 6d 20 61 20 63 61 6c 6c  ted (from a call
1a7d6 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 73 65   to sqlite3_rese
1a7d7 74 28 29 29 2e 20 54 68 65 20 6e 56 61 72 2c 20  t()). The nVar, 
1a7d8 6e 4d 65 6d 2c 20 6e 43 75 72 73 6f 72 20 0a 2a  nMem, nCursor .*
1a7d9 2a 20 61 6e 64 20 69 73 45 78 70 6c 61 69 6e 20  * and isExplain 
1a7da 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 6f  parameters are o
1a7db 6e 6c 79 20 70 61 73 73 65 64 20 63 6f 72 72 65  nly passed corre
1a7dc 63 74 20 76 61 6c 75 65 73 20 74 68 65 20 66 69  ct values the fi
1a7dd 72 73 74 20 74 69 6d 65 0a 2a 2a 20 74 68 65 20  rst time.** the 
1a7de 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
1a7df 65 64 2e 20 4f 6e 20 73 75 62 73 65 71 75 65 6e  ed. On subsequen
1a7e0 74 20 63 61 6c 6c 73 2c 20 66 72 6f 6d 20 73 71  t calls, from sq
1a7e1 6c 69 74 65 33 5f 72 65 73 65 74 28 29 2c 20 6e  lite3_reset(), n
1a7e2 56 61 72 0a 2a 2a 20 69 73 20 70 61 73 73 65 64  Var.** is passed
1a7e3 20 2d 31 20 61 6e 64 20 6e 4d 65 6d 2c 20 6e 43   -1 and nMem, nC
1a7e4 75 72 73 6f 72 20 61 6e 64 20 69 73 45 78 70 6c  ursor and isExpl
1a7e5 61 69 6e 20 61 72 65 20 61 6c 6c 20 70 61 73 73  ain are all pass
1a7e6 65 64 20 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49  ed zero..*/.SQLI
1a7e7 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1a7e8 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52  sqlite3VdbeMakeR
1a7e9 65 61 64 79 28 0a 20 20 56 64 62 65 20 2a 70 2c  eady(.  Vdbe *p,
1a7ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a7eb 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44         /* The VD
1a7ec 42 45 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72  BE */.  int nVar
1a7ed 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1a7ee 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1a7ef 20 6f 66 20 27 3f 27 20 73 65 65 20 69 6e 20 74   of '?' see in t
1a7f0 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
1a7f1 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 65 6d 2c 20   */.  int nMem, 
1a7f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a7f3 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1a7f4 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 74  f memory cells t
1a7f5 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20 20  o allocate */.  
1a7f6 69 6e 74 20 6e 43 75 72 73 6f 72 2c 20 20 20 20  int nCursor,    
1a7f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1a7f8 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 75 72 73  * Number of curs
1a7f9 6f 72 73 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ors to allocate 
1a7fa 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 20  */.  int nArg,  
1a7fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a7fc 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e      /* Maximum n
1a7fd 75 6d 62 65 72 20 6f 66 20 61 72 67 73 20 69 6e  umber of args in
1a7fe 20 53 75 62 50 72 6f 67 72 61 6d 73 20 2a 2f 0a   SubPrograms */.
1a7ff 20 20 69 6e 74 20 69 73 45 78 70 6c 61 69 6e 2c    int isExplain,
1a800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a801 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
1a802 45 58 50 4c 41 49 4e 20 6b 65 79 77 6f 72 64 73  EXPLAIN keywords
1a803 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20   is present */. 
1a804 20 69 6e 74 20 75 73 65 73 53 74 6d 74 4a 6f 75   int usesStmtJou
1a805 72 6e 61 6c 20 20 20 20 20 20 20 20 20 20 20 20  rnal            
1a806 2f 2a 20 54 72 75 65 20 74 6f 20 73 65 74 20 56  /* True to set V
1a807 64 62 65 2e 75 73 65 73 53 74 6d 74 4a 6f 75 72  dbe.usesStmtJour
1a808 6e 61 6c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  nal */.){.  int 
1a809 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  n;.  sqlite3 *db
1a80a 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 61 73 73   = p->db;..  ass
1a80b 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61  ert( p!=0 );.  a
1a80c 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d  ssert( p->magic=
1a80d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54  =VDBE_MAGIC_INIT
1a80e 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 72 65 20   );..  /* There 
1a80f 73 68 6f 75 6c 64 20 62 65 20 61 74 20 6c 65 61  should be at lea
1a810 73 74 20 6f 6e 65 20 6f 70 63 6f 64 65 2e 0a 20  st one opcode.. 
1a811 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d   */.  assert( p-
1a812 3e 6e 4f 70 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20  >nOp>0 );..  /* 
1a813 53 65 74 20 74 68 65 20 6d 61 67 69 63 20 74 6f  Set the magic to
1a814 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20   VDBE_MAGIC_RUN 
1a815 73 6f 6f 6e 65 72 20 72 61 74 68 65 72 20 74 68  sooner rather th
1a816 61 6e 20 6c 61 74 65 72 2e 20 2a 2f 0a 20 20 70  an later. */.  p
1a817 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d  ->magic = VDBE_M
1a818 41 47 49 43 5f 52 55 4e 3b 0a 0a 20 20 2f 2a 20  AGIC_RUN;..  /* 
1a819 46 6f 72 20 65 61 63 68 20 63 75 72 73 6f 72 20  For each cursor 
1a81a 72 65 71 75 69 72 65 64 2c 20 61 6c 73 6f 20 61  required, also a
1a81b 6c 6c 6f 63 61 74 65 20 61 20 6d 65 6d 6f 72 79  llocate a memory
1a81c 20 63 65 6c 6c 2e 20 4d 65 6d 6f 72 79 0a 20 20   cell. Memory.  
1a81d 2a 2a 20 63 65 6c 6c 73 20 28 6e 4d 65 6d 2b 31  ** cells (nMem+1
1a81e 2d 6e 43 75 72 73 6f 72 29 2e 2e 6e 4d 65 6d 2c  -nCursor)..nMem,
1a81f 20 69 6e 63 6c 75 73 69 76 65 2c 20 77 69 6c 6c   inclusive, will
1a820 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 20 62   never be used b
1a821 79 0a 20 20 2a 2a 20 74 68 65 20 76 64 62 65 20  y.  ** the vdbe 
1a822 70 72 6f 67 72 61 6d 2e 20 49 6e 73 74 65 61 64  program. Instead
1a823 20 74 68 65 79 20 61 72 65 20 75 73 65 64 20 74   they are used t
1a824 6f 20 61 6c 6c 6f 63 61 74 65 20 73 70 61 63 65  o allocate space
1a825 20 66 6f 72 0a 20 20 2a 2a 20 56 64 62 65 43 75   for.  ** VdbeCu
1a826 72 73 6f 72 2f 42 74 43 75 72 73 6f 72 20 73 74  rsor/BtCursor st
1a827 72 75 63 74 75 72 65 73 2e 20 54 68 65 20 62 6c  ructures. The bl
1a828 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61 73 73  ob of memory ass
1a829 6f 63 69 61 74 65 64 20 77 69 74 68 20 0a 20 20  ociated with .  
1a82a 2a 2a 20 63 75 72 73 6f 72 20 30 20 69 73 20 73  ** cursor 0 is s
1a82b 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20  tored in memory 
1a82c 63 65 6c 6c 20 6e 4d 65 6d 2e 20 4d 65 6d 6f 72  cell nMem. Memor
1a82d 79 20 63 65 6c 6c 20 28 6e 4d 65 6d 2d 31 29 0a  y cell (nMem-1).
1a82e 20 20 2a 2a 20 73 74 6f 72 65 73 20 74 68 65 20    ** stores the 
1a82f 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20 61  blob of memory a
1a830 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 63  ssociated with c
1a831 75 72 73 6f 72 20 31 2c 20 65 74 63 2e 0a 20 20  ursor 1, etc..  
1a832 2a 2a 0a 20 20 2a 2a 20 53 65 65 20 61 6c 73 6f  **.  ** See also
1a833 3a 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72  : allocateCursor
1a834 28 29 2e 0a 20 20 2a 2f 0a 20 20 6e 4d 65 6d 20  ()..  */.  nMem 
1a835 2b 3d 20 6e 43 75 72 73 6f 72 3b 0a 0a 20 20 2f  += nCursor;..  /
1a836 2a 20 41 6c 6c 6f 63 61 74 65 20 73 70 61 63 65  * Allocate space
1a837 20 66 6f 72 20 6d 65 6d 6f 72 79 20 72 65 67 69   for memory regi
1a838 73 74 65 72 73 2c 20 53 51 4c 20 76 61 72 69 61  sters, SQL varia
1a839 62 6c 65 73 2c 20 56 44 42 45 20 63 75 72 73 6f  bles, VDBE curso
1a83a 72 73 20 61 6e 64 20 0a 20 20 2a 2a 20 61 6e 20  rs and .  ** an 
1a83b 61 72 72 61 79 20 74 6f 20 6d 61 72 73 68 61 6c  array to marshal
1a83c 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 61 72   SQL function ar
1a83d 67 75 6d 65 6e 74 73 20 69 6e 2e 20 54 68 69 73  guments in. This
1a83e 20 69 73 20 6f 6e 6c 79 20 64 6f 6e 65 20 74 68   is only done th
1a83f 65 0a 20 20 2a 2a 20 66 69 72 73 74 20 74 69 6d  e.  ** first tim
1a840 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  e this function 
1a841 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 61 20  is called for a 
1a842 67 69 76 65 6e 20 56 44 42 45 2c 20 6e 6f 74 20  given VDBE, not 
1a843 77 68 65 6e 20 69 74 20 69 73 0a 20 20 2a 2a 20  when it is.  ** 
1a844 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f  being called fro
1a845 6d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  m sqlite3_reset(
1a846 29 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 76  ) to reset the v
1a847 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a  irtual machine..
1a848 20 20 2a 2f 0a 20 20 69 66 28 20 6e 56 61 72 3e    */.  if( nVar>
1a849 3d 30 20 26 26 20 41 4c 57 41 59 53 28 64 62 2d  =0 && ALWAYS(db-
1a84a 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30  >mallocFailed==0
1a84b 29 20 29 7b 0a 20 20 20 20 75 38 20 2a 7a 43 73  ) ){.    u8 *zCs
1a84c 72 20 3d 20 28 75 38 20 2a 29 26 70 2d 3e 61 4f  r = (u8 *)&p->aO
1a84d 70 5b 70 2d 3e 6e 4f 70 5d 3b 0a 20 20 20 20 75  p[p->nOp];.    u
1a84e 38 20 2a 7a 45 6e 64 20 3d 20 28 75 38 20 2a 29  8 *zEnd = (u8 *)
1a84f 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 41 6c  &p->aOp[p->nOpAl
1a850 6c 6f 63 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 42  loc];.    int nB
1a851 79 74 65 3b 0a 20 20 20 20 72 65 73 6f 6c 76 65  yte;.    resolve
1a852 50 32 56 61 6c 75 65 73 28 70 2c 20 26 6e 41 72  P2Values(p, &nAr
1a853 67 29 3b 0a 20 20 20 20 70 2d 3e 75 73 65 73 53  g);.    p->usesS
1a854 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d 20 28 75 38  tmtJournal = (u8
1a855 29 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c  )usesStmtJournal
1a856 3b 0a 20 20 20 20 69 66 28 20 69 73 45 78 70 6c  ;.    if( isExpl
1a857 61 69 6e 20 26 26 20 6e 4d 65 6d 3c 31 30 20 29  ain && nMem<10 )
1a858 7b 0a 20 20 20 20 20 20 6e 4d 65 6d 20 3d 20 31  {.      nMem = 1
1a859 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d  0;.    }.    mem
1a85a 73 65 74 28 7a 43 73 72 2c 20 30 2c 20 7a 45 6e  set(zCsr, 0, zEn
1a85b 64 2d 7a 43 73 72 29 3b 0a 20 20 20 20 7a 43 73  d-zCsr);.    zCs
1a85c 72 20 2b 3d 20 28 7a 43 73 72 20 2d 20 28 75 38  r += (zCsr - (u8
1a85d 2a 29 30 29 26 37 3b 0a 20 20 20 20 61 73 73 65  *)0)&7;.    asse
1a85e 72 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41  rt( EIGHT_BYTE_A
1a85f 4c 49 47 4e 4d 45 4e 54 28 7a 43 73 72 29 20 29  LIGNMENT(zCsr) )
1a860 3b 0a 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20  ;..    do {.    
1a861 20 20 6e 42 79 74 65 20 3d 20 30 3b 0a 20 20 20    nByte = 0;.   
1a862 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63     allocSpace((c
1a863 68 61 72 2a 29 26 70 2d 3e 61 4d 65 6d 2c 20 6e  har*)&p->aMem, n
1a864 4d 65 6d 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 2c  Mem*sizeof(Mem),
1a865 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c 20 26 6e   &zCsr, zEnd, &n
1a866 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61 6c 6c  Byte);.      all
1a867 6f 63 53 70 61 63 65 28 28 63 68 61 72 2a 29 26  ocSpace((char*)&
1a868 70 2d 3e 61 56 61 72 2c 20 6e 56 61 72 2a 73 69  p->aVar, nVar*si
1a869 7a 65 6f 66 28 4d 65 6d 29 2c 20 26 7a 43 73 72  zeof(Mem), &zCsr
1a86a 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 29 3b  , zEnd, &nByte);
1a86b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 53 70 61 63  .      allocSpac
1a86c 65 28 28 63 68 61 72 2a 29 26 70 2d 3e 61 70 41  e((char*)&p->apA
1a86d 72 67 2c 20 6e 41 72 67 2a 73 69 7a 65 6f 66 28  rg, nArg*sizeof(
1a86e 4d 65 6d 2a 29 2c 20 26 7a 43 73 72 2c 20 7a 45  Mem*), &zCsr, zE
1a86f 6e 64 2c 20 26 6e 42 79 74 65 29 3b 0a 20 20 20  nd, &nByte);.   
1a870 20 20 20 61 6c 6c 6f 63 53 70 61 63 65 28 28 63     allocSpace((c
1a871 68 61 72 2a 29 26 70 2d 3e 61 7a 56 61 72 2c 20  har*)&p->azVar, 
1a872 6e 56 61 72 2a 73 69 7a 65 6f 66 28 63 68 61 72  nVar*sizeof(char
1a873 2a 29 2c 20 26 7a 43 73 72 2c 20 7a 45 6e 64 2c  *), &zCsr, zEnd,
1a874 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20   &nByte);.      
1a875 61 6c 6c 6f 63 53 70 61 63 65 28 28 63 68 61 72  allocSpace((char
1a876 2a 29 26 70 2d 3e 61 70 43 73 72 2c 20 0a 20 20  *)&p->apCsr, .  
1a877 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e                 n
1a878 43 75 72 73 6f 72 2a 73 69 7a 65 6f 66 28 56 64  Cursor*sizeof(Vd
1a879 62 65 43 75 72 73 6f 72 2a 29 2c 20 26 7a 43 73  beCursor*), &zCs
1a87a 72 2c 20 7a 45 6e 64 2c 20 26 6e 42 79 74 65 0a  r, zEnd, &nByte.
1a87b 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 69        );.      i
1a87c 66 28 20 6e 42 79 74 65 20 29 7b 0a 20 20 20 20  f( nByte ){.    
1a87d 20 20 20 20 70 2d 3e 70 46 72 65 65 20 3d 20 73      p->pFree = s
1a87e 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
1a87f 72 6f 28 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20  ro(db, nByte);. 
1a880 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 43 73       }.      zCs
1a881 72 20 3d 20 70 2d 3e 70 46 72 65 65 3b 0a 20 20  r = p->pFree;.  
1a882 20 20 20 20 7a 45 6e 64 20 3d 20 26 7a 43 73 72      zEnd = &zCsr
1a883 5b 6e 42 79 74 65 5d 3b 0a 20 20 20 20 7d 77 68  [nByte];.    }wh
1a884 69 6c 65 28 20 6e 42 79 74 65 20 26 26 20 21 64  ile( nByte && !d
1a885 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1a886 29 3b 0a 0a 20 20 20 20 70 2d 3e 6e 43 75 72 73  );..    p->nCurs
1a887 6f 72 20 3d 20 28 75 31 36 29 6e 43 75 72 73 6f  or = (u16)nCurso
1a888 72 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 61 56  r;.    if( p->aV
1a889 61 72 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e  ar ){.      p->n
1a88a 56 61 72 20 3d 20 28 75 31 36 29 6e 56 61 72 3b  Var = (u16)nVar;
1a88b 0a 20 20 20 20 20 20 66 6f 72 28 6e 3d 30 3b 20  .      for(n=0; 
1a88c 6e 3c 6e 56 61 72 3b 20 6e 2b 2b 29 7b 0a 20 20  n<nVar; n++){.  
1a88d 20 20 20 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d        p->aVar[n]
1a88e 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  .flags = MEM_Nul
1a88f 6c 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 56  l;.        p->aV
1a890 61 72 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b 0a 20  ar[n].db = db;. 
1a891 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1a892 20 69 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a   if( p->aMem ){.
1a893 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d 2d 2d 3b        p->aMem--;
1a894 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a895 20 20 20 20 20 20 2f 2a 20 61 4d 65 6d 5b 5d 20        /* aMem[] 
1a896 67 6f 65 73 20 66 72 6f 6d 20 31 2e 2e 6e 4d 65  goes from 1..nMe
1a897 6d 20 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 6e 4d  m */.      p->nM
1a898 65 6d 20 3d 20 6e 4d 65 6d 3b 20 20 20 20 20 20  em = nMem;      
1a899 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20             /*   
1a89a 20 20 20 20 6e 6f 74 20 66 72 6f 6d 20 30 2e 2e      not from 0..
1a89b 6e 4d 65 6d 2d 31 20 2a 2f 0a 20 20 20 20 20 20  nMem-1 */.      
1a89c 66 6f 72 28 6e 3d 31 3b 20 6e 3c 3d 6e 4d 65 6d  for(n=1; n<=nMem
1a89d 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; n++){.        
1a89e 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e 66 6c 61 67 73  p->aMem[n].flags
1a89f 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20   = MEM_Null;.   
1a8a0 20 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e       p->aMem[n].
1a8a1 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d  db = db;.      }
1a8a2 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66 64 65  .    }.  }.#ifde
1a8a3 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
1a8a4 20 66 6f 72 28 6e 3d 31 3b 20 6e 3c 70 2d 3e 6e   for(n=1; n<p->n
1a8a5 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a 20 20 20 20 61  Mem; n++){.    a
1a8a6 73 73 65 72 74 28 20 70 2d 3e 61 4d 65 6d 5b 6e  ssert( p->aMem[n
1a8a7 5d 2e 64 62 3d 3d 64 62 20 29 3b 0a 20 20 7d 0a  ].db==db );.  }.
1a8a8 23 65 6e 64 69 66 0a 0a 20 20 70 2d 3e 70 63 20  #endif..  p->pc 
1a8a9 3d 20 2d 31 3b 0a 20 20 70 2d 3e 72 63 20 3d 20  = -1;.  p->rc = 
1a8aa 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 2d 3e  SQLITE_OK;.  p->
1a8ab 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45  errorAction = OE
1a8ac 5f 41 62 6f 72 74 3b 0a 20 20 70 2d 3e 65 78 70  _Abort;.  p->exp
1a8ad 6c 61 69 6e 20 7c 3d 20 69 73 45 78 70 6c 61 69  lain |= isExplai
1a8ae 6e 3b 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20  n;.  p->magic = 
1a8af 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 3b 0a  VDBE_MAGIC_RUN;.
1a8b0 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 30    p->nChange = 0
1a8b1 3b 0a 20 20 70 2d 3e 63 61 63 68 65 43 74 72 20  ;.  p->cacheCtr 
1a8b2 3d 20 31 3b 0a 20 20 70 2d 3e 6d 69 6e 57 72 69  = 1;.  p->minWri
1a8b3 74 65 46 69 6c 65 46 6f 72 6d 61 74 20 3d 20 32  teFileFormat = 2
1a8b4 35 35 3b 0a 20 20 70 2d 3e 69 53 74 61 74 65 6d  55;.  p->iStatem
1a8b5 65 6e 74 20 3d 20 30 3b 0a 23 69 66 64 65 66 20  ent = 0;.#ifdef 
1a8b6 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b  VDBE_PROFILE.  {
1a8b7 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
1a8b8 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f  for(i=0; i<p->nO
1a8b9 70 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70  p; i++){.      p
1a8ba 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3d 20 30  ->aOp[i].cnt = 0
1a8bb 3b 0a 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69  ;.      p->aOp[i
1a8bc 5d 2e 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20 20  ].cycles = 0;.  
1a8bd 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d    }.  }.#endif.}
1a8be 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20  ../*.** Close a 
1a8bf 56 44 42 45 20 63 75 72 73 6f 72 20 61 6e 64 20  VDBE cursor and 
1a8c0 72 65 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 20  release all the 
1a8c1 72 65 73 6f 75 72 63 65 73 20 74 68 61 74 20 63  resources that c
1a8c2 75 72 73 6f 72 20 0a 2a 2a 20 68 61 70 70 65 6e  ursor .** happen
1a8c3 73 20 74 6f 20 68 6f 6c 64 2e 0a 2a 2f 0a 53 51  s to hold..*/.SQ
1a8c4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1a8c5 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65  d sqlite3VdbeFre
1a8c6 65 43 75 72 73 6f 72 28 56 64 62 65 20 2a 70 2c  eCursor(Vdbe *p,
1a8c7 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78   VdbeCursor *pCx
1a8c8 29 7b 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 20  ){.  if( pCx==0 
1a8c9 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
1a8ca 20 7d 0a 20 20 69 66 28 20 70 43 78 2d 3e 70 42   }.  if( pCx->pB
1a8cb 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
1a8cc 42 74 72 65 65 43 6c 6f 73 65 28 70 43 78 2d 3e  BtreeClose(pCx->
1a8cd 70 42 74 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65  pBt);.    /* The
1a8ce 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 77 69   pCx->pCursor wi
1a8cf 6c 6c 20 62 65 20 63 6c 6f 73 65 20 61 75 74 6f  ll be close auto
1a8d0 6d 61 74 69 63 61 6c 6c 79 2c 20 69 66 20 69 74  matically, if it
1a8d1 20 65 78 69 73 74 73 2c 20 62 79 0a 20 20 20 20   exists, by.    
1a8d2 2a 2a 20 74 68 65 20 63 61 6c 6c 20 61 62 6f 76  ** the call abov
1a8d3 65 2e 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66  e. */.  }else if
1a8d4 28 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 29  ( pCx->pCursor )
1a8d5 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  {.    sqlite3Btr
1a8d6 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 43  eeCloseCursor(pC
1a8d7 78 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d  x->pCursor);.  }
1a8d8 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1a8d9 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1a8da 45 0a 20 20 69 66 28 20 70 43 78 2d 3e 70 56 74  E.  if( pCx->pVt
1a8db 61 62 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20  abCursor ){.    
1a8dc 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
1a8dd 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72  sor *pVtabCursor
1a8de 20 3d 20 70 43 78 2d 3e 70 56 74 61 62 43 75 72   = pCx->pVtabCur
1a8df 73 6f 72 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73  sor;.    const s
1a8e0 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
1a8e1 4d 6f 64 75 6c 65 20 3d 20 70 43 78 2d 3e 70 4d  Module = pCx->pM
1a8e2 6f 64 75 6c 65 3b 0a 20 20 20 20 70 2d 3e 69 6e  odule;.    p->in
1a8e3 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a  VtabMethod = 1;.
1a8e4 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
1a8e5 33 53 61 66 65 74 79 4f 66 66 28 70 2d 3e 64 62  3SafetyOff(p->db
1a8e6 29 3b 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e  );.    pModule->
1a8e7 78 43 6c 6f 73 65 28 70 56 74 61 62 43 75 72 73  xClose(pVtabCurs
1a8e8 6f 72 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73  or);.    (void)s
1a8e9 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 70  qlite3SafetyOn(p
1a8ea 2d 3e 64 62 29 3b 0a 20 20 20 20 70 2d 3e 69 6e  ->db);.    p->in
1a8eb 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a  VtabMethod = 0;.
1a8ec 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a    }.#endif.}../*
1a8ed 0a 2a 2a 20 43 6f 70 79 20 74 68 65 20 76 61 6c  .** Copy the val
1a8ee 75 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68  ues stored in th
1a8ef 65 20 56 64 62 65 46 72 61 6d 65 20 73 74 72 75  e VdbeFrame stru
1a8f0 63 74 75 72 65 20 74 6f 20 69 74 73 20 56 64 62  cture to its Vdb
1a8f1 65 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 75 73  e. This.** is us
1a8f2 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2c  ed, for example,
1a8f3 20 77 68 65 6e 20 61 20 74 72 69 67 67 65 72 20   when a trigger 
1a8f4 73 75 62 2d 70 72 6f 67 72 61 6d 20 69 73 20 68  sub-program is h
1a8f5 61 6c 74 65 64 20 74 6f 20 72 65 73 74 6f 72 65  alted to restore
1a8f6 0a 2a 2a 20 63 6f 6e 74 72 6f 6c 20 74 6f 20 74  .** control to t
1a8f7 68 65 20 6d 61 69 6e 20 70 72 6f 67 72 61 6d 2e  he main program.
1a8f8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1a8f9 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
1a8fa 62 65 46 72 61 6d 65 52 65 73 74 6f 72 65 28 56  beFrameRestore(V
1a8fb 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65  dbeFrame *pFrame
1a8fc 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
1a8fd 46 72 61 6d 65 2d 3e 76 3b 0a 20 20 76 2d 3e 61  Frame->v;.  v->a
1a8fe 4f 70 20 3d 20 70 46 72 61 6d 65 2d 3e 61 4f 70  Op = pFrame->aOp
1a8ff 3b 0a 20 20 76 2d 3e 6e 4f 70 20 3d 20 70 46 72  ;.  v->nOp = pFr
1a900 61 6d 65 2d 3e 6e 4f 70 3b 0a 20 20 76 2d 3e 61  ame->nOp;.  v->a
1a901 4d 65 6d 20 3d 20 70 46 72 61 6d 65 2d 3e 61 4d  Mem = pFrame->aM
1a902 65 6d 3b 0a 20 20 76 2d 3e 6e 4d 65 6d 20 3d 20  em;.  v->nMem = 
1a903 70 46 72 61 6d 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pFrame->nMem;.  
1a904 76 2d 3e 61 70 43 73 72 20 3d 20 70 46 72 61 6d  v->apCsr = pFram
1a905 65 2d 3e 61 70 43 73 72 3b 0a 20 20 76 2d 3e 6e  e->apCsr;.  v->n
1a906 43 75 72 73 6f 72 20 3d 20 70 46 72 61 6d 65 2d  Cursor = pFrame-
1a907 3e 6e 43 75 72 73 6f 72 3b 0a 20 20 76 2d 3e 64  >nCursor;.  v->d
1a908 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 70  b->lastRowid = p
1a909 46 72 61 6d 65 2d 3e 6c 61 73 74 52 6f 77 69 64  Frame->lastRowid
1a90a 3b 0a 20 20 76 2d 3e 6e 43 68 61 6e 67 65 20 3d  ;.  v->nChange =
1a90b 20 70 46 72 61 6d 65 2d 3e 6e 43 68 61 6e 67 65   pFrame->nChange
1a90c 3b 0a 20 20 72 65 74 75 72 6e 20 70 46 72 61 6d  ;.  return pFram
1a90d 65 2d 3e 70 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  e->pc;.}../*.** 
1a90e 43 6c 6f 73 65 20 61 6c 6c 20 63 75 72 73 6f 72  Close all cursor
1a90f 73 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 72 65  s..**.** Also re
1a910 6c 65 61 73 65 20 61 6e 79 20 64 79 6e 61 6d 69  lease any dynami
1a911 63 20 6d 65 6d 6f 72 79 20 68 65 6c 64 20 62 79  c memory held by
1a912 20 74 68 65 20 56 4d 20 69 6e 20 74 68 65 20 56   the VM in the V
1a913 64 62 65 2e 61 4d 65 6d 20 6d 65 6d 6f 72 79 20  dbe.aMem memory 
1a914 0a 2a 2a 20 63 65 6c 6c 20 61 72 72 61 79 2e 20  .** cell array. 
1a915 54 68 69 73 20 69 73 20 6e 65 63 65 73 73 61 72  This is necessar
1a916 79 20 61 73 20 74 68 65 20 6d 65 6d 6f 72 79 20  y as the memory 
1a917 63 65 6c 6c 20 61 72 72 61 79 20 6d 61 79 20 63  cell array may c
1a918 6f 6e 74 61 69 6e 0a 2a 2a 20 70 6f 69 6e 74 65  ontain.** pointe
1a919 72 73 20 74 6f 20 56 64 62 65 46 72 61 6d 65 20  rs to VdbeFrame 
1a91a 6f 62 6a 65 63 74 73 2c 20 77 68 69 63 68 20 6d  objects, which m
1a91b 61 79 20 69 6e 20 74 75 72 6e 20 63 6f 6e 74 61  ay in turn conta
1a91c 69 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f 0a 2a  in pointers to.*
1a91d 2a 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 2e 0a  * open cursors..
1a91e 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
1a91f 6c 6f 73 65 41 6c 6c 43 75 72 73 6f 72 73 28 56  loseAllCursors(V
1a920 64 62 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70  dbe *p){.  if( p
1a921 2d 3e 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 20  ->pFrame ){.    
1a922 56 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d  VdbeFrame *pFram
1a923 65 20 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0a 20  e = p->pFrame;. 
1a924 20 20 20 66 6f 72 28 70 46 72 61 6d 65 3d 70 2d     for(pFrame=p-
1a925 3e 70 46 72 61 6d 65 3b 20 70 46 72 61 6d 65 2d  >pFrame; pFrame-
1a926 3e 70 50 61 72 65 6e 74 3b 20 70 46 72 61 6d 65  >pParent; pFrame
1a927 3d 70 46 72 61 6d 65 2d 3e 70 50 61 72 65 6e 74  =pFrame->pParent
1a928 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1a929 62 65 46 72 61 6d 65 52 65 73 74 6f 72 65 28 70  beFrameRestore(p
1a92a 46 72 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 70 2d  Frame);.  }.  p-
1a92b 3e 70 46 72 61 6d 65 20 3d 20 30 3b 0a 20 20 70  >pFrame = 0;.  p
1a92c 2d 3e 6e 46 72 61 6d 65 20 3d 20 30 3b 0a 0a 20  ->nFrame = 0;.. 
1a92d 20 69 66 28 20 70 2d 3e 61 70 43 73 72 20 29 7b   if( p->apCsr ){
1a92e 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
1a92f 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43  for(i=0; i<p->nC
1a930 75 72 73 6f 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  ursor; i++){.   
1a931 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70     VdbeCursor *p
1a932 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b  C = p->apCsr[i];
1a933 0a 20 20 20 20 20 20 69 66 28 20 70 43 20 29 7b  .      if( pC ){
1a934 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1a935 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 70  VdbeFreeCursor(p
1a936 2c 20 70 43 29 3b 0a 20 20 20 20 20 20 20 20 70  , pC);.        p
1a937 2d 3e 61 70 43 73 72 5b 69 5d 20 3d 20 30 3b 0a  ->apCsr[i] = 0;.
1a938 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1a939 7d 0a 20 20 69 66 28 20 70 2d 3e 61 4d 65 6d 20  }.  if( p->aMem 
1a93a 29 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 4d 65  ){.    releaseMe
1a93b 6d 41 72 72 61 79 28 26 70 2d 3e 61 4d 65 6d 5b  mArray(&p->aMem[
1a93c 31 5d 2c 20 70 2d 3e 6e 4d 65 6d 29 3b 0a 20 20  1], p->nMem);.  
1a93d 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e  }.}../*.** Clean
1a93e 20 75 70 20 74 68 65 20 56 4d 20 61 66 74 65 72   up the VM after
1a93f 20 65 78 65 63 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a   execution..**.*
1a940 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77  * This routine w
1a941 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  ill automaticall
1a942 79 20 63 6c 6f 73 65 20 61 6e 79 20 63 75 72 73  y close any curs
1a943 6f 72 73 2c 20 6c 69 73 74 73 2c 20 61 6e 64 2f  ors, lists, and/
1a944 6f 72 0a 2a 2a 20 73 6f 72 74 65 72 73 20 74 68  or.** sorters th
1a945 61 74 20 77 65 72 65 20 6c 65 66 74 20 6f 70 65  at were left ope
1a946 6e 2e 20 20 49 74 20 61 6c 73 6f 20 64 65 6c 65  n.  It also dele
1a947 74 65 73 20 74 68 65 20 76 61 6c 75 65 73 20 6f  tes the values o
1a948 66 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 69  f.** variables i
1a949 6e 20 74 68 65 20 61 56 61 72 5b 5d 20 61 72 72  n the aVar[] arr
1a94a 61 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ay..*/.static vo
1a94b 69 64 20 43 6c 65 61 6e 75 70 28 56 64 62 65 20  id Cleanup(Vdbe 
1a94c 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  *p){.  sqlite3 *
1a94d 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 23 69 66  db = p->db;..#if
1a94e 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
1a94f 0a 20 20 2f 2a 20 45 78 65 63 75 74 65 20 61 73  .  /* Execute as
1a950 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
1a951 73 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74  s to ensure that
1a952 20 74 68 65 20 56 64 62 65 2e 61 70 43 73 72 5b   the Vdbe.apCsr[
1a953 5d 20 61 6e 64 20 0a 20 20 2a 2a 20 56 64 62 65  ] and .  ** Vdbe
1a954 2e 61 4d 65 6d 5b 5d 20 61 72 72 61 79 73 20 68  .aMem[] arrays h
1a955 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65 6e  ave already been
1a956 20 63 6c 65 61 6e 65 64 20 75 70 2e 20 20 2a 2f   cleaned up.  */
1a957 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28  .  int i;.  for(
1a958 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 75 72 73 6f  i=0; i<p->nCurso
1a959 72 3b 20 69 2b 2b 29 20 61 73 73 65 72 74 28 20  r; i++) assert( 
1a95a 70 2d 3e 61 70 43 73 72 3d 3d 30 20 7c 7c 20 70  p->apCsr==0 || p
1a95b 2d 3e 61 70 43 73 72 5b 69 5d 3d 3d 30 20 29 3b  ->apCsr[i]==0 );
1a95c 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 70  .  for(i=1; i<=p
1a95d 2d 3e 6e 4d 65 6d 3b 20 69 2b 2b 29 20 61 73 73  ->nMem; i++) ass
1a95e 65 72 74 28 20 70 2d 3e 61 4d 65 6d 3d 3d 30 20  ert( p->aMem==0 
1a95f 7c 7c 20 70 2d 3e 61 4d 65 6d 5b 69 5d 2e 66 6c  || p->aMem[i].fl
1a960 61 67 73 3d 3d 4d 45 4d 5f 4e 75 6c 6c 20 29 3b  ags==MEM_Null );
1a961 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74  .#endif..  sqlit
1a962 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
1a963 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a  zErrMsg);.  p->z
1a964 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 70 2d  ErrMsg = 0;.  p-
1a965 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 30 3b  >pResultSet = 0;
1a966 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
1a967 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 73 75  e number of resu
1a968 6c 74 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20  lt columns that 
1a969 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64  will be returned
1a96a 20 62 79 20 74 68 69 73 20 53 51 4c 0a 2a 2a 20   by this SQL.** 
1a96b 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20  statement. This 
1a96c 69 73 20 6e 6f 77 20 73 65 74 20 61 74 20 63 6f  is now set at co
1a96d 6d 70 69 6c 65 20 74 69 6d 65 2c 20 72 61 74 68  mpile time, rath
1a96e 65 72 20 74 68 61 6e 20 64 75 72 69 6e 67 0a 2a  er than during.*
1a96f 2a 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74  * execution of t
1a970 68 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20  he vdbe program 
1a971 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f  so that sqlite3_
1a972 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 29 20 63  column_count() c
1a973 61 6e 0a 2a 2a 20 62 65 20 63 61 6c 6c 65 64 20  an.** be called 
1a974 6f 6e 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d  on an SQL statem
1a975 65 6e 74 20 62 65 66 6f 72 65 20 73 71 6c 69 74  ent before sqlit
1a976 65 33 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a 53 51  e3_step()..*/.SQ
1a977 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1a978 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74  d sqlite3VdbeSet
1a979 4e 75 6d 43 6f 6c 73 28 56 64 62 65 20 2a 70 2c  NumCols(Vdbe *p,
1a97a 20 69 6e 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 29   int nResColumn)
1a97b 7b 0a 20 20 4d 65 6d 20 2a 70 43 6f 6c 4e 61 6d  {.  Mem *pColNam
1a97c 65 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71  e;.  int n;.  sq
1a97d 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64  lite3 *db = p->d
1a97e 62 3b 0a 0a 20 20 72 65 6c 65 61 73 65 4d 65 6d  b;..  releaseMem
1a97f 41 72 72 61 79 28 70 2d 3e 61 43 6f 6c 4e 61 6d  Array(p->aColNam
1a980 65 2c 20 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e  e, p->nResColumn
1a981 2a 43 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a 20 20 73  *COLNAME_N);.  s
1a982 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1a983 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20   p->aColName);. 
1a984 20 6e 20 3d 20 6e 52 65 73 43 6f 6c 75 6d 6e 2a   n = nResColumn*
1a985 43 4f 4c 4e 41 4d 45 5f 4e 3b 0a 20 20 70 2d 3e  COLNAME_N;.  p->
1a986 6e 52 65 73 43 6f 6c 75 6d 6e 20 3d 20 28 75 31  nResColumn = (u1
1a987 36 29 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a 20 20  6)nResColumn;.  
1a988 70 2d 3e 61 43 6f 6c 4e 61 6d 65 20 3d 20 70 43  p->aColName = pC
1a989 6f 6c 4e 61 6d 65 20 3d 20 28 4d 65 6d 2a 29 73  olName = (Mem*)s
1a98a 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
1a98b 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 4d 65  ro(db, sizeof(Me
1a98c 6d 29 2a 6e 20 29 3b 0a 20 20 69 66 28 20 70 2d  m)*n );.  if( p-
1a98d 3e 61 43 6f 6c 4e 61 6d 65 3d 3d 30 20 29 20 72  >aColName==0 ) r
1a98e 65 74 75 72 6e 3b 0a 20 20 77 68 69 6c 65 28 20  eturn;.  while( 
1a98f 6e 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 70  n-- > 0 ){.    p
1a990 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 20 3d  ColName->flags =
1a991 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70   MEM_Null;.    p
1a992 43 6f 6c 4e 61 6d 65 2d 3e 64 62 20 3d 20 70 2d  ColName->db = p-
1a993 3e 64 62 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d  >db;.    pColNam
1a994 65 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  e++;.  }.}../*.*
1a995 2a 20 53 65 74 20 74 68 65 20 6e 61 6d 65 20 6f  * Set the name o
1a996 66 20 74 68 65 20 69 64 78 27 74 68 20 63 6f 6c  f the idx'th col
1a997 75 6d 6e 20 74 6f 20 62 65 20 72 65 74 75 72 6e  umn to be return
1a998 65 64 20 62 79 20 74 68 65 20 53 51 4c 20 73 74  ed by the SQL st
1a999 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d  atement..** zNam
1a99a 65 20 6d 75 73 74 20 62 65 20 61 20 70 6f 69 6e  e must be a poin
1a99b 74 65 72 20 74 6f 20 61 20 6e 75 6c 20 74 65 72  ter to a nul ter
1a99c 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 2e 0a  minated string..
1a99d 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20  **.** This call 
1a99e 6d 75 73 74 20 62 65 20 6d 61 64 65 20 61 66 74  must be made aft
1a99f 65 72 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c  er a call to sql
1a9a0 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
1a9a1 6c 73 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ls()..**.** The 
1a9a2 66 69 6e 61 6c 20 70 61 72 61 6d 65 74 65 72 2c  final parameter,
1a9a3 20 78 44 65 6c 2c 20 6d 75 73 74 20 62 65 20 6f   xDel, must be o
1a9a4 6e 65 20 6f 66 20 53 51 4c 49 54 45 5f 44 59 4e  ne of SQLITE_DYN
1a9a5 41 4d 49 43 2c 20 53 51 4c 49 54 45 5f 53 54 41  AMIC, SQLITE_STA
1a9a6 54 49 43 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45  TIC.** or SQLITE
1a9a7 5f 54 52 41 4e 53 49 45 4e 54 2e 20 49 66 20 69  _TRANSIENT. If i
1a9a8 74 20 69 73 20 53 51 4c 49 54 45 5f 44 59 4e 41  t is SQLITE_DYNA
1a9a9 4d 49 43 2c 20 74 68 65 6e 20 74 68 65 20 62 75  MIC, then the bu
1a9aa 66 66 65 72 20 70 6f 69 6e 74 65 64 0a 2a 2a 20  ffer pointed.** 
1a9ab 74 6f 20 62 79 20 7a 4e 61 6d 65 20 77 69 6c 6c  to by zName will
1a9ac 20 62 65 20 66 72 65 65 64 20 62 79 20 73 71 6c   be freed by sql
1a9ad 69 74 65 33 44 62 46 72 65 65 28 29 20 77 68 65  ite3DbFree() whe
1a9ae 6e 20 74 68 65 20 76 64 62 65 20 69 73 20 64 65  n the vdbe is de
1a9af 73 74 72 6f 79 65 64 2e 0a 2a 2f 0a 53 51 4c 49  stroyed..*/.SQLI
1a9b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1a9b1 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
1a9b2 4e 61 6d 65 28 0a 20 20 56 64 62 65 20 2a 70 2c  Name(.  Vdbe *p,
1a9b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a9b4 20 20 20 20 20 20 20 20 20 2f 2a 20 56 64 62 65           /* Vdbe
1a9b5 20 62 65 69 6e 67 20 63 6f 6e 66 69 67 75 72 65   being configure
1a9b6 64 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 2c 20  d */.  int idx, 
1a9b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a9b8 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
1a9b9 20 6f 66 20 63 6f 6c 75 6d 6e 20 7a 4e 61 6d 65   of column zName
1a9ba 20 61 70 70 6c 69 65 73 20 74 6f 20 2a 2f 0a 20   applies to */. 
1a9bb 20 69 6e 74 20 76 61 72 2c 20 20 20 20 20 20 20   int var,       
1a9bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a9bd 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20    /* One of the 
1a9be 43 4f 4c 4e 41 4d 45 5f 2a 20 63 6f 6e 73 74 61  COLNAME_* consta
1a9bf 6e 74 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  nts */.  const c
1a9c0 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20  har *zName,     
1a9c1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
1a9c2 6e 74 65 72 20 74 6f 20 62 75 66 66 65 72 20 63  nter to buffer c
1a9c3 6f 6e 74 61 69 6e 69 6e 67 20 6e 61 6d 65 20 2a  ontaining name *
1a9c4 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29  /.  void (*xDel)
1a9c5 28 76 6f 69 64 2a 29 20 20 20 20 20 20 20 20 20  (void*)         
1a9c6 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6d       /* Memory m
1a9c7 61 6e 61 67 65 6d 65 6e 74 20 73 74 72 61 74 65  anagement strate
1a9c8 67 79 20 66 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a  gy for zName */.
1a9c9 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d  ){.  int rc;.  M
1a9ca 65 6d 20 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20  em *pColName;.  
1a9cb 61 73 73 65 72 74 28 20 69 64 78 3c 70 2d 3e 6e  assert( idx<p->n
1a9cc 52 65 73 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 61  ResColumn );.  a
1a9cd 73 73 65 72 74 28 20 76 61 72 3c 43 4f 4c 4e 41  ssert( var<COLNA
1a9ce 4d 45 5f 4e 20 29 3b 0a 20 20 69 66 28 20 70 2d  ME_N );.  if( p-
1a9cf 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
1a9d0 64 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  d ){.    assert(
1a9d1 20 21 7a 4e 61 6d 65 20 7c 7c 20 78 44 65 6c 21   !zName || xDel!
1a9d2 3d 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20  =SQLITE_DYNAMIC 
1a9d3 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
1a9d4 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
1a9d5 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 43 6f    assert( p->aCo
1a9d6 6c 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20 70 43  lName!=0 );.  pC
1a9d7 6f 6c 4e 61 6d 65 20 3d 20 26 28 70 2d 3e 61 43  olName = &(p->aC
1a9d8 6f 6c 4e 61 6d 65 5b 69 64 78 2b 76 61 72 2a 70  olName[idx+var*p
1a9d9 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 5d 29 3b 0a  ->nResColumn]);.
1a9da 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1a9db 62 65 4d 65 6d 53 65 74 53 74 72 28 70 43 6f 6c  beMemSetStr(pCol
1a9dc 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c  Name, zName, -1,
1a9dd 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 78 44   SQLITE_UTF8, xD
1a9de 65 6c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  el);.  assert( r
1a9df 63 21 3d 30 20 7c 7c 20 21 7a 4e 61 6d 65 20 7c  c!=0 || !zName |
1a9e0 7c 20 28 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61  | (pColName->fla
1a9e1 67 73 26 4d 45 4d 5f 54 65 72 6d 29 21 3d 30 20  gs&MEM_Term)!=0 
1a9e2 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1a9e3 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 72 65 61 64 20  }../*.** A read 
1a9e4 6f 72 20 77 72 69 74 65 20 74 72 61 6e 73 61 63  or write transac
1a9e5 74 69 6f 6e 20 6d 61 79 20 6f 72 20 6d 61 79 20  tion may or may 
1a9e6 6e 6f 74 20 62 65 20 61 63 74 69 76 65 20 6f 6e  not be active on
1a9e7 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
1a9e8 0a 2a 2a 20 64 62 2e 20 49 66 20 61 20 74 72 61  .** db. If a tra
1a9e9 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69  nsaction is acti
1a9ea 76 65 2c 20 63 6f 6d 6d 69 74 20 69 74 2e 20 49  ve, commit it. I
1a9eb 66 20 74 68 65 72 65 20 69 73 20 61 0a 2a 2a 20  f there is a.** 
1a9ec 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
1a9ed 6e 20 73 70 61 6e 6e 69 6e 67 20 6d 6f 72 65 20  n spanning more 
1a9ee 74 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73  than one databas
1a9ef 65 20 66 69 6c 65 2c 20 74 68 69 73 20 72 6f 75  e file, this rou
1a9f0 74 69 6e 65 0a 2a 2a 20 74 61 6b 65 73 20 63 61  tine.** takes ca
1a9f1 72 65 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72  re of the master
1a9f2 20 6a 6f 75 72 6e 61 6c 20 74 72 69 63 6b 65 72   journal tricker
1a9f3 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  y..*/.static int
1a9f4 20 76 64 62 65 43 6f 6d 6d 69 74 28 73 71 6c 69   vdbeCommit(sqli
1a9f5 74 65 33 20 2a 64 62 2c 20 56 64 62 65 20 2a 70  te3 *db, Vdbe *p
1a9f6 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
1a9f7 74 20 6e 54 72 61 6e 73 20 3d 20 30 3b 20 20 2f  t nTrans = 0;  /
1a9f8 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 61 74 61  * Number of data
1a9f9 62 61 73 65 73 20 77 69 74 68 20 61 6e 20 61 63  bases with an ac
1a9fa 74 69 76 65 20 77 72 69 74 65 2d 74 72 61 6e 73  tive write-trans
1a9fb 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  action */.  int 
1a9fc 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1a9fd 20 20 69 6e 74 20 6e 65 65 64 58 63 6f 6d 6d 69    int needXcommi
1a9fe 74 20 3d 20 30 3b 0a 0a 23 69 66 64 65 66 20 53  t = 0;..#ifdef S
1a9ff 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1aa00 41 4c 54 41 42 4c 45 0a 20 20 2f 2a 20 57 69 74  ALTABLE.  /* Wit
1aa01 68 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 73  h this option, s
1aa02 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 29  qlite3VtabSync()
1aa03 20 69 73 20 64 65 66 69 6e 65 64 20 74 6f 20 62   is defined to b
1aa04 65 20 73 69 6d 70 6c 79 20 0a 20 20 2a 2a 20 53  e simply .  ** S
1aa05 51 4c 49 54 45 5f 4f 4b 20 73 6f 20 70 20 69 73  QLITE_OK so p is
1aa06 20 6e 6f 74 20 75 73 65 64 2e 20 0a 20 20 2a 2f   not used. .  */
1aa07 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
1aa08 54 45 52 28 70 29 3b 0a 23 65 6e 64 69 66 0a 0a  TER(p);.#endif..
1aa09 20 20 2f 2a 20 42 65 66 6f 72 65 20 64 6f 69 6e    /* Before doin
1aa0a 67 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 2c  g anything else,
1aa0b 20 63 61 6c 6c 20 74 68 65 20 78 53 79 6e 63 28   call the xSync(
1aa0c 29 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61  ) callback for a
1aa0d 6e 79 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20  ny.  ** virtual 
1aa0e 6d 6f 64 75 6c 65 20 74 61 62 6c 65 73 20 77 72  module tables wr
1aa0f 69 74 74 65 6e 20 69 6e 20 74 68 69 73 20 74 72  itten in this tr
1aa10 61 6e 73 61 63 74 69 6f 6e 2e 20 54 68 69 73 20  ansaction. This 
1aa11 68 61 73 20 74 6f 0a 20 20 2a 2a 20 62 65 20 64  has to.  ** be d
1aa12 6f 6e 65 20 62 65 66 6f 72 65 20 64 65 74 65 72  one before deter
1aa13 6d 69 6e 69 6e 67 20 77 68 65 74 68 65 72 20 61  mining whether a
1aa14 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1aa15 66 69 6c 65 20 69 73 20 0a 20 20 2a 2a 20 72 65  file is .  ** re
1aa16 71 75 69 72 65 64 2c 20 61 73 20 61 6e 20 78 53  quired, as an xS
1aa17 79 6e 63 28 29 20 63 61 6c 6c 62 61 63 6b 20 6d  ync() callback m
1aa18 61 79 20 61 64 64 20 61 6e 20 61 74 74 61 63 68  ay add an attach
1aa19 65 64 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a  ed database.  **
1aa1a 20 74 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74   to the transact
1aa1b 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d  ion..  */.  rc =
1aa1c 20 73 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63   sqlite3VtabSync
1aa1d 28 64 62 2c 20 26 70 2d 3e 7a 45 72 72 4d 73 67  (db, &p->zErrMsg
1aa1e 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
1aa1f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
1aa20 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
1aa21 2f 2a 20 54 68 69 73 20 6c 6f 6f 70 20 64 65 74  /* This loop det
1aa22 65 72 6d 69 6e 65 73 20 28 61 29 20 69 66 20 74  ermines (a) if t
1aa23 68 65 20 63 6f 6d 6d 69 74 20 68 6f 6f 6b 20 73  he commit hook s
1aa24 68 6f 75 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64  hould be invoked
1aa25 20 61 6e 64 0a 20 20 2a 2a 20 28 62 29 20 68 6f   and.  ** (b) ho
1aa26 77 20 6d 61 6e 79 20 64 61 74 61 62 61 73 65 20  w many database 
1aa27 66 69 6c 65 73 20 68 61 76 65 20 6f 70 65 6e 20  files have open 
1aa28 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
1aa29 6e 73 2c 20 6e 6f 74 20 0a 20 20 2a 2a 20 69 6e  ns, not .  ** in
1aa2a 63 6c 75 64 69 6e 67 20 74 68 65 20 74 65 6d 70  cluding the temp
1aa2b 20 64 61 74 61 62 61 73 65 2e 20 28 62 29 20 69   database. (b) i
1aa2c 73 20 69 6d 70 6f 72 74 61 6e 74 20 62 65 63 61  s important beca
1aa2d 75 73 65 20 69 66 20 6d 6f 72 65 20 74 68 61 6e  use if more than
1aa2e 20 0a 20 20 2a 2a 20 6f 6e 65 20 64 61 74 61 62   .  ** one datab
1aa2f 61 73 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20  ase file has an 
1aa30 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73  open write trans
1aa31 61 63 74 69 6f 6e 2c 20 61 20 6d 61 73 74 65 72  action, a master
1aa32 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a 2a 20 66 69   journal.  ** fi
1aa33 6c 65 20 69 73 20 72 65 71 75 69 72 65 64 20 66  le is required f
1aa34 6f 72 20 61 6e 20 61 74 6f 6d 69 63 20 63 6f 6d  or an atomic com
1aa35 6d 69 74 2e 0a 20 20 2a 2f 20 0a 20 20 66 6f 72  mit..  */ .  for
1aa36 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
1aa37 20 69 2b 2b 29 7b 20 0a 20 20 20 20 42 74 72 65   i++){ .    Btre
1aa38 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62  e *pBt = db->aDb
1aa39 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28  [i].pBt;.    if(
1aa3a 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49   sqlite3BtreeIsI
1aa3b 6e 54 72 61 6e 73 28 70 42 74 29 20 29 7b 0a 20  nTrans(pBt) ){. 
1aa3c 20 20 20 20 20 6e 65 65 64 58 63 6f 6d 6d 69 74       needXcommit
1aa3d 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 66 28 20   = 1;.      if( 
1aa3e 69 21 3d 31 20 29 20 6e 54 72 61 6e 73 2b 2b 3b  i!=1 ) nTrans++;
1aa3f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
1aa40 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61 6e   If there are an
1aa41 79 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74  y write-transact
1aa42 69 6f 6e 73 20 61 74 20 61 6c 6c 2c 20 69 6e 76  ions at all, inv
1aa43 6f 6b 65 20 74 68 65 20 63 6f 6d 6d 69 74 20 68  oke the commit h
1aa44 6f 6f 6b 20 2a 2f 0a 20 20 69 66 28 20 6e 65 65  ook */.  if( nee
1aa45 64 58 63 6f 6d 6d 69 74 20 26 26 20 64 62 2d 3e  dXcommit && db->
1aa46 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 20  xCommitCallback 
1aa47 29 7b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c  ){.    (void)sql
1aa48 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
1aa49 29 3b 0a 20 20 20 20 72 63 20 3d 20 64 62 2d 3e  );.    rc = db->
1aa4a 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 28  xCommitCallback(
1aa4b 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 29 3b  db->pCommitArg);
1aa4c 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74  .    (void)sqlit
1aa4d 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a  e3SafetyOn(db);.
1aa4e 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
1aa4f 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1aa50 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20  E_CONSTRAINT;.  
1aa51 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68    }.  }..  /* Th
1aa52 65 20 73 69 6d 70 6c 65 20 63 61 73 65 20 2d 20  e simple case - 
1aa53 6e 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  no more than one
1aa54 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28   database file (
1aa55 6e 6f 74 20 63 6f 75 6e 74 69 6e 67 20 74 68 65  not counting the
1aa56 0a 20 20 2a 2a 20 54 45 4d 50 20 64 61 74 61 62  .  ** TEMP datab
1aa57 61 73 65 29 20 68 61 73 20 61 20 74 72 61 6e 73  ase) has a trans
1aa58 61 63 74 69 6f 6e 20 61 63 74 69 76 65 2e 20 20  action active.  
1aa59 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65   There is no nee
1aa5a 64 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6d  d for the.  ** m
1aa5b 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 2e 0a 20  aster-journal.. 
1aa5c 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65 20   **.  ** If the 
1aa5d 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20  return value of 
1aa5e 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 46  sqlite3BtreeGetF
1aa5f 69 6c 65 6e 61 6d 65 28 29 20 69 73 20 61 20 7a  ilename() is a z
1aa60 65 72 6f 20 6c 65 6e 67 74 68 0a 20 20 2a 2a 20  ero length.  ** 
1aa61 73 74 72 69 6e 67 2c 20 69 74 20 6d 65 61 6e 73  string, it means
1aa62 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
1aa63 73 65 20 69 73 20 3a 6d 65 6d 6f 72 79 3a 20 6f  se is :memory: o
1aa64 72 20 61 20 74 65 6d 70 20 66 69 6c 65 2e 20 20  r a temp file.  
1aa65 49 6e 20 0a 20 20 2a 2a 20 74 68 61 74 20 63 61  In .  ** that ca
1aa66 73 65 20 77 65 20 64 6f 20 6e 6f 74 20 73 75 70  se we do not sup
1aa67 70 6f 72 74 20 61 74 6f 6d 69 63 20 6d 75 6c 74  port atomic mult
1aa68 69 2d 66 69 6c 65 20 63 6f 6d 6d 69 74 73 2c 20  i-file commits, 
1aa69 73 6f 20 75 73 65 20 74 68 65 20 0a 20 20 2a 2a  so use the .  **
1aa6a 20 73 69 6d 70 6c 65 20 63 61 73 65 20 74 68 65   simple case the
1aa6b 6e 20 74 6f 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66  n too..  */.  if
1aa6c 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 6c  ( 0==sqlite3Strl
1aa6d 65 6e 33 30 28 73 71 6c 69 74 65 33 42 74 72 65  en30(sqlite3Btre
1aa6e 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 64 62 2d  eGetFilename(db-
1aa6f 3e 61 44 62 5b 30 5d 2e 70 42 74 29 29 0a 20 20  >aDb[0].pBt)).  
1aa70 20 7c 7c 20 6e 54 72 61 6e 73 3c 3d 31 0a 20 20   || nTrans<=1.  
1aa71 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ){.    for(i=0; 
1aa72 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1aa73 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
1aa74 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70  {.      Btree *p
1aa75 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e  Bt = db->aDb[i].
1aa76 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70  pBt;.      if( p
1aa77 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  Bt ){.        rc
1aa78 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43   = sqlite3BtreeC
1aa79 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 42  ommitPhaseOne(pB
1aa7a 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20  t, 0);.      }. 
1aa7b 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 6f 20     }..    /* Do 
1aa7c 74 68 65 20 63 6f 6d 6d 69 74 20 6f 6e 6c 79 20  the commit only 
1aa7d 69 66 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73  if all databases
1aa7e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f   successfully co
1aa7f 6d 70 6c 65 74 65 20 70 68 61 73 65 20 31 2e 20  mplete phase 1. 
1aa80 0a 20 20 20 20 2a 2a 20 49 66 20 6f 6e 65 20 6f  .    ** If one o
1aa81 66 20 74 68 65 20 42 74 72 65 65 43 6f 6d 6d 69  f the BtreeCommi
1aa82 74 50 68 61 73 65 4f 6e 65 28 29 20 63 61 6c 6c  tPhaseOne() call
1aa83 73 20 66 61 69 6c 73 2c 20 74 68 69 73 20 69 6e  s fails, this in
1aa84 64 69 63 61 74 65 73 20 61 6e 0a 20 20 20 20 2a  dicates an.    *
1aa85 2a 20 49 4f 20 65 72 72 6f 72 20 77 68 69 6c 65  * IO error while
1aa86 20 64 65 6c 65 74 69 6e 67 20 6f 72 20 74 72 75   deleting or tru
1aa87 6e 63 61 74 69 6e 67 20 61 20 6a 6f 75 72 6e 61  ncating a journa
1aa88 6c 20 66 69 6c 65 2e 20 49 74 20 69 73 20 75 6e  l file. It is un
1aa89 6c 69 6b 65 6c 79 2c 0a 20 20 20 20 2a 2a 20 62  likely,.    ** b
1aa8a 75 74 20 63 6f 75 6c 64 20 68 61 70 70 65 6e 2e  ut could happen.
1aa8b 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 62   In this case ab
1aa8c 61 6e 64 6f 6e 20 70 72 6f 63 65 73 73 69 6e 67  andon processing
1aa8d 20 61 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20   and return the 
1aa8e 65 72 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20  error..    */.  
1aa8f 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53    for(i=0; rc==S
1aa90 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64 62  QLITE_OK && i<db
1aa91 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
1aa92 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20     Btree *pBt = 
1aa93 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a  db->aDb[i].pBt;.
1aa94 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29 7b        if( pBt ){
1aa95 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
1aa96 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74  lite3BtreeCommit
1aa97 50 68 61 73 65 54 77 6f 28 70 42 74 29 3b 0a 20  PhaseTwo(pBt);. 
1aa98 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1aa99 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1aa9a 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  OK ){.      sqli
1aa9b 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28 64 62  te3VtabCommit(db
1aa9c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
1aa9d 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 78 20 63  /* The complex c
1aa9e 61 73 65 20 2d 20 54 68 65 72 65 20 69 73 20 61  ase - There is a
1aa9f 20 6d 75 6c 74 69 2d 66 69 6c 65 20 77 72 69 74   multi-file writ
1aaa0 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 63  e-transaction ac
1aaa1 74 69 76 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20  tive..  ** This 
1aaa2 72 65 71 75 69 72 65 73 20 61 20 6d 61 73 74 65  requires a maste
1aaa3 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74  r journal file t
1aaa4 6f 20 65 6e 73 75 72 65 20 74 68 65 20 74 72 61  o ensure the tra
1aaa5 6e 73 61 63 74 69 6f 6e 20 69 73 0a 20 20 2a 2a  nsaction is.  **
1aaa6 20 63 6f 6d 6d 69 74 74 65 64 20 61 74 6f 6d 69   committed atomi
1aaa7 63 6c 79 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65  cly..  */.#ifnde
1aaa8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 49  f SQLITE_OMIT_DI
1aaa9 53 4b 49 4f 0a 20 20 65 6c 73 65 7b 0a 20 20 20  SKIO.  else{.   
1aaaa 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
1aaab 66 73 20 3d 20 64 62 2d 3e 70 56 66 73 3b 0a 20  fs = db->pVfs;. 
1aaac 20 20 20 69 6e 74 20 6e 65 65 64 53 79 6e 63 20     int needSync 
1aaad 3d 20 30 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a  = 0;.    char *z
1aaae 4d 61 73 74 65 72 20 3d 20 30 3b 20 20 20 2f 2a  Master = 0;   /*
1aaaf 20 46 69 6c 65 2d 6e 61 6d 65 20 66 6f 72 20 74   File-name for t
1aab0 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
1aab1 6c 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 63 6f  l */.    char co
1aab2 6e 73 74 20 2a 7a 4d 61 69 6e 46 69 6c 65 20 3d  nst *zMainFile =
1aab3 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
1aab4 46 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62  Filename(db->aDb
1aab5 5b 30 5d 2e 70 42 74 29 3b 0a 20 20 20 20 73 71  [0].pBt);.    sq
1aab6 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4d 61 73  lite3_file *pMas
1aab7 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 69 36 34  ter = 0;.    i64
1aab8 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20   offset = 0;.   
1aab9 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20 20 20 2f   int res;..    /
1aaba 2a 20 53 65 6c 65 63 74 20 61 20 6d 61 73 74 65  * Select a maste
1aabb 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e  r journal file n
1aabc 61 6d 65 20 2a 2f 0a 20 20 20 20 64 6f 20 7b 0a  ame */.    do {.
1aabd 20 20 20 20 20 20 75 33 32 20 69 52 61 6e 64 6f        u32 iRando
1aabe 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  m;.      sqlite3
1aabf 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74  DbFree(db, zMast
1aac0 65 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  er);.      sqlit
1aac1 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69  e3_randomness(si
1aac2 7a 65 6f 66 28 69 52 61 6e 64 6f 6d 29 2c 20 26  zeof(iRandom), &
1aac3 69 52 61 6e 64 6f 6d 29 3b 0a 20 20 20 20 20 20  iRandom);.      
1aac4 7a 4d 61 73 74 65 72 20 3d 20 73 71 6c 69 74 65  zMaster = sqlite
1aac5 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73  3MPrintf(db, "%s
1aac6 2d 6d 6a 25 30 38 58 22 2c 20 7a 4d 61 69 6e 46  -mj%08X", zMainF
1aac7 69 6c 65 2c 20 69 52 61 6e 64 6f 6d 26 30 78 37  ile, iRandom&0x7
1aac8 66 66 66 66 66 66 66 29 3b 0a 20 20 20 20 20 20  fffffff);.      
1aac9 69 66 28 20 21 7a 4d 61 73 74 65 72 20 29 7b 0a  if( !zMaster ){.
1aaca 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
1aacb 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
1aacc 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20     }.      rc = 
1aacd 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28  sqlite3OsAccess(
1aace 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 53  pVfs, zMaster, S
1aacf 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49  QLITE_ACCESS_EXI
1aad0 53 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 20 20  STS, &res);.    
1aad1 7d 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49  }while( rc==SQLI
1aad2 54 45 5f 4f 4b 20 26 26 20 72 65 73 20 29 3b 0a  TE_OK && res );.
1aad3 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1aad4 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 2f  TE_OK ){.      /
1aad5 2a 20 4f 70 65 6e 20 74 68 65 20 6d 61 73 74 65  * Open the maste
1aad6 72 20 6a 6f 75 72 6e 61 6c 2e 20 2a 2f 0a 20 20  r journal. */.  
1aad7 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1aad8 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63 28 70 56 66  OsOpenMalloc(pVf
1aad9 73 2c 20 7a 4d 61 73 74 65 72 2c 20 26 70 4d 61  s, zMaster, &pMa
1aada 73 74 65 72 2c 20 0a 20 20 20 20 20 20 20 20 20  ster, .         
1aadb 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
1aadc 44 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50  DWRITE|SQLITE_OP
1aadd 45 4e 5f 43 52 45 41 54 45 7c 0a 20 20 20 20 20  EN_CREATE|.     
1aade 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
1aadf 5f 45 58 43 4c 55 53 49 56 45 7c 53 51 4c 49 54  _EXCLUSIVE|SQLIT
1aae0 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f  E_OPEN_MASTER_JO
1aae1 55 52 4e 41 4c 2c 20 30 0a 20 20 20 20 20 20 29  URNAL, 0.      )
1aae2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1aae3 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1aae4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
1aae5 46 72 65 65 28 64 62 2c 20 7a 4d 61 73 74 65 72  Free(db, zMaster
1aae6 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1aae7 72 63 3b 0a 20 20 20 20 7d 0a 20 0a 20 20 20 20  rc;.    }. .    
1aae8 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 61 6d  /* Write the nam
1aae9 65 20 6f 66 20 65 61 63 68 20 64 61 74 61 62 61  e of each databa
1aaea 73 65 20 66 69 6c 65 20 69 6e 20 74 68 65 20 74  se file in the t
1aaeb 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 74 6f 20  ransaction into 
1aaec 74 68 65 20 6e 65 77 0a 20 20 20 20 2a 2a 20 6d  the new.    ** m
1aaed 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
1aaee 6c 65 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  le. If an error 
1aaef 6f 63 63 75 72 73 20 61 74 20 74 68 69 73 20 70  occurs at this p
1aaf0 6f 69 6e 74 20 63 6c 6f 73 65 0a 20 20 20 20 2a  oint close.    *
1aaf1 2a 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68 65  * and delete the
1aaf2 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1aaf3 66 69 6c 65 2e 20 41 6c 6c 20 74 68 65 20 69 6e  file. All the in
1aaf4 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c  dividual journal
1aaf5 20 66 69 6c 65 73 0a 20 20 20 20 2a 2a 20 73 74   files.    ** st
1aaf6 69 6c 6c 20 68 61 76 65 20 27 6e 75 6c 6c 27 20  ill have 'null' 
1aaf7 61 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  as the master jo
1aaf8 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72 2c 20 73  urnal pointer, s
1aaf9 6f 20 74 68 65 79 20 77 69 6c 6c 20 72 6f 6c 6c  o they will roll
1aafa 0a 20 20 20 20 2a 2a 20 62 61 63 6b 20 69 6e 64  .    ** back ind
1aafb 65 70 65 6e 64 65 6e 74 6c 79 20 69 66 20 61 20  ependently if a 
1aafc 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 2e 0a  failure occurs..
1aafd 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69      */.    for(i
1aafe 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
1aaff 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65  ++){.      Btree
1ab00 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b   *pBt = db->aDb[
1ab01 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66  i].pBt;.      if
1ab02 28 20 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75  ( i==1 ) continu
1ab03 65 3b 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74  e;   /* Ignore t
1ab04 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65  he TEMP database
1ab05 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73 71   */.      if( sq
1ab06 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72  lite3BtreeIsInTr
1ab07 61 6e 73 28 70 42 74 29 20 29 7b 0a 20 20 20 20  ans(pBt) ){.    
1ab08 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a      char const *
1ab09 7a 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 42  zFile = sqlite3B
1ab0a 74 72 65 65 47 65 74 4a 6f 75 72 6e 61 6c 6e 61  treeGetJournalna
1ab0b 6d 65 28 70 42 74 29 3b 0a 20 20 20 20 20 20 20  me(pBt);.       
1ab0c 20 69 66 28 20 7a 46 69 6c 65 5b 30 5d 3d 3d 30   if( zFile[0]==0
1ab0d 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a   ) continue;  /*
1ab0e 20 49 67 6e 6f 72 65 20 3a 6d 65 6d 6f 72 79 3a   Ignore :memory:
1ab0f 20 64 61 74 61 62 61 73 65 73 20 2a 2f 0a 20 20   databases */.  
1ab10 20 20 20 20 20 20 69 66 28 20 21 6e 65 65 64 53        if( !needS
1ab11 79 6e 63 20 26 26 20 21 73 71 6c 69 74 65 33 42  ync && !sqlite3B
1ab12 74 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65 64  treeSyncDisabled
1ab13 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20  (pBt) ){.       
1ab14 20 20 20 6e 65 65 64 53 79 6e 63 20 3d 20 31 3b     needSync = 1;
1ab15 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1ab16 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
1ab17 73 57 72 69 74 65 28 70 4d 61 73 74 65 72 2c 20  sWrite(pMaster, 
1ab18 7a 46 69 6c 65 2c 20 73 71 6c 69 74 65 33 53 74  zFile, sqlite3St
1ab19 72 6c 65 6e 33 30 28 7a 46 69 6c 65 29 2b 31 2c  rlen30(zFile)+1,
1ab1a 20 6f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20   offset);.      
1ab1b 20 20 6f 66 66 73 65 74 20 2b 3d 20 73 71 6c 69    offset += sqli
1ab1c 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 46 69 6c  te3Strlen30(zFil
1ab1d 65 29 2b 31 3b 0a 20 20 20 20 20 20 20 20 69 66  e)+1;.        if
1ab1e 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1ab1f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
1ab20 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65 28  ite3OsCloseFree(
1ab21 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20  pMaster);.      
1ab22 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c      sqlite3OsDel
1ab23 65 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65  ete(pVfs, zMaste
1ab24 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  r, 0);.         
1ab25 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1ab26 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20  b, zMaster);.   
1ab27 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
1ab28 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1ab29 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
1ab2a 2a 20 53 79 6e 63 20 74 68 65 20 6d 61 73 74 65  * Sync the maste
1ab2b 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  r journal file. 
1ab2c 49 66 20 74 68 65 20 49 4f 43 41 50 5f 53 45 51  If the IOCAP_SEQ
1ab2d 55 45 4e 54 49 41 4c 20 64 65 76 69 63 65 0a 20  UENTIAL device. 
1ab2e 20 20 20 2a 2a 20 66 6c 61 67 20 69 73 20 73 65     ** flag is se
1ab2f 74 20 74 68 69 73 20 69 73 20 6e 6f 74 20 72 65  t this is not re
1ab30 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  quired..    */. 
1ab31 20 20 20 69 66 28 20 6e 65 65 64 53 79 6e 63 20     if( needSync 
1ab32 0a 20 20 20 20 20 26 26 20 30 3d 3d 28 73 71 6c  .     && 0==(sql
1ab33 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72  ite3OsDeviceChar
1ab34 61 63 74 65 72 69 73 74 69 63 73 28 70 4d 61 73  acteristics(pMas
1ab35 74 65 72 29 26 53 51 4c 49 54 45 5f 49 4f 43 41  ter)&SQLITE_IOCA
1ab36 50 5f 53 45 51 55 45 4e 54 49 41 4c 29 0a 20 20  P_SEQUENTIAL).  
1ab37 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 21     && SQLITE_OK!
1ab38 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73  =(rc = sqlite3Os
1ab39 53 79 6e 63 28 70 4d 61 73 74 65 72 2c 20 53 51  Sync(pMaster, SQ
1ab3a 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c  LITE_SYNC_NORMAL
1ab3b 29 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  )).    ){.      
1ab3c 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72  sqlite3OsCloseFr
1ab3d 65 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20  ee(pMaster);.   
1ab3e 20 20 20 73 71 6c 69 74 65 33 4f 73 44 65 6c 65     sqlite3OsDele
1ab3f 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72  te(pVfs, zMaster
1ab40 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
1ab41 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d  te3DbFree(db, zM
1ab42 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 72 65  aster);.      re
1ab43 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a  turn rc;.    }..
1ab44 20 20 20 20 2f 2a 20 53 79 6e 63 20 61 6c 6c 20      /* Sync all 
1ab45 74 68 65 20 64 62 20 66 69 6c 65 73 20 69 6e 76  the db files inv
1ab46 6f 6c 76 65 64 20 69 6e 20 74 68 65 20 74 72 61  olved in the tra
1ab47 6e 73 61 63 74 69 6f 6e 2e 20 54 68 65 20 73 61  nsaction. The sa
1ab48 6d 65 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 73  me call.    ** s
1ab49 65 74 73 20 74 68 65 20 6d 61 73 74 65 72 20 6a  ets the master j
1ab4a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65 72 20 69  ournal pointer i
1ab4b 6e 20 65 61 63 68 20 69 6e 64 69 76 69 64 75 61  n each individua
1ab4c 6c 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 0a 20 20  l journal. If.  
1ab4d 20 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63    ** an error oc
1ab4e 63 75 72 73 20 68 65 72 65 2c 20 64 6f 20 6e 6f  curs here, do no
1ab4f 74 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73  t delete the mas
1ab50 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
1ab51 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
1ab52 49 66 20 74 68 65 20 65 72 72 6f 72 20 6f 63 63  If the error occ
1ab53 75 72 73 20 64 75 72 69 6e 67 20 74 68 65 20 66  urs during the f
1ab54 69 72 73 74 20 63 61 6c 6c 20 74 6f 0a 20 20 20  irst call to.   
1ab55 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65   ** sqlite3Btree
1ab56 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 29  CommitPhaseOne()
1ab57 2c 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20  , then there is 
1ab58 61 20 63 68 61 6e 63 65 20 74 68 61 74 20 74 68  a chance that th
1ab59 65 0a 20 20 20 20 2a 2a 20 6d 61 73 74 65 72 20  e.    ** master 
1ab5a 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77 69 6c  journal file wil
1ab5b 6c 20 62 65 20 6f 72 70 68 61 6e 65 64 2e 20 42  l be orphaned. B
1ab5c 75 74 20 77 65 20 63 61 6e 6e 6f 74 20 64 65 6c  ut we cannot del
1ab5d 65 74 65 20 69 74 2c 0a 20 20 20 20 2a 2a 20 69  ete it,.    ** i
1ab5e 6e 20 63 61 73 65 20 74 68 65 20 6d 61 73 74 65  n case the maste
1ab5f 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e  r journal file n
1ab60 61 6d 65 20 77 61 73 20 77 72 69 74 74 65 6e 20  ame was written 
1ab61 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c  into the journal
1ab62 0a 20 20 20 20 2a 2a 20 66 69 6c 65 20 62 65 66  .    ** file bef
1ab63 6f 72 65 20 74 68 65 20 66 61 69 6c 75 72 65 20  ore the failure 
1ab64 6f 63 63 75 72 72 65 64 2e 0a 20 20 20 20 2a 2f  occurred..    */
1ab65 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 72 63  .    for(i=0; rc
1ab66 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
1ab67 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20  <db->nDb; i++){ 
1ab68 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42  .      Btree *pB
1ab69 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  t = db->aDb[i].p
1ab6a 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 42  Bt;.      if( pB
1ab6b 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  t ){.        rc 
1ab6c 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  = sqlite3BtreeCo
1ab6d 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28 70 42 74  mmitPhaseOne(pBt
1ab6e 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  , zMaster);.    
1ab6f 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71    }.    }.    sq
1ab70 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65 65  lite3OsCloseFree
1ab71 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 69  (pMaster);.    i
1ab72 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1ab73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1ab74 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61 73  3DbFree(db, zMas
1ab75 74 65 72 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ter);.      retu
1ab76 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20  rn rc;.    }..  
1ab77 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20    /* Delete the 
1ab78 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
1ab79 69 6c 65 2e 20 54 68 69 73 20 63 6f 6d 6d 69 74  ile. This commit
1ab7a 73 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  s the transactio
1ab7b 6e 2e 20 41 66 74 65 72 0a 20 20 20 20 2a 2a 20  n. After.    ** 
1ab7c 64 6f 69 6e 67 20 74 68 69 73 20 74 68 65 20 64  doing this the d
1ab7d 69 72 65 63 74 6f 72 79 20 69 73 20 73 79 6e 63  irectory is sync
1ab7e 65 64 20 61 67 61 69 6e 20 62 65 66 6f 72 65 20  ed again before 
1ab7f 61 6e 79 20 69 6e 64 69 76 69 64 75 61 6c 0a 20  any individual. 
1ab80 20 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f     ** transactio
1ab81 6e 20 66 69 6c 65 73 20 61 72 65 20 64 65 6c 65  n files are dele
1ab82 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ted..    */.    
1ab83 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65  rc = sqlite3OsDe
1ab84 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61 73 74  lete(pVfs, zMast
1ab85 65 72 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  er, 1);.    sqli
1ab86 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d  te3DbFree(db, zM
1ab87 61 73 74 65 72 29 3b 0a 20 20 20 20 7a 4d 61 73  aster);.    zMas
1ab88 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  ter = 0;.    if(
1ab89 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 74   rc ){.      ret
1ab8a 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20  urn rc;.    }.. 
1ab8b 20 20 20 2f 2a 20 41 6c 6c 20 66 69 6c 65 73 20     /* All files 
1ab8c 61 6e 64 20 64 69 72 65 63 74 6f 72 69 65 73 20  and directories 
1ab8d 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65 65  have already bee
1ab8e 6e 20 73 79 6e 63 65 64 2c 20 73 6f 20 74 68 65  n synced, so the
1ab8f 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20 20 2a   following.    *
1ab90 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  * calls to sqlit
1ab91 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
1ab92 73 65 54 77 6f 28 29 20 61 72 65 20 6f 6e 6c 79  seTwo() are only
1ab93 20 63 6c 6f 73 69 6e 67 20 66 69 6c 65 73 20 61   closing files a
1ab94 6e 64 0a 20 20 20 20 2a 2a 20 64 65 6c 65 74 69  nd.    ** deleti
1ab95 6e 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67  ng or truncating
1ab96 20 6a 6f 75 72 6e 61 6c 73 2e 20 49 66 20 73 6f   journals. If so
1ab97 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f  mething goes wro
1ab98 6e 67 20 77 68 69 6c 65 0a 20 20 20 20 2a 2a 20  ng while.    ** 
1ab99 74 68 69 73 20 69 73 20 68 61 70 70 65 6e 69 6e  this is happenin
1ab9a 67 20 77 65 20 64 6f 6e 27 74 20 72 65 61 6c 6c  g we don't reall
1ab9b 79 20 63 61 72 65 2e 20 54 68 65 20 69 6e 74 65  y care. The inte
1ab9c 67 72 69 74 79 20 6f 66 20 74 68 65 0a 20 20 20  grity of the.   
1ab9d 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20   ** transaction 
1ab9e 69 73 20 61 6c 72 65 61 64 79 20 67 75 61 72 61  is already guara
1ab9f 6e 74 65 65 64 2c 20 62 75 74 20 73 6f 6d 65 20  nteed, but some 
1aba0 73 74 72 61 79 20 27 63 6f 6c 64 27 20 6a 6f 75  stray 'cold' jou
1aba1 72 6e 61 6c 73 0a 20 20 20 20 2a 2a 20 6d 61 79  rnals.    ** may
1aba2 20 62 65 20 6c 79 69 6e 67 20 61 72 6f 75 6e 64   be lying around
1aba3 2e 20 52 65 74 75 72 6e 69 6e 67 20 61 6e 20 65  . Returning an e
1aba4 72 72 6f 72 20 63 6f 64 65 20 77 6f 6e 27 74 20  rror code won't 
1aba5 68 65 6c 70 20 6d 61 74 74 65 72 73 2e 0a 20 20  help matters..  
1aba6 20 20 2a 2f 0a 20 20 20 20 64 69 73 61 62 6c 65    */.    disable
1aba7 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72  _simulated_io_er
1aba8 72 6f 72 73 28 29 3b 0a 20 20 20 20 73 71 6c 69  rors();.    sqli
1aba9 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61  te3BeginBenignMa
1abaa 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 66 6f 72 28  lloc();.    for(
1abab 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  i=0; i<db->nDb; 
1abac 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 42 74 72  i++){ .      Btr
1abad 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44  ee *pBt = db->aD
1abae 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20  b[i].pBt;.      
1abaf 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20  if( pBt ){.     
1abb0 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43     sqlite3BtreeC
1abb1 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70 42  ommitPhaseTwo(pB
1abb2 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  t);.      }.    
1abb3 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64  }.    sqlite3End
1abb4 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
1abb5 20 20 20 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c      enable_simul
1abb6 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29  ated_io_errors()
1abb7 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74  ;..    sqlite3Vt
1abb8 61 62 43 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20  abCommit(db);.  
1abb9 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75  }.#endif..  retu
1abba 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  rn rc;.}../* .**
1abbb 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
1abbc 65 63 6b 73 20 74 68 61 74 20 74 68 65 20 73 71  ecks that the sq
1abbd 6c 69 74 65 33 2e 61 63 74 69 76 65 56 64 62 65  lite3.activeVdbe
1abbe 43 6e 74 20 63 6f 75 6e 74 20 76 61 72 69 61 62  Cnt count variab
1abbf 6c 65 0a 2a 2a 20 6d 61 74 63 68 65 73 20 74 68  le.** matches th
1abc0 65 20 6e 75 6d 62 65 72 20 6f 66 20 76 64 62 65  e number of vdbe
1abc1 27 73 20 69 6e 20 74 68 65 20 6c 69 73 74 20 73  's in the list s
1abc2 71 6c 69 74 65 33 2e 70 56 64 62 65 20 74 68 61  qlite3.pVdbe tha
1abc3 74 20 61 72 65 0a 2a 2a 20 63 75 72 72 65 6e 74  t are.** current
1abc4 6c 79 20 61 63 74 69 76 65 2e 20 41 6e 20 61 73  ly active. An as
1abc5 73 65 72 74 69 6f 6e 20 66 61 69 6c 73 20 69 66  sertion fails if
1abc6 20 74 68 65 20 74 77 6f 20 63 6f 75 6e 74 73 20   the two counts 
1abc7 64 6f 20 6e 6f 74 20 6d 61 74 63 68 2e 0a 2a 2a  do not match..**
1abc8 20 54 68 69 73 20 69 73 20 61 6e 20 69 6e 74 65   This is an inte
1abc9 72 6e 61 6c 20 73 65 6c 66 2d 63 68 65 63 6b 20  rnal self-check 
1abca 6f 6e 6c 79 20 2d 20 69 74 20 69 73 20 6e 6f 74  only - it is not
1abcb 20 61 6e 20 65 73 73 65 6e 74 69 61 6c 20 70 72   an essential pr
1abcc 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 73 74 65 70  ocessing.** step
1abcd 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ..**.** This is 
1abce 61 20 6e 6f 2d 6f 70 20 69 66 20 4e 44 45 42 55  a no-op if NDEBU
1abcf 47 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f  G is defined..*/
1abd0 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
1abd1 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 63  static void chec
1abd2 6b 41 63 74 69 76 65 56 64 62 65 43 6e 74 28 73  kActiveVdbeCnt(s
1abd3 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56  qlite3 *db){.  V
1abd4 64 62 65 20 2a 70 3b 0a 20 20 69 6e 74 20 63 6e  dbe *p;.  int cn
1abd5 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 57 72  t = 0;.  int nWr
1abd6 69 74 65 20 3d 20 30 3b 0a 20 20 70 20 3d 20 64  ite = 0;.  p = d
1abd7 62 2d 3e 70 56 64 62 65 3b 0a 20 20 77 68 69 6c  b->pVdbe;.  whil
1abd8 65 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20  e( p ){.    if( 
1abd9 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d  p->magic==VDBE_M
1abda 41 47 49 43 5f 52 55 4e 20 26 26 20 70 2d 3e 70  AGIC_RUN && p->p
1abdb 63 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6e  c>=0 ){.      cn
1abdc 74 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20 70  t++;.      if( p
1abdd 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d 30 20 29 20  ->readOnly==0 ) 
1abde 6e 57 72 69 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a  nWrite++;.    }.
1abdf 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74      p = p->pNext
1abe0 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
1abe1 63 6e 74 3d 3d 64 62 2d 3e 61 63 74 69 76 65 56  cnt==db->activeV
1abe2 64 62 65 43 6e 74 20 29 3b 0a 20 20 61 73 73 65  dbeCnt );.  asse
1abe3 72 74 28 20 6e 57 72 69 74 65 3d 3d 64 62 2d 3e  rt( nWrite==db->
1abe4 77 72 69 74 65 56 64 62 65 43 6e 74 20 29 3b 0a  writeVdbeCnt );.
1abe5 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  }.#else.#define 
1abe6 63 68 65 63 6b 41 63 74 69 76 65 56 64 62 65 43  checkActiveVdbeC
1abe7 6e 74 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  nt(x).#endif../*
1abe8 0a 2a 2a 20 46 6f 72 20 65 76 65 72 79 20 42 74  .** For every Bt
1abe9 72 65 65 20 74 68 61 74 20 69 6e 20 64 61 74 61  ree that in data
1abea 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1abeb 64 62 20 77 68 69 63 68 20 0a 2a 2a 20 68 61 73  db which .** has
1abec 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2c 20   been modified, 
1abed 22 74 72 69 70 22 20 6f 72 20 69 6e 76 61 6c 69  "trip" or invali
1abee 64 61 74 65 20 65 61 63 68 20 63 75 72 73 6f 72  date each cursor
1abef 20 69 6e 0a 2a 2a 20 74 68 61 74 20 42 74 72 65   in.** that Btre
1abf0 65 20 6d 69 67 68 74 20 68 61 76 65 20 62 65 65  e might have bee
1abf1 6e 20 6d 6f 64 69 66 69 65 64 20 73 6f 20 74 68  n modified so th
1abf2 61 74 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a  at the cursor.**
1abf3 20 63 61 6e 20 6e 65 76 65 72 20 62 65 20 75 73   can never be us
1abf4 65 64 20 61 67 61 69 6e 2e 20 20 54 68 69 73 20  ed again.  This 
1abf5 68 61 70 70 65 6e 73 20 77 68 65 6e 20 61 20 72  happens when a r
1abf6 6f 6c 6c 62 61 63 6b 0a 2a 2a 2a 20 6f 63 63 75  ollback.*** occu
1abf7 72 73 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20  rs.  We have to 
1abf8 74 72 69 70 20 61 6c 6c 20 74 68 65 20 6f 74 68  trip all the oth
1abf9 65 72 20 63 75 72 73 6f 72 73 2c 20 65 76 65 6e  er cursors, even
1abfa 0a 2a 2a 20 63 75 72 73 6f 72 20 66 72 6f 6d 20  .** cursor from 
1abfb 6f 74 68 65 72 20 56 4d 73 20 69 6e 20 64 69 66  other VMs in dif
1abfc 66 65 72 65 6e 74 20 64 61 74 61 62 61 73 65 20  ferent database 
1abfd 63 6f 6e 6e 65 63 74 69 6f 6e 73 2c 0a 2a 2a 20  connections,.** 
1abfe 73 6f 20 74 68 61 74 20 6e 6f 6e 65 20 6f 66 20  so that none of 
1abff 74 68 65 6d 20 74 72 79 20 74 6f 20 75 73 65 20  them try to use 
1ac00 74 68 65 20 64 61 74 61 20 61 74 20 77 68 69 63  the data at whic
1ac01 68 20 74 68 65 79 0a 2a 2a 20 77 65 72 65 20 70  h they.** were p
1ac02 6f 69 6e 74 69 6e 67 20 61 6e 64 20 77 68 69 63  ointing and whic
1ac03 68 20 6e 6f 77 20 6d 61 79 20 68 61 76 65 20 62  h now may have b
1ac04 65 65 6e 20 63 68 61 6e 67 65 64 20 64 75 65 0a  een changed due.
1ac05 2a 2a 20 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61  ** to the rollba
1ac06 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 6d 65 6d 62  ck..**.** Rememb
1ac07 65 72 20 74 68 61 74 20 61 20 72 6f 6c 6c 62 61  er that a rollba
1ac08 63 6b 20 63 61 6e 20 64 65 6c 65 74 65 20 74 61  ck can delete ta
1ac09 62 6c 65 73 20 63 6f 6d 70 6c 65 74 65 20 61 6e  bles complete an
1ac0a 64 0a 2a 2a 20 72 65 6f 72 64 65 72 20 72 6f 6f  d.** reorder roo
1ac0b 74 70 61 67 65 73 2e 20 20 53 6f 20 69 74 20 69  tpages.  So it i
1ac0c 73 20 6e 6f 74 20 73 75 66 66 69 63 69 65 6e 74  s not sufficient
1ac0d 20 6a 75 73 74 20 74 6f 20 73 61 76 65 0a 2a 2a   just to save.**
1ac0e 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68   the state of th
1ac0f 65 20 63 75 72 73 6f 72 2e 20 20 57 65 20 68 61  e cursor.  We ha
1ac10 76 65 20 74 6f 20 69 6e 76 61 6c 69 64 61 74 65  ve to invalidate
1ac11 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 73   the cursor.** s
1ac12 6f 20 74 68 61 74 20 69 74 20 69 73 20 6e 65 76  o that it is nev
1ac13 65 72 20 75 73 65 64 20 61 67 61 69 6e 2e 0a 2a  er used again..*
1ac14 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e  /.static void in
1ac15 76 61 6c 69 64 61 74 65 43 75 72 73 6f 72 73 4f  validateCursorsO
1ac16 6e 4d 6f 64 69 66 69 65 64 42 74 72 65 65 73 28  nModifiedBtrees(
1ac17 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
1ac18 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30  int i;.  for(i=0
1ac19 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
1ac1a 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70 20  ){.    Btree *p 
1ac1b 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74  = db->aDb[i].pBt
1ac1c 3b 0a 20 20 20 20 69 66 28 20 70 20 26 26 20 73  ;.    if( p && s
1ac1d 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54  qlite3BtreeIsInT
1ac1e 72 61 6e 73 28 70 29 20 29 7b 0a 20 20 20 20 20  rans(p) ){.     
1ac1f 20 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 69   sqlite3BtreeTri
1ac20 70 41 6c 6c 43 75 72 73 6f 72 73 28 70 2c 20 53  pAllCursors(p, S
1ac21 51 4c 49 54 45 5f 41 42 4f 52 54 29 3b 0a 20 20  QLITE_ABORT);.  
1ac22 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
1ac23 20 49 66 20 74 68 65 20 56 64 62 65 20 70 61 73   If the Vdbe pas
1ac24 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74  sed as the first
1ac25 20 61 72 67 75 6d 65 6e 74 20 6f 70 65 6e 65 64   argument opened
1ac26 20 61 20 73 74 61 74 65 6d 65 6e 74 2d 74 72 61   a statement-tra
1ac27 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a 20 63 6c 6f  nsaction,.** clo
1ac28 73 65 20 69 74 20 6e 6f 77 2e 20 41 72 67 75 6d  se it now. Argum
1ac29 65 6e 74 20 65 4f 70 20 6d 75 73 74 20 62 65 20  ent eOp must be 
1ac2a 65 69 74 68 65 72 20 53 41 56 45 50 4f 49 4e 54  either SAVEPOINT
1ac2b 5f 52 4f 4c 4c 42 41 43 4b 20 6f 72 0a 2a 2a 20  _ROLLBACK or.** 
1ac2c 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53  SAVEPOINT_RELEAS
1ac2d 45 2e 20 49 66 20 69 74 20 69 73 20 53 41 56 45  E. If it is SAVE
1ac2e 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20  POINT_ROLLBACK, 
1ac2f 74 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65  then the stateme
1ac30 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f  nt.** transactio
1ac31 6e 20 69 73 20 72 6f 6c 6c 65 64 20 62 61 63 6b  n is rolled back
1ac32 2e 20 49 66 20 65 4f 70 20 69 73 20 53 41 56 45  . If eOp is SAVE
1ac33 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20 74  POINT_RELEASE, t
1ac34 68 65 6e 20 74 68 65 20 0a 2a 2a 20 73 74 61 74  hen the .** stat
1ac35 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
1ac36 6e 20 69 73 20 63 6f 6d 6d 74 74 65 64 2e 0a 2a  n is commtted..*
1ac37 2a 0a 2a 2a 20 49 66 20 61 6e 20 49 4f 20 65 72  *.** If an IO er
1ac38 72 6f 72 20 6f 63 63 75 72 73 2c 20 61 6e 20 53  ror occurs, an S
1ac39 51 4c 49 54 45 5f 49 4f 45 52 52 5f 58 58 58 20  QLITE_IOERR_XXX 
1ac3a 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
1ac3b 74 75 72 6e 65 64 2e 20 0a 2a 2a 20 4f 74 68 65  turned. .** Othe
1ac3c 72 77 69 73 65 20 53 51 4c 49 54 45 5f 4f 4b 2e  rwise SQLITE_OK.
1ac3d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1ac3e 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
1ac3f 62 65 43 6c 6f 73 65 53 74 61 74 65 6d 65 6e 74  beCloseStatement
1ac40 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 65 4f  (Vdbe *p, int eO
1ac41 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 63  p){.  sqlite3 *c
1ac42 6f 6e 73 74 20 64 62 20 3d 20 70 2d 3e 64 62 3b  onst db = p->db;
1ac43 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
1ac44 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 49 66 20  TE_OK;..  /* If 
1ac45 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 69 73  p->iStatement is
1ac46 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65   greater than ze
1ac47 72 6f 2c 20 74 68 65 6e 20 74 68 69 73 20 56 64  ro, then this Vd
1ac48 62 65 20 6f 70 65 6e 65 64 20 61 20 0a 20 20 2a  be opened a .  *
1ac49 2a 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  * statement tran
1ac4a 73 61 63 74 69 6f 6e 20 74 68 61 74 20 73 68 6f  saction that sho
1ac4b 75 6c 64 20 62 65 20 63 6c 6f 73 65 64 20 68 65  uld be closed he
1ac4c 72 65 2e 20 54 68 65 20 6f 6e 6c 79 20 65 78 63  re. The only exc
1ac4d 65 70 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 74  eption.  ** is t
1ac4e 68 61 74 20 61 6e 20 49 4f 20 65 72 72 6f 72 20  hat an IO error 
1ac4f 6d 61 79 20 68 61 76 65 20 6f 63 63 75 72 65 64  may have occured
1ac50 2c 20 63 61 75 73 69 6e 67 20 61 6e 20 65 6d 65  , causing an eme
1ac51 72 67 65 6e 63 79 20 72 6f 6c 6c 62 61 63 6b 2e  rgency rollback.
1ac52 0a 20 20 2a 2a 20 49 6e 20 74 68 69 73 20 63 61  .  ** In this ca
1ac53 73 65 20 28 64 62 2d 3e 6e 53 74 61 74 65 6d 65  se (db->nStateme
1ac54 6e 74 3d 3d 30 29 2c 20 61 6e 64 20 74 68 65 72  nt==0), and ther
1ac55 65 20 69 73 20 6e 6f 74 68 69 6e 67 20 74 6f 20  e is nothing to 
1ac56 64 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  do..  */.  if( d
1ac57 62 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 20 26 26  b->nStatement &&
1ac58 20 70 2d 3e 69 53 74 61 74 65 6d 65 6e 74 20 29   p->iStatement )
1ac59 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  {.    int i;.   
1ac5a 20 63 6f 6e 73 74 20 69 6e 74 20 69 53 61 76 65   const int iSave
1ac5b 70 6f 69 6e 74 20 3d 20 70 2d 3e 69 53 74 61 74  point = p->iStat
1ac5c 65 6d 65 6e 74 2d 31 3b 0a 0a 20 20 20 20 61 73  ement-1;..    as
1ac5d 73 65 72 74 28 20 65 4f 70 3d 3d 53 41 56 45 50  sert( eOp==SAVEP
1ac5e 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 7c 7c  OINT_ROLLBACK ||
1ac5f 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f   eOp==SAVEPOINT_
1ac60 52 45 4c 45 41 53 45 29 3b 0a 20 20 20 20 61 73  RELEASE);.    as
1ac61 73 65 72 74 28 20 64 62 2d 3e 6e 53 74 61 74 65  sert( db->nState
1ac62 6d 65 6e 74 3e 30 20 29 3b 0a 20 20 20 20 61 73  ment>0 );.    as
1ac63 73 65 72 74 28 20 70 2d 3e 69 53 74 61 74 65 6d  sert( p->iStatem
1ac64 65 6e 74 3d 3d 28 64 62 2d 3e 6e 53 74 61 74 65  ent==(db->nState
1ac65 6d 65 6e 74 2b 64 62 2d 3e 6e 53 61 76 65 70 6f  ment+db->nSavepo
1ac66 69 6e 74 29 20 29 3b 0a 0a 20 20 20 20 66 6f 72  int) );..    for
1ac67 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  (i=0; i<db->nDb;
1ac68 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 69 6e   i++){ .      in
1ac69 74 20 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f  t rc2 = SQLITE_O
1ac6a 4b 3b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a  K;.      Btree *
1ac6b 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d  pBt = db->aDb[i]
1ac6c 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66 28 20  .pBt;.      if( 
1ac6d 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69  pBt ){.        i
1ac6e 66 28 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e  f( eOp==SAVEPOIN
1ac6f 54 5f 52 4f 4c 4c 42 41 43 4b 20 29 7b 0a 20 20  T_ROLLBACK ){.  
1ac70 20 20 20 20 20 20 20 20 72 63 32 20 3d 20 73 71          rc2 = sq
1ac71 6c 69 74 65 33 42 74 72 65 65 53 61 76 65 70 6f  lite3BtreeSavepo
1ac72 69 6e 74 28 70 42 74 2c 20 53 41 56 45 50 4f 49  int(pBt, SAVEPOI
1ac73 4e 54 5f 52 4f 4c 4c 42 41 43 4b 2c 20 69 53 61  NT_ROLLBACK, iSa
1ac74 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20 20  vepoint);.      
1ac75 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
1ac76 72 63 32 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  rc2==SQLITE_OK )
1ac77 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 32 20  {.          rc2 
1ac78 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 61  = sqlite3BtreeSa
1ac79 76 65 70 6f 69 6e 74 28 70 42 74 2c 20 53 41 56  vepoint(pBt, SAV
1ac7a 45 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 2c 20  EPOINT_RELEASE, 
1ac7b 69 53 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20  iSavepoint);.   
1ac7c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
1ac7d 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1ac7e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
1ac7f 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 20 20   = rc2;.        
1ac80 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
1ac81 20 20 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65      db->nStateme
1ac82 6e 74 2d 2d 3b 0a 20 20 20 20 70 2d 3e 69 53 74  nt--;.    p->iSt
1ac83 61 74 65 6d 65 6e 74 20 3d 20 30 3b 0a 0a 20 20  atement = 0;..  
1ac84 20 20 2f 2a 20 49 66 20 74 68 65 20 73 74 61 74    /* If the stat
1ac85 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
1ac86 6e 20 69 73 20 62 65 69 6e 67 20 72 6f 6c 6c 65  n is being rolle
1ac87 64 20 62 61 63 6b 2c 20 61 6c 73 6f 20 72 65 73  d back, also res
1ac88 74 6f 72 65 20 74 68 65 20 0a 20 20 20 20 2a 2a  tore the .    **
1ac89 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
1ac8a 73 20 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74  s deferred const
1ac8b 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 20 74 6f  raint counter to
1ac8c 20 74 68 65 20 76 61 6c 75 65 20 69 74 20 68 61   the value it ha
1ac8d 64 20 77 68 65 6e 20 0a 20 20 20 20 2a 2a 20 74  d when .    ** t
1ac8e 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61  he statement tra
1ac8f 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 6f 70 65  nsaction was ope
1ac90 6e 65 64 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28  ned.  */.    if(
1ac91 20 65 4f 70 3d 3d 53 41 56 45 50 4f 49 4e 54 5f   eOp==SAVEPOINT_
1ac92 52 4f 4c 4c 42 41 43 4b 20 29 7b 0a 20 20 20 20  ROLLBACK ){.    
1ac93 20 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64 43    db->nDeferredC
1ac94 6f 6e 73 20 3d 20 70 2d 3e 6e 53 74 6d 74 44 65  ons = p->nStmtDe
1ac95 66 43 6f 6e 73 3b 0a 20 20 20 20 7d 0a 20 20 7d  fCons;.    }.  }
1ac96 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1ac97 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65  ./*.** If SQLite
1ac98 20 69 73 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20   is compiled to 
1ac99 73 75 70 70 6f 72 74 20 73 68 61 72 65 64 2d 63  support shared-c
1ac9a 61 63 68 65 20 6d 6f 64 65 20 61 6e 64 20 74 6f  ache mode and to
1ac9b 20 62 65 20 74 68 72 65 61 64 73 61 66 65 2c 0a   be threadsafe,.
1ac9c 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
1ac9d 6f 62 74 61 69 6e 73 20 74 68 65 20 6d 75 74 65  obtains the mute
1ac9e 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  x associated wit
1ac9f 68 20 65 61 63 68 20 42 74 53 68 61 72 65 64 20  h each BtShared 
1aca0 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 74 68 61  structure.** tha
1aca1 74 20 6d 61 79 20 62 65 20 61 63 63 65 73 73 65  t may be accesse
1aca2 64 20 62 79 20 74 68 65 20 56 4d 20 70 61 73 73  d by the VM pass
1aca3 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e  ed as an argumen
1aca4 74 2e 20 49 6e 20 64 6f 69 6e 67 20 73 6f 20 69  t. In doing so i
1aca5 74 0a 2a 2a 20 73 65 74 73 20 74 68 65 20 42 74  t.** sets the Bt
1aca6 53 68 61 72 65 64 2e 64 62 20 6d 65 6d 62 65 72  Shared.db member
1aca7 20 6f 66 20 65 61 63 68 20 6f 66 20 74 68 65 20   of each of the 
1aca8 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74 75  BtShared structu
1aca9 72 65 73 2c 20 65 6e 73 75 72 69 6e 67 0a 2a 2a  res, ensuring.**
1acaa 20 74 68 61 74 20 74 68 65 20 63 6f 72 72 65 63   that the correc
1acab 74 20 62 75 73 79 2d 68 61 6e 64 6c 65 72 20 63  t busy-handler c
1acac 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b  allback is invok
1acad 65 64 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a  ed if required..
1acae 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65 20  **.** If SQLite 
1acaf 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66  is not threadsaf
1acb0 65 20 62 75 74 20 64 6f 65 73 20 73 75 70 70 6f  e but does suppo
1acb1 72 74 20 73 68 61 72 65 64 2d 63 61 63 68 65 20  rt shared-cache 
1acb2 6d 6f 64 65 2c 20 74 68 65 6e 0a 2a 2a 20 73 71  mode, then.** sq
1acb3 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41  lite3BtreeEnterA
1acb4 6c 6c 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 20  ll() is invoked 
1acb5 74 6f 20 73 65 74 20 74 68 65 20 42 74 53 68 61  to set the BtSha
1acb6 72 65 64 2e 64 62 20 76 61 72 69 61 62 6c 65 73  red.db variables
1acb7 0a 2a 2a 20 6f 66 20 61 6c 6c 20 6f 66 20 42 74  .** of all of Bt
1acb8 53 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65  Shared structure
1acb9 73 20 61 63 63 65 73 73 69 62 6c 65 20 76 69 61  s accessible via
1acba 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
1acbb 6e 64 6c 65 20 0a 2a 2a 20 61 73 73 6f 63 69 61  ndle .** associa
1acbc 74 65 64 20 77 69 74 68 20 74 68 65 20 56 4d 2e  ted with the VM.
1acbd 20 4f 66 20 63 6f 75 72 73 65 20 6f 6e 6c 79 20   Of course only 
1acbe 61 20 73 75 62 73 65 74 20 6f 66 20 74 68 65 73  a subset of thes
1acbf 65 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20  e structures.** 
1acc0 77 69 6c 6c 20 62 65 20 61 63 63 65 73 73 65 64  will be accessed
1acc1 20 62 79 20 74 68 65 20 56 4d 2c 20 61 6e 64 20   by the VM, and 
1acc2 77 65 20 63 6f 75 6c 64 20 75 73 65 20 56 64 62  we could use Vdb
1acc3 65 2e 62 74 72 65 65 4d 61 73 6b 20 74 6f 20 66  e.btreeMask to f
1acc4 69 67 75 72 65 0a 2a 2a 20 74 68 61 74 20 73 75  igure.** that su
1acc5 62 73 65 74 20 6f 75 74 2c 20 62 75 74 20 74 68  bset out, but th
1acc6 65 72 65 20 69 73 20 6e 6f 20 61 64 76 61 6e 74  ere is no advant
1acc7 61 67 65 20 74 6f 20 64 6f 69 6e 67 20 73 6f 2e  age to doing so.
1acc8 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 69 74 65  .**.** If SQLite
1acc9 20 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61   is not threadsa
1acca 66 65 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20  fe and does not 
1accb 73 75 70 70 6f 72 74 20 73 68 61 72 65 64 2d 63  support shared-c
1accc 61 63 68 65 20 6d 6f 64 65 2c 20 74 68 69 73 0a  ache mode, this.
1accd 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  ** function is a
1acce 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 23 69 66 6e 64   no-op..*/.#ifnd
1accf 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
1acd0 48 41 52 45 44 5f 43 41 43 48 45 0a 53 51 4c 49  HARED_CACHE.SQLI
1acd1 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1acd2 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74 65 78  sqlite3VdbeMutex
1acd3 41 72 72 61 79 45 6e 74 65 72 28 56 64 62 65 20  ArrayEnter(Vdbe 
1acd4 2a 70 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f  *p){.#if SQLITE_
1acd5 54 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c  THREADSAFE.  sql
1acd6 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41 72  ite3BtreeMutexAr
1acd7 72 61 79 45 6e 74 65 72 28 26 70 2d 3e 61 4d 75  rayEnter(&p->aMu
1acd8 74 65 78 29 3b 0a 23 65 6c 73 65 0a 20 20 73 71  tex);.#else.  sq
1acd9 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41  lite3BtreeEnterA
1acda 6c 6c 28 70 2d 3e 64 62 29 3b 0a 23 65 6e 64 69  ll(p->db);.#endi
1acdb 66 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  f.}.#endif../*.*
1acdc 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
1acdd 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61  is called when a
1acde 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65   transaction ope
1acdf 6e 65 64 20 62 79 20 74 68 65 20 64 61 74 61 62  ned by the datab
1ace0 61 73 65 20 0a 2a 2a 20 68 61 6e 64 6c 65 20 61  ase .** handle a
1ace1 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
1ace2 68 65 20 56 4d 20 70 61 73 73 65 64 20 61 73 20  he VM passed as 
1ace3 61 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61  an argument is a
1ace4 62 6f 75 74 20 74 6f 20 62 65 20 0a 2a 2a 20 63  bout to be .** c
1ace5 6f 6d 6d 69 74 74 65 64 2e 20 49 66 20 74 68 65  ommitted. If the
1ace6 72 65 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69  re are outstandi
1ace7 6e 67 20 64 65 66 65 72 72 65 64 20 66 6f 72 65  ng deferred fore
1ace8 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
1ace9 6e 74 0a 2a 2a 20 76 69 6f 6c 61 74 69 6f 6e 73  nt.** violations
1acea 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
1aceb 45 52 52 4f 52 2e 20 4f 74 68 65 72 77 69 73 65  ERROR. Otherwise
1acec 2c 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a  , SQLITE_OK..**.
1aced 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  ** If there are 
1acee 6f 75 74 73 74 61 6e 64 69 6e 67 20 46 4b 20 76  outstanding FK v
1acef 69 6f 6c 61 74 69 6f 6e 73 20 61 6e 64 20 74 68  iolations and th
1acf0 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  is function retu
1acf1 72 6e 73 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 45  rns .** SQLITE_E
1acf2 52 52 4f 52 2c 20 73 65 74 20 74 68 65 20 72 65  RROR, set the re
1acf3 73 75 6c 74 20 6f 66 20 74 68 65 20 56 4d 20 74  sult of the VM t
1acf4 6f 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  o SQLITE_CONSTRA
1acf5 49 4e 54 20 61 6e 64 20 77 72 69 74 65 0a 2a 2a  INT and write.**
1acf6 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
1acf7 65 20 74 6f 20 69 74 2e 20 54 68 65 6e 20 72 65  e to it. Then re
1acf8 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
1acf9 52 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  R..*/.#ifndef SQ
1acfa 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47  LITE_OMIT_FOREIG
1acfb 4e 5f 4b 45 59 0a 53 51 4c 49 54 45 5f 50 52 49  N_KEY.SQLITE_PRI
1acfc 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1acfd 56 64 62 65 43 68 65 63 6b 46 6b 28 56 64 62 65  VdbeCheckFk(Vdbe
1acfe 20 2a 70 2c 20 69 6e 74 20 64 65 66 65 72 72 65   *p, int deferre
1acff 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  d){.  sqlite3 *d
1ad00 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28  b = p->db;.  if(
1ad01 20 28 64 65 66 65 72 72 65 64 20 26 26 20 64 62   (deferred && db
1ad02 2d 3e 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3e  ->nDeferredCons>
1ad03 30 29 20 7c 7c 20 28 21 64 65 66 65 72 72 65 64  0) || (!deferred
1ad04 20 26 26 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72   && p->nFkConstr
1ad05 61 69 6e 74 3e 30 29 20 29 7b 0a 20 20 20 20 70  aint>0) ){.    p
1ad06 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f  ->rc = SQLITE_CO
1ad07 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 70 2d  NSTRAINT;.    p-
1ad08 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f  >errorAction = O
1ad09 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 73 71 6c  E_Abort;.    sql
1ad0a 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70  ite3SetString(&p
1ad0b 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22  ->zErrMsg, db, "
1ad0c 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
1ad0d 74 72 61 69 6e 74 20 66 61 69 6c 65 64 22 29 3b  traint failed");
1ad0e 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1ad0f 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
1ad10 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
1ad11 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
1ad12 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
1ad13 73 20 63 61 6c 6c 65 64 20 74 68 65 20 77 68 65  s called the whe
1ad14 6e 20 61 20 56 44 42 45 20 74 72 69 65 73 20 74  n a VDBE tries t
1ad15 6f 20 68 61 6c 74 2e 20 20 49 66 20 74 68 65 20  o halt.  If the 
1ad16 56 44 42 45 0a 2a 2a 20 68 61 73 20 6d 61 64 65  VDBE.** has made
1ad17 20 63 68 61 6e 67 65 73 20 61 6e 64 20 69 73 20   changes and is 
1ad18 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f  in autocommit mo
1ad19 64 65 2c 20 74 68 65 6e 20 63 6f 6d 6d 69 74 20  de, then commit 
1ad1a 74 68 6f 73 65 0a 2a 2a 20 63 68 61 6e 67 65 73  those.** changes
1ad1b 2e 20 20 49 66 20 61 20 72 6f 6c 6c 62 61 63 6b  .  If a rollback
1ad1c 20 69 73 20 6e 65 65 64 65 64 2c 20 74 68 65 6e   is needed, then
1ad1d 20 64 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b   do the rollback
1ad1e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1ad1f 74 69 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79  tine is the only
1ad20 20 77 61 79 20 74 6f 20 6d 6f 76 65 20 74 68 65   way to move the
1ad21 20 73 74 61 74 65 20 6f 66 20 61 20 56 4d 20 66   state of a VM f
1ad22 72 6f 6d 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 41  rom.** SQLITE_MA
1ad23 47 49 43 5f 52 55 4e 20 74 6f 20 53 51 4c 49 54  GIC_RUN to SQLIT
1ad24 45 5f 4d 41 47 49 43 5f 48 41 4c 54 2e 20 20 49  E_MAGIC_HALT.  I
1ad25 74 20 69 73 20 68 61 72 6d 6c 65 73 73 20 74 6f  t is harmless to
1ad26 0a 2a 2a 20 63 61 6c 6c 20 74 68 69 73 20 6f 6e  .** call this on
1ad27 20 61 20 56 4d 20 74 68 61 74 20 69 73 20 69 6e   a VM that is in
1ad28 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 47 49   the SQLITE_MAGI
1ad29 43 5f 48 41 4c 54 20 73 74 61 74 65 2e 0a 2a 2a  C_HALT state..**
1ad2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72  .** Return an er
1ad2b 72 6f 72 20 63 6f 64 65 2e 20 20 49 66 20 74 68  ror code.  If th
1ad2c 65 20 63 6f 6d 6d 69 74 20 63 6f 75 6c 64 20 6e  e commit could n
1ad2d 6f 74 20 63 6f 6d 70 6c 65 74 65 20 62 65 63 61  ot complete beca
1ad2e 75 73 65 20 6f 66 0a 2a 2a 20 6c 6f 63 6b 20 63  use of.** lock c
1ad2f 6f 6e 74 65 6e 74 69 6f 6e 2c 20 72 65 74 75 72  ontention, retur
1ad30 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20  n SQLITE_BUSY.  
1ad31 49 66 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69  If SQLITE_BUSY i
1ad32 73 20 72 65 74 75 72 6e 65 64 2c 20 69 74 0a 2a  s returned, it.*
1ad33 2a 20 6d 65 61 6e 73 20 74 68 65 20 63 6c 6f 73  * means the clos
1ad34 65 20 64 69 64 20 6e 6f 74 20 68 61 70 70 65 6e  e did not happen
1ad35 20 61 6e 64 20 6e 65 65 64 73 20 74 6f 20 62 65   and needs to be
1ad36 20 72 65 70 65 61 74 65 64 2e 0a 2a 2f 0a 53 51   repeated..*/.SQ
1ad37 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1ad38 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74   sqlite3VdbeHalt
1ad39 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74  (Vdbe *p){.  int
1ad3a 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
1ad3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ad3c 55 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 72  Used to store tr
1ad3d 61 6e 73 69 65 6e 74 20 72 65 74 75 72 6e 20 63  ansient return c
1ad3e 6f 64 65 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65  odes */.  sqlite
1ad3f 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a  3 *db = p->db;..
1ad40 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69    /* This functi
1ad41 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  on contains the 
1ad42 6c 6f 67 69 63 20 74 68 61 74 20 64 65 74 65 72  logic that deter
1ad43 6d 69 6e 65 73 20 69 66 20 61 20 73 74 61 74 65  mines if a state
1ad44 6d 65 6e 74 20 6f 72 0a 20 20 2a 2a 20 74 72 61  ment or.  ** tra
1ad45 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65  nsaction will be
1ad46 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72 6f   committed or ro
1ad47 6c 6c 65 64 20 62 61 63 6b 20 61 73 20 61 20 72  lled back as a r
1ad48 65 73 75 6c 74 20 6f 66 20 74 68 65 0a 20 20 2a  esult of the.  *
1ad49 2a 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74  * execution of t
1ad4a 68 69 73 20 76 69 72 74 75 61 6c 20 6d 61 63 68  his virtual mach
1ad4b 69 6e 65 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ine. .  **.  ** 
1ad4c 49 66 20 61 6e 79 20 6f 66 20 74 68 65 20 66 6f  If any of the fo
1ad4d 6c 6c 6f 77 69 6e 67 20 65 72 72 6f 72 73 20 6f  llowing errors o
1ad4e 63 63 75 72 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ccur:.  **.  ** 
1ad4f 20 20 20 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d      SQLITE_NOMEM
1ad50 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  .  **     SQLITE
1ad51 5f 49 4f 45 52 52 0a 20 20 2a 2a 20 20 20 20 20  _IOERR.  **     
1ad52 53 51 4c 49 54 45 5f 46 55 4c 4c 0a 20 20 2a 2a  SQLITE_FULL.  **
1ad53 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4e 54 45       SQLITE_INTE
1ad54 52 52 55 50 54 0a 20 20 2a 2a 0a 20 20 2a 2a 20  RRUPT.  **.  ** 
1ad55 54 68 65 6e 20 74 68 65 20 69 6e 74 65 72 6e 61  Then the interna
1ad56 6c 20 63 61 63 68 65 20 6d 69 67 68 74 20 68 61  l cache might ha
1ad57 76 65 20 62 65 65 6e 20 6c 65 66 74 20 69 6e 20  ve been left in 
1ad58 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 0a  an inconsistent.
1ad59 20 20 2a 2a 20 73 74 61 74 65 2e 20 20 57 65 20    ** state.  We 
1ad5a 6e 65 65 64 20 74 6f 20 72 6f 6c 6c 62 61 63 6b  need to rollback
1ad5b 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74   the statement t
1ad5c 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 66 20 74  ransaction, if t
1ad5d 68 65 72 65 20 69 73 0a 20 20 2a 2a 20 6f 6e 65  here is.  ** one
1ad5e 2c 20 6f 72 20 74 68 65 20 63 6f 6d 70 6c 65 74  , or the complet
1ad5f 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 66  e transaction if
1ad60 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73 74 61   there is no sta
1ad61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
1ad62 6f 6e 2e 0a 20 20 2a 2f 0a 0a 20 20 69 66 28 20  on..  */..  if( 
1ad63 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  p->db->mallocFai
1ad64 6c 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63  led ){.    p->rc
1ad65 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
1ad66 0a 20 20 7d 0a 20 20 63 6c 6f 73 65 41 6c 6c 43  .  }.  closeAllC
1ad67 75 72 73 6f 72 73 28 70 29 3b 0a 20 20 69 66 28  ursors(p);.  if(
1ad68 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f   p->magic!=VDBE_
1ad69 4d 41 47 49 43 5f 52 55 4e 20 29 7b 0a 20 20 20  MAGIC_RUN ){.   
1ad6a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1ad6b 4b 3b 0a 20 20 7d 0a 20 20 63 68 65 63 6b 41 63  K;.  }.  checkAc
1ad6c 74 69 76 65 56 64 62 65 43 6e 74 28 64 62 29 3b  tiveVdbeCnt(db);
1ad6d 0a 0a 20 20 2f 2a 20 4e 6f 20 63 6f 6d 6d 69 74  ..  /* No commit
1ad6e 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6e 65 65   or rollback nee
1ad6f 64 65 64 20 69 66 20 74 68 65 20 70 72 6f 67 72  ded if the progr
1ad70 61 6d 20 6e 65 76 65 72 20 73 74 61 72 74 65 64  am never started
1ad71 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e   */.  if( p->pc>
1ad72 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 72  =0 ){.    int mr
1ad73 63 3b 20 20 20 2f 2a 20 50 72 69 6d 61 72 79 20  c;   /* Primary 
1ad74 65 72 72 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20  error code from 
1ad75 70 2d 3e 72 63 20 2a 2f 0a 20 20 20 20 69 6e 74  p->rc */.    int
1ad76 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 3d 20   eStatementOp = 
1ad77 30 3b 0a 20 20 20 20 69 6e 74 20 69 73 53 70 65  0;.    int isSpe
1ad78 63 69 61 6c 45 72 72 6f 72 3b 20 20 20 20 20 20  cialError;      
1ad79 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74 6f 20        /* Set to 
1ad7a 74 72 75 65 20 69 66 20 61 20 27 73 70 65 63 69  true if a 'speci
1ad7b 61 6c 27 20 65 72 72 6f 72 20 2a 2f 0a 0a 20 20  al' error */..  
1ad7c 20 20 2f 2a 20 4c 6f 63 6b 20 61 6c 6c 20 62 74    /* Lock all bt
1ad7d 72 65 65 73 20 75 73 65 64 20 62 79 20 74 68 65  rees used by the
1ad7e 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
1ad7f 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 75 74    sqlite3VdbeMut
1ad80 65 78 41 72 72 61 79 45 6e 74 65 72 28 70 29 3b  exArrayEnter(p);
1ad81 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 66  ..    /* Check f
1ad82 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 70  or one of the sp
1ad83 65 63 69 61 6c 20 65 72 72 6f 72 73 20 2a 2f 0a  ecial errors */.
1ad84 20 20 20 20 6d 72 63 20 3d 20 70 2d 3e 72 63 20      mrc = p->rc 
1ad85 26 20 30 78 66 66 3b 0a 20 20 20 20 61 73 73 65  & 0xff;.    asse
1ad86 72 74 28 20 70 2d 3e 72 63 21 3d 53 51 4c 49 54  rt( p->rc!=SQLIT
1ad87 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 20  E_IOERR_BLOCKED 
1ad88 29 3b 20 20 2f 2a 20 54 68 69 73 20 65 72 72 6f  );  /* This erro
1ad89 72 20 6e 6f 20 6c 6f 6e 67 65 72 20 65 78 69 73  r no longer exis
1ad8a 74 73 20 2a 2f 0a 20 20 20 20 69 73 53 70 65 63  ts */.    isSpec
1ad8b 69 61 6c 45 72 72 6f 72 20 3d 20 6d 72 63 3d 3d  ialError = mrc==
1ad8c 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20  SQLITE_NOMEM || 
1ad8d 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52  mrc==SQLITE_IOER
1ad8e 52 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  R.              
1ad8f 20 20 20 20 20 20 20 7c 7c 20 6d 72 63 3d 3d 53         || mrc==S
1ad90 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 20  QLITE_INTERRUPT 
1ad91 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 46  || mrc==SQLITE_F
1ad92 55 4c 4c 3b 0a 20 20 20 20 69 66 28 20 69 73 53  ULL;.    if( isS
1ad93 70 65 63 69 61 6c 45 72 72 6f 72 20 29 7b 0a 20  pecialError ){. 
1ad94 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 71       /* If the q
1ad95 75 65 72 79 20 77 61 73 20 72 65 61 64 2d 6f 6e  uery was read-on
1ad96 6c 79 2c 20 77 65 20 6e 65 65 64 20 64 6f 20 6e  ly, we need do n
1ad97 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 74 20 61 6c  o rollback at al
1ad98 6c 2e 20 4f 74 68 65 72 77 69 73 65 2c 0a 20 20  l. Otherwise,.  
1ad99 20 20 20 20 2a 2a 20 70 72 6f 63 65 65 64 20 77      ** proceed w
1ad9a 69 74 68 20 74 68 65 20 73 70 65 63 69 61 6c 20  ith the special 
1ad9b 68 61 6e 64 6c 69 6e 67 2e 0a 20 20 20 20 20 20  handling..      
1ad9c 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21 70 2d  */.      if( !p-
1ad9d 3e 72 65 61 64 4f 6e 6c 79 20 7c 7c 20 6d 72 63  >readOnly || mrc
1ad9e 21 3d 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55  !=SQLITE_INTERRU
1ad9f 50 54 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  PT ){.        if
1ada0 28 20 28 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  ( (mrc==SQLITE_N
1ada1 4f 4d 45 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c  OMEM || mrc==SQL
1ada2 49 54 45 5f 46 55 4c 4c 29 20 26 26 20 70 2d 3e  ITE_FULL) && p->
1ada3 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20  usesStmtJournal 
1ada4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 65 53 74  ){.          eSt
1ada5 61 74 65 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45  atementOp = SAVE
1ada6 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3b 0a  POINT_ROLLBACK;.
1ada7 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
1ada8 20 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 61           /* We a
1ada9 72 65 20 66 6f 72 63 65 64 20 74 6f 20 72 6f 6c  re forced to rol
1adaa 6c 20 62 61 63 6b 20 74 68 65 20 61 63 74 69 76  l back the activ
1adab 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 42  e transaction. B
1adac 65 66 6f 72 65 20 64 6f 69 6e 67 0a 20 20 20 20  efore doing.    
1adad 20 20 20 20 20 20 2a 2a 20 73 6f 2c 20 61 62 6f        ** so, abo
1adae 72 74 20 61 6e 79 20 6f 74 68 65 72 20 73 74 61  rt any other sta
1adaf 74 65 6d 65 6e 74 73 20 74 68 69 73 20 68 61 6e  tements this han
1adb0 64 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 68 61  dle currently ha
1adb1 73 20 61 63 74 69 76 65 2e 0a 20 20 20 20 20 20  s active..      
1adb2 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20      */.         
1adb3 20 69 6e 76 61 6c 69 64 61 74 65 43 75 72 73 6f   invalidateCurso
1adb4 72 73 4f 6e 4d 6f 64 69 66 69 65 64 42 74 72 65  rsOnModifiedBtre
1adb5 65 73 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20  es(db);.        
1adb6 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63    sqlite3Rollbac
1adb7 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20  kAll(db);.      
1adb8 20 20 20 20 73 71 6c 69 74 65 33 43 6c 6f 73 65      sqlite3Close
1adb9 53 61 76 65 70 6f 69 6e 74 73 28 64 62 29 3b 0a  Savepoints(db);.
1adba 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75            db->au
1adbb 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20  toCommit = 1;.  
1adbc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1adbd 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 68      }..    /* Ch
1adbe 65 63 6b 20 66 6f 72 20 69 6d 6d 65 64 69 61 74  eck for immediat
1adbf 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 76 69  e foreign key vi
1adc0 6f 6c 61 74 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20  olations. */.   
1adc1 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49   if( p->rc==SQLI
1adc2 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73  TE_OK ){.      s
1adc3 71 6c 69 74 65 33 56 64 62 65 43 68 65 63 6b 46  qlite3VdbeCheckF
1adc4 6b 28 70 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20  k(p, 0);.    }. 
1adc5 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20   .    /* If the 
1adc6 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67  auto-commit flag
1adc7 20 69 73 20 73 65 74 20 61 6e 64 20 74 68 69 73   is set and this
1adc8 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 61 63 74   is the only act
1adc9 69 76 65 20 77 72 69 74 65 72 20 0a 20 20 20 20  ive writer .    
1adca 2a 2a 20 56 4d 2c 20 74 68 65 6e 20 77 65 20 64  ** VM, then we d
1adcb 6f 20 65 69 74 68 65 72 20 61 20 63 6f 6d 6d 69  o either a commi
1adcc 74 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20 6f 66  t or rollback of
1adcd 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61   the current tra
1adce 6e 73 61 63 74 69 6f 6e 2e 20 0a 20 20 20 20 2a  nsaction. .    *
1adcf 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 3a 20 54  *.    ** Note: T
1add0 68 69 73 20 62 6c 6f 63 6b 20 61 6c 73 6f 20 72  his block also r
1add1 75 6e 73 20 69 66 20 6f 6e 65 20 6f 66 20 74 68  uns if one of th
1add2 65 20 73 70 65 63 69 61 6c 20 65 72 72 6f 72 73  e special errors
1add3 20 68 61 6e 64 6c 65 64 20 0a 20 20 20 20 2a 2a   handled .    **
1add4 20 61 62 6f 76 65 20 68 61 73 20 6f 63 63 75 72   above has occur
1add5 72 65 64 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20  red. .    */.   
1add6 20 69 66 28 20 21 73 71 6c 69 74 65 33 56 74 61   if( !sqlite3Vta
1add7 62 49 6e 53 79 6e 63 28 64 62 29 20 0a 20 20 20  bInSync(db) .   
1add8 20 20 26 26 20 64 62 2d 3e 61 75 74 6f 43 6f 6d    && db->autoCom
1add9 6d 69 74 20 0a 20 20 20 20 20 26 26 20 64 62 2d  mit .     && db-
1adda 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3d 3d 28  >writeVdbeCnt==(
1addb 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d 3d 30 29 20  p->readOnly==0) 
1addc 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66  .    ){.      if
1addd 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f  ( p->rc==SQLITE_
1adde 4f 4b 20 7c 7c 20 28 70 2d 3e 65 72 72 6f 72 41  OK || (p->errorA
1addf 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 26  ction==OE_Fail &
1ade0 26 20 21 69 73 53 70 65 63 69 61 6c 45 72 72 6f  & !isSpecialErro
1ade1 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  r) ){.        if
1ade2 28 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 65  ( sqlite3VdbeChe
1ade3 63 6b 46 6b 28 70 2c 20 31 29 20 29 7b 0a 20 20  ckFk(p, 1) ){.  
1ade4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42          sqlite3B
1ade5 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65  treeMutexArrayLe
1ade6 61 76 65 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b  ave(&p->aMutex);
1ade7 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
1ade8 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
1ade9 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1adea 20 20 2f 2a 20 54 68 65 20 61 75 74 6f 2d 63 6f    /* The auto-co
1adeb 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 74 72 75  mmit flag is tru
1adec 65 2c 20 74 68 65 20 76 64 62 65 20 70 72 6f 67  e, the vdbe prog
1aded 72 61 6d 20 77 61 73 20 73 75 63 63 65 73 73 66  ram was successf
1adee 75 6c 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f  ul .        ** o
1adef 72 20 68 69 74 20 61 6e 20 27 4f 52 20 46 41 49  r hit an 'OR FAI
1adf0 4c 27 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 6e  L' constraint an
1adf1 64 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 64  d there are no d
1adf2 65 66 65 72 72 65 64 20 66 6f 72 65 69 67 6e 0a  eferred foreign.
1adf3 20 20 20 20 20 20 20 20 2a 2a 20 6b 65 79 20 63          ** key c
1adf4 6f 6e 73 74 72 61 69 6e 74 73 20 74 6f 20 68 6f  onstraints to ho
1adf5 6c 64 20 75 70 20 74 68 65 20 74 72 61 6e 73 61  ld up the transa
1adf6 63 74 69 6f 6e 2e 20 54 68 69 73 20 6d 65 61 6e  ction. This mean
1adf7 73 20 61 20 63 6f 6d 6d 69 74 20 0a 20 20 20 20  s a commit .    
1adf8 20 20 20 20 2a 2a 20 69 73 20 72 65 71 75 69 72      ** is requir
1adf9 65 64 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20  ed.  */.        
1adfa 72 63 20 3d 20 76 64 62 65 43 6f 6d 6d 69 74 28  rc = vdbeCommit(
1adfb 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 20 20  db, p);.        
1adfc 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42  if( rc==SQLITE_B
1adfd 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 20  USY ){.         
1adfe 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74   sqlite3BtreeMut
1adff 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d  exArrayLeave(&p-
1ae00 3e 61 4d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  >aMutex);.      
1ae01 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1ae02 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20  E_BUSY;.        
1ae03 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51  }else if( rc!=SQ
1ae04 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1ae05 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b       p->rc = rc;
1ae06 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1ae07 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62  e3RollbackAll(db
1ae08 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
1ae09 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e  {.          db->
1ae0a 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 20 3d 20  nDeferredCons = 
1ae0b 30 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  0;.          sql
1ae0c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72 6e  ite3CommitIntern
1ae0d 61 6c 43 68 61 6e 67 65 73 28 64 62 29 3b 0a 20  alChanges(db);. 
1ae0e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
1ae0f 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
1ae10 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c  lite3RollbackAll
1ae11 28 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  (db);.      }.  
1ae12 20 20 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65      db->nStateme
1ae13 6e 74 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  nt = 0;.    }els
1ae14 65 20 69 66 28 20 65 53 74 61 74 65 6d 65 6e 74  e if( eStatement
1ae15 4f 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  Op==0 ){.      i
1ae16 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45  f( p->rc==SQLITE
1ae17 5f 4f 4b 20 7c 7c 20 70 2d 3e 65 72 72 6f 72 41  _OK || p->errorA
1ae18 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69 6c 20 29  ction==OE_Fail )
1ae19 7b 0a 20 20 20 20 20 20 20 20 65 53 74 61 74 65  {.        eState
1ae1a 6d 65 6e 74 4f 70 20 3d 20 53 41 56 45 50 4f 49  mentOp = SAVEPOI
1ae1b 4e 54 5f 52 45 4c 45 41 53 45 3b 0a 20 20 20 20  NT_RELEASE;.    
1ae1c 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 65    }else if( p->e
1ae1d 72 72 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 41  rrorAction==OE_A
1ae1e 62 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20  bort ){.        
1ae1f 65 53 74 61 74 65 6d 65 6e 74 4f 70 20 3d 20 53  eStatementOp = S
1ae20 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43  AVEPOINT_ROLLBAC
1ae21 4b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  K;.      }else{.
1ae22 20 20 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61          invalida
1ae23 74 65 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66  teCursorsOnModif
1ae24 69 65 64 42 74 72 65 65 73 28 64 62 29 3b 0a 20  iedBtrees(db);. 
1ae25 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f         sqlite3Ro
1ae26 6c 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20  llbackAll(db);. 
1ae27 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6c         sqlite3Cl
1ae28 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 28 64 62  oseSavepoints(db
1ae29 29 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 61  );.        db->a
1ae2a 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20  utoCommit = 1;. 
1ae2b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a       }.    }.  .
1ae2c 20 20 20 20 2f 2a 20 49 66 20 65 53 74 61 74 65      /* If eState
1ae2d 6d 65 6e 74 4f 70 20 69 73 20 6e 6f 6e 2d 7a 65  mentOp is non-ze
1ae2e 72 6f 2c 20 74 68 65 6e 20 61 20 73 74 61 74 65  ro, then a state
1ae2f 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
1ae30 20 6e 65 65 64 73 20 74 6f 0a 20 20 20 20 2a 2a   needs to.    **
1ae31 20 62 65 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72   be committed or
1ae32 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 43 61   rolled back. Ca
1ae33 6c 6c 20 73 71 6c 69 74 65 33 56 64 62 65 43 6c  ll sqlite3VdbeCl
1ae34 6f 73 65 53 74 61 74 65 6d 65 6e 74 28 29 20 74  oseStatement() t
1ae35 6f 0a 20 20 20 20 2a 2a 20 64 6f 20 73 6f 2e 20  o.    ** do so. 
1ae36 49 66 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f  If this operatio
1ae37 6e 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72  n returns an err
1ae38 6f 72 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72  or, and the curr
1ae39 65 6e 74 20 73 74 61 74 65 6d 65 6e 74 0a 20 20  ent statement.  
1ae3a 20 20 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20    ** error code 
1ae3b 69 73 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 72 20  is SQLITE_OK or 
1ae3c 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
1ae3d 54 2c 20 74 68 65 6e 20 73 65 74 20 74 68 65 20  T, then set the 
1ae3e 65 72 72 6f 72 0a 20 20 20 20 2a 2a 20 63 6f 64  error.    ** cod
1ae3f 65 20 74 6f 20 74 68 65 20 6e 65 77 20 76 61 6c  e to the new val
1ae40 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ue..    */.    i
1ae41 66 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70 20  f( eStatementOp 
1ae42 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
1ae43 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74  lite3VdbeCloseSt
1ae44 61 74 65 6d 65 6e 74 28 70 2c 20 65 53 74 61 74  atement(p, eStat
1ae45 65 6d 65 6e 74 4f 70 29 3b 0a 20 20 20 20 20 20  ementOp);.      
1ae46 69 66 28 20 72 63 20 26 26 20 28 70 2d 3e 72 63  if( rc && (p->rc
1ae47 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70  ==SQLITE_OK || p
1ae48 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 43 4f 4e  ->rc==SQLITE_CON
1ae49 53 54 52 41 49 4e 54 29 20 29 7b 0a 20 20 20 20  STRAINT) ){.    
1ae4a 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b 0a      p->rc = rc;.
1ae4b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
1ae4c 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72  bFree(db, p->zEr
1ae4d 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 20 20 70  rMsg);.        p
1ae4e 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  ->zErrMsg = 0;. 
1ae4f 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a       }.    }.  .
1ae50 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 77      /* If this w
1ae51 61 73 20 61 6e 20 49 4e 53 45 52 54 2c 20 55 50  as an INSERT, UP
1ae52 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20 61  DATE or DELETE a
1ae53 6e 64 20 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20  nd no statement 
1ae54 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 20 20  transaction.    
1ae55 2a 2a 20 68 61 73 20 62 65 65 6e 20 72 6f 6c 6c  ** has been roll
1ae56 65 64 20 62 61 63 6b 2c 20 75 70 64 61 74 65 20  ed back, update 
1ae57 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
1ae58 6e 65 63 74 69 6f 6e 20 63 68 61 6e 67 65 2d 63  nection change-c
1ae59 6f 75 6e 74 65 72 2e 20 0a 20 20 20 20 2a 2f 0a  ounter. .    */.
1ae5a 20 20 20 20 69 66 28 20 70 2d 3e 63 68 61 6e 67      if( p->chang
1ae5b 65 43 6e 74 4f 6e 20 29 7b 0a 20 20 20 20 20 20  eCntOn ){.      
1ae5c 69 66 28 20 65 53 74 61 74 65 6d 65 6e 74 4f 70  if( eStatementOp
1ae5d 21 3d 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c  !=SAVEPOINT_ROLL
1ae5e 42 41 43 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  BACK ){.        
1ae5f 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 68  sqlite3VdbeSetCh
1ae60 61 6e 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68  anges(db, p->nCh
1ae61 61 6e 67 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c  ange);.      }el
1ae62 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
1ae63 74 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 65  te3VdbeSetChange
1ae64 73 28 64 62 2c 20 30 29 3b 0a 20 20 20 20 20 20  s(db, 0);.      
1ae65 7d 0a 20 20 20 20 20 20 70 2d 3e 6e 43 68 61 6e  }.      p->nChan
1ae66 67 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  ge = 0;.    }.  
1ae67 0a 20 20 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b  .    /* Rollback
1ae68 20 6f 72 20 63 6f 6d 6d 69 74 20 61 6e 79 20 73   or commit any s
1ae69 63 68 65 6d 61 20 63 68 61 6e 67 65 73 20 74 68  chema changes th
1ae6a 61 74 20 6f 63 63 75 72 72 65 64 2e 20 2a 2f 0a  at occurred. */.
1ae6b 20 20 20 20 69 66 28 20 70 2d 3e 72 63 21 3d 53      if( p->rc!=S
1ae6c 51 4c 49 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e  QLITE_OK && db->
1ae6d 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74  flags&SQLITE_Int
1ae6e 65 72 6e 43 68 61 6e 67 65 73 20 29 7b 0a 20 20  ernChanges ){.  
1ae6f 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65 74      sqlite3Reset
1ae70 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
1ae71 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 64 62 2d  b, 0);.      db-
1ae72 3e 66 6c 61 67 73 20 3d 20 28 64 62 2d 3e 66 6c  >flags = (db->fl
1ae73 61 67 73 20 7c 20 53 51 4c 49 54 45 5f 49 6e 74  ags | SQLITE_Int
1ae74 65 72 6e 43 68 61 6e 67 65 73 29 3b 0a 20 20 20  ernChanges);.   
1ae75 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61   }..    /* Relea
1ae76 73 65 20 74 68 65 20 6c 6f 63 6b 73 20 2a 2f 0a  se the locks */.
1ae77 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1ae78 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65 28  MutexArrayLeave(
1ae79 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 7d  &p->aMutex);.  }
1ae7a 0a 0a 20 20 2f 2a 20 57 65 20 68 61 76 65 20 73  ..  /* We have s
1ae7b 75 63 63 65 73 73 66 75 6c 6c 79 20 68 61 6c 74  uccessfully halt
1ae7c 65 64 20 61 6e 64 20 63 6c 6f 73 65 64 20 74 68  ed and closed th
1ae7d 65 20 56 4d 2e 20 20 52 65 63 6f 72 64 20 74 68  e VM.  Record th
1ae7e 69 73 20 66 61 63 74 2e 20 2a 2f 0a 20 20 69 66  is fact. */.  if
1ae7f 28 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20  ( p->pc>=0 ){.  
1ae80 20 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65    db->activeVdbe
1ae81 43 6e 74 2d 2d 3b 0a 20 20 20 20 69 66 28 20 21  Cnt--;.    if( !
1ae82 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20  p->readOnly ){. 
1ae83 20 20 20 20 20 64 62 2d 3e 77 72 69 74 65 56 64       db->writeVd
1ae84 62 65 43 6e 74 2d 2d 3b 0a 20 20 20 20 7d 0a 20  beCnt--;.    }. 
1ae85 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61     assert( db->a
1ae86 63 74 69 76 65 56 64 62 65 43 6e 74 3e 3d 64 62  ctiveVdbeCnt>=db
1ae87 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 20 29  ->writeVdbeCnt )
1ae88 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6d 61 67 69 63  ;.  }.  p->magic
1ae89 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 48 41   = VDBE_MAGIC_HA
1ae8a 4c 54 3b 0a 20 20 63 68 65 63 6b 41 63 74 69 76  LT;.  checkActiv
1ae8b 65 56 64 62 65 43 6e 74 28 64 62 29 3b 0a 20 20  eVdbeCnt(db);.  
1ae8c 69 66 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  if( p->db->mallo
1ae8d 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70  cFailed ){.    p
1ae8e 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  ->rc = SQLITE_NO
1ae8f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  MEM;.  }..  /* I
1ae90 66 20 74 68 65 20 61 75 74 6f 2d 63 6f 6d 6d 69  f the auto-commi
1ae91 74 20 66 6c 61 67 20 69 73 20 73 65 74 20 74 6f  t flag is set to
1ae92 20 74 72 75 65 2c 20 74 68 65 6e 20 61 6e 79 20   true, then any 
1ae93 6c 6f 63 6b 73 20 74 68 61 74 20 77 65 72 65 20  locks that were 
1ae94 68 65 6c 64 0a 20 20 2a 2a 20 62 79 20 63 6f 6e  held.  ** by con
1ae95 6e 65 63 74 69 6f 6e 20 64 62 20 68 61 76 65 20  nection db have 
1ae96 6e 6f 77 20 62 65 65 6e 20 72 65 6c 65 61 73 65  now been release
1ae97 64 2e 20 43 61 6c 6c 20 73 71 6c 69 74 65 33 43  d. Call sqlite3C
1ae98 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65  onnectionUnlocke
1ae99 64 28 29 20 0a 20 20 2a 2a 20 74 6f 20 69 6e 76  d() .  ** to inv
1ae9a 6f 6b 65 20 61 6e 79 20 72 65 71 75 69 72 65 64  oke any required
1ae9b 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63   unlock-notify c
1ae9c 61 6c 6c 62 61 63 6b 73 2e 0a 20 20 2a 2f 0a 20  allbacks..  */. 
1ae9d 20 69 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d   if( db->autoCom
1ae9e 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  mit ){.    sqlit
1ae9f 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 55 6e 6c 6f  e3ConnectionUnlo
1aea0 63 6b 65 64 28 64 62 29 3b 0a 20 20 7d 0a 0a 20  cked(db);.  }.. 
1aea1 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 63 74   assert( db->act
1aea2 69 76 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20  iveVdbeCnt>0 || 
1aea3 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d  db->autoCommit==
1aea4 30 20 7c 7c 20 64 62 2d 3e 6e 53 74 61 74 65 6d  0 || db->nStatem
1aea5 65 6e 74 3d 3d 30 20 29 3b 0a 20 20 72 65 74 75  ent==0 );.  retu
1aea6 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
1aea7 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 56 44 42  ../*.** Each VDB
1aea8 45 20 68 6f 6c 64 73 20 74 68 65 20 72 65 73 75  E holds the resu
1aea9 6c 74 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72  lt of the most r
1aeaa 65 63 65 6e 74 20 73 71 6c 69 74 65 33 5f 73 74  ecent sqlite3_st
1aeab 65 70 28 29 20 63 61 6c 6c 0a 2a 2a 20 69 6e 20  ep() call.** in 
1aeac 70 2d 3e 72 63 2e 20 20 54 68 69 73 20 72 6f 75  p->rc.  This rou
1aead 74 69 6e 65 20 73 65 74 73 20 74 68 61 74 20 72  tine sets that r
1aeae 65 73 75 6c 74 20 62 61 63 6b 20 74 6f 20 53 51  esult back to SQ
1aeaf 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49  LITE_OK..*/.SQLI
1aeb0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1aeb1 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74  sqlite3VdbeReset
1aeb2 53 74 65 70 52 65 73 75 6c 74 28 56 64 62 65 20  StepResult(Vdbe 
1aeb3 2a 70 29 7b 0a 20 20 70 2d 3e 72 63 20 3d 20 53  *p){.  p->rc = S
1aeb4 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
1aeb5 2a 2a 20 43 6c 65 61 6e 20 75 70 20 61 20 56 44  ** Clean up a VD
1aeb6 42 45 20 61 66 74 65 72 20 65 78 65 63 75 74 69  BE after executi
1aeb7 6f 6e 20 62 75 74 20 64 6f 20 6e 6f 74 20 64 65  on but do not de
1aeb8 6c 65 74 65 20 74 68 65 20 56 44 42 45 20 6a 75  lete the VDBE ju
1aeb9 73 74 20 79 65 74 2e 0a 2a 2a 20 57 72 69 74 65  st yet..** Write
1aeba 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61   any error messa
1aebb 67 65 73 20 69 6e 74 6f 20 2a 70 7a 45 72 72 4d  ges into *pzErrM
1aebc 73 67 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  sg.  Return the 
1aebd 72 65 73 75 6c 74 20 63 6f 64 65 2e 0a 2a 2a 0a  result code..**.
1aebe 2a 2a 20 41 66 74 65 72 20 74 68 69 73 20 72 6f  ** After this ro
1aebf 75 74 69 6e 65 20 69 73 20 72 75 6e 2c 20 74 68  utine is run, th
1aec0 65 20 56 44 42 45 20 73 68 6f 75 6c 64 20 62 65  e VDBE should be
1aec1 20 72 65 61 64 79 20 74 6f 20 62 65 20 65 78 65   ready to be exe
1aec2 63 75 74 65 64 0a 2a 2a 20 61 67 61 69 6e 2e 0a  cuted.** again..
1aec3 2a 2a 0a 2a 2a 20 54 6f 20 6c 6f 6f 6b 20 61 74  **.** To look at
1aec4 20 69 74 20 61 6e 6f 74 68 65 72 20 77 61 79 2c   it another way,
1aec5 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
1aec6 73 65 74 73 20 74 68 65 20 73 74 61 74 65 20 6f  sets the state o
1aec7 66 20 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c  f the.** virtual
1aec8 20 6d 61 63 68 69 6e 65 20 66 72 6f 6d 20 56 44   machine from VD
1aec9 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 6f 72 20  BE_MAGIC_RUN or 
1aeca 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20  VDBE_MAGIC_HALT 
1aecb 62 61 63 6b 20 74 6f 0a 2a 2a 20 56 44 42 45 5f  back to.** VDBE_
1aecc 4d 41 47 49 43 5f 49 4e 49 54 2e 0a 2a 2f 0a 53  MAGIC_INIT..*/.S
1aecd 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1aece 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73  t sqlite3VdbeRes
1aecf 65 74 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73  et(Vdbe *p){.  s
1aed0 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 64 62  qlite3 *db;.  db
1aed1 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20   = p->db;..  /* 
1aed2 49 66 20 74 68 65 20 56 4d 20 64 69 64 20 6e 6f  If the VM did no
1aed3 74 20 72 75 6e 20 74 6f 20 63 6f 6d 70 6c 65 74  t run to complet
1aed4 69 6f 6e 20 6f 72 20 69 66 20 69 74 20 65 6e 63  ion or if it enc
1aed5 6f 75 6e 74 65 72 65 64 20 61 6e 0a 20 20 2a 2a  ountered an.  **
1aed6 20 65 72 72 6f 72 2c 20 74 68 65 6e 20 69 74 20   error, then it 
1aed7 6d 69 67 68 74 20 6e 6f 74 20 68 61 76 65 20 62  might not have b
1aed8 65 65 6e 20 68 61 6c 74 65 64 20 70 72 6f 70 65  een halted prope
1aed9 72 6c 79 2e 20 20 53 6f 20 68 61 6c 74 0a 20 20  rly.  So halt.  
1aeda 2a 2a 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a  ** it now..  */.
1aedb 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
1aedc 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 73  afetyOn(db);.  s
1aedd 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 70  qlite3VdbeHalt(p
1aede 29 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74  );.  (void)sqlit
1aedf 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
1aee0 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 56 44  ..  /* If the VD
1aee1 42 45 20 68 61 73 20 62 65 20 72 75 6e 20 65 76  BE has be run ev
1aee2 65 6e 20 70 61 72 74 69 61 6c 6c 79 2c 20 74 68  en partially, th
1aee3 65 6e 20 74 72 61 6e 73 66 65 72 20 74 68 65 20  en transfer the 
1aee4 65 72 72 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20  error code.  ** 
1aee5 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67  and error messag
1aee6 65 20 66 72 6f 6d 20 74 68 65 20 56 44 42 45 20  e from the VDBE 
1aee7 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e 20 64 61  into the main da
1aee8 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65  tabase structure
1aee9 2e 20 20 42 75 74 0a 20 20 2a 2a 20 69 66 20 74  .  But.  ** if t
1aeea 68 65 20 56 44 42 45 20 68 61 73 20 6a 75 73 74  he VDBE has just
1aeeb 20 62 65 65 6e 20 73 65 74 20 74 6f 20 72 75 6e   been set to run
1aeec 20 62 75 74 20 68 61 73 20 6e 6f 74 20 61 63 74   but has not act
1aeed 75 61 6c 6c 79 20 65 78 65 63 75 74 65 64 20 61  ually executed a
1aeee 6e 79 0a 20 20 2a 2a 20 69 6e 73 74 72 75 63 74  ny.  ** instruct
1aeef 69 6f 6e 73 20 79 65 74 2c 20 6c 65 61 76 65 20  ions yet, leave 
1aef0 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
1aef1 65 20 65 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74  e error informat
1aef2 69 6f 6e 20 75 6e 63 68 61 6e 67 65 64 2e 0a 20  ion unchanged.. 
1aef3 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e   */.  if( p->pc>
1aef4 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  =0 ){.    if( p-
1aef5 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20  >zErrMsg ){.    
1aef6 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65    sqlite3BeginBe
1aef7 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20  nignMalloc();.  
1aef8 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65      sqlite3Value
1aef9 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c  SetStr(db->pErr,
1aefa 2d 31 2c 70 2d 3e 7a 45 72 72 4d 73 67 2c 53 51  -1,p->zErrMsg,SQ
1aefb 4c 49 54 45 5f 55 54 46 38 2c 53 51 4c 49 54 45  LITE_UTF8,SQLITE
1aefc 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20  _TRANSIENT);.   
1aefd 20 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e     sqlite3EndBen
1aefe 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20  ignMalloc();.   
1aeff 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d     db->errCode =
1af00 20 70 2d 3e 72 63 3b 0a 20 20 20 20 20 20 73 71   p->rc;.      sq
1af01 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1af02 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  p->zErrMsg);.   
1af03 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20     p->zErrMsg = 
1af04 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  0;.    }else if(
1af05 20 70 2d 3e 72 63 20 29 7b 0a 20 20 20 20 20 20   p->rc ){.      
1af06 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
1af07 20 70 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20   p->rc, 0);.    
1af08 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
1af09 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
1af0a 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20  LITE_OK, 0);.   
1af0b 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70   }.  }else if( p
1af0c 2d 3e 72 63 20 26 26 20 70 2d 3e 65 78 70 69 72  ->rc && p->expir
1af0d 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  ed ){.    /* The
1af0e 20 65 78 70 69 72 65 64 20 66 6c 61 67 20 77 61   expired flag wa
1af0f 73 20 73 65 74 20 6f 6e 20 74 68 65 20 56 44 42  s set on the VDB
1af10 45 20 62 65 66 6f 72 65 20 74 68 65 20 66 69 72  E before the fir
1af11 73 74 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 74  st call.    ** t
1af12 6f 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  o sqlite3_step()
1af13 2e 20 46 6f 72 20 63 6f 6e 73 69 73 74 65 6e 63  . For consistenc
1af14 79 20 28 73 69 6e 63 65 20 73 71 6c 69 74 65 33  y (since sqlite3
1af15 5f 73 74 65 70 28 29 20 77 61 73 0a 20 20 20 20  _step() was.    
1af16 2a 2a 20 63 61 6c 6c 65 64 29 2c 20 73 65 74 20  ** called), set 
1af17 74 68 65 20 64 61 74 61 62 61 73 65 20 65 72 72  the database err
1af18 6f 72 20 69 6e 20 74 68 69 73 20 63 61 73 65 20  or in this case 
1af19 61 73 20 77 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a  as well..    */.
1af1a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1af1b 28 64 62 2c 20 70 2d 3e 72 63 2c 20 30 29 3b 0a  (db, p->rc, 0);.
1af1c 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65      sqlite3Value
1af1d 53 65 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c  SetStr(db->pErr,
1af1e 20 2d 31 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 2c   -1, p->zErrMsg,
1af1f 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51   SQLITE_UTF8, SQ
1af20 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
1af21 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
1af22 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73  ee(db, p->zErrMs
1af23 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d  g);.    p->zErrM
1af24 73 67 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  sg = 0;.  }..  /
1af25 2a 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65  * Reclaim all me
1af26 6d 6f 72 79 20 75 73 65 64 20 62 79 20 74 68 65  mory used by the
1af27 20 56 44 42 45 0a 20 20 2a 2f 0a 20 20 43 6c 65   VDBE.  */.  Cle
1af28 61 6e 75 70 28 70 29 3b 0a 0a 20 20 2f 2a 20 53  anup(p);..  /* S
1af29 61 76 65 20 70 72 6f 66 69 6c 69 6e 67 20 69 6e  ave profiling in
1af2a 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74  formation from t
1af2b 68 69 73 20 56 44 42 45 20 72 75 6e 2e 0a 20 20  his VDBE run..  
1af2c 2a 2f 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50  */.#ifdef VDBE_P
1af2d 52 4f 46 49 4c 45 0a 20 20 7b 0a 20 20 20 20 46  ROFILE.  {.    F
1af2e 49 4c 45 20 2a 6f 75 74 20 3d 20 66 6f 70 65 6e  ILE *out = fopen
1af2f 28 22 76 64 62 65 5f 70 72 6f 66 69 6c 65 2e 6f  ("vdbe_profile.o
1af30 75 74 22 2c 20 22 61 22 29 3b 0a 20 20 20 20 69  ut", "a");.    i
1af31 66 28 20 6f 75 74 20 29 7b 0a 20 20 20 20 20 20  f( out ){.      
1af32 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 66 70 72  int i;.      fpr
1af33 69 6e 74 66 28 6f 75 74 2c 20 22 2d 2d 2d 2d 20  intf(out, "---- 
1af34 22 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  ");.      for(i=
1af35 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b  0; i<p->nOp; i++
1af36 29 7b 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e  ){.        fprin
1af37 74 66 28 6f 75 74 2c 20 22 25 30 32 78 22 2c 20  tf(out, "%02x", 
1af38 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65  p->aOp[i].opcode
1af39 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1af3a 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c   fprintf(out, "\
1af3b 6e 22 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  n");.      for(i
1af3c 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b  =0; i<p->nOp; i+
1af3d 2b 29 7b 0a 20 20 20 20 20 20 20 20 66 70 72 69  +){.        fpri
1af3e 6e 74 66 28 6f 75 74 2c 20 22 25 36 64 20 25 31  ntf(out, "%6d %1
1af3f 30 6c 6c 64 20 25 38 6c 6c 64 20 22 2c 0a 20 20  0lld %8lld ",.  
1af40 20 20 20 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b           p->aOp[
1af41 69 5d 2e 63 6e 74 2c 0a 20 20 20 20 20 20 20 20  i].cnt,.        
1af42 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63     p->aOp[i].cyc
1af43 6c 65 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20  les,.           
1af44 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 3e 30 20  p->aOp[i].cnt>0 
1af45 3f 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c  ? p->aOp[i].cycl
1af46 65 73 2f 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74  es/p->aOp[i].cnt
1af47 20 3a 20 30 0a 20 20 20 20 20 20 20 20 29 3b 0a   : 0.        );.
1af48 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1af49 64 62 65 50 72 69 6e 74 4f 70 28 6f 75 74 2c 20  dbePrintOp(out, 
1af4a 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a  i, &p->aOp[i]);.
1af4b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 63        }.      fc
1af4c 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20 20 20 7d  lose(out);.    }
1af4d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 2d  .  }.#endif.  p-
1af4e 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41  >magic = VDBE_MA
1af4f 47 49 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75  GIC_INIT;.  retu
1af50 72 6e 20 70 2d 3e 72 63 20 26 20 64 62 2d 3e 65  rn p->rc & db->e
1af51 72 72 4d 61 73 6b 3b 0a 7d 0a 20 0a 2f 2a 0a 2a  rrMask;.}. ./*.*
1af52 2a 20 43 6c 65 61 6e 20 75 70 20 61 6e 64 20 64  * Clean up and d
1af53 65 6c 65 74 65 20 61 20 56 44 42 45 20 61 66 74  elete a VDBE aft
1af54 65 72 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 52  er execution.  R
1af55 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72  eturn an integer
1af56 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65   which is.** the
1af57 20 72 65 73 75 6c 74 20 63 6f 64 65 2e 20 20 57   result code.  W
1af58 72 69 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d  rite any error m
1af59 65 73 73 61 67 65 20 74 65 78 74 20 69 6e 74 6f  essage text into
1af5a 20 2a 70 7a 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53   *pzErrMsg..*/.S
1af5b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1af5c 74 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e  t sqlite3VdbeFin
1af5d 61 6c 69 7a 65 28 56 64 62 65 20 2a 70 29 7b 0a  alize(Vdbe *p){.
1af5e 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
1af5f 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 6d  E_OK;.  if( p->m
1af60 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43  agic==VDBE_MAGIC
1af61 5f 52 55 4e 20 7c 7c 20 70 2d 3e 6d 61 67 69 63  _RUN || p->magic
1af62 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c  ==VDBE_MAGIC_HAL
1af63 54 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  T ){.    rc = sq
1af64 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 28 70  lite3VdbeReset(p
1af65 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  );.    assert( (
1af66 72 63 20 26 20 70 2d 3e 64 62 2d 3e 65 72 72 4d  rc & p->db->errM
1af67 61 73 6b 29 3d 3d 72 63 20 29 3b 0a 20 20 7d 0a  ask)==rc );.  }.
1af68 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c    sqlite3VdbeDel
1af69 65 74 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ete(p);.  return
1af6a 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61   rc;.}../*.** Ca
1af6b 6c 6c 20 74 68 65 20 64 65 73 74 72 75 63 74 6f  ll the destructo
1af6c 72 20 66 6f 72 20 65 61 63 68 20 61 75 78 64 61  r for each auxda
1af6d 74 61 20 65 6e 74 72 79 20 69 6e 20 70 56 64 62  ta entry in pVdb
1af6e 65 46 75 6e 63 20 66 6f 72 20 77 68 69 63 68 0a  eFunc for which.
1af6f 2a 2a 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  ** the correspon
1af70 64 69 6e 67 20 62 69 74 20 69 6e 20 6d 61 73 6b  ding bit in mask
1af71 20 69 73 20 63 6c 65 61 72 2e 20 20 41 75 78 64   is clear.  Auxd
1af72 61 74 61 20 65 6e 74 72 69 65 73 20 62 65 79 6f  ata entries beyo
1af73 6e 64 20 33 31 0a 2a 2a 20 61 72 65 20 61 6c 77  nd 31.** are alw
1af74 61 79 73 20 64 65 73 74 72 6f 79 65 64 2e 20 20  ays destroyed.  
1af75 54 6f 20 64 65 73 74 72 6f 79 20 61 6c 6c 20 61  To destroy all a
1af76 75 78 64 61 74 61 20 65 6e 74 72 69 65 73 2c 20  uxdata entries, 
1af77 63 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 6f 75  call this.** rou
1af78 74 69 6e 65 20 77 69 74 68 20 6d 61 73 6b 3d 3d  tine with mask==
1af79 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  0..*/.SQLITE_PRI
1af7a 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1af7b 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44 61  3VdbeDeleteAuxDa
1af7c 74 61 28 56 64 62 65 46 75 6e 63 20 2a 70 56 64  ta(VdbeFunc *pVd
1af7d 62 65 46 75 6e 63 2c 20 69 6e 74 20 6d 61 73 6b  beFunc, int mask
1af7e 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f  ){.  int i;.  fo
1af7f 72 28 69 3d 30 3b 20 69 3c 70 56 64 62 65 46 75  r(i=0; i<pVdbeFu
1af80 6e 63 2d 3e 6e 41 75 78 3b 20 69 2b 2b 29 7b 0a  nc->nAux; i++){.
1af81 20 20 20 20 73 74 72 75 63 74 20 41 75 78 44 61      struct AuxDa
1af82 74 61 20 2a 70 41 75 78 20 3d 20 26 70 56 64 62  ta *pAux = &pVdb
1af83 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 69 5d 3b  eFunc->apAux[i];
1af84 0a 20 20 20 20 69 66 28 20 28 69 3e 33 31 20 7c  .    if( (i>31 |
1af85 7c 20 21 28 6d 61 73 6b 26 28 28 28 75 33 32 29  | !(mask&(((u32)
1af86 31 29 3c 3c 69 29 29 29 20 26 26 20 70 41 75 78  1)<<i))) && pAux
1af87 2d 3e 70 41 75 78 20 29 7b 0a 20 20 20 20 20 20  ->pAux ){.      
1af88 69 66 28 20 70 41 75 78 2d 3e 78 44 65 6c 65 74  if( pAux->xDelet
1af89 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 41 75  e ){.        pAu
1af8a 78 2d 3e 78 44 65 6c 65 74 65 28 70 41 75 78 2d  x->xDelete(pAux-
1af8b 3e 70 41 75 78 29 3b 0a 20 20 20 20 20 20 7d 0a  >pAux);.      }.
1af8c 20 20 20 20 20 20 70 41 75 78 2d 3e 70 41 75 78        pAux->pAux
1af8d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
1af8e 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
1af8f 61 6e 20 65 6e 74 69 72 65 20 56 44 42 45 2e 0a  an entire VDBE..
1af90 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1af91 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
1af92 62 65 44 65 6c 65 74 65 28 56 64 62 65 20 2a 70  beDelete(Vdbe *p
1af93 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
1af94 3b 0a 0a 20 20 69 66 28 20 4e 45 56 45 52 28 70  ;..  if( NEVER(p
1af95 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 20  ==0) ) return;. 
1af96 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69   db = p->db;.  i
1af97 66 28 20 70 2d 3e 70 50 72 65 76 20 29 7b 0a 20  f( p->pPrev ){. 
1af98 20 20 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65     p->pPrev->pNe
1af99 78 74 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20  xt = p->pNext;. 
1af9a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
1af9b 72 74 28 20 64 62 2d 3e 70 56 64 62 65 3d 3d 70  rt( db->pVdbe==p
1af9c 20 29 3b 0a 20 20 20 20 64 62 2d 3e 70 56 64 62   );.    db->pVdb
1af9d 65 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20  e = p->pNext;.  
1af9e 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4e 65 78 74  }.  if( p->pNext
1af9f 20 29 7b 0a 20 20 20 20 70 2d 3e 70 4e 65 78 74   ){.    p->pNext
1afa0 2d 3e 70 50 72 65 76 20 3d 20 70 2d 3e 70 50 72  ->pPrev = p->pPr
1afa1 65 76 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73  ev;.  }.  releas
1afa2 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 56 61  eMemArray(p->aVa
1afa3 72 2c 20 70 2d 3e 6e 56 61 72 29 3b 0a 20 20 72  r, p->nVar);.  r
1afa4 65 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 70  eleaseMemArray(p
1afa5 2d 3e 61 43 6f 6c 4e 61 6d 65 2c 20 70 2d 3e 6e  ->aColName, p->n
1afa6 52 65 73 43 6f 6c 75 6d 6e 2a 43 4f 4c 4e 41 4d  ResColumn*COLNAM
1afa7 45 5f 4e 29 3b 0a 20 20 76 64 62 65 46 72 65 65  E_N);.  vdbeFree
1afa8 4f 70 41 72 72 61 79 28 64 62 2c 20 70 2d 3e 61  OpArray(db, p->a
1afa9 4f 70 2c 20 70 2d 3e 6e 4f 70 29 3b 0a 20 20 73  Op, p->nOp);.  s
1afaa 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1afab 20 70 2d 3e 61 4c 61 62 65 6c 29 3b 0a 20 20 73   p->aLabel);.  s
1afac 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1afad 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b 0a 20   p->aColName);. 
1afae 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1afaf 62 2c 20 70 2d 3e 7a 53 71 6c 29 3b 0a 20 20 70  b, p->zSql);.  p
1afb0 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d  ->magic = VDBE_M
1afb1 41 47 49 43 5f 44 45 41 44 3b 0a 20 20 73 71 6c  AGIC_DEAD;.  sql
1afb2 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1afb3 2d 3e 70 46 72 65 65 29 3b 0a 20 20 73 71 6c 69  ->pFree);.  sqli
1afb4 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29  te3DbFree(db, p)
1afb5 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  ;.}../*.** Make 
1afb6 73 75 72 65 20 74 68 65 20 63 75 72 73 6f 72 20  sure the cursor 
1afb7 70 20 69 73 20 72 65 61 64 79 20 74 6f 20 72 65  p is ready to re
1afb8 61 64 20 6f 72 20 77 72 69 74 65 20 74 68 65 20  ad or write the 
1afb9 72 6f 77 20 74 6f 20 77 68 69 63 68 20 69 74 0a  row to which it.
1afba 2a 2a 20 77 61 73 20 6c 61 73 74 20 70 6f 73 69  ** was last posi
1afbb 74 69 6f 6e 65 64 2e 20 20 52 65 74 75 72 6e 20  tioned.  Return 
1afbc 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66  an error code if
1afbd 20 61 6e 20 4f 4f 4d 20 66 61 75 6c 74 20 6f 72   an OOM fault or
1afbe 20 49 2f 4f 20 65 72 72 6f 72 0a 2a 2a 20 70 72   I/O error.** pr
1afbf 65 76 65 6e 74 73 20 75 73 20 66 72 6f 6d 20 70  events us from p
1afc0 6f 73 69 74 69 6f 6e 69 6e 67 20 74 68 65 20 63  ositioning the c
1afc1 75 72 73 6f 72 20 74 6f 20 69 74 73 20 63 6f 72  ursor to its cor
1afc2 72 65 63 74 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a  rect position..*
1afc3 2a 0a 2a 2a 20 49 66 20 61 20 4d 6f 76 65 54 6f  *.** If a MoveTo
1afc4 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 70 65   operation is pe
1afc5 6e 64 69 6e 67 20 6f 6e 20 74 68 65 20 67 69 76  nding on the giv
1afc6 65 6e 20 63 75 72 73 6f 72 2c 20 74 68 65 6e 20  en cursor, then 
1afc7 64 6f 20 74 68 61 74 0a 2a 2a 20 4d 6f 76 65 54  do that.** MoveT
1afc8 6f 20 6e 6f 77 2e 20 20 49 66 20 6e 6f 20 6d 6f  o now.  If no mo
1afc9 76 65 20 69 73 20 70 65 6e 64 69 6e 67 2c 20 63  ve is pending, c
1afca 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
1afcb 68 65 20 72 6f 77 20 68 61 73 20 62 65 65 6e 0a  he row has been.
1afcc 2a 2a 20 64 65 6c 65 74 65 64 20 6f 75 74 20 66  ** deleted out f
1afcd 72 6f 6d 20 75 6e 64 65 72 20 74 68 65 20 63 75  rom under the cu
1afce 72 73 6f 72 20 61 6e 64 20 69 66 20 69 74 20 68  rsor and if it h
1afcf 61 73 2c 20 6d 61 72 6b 20 74 68 65 20 72 6f 77  as, mark the row
1afd0 20 61 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20 72 6f   as.** a NULL ro
1afd1 77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  w..**.** If the 
1afd2 63 75 72 73 6f 72 20 69 73 20 61 6c 72 65 61 64  cursor is alread
1afd3 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68  y pointing to th
1afd4 65 20 63 6f 72 72 65 63 74 20 72 6f 77 20 61 6e  e correct row an
1afd5 64 20 74 68 61 74 20 72 6f 77 20 68 61 73 0a 2a  d that row has.*
1afd6 2a 20 6e 6f 74 20 62 65 65 6e 20 64 65 6c 65 74  * not been delet
1afd7 65 64 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65  ed out from unde
1afd8 72 20 74 68 65 20 63 75 72 73 6f 72 2c 20 74 68  r the cursor, th
1afd9 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
1afda 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53  is a no-op..*/.S
1afdb 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1afdc 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  t sqlite3VdbeCur
1afdd 73 6f 72 4d 6f 76 65 74 6f 28 56 64 62 65 43 75  sorMoveto(VdbeCu
1afde 72 73 6f 72 20 2a 70 29 7b 0a 20 20 69 66 28 20  rsor *p){.  if( 
1afdf 70 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  p->deferredMovet
1afe0 6f 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73  o ){.    int res
1afe1 2c 20 72 63 3b 0a 23 69 66 64 65 66 20 53 51 4c  , rc;.#ifdef SQL
1afe2 49 54 45 5f 54 45 53 54 0a 20 20 20 20 65 78 74  ITE_TEST.    ext
1afe3 65 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  ern int sqlite3_
1afe4 73 65 61 72 63 68 5f 63 6f 75 6e 74 3b 0a 23 65  search_count;.#e
1afe5 6e 64 69 66 0a 20 20 20 20 61 73 73 65 72 74 28  ndif.    assert(
1afe6 20 70 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20   p->isTable );. 
1afe7 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1afe8 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b  treeMovetoUnpack
1afe9 65 64 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 30  ed(p->pCursor, 0
1afea 2c 20 70 2d 3e 6d 6f 76 65 74 6f 54 61 72 67 65  , p->movetoTarge
1afeb 74 2c 20 30 2c 20 26 72 65 73 29 3b 0a 20 20 20  t, 0, &res);.   
1afec 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
1afed 20 72 63 3b 0a 20 20 20 20 70 2d 3e 6c 61 73 74   rc;.    p->last
1afee 52 6f 77 69 64 20 3d 20 70 2d 3e 6d 6f 76 65 74  Rowid = p->movet
1afef 6f 54 61 72 67 65 74 3b 0a 20 20 20 20 70 2d 3e  oTarget;.    p->
1aff0 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 41  rowidIsValid = A
1aff1 4c 57 41 59 53 28 72 65 73 3d 3d 30 29 20 3f 31  LWAYS(res==0) ?1
1aff2 3a 30 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45  :0;.    if( NEVE
1aff3 52 28 72 65 73 3c 30 29 20 29 7b 0a 20 20 20 20  R(res<0) ){.    
1aff4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1aff5 72 65 65 4e 65 78 74 28 70 2d 3e 70 43 75 72 73  reeNext(p->pCurs
1aff6 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20  or, &res);.     
1aff7 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
1aff8 20 72 63 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65   rc;.    }.#ifde
1aff9 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
1affa 20 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68    sqlite3_search
1affb 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66  _count++;.#endif
1affc 0a 20 20 20 20 70 2d 3e 64 65 66 65 72 72 65 64  .    p->deferred
1affd 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20  Moveto = 0;.    
1affe 70 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  p->cacheStatus =
1afff 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20   CACHE_STALE;.  
1b000 7d 65 6c 73 65 20 69 66 28 20 41 4c 57 41 59 53  }else if( ALWAYS
1b001 28 70 2d 3e 70 43 75 72 73 6f 72 29 20 29 7b 0a  (p->pCursor) ){.
1b002 20 20 20 20 69 6e 74 20 68 61 73 4d 6f 76 65 64      int hasMoved
1b003 3b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73  ;.    int rc = s
1b004 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f  qlite3BtreeCurso
1b005 72 48 61 73 4d 6f 76 65 64 28 70 2d 3e 70 43 75  rHasMoved(p->pCu
1b006 72 73 6f 72 2c 20 26 68 61 73 4d 6f 76 65 64 29  rsor, &hasMoved)
1b007 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72  ;.    if( rc ) r
1b008 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 69 66  eturn rc;.    if
1b009 28 20 68 61 73 4d 6f 76 65 64 20 29 7b 0a 20 20  ( hasMoved ){.  
1b00a 20 20 20 20 70 2d 3e 63 61 63 68 65 53 74 61 74      p->cacheStat
1b00b 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45  us = CACHE_STALE
1b00c 3b 0a 20 20 20 20 20 20 70 2d 3e 6e 75 6c 6c 52  ;.      p->nullR
1b00d 6f 77 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  ow = 1;.    }.  
1b00e 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
1b00f 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK;.}../*.** T
1b010 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e  he following fun
1b011 63 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a 20 73 71  ctions:.**.** sq
1b012 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54  lite3VdbeSerialT
1b013 79 70 65 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33  ype().** sqlite3
1b014 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65  VdbeSerialTypeLe
1b015 6e 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64  n().** sqlite3Vd
1b016 62 65 53 65 72 69 61 6c 4c 65 6e 28 29 0a 2a 2a  beSerialLen().**
1b017 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
1b018 61 6c 50 75 74 28 29 0a 2a 2a 20 73 71 6c 69 74  alPut().** sqlit
1b019 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28  e3VdbeSerialGet(
1b01a 29 0a 2a 2a 0a 2a 2a 20 65 6e 63 61 70 73 75 6c  ).**.** encapsul
1b01b 61 74 65 20 74 68 65 20 63 6f 64 65 20 74 68 61  ate the code tha
1b01c 74 20 73 65 72 69 61 6c 69 7a 65 73 20 76 61 6c  t serializes val
1b01d 75 65 73 20 66 6f 72 20 73 74 6f 72 61 67 65 20  ues for storage 
1b01e 69 6e 20 53 51 4c 69 74 65 0a 2a 2a 20 64 61 74  in SQLite.** dat
1b01f 61 20 61 6e 64 20 69 6e 64 65 78 20 72 65 63 6f  a and index reco
1b020 72 64 73 2e 20 45 61 63 68 20 73 65 72 69 61 6c  rds. Each serial
1b021 69 7a 65 64 20 76 61 6c 75 65 20 63 6f 6e 73 69  ized value consi
1b022 73 74 73 20 6f 66 20 61 0a 2a 2a 20 27 73 65 72  sts of a.** 'ser
1b023 69 61 6c 2d 74 79 70 65 27 20 61 6e 64 20 61 20  ial-type' and a 
1b024 62 6c 6f 62 20 6f 66 20 64 61 74 61 2e 20 54 68  blob of data. Th
1b025 65 20 73 65 72 69 61 6c 20 74 79 70 65 20 69 73  e serial type is
1b026 20 61 6e 20 38 2d 62 79 74 65 20 75 6e 73 69 67   an 8-byte unsig
1b027 6e 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 2c 20  ned.** integer, 
1b028 73 74 6f 72 65 64 20 61 73 20 61 20 76 61 72 69  stored as a vari
1b029 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 6e 20  nt..**.** In an 
1b02a 53 51 4c 69 74 65 20 69 6e 64 65 78 20 72 65 63  SQLite index rec
1b02b 6f 72 64 2c 20 74 68 65 20 73 65 72 69 61 6c 20  ord, the serial 
1b02c 74 79 70 65 20 69 73 20 73 74 6f 72 65 64 20 64  type is stored d
1b02d 69 72 65 63 74 6c 79 20 62 65 66 6f 72 65 0a 2a  irectly before.*
1b02e 2a 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 64 61  * the blob of da
1b02f 74 61 20 74 68 61 74 20 69 74 20 63 6f 72 72 65  ta that it corre
1b030 73 70 6f 6e 64 73 20 74 6f 2e 20 49 6e 20 61 20  sponds to. In a 
1b031 74 61 62 6c 65 20 72 65 63 6f 72 64 2c 20 61 6c  table record, al
1b032 6c 20 73 65 72 69 61 6c 0a 2a 2a 20 74 79 70 65  l serial.** type
1b033 73 20 61 72 65 20 73 74 6f 72 65 64 20 61 74 20  s are stored at 
1b034 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65  the start of the
1b035 20 72 65 63 6f 72 64 2c 20 61 6e 64 20 74 68 65   record, and the
1b036 20 62 6c 6f 62 73 20 6f 66 20 64 61 74 61 20 61   blobs of data a
1b037 74 0a 2a 2a 20 74 68 65 20 65 6e 64 2e 20 48 65  t.** the end. He
1b038 6e 63 65 20 74 68 65 73 65 20 66 75 6e 63 74 69  nce these functi
1b039 6f 6e 73 20 61 6c 6c 6f 77 20 74 68 65 20 63 61  ons allow the ca
1b03a 6c 6c 65 72 20 74 6f 20 68 61 6e 64 6c 65 20 74  ller to handle t
1b03b 68 65 0a 2a 2a 20 73 65 72 69 61 6c 2d 74 79 70  he.** serial-typ
1b03c 65 20 61 6e 64 20 64 61 74 61 20 62 6c 6f 62 20  e and data blob 
1b03d 73 65 70 65 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a  seperately..**.*
1b03e 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1b03f 74 61 62 6c 65 20 64 65 73 63 72 69 62 65 73 20  table describes 
1b040 74 68 65 20 76 61 72 69 6f 75 73 20 73 74 6f 72  the various stor
1b041 61 67 65 20 63 6c 61 73 73 65 73 20 66 6f 72 20  age classes for 
1b042 64 61 74 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 65  data:.**.**   se
1b043 72 69 61 6c 20 74 79 70 65 20 20 20 20 20 20 20  rial type       
1b044 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 20   bytes of data  
1b045 20 20 20 20 74 79 70 65 0a 2a 2a 20 20 20 2d 2d      type.**   --
1b046 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20  ------------    
1b047 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
1b048 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d      ------------
1b049 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 30 20 20 20  ---.**      0   
1b04a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b04b 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 4e    0            N
1b04c 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 31 20 20 20  ULL.**      1   
1b04d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b04e 20 20 31 20 20 20 20 20 20 20 20 20 20 20 20 73    1            s
1b04f 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a  igned integer.**
1b050 20 20 20 20 20 20 32 20 20 20 20 20 20 20 20 20        2         
1b051 20 20 20 20 20 20 20 20 20 20 20 20 32 20 20 20              2   
1b052 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20           signed 
1b053 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20  integer.**      
1b054 33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  3               
1b055 20 20 20 20 20 20 33 20 20 20 20 20 20 20 20 20        3         
1b056 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65     signed intege
1b057 72 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20 20  r.**      4     
1b058 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b059 34 20 20 20 20 20 20 20 20 20 20 20 20 73 69 67  4            sig
1b05a 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20  ned integer.**  
1b05b 20 20 20 20 35 20 20 20 20 20 20 20 20 20 20 20      5           
1b05c 20 20 20 20 20 20 20 20 20 20 36 20 20 20 20 20            6     
1b05d 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e         signed in
1b05e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 36 20  teger.**      6 
1b05f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b060 20 20 20 20 38 20 20 20 20 20 20 20 20 20 20 20      8           
1b061 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a   signed integer.
1b062 2a 2a 20 20 20 20 20 20 37 20 20 20 20 20 20 20  **      7       
1b063 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38 20                8 
1b064 20 20 20 20 20 20 20 20 20 20 20 49 45 45 45 20             IEEE 
1b065 66 6c 6f 61 74 0a 2a 2a 20 20 20 20 20 20 38 20  float.**      8 
1b066 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b067 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20      0           
1b068 20 49 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   Integer constan
1b069 74 20 30 0a 2a 2a 20 20 20 20 20 20 39 20 20 20  t 0.**      9   
1b06a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b06b 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 49    0            I
1b06c 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 20  nteger constant 
1b06d 31 0a 2a 2a 20 20 20 20 20 31 30 2c 31 31 20 20  1.**     10,11  
1b06e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b06f 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 73               res
1b070 65 72 76 65 64 20 66 6f 72 20 65 78 70 61 6e 73  erved for expans
1b071 69 6f 6e 0a 2a 2a 20 20 20 20 4e 3e 3d 31 32 20  ion.**    N>=12 
1b072 61 6e 64 20 65 76 65 6e 20 20 20 20 20 20 20 28  and even       (
1b073 4e 2d 31 32 29 2f 32 20 20 20 20 20 20 20 20 42  N-12)/2        B
1b074 4c 4f 42 0a 2a 2a 20 20 20 20 4e 3e 3d 31 33 20  LOB.**    N>=13 
1b075 61 6e 64 20 6f 64 64 20 20 20 20 20 20 20 20 28  and odd        (
1b076 4e 2d 31 33 29 2f 32 20 20 20 20 20 20 20 20 74  N-13)/2        t
1b077 65 78 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 38 20  ext.**.** The 8 
1b078 61 6e 64 20 39 20 74 79 70 65 73 20 77 65 72 65  and 9 types were
1b079 20 61 64 64 65 64 20 69 6e 20 33 2e 33 2e 30 2c   added in 3.3.0,
1b07a 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 34 2e 20   file format 4. 
1b07b 20 50 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 0a   Prior versions.
1b07c 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20 77 69 6c  ** of SQLite wil
1b07d 6c 20 6e 6f 74 20 75 6e 64 65 72 73 74 61 6e 64  l not understand
1b07e 20 74 68 6f 73 65 20 73 65 72 69 61 6c 20 74 79   those serial ty
1b07f 70 65 73 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52  pes..*/../*.** R
1b080 65 74 75 72 6e 20 74 68 65 20 73 65 72 69 61 6c  eturn the serial
1b081 2d 74 79 70 65 20 66 6f 72 20 74 68 65 20 76 61  -type for the va
1b082 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20 70 4d  lue stored in pM
1b083 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  em..*/.SQLITE_PR
1b084 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65  IVATE u32 sqlite
1b085 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28  3VdbeSerialType(
1b086 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 66  Mem *pMem, int f
1b087 69 6c 65 5f 66 6f 72 6d 61 74 29 7b 0a 20 20 69  ile_format){.  i
1b088 6e 74 20 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d  nt flags = pMem-
1b089 3e 66 6c 61 67 73 3b 0a 20 20 69 6e 74 20 6e 3b  >flags;.  int n;
1b08a 0a 0a 20 20 69 66 28 20 66 6c 61 67 73 26 4d 45  ..  if( flags&ME
1b08b 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65  M_Null ){.    re
1b08c 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66  turn 0;.  }.  if
1b08d 28 20 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20  ( flags&MEM_Int 
1b08e 29 7b 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65  ){.    /* Figure
1b08f 20 6f 75 74 20 77 68 65 74 68 65 72 20 74 6f 20   out whether to 
1b090 75 73 65 20 31 2c 20 32 2c 20 34 2c 20 36 20 6f  use 1, 2, 4, 6 o
1b091 72 20 38 20 62 79 74 65 73 2e 20 2a 2f 0a 23 20  r 8 bytes. */.# 
1b092 20 20 64 65 66 69 6e 65 20 4d 41 58 5f 36 42 59    define MAX_6BY
1b093 54 45 20 28 28 28 28 69 36 34 29 30 78 30 30 30  TE ((((i64)0x000
1b094 30 38 30 30 30 29 3c 3c 33 32 29 2d 31 29 0a 20  08000)<<32)-1). 
1b095 20 20 20 69 36 34 20 69 20 3d 20 70 4d 65 6d 2d     i64 i = pMem-
1b096 3e 75 2e 69 3b 0a 20 20 20 20 75 36 34 20 75 3b  >u.i;.    u64 u;
1b097 0a 20 20 20 20 69 66 28 20 66 69 6c 65 5f 66 6f  .    if( file_fo
1b098 72 6d 61 74 3e 3d 34 20 26 26 20 28 69 26 31 29  rmat>=4 && (i&1)
1b099 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==i ){.      ret
1b09a 75 72 6e 20 38 2b 28 75 33 32 29 69 3b 0a 20 20  urn 8+(u32)i;.  
1b09b 20 20 7d 0a 20 20 20 20 75 20 3d 20 69 3c 30 20    }.    u = i<0 
1b09c 3f 20 2d 69 20 3a 20 69 3b 0a 20 20 20 20 69 66  ? -i : i;.    if
1b09d 28 20 75 3c 3d 31 32 37 20 29 20 72 65 74 75 72  ( u<=127 ) retur
1b09e 6e 20 31 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d  n 1;.    if( u<=
1b09f 33 32 37 36 37 20 29 20 72 65 74 75 72 6e 20 32  32767 ) return 2
1b0a0 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 38 33 38  ;.    if( u<=838
1b0a1 38 36 30 37 20 29 20 72 65 74 75 72 6e 20 33 3b  8607 ) return 3;
1b0a2 0a 20 20 20 20 69 66 28 20 75 3c 3d 32 31 34 37  .    if( u<=2147
1b0a3 34 38 33 36 34 37 20 29 20 72 65 74 75 72 6e 20  483647 ) return 
1b0a4 34 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 4d 41  4;.    if( u<=MA
1b0a5 58 5f 36 42 59 54 45 20 29 20 72 65 74 75 72 6e  X_6BYTE ) return
1b0a6 20 35 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 36   5;.    return 6
1b0a7 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 67  ;.  }.  if( flag
1b0a8 73 26 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20  s&MEM_Real ){.  
1b0a9 20 20 72 65 74 75 72 6e 20 37 3b 0a 20 20 7d 0a    return 7;.  }.
1b0aa 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e    assert( pMem->
1b0ab 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1b0ac 20 7c 7c 20 66 6c 61 67 73 26 28 4d 45 4d 5f 53   || flags&(MEM_S
1b0ad 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 3b 0a  tr|MEM_Blob) );.
1b0ae 20 20 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20    n = pMem->n;. 
1b0af 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d   if( flags & MEM
1b0b0 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 6e 20 2b  _Zero ){.    n +
1b0b1 3d 20 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b  = pMem->u.nZero;
1b0b2 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e  .  }.  assert( n
1b0b3 3e 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  >=0 );.  return 
1b0b4 28 28 6e 2a 32 29 20 2b 20 31 32 20 2b 20 28 28  ((n*2) + 12 + ((
1b0b5 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 21 3d  flags&MEM_Str)!=
1b0b6 30 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  0));.}../*.** Re
1b0b7 74 75 72 6e 20 74 68 65 20 6c 65 6e 67 74 68 20  turn the length 
1b0b8 6f 66 20 74 68 65 20 64 61 74 61 20 63 6f 72 72  of the data corr
1b0b9 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65  esponding to the
1b0ba 20 73 75 70 70 6c 69 65 64 20 73 65 72 69 61 6c   supplied serial
1b0bb 2d 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  -type..*/.SQLITE
1b0bc 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c  _PRIVATE u32 sql
1b0bd 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79  ite3VdbeSerialTy
1b0be 70 65 4c 65 6e 28 75 33 32 20 73 65 72 69 61 6c  peLen(u32 serial
1b0bf 5f 74 79 70 65 29 7b 0a 20 20 69 66 28 20 73 65  _type){.  if( se
1b0c0 72 69 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b  rial_type>=12 ){
1b0c1 0a 20 20 20 20 72 65 74 75 72 6e 20 28 73 65 72  .    return (ser
1b0c2 69 61 6c 5f 74 79 70 65 2d 31 32 29 2f 32 3b 0a  ial_type-12)/2;.
1b0c3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 74 61    }else{.    sta
1b0c4 74 69 63 20 63 6f 6e 73 74 20 75 38 20 61 53 69  tic const u8 aSi
1b0c5 7a 65 5b 5d 20 3d 20 7b 20 30 2c 20 31 2c 20 32  ze[] = { 0, 1, 2
1b0c6 2c 20 33 2c 20 34 2c 20 36 2c 20 38 2c 20 38 2c  , 3, 4, 6, 8, 8,
1b0c7 20 30 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 20   0, 0, 0, 0 };. 
1b0c8 20 20 20 72 65 74 75 72 6e 20 61 53 69 7a 65 5b     return aSize[
1b0c9 73 65 72 69 61 6c 5f 74 79 70 65 5d 3b 0a 20 20  serial_type];.  
1b0ca 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65  }.}../*.** If we
1b0cb 20 61 72 65 20 6f 6e 20 61 6e 20 61 72 63 68 69   are on an archi
1b0cc 74 65 63 74 75 72 65 20 77 69 74 68 20 6d 69 78  tecture with mix
1b0cd 65 64 2d 65 6e 64 69 61 6e 20 66 6c 6f 61 74 69  ed-endian floati
1b0ce 6e 67 20 0a 2a 2a 20 70 6f 69 6e 74 73 20 28 65  ng .** points (e
1b0cf 78 3a 20 41 52 4d 37 29 20 74 68 65 6e 20 73 77  x: ARM7) then sw
1b0d0 61 70 20 74 68 65 20 6c 6f 77 65 72 20 34 20 62  ap the lower 4 b
1b0d1 79 74 65 73 20 77 69 74 68 20 74 68 65 20 0a 2a  ytes with the .*
1b0d2 2a 20 75 70 70 65 72 20 34 20 62 79 74 65 73 2e  * upper 4 bytes.
1b0d3 20 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 73    Return the res
1b0d4 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 6d  ult..**.** For m
1b0d5 6f 73 74 20 61 72 63 68 69 74 65 63 74 75 72 65  ost architecture
1b0d6 73 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d  s, this is a no-
1b0d7 6f 70 2e 0a 2a 2a 0a 2a 2a 20 28 6c 61 74 65 72  op..**.** (later
1b0d8 29 3a 20 20 49 74 20 69 73 20 72 65 70 6f 72 74  ):  It is report
1b0d9 65 64 20 74 6f 20 6d 65 20 74 68 61 74 20 74 68  ed to me that th
1b0da 65 20 6d 69 78 65 64 2d 65 6e 64 69 61 6e 20 70  e mixed-endian p
1b0db 72 6f 62 6c 65 6d 0a 2a 2a 20 6f 6e 20 41 52 4d  roblem.** on ARM
1b0dc 37 20 69 73 20 61 6e 20 69 73 73 75 65 20 77 69  7 is an issue wi
1b0dd 74 68 20 47 43 43 2c 20 6e 6f 74 20 77 69 74 68  th GCC, not with
1b0de 20 74 68 65 20 41 52 4d 37 20 63 68 69 70 2e 20   the ARM7 chip. 
1b0df 20 49 74 20 73 65 65 6d 73 0a 2a 2a 20 74 68 61   It seems.** tha
1b0e0 74 20 65 61 72 6c 79 20 76 65 72 73 69 6f 6e 73  t early versions
1b0e1 20 6f 66 20 47 43 43 20 73 74 6f 72 65 64 20 74   of GCC stored t
1b0e2 68 65 20 74 77 6f 20 77 6f 72 64 73 20 6f 66 20  he two words of 
1b0e3 61 20 36 34 2d 62 69 74 0a 2a 2a 20 66 6c 6f 61  a 64-bit.** floa
1b0e4 74 20 69 6e 20 74 68 65 20 77 72 6f 6e 67 20 6f  t in the wrong o
1b0e5 72 64 65 72 2e 20 20 41 6e 64 20 74 68 61 74 20  rder.  And that 
1b0e6 65 72 72 6f 72 20 68 61 73 20 62 65 65 6e 20 70  error has been p
1b0e7 72 6f 70 61 67 61 74 65 64 0a 2a 2a 20 65 76 65  ropagated.** eve
1b0e8 72 20 73 69 6e 63 65 2e 20 20 54 68 65 20 62 6c  r since.  The bl
1b0e9 61 6d 65 20 69 73 20 6e 6f 74 20 6e 65 63 65 73  ame is not neces
1b0ea 73 61 72 69 6c 79 20 77 69 74 68 20 47 43 43 2c  sarily with GCC,
1b0eb 20 74 68 6f 75 67 68 2e 0a 2a 2a 20 47 43 43 20   though..** GCC 
1b0ec 6d 69 67 68 74 20 68 61 76 65 20 6a 75 73 74 20  might have just 
1b0ed 63 6f 70 79 69 6e 67 20 74 68 65 20 70 72 6f 62  copying the prob
1b0ee 6c 65 6d 20 66 72 6f 6d 20 61 20 70 72 69 6f 72  lem from a prior
1b0ef 20 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2a 20 49 20   compiler..** I 
1b0f0 61 6d 20 61 6c 73 6f 20 74 6f 6c 64 20 74 68 61  am also told tha
1b0f1 74 20 6e 65 77 65 72 20 76 65 72 73 69 6f 6e 73  t newer versions
1b0f2 20 6f 66 20 47 43 43 20 74 68 61 74 20 66 6f 6c   of GCC that fol
1b0f3 6c 6f 77 20 61 20 64 69 66 66 65 72 65 6e 74 0a  low a different.
1b0f4 2a 2a 20 41 42 49 20 67 65 74 20 74 68 65 20 62  ** ABI get the b
1b0f5 79 74 65 20 6f 72 64 65 72 20 72 69 67 68 74 2e  yte order right.
1b0f6 0a 2a 2a 0a 2a 2a 20 44 65 76 65 6c 6f 70 65 72  .**.** Developer
1b0f7 73 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 6f  s using SQLite o
1b0f8 6e 20 61 6e 20 41 52 4d 37 20 73 68 6f 75 6c 64  n an ARM7 should
1b0f9 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e   compile and run
1b0fa 20 74 68 65 69 72 0a 2a 2a 20 61 70 70 6c 69 63   their.** applic
1b0fb 61 74 69 6f 6e 20 75 73 69 6e 67 20 2d 44 53 51  ation using -DSQ
1b0fc 4c 49 54 45 5f 44 45 42 55 47 3d 31 20 61 74 20  LITE_DEBUG=1 at 
1b0fd 6c 65 61 73 74 20 6f 6e 63 65 2e 20 20 57 69 74  least once.  Wit
1b0fe 68 20 44 45 42 55 47 0a 2a 2a 20 65 6e 61 62 6c  h DEBUG.** enabl
1b0ff 65 64 2c 20 73 6f 6d 65 20 61 73 73 65 72 74 73  ed, some asserts
1b100 20 62 65 6c 6f 77 20 77 69 6c 6c 20 65 6e 73 75   below will ensu
1b101 72 65 20 74 68 61 74 20 74 68 65 20 62 79 74 65  re that the byte
1b102 20 6f 72 64 65 72 20 6f 66 0a 2a 2a 20 66 6c 6f   order of.** flo
1b103 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75  ating point valu
1b104 65 73 20 69 73 20 63 6f 72 72 65 63 74 2e 0a 2a  es is correct..*
1b105 2a 0a 2a 2a 20 28 32 30 30 37 2d 30 38 2d 33 30  *.** (2007-08-30
1b106 29 20 20 46 72 61 6e 6b 20 76 61 6e 20 56 75 67  )  Frank van Vug
1b107 74 20 68 61 73 20 73 74 75 64 69 65 64 20 74 68  t has studied th
1b108 69 73 20 70 72 6f 62 6c 65 6d 20 63 6c 6f 73 65  is problem close
1b109 6c 79 0a 2a 2a 20 61 6e 64 20 68 61 73 20 73 65  ly.** and has se
1b10a 6e 64 20 68 69 73 20 66 69 6e 64 69 6e 67 73 20  nd his findings 
1b10b 74 6f 20 74 68 65 20 53 51 4c 69 74 65 20 64 65  to the SQLite de
1b10c 76 65 6c 6f 70 65 72 73 2e 20 20 46 72 61 6e 6b  velopers.  Frank
1b10d 0a 2a 2a 20 77 72 69 74 65 73 20 74 68 61 74 20  .** writes that 
1b10e 73 6f 6d 65 20 4c 69 6e 75 78 20 6b 65 72 6e 65  some Linux kerne
1b10f 6c 73 20 6f 66 66 65 72 20 66 6c 6f 61 74 69 6e  ls offer floatin
1b110 67 20 70 6f 69 6e 74 20 68 61 72 64 77 61 72 65  g point hardware
1b111 0a 2a 2a 20 65 6d 75 6c 61 74 69 6f 6e 20 74 68  .** emulation th
1b112 61 74 20 75 73 65 73 20 6f 6e 6c 79 20 33 32 2d  at uses only 32-
1b113 62 69 74 20 6d 61 6e 74 69 73 73 61 73 20 69 6e  bit mantissas in
1b114 73 74 65 61 64 20 6f 66 20 61 20 66 75 6c 6c 20  stead of a full 
1b115 0a 2a 2a 20 34 38 2d 62 69 74 73 20 61 73 20 72  .** 48-bits as r
1b116 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20 49  equired by the I
1b117 45 45 45 20 73 74 61 6e 64 61 72 64 2e 20 20 28  EEE standard.  (
1b118 54 68 69 73 20 69 73 20 74 68 65 0a 2a 2a 20 43  This is the.** C
1b119 4f 4e 46 49 47 5f 46 50 45 5f 46 41 53 54 46 50  ONFIG_FPE_FASTFP
1b11a 45 20 6f 70 74 69 6f 6e 2e 29 20 20 4f 6e 20 73  E option.)  On s
1b11b 75 63 68 20 73 79 73 74 65 6d 73 2c 20 66 6c 6f  uch systems, flo
1b11c 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 62  ating point.** b
1b11d 79 74 65 20 73 77 61 70 70 69 6e 67 20 62 65 63  yte swapping bec
1b11e 6f 6d 65 73 20 76 65 72 79 20 63 6f 6d 70 6c 69  omes very compli
1b11f 63 61 74 65 64 2e 20 20 54 6f 20 61 76 6f 69 64  cated.  To avoid
1b120 20 70 72 6f 62 6c 65 6d 73 2c 0a 2a 2a 20 74 68   problems,.** th
1b121 65 20 6e 65 63 65 73 73 61 72 79 20 62 79 74 65  e necessary byte
1b122 20 73 77 61 70 70 69 6e 67 20 69 73 20 63 61 72   swapping is car
1b123 72 69 65 64 20 6f 75 74 20 75 73 69 6e 67 20 61  ried out using a
1b124 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 0a   64-bit integer.
1b125 2a 2a 20 72 61 74 68 65 72 20 74 68 61 6e 20 61  ** rather than a
1b126 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 2e 20 20   64-bit float.  
1b127 46 72 61 6e 6b 20 61 73 73 75 72 65 73 20 75 73  Frank assures us
1b128 20 74 68 61 74 20 74 68 65 20 63 6f 64 65 20 68   that the code h
1b129 65 72 65 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72  ere.** works for
1b12a 20 68 69 6d 2e 20 20 57 65 2c 20 74 68 65 20 64   him.  We, the d
1b12b 65 76 65 6c 6f 70 65 72 73 2c 20 68 61 76 65 20  evelopers, have 
1b12c 6e 6f 20 77 61 79 20 74 6f 20 69 6e 64 65 70 65  no way to indepe
1b12d 6e 64 65 6e 74 6c 79 0a 2a 2a 20 76 65 72 69 66  ndently.** verif
1b12e 79 20 74 68 69 73 2c 20 62 75 74 20 46 72 61 6e  y this, but Fran
1b12f 6b 20 73 65 65 6d 73 20 74 6f 20 6b 6e 6f 77 20  k seems to know 
1b130 77 68 61 74 20 68 65 20 69 73 20 74 61 6c 6b 69  what he is talki
1b131 6e 67 20 61 62 6f 75 74 0a 2a 2a 20 73 6f 20 77  ng about.** so w
1b132 65 20 74 72 75 73 74 20 68 69 6d 2e 0a 2a 2f 0a  e trust him..*/.
1b133 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 49  #ifdef SQLITE_MI
1b134 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54  XED_ENDIAN_64BIT
1b135 5f 46 4c 4f 41 54 0a 73 74 61 74 69 63 20 75 36  _FLOAT.static u6
1b136 34 20 66 6c 6f 61 74 53 77 61 70 28 75 36 34 20  4 floatSwap(u64 
1b137 69 6e 29 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20  in){.  union {. 
1b138 20 20 20 75 36 34 20 72 3b 0a 20 20 20 20 75 33     u64 r;.    u3
1b139 32 20 69 5b 32 5d 3b 0a 20 20 7d 20 75 3b 0a 20  2 i[2];.  } u;. 
1b13a 20 75 33 32 20 74 3b 0a 0a 20 20 75 2e 72 20 3d   u32 t;..  u.r =
1b13b 20 69 6e 3b 0a 20 20 74 20 3d 20 75 2e 69 5b 30   in;.  t = u.i[0
1b13c 5d 3b 0a 20 20 75 2e 69 5b 30 5d 20 3d 20 75 2e  ];.  u.i[0] = u.
1b13d 69 5b 31 5d 3b 0a 20 20 75 2e 69 5b 31 5d 20 3d  i[1];.  u.i[1] =
1b13e 20 74 3b 0a 20 20 72 65 74 75 72 6e 20 75 2e 72   t;.  return u.r
1b13f 3b 0a 7d 0a 23 20 64 65 66 69 6e 65 20 73 77 61  ;.}.# define swa
1b140 70 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61  pMixedEndianFloa
1b141 74 28 58 29 20 20 58 20 3d 20 66 6c 6f 61 74 53  t(X)  X = floatS
1b142 77 61 70 28 58 29 0a 23 65 6c 73 65 0a 23 20 64  wap(X).#else.# d
1b143 65 66 69 6e 65 20 73 77 61 70 4d 69 78 65 64 45  efine swapMixedE
1b144 6e 64 69 61 6e 46 6c 6f 61 74 28 58 29 0a 23 65  ndianFloat(X).#e
1b145 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74  ndif../*.** Writ
1b146 65 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64  e the serialized
1b147 20 64 61 74 61 20 62 6c 6f 62 20 66 6f 72 20 74   data blob for t
1b148 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20  he value stored 
1b149 69 6e 20 70 4d 65 6d 20 69 6e 74 6f 20 0a 2a 2a  in pMem into .**
1b14a 20 62 75 66 2e 20 49 74 20 69 73 20 61 73 73 75   buf. It is assu
1b14b 6d 65 64 20 74 68 61 74 20 74 68 65 20 63 61 6c  med that the cal
1b14c 6c 65 72 20 68 61 73 20 61 6c 6c 6f 63 61 74 65  ler has allocate
1b14d 64 20 73 75 66 66 69 63 69 65 6e 74 20 73 70 61  d sufficient spa
1b14e 63 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ce..** Return th
1b14f 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
1b150 73 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a  s written..**.**
1b151 20 6e 42 75 66 20 69 73 20 74 68 65 20 61 6d 6f   nBuf is the amo
1b152 75 6e 74 20 6f 66 20 73 70 61 63 65 20 6c 65 66  unt of space lef
1b153 74 20 69 6e 20 62 75 66 5b 5d 2e 20 20 6e 42 75  t in buf[].  nBu
1b154 66 20 6d 75 73 74 20 61 6c 77 61 79 73 20 62 65  f must always be
1b155 0a 2a 2a 20 6c 61 72 67 65 20 65 6e 6f 75 67 68  .** large enough
1b156 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 65 6e 74   to hold the ent
1b157 69 72 65 20 66 69 65 6c 64 2e 20 20 45 78 63 65  ire field.  Exce
1b158 70 74 2c 20 69 66 20 74 68 65 20 66 69 65 6c 64  pt, if the field
1b159 20 69 73 0a 2a 2a 20 61 20 62 6c 6f 62 20 77 69   is.** a blob wi
1b15a 74 68 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64  th a zero-filled
1b15b 20 74 61 69 6c 2c 20 74 68 65 6e 20 62 75 66 5b   tail, then buf[
1b15c 5d 20 6d 69 67 68 74 20 62 65 20 6a 75 73 74 20  ] might be just 
1b15d 74 68 65 20 72 69 67 68 74 0a 2a 2a 20 73 69 7a  the right.** siz
1b15e 65 20 74 6f 20 68 6f 6c 64 20 65 76 65 72 79 74  e to hold everyt
1b15f 68 69 6e 67 20 65 78 63 65 70 74 20 66 6f 72 20  hing except for 
1b160 74 68 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20  the zero-filled 
1b161 74 61 69 6c 2e 20 20 49 66 20 62 75 66 5b 5d 0a  tail.  If buf[].
1b162 2a 2a 20 69 73 20 6f 6e 6c 79 20 62 69 67 20 65  ** is only big e
1b163 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68  nough to hold th
1b164 65 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 65 66 69  e non-zero prefi
1b165 78 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 77 72 69  x, then only wri
1b166 74 65 20 74 68 61 74 0a 2a 2a 20 70 72 65 66 69  te that.** prefi
1b167 78 20 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 42  x into buf[].  B
1b168 75 74 20 69 66 20 62 75 66 5b 5d 20 69 73 20 6c  ut if buf[] is l
1b169 61 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 68  arge enough to h
1b16a 6f 6c 64 20 62 6f 74 68 20 74 68 65 0a 2a 2a 20  old both the.** 
1b16b 70 72 65 66 69 78 20 61 6e 64 20 74 68 65 20 74  prefix and the t
1b16c 61 69 6c 20 74 68 65 6e 20 77 72 69 74 65 20 74  ail then write t
1b16d 68 65 20 70 72 65 66 69 78 20 61 6e 64 20 73 65  he prefix and se
1b16e 74 20 74 68 65 20 74 61 69 6c 20 74 6f 20 61 6c  t the tail to al
1b16f 6c 0a 2a 2a 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a  l.** zeros..**.*
1b170 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
1b171 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74  ber of bytes act
1b172 75 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 6e  ually written in
1b173 74 6f 20 62 75 66 5b 5d 2e 20 20 54 68 65 20 6e  to buf[].  The n
1b174 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65  umber.** of byte
1b175 73 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d 66 69  s in the zero-fi
1b176 6c 6c 65 64 20 74 61 69 6c 20 69 73 20 69 6e 63  lled tail is inc
1b177 6c 75 64 65 64 20 69 6e 20 74 68 65 20 72 65 74  luded in the ret
1b178 75 72 6e 20 76 61 6c 75 65 20 6f 6e 6c 79 0a 2a  urn value only.*
1b179 2a 20 69 66 20 74 68 6f 73 65 20 62 79 74 65 73  * if those bytes
1b17a 20 77 65 72 65 20 7a 65 72 6f 65 64 20 69 6e 20   were zeroed in 
1b17b 62 75 66 5b 5d 2e 0a 2a 2f 20 0a 53 51 4c 49 54  buf[]..*/ .SQLIT
1b17c 45 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71  E_PRIVATE u32 sq
1b17d 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 50  lite3VdbeSerialP
1b17e 75 74 28 75 38 20 2a 62 75 66 2c 20 69 6e 74 20  ut(u8 *buf, int 
1b17f 6e 42 75 66 2c 20 4d 65 6d 20 2a 70 4d 65 6d 2c  nBuf, Mem *pMem,
1b180 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74   int file_format
1b181 29 7b 0a 20 20 75 33 32 20 73 65 72 69 61 6c 5f  ){.  u32 serial_
1b182 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64  type = sqlite3Vd
1b183 62 65 53 65 72 69 61 6c 54 79 70 65 28 70 4d 65  beSerialType(pMe
1b184 6d 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b  m, file_format);
1b185 0a 20 20 75 33 32 20 6c 65 6e 3b 0a 0a 20 20 2f  .  u32 len;..  /
1b186 2a 20 49 6e 74 65 67 65 72 20 61 6e 64 20 52 65  * Integer and Re
1b187 61 6c 20 2a 2f 0a 20 20 69 66 28 20 73 65 72 69  al */.  if( seri
1b188 61 6c 5f 74 79 70 65 3c 3d 37 20 26 26 20 73 65  al_type<=7 && se
1b189 72 69 61 6c 5f 74 79 70 65 3e 30 20 29 7b 0a 20  rial_type>0 ){. 
1b18a 20 20 20 75 36 34 20 76 3b 0a 20 20 20 20 75 33     u64 v;.    u3
1b18b 32 20 69 3b 0a 20 20 20 20 69 66 28 20 73 65 72  2 i;.    if( ser
1b18c 69 61 6c 5f 74 79 70 65 3d 3d 37 20 29 7b 0a 20  ial_type==7 ){. 
1b18d 20 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a       assert( siz
1b18e 65 6f 66 28 76 29 3d 3d 73 69 7a 65 6f 66 28 70  eof(v)==sizeof(p
1b18f 4d 65 6d 2d 3e 72 29 20 29 3b 0a 20 20 20 20 20  Mem->r) );.     
1b190 20 6d 65 6d 63 70 79 28 26 76 2c 20 26 70 4d 65   memcpy(&v, &pMe
1b191 6d 2d 3e 72 2c 20 73 69 7a 65 6f 66 28 76 29 29  m->r, sizeof(v))
1b192 3b 0a 20 20 20 20 20 20 73 77 61 70 4d 69 78 65  ;.      swapMixe
1b193 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 76 29 3b  dEndianFloat(v);
1b194 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1b195 20 20 76 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b    v = pMem->u.i;
1b196 0a 20 20 20 20 7d 0a 20 20 20 20 6c 65 6e 20 3d  .    }.    len =
1b197 20 69 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65   i = sqlite3Vdbe
1b198 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65  SerialTypeLen(se
1b199 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20  rial_type);.    
1b19a 61 73 73 65 72 74 28 20 6c 65 6e 3c 3d 28 75 33  assert( len<=(u3
1b19b 32 29 6e 42 75 66 20 29 3b 0a 20 20 20 20 77 68  2)nBuf );.    wh
1b19c 69 6c 65 28 20 69 2d 2d 20 29 7b 0a 20 20 20 20  ile( i-- ){.    
1b19d 20 20 62 75 66 5b 69 5d 20 3d 20 28 75 38 29 28    buf[i] = (u8)(
1b19e 76 26 30 78 46 46 29 3b 0a 20 20 20 20 20 20 76  v&0xFF);.      v
1b19f 20 3e 3e 3d 20 38 3b 0a 20 20 20 20 7d 0a 20 20   >>= 8;.    }.  
1b1a0 20 20 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20    return len;.  
1b1a1 7d 0a 0a 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f  }..  /* String o
1b1a2 72 20 62 6c 6f 62 20 2a 2f 0a 20 20 69 66 28 20  r blob */.  if( 
1b1a3 73 65 72 69 61 6c 5f 74 79 70 65 3e 3d 31 32 20  serial_type>=12 
1b1a4 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
1b1a5 4d 65 6d 2d 3e 6e 20 2b 20 28 28 70 4d 65 6d 2d  Mem->n + ((pMem-
1b1a6 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72  >flags & MEM_Zer
1b1a7 6f 29 3f 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f  o)?pMem->u.nZero
1b1a8 3a 30 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  :0).            
1b1a9 20 3d 3d 20 28 69 6e 74 29 73 71 6c 69 74 65 33   == (int)sqlite3
1b1aa 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65  VdbeSerialTypeLe
1b1ab 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 29 20 29  n(serial_type) )
1b1ac 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4d  ;.    assert( pM
1b1ad 65 6d 2d 3e 6e 3c 3d 6e 42 75 66 20 29 3b 0a 20  em->n<=nBuf );. 
1b1ae 20 20 20 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e     len = pMem->n
1b1af 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 62 75 66  ;.    memcpy(buf
1b1b0 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 6c 65 6e 29 3b  , pMem->z, len);
1b1b1 0a 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66  .    if( pMem->f
1b1b2 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20  lags & MEM_Zero 
1b1b3 29 7b 0a 20 20 20 20 20 20 6c 65 6e 20 2b 3d 20  ){.      len += 
1b1b4 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 3b 0a 20  pMem->u.nZero;. 
1b1b5 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 42 75       assert( nBu
1b1b6 66 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66  f>=0 );.      if
1b1b7 28 20 6c 65 6e 20 3e 20 28 75 33 32 29 6e 42 75  ( len > (u32)nBu
1b1b8 66 20 29 7b 0a 20 20 20 20 20 20 20 20 6c 65 6e  f ){.        len
1b1b9 20 3d 20 28 75 33 32 29 6e 42 75 66 3b 0a 20 20   = (u32)nBuf;.  
1b1ba 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d 73      }.      mems
1b1bb 65 74 28 26 62 75 66 5b 70 4d 65 6d 2d 3e 6e 5d  et(&buf[pMem->n]
1b1bc 2c 20 30 2c 20 6c 65 6e 2d 70 4d 65 6d 2d 3e 6e  , 0, len-pMem->n
1b1bd 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  );.    }.    ret
1b1be 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d 0a 0a 20 20  urn len;.  }..  
1b1bf 2f 2a 20 4e 55 4c 4c 20 6f 72 20 63 6f 6e 73 74  /* NULL or const
1b1c0 61 6e 74 73 20 30 20 6f 72 20 31 20 2a 2f 0a 20  ants 0 or 1 */. 
1b1c1 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
1b1c2 0a 2a 2a 20 44 65 73 65 72 69 61 6c 69 7a 65 20  .** Deserialize 
1b1c3 74 68 65 20 64 61 74 61 20 62 6c 6f 62 20 70 6f  the data blob po
1b1c4 69 6e 74 65 64 20 74 6f 20 62 79 20 62 75 66 20  inted to by buf 
1b1c5 61 73 20 73 65 72 69 61 6c 20 74 79 70 65 20 73  as serial type s
1b1c6 65 72 69 61 6c 5f 74 79 70 65 0a 2a 2a 20 61 6e  erial_type.** an
1b1c7 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75  d store the resu
1b1c8 6c 74 20 69 6e 20 70 4d 65 6d 2e 20 20 52 65 74  lt in pMem.  Ret
1b1c9 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
1b1ca 66 20 62 79 74 65 73 20 72 65 61 64 2e 0a 2a 2f  f bytes read..*/
1b1cb 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45   .SQLITE_PRIVATE
1b1cc 20 75 33 32 20 73 71 6c 69 74 65 33 56 64 62 65   u32 sqlite3Vdbe
1b1cd 53 65 72 69 61 6c 47 65 74 28 0a 20 20 63 6f 6e  SerialGet(.  con
1b1ce 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
1b1cf 20 2a 62 75 66 2c 20 20 20 20 20 2f 2a 20 42 75   *buf,     /* Bu
1b1d0 66 66 65 72 20 74 6f 20 64 65 73 65 72 69 61 6c  ffer to deserial
1b1d1 69 7a 65 20 66 72 6f 6d 20 2a 2f 0a 20 20 75 33  ize from */.  u3
1b1d2 32 20 73 65 72 69 61 6c 5f 74 79 70 65 2c 20 20  2 serial_type,  
1b1d3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
1b1d4 65 72 69 61 6c 20 74 79 70 65 20 74 6f 20 64 65  erial type to de
1b1d5 73 65 72 69 61 6c 69 7a 65 20 2a 2f 0a 20 20 4d  serialize */.  M
1b1d6 65 6d 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20  em *pMem        
1b1d7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1b1d8 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 77  Memory cell to w
1b1d9 72 69 74 65 20 76 61 6c 75 65 20 69 6e 74 6f 20  rite value into 
1b1da 2a 2f 0a 29 7b 0a 20 20 73 77 69 74 63 68 28 20  */.){.  switch( 
1b1db 73 65 72 69 61 6c 5f 74 79 70 65 20 29 7b 0a 20  serial_type ){. 
1b1dc 20 20 20 63 61 73 65 20 31 30 3a 20 20 20 2f 2a     case 10:   /*
1b1dd 20 52 65 73 65 72 76 65 64 20 66 6f 72 20 66 75   Reserved for fu
1b1de 74 75 72 65 20 75 73 65 20 2a 2f 0a 20 20 20 20  ture use */.    
1b1df 63 61 73 65 20 31 31 3a 20 20 20 2f 2a 20 52 65  case 11:   /* Re
1b1e0 73 65 72 76 65 64 20 66 6f 72 20 66 75 74 75 72  served for futur
1b1e1 65 20 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73  e use */.    cas
1b1e2 65 20 30 3a 20 7b 20 20 2f 2a 20 4e 55 4c 4c 20  e 0: {  /* NULL 
1b1e3 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  */.      pMem->f
1b1e4 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b  lags = MEM_Null;
1b1e5 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1b1e6 20 20 7d 0a 20 20 20 20 63 61 73 65 20 31 3a 20    }.    case 1: 
1b1e7 7b 20 2f 2a 20 31 2d 62 79 74 65 20 73 69 67 6e  { /* 1-byte sign
1b1e8 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20  ed integer */.  
1b1e9 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20      pMem->u.i = 
1b1ea 28 73 69 67 6e 65 64 20 63 68 61 72 29 62 75 66  (signed char)buf
1b1eb 5b 30 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  [0];.      pMem-
1b1ec 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74  >flags = MEM_Int
1b1ed 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ;.      return 1
1b1ee 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
1b1ef 20 32 3a 20 7b 20 2f 2a 20 32 2d 62 79 74 65 20   2: { /* 2-byte 
1b1f0 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a  signed integer *
1b1f1 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e  /.      pMem->u.
1b1f2 69 20 3d 20 28 28 28 73 69 67 6e 65 64 20 63 68  i = (((signed ch
1b1f3 61 72 29 62 75 66 5b 30 5d 29 3c 3c 38 29 20 7c  ar)buf[0])<<8) |
1b1f4 20 62 75 66 5b 31 5d 3b 0a 20 20 20 20 20 20 70   buf[1];.      p
1b1f5 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
1b1f6 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75  _Int;.      retu
1b1f7 72 6e 20 32 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 2;.    }.    
1b1f8 63 61 73 65 20 33 3a 20 7b 20 2f 2a 20 33 2d 62  case 3: { /* 3-b
1b1f9 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67  yte signed integ
1b1fa 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d  er */.      pMem
1b1fb 2d 3e 75 2e 69 20 3d 20 28 28 28 73 69 67 6e 65  ->u.i = (((signe
1b1fc 64 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c  d char)buf[0])<<
1b1fd 31 36 29 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 38  16) | (buf[1]<<8
1b1fe 29 20 7c 20 62 75 66 5b 32 5d 3b 0a 20 20 20 20  ) | buf[2];.    
1b1ff 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20    pMem->flags = 
1b200 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72  MEM_Int;.      r
1b201 65 74 75 72 6e 20 33 3b 0a 20 20 20 20 7d 0a 20  eturn 3;.    }. 
1b202 20 20 20 63 61 73 65 20 34 3a 20 7b 20 2f 2a 20     case 4: { /* 
1b203 34 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69 6e  4-byte signed in
1b204 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 70  teger */.      p
1b205 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 62 75 66 5b  Mem->u.i = (buf[
1b206 30 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66 5b 31  0]<<24) | (buf[1
1b207 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 32 5d  ]<<16) | (buf[2]
1b208 3c 3c 38 29 20 7c 20 62 75 66 5b 33 5d 3b 0a 20  <<8) | buf[3];. 
1b209 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
1b20a 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20   = MEM_Int;.    
1b20b 20 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 20 20    return 4;.    
1b20c 7d 0a 20 20 20 20 63 61 73 65 20 35 3a 20 7b 20  }.    case 5: { 
1b20d 2f 2a 20 36 2d 62 79 74 65 20 73 69 67 6e 65 64  /* 6-byte signed
1b20e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20   integer */.    
1b20f 20 20 75 36 34 20 78 20 3d 20 28 28 28 73 69 67    u64 x = (((sig
1b210 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30 5d 29  ned char)buf[0])
1b211 3c 3c 38 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20  <<8) | buf[1];. 
1b212 20 20 20 20 20 75 33 32 20 79 20 3d 20 28 62 75       u32 y = (bu
1b213 66 5b 32 5d 3c 3c 32 34 29 20 7c 20 28 62 75 66  f[2]<<24) | (buf
1b214 5b 33 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b  [3]<<16) | (buf[
1b215 34 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 35 5d 3b  4]<<8) | buf[5];
1b216 0a 20 20 20 20 20 20 78 20 3d 20 28 78 3c 3c 33  .      x = (x<<3
1b217 32 29 20 7c 20 79 3b 0a 20 20 20 20 20 20 70 4d  2) | y;.      pM
1b218 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a  em->u.i = *(i64*
1b219 29 26 78 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  )&x;.      pMem-
1b21a 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74  >flags = MEM_Int
1b21b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 36  ;.      return 6
1b21c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
1b21d 20 36 3a 20 20 20 2f 2a 20 38 2d 62 79 74 65 20   6:   /* 8-byte 
1b21e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a  signed integer *
1b21f 2f 0a 20 20 20 20 63 61 73 65 20 37 3a 20 7b 20  /.    case 7: { 
1b220 2f 2a 20 49 45 45 45 20 66 6c 6f 61 74 69 6e 67  /* IEEE floating
1b221 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20   point */.      
1b222 75 36 34 20 78 3b 0a 20 20 20 20 20 20 75 33 32  u64 x;.      u32
1b223 20 79 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64   y;.#if !defined
1b224 28 4e 44 45 42 55 47 29 20 26 26 20 21 64 65 66  (NDEBUG) && !def
1b225 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1b226 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 29  _FLOATING_POINT)
1b227 0a 20 20 20 20 20 20 2f 2a 20 56 65 72 69 66 79  .      /* Verify
1b228 20 74 68 61 74 20 69 6e 74 65 67 65 72 73 20 61   that integers a
1b229 6e 64 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  nd floating poin
1b22a 74 20 76 61 6c 75 65 73 20 75 73 65 20 74 68 65  t values use the
1b22b 20 73 61 6d 65 0a 20 20 20 20 20 20 2a 2a 20 62   same.      ** b
1b22c 79 74 65 20 6f 72 64 65 72 2e 20 20 4f 72 2c 20  yte order.  Or, 
1b22d 74 68 61 74 20 69 66 20 53 51 4c 49 54 45 5f 4d  that if SQLITE_M
1b22e 49 58 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49  IXED_ENDIAN_64BI
1b22f 54 5f 46 4c 4f 41 54 20 69 73 0a 20 20 20 20 20  T_FLOAT is.     
1b230 20 2a 2a 20 64 65 66 69 6e 65 64 20 74 68 61 74   ** defined that
1b231 20 36 34 2d 62 69 74 20 66 6c 6f 61 74 69 6e 67   64-bit floating
1b232 20 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 72 65   point values re
1b233 61 6c 6c 79 20 61 72 65 20 6d 69 78 65 64 0a 20  ally are mixed. 
1b234 20 20 20 20 20 2a 2a 20 65 6e 64 69 61 6e 2e 0a       ** endian..
1b235 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
1b236 74 61 74 69 63 20 63 6f 6e 73 74 20 75 36 34 20  tatic const u64 
1b237 74 31 20 3d 20 28 28 75 36 34 29 30 78 33 66 66  t1 = ((u64)0x3ff
1b238 30 30 30 30 30 29 3c 3c 33 32 3b 0a 20 20 20 20  00000)<<32;.    
1b239 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 64    static const d
1b23a 6f 75 62 6c 65 20 72 31 20 3d 20 31 2e 30 3b 0a  ouble r1 = 1.0;.
1b23b 20 20 20 20 20 20 75 36 34 20 74 32 20 3d 20 74        u64 t2 = t
1b23c 31 3b 0a 20 20 20 20 20 20 73 77 61 70 4d 69 78  1;.      swapMix
1b23d 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74 28 74 32  edEndianFloat(t2
1b23e 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
1b23f 20 73 69 7a 65 6f 66 28 72 31 29 3d 3d 73 69 7a   sizeof(r1)==siz
1b240 65 6f 66 28 74 32 29 20 26 26 20 6d 65 6d 63 6d  eof(t2) && memcm
1b241 70 28 26 72 31 2c 20 26 74 32 2c 20 73 69 7a 65  p(&r1, &t2, size
1b242 6f 66 28 72 31 29 29 3d 3d 30 20 29 3b 0a 23 65  of(r1))==0 );.#e
1b243 6e 64 69 66 0a 0a 20 20 20 20 20 20 78 20 3d 20  ndif..      x = 
1b244 28 62 75 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28  (buf[0]<<24) | (
1b245 62 75 66 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62  buf[1]<<16) | (b
1b246 75 66 5b 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b  uf[2]<<8) | buf[
1b247 33 5d 3b 0a 20 20 20 20 20 20 79 20 3d 20 28 62  3];.      y = (b
1b248 75 66 5b 34 5d 3c 3c 32 34 29 20 7c 20 28 62 75  uf[4]<<24) | (bu
1b249 66 5b 35 5d 3c 3c 31 36 29 20 7c 20 28 62 75 66  f[5]<<16) | (buf
1b24a 5b 36 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 37 5d  [6]<<8) | buf[7]
1b24b 3b 0a 20 20 20 20 20 20 78 20 3d 20 28 78 3c 3c  ;.      x = (x<<
1b24c 33 32 29 20 7c 20 79 3b 0a 20 20 20 20 20 20 69  32) | y;.      i
1b24d 66 28 20 73 65 72 69 61 6c 5f 74 79 70 65 3d 3d  f( serial_type==
1b24e 36 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4d 65  6 ){.        pMe
1b24f 6d 2d 3e 75 2e 69 20 3d 20 2a 28 69 36 34 2a 29  m->u.i = *(i64*)
1b250 26 78 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d  &x;.        pMem
1b251 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
1b252 74 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  t;.      }else{.
1b253 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1b254 73 69 7a 65 6f 66 28 78 29 3d 3d 38 20 26 26 20  sizeof(x)==8 && 
1b255 73 69 7a 65 6f 66 28 70 4d 65 6d 2d 3e 72 29 3d  sizeof(pMem->r)=
1b256 3d 38 20 29 3b 0a 20 20 20 20 20 20 20 20 73 77  =8 );.        sw
1b257 61 70 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f  apMixedEndianFlo
1b258 61 74 28 78 29 3b 0a 20 20 20 20 20 20 20 20 6d  at(x);.        m
1b259 65 6d 63 70 79 28 26 70 4d 65 6d 2d 3e 72 2c 20  emcpy(&pMem->r, 
1b25a 26 78 2c 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a  &x, sizeof(x));.
1b25b 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c          pMem->fl
1b25c 61 67 73 20 3d 20 73 71 6c 69 74 65 33 49 73 4e  ags = sqlite3IsN
1b25d 61 4e 28 70 4d 65 6d 2d 3e 72 29 20 3f 20 4d 45  aN(pMem->r) ? ME
1b25e 4d 5f 4e 75 6c 6c 20 3a 20 4d 45 4d 5f 52 65 61  M_Null : MEM_Rea
1b25f 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  l;.      }.     
1b260 20 72 65 74 75 72 6e 20 38 3b 0a 20 20 20 20 7d   return 8;.    }
1b261 0a 20 20 20 20 63 61 73 65 20 38 3a 20 20 20 20  .    case 8:    
1b262 2f 2a 20 49 6e 74 65 67 65 72 20 30 20 2a 2f 0a  /* Integer 0 */.
1b263 20 20 20 20 63 61 73 65 20 39 3a 20 7b 20 20 2f      case 9: {  /
1b264 2a 20 49 6e 74 65 67 65 72 20 31 20 2a 2f 0a 20  * Integer 1 */. 
1b265 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d       pMem->u.i =
1b266 20 73 65 72 69 61 6c 5f 74 79 70 65 2d 38 3b 0a   serial_type-8;.
1b267 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
1b268 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20  s = MEM_Int;.   
1b269 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
1b26a 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20   }.    default: 
1b26b 7b 0a 20 20 20 20 20 20 75 33 32 20 6c 65 6e 20  {.      u32 len 
1b26c 3d 20 28 73 65 72 69 61 6c 5f 74 79 70 65 2d 31  = (serial_type-1
1b26d 32 29 2f 32 3b 0a 20 20 20 20 20 20 70 4d 65 6d  2)/2;.      pMem
1b26e 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a 29 62 75  ->z = (char *)bu
1b26f 66 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e  f;.      pMem->n
1b270 20 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 70 4d   = len;.      pM
1b271 65 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20  em->xDel = 0;.  
1b272 20 20 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74      if( serial_t
1b273 79 70 65 26 30 78 30 31 20 29 7b 0a 20 20 20 20  ype&0x01 ){.    
1b274 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
1b275 3d 20 4d 45 4d 5f 53 74 72 20 7c 20 4d 45 4d 5f  = MEM_Str | MEM_
1b276 45 70 68 65 6d 3b 0a 20 20 20 20 20 20 7d 65 6c  Ephem;.      }el
1b277 73 65 7b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d  se{.        pMem
1b278 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c  ->flags = MEM_Bl
1b279 6f 62 20 7c 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a  ob | MEM_Ephem;.
1b27a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
1b27b 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 20 20 7d 0a  turn len;.    }.
1b27c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
1b27d 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20  }.../*.** Given 
1b27e 74 68 65 20 6e 4b 65 79 2d 62 79 74 65 20 65 6e  the nKey-byte en
1b27f 63 6f 64 69 6e 67 20 6f 66 20 61 20 72 65 63 6f  coding of a reco
1b280 72 64 20 69 6e 20 70 4b 65 79 5b 5d 2c 20 70 61  rd in pKey[], pa
1b281 72 73 65 20 74 68 65 0a 2a 2a 20 72 65 63 6f 72  rse the.** recor
1b282 64 20 69 6e 74 6f 20 61 20 55 6e 70 61 63 6b 65  d into a Unpacke
1b283 64 52 65 63 6f 72 64 20 73 74 72 75 63 74 75 72  dRecord structur
1b284 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69  e.  Return a poi
1b285 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 68 61 74 20  nter to.** that 
1b286 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a  structure..**.**
1b287 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e   The calling fun
1b288 63 74 69 6f 6e 20 6d 69 67 68 74 20 70 72 6f 76  ction might prov
1b289 69 64 65 20 73 7a 53 70 61 63 65 20 62 79 74 65  ide szSpace byte
1b28a 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73  s of memory.** s
1b28b 70 61 63 65 20 61 74 20 70 53 70 61 63 65 2e 20  pace at pSpace. 
1b28c 20 54 68 69 73 20 73 70 61 63 65 20 63 61 6e 20   This space can 
1b28d 62 65 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20  be used to hold 
1b28e 74 68 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20  the returned.** 
1b28f 56 44 62 65 50 61 72 73 65 64 52 65 63 6f 72 64  VDbeParsedRecord
1b290 20 73 74 72 75 63 74 75 72 65 20 69 66 20 69 74   structure if it
1b291 20 69 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68   is large enough
1b292 2e 20 20 49 66 20 69 74 20 69 73 0a 2a 2a 20 6e  .  If it is.** n
1b293 6f 74 20 62 69 67 20 65 6e 6f 75 67 68 2c 20 73  ot big enough, s
1b294 70 61 63 65 20 69 73 20 6f 62 74 61 69 6e 65 64  pace is obtained
1b295 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61   from sqlite3_ma
1b296 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  lloc()..**.** Th
1b297 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 75 63  e returned struc
1b298 74 75 72 65 20 73 68 6f 75 6c 64 20 62 65 20 63  ture should be c
1b299 6c 6f 73 65 64 20 62 79 20 61 20 63 61 6c 6c 20  losed by a call 
1b29a 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62  to.** sqlite3Vdb
1b29b 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52  eDeleteUnpackedR
1b29c 65 63 6f 72 64 28 29 2e 0a 2a 2f 20 0a 53 51 4c  ecord()..*/ .SQL
1b29d 49 54 45 5f 50 52 49 56 41 54 45 20 55 6e 70 61  ITE_PRIVATE Unpa
1b29e 63 6b 65 64 52 65 63 6f 72 64 20 2a 73 71 6c 69  ckedRecord *sqli
1b29f 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e 70  te3VdbeRecordUnp
1b2a0 61 63 6b 28 0a 20 20 4b 65 79 49 6e 66 6f 20 2a  ack(.  KeyInfo *
1b2a1 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 2f 2a  pKeyInfo,     /*
1b2a2 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   Information abo
1b2a3 75 74 20 74 68 65 20 72 65 63 6f 72 64 20 66 6f  ut the record fo
1b2a4 72 6d 61 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b  rmat */.  int nK
1b2a5 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ey,             
1b2a6 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20   /* Size of the 
1b2a7 62 69 6e 61 72 79 20 72 65 63 6f 72 64 20 2a 2f  binary record */
1b2a8 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  .  const void *p
1b2a9 4b 65 79 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  Key,      /* The
1b2aa 20 62 69 6e 61 72 79 20 72 65 63 6f 72 64 20 2a   binary record *
1b2ab 2f 0a 20 20 63 68 61 72 20 2a 70 53 70 61 63 65  /.  char *pSpace
1b2ac 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 6e  ,          /* Un
1b2ad 61 6c 69 67 6e 65 64 20 73 70 61 63 65 20 61 76  aligned space av
1b2ae 61 69 6c 61 62 6c 65 20 74 6f 20 68 6f 6c 64 20  ailable to hold 
1b2af 74 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  the object */.  
1b2b0 69 6e 74 20 73 7a 53 70 61 63 65 20 20 20 20 20  int szSpace     
1b2b1 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
1b2b2 66 20 70 53 70 61 63 65 5b 5d 20 69 6e 20 62 79  f pSpace[] in by
1b2b3 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73  tes */.){.  cons
1b2b4 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
1b2b5 2a 61 4b 65 79 20 3d 20 28 63 6f 6e 73 74 20 75  *aKey = (const u
1b2b6 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70  nsigned char *)p
1b2b7 4b 65 79 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52  Key;.  UnpackedR
1b2b8 65 63 6f 72 64 20 2a 70 3b 20 20 2f 2a 20 54 68  ecord *p;  /* Th
1b2b9 65 20 75 6e 70 61 63 6b 65 64 20 72 65 63 6f 72  e unpacked recor
1b2ba 64 20 74 68 61 74 20 77 65 20 77 69 6c 6c 20 72  d that we will r
1b2bb 65 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e  eturn */.  int n
1b2bc 42 79 74 65 3b 20 20 20 20 20 20 20 20 20 20 2f  Byte;          /
1b2bd 2a 20 4d 65 6d 6f 72 79 20 73 70 61 63 65 20 6e  * Memory space n
1b2be 65 65 64 65 64 20 74 6f 20 68 6f 6c 64 20 70 2c  eeded to hold p,
1b2bf 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69   in bytes */.  i
1b2c0 6e 74 20 64 3b 0a 20 20 75 33 32 20 69 64 78 3b  nt d;.  u32 idx;
1b2c1 0a 20 20 75 31 36 20 75 3b 20 20 20 20 20 20 20  .  u16 u;       
1b2c2 20 20 20 20 20 20 20 2f 2a 20 55 6e 73 69 67 6e         /* Unsign
1b2c3 65 64 20 6c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  ed loop counter 
1b2c4 2a 2f 0a 20 20 75 33 32 20 73 7a 48 64 72 3b 0a  */.  u32 szHdr;.
1b2c5 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69    Mem *pMem;.  i
1b2c6 6e 74 20 6e 4f 66 66 3b 20 20 20 20 20 20 20 20  nt nOff;        
1b2c7 20 20 20 2f 2a 20 49 6e 63 72 65 61 73 65 20 70     /* Increase p
1b2c8 53 70 61 63 65 20 62 79 20 74 68 69 73 20 6d 75  Space by this mu
1b2c9 63 68 20 74 6f 20 38 2d 62 79 74 65 20 61 6c 69  ch to 8-byte ali
1b2ca 67 6e 20 69 74 20 2a 2f 0a 20 20 0a 20 20 2f 2a  gn it */.  .  /*
1b2cb 0a 20 20 2a 2a 20 57 65 20 77 61 6e 74 20 74 6f  .  ** We want to
1b2cc 20 73 68 69 66 74 20 74 68 65 20 70 6f 69 6e 74   shift the point
1b2cd 65 72 20 70 53 70 61 63 65 20 75 70 20 73 75 63  er pSpace up suc
1b2ce 68 20 74 68 61 74 20 69 74 20 69 73 20 38 2d 62  h that it is 8-b
1b2cf 79 74 65 20 61 6c 69 67 6e 65 64 2e 0a 20 20 2a  yte aligned..  *
1b2d0 2a 20 54 68 75 73 2c 20 77 65 20 6e 65 65 64 20  * Thus, we need 
1b2d1 74 6f 20 63 61 6c 63 75 6c 61 74 65 20 61 20 76  to calculate a v
1b2d2 61 6c 75 65 2c 20 6e 4f 66 66 2c 20 62 65 74 77  alue, nOff, betw
1b2d3 65 65 6e 20 30 20 61 6e 64 20 37 2c 20 74 6f 20  een 0 and 7, to 
1b2d4 73 68 69 66 74 20 0a 20 20 2a 2a 20 69 74 20 62  shift .  ** it b
1b2d5 79 2e 20 20 49 66 20 70 53 70 61 63 65 20 69 73  y.  If pSpace is
1b2d6 20 61 6c 72 65 61 64 79 20 38 2d 62 79 74 65 20   already 8-byte 
1b2d7 61 6c 69 67 6e 65 64 2c 20 6e 4f 66 66 20 73 68  aligned, nOff sh
1b2d8 6f 75 6c 64 20 62 65 20 7a 65 72 6f 2e 0a 20 20  ould be zero..  
1b2d9 2a 2f 0a 20 20 6e 4f 66 66 20 3d 20 28 38 20 2d  */.  nOff = (8 -
1b2da 20 28 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f   (SQLITE_PTR_TO_
1b2db 49 4e 54 28 70 53 70 61 63 65 29 20 26 20 37 29  INT(pSpace) & 7)
1b2dc 29 20 26 20 37 3b 0a 20 20 70 53 70 61 63 65 20  ) & 7;.  pSpace 
1b2dd 2b 3d 20 6e 4f 66 66 3b 0a 20 20 73 7a 53 70 61  += nOff;.  szSpa
1b2de 63 65 20 2d 3d 20 6e 4f 66 66 3b 0a 20 20 6e 42  ce -= nOff;.  nB
1b2df 79 74 65 20 3d 20 52 4f 55 4e 44 38 28 73 69 7a  yte = ROUND8(siz
1b2e0 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63 6f  eof(UnpackedReco
1b2e1 72 64 29 29 20 2b 20 73 69 7a 65 6f 66 28 4d 65  rd)) + sizeof(Me
1b2e2 6d 29 2a 28 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46  m)*(pKeyInfo->nF
1b2e3 69 65 6c 64 2b 31 29 3b 0a 20 20 69 66 28 20 6e  ield+1);.  if( n
1b2e4 42 79 74 65 3e 73 7a 53 70 61 63 65 20 29 7b 0a  Byte>szSpace ){.
1b2e5 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 44      p = sqlite3D
1b2e6 62 4d 61 6c 6c 6f 63 52 61 77 28 70 4b 65 79 49  bMallocRaw(pKeyI
1b2e7 6e 66 6f 2d 3e 64 62 2c 20 6e 42 79 74 65 29 3b  nfo->db, nByte);
1b2e8 0a 20 20 20 20 69 66 28 20 70 3d 3d 30 20 29 20  .    if( p==0 ) 
1b2e9 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 70 2d  return 0;.    p-
1b2ea 3e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45  >flags = UNPACKE
1b2eb 44 5f 4e 45 45 44 5f 46 52 45 45 20 7c 20 55 4e  D_NEED_FREE | UN
1b2ec 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54  PACKED_NEED_DEST
1b2ed 52 4f 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ROY;.  }else{.  
1b2ee 20 20 70 20 3d 20 28 55 6e 70 61 63 6b 65 64 52    p = (UnpackedR
1b2ef 65 63 6f 72 64 2a 29 70 53 70 61 63 65 3b 0a 20  ecord*)pSpace;. 
1b2f0 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 55 4e     p->flags = UN
1b2f1 50 41 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54  PACKED_NEED_DEST
1b2f2 52 4f 59 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4b  ROY;.  }.  p->pK
1b2f3 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66  eyInfo = pKeyInf
1b2f4 6f 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d  o;.  p->nField =
1b2f5 20 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c   pKeyInfo->nFiel
1b2f6 64 20 2b 20 31 3b 0a 20 20 70 2d 3e 61 4d 65 6d  d + 1;.  p->aMem
1b2f7 20 3d 20 70 4d 65 6d 20 3d 20 28 4d 65 6d 2a 29   = pMem = (Mem*)
1b2f8 26 28 28 63 68 61 72 2a 29 70 29 5b 52 4f 55 4e  &((char*)p)[ROUN
1b2f9 44 38 28 73 69 7a 65 6f 66 28 55 6e 70 61 63 6b  D8(sizeof(Unpack
1b2fa 65 64 52 65 63 6f 72 64 29 29 5d 3b 0a 20 20 61  edRecord))];.  a
1b2fb 73 73 65 72 74 28 20 45 49 47 48 54 5f 42 59 54  ssert( EIGHT_BYT
1b2fc 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 70 4d 65 6d  E_ALIGNMENT(pMem
1b2fd 29 20 29 3b 0a 20 20 69 64 78 20 3d 20 67 65 74  ) );.  idx = get
1b2fe 56 61 72 69 6e 74 33 32 28 61 4b 65 79 2c 20 73  Varint32(aKey, s
1b2ff 7a 48 64 72 29 3b 0a 20 20 64 20 3d 20 73 7a 48  zHdr);.  d = szH
1b300 64 72 3b 0a 20 20 75 20 3d 20 30 3b 0a 20 20 77  dr;.  u = 0;.  w
1b301 68 69 6c 65 28 20 69 64 78 3c 73 7a 48 64 72 20  hile( idx<szHdr 
1b302 26 26 20 75 3c 70 2d 3e 6e 46 69 65 6c 64 20 26  && u<p->nField &
1b303 26 20 64 3c 3d 6e 4b 65 79 20 29 7b 0a 20 20 20  & d<=nKey ){.   
1b304 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70 65   u32 serial_type
1b305 3b 0a 0a 20 20 20 20 69 64 78 20 2b 3d 20 67 65  ;..    idx += ge
1b306 74 56 61 72 69 6e 74 33 32 28 26 61 4b 65 79 5b  tVarint32(&aKey[
1b307 69 64 78 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70  idx], serial_typ
1b308 65 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e  e);.    pMem->en
1b309 63 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e  c = pKeyInfo->en
1b30a 63 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 64 62 20  c;.    pMem->db 
1b30b 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a  = pKeyInfo->db;.
1b30c 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
1b30d 3d 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a  = 0;.    pMem->z
1b30e 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20 20 20  Malloc = 0;.    
1b30f 64 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62 65  d += sqlite3Vdbe
1b310 53 65 72 69 61 6c 47 65 74 28 26 61 4b 65 79 5b  SerialGet(&aKey[
1b311 64 5d 2c 20 73 65 72 69 61 6c 5f 74 79 70 65 2c  d], serial_type,
1b312 20 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d 65 6d   pMem);.    pMem
1b313 2b 2b 3b 0a 20 20 20 20 75 2b 2b 3b 0a 20 20 7d  ++;.    u++;.  }
1b314 0a 20 20 61 73 73 65 72 74 28 20 75 3c 3d 70 4b  .  assert( u<=pK
1b315 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b  eyInfo->nField +
1b316 20 31 20 29 3b 0a 20 20 70 2d 3e 6e 46 69 65 6c   1 );.  p->nFiel
1b317 64 20 3d 20 75 3b 0a 20 20 72 65 74 75 72 6e 20  d = u;.  return 
1b318 28 76 6f 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a 0a  (void*)p;.}../*.
1b319 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1b31a 64 65 73 74 72 6f 79 73 20 61 20 55 6e 70 61 63  destroys a Unpac
1b31b 6b 65 64 52 65 63 6f 72 64 20 6f 62 6a 65 63 74  kedRecord object
1b31c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1b31d 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1b31e 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b  VdbeDeleteUnpack
1b31f 65 64 52 65 63 6f 72 64 28 55 6e 70 61 63 6b 65  edRecord(Unpacke
1b320 64 52 65 63 6f 72 64 20 2a 70 29 7b 0a 20 20 69  dRecord *p){.  i
1b321 6e 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65  nt i;.  Mem *pMe
1b322 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21  m;..  assert( p!
1b323 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1b324 70 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41 43  p->flags & UNPAC
1b325 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59  KED_NEED_DESTROY
1b326 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70   );.  for(i=0, p
1b327 4d 65 6d 3d 70 2d 3e 61 4d 65 6d 3b 20 69 3c 70  Mem=p->aMem; i<p
1b328 2d 3e 6e 46 69 65 6c 64 3b 20 69 2b 2b 2c 20 70  ->nField; i++, p
1b329 4d 65 6d 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20 54  Mem++){.    /* T
1b32a 68 65 20 75 6e 70 61 63 6b 65 64 20 72 65 63 6f  he unpacked reco
1b32b 72 64 20 69 73 20 61 6c 77 61 79 73 20 63 6f 6e  rd is always con
1b32c 73 74 72 75 63 74 65 64 20 62 79 20 74 68 65 0a  structed by the.
1b32d 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64      ** sqlite3Vd
1b32e 62 65 55 6e 70 61 63 6b 52 65 63 6f 72 64 28 29  beUnpackRecord()
1b32f 20 66 75 6e 63 74 69 6f 6e 20 61 62 6f 76 65 2c   function above,
1b330 20 77 68 69 63 68 20 6d 61 6b 65 73 20 61 6c 6c   which makes all
1b331 0a 20 20 20 20 2a 2a 20 73 74 72 69 6e 67 73 20  .    ** strings 
1b332 61 6e 64 20 62 6c 6f 62 73 20 73 74 61 74 69 63  and blobs static
1b333 2e 20 20 41 6e 64 20 6e 6f 6e 65 20 6f 66 20 74  .  And none of t
1b334 68 65 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 0a  he elements are.
1b335 20 20 20 20 2a 2a 20 65 76 65 72 20 74 72 61 6e      ** ever tran
1b336 73 66 6f 72 6d 65 64 2c 20 73 6f 20 74 68 65 72  sformed, so ther
1b337 65 20 69 73 20 6e 65 76 65 72 20 61 6e 79 74 68  e is never anyth
1b338 69 6e 67 20 74 6f 20 64 65 6c 65 74 65 2e 0a 20  ing to delete.. 
1b339 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 4e 45     */.    if( NE
1b33a 56 45 52 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f  VER(pMem->zMallo
1b33b 63 29 20 29 20 73 71 6c 69 74 65 33 56 64 62 65  c) ) sqlite3Vdbe
1b33c 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29  MemRelease(pMem)
1b33d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 66  ;.  }.  if( p->f
1b33e 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f  lags & UNPACKED_
1b33f 4e 45 45 44 5f 46 52 45 45 20 29 7b 0a 20 20 20  NEED_FREE ){.   
1b340 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70   sqlite3DbFree(p
1b341 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20  ->pKeyInfo->db, 
1b342 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
1b343 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63   This function c
1b344 6f 6d 70 61 72 65 73 20 74 68 65 20 74 77 6f 20  ompares the two 
1b345 74 61 62 6c 65 20 72 6f 77 73 20 6f 72 20 69 6e  table rows or in
1b346 64 65 78 20 72 65 63 6f 72 64 73 0a 2a 2a 20 73  dex records.** s
1b347 70 65 63 69 66 69 65 64 20 62 79 20 7b 6e 4b 65  pecified by {nKe
1b348 79 31 2c 20 70 4b 65 79 31 7d 20 61 6e 64 20 70  y1, pKey1} and p
1b349 50 4b 65 79 32 2e 20 20 49 74 20 72 65 74 75 72  PKey2.  It retur
1b34a 6e 73 20 61 20 6e 65 67 61 74 69 76 65 2c 20 7a  ns a negative, z
1b34b 65 72 6f 0a 2a 2a 20 6f 72 20 70 6f 73 69 74 69  ero.** or positi
1b34c 76 65 20 69 6e 74 65 67 65 72 20 69 66 20 6b 65  ve integer if ke
1b34d 79 31 20 69 73 20 6c 65 73 73 20 74 68 61 6e 2c  y1 is less than,
1b34e 20 65 71 75 61 6c 20 74 6f 20 6f 72 20 0a 2a 2a   equal to or .**
1b34f 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6b 65   greater than ke
1b350 79 32 2e 20 20 54 68 65 20 7b 6e 4b 65 79 31 2c  y2.  The {nKey1,
1b351 20 70 4b 65 79 31 7d 20 6b 65 79 20 6d 75 73 74   pKey1} key must
1b352 20 62 65 20 61 20 62 6c 6f 62 0a 2a 2a 20 63 72   be a blob.** cr
1b353 65 61 74 65 64 20 62 79 20 74 68 20 4f 50 5f 4d  eated by th OP_M
1b354 61 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65  akeRecord opcode
1b355 20 6f 66 20 74 68 65 20 56 44 42 45 2e 20 20 54   of the VDBE.  T
1b356 68 65 20 70 50 4b 65 79 32 0a 2a 2a 20 6b 65 79  he pPKey2.** key
1b357 20 6d 75 73 74 20 62 65 20 61 20 70 61 72 73 65   must be a parse
1b358 64 20 6b 65 79 20 73 75 63 68 20 61 73 20 6f 62  d key such as ob
1b359 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73  tained from.** s
1b35a 71 6c 69 74 65 33 56 64 62 65 50 61 72 73 65 52  qlite3VdbeParseR
1b35b 65 63 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 4b 65 79  ecord..**.** Key
1b35c 31 20 61 6e 64 20 4b 65 79 32 20 64 6f 20 6e 6f  1 and Key2 do no
1b35d 74 20 68 61 76 65 20 74 6f 20 63 6f 6e 74 61 69  t have to contai
1b35e 6e 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65  n the same numbe
1b35f 72 20 6f 66 20 66 69 65 6c 64 73 2e 0a 2a 2a 20  r of fields..** 
1b360 54 68 65 20 6b 65 79 20 77 69 74 68 20 66 65 77  The key with few
1b361 65 72 20 66 69 65 6c 64 73 20 69 73 20 75 73 75  er fields is usu
1b362 61 6c 6c 79 20 63 6f 6d 70 61 72 65 73 20 6c 65  ally compares le
1b363 73 73 20 74 68 61 6e 20 74 68 65 20 0a 2a 2a 20  ss than the .** 
1b364 6c 6f 6e 67 65 72 20 6b 65 79 2e 20 20 48 6f 77  longer key.  How
1b365 65 76 65 72 20 69 66 20 74 68 65 20 55 4e 50 41  ever if the UNPA
1b366 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20 66 6c 61  CKED_INCRKEY fla
1b367 67 73 20 69 6e 20 70 50 4b 65 79 32 20 69 73 20  gs in pPKey2 is 
1b368 73 65 74 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63  set.** and the c
1b369 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 65 73 20 61  ommon prefixes a
1b36a 72 65 20 65 71 75 61 6c 2c 20 74 68 65 6e 20 6b  re equal, then k
1b36b 65 79 31 20 69 73 20 6c 65 73 73 20 74 68 61 6e  ey1 is less than
1b36c 20 6b 65 79 32 2e 0a 2a 2a 20 4f 72 20 69 66 20   key2..** Or if 
1b36d 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 4d 41 54  the UNPACKED_MAT
1b36e 43 48 5f 50 52 45 46 49 58 20 66 6c 61 67 20 69  CH_PREFIX flag i
1b36f 73 20 73 65 74 20 61 6e 64 20 74 68 65 20 70 72  s set and the pr
1b370 65 66 69 78 65 73 20 61 72 65 0a 2a 2a 20 65 71  efixes are.** eq
1b371 75 61 6c 2c 20 74 68 65 6e 20 74 68 65 20 6b 65  ual, then the ke
1b372 79 73 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65  ys are considere
1b373 64 20 74 6f 20 62 65 20 65 71 75 61 6c 20 61 6e  d to be equal an
1b374 64 0a 2a 2a 20 74 68 65 20 70 61 72 74 73 20 62  d.** the parts b
1b375 65 79 6f 6e 64 20 74 68 65 20 63 6f 6d 6d 6f 6e  eyond the common
1b376 20 70 72 65 66 69 78 20 61 72 65 20 69 67 6e 6f   prefix are igno
1b377 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  red..**.** If th
1b378 65 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52  e UNPACKED_IGNOR
1b379 45 5f 52 4f 57 49 44 20 66 6c 61 67 20 69 73 20  E_ROWID flag is 
1b37a 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 6c 61  set, then the la
1b37b 73 74 20 62 79 74 65 20 6f 66 0a 2a 2a 20 74 68  st byte of.** th
1b37c 65 20 68 65 61 64 65 72 20 6f 66 20 70 4b 65 79  e header of pKey
1b37d 31 20 69 73 20 69 67 6e 6f 72 65 64 2e 20 20 49  1 is ignored.  I
1b37e 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68 61  t is assumed tha
1b37f 74 20 70 4b 65 79 31 20 69 73 0a 2a 2a 20 61 6e  t pKey1 is.** an
1b380 20 69 6e 64 65 78 20 6b 65 79 2c 20 61 6e 64 20   index key, and 
1b381 74 68 75 73 20 65 6e 64 73 20 77 69 74 68 20 61  thus ends with a
1b382 20 72 6f 77 69 64 20 76 61 6c 75 65 2e 20 20 54   rowid value.  T
1b383 68 65 20 6c 61 73 74 20 62 79 74 65 0a 2a 2a 20  he last byte.** 
1b384 6f 66 20 74 68 65 20 68 65 61 64 65 72 20 77 69  of the header wi
1b385 6c 6c 20 74 68 65 72 65 66 6f 72 65 20 62 65 20  ll therefore be 
1b386 74 68 65 20 73 65 72 69 61 6c 20 74 79 70 65 20  the serial type 
1b387 6f 66 20 74 68 65 20 72 6f 77 69 64 3a 0a 2a 2a  of the rowid:.**
1b388 20 6f 6e 65 20 6f 66 20 31 2c 20 32 2c 20 33 2c   one of 1, 2, 3,
1b389 20 34 2c 20 35 2c 20 36 2c 20 38 2c 20 6f 72 20   4, 5, 6, 8, or 
1b38a 39 20 2d 20 74 68 65 20 69 6e 74 65 67 65 72 20  9 - the integer 
1b38b 73 65 72 69 61 6c 20 74 79 70 65 73 2e 0a 2a 2a  serial types..**
1b38c 20 54 68 65 20 73 65 72 69 61 6c 20 74 79 70 65   The serial type
1b38d 20 6f 66 20 74 68 65 20 66 69 6e 61 6c 20 72 6f   of the final ro
1b38e 77 69 64 20 77 69 6c 6c 20 61 6c 77 61 79 73 20  wid will always 
1b38f 62 65 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65  be a single byte
1b390 2e 0a 2a 2a 20 42 79 20 69 67 6e 6f 72 69 6e 67  ..** By ignoring
1b391 20 74 68 69 73 20 6c 61 73 74 20 62 79 74 65 20   this last byte 
1b392 6f 66 20 74 68 65 20 68 65 61 64 65 72 2c 20 77  of the header, w
1b393 65 20 66 6f 72 63 65 20 74 68 65 20 63 6f 6d 70  e force the comp
1b394 61 72 69 73 6f 6e 0a 2a 2a 20 74 6f 20 69 67 6e  arison.** to ign
1b395 6f 72 65 20 74 68 65 20 72 6f 77 69 64 20 61 74  ore the rowid at
1b396 20 74 68 65 20 65 6e 64 20 6f 66 20 6b 65 79 31   the end of key1
1b397 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1b398 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
1b399 64 62 65 52 65 63 6f 72 64 43 6f 6d 70 61 72 65  dbeRecordCompare
1b39a 28 0a 20 20 69 6e 74 20 6e 4b 65 79 31 2c 20 63  (.  int nKey1, c
1b39b 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31  onst void *pKey1
1b39c 2c 20 2f 2a 20 4c 65 66 74 20 6b 65 79 20 2a 2f  , /* Left key */
1b39d 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  .  UnpackedRecor
1b39e 64 20 2a 70 50 4b 65 79 32 20 20 20 20 20 20 20  d *pPKey2       
1b39f 20 2f 2a 20 52 69 67 68 74 20 6b 65 79 20 2a 2f   /* Right key */
1b3a0 0a 29 7b 0a 20 20 69 6e 74 20 64 31 3b 20 20 20  .){.  int d1;   
1b3a1 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73           /* Offs
1b3a2 65 74 20 69 6e 74 6f 20 61 4b 65 79 5b 5d 20 6f  et into aKey[] o
1b3a3 66 20 6e 65 78 74 20 64 61 74 61 20 65 6c 65 6d  f next data elem
1b3a4 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20 69 64 78  ent */.  u32 idx
1b3a5 31 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  1;          /* O
1b3a6 66 66 73 65 74 20 69 6e 74 6f 20 61 4b 65 79 5b  ffset into aKey[
1b3a7 5d 20 6f 66 20 6e 65 78 74 20 68 65 61 64 65 72  ] of next header
1b3a8 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 75 33   element */.  u3
1b3a9 32 20 73 7a 48 64 72 31 3b 20 20 20 20 20 20 20  2 szHdr1;       
1b3aa 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1b3ab 74 65 73 20 69 6e 20 68 65 61 64 65 72 20 2a 2f  tes in header */
1b3ac 0a 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20  .  int i = 0;.  
1b3ad 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20 20 69 6e  int nField;.  in
1b3ae 74 20 72 63 20 3d 20 30 3b 0a 20 20 63 6f 6e 73  t rc = 0;.  cons
1b3af 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
1b3b0 2a 61 4b 65 79 31 20 3d 20 28 63 6f 6e 73 74 20  *aKey1 = (const 
1b3b1 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
1b3b2 70 4b 65 79 31 3b 0a 20 20 4b 65 79 49 6e 66 6f  pKey1;.  KeyInfo
1b3b3 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 4d 65   *pKeyInfo;.  Me
1b3b4 6d 20 6d 65 6d 31 3b 0a 0a 20 20 70 4b 65 79 49  m mem1;..  pKeyI
1b3b5 6e 66 6f 20 3d 20 70 50 4b 65 79 32 2d 3e 70 4b  nfo = pPKey2->pK
1b3b6 65 79 49 6e 66 6f 3b 0a 20 20 6d 65 6d 31 2e 65  eyInfo;.  mem1.e
1b3b7 6e 63 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 65  nc = pKeyInfo->e
1b3b8 6e 63 3b 0a 20 20 6d 65 6d 31 2e 64 62 20 3d 20  nc;.  mem1.db = 
1b3b9 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20  pKeyInfo->db;.  
1b3ba 6d 65 6d 31 2e 66 6c 61 67 73 20 3d 20 30 3b 0a  mem1.flags = 0;.
1b3bb 20 20 6d 65 6d 31 2e 75 2e 69 20 3d 20 30 3b 20    mem1.u.i = 0; 
1b3bc 20 2f 2a 20 6e 6f 74 20 6e 65 65 64 65 64 2c 20   /* not needed, 
1b3bd 68 65 72 65 20 74 6f 20 73 69 6c 65 6e 63 65 20  here to silence 
1b3be 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67  compiler warning
1b3bf 20 2a 2f 0a 20 20 6d 65 6d 31 2e 7a 4d 61 6c 6c   */.  mem1.zMall
1b3c0 6f 63 20 3d 20 30 3b 0a 20 20 0a 20 20 69 64 78  oc = 0;.  .  idx
1b3c1 31 20 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28  1 = getVarint32(
1b3c2 61 4b 65 79 31 2c 20 73 7a 48 64 72 31 29 3b 0a  aKey1, szHdr1);.
1b3c3 20 20 64 31 20 3d 20 73 7a 48 64 72 31 3b 0a 20    d1 = szHdr1;. 
1b3c4 20 69 66 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61   if( pPKey2->fla
1b3c5 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 49 47  gs & UNPACKED_IG
1b3c6 4e 4f 52 45 5f 52 4f 57 49 44 20 29 7b 0a 20 20  NORE_ROWID ){.  
1b3c7 20 20 73 7a 48 64 72 31 2d 2d 3b 0a 20 20 7d 0a    szHdr1--;.  }.
1b3c8 20 20 6e 46 69 65 6c 64 20 3d 20 70 4b 65 79 49    nField = pKeyI
1b3c9 6e 66 6f 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 77  nfo->nField;.  w
1b3ca 68 69 6c 65 28 20 69 64 78 31 3c 73 7a 48 64 72  hile( idx1<szHdr
1b3cb 31 20 26 26 20 69 3c 70 50 4b 65 79 32 2d 3e 6e  1 && i<pPKey2->n
1b3cc 46 69 65 6c 64 20 29 7b 0a 20 20 20 20 75 33 32  Field ){.    u32
1b3cd 20 73 65 72 69 61 6c 5f 74 79 70 65 31 3b 0a 0a   serial_type1;..
1b3ce 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20      /* Read the 
1b3cf 73 65 72 69 61 6c 20 74 79 70 65 73 20 66 6f 72  serial types for
1b3d0 20 74 68 65 20 6e 65 78 74 20 65 6c 65 6d 65 6e   the next elemen
1b3d1 74 20 69 6e 20 65 61 63 68 20 6b 65 79 2e 20 2a  t in each key. *
1b3d2 2f 0a 20 20 20 20 69 64 78 31 20 2b 3d 20 67 65  /.    idx1 += ge
1b3d3 74 56 61 72 69 6e 74 33 32 28 20 61 4b 65 79 31  tVarint32( aKey1
1b3d4 2b 69 64 78 31 2c 20 73 65 72 69 61 6c 5f 74 79  +idx1, serial_ty
1b3d5 70 65 31 20 29 3b 0a 20 20 20 20 69 66 28 20 64  pe1 );.    if( d
1b3d6 31 3e 3d 6e 4b 65 79 31 20 26 26 20 73 71 6c 69  1>=nKey1 && sqli
1b3d7 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
1b3d8 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65  eLen(serial_type
1b3d9 31 29 3e 30 20 29 20 62 72 65 61 6b 3b 0a 0a 20  1)>0 ) break;.. 
1b3da 20 20 20 2f 2a 20 45 78 74 72 61 63 74 20 74 68     /* Extract th
1b3db 65 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 63  e values to be c
1b3dc 6f 6d 70 61 72 65 64 2e 0a 20 20 20 20 2a 2f 0a  ompared..    */.
1b3dd 20 20 20 20 64 31 20 2b 3d 20 73 71 6c 69 74 65      d1 += sqlite
1b3de 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 26  3VdbeSerialGet(&
1b3df 61 4b 65 79 31 5b 64 31 5d 2c 20 73 65 72 69 61  aKey1[d1], seria
1b3e0 6c 5f 74 79 70 65 31 2c 20 26 6d 65 6d 31 29 3b  l_type1, &mem1);
1b3e1 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20  ..    /* Do the 
1b3e2 63 6f 6d 70 61 72 69 73 6f 6e 0a 20 20 20 20 2a  comparison.    *
1b3e3 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
1b3e4 65 33 4d 65 6d 43 6f 6d 70 61 72 65 28 26 6d 65  e3MemCompare(&me
1b3e5 6d 31 2c 20 26 70 50 4b 65 79 32 2d 3e 61 4d 65  m1, &pPKey2->aMe
1b3e6 6d 5b 69 5d 2c 0a 20 20 20 20 20 20 20 20 20 20  m[i],.          
1b3e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b3e8 20 69 3c 6e 46 69 65 6c 64 20 3f 20 70 4b 65 79   i<nField ? pKey
1b3e9 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3a  Info->aColl[i] :
1b3ea 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21   0);.    if( rc!
1b3eb 3d 30 20 29 7b 0a 20 20 20 20 20 20 62 72 65 61  =0 ){.      brea
1b3ec 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 2b 2b  k;.    }.    i++
1b3ed 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 20 6d  ;.  }..  /* No m
1b3ee 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
1b3ef 20 69 73 20 65 76 65 72 20 75 73 65 64 20 6f 6e   is ever used on
1b3f0 20 6d 65 6d 31 2e 20 2a 2f 0a 20 20 69 66 28 20   mem1. */.  if( 
1b3f1 4e 45 56 45 52 28 6d 65 6d 31 2e 7a 4d 61 6c 6c  NEVER(mem1.zMall
1b3f2 6f 63 29 20 29 20 73 71 6c 69 74 65 33 56 64 62  oc) ) sqlite3Vdb
1b3f3 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 65 6d  eMemRelease(&mem
1b3f4 31 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  1);..  /* If the
1b3f5 20 50 52 45 46 49 58 5f 53 45 41 52 43 48 20 66   PREFIX_SEARCH f
1b3f6 6c 61 67 20 69 73 20 73 65 74 20 61 6e 64 20 61  lag is set and a
1b3f7 6c 6c 20 66 69 65 6c 64 73 20 65 78 63 65 70 74  ll fields except
1b3f8 20 74 68 65 20 66 69 6e 61 6c 0a 20 20 2a 2a 20   the final.  ** 
1b3f9 72 6f 77 69 64 20 66 69 65 6c 64 20 77 65 72 65  rowid field were
1b3fa 20 65 71 75 61 6c 2c 20 74 68 65 6e 20 63 6c 65   equal, then cle
1b3fb 61 72 20 74 68 65 20 50 52 45 46 49 58 5f 53 45  ar the PREFIX_SE
1b3fc 41 52 43 48 20 66 6c 61 67 20 61 6e 64 20 73 65  ARCH flag and se
1b3fd 74 20 0a 20 20 2a 2a 20 70 50 4b 65 79 32 2d 3e  t .  ** pPKey2->
1b3fe 72 6f 77 69 64 20 74 6f 20 74 68 65 20 76 61 6c  rowid to the val
1b3ff 75 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20  ue of the rowid 
1b400 66 69 65 6c 64 20 69 6e 20 28 70 4b 65 79 31 2c  field in (pKey1,
1b401 20 6e 4b 65 79 31 29 2e 0a 20 20 2a 2a 20 54 68   nKey1)..  ** Th
1b402 69 73 20 69 73 20 75 73 65 64 20 62 79 20 74 68  is is used by th
1b403 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 20 6f 70  e OP_IsUnique op
1b404 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  code..  */.  if(
1b405 20 28 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20   (pPKey2->flags 
1b406 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49  & UNPACKED_PREFI
1b407 58 5f 53 45 41 52 43 48 29 20 26 26 20 69 3d 3d  X_SEARCH) && i==
1b408 28 70 50 4b 65 79 32 2d 3e 6e 46 69 65 6c 64 2d  (pPKey2->nField-
1b409 31 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  1) ){.    assert
1b40a 28 20 69 64 78 31 3d 3d 73 7a 48 64 72 31 20 26  ( idx1==szHdr1 &
1b40b 26 20 72 63 20 29 3b 0a 20 20 20 20 61 73 73 65  & rc );.    asse
1b40c 72 74 28 20 6d 65 6d 31 2e 66 6c 61 67 73 20 26  rt( mem1.flags &
1b40d 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20   MEM_Int );.    
1b40e 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20 26 3d  pPKey2->flags &=
1b40f 20 7e 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49   ~UNPACKED_PREFI
1b410 58 5f 53 45 41 52 43 48 3b 0a 20 20 20 20 70 50  X_SEARCH;.    pP
1b411 4b 65 79 32 2d 3e 72 6f 77 69 64 20 3d 20 6d 65  Key2->rowid = me
1b412 6d 31 2e 75 2e 69 3b 0a 20 20 7d 0a 0a 20 20 69  m1.u.i;.  }..  i
1b413 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20  f( rc==0 ){.    
1b414 2f 2a 20 72 63 3d 3d 30 20 68 65 72 65 20 6d 65  /* rc==0 here me
1b415 61 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f 66 20  ans that one of 
1b416 74 68 65 20 6b 65 79 73 20 72 61 6e 20 6f 75 74  the keys ran out
1b417 20 6f 66 20 66 69 65 6c 64 73 20 61 6e 64 0a 20   of fields and. 
1b418 20 20 20 2a 2a 20 61 6c 6c 20 74 68 65 20 66 69     ** all the fi
1b419 65 6c 64 73 20 75 70 20 74 6f 20 74 68 61 74 20  elds up to that 
1b41a 70 6f 69 6e 74 20 77 65 72 65 20 65 71 75 61 6c  point were equal
1b41b 2e 20 49 66 20 74 68 65 20 55 4e 50 41 43 4b 45  . If the UNPACKE
1b41c 44 5f 49 4e 43 52 4b 45 59 0a 20 20 20 20 2a 2a  D_INCRKEY.    **
1b41d 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68   flag is set, th
1b41e 65 6e 20 62 72 65 61 6b 20 74 68 65 20 74 69 65  en break the tie
1b41f 20 62 79 20 74 72 65 61 74 69 6e 67 20 6b 65 79   by treating key
1b420 32 20 61 73 20 6c 61 72 67 65 72 2e 0a 20 20 20  2 as larger..   
1b421 20 2a 2a 20 49 66 20 74 68 65 20 55 50 41 43 4b   ** If the UPACK
1b422 45 44 5f 50 52 45 46 49 58 5f 4d 41 54 43 48 20  ED_PREFIX_MATCH 
1b423 66 6c 61 67 20 69 73 20 73 65 74 2c 20 74 68 65  flag is set, the
1b424 6e 20 6b 65 79 73 20 77 69 74 68 20 63 6f 6d 6d  n keys with comm
1b425 6f 6e 20 70 72 65 66 69 78 65 73 0a 20 20 20 20  on prefixes.    
1b426 2a 2a 20 61 72 65 20 63 6f 6e 73 69 64 65 72 65  ** are considere
1b427 64 20 74 6f 20 62 65 20 65 71 75 61 6c 2e 20 20  d to be equal.  
1b428 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 6c  Otherwise, the l
1b429 6f 6e 67 65 72 20 6b 65 79 20 69 73 20 74 68 65  onger key is the
1b42a 20 0a 20 20 20 20 2a 2a 20 6c 61 72 67 65 72 2e   .    ** larger.
1b42b 20 20 41 73 20 69 74 20 68 61 70 70 65 6e 73 2c    As it happens,
1b42c 20 74 68 65 20 70 50 4b 65 79 32 20 77 69 6c 6c   the pPKey2 will
1b42d 20 61 6c 77 61 79 73 20 62 65 20 74 68 65 20 6c   always be the l
1b42e 6f 6e 67 65 72 0a 20 20 20 20 2a 2a 20 69 66 20  onger.    ** if 
1b42f 74 68 65 72 65 20 69 73 20 61 20 64 69 66 66 65  there is a diffe
1b430 72 65 6e 63 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  rence..    */.  
1b431 20 20 69 66 28 20 70 50 4b 65 79 32 2d 3e 66 6c    if( pPKey2->fl
1b432 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 49  ags & UNPACKED_I
1b433 4e 43 52 4b 45 59 20 29 7b 0a 20 20 20 20 20 20  NCRKEY ){.      
1b434 72 63 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 65 6c  rc = -1;.    }el
1b435 73 65 20 69 66 28 20 70 50 4b 65 79 32 2d 3e 66  se if( pPKey2->f
1b436 6c 61 67 73 20 26 20 55 4e 50 41 43 4b 45 44 5f  lags & UNPACKED_
1b437 50 52 45 46 49 58 5f 4d 41 54 43 48 20 29 7b 0a  PREFIX_MATCH ){.
1b438 20 20 20 20 20 20 2f 2a 20 4c 65 61 76 65 20 72        /* Leave r
1b439 63 3d 3d 30 20 2a 2f 0a 20 20 20 20 7d 65 6c 73  c==0 */.    }els
1b43a 65 20 69 66 28 20 69 64 78 31 3c 73 7a 48 64 72  e if( idx1<szHdr
1b43b 31 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  1 ){.      rc = 
1b43c 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  1;.    }.  }else
1b43d 20 69 66 28 20 70 4b 65 79 49 6e 66 6f 2d 3e 61   if( pKeyInfo->a
1b43e 53 6f 72 74 4f 72 64 65 72 20 26 26 20 69 3c 70  SortOrder && i<p
1b43f 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 0a  KeyInfo->nField.
1b440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
1b441 26 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72  & pKeyInfo->aSor
1b442 74 4f 72 64 65 72 5b 69 5d 20 29 7b 0a 20 20 20  tOrder[i] ){.   
1b443 20 72 63 20 3d 20 2d 72 63 3b 0a 20 20 7d 0a 0a   rc = -rc;.  }..
1b444 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 20    return rc;.}. 
1b445 0a 0a 2f 2a 0a 2a 2a 20 70 43 75 72 20 70 6f 69  ../*.** pCur poi
1b446 6e 74 73 20 61 74 20 61 6e 20 69 6e 64 65 78 20  nts at an index 
1b447 65 6e 74 72 79 20 63 72 65 61 74 65 64 20 75 73  entry created us
1b448 69 6e 67 20 74 68 65 20 4f 50 5f 4d 61 6b 65 52  ing the OP_MakeR
1b449 65 63 6f 72 64 20 6f 70 63 6f 64 65 2e 0a 2a 2a  ecord opcode..**
1b44a 20 52 65 61 64 20 74 68 65 20 72 6f 77 69 64 20   Read the rowid 
1b44b 28 74 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20  (the last field 
1b44c 69 6e 20 74 68 65 20 72 65 63 6f 72 64 29 20 61  in the record) a
1b44d 6e 64 20 73 74 6f 72 65 20 69 74 20 69 6e 20 2a  nd store it in *
1b44e 72 6f 77 69 64 2e 0a 2a 2a 20 52 65 74 75 72 6e  rowid..** Return
1b44f 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76   SQLITE_OK if ev
1b450 65 72 79 74 68 69 6e 67 20 77 6f 72 6b 73 2c 20  erything works, 
1b451 6f 72 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  or an error code
1b452 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a   otherwise..**.*
1b453 2a 20 70 43 75 72 20 6d 69 67 68 74 20 62 65 20  * pCur might be 
1b454 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 65 78 74  pointing to text
1b455 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61   obtained from a
1b456 20 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73   corrupt databas
1b457 65 20 66 69 6c 65 2e 0a 2a 2a 20 53 6f 20 74 68  e file..** So th
1b458 65 20 63 6f 6e 74 65 6e 74 20 63 61 6e 6e 6f 74  e content cannot
1b459 20 62 65 20 74 72 75 73 74 65 64 2e 20 20 44 6f   be trusted.  Do
1b45a 20 61 70 70 72 6f 70 72 69 61 74 65 20 63 68 65   appropriate che
1b45b 63 6b 73 20 6f 6e 20 74 68 65 20 63 6f 6e 74 65  cks on the conte
1b45c 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
1b45d 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1b45e 33 56 64 62 65 49 64 78 52 6f 77 69 64 28 73 71  3VdbeIdxRowid(sq
1b45f 6c 69 74 65 33 20 2a 64 62 2c 20 42 74 43 75 72  lite3 *db, BtCur
1b460 73 6f 72 20 2a 70 43 75 72 2c 20 69 36 34 20 2a  sor *pCur, i64 *
1b461 72 6f 77 69 64 29 7b 0a 20 20 69 36 34 20 6e 43  rowid){.  i64 nC
1b462 65 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20 20 69 6e  ellKey = 0;.  in
1b463 74 20 72 63 3b 0a 20 20 75 33 32 20 73 7a 48 64  t rc;.  u32 szHd
1b464 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a  r;        /* Siz
1b465 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 72 20  e of the header 
1b466 2a 2f 0a 20 20 75 33 32 20 74 79 70 65 52 6f 77  */.  u32 typeRow
1b467 69 64 3b 20 20 20 20 2f 2a 20 53 65 72 69 61 6c  id;    /* Serial
1b468 20 74 79 70 65 20 6f 66 20 74 68 65 20 72 6f 77   type of the row
1b469 69 64 20 2a 2f 0a 20 20 75 33 32 20 6c 65 6e 52  id */.  u32 lenR
1b46a 6f 77 69 64 3b 20 20 20 20 20 2f 2a 20 53 69 7a  owid;     /* Siz
1b46b 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 2a  e of the rowid *
1b46c 2f 0a 20 20 4d 65 6d 20 6d 2c 20 76 3b 0a 0a 20  /.  Mem m, v;.. 
1b46d 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
1b46e 52 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 47 65 74  R(db);..  /* Get
1b46f 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
1b470 20 69 6e 64 65 78 20 65 6e 74 72 79 2e 20 20 4f   index entry.  O
1b471 6e 6c 79 20 69 6e 64 69 63 65 73 20 65 6e 74 72  nly indices entr
1b472 69 65 73 20 6f 66 20 6c 65 73 73 0a 20 20 2a 2a  ies of less.  **
1b473 20 74 68 61 6e 20 32 47 69 42 20 61 72 65 20 73   than 2GiB are s
1b474 75 70 70 6f 72 74 20 2d 20 61 6e 79 74 68 69 6e  upport - anythin
1b475 67 20 6c 61 72 67 65 20 6d 75 73 74 20 62 65 20  g large must be 
1b476 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  database corrupt
1b477 69 6f 6e 2e 0a 20 20 2a 2a 20 41 6e 79 20 63 6f  ion..  ** Any co
1b478 72 72 75 70 74 69 6f 6e 20 69 73 20 64 65 74 65  rruption is dete
1b479 63 74 65 64 20 69 6e 20 73 71 6c 69 74 65 33 42  cted in sqlite3B
1b47a 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72  treeParseCellPtr
1b47b 28 29 2c 20 74 68 6f 75 67 68 2c 20 73 6f 0a 20  (), though, so. 
1b47c 20 2a 2a 20 74 68 69 73 20 63 6f 64 65 20 63 61   ** this code ca
1b47d 6e 20 73 61 66 65 6c 79 20 61 73 73 75 6d 65 20  n safely assume 
1b47e 74 68 61 74 20 6e 43 65 6c 6c 4b 65 79 20 69 73  that nCellKey is
1b47f 20 33 32 2d 62 69 74 73 20 20 0a 20 20 2a 2f 0a   32-bits  .  */.
1b480 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1b481 33 42 74 72 65 65 43 75 72 73 6f 72 49 73 56 61  3BtreeCursorIsVa
1b482 6c 69 64 28 70 43 75 72 29 20 29 3b 0a 20 20 72  lid(pCur) );.  r
1b483 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1b484 4b 65 79 53 69 7a 65 28 70 43 75 72 2c 20 26 6e  KeySize(pCur, &n
1b485 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 61 73 73 65  CellKey);.  asse
1b486 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
1b487 4b 20 29 3b 20 20 20 20 20 2f 2a 20 70 43 75 72  K );     /* pCur
1b488 20 69 73 20 61 6c 77 61 79 73 20 76 61 6c 69 64   is always valid
1b489 20 73 6f 20 4b 65 79 53 69 7a 65 20 63 61 6e 6e   so KeySize cann
1b48a 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 61 73 73  ot fail */.  ass
1b48b 65 72 74 28 20 28 6e 43 65 6c 6c 4b 65 79 20 26  ert( (nCellKey &
1b48c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 55 33 32 29   SQLITE_MAX_U32)
1b48d 3d 3d 28 75 36 34 29 6e 43 65 6c 6c 4b 65 79 20  ==(u64)nCellKey 
1b48e 29 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 69 6e  );..  /* Read in
1b48f 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 63 6f   the complete co
1b490 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 69 6e 64  ntent of the ind
1b491 65 78 20 65 6e 74 72 79 20 2a 2f 0a 20 20 6d 65  ex entry */.  me
1b492 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a 65  mset(&m, 0, size
1b493 6f 66 28 6d 29 29 3b 0a 20 20 72 63 20 3d 20 73  of(m));.  rc = s
1b494 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72 6f  qlite3VdbeMemFro
1b495 6d 42 74 72 65 65 28 70 43 75 72 2c 20 30 2c 20  mBtree(pCur, 0, 
1b496 28 69 6e 74 29 6e 43 65 6c 6c 4b 65 79 2c 20 31  (int)nCellKey, 1
1b497 2c 20 26 6d 29 3b 0a 20 20 69 66 28 20 72 63 20  , &m);.  if( rc 
1b498 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
1b499 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20  ;.  }..  /* The 
1b49a 69 6e 64 65 78 20 65 6e 74 72 79 20 6d 75 73 74  index entry must
1b49b 20 62 65 67 69 6e 20 77 69 74 68 20 61 20 68 65   begin with a he
1b49c 61 64 65 72 20 73 69 7a 65 20 2a 2f 0a 20 20 28  ader size */.  (
1b49d 76 6f 69 64 29 67 65 74 56 61 72 69 6e 74 33 32  void)getVarint32
1b49e 28 28 75 38 2a 29 6d 2e 7a 2c 20 73 7a 48 64 72  ((u8*)m.z, szHdr
1b49f 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 73  );.  testcase( s
1b4a0 7a 48 64 72 3d 3d 33 20 29 3b 0a 20 20 74 65 73  zHdr==3 );.  tes
1b4a1 74 63 61 73 65 28 20 73 7a 48 64 72 3d 3d 6d 2e  tcase( szHdr==m.
1b4a2 6e 20 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69 6b  n );.  if( unlik
1b4a3 65 6c 79 28 73 7a 48 64 72 3c 33 20 7c 7c 20 28  ely(szHdr<3 || (
1b4a4 69 6e 74 29 73 7a 48 64 72 3e 6d 2e 6e 29 20 29  int)szHdr>m.n) )
1b4a5 7b 0a 20 20 20 20 67 6f 74 6f 20 69 64 78 5f 72  {.    goto idx_r
1b4a6 6f 77 69 64 5f 63 6f 72 72 75 70 74 69 6f 6e 3b  owid_corruption;
1b4a7 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 6c  .  }..  /* The l
1b4a8 61 73 74 20 66 69 65 6c 64 20 6f 66 20 74 68 65  ast field of the
1b4a9 20 69 6e 64 65 78 20 73 68 6f 75 6c 64 20 62 65   index should be
1b4aa 20 61 6e 20 69 6e 74 65 67 65 72 20 2d 20 74 68   an integer - th
1b4ab 65 20 52 4f 57 49 44 2e 0a 20 20 2a 2a 20 56 65  e ROWID..  ** Ve
1b4ac 72 69 66 79 20 74 68 61 74 20 74 68 65 20 6c 61  rify that the la
1b4ad 73 74 20 65 6e 74 72 79 20 72 65 61 6c 6c 79 20  st entry really 
1b4ae 69 73 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 2a  is an integer. *
1b4af 2f 0a 20 20 28 76 6f 69 64 29 67 65 74 56 61 72  /.  (void)getVar
1b4b0 69 6e 74 33 32 28 28 75 38 2a 29 26 6d 2e 7a 5b  int32((u8*)&m.z[
1b4b1 73 7a 48 64 72 2d 31 5d 2c 20 74 79 70 65 52 6f  szHdr-1], typeRo
1b4b2 77 69 64 29 3b 0a 20 20 74 65 73 74 63 61 73 65  wid);.  testcase
1b4b3 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 31 20 29  ( typeRowid==1 )
1b4b4 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79  ;.  testcase( ty
1b4b5 70 65 52 6f 77 69 64 3d 3d 32 20 29 3b 0a 20 20  peRowid==2 );.  
1b4b6 74 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f  testcase( typeRo
1b4b7 77 69 64 3d 3d 33 20 29 3b 0a 20 20 74 65 73 74  wid==3 );.  test
1b4b8 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d  case( typeRowid=
1b4b9 3d 34 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65  =4 );.  testcase
1b4ba 28 20 74 79 70 65 52 6f 77 69 64 3d 3d 35 20 29  ( typeRowid==5 )
1b4bb 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 74 79  ;.  testcase( ty
1b4bc 70 65 52 6f 77 69 64 3d 3d 36 20 29 3b 0a 20 20  peRowid==6 );.  
1b4bd 74 65 73 74 63 61 73 65 28 20 74 79 70 65 52 6f  testcase( typeRo
1b4be 77 69 64 3d 3d 38 20 29 3b 0a 20 20 74 65 73 74  wid==8 );.  test
1b4bf 63 61 73 65 28 20 74 79 70 65 52 6f 77 69 64 3d  case( typeRowid=
1b4c0 3d 39 20 29 3b 0a 20 20 69 66 28 20 75 6e 6c 69  =9 );.  if( unli
1b4c1 6b 65 6c 79 28 74 79 70 65 52 6f 77 69 64 3c 31  kely(typeRowid<1
1b4c2 20 7c 7c 20 74 79 70 65 52 6f 77 69 64 3e 39 20   || typeRowid>9 
1b4c3 7c 7c 20 74 79 70 65 52 6f 77 69 64 3d 3d 37 29  || typeRowid==7)
1b4c4 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69 64 78   ){.    goto idx
1b4c5 5f 72 6f 77 69 64 5f 63 6f 72 72 75 70 74 69 6f  _rowid_corruptio
1b4c6 6e 3b 0a 20 20 7d 0a 20 20 6c 65 6e 52 6f 77 69  n;.  }.  lenRowi
1b4c7 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53  d = sqlite3VdbeS
1b4c8 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 74 79 70  erialTypeLen(typ
1b4c9 65 52 6f 77 69 64 29 3b 0a 20 20 74 65 73 74 63  eRowid);.  testc
1b4ca 61 73 65 28 20 28 75 33 32 29 6d 2e 6e 3d 3d 73  ase( (u32)m.n==s
1b4cb 7a 48 64 72 2b 6c 65 6e 52 6f 77 69 64 20 29 3b  zHdr+lenRowid );
1b4cc 0a 20 20 69 66 28 20 75 6e 6c 69 6b 65 6c 79 28  .  if( unlikely(
1b4cd 28 75 33 32 29 6d 2e 6e 3c 73 7a 48 64 72 2b 6c  (u32)m.n<szHdr+l
1b4ce 65 6e 52 6f 77 69 64 29 20 29 7b 0a 20 20 20 20  enRowid) ){.    
1b4cf 67 6f 74 6f 20 69 64 78 5f 72 6f 77 69 64 5f 63  goto idx_rowid_c
1b4d0 6f 72 72 75 70 74 69 6f 6e 3b 0a 20 20 7d 0a 0a  orruption;.  }..
1b4d1 20 20 2f 2a 20 46 65 74 63 68 20 74 68 65 20 69    /* Fetch the i
1b4d2 6e 74 65 67 65 72 20 6f 66 66 20 74 68 65 20 65  nteger off the e
1b4d3 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  nd of the index 
1b4d4 72 65 63 6f 72 64 20 2a 2f 0a 20 20 73 71 6c 69  record */.  sqli
1b4d5 74 65 33 56 64 62 65 53 65 72 69 61 6c 47 65 74  te3VdbeSerialGet
1b4d6 28 28 75 38 2a 29 26 6d 2e 7a 5b 6d 2e 6e 2d 6c  ((u8*)&m.z[m.n-l
1b4d7 65 6e 52 6f 77 69 64 5d 2c 20 74 79 70 65 52 6f  enRowid], typeRo
1b4d8 77 69 64 2c 20 26 76 29 3b 0a 20 20 2a 72 6f 77  wid, &v);.  *row
1b4d9 69 64 20 3d 20 76 2e 75 2e 69 3b 0a 20 20 73 71  id = v.u.i;.  sq
1b4da 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
1b4db 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 74 75 72  ase(&m);.  retur
1b4dc 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  n SQLITE_OK;..  
1b4dd 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69 66 20  /* Jump here if 
1b4de 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  database corrupt
1b4df 69 6f 6e 20 69 73 20 64 65 74 65 63 74 65 64 20  ion is detected 
1b4e0 61 66 74 65 72 20 6d 20 68 61 73 20 62 65 65 6e  after m has been
1b4e1 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2e  .  ** allocated.
1b4e2 20 20 46 72 65 65 20 74 68 65 20 6d 20 6f 62 6a    Free the m obj
1b4e3 65 63 74 20 61 6e 64 20 72 65 74 75 72 6e 20 53  ect and return S
1b4e4 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2e 20 2a  QLITE_CORRUPT. *
1b4e5 2f 0a 69 64 78 5f 72 6f 77 69 64 5f 63 6f 72 72  /.idx_rowid_corr
1b4e6 75 70 74 69 6f 6e 3a 0a 20 20 74 65 73 74 63 61  uption:.  testca
1b4e7 73 65 28 20 6d 2e 7a 4d 61 6c 6c 6f 63 21 3d 30  se( m.zMalloc!=0
1b4e8 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   );.  sqlite3Vdb
1b4e9 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b  eMemRelease(&m);
1b4ea 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1b4eb 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 7d  _CORRUPT_BKPT;.}
1b4ec 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20  ../*.** Compare 
1b4ed 74 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 69  the key of the i
1b4ee 6e 64 65 78 20 65 6e 74 72 79 20 74 68 61 74 20  ndex entry that 
1b4ef 63 75 72 73 6f 72 20 70 43 20 69 73 20 70 6f 69  cursor pC is poi
1b4f0 6e 74 69 6e 67 20 74 6f 20 61 67 61 69 6e 73 74  nting to against
1b4f1 0a 2a 2a 20 74 68 65 20 6b 65 79 20 73 74 72 69  .** the key stri
1b4f2 6e 67 20 69 6e 20 70 55 6e 70 61 63 6b 65 64 2e  ng in pUnpacked.
1b4f3 20 20 57 72 69 74 65 20 69 6e 74 6f 20 2a 70 52    Write into *pR
1b4f4 65 73 20 61 20 6e 75 6d 62 65 72 0a 2a 2a 20 74  es a number.** t
1b4f5 68 61 74 20 69 73 20 6e 65 67 61 74 69 76 65 2c  hat is negative,
1b4f6 20 7a 65 72 6f 2c 20 6f 72 20 70 6f 73 69 74 69   zero, or positi
1b4f7 76 65 20 69 66 20 70 43 20 69 73 20 6c 65 73 73  ve if pC is less
1b4f8 20 74 68 61 6e 2c 20 65 71 75 61 6c 20 74 6f 2c   than, equal to,
1b4f9 0a 2a 2a 20 6f 72 20 67 72 65 61 74 65 72 20 74  .** or greater t
1b4fa 68 61 6e 20 70 55 6e 70 61 63 6b 65 64 2e 20 20  han pUnpacked.  
1b4fb 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
1b4fc 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 0a   on success..**.
1b4fd 2a 2a 20 70 55 6e 70 61 63 6b 65 64 20 69 73 20  ** pUnpacked is 
1b4fe 65 69 74 68 65 72 20 63 72 65 61 74 65 64 20 77  either created w
1b4ff 69 74 68 6f 75 74 20 61 20 72 6f 77 69 64 20 6f  ithout a rowid o
1b500 72 20 69 73 20 74 72 75 6e 63 61 74 65 64 20 73  r is truncated s
1b501 6f 20 74 68 61 74 20 69 74 0a 2a 2a 20 6f 6d 69  o that it.** omi
1b502 74 73 20 74 68 65 20 72 6f 77 69 64 20 61 74 20  ts the rowid at 
1b503 74 68 65 20 65 6e 64 2e 20 20 54 68 65 20 72 6f  the end.  The ro
1b504 77 69 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f  wid at the end o
1b505 66 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72  f the index entr
1b506 79 0a 2a 2a 20 69 73 20 69 67 6e 6f 72 65 64 20  y.** is ignored 
1b507 61 73 20 77 65 6c 6c 2e 20 20 48 65 6e 63 65 2c  as well.  Hence,
1b508 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e   this routine on
1b509 6c 79 20 63 6f 6d 70 61 72 65 73 20 74 68 65 20  ly compares the 
1b50a 70 72 65 66 69 78 65 73 20 0a 2a 2a 20 6f 66 20  prefixes .** of 
1b50b 74 68 65 20 6b 65 79 73 20 70 72 69 6f 72 20 74  the keys prior t
1b50c 6f 20 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 69  o the final rowi
1b50d 64 2c 20 6e 6f 74 20 74 68 65 20 65 6e 74 69 72  d, not the entir
1b50e 65 20 6b 65 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  e key..*/.SQLITE
1b50f 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1b510 69 74 65 33 56 64 62 65 49 64 78 4b 65 79 43 6f  ite3VdbeIdxKeyCo
1b511 6d 70 61 72 65 28 0a 20 20 56 64 62 65 43 75 72  mpare(.  VdbeCur
1b512 73 6f 72 20 2a 70 43 2c 20 20 20 20 20 20 20 20  sor *pC,        
1b513 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73       /* The curs
1b514 6f 72 20 74 6f 20 63 6f 6d 70 61 72 65 20 61 67  or to compare ag
1b515 61 69 6e 73 74 20 2a 2f 0a 20 20 55 6e 70 61 63  ainst */.  Unpac
1b516 6b 65 64 52 65 63 6f 72 64 20 2a 70 55 6e 70 61  kedRecord *pUnpa
1b517 63 6b 65 64 2c 20 20 2f 2a 20 55 6e 70 61 63 6b  cked,  /* Unpack
1b518 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 6b 65  ed version of ke
1b519 79 20 74 6f 20 63 6f 6d 70 61 72 65 20 61 67 61  y to compare aga
1b51a 69 6e 73 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 72  inst */.  int *r
1b51b 65 73 20 20 20 20 20 20 20 20 20 20 20 20 20 20  es              
1b51c 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74        /* Write t
1b51d 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 72 65  he comparison re
1b51e 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  sult here */.){.
1b51f 20 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d    i64 nCellKey =
1b520 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20   0;.  int rc;.  
1b521 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 20 3d  BtCursor *pCur =
1b522 20 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20   pC->pCursor;.  
1b523 4d 65 6d 20 6d 3b 0a 0a 20 20 61 73 73 65 72 74  Mem m;..  assert
1b524 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  ( sqlite3BtreeCu
1b525 72 73 6f 72 49 73 56 61 6c 69 64 28 70 43 75 72  rsorIsValid(pCur
1b526 29 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  ) );.  rc = sqli
1b527 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28  te3BtreeKeySize(
1b528 70 43 75 72 2c 20 26 6e 43 65 6c 6c 4b 65 79 29  pCur, &nCellKey)
1b529 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ;.  assert( rc==
1b52a 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20  SQLITE_OK );    
1b52b 2f 2a 20 70 43 75 72 20 69 73 20 61 6c 77 61 79  /* pCur is alway
1b52c 73 20 76 61 6c 69 64 20 73 6f 20 4b 65 79 53 69  s valid so KeySi
1b52d 7a 65 20 63 61 6e 6e 6f 74 20 66 61 69 6c 20 2a  ze cannot fail *
1b52e 2f 0a 20 20 2f 2a 20 6e 43 65 6c 6c 4b 65 79 20  /.  /* nCellKey 
1b52f 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 62  will always be b
1b530 65 74 77 65 65 6e 20 30 20 61 6e 64 20 30 78 66  etween 0 and 0xf
1b531 66 66 66 66 66 66 66 20 62 65 63 61 75 73 65 20  fffffff because 
1b532 6f 66 20 74 68 65 20 73 61 79 0a 20 20 2a 2a 20  of the say.  ** 
1b533 74 68 61 74 20 62 74 72 65 65 50 61 72 73 65 43  that btreeParseC
1b534 65 6c 6c 50 74 72 28 29 20 61 6e 64 20 73 71 6c  ellPtr() and sql
1b535 69 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28  ite3GetVarint32(
1b536 29 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74 65  ) are implemente
1b537 64 20 2a 2f 0a 20 20 69 66 28 20 6e 43 65 6c 6c  d */.  if( nCell
1b538 4b 65 79 3c 3d 30 20 7c 7c 20 6e 43 65 6c 6c 4b  Key<=0 || nCellK
1b539 65 79 3e 30 78 37 66 66 66 66 66 66 66 20 29 7b  ey>0x7fffffff ){
1b53a 0a 20 20 20 20 2a 72 65 73 20 3d 20 30 3b 0a 20  .    *res = 0;. 
1b53b 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1b53c 5f 43 4f 52 52 55 50 54 3b 0a 20 20 7d 0a 20 20  _CORRUPT;.  }.  
1b53d 6d 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69  memset(&m, 0, si
1b53e 7a 65 6f 66 28 6d 29 29 3b 0a 20 20 72 63 20 3d  zeof(m));.  rc =
1b53f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46   sqlite3VdbeMemF
1b540 72 6f 6d 42 74 72 65 65 28 70 43 2d 3e 70 43 75  romBtree(pC->pCu
1b541 72 73 6f 72 2c 20 30 2c 20 28 69 6e 74 29 6e 43  rsor, 0, (int)nC
1b542 65 6c 6c 4b 65 79 2c 20 31 2c 20 26 6d 29 3b 0a  ellKey, 1, &m);.
1b543 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
1b544 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
1b545 20 61 73 73 65 72 74 28 20 70 55 6e 70 61 63 6b   assert( pUnpack
1b546 65 64 2d 3e 66 6c 61 67 73 20 26 20 55 4e 50 41  ed->flags & UNPA
1b547 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49  CKED_IGNORE_ROWI
1b548 44 20 29 3b 0a 20 20 2a 72 65 73 20 3d 20 73 71  D );.  *res = sq
1b549 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 43  lite3VdbeRecordC
1b54a 6f 6d 70 61 72 65 28 6d 2e 6e 2c 20 6d 2e 7a 2c  ompare(m.n, m.z,
1b54b 20 70 55 6e 70 61 63 6b 65 64 29 3b 0a 20 20 73   pUnpacked);.  s
1b54c 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1b54d 65 61 73 65 28 26 6d 29 3b 0a 20 20 72 65 74 75  ease(&m);.  retu
1b54e 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
1b54f 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
1b550 69 6e 65 20 73 65 74 73 20 74 68 65 20 76 61 6c  ine sets the val
1b551 75 65 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65  ue to be returne
1b552 64 20 62 79 20 73 75 62 73 65 71 75 65 6e 74 20  d by subsequent 
1b553 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73 71 6c 69  calls to.** sqli
1b554 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20 6f 6e  te3_changes() on
1b555 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
1b556 6e 64 6c 65 20 27 64 62 27 2e 20 0a 2a 2f 0a 53  ndle 'db'. .*/.S
1b557 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1b558 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  id sqlite3VdbeSe
1b559 74 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33  tChanges(sqlite3
1b55a 20 2a 64 62 2c 20 69 6e 74 20 6e 43 68 61 6e 67   *db, int nChang
1b55b 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  e){.  assert( sq
1b55c 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1b55d 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
1b55e 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 6e   db->nChange = n
1b55f 43 68 61 6e 67 65 3b 0a 20 20 64 62 2d 3e 6e 54  Change;.  db->nT
1b560 6f 74 61 6c 43 68 61 6e 67 65 20 2b 3d 20 6e 43  otalChange += nC
1b561 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  hange;.}../*.** 
1b562 53 65 74 20 61 20 66 6c 61 67 20 69 6e 20 74 68  Set a flag in th
1b563 65 20 76 64 62 65 20 74 6f 20 75 70 64 61 74 65  e vdbe to update
1b564 20 74 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e   the change coun
1b565 74 65 72 20 77 68 65 6e 20 69 74 20 69 73 20 66  ter when it is f
1b566 69 6e 61 6c 69 73 65 64 0a 2a 2a 20 6f 72 20 72  inalised.** or r
1b567 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eset..*/.SQLITE_
1b568 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1b569 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61  ite3VdbeCountCha
1b56a 6e 67 65 73 28 56 64 62 65 20 2a 76 29 7b 0a 20  nges(Vdbe *v){. 
1b56b 20 76 2d 3e 63 68 61 6e 67 65 43 6e 74 4f 6e 20   v->changeCntOn 
1b56c 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61  = 1;.}../*.** Ma
1b56d 72 6b 20 65 76 65 72 79 20 70 72 65 70 61 72 65  rk every prepare
1b56e 64 20 73 74 61 74 65 6d 65 6e 74 20 61 73 73 6f  d statement asso
1b56f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 64 61  ciated with a da
1b570 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1b571 6e 0a 2a 2a 20 61 73 20 65 78 70 69 72 65 64 2e  n.** as expired.
1b572 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 69 72 65  .**.** An expire
1b573 64 20 73 74 61 74 65 6d 65 6e 74 20 6d 65 61 6e  d statement mean
1b574 73 20 74 68 61 74 20 72 65 63 6f 6d 70 69 6c 61  s that recompila
1b575 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61 74  tion of the stat
1b576 65 6d 65 6e 74 20 69 73 0a 2a 2a 20 72 65 63 6f  ement is.** reco
1b577 6d 6d 65 6e 64 2e 20 20 53 74 61 74 65 6d 65 6e  mmend.  Statemen
1b578 74 73 20 65 78 70 69 72 65 20 77 68 65 6e 20 74  ts expire when t
1b579 68 69 6e 67 73 20 68 61 70 70 65 6e 20 74 68 61  hings happen tha
1b57a 74 20 6d 61 6b 65 20 74 68 65 69 72 0a 2a 2a 20  t make their.** 
1b57b 70 72 6f 67 72 61 6d 73 20 6f 62 73 6f 6c 65 74  programs obsolet
1b57c 65 2e 20 20 52 65 6d 6f 76 69 6e 67 20 75 73 65  e.  Removing use
1b57d 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  r-defined functi
1b57e 6f 6e 73 20 6f 72 20 63 6f 6c 6c 61 74 69 6e 67  ons or collating
1b57f 0a 2a 2a 20 73 65 71 75 65 6e 63 65 73 2c 20 6f  .** sequences, o
1b580 72 20 63 68 61 6e 67 69 6e 67 20 61 6e 20 61 75  r changing an au
1b581 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63  thorization func
1b582 74 69 6f 6e 20 61 72 65 20 74 68 65 20 74 79 70  tion are the typ
1b583 65 73 20 6f 66 0a 2a 2a 20 74 68 69 6e 67 73 20  es of.** things 
1b584 74 68 61 74 20 6d 61 6b 65 20 70 72 65 70 61 72  that make prepar
1b585 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 62  ed statements ob
1b586 73 6f 6c 65 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54  solete..*/.SQLIT
1b587 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1b588 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70  qlite3ExpirePrep
1b589 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 73  aredStatements(s
1b58a 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 56  qlite3 *db){.  V
1b58b 64 62 65 20 2a 70 3b 0a 20 20 66 6f 72 28 70 20  dbe *p;.  for(p 
1b58c 3d 20 64 62 2d 3e 70 56 64 62 65 3b 20 70 3b 20  = db->pVdbe; p; 
1b58d 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  p=p->pNext){.   
1b58e 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b   p->expired = 1;
1b58f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
1b590 74 75 72 6e 20 74 68 65 20 64 61 74 61 62 61 73  turn the databas
1b591 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
1b592 68 20 74 68 65 20 56 64 62 65 2e 0a 2a 2f 0a 53  h the Vdbe..*/.S
1b593 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73 71  QLITE_PRIVATE sq
1b594 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 56 64  lite3 *sqlite3Vd
1b595 62 65 44 62 28 56 64 62 65 20 2a 76 29 7b 0a 20  beDb(Vdbe *v){. 
1b596 20 72 65 74 75 72 6e 20 76 2d 3e 64 62 3b 0a 7d   return v->db;.}
1b597 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ../*.** Return a
1b598 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73   pointer to an s
1b599 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 73 74 72  qlite3_value str
1b59a 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 69 6e  ucture containin
1b59b 67 20 74 68 65 20 76 61 6c 75 65 20 62 6f 75 6e  g the value boun
1b59c 64 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 20 69  d.** parameter i
1b59d 56 61 72 20 6f 66 20 56 4d 20 76 2e 20 45 78 63  Var of VM v. Exc
1b59e 65 70 74 2c 20 69 66 20 74 68 65 20 76 61 6c 75  ept, if the valu
1b59f 65 20 69 73 20 61 6e 20 53 51 4c 20 4e 55 4c 4c  e is an SQL NULL
1b5a0 2c 20 72 65 74 75 72 6e 20 0a 2a 2a 20 30 20 69  , return .** 0 i
1b5a1 6e 73 74 65 61 64 2e 20 55 6e 6c 65 73 73 20 69  nstead. Unless i
1b5a2 74 20 69 73 20 4e 55 4c 4c 2c 20 61 70 70 6c 79  t is NULL, apply
1b5a3 20 61 66 66 69 6e 69 74 79 20 61 66 66 20 28 6f   affinity aff (o
1b5a4 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45  ne of the SQLITE
1b5a5 5f 41 46 46 5f 2a 0a 2a 2a 20 63 6f 6e 73 74 61  _AFF_*.** consta
1b5a6 6e 74 73 29 20 74 6f 20 74 68 65 20 76 61 6c 75  nts) to the valu
1b5a7 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  e before returni
1b5a8 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ng it..**.** The
1b5a9 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20   returned value 
1b5aa 6d 75 73 74 20 62 65 20 66 72 65 65 64 20 62 79  must be freed by
1b5ab 20 74 68 65 20 63 61 6c 6c 65 72 20 75 73 69 6e   the caller usin
1b5ac 67 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72  g sqlite3ValueFr
1b5ad 65 65 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ee()..*/.SQLITE_
1b5ae 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f  PRIVATE sqlite3_
1b5af 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 64  value *sqlite3Vd
1b5b0 62 65 47 65 74 56 61 6c 75 65 28 56 64 62 65 20  beGetValue(Vdbe 
1b5b1 2a 76 2c 20 69 6e 74 20 69 56 61 72 2c 20 75 38  *v, int iVar, u8
1b5b2 20 61 66 66 29 7b 0a 20 20 61 73 73 65 72 74 28   aff){.  assert(
1b5b3 20 69 56 61 72 3e 30 20 29 3b 0a 20 20 69 66 28   iVar>0 );.  if(
1b5b4 20 76 20 29 7b 0a 20 20 20 20 4d 65 6d 20 2a 70   v ){.    Mem *p
1b5b5 4d 65 6d 20 3d 20 26 76 2d 3e 61 56 61 72 5b 69  Mem = &v->aVar[i
1b5b6 56 61 72 2d 31 5d 3b 0a 20 20 20 20 69 66 28 20  Var-1];.    if( 
1b5b7 30 3d 3d 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  0==(pMem->flags 
1b5b8 26 20 4d 45 4d 5f 4e 75 6c 6c 29 20 29 7b 0a 20  & MEM_Null) ){. 
1b5b9 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c       sqlite3_val
1b5ba 75 65 20 2a 70 52 65 74 20 3d 20 73 71 6c 69 74  ue *pRet = sqlit
1b5bb 65 33 56 61 6c 75 65 4e 65 77 28 76 2d 3e 64 62  e3ValueNew(v->db
1b5bc 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 52 65  );.      if( pRe
1b5bd 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  t ){.        sql
1b5be 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79 28  ite3VdbeMemCopy(
1b5bf 28 4d 65 6d 20 2a 29 70 52 65 74 2c 20 70 4d 65  (Mem *)pRet, pMe
1b5c0 6d 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  m);.        sqli
1b5c1 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66  te3ValueApplyAff
1b5c2 69 6e 69 74 79 28 70 52 65 74 2c 20 61 66 66 2c  inity(pRet, aff,
1b5c3 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20   SQLITE_UTF8);. 
1b5c4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1b5c5 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 28  beMemStoreType((
1b5c6 4d 65 6d 20 2a 29 70 52 65 74 29 3b 0a 20 20 20  Mem *)pRet);.   
1b5c7 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72     }.      retur
1b5c8 6e 20 70 52 65 74 3b 0a 20 20 20 20 7d 0a 20 20  n pRet;.    }.  
1b5c9 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
1b5ca 0a 2f 2a 0a 2a 2a 20 43 6f 6e 66 69 67 75 72 65  ./*.** Configure
1b5cb 20 53 51 4c 20 76 61 72 69 61 62 6c 65 20 69 56   SQL variable iV
1b5cc 61 72 20 73 6f 20 74 68 61 74 20 62 69 6e 64 69  ar so that bindi
1b5cd 6e 67 20 61 20 6e 65 77 20 76 61 6c 75 65 20 74  ng a new value t
1b5ce 6f 20 69 74 20 73 69 67 6e 61 6c 73 0a 2a 2a 20  o it signals.** 
1b5cf 74 6f 20 73 71 6c 69 74 65 33 5f 72 65 6f 70 74  to sqlite3_reopt
1b5d0 69 6d 69 7a 65 28 29 20 74 68 61 74 20 72 65 2d  imize() that re-
1b5d1 70 72 65 70 61 72 69 6e 67 20 74 68 65 20 73 74  preparing the st
1b5d2 61 74 65 6d 65 6e 74 20 6d 61 79 20 72 65 73 75  atement may resu
1b5d3 6c 74 0a 2a 2a 20 69 6e 20 61 20 62 65 74 74 65  lt.** in a bette
1b5d4 72 20 71 75 65 72 79 20 70 6c 61 6e 2e 0a 2a 2f  r query plan..*/
1b5d5 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1b5d6 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
1b5d7 53 65 74 56 61 72 6d 61 73 6b 28 56 64 62 65 20  SetVarmask(Vdbe 
1b5d8 2a 76 2c 20 69 6e 74 20 69 56 61 72 29 7b 0a 20  *v, int iVar){. 
1b5d9 20 61 73 73 65 72 74 28 20 69 56 61 72 3e 30 20   assert( iVar>0 
1b5da 29 3b 0a 20 20 69 66 28 20 69 56 61 72 3e 33 32  );.  if( iVar>32
1b5db 20 29 7b 0a 20 20 20 20 76 2d 3e 65 78 70 6d 61   ){.    v->expma
1b5dc 73 6b 20 3d 20 30 78 66 66 66 66 66 66 66 66 3b  sk = 0xffffffff;
1b5dd 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 76 2d  .  }else{.    v-
1b5de 3e 65 78 70 6d 61 73 6b 20 7c 3d 20 28 28 75 33  >expmask |= ((u3
1b5df 32 29 31 20 3c 3c 20 28 69 56 61 72 2d 31 29 29  2)1 << (iVar-1))
1b5e0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  ;.  }.}../******
1b5e1 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
1b5e2 76 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a  vdbeaux.c ******
1b5e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b5e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b5e5 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
1b5e6 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
1b5e7 69 6c 65 20 76 64 62 65 61 70 69 2e 63 20 2a 2a  ile vdbeapi.c **
1b5e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b5e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b5ea 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
1b5eb 30 30 34 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a  004 May 26.**.**
1b5ec 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1b5ed 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1b5ee 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1b5ef 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1b5f0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1b5f1 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1b5f2 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1b5f3 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1b5f4 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1b5f5 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1b5f6 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1b5f7 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1b5f8 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1b5f9 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1b5fa 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1b5fb 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1b5fc 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1b5fd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b5fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b5ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b601 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
1b602 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
1b603 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f 20 69  ns code use to i
1b604 6d 70 6c 65 6d 65 6e 74 20 41 50 49 73 20 74 68  mplement APIs th
1b605 61 74 20 61 72 65 20 70 61 72 74 20 6f 66 20 74  at are part of t
1b606 68 65 0a 2a 2a 20 56 44 42 45 2e 0a 2a 2a 0a 2a  he.** VDBE..**.*
1b607 2a 20 24 49 64 3a 20 76 64 62 65 61 70 69 2e 63  * $Id: vdbeapi.c
1b608 2c 76 20 31 2e 31 36 37 20 32 30 30 39 2f 30 36  ,v 1.167 2009/06
1b609 2f 32 35 20 30 31 3a 34 37 3a 31 32 20 64 72 68  /25 01:47:12 drh
1b60a 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64   Exp $.*/..#ifnd
1b60b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
1b60c 45 50 52 45 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20  EPRECATED./*.** 
1b60d 52 65 74 75 72 6e 20 54 52 55 45 20 28 6e 6f 6e  Return TRUE (non
1b60e 2d 7a 65 72 6f 29 20 6f 66 20 74 68 65 20 73 74  -zero) of the st
1b60f 61 74 65 6d 65 6e 74 20 73 75 70 70 6c 69 65 64  atement supplied
1b610 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20   as an argument 
1b611 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20 72  needs.** to be r
1b612 65 63 6f 6d 70 69 6c 65 64 2e 20 20 41 20 73 74  ecompiled.  A st
1b613 61 74 65 6d 65 6e 74 20 6e 65 65 64 73 20 74 6f  atement needs to
1b614 20 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20 77   be recompiled w
1b615 68 65 6e 65 76 65 72 20 74 68 65 0a 2a 2a 20 65  henever the.** e
1b616 78 65 63 75 74 69 6f 6e 20 65 6e 76 69 72 6f 6e  xecution environ
1b617 6d 65 6e 74 20 63 68 61 6e 67 65 73 20 69 6e 20  ment changes in 
1b618 61 20 77 61 79 20 74 68 61 74 20 77 6f 75 6c 64  a way that would
1b619 20 61 6c 74 65 72 20 74 68 65 20 70 72 6f 67 72   alter the progr
1b61a 61 6d 0a 2a 2a 20 74 68 61 74 20 73 71 6c 69 74  am.** that sqlit
1b61b 65 33 5f 70 72 65 70 61 72 65 28 29 20 67 65 6e  e3_prepare() gen
1b61c 65 72 61 74 65 73 2e 20 20 46 6f 72 20 65 78 61  erates.  For exa
1b61d 6d 70 6c 65 2c 20 69 66 20 6e 65 77 20 66 75 6e  mple, if new fun
1b61e 63 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20 63 6f 6c  ctions or.** col
1b61f 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73  lating sequences
1b620 20 61 72 65 20 72 65 67 69 73 74 65 72 65 64 20   are registered 
1b621 6f 72 20 69 66 20 61 6e 20 61 75 74 68 6f 72 69  or if an authori
1b622 7a 65 72 20 66 75 6e 63 74 69 6f 6e 20 69 73 0a  zer function is.
1b623 2a 2a 20 61 64 64 65 64 20 6f 72 20 63 68 61 6e  ** added or chan
1b624 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ged..*/.SQLITE_A
1b625 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  PI int sqlite3_e
1b626 78 70 69 72 65 64 28 73 71 6c 69 74 65 33 5f 73  xpired(sqlite3_s
1b627 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56  tmt *pStmt){.  V
1b628 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29  dbe *p = (Vdbe*)
1b629 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e 20  pStmt;.  return 
1b62a 70 3d 3d 30 20 7c 7c 20 70 2d 3e 65 78 70 69 72  p==0 || p->expir
1b62b 65 64 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ed;.}.#endif../*
1b62c 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
1b62d 67 20 72 6f 75 74 69 6e 65 20 64 65 73 74 72 6f  g routine destro
1b62e 79 73 20 61 20 76 69 72 74 75 61 6c 20 6d 61 63  ys a virtual mac
1b62f 68 69 6e 65 20 74 68 61 74 20 69 73 20 63 72 65  hine that is cre
1b630 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 73  ated by.** the s
1b631 71 6c 69 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29  qlite3_compile()
1b632 20 72 6f 75 74 69 6e 65 2e 20 54 68 65 20 69 6e   routine. The in
1b633 74 65 67 65 72 20 72 65 74 75 72 6e 65 64 20 69  teger returned i
1b634 73 20 61 6e 20 53 51 4c 49 54 45 5f 0a 2a 2a 20  s an SQLITE_.** 
1b635 73 75 63 63 65 73 73 2f 66 61 69 6c 75 72 65 20  success/failure 
1b636 63 6f 64 65 20 74 68 61 74 20 64 65 73 63 72 69  code that descri
1b637 62 65 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f  bes the result o
1b638 66 20 65 78 65 63 75 74 69 6e 67 20 74 68 65 20  f executing the 
1b639 76 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69  virtual.** machi
1b63a 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ne..**.** This r
1b63b 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20  outine sets the 
1b63c 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 20 73  error code and s
1b63d 74 72 69 6e 67 20 72 65 74 75 72 6e 65 64 20 62  tring returned b
1b63e 79 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72  y.** sqlite3_err
1b63f 63 6f 64 65 28 29 2c 20 73 71 6c 69 74 65 33 5f  code(), sqlite3_
1b640 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71 6c  errmsg() and sql
1b641 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 2e  ite3_errmsg16().
1b642 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
1b643 6e 74 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  nt sqlite3_final
1b644 69 7a 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ize(sqlite3_stmt
1b645 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74 20   *pStmt){.  int 
1b646 72 63 3b 0a 20 20 69 66 28 20 70 53 74 6d 74 3d  rc;.  if( pStmt=
1b647 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53  =0 ){.    rc = S
1b648 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73  QLITE_OK;.  }els
1b649 65 7b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d  e{.    Vdbe *v =
1b64a 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20   (Vdbe*)pStmt;. 
1b64b 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d     sqlite3 *db =
1b64c 20 76 2d 3e 64 62 3b 0a 23 69 66 20 53 51 4c 49   v->db;.#if SQLI
1b64d 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20  TE_THREADSAFE.  
1b64e 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
1b64f 2a 6d 75 74 65 78 20 3d 20 76 2d 3e 64 62 2d 3e  *mutex = v->db->
1b650 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 0a 20 20  mutex;.#endif.  
1b651 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1b652 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20  enter(mutex);.  
1b653 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1b654 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a 20  beFinalize(v);. 
1b655 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41     rc = sqlite3A
1b656 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a  piExit(db, rc);.
1b657 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1b658 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a  x_leave(mutex);.
1b659 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1b65a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 72 6d 69 6e  .}../*.** Termin
1b65b 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ate the current 
1b65c 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 61 6e 20  execution of an 
1b65d 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 6e  SQL statement an
1b65e 64 20 72 65 73 65 74 20 69 74 0a 2a 2a 20 62 61  d reset it.** ba
1b65f 63 6b 20 74 6f 20 69 74 73 20 73 74 61 72 74 69  ck to its starti
1b660 6e 67 20 73 74 61 74 65 20 73 6f 20 74 68 61 74  ng state so that
1b661 20 69 74 20 63 61 6e 20 62 65 20 72 65 75 73 65   it can be reuse
1b662 64 2e 20 41 20 73 75 63 63 65 73 73 20 63 6f 64  d. A success cod
1b663 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 70 72  e from.** the pr
1b664 69 6f 72 20 65 78 65 63 75 74 69 6f 6e 20 69 73  ior execution is
1b665 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
1b666 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65   This routine se
1b667 74 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  ts the error cod
1b668 65 20 61 6e 64 20 73 74 72 69 6e 67 20 72 65 74  e and string ret
1b669 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69  urned by.** sqli
1b66a 74 65 33 5f 65 72 72 63 6f 64 65 28 29 2c 20 73  te3_errcode(), s
1b66b 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20  qlite3_errmsg() 
1b66c 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  and sqlite3_errm
1b66d 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  sg16()..*/.SQLIT
1b66e 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1b66f 33 5f 72 65 73 65 74 28 73 71 6c 69 74 65 33 5f  3_reset(sqlite3_
1b670 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20  stmt *pStmt){.  
1b671 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 53  int rc;.  if( pS
1b672 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63  tmt==0 ){.    rc
1b673 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1b674 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64 62 65 20  }else{.    Vdbe 
1b675 2a 76 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d  *v = (Vdbe*)pStm
1b676 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  t;.    sqlite3_m
1b677 75 74 65 78 5f 65 6e 74 65 72 28 76 2d 3e 64 62  utex_enter(v->db
1b678 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63  ->mutex);.    rc
1b679 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65   = sqlite3VdbeRe
1b67a 73 65 74 28 76 29 3b 0a 20 20 20 20 73 71 6c 69  set(v);.    sqli
1b67b 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79  te3VdbeMakeReady
1b67c 28 76 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 30 2c  (v, -1, 0, 0, 0,
1b67d 20 30 2c 20 30 29 3b 0a 20 20 20 20 61 73 73 65   0, 0);.    asse
1b67e 72 74 28 20 28 72 63 20 26 20 28 76 2d 3e 64 62  rt( (rc & (v->db
1b67f 2d 3e 65 72 72 4d 61 73 6b 29 29 3d 3d 72 63 20  ->errMask))==rc 
1b680 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
1b681 74 65 33 41 70 69 45 78 69 74 28 76 2d 3e 64 62  te3ApiExit(v->db
1b682 2c 20 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74  , rc);.    sqlit
1b683 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 76  e3_mutex_leave(v
1b684 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
1b685 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1b686 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 61 6c 6c 20  ../*.** Set all 
1b687 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 69  the parameters i
1b688 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 64 20 53  n the compiled S
1b689 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20  QL statement to 
1b68a 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  NULL..*/.SQLITE_
1b68b 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1b68c 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 28 73  clear_bindings(s
1b68d 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1b68e 6d 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  mt){.  int i;.  
1b68f 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1b690 4f 4b 3b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20  OK;.  Vdbe *p = 
1b691 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 23 69  (Vdbe*)pStmt;.#i
1b692 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
1b693 41 46 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  AFE.  sqlite3_mu
1b694 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 28 28 56  tex *mutex = ((V
1b695 64 62 65 2a 29 70 53 74 6d 74 29 2d 3e 64 62 2d  dbe*)pStmt)->db-
1b696 3e 6d 75 74 65 78 3b 0a 23 65 6e 64 69 66 0a 20  >mutex;.#endif. 
1b697 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
1b698 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20 20 66  nter(mutex);.  f
1b699 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 56 61  or(i=0; i<p->nVa
1b69a 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  r; i++){.    sql
1b69b 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
1b69c 73 65 28 26 70 2d 3e 61 56 61 72 5b 69 5d 29 3b  se(&p->aVar[i]);
1b69d 0a 20 20 20 20 70 2d 3e 61 56 61 72 5b 69 5d 2e  .    p->aVar[i].
1b69e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
1b69f 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69  ;.  }.  if( p->i
1b6a0 73 50 72 65 70 61 72 65 56 32 20 26 26 20 70 2d  sPrepareV2 && p-
1b6a1 3e 65 78 70 6d 61 73 6b 20 29 7b 0a 20 20 20 20  >expmask ){.    
1b6a2 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b 0a  p->expired = 1;.
1b6a3 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
1b6a4 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29  tex_leave(mutex)
1b6a5 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
1b6a6 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .../************
1b6a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b6a8 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 20   sqlite3_value_ 
1b6a9 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1b6aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b6ab 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
1b6ac 67 20 72 6f 75 74 69 6e 65 73 20 65 78 74 72 61  g routines extra
1b6ad 63 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66  ct information f
1b6ae 72 6f 6d 20 61 20 4d 65 6d 20 6f 72 20 73 71 6c  rom a Mem or sql
1b6af 69 74 65 33 5f 76 61 6c 75 65 0a 2a 2a 20 73 74  ite3_value.** st
1b6b0 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49  ructure..*/.SQLI
1b6b1 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
1b6b2 64 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  d *sqlite3_value
1b6b3 5f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f 76 61  _blob(sqlite3_va
1b6b4 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 4d 65  lue *pVal){.  Me
1b6b5 6d 20 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 56 61  m *p = (Mem*)pVa
1b6b6 6c 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67  l;.  if( p->flag
1b6b7 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45  s & (MEM_Blob|ME
1b6b8 4d 5f 53 74 72 29 20 29 7b 0a 20 20 20 20 73 71  M_Str) ){.    sq
1b6b9 6c 69 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61  lite3VdbeMemExpa
1b6ba 6e 64 42 6c 6f 62 28 70 29 3b 0a 20 20 20 20 70  ndBlob(p);.    p
1b6bb 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f  ->flags &= ~MEM_
1b6bc 53 74 72 3b 0a 20 20 20 20 70 2d 3e 66 6c 61 67  Str;.    p->flag
1b6bd 73 20 7c 3d 20 4d 45 4d 5f 42 6c 6f 62 3b 0a 20  s |= MEM_Blob;. 
1b6be 20 20 20 72 65 74 75 72 6e 20 70 2d 3e 7a 3b 0a     return p->z;.
1b6bf 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
1b6c0 75 72 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  urn sqlite3_valu
1b6c1 65 5f 74 65 78 74 28 70 56 61 6c 29 3b 0a 20 20  e_text(pVal);.  
1b6c2 7d 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  }.}.SQLITE_API i
1b6c3 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt sqlite3_value
1b6c4 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76  _bytes(sqlite3_v
1b6c5 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72  alue *pVal){.  r
1b6c6 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c  eturn sqlite3Val
1b6c7 75 65 42 79 74 65 73 28 70 56 61 6c 2c 20 53 51  ueBytes(pVal, SQ
1b6c8 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 53 51  LITE_UTF8);.}.SQ
1b6c9 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1b6ca 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
1b6cb 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  16(sqlite3_value
1b6cc 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72   *pVal){.  retur
1b6cd 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79  n sqlite3ValueBy
1b6ce 74 65 73 28 70 56 61 6c 2c 20 53 51 4c 49 54 45  tes(pVal, SQLITE
1b6cf 5f 55 54 46 31 36 4e 41 54 49 56 45 29 3b 0a 7d  _UTF16NATIVE);.}
1b6d0 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62  .SQLITE_API doub
1b6d1 6c 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  le sqlite3_value
1b6d2 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f  _double(sqlite3_
1b6d3 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20  value *pVal){.  
1b6d4 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64  return sqlite3Vd
1b6d5 62 65 52 65 61 6c 56 61 6c 75 65 28 28 4d 65 6d  beRealValue((Mem
1b6d6 2a 29 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54  *)pVal);.}.SQLIT
1b6d7 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1b6d8 33 5f 76 61 6c 75 65 5f 69 6e 74 28 73 71 6c 69  3_value_int(sqli
1b6d9 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29  te3_value *pVal)
1b6da 7b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74 29  {.  return (int)
1b6db 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61  sqlite3VdbeIntVa
1b6dc 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b  lue((Mem*)pVal);
1b6dd 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  .}.SQLITE_API sq
1b6de 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 6c 69 74  lite_int64 sqlit
1b6df 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 73  e3_value_int64(s
1b6e0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
1b6e1 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  al){.  return sq
1b6e2 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c 75  lite3VdbeIntValu
1b6e3 65 28 28 4d 65 6d 2a 29 70 56 61 6c 29 3b 0a 7d  e((Mem*)pVal);.}
1b6e4 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
1b6e5 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
1b6e6 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  *sqlite3_value_t
1b6e7 65 78 74 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  ext(sqlite3_valu
1b6e8 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75  e *pVal){.  retu
1b6e9 72 6e 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e  rn (const unsign
1b6ea 65 64 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65  ed char *)sqlite
1b6eb 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c  3ValueText(pVal,
1b6ec 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 7d   SQLITE_UTF8);.}
1b6ed 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1b6ee 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54  OMIT_UTF16.SQLIT
1b6ef 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
1b6f0 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   *sqlite3_value_
1b6f1 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 76  text16(sqlite3_v
1b6f2 61 6c 75 65 2a 20 70 56 61 6c 29 7b 0a 20 20 72  alue* pVal){.  r
1b6f3 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61 6c  eturn sqlite3Val
1b6f4 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51 4c  ueText(pVal, SQL
1b6f5 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 29  ITE_UTF16NATIVE)
1b6f6 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  ;.}.SQLITE_API c
1b6f7 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74  onst void *sqlit
1b6f8 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62  e3_value_text16b
1b6f9 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  e(sqlite3_value 
1b6fa 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e  *pVal){.  return
1b6fb 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78   sqlite3ValueTex
1b6fc 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55  t(pVal, SQLITE_U
1b6fd 54 46 31 36 42 45 29 3b 0a 7d 0a 53 51 4c 49 54  TF16BE);.}.SQLIT
1b6fe 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
1b6ff 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   *sqlite3_value_
1b700 74 65 78 74 31 36 6c 65 28 73 71 6c 69 74 65 33  text16le(sqlite3
1b701 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20  _value *pVal){. 
1b702 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56   return sqlite3V
1b703 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53  alueText(pVal, S
1b704 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 29 3b 0a  QLITE_UTF16LE);.
1b705 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1b706 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
1b707 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1b708 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
1b709 70 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  pe(sqlite3_value
1b70a 2a 20 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72  * pVal){.  retur
1b70b 6e 20 70 56 61 6c 2d 3e 74 79 70 65 3b 0a 7d 0a  n pVal->type;.}.
1b70c 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1b70d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73  ************** s
1b70e 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 20 20  qlite3_result_  
1b70f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1b711 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
1b712 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73   routines are us
1b713 65 64 20 62 79 20 75 73 65 72 2d 64 65 66 69 6e  ed by user-defin
1b714 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20  ed functions to 
1b715 73 70 65 63 69 66 79 0a 2a 2a 20 74 68 65 20 66  specify.** the f
1b716 75 6e 63 74 69 6f 6e 20 72 65 73 75 6c 74 2e 0a  unction result..
1b717 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 74 53 74 72  **.** The setStr
1b718 4f 72 45 72 72 6f 72 28 29 20 66 75 6e 74 69 6f  OrError() funtio
1b719 6e 20 63 61 6c 6c 73 20 73 71 6c 69 74 65 33 56  n calls sqlite3V
1b71a 64 62 65 4d 65 6d 53 65 74 53 74 72 28 29 20 74  dbeMemSetStr() t
1b71b 6f 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20 72  o store the.** r
1b71c 65 73 75 6c 74 20 61 73 20 61 20 73 74 72 69 6e  esult as a strin
1b71d 67 20 6f 72 20 62 6c 6f 62 20 62 75 74 20 69 66  g or blob but if
1b71e 20 74 68 65 20 73 74 72 69 6e 67 20 6f 72 20 62   the string or b
1b71f 6c 6f 62 20 69 73 20 74 6f 6f 20 6c 61 72 67 65  lob is too large
1b720 2c 20 69 74 0a 2a 2a 20 74 68 65 6e 20 73 65 74  , it.** then set
1b721 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  s the error code
1b722 20 74 6f 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49   to SQLITE_TOOBI
1b723 47 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  G.*/.static void
1b724 20 73 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45   setResultStrOrE
1b725 72 72 6f 72 28 0a 20 20 73 71 6c 69 74 65 33 5f  rror(.  sqlite3_
1b726 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 20  context *pCtx,  
1b727 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 63 6f 6e 74  /* Function cont
1b728 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ext */.  const c
1b729 68 61 72 20 2a 7a 2c 20 20 20 20 20 20 20 20 20  har *z,         
1b72a 20 2f 2a 20 53 74 72 69 6e 67 20 70 6f 69 6e 74   /* String point
1b72b 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c 20 20  er */.  int n,  
1b72c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b72d 2f 2a 20 42 79 74 65 73 20 69 6e 20 73 74 72 69  /* Bytes in stri
1b72e 6e 67 2c 20 6f 72 20 6e 65 67 61 74 69 76 65 20  ng, or negative 
1b72f 2a 2f 0a 20 20 75 38 20 65 6e 63 2c 20 20 20 20  */.  u8 enc,    
1b730 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1b731 45 6e 63 6f 64 69 6e 67 20 6f 66 20 7a 2e 20 20  Encoding of z.  
1b732 30 20 66 6f 72 20 42 4c 4f 42 73 20 2a 2f 0a 20  0 for BLOBs */. 
1b733 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
1b734 69 64 2a 29 20 20 20 20 20 2f 2a 20 44 65 73 74  id*)     /* Dest
1b735 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20  ructor function 
1b736 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 73 71 6c 69  */.){.  if( sqli
1b737 74 65 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72  te3VdbeMemSetStr
1b738 28 26 70 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c  (&pCtx->s, z, n,
1b739 20 65 6e 63 2c 20 78 44 65 6c 29 3d 3d 53 51 4c   enc, xDel)==SQL
1b73a 49 54 45 5f 54 4f 4f 42 49 47 20 29 7b 0a 20 20  ITE_TOOBIG ){.  
1b73b 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1b73c 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 70 43  _error_toobig(pC
1b73d 74 78 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54  tx);.  }.}.SQLIT
1b73e 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
1b73f 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 0a  e3_result_blob(.
1b740 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
1b741 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73  t *pCtx, .  cons
1b742 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e  t void *z, .  in
1b743 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78  t n, .  void (*x
1b744 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a  Del)(void *).){.
1b745 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30 20 29    assert( n>=0 )
1b746 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
1b747 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1b748 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78  Ctx->s.db->mutex
1b749 29 20 29 3b 0a 20 20 73 65 74 52 65 73 75 6c 74  ) );.  setResult
1b74a 53 74 72 4f 72 45 72 72 6f 72 28 70 43 74 78 2c  StrOrError(pCtx,
1b74b 20 7a 2c 20 6e 2c 20 30 2c 20 78 44 65 6c 29 3b   z, n, 0, xDel);
1b74c 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  .}.SQLITE_API vo
1b74d 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
1b74e 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33  t_double(sqlite3
1b74f 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
1b750 64 6f 75 62 6c 65 20 72 56 61 6c 29 7b 0a 20 20  double rVal){.  
1b751 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
1b752 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d  mutex_held(pCtx-
1b753 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  >s.db->mutex) );
1b754 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1b755 6d 53 65 74 44 6f 75 62 6c 65 28 26 70 43 74 78  mSetDouble(&pCtx
1b756 2d 3e 73 2c 20 72 56 61 6c 29 3b 0a 7d 0a 53 51  ->s, rVal);.}.SQ
1b757 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
1b758 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
1b759 6f 72 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  or(sqlite3_conte
1b75a 78 74 20 2a 70 43 74 78 2c 20 63 6f 6e 73 74 20  xt *pCtx, const 
1b75b 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b  char *z, int n){
1b75c 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1b75d 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
1b75e 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29  tx->s.db->mutex)
1b75f 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72   );.  pCtx->isEr
1b760 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f 45 52 52  ror = SQLITE_ERR
1b761 4f 52 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  OR;.  sqlite3Vdb
1b762 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78  eMemSetStr(&pCtx
1b763 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54  ->s, z, n, SQLIT
1b764 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 54  E_UTF8, SQLITE_T
1b765 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23 69 66  RANSIENT);.}.#if
1b766 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1b767 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50  _UTF16.SQLITE_AP
1b768 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
1b769 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28 73 71  esult_error16(sq
1b76a 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
1b76b 43 74 78 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  Ctx, const void 
1b76c 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 61 73  *z, int n){.  as
1b76d 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1b76e 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
1b76f 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
1b770 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d   pCtx->isError =
1b771 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1b772 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1b773 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20  etStr(&pCtx->s, 
1b774 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54 46  z, n, SQLITE_UTF
1b775 31 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45  16NATIVE, SQLITE
1b776 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 23  _TRANSIENT);.}.#
1b777 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 41 50 49  endif.SQLITE_API
1b778 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
1b779 73 75 6c 74 5f 69 6e 74 28 73 71 6c 69 74 65 33  sult_int(sqlite3
1b77a 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
1b77b 69 6e 74 20 69 56 61 6c 29 7b 0a 20 20 61 73 73  int iVal){.  ass
1b77c 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1b77d 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
1b77e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
1b77f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1b780 74 49 6e 74 36 34 28 26 70 43 74 78 2d 3e 73 2c  tInt64(&pCtx->s,
1b781 20 28 69 36 34 29 69 56 61 6c 29 3b 0a 7d 0a 53   (i64)iVal);.}.S
1b782 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
1b783 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
1b784 74 36 34 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  t64(sqlite3_cont
1b785 65 78 74 20 2a 70 43 74 78 2c 20 69 36 34 20 69  ext *pCtx, i64 i
1b786 56 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Val){.  assert( 
1b787 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1b788 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  ld(pCtx->s.db->m
1b789 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
1b78a 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36  e3VdbeMemSetInt6
1b78b 34 28 26 70 43 74 78 2d 3e 73 2c 20 69 56 61 6c  4(&pCtx->s, iVal
1b78c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  );.}.SQLITE_API 
1b78d 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73  void sqlite3_res
1b78e 75 6c 74 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33  ult_null(sqlite3
1b78f 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 29 7b  _context *pCtx){
1b790 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1b791 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
1b792 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29  tx->s.db->mutex)
1b793 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   );.  sqlite3Vdb
1b794 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26 70 43 74  eMemSetNull(&pCt
1b795 78 2d 3e 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  x->s);.}.SQLITE_
1b796 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
1b797 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 0a 20 20  _result_text(.  
1b798 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1b799 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20  *pCtx, .  const 
1b79a 63 68 61 72 20 2a 7a 2c 20 0a 20 20 69 6e 74 20  char *z, .  int 
1b79b 6e 2c 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c  n,.  void (*xDel
1b79c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61  )(void *).){.  a
1b79d 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
1b79e 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
1b79f 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
1b7a0 20 20 73 65 74 52 65 73 75 6c 74 53 74 72 4f 72    setResultStrOr
1b7a1 45 72 72 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e  Error(pCtx, z, n
1b7a2 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 78  , SQLITE_UTF8, x
1b7a3 44 65 6c 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20  Del);.}.#ifndef 
1b7a4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
1b7a5 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  6.SQLITE_API voi
1b7a6 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
1b7a7 5f 74 65 78 74 31 36 28 0a 20 20 73 71 6c 69 74  _text16(.  sqlit
1b7a8 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
1b7a9 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  , .  const void 
1b7aa 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20 0a 20  *z, .  int n, . 
1b7ab 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
1b7ac 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73 65 72  id *).){.  asser
1b7ad 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1b7ae 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
1b7af 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 65  ->mutex) );.  se
1b7b0 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72 72 6f  tResultStrOrErro
1b7b1 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20 53 51  r(pCtx, z, n, SQ
1b7b2 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
1b7b3 2c 20 78 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54  , xDel);.}.SQLIT
1b7b4 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
1b7b5 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36  e3_result_text16
1b7b6 62 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  be(.  sqlite3_co
1b7b7 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20  ntext *pCtx, .  
1b7b8 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a  const void *z, .
1b7b9 20 20 69 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64    int n, .  void
1b7ba 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29   (*xDel)(void *)
1b7bb 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  .){.  assert( sq
1b7bc 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1b7bd 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74  (pCtx->s.db->mut
1b7be 65 78 29 20 29 3b 0a 20 20 73 65 74 52 65 73 75  ex) );.  setResu
1b7bf 6c 74 53 74 72 4f 72 45 72 72 6f 72 28 70 43 74  ltStrOrError(pCt
1b7c0 78 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f  x, z, n, SQLITE_
1b7c1 55 54 46 31 36 42 45 2c 20 78 44 65 6c 29 3b 0a  UTF16BE, xDel);.
1b7c2 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  }.SQLITE_API voi
1b7c3 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
1b7c4 5f 74 65 78 74 31 36 6c 65 28 0a 20 20 73 71 6c  _text16le(.  sql
1b7c5 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
1b7c6 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69  tx, .  const voi
1b7c7 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20  d *z, .  int n, 
1b7c8 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28  .  void (*xDel)(
1b7c9 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61 73 73  void *).){.  ass
1b7ca 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1b7cb 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
1b7cc 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
1b7cd 73 65 74 52 65 73 75 6c 74 53 74 72 4f 72 45 72  setResultStrOrEr
1b7ce 72 6f 72 28 70 43 74 78 2c 20 7a 2c 20 6e 2c 20  ror(pCtx, z, n, 
1b7cf 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c 20  SQLITE_UTF16LE, 
1b7d0 78 44 65 6c 29 3b 0a 7d 0a 23 65 6e 64 69 66 20  xDel);.}.#endif 
1b7d1 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  /* SQLITE_OMIT_U
1b7d2 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  TF16 */.SQLITE_A
1b7d3 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
1b7d4 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 73 71 6c  result_value(sql
1b7d5 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
1b7d6 74 78 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  tx, sqlite3_valu
1b7d7 65 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 61 73  e *pValue){.  as
1b7d8 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
1b7d9 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73  tex_held(pCtx->s
1b7da 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
1b7db 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43   sqlite3VdbeMemC
1b7dc 6f 70 79 28 26 70 43 74 78 2d 3e 73 2c 20 70 56  opy(&pCtx->s, pV
1b7dd 61 6c 75 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  alue);.}.SQLITE_
1b7de 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
1b7df 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62  _result_zeroblob
1b7e0 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
1b7e1 20 2a 70 43 74 78 2c 20 69 6e 74 20 6e 29 7b 0a   *pCtx, int n){.
1b7e2 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1b7e3 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
1b7e4 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
1b7e5 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1b7e6 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62 28 26  MemSetZeroBlob(&
1b7e7 70 43 74 78 2d 3e 73 2c 20 6e 29 3b 0a 7d 0a 53  pCtx->s, n);.}.S
1b7e8 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
1b7e9 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
1b7ea 72 6f 72 5f 63 6f 64 65 28 73 71 6c 69 74 65 33  ror_code(sqlite3
1b7eb 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
1b7ec 69 6e 74 20 65 72 72 43 6f 64 65 29 7b 0a 20 20  int errCode){.  
1b7ed 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20  pCtx->isError = 
1b7ee 65 72 72 43 6f 64 65 3b 0a 20 20 69 66 28 20 70  errCode;.  if( p
1b7ef 43 74 78 2d 3e 73 2e 66 6c 61 67 73 20 26 20 4d  Ctx->s.flags & M
1b7f0 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73  EM_Null ){.    s
1b7f1 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
1b7f2 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20 73 71  Str(&pCtx->s, sq
1b7f3 6c 69 74 65 33 45 72 72 53 74 72 28 65 72 72 43  lite3ErrStr(errC
1b7f4 6f 64 65 29 2c 20 2d 31 2c 20 0a 20 20 20 20 20  ode), -1, .     
1b7f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b7f6 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c      SQLITE_UTF8,
1b7f7 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
1b7f8 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 46 6f 72 63 65  .  }.}../* Force
1b7f9 20 61 6e 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49   an SQLITE_TOOBI
1b7fa 47 20 65 72 72 6f 72 2e 20 2a 2f 0a 53 51 4c 49  G error. */.SQLI
1b7fb 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
1b7fc 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
1b7fd 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65 33 5f  _toobig(sqlite3_
1b7fe 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 29 7b 0a  context *pCtx){.
1b7ff 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1b800 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
1b801 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
1b802 29 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72  );.  pCtx->isErr
1b803 6f 72 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42  or = SQLITE_TOOB
1b804 49 47 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  IG;.  sqlite3Vdb
1b805 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78  eMemSetStr(&pCtx
1b806 2d 3e 73 2c 20 22 73 74 72 69 6e 67 20 6f 72 20  ->s, "string or 
1b807 62 6c 6f 62 20 74 6f 6f 20 62 69 67 22 2c 20 2d  blob too big", -
1b808 31 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  1, .            
1b809 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54             SQLIT
1b80a 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53  E_UTF8, SQLITE_S
1b80b 54 41 54 49 43 29 3b 0a 7d 0a 0a 2f 2a 20 41 6e  TATIC);.}../* An
1b80c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 65 72   SQLITE_NOMEM er
1b80d 72 6f 72 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41  ror. */.SQLITE_A
1b80e 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
1b80f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
1b810 65 6d 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  em(sqlite3_conte
1b811 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 73  xt *pCtx){.  ass
1b812 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1b813 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
1b814 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
1b815 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1b816 74 4e 75 6c 6c 28 26 70 43 74 78 2d 3e 73 29 3b  tNull(&pCtx->s);
1b817 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f 72  .  pCtx->isError
1b818 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
1b819 0a 20 20 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  .  pCtx->s.db->m
1b81a 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
1b81b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74  .}../*.** Execut
1b81c 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  e the statement 
1b81d 70 53 74 6d 74 2c 20 65 69 74 68 65 72 20 75 6e  pStmt, either un
1b81e 74 69 6c 20 61 20 72 6f 77 20 6f 66 20 64 61 74  til a row of dat
1b81f 61 20 69 73 20 72 65 61 64 79 2c 20 74 68 65 0a  a is ready, the.
1b820 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  ** statement is 
1b821 63 6f 6d 70 6c 65 74 65 6c 79 20 65 78 65 63 75  completely execu
1b822 74 65 64 20 6f 72 20 61 6e 20 65 72 72 6f 72 20  ted or an error 
1b823 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  occurs..**.** Th
1b824 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65  is routine imple
1b825 6d 65 6e 74 73 20 74 68 65 20 62 75 6c 6b 20 6f  ments the bulk o
1b826 66 20 74 68 65 20 6c 6f 67 69 63 20 62 65 68 69  f the logic behi
1b827 6e 64 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  nd the sqlite_st
1b828 65 70 28 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68  ep().** API.  Th
1b829 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 6f 6d 69  e only thing omi
1b82a 74 74 65 64 20 69 73 20 74 68 65 20 61 75 74 6f  tted is the auto
1b82b 6d 61 74 69 63 20 72 65 63 6f 6d 70 69 6c 65 20  matic recompile 
1b82c 69 66 20 61 20 0a 2a 2a 20 73 63 68 65 6d 61 20  if a .** schema 
1b82d 63 68 61 6e 67 65 20 68 61 73 20 6f 63 63 75 72  change has occur
1b82e 72 65 64 2e 20 20 54 68 61 74 20 64 65 74 61 69  red.  That detai
1b82f 6c 20 69 73 20 68 61 6e 64 6c 65 64 20 62 79 20  l is handled by 
1b830 74 68 65 0a 2a 2a 20 6f 75 74 65 72 20 73 71 6c  the.** outer sql
1b831 69 74 65 33 5f 73 74 65 70 28 29 20 77 72 61 70  ite3_step() wrap
1b832 70 65 72 20 70 72 6f 63 65 64 75 72 65 2e 0a 2a  per procedure..*
1b833 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
1b834 69 74 65 33 53 74 65 70 28 56 64 62 65 20 2a 70  ite3Step(Vdbe *p
1b835 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
1b836 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  ;.  int rc;..  a
1b837 73 73 65 72 74 28 70 29 3b 0a 20 20 69 66 28 20  ssert(p);.  if( 
1b838 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d  p->magic!=VDBE_M
1b839 41 47 49 43 5f 52 55 4e 20 29 7b 0a 20 20 20 20  AGIC_RUN ){.    
1b83a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
1b83b 53 55 53 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  SUSE;.  }..  /* 
1b83c 41 73 73 65 72 74 20 74 68 61 74 20 6d 61 6c 6c  Assert that mall
1b83d 6f 63 28 29 20 68 61 73 20 6e 6f 74 20 66 61 69  oc() has not fai
1b83e 6c 65 64 20 2a 2f 0a 20 20 64 62 20 3d 20 70 2d  led */.  db = p-
1b83f 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d  >db;.  if( db->m
1b840 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1b841 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1b842 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69  _NOMEM;.  }..  i
1b843 66 28 20 70 2d 3e 70 63 3c 3d 30 20 26 26 20 70  f( p->pc<=0 && p
1b844 2d 3e 65 78 70 69 72 65 64 20 29 7b 0a 20 20 20  ->expired ){.   
1b845 20 69 66 28 20 41 4c 57 41 59 53 28 70 2d 3e 72   if( ALWAYS(p->r
1b846 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
1b847 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43  p->rc==SQLITE_SC
1b848 48 45 4d 41 29 20 29 7b 0a 20 20 20 20 20 20 70  HEMA) ){.      p
1b849 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 53 43  ->rc = SQLITE_SC
1b84a 48 45 4d 41 3b 0a 20 20 20 20 7d 0a 20 20 20 20  HEMA;.    }.    
1b84b 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
1b84c 52 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  R;.    goto end_
1b84d 6f 66 5f 73 74 65 70 3b 0a 20 20 7d 0a 20 20 69  of_step;.  }.  i
1b84e 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1b84f 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 70 2d  On(db) ){.    p-
1b850 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53  >rc = SQLITE_MIS
1b851 55 53 45 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  USE;.    return 
1b852 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20  SQLITE_MISUSE;. 
1b853 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 63 3c 30   }.  if( p->pc<0
1b854 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   ){.    /* If th
1b855 65 72 65 20 61 72 65 20 6e 6f 20 6f 74 68 65 72  ere are no other
1b856 20 73 74 61 74 65 6d 65 6e 74 73 20 63 75 72 72   statements curr
1b857 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 2c 20 74  ently running, t
1b858 68 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 65 74  hen.    ** reset
1b859 20 74 68 65 20 69 6e 74 65 72 72 75 70 74 20 66   the interrupt f
1b85a 6c 61 67 2e 20 20 54 68 69 73 20 70 72 65 76 65  lag.  This preve
1b85b 6e 74 73 20 61 20 63 61 6c 6c 20 74 6f 20 73 71  nts a call to sq
1b85c 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 0a  lite3_interrupt.
1b85d 20 20 20 20 2a 2a 20 66 72 6f 6d 20 69 6e 74 65      ** from inte
1b85e 72 72 75 70 74 69 6e 67 20 61 20 73 74 61 74 65  rrupting a state
1b85f 6d 65 6e 74 20 74 68 61 74 20 68 61 73 20 6e 6f  ment that has no
1b860 74 20 79 65 74 20 73 74 61 72 74 65 64 2e 0a 20  t yet started.. 
1b861 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62     */.    if( db
1b862 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3d  ->activeVdbeCnt=
1b863 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e  =0 ){.      db->
1b864 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65 64  u1.isInterrupted
1b865 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20 20   = 0;.    }..   
1b866 20 61 73 73 65 72 74 28 20 64 62 2d 3e 77 72 69   assert( db->wri
1b867 74 65 56 64 62 65 43 6e 74 3e 30 20 7c 7c 20 64  teVdbeCnt>0 || d
1b868 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30  b->autoCommit==0
1b869 20 7c 7c 20 64 62 2d 3e 6e 44 65 66 65 72 72 65   || db->nDeferre
1b86a 64 43 6f 6e 73 3d 3d 30 20 29 3b 0a 0a 23 69 66  dCons==0 );..#if
1b86b 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1b86c 5f 54 52 41 43 45 0a 20 20 20 20 69 66 28 20 64  _TRACE.    if( d
1b86d 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26 26 20 21  b->xProfile && !
1b86e 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b  db->init.busy ){
1b86f 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 4e  .      double rN
1b870 6f 77 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ow;.      sqlite
1b871 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 64  3OsCurrentTime(d
1b872 62 2d 3e 70 56 66 73 2c 20 26 72 4e 6f 77 29 3b  b->pVfs, &rNow);
1b873 0a 20 20 20 20 20 20 70 2d 3e 73 74 61 72 74 54  .      p->startT
1b874 69 6d 65 20 3d 20 28 75 36 34 29 28 28 72 4e 6f  ime = (u64)((rNo
1b875 77 20 2d 20 28 69 6e 74 29 72 4e 6f 77 29 2a 33  w - (int)rNow)*3
1b876 36 30 30 2e 30 2a 32 34 2e 30 2a 31 30 30 30 30  600.0*24.0*10000
1b877 30 30 30 30 30 2e 30 29 3b 0a 20 20 20 20 7d 0a  00000.0);.    }.
1b878 23 65 6e 64 69 66 0a 0a 20 20 20 20 64 62 2d 3e  #endif..    db->
1b879 61 63 74 69 76 65 56 64 62 65 43 6e 74 2b 2b 3b  activeVdbeCnt++;
1b87a 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 65 61 64  .    if( p->read
1b87b 4f 6e 6c 79 3d 3d 30 20 29 20 64 62 2d 3e 77 72  Only==0 ) db->wr
1b87c 69 74 65 56 64 62 65 43 6e 74 2b 2b 3b 0a 20 20  iteVdbeCnt++;.  
1b87d 20 20 70 2d 3e 70 63 20 3d 20 30 3b 0a 20 20 7d    p->pc = 0;.  }
1b87e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1b87f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 69  OMIT_EXPLAIN.  i
1b880 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 20 29 7b  f( p->explain ){
1b881 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1b882 33 56 64 62 65 4c 69 73 74 28 70 29 3b 0a 20 20  3VdbeList(p);.  
1b883 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20  }else.#endif /* 
1b884 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
1b885 41 49 4e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 72  AIN */.  {.    r
1b886 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 45  c = sqlite3VdbeE
1b887 78 65 63 28 70 29 3b 0a 20 20 7d 0a 0a 20 20 69  xec(p);.  }..  i
1b888 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1b889 4f 66 66 28 64 62 29 20 29 7b 0a 20 20 20 20 72  Off(db) ){.    r
1b88a 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  c = SQLITE_MISUS
1b88b 45 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20  E;.  }..#ifndef 
1b88c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
1b88d 45 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68  E.  /* Invoke th
1b88e 65 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61  e profile callba
1b88f 63 6b 20 69 66 20 74 68 65 72 65 20 69 73 20 6f  ck if there is o
1b890 6e 65 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  ne.  */.  if( rc
1b891 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20  !=SQLITE_ROW && 
1b892 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 26 26 20  db->xProfile && 
1b893 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26  !db->init.busy &
1b894 26 20 70 2d 3e 7a 53 71 6c 20 29 7b 0a 20 20 20  & p->zSql ){.   
1b895 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20 20   double rNow;.  
1b896 20 20 75 36 34 20 65 6c 61 70 73 65 54 69 6d 65    u64 elapseTime
1b897 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73  ;..    sqlite3Os
1b898 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e  CurrentTime(db->
1b899 70 56 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20  pVfs, &rNow);.  
1b89a 20 20 65 6c 61 70 73 65 54 69 6d 65 20 3d 20 28    elapseTime = (
1b89b 75 36 34 29 28 28 72 4e 6f 77 20 2d 20 28 69 6e  u64)((rNow - (in
1b89c 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e 30 2a 32  t)rNow)*3600.0*2
1b89d 34 2e 30 2a 31 30 30 30 30 30 30 30 30 30 2e 30  4.0*1000000000.0
1b89e 29 3b 0a 20 20 20 20 65 6c 61 70 73 65 54 69 6d  );.    elapseTim
1b89f 65 20 2d 3d 20 70 2d 3e 73 74 61 72 74 54 69 6d  e -= p->startTim
1b8a0 65 3b 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f 66  e;.    db->xProf
1b8a1 69 6c 65 28 64 62 2d 3e 70 50 72 6f 66 69 6c 65  ile(db->pProfile
1b8a2 41 72 67 2c 20 70 2d 3e 7a 53 71 6c 2c 20 65 6c  Arg, p->zSql, el
1b8a3 61 70 73 65 54 69 6d 65 29 3b 0a 20 20 7d 0a 23  apseTime);.  }.#
1b8a4 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 65 72 72  endif..  db->err
1b8a5 43 6f 64 65 20 3d 20 72 63 3b 0a 20 20 69 66 28  Code = rc;.  if(
1b8a6 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73   SQLITE_NOMEM==s
1b8a7 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 2d  qlite3ApiExit(p-
1b8a8 3e 64 62 2c 20 70 2d 3e 72 63 29 20 29 7b 0a 20  >db, p->rc) ){. 
1b8a9 20 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54     p->rc = SQLIT
1b8aa 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 65 6e 64  E_NOMEM;.  }.end
1b8ab 5f 6f 66 5f 73 74 65 70 3a 0a 20 20 2f 2a 20 41  _of_step:.  /* A
1b8ac 74 20 74 68 69 73 20 70 6f 69 6e 74 20 6c 6f 63  t this point loc
1b8ad 61 6c 20 76 61 72 69 61 62 6c 65 20 72 63 20 68  al variable rc h
1b8ae 6f 6c 64 73 20 74 68 65 20 76 61 6c 75 65 20 74  olds the value t
1b8af 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 0a 20  hat should be . 
1b8b0 20 2a 2a 20 72 65 74 75 72 6e 65 64 20 69 66 20   ** returned if 
1b8b1 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 77  this statement w
1b8b2 61 73 20 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e  as compiled usin
1b8b3 67 20 74 68 65 20 6c 65 67 61 63 79 20 0a 20 20  g the legacy .  
1b8b4 2a 2a 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  ** sqlite3_prepa
1b8b5 72 65 28 29 20 69 6e 74 65 72 66 61 63 65 2e 20  re() interface. 
1b8b6 41 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65  According to the
1b8b7 20 64 6f 63 73 2c 20 74 68 69 73 20 63 61 6e 20   docs, this can 
1b8b8 6f 6e 6c 79 0a 20 20 2a 2a 20 62 65 20 6f 6e 65  only.  ** be one
1b8b9 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69   of the values i
1b8ba 6e 20 74 68 65 20 66 69 72 73 74 20 61 73 73 65  n the first asse
1b8bb 72 74 28 29 20 62 65 6c 6f 77 2e 20 56 61 72 69  rt() below. Vari
1b8bc 61 62 6c 65 20 70 2d 3e 72 63 20 0a 20 20 2a 2a  able p->rc .  **
1b8bd 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 76 61   contains the va
1b8be 6c 75 65 20 74 68 61 74 20 77 6f 75 6c 64 20 62  lue that would b
1b8bf 65 20 72 65 74 75 72 6e 65 64 20 69 66 20 73 71  e returned if sq
1b8c0 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
1b8c1 20 0a 20 20 2a 2a 20 77 65 72 65 20 63 61 6c 6c   .  ** were call
1b8c2 65 64 20 6f 6e 20 73 74 61 74 65 6d 65 6e 74 20  ed on statement 
1b8c3 70 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  p..  */.  assert
1b8c4 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc==SQLITE_ROW
1b8c5 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f    || rc==SQLITE_
1b8c6 44 4f 4e 45 20 20 20 7c 7c 20 72 63 3d 3d 53 51  DONE   || rc==SQ
1b8c7 4c 49 54 45 5f 45 52 52 4f 52 20 0a 20 20 20 20  LITE_ERROR .    
1b8c8 20 20 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45     || rc==SQLITE
1b8c9 5f 42 55 53 59 20 7c 7c 20 72 63 3d 3d 53 51 4c  _BUSY || rc==SQL
1b8ca 49 54 45 5f 4d 49 53 55 53 45 0a 20 20 29 3b 0a  ITE_MISUSE.  );.
1b8cb 20 20 61 73 73 65 72 74 28 20 70 2d 3e 72 63 21    assert( p->rc!
1b8cc 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 70  =SQLITE_ROW && p
1b8cd 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  ->rc!=SQLITE_DON
1b8ce 45 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 73  E );.  if( p->is
1b8cf 50 72 65 70 61 72 65 56 32 20 26 26 20 72 63 21  PrepareV2 && rc!
1b8d0 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26 20 72  =SQLITE_ROW && r
1b8d1 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c!=SQLITE_DONE )
1b8d2 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73  {.    /* If this
1b8d3 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 70   statement was p
1b8d4 72 65 70 61 72 65 64 20 75 73 69 6e 67 20 73 71  repared using sq
1b8d5 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1b8d6 28 29 2c 20 61 6e 64 20 61 6e 0a 20 20 20 20 2a  (), and an.    *
1b8d7 2a 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75  * error has occu
1b8d8 72 65 64 2c 20 74 68 65 6e 20 72 65 74 75 72 6e  red, then return
1b8d9 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20   the error code 
1b8da 69 6e 20 70 2d 3e 72 63 20 74 6f 20 74 68 65 0a  in p->rc to the.
1b8db 20 20 20 20 2a 2a 20 63 61 6c 6c 65 72 2e 20 53      ** caller. S
1b8dc 65 74 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  et the error cod
1b8dd 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
1b8de 65 20 68 61 6e 64 6c 65 20 74 6f 20 74 68 65 20  e handle to the 
1b8df 73 61 6d 65 20 76 61 6c 75 65 2e 0a 20 20 20 20  same value..    
1b8e0 2a 2f 20 0a 20 20 20 20 72 63 20 3d 20 64 62 2d  */ .    rc = db-
1b8e1 3e 65 72 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63  >errCode = p->rc
1b8e2 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  ;.  }.  return (
1b8e3 72 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3b  rc&db->errMask);
1b8e4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69  .}../*.** This i
1b8e5 73 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20  s the top-level 
1b8e6 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
1b8e7 66 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  f sqlite3_step()
1b8e8 2e 20 20 43 61 6c 6c 0a 2a 2a 20 73 71 6c 69 74  .  Call.** sqlit
1b8e9 65 33 53 74 65 70 28 29 20 74 6f 20 64 6f 20 6d  e3Step() to do m
1b8ea 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72 6b 2e  ost of the work.
1b8eb 20 20 49 66 20 61 20 73 63 68 65 6d 61 20 65 72    If a schema er
1b8ec 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 63  ror occurs,.** c
1b8ed 61 6c 6c 20 73 71 6c 69 74 65 33 52 65 70 72 65  all sqlite3Repre
1b8ee 70 61 72 65 28 29 20 61 6e 64 20 74 72 79 20 61  pare() and try a
1b8ef 67 61 69 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  gain..*/.SQLITE_
1b8f0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1b8f1 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74 6d  step(sqlite3_stm
1b8f2 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 69 6e 74  t *pStmt){.  int
1b8f3 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53   rc = SQLITE_MIS
1b8f4 55 53 45 3b 0a 20 20 69 66 28 20 70 53 74 6d 74  USE;.  if( pStmt
1b8f5 20 29 7b 0a 20 20 20 20 69 6e 74 20 63 6e 74 20   ){.    int cnt 
1b8f6 3d 20 30 3b 0a 20 20 20 20 56 64 62 65 20 2a 76  = 0;.    Vdbe *v
1b8f7 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b   = (Vdbe*)pStmt;
1b8f8 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
1b8f9 20 3d 20 76 2d 3e 64 62 3b 0a 20 20 20 20 73 71   = v->db;.    sq
1b8fa 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
1b8fb 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
1b8fc 20 20 77 68 69 6c 65 28 20 28 72 63 20 3d 20 73    while( (rc = s
1b8fd 71 6c 69 74 65 33 53 74 65 70 28 76 29 29 3d 3d  qlite3Step(v))==
1b8fe 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 0a 20 20  SQLITE_SCHEMA.  
1b8ff 20 20 20 20 20 20 20 20 20 26 26 20 63 6e 74 2b           && cnt+
1b900 2b 20 3c 20 35 0a 20 20 20 20 20 20 20 20 20 20  + < 5.          
1b901 20 26 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65   && (rc = sqlite
1b902 33 52 65 70 72 65 70 61 72 65 28 76 29 29 3d 3d  3Reprepare(v))==
1b903 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1b904 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74     sqlite3_reset
1b905 28 70 53 74 6d 74 29 3b 0a 20 20 20 20 20 20 76  (pStmt);.      v
1b906 2d 3e 65 78 70 69 72 65 64 20 3d 20 30 3b 0a 20  ->expired = 0;. 
1b907 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d     }.    if( rc=
1b908 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 26  =SQLITE_SCHEMA &
1b909 26 20 41 4c 57 41 59 53 28 76 2d 3e 69 73 50 72  & ALWAYS(v->isPr
1b90a 65 70 61 72 65 56 32 29 20 26 26 20 41 4c 57 41  epareV2) && ALWA
1b90b 59 53 28 64 62 2d 3e 70 45 72 72 29 20 29 7b 0a  YS(db->pErr) ){.
1b90c 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61        /* This ca
1b90d 73 65 20 6f 63 63 75 72 73 20 61 66 74 65 72 20  se occurs after 
1b90e 66 61 69 6c 69 6e 67 20 74 6f 20 72 65 63 6f 6d  failing to recom
1b90f 70 69 6c 65 20 61 6e 20 73 71 6c 20 73 74 61 74  pile an sql stat
1b910 65 6d 65 6e 74 2e 20 0a 20 20 20 20 20 20 2a 2a  ement. .      **
1b911 20 54 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   The error messa
1b912 67 65 20 66 72 6f 6d 20 74 68 65 20 53 51 4c 20  ge from the SQL 
1b913 63 6f 6d 70 69 6c 65 72 20 68 61 73 20 61 6c 72  compiler has alr
1b914 65 61 64 79 20 62 65 65 6e 20 6c 6f 61 64 65 64  eady been loaded
1b915 20 0a 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20   .      ** into 
1b916 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e  the database han
1b917 64 6c 65 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20  dle. This block 
1b918 63 6f 70 69 65 73 20 74 68 65 20 65 72 72 6f 72  copies the error
1b919 20 6d 65 73 73 61 67 65 20 0a 20 20 20 20 20 20   message .      
1b91a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ** from the data
1b91b 62 61 73 65 20 68 61 6e 64 6c 65 20 69 6e 74 6f  base handle into
1b91c 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 61   the statement a
1b91d 6e 64 20 73 65 74 73 20 74 68 65 20 73 74 61 74  nd sets the stat
1b91e 65 6d 65 6e 74 0a 20 20 20 20 20 20 2a 2a 20 70  ement.      ** p
1b91f 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 74  rogram counter t
1b920 6f 20 30 20 74 6f 20 65 6e 73 75 72 65 20 74 68  o 0 to ensure th
1b921 61 74 20 77 68 65 6e 20 74 68 65 20 73 74 61 74  at when the stat
1b922 65 6d 65 6e 74 20 69 73 20 0a 20 20 20 20 20 20  ement is .      
1b923 2a 2a 20 66 69 6e 61 6c 69 7a 65 64 20 6f 72 20  ** finalized or 
1b924 72 65 73 65 74 20 74 68 65 20 70 61 72 73 65 72  reset the parser
1b925 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
1b926 73 20 61 76 61 69 6c 61 62 6c 65 20 76 69 61 0a  s available via.
1b927 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33        ** sqlite3
1b928 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20 73 71  _errmsg() and sq
1b929 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 2e  lite3_errcode().
1b92a 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
1b92b 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 72  const char *zErr
1b92c 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
1b92d 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
1b92e 65 78 74 28 64 62 2d 3e 70 45 72 72 29 3b 20 0a  ext(db->pErr); .
1b92f 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
1b930 72 65 65 28 64 62 2c 20 76 2d 3e 7a 45 72 72 4d  ree(db, v->zErrM
1b931 73 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  sg);.      if( !
1b932 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1b933 20 29 7b 0a 20 20 20 20 20 20 20 20 76 2d 3e 7a   ){.        v->z
1b934 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33  ErrMsg = sqlite3
1b935 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a 45 72  DbStrDup(db, zEr
1b936 72 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65  r);.      } else
1b937 20 7b 0a 20 20 20 20 20 20 20 20 76 2d 3e 7a 45   {.        v->zE
1b938 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 20  rrMsg = 0;.     
1b939 20 20 20 76 2d 3e 72 63 20 3d 20 53 51 4c 49 54     v->rc = SQLIT
1b93a 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
1b93b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
1b93c 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
1b93d 62 2c 20 72 63 29 3b 0a 20 20 20 20 73 71 6c 69  b, rc);.    sqli
1b93e 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
1b93f 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a  db->mutex);.  }.
1b940 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1b941 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68  /*.** Extract th
1b942 65 20 75 73 65 72 20 64 61 74 61 20 66 72 6f 6d  e user data from
1b943 20 61 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   a sqlite3_conte
1b944 78 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64  xt structure and
1b945 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20 70 6f 69   return a.** poi
1b946 6e 74 65 72 20 74 6f 20 69 74 2e 0a 2a 2f 0a 53  nter to it..*/.S
1b947 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
1b948 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
1b949 61 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  a(sqlite3_contex
1b94a 74 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  t *p){.  assert(
1b94b 20 70 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 29   p && p->pFunc )
1b94c 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 46  ;.  return p->pF
1b94d 75 6e 63 2d 3e 70 55 73 65 72 44 61 74 61 3b 0a  unc->pUserData;.
1b94e 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74  }../*.** Extract
1b94f 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20 66   the user data f
1b950 72 6f 6d 20 61 20 73 71 6c 69 74 65 33 5f 63 6f  rom a sqlite3_co
1b951 6e 74 65 78 74 20 73 74 72 75 63 74 75 72 65 20  ntext structure 
1b952 61 6e 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20  and return a.** 
1b953 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 0a 2a  pointer to it..*
1b954 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  /.SQLITE_API sql
1b955 69 74 65 33 20 2a 73 71 6c 69 74 65 33 5f 63 6f  ite3 *sqlite3_co
1b956 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
1b957 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
1b958 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
1b959 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 29 3b 0a   && p->pFunc );.
1b95a 20 20 72 65 74 75 72 6e 20 70 2d 3e 73 2e 64 62    return p->s.db
1b95b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  ;.}../*.** The f
1b95c 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 68 65 20  ollowing is the 
1b95d 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
1b95e 66 20 61 6e 20 53 51 4c 20 66 75 6e 63 74 69 6f  f an SQL functio
1b95f 6e 20 74 68 61 74 20 61 6c 77 61 79 73 0a 2a 2a  n that always.**
1b960 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 20 65   fails with an e
1b961 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 61  rror message sta
1b962 74 69 6e 67 20 74 68 61 74 20 74 68 65 20 66 75  ting that the fu
1b963 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69  nction is used i
1b964 6e 20 74 68 65 0a 2a 2a 20 77 72 6f 6e 67 20 63  n the.** wrong c
1b965 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 73 71 6c  ontext.  The sql
1b966 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75  ite3_overload_fu
1b967 6e 63 74 69 6f 6e 28 29 20 41 50 49 20 6d 69 67  nction() API mig
1b968 68 74 20 63 6f 6e 73 74 72 75 63 74 0a 2a 2a 20  ht construct.** 
1b969 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  SQL function tha
1b96a 74 20 75 73 65 20 74 68 69 73 20 72 6f 75 74 69  t use this routi
1b96b 6e 65 20 73 6f 20 74 68 61 74 20 74 68 65 20 66  ne so that the f
1b96c 75 6e 63 74 69 6f 6e 73 20 77 69 6c 6c 20 65 78  unctions will ex
1b96d 69 73 74 0a 2a 2a 20 66 6f 72 20 6e 61 6d 65 20  ist.** for name 
1b96e 72 65 73 6f 6c 75 74 69 6f 6e 20 62 75 74 20 61  resolution but a
1b96f 72 65 20 61 63 74 75 61 6c 6c 79 20 6f 76 65 72  re actually over
1b970 6c 6f 61 64 65 64 20 62 79 20 74 68 65 20 78 46  loaded by the xF
1b971 69 6e 64 46 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d  indFunction.** m
1b972 65 74 68 6f 64 20 6f 66 20 76 69 72 74 75 61 6c  ethod of virtual
1b973 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49   tables..*/.SQLI
1b974 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1b975 73 71 6c 69 74 65 33 49 6e 76 61 6c 69 64 46 75  sqlite3InvalidFu
1b976 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65  nction(.  sqlite
1b977 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
1b978 78 74 2c 20 20 2f 2a 20 54 68 65 20 66 75 6e 63  xt,  /* The func
1b979 74 69 6f 6e 20 63 61 6c 6c 69 6e 67 20 63 6f 6e  tion calling con
1b97a 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 4e 6f  text */.  int No
1b97b 74 55 73 65 64 2c 20 20 20 20 20 20 20 20 20 20  tUsed,          
1b97c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1b97d 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74  f arguments to t
1b97e 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  he function */. 
1b97f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
1b980 2a 4e 6f 74 55 73 65 64 32 20 20 20 2f 2a 20 56  *NotUsed2   /* V
1b981 61 6c 75 65 20 6f 66 20 65 61 63 68 20 61 72 67  alue of each arg
1b982 75 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 63 6f  ument */.){.  co
1b983 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20  nst char *zName 
1b984 3d 20 63 6f 6e 74 65 78 74 2d 3e 70 46 75 6e 63  = context->pFunc
1b985 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 63 68 61 72 20  ->zName;.  char 
1b986 2a 7a 45 72 72 3b 0a 20 20 55 4e 55 53 45 44 5f  *zErr;.  UNUSED_
1b987 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73  PARAMETER2(NotUs
1b988 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20  ed, NotUsed2);. 
1b989 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f   zErr = sqlite3_
1b98a 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 20 20 22  mprintf(.      "
1b98b 75 6e 61 62 6c 65 20 74 6f 20 75 73 65 20 66 75  unable to use fu
1b98c 6e 63 74 69 6f 6e 20 25 73 20 69 6e 20 74 68 65  nction %s in the
1b98d 20 72 65 71 75 65 73 74 65 64 20 63 6f 6e 74 65   requested conte
1b98e 78 74 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 73  xt", zName);.  s
1b98f 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
1b990 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72  ror(context, zEr
1b991 72 2c 20 2d 31 29 3b 0a 20 20 73 71 6c 69 74 65  r, -1);.  sqlite
1b992 33 5f 66 72 65 65 28 7a 45 72 72 29 3b 0a 7d 0a  3_free(zErr);.}.
1b993 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
1b994 6f 72 20 72 65 74 75 72 6e 20 74 68 65 20 61 67  or return the ag
1b995 67 72 65 67 61 74 65 20 63 6f 6e 74 65 78 74 20  gregate context 
1b996 66 6f 72 20 61 20 75 73 65 72 20 66 75 6e 63 74  for a user funct
1b997 69 6f 6e 2e 20 20 41 20 6e 65 77 0a 2a 2a 20 63  ion.  A new.** c
1b998 6f 6e 74 65 78 74 20 69 73 20 61 6c 6c 6f 63 61  ontext is alloca
1b999 74 65 64 20 6f 6e 20 74 68 65 20 66 69 72 73 74  ted on the first
1b99a 20 63 61 6c 6c 2e 20 20 53 75 62 73 65 71 75 65   call.  Subseque
1b99b 6e 74 20 63 61 6c 6c 73 20 72 65 74 75 72 6e 20  nt calls return 
1b99c 74 68 65 0a 2a 2a 20 73 61 6d 65 20 63 6f 6e 74  the.** same cont
1b99d 65 78 74 20 74 68 61 74 20 77 61 73 20 72 65 74  ext that was ret
1b99e 75 72 6e 65 64 20 6f 6e 20 70 72 69 6f 72 20 63  urned on prior c
1b99f 61 6c 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  alls..*/.SQLITE_
1b9a0 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  API void *sqlite
1b9a1 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74  3_aggregate_cont
1b9a2 65 78 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ext(sqlite3_cont
1b9a3 65 78 74 20 2a 70 2c 20 69 6e 74 20 6e 42 79 74  ext *p, int nByt
1b9a4 65 29 7b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b  e){.  Mem *pMem;
1b9a5 0a 20 20 61 73 73 65 72 74 28 20 70 20 26 26 20  .  assert( p && 
1b9a6 70 2d 3e 70 46 75 6e 63 20 26 26 20 70 2d 3e 70  p->pFunc && p->p
1b9a7 46 75 6e 63 2d 3e 78 53 74 65 70 20 29 3b 0a 20  Func->xStep );. 
1b9a8 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1b9a9 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 73  _mutex_held(p->s
1b9aa 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  .db->mutex) );. 
1b9ab 20 70 4d 65 6d 20 3d 20 70 2d 3e 70 4d 65 6d 3b   pMem = p->pMem;
1b9ac 0a 20 20 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c  .  if( (pMem->fl
1b9ad 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d 3d  ags & MEM_Agg)==
1b9ae 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 42 79  0 ){.    if( nBy
1b9af 74 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  te==0 ){.      s
1b9b0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1b9b1 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 4d 65  easeExternal(pMe
1b9b2 6d 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  m);.      pMem->
1b9b3 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
1b9b4 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20  ;.      pMem->z 
1b9b5 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 0;.    }else{.
1b9b6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1b9b7 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e  eMemGrow(pMem, n
1b9b8 42 79 74 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Byte, 0);.      
1b9b9 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
1b9ba 4d 5f 41 67 67 3b 0a 20 20 20 20 20 20 70 4d 65  M_Agg;.      pMe
1b9bb 6d 2d 3e 75 2e 70 44 65 66 20 3d 20 70 2d 3e 70  m->u.pDef = p->p
1b9bc 46 75 6e 63 3b 0a 20 20 20 20 20 20 69 66 28 20  Func;.      if( 
1b9bd 70 4d 65 6d 2d 3e 7a 20 29 7b 0a 20 20 20 20 20  pMem->z ){.     
1b9be 20 20 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e     memset(pMem->
1b9bf 7a 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20  z, 0, nByte);.  
1b9c0 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1b9c1 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29    return (void*)
1b9c2 70 4d 65 6d 2d 3e 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a  pMem->z;.}../*.*
1b9c3 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61 75 78  * Return the aux
1b9c4 69 6c 61 72 79 20 64 61 74 61 20 70 6f 69 6e 74  ilary data point
1b9c5 65 72 2c 20 69 66 20 61 6e 79 2c 20 66 6f 72 20  er, if any, for 
1b9c6 74 68 65 20 69 41 72 67 27 74 68 20 61 72 67 75  the iArg'th argu
1b9c7 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 75  ment to.** the u
1b9c8 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65 66  ser-function def
1b9c9 69 6e 65 64 20 62 79 20 70 43 74 78 2e 0a 2a 2f  ined by pCtx..*/
1b9ca 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
1b9cb 20 2a 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75   *sqlite3_get_au
1b9cc 78 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63 6f  xdata(sqlite3_co
1b9cd 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 6e 74  ntext *pCtx, int
1b9ce 20 69 41 72 67 29 7b 0a 20 20 56 64 62 65 46 75   iArg){.  VdbeFu
1b9cf 6e 63 20 2a 70 56 64 62 65 46 75 6e 63 3b 0a 0a  nc *pVdbeFunc;..
1b9d0 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1b9d1 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
1b9d2 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
1b9d3 29 3b 0a 20 20 70 56 64 62 65 46 75 6e 63 20 3d  );.  pVdbeFunc =
1b9d4 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63   pCtx->pVdbeFunc
1b9d5 3b 0a 20 20 69 66 28 20 21 70 56 64 62 65 46 75  ;.  if( !pVdbeFu
1b9d6 6e 63 20 7c 7c 20 69 41 72 67 3e 3d 70 56 64 62  nc || iArg>=pVdb
1b9d7 65 46 75 6e 63 2d 3e 6e 41 75 78 20 7c 7c 20 69  eFunc->nAux || i
1b9d8 41 72 67 3c 30 20 29 7b 0a 20 20 20 20 72 65 74  Arg<0 ){.    ret
1b9d9 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  urn 0;.  }.  ret
1b9da 75 72 6e 20 70 56 64 62 65 46 75 6e 63 2d 3e 61  urn pVdbeFunc->a
1b9db 70 41 75 78 5b 69 41 72 67 5d 2e 70 41 75 78 3b  pAux[iArg].pAux;
1b9dc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
1b9dd 65 20 61 75 78 69 6c 61 72 79 20 64 61 74 61 20  e auxilary data 
1b9de 70 6f 69 6e 74 65 72 20 61 6e 64 20 64 65 6c 65  pointer and dele
1b9df 74 65 20 66 75 6e 63 74 69 6f 6e 2c 20 66 6f 72  te function, for
1b9e0 20 74 68 65 20 69 41 72 67 27 74 68 0a 2a 2a 20   the iArg'th.** 
1b9e1 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
1b9e2 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 65  user-function de
1b9e3 66 69 6e 65 64 20 62 79 20 70 43 74 78 2e 20 41  fined by pCtx. A
1b9e4 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75  ny previous valu
1b9e5 65 20 69 73 0a 2a 2a 20 64 65 6c 65 74 65 64 20  e is.** deleted 
1b9e6 62 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 64  by calling the d
1b9e7 65 6c 65 74 65 20 66 75 6e 63 74 69 6f 6e 20 73  elete function s
1b9e8 70 65 63 69 66 69 65 64 20 77 68 65 6e 20 69 74  pecified when it
1b9e9 20 77 61 73 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c   was set..*/.SQL
1b9ea 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
1b9eb 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
1b9ec 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
1b9ed 65 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 69 6e  ext *pCtx, .  in
1b9ee 74 20 69 41 72 67 2c 20 0a 20 20 76 6f 69 64 20  t iArg, .  void 
1b9ef 2a 70 41 75 78 2c 20 0a 20 20 76 6f 69 64 20 28  *pAux, .  void (
1b9f0 2a 78 44 65 6c 65 74 65 29 28 76 6f 69 64 2a 29  *xDelete)(void*)
1b9f1 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 41 75 78  .){.  struct Aux
1b9f2 44 61 74 61 20 2a 70 41 75 78 44 61 74 61 3b 0a  Data *pAuxData;.
1b9f3 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62    VdbeFunc *pVdb
1b9f4 65 46 75 6e 63 3b 0a 20 20 69 66 28 20 69 41 72  eFunc;.  if( iAr
1b9f5 67 3c 30 20 29 20 67 6f 74 6f 20 66 61 69 6c 65  g<0 ) goto faile
1b9f6 64 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  d;..  assert( sq
1b9f7 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1b9f8 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74  (pCtx->s.db->mut
1b9f9 65 78 29 20 29 3b 0a 20 20 70 56 64 62 65 46 75  ex) );.  pVdbeFu
1b9fa 6e 63 20 3d 20 70 43 74 78 2d 3e 70 56 64 62 65  nc = pCtx->pVdbe
1b9fb 46 75 6e 63 3b 0a 20 20 69 66 28 20 21 70 56 64  Func;.  if( !pVd
1b9fc 62 65 46 75 6e 63 20 7c 7c 20 70 56 64 62 65 46  beFunc || pVdbeF
1b9fd 75 6e 63 2d 3e 6e 41 75 78 3c 3d 69 41 72 67 20  unc->nAux<=iArg 
1b9fe 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 75 78 20  ){.    int nAux 
1b9ff 3d 20 28 70 56 64 62 65 46 75 6e 63 20 3f 20 70  = (pVdbeFunc ? p
1ba00 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78 20 3a  VdbeFunc->nAux :
1ba01 20 30 29 3b 0a 20 20 20 20 69 6e 74 20 6e 4d 61   0);.    int nMa
1ba02 6c 6c 6f 63 20 3d 20 73 69 7a 65 6f 66 28 56 64  lloc = sizeof(Vd
1ba03 62 65 46 75 6e 63 29 20 2b 20 73 69 7a 65 6f 66  beFunc) + sizeof
1ba04 28 73 74 72 75 63 74 20 41 75 78 44 61 74 61 29  (struct AuxData)
1ba05 2a 69 41 72 67 3b 0a 20 20 20 20 70 56 64 62 65  *iArg;.    pVdbe
1ba06 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 44 62  Func = sqlite3Db
1ba07 52 65 61 6c 6c 6f 63 28 70 43 74 78 2d 3e 73 2e  Realloc(pCtx->s.
1ba08 64 62 2c 20 70 56 64 62 65 46 75 6e 63 2c 20 6e  db, pVdbeFunc, n
1ba09 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 69 66 28  Malloc);.    if(
1ba0a 20 21 70 56 64 62 65 46 75 6e 63 20 29 7b 0a 20   !pVdbeFunc ){. 
1ba0b 20 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64       goto failed
1ba0c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 74 78  ;.    }.    pCtx
1ba0d 2d 3e 70 56 64 62 65 46 75 6e 63 20 3d 20 70 56  ->pVdbeFunc = pV
1ba0e 64 62 65 46 75 6e 63 3b 0a 20 20 20 20 6d 65 6d  dbeFunc;.    mem
1ba0f 73 65 74 28 26 70 56 64 62 65 46 75 6e 63 2d 3e  set(&pVdbeFunc->
1ba10 61 70 41 75 78 5b 6e 41 75 78 5d 2c 20 30 2c 20  apAux[nAux], 0, 
1ba11 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 41 75  sizeof(struct Au
1ba12 78 44 61 74 61 29 2a 28 69 41 72 67 2b 31 2d 6e  xData)*(iArg+1-n
1ba13 41 75 78 29 29 3b 0a 20 20 20 20 70 56 64 62 65  Aux));.    pVdbe
1ba14 46 75 6e 63 2d 3e 6e 41 75 78 20 3d 20 69 41 72  Func->nAux = iAr
1ba15 67 2b 31 3b 0a 20 20 20 20 70 56 64 62 65 46 75  g+1;.    pVdbeFu
1ba16 6e 63 2d 3e 70 46 75 6e 63 20 3d 20 70 43 74 78  nc->pFunc = pCtx
1ba17 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20  ->pFunc;.  }..  
1ba18 70 41 75 78 44 61 74 61 20 3d 20 26 70 56 64 62  pAuxData = &pVdb
1ba19 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 69 41 72  eFunc->apAux[iAr
1ba1a 67 5d 3b 0a 20 20 69 66 28 20 70 41 75 78 44 61  g];.  if( pAuxDa
1ba1b 74 61 2d 3e 70 41 75 78 20 26 26 20 70 41 75 78  ta->pAux && pAux
1ba1c 44 61 74 61 2d 3e 78 44 65 6c 65 74 65 20 29 7b  Data->xDelete ){
1ba1d 0a 20 20 20 20 70 41 75 78 44 61 74 61 2d 3e 78  .    pAuxData->x
1ba1e 44 65 6c 65 74 65 28 70 41 75 78 44 61 74 61 2d  Delete(pAuxData-
1ba1f 3e 70 41 75 78 29 3b 0a 20 20 7d 0a 20 20 70 41  >pAux);.  }.  pA
1ba20 75 78 44 61 74 61 2d 3e 70 41 75 78 20 3d 20 70  uxData->pAux = p
1ba21 41 75 78 3b 0a 20 20 70 41 75 78 44 61 74 61 2d  Aux;.  pAuxData-
1ba22 3e 78 44 65 6c 65 74 65 20 3d 20 78 44 65 6c 65  >xDelete = xDele
1ba23 74 65 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 66  te;.  return;..f
1ba24 61 69 6c 65 64 3a 0a 20 20 69 66 28 20 78 44 65  ailed:.  if( xDe
1ba25 6c 65 74 65 20 29 7b 0a 20 20 20 20 78 44 65 6c  lete ){.    xDel
1ba26 65 74 65 28 70 41 75 78 29 3b 0a 20 20 7d 0a 7d  ete(pAux);.  }.}
1ba27 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1ba28 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
1ba29 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
1ba2a 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65  e number of time
1ba2b 73 20 74 68 65 20 53 74 65 70 20 66 75 6e 63 74  s the Step funct
1ba2c 69 6f 6e 20 6f 66 20 61 20 61 67 67 72 65 67 61  ion of a aggrega
1ba2d 74 65 20 68 61 73 20 62 65 65 6e 20 0a 2a 2a 20  te has been .** 
1ba2e 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  called..**.** Th
1ba2f 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64  is function is d
1ba30 65 70 72 65 63 61 74 65 64 2e 20 20 44 6f 20 6e  eprecated.  Do n
1ba31 6f 74 20 75 73 65 20 69 74 20 66 6f 72 20 6e 65  ot use it for ne
1ba32 77 20 63 6f 64 65 2e 20 20 49 74 20 69 73 0a 2a  w code.  It is.*
1ba33 2a 20 70 72 6f 76 69 64 65 20 6f 6e 6c 79 20 74  * provide only t
1ba34 6f 20 61 76 6f 69 64 20 62 72 65 61 6b 69 6e 67  o avoid breaking
1ba35 20 6c 65 67 61 63 79 20 63 6f 64 65 2e 20 20 4e   legacy code.  N
1ba36 65 77 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  ew aggregate fun
1ba37 63 74 69 6f 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65  ction.** impleme
1ba38 6e 74 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20  ntations should 
1ba39 6b 65 65 70 20 74 68 65 69 72 20 6f 77 6e 20 63  keep their own c
1ba3a 6f 75 6e 74 73 20 77 69 74 68 69 6e 20 74 68 65  ounts within the
1ba3b 69 72 20 61 67 67 72 65 67 61 74 65 0a 2a 2a 20  ir aggregate.** 
1ba3c 63 6f 6e 74 65 78 74 2e 0a 2a 2f 0a 53 51 4c 49  context..*/.SQLI
1ba3d 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1ba3e 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75  e3_aggregate_cou
1ba3f 6e 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  nt(sqlite3_conte
1ba40 78 74 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  xt *p){.  assert
1ba41 28 20 70 20 26 26 20 70 2d 3e 70 4d 65 6d 20 26  ( p && p->pMem &
1ba42 26 20 70 2d 3e 70 46 75 6e 63 20 26 26 20 70 2d  & p->pFunc && p-
1ba43 3e 70 46 75 6e 63 2d 3e 78 53 74 65 70 20 29 3b  >pFunc->xStep );
1ba44 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 4d 65  .  return p->pMe
1ba45 6d 2d 3e 6e 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  m->n;.}.#endif..
1ba46 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
1ba47 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
1ba48 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ns in the result
1ba49 20 73 65 74 20 66 6f 72 20 74 68 65 20 73 74 61   set for the sta
1ba4a 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a 2f  tement pStmt..*/
1ba4b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1ba4c 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63  sqlite3_column_c
1ba4d 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d  ount(sqlite3_stm
1ba4e 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62  t *pStmt){.  Vdb
1ba4f 65 20 2a 70 56 6d 20 3d 20 28 56 64 62 65 20 2a  e *pVm = (Vdbe *
1ba50 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e  )pStmt;.  return
1ba51 20 70 56 6d 20 3f 20 70 56 6d 2d 3e 6e 52 65 73   pVm ? pVm->nRes
1ba52 43 6f 6c 75 6d 6e 20 3a 20 30 3b 0a 7d 0a 0a 2f  Column : 0;.}../
1ba53 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1ba54 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75 65 73  number of values
1ba55 20 61 76 61 69 6c 61 62 6c 65 20 66 72 6f 6d 20   available from 
1ba56 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20  the current row 
1ba57 6f 66 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e  of the.** curren
1ba58 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20 73 74  tly executing st
1ba59 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a  atement pStmt..*
1ba5a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1ba5b 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f   sqlite3_data_co
1ba5c 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  unt(sqlite3_stmt
1ba5d 20 2a 70 53 74 6d 74 29 7b 0a 20 20 56 64 62 65   *pStmt){.  Vdbe
1ba5e 20 2a 70 56 6d 20 3d 20 28 56 64 62 65 20 2a 29   *pVm = (Vdbe *)
1ba5f 70 53 74 6d 74 3b 0a 20 20 69 66 28 20 70 56 6d  pStmt;.  if( pVm
1ba60 3d 3d 30 20 7c 7c 20 70 56 6d 2d 3e 70 52 65 73  ==0 || pVm->pRes
1ba61 75 6c 74 53 65 74 3d 3d 30 20 29 20 72 65 74 75  ultSet==0 ) retu
1ba62 72 6e 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 70  rn 0;.  return p
1ba63 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3b 0a  Vm->nResColumn;.
1ba64 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  }.../*.** Check 
1ba65 74 6f 20 73 65 65 20 69 66 20 63 6f 6c 75 6d 6e  to see if column
1ba66 20 69 43 6f 6c 20 6f 66 20 74 68 65 20 67 69 76   iCol of the giv
1ba67 65 6e 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  en statement is 
1ba68 76 61 6c 69 64 2e 20 20 49 66 0a 2a 2a 20 69 74  valid.  If.** it
1ba69 20 69 73 2c 20 72 65 74 75 72 6e 20 61 20 70 6f   is, return a po
1ba6a 69 6e 74 65 72 20 74 6f 20 74 68 65 20 4d 65 6d  inter to the Mem
1ba6b 20 66 6f 72 20 74 68 65 20 76 61 6c 75 65 20 6f   for the value o
1ba6c 66 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a 2a  f that column..*
1ba6d 2a 20 49 66 20 69 43 6f 6c 20 69 73 20 6e 6f 74  * If iCol is not
1ba6e 20 76 61 6c 69 64 2c 20 72 65 74 75 72 6e 20 61   valid, return a
1ba6f 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 4d 65   pointer to a Me
1ba70 6d 20 77 68 69 63 68 20 68 61 73 20 61 20 76 61  m which has a va
1ba71 6c 75 65 0a 2a 2a 20 6f 66 20 4e 55 4c 4c 2e 0a  lue.** of NULL..
1ba72 2a 2f 0a 73 74 61 74 69 63 20 4d 65 6d 20 2a 63  */.static Mem *c
1ba73 6f 6c 75 6d 6e 4d 65 6d 28 73 71 6c 69 74 65 33  olumnMem(sqlite3
1ba74 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
1ba75 74 20 69 29 7b 0a 20 20 56 64 62 65 20 2a 70 56  t i){.  Vdbe *pV
1ba76 6d 3b 0a 20 20 69 6e 74 20 76 61 6c 73 3b 0a 20  m;.  int vals;. 
1ba77 20 4d 65 6d 20 2a 70 4f 75 74 3b 0a 0a 20 20 70   Mem *pOut;..  p
1ba78 56 6d 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74  Vm = (Vdbe *)pSt
1ba79 6d 74 3b 0a 20 20 69 66 28 20 70 56 6d 20 26 26  mt;.  if( pVm &&
1ba7a 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74   pVm->pResultSet
1ba7b 21 3d 30 20 26 26 20 69 3c 70 56 6d 2d 3e 6e 52  !=0 && i<pVm->nR
1ba7c 65 73 43 6f 6c 75 6d 6e 20 26 26 20 69 3e 3d 30  esColumn && i>=0
1ba7d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
1ba7e 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d  mutex_enter(pVm-
1ba7f 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  >db->mutex);.   
1ba80 20 76 61 6c 73 20 3d 20 73 71 6c 69 74 65 33 5f   vals = sqlite3_
1ba81 64 61 74 61 5f 63 6f 75 6e 74 28 70 53 74 6d 74  data_count(pStmt
1ba82 29 3b 0a 20 20 20 20 70 4f 75 74 20 3d 20 26 70  );.    pOut = &p
1ba83 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74 5b 69  Vm->pResultSet[i
1ba84 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ];.  }else{.    
1ba85 2f 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20  /* If the value 
1ba86 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65  passed as the se
1ba87 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
1ba88 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 72   out of range, r
1ba89 65 74 75 72 6e 0a 20 20 20 20 2a 2a 20 61 20 70  eturn.    ** a p
1ba8a 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 6f  ointer to the fo
1ba8b 6c 6c 6f 77 69 6e 67 20 73 74 61 74 69 63 20 4d  llowing static M
1ba8c 65 6d 20 6f 62 6a 65 63 74 20 77 68 69 63 68 20  em object which 
1ba8d 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 20 20 20  contains the.   
1ba8e 20 2a 2a 20 76 61 6c 75 65 20 53 51 4c 20 4e 55   ** value SQL NU
1ba8f 4c 4c 2e 20 45 76 65 6e 20 74 68 6f 75 67 68 20  LL. Even though 
1ba90 74 68 65 20 4d 65 6d 20 73 74 72 75 63 74 75 72  the Mem structur
1ba91 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 6c  e contains an el
1ba92 65 6d 65 6e 74 0a 20 20 20 20 2a 2a 20 6f 66 20  ement.    ** of 
1ba93 74 79 70 65 20 69 36 34 2c 20 6f 6e 20 63 65 72  type i64, on cer
1ba94 74 61 69 6e 20 61 72 63 68 69 74 65 63 74 75 72  tain architectur
1ba95 65 20 28 78 38 36 29 20 77 69 74 68 20 63 65 72  e (x86) with cer
1ba96 74 61 69 6e 20 63 6f 6d 70 69 6c 65 72 0a 20 20  tain compiler.  
1ba97 20 20 2a 2a 20 73 77 69 74 63 68 65 73 20 28 2d    ** switches (-
1ba98 4f 73 29 2c 20 67 63 63 20 6d 61 79 20 61 6c 69  Os), gcc may ali
1ba99 67 6e 20 74 68 69 73 20 4d 65 6d 20 6f 62 6a 65  gn this Mem obje
1ba9a 63 74 20 6f 6e 20 61 20 34 2d 62 79 74 65 20 62  ct on a 4-byte b
1ba9b 6f 75 6e 64 61 72 79 0a 20 20 20 20 2a 2a 20 69  oundary.    ** i
1ba9c 6e 73 74 65 61 64 20 6f 66 20 61 6e 20 38 2d 62  nstead of an 8-b
1ba9d 79 74 65 20 6f 6e 65 2e 20 54 68 69 73 20 61 6c  yte one. This al
1ba9e 6c 20 77 6f 72 6b 73 20 66 69 6e 65 2c 20 65 78  l works fine, ex
1ba9f 63 65 70 74 20 74 68 61 74 20 77 68 65 6e 0a 20  cept that when. 
1baa0 20 20 20 2a 2a 20 72 75 6e 6e 69 6e 67 20 77 69     ** running wi
1baa1 74 68 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  th SQLITE_DEBUG 
1baa2 64 65 66 69 6e 65 64 20 74 68 65 20 53 51 4c 69  defined the SQLi
1baa3 74 65 20 63 6f 64 65 20 73 6f 6d 65 74 69 6d 65  te code sometime
1baa4 73 20 61 73 73 65 72 74 28 29 73 0a 20 20 20 20  s assert()s.    
1baa5 2a 2a 20 74 68 61 74 20 61 20 4d 65 6d 20 73 74  ** that a Mem st
1baa6 72 75 63 74 75 72 65 20 69 73 20 6c 6f 63 61 74  ructure is locat
1baa7 65 64 20 6f 6e 20 61 6e 20 38 2d 62 79 74 65 20  ed on an 8-byte 
1baa8 62 6f 75 6e 64 61 72 79 2e 20 54 6f 20 70 72 65  boundary. To pre
1baa9 76 65 6e 74 0a 20 20 20 20 2a 2a 20 74 68 69 73  vent.    ** this
1baaa 20 61 73 73 65 72 74 28 29 20 66 72 6f 6d 20 66   assert() from f
1baab 61 69 6c 69 6e 67 2c 20 77 68 65 6e 20 62 75 69  ailing, when bui
1baac 6c 64 69 6e 67 20 77 69 74 68 20 53 51 4c 49 54  lding with SQLIT
1baad 45 5f 44 45 42 55 47 20 64 65 66 69 6e 65 64 0a  E_DEBUG defined.
1baae 20 20 20 20 2a 2a 20 75 73 69 6e 67 20 67 63 63      ** using gcc
1baaf 2c 20 66 6f 72 63 65 20 6e 75 6c 6c 4d 65 6d 20  , force nullMem 
1bab0 74 6f 20 62 65 20 38 2d 62 79 74 65 20 61 6c 69  to be 8-byte ali
1bab1 67 6e 65 64 20 75 73 69 6e 67 20 74 68 65 20 6d  gned using the m
1bab2 61 67 69 63 61 6c 0a 20 20 20 20 2a 2a 20 5f 5f  agical.    ** __
1bab3 61 74 74 72 69 62 75 74 65 5f 5f 28 28 61 6c 69  attribute__((ali
1bab4 67 6e 65 64 28 38 29 29 29 20 6d 61 63 72 6f 2e  gned(8))) macro.
1bab5 20 20 2a 2f 0a 20 20 20 20 73 74 61 74 69 63 20    */.    static 
1bab6 63 6f 6e 73 74 20 4d 65 6d 20 6e 75 6c 6c 4d 65  const Mem nullMe
1bab7 6d 20 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  m .#if defined(S
1bab8 51 4c 49 54 45 5f 44 45 42 55 47 29 20 26 26 20  QLITE_DEBUG) && 
1bab9 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f  defined(__GNUC__
1baba 29 0a 20 20 20 20 20 20 5f 5f 61 74 74 72 69 62  ).      __attrib
1babb 75 74 65 5f 5f 28 28 61 6c 69 67 6e 65 64 28 38  ute__((aligned(8
1babc 29 29 29 20 0a 23 65 6e 64 69 66 0a 20 20 20 20  ))) .#endif.    
1babd 20 20 3d 20 7b 7b 30 7d 2c 20 28 64 6f 75 62 6c    = {{0}, (doubl
1babe 65 29 30 2c 20 30 2c 20 22 22 2c 20 30 2c 20 4d  e)0, 0, "", 0, M
1babf 45 4d 5f 4e 75 6c 6c 2c 20 53 51 4c 49 54 45 5f  EM_Null, SQLITE_
1bac0 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20 30 20 7d 3b  NULL, 0, 0, 0 };
1bac1 0a 0a 20 20 20 20 69 66 28 20 70 56 6d 20 26 26  ..    if( pVm &&
1bac2 20 41 4c 57 41 59 53 28 70 56 6d 2d 3e 64 62 29   ALWAYS(pVm->db)
1bac3 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1bac4 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 56  3_mutex_enter(pV
1bac5 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  m->db->mutex);. 
1bac6 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1bac7 72 28 70 56 6d 2d 3e 64 62 2c 20 53 51 4c 49 54  r(pVm->db, SQLIT
1bac8 45 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20  E_RANGE, 0);.   
1bac9 20 7d 0a 20 20 20 20 70 4f 75 74 20 3d 20 28 4d   }.    pOut = (M
1baca 65 6d 2a 29 26 6e 75 6c 6c 4d 65 6d 3b 0a 20 20  em*)&nullMem;.  
1bacb 7d 0a 20 20 72 65 74 75 72 6e 20 70 4f 75 74 3b  }.  return pOut;
1bacc 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
1bacd 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
1bace 64 20 61 66 74 65 72 20 69 6e 76 6f 6b 69 6e 67  d after invoking
1bacf 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   an sqlite3_valu
1bad0 65 5f 58 58 58 20 66 75 6e 63 74 69 6f 6e 20 6f  e_XXX function o
1bad1 6e 20 61 20 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 76  n a .** column v
1bad2 61 6c 75 65 20 28 69 2e 65 2e 20 61 20 76 61 6c  alue (i.e. a val
1bad3 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 65  ue returned by e
1bad4 76 61 6c 75 61 74 69 6e 67 20 61 6e 20 53 51 4c  valuating an SQL
1bad5 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74   expression in t
1bad6 68 65 0a 2a 2a 20 73 65 6c 65 63 74 20 6c 69 73  he.** select lis
1bad7 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74  t of a SELECT st
1bad8 61 74 65 6d 65 6e 74 29 20 74 68 61 74 20 6d 61  atement) that ma
1bad9 79 20 63 61 75 73 65 20 61 20 6d 61 6c 6c 6f 63  y cause a malloc
1bada 28 29 20 66 61 69 6c 75 72 65 2e 20 49 66 20 0a  () failure. If .
1badb 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20  ** malloc() has 
1badc 66 61 69 6c 65 64 2c 20 74 68 65 20 74 68 72 65  failed, the thre
1badd 61 64 73 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  ads mallocFailed
1bade 20 66 6c 61 67 20 69 73 20 63 6c 65 61 72 65 64   flag is cleared
1badf 20 61 6e 64 20 74 68 65 20 72 65 73 75 6c 74 0a   and the result.
1bae0 2a 2a 20 63 6f 64 65 20 6f 66 20 73 74 61 74 65  ** code of state
1bae1 6d 65 6e 74 20 70 53 74 6d 74 20 73 65 74 20 74  ment pStmt set t
1bae2 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a  o SQLITE_NOMEM..
1bae3 2a 2a 0a 2a 2a 20 53 70 65 63 69 66 69 63 61 6c  **.** Specifical
1bae4 6c 79 2c 20 74 68 69 73 20 69 73 20 63 61 6c 6c  ly, this is call
1bae5 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 3a 0a  ed from within:.
1bae6 2a 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65  **.**     sqlite
1bae7 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 29 0a 2a  3_column_int().*
1bae8 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f  *     sqlite3_co
1bae9 6c 75 6d 6e 5f 69 6e 74 36 34 28 29 0a 2a 2a 20  lumn_int64().** 
1baea 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75      sqlite3_colu
1baeb 6d 6e 5f 74 65 78 74 28 29 0a 2a 2a 20 20 20 20  mn_text().**    
1baec 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1baed 74 65 78 74 31 36 28 29 0a 2a 2a 20 20 20 20 20  text16().**     
1baee 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 72  sqlite3_column_r
1baef 65 61 6c 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c  eal().**     sql
1baf0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
1baf1 73 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  s().**     sqlit
1baf2 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31  e3_column_bytes1
1baf3 36 28 29 0a 2a 2a 0a 2a 2a 20 42 75 74 20 6e 6f  6().**.** But no
1baf4 74 20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 6f  t for sqlite3_co
1baf5 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 77 68 69  lumn_blob(), whi
1baf6 63 68 20 6e 65 76 65 72 20 63 61 6c 6c 73 20 6d  ch never calls m
1baf7 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74  alloc()..*/.stat
1baf8 69 63 20 76 6f 69 64 20 63 6f 6c 75 6d 6e 4d 61  ic void columnMa
1baf9 6c 6c 6f 63 46 61 69 6c 75 72 65 28 73 71 6c 69  llocFailure(sqli
1bafa 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29  te3_stmt *pStmt)
1bafb 0a 7b 0a 20 20 2f 2a 20 49 66 20 6d 61 6c 6c 6f  .{.  /* If mallo
1bafc 63 28 29 20 66 61 69 6c 65 64 20 64 75 72 69 6e  c() failed durin
1bafd 67 20 61 6e 20 65 6e 63 6f 64 69 6e 67 20 63 6f  g an encoding co
1bafe 6e 76 65 72 73 69 6f 6e 20 77 69 74 68 69 6e 20  nversion within 
1baff 61 6e 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  an.  ** sqlite3_
1bb00 63 6f 6c 75 6d 6e 5f 58 58 58 20 41 50 49 2c 20  column_XXX API, 
1bb01 74 68 65 6e 20 73 65 74 20 74 68 65 20 72 65 74  then set the ret
1bb02 75 72 6e 20 63 6f 64 65 20 6f 66 20 74 68 65 20  urn code of the 
1bb03 73 74 61 74 65 6d 65 6e 74 20 74 6f 0a 20 20 2a  statement to.  *
1bb04 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 20  * SQLITE_NOMEM. 
1bb05 54 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f  The next call to
1bb06 20 5f 73 74 65 70 28 29 20 28 69 66 20 61 6e 79   _step() (if any
1bb07 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51  ) will return SQ
1bb08 4c 49 54 45 5f 45 52 52 4f 52 0a 20 20 2a 2a 20  LITE_ERROR.  ** 
1bb09 61 6e 64 20 5f 66 69 6e 61 6c 69 7a 65 28 29 20  and _finalize() 
1bb0a 77 69 6c 6c 20 72 65 74 75 72 6e 20 4e 4f 4d 45  will return NOME
1bb0b 4d 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 20 2a  M..  */.  Vdbe *
1bb0c 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d  p = (Vdbe *)pStm
1bb0d 74 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  t;.  if( p ){.  
1bb0e 20 20 70 2d 3e 72 63 20 3d 20 73 71 6c 69 74 65    p->rc = sqlite
1bb0f 33 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20  3ApiExit(p->db, 
1bb10 70 2d 3e 72 63 29 3b 0a 20 20 20 20 73 71 6c 69  p->rc);.    sqli
1bb11 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
1bb12 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  p->db->mutex);. 
1bb13 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a   }.}../*********
1bb14 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bb15 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  *** sqlite3_colu
1bb16 6d 6e 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  mn_  ***********
1bb17 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bb18 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ****.** The foll
1bb19 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20 61  owing routines a
1bb1a 72 65 20 75 73 65 64 20 74 6f 20 61 63 63 65 73  re used to acces
1bb1b 73 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68  s elements of th
1bb1c 65 20 63 75 72 72 65 6e 74 20 72 6f 77 0a 2a 2a  e current row.**
1bb1d 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
1bb1e 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  et..*/.SQLITE_AP
1bb1f 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
1bb20 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
1bb21 62 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  b(sqlite3_stmt *
1bb22 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
1bb23 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c   const void *val
1bb24 3b 0a 20 20 76 61 6c 20 3d 20 73 71 6c 69 74 65  ;.  val = sqlite
1bb25 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 20 63 6f  3_value_blob( co
1bb26 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29  lumnMem(pStmt,i)
1bb27 20 29 3b 0a 20 20 2f 2a 20 45 76 65 6e 20 74 68   );.  /* Even th
1bb28 6f 75 67 68 20 74 68 65 72 65 20 69 73 20 6e 6f  ough there is no
1bb29 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72   encoding conver
1bb2a 73 69 6f 6e 2c 20 76 61 6c 75 65 5f 62 6c 6f 62  sion, value_blob
1bb2b 28 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20 6e 65  () might.  ** ne
1bb2c 65 64 20 74 6f 20 63 61 6c 6c 20 6d 61 6c 6c 6f  ed to call mallo
1bb2d 63 28 29 20 74 6f 20 65 78 70 61 6e 64 20 74 68  c() to expand th
1bb2e 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 7a 65  e result of a ze
1bb2f 72 6f 62 6c 6f 62 28 29 20 0a 20 20 2a 2a 20 65  roblob() .  ** e
1bb30 78 70 72 65 73 73 69 6f 6e 2e 20 0a 20 20 2a 2f  xpression. .  */
1bb31 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  .  columnMallocF
1bb32 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20  ailure(pStmt);. 
1bb33 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53   return val;.}.S
1bb34 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1bb35 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
1bb36 65 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  es(sqlite3_stmt 
1bb37 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a  *pStmt, int i){.
1bb38 20 20 69 6e 74 20 76 61 6c 20 3d 20 73 71 6c 69    int val = sqli
1bb39 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
1bb3a 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74   columnMem(pStmt
1bb3b 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d  ,i) );.  columnM
1bb3c 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74  allocFailure(pSt
1bb3d 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 76 61  mt);.  return va
1bb3e 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  l;.}.SQLITE_API 
1bb3f 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  int sqlite3_colu
1bb40 6d 6e 5f 62 79 74 65 73 31 36 28 73 71 6c 69 74  mn_bytes16(sqlit
1bb41 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1bb42 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61  int i){.  int va
1bb43 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
1bb44 65 5f 62 79 74 65 73 31 36 28 20 63 6f 6c 75 6d  e_bytes16( colum
1bb45 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b  nMem(pStmt,i) );
1bb46 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  .  columnMallocF
1bb47 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20  ailure(pStmt);. 
1bb48 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53   return val;.}.S
1bb49 51 4c 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65  QLITE_API double
1bb4a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1bb4b 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73  double(sqlite3_s
1bb4c 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
1bb4d 69 29 7b 0a 20 20 64 6f 75 62 6c 65 20 76 61 6c  i){.  double val
1bb4e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
1bb4f 5f 64 6f 75 62 6c 65 28 20 63 6f 6c 75 6d 6e 4d  _double( columnM
1bb50 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20  em(pStmt,i) );. 
1bb51 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69   columnMallocFai
1bb52 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72  lure(pStmt);.  r
1bb53 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c  eturn val;.}.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 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73  te3_column_int(s
1bb56 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1bb57 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e  mt, int i){.  in
1bb58 74 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f  t val = sqlite3_
1bb59 76 61 6c 75 65 5f 69 6e 74 28 20 63 6f 6c 75 6d  value_int( colum
1bb5a 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b  nMem(pStmt,i) );
1bb5b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  .  columnMallocF
1bb5c 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20  ailure(pStmt);. 
1bb5d 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53   return val;.}.S
1bb5e 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
1bb5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 63  _int64 sqlite3_c
1bb60 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71 6c 69  olumn_int64(sqli
1bb61 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1bb62 20 69 6e 74 20 69 29 7b 0a 20 20 73 71 6c 69 74   int i){.  sqlit
1bb63 65 5f 69 6e 74 36 34 20 76 61 6c 20 3d 20 73 71  e_int64 val = sq
1bb64 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36  lite3_value_int6
1bb65 34 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74  4( columnMem(pSt
1bb66 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d  mt,i) );.  colum
1bb67 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70  nMallocFailure(p
1bb68 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  Stmt);.  return 
1bb69 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  val;.}.SQLITE_AP
1bb6a 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  I const unsigned
1bb6b 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
1bb6c 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69 74  olumn_text(sqlit
1bb6d 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1bb6e 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73 74 20  int i){.  const 
1bb6f 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 76  unsigned char *v
1bb70 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  al = sqlite3_val
1bb71 75 65 5f 74 65 78 74 28 20 63 6f 6c 75 6d 6e 4d  ue_text( columnM
1bb72 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20  em(pStmt,i) );. 
1bb73 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69   columnMallocFai
1bb74 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72  lure(pStmt);.  r
1bb75 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c  eturn val;.}.SQL
1bb76 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
1bb77 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 5f 63  value *sqlite3_c
1bb78 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 73 71 6c 69  olumn_value(sqli
1bb79 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1bb7a 20 69 6e 74 20 69 29 7b 0a 20 20 4d 65 6d 20 2a   int i){.  Mem *
1bb7b 70 4f 75 74 20 3d 20 63 6f 6c 75 6d 6e 4d 65 6d  pOut = columnMem
1bb7c 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20 20 69 66  (pStmt, i);.  if
1bb7d 28 20 70 4f 75 74 2d 3e 66 6c 61 67 73 26 4d 45  ( pOut->flags&ME
1bb7e 4d 5f 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20  M_Static ){.    
1bb7f 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  pOut->flags &= ~
1bb80 4d 45 4d 5f 53 74 61 74 69 63 3b 0a 20 20 20 20  MEM_Static;.    
1bb81 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d  pOut->flags |= M
1bb82 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 7d 0a 20 20  EM_Ephem;.  }.  
1bb83 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c  columnMallocFail
1bb84 75 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65  ure(pStmt);.  re
1bb85 74 75 72 6e 20 28 73 71 6c 69 74 65 33 5f 76 61  turn (sqlite3_va
1bb86 6c 75 65 20 2a 29 70 4f 75 74 3b 0a 7d 0a 23 69  lue *)pOut;.}.#i
1bb87 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1bb88 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41  T_UTF16.SQLITE_A
1bb89 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
1bb8a 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
1bb8b 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  xt16(sqlite3_stm
1bb8c 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29  t *pStmt, int i)
1bb8d 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  {.  const void *
1bb8e 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  val = sqlite3_va
1bb8f 6c 75 65 5f 74 65 78 74 31 36 28 20 63 6f 6c 75  lue_text16( colu
1bb90 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29  mnMem(pStmt,i) )
1bb91 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63  ;.  columnMalloc
1bb92 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a  Failure(pStmt);.
1bb93 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a    return val;.}.
1bb94 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1bb95 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53  _OMIT_UTF16 */.S
1bb96 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1bb97 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
1bb98 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
1bb99 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
1bb9a 20 69 6e 74 20 69 54 79 70 65 20 3d 20 73 71 6c   int iType = sql
1bb9b 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
1bb9c 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74   columnMem(pStmt
1bb9d 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d  ,i) );.  columnM
1bb9e 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70 53 74  allocFailure(pSt
1bb9f 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 54  mt);.  return iT
1bba0 79 70 65 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66  ype;.}../* The f
1bba1 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f  ollowing functio
1bba2 6e 20 69 73 20 65 78 70 65 72 69 6d 65 6e 74 61  n is experimenta
1bba3 6c 20 61 6e 64 20 73 75 62 6a 65 63 74 20 74 6f  l and subject to
1bba4 20 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72 65   change or.** re
1bba5 6d 6f 76 61 6c 20 2a 2f 0a 2f 2a 69 6e 74 20 73  moval */./*int s
1bba6 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 75  qlite3_column_nu
1bba7 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69 74  meric_type(sqlit
1bba8 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1bba9 69 6e 74 20 69 29 7b 0a 2a 2a 20 20 72 65 74 75  int i){.**  retu
1bbaa 72 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  rn sqlite3_value
1bbab 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 20 63  _numeric_type( c
1bbac 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69  olumnMem(pStmt,i
1bbad 29 20 29 3b 0a 2a 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a  ) );.**}.*/../*.
1bbae 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 4e  ** Convert the N
1bbaf 2d 74 68 20 65 6c 65 6d 65 6e 74 20 6f 66 20 70  -th element of p
1bbb0 53 74 6d 74 2d 3e 70 43 6f 6c 4e 61 6d 65 5b 5d  Stmt->pColName[]
1bbb1 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 75   into a string u
1bbb2 73 69 6e 67 0a 2a 2a 20 78 46 75 6e 63 28 29 20  sing.** xFunc() 
1bbb3 74 68 65 6e 20 72 65 74 75 72 6e 20 74 68 61 74  then return that
1bbb4 20 73 74 72 69 6e 67 2e 20 20 49 66 20 4e 20 69   string.  If N i
1bbb5 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20  s out of range, 
1bbb6 72 65 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20  return 0..**.** 
1bbb7 54 68 65 72 65 20 61 72 65 20 75 70 20 74 6f 20  There are up to 
1bbb8 35 20 6e 61 6d 65 73 20 66 6f 72 20 65 61 63 68  5 names for each
1bbb9 20 63 6f 6c 75 6d 6e 2e 20 20 75 73 65 54 79 70   column.  useTyp
1bbba 65 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 69  e determines whi
1bbbb 63 68 0a 2a 2a 20 6e 61 6d 65 20 69 73 20 72 65  ch.** name is re
1bbbc 74 75 72 6e 65 64 2e 20 20 48 65 72 65 20 61 72  turned.  Here ar
1bbbd 65 20 74 68 65 20 6e 61 6d 65 73 3a 0a 2a 2a 0a  e the names:.**.
1bbbe 2a 2a 20 20 20 20 30 20 20 20 20 20 20 54 68 65  **    0      The
1bbbf 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 61 73 20   column name as 
1bbc0 69 74 20 73 68 6f 75 6c 64 20 62 65 20 64 69 73  it should be dis
1bbc1 70 6c 61 79 65 64 20 66 6f 72 20 6f 75 74 70 75  played for outpu
1bbc2 74 0a 2a 2a 20 20 20 20 31 20 20 20 20 20 20 54  t.**    1      T
1bbc3 68 65 20 64 61 74 61 74 79 70 65 20 6e 61 6d 65  he datatype name
1bbc4 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 0a   for the column.
1bbc5 2a 2a 20 20 20 20 32 20 20 20 20 20 20 54 68 65  **    2      The
1bbc6 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74   name of the dat
1bbc7 61 62 61 73 65 20 74 68 61 74 20 74 68 65 20 63  abase that the c
1bbc8 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72  olumn derives fr
1bbc9 6f 6d 0a 2a 2a 20 20 20 20 33 20 20 20 20 20 20  om.**    3      
1bbca 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
1bbcb 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 63  table that the c
1bbcc 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72  olumn derives fr
1bbcd 6f 6d 0a 2a 2a 20 20 20 20 34 20 20 20 20 20 20  om.**    4      
1bbce 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
1bbcf 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61  table column tha
1bbd0 74 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c  t the result col
1bbd1 75 6d 6e 20 64 65 72 69 76 65 73 20 66 72 6f 6d  umn derives from
1bbd2 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65  .**.** If the re
1bbd3 73 75 6c 74 20 69 73 20 6e 6f 74 20 61 20 73 69  sult is not a si
1bbd4 6d 70 6c 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65  mple column refe
1bbd5 72 65 6e 63 65 20 28 69 66 20 69 74 20 69 73 20  rence (if it is 
1bbd6 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a  an expression.**
1bbd7 20 6f 72 20 61 20 63 6f 6e 73 74 61 6e 74 29 20   or a constant) 
1bbd8 74 68 65 6e 20 75 73 65 54 79 70 65 73 20 32 2c  then useTypes 2,
1bbd9 20 33 2c 20 61 6e 64 20 34 20 72 65 74 75 72 6e   3, and 4 return
1bbda 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63   NULL..*/.static
1bbdb 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 63 6f 6c   const void *col
1bbdc 75 6d 6e 4e 61 6d 65 28 0a 20 20 73 71 6c 69 74  umnName(.  sqlit
1bbdd 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 0a  e3_stmt *pStmt,.
1bbde 20 20 69 6e 74 20 4e 2c 0a 20 20 63 6f 6e 73 74    int N,.  const
1bbdf 20 76 6f 69 64 20 2a 28 2a 78 46 75 6e 63 29 28   void *(*xFunc)(
1bbe0 4d 65 6d 2a 29 2c 0a 20 20 69 6e 74 20 75 73 65  Mem*),.  int use
1bbe1 54 79 70 65 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  Type.){.  const 
1bbe2 76 6f 69 64 20 2a 72 65 74 20 3d 20 30 3b 0a 20  void *ret = 0;. 
1bbe3 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65   Vdbe *p = (Vdbe
1bbe4 20 2a 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20   *)pStmt;.  int 
1bbe5 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  n;.  sqlite3 *db
1bbe6 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 0a 20 20 61   = p->db;.  .  a
1bbe7 73 73 65 72 74 28 20 64 62 21 3d 30 20 29 3b 0a  ssert( db!=0 );.
1bbe8 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f    n = sqlite3_co
1bbe9 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 6d 74  lumn_count(pStmt
1bbea 29 3b 0a 20 20 69 66 28 20 4e 3c 6e 20 26 26 20  );.  if( N<n && 
1bbeb 4e 3e 3d 30 20 29 7b 0a 20 20 20 20 4e 20 2b 3d  N>=0 ){.    N +=
1bbec 20 75 73 65 54 79 70 65 2a 6e 3b 0a 20 20 20 20   useType*n;.    
1bbed 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1bbee 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
1bbef 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
1bbf0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d 30 20  mallocFailed==0 
1bbf1 29 3b 0a 20 20 20 20 72 65 74 20 3d 20 78 46 75  );.    ret = xFu
1bbf2 6e 63 28 26 70 2d 3e 61 43 6f 6c 4e 61 6d 65 5b  nc(&p->aColName[
1bbf3 4e 5d 29 3b 0a 20 20 20 20 20 2f 2a 20 41 20 6d  N]);.     /* A m
1bbf4 61 6c 6c 6f 63 20 6d 61 79 20 68 61 76 65 20 66  alloc may have f
1bbf5 61 69 6c 65 64 20 69 6e 73 69 64 65 20 6f 66 20  ailed inside of 
1bbf6 74 68 65 20 78 46 75 6e 63 28 29 20 63 61 6c 6c  the xFunc() call
1bbf7 2e 20 49 66 20 74 68 69 73 0a 20 20 20 20 2a 2a  . If this.    **
1bbf8 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 63 6c   is the case, cl
1bbf9 65 61 72 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61  ear the mallocFa
1bbfa 69 6c 65 64 20 66 6c 61 67 20 61 6e 64 20 72 65  iled flag and re
1bbfb 74 75 72 6e 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a  turn NULL..    *
1bbfc 2f 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61  /.    if( db->ma
1bbfd 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
1bbfe 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
1bbff 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20  iled = 0;.      
1bc00 72 65 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  ret = 0;.    }. 
1bc01 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
1bc02 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
1bc03 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1bc04 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ret;.}../*.** Re
1bc05 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66  turn the name of
1bc06 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20   the Nth column 
1bc07 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  of the result se
1bc08 74 20 72 65 74 75 72 6e 65 64 20 62 79 20 53 51  t returned by SQ
1bc09 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 70  L.** statement p
1bc0a 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  Stmt..*/.SQLITE_
1bc0b 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
1bc0c 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
1bc0d 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ame(sqlite3_stmt
1bc0e 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b   *pStmt, int N){
1bc0f 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e  .  return column
1bc10 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d  Name(.      pStm
1bc11 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69  t, N, (const voi
1bc12 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69  d*(*)(Mem*))sqli
1bc13 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20  te3_value_text, 
1bc14 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b 0a 7d  COLNAME_NAME);.}
1bc15 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1bc16 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54  OMIT_UTF16.SQLIT
1bc17 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
1bc18 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
1bc19 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65 33 5f  _name16(sqlite3_
1bc1a 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
1bc1b 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f   N){.  return co
1bc1c 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20  lumnName(.      
1bc1d 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74  pStmt, N, (const
1bc1e 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29   void*(*)(Mem*))
1bc1f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1bc20 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  xt16, COLNAME_NA
1bc21 4d 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  ME);.}.#endif../
1bc22 2a 0a 2a 2a 20 43 6f 6e 73 74 72 61 69 6e 74 3a  *.** Constraint:
1bc23 20 20 49 66 20 79 6f 75 20 68 61 76 65 20 45 4e    If you have EN
1bc24 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41  ABLE_COLUMN_META
1bc25 44 41 54 41 20 74 68 65 6e 20 79 6f 75 20 6d 75  DATA then you mu
1bc26 73 74 0a 2a 2a 20 6e 6f 74 20 64 65 66 69 6e 65  st.** not define
1bc27 20 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 2e 0a   OMIT_DECLTYPE..
1bc28 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
1bc29 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54  QLITE_OMIT_DECLT
1bc2a 59 50 45 29 20 26 26 20 64 65 66 69 6e 65 64 28  YPE) && defined(
1bc2b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f  SQLITE_ENABLE_CO
1bc2c 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 29 0a 23  LUMN_METADATA).#
1bc2d 20 65 72 72 6f 72 20 22 4d 75 73 74 20 6e 6f 74   error "Must not
1bc2e 20 64 65 66 69 6e 65 20 62 6f 74 68 20 53 51 4c   define both SQL
1bc2f 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50  ITE_OMIT_DECLTYP
1bc30 45 20 5c 0a 20 20 20 20 20 20 20 20 20 61 6e 64  E \.         and
1bc31 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43   SQLITE_ENABLE_C
1bc32 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 22 0a  OLUMN_METADATA".
1bc33 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
1bc34 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c  SQLITE_OMIT_DECL
1bc35 54 59 50 45 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  TYPE./*.** Retur
1bc36 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64 65 63  n the column dec
1bc37 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 28 69  laration type (i
1bc38 66 20 61 70 70 6c 69 63 61 62 6c 65 29 20 6f 66  f applicable) of
1bc39 20 74 68 65 20 27 69 27 74 68 20 63 6f 6c 75 6d   the 'i'th colum
1bc3a 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 72 65 73 75  n.** of the resu
1bc3b 6c 74 20 73 65 74 20 6f 66 20 53 51 4c 20 73 74  lt set of SQL st
1bc3c 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e 0a 2a  atement pStmt..*
1bc3d 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
1bc3e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
1bc3f 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65  _column_decltype
1bc40 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1bc41 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20  Stmt, int N){.  
1bc42 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d  return columnNam
1bc43 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20  e(.      pStmt, 
1bc44 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28  N, (const void*(
1bc45 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33  *)(Mem*))sqlite3
1bc46 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c  _value_text, COL
1bc47 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a  NAME_DECLTYPE);.
1bc48 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
1bc49 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49  _OMIT_UTF16.SQLI
1bc4a 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69  TE_API const voi
1bc4b 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  d *sqlite3_colum
1bc4c 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 73 71 6c  n_decltype16(sql
1bc4d 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1bc4e 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75  , int N){.  retu
1bc4f 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20  rn columnName(. 
1bc50 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28       pStmt, N, (
1bc51 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d  const void*(*)(M
1bc52 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c  em*))sqlite3_val
1bc53 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41  ue_text16, COLNA
1bc54 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a 7d 0a  ME_DECLTYPE);.}.
1bc55 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1bc56 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 23  _OMIT_UTF16 */.#
1bc57 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1bc58 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20 2a 2f  OMIT_DECLTYPE */
1bc59 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
1bc5a 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45  ENABLE_COLUMN_ME
1bc5b 54 41 44 41 54 41 0a 2f 2a 0a 2a 2a 20 52 65 74  TADATA./*.** Ret
1bc5c 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  urn the name of 
1bc5d 74 68 65 20 64 61 74 61 62 61 73 65 20 66 72 6f  the database fro
1bc5e 6d 20 77 68 69 63 68 20 61 20 72 65 73 75 6c 74  m which a result
1bc5f 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65 73 2e   column derives.
1bc60 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75  .** NULL is retu
1bc61 72 6e 65 64 20 69 66 20 74 68 65 20 72 65 73 75  rned if the resu
1bc62 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61 6e 20  lt column is an 
1bc63 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20 63 6f  expression or co
1bc64 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61 6e 79  nstant or.** any
1bc65 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69 63 68  thing else which
1bc66 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61 62 69   is not an unabi
1bc67 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63 65 20  guous reference 
1bc68 74 6f 20 61 20 64 61 74 61 62 61 73 65 20 63 6f  to a database co
1bc69 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lumn..*/.SQLITE_
1bc6a 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
1bc6b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
1bc6c 61 74 61 62 61 73 65 5f 6e 61 6d 65 28 73 71 6c  atabase_name(sql
1bc6d 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1bc6e 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75  , int N){.  retu
1bc6f 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20  rn columnName(. 
1bc70 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28       pStmt, N, (
1bc71 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d  const void*(*)(M
1bc72 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c  em*))sqlite3_val
1bc73 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41 4d 45  ue_text, COLNAME
1bc74 5f 44 41 54 41 42 41 53 45 29 3b 0a 7d 0a 23 69  _DATABASE);.}.#i
1bc75 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1bc76 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41  T_UTF16.SQLITE_A
1bc77 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
1bc78 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61  qlite3_column_da
1bc79 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 73 71  tabase_name16(sq
1bc7a 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1bc7b 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74  t, int N){.  ret
1bc7c 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a  urn columnName(.
1bc7d 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20        pStmt, N, 
1bc7e 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28  (const void*(*)(
1bc7f 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61  Mem*))sqlite3_va
1bc80 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e  lue_text16, COLN
1bc81 41 4d 45 5f 44 41 54 41 42 41 53 45 29 3b 0a 7d  AME_DATABASE);.}
1bc82 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1bc83 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a  E_OMIT_UTF16 */.
1bc84 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
1bc85 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  e name of the ta
1bc86 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 61  ble from which a
1bc87 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64   result column d
1bc88 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c 4c 20  erives..** NULL 
1bc89 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 74  is returned if t
1bc8a 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  he result column
1bc8b 20 69 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f   is an expressio
1bc8c 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20 6f 72  n or constant or
1bc8d 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65 6c 73  .** anything els
1bc8e 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74 20 61  e which is not a
1bc8f 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72 65 66  n unabiguous ref
1bc90 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61 74 61  erence to a data
1bc91 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a  base column..*/.
1bc92 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
1bc93 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
1bc94 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65  olumn_table_name
1bc95 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1bc96 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20  Stmt, int N){.  
1bc97 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d  return columnNam
1bc98 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20  e(.      pStmt, 
1bc99 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28  N, (const void*(
1bc9a 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33  *)(Mem*))sqlite3
1bc9b 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c  _value_text, COL
1bc9c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23  NAME_TABLE);.}.#
1bc9d 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1bc9e 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f  IT_UTF16.SQLITE_
1bc9f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
1bca0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
1bca1 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71 6c 69  able_name16(sqli
1bca2 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1bca3 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72   int N){.  retur
1bca4 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20  n columnName(.  
1bca5 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63      pStmt, N, (c
1bca6 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65  onst void*(*)(Me
1bca7 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75  m*))sqlite3_valu
1bca8 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d  e_text16, COLNAM
1bca9 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23 65 6e 64  E_TABLE);.}.#end
1bcaa 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1bcab 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a  T_UTF16 */../*.*
1bcac 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d  * Return the nam
1bcad 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63  e of the table c
1bcae 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77 68 69 63 68  olumn from which
1bcaf 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e   a result column
1bcb0 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c   derives..** NUL
1bcb1 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  L is returned if
1bcb2 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75   the result colu
1bcb3 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73 73  mn is an express
1bcb4 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20  ion or constant 
1bcb5 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65  or.** anything e
1bcb6 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74  lse which is not
1bcb7 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72   an unabiguous r
1bcb8 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61  eference to a da
1bcb9 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a  tabase column..*
1bcba 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
1bcbb 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
1bcbc 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  _column_origin_n
1bcbd 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ame(sqlite3_stmt
1bcbe 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b   *pStmt, int N){
1bcbf 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e  .  return column
1bcc0 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d  Name(.      pStm
1bcc1 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69  t, N, (const voi
1bcc2 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69  d*(*)(Mem*))sqli
1bcc3 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20  te3_value_text, 
1bcc4 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b  COLNAME_COLUMN);
1bcc5 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  .}.#ifndef SQLIT
1bcc6 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c  E_OMIT_UTF16.SQL
1bcc7 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
1bcc8 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
1bcc9 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36  mn_origin_name16
1bcca 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1bccb 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20  Stmt, int N){.  
1bccc 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d  return columnNam
1bccd 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20  e(.      pStmt, 
1bcce 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28  N, (const void*(
1bccf 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33  *)(Mem*))sqlite3
1bcd0 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43  _value_text16, C
1bcd1 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29 3b 0a  OLNAME_COLUMN);.
1bcd2 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1bcd3 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
1bcd4 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1bcd5 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f  E_ENABLE_COLUMN_
1bcd6 4d 45 54 41 44 41 54 41 20 2a 2f 0a 0a 0a 2f 2a  METADATA */.../*
1bcd7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bcd8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73  ************** s
1bcd9 71 6c 69 74 65 33 5f 62 69 6e 64 5f 20 20 2a 2a  qlite3_bind_  **
1bcda 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bcdb 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 0a 2a 2a  *********.** .**
1bcdc 20 52 6f 75 74 69 6e 65 73 20 75 73 65 64 20 74   Routines used t
1bcdd 6f 20 61 74 74 61 63 68 20 76 61 6c 75 65 73 20  o attach values 
1bcde 74 6f 20 77 69 6c 64 63 61 72 64 73 20 69 6e 20  to wildcards in 
1bcdf 61 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73  a compiled SQL s
1bce0 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 2f 2a 0a  tatement..*/./*.
1bce1 2a 2a 20 55 6e 62 69 6e 64 20 74 68 65 20 76 61  ** Unbind the va
1bce2 6c 75 65 20 62 6f 75 6e 64 20 74 6f 20 76 61 72  lue bound to var
1bce3 69 61 62 6c 65 20 69 20 69 6e 20 76 69 72 74 75  iable i in virtu
1bce4 61 6c 20 6d 61 63 68 69 6e 65 20 70 2e 20 54 68  al machine p. Th
1bce5 69 73 20 69 73 20 74 68 65 20 0a 2a 2a 20 74 68  is is the .** th
1bce6 65 20 73 61 6d 65 20 61 73 20 62 69 6e 64 69 6e  e same as bindin
1bce7 67 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65 20 74  g a NULL value t
1bce8 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20 49 66  o the column. If
1bce9 20 74 68 65 20 22 69 22 20 70 61 72 61 6d 65 74   the "i" paramet
1bcea 65 72 20 69 73 0a 2a 2a 20 6f 75 74 20 6f 66 20  er is.** out of 
1bceb 72 61 6e 67 65 2c 20 74 68 65 6e 20 53 51 4c 49  range, then SQLI
1bcec 54 45 5f 52 41 4e 47 45 20 69 73 20 72 65 74 75  TE_RANGE is retu
1bced 72 6e 65 64 2e 20 4f 74 68 65 77 69 73 65 20 53  rned. Othewise S
1bcee 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20  QLITE_OK..**.** 
1bcef 41 20 73 75 63 63 65 73 73 66 75 6c 20 65 76 61  A successful eva
1bcf0 6c 75 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20  luation of this 
1bcf1 72 6f 75 74 69 6e 65 20 61 63 71 75 69 72 65 73  routine acquires
1bcf2 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20 70 2e   the mutex on p.
1bcf3 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20 69 73  .** the mutex is
1bcf4 20 72 65 6c 65 61 73 65 64 20 69 66 20 61 6e 79   released if any
1bcf5 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 20 6f   kind of error o
1bcf6 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ccurs..**.** The
1bcf7 20 65 72 72 6f 72 20 63 6f 64 65 20 73 74 6f 72   error code stor
1bcf8 65 64 20 69 6e 20 64 61 74 61 62 61 73 65 20 70  ed in database p
1bcf9 2d 3e 64 62 20 69 73 20 6f 76 65 72 77 72 69 74  ->db is overwrit
1bcfa 74 65 6e 20 77 69 74 68 20 74 68 65 20 72 65 74  ten with the ret
1bcfb 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20  urn.** value in 
1bcfc 61 6e 79 20 63 61 73 65 2e 0a 2a 2f 0a 73 74 61  any case..*/.sta
1bcfd 74 69 63 20 69 6e 74 20 76 64 62 65 55 6e 62 69  tic int vdbeUnbi
1bcfe 6e 64 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20  nd(Vdbe *p, int 
1bcff 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b  i){.  Mem *pVar;
1bd00 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
1bd01 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  turn SQLITE_MISU
1bd02 53 45 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  SE;.  sqlite3_mu
1bd03 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62 2d  tex_enter(p->db-
1bd04 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70  >mutex);.  if( p
1bd05 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41  ->magic!=VDBE_MA
1bd06 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e 70 63  GIC_RUN || p->pc
1bd07 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  >=0 ){.    sqlit
1bd08 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53  e3Error(p->db, S
1bd09 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20 30 29  QLITE_MISUSE, 0)
1bd0a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
1bd0b 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d  tex_leave(p->db-
1bd0c 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74  >mutex);.    ret
1bd0d 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
1bd0e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 3c 31  E;.  }.  if( i<1
1bd0f 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29 7b   || i>p->nVar ){
1bd10 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
1bd11 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f  r(p->db, SQLITE_
1bd12 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20 20 73  RANGE, 0);.    s
1bd13 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1bd14 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ve(p->db->mutex)
1bd15 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
1bd16 49 54 45 5f 52 41 4e 47 45 3b 0a 20 20 7d 0a 20  ITE_RANGE;.  }. 
1bd17 20 69 2d 2d 3b 0a 20 20 70 56 61 72 20 3d 20 26   i--;.  pVar = &
1bd18 70 2d 3e 61 56 61 72 5b 69 5d 3b 0a 20 20 73 71  p->aVar[i];.  sq
1bd19 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
1bd1a 61 73 65 28 70 56 61 72 29 3b 0a 20 20 70 56 61  ase(pVar);.  pVa
1bd1b 72 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  r->flags = MEM_N
1bd1c 75 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33 45 72  ull;.  sqlite3Er
1bd1d 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54  ror(p->db, SQLIT
1bd1e 45 5f 4f 4b 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20  E_OK, 0);..  /* 
1bd1f 49 66 20 74 68 65 20 62 69 74 20 63 6f 72 72 65  If the bit corre
1bd20 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 69 73  sponding to this
1bd21 20 76 61 72 69 61 62 6c 65 20 69 6e 20 56 64 62   variable in Vdb
1bd22 65 2e 65 78 70 6d 61 73 6b 20 69 73 20 73 65 74  e.expmask is set
1bd23 2c 20 74 68 65 6e 20 0a 20 20 2a 2a 20 62 69 6e  , then .  ** bin
1bd24 64 69 6e 67 20 61 20 6e 65 77 20 76 61 6c 75 65  ding a new value
1bd25 20 74 6f 20 74 68 69 73 20 76 61 72 69 61 62 6c   to this variabl
1bd26 65 20 69 6e 76 61 6c 69 64 61 74 65 73 20 74 68  e invalidates th
1bd27 65 20 63 75 72 72 65 6e 74 20 71 75 65 72 79 20  e current query 
1bd28 70 6c 61 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  plan..  */.  if(
1bd29 20 70 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20   p->isPrepareV2 
1bd2a 26 26 0a 20 20 20 20 20 28 28 69 3c 33 32 20 26  &&.     ((i<32 &
1bd2b 26 20 70 2d 3e 65 78 70 6d 61 73 6b 20 26 20 28  & p->expmask & (
1bd2c 28 75 33 32 29 31 20 3c 3c 20 69 29 29 20 7c 7c  (u32)1 << i)) ||
1bd2d 20 70 2d 3e 65 78 70 6d 61 73 6b 3d 3d 30 78 66   p->expmask==0xf
1bd2e 66 66 66 66 66 66 66 29 0a 20 20 29 7b 0a 20 20  fffffff).  ){.  
1bd2f 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20 31    p->expired = 1
1bd30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
1bd31 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
1bd32 2a 2a 20 42 69 6e 64 20 61 20 74 65 78 74 20 6f  ** Bind a text o
1bd33 72 20 42 4c 4f 42 20 76 61 6c 75 65 2e 0a 2a 2f  r BLOB value..*/
1bd34 0a 73 74 61 74 69 63 20 69 6e 74 20 62 69 6e 64  .static int bind
1bd35 54 65 78 74 28 0a 20 20 73 71 6c 69 74 65 33 5f  Text(.  sqlite3_
1bd36 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 20 20 2f  stmt *pStmt,   /
1bd37 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20  * The statement 
1bd38 74 6f 20 62 69 6e 64 20 61 67 61 69 6e 73 74 20  to bind against 
1bd39 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 20  */.  int i,     
1bd3a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
1bd3b 6e 64 65 78 20 6f 66 20 74 68 65 20 70 61 72 61  ndex of the para
1bd3c 6d 65 74 65 72 20 74 6f 20 62 69 6e 64 20 2a 2f  meter to bind */
1bd3d 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
1bd3e 44 61 74 61 2c 20 20 20 20 20 2f 2a 20 50 6f 69  Data,     /* Poi
1bd3f 6e 74 65 72 20 74 6f 20 74 68 65 20 64 61 74 61  nter to the data
1bd40 20 74 6f 20 62 65 20 62 6f 75 6e 64 20 2a 2f 0a   to be bound */.
1bd41 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 20 20 20    int nData,    
1bd42 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1bd43 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64  er of bytes of d
1bd44 61 74 61 20 74 6f 20 62 65 20 62 6f 75 6e 64 20  ata to be bound 
1bd45 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c  */.  void (*xDel
1bd46 29 28 76 6f 69 64 2a 29 2c 20 20 20 2f 2a 20 44  )(void*),   /* D
1bd47 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20 74 68  estructor for th
1bd48 65 20 64 61 74 61 20 2a 2f 0a 20 20 75 38 20 65  e data */.  u8 e
1bd49 6e 63 6f 64 69 6e 67 20 20 20 20 20 20 20 20 20  ncoding         
1bd4a 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20 66     /* Encoding f
1bd4b 6f 72 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 29  or the data */.)
1bd4c 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56  {.  Vdbe *p = (V
1bd4d 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20 4d  dbe *)pStmt;.  M
1bd4e 65 6d 20 2a 70 56 61 72 3b 0a 20 20 69 6e 74 20  em *pVar;.  int 
1bd4f 72 63 3b 0a 0a 20 20 72 63 20 3d 20 76 64 62 65  rc;..  rc = vdbe
1bd50 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20  Unbind(p, i);.  
1bd51 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1bd52 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 7a 44 61  K ){.    if( zDa
1bd53 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 70  ta!=0 ){.      p
1bd54 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72 5b 69  Var = &p->aVar[i
1bd55 2d 31 5d 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  -1];.      rc = 
1bd56 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1bd57 74 53 74 72 28 70 56 61 72 2c 20 7a 44 61 74 61  tStr(pVar, zData
1bd58 2c 20 6e 44 61 74 61 2c 20 65 6e 63 6f 64 69 6e  , nData, encodin
1bd59 67 2c 20 78 44 65 6c 29 3b 0a 20 20 20 20 20 20  g, xDel);.      
1bd5a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1bd5b 4b 20 26 26 20 65 6e 63 6f 64 69 6e 67 21 3d 30  K && encoding!=0
1bd5c 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
1bd5d 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1bd5e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 61 72 2c  geEncoding(pVar,
1bd5f 20 45 4e 43 28 70 2d 3e 64 62 29 29 3b 0a 20 20   ENC(p->db));.  
1bd60 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
1bd61 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20  te3Error(p->db, 
1bd62 72 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 63  rc, 0);.      rc
1bd63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
1bd64 74 28 70 2d 3e 64 62 2c 20 72 63 29 3b 0a 20 20  t(p->db, rc);.  
1bd65 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
1bd66 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64  mutex_leave(p->d
1bd67 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20  b->mutex);.  }. 
1bd68 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a   return rc;.}...
1bd69 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61 20 62 6c 6f  /*.** Bind a blo
1bd6a 62 20 76 61 6c 75 65 20 74 6f 20 61 6e 20 53 51  b value to an SQ
1bd6b 4c 20 73 74 61 74 65 6d 65 6e 74 20 76 61 72 69  L statement vari
1bd6c 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  able..*/.SQLITE_
1bd6d 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1bd6e 62 69 6e 64 5f 62 6c 6f 62 28 0a 20 20 73 71 6c  bind_blob(.  sql
1bd6f 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1bd70 2c 20 0a 20 20 69 6e 74 20 69 2c 20 0a 20 20 63  , .  int i, .  c
1bd71 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 44 61 74 61  onst void *zData
1bd72 2c 20 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20  , .  int nData, 
1bd73 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28  .  void (*xDel)(
1bd74 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75  void*).){.  retu
1bd75 72 6e 20 62 69 6e 64 54 65 78 74 28 70 53 74 6d  rn bindText(pStm
1bd76 74 2c 20 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61  t, i, zData, nDa
1bd77 74 61 2c 20 78 44 65 6c 2c 20 30 29 3b 0a 7d 0a  ta, xDel, 0);.}.
1bd78 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1bd79 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62  qlite3_bind_doub
1bd7a 6c 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  le(sqlite3_stmt 
1bd7b 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 64  *pStmt, int i, d
1bd7c 6f 75 62 6c 65 20 72 56 61 6c 75 65 29 7b 0a 20  ouble rValue){. 
1bd7d 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 65 20   int rc;.  Vdbe 
1bd7e 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74  *p = (Vdbe *)pSt
1bd7f 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 65 55  mt;.  rc = vdbeU
1bd80 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69  nbind(p, i);.  i
1bd81 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1bd82 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1bd83 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28  dbeMemSetDouble(
1bd84 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 72  &p->aVar[i-1], r
1bd85 56 61 6c 75 65 29 3b 0a 20 20 20 20 73 71 6c 69  Value);.    sqli
1bd86 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
1bd87 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  p->db->mutex);. 
1bd88 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1bd89 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
1bd8a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
1bd8b 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  t(sqlite3_stmt *
1bd8c 70 2c 20 69 6e 74 20 69 2c 20 69 6e 74 20 69 56  p, int i, int iV
1bd8d 61 6c 75 65 29 7b 0a 20 20 72 65 74 75 72 6e 20  alue){.  return 
1bd8e 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
1bd8f 36 34 28 70 2c 20 69 2c 20 28 69 36 34 29 69 56  64(p, i, (i64)iV
1bd90 61 6c 75 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  alue);.}.SQLITE_
1bd91 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1bd92 62 69 6e 64 5f 69 6e 74 36 34 28 73 71 6c 69 74  bind_int64(sqlit
1bd93 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20  e3_stmt *pStmt, 
1bd94 69 6e 74 20 69 2c 20 73 71 6c 69 74 65 5f 69 6e  int i, sqlite_in
1bd95 74 36 34 20 69 56 61 6c 75 65 29 7b 0a 20 20 69  t64 iValue){.  i
1bd96 6e 74 20 72 63 3b 0a 20 20 56 64 62 65 20 2a 70  nt rc;.  Vdbe *p
1bd97 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74   = (Vdbe *)pStmt
1bd98 3b 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62  ;.  rc = vdbeUnb
1bd99 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28  ind(p, i);.  if(
1bd9a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
1bd9b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1bd9c 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 26 70 2d  eMemSetInt64(&p-
1bd9d 3e 61 56 61 72 5b 69 2d 31 5d 2c 20 69 56 61 6c  >aVar[i-1], iVal
1bd9e 75 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ue);.    sqlite3
1bd9f 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e  _mutex_leave(p->
1bda0 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a  db->mutex);.  }.
1bda1 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53    return rc;.}.S
1bda2 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1bda3 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28  lite3_bind_null(
1bda4 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1bda5 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69  tmt, int i){.  i
1bda6 6e 74 20 72 63 3b 0a 20 20 56 64 62 65 20 2a 70  nt rc;.  Vdbe *p
1bda7 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74 3b   = (Vdbe*)pStmt;
1bda8 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e 62 69  .  rc = vdbeUnbi
1bda9 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 66 28 20  nd(p, i);.  if( 
1bdaa 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1bdab 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
1bdac 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e  ex_leave(p->db->
1bdad 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65  mutex);.  }.  re
1bdae 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54  turn rc;.}.SQLIT
1bdaf 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
1bdb0 33 5f 62 69 6e 64 5f 74 65 78 74 28 20 0a 20 20  3_bind_text( .  
1bdb1 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
1bdb2 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69 2c 20 0a  tmt, .  int i, .
1bdb3 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
1bdb4 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e 44 61 74  ata, .  int nDat
1bdb5 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65  a, .  void (*xDe
1bdb6 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 72  l)(void*).){.  r
1bdb7 65 74 75 72 6e 20 62 69 6e 64 54 65 78 74 28 70  eturn bindText(p
1bdb8 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74 61 2c 20  Stmt, i, zData, 
1bdb9 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20 53 51 4c  nData, xDel, SQL
1bdba 49 54 45 5f 55 54 46 38 29 3b 0a 7d 0a 23 69 66  ITE_UTF8);.}.#if
1bdbb 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1bdbc 5f 55 54 46 31 36 0a 53 51 4c 49 54 45 5f 41 50  _UTF16.SQLITE_AP
1bdbd 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
1bdbe 6e 64 5f 74 65 78 74 31 36 28 0a 20 20 73 71 6c  nd_text16(.  sql
1bdbf 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
1bdc0 2c 20 0a 20 20 69 6e 74 20 69 2c 20 0a 20 20 63  , .  int i, .  c
1bdc1 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 44 61 74 61  onst void *zData
1bdc2 2c 20 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20  , .  int nData, 
1bdc3 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28  .  void (*xDel)(
1bdc4 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 72 65 74 75  void*).){.  retu
1bdc5 72 6e 20 62 69 6e 64 54 65 78 74 28 70 53 74 6d  rn bindText(pStm
1bdc6 74 2c 20 69 2c 20 7a 44 61 74 61 2c 20 6e 44 61  t, i, zData, nDa
1bdc7 74 61 2c 20 78 44 65 6c 2c 20 53 51 4c 49 54 45  ta, xDel, SQLITE
1bdc8 5f 55 54 46 31 36 4e 41 54 49 56 45 29 3b 0a 7d  _UTF16NATIVE);.}
1bdc9 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1bdca 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a  E_OMIT_UTF16 */.
1bdcb 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1bdcc 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75  qlite3_bind_valu
1bdcd 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
1bdce 70 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 63 6f  pStmt, int i, co
1bdcf 6e 73 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  nst sqlite3_valu
1bdd0 65 20 2a 70 56 61 6c 75 65 29 7b 0a 20 20 69 6e  e *pValue){.  in
1bdd1 74 20 72 63 3b 0a 20 20 73 77 69 74 63 68 28 20  t rc;.  switch( 
1bdd2 70 56 61 6c 75 65 2d 3e 74 79 70 65 20 29 7b 0a  pValue->type ){.
1bdd3 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
1bdd4 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20  INTEGER: {.     
1bdd5 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
1bdd6 6e 64 5f 69 6e 74 36 34 28 70 53 74 6d 74 2c 20  nd_int64(pStmt, 
1bdd7 69 2c 20 70 56 61 6c 75 65 2d 3e 75 2e 69 29 3b  i, pValue->u.i);
1bdd8 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1bdd9 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
1bdda 49 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20  ITE_FLOAT: {.   
1bddb 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
1bddc 62 69 6e 64 5f 64 6f 75 62 6c 65 28 70 53 74 6d  bind_double(pStm
1bddd 74 2c 20 69 2c 20 70 56 61 6c 75 65 2d 3e 72 29  t, i, pValue->r)
1bdde 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1bddf 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
1bde0 4c 49 54 45 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20  LITE_BLOB: {.   
1bde1 20 20 20 69 66 28 20 70 56 61 6c 75 65 2d 3e 66     if( pValue->f
1bde2 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20  lags & MEM_Zero 
1bde3 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
1bde4 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72  sqlite3_bind_zer
1bde5 6f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 69 2c 20  oblob(pStmt, i, 
1bde6 70 56 61 6c 75 65 2d 3e 75 2e 6e 5a 65 72 6f 29  pValue->u.nZero)
1bde7 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1bde8 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1bde9 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 70 53  te3_bind_blob(pS
1bdea 74 6d 74 2c 20 69 2c 20 70 56 61 6c 75 65 2d 3e  tmt, i, pValue->
1bdeb 7a 2c 20 70 56 61 6c 75 65 2d 3e 6e 2c 53 51 4c  z, pValue->n,SQL
1bdec 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
1bded 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
1bdee 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
1bdef 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58 54 3a  ase SQLITE_TEXT:
1bdf0 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 62 69   {.      rc = bi
1bdf1 6e 64 54 65 78 74 28 70 53 74 6d 74 2c 69 2c 20  ndText(pStmt,i, 
1bdf2 20 70 56 61 6c 75 65 2d 3e 7a 2c 20 70 56 61 6c   pValue->z, pVal
1bdf3 75 65 2d 3e 6e 2c 20 53 51 4c 49 54 45 5f 54 52  ue->n, SQLITE_TR
1bdf4 41 4e 53 49 45 4e 54 2c 0a 20 20 20 20 20 20 20  ANSIENT,.       
1bdf5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bdf6 20 20 20 20 20 20 20 70 56 61 6c 75 65 2d 3e 65         pValue->e
1bdf7 6e 63 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  nc);.      break
1bdf8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
1bdf9 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 63 20  ult: {.      rc 
1bdfa 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e  = sqlite3_bind_n
1bdfb 75 6c 6c 28 70 53 74 6d 74 2c 20 69 29 3b 0a 20  ull(pStmt, i);. 
1bdfc 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1bdfd 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
1bdfe 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  c;.}.SQLITE_API 
1bdff 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
1be00 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65  _zeroblob(sqlite
1be01 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
1be02 6e 74 20 69 2c 20 69 6e 74 20 6e 29 7b 0a 20 20  nt i, int n){.  
1be03 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 65 20 2a  int rc;.  Vdbe *
1be04 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d  p = (Vdbe *)pStm
1be05 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 65 55 6e  t;.  rc = vdbeUn
1be06 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20 69 66  bind(p, i);.  if
1be07 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1be08 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1be09 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c 6f 62  beMemSetZeroBlob
1be0a 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d 2c 20  (&p->aVar[i-1], 
1be0b 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  n);.    sqlite3_
1be0c 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64  mutex_leave(p->d
1be0d 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20  b->mutex);.  }. 
1be0e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1be0f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
1be10 6e 75 6d 62 65 72 20 6f 66 20 77 69 6c 64 63 61  number of wildca
1be11 72 64 73 20 74 68 61 74 20 63 61 6e 20 62 65 20  rds that can be 
1be12 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 6f 75 6e  potentially boun
1be13 64 20 74 6f 2e 0a 2a 2a 20 54 68 69 73 20 72 6f  d to..** This ro
1be14 75 74 69 6e 65 20 69 73 20 61 64 64 65 64 20 74  utine is added t
1be15 6f 20 73 75 70 70 6f 72 74 20 44 42 44 3a 3a 53  o support DBD::S
1be16 51 4c 69 74 65 2e 20 20 0a 2a 2f 0a 53 51 4c 49  QLite.  .*/.SQLI
1be17 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1be18 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
1be19 72 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f  r_count(sqlite3_
1be1a 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20  stmt *pStmt){.  
1be1b 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a  Vdbe *p = (Vdbe*
1be1c 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75 72 6e  )pStmt;.  return
1be1d 20 70 20 3f 20 70 2d 3e 6e 56 61 72 20 3a 20 30   p ? p->nVar : 0
1be1e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  ;.}../*.** Creat
1be1f 65 20 61 20 6d 61 70 70 69 6e 67 20 66 72 6f 6d  e a mapping from
1be20 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72   variable number
1be21 73 20 74 6f 20 76 61 72 69 61 62 6c 65 20 6e 61  s to variable na
1be22 6d 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 56 64  mes.** in the Vd
1be23 62 65 2e 61 7a 56 61 72 5b 5d 20 61 72 72 61 79  be.azVar[] array
1be24 2c 20 69 66 20 73 75 63 68 20 61 20 6d 61 70 70  , if such a mapp
1be25 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72  ing does not alr
1be26 65 61 64 79 0a 2a 2a 20 65 78 69 73 74 2e 0a 2a  eady.** exist..*
1be27 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 72  /.static void cr
1be28 65 61 74 65 56 61 72 4d 61 70 28 56 64 62 65 20  eateVarMap(Vdbe 
1be29 2a 70 29 7b 0a 20 20 69 66 28 20 21 70 2d 3e 6f  *p){.  if( !p->o
1be2a 6b 56 61 72 20 29 7b 0a 20 20 20 20 69 6e 74 20  kVar ){.    int 
1be2b 6a 3b 0a 20 20 20 20 4f 70 20 2a 70 4f 70 3b 0a  j;.    Op *pOp;.
1be2c 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1be2d 78 5f 65 6e 74 65 72 28 70 2d 3e 64 62 2d 3e 6d  x_enter(p->db->m
1be2e 75 74 65 78 29 3b 0a 20 20 20 20 2f 2a 20 54 68  utex);.    /* Th
1be2f 65 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e  e race condition
1be30 20 68 65 72 65 20 69 73 20 68 61 72 6d 6c 65 73   here is harmles
1be31 73 2e 20 20 49 66 20 74 77 6f 20 74 68 72 65 61  s.  If two threa
1be32 64 73 20 63 61 6c 6c 20 74 68 69 73 0a 20 20 20  ds call this.   
1be33 20 2a 2a 20 72 6f 75 74 69 6e 65 20 6f 6e 20 74   ** routine on t
1be34 68 65 20 73 61 6d 65 20 56 64 62 65 20 61 74 20  he same Vdbe at 
1be35 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2c 20 74  the same time, t
1be36 68 65 79 20 62 6f 74 68 20 6d 69 67 68 74 20 65  hey both might e
1be37 6e 64 0a 20 20 20 20 2a 2a 20 75 70 20 69 6e 69  nd.    ** up ini
1be38 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 20 56 64  tializing the Vd
1be39 62 65 2e 61 7a 56 61 72 5b 5d 20 61 72 72 61 79  be.azVar[] array
1be3a 2e 20 20 54 68 61 74 20 69 73 20 61 20 6c 69 74  .  That is a lit
1be3b 74 6c 65 20 65 78 74 72 61 0a 20 20 20 20 2a 2a  tle extra.    **
1be3c 20 77 6f 72 6b 20 62 75 74 20 69 74 20 72 65 73   work but it res
1be3d 75 6c 74 73 20 69 6e 20 74 68 65 20 73 61 6d 65  ults in the same
1be3e 20 61 6e 73 77 65 72 2e 0a 20 20 20 20 2a 2f 0a   answer..    */.
1be3f 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 4f 70      for(j=0, pOp
1be40 3d 70 2d 3e 61 4f 70 3b 20 6a 3c 70 2d 3e 6e 4f  =p->aOp; j<p->nO
1be41 70 3b 20 6a 2b 2b 2c 20 70 4f 70 2b 2b 29 7b 0a  p; j++, pOp++){.
1be42 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f        if( pOp->o
1be43 70 63 6f 64 65 3d 3d 4f 50 5f 56 61 72 69 61 62  pcode==OP_Variab
1be44 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  le ){.        as
1be45 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20  sert( pOp->p1>0 
1be46 26 26 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e  && pOp->p1<=p->n
1be47 56 61 72 20 29 3b 0a 20 20 20 20 20 20 20 20 70  Var );.        p
1be48 2d 3e 61 7a 56 61 72 5b 70 4f 70 2d 3e 70 31 2d  ->azVar[pOp->p1-
1be49 31 5d 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a  1] = pOp->p4.z;.
1be4a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1be4b 20 20 70 2d 3e 6f 6b 56 61 72 20 3d 20 31 3b 0a    p->okVar = 1;.
1be4c 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1be4d 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d  x_leave(p->db->m
1be4e 75 74 65 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  utex);.  }.}../*
1be4f 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
1be50 61 6d 65 20 6f 66 20 61 20 77 69 6c 64 63 61 72  ame of a wildcar
1be51 64 20 70 61 72 61 6d 65 74 65 72 2e 20 20 52 65  d parameter.  Re
1be52 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65  turn NULL if the
1be53 20 69 6e 64 65 78 0a 2a 2a 20 69 73 20 6f 75 74   index.** is out
1be54 20 6f 66 20 72 61 6e 67 65 20 6f 72 20 69 66 20   of range or if 
1be55 74 68 65 20 77 69 6c 64 63 61 72 64 20 69 73 20  the wildcard is 
1be56 75 6e 6e 61 6d 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  unnamed..**.** T
1be57 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c 77  he result is alw
1be58 61 79 73 20 55 54 46 2d 38 2e 0a 2a 2f 0a 53 51  ays UTF-8..*/.SQ
1be59 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63  LITE_API const c
1be5a 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 62 69 6e  har *sqlite3_bin
1be5b 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65  d_parameter_name
1be5c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1be5d 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20  Stmt, int i){.  
1be5e 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a  Vdbe *p = (Vdbe*
1be5f 29 70 53 74 6d 74 3b 0a 20 20 69 66 28 20 70 3d  )pStmt;.  if( p=
1be60 3d 30 20 7c 7c 20 69 3c 31 20 7c 7c 20 69 3e 70  =0 || i<1 || i>p
1be61 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 72 65  ->nVar ){.    re
1be62 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 63 72  turn 0;.  }.  cr
1be63 65 61 74 65 56 61 72 4d 61 70 28 70 29 3b 0a 20  eateVarMap(p);. 
1be64 20 72 65 74 75 72 6e 20 70 2d 3e 61 7a 56 61 72   return p->azVar
1be65 5b 69 2d 31 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  [i-1];.}../*.** 
1be66 47 69 76 65 6e 20 61 20 77 69 6c 64 63 61 72 64  Given a wildcard
1be67 20 70 61 72 61 6d 65 74 65 72 20 6e 61 6d 65 2c   parameter name,
1be68 20 72 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65   return the inde
1be69 78 20 6f 66 20 74 68 65 20 76 61 72 69 61 62 6c  x of the variabl
1be6a 65 0a 2a 2a 20 77 69 74 68 20 74 68 61 74 20 6e  e.** with that n
1be6b 61 6d 65 2e 20 20 49 66 20 74 68 65 72 65 20 69  ame.  If there i
1be6c 73 20 6e 6f 20 76 61 72 69 61 62 6c 65 20 77 69  s no variable wi
1be6d 74 68 20 74 68 65 20 67 69 76 65 6e 20 6e 61 6d  th the given nam
1be6e 65 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 0a  e,.** return 0..
1be6f 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1be70 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70  t sqlite3_bind_p
1be71 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 73  arameter_index(s
1be72 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
1be73 6d 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  mt, const char *
1be74 7a 4e 61 6d 65 29 7b 0a 20 20 56 64 62 65 20 2a  zName){.  Vdbe *
1be75 70 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74  p = (Vdbe*)pStmt
1be76 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  ;.  int i;.  if(
1be77 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74   p==0 ){.    ret
1be78 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 63 72 65  urn 0;.  }.  cre
1be79 61 74 65 56 61 72 4d 61 70 28 70 29 3b 20 0a 20  ateVarMap(p); . 
1be7a 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20   if( zName ){.  
1be7b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
1be7c 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nVar; i++){.    
1be7d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20    const char *z 
1be7e 3d 20 70 2d 3e 61 7a 56 61 72 5b 69 5d 3b 0a 20  = p->azVar[i];. 
1be7f 20 20 20 20 20 69 66 28 20 7a 20 26 26 20 73 74       if( z && st
1be80 72 63 6d 70 28 7a 2c 7a 4e 61 6d 65 29 3d 3d 30  rcmp(z,zName)==0
1be81 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
1be82 72 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d 0a  rn i+1;.      }.
1be83 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1be84 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  rn 0;.}../*.** T
1be85 72 61 6e 73 66 65 72 20 61 6c 6c 20 62 69 6e 64  ransfer all bind
1be86 69 6e 67 73 20 66 72 6f 6d 20 74 68 65 20 66 69  ings from the fi
1be87 72 73 74 20 73 74 61 74 65 6d 65 6e 74 20 6f 76  rst statement ov
1be88 65 72 20 74 6f 20 74 68 65 20 73 65 63 6f 6e 64  er to the second
1be89 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1be8a 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 54  ATE int sqlite3T
1be8b 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67 73 28  ransferBindings(
1be8c 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 46  sqlite3_stmt *pF
1be8d 72 6f 6d 53 74 6d 74 2c 20 73 71 6c 69 74 65 33  romStmt, sqlite3
1be8e 5f 73 74 6d 74 20 2a 70 54 6f 53 74 6d 74 29 7b  _stmt *pToStmt){
1be8f 0a 20 20 56 64 62 65 20 2a 70 46 72 6f 6d 20 3d  .  Vdbe *pFrom =
1be90 20 28 56 64 62 65 2a 29 70 46 72 6f 6d 53 74 6d   (Vdbe*)pFromStm
1be91 74 3b 0a 20 20 56 64 62 65 20 2a 70 54 6f 20 3d  t;.  Vdbe *pTo =
1be92 20 28 56 64 62 65 2a 29 70 54 6f 53 74 6d 74 3b   (Vdbe*)pToStmt;
1be93 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65  .  int i;.  asse
1be94 72 74 28 20 70 54 6f 2d 3e 64 62 3d 3d 70 46 72  rt( pTo->db==pFr
1be95 6f 6d 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65  om->db );.  asse
1be96 72 74 28 20 70 54 6f 2d 3e 6e 56 61 72 3d 3d 70  rt( pTo->nVar==p
1be97 46 72 6f 6d 2d 3e 6e 56 61 72 20 29 3b 0a 20 20  From->nVar );.  
1be98 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1be99 74 65 72 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74  ter(pTo->db->mut
1be9a 65 78 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  ex);.  for(i=0; 
1be9b 69 3c 70 46 72 6f 6d 2d 3e 6e 56 61 72 3b 20 69  i<pFrom->nVar; i
1be9c 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
1be9d 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 70 54 6f  VdbeMemMove(&pTo
1be9e 2d 3e 61 56 61 72 5b 69 5d 2c 20 26 70 46 72 6f  ->aVar[i], &pFro
1be9f 6d 2d 3e 61 56 61 72 5b 69 5d 29 3b 0a 20 20 7d  m->aVar[i]);.  }
1bea0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
1bea1 5f 6c 65 61 76 65 28 70 54 6f 2d 3e 64 62 2d 3e  _leave(pTo->db->
1bea2 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
1bea3 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23   SQLITE_OK;.}..#
1bea4 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1bea5 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a  IT_DEPRECATED./*
1bea6 0a 2a 2a 20 44 65 70 72 65 63 61 74 65 64 20 65  .** Deprecated e
1bea7 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66 61 63  xternal interfac
1bea8 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f 63 6f 72  e.  Internal/cor
1bea9 65 20 53 51 4c 69 74 65 20 63 6f 64 65 0a 2a 2a  e SQLite code.**
1beaa 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73 71 6c   should call sql
1beab 69 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e 64  ite3TransferBind
1beac 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 49 73 20 69  ings..**.** Is i
1bead 73 20 6d 69 73 75 73 65 20 74 6f 20 63 61 6c 6c  s misuse to call
1beae 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   this routine wi
1beaf 74 68 20 73 74 61 74 65 6d 65 6e 74 73 20 66 72  th statements fr
1beb0 6f 6d 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20  om different.** 
1beb1 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1beb2 69 6f 6e 73 2e 20 20 42 75 74 20 61 73 20 74 68  ions.  But as th
1beb3 69 73 20 69 73 20 61 20 64 65 70 72 65 63 61 74  is is a deprecat
1beb4 65 64 20 69 6e 74 65 72 66 61 63 65 2c 20 77 65  ed interface, we
1beb5 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 6f 74  .** will not bot
1beb6 68 65 72 20 74 6f 20 63 68 65 63 6b 20 66 6f 72  her to check for
1beb7 20 74 68 61 74 20 63 6f 6e 64 69 74 69 6f 6e 2e   that condition.
1beb8 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 74 77  .**.** If the tw
1beb9 6f 20 73 74 61 74 65 6d 65 6e 74 73 20 63 6f 6e  o statements con
1beba 74 61 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74  tain a different
1bebb 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 6e 64 69   number of bindi
1bebc 6e 67 73 2c 20 74 68 65 6e 0a 2a 2a 20 61 6e 20  ngs, then.** an 
1bebd 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 69 73 20  SQLITE_ERROR is 
1bebe 72 65 74 75 72 6e 65 64 2e 20 20 4e 6f 74 68 69  returned.  Nothi
1bebf 6e 67 20 65 6c 73 65 20 63 61 6e 20 67 6f 20 77  ng else can go w
1bec0 72 6f 6e 67 2c 20 73 6f 20 6f 74 68 65 72 77 69  rong, so otherwi
1bec1 73 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20  se.** SQLITE_OK 
1bec2 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
1bec3 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1bec4 71 6c 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f  qlite3_transfer_
1bec5 62 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33  bindings(sqlite3
1bec6 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53 74 6d 74  _stmt *pFromStmt
1bec7 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  , sqlite3_stmt *
1bec8 70 54 6f 53 74 6d 74 29 7b 0a 20 20 56 64 62 65  pToStmt){.  Vdbe
1bec9 20 2a 70 46 72 6f 6d 20 3d 20 28 56 64 62 65 2a   *pFrom = (Vdbe*
1beca 29 70 46 72 6f 6d 53 74 6d 74 3b 0a 20 20 56 64  )pFromStmt;.  Vd
1becb 62 65 20 2a 70 54 6f 20 3d 20 28 56 64 62 65 2a  be *pTo = (Vdbe*
1becc 29 70 54 6f 53 74 6d 74 3b 0a 20 20 69 66 28 20  )pToStmt;.  if( 
1becd 70 46 72 6f 6d 2d 3e 6e 56 61 72 21 3d 70 54 6f  pFrom->nVar!=pTo
1bece 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 72 65  ->nVar ){.    re
1becf 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
1bed0 52 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 54 6f  R;.  }.  if( pTo
1bed1 2d 3e 69 73 50 72 65 70 61 72 65 56 32 20 26 26  ->isPrepareV2 &&
1bed2 20 70 54 6f 2d 3e 65 78 70 6d 61 73 6b 20 29 7b   pTo->expmask ){
1bed3 0a 20 20 20 20 70 54 6f 2d 3e 65 78 70 69 72 65  .    pTo->expire
1bed4 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28  d = 1;.  }.  if(
1bed5 20 70 46 72 6f 6d 2d 3e 69 73 50 72 65 70 61 72   pFrom->isPrepar
1bed6 65 56 32 20 26 26 20 70 46 72 6f 6d 2d 3e 65 78  eV2 && pFrom->ex
1bed7 70 6d 61 73 6b 20 29 7b 0a 20 20 20 20 70 46 72  pmask ){.    pFr
1bed8 6f 6d 2d 3e 65 78 70 69 72 65 64 20 3d 20 31 3b  om->expired = 1;
1bed9 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71  .  }.  return sq
1beda 6c 69 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e  lite3TransferBin
1bedb 64 69 6e 67 73 28 70 46 72 6f 6d 53 74 6d 74 2c  dings(pFromStmt,
1bedc 20 70 54 6f 53 74 6d 74 29 3b 0a 7d 0a 23 65 6e   pToStmt);.}.#en
1bedd 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  dif../*.** Retur
1bede 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2a 20 64  n the sqlite3* d
1bedf 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74  atabase handle t
1bee0 6f 20 77 68 69 63 68 20 74 68 65 20 70 72 65 70  o which the prep
1bee1 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 67  ared statement g
1bee2 69 76 65 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 61  iven.** in the a
1bee3 72 67 75 6d 65 6e 74 20 62 65 6c 6f 6e 67 73 2e  rgument belongs.
1bee4 20 20 54 68 69 73 20 69 73 20 74 68 65 20 73 61    This is the sa
1bee5 6d 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  me database hand
1bee6 6c 65 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 74  le that was.** t
1bee7 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
1bee8 74 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  t to the sqlite3
1bee9 5f 70 72 65 70 61 72 65 28 29 20 74 68 61 74 20  _prepare() that 
1beea 77 61 73 20 75 73 65 64 20 74 6f 20 63 72 65 61  was used to crea
1beeb 74 65 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 6d  te.** the statem
1beec 65 6e 74 20 69 6e 20 74 68 65 20 66 69 72 73 74  ent in the first
1beed 20 70 6c 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54   place..*/.SQLIT
1beee 45 5f 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73  E_API sqlite3 *s
1beef 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65  qlite3_db_handle
1bef0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
1bef1 53 74 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e 20  Stmt){.  return 
1bef2 70 53 74 6d 74 20 3f 20 28 28 56 64 62 65 2a 29  pStmt ? ((Vdbe*)
1bef3 70 53 74 6d 74 29 2d 3e 64 62 20 3a 20 30 3b 0a  pStmt)->db : 0;.
1bef4 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1bef5 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
1bef6 20 6e 65 78 74 20 70 72 65 70 61 72 65 64 20 73   next prepared s
1bef7 74 61 74 65 6d 65 6e 74 20 61 66 74 65 72 20 70  tatement after p
1bef8 53 74 6d 74 20 61 73 73 6f 63 69 61 74 65 64 0a  Stmt associated.
1bef9 2a 2a 20 77 69 74 68 20 64 61 74 61 62 61 73 65  ** with database
1befa 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 44 62 2e   connection pDb.
1befb 20 20 49 66 20 70 53 74 6d 74 20 69 73 20 4e 55    If pStmt is NU
1befc 4c 4c 2c 20 72 65 74 75 72 6e 20 74 68 65 20 66  LL, return the f
1befd 69 72 73 74 0a 2a 2a 20 70 72 65 70 61 72 65 64  irst.** prepared
1befe 20 73 74 61 74 65 6d 65 6e 74 20 66 6f 72 20 74   statement for t
1beff 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
1bf00 65 63 74 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20  ection.  Return 
1bf01 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 0a 2a 2a  NULL if there.**
1bf02 20 61 72 65 20 6e 6f 20 6d 6f 72 65 2e 0a 2a 2f   are no more..*/
1bf03 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
1bf04 74 65 33 5f 73 74 6d 74 20 2a 73 71 6c 69 74 65  te3_stmt *sqlite
1bf05 33 5f 6e 65 78 74 5f 73 74 6d 74 28 73 71 6c 69  3_next_stmt(sqli
1bf06 74 65 33 20 2a 70 44 62 2c 20 73 71 6c 69 74 65  te3 *pDb, sqlite
1bf07 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a  3_stmt *pStmt){.
1bf08 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
1bf09 70 4e 65 78 74 3b 0a 20 20 73 71 6c 69 74 65 33  pNext;.  sqlite3
1bf0a 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 44 62  _mutex_enter(pDb
1bf0b 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20  ->mutex);.  if( 
1bf0c 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20  pStmt==0 ){.    
1bf0d 70 4e 65 78 74 20 3d 20 28 73 71 6c 69 74 65 33  pNext = (sqlite3
1bf0e 5f 73 74 6d 74 2a 29 70 44 62 2d 3e 70 56 64 62  _stmt*)pDb->pVdb
1bf0f 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  e;.  }else{.    
1bf10 70 4e 65 78 74 20 3d 20 28 73 71 6c 69 74 65 33  pNext = (sqlite3
1bf11 5f 73 74 6d 74 2a 29 28 28 56 64 62 65 2a 29 70  _stmt*)((Vdbe*)p
1bf12 53 74 6d 74 29 2d 3e 70 4e 65 78 74 3b 0a 20 20  Stmt)->pNext;.  
1bf13 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
1bf14 78 5f 6c 65 61 76 65 28 70 44 62 2d 3e 6d 75 74  x_leave(pDb->mut
1bf15 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e  ex);.  return pN
1bf16 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ext;.}../*.** Re
1bf17 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f  turn the value o
1bf18 66 20 61 20 73 74 61 74 75 73 20 63 6f 75 6e 74  f a status count
1bf19 65 72 20 66 6f 72 20 61 20 70 72 65 70 61 72 65  er for a prepare
1bf1a 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53  d statement.*/.S
1bf1b 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
1bf1c 6c 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75  lite3_stmt_statu
1bf1d 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  s(sqlite3_stmt *
1bf1e 70 53 74 6d 74 2c 20 69 6e 74 20 6f 70 2c 20 69  pStmt, int op, i
1bf1f 6e 74 20 72 65 73 65 74 46 6c 61 67 29 7b 0a 20  nt resetFlag){. 
1bf20 20 56 64 62 65 20 2a 70 56 64 62 65 20 3d 20 28   Vdbe *pVdbe = (
1bf21 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69  Vdbe*)pStmt;.  i
1bf22 6e 74 20 76 20 3d 20 70 56 64 62 65 2d 3e 61 43  nt v = pVdbe->aC
1bf23 6f 75 6e 74 65 72 5b 6f 70 2d 31 5d 3b 0a 20 20  ounter[op-1];.  
1bf24 69 66 28 20 72 65 73 65 74 46 6c 61 67 20 29 20  if( resetFlag ) 
1bf25 70 56 64 62 65 2d 3e 61 43 6f 75 6e 74 65 72 5b  pVdbe->aCounter[
1bf26 6f 70 2d 31 5d 20 3d 20 30 3b 0a 20 20 72 65 74  op-1] = 0;.  ret
1bf27 75 72 6e 20 76 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  urn v;.}../*****
1bf28 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
1bf29 20 76 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a   vdbeapi.c *****
1bf2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf2c 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
1bf2d 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
1bf2e 66 69 6c 65 20 76 64 62 65 2e 63 20 2a 2a 2a 2a  file vdbe.c ****
1bf2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf31 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
1bf32 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
1bf33 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
1bf34 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
1bf35 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
1bf36 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
1bf37 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
1bf38 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
1bf39 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
1bf3a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
1bf3b 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
1bf3c 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
1bf3d 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
1bf3e 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
1bf3f 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
1bf40 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
1bf41 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
1bf42 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
1bf43 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
1bf44 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
1bf45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf48 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1bf49 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
1bf4a 74 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d  this file implem
1bf4b 65 6e 74 73 20 65 78 65 63 75 74 69 6f 6e 20 6d  ents execution m
1bf4c 65 74 68 6f 64 20 6f 66 20 74 68 65 20 0a 2a 2a  ethod of the .**
1bf4d 20 56 69 72 74 75 61 6c 20 44 61 74 61 62 61 73   Virtual Databas
1bf4e 65 20 45 6e 67 69 6e 65 20 28 56 44 42 45 29 2e  e Engine (VDBE).
1bf4f 20 20 41 20 73 65 70 61 72 61 74 65 20 66 69 6c    A separate fil
1bf50 65 20 28 22 76 64 62 65 61 75 78 2e 63 22 29 0a  e ("vdbeaux.c").
1bf51 2a 2a 20 68 61 6e 64 6c 65 73 20 68 6f 75 73 65  ** handles house
1bf52 6b 65 65 70 69 6e 67 20 64 65 74 61 69 6c 73 20  keeping details 
1bf53 73 75 63 68 20 61 73 20 63 72 65 61 74 69 6e 67  such as creating
1bf54 20 61 6e 64 20 64 65 6c 65 74 69 6e 67 0a 2a 2a   and deleting.**
1bf55 20 56 44 42 45 20 69 6e 73 74 61 6e 63 65 73 2e   VDBE instances.
1bf56 20 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 73    This file is s
1bf57 6f 6c 65 6c 79 20 69 6e 74 65 72 65 73 74 65 64  olely interested
1bf58 20 69 6e 20 65 78 65 63 75 74 69 6e 67 0a 2a 2a   in executing.**
1bf59 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61   the VDBE progra
1bf5a 6d 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20  m..**.** In the 
1bf5b 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66 61  external interfa
1bf5c 63 65 2c 20 61 6e 20 22 73 71 6c 69 74 65 33 5f  ce, an "sqlite3_
1bf5d 73 74 6d 74 2a 22 20 69 73 20 61 6e 20 6f 70 61  stmt*" is an opa
1bf5e 71 75 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74  que pointer.** t
1bf5f 6f 20 61 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20  o a VDBE..**.** 
1bf60 54 68 65 20 53 51 4c 20 70 61 72 73 65 72 20 67  The SQL parser g
1bf61 65 6e 65 72 61 74 65 73 20 61 20 70 72 6f 67 72  enerates a progr
1bf62 61 6d 20 77 68 69 63 68 20 69 73 20 74 68 65 6e  am which is then
1bf63 20 65 78 65 63 75 74 65 64 20 62 79 0a 2a 2a 20   executed by.** 
1bf64 74 68 65 20 56 44 42 45 20 74 6f 20 64 6f 20 74  the VDBE to do t
1bf65 68 65 20 77 6f 72 6b 20 6f 66 20 74 68 65 20 53  he work of the S
1bf66 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 56  QL statement.  V
1bf67 44 42 45 20 70 72 6f 67 72 61 6d 73 20 61 72 65  DBE programs are
1bf68 20 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 69 6e 20   .** similar in 
1bf69 66 6f 72 6d 20 74 6f 20 61 73 73 65 6d 62 6c 79  form to assembly
1bf6a 20 6c 61 6e 67 75 61 67 65 2e 20 20 54 68 65 20   language.  The 
1bf6b 70 72 6f 67 72 61 6d 20 63 6f 6e 73 69 73 74 73  program consists
1bf6c 20 6f 66 0a 2a 2a 20 61 20 6c 69 6e 65 61 72 20   of.** a linear 
1bf6d 73 65 71 75 65 6e 63 65 20 6f 66 20 6f 70 65 72  sequence of oper
1bf6e 61 74 69 6f 6e 73 2e 20 20 45 61 63 68 20 6f 70  ations.  Each op
1bf6f 65 72 61 74 69 6f 6e 20 68 61 73 20 61 6e 20 6f  eration has an o
1bf70 70 63 6f 64 65 20 0a 2a 2a 20 61 6e 64 20 35 20  pcode .** and 5 
1bf71 6f 70 65 72 61 6e 64 73 2e 20 20 4f 70 65 72 61  operands.  Opera
1bf72 6e 64 73 20 50 31 2c 20 50 32 2c 20 61 6e 64 20  nds P1, P2, and 
1bf73 50 33 20 61 72 65 20 69 6e 74 65 67 65 72 73 2e  P3 are integers.
1bf74 20 20 4f 70 65 72 61 6e 64 20 50 34 20 0a 2a 2a    Operand P4 .**
1bf75 20 69 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69   is a null-termi
1bf76 6e 61 74 65 64 20 73 74 72 69 6e 67 2e 20 20 4f  nated string.  O
1bf77 70 65 72 61 6e 64 20 50 35 20 69 73 20 61 6e 20  perand P5 is an 
1bf78 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61 63 74  unsigned charact
1bf79 65 72 2e 0a 2a 2a 20 46 65 77 20 6f 70 63 6f 64  er..** Few opcod
1bf7a 65 73 20 75 73 65 20 61 6c 6c 20 35 20 6f 70 65  es use all 5 ope
1bf7b 72 61 6e 64 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6d  rands..**.** Com
1bf7c 70 75 74 61 74 69 6f 6e 20 72 65 73 75 6c 74 73  putation results
1bf7d 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 61   are stored on a
1bf7e 20 73 65 74 20 6f 66 20 72 65 67 69 73 74 65 72   set of register
1bf7f 73 20 6e 75 6d 62 65 72 65 64 20 62 65 67 69 6e  s numbered begin
1bf80 6e 69 6e 67 0a 2a 2a 20 77 69 74 68 20 31 20 61  ning.** with 1 a
1bf81 6e 64 20 67 6f 69 6e 67 20 75 70 20 74 6f 20 56  nd going up to V
1bf82 64 62 65 2e 6e 4d 65 6d 2e 20 20 45 61 63 68 20  dbe.nMem.  Each 
1bf83 72 65 67 69 73 74 65 72 20 63 61 6e 20 73 74 6f  register can sto
1bf84 72 65 0a 2a 2a 20 65 69 74 68 65 72 20 61 6e 20  re.** either an 
1bf85 69 6e 74 65 67 65 72 2c 20 61 20 6e 75 6c 6c 2d  integer, a null-
1bf86 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e  terminated strin
1bf87 67 2c 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f  g, a floating po
1bf88 69 6e 74 0a 2a 2a 20 6e 75 6d 62 65 72 2c 20 6f  int.** number, o
1bf89 72 20 74 68 65 20 53 51 4c 20 22 4e 55 4c 4c 22  r the SQL "NULL"
1bf8a 20 76 61 6c 75 65 2e 20 20 41 6e 20 69 6d 70 6c   value.  An impl
1bf8b 69 63 69 74 20 63 6f 6e 76 65 72 73 69 6f 6e 20  icit conversion 
1bf8c 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 74 79 70 65  from one.** type
1bf8d 20 74 6f 20 74 68 65 20 6f 74 68 65 72 20 6f 63   to the other oc
1bf8e 63 75 72 73 20 61 73 20 6e 65 63 65 73 73 61 72  curs as necessar
1bf8f 79 2e 0a 2a 2a 20 0a 2a 2a 20 4d 6f 73 74 20 6f  y..** .** Most o
1bf90 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68  f the code in th
1bf91 69 73 20 66 69 6c 65 20 69 73 20 74 61 6b 65 6e  is file is taken
1bf92 20 75 70 20 62 79 20 74 68 65 20 73 71 6c 69 74   up by the sqlit
1bf93 65 33 56 64 62 65 45 78 65 63 28 29 0a 2a 2a 20  e3VdbeExec().** 
1bf94 66 75 6e 63 74 69 6f 6e 20 77 68 69 63 68 20 64  function which d
1bf95 6f 65 73 20 74 68 65 20 77 6f 72 6b 20 6f 66 20  oes the work of 
1bf96 69 6e 74 65 72 70 72 65 74 69 6e 67 20 61 20 56  interpreting a V
1bf97 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20  DBE program..** 
1bf98 42 75 74 20 6f 74 68 65 72 20 72 6f 75 74 69 6e  But other routin
1bf99 65 73 20 61 72 65 20 61 6c 73 6f 20 70 72 6f 76  es are also prov
1bf9a 69 64 65 64 20 74 6f 20 68 65 6c 70 20 69 6e 20  ided to help in 
1bf9b 62 75 69 6c 64 69 6e 67 20 75 70 0a 2a 2a 20 61  building up.** a
1bf9c 20 70 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63   program instruc
1bf9d 74 69 6f 6e 20 62 79 20 69 6e 73 74 72 75 63 74  tion by instruct
1bf9e 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 69 6f  ion..**.** Vario
1bf9f 75 73 20 73 63 72 69 70 74 73 20 73 63 61 6e 20  us scripts scan 
1bfa0 74 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65  this source file
1bfa1 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 6e   in order to gen
1bfa2 65 72 61 74 65 20 48 54 4d 4c 0a 2a 2a 20 64 6f  erate HTML.** do
1bfa3 63 75 6d 65 6e 74 61 74 69 6f 6e 2c 20 68 65 61  cumentation, hea
1bfa4 64 65 72 73 20 66 69 6c 65 73 2c 20 6f 72 20 6f  ders files, or o
1bfa5 74 68 65 72 20 64 65 72 69 76 65 64 20 66 69 6c  ther derived fil
1bfa6 65 73 2e 20 20 54 68 65 20 66 6f 72 6d 61 74 74  es.  The formatt
1bfa7 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 65 20 63 6f  ing.** of the co
1bfa8 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
1bfa9 69 73 2c 20 74 68 65 72 65 66 6f 72 65 2c 20 69  is, therefore, i
1bfaa 6d 70 6f 72 74 61 6e 74 2e 20 20 53 65 65 20 6f  mportant.  See o
1bfab 74 68 65 72 20 63 6f 6d 6d 65 6e 74 73 0a 2a 2a  ther comments.**
1bfac 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 66 6f   in this file fo
1bfad 72 20 64 65 74 61 69 6c 73 2e 20 20 49 66 20 69  r details.  If i
1bfae 6e 20 64 6f 75 62 74 2c 20 64 6f 20 6e 6f 74 20  n doubt, do not 
1bfaf 64 65 76 69 61 74 65 20 66 72 6f 6d 20 65 78 69  deviate from exi
1bfb0 73 74 69 6e 67 0a 2a 2a 20 63 6f 6d 6d 65 6e 74  sting.** comment
1bfb1 69 6e 67 20 61 6e 64 20 69 6e 64 65 6e 74 61 74  ing and indentat
1bfb2 69 6f 6e 20 70 72 61 63 74 69 63 65 73 20 77 68  ion practices wh
1bfb3 65 6e 20 63 68 61 6e 67 69 6e 67 20 6f 72 20 61  en changing or a
1bfb4 64 64 69 6e 67 20 63 6f 64 65 2e 0a 2a 2a 0a 2a  dding code..**.*
1bfb5 2a 20 24 49 64 3a 20 76 64 62 65 2e 63 2c 76 20  * $Id: vdbe.c,v 
1bfb6 31 2e 38 37 34 20 32 30 30 39 2f 30 37 2f 32 34  1.874 2009/07/24
1bfb7 20 31 37 3a 35 38 3a 35 33 20 64 61 6e 69 65 6c   17:58:53 daniel
1bfb8 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
1bfb9 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
1bfba 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  ing global varia
1bfbb 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74  ble is increment
1bfbc 65 64 20 65 76 65 72 79 20 74 69 6d 65 20 61 20  ed every time a 
1bfbd 63 75 72 73 6f 72 0a 2a 2a 20 6d 6f 76 65 73 2c  cursor.** moves,
1bfbe 20 65 69 74 68 65 72 20 62 79 20 74 68 65 20 4f   either by the O
1bfbf 50 5f 53 65 65 6b 58 58 2c 20 4f 50 5f 4e 65 78  P_SeekXX, OP_Nex
1bfc0 74 2c 20 6f 72 20 4f 50 5f 50 72 65 76 20 6f 70  t, or OP_Prev op
1bfc1 63 6f 64 65 73 2e 20 20 54 68 65 20 74 65 73 74  codes.  The test
1bfc2 0a 2a 2a 20 70 72 6f 63 65 64 75 72 65 73 20 75  .** procedures u
1bfc3 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74  se this informat
1bfc4 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  ion to make sure
1bfc5 20 74 68 61 74 20 69 6e 64 69 63 65 73 20 61 72   that indices ar
1bfc6 65 0a 2a 2a 20 77 6f 72 6b 69 6e 67 20 63 6f 72  e.** working cor
1bfc7 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20 76 61  rectly.  This va
1bfc8 72 69 61 62 6c 65 20 68 61 73 20 6e 6f 20 66 75  riable has no fu
1bfc9 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61  nction other tha
1bfca 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72  n to.** help ver
1bfcb 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20  ify the correct 
1bfcc 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  operation of the
1bfcd 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66   library..*/.#if
1bfce 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
1bfcf 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1bfd0 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f  qlite3_search_co
1bfd1 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  unt = 0;.#endif.
1bfd2 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73  ./*.** When this
1bfd3 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
1bfd4 20 69 73 20 70 6f 73 69 74 69 76 65 2c 20 69 74   is positive, it
1bfd5 20 67 65 74 73 20 64 65 63 72 65 6d 65 6e 74 65   gets decremente
1bfd6 64 20 6f 6e 63 65 20 62 65 66 6f 72 65 0a 2a 2a  d once before.**
1bfd7 20 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f   each instructio
1bfd8 6e 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20  n in the VDBE.  
1bfd9 57 68 65 6e 20 72 65 61 63 68 65 73 20 7a 65 72  When reaches zer
1bfda 6f 2c 20 74 68 65 20 75 31 2e 69 73 49 6e 74 65  o, the u1.isInte
1bfdb 72 72 75 70 74 65 64 0a 2a 2a 20 66 69 65 6c 64  rrupted.** field
1bfdc 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 20   of the sqlite3 
1bfdd 73 74 72 75 63 74 75 72 65 20 69 73 20 73 65 74  structure is set
1bfde 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 69 6d   in order to sim
1bfdf 75 6c 61 74 65 20 61 6e 64 20 69 6e 74 65 72 72  ulate and interr
1bfe0 75 70 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  upt..**.** This 
1bfe1 66 61 63 69 6c 69 74 79 20 69 73 20 75 73 65 64  facility is used
1bfe2 20 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72   for testing pur
1bfe3 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20  poses only.  It 
1bfe4 64 6f 65 73 20 6e 6f 74 20 66 75 6e 63 74 69 6f  does not functio
1bfe5 6e 0a 2a 2a 20 69 6e 20 61 6e 20 6f 72 64 69 6e  n.** in an ordin
1bfe6 61 72 79 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69  ary build..*/.#i
1bfe7 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
1bfe8 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
1bfe9 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
1bfea 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e  t_count = 0;.#en
1bfeb 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e  dif../*.** The n
1bfec 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  ext global varia
1bfed 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74  ble is increment
1bfee 65 64 20 65 61 63 68 20 74 79 70 65 20 74 68 65  ed each type the
1bfef 20 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64 65 0a   OP_Sort opcode.
1bff0 2a 2a 20 69 73 20 65 78 65 63 75 74 65 64 2e 20  ** is executed. 
1bff1 20 54 68 65 20 74 65 73 74 20 70 72 6f 63 65 64   The test proced
1bff2 75 72 65 73 20 75 73 65 20 74 68 69 73 20 69 6e  ures use this in
1bff3 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b  formation to mak
1bff4 65 20 73 75 72 65 20 74 68 61 74 0a 2a 2a 20 73  e sure that.** s
1bff5 6f 72 74 69 6e 67 20 69 73 20 6f 63 63 75 72 72  orting is occurr
1bff6 69 6e 67 20 6f 72 20 6e 6f 74 20 6f 63 63 75 72  ing or not occur
1bff7 72 69 6e 67 20 61 74 20 61 70 70 72 6f 70 72 69  ring at appropri
1bff8 61 74 65 20 74 69 6d 65 73 2e 20 20 20 54 68 69  ate times.   Thi
1bff9 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 68 61  s variable.** ha
1bffa 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74  s no function ot
1bffb 68 65 72 20 74 68 61 6e 20 74 6f 20 68 65 6c 70  her than to help
1bffc 20 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 72   verify the corr
1bffd 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66  ect operation of
1bffe 20 74 68 65 0a 2a 2a 20 6c 69 62 72 61 72 79 2e   the.** library.
1bfff 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
1c000 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
1c001 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6f  I int sqlite3_so
1c002 72 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65  rt_count = 0;.#e
1c003 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
1c004 6e 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69  next global vari
1c005 61 62 6c 65 20 72 65 63 6f 72 64 73 20 74 68 65  able records the
1c006 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72   size of the lar
1c007 67 65 73 74 20 4d 45 4d 5f 42 6c 6f 62 0a 2a 2a  gest MEM_Blob.**
1c008 20 6f 72 20 4d 45 4d 5f 53 74 72 20 74 68 61 74   or MEM_Str that
1c009 20 68 61 73 20 62 65 65 6e 20 75 73 65 64 20 62   has been used b
1c00a 79 20 61 20 56 44 42 45 20 6f 70 63 6f 64 65 2e  y a VDBE opcode.
1c00b 20 20 54 68 65 20 74 65 73 74 20 70 72 6f 63 65    The test proce
1c00c 64 75 72 65 73 0a 2a 2a 20 75 73 65 20 74 68 69  dures.** use thi
1c00d 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f  s information to
1c00e 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
1c00f 74 68 65 20 7a 65 72 6f 2d 62 6c 6f 62 20 66 75  the zero-blob fu
1c010 6e 63 74 69 6f 6e 61 6c 69 74 79 0a 2a 2a 20 69  nctionality.** i
1c011 73 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63  s working correc
1c012 74 6c 79 2e 20 20 20 54 68 69 73 20 76 61 72 69  tly.   This vari
1c013 61 62 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e 63  able has no func
1c014 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20  tion other than 
1c015 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 66  to.** help verif
1c016 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70  y the correct op
1c017 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c  eration of the l
1c018 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65  ibrary..*/.#ifde
1c019 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
1c01a 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1c01b 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a  ite3_max_blobsiz
1c01c 65 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 76 6f  e = 0;.static vo
1c01d 69 64 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62  id updateMaxBlob
1c01e 73 69 7a 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20  size(Mem *p){.  
1c01f 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20  if( (p->flags & 
1c020 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f  (MEM_Str|MEM_Blo
1c021 62 29 29 21 3d 30 20 26 26 20 70 2d 3e 6e 3e 73  b))!=0 && p->n>s
1c022 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73  qlite3_max_blobs
1c023 69 7a 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ize ){.    sqlit
1c024 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20  e3_max_blobsize 
1c025 3d 20 70 2d 3e 6e 3b 0a 20 20 7d 0a 7d 0a 23 65  = p->n;.  }.}.#e
1c026 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
1c027 6e 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69  next global vari
1c028 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e  able is incremen
1c029 74 65 64 20 65 61 63 68 20 74 79 70 65 20 74 68  ted each type th
1c02a 65 20 4f 50 5f 46 6f 75 6e 64 20 6f 70 63 6f 64  e OP_Found opcod
1c02b 65 0a 2a 2a 20 69 73 20 65 78 65 63 75 74 65 64  e.** is executed
1c02c 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74  . This is used t
1c02d 6f 20 74 65 73 74 20 77 68 65 74 68 65 72 20 6f  o test whether o
1c02e 72 20 6e 6f 74 20 74 68 65 20 66 6f 72 65 69 67  r not the foreig
1c02f 6e 20 6b 65 79 0a 2a 2a 20 6f 70 65 72 61 74 69  n key.** operati
1c030 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 75  on implemented u
1c031 73 69 6e 67 20 4f 50 5f 46 6b 49 73 5a 65 72 6f  sing OP_FkIsZero
1c032 20 69 73 20 77 6f 72 6b 69 6e 67 2e 20 54 68 69   is working. Thi
1c033 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 68 61  s variable.** ha
1c034 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74  s no function ot
1c035 68 65 72 20 74 68 61 6e 20 74 6f 20 68 65 6c 70  her than to help
1c036 20 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 72   verify the corr
1c037 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66  ect operation of
1c038 20 74 68 65 0a 2a 2a 20 6c 69 62 72 61 72 79 2e   the.** library.
1c039 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
1c03a 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
1c03b 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 6f  I int sqlite3_fo
1c03c 75 6e 64 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23  und_count = 0;.#
1c03d 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73  endif../*.** Tes
1c03e 74 20 61 20 72 65 67 69 73 74 65 72 20 74 6f 20  t a register to 
1c03f 73 65 65 20 69 66 20 69 74 20 65 78 63 65 65 64  see if it exceed
1c040 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61  s the current ma
1c041 78 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e  ximum blob size.
1c042 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 2c 20  .** If it does, 
1c043 72 65 63 6f 72 64 20 74 68 65 20 6e 65 77 20 6d  record the new m
1c044 61 78 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65  aximum blob size
1c045 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64  ..*/.#if defined
1c046 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26  (SQLITE_TEST) &&
1c047 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1c048 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45  _OMIT_BUILTIN_TE
1c049 53 54 29 0a 23 20 64 65 66 69 6e 65 20 55 50 44  ST).# define UPD
1c04a 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45  ATE_MAX_BLOBSIZE
1c04b 28 50 29 20 20 75 70 64 61 74 65 4d 61 78 42 6c  (P)  updateMaxBl
1c04c 6f 62 73 69 7a 65 28 50 29 0a 23 65 6c 73 65 0a  obsize(P).#else.
1c04d 23 20 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f  # define UPDATE_
1c04e 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 0a  MAX_BLOBSIZE(P).
1c04f 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f  #endif../*.** Co
1c050 6e 76 65 72 74 20 74 68 65 20 67 69 76 65 6e 20  nvert the given 
1c051 72 65 67 69 73 74 65 72 20 69 6e 74 6f 20 61 20  register into a 
1c052 73 74 72 69 6e 67 20 69 66 20 69 74 20 69 73 6e  string if it isn
1c053 27 74 20 6f 6e 65 0a 2a 2a 20 61 6c 72 65 61 64  't one.** alread
1c054 79 2e 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65  y. Return non-ze
1c055 72 6f 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29  ro if a malloc()
1c056 20 66 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69   fails..*/.#defi
1c057 6e 65 20 53 74 72 69 6e 67 69 66 79 28 50 2c 20  ne Stringify(P, 
1c058 65 6e 63 29 20 5c 0a 20 20 20 69 66 28 28 28 50  enc) \.   if(((P
1c059 29 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74  )->flags&(MEM_St
1c05a 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20  r|MEM_Blob))==0 
1c05b 26 26 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  && sqlite3VdbeMe
1c05c 6d 53 74 72 69 6e 67 69 66 79 28 50 2c 65 6e 63  mStringify(P,enc
1c05d 29 29 20 5c 0a 20 20 20 20 20 7b 20 67 6f 74 6f  )) \.     { goto
1c05e 20 6e 6f 5f 6d 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a   no_mem; }../*.*
1c05f 2a 20 41 6e 20 65 70 68 65 6d 65 72 61 6c 20 73  * An ephemeral s
1c060 74 72 69 6e 67 20 76 61 6c 75 65 20 28 73 69 67  tring value (sig
1c061 6e 69 66 69 65 64 20 62 79 20 74 68 65 20 4d 45  nified by the ME
1c062 4d 5f 45 70 68 65 6d 20 66 6c 61 67 29 20 63 6f  M_Ephem flag) co
1c063 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e  ntains.** a poin
1c064 74 65 72 20 74 6f 20 61 20 64 79 6e 61 6d 69 63  ter to a dynamic
1c065 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73  ally allocated s
1c066 74 72 69 6e 67 20 77 68 65 72 65 20 73 6f 6d 65  tring where some
1c067 20 6f 74 68 65 72 20 65 6e 74 69 74 79 0a 2a 2a   other entity.**
1c068 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
1c069 66 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67  for deallocating
1c06a 20 74 68 61 74 20 73 74 72 69 6e 67 2e 20 20 42   that string.  B
1c06b 65 63 61 75 73 65 20 74 68 65 20 72 65 67 69 73  ecause the regis
1c06c 74 65 72 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20  ter.** does not 
1c06d 63 6f 6e 74 72 6f 6c 20 74 68 65 20 73 74 72 69  control the stri
1c06e 6e 67 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20  ng, it might be 
1c06f 64 65 6c 65 74 65 64 20 77 69 74 68 6f 75 74 20  deleted without 
1c070 74 68 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20  the register.** 
1c071 6b 6e 6f 77 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a  knowing it..**.*
1c072 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
1c073 6f 6e 76 65 72 74 73 20 61 6e 20 65 70 68 65 6d  onverts an ephem
1c074 65 72 61 6c 20 73 74 72 69 6e 67 20 69 6e 74 6f  eral string into
1c075 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61   a dynamically a
1c076 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 73 74 72 69  llocated.** stri
1c077 6e 67 20 74 68 61 74 20 74 68 65 20 72 65 67 69  ng that the regi
1c078 73 74 65 72 20 69 74 73 65 6c 66 20 63 6f 6e 74  ster itself cont
1c079 72 6f 6c 73 2e 20 20 49 6e 20 6f 74 68 65 72 20  rols.  In other 
1c07a 77 6f 72 64 73 2c 20 69 74 0a 2a 2a 20 63 6f 6e  words, it.** con
1c07b 76 65 72 74 73 20 61 6e 20 4d 45 4d 5f 45 70 68  verts an MEM_Eph
1c07c 65 6d 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61  em string into a
1c07d 6e 20 4d 45 4d 5f 44 79 6e 20 73 74 72 69 6e 67  n MEM_Dyn string
1c07e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 65 65  ..*/.#define Dee
1c07f 70 68 65 6d 65 72 61 6c 69 7a 65 28 50 29 20 5c  phemeralize(P) \
1c080 0a 20 20 20 69 66 28 20 28 28 50 29 2d 3e 66 6c  .   if( ((P)->fl
1c081 61 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29 21 3d  ags&MEM_Ephem)!=
1c082 30 20 5c 0a 20 20 20 20 20 20 20 26 26 20 73 71  0 \.       && sq
1c083 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65  lite3VdbeMemMake
1c084 57 72 69 74 65 61 62 6c 65 28 50 29 20 29 7b 20  Writeable(P) ){ 
1c085 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f  goto no_mem;}../
1c086 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65  *.** Call sqlite
1c087 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c  3VdbeMemExpandBl
1c088 6f 62 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70  ob() on the supp
1c089 6c 69 65 64 20 76 61 6c 75 65 20 28 74 79 70 65  lied value (type
1c08a 20 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72   Mem*).** P if r
1c08b 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66  equired..*/.#def
1c08c 69 6e 65 20 45 78 70 61 6e 64 42 6c 6f 62 28 50  ine ExpandBlob(P
1c08d 29 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d  ) (((P)->flags&M
1c08e 45 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33  EM_Zero)?sqlite3
1c08f 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f  VdbeMemExpandBlo
1c090 62 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 41  b(P):0)../*.** A
1c091 72 67 75 6d 65 6e 74 20 70 4d 65 6d 20 70 6f 69  rgument pMem poi
1c092 6e 74 73 20 61 74 20 61 20 72 65 67 69 73 74 65  nts at a registe
1c093 72 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 70  r that will be p
1c094 61 73 73 65 64 20 74 6f 20 61 0a 2a 2a 20 75 73  assed to a.** us
1c095 65 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  er-defined funct
1c096 69 6f 6e 20 6f 72 20 72 65 74 75 72 6e 65 64 20  ion or returned 
1c097 74 6f 20 74 68 65 20 75 73 65 72 20 61 73 20 74  to the user as t
1c098 68 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 71  he result of a q
1c099 75 65 72 79 2e 0a 2a 2a 20 54 68 69 73 20 72 6f  uery..** This ro
1c09a 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 70  utine sets the p
1c09b 4d 65 6d 2d 3e 74 79 70 65 20 76 61 72 69 61 62  Mem->type variab
1c09c 6c 65 20 75 73 65 64 20 62 79 20 74 68 65 20 73  le used by the s
1c09d 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 2a 28 29  qlite3_value_*()
1c09e 20 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2e 0a 2a   .** routines..*
1c09f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1c0a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
1c0a1 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 4d 65  eMemStoreType(Me
1c0a2 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20  m *pMem){.  int 
1c0a3 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c  flags = pMem->fl
1c0a4 61 67 73 3b 0a 20 20 69 66 28 20 66 6c 61 67 73  ags;.  if( flags
1c0a5 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20   & MEM_Null ){. 
1c0a6 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
1c0a7 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 7d  SQLITE_NULL;.  }
1c0a8 0a 20 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67  .  else if( flag
1c0a9 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20  s & MEM_Int ){. 
1c0aa 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
1c0ab 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a  SQLITE_INTEGER;.
1c0ac 20 20 7d 0a 20 20 65 6c 73 65 20 69 66 28 20 66    }.  else if( f
1c0ad 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20  lags & MEM_Real 
1c0ae 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70  ){.    pMem->typ
1c0af 65 20 3d 20 53 51 4c 49 54 45 5f 46 4c 4f 41 54  e = SQLITE_FLOAT
1c0b0 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 20 69 66 28  ;.  }.  else if(
1c0b1 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72   flags & MEM_Str
1c0b2 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79   ){.    pMem->ty
1c0b3 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45 58 54  pe = SQLITE_TEXT
1c0b4 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
1c0b5 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49  Mem->type = SQLI
1c0b6 54 45 5f 42 4c 4f 42 3b 0a 20 20 7d 0a 7d 0a 0a  TE_BLOB;.  }.}..
1c0b7 2f 2a 0a 2a 2a 20 50 72 6f 70 65 72 74 69 65 73  /*.** Properties
1c0b8 20 6f 66 20 6f 70 63 6f 64 65 73 2e 20 20 54 68   of opcodes.  Th
1c0b9 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49  e OPFLG_INITIALI
1c0ba 5a 45 52 20 6d 61 63 72 6f 20 69 73 0a 2a 2a 20  ZER macro is.** 
1c0bb 63 72 65 61 74 65 64 20 62 79 20 6d 6b 6f 70 63  created by mkopc
1c0bc 6f 64 65 68 2e 61 77 6b 20 64 75 72 69 6e 67 20  odeh.awk during 
1c0bd 63 6f 6d 70 69 6c 61 74 69 6f 6e 2e 20 20 44 61  compilation.  Da
1c0be 74 61 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a  ta is obtained.*
1c0bf 2a 20 66 72 6f 6d 20 74 68 65 20 63 6f 6d 6d 65  * from the comme
1c0c0 6e 74 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  nts following th
1c0c1 65 20 22 63 61 73 65 20 4f 50 5f 78 78 78 78 3a  e "case OP_xxxx:
1c0c2 22 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 0a  " statements in.
1c0c3 2a 2a 20 74 68 69 73 20 66 69 6c 65 2e 20 20 0a  ** this file.  .
1c0c4 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
1c0c5 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6f 70  unsigned char op
1c0c6 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 5d 20 3d  codeProperty[] =
1c0c7 20 4f 50 46 4c 47 5f 49 4e 49 54 49 41 4c 49 5a   OPFLG_INITIALIZ
1c0c8 45 52 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ER;../*.** Retur
1c0c9 6e 20 74 72 75 65 20 69 66 20 61 6e 20 6f 70 63  n true if an opc
1c0ca 6f 64 65 20 68 61 73 20 61 6e 79 20 6f 66 20 74  ode has any of t
1c0cb 68 65 20 4f 50 46 4c 47 5f 78 78 78 20 70 72 6f  he OPFLG_xxx pro
1c0cc 70 65 72 74 69 65 73 0a 2a 2a 20 73 70 65 63 69  perties.** speci
1c0cd 66 69 65 64 20 62 79 20 6d 61 73 6b 2e 0a 2a 2f  fied by mask..*/
1c0ce 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1c0cf 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4f  int sqlite3VdbeO
1c0d0 70 63 6f 64 65 48 61 73 50 72 6f 70 65 72 74 79  pcodeHasProperty
1c0d1 28 69 6e 74 20 6f 70 63 6f 64 65 2c 20 69 6e 74  (int opcode, int
1c0d2 20 6d 61 73 6b 29 7b 0a 20 20 61 73 73 65 72 74   mask){.  assert
1c0d3 28 20 6f 70 63 6f 64 65 3e 30 20 26 26 20 6f 70  ( opcode>0 && op
1c0d4 63 6f 64 65 3c 28 69 6e 74 29 73 69 7a 65 6f 66  code<(int)sizeof
1c0d5 28 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 29  (opcodeProperty)
1c0d6 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 6f 70   );.  return (op
1c0d7 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 6f 70 63  codeProperty[opc
1c0d8 6f 64 65 5d 26 6d 61 73 6b 29 21 3d 30 3b 0a 7d  ode]&mask)!=0;.}
1c0d9 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
1c0da 20 56 64 62 65 43 75 72 73 6f 72 20 6e 75 6d 62   VdbeCursor numb
1c0db 65 72 20 69 43 75 72 2e 20 20 52 65 74 75 72 6e  er iCur.  Return
1c0dc 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74   a pointer to it
1c0dd 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 0a 2a  .  Return NULL.*
1c0de 2a 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20  * if we run out 
1c0df 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74  of memory..*/.st
1c0e0 61 74 69 63 20 56 64 62 65 43 75 72 73 6f 72 20  atic VdbeCursor 
1c0e1 2a 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28  *allocateCursor(
1c0e2 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20 20  .  Vdbe *p,     
1c0e3 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1c0e4 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  virtual machine 
1c0e5 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20  */.  int iCur,  
1c0e6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
1c0e7 64 65 78 20 6f 66 20 74 68 65 20 6e 65 77 20 56  dex of the new V
1c0e8 64 62 65 43 75 72 73 6f 72 20 2a 2f 0a 20 20 69  dbeCursor */.  i
1c0e9 6e 74 20 6e 46 69 65 6c 64 2c 20 20 20 20 20 20  nt nField,      
1c0ea 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1c0eb 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20  f fields in the 
1c0ec 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 2a  table or index *
1c0ed 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20  /.  int iDb,    
1c0ee 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65            /* Whe
1c0ef 6e 20 64 61 74 61 62 61 73 65 20 74 68 65 20 63  n database the c
1c0f0 75 72 73 6f 72 20 62 65 6c 6f 6e 67 73 20 74 6f  ursor belongs to
1c0f1 2c 20 6f 72 20 2d 31 20 2a 2f 0a 20 20 69 6e 74  , or -1 */.  int
1c0f2 20 69 73 42 74 72 65 65 43 75 72 73 6f 72 20 20   isBtreeCursor  
1c0f3 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 42     /* True for B
1c0f4 2d 54 72 65 65 2e 20 20 46 61 6c 73 65 20 66 6f  -Tree.  False fo
1c0f5 72 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 6f  r pseudo-table o
1c0f6 72 20 76 74 61 62 20 2a 2f 0a 29 7b 0a 20 20 2f  r vtab */.){.  /
1c0f7 2a 20 46 69 6e 64 20 74 68 65 20 6d 65 6d 6f 72  * Find the memor
1c0f8 79 20 63 65 6c 6c 20 74 68 61 74 20 77 69 6c 6c  y cell that will
1c0f9 20 62 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72   be used to stor
1c0fa 65 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65  e the blob of me
1c0fb 6d 6f 72 79 0a 20 20 2a 2a 20 72 65 71 75 69 72  mory.  ** requir
1c0fc 65 64 20 66 6f 72 20 74 68 69 73 20 56 64 62 65  ed for this Vdbe
1c0fd 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65  Cursor structure
1c0fe 2e 20 49 74 20 69 73 20 63 6f 6e 76 65 6e 69 65  . It is convenie
1c0ff 6e 74 20 74 6f 20 75 73 65 20 61 20 0a 20 20 2a  nt to use a .  *
1c100 2a 20 76 64 62 65 20 6d 65 6d 6f 72 79 20 63 65  * vdbe memory ce
1c101 6c 6c 20 74 6f 20 6d 61 6e 61 67 65 20 74 68 65  ll to manage the
1c102 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
1c103 6f 6e 20 72 65 71 75 69 72 65 64 20 66 6f 72 20  on required for 
1c104 61 0a 20 20 2a 2a 20 56 64 62 65 43 75 72 73 6f  a.  ** VdbeCurso
1c105 72 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20  r structure for 
1c106 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 65  the following re
1c107 61 73 6f 6e 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  asons:.  **.  **
1c108 20 20 20 2a 20 53 6f 6d 65 74 69 6d 65 73 20 63     * Sometimes c
1c109 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 72  ursor numbers ar
1c10a 65 20 75 73 65 64 20 66 6f 72 20 61 20 63 6f 75  e used for a cou
1c10b 70 6c 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74  ple of different
1c10c 0a 20 20 2a 2a 20 20 20 20 20 70 75 72 70 6f 73  .  **     purpos
1c10d 65 73 20 69 6e 20 61 20 76 64 62 65 20 70 72 6f  es in a vdbe pro
1c10e 67 72 61 6d 2e 20 54 68 65 20 64 69 66 66 65 72  gram. The differ
1c10f 65 6e 74 20 75 73 65 73 20 6d 69 67 68 74 20 72  ent uses might r
1c110 65 71 75 69 72 65 0a 20 20 2a 2a 20 20 20 20 20  equire.  **     
1c111 64 69 66 66 65 72 65 6e 74 20 73 69 7a 65 64 20  different sized 
1c112 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e 20 4d 65 6d  allocations. Mem
1c113 6f 72 79 20 63 65 6c 6c 73 20 70 72 6f 76 69 64  ory cells provid
1c114 65 20 67 72 6f 77 61 62 6c 65 0a 20 20 2a 2a 20  e growable.  ** 
1c115 20 20 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 2e      allocations.
1c116 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 2a 20 57  .  **.  **   * W
1c117 68 65 6e 20 75 73 69 6e 67 20 45 4e 41 42 4c 45  hen using ENABLE
1c118 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45  _MEMORY_MANAGEME
1c119 4e 54 2c 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  NT, memory cell 
1c11a 62 75 66 66 65 72 73 20 63 61 6e 0a 20 20 2a 2a  buffers can.  **
1c11b 20 20 20 20 20 62 65 20 66 72 65 65 64 20 6c 61       be freed la
1c11c 7a 69 6c 79 20 76 69 61 20 74 68 65 20 73 71 6c  zily via the sql
1c11d 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d  ite3_release_mem
1c11e 6f 72 79 28 29 20 41 50 49 2e 20 54 68 69 73 0a  ory() API. This.
1c11f 20 20 2a 2a 20 20 20 20 20 6d 69 6e 69 6d 69 7a    **     minimiz
1c120 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  es the number of
1c121 20 6d 61 6c 6c 6f 63 20 63 61 6c 6c 73 20 6d 61   malloc calls ma
1c122 64 65 20 62 79 20 74 68 65 20 73 79 73 74 65 6d  de by the system
1c123 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4d 65 6d 6f  ..  **.  ** Memo
1c124 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 63 75 72  ry cells for cur
1c125 73 6f 72 73 20 61 72 65 20 61 6c 6c 6f 63 61 74  sors are allocat
1c126 65 64 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66  ed at the top of
1c127 20 74 68 65 20 61 64 64 72 65 73 73 0a 20 20 2a   the address.  *
1c128 2a 20 73 70 61 63 65 2e 20 4d 65 6d 6f 72 79 20  * space. Memory 
1c129 63 65 6c 6c 20 28 70 2d 3e 6e 4d 65 6d 29 20 63  cell (p->nMem) c
1c12a 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 63 75  orresponds to cu
1c12b 72 73 6f 72 20 30 2e 20 53 70 61 63 65 20 66 6f  rsor 0. Space fo
1c12c 72 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20 31 20  r.  ** cursor 1 
1c12d 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20 6d 65  is managed by me
1c12e 6d 6f 72 79 20 63 65 6c 6c 20 28 70 2d 3e 6e 4d  mory cell (p->nM
1c12f 65 6d 2d 31 29 2c 20 65 74 63 2e 0a 20 20 2a 2f  em-1), etc..  */
1c130 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20 26  .  Mem *pMem = &
1c131 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d 2d  p->aMem[p->nMem-
1c132 69 43 75 72 5d 3b 0a 0a 20 20 69 6e 74 20 6e 42  iCur];..  int nB
1c133 79 74 65 3b 0a 20 20 56 64 62 65 43 75 72 73 6f  yte;.  VdbeCurso
1c134 72 20 2a 70 43 78 20 3d 20 30 3b 0a 20 20 6e 42  r *pCx = 0;.  nB
1c135 79 74 65 20 3d 20 0a 20 20 20 20 20 20 73 69 7a  yte = .      siz
1c136 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 20  eof(VdbeCursor) 
1c137 2b 20 0a 20 20 20 20 20 20 28 69 73 42 74 72 65  + .      (isBtre
1c138 65 43 75 72 73 6f 72 3f 73 71 6c 69 74 65 33 42  eCursor?sqlite3B
1c139 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 28 29  treeCursorSize()
1c13a 3a 30 29 20 2b 20 0a 20 20 20 20 20 20 32 2a 6e  :0) + .      2*n
1c13b 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28 75 33 32  Field*sizeof(u32
1c13c 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 43  );..  assert( iC
1c13d 75 72 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b  ur<p->nCursor );
1c13e 0a 20 20 69 66 28 20 70 2d 3e 61 70 43 73 72 5b  .  if( p->apCsr[
1c13f 69 43 75 72 5d 20 29 7b 0a 20 20 20 20 73 71 6c  iCur] ){.    sql
1c140 69 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73  ite3VdbeFreeCurs
1c141 6f 72 28 70 2c 20 70 2d 3e 61 70 43 73 72 5b 69  or(p, p->apCsr[i
1c142 43 75 72 5d 29 3b 0a 20 20 20 20 70 2d 3e 61 70  Cur]);.    p->ap
1c143 43 73 72 5b 69 43 75 72 5d 20 3d 20 30 3b 0a 20  Csr[iCur] = 0;. 
1c144 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f   }.  if( SQLITE_
1c145 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 64 62 65 4d  OK==sqlite3VdbeM
1c146 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79  emGrow(pMem, nBy
1c147 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 70 2d  te, 0) ){.    p-
1c148 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 3d 20 70  >apCsr[iCur] = p
1c149 43 78 20 3d 20 28 56 64 62 65 43 75 72 73 6f 72  Cx = (VdbeCursor
1c14a 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20 6d  *)pMem->z;.    m
1c14b 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c 20 30  emset(pMem->z, 0
1c14c 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 70 43  , nByte);.    pC
1c14d 78 2d 3e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20  x->iDb = iDb;.  
1c14e 20 20 70 43 78 2d 3e 6e 46 69 65 6c 64 20 3d 20    pCx->nField = 
1c14f 6e 46 69 65 6c 64 3b 0a 20 20 20 20 69 66 28 20  nField;.    if( 
1c150 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20 20 20 20  nField ){.      
1c151 70 43 78 2d 3e 61 54 79 70 65 20 3d 20 28 75 33  pCx->aType = (u3
1c152 32 20 2a 29 26 70 4d 65 6d 2d 3e 7a 5b 73 69 7a  2 *)&pMem->z[siz
1c153 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72 29 5d  eof(VdbeCursor)]
1c154 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1c155 69 73 42 74 72 65 65 43 75 72 73 6f 72 20 29 7b  isBtreeCursor ){
1c156 0a 20 20 20 20 20 20 70 43 78 2d 3e 70 43 75 72  .      pCx->pCur
1c157 73 6f 72 20 3d 20 28 42 74 43 75 72 73 6f 72 2a  sor = (BtCursor*
1c158 29 0a 20 20 20 20 20 20 20 20 20 20 26 70 4d 65  ).          &pMe
1c159 6d 2d 3e 7a 5b 73 69 7a 65 6f 66 28 56 64 62 65  m->z[sizeof(Vdbe
1c15a 43 75 72 73 6f 72 29 2b 32 2a 6e 46 69 65 6c 64  Cursor)+2*nField
1c15b 2a 73 69 7a 65 6f 66 28 75 33 32 29 5d 3b 0a 20  *sizeof(u32)];. 
1c15c 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
1c15d 6e 20 70 43 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n pCx;.}../*.** 
1c15e 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 61  Try to convert a
1c15f 20 76 61 6c 75 65 20 69 6e 74 6f 20 61 20 6e 75   value into a nu
1c160 6d 65 72 69 63 20 72 65 70 72 65 73 65 6e 74 61  meric representa
1c161 74 69 6f 6e 20 69 66 20 77 65 20 63 61 6e 0a 2a  tion if we can.*
1c162 2a 20 64 6f 20 73 6f 20 77 69 74 68 6f 75 74 20  * do so without 
1c163 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d 61 74  loss of informat
1c164 69 6f 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  ion.  In other w
1c165 6f 72 64 73 2c 20 69 66 20 74 68 65 20 73 74 72  ords, if the str
1c166 69 6e 67 0a 2a 2a 20 6c 6f 6f 6b 73 20 6c 69 6b  ing.** looks lik
1c167 65 20 61 20 6e 75 6d 62 65 72 2c 20 63 6f 6e 76  e a number, conv
1c168 65 72 74 20 69 74 20 69 6e 74 6f 20 61 20 6e 75  ert it into a nu
1c169 6d 62 65 72 2e 20 20 49 66 20 69 74 20 64 6f 65  mber.  If it doe
1c16a 73 20 6e 6f 74 0a 2a 2a 20 6c 6f 6f 6b 20 6c 69  s not.** look li
1c16b 6b 65 20 61 20 6e 75 6d 62 65 72 2c 20 6c 65 61  ke a number, lea
1c16c 76 65 20 69 74 20 61 6c 6f 6e 65 2e 0a 2a 2f 0a  ve it alone..*/.
1c16d 73 74 61 74 69 63 20 76 6f 69 64 20 61 70 70 6c  static void appl
1c16e 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79  yNumericAffinity
1c16f 28 4d 65 6d 20 2a 70 52 65 63 29 7b 0a 20 20 69  (Mem *pRec){.  i
1c170 66 28 20 28 70 52 65 63 2d 3e 66 6c 61 67 73 20  f( (pRec->flags 
1c171 26 20 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f  & (MEM_Real|MEM_
1c172 49 6e 74 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20  Int))==0 ){.    
1c173 69 6e 74 20 72 65 61 6c 6e 75 6d 3b 0a 20 20 20  int realnum;.   
1c174 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e   sqlite3VdbeMemN
1c175 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 52 65 63  ulTerminate(pRec
1c176 29 3b 0a 20 20 20 20 69 66 28 20 28 70 52 65 63  );.    if( (pRec
1c177 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29  ->flags&MEM_Str)
1c178 0a 20 20 20 20 20 20 20 20 20 26 26 20 73 71 6c  .         && sql
1c179 69 74 65 33 49 73 4e 75 6d 62 65 72 28 70 52 65  ite3IsNumber(pRe
1c17a 63 2d 3e 7a 2c 20 26 72 65 61 6c 6e 75 6d 2c 20  c->z, &realnum, 
1c17b 70 52 65 63 2d 3e 65 6e 63 29 20 29 7b 0a 20 20  pRec->enc) ){.  
1c17c 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a 20      i64 value;. 
1c17d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1c17e 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70  ChangeEncoding(p
1c17f 52 65 63 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Rec, SQLITE_UTF8
1c180 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 72 65  );.      if( !re
1c181 61 6c 6e 75 6d 20 26 26 20 73 71 6c 69 74 65 33  alnum && sqlite3
1c182 41 74 6f 69 36 34 28 70 52 65 63 2d 3e 7a 2c 20  Atoi64(pRec->z, 
1c183 26 76 61 6c 75 65 29 20 29 7b 0a 20 20 20 20 20  &value) ){.     
1c184 20 20 20 70 52 65 63 2d 3e 75 2e 69 20 3d 20 76     pRec->u.i = v
1c185 61 6c 75 65 3b 0a 20 20 20 20 20 20 20 20 4d 65  alue;.        Me
1c186 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 52 65  mSetTypeFlag(pRe
1c187 63 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20  c, MEM_Int);.   
1c188 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1c189 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1c18a 52 65 61 6c 69 66 79 28 70 52 65 63 29 3b 0a 20  Realify(pRec);. 
1c18b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
1c18c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73  .}../*.** Proces
1c18d 73 69 6e 67 20 69 73 20 64 65 74 65 72 6d 69 6e  sing is determin
1c18e 65 20 62 79 20 74 68 65 20 61 66 66 69 6e 69 74  e by the affinit
1c18f 79 20 70 61 72 61 6d 65 74 65 72 3a 0a 2a 2a 0a  y parameter:.**.
1c190 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e  ** SQLITE_AFF_IN
1c191 54 45 47 45 52 3a 0a 2a 2a 20 53 51 4c 49 54 45  TEGER:.** SQLITE
1c192 5f 41 46 46 5f 52 45 41 4c 3a 0a 2a 2a 20 53 51  _AFF_REAL:.** SQ
1c193 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
1c194 3a 0a 2a 2a 20 20 20 20 54 72 79 20 74 6f 20 63  :.**    Try to c
1c195 6f 6e 76 65 72 74 20 70 52 65 63 20 74 6f 20 61  onvert pRec to a
1c196 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72 65 73  n integer repres
1c197 65 6e 74 61 74 69 6f 6e 20 6f 72 20 61 20 0a 2a  entation or a .*
1c198 2a 20 20 20 20 66 6c 6f 61 74 69 6e 67 2d 70 6f  *    floating-po
1c199 69 6e 74 20 72 65 70 72 65 73 65 6e 74 61 74 69  int representati
1c19a 6f 6e 20 69 66 20 61 6e 20 69 6e 74 65 67 65 72  on if an integer
1c19b 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a   representation.
1c19c 2a 2a 20 20 20 20 69 73 20 6e 6f 74 20 70 6f 73  **    is not pos
1c19d 73 69 62 6c 65 2e 20 20 4e 6f 74 65 20 74 68 61  sible.  Note tha
1c19e 74 20 74 68 65 20 69 6e 74 65 67 65 72 20 72 65  t the integer re
1c19f 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 73 0a  presentation is.
1c1a0 2a 2a 20 20 20 20 61 6c 77 61 79 73 20 70 72 65  **    always pre
1c1a1 66 65 72 72 65 64 2c 20 65 76 65 6e 20 69 66 20  ferred, even if 
1c1a2 74 68 65 20 61 66 66 69 6e 69 74 79 20 69 73 20  the affinity is 
1c1a3 52 45 41 4c 2c 20 62 65 63 61 75 73 65 0a 2a 2a  REAL, because.**
1c1a4 20 20 20 20 61 6e 20 69 6e 74 65 67 65 72 20 72      an integer r
1c1a5 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69 73  epresentation is
1c1a6 20 6d 6f 72 65 20 73 70 61 63 65 20 65 66 66 69   more space effi
1c1a7 63 69 65 6e 74 20 6f 6e 20 64 69 73 6b 2e 0a 2a  cient on disk..*
1c1a8 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f  *.** SQLITE_AFF_
1c1a9 54 45 58 54 3a 0a 2a 2a 20 20 20 20 43 6f 6e 76  TEXT:.**    Conv
1c1aa 65 72 74 20 70 52 65 63 20 74 6f 20 61 20 74 65  ert pRec to a te
1c1ab 78 74 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  xt representatio
1c1ac 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f  n..**.** SQLITE_
1c1ad 41 46 46 5f 4e 4f 4e 45 3a 0a 2a 2a 20 20 20 20  AFF_NONE:.**    
1c1ae 4e 6f 2d 6f 70 2e 20 20 70 52 65 63 20 69 73 20  No-op.  pRec is 
1c1af 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74  unchanged..*/.st
1c1b0 61 74 69 63 20 76 6f 69 64 20 61 70 70 6c 79 41  atic void applyA
1c1b1 66 66 69 6e 69 74 79 28 0a 20 20 4d 65 6d 20 2a  ffinity(.  Mem *
1c1b2 70 52 65 63 2c 20 20 20 20 20 20 20 20 20 20 2f  pRec,          /
1c1b3 2a 20 54 68 65 20 76 61 6c 75 65 20 74 6f 20 61  * The value to a
1c1b4 70 70 6c 79 20 61 66 66 69 6e 69 74 79 20 74 6f  pply affinity to
1c1b5 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e   */.  char affin
1c1b6 69 74 79 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  ity,      /* The
1c1b7 20 61 66 66 69 6e 69 74 79 20 74 6f 20 62 65 20   affinity to be 
1c1b8 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20 75 38 20  applied */.  u8 
1c1b9 65 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 20  enc             
1c1ba 20 2f 2a 20 55 73 65 20 74 68 69 73 20 74 65 78   /* Use this tex
1c1bb 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 29 7b  t encoding */.){
1c1bc 0a 20 20 69 66 28 20 61 66 66 69 6e 69 74 79 3d  .  if( affinity=
1c1bd 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54  =SQLITE_AFF_TEXT
1c1be 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20   ){.    /* Only 
1c1bf 61 74 74 65 6d 70 74 20 74 68 65 20 63 6f 6e 76  attempt the conv
1c1c0 65 72 73 69 6f 6e 20 74 6f 20 54 45 58 54 20 69  ersion to TEXT i
1c1c1 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 69 6e  f there is an in
1c1c2 74 65 67 65 72 20 6f 72 20 72 65 61 6c 0a 20 20  teger or real.  
1c1c3 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74    ** representat
1c1c4 69 6f 6e 20 28 62 6c 6f 62 20 61 6e 64 20 4e 55  ion (blob and NU
1c1c5 4c 4c 20 64 6f 20 6e 6f 74 20 67 65 74 20 63 6f  LL do not get co
1c1c6 6e 76 65 72 74 65 64 29 20 62 75 74 20 6e 6f 20  nverted) but no 
1c1c7 73 74 72 69 6e 67 0a 20 20 20 20 2a 2a 20 72 65  string.    ** re
1c1c8 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 20 20  presentation..  
1c1c9 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 30 3d 3d    */.    if( 0==
1c1ca 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d  (pRec->flags&MEM
1c1cb 5f 53 74 72 29 20 26 26 20 28 70 52 65 63 2d 3e  _Str) && (pRec->
1c1cc 66 6c 61 67 73 26 28 4d 45 4d 5f 52 65 61 6c 7c  flags&(MEM_Real|
1c1cd 4d 45 4d 5f 49 6e 74 29 29 20 29 7b 0a 20 20 20  MEM_Int)) ){.   
1c1ce 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
1c1cf 6d 53 74 72 69 6e 67 69 66 79 28 70 52 65 63 2c  mStringify(pRec,
1c1d0 20 65 6e 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20   enc);.    }.   
1c1d1 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26 3d 20   pRec->flags &= 
1c1d2 7e 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 49  ~(MEM_Real|MEM_I
1c1d3 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  nt);.  }else if(
1c1d4 20 61 66 66 69 6e 69 74 79 21 3d 53 51 4c 49 54   affinity!=SQLIT
1c1d5 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a 20 20  E_AFF_NONE ){.  
1c1d6 20 20 61 73 73 65 72 74 28 20 61 66 66 69 6e 69    assert( affini
1c1d7 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 49  ty==SQLITE_AFF_I
1c1d8 4e 54 45 47 45 52 20 7c 7c 20 61 66 66 69 6e 69  NTEGER || affini
1c1d9 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52  ty==SQLITE_AFF_R
1c1da 45 41 4c 0a 20 20 20 20 20 20 20 20 20 20 20 20  EAL.            
1c1db 20 7c 7c 20 61 66 66 69 6e 69 74 79 3d 3d 53 51   || affinity==SQ
1c1dc 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
1c1dd 20 29 3b 0a 20 20 20 20 61 70 70 6c 79 4e 75 6d   );.    applyNum
1c1de 65 72 69 63 41 66 66 69 6e 69 74 79 28 70 52 65  ericAffinity(pRe
1c1df 63 29 3b 0a 20 20 20 20 69 66 28 20 70 52 65 63  c);.    if( pRec
1c1e0 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65  ->flags & MEM_Re
1c1e1 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  al ){.      sqli
1c1e2 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 41 66  te3VdbeIntegerAf
1c1e3 66 69 6e 69 74 79 28 70 52 65 63 29 3b 0a 20 20  finity(pRec);.  
1c1e4 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
1c1e5 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20   Try to convert 
1c1e6 74 68 65 20 74 79 70 65 20 6f 66 20 61 20 66 75  the type of a fu
1c1e7 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20  nction argument 
1c1e8 6f 72 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75  or a result colu
1c1e9 6d 6e 0a 2a 2a 20 69 6e 74 6f 20 61 20 6e 75 6d  mn.** into a num
1c1ea 65 72 69 63 20 72 65 70 72 65 73 65 6e 74 61 74  eric representat
1c1eb 69 6f 6e 2e 20 20 55 73 65 20 65 69 74 68 65 72  ion.  Use either
1c1ec 20 49 4e 54 45 47 45 52 20 6f 72 20 52 45 41 4c   INTEGER or REAL
1c1ed 20 77 68 69 63 68 65 76 65 72 0a 2a 2a 20 69 73   whichever.** is
1c1ee 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20 20 42   appropriate.  B
1c1ef 75 74 20 6f 6e 6c 79 20 64 6f 20 74 68 65 20 63  ut only do the c
1c1f0 6f 6e 76 65 72 73 69 6f 6e 20 69 66 20 69 74 20  onversion if it 
1c1f1 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74 68  is possible with
1c1f2 6f 75 74 0a 2a 2a 20 6c 6f 73 73 20 6f 66 20 69  out.** loss of i
1c1f3 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e 64 20 72  nformation and r
1c1f4 65 74 75 72 6e 20 74 68 65 20 72 65 76 69 73 65  eturn the revise
1c1f5 64 20 74 79 70 65 20 6f 66 20 74 68 65 20 61 72  d type of the ar
1c1f6 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  gument..**.** Th
1c1f7 69 73 20 69 73 20 61 6e 20 45 58 50 45 52 49 4d  is is an EXPERIM
1c1f8 45 4e 54 41 4c 20 61 70 69 20 61 6e 64 20 69 73  ENTAL api and is
1c1f9 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e   subject to chan
1c1fa 67 65 20 6f 72 20 72 65 6d 6f 76 61 6c 2e 0a 2a  ge or removal..*
1c1fb 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1c1fc 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e   sqlite3_value_n
1c1fd 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71 6c 69  umeric_type(sqli
1c1fe 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29  te3_value *pVal)
1c1ff 7b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 3d 20  {.  Mem *pMem = 
1c200 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20 61 70  (Mem*)pVal;.  ap
1c201 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69  plyNumericAffini
1c202 74 79 28 70 4d 65 6d 29 3b 0a 20 20 73 71 6c 69  ty(pMem);.  sqli
1c203 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54  te3VdbeMemStoreT
1c204 79 70 65 28 70 4d 65 6d 29 3b 0a 20 20 72 65 74  ype(pMem);.  ret
1c205 75 72 6e 20 70 4d 65 6d 2d 3e 74 79 70 65 3b 0a  urn pMem->type;.
1c206 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 70 6f 72 74 65  }../*.** Exporte
1c207 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 61 70 70  d version of app
1c208 6c 79 41 66 66 69 6e 69 74 79 28 29 2e 20 54 68  lyAffinity(). Th
1c209 69 73 20 6f 6e 65 20 77 6f 72 6b 73 20 6f 6e 20  is one works on 
1c20a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20  sqlite3_value*, 
1c20b 0a 2a 2a 20 6e 6f 74 20 74 68 65 20 69 6e 74 65  .** not the inte
1c20c 72 6e 61 6c 20 4d 65 6d 2a 20 74 79 70 65 2e 0a  rnal Mem* type..
1c20d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1c20e 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 61  E void sqlite3Va
1c20f 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79  lueApplyAffinity
1c210 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  (.  sqlite3_valu
1c211 65 20 2a 70 56 61 6c 2c 20 0a 20 20 75 38 20 61  e *pVal, .  u8 a
1c212 66 66 69 6e 69 74 79 2c 20 0a 20 20 75 38 20 65  ffinity, .  u8 e
1c213 6e 63 0a 29 7b 0a 20 20 61 70 70 6c 79 41 66 66  nc.){.  applyAff
1c214 69 6e 69 74 79 28 28 4d 65 6d 20 2a 29 70 56 61  inity((Mem *)pVa
1c215 6c 2c 20 61 66 66 69 6e 69 74 79 2c 20 65 6e 63  l, affinity, enc
1c216 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c  );.}..#ifdef SQL
1c217 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20  ITE_DEBUG./*.** 
1c218 57 72 69 74 65 20 61 20 6e 69 63 65 20 73 74 72  Write a nice str
1c219 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69  ing representati
1c21a 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e  on of the conten
1c21b 74 73 20 6f 66 20 63 65 6c 6c 20 70 4d 65 6d 0a  ts of cell pMem.
1c21c 2a 2a 20 69 6e 74 6f 20 62 75 66 66 65 72 20 7a  ** into buffer z
1c21d 42 75 66 2c 20 6c 65 6e 67 74 68 20 6e 42 75 66  Buf, length nBuf
1c21e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1c21f 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1c220 56 64 62 65 4d 65 6d 50 72 65 74 74 79 50 72 69  VdbeMemPrettyPri
1c221 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 63 68  nt(Mem *pMem, ch
1c222 61 72 20 2a 7a 42 75 66 29 7b 0a 20 20 63 68 61  ar *zBuf){.  cha
1c223 72 20 2a 7a 43 73 72 20 3d 20 7a 42 75 66 3b 0a  r *zCsr = zBuf;.
1c224 20 20 69 6e 74 20 66 20 3d 20 70 4d 65 6d 2d 3e    int f = pMem->
1c225 66 6c 61 67 73 3b 0a 0a 20 20 73 74 61 74 69 63  flags;..  static
1c226 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
1c227 73 74 20 65 6e 63 6e 61 6d 65 73 5b 5d 20 3d 20  st encnames[] = 
1c228 7b 22 28 58 29 22 2c 20 22 28 38 29 22 2c 20 22  {"(X)", "(8)", "
1c229 28 31 36 4c 45 29 22 2c 20 22 28 31 36 42 45 29  (16LE)", "(16BE)
1c22a 22 7d 3b 0a 0a 20 20 69 66 28 20 66 26 4d 45 4d  "};..  if( f&MEM
1c22b 5f 42 6c 6f 62 20 29 7b 0a 20 20 20 20 69 6e 74  _Blob ){.    int
1c22c 20 69 3b 0a 20 20 20 20 63 68 61 72 20 63 3b 0a   i;.    char c;.
1c22d 20 20 20 20 69 66 28 20 66 20 26 20 4d 45 4d 5f      if( f & MEM_
1c22e 44 79 6e 20 29 7b 0a 20 20 20 20 20 20 63 20 3d  Dyn ){.      c =
1c22f 20 27 7a 27 3b 0a 20 20 20 20 20 20 61 73 73 65   'z';.      asse
1c230 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53 74  rt( (f & (MEM_St
1c231 61 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 29  atic|MEM_Ephem))
1c232 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ==0 );.    }else
1c233 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 53 74 61   if( f & MEM_Sta
1c234 74 69 63 20 29 7b 0a 20 20 20 20 20 20 63 20 3d  tic ){.      c =
1c235 20 27 74 27 3b 0a 20 20 20 20 20 20 61 73 73 65   't';.      asse
1c236 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 44 79  rt( (f & (MEM_Dy
1c237 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30  n|MEM_Ephem))==0
1c238 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   );.    }else if
1c239 28 20 66 20 26 20 4d 45 4d 5f 45 70 68 65 6d 20  ( f & MEM_Ephem 
1c23a 29 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 65 27  ){.      c = 'e'
1c23b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1c23c 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 63  (f & (MEM_Static
1c23d 7c 4d 45 4d 5f 44 79 6e 29 29 3d 3d 30 20 29 3b  |MEM_Dyn))==0 );
1c23e 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1c23f 20 20 63 20 3d 20 27 73 27 3b 0a 20 20 20 20 7d    c = 's';.    }
1c240 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  ..    sqlite3_sn
1c241 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72  printf(100, zCsr
1c242 2c 20 22 25 63 22 2c 20 63 29 3b 0a 20 20 20 20  , "%c", c);.    
1c243 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74 65 33 53  zCsr += sqlite3S
1c244 74 72 6c 65 6e 33 30 28 7a 43 73 72 29 3b 0a 20  trlen30(zCsr);. 
1c245 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1c246 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 22  ntf(100, zCsr, "
1c247 25 64 5b 22 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a  %d[", pMem->n);.
1c248 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69      zCsr += sqli
1c249 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72  te3Strlen30(zCsr
1c24a 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  );.    for(i=0; 
1c24b 69 3c 31 36 20 26 26 20 69 3c 70 4d 65 6d 2d 3e  i<16 && i<pMem->
1c24c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73  n; i++){.      s
1c24d 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1c24e 31 30 30 2c 20 7a 43 73 72 2c 20 22 25 30 32 58  100, zCsr, "%02X
1c24f 22 2c 20 28 28 69 6e 74 29 70 4d 65 6d 2d 3e 7a  ", ((int)pMem->z
1c250 5b 69 5d 20 26 20 30 78 46 46 29 29 3b 0a 20 20  [i] & 0xFF));.  
1c251 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69      zCsr += sqli
1c252 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72  te3Strlen30(zCsr
1c253 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  );.    }.    for
1c254 28 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 3c  (i=0; i<16 && i<
1c255 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20  pMem->n; i++){. 
1c256 20 20 20 20 20 63 68 61 72 20 7a 20 3d 20 70 4d       char z = pM
1c257 65 6d 2d 3e 7a 5b 69 5d 3b 0a 20 20 20 20 20 20  em->z[i];.      
1c258 69 66 28 20 7a 3c 33 32 20 7c 7c 20 7a 3e 31 32  if( z<32 || z>12
1c259 36 20 29 20 2a 7a 43 73 72 2b 2b 20 3d 20 27 2e  6 ) *zCsr++ = '.
1c25a 27 3b 0a 20 20 20 20 20 20 65 6c 73 65 20 2a 7a  ';.      else *z
1c25b 43 73 72 2b 2b 20 3d 20 7a 3b 0a 20 20 20 20 7d  Csr++ = z;.    }
1c25c 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  ..    sqlite3_sn
1c25d 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72  printf(100, zCsr
1c25e 2c 20 22 5d 25 73 22 2c 20 65 6e 63 6e 61 6d 65  , "]%s", encname
1c25f 73 5b 70 4d 65 6d 2d 3e 65 6e 63 5d 29 3b 0a 20  s[pMem->enc]);. 
1c260 20 20 20 7a 43 73 72 20 2b 3d 20 73 71 6c 69 74     zCsr += sqlit
1c261 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 73 72 29  e3Strlen30(zCsr)
1c262 3b 0a 20 20 20 20 69 66 28 20 66 20 26 20 4d 45  ;.    if( f & ME
1c263 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 20 20  M_Zero ){.      
1c264 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1c265 28 31 30 30 2c 20 7a 43 73 72 2c 22 2b 25 64 7a  (100, zCsr,"+%dz
1c266 22 2c 70 4d 65 6d 2d 3e 75 2e 6e 5a 65 72 6f 29  ",pMem->u.nZero)
1c267 3b 0a 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20  ;.      zCsr += 
1c268 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
1c269 7a 43 73 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20  zCsr);.    }.   
1c26a 20 2a 7a 43 73 72 20 3d 20 27 5c 30 27 3b 0a 20   *zCsr = '\0';. 
1c26b 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d   }else if( f & M
1c26c 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20 20 69 6e  EM_Str ){.    in
1c26d 74 20 6a 2c 20 6b 3b 0a 20 20 20 20 7a 42 75 66  t j, k;.    zBuf
1c26e 5b 30 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 69  [0] = ' ';.    i
1c26f 66 28 20 66 20 26 20 4d 45 4d 5f 44 79 6e 20 29  f( f & MEM_Dyn )
1c270 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 31 5d 20  {.      zBuf[1] 
1c271 3d 20 27 7a 27 3b 0a 20 20 20 20 20 20 61 73 73  = 'z';.      ass
1c272 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d 5f 53  ert( (f & (MEM_S
1c273 74 61 74 69 63 7c 4d 45 4d 5f 45 70 68 65 6d 29  tatic|MEM_Ephem)
1c274 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73  )==0 );.    }els
1c275 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 53 74  e if( f & MEM_St
1c276 61 74 69 63 20 29 7b 0a 20 20 20 20 20 20 7a 42  atic ){.      zB
1c277 75 66 5b 31 5d 20 3d 20 27 74 27 3b 0a 20 20 20  uf[1] = 't';.   
1c278 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20     assert( (f & 
1c279 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68  (MEM_Dyn|MEM_Eph
1c27a 65 6d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d  em))==0 );.    }
1c27b 65 6c 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d  else if( f & MEM
1c27c 5f 45 70 68 65 6d 20 29 7b 0a 20 20 20 20 20 20  _Ephem ){.      
1c27d 7a 42 75 66 5b 31 5d 20 3d 20 27 65 27 3b 0a 20  zBuf[1] = 'e';. 
1c27e 20 20 20 20 20 61 73 73 65 72 74 28 20 28 66 20       assert( (f 
1c27f 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45  & (MEM_Static|ME
1c280 4d 5f 44 79 6e 29 29 3d 3d 30 20 29 3b 0a 20 20  M_Dyn))==0 );.  
1c281 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a    }else{.      z
1c282 42 75 66 5b 31 5d 20 3d 20 27 73 27 3b 0a 20 20  Buf[1] = 's';.  
1c283 20 20 7d 0a 20 20 20 20 6b 20 3d 20 32 3b 0a 20    }.    k = 2;. 
1c284 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1c285 6e 74 66 28 31 30 30 2c 20 26 7a 42 75 66 5b 6b  ntf(100, &zBuf[k
1c286 5d 2c 20 22 25 64 22 2c 20 70 4d 65 6d 2d 3e 6e  ], "%d", pMem->n
1c287 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 73 71 6c 69  );.    k += sqli
1c288 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 42 75  te3Strlen30(&zBu
1c289 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a 42 75 66 5b  f[k]);.    zBuf[
1c28a 6b 2b 2b 5d 20 3d 20 27 5b 27 3b 0a 20 20 20 20  k++] = '[';.    
1c28b 66 6f 72 28 6a 3d 30 3b 20 6a 3c 31 35 20 26 26  for(j=0; j<15 &&
1c28c 20 6a 3c 70 4d 65 6d 2d 3e 6e 3b 20 6a 2b 2b 29   j<pMem->n; j++)
1c28d 7b 0a 20 20 20 20 20 20 75 38 20 63 20 3d 20 70  {.      u8 c = p
1c28e 4d 65 6d 2d 3e 7a 5b 6a 5d 3b 0a 20 20 20 20 20  Mem->z[j];.     
1c28f 20 69 66 28 20 63 3e 3d 30 78 32 30 20 26 26 20   if( c>=0x20 && 
1c290 63 3c 30 78 37 66 20 29 7b 0a 20 20 20 20 20 20  c<0x7f ){.      
1c291 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d 20 63 3b    zBuf[k++] = c;
1c292 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
1c293 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20        zBuf[k++] 
1c294 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 7d 0a 20  = '.';.      }. 
1c295 20 20 20 7d 0a 20 20 20 20 7a 42 75 66 5b 6b 2b     }.    zBuf[k+
1c296 2b 5d 20 3d 20 27 5d 27 3b 0a 20 20 20 20 73 71  +] = ']';.    sq
1c297 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 31  lite3_snprintf(1
1c298 30 30 2c 26 7a 42 75 66 5b 6b 5d 2c 20 65 6e 63  00,&zBuf[k], enc
1c299 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65 6e 63 5d  names[pMem->enc]
1c29a 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 73 71 6c 69  );.    k += sqli
1c29b 74 65 33 53 74 72 6c 65 6e 33 30 28 26 7a 42 75  te3Strlen30(&zBu
1c29c 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a 42 75 66 5b  f[k]);.    zBuf[
1c29d 6b 2b 2b 5d 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a  k++] = 0;.  }.}.
1c29e 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
1c29f 51 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a  QLITE_DEBUG./*.*
1c2a0 2a 20 50 72 69 6e 74 20 74 68 65 20 76 61 6c 75  * Print the valu
1c2a1 65 20 6f 66 20 61 20 72 65 67 69 73 74 65 72 20  e of a register 
1c2a2 66 6f 72 20 74 72 61 63 69 6e 67 20 70 75 72 70  for tracing purp
1c2a3 6f 73 65 73 3a 0a 2a 2f 0a 73 74 61 74 69 63 20  oses:.*/.static 
1c2a4 76 6f 69 64 20 6d 65 6d 54 72 61 63 65 50 72 69  void memTracePri
1c2a5 6e 74 28 46 49 4c 45 20 2a 6f 75 74 2c 20 4d 65  nt(FILE *out, Me
1c2a6 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e  m *p){.  if( p->
1c2a7 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
1c2a8 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28   ){.    fprintf(
1c2a9 6f 75 74 2c 20 22 20 4e 55 4c 4c 22 29 3b 0a 20  out, " NULL");. 
1c2aa 20 7d 65 6c 73 65 20 69 66 28 20 28 70 2d 3e 66   }else if( (p->f
1c2ab 6c 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c  lags & (MEM_Int|
1c2ac 4d 45 4d 5f 53 74 72 29 29 3d 3d 28 4d 45 4d 5f  MEM_Str))==(MEM_
1c2ad 49 6e 74 7c 4d 45 4d 5f 53 74 72 29 20 29 7b 0a  Int|MEM_Str) ){.
1c2ae 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
1c2af 20 22 20 73 69 3a 25 6c 6c 64 22 2c 20 70 2d 3e   " si:%lld", p->
1c2b0 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  u.i);.  }else if
1c2b1 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  ( p->flags & MEM
1c2b2 5f 49 6e 74 20 29 7b 0a 20 20 20 20 66 70 72 69  _Int ){.    fpri
1c2b3 6e 74 66 28 6f 75 74 2c 20 22 20 69 3a 25 6c 6c  ntf(out, " i:%ll
1c2b4 64 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a 23 69 66  d", p->u.i);.#if
1c2b5 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1c2b6 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
1c2b7 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66    }else if( p->f
1c2b8 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 20  lags & MEM_Real 
1c2b9 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f  ){.    fprintf(o
1c2ba 75 74 2c 20 22 20 72 3a 25 67 22 2c 20 70 2d 3e  ut, " r:%g", p->
1c2bb 72 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c  r);.#endif.  }el
1c2bc 73 65 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20  se if( p->flags 
1c2bd 26 20 4d 45 4d 5f 52 6f 77 53 65 74 20 29 7b 0a  & MEM_RowSet ){.
1c2be 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
1c2bf 20 22 20 28 72 6f 77 73 65 74 29 22 29 3b 0a 20   " (rowset)");. 
1c2c0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72   }else{.    char
1c2c1 20 7a 42 75 66 5b 32 30 30 5d 3b 0a 20 20 20 20   zBuf[200];.    
1c2c2 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 50 72  sqlite3VdbeMemPr
1c2c3 65 74 74 79 50 72 69 6e 74 28 70 2c 20 7a 42 75  ettyPrint(p, zBu
1c2c4 66 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28  f);.    fprintf(
1c2c5 6f 75 74 2c 20 22 20 22 29 3b 0a 20 20 20 20 66  out, " ");.    f
1c2c6 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 73 22  printf(out, "%s"
1c2c7 2c 20 7a 42 75 66 29 3b 0a 20 20 7d 0a 7d 0a 73  , zBuf);.  }.}.s
1c2c8 74 61 74 69 63 20 76 6f 69 64 20 72 65 67 69 73  tatic void regis
1c2c9 74 65 72 54 72 61 63 65 28 46 49 4c 45 20 2a 6f  terTrace(FILE *o
1c2ca 75 74 2c 20 69 6e 74 20 69 52 65 67 2c 20 4d 65  ut, int iReg, Me
1c2cb 6d 20 2a 70 29 7b 0a 20 20 66 70 72 69 6e 74 66  m *p){.  fprintf
1c2cc 28 6f 75 74 2c 20 22 52 45 47 5b 25 64 5d 20 3d  (out, "REG[%d] =
1c2cd 20 22 2c 20 69 52 65 67 29 3b 0a 20 20 6d 65 6d   ", iReg);.  mem
1c2ce 54 72 61 63 65 50 72 69 6e 74 28 6f 75 74 2c 20  TracePrint(out, 
1c2cf 70 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75  p);.  fprintf(ou
1c2d0 74 2c 20 22 5c 6e 22 29 3b 0a 7d 0a 23 65 6e 64  t, "\n");.}.#end
1c2d1 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
1c2d2 45 5f 44 45 42 55 47 0a 23 20 20 64 65 66 69 6e  E_DEBUG.#  defin
1c2d3 65 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45  e REGISTER_TRACE
1c2d4 28 52 2c 4d 29 20 69 66 28 70 2d 3e 74 72 61 63  (R,M) if(p->trac
1c2d5 65 29 72 65 67 69 73 74 65 72 54 72 61 63 65 28  e)registerTrace(
1c2d6 70 2d 3e 74 72 61 63 65 2c 52 2c 4d 29 0a 23 65  p->trace,R,M).#e
1c2d7 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 52 45  lse.#  define RE
1c2d8 47 49 53 54 45 52 5f 54 52 41 43 45 28 52 2c 4d  GISTER_TRACE(R,M
1c2d9 29 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 64 65  ).#endif...#ifde
1c2da 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a 0a  f VDBE_PROFILE..
1c2db 2f 2a 20 0a 2a 2a 20 68 77 74 69 6d 65 2e 68 20  /* .** hwtime.h 
1c2dc 63 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20  contains inline 
1c2dd 61 73 73 65 6d 62 6c 65 72 20 63 6f 64 65 20 66  assembler code f
1c2de 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20  or implementing 
1c2df 0a 2a 2a 20 68 69 67 68 2d 70 65 72 66 6f 72 6d  .** high-perform
1c2e0 61 6e 63 65 20 74 69 6d 69 6e 67 20 72 6f 75 74  ance timing rout
1c2e1 69 6e 65 73 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  ines..*/./******
1c2e2 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
1c2e3 20 68 77 74 69 6d 65 2e 68 20 69 6e 20 74 68 65   hwtime.h in the
1c2e4 20 6d 69 64 64 6c 65 20 6f 66 20 76 64 62 65 2e   middle of vdbe.
1c2e5 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1c2e6 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
1c2e7 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
1c2e8 69 6c 65 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a  ile hwtime.h ***
1c2e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c2ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c2eb 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
1c2ec 30 30 38 20 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a  008 May 27.**.**
1c2ed 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1c2ee 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1c2ef 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1c2f0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1c2f1 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1c2f2 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1c2f3 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1c2f4 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1c2f5 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1c2f6 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1c2f7 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1c2f8 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1c2f9 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1c2fa 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1c2fb 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1c2fc 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1c2fd 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1c2fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c2ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c301 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c302 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
1c303 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
1c304 6f 6e 74 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61  ontains inline a
1c305 73 6d 20 63 6f 64 65 20 66 6f 72 20 72 65 74 72  sm code for retr
1c306 69 65 76 69 6e 67 20 22 68 69 67 68 2d 70 65 72  ieving "high-per
1c307 66 6f 72 6d 61 6e 63 65 22 0a 2a 2a 20 63 6f 75  formance".** cou
1c308 6e 74 65 72 73 20 66 6f 72 20 78 38 36 20 63 6c  nters for x86 cl
1c309 61 73 73 20 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20  ass CPUs..**.** 
1c30a 24 49 64 3a 20 68 77 74 69 6d 65 2e 68 2c 76 20  $Id: hwtime.h,v 
1c30b 31 2e 33 20 32 30 30 38 2f 30 38 2f 30 31 20 31  1.3 2008/08/01 1
1c30c 34 3a 33 33 3a 31 35 20 73 68 61 6e 65 20 45 78  4:33:15 shane Ex
1c30d 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f  p $.*/.#ifndef _
1c30e 48 57 54 49 4d 45 5f 48 5f 0a 23 64 65 66 69 6e  HWTIME_H_.#defin
1c30f 65 20 5f 48 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a  e _HWTIME_H_../*
1c310 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
1c311 67 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 77  g routine only w
1c312 6f 72 6b 73 20 6f 6e 20 70 65 6e 74 69 75 6d 2d  orks on pentium-
1c313 63 6c 61 73 73 20 28 6f 72 20 6e 65 77 65 72 29  class (or newer)
1c314 20 70 72 6f 63 65 73 73 6f 72 73 2e 0a 2a 2a 20   processors..** 
1c315 49 74 20 75 73 65 73 20 74 68 65 20 52 44 54 53  It uses the RDTS
1c316 43 20 6f 70 63 6f 64 65 20 74 6f 20 72 65 61 64  C opcode to read
1c317 20 74 68 65 20 63 79 63 6c 65 20 63 6f 75 6e 74   the cycle count
1c318 20 76 61 6c 75 65 20 6f 75 74 20 6f 66 20 74 68   value out of th
1c319 65 0a 2a 2a 20 70 72 6f 63 65 73 73 6f 72 20 61  e.** processor a
1c31a 6e 64 20 72 65 74 75 72 6e 73 20 74 68 61 74 20  nd returns that 
1c31b 76 61 6c 75 65 2e 20 20 54 68 69 73 20 63 61 6e  value.  This can
1c31c 20 62 65 20 75 73 65 64 20 66 6f 72 20 68 69 67   be used for hig
1c31d 68 2d 72 65 73 0a 2a 2a 20 70 72 6f 66 69 6c 69  h-res.** profili
1c31e 6e 67 2e 0a 2a 2f 0a 23 69 66 20 28 64 65 66 69  ng..*/.#if (defi
1c31f 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c  ned(__GNUC__) ||
1c320 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45   defined(_MSC_VE
1c321 52 29 29 20 26 26 20 5c 0a 20 20 20 20 20 20 28  R)) && \.      (
1c322 64 65 66 69 6e 65 64 28 69 33 38 36 29 20 7c 7c  defined(i386) ||
1c323 20 64 65 66 69 6e 65 64 28 5f 5f 69 33 38 36 5f   defined(__i386_
1c324 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 4d  _) || defined(_M
1c325 5f 49 58 38 36 29 29 0a 0a 20 20 23 69 66 20 64  _IX86))..  #if d
1c326 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29  efined(__GNUC__)
1c327 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73  ..  __inline__ s
1c328 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
1c329 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
1c32a 7b 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  {.     unsigned 
1c32b 69 6e 74 20 6c 6f 2c 20 68 69 3b 0a 20 20 20 20  int lo, hi;.    
1c32c 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74   __asm__ __volat
1c32d 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a  ile__ ("rdtsc" :
1c32e 20 22 3d 61 22 20 28 6c 6f 29 2c 20 22 3d 64 22   "=a" (lo), "=d"
1c32f 20 28 68 69 29 29 3b 0a 20 20 20 20 20 72 65 74   (hi));.     ret
1c330 75 72 6e 20 28 73 71 6c 69 74 65 5f 75 69 6e 74  urn (sqlite_uint
1c331 36 34 29 68 69 20 3c 3c 20 33 32 20 7c 20 6c 6f  64)hi << 32 | lo
1c332 3b 0a 20 20 7d 0a 0a 20 20 23 65 6c 69 66 20 64  ;.  }..  #elif d
1c333 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29  efined(_MSC_VER)
1c334 0a 0a 20 20 5f 5f 64 65 63 6c 73 70 65 63 28 6e  ..  __declspec(n
1c335 61 6b 65 64 29 20 5f 5f 69 6e 6c 69 6e 65 20 73  aked) __inline s
1c336 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 5f 5f 63  qlite_uint64 __c
1c337 64 65 63 6c 20 73 71 6c 69 74 65 33 48 77 74 69  decl sqlite3Hwti
1c338 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 5f  me(void){.     _
1c339 5f 61 73 6d 20 7b 0a 20 20 20 20 20 20 20 20 72  _asm {.        r
1c33a 64 74 73 63 0a 20 20 20 20 20 20 20 20 72 65 74  dtsc.        ret
1c33b 20 20 20 20 20 20 20 3b 20 72 65 74 75 72 6e 20         ; return 
1c33c 76 61 6c 75 65 20 61 74 20 45 44 58 3a 45 41 58  value at EDX:EAX
1c33d 0a 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 23  .     }.  }..  #
1c33e 65 6e 64 69 66 0a 0a 23 65 6c 69 66 20 28 64 65  endif..#elif (de
1c33f 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20  fined(__GNUC__) 
1c340 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 78 38 36  && defined(__x86
1c341 5f 36 34 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c  _64__))..  __inl
1c342 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e  ine__ sqlite_uin
1c343 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t64 sqlite3Hwtim
1c344 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75  e(void){.      u
1c345 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 76 61 6c  nsigned long val
1c346 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20  ;.      __asm__ 
1c347 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72  __volatile__ ("r
1c348 64 74 73 63 22 20 3a 20 22 3d 41 22 20 28 76 61  dtsc" : "=A" (va
1c349 6c 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  l));.      retur
1c34a 6e 20 76 61 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c  n val;.  }. .#el
1c34b 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e  if (defined(__GN
1c34c 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64  UC__) && defined
1c34d 28 5f 5f 70 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f  (__ppc__))..  __
1c34e 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f  inline__ sqlite_
1c34f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77  uint64 sqlite3Hw
1c350 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20  time(void){.    
1c351 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20    unsigned long 
1c352 6c 6f 6e 67 20 72 65 74 76 61 6c 3b 0a 20 20 20  long retval;.   
1c353 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67     unsigned long
1c354 20 6a 75 6e 6b 3b 0a 20 20 20 20 20 20 5f 5f 61   junk;.      __a
1c355 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f  sm__ __volatile_
1c356 5f 20 28 22 5c 6e 5c 0a 20 20 20 20 20 20 20 20  _ ("\n\.        
1c357 20 20 31 3a 20 20 20 20 20 20 6d 66 74 62 75 20    1:      mftbu 
1c358 20 20 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20    %1\n\.        
1c359 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 20 20            mftb  
1c35a 20 20 25 4c 30 5c 6e 5c 0a 20 20 20 20 20 20 20    %L0\n\.       
1c35b 20 20 20 20 20 20 20 20 20 20 20 6d 66 74 62 75             mftbu
1c35c 20 20 20 25 30 5c 6e 5c 0a 20 20 20 20 20 20 20     %0\n\.       
1c35d 20 20 20 20 20 20 20 20 20 20 20 63 6d 70 77 20             cmpw 
1c35e 20 20 20 25 30 2c 25 31 5c 6e 5c 0a 20 20 20 20     %0,%1\n\.    
1c35f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6e                bn
1c360 65 20 20 20 20 20 31 62 22 0a 20 20 20 20 20 20  e     1b".      
1c361 20 20 20 20 20 20 20 20 20 20 20 20 3a 20 22 3d              : "=
1c362 72 22 20 28 72 65 74 76 61 6c 29 2c 20 22 3d 72  r" (retval), "=r
1c363 22 20 28 6a 75 6e 6b 29 29 3b 0a 20 20 20 20 20  " (junk));.     
1c364 20 72 65 74 75 72 6e 20 72 65 74 76 61 6c 3b 0a   return retval;.
1c365 20 20 7d 0a 0a 23 65 6c 73 65 0a 0a 20 20 23 65    }..#else..  #e
1c366 72 72 6f 72 20 4e 65 65 64 20 69 6d 70 6c 65 6d  rror Need implem
1c367 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c 69  entation of sqli
1c368 74 65 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20  te3Hwtime() for 
1c369 79 6f 75 72 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a  your platform...
1c36a 20 20 2f 2a 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d    /*.  ** To com
1c36b 70 69 6c 65 20 77 69 74 68 6f 75 74 20 69 6d 70  pile without imp
1c36c 6c 65 6d 65 6e 74 69 6e 67 20 73 71 6c 69 74 65  lementing sqlite
1c36d 33 48 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f  3Hwtime() for yo
1c36e 75 72 20 70 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a  ur platform,.  *
1c36f 2a 20 79 6f 75 20 63 61 6e 20 72 65 6d 6f 76 65  * you can remove
1c370 20 74 68 65 20 61 62 6f 76 65 20 23 65 72 72 6f   the above #erro
1c371 72 20 61 6e 64 20 75 73 65 20 74 68 65 20 66 6f  r and use the fo
1c372 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20 73 74 75  llowing.  ** stu
1c373 62 20 66 75 6e 63 74 69 6f 6e 2e 20 20 59 6f 75  b function.  You
1c374 20 77 69 6c 6c 20 6c 6f 73 65 20 74 69 6d 69 6e   will lose timin
1c375 67 20 73 75 70 70 6f 72 74 20 66 6f 72 20 6d 61  g support for ma
1c376 6e 79 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 64  ny.  ** of the d
1c377 65 62 75 67 67 69 6e 67 20 61 6e 64 20 74 65 73  ebugging and tes
1c378 74 69 6e 67 20 75 74 69 6c 69 74 69 65 73 2c 20  ting utilities, 
1c379 62 75 74 20 69 74 20 73 68 6f 75 6c 64 20 61 74  but it should at
1c37a 0a 20 20 2a 2a 20 6c 65 61 73 74 20 63 6f 6d 70  .  ** least comp
1c37b 69 6c 65 20 61 6e 64 20 72 75 6e 2e 0a 20 20 2a  ile and run..  *
1c37c 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1c37d 20 20 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34     sqlite_uint64
1c37e 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76   sqlite3Hwtime(v
1c37f 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 28 28 73  oid){ return ((s
1c380 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 3b  qlite_uint64)0);
1c381 20 7d 0a 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64   }..#endif..#end
1c382 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 5f  if /* !defined(_
1c383 48 57 54 49 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f  HWTIME_H_) */../
1c384 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
1c385 6e 64 20 6f 66 20 68 77 74 69 6d 65 2e 68 20 2a  nd of hwtime.h *
1c386 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c387 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c388 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1c389 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
1c38a 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
1c38b 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 76  we left off in v
1c38c 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dbe.c **********
1c38d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
1c38e 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
1c38f 65 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45  e CHECK_FOR_INTE
1c390 52 52 55 50 54 20 6d 61 63 72 6f 20 64 65 66 69  RRUPT macro defi
1c391 6e 65 64 20 68 65 72 65 20 6c 6f 6f 6b 73 20 74  ned here looks t
1c392 6f 20 73 65 65 20 69 66 20 74 68 65 0a 2a 2a 20  o see if the.** 
1c393 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
1c394 74 28 29 20 72 6f 75 74 69 6e 65 20 68 61 73 20  t() routine has 
1c395 62 65 65 6e 20 63 61 6c 6c 65 64 2e 20 20 49 66  been called.  If
1c396 20 69 74 20 68 61 73 20 62 65 65 6e 2c 20 74 68   it has been, th
1c397 65 6e 0a 2a 2a 20 70 72 6f 63 65 73 73 69 6e 67  en.** processing
1c398 20 6f 66 20 74 68 65 20 56 44 42 45 20 70 72 6f   of the VDBE pro
1c399 67 72 61 6d 20 69 73 20 69 6e 74 65 72 72 75 70  gram is interrup
1c39a 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ted..**.** This 
1c39b 6d 61 63 72 6f 20 61 64 64 65 64 20 74 6f 20 65  macro added to e
1c39c 76 65 72 79 20 69 6e 73 74 72 75 63 74 69 6f 6e  very instruction
1c39d 20 74 68 61 74 20 64 6f 65 73 20 61 20 6a 75 6d   that does a jum
1c39e 70 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a  p in order to.**
1c39f 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 6c 6f 6f   implement a loo
1c3a0 70 2e 20 20 54 68 69 73 20 74 65 73 74 20 75 73  p.  This test us
1c3a1 65 64 20 74 6f 20 62 65 20 6f 6e 20 65 76 65 72  ed to be on ever
1c3a2 79 20 73 69 6e 67 6c 65 20 69 6e 73 74 72 75 63  y single instruc
1c3a3 74 69 6f 6e 2c 0a 2a 2a 20 62 75 74 20 74 68 61  tion,.** but tha
1c3a4 74 20 6d 65 61 6e 74 20 77 65 20 6d 6f 72 65 20  t meant we more 
1c3a5 74 65 73 74 69 6e 67 20 74 68 61 74 20 77 65 20  testing that we 
1c3a6 6e 65 65 64 65 64 2e 20 20 42 79 20 6f 6e 6c 79  needed.  By only
1c3a7 20 74 65 73 74 69 6e 67 20 74 68 65 0a 2a 2a 20   testing the.** 
1c3a8 66 6c 61 67 20 6f 6e 20 6a 75 6d 70 20 69 6e 73  flag on jump ins
1c3a9 74 72 75 63 74 69 6f 6e 73 2c 20 77 65 20 67 65  tructions, we ge
1c3aa 74 20 61 20 28 73 6d 61 6c 6c 29 20 73 70 65 65  t a (small) spee
1c3ab 64 20 69 6d 70 72 6f 76 65 6d 65 6e 74 2e 0a 2a  d improvement..*
1c3ac 2f 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b 5f  /.#define CHECK_
1c3ad 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 20 5c 0a  FOR_INTERRUPT \.
1c3ae 20 20 20 69 66 28 20 64 62 2d 3e 75 31 2e 69 73     if( db->u1.is
1c3af 49 6e 74 65 72 72 75 70 74 65 64 20 29 20 67 6f  Interrupted ) go
1c3b0 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1c3b1 69 6e 74 65 72 72 75 70 74 3b 0a 0a 23 69 66 64  interrupt;..#ifd
1c3b2 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
1c3b3 73 74 61 74 69 63 20 69 6e 74 20 66 69 6c 65 45  static int fileE
1c3b4 78 69 73 74 73 28 73 71 6c 69 74 65 33 20 2a 64  xists(sqlite3 *d
1c3b5 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
1c3b6 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72 65 73  File){.  int res
1c3b7 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d   = 0;.  int rc =
1c3b8 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 69 66 64   SQLITE_OK;.#ifd
1c3b9 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
1c3ba 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 63 75   /* If we are cu
1c3bb 72 72 65 6e 74 6c 79 20 74 65 73 74 69 6e 67 20  rrently testing 
1c3bc 49 4f 20 65 72 72 6f 72 73 2c 20 74 68 65 6e 20  IO errors, then 
1c3bd 64 6f 20 6e 6f 74 20 63 61 6c 6c 20 4f 73 41 63  do not call OsAc
1c3be 63 65 73 73 28 29 20 74 6f 0a 20 20 2a 2a 20 74  cess() to.  ** t
1c3bf 65 73 74 20 66 6f 72 20 74 68 65 20 70 72 65 73  est for the pres
1c3c0 65 6e 63 65 20 6f 66 20 7a 46 69 6c 65 2e 20 54  ence of zFile. T
1c3c1 68 69 73 20 69 73 20 62 65 63 61 75 73 65 20 61  his is because a
1c3c2 6e 79 20 49 4f 20 65 72 72 6f 72 20 74 68 61 74  ny IO error that
1c3c3 0a 20 20 2a 2a 20 6f 63 63 75 72 73 20 68 65 72  .  ** occurs her
1c3c4 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65  e will not be re
1c3c5 70 6f 72 74 65 64 2c 20 63 61 75 73 69 6e 67 20  ported, causing 
1c3c6 74 68 65 20 74 65 73 74 20 74 6f 20 66 61 69 6c  the test to fail
1c3c7 2e 0a 20 20 2a 2f 0a 20 20 65 78 74 65 72 6e 20  ..  */.  extern 
1c3c8 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
1c3c9 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b 0a 20 20  rror_pending;.  
1c3ca 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  if( sqlite3_io_e
1c3cb 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3c 3d 30 20  rror_pending<=0 
1c3cc 29 0a 23 65 6e 64 69 66 0a 20 20 20 20 72 63 20  ).#endif.    rc 
1c3cd 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73  = sqlite3OsAcces
1c3ce 73 28 64 62 2d 3e 70 56 66 73 2c 20 7a 46 69 6c  s(db->pVfs, zFil
1c3cf 65 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  e, SQLITE_ACCESS
1c3d0 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29 3b 0a  _EXISTS, &res);.
1c3d1 20 20 72 65 74 75 72 6e 20 28 72 65 73 20 26 26    return (res &&
1c3d2 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 3b   rc==SQLITE_OK);
1c3d3 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  .}.#endif..#ifnd
1c3d4 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20  ef NDEBUG./*.** 
1c3d5 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
1c3d6 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72 6f   only called fro
1c3d7 6d 20 77 69 74 68 69 6e 20 61 6e 20 61 73 73 65  m within an asse
1c3d8 72 74 28 29 20 65 78 70 72 65 73 73 69 6f 6e 2e  rt() expression.
1c3d9 20 49 74 0a 2a 2a 20 63 68 65 63 6b 73 20 74 68   It.** checks th
1c3da 61 74 20 74 68 65 20 73 71 6c 69 74 65 33 2e 6e  at the sqlite3.n
1c3db 54 72 61 6e 73 61 63 74 69 6f 6e 20 76 61 72 69  Transaction vari
1c3dc 61 62 6c 65 20 69 73 20 63 6f 72 72 65 63 74 6c  able is correctl
1c3dd 79 20 73 65 74 20 74 6f 0a 2a 2a 20 74 68 65 20  y set to.** the 
1c3de 6e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 74 72  number of non-tr
1c3df 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f  ansaction savepo
1c3e0 69 6e 74 73 20 63 75 72 72 65 6e 74 6c 79 20 69  ints currently i
1c3e1 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 6e 6b 65 64  n the .** linked
1c3e2 20 6c 69 73 74 20 73 74 61 72 74 69 6e 67 20 61   list starting a
1c3e3 74 20 73 71 6c 69 74 65 33 2e 70 53 61 76 65 70  t sqlite3.pSavep
1c3e4 6f 69 6e 74 2e 0a 2a 2a 20 0a 2a 2a 20 55 73 61  oint..** .** Usa
1c3e5 67 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 73  ge:.**.**     as
1c3e6 73 65 72 74 28 20 63 68 65 63 6b 53 61 76 65 70  sert( checkSavep
1c3e7 6f 69 6e 74 43 6f 75 6e 74 28 64 62 29 20 29 3b  ointCount(db) );
1c3e8 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
1c3e9 68 65 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75  heckSavepointCou
1c3ea 6e 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  nt(sqlite3 *db){
1c3eb 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20  .  int n = 0;.  
1c3ec 53 61 76 65 70 6f 69 6e 74 20 2a 70 3b 0a 20 20  Savepoint *p;.  
1c3ed 66 6f 72 28 70 3d 64 62 2d 3e 70 53 61 76 65 70  for(p=db->pSavep
1c3ee 6f 69 6e 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e  oint; p; p=p->pN
1c3ef 65 78 74 29 20 6e 2b 2b 3b 0a 20 20 61 73 73 65  ext) n++;.  asse
1c3f0 72 74 28 20 6e 3d 3d 28 64 62 2d 3e 6e 53 61 76  rt( n==(db->nSav
1c3f1 65 70 6f 69 6e 74 20 2b 20 64 62 2d 3e 69 73 54  epoint + db->isT
1c3f2 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70 6f  ransactionSavepo
1c3f3 69 6e 74 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  int) );.  return
1c3f4 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a   1;.}.#endif../*
1c3f5 0a 2a 2a 20 45 78 65 63 75 74 65 20 61 73 20 6d  .** Execute as m
1c3f6 75 63 68 20 6f 66 20 61 20 56 44 42 45 20 70 72  uch of a VDBE pr
1c3f7 6f 67 72 61 6d 20 61 73 20 77 65 20 63 61 6e 20  ogram as we can 
1c3f8 74 68 65 6e 20 72 65 74 75 72 6e 2e 0a 2a 2a 0a  then return..**.
1c3f9 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61  ** sqlite3VdbeMa
1c3fa 6b 65 52 65 61 64 79 28 29 20 6d 75 73 74 20 62  keReady() must b
1c3fb 65 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20  e called before 
1c3fc 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6e 20  this routine in 
1c3fd 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 6c 6f 73  order to.** clos
1c3fe 65 20 74 68 65 20 70 72 6f 67 72 61 6d 20 77 69  e the program wi
1c3ff 74 68 20 61 20 66 69 6e 61 6c 20 4f 50 5f 48 61  th a final OP_Ha
1c400 6c 74 20 61 6e 64 20 74 6f 20 73 65 74 20 75 70  lt and to set up
1c401 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 73 0a 2a   the callbacks.*
1c402 2a 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20  * and the error 
1c403 6d 65 73 73 61 67 65 20 70 6f 69 6e 74 65 72 2e  message pointer.
1c404 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 65 76 65 72 20  .**.** Whenever 
1c405 61 20 72 6f 77 20 6f 72 20 72 65 73 75 6c 74 20  a row or result 
1c406 64 61 74 61 20 69 73 20 61 76 61 69 6c 61 62 6c  data is availabl
1c407 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  e, this routine 
1c408 77 69 6c 6c 20 65 69 74 68 65 72 0a 2a 2a 20 69  will either.** i
1c409 6e 76 6f 6b 65 20 74 68 65 20 72 65 73 75 6c 74  nvoke the result
1c40a 20 63 61 6c 6c 62 61 63 6b 20 28 69 66 20 74 68   callback (if th
1c40b 65 72 65 20 69 73 20 6f 6e 65 29 20 6f 72 20 72  ere is one) or r
1c40c 65 74 75 72 6e 20 77 69 74 68 0a 2a 2a 20 53 51  eturn with.** SQ
1c40d 4c 49 54 45 5f 52 4f 57 2e 0a 2a 2a 0a 2a 2a 20  LITE_ROW..**.** 
1c40e 49 66 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73  If an attempt is
1c40f 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20 61 20   made to open a 
1c410 6c 6f 63 6b 65 64 20 64 61 74 61 62 61 73 65 2c  locked database,
1c411 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   then this routi
1c412 6e 65 0a 2a 2a 20 77 69 6c 6c 20 65 69 74 68 65  ne.** will eithe
1c413 72 20 69 6e 76 6f 6b 65 20 74 68 65 20 62 75 73  r invoke the bus
1c414 79 20 63 61 6c 6c 62 61 63 6b 20 28 69 66 20 74  y callback (if t
1c415 68 65 72 65 20 69 73 20 6f 6e 65 29 20 6f 72 20  here is one) or 
1c416 69 74 20 77 69 6c 6c 0a 2a 2a 20 72 65 74 75 72  it will.** retur
1c417 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a 2a  n SQLITE_BUSY..*
1c418 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  *.** If an error
1c419 20 6f 63 63 75 72 73 2c 20 61 6e 20 65 72 72 6f   occurs, an erro
1c41a 72 20 6d 65 73 73 61 67 65 20 69 73 20 77 72 69  r message is wri
1c41b 74 74 65 6e 20 74 6f 20 6d 65 6d 6f 72 79 20 6f  tten to memory o
1c41c 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20  btained.** from 
1c41d 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
1c41e 20 61 6e 64 20 70 2d 3e 7a 45 72 72 4d 73 67 20   and p->zErrMsg 
1c41f 69 73 20 6d 61 64 65 20 74 6f 20 70 6f 69 6e 74  is made to point
1c420 20 74 6f 20 74 68 61 74 20 6d 65 6d 6f 72 79 2e   to that memory.
1c421 0a 2a 2a 20 54 68 65 20 65 72 72 6f 72 20 63 6f  .** The error co
1c422 64 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  de is stored in 
1c423 70 2d 3e 72 63 20 61 6e 64 20 74 68 69 73 20 72  p->rc and this r
1c424 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53  outine returns S
1c425 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a  QLITE_ERROR..**.
1c426 2a 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61  ** If the callba
1c427 63 6b 20 65 76 65 72 20 72 65 74 75 72 6e 73 20  ck ever returns 
1c428 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74  non-zero, then t
1c429 68 65 20 70 72 6f 67 72 61 6d 20 65 78 69 74 73  he program exits
1c42a 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e  .** immediately.
1c42b 20 20 54 68 65 72 65 20 77 69 6c 6c 20 62 65 20    There will be 
1c42c 6e 6f 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  no error message
1c42d 20 62 75 74 20 74 68 65 20 70 2d 3e 72 63 20 66   but the p->rc f
1c42e 69 65 6c 64 20 69 73 0a 2a 2a 20 73 65 74 20 74  ield is.** set t
1c42f 6f 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 61  o SQLITE_ABORT a
1c430 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  nd this routine 
1c431 77 69 6c 6c 20 72 65 74 75 72 6e 20 53 51 4c 49  will return SQLI
1c432 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20  TE_ERROR..**.** 
1c433 41 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  A memory allocat
1c434 69 6f 6e 20 65 72 72 6f 72 20 63 61 75 73 65 73  ion error causes
1c435 20 70 2d 3e 72 63 20 74 6f 20 62 65 20 73 65 74   p->rc to be set
1c436 20 74 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d   to SQLITE_NOMEM
1c437 20 61 6e 64 20 74 68 69 73 0a 2a 2a 20 72 6f 75   and this.** rou
1c438 74 69 6e 65 20 74 6f 20 72 65 74 75 72 6e 20 53  tine to return S
1c439 51 4c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a  QLITE_ERROR..**.
1c43a 2a 2a 20 4f 74 68 65 72 20 66 61 74 61 6c 20 65  ** Other fatal e
1c43b 72 72 6f 72 73 20 72 65 74 75 72 6e 20 53 51 4c  rrors return SQL
1c43c 49 54 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a  ITE_ERROR..**.**
1c43d 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74   After this rout
1c43e 69 6e 65 20 68 61 73 20 66 69 6e 69 73 68 65 64  ine has finished
1c43f 2c 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e  , sqlite3VdbeFin
1c440 61 6c 69 7a 65 28 29 20 73 68 6f 75 6c 64 20 62  alize() should b
1c441 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 63 6c 65  e.** used to cle
1c442 61 6e 20 75 70 20 74 68 65 20 6d 65 73 73 20 74  an up the mess t
1c443 68 61 74 20 77 61 73 20 6c 65 66 74 20 62 65 68  hat was left beh
1c444 69 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ind..*/.SQLITE_P
1c445 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1c446 65 33 56 64 62 65 45 78 65 63 28 0a 20 20 56 64  e3VdbeExec(.  Vd
1c447 62 65 20 2a 70 20 20 20 20 20 20 20 20 20 20 20  be *p           
1c448 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1c449 56 44 42 45 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  VDBE */.){.  int
1c44a 20 70 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   pc;            
1c44b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
1c44c 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a  rogram counter *
1c44d 2f 0a 20 20 4f 70 20 2a 70 4f 70 3b 20 20 20 20  /.  Op *pOp;    
1c44e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1c44f 2a 20 43 75 72 72 65 6e 74 20 6f 70 65 72 61 74  * Current operat
1c450 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20  ion */.  int rc 
1c451 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 20 20 20  = SQLITE_OK;    
1c452 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f 20      /* Value to 
1c453 72 65 74 75 72 6e 20 2a 2f 0a 20 20 73 71 6c 69  return */.  sqli
1c454 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b  te3 *db = p->db;
1c455 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
1c456 74 61 62 61 73 65 20 2a 2f 0a 20 20 75 38 20 65  tabase */.  u8 e
1c457 6e 63 6f 64 69 6e 67 20 3d 20 45 4e 43 28 64 62  ncoding = ENC(db
1c458 29 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61  );     /* The da
1c459 74 61 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20  tabase encoding 
1c45a 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 6e 31 20 3d  */.  Mem *pIn1 =
1c45b 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1c45c 2f 2a 20 31 73 74 20 69 6e 70 75 74 20 6f 70 65  /* 1st input ope
1c45d 72 61 6e 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  rand */.  Mem *p
1c45e 49 6e 32 20 3d 20 30 3b 20 20 20 20 20 20 20 20  In2 = 0;        
1c45f 20 20 20 20 20 2f 2a 20 32 6e 64 20 69 6e 70 75       /* 2nd inpu
1c460 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 4d  t operand */.  M
1c461 65 6d 20 2a 70 49 6e 33 20 3d 20 30 3b 20 20 20  em *pIn3 = 0;   
1c462 20 20 20 20 20 20 20 20 20 20 2f 2a 20 33 72 64            /* 3rd
1c463 20 69 6e 70 75 74 20 6f 70 65 72 61 6e 64 20 2a   input operand *
1c464 2f 0a 20 20 4d 65 6d 20 2a 70 4f 75 74 20 3d 20  /.  Mem *pOut = 
1c465 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0;             /
1c466 2a 20 4f 75 74 70 75 74 20 6f 70 65 72 61 6e 64  * Output operand
1c467 20 2a 2f 0a 20 20 75 38 20 6f 70 50 72 6f 70 65   */.  u8 opPrope
1c468 72 74 79 3b 0a 20 20 69 6e 74 20 69 43 6f 6d 70  rty;.  int iComp
1c469 61 72 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20  are = 0;        
1c46a 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 6c    /* Result of l
1c46b 61 73 74 20 4f 50 5f 43 6f 6d 70 61 72 65 20 6f  ast OP_Compare o
1c46c 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  peration */.  in
1c46d 74 20 2a 61 50 65 72 6d 75 74 65 20 3d 20 30 3b  t *aPermute = 0;
1c46e 20 20 20 20 20 20 20 20 20 2f 2a 20 50 65 72 6d           /* Perm
1c46f 75 74 61 74 69 6f 6e 20 6f 66 20 63 6f 6c 75 6d  utation of colum
1c470 6e 73 20 66 6f 72 20 4f 50 5f 43 6f 6d 70 61 72  ns for OP_Compar
1c471 65 20 2a 2f 0a 23 69 66 64 65 66 20 56 44 42 45  e */.#ifdef VDBE
1c472 5f 50 52 4f 46 49 4c 45 0a 20 20 75 36 34 20 73  _PROFILE.  u64 s
1c473 74 61 72 74 3b 20 20 20 20 20 20 20 20 20 20 20  tart;           
1c474 20 20 20 20 20 20 2f 2a 20 43 50 55 20 63 6c 6f        /* CPU clo
1c475 63 6b 20 63 6f 75 6e 74 20 61 74 20 73 74 61 72  ck count at star
1c476 74 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f 0a 20  t of opcode */. 
1c477 20 69 6e 74 20 6f 72 69 67 50 63 3b 20 20 20 20   int origPc;    
1c478 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
1c479 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 61  rogram counter a
1c47a 74 20 73 74 61 72 74 20 6f 66 20 6f 70 63 6f 64  t start of opcod
1c47b 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e  e */.#endif.#ifn
1c47c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1c47d 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43  PROGRESS_CALLBAC
1c47e 4b 0a 20 20 69 6e 74 20 6e 50 72 6f 67 72 65 73  K.  int nProgres
1c47f 73 4f 70 73 20 3d 20 30 3b 20 20 20 20 20 20 2f  sOps = 0;      /
1c480 2a 20 4f 70 63 6f 64 65 73 20 65 78 65 63 75 74  * Opcodes execut
1c481 65 64 20 73 69 6e 63 65 20 70 72 6f 67 72 65 73  ed since progres
1c482 73 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 23  s callback. */.#
1c483 65 6e 64 69 66 0a 20 20 2f 2a 2a 2a 2a 2a 2a 2a  endif.  /*******
1c484 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c485 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c486 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c487 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20  *************.  
1c488 2a 2a 20 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ** Automatically
1c489 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64 65 0a   generated code.
1c48a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 6f    **.  ** The fo
1c48b 6c 6c 6f 77 69 6e 67 20 75 6e 69 6f 6e 20 69 73  llowing union is
1c48c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67   automatically g
1c48d 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 0a  enerated by the.
1c48e 20 20 2a 2a 20 76 64 62 65 2d 63 6f 6d 70 72 65    ** vdbe-compre
1c48f 73 73 2e 74 63 6c 20 73 63 72 69 70 74 2e 20 20  ss.tcl script.  
1c490 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74  The purpose of t
1c491 68 69 73 20 75 6e 69 6f 6e 20 69 73 20 74 6f 0a  his union is to.
1c492 20 20 2a 2a 20 72 65 64 75 63 65 20 74 68 65 20    ** reduce the 
1c493 61 6d 6f 75 6e 74 20 6f 66 20 73 74 61 63 6b 20  amount of stack 
1c494 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20 62  space required b
1c495 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  y this function.
1c496 0a 20 20 2a 2a 20 53 65 65 20 63 6f 6d 6d 65 6e  .  ** See commen
1c497 74 73 20 69 6e 20 74 68 65 20 76 64 62 65 2d 63  ts in the vdbe-c
1c498 6f 6d 70 72 65 73 73 2e 74 63 6c 20 73 63 72 69  ompress.tcl scri
1c499 70 74 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a  pt for details..
1c49a 20 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 76 64 62    */.  union vdb
1c49b 65 45 78 65 63 55 6e 69 6f 6e 20 7b 0a 20 20 20  eExecUnion {.   
1c49c 20 73 74 72 75 63 74 20 4f 50 5f 59 69 65 6c 64   struct OP_Yield
1c49d 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c49e 20 20 20 20 69 6e 74 20 70 63 44 65 73 74 3b 0a      int pcDest;.
1c49f 20 20 20 20 7d 20 61 61 3b 0a 20 20 20 20 73 74      } aa;.    st
1c4a0 72 75 63 74 20 4f 50 5f 56 61 72 69 61 62 6c 65  ruct OP_Variable
1c4a1 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c4a2 20 20 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20      int p1;     
1c4a3 20 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c 65       /* Variable
1c4a4 20 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a 2f   to copy from */
1c4a5 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b 20 20  .      int p2;  
1c4a6 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
1c4a7 74 65 72 20 74 6f 20 63 6f 70 79 20 74 6f 20 2a  ter to copy to *
1c4a8 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 20 20  /.      int n;  
1c4a9 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1c4aa 65 72 20 6f 66 20 76 61 6c 75 65 73 20 6c 65 66  er of values lef
1c4ab 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 20  t to copy */.   
1c4ac 20 20 20 4d 65 6d 20 2a 70 56 61 72 3b 20 20 20     Mem *pVar;   
1c4ad 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 62 65 69      /* Value bei
1c4ae 6e 67 20 74 72 61 6e 73 66 65 72 72 65 64 20 2a  ng transferred *
1c4af 2f 0a 20 20 20 20 7d 20 61 62 3b 0a 20 20 20 20  /.    } ab;.    
1c4b0 73 74 72 75 63 74 20 4f 50 5f 4d 6f 76 65 5f 73  struct OP_Move_s
1c4b1 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c4b2 20 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b    char *zMalloc;
1c4b3 20 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 20 76 61     /* Holding va
1c4b4 72 69 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63  riable for alloc
1c4b5 61 74 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20  ated memory */. 
1c4b6 20 20 20 20 20 69 6e 74 20 6e 3b 20 20 20 20 20       int n;     
1c4b7 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1c4b8 6f 66 20 72 65 67 69 73 74 65 72 73 20 6c 65 66  of registers lef
1c4b9 74 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 20  t to copy */.   
1c4ba 20 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20     int p1;      
1c4bb 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
1c4bc 74 6f 20 63 6f 70 79 20 66 72 6f 6d 20 2a 2f 0a  to copy from */.
1c4bd 20 20 20 20 20 20 69 6e 74 20 70 32 3b 20 20 20        int p2;   
1c4be 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
1c4bf 65 72 20 74 6f 20 63 6f 70 79 20 74 6f 20 2a 2f  er to copy to */
1c4c0 0a 20 20 20 20 7d 20 61 63 3b 0a 20 20 20 20 73  .    } ac;.    s
1c4c1 74 72 75 63 74 20 4f 50 5f 52 65 73 75 6c 74 52  truct OP_ResultR
1c4c2 6f 77 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  ow_stack_vars {.
1c4c3 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b        Mem *pMem;
1c4c4 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
1c4c5 20 20 7d 20 61 64 3b 0a 20 20 20 20 73 74 72 75    } ad;.    stru
1c4c6 63 74 20 4f 50 5f 43 6f 6e 63 61 74 5f 73 74 61  ct OP_Concat_sta
1c4c7 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c4c8 69 36 34 20 6e 42 79 74 65 3b 0a 20 20 20 20 7d  i64 nByte;.    }
1c4c9 20 61 65 3b 0a 20 20 20 20 73 74 72 75 63 74 20   ae;.    struct 
1c4ca 4f 50 5f 52 65 6d 61 69 6e 64 65 72 5f 73 74 61  OP_Remainder_sta
1c4cb 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c4cc 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20  int flags;      
1c4cd 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 4d 45 4d 5f  /* Combined MEM_
1c4ce 2a 20 66 6c 61 67 73 20 66 72 6f 6d 20 62 6f 74  * flags from bot
1c4cf 68 20 69 6e 70 75 74 73 20 2a 2f 0a 20 20 20 20  h inputs */.    
1c4d0 20 20 69 36 34 20 69 41 3b 20 20 20 20 20 20 20    i64 iA;       
1c4d1 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61 6c    /* Integer val
1c4d2 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 65 72 61  ue of left opera
1c4d3 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20  nd */.      i64 
1c4d4 69 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 49  iB;         /* I
1c4d5 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6f 66 20  nteger value of 
1c4d6 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f  right operand */
1c4d7 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 41  .      double rA
1c4d8 3b 20 20 20 20 20 20 2f 2a 20 52 65 61 6c 20 76  ;      /* Real v
1c4d9 61 6c 75 65 20 6f 66 20 6c 65 66 74 20 6f 70 65  alue of left ope
1c4da 72 61 6e 64 20 2a 2f 0a 20 20 20 20 20 20 64 6f  rand */.      do
1c4db 75 62 6c 65 20 72 42 3b 20 20 20 20 20 20 2f 2a  uble rB;      /*
1c4dc 20 52 65 61 6c 20 76 61 6c 75 65 20 6f 66 20 72   Real value of r
1c4dd 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  ight operand */.
1c4de 20 20 20 20 7d 20 61 66 3b 0a 20 20 20 20 73 74      } af;.    st
1c4df 72 75 63 74 20 4f 50 5f 46 75 6e 63 74 69 6f 6e  ruct OP_Function
1c4e0 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c4e1 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20      int i;.     
1c4e2 20 4d 65 6d 20 2a 70 41 72 67 3b 0a 20 20 20 20   Mem *pArg;.    
1c4e3 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
1c4e4 74 20 63 74 78 3b 0a 20 20 20 20 20 20 73 71 6c  t ctx;.      sql
1c4e5 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56  ite3_value **apV
1c4e6 61 6c 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b  al;.      int n;
1c4e7 0a 20 20 20 20 7d 20 61 67 3b 0a 20 20 20 20 73  .    } ag;.    s
1c4e8 74 72 75 63 74 20 4f 50 5f 53 68 69 66 74 52 69  truct OP_ShiftRi
1c4e9 67 68 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  ght_stack_vars {
1c4ea 0a 20 20 20 20 20 20 69 36 34 20 61 3b 0a 20 20  .      i64 a;.  
1c4eb 20 20 20 20 69 36 34 20 62 3b 0a 20 20 20 20 7d      i64 b;.    }
1c4ec 20 61 68 3b 0a 20 20 20 20 73 74 72 75 63 74 20   ah;.    struct 
1c4ed 4f 50 5f 47 65 5f 73 74 61 63 6b 5f 76 61 72 73  OP_Ge_stack_vars
1c4ee 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73   {.      int res
1c4ef 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
1c4f0 52 65 73 75 6c 74 20 6f 66 20 74 68 65 20 63 6f  Result of the co
1c4f1 6d 70 61 72 69 73 6f 6e 20 6f 66 20 70 49 6e 31  mparison of pIn1
1c4f2 20 61 67 61 69 6e 73 74 20 70 49 6e 33 20 2a 2f   against pIn3 */
1c4f3 0a 20 20 20 20 20 20 63 68 61 72 20 61 66 66 69  .      char affi
1c4f4 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 41 66  nity;      /* Af
1c4f5 66 69 6e 69 74 79 20 74 6f 20 75 73 65 20 66 6f  finity to use fo
1c4f6 72 20 63 6f 6d 70 61 72 69 73 6f 6e 20 2a 2f 0a  r comparison */.
1c4f7 20 20 20 20 7d 20 61 69 3b 0a 20 20 20 20 73 74      } ai;.    st
1c4f8 72 75 63 74 20 4f 50 5f 43 6f 6d 70 61 72 65 5f  ruct OP_Compare_
1c4f9 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c4fa 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 20 20     int n;.      
1c4fb 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 6e 74  int i;.      int
1c4fc 20 70 31 3b 0a 20 20 20 20 20 20 69 6e 74 20 70   p1;.      int p
1c4fd 32 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 4b  2;.      const K
1c4fe 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f  eyInfo *pKeyInfo
1c4ff 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 64 78 3b  ;.      int idx;
1c500 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  .      CollSeq *
1c501 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43 6f 6c  pColl;    /* Col
1c502 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
1c503 74 6f 20 75 73 65 20 6f 6e 20 74 68 69 73 20 74  to use on this t
1c504 65 72 6d 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  erm */.      int
1c505 20 62 52 65 76 3b 20 20 20 20 20 20 20 20 20 20   bRev;          
1c506 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53 43  /* True for DESC
1c507 45 4e 44 49 4e 47 20 73 6f 72 74 20 6f 72 64 65  ENDING sort orde
1c508 72 20 2a 2f 0a 20 20 20 20 7d 20 61 6a 3b 0a 20  r */.    } aj;. 
1c509 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4f 72 5f     struct OP_Or_
1c50a 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c50b 20 20 20 69 6e 74 20 76 31 3b 20 20 20 20 2f 2a     int v1;    /*
1c50c 20 4c 65 66 74 20 6f 70 65 72 61 6e 64 3a 20 20   Left operand:  
1c50d 30 3d 3d 46 41 4c 53 45 2c 20 31 3d 3d 54 52 55  0==FALSE, 1==TRU
1c50e 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72  E, 2==UNKNOWN or
1c50f 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 69   NULL */.      i
1c510 6e 74 20 76 32 3b 20 20 20 20 2f 2a 20 52 69 67  nt v2;    /* Rig
1c511 68 74 20 6f 70 65 72 61 6e 64 3a 20 30 3d 3d 46  ht operand: 0==F
1c512 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32  ALSE, 1==TRUE, 2
1c513 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c  ==UNKNOWN or NUL
1c514 4c 20 2a 2f 0a 20 20 20 20 7d 20 61 6b 3b 0a 20  L */.    } ak;. 
1c515 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 66 4e     struct OP_IfN
1c516 6f 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  ot_stack_vars {.
1c517 20 20 20 20 20 20 69 6e 74 20 63 3b 0a 20 20 20        int c;.   
1c518 20 7d 20 61 6c 3b 0a 20 20 20 20 73 74 72 75 63   } al;.    struc
1c519 74 20 4f 50 5f 43 6f 6c 75 6d 6e 5f 73 74 61 63  t OP_Column_stac
1c51a 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 75  k_vars {.      u
1c51b 33 32 20 70 61 79 6c 6f 61 64 53 69 7a 65 3b 20  32 payloadSize; 
1c51c 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
1c51d 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f  ytes in the reco
1c51e 72 64 20 2a 2f 0a 20 20 20 20 20 20 69 36 34 20  rd */.      i64 
1c51f 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 3b 20 2f  payloadSize64; /
1c520 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
1c521 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20  s in the record 
1c522 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 70 31 3b  */.      int p1;
1c523 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
1c524 31 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f  1 value of the o
1c525 70 63 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 69  pcode */.      i
1c526 6e 74 20 70 32 3b 20 20 20 20 20 20 20 20 20 20  nt p2;          
1c527 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62    /* column numb
1c528 65 72 20 74 6f 20 72 65 74 72 69 65 76 65 20 2a  er to retrieve *
1c529 2f 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73  /.      VdbeCurs
1c52a 6f 72 20 2a 70 43 3b 20 20 20 20 2f 2a 20 54 68  or *pC;    /* Th
1c52b 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 2a 2f  e VDBE cursor */
1c52c 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 52 65  .      char *zRe
1c52d 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69  c;        /* Poi
1c52e 6e 74 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65  nter to complete
1c52f 20 72 65 63 6f 72 64 2d 64 61 74 61 20 2a 2f 0a   record-data */.
1c530 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a        BtCursor *
1c531 70 43 72 73 72 3b 20 20 20 2f 2a 20 54 68 65 20  pCrsr;   /* The 
1c532 42 54 72 65 65 20 63 75 72 73 6f 72 20 2a 2f 0a  BTree cursor */.
1c533 20 20 20 20 20 20 75 33 32 20 2a 61 54 79 70 65        u32 *aType
1c534 3b 20 20 20 20 20 20 20 20 2f 2a 20 61 54 79 70  ;        /* aTyp
1c535 65 5b 69 5d 20 68 6f 6c 64 73 20 74 68 65 20 6e  e[i] holds the n
1c536 75 6d 65 72 69 63 20 74 79 70 65 20 6f 66 20 74  umeric type of t
1c537 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a  he i-th column *
1c538 2f 0a 20 20 20 20 20 20 75 33 32 20 2a 61 4f 66  /.      u32 *aOf
1c539 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 61 4f  fset;      /* aO
1c53a 66 66 73 65 74 5b 69 5d 20 69 73 20 6f 66 66 73  ffset[i] is offs
1c53b 65 74 20 74 6f 20 73 74 61 72 74 20 6f 66 20 64  et to start of d
1c53c 61 74 61 20 66 6f 72 20 69 2d 74 68 20 63 6f 6c  ata for i-th col
1c53d 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  umn */.      int
1c53e 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20   nField;        
1c53f 2f 2a 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65  /* number of fie
1c540 6c 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72  lds in the recor
1c541 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6c  d */.      int l
1c542 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  en;           /*
1c543 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74   The length of t
1c544 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61  he serialized da
1c545 74 61 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d  ta for the colum
1c546 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69  n */.      int i
1c547 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
1c548 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
1c549 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 44 61  .      char *zDa
1c54a 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 50 61 72  ta;       /* Par
1c54b 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20  t of the record 
1c54c 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f  being decoded */
1c54d 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 44 65 73  .      Mem *pDes
1c54e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65  t;        /* Whe
1c54f 72 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 20  re to write the 
1c550 65 78 74 72 61 63 74 65 64 20 76 61 6c 75 65 20  extracted value 
1c551 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 73 4d 65  */.      Mem sMe
1c552 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  m;          /* F
1c553 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 20 72  or storing the r
1c554 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f  ecord being deco
1c555 64 65 64 20 2a 2f 0a 20 20 20 20 20 20 75 38 20  ded */.      u8 
1c556 2a 7a 49 64 78 3b 20 20 20 20 20 20 20 20 20 20  *zIdx;          
1c557 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20 68 65  /* Index into he
1c558 61 64 65 72 20 2a 2f 0a 20 20 20 20 20 20 75 38  ader */.      u8
1c559 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20 20 20 20   *zEndHdr;      
1c55a 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66   /* Pointer to f
1c55b 69 72 73 74 20 62 79 74 65 20 61 66 74 65 72 20  irst byte after 
1c55c 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  the header */.  
1c55d 20 20 20 20 75 33 32 20 6f 66 66 73 65 74 3b 20      u32 offset; 
1c55e 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
1c55f 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 20 2a   into the data *
1c560 2f 0a 20 20 20 20 20 20 75 36 34 20 6f 66 66 73  /.      u64 offs
1c561 65 74 36 34 3b 20 20 20 20 20 20 2f 2a 20 36 34  et64;      /* 64
1c562 2d 62 69 74 20 6f 66 66 73 65 74 2e 20 20 36 34  -bit offset.  64
1c563 20 62 69 74 73 20 6e 65 65 64 65 64 20 74 6f 20   bits needed to 
1c564 63 61 74 63 68 20 6f 76 65 72 66 6c 6f 77 20 2a  catch overflow *
1c565 2f 0a 20 20 20 20 20 20 69 6e 74 20 73 7a 48 64  /.      int szHd
1c566 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69  r;         /* Si
1c567 7a 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 72  ze of the header
1c568 20 73 69 7a 65 20 66 69 65 6c 64 20 61 74 20 73   size field at s
1c569 74 61 72 74 20 6f 66 20 72 65 63 6f 72 64 20 2a  tart of record *
1c56a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 76 61 69  /.      int avai
1c56b 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  l;         /* Nu
1c56c 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66  mber of bytes of
1c56d 20 61 76 61 69 6c 61 62 6c 65 20 64 61 74 61 20   available data 
1c56e 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52  */.      Mem *pR
1c56f 65 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50  eg;         /* P
1c570 73 65 75 64 6f 54 61 62 6c 65 20 69 6e 70 75 74  seudoTable input
1c571 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20   register */.   
1c572 20 7d 20 61 6d 3b 0a 20 20 20 20 73 74 72 75 63   } am;.    struc
1c573 74 20 4f 50 5f 41 66 66 69 6e 69 74 79 5f 73 74  t OP_Affinity_st
1c574 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c575 20 63 68 61 72 20 2a 7a 41 66 66 69 6e 69 74 79   char *zAffinity
1c576 3b 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e  ;   /* The affin
1c577 69 74 79 20 74 6f 20 62 65 20 61 70 70 6c 69 65  ity to be applie
1c578 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20 2a  d */.      Mem *
1c579 70 44 61 74 61 30 3b 20 20 20 20 20 20 20 2f 2a  pData0;       /*
1c57a 20 46 69 72 73 74 20 72 65 67 69 73 74 65 72 20   First register 
1c57b 74 6f 20 77 68 69 63 68 20 74 6f 20 61 70 70 6c  to which to appl
1c57c 79 20 61 66 66 69 6e 69 74 79 20 2a 2f 0a 20 20  y affinity */.  
1c57d 20 20 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b 20      Mem *pLast; 
1c57e 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 72         /* Last r
1c57f 65 67 69 73 74 65 72 20 74 6f 20 77 68 69 63 68  egister to which
1c580 20 74 6f 20 61 70 70 6c 79 20 61 66 66 69 6e 69   to apply affini
1c581 74 79 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20  ty */.      Mem 
1c582 2a 70 52 65 63 3b 20 20 20 20 20 20 20 20 20 2f  *pRec;         /
1c583 2a 20 43 75 72 72 65 6e 74 20 72 65 67 69 73 74  * Current regist
1c584 65 72 20 2a 2f 0a 20 20 20 20 7d 20 61 6e 3b 0a  er */.    } an;.
1c585 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4d 61      struct OP_Ma
1c586 6b 65 52 65 63 6f 72 64 5f 73 74 61 63 6b 5f 76  keRecord_stack_v
1c587 61 72 73 20 7b 0a 20 20 20 20 20 20 75 38 20 2a  ars {.      u8 *
1c588 7a 4e 65 77 52 65 63 6f 72 64 3b 20 20 20 20 20  zNewRecord;     
1c589 20 20 20 2f 2a 20 41 20 62 75 66 66 65 72 20 74     /* A buffer t
1c58a 6f 20 68 6f 6c 64 20 74 68 65 20 64 61 74 61 20  o hold the data 
1c58b 66 6f 72 20 74 68 65 20 6e 65 77 20 72 65 63 6f  for the new reco
1c58c 72 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20  rd */.      Mem 
1c58d 2a 70 52 65 63 3b 20 20 20 20 20 20 20 20 20 20  *pRec;          
1c58e 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 65     /* The new re
1c58f 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 75 36  cord */.      u6
1c590 34 20 6e 44 61 74 61 3b 20 20 20 20 20 20 20 20  4 nData;        
1c591 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
1c592 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20  f bytes of data 
1c593 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 69  space */.      i
1c594 6e 74 20 6e 48 64 72 3b 20 20 20 20 20 20 20 20  nt nHdr;        
1c595 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1c596 6f 66 20 62 79 74 65 73 20 6f 66 20 68 65 61 64  of bytes of head
1c597 65 72 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20  er space */.    
1c598 20 20 69 36 34 20 6e 42 79 74 65 3b 20 20 20 20    i64 nByte;    
1c599 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
1c59a 20 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20   space required 
1c59b 66 6f 72 20 74 68 69 73 20 72 65 63 6f 72 64 20  for this record 
1c59c 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 5a 65  */.      int nZe
1c59d 72 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ro;             
1c59e 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65 72  /* Number of zer
1c59f 6f 20 62 79 74 65 73 20 61 74 20 74 68 65 20 65  o bytes at the e
1c5a0 6e 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64  nd of the record
1c5a1 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 56   */.      int nV
1c5a2 61 72 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20  arint;          
1c5a3 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1c5a4 74 65 73 20 69 6e 20 61 20 76 61 72 69 6e 74 20  tes in a varint 
1c5a5 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 73 65 72  */.      u32 ser
1c5a6 69 61 6c 5f 74 79 70 65 3b 20 20 20 20 20 20 20  ial_type;       
1c5a7 2f 2a 20 54 79 70 65 20 66 69 65 6c 64 20 2a 2f  /* Type field */
1c5a8 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 44 61 74  .      Mem *pDat
1c5a9 61 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  a0;           /*
1c5aa 20 46 69 72 73 74 20 66 69 65 6c 64 20 74 6f 20   First field to 
1c5ab 62 65 20 63 6f 6d 62 69 6e 65 64 20 69 6e 74 6f  be combined into
1c5ac 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20   the record */. 
1c5ad 20 20 20 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b       Mem *pLast;
1c5ae 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1c5af 61 73 74 20 66 69 65 6c 64 20 6f 66 20 74 68 65  ast field of the
1c5b0 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20   record */.     
1c5b1 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20   int nField;    
1c5b2 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1c5b3 72 20 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74  r of fields in t
1c5b4 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20  he record */.   
1c5b5 20 20 20 63 68 61 72 20 2a 7a 41 66 66 69 6e 69     char *zAffini
1c5b6 74 79 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ty;       /* The
1c5b7 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67   affinity string
1c5b8 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64 20   for the record 
1c5b9 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 66 69 6c  */.      int fil
1c5ba 65 5f 66 6f 72 6d 61 74 3b 20 20 20 20 20 20 20  e_format;       
1c5bb 2f 2a 20 46 69 6c 65 20 66 6f 72 6d 61 74 20 74  /* File format t
1c5bc 6f 20 75 73 65 20 66 6f 72 20 65 6e 63 6f 64 69  o use for encodi
1c5bd 6e 67 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  ng */.      int 
1c5be 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
1c5bf 20 20 20 2f 2a 20 53 70 61 63 65 20 75 73 65 64     /* Space used
1c5c0 20 69 6e 20 7a 4e 65 77 52 65 63 6f 72 64 5b 5d   in zNewRecord[]
1c5c1 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6c 65   */.      int le
1c5c2 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
1c5c3 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 61 20   /* Length of a 
1c5c4 66 69 65 6c 64 20 2a 2f 0a 20 20 20 20 7d 20 61  field */.    } a
1c5c5 6f 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  o;.    struct OP
1c5c6 5f 43 6f 75 6e 74 5f 73 74 61 63 6b 5f 76 61 72  _Count_stack_var
1c5c7 73 20 7b 0a 20 20 20 20 20 20 69 36 34 20 6e 45  s {.      i64 nE
1c5c8 6e 74 72 79 3b 0a 20 20 20 20 20 20 42 74 43 75  ntry;.      BtCu
1c5c9 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20  rsor *pCrsr;.   
1c5ca 20 7d 20 61 70 3b 0a 20 20 20 20 73 74 72 75 63   } ap;.    struc
1c5cb 74 20 4f 50 5f 53 61 76 65 70 6f 69 6e 74 5f 73  t OP_Savepoint_s
1c5cc 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c5cd 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20    int p1;       
1c5ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c5cf 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20 50 31    /* Value of P1
1c5d0 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20   operand */.    
1c5d1 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
1c5d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c5d3 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 73 61 76    /* Name of sav
1c5d4 65 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20  epoint */.      
1c5d5 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 20 20 20  int nName;.     
1c5d6 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 4e 65 77   Savepoint *pNew
1c5d7 3b 0a 20 20 20 20 20 20 53 61 76 65 70 6f 69 6e  ;.      Savepoin
1c5d8 74 20 2a 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20  t *pSavepoint;. 
1c5d9 20 20 20 20 20 53 61 76 65 70 6f 69 6e 74 20 2a       Savepoint *
1c5da 70 54 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74 20  pTmp;.      int 
1c5db 69 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20  iSavepoint;.    
1c5dc 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 7d 20    int ii;.    } 
1c5dd 61 71 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  aq;.    struct O
1c5de 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 5f 73 74 61  P_AutoCommit_sta
1c5df 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c5e0 69 6e 74 20 64 65 73 69 72 65 64 41 75 74 6f 43  int desiredAutoC
1c5e1 6f 6d 6d 69 74 3b 0a 20 20 20 20 20 20 69 6e 74  ommit;.      int
1c5e2 20 69 52 6f 6c 6c 62 61 63 6b 3b 0a 20 20 20 20   iRollback;.    
1c5e3 20 20 69 6e 74 20 74 75 72 6e 4f 6e 41 43 3b 0a    int turnOnAC;.
1c5e4 20 20 20 20 7d 20 61 72 3b 0a 20 20 20 20 73 74      } ar;.    st
1c5e5 72 75 63 74 20 4f 50 5f 54 72 61 6e 73 61 63 74  ruct OP_Transact
1c5e6 69 6f 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  ion_stack_vars {
1c5e7 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70 42  .      Btree *pB
1c5e8 74 3b 0a 20 20 20 20 7d 20 61 73 3b 0a 20 20 20  t;.    } as;.   
1c5e9 20 73 74 72 75 63 74 20 4f 50 5f 52 65 61 64 43   struct OP_ReadC
1c5ea 6f 6f 6b 69 65 5f 73 74 61 63 6b 5f 76 61 72 73  ookie_stack_vars
1c5eb 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 4d 65   {.      int iMe
1c5ec 74 61 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 44  ta;.      int iD
1c5ed 62 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f  b;.      int iCo
1c5ee 6f 6b 69 65 3b 0a 20 20 20 20 7d 20 61 74 3b 0a  okie;.    } at;.
1c5ef 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 53 65      struct OP_Se
1c5f0 74 43 6f 6f 6b 69 65 5f 73 74 61 63 6b 5f 76 61  tCookie_stack_va
1c5f1 72 73 20 7b 0a 20 20 20 20 20 20 44 62 20 2a 70  rs {.      Db *p
1c5f2 44 62 3b 0a 20 20 20 20 7d 20 61 75 3b 0a 20 20  Db;.    } au;.  
1c5f3 20 20 73 74 72 75 63 74 20 4f 50 5f 56 65 72 69    struct OP_Veri
1c5f4 66 79 43 6f 6f 6b 69 65 5f 73 74 61 63 6b 5f 76  fyCookie_stack_v
1c5f5 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  ars {.      int 
1c5f6 69 4d 65 74 61 3b 0a 20 20 20 20 20 20 42 74 72  iMeta;.      Btr
1c5f7 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 7d 20 61  ee *pBt;.    } a
1c5f8 76 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  v;.    struct OP
1c5f9 5f 4f 70 65 6e 57 72 69 74 65 5f 73 74 61 63 6b  _OpenWrite_stack
1c5fa 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e  _vars {.      in
1c5fb 74 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20 20  t nField;.      
1c5fc 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66  KeyInfo *pKeyInf
1c5fd 6f 3b 0a 20 20 20 20 20 20 69 6e 74 20 70 32 3b  o;.      int p2;
1c5fe 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a  .      int iDb;.
1c5ff 20 20 20 20 20 20 69 6e 74 20 77 72 46 6c 61 67        int wrFlag
1c600 3b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a 70  ;.      Btree *p
1c601 58 3b 0a 20 20 20 20 20 20 56 64 62 65 43 75 72  X;.      VdbeCur
1c602 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 20 20 20  sor *pCur;.     
1c603 20 44 62 20 2a 70 44 62 3b 0a 20 20 20 20 7d 20   Db *pDb;.    } 
1c604 61 77 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  aw;.    struct O
1c605 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 5f  P_OpenEphemeral_
1c606 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c607 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70     VdbeCursor *p
1c608 43 78 3b 0a 20 20 20 20 7d 20 61 78 3b 0a 20 20  Cx;.    } ax;.  
1c609 20 20 73 74 72 75 63 74 20 4f 50 5f 4f 70 65 6e    struct OP_Open
1c60a 50 73 65 75 64 6f 5f 73 74 61 63 6b 5f 76 61 72  Pseudo_stack_var
1c60b 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75  s {.      VdbeCu
1c60c 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 20 20 7d  rsor *pCx;.    }
1c60d 20 61 79 3b 0a 20 20 20 20 73 74 72 75 63 74 20   ay;.    struct 
1c60e 4f 50 5f 53 65 65 6b 47 74 5f 73 74 61 63 6b 5f  OP_SeekGt_stack_
1c60f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74  vars {.      int
1c610 20 72 65 73 3b 0a 20 20 20 20 20 20 69 6e 74 20   res;.      int 
1c611 6f 63 3b 0a 20 20 20 20 20 20 56 64 62 65 43 75  oc;.      VdbeCu
1c612 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20  rsor *pC;.      
1c613 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72  UnpackedRecord r
1c614 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 46 69 65  ;.      int nFie
1c615 6c 64 3b 0a 20 20 20 20 20 20 69 36 34 20 69 4b  ld;.      i64 iK
1c616 65 79 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ey;      /* The 
1c617 72 6f 77 69 64 20 77 65 20 61 72 65 20 74 6f 20  rowid we are to 
1c618 73 65 65 6b 20 74 6f 20 2a 2f 0a 20 20 20 20 7d  seek to */.    }
1c619 20 61 7a 3b 0a 20 20 20 20 73 74 72 75 63 74 20   az;.    struct 
1c61a 4f 50 5f 53 65 65 6b 5f 73 74 61 63 6b 5f 76 61  OP_Seek_stack_va
1c61b 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43  rs {.      VdbeC
1c61c 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 7d  ursor *pC;.    }
1c61d 20 62 61 3b 0a 20 20 20 20 73 74 72 75 63 74 20   ba;.    struct 
1c61e 4f 50 5f 46 6f 75 6e 64 5f 73 74 61 63 6b 5f 76  OP_Found_stack_v
1c61f 61 72 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  ars {.      int 
1c620 61 6c 72 65 61 64 79 45 78 69 73 74 73 3b 0a 20  alreadyExists;. 
1c621 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20       VdbeCursor 
1c622 2a 70 43 3b 0a 20 20 20 20 20 20 69 6e 74 20 72  *pC;.      int r
1c623 65 73 3b 0a 20 20 20 20 20 20 55 6e 70 61 63 6b  es;.      Unpack
1c624 65 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65  edRecord *pIdxKe
1c625 79 3b 0a 20 20 20 20 20 20 63 68 61 72 20 61 54  y;.      char aT
1c626 65 6d 70 52 65 63 5b 52 4f 55 4e 44 38 28 73 69  empRec[ROUND8(si
1c627 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63  zeof(UnpackedRec
1c628 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f 66 28 4d  ord)) + sizeof(M
1c629 65 6d 29 2a 33 20 2b 20 37 5d 3b 0a 20 20 20 20  em)*3 + 7];.    
1c62a 7d 20 62 62 3b 0a 20 20 20 20 73 74 72 75 63 74  } bb;.    struct
1c62b 20 4f 50 5f 49 73 55 6e 69 71 75 65 5f 73 74 61   OP_IsUnique_sta
1c62c 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c62d 75 31 36 20 69 69 3b 0a 20 20 20 20 20 20 56 64  u16 ii;.      Vd
1c62e 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20  beCursor *pCx;. 
1c62f 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70       BtCursor *p
1c630 43 72 73 72 3b 0a 20 20 20 20 20 20 75 31 36 20  Crsr;.      u16 
1c631 6e 46 69 65 6c 64 3b 0a 20 20 20 20 20 20 4d 65  nField;.      Me
1c632 6d 20 2a 61 4d 65 6d 3b 0a 20 20 20 20 20 20 55  m *aMem;.      U
1c633 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 72 3b  npackedRecord r;
1c634 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c635 20 20 2f 2a 20 42 2d 54 72 65 65 20 69 6e 64 65    /* B-Tree inde
1c636 78 20 73 65 61 72 63 68 20 6b 65 79 20 2a 2f 0a  x search key */.
1c637 20 20 20 20 20 20 69 36 34 20 52 3b 20 20 20 20        i64 R;    
1c638 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c639 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69           /* Rowi
1c63a 64 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69  d stored in regi
1c63b 73 74 65 72 20 50 33 20 2a 2f 0a 20 20 20 20 7d  ster P3 */.    }
1c63c 20 62 63 3b 0a 20 20 20 20 73 74 72 75 63 74 20   bc;.    struct 
1c63d 4f 50 5f 4e 6f 74 45 78 69 73 74 73 5f 73 74 61  OP_NotExists_sta
1c63e 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c63f 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a  VdbeCursor *pC;.
1c640 20 20 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a        BtCursor *
1c641 70 43 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74  pCrsr;.      int
1c642 20 72 65 73 3b 0a 20 20 20 20 20 20 75 36 34 20   res;.      u64 
1c643 69 4b 65 79 3b 0a 20 20 20 20 7d 20 62 64 3b 0a  iKey;.    } bd;.
1c644 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 4e 65      struct OP_Ne
1c645 77 52 6f 77 69 64 5f 73 74 61 63 6b 5f 76 61 72  wRowid_stack_var
1c646 73 20 7b 0a 20 20 20 20 20 20 69 36 34 20 76 3b  s {.      i64 v;
1c647 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c648 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 77 69   /* The new rowi
1c649 64 20 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 43  d */.      VdbeC
1c64a 75 72 73 6f 72 20 2a 70 43 3b 20 20 20 20 20 20  ursor *pC;      
1c64b 20 20 2f 2a 20 43 75 72 73 6f 72 20 6f 66 20 74    /* Cursor of t
1c64c 61 62 6c 65 20 74 6f 20 67 65 74 20 74 68 65 20  able to get the 
1c64d 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 20  new rowid */.   
1c64e 20 20 20 69 6e 74 20 72 65 73 3b 20 20 20 20 20     int res;     
1c64f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
1c650 75 6c 74 20 6f 66 20 61 6e 20 73 71 6c 69 74 65  ult of an sqlite
1c651 33 42 74 72 65 65 4c 61 73 74 28 29 20 2a 2f 0a  3BtreeLast() */.
1c652 20 20 20 20 20 20 69 6e 74 20 63 6e 74 3b 20 20        int cnt;  
1c653 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c654 43 6f 75 6e 74 65 72 20 74 6f 20 6c 69 6d 69 74  Counter to limit
1c655 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73   the number of s
1c656 65 61 72 63 68 65 73 20 2a 2f 0a 20 20 20 20 20  earches */.     
1c657 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20   Mem *pMem;     
1c658 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73          /* Regis
1c659 74 65 72 20 68 6f 6c 64 69 6e 67 20 6c 61 72 67  ter holding larg
1c65a 65 73 74 20 72 6f 77 69 64 20 66 6f 72 20 41 55  est rowid for AU
1c65b 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a 20  TOINCREMENT */. 
1c65c 20 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a       VdbeFrame *
1c65d 70 46 72 61 6d 65 3b 20 20 20 20 20 2f 2a 20 52  pFrame;     /* R
1c65e 6f 6f 74 20 66 72 61 6d 65 20 6f 66 20 56 44 42  oot frame of VDB
1c65f 45 20 2a 2f 0a 20 20 20 20 7d 20 62 65 3b 0a 20  E */.    } be;. 
1c660 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 6e 73     struct OP_Ins
1c661 65 72 74 49 6e 74 5f 73 74 61 63 6b 5f 76 61 72  ertInt_stack_var
1c662 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70  s {.      Mem *p
1c663 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 4d  Data;       /* M
1c664 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e 67 20  EM cell holding 
1c665 64 61 74 61 20 66 6f 72 20 74 68 65 20 72 65 63  data for the rec
1c666 6f 72 64 20 74 6f 20 62 65 20 69 6e 73 65 72 74  ord to be insert
1c667 65 64 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d 20  ed */.      Mem 
1c668 2a 70 4b 65 79 3b 20 20 20 20 20 20 20 20 2f 2a  *pKey;        /*
1c669 20 4d 45 4d 20 63 65 6c 6c 20 68 6f 6c 64 69 6e   MEM cell holdin
1c66a 67 20 6b 65 79 20 20 66 6f 72 20 74 68 65 20 72  g key  for the r
1c66b 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20 69  ecord */.      i
1c66c 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 20 20  64 iKey;        
1c66d 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20   /* The integer 
1c66e 52 4f 57 49 44 20 6f 72 20 6b 65 79 20 66 6f 72  ROWID or key for
1c66f 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 62   the record to b
1c670 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20  e inserted */.  
1c671 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a      VdbeCursor *
1c672 70 43 3b 20 20 20 2f 2a 20 43 75 72 73 6f 72 20  pC;   /* Cursor 
1c673 74 6f 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68  to table into wh
1c674 69 63 68 20 69 6e 73 65 72 74 20 69 73 20 77 72  ich insert is wr
1c675 69 74 74 65 6e 20 2a 2f 0a 20 20 20 20 20 20 69  itten */.      i
1c676 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20 20 20 20  nt nZero;       
1c677 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a 65   /* Number of ze
1c678 72 6f 2d 62 79 74 65 73 20 74 6f 20 61 70 70 65  ro-bytes to appe
1c679 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  nd */.      int 
1c67a 73 65 65 6b 52 65 73 75 6c 74 3b 20 20 20 2f 2a  seekResult;   /*
1c67b 20 52 65 73 75 6c 74 20 6f 66 20 70 72 69 6f 72   Result of prior
1c67c 20 73 65 65 6b 20 6f 72 20 30 20 69 66 20 6e 6f   seek or 0 if no
1c67d 20 55 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66   USESEEKRESULT f
1c67e 6c 61 67 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e  lag */.      con
1c67f 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 2f  st char *zDb;  /
1c680 2a 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  * database name 
1c681 2d 20 75 73 65 64 20 62 79 20 74 68 65 20 75 70  - used by the up
1c682 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 20 20  date hook */.   
1c683 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1c684 54 62 6c 3b 20 2f 2a 20 54 61 62 6c 65 20 6e 61  Tbl; /* Table na
1c685 6d 65 20 2d 20 75 73 65 64 20 62 79 20 74 68 65  me - used by the
1c686 20 6f 70 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a   opdate hook */.
1c687 20 20 20 20 20 20 69 6e 74 20 6f 70 3b 20 20 20        int op;   
1c688 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f 64          /* Opcod
1c689 65 20 66 6f 72 20 75 70 64 61 74 65 20 68 6f 6f  e for update hoo
1c68a 6b 3a 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45  k: SQLITE_UPDATE
1c68b 20 6f 72 20 53 51 4c 49 54 45 5f 49 4e 53 45 52   or SQLITE_INSER
1c68c 54 20 2a 2f 0a 20 20 20 20 7d 20 62 66 3b 0a 20  T */.    } bf;. 
1c68d 20 20 20 73 74 72 75 63 74 20 4f 50 5f 44 65 6c     struct OP_Del
1c68e 65 74 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  ete_stack_vars {
1c68f 0a 20 20 20 20 20 20 69 36 34 20 69 4b 65 79 3b  .      i64 iKey;
1c690 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f  .      VdbeCurso
1c691 72 20 2a 70 43 3b 0a 20 20 20 20 7d 20 62 67 3b  r *pC;.    } bg;
1c692 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 52  .    struct OP_R
1c693 6f 77 44 61 74 61 5f 73 74 61 63 6b 5f 76 61 72  owData_stack_var
1c694 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43 75  s {.      VdbeCu
1c695 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20 20  rsor *pC;.      
1c696 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b  BtCursor *pCrsr;
1c697 0a 20 20 20 20 20 20 75 33 32 20 6e 3b 0a 20 20  .      u32 n;.  
1c698 20 20 20 20 69 36 34 20 6e 36 34 3b 0a 20 20 20      i64 n64;.   
1c699 20 7d 20 62 68 3b 0a 20 20 20 20 73 74 72 75 63   } bh;.    struc
1c69a 74 20 4f 50 5f 52 6f 77 69 64 5f 73 74 61 63 6b  t OP_Rowid_stack
1c69b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64  _vars {.      Vd
1c69c 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20  beCursor *pC;.  
1c69d 20 20 20 20 69 36 34 20 76 3b 0a 20 20 20 20 20      i64 v;.     
1c69e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
1c69f 56 74 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e 73  Vtab;.      cons
1c6a0 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
1c6a1 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d   *pModule;.    }
1c6a2 20 62 69 3b 0a 20 20 20 20 73 74 72 75 63 74 20   bi;.    struct 
1c6a3 4f 50 5f 4e 75 6c 6c 52 6f 77 5f 73 74 61 63 6b  OP_NullRow_stack
1c6a4 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64  _vars {.      Vd
1c6a5 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20  beCursor *pC;.  
1c6a6 20 20 7d 20 62 6a 3b 0a 20 20 20 20 73 74 72 75    } bj;.    stru
1c6a7 63 74 20 4f 50 5f 4c 61 73 74 5f 73 74 61 63 6b  ct OP_Last_stack
1c6a8 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64  _vars {.      Vd
1c6a9 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20  beCursor *pC;.  
1c6aa 20 20 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43      BtCursor *pC
1c6ab 72 73 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72  rsr;.      int r
1c6ac 65 73 3b 0a 20 20 20 20 7d 20 62 6b 3b 0a 20 20  es;.    } bk;.  
1c6ad 20 20 73 74 72 75 63 74 20 4f 50 5f 52 65 77 69    struct OP_Rewi
1c6ae 6e 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  nd_stack_vars {.
1c6af 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72        VdbeCursor
1c6b0 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43 75   *pC;.      BtCu
1c6b1 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 20  rsor *pCrsr;.   
1c6b2 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20     int res;.    
1c6b3 7d 20 62 6c 3b 0a 20 20 20 20 73 74 72 75 63 74  } bl;.    struct
1c6b4 20 4f 50 5f 4e 65 78 74 5f 73 74 61 63 6b 5f 76   OP_Next_stack_v
1c6b5 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65  ars {.      Vdbe
1c6b6 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20  Cursor *pC;.    
1c6b7 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
1c6b8 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73  r;.      int res
1c6b9 3b 0a 20 20 20 20 7d 20 62 6d 3b 0a 20 20 20 20  ;.    } bm;.    
1c6ba 73 74 72 75 63 74 20 4f 50 5f 49 64 78 49 6e 73  struct OP_IdxIns
1c6bb 65 72 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b  ert_stack_vars {
1c6bc 0a 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f  .      VdbeCurso
1c6bd 72 20 2a 70 43 3b 0a 20 20 20 20 20 20 42 74 43  r *pC;.      BtC
1c6be 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20  ursor *pCrsr;.  
1c6bf 20 20 20 20 69 6e 74 20 6e 4b 65 79 3b 0a 20 20      int nKey;.  
1c6c0 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
1c6c1 7a 4b 65 79 3b 0a 20 20 20 20 7d 20 62 6e 3b 0a  zKey;.    } bn;.
1c6c2 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 64      struct OP_Id
1c6c3 78 44 65 6c 65 74 65 5f 73 74 61 63 6b 5f 76 61  xDelete_stack_va
1c6c4 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43  rs {.      VdbeC
1c6c5 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 20 20 20  ursor *pC;.     
1c6c6 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72   BtCursor *pCrsr
1c6c7 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b  ;.      int res;
1c6c8 0a 20 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52  .      UnpackedR
1c6c9 65 63 6f 72 64 20 72 3b 0a 20 20 20 20 7d 20 62  ecord r;.    } b
1c6ca 6f 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  o;.    struct OP
1c6cb 5f 49 64 78 52 6f 77 69 64 5f 73 74 61 63 6b 5f  _IdxRowid_stack_
1c6cc 76 61 72 73 20 7b 0a 20 20 20 20 20 20 42 74 43  vars {.      BtC
1c6cd 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20  ursor *pCrsr;.  
1c6ce 20 20 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a      VdbeCursor *
1c6cf 70 43 3b 0a 20 20 20 20 20 20 69 36 34 20 72 6f  pC;.      i64 ro
1c6d0 77 69 64 3b 0a 20 20 20 20 7d 20 62 70 3b 0a 20  wid;.    } bp;. 
1c6d1 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49 64 78     struct OP_Idx
1c6d2 47 45 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  GE_stack_vars {.
1c6d3 20 20 20 20 20 20 56 64 62 65 43 75 72 73 6f 72        VdbeCursor
1c6d4 20 2a 70 43 3b 0a 20 20 20 20 20 20 69 6e 74 20   *pC;.      int 
1c6d5 72 65 73 3b 0a 20 20 20 20 20 20 55 6e 70 61 63  res;.      Unpac
1c6d6 6b 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20  kedRecord r;.   
1c6d7 20 7d 20 62 71 3b 0a 20 20 20 20 73 74 72 75 63   } bq;.    struc
1c6d8 74 20 4f 50 5f 44 65 73 74 72 6f 79 5f 73 74 61  t OP_Destroy_sta
1c6d9 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c6da 69 6e 74 20 69 4d 6f 76 65 64 3b 0a 20 20 20 20  int iMoved;.    
1c6db 20 20 69 6e 74 20 69 43 6e 74 3b 0a 20 20 20 20    int iCnt;.    
1c6dc 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b 0a 20    Vdbe *pVdbe;. 
1c6dd 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20       int iDb;.  
1c6de 20 20 7d 20 62 72 3b 0a 20 20 20 20 73 74 72 75    } br;.    stru
1c6df 63 74 20 4f 50 5f 43 6c 65 61 72 5f 73 74 61 63  ct OP_Clear_stac
1c6e0 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 69  k_vars {.      i
1c6e1 6e 74 20 6e 43 68 61 6e 67 65 3b 0a 20 20 20 20  nt nChange;.    
1c6e2 7d 20 62 73 3b 0a 20 20 20 20 73 74 72 75 63 74  } bs;.    struct
1c6e3 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 5f   OP_CreateTable_
1c6e4 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c6e5 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20 20 20     int pgno;.   
1c6e6 20 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20     int flags;.  
1c6e7 20 20 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 20      Db *pDb;.   
1c6e8 20 7d 20 62 74 3b 0a 20 20 20 20 73 74 72 75 63   } bt;.    struc
1c6e9 74 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61  t OP_ParseSchema
1c6ea 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c6eb 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20      int iDb;.   
1c6ec 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1c6ed 4d 61 73 74 65 72 3b 0a 20 20 20 20 20 20 63 68  Master;.      ch
1c6ee 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 20 20 20 20  ar *zSql;.      
1c6ef 49 6e 69 74 44 61 74 61 20 69 6e 69 74 44 61 74  InitData initDat
1c6f0 61 3b 0a 20 20 20 20 7d 20 62 75 3b 0a 20 20 20  a;.    } bu;.   
1c6f1 20 73 74 72 75 63 74 20 4f 50 5f 49 6e 74 65 67   struct OP_Integ
1c6f2 72 69 74 79 43 6b 5f 73 74 61 63 6b 5f 76 61 72  rityCk_stack_var
1c6f3 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 52  s {.      int nR
1c6f4 6f 6f 74 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d  oot;      /* Num
1c6f5 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 74 6f  ber of tables to
1c6f6 20 63 68 65 63 6b 2e 20 20 28 4e 75 6d 62 65 72   check.  (Number
1c6f7 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73 2e 29   of root pages.)
1c6f8 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 2a 61   */.      int *a
1c6f9 52 6f 6f 74 3b 20 20 20 20 20 2f 2a 20 41 72 72  Root;     /* Arr
1c6fa 61 79 20 6f 66 20 72 6f 6f 74 70 61 67 65 20 6e  ay of rootpage n
1c6fb 75 6d 62 65 72 73 20 66 6f 72 20 74 61 62 6c 65  umbers for table
1c6fc 73 20 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20  s to be checked 
1c6fd 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 20  */.      int j; 
1c6fe 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
1c6ff 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20   counter */.    
1c700 20 20 69 6e 74 20 6e 45 72 72 3b 20 20 20 20 20    int nErr;     
1c701 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
1c702 72 72 6f 72 73 20 72 65 70 6f 72 74 65 64 20 2a  rrors reported *
1c703 2f 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 3b  /.      char *z;
1c704 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20          /* Text 
1c705 6f 66 20 74 68 65 20 65 72 72 6f 72 20 72 65 70  of the error rep
1c706 6f 72 74 20 2a 2f 0a 20 20 20 20 20 20 4d 65 6d  ort */.      Mem
1c707 20 2a 70 6e 45 72 72 3b 20 20 20 20 20 2f 2a 20   *pnErr;     /* 
1c708 52 65 67 69 73 74 65 72 20 6b 65 65 70 69 6e 67  Register keeping
1c709 20 74 72 61 63 6b 20 6f 66 20 65 72 72 6f 72 73   track of errors
1c70a 20 72 65 6d 61 69 6e 69 6e 67 20 2a 2f 0a 20 20   remaining */.  
1c70b 20 20 7d 20 62 76 3b 0a 20 20 20 20 73 74 72 75    } bv;.    stru
1c70c 63 74 20 4f 50 5f 52 6f 77 53 65 74 41 64 64 5f  ct OP_RowSetAdd_
1c70d 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c70e 20 20 20 4d 65 6d 20 2a 70 49 64 78 3b 0a 20 20     Mem *pIdx;.  
1c70f 20 20 20 20 4d 65 6d 20 2a 70 56 61 6c 3b 0a 20      Mem *pVal;. 
1c710 20 20 20 7d 20 62 77 3b 0a 20 20 20 20 73 74 72     } bw;.    str
1c711 75 63 74 20 4f 50 5f 52 6f 77 53 65 74 52 65 61  uct OP_RowSetRea
1c712 64 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  d_stack_vars {. 
1c713 20 20 20 20 20 4d 65 6d 20 2a 70 49 64 78 3b 0a       Mem *pIdx;.
1c714 20 20 20 20 20 20 69 36 34 20 76 61 6c 3b 0a 20        i64 val;. 
1c715 20 20 20 7d 20 62 78 3b 0a 20 20 20 20 73 74 72     } bx;.    str
1c716 75 63 74 20 4f 50 5f 52 6f 77 53 65 74 54 65 73  uct OP_RowSetTes
1c717 74 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  t_stack_vars {. 
1c718 20 20 20 20 20 69 6e 74 20 69 53 65 74 3b 0a 20       int iSet;. 
1c719 20 20 20 20 20 69 6e 74 20 65 78 69 73 74 73 3b       int exists;
1c71a 0a 20 20 20 20 7d 20 62 79 3b 0a 20 20 20 20 73  .    } by;.    s
1c71b 74 72 75 63 74 20 4f 50 5f 50 72 6f 67 72 61 6d  truct OP_Program
1c71c 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20  _stack_vars {.  
1c71d 20 20 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20      int nMem;   
1c71e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1c71f 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20  umber of memory 
1c720 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 73 75  registers for su
1c721 62 2d 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 20  b-program */.   
1c722 20 20 20 69 6e 74 20 6e 42 79 74 65 3b 20 20 20     int nByte;   
1c723 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79             /* By
1c724 74 65 73 20 6f 66 20 72 75 6e 74 69 6d 65 20 73  tes of runtime s
1c725 70 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f  pace required fo
1c726 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 2a 2f  r sub-program */
1c727 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 52 74 3b  .      Mem *pRt;
1c728 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1c729 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 61 6c  * Register to al
1c72a 6c 6f 63 61 74 65 20 72 75 6e 74 69 6d 65 20 73  locate runtime s
1c72b 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 4d 65  pace */.      Me
1c72c 6d 20 2a 70 4d 65 6d 3b 20 20 20 20 20 20 20 20  m *pMem;        
1c72d 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
1c72e 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
1c72f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 2a 2f   memory cells */
1c730 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 45 6e 64  .      Mem *pEnd
1c731 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1c732 2a 20 4c 61 73 74 20 6d 65 6d 6f 72 79 20 63 65  * Last memory ce
1c733 6c 6c 20 69 6e 20 6e 65 77 20 61 72 72 61 79 20  ll in new array 
1c734 2a 2f 0a 20 20 20 20 20 20 56 64 62 65 46 72 61  */.      VdbeFra
1c735 6d 65 20 2a 70 46 72 61 6d 65 3b 20 20 20 20 20  me *pFrame;     
1c736 20 2f 2a 20 4e 65 77 20 76 64 62 65 20 66 72 61   /* New vdbe fra
1c737 6d 65 20 74 6f 20 65 78 65 63 75 74 65 20 69 6e  me to execute in
1c738 20 2a 2f 0a 20 20 20 20 20 20 53 75 62 50 72 6f   */.      SubPro
1c739 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20  gram *pProgram; 
1c73a 20 20 2f 2a 20 53 75 62 2d 70 72 6f 67 72 61 6d    /* Sub-program
1c73b 20 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 20   to execute */. 
1c73c 20 20 20 20 20 76 6f 69 64 20 2a 74 3b 20 20 20       void *t;   
1c73d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1c73e 54 6f 6b 65 6e 20 69 64 65 6e 74 69 66 79 69 6e  Token identifyin
1c73f 67 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 20  g trigger */.   
1c740 20 7d 20 62 7a 3b 0a 20 20 20 20 73 74 72 75 63   } bz;.    struc
1c741 74 20 4f 50 5f 50 61 72 61 6d 5f 73 74 61 63 6b  t OP_Param_stack
1c742 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20 56 64  _vars {.      Vd
1c743 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b  beFrame *pFrame;
1c744 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 49 6e 3b  .      Mem *pIn;
1c745 0a 20 20 20 20 7d 20 63 61 3b 0a 20 20 20 20 73  .    } ca;.    s
1c746 74 72 75 63 74 20 4f 50 5f 4d 65 6d 4d 61 78 5f  truct OP_MemMax_
1c747 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20  stack_vars {.   
1c748 20 20 20 4d 65 6d 20 2a 70 49 6e 31 3b 0a 20 20     Mem *pIn1;.  
1c749 20 20 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70      VdbeFrame *p
1c74a 46 72 61 6d 65 3b 0a 20 20 20 20 7d 20 63 62 3b  Frame;.    } cb;
1c74b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 41  .    struct OP_A
1c74c 67 67 53 74 65 70 5f 73 74 61 63 6b 5f 76 61 72  ggStep_stack_var
1c74d 73 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b  s {.      int n;
1c74e 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
1c74f 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20      Mem *pMem;. 
1c750 20 20 20 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a       Mem *pRec;.
1c751 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f        sqlite3_co
1c752 6e 74 65 78 74 20 63 74 78 3b 0a 20 20 20 20 20  ntext ctx;.     
1c753 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
1c754 2a 61 70 56 61 6c 3b 0a 20 20 20 20 7d 20 63 63  *apVal;.    } cc
1c755 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f  ;.    struct OP_
1c756 41 67 67 46 69 6e 61 6c 5f 73 74 61 63 6b 5f 76  AggFinal_stack_v
1c757 61 72 73 20 7b 0a 20 20 20 20 20 20 4d 65 6d 20  ars {.      Mem 
1c758 2a 70 4d 65 6d 3b 0a 20 20 20 20 7d 20 63 64 3b  *pMem;.    } cd;
1c759 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50 5f 49  .    struct OP_I
1c75a 6e 63 72 56 61 63 75 75 6d 5f 73 74 61 63 6b 5f  ncrVacuum_stack_
1c75b 76 61 72 73 20 7b 0a 20 20 20 20 20 20 42 74 72  vars {.      Btr
1c75c 65 65 20 2a 70 42 74 3b 0a 20 20 20 20 7d 20 63  ee *pBt;.    } c
1c75d 65 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f 50  e;.    struct OP
1c75e 5f 56 42 65 67 69 6e 5f 73 74 61 63 6b 5f 76 61  _VBegin_stack_va
1c75f 72 73 20 7b 0a 20 20 20 20 20 20 56 54 61 62 6c  rs {.      VTabl
1c760 65 20 2a 70 56 54 61 62 3b 0a 20 20 20 20 7d 20  e *pVTab;.    } 
1c761 63 66 3b 0a 20 20 20 20 73 74 72 75 63 74 20 4f  cf;.    struct O
1c762 50 5f 56 4f 70 65 6e 5f 73 74 61 63 6b 5f 76 61  P_VOpen_stack_va
1c763 72 73 20 7b 0a 20 20 20 20 20 20 56 64 62 65 43  rs {.      VdbeC
1c764 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 20  ursor *pCur;.   
1c765 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f     sqlite3_vtab_
1c766 63 75 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72  cursor *pVtabCur
1c767 73 6f 72 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  sor;.      sqlit
1c768 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a  e3_vtab *pVtab;.
1c769 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 6f        sqlite3_mo
1c76a 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20  dule *pModule;. 
1c76b 20 20 20 7d 20 63 67 3b 0a 20 20 20 20 73 74 72     } cg;.    str
1c76c 75 63 74 20 4f 50 5f 56 46 69 6c 74 65 72 5f 73  uct OP_VFilter_s
1c76d 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c76e 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20    int nArg;.    
1c76f 20 20 69 6e 74 20 69 51 75 65 72 79 3b 0a 20 20    int iQuery;.  
1c770 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65      const sqlite
1c771 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  3_module *pModul
1c772 65 3b 0a 20 20 20 20 20 20 4d 65 6d 20 2a 70 51  e;.      Mem *pQ
1c773 75 65 72 79 3b 0a 20 20 20 20 20 20 4d 65 6d 20  uery;.      Mem 
1c774 2a 70 41 72 67 63 3b 0a 20 20 20 20 20 20 73 71  *pArgc;.      sq
1c775 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
1c776 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a  r *pVtabCursor;.
1c777 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74        sqlite3_vt
1c778 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20  ab *pVtab;.     
1c779 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75   VdbeCursor *pCu
1c77a 72 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73  r;.      int res
1c77b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20  ;.      int i;. 
1c77c 20 20 20 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67       Mem **apArg
1c77d 3b 0a 20 20 20 20 7d 20 63 68 3b 0a 20 20 20 20  ;.    } ch;.    
1c77e 73 74 72 75 63 74 20 4f 50 5f 56 43 6f 6c 75 6d  struct OP_VColum
1c77f 6e 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20  n_stack_vars {. 
1c780 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61       sqlite3_vta
1c781 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20 20  b *pVtab;.      
1c782 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
1c783 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20  dule *pModule;. 
1c784 20 20 20 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b       Mem *pDest;
1c785 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63  .      sqlite3_c
1c786 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b  ontext sContext;
1c787 0a 20 20 20 20 7d 20 63 69 3b 0a 20 20 20 20 73  .    } ci;.    s
1c788 74 72 75 63 74 20 4f 50 5f 56 4e 65 78 74 5f 73  truct OP_VNext_s
1c789 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c78a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
1c78b 70 56 74 61 62 3b 0a 20 20 20 20 20 20 63 6f 6e  pVtab;.      con
1c78c 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
1c78d 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20  e *pModule;.    
1c78e 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20 20 20    int res;.     
1c78f 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75   VdbeCursor *pCu
1c790 72 3b 0a 20 20 20 20 7d 20 63 6a 3b 0a 20 20 20  r;.    } cj;.   
1c791 20 73 74 72 75 63 74 20 4f 50 5f 56 52 65 6e 61   struct OP_VRena
1c792 6d 65 5f 73 74 61 63 6b 5f 76 61 72 73 20 7b 0a  me_stack_vars {.
1c793 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74        sqlite3_vt
1c794 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 20 20 20  ab *pVtab;.     
1c795 20 4d 65 6d 20 2a 70 4e 61 6d 65 3b 0a 20 20 20   Mem *pName;.   
1c796 20 7d 20 63 6b 3b 0a 20 20 20 20 73 74 72 75 63   } ck;.    struc
1c797 74 20 4f 50 5f 56 55 70 64 61 74 65 5f 73 74 61  t OP_VUpdate_sta
1c798 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20 20  ck_vars {.      
1c799 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
1c79a 74 61 62 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  tab;.      sqlit
1c79b 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75  e3_module *pModu
1c79c 6c 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 41  le;.      int nA
1c79d 72 67 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b  rg;.      int i;
1c79e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e  .      sqlite_in
1c79f 74 36 34 20 72 6f 77 69 64 3b 0a 20 20 20 20 20  t64 rowid;.     
1c7a0 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 20 20   Mem **apArg;.  
1c7a1 20 20 20 20 4d 65 6d 20 2a 70 58 3b 0a 20 20 20      Mem *pX;.   
1c7a2 20 7d 20 63 6c 3b 0a 20 20 20 20 73 74 72 75 63   } cl;.    struc
1c7a3 74 20 4f 50 5f 50 61 67 65 63 6f 75 6e 74 5f 73  t OP_Pagecount_s
1c7a4 74 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20  tack_vars {.    
1c7a5 20 20 69 6e 74 20 70 31 3b 0a 20 20 20 20 20 20    int p1;.      
1c7a6 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20 20 20 20  int nPage;.     
1c7a7 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a   Pager *pPager;.
1c7a8 20 20 20 20 7d 20 63 6d 3b 0a 20 20 20 20 73 74      } cm;.    st
1c7a9 72 75 63 74 20 4f 50 5f 54 72 61 63 65 5f 73 74  ruct OP_Trace_st
1c7aa 61 63 6b 5f 76 61 72 73 20 7b 0a 20 20 20 20 20  ack_vars {.     
1c7ab 20 63 68 61 72 20 2a 7a 54 72 61 63 65 3b 0a 20   char *zTrace;. 
1c7ac 20 20 20 7d 20 63 6e 3b 0a 20 20 7d 20 75 3b 0a     } cn;.  } u;.
1c7ad 20 20 2f 2a 20 45 6e 64 20 61 75 74 6f 6d 61 74    /* End automat
1c7ae 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64  ically generated
1c7af 20 63 6f 64 65 0a 20 20 2a 2a 2a 2a 2a 2a 2a 2a   code.  ********
1c7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c7b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c7b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c7b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 20  ************/.. 
1c7b4 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69   assert( p->magi
1c7b5 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55  c==VDBE_MAGIC_RU
1c7b6 4e 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33  N );  /* sqlite3
1c7b7 5f 73 74 65 70 28 29 20 76 65 72 69 66 69 65 73  _step() verifies
1c7b8 20 74 68 69 73 20 2a 2f 0a 20 20 61 73 73 65 72   this */.  asser
1c7b9 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51  t( db->magic==SQ
1c7ba 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20  LITE_MAGIC_BUSY 
1c7bb 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1c7bc 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28  MutexArrayEnter(
1c7bd 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 72 63 3d  p);.  if( p->rc=
1c7be 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b  =SQLITE_NOMEM ){
1c7bf 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70  .    /* This hap
1c7c0 70 65 6e 73 20 69 66 20 61 20 6d 61 6c 6c 6f 63  pens if a malloc
1c7c1 28 29 20 69 6e 73 69 64 65 20 61 20 63 61 6c 6c  () inside a call
1c7c2 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   to sqlite3_colu
1c7c3 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 20 20 20  mn_text() or.   
1c7c4 20 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75   ** sqlite3_colu
1c7c5 6d 6e 5f 74 65 78 74 31 36 28 29 20 66 61 69 6c  mn_text16() fail
1c7c6 65 64 2e 20 20 2a 2f 0a 20 20 20 20 67 6f 74 6f  ed.  */.    goto
1c7c7 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 61   no_mem;.  }.  a
1c7c8 73 73 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51  ssert( p->rc==SQ
1c7c9 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63  LITE_OK || p->rc
1c7ca 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 3b  ==SQLITE_BUSY );
1c7cb 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54  .  p->rc = SQLIT
1c7cc 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20  E_OK;.  assert( 
1c7cd 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 30 20 29 3b  p->explain==0 );
1c7ce 0a 20 20 70 2d 3e 70 52 65 73 75 6c 74 53 65 74  .  p->pResultSet
1c7cf 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 62 75 73 79   = 0;.  db->busy
1c7d0 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20 3d 20  Handler.nBusy = 
1c7d1 30 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49  0;.  CHECK_FOR_I
1c7d2 4e 54 45 52 52 55 50 54 3b 0a 20 20 73 71 6c 69  NTERRUPT;.  sqli
1c7d3 74 65 33 56 64 62 65 49 4f 54 72 61 63 65 53 71  te3VdbeIOTraceSq
1c7d4 6c 28 70 29 3b 0a 23 69 66 64 65 66 20 53 51 4c  l(p);.#ifdef SQL
1c7d5 49 54 45 5f 44 45 42 55 47 0a 20 20 73 71 6c 69  ITE_DEBUG.  sqli
1c7d6 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61  te3BeginBenignMa
1c7d7 6c 6c 6f 63 28 29 3b 0a 20 20 69 66 28 20 70 2d  lloc();.  if( p-
1c7d8 3e 70 63 3d 3d 30 20 0a 20 20 20 26 26 20 28 28  >pc==0 .   && ((
1c7d9 70 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  p->db->flags & S
1c7da 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69 6e  QLITE_VdbeListin
1c7db 67 29 20 7c 7c 20 66 69 6c 65 45 78 69 73 74 73  g) || fileExists
1c7dc 28 64 62 2c 20 22 76 64 62 65 5f 65 78 70 6c 61  (db, "vdbe_expla
1c7dd 69 6e 22 29 29 0a 20 20 29 7b 0a 20 20 20 20 69  in")).  ){.    i
1c7de 6e 74 20 69 3b 0a 20 20 20 20 70 72 69 6e 74 66  nt i;.    printf
1c7df 28 22 56 44 42 45 20 50 72 6f 67 72 61 6d 20 4c  ("VDBE Program L
1c7e0 69 73 74 69 6e 67 3a 5c 6e 22 29 3b 0a 20 20 20  isting:\n");.   
1c7e1 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e   sqlite3VdbePrin
1c7e2 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 66 6f 72  tSql(p);.    for
1c7e3 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20  (i=0; i<p->nOp; 
1c7e4 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
1c7e5 74 65 33 56 64 62 65 50 72 69 6e 74 4f 70 28 73  te3VdbePrintOp(s
1c7e6 74 64 6f 75 74 2c 20 69 2c 20 26 70 2d 3e 61 4f  tdout, i, &p->aO
1c7e7 70 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  p[i]);.    }.  }
1c7e8 0a 20 20 69 66 28 20 66 69 6c 65 45 78 69 73 74  .  if( fileExist
1c7e9 73 28 64 62 2c 20 22 76 64 62 65 5f 74 72 61 63  s(db, "vdbe_trac
1c7ea 65 22 29 20 29 7b 0a 20 20 20 20 70 2d 3e 74 72  e") ){.    p->tr
1c7eb 61 63 65 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20  ace = stdout;.  
1c7ec 7d 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 42 65  }.  sqlite3EndBe
1c7ed 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 23 65  nignMalloc();.#e
1c7ee 6e 64 69 66 0a 20 20 66 6f 72 28 70 63 3d 70 2d  ndif.  for(pc=p-
1c7ef 3e 70 63 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  >pc; rc==SQLITE_
1c7f0 4f 4b 3b 20 70 63 2b 2b 29 7b 0a 20 20 20 20 61  OK; pc++){.    a
1c7f1 73 73 65 72 74 28 20 70 63 3e 3d 30 20 26 26 20  ssert( pc>=0 && 
1c7f2 70 63 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 20  pc<p->nOp );.   
1c7f3 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
1c7f4 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f  ailed ) goto no_
1c7f5 6d 65 6d 3b 0a 23 69 66 64 65 66 20 56 44 42 45  mem;.#ifdef VDBE
1c7f6 5f 50 52 4f 46 49 4c 45 0a 20 20 20 20 6f 72 69  _PROFILE.    ori
1c7f7 67 50 63 20 3d 20 70 63 3b 0a 20 20 20 20 73 74  gPc = pc;.    st
1c7f8 61 72 74 20 3d 20 73 71 6c 69 74 65 33 48 77 74  art = sqlite3Hwt
1c7f9 69 6d 65 28 29 3b 0a 23 65 6e 64 69 66 0a 20 20  ime();.#endif.  
1c7fa 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b    pOp = &p->aOp[
1c7fb 70 63 5d 3b 0a 0a 20 20 20 20 2f 2a 20 4f 6e 6c  pc];..    /* Onl
1c7fc 79 20 61 6c 6c 6f 77 20 74 72 61 63 69 6e 67 20  y allow tracing 
1c7fd 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20  if SQLITE_DEBUG 
1c7fe 69 73 20 64 65 66 69 6e 65 64 2e 0a 20 20 20 20  is defined..    
1c7ff 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
1c800 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28 20 70  _DEBUG.    if( p
1c801 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 20  ->trace ){.     
1c802 20 69 66 28 20 70 63 3d 3d 30 20 29 7b 0a 20 20   if( pc==0 ){.  
1c803 20 20 20 20 20 20 70 72 69 6e 74 66 28 22 56 44        printf("VD
1c804 42 45 20 45 78 65 63 75 74 69 6f 6e 20 54 72 61  BE Execution Tra
1c805 63 65 3a 5c 6e 22 29 3b 0a 20 20 20 20 20 20 20  ce:\n");.       
1c806 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e   sqlite3VdbePrin
1c807 74 53 71 6c 28 70 29 3b 0a 20 20 20 20 20 20 7d  tSql(p);.      }
1c808 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1c809 62 65 50 72 69 6e 74 4f 70 28 70 2d 3e 74 72 61  bePrintOp(p->tra
1c80a 63 65 2c 20 70 63 2c 20 70 4f 70 29 3b 0a 20 20  ce, pc, pOp);.  
1c80b 20 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 74    }.    if( p->t
1c80c 72 61 63 65 3d 3d 30 20 26 26 20 70 63 3d 3d 30  race==0 && pc==0
1c80d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1c80e 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c  3BeginBenignMall
1c80f 6f 63 28 29 3b 0a 20 20 20 20 20 20 69 66 28 20  oc();.      if( 
1c810 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c 20 22  fileExists(db, "
1c811 76 64 62 65 5f 73 71 6c 74 72 61 63 65 22 29 20  vdbe_sqltrace") 
1c812 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1c813 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 70  e3VdbePrintSql(p
1c814 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1c815 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
1c816 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 7d  nMalloc();.    }
1c817 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 0a 0a  .#endif.      ..
1c818 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20      /* Check to 
1c819 73 65 65 20 69 66 20 77 65 20 6e 65 65 64 20 74  see if we need t
1c81a 6f 20 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6e  o simulate an in
1c81b 74 65 72 72 75 70 74 2e 20 20 54 68 69 73 20 6f  terrupt.  This o
1c81c 6e 6c 79 20 68 61 70 70 65 6e 73 0a 20 20 20 20  nly happens.    
1c81d 2a 2a 20 69 66 20 77 65 20 68 61 76 65 20 61 20  ** if we have a 
1c81e 73 70 65 63 69 61 6c 20 74 65 73 74 20 62 75 69  special test bui
1c81f 6c 64 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 64 65  ld..    */.#ifde
1c820 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
1c821 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69 6e    if( sqlite3_in
1c822 74 65 72 72 75 70 74 5f 63 6f 75 6e 74 3e 30 20  terrupt_count>0 
1c823 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1c824 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74  _interrupt_count
1c825 2d 2d 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71  --;.      if( sq
1c826 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 5f  lite3_interrupt_
1c827 63 6f 75 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20  count==0 ){.    
1c828 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 65      sqlite3_inte
1c829 72 72 75 70 74 28 64 62 29 3b 0a 20 20 20 20 20  rrupt(db);.     
1c82a 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a   }.    }.#endif.
1c82b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1c82c 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53 5f 43 41  OMIT_PROGRESS_CA
1c82d 4c 4c 42 41 43 4b 0a 20 20 20 20 2f 2a 20 43 61  LLBACK.    /* Ca
1c82e 6c 6c 20 74 68 65 20 70 72 6f 67 72 65 73 73 20  ll the progress 
1c82f 63 61 6c 6c 62 61 63 6b 20 69 66 20 69 74 20 69  callback if it i
1c830 73 20 63 6f 6e 66 69 67 75 72 65 64 20 61 6e 64  s configured and
1c831 20 74 68 65 20 72 65 71 75 69 72 65 64 20 6e 75   the required nu
1c832 6d 62 65 72 0a 20 20 20 20 2a 2a 20 6f 66 20 56  mber.    ** of V
1c833 44 42 45 20 6f 70 73 20 68 61 76 65 20 62 65 65  DBE ops have bee
1c834 6e 20 65 78 65 63 75 74 65 64 20 28 65 69 74 68  n executed (eith
1c835 65 72 20 73 69 6e 63 65 20 74 68 69 73 20 69 6e  er since this in
1c836 76 6f 63 61 74 69 6f 6e 20 6f 66 0a 20 20 20 20  vocation of.    
1c837 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 45 78  ** sqlite3VdbeEx
1c838 65 63 28 29 20 6f 72 20 73 69 6e 63 65 20 6c 61  ec() or since la
1c839 73 74 20 74 69 6d 65 20 74 68 65 20 70 72 6f 67  st time the prog
1c83a 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 77 61  ress callback wa
1c83b 73 20 63 61 6c 6c 65 64 29 2e 0a 20 20 20 20 2a  s called)..    *
1c83c 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72 65 73  * If the progres
1c83d 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  s callback retur
1c83e 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 65 78 69  ns non-zero, exi
1c83f 74 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61  t the virtual ma
1c840 63 68 69 6e 65 20 77 69 74 68 0a 20 20 20 20 2a  chine with.    *
1c841 2a 20 61 20 72 65 74 75 72 6e 20 63 6f 64 65 20  * a return code 
1c842 53 51 4c 49 54 45 5f 41 42 4f 52 54 2e 0a 20 20  SQLITE_ABORT..  
1c843 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 64 62 2d    */.    if( db-
1c844 3e 78 50 72 6f 67 72 65 73 73 20 29 7b 0a 20 20  >xProgress ){.  
1c845 20 20 20 20 69 66 28 20 64 62 2d 3e 6e 50 72 6f      if( db->nPro
1c846 67 72 65 73 73 4f 70 73 3d 3d 6e 50 72 6f 67 72  gressOps==nProgr
1c847 65 73 73 4f 70 73 20 29 7b 0a 20 20 20 20 20 20  essOps ){.      
1c848 20 20 69 6e 74 20 70 72 63 3b 0a 20 20 20 20 20    int prc;.     
1c849 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61     if( sqlite3Sa
1c84a 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f  fetyOff(db) ) go
1c84b 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1c84c 6d 69 73 75 73 65 3b 0a 20 20 20 20 20 20 20 20  misuse;.        
1c84d 70 72 63 20 3d 64 62 2d 3e 78 50 72 6f 67 72 65  prc =db->xProgre
1c84e 73 73 28 64 62 2d 3e 70 50 72 6f 67 72 65 73 73  ss(db->pProgress
1c84f 41 72 67 29 3b 0a 20 20 20 20 20 20 20 20 69 66  Arg);.        if
1c850 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1c851 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  n(db) ) goto abo
1c852 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1c853 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 72  ;.        if( pr
1c854 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  c!=0 ){.        
1c855 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4e    rc = SQLITE_IN
1c856 54 45 52 52 55 50 54 3b 0a 20 20 20 20 20 20 20  TERRUPT;.       
1c857 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72     goto vdbe_err
1c858 6f 72 5f 68 61 6c 74 3b 0a 20 20 20 20 20 20 20  or_halt;.       
1c859 20 7d 0a 20 20 20 20 20 20 20 20 6e 50 72 6f 67   }.        nProg
1c85a 72 65 73 73 4f 70 73 20 3d 20 30 3b 0a 20 20 20  ressOps = 0;.   
1c85b 20 20 20 7d 0a 20 20 20 20 20 20 6e 50 72 6f 67     }.      nProg
1c85c 72 65 73 73 4f 70 73 2b 2b 3b 0a 20 20 20 20 7d  ressOps++;.    }
1c85d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20  .#endif..    /* 
1c85e 44 6f 20 63 6f 6d 6d 6f 6e 20 73 65 74 75 70 20  Do common setup 
1c85f 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 61  processing for a
1c860 6e 79 20 6f 70 63 6f 64 65 20 74 68 61 74 20 69  ny opcode that i
1c861 73 20 6d 61 72 6b 65 64 0a 20 20 20 20 2a 2a 20  s marked.    ** 
1c862 77 69 74 68 20 74 68 65 20 22 6f 75 74 32 2d 70  with the "out2-p
1c863 72 65 72 65 6c 65 61 73 65 22 20 74 61 67 2e 20  rerelease" tag. 
1c864 20 53 75 63 68 20 6f 70 63 6f 64 65 73 20 68 61   Such opcodes ha
1c865 76 65 20 61 20 73 69 6e 67 6c 65 0a 20 20 20 20  ve a single.    
1c866 2a 2a 20 6f 75 74 70 75 74 20 77 68 69 63 68 20  ** output which 
1c867 69 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20  is specified by 
1c868 74 68 65 20 50 32 20 70 61 72 61 6d 65 74 65 72  the P2 parameter
1c869 2e 20 20 54 68 65 20 50 32 20 72 65 67 69 73 74  .  The P2 regist
1c86a 65 72 0a 20 20 20 20 2a 2a 20 69 73 20 69 6e 69  er.    ** is ini
1c86b 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 20 4e 55  tialized to a NU
1c86c 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6f  LL..    */.    o
1c86d 70 50 72 6f 70 65 72 74 79 20 3d 20 6f 70 63 6f  pProperty = opco
1c86e 64 65 50 72 6f 70 65 72 74 79 5b 70 4f 70 2d 3e  deProperty[pOp->
1c86f 6f 70 63 6f 64 65 5d 3b 0a 20 20 20 20 69 66 28  opcode];.    if(
1c870 20 28 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f   (opProperty & O
1c871 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c  PFLG_OUT2_PREREL
1c872 45 41 53 45 29 21 3d 30 20 29 7b 0a 20 20 20 20  EASE)!=0 ){.    
1c873 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1c874 32 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  2>0 );.      ass
1c875 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d  ert( pOp->p2<=p-
1c876 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70  >nMem );.      p
1c877 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Out = &p->aMem[p
1c878 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 73  Op->p2];.      s
1c879 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1c87a 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70 4f 75  easeExternal(pOu
1c87b 74 29 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e  t);.      pOut->
1c87c 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
1c87d 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 6e 20  ;.      pOut->n 
1c87e 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 20  = 0;.    }else. 
1c87f 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f 6d 6d 6f  .    /* Do commo
1c880 6e 20 73 65 74 75 70 20 66 6f 72 20 6f 70 63 6f  n setup for opco
1c881 64 65 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20  des marked with 
1c882 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  one of the follo
1c883 77 69 6e 67 0a 20 20 20 20 2a 2a 20 63 6f 6d 62  wing.    ** comb
1c884 69 6e 61 74 69 6f 6e 73 20 6f 66 20 70 72 6f 70  inations of prop
1c885 65 72 74 69 65 73 2e 0a 20 20 20 20 2a 2a 0a 20  erties..    **. 
1c886 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20     **           
1c887 69 6e 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  in1.    **      
1c888 20 20 20 20 20 69 6e 31 20 69 6e 32 0a 20 20 20       in1 in2.   
1c889 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69 6e   **           in
1c88a 31 20 69 6e 32 20 6f 75 74 33 0a 20 20 20 20 2a  1 in2 out3.    *
1c88b 2a 20 20 20 20 20 20 20 20 20 20 20 69 6e 31 20  *           in1 
1c88c 69 6e 33 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  in3.    **.    *
1c88d 2a 20 56 61 72 69 61 62 6c 65 73 20 70 49 6e 31  * Variables pIn1
1c88e 2c 20 70 49 6e 32 2c 20 61 6e 64 20 70 49 6e 33  , pIn2, and pIn3
1c88f 20 61 72 65 20 6d 61 64 65 20 74 6f 20 70 6f 69   are made to poi
1c890 6e 74 20 74 6f 20 61 70 70 72 6f 70 72 69 61 74  nt to appropriat
1c891 65 0a 20 20 20 20 2a 2a 20 72 65 67 69 73 74 65  e.    ** registe
1c892 72 73 20 66 6f 72 20 69 6e 70 75 74 73 2e 20 20  rs for inputs.  
1c893 56 61 72 69 61 62 6c 65 20 70 4f 75 74 20 70 6f  Variable pOut po
1c894 69 6e 74 73 20 74 6f 20 74 68 65 20 6f 75 74 70  ints to the outp
1c895 75 74 20 72 65 67 69 73 74 65 72 2e 0a 20 20 20  ut register..   
1c896 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 6f 70 50   */.    if( (opP
1c897 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f  roperty & OPFLG_
1c898 49 4e 31 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  IN1)!=0 ){.     
1c899 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
1c89a 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  >0 );.      asse
1c89b 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e  rt( pOp->p1<=p->
1c89c 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70 49  nMem );.      pI
1c89d 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  n1 = &p->aMem[pO
1c89e 70 2d 3e 70 31 5d 3b 0a 20 20 20 20 20 20 52 45  p->p1];.      RE
1c89f 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
1c8a0 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 20  ->p1, pIn1);.   
1c8a1 20 20 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72     if( (opProper
1c8a2 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 32 29 21  ty & OPFLG_IN2)!
1c8a3 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  =0 ){.        as
1c8a4 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20  sert( pOp->p2>0 
1c8a5 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
1c8a6 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e  t( pOp->p2<=p->n
1c8a7 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 70  Mem );.        p
1c8a8 49 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  In2 = &p->aMem[p
1c8a9 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 20  Op->p2];.       
1c8aa 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
1c8ab 70 4f 70 2d 3e 70 32 2c 20 70 49 6e 32 29 3b 0a  pOp->p2, pIn2);.
1c8ac 20 20 20 20 20 20 20 20 2f 2a 20 41 73 20 63 75          /* As cu
1c8ad 72 72 65 6e 74 6c 79 20 69 6d 70 6c 65 6d 65 6e  rrently implemen
1c8ae 74 65 64 2c 20 69 6e 32 20 69 6d 70 6c 69 65 73  ted, in2 implies
1c8af 20 6f 75 74 33 2e 20 20 54 68 65 72 65 20 69 73   out3.  There is
1c8b0 20 6e 6f 20 72 65 61 73 6f 6e 0a 20 20 20 20 20   no reason.     
1c8b1 20 20 20 2a 2a 20 77 68 79 20 74 68 69 73 20 68     ** why this h
1c8b2 61 73 20 74 6f 20 62 65 2c 20 69 74 20 6a 75 73  as to be, it jus
1c8b3 74 20 77 6f 72 6b 65 64 20 6f 75 74 20 74 68 61  t worked out tha
1c8b4 74 20 77 61 79 2e 20 2a 2f 0a 20 20 20 20 20 20  t way. */.      
1c8b5 20 20 61 73 73 65 72 74 28 20 28 6f 70 50 72 6f    assert( (opPro
1c8b6 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f 4f 55  perty & OPFLG_OU
1c8b7 54 33 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  T3)!=0 );.      
1c8b8 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1c8b9 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61  3>0 );.        a
1c8ba 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d  ssert( pOp->p3<=
1c8bb 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20  p->nMem );.     
1c8bc 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d     pOut = &p->aM
1c8bd 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20  em[pOp->p3];.   
1c8be 20 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f 70     }else if( (op
1c8bf 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47  Property & OPFLG
1c8c0 5f 49 4e 33 29 21 3d 30 20 29 7b 0a 20 20 20 20  _IN3)!=0 ){.    
1c8c1 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
1c8c2 3e 70 33 3e 30 20 29 3b 0a 20 20 20 20 20 20 20  >p3>0 );.       
1c8c3 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33   assert( pOp->p3
1c8c4 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20  <=p->nMem );.   
1c8c5 20 20 20 20 20 70 49 6e 33 20 3d 20 26 70 2d 3e       pIn3 = &p->
1c8c6 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20  aMem[pOp->p3];. 
1c8c7 20 20 20 20 20 20 20 52 45 47 49 53 54 45 52 5f         REGISTER_
1c8c8 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70  TRACE(pOp->p3, p
1c8c9 49 6e 33 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  In3);.      }.  
1c8ca 20 20 7d 65 6c 73 65 20 69 66 28 20 28 6f 70 50    }else if( (opP
1c8cb 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f  roperty & OPFLG_
1c8cc 49 4e 32 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  IN2)!=0 ){.     
1c8cd 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32   assert( pOp->p2
1c8ce 3e 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  >0 );.      asse
1c8cf 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e  rt( pOp->p2<=p->
1c8d0 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70 49  nMem );.      pI
1c8d1 6e 32 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  n2 = &p->aMem[pO
1c8d2 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 20 20 52 45  p->p2];.      RE
1c8d3 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
1c8d4 2d 3e 70 32 2c 20 70 49 6e 32 29 3b 0a 20 20 20  ->p2, pIn2);.   
1c8d5 20 7d 65 6c 73 65 20 69 66 28 20 28 6f 70 50 72   }else if( (opPr
1c8d6 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f 49  operty & OPFLG_I
1c8d7 4e 33 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  N3)!=0 ){.      
1c8d8 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e  assert( pOp->p3>
1c8d9 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  0 );.      asser
1c8da 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e  t( pOp->p3<=p->n
1c8db 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 70 49 6e  Mem );.      pIn
1c8dc 33 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  3 = &p->aMem[pOp
1c8dd 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 52 45 47  ->p3];.      REG
1c8de 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1c8df 3e 70 33 2c 20 70 49 6e 33 29 3b 0a 20 20 20 20  >p3, pIn3);.    
1c8e0 7d 0a 0a 20 20 20 20 73 77 69 74 63 68 28 20 70  }..    switch( p
1c8e1 4f 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 0a 2f  Op->opcode ){../
1c8e2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c8e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c8e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c8e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c8e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
1c8e7 20 57 68 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73   What follows is
1c8e8 20 61 20 6d 61 73 73 69 76 65 20 73 77 69 74 63   a massive switc
1c8e9 68 20 73 74 61 74 65 6d 65 6e 74 20 77 68 65 72  h statement wher
1c8ea 65 20 65 61 63 68 20 63 61 73 65 20 69 6d 70 6c  e each case impl
1c8eb 65 6d 65 6e 74 73 20 61 0a 2a 2a 20 73 65 70 61  ements a.** sepa
1c8ec 72 61 74 65 20 69 6e 73 74 72 75 63 74 69 6f 6e  rate instruction
1c8ed 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c 20   in the virtual 
1c8ee 6d 61 63 68 69 6e 65 2e 20 20 49 66 20 77 65 20  machine.  If we 
1c8ef 66 6f 6c 6c 6f 77 20 74 68 65 20 75 73 75 61 6c  follow the usual
1c8f0 0a 2a 2a 20 69 6e 64 65 6e 74 61 74 69 6f 6e 20  .** indentation 
1c8f1 63 6f 6e 76 65 6e 74 69 6f 6e 73 2c 20 65 61 63  conventions, eac
1c8f2 68 20 63 61 73 65 20 73 68 6f 75 6c 64 20 62 65  h case should be
1c8f3 20 69 6e 64 65 6e 74 65 64 20 62 79 20 36 20 73   indented by 6 s
1c8f4 70 61 63 65 73 2e 20 20 42 75 74 0a 2a 2a 20 74  paces.  But.** t
1c8f5 68 61 74 20 69 73 20 61 20 6c 6f 74 20 6f 66 20  hat is a lot of 
1c8f6 77 61 73 74 65 64 20 73 70 61 63 65 20 6f 6e 20  wasted space on 
1c8f7 74 68 65 20 6c 65 66 74 20 6d 61 72 67 69 6e 2e  the left margin.
1c8f8 20 20 53 6f 20 74 68 65 20 63 6f 64 65 20 77 69    So the code wi
1c8f9 74 68 69 6e 0a 2a 2a 20 74 68 65 20 73 77 69 74  thin.** the swit
1c8fa 63 68 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c  ch statement wil
1c8fb 6c 20 62 72 65 61 6b 20 77 69 74 68 20 63 6f 6e  l break with con
1c8fc 76 65 6e 74 69 6f 6e 20 61 6e 64 20 62 65 20 66  vention and be f
1c8fd 6c 75 73 68 2d 6c 65 66 74 2e 20 41 6e 6f 74 68  lush-left. Anoth
1c8fe 65 72 0a 2a 2a 20 62 69 67 20 63 6f 6d 6d 65 6e  er.** big commen
1c8ff 74 20 28 73 69 6d 69 6c 61 72 20 74 6f 20 74 68  t (similar to th
1c900 69 73 20 6f 6e 65 29 20 77 69 6c 6c 20 6d 61 72  is one) will mar
1c901 6b 20 74 68 65 20 70 6f 69 6e 74 20 69 6e 20 74  k the point in t
1c902 68 65 20 63 6f 64 65 20 77 68 65 72 65 0a 2a 2a  he code where.**
1c903 20 77 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 62   we transition b
1c904 61 63 6b 20 74 6f 20 6e 6f 72 6d 61 6c 20 69 6e  ack to normal in
1c905 64 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  dentation..**.**
1c906 20 54 68 65 20 66 6f 72 6d 61 74 74 69 6e 67 20   The formatting 
1c907 6f 66 20 65 61 63 68 20 63 61 73 65 20 69 73 20  of each case is 
1c908 69 6d 70 6f 72 74 61 6e 74 2e 20 20 54 68 65 20  important.  The 
1c909 6d 61 6b 65 66 69 6c 65 20 66 6f 72 20 53 51 4c  makefile for SQL
1c90a 69 74 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 73  ite.** generates
1c90b 20 74 77 6f 20 43 20 66 69 6c 65 73 20 22 6f 70   two C files "op
1c90c 63 6f 64 65 73 2e 68 22 20 61 6e 64 20 22 6f 70  codes.h" and "op
1c90d 63 6f 64 65 73 2e 63 22 20 62 79 20 73 63 61 6e  codes.c" by scan
1c90e 6e 69 6e 67 20 74 68 69 73 0a 2a 2a 20 66 69 6c  ning this.** fil
1c90f 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6c 69  e looking for li
1c910 6e 65 73 20 74 68 61 74 20 62 65 67 69 6e 20 77  nes that begin w
1c911 69 74 68 20 22 63 61 73 65 20 4f 50 5f 22 2e 20  ith "case OP_". 
1c912 20 54 68 65 20 6f 70 63 6f 64 65 73 2e 68 20 66   The opcodes.h f
1c913 69 6c 65 73 0a 2a 2a 20 77 69 6c 6c 20 62 65 20  iles.** will be 
1c914 66 69 6c 6c 65 64 20 77 69 74 68 20 23 64 65 66  filled with #def
1c915 69 6e 65 73 20 74 68 61 74 20 67 69 76 65 20 75  ines that give u
1c916 6e 69 71 75 65 20 69 6e 74 65 67 65 72 20 76 61  nique integer va
1c917 6c 75 65 73 20 74 6f 20 65 61 63 68 0a 2a 2a 20  lues to each.** 
1c918 6f 70 63 6f 64 65 20 61 6e 64 20 74 68 65 20 6f  opcode and the o
1c919 70 63 6f 64 65 73 2e 63 20 66 69 6c 65 20 69 73  pcodes.c file is
1c91a 20 66 69 6c 6c 65 64 20 77 69 74 68 20 61 6e 20   filled with an 
1c91b 61 72 72 61 79 20 6f 66 20 73 74 72 69 6e 67 73  array of strings
1c91c 20 77 68 65 72 65 0a 2a 2a 20 65 61 63 68 20 73   where.** each s
1c91d 74 72 69 6e 67 20 69 73 20 74 68 65 20 73 79 6d  tring is the sym
1c91e 62 6f 6c 69 63 20 6e 61 6d 65 20 66 6f 72 20 74  bolic name for t
1c91f 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
1c920 20 6f 70 63 6f 64 65 2e 20 20 49 66 20 74 68 65   opcode.  If the
1c921 0a 2a 2a 20 63 61 73 65 20 73 74 61 74 65 6d 65  .** case stateme
1c922 6e 74 20 69 73 20 66 6f 6c 6c 6f 77 65 64 20 62  nt is followed b
1c923 79 20 61 20 63 6f 6d 6d 65 6e 74 20 6f 66 20 74  y a comment of t
1c924 68 65 20 66 6f 72 6d 20 22 2f 23 20 73 61 6d 65  he form "/# same
1c925 20 61 73 20 2e 2e 2e 20 23 2f 22 0a 2a 2a 20 74   as ... #/".** t
1c926 68 61 74 20 63 6f 6d 6d 65 6e 74 20 69 73 20 75  hat comment is u
1c927 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65  sed to determine
1c928 20 74 68 65 20 70 61 72 74 69 63 75 6c 61 72 20   the particular 
1c929 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 70 63  value of the opc
1c92a 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65 72  ode..**.** Other
1c92b 20 6b 65 79 77 6f 72 64 73 20 69 6e 20 74 68 65   keywords in the
1c92c 20 63 6f 6d 6d 65 6e 74 20 74 68 61 74 20 66 6f   comment that fo
1c92d 6c 6c 6f 77 73 20 65 61 63 68 20 63 61 73 65 20  llows each case 
1c92e 61 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63  are used to.** c
1c92f 6f 6e 73 74 72 75 63 74 20 74 68 65 20 4f 50 46  onstruct the OPF
1c930 4c 47 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 76  LG_INITIALIZER v
1c931 61 6c 75 65 20 74 68 61 74 20 69 6e 69 74 69 61  alue that initia
1c932 6c 69 7a 65 73 20 6f 70 63 6f 64 65 50 72 6f 70  lizes opcodeProp
1c933 65 72 74 79 5b 5d 2e 0a 2a 2a 20 4b 65 79 77 6f  erty[]..** Keywo
1c934 72 64 73 20 69 6e 63 6c 75 64 65 3a 20 69 6e 31  rds include: in1
1c935 2c 20 69 6e 32 2c 20 69 6e 33 2c 20 6f 75 74 32  , in2, in3, out2
1c936 5f 70 72 65 72 65 6c 65 61 73 65 2c 20 6f 75 74  _prerelease, out
1c937 32 2c 20 6f 75 74 33 2e 20 20 53 65 65 0a 2a 2a  2, out3.  See.**
1c938 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61   the mkopcodeh.a
1c939 77 6b 20 73 63 72 69 70 74 20 66 6f 72 20 61 64  wk script for ad
1c93a 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
1c93b 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44 6f 63 75  tion..**.** Docu
1c93c 6d 65 6e 74 61 74 69 6f 6e 20 61 62 6f 75 74 20  mentation about 
1c93d 56 44 42 45 20 6f 70 63 6f 64 65 73 20 69 73 20  VDBE opcodes is 
1c93e 67 65 6e 65 72 61 74 65 64 20 62 79 20 73 63 61  generated by sca
1c93f 6e 6e 69 6e 67 20 74 68 69 73 20 66 69 6c 65 0a  nning this file.
1c940 2a 2a 20 66 6f 72 20 6c 69 6e 65 73 20 6f 66 20  ** for lines of 
1c941 74 68 61 74 20 63 6f 6e 74 61 69 6e 20 22 4f 70  that contain "Op
1c942 63 6f 64 65 3a 22 2e 20 20 54 68 61 74 20 6c 69  code:".  That li
1c943 6e 65 20 61 6e 64 20 61 6c 6c 20 73 75 62 73 65  ne and all subse
1c944 71 75 65 6e 74 0a 2a 2a 20 63 6f 6d 6d 65 6e 74  quent.** comment
1c945 20 6c 69 6e 65 73 20 61 72 65 20 75 73 65 64 20   lines are used 
1c946 69 6e 20 74 68 65 20 67 65 6e 65 72 61 74 69 6f  in the generatio
1c947 6e 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 2e  n of the opcode.
1c948 68 74 6d 6c 20 64 6f 63 75 6d 65 6e 74 61 74 69  html documentati
1c949 6f 6e 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  on.** file..**.*
1c94a 2a 20 53 55 4d 4d 41 52 59 3a 0a 2a 2a 0a 2a 2a  * SUMMARY:.**.**
1c94b 20 20 20 20 20 46 6f 72 6d 61 74 74 69 6e 67 20       Formatting 
1c94c 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20  is important to 
1c94d 73 63 72 69 70 74 73 20 74 68 61 74 20 73 63 61  scripts that sca
1c94e 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 2a 2a 20  n this file..** 
1c94f 20 20 20 20 44 6f 20 6e 6f 74 20 64 65 76 69 61      Do not devia
1c950 74 65 20 66 72 6f 6d 20 74 68 65 20 66 6f 72 6d  te from the form
1c951 61 74 74 69 6e 67 20 73 74 79 6c 65 20 63 75 72  atting style cur
1c952 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a 2a  rently in use..*
1c953 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
1c954 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c955 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c956 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c957 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
1c958 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47 6f  ../* Opcode:  Go
1c959 74 6f 20 2a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  to * P2 * * *.**
1c95a 0a 2a 2a 20 41 6e 20 75 6e 63 6f 6e 64 69 74 69  .** An unconditi
1c95b 6f 6e 61 6c 20 6a 75 6d 70 20 74 6f 20 61 64 64  onal jump to add
1c95c 72 65 73 73 20 50 32 2e 0a 2a 2a 20 54 68 65 20  ress P2..** The 
1c95d 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  next instruction
1c95e 20 65 78 65 63 75 74 65 64 20 77 69 6c 6c 20 62   executed will b
1c95f 65 20 0a 2a 2a 20 74 68 65 20 6f 6e 65 20 61 74  e .** the one at
1c960 20 69 6e 64 65 78 20 50 32 20 66 72 6f 6d 20 74   index P2 from t
1c961 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 0a  he beginning of.
1c962 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a  ** the program..
1c963 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 74 6f 3a  */.case OP_Goto:
1c964 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f   {             /
1c965 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 43 48 45 43  * jump */.  CHEC
1c966 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54 3b  K_FOR_INTERRUPT;
1c967 0a 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20  .  pc = pOp->p2 
1c968 2d 20 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  - 1;.  break;.}.
1c969 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 47 6f 73  ./* Opcode:  Gos
1c96a 75 62 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  ub P1 P2 * * *.*
1c96b 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 63  *.** Write the c
1c96c 75 72 72 65 6e 74 20 61 64 64 72 65 73 73 20 6f  urrent address o
1c96d 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 31 0a  nto register P1.
1c96e 2a 2a 20 61 6e 64 20 74 68 65 6e 20 6a 75 6d 70  ** and then jump
1c96f 20 74 6f 20 61 64 64 72 65 73 73 20 50 32 2e 0a   to address P2..
1c970 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 6f 73 75 62  */.case OP_Gosub
1c971 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f  : {            /
1c972 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 61 73 73 65  * jump */.  asse
1c973 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b  rt( pOp->p1>0 );
1c974 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1c975 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  p1<=p->nMem );. 
1c976 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d   pIn1 = &p->aMem
1c977 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73  [pOp->p1];.  ass
1c978 65 72 74 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67  ert( (pIn1->flag
1c979 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d 3d 30 20  s & MEM_Dyn)==0 
1c97a 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73  );.  pIn1->flags
1c97b 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 70 49   = MEM_Int;.  pI
1c97c 6e 31 2d 3e 75 2e 69 20 3d 20 70 63 3b 0a 20 20  n1->u.i = pc;.  
1c97d 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1c97e 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20  Op->p1, pIn1);. 
1c97f 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1c980 31 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  1;.  break;.}../
1c981 2a 20 4f 70 63 6f 64 65 3a 20 20 52 65 74 75 72  * Opcode:  Retur
1c982 6e 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a  n P1 * * * *.**.
1c983 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 6e  ** Jump to the n
1c984 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ext instruction 
1c985 61 66 74 65 72 20 74 68 65 20 61 64 64 72 65 73  after the addres
1c986 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  s in register P1
1c987 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65 74  ..*/.case OP_Ret
1c988 75 72 6e 3a 20 7b 20 20 20 20 20 20 20 20 20 20  urn: {          
1c989 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73   /* in1 */.  ass
1c98a 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73  ert( pIn1->flags
1c98b 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20   & MEM_Int );.  
1c98c 70 63 20 3d 20 28 69 6e 74 29 70 49 6e 31 2d 3e  pc = (int)pIn1->
1c98d 75 2e 69 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  u.i;.  break;.}.
1c98e 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 59 69 65  ./* Opcode:  Yie
1c98f 6c 64 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a  ld P1 * * * *.**
1c990 0a 2a 2a 20 53 77 61 70 20 74 68 65 20 70 72 6f  .** Swap the pro
1c991 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 77 69 74  gram counter wit
1c992 68 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  h the value in r
1c993 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f 0a 63  egister P1..*/.c
1c994 61 73 65 20 4f 50 5f 59 69 65 6c 64 3a 20 7b 20  ase OP_Yield: { 
1c995 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e             /* in
1c996 31 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  1 */.#if 0  /* l
1c997 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1c998 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 61 20 2a  oved into u.aa *
1c999 2f 0a 20 20 69 6e 74 20 70 63 44 65 73 74 3b 0a  /.  int pcDest;.
1c99a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1c99b 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1c99c 69 6e 74 6f 20 75 2e 61 61 20 2a 2f 0a 20 20 61  into u.aa */.  a
1c99d 73 73 65 72 74 28 20 28 70 49 6e 31 2d 3e 66 6c  ssert( (pIn1->fl
1c99e 61 67 73 20 26 20 4d 45 4d 5f 44 79 6e 29 3d 3d  ags & MEM_Dyn)==
1c99f 30 20 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61  0 );.  pIn1->fla
1c9a0 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20  gs = MEM_Int;.  
1c9a1 75 2e 61 61 2e 70 63 44 65 73 74 20 3d 20 28 69  u.aa.pcDest = (i
1c9a2 6e 74 29 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20  nt)pIn1->u.i;.  
1c9a3 70 49 6e 31 2d 3e 75 2e 69 20 3d 20 70 63 3b 0a  pIn1->u.i = pc;.
1c9a4 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1c9a5 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b  (pOp->p1, pIn1);
1c9a6 0a 20 20 70 63 20 3d 20 75 2e 61 61 2e 70 63 44  .  pc = u.aa.pcD
1c9a7 65 73 74 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  est;.  break;.}.
1c9a8 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 20 48 61 6c  ./* Opcode:  Hal
1c9a9 74 49 66 4e 75 6c 6c 20 20 50 31 20 50 32 20 50  tIfNull  P1 P2 P
1c9aa 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65  3 P4 *.**.** Che
1c9ab 63 6b 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  ck the value in 
1c9ac 72 65 67 69 73 74 65 72 20 50 33 2e 20 20 49 66  register P3.  If
1c9ad 20 69 73 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e   is is NULL then
1c9ae 20 48 61 6c 74 20 75 73 69 6e 67 0a 2a 2a 20 70   Halt using.** p
1c9af 61 72 61 6d 65 74 65 72 20 50 31 2c 20 50 32 2c  arameter P1, P2,
1c9b0 20 61 6e 64 20 50 34 20 61 73 20 69 66 20 74 68   and P4 as if th
1c9b1 69 73 20 77 65 72 65 20 61 20 48 61 6c 74 20 69  is were a Halt i
1c9b2 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 49 66 20  nstruction.  If 
1c9b3 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 69 6e 20  the.** value in 
1c9b4 72 65 67 69 73 74 65 72 20 50 33 20 69 73 20 6e  register P3 is n
1c9b5 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68  ot NULL, then th
1c9b6 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20  is routine is a 
1c9b7 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f  no-op..*/.case O
1c9b8 50 5f 48 61 6c 74 49 66 4e 75 6c 6c 3a 20 7b 20  P_HaltIfNull: { 
1c9b9 20 20 20 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a 20       /* in3 */. 
1c9ba 20 69 66 28 20 28 70 49 6e 33 2d 3e 66 6c 61 67   if( (pIn3->flag
1c9bb 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30  s & MEM_Null)==0
1c9bc 20 29 20 62 72 65 61 6b 3b 0a 20 20 2f 2a 20 46   ) break;.  /* F
1c9bd 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f  all through into
1c9be 20 4f 50 5f 48 61 6c 74 20 2a 2f 0a 7d 0a 0a 2f   OP_Halt */.}../
1c9bf 2a 20 4f 70 63 6f 64 65 3a 20 20 48 61 6c 74 20  * Opcode:  Halt 
1c9c0 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a  P1 P2 * P4 *.**.
1c9c1 2a 2a 20 45 78 69 74 20 69 6d 6d 65 64 69 61 74  ** Exit immediat
1c9c2 65 6c 79 2e 20 20 41 6c 6c 20 6f 70 65 6e 20 63  ely.  All open c
1c9c3 75 72 73 6f 72 73 2c 20 65 74 63 20 61 72 65 20  ursors, etc are 
1c9c4 63 6c 6f 73 65 64 0a 2a 2a 20 61 75 74 6f 6d 61  closed.** automa
1c9c5 74 69 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50  tically..**.** P
1c9c6 31 20 69 73 20 74 68 65 20 72 65 73 75 6c 74 20  1 is the result 
1c9c7 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20 62 79  code returned by
1c9c8 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 2c   sqlite3_exec(),
1c9c9 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29   sqlite3_reset()
1c9ca 2c 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f  ,.** or sqlite3_
1c9cb 66 69 6e 61 6c 69 7a 65 28 29 2e 20 20 46 6f 72  finalize().  For
1c9cc 20 61 20 6e 6f 72 6d 61 6c 20 68 61 6c 74 2c 20   a normal halt, 
1c9cd 74 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20 53  this should be S
1c9ce 51 4c 49 54 45 5f 4f 4b 20 28 30 29 2e 0a 2a 2a  QLITE_OK (0)..**
1c9cf 20 46 6f 72 20 65 72 72 6f 72 73 2c 20 69 74 20   For errors, it 
1c9d0 63 61 6e 20 62 65 20 73 6f 6d 65 20 6f 74 68 65  can be some othe
1c9d1 72 20 76 61 6c 75 65 2e 20 20 49 66 20 50 31 21  r value.  If P1!
1c9d2 3d 30 20 74 68 65 6e 20 50 32 20 77 69 6c 6c 20  =0 then P2 will 
1c9d3 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 65  determine.** whe
1c9d4 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f 20 72  ther or not to r
1c9d5 6f 6c 6c 62 61 63 6b 20 74 68 65 20 63 75 72 72  ollback the curr
1c9d6 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ent transaction.
1c9d7 20 20 44 6f 20 6e 6f 74 20 72 6f 6c 6c 62 61 63    Do not rollbac
1c9d8 6b 0a 2a 2a 20 69 66 20 50 32 3d 3d 4f 45 5f 46  k.** if P2==OE_F
1c9d9 61 69 6c 2e 20 44 6f 20 74 68 65 20 72 6f 6c 6c  ail. Do the roll
1c9da 62 61 63 6b 20 69 66 20 50 32 3d 3d 4f 45 5f 52  back if P2==OE_R
1c9db 6f 6c 6c 62 61 63 6b 2e 20 20 49 66 20 50 32 3d  ollback.  If P2=
1c9dc 3d 4f 45 5f 41 62 6f 72 74 2c 0a 2a 2a 20 74 68  =OE_Abort,.** th
1c9dd 65 6e 20 62 61 63 6b 20 6f 75 74 20 61 6c 6c 20  en back out all 
1c9de 63 68 61 6e 67 65 73 20 74 68 61 74 20 68 61 76  changes that hav
1c9df 65 20 6f 63 63 75 72 72 65 64 20 64 75 72 69 6e  e occurred durin
1c9e0 67 20 74 68 69 73 20 65 78 65 63 75 74 69 6f 6e  g this execution
1c9e1 20 6f 66 20 74 68 65 0a 2a 2a 20 56 44 42 45 2c   of the.** VDBE,
1c9e2 20 62 75 74 20 64 6f 20 6e 6f 74 20 72 6f 6c 6c   but do not roll
1c9e3 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63  back the transac
1c9e4 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20  tion. .**.** If 
1c9e5 50 34 20 69 73 20 6e 6f 74 20 6e 75 6c 6c 20 74  P4 is not null t
1c9e6 68 65 6e 20 69 74 20 69 73 20 61 6e 20 65 72 72  hen it is an err
1c9e7 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e  or message strin
1c9e8 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69  g..**.** There i
1c9e9 73 20 61 6e 20 69 6d 70 6c 69 65 64 20 22 48 61  s an implied "Ha
1c9ea 6c 74 20 30 20 30 20 30 22 20 69 6e 73 74 72 75  lt 0 0 0" instru
1c9eb 63 74 69 6f 6e 20 69 6e 73 65 72 74 65 64 20 61  ction inserted a
1c9ec 74 20 74 68 65 20 76 65 72 79 20 65 6e 64 20 6f  t the very end o
1c9ed 66 0a 2a 2a 20 65 76 65 72 79 20 70 72 6f 67 72  f.** every progr
1c9ee 61 6d 2e 20 20 53 6f 20 61 20 6a 75 6d 70 20 70  am.  So a jump p
1c9ef 61 73 74 20 74 68 65 20 6c 61 73 74 20 69 6e 73  ast the last ins
1c9f0 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68 65 20  truction of the 
1c9f1 70 72 6f 67 72 61 6d 0a 2a 2a 20 69 73 20 74 68  program.** is th
1c9f2 65 20 73 61 6d 65 20 61 73 20 65 78 65 63 75 74  e same as execut
1c9f3 69 6e 67 20 48 61 6c 74 2e 0a 2a 2f 0a 63 61 73  ing Halt..*/.cas
1c9f4 65 20 4f 50 5f 48 61 6c 74 3a 20 7b 0a 20 20 69  e OP_Halt: {.  i
1c9f5 66 28 20 70 4f 70 2d 3e 70 31 3d 3d 53 51 4c 49  f( pOp->p1==SQLI
1c9f6 54 45 5f 4f 4b 20 26 26 20 70 2d 3e 70 46 72 61  TE_OK && p->pFra
1c9f7 6d 65 20 29 7b 0a 20 20 20 20 2f 2a 20 48 61 6c  me ){.    /* Hal
1c9f8 74 20 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61  t the sub-progra
1c9f9 6d 2e 20 52 65 74 75 72 6e 20 63 6f 6e 74 72 6f  m. Return contro
1c9fa 6c 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20  l to the parent 
1c9fb 66 72 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 56 64  frame. */.    Vd
1c9fc 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 20  beFrame *pFrame 
1c9fd 3d 20 70 2d 3e 70 46 72 61 6d 65 3b 0a 20 20 20  = p->pFrame;.   
1c9fe 20 70 2d 3e 70 46 72 61 6d 65 20 3d 20 70 46 72   p->pFrame = pFr
1c9ff 61 6d 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20  ame->pParent;.  
1ca00 20 20 70 2d 3e 6e 46 72 61 6d 65 2d 2d 3b 0a 20    p->nFrame--;. 
1ca01 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
1ca02 74 43 68 61 6e 67 65 73 28 64 62 2c 20 70 2d 3e  tChanges(db, p->
1ca03 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 70 63  nChange);.    pc
1ca04 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 46 72   = sqlite3VdbeFr
1ca05 61 6d 65 52 65 73 74 6f 72 65 28 70 46 72 61 6d  ameRestore(pFram
1ca06 65 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d  e);.    if( pOp-
1ca07 3e 70 32 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 29  >p2==OE_Ignore )
1ca08 7b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 73 74 72  {.      /* Instr
1ca09 75 63 74 69 6f 6e 20 70 63 20 69 73 20 74 68 65  uction pc is the
1ca0a 20 4f 50 5f 50 72 6f 67 72 61 6d 20 74 68 61 74   OP_Program that
1ca0b 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 73 75 62   invoked the sub
1ca0c 2d 70 72 6f 67 72 61 6d 20 0a 20 20 20 20 20 20  -program .      
1ca0d 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 62 65 69  ** currently bei
1ca0e 6e 67 20 68 61 6c 74 65 64 2e 20 49 66 20 74 68  ng halted. If th
1ca0f 65 20 70 32 20 69 6e 73 74 72 75 63 74 69 6f 6e  e p2 instruction
1ca10 20 6f 66 20 74 68 69 73 20 4f 50 5f 48 61 6c 74   of this OP_Halt
1ca11 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 72 75  .      ** instru
1ca12 63 74 69 6f 6e 20 69 73 20 73 65 74 20 74 6f 20  ction is set to 
1ca13 4f 45 5f 49 67 6e 6f 72 65 2c 20 74 68 65 6e 20  OE_Ignore, then 
1ca14 74 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20  the sub-program 
1ca15 69 73 20 74 68 72 6f 77 69 6e 67 0a 20 20 20 20  is throwing.    
1ca16 20 20 2a 2a 20 61 6e 20 49 47 4e 4f 52 45 20 65    ** an IGNORE e
1ca17 78 63 65 70 74 69 6f 6e 2e 20 49 6e 20 74 68 69  xception. In thi
1ca18 73 20 63 61 73 65 20 6a 75 6d 70 20 74 6f 20 74  s case jump to t
1ca19 68 65 20 61 64 64 72 65 73 73 20 73 70 65 63 69  he address speci
1ca1a 66 69 65 64 0a 20 20 20 20 20 20 2a 2a 20 61 73  fied.      ** as
1ca1b 20 74 68 65 20 70 32 20 6f 66 20 74 68 65 20 63   the p2 of the c
1ca1c 61 6c 6c 69 6e 67 20 4f 50 5f 50 72 6f 67 72 61  alling OP_Progra
1ca1d 6d 2e 20 20 2a 2f 0a 20 20 20 20 20 20 70 63 20  m.  */.      pc 
1ca1e 3d 20 70 2d 3e 61 4f 70 5b 70 63 5d 2e 70 32 2d  = p->aOp[pc].p2-
1ca1f 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62 72 65  1;.    }.    bre
1ca20 61 6b 3b 0a 20 20 7d 0a 0a 20 20 70 2d 3e 72 63  ak;.  }..  p->rc
1ca21 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 70 2d   = pOp->p1;.  p-
1ca22 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20 28  >errorAction = (
1ca23 75 38 29 70 4f 70 2d 3e 70 32 3b 0a 20 20 70 2d  u8)pOp->p2;.  p-
1ca24 3e 70 63 20 3d 20 70 63 3b 0a 20 20 69 66 28 20  >pc = pc;.  if( 
1ca25 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20  pOp->p4.z ){.   
1ca26 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
1ca27 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  g(&p->zErrMsg, d
1ca28 62 2c 20 22 25 73 22 2c 20 70 4f 70 2d 3e 70 34  b, "%s", pOp->p4
1ca29 2e 7a 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  .z);.  }.  rc = 
1ca2a 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28  sqlite3VdbeHalt(
1ca2b 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63  p);.  assert( rc
1ca2c 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 7c 7c  ==SQLITE_BUSY ||
1ca2d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc==SQLITE_OK |
1ca2e 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52 52  | rc==SQLITE_ERR
1ca2f 4f 52 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  OR );.  if( rc==
1ca30 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20  SQLITE_BUSY ){. 
1ca31 20 20 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20     p->rc = rc = 
1ca32 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
1ca33 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
1ca34 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1ca35 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45  || p->rc==SQLITE
1ca36 5f 43 4f 4e 53 54 52 41 49 4e 54 20 29 3b 0a 20  _CONSTRAINT );. 
1ca37 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53     assert( rc==S
1ca38 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e  QLITE_OK || db->
1ca39 6e 44 65 66 65 72 72 65 64 43 6f 6e 73 3e 30 20  nDeferredCons>0 
1ca3a 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 2d 3e 72  );.    rc = p->r
1ca3b 63 20 3f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c ? SQLITE_ERROR
1ca3c 20 3a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a   : SQLITE_DONE;.
1ca3d 20 20 7d 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f    }.  goto vdbe_
1ca3e 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70  return;.}../* Op
1ca3f 63 6f 64 65 3a 20 49 6e 74 65 67 65 72 20 50 31  code: Integer P1
1ca40 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1ca41 54 68 65 20 33 32 2d 62 69 74 20 69 6e 74 65 67  The 32-bit integ
1ca42 65 72 20 76 61 6c 75 65 20 50 31 20 69 73 20 77  er value P1 is w
1ca43 72 69 74 74 65 6e 20 69 6e 74 6f 20 72 65 67 69  ritten into regi
1ca44 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65  ster P2..*/.case
1ca45 20 4f 50 5f 49 6e 74 65 67 65 72 3a 20 7b 20 20   OP_Integer: {  
1ca46 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70         /* out2-p
1ca47 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 70  rerelease */.  p
1ca48 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Out->flags = MEM
1ca49 5f 49 6e 74 3b 0a 20 20 70 4f 75 74 2d 3e 75 2e  _Int;.  pOut->u.
1ca4a 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 62  i = pOp->p1;.  b
1ca4b 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1ca4c 64 65 3a 20 49 6e 74 36 34 20 2a 20 50 32 20 2a  de: Int64 * P2 *
1ca4d 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69   P4 *.**.** P4 i
1ca4e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
1ca4f 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20   64-bit integer 
1ca50 76 61 6c 75 65 2e 0a 2a 2a 20 57 72 69 74 65 20  value..** Write 
1ca51 74 68 61 74 20 76 61 6c 75 65 20 69 6e 74 6f 20  that value into 
1ca52 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a  register P2..*/.
1ca53 63 61 73 65 20 4f 50 5f 49 6e 74 36 34 3a 20 7b  case OP_Int64: {
1ca54 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75             /* ou
1ca55 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f  t2-prerelease */
1ca56 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1ca57 70 34 2e 70 49 36 34 21 3d 30 20 29 3b 0a 20 20  p4.pI64!=0 );.  
1ca58 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pOut->flags = ME
1ca59 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75 74 2d 3e 75  M_Int;.  pOut->u
1ca5a 2e 69 20 3d 20 2a 70 4f 70 2d 3e 70 34 2e 70 49  .i = *pOp->p4.pI
1ca5b 36 34 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  64;.  break;.}..
1ca5c 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 6c 20  /* Opcode: Real 
1ca5d 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a  * P2 * P4 *.**.*
1ca5e 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65  * P4 is a pointe
1ca5f 72 20 74 6f 20 61 20 36 34 2d 62 69 74 20 66 6c  r to a 64-bit fl
1ca60 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c  oating point val
1ca61 75 65 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 61  ue..** Write tha
1ca62 74 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65 67  t value into reg
1ca63 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73  ister P2..*/.cas
1ca64 65 20 4f 50 5f 52 65 61 6c 3a 20 7b 20 20 20 20  e OP_Real: {    
1ca65 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
1ca66 61 73 20 54 4b 5f 46 4c 4f 41 54 2c 20 6f 75 74  as TK_FLOAT, out
1ca67 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a  2-prerelease */.
1ca68 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20    pOut->flags = 
1ca69 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 61 73 73 65  MEM_Real;.  asse
1ca6a 72 74 28 20 21 73 71 6c 69 74 65 33 49 73 4e 61  rt( !sqlite3IsNa
1ca6b 4e 28 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c  N(*pOp->p4.pReal
1ca6c 29 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 72 20 3d  ) );.  pOut->r =
1ca6d 20 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c 3b   *pOp->p4.pReal;
1ca6e 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1ca6f 4f 70 63 6f 64 65 3a 20 53 74 72 69 6e 67 38 20  Opcode: String8 
1ca70 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a  * P2 * P4 *.**.*
1ca71 2a 20 50 34 20 70 6f 69 6e 74 73 20 74 6f 20 61  * P4 points to a
1ca72 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64 20   nul terminated 
1ca73 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 20 54 68  UTF-8 string. Th
1ca74 69 73 20 6f 70 63 6f 64 65 20 69 73 20 74 72 61  is opcode is tra
1ca75 6e 73 66 6f 72 6d 65 64 20 0a 2a 2a 20 69 6e 74  nsformed .** int
1ca76 6f 20 61 6e 20 4f 50 5f 53 74 72 69 6e 67 20 62  o an OP_String b
1ca77 65 66 6f 72 65 20 69 74 20 69 73 20 65 78 65 63  efore it is exec
1ca78 75 74 65 64 20 66 6f 72 20 74 68 65 20 66 69 72  uted for the fir
1ca79 73 74 20 74 69 6d 65 2e 0a 2a 2f 0a 63 61 73 65  st time..*/.case
1ca7a 20 4f 50 5f 53 74 72 69 6e 67 38 3a 20 7b 20 20   OP_String8: {  
1ca7b 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1ca7c 73 20 54 4b 5f 53 54 52 49 4e 47 2c 20 6f 75 74  s TK_STRING, out
1ca7d 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a  2-prerelease */.
1ca7e 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1ca7f 34 2e 7a 21 3d 30 20 29 3b 0a 20 20 70 4f 70 2d  4.z!=0 );.  pOp-
1ca80 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 53 74 72  >opcode = OP_Str
1ca81 69 6e 67 3b 0a 20 20 70 4f 70 2d 3e 70 31 20 3d  ing;.  pOp->p1 =
1ca82 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
1ca83 28 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 0a 23 69  (pOp->p4.z);..#i
1ca84 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1ca85 54 5f 55 54 46 31 36 0a 20 20 69 66 28 20 65 6e  T_UTF16.  if( en
1ca86 63 6f 64 69 6e 67 21 3d 53 51 4c 49 54 45 5f 55  coding!=SQLITE_U
1ca87 54 46 38 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  TF8 ){.    rc = 
1ca88 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1ca89 74 53 74 72 28 70 4f 75 74 2c 20 70 4f 70 2d 3e  tStr(pOut, pOp->
1ca8a 70 34 2e 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45  p4.z, -1, SQLITE
1ca8b 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54  _UTF8, SQLITE_ST
1ca8c 41 54 49 43 29 3b 0a 20 20 20 20 69 66 28 20 72  ATIC);.    if( r
1ca8d 63 3d 3d 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47  c==SQLITE_TOOBIG
1ca8e 20 29 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b   ) goto too_big;
1ca8f 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f  .    if( SQLITE_
1ca90 4f 4b 21 3d 73 71 6c 69 74 65 33 56 64 62 65 43  OK!=sqlite3VdbeC
1ca91 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4f  hangeEncoding(pO
1ca92 75 74 2c 20 65 6e 63 6f 64 69 6e 67 29 20 29 20  ut, encoding) ) 
1ca93 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20  goto no_mem;.   
1ca94 20 61 73 73 65 72 74 28 20 70 4f 75 74 2d 3e 7a   assert( pOut->z
1ca95 4d 61 6c 6c 6f 63 3d 3d 70 4f 75 74 2d 3e 7a 20  Malloc==pOut->z 
1ca96 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
1ca97 4f 75 74 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Out->flags & MEM
1ca98 5f 44 79 6e 20 29 3b 0a 20 20 20 20 70 4f 75 74  _Dyn );.    pOut
1ca99 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 20  ->zMalloc = 0;. 
1ca9a 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c     pOut->flags |
1ca9b 3d 20 4d 45 4d 5f 53 74 61 74 69 63 3b 0a 20 20  = MEM_Static;.  
1ca9c 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 26 3d    pOut->flags &=
1ca9d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 20 20 20 20 69   ~MEM_Dyn;.    i
1ca9e 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d  f( pOp->p4type==
1ca9f 50 34 5f 44 59 4e 41 4d 49 43 20 29 7b 0a 20 20  P4_DYNAMIC ){.  
1caa0 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1caa1 65 28 64 62 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29  e(db, pOp->p4.z)
1caa2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f 70 2d  ;.    }.    pOp-
1caa3 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 44 59 4e  >p4type = P4_DYN
1caa4 41 4d 49 43 3b 0a 20 20 20 20 70 4f 70 2d 3e 70  AMIC;.    pOp->p
1caa5 34 2e 7a 20 3d 20 70 4f 75 74 2d 3e 7a 3b 0a 20  4.z = pOut->z;. 
1caa6 20 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70 4f 75     pOp->p1 = pOu
1caa7 74 2d 3e 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  t->n;.  }.#endif
1caa8 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 3e 64  .  if( pOp->p1>d
1caa9 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
1caaa 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29  _LIMIT_LENGTH] )
1caab 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62  {.    goto too_b
1caac 69 67 3b 0a 20 20 7d 0a 20 20 2f 2a 20 46 61 6c  ig;.  }.  /* Fal
1caad 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65  l through to the
1caae 20 6e 65 78 74 20 63 61 73 65 2c 20 4f 50 5f 53   next case, OP_S
1caaf 74 72 69 6e 67 20 2a 2f 0a 7d 0a 20 20 0a 2f 2a  tring */.}.  ./*
1cab0 20 4f 70 63 6f 64 65 3a 20 53 74 72 69 6e 67 20   Opcode: String 
1cab1 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a  P1 P2 * P4 *.**.
1cab2 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20 76 61  ** The string va
1cab3 6c 75 65 20 50 34 20 6f 66 20 6c 65 6e 67 74 68  lue P4 of length
1cab4 20 50 31 20 28 62 79 74 65 73 29 20 69 73 20 73   P1 (bytes) is s
1cab5 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65  tored in registe
1cab6 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  r P2..*/.case OP
1cab7 5f 53 74 72 69 6e 67 3a 20 7b 20 20 20 20 20 20  _String: {      
1cab8 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72      /* out2-prer
1cab9 65 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65  elease */.  asse
1caba 72 74 28 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30  rt( pOp->p4.z!=0
1cabb 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67   );.  pOut->flag
1cabc 73 20 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f  s = MEM_Str|MEM_
1cabd 53 74 61 74 69 63 7c 4d 45 4d 5f 54 65 72 6d 3b  Static|MEM_Term;
1cabe 0a 20 20 70 4f 75 74 2d 3e 7a 20 3d 20 70 4f 70  .  pOut->z = pOp
1cabf 2d 3e 70 34 2e 7a 3b 0a 20 20 70 4f 75 74 2d 3e  ->p4.z;.  pOut->
1cac0 6e 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 70  n = pOp->p1;.  p
1cac1 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e 63 6f 64  Out->enc = encod
1cac2 69 6e 67 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41  ing;.  UPDATE_MA
1cac3 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29  X_BLOBSIZE(pOut)
1cac4 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1cac5 20 4f 70 63 6f 64 65 3a 20 4e 75 6c 6c 20 2a 20   Opcode: Null * 
1cac6 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57  P2 * * *.**.** W
1cac7 72 69 74 65 20 61 20 4e 55 4c 4c 20 69 6e 74 6f  rite a NULL into
1cac8 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f   register P2..*/
1cac9 0a 63 61 73 65 20 4f 50 5f 4e 75 6c 6c 3a 20 7b  .case OP_Null: {
1caca 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75             /* ou
1cacb 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f  t2-prerelease */
1cacc 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a  .  break;.}.../*
1cacd 20 4f 70 63 6f 64 65 3a 20 42 6c 6f 62 20 50 31   Opcode: Blob P1
1cace 20 50 32 20 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50   P2 * P4.**.** P
1cacf 34 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 62 6c  4 points to a bl
1cad0 6f 62 20 6f 66 20 64 61 74 61 20 50 31 20 62 79  ob of data P1 by
1cad1 74 65 73 20 6c 6f 6e 67 2e 20 20 53 74 6f 72 65  tes long.  Store
1cad2 20 74 68 69 73 0a 2a 2a 20 62 6c 6f 62 20 69 6e   this.** blob in
1cad3 20 72 65 67 69 73 74 65 72 20 50 32 2e 20 54 68   register P2. Th
1cad4 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69  is instruction i
1cad5 73 20 6e 6f 74 20 63 6f 64 65 64 20 64 69 72 65  s not coded dire
1cad6 63 74 6c 79 0a 2a 2a 20 62 79 20 74 68 65 20 63  ctly.** by the c
1cad7 6f 6d 70 69 6c 65 72 2e 20 49 6e 73 74 65 61 64  ompiler. Instead
1cad8 2c 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20 6c  , the compiler l
1cad9 61 79 65 72 20 73 70 65 63 69 66 69 65 73 0a 2a  ayer specifies.*
1cada 2a 20 61 6e 20 4f 50 5f 48 65 78 42 6c 6f 62 20  * an OP_HexBlob 
1cadb 6f 70 63 6f 64 65 2c 20 77 69 74 68 20 74 68 65  opcode, with the
1cadc 20 68 65 78 20 73 74 72 69 6e 67 20 72 65 70 72   hex string repr
1cadd 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 0a 2a 2a  esentation of.**
1cade 20 74 68 65 20 62 6c 6f 62 20 61 73 20 50 34 2e   the blob as P4.
1cadf 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20   This opcode is 
1cae0 74 72 61 6e 73 66 6f 72 6d 65 64 20 74 6f 20 61  transformed to a
1cae1 6e 20 4f 50 5f 42 6c 6f 62 0a 2a 2a 20 74 68 65  n OP_Blob.** the
1cae2 20 66 69 72 73 74 20 74 69 6d 65 20 69 74 20 69   first time it i
1cae3 73 20 65 78 65 63 75 74 65 64 2e 0a 2a 2f 0a 63  s executed..*/.c
1cae4 61 73 65 20 4f 50 5f 42 6c 6f 62 3a 20 7b 20 20  ase OP_Blob: {  
1cae5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1cae6 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65   out2-prerelease
1cae7 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f   */.  assert( pO
1cae8 70 2d 3e 70 31 20 3c 3d 20 53 51 4c 49 54 45 5f  p->p1 <= SQLITE_
1cae9 4d 41 58 5f 4c 45 4e 47 54 48 20 29 3b 0a 20 20  MAX_LENGTH );.  
1caea 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1caeb 74 53 74 72 28 70 4f 75 74 2c 20 70 4f 70 2d 3e  tStr(pOut, pOp->
1caec 70 34 2e 7a 2c 20 70 4f 70 2d 3e 70 31 2c 20 30  p4.z, pOp->p1, 0
1caed 2c 20 30 29 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e  , 0);.  pOut->en
1caee 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20  c = encoding;.  
1caef 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53  UPDATE_MAX_BLOBS
1caf0 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65  IZE(pOut);.  bre
1caf1 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1caf2 3a 20 56 61 72 69 61 62 6c 65 20 50 31 20 50 32  : Variable P1 P2
1caf3 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54   P3 P4 *.**.** T
1caf4 72 61 6e 73 66 65 72 20 74 68 65 20 76 61 6c 75  ransfer the valu
1caf5 65 73 20 6f 66 20 62 6f 75 6e 64 20 70 61 72 61  es of bound para
1caf6 6d 65 74 65 72 73 20 50 31 2e 2e 50 31 2b 50 33  meters P1..P1+P3
1caf7 2d 31 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  -1 into register
1caf8 73 0a 2a 2a 20 50 32 2e 2e 50 32 2b 50 33 2d 31  s.** P2..P2+P3-1
1caf9 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70  ..**.** If the p
1cafa 61 72 61 6d 65 74 65 72 20 69 73 20 6e 61 6d 65  arameter is name
1cafb 64 2c 20 74 68 65 6e 20 69 74 73 20 6e 61 6d 65  d, then its name
1cafc 20 61 70 70 65 61 72 73 20 69 6e 20 50 34 20 61   appears in P4 a
1cafd 6e 64 20 50 33 3d 3d 31 2e 0a 2a 2a 20 54 68 65  nd P3==1..** The
1cafe 20 50 34 20 76 61 6c 75 65 20 69 73 20 75 73 65   P4 value is use
1caff 64 20 62 79 20 73 71 6c 69 74 65 33 5f 62 69 6e  d by sqlite3_bin
1cb00 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65  d_parameter_name
1cb01 28 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56  ()..*/.case OP_V
1cb02 61 72 69 61 62 6c 65 3a 20 7b 0a 23 69 66 20 30  ariable: {.#if 0
1cb03 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1cb04 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1cb05 75 2e 61 62 20 2a 2f 0a 20 20 69 6e 74 20 70 31  u.ab */.  int p1
1cb06 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61  ;          /* Va
1cb07 72 69 61 62 6c 65 20 74 6f 20 63 6f 70 79 20 66  riable to copy f
1cb08 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 70 32 3b  rom */.  int p2;
1cb09 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
1cb0a 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20 74 6f  ister to copy to
1cb0b 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20   */.  int n;    
1cb0c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1cb0d 20 6f 66 20 76 61 6c 75 65 73 20 6c 65 66 74 20   of values left 
1cb0e 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 4d 65 6d  to copy */.  Mem
1cb0f 20 2a 70 56 61 72 3b 20 20 20 20 20 20 20 2f 2a   *pVar;       /*
1cb10 20 56 61 6c 75 65 20 62 65 69 6e 67 20 74 72 61   Value being tra
1cb11 6e 73 66 65 72 72 65 64 20 2a 2f 0a 23 65 6e 64  nsferred */.#end
1cb12 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1cb13 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1cb14 20 75 2e 61 62 20 2a 2f 0a 0a 20 20 75 2e 61 62   u.ab */..  u.ab
1cb15 2e 70 31 20 3d 20 70 4f 70 2d 3e 70 31 20 2d 20  .p1 = pOp->p1 - 
1cb16 31 3b 0a 20 20 75 2e 61 62 2e 70 32 20 3d 20 70  1;.  u.ab.p2 = p
1cb17 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 62 2e 6e  Op->p2;.  u.ab.n
1cb18 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73   = pOp->p3;.  as
1cb19 73 65 72 74 28 20 75 2e 61 62 2e 70 31 3e 3d 30  sert( u.ab.p1>=0
1cb1a 20 26 26 20 75 2e 61 62 2e 70 31 2b 75 2e 61 62   && u.ab.p1+u.ab
1cb1b 2e 6e 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b 0a 20  .n<=p->nVar );. 
1cb1c 20 61 73 73 65 72 74 28 20 75 2e 61 62 2e 70 32   assert( u.ab.p2
1cb1d 3e 3d 31 20 26 26 20 75 2e 61 62 2e 70 32 2b 75  >=1 && u.ab.p2+u
1cb1e 2e 61 62 2e 6e 2d 31 3c 3d 70 2d 3e 6e 4d 65 6d  .ab.n-1<=p->nMem
1cb1f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
1cb20 70 2d 3e 70 34 2e 7a 3d 3d 30 20 7c 7c 20 70 4f  p->p4.z==0 || pO
1cb21 70 2d 3e 70 33 3d 3d 31 20 7c 7c 20 70 4f 70 2d  p->p3==1 || pOp-
1cb22 3e 70 33 3d 3d 30 20 29 3b 0a 0a 20 20 77 68 69  >p3==0 );..  whi
1cb23 6c 65 28 20 75 2e 61 62 2e 6e 2d 2d 20 3e 20 30  le( u.ab.n-- > 0
1cb24 20 29 7b 0a 20 20 20 20 75 2e 61 62 2e 70 56 61   ){.    u.ab.pVa
1cb25 72 20 3d 20 26 70 2d 3e 61 56 61 72 5b 75 2e 61  r = &p->aVar[u.a
1cb26 62 2e 70 31 2b 2b 5d 3b 0a 20 20 20 20 69 66 28  b.p1++];.    if(
1cb27 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54   sqlite3VdbeMemT
1cb28 6f 6f 42 69 67 28 75 2e 61 62 2e 70 56 61 72 29  ooBig(u.ab.pVar)
1cb29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74   ){.      goto t
1cb2a 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20  oo_big;.    }.  
1cb2b 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65    pOut = &p->aMe
1cb2c 6d 5b 75 2e 61 62 2e 70 32 2b 2b 5d 3b 0a 20 20  m[u.ab.p2++];.  
1cb2d 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1cb2e 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28  ReleaseExternal(
1cb2f 70 4f 75 74 29 3b 0a 20 20 20 20 70 4f 75 74 2d  pOut);.    pOut-
1cb30 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c  >flags = MEM_Nul
1cb31 6c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  l;.    sqlite3Vd
1cb32 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79  beMemShallowCopy
1cb33 28 70 4f 75 74 2c 20 75 2e 61 62 2e 70 56 61 72  (pOut, u.ab.pVar
1cb34 2c 20 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20  , MEM_Static);. 
1cb35 20 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c     UPDATE_MAX_BL
1cb36 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20  OBSIZE(pOut);.  
1cb37 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1cb38 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 20 50 31   Opcode: Move P1
1cb39 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1cb3a 20 4d 6f 76 65 20 74 68 65 20 76 61 6c 75 65 73   Move the values
1cb3b 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e   in register P1.
1cb3c 2e 50 31 2b 50 33 2d 31 20 6f 76 65 72 20 69 6e  .P1+P3-1 over in
1cb3d 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20  to.** registers 
1cb3e 50 32 2e 2e 50 32 2b 50 33 2d 31 2e 20 20 52 65  P2..P2+P3-1.  Re
1cb3f 67 69 73 74 65 72 73 20 50 31 2e 2e 50 31 2b 50  gisters P1..P1+P
1cb40 31 2d 31 20 61 72 65 0a 2a 2a 20 6c 65 66 74 20  1-1 are.** left 
1cb41 68 6f 6c 64 69 6e 67 20 61 20 4e 55 4c 4c 2e 20  holding a NULL. 
1cb42 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20   It is an error 
1cb43 66 6f 72 20 72 65 67 69 73 74 65 72 20 72 61 6e  for register ran
1cb44 67 65 73 0a 2a 2a 20 50 31 2e 2e 50 31 2b 50 33  ges.** P1..P1+P3
1cb45 2d 31 20 61 6e 64 20 50 32 2e 2e 50 32 2b 50 33  -1 and P2..P2+P3
1cb46 2d 31 20 74 6f 20 6f 76 65 72 6c 61 70 2e 0a 2a  -1 to overlap..*
1cb47 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 3a 20  /.case OP_Move: 
1cb48 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  {.#if 0  /* loca
1cb49 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1cb4a 64 20 69 6e 74 6f 20 75 2e 61 63 20 2a 2f 0a 20  d into u.ac */. 
1cb4b 20 63 68 61 72 20 2a 7a 4d 61 6c 6c 6f 63 3b 20   char *zMalloc; 
1cb4c 20 20 2f 2a 20 48 6f 6c 64 69 6e 67 20 76 61 72    /* Holding var
1cb4d 69 61 62 6c 65 20 66 6f 72 20 61 6c 6c 6f 63 61  iable for alloca
1cb4e 74 65 64 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20  ted memory */.  
1cb4f 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20  int n;          
1cb50 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 65   /* Number of re
1cb51 67 69 73 74 65 72 73 20 6c 65 66 74 20 74 6f 20  gisters left to 
1cb52 63 6f 70 79 20 2a 2f 0a 20 20 69 6e 74 20 70 31  copy */.  int p1
1cb53 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  ;          /* Re
1cb54 67 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20 66  gister to copy f
1cb55 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 70 32 3b  rom */.  int p2;
1cb56 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
1cb57 69 73 74 65 72 20 74 6f 20 63 6f 70 79 20 74 6f  ister to copy to
1cb58 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f   */.#endif /* lo
1cb59 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1cb5a 76 65 64 20 69 6e 74 6f 20 75 2e 61 63 20 2a 2f  ved into u.ac */
1cb5b 0a 0a 20 20 75 2e 61 63 2e 6e 20 3d 20 70 4f 70  ..  u.ac.n = pOp
1cb5c 2d 3e 70 33 3b 0a 20 20 75 2e 61 63 2e 70 31 20  ->p3;.  u.ac.p1 
1cb5d 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61  = pOp->p1;.  u.a
1cb5e 63 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a  c.p2 = pOp->p2;.
1cb5f 20 20 61 73 73 65 72 74 28 20 75 2e 61 63 2e 6e    assert( u.ac.n
1cb60 3e 30 20 26 26 20 75 2e 61 63 2e 70 31 3e 30 20  >0 && u.ac.p1>0 
1cb61 26 26 20 75 2e 61 63 2e 70 32 3e 30 20 29 3b 0a  && u.ac.p2>0 );.
1cb62 20 20 61 73 73 65 72 74 28 20 75 2e 61 63 2e 70    assert( u.ac.p
1cb63 31 2b 75 2e 61 63 2e 6e 3c 3d 75 2e 61 63 2e 70  1+u.ac.n<=u.ac.p
1cb64 32 20 7c 7c 20 75 2e 61 63 2e 70 32 2b 75 2e 61  2 || u.ac.p2+u.a
1cb65 63 2e 6e 3c 3d 75 2e 61 63 2e 70 31 20 29 3b 0a  c.n<=u.ac.p1 );.
1cb66 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d  .  pIn1 = &p->aM
1cb67 65 6d 5b 75 2e 61 63 2e 70 31 5d 3b 0a 20 20 70  em[u.ac.p1];.  p
1cb68 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75  Out = &p->aMem[u
1cb69 2e 61 63 2e 70 32 5d 3b 0a 20 20 77 68 69 6c 65  .ac.p2];.  while
1cb6a 28 20 75 2e 61 63 2e 6e 2d 2d 20 29 7b 0a 20 20  ( u.ac.n-- ){.  
1cb6b 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 3c 3d    assert( pOut<=
1cb6c 26 70 2d 3e 61 4d 65 6d 5b 70 2d 3e 6e 4d 65 6d  &p->aMem[p->nMem
1cb6d 5d 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ] );.    assert(
1cb6e 20 70 49 6e 31 3c 3d 26 70 2d 3e 61 4d 65 6d 5b   pIn1<=&p->aMem[
1cb6f 70 2d 3e 6e 4d 65 6d 5d 20 29 3b 0a 20 20 20 20  p->nMem] );.    
1cb70 75 2e 61 63 2e 7a 4d 61 6c 6c 6f 63 20 3d 20 70  u.ac.zMalloc = p
1cb71 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20  Out->zMalloc;.  
1cb72 20 20 70 4f 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20    pOut->zMalloc 
1cb73 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
1cb74 56 64 62 65 4d 65 6d 4d 6f 76 65 28 70 4f 75 74  VdbeMemMove(pOut
1cb75 2c 20 70 49 6e 31 29 3b 0a 20 20 20 20 70 49 6e  , pIn1);.    pIn
1cb76 31 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 75 2e 61  1->zMalloc = u.a
1cb77 63 2e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20 52  c.zMalloc;.    R
1cb78 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 75 2e  EGISTER_TRACE(u.
1cb79 61 63 2e 70 32 2b 2b 2c 20 70 4f 75 74 29 3b 0a  ac.p2++, pOut);.
1cb7a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a 20 20 20 20      pIn1++;.    
1cb7b 70 4f 75 74 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72  pOut++;.  }.  br
1cb7c 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1cb7d 65 3a 20 43 6f 70 79 20 50 31 20 50 32 20 2a 20  e: Copy P1 P2 * 
1cb7e 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61 6b 65 20 61  * *.**.** Make a
1cb7f 20 63 6f 70 79 20 6f 66 20 72 65 67 69 73 74 65   copy of registe
1cb80 72 20 50 31 20 69 6e 74 6f 20 72 65 67 69 73 74  r P1 into regist
1cb81 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  er P2..**.** Thi
1cb82 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6d 61  s instruction ma
1cb83 6b 65 73 20 61 20 64 65 65 70 20 63 6f 70 79 20  kes a deep copy 
1cb84 6f 66 20 74 68 65 20 76 61 6c 75 65 2e 20 20 41  of the value.  A
1cb85 20 64 75 70 6c 69 63 61 74 65 0a 2a 2a 20 69 73   duplicate.** is
1cb86 20 6d 61 64 65 20 6f 66 20 61 6e 79 20 73 74 72   made of any str
1cb87 69 6e 67 20 6f 72 20 62 6c 6f 62 20 63 6f 6e 73  ing or blob cons
1cb88 74 61 6e 74 2e 20 20 53 65 65 20 61 6c 73 6f 20  tant.  See also 
1cb89 4f 50 5f 53 43 6f 70 79 2e 0a 2a 2f 0a 63 61 73  OP_SCopy..*/.cas
1cb8a 65 20 4f 50 5f 43 6f 70 79 3a 20 7b 20 20 20 20  e OP_Copy: {    
1cb8b 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20           /* in1 
1cb8c 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  */.  assert( pOp
1cb8d 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 61 73 73 65  ->p2>0 );.  asse
1cb8e 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e  rt( pOp->p2<=p->
1cb8f 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 20 3d  nMem );.  pOut =
1cb90 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1cb91 32 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  2];.  assert( pO
1cb92 75 74 21 3d 70 49 6e 31 20 29 3b 0a 20 20 73 71  ut!=pIn1 );.  sq
1cb93 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c  lite3VdbeMemShal
1cb94 6c 6f 77 43 6f 70 79 28 70 4f 75 74 2c 20 70 49  lowCopy(pOut, pI
1cb95 6e 31 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a  n1, MEM_Ephem);.
1cb96 20 20 44 65 65 70 68 65 6d 65 72 61 6c 69 7a 65    Deephemeralize
1cb97 28 70 4f 75 74 29 3b 0a 20 20 52 45 47 49 53 54  (pOut);.  REGIST
1cb98 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32  ER_TRACE(pOp->p2
1cb99 2c 20 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b  , pOut);.  break
1cb9a 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1cb9b 53 43 6f 70 79 20 50 31 20 50 32 20 2a 20 2a 20  SCopy P1 P2 * * 
1cb9c 2a 0a 2a 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 73  *.**.** Make a s
1cb9d 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f 66 20 72  hallow copy of r
1cb9e 65 67 69 73 74 65 72 20 50 31 20 69 6e 74 6f 20  egister P1 into 
1cb9f 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a  register P2..**.
1cba0 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74  ** This instruct
1cba1 69 6f 6e 20 6d 61 6b 65 73 20 61 20 73 68 61 6c  ion makes a shal
1cba2 6c 6f 77 20 63 6f 70 79 20 6f 66 20 74 68 65 20  low copy of the 
1cba3 76 61 6c 75 65 2e 20 20 49 66 20 74 68 65 20 76  value.  If the v
1cba4 61 6c 75 65 0a 2a 2a 20 69 73 20 61 20 73 74 72  alue.** is a str
1cba5 69 6e 67 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65  ing or blob, the
1cba6 6e 20 74 68 65 20 63 6f 70 79 20 69 73 20 6f 6e  n the copy is on
1cba7 6c 79 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ly a pointer to 
1cba8 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20  the.** original 
1cba9 61 6e 64 20 68 65 6e 63 65 20 69 66 20 74 68 65  and hence if the
1cbaa 20 6f 72 69 67 69 6e 61 6c 20 63 68 61 6e 67 65   original change
1cbab 73 20 73 6f 20 77 69 6c 6c 20 74 68 65 20 63 6f  s so will the co
1cbac 70 79 2e 0a 2a 2a 20 57 6f 72 73 65 2c 20 69 66  py..** Worse, if
1cbad 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 69 73   the original is
1cbae 20 64 65 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68   deallocated, th
1cbaf 65 20 63 6f 70 79 20 62 65 63 6f 6d 65 73 20 69  e copy becomes i
1cbb0 6e 76 61 6c 69 64 2e 0a 2a 2a 20 54 68 75 73 20  nvalid..** Thus 
1cbb1 74 68 65 20 70 72 6f 67 72 61 6d 20 6d 75 73 74  the program must
1cbb2 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20   guarantee that 
1cbb3 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 77 69 6c  the original wil
1cbb4 6c 20 6e 6f 74 20 63 68 61 6e 67 65 0a 2a 2a 20  l not change.** 
1cbb5 64 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65 74  during the lifet
1cbb6 69 6d 65 20 6f 66 20 74 68 65 20 63 6f 70 79 2e  ime of the copy.
1cbb7 20 20 55 73 65 20 4f 50 5f 43 6f 70 79 20 74 6f    Use OP_Copy to
1cbb8 20 6d 61 6b 65 20 61 20 63 6f 6d 70 6c 65 74 65   make a complete
1cbb9 0a 2a 2a 20 63 6f 70 79 2e 0a 2a 2f 0a 63 61 73  .** copy..*/.cas
1cbba 65 20 4f 50 5f 53 43 6f 70 79 3a 20 7b 20 20 20  e OP_SCopy: {   
1cbbb 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 20           /* in1 
1cbbc 2a 2f 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52  */.  REGISTER_TR
1cbbd 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49 6e  ACE(pOp->p1, pIn
1cbbe 31 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  1);.  assert( pO
1cbbf 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 61 73 73  p->p2>0 );.  ass
1cbc0 65 72 74 28 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d  ert( pOp->p2<=p-
1cbc1 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 20  >nMem );.  pOut 
1cbc2 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
1cbc3 70 32 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  p2];.  assert( p
1cbc4 4f 75 74 21 3d 70 49 6e 31 20 29 3b 0a 20 20 73  Out!=pIn1 );.  s
1cbc5 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61  qlite3VdbeMemSha
1cbc6 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 2c 20 70  llowCopy(pOut, p
1cbc7 49 6e 31 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b  In1, MEM_Ephem);
1cbc8 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43  .  REGISTER_TRAC
1cbc9 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f 75 74 29  E(pOp->p2, pOut)
1cbca 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1cbcb 20 4f 70 63 6f 64 65 3a 20 52 65 73 75 6c 74 52   Opcode: ResultR
1cbcc 6f 77 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  ow P1 P2 * * *.*
1cbcd 2a 0a 2a 2a 20 54 68 65 20 72 65 67 69 73 74 65  *.** The registe
1cbce 72 73 20 50 31 20 74 68 72 6f 75 67 68 20 50 31  rs P1 through P1
1cbcf 2b 50 32 2d 31 20 63 6f 6e 74 61 69 6e 20 61 20  +P2-1 contain a 
1cbd0 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 0a 2a 2a  single row of.**
1cbd1 20 72 65 73 75 6c 74 73 2e 20 54 68 69 73 20 6f   results. This o
1cbd2 70 63 6f 64 65 20 63 61 75 73 65 73 20 74 68 65  pcode causes the
1cbd3 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20   sqlite3_step() 
1cbd4 63 61 6c 6c 20 74 6f 20 74 65 72 6d 69 6e 61 74  call to terminat
1cbd5 65 0a 2a 2a 20 77 69 74 68 20 61 6e 20 53 51 4c  e.** with an SQL
1cbd6 49 54 45 5f 52 4f 57 20 72 65 74 75 72 6e 20 63  ITE_ROW return c
1cbd7 6f 64 65 20 61 6e 64 20 69 74 20 73 65 74 73 20  ode and it sets 
1cbd8 75 70 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73  up the sqlite3_s
1cbd9 74 6d 74 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  tmt.** structure
1cbda 20 74 6f 20 70 72 6f 76 69 64 65 20 61 63 63 65   to provide acce
1cbdb 73 73 20 74 6f 20 74 68 65 20 74 6f 70 20 50 31  ss to the top P1
1cbdc 20 76 61 6c 75 65 73 20 61 73 20 74 68 65 20 72   values as the r
1cbdd 65 73 75 6c 74 0a 2a 2a 20 72 6f 77 2e 0a 2a 2f  esult.** row..*/
1cbde 0a 63 61 73 65 20 4f 50 5f 52 65 73 75 6c 74 52  .case OP_ResultR
1cbdf 6f 77 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20  ow: {.#if 0  /* 
1cbe0 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1cbe1 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 64 20  moved into u.ad 
1cbe2 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a  */.  Mem *pMem;.
1cbe3 20 20 69 6e 74 20 69 3b 0a 23 65 6e 64 69 66 20    int i;.#endif 
1cbe4 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1cbe5 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1cbe6 61 64 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  ad */.  assert( 
1cbe7 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3d 3d 70  p->nResColumn==p
1cbe8 4f 70 2d 3e 70 32 20 29 3b 0a 20 20 61 73 73 65  Op->p2 );.  asse
1cbe9 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b  rt( pOp->p1>0 );
1cbea 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1cbeb 70 31 2b 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e  p1+pOp->p2<=p->n
1cbec 4d 65 6d 2b 31 20 29 3b 0a 0a 20 20 2f 2a 20 49  Mem+1 );..  /* I
1cbed 66 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e 74  f this statement
1cbee 20 68 61 73 20 76 69 6f 6c 61 74 65 64 20 69 6d   has violated im
1cbef 6d 65 64 69 61 74 65 20 66 6f 72 65 69 67 6e 20  mediate foreign 
1cbf0 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c  key constraints,
1cbf1 20 64 6f 0a 20 20 2a 2a 20 6e 6f 74 20 72 65 74   do.  ** not ret
1cbf2 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
1cbf3 66 20 72 6f 77 73 20 6d 6f 64 69 66 69 65 64 2e  f rows modified.
1cbf4 20 41 6e 64 20 64 6f 20 6e 6f 74 20 52 45 4c 45   And do not RELE
1cbf5 41 53 45 20 74 68 65 20 73 74 61 74 65 6d 65 6e  ASE the statemen
1cbf6 74 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69  t.  ** transacti
1cbf7 6f 6e 2e 20 49 74 20 6e 65 65 64 73 20 74 6f 20  on. It needs to 
1cbf8 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20  be rolled back. 
1cbf9 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45   */.  if( SQLITE
1cbfa 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK!=(rc = sqlit
1cbfb 65 33 56 64 62 65 43 68 65 63 6b 46 6b 28 70 2c  e3VdbeCheckFk(p,
1cbfc 20 30 29 29 20 29 7b 0a 20 20 20 20 61 73 73 65   0)) ){.    asse
1cbfd 72 74 28 20 64 62 2d 3e 66 6c 61 67 73 26 53 51  rt( db->flags&SQ
1cbfe 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29  LITE_CountRows )
1cbff 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  ;.    assert( p-
1cc00 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c  >usesStmtJournal
1cc01 20 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20   );.    break;. 
1cc02 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
1cc03 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73  SQLITE_CountRows
1cc04 20 66 6c 61 67 20 69 73 20 73 65 74 20 69 6e 20   flag is set in 
1cc05 73 71 6c 69 74 65 33 2e 66 6c 61 67 73 20 6d 61  sqlite3.flags ma
1cc06 73 6b 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 44 4d  sk, then.  ** DM
1cc07 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 76  L statements inv
1cc08 6f 6b 65 20 74 68 69 73 20 6f 70 63 6f 64 65 20  oke this opcode 
1cc09 74 6f 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75  to return the nu
1cc0a 6d 62 65 72 20 6f 66 20 72 6f 77 73 0a 20 20 2a  mber of rows.  *
1cc0b 2a 20 6d 6f 64 69 66 69 65 64 20 74 6f 20 74 68  * modified to th
1cc0c 65 20 75 73 65 72 2e 20 54 68 69 73 20 69 73 20  e user. This is 
1cc0d 74 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 68 61  the only way tha
1cc0e 74 20 61 20 56 4d 20 74 68 61 74 0a 20 20 2a 2a  t a VM that.  **
1cc0f 20 6f 70 65 6e 73 20 61 20 73 74 61 74 65 6d 65   opens a stateme
1cc10 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d  nt transaction m
1cc11 61 79 20 69 6e 76 6f 6b 65 20 74 68 69 73 20 6f  ay invoke this o
1cc12 70 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  pcode..  **.  **
1cc13 20 49 6e 20 63 61 73 65 20 74 68 69 73 20 69 73   In case this is
1cc14 20 73 75 63 68 20 61 20 73 74 61 74 65 6d 65 6e   such a statemen
1cc15 74 2c 20 63 6c 6f 73 65 20 61 6e 79 20 73 74 61  t, close any sta
1cc16 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
1cc17 6f 6e 0a 20 20 2a 2a 20 6f 70 65 6e 65 64 20 62  on.  ** opened b
1cc18 79 20 74 68 69 73 20 56 4d 20 62 65 66 6f 72 65  y this VM before
1cc19 20 72 65 74 75 72 6e 69 6e 67 20 63 6f 6e 74 72   returning contr
1cc1a 6f 6c 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20  ol to the user. 
1cc1b 54 68 69 73 20 69 73 20 74 6f 0a 20 20 2a 2a 20  This is to.  ** 
1cc1c 65 6e 73 75 72 65 20 74 68 61 74 20 73 74 61 74  ensure that stat
1cc1d 65 6d 65 6e 74 2d 74 72 61 6e 73 61 63 74 69 6f  ement-transactio
1cc1e 6e 73 20 61 72 65 20 61 6c 77 61 79 73 20 6e 65  ns are always ne
1cc1f 73 74 65 64 2c 20 6e 6f 74 20 6f 76 65 72 6c 61  sted, not overla
1cc20 70 70 69 6e 67 2e 0a 20 20 2a 2a 20 49 66 20 74  pping..  ** If t
1cc21 68 65 20 6f 70 65 6e 20 73 74 61 74 65 6d 65 6e  he open statemen
1cc22 74 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  t-transaction is
1cc23 20 6e 6f 74 20 63 6c 6f 73 65 64 20 68 65 72 65   not closed here
1cc24 2c 20 74 68 65 6e 20 74 68 65 20 75 73 65 72 0a  , then the user.
1cc25 20 20 2a 2a 20 6d 61 79 20 73 74 65 70 20 61 6e    ** may step an
1cc26 6f 74 68 65 72 20 56 4d 20 74 68 61 74 20 6f 70  other VM that op
1cc27 65 6e 73 20 69 74 73 20 6f 77 6e 20 73 74 61 74  ens its own stat
1cc28 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
1cc29 6e 2e 20 54 68 69 73 0a 20 20 2a 2a 20 6d 61 79  n. This.  ** may
1cc2a 20 6c 65 61 64 20 74 6f 20 6f 76 65 72 6c 61 70   lead to overlap
1cc2b 70 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 74  ping statement t
1cc2c 72 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 20 20 2a  ransactions..  *
1cc2d 2a 0a 20 20 2a 2a 20 54 68 65 20 73 74 61 74 65  *.  ** The state
1cc2e 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  ment transaction
1cc2f 20 69 73 20 6e 65 76 65 72 20 61 20 74 6f 70 2d   is never a top-
1cc30 6c 65 76 65 6c 20 74 72 61 6e 73 61 63 74 69 6f  level transactio
1cc31 6e 2e 20 20 48 65 6e 63 65 0a 20 20 2a 2a 20 74  n.  Hence.  ** t
1cc32 68 65 20 52 45 4c 45 41 53 45 20 63 61 6c 6c 20  he RELEASE call 
1cc33 62 65 6c 6f 77 20 63 61 6e 20 6e 65 76 65 72 20  below can never 
1cc34 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  fail..  */.  ass
1cc35 65 72 74 28 20 70 2d 3e 69 53 74 61 74 65 6d 65  ert( p->iStateme
1cc36 6e 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 66 6c 61  nt==0 || db->fla
1cc37 67 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52  gs&SQLITE_CountR
1cc38 6f 77 73 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  ows );.  rc = sq
1cc39 6c 69 74 65 33 56 64 62 65 43 6c 6f 73 65 53 74  lite3VdbeCloseSt
1cc3a 61 74 65 6d 65 6e 74 28 70 2c 20 53 41 56 45 50  atement(p, SAVEP
1cc3b 4f 49 4e 54 5f 52 45 4c 45 41 53 45 29 3b 0a 20  OINT_RELEASE);. 
1cc3c 20 69 66 28 20 4e 45 56 45 52 28 72 63 21 3d 53   if( NEVER(rc!=S
1cc3d 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a 20 20 20  QLITE_OK) ){.   
1cc3e 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f   break;.  }..  /
1cc3f 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c  * Invalidate all
1cc40 20 65 70 68 65 6d 65 72 61 6c 20 63 75 72 73 6f   ephemeral curso
1cc41 72 20 72 6f 77 20 63 61 63 68 65 73 20 2a 2f 0a  r row caches */.
1cc42 20 20 70 2d 3e 63 61 63 68 65 43 74 72 20 3d 20    p->cacheCtr = 
1cc43 28 70 2d 3e 63 61 63 68 65 43 74 72 20 2b 20 32  (p->cacheCtr + 2
1cc44 29 7c 31 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  )|1;..  /* Make 
1cc45 73 75 72 65 20 74 68 65 20 72 65 73 75 6c 74 73  sure the results
1cc46 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   of the current 
1cc47 72 6f 77 20 61 72 65 20 5c 30 30 30 20 74 65 72  row are \000 ter
1cc48 6d 69 6e 61 74 65 64 0a 20 20 2a 2a 20 61 6e 64  minated.  ** and
1cc49 20 68 61 76 65 20 61 6e 20 61 73 73 69 67 6e 65   have an assigne
1cc4a 64 20 74 79 70 65 2e 20 20 54 68 65 20 72 65 73  d type.  The res
1cc4b 75 6c 74 73 20 61 72 65 20 64 65 2d 65 70 68 65  ults are de-ephe
1cc4c 6d 65 72 61 6c 69 7a 65 64 20 61 73 0a 20 20 2a  meralized as.  *
1cc4d 2a 20 61 73 20 73 69 64 65 20 65 66 66 65 63 74  * as side effect
1cc4e 2e 0a 20 20 2a 2f 0a 20 20 75 2e 61 64 2e 70 4d  ..  */.  u.ad.pM
1cc4f 65 6d 20 3d 20 70 2d 3e 70 52 65 73 75 6c 74 53  em = p->pResultS
1cc50 65 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  et = &p->aMem[pO
1cc51 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 75 2e  p->p1];.  for(u.
1cc52 61 64 2e 69 3d 30 3b 20 75 2e 61 64 2e 69 3c 70  ad.i=0; u.ad.i<p
1cc53 4f 70 2d 3e 70 32 3b 20 75 2e 61 64 2e 69 2b 2b  Op->p2; u.ad.i++
1cc54 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1cc55 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61 74  beMemNulTerminat
1cc56 65 28 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e 61  e(&u.ad.pMem[u.a
1cc57 64 2e 69 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74  d.i]);.    sqlit
1cc58 65 33 56 64 62 65 4d 65 6d 53 74 6f 72 65 54 79  e3VdbeMemStoreTy
1cc59 70 65 28 26 75 2e 61 64 2e 70 4d 65 6d 5b 75 2e  pe(&u.ad.pMem[u.
1cc5a 61 64 2e 69 5d 29 3b 0a 20 20 20 20 52 45 47 49  ad.i]);.    REGI
1cc5b 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
1cc5c 70 31 2b 75 2e 61 64 2e 69 2c 20 26 75 2e 61 64  p1+u.ad.i, &u.ad
1cc5d 2e 70 4d 65 6d 5b 75 2e 61 64 2e 69 5d 29 3b 0a  .pMem[u.ad.i]);.
1cc5e 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61    }.  if( db->ma
1cc5f 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74  llocFailed ) got
1cc60 6f 20 6e 6f 5f 6d 65 6d 3b 0a 0a 20 20 2f 2a 20  o no_mem;..  /* 
1cc61 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 4f  Return SQLITE_RO
1cc62 57 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 63 20 3d  W.  */.  p->pc =
1cc63 20 70 63 20 2b 20 31 3b 0a 20 20 72 63 20 3d 20   pc + 1;.  rc = 
1cc64 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 67 6f  SQLITE_ROW;.  go
1cc65 74 6f 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a  to vdbe_return;.
1cc66 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f  }../* Opcode: Co
1cc67 6e 63 61 74 20 50 31 20 50 32 20 50 33 20 2a 20  ncat P1 P2 P3 * 
1cc68 2a 0a 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20  *.**.** Add the 
1cc69 74 65 78 74 20 69 6e 20 72 65 67 69 73 74 65 72  text in register
1cc6a 20 50 31 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64   P1 onto the end
1cc6b 20 6f 66 20 74 68 65 20 74 65 78 74 20 69 6e 0a   of the text in.
1cc6c 2a 2a 20 72 65 67 69 73 74 65 72 20 50 32 20 61  ** register P2 a
1cc6d 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  nd store the res
1cc6e 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20  ult in register 
1cc6f 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  P3..** If either
1cc70 20 74 68 65 20 50 31 20 6f 72 20 50 32 20 74 65   the P1 or P2 te
1cc71 78 74 20 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e  xt are NULL then
1cc72 20 73 74 6f 72 65 20 4e 55 4c 4c 20 69 6e 20 50   store NULL in P
1cc73 33 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 33 20 3d 20  3..**.**   P3 = 
1cc74 50 32 20 7c 7c 20 50 31 0a 2a 2a 0a 2a 2a 20 49  P2 || P1.**.** I
1cc75 74 20 69 73 20 69 6c 6c 65 67 61 6c 20 66 6f 72  t is illegal for
1cc76 20 50 31 20 61 6e 64 20 50 33 20 74 6f 20 62 65   P1 and P3 to be
1cc77 20 74 68 65 20 73 61 6d 65 20 72 65 67 69 73 74   the same regist
1cc78 65 72 2e 20 53 6f 6d 65 74 69 6d 65 73 2c 0a 2a  er. Sometimes,.*
1cc79 2a 20 69 66 20 50 33 20 69 73 20 74 68 65 20 73  * if P3 is the s
1cc7a 61 6d 65 20 72 65 67 69 73 74 65 72 20 61 73 20  ame register as 
1cc7b 50 32 2c 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  P2, the implemen
1cc7c 74 61 74 69 6f 6e 20 69 73 20 61 62 6c 65 0a 2a  tation is able.*
1cc7d 2a 20 74 6f 20 61 76 6f 69 64 20 61 20 6d 65 6d  * to avoid a mem
1cc7e 63 70 79 28 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f  cpy()..*/.case O
1cc7f 50 5f 43 6f 6e 63 61 74 3a 20 7b 20 20 20 20 20  P_Concat: {     
1cc80 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
1cc81 20 54 4b 5f 43 4f 4e 43 41 54 2c 20 69 6e 31 2c   TK_CONCAT, in1,
1cc82 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 23 69   in2, out3 */.#i
1cc83 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1cc84 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1cc85 74 6f 20 75 2e 61 65 20 2a 2f 0a 20 20 69 36 34  to u.ae */.  i64
1cc86 20 6e 42 79 74 65 3b 0a 23 65 6e 64 69 66 20 2f   nByte;.#endif /
1cc87 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1cc88 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1cc89 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  e */..  assert( 
1cc8a 70 49 6e 31 21 3d 70 4f 75 74 20 29 3b 0a 20 20  pIn1!=pOut );.  
1cc8b 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73  if( (pIn1->flags
1cc8c 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67 73 29 20   | pIn2->flags) 
1cc8d 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20  & MEM_Null ){.  
1cc8e 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1cc8f 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20  SetNull(pOut);. 
1cc90 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20     break;.  }.  
1cc91 69 66 28 20 45 78 70 61 6e 64 42 6c 6f 62 28 70  if( ExpandBlob(p
1cc92 49 6e 31 29 20 7c 7c 20 45 78 70 61 6e 64 42 6c  In1) || ExpandBl
1cc93 6f 62 28 70 49 6e 32 29 20 29 20 67 6f 74 6f 20  ob(pIn2) ) goto 
1cc94 6e 6f 5f 6d 65 6d 3b 0a 20 20 53 74 72 69 6e 67  no_mem;.  String
1cc95 69 66 79 28 70 49 6e 31 2c 20 65 6e 63 6f 64 69  ify(pIn1, encodi
1cc96 6e 67 29 3b 0a 20 20 53 74 72 69 6e 67 69 66 79  ng);.  Stringify
1cc97 28 70 49 6e 32 2c 20 65 6e 63 6f 64 69 6e 67 29  (pIn2, encoding)
1cc98 3b 0a 20 20 75 2e 61 65 2e 6e 42 79 74 65 20 3d  ;.  u.ae.nByte =
1cc99 20 70 49 6e 31 2d 3e 6e 20 2b 20 70 49 6e 32 2d   pIn1->n + pIn2-
1cc9a 3e 6e 3b 0a 20 20 69 66 28 20 75 2e 61 65 2e 6e  >n;.  if( u.ae.n
1cc9b 42 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b  Byte>db->aLimit[
1cc9c 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e  SQLITE_LIMIT_LEN
1cc9d 47 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f  GTH] ){.    goto
1cc9e 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20   too_big;.  }.  
1cc9f 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
1cca0 4f 75 74 2c 20 4d 45 4d 5f 53 74 72 29 3b 0a 20  Out, MEM_Str);. 
1cca1 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
1cca2 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 28 69  MemGrow(pOut, (i
1cca3 6e 74 29 75 2e 61 65 2e 6e 42 79 74 65 2b 32 2c  nt)u.ae.nByte+2,
1cca4 20 70 4f 75 74 3d 3d 70 49 6e 32 29 20 29 7b 0a   pOut==pIn2) ){.
1cca5 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b      goto no_mem;
1cca6 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 75 74 21  .  }.  if( pOut!
1cca7 3d 70 49 6e 32 20 29 7b 0a 20 20 20 20 6d 65 6d  =pIn2 ){.    mem
1cca8 63 70 79 28 70 4f 75 74 2d 3e 7a 2c 20 70 49 6e  cpy(pOut->z, pIn
1cca9 32 2d 3e 7a 2c 20 70 49 6e 32 2d 3e 6e 29 3b 0a  2->z, pIn2->n);.
1ccaa 20 20 7d 0a 20 20 6d 65 6d 63 70 79 28 26 70 4f    }.  memcpy(&pO
1ccab 75 74 2d 3e 7a 5b 70 49 6e 32 2d 3e 6e 5d 2c 20  ut->z[pIn2->n], 
1ccac 70 49 6e 31 2d 3e 7a 2c 20 70 49 6e 31 2d 3e 6e  pIn1->z, pIn1->n
1ccad 29 3b 0a 20 20 70 4f 75 74 2d 3e 7a 5b 75 2e 61  );.  pOut->z[u.a
1ccae 65 2e 6e 42 79 74 65 5d 20 3d 20 30 3b 0a 20 20  e.nByte] = 0;.  
1ccaf 70 4f 75 74 2d 3e 7a 5b 75 2e 61 65 2e 6e 42 79  pOut->z[u.ae.nBy
1ccb0 74 65 2b 31 5d 20 3d 20 30 3b 0a 20 20 70 4f 75  te+1] = 0;.  pOu
1ccb1 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f  t->flags |= MEM_
1ccb2 54 65 72 6d 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20  Term;.  pOut->n 
1ccb3 3d 20 28 69 6e 74 29 75 2e 61 65 2e 6e 42 79 74  = (int)u.ae.nByt
1ccb4 65 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d  e;.  pOut->enc =
1ccb5 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 50 44   encoding;.  UPD
1ccb6 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45  ATE_MAX_BLOBSIZE
1ccb7 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b  (pOut);.  break;
1ccb8 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41  .}../* Opcode: A
1ccb9 64 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  dd P1 P2 P3 * *.
1ccba 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 76 61  **.** Add the va
1ccbb 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1ccbc 50 31 20 74 6f 20 74 68 65 20 76 61 6c 75 65 20  P1 to the value 
1ccbd 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a  in register P2.*
1ccbe 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20  * and store the 
1ccbf 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74  result in regist
1ccc0 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74  er P3..** If eit
1ccc1 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c  her input is NUL
1ccc2 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  L, the result is
1ccc3 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63   NULL..*/./* Opc
1ccc4 6f 64 65 3a 20 4d 75 6c 74 69 70 6c 79 20 50 31  ode: Multiply P1
1ccc5 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1ccc6 0a 2a 2a 20 4d 75 6c 74 69 70 6c 79 20 74 68 65  .** Multiply the
1ccc7 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1ccc8 65 72 20 50 31 20 62 79 20 74 68 65 20 76 61 6c  er P1 by the val
1ccc9 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1ccca 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74  2.** and store t
1cccb 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67  he result in reg
1cccc 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20  ister P3..** If 
1cccd 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73 20  either input is 
1ccce 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74  NULL, the result
1cccf 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20   is NULL..*/./* 
1ccd0 4f 70 63 6f 64 65 3a 20 53 75 62 74 72 61 63 74  Opcode: Subtract
1ccd1 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
1ccd2 0a 2a 2a 20 53 75 62 74 72 61 63 74 20 74 68 65  .** Subtract the
1ccd3 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1ccd4 65 72 20 50 31 20 66 72 6f 6d 20 74 68 65 20 76  er P1 from the v
1ccd5 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1ccd6 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65   P2.** and store
1ccd7 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72   the result in r
1ccd8 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49  egister P3..** I
1ccd9 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69  f either input i
1ccda 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75  s NULL, the resu
1ccdb 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f  lt is NULL..*/./
1ccdc 2a 20 4f 70 63 6f 64 65 3a 20 44 69 76 69 64 65  * Opcode: Divide
1ccdd 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a   P1 P2 P3 * *.**
1ccde 0a 2a 2a 20 44 69 76 69 64 65 20 74 68 65 20 76  .** Divide the v
1ccdf 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1cce0 20 50 31 20 62 79 20 74 68 65 20 76 61 6c 75 65   P1 by the value
1cce1 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a   in register P2.
1cce2 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65  ** and store the
1cce3 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73   result in regis
1cce4 74 65 72 20 50 33 20 28 50 33 3d 50 32 2f 50 31  ter P3 (P3=P2/P1
1cce5 29 2e 20 49 66 20 74 68 65 20 76 61 6c 75 65 20  ). If the value 
1cce6 69 6e 20 0a 2a 2a 20 72 65 67 69 73 74 65 72 20  in .** register 
1cce7 50 31 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e  P1 is zero, then
1cce8 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e   the result is N
1cce9 55 4c 4c 2e 20 49 66 20 65 69 74 68 65 72 20 69  ULL. If either i
1ccea 6e 70 75 74 20 69 73 20 0a 2a 2a 20 4e 55 4c 4c  nput is .** NULL
1cceb 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  , the result is 
1ccec 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  NULL..*/./* Opco
1cced 64 65 3a 20 52 65 6d 61 69 6e 64 65 72 20 50 31  de: Remainder P1
1ccee 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1ccef 20 43 6f 6d 70 75 74 65 20 74 68 65 20 72 65 6d   Compute the rem
1ccf0 61 69 6e 64 65 72 20 61 66 74 65 72 20 69 6e 74  ainder after int
1ccf1 65 67 65 72 20 64 69 76 69 73 69 6f 6e 20 6f 66  eger division of
1ccf2 20 74 68 65 20 76 61 6c 75 65 20 69 6e 0a 2a 2a   the value in.**
1ccf3 20 72 65 67 69 73 74 65 72 20 50 31 20 62 79 20   register P1 by 
1ccf4 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1ccf5 69 73 74 65 72 20 50 32 20 61 6e 64 20 73 74 6f  ister P2 and sto
1ccf6 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e  re the result in
1ccf7 20 50 33 2e 20 0a 2a 2a 20 49 66 20 74 68 65 20   P3. .** If the 
1ccf8 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
1ccf9 72 20 50 32 20 69 73 20 7a 65 72 6f 20 74 68 65  r P2 is zero the
1ccfa 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e   result is NULL.
1ccfb 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 6f 70  .** If either op
1ccfc 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 2c 20 74  erand is NULL, t
1ccfd 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c  he result is NUL
1ccfe 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 64  L..*/.case OP_Ad
1ccff 64 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d:              
1cd00 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1cd01 54 4b 5f 50 4c 55 53 2c 20 69 6e 31 2c 20 69 6e  TK_PLUS, in1, in
1cd02 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20  2, out3 */.case 
1cd03 4f 50 5f 53 75 62 74 72 61 63 74 3a 20 20 20 20  OP_Subtract:    
1cd04 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
1cd05 65 20 61 73 20 54 4b 5f 4d 49 4e 55 53 2c 20 69  e as TK_MINUS, i
1cd06 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f  n1, in2, out3 */
1cd07 0a 63 61 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c  .case OP_Multipl
1cd08 79 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y:              
1cd09 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 54  /* same as TK_ST
1cd0a 41 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75  AR, in1, in2, ou
1cd0b 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 69  t3 */.case OP_Di
1cd0c 76 69 64 65 3a 20 20 20 20 20 20 20 20 20 20 20  vide:           
1cd0d 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1cd0e 54 4b 5f 53 4c 41 53 48 2c 20 69 6e 31 2c 20 69  TK_SLASH, in1, i
1cd0f 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65  n2, out3 */.case
1cd10 20 4f 50 5f 52 65 6d 61 69 6e 64 65 72 3a 20 7b   OP_Remainder: {
1cd11 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
1cd12 6d 65 20 61 73 20 54 4b 5f 52 45 4d 2c 20 69 6e  me as TK_REM, in
1cd13 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a  1, in2, out3 */.
1cd14 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1cd15 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1cd16 69 6e 74 6f 20 75 2e 61 66 20 2a 2f 0a 20 20 69  into u.af */.  i
1cd17 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 2f  nt flags;      /
1cd18 2a 20 43 6f 6d 62 69 6e 65 64 20 4d 45 4d 5f 2a  * Combined MEM_*
1cd19 20 66 6c 61 67 73 20 66 72 6f 6d 20 62 6f 74 68   flags from both
1cd1a 20 69 6e 70 75 74 73 20 2a 2f 0a 20 20 69 36 34   inputs */.  i64
1cd1b 20 69 41 3b 20 20 20 20 20 20 20 20 20 2f 2a 20   iA;         /* 
1cd1c 49 6e 74 65 67 65 72 20 76 61 6c 75 65 20 6f 66  Integer value of
1cd1d 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f   left operand */
1cd1e 0a 20 20 69 36 34 20 69 42 3b 20 20 20 20 20 20  .  i64 iB;      
1cd1f 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 76 61     /* Integer va
1cd20 6c 75 65 20 6f 66 20 72 69 67 68 74 20 6f 70 65  lue of right ope
1cd21 72 61 6e 64 20 2a 2f 0a 20 20 64 6f 75 62 6c 65  rand */.  double
1cd22 20 72 41 3b 20 20 20 20 20 20 2f 2a 20 52 65 61   rA;      /* Rea
1cd23 6c 20 76 61 6c 75 65 20 6f 66 20 6c 65 66 74 20  l value of left 
1cd24 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 64 6f 75  operand */.  dou
1cd25 62 6c 65 20 72 42 3b 20 20 20 20 20 20 2f 2a 20  ble rB;      /* 
1cd26 52 65 61 6c 20 76 61 6c 75 65 20 6f 66 20 72 69  Real value of ri
1cd27 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 23  ght operand */.#
1cd28 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1cd29 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1cd2a 6e 74 6f 20 75 2e 61 66 20 2a 2f 0a 0a 20 20 61  nto u.af */..  a
1cd2b 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e  pplyNumericAffin
1cd2c 69 74 79 28 70 49 6e 31 29 3b 0a 20 20 61 70 70  ity(pIn1);.  app
1cd2d 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74  lyNumericAffinit
1cd2e 79 28 70 49 6e 32 29 3b 0a 20 20 75 2e 61 66 2e  y(pIn2);.  u.af.
1cd2f 66 6c 61 67 73 20 3d 20 70 49 6e 31 2d 3e 66 6c  flags = pIn1->fl
1cd30 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67  ags | pIn2->flag
1cd31 73 3b 0a 20 20 69 66 28 20 28 75 2e 61 66 2e 66  s;.  if( (u.af.f
1cd32 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29  lags & MEM_Null)
1cd33 21 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68  !=0 ) goto arith
1cd34 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f  metic_result_is_
1cd35 6e 75 6c 6c 3b 0a 20 20 69 66 28 20 28 70 49 6e  null;.  if( (pIn
1cd36 31 2d 3e 66 6c 61 67 73 20 26 20 70 49 6e 32 2d  1->flags & pIn2-
1cd37 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74  >flags & MEM_Int
1cd38 29 3d 3d 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20  )==MEM_Int ){.  
1cd39 20 20 75 2e 61 66 2e 69 41 20 3d 20 70 49 6e 31    u.af.iA = pIn1
1cd3a 2d 3e 75 2e 69 3b 0a 20 20 20 20 75 2e 61 66 2e  ->u.i;.    u.af.
1cd3b 69 42 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b 0a  iB = pIn2->u.i;.
1cd3c 20 20 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d      switch( pOp-
1cd3d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 20  >opcode ){.     
1cd3e 20 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20   case OP_Add:   
1cd3f 20 20 20 20 20 20 75 2e 61 66 2e 69 42 20 2b 3d        u.af.iB +=
1cd40 20 75 2e 61 66 2e 69 41 3b 20 20 20 20 20 20 20   u.af.iA;       
1cd41 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
1cd42 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a 20 20  e OP_Subtract:  
1cd43 20 20 75 2e 61 66 2e 69 42 20 2d 3d 20 75 2e 61    u.af.iB -= u.a
1cd44 66 2e 69 41 3b 20 20 20 20 20 20 20 62 72 65 61  f.iA;       brea
1cd45 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50  k;.      case OP
1cd46 5f 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 75 2e  _Multiply:    u.
1cd47 61 66 2e 69 42 20 2a 3d 20 75 2e 61 66 2e 69 41  af.iB *= u.af.iA
1cd48 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;       break;. 
1cd49 20 20 20 20 20 63 61 73 65 20 4f 50 5f 44 69 76       case OP_Div
1cd4a 69 64 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 69  ide: {.        i
1cd4b 66 28 20 75 2e 61 66 2e 69 41 3d 3d 30 20 29 20  f( u.af.iA==0 ) 
1cd4c 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f  goto arithmetic_
1cd4d 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a  result_is_null;.
1cd4e 20 20 20 20 20 20 20 20 2f 2a 20 44 69 76 69 64          /* Divid
1cd4f 69 6e 67 20 74 68 65 20 6c 61 72 67 65 73 74 20  ing the largest 
1cd50 70 6f 73 73 69 62 6c 65 20 6e 65 67 61 74 69 76  possible negativ
1cd51 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72  e 64-bit integer
1cd52 20 28 31 3c 3c 36 33 29 20 62 79 0a 20 20 20 20   (1<<63) by.    
1cd53 20 20 20 20 2a 2a 20 2d 31 20 72 65 74 75 72 6e      ** -1 return
1cd54 73 20 61 6e 20 69 6e 74 65 67 65 72 20 74 6f 6f  s an integer too
1cd55 20 6c 61 72 67 65 20 74 6f 20 73 74 6f 72 65 20   large to store 
1cd56 69 6e 20 61 20 36 34 2d 62 69 74 20 64 61 74 61  in a 64-bit data
1cd57 2d 74 79 70 65 2e 20 4f 6e 0a 20 20 20 20 20 20  -type. On.      
1cd58 20 20 2a 2a 20 73 6f 6d 65 20 61 72 63 68 69 74    ** some archit
1cd59 65 63 74 75 72 65 73 2c 20 74 68 65 20 76 61 6c  ectures, the val
1cd5a 75 65 20 6f 76 65 72 66 6c 6f 77 73 20 74 6f 20  ue overflows to 
1cd5b 28 31 3c 3c 36 33 29 2e 20 4f 6e 20 6f 74 68 65  (1<<63). On othe
1cd5c 72 73 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 61  rs,.        ** a
1cd5d 20 53 49 47 46 50 45 20 69 73 20 69 73 73 75 65   SIGFPE is issue
1cd5e 64 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  d. The following
1cd5f 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f 72 6d 61   statement norma
1cd60 6c 69 7a 65 73 20 74 68 69 73 0a 20 20 20 20 20  lizes this.     
1cd61 20 20 20 2a 2a 20 62 65 68 61 76 69 6f 72 20 73     ** behavior s
1cd62 6f 20 74 68 61 74 20 61 6c 6c 20 61 72 63 68 69  o that all archi
1cd63 74 65 63 74 75 72 65 73 20 62 65 68 61 76 65 20  tectures behave 
1cd64 61 73 20 69 66 20 69 6e 74 65 67 65 72 0a 20 20  as if integer.  
1cd65 20 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f        ** overflo
1cd66 77 20 6f 63 63 75 72 72 65 64 2e 0a 20 20 20 20  w occurred..    
1cd67 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
1cd68 66 28 20 75 2e 61 66 2e 69 41 3d 3d 2d 31 20 26  f( u.af.iA==-1 &
1cd69 26 20 75 2e 61 66 2e 69 42 3d 3d 53 4d 41 4c 4c  & u.af.iB==SMALL
1cd6a 45 53 54 5f 49 4e 54 36 34 20 29 20 75 2e 61 66  EST_INT64 ) u.af
1cd6b 2e 69 41 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  .iA = 1;.       
1cd6c 20 75 2e 61 66 2e 69 42 20 2f 3d 20 75 2e 61 66   u.af.iB /= u.af
1cd6d 2e 69 41 3b 0a 20 20 20 20 20 20 20 20 62 72 65  .iA;.        bre
1cd6e 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
1cd6f 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
1cd70 20 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41       if( u.af.iA
1cd71 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68  ==0 ) goto arith
1cd72 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f  metic_result_is_
1cd73 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 66  null;.        if
1cd74 28 20 75 2e 61 66 2e 69 41 3d 3d 2d 31 20 29 20  ( u.af.iA==-1 ) 
1cd75 75 2e 61 66 2e 69 41 20 3d 20 31 3b 0a 20 20 20  u.af.iA = 1;.   
1cd76 20 20 20 20 20 75 2e 61 66 2e 69 42 20 25 3d 20       u.af.iB %= 
1cd77 75 2e 61 66 2e 69 41 3b 0a 20 20 20 20 20 20 20  u.af.iA;.       
1cd78 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
1cd79 20 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 2d 3e      }.    pOut->
1cd7a 75 2e 69 20 3d 20 75 2e 61 66 2e 69 42 3b 0a 20  u.i = u.af.iB;. 
1cd7b 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61     MemSetTypeFla
1cd7c 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29  g(pOut, MEM_Int)
1cd7d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75  ;.  }else{.    u
1cd7e 2e 61 66 2e 72 41 20 3d 20 73 71 6c 69 74 65 33  .af.rA = sqlite3
1cd7f 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49  VdbeRealValue(pI
1cd80 6e 31 29 3b 0a 20 20 20 20 75 2e 61 66 2e 72 42  n1);.    u.af.rB
1cd81 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65   = sqlite3VdbeRe
1cd82 61 6c 56 61 6c 75 65 28 70 49 6e 32 29 3b 0a 20  alValue(pIn2);. 
1cd83 20 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e     switch( pOp->
1cd84 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 20 20  opcode ){.      
1cd85 63 61 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20  case OP_Add:    
1cd86 20 20 20 20 20 75 2e 61 66 2e 72 42 20 2b 3d 20       u.af.rB += 
1cd87 75 2e 61 66 2e 72 41 3b 20 20 20 20 20 20 20 62  u.af.rA;       b
1cd88 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
1cd89 20 4f 50 5f 53 75 62 74 72 61 63 74 3a 20 20 20   OP_Subtract:   
1cd8a 20 75 2e 61 66 2e 72 42 20 2d 3d 20 75 2e 61 66   u.af.rB -= u.af
1cd8b 2e 72 41 3b 20 20 20 20 20 20 20 62 72 65 61 6b  .rA;       break
1cd8c 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f  ;.      case OP_
1cd8d 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 75 2e 61  Multiply:    u.a
1cd8e 66 2e 72 42 20 2a 3d 20 75 2e 61 66 2e 72 41 3b  f.rB *= u.af.rA;
1cd8f 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1cd90 20 20 20 20 63 61 73 65 20 4f 50 5f 44 69 76 69      case OP_Divi
1cd91 64 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 2f 2a  de: {.        /*
1cd92 20 28 64 6f 75 62 6c 65 29 30 20 49 6e 20 63 61   (double)0 In ca
1cd93 73 65 20 6f 66 20 53 51 4c 49 54 45 5f 4f 4d 49  se of SQLITE_OMI
1cd94 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54  T_FLOATING_POINT
1cd95 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  ... */.        i
1cd96 66 28 20 75 2e 61 66 2e 72 41 3d 3d 28 64 6f 75  f( u.af.rA==(dou
1cd97 62 6c 65 29 30 20 29 20 67 6f 74 6f 20 61 72 69  ble)0 ) goto ari
1cd98 74 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69  thmetic_result_i
1cd99 73 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20  s_null;.        
1cd9a 75 2e 61 66 2e 72 42 20 2f 3d 20 75 2e 61 66 2e  u.af.rB /= u.af.
1cd9b 72 41 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  rA;.        brea
1cd9c 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
1cd9d 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
1cd9e 20 20 20 20 75 2e 61 66 2e 69 41 20 3d 20 28 69      u.af.iA = (i
1cd9f 36 34 29 75 2e 61 66 2e 72 41 3b 0a 20 20 20 20  64)u.af.rA;.    
1cda0 20 20 20 20 75 2e 61 66 2e 69 42 20 3d 20 28 69      u.af.iB = (i
1cda1 36 34 29 75 2e 61 66 2e 72 42 3b 0a 20 20 20 20  64)u.af.rB;.    
1cda2 20 20 20 20 69 66 28 20 75 2e 61 66 2e 69 41 3d      if( u.af.iA=
1cda3 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d  =0 ) goto arithm
1cda4 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e  etic_result_is_n
1cda5 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ull;.        if(
1cda6 20 75 2e 61 66 2e 69 41 3d 3d 2d 31 20 29 20 75   u.af.iA==-1 ) u
1cda7 2e 61 66 2e 69 41 20 3d 20 31 3b 0a 20 20 20 20  .af.iA = 1;.    
1cda8 20 20 20 20 75 2e 61 66 2e 72 42 20 3d 20 28 64      u.af.rB = (d
1cda9 6f 75 62 6c 65 29 28 75 2e 61 66 2e 69 42 20 25  ouble)(u.af.iB %
1cdaa 20 75 2e 61 66 2e 69 41 29 3b 0a 20 20 20 20 20   u.af.iA);.     
1cdab 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
1cdac 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
1cdad 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 75 2e 61  sqlite3IsNaN(u.a
1cdae 66 2e 72 42 29 20 29 7b 0a 20 20 20 20 20 20 67  f.rB) ){.      g
1cdaf 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72  oto arithmetic_r
1cdb0 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20  esult_is_null;. 
1cdb1 20 20 20 7d 0a 20 20 20 20 70 4f 75 74 2d 3e 72     }.    pOut->r
1cdb2 20 3d 20 75 2e 61 66 2e 72 42 3b 0a 20 20 20 20   = u.af.rB;.    
1cdb3 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
1cdb4 4f 75 74 2c 20 4d 45 4d 5f 52 65 61 6c 29 3b 0a  Out, MEM_Real);.
1cdb5 20 20 20 20 69 66 28 20 28 75 2e 61 66 2e 66 6c      if( (u.af.fl
1cdb6 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c 29 3d  ags & MEM_Real)=
1cdb7 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
1cdb8 74 65 33 56 64 62 65 49 6e 74 65 67 65 72 41 66  te3VdbeIntegerAf
1cdb9 66 69 6e 69 74 79 28 70 4f 75 74 29 3b 0a 20 20  finity(pOut);.  
1cdba 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b    }.  }.  break;
1cdbb 0a 0a 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73  ..arithmetic_res
1cdbc 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3a 0a 20 20 73  ult_is_null:.  s
1cdbd 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
1cdbe 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 62 72  Null(pOut);.  br
1cdbf 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1cdc0 65 3a 20 43 6f 6c 6c 53 65 71 20 2a 20 2a 20 50  e: CollSeq * * P
1cdc1 34 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20  4.**.** P4 is a 
1cdc2 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 43 6f 6c  pointer to a Col
1cdc3 6c 53 65 71 20 73 74 72 75 63 74 2e 20 49 66 20  lSeq struct. If 
1cdc4 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74 6f  the next call to
1cdc5 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e   a user function
1cdc6 0a 2a 2a 20 6f 72 20 61 67 67 72 65 67 61 74 65  .** or aggregate
1cdc7 20 63 61 6c 6c 73 20 73 71 6c 69 74 65 33 47 65   calls sqlite3Ge
1cdc8 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 29 2c 20  tFuncCollSeq(), 
1cdc9 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  this collation s
1cdca 65 71 75 65 6e 63 65 20 77 69 6c 6c 0a 2a 2a 20  equence will.** 
1cdcb 62 65 20 72 65 74 75 72 6e 65 64 2e 20 54 68 69  be returned. Thi
1cdcc 73 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65  s is used by the
1cdcd 20 62 75 69 6c 74 2d 69 6e 20 6d 69 6e 28 29 2c   built-in min(),
1cdce 20 6d 61 78 28 29 20 61 6e 64 20 6e 75 6c 6c 69   max() and nulli
1cdcf 66 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73  f().** functions
1cdd0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65  ..**.** The inte
1cdd1 72 66 61 63 65 20 75 73 65 64 20 62 79 20 74 68  rface used by th
1cdd2 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
1cdd3 20 6f 66 20 74 68 65 20 61 66 6f 72 65 6d 65 6e   of the aforemen
1cdd4 74 69 6f 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73  tioned functions
1cdd5 0a 2a 2a 20 74 6f 20 72 65 74 72 69 65 76 65 20  .** to retrieve 
1cdd6 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  the collation se
1cdd7 71 75 65 6e 63 65 20 73 65 74 20 62 79 20 74 68  quence set by th
1cdd8 69 73 20 6f 70 63 6f 64 65 20 69 73 20 6e 6f 74  is opcode is not
1cdd9 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 70 75   available.** pu
1cdda 62 6c 69 63 6c 79 2c 20 6f 6e 6c 79 20 74 6f 20  blicly, only to 
1cddb 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 64  user functions d
1cddc 65 66 69 6e 65 64 20 69 6e 20 66 75 6e 63 2e 63  efined in func.c
1cddd 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6c  ..*/.case OP_Col
1cdde 6c 53 65 71 3a 20 7b 0a 20 20 61 73 73 65 72 74  lSeq: {.  assert
1cddf 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1cde0 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 62  4_COLLSEQ );.  b
1cde1 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1cde2 64 65 3a 20 46 75 6e 63 74 69 6f 6e 20 50 31 20  de: Function P1 
1cde3 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a  P2 P3 P4 P5.**.*
1cde4 2a 20 49 6e 76 6f 6b 65 20 61 20 75 73 65 72 20  * Invoke a user 
1cde5 66 75 6e 63 74 69 6f 6e 20 28 50 34 20 69 73 20  function (P4 is 
1cde6 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 46  a pointer to a F
1cde7 75 6e 63 74 69 6f 6e 20 73 74 72 75 63 74 75 72  unction structur
1cde8 65 20 74 68 61 74 0a 2a 2a 20 64 65 66 69 6e 65  e that.** define
1cde9 73 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 29 20  s the function) 
1cdea 77 69 74 68 20 50 35 20 61 72 67 75 6d 65 6e 74  with P5 argument
1cdeb 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72 65 67  s taken from reg
1cdec 69 73 74 65 72 20 50 32 20 61 6e 64 0a 2a 2a 20  ister P2 and.** 
1cded 73 75 63 63 65 73 73 6f 72 73 2e 20 20 54 68 65  successors.  The
1cdee 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 66   result of the f
1cdef 75 6e 63 74 69 6f 6e 20 69 73 20 73 74 6f 72 65  unction is store
1cdf0 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33  d in register P3
1cdf1 2e 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 33  ..** Register P3
1cdf2 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 6f 6e 65   must not be one
1cdf3 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
1cdf4 20 69 6e 70 75 74 73 2e 0a 2a 2a 0a 2a 2a 20 50   inputs..**.** P
1cdf5 31 20 69 73 20 61 20 33 32 2d 62 69 74 20 62 69  1 is a 32-bit bi
1cdf6 74 6d 61 73 6b 20 69 6e 64 69 63 61 74 69 6e 67  tmask indicating
1cdf7 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
1cdf8 65 61 63 68 20 61 72 67 75 6d 65 6e 74 20 74 6f  each argument to
1cdf9 20 74 68 65 20 0a 2a 2a 20 66 75 6e 63 74 69 6f   the .** functio
1cdfa 6e 20 77 61 73 20 64 65 74 65 72 6d 69 6e 65 64  n was determined
1cdfb 20 74 6f 20 62 65 20 63 6f 6e 73 74 61 6e 74 20   to be constant 
1cdfc 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2e  at compile time.
1cdfd 20 49 66 20 74 68 65 20 66 69 72 73 74 0a 2a 2a   If the first.**
1cdfe 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20 63 6f   argument was co
1cdff 6e 73 74 61 6e 74 20 74 68 65 6e 20 62 69 74 20  nstant then bit 
1ce00 30 20 6f 66 20 50 31 20 69 73 20 73 65 74 2e 20  0 of P1 is set. 
1ce01 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20  This is used to 
1ce02 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 65  determine.** whe
1ce03 74 68 65 72 20 6d 65 74 61 20 64 61 74 61 20 61  ther meta data a
1ce04 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
1ce05 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 61   user function a
1ce06 72 67 75 6d 65 6e 74 20 75 73 69 6e 67 20 74 68  rgument using th
1ce07 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 73 65 74  e.** sqlite3_set
1ce08 5f 61 75 78 64 61 74 61 28 29 20 41 50 49 20 6d  _auxdata() API m
1ce09 61 79 20 62 65 20 73 61 66 65 6c 79 20 72 65 74  ay be safely ret
1ce0a 61 69 6e 65 64 20 75 6e 74 69 6c 20 74 68 65 20  ained until the 
1ce0b 6e 65 78 74 0a 2a 2a 20 69 6e 76 6f 63 61 74 69  next.** invocati
1ce0c 6f 6e 20 6f 66 20 74 68 69 73 20 6f 70 63 6f 64  on of this opcod
1ce0d 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  e..**.** See als
1ce0e 6f 3a 20 41 67 67 53 74 65 70 20 61 6e 64 20 41  o: AggStep and A
1ce0f 67 67 46 69 6e 61 6c 0a 2a 2f 0a 63 61 73 65 20  ggFinal.*/.case 
1ce10 4f 50 5f 46 75 6e 63 74 69 6f 6e 3a 20 7b 0a 23  OP_Function: {.#
1ce11 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1ce12 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1ce13 6e 74 6f 20 75 2e 61 67 20 2a 2f 0a 20 20 69 6e  nto u.ag */.  in
1ce14 74 20 69 3b 0a 20 20 4d 65 6d 20 2a 70 41 72 67  t i;.  Mem *pArg
1ce15 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ;.  sqlite3_cont
1ce16 65 78 74 20 63 74 78 3b 0a 20 20 73 71 6c 69 74  ext ctx;.  sqlit
1ce17 65 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c  e3_value **apVal
1ce18 3b 0a 20 20 69 6e 74 20 6e 3b 0a 23 65 6e 64 69  ;.  int n;.#endi
1ce19 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1ce1a 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1ce1b 75 2e 61 67 20 2a 2f 0a 0a 20 20 75 2e 61 67 2e  u.ag */..  u.ag.
1ce1c 6e 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 75  n = pOp->p5;.  u
1ce1d 2e 61 67 2e 61 70 56 61 6c 20 3d 20 70 2d 3e 61  .ag.apVal = p->a
1ce1e 70 41 72 67 3b 0a 20 20 61 73 73 65 72 74 28 20  pArg;.  assert( 
1ce1f 75 2e 61 67 2e 61 70 56 61 6c 20 7c 7c 20 75 2e  u.ag.apVal || u.
1ce20 61 67 2e 6e 3d 3d 30 20 29 3b 0a 0a 20 20 61 73  ag.n==0 );..  as
1ce21 73 65 72 74 28 20 75 2e 61 67 2e 6e 3d 3d 30 20  sert( u.ag.n==0 
1ce22 7c 7c 20 28 70 4f 70 2d 3e 70 32 3e 30 20 26 26  || (pOp->p2>0 &&
1ce23 20 70 4f 70 2d 3e 70 32 2b 75 2e 61 67 2e 6e 3c   pOp->p2+u.ag.n<
1ce24 3d 70 2d 3e 6e 4d 65 6d 2b 31 29 20 29 3b 0a 20  =p->nMem+1) );. 
1ce25 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33   assert( pOp->p3
1ce26 3c 70 4f 70 2d 3e 70 32 20 7c 7c 20 70 4f 70 2d  <pOp->p2 || pOp-
1ce27 3e 70 33 3e 3d 70 4f 70 2d 3e 70 32 2b 75 2e 61  >p3>=pOp->p2+u.a
1ce28 67 2e 6e 20 29 3b 0a 20 20 75 2e 61 67 2e 70 41  g.n );.  u.ag.pA
1ce29 72 67 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  rg = &p->aMem[pO
1ce2a 70 2d 3e 70 32 5d 3b 0a 20 20 66 6f 72 28 75 2e  p->p2];.  for(u.
1ce2b 61 67 2e 69 3d 30 3b 20 75 2e 61 67 2e 69 3c 75  ag.i=0; u.ag.i<u
1ce2c 2e 61 67 2e 6e 3b 20 75 2e 61 67 2e 69 2b 2b 2c  .ag.n; u.ag.i++,
1ce2d 20 75 2e 61 67 2e 70 41 72 67 2b 2b 29 7b 0a 20   u.ag.pArg++){. 
1ce2e 20 20 20 75 2e 61 67 2e 61 70 56 61 6c 5b 75 2e     u.ag.apVal[u.
1ce2f 61 67 2e 69 5d 20 3d 20 75 2e 61 67 2e 70 41 72  ag.i] = u.ag.pAr
1ce30 67 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  g;.    sqlite3Vd
1ce31 62 65 4d 65 6d 53 74 6f 72 65 54 79 70 65 28 75  beMemStoreType(u
1ce32 2e 61 67 2e 70 41 72 67 29 3b 0a 20 20 20 20 52  .ag.pArg);.    R
1ce33 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
1ce34 70 2d 3e 70 32 2c 20 75 2e 61 67 2e 70 41 72 67  p->p2, u.ag.pArg
1ce35 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  );.  }..  assert
1ce36 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1ce37 34 5f 46 55 4e 43 44 45 46 20 7c 7c 20 70 4f 70  4_FUNCDEF || pOp
1ce38 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 44 42  ->p4type==P4_VDB
1ce39 45 46 55 4e 43 20 29 3b 0a 20 20 69 66 28 20 70  EFUNC );.  if( p
1ce3a 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 46  Op->p4type==P4_F
1ce3b 55 4e 43 44 45 46 20 29 7b 0a 20 20 20 20 75 2e  UNCDEF ){.    u.
1ce3c 61 67 2e 63 74 78 2e 70 46 75 6e 63 20 3d 20 70  ag.ctx.pFunc = p
1ce3d 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20  Op->p4.pFunc;.  
1ce3e 20 20 75 2e 61 67 2e 63 74 78 2e 70 56 64 62 65    u.ag.ctx.pVdbe
1ce3f 46 75 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  Func = 0;.  }els
1ce40 65 7b 0a 20 20 20 20 75 2e 61 67 2e 63 74 78 2e  e{.    u.ag.ctx.
1ce41 70 56 64 62 65 46 75 6e 63 20 3d 20 28 56 64 62  pVdbeFunc = (Vdb
1ce42 65 46 75 6e 63 2a 29 70 4f 70 2d 3e 70 34 2e 70  eFunc*)pOp->p4.p
1ce43 56 64 62 65 46 75 6e 63 3b 0a 20 20 20 20 75 2e  VdbeFunc;.    u.
1ce44 61 67 2e 63 74 78 2e 70 46 75 6e 63 20 3d 20 75  ag.ctx.pFunc = u
1ce45 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46 75 6e  .ag.ctx.pVdbeFun
1ce46 63 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20  c->pFunc;.  }.. 
1ce47 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33   assert( pOp->p3
1ce48 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70  >0 && pOp->p3<=p
1ce49 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74  ->nMem );.  pOut
1ce4a 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1ce4b 3e 70 33 5d 3b 0a 20 20 75 2e 61 67 2e 63 74 78  >p3];.  u.ag.ctx
1ce4c 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  .s.flags = MEM_N
1ce4d 75 6c 6c 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e  ull;.  u.ag.ctx.
1ce4e 73 2e 64 62 20 3d 20 64 62 3b 0a 20 20 75 2e 61  s.db = db;.  u.a
1ce4f 67 2e 63 74 78 2e 73 2e 78 44 65 6c 20 3d 20 30  g.ctx.s.xDel = 0
1ce50 3b 0a 20 20 75 2e 61 67 2e 63 74 78 2e 73 2e 7a  ;.  u.ag.ctx.s.z
1ce51 4d 61 6c 6c 6f 63 20 3d 20 30 3b 0a 0a 20 20 2f  Malloc = 0;..  /
1ce52 2a 20 54 68 65 20 6f 75 74 70 75 74 20 63 65 6c  * The output cel
1ce53 6c 20 6d 61 79 20 61 6c 72 65 61 64 79 20 68 61  l may already ha
1ce54 76 65 20 61 20 62 75 66 66 65 72 20 61 6c 6c 6f  ve a buffer allo
1ce55 63 61 74 65 64 2e 20 4d 6f 76 65 0a 20 20 2a 2a  cated. Move.  **
1ce56 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74 6f 20   the pointer to 
1ce57 75 2e 61 67 2e 63 74 78 2e 73 20 73 6f 20 69 6e  u.ag.ctx.s so in
1ce58 20 63 61 73 65 20 74 68 65 20 75 73 65 72 2d 66   case the user-f
1ce59 75 6e 63 74 69 6f 6e 20 63 61 6e 20 75 73 65 0a  unction can use.
1ce5a 20 20 2a 2a 20 74 68 65 20 61 6c 72 65 61 64 79    ** the already
1ce5b 20 61 6c 6c 6f 63 61 74 65 64 20 62 75 66 66 65   allocated buffe
1ce5c 72 20 69 6e 73 74 65 61 64 20 6f 66 20 61 6c 6c  r instead of all
1ce5d 6f 63 61 74 69 6e 67 20 61 20 6e 65 77 20 6f 6e  ocating a new on
1ce5e 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  e..  */.  sqlite
1ce5f 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e  3VdbeMemMove(&u.
1ce60 61 67 2e 63 74 78 2e 73 2c 20 70 4f 75 74 29 3b  ag.ctx.s, pOut);
1ce61 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61  .  MemSetTypeFla
1ce62 67 28 26 75 2e 61 67 2e 63 74 78 2e 73 2c 20 4d  g(&u.ag.ctx.s, M
1ce63 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20 75 2e 61  EM_Null);..  u.a
1ce64 67 2e 63 74 78 2e 69 73 45 72 72 6f 72 20 3d 20  g.ctx.isError = 
1ce65 30 3b 0a 20 20 69 66 28 20 75 2e 61 67 2e 63 74  0;.  if( u.ag.ct
1ce66 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26  x.pFunc->flags &
1ce67 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45   SQLITE_FUNC_NEE
1ce68 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 73 73  DCOLL ){.    ass
1ce69 65 72 74 28 20 70 4f 70 3e 70 2d 3e 61 4f 70 20  ert( pOp>p->aOp 
1ce6a 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
1ce6b 4f 70 5b 2d 31 5d 2e 70 34 74 79 70 65 3d 3d 50  Op[-1].p4type==P
1ce6c 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 20  4_COLLSEQ );.   
1ce6d 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d   assert( pOp[-1]
1ce6e 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c 6c  .opcode==OP_Coll
1ce6f 53 65 71 20 29 3b 0a 20 20 20 20 75 2e 61 67 2e  Seq );.    u.ag.
1ce70 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b  ctx.pColl = pOp[
1ce71 2d 31 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20  -1].p4.pColl;.  
1ce72 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  }.  if( sqlite3S
1ce73 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20 67  afetyOff(db) ) g
1ce74 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1ce75 5f 6d 69 73 75 73 65 3b 0a 20 20 28 2a 75 2e 61  _misuse;.  (*u.a
1ce76 67 2e 63 74 78 2e 70 46 75 6e 63 2d 3e 78 46 75  g.ctx.pFunc->xFu
1ce77 6e 63 29 28 26 75 2e 61 67 2e 63 74 78 2c 20 75  nc)(&u.ag.ctx, u
1ce78 2e 61 67 2e 6e 2c 20 75 2e 61 67 2e 61 70 56 61  .ag.n, u.ag.apVa
1ce79 6c 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  l);.  if( sqlite
1ce7a 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 7b  3SafetyOn(db) ){
1ce7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1ce7c 4d 65 6d 52 65 6c 65 61 73 65 28 26 75 2e 61 67  MemRelease(&u.ag
1ce7d 2e 63 74 78 2e 73 29 3b 0a 20 20 20 20 67 6f 74  .ctx.s);.    got
1ce7e 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
1ce7f 69 73 75 73 65 3b 0a 20 20 7d 0a 20 20 69 66 28  isuse;.  }.  if(
1ce80 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1ce81 64 20 29 7b 0a 20 20 20 20 2f 2a 20 45 76 65 6e  d ){.    /* Even
1ce82 20 74 68 6f 75 67 68 20 61 20 6d 61 6c 6c 6f 63   though a malloc
1ce83 28 29 20 68 61 73 20 66 61 69 6c 65 64 2c 20 74  () has failed, t
1ce84 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
1ce85 6e 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20  n of the.    ** 
1ce86 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 6d 61  user function ma
1ce87 79 20 68 61 76 65 20 63 61 6c 6c 65 64 20 61 6e  y have called an
1ce88 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
1ce89 58 58 58 28 29 20 66 75 6e 63 74 69 6f 6e 0a 20  XXX() function. 
1ce8a 20 20 20 2a 2a 20 74 6f 20 72 65 74 75 72 6e 20     ** to return 
1ce8b 61 20 76 61 6c 75 65 2e 20 54 68 65 20 66 6f 6c  a value. The fol
1ce8c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 72 65 6c 65  lowing call rele
1ce8d 61 73 65 73 20 61 6e 79 20 72 65 73 6f 75 72 63  ases any resourc
1ce8e 65 73 0a 20 20 20 20 2a 2a 20 61 73 73 6f 63 69  es.    ** associ
1ce8f 61 74 65 64 20 77 69 74 68 20 73 75 63 68 20 61  ated with such a
1ce90 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2a 0a 20   value..    **. 
1ce91 20 20 20 2a 2a 20 4e 6f 74 65 3a 20 4d 61 79 62     ** Note: Mayb
1ce92 65 20 4d 65 6d 52 65 6c 65 61 73 65 28 29 20 73  e MemRelease() s
1ce93 68 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20  hould be called 
1ce94 69 66 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  if sqlite3Safety
1ce95 4f 6e 28 29 0a 20 20 20 20 2a 2a 20 66 61 69 6c  On().    ** fail
1ce96 73 20 61 6c 73 6f 20 28 74 68 65 20 69 66 28 2e  s also (the if(.
1ce97 2e 2e 29 20 73 74 61 74 65 6d 65 6e 74 20 61 62  ..) statement ab
1ce98 6f 76 65 29 2e 20 42 75 74 20 69 66 20 70 65 6f  ove). But if peo
1ce99 70 6c 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 6d  ple are.    ** m
1ce9a 69 73 75 73 69 6e 67 20 73 71 6c 69 74 65 2c 20  isusing sqlite, 
1ce9b 74 68 65 79 20 68 61 76 65 20 62 69 67 67 65 72  they have bigger
1ce9c 20 70 72 6f 62 6c 65 6d 73 20 74 68 61 6e 20 61   problems than a
1ce9d 20 6c 65 61 6b 65 64 20 76 61 6c 75 65 2e 0a 20   leaked value.. 
1ce9e 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
1ce9f 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
1cea0 26 75 2e 61 67 2e 63 74 78 2e 73 29 3b 0a 20 20  &u.ag.ctx.s);.  
1cea1 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20    goto no_mem;. 
1cea2 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 79 20   }..  /* If any 
1cea3 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 20 66  auxiliary data f
1cea4 75 6e 63 74 69 6f 6e 73 20 68 61 76 65 20 62 65  unctions have be
1cea5 65 6e 20 63 61 6c 6c 65 64 20 62 79 20 74 68 69  en called by thi
1cea6 73 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2c  s user function,
1cea7 0a 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c  .  ** immediatel
1cea8 79 20 63 61 6c 6c 20 74 68 65 20 64 65 73 74 72  y call the destr
1cea9 75 63 74 6f 72 20 66 6f 72 20 61 6e 79 20 6e 6f  uctor for any no
1ceaa 6e 2d 73 74 61 74 69 63 20 76 61 6c 75 65 73 2e  n-static values.
1ceab 0a 20 20 2a 2f 0a 20 20 69 66 28 20 75 2e 61 67  .  */.  if( u.ag
1ceac 2e 63 74 78 2e 70 56 64 62 65 46 75 6e 63 20 29  .ctx.pVdbeFunc )
1cead 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1ceae 65 44 65 6c 65 74 65 41 75 78 44 61 74 61 28 75  eDeleteAuxData(u
1ceaf 2e 61 67 2e 63 74 78 2e 70 56 64 62 65 46 75 6e  .ag.ctx.pVdbeFun
1ceb0 63 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 20  c, pOp->p1);.   
1ceb1 20 70 4f 70 2d 3e 70 34 2e 70 56 64 62 65 46 75   pOp->p4.pVdbeFu
1ceb2 6e 63 20 3d 20 75 2e 61 67 2e 63 74 78 2e 70 56  nc = u.ag.ctx.pV
1ceb3 64 62 65 46 75 6e 63 3b 0a 20 20 20 20 70 4f 70  dbeFunc;.    pOp
1ceb4 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 56 44  ->p4type = P4_VD
1ceb5 42 45 46 55 4e 43 3b 0a 20 20 7d 0a 0a 20 20 2f  BEFUNC;.  }..  /
1ceb6 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f  * If the functio
1ceb7 6e 20 72 65 74 75 72 6e 65 64 20 61 6e 20 65 72  n returned an er
1ceb8 72 6f 72 2c 20 74 68 72 6f 77 20 61 6e 20 65 78  ror, throw an ex
1ceb9 63 65 70 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28  ception */.  if(
1ceba 20 75 2e 61 67 2e 63 74 78 2e 69 73 45 72 72 6f   u.ag.ctx.isErro
1cebb 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  r ){.    sqlite3
1cebc 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
1cebd 72 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c  rrMsg, db, "%s",
1cebe 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
1cebf 65 78 74 28 26 75 2e 61 67 2e 63 74 78 2e 73 29  ext(&u.ag.ctx.s)
1cec0 29 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e 61 67  );.    rc = u.ag
1cec1 2e 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20  .ctx.isError;.  
1cec2 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 68 65  }..  /* Copy the
1cec3 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 66   result of the f
1cec4 75 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 72 65 67  unction into reg
1cec5 69 73 74 65 72 20 50 33 20 2a 2f 0a 20 20 73 71  ister P3 */.  sq
1cec6 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
1cec7 6e 63 6f 64 69 6e 67 28 26 75 2e 61 67 2e 63 74  ncoding(&u.ag.ct
1cec8 78 2e 73 2c 20 65 6e 63 6f 64 69 6e 67 29 3b 0a  x.s, encoding);.
1cec9 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1ceca 4d 6f 76 65 28 70 4f 75 74 2c 20 26 75 2e 61 67  Move(pOut, &u.ag
1cecb 2e 63 74 78 2e 73 29 3b 0a 20 20 69 66 28 20 73  .ctx.s);.  if( s
1cecc 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f  qlite3VdbeMemToo
1cecd 42 69 67 28 70 4f 75 74 29 20 29 7b 0a 20 20 20  Big(pOut) ){.   
1cece 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20   goto too_big;. 
1cecf 20 7d 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52   }.  REGISTER_TR
1ced0 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 4f 75  ACE(pOp->p3, pOu
1ced1 74 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58  t);.  UPDATE_MAX
1ced2 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b  _BLOBSIZE(pOut);
1ced3 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1ced4 4f 70 63 6f 64 65 3a 20 42 69 74 41 6e 64 20 50  Opcode: BitAnd P
1ced5 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
1ced6 2a 20 54 61 6b 65 20 74 68 65 20 62 69 74 2d 77  * Take the bit-w
1ced7 69 73 65 20 41 4e 44 20 6f 66 20 74 68 65 20 76  ise AND of the v
1ced8 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65  alues in registe
1ced9 72 20 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a  r P1 and P2 and.
1ceda 2a 2a 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  ** store the res
1cedb 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20  ult in register 
1cedc 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72  P3..** If either
1cedd 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20   input is NULL, 
1cede 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55  the result is NU
1cedf 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  LL..*/./* Opcode
1cee0 3a 20 42 69 74 4f 72 20 50 31 20 50 32 20 50 33  : BitOr P1 P2 P3
1cee1 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20   * *.**.** Take 
1cee2 74 68 65 20 62 69 74 2d 77 69 73 65 20 4f 52 20  the bit-wise OR 
1cee3 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e  of the values in
1cee4 20 72 65 67 69 73 74 65 72 20 50 31 20 61 6e 64   register P1 and
1cee5 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 6f 72 65   P2 and.** store
1cee6 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72   the result in r
1cee7 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49  egister P3..** I
1cee8 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69  f either input i
1cee9 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75  s NULL, the resu
1ceea 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f  lt is NULL..*/./
1ceeb 2a 20 4f 70 63 6f 64 65 3a 20 53 68 69 66 74 4c  * Opcode: ShiftL
1ceec 65 66 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a  eft P1 P2 P3 * *
1ceed 0a 2a 2a 0a 2a 2a 20 53 68 69 66 74 20 74 68 65  .**.** Shift the
1ceee 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69   integer value i
1ceef 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 74 6f  n register P2 to
1cef0 20 74 68 65 20 6c 65 66 74 20 62 79 20 74 68 65   the left by the
1cef1 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 69  .** number of bi
1cef2 74 73 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ts specified by 
1cef3 74 68 65 20 69 6e 74 65 67 65 72 20 69 6e 20 72  the integer in r
1cef4 65 67 69 73 65 72 20 50 31 2e 0a 2a 2a 20 53 74  egiser P1..** St
1cef5 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20 69  ore the result i
1cef6 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  n register P3..*
1cef7 2a 20 49 66 20 65 69 74 68 65 72 20 69 6e 70 75  * If either inpu
1cef8 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72  t is NULL, the r
1cef9 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a  esult is NULL..*
1cefa 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 68 69  /./* Opcode: Shi
1cefb 66 74 52 69 67 68 74 20 50 31 20 50 32 20 50 33  ftRight P1 P2 P3
1cefc 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 68 69 66 74   * *.**.** Shift
1cefd 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c   the integer val
1cefe 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1ceff 32 20 74 6f 20 74 68 65 20 72 69 67 68 74 20 62  2 to the right b
1cf00 79 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20  y the.** number 
1cf01 6f 66 20 62 69 74 73 20 73 70 65 63 69 66 69 65  of bits specifie
1cf02 64 20 62 79 20 74 68 65 20 69 6e 74 65 67 65 72  d by the integer
1cf03 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e   in register P1.
1cf04 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 72 65  .** Store the re
1cf05 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72  sult in register
1cf06 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65   P3..** If eithe
1cf07 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c  r input is NULL,
1cf08 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e   the result is N
1cf09 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ULL..*/.case OP_
1cf0a 42 69 74 41 6e 64 3a 20 20 20 20 20 20 20 20 20  BitAnd:         
1cf0b 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
1cf0c 61 73 20 54 4b 5f 42 49 54 41 4e 44 2c 20 69 6e  as TK_BITAND, in
1cf0d 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a  1, in2, out3 */.
1cf0e 63 61 73 65 20 4f 50 5f 42 69 74 4f 72 3a 20 20  case OP_BitOr:  
1cf0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cf10 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49  /* same as TK_BI
1cf11 54 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f  TOR, in1, in2, o
1cf12 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53  ut3 */.case OP_S
1cf13 68 69 66 74 4c 65 66 74 3a 20 20 20 20 20 20 20  hiftLeft:       
1cf14 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1cf15 73 20 54 4b 5f 4c 53 48 49 46 54 2c 20 69 6e 31  s TK_LSHIFT, in1
1cf16 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63  , in2, out3 */.c
1cf17 61 73 65 20 4f 50 5f 53 68 69 66 74 52 69 67 68  ase OP_ShiftRigh
1cf18 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 2f  t: {           /
1cf19 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 53 48  * same as TK_RSH
1cf1a 49 46 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f  IFT, in1, in2, o
1cf1b 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a  ut3 */.#if 0  /*
1cf1c 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1cf1d 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 68   moved into u.ah
1cf1e 20 2a 2f 0a 20 20 69 36 34 20 61 3b 0a 20 20 69   */.  i64 a;.  i
1cf1f 36 34 20 62 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  64 b;.#endif /* 
1cf20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1cf21 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 68 20  moved into u.ah 
1cf22 2a 2f 0a 0a 20 20 69 66 28 20 28 70 49 6e 31 2d  */..  if( (pIn1-
1cf23 3e 66 6c 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66  >flags | pIn2->f
1cf24 6c 61 67 73 29 20 26 20 4d 45 4d 5f 4e 75 6c 6c  lags) & MEM_Null
1cf25 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
1cf26 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f  dbeMemSetNull(pO
1cf27 75 74 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a  ut);.    break;.
1cf28 20 20 7d 0a 20 20 75 2e 61 68 2e 61 20 3d 20 73    }.  u.ah.a = s
1cf29 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c  qlite3VdbeIntVal
1cf2a 75 65 28 70 49 6e 32 29 3b 0a 20 20 75 2e 61 68  ue(pIn2);.  u.ah
1cf2b 2e 62 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  .b = sqlite3Vdbe
1cf2c 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 3b 0a  IntValue(pIn1);.
1cf2d 20 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f    switch( pOp->o
1cf2e 70 63 6f 64 65 20 29 7b 0a 20 20 20 20 63 61 73  pcode ){.    cas
1cf2f 65 20 4f 50 5f 42 69 74 41 6e 64 3a 20 20 20 20  e OP_BitAnd:    
1cf30 20 20 75 2e 61 68 2e 61 20 26 3d 20 75 2e 61 68    u.ah.a &= u.ah
1cf31 2e 62 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  .b;     break;. 
1cf32 20 20 20 63 61 73 65 20 4f 50 5f 42 69 74 4f 72     case OP_BitOr
1cf33 3a 20 20 20 20 20 20 20 75 2e 61 68 2e 61 20 7c  :       u.ah.a |
1cf34 3d 20 75 2e 61 68 2e 62 3b 20 20 20 20 20 62 72  = u.ah.b;     br
1cf35 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50  eak;.    case OP
1cf36 5f 53 68 69 66 74 4c 65 66 74 3a 20 20 20 75 2e  _ShiftLeft:   u.
1cf37 61 68 2e 61 20 3c 3c 3d 20 75 2e 61 68 2e 62 3b  ah.a <<= u.ah.b;
1cf38 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64      break;.    d
1cf39 65 66 61 75 6c 74 3a 20 20 61 73 73 65 72 74 28  efault:  assert(
1cf3a 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
1cf3b 5f 53 68 69 66 74 52 69 67 68 74 20 29 3b 0a 20  _ShiftRight );. 
1cf3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cf3d 20 20 20 20 20 20 20 20 75 2e 61 68 2e 61 20 3e          u.ah.a >
1cf3e 3e 3d 20 75 2e 61 68 2e 62 3b 20 20 20 20 62 72  >= u.ah.b;    br
1cf3f 65 61 6b 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d  eak;.  }.  pOut-
1cf40 3e 75 2e 69 20 3d 20 75 2e 61 68 2e 61 3b 0a 20  >u.i = u.ah.a;. 
1cf41 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28   MemSetTypeFlag(
1cf42 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a  pOut, MEM_Int);.
1cf43 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1cf44 70 63 6f 64 65 3a 20 41 64 64 49 6d 6d 20 20 50  pcode: AddImm  P
1cf45 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20 0a 2a  1 P2 * * *.** .*
1cf46 2a 20 41 64 64 20 74 68 65 20 63 6f 6e 73 74 61  * Add the consta
1cf47 6e 74 20 50 32 20 74 6f 20 74 68 65 20 76 61 6c  nt P2 to the val
1cf48 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1cf49 31 2e 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74  1..** The result
1cf4a 20 69 73 20 61 6c 77 61 79 73 20 61 6e 20 69 6e   is always an in
1cf4b 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20  teger..**.** To 
1cf4c 66 6f 72 63 65 20 61 6e 79 20 72 65 67 69 73 74  force any regist
1cf4d 65 72 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65  er to be an inte
1cf4e 67 65 72 2c 20 6a 75 73 74 20 61 64 64 20 30 2e  ger, just add 0.
1cf4f 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 64 64 49  .*/.case OP_AddI
1cf50 6d 6d 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  mm: {           
1cf51 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 73 71 6c   /* in1 */.  sql
1cf52 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67  ite3VdbeMemInteg
1cf53 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 70  erify(pIn1);.  p
1cf54 49 6e 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70 2d  In1->u.i += pOp-
1cf55 3e 70 32 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  >p2;.  break;.}.
1cf56 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d 75 73 74  ./* Opcode: Must
1cf57 42 65 49 6e 74 20 50 31 20 50 32 20 2a 20 2a 20  BeInt P1 P2 * * 
1cf58 2a 0a 2a 2a 20 0a 2a 2a 20 46 6f 72 63 65 20 74  *.** .** Force t
1cf59 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1cf5a 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 61 6e  ster P1 to be an
1cf5b 20 69 6e 74 65 67 65 72 2e 20 20 49 66 20 74 68   integer.  If th
1cf5c 65 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31  e value.** in P1
1cf5d 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67   is not an integ
1cf5e 65 72 20 61 6e 64 20 63 61 6e 6e 6f 74 20 62 65  er and cannot be
1cf5f 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20   converted into 
1cf60 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 77 69  an integer.** wi
1cf61 74 68 6f 75 74 20 64 61 74 61 20 6c 6f 73 73 2c  thout data loss,
1cf62 20 74 68 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64   then jump immed
1cf63 69 61 74 65 6c 79 20 74 6f 20 50 32 2c 20 6f 72  iately to P2, or
1cf64 20 69 66 20 50 32 3d 3d 30 0a 2a 2a 20 72 61 69   if P2==0.** rai
1cf65 73 65 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53  se an SQLITE_MIS
1cf66 4d 41 54 43 48 20 65 78 63 65 70 74 69 6f 6e 2e  MATCH exception.
1cf67 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 75 73 74  .*/.case OP_Must
1cf68 42 65 49 6e 74 3a 20 7b 20 20 20 20 20 20 20 20  BeInt: {        
1cf69 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 31      /* jump, in1
1cf6a 20 2a 2f 0a 20 20 61 70 70 6c 79 41 66 66 69 6e   */.  applyAffin
1cf6b 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49 54 45  ity(pIn1, SQLITE
1cf6c 5f 41 46 46 5f 4e 55 4d 45 52 49 43 2c 20 65 6e  _AFF_NUMERIC, en
1cf6d 63 6f 64 69 6e 67 29 3b 0a 20 20 69 66 28 20 28  coding);.  if( (
1cf6e 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1cf6f 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20 20  M_Int)==0 ){.   
1cf70 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 30 20   if( pOp->p2==0 
1cf71 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
1cf72 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 3b 0a 20  LITE_MISMATCH;. 
1cf73 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f       goto abort_
1cf74 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
1cf75 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
1cf76 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1cf77 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
1cf78 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c      MemSetTypeFl
1cf79 61 67 28 70 49 6e 31 2c 20 4d 45 4d 5f 49 6e 74  ag(pIn1, MEM_Int
1cf7a 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  );.  }.  break;.
1cf7b 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65  }../* Opcode: Re
1cf7c 61 6c 41 66 66 69 6e 69 74 79 20 50 31 20 2a 20  alAffinity P1 * 
1cf7d 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 72  * * *.**.** If r
1cf7e 65 67 69 73 74 65 72 20 50 31 20 68 6f 6c 64 73  egister P1 holds
1cf7f 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 6e 76   an integer conv
1cf80 65 72 74 20 69 74 20 74 6f 20 61 20 72 65 61 6c  ert it to a real
1cf81 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   value..**.** Th
1cf82 69 73 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65  is opcode is use
1cf83 64 20 77 68 65 6e 20 65 78 74 72 61 63 74 69 6e  d when extractin
1cf84 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72  g information fr
1cf85 6f 6d 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74  om a column that
1cf86 0a 2a 2a 20 68 61 73 20 52 45 41 4c 20 61 66 66  .** has REAL aff
1cf87 69 6e 69 74 79 2e 20 20 53 75 63 68 20 63 6f 6c  inity.  Such col
1cf88 75 6d 6e 20 76 61 6c 75 65 73 20 6d 61 79 20 73  umn values may s
1cf89 74 69 6c 6c 20 62 65 20 73 74 6f 72 65 64 20 61  till be stored a
1cf8a 73 0a 2a 2a 20 69 6e 74 65 67 65 72 73 2c 20 66  s.** integers, f
1cf8b 6f 72 20 73 70 61 63 65 20 65 66 66 69 63 69 65  or space efficie
1cf8c 6e 63 79 2c 20 62 75 74 20 61 66 74 65 72 20 65  ncy, but after e
1cf8d 78 74 72 61 63 74 69 6f 6e 20 77 65 20 77 61 6e  xtraction we wan
1cf8e 74 20 74 68 65 6d 0a 2a 2a 20 74 6f 20 68 61 76  t them.** to hav
1cf8f 65 20 6f 6e 6c 79 20 61 20 72 65 61 6c 20 76 61  e only a real va
1cf90 6c 75 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  lue..*/.case OP_
1cf91 52 65 61 6c 41 66 66 69 6e 69 74 79 3a 20 7b 20  RealAffinity: { 
1cf92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cf93 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28   /* in1 */.  if(
1cf94 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d   pIn1->flags & M
1cf95 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 73 71  EM_Int ){.    sq
1cf96 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c  lite3VdbeMemReal
1cf97 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20  ify(pIn1);.  }. 
1cf98 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64   break;.}..#ifnd
1cf99 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
1cf9a 41 53 54 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54  AST./* Opcode: T
1cf9b 6f 54 65 78 74 20 50 31 20 2a 20 2a 20 2a 20 2a  oText P1 * * * *
1cf9c 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65  .**.** Force the
1cf9d 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1cf9e 65 72 20 50 31 20 74 6f 20 62 65 20 74 65 78 74  er P1 to be text
1cf9f 2e 0a 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75  ..** If the valu
1cfa0 65 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 63 6f  e is numeric, co
1cfa1 6e 76 65 72 74 20 69 74 20 74 6f 20 61 20 73 74  nvert it to a st
1cfa2 72 69 6e 67 20 75 73 69 6e 67 20 74 68 65 0a 2a  ring using the.*
1cfa3 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f 66 20  * equivalent of 
1cfa4 70 72 69 6e 74 66 28 29 2e 20 20 42 6c 6f 62 20  printf().  Blob 
1cfa5 76 61 6c 75 65 73 20 61 72 65 20 75 6e 63 68 61  values are uncha
1cfa6 6e 67 65 64 20 61 6e 64 0a 2a 2a 20 61 72 65 20  nged and.** are 
1cfa7 61 66 74 65 72 77 61 72 64 73 20 73 69 6d 70 6c  afterwards simpl
1cfa8 79 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73  y interpreted as
1cfa9 20 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e   text..**.** A N
1cfaa 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f 74  ULL value is not
1cfab 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69 73   changed by this
1cfac 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72 65   routine.  It re
1cfad 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63  mains NULL..*/.c
1cfae 61 73 65 20 4f 50 5f 54 6f 54 65 78 74 3a 20 7b  ase OP_ToText: {
1cfaf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cfb0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
1cfb1 54 4f 5f 54 45 58 54 2c 20 69 6e 31 20 2a 2f 0a  TO_TEXT, in1 */.
1cfb2 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67    if( pIn1->flag
1cfb3 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62  s & MEM_Null ) b
1cfb4 72 65 61 6b 3b 0a 20 20 61 73 73 65 72 74 28 20  reak;.  assert( 
1cfb5 4d 45 4d 5f 53 74 72 3d 3d 28 4d 45 4d 5f 42 6c  MEM_Str==(MEM_Bl
1cfb6 6f 62 3e 3e 33 29 20 29 3b 0a 20 20 70 49 6e 31  ob>>3) );.  pIn1
1cfb7 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 49 6e 31  ->flags |= (pIn1
1cfb8 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 42 6c 6f 62  ->flags&MEM_Blob
1cfb9 29 3e 3e 33 3b 0a 20 20 61 70 70 6c 79 41 66 66  )>>3;.  applyAff
1cfba 69 6e 69 74 79 28 70 49 6e 31 2c 20 53 51 4c 49  inity(pIn1, SQLI
1cfbb 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 65 6e 63  TE_AFF_TEXT, enc
1cfbc 6f 64 69 6e 67 29 3b 0a 20 20 72 63 20 3d 20 45  oding);.  rc = E
1cfbd 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 31 29 3b  xpandBlob(pIn1);
1cfbe 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 2d  .  assert( pIn1-
1cfbf 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53 74 72  >flags & MEM_Str
1cfc0 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
1cfc1 69 6c 65 64 20 29 3b 0a 20 20 70 49 6e 31 2d 3e  iled );.  pIn1->
1cfc2 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 49  flags &= ~(MEM_I
1cfc3 6e 74 7c 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d 5f  nt|MEM_Real|MEM_
1cfc4 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 29 3b 0a  Blob|MEM_Zero);.
1cfc5 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f    UPDATE_MAX_BLO
1cfc6 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 62  BSIZE(pIn1);.  b
1cfc7 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1cfc8 64 65 3a 20 54 6f 42 6c 6f 62 20 50 31 20 2a 20  de: ToBlob P1 * 
1cfc9 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63  * * *.**.** Forc
1cfca 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  e the value in r
1cfcb 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65  egister P1 to be
1cfcc 20 61 20 42 4c 4f 42 2e 0a 2a 2a 20 49 66 20 74   a BLOB..** If t
1cfcd 68 65 20 76 61 6c 75 65 20 69 73 20 6e 75 6d 65  he value is nume
1cfce 72 69 63 2c 20 63 6f 6e 76 65 72 74 20 69 74 20  ric, convert it 
1cfcf 74 6f 20 61 20 73 74 72 69 6e 67 20 66 69 72 73  to a string firs
1cfd0 74 2e 0a 2a 2a 20 53 74 72 69 6e 67 73 20 61 72  t..** Strings ar
1cfd1 65 20 73 69 6d 70 6c 79 20 72 65 69 6e 74 65 72  e simply reinter
1cfd2 70 72 65 74 65 64 20 61 73 20 62 6c 6f 62 73 20  preted as blobs 
1cfd3 77 69 74 68 20 6e 6f 20 63 68 61 6e 67 65 0a 2a  with no change.*
1cfd4 2a 20 74 6f 20 74 68 65 20 75 6e 64 65 72 6c 79  * to the underly
1cfd5 69 6e 67 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20  ing data..**.** 
1cfd6 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20  A NULL value is 
1cfd7 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74  not changed by t
1cfd8 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74  his routine.  It
1cfd9 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a   remains NULL..*
1cfda 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 42 6c 6f 62  /.case OP_ToBlob
1cfdb 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  : {             
1cfdc 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1cfdd 54 4b 5f 54 4f 5f 42 4c 4f 42 2c 20 69 6e 31 20  TK_TO_BLOB, in1 
1cfde 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66  */.  if( pIn1->f
1cfdf 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20  lags & MEM_Null 
1cfe0 29 20 62 72 65 61 6b 3b 0a 20 20 69 66 28 20 28  ) break;.  if( (
1cfe1 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1cfe2 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 7b 0a 20 20  M_Blob)==0 ){.  
1cfe3 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28    applyAffinity(
1cfe4 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46  pIn1, SQLITE_AFF
1cfe5 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67 29  _TEXT, encoding)
1cfe6 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49  ;.    assert( pI
1cfe7 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n1->flags & MEM_
1cfe8 53 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Str || db->mallo
1cfe9 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 4d  cFailed );.    M
1cfea 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 49  emSetTypeFlag(pI
1cfeb 6e 31 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20  n1, MEM_Blob);. 
1cfec 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 31   }else{.    pIn1
1cfed 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d  ->flags &= ~(MEM
1cfee 5f 54 79 70 65 4d 61 73 6b 26 7e 4d 45 4d 5f 42  _TypeMask&~MEM_B
1cfef 6c 6f 62 29 3b 0a 20 20 7d 0a 20 20 55 50 44 41  lob);.  }.  UPDA
1cff0 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28  TE_MAX_BLOBSIZE(
1cff1 70 49 6e 31 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  pIn1);.  break;.
1cff2 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f  }../* Opcode: To
1cff3 4e 75 6d 65 72 69 63 20 50 31 20 2a 20 2a 20 2a  Numeric P1 * * *
1cff4 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20 74   *.**.** Force t
1cff5 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1cff6 73 74 65 72 20 50 31 20 74 6f 20 62 65 20 6e 75  ster P1 to be nu
1cff7 6d 65 72 69 63 20 28 65 69 74 68 65 72 20 61 6e  meric (either an
1cff8 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6f 72 20 61  .** integer or a
1cff9 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20   floating-point 
1cffa 6e 75 6d 62 65 72 2e 29 0a 2a 2a 20 49 66 20 74  number.).** If t
1cffb 68 65 20 76 61 6c 75 65 20 69 73 20 74 65 78 74  he value is text
1cffc 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f   or blob, try to
1cffd 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61   convert it to a
1cffe 6e 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65  n using the.** e
1cfff 71 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f  quivalent of ato
1d000 69 28 29 20 6f 72 20 61 74 6f 66 28 29 20 61 6e  i() or atof() an
1d001 64 20 73 74 6f 72 65 20 30 20 69 66 20 6e 6f 20  d store 0 if no 
1d002 73 75 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20  such conversion 
1d003 0a 2a 2a 20 69 73 20 70 6f 73 73 69 62 6c 65 2e  .** is possible.
1d004 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61  .**.** A NULL va
1d005 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67  lue is not chang
1d006 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69  ed by this routi
1d007 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20  ne.  It remains 
1d008 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  NULL..*/.case OP
1d009 5f 54 6f 4e 75 6d 65 72 69 63 3a 20 7b 20 20 20  _ToNumeric: {   
1d00a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1d00b 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f  * same as TK_TO_
1d00c 4e 55 4d 45 52 49 43 2c 20 69 6e 31 20 2a 2f 0a  NUMERIC, in1 */.
1d00d 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61    if( (pIn1->fla
1d00e 67 73 20 26 20 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d  gs & (MEM_Null|M
1d00f 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29  EM_Int|MEM_Real)
1d010 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  )==0 ){.    sqli
1d011 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69  te3VdbeMemNumeri
1d012 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20  fy(pIn1);.  }.  
1d013 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
1d014 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  /* SQLITE_OMIT_C
1d015 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64  AST */../* Opcod
1d016 65 3a 20 54 6f 49 6e 74 20 50 31 20 2a 20 2a 20  e: ToInt P1 * * 
1d017 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63 65 20  * *.**.** Force 
1d018 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1d019 69 73 74 65 72 20 50 31 20 62 65 20 61 6e 20 69  ister P1 be an i
1d01a 6e 74 65 67 65 72 2e 20 20 49 66 0a 2a 2a 20 54  nteger.  If.** T
1d01b 68 65 20 76 61 6c 75 65 20 69 73 20 63 75 72 72  he value is curr
1d01c 65 6e 74 6c 79 20 61 20 72 65 61 6c 20 6e 75 6d  ently a real num
1d01d 62 65 72 2c 20 64 72 6f 70 20 69 74 73 20 66 72  ber, drop its fr
1d01e 61 63 74 69 6f 6e 61 6c 20 70 61 72 74 2e 0a 2a  actional part..*
1d01f 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69  * If the value i
1d020 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c 20  s text or blob, 
1d021 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20 69  try to convert i
1d022 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20  t to an integer 
1d023 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75  using the.** equ
1d024 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69 28  ivalent of atoi(
1d025 29 20 61 6e 64 20 73 74 6f 72 65 20 30 20 69 66  ) and store 0 if
1d026 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76 65 72 73   no such convers
1d027 69 6f 6e 20 69 73 20 70 6f 73 73 69 62 6c 65 2e  ion is possible.
1d028 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61  .**.** A NULL va
1d029 6c 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67  lue is not chang
1d02a 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69  ed by this routi
1d02b 6e 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20  ne.  It remains 
1d02c 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  NULL..*/.case OP
1d02d 5f 54 6f 49 6e 74 3a 20 7b 20 20 20 20 20 20 20  _ToInt: {       
1d02e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
1d02f 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 49 4e 54 2c  me as TK_TO_INT,
1d030 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70   in1 */.  if( (p
1d031 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
1d032 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20  _Null)==0 ){.   
1d033 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49   sqlite3VdbeMemI
1d034 6e 74 65 67 65 72 69 66 79 28 70 49 6e 31 29 3b  ntegerify(pIn1);
1d035 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1d036 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1d037 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a 20 4f 70 63  OMIT_CAST./* Opc
1d038 6f 64 65 3a 20 54 6f 52 65 61 6c 20 50 31 20 2a  ode: ToReal P1 *
1d039 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72   * * *.**.** For
1d03a 63 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  ce the value in 
1d03b 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62  register P1 to b
1d03c 65 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  e a floating poi
1d03d 6e 74 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20 49 66  nt number..** If
1d03e 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 63 75   The value is cu
1d03f 72 72 65 6e 74 6c 79 20 61 6e 20 69 6e 74 65 67  rrently an integ
1d040 65 72 2c 20 63 6f 6e 76 65 72 74 20 69 74 2e 0a  er, convert it..
1d041 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20  ** If the value 
1d042 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c  is text or blob,
1d043 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20   try to convert 
1d044 69 74 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72  it to an integer
1d045 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71   using the.** eq
1d046 75 69 76 61 6c 65 6e 74 20 6f 66 20 61 74 6f 69  uivalent of atoi
1d047 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30 2e 30  () and store 0.0
1d048 20 69 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76   if no such conv
1d049 65 72 73 69 6f 6e 20 69 73 20 70 6f 73 73 69 62  ersion is possib
1d04a 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c  le..**.** A NULL
1d04b 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 63 68   value is not ch
1d04c 61 6e 67 65 64 20 62 79 20 74 68 69 73 20 72 6f  anged by this ro
1d04d 75 74 69 6e 65 2e 20 20 49 74 20 72 65 6d 61 69  utine.  It remai
1d04e 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65  ns NULL..*/.case
1d04f 20 4f 50 5f 54 6f 52 65 61 6c 3a 20 7b 20 20 20   OP_ToReal: {   
1d050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1d051 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f  * same as TK_TO_
1d052 52 45 41 4c 2c 20 69 6e 31 20 2a 2f 0a 20 20 69  REAL, in1 */.  i
1d053 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20  f( (pIn1->flags 
1d054 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29  & MEM_Null)==0 )
1d055 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1d056 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49 6e 31  eMemRealify(pIn1
1d057 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  );.  }.  break;.
1d058 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1d059 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a  TE_OMIT_CAST */.
1d05a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4c 74 20 50  ./* Opcode: Lt P
1d05b 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a  1 P2 P3 P4 P5.**
1d05c 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20  .** Compare the 
1d05d 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74  values in regist
1d05e 65 72 20 50 31 20 61 6e 64 20 50 33 2e 20 20 49  er P1 and P3.  I
1d05f 66 20 72 65 67 28 50 33 29 3c 72 65 67 28 50 31  f reg(P3)<reg(P1
1d060 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70 20 74  ) then.** jump t
1d061 6f 20 61 64 64 72 65 73 73 20 50 32 2e 20 20 0a  o address P2.  .
1d062 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c  **.** If the SQL
1d063 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 62  ITE_JUMPIFNULL b
1d064 69 74 20 6f 66 20 50 35 20 69 73 20 73 65 74 20  it of P5 is set 
1d065 61 6e 64 20 65 69 74 68 65 72 20 72 65 67 28 50  and either reg(P
1d066 31 29 20 6f 72 0a 2a 2a 20 72 65 67 28 50 33 29  1) or.** reg(P3)
1d067 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61   is NULL then ta
1d068 6b 65 20 74 68 65 20 6a 75 6d 70 2e 20 20 49 66  ke the jump.  If
1d069 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50   the SQLITE_JUMP
1d06a 49 46 4e 55 4c 4c 20 0a 2a 2a 20 62 69 74 20 69  IFNULL .** bit i
1d06b 73 20 63 6c 65 61 72 20 74 68 65 6e 20 66 61 6c  s clear then fal
1d06c 6c 20 74 68 72 75 20 69 66 20 65 69 74 68 65 72  l thru if either
1d06d 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c   operand is NULL
1d06e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ..**.** The SQLI
1d06f 54 45 5f 41 46 46 5f 4d 41 53 4b 20 70 6f 72 74  TE_AFF_MASK port
1d070 69 6f 6e 20 6f 66 20 50 35 20 6d 75 73 74 20 62  ion of P5 must b
1d071 65 20 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68  e an affinity ch
1d072 61 72 61 63 74 65 72 20 2d 0a 2a 2a 20 53 51 4c  aracter -.** SQL
1d073 49 54 45 5f 41 46 46 5f 54 45 58 54 2c 20 53 51  ITE_AFF_TEXT, SQ
1d074 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
1d075 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20  , and so forth. 
1d076 41 6e 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61  An attempt is ma
1d077 64 65 20 0a 2a 2a 20 74 6f 20 63 6f 65 72 63 65  de .** to coerce
1d078 20 62 6f 74 68 20 69 6e 70 75 74 73 20 61 63 63   both inputs acc
1d079 6f 72 64 69 6e 67 20 74 6f 20 74 68 69 73 20 61  ording to this a
1d07a 66 66 69 6e 69 74 79 20 62 65 66 6f 72 65 20 74  ffinity before t
1d07b 68 65 0a 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e  he.** comparison
1d07c 20 69 73 20 6d 61 64 65 2e 20 49 66 20 74 68 65   is made. If the
1d07d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b   SQLITE_AFF_MASK
1d07e 20 69 73 20 30 78 30 30 2c 20 74 68 65 6e 20 6e   is 0x00, then n
1d07f 75 6d 65 72 69 63 0a 2a 2a 20 61 66 66 69 6e 69  umeric.** affini
1d080 74 79 20 69 73 20 75 73 65 64 2e 20 4e 6f 74 65  ty is used. Note
1d081 20 74 68 61 74 20 74 68 65 20 61 66 66 69 6e 69   that the affini
1d082 74 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61  ty conversions a
1d083 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 62 61 63  re stored.** bac
1d084 6b 20 69 6e 74 6f 20 74 68 65 20 69 6e 70 75 74  k into the input
1d085 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e   registers P1 an
1d086 64 20 50 33 2e 20 20 53 6f 20 74 68 69 73 20 6f  d P3.  So this o
1d087 70 63 6f 64 65 20 63 61 6e 20 63 61 75 73 65 0a  pcode can cause.
1d088 2a 2a 20 70 65 72 73 69 73 74 65 6e 74 20 63 68  ** persistent ch
1d089 61 6e 67 65 73 20 74 6f 20 72 65 67 69 73 74 65  anges to registe
1d08a 72 73 20 50 31 20 61 6e 64 20 50 33 2e 0a 2a 2a  rs P1 and P3..**
1d08b 0a 2a 2a 20 4f 6e 63 65 20 61 6e 79 20 63 6f 6e  .** Once any con
1d08c 76 65 72 73 69 6f 6e 73 20 68 61 76 65 20 74 61  versions have ta
1d08d 6b 65 6e 20 70 6c 61 63 65 2c 20 61 6e 64 20 6e  ken place, and n
1d08e 65 69 74 68 65 72 20 76 61 6c 75 65 20 69 73 20  either value is 
1d08f 4e 55 4c 4c 2c 20 0a 2a 2a 20 74 68 65 20 76 61  NULL, .** the va
1d090 6c 75 65 73 20 61 72 65 20 63 6f 6d 70 61 72 65  lues are compare
1d091 64 2e 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65  d. If both value
1d092 73 20 61 72 65 20 62 6c 6f 62 73 20 74 68 65 6e  s are blobs then
1d093 20 6d 65 6d 63 6d 70 28 29 20 69 73 0a 2a 2a 20   memcmp() is.** 
1d094 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e  used to determin
1d095 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66  e the results of
1d096 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e   the comparison.
1d097 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73    If both values
1d098 0a 2a 2a 20 61 72 65 20 74 65 78 74 2c 20 74 68  .** are text, th
1d099 65 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  en the appropria
1d09a 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e  te collating fun
1d09b 63 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20  ction specified 
1d09c 69 6e 0a 2a 2a 20 50 34 20 69 73 20 20 75 73 65  in.** P4 is  use
1d09d 64 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70  d to do the comp
1d09e 61 72 69 73 6f 6e 2e 20 20 49 66 20 50 34 20 69  arison.  If P4 i
1d09f 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20  s not specified 
1d0a0 74 68 65 6e 0a 2a 2a 20 6d 65 6d 63 6d 70 28 29  then.** memcmp()
1d0a1 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70   is used to comp
1d0a2 61 72 65 20 74 65 78 74 20 73 74 72 69 6e 67 2e  are text string.
1d0a3 20 20 49 66 20 62 6f 74 68 20 76 61 6c 75 65 73    If both values
1d0a4 20 61 72 65 0a 2a 2a 20 6e 75 6d 65 72 69 63 2c   are.** numeric,
1d0a5 20 74 68 65 6e 20 61 20 6e 75 6d 65 72 69 63 20   then a numeric 
1d0a6 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 75 73  comparison is us
1d0a7 65 64 2e 20 49 66 20 74 68 65 20 74 77 6f 20 76  ed. If the two v
1d0a8 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 6f 66 20  alues.** are of 
1d0a9 64 69 66 66 65 72 65 6e 74 20 74 79 70 65 73 2c  different types,
1d0aa 20 74 68 65 6e 20 6e 75 6d 62 65 72 73 20 61 72   then numbers ar
1d0ab 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73  e considered les
1d0ac 73 20 74 68 61 6e 0a 2a 2a 20 73 74 72 69 6e 67  s than.** string
1d0ad 73 20 61 6e 64 20 73 74 72 69 6e 67 73 20 61 72  s and strings ar
1d0ae 65 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65 73  e considered les
1d0af 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a 2a 2a  s than blobs..**
1d0b0 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49 54  .** If the SQLIT
1d0b1 45 5f 53 54 4f 52 45 50 32 20 62 69 74 20 6f 66  E_STOREP2 bit of
1d0b2 20 50 35 20 69 73 20 73 65 74 2c 20 74 68 65 6e   P5 is set, then
1d0b3 20 64 6f 20 6e 6f 74 20 6a 75 6d 70 2e 20 20 49   do not jump.  I
1d0b4 6e 73 74 65 61 64 2c 0a 2a 2a 20 73 74 6f 72 65  nstead,.** store
1d0b5 20 61 20 62 6f 6f 6c 65 61 6e 20 72 65 73 75 6c   a boolean resul
1d0b6 74 20 28 65 69 74 68 65 72 20 30 2c 20 6f 72 20  t (either 0, or 
1d0b7 31 2c 20 6f 72 20 4e 55 4c 4c 29 20 69 6e 20 72  1, or NULL) in r
1d0b8 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 2f  egister P2..*/./
1d0b9 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 20 50 31 20  * Opcode: Ne P1 
1d0ba 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a  P2 P3 P4 P5.**.*
1d0bb 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73  * This works jus
1d0bc 74 20 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70  t like the Lt op
1d0bd 63 6f 64 65 20 65 78 63 65 70 74 20 74 68 61 74  code except that
1d0be 20 74 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b   the jump is tak
1d0bf 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65  en if.** the ope
1d0c0 72 61 6e 64 73 20 69 6e 20 72 65 67 69 73 74 65  rands in registe
1d0c1 72 73 20 50 31 20 61 6e 64 20 50 33 20 61 72 65  rs P1 and P3 are
1d0c2 20 6e 6f 74 20 65 71 75 61 6c 2e 20 20 53 65 65   not equal.  See
1d0c3 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66   the Lt opcode f
1d0c4 6f 72 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c  or.** additional
1d0c5 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a   information..**
1d0c6 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 4e 55  .** If SQLITE_NU
1d0c7 4c 4c 45 51 20 69 73 20 73 65 74 20 69 6e 20 50  LLEQ is set in P
1d0c8 35 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  5 then the resul
1d0c9 74 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e 20  t of comparison 
1d0ca 69 73 20 61 6c 77 61 79 73 20 65 69 74 68 65 72  is always either
1d0cb 0a 2a 2a 20 74 72 75 65 20 6f 72 20 66 61 6c 73  .** true or fals
1d0cc 65 20 61 6e 64 20 69 73 20 6e 65 76 65 72 20 4e  e and is never N
1d0cd 55 4c 4c 2e 20 20 49 66 20 62 6f 74 68 20 6f 70  ULL.  If both op
1d0ce 65 72 61 6e 64 73 20 61 72 65 20 4e 55 4c 4c 20  erands are NULL 
1d0cf 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 0a  then the result.
1d0d0 2a 2a 20 6f 66 20 63 6f 6d 70 61 72 69 73 6f 6e  ** of comparison
1d0d1 20 69 73 20 66 61 6c 73 65 2e 20 20 49 66 20 65   is false.  If e
1d0d2 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73  ither operand is
1d0d3 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 72   NULL then the r
1d0d4 65 73 75 6c 74 20 69 73 20 74 72 75 65 2e 0a 2a  esult is true..*
1d0d5 2a 20 49 66 20 6e 65 69 74 68 65 72 20 6f 70 65  * If neither ope
1d0d6 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68 65  rand is NULL the
1d0d7 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 74   the result is t
1d0d8 68 65 20 73 61 6d 65 20 61 73 20 69 74 20 77 6f  he same as it wo
1d0d9 75 6c 64 20 62 65 20 69 66 0a 2a 2a 20 74 68 65  uld be if.** the
1d0da 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 66   SQLITE_NULLEQ f
1d0db 6c 61 67 20 77 65 72 65 20 6f 6d 69 74 74 65 64  lag were omitted
1d0dc 20 66 72 6f 6d 20 50 35 2e 0a 2a 2f 0a 2f 2a 20   from P5..*/./* 
1d0dd 4f 70 63 6f 64 65 3a 20 45 71 20 50 31 20 50 32  Opcode: Eq P1 P2
1d0de 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20   P3 P4 P5.**.** 
1d0df 54 68 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20  This works just 
1d0e0 6c 69 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f  like the Lt opco
1d0e1 64 65 20 65 78 63 65 70 74 20 74 68 61 74 20 74  de except that t
1d0e2 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e  he jump is taken
1d0e3 20 69 66 0a 2a 2a 20 74 68 65 20 6f 70 65 72 61   if.** the opera
1d0e4 6e 64 73 20 69 6e 20 72 65 67 69 73 74 65 72 73  nds in registers
1d0e5 20 50 31 20 61 6e 64 20 50 33 20 61 72 65 20 65   P1 and P3 are e
1d0e6 71 75 61 6c 2e 0a 2a 2a 20 53 65 65 20 74 68 65  qual..** See the
1d0e7 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61   Lt opcode for a
1d0e8 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
1d0e9 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ation..**.** If 
1d0ea 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 20 69 73  SQLITE_NULLEQ is
1d0eb 20 73 65 74 20 69 6e 20 50 35 20 74 68 65 6e 20   set in P5 then 
1d0ec 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 63 6f  the result of co
1d0ed 6d 70 61 72 69 73 6f 6e 20 69 73 20 61 6c 77 61  mparison is alwa
1d0ee 79 73 20 65 69 74 68 65 72 0a 2a 2a 20 74 72 75  ys either.** tru
1d0ef 65 20 6f 72 20 66 61 6c 73 65 20 61 6e 64 20 69  e or false and i
1d0f0 73 20 6e 65 76 65 72 20 4e 55 4c 4c 2e 20 20 49  s never NULL.  I
1d0f1 66 20 62 6f 74 68 20 6f 70 65 72 61 6e 64 73 20  f both operands 
1d0f2 61 72 65 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68  are NULL then th
1d0f3 65 20 72 65 73 75 6c 74 0a 2a 2a 20 6f 66 20 63  e result.** of c
1d0f4 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 72 75  omparison is tru
1d0f5 65 2e 20 20 49 66 20 65 69 74 68 65 72 20 6f 70  e.  If either op
1d0f6 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 74 68  erand is NULL th
1d0f7 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  en the result is
1d0f8 20 66 61 6c 73 65 2e 0a 2a 2a 20 49 66 20 6e 65   false..** If ne
1d0f9 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73  ither operand is
1d0fa 20 4e 55 4c 4c 20 74 68 65 20 74 68 65 20 72 65   NULL the the re
1d0fb 73 75 6c 74 20 69 73 20 74 68 65 20 73 61 6d 65  sult is the same
1d0fc 20 61 73 20 69 74 20 77 6f 75 6c 64 20 62 65 20   as it would be 
1d0fd 69 66 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45  if.** the SQLITE
1d0fe 5f 4e 55 4c 4c 45 51 20 66 6c 61 67 20 77 65 72  _NULLEQ flag wer
1d0ff 65 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 20 50  e omitted from P
1d100 35 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  5..*/./* Opcode:
1d101 20 4c 65 20 50 31 20 50 32 20 50 33 20 50 34 20   Le P1 P2 P3 P4 
1d102 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f  P5.**.** This wo
1d103 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68  rks just like th
1d104 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65  e Lt opcode exce
1d105 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70  pt that the jump
1d106 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20   is taken if.** 
1d107 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72  the content of r
1d108 65 67 69 73 74 65 72 20 50 33 20 69 73 20 6c 65  egister P3 is le
1d109 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  ss than or equal
1d10a 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20   to the content 
1d10b 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50  of.** register P
1d10c 31 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f  1.  See the Lt o
1d10d 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69  pcode for additi
1d10e 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
1d10f 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
1d110 47 74 20 50 31 20 50 32 20 50 33 20 50 34 20 50  Gt P1 P2 P3 P4 P
1d111 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72  5.**.** This wor
1d112 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65  ks just like the
1d113 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70   Lt opcode excep
1d114 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20  t that the jump 
1d115 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74  is taken if.** t
1d116 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65  he content of re
1d117 67 69 73 74 65 72 20 50 33 20 69 73 20 67 72 65  gister P3 is gre
1d118 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 6f  ater than the co
1d119 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69  ntent of.** regi
1d11a 73 74 65 72 20 50 31 2e 20 20 53 65 65 20 74 68  ster P1.  See th
1d11b 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20  e Lt opcode for 
1d11c 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
1d11d 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70  mation..*/./* Op
1d11e 63 6f 64 65 3a 20 47 65 20 50 31 20 50 32 20 50  code: Ge P1 P2 P
1d11f 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68  3 P4 P5.**.** Th
1d120 69 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69  is works just li
1d121 6b 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65  ke the Lt opcode
1d122 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65   except that the
1d123 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69   jump is taken i
1d124 66 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74  f.** the content
1d125 20 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20   of register P3 
1d126 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
1d127 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20  or equal to the 
1d128 63 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65  content of.** re
1d129 67 69 73 74 65 72 20 50 31 2e 20 20 53 65 65 20  gister P1.  See 
1d12a 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f  the Lt opcode fo
1d12b 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
1d12c 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73  ormation..*/.cas
1d12d 65 20 4f 50 5f 45 71 3a 20 20 20 20 20 20 20 20  e OP_Eq:        
1d12e 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1d12f 73 20 54 4b 5f 45 51 2c 20 6a 75 6d 70 2c 20 69  s TK_EQ, jump, i
1d130 6e 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20  n1, in3 */.case 
1d131 4f 50 5f 4e 65 3a 20 20 20 20 20 20 20 20 20 20  OP_Ne:          
1d132 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1d133 54 4b 5f 4e 45 2c 20 6a 75 6d 70 2c 20 69 6e 31  TK_NE, jump, in1
1d134 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50  , in3 */.case OP
1d135 5f 4c 74 3a 20 20 20 20 20 20 20 20 20 20 20 20  _Lt:            
1d136 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
1d137 5f 4c 54 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20  _LT, jump, in1, 
1d138 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c  in3 */.case OP_L
1d139 65 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e:              
1d13a 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c   /* same as TK_L
1d13b 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e  E, jump, in1, in
1d13c 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 74 3a  3 */.case OP_Gt:
1d13d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1d13e 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 2c  * same as TK_GT,
1d13f 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20   jump, in1, in3 
1d140 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 65 3a 20 7b  */.case OP_Ge: {
1d141 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d142 73 61 6d 65 20 61 73 20 54 4b 5f 47 45 2c 20 6a  same as TK_GE, j
1d143 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f  ump, in1, in3 */
1d144 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1d145 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1d146 20 69 6e 74 6f 20 75 2e 61 69 20 2a 2f 0a 20 20   into u.ai */.  
1d147 69 6e 74 20 72 65 73 3b 20 20 20 20 20 20 20 20  int res;        
1d148 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66      /* Result of
1d149 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
1d14a 6f 66 20 70 49 6e 31 20 61 67 61 69 6e 73 74 20  of pIn1 against 
1d14b 70 49 6e 33 20 2a 2f 0a 20 20 63 68 61 72 20 61  pIn3 */.  char a
1d14c 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a  ffinity;      /*
1d14d 20 41 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65   Affinity to use
1d14e 20 66 6f 72 20 63 6f 6d 70 61 72 69 73 6f 6e 20   for comparison 
1d14f 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  */.#endif /* loc
1d150 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1d151 65 64 20 69 6e 74 6f 20 75 2e 61 69 20 2a 2f 0a  ed into u.ai */.
1d152 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c  .  if( (pIn1->fl
1d153 61 67 73 20 7c 20 70 49 6e 33 2d 3e 66 6c 61 67  ags | pIn3->flag
1d154 73 29 26 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20  s)&MEM_Null ){. 
1d155 20 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 62 6f 74     /* One or bot
1d156 68 20 6f 70 65 72 61 6e 64 73 20 61 72 65 20 4e  h operands are N
1d157 55 4c 4c 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  ULL */.    if( p
1d158 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f  Op->p5 & SQLITE_
1d159 4e 55 4c 4c 45 51 20 29 7b 0a 20 20 20 20 20 20  NULLEQ ){.      
1d15a 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 4e 55 4c  /* If SQLITE_NUL
1d15b 4c 45 51 20 69 73 20 73 65 74 20 28 77 68 69 63  LEQ is set (whic
1d15c 68 20 77 69 6c 6c 20 6f 6e 6c 79 20 68 61 70 70  h will only happ
1d15d 65 6e 20 69 66 20 74 68 65 20 6f 70 65 72 61 74  en if the operat
1d15e 6f 72 20 69 73 0a 20 20 20 20 20 20 2a 2a 20 4f  or is.      ** O
1d15f 50 5f 45 71 20 6f 72 20 4f 50 5f 4e 65 29 20 74  P_Eq or OP_Ne) t
1d160 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d  hen take the jum
1d161 70 20 6f 72 20 6e 6f 74 20 64 65 70 65 6e 64 69  p or not dependi
1d162 6e 67 20 6f 6e 20 77 68 65 74 68 65 72 0a 20 20  ng on whether.  
1d163 20 20 20 20 2a 2a 20 6f 72 20 6e 6f 74 20 62 6f      ** or not bo
1d164 74 68 20 6f 70 65 72 61 6e 64 73 20 61 72 65 20  th operands are 
1d165 6e 75 6c 6c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  null..      */. 
1d166 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
1d167 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 45 71 20  ->opcode==OP_Eq 
1d168 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  || pOp->opcode==
1d169 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20 75  OP_Ne );.      u
1d16a 2e 61 69 2e 72 65 73 20 3d 20 28 70 49 6e 31 2d  .ai.res = (pIn1-
1d16b 3e 66 6c 61 67 73 20 26 20 70 49 6e 33 2d 3e 66  >flags & pIn3->f
1d16c 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29  lags & MEM_Null)
1d16d 3d 3d 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ==0;.    }else{.
1d16e 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f        /* SQLITE_
1d16f 4e 55 4c 4c 45 51 20 69 73 20 63 6c 65 61 72 20  NULLEQ is clear 
1d170 61 6e 64 20 61 74 20 6c 65 61 73 74 20 6f 6e 65  and at least one
1d171 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c   operand is NULL
1d172 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20  ,.      ** then 
1d173 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 6c  the result is al
1d174 77 61 79 73 20 4e 55 4c 4c 2e 0a 20 20 20 20 20  ways NULL..     
1d175 20 2a 2a 20 54 68 65 20 6a 75 6d 70 20 69 73 20   ** The jump is 
1d176 74 61 6b 65 6e 20 69 66 20 74 68 65 20 53 51 4c  taken if the SQL
1d177 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 62  ITE_JUMPIFNULL b
1d178 69 74 20 69 73 20 73 65 74 2e 0a 20 20 20 20 20  it is set..     
1d179 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 4f   */.      if( pO
1d17a 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 53  p->p5 & SQLITE_S
1d17b 54 4f 52 45 50 32 20 29 7b 0a 20 20 20 20 20 20  TOREP2 ){.      
1d17c 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65    pOut = &p->aMe
1d17d 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20  m[pOp->p2];.    
1d17e 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c      MemSetTypeFl
1d17f 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e 75 6c  ag(pOut, MEM_Nul
1d180 6c 29 3b 0a 20 20 20 20 20 20 20 20 52 45 47 49  l);.        REGI
1d181 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
1d182 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 20  p2, pOut);.     
1d183 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e   }else if( pOp->
1d184 70 35 20 26 20 53 51 4c 49 54 45 5f 4a 55 4d 50  p5 & SQLITE_JUMP
1d185 49 46 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20  IFNULL ){.      
1d186 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31    pc = pOp->p2-1
1d187 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1d188 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
1d189 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4e 65 69  else{.    /* Nei
1d18a 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20  ther operand is 
1d18b 4e 55 4c 4c 2e 20 20 44 6f 20 61 20 63 6f 6d 70  NULL.  Do a comp
1d18c 61 72 69 73 6f 6e 2e 20 2a 2f 0a 20 20 20 20 75  arison. */.    u
1d18d 2e 61 69 2e 61 66 66 69 6e 69 74 79 20 3d 20 70  .ai.affinity = p
1d18e 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45 5f  Op->p5 & SQLITE_
1d18f 41 46 46 5f 4d 41 53 4b 3b 0a 20 20 20 20 69 66  AFF_MASK;.    if
1d190 28 20 75 2e 61 69 2e 61 66 66 69 6e 69 74 79 20  ( u.ai.affinity 
1d191 29 7b 0a 20 20 20 20 20 20 61 70 70 6c 79 41 66  ){.      applyAf
1d192 66 69 6e 69 74 79 28 70 49 6e 31 2c 20 75 2e 61  finity(pIn1, u.a
1d193 69 2e 61 66 66 69 6e 69 74 79 2c 20 65 6e 63 6f  i.affinity, enco
1d194 64 69 6e 67 29 3b 0a 20 20 20 20 20 20 61 70 70  ding);.      app
1d195 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e 33 2c  lyAffinity(pIn3,
1d196 20 75 2e 61 69 2e 61 66 66 69 6e 69 74 79 2c 20   u.ai.affinity, 
1d197 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 20  encoding);.     
1d198 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
1d199 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6e 6f 5f  ailed ) goto no_
1d19a 6d 65 6d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  mem;.    }..    
1d19b 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74  assert( pOp->p4t
1d19c 79 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20  ype==P4_COLLSEQ 
1d19d 7c 7c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c  || pOp->p4.pColl
1d19e 3d 3d 30 20 29 3b 0a 20 20 20 20 45 78 70 61 6e  ==0 );.    Expan
1d19f 64 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 20  dBlob(pIn1);.   
1d1a0 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 33   ExpandBlob(pIn3
1d1a1 29 3b 0a 20 20 20 20 75 2e 61 69 2e 72 65 73 20  );.    u.ai.res 
1d1a2 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70  = sqlite3MemComp
1d1a3 61 72 65 28 70 49 6e 33 2c 20 70 49 6e 31 2c 20  are(pIn3, pIn1, 
1d1a4 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 29 3b 0a  pOp->p4.pColl);.
1d1a5 20 20 7d 0a 20 20 73 77 69 74 63 68 28 20 70 4f    }.  switch( pO
1d1a6 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20  p->opcode ){.   
1d1a7 20 63 61 73 65 20 4f 50 5f 45 71 3a 20 20 20 20   case OP_Eq:    
1d1a8 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e  u.ai.res = u.ai.
1d1a9 72 65 73 3d 3d 30 3b 20 20 20 20 20 62 72 65 61  res==0;     brea
1d1aa 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4e  k;.    case OP_N
1d1ab 65 3a 20 20 20 20 75 2e 61 69 2e 72 65 73 20 3d  e:    u.ai.res =
1d1ac 20 75 2e 61 69 2e 72 65 73 21 3d 30 3b 20 20 20   u.ai.res!=0;   
1d1ad 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
1d1ae 65 20 4f 50 5f 4c 74 3a 20 20 20 20 75 2e 61 69  e OP_Lt:    u.ai
1d1af 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65 73 3c  .res = u.ai.res<
1d1b0 30 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  0;      break;. 
1d1b1 20 20 20 63 61 73 65 20 4f 50 5f 4c 65 3a 20 20     case OP_Le:  
1d1b2 20 20 75 2e 61 69 2e 72 65 73 20 3d 20 75 2e 61    u.ai.res = u.a
1d1b3 69 2e 72 65 73 3c 3d 30 3b 20 20 20 20 20 62 72  i.res<=0;     br
1d1b4 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50  eak;.    case OP
1d1b5 5f 47 74 3a 20 20 20 20 75 2e 61 69 2e 72 65 73  _Gt:    u.ai.res
1d1b6 20 3d 20 75 2e 61 69 2e 72 65 73 3e 30 3b 20 20   = u.ai.res>0;  
1d1b7 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64      break;.    d
1d1b8 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20 75 2e  efault:       u.
1d1b9 61 69 2e 72 65 73 20 3d 20 75 2e 61 69 2e 72 65  ai.res = u.ai.re
1d1ba 73 3e 3d 30 3b 20 20 20 20 20 62 72 65 61 6b 3b  s>=0;     break;
1d1bb 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4f 70 2d  .  }..  if( pOp-
1d1bc 3e 70 35 20 26 20 53 51 4c 49 54 45 5f 53 54 4f  >p5 & SQLITE_STO
1d1bd 52 45 50 32 20 29 7b 0a 20 20 20 20 70 4f 75 74  REP2 ){.    pOut
1d1be 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1d1bf 3e 70 32 5d 3b 0a 20 20 20 20 4d 65 6d 53 65 74  >p2];.    MemSet
1d1c0 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d  TypeFlag(pOut, M
1d1c1 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 70 4f 75  EM_Int);.    pOu
1d1c2 74 2d 3e 75 2e 69 20 3d 20 75 2e 61 69 2e 72 65  t->u.i = u.ai.re
1d1c3 73 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f  s;.    REGISTER_
1d1c4 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70  TRACE(pOp->p2, p
1d1c5 4f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  Out);.  }else if
1d1c6 28 20 75 2e 61 69 2e 72 65 73 20 29 7b 0a 20 20  ( u.ai.res ){.  
1d1c7 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31    pc = pOp->p2-1
1d1c8 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1d1c9 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 65 72  ../* Opcode: Per
1d1ca 6d 75 74 61 74 69 6f 6e 20 2a 20 2a 20 2a 20 50  mutation * * * P
1d1cb 34 20 2a 0a 2a 2a 0a 2a 2a 20 53 65 74 20 74 68  4 *.**.** Set th
1d1cc 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 75 73  e permutation us
1d1cd 65 64 20 62 79 20 74 68 65 20 4f 50 5f 43 6f 6d  ed by the OP_Com
1d1ce 70 61 72 65 20 6f 70 65 72 61 74 6f 72 20 74 6f  pare operator to
1d1cf 20 62 65 20 74 68 65 20 61 72 72 61 79 0a 2a 2a   be the array.**
1d1d0 20 6f 66 20 69 6e 74 65 67 65 72 73 20 69 6e 20   of integers in 
1d1d1 50 34 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 65  P4..**.** The pe
1d1d2 72 6d 75 74 61 74 69 6f 6e 20 69 73 20 6f 6e 6c  rmutation is onl
1d1d3 79 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68  y valid until th
1d1d4 65 20 6e 65 78 74 20 4f 50 5f 50 65 72 6d 75 74  e next OP_Permut
1d1d5 61 74 69 6f 6e 2c 20 4f 50 5f 43 6f 6d 70 61 72  ation, OP_Compar
1d1d6 65 2c 0a 2a 2a 20 4f 50 5f 48 61 6c 74 2c 20 6f  e,.** OP_Halt, o
1d1d7 72 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2e 20  r OP_ResultRow. 
1d1d8 20 54 79 70 69 63 61 6c 6c 79 20 74 68 65 20 4f   Typically the O
1d1d9 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 20 73 68  P_Permutation sh
1d1da 6f 75 6c 64 20 6f 63 63 75 72 0a 2a 2a 20 69 6d  ould occur.** im
1d1db 6d 65 64 69 61 74 65 6c 79 20 70 72 69 6f 72 20  mediately prior 
1d1dc 74 6f 20 74 68 65 20 4f 50 5f 43 6f 6d 70 61 72  to the OP_Compar
1d1dd 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 65  e..*/.case OP_Pe
1d1de 72 6d 75 74 61 74 69 6f 6e 3a 20 7b 0a 20 20 61  rmutation: {.  a
1d1df 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79  ssert( pOp->p4ty
1d1e0 70 65 3d 3d 50 34 5f 49 4e 54 41 52 52 41 59 20  pe==P4_INTARRAY 
1d1e1 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
1d1e2 2d 3e 70 34 2e 61 69 20 29 3b 0a 20 20 61 50 65  ->p4.ai );.  aPe
1d1e3 72 6d 75 74 65 20 3d 20 70 4f 70 2d 3e 70 34 2e  rmute = pOp->p4.
1d1e4 61 69 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  ai;.  break;.}..
1d1e5 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6d 70 61  /* Opcode: Compa
1d1e6 72 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a  re P1 P2 P3 P4 *
1d1e7 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74  .**.** Compare t
1d1e8 6f 20 76 65 63 74 6f 72 73 20 6f 66 20 72 65 67  o vectors of reg
1d1e9 69 73 74 65 72 73 20 69 6e 20 72 65 67 28 50 31  isters in reg(P1
1d1ea 29 2e 2e 72 65 67 28 50 31 2b 50 33 2d 31 29 20  )..reg(P1+P3-1) 
1d1eb 28 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 6f 6e 65  (all this.** one
1d1ec 20 22 41 22 29 20 61 6e 64 20 69 6e 20 72 65 67   "A") and in reg
1d1ed 28 50 32 29 2e 2e 72 65 67 28 50 32 2b 50 33 2d  (P2)..reg(P2+P3-
1d1ee 31 29 20 28 22 42 22 29 2e 20 20 53 61 76 65 20  1) ("B").  Save 
1d1ef 74 68 65 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a  the result of.**
1d1f0 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
1d1f1 66 6f 72 20 75 73 65 20 62 79 20 74 68 65 20 6e  for use by the n
1d1f2 65 78 74 20 4f 50 5f 4a 75 6d 70 20 69 6e 73 74  ext OP_Jump inst
1d1f3 72 75 63 74 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69  ruct..**.** P4 i
1d1f4 73 20 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75  s a KeyInfo stru
1d1f5 63 74 75 72 65 20 74 68 61 74 20 64 65 66 69 6e  cture that defin
1d1f6 65 73 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  es collating seq
1d1f7 75 65 6e 63 65 73 20 61 6e 64 20 73 6f 72 74 0a  uences and sort.
1d1f8 2a 2a 20 6f 72 64 65 72 73 20 66 6f 72 20 74 68  ** orders for th
1d1f9 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20 54  e comparison.  T
1d1fa 68 65 20 70 65 72 6d 75 74 61 74 69 6f 6e 20 61  he permutation a
1d1fb 70 70 6c 69 65 73 20 74 6f 20 72 65 67 69 73 74  pplies to regist
1d1fc 65 72 73 0a 2a 2a 20 6f 6e 6c 79 2e 20 20 54 68  ers.** only.  Th
1d1fd 65 20 4b 65 79 49 6e 66 6f 20 65 6c 65 6d 65 6e  e KeyInfo elemen
1d1fe 74 73 20 61 72 65 20 75 73 65 64 20 73 65 71 75  ts are used sequ
1d1ff 65 6e 74 69 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a 20  entially..**.** 
1d200 54 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69  The comparison i
1d201 73 20 61 20 73 6f 72 74 20 63 6f 6d 70 61 72 69  s a sort compari
1d202 73 6f 6e 2c 20 73 6f 20 4e 55 4c 4c 73 20 63 6f  son, so NULLs co
1d203 6d 70 61 72 65 20 65 71 75 61 6c 2c 0a 2a 2a 20  mpare equal,.** 
1d204 4e 55 4c 4c 73 20 61 72 65 20 6c 65 73 73 20 74  NULLs are less t
1d205 68 61 6e 20 6e 75 6d 62 65 72 73 2c 20 6e 75 6d  han numbers, num
1d206 62 65 72 73 20 61 72 65 20 6c 65 73 73 20 74 68  bers are less th
1d207 61 6e 20 73 74 72 69 6e 67 73 2c 0a 2a 2a 20 61  an strings,.** a
1d208 6e 64 20 73 74 72 69 6e 67 73 20 61 72 65 20 6c  nd strings are l
1d209 65 73 73 20 74 68 61 6e 20 62 6c 6f 62 73 2e 0a  ess than blobs..
1d20a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6d 70 61  */.case OP_Compa
1d20b 72 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20  re: {.#if 0  /* 
1d20c 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1d20d 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6a 20  moved into u.aj 
1d20e 2a 2f 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 69 6e  */.  int n;.  in
1d20f 74 20 69 3b 0a 20 20 69 6e 74 20 70 31 3b 0a 20  t i;.  int p1;. 
1d210 20 69 6e 74 20 70 32 3b 0a 20 20 63 6f 6e 73 74   int p2;.  const
1d211 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
1d212 66 6f 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20  fo;.  int idx;. 
1d213 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
1d214 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69 6e 67      /* Collating
1d215 20 73 65 71 75 65 6e 63 65 20 74 6f 20 75 73 65   sequence to use
1d216 20 6f 6e 20 74 68 69 73 20 74 65 72 6d 20 2a 2f   on this term */
1d217 0a 20 20 69 6e 74 20 62 52 65 76 3b 20 20 20 20  .  int bRev;    
1d218 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f        /* True fo
1d219 72 20 44 45 53 43 45 4e 44 49 4e 47 20 73 6f 72  r DESCENDING sor
1d21a 74 20 6f 72 64 65 72 20 2a 2f 0a 23 65 6e 64 69  t order */.#endi
1d21b 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1d21c 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1d21d 75 2e 61 6a 20 2a 2f 0a 0a 20 20 75 2e 61 6a 2e  u.aj */..  u.aj.
1d21e 6e 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 75  n = pOp->p3;.  u
1d21f 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70  .aj.pKeyInfo = p
1d220 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b  Op->p4.pKeyInfo;
1d221 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61 6a 2e  .  assert( u.aj.
1d222 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  n>0 );.  assert(
1d223 20 75 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f 21 3d   u.aj.pKeyInfo!=
1d224 30 20 29 3b 0a 20 20 75 2e 61 6a 2e 70 31 20 3d  0 );.  u.aj.p1 =
1d225 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 6a   pOp->p1;.  u.aj
1d226 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 23  .p2 = pOp->p2;.#
1d227 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  if SQLITE_DEBUG.
1d228 20 20 69 66 28 20 61 50 65 72 6d 75 74 65 20 29    if( aPermute )
1d229 7b 0a 20 20 20 20 69 6e 74 20 6b 2c 20 6d 78 20  {.    int k, mx 
1d22a 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 6b 3d 30  = 0;.    for(k=0
1d22b 3b 20 6b 3c 75 2e 61 6a 2e 6e 3b 20 6b 2b 2b 29  ; k<u.aj.n; k++)
1d22c 20 69 66 28 20 61 50 65 72 6d 75 74 65 5b 6b 5d   if( aPermute[k]
1d22d 3e 6d 78 20 29 20 6d 78 20 3d 20 61 50 65 72 6d  >mx ) mx = aPerm
1d22e 75 74 65 5b 6b 5d 3b 0a 20 20 20 20 61 73 73 65  ute[k];.    asse
1d22f 72 74 28 20 75 2e 61 6a 2e 70 31 3e 30 20 26 26  rt( u.aj.p1>0 &&
1d230 20 75 2e 61 6a 2e 70 31 2b 6d 78 3c 3d 70 2d 3e   u.aj.p1+mx<=p->
1d231 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 20 20 61 73  nMem+1 );.    as
1d232 73 65 72 74 28 20 75 2e 61 6a 2e 70 32 3e 30 20  sert( u.aj.p2>0 
1d233 26 26 20 75 2e 61 6a 2e 70 32 2b 6d 78 3c 3d 70  && u.aj.p2+mx<=p
1d234 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 7d 65  ->nMem+1 );.  }e
1d235 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
1d236 20 75 2e 61 6a 2e 70 31 3e 30 20 26 26 20 75 2e   u.aj.p1>0 && u.
1d237 61 6a 2e 70 31 2b 75 2e 61 6a 2e 6e 3c 3d 70 2d  aj.p1+u.aj.n<=p-
1d238 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 20 20 61  >nMem+1 );.    a
1d239 73 73 65 72 74 28 20 75 2e 61 6a 2e 70 32 3e 30  ssert( u.aj.p2>0
1d23a 20 26 26 20 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a   && u.aj.p2+u.aj
1d23b 2e 6e 3c 3d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b  .n<=p->nMem+1 );
1d23c 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
1d23d 51 4c 49 54 45 5f 44 45 42 55 47 20 2a 2f 0a 20  QLITE_DEBUG */. 
1d23e 20 66 6f 72 28 75 2e 61 6a 2e 69 3d 30 3b 20 75   for(u.aj.i=0; u
1d23f 2e 61 6a 2e 69 3c 75 2e 61 6a 2e 6e 3b 20 75 2e  .aj.i<u.aj.n; u.
1d240 61 6a 2e 69 2b 2b 29 7b 0a 20 20 20 20 75 2e 61  aj.i++){.    u.a
1d241 6a 2e 69 64 78 20 3d 20 61 50 65 72 6d 75 74 65  j.idx = aPermute
1d242 20 3f 20 61 50 65 72 6d 75 74 65 5b 75 2e 61 6a   ? aPermute[u.aj
1d243 2e 69 5d 20 3a 20 75 2e 61 6a 2e 69 3b 0a 20 20  .i] : u.aj.i;.  
1d244 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1d245 28 75 2e 61 6a 2e 70 31 2b 75 2e 61 6a 2e 69 64  (u.aj.p1+u.aj.id
1d246 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 6a  x, &p->aMem[u.aj
1d247 2e 70 31 2b 75 2e 61 6a 2e 69 64 78 5d 29 3b 0a  .p1+u.aj.idx]);.
1d248 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52 41      REGISTER_TRA
1d249 43 45 28 75 2e 61 6a 2e 70 32 2b 75 2e 61 6a 2e  CE(u.aj.p2+u.aj.
1d24a 69 64 78 2c 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e  idx, &p->aMem[u.
1d24b 61 6a 2e 70 32 2b 75 2e 61 6a 2e 69 64 78 5d 29  aj.p2+u.aj.idx])
1d24c 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e  ;.    assert( u.
1d24d 61 6a 2e 69 3c 75 2e 61 6a 2e 70 4b 65 79 49 6e  aj.i<u.aj.pKeyIn
1d24e 66 6f 2d 3e 6e 46 69 65 6c 64 20 29 3b 0a 20 20  fo->nField );.  
1d24f 20 20 75 2e 61 6a 2e 70 43 6f 6c 6c 20 3d 20 75    u.aj.pColl = u
1d250 2e 61 6a 2e 70 4b 65 79 49 6e 66 6f 2d 3e 61 43  .aj.pKeyInfo->aC
1d251 6f 6c 6c 5b 75 2e 61 6a 2e 69 5d 3b 0a 20 20 20  oll[u.aj.i];.   
1d252 20 75 2e 61 6a 2e 62 52 65 76 20 3d 20 75 2e 61   u.aj.bRev = u.a
1d253 6a 2e 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72  j.pKeyInfo->aSor
1d254 74 4f 72 64 65 72 5b 75 2e 61 6a 2e 69 5d 3b 0a  tOrder[u.aj.i];.
1d255 20 20 20 20 69 43 6f 6d 70 61 72 65 20 3d 20 73      iCompare = s
1d256 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72 65  qlite3MemCompare
1d257 28 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 6a 2e 70  (&p->aMem[u.aj.p
1d258 31 2b 75 2e 61 6a 2e 69 64 78 5d 2c 20 26 70 2d  1+u.aj.idx], &p-
1d259 3e 61 4d 65 6d 5b 75 2e 61 6a 2e 70 32 2b 75 2e  >aMem[u.aj.p2+u.
1d25a 61 6a 2e 69 64 78 5d 2c 20 75 2e 61 6a 2e 70 43  aj.idx], u.aj.pC
1d25b 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 69 43  oll);.    if( iC
1d25c 6f 6d 70 61 72 65 20 29 7b 0a 20 20 20 20 20 20  ompare ){.      
1d25d 69 66 28 20 75 2e 61 6a 2e 62 52 65 76 20 29 20  if( u.aj.bRev ) 
1d25e 69 43 6f 6d 70 61 72 65 20 3d 20 2d 69 43 6f 6d  iCompare = -iCom
1d25f 70 61 72 65 3b 0a 20 20 20 20 20 20 62 72 65 61  pare;.      brea
1d260 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61  k;.    }.  }.  a
1d261 50 65 72 6d 75 74 65 20 3d 20 30 3b 0a 20 20 62  Permute = 0;.  b
1d262 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1d263 64 65 3a 20 4a 75 6d 70 20 50 31 20 50 32 20 50  de: Jump P1 P2 P
1d264 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70  3 * *.**.** Jump
1d265 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63 74   to the instruct
1d266 69 6f 6e 20 61 74 20 61 64 64 72 65 73 73 20 50  ion at address P
1d267 31 2c 20 50 32 2c 20 6f 72 20 50 33 20 64 65 70  1, P2, or P3 dep
1d268 65 6e 64 69 6e 67 20 6f 6e 20 77 68 65 74 68 65  ending on whethe
1d269 72 0a 2a 2a 20 69 6e 20 74 68 65 20 6d 6f 73 74  r.** in the most
1d26a 20 72 65 63 65 6e 74 20 4f 50 5f 43 6f 6d 70 61   recent OP_Compa
1d26b 72 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74  re instruction t
1d26c 68 65 20 50 31 20 76 65 63 74 6f 72 20 77 61 73  he P1 vector was
1d26d 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 65 71   less than.** eq
1d26e 75 61 6c 20 74 6f 2c 20 6f 72 20 67 72 65 61 74  ual to, or great
1d26f 65 72 20 74 68 61 6e 20 74 68 65 20 50 32 20 76  er than the P2 v
1d270 65 63 74 6f 72 2c 20 72 65 73 70 65 63 74 69 76  ector, respectiv
1d271 65 6c 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ely..*/.case OP_
1d272 4a 75 6d 70 3a 20 7b 20 20 20 20 20 20 20 20 20  Jump: {         
1d273 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 20      /* jump */. 
1d274 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3c 30 20   if( iCompare<0 
1d275 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  ){.    pc = pOp-
1d276 3e 70 31 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65  >p1 - 1;.  }else
1d277 20 69 66 28 20 69 43 6f 6d 70 61 72 65 3d 3d 30   if( iCompare==0
1d278 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70   ){.    pc = pOp
1d279 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 65 6c 73  ->p2 - 1;.  }els
1d27a 65 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  e{.    pc = pOp-
1d27b 3e 70 33 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p3 - 1;.  }.  b
1d27c 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1d27d 64 65 3a 20 41 6e 64 20 50 31 20 50 32 20 50 33  de: And P1 P2 P3
1d27e 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20   * *.**.** Take 
1d27f 74 68 65 20 6c 6f 67 69 63 61 6c 20 41 4e 44 20  the logical AND 
1d280 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e  of the values in
1d281 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e   registers P1 an
1d282 64 20 50 32 20 61 6e 64 0a 2a 2a 20 77 72 69 74  d P2 and.** writ
1d283 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74  e the result int
1d284 6f 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a  o register P3..*
1d285 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50  *.** If either P
1d286 31 20 6f 72 20 50 32 20 69 73 20 30 20 28 66 61  1 or P2 is 0 (fa
1d287 6c 73 65 29 20 74 68 65 6e 20 74 68 65 20 72 65  lse) then the re
1d288 73 75 6c 74 20 69 73 20 30 20 65 76 65 6e 20 69  sult is 0 even i
1d289 66 0a 2a 2a 20 74 68 65 20 6f 74 68 65 72 20 69  f.** the other i
1d28a 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2e 20 20 41  nput is NULL.  A
1d28b 20 4e 55 4c 4c 20 61 6e 64 20 74 72 75 65 20 6f   NULL and true o
1d28c 72 20 74 77 6f 20 4e 55 4c 4c 73 20 67 69 76 65  r two NULLs give
1d28d 0a 2a 2a 20 61 20 4e 55 4c 4c 20 6f 75 74 70 75  .** a NULL outpu
1d28e 74 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  t..*/./* Opcode:
1d28f 20 4f 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a   Or P1 P2 P3 * *
1d290 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68 65 20  .**.** Take the 
1d291 6c 6f 67 69 63 61 6c 20 4f 52 20 6f 66 20 74 68  logical OR of th
1d292 65 20 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69  e values in regi
1d293 73 74 65 72 20 50 31 20 61 6e 64 20 50 32 20 61  ster P1 and P2 a
1d294 6e 64 0a 2a 2a 20 73 74 6f 72 65 20 74 68 65 20  nd.** store the 
1d295 61 6e 73 77 65 72 20 69 6e 20 72 65 67 69 73 74  answer in regist
1d296 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  er P3..**.** If 
1d297 65 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20  either P1 or P2 
1d298 69 73 20 6e 6f 6e 7a 65 72 6f 20 28 74 72 75 65  is nonzero (true
1d299 29 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  ) then the resul
1d29a 74 20 69 73 20 31 20 28 74 72 75 65 29 0a 2a 2a  t is 1 (true).**
1d29b 20 65 76 65 6e 20 69 66 20 74 68 65 20 6f 74 68   even if the oth
1d29c 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c  er input is NULL
1d29d 2e 20 20 41 20 4e 55 4c 4c 20 61 6e 64 20 66 61  .  A NULL and fa
1d29e 6c 73 65 20 6f 72 20 74 77 6f 20 4e 55 4c 4c 73  lse or two NULLs
1d29f 0a 2a 2a 20 67 69 76 65 20 61 20 4e 55 4c 4c 20  .** give a NULL 
1d2a0 6f 75 74 70 75 74 2e 0a 2a 2f 0a 63 61 73 65 20  output..*/.case 
1d2a1 4f 50 5f 41 6e 64 3a 20 20 20 20 20 20 20 20 20  OP_And:         
1d2a2 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1d2a3 54 4b 5f 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32  TK_AND, in1, in2
1d2a4 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f  , out3 */.case O
1d2a5 50 5f 4f 72 3a 20 7b 20 20 20 20 20 20 20 20 20  P_Or: {         
1d2a6 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1d2a7 4b 5f 4f 52 2c 20 69 6e 31 2c 20 69 6e 32 2c 20  K_OR, in1, in2, 
1d2a8 6f 75 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f  out3 */.#if 0  /
1d2a9 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1d2aa 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1d2ab 6b 20 2a 2f 0a 20 20 69 6e 74 20 76 31 3b 20 20  k */.  int v1;  
1d2ac 20 20 2f 2a 20 4c 65 66 74 20 6f 70 65 72 61 6e    /* Left operan
1d2ad 64 3a 20 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d  d:  0==FALSE, 1=
1d2ae 3d 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57  =TRUE, 2==UNKNOW
1d2af 4e 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69  N or NULL */.  i
1d2b0 6e 74 20 76 32 3b 20 20 20 20 2f 2a 20 52 69 67  nt v2;    /* Rig
1d2b1 68 74 20 6f 70 65 72 61 6e 64 3a 20 30 3d 3d 46  ht operand: 0==F
1d2b2 41 4c 53 45 2c 20 31 3d 3d 54 52 55 45 2c 20 32  ALSE, 1==TRUE, 2
1d2b3 3d 3d 55 4e 4b 4e 4f 57 4e 20 6f 72 20 4e 55 4c  ==UNKNOWN or NUL
1d2b4 4c 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  L */.#endif /* l
1d2b5 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1d2b6 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6b 20 2a  oved into u.ak *
1d2b7 2f 0a 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66  /..  if( pIn1->f
1d2b8 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20  lags & MEM_Null 
1d2b9 29 7b 0a 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d  ){.    u.ak.v1 =
1d2ba 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   2;.  }else{.   
1d2bb 20 75 2e 61 6b 2e 76 31 20 3d 20 73 71 6c 69 74   u.ak.v1 = sqlit
1d2bc 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70  e3VdbeIntValue(p
1d2bd 49 6e 31 29 21 3d 30 3b 0a 20 20 7d 0a 20 20 69  In1)!=0;.  }.  i
1d2be 66 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26  f( pIn2->flags &
1d2bf 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20   MEM_Null ){.   
1d2c0 20 75 2e 61 6b 2e 76 32 20 3d 20 32 3b 0a 20 20   u.ak.v2 = 2;.  
1d2c1 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 6b 2e  }else{.    u.ak.
1d2c2 76 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  v2 = sqlite3Vdbe
1d2c3 49 6e 74 56 61 6c 75 65 28 70 49 6e 32 29 21 3d  IntValue(pIn2)!=
1d2c4 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70  0;.  }.  if( pOp
1d2c5 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 41 6e 64  ->opcode==OP_And
1d2c6 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63   ){.    static c
1d2c7 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
1d2c8 61 72 20 61 6e 64 5f 6c 6f 67 69 63 5b 5d 20 3d  ar and_logic[] =
1d2c9 20 7b 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31   { 0, 0, 0, 0, 1
1d2ca 2c 20 32 2c 20 30 2c 20 32 2c 20 32 20 7d 3b 0a  , 2, 0, 2, 2 };.
1d2cb 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20 61 6e      u.ak.v1 = an
1d2cc 64 5f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76 31 2a  d_logic[u.ak.v1*
1d2cd 33 2b 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20 7d 65  3+u.ak.v2];.  }e
1d2ce 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63 20  lse{.    static 
1d2cf 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
1d2d0 68 61 72 20 6f 72 5f 6c 6f 67 69 63 5b 5d 20 3d  har or_logic[] =
1d2d1 20 7b 20 30 2c 20 31 2c 20 32 2c 20 31 2c 20 31   { 0, 1, 2, 1, 1
1d2d2 2c 20 31 2c 20 32 2c 20 31 2c 20 32 20 7d 3b 0a  , 1, 2, 1, 2 };.
1d2d3 20 20 20 20 75 2e 61 6b 2e 76 31 20 3d 20 6f 72      u.ak.v1 = or
1d2d4 5f 6c 6f 67 69 63 5b 75 2e 61 6b 2e 76 31 2a 33  _logic[u.ak.v1*3
1d2d5 2b 75 2e 61 6b 2e 76 32 5d 3b 0a 20 20 7d 0a 20  +u.ak.v2];.  }. 
1d2d6 20 69 66 28 20 75 2e 61 6b 2e 76 31 3d 3d 32 20   if( u.ak.v1==2 
1d2d7 29 7b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70  ){.    MemSetTyp
1d2d8 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f  eFlag(pOut, MEM_
1d2d9 4e 75 6c 6c 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Null);.  }else{.
1d2da 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20      pOut->u.i = 
1d2db 75 2e 61 6b 2e 76 31 3b 0a 20 20 20 20 4d 65 6d  u.ak.v1;.    Mem
1d2dc 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74  SetTypeFlag(pOut
1d2dd 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 0a  , MEM_Int);.  }.
1d2de 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1d2df 70 63 6f 64 65 3a 20 4e 6f 74 20 50 31 20 50 32  pcode: Not P1 P2
1d2e0 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74   * * *.**.** Int
1d2e1 65 72 70 72 65 74 20 74 68 65 20 76 61 6c 75 65  erpret the value
1d2e2 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
1d2e3 61 73 20 61 20 62 6f 6f 6c 65 61 6e 20 76 61 6c  as a boolean val
1d2e4 75 65 2e 20 20 53 74 6f 72 65 20 74 68 65 0a 2a  ue.  Store the.*
1d2e5 2a 20 62 6f 6f 6c 65 61 6e 20 63 6f 6d 70 6c 65  * boolean comple
1d2e6 6d 65 6e 74 20 69 6e 20 72 65 67 69 73 74 65 72  ment in register
1d2e7 20 50 32 2e 20 20 49 66 20 74 68 65 20 76 61 6c   P2.  If the val
1d2e8 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1d2e9 31 20 69 73 20 0a 2a 2a 20 4e 55 4c 4c 2c 20 74  1 is .** NULL, t
1d2ea 68 65 6e 20 61 20 4e 55 4c 4c 20 69 73 20 73 74  hen a NULL is st
1d2eb 6f 72 65 64 20 69 6e 20 50 32 2e 0a 2a 2f 0a 63  ored in P2..*/.c
1d2ec 61 73 65 20 4f 50 5f 4e 6f 74 3a 20 7b 20 20 20  ase OP_Not: {   
1d2ed 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1d2ee 73 61 6d 65 20 61 73 20 54 4b 5f 4e 4f 54 2c 20  same as TK_NOT, 
1d2ef 69 6e 31 20 2a 2f 0a 20 20 70 4f 75 74 20 3d 20  in1 */.  pOut = 
1d2f0 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32  &p->aMem[pOp->p2
1d2f1 5d 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66  ];.  if( pIn1->f
1d2f2 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20  lags & MEM_Null 
1d2f3 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
1d2f4 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4f 75  beMemSetNull(pOu
1d2f5 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t);.  }else{.   
1d2f6 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1d2f7 65 74 49 6e 74 36 34 28 70 4f 75 74 2c 20 21 73  etInt64(pOut, !s
1d2f8 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c  qlite3VdbeIntVal
1d2f9 75 65 28 70 49 6e 31 29 29 3b 0a 20 20 7d 0a 20  ue(pIn1));.  }. 
1d2fa 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1d2fb 63 6f 64 65 3a 20 42 69 74 4e 6f 74 20 50 31 20  code: BitNot P1 
1d2fc 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49  P2 * * *.**.** I
1d2fd 6e 74 65 72 70 72 65 74 20 74 68 65 20 63 6f 6e  nterpret the con
1d2fe 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72  tent of register
1d2ff 20 50 31 20 61 73 20 61 6e 20 69 6e 74 65 67 65   P1 as an intege
1d300 72 2e 20 20 53 74 6f 72 65 20 74 68 65 0a 2a 2a  r.  Store the.**
1d301 20 6f 6e 65 73 2d 63 6f 6d 70 6c 65 6d 65 6e 74   ones-complement
1d302 20 6f 66 20 74 68 65 20 50 31 20 76 61 6c 75 65   of the P1 value
1d303 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50   into register P
1d304 32 2e 20 20 49 66 20 50 31 20 68 6f 6c 64 73 0a  2.  If P1 holds.
1d305 2a 2a 20 61 20 4e 55 4c 4c 20 74 68 65 6e 20 73  ** a NULL then s
1d306 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69 6e 20 50  tore a NULL in P
1d307 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69  2..*/.case OP_Bi
1d308 74 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 20  tNot: {         
1d309 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1d30a 4b 5f 42 49 54 4e 4f 54 2c 20 69 6e 31 20 2a 2f  K_BITNOT, in1 */
1d30b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d  .  pOut = &p->aM
1d30c 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 69  em[pOp->p2];.  i
1d30d 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  f( pIn1->flags &
1d30e 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20   MEM_Null ){.   
1d30f 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1d310 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20  etNull(pOut);.  
1d311 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
1d312 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74 36  e3VdbeMemSetInt6
1d313 34 28 70 4f 75 74 2c 20 7e 73 71 6c 69 74 65 33  4(pOut, ~sqlite3
1d314 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e  VdbeIntValue(pIn
1d315 31 29 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  1));.  }.  break
1d316 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1d317 49 66 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  If P1 P2 P3 * *.
1d318 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50 32  **.** Jump to P2
1d319 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69 6e   if the value in
1d31a 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73 20   register P1 is 
1d31b 74 72 75 65 2e 20 20 54 68 65 20 76 61 6c 75 65  true.  The value
1d31c 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64   is.** is consid
1d31d 65 72 65 64 20 74 72 75 65 20 69 66 20 69 74 20  ered true if it 
1d31e 69 73 20 6e 75 6d 65 72 69 63 20 61 6e 64 20 6e  is numeric and n
1d31f 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20 74 68 65  on-zero.  If the
1d320 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20   value.** in P1 
1d321 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b  is NULL then tak
1d322 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33  e the jump if P3
1d323 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 2f 2a 20   is true..*/./* 
1d324 4f 70 63 6f 64 65 3a 20 49 66 4e 6f 74 20 50 31  Opcode: IfNot P1
1d325 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1d326 20 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74   Jump to P2 if t
1d327 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69  he value in regi
1d328 73 74 65 72 20 50 31 20 69 73 20 46 61 6c 73 65  ster P1 is False
1d329 2e 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 0a  .  The value is.
1d32a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  ** is considered
1d32b 20 74 72 75 65 20 69 66 20 69 74 20 68 61 73 20   true if it has 
1d32c 61 20 6e 75 6d 65 72 69 63 20 76 61 6c 75 65 20  a numeric value 
1d32d 6f 66 20 7a 65 72 6f 2e 20 20 49 66 20 74 68 65  of zero.  If the
1d32e 20 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 50 31 20   value.** in P1 
1d32f 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 61 6b  is NULL then tak
1d330 65 20 74 68 65 20 6a 75 6d 70 20 69 66 20 50 33  e the jump if P3
1d331 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 63 61 73   is true..*/.cas
1d332 65 20 4f 50 5f 49 66 3a 20 20 20 20 20 20 20 20  e OP_If:        
1d333 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70           /* jump
1d334 2c 20 69 6e 31 20 2a 2f 0a 63 61 73 65 20 4f 50  , in1 */.case OP
1d335 5f 49 66 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20  _IfNot: {       
1d336 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1d337 31 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  1 */.#if 0  /* l
1d338 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1d339 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 6c 20 2a  oved into u.al *
1d33a 2f 0a 20 20 69 6e 74 20 63 3b 0a 23 65 6e 64 69  /.  int c;.#endi
1d33b 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1d33c 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1d33d 75 2e 61 6c 20 2a 2f 0a 20 20 69 66 28 20 70 49  u.al */.  if( pI
1d33e 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n1->flags & MEM_
1d33f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75 2e 61 6c  Null ){.    u.al
1d340 2e 63 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20  .c = pOp->p3;.  
1d341 7d 65 6c 73 65 7b 0a 23 69 66 64 65 66 20 53 51  }else{.#ifdef SQ
1d342 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
1d343 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 75 2e 61  NG_POINT.    u.a
1d344 6c 2e 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62  l.c = sqlite3Vdb
1d345 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 21  eIntValue(pIn1)!
1d346 3d 30 3b 0a 23 65 6c 73 65 0a 20 20 20 20 75 2e  =0;.#else.    u.
1d347 61 6c 2e 63 20 3d 20 73 71 6c 69 74 65 33 56 64  al.c = sqlite3Vd
1d348 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49 6e 31  beRealValue(pIn1
1d349 29 21 3d 30 2e 30 3b 0a 23 65 6e 64 69 66 0a 20  )!=0.0;.#endif. 
1d34a 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f     if( pOp->opco
1d34b 64 65 3d 3d 4f 50 5f 49 66 4e 6f 74 20 29 20 75  de==OP_IfNot ) u
1d34c 2e 61 6c 2e 63 20 3d 20 21 75 2e 61 6c 2e 63 3b  .al.c = !u.al.c;
1d34d 0a 20 20 7d 0a 20 20 69 66 28 20 75 2e 61 6c 2e  .  }.  if( u.al.
1d34e 63 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f  c ){.    pc = pO
1d34f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 20 62  p->p2-1;.  }.  b
1d350 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1d351 64 65 3a 20 49 73 4e 75 6c 6c 20 50 31 20 50 32  de: IsNull P1 P2
1d352 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d   * * *.**.** Jum
1d353 70 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76  p to P2 if the v
1d354 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1d355 20 50 31 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a   P1 is NULL..*/.
1d356 63 61 73 65 20 4f 50 5f 49 73 4e 75 6c 6c 3a 20  case OP_IsNull: 
1d357 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {            /* 
1d358 73 61 6d 65 20 61 73 20 54 4b 5f 49 53 4e 55 4c  same as TK_ISNUL
1d359 4c 2c 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a  L, jump, in1 */.
1d35a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61    if( (pIn1->fla
1d35b 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d  gs & MEM_Null)!=
1d35c 30 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f  0 ){.    pc = pO
1d35d 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20  p->p2 - 1;.  }. 
1d35e 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1d35f 63 6f 64 65 3a 20 4e 6f 74 4e 75 6c 6c 20 50 31  code: NotNull P1
1d360 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
1d361 4a 75 6d 70 20 74 6f 20 50 32 20 69 66 20 74 68  Jump to P2 if th
1d362 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
1d363 74 65 72 20 50 31 20 69 73 20 6e 6f 74 20 4e 55  ter P1 is not NU
1d364 4c 4c 2e 20 20 0a 2a 2f 0a 63 61 73 65 20 4f 50  LL.  .*/.case OP
1d365 5f 4e 6f 74 4e 75 6c 6c 3a 20 7b 20 20 20 20 20  _NotNull: {     
1d366 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
1d367 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 6a 75  s TK_NOTNULL, ju
1d368 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28  mp, in1 */.  if(
1d369 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20   (pIn1->flags & 
1d36a 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 7b 0a  MEM_Null)==0 ){.
1d36b 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
1d36c 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61   - 1;.  }.  brea
1d36d 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1d36e 20 43 6f 6c 75 6d 6e 20 50 31 20 50 32 20 50 33   Column P1 P2 P3
1d36f 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 49 6e 74   P4 P5.**.** Int
1d370 65 72 70 72 65 74 20 74 68 65 20 64 61 74 61 20  erpret the data 
1d371 74 68 61 74 20 63 75 72 73 6f 72 20 50 31 20 70  that cursor P1 p
1d372 6f 69 6e 74 73 20 74 6f 20 61 73 20 61 20 73 74  oints to as a st
1d373 72 75 63 74 75 72 65 20 62 75 69 6c 74 20 75 73  ructure built us
1d374 69 6e 67 0a 2a 2a 20 74 68 65 20 4d 61 6b 65 52  ing.** the MakeR
1d375 65 63 6f 72 64 20 69 6e 73 74 72 75 63 74 69 6f  ecord instructio
1d376 6e 2e 20 20 28 53 65 65 20 74 68 65 20 4d 61 6b  n.  (See the Mak
1d377 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20 66  eRecord opcode f
1d378 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a  or additional.**
1d379 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
1d37a 75 74 20 74 68 65 20 66 6f 72 6d 61 74 20 6f 66  ut the format of
1d37b 20 74 68 65 20 64 61 74 61 2e 29 20 20 45 78 74   the data.)  Ext
1d37c 72 61 63 74 20 74 68 65 20 50 32 2d 74 68 20 63  ract the P2-th c
1d37d 6f 6c 75 6d 6e 0a 2a 2a 20 66 72 6f 6d 20 74 68  olumn.** from th
1d37e 69 73 20 72 65 63 6f 72 64 2e 20 20 49 66 20 74  is record.  If t
1d37f 68 65 72 65 20 61 72 65 20 6c 65 73 73 20 74 68  here are less th
1d380 61 74 20 28 50 32 2b 31 29 20 0a 2a 2a 20 76 61  at (P2+1) .** va
1d381 6c 75 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f  lues in the reco
1d382 72 64 2c 20 65 78 74 72 61 63 74 20 61 20 4e 55  rd, extract a NU
1d383 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61  LL..**.** The va
1d384 6c 75 65 20 65 78 74 72 61 63 74 65 64 20 69 73  lue extracted is
1d385 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
1d386 74 65 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66  ter P3..**.** If
1d387 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74   the column cont
1d388 61 69 6e 73 20 66 65 77 65 72 20 74 68 61 6e 20  ains fewer than 
1d389 50 32 20 66 69 65 6c 64 73 2c 20 74 68 65 6e 20  P2 fields, then 
1d38a 65 78 74 72 61 63 74 20 61 20 4e 55 4c 4c 2e 20  extract a NULL. 
1d38b 20 4f 72 2c 0a 2a 2a 20 69 66 20 74 68 65 20 50   Or,.** if the P
1d38c 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 20  4 argument is a 
1d38d 50 34 5f 4d 45 4d 20 75 73 65 20 74 68 65 20 76  P4_MEM use the v
1d38e 61 6c 75 65 20 6f 66 20 74 68 65 20 50 34 20 61  alue of the P4 a
1d38f 72 67 75 6d 65 6e 74 20 61 73 0a 2a 2a 20 74 68  rgument as.** th
1d390 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20  e result..**.** 
1d391 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 43 4c  If the OPFLAG_CL
1d392 45 41 52 43 41 43 48 45 20 62 69 74 20 69 73 20  EARCACHE bit is 
1d393 73 65 74 20 6f 6e 20 50 35 20 61 6e 64 20 50 31  set on P5 and P1
1d394 20 69 73 20 61 20 70 73 65 75 64 6f 2d 74 61 62   is a pseudo-tab
1d395 6c 65 20 63 75 72 73 6f 72 2c 0a 2a 2a 20 74 68  le cursor,.** th
1d396 65 6e 20 74 68 65 20 63 61 63 68 65 20 6f 66 20  en the cache of 
1d397 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 72 65  the cursor is re
1d398 73 65 74 20 70 72 69 6f 72 20 74 6f 20 65 78 74  set prior to ext
1d399 72 61 63 74 69 6e 67 20 74 68 65 20 63 6f 6c 75  racting the colu
1d39a 6d 6e 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  mn..** The first
1d39b 20 4f 50 5f 43 6f 6c 75 6d 6e 20 61 67 61 69 6e   OP_Column again
1d39c 73 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c  st a pseudo-tabl
1d39d 65 20 61 66 74 65 72 20 74 68 65 20 76 61 6c 75  e after the valu
1d39e 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e 74  e of the content
1d39f 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 68 61 73  .** register has
1d3a0 20 63 68 61 6e 67 65 64 20 73 68 6f 75 6c 64 20   changed should 
1d3a1 68 61 76 65 20 74 68 69 73 20 62 69 74 20 73 65  have this bit se
1d3a2 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f  t..*/.case OP_Co
1d3a3 6c 75 6d 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f  lumn: {.#if 0  /
1d3a4 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1d3a5 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1d3a6 6d 20 2a 2f 0a 20 20 75 33 32 20 70 61 79 6c 6f  m */.  u32 paylo
1d3a7 61 64 53 69 7a 65 3b 20 20 20 2f 2a 20 4e 75 6d  adSize;   /* Num
1d3a8 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
1d3a9 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
1d3aa 69 36 34 20 70 61 79 6c 6f 61 64 53 69 7a 65 36  i64 payloadSize6
1d3ab 34 3b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20  4; /* Number of 
1d3ac 62 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 63  bytes in the rec
1d3ad 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 70 31 3b  ord */.  int p1;
1d3ae 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
1d3af 31 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6f  1 value of the o
1d3b0 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70  pcode */.  int p
1d3b1 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  2;            /*
1d3b2 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 74   column number t
1d3b3 6f 20 72 65 74 72 69 65 76 65 20 2a 2f 0a 20 20  o retrieve */.  
1d3b4 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20  VdbeCursor *pC; 
1d3b5 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63     /* The VDBE c
1d3b6 75 72 73 6f 72 20 2a 2f 0a 20 20 63 68 61 72 20  ursor */.  char 
1d3b7 2a 7a 52 65 63 3b 20 20 20 20 20 20 20 20 2f 2a  *zRec;        /*
1d3b8 20 50 6f 69 6e 74 65 72 20 74 6f 20 63 6f 6d 70   Pointer to comp
1d3b9 6c 65 74 65 20 72 65 63 6f 72 64 2d 64 61 74 61  lete record-data
1d3ba 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20 2a   */.  BtCursor *
1d3bb 70 43 72 73 72 3b 20 20 20 2f 2a 20 54 68 65 20  pCrsr;   /* The 
1d3bc 42 54 72 65 65 20 63 75 72 73 6f 72 20 2a 2f 0a  BTree cursor */.
1d3bd 20 20 75 33 32 20 2a 61 54 79 70 65 3b 20 20 20    u32 *aType;   
1d3be 20 20 20 20 20 2f 2a 20 61 54 79 70 65 5b 69 5d       /* aType[i]
1d3bf 20 68 6f 6c 64 73 20 74 68 65 20 6e 75 6d 65 72   holds the numer
1d3c0 69 63 20 74 79 70 65 20 6f 66 20 74 68 65 20 69  ic type of the i
1d3c1 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20  -th column */.  
1d3c2 75 33 32 20 2a 61 4f 66 66 73 65 74 3b 20 20 20  u32 *aOffset;   
1d3c3 20 20 20 2f 2a 20 61 4f 66 66 73 65 74 5b 69 5d     /* aOffset[i]
1d3c4 20 69 73 20 6f 66 66 73 65 74 20 74 6f 20 73 74   is offset to st
1d3c5 61 72 74 20 6f 66 20 64 61 74 61 20 66 6f 72 20  art of data for 
1d3c6 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20  i-th column */. 
1d3c7 20 69 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20   int nField;    
1d3c8 20 20 20 20 2f 2a 20 6e 75 6d 62 65 72 20 6f 66      /* number of
1d3c9 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 72   fields in the r
1d3ca 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6c  ecord */.  int l
1d3cb 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  en;           /*
1d3cc 20 54 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74   The length of t
1d3cd 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20 64 61  he serialized da
1d3ce 74 61 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d  ta for the colum
1d3cf 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  n */.  int i;   
1d3d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
1d3d1 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 63  p counter */.  c
1d3d2 68 61 72 20 2a 7a 44 61 74 61 3b 20 20 20 20 20  har *zData;     
1d3d3 20 20 2f 2a 20 50 61 72 74 20 6f 66 20 74 68 65    /* Part of the
1d3d4 20 72 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65   record being de
1d3d5 63 6f 64 65 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a  coded */.  Mem *
1d3d6 70 44 65 73 74 3b 20 20 20 20 20 20 20 20 2f 2a  pDest;        /*
1d3d7 20 57 68 65 72 65 20 74 6f 20 77 72 69 74 65 20   Where to write 
1d3d8 74 68 65 20 65 78 74 72 61 63 74 65 64 20 76 61  the extracted va
1d3d9 6c 75 65 20 2a 2f 0a 20 20 4d 65 6d 20 73 4d 65  lue */.  Mem sMe
1d3da 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  m;          /* F
1d3db 6f 72 20 73 74 6f 72 69 6e 67 20 74 68 65 20 72  or storing the r
1d3dc 65 63 6f 72 64 20 62 65 69 6e 67 20 64 65 63 6f  ecord being deco
1d3dd 64 65 64 20 2a 2f 0a 20 20 75 38 20 2a 7a 49 64  ded */.  u8 *zId
1d3de 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  x;          /* I
1d3df 6e 64 65 78 20 69 6e 74 6f 20 68 65 61 64 65 72  ndex into header
1d3e0 20 2a 2f 0a 20 20 75 38 20 2a 7a 45 6e 64 48 64   */.  u8 *zEndHd
1d3e1 72 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  r;       /* Poin
1d3e2 74 65 72 20 74 6f 20 66 69 72 73 74 20 62 79 74  ter to first byt
1d3e3 65 20 61 66 74 65 72 20 74 68 65 20 68 65 61 64  e after the head
1d3e4 65 72 20 2a 2f 0a 20 20 75 33 32 20 6f 66 66 73  er */.  u32 offs
1d3e5 65 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66  et;        /* Of
1d3e6 66 73 65 74 20 69 6e 74 6f 20 74 68 65 20 64 61  fset into the da
1d3e7 74 61 20 2a 2f 0a 20 20 75 36 34 20 6f 66 66 73  ta */.  u64 offs
1d3e8 65 74 36 34 3b 20 20 20 20 20 20 2f 2a 20 36 34  et64;      /* 64
1d3e9 2d 62 69 74 20 6f 66 66 73 65 74 2e 20 20 36 34  -bit offset.  64
1d3ea 20 62 69 74 73 20 6e 65 65 64 65 64 20 74 6f 20   bits needed to 
1d3eb 63 61 74 63 68 20 6f 76 65 72 66 6c 6f 77 20 2a  catch overflow *
1d3ec 2f 0a 20 20 69 6e 74 20 73 7a 48 64 72 3b 20 20  /.  int szHdr;  
1d3ed 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
1d3ee 66 20 74 68 65 20 68 65 61 64 65 72 20 73 69 7a  f the header siz
1d3ef 65 20 66 69 65 6c 64 20 61 74 20 73 74 61 72 74  e field at start
1d3f0 20 6f 66 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20   of record */.  
1d3f1 69 6e 74 20 61 76 61 69 6c 3b 20 20 20 20 20 20  int avail;      
1d3f2 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
1d3f3 62 79 74 65 73 20 6f 66 20 61 76 61 69 6c 61 62  bytes of availab
1d3f4 6c 65 20 64 61 74 61 20 2a 2f 0a 20 20 4d 65 6d  le data */.  Mem
1d3f5 20 2a 70 52 65 67 3b 20 20 20 20 20 20 20 20 20   *pReg;         
1d3f6 2f 2a 20 50 73 65 75 64 6f 54 61 62 6c 65 20 69  /* PseudoTable i
1d3f7 6e 70 75 74 20 72 65 67 69 73 74 65 72 20 2a 2f  nput register */
1d3f8 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1d3f9 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1d3fa 20 69 6e 74 6f 20 75 2e 61 6d 20 2a 2f 0a 0a 0a   into u.am */...
1d3fb 20 20 75 2e 61 6d 2e 70 31 20 3d 20 70 4f 70 2d    u.am.p1 = pOp-
1d3fc 3e 70 31 3b 0a 20 20 75 2e 61 6d 2e 70 32 20 3d  >p1;.  u.am.p2 =
1d3fd 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75 2e 61 6d   pOp->p2;.  u.am
1d3fe 2e 70 43 20 3d 20 30 3b 0a 20 20 6d 65 6d 73 65  .pC = 0;.  memse
1d3ff 74 28 26 75 2e 61 6d 2e 73 4d 65 6d 2c 20 30 2c  t(&u.am.sMem, 0,
1d400 20 73 69 7a 65 6f 66 28 75 2e 61 6d 2e 73 4d 65   sizeof(u.am.sMe
1d401 6d 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75  m));.  assert( u
1d402 2e 61 6d 2e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f  .am.p1<p->nCurso
1d403 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  r );.  assert( p
1d404 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d  Op->p3>0 && pOp-
1d405 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p3<=p->nMem );.
1d406 20 20 75 2e 61 6d 2e 70 44 65 73 74 20 3d 20 26    u.am.pDest = &
1d407 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  p->aMem[pOp->p3]
1d408 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c  ;.  MemSetTypeFl
1d409 61 67 28 75 2e 61 6d 2e 70 44 65 73 74 2c 20 4d  ag(u.am.pDest, M
1d40a 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 75 2e 61 6d  EM_Null);.  u.am
1d40b 2e 7a 52 65 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a  .zRec = 0;..  /*
1d40c 20 54 68 69 73 20 62 6c 6f 63 6b 20 73 65 74 73   This block sets
1d40d 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 75 2e   the variable u.
1d40e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 74  am.payloadSize t
1d40f 6f 20 62 65 20 74 68 65 20 74 6f 74 61 6c 20 6e  o be the total n
1d410 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20 62 79  umber of.  ** by
1d411 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72  tes in the recor
1d412 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 75 2e 61  d..  **.  ** u.a
1d413 6d 2e 7a 52 65 63 20 69 73 20 73 65 74 20 74 6f  m.zRec is set to
1d414 20 62 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65   be the complete
1d415 20 74 65 78 74 20 6f 66 20 74 68 65 20 72 65 63   text of the rec
1d416 6f 72 64 20 69 66 20 69 74 20 69 73 20 61 76 61  ord if it is ava
1d417 69 6c 61 62 6c 65 2e 0a 20 20 2a 2a 20 54 68 65  ilable..  ** The
1d418 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72 64   complete record
1d419 20 74 65 78 74 20 69 73 20 61 6c 77 61 79 73 20   text is always 
1d41a 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 70 73  available for ps
1d41b 65 75 64 6f 2d 74 61 62 6c 65 73 0a 20 20 2a 2a  eudo-tables.  **
1d41c 20 49 66 20 74 68 65 20 72 65 63 6f 72 64 20 69   If the record i
1d41d 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20 63 75  s stored in a cu
1d41e 72 73 6f 72 2c 20 74 68 65 20 63 6f 6d 70 6c 65  rsor, the comple
1d41f 74 65 20 72 65 63 6f 72 64 20 74 65 78 74 0a 20  te record text. 
1d420 20 2a 2a 20 6d 69 67 68 74 20 62 65 20 61 76 61   ** might be ava
1d421 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 20 75  ilable in the  u
1d422 2e 61 6d 2e 70 43 2d 3e 61 52 6f 77 20 63 61 63  .am.pC->aRow cac
1d423 68 65 2e 20 20 4f 72 20 69 74 20 6d 69 67 68 74  he.  Or it might
1d424 20 6e 6f 74 20 62 65 2e 0a 20 20 2a 2a 20 49 66   not be..  ** If
1d425 20 74 68 65 20 64 61 74 61 20 69 73 20 75 6e 61   the data is una
1d426 76 61 69 6c 61 62 6c 65 2c 20 20 75 2e 61 6d 2e  vailable,  u.am.
1d427 7a 52 65 63 20 69 73 20 73 65 74 20 74 6f 20 4e  zRec is set to N
1d428 55 4c 4c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57  ULL..  **.  ** W
1d429 65 20 61 6c 73 6f 20 63 6f 6d 70 75 74 65 20 74  e also compute t
1d42a 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
1d42b 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 63 6f  umns in the reco
1d42c 72 64 2e 20 20 46 6f 72 20 63 75 72 73 6f 72 73  rd.  For cursors
1d42d 2c 0a 20 20 2a 2a 20 74 68 65 20 6e 75 6d 62 65  ,.  ** the numbe
1d42e 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 73 20  r of columns is 
1d42f 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 56 64  stored in the Vd
1d430 62 65 43 75 72 73 6f 72 2e 6e 46 69 65 6c 64 20  beCursor.nField 
1d431 65 6c 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20  element..  */.  
1d432 75 2e 61 6d 2e 70 43 20 3d 20 70 2d 3e 61 70 43  u.am.pC = p->apC
1d433 73 72 5b 75 2e 61 6d 2e 70 31 5d 3b 0a 20 20 61  sr[u.am.p1];.  a
1d434 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 43 21 3d  ssert( u.am.pC!=
1d435 30 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  0 );.#ifndef SQL
1d436 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1d437 54 41 42 4c 45 0a 20 20 61 73 73 65 72 74 28 20  TABLE.  assert( 
1d438 75 2e 61 6d 2e 70 43 2d 3e 70 56 74 61 62 43 75  u.am.pC->pVtabCu
1d439 72 73 6f 72 3d 3d 30 20 29 3b 0a 23 65 6e 64 69  rsor==0 );.#endi
1d43a 66 0a 20 20 75 2e 61 6d 2e 70 43 72 73 72 20 3d  f.  u.am.pCrsr =
1d43b 20 75 2e 61 6d 2e 70 43 2d 3e 70 43 75 72 73 6f   u.am.pC->pCurso
1d43c 72 3b 0a 20 20 69 66 28 20 75 2e 61 6d 2e 70 43  r;.  if( u.am.pC
1d43d 72 73 72 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  rsr!=0 ){.    /*
1d43e 20 54 68 65 20 72 65 63 6f 72 64 20 69 73 20 73   The record is s
1d43f 74 6f 72 65 64 20 69 6e 20 61 20 42 2d 54 72 65  tored in a B-Tre
1d440 65 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71  e */.    rc = sq
1d441 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d  lite3VdbeCursorM
1d442 6f 76 65 74 6f 28 75 2e 61 6d 2e 70 43 29 3b 0a  oveto(u.am.pC);.
1d443 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f 74      if( rc ) got
1d444 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
1d445 72 72 6f 72 3b 0a 20 20 20 20 69 66 28 20 75 2e  rror;.    if( u.
1d446 61 6d 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29  am.pC->nullRow )
1d447 7b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 70 61 79  {.      u.am.pay
1d448 6c 6f 61 64 53 69 7a 65 20 3d 20 30 3b 0a 20 20  loadSize = 0;.  
1d449 20 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 6d    }else if( u.am
1d44a 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73  .pC->cacheStatus
1d44b 3d 3d 70 2d 3e 63 61 63 68 65 43 74 72 20 29 7b  ==p->cacheCtr ){
1d44c 0a 20 20 20 20 20 20 75 2e 61 6d 2e 70 61 79 6c  .      u.am.payl
1d44d 6f 61 64 53 69 7a 65 20 3d 20 75 2e 61 6d 2e 70  oadSize = u.am.p
1d44e 43 2d 3e 70 61 79 6c 6f 61 64 53 69 7a 65 3b 0a  C->payloadSize;.
1d44f 20 20 20 20 20 20 75 2e 61 6d 2e 7a 52 65 63 20        u.am.zRec 
1d450 3d 20 28 63 68 61 72 2a 29 75 2e 61 6d 2e 70 43  = (char*)u.am.pC
1d451 2d 3e 61 52 6f 77 3b 0a 20 20 20 20 7d 65 6c 73  ->aRow;.    }els
1d452 65 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 69  e if( u.am.pC->i
1d453 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 20  sIndex ){.      
1d454 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42  assert( sqlite3B
1d455 74 72 65 65 43 75 72 73 6f 72 49 73 56 61 6c 69  treeCursorIsVali
1d456 64 28 75 2e 61 6d 2e 70 43 72 73 72 29 20 29 3b  d(u.am.pCrsr) );
1d457 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1d458 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28  te3BtreeKeySize(
1d459 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e 61  u.am.pCrsr, &u.a
1d45a 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 29  m.payloadSize64)
1d45b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1d45c 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
1d45d 20 20 20 2f 2a 20 54 72 75 65 20 62 65 63 61 75     /* True becau
1d45e 73 65 20 6f 66 20 43 75 72 73 6f 72 4d 6f 76 65  se of CursorMove
1d45f 74 6f 28 29 20 63 61 6c 6c 20 61 62 6f 76 65 20  to() call above 
1d460 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 73 71 6c 69  */.      /* sqli
1d461 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c  te3BtreeParseCel
1d462 6c 50 74 72 28 29 20 75 73 65 73 20 67 65 74 56  lPtr() uses getV
1d463 61 72 69 6e 74 33 32 28 29 20 74 6f 20 65 78 74  arint32() to ext
1d464 72 61 63 74 20 74 68 65 0a 20 20 20 20 20 20 2a  ract the.      *
1d465 2a 20 70 61 79 6c 6f 61 64 20 73 69 7a 65 2c 20  * payload size, 
1d466 73 6f 20 69 74 20 69 73 20 69 6d 70 6f 73 73 69  so it is impossi
1d467 62 6c 65 20 66 6f 72 20 75 2e 61 6d 2e 70 61 79  ble for u.am.pay
1d468 6c 6f 61 64 53 69 7a 65 36 34 20 74 6f 20 62 65  loadSize64 to be
1d469 0a 20 20 20 20 20 20 2a 2a 20 6c 61 72 67 65 72  .      ** larger
1d46a 20 74 68 61 6e 20 33 32 20 62 69 74 73 2e 20 2a   than 32 bits. *
1d46b 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  /.      assert( 
1d46c 28 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a  (u.am.payloadSiz
1d46d 65 36 34 20 26 20 53 51 4c 49 54 45 5f 4d 41 58  e64 & SQLITE_MAX
1d46e 5f 55 33 32 29 3d 3d 28 75 36 34 29 75 2e 61 6d  _U32)==(u64)u.am
1d46f 2e 70 61 79 6c 6f 61 64 53 69 7a 65 36 34 20 29  .payloadSize64 )
1d470 3b 0a 20 20 20 20 20 20 75 2e 61 6d 2e 70 61 79  ;.      u.am.pay
1d471 6c 6f 61 64 53 69 7a 65 20 3d 20 28 75 33 32 29  loadSize = (u32)
1d472 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65  u.am.payloadSize
1d473 36 34 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  64;.    }else{. 
1d474 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c       assert( sql
1d475 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49  ite3BtreeCursorI
1d476 73 56 61 6c 69 64 28 75 2e 61 6d 2e 70 43 72 73  sValid(u.am.pCrs
1d477 72 29 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  r) );.      rc =
1d478 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74   sqlite3BtreeDat
1d479 61 53 69 7a 65 28 75 2e 61 6d 2e 70 43 72 73 72  aSize(u.am.pCrsr
1d47a 2c 20 26 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53  , &u.am.payloadS
1d47b 69 7a 65 29 3b 0a 20 20 20 20 20 20 61 73 73 65  ize);.      asse
1d47c 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
1d47d 4b 20 29 3b 20 20 20 2f 2a 20 44 61 74 61 53 69  K );   /* DataSi
1d47e 7a 65 28 29 20 63 61 6e 6e 6f 74 20 66 61 69 6c  ze() cannot fail
1d47f 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73   */.    }.  }els
1d480 65 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d 3e 70  e if( u.am.pC->p
1d481 73 65 75 64 6f 54 61 62 6c 65 52 65 67 3e 30 20  seudoTableReg>0 
1d482 29 7b 0a 20 20 20 20 75 2e 61 6d 2e 70 52 65 67  ){.    u.am.pReg
1d483 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 75 2e 61 6d   = &p->aMem[u.am
1d484 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65  .pC->pseudoTable
1d485 52 65 67 5d 3b 0a 20 20 20 20 61 73 73 65 72 74  Reg];.    assert
1d486 28 20 75 2e 61 6d 2e 70 52 65 67 2d 3e 66 6c 61  ( u.am.pReg->fla
1d487 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20 29 3b  gs & MEM_Blob );
1d488 0a 20 20 20 20 75 2e 61 6d 2e 70 61 79 6c 6f 61  .    u.am.payloa
1d489 64 53 69 7a 65 20 3d 20 75 2e 61 6d 2e 70 52 65  dSize = u.am.pRe
1d48a 67 2d 3e 6e 3b 0a 20 20 20 20 75 2e 61 6d 2e 7a  g->n;.    u.am.z
1d48b 52 65 63 20 3d 20 75 2e 61 6d 2e 70 52 65 67 2d  Rec = u.am.pReg-
1d48c 3e 7a 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 43 2d  >z;.    u.am.pC-
1d48d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 28  >cacheStatus = (
1d48e 70 4f 70 2d 3e 70 35 26 4f 50 46 4c 41 47 5f 43  pOp->p5&OPFLAG_C
1d48f 4c 45 41 52 43 41 43 48 45 29 20 3f 20 43 41 43  LEARCACHE) ? CAC
1d490 48 45 5f 53 54 41 4c 45 20 3a 20 70 2d 3e 63 61  HE_STALE : p->ca
1d491 63 68 65 43 74 72 3b 0a 20 20 20 20 61 73 73 65  cheCtr;.    asse
1d492 72 74 28 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64  rt( u.am.payload
1d493 53 69 7a 65 3d 3d 30 20 7c 7c 20 75 2e 61 6d 2e  Size==0 || u.am.
1d494 7a 52 65 63 21 3d 30 20 29 3b 0a 20 20 7d 65 6c  zRec!=0 );.  }el
1d495 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f 6e 73 69  se{.    /* Consi
1d496 64 65 72 20 74 68 65 20 72 6f 77 20 74 6f 20 62  der the row to b
1d497 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 75 2e  e NULL */.    u.
1d498 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d  am.payloadSize =
1d499 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66   0;.  }..  /* If
1d49a 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a   u.am.payloadSiz
1d49b 65 20 69 73 20 30 2c 20 74 68 65 6e 20 6a 75 73  e is 0, then jus
1d49c 74 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 2a  t store a NULL *
1d49d 2f 0a 20 20 69 66 28 20 75 2e 61 6d 2e 70 61 79  /.  if( u.am.pay
1d49e 6c 6f 61 64 53 69 7a 65 3d 3d 30 20 29 7b 0a 20  loadSize==0 ){. 
1d49f 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e     assert( u.am.
1d4a0 70 44 65 73 74 2d 3e 66 6c 61 67 73 26 4d 45 4d  pDest->flags&MEM
1d4a1 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 67 6f 74  _Null );.    got
1d4a2 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b  o op_column_out;
1d4a3 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 64  .  }.  assert( d
1d4a4 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
1d4a5 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 3e 3d  _LIMIT_LENGTH]>=
1d4a6 30 20 29 3b 0a 20 20 69 66 28 20 75 2e 61 6d 2e  0 );.  if( u.am.
1d4a7 70 61 79 6c 6f 61 64 53 69 7a 65 20 3e 20 28 75  payloadSize > (u
1d4a8 33 32 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  32)db->aLimit[SQ
1d4a9 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
1d4aa 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74  H] ){.    goto t
1d4ab 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 0a 20 20 75  oo_big;.  }..  u
1d4ac 2e 61 6d 2e 6e 46 69 65 6c 64 20 3d 20 75 2e 61  .am.nField = u.a
1d4ad 6d 2e 70 43 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20  m.pC->nField;.  
1d4ae 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 70 32 3c  assert( u.am.p2<
1d4af 75 2e 61 6d 2e 6e 46 69 65 6c 64 20 29 3b 0a 0a  u.am.nField );..
1d4b0 20 20 2f 2a 20 52 65 61 64 20 61 6e 64 20 70 61    /* Read and pa
1d4b1 72 73 65 20 74 68 65 20 74 61 62 6c 65 20 68 65  rse the table he
1d4b2 61 64 65 72 2e 20 20 53 74 6f 72 65 20 74 68 65  ader.  Store the
1d4b3 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20   results of the 
1d4b4 70 61 72 73 65 0a 20 20 2a 2a 20 69 6e 74 6f 20  parse.  ** into 
1d4b5 74 68 65 20 72 65 63 6f 72 64 20 68 65 61 64 65  the record heade
1d4b6 72 20 63 61 63 68 65 20 66 69 65 6c 64 73 20 6f  r cache fields o
1d4b7 66 20 74 68 65 20 63 75 72 73 6f 72 2e 0a 20 20  f the cursor..  
1d4b8 2a 2f 0a 20 20 75 2e 61 6d 2e 61 54 79 70 65 20  */.  u.am.aType 
1d4b9 3d 20 75 2e 61 6d 2e 70 43 2d 3e 61 54 79 70 65  = u.am.pC->aType
1d4ba 3b 0a 20 20 69 66 28 20 75 2e 61 6d 2e 70 43 2d  ;.  if( u.am.pC-
1d4bb 3e 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70 2d  >cacheStatus==p-
1d4bc 3e 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20 20  >cacheCtr ){.   
1d4bd 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 20 3d 20   u.am.aOffset = 
1d4be 75 2e 61 6d 2e 70 43 2d 3e 61 4f 66 66 73 65 74  u.am.pC->aOffset
1d4bf 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
1d4c0 73 73 65 72 74 28 75 2e 61 6d 2e 61 54 79 70 65  ssert(u.am.aType
1d4c1 29 3b 0a 20 20 20 20 75 2e 61 6d 2e 61 76 61 69  );.    u.am.avai
1d4c2 6c 20 3d 20 30 3b 0a 20 20 20 20 75 2e 61 6d 2e  l = 0;.    u.am.
1d4c3 70 43 2d 3e 61 4f 66 66 73 65 74 20 3d 20 75 2e  pC->aOffset = u.
1d4c4 61 6d 2e 61 4f 66 66 73 65 74 20 3d 20 26 75 2e  am.aOffset = &u.
1d4c5 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 6e 46  am.aType[u.am.nF
1d4c6 69 65 6c 64 5d 3b 0a 20 20 20 20 75 2e 61 6d 2e  ield];.    u.am.
1d4c7 70 43 2d 3e 70 61 79 6c 6f 61 64 53 69 7a 65 20  pC->payloadSize 
1d4c8 3d 20 75 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69  = u.am.payloadSi
1d4c9 7a 65 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 43 2d  ze;.    u.am.pC-
1d4ca 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 70  >cacheStatus = p
1d4cb 2d 3e 63 61 63 68 65 43 74 72 3b 0a 0a 20 20 20  ->cacheCtr;..   
1d4cc 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68   /* Figure out h
1d4cd 6f 77 20 6d 61 6e 79 20 62 79 74 65 73 20 61 72  ow many bytes ar
1d4ce 65 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 20  e in the header 
1d4cf 2a 2f 0a 20 20 20 20 69 66 28 20 75 2e 61 6d 2e  */.    if( u.am.
1d4d0 7a 52 65 63 20 29 7b 0a 20 20 20 20 20 20 75 2e  zRec ){.      u.
1d4d1 61 6d 2e 7a 44 61 74 61 20 3d 20 75 2e 61 6d 2e  am.zData = u.am.
1d4d2 7a 52 65 63 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  zRec;.    }else{
1d4d3 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 6d 2e  .      if( u.am.
1d4d4 70 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20  pC->isIndex ){. 
1d4d5 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a 44 61 74         u.am.zDat
1d4d6 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  a = (char*)sqlit
1d4d7 65 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28  e3BtreeKeyFetch(
1d4d8 75 2e 61 6d 2e 70 43 72 73 72 2c 20 26 75 2e 61  u.am.pCrsr, &u.a
1d4d9 6d 2e 61 76 61 69 6c 29 3b 0a 20 20 20 20 20 20  m.avail);.      
1d4da 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 75  }else{.        u
1d4db 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 28 63 68 61  .am.zData = (cha
1d4dc 72 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 44  r*)sqlite3BtreeD
1d4dd 61 74 61 46 65 74 63 68 28 75 2e 61 6d 2e 70 43  ataFetch(u.am.pC
1d4de 72 73 72 2c 20 26 75 2e 61 6d 2e 61 76 61 69 6c  rsr, &u.am.avail
1d4df 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1d4e0 20 2f 2a 20 49 66 20 4b 65 79 46 65 74 63 68 28   /* If KeyFetch(
1d4e1 29 2f 44 61 74 61 46 65 74 63 68 28 29 20 6d 61  )/DataFetch() ma
1d4e2 6e 61 67 65 64 20 74 6f 20 67 65 74 20 74 68 65  naged to get the
1d4e3 20 65 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 2c   entire payload,
1d4e4 0a 20 20 20 20 20 20 2a 2a 20 73 61 76 65 20 74  .      ** save t
1d4e5 68 65 20 70 61 79 6c 6f 61 64 20 69 6e 20 74 68  he payload in th
1d4e6 65 20 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f 77 20  e u.am.pC->aRow 
1d4e7 63 61 63 68 65 2e 20 20 54 68 61 74 20 77 69 6c  cache.  That wil
1d4e8 6c 20 73 61 76 65 20 75 73 20 66 72 6f 6d 0a 20  l save us from. 
1d4e9 20 20 20 20 20 2a 2a 20 68 61 76 69 6e 67 20 74       ** having t
1d4ea 6f 20 6d 61 6b 65 20 61 64 64 69 74 69 6f 6e 61  o make additiona
1d4eb 6c 20 63 61 6c 6c 73 20 74 6f 20 66 65 74 63 68  l calls to fetch
1d4ec 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 70 6f 72   the content por
1d4ed 74 69 6f 6e 20 6f 66 0a 20 20 20 20 20 20 2a 2a  tion of.      **
1d4ee 20 74 68 65 20 72 65 63 6f 72 64 2e 0a 20 20 20   the record..   
1d4ef 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65     */.      asse
1d4f0 72 74 28 20 75 2e 61 6d 2e 61 76 61 69 6c 3e 3d  rt( u.am.avail>=
1d4f1 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 75  0 );.      if( u
1d4f2 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 20  .am.payloadSize 
1d4f3 3c 3d 20 28 75 33 32 29 75 2e 61 6d 2e 61 76 61  <= (u32)u.am.ava
1d4f4 69 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 75 2e  il ){.        u.
1d4f5 61 6d 2e 7a 52 65 63 20 3d 20 75 2e 61 6d 2e 7a  am.zRec = u.am.z
1d4f6 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20 75 2e  Data;.        u.
1d4f7 61 6d 2e 70 43 2d 3e 61 52 6f 77 20 3d 20 28 75  am.pC->aRow = (u
1d4f8 38 2a 29 75 2e 61 6d 2e 7a 44 61 74 61 3b 0a 20  8*)u.am.zData;. 
1d4f9 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1d4fa 20 20 20 20 75 2e 61 6d 2e 70 43 2d 3e 61 52 6f      u.am.pC->aRo
1d4fb 77 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  w = 0;.      }. 
1d4fc 20 20 20 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20     }.    /* The 
1d4fd 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74  following assert
1d4fe 20 69 73 20 74 72 75 65 20 69 6e 20 61 6c 6c 20   is true in all 
1d4ff 63 61 73 65 73 20 61 63 63 65 70 74 20 77 68 65  cases accept whe
1d500 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74  n.    ** the dat
1d501 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 62  abase file has b
1d502 65 65 6e 20 63 6f 72 72 75 70 74 65 64 20 65 78  een corrupted ex
1d503 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 20 20 2a 2a  ternally..    **
1d504 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 6d      assert( u.am
1d505 2e 7a 52 65 63 21 3d 30 20 7c 7c 20 75 2e 61 6d  .zRec!=0 || u.am
1d506 2e 61 76 61 69 6c 3e 3d 75 2e 61 6d 2e 70 61 79  .avail>=u.am.pay
1d507 6c 6f 61 64 53 69 7a 65 20 7c 7c 20 75 2e 61 6d  loadSize || u.am
1d508 2e 61 76 61 69 6c 3e 3d 39 20 29 3b 20 2a 2f 0a  .avail>=9 ); */.
1d509 20 20 20 20 75 2e 61 6d 2e 73 7a 48 64 72 20 3d      u.am.szHdr =
1d50a 20 67 65 74 56 61 72 69 6e 74 33 32 28 28 75 38   getVarint32((u8
1d50b 2a 29 75 2e 61 6d 2e 7a 44 61 74 61 2c 20 75 2e  *)u.am.zData, u.
1d50c 61 6d 2e 6f 66 66 73 65 74 29 3b 0a 0a 20 20 20  am.offset);..   
1d50d 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 20   /* Make sure a 
1d50e 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65  corrupt database
1d50f 20 68 61 73 20 6e 6f 74 20 67 69 76 65 6e 20 75   has not given u
1d510 73 20 61 6e 20 6f 76 65 72 73 69 7a 65 20 68 65  s an oversize he
1d511 61 64 65 72 2e 0a 20 20 20 20 2a 2a 20 44 6f 20  ader..    ** Do 
1d512 74 68 69 73 20 6e 6f 77 20 74 6f 20 61 76 6f 69  this now to avoi
1d513 64 20 61 6e 20 6f 76 65 72 73 69 7a 65 20 6d 65  d an oversize me
1d514 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e  mory allocation.
1d515 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
1d516 79 70 65 20 65 6e 74 72 69 65 73 20 63 61 6e 20  ype entries can 
1d517 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64  be between 1 and
1d518 20 35 20 62 79 74 65 73 20 65 61 63 68 2e 20 20   5 bytes each.  
1d519 42 75 74 20 34 20 61 6e 64 20 35 20 62 79 74 65  But 4 and 5 byte
1d51a 0a 20 20 20 20 2a 2a 20 74 79 70 65 73 20 75 73  .    ** types us
1d51b 65 20 73 6f 20 6d 75 63 68 20 64 61 74 61 20 73  e so much data s
1d51c 70 61 63 65 20 74 68 61 74 20 74 68 65 72 65 20  pace that there 
1d51d 63 61 6e 20 6f 6e 6c 79 20 62 65 20 34 30 39 36  can only be 4096
1d51e 20 61 6e 64 20 33 32 20 6f 66 0a 20 20 20 20 2a   and 32 of.    *
1d51f 2a 20 74 68 65 6d 2c 20 72 65 73 70 65 63 74 69  * them, respecti
1d520 76 65 6c 79 2e 20 20 53 6f 20 74 68 65 20 6d 61  vely.  So the ma
1d521 78 69 6d 75 6d 20 68 65 61 64 65 72 20 6c 65 6e  ximum header len
1d522 67 74 68 20 72 65 73 75 6c 74 73 20 66 72 6f 6d  gth results from
1d523 20 61 0a 20 20 20 20 2a 2a 20 33 2d 62 79 74 65   a.    ** 3-byte
1d524 20 74 79 70 65 20 66 6f 72 20 65 61 63 68 20 6f   type for each o
1d525 66 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66  f the maximum of
1d526 20 33 32 37 36 38 20 63 6f 6c 75 6d 6e 73 20 70   32768 columns p
1d527 6c 75 73 20 74 68 72 65 65 0a 20 20 20 20 2a 2a  lus three.    **
1d528 20 65 78 74 72 61 20 62 79 74 65 73 20 66 6f 72   extra bytes for
1d529 20 74 68 65 20 68 65 61 64 65 72 20 6c 65 6e 67   the header leng
1d52a 74 68 20 69 74 73 65 6c 66 2e 20 20 33 32 37 36  th itself.  3276
1d52b 38 2a 33 20 2b 20 33 20 3d 20 39 38 33 30 37 2e  8*3 + 3 = 98307.
1d52c 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1d52d 75 2e 61 6d 2e 6f 66 66 73 65 74 20 3e 20 39 38  u.am.offset > 98
1d52e 33 30 37 20 29 7b 0a 20 20 20 20 20 20 72 63 20  307 ){.      rc 
1d52f 3d 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  = SQLITE_CORRUPT
1d530 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74  _BKPT;.      got
1d531 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b  o op_column_out;
1d532 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43  .    }..    /* C
1d533 6f 6d 70 75 74 65 20 69 6e 20 75 2e 61 6d 2e 6c  ompute in u.am.l
1d534 65 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  en the number of
1d535 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 77   bytes of data w
1d536 65 20 6e 65 65 64 20 74 6f 20 72 65 61 64 20 69  e need to read i
1d537 6e 20 6f 72 64 65 72 0a 20 20 20 20 2a 2a 20 74  n order.    ** t
1d538 6f 20 67 65 74 20 75 2e 61 6d 2e 6e 46 69 65 6c  o get u.am.nFiel
1d539 64 20 74 79 70 65 20 76 61 6c 75 65 73 2e 20 20  d type values.  
1d53a 75 2e 61 6d 2e 6f 66 66 73 65 74 20 69 73 20 61  u.am.offset is a
1d53b 6e 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e  n upper bound on
1d53c 20 74 68 69 73 2e 20 20 42 75 74 0a 20 20 20 20   this.  But.    
1d53d 2a 2a 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 20 6d  ** u.am.nField m
1d53e 69 67 68 74 20 62 65 20 73 69 67 6e 69 66 69 63  ight be signific
1d53f 61 6e 74 6c 79 20 6c 65 73 73 20 74 68 61 6e 20  antly less than 
1d540 74 68 65 20 74 72 75 65 20 6e 75 6d 62 65 72 20  the true number 
1d541 6f 66 20 63 6f 6c 75 6d 6e 73 0a 20 20 20 20 2a  of columns.    *
1d542 2a 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20  * in the table, 
1d543 61 6e 64 20 69 6e 20 74 68 61 74 20 63 61 73 65  and in that case
1d544 2c 20 35 2a 75 2e 61 6d 2e 6e 46 69 65 6c 64 2b  , 5*u.am.nField+
1d545 33 20 6d 69 67 68 74 20 62 65 20 73 6d 61 6c 6c  3 might be small
1d546 65 72 20 74 68 61 6e 20 75 2e 61 6d 2e 6f 66 66  er than u.am.off
1d547 73 65 74 2e 0a 20 20 20 20 2a 2a 20 57 65 20 77  set..    ** We w
1d548 61 6e 74 20 74 6f 20 6d 69 6e 69 6d 69 7a 65 20  ant to minimize 
1d549 75 2e 61 6d 2e 6c 65 6e 20 69 6e 20 6f 72 64 65  u.am.len in orde
1d54a 72 20 74 6f 20 6c 69 6d 69 74 20 74 68 65 20 73  r to limit the s
1d54b 69 7a 65 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  ize of the memor
1d54c 79 0a 20 20 20 20 2a 2a 20 61 6c 6c 6f 63 61 74  y.    ** allocat
1d54d 69 6f 6e 2c 20 65 73 70 65 63 69 61 6c 6c 79 20  ion, especially 
1d54e 69 66 20 61 20 63 6f 72 72 75 70 74 20 64 61 74  if a corrupt dat
1d54f 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 63  abase file has c
1d550 61 75 73 65 64 20 75 2e 61 6d 2e 6f 66 66 73 65  aused u.am.offse
1d551 74 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 6f  t.    ** to be o
1d552 76 65 72 73 69 7a 65 64 2e 20 4f 66 66 73 65 74  versized. Offset
1d553 20 69 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 39   is limited to 9
1d554 38 33 30 37 20 61 62 6f 76 65 2e 20 20 42 75 74  8307 above.  But
1d555 20 39 38 33 30 37 20 6d 69 67 68 74 0a 20 20 20   98307 might.   
1d556 20 2a 2a 20 73 74 69 6c 6c 20 65 78 63 65 65 64   ** still exceed
1d557 20 52 6f 62 73 6f 6e 20 6d 65 6d 6f 72 79 20 61   Robson memory a
1d558 6c 6c 6f 63 61 74 69 6f 6e 20 6c 69 6d 69 74 73  llocation limits
1d559 20 6f 6e 20 73 6f 6d 65 20 63 6f 6e 66 69 67 75   on some configu
1d55a 72 61 74 69 6f 6e 73 2e 0a 20 20 20 20 2a 2a 20  rations..    ** 
1d55b 4f 6e 20 73 79 73 74 65 6d 73 20 74 68 61 74 20  On systems that 
1d55c 63 61 6e 6e 6f 74 20 74 6f 6c 65 72 61 74 65 20  cannot tolerate 
1d55d 6c 61 72 67 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  large memory all
1d55e 6f 63 61 74 69 6f 6e 73 2c 20 75 2e 61 6d 2e 6e  ocations, u.am.n
1d55f 46 69 65 6c 64 2a 35 2b 33 0a 20 20 20 20 2a 2a  Field*5+3.    **
1d560 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 62 65 20   will likely be 
1d561 6d 75 63 68 20 73 6d 61 6c 6c 65 72 20 73 69 6e  much smaller sin
1d562 63 65 20 75 2e 61 6d 2e 6e 46 69 65 6c 64 20 77  ce u.am.nField w
1d563 69 6c 6c 20 6c 69 6b 65 6c 79 20 62 65 20 6c 65  ill likely be le
1d564 73 73 20 74 68 61 6e 0a 20 20 20 20 2a 2a 20 32  ss than.    ** 2
1d565 30 20 6f 72 20 73 6f 2e 20 20 54 68 69 73 20 69  0 or so.  This i
1d566 6e 73 75 72 65 73 20 74 68 61 74 20 52 6f 62 73  nsures that Robs
1d567 6f 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  on memory alloca
1d568 74 69 6f 6e 20 6c 69 6d 69 74 73 20 61 72 65 0a  tion limits are.
1d569 20 20 20 20 2a 2a 20 6e 6f 74 20 65 78 63 65 65      ** not excee
1d56a 64 65 64 20 65 76 65 6e 20 66 6f 72 20 63 6f 72  ded even for cor
1d56b 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69  rupt database fi
1d56c 6c 65 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  les..    */.    
1d56d 75 2e 61 6d 2e 6c 65 6e 20 3d 20 75 2e 61 6d 2e  u.am.len = u.am.
1d56e 6e 46 69 65 6c 64 2a 35 20 2b 20 33 3b 0a 20 20  nField*5 + 3;.  
1d56f 20 20 69 66 28 20 75 2e 61 6d 2e 6c 65 6e 20 3e    if( u.am.len >
1d570 20 28 69 6e 74 29 75 2e 61 6d 2e 6f 66 66 73 65   (int)u.am.offse
1d571 74 20 29 20 75 2e 61 6d 2e 6c 65 6e 20 3d 20 28  t ) u.am.len = (
1d572 69 6e 74 29 75 2e 61 6d 2e 6f 66 66 73 65 74 3b  int)u.am.offset;
1d573 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 4b 65 79  ..    /* The Key
1d574 46 65 74 63 68 28 29 20 6f 72 20 44 61 74 61 46  Fetch() or DataF
1d575 65 74 63 68 28 29 20 61 62 6f 76 65 20 61 72 65  etch() above are
1d576 20 66 61 73 74 20 61 6e 64 20 77 69 6c 6c 20 67   fast and will g
1d577 65 74 20 74 68 65 20 65 6e 74 69 72 65 0a 20 20  et the entire.  
1d578 20 20 2a 2a 20 72 65 63 6f 72 64 20 68 65 61 64    ** record head
1d579 65 72 20 69 6e 20 6d 6f 73 74 20 63 61 73 65 73  er in most cases
1d57a 2e 20 20 42 75 74 20 74 68 65 79 20 77 69 6c 6c  .  But they will
1d57b 20 66 61 69 6c 20 74 6f 20 67 65 74 20 74 68 65   fail to get the
1d57c 20 63 6f 6d 70 6c 65 74 65 0a 20 20 20 20 2a 2a   complete.    **
1d57d 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 69   record header i
1d57e 66 20 74 68 65 20 72 65 63 6f 72 64 20 68 65 61  f the record hea
1d57f 64 65 72 20 64 6f 65 73 20 6e 6f 74 20 66 69 74  der does not fit
1d580 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67   on a single pag
1d581 65 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20  e.    ** in the 
1d582 42 2d 54 72 65 65 2e 20 20 57 68 65 6e 20 74 68  B-Tree.  When th
1d583 61 74 20 68 61 70 70 65 6e 73 2c 20 75 73 65 20  at happens, use 
1d584 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 72  sqlite3VdbeMemFr
1d585 6f 6d 42 74 72 65 65 28 29 20 74 6f 0a 20 20 20  omBtree() to.   
1d586 20 2a 2a 20 61 63 71 75 69 72 65 20 74 68 65 20   ** acquire the 
1d587 63 6f 6d 70 6c 65 74 65 20 68 65 61 64 65 72 20  complete header 
1d588 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  text..    */.   
1d589 20 69 66 28 20 21 75 2e 61 6d 2e 7a 52 65 63 20   if( !u.am.zRec 
1d58a 26 26 20 75 2e 61 6d 2e 61 76 61 69 6c 3c 75 2e  && u.am.avail<u.
1d58b 61 6d 2e 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20  am.len ){.      
1d58c 75 2e 61 6d 2e 73 4d 65 6d 2e 66 6c 61 67 73 20  u.am.sMem.flags 
1d58d 3d 20 30 3b 0a 20 20 20 20 20 20 75 2e 61 6d 2e  = 0;.      u.am.
1d58e 73 4d 65 6d 2e 64 62 20 3d 20 30 3b 0a 20 20 20  sMem.db = 0;.   
1d58f 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
1d590 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28  dbeMemFromBtree(
1d591 75 2e 61 6d 2e 70 43 72 73 72 2c 20 30 2c 20 75  u.am.pCrsr, 0, u
1d592 2e 61 6d 2e 6c 65 6e 2c 20 75 2e 61 6d 2e 70 43  .am.len, u.am.pC
1d593 2d 3e 69 73 49 6e 64 65 78 2c 20 26 75 2e 61 6d  ->isIndex, &u.am
1d594 2e 73 4d 65 6d 29 3b 0a 20 20 20 20 20 20 69 66  .sMem);.      if
1d595 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1d596 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  ){.        goto 
1d597 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74 3b 0a 20  op_column_out;. 
1d598 20 20 20 20 20 7d 0a 20 20 20 20 20 20 75 2e 61       }.      u.a
1d599 6d 2e 7a 44 61 74 61 20 3d 20 75 2e 61 6d 2e 73  m.zData = u.am.s
1d59a 4d 65 6d 2e 7a 3b 0a 20 20 20 20 7d 0a 20 20 20  Mem.z;.    }.   
1d59b 20 75 2e 61 6d 2e 7a 45 6e 64 48 64 72 20 3d 20   u.am.zEndHdr = 
1d59c 28 75 38 20 2a 29 26 75 2e 61 6d 2e 7a 44 61 74  (u8 *)&u.am.zDat
1d59d 61 5b 75 2e 61 6d 2e 6c 65 6e 5d 3b 0a 20 20 20  a[u.am.len];.   
1d59e 20 75 2e 61 6d 2e 7a 49 64 78 20 3d 20 28 75 38   u.am.zIdx = (u8
1d59f 20 2a 29 26 75 2e 61 6d 2e 7a 44 61 74 61 5b 75   *)&u.am.zData[u
1d5a0 2e 61 6d 2e 73 7a 48 64 72 5d 3b 0a 0a 20 20 20  .am.szHdr];..   
1d5a1 20 2f 2a 20 53 63 61 6e 20 74 68 65 20 68 65 61   /* Scan the hea
1d5a2 64 65 72 20 61 6e 64 20 75 73 65 20 69 74 20 74  der and use it t
1d5a3 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 75 2e  o fill in the u.
1d5a4 61 6d 2e 61 54 79 70 65 5b 5d 20 61 6e 64 20 75  am.aType[] and u
1d5a5 2e 61 6d 2e 61 4f 66 66 73 65 74 5b 5d 0a 20 20  .am.aOffset[].  
1d5a6 20 20 2a 2a 20 61 72 72 61 79 73 2e 20 20 75 2e    ** arrays.  u.
1d5a7 61 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 69 5d  am.aType[u.am.i]
1d5a8 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68   will contain th
1d5a9 65 20 74 79 70 65 20 69 6e 74 65 67 65 72 20 66  e type integer f
1d5aa 6f 72 20 74 68 65 20 75 2e 61 6d 2e 69 2d 74 68  or the u.am.i-th
1d5ab 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 61  .    ** column a
1d5ac 6e 64 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74 5b  nd u.am.aOffset[
1d5ad 75 2e 61 6d 2e 69 5d 20 77 69 6c 6c 20 63 6f 6e  u.am.i] will con
1d5ae 74 61 69 6e 20 74 68 65 20 75 2e 61 6d 2e 6f 66  tain the u.am.of
1d5af 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20 62 65  fset from the be
1d5b0 67 69 6e 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 6f  ginning.    ** o
1d5b1 66 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20  f the record to 
1d5b2 74 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65  the start of the
1d5b3 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 75 2e   data for the u.
1d5b4 61 6d 2e 69 2d 74 68 20 63 6f 6c 75 6d 6e 0a 20  am.i-th column. 
1d5b5 20 20 20 2a 2f 0a 20 20 20 20 75 2e 61 6d 2e 6f     */.    u.am.o
1d5b6 66 66 73 65 74 36 34 20 3d 20 75 2e 61 6d 2e 6f  ffset64 = u.am.o
1d5b7 66 66 73 65 74 3b 0a 20 20 20 20 66 6f 72 28 75  ffset;.    for(u
1d5b8 2e 61 6d 2e 69 3d 30 3b 20 75 2e 61 6d 2e 69 3c  .am.i=0; u.am.i<
1d5b9 75 2e 61 6d 2e 6e 46 69 65 6c 64 3b 20 75 2e 61  u.am.nField; u.a
1d5ba 6d 2e 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  m.i++){.      if
1d5bb 28 20 75 2e 61 6d 2e 7a 49 64 78 3c 75 2e 61 6d  ( u.am.zIdx<u.am
1d5bc 2e 7a 45 6e 64 48 64 72 20 29 7b 0a 20 20 20 20  .zEndHdr ){.    
1d5bd 20 20 20 20 75 2e 61 6d 2e 61 4f 66 66 73 65 74      u.am.aOffset
1d5be 5b 75 2e 61 6d 2e 69 5d 20 3d 20 28 75 33 32 29  [u.am.i] = (u32)
1d5bf 75 2e 61 6d 2e 6f 66 66 73 65 74 36 34 3b 0a 20  u.am.offset64;. 
1d5c0 20 20 20 20 20 20 20 75 2e 61 6d 2e 7a 49 64 78         u.am.zIdx
1d5c1 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28   += getVarint32(
1d5c2 75 2e 61 6d 2e 7a 49 64 78 2c 20 75 2e 61 6d 2e  u.am.zIdx, u.am.
1d5c3 61 54 79 70 65 5b 75 2e 61 6d 2e 69 5d 29 3b 0a  aType[u.am.i]);.
1d5c4 20 20 20 20 20 20 20 20 75 2e 61 6d 2e 6f 66 66          u.am.off
1d5c5 73 65 74 36 34 20 2b 3d 20 73 71 6c 69 74 65 33  set64 += sqlite3
1d5c6 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65  VdbeSerialTypeLe
1d5c7 6e 28 75 2e 61 6d 2e 61 54 79 70 65 5b 75 2e 61  n(u.am.aType[u.a
1d5c8 6d 2e 69 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c  m.i]);.      }el
1d5c9 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49  se{.        /* I
1d5ca 66 20 75 2e 61 6d 2e 69 20 69 73 20 6c 65 73 73  f u.am.i is less
1d5cb 20 74 68 61 74 20 75 2e 61 6d 2e 6e 46 69 65 6c   that u.am.nFiel
1d5cc 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20 61 72  d, then there ar
1d5cd 65 20 6c 65 73 73 20 66 69 65 6c 64 73 20 69 6e  e less fields in
1d5ce 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a   this.        **
1d5cf 20 72 65 63 6f 72 64 20 74 68 61 6e 20 53 65 74   record than Set
1d5d0 4e 75 6d 43 6f 6c 75 6d 6e 73 20 69 6e 64 69 63  NumColumns indic
1d5d1 61 74 65 64 20 74 68 65 72 65 20 61 72 65 20 63  ated there are c
1d5d2 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 0a 20 20  olumns in the.  
1d5d3 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 2e 20        ** table. 
1d5d4 53 65 74 20 74 68 65 20 75 2e 61 6d 2e 6f 66 66  Set the u.am.off
1d5d5 73 65 74 20 66 6f 72 20 61 6e 79 20 65 78 74 72  set for any extr
1d5d6 61 20 63 6f 6c 75 6d 6e 73 20 6e 6f 74 20 70 72  a columns not pr
1d5d7 65 73 65 6e 74 20 69 6e 0a 20 20 20 20 20 20 20  esent in.       
1d5d8 20 2a 2a 20 74 68 65 20 72 65 63 6f 72 64 20 74   ** the record t
1d5d9 6f 20 30 2e 20 54 68 69 73 20 74 65 6c 6c 73 20  o 0. This tells 
1d5da 63 6f 64 65 20 62 65 6c 6f 77 20 74 6f 20 73 74  code below to st
1d5db 6f 72 65 20 61 20 4e 55 4c 4c 0a 20 20 20 20 20  ore a NULL.     
1d5dc 20 20 20 2a 2a 20 69 6e 73 74 65 61 64 20 6f 66     ** instead of
1d5dd 20 64 65 73 65 72 69 61 6c 69 7a 69 6e 67 20 61   deserializing a
1d5de 20 76 61 6c 75 65 20 66 72 6f 6d 20 74 68 65 20   value from the 
1d5df 72 65 63 6f 72 64 2e 0a 20 20 20 20 20 20 20 20  record..        
1d5e0 2a 2f 0a 20 20 20 20 20 20 20 20 75 2e 61 6d 2e  */.        u.am.
1d5e1 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 69 5d 20  aOffset[u.am.i] 
1d5e2 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
1d5e3 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
1d5e4 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 75 2e  beMemRelease(&u.
1d5e5 61 6d 2e 73 4d 65 6d 29 3b 0a 20 20 20 20 75 2e  am.sMem);.    u.
1d5e6 61 6d 2e 73 4d 65 6d 2e 66 6c 61 67 73 20 3d 20  am.sMem.flags = 
1d5e7 4d 45 4d 5f 4e 75 6c 6c 3b 0a 0a 20 20 20 20 2f  MEM_Null;..    /
1d5e8 2a 20 49 66 20 77 65 20 68 61 76 65 20 72 65 61  * If we have rea
1d5e9 64 20 6d 6f 72 65 20 68 65 61 64 65 72 20 64 61  d more header da
1d5ea 74 61 20 74 68 61 6e 20 77 61 73 20 63 6f 6e 74  ta than was cont
1d5eb 61 69 6e 65 64 20 69 6e 20 74 68 65 20 68 65 61  ained in the hea
1d5ec 64 65 72 2c 0a 20 20 20 20 2a 2a 20 6f 72 20 69  der,.    ** or i
1d5ed 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  f the end of the
1d5ee 20 6c 61 73 74 20 66 69 65 6c 64 20 61 70 70 65   last field appe
1d5ef 61 72 73 20 74 6f 20 62 65 20 70 61 73 74 20 74  ars to be past t
1d5f0 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 20 20  he end of the.  
1d5f1 20 20 2a 2a 20 72 65 63 6f 72 64 2c 20 6f 72 20    ** record, or 
1d5f2 69 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  if the end of th
1d5f3 65 20 6c 61 73 74 20 66 69 65 6c 64 20 61 70 70  e last field app
1d5f4 65 61 72 73 20 74 6f 20 62 65 20 62 65 66 6f 72  ears to be befor
1d5f5 65 20 74 68 65 20 65 6e 64 0a 20 20 20 20 2a 2a  e the end.    **
1d5f6 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 20 28   of the record (
1d5f7 77 68 65 6e 20 61 6c 6c 20 66 69 65 6c 64 73 20  when all fields 
1d5f8 70 72 65 73 65 6e 74 29 2c 20 74 68 65 6e 20 77  present), then w
1d5f9 65 20 6d 75 73 74 20 62 65 20 64 65 61 6c 69 6e  e must be dealin
1d5fa 67 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 61 20  g.    ** with a 
1d5fb 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65  corrupt database
1d5fc 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1d5fd 20 28 75 2e 61 6d 2e 7a 49 64 78 20 3e 20 75 2e   (u.am.zIdx > u.
1d5fe 61 6d 2e 7a 45 6e 64 48 64 72 29 7c 7c 20 28 75  am.zEndHdr)|| (u
1d5ff 2e 61 6d 2e 6f 66 66 73 65 74 36 34 20 3e 20 75  .am.offset64 > u
1d600 2e 61 6d 2e 70 61 79 6c 6f 61 64 53 69 7a 65 29  .am.payloadSize)
1d601 0a 20 20 20 20 20 7c 7c 20 28 75 2e 61 6d 2e 7a  .     || (u.am.z
1d602 49 64 78 3d 3d 75 2e 61 6d 2e 7a 45 6e 64 48 64  Idx==u.am.zEndHd
1d603 72 20 26 26 20 75 2e 61 6d 2e 6f 66 66 73 65 74  r && u.am.offset
1d604 36 34 21 3d 28 75 36 34 29 75 2e 61 6d 2e 70 61  64!=(u64)u.am.pa
1d605 79 6c 6f 61 64 53 69 7a 65 29 20 29 7b 0a 20 20  yloadSize) ){.  
1d606 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1d607 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
1d608 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75      goto op_colu
1d609 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  mn_out;.    }.  
1d60a 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20  }..  /* Get the 
1d60b 63 6f 6c 75 6d 6e 20 69 6e 66 6f 72 6d 61 74 69  column informati
1d60c 6f 6e 2e 20 49 66 20 75 2e 61 6d 2e 61 4f 66 66  on. If u.am.aOff
1d60d 73 65 74 5b 75 2e 61 6d 2e 70 32 5d 20 69 73 20  set[u.am.p2] is 
1d60e 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 0a 20  non-zero, then. 
1d60f 20 2a 2a 20 64 65 73 65 72 69 61 6c 69 7a 65 20   ** deserialize 
1d610 74 68 65 20 76 61 6c 75 65 20 66 72 6f 6d 20 74  the value from t
1d611 68 65 20 72 65 63 6f 72 64 2e 20 49 66 20 75 2e  he record. If u.
1d612 61 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e  am.aOffset[u.am.
1d613 70 32 5d 20 69 73 20 7a 65 72 6f 2c 0a 20 20 2a  p2] is zero,.  *
1d614 2a 20 74 68 65 6e 20 74 68 65 72 65 20 61 72 65  * then there are
1d615 20 6e 6f 74 20 65 6e 6f 75 67 68 20 66 69 65 6c   not enough fiel
1d616 64 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64  ds in the record
1d617 20 74 6f 20 73 61 74 69 73 66 79 20 74 68 65 0a   to satisfy the.
1d618 20 20 2a 2a 20 72 65 71 75 65 73 74 2e 20 20 49    ** request.  I
1d619 6e 20 74 68 69 73 20 63 61 73 65 2c 20 73 65 74  n this case, set
1d61a 20 74 68 65 20 76 61 6c 75 65 20 4e 55 4c 4c 20   the value NULL 
1d61b 6f 72 20 74 6f 20 50 34 20 69 66 20 50 34 20 69  or to P4 if P4 i
1d61c 73 0a 20 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72  s.  ** a pointer
1d61d 20 74 6f 20 61 20 4d 65 6d 20 6f 62 6a 65 63 74   to a Mem object
1d61e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 75 2e 61  ..  */.  if( u.a
1d61f 6d 2e 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70  m.aOffset[u.am.p
1d620 32 5d 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  2] ){.    assert
1d621 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1d622 29 3b 0a 20 20 20 20 69 66 28 20 75 2e 61 6d 2e  );.    if( u.am.
1d623 7a 52 65 63 20 29 7b 0a 20 20 20 20 20 20 73 71  zRec ){.      sq
1d624 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
1d625 61 73 65 45 78 74 65 72 6e 61 6c 28 75 2e 61 6d  aseExternal(u.am
1d626 2e 70 44 65 73 74 29 3b 0a 20 20 20 20 20 20 73  .pDest);.      s
1d627 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
1d628 47 65 74 28 28 75 38 20 2a 29 26 75 2e 61 6d 2e  Get((u8 *)&u.am.
1d629 7a 52 65 63 5b 75 2e 61 6d 2e 61 4f 66 66 73 65  zRec[u.am.aOffse
1d62a 74 5b 75 2e 61 6d 2e 70 32 5d 5d 2c 20 75 2e 61  t[u.am.p2]], u.a
1d62b 6d 2e 61 54 79 70 65 5b 75 2e 61 6d 2e 70 32 5d  m.aType[u.am.p2]
1d62c 2c 20 75 2e 61 6d 2e 70 44 65 73 74 29 3b 0a 20  , u.am.pDest);. 
1d62d 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1d62e 75 2e 61 6d 2e 6c 65 6e 20 3d 20 73 71 6c 69 74  u.am.len = sqlit
1d62f 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
1d630 4c 65 6e 28 75 2e 61 6d 2e 61 54 79 70 65 5b 75  Len(u.am.aType[u
1d631 2e 61 6d 2e 70 32 5d 29 3b 0a 20 20 20 20 20 20  .am.p2]);.      
1d632 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f  sqlite3VdbeMemMo
1d633 76 65 28 26 75 2e 61 6d 2e 73 4d 65 6d 2c 20 75  ve(&u.am.sMem, u
1d634 2e 61 6d 2e 70 44 65 73 74 29 3b 0a 20 20 20 20  .am.pDest);.    
1d635 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1d636 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65 28 75  beMemFromBtree(u
1d637 2e 61 6d 2e 70 43 72 73 72 2c 20 75 2e 61 6d 2e  .am.pCrsr, u.am.
1d638 61 4f 66 66 73 65 74 5b 75 2e 61 6d 2e 70 32 5d  aOffset[u.am.p2]
1d639 2c 20 75 2e 61 6d 2e 6c 65 6e 2c 20 75 2e 61 6d  , u.am.len, u.am
1d63a 2e 70 43 2d 3e 69 73 49 6e 64 65 78 2c 20 26 75  .pC->isIndex, &u
1d63b 2e 61 6d 2e 73 4d 65 6d 29 3b 0a 20 20 20 20 20  .am.sMem);.     
1d63c 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1d63d 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  OK ){.        go
1d63e 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74  to op_column_out
1d63f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1d640 75 2e 61 6d 2e 7a 44 61 74 61 20 3d 20 75 2e 61  u.am.zData = u.a
1d641 6d 2e 73 4d 65 6d 2e 7a 3b 0a 20 20 20 20 20 20  m.sMem.z;.      
1d642 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1d643 6c 47 65 74 28 28 75 38 2a 29 75 2e 61 6d 2e 7a  lGet((u8*)u.am.z
1d644 44 61 74 61 2c 20 75 2e 61 6d 2e 61 54 79 70 65  Data, u.am.aType
1d645 5b 75 2e 61 6d 2e 70 32 5d 2c 20 75 2e 61 6d 2e  [u.am.p2], u.am.
1d646 70 44 65 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20  pDest);.    }.  
1d647 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 65 6e    u.am.pDest->en
1d648 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20  c = encoding;.  
1d649 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 70  }else{.    if( p
1d64a 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4d  Op->p4type==P4_M
1d64b 45 4d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  EM ){.      sqli
1d64c 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f  te3VdbeMemShallo
1d64d 77 43 6f 70 79 28 75 2e 61 6d 2e 70 44 65 73 74  wCopy(u.am.pDest
1d64e 2c 20 70 4f 70 2d 3e 70 34 2e 70 4d 65 6d 2c 20  , pOp->p4.pMem, 
1d64f 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20  MEM_Static);.   
1d650 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73   }else{.      as
1d651 73 65 72 74 28 20 75 2e 61 6d 2e 70 44 65 73 74  sert( u.am.pDest
1d652 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c  ->flags&MEM_Null
1d653 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   );.    }.  }.. 
1d654 20 2f 2a 20 49 66 20 77 65 20 64 79 6e 61 6d 69   /* If we dynami
1d655 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  cally allocated 
1d656 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68  space to hold th
1d657 65 20 64 61 74 61 20 28 69 6e 20 74 68 65 0a 20  e data (in the. 
1d658 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 4d   ** sqlite3VdbeM
1d659 65 6d 46 72 6f 6d 42 74 72 65 65 28 29 20 63 61  emFromBtree() ca
1d65a 6c 6c 20 61 62 6f 76 65 29 20 74 68 65 6e 20 74  ll above) then t
1d65b 72 61 6e 73 66 65 72 20 63 6f 6e 74 72 6f 6c 20  ransfer control 
1d65c 6f 66 20 74 68 61 74 0a 20 20 2a 2a 20 64 79 6e  of that.  ** dyn
1d65d 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74  amically allocat
1d65e 65 64 20 73 70 61 63 65 20 6f 76 65 72 20 74 6f  ed space over to
1d65f 20 74 68 65 20 75 2e 61 6d 2e 70 44 65 73 74 20   the u.am.pDest 
1d660 73 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2a 20  structure..  ** 
1d661 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 20  This prevents a 
1d662 6d 65 6d 6f 72 79 20 63 6f 70 79 2e 0a 20 20 2a  memory copy..  *
1d663 2f 0a 20 20 69 66 28 20 75 2e 61 6d 2e 73 4d 65  /.  if( u.am.sMe
1d664 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20  m.zMalloc ){.   
1d665 20 61 73 73 65 72 74 28 20 75 2e 61 6d 2e 73 4d   assert( u.am.sM
1d666 65 6d 2e 7a 3d 3d 75 2e 61 6d 2e 73 4d 65 6d 2e  em.z==u.am.sMem.
1d667 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20 20 20 61  zMalloc );.    a
1d668 73 73 65 72 74 28 20 21 28 75 2e 61 6d 2e 70 44  ssert( !(u.am.pD
1d669 65 73 74 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  est->flags & MEM
1d66a 5f 44 79 6e 29 20 29 3b 0a 20 20 20 20 61 73 73  _Dyn) );.    ass
1d66b 65 72 74 28 20 21 28 75 2e 61 6d 2e 70 44 65 73  ert( !(u.am.pDes
1d66c 74 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  t->flags & (MEM_
1d66d 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 20 7c  Blob|MEM_Str)) |
1d66e 7c 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e 7a 3d  | u.am.pDest->z=
1d66f 3d 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 20 29 3b 0a  =u.am.sMem.z );.
1d670 20 20 20 20 75 2e 61 6d 2e 70 44 65 73 74 2d 3e      u.am.pDest->
1d671 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 45  flags &= ~(MEM_E
1d672 70 68 65 6d 7c 4d 45 4d 5f 53 74 61 74 69 63 29  phem|MEM_Static)
1d673 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 44 65 73 74  ;.    u.am.pDest
1d674 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 54  ->flags |= MEM_T
1d675 65 72 6d 3b 0a 20 20 20 20 75 2e 61 6d 2e 70 44  erm;.    u.am.pD
1d676 65 73 74 2d 3e 7a 20 3d 20 75 2e 61 6d 2e 73 4d  est->z = u.am.sM
1d677 65 6d 2e 7a 3b 0a 20 20 20 20 75 2e 61 6d 2e 70  em.z;.    u.am.p
1d678 44 65 73 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20  Dest->zMalloc = 
1d679 75 2e 61 6d 2e 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f  u.am.sMem.zMallo
1d67a 63 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73  c;.  }..  rc = s
1d67b 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b  qlite3VdbeMemMak
1d67c 65 57 72 69 74 65 61 62 6c 65 28 75 2e 61 6d 2e  eWriteable(u.am.
1d67d 70 44 65 73 74 29 3b 0a 0a 6f 70 5f 63 6f 6c 75  pDest);..op_colu
1d67e 6d 6e 5f 6f 75 74 3a 0a 20 20 55 50 44 41 54 45  mn_out:.  UPDATE
1d67f 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 75 2e  _MAX_BLOBSIZE(u.
1d680 61 6d 2e 70 44 65 73 74 29 3b 0a 20 20 52 45 47  am.pDest);.  REG
1d681 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1d682 3e 70 33 2c 20 75 2e 61 6d 2e 70 44 65 73 74 29  >p3, u.am.pDest)
1d683 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1d684 20 4f 70 63 6f 64 65 3a 20 41 66 66 69 6e 69 74   Opcode: Affinit
1d685 79 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a  y P1 P2 * P4 *.*
1d686 2a 0a 2a 2a 20 41 70 70 6c 79 20 61 66 66 69 6e  *.** Apply affin
1d687 69 74 69 65 73 20 74 6f 20 61 20 72 61 6e 67 65  ities to a range
1d688 20 6f 66 20 50 32 20 72 65 67 69 73 74 65 72 73   of P2 registers
1d689 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 50   starting with P
1d68a 31 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73 20 61  1..**.** P4 is a
1d68b 20 73 74 72 69 6e 67 20 74 68 61 74 20 69 73 20   string that is 
1d68c 50 32 20 63 68 61 72 61 63 74 65 72 73 20 6c 6f  P2 characters lo
1d68d 6e 67 2e 20 54 68 65 20 6e 74 68 20 63 68 61 72  ng. The nth char
1d68e 61 63 74 65 72 20 6f 66 20 74 68 65 0a 2a 2a 20  acter of the.** 
1d68f 73 74 72 69 6e 67 20 69 6e 64 69 63 61 74 65 73  string indicates
1d690 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69   the column affi
1d691 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64  nity that should
1d692 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65   be used for the
1d693 20 6e 74 68 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63   nth.** memory c
1d694 65 6c 6c 20 69 6e 20 74 68 65 20 72 61 6e 67 65  ell in the range
1d695 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 66 66  ..*/.case OP_Aff
1d696 69 6e 69 74 79 3a 20 7b 0a 23 69 66 20 30 20 20  inity: {.#if 0  
1d697 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1d698 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1d699 61 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 41  an */.  char *zA
1d69a 66 66 69 6e 69 74 79 3b 20 20 20 2f 2a 20 54 68  ffinity;   /* Th
1d69b 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 62 65  e affinity to be
1d69c 20 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20 4d 65   applied */.  Me
1d69d 6d 20 2a 70 44 61 74 61 30 3b 20 20 20 20 20 20  m *pData0;      
1d69e 20 2f 2a 20 46 69 72 73 74 20 72 65 67 69 73 74   /* First regist
1d69f 65 72 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61  er to which to a
1d6a0 70 70 6c 79 20 61 66 66 69 6e 69 74 79 20 2a 2f  pply affinity */
1d6a1 0a 20 20 4d 65 6d 20 2a 70 4c 61 73 74 3b 20 20  .  Mem *pLast;  
1d6a2 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 72 65        /* Last re
1d6a3 67 69 73 74 65 72 20 74 6f 20 77 68 69 63 68 20  gister to which 
1d6a4 74 6f 20 61 70 70 6c 79 20 61 66 66 69 6e 69 74  to apply affinit
1d6a5 79 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 63  y */.  Mem *pRec
1d6a6 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72  ;         /* Cur
1d6a7 72 65 6e 74 20 72 65 67 69 73 74 65 72 20 2a 2f  rent register */
1d6a8 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1d6a9 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1d6aa 20 69 6e 74 6f 20 75 2e 61 6e 20 2a 2f 0a 0a 20   into u.an */.. 
1d6ab 20 75 2e 61 6e 2e 7a 41 66 66 69 6e 69 74 79 20   u.an.zAffinity 
1d6ac 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 75  = pOp->p4.z;.  u
1d6ad 2e 61 6e 2e 70 44 61 74 61 30 20 3d 20 26 70 2d  .an.pData0 = &p-
1d6ae 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  >aMem[pOp->p1];.
1d6af 20 20 75 2e 61 6e 2e 70 4c 61 73 74 20 3d 20 26    u.an.pLast = &
1d6b0 75 2e 61 6e 2e 70 44 61 74 61 30 5b 70 4f 70 2d  u.an.pData0[pOp-
1d6b1 3e 70 32 2d 31 5d 3b 0a 20 20 66 6f 72 28 75 2e  >p2-1];.  for(u.
1d6b2 61 6e 2e 70 52 65 63 3d 75 2e 61 6e 2e 70 44 61  an.pRec=u.an.pDa
1d6b3 74 61 30 3b 20 75 2e 61 6e 2e 70 52 65 63 3c 3d  ta0; u.an.pRec<=
1d6b4 75 2e 61 6e 2e 70 4c 61 73 74 3b 20 75 2e 61 6e  u.an.pLast; u.an
1d6b5 2e 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 45 78  .pRec++){.    Ex
1d6b6 70 61 6e 64 42 6c 6f 62 28 75 2e 61 6e 2e 70 52  pandBlob(u.an.pR
1d6b7 65 63 29 3b 0a 20 20 20 20 61 70 70 6c 79 41 66  ec);.    applyAf
1d6b8 66 69 6e 69 74 79 28 75 2e 61 6e 2e 70 52 65 63  finity(u.an.pRec
1d6b9 2c 20 75 2e 61 6e 2e 7a 41 66 66 69 6e 69 74 79  , u.an.zAffinity
1d6ba 5b 75 2e 61 6e 2e 70 52 65 63 2d 75 2e 61 6e 2e  [u.an.pRec-u.an.
1d6bb 70 44 61 74 61 30 5d 2c 20 65 6e 63 6f 64 69 6e  pData0], encodin
1d6bc 67 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  g);.  }.  break;
1d6bd 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d  .}../* Opcode: M
1d6be 61 6b 65 52 65 63 6f 72 64 20 50 31 20 50 32 20  akeRecord P1 P2 
1d6bf 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 43 6f  P3 P4 *.**.** Co
1d6c0 6e 76 65 72 74 20 50 32 20 72 65 67 69 73 74 65  nvert P2 registe
1d6c1 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74  rs beginning wit
1d6c2 68 20 50 31 20 69 6e 74 6f 20 61 20 73 69 6e 67  h P1 into a sing
1d6c3 6c 65 20 65 6e 74 72 79 0a 2a 2a 20 73 75 69 74  le entry.** suit
1d6c4 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61 73 20  able for use as 
1d6c5 61 20 64 61 74 61 20 72 65 63 6f 72 64 20 69 6e  a data record in
1d6c6 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62 6c   a database tabl
1d6c7 65 20 6f 72 20 61 73 20 61 20 6b 65 79 0a 2a 2a  e or as a key.**
1d6c8 20 69 6e 20 61 6e 20 69 6e 64 65 78 2e 20 20 54   in an index.  T
1d6c9 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68  he details of th
1d6ca 65 20 66 6f 72 6d 61 74 20 61 72 65 20 69 72 72  e format are irr
1d6cb 65 6c 65 76 61 6e 74 20 61 73 20 6c 6f 6e 67 20  elevant as long 
1d6cc 61 73 0a 2a 2a 20 74 68 65 20 4f 50 5f 43 6f 6c  as.** the OP_Col
1d6cd 75 6d 6e 20 6f 70 63 6f 64 65 20 63 61 6e 20 64  umn opcode can d
1d6ce 65 63 6f 64 65 20 74 68 65 20 72 65 63 6f 72 64  ecode the record
1d6cf 20 6c 61 74 65 72 2e 0a 2a 2a 20 52 65 66 65 72   later..** Refer
1d6d0 20 74 6f 20 73 6f 75 72 63 65 20 63 6f 64 65 20   to source code 
1d6d1 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 74 68 65  comments for the
1d6d2 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 65 20   details of the 
1d6d3 72 65 63 6f 72 64 0a 2a 2a 20 66 6f 72 6d 61 74  record.** format
1d6d4 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 6d 61 79 20 62  ..**.** P4 may b
1d6d5 65 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20  e a string that 
1d6d6 69 73 20 50 32 20 63 68 61 72 61 63 74 65 72 73  is P2 characters
1d6d7 20 6c 6f 6e 67 2e 20 20 54 68 65 20 6e 74 68 20   long.  The nth 
1d6d8 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65  character of the
1d6d9 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 64 69 63  .** string indic
1d6da 61 74 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20  ates the column 
1d6db 61 66 66 69 6e 69 74 79 20 74 68 61 74 20 73 68  affinity that sh
1d6dc 6f 75 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72  ould be used for
1d6dd 20 74 68 65 20 6e 74 68 0a 2a 2a 20 66 69 65 6c   the nth.** fiel
1d6de 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 6b  d of the index k
1d6df 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61  ey..**.** The ma
1d6e0 70 70 69 6e 67 20 66 72 6f 6d 20 63 68 61 72 61  pping from chara
1d6e1 63 74 65 72 20 74 6f 20 61 66 66 69 6e 69 74 79  cter to affinity
1d6e2 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68 65   is given by the
1d6e3 20 53 51 4c 49 54 45 5f 41 46 46 5f 0a 2a 2a 20   SQLITE_AFF_.** 
1d6e4 6d 61 63 72 6f 73 20 64 65 66 69 6e 65 64 20 69  macros defined i
1d6e5 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 2e 0a 2a  n sqliteInt.h..*
1d6e6 2a 0a 2a 2a 20 49 66 20 50 34 20 69 73 20 4e 55  *.** If P4 is NU
1d6e7 4c 4c 20 74 68 65 6e 20 61 6c 6c 20 69 6e 64 65  LL then all inde
1d6e8 78 20 66 69 65 6c 64 73 20 68 61 76 65 20 74 68  x fields have th
1d6e9 65 20 61 66 66 69 6e 69 74 79 20 4e 4f 4e 45 2e  e affinity NONE.
1d6ea 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 61 6b 65  .*/.case OP_Make
1d6eb 52 65 63 6f 72 64 3a 20 7b 0a 23 69 66 20 30 20  Record: {.#if 0 
1d6ec 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1d6ed 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1d6ee 2e 61 6f 20 2a 2f 0a 20 20 75 38 20 2a 7a 4e 65  .ao */.  u8 *zNe
1d6ef 77 52 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20  wRecord;        
1d6f0 2f 2a 20 41 20 62 75 66 66 65 72 20 74 6f 20 68  /* A buffer to h
1d6f1 6f 6c 64 20 74 68 65 20 64 61 74 61 20 66 6f 72  old the data for
1d6f2 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20   the new record 
1d6f3 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 65 63 3b 20  */.  Mem *pRec; 
1d6f4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1d6f5 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f  he new record */
1d6f6 0a 20 20 75 36 34 20 6e 44 61 74 61 3b 20 20 20  .  u64 nData;   
1d6f7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1d6f8 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
1d6f9 64 61 74 61 20 73 70 61 63 65 20 2a 2f 0a 20 20  data space */.  
1d6fa 69 6e 74 20 6e 48 64 72 3b 20 20 20 20 20 20 20  int nHdr;       
1d6fb 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1d6fc 20 6f 66 20 62 79 74 65 73 20 6f 66 20 68 65 61   of bytes of hea
1d6fd 64 65 72 20 73 70 61 63 65 20 2a 2f 0a 20 20 69  der space */.  i
1d6fe 36 34 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20  64 nByte;       
1d6ff 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 73 70        /* Data sp
1d700 61 63 65 20 72 65 71 75 69 72 65 64 20 66 6f 72  ace required for
1d701 20 74 68 69 73 20 72 65 63 6f 72 64 20 2a 2f 0a   this record */.
1d702 20 20 69 6e 74 20 6e 5a 65 72 6f 3b 20 20 20 20    int nZero;    
1d703 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
1d704 65 72 20 6f 66 20 7a 65 72 6f 20 62 79 74 65 73  er of zero bytes
1d705 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74   at the end of t
1d706 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69  he record */.  i
1d707 6e 74 20 6e 56 61 72 69 6e 74 3b 20 20 20 20 20  nt nVarint;     
1d708 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1d709 6f 66 20 62 79 74 65 73 20 69 6e 20 61 20 76 61  of bytes in a va
1d70a 72 69 6e 74 20 2a 2f 0a 20 20 75 33 32 20 73 65  rint */.  u32 se
1d70b 72 69 61 6c 5f 74 79 70 65 3b 20 20 20 20 20 20  rial_type;      
1d70c 20 2f 2a 20 54 79 70 65 20 66 69 65 6c 64 20 2a   /* Type field *
1d70d 2f 0a 20 20 4d 65 6d 20 2a 70 44 61 74 61 30 3b  /.  Mem *pData0;
1d70e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
1d70f 72 73 74 20 66 69 65 6c 64 20 74 6f 20 62 65 20  rst field to be 
1d710 63 6f 6d 62 69 6e 65 64 20 69 6e 74 6f 20 74 68  combined into th
1d711 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 4d 65  e record */.  Me
1d712 6d 20 2a 70 4c 61 73 74 3b 20 20 20 20 20 20 20  m *pLast;       
1d713 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 66 69 65       /* Last fie
1d714 6c 64 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64  ld of the record
1d715 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64   */.  int nField
1d716 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
1d717 4e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73  Number of fields
1d718 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a   in the record *
1d719 2f 0a 20 20 63 68 61 72 20 2a 7a 41 66 66 69 6e  /.  char *zAffin
1d71a 69 74 79 3b 20 20 20 20 20 20 20 2f 2a 20 54 68  ity;       /* Th
1d71b 65 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e  e affinity strin
1d71c 67 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64  g for the record
1d71d 20 2a 2f 0a 20 20 69 6e 74 20 66 69 6c 65 5f 66   */.  int file_f
1d71e 6f 72 6d 61 74 3b 20 20 20 20 20 20 20 2f 2a 20  ormat;       /* 
1d71f 46 69 6c 65 20 66 6f 72 6d 61 74 20 74 6f 20 75  File format to u
1d720 73 65 20 66 6f 72 20 65 6e 63 6f 64 69 6e 67 20  se for encoding 
1d721 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  */.  int i;     
1d722 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
1d723 70 61 63 65 20 75 73 65 64 20 69 6e 20 7a 4e 65  pace used in zNe
1d724 77 52 65 63 6f 72 64 5b 5d 20 2a 2f 0a 20 20 69  wRecord[] */.  i
1d725 6e 74 20 6c 65 6e 3b 20 20 20 20 20 20 20 20 20  nt len;         
1d726 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
1d727 6f 66 20 61 20 66 69 65 6c 64 20 2a 2f 0a 23 65  of a field */.#e
1d728 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1d729 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1d72a 74 6f 20 75 2e 61 6f 20 2a 2f 0a 0a 20 20 2f 2a  to u.ao */..  /*
1d72b 20 41 73 73 75 6d 69 6e 67 20 74 68 65 20 72 65   Assuming the re
1d72c 63 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20 4e 20  cord contains N 
1d72d 66 69 65 6c 64 73 2c 20 74 68 65 20 72 65 63 6f  fields, the reco
1d72e 72 64 20 66 6f 72 6d 61 74 20 6c 6f 6f 6b 73 0a  rd format looks.
1d72f 20 20 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a    ** like this:.
1d730 20 20 2a 2a 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d    **.  ** ------
1d731 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d732 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d733 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d734 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d735 2d 2d 0a 20 20 2a 2a 20 7c 20 68 64 72 2d 73 69  --.  ** | hdr-si
1d736 7a 65 20 7c 20 74 79 70 65 20 30 20 7c 20 74 79  ze | type 0 | ty
1d737 70 65 20 31 20 7c 20 2e 2e 2e 20 7c 20 74 79 70  pe 1 | ... | typ
1d738 65 20 4e 2d 31 20 7c 20 64 61 74 61 30 20 7c 20  e N-1 | data0 | 
1d739 2e 2e 2e 20 7c 20 64 61 74 61 20 4e 2d 31 20 7c  ... | data N-1 |
1d73a 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .  ** ----------
1d73b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d73c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d73d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1d73e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20  --------------. 
1d73f 20 2a 2a 0a 20 20 2a 2a 20 44 61 74 61 28 30 29   **.  ** Data(0)
1d740 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 72   is taken from r
1d741 65 67 69 73 74 65 72 20 50 31 2e 20 20 44 61 74  egister P1.  Dat
1d742 61 28 31 29 20 63 6f 6d 65 73 20 66 72 6f 6d 20  a(1) comes from 
1d743 72 65 67 69 73 74 65 72 20 50 31 2b 31 0a 20 20  register P1+1.  
1d744 2a 2a 20 61 6e 64 20 73 6f 20 66 72 6f 74 68 2e  ** and so froth.
1d745 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 61 63 68 20  .  **.  ** Each 
1d746 74 79 70 65 20 66 69 65 6c 64 20 69 73 20 61 20  type field is a 
1d747 76 61 72 69 6e 74 20 72 65 70 72 65 73 65 6e 74  varint represent
1d748 69 6e 67 20 74 68 65 20 73 65 72 69 61 6c 20 74  ing the serial t
1d749 79 70 65 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20  ype of the.  ** 
1d74a 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 64 61  corresponding da
1d74b 74 61 20 65 6c 65 6d 65 6e 74 20 28 73 65 65 20  ta element (see 
1d74c 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1d74d 6c 54 79 70 65 28 29 29 2e 20 54 68 65 0a 20 20  lType()). The.  
1d74e 2a 2a 20 68 64 72 2d 73 69 7a 65 20 66 69 65 6c  ** hdr-size fiel
1d74f 64 20 69 73 20 61 6c 73 6f 20 61 20 76 61 72 69  d is also a vari
1d750 6e 74 20 77 68 69 63 68 20 69 73 20 74 68 65 20  nt which is the 
1d751 6f 66 66 73 65 74 20 66 72 6f 6d 20 74 68 65 20  offset from the 
1d752 62 65 67 69 6e 6e 69 6e 67 0a 20 20 2a 2a 20 6f  beginning.  ** o
1d753 66 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20  f the record to 
1d754 64 61 74 61 30 2e 0a 20 20 2a 2f 0a 20 20 75 2e  data0..  */.  u.
1d755 61 6f 2e 6e 44 61 74 61 20 3d 20 30 3b 20 20 20  ao.nData = 0;   
1d756 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1d757 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61 74 61  of bytes of data
1d758 20 73 70 61 63 65 20 2a 2f 0a 20 20 75 2e 61 6f   space */.  u.ao
1d759 2e 6e 48 64 72 20 3d 20 30 3b 20 20 20 20 20 20  .nHdr = 0;      
1d75a 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1d75b 20 62 79 74 65 73 20 6f 66 20 68 65 61 64 65 72   bytes of header
1d75c 20 73 70 61 63 65 20 2a 2f 0a 20 20 75 2e 61 6f   space */.  u.ao
1d75d 2e 6e 42 79 74 65 20 3d 20 30 3b 20 20 20 20 20  .nByte = 0;     
1d75e 20 20 20 20 2f 2a 20 44 61 74 61 20 73 70 61 63      /* Data spac
1d75f 65 20 72 65 71 75 69 72 65 64 20 66 6f 72 20 74  e required for t
1d760 68 69 73 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  his record */.  
1d761 75 2e 61 6f 2e 6e 5a 65 72 6f 20 3d 20 30 3b 20  u.ao.nZero = 0; 
1d762 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1d763 72 20 6f 66 20 7a 65 72 6f 20 62 79 74 65 73 20  r of zero bytes 
1d764 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  at the end of th
1d765 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 2e  e record */.  u.
1d766 61 6f 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d  ao.nField = pOp-
1d767 3e 70 31 3b 0a 20 20 75 2e 61 6f 2e 7a 41 66 66  >p1;.  u.ao.zAff
1d768 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 34 2e  inity = pOp->p4.
1d769 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 61  z;.  assert( u.a
1d76a 6f 2e 6e 46 69 65 6c 64 3e 30 20 26 26 20 70 4f  o.nField>0 && pO
1d76b 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e  p->p2>0 && pOp->
1d76c 70 32 2b 75 2e 61 6f 2e 6e 46 69 65 6c 64 3c 3d  p2+u.ao.nField<=
1d76d 70 2d 3e 6e 4d 65 6d 2b 31 20 29 3b 0a 20 20 75  p->nMem+1 );.  u
1d76e 2e 61 6f 2e 70 44 61 74 61 30 20 3d 20 26 70 2d  .ao.pData0 = &p-
1d76f 3e 61 4d 65 6d 5b 75 2e 61 6f 2e 6e 46 69 65 6c  >aMem[u.ao.nFiel
1d770 64 5d 3b 0a 20 20 75 2e 61 6f 2e 6e 46 69 65 6c  d];.  u.ao.nFiel
1d771 64 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75  d = pOp->p2;.  u
1d772 2e 61 6f 2e 70 4c 61 73 74 20 3d 20 26 75 2e 61  .ao.pLast = &u.a
1d773 6f 2e 70 44 61 74 61 30 5b 75 2e 61 6f 2e 6e 46  o.pData0[u.ao.nF
1d774 69 65 6c 64 2d 31 5d 3b 0a 20 20 75 2e 61 6f 2e  ield-1];.  u.ao.
1d775 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 70 2d  file_format = p-
1d776 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46 6f 72  >minWriteFileFor
1d777 6d 61 74 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20  mat;..  /* Loop 
1d778 74 68 72 6f 75 67 68 20 74 68 65 20 65 6c 65 6d  through the elem
1d779 65 6e 74 73 20 74 68 61 74 20 77 69 6c 6c 20 6d  ents that will m
1d77a 61 6b 65 20 75 70 20 74 68 65 20 72 65 63 6f 72  ake up the recor
1d77b 64 20 74 6f 20 66 69 67 75 72 65 0a 20 20 2a 2a  d to figure.  **
1d77c 20 6f 75 74 20 68 6f 77 20 6d 75 63 68 20 73 70   out how much sp
1d77d 61 63 65 20 69 73 20 72 65 71 75 69 72 65 64 20  ace is required 
1d77e 66 6f 72 20 74 68 65 20 6e 65 77 20 72 65 63 6f  for the new reco
1d77f 72 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 75  rd..  */.  for(u
1d780 2e 61 6f 2e 70 52 65 63 3d 75 2e 61 6f 2e 70 44  .ao.pRec=u.ao.pD
1d781 61 74 61 30 3b 20 75 2e 61 6f 2e 70 52 65 63 3c  ata0; u.ao.pRec<
1d782 3d 75 2e 61 6f 2e 70 4c 61 73 74 3b 20 75 2e 61  =u.ao.pLast; u.a
1d783 6f 2e 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 69  o.pRec++){.    i
1d784 66 28 20 75 2e 61 6f 2e 7a 41 66 66 69 6e 69 74  f( u.ao.zAffinit
1d785 79 20 29 7b 0a 20 20 20 20 20 20 61 70 70 6c 79  y ){.      apply
1d786 41 66 66 69 6e 69 74 79 28 75 2e 61 6f 2e 70 52  Affinity(u.ao.pR
1d787 65 63 2c 20 75 2e 61 6f 2e 7a 41 66 66 69 6e 69  ec, u.ao.zAffini
1d788 74 79 5b 75 2e 61 6f 2e 70 52 65 63 2d 75 2e 61  ty[u.ao.pRec-u.a
1d789 6f 2e 70 44 61 74 61 30 5d 2c 20 65 6e 63 6f 64  o.pData0], encod
1d78a 69 6e 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ing);.    }.    
1d78b 69 66 28 20 75 2e 61 6f 2e 70 52 65 63 2d 3e 66  if( u.ao.pRec->f
1d78c 6c 61 67 73 26 4d 45 4d 5f 5a 65 72 6f 20 26 26  lags&MEM_Zero &&
1d78d 20 75 2e 61 6f 2e 70 52 65 63 2d 3e 6e 3e 30 20   u.ao.pRec->n>0 
1d78e 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1d78f 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f  VdbeMemExpandBlo
1d790 62 28 75 2e 61 6f 2e 70 52 65 63 29 3b 0a 20 20  b(u.ao.pRec);.  
1d791 20 20 7d 0a 20 20 20 20 75 2e 61 6f 2e 73 65 72    }.    u.ao.ser
1d792 69 61 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74  ial_type = sqlit
1d793 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
1d794 28 75 2e 61 6f 2e 70 52 65 63 2c 20 75 2e 61 6f  (u.ao.pRec, u.ao
1d795 2e 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20  .file_format);. 
1d796 20 20 20 75 2e 61 6f 2e 6c 65 6e 20 3d 20 73 71     u.ao.len = sq
1d797 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54  lite3VdbeSerialT
1d798 79 70 65 4c 65 6e 28 75 2e 61 6f 2e 73 65 72 69  ypeLen(u.ao.seri
1d799 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 75 2e  al_type);.    u.
1d79a 61 6f 2e 6e 44 61 74 61 20 2b 3d 20 75 2e 61 6f  ao.nData += u.ao
1d79b 2e 6c 65 6e 3b 0a 20 20 20 20 75 2e 61 6f 2e 6e  .len;.    u.ao.n
1d79c 48 64 72 20 2b 3d 20 73 71 6c 69 74 65 33 56 61  Hdr += sqlite3Va
1d79d 72 69 6e 74 4c 65 6e 28 75 2e 61 6f 2e 73 65 72  rintLen(u.ao.ser
1d79e 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 69  ial_type);.    i
1d79f 66 28 20 75 2e 61 6f 2e 70 52 65 63 2d 3e 66 6c  f( u.ao.pRec->fl
1d7a0 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29  ags & MEM_Zero )
1d7a1 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20  {.      /* Only 
1d7a2 70 75 72 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64  pure zero-filled
1d7a3 20 42 4c 4f 42 73 20 63 61 6e 20 62 65 20 69 6e   BLOBs can be in
1d7a4 70 75 74 20 74 6f 20 74 68 69 73 20 4f 70 63 6f  put to this Opco
1d7a5 64 65 2e 0a 20 20 20 20 20 20 2a 2a 20 57 65 20  de..      ** We 
1d7a6 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 62 6c 6f  do not allow blo
1d7a7 62 73 20 77 69 74 68 20 61 20 70 72 65 66 69 78  bs with a prefix
1d7a8 20 61 6e 64 20 61 20 7a 65 72 6f 2d 66 69 6c 6c   and a zero-fill
1d7a9 65 64 20 74 61 69 6c 2e 20 2a 2f 0a 20 20 20 20  ed tail. */.    
1d7aa 20 20 75 2e 61 6f 2e 6e 5a 65 72 6f 20 2b 3d 20    u.ao.nZero += 
1d7ab 75 2e 61 6f 2e 70 52 65 63 2d 3e 75 2e 6e 5a 65  u.ao.pRec->u.nZe
1d7ac 72 6f 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  ro;.    }else if
1d7ad 28 20 75 2e 61 6f 2e 6c 65 6e 20 29 7b 0a 20 20  ( u.ao.len ){.  
1d7ae 20 20 20 20 75 2e 61 6f 2e 6e 5a 65 72 6f 20 3d      u.ao.nZero =
1d7af 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   0;.    }.  }.. 
1d7b0 20 2f 2a 20 41 64 64 20 74 68 65 20 69 6e 69 74   /* Add the init
1d7b1 69 61 6c 20 68 65 61 64 65 72 20 76 61 72 69 6e  ial header varin
1d7b2 74 20 61 6e 64 20 74 6f 74 61 6c 20 74 68 65 20  t and total the 
1d7b3 73 69 7a 65 20 2a 2f 0a 20 20 75 2e 61 6f 2e 6e  size */.  u.ao.n
1d7b4 48 64 72 20 2b 3d 20 75 2e 61 6f 2e 6e 56 61 72  Hdr += u.ao.nVar
1d7b5 69 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 61 72  int = sqlite3Var
1d7b6 69 6e 74 4c 65 6e 28 75 2e 61 6f 2e 6e 48 64 72  intLen(u.ao.nHdr
1d7b7 29 3b 0a 20 20 69 66 28 20 75 2e 61 6f 2e 6e 56  );.  if( u.ao.nV
1d7b8 61 72 69 6e 74 3c 73 71 6c 69 74 65 33 56 61 72  arint<sqlite3Var
1d7b9 69 6e 74 4c 65 6e 28 75 2e 61 6f 2e 6e 48 64 72  intLen(u.ao.nHdr
1d7ba 29 20 29 7b 0a 20 20 20 20 75 2e 61 6f 2e 6e 48  ) ){.    u.ao.nH
1d7bb 64 72 2b 2b 3b 0a 20 20 7d 0a 20 20 75 2e 61 6f  dr++;.  }.  u.ao
1d7bc 2e 6e 42 79 74 65 20 3d 20 75 2e 61 6f 2e 6e 48  .nByte = u.ao.nH
1d7bd 64 72 2b 75 2e 61 6f 2e 6e 44 61 74 61 2d 75 2e  dr+u.ao.nData-u.
1d7be 61 6f 2e 6e 5a 65 72 6f 3b 0a 20 20 69 66 28 20  ao.nZero;.  if( 
1d7bf 75 2e 61 6f 2e 6e 42 79 74 65 3e 64 62 2d 3e 61  u.ao.nByte>db->a
1d7c0 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
1d7c1 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20  IT_LENGTH] ){.  
1d7c2 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a    goto too_big;.
1d7c3 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73    }..  /* Make s
1d7c4 75 72 65 20 74 68 65 20 6f 75 74 70 75 74 20 72  ure the output r
1d7c5 65 67 69 73 74 65 72 20 68 61 73 20 61 20 62 75  egister has a bu
1d7c6 66 66 65 72 20 6c 61 72 67 65 20 65 6e 6f 75 67  ffer large enoug
1d7c7 68 20 74 6f 20 73 74 6f 72 65 0a 20 20 2a 2a 20  h to store.  ** 
1d7c8 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 2e 20  the new record. 
1d7c9 54 68 65 20 6f 75 74 70 75 74 20 72 65 67 69 73  The output regis
1d7ca 74 65 72 20 28 70 4f 70 2d 3e 70 33 29 20 69 73  ter (pOp->p3) is
1d7cb 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 0a   not allowed to.
1d7cc 20 20 2a 2a 20 62 65 20 6f 6e 65 20 6f 66 20 74    ** be one of t
1d7cd 68 65 20 69 6e 70 75 74 20 72 65 67 69 73 74 65  he input registe
1d7ce 72 73 20 28 62 65 63 61 75 73 65 20 74 68 65 20  rs (because the 
1d7cf 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 74  following call t
1d7d0 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64  o.  ** sqlite3Vd
1d7d1 62 65 4d 65 6d 47 72 6f 77 28 29 20 63 6f 75 6c  beMemGrow() coul
1d7d2 64 20 63 6c 6f 62 62 65 72 20 74 68 65 20 76 61  d clobber the va
1d7d3 6c 75 65 20 62 65 66 6f 72 65 20 69 74 20 69 73  lue before it is
1d7d4 20 75 73 65 64 29 2e 0a 20 20 2a 2f 0a 20 20 61   used)..  */.  a
1d7d5 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 70  ssert( pOp->p3<p
1d7d6 4f 70 2d 3e 70 31 20 7c 7c 20 70 4f 70 2d 3e 70  Op->p1 || pOp->p
1d7d7 33 3e 3d 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e  3>=pOp->p1+pOp->
1d7d8 70 32 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26  p2 );.  pOut = &
1d7d9 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d  p->aMem[pOp->p3]
1d7da 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56  ;.  if( sqlite3V
1d7db 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c  dbeMemGrow(pOut,
1d7dc 20 28 69 6e 74 29 75 2e 61 6f 2e 6e 42 79 74 65   (int)u.ao.nByte
1d7dd 2c 20 30 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  , 0) ){.    goto
1d7de 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 75   no_mem;.  }.  u
1d7df 2e 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64 20 3d  .ao.zNewRecord =
1d7e0 20 28 75 38 20 2a 29 70 4f 75 74 2d 3e 7a 3b 0a   (u8 *)pOut->z;.
1d7e1 0a 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20  .  /* Write the 
1d7e2 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 2e 61 6f  record */.  u.ao
1d7e3 2e 69 20 3d 20 70 75 74 56 61 72 69 6e 74 33 32  .i = putVarint32
1d7e4 28 75 2e 61 6f 2e 7a 4e 65 77 52 65 63 6f 72 64  (u.ao.zNewRecord
1d7e5 2c 20 75 2e 61 6f 2e 6e 48 64 72 29 3b 0a 20 20  , u.ao.nHdr);.  
1d7e6 66 6f 72 28 75 2e 61 6f 2e 70 52 65 63 3d 75 2e  for(u.ao.pRec=u.
1d7e7 61 6f 2e 70 44 61 74 61 30 3b 20 75 2e 61 6f 2e  ao.pData0; u.ao.
1d7e8 70 52 65 63 3c 3d 75 2e 61 6f 2e 70 4c 61 73 74  pRec<=u.ao.pLast
1d7e9 3b 20 75 2e 61 6f 2e 70 52 65 63 2b 2b 29 7b 0a  ; u.ao.pRec++){.
1d7ea 20 20 20 20 75 2e 61 6f 2e 73 65 72 69 61 6c 5f      u.ao.serial_
1d7eb 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64  type = sqlite3Vd
1d7ec 62 65 53 65 72 69 61 6c 54 79 70 65 28 75 2e 61  beSerialType(u.a
1d7ed 6f 2e 70 52 65 63 2c 20 75 2e 61 6f 2e 66 69 6c  o.pRec, u.ao.fil
1d7ee 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20 20 20 75  e_format);.    u
1d7ef 2e 61 6f 2e 69 20 2b 3d 20 70 75 74 56 61 72 69  .ao.i += putVari
1d7f0 6e 74 33 32 28 26 75 2e 61 6f 2e 7a 4e 65 77 52  nt32(&u.ao.zNewR
1d7f1 65 63 6f 72 64 5b 75 2e 61 6f 2e 69 5d 2c 20 75  ecord[u.ao.i], u
1d7f2 2e 61 6f 2e 73 65 72 69 61 6c 5f 74 79 70 65 29  .ao.serial_type)
1d7f3 3b 20 20 20 20 20 20 2f 2a 20 73 65 72 69 61 6c  ;      /* serial
1d7f4 20 74 79 70 65 20 2a 2f 0a 20 20 7d 0a 20 20 66   type */.  }.  f
1d7f5 6f 72 28 75 2e 61 6f 2e 70 52 65 63 3d 75 2e 61  or(u.ao.pRec=u.a
1d7f6 6f 2e 70 44 61 74 61 30 3b 20 75 2e 61 6f 2e 70  o.pData0; u.ao.p
1d7f7 52 65 63 3c 3d 75 2e 61 6f 2e 70 4c 61 73 74 3b  Rec<=u.ao.pLast;
1d7f8 20 75 2e 61 6f 2e 70 52 65 63 2b 2b 29 7b 20 20   u.ao.pRec++){  
1d7f9 2f 2a 20 73 65 72 69 61 6c 20 64 61 74 61 20 2a  /* serial data *
1d7fa 2f 0a 20 20 20 20 75 2e 61 6f 2e 69 20 2b 3d 20  /.    u.ao.i += 
1d7fb 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61  sqlite3VdbeSeria
1d7fc 6c 50 75 74 28 26 75 2e 61 6f 2e 7a 4e 65 77 52  lPut(&u.ao.zNewR
1d7fd 65 63 6f 72 64 5b 75 2e 61 6f 2e 69 5d 2c 20 28  ecord[u.ao.i], (
1d7fe 69 6e 74 29 28 75 2e 61 6f 2e 6e 42 79 74 65 2d  int)(u.ao.nByte-
1d7ff 75 2e 61 6f 2e 69 29 2c 20 75 2e 61 6f 2e 70 52  u.ao.i), u.ao.pR
1d800 65 63 2c 75 2e 61 6f 2e 66 69 6c 65 5f 66 6f 72  ec,u.ao.file_for
1d801 6d 61 74 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  mat);.  }.  asse
1d802 72 74 28 20 75 2e 61 6f 2e 69 3d 3d 75 2e 61 6f  rt( u.ao.i==u.ao
1d803 2e 6e 42 79 74 65 20 29 3b 0a 0a 20 20 61 73 73  .nByte );..  ass
1d804 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26  ert( pOp->p3>0 &
1d805 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d  & pOp->p3<=p->nM
1d806 65 6d 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 6e 20  em );.  pOut->n 
1d807 3d 20 28 69 6e 74 29 75 2e 61 6f 2e 6e 42 79 74  = (int)u.ao.nByt
1d808 65 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  e;.  pOut->flags
1d809 20 3d 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d 45   = MEM_Blob | ME
1d80a 4d 5f 44 79 6e 3b 0a 20 20 70 4f 75 74 2d 3e 78  M_Dyn;.  pOut->x
1d80b 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 75  Del = 0;.  if( u
1d80c 2e 61 6f 2e 6e 5a 65 72 6f 20 29 7b 0a 20 20 20  .ao.nZero ){.   
1d80d 20 70 4f 75 74 2d 3e 75 2e 6e 5a 65 72 6f 20 3d   pOut->u.nZero =
1d80e 20 75 2e 61 6f 2e 6e 5a 65 72 6f 3b 0a 20 20 20   u.ao.nZero;.   
1d80f 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20   pOut->flags |= 
1d810 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 7d 0a 20 20  MEM_Zero;.  }.  
1d811 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 51 4c 49  pOut->enc = SQLI
1d812 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20 49 6e 20  TE_UTF8;  /* In 
1d813 63 61 73 65 20 74 68 65 20 62 6c 6f 62 20 69 73  case the blob is
1d814 20 65 76 65 72 20 63 6f 6e 76 65 72 74 65 64 20   ever converted 
1d815 74 6f 20 74 65 78 74 20 2a 2f 0a 20 20 52 45 47  to text */.  REG
1d816 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
1d817 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20 55 50  >p3, pOut);.  UP
1d818 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a  DATE_MAX_BLOBSIZ
1d819 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b  E(pOut);.  break
1d81a 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1d81b 43 6f 75 6e 74 20 50 31 20 50 32 20 2a 20 2a 20  Count P1 P2 * * 
1d81c 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20 74 68  *.**.** Store th
1d81d 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  e number of entr
1d81e 69 65 73 20 28 61 6e 20 69 6e 74 65 67 65 72 20  ies (an integer 
1d81f 76 61 6c 75 65 29 20 69 6e 20 74 68 65 20 74 61  value) in the ta
1d820 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 0a 2a 2a  ble or index .**
1d821 20 6f 70 65 6e 65 64 20 62 79 20 63 75 72 73 6f   opened by curso
1d822 72 20 50 31 20 69 6e 20 72 65 67 69 73 74 65 72  r P1 in register
1d823 20 50 32 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53   P2.*/.#ifndef S
1d824 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54 52 45 45  QLITE_OMIT_BTREE
1d825 43 4f 55 4e 54 0a 63 61 73 65 20 4f 50 5f 43 6f  COUNT.case OP_Co
1d826 75 6e 74 3a 20 7b 20 20 20 20 20 20 20 20 20 2f  unt: {         /
1d827 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
1d828 65 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  e */.#if 0  /* l
1d829 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1d82a 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 70 20 2a  oved into u.ap *
1d82b 2f 0a 20 20 69 36 34 20 6e 45 6e 74 72 79 3b 0a  /.  i64 nEntry;.
1d82c 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73    BtCursor *pCrs
1d82d 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  r;.#endif /* loc
1d82e 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1d82f 65 64 20 69 6e 74 6f 20 75 2e 61 70 20 2a 2f 0a  ed into u.ap */.
1d830 0a 20 20 75 2e 61 70 2e 70 43 72 73 72 20 3d 20  .  u.ap.pCrsr = 
1d831 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31  p->apCsr[pOp->p1
1d832 5d 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66  ]->pCursor;.  if
1d833 28 20 75 2e 61 70 2e 70 43 72 73 72 20 29 7b 0a  ( u.ap.pCrsr ){.
1d834 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1d835 42 74 72 65 65 43 6f 75 6e 74 28 75 2e 61 70 2e  BtreeCount(u.ap.
1d836 70 43 72 73 72 2c 20 26 75 2e 61 70 2e 6e 45 6e  pCrsr, &u.ap.nEn
1d837 74 72 79 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  try);.  }else{. 
1d838 20 20 20 75 2e 61 70 2e 6e 45 6e 74 72 79 20 3d     u.ap.nEntry =
1d839 20 30 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e   0;.  }.  pOut->
1d83a 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
1d83b 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75  .  pOut->u.i = u
1d83c 2e 61 70 2e 6e 45 6e 74 72 79 3b 0a 20 20 62 72  .ap.nEntry;.  br
1d83d 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  eak;.}.#endif../
1d83e 2a 20 4f 70 63 6f 64 65 3a 20 53 61 76 65 70 6f  * Opcode: Savepo
1d83f 69 6e 74 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a  int P1 * * P4 *.
1d840 2a 2a 0a 2a 2a 20 4f 70 65 6e 2c 20 72 65 6c 65  **.** Open, rele
1d841 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20  ase or rollback 
1d842 74 68 65 20 73 61 76 65 70 6f 69 6e 74 20 6e 61  the savepoint na
1d843 6d 65 64 20 62 79 20 70 61 72 61 6d 65 74 65 72  med by parameter
1d844 20 50 34 2c 20 64 65 70 65 6e 64 69 6e 67 0a 2a   P4, depending.*
1d845 2a 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f  * on the value o
1d846 66 20 50 31 2e 20 54 6f 20 6f 70 65 6e 20 61 20  f P1. To open a 
1d847 6e 65 77 20 73 61 76 65 70 6f 69 6e 74 2c 20 50  new savepoint, P
1d848 31 3d 3d 30 2e 20 54 6f 20 72 65 6c 65 61 73 65  1==0. To release
1d849 20 28 63 6f 6d 6d 69 74 29 20 61 6e 0a 2a 2a 20   (commit) an.** 
1d84a 65 78 69 73 74 69 6e 67 20 73 61 76 65 70 6f 69  existing savepoi
1d84b 6e 74 2c 20 50 31 3d 3d 31 2c 20 6f 72 20 74 6f  nt, P1==1, or to
1d84c 20 72 6f 6c 6c 62 61 63 6b 20 61 6e 20 65 78 69   rollback an exi
1d84d 73 74 69 6e 67 20 73 61 76 65 70 6f 69 6e 74 20  sting savepoint 
1d84e 50 31 3d 3d 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f  P1==2..*/.case O
1d84f 50 5f 53 61 76 65 70 6f 69 6e 74 3a 20 7b 0a 23  P_Savepoint: {.#
1d850 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1d851 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1d852 6e 74 6f 20 75 2e 61 71 20 2a 2f 0a 20 20 69 6e  nto u.aq */.  in
1d853 74 20 70 31 3b 20 20 20 20 20 20 20 20 20 20 20  t p1;           
1d854 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1d855 20 56 61 6c 75 65 20 6f 66 20 50 31 20 6f 70 65   Value of P1 ope
1d856 72 61 6e 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  rand */.  char *
1d857 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  zName;          
1d858 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
1d859 65 20 6f 66 20 73 61 76 65 70 6f 69 6e 74 20 2a  e of savepoint *
1d85a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20  /.  int nName;. 
1d85b 20 53 61 76 65 70 6f 69 6e 74 20 2a 70 4e 65 77   Savepoint *pNew
1d85c 3b 0a 20 20 53 61 76 65 70 6f 69 6e 74 20 2a 70  ;.  Savepoint *p
1d85d 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 53 61 76  Savepoint;.  Sav
1d85e 65 70 6f 69 6e 74 20 2a 70 54 6d 70 3b 0a 20 20  epoint *pTmp;.  
1d85f 69 6e 74 20 69 53 61 76 65 70 6f 69 6e 74 3b 0a  int iSavepoint;.
1d860 20 20 69 6e 74 20 69 69 3b 0a 23 65 6e 64 69 66    int ii;.#endif
1d861 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1d862 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1d863 2e 61 71 20 2a 2f 0a 0a 20 20 75 2e 61 71 2e 70  .aq */..  u.aq.p
1d864 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 75  1 = pOp->p1;.  u
1d865 2e 61 71 2e 7a 4e 61 6d 65 20 3d 20 70 4f 70 2d  .aq.zName = pOp-
1d866 3e 70 34 2e 7a 3b 0a 0a 20 20 2f 2a 20 41 73 73  >p4.z;..  /* Ass
1d867 65 72 74 20 74 68 61 74 20 74 68 65 20 75 2e 61  ert that the u.a
1d868 71 2e 70 31 20 70 61 72 61 6d 65 74 65 72 20 69  q.p1 parameter i
1d869 73 20 76 61 6c 69 64 2e 20 41 6c 73 6f 20 74 68  s valid. Also th
1d86a 61 74 20 69 66 20 74 68 65 72 65 20 69 73 20 6e  at if there is n
1d86b 6f 20 6f 70 65 6e 0a 20 20 2a 2a 20 74 72 61 6e  o open.  ** tran
1d86c 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68  saction, then th
1d86d 65 72 65 20 63 61 6e 6e 6f 74 20 62 65 20 61 6e  ere cannot be an
1d86e 79 20 73 61 76 65 70 6f 69 6e 74 73 2e 0a 20 20  y savepoints..  
1d86f 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  */.  assert( db-
1d870 3e 70 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20 7c  >pSavepoint==0 |
1d871 7c 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74  | db->autoCommit
1d872 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
1d873 20 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f   u.aq.p1==SAVEPO
1d874 49 4e 54 5f 42 45 47 49 4e 7c 7c 75 2e 61 71 2e  INT_BEGIN||u.aq.
1d875 70 31 3d 3d 53 41 56 45 50 4f 49 4e 54 5f 52 45  p1==SAVEPOINT_RE
1d876 4c 45 41 53 45 7c 7c 75 2e 61 71 2e 70 31 3d 3d  LEASE||u.aq.p1==
1d877 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
1d878 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  CK );.  assert( 
1d879 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20 7c  db->pSavepoint |
1d87a 7c 20 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74  | db->isTransact
1d87b 69 6f 6e 53 61 76 65 70 6f 69 6e 74 3d 3d 30 20  ionSavepoint==0 
1d87c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 63 68 65  );.  assert( che
1d87d 63 6b 53 61 76 65 70 6f 69 6e 74 43 6f 75 6e 74  ckSavepointCount
1d87e 28 64 62 29 20 29 3b 0a 0a 20 20 69 66 28 20 75  (db) );..  if( u
1d87f 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f 49 4e  .aq.p1==SAVEPOIN
1d880 54 5f 42 45 47 49 4e 20 29 7b 0a 20 20 20 20 69  T_BEGIN ){.    i
1d881 66 28 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65  f( db->writeVdbe
1d882 43 6e 74 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f  Cnt>0 ){.      /
1d883 2a 20 41 20 6e 65 77 20 73 61 76 65 70 6f 69 6e  * A new savepoin
1d884 74 20 63 61 6e 6e 6f 74 20 62 65 20 63 72 65 61  t cannot be crea
1d885 74 65 64 20 69 66 20 74 68 65 72 65 20 61 72 65  ted if there are
1d886 20 61 63 74 69 76 65 20 77 72 69 74 65 0a 20 20   active write.  
1d887 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74      ** statement
1d888 73 20 28 69 2e 65 2e 20 6f 70 65 6e 20 72 65 61  s (i.e. open rea
1d889 64 2f 77 72 69 74 65 20 69 6e 63 72 65 6d 65 6e  d/write incremen
1d88a 74 61 6c 20 62 6c 6f 62 20 68 61 6e 64 6c 65 73  tal blob handles
1d88b 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  )..      */.    
1d88c 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
1d88d 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
1d88e 64 62 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e  db, "cannot open
1d88f 20 73 61 76 65 70 6f 69 6e 74 20 2d 20 22 0a 20   savepoint - ". 
1d890 20 20 20 20 20 20 20 22 53 51 4c 20 73 74 61 74         "SQL stat
1d891 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65  ements in progre
1d892 73 73 22 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  ss");.      rc =
1d893 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
1d894 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 75    }else{.      u
1d895 2e 61 71 2e 6e 4e 61 6d 65 20 3d 20 73 71 6c 69  .aq.nName = sqli
1d896 74 65 33 53 74 72 6c 65 6e 33 30 28 75 2e 61 71  te3Strlen30(u.aq
1d897 2e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20 20 20 20  .zName);..      
1d898 2f 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  /* Create a new 
1d899 73 61 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74  savepoint struct
1d89a 75 72 65 2e 20 2a 2f 0a 20 20 20 20 20 20 75 2e  ure. */.      u.
1d89b 61 71 2e 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  aq.pNew = sqlite
1d89c 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c  3DbMallocRaw(db,
1d89d 20 73 69 7a 65 6f 66 28 53 61 76 65 70 6f 69 6e   sizeof(Savepoin
1d89e 74 29 2b 75 2e 61 71 2e 6e 4e 61 6d 65 2b 31 29  t)+u.aq.nName+1)
1d89f 3b 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61 71  ;.      if( u.aq
1d8a0 2e 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20  .pNew ){.       
1d8a1 20 75 2e 61 71 2e 70 4e 65 77 2d 3e 7a 4e 61 6d   u.aq.pNew->zNam
1d8a2 65 20 3d 20 28 63 68 61 72 20 2a 29 26 75 2e 61  e = (char *)&u.a
1d8a3 71 2e 70 4e 65 77 5b 31 5d 3b 0a 20 20 20 20 20  q.pNew[1];.     
1d8a4 20 20 20 6d 65 6d 63 70 79 28 75 2e 61 71 2e 70     memcpy(u.aq.p
1d8a5 4e 65 77 2d 3e 7a 4e 61 6d 65 2c 20 75 2e 61 71  New->zName, u.aq
1d8a6 2e 7a 4e 61 6d 65 2c 20 75 2e 61 71 2e 6e 4e 61  .zName, u.aq.nNa
1d8a7 6d 65 2b 31 29 3b 0a 0a 20 20 20 20 20 20 20 20  me+1);..        
1d8a8 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  /* If there is n
1d8a9 6f 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 74 69  o open transacti
1d8aa 6f 6e 2c 20 74 68 65 6e 20 6d 61 72 6b 20 74 68  on, then mark th
1d8ab 69 73 20 61 73 20 61 20 73 70 65 63 69 61 6c 0a  is as a special.
1d8ac 20 20 20 20 20 20 20 20 2a 2a 20 22 74 72 61 6e          ** "tran
1d8ad 73 61 63 74 69 6f 6e 20 73 61 76 65 70 6f 69 6e  saction savepoin
1d8ae 74 22 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  t". */.        i
1d8af 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  f( db->autoCommi
1d8b0 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64  t ){.          d
1d8b1 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20  b->autoCommit = 
1d8b2 30 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d  0;.          db-
1d8b3 3e 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 53 61  >isTransactionSa
1d8b4 76 65 70 6f 69 6e 74 20 3d 20 31 3b 0a 20 20 20  vepoint = 1;.   
1d8b5 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1d8b6 20 20 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 70        db->nSavep
1d8b7 6f 69 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20  oint++;.        
1d8b8 7d 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4c 69  }..        /* Li
1d8b9 6e 6b 20 74 68 65 20 6e 65 77 20 73 61 76 65 70  nk the new savep
1d8ba 6f 69 6e 74 20 69 6e 74 6f 20 74 68 65 20 64 61  oint into the da
1d8bb 74 61 62 61 73 65 20 68 61 6e 64 6c 65 27 73 20  tabase handle's 
1d8bc 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20  list. */.       
1d8bd 20 75 2e 61 71 2e 70 4e 65 77 2d 3e 70 4e 65 78   u.aq.pNew->pNex
1d8be 74 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f 69  t = db->pSavepoi
1d8bf 6e 74 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e  nt;.        db->
1d8c0 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61  pSavepoint = u.a
1d8c1 71 2e 70 4e 65 77 3b 0a 20 20 20 20 20 20 20 20  q.pNew;.        
1d8c2 75 2e 61 71 2e 70 4e 65 77 2d 3e 6e 44 65 66 65  u.aq.pNew->nDefe
1d8c3 72 72 65 64 43 6f 6e 73 20 3d 20 64 62 2d 3e 6e  rredCons = db->n
1d8c4 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20  DeferredCons;.  
1d8c5 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
1d8c6 6c 73 65 7b 0a 20 20 20 20 75 2e 61 71 2e 69 53  lse{.    u.aq.iS
1d8c7 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a 0a 20  avepoint = 0;.. 
1d8c8 20 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 6e     /* Find the n
1d8c9 61 6d 65 64 20 73 61 76 65 70 6f 69 6e 74 2e 20  amed savepoint. 
1d8ca 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 73  If there is no s
1d8cb 75 63 68 20 73 61 76 65 70 6f 69 6e 74 2c 20 74  uch savepoint, t
1d8cc 68 65 6e 20 61 6e 0a 20 20 20 20 2a 2a 20 61 6e  hen an.    ** an
1d8cd 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e   error is return
1d8ce 65 64 20 74 6f 20 74 68 65 20 75 73 65 72 2e 20  ed to the user. 
1d8cf 20 2a 2f 0a 20 20 20 20 66 6f 72 28 0a 20 20 20   */.    for(.   
1d8d0 20 20 20 75 2e 61 71 2e 70 53 61 76 65 70 6f 69     u.aq.pSavepoi
1d8d1 6e 74 20 3d 20 64 62 2d 3e 70 53 61 76 65 70 6f  nt = db->pSavepo
1d8d2 69 6e 74 3b 0a 20 20 20 20 20 20 75 2e 61 71 2e  int;.      u.aq.
1d8d3 70 53 61 76 65 70 6f 69 6e 74 20 26 26 20 73 71  pSavepoint && sq
1d8d4 6c 69 74 65 33 53 74 72 49 43 6d 70 28 75 2e 61  lite3StrICmp(u.a
1d8d5 71 2e 70 53 61 76 65 70 6f 69 6e 74 2d 3e 7a 4e  q.pSavepoint->zN
1d8d6 61 6d 65 2c 20 75 2e 61 71 2e 7a 4e 61 6d 65 29  ame, u.aq.zName)
1d8d7 3b 0a 20 20 20 20 20 20 75 2e 61 71 2e 70 53 61  ;.      u.aq.pSa
1d8d8 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61 71 2e 70  vepoint = u.aq.p
1d8d9 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e 65 78 74  Savepoint->pNext
1d8da 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 75 2e  .    ){.      u.
1d8db 61 71 2e 69 53 61 76 65 70 6f 69 6e 74 2b 2b 3b  aq.iSavepoint++;
1d8dc 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21  .    }.    if( !
1d8dd 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 20  u.aq.pSavepoint 
1d8de 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1d8df 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
1d8e0 72 72 4d 73 67 2c 20 64 62 2c 20 22 6e 6f 20 73  rrMsg, db, "no s
1d8e1 75 63 68 20 73 61 76 65 70 6f 69 6e 74 3a 20 25  uch savepoint: %
1d8e2 73 22 2c 20 75 2e 61 71 2e 7a 4e 61 6d 65 29 3b  s", u.aq.zName);
1d8e3 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
1d8e4 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 65  TE_ERROR;.    }e
1d8e5 6c 73 65 20 69 66 28 0a 20 20 20 20 20 20 20 20  lse if(.        
1d8e6 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74  db->writeVdbeCnt
1d8e7 3e 30 20 7c 7c 20 28 75 2e 61 71 2e 70 31 3d 3d  >0 || (u.aq.p1==
1d8e8 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
1d8e9 43 4b 20 26 26 20 64 62 2d 3e 61 63 74 69 76 65  CK && db->active
1d8ea 56 64 62 65 43 6e 74 3e 31 29 0a 20 20 20 20 29  VdbeCnt>1).    )
1d8eb 7b 0a 20 20 20 20 20 20 2f 2a 20 49 74 20 69 73  {.      /* It is
1d8ec 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f   not possible to
1d8ed 20 72 65 6c 65 61 73 65 20 28 63 6f 6d 6d 69 74   release (commit
1d8ee 29 20 61 20 73 61 76 65 70 6f 69 6e 74 20 69 66  ) a savepoint if
1d8ef 20 74 68 65 72 65 20 61 72 65 0a 20 20 20 20 20   there are.     
1d8f0 20 2a 2a 20 61 63 74 69 76 65 20 77 72 69 74 65   ** active write
1d8f1 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 49 74 20   statements. It 
1d8f2 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  is not possible 
1d8f3 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 61 20 73 61  to rollback a sa
1d8f4 76 65 70 6f 69 6e 74 0a 20 20 20 20 20 20 2a 2a  vepoint.      **
1d8f5 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e   if there are an
1d8f6 79 20 61 63 74 69 76 65 20 73 74 61 74 65 6d 65  y active stateme
1d8f7 6e 74 73 20 61 74 20 61 6c 6c 2e 0a 20 20 20 20  nts at all..    
1d8f8 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74    */.      sqlit
1d8f9 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
1d8fa 7a 45 72 72 4d 73 67 2c 20 64 62 2c 0a 20 20 20  zErrMsg, db,.   
1d8fb 20 20 20 20 20 22 63 61 6e 6e 6f 74 20 25 73 20       "cannot %s 
1d8fc 73 61 76 65 70 6f 69 6e 74 20 2d 20 53 51 4c 20  savepoint - SQL 
1d8fd 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 70 72  statements in pr
1d8fe 6f 67 72 65 73 73 22 2c 0a 20 20 20 20 20 20 20  ogress",.       
1d8ff 20 28 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50   (u.aq.p1==SAVEP
1d900 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 3f 20  OINT_ROLLBACK ? 
1d901 22 72 6f 6c 6c 62 61 63 6b 22 3a 20 22 72 65 6c  "rollback": "rel
1d902 65 61 73 65 22 29 0a 20 20 20 20 20 20 29 3b 0a  ease").      );.
1d903 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1d904 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73  E_BUSY;.    }els
1d905 65 7b 0a 0a 20 20 20 20 20 20 2f 2a 20 44 65 74  e{..      /* Det
1d906 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f  ermine whether o
1d907 72 20 6e 6f 74 20 74 68 69 73 20 69 73 20 61 20  r not this is a 
1d908 74 72 61 6e 73 61 63 74 69 6f 6e 20 73 61 76 65  transaction save
1d909 70 6f 69 6e 74 2e 20 49 66 20 73 6f 2c 0a 20 20  point. If so,.  
1d90a 20 20 20 20 2a 2a 20 61 6e 64 20 74 68 69 73 20      ** and this 
1d90b 69 73 20 61 20 52 45 4c 45 41 53 45 20 63 6f 6d  is a RELEASE com
1d90c 6d 61 6e 64 2c 20 74 68 65 6e 20 74 68 65 20 63  mand, then the c
1d90d 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69  urrent transacti
1d90e 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 63  on.      ** is c
1d90f 6f 6d 6d 69 74 74 65 64 2e 0a 20 20 20 20 20 20  ommitted..      
1d910 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 73 54  */.      int isT
1d911 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 75 2e 61  ransaction = u.a
1d912 71 2e 70 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e  q.pSavepoint->pN
1d913 65 78 74 3d 3d 30 20 26 26 20 64 62 2d 3e 69 73  ext==0 && db->is
1d914 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70  TransactionSavep
1d915 6f 69 6e 74 3b 0a 20 20 20 20 20 20 69 66 28 20  oint;.      if( 
1d916 69 73 54 72 61 6e 73 61 63 74 69 6f 6e 20 26 26  isTransaction &&
1d917 20 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f   u.aq.p1==SAVEPO
1d918 49 4e 54 5f 52 45 4c 45 41 53 45 20 29 7b 0a 20  INT_RELEASE ){. 
1d919 20 20 20 20 20 20 20 69 66 28 20 28 72 63 20 3d         if( (rc =
1d91a 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 65 63   sqlite3VdbeChec
1d91b 6b 46 6b 28 70 2c 20 31 29 29 21 3d 53 51 4c 49  kFk(p, 1))!=SQLI
1d91c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1d91d 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74     goto vdbe_ret
1d91e 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  urn;.        }. 
1d91f 20 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43         db->autoC
1d920 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20  ommit = 1;.     
1d921 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64     if( sqlite3Vd
1d922 62 65 48 61 6c 74 28 70 29 3d 3d 53 51 4c 49 54  beHalt(p)==SQLIT
1d923 45 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20 20  E_BUSY ){.      
1d924 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a      p->pc = pc;.
1d925 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61 75            db->au
1d926 74 6f 43 6f 6d 6d 69 74 20 3d 20 30 3b 0a 20 20  toCommit = 0;.  
1d927 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20          p->rc = 
1d928 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
1d929 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f  ;.          goto
1d92a 20 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20   vdbe_return;.  
1d92b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1d92c 64 62 2d 3e 69 73 54 72 61 6e 73 61 63 74 69 6f  db->isTransactio
1d92d 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 30 3b 0a  nSavepoint = 0;.
1d92e 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 2d 3e          rc = p->
1d92f 72 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  rc;.      }else{
1d930 0a 20 20 20 20 20 20 20 20 75 2e 61 71 2e 69 53  .        u.aq.iS
1d931 61 76 65 70 6f 69 6e 74 20 3d 20 64 62 2d 3e 6e  avepoint = db->n
1d932 53 61 76 65 70 6f 69 6e 74 20 2d 20 75 2e 61 71  Savepoint - u.aq
1d933 2e 69 53 61 76 65 70 6f 69 6e 74 20 2d 20 31 3b  .iSavepoint - 1;
1d934 0a 20 20 20 20 20 20 20 20 66 6f 72 28 75 2e 61  .        for(u.a
1d935 71 2e 69 69 3d 30 3b 20 75 2e 61 71 2e 69 69 3c  q.ii=0; u.aq.ii<
1d936 64 62 2d 3e 6e 44 62 3b 20 75 2e 61 71 2e 69 69  db->nDb; u.aq.ii
1d937 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  ++){.          r
1d938 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1d939 53 61 76 65 70 6f 69 6e 74 28 64 62 2d 3e 61 44  Savepoint(db->aD
1d93a 62 5b 75 2e 61 71 2e 69 69 5d 2e 70 42 74 2c 20  b[u.aq.ii].pBt, 
1d93b 75 2e 61 71 2e 70 31 2c 20 75 2e 61 71 2e 69 53  u.aq.p1, u.aq.iS
1d93c 61 76 65 70 6f 69 6e 74 29 3b 0a 20 20 20 20 20  avepoint);.     
1d93d 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1d93e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1d93f 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74        goto abort
1d940 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20  _due_to_error;. 
1d941 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1d942 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
1d943 20 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45 50 4f   u.aq.p1==SAVEPO
1d944 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 26 26 20  INT_ROLLBACK && 
1d945 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54  (db->flags&SQLIT
1d946 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73 29  E_InternChanges)
1d947 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  !=0 ){.         
1d948 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72   sqlite3ExpirePr
1d949 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73  eparedStatements
1d94a 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20  (db);.          
1d94b 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65  sqlite3ResetInte
1d94c 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30  rnalSchema(db, 0
1d94d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
1d94e 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 52     }..      /* R
1d94f 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65  egardless of whe
1d950 74 68 65 72 20 74 68 69 73 20 69 73 20 61 20 52  ther this is a R
1d951 45 4c 45 41 53 45 20 6f 72 20 52 4f 4c 4c 42 41  ELEASE or ROLLBA
1d952 43 4b 2c 20 64 65 73 74 72 6f 79 20 61 6c 6c 0a  CK, destroy all.
1d953 20 20 20 20 20 20 2a 2a 20 73 61 76 65 70 6f 69        ** savepoi
1d954 6e 74 73 20 6e 65 73 74 65 64 20 69 6e 73 69 64  nts nested insid
1d955 65 20 6f 66 20 74 68 65 20 73 61 76 65 70 6f 69  e of the savepoi
1d956 6e 74 20 62 65 69 6e 67 20 6f 70 65 72 61 74 65  nt being operate
1d957 64 20 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 77  d on. */.      w
1d958 68 69 6c 65 28 20 64 62 2d 3e 70 53 61 76 65 70  hile( db->pSavep
1d959 6f 69 6e 74 21 3d 75 2e 61 71 2e 70 53 61 76 65  oint!=u.aq.pSave
1d95a 70 6f 69 6e 74 20 29 7b 0a 20 20 20 20 20 20 20  point ){.       
1d95b 20 75 2e 61 71 2e 70 54 6d 70 20 3d 20 64 62 2d   u.aq.pTmp = db-
1d95c 3e 70 53 61 76 65 70 6f 69 6e 74 3b 0a 20 20 20  >pSavepoint;.   
1d95d 20 20 20 20 20 64 62 2d 3e 70 53 61 76 65 70 6f       db->pSavepo
1d95e 69 6e 74 20 3d 20 75 2e 61 71 2e 70 54 6d 70 2d  int = u.aq.pTmp-
1d95f 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20  >pNext;.        
1d960 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1d961 2c 20 75 2e 61 71 2e 70 54 6d 70 29 3b 0a 20 20  , u.aq.pTmp);.  
1d962 20 20 20 20 20 20 64 62 2d 3e 6e 53 61 76 65 70        db->nSavep
1d963 6f 69 6e 74 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a  oint--;.      }.
1d964 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 69 74 20  .      /* If it 
1d965 69 73 20 61 20 52 45 4c 45 41 53 45 2c 20 74 68  is a RELEASE, th
1d966 65 6e 20 64 65 73 74 72 6f 79 20 74 68 65 20 73  en destroy the s
1d967 61 76 65 70 6f 69 6e 74 20 62 65 69 6e 67 20 6f  avepoint being o
1d968 70 65 72 61 74 65 64 20 6f 6e 0a 20 20 20 20 20  perated on.     
1d969 20 2a 2a 20 74 6f 6f 2e 20 49 66 20 69 74 20 69   ** too. If it i
1d96a 73 20 61 20 52 4f 4c 4c 42 41 43 4b 20 54 4f 2c  s a ROLLBACK TO,
1d96b 20 74 68 65 6e 20 73 65 74 20 74 68 65 20 6e 75   then set the nu
1d96c 6d 62 65 72 20 6f 66 20 64 65 66 65 72 72 65 64  mber of deferred
1d96d 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72  .      ** constr
1d96e 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 73 20  aint violations 
1d96f 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20 64  present in the d
1d970 61 74 61 62 61 73 65 20 74 6f 20 74 68 65 20 76  atabase to the v
1d971 61 6c 75 65 20 73 74 6f 72 65 64 0a 20 20 20 20  alue stored.    
1d972 20 20 2a 2a 20 77 68 65 6e 20 74 68 65 20 73 61    ** when the sa
1d973 76 65 70 6f 69 6e 74 20 77 61 73 20 63 72 65 61  vepoint was crea
1d974 74 65 64 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69  ted.  */.      i
1d975 66 28 20 75 2e 61 71 2e 70 31 3d 3d 53 41 56 45  f( u.aq.p1==SAVE
1d976 50 4f 49 4e 54 5f 52 45 4c 45 41 53 45 20 29 7b  POINT_RELEASE ){
1d977 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1d978 20 75 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74   u.aq.pSavepoint
1d979 3d 3d 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74  ==db->pSavepoint
1d97a 20 29 3b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e   );.        db->
1d97b 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 75 2e 61  pSavepoint = u.a
1d97c 71 2e 70 53 61 76 65 70 6f 69 6e 74 2d 3e 70 4e  q.pSavepoint->pN
1d97d 65 78 74 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ext;.        sql
1d97e 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 75  ite3DbFree(db, u
1d97f 2e 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 29 3b  .aq.pSavepoint);
1d980 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 69 73  .        if( !is
1d981 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20  Transaction ){. 
1d982 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 53 61           db->nSa
1d983 76 65 70 6f 69 6e 74 2d 2d 3b 0a 20 20 20 20 20  vepoint--;.     
1d984 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
1d985 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6e 44  {.        db->nD
1d986 65 66 65 72 72 65 64 43 6f 6e 73 20 3d 20 75 2e  eferredCons = u.
1d987 61 71 2e 70 53 61 76 65 70 6f 69 6e 74 2d 3e 6e  aq.pSavepoint->n
1d988 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20  DeferredCons;.  
1d989 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1d98a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1d98b 4f 70 63 6f 64 65 3a 20 41 75 74 6f 43 6f 6d 6d  Opcode: AutoComm
1d98c 69 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  it P1 P2 * * *.*
1d98d 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 64 61 74  *.** Set the dat
1d98e 61 62 61 73 65 20 61 75 74 6f 2d 63 6f 6d 6d 69  abase auto-commi
1d98f 74 20 66 6c 61 67 20 74 6f 20 50 31 20 28 31 20  t flag to P1 (1 
1d990 6f 72 20 30 29 2e 20 49 66 20 50 32 20 69 73 20  or 0). If P2 is 
1d991 74 72 75 65 2c 20 72 6f 6c 6c 0a 2a 2a 20 62 61  true, roll.** ba
1d992 63 6b 20 61 6e 79 20 63 75 72 72 65 6e 74 6c 79  ck any currently
1d993 20 61 63 74 69 76 65 20 62 74 72 65 65 20 74 72   active btree tr
1d994 61 6e 73 61 63 74 69 6f 6e 73 2e 20 49 66 20 74  ansactions. If t
1d995 68 65 72 65 20 61 72 65 20 61 6e 79 20 61 63 74  here are any act
1d996 69 76 65 0a 2a 2a 20 56 4d 73 20 28 61 70 61 72  ive.** VMs (apar
1d997 74 20 66 72 6f 6d 20 74 68 69 73 20 6f 6e 65 29  t from this one)
1d998 2c 20 74 68 65 6e 20 61 20 52 4f 4c 4c 42 41 43  , then a ROLLBAC
1d999 4b 20 66 61 69 6c 73 2e 20 20 41 20 43 4f 4d 4d  K fails.  A COMM
1d99a 49 54 20 66 61 69 6c 73 20 69 66 0a 2a 2a 20 74  IT fails if.** t
1d99b 68 65 72 65 20 61 72 65 20 61 63 74 69 76 65 20  here are active 
1d99c 77 72 69 74 69 6e 67 20 56 4d 73 20 6f 72 20 61  writing VMs or a
1d99d 63 74 69 76 65 20 56 4d 73 20 74 68 61 74 20 75  ctive VMs that u
1d99e 73 65 20 73 68 61 72 65 64 20 63 61 63 68 65 2e  se shared cache.
1d99f 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74  .**.** This inst
1d9a0 72 75 63 74 69 6f 6e 20 63 61 75 73 65 73 20 74  ruction causes t
1d9a1 68 65 20 56 4d 20 74 6f 20 68 61 6c 74 2e 0a 2a  he VM to halt..*
1d9a2 2f 0a 63 61 73 65 20 4f 50 5f 41 75 74 6f 43 6f  /.case OP_AutoCo
1d9a3 6d 6d 69 74 3a 20 7b 0a 23 69 66 20 30 20 20 2f  mmit: {.#if 0  /
1d9a4 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1d9a5 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1d9a6 72 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73 69 72  r */.  int desir
1d9a7 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20  edAutoCommit;.  
1d9a8 69 6e 74 20 69 52 6f 6c 6c 62 61 63 6b 3b 0a 20  int iRollback;. 
1d9a9 20 69 6e 74 20 74 75 72 6e 4f 6e 41 43 3b 0a 23   int turnOnAC;.#
1d9aa 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1d9ab 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1d9ac 6e 74 6f 20 75 2e 61 72 20 2a 2f 0a 0a 20 20 75  nto u.ar */..  u
1d9ad 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43  .ar.desiredAutoC
1d9ae 6f 6d 6d 69 74 20 3d 20 70 4f 70 2d 3e 70 31 3b  ommit = pOp->p1;
1d9af 0a 20 20 75 2e 61 72 2e 69 52 6f 6c 6c 62 61 63  .  u.ar.iRollbac
1d9b0 6b 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 75  k = pOp->p2;.  u
1d9b1 2e 61 72 2e 74 75 72 6e 4f 6e 41 43 20 3d 20 75  .ar.turnOnAC = u
1d9b2 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43  .ar.desiredAutoC
1d9b3 6f 6d 6d 69 74 20 26 26 20 21 64 62 2d 3e 61 75  ommit && !db->au
1d9b4 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 61 73 73 65  toCommit;.  asse
1d9b5 72 74 28 20 75 2e 61 72 2e 64 65 73 69 72 65 64  rt( u.ar.desired
1d9b6 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c 7c  AutoCommit==1 ||
1d9b7 20 75 2e 61 72 2e 64 65 73 69 72 65 64 41 75 74   u.ar.desiredAut
1d9b8 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 3b 0a 20 20  oCommit==0 );.  
1d9b9 61 73 73 65 72 74 28 20 75 2e 61 72 2e 64 65 73  assert( u.ar.des
1d9ba 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d  iredAutoCommit==
1d9bb 31 20 7c 7c 20 75 2e 61 72 2e 69 52 6f 6c 6c 62  1 || u.ar.iRollb
1d9bc 61 63 6b 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  ack==0 );.  asse
1d9bd 72 74 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64  rt( db->activeVd
1d9be 62 65 43 6e 74 3e 30 20 29 3b 20 20 2f 2a 20 41  beCnt>0 );  /* A
1d9bf 74 20 6c 65 61 73 74 20 74 68 69 73 20 6f 6e 65  t least this one
1d9c0 20 56 4d 20 69 73 20 61 63 74 69 76 65 20 2a 2f   VM is active */
1d9c1 0a 0a 20 20 69 66 28 20 75 2e 61 72 2e 74 75 72  ..  if( u.ar.tur
1d9c2 6e 4f 6e 41 43 20 26 26 20 75 2e 61 72 2e 69 52  nOnAC && u.ar.iR
1d9c3 6f 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d 3e 61  ollback && db->a
1d9c4 63 74 69 76 65 56 64 62 65 43 6e 74 3e 31 20 29  ctiveVdbeCnt>1 )
1d9c5 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73  {.    /* If this
1d9c6 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6d 70   instruction imp
1d9c7 6c 65 6d 65 6e 74 73 20 61 20 52 4f 4c 4c 42 41  lements a ROLLBA
1d9c8 43 4b 20 61 6e 64 20 6f 74 68 65 72 20 56 4d 73  CK and other VMs
1d9c9 20 61 72 65 0a 20 20 20 20 2a 2a 20 73 74 69 6c   are.    ** stil
1d9ca 6c 20 72 75 6e 6e 69 6e 67 2c 20 61 6e 64 20 61  l running, and a
1d9cb 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
1d9cc 61 63 74 69 76 65 2c 20 72 65 74 75 72 6e 20 61  active, return a
1d9cd 6e 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 69  n error indicati
1d9ce 6e 67 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 74  ng.    ** that t
1d9cf 68 65 20 6f 74 68 65 72 20 56 4d 73 20 6d 75 73  he other VMs mus
1d9d0 74 20 63 6f 6d 70 6c 65 74 65 20 66 69 72 73 74  t complete first
1d9d1 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c  ..    */.    sql
1d9d2 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26 70  ite3SetString(&p
1d9d3 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22  ->zErrMsg, db, "
1d9d4 63 61 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20  cannot rollback 
1d9d5 74 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a  transaction - ".
1d9d6 20 20 20 20 20 20 20 20 22 53 51 4c 20 73 74 61          "SQL sta
1d9d7 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72  tements in progr
1d9d8 65 73 73 22 29 3b 0a 20 20 20 20 72 63 20 3d 20  ess");.    rc = 
1d9d9 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d  SQLITE_BUSY;.  }
1d9da 65 6c 73 65 20 69 66 28 20 75 2e 61 72 2e 74 75  else if( u.ar.tu
1d9db 72 6e 4f 6e 41 43 20 26 26 20 21 75 2e 61 72 2e  rnOnAC && !u.ar.
1d9dc 69 52 6f 6c 6c 62 61 63 6b 20 26 26 20 64 62 2d  iRollback && db-
1d9dd 3e 77 72 69 74 65 56 64 62 65 43 6e 74 3e 30 20  >writeVdbeCnt>0 
1d9de 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69  ){.    /* If thi
1d9df 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6d  s instruction im
1d9e0 70 6c 65 6d 65 6e 74 73 20 61 20 43 4f 4d 4d 49  plements a COMMI
1d9e1 54 20 61 6e 64 20 6f 74 68 65 72 20 56 4d 73 20  T and other VMs 
1d9e2 61 72 65 20 77 72 69 74 69 6e 67 0a 20 20 20 20  are writing.    
1d9e3 2a 2a 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  ** return an err
1d9e4 6f 72 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68  or indicating th
1d9e5 61 74 20 74 68 65 20 6f 74 68 65 72 20 56 4d 73  at the other VMs
1d9e6 20 6d 75 73 74 20 63 6f 6d 70 6c 65 74 65 20 66   must complete f
1d9e7 69 72 73 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  irst..    */.   
1d9e8 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
1d9e9 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64  g(&p->zErrMsg, d
1d9ea 62 2c 20 22 63 61 6e 6e 6f 74 20 63 6f 6d 6d 69  b, "cannot commi
1d9eb 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2d 20  t transaction - 
1d9ec 22 0a 20 20 20 20 20 20 20 20 22 53 51 4c 20 73  ".        "SQL s
1d9ed 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 70 72 6f  tatements in pro
1d9ee 67 72 65 73 73 22 29 3b 0a 20 20 20 20 72 63 20  gress");.    rc 
1d9ef 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  = SQLITE_BUSY;. 
1d9f0 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 72 2e   }else if( u.ar.
1d9f1 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69  desiredAutoCommi
1d9f2 74 21 3d 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  t!=db->autoCommi
1d9f3 74 20 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 61  t ){.    if( u.a
1d9f4 72 2e 69 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20  r.iRollback ){. 
1d9f5 20 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61       assert( u.a
1d9f6 72 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d  r.desiredAutoCom
1d9f7 6d 69 74 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20  mit==1 );.      
1d9f8 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41  sqlite3RollbackA
1d9f9 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20 64 62  ll(db);.      db
1d9fa 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31  ->autoCommit = 1
1d9fb 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
1d9fc 28 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62  (rc = sqlite3Vdb
1d9fd 65 43 68 65 63 6b 46 6b 28 70 2c 20 31 29 29 21  eCheckFk(p, 1))!
1d9fe 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1d9ff 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65      goto vdbe_re
1da00 74 75 72 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  turn;.    }else{
1da01 0a 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43  .      db->autoC
1da02 6f 6d 6d 69 74 20 3d 20 28 75 38 29 75 2e 61 72  ommit = (u8)u.ar
1da03 2e 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d  .desiredAutoComm
1da04 69 74 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71  it;.      if( sq
1da05 6c 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29  lite3VdbeHalt(p)
1da06 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20 29 7b  ==SQLITE_BUSY ){
1da07 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 63 20 3d  .        p->pc =
1da08 20 70 63 3b 0a 20 20 20 20 20 20 20 20 64 62 2d   pc;.        db-
1da09 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 28 75  >autoCommit = (u
1da0a 38 29 28 31 2d 75 2e 61 72 2e 64 65 73 69 72 65  8)(1-u.ar.desire
1da0b 64 41 75 74 6f 43 6f 6d 6d 69 74 29 3b 0a 20 20  dAutoCommit);.  
1da0c 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63        p->rc = rc
1da0d 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a   = SQLITE_BUSY;.
1da0e 20 20 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62          goto vdb
1da0f 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20  e_return;.      
1da10 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  }.    }.    asse
1da11 72 74 28 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65  rt( db->nStateme
1da12 6e 74 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c  nt==0 );.    sql
1da13 69 74 65 33 43 6c 6f 73 65 53 61 76 65 70 6f 69  ite3CloseSavepoi
1da14 6e 74 73 28 64 62 29 3b 0a 20 20 20 20 69 66 28  nts(db);.    if(
1da15 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   p->rc==SQLITE_O
1da16 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  K ){.      rc = 
1da17 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20  SQLITE_DONE;.   
1da18 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
1da19 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
1da1a 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20  .    }.    goto 
1da1b 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 7d  vdbe_return;.  }
1da1c 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
1da1d 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a  3SetString(&p->z
1da1e 45 72 72 4d 73 67 2c 20 64 62 2c 0a 20 20 20 20  ErrMsg, db,.    
1da1f 20 20 20 20 28 21 75 2e 61 72 2e 64 65 73 69 72      (!u.ar.desir
1da20 65 64 41 75 74 6f 43 6f 6d 6d 69 74 29 3f 22 63  edAutoCommit)?"c
1da21 61 6e 6e 6f 74 20 73 74 61 72 74 20 61 20 74 72  annot start a tr
1da22 61 6e 73 61 63 74 69 6f 6e 20 77 69 74 68 69 6e  ansaction within
1da23 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 3a   a transaction":
1da24 28 0a 20 20 20 20 20 20 20 20 28 75 2e 61 72 2e  (.        (u.ar.
1da25 69 52 6f 6c 6c 62 61 63 6b 29 3f 22 63 61 6e 6e  iRollback)?"cann
1da26 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 2d 20 6e 6f  ot rollback - no
1da27 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
1da28 61 63 74 69 76 65 22 3a 0a 20 20 20 20 20 20 20  active":.       
1da29 20 20 20 20 20 20 20 20 20 20 20 20 22 63 61 6e              "can
1da2a 6e 6f 74 20 63 6f 6d 6d 69 74 20 2d 20 6e 6f 20  not commit - no 
1da2b 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61  transaction is a
1da2c 63 74 69 76 65 22 29 29 3b 0a 0a 20 20 20 20 72  ctive"));..    r
1da2d 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
1da2e 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1da2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 72 61  ../* Opcode: Tra
1da30 6e 73 61 63 74 69 6f 6e 20 50 31 20 50 32 20 2a  nsaction P1 P2 *
1da31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 65 67 69 6e   * *.**.** Begin
1da32 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20   a transaction. 
1da33 20 54 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   The transaction
1da34 20 65 6e 64 73 20 77 68 65 6e 20 61 20 43 6f 6d   ends when a Com
1da35 6d 69 74 20 6f 72 20 52 6f 6c 6c 62 61 63 6b 0a  mit or Rollback.
1da36 2a 2a 20 6f 70 63 6f 64 65 20 69 73 20 65 6e 63  ** opcode is enc
1da37 6f 75 6e 74 65 72 65 64 2e 20 20 44 65 70 65 6e  ountered.  Depen
1da38 64 69 6e 67 20 6f 6e 20 74 68 65 20 4f 4e 20 43  ding on the ON C
1da39 4f 4e 46 4c 49 43 54 20 73 65 74 74 69 6e 67 2c  ONFLICT setting,
1da3a 20 74 68 65 0a 2a 2a 20 74 72 61 6e 73 61 63 74   the.** transact
1da3b 69 6f 6e 20 6d 69 67 68 74 20 61 6c 73 6f 20 62  ion might also b
1da3c 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69 66  e rolled back if
1da3d 20 61 6e 20 65 72 72 6f 72 20 69 73 20 65 6e 63   an error is enc
1da3e 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  ountered..**.** 
1da3f 50 31 20 69 73 20 74 68 65 20 69 6e 64 65 78 20  P1 is the index 
1da40 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
1da41 66 69 6c 65 20 6f 6e 20 77 68 69 63 68 20 74 68  file on which th
1da42 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  e transaction is
1da43 0a 2a 2a 20 73 74 61 72 74 65 64 2e 20 20 49 6e  .** started.  In
1da44 64 65 78 20 30 20 69 73 20 74 68 65 20 6d 61 69  dex 0 is the mai
1da45 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  n database file 
1da46 61 6e 64 20 69 6e 64 65 78 20 31 20 69 73 20 74  and index 1 is t
1da47 68 65 0a 2a 2a 20 66 69 6c 65 20 75 73 65 64 20  he.** file used 
1da48 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 74 61  for temporary ta
1da49 62 6c 65 73 2e 20 20 49 6e 64 69 63 65 73 20 6f  bles.  Indices o
1da4a 66 20 32 20 6f 72 20 6d 6f 72 65 20 61 72 65 20  f 2 or more are 
1da4b 75 73 65 64 20 66 6f 72 0a 2a 2a 20 61 74 74 61  used for.** atta
1da4c 63 68 65 64 20 64 61 74 61 62 61 73 65 73 2e 0a  ched databases..
1da4d 2a 2a 0a 2a 2a 20 49 66 20 50 32 20 69 73 20 6e  **.** If P2 is n
1da4e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 61 20  on-zero, then a 
1da4f 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69 6f  write-transactio
1da50 6e 20 69 73 20 73 74 61 72 74 65 64 2e 20 20 41  n is started.  A
1da51 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 69   RESERVED lock i
1da52 73 0a 2a 2a 20 6f 62 74 61 69 6e 65 64 20 6f 6e  s.** obtained on
1da53 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1da54 6c 65 20 77 68 65 6e 20 61 20 77 72 69 74 65 2d  le when a write-
1da55 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73  transaction is s
1da56 74 61 72 74 65 64 2e 20 20 4e 6f 0a 2a 2a 20 6f  tarted.  No.** o
1da57 74 68 65 72 20 70 72 6f 63 65 73 73 20 63 61 6e  ther process can
1da58 20 73 74 61 72 74 20 61 6e 6f 74 68 65 72 20 77   start another w
1da59 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
1da5a 20 77 68 69 6c 65 20 74 68 69 73 20 74 72 61 6e   while this tran
1da5b 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 75 6e  saction is.** un
1da5c 64 65 72 77 61 79 2e 20 20 53 74 61 72 74 69 6e  derway.  Startin
1da5d 67 20 61 20 77 72 69 74 65 20 74 72 61 6e 73 61  g a write transa
1da5e 63 74 69 6f 6e 20 61 6c 73 6f 20 63 72 65 61 74  ction also creat
1da5f 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f  es a rollback jo
1da60 75 72 6e 61 6c 2e 20 41 0a 2a 2a 20 77 72 69 74  urnal. A.** writ
1da61 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75  e transaction mu
1da62 73 74 20 62 65 20 73 74 61 72 74 65 64 20 62 65  st be started be
1da63 66 6f 72 65 20 61 6e 79 20 63 68 61 6e 67 65 73  fore any changes
1da64 20 63 61 6e 20 62 65 20 6d 61 64 65 20 74 6f 20   can be made to 
1da65 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e  the.** database.
1da66 20 20 49 66 20 50 32 20 69 73 20 32 20 6f 72 20    If P2 is 2 or 
1da67 67 72 65 61 74 65 72 20 74 68 65 6e 20 61 6e 20  greater then an 
1da68 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 69  EXCLUSIVE lock i
1da69 73 20 61 6c 73 6f 20 6f 62 74 61 69 6e 65 64 0a  s also obtained.
1da6a 2a 2a 20 6f 6e 20 74 68 65 20 66 69 6c 65 2e 0a  ** on the file..
1da6b 2a 2a 0a 2a 2a 20 49 66 20 61 20 77 72 69 74 65  **.** If a write
1da6c 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20  -transaction is 
1da6d 73 74 61 72 74 65 64 20 61 6e 64 20 74 68 65 20  started and the 
1da6e 56 64 62 65 2e 75 73 65 73 53 74 6d 74 4a 6f 75  Vdbe.usesStmtJou
1da6f 72 6e 61 6c 20 66 6c 61 67 20 69 73 0a 2a 2a 20  rnal flag is.** 
1da70 74 72 75 65 20 28 74 68 69 73 20 66 6c 61 67 20  true (this flag 
1da71 69 73 20 73 65 74 20 69 66 20 74 68 65 20 56 64  is set if the Vd
1da72 62 65 20 6d 61 79 20 6d 6f 64 69 66 79 20 6d 6f  be may modify mo
1da73 72 65 20 74 68 61 6e 20 6f 6e 65 20 72 6f 77 20  re than one row 
1da74 61 6e 64 20 6d 61 79 0a 2a 2a 20 74 68 72 6f 77  and may.** throw
1da75 20 61 6e 20 41 42 4f 52 54 20 65 78 63 65 70 74   an ABORT except
1da76 69 6f 6e 29 2c 20 61 20 73 74 61 74 65 6d 65 6e  ion), a statemen
1da77 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 61  t transaction ma
1da78 79 20 61 6c 73 6f 20 62 65 20 6f 70 65 6e 65 64  y also be opened
1da79 2e 0a 2a 2a 20 4d 6f 72 65 20 73 70 65 63 69 66  ..** More specif
1da7a 69 63 61 6c 6c 79 2c 20 61 20 73 74 61 74 65 6d  ically, a statem
1da7b 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
1da7c 69 73 20 6f 70 65 6e 65 64 20 69 66 66 20 74 68  is opened iff th
1da7d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f  e database.** co
1da7e 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 75 72 72  nnection is curr
1da7f 65 6e 74 6c 79 20 6e 6f 74 20 69 6e 20 61 75 74  ently not in aut
1da80 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 20 6f 72  ocommit mode, or
1da81 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6f 74   if there are ot
1da82 68 65 72 0a 2a 2a 20 61 63 74 69 76 65 20 73 74  her.** active st
1da83 61 74 65 6d 65 6e 74 73 2e 20 41 20 73 74 61 74  atements. A stat
1da84 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  ement transactio
1da85 6e 20 61 6c 6c 6f 77 73 20 74 68 65 20 61 66 66  n allows the aff
1da86 65 63 74 73 20 6f 66 20 74 68 69 73 0a 2a 2a 20  ects of this.** 
1da87 56 44 42 45 20 74 6f 20 62 65 20 72 6f 6c 6c 65  VDBE to be rolle
1da88 64 20 62 61 63 6b 20 61 66 74 65 72 20 61 6e 20  d back after an 
1da89 65 72 72 6f 72 20 77 69 74 68 6f 75 74 20 68 61  error without ha
1da8a 76 69 6e 67 20 74 6f 20 72 6f 6c 6c 20 62 61 63  ving to roll bac
1da8b 6b 20 74 68 65 0a 2a 2a 20 65 6e 74 69 72 65 20  k the.** entire 
1da8c 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 66 20  transaction. If 
1da8d 6e 6f 20 65 72 72 6f 72 20 69 73 20 65 6e 63 6f  no error is enco
1da8e 75 6e 74 65 72 65 64 2c 20 74 68 65 20 73 74 61  untered, the sta
1da8f 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
1da90 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 61 75 74 6f 6d  on.** will autom
1da91 61 74 69 63 61 6c 6c 79 20 63 6f 6d 6d 69 74 20  atically commit 
1da92 77 68 65 6e 20 74 68 65 20 56 44 42 45 20 68 61  when the VDBE ha
1da93 6c 74 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32  lts..**.** If P2
1da94 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 6e 20 61   is zero, then a
1da95 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20 6f 62   read-lock is ob
1da96 74 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 61  tained on the da
1da97 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a  tabase file..*/.
1da98 63 61 73 65 20 4f 50 5f 54 72 61 6e 73 61 63 74  case OP_Transact
1da99 69 6f 6e 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a  ion: {.#if 0  /*
1da9a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1da9b 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 73   moved into u.as
1da9c 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 42 74   */.  Btree *pBt
1da9d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1da9e 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1da9f 64 20 69 6e 74 6f 20 75 2e 61 73 20 2a 2f 0a 0a  d into u.as */..
1daa0 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1daa1 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
1daa2 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73  db->nDb );.  ass
1daa3 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61  ert( (p->btreeMa
1daa4 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31  sk & (1<<pOp->p1
1daa5 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e 61 73 2e  ))!=0 );.  u.as.
1daa6 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 70 4f  pBt = db->aDb[pO
1daa7 70 2d 3e 70 31 5d 2e 70 42 74 3b 0a 0a 20 20 69  p->p1].pBt;..  i
1daa8 66 28 20 75 2e 61 73 2e 70 42 74 20 29 7b 0a 20  f( u.as.pBt ){. 
1daa9 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1daaa 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 75  treeBeginTrans(u
1daab 2e 61 73 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 32  .as.pBt, pOp->p2
1daac 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
1daad 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20  QLITE_BUSY ){.  
1daae 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a      p->pc = pc;.
1daaf 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72 63        p->rc = rc
1dab0 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a   = SQLITE_BUSY;.
1dab1 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f        goto vdbe_
1dab2 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
1dab3 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1dab4 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  _OK ){.      got
1dab5 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
1dab6 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  rror;.    }..   
1dab7 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 26 26 20   if( pOp->p2 && 
1dab8 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e  p->usesStmtJourn
1dab9 61 6c 0a 20 20 20 20 20 26 26 20 28 64 62 2d 3e  al.     && (db->
1daba 61 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 7c 7c  autoCommit==0 ||
1dabb 20 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43   db->activeVdbeC
1dabc 6e 74 3e 31 29 0a 20 20 20 20 29 7b 0a 20 20 20  nt>1).    ){.   
1dabd 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
1dabe 65 33 42 74 72 65 65 49 73 49 6e 54 72 61 6e 73  e3BtreeIsInTrans
1dabf 28 75 2e 61 73 2e 70 42 74 29 20 29 3b 0a 20 20  (u.as.pBt) );.  
1dac0 20 20 20 20 69 66 28 20 70 2d 3e 69 53 74 61 74      if( p->iStat
1dac1 65 6d 65 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20  ement==0 ){.    
1dac2 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
1dac3 6e 53 74 61 74 65 6d 65 6e 74 3e 3d 30 20 26 26  nStatement>=0 &&
1dac4 20 64 62 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 3e   db->nSavepoint>
1dac5 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 64 62  =0 );.        db
1dac6 2d 3e 6e 53 74 61 74 65 6d 65 6e 74 2b 2b 3b 0a  ->nStatement++;.
1dac7 20 20 20 20 20 20 20 20 70 2d 3e 69 53 74 61 74          p->iStat
1dac8 65 6d 65 6e 74 20 3d 20 64 62 2d 3e 6e 53 61 76  ement = db->nSav
1dac9 65 70 6f 69 6e 74 20 2b 20 64 62 2d 3e 6e 53 74  epoint + db->nSt
1daca 61 74 65 6d 65 6e 74 3b 0a 20 20 20 20 20 20 7d  atement;.      }
1dacb 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1dacc 74 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d  te3BtreeBeginStm
1dacd 74 28 75 2e 61 73 2e 70 42 74 2c 20 70 2d 3e 69  t(u.as.pBt, p->i
1dace 53 74 61 74 65 6d 65 6e 74 29 3b 0a 0a 20 20 20  Statement);..   
1dacf 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68 65 20     /* Store the 
1dad0 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20 6f 66  current value of
1dad1 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61   the database ha
1dad2 6e 64 6c 65 73 20 64 65 66 65 72 72 65 64 20 63  ndles deferred c
1dad3 6f 6e 73 74 72 61 69 6e 74 0a 20 20 20 20 20 20  onstraint.      
1dad4 2a 2a 20 63 6f 75 6e 74 65 72 2e 20 49 66 20 74  ** counter. If t
1dad5 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61  he statement tra
1dad6 6e 73 61 63 74 69 6f 6e 20 6e 65 65 64 73 20 74  nsaction needs t
1dad7 6f 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  o be rolled back
1dad8 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 76  ,.      ** the v
1dad9 61 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 75  alue of this cou
1dada 6e 74 65 72 20 6e 65 65 64 73 20 74 6f 20 62 65  nter needs to be
1dadb 20 72 65 73 74 6f 72 65 64 20 74 6f 6f 2e 20 20   restored too.  
1dadc 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 6e 53 74 6d  */.      p->nStm
1dadd 74 44 65 66 43 6f 6e 73 20 3d 20 64 62 2d 3e 6e  tDefCons = db->n
1dade 44 65 66 65 72 72 65 64 43 6f 6e 73 3b 0a 20 20  DeferredCons;.  
1dadf 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b    }.  }.  break;
1dae0 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52  .}../* Opcode: R
1dae1 65 61 64 43 6f 6f 6b 69 65 20 50 31 20 50 32 20  eadCookie P1 P2 
1dae2 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61  P3 * *.**.** Rea
1dae3 64 20 63 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20  d cookie number 
1dae4 50 33 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65  P3 from database
1dae5 20 50 31 20 61 6e 64 20 77 72 69 74 65 20 69 74   P1 and write it
1dae6 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50   into register P
1dae7 32 2e 0a 2a 2a 20 50 33 3d 3d 31 20 69 73 20 74  2..** P3==1 is t
1dae8 68 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f  he schema versio
1dae9 6e 2e 20 20 50 33 3d 3d 32 20 69 73 20 74 68 65  n.  P3==2 is the
1daea 20 64 61 74 61 62 61 73 65 20 66 6f 72 6d 61 74   database format
1daeb 2e 0a 2a 2a 20 50 33 3d 3d 33 20 69 73 20 74 68  ..** P3==3 is th
1daec 65 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 70 61  e recommended pa
1daed 67 65 72 20 63 61 63 68 65 20 73 69 7a 65 2c 20  ger cache size, 
1daee 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 50  and so forth.  P
1daef 31 3d 3d 30 20 69 73 0a 2a 2a 20 74 68 65 20 6d  1==0 is.** the m
1daf0 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c  ain database fil
1daf1 65 20 61 6e 64 20 50 31 3d 3d 31 20 69 73 20 74  e and P1==1 is t
1daf2 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1daf3 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a   used to store.*
1daf4 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  * temporary tabl
1daf5 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  es..**.** There 
1daf6 6d 75 73 74 20 62 65 20 61 20 72 65 61 64 2d 6c  must be a read-l
1daf7 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
1daf8 61 73 65 20 28 65 69 74 68 65 72 20 61 20 74 72  ase (either a tr
1daf9 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 6d 75 73  ansaction.** mus
1dafa 74 20 62 65 20 73 74 61 72 74 65 64 20 6f 72 20  t be started or 
1dafb 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e  there must be an
1dafc 20 6f 70 65 6e 20 63 75 72 73 6f 72 29 20 62 65   open cursor) be
1dafd 66 6f 72 65 0a 2a 2a 20 65 78 65 63 75 74 69 6e  fore.** executin
1dafe 67 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  g this instructi
1daff 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52  on..*/.case OP_R
1db00 65 61 64 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20  eadCookie: {    
1db01 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75             /* ou
1db02 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f  t2-prerelease */
1db03 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1db04 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1db05 20 69 6e 74 6f 20 75 2e 61 74 20 2a 2f 0a 20 20   into u.at */.  
1db06 69 6e 74 20 69 4d 65 74 61 3b 0a 20 20 69 6e 74  int iMeta;.  int
1db07 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 43 6f 6f   iDb;.  int iCoo
1db08 6b 69 65 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  kie;.#endif /* l
1db09 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1db0a 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 74 20 2a  oved into u.at *
1db0b 2f 0a 0a 20 20 75 2e 61 74 2e 69 44 62 20 3d 20  /..  u.at.iDb = 
1db0c 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 61 74 2e  pOp->p1;.  u.at.
1db0d 69 43 6f 6f 6b 69 65 20 3d 20 70 4f 70 2d 3e 70  iCookie = pOp->p
1db0e 33 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  3;.  assert( pOp
1db0f 2d 3e 70 33 3c 53 51 4c 49 54 45 5f 4e 5f 42 54  ->p3<SQLITE_N_BT
1db10 52 45 45 5f 4d 45 54 41 20 29 3b 0a 20 20 61 73  REE_META );.  as
1db11 73 65 72 74 28 20 75 2e 61 74 2e 69 44 62 3e 3d  sert( u.at.iDb>=
1db12 30 20 26 26 20 75 2e 61 74 2e 69 44 62 3c 64 62  0 && u.at.iDb<db
1db13 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72  ->nDb );.  asser
1db14 74 28 20 64 62 2d 3e 61 44 62 5b 75 2e 61 74 2e  t( db->aDb[u.at.
1db15 69 44 62 5d 2e 70 42 74 21 3d 30 20 29 3b 0a 20  iDb].pBt!=0 );. 
1db16 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72   assert( (p->btr
1db17 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 75 2e 61  eeMask & (1<<u.a
1db18 74 2e 69 44 62 29 29 21 3d 30 20 29 3b 0a 0a 20  t.iDb))!=0 );.. 
1db19 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
1db1a 4d 65 74 61 28 64 62 2d 3e 61 44 62 5b 75 2e 61  Meta(db->aDb[u.a
1db1b 74 2e 69 44 62 5d 2e 70 42 74 2c 20 75 2e 61 74  t.iDb].pBt, u.at
1db1c 2e 69 43 6f 6f 6b 69 65 2c 20 28 75 33 32 20 2a  .iCookie, (u32 *
1db1d 29 26 75 2e 61 74 2e 69 4d 65 74 61 29 3b 0a 20  )&u.at.iMeta);. 
1db1e 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 61   pOut->u.i = u.a
1db1f 74 2e 69 4d 65 74 61 3b 0a 20 20 4d 65 6d 53 65  t.iMeta;.  MemSe
1db20 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
1db21 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61  MEM_Int);.  brea
1db22 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1db23 20 53 65 74 43 6f 6f 6b 69 65 20 50 31 20 50 32   SetCookie P1 P2
1db24 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72   P3 * *.**.** Wr
1db25 69 74 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ite the content 
1db26 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20 28  of register P3 (
1db27 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 61  interpreted as a
1db28 6e 20 69 6e 74 65 67 65 72 29 0a 2a 2a 20 69 6e  n integer).** in
1db29 74 6f 20 63 6f 6f 6b 69 65 20 6e 75 6d 62 65 72  to cookie number
1db2a 20 50 32 20 6f 66 20 64 61 74 61 62 61 73 65 20   P2 of database 
1db2b 50 31 2e 20 20 50 32 3d 3d 31 20 69 73 20 74 68  P1.  P2==1 is th
1db2c 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e  e schema version
1db2d 2e 20 20 0a 2a 2a 20 50 32 3d 3d 32 20 69 73 20  .  .** P2==2 is 
1db2e 74 68 65 20 64 61 74 61 62 61 73 65 20 66 6f 72  the database for
1db2f 6d 61 74 2e 20 50 32 3d 3d 33 20 69 73 20 74 68  mat. P2==3 is th
1db30 65 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 70 61  e recommended pa
1db31 67 65 72 20 63 61 63 68 65 20 0a 2a 2a 20 73 69  ger cache .** si
1db32 7a 65 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68  ze, and so forth
1db33 2e 20 20 50 31 3d 3d 30 20 69 73 20 74 68 65 20  .  P1==0 is the 
1db34 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
1db35 6c 65 20 61 6e 64 20 50 31 3d 3d 31 20 69 73 20  le and P1==1 is 
1db36 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65  the .** database
1db37 20 66 69 6c 65 20 75 73 65 64 20 74 6f 20 73 74   file used to st
1db38 6f 72 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61  ore temporary ta
1db39 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 72  bles..**.** A tr
1db3a 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62  ansaction must b
1db3b 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 65  e started before
1db3c 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20   executing this 
1db3d 6f 70 63 6f 64 65 2e 0a 2a 2f 0a 63 61 73 65 20  opcode..*/.case 
1db3e 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 3a 20 7b 20  OP_SetCookie: { 
1db3f 20 20 20 20 20 20 2f 2a 20 69 6e 33 20 2a 2f 0a        /* in3 */.
1db40 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20  #if 0  /* local 
1db41 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1db42 69 6e 74 6f 20 75 2e 61 75 20 2a 2f 0a 20 20 44  into u.au */.  D
1db43 62 20 2a 70 44 62 3b 0a 23 65 6e 64 69 66 20 2f  b *pDb;.#endif /
1db44 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1db45 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61  s moved into u.a
1db46 75 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  u */.  assert( p
1db47 4f 70 2d 3e 70 32 3c 53 51 4c 49 54 45 5f 4e 5f  Op->p2<SQLITE_N_
1db48 42 54 52 45 45 5f 4d 45 54 41 20 29 3b 0a 20 20  BTREE_META );.  
1db49 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e  assert( pOp->p1>
1db4a 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62  =0 && pOp->p1<db
1db4b 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72  ->nDb );.  asser
1db4c 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b  t( (p->btreeMask
1db4d 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29   & (1<<pOp->p1))
1db4e 21 3d 30 20 29 3b 0a 20 20 75 2e 61 75 2e 70 44  !=0 );.  u.au.pD
1db4f 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 70 4f 70  b = &db->aDb[pOp
1db50 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1db51 20 75 2e 61 75 2e 70 44 62 2d 3e 70 42 74 21 3d   u.au.pDb->pBt!=
1db52 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  0 );.  sqlite3Vd
1db53 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28  beMemIntegerify(
1db54 70 49 6e 33 29 3b 0a 20 20 2f 2a 20 53 65 65 20  pIn3);.  /* See 
1db55 6e 6f 74 65 20 61 62 6f 75 74 20 69 6e 64 65 78  note about index
1db56 20 73 68 69 66 74 69 6e 67 20 6f 6e 20 4f 50 5f   shifting on OP_
1db57 52 65 61 64 43 6f 6f 6b 69 65 20 2a 2f 0a 20 20  ReadCookie */.  
1db58 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1db59 65 55 70 64 61 74 65 4d 65 74 61 28 75 2e 61 75  eUpdateMeta(u.au
1db5a 2e 70 44 62 2d 3e 70 42 74 2c 20 70 4f 70 2d 3e  .pDb->pBt, pOp->
1db5b 70 32 2c 20 28 69 6e 74 29 70 49 6e 33 2d 3e 75  p2, (int)pIn3->u
1db5c 2e 69 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e  .i);.  if( pOp->
1db5d 70 32 3d 3d 42 54 52 45 45 5f 53 43 48 45 4d 41  p2==BTREE_SCHEMA
1db5e 5f 56 45 52 53 49 4f 4e 20 29 7b 0a 20 20 20 20  _VERSION ){.    
1db5f 2f 2a 20 57 68 65 6e 20 74 68 65 20 73 63 68 65  /* When the sche
1db60 6d 61 20 63 6f 6f 6b 69 65 20 63 68 61 6e 67 65  ma cookie change
1db61 73 2c 20 72 65 63 6f 72 64 20 74 68 65 20 6e 65  s, record the ne
1db62 77 20 63 6f 6f 6b 69 65 20 69 6e 74 65 72 6e 61  w cookie interna
1db63 6c 6c 79 20 2a 2f 0a 20 20 20 20 75 2e 61 75 2e  lly */.    u.au.
1db64 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63  pDb->pSchema->sc
1db65 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 3d 20 28 69  hema_cookie = (i
1db66 6e 74 29 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20  nt)pIn3->u.i;.  
1db67 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53    db->flags |= S
1db68 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e  QLITE_InternChan
1db69 67 65 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ges;.  }else if(
1db6a 20 70 4f 70 2d 3e 70 32 3d 3d 42 54 52 45 45 5f   pOp->p2==BTREE_
1db6b 46 49 4c 45 5f 46 4f 52 4d 41 54 20 29 7b 0a 20  FILE_FORMAT ){. 
1db6c 20 20 20 2f 2a 20 52 65 63 6f 72 64 20 63 68 61     /* Record cha
1db6d 6e 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65  nges in the file
1db6e 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 20 20 75   format */.    u
1db6f 2e 61 75 2e 70 44 62 2d 3e 70 53 63 68 65 6d 61  .au.pDb->pSchema
1db70 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20  ->file_format = 
1db71 28 75 38 29 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20  (u8)pIn3->u.i;. 
1db72 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31   }.  if( pOp->p1
1db73 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e  ==1 ){.    /* In
1db74 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 70 72 65  validate all pre
1db75 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73  pared statements
1db76 20 77 68 65 6e 65 76 65 72 20 74 68 65 20 54 45   whenever the TE
1db77 4d 50 20 64 61 74 61 62 61 73 65 0a 20 20 20 20  MP database.    
1db78 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 63 68 61  ** schema is cha
1db79 6e 67 65 64 2e 20 20 54 69 63 6b 65 74 20 23 31  nged.  Ticket #1
1db7a 36 34 34 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  644 */.    sqlit
1db7b 65 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64  e3ExpirePrepared
1db7c 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a  Statements(db);.
1db7d 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d      p->expired =
1db7e 20 30 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   0;.  }.  break;
1db7f 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56  .}../* Opcode: V
1db80 65 72 69 66 79 43 6f 6f 6b 69 65 20 50 31 20 50  erifyCookie P1 P
1db81 32 20 2a 0a 2a 2a 0a 2a 2a 20 43 68 65 63 6b 20  2 *.**.** Check 
1db82 74 68 65 20 76 61 6c 75 65 20 6f 66 20 67 6c 6f  the value of glo
1db83 62 61 6c 20 64 61 74 61 62 61 73 65 20 70 61 72  bal database par
1db84 61 6d 65 74 65 72 20 6e 75 6d 62 65 72 20 30 20  ameter number 0 
1db85 28 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 76  (the.** schema v
1db86 65 72 73 69 6f 6e 29 20 61 6e 64 20 6d 61 6b 65  ersion) and make
1db87 20 73 75 72 65 20 69 74 20 69 73 20 65 71 75 61   sure it is equa
1db88 6c 20 74 6f 20 50 32 2e 20 20 0a 2a 2a 20 50 31  l to P2.  .** P1
1db89 20 69 73 20 74 68 65 20 64 61 74 61 62 61 73 65   is the database
1db8a 20 6e 75 6d 62 65 72 20 77 68 69 63 68 20 69 73   number which is
1db8b 20 30 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20   0 for the main 
1db8c 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a  database file.**
1db8d 20 61 6e 64 20 31 20 66 6f 72 20 74 68 65 20 66   and 1 for the f
1db8e 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74 65 6d 70  ile holding temp
1db8f 6f 72 61 72 79 20 74 61 62 6c 65 73 20 61 6e 64  orary tables and
1db90 20 73 6f 6d 65 20 68 69 67 68 65 72 20 6e 75 6d   some higher num
1db91 62 65 72 0a 2a 2a 20 66 6f 72 20 61 75 78 69 6c  ber.** for auxil
1db92 69 61 72 79 20 64 61 74 61 62 61 73 65 73 2e 0a  iary databases..
1db93 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6f 6b 69 65  **.** The cookie
1db94 20 63 68 61 6e 67 65 73 20 69 74 73 20 76 61 6c   changes its val
1db95 75 65 20 77 68 65 6e 65 76 65 72 20 74 68 65 20  ue whenever the 
1db96 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20  database schema 
1db97 63 68 61 6e 67 65 73 2e 0a 2a 2a 20 54 68 69 73  changes..** This
1db98 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 75 73   operation is us
1db99 65 64 20 74 6f 20 64 65 74 65 63 74 20 77 68 65  ed to detect whe
1db9a 6e 20 74 68 61 74 20 74 68 65 20 63 6f 6f 6b 69  n that the cooki
1db9b 65 20 68 61 73 20 63 68 61 6e 67 65 64 0a 2a 2a  e has changed.**
1db9c 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 63 75   and that the cu
1db9d 72 72 65 6e 74 20 70 72 6f 63 65 73 73 20 6e 65  rrent process ne
1db9e 65 64 73 20 74 6f 20 72 65 72 65 61 64 20 74 68  eds to reread th
1db9f 65 20 73 63 68 65 6d 61 2e 0a 2a 2a 0a 2a 2a 20  e schema..**.** 
1dba0 45 69 74 68 65 72 20 61 20 74 72 61 6e 73 61 63  Either a transac
1dba1 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f 20 68 61  tion needs to ha
1dba2 76 65 20 62 65 65 6e 20 73 74 61 72 74 65 64 20  ve been started 
1dba3 6f 72 20 61 6e 20 4f 50 5f 4f 70 65 6e 20 6e 65  or an OP_Open ne
1dba4 65 64 73 0a 2a 2a 20 74 6f 20 62 65 20 65 78 65  eds.** to be exe
1dba5 63 75 74 65 64 20 28 74 6f 20 65 73 74 61 62 6c  cuted (to establ
1dba6 69 73 68 20 61 20 72 65 61 64 20 6c 6f 63 6b 29  ish a read lock)
1dba7 20 62 65 66 6f 72 65 20 74 68 69 73 20 6f 70 63   before this opc
1dba8 6f 64 65 20 69 73 0a 2a 2a 20 69 6e 76 6f 6b 65  ode is.** invoke
1dba9 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 65  d..*/.case OP_Ve
1dbaa 72 69 66 79 43 6f 6f 6b 69 65 3a 20 7b 0a 23 69  rifyCookie: {.#i
1dbab 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1dbac 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1dbad 74 6f 20 75 2e 61 76 20 2a 2f 0a 20 20 69 6e 74  to u.av */.  int
1dbae 20 69 4d 65 74 61 3b 0a 20 20 42 74 72 65 65 20   iMeta;.  Btree 
1dbaf 2a 70 42 74 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  *pBt;.#endif /* 
1dbb0 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1dbb1 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 61 76 20  moved into u.av 
1dbb2 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  */.  assert( pOp
1dbb3 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
1dbb4 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  p1<db->nDb );.  
1dbb5 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65  assert( (p->btre
1dbb6 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d  eMask & (1<<pOp-
1dbb7 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 75 2e  >p1))!=0 );.  u.
1dbb8 61 76 2e 70 42 74 20 3d 20 64 62 2d 3e 61 44 62  av.pBt = db->aDb
1dbb9 5b 70 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b 0a 20  [pOp->p1].pBt;. 
1dbba 20 69 66 28 20 75 2e 61 76 2e 70 42 74 20 29 7b   if( u.av.pBt ){
1dbbb 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1dbbc 65 47 65 74 4d 65 74 61 28 75 2e 61 76 2e 70 42  eGetMeta(u.av.pB
1dbbd 74 2c 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f  t, BTREE_SCHEMA_
1dbbe 56 45 52 53 49 4f 4e 2c 20 28 75 33 32 20 2a 29  VERSION, (u32 *)
1dbbf 26 75 2e 61 76 2e 69 4d 65 74 61 29 3b 0a 20 20  &u.av.iMeta);.  
1dbc0 7d 65 6c 73 65 7b 0a 20 20 20 20 75 2e 61 76 2e  }else{.    u.av.
1dbc1 69 4d 65 74 61 20 3d 20 30 3b 0a 20 20 7d 0a 20  iMeta = 0;.  }. 
1dbc2 20 69 66 28 20 75 2e 61 76 2e 69 4d 65 74 61 21   if( u.av.iMeta!
1dbc3 3d 70 4f 70 2d 3e 70 32 20 29 7b 0a 20 20 20 20  =pOp->p2 ){.    
1dbc4 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1dbc5 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20  , p->zErrMsg);. 
1dbc6 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20     p->zErrMsg = 
1dbc7 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
1dbc8 64 62 2c 20 22 64 61 74 61 62 61 73 65 20 73 63  db, "database sc
1dbc9 68 65 6d 61 20 68 61 73 20 63 68 61 6e 67 65 64  hema has changed
1dbca 22 29 3b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  ");.    /* If th
1dbcb 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 20  e schema-cookie 
1dbcc 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
1dbcd 65 20 66 69 6c 65 20 6d 61 74 63 68 65 73 20 74  e file matches t
1dbce 68 65 20 63 6f 6f 6b 69 65 0a 20 20 20 20 2a 2a  he cookie.    **
1dbcf 20 73 74 6f 72 65 64 20 77 69 74 68 20 74 68 65   stored with the
1dbd0 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72 65   in-memory repre
1dbd1 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  sentation of the
1dbd2 20 73 63 68 65 6d 61 2c 20 64 6f 0a 20 20 20 20   schema, do.    
1dbd3 2a 2a 20 6e 6f 74 20 72 65 6c 6f 61 64 20 74 68  ** not reload th
1dbd4 65 20 73 63 68 65 6d 61 20 66 72 6f 6d 20 74 68  e schema from th
1dbd5 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
1dbd6 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49  .    **.    ** I
1dbd7 66 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 73  f virtual-tables
1dbd8 20 61 72 65 20 69 6e 20 75 73 65 2c 20 74 68 69   are in use, thi
1dbd9 73 20 69 73 20 6e 6f 74 20 6a 75 73 74 20 61 6e  s is not just an
1dbda 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 0a 20   optimization.. 
1dbdb 20 20 20 2a 2a 20 4f 66 74 65 6e 2c 20 76 2d 74     ** Often, v-t
1dbdc 61 62 6c 65 73 20 73 74 6f 72 65 20 74 68 65 69  ables store thei
1dbdd 72 20 64 61 74 61 20 69 6e 20 6f 74 68 65 72 20  r data in other 
1dbde 53 51 4c 69 74 65 20 74 61 62 6c 65 73 2c 20 77  SQLite tables, w
1dbdf 68 69 63 68 0a 20 20 20 20 2a 2a 20 61 72 65 20  hich.    ** are 
1dbe0 71 75 65 72 69 65 64 20 66 72 6f 6d 20 77 69 74  queried from wit
1dbe1 68 69 6e 20 78 4e 65 78 74 28 29 20 61 6e 64 20  hin xNext() and 
1dbe2 6f 74 68 65 72 20 76 2d 74 61 62 6c 65 20 6d 65  other v-table me
1dbe3 74 68 6f 64 73 20 75 73 69 6e 67 0a 20 20 20 20  thods using.    
1dbe4 2a 2a 20 70 72 65 70 61 72 65 64 20 71 75 65 72  ** prepared quer
1dbe5 69 65 73 2e 20 49 66 20 73 75 63 68 20 61 20 71  ies. If such a q
1dbe6 75 65 72 79 20 69 73 20 6f 75 74 2d 6f 66 2d 64  uery is out-of-d
1dbe7 61 74 65 2c 20 77 65 20 64 6f 20 6e 6f 74 20 77  ate, we do not w
1dbe8 61 6e 74 20 74 6f 0a 20 20 20 20 2a 2a 20 64 69  ant to.    ** di
1dbe9 73 63 61 72 64 20 74 68 65 20 64 61 74 61 62 61  scard the databa
1dbea 73 65 20 73 63 68 65 6d 61 2c 20 61 73 20 74 68  se schema, as th
1dbeb 65 20 75 73 65 72 20 63 6f 64 65 20 69 6d 70 6c  e user code impl
1dbec 65 6d 65 6e 74 69 6e 67 20 74 68 65 0a 20 20 20  ementing the.   
1dbed 20 2a 2a 20 76 2d 74 61 62 6c 65 20 77 6f 75 6c   ** v-table woul
1dbee 64 20 68 61 76 65 20 74 6f 20 62 65 20 72 65 61  d have to be rea
1dbef 64 79 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74  dy for the sqlit
1dbf0 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72  e3_vtab structur
1dbf1 65 20 69 74 73 65 6c 66 0a 20 20 20 20 2a 2a 20  e itself.    ** 
1dbf2 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65  to be invalidate
1dbf3 64 20 77 68 65 6e 65 76 65 72 20 73 71 6c 69 74  d whenever sqlit
1dbf4 65 33 5f 73 74 65 70 28 29 20 69 73 20 63 61 6c  e3_step() is cal
1dbf5 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 0a  led from within.
1dbf6 20 20 20 20 2a 2a 20 61 20 76 2d 74 61 62 6c 65      ** a v-table
1dbf7 20 6d 65 74 68 6f 64 2e 0a 20 20 20 20 2a 2f 0a   method..    */.
1dbf8 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b      if( db->aDb[
1dbf9 70 4f 70 2d 3e 70 31 5d 2e 70 53 63 68 65 6d 61  pOp->p1].pSchema
1dbfa 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 21  ->schema_cookie!
1dbfb 3d 75 2e 61 76 2e 69 4d 65 74 61 20 29 7b 0a 20  =u.av.iMeta ){. 
1dbfc 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 65       sqlite3Rese
1dbfd 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28  tInternalSchema(
1dbfe 64 62 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20 20  db, pOp->p1);.  
1dbff 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33    }..    sqlite3
1dc00 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74  ExpirePreparedSt
1dc01 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20  atements(db);.  
1dc02 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 53 43    rc = SQLITE_SC
1dc03 48 45 4d 41 3b 0a 20 20 7d 0a 20 20 62 72 65 61  HEMA;.  }.  brea
1dc04 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1dc05 20 4f 70 65 6e 52 65 61 64 20 50 31 20 50 32 20   OpenRead P1 P2 
1dc06 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f  P3 P4 P5.**.** O
1dc07 70 65 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20  pen a read-only 
1dc08 63 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 64  cursor for the d
1dc09 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 77 68  atabase table wh
1dc0a 6f 73 65 20 72 6f 6f 74 20 70 61 67 65 20 69 73  ose root page is
1dc0b 0a 2a 2a 20 50 32 20 69 6e 20 61 20 64 61 74 61  .** P2 in a data
1dc0c 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20  base file.  The 
1dc0d 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 73  database file is
1dc0e 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 50   determined by P
1dc0f 33 2e 20 0a 2a 2a 20 50 33 3d 3d 30 20 6d 65 61  3. .** P3==0 mea
1dc10 6e 73 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  ns the main data
1dc11 62 61 73 65 2c 20 50 33 3d 3d 31 20 6d 65 61 6e  base, P3==1 mean
1dc12 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 75  s the database u
1dc13 73 65 64 20 66 6f 72 20 0a 2a 2a 20 74 65 6d 70  sed for .** temp
1dc14 6f 72 61 72 79 20 74 61 62 6c 65 73 2c 20 61 6e  orary tables, an
1dc15 64 20 50 33 3e 31 20 6d 65 61 6e 73 20 75 73 65  d P3>1 means use
1dc16 64 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  d the correspond
1dc17 69 6e 67 20 61 74 74 61 63 68 65 64 0a 2a 2a 20  ing attached.** 
1dc18 64 61 74 61 62 61 73 65 2e 20 20 47 69 76 65 20  database.  Give 
1dc19 74 68 65 20 6e 65 77 20 63 75 72 73 6f 72 20 61  the new cursor a
1dc1a 6e 20 69 64 65 6e 74 69 66 69 65 72 20 6f 66 20  n identifier of 
1dc1b 50 31 2e 20 20 54 68 65 20 50 31 0a 2a 2a 20 76  P1.  The P1.** v
1dc1c 61 6c 75 65 73 20 6e 65 65 64 20 6e 6f 74 20 62  alues need not b
1dc1d 65 20 63 6f 6e 74 69 67 75 6f 75 73 20 62 75 74  e contiguous but
1dc1e 20 61 6c 6c 20 50 31 20 76 61 6c 75 65 73 20 73   all P1 values s
1dc1f 68 6f 75 6c 64 20 62 65 20 73 6d 61 6c 6c 20 69  hould be small i
1dc20 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 49 74 20 69  ntegers..** It i
1dc21 73 20 61 6e 20 65 72 72 6f 72 20 66 6f 72 20 50  s an error for P
1dc22 31 20 74 6f 20 62 65 20 6e 65 67 61 74 69 76 65  1 to be negative
1dc23 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 21 3d 30  ..**.** If P5!=0
1dc24 20 74 68 65 6e 20 75 73 65 20 74 68 65 20 63 6f   then use the co
1dc25 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74 65  ntent of registe
1dc26 72 20 50 32 20 61 73 20 74 68 65 20 72 6f 6f 74  r P2 as the root
1dc27 20 70 61 67 65 2c 20 6e 6f 74 0a 2a 2a 20 74 68   page, not.** th
1dc28 65 20 76 61 6c 75 65 20 6f 66 20 50 32 20 69 74  e value of P2 it
1dc29 73 65 6c 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  self..**.** Ther
1dc2a 65 20 77 69 6c 6c 20 62 65 20 61 20 72 65 61 64  e will be a read
1dc2b 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
1dc2c 61 62 61 73 65 20 77 68 65 6e 65 76 65 72 20 74  abase whenever t
1dc2d 68 65 72 65 20 69 73 20 61 6e 0a 2a 2a 20 6f 70  here is an.** op
1dc2e 65 6e 20 63 75 72 73 6f 72 2e 20 20 49 66 20 74  en cursor.  If t
1dc2f 68 65 20 64 61 74 61 62 61 73 65 20 77 61 73 20  he database was 
1dc30 75 6e 6c 6f 63 6b 65 64 20 70 72 69 6f 72 20 74  unlocked prior t
1dc31 6f 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69  o this instructi
1dc32 6f 6e 0a 2a 2a 20 74 68 65 6e 20 61 20 72 65 61  on.** then a rea
1dc33 64 20 6c 6f 63 6b 20 69 73 20 61 63 71 75 69 72  d lock is acquir
1dc34 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68  ed as part of th
1dc35 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20  is instruction. 
1dc36 20 41 20 72 65 61 64 0a 2a 2a 20 6c 6f 63 6b 20   A read.** lock 
1dc37 61 6c 6c 6f 77 73 20 6f 74 68 65 72 20 70 72 6f  allows other pro
1dc38 63 65 73 73 65 73 20 74 6f 20 72 65 61 64 20 74  cesses to read t
1dc39 68 65 20 64 61 74 61 62 61 73 65 20 62 75 74 20  he database but 
1dc3a 70 72 6f 68 69 62 69 74 73 0a 2a 2a 20 61 6e 79  prohibits.** any
1dc3b 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 20 66   other process f
1dc3c 72 6f 6d 20 6d 6f 64 69 66 79 69 6e 67 20 74 68  rom modifying th
1dc3d 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65  e database.  The
1dc3e 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 0a 2a 2a   read lock is.**
1dc3f 20 72 65 6c 65 61 73 65 64 20 77 68 65 6e 20 61   released when a
1dc40 6c 6c 20 63 75 72 73 6f 72 73 20 61 72 65 20 63  ll cursors are c
1dc41 6c 6f 73 65 64 2e 20 20 49 66 20 74 68 69 73 20  losed.  If this 
1dc42 69 6e 73 74 72 75 63 74 69 6f 6e 20 61 74 74 65  instruction atte
1dc43 6d 70 74 73 0a 2a 2a 20 74 6f 20 67 65 74 20 61  mpts.** to get a
1dc44 20 72 65 61 64 20 6c 6f 63 6b 20 62 75 74 20 66   read lock but f
1dc45 61 69 6c 73 2c 20 74 68 65 20 73 63 72 69 70 74  ails, the script
1dc46 20 74 65 72 6d 69 6e 61 74 65 73 20 77 69 74 68   terminates with
1dc47 20 61 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55   an.** SQLITE_BU
1dc48 53 59 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a  SY error code..*
1dc49 2a 0a 2a 2a 20 54 68 65 20 50 34 20 76 61 6c 75  *.** The P4 valu
1dc4a 65 20 6d 61 79 20 62 65 20 65 69 74 68 65 72 20  e may be either 
1dc4b 61 6e 20 69 6e 74 65 67 65 72 20 28 50 34 5f 49  an integer (P4_I
1dc4c 4e 54 33 32 29 20 6f 72 20 61 20 70 6f 69 6e 74  NT32) or a point
1dc4d 65 72 20 74 6f 0a 2a 2a 20 61 20 4b 65 79 49 6e  er to.** a KeyIn
1dc4e 66 6f 20 73 74 72 75 63 74 75 72 65 20 28 50 34  fo structure (P4
1dc4f 5f 4b 45 59 49 4e 46 4f 29 2e 20 49 66 20 69 74  _KEYINFO). If it
1dc50 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
1dc51 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a 2a 20 73   a KeyInfo .** s
1dc52 74 72 75 63 74 75 72 65 2c 20 74 68 65 6e 20 73  tructure, then s
1dc53 61 69 64 20 73 74 72 75 63 74 75 72 65 20 64 65  aid structure de
1dc54 66 69 6e 65 73 20 74 68 65 20 63 6f 6e 74 65 6e  fines the conten
1dc55 74 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6e 67 20  t and collating 
1dc56 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20 6f 66 20  .** sequence of 
1dc57 74 68 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20  the index being 
1dc58 6f 70 65 6e 65 64 2e 20 4f 74 68 65 72 77 69 73  opened. Otherwis
1dc59 65 2c 20 69 66 20 50 34 20 69 73 20 61 6e 20 69  e, if P4 is an i
1dc5a 6e 74 65 67 65 72 20 0a 2a 2a 20 76 61 6c 75 65  nteger .** value
1dc5b 2c 20 69 74 20 69 73 20 73 65 74 20 74 6f 20 74  , it is set to t
1dc5c 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
1dc5d 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c  umns in the tabl
1dc5e 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  e..**.** See als
1dc5f 6f 20 4f 70 65 6e 57 72 69 74 65 2e 0a 2a 2f 0a  o OpenWrite..*/.
1dc60 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f 70 65 6e 57  /* Opcode: OpenW
1dc61 72 69 74 65 20 50 31 20 50 32 20 50 33 20 50 34  rite P1 P2 P3 P4
1dc62 20 50 35 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61   P5.**.** Open a
1dc63 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73   read/write curs
1dc64 6f 72 20 6e 61 6d 65 64 20 50 31 20 6f 6e 20 74  or named P1 on t
1dc65 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65  he table or inde
1dc66 78 20 77 68 6f 73 65 20 72 6f 6f 74 0a 2a 2a 20  x whose root.** 
1dc67 70 61 67 65 20 69 73 20 50 32 2e 20 20 4f 72 20  page is P2.  Or 
1dc68 69 66 20 50 35 21 3d 30 20 75 73 65 20 74 68 65  if P5!=0 use the
1dc69 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69   content of regi
1dc6a 73 74 65 72 20 50 32 20 74 6f 20 66 69 6e 64 20  ster P2 to find 
1dc6b 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65  the.** root page
1dc6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 76  ..**.** The P4 v
1dc6d 61 6c 75 65 20 6d 61 79 20 62 65 20 65 69 74 68  alue may be eith
1dc6e 65 72 20 61 6e 20 69 6e 74 65 67 65 72 20 28 50  er an integer (P
1dc6f 34 5f 49 4e 54 33 32 29 20 6f 72 20 61 20 70 6f  4_INT32) or a po
1dc70 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61 20 4b 65  inter to.** a Ke
1dc71 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  yInfo structure 
1dc72 28 50 34 5f 4b 45 59 49 4e 46 4f 29 2e 20 49 66  (P4_KEYINFO). If
1dc73 20 69 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72   it is a pointer
1dc74 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 0a 2a   to a KeyInfo .*
1dc75 2a 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65  * structure, the
1dc76 6e 20 73 61 69 64 20 73 74 72 75 63 74 75 72 65  n said structure
1dc77 20 64 65 66 69 6e 65 73 20 74 68 65 20 63 6f 6e   defines the con
1dc78 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c 61 74 69  tent and collati
1dc79 6e 67 20 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20  ng .** sequence 
1dc7a 6f 66 20 74 68 65 20 69 6e 64 65 78 20 62 65 69  of the index bei
1dc7b 6e 67 20 6f 70 65 6e 65 64 2e 20 4f 74 68 65 72  ng opened. Other
1dc7c 77 69 73 65 2c 20 69 66 20 50 34 20 69 73 20 61  wise, if P4 is a
1dc7d 6e 20 69 6e 74 65 67 65 72 20 0a 2a 2a 20 76 61  n integer .** va
1dc7e 6c 75 65 2c 20 69 74 20 69 73 20 73 65 74 20 74  lue, it is set t
1dc7f 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  o the number of 
1dc80 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 74  columns in the t
1dc81 61 62 6c 65 2c 20 6f 72 20 74 6f 20 74 68 65 0a  able, or to the.
1dc82 2a 2a 20 6c 61 72 67 65 73 74 20 69 6e 64 65 78  ** largest index
1dc83 20 6f 66 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6f   of any column o
1dc84 66 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74  f the table that
1dc85 20 69 73 20 61 63 74 75 61 6c 6c 79 20 75 73 65   is actually use
1dc86 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  d..**.** This in
1dc87 73 74 72 75 63 74 69 6f 6e 20 77 6f 72 6b 73 20  struction works 
1dc88 6a 75 73 74 20 6c 69 6b 65 20 4f 70 65 6e 52 65  just like OpenRe
1dc89 61 64 20 65 78 63 65 70 74 20 74 68 61 74 20 69  ad except that i
1dc8a 74 20 6f 70 65 6e 73 20 74 68 65 20 63 75 72 73  t opens the curs
1dc8b 6f 72 0a 2a 2a 20 69 6e 20 72 65 61 64 2f 77 72  or.** in read/wr
1dc8c 69 74 65 20 6d 6f 64 65 2e 20 20 46 6f 72 20 61  ite mode.  For a
1dc8d 20 67 69 76 65 6e 20 74 61 62 6c 65 2c 20 74 68   given table, th
1dc8e 65 72 65 20 63 61 6e 20 62 65 20 6f 6e 65 20 6f  ere can be one o
1dc8f 72 20 6d 6f 72 65 20 72 65 61 64 2d 6f 6e 6c 79  r more read-only
1dc90 0a 2a 2a 20 63 75 72 73 6f 72 73 20 6f 72 20 61  .** cursors or a
1dc91 20 73 69 6e 67 6c 65 20 72 65 61 64 2f 77 72 69   single read/wri
1dc92 74 65 20 63 75 72 73 6f 72 20 62 75 74 20 6e 6f  te cursor but no
1dc93 74 20 62 6f 74 68 2e 0a 2a 2a 0a 2a 2a 20 53 65  t both..**.** Se
1dc94 65 20 61 6c 73 6f 20 4f 70 65 6e 52 65 61 64 2e  e also OpenRead.
1dc95 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70 65 6e  .*/.case OP_Open
1dc96 52 65 61 64 3a 0a 63 61 73 65 20 4f 50 5f 4f 70  Read:.case OP_Op
1dc97 65 6e 57 72 69 74 65 3a 20 7b 0a 23 69 66 20 30  enWrite: {.#if 0
1dc98 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1dc99 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1dc9a 75 2e 61 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 46  u.aw */.  int nF
1dc9b 69 65 6c 64 3b 0a 20 20 4b 65 79 49 6e 66 6f 20  ield;.  KeyInfo 
1dc9c 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 69 6e 74  *pKeyInfo;.  int
1dc9d 20 70 32 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a   p2;.  int iDb;.
1dc9e 20 20 69 6e 74 20 77 72 46 6c 61 67 3b 0a 20 20    int wrFlag;.  
1dc9f 42 74 72 65 65 20 2a 70 58 3b 0a 20 20 56 64 62  Btree *pX;.  Vdb
1dca0 65 43 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20  eCursor *pCur;. 
1dca1 20 44 62 20 2a 70 44 62 3b 0a 23 65 6e 64 69 66   Db *pDb;.#endif
1dca2 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1dca3 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1dca4 2e 61 77 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d  .aw */..  if( p-
1dca5 3e 65 78 70 69 72 65 64 20 29 7b 0a 20 20 20 20  >expired ){.    
1dca6 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52  rc = SQLITE_ABOR
1dca7 54 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  T;.    break;.  
1dca8 7d 0a 0a 20 20 75 2e 61 77 2e 6e 46 69 65 6c 64  }..  u.aw.nField
1dca9 20 3d 20 30 3b 0a 20 20 75 2e 61 77 2e 70 4b 65   = 0;.  u.aw.pKe
1dcaa 79 49 6e 66 6f 20 3d 20 30 3b 0a 20 20 75 2e 61  yInfo = 0;.  u.a
1dcab 77 2e 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a  w.p2 = pOp->p2;.
1dcac 20 20 75 2e 61 77 2e 69 44 62 20 3d 20 70 4f 70    u.aw.iDb = pOp
1dcad 2d 3e 70 33 3b 0a 20 20 61 73 73 65 72 74 28 20  ->p3;.  assert( 
1dcae 75 2e 61 77 2e 69 44 62 3e 3d 30 20 26 26 20 75  u.aw.iDb>=0 && u
1dcaf 2e 61 77 2e 69 44 62 3c 64 62 2d 3e 6e 44 62 20  .aw.iDb<db->nDb 
1dcb0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d  );.  assert( (p-
1dcb1 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c  >btreeMask & (1<
1dcb2 3c 75 2e 61 77 2e 69 44 62 29 29 21 3d 30 20 29  <u.aw.iDb))!=0 )
1dcb3 3b 0a 20 20 75 2e 61 77 2e 70 44 62 20 3d 20 26  ;.  u.aw.pDb = &
1dcb4 64 62 2d 3e 61 44 62 5b 75 2e 61 77 2e 69 44 62  db->aDb[u.aw.iDb
1dcb5 5d 3b 0a 20 20 75 2e 61 77 2e 70 58 20 3d 20 75  ];.  u.aw.pX = u
1dcb6 2e 61 77 2e 70 44 62 2d 3e 70 42 74 3b 0a 20 20  .aw.pDb->pBt;.  
1dcb7 61 73 73 65 72 74 28 20 75 2e 61 77 2e 70 58 21  assert( u.aw.pX!
1dcb8 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d  =0 );.  if( pOp-
1dcb9 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e  >opcode==OP_Open
1dcba 57 72 69 74 65 20 29 7b 0a 20 20 20 20 75 2e 61  Write ){.    u.a
1dcbb 77 2e 77 72 46 6c 61 67 20 3d 20 31 3b 0a 20 20  w.wrFlag = 1;.  
1dcbc 20 20 69 66 28 20 75 2e 61 77 2e 70 44 62 2d 3e    if( u.aw.pDb->
1dcbd 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f  pSchema->file_fo
1dcbe 72 6d 61 74 20 3c 20 70 2d 3e 6d 69 6e 57 72 69  rmat < p->minWri
1dcbf 74 65 46 69 6c 65 46 6f 72 6d 61 74 20 29 7b 0a  teFileFormat ){.
1dcc0 20 20 20 20 20 20 70 2d 3e 6d 69 6e 57 72 69 74        p->minWrit
1dcc1 65 46 69 6c 65 46 6f 72 6d 61 74 20 3d 20 75 2e  eFileFormat = u.
1dcc2 61 77 2e 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  aw.pDb->pSchema-
1dcc3 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 0a 20 20  >file_format;.  
1dcc4 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
1dcc5 20 75 2e 61 77 2e 77 72 46 6c 61 67 20 3d 20 30   u.aw.wrFlag = 0
1dcc6 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d  ;.  }.  if( pOp-
1dcc7 3e 70 35 20 29 7b 0a 20 20 20 20 61 73 73 65 72  >p5 ){.    asser
1dcc8 74 28 20 75 2e 61 77 2e 70 32 3e 30 20 29 3b 0a  t( u.aw.p2>0 );.
1dcc9 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 61 77      assert( u.aw
1dcca 2e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  .p2<=p->nMem );.
1dccb 20 20 20 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61      pIn2 = &p->a
1dccc 4d 65 6d 5b 75 2e 61 77 2e 70 32 5d 3b 0a 20 20  Mem[u.aw.p2];.  
1dccd 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1dcce 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 32 29  Integerify(pIn2)
1dccf 3b 0a 20 20 20 20 75 2e 61 77 2e 70 32 20 3d 20  ;.    u.aw.p2 = 
1dcd0 28 69 6e 74 29 70 49 6e 32 2d 3e 75 2e 69 3b 0a  (int)pIn2->u.i;.
1dcd1 20 20 20 20 2f 2a 20 54 68 65 20 75 2e 61 77 2e      /* The u.aw.
1dcd2 70 32 20 76 61 6c 75 65 20 61 6c 77 61 79 73 20  p2 value always 
1dcd3 63 6f 6d 65 73 20 66 72 6f 6d 20 61 20 70 72 69  comes from a pri
1dcd4 6f 72 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c  or OP_CreateTabl
1dcd5 65 20 6f 70 63 6f 64 65 20 61 6e 64 0a 20 20 20  e opcode and.   
1dcd6 20 2a 2a 20 74 68 61 74 20 6f 70 63 6f 64 65 20   ** that opcode 
1dcd7 77 69 6c 6c 20 61 6c 77 61 79 73 20 73 65 74 20  will always set 
1dcd8 74 68 65 20 75 2e 61 77 2e 70 32 20 76 61 6c 75  the u.aw.p2 valu
1dcd9 65 20 74 6f 20 32 20 6f 72 20 6d 6f 72 65 20 6f  e to 2 or more o
1dcda 72 20 65 6c 73 65 20 66 61 69 6c 2e 0a 20 20 20  r else fail..   
1dcdb 20 2a 2a 20 49 66 20 74 68 65 72 65 20 77 65 72   ** If there wer
1dcdc 65 20 61 20 66 61 69 6c 75 72 65 2c 20 74 68 65  e a failure, the
1dcdd 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
1dcde 65 6e 74 20 77 6f 75 6c 64 20 68 61 76 65 20 68  ent would have h
1dcdf 61 6c 74 65 64 0a 20 20 20 20 2a 2a 20 62 65 66  alted.    ** bef
1dce0 6f 72 65 20 72 65 61 63 68 69 6e 67 20 74 68 69  ore reaching thi
1dce1 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 2a  s instruction. *
1dce2 2f 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28  /.    if( NEVER(
1dce3 75 2e 61 77 2e 70 32 3c 32 29 20 29 20 7b 0a 20  u.aw.p2<2) ) {. 
1dce4 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1dce5 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
1dce6 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f       goto abort_
1dce7 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
1dce8 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f    }.  }.  if( pO
1dce9 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45  p->p4type==P4_KE
1dcea 59 49 4e 46 4f 20 29 7b 0a 20 20 20 20 75 2e 61  YINFO ){.    u.a
1dceb 77 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70  w.pKeyInfo = pOp
1dcec 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20  ->p4.pKeyInfo;. 
1dced 20 20 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f     u.aw.pKeyInfo
1dcee 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64  ->enc = ENC(p->d
1dcef 62 29 3b 0a 20 20 20 20 75 2e 61 77 2e 6e 46 69  b);.    u.aw.nFi
1dcf0 65 6c 64 20 3d 20 75 2e 61 77 2e 70 4b 65 79 49  eld = u.aw.pKeyI
1dcf1 6e 66 6f 2d 3e 6e 46 69 65 6c 64 2b 31 3b 0a 20  nfo->nField+1;. 
1dcf2 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e   }else if( pOp->
1dcf3 70 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32  p4type==P4_INT32
1dcf4 20 29 7b 0a 20 20 20 20 75 2e 61 77 2e 6e 46 69   ){.    u.aw.nFi
1dcf5 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b  eld = pOp->p4.i;
1dcf6 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
1dcf7 4f 70 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20 20 75  Op->p1>=0 );.  u
1dcf8 2e 61 77 2e 70 43 75 72 20 3d 20 61 6c 6c 6f 63  .aw.pCur = alloc
1dcf9 61 74 65 43 75 72 73 6f 72 28 70 2c 20 70 4f 70  ateCursor(p, pOp
1dcfa 2d 3e 70 31 2c 20 75 2e 61 77 2e 6e 46 69 65 6c  ->p1, u.aw.nFiel
1dcfb 64 2c 20 75 2e 61 77 2e 69 44 62 2c 20 31 29 3b  d, u.aw.iDb, 1);
1dcfc 0a 20 20 69 66 28 20 75 2e 61 77 2e 70 43 75 72  .  if( u.aw.pCur
1dcfd 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65  ==0 ) goto no_me
1dcfe 6d 3b 0a 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e  m;.  u.aw.pCur->
1dcff 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 72  nullRow = 1;.  r
1dd00 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1dd01 43 75 72 73 6f 72 28 75 2e 61 77 2e 70 58 2c 20  Cursor(u.aw.pX, 
1dd02 75 2e 61 77 2e 70 32 2c 20 75 2e 61 77 2e 77 72  u.aw.p2, u.aw.wr
1dd03 46 6c 61 67 2c 20 75 2e 61 77 2e 70 4b 65 79 49  Flag, u.aw.pKeyI
1dd04 6e 66 6f 2c 20 75 2e 61 77 2e 70 43 75 72 2d 3e  nfo, u.aw.pCur->
1dd05 70 43 75 72 73 6f 72 29 3b 0a 20 20 75 2e 61 77  pCursor);.  u.aw
1dd06 2e 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20  .pCur->pKeyInfo 
1dd07 3d 20 75 2e 61 77 2e 70 4b 65 79 49 6e 66 6f 3b  = u.aw.pKeyInfo;
1dd08 0a 0a 20 20 2f 2a 20 53 69 6e 63 65 20 69 74 20  ..  /* Since it 
1dd09 70 65 72 66 6f 72 6d 73 20 6e 6f 20 6d 65 6d 6f  performs no memo
1dd0a 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 72  ry allocation or
1dd0b 20 49 4f 2c 20 74 68 65 20 6f 6e 6c 79 20 76 61   IO, the only va
1dd0c 6c 75 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 73  lues that.  ** s
1dd0d 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f  qlite3BtreeCurso
1dd0e 72 28 29 20 6d 61 79 20 72 65 74 75 72 6e 20 61  r() may return a
1dd0f 72 65 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 20  re SQLITE_EMPTY 
1dd10 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 20  and SQLITE_OK.. 
1dd11 20 2a 2a 20 53 51 4c 49 54 45 5f 45 4d 50 54 59   ** SQLITE_EMPTY
1dd12 20 69 73 20 6f 6e 6c 79 20 72 65 74 75 72 6e 65   is only returne
1dd13 64 20 77 68 65 6e 20 61 74 74 65 6d 70 74 69 6e  d when attemptin
1dd14 67 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 74 61  g to open the ta
1dd15 62 6c 65 0a 20 20 2a 2a 20 72 6f 6f 74 65 64 20  ble.  ** rooted 
1dd16 61 74 20 70 61 67 65 20 31 20 6f 66 20 61 20 7a  at page 1 of a z
1dd17 65 72 6f 2d 62 79 74 65 20 64 61 74 61 62 61 73  ero-byte databas
1dd18 65 2e 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  e.  */.  assert(
1dd19 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 4d 50 54   rc==SQLITE_EMPT
1dd1a 59 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f  Y || rc==SQLITE_
1dd1b 4f 4b 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  OK );.  if( rc==
1dd1c 53 51 4c 49 54 45 5f 45 4d 50 54 59 20 29 7b 0a  SQLITE_EMPTY ){.
1dd1d 20 20 20 20 75 2e 61 77 2e 70 43 75 72 2d 3e 70      u.aw.pCur->p
1dd1e 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 20 20  Cursor = 0;.    
1dd1f 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1dd20 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68    }..  /* Set th
1dd21 65 20 56 64 62 65 43 75 72 73 6f 72 2e 69 73 54  e VdbeCursor.isT
1dd22 61 62 6c 65 20 61 6e 64 20 69 73 49 6e 64 65 78  able and isIndex
1dd23 20 76 61 72 69 61 62 6c 65 73 2e 20 50 72 65 76   variables. Prev
1dd24 69 6f 75 73 20 76 65 72 73 69 6f 6e 73 20 6f 66  ious versions of
1dd25 0a 20 20 2a 2a 20 53 51 4c 69 74 65 20 75 73 65  .  ** SQLite use
1dd26 64 20 74 6f 20 63 68 65 63 6b 20 69 66 20 74 68  d to check if th
1dd27 65 20 72 6f 6f 74 2d 70 61 67 65 20 66 6c 61 67  e root-page flag
1dd28 73 20 77 65 72 65 20 73 61 6e 65 20 61 74 20 74  s were sane at t
1dd29 68 69 73 20 70 6f 69 6e 74 0a 20 20 2a 2a 20 61  his point.  ** a
1dd2a 6e 64 20 72 65 70 6f 72 74 20 64 61 74 61 62 61  nd report databa
1dd2b 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 69 66  se corruption if
1dd2c 20 74 68 65 79 20 77 65 72 65 20 6e 6f 74 2c 20   they were not, 
1dd2d 62 75 74 20 74 68 69 73 20 63 68 65 63 6b 20 68  but this check h
1dd2e 61 73 0a 20 20 2a 2a 20 73 69 6e 63 65 20 6d 6f  as.  ** since mo
1dd2f 76 65 64 20 69 6e 74 6f 20 74 68 65 20 62 74 72  ved into the btr
1dd30 65 65 20 6c 61 79 65 72 2e 20 20 2a 2f 0a 20 20  ee layer.  */.  
1dd31 75 2e 61 77 2e 70 43 75 72 2d 3e 69 73 54 61 62  u.aw.pCur->isTab
1dd32 6c 65 20 3d 20 70 4f 70 2d 3e 70 34 74 79 70 65  le = pOp->p4type
1dd33 21 3d 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a 20 20  !=P4_KEYINFO;.  
1dd34 75 2e 61 77 2e 70 43 75 72 2d 3e 69 73 49 6e 64  u.aw.pCur->isInd
1dd35 65 78 20 3d 20 21 75 2e 61 77 2e 70 43 75 72 2d  ex = !u.aw.pCur-
1dd36 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62 72 65 61  >isTable;.  brea
1dd37 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1dd38 20 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 20 50   OpenEphemeral P
1dd39 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a  1 P2 * P4 *.**.*
1dd3a 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20 63 75 72  * Open a new cur
1dd3b 73 6f 72 20 50 31 20 74 6f 20 61 20 74 72 61 6e  sor P1 to a tran
1dd3c 73 69 65 6e 74 20 74 61 62 6c 65 2e 0a 2a 2a 20  sient table..** 
1dd3d 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 61 6c  The cursor is al
1dd3e 77 61 79 73 20 6f 70 65 6e 65 64 20 72 65 61 64  ways opened read
1dd3f 2f 77 72 69 74 65 20 65 76 65 6e 20 69 66 20 0a  /write even if .
1dd40 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  ** the main data
1dd41 62 61 73 65 20 69 73 20 72 65 61 64 2d 6f 6e 6c  base is read-onl
1dd42 79 2e 20 20 54 68 65 20 74 72 61 6e 73 69 65 6e  y.  The transien
1dd43 74 20 6f 72 20 76 69 72 74 75 61 6c 0a 2a 2a 20  t or virtual.** 
1dd44 74 61 62 6c 65 20 69 73 20 64 65 6c 65 74 65 64  table is deleted
1dd45 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 77   automatically w
1dd46 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69  hen the cursor i
1dd47 73 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20  s closed..**.** 
1dd48 50 32 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  P2 is the number
1dd49 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
1dd4a 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
1dd4b 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72 20  ..** The cursor 
1dd4c 70 6f 69 6e 74 73 20 74 6f 20 61 20 42 54 72 65  points to a BTre
1dd4d 65 20 74 61 62 6c 65 20 69 66 20 50 34 3d 3d 30  e table if P4==0
1dd4e 20 61 6e 64 20 74 6f 20 61 20 42 54 72 65 65 20   and to a BTree 
1dd4f 69 6e 64 65 78 0a 2a 2a 20 69 66 20 50 34 20 69  index.** if P4 i
1dd50 73 20 6e 6f 74 20 30 2e 20 20 49 66 20 50 34 20  s not 0.  If P4 
1dd51 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69 74 20  is not NULL, it 
1dd52 70 6f 69 6e 74 73 20 74 6f 20 61 20 4b 65 79 49  points to a KeyI
1dd53 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a 2a 2a  nfo structure.**
1dd54 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74 68   that defines th
1dd55 65 20 66 6f 72 6d 61 74 20 6f 66 20 6b 65 79 73  e format of keys
1dd56 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a   in the index..*
1dd57 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65  *.** This opcode
1dd58 20 77 61 73 20 6f 6e 63 65 20 63 61 6c 6c 65 64   was once called
1dd59 20 4f 70 65 6e 54 65 6d 70 2e 20 20 42 75 74 20   OpenTemp.  But 
1dd5a 74 68 61 74 20 63 72 65 61 74 65 64 0a 2a 2a 20  that created.** 
1dd5b 63 6f 6e 66 75 73 69 6f 6e 20 62 65 63 61 75 73  confusion becaus
1dd5c 65 20 74 68 65 20 74 65 72 6d 20 22 74 65 6d 70  e the term "temp
1dd5d 20 74 61 62 6c 65 22 2c 20 6d 69 67 68 74 20 72   table", might r
1dd5e 65 66 65 72 20 65 69 74 68 65 72 0a 2a 2a 20 74  efer either.** t
1dd5f 6f 20 61 20 54 45 4d 50 20 74 61 62 6c 65 20 61  o a TEMP table a
1dd60 74 20 74 68 65 20 53 51 4c 20 6c 65 76 65 6c 2c  t the SQL level,
1dd61 20 6f 72 20 74 6f 20 61 20 74 61 62 6c 65 20 6f   or to a table o
1dd62 70 65 6e 65 64 20 62 79 0a 2a 2a 20 74 68 69 73  pened by.** this
1dd63 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 6e 20 74   opcode.  Then t
1dd64 68 69 73 20 6f 70 63 6f 64 65 20 77 61 73 20 63  his opcode was c
1dd65 61 6c 6c 20 4f 70 65 6e 56 69 72 74 75 61 6c 2e  all OpenVirtual.
1dd66 20 20 42 75 74 0a 2a 2a 20 74 68 61 74 20 63 72    But.** that cr
1dd67 65 61 74 65 64 20 63 6f 6e 66 75 73 69 6f 6e 20  eated confusion 
1dd68 77 69 74 68 20 74 68 65 20 77 68 6f 6c 65 20 76  with the whole v
1dd69 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 69 64 65  irtual-table ide
1dd6a 61 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4f 70  a..*/.case OP_Op
1dd6b 65 6e 45 70 68 65 6d 65 72 61 6c 3a 20 7b 0a 23  enEphemeral: {.#
1dd6c 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1dd6d 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1dd6e 6e 74 6f 20 75 2e 61 78 20 2a 2f 0a 20 20 56 64  nto u.ax */.  Vd
1dd6f 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 23  beCursor *pCx;.#
1dd70 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1dd71 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1dd72 6e 74 6f 20 75 2e 61 78 20 2a 2f 0a 20 20 73 74  nto u.ax */.  st
1dd73 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 6f  atic const int o
1dd74 70 65 6e 46 6c 61 67 73 20 3d 0a 20 20 20 20 20  penFlags =.     
1dd75 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
1dd76 44 57 52 49 54 45 20 7c 0a 20 20 20 20 20 20 53  DWRITE |.      S
1dd77 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
1dd78 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45  E |.      SQLITE
1dd79 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20  _OPEN_EXCLUSIVE 
1dd7a 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f  |.      SQLITE_O
1dd7b 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53  PEN_DELETEONCLOS
1dd7c 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45  E |.      SQLITE
1dd7d 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f  _OPEN_TRANSIENT_
1dd7e 44 42 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  DB;..  assert( p
1dd7f 4f 70 2d 3e 70 31 3e 3d 30 20 29 3b 0a 20 20 75  Op->p1>=0 );.  u
1dd80 2e 61 78 2e 70 43 78 20 3d 20 61 6c 6c 6f 63 61  .ax.pCx = alloca
1dd81 74 65 43 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d  teCursor(p, pOp-
1dd82 3e 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20 2d 31  >p1, pOp->p2, -1
1dd83 2c 20 31 29 3b 0a 20 20 69 66 28 20 75 2e 61 78  , 1);.  if( u.ax
1dd84 2e 70 43 78 3d 3d 30 20 29 20 67 6f 74 6f 20 6e  .pCx==0 ) goto n
1dd85 6f 5f 6d 65 6d 3b 0a 20 20 75 2e 61 78 2e 70 43  o_mem;.  u.ax.pC
1dd86 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a  x->nullRow = 1;.
1dd87 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1dd88 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20 30  reeFactory(db, 0
1dd89 2c 20 31 2c 20 53 51 4c 49 54 45 5f 44 45 46 41  , 1, SQLITE_DEFA
1dd8a 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53  ULT_TEMP_CACHE_S
1dd8b 49 5a 45 2c 20 6f 70 65 6e 46 6c 61 67 73 2c 0a  IZE, openFlags,.
1dd8c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dd8d 20 20 20 20 20 20 20 20 20 20 20 26 75 2e 61 78             &u.ax
1dd8e 2e 70 43 78 2d 3e 70 42 74 29 3b 0a 20 20 69 66  .pCx->pBt);.  if
1dd8f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1dd90 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
1dd91 74 65 33 42 74 72 65 65 42 65 67 69 6e 54 72 61  te3BtreeBeginTra
1dd92 6e 73 28 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74  ns(u.ax.pCx->pBt
1dd93 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  , 1);.  }.  if( 
1dd94 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1dd95 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 74 72 61  .    /* If a tra
1dd96 6e 73 69 65 6e 74 20 69 6e 64 65 78 20 69 73 20  nsient index is 
1dd97 72 65 71 75 69 72 65 64 2c 20 63 72 65 61 74 65  required, create
1dd98 20 69 74 20 62 79 20 63 61 6c 6c 69 6e 67 0a 20   it by calling. 
1dd99 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72     ** sqlite3Btr
1dd9a 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 29 20  eeCreateTable() 
1dd9b 77 69 74 68 20 74 68 65 20 42 54 52 45 45 5f 5a  with the BTREE_Z
1dd9c 45 52 4f 44 41 54 41 20 66 6c 61 67 20 62 65 66  ERODATA flag bef
1dd9d 6f 72 65 0a 20 20 20 20 2a 2a 20 6f 70 65 6e 69  ore.    ** openi
1dd9e 6e 67 20 69 74 2e 20 49 66 20 61 20 74 72 61 6e  ng it. If a tran
1dd9f 73 69 65 6e 74 20 74 61 62 6c 65 20 69 73 20 72  sient table is r
1dda0 65 71 75 69 72 65 64 2c 20 6a 75 73 74 20 75 73  equired, just us
1dda1 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 61 75 74  e the.    ** aut
1dda2 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65 61 74  omatically creat
1dda3 65 64 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f  ed table with ro
1dda4 6f 74 2d 70 61 67 65 20 31 20 28 61 6e 20 49 4e  ot-page 1 (an IN
1dda5 54 4b 45 59 20 74 61 62 6c 65 29 2e 0a 20 20 20  TKEY table)..   
1dda6 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 70 2d   */.    if( pOp-
1dda7 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 29 7b 0a  >p4.pKeyInfo ){.
1dda8 20 20 20 20 20 20 69 6e 74 20 70 67 6e 6f 3b 0a        int pgno;.
1dda9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1ddaa 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 4b 45  p->p4type==P4_KE
1ddab 59 49 4e 46 4f 20 29 3b 0a 20 20 20 20 20 20 72  YINFO );.      r
1ddac 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1ddad 43 72 65 61 74 65 54 61 62 6c 65 28 75 2e 61 78  CreateTable(u.ax
1ddae 2e 70 43 78 2d 3e 70 42 74 2c 20 26 70 67 6e 6f  .pCx->pBt, &pgno
1ddaf 2c 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41  , BTREE_ZERODATA
1ddb0 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
1ddb1 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1ddb2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 67        assert( pg
1ddb3 6e 6f 3d 3d 4d 41 53 54 45 52 5f 52 4f 4f 54 2b  no==MASTER_ROOT+
1ddb4 31 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  1 );.        rc 
1ddb5 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75  = sqlite3BtreeCu
1ddb6 72 73 6f 72 28 75 2e 61 78 2e 70 43 78 2d 3e 70  rsor(u.ax.pCx->p
1ddb7 42 74 2c 20 70 67 6e 6f 2c 20 31 2c 0a 20 20 20  Bt, pgno, 1,.   
1ddb8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ddb9 20 20 20 20 20 20 20 20 20 20 20 20 20 28 4b 65               (Ke
1ddba 79 49 6e 66 6f 2a 29 70 4f 70 2d 3e 70 34 2e 7a  yInfo*)pOp->p4.z
1ddbb 2c 20 75 2e 61 78 2e 70 43 78 2d 3e 70 43 75 72  , u.ax.pCx->pCur
1ddbc 73 6f 72 29 3b 0a 20 20 20 20 20 20 20 20 75 2e  sor);.        u.
1ddbd 61 78 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f  ax.pCx->pKeyInfo
1ddbe 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49   = pOp->p4.pKeyI
1ddbf 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 75 2e 61  nfo;.        u.a
1ddc0 78 2e 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d  x.pCx->pKeyInfo-
1ddc1 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62  >enc = ENC(p->db
1ddc2 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1ddc3 20 75 2e 61 78 2e 70 43 78 2d 3e 69 73 54 61 62   u.ax.pCx->isTab
1ddc4 6c 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  le = 0;.    }els
1ddc5 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  e{.      rc = sq
1ddc6 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
1ddc7 28 75 2e 61 78 2e 70 43 78 2d 3e 70 42 74 2c 20  (u.ax.pCx->pBt, 
1ddc8 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20 31 2c 20  MASTER_ROOT, 1, 
1ddc9 30 2c 20 75 2e 61 78 2e 70 43 78 2d 3e 70 43 75  0, u.ax.pCx->pCu
1ddca 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 75 2e 61  rsor);.      u.a
1ddcb 78 2e 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d  x.pCx->isTable =
1ddcc 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
1ddcd 75 2e 61 78 2e 70 43 78 2d 3e 69 73 49 6e 64 65  u.ax.pCx->isInde
1ddce 78 20 3d 20 21 75 2e 61 78 2e 70 43 78 2d 3e 69  x = !u.ax.pCx->i
1ddcf 73 54 61 62 6c 65 3b 0a 20 20 62 72 65 61 6b 3b  sTable;.  break;
1ddd0 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4f  .}../* Opcode: O
1ddd1 70 65 6e 50 73 65 75 64 6f 20 50 31 20 50 32 20  penPseudo P1 P2 
1ddd2 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65  P3 * *.**.** Ope
1ddd3 6e 20 61 20 6e 65 77 20 63 75 72 73 6f 72 20 74  n a new cursor t
1ddd4 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  hat points to a 
1ddd5 66 61 6b 65 20 74 61 62 6c 65 20 74 68 61 74 20  fake table that 
1ddd6 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c  contains a singl
1ddd7 65 0a 2a 2a 20 72 6f 77 20 6f 66 20 64 61 74 61  e.** row of data
1ddd8 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f  .  The content o
1ddd9 66 20 74 68 61 74 20 6f 6e 65 20 72 6f 77 20 69  f that one row i
1ddda 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  n the content of
1dddb 20 6d 65 6d 6f 72 79 0a 2a 2a 20 72 65 67 69 73   memory.** regis
1dddc 74 65 72 20 50 32 2e 20 20 49 6e 20 6f 74 68 65  ter P2.  In othe
1dddd 72 20 77 6f 72 64 73 2c 20 63 75 72 73 6f 72 20  r words, cursor 
1ddde 50 31 20 62 65 63 6f 6d 65 73 20 61 6e 20 61 6c  P1 becomes an al
1dddf 69 61 73 20 66 6f 72 20 74 68 65 20 0a 2a 2a 20  ias for the .** 
1dde0 4d 45 4d 5f 42 6c 6f 62 20 63 6f 6e 74 65 6e 74  MEM_Blob content
1dde1 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 72 65   contained in re
1dde2 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a  gister P2..**.**
1dde3 20 41 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20   A pseudo-table 
1dde4 63 72 65 61 74 65 64 20 62 79 20 74 68 69 73 20  created by this 
1dde5 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64 20 74  opcode is used t
1dde6 6f 20 68 6f 6c 64 20 74 68 65 20 61 20 73 69 6e  o hold the a sin
1dde7 67 6c 65 0a 2a 2a 20 72 6f 77 20 6f 75 74 70 75  gle.** row outpu
1dde8 74 20 66 72 6f 6d 20 74 68 65 20 73 6f 72 74 65  t from the sorte
1dde9 72 20 73 6f 20 74 68 61 74 20 74 68 65 20 72 6f  r so that the ro
1ddea 77 20 63 61 6e 20 62 65 20 64 65 63 6f 6d 70 6f  w can be decompo
1ddeb 73 65 64 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64 69  sed into.** indi
1ddec 76 69 64 75 61 6c 20 63 6f 6c 75 6d 6e 73 20 75  vidual columns u
1dded 73 69 6e 67 20 74 68 65 20 4f 50 5f 43 6f 6c 75  sing the OP_Colu
1ddee 6d 6e 20 6f 70 63 6f 64 65 2e 20 20 54 68 65 20  mn opcode.  The 
1ddef 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65  OP_Column opcode
1ddf0 0a 2a 2a 20 69 73 20 74 68 65 20 6f 6e 6c 79 20  .** is the only 
1ddf1 63 75 72 73 6f 72 20 6f 70 63 6f 64 65 20 74 68  cursor opcode th
1ddf2 61 74 20 77 6f 72 6b 73 20 77 69 74 68 20 61 20  at works with a 
1ddf3 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2a  pseudo-table..**
1ddf4 0a 2a 2a 20 50 33 20 69 73 20 74 68 65 20 6e 75  .** P3 is the nu
1ddf5 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 20 69  mber of fields i
1ddf6 6e 20 74 68 65 20 72 65 63 6f 72 64 73 20 74 68  n the records th
1ddf7 61 74 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 65  at will be store
1ddf8 64 20 62 79 0a 2a 2a 20 74 68 65 20 70 73 65 75  d by.** the pseu
1ddf9 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73  do-table..*/.cas
1ddfa 65 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f 3a  e OP_OpenPseudo:
1ddfb 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
1ddfc 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1ddfd 65 64 20 69 6e 74 6f 20 75 2e 61 79 20 2a 2f 0a  ed into u.ay */.
1ddfe 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1ddff 78 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  x;.#endif /* loc
1de00 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1de01 65 64 20 69 6e 74 6f 20 75 2e 61 79 20 2a 2f 0a  ed into u.ay */.
1de02 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1de03 70 31 3e 3d 30 20 29 3b 0a 20 20 75 2e 61 79 2e  p1>=0 );.  u.ay.
1de04 70 43 78 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75  pCx = allocateCu
1de05 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70 31 2c  rsor(p, pOp->p1,
1de06 20 70 4f 70 2d 3e 70 33 2c 20 2d 31 2c 20 30 29   pOp->p3, -1, 0)
1de07 3b 0a 20 20 69 66 28 20 75 2e 61 79 2e 70 43 78  ;.  if( u.ay.pCx
1de08 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65  ==0 ) goto no_me
1de09 6d 3b 0a 20 20 75 2e 61 79 2e 70 43 78 2d 3e 6e  m;.  u.ay.pCx->n
1de0a 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20 20 75 2e  ullRow = 1;.  u.
1de0b 61 79 2e 70 43 78 2d 3e 70 73 65 75 64 6f 54 61  ay.pCx->pseudoTa
1de0c 62 6c 65 52 65 67 20 3d 20 70 4f 70 2d 3e 70 32  bleReg = pOp->p2
1de0d 3b 0a 20 20 75 2e 61 79 2e 70 43 78 2d 3e 69 73  ;.  u.ay.pCx->is
1de0e 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 75 2e 61  Table = 1;.  u.a
1de0f 79 2e 70 43 78 2d 3e 69 73 49 6e 64 65 78 20 3d  y.pCx->isIndex =
1de10 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a   0;.  break;.}..
1de11 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6c 6f 73 65  /* Opcode: Close
1de12 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a   P1 * * * *.**.*
1de13 2a 20 43 6c 6f 73 65 20 61 20 63 75 72 73 6f 72  * Close a cursor
1de14 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e   previously open
1de15 65 64 20 61 73 20 50 31 2e 20 20 49 66 20 50 31  ed as P1.  If P1
1de16 20 69 73 20 6e 6f 74 0a 2a 2a 20 63 75 72 72 65   is not.** curre
1de17 6e 74 6c 79 20 6f 70 65 6e 2c 20 74 68 69 73 20  ntly open, this 
1de18 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61  instruction is a
1de19 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 63 61 73 65 20   no-op..*/.case 
1de1a 4f 50 5f 43 6c 6f 73 65 3a 20 7b 0a 20 20 61 73  OP_Close: {.  as
1de1b 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1de1c 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
1de1d 43 75 72 73 6f 72 20 29 3b 0a 20 20 73 71 6c 69  Cursor );.  sqli
1de1e 74 65 33 56 64 62 65 46 72 65 65 43 75 72 73 6f  te3VdbeFreeCurso
1de1f 72 28 70 2c 20 70 2d 3e 61 70 43 73 72 5b 70 4f  r(p, p->apCsr[pO
1de20 70 2d 3e 70 31 5d 29 3b 0a 20 20 70 2d 3e 61 70  p->p1]);.  p->ap
1de21 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 20 3d 20 30  Csr[pOp->p1] = 0
1de22 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1de23 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 47 65 20   Opcode: SeekGe 
1de24 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a  P1 P2 P3 P4 *.**
1de25 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31  .** If cursor P1
1de26 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51   refers to an SQ
1de27 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20  L table (B-Tree 
1de28 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 65  that uses intege
1de29 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65  r keys), .** use
1de2a 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
1de2b 67 69 73 74 65 72 20 50 33 20 61 73 20 74 68 65  gister P3 as the
1de2c 20 6b 65 79 2e 20 20 49 66 20 63 75 72 73 6f 72   key.  If cursor
1de2d 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74   P1 refers .** t
1de2e 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20  o an SQL index, 
1de2f 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66  then P3 is the f
1de30 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79  irst in an array
1de31 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73   of P4 registers
1de32 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73   .** that are us
1de33 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65  ed as an unpacke
1de34 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a  d index key. .**
1de35 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63  .** Reposition c
1de36 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74  ursor P1 so that
1de37 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74    it points to t
1de38 68 65 20 73 6d 61 6c 6c 65 73 74 20 65 6e 74 72  he smallest entr
1de39 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 67 72  y that .** is gr
1de3a 65 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71  eater than or eq
1de3b 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 76  ual to the key v
1de3c 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20 61  alue. If there a
1de3d 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 0a 2a  re no records .*
1de3e 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  * greater than o
1de3f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6b  r equal to the k
1de40 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f 74  ey and P2 is not
1de41 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70   zero, then jump
1de42 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65   to P2..**.** Se
1de43 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e  e also: Found, N
1de44 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63  otFound, Distinc
1de45 74 2c 20 53 65 65 6b 4c 74 2c 20 53 65 65 6b 47  t, SeekLt, SeekG
1de46 74 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f 2a 20  t, SeekLe.*/./* 
1de47 4f 70 63 6f 64 65 3a 20 53 65 65 6b 47 74 20 50  Opcode: SeekGt P
1de48 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a  1 P2 P3 P4 *.**.
1de49 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 20  ** If cursor P1 
1de4a 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c  refers to an SQL
1de4b 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74   table (B-Tree t
1de4c 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 72  hat uses integer
1de4d 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20   keys), .** use 
1de4e 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67  the value in reg
1de4f 69 73 74 65 72 20 50 33 20 61 73 20 61 20 6b 65  ister P3 as a ke
1de50 79 2e 20 49 66 20 63 75 72 73 6f 72 20 50 31 20  y. If cursor P1 
1de51 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20 61 6e  refers .** to an
1de52 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 68 65 6e   SQL index, then
1de53 20 50 33 20 69 73 20 74 68 65 20 66 69 72 73 74   P3 is the first
1de54 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20   in an array of 
1de55 50 34 20 72 65 67 69 73 74 65 72 73 20 0a 2a 2a  P4 registers .**
1de56 20 74 68 61 74 20 61 72 65 20 75 73 65 64 20 61   that are used a
1de57 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e  s an unpacked in
1de58 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a 2a 20  dex key. .**.** 
1de59 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 72 73 6f  Reposition curso
1de5a 72 20 50 31 20 73 6f 20 74 68 61 74 20 20 69 74  r P1 so that  it
1de5b 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 73   points to the s
1de5c 6d 61 6c 6c 65 73 74 20 65 6e 74 72 79 20 74 68  mallest entry th
1de5d 61 74 20 0a 2a 2a 20 69 73 20 67 72 65 61 74 65  at .** is greate
1de5e 72 20 74 68 61 6e 20 74 68 65 20 6b 65 79 20 76  r than the key v
1de5f 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20 61  alue. If there a
1de60 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 67 72  re no records gr
1de61 65 61 74 65 72 20 74 68 61 6e 20 0a 2a 2a 20 74  eater than .** t
1de62 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73  he key and P2 is
1de63 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20   not zero, then 
1de64 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a  jump to P2..**.*
1de65 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e  * See also: Foun
1de66 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73  d, NotFound, Dis
1de67 74 69 6e 63 74 2c 20 53 65 65 6b 4c 74 2c 20 53  tinct, SeekLt, S
1de68 65 65 6b 47 65 2c 20 53 65 65 6b 4c 65 0a 2a 2f  eekGe, SeekLe.*/
1de69 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b  ./* Opcode: Seek
1de6a 4c 74 20 50 31 20 50 32 20 50 33 20 50 34 20 2a  Lt P1 P2 P3 P4 *
1de6b 20 0a 2a 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f   .**.** If curso
1de6c 72 20 50 31 20 72 65 66 65 72 73 20 74 6f 20 61  r P1 refers to a
1de6d 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 42 2d 54  n SQL table (B-T
1de6e 72 65 65 20 74 68 61 74 20 75 73 65 73 20 69 6e  ree that uses in
1de6f 74 65 67 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a  teger keys), .**
1de70 20 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 69   use the value i
1de71 6e 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73  n register P3 as
1de72 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f   a key. If curso
1de73 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20  r P1 refers .** 
1de74 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c  to an SQL index,
1de75 20 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20   then P3 is the 
1de76 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61  first in an arra
1de77 79 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72  y of P4 register
1de78 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75  s .** that are u
1de79 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b  sed as an unpack
1de7a 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a  ed index key. .*
1de7b 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20  *.** Reposition 
1de7c 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61  cursor P1 so tha
1de7d 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  t  it points to 
1de7e 74 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72  the largest entr
1de7f 79 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65  y that .** is le
1de80 73 73 20 74 68 61 6e 20 74 68 65 20 6b 65 79 20  ss than the key 
1de81 76 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20  value. If there 
1de82 61 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 6c  are no records l
1de83 65 73 73 20 74 68 61 6e 20 0a 2a 2a 20 74 68 65  ess than .** the
1de84 20 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e   key and P2 is n
1de85 6f 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75  ot zero, then ju
1de86 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  mp to P2..**.** 
1de87 53 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c  See also: Found,
1de88 20 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69   NotFound, Disti
1de89 6e 63 74 2c 20 53 65 65 6b 47 74 2c 20 53 65 65  nct, SeekGt, See
1de8a 6b 47 65 2c 20 53 65 65 6b 4c 65 0a 2a 2f 0a 2f  kGe, SeekLe.*/./
1de8b 2a 20 4f 70 63 6f 64 65 3a 20 53 65 65 6b 4c 65  * Opcode: SeekLe
1de8c 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a   P1 P2 P3 P4 *.*
1de8d 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50  *.** If cursor P
1de8e 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53  1 refers to an S
1de8f 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65  QL table (B-Tree
1de90 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67   that uses integ
1de91 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73  er keys), .** us
1de92 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  e the value in r
1de93 65 67 69 73 74 65 72 20 50 33 20 61 73 20 61 20  egister P3 as a 
1de94 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72 20 50  key. If cursor P
1de95 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74 6f 20  1 refers .** to 
1de96 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20 74 68  an SQL index, th
1de97 65 6e 20 50 33 20 69 73 20 74 68 65 20 66 69 72  en P3 is the fir
1de98 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f  st in an array o
1de99 66 20 50 34 20 72 65 67 69 73 74 65 72 73 20 0a  f P4 registers .
1de9a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73 65 64  ** that are used
1de9b 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65 64 20   as an unpacked 
1de9c 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a 0a 2a  index key. .**.*
1de9d 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63 75 72  * Reposition cur
1de9e 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 69  sor P1 so that i
1de9f 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  t points to the 
1dea0 6c 61 72 67 65 73 74 20 65 6e 74 72 79 20 74 68  largest entry th
1dea1 61 74 20 0a 2a 2a 20 69 73 20 6c 65 73 73 20 74  at .** is less t
1dea2 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
1dea3 74 68 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 49  the key value. I
1dea4 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72  f there are no r
1dea5 65 63 6f 72 64 73 20 0a 2a 2a 20 6c 65 73 73 20  ecords .** less 
1dea6 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
1dea7 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20   the key and P2 
1dea8 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65  is not zero, the
1dea9 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a  n jump to P2..**
1deaa 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46 6f  .** See also: Fo
1deab 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c 20 44  und, NotFound, D
1deac 69 73 74 69 6e 63 74 2c 20 53 65 65 6b 47 74 2c  istinct, SeekGt,
1dead 20 53 65 65 6b 47 65 2c 20 53 65 65 6b 4c 74 0a   SeekGe, SeekLt.
1deae 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b 4c  */.case OP_SeekL
1deaf 74 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75  t:         /* ju
1deb0 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20  mp, in3 */.case 
1deb1 4f 50 5f 53 65 65 6b 4c 65 3a 20 20 20 20 20 20  OP_SeekLe:      
1deb2 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20     /* jump, in3 
1deb3 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65 6b 47  */.case OP_SeekG
1deb4 65 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75  e:         /* ju
1deb5 6d 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20  mp, in3 */.case 
1deb6 4f 50 5f 53 65 65 6b 47 74 3a 20 7b 20 20 20 20  OP_SeekGt: {    
1deb7 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20     /* jump, in3 
1deb8 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
1deb9 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1deba 65 64 20 69 6e 74 6f 20 75 2e 61 7a 20 2a 2f 0a  ed into u.az */.
1debb 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 69 6e 74    int res;.  int
1debc 20 6f 63 3b 0a 20 20 56 64 62 65 43 75 72 73 6f   oc;.  VdbeCurso
1debd 72 20 2a 70 43 3b 0a 20 20 55 6e 70 61 63 6b 65  r *pC;.  Unpacke
1debe 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 69 6e 74  dRecord r;.  int
1debf 20 6e 46 69 65 6c 64 3b 0a 20 20 69 36 34 20 69   nField;.  i64 i
1dec0 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 54 68 65  Key;      /* The
1dec1 20 72 6f 77 69 64 20 77 65 20 61 72 65 20 74 6f   rowid we are to
1dec2 20 73 65 65 6b 20 74 6f 20 2a 2f 0a 23 65 6e 64   seek to */.#end
1dec3 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1dec4 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1dec5 20 75 2e 61 7a 20 2a 2f 0a 0a 20 20 61 73 73 65   u.az */..  asse
1dec6 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1dec7 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
1dec8 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74  rsor );.  assert
1dec9 28 20 70 4f 70 2d 3e 70 32 21 3d 30 20 29 3b 0a  ( pOp->p2!=0 );.
1deca 20 20 75 2e 61 7a 2e 70 43 20 3d 20 70 2d 3e 61    u.az.pC = p->a
1decb 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20  pCsr[pOp->p1];. 
1decc 20 61 73 73 65 72 74 28 20 75 2e 61 7a 2e 70 43   assert( u.az.pC
1decd 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
1dece 20 75 2e 61 7a 2e 70 43 2d 3e 70 73 65 75 64 6f   u.az.pC->pseudo
1decf 54 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20  TableReg==0 );. 
1ded0 20 69 66 28 20 75 2e 61 7a 2e 70 43 2d 3e 70 43   if( u.az.pC->pC
1ded1 75 72 73 6f 72 21 3d 30 20 29 7b 0a 20 20 20 20  ursor!=0 ){.    
1ded2 75 2e 61 7a 2e 6f 63 20 3d 20 70 4f 70 2d 3e 6f  u.az.oc = pOp->o
1ded3 70 63 6f 64 65 3b 0a 20 20 20 20 75 2e 61 7a 2e  pcode;.    u.az.
1ded4 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b  pC->nullRow = 0;
1ded5 0a 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 70 43  .    if( u.az.pC
1ded6 2d 3e 69 73 54 61 62 6c 65 20 29 7b 0a 20 20 20  ->isTable ){.   
1ded7 20 20 20 2f 2a 20 54 68 65 20 69 6e 70 75 74 20     /* The input 
1ded8 76 61 6c 75 65 20 69 6e 20 50 33 20 6d 69 67 68  value in P3 migh
1ded9 74 20 62 65 20 6f 66 20 61 6e 79 20 74 79 70 65  t be of any type
1deda 3a 20 69 6e 74 65 67 65 72 2c 20 72 65 61 6c 2c  : integer, real,
1dedb 20 73 74 72 69 6e 67 2c 0a 20 20 20 20 20 20 2a   string,.      *
1dedc 2a 20 62 6c 6f 62 2c 20 6f 72 20 4e 55 4c 4c 2e  * blob, or NULL.
1dedd 20 20 42 75 74 20 69 74 20 6e 65 65 64 73 20 74    But it needs t
1dede 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65 72 20  o be an integer 
1dedf 62 65 66 6f 72 65 20 77 65 20 63 61 6e 20 64 6f  before we can do
1dee0 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 73 65  .      ** the se
1dee1 65 6b 2c 20 73 6f 20 63 6f 76 65 72 74 20 69 74  ek, so covert it
1dee2 2e 20 2a 2f 0a 20 20 20 20 20 20 61 70 70 6c 79  . */.      apply
1dee3 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28  NumericAffinity(
1dee4 70 49 6e 33 29 3b 0a 20 20 20 20 20 20 75 2e 61  pIn3);.      u.a
1dee5 7a 2e 69 4b 65 79 20 3d 20 73 71 6c 69 74 65 33  z.iKey = sqlite3
1dee6 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e  VdbeIntValue(pIn
1dee7 33 29 3b 0a 20 20 20 20 20 20 75 2e 61 7a 2e 70  3);.      u.az.p
1dee8 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20  C->rowidIsValid 
1dee9 3d 20 30 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49  = 0;..      /* I
1deea 66 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 63  f the P3 value c
1deeb 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 6f 6e 76  ould not be conv
1deec 65 72 74 65 64 20 69 6e 74 6f 20 61 6e 20 69 6e  erted into an in
1deed 74 65 67 65 72 20 77 69 74 68 6f 75 74 0a 20 20  teger without.  
1deee 20 20 20 20 2a 2a 20 6c 6f 73 73 20 6f 66 20 69      ** loss of i
1deef 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 74 68 65 6e  nformation, then
1def0 20 73 70 65 63 69 61 6c 20 70 72 6f 63 65 73 73   special process
1def1 69 6e 67 20 69 73 20 72 65 71 75 69 72 65 64 2e  ing is required.
1def2 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  .. */.      if( 
1def3 28 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d  (pIn3->flags & M
1def4 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b 0a 20 20  EM_Int)==0 ){.  
1def5 20 20 20 20 20 20 69 66 28 20 28 70 49 6e 33 2d        if( (pIn3-
1def6 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61  >flags & MEM_Rea
1def7 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  l)==0 ){.       
1def8 20 20 20 2f 2a 20 49 66 20 74 68 65 20 50 33 20     /* If the P3 
1def9 76 61 6c 75 65 20 63 61 6e 6e 6f 74 20 62 65 20  value cannot be 
1defa 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61  converted into a
1defb 6e 79 20 6b 69 6e 64 20 6f 66 20 61 20 6e 75 6d  ny kind of a num
1defc 62 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20 2a  ber,.          *
1defd 2a 20 74 68 65 6e 20 74 68 65 20 73 65 65 6b 20  * then the seek 
1defe 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2c  is not possible,
1deff 20 73 6f 20 6a 75 6d 70 20 74 6f 20 50 32 20 2a   so jump to P2 *
1df00 2f 0a 20 20 20 20 20 20 20 20 20 20 70 63 20 3d  /.          pc =
1df01 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20   pOp->p2 - 1;.  
1df02 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
1df03 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1df04 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63 68 20   /* If we reach 
1df05 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e  this point, then
1df06 20 74 68 65 20 50 33 20 76 61 6c 75 65 20 6d 75   the P3 value mu
1df07 73 74 20 62 65 20 61 20 66 6c 6f 61 74 69 6e 67  st be a floating
1df08 0a 20 20 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e  .        ** poin
1df09 74 20 6e 75 6d 62 65 72 2e 20 2a 2f 0a 20 20 20  t number. */.   
1df0a 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70 49       assert( (pI
1df0b 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n3->flags & MEM_
1df0c 52 65 61 6c 29 21 3d 30 20 29 3b 0a 0a 20 20 20  Real)!=0 );..   
1df0d 20 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 69 4b       if( u.az.iK
1df0e 65 79 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54  ey==SMALLEST_INT
1df0f 36 34 20 26 26 20 28 70 49 6e 33 2d 3e 72 3c 28  64 && (pIn3->r<(
1df10 64 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79  double)u.az.iKey
1df11 20 7c 7c 20 70 49 6e 33 2d 3e 72 3e 30 29 20 29   || pIn3->r>0) )
1df12 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  {.          /* T
1df13 68 65 20 50 33 20 76 61 6c 75 65 20 69 73 20 74  he P3 value is t
1df14 6f 6f 20 6c 61 72 67 65 20 69 6e 20 6d 61 67 6e  oo large in magn
1df15 69 74 75 64 65 20 74 6f 20 62 65 20 65 78 70 72  itude to be expr
1df16 65 73 73 65 64 20 61 73 20 61 6e 0a 20 20 20 20  essed as an.    
1df17 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 67 65 72        ** integer
1df18 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 75  . */.          u
1df19 2e 61 7a 2e 72 65 73 20 3d 20 31 3b 0a 20 20 20  .az.res = 1;.   
1df1a 20 20 20 20 20 20 20 69 66 28 20 70 49 6e 33 2d         if( pIn3-
1df1b 3e 72 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  >r<0 ){.        
1df1c 20 20 20 20 69 66 28 20 75 2e 61 7a 2e 6f 63 3d      if( u.az.oc=
1df1d 3d 4f 50 5f 53 65 65 6b 47 74 20 7c 7c 20 75 2e  =OP_SeekGt || u.
1df1e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65  az.oc==OP_SeekGe
1df1f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1df20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1df21 72 65 65 46 69 72 73 74 28 75 2e 61 7a 2e 70 43  reeFirst(u.az.pC
1df22 2d 3e 70 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a  ->pCursor, &u.az
1df23 2e 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 20  .res);.         
1df24 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
1df25 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62  ITE_OK ) goto ab
1df26 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1df27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
1df28 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
1df29 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
1df2a 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65   u.az.oc==OP_See
1df2b 6b 4c 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d  kLt || u.az.oc==
1df2c 4f 50 5f 53 65 65 6b 4c 65 20 29 7b 0a 20 20 20  OP_SeekLe ){.   
1df2d 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
1df2e 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73 74  sqlite3BtreeLast
1df2f 28 75 2e 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f  (u.az.pC->pCurso
1df30 72 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a 20  r, &u.az.res);. 
1df31 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
1df32 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1df33 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1df34 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20  to_error;.      
1df35 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1df36 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66    }.          if
1df37 28 20 75 2e 61 7a 2e 72 65 73 20 29 7b 0a 20 20  ( u.az.res ){.  
1df38 20 20 20 20 20 20 20 20 20 20 70 63 20 3d 20 70            pc = p
1df39 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20  Op->p2 - 1;.    
1df3a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1df3b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
1df3c 20 7d 65 6c 73 65 20 69 66 28 20 75 2e 61 7a 2e   }else if( u.az.
1df3d 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c 74 20 7c 7c  oc==OP_SeekLt ||
1df3e 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65   u.az.oc==OP_See
1df3f 6b 47 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  kGe ){.         
1df40 20 2f 2a 20 55 73 65 20 74 68 65 20 63 65 69 6c   /* Use the ceil
1df41 69 6e 67 28 29 20 66 75 6e 63 74 69 6f 6e 20 74  ing() function t
1df42 6f 20 63 6f 6e 76 65 72 74 20 72 65 61 6c 2d 3e  o convert real->
1df43 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  int */.         
1df44 20 69 66 28 20 70 49 6e 33 2d 3e 72 20 3e 20 28   if( pIn3->r > (
1df45 64 6f 75 62 6c 65 29 75 2e 61 7a 2e 69 4b 65 79  double)u.az.iKey
1df46 20 29 20 75 2e 61 7a 2e 69 4b 65 79 2b 2b 3b 0a   ) u.az.iKey++;.
1df47 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
1df48 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20           /* Use 
1df49 74 68 65 20 66 6c 6f 6f 72 28 29 20 66 75 6e 63  the floor() func
1df4a 74 69 6f 6e 20 74 6f 20 63 6f 6e 76 65 72 74 20  tion to convert 
1df4b 72 65 61 6c 2d 3e 69 6e 74 20 2a 2f 0a 20 20 20  real->int */.   
1df4c 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75         assert( u
1df4d 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 4c  .az.oc==OP_SeekL
1df4e 65 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50  e || u.az.oc==OP
1df4f 5f 53 65 65 6b 47 74 20 29 3b 0a 20 20 20 20 20  _SeekGt );.     
1df50 20 20 20 20 20 69 66 28 20 70 49 6e 33 2d 3e 72       if( pIn3->r
1df51 20 3c 20 28 64 6f 75 62 6c 65 29 75 2e 61 7a 2e   < (double)u.az.
1df52 69 4b 65 79 20 29 20 75 2e 61 7a 2e 69 4b 65 79  iKey ) u.az.iKey
1df53 2d 2d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  --;.        }.  
1df54 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d      }.      rc =
1df55 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76   sqlite3BtreeMov
1df56 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e 61 7a  etoUnpacked(u.az
1df57 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c  .pC->pCursor, 0,
1df58 20 28 75 36 34 29 75 2e 61 7a 2e 69 4b 65 79 2c   (u64)u.az.iKey,
1df59 20 30 2c 20 26 75 2e 61 7a 2e 72 65 73 29 3b 0a   0, &u.az.res);.
1df5a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1df5b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1df5c 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
1df5d 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
1df5e 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 75 2e    }.      if( u.
1df5f 61 7a 2e 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20  az.res==0 ){.   
1df60 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f       u.az.pC->ro
1df61 77 69 64 49 73 56 61 6c 69 64 20 3d 20 31 3b 0a  widIsValid = 1;.
1df62 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d          u.az.pC-
1df63 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 75 2e 61  >lastRowid = u.a
1df64 7a 2e 69 4b 65 79 3b 0a 20 20 20 20 20 20 7d 0a  z.iKey;.      }.
1df65 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1df66 20 75 2e 61 7a 2e 6e 46 69 65 6c 64 20 3d 20 70   u.az.nField = p
1df67 4f 70 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 20 20  Op->p4.i;.      
1df68 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74  assert( pOp->p4t
1df69 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b  ype==P4_INT32 );
1df6a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 75  .      assert( u
1df6b 2e 61 7a 2e 6e 46 69 65 6c 64 3e 30 20 29 3b 0a  .az.nField>0 );.
1df6c 20 20 20 20 20 20 75 2e 61 7a 2e 72 2e 70 4b 65        u.az.r.pKe
1df6d 79 49 6e 66 6f 20 3d 20 75 2e 61 7a 2e 70 43 2d  yInfo = u.az.pC-
1df6e 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20  >pKeyInfo;.     
1df6f 20 75 2e 61 7a 2e 72 2e 6e 46 69 65 6c 64 20 3d   u.az.r.nField =
1df70 20 28 75 31 36 29 75 2e 61 7a 2e 6e 46 69 65 6c   (u16)u.az.nFiel
1df71 64 3b 0a 20 20 20 20 20 20 69 66 28 20 75 2e 61  d;.      if( u.a
1df72 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 74 20  z.oc==OP_SeekGt 
1df73 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53  || u.az.oc==OP_S
1df74 65 65 6b 4c 65 20 29 7b 0a 20 20 20 20 20 20 20  eekLe ){.       
1df75 20 75 2e 61 7a 2e 72 2e 66 6c 61 67 73 20 3d 20   u.az.r.flags = 
1df76 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59  UNPACKED_INCRKEY
1df77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1df78 20 20 20 20 20 20 20 75 2e 61 7a 2e 72 2e 66 6c         u.az.r.fl
1df79 61 67 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  ags = 0;.      }
1df7a 0a 20 20 20 20 20 20 75 2e 61 7a 2e 72 2e 61 4d  .      u.az.r.aM
1df7b 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  em = &p->aMem[pO
1df7c 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 72 63  p->p3];.      rc
1df7d 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d   = sqlite3BtreeM
1df7e 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 75 2e  ovetoUnpacked(u.
1df7f 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20  az.pC->pCursor, 
1df80 26 75 2e 61 7a 2e 72 2c 20 30 2c 20 30 2c 20 26  &u.az.r, 0, 0, &
1df81 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20  u.az.res);.     
1df82 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1df83 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  OK ){.        go
1df84 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
1df85 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20  error;.      }. 
1df86 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f       u.az.pC->ro
1df87 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a  widIsValid = 0;.
1df88 20 20 20 20 7d 0a 20 20 20 20 75 2e 61 7a 2e 70      }.    u.az.p
1df89 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
1df8a 6f 20 3d 20 30 3b 0a 20 20 20 20 75 2e 61 7a 2e  o = 0;.    u.az.
1df8b 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20  pC->cacheStatus 
1df8c 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 23  = CACHE_STALE;.#
1df8d 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
1df8e 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65  T.    sqlite3_se
1df8f 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65  arch_count++;.#e
1df90 6e 64 69 66 0a 20 20 20 20 69 66 28 20 75 2e 61  ndif.    if( u.a
1df91 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b 47 65 20  z.oc==OP_SeekGe 
1df92 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53  || u.az.oc==OP_S
1df93 65 65 6b 47 74 20 29 7b 0a 20 20 20 20 20 20 69  eekGt ){.      i
1df94 66 28 20 75 2e 61 7a 2e 72 65 73 3c 30 20 7c 7c  f( u.az.res<0 ||
1df95 20 28 75 2e 61 7a 2e 72 65 73 3d 3d 30 20 26 26   (u.az.res==0 &&
1df96 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65   u.az.oc==OP_See
1df97 6b 47 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  kGt) ){.        
1df98 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1df99 65 4e 65 78 74 28 75 2e 61 7a 2e 70 43 2d 3e 70  eNext(u.az.pC->p
1df9a 43 75 72 73 6f 72 2c 20 26 75 2e 61 7a 2e 72 65  Cursor, &u.az.re
1df9b 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  s);.        if( 
1df9c 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
1df9d 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1df9e 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20  o_error;.       
1df9f 20 75 2e 61 7a 2e 70 43 2d 3e 72 6f 77 69 64 49   u.az.pC->rowidI
1dfa0 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20  sValid = 0;.    
1dfa1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1dfa2 20 75 2e 61 7a 2e 72 65 73 20 3d 20 30 3b 0a 20   u.az.res = 0;. 
1dfa3 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
1dfa4 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1dfa5 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f 53 65 65 6b  u.az.oc==OP_Seek
1dfa6 4c 74 20 7c 7c 20 75 2e 61 7a 2e 6f 63 3d 3d 4f  Lt || u.az.oc==O
1dfa7 50 5f 53 65 65 6b 4c 65 20 29 3b 0a 20 20 20 20  P_SeekLe );.    
1dfa8 20 20 69 66 28 20 75 2e 61 7a 2e 72 65 73 3e 30    if( u.az.res>0
1dfa9 20 7c 7c 20 28 75 2e 61 7a 2e 72 65 73 3d 3d 30   || (u.az.res==0
1dfaa 20 26 26 20 75 2e 61 7a 2e 6f 63 3d 3d 4f 50 5f   && u.az.oc==OP_
1dfab 53 65 65 6b 4c 74 29 20 29 7b 0a 20 20 20 20 20  SeekLt) ){.     
1dfac 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1dfad 74 72 65 65 50 72 65 76 69 6f 75 73 28 75 2e 61  treePrevious(u.a
1dfae 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26  z.pC->pCursor, &
1dfaf 75 2e 61 7a 2e 72 65 73 29 3b 0a 20 20 20 20 20  u.az.res);.     
1dfb0 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
1dfb1 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72  E_OK ) goto abor
1dfb2 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1dfb3 20 20 20 20 20 20 20 20 75 2e 61 7a 2e 70 43 2d          u.az.pC-
1dfb4 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20  >rowidIsValid = 
1dfb5 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  0;.      }else{.
1dfb6 20 20 20 20 20 20 20 20 2f 2a 20 75 2e 61 7a 2e          /* u.az.
1dfb7 72 65 73 20 6d 69 67 68 74 20 62 65 20 6e 65 67  res might be neg
1dfb8 61 74 69 76 65 20 62 65 63 61 75 73 65 20 74 68  ative because th
1dfb9 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74 79  e table is empty
1dfba 2e 20 20 43 68 65 63 6b 20 74 6f 0a 20 20 20 20  .  Check to.    
1dfbb 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20 74 68      ** see if th
1dfbc 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a  is is the case..
1dfbd 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
1dfbe 20 20 20 75 2e 61 7a 2e 72 65 73 20 3d 20 73 71     u.az.res = sq
1dfbf 6c 69 74 65 33 42 74 72 65 65 45 6f 66 28 75 2e  lite3BtreeEof(u.
1dfc0 61 7a 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b  az.pC->pCursor);
1dfc1 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1dfc2 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
1dfc3 70 32 3e 30 20 29 3b 0a 20 20 20 20 69 66 28 20  p2>0 );.    if( 
1dfc4 75 2e 61 7a 2e 72 65 73 20 29 7b 0a 20 20 20 20  u.az.res ){.    
1dfc5 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1dfc6 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73   1;.    }.  }els
1dfc7 65 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68  e{.    /* This h
1dfc8 61 70 70 65 6e 73 20 77 68 65 6e 20 61 74 74 65  appens when atte
1dfc9 6d 70 74 69 6e 67 20 74 6f 20 6f 70 65 6e 20 74  mpting to open t
1dfca 68 65 20 73 71 6c 69 74 65 33 5f 6d 61 73 74 65  he sqlite3_maste
1dfcb 72 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 66  r table.    ** f
1dfcc 6f 72 20 72 65 61 64 20 61 63 63 65 73 73 20 72  or read access r
1dfcd 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 45 4d  eturns SQLITE_EM
1dfce 50 54 59 2e 20 49 6e 20 74 68 69 73 20 63 61 73  PTY. In this cas
1dfcf 65 20 61 6c 77 61 79 73 0a 20 20 20 20 2a 2a 20  e always.    ** 
1dfd0 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 28 73  take the jump (s
1dfd1 69 6e 63 65 20 74 68 65 72 65 20 61 72 65 20 6e  ince there are n
1dfd2 6f 20 72 65 63 6f 72 64 73 20 69 6e 20 74 68 65  o records in the
1dfd3 20 74 61 62 6c 65 29 2e 0a 20 20 20 20 2a 2f 0a   table)..    */.
1dfd4 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
1dfd5 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61   - 1;.  }.  brea
1dfd6 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1dfd7 20 53 65 65 6b 20 50 31 20 50 32 20 2a 20 2a 20   Seek P1 P2 * * 
1dfd8 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 61 6e  *.**.** P1 is an
1dfd9 20 6f 70 65 6e 20 74 61 62 6c 65 20 63 75 72 73   open table curs
1dfda 6f 72 20 61 6e 64 20 50 32 20 69 73 20 61 20 72  or and P2 is a r
1dfdb 6f 77 69 64 20 69 6e 74 65 67 65 72 2e 20 20 41  owid integer.  A
1dfdc 72 72 61 6e 67 65 0a 2a 2a 20 66 6f 72 20 50 31  rrange.** for P1
1dfdd 20 74 6f 20 6d 6f 76 65 20 73 6f 20 74 68 61 74   to move so that
1dfde 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74 68   it points to th
1dfdf 65 20 72 6f 77 69 64 20 67 69 76 65 6e 20 62 79  e rowid given by
1dfe0 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20   P2..**.** This 
1dfe1 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20 64 65  is actually a de
1dfe2 66 65 72 72 65 64 20 73 65 65 6b 2e 20 20 4e 6f  ferred seek.  No
1dfe3 74 68 69 6e 67 20 61 63 74 75 61 6c 6c 79 20 68  thing actually h
1dfe4 61 70 70 65 6e 73 20 75 6e 74 69 6c 0a 2a 2a 20  appens until.** 
1dfe5 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 75 73  the cursor is us
1dfe6 65 64 20 74 6f 20 72 65 61 64 20 61 20 72 65 63  ed to read a rec
1dfe7 6f 72 64 2e 20 20 54 68 61 74 20 77 61 79 2c 20  ord.  That way, 
1dfe8 69 66 20 6e 6f 20 72 65 61 64 73 0a 2a 2a 20 6f  if no reads.** o
1dfe9 63 63 75 72 2c 20 6e 6f 20 75 6e 6e 65 63 65 73  ccur, no unneces
1dfea 73 61 72 79 20 49 2f 4f 20 68 61 70 70 65 6e 73  sary I/O happens
1dfeb 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 65  ..*/.case OP_See
1dfec 6b 3a 20 7b 20 20 20 20 2f 2a 20 69 6e 32 20 2a  k: {    /* in2 *
1dfed 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
1dfee 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1dfef 64 20 69 6e 74 6f 20 75 2e 62 61 20 2a 2f 0a 20  d into u.ba */. 
1dff0 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1dff1 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1dff2 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1dff3 20 69 6e 74 6f 20 75 2e 62 61 20 2a 2f 0a 0a 20   into u.ba */.. 
1dff4 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
1dff5 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70  >=0 && pOp->p1<p
1dff6 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75  ->nCursor );.  u
1dff7 2e 62 61 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73  .ba.pC = p->apCs
1dff8 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73  r[pOp->p1];.  as
1dff9 73 65 72 74 28 20 75 2e 62 61 2e 70 43 21 3d 30  sert( u.ba.pC!=0
1dffa 20 29 3b 0a 20 20 69 66 28 20 41 4c 57 41 59 53   );.  if( ALWAYS
1dffb 28 75 2e 62 61 2e 70 43 2d 3e 70 43 75 72 73 6f  (u.ba.pC->pCurso
1dffc 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 61 73 73  r!=0) ){.    ass
1dffd 65 72 74 28 20 75 2e 62 61 2e 70 43 2d 3e 69 73  ert( u.ba.pC->is
1dffe 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 75 2e 62  Table );.    u.b
1dfff 61 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20  a.pC->nullRow = 
1e000 30 3b 0a 20 20 20 20 75 2e 62 61 2e 70 43 2d 3e  0;.    u.ba.pC->
1e001 6d 6f 76 65 74 6f 54 61 72 67 65 74 20 3d 20 73  movetoTarget = s
1e002 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61 6c  qlite3VdbeIntVal
1e003 75 65 28 70 49 6e 32 29 3b 0a 20 20 20 20 75 2e  ue(pIn2);.    u.
1e004 62 61 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61  ba.pC->rowidIsVa
1e005 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62  lid = 0;.    u.b
1e006 61 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f  a.pC->deferredMo
1e007 76 65 74 6f 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  veto = 1;.  }.  
1e008 62 72 65 61 6b 3b 0a 7d 0a 20 20 0a 0a 2f 2a 20  break;.}.  ../* 
1e009 4f 70 63 6f 64 65 3a 20 46 6f 75 6e 64 20 50 31  Opcode: Found P1
1e00a 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a   P2 P3 * *.**.**
1e00b 20 52 65 67 69 73 74 65 72 20 50 33 20 68 6f 6c   Register P3 hol
1e00c 64 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72  ds a blob constr
1e00d 75 63 74 65 64 20 62 79 20 4d 61 6b 65 52 65 63  ucted by MakeRec
1e00e 6f 72 64 2e 20 20 50 31 20 69 73 20 61 6e 20 69  ord.  P1 is an i
1e00f 6e 64 65 78 2e 0a 2a 2a 20 49 66 20 61 6e 20 65  ndex..** If an e
1e010 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 65  ntry that matche
1e011 73 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  s the value in r
1e012 65 67 69 73 74 65 72 20 70 33 20 65 78 69 73 74  egister p3 exist
1e013 73 20 69 6e 20 50 31 20 74 68 65 6e 0a 2a 2a 20  s in P1 then.** 
1e014 6a 75 6d 70 20 74 6f 20 50 32 2e 20 20 49 66 20  jump to P2.  If 
1e015 74 68 65 20 50 33 20 76 61 6c 75 65 20 64 6f 65  the P3 value doe
1e016 73 20 6e 6f 74 20 6d 61 74 63 68 20 61 6e 79 20  s not match any 
1e017 65 6e 74 72 79 20 69 6e 20 50 31 0a 2a 2a 20 74  entry in P1.** t
1e018 68 65 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20 20  hen fall thru.  
1e019 54 68 65 20 50 31 20 63 75 72 73 6f 72 20 69 73  The P1 cursor is
1e01a 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61   left pointing a
1e01b 74 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 65  t the matching e
1e01c 6e 74 72 79 0a 2a 2a 20 69 66 20 69 74 20 65 78  ntry.** if it ex
1e01d 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ists..**.** This
1e01e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20   instruction is 
1e01f 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  used to implemen
1e020 74 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f  t the IN operato
1e021 72 20 77 68 65 72 65 20 74 68 65 0a 2a 2a 20 6c  r where the.** l
1e022 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 69 73  eft-hand side is
1e023 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
1e024 65 6e 74 2e 20 20 50 31 20 6d 61 79 20 62 65 20  ent.  P1 may be 
1e025 61 20 74 72 75 65 20 69 6e 64 65 78 2c 20 6f 72  a true index, or
1e026 20 69 74 0a 2a 2a 20 6d 61 79 20 62 65 20 61 20   it.** may be a 
1e027 74 65 6d 70 6f 72 61 72 79 20 69 6e 64 65 78 20  temporary index 
1e028 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 72  that holds the r
1e029 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 53 45  esults of the SE
1e02a 4c 45 43 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  LECT.** statemen
1e02b 74 2e 20 20 20 54 68 69 73 20 69 6e 73 74 72 75  t.   This instru
1e02c 63 74 69 6f 6e 20 69 73 20 61 6c 73 6f 20 75 73  ction is also us
1e02d 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
1e02e 74 68 65 0a 2a 2a 20 44 49 53 54 49 4e 43 54 20  the.** DISTINCT 
1e02f 6b 65 79 77 6f 72 64 20 69 6e 20 53 45 4c 45 43  keyword in SELEC
1e030 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a  T statements..**
1e031 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63  .** This instruc
1e032 74 69 6f 6e 20 63 68 65 63 6b 73 20 69 66 20 69  tion checks if i
1e033 6e 64 65 78 20 50 31 20 63 6f 6e 74 61 69 6e 73  ndex P1 contains
1e034 20 61 20 72 65 63 6f 72 64 20 66 6f 72 20 77 68   a record for wh
1e035 69 63 68 20 0a 2a 2a 20 74 68 65 20 66 69 72 73  ich .** the firs
1e036 74 20 4e 20 73 65 72 69 61 6c 69 7a 65 64 20 76  t N serialized v
1e037 61 6c 75 65 73 20 65 78 61 63 74 6c 79 20 6d 61  alues exactly ma
1e038 74 63 68 20 74 68 65 20 4e 20 73 65 72 69 61 6c  tch the N serial
1e039 69 7a 65 64 20 76 61 6c 75 65 73 0a 2a 2a 20 69  ized values.** i
1e03a 6e 20 74 68 65 20 72 65 63 6f 72 64 20 69 6e 20  n the record in 
1e03b 72 65 67 69 73 74 65 72 20 50 33 2c 20 77 68 65  register P3, whe
1e03c 72 65 20 4e 20 69 73 20 74 68 65 20 74 6f 74 61  re N is the tota
1e03d 6c 20 6e 75 6d 62 65 72 20 6f 66 20 76 61 6c 75  l number of valu
1e03e 65 73 20 69 6e 0a 2a 2a 20 74 68 65 20 50 33 20  es in.** the P3 
1e03f 72 65 63 6f 72 64 20 28 74 68 65 20 50 33 20 72  record (the P3 r
1e040 65 63 6f 72 64 20 69 73 20 61 20 70 72 65 66 69  ecord is a prefi
1e041 78 20 6f 66 20 74 68 65 20 50 31 20 72 65 63 6f  x of the P1 reco
1e042 72 64 29 2e 20 0a 2a 2a 0a 2a 2a 20 53 65 65 20  rd). .**.** See 
1e043 61 6c 73 6f 3a 20 4e 6f 74 46 6f 75 6e 64 2c 20  also: NotFound, 
1e044 49 73 55 6e 69 71 75 65 2c 20 4e 6f 74 45 78 69  IsUnique, NotExi
1e045 73 74 73 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65  sts.*/./* Opcode
1e046 3a 20 4e 6f 74 46 6f 75 6e 64 20 50 31 20 50 32  : NotFound P1 P2
1e047 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65   P3 * *.**.** Re
1e048 67 69 73 74 65 72 20 50 33 20 68 6f 6c 64 73 20  gister P3 holds 
1e049 61 20 62 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74  a blob construct
1e04a 65 64 20 62 79 20 4d 61 6b 65 52 65 63 6f 72 64  ed by MakeRecord
1e04b 2e 20 20 50 31 20 69 73 0a 2a 2a 20 61 6e 20 69  .  P1 is.** an i
1e04c 6e 64 65 78 2e 20 20 49 66 20 6e 6f 20 65 6e 74  ndex.  If no ent
1e04d 72 79 20 65 78 69 73 74 73 20 69 6e 20 50 31 20  ry exists in P1 
1e04e 74 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65  that matches the
1e04f 20 62 6c 6f 62 20 74 68 65 6e 20 6a 75 6d 70 0a   blob then jump.
1e050 2a 2a 20 74 6f 20 50 32 2e 20 20 49 66 20 61 6e  ** to P2.  If an
1e051 20 65 6e 74 72 79 20 64 6f 65 73 20 65 78 69 73   entry does exis
1e052 74 69 6e 67 2c 20 66 61 6c 6c 20 74 68 72 6f 75  ting, fall throu
1e053 67 68 2e 20 20 54 68 65 20 63 75 72 73 6f 72 20  gh.  The cursor 
1e054 69 73 20 6c 65 66 74 0a 2a 2a 20 70 6f 69 6e 74  is left.** point
1e055 69 6e 67 20 74 6f 20 74 68 65 20 65 6e 74 72 79  ing to the entry
1e056 20 74 68 61 74 20 6d 61 74 63 68 65 73 2e 0a 2a   that matches..*
1e057 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 46  *.** See also: F
1e058 6f 75 6e 64 2c 20 4e 6f 74 45 78 69 73 74 73 2c  ound, NotExists,
1e059 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73   IsUnique.*/.cas
1e05a 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 3a 20 20  e OP_NotFound:  
1e05b 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e       /* jump, in
1e05c 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 6f 75  3 */.case OP_Fou
1e05d 6e 64 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20  nd: {        /* 
1e05e 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23 69 66  jump, in3 */.#if
1e05f 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1e060 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e061 6f 20 75 2e 62 62 20 2a 2f 0a 20 20 69 6e 74 20  o u.bb */.  int 
1e062 61 6c 72 65 61 64 79 45 78 69 73 74 73 3b 0a 20  alreadyExists;. 
1e063 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1e064 0a 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 55 6e  .  int res;.  Un
1e065 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 49  packedRecord *pI
1e066 64 78 4b 65 79 3b 0a 20 20 63 68 61 72 20 61 54  dxKey;.  char aT
1e067 65 6d 70 52 65 63 5b 52 4f 55 4e 44 38 28 73 69  empRec[ROUND8(si
1e068 7a 65 6f 66 28 55 6e 70 61 63 6b 65 64 52 65 63  zeof(UnpackedRec
1e069 6f 72 64 29 29 20 2b 20 73 69 7a 65 6f 66 28 4d  ord)) + sizeof(M
1e06a 65 6d 29 2a 33 20 2b 20 37 5d 3b 0a 23 65 6e 64  em)*3 + 7];.#end
1e06b 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1e06c 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e06d 20 75 2e 62 62 20 2a 2f 0a 0a 23 69 66 64 65 66   u.bb */..#ifdef
1e06e 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73   SQLITE_TEST.  s
1e06f 71 6c 69 74 65 33 5f 66 6f 75 6e 64 5f 63 6f 75  qlite3_found_cou
1e070 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  nt++;.#endif..  
1e071 75 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69 73  u.bb.alreadyExis
1e072 74 73 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  ts = 0;.  assert
1e073 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
1e074 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73  pOp->p1<p->nCurs
1e075 6f 72 20 29 3b 0a 20 20 75 2e 62 62 2e 70 43 20  or );.  u.bb.pC 
1e076 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e  = p->apCsr[pOp->
1e077 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75  p1];.  assert( u
1e078 2e 62 62 2e 70 43 21 3d 30 20 29 3b 0a 20 20 69  .bb.pC!=0 );.  i
1e079 66 28 20 41 4c 57 41 59 53 28 75 2e 62 62 2e 70  f( ALWAYS(u.bb.p
1e07a 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 29 20 29  C->pCursor!=0) )
1e07b 7b 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 75  {..    assert( u
1e07c 2e 62 62 2e 70 43 2d 3e 69 73 54 61 62 6c 65 3d  .bb.pC->isTable=
1e07d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
1e07e 28 20 70 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20  ( pIn3->flags & 
1e07f 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20  MEM_Blob );.    
1e080 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 33 29  ExpandBlob(pIn3)
1e081 3b 0a 20 20 20 20 75 2e 62 62 2e 70 49 64 78 4b  ;.    u.bb.pIdxK
1e082 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  ey = sqlite3Vdbe
1e083 52 65 63 6f 72 64 55 6e 70 61 63 6b 28 75 2e 62  RecordUnpack(u.b
1e084 62 2e 70 43 2d 3e 70 4b 65 79 49 6e 66 6f 2c 20  b.pC->pKeyInfo, 
1e085 70 49 6e 33 2d 3e 6e 2c 20 70 49 6e 33 2d 3e 7a  pIn3->n, pIn3->z
1e086 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1e087 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e088 20 20 20 20 20 20 20 20 75 2e 62 62 2e 61 54 65          u.bb.aTe
1e089 6d 70 52 65 63 2c 20 73 69 7a 65 6f 66 28 75 2e  mpRec, sizeof(u.
1e08a 62 62 2e 61 54 65 6d 70 52 65 63 29 29 3b 0a 20  bb.aTempRec));. 
1e08b 20 20 20 69 66 28 20 75 2e 62 62 2e 70 49 64 78     if( u.bb.pIdx
1e08c 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Key==0 ){.      
1e08d 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20  goto no_mem;.   
1e08e 20 7d 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e   }.    if( pOp->
1e08f 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6f 75 6e 64  opcode==OP_Found
1e090 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 62 2e 70   ){.      u.bb.p
1e091 49 64 78 4b 65 79 2d 3e 66 6c 61 67 73 20 7c 3d  IdxKey->flags |=
1e092 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46 49 58   UNPACKED_PREFIX
1e093 5f 4d 41 54 43 48 3b 0a 20 20 20 20 7d 0a 20 20  _MATCH;.    }.  
1e094 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1e095 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65  reeMovetoUnpacke
1e096 64 28 75 2e 62 62 2e 70 43 2d 3e 70 43 75 72 73  d(u.bb.pC->pCurs
1e097 6f 72 2c 20 75 2e 62 62 2e 70 49 64 78 4b 65 79  or, u.bb.pIdxKey
1e098 2c 20 30 2c 20 30 2c 20 26 75 2e 62 62 2e 72 65  , 0, 0, &u.bb.re
1e099 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  s);.    sqlite3V
1e09a 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65  dbeDeleteUnpacke
1e09b 64 52 65 63 6f 72 64 28 75 2e 62 62 2e 70 49 64  dRecord(u.bb.pId
1e09c 78 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20 72  xKey);.    if( r
1e09d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1e09e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1e09f 20 7d 0a 20 20 20 20 75 2e 62 62 2e 61 6c 72 65   }.    u.bb.alre
1e0a0 61 64 79 45 78 69 73 74 73 20 3d 20 28 75 2e 62  adyExists = (u.b
1e0a1 62 2e 72 65 73 3d 3d 30 29 3b 0a 20 20 20 20 75  b.res==0);.    u
1e0a2 2e 62 62 2e 70 43 2d 3e 64 65 66 65 72 72 65 64  .bb.pC->deferred
1e0a3 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20  Moveto = 0;.    
1e0a4 75 2e 62 62 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.bb.pC->cacheSt
1e0a5 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41  atus = CACHE_STA
1e0a6 4c 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f  LE;.  }.  if( pO
1e0a7 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6f  p->opcode==OP_Fo
1e0a8 75 6e 64 20 29 7b 0a 20 20 20 20 69 66 28 20 75  und ){.    if( u
1e0a9 2e 62 62 2e 61 6c 72 65 61 64 79 45 78 69 73 74  .bb.alreadyExist
1e0aa 73 20 29 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32  s ) pc = pOp->p2
1e0ab 20 2d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   - 1;.  }else{. 
1e0ac 20 20 20 69 66 28 20 21 75 2e 62 62 2e 61 6c 72     if( !u.bb.alr
1e0ad 65 61 64 79 45 78 69 73 74 73 20 29 20 70 63 20  eadyExists ) pc 
1e0ae 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1e0af 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1e0b0 2a 20 4f 70 63 6f 64 65 3a 20 49 73 55 6e 69 71  * Opcode: IsUniq
1e0b1 75 65 20 50 31 20 50 32 20 50 33 20 50 34 20 2a  ue P1 P2 P3 P4 *
1e0b2 0a 2a 2a 0a 2a 2a 20 43 75 72 73 6f 72 20 50 31  .**.** Cursor P1
1e0b3 20 69 73 20 6f 70 65 6e 20 6f 6e 20 61 6e 20 69   is open on an i
1e0b4 6e 64 65 78 2e 20 20 53 6f 20 69 74 20 68 61 73  ndex.  So it has
1e0b5 20 6e 6f 20 64 61 74 61 20 61 6e 64 20 69 74 73   no data and its
1e0b6 20 6b 65 79 20 63 6f 6e 73 69 73 74 73 20 0a 2a   key consists .*
1e0b7 2a 20 6f 66 20 61 20 72 65 63 6f 72 64 20 67 65  * of a record ge
1e0b8 6e 65 72 61 74 65 64 20 62 79 20 4f 50 5f 4d 61  nerated by OP_Ma
1e0b9 6b 65 52 65 63 6f 72 64 20 77 68 65 72 65 20 74  keRecord where t
1e0ba 68 65 20 6c 61 73 74 20 66 69 65 6c 64 20 69 73  he last field is
1e0bb 20 74 68 65 20 0a 2a 2a 20 72 6f 77 69 64 20 6f   the .** rowid o
1e0bc 66 20 74 68 65 20 65 6e 74 72 79 20 74 68 61 74  f the entry that
1e0bd 20 74 68 65 20 69 6e 64 65 78 20 72 65 66 65 72   the index refer
1e0be 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  s to..**.** The 
1e0bf 50 33 20 72 65 67 69 73 74 65 72 20 63 6f 6e 74  P3 register cont
1e0c0 61 69 6e 73 20 61 6e 20 69 6e 74 65 67 65 72 20  ains an integer 
1e0c1 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20 43  record number. C
1e0c2 61 6c 6c 20 74 68 69 73 20 72 65 63 6f 72 64 20  all this record 
1e0c3 0a 2a 2a 20 6e 75 6d 62 65 72 20 52 2e 20 52 65  .** number R. Re
1e0c4 67 69 73 74 65 72 20 50 34 20 69 73 20 74 68 65  gister P4 is the
1e0c5 20 66 69 72 73 74 20 69 6e 20 61 20 73 65 74 20   first in a set 
1e0c6 6f 66 20 4e 20 63 6f 6e 74 69 67 75 6f 75 73 20  of N contiguous 
1e0c7 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 74 68 61  registers.** tha
1e0c8 74 20 6d 61 6b 65 20 75 70 20 61 6e 20 75 6e 70  t make up an unp
1e0c9 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 20  acked index key 
1e0ca 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 64  that can be used
1e0cb 20 77 69 74 68 20 63 75 72 73 6f 72 20 50 31 2e   with cursor P1.
1e0cc 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66  .** The value of
1e0cd 20 4e 20 63 61 6e 20 62 65 20 69 6e 66 65 72 72   N can be inferr
1e0ce 65 64 20 66 72 6f 6d 20 74 68 65 20 63 75 72 73  ed from the curs
1e0cf 6f 72 2e 20 4e 20 69 6e 63 6c 75 64 65 73 20 74  or. N includes t
1e0d0 68 65 20 72 6f 77 69 64 0a 2a 2a 20 76 61 6c 75  he rowid.** valu
1e0d1 65 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68  e appended to th
1e0d2 65 20 65 6e 64 20 6f 66 20 74 68 65 20 69 6e 64  e end of the ind
1e0d3 65 78 20 72 65 63 6f 72 64 2e 20 54 68 69 73 20  ex record. This 
1e0d4 72 6f 77 69 64 20 76 61 6c 75 65 20 6d 61 79 0a  rowid value may.
1e0d5 2a 2a 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65  ** or may not be
1e0d6 20 74 68 65 20 73 61 6d 65 20 61 73 20 52 2e 0a   the same as R..
1e0d7 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20  **.** If any of 
1e0d8 74 68 65 20 4e 20 72 65 67 69 73 74 65 72 73 20  the N registers 
1e0d9 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 72  beginning with r
1e0da 65 67 69 73 74 65 72 20 50 34 20 63 6f 6e 74 61  egister P4 conta
1e0db 69 6e 73 20 61 20 4e 55 4c 4c 0a 2a 2a 20 76 61  ins a NULL.** va
1e0dc 6c 75 65 2c 20 6a 75 6d 70 20 69 6d 6d 65 64 69  lue, jump immedi
1e0dd 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a  ately to P2..**.
1e0de 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68  ** Otherwise, th
1e0df 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 63  is instruction c
1e0e0 68 65 63 6b 73 20 69 66 20 63 75 72 73 6f 72 20  hecks if cursor 
1e0e1 50 31 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65  P1 contains an e
1e0e2 6e 74 72 79 0a 2a 2a 20 77 68 65 72 65 20 74 68  ntry.** where th
1e0e3 65 20 66 69 72 73 74 20 28 4e 2d 31 29 20 66 69  e first (N-1) fi
1e0e4 65 6c 64 73 20 6d 61 74 63 68 20 62 75 74 20 74  elds match but t
1e0e5 68 65 20 72 6f 77 69 64 20 76 61 6c 75 65 20 61  he rowid value a
1e0e6 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f 66 20  t the end.** of 
1e0e7 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72 79 20  the index entry 
1e0e8 69 73 20 6e 6f 74 20 52 2e 20 49 66 20 74 68 65  is not R. If the
1e0e9 72 65 20 69 73 20 6e 6f 20 73 75 63 68 20 65 6e  re is no such en
1e0ea 74 72 79 2c 20 63 6f 6e 74 72 6f 6c 20 6a 75 6d  try, control jum
1e0eb 70 73 0a 2a 2a 20 74 6f 20 69 6e 73 74 72 75 63  ps.** to instruc
1e0ec 74 69 6f 6e 20 50 32 2e 20 4f 74 68 65 72 77 69  tion P2. Otherwi
1e0ed 73 65 2c 20 74 68 65 20 72 6f 77 69 64 20 6f 66  se, the rowid of
1e0ee 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 69 6e 67   the conflicting
1e0ef 20 69 6e 64 65 78 0a 2a 2a 20 65 6e 74 72 79 20   index.** entry 
1e0f0 69 73 20 63 6f 70 69 65 64 20 74 6f 20 72 65 67  is copied to reg
1e0f1 69 73 74 65 72 20 50 33 20 61 6e 64 20 63 6f 6e  ister P3 and con
1e0f2 74 72 6f 6c 20 66 61 6c 6c 73 20 74 68 72 6f 75  trol falls throu
1e0f3 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 0a 2a  gh to the next.*
1e0f4 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a  * instruction..*
1e0f5 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 4e  *.** See also: N
1e0f6 6f 74 46 6f 75 6e 64 2c 20 4e 6f 74 45 78 69 73  otFound, NotExis
1e0f7 74 73 2c 20 46 6f 75 6e 64 0a 2a 2f 0a 63 61 73  ts, Found.*/.cas
1e0f8 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 3a 20 7b  e OP_IsUnique: {
1e0f9 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c          /* jump,
1e0fa 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f   in3 */.#if 0  /
1e0fb 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1e0fc 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1e0fd 63 20 2a 2f 0a 20 20 75 31 36 20 69 69 3b 0a 20  c */.  u16 ii;. 
1e0fe 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78   VdbeCursor *pCx
1e0ff 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  ;.  BtCursor *pC
1e100 72 73 72 3b 0a 20 20 75 31 36 20 6e 46 69 65 6c  rsr;.  u16 nFiel
1e101 64 3b 0a 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 0a  d;.  Mem *aMem;.
1e102 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64    UnpackedRecord
1e103 20 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   r;             
1e104 20 20 20 20 20 2f 2a 20 42 2d 54 72 65 65 20 69       /* B-Tree i
1e105 6e 64 65 78 20 73 65 61 72 63 68 20 6b 65 79 20  ndex search key 
1e106 2a 2f 0a 20 20 69 36 34 20 52 3b 20 20 20 20 20  */.  i64 R;     
1e107 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e108 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69 64          /* Rowid
1e109 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
1e10a 74 65 72 20 50 33 20 2a 2f 0a 23 65 6e 64 69 66  ter P3 */.#endif
1e10b 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1e10c 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1e10d 2e 62 63 20 2a 2f 0a 0a 20 20 75 2e 62 63 2e 61  .bc */..  u.bc.a
1e10e 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Mem = &p->aMem[p
1e10f 4f 70 2d 3e 70 34 2e 69 5d 3b 0a 20 20 2f 2a 20  Op->p4.i];.  /* 
1e110 41 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20  Assert that the 
1e111 76 61 6c 75 65 73 20 6f 66 20 70 61 72 61 6d 65  values of parame
1e112 74 65 72 73 20 50 31 20 61 6e 64 20 50 34 20 61  ters P1 and P4 a
1e113 72 65 20 69 6e 20 72 61 6e 67 65 2e 20 2a 2f 0a  re in range. */.
1e114 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1e115 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20  4type==P4_INT32 
1e116 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
1e117 2d 3e 70 34 2e 69 3e 30 20 26 26 20 70 4f 70 2d  ->p4.i>0 && pOp-
1e118 3e 70 34 2e 69 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  >p4.i<=p->nMem )
1e119 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1e11a 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1>=0 && pOp->p
1e11b 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  1<p->nCursor );.
1e11c 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 69  .  /* Find the i
1e11d 6e 64 65 78 20 63 75 72 73 6f 72 2e 20 2a 2f 0a  ndex cursor. */.
1e11e 20 20 75 2e 62 63 2e 70 43 78 20 3d 20 70 2d 3e    u.bc.pCx = p->
1e11f 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  apCsr[pOp->p1];.
1e120 20 20 61 73 73 65 72 74 28 20 75 2e 62 63 2e 70    assert( u.bc.p
1e121 43 78 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  Cx->deferredMove
1e122 74 6f 3d 3d 30 20 29 3b 0a 20 20 75 2e 62 63 2e  to==0 );.  u.bc.
1e123 70 43 78 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20  pCx->seekResult 
1e124 3d 20 30 3b 0a 20 20 75 2e 62 63 2e 70 43 78 2d  = 0;.  u.bc.pCx-
1e125 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43  >cacheStatus = C
1e126 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 75 2e  ACHE_STALE;.  u.
1e127 62 63 2e 70 43 72 73 72 20 3d 20 75 2e 62 63 2e  bc.pCrsr = u.bc.
1e128 70 43 78 2d 3e 70 43 75 72 73 6f 72 3b 0a 0a 20  pCx->pCursor;.. 
1e129 20 2f 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68   /* If any of th
1e12a 65 20 76 61 6c 75 65 73 20 61 72 65 20 4e 55 4c  e values are NUL
1e12b 4c 2c 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70  L, take the jump
1e12c 2e 20 2a 2f 0a 20 20 75 2e 62 63 2e 6e 46 69 65  . */.  u.bc.nFie
1e12d 6c 64 20 3d 20 75 2e 62 63 2e 70 43 78 2d 3e 70  ld = u.bc.pCx->p
1e12e 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 3b  KeyInfo->nField;
1e12f 0a 20 20 66 6f 72 28 75 2e 62 63 2e 69 69 3d 30  .  for(u.bc.ii=0
1e130 3b 20 75 2e 62 63 2e 69 69 3c 75 2e 62 63 2e 6e  ; u.bc.ii<u.bc.n
1e131 46 69 65 6c 64 3b 20 75 2e 62 63 2e 69 69 2b 2b  Field; u.bc.ii++
1e132 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 62 63 2e  ){.    if( u.bc.
1e133 61 4d 65 6d 5b 75 2e 62 63 2e 69 69 5d 2e 66 6c  aMem[u.bc.ii].fl
1e134 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29  ags & MEM_Null )
1e135 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70  {.      pc = pOp
1e136 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20  ->p2 - 1;.      
1e137 75 2e 62 63 2e 70 43 72 73 72 20 3d 20 30 3b 0a  u.bc.pCrsr = 0;.
1e138 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1e139 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28   }.  }.  assert(
1e13a 20 28 75 2e 62 63 2e 61 4d 65 6d 5b 75 2e 62 63   (u.bc.aMem[u.bc
1e13b 2e 6e 46 69 65 6c 64 5d 2e 66 6c 61 67 73 20 26  .nField].flags &
1e13c 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30 20 29 3b   MEM_Null)==0 );
1e13d 0a 0a 20 20 69 66 28 20 75 2e 62 63 2e 70 43 72  ..  if( u.bc.pCr
1e13e 73 72 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  sr!=0 ){.    /* 
1e13f 50 6f 70 75 6c 61 74 65 20 74 68 65 20 69 6e 64  Populate the ind
1e140 65 78 20 73 65 61 72 63 68 20 6b 65 79 2e 20 2a  ex search key. *
1e141 2f 0a 20 20 20 20 75 2e 62 63 2e 72 2e 70 4b 65  /.    u.bc.r.pKe
1e142 79 49 6e 66 6f 20 3d 20 75 2e 62 63 2e 70 43 78  yInfo = u.bc.pCx
1e143 2d 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20  ->pKeyInfo;.    
1e144 75 2e 62 63 2e 72 2e 6e 46 69 65 6c 64 20 3d 20  u.bc.r.nField = 
1e145 75 2e 62 63 2e 6e 46 69 65 6c 64 20 2b 20 31 3b  u.bc.nField + 1;
1e146 0a 20 20 20 20 75 2e 62 63 2e 72 2e 66 6c 61 67  .    u.bc.r.flag
1e147 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f 50 52 45  s = UNPACKED_PRE
1e148 46 49 58 5f 53 45 41 52 43 48 3b 0a 20 20 20 20  FIX_SEARCH;.    
1e149 75 2e 62 63 2e 72 2e 61 4d 65 6d 20 3d 20 75 2e  u.bc.r.aMem = u.
1e14a 62 63 2e 61 4d 65 6d 3b 0a 0a 20 20 20 20 2f 2a  bc.aMem;..    /*
1e14b 20 45 78 74 72 61 63 74 20 74 68 65 20 76 61 6c   Extract the val
1e14c 75 65 20 6f 66 20 75 2e 62 63 2e 52 20 66 72 6f  ue of u.bc.R fro
1e14d 6d 20 72 65 67 69 73 74 65 72 20 50 33 2e 20 2a  m register P3. *
1e14e 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
1e14f 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70  eMemIntegerify(p
1e150 49 6e 33 29 3b 0a 20 20 20 20 75 2e 62 63 2e 52  In3);.    u.bc.R
1e151 20 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 0a 20   = pIn3->u.i;.. 
1e152 20 20 20 2f 2a 20 53 65 61 72 63 68 20 74 68 65     /* Search the
1e153 20 42 2d 54 72 65 65 20 69 6e 64 65 78 2e 20 49   B-Tree index. I
1e154 66 20 6e 6f 20 63 6f 6e 66 6c 69 63 74 69 6e 67  f no conflicting
1e155 20 72 65 63 6f 72 64 20 69 73 20 66 6f 75 6e 64   record is found
1e156 2c 20 6a 75 6d 70 0a 20 20 20 20 2a 2a 20 74 6f  , jump.    ** to
1e157 20 50 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20   P2. Otherwise, 
1e158 63 6f 70 79 20 74 68 65 20 72 6f 77 69 64 20 6f  copy the rowid o
1e159 66 20 74 68 65 20 63 6f 6e 66 6c 69 63 74 69 6e  f the conflictin
1e15a 67 20 72 65 63 6f 72 64 20 74 6f 0a 20 20 20 20  g record to.    
1e15b 2a 2a 20 72 65 67 69 73 74 65 72 20 50 33 20 61  ** register P3 a
1e15c 6e 64 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20  nd fall through 
1e15d 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74  to the next inst
1e15e 72 75 63 74 69 6f 6e 2e 20 20 2a 2f 0a 20 20 20  ruction.  */.   
1e15f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1e160 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64  eeMovetoUnpacked
1e161 28 75 2e 62 63 2e 70 43 72 73 72 2c 20 26 75 2e  (u.bc.pCrsr, &u.
1e162 62 63 2e 72 2c 20 30 2c 20 30 2c 20 26 75 2e 62  bc.r, 0, 0, &u.b
1e163 63 2e 70 43 78 2d 3e 73 65 65 6b 52 65 73 75 6c  c.pCx->seekResul
1e164 74 29 3b 0a 20 20 20 20 69 66 28 20 28 75 2e 62  t);.    if( (u.b
1e165 63 2e 72 2e 66 6c 61 67 73 20 26 20 55 4e 50 41  c.r.flags & UNPA
1e166 43 4b 45 44 5f 50 52 45 46 49 58 5f 53 45 41 52  CKED_PREFIX_SEAR
1e167 43 48 29 20 7c 7c 20 75 2e 62 63 2e 72 2e 72 6f  CH) || u.bc.r.ro
1e168 77 69 64 3d 3d 75 2e 62 63 2e 52 20 29 7b 0a 20  wid==u.bc.R ){. 
1e169 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70       pc = pOp->p
1e16a 32 20 2d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65  2 - 1;.    }else
1e16b 7b 0a 20 20 20 20 20 20 70 49 6e 33 2d 3e 75 2e  {.      pIn3->u.
1e16c 69 20 3d 20 75 2e 62 63 2e 72 2e 72 6f 77 69 64  i = u.bc.r.rowid
1e16d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72  ;.    }.  }.  br
1e16e 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1e16f 65 3a 20 4e 6f 74 45 78 69 73 74 73 20 50 31 20  e: NotExists P1 
1e170 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  P2 P3 * *.**.** 
1e171 55 73 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  Use the content 
1e172 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20 61  of register P3 a
1e173 73 20 61 20 69 6e 74 65 67 65 72 20 6b 65 79 2e  s a integer key.
1e174 20 20 49 66 20 61 20 72 65 63 6f 72 64 20 0a 2a    If a record .*
1e175 2a 20 77 69 74 68 20 74 68 61 74 20 6b 65 79 20  * with that key 
1e176 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69  does not exist i
1e177 6e 20 74 61 62 6c 65 20 6f 66 20 50 31 2c 20 74  n table of P1, t
1e178 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20  hen jump to P2. 
1e179 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72  .** If the recor
1e17a 64 20 64 6f 65 73 20 65 78 69 73 74 2c 20 74 68  d does exist, th
1e17b 65 6e 20 66 61 6c 6c 20 74 68 72 75 2e 20 20 54  en fall thru.  T
1e17c 68 65 20 63 75 72 73 6f 72 20 69 73 20 6c 65 66  he cursor is lef
1e17d 74 20 0a 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74  t .** pointing t
1e17e 6f 20 74 68 65 20 72 65 63 6f 72 64 20 69 66 20  o the record if 
1e17f 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a  it exists..**.**
1e180 20 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20   The difference 
1e181 62 65 74 77 65 65 6e 20 74 68 69 73 20 6f 70 65  between this ope
1e182 72 61 74 69 6f 6e 20 61 6e 64 20 4e 6f 74 46 6f  ration and NotFo
1e183 75 6e 64 20 69 73 20 74 68 61 74 20 74 68 69 73  und is that this
1e184 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 61 73  .** operation as
1e185 73 75 6d 65 73 20 74 68 65 20 6b 65 79 20 69 73  sumes the key is
1e186 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 20   an integer and 
1e187 74 68 61 74 20 50 31 20 69 73 20 61 20 74 61 62  that P1 is a tab
1e188 6c 65 20 77 68 65 72 65 61 73 0a 2a 2a 20 4e 6f  le whereas.** No
1e189 74 46 6f 75 6e 64 20 61 73 73 75 6d 65 73 20 6b  tFound assumes k
1e18a 65 79 20 69 73 20 61 20 62 6c 6f 62 20 63 6f 6e  ey is a blob con
1e18b 73 74 72 75 63 74 65 64 20 66 72 6f 6d 20 4d 61  structed from Ma
1e18c 6b 65 52 65 63 6f 72 64 20 61 6e 64 0a 2a 2a 20  keRecord and.** 
1e18d 50 31 20 69 73 20 61 6e 20 69 6e 64 65 78 2e 0a  P1 is an index..
1e18e 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
1e18f 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75 6e 64 2c  Found, NotFound,
1e190 20 49 73 55 6e 69 71 75 65 0a 2a 2f 0a 63 61 73   IsUnique.*/.cas
1e191 65 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 3a 20  e OP_NotExists: 
1e192 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70  {        /* jump
1e193 2c 20 69 6e 33 20 2a 2f 0a 23 69 66 20 30 20 20  , in3 */.#if 0  
1e194 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1e195 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1e196 62 64 20 2a 2f 0a 20 20 56 64 62 65 43 75 72 73  bd */.  VdbeCurs
1e197 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73  or *pC;.  BtCurs
1e198 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74  or *pCrsr;.  int
1e199 20 72 65 73 3b 0a 20 20 75 36 34 20 69 4b 65 79   res;.  u64 iKey
1e19a 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1e19b 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e19c 64 20 69 6e 74 6f 20 75 2e 62 64 20 2a 2f 0a 0a  d into u.bd */..
1e19d 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e    assert( pIn3->
1e19e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20  flags & MEM_Int 
1e19f 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
1e1a0 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
1e1a1 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b  p1<p->nCursor );
1e1a2 0a 20 20 75 2e 62 64 2e 70 43 20 3d 20 70 2d 3e  .  u.bd.pC = p->
1e1a3 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  apCsr[pOp->p1];.
1e1a4 20 20 61 73 73 65 72 74 28 20 75 2e 62 64 2e 70    assert( u.bd.p
1e1a5 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  C!=0 );.  assert
1e1a6 28 20 75 2e 62 64 2e 70 43 2d 3e 69 73 54 61 62  ( u.bd.pC->isTab
1e1a7 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  le );.  assert( 
1e1a8 75 2e 62 64 2e 70 43 2d 3e 70 73 65 75 64 6f 54  u.bd.pC->pseudoT
1e1a9 61 62 6c 65 52 65 67 3d 3d 30 20 29 3b 0a 20 20  ableReg==0 );.  
1e1aa 75 2e 62 64 2e 70 43 72 73 72 20 3d 20 75 2e 62  u.bd.pCrsr = u.b
1e1ab 64 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20  d.pC->pCursor;. 
1e1ac 20 69 66 28 20 75 2e 62 64 2e 70 43 72 73 72 21   if( u.bd.pCrsr!
1e1ad 3d 30 20 29 7b 0a 20 20 20 20 75 2e 62 64 2e 72  =0 ){.    u.bd.r
1e1ae 65 73 20 3d 20 30 3b 0a 20 20 20 20 75 2e 62 64  es = 0;.    u.bd
1e1af 2e 69 4b 65 79 20 3d 20 70 49 6e 33 2d 3e 75 2e  .iKey = pIn3->u.
1e1b0 69 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  i;.    rc = sqli
1e1b1 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e  te3BtreeMovetoUn
1e1b2 70 61 63 6b 65 64 28 75 2e 62 64 2e 70 43 72 73  packed(u.bd.pCrs
1e1b3 72 2c 20 30 2c 20 75 2e 62 64 2e 69 4b 65 79 2c  r, 0, u.bd.iKey,
1e1b4 20 30 2c 20 26 75 2e 62 64 2e 72 65 73 29 3b 0a   0, &u.bd.res);.
1e1b5 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 6c 61 73      u.bd.pC->las
1e1b6 74 52 6f 77 69 64 20 3d 20 70 49 6e 33 2d 3e 75  tRowid = pIn3->u
1e1b7 2e 69 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d  .i;.    u.bd.pC-
1e1b8 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20  >rowidIsValid = 
1e1b9 75 2e 62 64 2e 72 65 73 3d 3d 30 20 3f 31 3a 30  u.bd.res==0 ?1:0
1e1ba 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d 3e 6e  ;.    u.bd.pC->n
1e1bb 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20  ullRow = 0;.    
1e1bc 75 2e 62 64 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.bd.pC->cacheSt
1e1bd 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41  atus = CACHE_STA
1e1be 4c 45 3b 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d  LE;.    u.bd.pC-
1e1bf 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 20  >deferredMoveto 
1e1c0 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 75 2e 62  = 0;.    if( u.b
1e1c1 64 2e 72 65 73 21 3d 30 20 29 7b 0a 20 20 20 20  d.res!=0 ){.    
1e1c2 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d    pc = pOp->p2 -
1e1c3 20 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   1;.      assert
1e1c4 28 20 75 2e 62 64 2e 70 43 2d 3e 72 6f 77 69 64  ( u.bd.pC->rowid
1e1c5 49 73 56 61 6c 69 64 3d 3d 30 20 29 3b 0a 20 20  IsValid==0 );.  
1e1c6 20 20 7d 0a 20 20 20 20 75 2e 62 64 2e 70 43 2d    }.    u.bd.pC-
1e1c7 3e 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 75 2e  >seekResult = u.
1e1c8 62 64 2e 72 65 73 3b 0a 20 20 7d 65 6c 73 65 7b  bd.res;.  }else{
1e1c9 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61 70  .    /* This hap
1e1ca 70 65 6e 73 20 77 68 65 6e 20 61 6e 20 61 74 74  pens when an att
1e1cb 65 6d 70 74 20 74 6f 20 6f 70 65 6e 20 61 20 72  empt to open a r
1e1cc 65 61 64 20 63 75 72 73 6f 72 20 6f 6e 20 74 68  ead cursor on th
1e1cd 65 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 5f  e.    ** sqlite_
1e1ce 6d 61 73 74 65 72 20 74 61 62 6c 65 20 72 65 74  master table ret
1e1cf 75 72 6e 73 20 53 51 4c 49 54 45 5f 45 4d 50 54  urns SQLITE_EMPT
1e1d0 59 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 63  Y..    */.    pc
1e1d1 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1e1d2 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 64      assert( u.bd
1e1d3 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69  .pC->rowidIsVali
1e1d4 64 3d 3d 30 20 29 3b 0a 20 20 20 20 75 2e 62 64  d==0 );.    u.bd
1e1d5 2e 70 43 2d 3e 73 65 65 6b 52 65 73 75 6c 74 20  .pC->seekResult 
1e1d6 3d 20 30 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  = 0;.  }.  break
1e1d7 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1e1d8 53 65 71 75 65 6e 63 65 20 50 31 20 50 32 20 2a  Sequence P1 P2 *
1e1d9 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 20   * *.**.** Find 
1e1da 74 68 65 20 6e 65 78 74 20 61 76 61 69 6c 61 62  the next availab
1e1db 6c 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62  le sequence numb
1e1dc 65 72 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31  er for cursor P1
1e1dd 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 73  ..** Write the s
1e1de 65 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 69  equence number i
1e1df 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e  nto register P2.
1e1e0 0a 2a 2a 20 54 68 65 20 73 65 71 75 65 6e 63 65  .** The sequence
1e1e1 20 6e 75 6d 62 65 72 20 6f 6e 20 74 68 65 20 63   number on the c
1e1e2 75 72 73 6f 72 20 69 73 20 69 6e 63 72 65 6d 65  ursor is increme
1e1e3 6e 74 65 64 20 61 66 74 65 72 20 74 68 69 73 0a  nted after this.
1e1e4 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20  ** instruction. 
1e1e5 20 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 71   .*/.case OP_Seq
1e1e6 75 65 6e 63 65 3a 20 7b 20 20 20 20 20 20 20 20  uence: {        
1e1e7 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65     /* out2-prere
1e1e8 6c 65 61 73 65 20 2a 2f 0a 20 20 61 73 73 65 72  lease */.  asser
1e1e9 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
1e1ea 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72   pOp->p1<p->nCur
1e1eb 73 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28  sor );.  assert(
1e1ec 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70   p->apCsr[pOp->p
1e1ed 31 5d 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d  1]!=0 );.  pOut-
1e1ee 3e 75 2e 69 20 3d 20 70 2d 3e 61 70 43 73 72 5b  >u.i = p->apCsr[
1e1ef 70 4f 70 2d 3e 70 31 5d 2d 3e 73 65 71 43 6f 75  pOp->p1]->seqCou
1e1f0 6e 74 2b 2b 3b 0a 20 20 4d 65 6d 53 65 74 54 79  nt++;.  MemSetTy
1e1f1 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d  peFlag(pOut, MEM
1e1f2 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  _Int);.  break;.
1e1f3 7d 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e  }.../* Opcode: N
1e1f4 65 77 52 6f 77 69 64 20 50 31 20 50 32 20 50 33  ewRowid P1 P2 P3
1e1f5 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 47 65 74 20 61   * *.**.** Get a
1e1f6 20 6e 65 77 20 69 6e 74 65 67 65 72 20 72 65 63   new integer rec
1e1f7 6f 72 64 20 6e 75 6d 62 65 72 20 28 61 2e 6b 2e  ord number (a.k.
1e1f8 61 20 22 72 6f 77 69 64 22 29 20 75 73 65 64 20  a "rowid") used 
1e1f9 61 73 20 74 68 65 20 6b 65 79 20 74 6f 20 61 20  as the key to a 
1e1fa 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 72 65  table..** The re
1e1fb 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20 6e  cord number is n
1e1fc 6f 74 20 70 72 65 76 69 6f 75 73 6c 79 20 75 73  ot previously us
1e1fd 65 64 20 61 73 20 61 20 6b 65 79 20 69 6e 20 74  ed as a key in t
1e1fe 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 74  he database.** t
1e1ff 61 62 6c 65 20 74 68 61 74 20 63 75 72 73 6f 72  able that cursor
1e200 20 50 31 20 70 6f 69 6e 74 73 20 74 6f 2e 20 20   P1 points to.  
1e201 54 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 6e  The new record n
1e202 75 6d 62 65 72 20 69 73 20 77 72 69 74 74 65 6e  umber is written
1e203 0a 2a 2a 20 77 72 69 74 74 65 6e 20 74 6f 20 72  .** written to r
1e204 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a  egister P2..**.*
1e205 2a 20 49 66 20 50 33 3e 30 20 74 68 65 6e 20 50  * If P3>0 then P
1e206 33 20 69 73 20 61 20 72 65 67 69 73 74 65 72 20  3 is a register 
1e207 69 6e 20 74 68 65 20 72 6f 6f 74 20 66 72 61 6d  in the root fram
1e208 65 20 6f 66 20 74 68 69 73 20 56 44 42 45 20 74  e of this VDBE t
1e209 68 61 74 20 68 6f 6c 64 73 20 0a 2a 2a 20 74 68  hat holds .** th
1e20a 65 20 6c 61 72 67 65 73 74 20 70 72 65 76 69 6f  e largest previo
1e20b 75 73 6c 79 20 67 65 6e 65 72 61 74 65 64 20 72  usly generated r
1e20c 65 63 6f 72 64 20 6e 75 6d 62 65 72 2e 20 4e 6f  ecord number. No
1e20d 20 6e 65 77 20 72 65 63 6f 72 64 20 6e 75 6d 62   new record numb
1e20e 65 72 73 20 61 72 65 0a 2a 2a 20 61 6c 6c 6f 77  ers are.** allow
1e20f 65 64 20 74 6f 20 62 65 20 6c 65 73 73 20 74 68  ed to be less th
1e210 61 6e 20 74 68 69 73 20 76 61 6c 75 65 2e 20 57  an this value. W
1e211 68 65 6e 20 74 68 69 73 20 76 61 6c 75 65 20 72  hen this value r
1e212 65 61 63 68 65 73 20 69 74 73 20 6d 61 78 69 6d  eaches its maxim
1e213 75 6d 2c 20 0a 2a 2a 20 61 20 53 51 4c 49 54 45  um, .** a SQLITE
1e214 5f 46 55 4c 4c 20 65 72 72 6f 72 20 69 73 20 67  _FULL error is g
1e215 65 6e 65 72 61 74 65 64 2e 20 54 68 65 20 50 33  enerated. The P3
1e216 20 72 65 67 69 73 74 65 72 20 69 73 20 75 70 64   register is upd
1e217 61 74 65 64 20 77 69 74 68 20 74 68 65 20 27 0a  ated with the '.
1e218 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 72 65 63  ** generated rec
1e219 6f 72 64 20 6e 75 6d 62 65 72 2e 20 54 68 69 73  ord number. This
1e21a 20 50 33 20 6d 65 63 68 61 6e 69 73 6d 20 69 73   P3 mechanism is
1e21b 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 69 6d   used to help im
1e21c 70 6c 65 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 41  plement the.** A
1e21d 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 66 65 61  UTOINCREMENT fea
1e21e 74 75 72 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  ture..*/.case OP
1e21f 5f 4e 65 77 52 6f 77 69 64 3a 20 7b 20 20 20 20  _NewRowid: {    
1e220 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70         /* out2-p
1e221 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66  rerelease */.#if
1e222 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1e223 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e224 6f 20 75 2e 62 65 20 2a 2f 0a 20 20 69 36 34 20  o u.be */.  i64 
1e225 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
1e226 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f     /* The new ro
1e227 77 69 64 20 2a 2f 0a 20 20 56 64 62 65 43 75 72  wid */.  VdbeCur
1e228 73 6f 72 20 2a 70 43 3b 20 20 20 20 20 20 20 20  sor *pC;        
1e229 2f 2a 20 43 75 72 73 6f 72 20 6f 66 20 74 61 62  /* Cursor of tab
1e22a 6c 65 20 74 6f 20 67 65 74 20 74 68 65 20 6e 65  le to get the ne
1e22b 77 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74  w rowid */.  int
1e22c 20 72 65 73 3b 20 20 20 20 20 20 20 20 20 20 20   res;           
1e22d 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66      /* Result of
1e22e 20 61 6e 20 73 71 6c 69 74 65 33 42 74 72 65 65   an sqlite3Btree
1e22f 4c 61 73 74 28 29 20 2a 2f 0a 20 20 69 6e 74 20  Last() */.  int 
1e230 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  cnt;            
1e231 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20 74 6f     /* Counter to
1e232 20 6c 69 6d 69 74 20 74 68 65 20 6e 75 6d 62 65   limit the numbe
1e233 72 20 6f 66 20 73 65 61 72 63 68 65 73 20 2a 2f  r of searches */
1e234 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20  .  Mem *pMem;   
1e235 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67            /* Reg
1e236 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 6c 61  ister holding la
1e237 72 67 65 73 74 20 72 6f 77 69 64 20 66 6f 72 20  rgest rowid for 
1e238 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f  AUTOINCREMENT */
1e239 0a 20 20 56 64 62 65 46 72 61 6d 65 20 2a 70 46  .  VdbeFrame *pF
1e23a 72 61 6d 65 3b 20 20 20 20 20 2f 2a 20 52 6f 6f  rame;     /* Roo
1e23b 74 20 66 72 61 6d 65 20 6f 66 20 56 44 42 45 20  t frame of VDBE 
1e23c 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  */.#endif /* loc
1e23d 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e23e 65 64 20 69 6e 74 6f 20 75 2e 62 65 20 2a 2f 0a  ed into u.be */.
1e23f 0a 20 20 75 2e 62 65 2e 76 20 3d 20 30 3b 0a 20  .  u.be.v = 0;. 
1e240 20 75 2e 62 65 2e 72 65 73 20 3d 20 30 3b 0a 20   u.be.res = 0;. 
1e241 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
1e242 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70  >=0 && pOp->p1<p
1e243 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75  ->nCursor );.  u
1e244 2e 62 65 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73  .be.pC = p->apCs
1e245 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73  r[pOp->p1];.  as
1e246 73 65 72 74 28 20 75 2e 62 65 2e 70 43 21 3d 30  sert( u.be.pC!=0
1e247 20 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28   );.  if( NEVER(
1e248 75 2e 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72  u.be.pC->pCursor
1e249 3d 3d 30 29 20 29 7b 0a 20 20 20 20 2f 2a 20 54  ==0) ){.    /* T
1e24a 68 65 20 7a 65 72 6f 20 69 6e 69 74 69 61 6c 69  he zero initiali
1e24b 7a 61 74 69 6f 6e 20 61 62 6f 76 65 20 69 73 20  zation above is 
1e24c 61 6c 6c 20 74 68 61 74 20 69 73 20 6e 65 65 64  all that is need
1e24d 65 64 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20  ed */.  }else{. 
1e24e 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20 72     /* The next r
1e24f 6f 77 69 64 20 6f 72 20 72 65 63 6f 72 64 20 6e  owid or record n
1e250 75 6d 62 65 72 20 28 64 69 66 66 65 72 65 6e 74  umber (different
1e251 20 74 65 72 6d 73 20 66 6f 72 20 74 68 65 20 73   terms for the s
1e252 61 6d 65 0a 20 20 20 20 2a 2a 20 74 68 69 6e 67  ame.    ** thing
1e253 29 20 69 73 20 6f 62 74 61 69 6e 65 64 20 69 6e  ) is obtained in
1e254 20 61 20 74 77 6f 2d 73 74 65 70 20 61 6c 67 6f   a two-step algo
1e255 72 69 74 68 6d 2e 0a 20 20 20 20 2a 2a 0a 20 20  rithm..    **.  
1e256 20 20 2a 2a 20 46 69 72 73 74 20 77 65 20 61 74    ** First we at
1e257 74 65 6d 70 74 20 74 6f 20 66 69 6e 64 20 74 68  tempt to find th
1e258 65 20 6c 61 72 67 65 73 74 20 65 78 69 73 74 69  e largest existi
1e259 6e 67 20 72 6f 77 69 64 20 61 6e 64 20 61 64 64  ng rowid and add
1e25a 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 74 6f 20 74   one.    ** to t
1e25b 68 61 74 2e 20 20 42 75 74 20 69 66 20 74 68 65  hat.  But if the
1e25c 20 6c 61 72 67 65 73 74 20 65 78 69 73 74 69 6e   largest existin
1e25d 67 20 72 6f 77 69 64 20 69 73 20 61 6c 72 65 61  g rowid is alrea
1e25e 64 79 20 74 68 65 20 6d 61 78 69 6d 75 6d 0a 20  dy the maximum. 
1e25f 20 20 20 2a 2a 20 70 6f 73 69 74 69 76 65 20 69     ** positive i
1e260 6e 74 65 67 65 72 2c 20 77 65 20 68 61 76 65 20  nteger, we have 
1e261 74 6f 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20  to fall through 
1e262 74 6f 20 74 68 65 20 73 65 63 6f 6e 64 0a 20 20  to the second.  
1e263 20 20 2a 2a 20 70 72 6f 62 61 62 69 6c 69 73 74    ** probabilist
1e264 69 63 20 61 6c 67 6f 72 69 74 68 6d 0a 20 20 20  ic algorithm.   
1e265 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 73   **.    ** The s
1e266 65 63 6f 6e 64 20 61 6c 67 6f 72 69 74 68 6d 20  econd algorithm 
1e267 69 73 20 74 6f 20 73 65 6c 65 63 74 20 61 20 72  is to select a r
1e268 6f 77 69 64 20 61 74 20 72 61 6e 64 6f 6d 20 61  owid at random a
1e269 6e 64 20 73 65 65 20 69 66 0a 20 20 20 20 2a 2a  nd see if.    **
1e26a 20 69 74 20 61 6c 72 65 61 64 79 20 65 78 69 73   it already exis
1e26b 74 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e  ts in the table.
1e26c 20 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74    If it does not
1e26d 20 65 78 69 73 74 2c 20 77 65 20 68 61 76 65 0a   exist, we have.
1e26e 20 20 20 20 2a 2a 20 73 75 63 63 65 65 64 65 64      ** succeeded
1e26f 2e 20 20 49 66 20 74 68 65 20 72 61 6e 64 6f 6d  .  If the random
1e270 20 72 6f 77 69 64 20 64 6f 65 73 20 65 78 69 73   rowid does exis
1e271 74 2c 20 77 65 20 73 65 6c 65 63 74 20 61 20 6e  t, we select a n
1e272 65 77 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 61 6e  ew one.    ** an
1e273 64 20 74 72 79 20 61 67 61 69 6e 2c 20 75 70 20  d try again, up 
1e274 74 6f 20 31 30 30 20 74 69 6d 65 73 2e 0a 20 20  to 100 times..  
1e275 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
1e276 20 75 2e 62 65 2e 70 43 2d 3e 69 73 54 61 62 6c   u.be.pC->isTabl
1e277 65 20 29 3b 0a 20 20 20 20 75 2e 62 65 2e 63 6e  e );.    u.be.cn
1e278 74 20 3d 20 30 3b 0a 0a 23 69 66 64 65 66 20 53  t = 0;..#ifdef S
1e279 51 4c 49 54 45 5f 33 32 42 49 54 5f 52 4f 57 49  QLITE_32BIT_ROWI
1e27a 44 0a 23 20 20 20 64 65 66 69 6e 65 20 4d 41 58  D.#   define MAX
1e27b 5f 52 4f 57 49 44 20 30 78 37 66 66 66 66 66 66  _ROWID 0x7ffffff
1e27c 66 0a 23 65 6c 73 65 0a 20 20 20 20 2f 2a 20 53  f.#else.    /* S
1e27d 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f  ome compilers co
1e27e 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 63 6f 6e  mplain about con
1e27f 73 74 61 6e 74 73 20 6f 66 20 74 68 65 20 66 6f  stants of the fo
1e280 72 6d 20 30 78 37 66 66 66 66 66 66 66 66 66 66  rm 0x7ffffffffff
1e281 66 66 66 66 66 2e 0a 20 20 20 20 2a 2a 20 4f 74  fffff..    ** Ot
1e282 68 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20 61 62  hers complain ab
1e283 6f 75 74 20 30 78 37 66 66 66 66 66 66 66 66 66  out 0x7fffffffff
1e284 66 66 66 66 66 66 66 4c 4c 2e 20 20 54 68 65 20  fffffffLL.  The 
1e285 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20  following macro 
1e286 73 65 65 6d 73 0a 20 20 20 20 2a 2a 20 74 6f 20  seems.    ** to 
1e287 70 72 6f 76 69 64 65 20 74 68 65 20 63 6f 6e 73  provide the cons
1e288 74 61 6e 74 20 77 68 69 6c 65 20 6d 61 6b 69 6e  tant while makin
1e289 67 20 61 6c 6c 20 63 6f 6d 70 69 6c 65 72 73 20  g all compilers 
1e28a 68 61 70 70 79 2e 0a 20 20 20 20 2a 2f 0a 23 20  happy..    */.# 
1e28b 20 20 64 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57    define MAX_ROW
1e28c 49 44 20 20 28 69 36 34 29 28 20 28 28 28 75 36  ID  (i64)( (((u6
1e28d 34 29 30 78 37 66 66 66 66 66 66 66 29 3c 3c 33  4)0x7fffffff)<<3
1e28e 32 29 20 7c 20 28 75 36 34 29 30 78 66 66 66 66  2) | (u64)0xffff
1e28f 66 66 66 66 20 29 0a 23 65 6e 64 69 66 0a 0a 20  ffff ).#endif.. 
1e290 20 20 20 69 66 28 20 21 75 2e 62 65 2e 70 43 2d     if( !u.be.pC-
1e291 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 20  >useRandomRowid 
1e292 29 7b 0a 20 20 20 20 20 20 75 2e 62 65 2e 76 20  ){.      u.be.v 
1e293 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
1e294 74 43 61 63 68 65 64 52 6f 77 69 64 28 75 2e 62  tCachedRowid(u.b
1e295 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a  e.pC->pCursor);.
1e296 20 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e 76        if( u.be.v
1e297 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
1e298 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1e299 4c 61 73 74 28 75 2e 62 65 2e 70 43 2d 3e 70 43  Last(u.be.pC->pC
1e29a 75 72 73 6f 72 2c 20 26 75 2e 62 65 2e 72 65 73  ursor, &u.be.res
1e29b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
1e29c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1e29d 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61            goto a
1e29e 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1e29f 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  r;.        }.   
1e2a0 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e 72 65       if( u.be.re
1e2a1 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75  s ){.          u
1e2a2 2e 62 65 2e 76 20 3d 20 31 3b 0a 20 20 20 20 20  .be.v = 1;.     
1e2a3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1e2a4 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69      assert( sqli
1e2a5 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 49 73  te3BtreeCursorIs
1e2a6 56 61 6c 69 64 28 75 2e 62 65 2e 70 43 2d 3e 70  Valid(u.be.pC->p
1e2a7 43 75 72 73 6f 72 29 20 29 3b 0a 20 20 20 20 20  Cursor) );.     
1e2a8 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1e2a9 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 75 2e  3BtreeKeySize(u.
1e2aa 62 65 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20  be.pC->pCursor, 
1e2ab 26 75 2e 62 65 2e 76 29 3b 0a 20 20 20 20 20 20  &u.be.v);.      
1e2ac 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
1e2ad 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 2f  SQLITE_OK );   /
1e2ae 2a 20 43 61 6e 6e 6f 74 20 66 61 69 6c 20 66 6f  * Cannot fail fo
1e2af 6c 6c 6f 77 69 6e 67 20 42 74 72 65 65 4c 61 73  llowing BtreeLas
1e2b0 74 28 29 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  t() */.         
1e2b1 20 69 66 28 20 75 2e 62 65 2e 76 3d 3d 4d 41 58   if( u.be.v==MAX
1e2b2 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 20 20  _ROWID ){.      
1e2b3 20 20 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e 75        u.be.pC->u
1e2b4 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 20 3d 20  seRandomRowid = 
1e2b5 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  1;.          }el
1e2b6 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
1e2b7 75 2e 62 65 2e 76 2b 2b 3b 0a 20 20 20 20 20 20  u.be.v++;.      
1e2b8 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
1e2b9 20 20 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66        }..#ifndef
1e2ba 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1e2bb 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 20  OINCREMENT.     
1e2bc 20 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b 0a   if( pOp->p3 ){.
1e2bd 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73 65 72          /* Asser
1e2be 74 20 74 68 61 74 20 50 33 20 69 73 20 61 20 76  t that P3 is a v
1e2bf 61 6c 69 64 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  alid memory cell
1e2c0 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73  . */.        ass
1e2c1 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29  ert( pOp->p3>0 )
1e2c2 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 2d  ;.        if( p-
1e2c3 3e 70 46 72 61 6d 65 20 29 7b 0a 20 20 20 20 20  >pFrame ){.     
1e2c4 20 20 20 20 20 66 6f 72 28 75 2e 62 65 2e 70 46       for(u.be.pF
1e2c5 72 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 3b 20  rame=p->pFrame; 
1e2c6 75 2e 62 65 2e 70 46 72 61 6d 65 2d 3e 70 50 61  u.be.pFrame->pPa
1e2c7 72 65 6e 74 3b 20 75 2e 62 65 2e 70 46 72 61 6d  rent; u.be.pFram
1e2c8 65 3d 75 2e 62 65 2e 70 46 72 61 6d 65 2d 3e 70  e=u.be.pFrame->p
1e2c9 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 20 20 20  Parent);.       
1e2ca 20 20 20 2f 2a 20 41 73 73 65 72 74 20 74 68 61     /* Assert tha
1e2cb 74 20 50 33 20 69 73 20 61 20 76 61 6c 69 64 20  t P3 is a valid 
1e2cc 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 2a 2f 0a  memory cell. */.
1e2cd 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
1e2ce 28 20 70 4f 70 2d 3e 70 33 3c 3d 75 2e 62 65 2e  ( pOp->p3<=u.be.
1e2cf 70 46 72 61 6d 65 2d 3e 6e 4d 65 6d 20 29 3b 0a  pFrame->nMem );.
1e2d0 20 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 70            u.be.p
1e2d1 4d 65 6d 20 3d 20 26 75 2e 62 65 2e 70 46 72 61  Mem = &u.be.pFra
1e2d2 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33  me->aMem[pOp->p3
1e2d3 5d 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  ];.        }else
1e2d4 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41  {.          /* A
1e2d5 73 73 65 72 74 20 74 68 61 74 20 50 33 20 69 73  ssert that P3 is
1e2d6 20 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20   a valid memory 
1e2d7 63 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 20  cell. */.       
1e2d8 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
1e2d9 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  p3<=p->nMem );. 
1e2da 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 70 4d           u.be.pM
1e2db 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  em = &p->aMem[pO
1e2dc 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 20 20  p->p3];.        
1e2dd 7d 0a 0a 20 20 20 20 20 20 20 20 52 45 47 49 53  }..        REGIS
1e2de 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
1e2df 33 2c 20 75 2e 62 65 2e 70 4d 65 6d 29 3b 0a 20  3, u.be.pMem);. 
1e2e0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1e2e1 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28  beMemIntegerify(
1e2e2 75 2e 62 65 2e 70 4d 65 6d 29 3b 0a 20 20 20 20  u.be.pMem);.    
1e2e3 20 20 20 20 61 73 73 65 72 74 28 20 28 75 2e 62      assert( (u.b
1e2e4 65 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20  e.pMem->flags & 
1e2e5 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 20 20  MEM_Int)!=0 );  
1e2e6 2f 2a 20 6d 65 6d 28 50 33 29 20 68 6f 6c 64 73  /* mem(P3) holds
1e2e7 20 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20   an integer */. 
1e2e8 20 20 20 20 20 20 20 69 66 28 20 75 2e 62 65 2e         if( u.be.
1e2e9 70 4d 65 6d 2d 3e 75 2e 69 3d 3d 4d 41 58 5f 52  pMem->u.i==MAX_R
1e2ea 4f 57 49 44 20 7c 7c 20 75 2e 62 65 2e 70 43 2d  OWID || u.be.pC-
1e2eb 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 20  >useRandomRowid 
1e2ec 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
1e2ed 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20  = SQLITE_FULL;. 
1e2ee 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62           goto ab
1e2ef 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1e2f0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1e2f1 20 20 20 20 69 66 28 20 75 2e 62 65 2e 76 3c 75      if( u.be.v<u
1e2f2 2e 62 65 2e 70 4d 65 6d 2d 3e 75 2e 69 2b 31 20  .be.pMem->u.i+1 
1e2f3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 2e 62  ){.          u.b
1e2f4 65 2e 76 20 3d 20 75 2e 62 65 2e 70 4d 65 6d 2d  e.v = u.be.pMem-
1e2f5 3e 75 2e 69 20 2b 20 31 3b 0a 20 20 20 20 20 20  >u.i + 1;.      
1e2f6 20 20 7d 0a 20 20 20 20 20 20 20 20 75 2e 62 65    }.        u.be
1e2f7 2e 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 75 2e 62  .pMem->u.i = u.b
1e2f8 65 2e 76 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  e.v;.      }.#en
1e2f9 64 69 66 0a 0a 20 20 20 20 20 20 73 71 6c 69 74  dif..      sqlit
1e2fa 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 64  e3BtreeSetCached
1e2fb 52 6f 77 69 64 28 75 2e 62 65 2e 70 43 2d 3e 70  Rowid(u.be.pC->p
1e2fc 43 75 72 73 6f 72 2c 20 75 2e 62 65 2e 76 3c 4d  Cursor, u.be.v<M
1e2fd 41 58 5f 52 4f 57 49 44 20 3f 20 75 2e 62 65 2e  AX_ROWID ? u.be.
1e2fe 76 2b 31 20 3a 20 30 29 3b 0a 20 20 20 20 7d 0a  v+1 : 0);.    }.
1e2ff 20 20 20 20 69 66 28 20 75 2e 62 65 2e 70 43 2d      if( u.be.pC-
1e300 3e 75 73 65 52 61 6e 64 6f 6d 52 6f 77 69 64 20  >useRandomRowid 
1e301 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
1e302 20 70 4f 70 2d 3e 70 33 3d 3d 30 20 29 3b 20 20   pOp->p3==0 );  
1e303 2f 2a 20 57 65 20 63 61 6e 6e 6f 74 20 62 65 20  /* We cannot be 
1e304 69 6e 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20  in random rowid 
1e305 6d 6f 64 65 20 69 66 20 74 68 69 73 20 69 73 0a  mode if this is.
1e306 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e307 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
1e308 61 6e 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  an AUTOINCREMENT
1e309 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 20   table. */.     
1e30a 20 75 2e 62 65 2e 76 20 3d 20 64 62 2d 3e 6c 61   u.be.v = db->la
1e30b 73 74 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 75  stRowid;.      u
1e30c 2e 62 65 2e 63 6e 74 20 3d 20 30 3b 0a 20 20 20  .be.cnt = 0;.   
1e30d 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 20 20 69     do{.        i
1e30e 66 28 20 75 2e 62 65 2e 63 6e 74 3d 3d 30 20 26  f( u.be.cnt==0 &
1e30f 26 20 28 75 2e 62 65 2e 76 26 30 78 66 66 66 66  & (u.be.v&0xffff
1e310 66 66 29 3d 3d 75 2e 62 65 2e 76 20 29 7b 0a 20  ff)==u.be.v ){. 
1e311 20 20 20 20 20 20 20 20 20 75 2e 62 65 2e 76 2b           u.be.v+
1e312 2b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  +;.        }else
1e313 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
1e314 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73  te3_randomness(s
1e315 69 7a 65 6f 66 28 75 2e 62 65 2e 76 29 2c 20 26  izeof(u.be.v), &
1e316 75 2e 62 65 2e 76 29 3b 0a 20 20 20 20 20 20 20  u.be.v);.       
1e317 20 20 20 69 66 28 20 75 2e 62 65 2e 63 6e 74 3c     if( u.be.cnt<
1e318 35 20 29 20 75 2e 62 65 2e 76 20 26 3d 20 30 78  5 ) u.be.v &= 0x
1e319 66 66 66 66 66 66 3b 0a 20 20 20 20 20 20 20 20  ffffff;.        
1e31a 7d 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  }.        rc = s
1e31b 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74  qlite3BtreeMovet
1e31c 6f 55 6e 70 61 63 6b 65 64 28 75 2e 62 65 2e 70  oUnpacked(u.be.p
1e31d 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28  C->pCursor, 0, (
1e31e 75 36 34 29 75 2e 62 65 2e 76 2c 20 30 2c 20 26  u64)u.be.v, 0, &
1e31f 75 2e 62 65 2e 72 65 73 29 3b 0a 20 20 20 20 20  u.be.res);.     
1e320 20 20 20 75 2e 62 65 2e 63 6e 74 2b 2b 3b 0a 20     u.be.cnt++;. 
1e321 20 20 20 20 20 7d 77 68 69 6c 65 28 20 75 2e 62       }while( u.b
1e322 65 2e 63 6e 74 3c 31 30 30 20 26 26 20 72 63 3d  e.cnt<100 && rc=
1e323 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 75 2e  =SQLITE_OK && u.
1e324 62 65 2e 72 65 73 3d 3d 30 20 29 3b 0a 20 20 20  be.res==0 );.   
1e325 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1e326 45 5f 4f 4b 20 26 26 20 75 2e 62 65 2e 72 65 73  E_OK && u.be.res
1e327 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
1e328 63 20 3d 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b  c = SQLITE_FULL;
1e329 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62  .        goto ab
1e32a 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1e32b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1e32c 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e 72 6f 77      u.be.pC->row
1e32d 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20  idIsValid = 0;. 
1e32e 20 20 20 75 2e 62 65 2e 70 43 2d 3e 64 65 66 65     u.be.pC->defe
1e32f 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a  rredMoveto = 0;.
1e330 20 20 20 20 75 2e 62 65 2e 70 43 2d 3e 63 61 63      u.be.pC->cac
1e331 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45  heStatus = CACHE
1e332 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 4d 65  _STALE;.  }.  Me
1e333 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75  mSetTypeFlag(pOu
1e334 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 70  t, MEM_Int);.  p
1e335 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 65 2e  Out->u.i = u.be.
1e336 76 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  v;.  break;.}../
1e337 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 73 65 72 74  * Opcode: Insert
1e338 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a   P1 P2 P3 P4 P5.
1e339 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 65  **.** Write an e
1e33a 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 74 61  ntry into the ta
1e33b 62 6c 65 20 6f 66 20 63 75 72 73 6f 72 20 50 31  ble of cursor P1
1e33c 2e 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 69  .  A new entry i
1e33d 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 69 66 20  s.** created if 
1e33e 69 74 20 64 6f 65 73 6e 27 74 20 61 6c 72 65 61  it doesn't alrea
1e33f 64 79 20 65 78 69 73 74 20 6f 72 20 74 68 65 20  dy exist or the 
1e340 64 61 74 61 20 66 6f 72 20 61 6e 20 65 78 69 73  data for an exis
1e341 74 69 6e 67 0a 2a 2a 20 65 6e 74 72 79 20 69 73  ting.** entry is
1e342 20 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20 54   overwritten.  T
1e343 68 65 20 64 61 74 61 20 69 73 20 74 68 65 20 76  he data is the v
1e344 61 6c 75 65 20 4d 45 4d 5f 42 6c 6f 62 20 73 74  alue MEM_Blob st
1e345 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72  ored in register
1e346 0a 2a 2a 20 6e 75 6d 62 65 72 20 50 32 2e 20 54  .** number P2. T
1e347 68 65 20 6b 65 79 20 69 73 20 73 74 6f 72 65 64  he key is stored
1e348 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e   in register P3.
1e349 20 54 68 65 20 6b 65 79 20 6d 75 73 74 0a 2a 2a   The key must.**
1e34a 20 62 65 20 61 20 4d 45 4d 5f 49 6e 74 2e 0a 2a   be a MEM_Int..*
1e34b 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c  *.** If the OPFL
1e34c 41 47 5f 4e 43 48 41 4e 47 45 20 66 6c 61 67 20  AG_NCHANGE flag 
1e34d 6f 66 20 50 35 20 69 73 20 73 65 74 2c 20 74 68  of P5 is set, th
1e34e 65 6e 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67  en the row chang
1e34f 65 20 63 6f 75 6e 74 20 69 73 0a 2a 2a 20 69 6e  e count is.** in
1e350 63 72 65 6d 65 6e 74 65 64 20 28 6f 74 68 65 72  cremented (other
1e351 77 69 73 65 20 6e 6f 74 29 2e 20 20 49 66 20 74  wise not).  If t
1e352 68 65 20 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f  he OPFLAG_LASTRO
1e353 57 49 44 20 66 6c 61 67 20 6f 66 20 50 35 20 69  WID flag of P5 i
1e354 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e 20 72  s set,.** then r
1e355 6f 77 69 64 20 69 73 20 73 74 6f 72 65 64 20 66  owid is stored f
1e356 6f 72 20 73 75 62 73 65 71 75 65 6e 74 20 72 65  or subsequent re
1e357 74 75 72 6e 20 62 79 20 74 68 65 0a 2a 2a 20 73  turn by the.** s
1e358 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
1e359 72 74 5f 72 6f 77 69 64 28 29 20 66 75 6e 63 74  rt_rowid() funct
1e35a 69 6f 6e 20 28 6f 74 68 65 72 77 69 73 65 20 69  ion (otherwise i
1e35b 74 20 69 73 20 75 6e 6d 6f 64 69 66 69 65 64 29  t is unmodified)
1e35c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f  ..**.** If the O
1e35d 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53  PFLAG_USESEEKRES
1e35e 55 4c 54 20 66 6c 61 67 20 6f 66 20 50 35 20 69  ULT flag of P5 i
1e35f 73 20 73 65 74 20 61 6e 64 20 69 66 20 74 68 65  s set and if the
1e360 20 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68   result of.** th
1e361 65 20 6c 61 73 74 20 73 65 65 6b 20 6f 70 65 72  e last seek oper
1e362 61 74 69 6f 6e 20 28 4f 50 5f 4e 6f 74 45 78 69  ation (OP_NotExi
1e363 73 74 73 29 20 77 61 73 20 61 20 73 75 63 63 65  sts) was a succe
1e364 73 73 2c 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a  ss, then this.**
1e365 20 6f 70 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20   operation will 
1e366 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 66  not attempt to f
1e367 69 6e 64 20 74 68 65 20 61 70 70 72 6f 70 72 69  ind the appropri
1e368 61 74 65 20 72 6f 77 20 62 65 66 6f 72 65 20 64  ate row before d
1e369 6f 69 6e 67 0a 2a 2a 20 74 68 65 20 69 6e 73 65  oing.** the inse
1e36a 72 74 20 62 75 74 20 77 69 6c 6c 20 69 6e 73 74  rt but will inst
1e36b 65 61 64 20 6f 76 65 72 77 72 69 74 65 20 74 68  ead overwrite th
1e36c 65 20 72 6f 77 20 74 68 61 74 20 74 68 65 20 63  e row that the c
1e36d 75 72 73 6f 72 20 69 73 0a 2a 2a 20 63 75 72 72  ursor is.** curr
1e36e 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74  ently pointing t
1e36f 6f 2e 20 20 50 72 65 73 75 6d 61 62 6c 79 2c 20  o.  Presumably, 
1e370 74 68 65 20 70 72 69 6f 72 20 4f 50 5f 4e 6f 74  the prior OP_Not
1e371 45 78 69 73 74 73 20 6f 70 63 6f 64 65 0a 2a 2a  Exists opcode.**
1e372 20 68 61 73 20 61 6c 72 65 61 64 79 20 70 6f 73   has already pos
1e373 69 74 69 6f 6e 65 64 20 74 68 65 20 63 75 72 73  itioned the curs
1e374 6f 72 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54  or correctly.  T
1e375 68 69 73 20 69 73 20 61 6e 20 6f 70 74 69 6d 69  his is an optimi
1e376 7a 61 74 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 62  zation.** that b
1e377 6f 6f 73 74 73 20 70 65 72 66 6f 72 6d 61 6e 63  oosts performanc
1e378 65 20 62 79 20 61 76 6f 69 64 69 6e 67 20 72 65  e by avoiding re
1e379 64 75 6e 64 61 6e 74 20 73 65 65 6b 73 2e 0a 2a  dundant seeks..*
1e37a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 46 4c  *.** If the OPFL
1e37b 41 47 5f 49 53 55 50 44 41 54 45 20 66 6c 61 67  AG_ISUPDATE flag
1e37c 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68   is set, then th
1e37d 69 73 20 6f 70 63 6f 64 65 20 69 73 20 70 61 72  is opcode is par
1e37e 74 20 6f 66 20 61 6e 0a 2a 2a 20 55 50 44 41 54  t of an.** UPDAT
1e37f 45 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4f 74  E operation.  Ot
1e380 68 65 72 77 69 73 65 20 28 69 66 20 74 68 65 20  herwise (if the 
1e381 66 6c 61 67 20 69 73 20 63 6c 65 61 72 29 20 74  flag is clear) t
1e382 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 65 0a  hen this opcode.
1e383 2a 2a 20 69 73 20 70 61 72 74 20 6f 66 20 61 6e  ** is part of an
1e384 20 49 4e 53 45 52 54 20 6f 70 65 72 61 74 69 6f   INSERT operatio
1e385 6e 2e 20 20 54 68 65 20 64 69 66 66 65 72 65 6e  n.  The differen
1e386 63 65 20 69 73 20 6f 6e 6c 79 20 69 6d 70 6f 72  ce is only impor
1e387 74 61 6e 74 20 74 6f 0a 2a 2a 20 74 68 65 20 75  tant to.** the u
1e388 70 64 61 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 0a 2a  pdate hook..**.*
1e389 2a 20 50 61 72 61 6d 65 74 65 72 20 50 34 20 6d  * Parameter P4 m
1e38a 61 79 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74  ay point to a st
1e38b 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ring containing 
1e38c 74 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65 2c 20  the table-name, 
1e38d 6f 72 0a 2a 2a 20 6d 61 79 20 62 65 20 4e 55 4c  or.** may be NUL
1e38e 4c 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20  L. If it is not 
1e38f 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 75  NULL, then the u
1e390 70 64 61 74 65 2d 68 6f 6f 6b 20 0a 2a 2a 20 28  pdate-hook .** (
1e391 73 71 6c 69 74 65 33 2e 78 55 70 64 61 74 65 43  sqlite3.xUpdateC
1e392 61 6c 6c 62 61 63 6b 29 20 69 73 20 69 6e 76 6f  allback) is invo
1e393 6b 65 64 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20  ked following a 
1e394 73 75 63 63 65 73 73 66 75 6c 20 69 6e 73 65 72  successful inser
1e395 74 2e 0a 2a 2a 0a 2a 2a 20 28 57 41 52 4e 49 4e  t..**.** (WARNIN
1e396 47 2f 54 4f 44 4f 3a 20 49 66 20 50 31 20 69 73  G/TODO: If P1 is
1e397 20 61 20 70 73 65 75 64 6f 2d 63 75 72 73 6f 72   a pseudo-cursor
1e398 20 61 6e 64 20 50 32 20 69 73 20 64 79 6e 61 6d   and P2 is dynam
1e399 69 63 61 6c 6c 79 0a 2a 2a 20 61 6c 6c 6f 63 61  ically.** alloca
1e39a 74 65 64 2c 20 74 68 65 6e 20 6f 77 6e 65 72 73  ted, then owners
1e39b 68 69 70 20 6f 66 20 50 32 20 69 73 20 74 72 61  hip of P2 is tra
1e39c 6e 73 66 65 72 72 65 64 20 74 6f 20 74 68 65 20  nsferred to the 
1e39d 70 73 65 75 64 6f 2d 63 75 72 73 6f 72 0a 2a 2a  pseudo-cursor.**
1e39e 20 61 6e 64 20 72 65 67 69 73 74 65 72 20 50 32   and register P2
1e39f 20 62 65 63 6f 6d 65 73 20 65 70 68 65 6d 65 72   becomes ephemer
1e3a0 61 6c 2e 20 20 49 66 20 74 68 65 20 63 75 72 73  al.  If the curs
1e3a1 6f 72 20 69 73 20 63 68 61 6e 67 65 64 2c 20 74  or is changed, t
1e3a2 68 65 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 72  he.** value of r
1e3a3 65 67 69 73 74 65 72 20 50 32 20 77 69 6c 6c 20  egister P2 will 
1e3a4 74 68 65 6e 20 63 68 61 6e 67 65 2e 20 20 4d 61  then change.  Ma
1e3a5 6b 65 20 73 75 72 65 20 74 68 69 73 20 64 6f 65  ke sure this doe
1e3a6 73 20 6e 6f 74 0a 2a 2a 20 63 61 75 73 65 20 61  s not.** cause a
1e3a7 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 29 0a 2a 2a  ny problems.).**
1e3a8 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63  .** This instruc
1e3a9 74 69 6f 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73 20  tion only works 
1e3aa 6f 6e 20 74 61 62 6c 65 73 2e 20 20 54 68 65 20  on tables.  The 
1e3ab 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 73 74 72  equivalent instr
1e3ac 75 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 69 6e  uction.** for in
1e3ad 64 69 63 65 73 20 69 73 20 4f 50 5f 49 64 78 49  dices is OP_IdxI
1e3ae 6e 73 65 72 74 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63  nsert..*/./* Opc
1e3af 6f 64 65 3a 20 49 6e 73 65 72 74 49 6e 74 20 50  ode: InsertInt P
1e3b0 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a 2a  1 P2 P3 P4 P5.**
1e3b1 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b 73 20 65  .** This works e
1e3b2 78 61 63 74 6c 79 20 6c 69 6b 65 20 4f 50 5f 49  xactly like OP_I
1e3b3 6e 73 65 72 74 20 65 78 63 65 70 74 20 74 68 61  nsert except tha
1e3b4 74 20 74 68 65 20 6b 65 79 20 69 73 20 74 68 65  t the key is the
1e3b5 0a 2a 2a 20 69 6e 74 65 67 65 72 20 76 61 6c 75  .** integer valu
1e3b6 65 20 50 33 2c 20 6e 6f 74 20 74 68 65 20 76 61  e P3, not the va
1e3b7 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67  lue of the integ
1e3b8 65 72 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67  er stored in reg
1e3b9 69 73 74 65 72 20 50 33 2e 0a 2a 2f 0a 63 61 73  ister P3..*/.cas
1e3ba 65 20 4f 50 5f 49 6e 73 65 72 74 3a 20 0a 63 61  e OP_Insert: .ca
1e3bb 73 65 20 4f 50 5f 49 6e 73 65 72 74 49 6e 74 3a  se OP_InsertInt:
1e3bc 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
1e3bd 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e3be 65 64 20 69 6e 74 6f 20 75 2e 62 66 20 2a 2f 0a  ed into u.bf */.
1e3bf 20 20 4d 65 6d 20 2a 70 44 61 74 61 3b 20 20 20    Mem *pData;   
1e3c0 20 20 20 20 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20      /* MEM cell 
1e3c1 68 6f 6c 64 69 6e 67 20 64 61 74 61 20 66 6f 72  holding data for
1e3c2 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 62   the record to b
1e3c3 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20  e inserted */.  
1e3c4 4d 65 6d 20 2a 70 4b 65 79 3b 20 20 20 20 20 20  Mem *pKey;      
1e3c5 20 20 2f 2a 20 4d 45 4d 20 63 65 6c 6c 20 68 6f    /* MEM cell ho
1e3c6 6c 64 69 6e 67 20 6b 65 79 20 20 66 6f 72 20 74  lding key  for t
1e3c7 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69  he record */.  i
1e3c8 36 34 20 69 4b 65 79 3b 20 20 20 20 20 20 20 20  64 iKey;        
1e3c9 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 65 72 20   /* The integer 
1e3ca 52 4f 57 49 44 20 6f 72 20 6b 65 79 20 66 6f 72  ROWID or key for
1e3cb 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f 20 62   the record to b
1e3cc 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20  e inserted */.  
1e3cd 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 20  VdbeCursor *pC; 
1e3ce 20 20 2f 2a 20 43 75 72 73 6f 72 20 74 6f 20 74    /* Cursor to t
1e3cf 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20  able into which 
1e3d0 69 6e 73 65 72 74 20 69 73 20 77 72 69 74 74 65  insert is writte
1e3d1 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f  n */.  int nZero
1e3d2 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ;        /* Numb
1e3d3 65 72 20 6f 66 20 7a 65 72 6f 2d 62 79 74 65 73  er of zero-bytes
1e3d4 20 74 6f 20 61 70 70 65 6e 64 20 2a 2f 0a 20 20   to append */.  
1e3d5 69 6e 74 20 73 65 65 6b 52 65 73 75 6c 74 3b 20  int seekResult; 
1e3d6 20 20 2f 2a 20 52 65 73 75 6c 74 20 6f 66 20 70    /* Result of p
1e3d7 72 69 6f 72 20 73 65 65 6b 20 6f 72 20 30 20 69  rior seek or 0 i
1e3d8 66 20 6e 6f 20 55 53 45 53 45 45 4b 52 45 53 55  f no USESEEKRESU
1e3d9 4c 54 20 66 6c 61 67 20 2a 2f 0a 20 20 63 6f 6e  LT flag */.  con
1e3da 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 2f  st char *zDb;  /
1e3db 2a 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  * database name 
1e3dc 2d 20 75 73 65 64 20 62 79 20 74 68 65 20 75 70  - used by the up
1e3dd 64 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 20 63  date hook */.  c
1e3de 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 62 6c 3b  onst char *zTbl;
1e3df 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2d   /* Table name -
1e3e0 20 75 73 65 64 20 62 79 20 74 68 65 20 6f 70 64   used by the opd
1e3e1 61 74 65 20 68 6f 6f 6b 20 2a 2f 0a 20 20 69 6e  ate hook */.  in
1e3e2 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20  t op;           
1e3e3 2f 2a 20 4f 70 63 6f 64 65 20 66 6f 72 20 75 70  /* Opcode for up
1e3e4 64 61 74 65 20 68 6f 6f 6b 3a 20 53 51 4c 49 54  date hook: SQLIT
1e3e5 45 5f 55 50 44 41 54 45 20 6f 72 20 53 51 4c 49  E_UPDATE or SQLI
1e3e6 54 45 5f 49 4e 53 45 52 54 20 2a 2f 0a 23 65 6e  TE_INSERT */.#en
1e3e7 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1e3e8 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1e3e9 6f 20 75 2e 62 66 20 2a 2f 0a 0a 20 20 75 2e 62  o u.bf */..  u.b
1e3ea 66 2e 70 44 61 74 61 20 3d 20 26 70 2d 3e 61 4d  f.pData = &p->aM
1e3eb 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61  em[pOp->p2];.  a
1e3ec 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d  ssert( pOp->p1>=
1e3ed 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e  0 && pOp->p1<p->
1e3ee 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62  nCursor );.  u.b
1e3ef 66 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b  f.pC = p->apCsr[
1e3f0 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65  pOp->p1];.  asse
1e3f1 72 74 28 20 75 2e 62 66 2e 70 43 21 3d 30 20 29  rt( u.bf.pC!=0 )
1e3f2 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 66  ;.  assert( u.bf
1e3f3 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20  .pC->pCursor!=0 
1e3f4 29 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62  );.  assert( u.b
1e3f5 66 2e 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c  f.pC->pseudoTabl
1e3f6 65 52 65 67 3d 3d 30 20 29 3b 0a 20 20 61 73 73  eReg==0 );.  ass
1e3f7 65 72 74 28 20 75 2e 62 66 2e 70 43 2d 3e 69 73  ert( u.bf.pC->is
1e3f8 54 61 62 6c 65 20 29 3b 0a 20 20 52 45 47 49 53  Table );.  REGIS
1e3f9 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
1e3fa 32 2c 20 75 2e 62 66 2e 70 44 61 74 61 29 3b 0a  2, u.bf.pData);.
1e3fb 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f  .  if( pOp->opco
1e3fc 64 65 3d 3d 4f 50 5f 49 6e 73 65 72 74 20 29 7b  de==OP_Insert ){
1e3fd 0a 20 20 20 20 75 2e 62 66 2e 70 4b 65 79 20 3d  .    u.bf.pKey =
1e3fe 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1e3ff 33 5d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  3];.    assert( 
1e400 75 2e 62 66 2e 70 4b 65 79 2d 3e 66 6c 61 67 73  u.bf.pKey->flags
1e401 20 26 20 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20   & MEM_Int );.  
1e402 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1e403 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 62 66 2e 70  (pOp->p3, u.bf.p
1e404 4b 65 79 29 3b 0a 20 20 20 20 75 2e 62 66 2e 69  Key);.    u.bf.i
1e405 4b 65 79 20 3d 20 75 2e 62 66 2e 70 4b 65 79 2d  Key = u.bf.pKey-
1e406 3e 75 2e 69 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  >u.i;.  }else{. 
1e407 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
1e408 6f 70 63 6f 64 65 3d 3d 4f 50 5f 49 6e 73 65 72  opcode==OP_Inser
1e409 74 49 6e 74 20 29 3b 0a 20 20 20 20 75 2e 62 66  tInt );.    u.bf
1e40a 2e 69 4b 65 79 20 3d 20 70 4f 70 2d 3e 70 33 3b  .iKey = pOp->p3;
1e40b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4f 70 2d  .  }..  if( pOp-
1e40c 3e 70 35 20 26 20 4f 50 46 4c 41 47 5f 4e 43 48  >p5 & OPFLAG_NCH
1e40d 41 4e 47 45 20 29 20 70 2d 3e 6e 43 68 61 6e 67  ANGE ) p->nChang
1e40e 65 2b 2b 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e  e++;.  if( pOp->
1e40f 70 35 20 26 20 4f 50 46 4c 41 47 5f 4c 41 53 54  p5 & OPFLAG_LAST
1e410 52 4f 57 49 44 20 29 20 64 62 2d 3e 6c 61 73 74  ROWID ) db->last
1e411 52 6f 77 69 64 20 3d 20 75 2e 62 66 2e 69 4b 65  Rowid = u.bf.iKe
1e412 79 3b 0a 20 20 69 66 28 20 75 2e 62 66 2e 70 44  y;.  if( u.bf.pD
1e413 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  ata->flags & MEM
1e414 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 75 2e 62  _Null ){.    u.b
1e415 66 2e 70 44 61 74 61 2d 3e 7a 20 3d 20 30 3b 0a  f.pData->z = 0;.
1e416 20 20 20 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e      u.bf.pData->
1e417 6e 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  n = 0;.  }else{.
1e418 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 66      assert( u.bf
1e419 2e 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20  .pData->flags & 
1e41a 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74  (MEM_Blob|MEM_St
1e41b 72 29 20 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 66  r) );.  }.  u.bf
1e41c 2e 73 65 65 6b 52 65 73 75 6c 74 20 3d 20 28 28  .seekResult = ((
1e41d 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47  pOp->p5 & OPFLAG
1e41e 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29 20  _USESEEKRESULT) 
1e41f 3f 20 75 2e 62 66 2e 70 43 2d 3e 73 65 65 6b 52  ? u.bf.pC->seekR
1e420 65 73 75 6c 74 20 3a 20 30 29 3b 0a 20 20 69 66  esult : 0);.  if
1e421 28 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 66 6c  ( u.bf.pData->fl
1e422 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29  ags & MEM_Zero )
1e423 7b 0a 20 20 20 20 75 2e 62 66 2e 6e 5a 65 72 6f  {.    u.bf.nZero
1e424 20 3d 20 75 2e 62 66 2e 70 44 61 74 61 2d 3e 75   = u.bf.pData->u
1e425 2e 6e 5a 65 72 6f 3b 0a 20 20 7d 65 6c 73 65 7b  .nZero;.  }else{
1e426 0a 20 20 20 20 75 2e 62 66 2e 6e 5a 65 72 6f 20  .    u.bf.nZero 
1e427 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  = 0;.  }.  sqlit
1e428 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65 64  e3BtreeSetCached
1e429 52 6f 77 69 64 28 75 2e 62 66 2e 70 43 2d 3e 70  Rowid(u.bf.pC->p
1e42a 43 75 72 73 6f 72 2c 20 30 29 3b 0a 20 20 72 63  Cursor, 0);.  rc
1e42b 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49   = sqlite3BtreeI
1e42c 6e 73 65 72 74 28 75 2e 62 66 2e 70 43 2d 3e 70  nsert(u.bf.pC->p
1e42d 43 75 72 73 6f 72 2c 20 30 2c 20 75 2e 62 66 2e  Cursor, 0, u.bf.
1e42e 69 4b 65 79 2c 0a 20 20 20 20 20 20 20 20 20 20  iKey,.          
1e42f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e430 75 2e 62 66 2e 70 44 61 74 61 2d 3e 7a 2c 20 75  u.bf.pData->z, u
1e431 2e 62 66 2e 70 44 61 74 61 2d 3e 6e 2c 20 75 2e  .bf.pData->n, u.
1e432 62 66 2e 6e 5a 65 72 6f 2c 0a 20 20 20 20 20 20  bf.nZero,.      
1e433 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e434 20 20 20 20 70 4f 70 2d 3e 70 35 20 26 20 4f 50      pOp->p5 & OP
1e435 46 4c 41 47 5f 41 50 50 45 4e 44 2c 20 75 2e 62  FLAG_APPEND, u.b
1e436 66 2e 73 65 65 6b 52 65 73 75 6c 74 0a 20 20 29  f.seekResult.  )
1e437 3b 0a 20 20 75 2e 62 66 2e 70 43 2d 3e 72 6f 77  ;.  u.bf.pC->row
1e438 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20  idIsValid = 0;. 
1e439 20 75 2e 62 66 2e 70 43 2d 3e 64 65 66 65 72 72   u.bf.pC->deferr
1e43a 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20  edMoveto = 0;.  
1e43b 75 2e 62 66 2e 70 43 2d 3e 63 61 63 68 65 53 74  u.bf.pC->cacheSt
1e43c 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41  atus = CACHE_STA
1e43d 4c 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65  LE;..  /* Invoke
1e43e 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b   the update-hook
1e43f 20 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f   if required. */
1e440 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1e441 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64  E_OK && db->xUpd
1e442 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70  ateCallback && p
1e443 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20  Op->p4.z ){.    
1e444 75 2e 62 66 2e 7a 44 62 20 3d 20 64 62 2d 3e 61  u.bf.zDb = db->a
1e445 44 62 5b 75 2e 62 66 2e 70 43 2d 3e 69 44 62 5d  Db[u.bf.pC->iDb]
1e446 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 75 2e 62 66  .zName;.    u.bf
1e447 2e 7a 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e  .zTbl = pOp->p4.
1e448 7a 3b 0a 20 20 20 20 75 2e 62 66 2e 6f 70 20 3d  z;.    u.bf.op =
1e449 20 28 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46   ((pOp->p5 & OPF
1e44a 4c 41 47 5f 49 53 55 50 44 41 54 45 29 20 3f 20  LAG_ISUPDATE) ? 
1e44b 53 51 4c 49 54 45 5f 55 50 44 41 54 45 20 3a 20  SQLITE_UPDATE : 
1e44c 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 29 3b 0a  SQLITE_INSERT);.
1e44d 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 66      assert( u.bf
1e44e 2e 70 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a  .pC->isTable );.
1e44f 20 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43      db->xUpdateC
1e450 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70 64  allback(db->pUpd
1e451 61 74 65 41 72 67 2c 20 75 2e 62 66 2e 6f 70 2c  ateArg, u.bf.op,
1e452 20 75 2e 62 66 2e 7a 44 62 2c 20 75 2e 62 66 2e   u.bf.zDb, u.bf.
1e453 7a 54 62 6c 2c 20 75 2e 62 66 2e 69 4b 65 79 29  zTbl, u.bf.iKey)
1e454 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e  ;.    assert( u.
1e455 62 66 2e 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b  bf.pC->iDb>=0 );
1e456 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1e457 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65 6c 65  ./* Opcode: Dele
1e458 74 65 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a  te P1 P2 * P4 *.
1e459 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65  **.** Delete the
1e45a 20 72 65 63 6f 72 64 20 61 74 20 77 68 69 63 68   record at which
1e45b 20 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 69   the P1 cursor i
1e45c 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  s currently poin
1e45d 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ting..**.** The 
1e45e 63 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 6c  cursor will be l
1e45f 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  eft pointing at 
1e460 65 69 74 68 65 72 20 74 68 65 20 6e 65 78 74 20  either the next 
1e461 6f 72 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a  or the previous.
1e462 2a 2a 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65  ** record in the
1e463 20 74 61 62 6c 65 2e 20 49 66 20 69 74 20 69 73   table. If it is
1e464 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61   left pointing a
1e465 74 20 74 68 65 20 6e 65 78 74 20 72 65 63 6f 72  t the next recor
1e466 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e  d, then.** the n
1e467 65 78 74 20 4e 65 78 74 20 69 6e 73 74 72 75 63  ext Next instruc
1e468 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 61 20 6e  tion will be a n
1e469 6f 2d 6f 70 2e 20 20 48 65 6e 63 65 20 69 74 20  o-op.  Hence it 
1e46a 69 73 20 4f 4b 20 74 6f 20 64 65 6c 65 74 65 0a  is OK to delete.
1e46b 2a 2a 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d  ** a record from
1e46c 20 77 69 74 68 69 6e 20 61 6e 20 4e 65 78 74 20   within an Next 
1e46d 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  loop..**.** If t
1e46e 68 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47  he OPFLAG_NCHANG
1e46f 45 20 66 6c 61 67 20 6f 66 20 50 32 20 69 73 20  E flag of P2 is 
1e470 73 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72 6f  set, then the ro
1e471 77 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 69  w change count i
1e472 73 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64  s.** incremented
1e473 20 28 6f 74 68 65 72 77 69 73 65 20 6e 6f 74 29   (otherwise not)
1e474 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 6d 75 73 74 20  ..**.** P1 must 
1e475 6e 6f 74 20 62 65 20 70 73 65 75 64 6f 2d 74 61  not be pseudo-ta
1e476 62 6c 65 2e 20 20 49 74 20 68 61 73 20 74 6f 20  ble.  It has to 
1e477 62 65 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20  be a real table 
1e478 77 69 74 68 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65  with.** multiple
1e479 20 72 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   rows..**.** If 
1e47a 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  P4 is not NULL, 
1e47b 74 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 6e  then it is the n
1e47c 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
1e47d 20 74 68 61 74 20 50 31 20 69 73 0a 2a 2a 20 70   that P1 is.** p
1e47e 6f 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65  ointing to.  The
1e47f 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 77 69 6c   update hook wil
1e480 6c 20 62 65 20 69 6e 76 6f 6b 65 64 2c 20 69 66  l be invoked, if
1e481 20 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 20 49   it exists..** I
1e482 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  f P4 is not NULL
1e483 20 74 68 65 6e 20 74 68 65 20 50 31 20 63 75 72   then the P1 cur
1e484 73 6f 72 20 6d 75 73 74 20 68 61 76 65 20 62 65  sor must have be
1e485 65 6e 20 70 6f 73 69 74 69 6f 6e 65 64 0a 2a 2a  en positioned.**
1e486 20 75 73 69 6e 67 20 4f 50 5f 4e 6f 74 46 6f 75   using OP_NotFou
1e487 6e 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f  nd prior to invo
1e488 6b 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65  king this opcode
1e489 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 65 6c  ..*/.case OP_Del
1e48a 65 74 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a  ete: {.#if 0  /*
1e48b 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e48c 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 67   moved into u.bg
1e48d 20 2a 2f 0a 20 20 69 36 34 20 69 4b 65 79 3b 0a   */.  i64 iKey;.
1e48e 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1e48f 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1e490 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e491 64 20 69 6e 74 6f 20 75 2e 62 67 20 2a 2f 0a 0a  d into u.bg */..
1e492 20 20 75 2e 62 67 2e 69 4b 65 79 20 3d 20 30 3b    u.bg.iKey = 0;
1e493 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1e494 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
1e495 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1e496 20 75 2e 62 67 2e 70 43 20 3d 20 70 2d 3e 61 70   u.bg.pC = p->ap
1e497 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1e498 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43 21  assert( u.bg.pC!
1e499 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
1e49a 75 2e 62 67 2e 70 43 2d 3e 70 43 75 72 73 6f 72  u.bg.pC->pCursor
1e49b 21 3d 30 20 29 3b 20 20 2f 2a 20 4f 6e 6c 79 20  !=0 );  /* Only 
1e49c 76 61 6c 69 64 20 66 6f 72 20 72 65 61 6c 20 74  valid for real t
1e49d 61 62 6c 65 73 2c 20 6e 6f 20 70 73 65 75 64 6f  ables, no pseudo
1e49e 74 61 62 6c 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20  tables */..  /* 
1e49f 49 66 20 74 68 65 20 75 70 64 61 74 65 2d 68 6f  If the update-ho
1e4a0 6f 6b 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f 6b  ok will be invok
1e4a1 65 64 2c 20 73 65 74 20 75 2e 62 67 2e 69 4b 65  ed, set u.bg.iKe
1e4a2 79 20 74 6f 20 74 68 65 20 72 6f 77 69 64 20 6f  y to the rowid o
1e4a3 66 20 74 68 65 0a 20 20 2a 2a 20 72 6f 77 20 62  f the.  ** row b
1e4a4 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 20 20  eing deleted..  
1e4a5 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 78 55 70  */.  if( db->xUp
1e4a6 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20  dateCallback && 
1e4a7 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20  pOp->p4.z ){.   
1e4a8 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43   assert( u.bg.pC
1e4a9 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20  ->isTable );.   
1e4aa 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70 43   assert( u.bg.pC
1e4ab 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 29  ->rowidIsValid )
1e4ac 3b 20 20 2f 2a 20 6c 61 73 74 52 6f 77 69 64 20  ;  /* lastRowid 
1e4ad 73 65 74 20 62 79 20 70 72 65 76 69 6f 75 73 20  set by previous 
1e4ae 4f 50 5f 4e 6f 74 46 6f 75 6e 64 20 2a 2f 0a 20  OP_NotFound */. 
1e4af 20 20 20 75 2e 62 67 2e 69 4b 65 79 20 3d 20 75     u.bg.iKey = u
1e4b0 2e 62 67 2e 70 43 2d 3e 6c 61 73 74 52 6f 77 69  .bg.pC->lastRowi
1e4b1 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65  d;.  }..  /* The
1e4b2 20 4f 50 5f 44 65 6c 65 74 65 20 6f 70 63 6f 64   OP_Delete opcod
1e4b3 65 20 61 6c 77 61 79 73 20 66 6f 6c 6c 6f 77 73  e always follows
1e4b4 20 61 6e 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73   an OP_NotExists
1e4b5 20 6f 72 20 4f 50 5f 4c 61 73 74 20 6f 72 0a 20   or OP_Last or. 
1e4b6 20 2a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 6e   ** OP_Column on
1e4b7 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65 20   the same table 
1e4b8 77 69 74 68 6f 75 74 20 61 6e 79 20 69 6e 74 65  without any inte
1e4b9 72 76 65 6e 69 6e 67 20 6f 70 65 72 61 74 69 6f  rvening operatio
1e4ba 6e 73 20 74 68 61 74 0a 20 20 2a 2a 20 6d 69 67  ns that.  ** mig
1e4bb 68 74 20 6d 6f 76 65 20 6f 72 20 69 6e 76 61 6c  ht move or inval
1e4bc 69 64 61 74 65 20 74 68 65 20 63 75 72 73 6f 72  idate the cursor
1e4bd 2e 20 20 48 65 6e 63 65 20 63 75 72 73 6f 72 20  .  Hence cursor 
1e4be 75 2e 62 67 2e 70 43 20 69 73 20 61 6c 77 61 79  u.bg.pC is alway
1e4bf 73 20 70 6f 69 6e 74 69 6e 67 0a 20 20 2a 2a 20  s pointing.  ** 
1e4c0 74 6f 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65  to the row to be
1e4c1 20 64 65 6c 65 74 65 64 20 61 6e 64 20 74 68 65   deleted and the
1e4c2 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73   sqlite3VdbeCurs
1e4c3 6f 72 4d 6f 76 65 74 6f 28 29 20 6f 70 65 72 61  orMoveto() opera
1e4c4 74 69 6f 6e 0a 20 20 2a 2a 20 62 65 6c 6f 77 20  tion.  ** below 
1e4c5 69 73 20 61 6c 77 61 79 73 20 61 20 6e 6f 2d 6f  is always a no-o
1e4c6 70 20 61 6e 64 20 63 61 6e 6e 6f 74 20 66 61 69  p and cannot fai
1e4c7 6c 2e 20 20 57 65 20 77 69 6c 6c 20 72 75 6e 20  l.  We will run 
1e4c8 69 74 20 61 6e 79 68 6f 77 2c 20 74 68 6f 75 67  it anyhow, thoug
1e4c9 68 2c 0a 20 20 2a 2a 20 74 6f 20 67 75 61 72 64  h,.  ** to guard
1e4ca 20 61 67 61 69 6e 73 74 20 66 75 74 75 72 65 20   against future 
1e4cb 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 63  changes to the c
1e4cc 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 2e 0a 20  ode generator.. 
1e4cd 20 2a 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 75   **/.  assert( u
1e4ce 2e 62 67 2e 70 43 2d 3e 64 65 66 65 72 72 65 64  .bg.pC->deferred
1e4cf 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20 72  Moveto==0 );.  r
1e4d0 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  c = sqlite3VdbeC
1e4d1 75 72 73 6f 72 4d 6f 76 65 74 6f 28 75 2e 62 67  ursorMoveto(u.bg
1e4d2 2e 70 43 29 3b 0a 20 20 69 66 28 20 4e 45 56 45  .pC);.  if( NEVE
1e4d3 52 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 29  R(rc!=SQLITE_OK)
1e4d4 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1e4d5 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 0a 20 20 73  e_to_error;..  s
1e4d6 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 43 61  qlite3BtreeSetCa
1e4d7 63 68 65 64 52 6f 77 69 64 28 75 2e 62 67 2e 70  chedRowid(u.bg.p
1e4d8 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30 29 3b 0a  C->pCursor, 0);.
1e4d9 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1e4da 72 65 65 44 65 6c 65 74 65 28 75 2e 62 67 2e 70  reeDelete(u.bg.p
1e4db 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 75  C->pCursor);.  u
1e4dc 2e 62 67 2e 70 43 2d 3e 63 61 63 68 65 53 74 61  .bg.pC->cacheSta
1e4dd 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c  tus = CACHE_STAL
1e4de 45 3b 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  E;..  /* Invoke 
1e4df 74 68 65 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20  the update-hook 
1e4e0 69 66 20 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a  if required. */.
1e4e1 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1e4e2 5f 4f 4b 20 26 26 20 64 62 2d 3e 78 55 70 64 61  _OK && db->xUpda
1e4e3 74 65 43 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f  teCallback && pO
1e4e4 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 63  p->p4.z ){.    c
1e4e5 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d  onst char *zDb =
1e4e6 20 64 62 2d 3e 61 44 62 5b 75 2e 62 67 2e 70 43   db->aDb[u.bg.pC
1e4e7 2d 3e 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  ->iDb].zName;.  
1e4e8 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
1e4e9 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b 0a  bl = pOp->p4.z;.
1e4ea 20 20 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43      db->xUpdateC
1e4eb 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 55 70 64  allback(db->pUpd
1e4ec 61 74 65 41 72 67 2c 20 53 51 4c 49 54 45 5f 44  ateArg, SQLITE_D
1e4ed 45 4c 45 54 45 2c 20 7a 44 62 2c 20 7a 54 62 6c  ELETE, zDb, zTbl
1e4ee 2c 20 75 2e 62 67 2e 69 4b 65 79 29 3b 0a 20 20  , u.bg.iKey);.  
1e4ef 20 20 61 73 73 65 72 74 28 20 75 2e 62 67 2e 70    assert( u.bg.p
1e4f0 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 7d  C->iDb>=0 );.  }
1e4f1 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 26  .  if( pOp->p2 &
1e4f2 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20   OPFLAG_NCHANGE 
1e4f3 29 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a  ) p->nChange++;.
1e4f4 20 20 62 72 65 61 6b 3b 0a 7d 0a 2f 2a 20 4f 70    break;.}./* Op
1e4f5 63 6f 64 65 3a 20 52 65 73 65 74 43 6f 75 6e 74  code: ResetCount
1e4f6 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a   * * * * *.**.**
1e4f7 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   The value of th
1e4f8 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72  e change counter
1e4f9 20 69 73 20 63 6f 70 69 65 64 20 74 6f 20 74 68   is copied to th
1e4fa 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
1e4fb 65 0a 2a 2a 20 63 68 61 6e 67 65 20 63 6f 75 6e  e.** change coun
1e4fc 74 65 72 20 28 72 65 74 75 72 6e 65 64 20 62 79  ter (returned by
1e4fd 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
1e4fe 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 68 61  s to sqlite3_cha
1e4ff 6e 67 65 73 28 29 29 2e 0a 2a 2a 20 54 68 65 6e  nges())..** Then
1e500 20 74 68 65 20 56 4d 73 20 69 6e 74 65 72 6e 61   the VMs interna
1e501 6c 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72  l change counter
1e502 20 72 65 73 65 74 73 20 74 6f 20 30 2e 0a 2a 2a   resets to 0..**
1e503 20 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79   This is used by
1e504 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d   trigger program
1e505 73 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 65  s..*/.case OP_Re
1e506 73 65 74 43 6f 75 6e 74 3a 20 7b 0a 20 20 73 71  setCount: {.  sq
1e507 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e  lite3VdbeSetChan
1e508 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e  ges(db, p->nChan
1e509 67 65 29 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e 67  ge);.  p->nChang
1e50a 65 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a  e = 0;.  break;.
1e50b 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f  }../* Opcode: Ro
1e50c 77 44 61 74 61 20 50 31 20 50 32 20 2a 20 2a 20  wData P1 P2 * * 
1e50d 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69 6e  *.**.** Write in
1e50e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 20 74  to register P2 t
1e50f 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77 20  he complete row 
1e510 64 61 74 61 20 66 6f 72 20 63 75 72 73 6f 72 20  data for cursor 
1e511 50 31 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73 20  P1..** There is 
1e512 6e 6f 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f  no interpretatio
1e513 6e 20 6f 66 20 74 68 65 20 64 61 74 61 2e 20 20  n of the data.  
1e514 0a 2a 2a 20 49 74 20 69 73 20 6a 75 73 74 20 63  .** It is just c
1e515 6f 70 69 65 64 20 6f 6e 74 6f 20 74 68 65 20 50  opied onto the P
1e516 32 20 72 65 67 69 73 74 65 72 20 65 78 61 63 74  2 register exact
1e517 6c 79 20 61 73 20 0a 2a 2a 20 69 74 20 69 73 20  ly as .** it is 
1e518 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61 74  found in the dat
1e519 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  abase file..**.*
1e51a 2a 20 49 66 20 74 68 65 20 50 31 20 63 75 72 73  * If the P1 curs
1e51b 6f 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74  or must be point
1e51c 69 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 20 72  ing to a valid r
1e51d 6f 77 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20 72  ow (not a NULL r
1e51e 6f 77 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61 6c  ow).** of a real
1e51f 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73   table, not a ps
1e520 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 2f  eudo-table..*/./
1e521 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 4b 65 79  * Opcode: RowKey
1e522 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1e523 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72 65  ** Write into re
1e524 67 69 73 74 65 72 20 50 32 20 74 68 65 20 63 6f  gister P2 the co
1e525 6d 70 6c 65 74 65 20 72 6f 77 20 6b 65 79 20 66  mplete row key f
1e526 6f 72 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a 2a  or cursor P1..**
1e527 20 54 68 65 72 65 20 69 73 20 6e 6f 20 69 6e 74   There is no int
1e528 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 74  erpretation of t
1e529 68 65 20 64 61 74 61 2e 20 20 0a 2a 2a 20 54 68  he data.  .** Th
1e52a 65 20 6b 65 79 20 69 73 20 63 6f 70 69 65 64 20  e key is copied 
1e52b 6f 6e 74 6f 20 74 68 65 20 50 33 20 72 65 67 69  onto the P3 regi
1e52c 73 74 65 72 20 65 78 61 63 74 6c 79 20 61 73 20  ster exactly as 
1e52d 0a 2a 2a 20 69 74 20 69 73 20 66 6f 75 6e 64 20  .** it is found 
1e52e 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
1e52f 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  file..**.** If t
1e530 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73  he P1 cursor mus
1e531 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f  t be pointing to
1e532 20 61 20 76 61 6c 69 64 20 72 6f 77 20 28 6e 6f   a valid row (no
1e533 74 20 61 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a 2a  t a NULL row).**
1e534 20 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c 65   of a real table
1e535 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d 74  , not a pseudo-t
1e536 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  able..*/.case OP
1e537 5f 52 6f 77 4b 65 79 3a 0a 63 61 73 65 20 4f 50  _RowKey:.case OP
1e538 5f 52 6f 77 44 61 74 61 3a 20 7b 0a 23 69 66 20  _RowData: {.#if 
1e539 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1e53a 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e53b 20 75 2e 62 68 20 2a 2f 0a 20 20 56 64 62 65 43   u.bh */.  VdbeC
1e53c 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43  ursor *pC;.  BtC
1e53d 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20  ursor *pCrsr;.  
1e53e 75 33 32 20 6e 3b 0a 20 20 69 36 34 20 6e 36 34  u32 n;.  i64 n64
1e53f 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1e540 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e541 64 20 69 6e 74 6f 20 75 2e 62 68 20 2a 2f 0a 0a  d into u.bh */..
1e542 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65    pOut = &p->aMe
1e543 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 0a 20 20 2f  m[pOp->p2];..  /
1e544 2a 20 4e 6f 74 65 20 74 68 61 74 20 52 6f 77 4b  * Note that RowK
1e545 65 79 20 61 6e 64 20 52 6f 77 44 61 74 61 20 61  ey and RowData a
1e546 72 65 20 72 65 61 6c 6c 79 20 65 78 61 63 74 6c  re really exactl
1e547 79 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 72  y the same instr
1e548 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65  uction */.  asse
1e549 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1e54a 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
1e54b 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 68 2e 70  rsor );.  u.bh.p
1e54c 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  C = p->apCsr[pOp
1e54d 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1e54e 20 75 2e 62 68 2e 70 43 2d 3e 69 73 54 61 62 6c   u.bh.pC->isTabl
1e54f 65 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  e || pOp->opcode
1e550 3d 3d 4f 50 5f 52 6f 77 4b 65 79 20 29 3b 0a 20  ==OP_RowKey );. 
1e551 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70 43   assert( u.bh.pC
1e552 2d 3e 69 73 49 6e 64 65 78 20 7c 7c 20 70 4f 70  ->isIndex || pOp
1e553 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77  ->opcode==OP_Row
1e554 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74  Data );.  assert
1e555 28 20 75 2e 62 68 2e 70 43 21 3d 30 20 29 3b 0a  ( u.bh.pC!=0 );.
1e556 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e 70    assert( u.bh.p
1e557 43 2d 3e 6e 75 6c 6c 52 6f 77 3d 3d 30 20 29 3b  C->nullRow==0 );
1e558 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e  .  assert( u.bh.
1e559 70 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52  pC->pseudoTableR
1e55a 65 67 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  eg==0 );.  asser
1e55b 74 28 20 75 2e 62 68 2e 70 43 2d 3e 70 43 75 72  t( u.bh.pC->pCur
1e55c 73 6f 72 21 3d 30 20 29 3b 0a 20 20 75 2e 62 68  sor!=0 );.  u.bh
1e55d 2e 70 43 72 73 72 20 3d 20 75 2e 62 68 2e 70 43  .pCrsr = u.bh.pC
1e55e 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 61 73 73  ->pCursor;.  ass
1e55f 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1e560 65 43 75 72 73 6f 72 49 73 56 61 6c 69 64 28 75  eCursorIsValid(u
1e561 2e 62 68 2e 70 43 72 73 72 29 20 29 3b 0a 0a 20  .bh.pCrsr) );.. 
1e562 20 2f 2a 20 54 68 65 20 4f 50 5f 52 6f 77 4b 65   /* The OP_RowKe
1e563 79 20 61 6e 64 20 4f 50 5f 52 6f 77 44 61 74 61  y and OP_RowData
1e564 20 6f 70 63 6f 64 65 73 20 61 6c 77 61 79 73 20   opcodes always 
1e565 66 6f 6c 6c 6f 77 20 4f 50 5f 4e 6f 74 45 78 69  follow OP_NotExi
1e566 73 74 73 20 6f 72 0a 20 20 2a 2a 20 4f 50 5f 52  sts or.  ** OP_R
1e567 65 77 69 6e 64 2f 4f 70 5f 4e 65 78 74 20 77 69  ewind/Op_Next wi
1e568 74 68 20 6e 6f 20 69 6e 74 65 72 76 65 6e 69 6e  th no intervenin
1e569 67 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 74  g instructions t
1e56a 68 61 74 20 6d 69 67 68 74 20 69 6e 76 61 6c 69  hat might invali
1e56b 64 61 74 65 0a 20 20 2a 2a 20 74 68 65 20 63 75  date.  ** the cu
1e56c 72 73 6f 72 2e 20 20 48 65 6e 63 65 20 74 68 65  rsor.  Hence the
1e56d 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 71 6c 69 74   following sqlit
1e56e 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76 65  e3VdbeCursorMove
1e56f 74 6f 28 29 20 63 61 6c 6c 20 69 73 20 61 6c 77  to() call is alw
1e570 61 79 73 0a 20 20 2a 2a 20 61 20 6e 6f 2d 6f 70  ays.  ** a no-op
1e571 20 61 6e 64 20 63 61 6e 20 6e 65 76 65 72 20 66   and can never f
1e572 61 69 6c 2e 20 20 42 75 74 20 77 65 20 6c 65 61  ail.  But we lea
1e573 76 65 20 69 74 20 69 6e 20 70 6c 61 63 65 20 61  ve it in place a
1e574 73 20 61 20 73 61 66 65 74 79 2e 0a 20 20 2a 2f  s a safety..  */
1e575 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 68 2e  .  assert( u.bh.
1e576 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
1e577 74 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20  to==0 );.  rc = 
1e578 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f  sqlite3VdbeCurso
1e579 72 4d 6f 76 65 74 6f 28 75 2e 62 68 2e 70 43 29  rMoveto(u.bh.pC)
1e57a 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 72 63  ;.  if( NEVER(rc
1e57b 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 20 67  !=SQLITE_OK) ) g
1e57c 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1e57d 5f 65 72 72 6f 72 3b 0a 0a 20 20 69 66 28 20 75  _error;..  if( u
1e57e 2e 62 68 2e 70 43 2d 3e 69 73 49 6e 64 65 78 20  .bh.pC->isIndex 
1e57f 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  ){.    assert( !
1e580 75 2e 62 68 2e 70 43 2d 3e 69 73 54 61 62 6c 65  u.bh.pC->isTable
1e581 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   );.    rc = sql
1e582 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65  ite3BtreeKeySize
1e583 28 75 2e 62 68 2e 70 43 72 73 72 2c 20 26 75 2e  (u.bh.pCrsr, &u.
1e584 62 68 2e 6e 36 34 29 3b 0a 20 20 20 20 61 73 73  bh.n64);.    ass
1e585 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
1e586 4f 4b 20 29 3b 20 20 20 20 2f 2a 20 54 72 75 65  OK );    /* True
1e587 20 62 65 63 61 75 73 65 20 6f 66 20 43 75 72 73   because of Curs
1e588 6f 72 4d 6f 76 65 74 6f 28 29 20 63 61 6c 6c 20  orMoveto() call 
1e589 61 62 6f 76 65 20 2a 2f 0a 20 20 20 20 69 66 28  above */.    if(
1e58a 20 75 2e 62 68 2e 6e 36 34 3e 64 62 2d 3e 61 4c   u.bh.n64>db->aL
1e58b 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
1e58c 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20  T_LENGTH] ){.   
1e58d 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b     goto too_big;
1e58e 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 68 2e  .    }.    u.bh.
1e58f 6e 20 3d 20 28 75 33 32 29 75 2e 62 68 2e 6e 36  n = (u32)u.bh.n6
1e590 34 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  4;.  }else{.    
1e591 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1e592 65 44 61 74 61 53 69 7a 65 28 75 2e 62 68 2e 70  eDataSize(u.bh.p
1e593 43 72 73 72 2c 20 26 75 2e 62 68 2e 6e 29 3b 0a  Crsr, &u.bh.n);.
1e594 20 20 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d      assert( rc==
1e595 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 20 20 20 20  SQLITE_OK );    
1e596 2f 2a 20 44 61 74 61 53 69 7a 65 28 29 20 63 61  /* DataSize() ca
1e597 6e 6e 6f 74 20 66 61 69 6c 20 2a 2f 0a 20 20 20  nnot fail */.   
1e598 20 69 66 28 20 75 2e 62 68 2e 6e 3e 28 75 33 32   if( u.bh.n>(u32
1e599 29 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  )db->aLimit[SQLI
1e59a 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
1e59b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74   ){.      goto t
1e59c 6f 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20  oo_big;.    }.  
1e59d 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56  }.  if( sqlite3V
1e59e 64 62 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c  dbeMemGrow(pOut,
1e59f 20 75 2e 62 68 2e 6e 2c 20 30 29 20 29 7b 0a 20   u.bh.n, 0) ){. 
1e5a0 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a     goto no_mem;.
1e5a1 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20    }.  pOut->n = 
1e5a2 75 2e 62 68 2e 6e 3b 0a 20 20 4d 65 6d 53 65 74  u.bh.n;.  MemSet
1e5a3 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d  TypeFlag(pOut, M
1e5a4 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 69 66 28 20  EM_Blob);.  if( 
1e5a5 75 2e 62 68 2e 70 43 2d 3e 69 73 49 6e 64 65 78  u.bh.pC->isIndex
1e5a6 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
1e5a7 69 74 65 33 42 74 72 65 65 4b 65 79 28 75 2e 62  ite3BtreeKey(u.b
1e5a8 68 2e 70 43 72 73 72 2c 20 30 2c 20 75 2e 62 68  h.pCrsr, 0, u.bh
1e5a9 2e 6e 2c 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20  .n, pOut->z);.  
1e5aa 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
1e5ab 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61  sqlite3BtreeData
1e5ac 28 75 2e 62 68 2e 70 43 72 73 72 2c 20 30 2c 20  (u.bh.pCrsr, 0, 
1e5ad 75 2e 62 68 2e 6e 2c 20 70 4f 75 74 2d 3e 7a 29  u.bh.n, pOut->z)
1e5ae 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 65 6e  ;.  }.  pOut->en
1e5af 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b  c = SQLITE_UTF8;
1e5b0 20 20 2f 2a 20 49 6e 20 63 61 73 65 20 74 68 65    /* In case the
1e5b1 20 62 6c 6f 62 20 69 73 20 65 76 65 72 20 63 61   blob is ever ca
1e5b2 73 74 20 74 6f 20 74 65 78 74 20 2a 2f 0a 20 20  st to text */.  
1e5b3 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53  UPDATE_MAX_BLOBS
1e5b4 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65  IZE(pOut);.  bre
1e5b5 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1e5b6 3a 20 52 6f 77 69 64 20 50 31 20 50 32 20 2a 20  : Rowid P1 P2 * 
1e5b7 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 20  * *.**.** Store 
1e5b8 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 61  in register P2 a
1e5b9 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20  n integer which 
1e5ba 69 73 20 74 68 65 20 6b 65 79 20 6f 66 20 74 68  is the key of th
1e5bb 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 74 68  e table entry th
1e5bc 61 74 0a 2a 2a 20 50 31 20 69 73 20 63 75 72 72  at.** P1 is curr
1e5bd 65 6e 74 6c 79 20 70 6f 69 6e 74 20 74 6f 2e 0a  ently point to..
1e5be 2a 2a 0a 2a 2a 20 50 31 20 63 61 6e 20 62 65 20  **.** P1 can be 
1e5bf 65 69 74 68 65 72 20 61 6e 20 6f 72 64 69 6e 61  either an ordina
1e5c0 72 79 20 74 61 62 6c 65 20 6f 72 20 61 20 76 69  ry table or a vi
1e5c1 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68  rtual table.  Th
1e5c2 65 72 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 62  ere used to.** b
1e5c3 65 20 61 20 73 65 70 61 72 61 74 65 20 4f 50 5f  e a separate OP_
1e5c4 56 52 6f 77 69 64 20 6f 70 63 6f 64 65 20 66 6f  VRowid opcode fo
1e5c5 72 20 75 73 65 20 77 69 74 68 20 76 69 72 74 75  r use with virtu
1e5c6 61 6c 20 74 61 62 6c 65 73 2c 20 62 75 74 20 74  al tables, but t
1e5c7 68 69 73 0a 2a 2a 20 6f 6e 65 20 6f 70 63 6f 64  his.** one opcod
1e5c8 65 20 6e 6f 77 20 77 6f 72 6b 73 20 66 6f 72 20  e now works for 
1e5c9 62 6f 74 68 20 74 61 62 6c 65 20 74 79 70 65 73  both table types
1e5ca 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77  ..*/.case OP_Row
1e5cb 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  id: {           
1e5cc 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1e5cd 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20  erelease */.#if 
1e5ce 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1e5cf 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e5d0 20 75 2e 62 69 20 2a 2f 0a 20 20 56 64 62 65 43   u.bi */.  VdbeC
1e5d1 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69 36 34  ursor *pC;.  i64
1e5d2 20 76 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74   v;.  sqlite3_vt
1e5d3 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f 6e  ab *pVtab;.  con
1e5d4 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
1e5d5 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 23 65 6e 64  e *pModule;.#end
1e5d6 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1e5d7 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e5d8 20 75 2e 62 69 20 2a 2f 0a 0a 20 20 61 73 73 65   u.bi */..  asse
1e5d9 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1e5da 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
1e5db 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 69 2e 70  rsor );.  u.bi.p
1e5dc 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  C = p->apCsr[pOp
1e5dd 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1e5de 20 75 2e 62 69 2e 70 43 21 3d 30 20 29 3b 0a 20   u.bi.pC!=0 );. 
1e5df 20 61 73 73 65 72 74 28 20 75 2e 62 69 2e 70 43   assert( u.bi.pC
1e5e0 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 52 65 67  ->pseudoTableReg
1e5e1 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 75 2e 62  ==0 );.  if( u.b
1e5e2 69 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b  i.pC->nullRow ){
1e5e3 0a 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69  .    /* Do nothi
1e5e4 6e 67 20 73 6f 20 74 68 61 74 20 72 65 67 5b 50  ng so that reg[P
1e5e5 32 5d 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 20  2] remains NULL 
1e5e6 2a 2f 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  */.    break;.  
1e5e7 7d 65 6c 73 65 20 69 66 28 20 75 2e 62 69 2e 70  }else if( u.bi.p
1e5e8 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
1e5e9 6f 20 29 7b 0a 20 20 20 20 75 2e 62 69 2e 76 20  o ){.    u.bi.v 
1e5ea 3d 20 75 2e 62 69 2e 70 43 2d 3e 6d 6f 76 65 74  = u.bi.pC->movet
1e5eb 6f 54 61 72 67 65 74 3b 0a 23 69 66 6e 64 65 66  oTarget;.#ifndef
1e5ec 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1e5ed 54 55 41 4c 54 41 42 4c 45 0a 20 20 7d 65 6c 73  TUALTABLE.  }els
1e5ee 65 20 69 66 28 20 75 2e 62 69 2e 70 43 2d 3e 70  e if( u.bi.pC->p
1e5ef 56 74 61 62 43 75 72 73 6f 72 20 29 7b 0a 20 20  VtabCursor ){.  
1e5f0 20 20 75 2e 62 69 2e 70 56 74 61 62 20 3d 20 75    u.bi.pVtab = u
1e5f1 2e 62 69 2e 70 43 2d 3e 70 56 74 61 62 43 75 72  .bi.pC->pVtabCur
1e5f2 73 6f 72 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20  sor->pVtab;.    
1e5f3 75 2e 62 69 2e 70 4d 6f 64 75 6c 65 20 3d 20 75  u.bi.pModule = u
1e5f4 2e 62 69 2e 70 56 74 61 62 2d 3e 70 4d 6f 64 75  .bi.pVtab->pModu
1e5f5 6c 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  le;.    assert( 
1e5f6 75 2e 62 69 2e 70 4d 6f 64 75 6c 65 2d 3e 78 52  u.bi.pModule->xR
1e5f7 6f 77 69 64 20 29 3b 0a 20 20 20 20 69 66 28 20  owid );.    if( 
1e5f8 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
1e5f9 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72  (db) ) goto abor
1e5fa 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
1e5fb 0a 20 20 20 20 72 63 20 3d 20 75 2e 62 69 2e 70  .    rc = u.bi.p
1e5fc 4d 6f 64 75 6c 65 2d 3e 78 52 6f 77 69 64 28 75  Module->xRowid(u
1e5fd 2e 62 69 2e 70 43 2d 3e 70 56 74 61 62 43 75 72  .bi.pC->pVtabCur
1e5fe 73 6f 72 2c 20 26 75 2e 62 69 2e 76 29 3b 0a 20  sor, &u.bi.v);. 
1e5ff 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1e600 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29  (db, p->zErrMsg)
1e601 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67  ;.    p->zErrMsg
1e602 20 3d 20 75 2e 62 69 2e 70 56 74 61 62 2d 3e 7a   = u.bi.pVtab->z
1e603 45 72 72 4d 73 67 3b 0a 20 20 20 20 75 2e 62 69  ErrMsg;.    u.bi
1e604 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20  .pVtab->zErrMsg 
1e605 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  = 0;.    if( sql
1e606 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
1e607 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1e608 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 23 65 6e  e_to_misuse;.#en
1e609 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
1e60a 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20  IT_VIRTUALTABLE 
1e60b 2a 2f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  */.  }else{.    
1e60c 61 73 73 65 72 74 28 20 75 2e 62 69 2e 70 43 2d  assert( u.bi.pC-
1e60d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 3b 0a 20  >pCursor!=0 );. 
1e60e 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
1e60f 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28  dbeCursorMoveto(
1e610 75 2e 62 69 2e 70 43 29 3b 0a 20 20 20 20 69 66  u.bi.pC);.    if
1e611 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f 72  ( rc ) goto abor
1e612 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1e613 20 20 20 20 69 66 28 20 75 2e 62 69 2e 70 43 2d      if( u.bi.pC-
1e614 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 29 7b  >rowidIsValid ){
1e615 0a 20 20 20 20 20 20 75 2e 62 69 2e 76 20 3d 20  .      u.bi.v = 
1e616 75 2e 62 69 2e 70 43 2d 3e 6c 61 73 74 52 6f 77  u.bi.pC->lastRow
1e617 69 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  id;.    }else{. 
1e618 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1e619 33 42 74 72 65 65 4b 65 79 53 69 7a 65 28 75 2e  3BtreeKeySize(u.
1e61a 62 69 2e 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20  bi.pC->pCursor, 
1e61b 26 75 2e 62 69 2e 76 29 3b 0a 20 20 20 20 20 20  &u.bi.v);.      
1e61c 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
1e61d 54 45 5f 4f 4b 20 29 3b 20 20 2f 2a 20 41 6c 77  TE_OK );  /* Alw
1e61e 61 79 73 20 73 6f 20 62 65 63 61 75 73 65 20 6f  ays so because o
1e61f 66 20 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28 29  f CursorMoveto()
1e620 20 61 62 6f 76 65 20 2a 2f 0a 20 20 20 20 7d 0a   above */.    }.
1e621 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20    }.  pOut->u.i 
1e622 3d 20 75 2e 62 69 2e 76 3b 0a 20 20 4d 65 6d 53  = u.bi.v;.  MemS
1e623 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c  etTypeFlag(pOut,
1e624 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65   MEM_Int);.  bre
1e625 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1e626 3a 20 4e 75 6c 6c 52 6f 77 20 50 31 20 2a 20 2a  : NullRow P1 * *
1e627 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20   * *.**.** Move 
1e628 74 68 65 20 63 75 72 73 6f 72 20 50 31 20 74 6f  the cursor P1 to
1e629 20 61 20 6e 75 6c 6c 20 72 6f 77 2e 20 20 41 6e   a null row.  An
1e62a 79 20 4f 50 5f 43 6f 6c 75 6d 6e 20 6f 70 65 72  y OP_Column oper
1e62b 61 74 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 6f  ations.** that o
1e62c 63 63 75 72 20 77 68 69 6c 65 20 74 68 65 20 63  ccur while the c
1e62d 75 72 73 6f 72 20 69 73 20 6f 6e 20 74 68 65 20  ursor is on the 
1e62e 6e 75 6c 6c 20 72 6f 77 20 77 69 6c 6c 20 61 6c  null row will al
1e62f 77 61 79 73 0a 2a 2a 20 77 72 69 74 65 20 61 20  ways.** write a 
1e630 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  NULL..*/.case OP
1e631 5f 4e 75 6c 6c 52 6f 77 3a 20 7b 0a 23 69 66 20  _NullRow: {.#if 
1e632 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1e633 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e634 20 75 2e 62 6a 20 2a 2f 0a 20 20 56 64 62 65 43   u.bj */.  VdbeC
1e635 75 72 73 6f 72 20 2a 70 43 3b 0a 23 65 6e 64 69  ursor *pC;.#endi
1e636 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1e637 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e638 75 2e 62 6a 20 2a 2f 0a 0a 20 20 61 73 73 65 72  u.bj */..  asser
1e639 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26  t( pOp->p1>=0 &&
1e63a 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72   pOp->p1<p->nCur
1e63b 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6a 2e 70 43  sor );.  u.bj.pC
1e63c 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d   = p->apCsr[pOp-
1e63d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  >p1];.  assert( 
1e63e 75 2e 62 6a 2e 70 43 21 3d 30 20 29 3b 0a 20 20  u.bj.pC!=0 );.  
1e63f 75 2e 62 6a 2e 70 43 2d 3e 6e 75 6c 6c 52 6f 77  u.bj.pC->nullRow
1e640 20 3d 20 31 3b 0a 20 20 75 2e 62 6a 2e 70 43 2d   = 1;.  u.bj.pC-
1e641 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20  >rowidIsValid = 
1e642 30 3b 0a 20 20 69 66 28 20 75 2e 62 6a 2e 70 43  0;.  if( u.bj.pC
1e643 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20 20  ->pCursor ){.   
1e644 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65   sqlite3BtreeCle
1e645 61 72 43 75 72 73 6f 72 28 75 2e 62 6a 2e 70 43  arCursor(u.bj.pC
1e646 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a  ->pCursor);.  }.
1e647 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1e648 70 63 6f 64 65 3a 20 4c 61 73 74 20 50 31 20 50  pcode: Last P1 P
1e649 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68  2 * * *.**.** Th
1e64a 65 20 6e 65 78 74 20 75 73 65 20 6f 66 20 74 68  e next use of th
1e64b 65 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c 75 6d  e Rowid or Colum
1e64c 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73 74 72 75  n or Next instru
1e64d 63 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a 2a 2a  ction for P1 .**
1e64e 20 77 69 6c 6c 20 72 65 66 65 72 20 74 6f 20 74   will refer to t
1e64f 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e  he last entry in
1e650 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61   the database ta
1e651 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a  ble or index..**
1e652 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72   If the table or
1e653 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74 79 20   index is empty 
1e654 61 6e 64 20 50 32 3e 30 2c 20 74 68 65 6e 20 6a  and P2>0, then j
1e655 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  ump immediately 
1e656 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 50 32 20  to P2..** If P2 
1e657 69 73 20 30 20 6f 72 20 69 66 20 74 68 65 20 74  is 0 or if the t
1e658 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 73  able or index is
1e659 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66 61 6c 6c   not empty, fall
1e65a 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20 74   through.** to t
1e65b 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73  he following ins
1e65c 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73  truction..*/.cas
1e65d 65 20 4f 50 5f 4c 61 73 74 3a 20 7b 20 20 20 20  e OP_Last: {    
1e65e 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23      /* jump */.#
1e65f 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1e660 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1e661 6e 74 6f 20 75 2e 62 6b 20 2a 2f 0a 20 20 56 64  nto u.bk */.  Vd
1e662 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20  beCursor *pC;.  
1e663 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b  BtCursor *pCrsr;
1e664 0a 20 20 69 6e 74 20 72 65 73 3b 0a 23 65 6e 64  .  int res;.#end
1e665 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1e666 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e667 20 75 2e 62 6b 20 2a 2f 0a 0a 20 20 61 73 73 65   u.bk */..  asse
1e668 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1e669 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
1e66a 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6b 2e 70  rsor );.  u.bk.p
1e66b 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  C = p->apCsr[pOp
1e66c 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1e66d 20 75 2e 62 6b 2e 70 43 21 3d 30 20 29 3b 0a 20   u.bk.pC!=0 );. 
1e66e 20 75 2e 62 6b 2e 70 43 72 73 72 20 3d 20 75 2e   u.bk.pCrsr = u.
1e66f 62 6b 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a  bk.pC->pCursor;.
1e670 20 20 69 66 28 20 75 2e 62 6b 2e 70 43 72 73 72    if( u.bk.pCrsr
1e671 3d 3d 30 20 29 7b 0a 20 20 20 20 75 2e 62 6b 2e  ==0 ){.    u.bk.
1e672 72 65 73 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  res = 1;.  }else
1e673 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1e674 65 33 42 74 72 65 65 4c 61 73 74 28 75 2e 62 6b  e3BtreeLast(u.bk
1e675 2e 70 43 72 73 72 2c 20 26 75 2e 62 6b 2e 72 65  .pCrsr, &u.bk.re
1e676 73 29 3b 0a 20 20 7d 0a 20 20 75 2e 62 6b 2e 70  s);.  }.  u.bk.p
1e677 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75 38  C->nullRow = (u8
1e678 29 75 2e 62 6b 2e 72 65 73 3b 0a 20 20 75 2e 62  )u.bk.res;.  u.b
1e679 6b 2e 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f  k.pC->deferredMo
1e67a 76 65 74 6f 20 3d 20 30 3b 0a 20 20 75 2e 62 6b  veto = 0;.  u.bk
1e67b 2e 70 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69  .pC->rowidIsVali
1e67c 64 20 3d 20 30 3b 0a 20 20 75 2e 62 6b 2e 70 43  d = 0;.  u.bk.pC
1e67d 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20  ->cacheStatus = 
1e67e 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 69  CACHE_STALE;.  i
1e67f 66 28 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20  f( pOp->p2>0 && 
1e680 75 2e 62 6b 2e 72 65 73 20 29 7b 0a 20 20 20 20  u.bk.res ){.    
1e681 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
1e682 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  ;.  }.  break;.}
1e683 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 6f  .../* Opcode: So
1e684 72 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  rt P1 P2 * * *.*
1e685 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65  *.** This opcode
1e686 20 64 6f 65 73 20 65 78 61 63 74 6c 79 20 74 68   does exactly th
1e687 65 20 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20  e same thing as 
1e688 4f 50 5f 52 65 77 69 6e 64 20 65 78 63 65 70 74  OP_Rewind except
1e689 20 74 68 61 74 0a 2a 2a 20 69 74 20 69 6e 63 72   that.** it incr
1e68a 65 6d 65 6e 74 73 20 61 6e 20 75 6e 64 6f 63 75  ements an undocu
1e68b 6d 65 6e 74 65 64 20 67 6c 6f 62 61 6c 20 76 61  mented global va
1e68c 72 69 61 62 6c 65 20 75 73 65 64 20 66 6f 72 20  riable used for 
1e68d 74 65 73 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53  testing..**.** S
1e68e 6f 72 74 69 6e 67 20 69 73 20 61 63 63 6f 6d 70  orting is accomp
1e68f 6c 69 73 68 65 64 20 62 79 20 77 72 69 74 69 6e  lished by writin
1e690 67 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 61  g records into a
1e691 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 2c 0a   sorting index,.
1e692 2a 2a 20 74 68 65 6e 20 72 65 77 69 6e 64 69 6e  ** then rewindin
1e693 67 20 74 68 61 74 20 69 6e 64 65 78 20 61 6e 64  g that index and
1e694 20 70 6c 61 79 69 6e 67 20 69 74 20 62 61 63 6b   playing it back
1e695 20 66 72 6f 6d 20 62 65 67 69 6e 6e 69 6e 67 20   from beginning 
1e696 74 6f 0a 2a 2a 20 65 6e 64 2e 20 20 57 65 20 75  to.** end.  We u
1e697 73 65 20 74 68 65 20 4f 50 5f 53 6f 72 74 20 6f  se the OP_Sort o
1e698 70 63 6f 64 65 20 69 6e 73 74 65 61 64 20 6f 66  pcode instead of
1e699 20 4f 50 5f 52 65 77 69 6e 64 20 74 6f 20 64 6f   OP_Rewind to do
1e69a 20 74 68 65 0a 2a 2a 20 72 65 77 69 6e 64 69 6e   the.** rewindin
1e69b 67 20 73 6f 20 74 68 61 74 20 74 68 65 20 67 6c  g so that the gl
1e69c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 77 69  obal variable wi
1e69d 6c 6c 20 62 65 20 69 6e 63 72 65 6d 65 6e 74 65  ll be incremente
1e69e 64 20 61 6e 64 0a 2a 2a 20 72 65 67 72 65 73 73  d and.** regress
1e69f 69 6f 6e 20 74 65 73 74 73 20 63 61 6e 20 64 65  ion tests can de
1e6a0 74 65 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20  termine whether 
1e6a1 6f 72 20 6e 6f 74 20 74 68 65 20 6f 70 74 69 6d  or not the optim
1e6a2 69 7a 65 72 20 69 73 0a 2a 2a 20 63 6f 72 72 65  izer is.** corre
1e6a3 63 74 6c 79 20 6f 70 74 69 6d 69 7a 69 6e 67 20  ctly optimizing 
1e6a4 6f 75 74 20 73 6f 72 74 73 2e 0a 2a 2f 0a 63 61  out sorts..*/.ca
1e6a5 73 65 20 4f 50 5f 53 6f 72 74 3a 20 7b 20 20 20  se OP_Sort: {   
1e6a6 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a       /* jump */.
1e6a7 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
1e6a8 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 73 6f 72  ST.  sqlite3_sor
1e6a9 74 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 73 71 6c  t_count++;.  sql
1e6aa 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e  ite3_search_coun
1e6ab 74 2d 2d 3b 0a 23 65 6e 64 69 66 0a 20 20 70 2d  t--;.#endif.  p-
1e6ac 3e 61 43 6f 75 6e 74 65 72 5b 53 51 4c 49 54 45  >aCounter[SQLITE
1e6ad 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54  _STMTSTATUS_SORT
1e6ae 2d 31 5d 2b 2b 3b 0a 20 20 2f 2a 20 46 61 6c 6c  -1]++;.  /* Fall
1e6af 20 74 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f 50   through into OP
1e6b0 5f 52 65 77 69 6e 64 20 2a 2f 0a 7d 0a 2f 2a 20  _Rewind */.}./* 
1e6b1 4f 70 63 6f 64 65 3a 20 52 65 77 69 6e 64 20 50  Opcode: Rewind P
1e6b2 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1e6b3 20 54 68 65 20 6e 65 78 74 20 75 73 65 20 6f 66   The next use of
1e6b4 20 74 68 65 20 52 6f 77 69 64 20 6f 72 20 43 6f   the Rowid or Co
1e6b5 6c 75 6d 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73  lumn or Next ins
1e6b6 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 50 31 20  truction for P1 
1e6b7 0a 2a 2a 20 77 69 6c 6c 20 72 65 66 65 72 20 74  .** will refer t
1e6b8 6f 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72  o the first entr
1e6b9 79 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  y in the databas
1e6ba 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  e table or index
1e6bb 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c  ..** If the tabl
1e6bc 65 20 6f 72 20 69 6e 64 65 78 20 69 73 20 65 6d  e or index is em
1e6bd 70 74 79 20 61 6e 64 20 50 32 3e 30 2c 20 74 68  pty and P2>0, th
1e6be 65 6e 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74  en jump immediat
1e6bf 65 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 66  ely to P2..** If
1e6c0 20 50 32 20 69 73 20 30 20 6f 72 20 69 66 20 74   P2 is 0 or if t
1e6c1 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65  he table or inde
1e6c2 78 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 20  x is not empty, 
1e6c3 66 61 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20  fall through.** 
1e6c4 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  to the following
1e6c5 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f   instruction..*/
1e6c6 0a 63 61 73 65 20 4f 50 5f 52 65 77 69 6e 64 3a  .case OP_Rewind:
1e6c7 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
1e6c8 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c  p */.#if 0  /* l
1e6c9 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1e6ca 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 6c 20 2a  oved into u.bl *
1e6cb 2f 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a  /.  VdbeCursor *
1e6cc 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a  pC;.  BtCursor *
1e6cd 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73  pCrsr;.  int res
1e6ce 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1e6cf 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1e6d0 64 20 69 6e 74 6f 20 75 2e 62 6c 20 2a 2f 0a 0a  d into u.bl */..
1e6d1 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1e6d2 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c  1>=0 && pOp->p1<
1e6d3 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1e6d4 75 2e 62 6c 2e 70 43 20 3d 20 70 2d 3e 61 70 43  u.bl.pC = p->apC
1e6d5 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61  sr[pOp->p1];.  a
1e6d6 73 73 65 72 74 28 20 75 2e 62 6c 2e 70 43 21 3d  ssert( u.bl.pC!=
1e6d7 30 20 29 3b 0a 20 20 69 66 28 20 28 75 2e 62 6c  0 );.  if( (u.bl
1e6d8 2e 70 43 72 73 72 20 3d 20 75 2e 62 6c 2e 70 43  .pCrsr = u.bl.pC
1e6d9 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29 7b  ->pCursor)!=0 ){
1e6da 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1e6db 33 42 74 72 65 65 46 69 72 73 74 28 75 2e 62 6c  3BtreeFirst(u.bl
1e6dc 2e 70 43 72 73 72 2c 20 26 75 2e 62 6c 2e 72 65  .pCrsr, &u.bl.re
1e6dd 73 29 3b 0a 20 20 20 20 75 2e 62 6c 2e 70 43 2d  s);.    u.bl.pC-
1e6de 3e 61 74 46 69 72 73 74 20 3d 20 75 2e 62 6c 2e  >atFirst = u.bl.
1e6df 72 65 73 3d 3d 30 20 3f 31 3a 30 3b 0a 20 20 20  res==0 ?1:0;.   
1e6e0 20 75 2e 62 6c 2e 70 43 2d 3e 64 65 66 65 72 72   u.bl.pC->deferr
1e6e1 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20  edMoveto = 0;.  
1e6e2 20 20 75 2e 62 6c 2e 70 43 2d 3e 63 61 63 68 65    u.bl.pC->cache
1e6e3 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53  Status = CACHE_S
1e6e4 54 41 4c 45 3b 0a 20 20 20 20 75 2e 62 6c 2e 70  TALE;.    u.bl.p
1e6e5 43 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 20  C->rowidIsValid 
1e6e6 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 0;.  }else{.  
1e6e7 20 20 75 2e 62 6c 2e 72 65 73 20 3d 20 31 3b 0a    u.bl.res = 1;.
1e6e8 20 20 7d 0a 20 20 75 2e 62 6c 2e 70 43 2d 3e 6e    }.  u.bl.pC->n
1e6e9 75 6c 6c 52 6f 77 20 3d 20 28 75 38 29 75 2e 62  ullRow = (u8)u.b
1e6ea 6c 2e 72 65 73 3b 0a 20 20 61 73 73 65 72 74 28  l.res;.  assert(
1e6eb 20 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f   pOp->p2>0 && pO
1e6ec 70 2d 3e 70 32 3c 70 2d 3e 6e 4f 70 20 29 3b 0a  p->p2<p->nOp );.
1e6ed 20 20 69 66 28 20 75 2e 62 6c 2e 72 65 73 20 29    if( u.bl.res )
1e6ee 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e  {.    pc = pOp->
1e6ef 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72  p2 - 1;.  }.  br
1e6f0 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1e6f1 65 3a 20 4e 65 78 74 20 50 31 20 50 32 20 2a 20  e: Next P1 P2 * 
1e6f2 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 76 61 6e 63  * *.**.** Advanc
1e6f3 65 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74  e cursor P1 so t
1e6f4 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74 6f  hat it points to
1e6f5 20 74 68 65 20 6e 65 78 74 20 6b 65 79 2f 64 61   the next key/da
1e6f6 74 61 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a  ta pair in its.*
1e6f7 2a 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  * table or index
1e6f8 2e 20 20 49 66 20 74 68 65 72 65 20 61 72 65 20  .  If there are 
1e6f9 6e 6f 20 6d 6f 72 65 20 6b 65 79 2f 76 61 6c 75  no more key/valu
1e6fa 65 20 70 61 69 72 73 20 74 68 65 6e 20 66 61 6c  e pairs then fal
1e6fb 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20  l through.** to 
1e6fc 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e  the following in
1e6fd 73 74 72 75 63 74 69 6f 6e 2e 20 20 42 75 74 20  struction.  But 
1e6fe 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 61 64  if the cursor ad
1e6ff 76 61 6e 63 65 20 77 61 73 20 73 75 63 63 65 73  vance was succes
1e700 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69 6d  sful,.** jump im
1e701 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e  mediately to P2.
1e702 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63 75  .**.** The P1 cu
1e703 72 73 6f 72 20 6d 75 73 74 20 62 65 20 66 6f 72  rsor must be for
1e704 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e   a real table, n
1e705 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c  ot a pseudo-tabl
1e706 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  e..**.** See als
1e707 6f 3a 20 50 72 65 76 0a 2a 2f 0a 2f 2a 20 4f 70  o: Prev.*/./* Op
1e708 63 6f 64 65 3a 20 50 72 65 76 20 50 31 20 50 32  code: Prev P1 P2
1e709 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 61 63   * * *.**.** Bac
1e70a 6b 20 75 70 20 63 75 72 73 6f 72 20 50 31 20 73  k up cursor P1 s
1e70b 6f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73  o that it points
1e70c 20 74 6f 20 74 68 65 20 70 72 65 76 69 6f 75 73   to the previous
1e70d 20 6b 65 79 2f 64 61 74 61 20 70 61 69 72 20 69   key/data pair i
1e70e 6e 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 6f  n its.** table o
1e70f 72 20 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65  r index.  If the
1e710 72 65 20 69 73 20 6e 6f 20 70 72 65 76 69 6f 75  re is no previou
1e711 73 20 6b 65 79 2f 76 61 6c 75 65 20 70 61 69 72  s key/value pair
1e712 73 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f  s then fall thro
1e713 75 67 68 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f  ugh.** to the fo
1e714 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72 75 63 74  llowing instruct
1e715 69 6f 6e 2e 20 20 42 75 74 20 69 66 20 74 68 65  ion.  But if the
1e716 20 63 75 72 73 6f 72 20 62 61 63 6b 75 70 20 77   cursor backup w
1e717 61 73 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a  as successful,.*
1e718 2a 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65  * jump immediate
1e719 6c 79 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  ly to P2..**.** 
1e71a 54 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75  The P1 cursor mu
1e71b 73 74 20 62 65 20 66 6f 72 20 61 20 72 65 61 6c  st be for a real
1e71c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73   table, not a ps
1e71d 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a 63  eudo-table..*/.c
1e71e 61 73 65 20 4f 50 5f 50 72 65 76 3a 20 20 20 20  ase OP_Prev:    
1e71f 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f        /* jump */
1e720 0a 63 61 73 65 20 4f 50 5f 4e 65 78 74 3a 20 7b  .case OP_Next: {
1e721 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20          /* jump 
1e722 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
1e723 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e724 65 64 20 69 6e 74 6f 20 75 2e 62 6d 20 2a 2f 0a  ed into u.bm */.
1e725 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1e726 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  ;.  BtCursor *pC
1e727 72 73 72 3b 0a 20 20 69 6e 74 20 72 65 73 3b 0a  rsr;.  int res;.
1e728 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1e729 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1e72a 69 6e 74 6f 20 75 2e 62 6d 20 2a 2f 0a 0a 20 20  into u.bm */..  
1e72b 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52  CHECK_FOR_INTERR
1e72c 55 50 54 3b 0a 20 20 61 73 73 65 72 74 28 20 70  UPT;.  assert( p
1e72d 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70  Op->p1>=0 && pOp
1e72e 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  ->p1<p->nCursor 
1e72f 29 3b 0a 20 20 75 2e 62 6d 2e 70 43 20 3d 20 70  );.  u.bm.pC = p
1e730 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d  ->apCsr[pOp->p1]
1e731 3b 0a 20 20 69 66 28 20 75 2e 62 6d 2e 70 43 3d  ;.  if( u.bm.pC=
1e732 3d 30 20 29 7b 0a 20 20 20 20 62 72 65 61 6b 3b  =0 ){.    break;
1e733 20 20 2f 2a 20 53 65 65 20 74 69 63 6b 65 74 20    /* See ticket 
1e734 23 32 32 37 33 20 2a 2f 0a 20 20 7d 0a 20 20 75  #2273 */.  }.  u
1e735 2e 62 6d 2e 70 43 72 73 72 20 3d 20 75 2e 62 6d  .bm.pCrsr = u.bm
1e736 2e 70 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20  .pC->pCursor;.  
1e737 69 66 28 20 75 2e 62 6d 2e 70 43 72 73 72 3d 3d  if( u.bm.pCrsr==
1e738 30 20 29 7b 0a 20 20 20 20 75 2e 62 6d 2e 70 43  0 ){.    u.bm.pC
1e739 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20  ->nullRow = 1;. 
1e73a 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20     break;.  }.  
1e73b 75 2e 62 6d 2e 72 65 73 20 3d 20 31 3b 0a 20 20  u.bm.res = 1;.  
1e73c 61 73 73 65 72 74 28 20 75 2e 62 6d 2e 70 43 2d  assert( u.bm.pC-
1e73d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d  >deferredMoveto=
1e73e 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 70 4f 70  =0 );.  rc = pOp
1e73f 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 65 78  ->opcode==OP_Nex
1e740 74 20 3f 20 73 71 6c 69 74 65 33 42 74 72 65 65  t ? sqlite3Btree
1e741 4e 65 78 74 28 75 2e 62 6d 2e 70 43 72 73 72 2c  Next(u.bm.pCrsr,
1e742 20 26 75 2e 62 6d 2e 72 65 73 29 20 3a 0a 20 20   &u.bm.res) :.  
1e743 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e744 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
1e745 74 65 33 42 74 72 65 65 50 72 65 76 69 6f 75 73  te3BtreePrevious
1e746 28 75 2e 62 6d 2e 70 43 72 73 72 2c 20 26 75 2e  (u.bm.pCrsr, &u.
1e747 62 6d 2e 72 65 73 29 3b 0a 20 20 75 2e 62 6d 2e  bm.res);.  u.bm.
1e748 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 28 75  pC->nullRow = (u
1e749 38 29 75 2e 62 6d 2e 72 65 73 3b 0a 20 20 75 2e  8)u.bm.res;.  u.
1e74a 62 6d 2e 70 43 2d 3e 63 61 63 68 65 53 74 61 74  bm.pC->cacheStat
1e74b 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45  us = CACHE_STALE
1e74c 3b 0a 20 20 69 66 28 20 75 2e 62 6d 2e 72 65 73  ;.  if( u.bm.res
1e74d 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63 20 3d 20  ==0 ){.    pc = 
1e74e 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20  pOp->p2 - 1;.   
1e74f 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 20 70   if( pOp->p5 ) p
1e750 2d 3e 61 43 6f 75 6e 74 65 72 5b 70 4f 70 2d 3e  ->aCounter[pOp->
1e751 70 35 2d 31 5d 2b 2b 3b 0a 23 69 66 64 65 66 20  p5-1]++;.#ifdef 
1e752 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 20  SQLITE_TEST.    
1e753 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63  sqlite3_search_c
1e754 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20  ount++;.#endif. 
1e755 20 7d 0a 20 20 75 2e 62 6d 2e 70 43 2d 3e 72 6f   }.  u.bm.pC->ro
1e756 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a  widIsValid = 0;.
1e757 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1e758 70 63 6f 64 65 3a 20 49 64 78 49 6e 73 65 72 74  pcode: IdxInsert
1e759 20 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a 2a   P1 P2 P3 * P5.*
1e75a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 32  *.** Register P2
1e75b 20 68 6f 6c 64 73 20 61 20 53 51 4c 20 69 6e 64   holds a SQL ind
1e75c 65 78 20 6b 65 79 20 6d 61 64 65 20 75 73 69 6e  ex key made usin
1e75d 67 20 74 68 65 0a 2a 2a 20 4d 61 6b 65 52 65 63  g the.** MakeRec
1e75e 6f 72 64 20 69 6e 73 74 72 75 63 74 69 6f 6e 73  ord instructions
1e75f 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77  .  This opcode w
1e760 72 69 74 65 73 20 74 68 61 74 20 6b 65 79 0a 2a  rites that key.*
1e761 2a 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 65 78  * into the index
1e762 20 50 31 2e 20 20 44 61 74 61 20 66 6f 72 20 74   P1.  Data for t
1e763 68 65 20 65 6e 74 72 79 20 69 73 20 6e 69 6c 2e  he entry is nil.
1e764 0a 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 61 20 66  .**.** P3 is a f
1e765 6c 61 67 20 74 68 61 74 20 70 72 6f 76 69 64 65  lag that provide
1e766 73 20 61 20 68 69 6e 74 20 74 6f 20 74 68 65 20  s a hint to the 
1e767 62 2d 74 72 65 65 20 6c 61 79 65 72 20 74 68 61  b-tree layer tha
1e768 74 20 74 68 69 73 0a 2a 2a 20 69 6e 73 65 72 74  t this.** insert
1e769 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65   is likely to be
1e76a 20 61 6e 20 61 70 70 65 6e 64 2e 0a 2a 2a 0a 2a   an append..**.*
1e76b 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69  * This instructi
1e76c 6f 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f  on only works fo
1e76d 72 20 69 6e 64 69 63 65 73 2e 20 20 54 68 65 20  r indices.  The 
1e76e 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 73 74 72  equivalent instr
1e76f 75 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 61  uction.** for ta
1e770 62 6c 65 73 20 69 73 20 4f 50 5f 49 6e 73 65 72  bles is OP_Inser
1e771 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64  t..*/.case OP_Id
1e772 78 49 6e 73 65 72 74 3a 20 7b 20 20 20 20 20 20  xInsert: {      
1e773 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a 23 69 66 20    /* in2 */.#if 
1e774 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1e775 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e776 20 75 2e 62 6e 20 2a 2f 0a 20 20 56 64 62 65 43   u.bn */.  VdbeC
1e777 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43  ursor *pC;.  BtC
1e778 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20  ursor *pCrsr;.  
1e779 69 6e 74 20 6e 4b 65 79 3b 0a 20 20 63 6f 6e 73  int nKey;.  cons
1e77a 74 20 63 68 61 72 20 2a 7a 4b 65 79 3b 0a 23 65  t char *zKey;.#e
1e77b 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1e77c 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e77d 74 6f 20 75 2e 62 6e 20 2a 2f 0a 0a 20 20 61 73  to u.bn */..  as
1e77e 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30  sert( pOp->p1>=0
1e77f 20 26 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e   && pOp->p1<p->n
1e780 43 75 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 6e  Cursor );.  u.bn
1e781 2e 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  .pC = p->apCsr[p
1e782 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
1e783 74 28 20 75 2e 62 6e 2e 70 43 21 3d 30 20 29 3b  t( u.bn.pC!=0 );
1e784 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 32 2d  .  assert( pIn2-
1e785 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f  >flags & MEM_Blo
1e786 62 20 29 3b 0a 20 20 75 2e 62 6e 2e 70 43 72 73  b );.  u.bn.pCrs
1e787 72 20 3d 20 75 2e 62 6e 2e 70 43 2d 3e 70 43 75  r = u.bn.pC->pCu
1e788 72 73 6f 72 3b 0a 20 20 69 66 28 20 41 4c 57 41  rsor;.  if( ALWA
1e789 59 53 28 75 2e 62 6e 2e 70 43 72 73 72 21 3d 30  YS(u.bn.pCrsr!=0
1e78a 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ) ){.    assert(
1e78b 20 75 2e 62 6e 2e 70 43 2d 3e 69 73 54 61 62 6c   u.bn.pC->isTabl
1e78c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d  e==0 );.    rc =
1e78d 20 45 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 32   ExpandBlob(pIn2
1e78e 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
1e78f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1e790 20 20 75 2e 62 6e 2e 6e 4b 65 79 20 3d 20 70 49    u.bn.nKey = pI
1e791 6e 32 2d 3e 6e 3b 0a 20 20 20 20 20 20 75 2e 62  n2->n;.      u.b
1e792 6e 2e 7a 4b 65 79 20 3d 20 70 49 6e 32 2d 3e 7a  n.zKey = pIn2->z
1e793 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
1e794 69 74 65 33 42 74 72 65 65 49 6e 73 65 72 74 28  ite3BtreeInsert(
1e795 75 2e 62 6e 2e 70 43 72 73 72 2c 20 75 2e 62 6e  u.bn.pCrsr, u.bn
1e796 2e 7a 4b 65 79 2c 20 75 2e 62 6e 2e 6e 4b 65 79  .zKey, u.bn.nKey
1e797 2c 20 22 22 2c 20 30 2c 20 30 2c 20 70 4f 70 2d  , "", 0, 0, pOp-
1e798 3e 70 33 2c 0a 20 20 20 20 20 20 20 20 20 20 28  >p3,.          (
1e799 28 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41  (pOp->p5 & OPFLA
1e79a 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c 54 29  G_USESEEKRESULT)
1e79b 20 3f 20 75 2e 62 6e 2e 70 43 2d 3e 73 65 65 6b   ? u.bn.pC->seek
1e79c 52 65 73 75 6c 74 20 3a 20 30 29 0a 20 20 20 20  Result : 0).    
1e79d 20 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72    );.      asser
1e79e 74 28 20 75 2e 62 6e 2e 70 43 2d 3e 64 65 66 65  t( u.bn.pC->defe
1e79f 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b  rredMoveto==0 );
1e7a0 0a 20 20 20 20 20 20 75 2e 62 6e 2e 70 43 2d 3e  .      u.bn.pC->
1e7a1 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41  cacheStatus = CA
1e7a2 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 7d  CHE_STALE;.    }
1e7a3 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1e7a4 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 44  ./* Opcode: IdxD
1e7a5 65 6c 65 74 65 20 50 31 20 50 32 20 50 33 20 2a  elete P1 P2 P3 *
1e7a6 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e   *.**.** The con
1e7a7 74 65 6e 74 20 6f 66 20 50 33 20 72 65 67 69 73  tent of P3 regis
1e7a8 74 65 72 73 20 73 74 61 72 74 69 6e 67 20 61 74  ters starting at
1e7a9 20 72 65 67 69 73 74 65 72 20 50 32 20 66 6f 72   register P2 for
1e7aa 6d 0a 2a 2a 20 61 6e 20 75 6e 70 61 63 6b 65 64  m.** an unpacked
1e7ab 20 69 6e 64 65 78 20 6b 65 79 2e 20 54 68 69 73   index key. This
1e7ac 20 6f 70 63 6f 64 65 20 72 65 6d 6f 76 65 73 20   opcode removes 
1e7ad 74 68 61 74 20 65 6e 74 72 79 20 66 72 6f 6d 20  that entry from 
1e7ae 74 68 65 20 0a 2a 2a 20 69 6e 64 65 78 20 6f 70  the .** index op
1e7af 65 6e 65 64 20 62 79 20 63 75 72 73 6f 72 20 50  ened by cursor P
1e7b0 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64  1..*/.case OP_Id
1e7b1 78 44 65 6c 65 74 65 3a 20 7b 0a 23 69 66 20 30  xDelete: {.#if 0
1e7b2 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1e7b3 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e7b4 75 2e 62 6f 20 2a 2f 0a 20 20 56 64 62 65 43 75  u.bo */.  VdbeCu
1e7b5 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75  rsor *pC;.  BtCu
1e7b6 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69  rsor *pCrsr;.  i
1e7b7 6e 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63 6b  nt res;.  Unpack
1e7b8 65 64 52 65 63 6f 72 64 20 72 3b 0a 23 65 6e 64  edRecord r;.#end
1e7b9 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1e7ba 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e7bb 20 75 2e 62 6f 20 2a 2f 0a 0a 20 20 61 73 73 65   u.bo */..  asse
1e7bc 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b  rt( pOp->p3>0 );
1e7bd 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1e7be 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b  p2>0 && pOp->p2+
1e7bf 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p3<=p->nMem
1e7c0 2b 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  +1 );.  assert( 
1e7c1 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f  pOp->p1>=0 && pO
1e7c2 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72  p->p1<p->nCursor
1e7c3 20 29 3b 0a 20 20 75 2e 62 6f 2e 70 43 20 3d 20   );.  u.bo.pC = 
1e7c4 70 2d 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31  p->apCsr[pOp->p1
1e7c5 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62  ];.  assert( u.b
1e7c6 6f 2e 70 43 21 3d 30 20 29 3b 0a 20 20 75 2e 62  o.pC!=0 );.  u.b
1e7c7 6f 2e 70 43 72 73 72 20 3d 20 75 2e 62 6f 2e 70  o.pCrsr = u.bo.p
1e7c8 43 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 69 66  C->pCursor;.  if
1e7c9 28 20 41 4c 57 41 59 53 28 75 2e 62 6f 2e 70 43  ( ALWAYS(u.bo.pC
1e7ca 72 73 72 21 3d 30 29 20 29 7b 0a 20 20 20 20 75  rsr!=0) ){.    u
1e7cb 2e 62 6f 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d  .bo.r.pKeyInfo =
1e7cc 20 75 2e 62 6f 2e 70 43 2d 3e 70 4b 65 79 49 6e   u.bo.pC->pKeyIn
1e7cd 66 6f 3b 0a 20 20 20 20 75 2e 62 6f 2e 72 2e 6e  fo;.    u.bo.r.n
1e7ce 46 69 65 6c 64 20 3d 20 28 75 31 36 29 70 4f 70  Field = (u16)pOp
1e7cf 2d 3e 70 33 3b 0a 20 20 20 20 75 2e 62 6f 2e 72  ->p3;.    u.bo.r
1e7d0 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20 20 20  .flags = 0;.    
1e7d1 75 2e 62 6f 2e 72 2e 61 4d 65 6d 20 3d 20 26 70  u.bo.r.aMem = &p
1e7d2 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b  ->aMem[pOp->p2];
1e7d3 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1e7d4 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61  3BtreeMovetoUnpa
1e7d5 63 6b 65 64 28 75 2e 62 6f 2e 70 43 72 73 72 2c  cked(u.bo.pCrsr,
1e7d6 20 26 75 2e 62 6f 2e 72 2c 20 30 2c 20 30 2c 20   &u.bo.r, 0, 0, 
1e7d7 26 75 2e 62 6f 2e 72 65 73 29 3b 0a 20 20 20 20  &u.bo.res);.    
1e7d8 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1e7d9 4b 20 26 26 20 75 2e 62 6f 2e 72 65 73 3d 3d 30  K && u.bo.res==0
1e7da 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
1e7db 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74  qlite3BtreeDelet
1e7dc 65 28 75 2e 62 6f 2e 70 43 72 73 72 29 3b 0a 20  e(u.bo.pCrsr);. 
1e7dd 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
1e7de 20 75 2e 62 6f 2e 70 43 2d 3e 64 65 66 65 72 72   u.bo.pC->deferr
1e7df 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20  edMoveto==0 );. 
1e7e0 20 20 20 75 2e 62 6f 2e 70 43 2d 3e 63 61 63 68     u.bo.pC->cach
1e7e1 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f  eStatus = CACHE_
1e7e2 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 62 72 65  STALE;.  }.  bre
1e7e3 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1e7e4 3a 20 49 64 78 52 6f 77 69 64 20 50 31 20 50 32  : IdxRowid P1 P2
1e7e5 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69   * * *.**.** Wri
1e7e6 74 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  te into register
1e7e7 20 50 32 20 61 6e 20 69 6e 74 65 67 65 72 20 77   P2 an integer w
1e7e8 68 69 63 68 20 69 73 20 74 68 65 20 6c 61 73 74  hich is the last
1e7e9 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 72 65   entry in the re
1e7ea 63 6f 72 64 20 61 74 0a 2a 2a 20 74 68 65 20 65  cord at.** the e
1e7eb 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  nd of the index 
1e7ec 6b 65 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  key pointed to b
1e7ed 79 20 63 75 72 73 6f 72 20 50 31 2e 20 20 54 68  y cursor P1.  Th
1e7ee 69 73 20 69 6e 74 65 67 65 72 20 73 68 6f 75 6c  is integer shoul
1e7ef 64 20 62 65 0a 2a 2a 20 74 68 65 20 72 6f 77 69  d be.** the rowi
1e7f0 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 65  d of the table e
1e7f1 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 74 68  ntry to which th
1e7f2 69 73 20 69 6e 64 65 78 20 65 6e 74 72 79 20 70  is index entry p
1e7f3 6f 69 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  oints..**.** See
1e7f4 20 61 6c 73 6f 3a 20 52 6f 77 69 64 2c 20 4d 61   also: Rowid, Ma
1e7f5 6b 65 52 65 63 6f 72 64 2e 0a 2a 2f 0a 63 61 73  keRecord..*/.cas
1e7f6 65 20 4f 50 5f 49 64 78 52 6f 77 69 64 3a 20 7b  e OP_IdxRowid: {
1e7f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1e7f8 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65   out2-prerelease
1e7f9 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f   */.#if 0  /* lo
1e7fa 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f  cal variables mo
1e7fb 76 65 64 20 69 6e 74 6f 20 75 2e 62 70 20 2a 2f  ved into u.bp */
1e7fc 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72  .  BtCursor *pCr
1e7fd 73 72 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72  sr;.  VdbeCursor
1e7fe 20 2a 70 43 3b 0a 20 20 69 36 34 20 72 6f 77 69   *pC;.  i64 rowi
1e7ff 64 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63  d;.#endif /* loc
1e800 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1e801 65 64 20 69 6e 74 6f 20 75 2e 62 70 20 2a 2f 0a  ed into u.bp */.
1e802 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1e803 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31  p1>=0 && pOp->p1
1e804 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1e805 20 75 2e 62 70 2e 70 43 20 3d 20 70 2d 3e 61 70   u.bp.pC = p->ap
1e806 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1e807 61 73 73 65 72 74 28 20 75 2e 62 70 2e 70 43 21  assert( u.bp.pC!
1e808 3d 30 20 29 3b 0a 20 20 75 2e 62 70 2e 70 43 72  =0 );.  u.bp.pCr
1e809 73 72 20 3d 20 75 2e 62 70 2e 70 43 2d 3e 70 43  sr = u.bp.pC->pC
1e80a 75 72 73 6f 72 3b 0a 20 20 69 66 28 20 41 4c 57  ursor;.  if( ALW
1e80b 41 59 53 28 75 2e 62 70 2e 70 43 72 73 72 21 3d  AYS(u.bp.pCrsr!=
1e80c 30 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  0) ){.    rc = s
1e80d 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72  qlite3VdbeCursor
1e80e 4d 6f 76 65 74 6f 28 75 2e 62 70 2e 70 43 29 3b  Moveto(u.bp.pC);
1e80f 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72  .    if( NEVER(r
1e810 63 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  c) ) goto abort_
1e811 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20  due_to_error;.  
1e812 20 20 61 73 73 65 72 74 28 20 75 2e 62 70 2e 70    assert( u.bp.p
1e813 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
1e814 6f 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  o==0 );.    asse
1e815 72 74 28 20 75 2e 62 70 2e 70 43 2d 3e 69 73 54  rt( u.bp.pC->isT
1e816 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 69  able==0 );.    i
1e817 66 28 20 21 75 2e 62 70 2e 70 43 2d 3e 6e 75 6c  f( !u.bp.pC->nul
1e818 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 72 63  lRow ){.      rc
1e819 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 64   = sqlite3VdbeId
1e81a 78 52 6f 77 69 64 28 64 62 2c 20 75 2e 62 70 2e  xRowid(db, u.bp.
1e81b 70 43 72 73 72 2c 20 26 75 2e 62 70 2e 72 6f 77  pCrsr, &u.bp.row
1e81c 69 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  id);.      if( r
1e81d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1e81e 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f          goto abo
1e81f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
1e820 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 4d  .      }.      M
1e821 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f  emSetTypeFlag(pO
1e822 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20  ut, MEM_Int);.  
1e823 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20      pOut->u.i = 
1e824 75 2e 62 70 2e 72 6f 77 69 64 3b 0a 20 20 20 20  u.bp.rowid;.    
1e825 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  }.  }.  break;.}
1e826 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78  ../* Opcode: Idx
1e827 47 45 20 50 31 20 50 32 20 50 33 20 50 34 20 50  GE P1 P2 P3 P4 P
1e828 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 34 20 72  5.**.** The P4 r
1e829 65 67 69 73 74 65 72 20 76 61 6c 75 65 73 20 62  egister values b
1e82a 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 50 33  eginning with P3
1e82b 20 66 6f 72 6d 20 61 6e 20 75 6e 70 61 63 6b 65   form an unpacke
1e82c 64 20 69 6e 64 65 78 20 0a 2a 2a 20 6b 65 79 20  d index .** key 
1e82d 74 68 61 74 20 6f 6d 69 74 73 20 74 68 65 20 52  that omits the R
1e82e 4f 57 49 44 2e 20 20 43 6f 6d 70 61 72 65 20 74  OWID.  Compare t
1e82f 68 69 73 20 6b 65 79 20 76 61 6c 75 65 20 61 67  his key value ag
1e830 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65 78 20  ainst the index 
1e831 0a 2a 2a 20 74 68 61 74 20 50 31 20 69 73 20 63  .** that P1 is c
1e832 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e  urrently pointin
1e833 67 20 74 6f 2c 20 69 67 6e 6f 72 69 6e 67 20 74  g to, ignoring t
1e834 68 65 20 52 4f 57 49 44 20 6f 6e 20 74 68 65 20  he ROWID on the 
1e835 50 31 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20  P1 index..**.** 
1e836 49 66 20 74 68 65 20 50 31 20 69 6e 64 65 78 20  If the P1 index 
1e837 65 6e 74 72 79 20 69 73 20 67 72 65 61 74 65 72  entry is greater
1e838 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
1e839 6f 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 0a  o the key value.
1e83a 2a 2a 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20  ** then jump to 
1e83b 50 32 2e 20 20 4f 74 68 65 72 77 69 73 65 20 66  P2.  Otherwise f
1e83c 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74  all through to t
1e83d 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74  he next instruct
1e83e 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35  ion..**.** If P5
1e83f 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65   is non-zero the
1e840 6e 20 74 68 65 20 6b 65 79 20 76 61 6c 75 65 20  n the key value 
1e841 69 73 20 69 6e 63 72 65 61 73 65 64 20 62 79 20  is increased by 
1e842 61 6e 20 65 70 73 69 6c 6f 6e 20 0a 2a 2a 20 70  an epsilon .** p
1e843 72 69 6f 72 20 74 6f 20 74 68 65 20 63 6f 6d 70  rior to the comp
1e844 61 72 69 73 6f 6e 2e 20 20 54 68 69 73 20 6d 61  arison.  This ma
1e845 6b 65 20 74 68 65 20 6f 70 63 6f 64 65 20 77 6f  ke the opcode wo
1e846 72 6b 20 6c 69 6b 65 20 49 64 78 47 54 20 65 78  rk like IdxGT ex
1e847 63 65 70 74 0a 2a 2a 20 74 68 61 74 20 69 66 20  cept.** that if 
1e848 74 68 65 20 6b 65 79 20 66 72 6f 6d 20 72 65 67  the key from reg
1e849 69 73 74 65 72 20 50 33 20 69 73 20 61 20 70 72  ister P3 is a pr
1e84a 65 66 69 78 20 6f 66 20 74 68 65 20 6b 65 79 20  efix of the key 
1e84b 69 6e 20 74 68 65 20 63 75 72 73 6f 72 2c 0a 2a  in the cursor,.*
1e84c 2a 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  * the result is 
1e84d 66 61 6c 73 65 20 77 68 65 72 65 61 73 20 69 74  false whereas it
1e84e 20 77 6f 75 6c 64 20 62 65 20 74 72 75 65 20 77   would be true w
1e84f 69 74 68 20 49 64 78 47 54 2e 0a 2a 2f 0a 2f 2a  ith IdxGT..*/./*
1e850 20 4f 70 63 6f 64 65 3a 20 49 64 78 4c 54 20 50   Opcode: IdxLT P
1e851 31 20 50 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a  1 P2 P3 * P5.**.
1e852 2a 2a 20 54 68 65 20 50 34 20 72 65 67 69 73 74  ** The P4 regist
1e853 65 72 20 76 61 6c 75 65 73 20 62 65 67 69 6e 6e  er values beginn
1e854 69 6e 67 20 77 69 74 68 20 50 33 20 66 6f 72 6d  ing with P3 form
1e855 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64   an unpacked ind
1e856 65 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61 74 20  ex .** key that 
1e857 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 49 44 2e  omits the ROWID.
1e858 20 20 43 6f 6d 70 61 72 65 20 74 68 69 73 20 6b    Compare this k
1e859 65 79 20 76 61 6c 75 65 20 61 67 61 69 6e 73 74  ey value against
1e85a 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a 20 74   the index .** t
1e85b 68 61 74 20 50 31 20 69 73 20 63 75 72 72 65 6e  hat P1 is curren
1e85c 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2c  tly pointing to,
1e85d 20 69 67 6e 6f 72 69 6e 67 20 74 68 65 20 52 4f   ignoring the RO
1e85e 57 49 44 20 6f 6e 20 74 68 65 20 50 31 20 69 6e  WID on the P1 in
1e85f 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  dex..**.** If th
1e860 65 20 50 31 20 69 6e 64 65 78 20 65 6e 74 72 79  e P1 index entry
1e861 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68   is less than th
1e862 65 20 6b 65 79 20 76 61 6c 75 65 20 74 68 65 6e  e key value then
1e863 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 20   jump to P2..** 
1e864 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c 20 74  Otherwise fall t
1e865 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65  hrough to the ne
1e866 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  xt instruction..
1e867 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73 20 6e  **.** If P5 is n
1e868 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 65  on-zero then the
1e869 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 69 6e   key value is in
1e86a 63 72 65 61 73 65 64 20 62 79 20 61 6e 20 65 70  creased by an ep
1e86b 73 69 6c 6f 6e 20 70 72 69 6f 72 20 0a 2a 2a 20  silon prior .** 
1e86c 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f  to the compariso
1e86d 6e 2e 20 20 54 68 69 73 20 6d 61 6b 65 73 20 74  n.  This makes t
1e86e 68 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20 6c  he opcode work l
1e86f 69 6b 65 20 49 64 78 4c 45 2e 0a 2a 2f 0a 63 61  ike IdxLE..*/.ca
1e870 73 65 20 4f 50 5f 49 64 78 4c 54 3a 20 20 20 20  se OP_IdxLT:    
1e871 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69        /* jump, i
1e872 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64  n3 */.case OP_Id
1e873 78 47 45 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a  xGE: {        /*
1e874 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a 23 69   jump, in3 */.#i
1e875 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  f 0  /* local va
1e876 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1e877 74 6f 20 75 2e 62 71 20 2a 2f 0a 20 20 56 64 62  to u.bq */.  Vdb
1e878 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 69  eCursor *pC;.  i
1e879 6e 74 20 72 65 73 3b 0a 20 20 55 6e 70 61 63 6b  nt res;.  Unpack
1e87a 65 64 52 65 63 6f 72 64 20 72 3b 0a 23 65 6e 64  edRecord r;.#end
1e87b 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1e87c 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e87d 20 75 2e 62 71 20 2a 2f 0a 0a 20 20 61 73 73 65   u.bq */..  asse
1e87e 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26  rt( pOp->p1>=0 &
1e87f 26 20 70 4f 70 2d 3e 70 31 3c 70 2d 3e 6e 43 75  & pOp->p1<p->nCu
1e880 72 73 6f 72 20 29 3b 0a 20 20 75 2e 62 71 2e 70  rsor );.  u.bq.p
1e881 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  C = p->apCsr[pOp
1e882 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1e883 20 75 2e 62 71 2e 70 43 21 3d 30 20 29 3b 0a 20   u.bq.pC!=0 );. 
1e884 20 69 66 28 20 41 4c 57 41 59 53 28 75 2e 62 71   if( ALWAYS(u.bq
1e885 2e 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 29  .pC->pCursor!=0)
1e886 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
1e887 75 2e 62 71 2e 70 43 2d 3e 64 65 66 65 72 72 65  u.bq.pC->deferre
1e888 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20  dMoveto==0 );.  
1e889 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1e88a 35 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 35 3d  5==0 || pOp->p5=
1e88b 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =1 );.    assert
1e88c 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1e88d 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 75  4_INT32 );.    u
1e88e 2e 62 71 2e 72 2e 70 4b 65 79 49 6e 66 6f 20 3d  .bq.r.pKeyInfo =
1e88f 20 75 2e 62 71 2e 70 43 2d 3e 70 4b 65 79 49 6e   u.bq.pC->pKeyIn
1e890 66 6f 3b 0a 20 20 20 20 75 2e 62 71 2e 72 2e 6e  fo;.    u.bq.r.n
1e891 46 69 65 6c 64 20 3d 20 28 75 31 36 29 70 4f 70  Field = (u16)pOp
1e892 2d 3e 70 34 2e 69 3b 0a 20 20 20 20 69 66 28 20  ->p4.i;.    if( 
1e893 70 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 20  pOp->p5 ){.     
1e894 20 75 2e 62 71 2e 72 2e 66 6c 61 67 73 20 3d 20   u.bq.r.flags = 
1e895 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59  UNPACKED_INCRKEY
1e896 20 7c 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f   | UNPACKED_IGNO
1e897 52 45 5f 52 4f 57 49 44 3b 0a 20 20 20 20 7d 65  RE_ROWID;.    }e
1e898 6c 73 65 7b 0a 20 20 20 20 20 20 75 2e 62 71 2e  lse{.      u.bq.
1e899 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b  r.flags = UNPACK
1e89a 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 3b  ED_IGNORE_ROWID;
1e89b 0a 20 20 20 20 7d 0a 20 20 20 20 75 2e 62 71 2e  .    }.    u.bq.
1e89c 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65  r.aMem = &p->aMe
1e89d 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20  m[pOp->p3];.    
1e89e 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rc = sqlite3Vdbe
1e89f 49 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 75 2e  IdxKeyCompare(u.
1e8a0 62 71 2e 70 43 2c 20 26 75 2e 62 71 2e 72 2c 20  bq.pC, &u.bq.r, 
1e8a1 26 75 2e 62 71 2e 72 65 73 29 3b 0a 20 20 20 20  &u.bq.res);.    
1e8a2 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
1e8a3 3d 4f 50 5f 49 64 78 4c 54 20 29 7b 0a 20 20 20  =OP_IdxLT ){.   
1e8a4 20 20 20 75 2e 62 71 2e 72 65 73 20 3d 20 2d 75     u.bq.res = -u
1e8a5 2e 62 71 2e 72 65 73 3b 0a 20 20 20 20 7d 65 6c  .bq.res;.    }el
1e8a6 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
1e8a7 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
1e8a8 50 5f 49 64 78 47 45 20 29 3b 0a 20 20 20 20 20  P_IdxGE );.     
1e8a9 20 75 2e 62 71 2e 72 65 73 2b 2b 3b 0a 20 20 20   u.bq.res++;.   
1e8aa 20 7d 0a 20 20 20 20 69 66 28 20 75 2e 62 71 2e   }.    if( u.bq.
1e8ab 72 65 73 3e 30 20 29 7b 0a 20 20 20 20 20 20 70  res>0 ){.      p
1e8ac 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 20  c = pOp->p2 - 1 
1e8ad 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72  ;.    }.  }.  br
1e8ae 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
1e8af 65 3a 20 44 65 73 74 72 6f 79 20 50 31 20 50 32  e: Destroy P1 P2
1e8b0 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 44 65   P3 * *.**.** De
1e8b1 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65 20 64  lete an entire d
1e8b2 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72  atabase table or
1e8b3 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f   index whose roo
1e8b4 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 64 61  t page in the da
1e8b5 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69  tabase.** file i
1e8b6 73 20 67 69 76 65 6e 20 62 79 20 50 31 2e 0a 2a  s given by P1..*
1e8b7 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 62  *.** The table b
1e8b8 65 69 6e 67 20 64 65 73 74 72 6f 79 65 64 20 69  eing destroyed i
1e8b9 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61  s in the main da
1e8ba 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50  tabase file if P
1e8bb 33 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 33 3d  3==0.  If.** P3=
1e8bc 3d 31 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c  =1 then the tabl
1e8bd 65 20 74 6f 20 62 65 20 63 6c 65 61 72 20 69 73  e to be clear is
1e8be 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72   in the auxiliar
1e8bf 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a  y database file.
1e8c0 2a 2a 20 74 68 61 74 20 69 73 20 75 73 65 64 20  ** that is used 
1e8c1 74 6f 20 73 74 6f 72 65 20 74 61 62 6c 65 73 20  to store tables 
1e8c2 63 72 65 61 74 65 20 75 73 69 6e 67 20 43 52 45  create using CRE
1e8c3 41 54 45 20 54 45 4d 50 4f 52 41 52 59 20 54 41  ATE TEMPORARY TA
1e8c4 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55  BLE..**.** If AU
1e8c5 54 4f 56 41 43 55 55 4d 20 69 73 20 65 6e 61 62  TOVACUUM is enab
1e8c6 6c 65 64 20 74 68 65 6e 20 69 74 20 69 73 20 70  led then it is p
1e8c7 6f 73 73 69 62 6c 65 20 74 68 61 74 20 61 6e 6f  ossible that ano
1e8c8 74 68 65 72 20 72 6f 6f 74 20 70 61 67 65 0a 2a  ther root page.*
1e8c9 2a 20 6d 69 67 68 74 20 62 65 20 6d 6f 76 65 64  * might be moved
1e8ca 20 69 6e 74 6f 20 74 68 65 20 6e 65 77 6c 79 20   into the newly 
1e8cb 64 65 6c 65 74 65 64 20 72 6f 6f 74 20 70 61 67  deleted root pag
1e8cc 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65  e in order to ke
1e8cd 65 70 20 61 6c 6c 0a 2a 2a 20 72 6f 6f 74 20 70  ep all.** root p
1e8ce 61 67 65 73 20 63 6f 6e 74 69 67 75 6f 75 73 20  ages contiguous 
1e8cf 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
1e8d0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1e8d1 2e 20 20 54 68 65 20 66 6f 72 6d 65 72 0a 2a 2a  .  The former.**
1e8d2 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 72 6f   value of the ro
1e8d3 6f 74 20 70 61 67 65 20 74 68 61 74 20 6d 6f 76  ot page that mov
1e8d4 65 64 20 2d 20 69 74 73 20 76 61 6c 75 65 20 62  ed - its value b
1e8d5 65 66 6f 72 65 20 74 68 65 20 6d 6f 76 65 20 6f  efore the move o
1e8d6 63 63 75 72 72 65 64 20 2d 0a 2a 2a 20 69 73 20  ccurred -.** is 
1e8d7 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74  stored in regist
1e8d8 65 72 20 50 32 2e 20 20 49 66 20 6e 6f 20 70 61  er P2.  If no pa
1e8d9 67 65 20 0a 2a 2a 20 6d 6f 76 65 6d 65 6e 74 20  ge .** movement 
1e8da 77 61 73 20 72 65 71 75 69 72 65 64 20 28 62 65  was required (be
1e8db 63 61 75 73 65 20 74 68 65 20 74 61 62 6c 65 20  cause the table 
1e8dc 62 65 69 6e 67 20 64 72 6f 70 70 65 64 20 77 61  being dropped wa
1e8dd 73 20 61 6c 72 65 61 64 79 20 0a 2a 2a 20 74 68  s already .** th
1e8de 65 20 6c 61 73 74 20 6f 6e 65 20 69 6e 20 74 68  e last one in th
1e8df 65 20 64 61 74 61 62 61 73 65 29 20 74 68 65 6e  e database) then
1e8e0 20 61 20 7a 65 72 6f 20 69 73 20 73 74 6f 72 65   a zero is store
1e8e1 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32  d in register P2
1e8e2 2e 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55  ..** If AUTOVACU
1e8e3 55 4d 20 69 73 20 64 69 73 61 62 6c 65 64 20 74  UM is disabled t
1e8e4 68 65 6e 20 61 20 7a 65 72 6f 20 69 73 20 73 74  hen a zero is st
1e8e5 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72  ored in register
1e8e6 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61   P2..**.** See a
1e8e7 6c 73 6f 3a 20 43 6c 65 61 72 0a 2a 2f 0a 63 61  lso: Clear.*/.ca
1e8e8 73 65 20 4f 50 5f 44 65 73 74 72 6f 79 3a 20 7b  se OP_Destroy: {
1e8e9 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65       /* out2-pre
1e8ea 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30  release */.#if 0
1e8eb 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1e8ec 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e8ed 75 2e 62 72 20 2a 2f 0a 20 20 69 6e 74 20 69 4d  u.br */.  int iM
1e8ee 6f 76 65 64 3b 0a 20 20 69 6e 74 20 69 43 6e 74  oved;.  int iCnt
1e8ef 3b 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b  ;.  Vdbe *pVdbe;
1e8f0 0a 20 20 69 6e 74 20 69 44 62 3b 0a 23 65 6e 64  .  int iDb;.#end
1e8f1 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1e8f2 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e8f3 20 75 2e 62 72 20 2a 2f 0a 23 69 66 6e 64 65 66   u.br */.#ifndef
1e8f4 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1e8f5 54 55 41 4c 54 41 42 4c 45 0a 20 20 75 2e 62 72  TUALTABLE.  u.br
1e8f6 2e 69 43 6e 74 20 3d 20 30 3b 0a 20 20 66 6f 72  .iCnt = 0;.  for
1e8f7 28 75 2e 62 72 2e 70 56 64 62 65 3d 64 62 2d 3e  (u.br.pVdbe=db->
1e8f8 70 56 64 62 65 3b 20 75 2e 62 72 2e 70 56 64 62  pVdbe; u.br.pVdb
1e8f9 65 3b 20 75 2e 62 72 2e 70 56 64 62 65 20 3d 20  e; u.br.pVdbe = 
1e8fa 75 2e 62 72 2e 70 56 64 62 65 2d 3e 70 4e 65 78  u.br.pVdbe->pNex
1e8fb 74 29 7b 0a 20 20 20 20 69 66 28 20 75 2e 62 72  t){.    if( u.br
1e8fc 2e 70 56 64 62 65 2d 3e 6d 61 67 69 63 3d 3d 56  .pVdbe->magic==V
1e8fd 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26 26  DBE_MAGIC_RUN &&
1e8fe 20 75 2e 62 72 2e 70 56 64 62 65 2d 3e 69 6e 56   u.br.pVdbe->inV
1e8ff 74 61 62 4d 65 74 68 6f 64 3c 32 20 26 26 20 75  tabMethod<2 && u
1e900 2e 62 72 2e 70 56 64 62 65 2d 3e 70 63 3e 3d 30  .br.pVdbe->pc>=0
1e901 20 29 7b 0a 20 20 20 20 20 20 75 2e 62 72 2e 69   ){.      u.br.i
1e902 43 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d  Cnt++;.    }.  }
1e903 0a 23 65 6c 73 65 0a 20 20 75 2e 62 72 2e 69 43  .#else.  u.br.iC
1e904 6e 74 20 3d 20 64 62 2d 3e 61 63 74 69 76 65 56  nt = db->activeV
1e905 64 62 65 43 6e 74 3b 0a 23 65 6e 64 69 66 0a 20  dbeCnt;.#endif. 
1e906 20 69 66 28 20 75 2e 62 72 2e 69 43 6e 74 3e 31   if( u.br.iCnt>1
1e907 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
1e908 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20  ITE_LOCKED;.    
1e909 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e 20 3d  p->errorAction =
1e90a 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 7d 65 6c   OE_Abort;.  }el
1e90b 73 65 7b 0a 20 20 20 20 75 2e 62 72 2e 69 44 62  se{.    u.br.iDb
1e90c 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 20 20   = pOp->p3;.    
1e90d 61 73 73 65 72 74 28 20 75 2e 62 72 2e 69 43 6e  assert( u.br.iCn
1e90e 74 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65  t==1 );.    asse
1e90f 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73  rt( (p->btreeMas
1e910 6b 20 26 20 28 31 3c 3c 75 2e 62 72 2e 69 44 62  k & (1<<u.br.iDb
1e911 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 72 63 20  ))!=0 );.    rc 
1e912 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72  = sqlite3BtreeDr
1e913 6f 70 54 61 62 6c 65 28 64 62 2d 3e 61 44 62 5b  opTable(db->aDb[
1e914 75 2e 62 72 2e 69 44 62 5d 2e 70 42 74 2c 20 70  u.br.iDb].pBt, p
1e915 4f 70 2d 3e 70 31 2c 20 26 75 2e 62 72 2e 69 4d  Op->p1, &u.br.iM
1e916 6f 76 65 64 29 3b 0a 20 20 20 20 4d 65 6d 53 65  oved);.    MemSe
1e917 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
1e918 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20 20 70 4f  MEM_Int);.    pO
1e919 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62 72 2e 69  ut->u.i = u.br.i
1e91a 4d 6f 76 65 64 3b 0a 23 69 66 6e 64 65 66 20 53  Moved;.#ifndef S
1e91b 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
1e91c 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 72 63  ACUUM.    if( rc
1e91d 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 75  ==SQLITE_OK && u
1e91e 2e 62 72 2e 69 4d 6f 76 65 64 21 3d 30 20 29 7b  .br.iMoved!=0 ){
1e91f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f  .      sqlite3Ro
1e920 6f 74 50 61 67 65 4d 6f 76 65 64 28 26 64 62 2d  otPageMoved(&db-
1e921 3e 61 44 62 5b 75 2e 62 72 2e 69 44 62 5d 2c 20  >aDb[u.br.iDb], 
1e922 75 2e 62 72 2e 69 4d 6f 76 65 64 2c 20 70 4f 70  u.br.iMoved, pOp
1e923 2d 3e 70 31 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  ->p1);.    }.#en
1e924 64 69 66 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  dif.  }.  break;
1e925 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43  .}../* Opcode: C
1e926 6c 65 61 72 20 50 31 20 50 32 20 50 33 0a 2a 2a  lear P1 P2 P3.**
1e927 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 63  .** Delete all c
1e928 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 64  ontents of the d
1e929 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 6f 72  atabase table or
1e92a 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f   index whose roo
1e92b 74 20 70 61 67 65 0a 2a 2a 20 69 6e 20 74 68 65  t page.** in the
1e92c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69   database file i
1e92d 73 20 67 69 76 65 6e 20 62 79 20 50 31 2e 20 20  s given by P1.  
1e92e 42 75 74 2c 20 75 6e 6c 69 6b 65 20 44 65 73 74  But, unlike Dest
1e92f 72 6f 79 2c 20 64 6f 20 6e 6f 74 0a 2a 2a 20 72  roy, do not.** r
1e930 65 6d 6f 76 65 20 74 68 65 20 74 61 62 6c 65 20  emove the table 
1e931 6f 72 20 69 6e 64 65 78 20 66 72 6f 6d 20 74 68  or index from th
1e932 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
1e933 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65  .**.** The table
1e934 20 62 65 69 6e 67 20 63 6c 65 61 72 20 69 73 20   being clear is 
1e935 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  in the main data
1e936 62 61 73 65 20 66 69 6c 65 20 69 66 20 50 32 3d  base file if P2=
1e937 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 32 3d 3d 31  =0.  If.** P2==1
1e938 20 74 68 65 6e 20 74 68 65 20 74 61 62 6c 65 20   then the table 
1e939 74 6f 20 62 65 20 63 6c 65 61 72 20 69 73 20 69  to be clear is i
1e93a 6e 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20  n the auxiliary 
1e93b 64 61 74 61 62 61 73 65 20 66 69 6c 65 0a 2a 2a  database file.**
1e93c 20 74 68 61 74 20 69 73 20 75 73 65 64 20 74 6f   that is used to
1e93d 20 73 74 6f 72 65 20 74 61 62 6c 65 73 20 63 72   store tables cr
1e93e 65 61 74 65 20 75 73 69 6e 67 20 43 52 45 41 54  eate using CREAT
1e93f 45 20 54 45 4d 50 4f 52 41 52 59 20 54 41 42 4c  E TEMPORARY TABL
1e940 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  E..**.** If the 
1e941 50 33 20 76 61 6c 75 65 20 69 73 20 6e 6f 6e 2d  P3 value is non-
1e942 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 74  zero, then the t
1e943 61 62 6c 65 20 72 65 66 65 72 72 65 64 20 74 6f  able referred to
1e944 20 6d 75 73 74 20 62 65 20 61 6e 0a 2a 2a 20 69   must be an.** i
1e945 6e 74 6b 65 79 20 74 61 62 6c 65 20 28 61 6e 20  ntkey table (an 
1e946 53 51 4c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61  SQL table, not a
1e947 6e 20 69 6e 64 65 78 29 2e 20 49 6e 20 74 68 69  n index). In thi
1e948 73 20 63 61 73 65 20 74 68 65 20 72 6f 77 20 63  s case the row c
1e949 68 61 6e 67 65 20 0a 2a 2a 20 63 6f 75 6e 74 20  hange .** count 
1e94a 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62  is incremented b
1e94b 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  y the number of 
1e94c 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c  rows in the tabl
1e94d 65 20 62 65 69 6e 67 20 63 6c 65 61 72 65 64 2e  e being cleared.
1e94e 20 0a 2a 2a 20 49 66 20 50 33 20 69 73 20 67 72   .** If P3 is gr
1e94f 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f 2c  eater than zero,
1e950 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20   then the value 
1e951 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74  stored in regist
1e952 65 72 20 50 33 20 69 73 0a 2a 2a 20 61 6c 73 6f  er P3 is.** also
1e953 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20   incremented by 
1e954 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  the number of ro
1e955 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ws in the table 
1e956 62 65 69 6e 67 20 63 6c 65 61 72 65 64 2e 0a 2a  being cleared..*
1e957 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 44  *.** See also: D
1e958 65 73 74 72 6f 79 0a 2a 2f 0a 63 61 73 65 20 4f  estroy.*/.case O
1e959 50 5f 43 6c 65 61 72 3a 20 7b 0a 23 69 66 20 30  P_Clear: {.#if 0
1e95a 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1e95b 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e95c 75 2e 62 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  u.bs */.  int nC
1e95d 68 61 6e 67 65 3b 0a 23 65 6e 64 69 66 20 2f 2a  hange;.#endif /*
1e95e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1e95f 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 73   moved into u.bs
1e960 20 2a 2f 0a 0a 20 20 75 2e 62 73 2e 6e 43 68 61   */..  u.bs.nCha
1e961 6e 67 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 72  nge = 0;.  asser
1e962 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b  t( (p->btreeMask
1e963 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 32 29 29   & (1<<pOp->p2))
1e964 21 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71  !=0 );.  rc = sq
1e965 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 54  lite3BtreeClearT
1e966 61 62 6c 65 28 0a 20 20 20 20 20 20 64 62 2d 3e  able(.      db->
1e967 61 44 62 5b 70 4f 70 2d 3e 70 32 5d 2e 70 42 74  aDb[pOp->p2].pBt
1e968 2c 20 70 4f 70 2d 3e 70 31 2c 20 28 70 4f 70 2d  , pOp->p1, (pOp-
1e969 3e 70 33 20 3f 20 26 75 2e 62 73 2e 6e 43 68 61  >p3 ? &u.bs.nCha
1e96a 6e 67 65 20 3a 20 30 29 0a 20 20 29 3b 0a 20 20  nge : 0).  );.  
1e96b 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b 0a 20  if( pOp->p3 ){. 
1e96c 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 2b 3d     p->nChange +=
1e96d 20 75 2e 62 73 2e 6e 43 68 61 6e 67 65 3b 0a 20   u.bs.nChange;. 
1e96e 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 3e 30     if( pOp->p3>0
1e96f 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 4d 65   ){.      p->aMe
1e970 6d 5b 70 4f 70 2d 3e 70 33 5d 2e 75 2e 69 20 2b  m[pOp->p3].u.i +
1e971 3d 20 75 2e 62 73 2e 6e 43 68 61 6e 67 65 3b 0a  = u.bs.nChange;.
1e972 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61      }.  }.  brea
1e973 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1e974 20 43 72 65 61 74 65 54 61 62 6c 65 20 50 31 20   CreateTable P1 
1e975 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41  P2 * * *.**.** A
1e976 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 74 61  llocate a new ta
1e977 62 6c 65 20 69 6e 20 74 68 65 20 6d 61 69 6e 20  ble in the main 
1e978 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66  database file if
1e979 20 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65   P1==0 or in the
1e97a 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61  .** auxiliary da
1e97b 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50  tabase file if P
1e97c 31 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61 74  1==1 or in an at
1e97d 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 20  tached database 
1e97e 69 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69  if.** P1>1.  Wri
1e97f 74 65 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  te the root page
1e980 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e   number of the n
1e981 65 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a  ew table into.**
1e982 20 72 65 67 69 73 74 65 72 20 50 32 0a 2a 2a 0a   register P2.**.
1e983 2a 2a 20 54 68 65 20 64 69 66 66 65 72 65 6e 63  ** The differenc
1e984 65 20 62 65 74 77 65 65 6e 20 61 20 74 61 62 6c  e between a tabl
1e985 65 20 61 6e 64 20 61 6e 20 69 6e 64 65 78 20 69  e and an index i
1e986 73 20 74 68 69 73 3a 20 20 41 20 74 61 62 6c 65  s this:  A table
1e987 20 6d 75 73 74 0a 2a 2a 20 68 61 76 65 20 61 20   must.** have a 
1e988 34 2d 62 79 74 65 20 69 6e 74 65 67 65 72 20 6b  4-byte integer k
1e989 65 79 20 61 6e 64 20 63 61 6e 20 68 61 76 65 20  ey and can have 
1e98a 61 72 62 69 74 72 61 72 79 20 64 61 74 61 2e 20  arbitrary data. 
1e98b 20 41 6e 20 69 6e 64 65 78 0a 2a 2a 20 68 61 73   An index.** has
1e98c 20 61 6e 20 61 72 62 69 74 72 61 72 79 20 6b 65   an arbitrary ke
1e98d 79 20 62 75 74 20 6e 6f 20 64 61 74 61 2e 0a 2a  y but no data..*
1e98e 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43  *.** See also: C
1e98f 72 65 61 74 65 49 6e 64 65 78 0a 2a 2f 0a 2f 2a  reateIndex.*/./*
1e990 20 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 65 49   Opcode: CreateI
1e991 6e 64 65 78 20 50 31 20 50 32 20 2a 20 2a 20 2a  ndex P1 P2 * * *
1e992 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  .**.** Allocate 
1e993 61 20 6e 65 77 20 69 6e 64 65 78 20 69 6e 20 74  a new index in t
1e994 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
1e995 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f   file if P1==0 o
1e996 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69  r in the.** auxi
1e997 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66  liary database f
1e998 69 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f 72 20  ile if P1==1 or 
1e999 69 6e 20 61 6e 20 61 74 74 61 63 68 65 64 20 64  in an attached d
1e99a 61 74 61 62 61 73 65 20 69 66 0a 2a 2a 20 50 31  atabase if.** P1
1e99b 3e 31 2e 20 20 57 72 69 74 65 20 74 68 65 20 72  >1.  Write the r
1e99c 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  oot page number 
1e99d 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  of the new table
1e99e 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65   into.** registe
1e99f 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  r P2..**.** See 
1e9a0 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 6e  documentation on
1e9a1 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65 20   OP_CreateTable 
1e9a2 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  for additional i
1e9a3 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63  nformation..*/.c
1e9a4 61 73 65 20 4f 50 5f 43 72 65 61 74 65 49 6e 64  ase OP_CreateInd
1e9a5 65 78 3a 20 20 20 20 20 20 20 20 20 20 20 20 2f  ex:            /
1e9a6 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
1e9a7 65 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 72 65  e */.case OP_Cre
1e9a8 61 74 65 54 61 62 6c 65 3a 20 7b 20 20 20 20 20  ateTable: {     
1e9a9 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65       /* out2-pre
1e9aa 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20 30  release */.#if 0
1e9ab 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61    /* local varia
1e9ac 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1e9ad 75 2e 62 74 20 2a 2f 0a 20 20 69 6e 74 20 70 67  u.bt */.  int pg
1e9ae 6e 6f 3b 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b  no;.  int flags;
1e9af 0a 20 20 44 62 20 2a 70 44 62 3b 0a 23 65 6e 64  .  Db *pDb;.#end
1e9b0 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  if /* local vari
1e9b1 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e9b2 20 75 2e 62 74 20 2a 2f 0a 0a 20 20 75 2e 62 74   u.bt */..  u.bt
1e9b3 2e 70 67 6e 6f 20 3d 20 30 3b 0a 20 20 61 73 73  .pgno = 0;.  ass
1e9b4 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20  ert( pOp->p1>=0 
1e9b5 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e  && pOp->p1<db->n
1e9b6 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Db );.  assert( 
1e9b7 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20  (p->btreeMask & 
1e9b8 28 31 3c 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30  (1<<pOp->p1))!=0
1e9b9 20 29 3b 0a 20 20 75 2e 62 74 2e 70 44 62 20 3d   );.  u.bt.pDb =
1e9ba 20 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70   &db->aDb[pOp->p
1e9bb 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e  1];.  assert( u.
1e9bc 62 74 2e 70 44 62 2d 3e 70 42 74 21 3d 30 20 29  bt.pDb->pBt!=0 )
1e9bd 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63  ;.  if( pOp->opc
1e9be 6f 64 65 3d 3d 4f 50 5f 43 72 65 61 74 65 54 61  ode==OP_CreateTa
1e9bf 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 75 2e  ble ){.    /* u.
1e9c0 62 74 2e 66 6c 61 67 73 20 3d 20 42 54 52 45 45  bt.flags = BTREE
1e9c1 5f 49 4e 54 4b 45 59 3b 20 2a 2f 0a 20 20 20 20  _INTKEY; */.    
1e9c2 75 2e 62 74 2e 66 6c 61 67 73 20 3d 20 42 54 52  u.bt.flags = BTR
1e9c3 45 45 5f 4c 45 41 46 44 41 54 41 7c 42 54 52 45  EE_LEAFDATA|BTRE
1e9c4 45 5f 49 4e 54 4b 45 59 3b 0a 20 20 7d 65 6c 73  E_INTKEY;.  }els
1e9c5 65 7b 0a 20 20 20 20 75 2e 62 74 2e 66 6c 61 67  e{.    u.bt.flag
1e9c6 73 20 3d 20 42 54 52 45 45 5f 5a 45 52 4f 44 41  s = BTREE_ZERODA
1e9c7 54 41 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73  TA;.  }.  rc = s
1e9c8 71 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74  qlite3BtreeCreat
1e9c9 65 54 61 62 6c 65 28 75 2e 62 74 2e 70 44 62 2d  eTable(u.bt.pDb-
1e9ca 3e 70 42 74 2c 20 26 75 2e 62 74 2e 70 67 6e 6f  >pBt, &u.bt.pgno
1e9cb 2c 20 75 2e 62 74 2e 66 6c 61 67 73 29 3b 0a 20  , u.bt.flags);. 
1e9cc 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 62   pOut->u.i = u.b
1e9cd 74 2e 70 67 6e 6f 3b 0a 20 20 4d 65 6d 53 65 74  t.pgno;.  MemSet
1e9ce 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d  TypeFlag(pOut, M
1e9cf 45 4d 5f 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b  EM_Int);.  break
1e9d0 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1e9d1 50 61 72 73 65 53 63 68 65 6d 61 20 50 31 20 50  ParseSchema P1 P
1e9d2 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52  2 * P4 *.**.** R
1e9d3 65 61 64 20 61 6e 64 20 70 61 72 73 65 20 61 6c  ead and parse al
1e9d4 6c 20 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74  l entries from t
1e9d5 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52  he SQLITE_MASTER
1e9d6 20 74 61 62 6c 65 20 6f 66 20 64 61 74 61 62 61   table of databa
1e9d7 73 65 20 50 31 0a 2a 2a 20 74 68 61 74 20 6d 61  se P1.** that ma
1e9d8 74 63 68 20 74 68 65 20 57 48 45 52 45 20 63 6c  tch the WHERE cl
1e9d9 61 75 73 65 20 50 34 2e 20 20 50 32 20 69 73 20  ause P4.  P2 is 
1e9da 74 68 65 20 22 66 6f 72 63 65 22 20 66 6c 61 67  the "force" flag
1e9db 2e 20 20 20 41 6c 77 61 79 73 20 64 6f 0a 2a 2a  .   Always do.**
1e9dc 20 74 68 65 20 70 61 72 73 69 6e 67 20 69 66 20   the parsing if 
1e9dd 50 32 20 69 73 20 74 72 75 65 2e 20 20 49 66 20  P2 is true.  If 
1e9de 50 32 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65  P2 is false, the
1e9df 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
1e9e0 73 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 20 69 66 20  s a.** no-op if 
1e9e1 74 68 65 20 73 63 68 65 6d 61 20 69 73 20 6e 6f  the schema is no
1e9e2 74 20 63 75 72 72 65 6e 74 6c 79 20 6c 6f 61 64  t currently load
1e9e3 65 64 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f  ed.  In other wo
1e9e4 72 64 73 2c 20 69 66 20 50 32 0a 2a 2a 20 69 73  rds, if P2.** is
1e9e5 20 66 61 6c 73 65 2c 20 74 68 65 20 53 51 4c 49   false, the SQLI
1e9e6 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20  TE_MASTER table 
1e9e7 69 73 20 6f 6e 6c 79 20 70 61 72 73 65 64 20 69  is only parsed i
1e9e8 66 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68  f the rest of th
1e9e9 65 0a 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 61  e.** schema is a
1e9ea 6c 72 65 61 64 79 20 6c 6f 61 64 65 64 20 69 6e  lready loaded in
1e9eb 74 6f 20 74 68 65 20 73 79 6d 62 6f 6c 20 74 61  to the symbol ta
1e9ec 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ble..**.** This 
1e9ed 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74  opcode invokes t
1e9ee 68 65 20 70 61 72 73 65 72 20 74 6f 20 63 72 65  he parser to cre
1e9ef 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75 61  ate a new virtua
1e9f0 6c 20 6d 61 63 68 69 6e 65 2c 0a 2a 2a 20 74 68  l machine,.** th
1e9f1 65 6e 20 72 75 6e 73 20 74 68 65 20 6e 65 77 20  en runs the new 
1e9f2 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e  virtual machine.
1e9f3 20 20 49 74 20 69 73 20 74 68 75 73 20 61 20 72    It is thus a r
1e9f4 65 2d 65 6e 74 72 61 6e 74 20 6f 70 63 6f 64 65  e-entrant opcode
1e9f5 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 72  ..*/.case OP_Par
1e9f6 73 65 53 63 68 65 6d 61 3a 20 7b 0a 23 69 66 20  seSchema: {.#if 
1e9f7 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1e9f8 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1e9f9 20 75 2e 62 75 20 2a 2f 0a 20 20 69 6e 74 20 69   u.bu */.  int i
1e9fa 44 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  Db;.  const char
1e9fb 20 2a 7a 4d 61 73 74 65 72 3b 0a 20 20 63 68 61   *zMaster;.  cha
1e9fc 72 20 2a 7a 53 71 6c 3b 0a 20 20 49 6e 69 74 44  r *zSql;.  InitD
1e9fd 61 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 23 65  ata initData;.#e
1e9fe 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61  ndif /* local va
1e9ff 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e  riables moved in
1ea00 74 6f 20 75 2e 62 75 20 2a 2f 0a 0a 20 20 75 2e  to u.bu */..  u.
1ea01 62 75 2e 69 44 62 20 3d 20 70 4f 70 2d 3e 70 31  bu.iDb = pOp->p1
1ea02 3b 0a 20 20 61 73 73 65 72 74 28 20 75 2e 62 75  ;.  assert( u.bu
1ea03 2e 69 44 62 3e 3d 30 20 26 26 20 75 2e 62 75 2e  .iDb>=0 && u.bu.
1ea04 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 0a  iDb<db->nDb );..
1ea05 20 20 2f 2a 20 49 66 20 70 4f 70 2d 3e 70 32 20    /* If pOp->p2 
1ea06 69 73 20 30 2c 20 74 68 65 6e 20 74 68 69 73 20  is 0, then this 
1ea07 6f 70 63 6f 64 65 20 69 73 20 62 65 69 6e 67 20  opcode is being 
1ea08 65 78 65 63 75 74 65 64 20 74 6f 20 72 65 61 64  executed to read
1ea09 20 61 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 20 72   a.  ** single r
1ea0a 6f 77 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 20  ow, for example 
1ea0b 74 68 65 20 72 6f 77 20 63 6f 72 72 65 73 70 6f  the row correspo
1ea0c 6e 64 69 6e 67 20 74 6f 20 61 20 6e 65 77 20 69  nding to a new i
1ea0d 6e 64 65 78 0a 20 20 2a 2a 20 63 72 65 61 74 65  ndex.  ** create
1ea0e 64 20 62 79 20 74 68 69 73 20 56 44 42 45 2c 20  d by this VDBE, 
1ea0f 66 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f  from the sqlite_
1ea10 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20 49 74  master table. It
1ea11 20 6f 6e 6c 79 0a 20 20 2a 2a 20 64 6f 65 73 20   only.  ** does 
1ea12 74 68 69 73 20 69 66 20 74 68 65 20 63 6f 72 72  this if the corr
1ea13 65 73 70 6f 6e 64 69 6e 67 20 69 6e 2d 6d 65 6d  esponding in-mem
1ea14 6f 72 79 20 73 63 68 65 6d 61 20 69 73 20 63 75  ory schema is cu
1ea15 72 72 65 6e 74 6c 79 0a 20 20 2a 2a 20 6c 6f 61  rrently.  ** loa
1ea16 64 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ded. Otherwise, 
1ea17 74 68 65 20 6e 65 77 20 69 6e 64 65 78 20 64 65  the new index de
1ea18 66 69 6e 69 74 69 6f 6e 20 63 61 6e 20 62 65 20  finition can be 
1ea19 6c 6f 61 64 65 64 20 61 6c 6f 6e 67 0a 20 20 2a  loaded along.  *
1ea1a 2a 20 77 69 74 68 20 74 68 65 20 72 65 73 74 20  * with the rest 
1ea1b 6f 66 20 74 68 65 20 73 63 68 65 6d 61 20 77 68  of the schema wh
1ea1c 65 6e 20 69 74 20 69 73 20 72 65 71 75 69 72 65  en it is require
1ea1d 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6c 74  d..  **.  ** Alt
1ea1e 68 6f 75 67 68 20 74 68 65 20 6d 75 74 65 78 20  hough the mutex 
1ea1f 6f 6e 20 74 68 65 20 42 74 53 68 61 72 65 64 20  on the BtShared 
1ea20 6f 62 6a 65 63 74 20 74 68 61 74 20 63 6f 72 72  object that corr
1ea21 65 73 70 6f 6e 64 73 20 74 6f 0a 20 20 2a 2a 20  esponds to.  ** 
1ea22 64 61 74 61 62 61 73 65 20 75 2e 62 75 2e 69 44  database u.bu.iD
1ea23 62 20 28 74 68 65 20 64 61 74 61 62 61 73 65 20  b (the database 
1ea24 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 73  containing the s
1ea25 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
1ea26 6c 65 0a 20 20 2a 2a 20 72 65 61 64 20 62 79 20  le.  ** read by 
1ea27 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  this instruction
1ea28 29 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 68  ) is currently h
1ea29 65 6c 64 2c 20 69 74 20 69 73 20 6e 65 63 65 73  eld, it is neces
1ea2a 73 61 72 79 20 74 6f 0a 20 20 2a 2a 20 6f 62 74  sary to.  ** obt
1ea2b 61 69 6e 20 74 68 65 20 6d 75 74 65 78 65 73 20  ain the mutexes 
1ea2c 6f 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20  on all attached 
1ea2d 64 61 74 61 62 61 73 65 73 20 62 65 66 6f 72 65  databases before
1ea2e 20 63 68 65 63 6b 69 6e 67 20 69 66 0a 20 20 2a   checking if.  *
1ea2f 2a 20 74 68 65 20 73 63 68 65 6d 61 20 6f 66 20  * the schema of 
1ea30 75 2e 62 75 2e 69 44 62 20 69 73 20 6c 6f 61 64  u.bu.iDb is load
1ea31 65 64 2e 20 54 68 69 73 20 69 73 20 62 65 63 61  ed. This is beca
1ea32 75 73 65 2c 20 61 74 20 74 68 65 20 73 74 61 72  use, at the star
1ea33 74 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 73 71  t of.  ** the sq
1ea34 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63 61 6c  lite3_exec() cal
1ea35 6c 20 62 65 6c 6f 77 2c 20 53 51 4c 69 74 65 20  l below, SQLite 
1ea36 77 69 6c 6c 20 69 6e 76 6f 6b 65 0a 20 20 2a 2a  will invoke.  **
1ea37 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74   sqlite3BtreeEnt
1ea38 65 72 41 6c 6c 28 29 2e 20 49 66 20 61 6c 6c 20  erAll(). If all 
1ea39 6d 75 74 65 78 65 73 20 61 72 65 20 6e 6f 74 20  mutexes are not 
1ea3a 61 6c 72 65 61 64 79 20 68 65 6c 64 2c 20 74 68  already held, th
1ea3b 65 0a 20 20 2a 2a 20 75 2e 62 75 2e 69 44 62 20  e.  ** u.bu.iDb 
1ea3c 6d 75 74 65 78 20 6d 61 79 20 62 65 20 74 65 6d  mutex may be tem
1ea3d 70 6f 72 61 72 69 6c 79 20 72 65 6c 65 61 73 65  porarily release
1ea3e 64 20 74 6f 20 61 76 6f 69 64 20 64 65 61 64 6c  d to avoid deadl
1ea3f 6f 63 6b 2e 20 49 66 0a 20 20 2a 2a 20 74 68 69  ock. If.  ** thi
1ea40 73 20 68 61 70 70 65 6e 73 2c 20 74 68 65 6e 20  s happens, then 
1ea41 73 6f 6d 65 20 6f 74 68 65 72 20 74 68 72 65 61  some other threa
1ea42 64 20 6d 61 79 20 64 65 6c 65 74 65 20 74 68 65  d may delete the
1ea43 20 69 6e 2d 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20   in-memory.  ** 
1ea44 73 63 68 65 6d 61 20 6f 66 20 64 61 74 61 62 61  schema of databa
1ea45 73 65 20 75 2e 62 75 2e 69 44 62 20 62 65 66 6f  se u.bu.iDb befo
1ea46 72 65 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  re the SQL state
1ea47 6d 65 6e 74 20 72 75 6e 73 2e 20 54 68 65 20 73  ment runs. The s
1ea48 63 68 65 6d 61 0a 20 20 2a 2a 20 77 69 6c 6c 20  chema.  ** will 
1ea49 6e 6f 74 20 62 65 20 72 65 6c 6f 61 64 65 64 20  not be reloaded 
1ea4a 62 65 63 75 61 73 65 20 74 68 65 20 64 62 2d 3e  becuase the db->
1ea4b 69 6e 69 74 2e 62 75 73 79 20 66 6c 61 67 20 69  init.busy flag i
1ea4c 73 20 73 65 74 2e 20 54 68 69 73 0a 20 20 2a 2a  s set. This.  **
1ea4d 20 63 61 6e 20 72 65 73 75 6c 74 20 69 6e 20 61   can result in a
1ea4e 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 3a   "no such table:
1ea4f 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22 20   sqlite_master" 
1ea50 6f 72 20 22 6d 61 6c 66 6f 72 6d 65 64 0a 20 20  or "malformed.  
1ea51 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63 68 65  ** database sche
1ea52 6d 61 22 20 65 72 72 6f 72 20 62 65 69 6e 67 20  ma" error being 
1ea53 72 65 74 75 72 6e 65 64 20 74 6f 20 74 68 65 20  returned to the 
1ea54 75 73 65 72 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  user..  */.  ass
1ea55 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
1ea56 65 48 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d 3e  eHoldsMutex(db->
1ea57 61 44 62 5b 75 2e 62 75 2e 69 44 62 5d 2e 70 42  aDb[u.bu.iDb].pB
1ea58 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  t) );.  sqlite3B
1ea59 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29  treeEnterAll(db)
1ea5a 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 20  ;.  if( pOp->p2 
1ea5b 7c 7c 20 44 62 48 61 73 50 72 6f 70 65 72 74 79  || DbHasProperty
1ea5c 28 64 62 2c 20 75 2e 62 75 2e 69 44 62 2c 20 44  (db, u.bu.iDb, D
1ea5d 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 20  B_SchemaLoaded) 
1ea5e 29 7b 0a 20 20 20 20 75 2e 62 75 2e 7a 4d 61 73  ){.    u.bu.zMas
1ea5f 74 65 72 20 3d 20 53 43 48 45 4d 41 5f 54 41 42  ter = SCHEMA_TAB
1ea60 4c 45 28 75 2e 62 75 2e 69 44 62 29 3b 0a 20 20  LE(u.bu.iDb);.  
1ea61 20 20 75 2e 62 75 2e 69 6e 69 74 44 61 74 61 2e    u.bu.initData.
1ea62 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 75 2e 62  db = db;.    u.b
1ea63 75 2e 69 6e 69 74 44 61 74 61 2e 69 44 62 20 3d  u.initData.iDb =
1ea64 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 20 20 75 2e   pOp->p1;.    u.
1ea65 62 75 2e 69 6e 69 74 44 61 74 61 2e 70 7a 45 72  bu.initData.pzEr
1ea66 72 4d 73 67 20 3d 20 26 70 2d 3e 7a 45 72 72 4d  rMsg = &p->zErrM
1ea67 73 67 3b 0a 20 20 20 20 75 2e 62 75 2e 7a 53 71  sg;.    u.bu.zSq
1ea68 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  l = sqlite3MPrin
1ea69 74 66 28 64 62 2c 0a 20 20 20 20 20 20 20 22 53  tf(db,.       "S
1ea6a 45 4c 45 43 54 20 6e 61 6d 65 2c 20 72 6f 6f 74  ELECT name, root
1ea6b 70 61 67 65 2c 20 73 71 6c 20 46 52 4f 4d 20 27  page, sql FROM '
1ea6c 25 71 27 2e 25 73 20 57 48 45 52 45 20 25 73 22  %q'.%s WHERE %s"
1ea6d 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62  ,.       db->aDb
1ea6e 5b 75 2e 62 75 2e 69 44 62 5d 2e 7a 4e 61 6d 65  [u.bu.iDb].zName
1ea6f 2c 20 75 2e 62 75 2e 7a 4d 61 73 74 65 72 2c 20  , u.bu.zMaster, 
1ea70 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20  pOp->p4.z);.    
1ea71 69 66 28 20 75 2e 62 75 2e 7a 53 71 6c 3d 3d 30  if( u.bu.zSql==0
1ea72 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
1ea73 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
1ea74 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 76   }else{.      (v
1ea75 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
1ea76 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20  yOff(db);.      
1ea77 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69 74  assert( db->init
1ea78 2e 62 75 73 79 3d 3d 30 20 29 3b 0a 20 20 20 20  .busy==0 );.    
1ea79 20 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20    db->init.busy 
1ea7a 3d 20 31 3b 0a 20 20 20 20 20 20 75 2e 62 75 2e  = 1;.      u.bu.
1ea7b 69 6e 69 74 44 61 74 61 2e 72 63 20 3d 20 53 51  initData.rc = SQ
1ea7c 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 61  LITE_OK;.      a
1ea7d 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c  ssert( !db->mall
1ea7e 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20  ocFailed );.    
1ea7f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 65    rc = sqlite3_e
1ea80 78 65 63 28 64 62 2c 20 75 2e 62 75 2e 7a 53 71  xec(db, u.bu.zSq
1ea81 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61  l, sqlite3InitCa
1ea82 6c 6c 62 61 63 6b 2c 20 26 75 2e 62 75 2e 69 6e  llback, &u.bu.in
1ea83 69 74 44 61 74 61 2c 20 30 29 3b 0a 20 20 20 20  itData, 0);.    
1ea84 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1ea85 5f 4f 4b 20 29 20 72 63 20 3d 20 75 2e 62 75 2e  _OK ) rc = u.bu.
1ea86 69 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20 20  initData.rc;.   
1ea87 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1ea88 28 64 62 2c 20 75 2e 62 75 2e 7a 53 71 6c 29 3b  (db, u.bu.zSql);
1ea89 0a 20 20 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e  .      db->init.
1ea8a 62 75 73 79 20 3d 20 30 3b 0a 20 20 20 20 20 20  busy = 0;.      
1ea8b 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
1ea8c 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 7d  etyOn(db);.    }
1ea8d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74  .  }.  sqlite3Bt
1ea8e 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b  reeLeaveAll(db);
1ea8f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1ea90 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 67  E_NOMEM ){.    g
1ea91 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a  oto no_mem;.  }.
1ea92 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 20    break;.}..#if 
1ea93 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
1ea94 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 29 0a 2f 2a  OMIT_ANALYZE)./*
1ea95 20 4f 70 63 6f 64 65 3a 20 4c 6f 61 64 41 6e 61   Opcode: LoadAna
1ea96 6c 79 73 69 73 20 50 31 20 2a 20 2a 20 2a 20 2a  lysis P1 * * * *
1ea97 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20  .**.** Read the 
1ea98 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62  sqlite_stat1 tab
1ea99 6c 65 20 66 6f 72 20 64 61 74 61 62 61 73 65 20  le for database 
1ea9a 50 31 20 61 6e 64 20 6c 6f 61 64 20 74 68 65 20  P1 and load the 
1ea9b 63 6f 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68  content.** of th
1ea9c 61 74 20 74 61 62 6c 65 20 69 6e 74 6f 20 74 68  at table into th
1ea9d 65 20 69 6e 74 65 72 6e 61 6c 20 69 6e 64 65 78  e internal index
1ea9e 20 68 61 73 68 20 74 61 62 6c 65 2e 20 20 54 68   hash table.  Th
1ea9f 69 73 20 77 69 6c 6c 20 63 61 75 73 65 0a 2a 2a  is will cause.**
1eaa0 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 74 6f   the analysis to
1eaa1 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 70 72   be used when pr
1eaa2 65 70 61 72 69 6e 67 20 61 6c 6c 20 73 75 62 73  eparing all subs
1eaa3 65 71 75 65 6e 74 20 71 75 65 72 69 65 73 2e 0a  equent queries..
1eaa4 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 6f 61 64 41  */.case OP_LoadA
1eaa5 6e 61 6c 79 73 69 73 3a 20 7b 0a 20 20 61 73 73  nalysis: {.  ass
1eaa6 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20  ert( pOp->p1>=0 
1eaa7 26 26 20 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e  && pOp->p1<db->n
1eaa8 44 62 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  Db );.  rc = sql
1eaa9 69 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64  ite3AnalysisLoad
1eaaa 28 64 62 2c 20 70 4f 70 2d 3e 70 31 29 3b 0a 20  (db, pOp->p1);. 
1eaab 20 62 72 65 61 6b 3b 20 20 0a 7d 0a 23 65 6e 64   break;  .}.#end
1eaac 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
1eaad 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59  QLITE_OMIT_ANALY
1eaae 5a 45 29 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64  ZE) */../* Opcod
1eaaf 65 3a 20 44 72 6f 70 54 61 62 6c 65 20 50 31 20  e: DropTable P1 
1eab0 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52  * * P4 *.**.** R
1eab1 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72 6e  emove the intern
1eab2 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20 64  al (in-memory) d
1eab3 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 74  ata structures t
1eab4 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a 20  hat describe.** 
1eab5 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 64 20  the table named 
1eab6 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 20 50  P4 in database P
1eab7 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c  1.  This is call
1eab8 65 64 20 61 66 74 65 72 20 61 20 74 61 62 6c 65  ed after a table
1eab9 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69  .** is dropped i
1eaba 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20  n order to keep 
1eabb 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70  the internal rep
1eabc 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
1eabd 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e  he.** schema con
1eabe 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 61  sistent with wha
1eabf 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f  t is on disk..*/
1eac0 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 61 62  .case OP_DropTab
1eac1 6c 65 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 55  le: {.  sqlite3U
1eac2 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61  nlinkAndDeleteTa
1eac3 62 6c 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c  ble(db, pOp->p1,
1eac4 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62   pOp->p4.z);.  b
1eac5 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1eac6 64 65 3a 20 44 72 6f 70 49 6e 64 65 78 20 50 31  de: DropIndex P1
1eac7 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20   * * P4 *.**.** 
1eac8 52 65 6d 6f 76 65 20 74 68 65 20 69 6e 74 65 72  Remove the inter
1eac9 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f 72 79 29 20  nal (in-memory) 
1eaca 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20  data structures 
1eacb 74 68 61 74 20 64 65 73 63 72 69 62 65 0a 2a 2a  that describe.**
1eacc 20 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 64   the index named
1eacd 20 50 34 20 69 6e 20 64 61 74 61 62 61 73 65 20   P4 in database 
1eace 50 31 2e 20 20 54 68 69 73 20 69 73 20 63 61 6c  P1.  This is cal
1eacf 6c 65 64 20 61 66 74 65 72 20 61 6e 20 69 6e 64  led after an ind
1ead0 65 78 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64  ex.** is dropped
1ead1 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65   in order to kee
1ead2 70 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72  p the internal r
1ead3 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
1ead4 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63   the.** schema c
1ead5 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20 77  onsistent with w
1ead6 68 61 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a  hat is on disk..
1ead7 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 49  */.case OP_DropI
1ead8 6e 64 65 78 3a 20 7b 0a 20 20 73 71 6c 69 74 65  ndex: {.  sqlite
1ead9 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65  3UnlinkAndDelete
1eada 49 6e 64 65 78 28 64 62 2c 20 70 4f 70 2d 3e 70  Index(db, pOp->p
1eadb 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20  1, pOp->p4.z);. 
1eadc 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1eadd 63 6f 64 65 3a 20 44 72 6f 70 54 72 69 67 67 65  code: DropTrigge
1eade 72 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a  r P1 * * P4 *.**
1eadf 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 69  .** Remove the i
1eae0 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65 6d 6f  nternal (in-memo
1eae1 72 79 29 20 64 61 74 61 20 73 74 72 75 63 74 75  ry) data structu
1eae2 72 65 73 20 74 68 61 74 20 64 65 73 63 72 69 62  res that describ
1eae3 65 0a 2a 2a 20 74 68 65 20 74 72 69 67 67 65 72  e.** the trigger
1eae4 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 74   named P4 in dat
1eae5 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 20  abase P1.  This 
1eae6 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  is called after 
1eae7 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 69 73 20  a trigger.** is 
1eae8 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72  dropped in order
1eae9 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74   to keep the int
1eaea 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61  ernal representa
1eaeb 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73  tion of the.** s
1eaec 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74  chema consistent
1eaed 20 77 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e   with what is on
1eaee 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f   disk..*/.case O
1eaef 50 5f 44 72 6f 70 54 72 69 67 67 65 72 3a 20 7b  P_DropTrigger: {
1eaf0 0a 20 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b  .  sqlite3Unlink
1eaf1 41 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72  AndDeleteTrigger
1eaf2 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f  (db, pOp->p1, pO
1eaf3 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 62 72 65 61  p->p4.z);.  brea
1eaf4 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53  k;.}...#ifndef S
1eaf5 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47  QLITE_OMIT_INTEG
1eaf6 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 20 4f 70  RITY_CHECK./* Op
1eaf7 63 6f 64 65 3a 20 49 6e 74 65 67 72 69 74 79 43  code: IntegrityC
1eaf8 6b 20 50 31 20 50 32 20 50 33 20 2a 20 50 35 0a  k P1 P2 P3 * P5.
1eaf9 2a 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61 6e 61 6c  **.** Do an anal
1eafa 79 73 69 73 20 6f 66 20 74 68 65 20 63 75 72 72  ysis of the curr
1eafb 65 6e 74 6c 79 20 6f 70 65 6e 20 64 61 74 61 62  ently open datab
1eafc 61 73 65 2e 20 20 53 74 6f 72 65 20 69 6e 0a 2a  ase.  Store in.*
1eafd 2a 20 72 65 67 69 73 74 65 72 20 50 31 20 74 68  * register P1 th
1eafe 65 20 74 65 78 74 20 6f 66 20 61 6e 20 65 72 72  e text of an err
1eaff 6f 72 20 6d 65 73 73 61 67 65 20 64 65 73 63 72  or message descr
1eb00 69 62 69 6e 67 20 61 6e 79 20 70 72 6f 62 6c 65  ibing any proble
1eb01 6d 73 2e 0a 2a 2a 20 49 66 20 6e 6f 20 70 72 6f  ms..** If no pro
1eb02 62 6c 65 6d 73 20 61 72 65 20 66 6f 75 6e 64 2c  blems are found,
1eb03 20 73 74 6f 72 65 20 61 20 4e 55 4c 4c 20 69 6e   store a NULL in
1eb04 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a   register P1..**
1eb05 0a 2a 2a 20 54 68 65 20 72 65 67 69 73 74 65 72  .** The register
1eb06 20 50 33 20 63 6f 6e 74 61 69 6e 73 20 74 68 65   P3 contains the
1eb07 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
1eb08 6f 66 20 61 6c 6c 6f 77 65 64 20 65 72 72 6f 72  of allowed error
1eb09 73 2e 0a 2a 2a 20 41 74 20 6d 6f 73 74 20 72 65  s..** At most re
1eb0a 67 28 50 33 29 20 65 72 72 6f 72 73 20 77 69 6c  g(P3) errors wil
1eb0b 6c 20 62 65 20 72 65 70 6f 72 74 65 64 2e 0a 2a  l be reported..*
1eb0c 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73  * In other words
1eb0d 2c 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 73  , the analysis s
1eb0e 74 6f 70 73 20 61 73 20 73 6f 6f 6e 20 61 73 20  tops as soon as 
1eb0f 72 65 67 28 50 31 29 20 65 72 72 6f 72 73 20 61  reg(P1) errors a
1eb10 72 65 20 0a 2a 2a 20 73 65 65 6e 2e 20 20 52 65  re .** seen.  Re
1eb11 67 28 50 31 29 20 69 73 20 75 70 64 61 74 65 64  g(P1) is updated
1eb12 20 77 69 74 68 20 74 68 65 20 6e 75 6d 62 65 72   with the number
1eb13 20 6f 66 20 65 72 72 6f 72 73 20 72 65 6d 61 69   of errors remai
1eb14 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ning..**.** The 
1eb15 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72  root page number
1eb16 73 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20  s of all tables 
1eb17 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
1eb18 61 72 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 73  are integer.** s
1eb19 74 6f 72 65 64 20 69 6e 20 72 65 67 28 50 31 29  tored in reg(P1)
1eb1a 2c 20 72 65 67 28 50 31 2b 31 29 2c 20 72 65 67  , reg(P1+1), reg
1eb1b 28 50 31 2b 32 29 2c 20 2e 2e 2e 2e 20 20 54 68  (P1+2), ....  Th
1eb1c 65 72 65 20 61 72 65 20 50 32 20 74 61 62 6c 65  ere are P2 table
1eb1d 73 0a 2a 2a 20 74 6f 74 61 6c 2e 0a 2a 2a 0a 2a  s.** total..**.*
1eb1e 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 74 20 7a  * If P5 is not z
1eb1f 65 72 6f 2c 20 74 68 65 20 63 68 65 63 6b 20 69  ero, the check i
1eb20 73 20 64 6f 6e 65 20 6f 6e 20 74 68 65 20 61 75  s done on the au
1eb21 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73 65  xiliary database
1eb22 0a 2a 2a 20 66 69 6c 65 2c 20 6e 6f 74 20 74 68  .** file, not th
1eb23 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
1eb24 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  file..**.** This
1eb25 20 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64 20   opcode is used 
1eb26 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
1eb27 20 69 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b   integrity_check
1eb28 20 70 72 61 67 6d 61 2e 0a 2a 2f 0a 63 61 73 65   pragma..*/.case
1eb29 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b 3a   OP_IntegrityCk:
1eb2a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
1eb2b 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1eb2c 65 64 20 69 6e 74 6f 20 75 2e 62 76 20 2a 2f 0a  ed into u.bv */.
1eb2d 20 20 69 6e 74 20 6e 52 6f 6f 74 3b 20 20 20 20    int nRoot;    
1eb2e 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
1eb2f 61 62 6c 65 73 20 74 6f 20 63 68 65 63 6b 2e 20  ables to check. 
1eb30 20 28 4e 75 6d 62 65 72 20 6f 66 20 72 6f 6f 74   (Number of root
1eb31 20 70 61 67 65 73 2e 29 20 2a 2f 0a 20 20 69 6e   pages.) */.  in
1eb32 74 20 2a 61 52 6f 6f 74 3b 20 20 20 20 20 2f 2a  t *aRoot;     /*
1eb33 20 41 72 72 61 79 20 6f 66 20 72 6f 6f 74 70 61   Array of rootpa
1eb34 67 65 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 74  ge numbers for t
1eb35 61 62 6c 65 73 20 74 6f 20 62 65 20 63 68 65 63  ables to be chec
1eb36 6b 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20  ked */.  int j; 
1eb37 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
1eb38 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e   counter */.  in
1eb39 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 2f 2a  t nErr;       /*
1eb3a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   Number of error
1eb3b 73 20 72 65 70 6f 72 74 65 64 20 2a 2f 0a 20 20  s reported */.  
1eb3c 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20  char *z;        
1eb3d 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 65  /* Text of the e
1eb3e 72 72 6f 72 20 72 65 70 6f 72 74 20 2a 2f 0a 20  rror report */. 
1eb3f 20 4d 65 6d 20 2a 70 6e 45 72 72 3b 20 20 20 20   Mem *pnErr;    
1eb40 20 2f 2a 20 52 65 67 69 73 74 65 72 20 6b 65 65   /* Register kee
1eb41 70 69 6e 67 20 74 72 61 63 6b 20 6f 66 20 65 72  ping track of er
1eb42 72 6f 72 73 20 72 65 6d 61 69 6e 69 6e 67 20 2a  rors remaining *
1eb43 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  /.#endif /* loca
1eb44 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1eb45 64 20 69 6e 74 6f 20 75 2e 62 76 20 2a 2f 0a 0a  d into u.bv */..
1eb46 20 20 75 2e 62 76 2e 6e 52 6f 6f 74 20 3d 20 70    u.bv.nRoot = p
1eb47 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74  Op->p2;.  assert
1eb48 28 20 75 2e 62 76 2e 6e 52 6f 6f 74 3e 30 20 29  ( u.bv.nRoot>0 )
1eb49 3b 0a 20 20 75 2e 62 76 2e 61 52 6f 6f 74 20 3d  ;.  u.bv.aRoot =
1eb4a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
1eb4b 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69  Raw(db, sizeof(i
1eb4c 6e 74 29 2a 28 75 2e 62 76 2e 6e 52 6f 6f 74 2b  nt)*(u.bv.nRoot+
1eb4d 31 29 20 29 3b 0a 20 20 69 66 28 20 75 2e 62 76  1) );.  if( u.bv
1eb4e 2e 61 52 6f 6f 74 3d 3d 30 20 29 20 67 6f 74 6f  .aRoot==0 ) goto
1eb4f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 61 73 73 65 72   no_mem;.  asser
1eb50 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20  t( pOp->p3>0 && 
1eb51 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p3<=p->nMem
1eb52 20 29 3b 0a 20 20 75 2e 62 76 2e 70 6e 45 72 72   );.  u.bv.pnErr
1eb53 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1eb54 3e 70 33 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  >p3];.  assert( 
1eb55 28 75 2e 62 76 2e 70 6e 45 72 72 2d 3e 66 6c 61  (u.bv.pnErr->fla
1eb56 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 21 3d 30  gs & MEM_Int)!=0
1eb57 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 75   );.  assert( (u
1eb58 2e 62 76 2e 70 6e 45 72 72 2d 3e 66 6c 61 67 73  .bv.pnErr->flags
1eb59 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f   & (MEM_Str|MEM_
1eb5a 42 6c 6f 62 29 29 3d 3d 30 20 29 3b 0a 20 20 70  Blob))==0 );.  p
1eb5b 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  In1 = &p->aMem[p
1eb5c 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66 6f 72 28 75  Op->p1];.  for(u
1eb5d 2e 62 76 2e 6a 3d 30 3b 20 75 2e 62 76 2e 6a 3c  .bv.j=0; u.bv.j<
1eb5e 75 2e 62 76 2e 6e 52 6f 6f 74 3b 20 75 2e 62 76  u.bv.nRoot; u.bv
1eb5f 2e 6a 2b 2b 29 7b 0a 20 20 20 20 75 2e 62 76 2e  .j++){.    u.bv.
1eb60 61 52 6f 6f 74 5b 75 2e 62 76 2e 6a 5d 20 3d 20  aRoot[u.bv.j] = 
1eb61 28 69 6e 74 29 73 71 6c 69 74 65 33 56 64 62 65  (int)sqlite3Vdbe
1eb62 49 6e 74 56 61 6c 75 65 28 26 70 49 6e 31 5b 75  IntValue(&pIn1[u
1eb63 2e 62 76 2e 6a 5d 29 3b 0a 20 20 7d 0a 20 20 75  .bv.j]);.  }.  u
1eb64 2e 62 76 2e 61 52 6f 6f 74 5b 75 2e 62 76 2e 6a  .bv.aRoot[u.bv.j
1eb65 5d 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28  ] = 0;.  assert(
1eb66 20 70 4f 70 2d 3e 70 35 3c 64 62 2d 3e 6e 44 62   pOp->p5<db->nDb
1eb67 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   );.  assert( (p
1eb68 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31  ->btreeMask & (1
1eb69 3c 3c 70 4f 70 2d 3e 70 35 29 29 21 3d 30 20 29  <<pOp->p5))!=0 )
1eb6a 3b 0a 20 20 75 2e 62 76 2e 7a 20 3d 20 73 71 6c  ;.  u.bv.z = sql
1eb6b 69 74 65 33 42 74 72 65 65 49 6e 74 65 67 72 69  ite3BtreeIntegri
1eb6c 74 79 43 68 65 63 6b 28 64 62 2d 3e 61 44 62 5b  tyCheck(db->aDb[
1eb6d 70 4f 70 2d 3e 70 35 5d 2e 70 42 74 2c 20 75 2e  pOp->p5].pBt, u.
1eb6e 62 76 2e 61 52 6f 6f 74 2c 20 75 2e 62 76 2e 6e  bv.aRoot, u.bv.n
1eb6f 52 6f 6f 74 2c 0a 20 20 20 20 20 20 20 20 20 20  Root,.          
1eb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb71 20 20 20 20 20 20 20 28 69 6e 74 29 75 2e 62 76         (int)u.bv
1eb72 2e 70 6e 45 72 72 2d 3e 75 2e 69 2c 20 26 75 2e  .pnErr->u.i, &u.
1eb73 62 76 2e 6e 45 72 72 29 3b 0a 20 20 73 71 6c 69  bv.nErr);.  sqli
1eb74 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 75 2e  te3DbFree(db, u.
1eb75 62 76 2e 61 52 6f 6f 74 29 3b 0a 20 20 75 2e 62  bv.aRoot);.  u.b
1eb76 76 2e 70 6e 45 72 72 2d 3e 75 2e 69 20 2d 3d 20  v.pnErr->u.i -= 
1eb77 75 2e 62 76 2e 6e 45 72 72 3b 0a 20 20 73 71 6c  u.bv.nErr;.  sql
1eb78 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
1eb79 6c 6c 28 70 49 6e 31 29 3b 0a 20 20 69 66 28 20  ll(pIn1);.  if( 
1eb7a 75 2e 62 76 2e 6e 45 72 72 3d 3d 30 20 29 7b 0a  u.bv.nErr==0 ){.
1eb7b 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 76      assert( u.bv
1eb7c 2e 7a 3d 3d 30 20 29 3b 0a 20 20 7d 65 6c 73 65  .z==0 );.  }else
1eb7d 20 69 66 28 20 75 2e 62 76 2e 7a 3d 3d 30 20 29   if( u.bv.z==0 )
1eb7e 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65  {.    goto no_me
1eb7f 6d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  m;.  }else{.    
1eb80 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1eb81 74 53 74 72 28 70 49 6e 31 2c 20 75 2e 62 76 2e  tStr(pIn1, u.bv.
1eb82 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 55 54  z, -1, SQLITE_UT
1eb83 46 38 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65  F8, sqlite3_free
1eb84 29 3b 0a 20 20 7d 0a 20 20 55 50 44 41 54 45 5f  );.  }.  UPDATE_
1eb85 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 49 6e  MAX_BLOBSIZE(pIn
1eb86 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  1);.  sqlite3Vdb
1eb87 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28  eChangeEncoding(
1eb88 70 49 6e 31 2c 20 65 6e 63 6f 64 69 6e 67 29 3b  pIn1, encoding);
1eb89 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64  .  break;.}.#end
1eb8a 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1eb8b 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  T_INTEGRITY_CHEC
1eb8c 4b 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  K */../* Opcode:
1eb8d 20 52 6f 77 53 65 74 41 64 64 20 50 31 20 50 32   RowSetAdd P1 P2
1eb8e 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 73   * * *.**.** Ins
1eb8f 65 72 74 20 74 68 65 20 69 6e 74 65 67 65 72 20  ert the integer 
1eb90 76 61 6c 75 65 20 68 65 6c 64 20 62 79 20 72 65  value held by re
1eb91 67 69 73 74 65 72 20 50 32 20 69 6e 74 6f 20 61  gister P2 into a
1eb92 20 62 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 0a 2a   boolean index.*
1eb93 2a 20 68 65 6c 64 20 69 6e 20 72 65 67 69 73 74  * held in regist
1eb94 65 72 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20  er P1..**.** An 
1eb95 61 73 73 65 72 74 69 6f 6e 20 66 61 69 6c 73 20  assertion fails 
1eb96 69 66 20 50 32 20 69 73 20 6e 6f 74 20 61 6e 20  if P2 is not an 
1eb97 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 63 61 73 65  integer..*/.case
1eb98 20 4f 50 5f 52 6f 77 53 65 74 41 64 64 3a 20 7b   OP_RowSetAdd: {
1eb99 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20 2a 2f         /* in2 */
1eb9a 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1eb9b 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1eb9c 20 69 6e 74 6f 20 75 2e 62 77 20 2a 2f 0a 20 20   into u.bw */.  
1eb9d 4d 65 6d 20 2a 70 49 64 78 3b 0a 20 20 4d 65 6d  Mem *pIdx;.  Mem
1eb9e 20 2a 70 56 61 6c 3b 0a 23 65 6e 64 69 66 20 2f   *pVal;.#endif /
1eb9f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1eba0 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62  s moved into u.b
1eba1 77 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  w */.  assert( p
1eba2 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d  Op->p1>0 && pOp-
1eba3 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p1<=p->nMem );.
1eba4 20 20 75 2e 62 77 2e 70 49 64 78 20 3d 20 26 70    u.bw.pIdx = &p
1eba5 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b  ->aMem[pOp->p1];
1eba6 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
1eba7 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 3c  p2>0 && pOp->p2<
1eba8 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 75 2e  =p->nMem );.  u.
1eba9 62 77 2e 70 56 61 6c 20 3d 20 26 70 2d 3e 61 4d  bw.pVal = &p->aM
1ebaa 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 61  em[pOp->p2];.  a
1ebab 73 73 65 72 74 28 20 28 75 2e 62 77 2e 70 56 61  ssert( (u.bw.pVa
1ebac 6c 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49  l->flags & MEM_I
1ebad 6e 74 29 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  nt)!=0 );.  if( 
1ebae 28 75 2e 62 77 2e 70 49 64 78 2d 3e 66 6c 61 67  (u.bw.pIdx->flag
1ebaf 73 20 26 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d  s & MEM_RowSet)=
1ebb0 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
1ebb1 33 56 64 62 65 4d 65 6d 53 65 74 52 6f 77 53 65  3VdbeMemSetRowSe
1ebb2 74 28 75 2e 62 77 2e 70 49 64 78 29 3b 0a 20 20  t(u.bw.pIdx);.  
1ebb3 20 20 69 66 28 20 28 75 2e 62 77 2e 70 49 64 78    if( (u.bw.pIdx
1ebb4 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f  ->flags & MEM_Ro
1ebb5 77 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f 20  wSet)==0 ) goto 
1ebb6 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 73 71  no_mem;.  }.  sq
1ebb7 6c 69 74 65 33 52 6f 77 53 65 74 49 6e 73 65 72  lite3RowSetInser
1ebb8 74 28 75 2e 62 77 2e 70 49 64 78 2d 3e 75 2e 70  t(u.bw.pIdx->u.p
1ebb9 52 6f 77 53 65 74 2c 20 75 2e 62 77 2e 70 56 61  RowSet, u.bw.pVa
1ebba 6c 2d 3e 75 2e 69 29 3b 0a 20 20 62 72 65 61 6b  l->u.i);.  break
1ebbb 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1ebbc 52 6f 77 53 65 74 52 65 61 64 20 50 31 20 50 32  RowSetRead P1 P2
1ebbd 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 45 78   P3 * *.**.** Ex
1ebbe 74 72 61 63 74 20 74 68 65 20 73 6d 61 6c 6c 65  tract the smalle
1ebbf 73 74 20 76 61 6c 75 65 20 66 72 6f 6d 20 62 6f  st value from bo
1ebc0 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50 31 20 61  olean index P1 a
1ebc1 6e 64 20 70 75 74 20 74 68 61 74 20 76 61 6c 75  nd put that valu
1ebc2 65 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74  e into.** regist
1ebc3 65 72 20 50 33 2e 20 20 4f 72 2c 20 69 66 20 62  er P3.  Or, if b
1ebc4 6f 6f 6c 65 61 6e 20 69 6e 64 65 78 20 50 31 20  oolean index P1 
1ebc5 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 65 6d 70  is initially emp
1ebc6 74 79 2c 20 6c 65 61 76 65 20 50 33 0a 2a 2a 20  ty, leave P3.** 
1ebc7 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 6a 75  unchanged and ju
1ebc8 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69 6f  mp to instructio
1ebc9 6e 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  n P2..*/.case OP
1ebca 5f 52 6f 77 53 65 74 52 65 61 64 3a 20 7b 20 20  _RowSetRead: {  
1ebcb 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 6f 75       /* jump, ou
1ebcc 74 33 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20  t3 */.#if 0  /* 
1ebcd 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1ebce 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 62 78 20  moved into u.bx 
1ebcf 2a 2f 0a 20 20 4d 65 6d 20 2a 70 49 64 78 3b 0a  */.  Mem *pIdx;.
1ebd0 20 20 69 36 34 20 76 61 6c 3b 0a 23 65 6e 64 69    i64 val;.#endi
1ebd1 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1ebd2 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1ebd3 75 2e 62 78 20 2a 2f 0a 20 20 61 73 73 65 72 74  u.bx */.  assert
1ebd4 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70  ( pOp->p1>0 && p
1ebd5 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p1<=p->nMem 
1ebd6 29 3b 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49  );.  CHECK_FOR_I
1ebd7 4e 54 45 52 52 55 50 54 3b 0a 20 20 75 2e 62 78  NTERRUPT;.  u.bx
1ebd8 2e 70 49 64 78 20 3d 20 26 70 2d 3e 61 4d 65 6d  .pIdx = &p->aMem
1ebd9 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 70 4f 75  [pOp->p1];.  pOu
1ebda 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  t = &p->aMem[pOp
1ebdb 2d 3e 70 33 5d 3b 0a 20 20 69 66 28 20 28 75 2e  ->p3];.  if( (u.
1ebdc 62 78 2e 70 49 64 78 2d 3e 66 6c 61 67 73 20 26  bx.pIdx->flags &
1ebdd 20 4d 45 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 0a   MEM_RowSet)==0.
1ebde 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 52 6f 77     || sqlite3Row
1ebdf 53 65 74 4e 65 78 74 28 75 2e 62 78 2e 70 49 64  SetNext(u.bx.pId
1ebe0 78 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 26 75  x->u.pRowSet, &u
1ebe1 2e 62 78 2e 76 61 6c 29 3d 3d 30 0a 20 20 29 7b  .bx.val)==0.  ){
1ebe2 0a 20 20 20 20 2f 2a 20 54 68 65 20 62 6f 6f 6c  .    /* The bool
1ebe3 65 61 6e 20 69 6e 64 65 78 20 69 73 20 65 6d 70  ean index is emp
1ebe4 74 79 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  ty */.    sqlite
1ebe5 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28  3VdbeMemSetNull(
1ebe6 75 2e 62 78 2e 70 49 64 78 29 3b 0a 20 20 20 20  u.bx.pIdx);.    
1ebe7 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31  pc = pOp->p2 - 1
1ebe8 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
1ebe9 2a 20 41 20 76 61 6c 75 65 20 77 61 73 20 70 75  * A value was pu
1ebea 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 69 6e  lled from the in
1ebeb 64 65 78 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  dex */.    asser
1ebec 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20  t( pOp->p3>0 && 
1ebed 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p3<=p->nMem
1ebee 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   );.    sqlite3V
1ebef 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 70  dbeMemSetInt64(p
1ebf0 4f 75 74 2c 20 75 2e 62 78 2e 76 61 6c 29 3b 0a  Out, u.bx.val);.
1ebf1 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1ebf2 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 53 65  /* Opcode: RowSe
1ebf3 74 54 65 73 74 20 50 31 20 50 32 20 50 33 20 50  tTest P1 P2 P3 P
1ebf4 34 0a 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  4.**.** Register
1ebf5 20 50 33 20 69 73 20 61 73 73 75 6d 65 64 20 74   P3 is assumed t
1ebf6 6f 20 68 6f 6c 64 20 61 20 36 34 2d 62 69 74 20  o hold a 64-bit 
1ebf7 69 6e 74 65 67 65 72 20 76 61 6c 75 65 2e 20 49  integer value. I
1ebf8 66 20 72 65 67 69 73 74 65 72 20 50 31 0a 2a 2a  f register P1.**
1ebf9 20 63 6f 6e 74 61 69 6e 73 20 61 20 52 6f 77 53   contains a RowS
1ebfa 65 74 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68  et object and th
1ebfb 61 74 20 52 6f 77 53 65 74 20 6f 62 6a 65 63 74  at RowSet object
1ebfc 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65   contains.** the
1ebfd 20 76 61 6c 75 65 20 68 65 6c 64 20 69 6e 20 50   value held in P
1ebfe 33 2c 20 6a 75 6d 70 20 74 6f 20 72 65 67 69 73  3, jump to regis
1ebff 74 65 72 20 50 32 2e 20 4f 74 68 65 72 77 69 73  ter P2. Otherwis
1ec00 65 2c 20 69 6e 73 65 72 74 20 74 68 65 0a 2a 2a  e, insert the.**
1ec01 20 69 6e 74 65 67 65 72 20 69 6e 20 50 33 20 69   integer in P3 i
1ec02 6e 74 6f 20 74 68 65 20 52 6f 77 53 65 74 20 61  nto the RowSet a
1ec03 6e 64 20 63 6f 6e 74 69 6e 75 65 20 6f 6e 20 74  nd continue on t
1ec04 6f 20 74 68 65 0a 2a 2a 20 6e 65 78 74 20 6f 70  o the.** next op
1ec05 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  code..**.** The 
1ec06 52 6f 77 53 65 74 20 6f 62 6a 65 63 74 20 69 73  RowSet object is
1ec07 20 6f 70 74 69 6d 69 7a 65 64 20 66 6f 72 20 74   optimized for t
1ec08 68 65 20 63 61 73 65 20 77 68 65 72 65 20 73 75  he case where su
1ec09 63 63 65 73 73 69 76 65 20 73 65 74 73 0a 2a 2a  ccessive sets.**
1ec0a 20 6f 66 20 69 6e 74 65 67 65 72 73 2c 20 77 68   of integers, wh
1ec0b 65 72 65 20 65 61 63 68 20 73 65 74 20 63 6f 6e  ere each set con
1ec0c 74 61 69 6e 73 20 6e 6f 20 64 75 70 6c 69 63 61  tains no duplica
1ec0d 74 65 73 2e 20 45 61 63 68 20 73 65 74 0a 2a 2a  tes. Each set.**
1ec0e 20 6f 66 20 76 61 6c 75 65 73 20 69 73 20 69 64   of values is id
1ec0f 65 6e 74 69 66 69 65 64 20 62 79 20 61 20 75 6e  entified by a un
1ec10 69 71 75 65 20 50 34 20 76 61 6c 75 65 2e 20 54  ique P4 value. T
1ec11 68 65 20 66 69 72 73 74 20 73 65 74 0a 2a 2a 20  he first set.** 
1ec12 6d 75 73 74 20 68 61 76 65 20 50 34 3d 3d 30 2c  must have P4==0,
1ec13 20 74 68 65 20 66 69 6e 61 6c 20 73 65 74 20 50   the final set P
1ec14 34 3d 2d 31 2e 20 20 50 34 20 6d 75 73 74 20 62  4=-1.  P4 must b
1ec15 65 20 65 69 74 68 65 72 20 2d 31 20 6f 72 0a 2a  e either -1 or.*
1ec16 2a 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20  * non-negative. 
1ec17 20 46 6f 72 20 6e 6f 6e 2d 6e 65 67 61 74 69 76   For non-negativ
1ec18 65 20 76 61 6c 75 65 73 20 6f 66 20 50 34 20 6f  e values of P4 o
1ec19 6e 6c 79 20 74 68 65 20 6c 6f 77 65 72 20 34 0a  nly the lower 4.
1ec1a 2a 2a 20 62 69 74 73 20 61 72 65 20 73 69 67 6e  ** bits are sign
1ec1b 69 66 69 63 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  ificant..**.** T
1ec1c 68 69 73 20 61 6c 6c 6f 77 73 20 6f 70 74 69 6d  his allows optim
1ec1d 69 7a 61 74 69 6f 6e 73 3a 20 28 61 29 20 77 68  izations: (a) wh
1ec1e 65 6e 20 50 34 3d 3d 30 20 74 68 65 72 65 20 69  en P4==0 there i
1ec1f 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 74 65 73  s no need to tes
1ec20 74 0a 2a 2a 20 74 68 65 20 72 6f 77 73 65 74 20  t.** the rowset 
1ec21 6f 62 6a 65 63 74 20 66 6f 72 20 50 33 2c 20 61  object for P3, a
1ec22 73 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65  s it is guarante
1ec23 65 64 20 6e 6f 74 20 74 6f 20 63 6f 6e 74 61 69  ed not to contai
1ec24 6e 20 69 74 2c 0a 2a 2a 20 28 62 29 20 77 68 65  n it,.** (b) whe
1ec25 6e 20 50 34 3d 3d 2d 31 20 74 68 65 72 65 20 69  n P4==-1 there i
1ec26 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 69 6e 73  s no need to ins
1ec27 65 72 74 20 74 68 65 20 76 61 6c 75 65 2c 20 61  ert the value, a
1ec28 73 20 69 74 20 77 69 6c 6c 0a 2a 2a 20 6e 65 76  s it will.** nev
1ec29 65 72 20 62 65 20 74 65 73 74 65 64 20 66 6f 72  er be tested for
1ec2a 2c 20 61 6e 64 20 28 63 29 20 77 68 65 6e 20 61  , and (c) when a
1ec2b 20 76 61 6c 75 65 20 74 68 61 74 20 69 73 20 70   value that is p
1ec2c 61 72 74 20 6f 66 20 73 65 74 20 58 20 69 73 0a  art of set X is.
1ec2d 2a 2a 20 69 6e 73 65 72 74 65 64 2c 20 74 68 65  ** inserted, the
1ec2e 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f  re is no need to
1ec2f 20 73 65 61 72 63 68 20 74 6f 20 73 65 65 20 69   search to see i
1ec30 66 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65  f the same value
1ec31 20 77 61 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73   was.** previous
1ec32 6c 79 20 69 6e 73 65 72 74 65 64 20 61 73 20 70  ly inserted as p
1ec33 61 72 74 20 6f 66 20 73 65 74 20 58 20 28 6f 6e  art of set X (on
1ec34 6c 79 20 69 66 20 69 74 20 77 61 73 20 70 72 65  ly if it was pre
1ec35 76 69 6f 75 73 6c 79 0a 2a 2a 20 69 6e 73 65 72  viously.** inser
1ec36 74 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 73  ted as part of s
1ec37 6f 6d 65 20 6f 74 68 65 72 20 73 65 74 29 2e 0a  ome other set)..
1ec38 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f 77 53 65  */.case OP_RowSe
1ec39 74 54 65 73 74 3a 20 7b 20 20 20 20 20 20 20 20  tTest: {        
1ec3a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1ec3b 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a  jump, in1, in3 *
1ec3c 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  /.#if 0  /* loca
1ec3d 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1ec3e 64 20 69 6e 74 6f 20 75 2e 62 79 20 2a 2f 0a 20  d into u.by */. 
1ec3f 20 69 6e 74 20 69 53 65 74 3b 0a 20 20 69 6e 74   int iSet;.  int
1ec40 20 65 78 69 73 74 73 3b 0a 23 65 6e 64 69 66 20   exists;.#endif 
1ec41 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1ec42 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1ec43 62 79 20 2a 2f 0a 0a 20 20 75 2e 62 79 2e 69 53  by */..  u.by.iS
1ec44 65 74 20 3d 20 70 4f 70 2d 3e 70 34 2e 69 3b 0a  et = pOp->p4.i;.
1ec45 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e    assert( pIn3->
1ec46 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b  flags&MEM_Int );
1ec47 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
1ec48 69 73 20 61 6e 79 74 68 69 6e 67 20 6f 74 68 65  is anything othe
1ec49 72 20 74 68 61 6e 20 61 20 72 6f 77 73 65 74 20  r than a rowset 
1ec4a 6f 62 6a 65 63 74 20 69 6e 20 6d 65 6d 6f 72 79  object in memory
1ec4b 20 63 65 6c 6c 20 50 31 2c 0a 20 20 2a 2a 20 64   cell P1,.  ** d
1ec4c 65 6c 65 74 65 20 69 74 20 6e 6f 77 20 61 6e 64  elete it now and
1ec4d 20 69 6e 69 74 69 61 6c 69 7a 65 20 50 31 20 77   initialize P1 w
1ec4e 69 74 68 20 61 6e 20 65 6d 70 74 79 20 72 6f 77  ith an empty row
1ec4f 73 65 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  set.  */.  if( (
1ec50 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1ec51 4d 5f 52 6f 77 53 65 74 29 3d 3d 30 20 29 7b 0a  M_RowSet)==0 ){.
1ec52 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1ec53 65 6d 53 65 74 52 6f 77 53 65 74 28 70 49 6e 31  emSetRowSet(pIn1
1ec54 29 3b 0a 20 20 20 20 69 66 28 20 28 70 49 6e 31  );.    if( (pIn1
1ec55 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 6f  ->flags & MEM_Ro
1ec56 77 53 65 74 29 3d 3d 30 20 29 20 67 6f 74 6f 20  wSet)==0 ) goto 
1ec57 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 0a 20 20 61  no_mem;.  }..  a
1ec58 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79  ssert( pOp->p4ty
1ec59 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a  pe==P4_INT32 );.
1ec5a 20 20 61 73 73 65 72 74 28 20 75 2e 62 79 2e 69    assert( u.by.i
1ec5b 53 65 74 3d 3d 2d 31 20 7c 7c 20 75 2e 62 79 2e  Set==-1 || u.by.
1ec5c 69 53 65 74 3e 3d 30 20 29 3b 0a 20 20 69 66 28  iSet>=0 );.  if(
1ec5d 20 75 2e 62 79 2e 69 53 65 74 20 29 7b 0a 20 20   u.by.iSet ){.  
1ec5e 20 20 75 2e 62 79 2e 65 78 69 73 74 73 20 3d 20    u.by.exists = 
1ec5f 73 71 6c 69 74 65 33 52 6f 77 53 65 74 54 65 73  sqlite3RowSetTes
1ec60 74 28 70 49 6e 31 2d 3e 75 2e 70 52 6f 77 53 65  t(pIn1->u.pRowSe
1ec61 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
1ec62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ec63 20 20 28 75 38 29 28 75 2e 62 79 2e 69 53 65 74    (u8)(u.by.iSet
1ec64 3e 3d 30 20 3f 20 75 2e 62 79 2e 69 53 65 74 20  >=0 ? u.by.iSet 
1ec65 26 20 30 78 66 20 3a 20 30 78 66 66 29 2c 0a 20  & 0xf : 0xff),. 
1ec66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ec67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49                pI
1ec68 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 69 66  n3->u.i);.    if
1ec69 28 20 75 2e 62 79 2e 65 78 69 73 74 73 20 29 7b  ( u.by.exists ){
1ec6a 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  .      pc = pOp-
1ec6b 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20 20 62  >p2 - 1;.      b
1ec6c 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
1ec6d 20 20 69 66 28 20 75 2e 62 79 2e 69 53 65 74 3e    if( u.by.iSet>
1ec6e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
1ec6f 33 52 6f 77 53 65 74 49 6e 73 65 72 74 28 70 49  3RowSetInsert(pI
1ec70 6e 31 2d 3e 75 2e 70 52 6f 77 53 65 74 2c 20 70  n1->u.pRowSet, p
1ec71 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 7d 0a 20  In3->u.i);.  }. 
1ec72 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e   break;.}...#ifn
1ec73 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1ec74 54 52 49 47 47 45 52 0a 0a 2f 2a 20 4f 70 63 6f  TRIGGER../* Opco
1ec75 64 65 3a 20 50 72 6f 67 72 61 6d 20 50 31 20 50  de: Program P1 P
1ec76 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  2 P3 P4 *.**.** 
1ec77 45 78 65 63 75 74 65 20 74 68 65 20 74 72 69 67  Execute the trig
1ec78 67 65 72 20 70 72 6f 67 72 61 6d 20 70 61 73 73  ger program pass
1ec79 65 64 20 61 73 20 50 34 20 28 74 79 70 65 20 50  ed as P4 (type P
1ec7a 34 5f 53 55 42 50 52 4f 47 52 41 4d 29 2e 20 0a  4_SUBPROGRAM). .
1ec7b 2a 2a 0a 2a 2a 20 50 31 20 63 6f 6e 74 61 69 6e  **.** P1 contain
1ec7c 73 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66  s the address of
1ec7d 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c   the memory cell
1ec7e 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74   that contains t
1ec7f 68 65 20 66 69 72 73 74 20 6d 65 6d 6f 72 79 20  he first memory 
1ec80 0a 2a 2a 20 63 65 6c 6c 20 69 6e 20 61 6e 20 61  .** cell in an a
1ec81 72 72 61 79 20 6f 66 20 76 61 6c 75 65 73 20 75  rray of values u
1ec82 73 65 64 20 61 73 20 61 72 67 75 6d 65 6e 74 73  sed as arguments
1ec83 20 74 6f 20 74 68 65 20 73 75 62 2d 70 72 6f 67   to the sub-prog
1ec84 72 61 6d 2e 20 50 32 20 0a 2a 2a 20 63 6f 6e 74  ram. P2 .** cont
1ec85 61 69 6e 73 20 74 68 65 20 61 64 64 72 65 73 73  ains the address
1ec86 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69 66 20 74   to jump to if t
1ec87 68 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 74  he sub-program t
1ec88 68 72 6f 77 73 20 61 6e 20 49 47 4e 4f 52 45 20  hrows an IGNORE 
1ec89 0a 2a 2a 20 65 78 63 65 70 74 69 6f 6e 20 75 73  .** exception us
1ec8a 69 6e 67 20 74 68 65 20 52 41 49 53 45 28 29 20  ing the RAISE() 
1ec8b 66 75 6e 63 74 69 6f 6e 2e 20 52 65 67 69 73 74  function. Regist
1ec8c 65 72 20 50 33 20 63 6f 6e 74 61 69 6e 73 20 74  er P3 contains t
1ec8d 68 65 20 61 64 64 72 65 73 73 20 0a 2a 2a 20 6f  he address .** o
1ec8e 66 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20  f a memory cell 
1ec8f 69 6e 20 74 68 69 73 20 28 74 68 65 20 70 61 72  in this (the par
1ec90 65 6e 74 29 20 56 4d 20 74 68 61 74 20 69 73 20  ent) VM that is 
1ec91 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74 65  used to allocate
1ec92 20 74 68 65 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20   the .** memory 
1ec93 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20  required by the 
1ec94 73 75 62 2d 76 64 62 65 20 61 74 20 72 75 6e 74  sub-vdbe at runt
1ec95 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73  ime..**.** P4 is
1ec96 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
1ec97 65 20 56 4d 20 63 6f 6e 74 61 69 6e 69 6e 67 20  e VM containing 
1ec98 74 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67  the trigger prog
1ec99 72 61 6d 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ram..*/.case OP_
1ec9a 50 72 6f 67 72 61 6d 3a 20 7b 20 20 20 20 20 20  Program: {      
1ec9b 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69 66    /* jump */.#if
1ec9c 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72   0  /* local var
1ec9d 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1ec9e 6f 20 75 2e 62 7a 20 2a 2f 0a 20 20 69 6e 74 20  o u.bz */.  int 
1ec9f 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20  nMem;           
1eca0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1eca1 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72   memory register
1eca2 73 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 72 61  s for sub-progra
1eca3 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  m */.  int nByte
1eca4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1eca5 2a 20 42 79 74 65 73 20 6f 66 20 72 75 6e 74 69  * Bytes of runti
1eca6 6d 65 20 73 70 61 63 65 20 72 65 71 75 69 72 65  me space require
1eca7 64 20 66 6f 72 20 73 75 62 2d 70 72 6f 67 72 61  d for sub-progra
1eca8 6d 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52 74 3b  m */.  Mem *pRt;
1eca9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1ecaa 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20 61 6c  * Register to al
1ecab 6c 6f 63 61 74 65 20 72 75 6e 74 69 6d 65 20 73  locate runtime s
1ecac 70 61 63 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70  pace */.  Mem *p
1ecad 4d 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20  Mem;            
1ecae 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 69 74 65    /* Used to ite
1ecaf 72 61 74 65 20 74 68 72 6f 75 67 68 20 6d 65 6d  rate through mem
1ecb0 6f 72 79 20 63 65 6c 6c 73 20 2a 2f 0a 20 20 4d  ory cells */.  M
1ecb1 65 6d 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20  em *pEnd;       
1ecb2 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74 20 6d         /* Last m
1ecb3 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20 6e 65  emory cell in ne
1ecb4 77 20 61 72 72 61 79 20 2a 2f 0a 20 20 56 64 62  w array */.  Vdb
1ecb5 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 20  eFrame *pFrame; 
1ecb6 20 20 20 20 20 2f 2a 20 4e 65 77 20 76 64 62 65       /* New vdbe
1ecb7 20 66 72 61 6d 65 20 74 6f 20 65 78 65 63 75 74   frame to execut
1ecb8 65 20 69 6e 20 2a 2f 0a 20 20 53 75 62 50 72 6f  e in */.  SubPro
1ecb9 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 3b 20  gram *pProgram; 
1ecba 20 20 2f 2a 20 53 75 62 2d 70 72 6f 67 72 61 6d    /* Sub-program
1ecbb 20 74 6f 20 65 78 65 63 75 74 65 20 2a 2f 0a 20   to execute */. 
1ecbc 20 76 6f 69 64 20 2a 74 3b 20 20 20 20 20 20 20   void *t;       
1ecbd 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65           /* Toke
1ecbe 6e 20 69 64 65 6e 74 69 66 79 69 6e 67 20 74 72  n identifying tr
1ecbf 69 67 67 65 72 20 2a 2f 0a 23 65 6e 64 69 66 20  igger */.#endif 
1ecc0 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c  /* local variabl
1ecc1 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e  es moved into u.
1ecc2 62 7a 20 2a 2f 0a 0a 20 20 75 2e 62 7a 2e 70 50  bz */..  u.bz.pP
1ecc3 72 6f 67 72 61 6d 20 3d 20 70 4f 70 2d 3e 70 34  rogram = pOp->p4
1ecc4 2e 70 50 72 6f 67 72 61 6d 3b 0a 20 20 75 2e 62  .pProgram;.  u.b
1ecc5 7a 2e 70 52 74 20 3d 20 26 70 2d 3e 61 4d 65 6d  z.pRt = &p->aMem
1ecc6 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 61 73 73  [pOp->p3];.  ass
1ecc7 65 72 74 28 20 75 2e 62 7a 2e 70 50 72 6f 67 72  ert( u.bz.pProgr
1ecc8 61 6d 2d 3e 6e 4f 70 3e 30 20 29 3b 0a 0a 20 20  am->nOp>0 );..  
1ecc9 2f 2a 20 49 66 20 74 68 65 20 70 35 20 66 6c 61  /* If the p5 fla
1ecca 67 20 69 73 20 63 6c 65 61 72 2c 20 74 68 65 6e  g is clear, then
1eccb 20 72 65 63 75 72 73 69 76 65 20 69 6e 76 6f 63   recursive invoc
1eccc 61 74 69 6f 6e 20 6f 66 20 74 72 69 67 67 65 72  ation of trigger
1eccd 73 20 69 73 0a 20 20 2a 2a 20 64 69 73 61 62 6c  s is.  ** disabl
1ecce 65 64 20 66 6f 72 20 62 61 63 6b 77 61 72 64 73  ed for backwards
1eccf 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 28   compatibility (
1ecd0 70 35 20 69 73 20 73 65 74 20 69 66 20 74 68 69  p5 is set if thi
1ecd1 73 20 73 75 62 2d 70 72 6f 67 72 61 6d 0a 20 20  s sub-program.  
1ecd2 2a 2a 20 69 73 20 72 65 61 6c 6c 79 20 61 20 74  ** is really a t
1ecd3 72 69 67 67 65 72 2c 20 6e 6f 74 20 61 20 66 6f  rigger, not a fo
1ecd4 72 65 69 67 6e 20 6b 65 79 20 61 63 74 69 6f 6e  reign key action
1ecd5 2c 20 61 6e 64 20 74 68 65 20 66 6c 61 67 20 73  , and the flag s
1ecd6 65 74 0a 20 20 2a 2a 20 61 6e 64 20 63 6c 65 61  et.  ** and clea
1ecd7 72 65 64 20 62 79 20 74 68 65 20 22 50 52 41 47  red by the "PRAG
1ecd8 4d 41 20 72 65 63 75 72 73 69 76 65 5f 74 72 69  MA recursive_tri
1ecd9 67 67 65 72 73 22 20 63 6f 6d 6d 61 6e 64 20 69  ggers" command i
1ecda 73 20 63 6c 65 61 72 29 2e 0a 20 20 2a 2a 0a 20  s clear)..  **. 
1ecdb 20 2a 2a 20 49 74 20 69 73 20 72 65 63 75 72 73   ** It is recurs
1ecdc 69 76 65 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f  ive invocation o
1ecdd 66 20 74 72 69 67 67 65 72 73 2c 20 61 74 20 74  f triggers, at t
1ecde 68 65 20 53 51 4c 20 6c 65 76 65 6c 2c 20 74 68  he SQL level, th
1ecdf 61 74 20 69 73 0a 20 20 2a 2a 20 64 69 73 61 62  at is.  ** disab
1ece0 6c 65 64 2e 20 49 6e 20 73 6f 6d 65 20 63 61 73  led. In some cas
1ece1 65 73 20 61 20 73 69 6e 67 6c 65 20 74 72 69 67  es a single trig
1ece2 67 65 72 20 6d 61 79 20 67 65 6e 65 72 61 74 65  ger may generate
1ece3 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 0a 20   more than one. 
1ece4 20 2a 2a 20 53 75 62 50 72 6f 67 72 61 6d 20 28   ** SubProgram (
1ece5 69 66 20 74 68 65 20 74 72 69 67 67 65 72 20 6d  if the trigger m
1ece6 61 79 20 62 65 20 65 78 65 63 75 74 65 64 20 77  ay be executed w
1ece7 69 74 68 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e  ith more than on
1ece8 65 20 64 69 66 66 65 72 65 6e 74 0a 20 20 2a 2a  e different.  **
1ece9 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67   ON CONFLICT alg
1ecea 6f 72 69 74 68 6d 29 2e 20 53 75 62 50 72 6f 67  orithm). SubProg
1eceb 72 61 6d 20 73 74 72 75 63 74 75 72 65 73 20 61  ram structures a
1ecec 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61  ssociated with a
1eced 0a 20 20 2a 2a 20 73 69 6e 67 6c 65 20 74 72 69  .  ** single tri
1ecee 67 67 65 72 20 61 6c 6c 20 68 61 76 65 20 74 68  gger all have th
1ecef 65 20 73 61 6d 65 20 76 61 6c 75 65 20 66 6f 72  e same value for
1ecf0 20 74 68 65 20 53 75 62 50 72 6f 67 72 61 6d 2e   the SubProgram.
1ecf1 74 6f 6b 65 6e 0a 20 20 2a 2a 20 76 61 72 69 61  token.  ** varia
1ecf2 62 6c 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20 70  ble.  */.  if( p
1ecf3 4f 70 2d 3e 70 35 20 29 7b 0a 20 20 20 20 75 2e  Op->p5 ){.    u.
1ecf4 62 7a 2e 74 20 3d 20 75 2e 62 7a 2e 70 50 72 6f  bz.t = u.bz.pPro
1ecf5 67 72 61 6d 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20  gram->token;.   
1ecf6 20 66 6f 72 28 75 2e 62 7a 2e 70 46 72 61 6d 65   for(u.bz.pFrame
1ecf7 3d 70 2d 3e 70 46 72 61 6d 65 3b 20 75 2e 62 7a  =p->pFrame; u.bz
1ecf8 2e 70 46 72 61 6d 65 20 26 26 20 75 2e 62 7a 2e  .pFrame && u.bz.
1ecf9 70 46 72 61 6d 65 2d 3e 74 6f 6b 65 6e 21 3d 75  pFrame->token!=u
1ecfa 2e 62 7a 2e 74 3b 20 75 2e 62 7a 2e 70 46 72 61  .bz.t; u.bz.pFra
1ecfb 6d 65 3d 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e  me=u.bz.pFrame->
1ecfc 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 69 66  pParent);.    if
1ecfd 28 20 75 2e 62 7a 2e 70 46 72 61 6d 65 20 29 20  ( u.bz.pFrame ) 
1ecfe 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66  break;.  }..  if
1ecff 28 20 70 2d 3e 6e 46 72 61 6d 65 3e 3d 64 62 2d  ( p->nFrame>=db-
1ed00 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
1ed01 49 4d 49 54 5f 54 52 49 47 47 45 52 5f 44 45 50  IMIT_TRIGGER_DEP
1ed02 54 48 5d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  TH] ){.    rc = 
1ed03 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1ed04 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
1ed05 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
1ed06 64 62 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 6c 65  db, "too many le
1ed07 76 65 6c 73 20 6f 66 20 74 72 69 67 67 65 72 20  vels of trigger 
1ed08 72 65 63 75 72 73 69 6f 6e 22 29 3b 0a 20 20 20  recursion");.   
1ed09 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f   break;.  }..  /
1ed0a 2a 20 52 65 67 69 73 74 65 72 20 75 2e 62 7a 2e  * Register u.bz.
1ed0b 70 52 74 20 69 73 20 75 73 65 64 20 74 6f 20 73  pRt is used to s
1ed0c 74 6f 72 65 20 74 68 65 20 6d 65 6d 6f 72 79 20  tore the memory 
1ed0d 72 65 71 75 69 72 65 64 20 74 6f 20 73 61 76 65  required to save
1ed0e 20 74 68 65 20 73 74 61 74 65 0a 20 20 2a 2a 20   the state.  ** 
1ed0f 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 70  of the current p
1ed10 72 6f 67 72 61 6d 2c 20 61 6e 64 20 74 68 65 20  rogram, and the 
1ed11 6d 65 6d 6f 72 79 20 72 65 71 75 69 72 65 64 20  memory required 
1ed12 61 74 20 72 75 6e 74 69 6d 65 20 74 6f 20 65 78  at runtime to ex
1ed13 65 63 75 74 65 0a 20 20 2a 2a 20 74 68 65 20 74  ecute.  ** the t
1ed14 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e 20  rigger program. 
1ed15 49 66 20 74 68 69 73 20 74 72 69 67 67 65 72 20  If this trigger 
1ed16 68 61 73 20 62 65 65 6e 20 66 69 72 65 64 20 62  has been fired b
1ed17 65 66 6f 72 65 2c 20 74 68 65 6e 20 75 2e 62 7a  efore, then u.bz
1ed18 2e 70 52 74 0a 20 20 2a 2a 20 69 73 20 61 6c 72  .pRt.  ** is alr
1ed19 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20  eady allocated. 
1ed1a 4f 74 68 65 72 77 69 73 65 2c 20 69 74 20 6d 75  Otherwise, it mu
1ed1b 73 74 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65  st be initialize
1ed1c 64 2e 20 20 2a 2f 0a 20 20 69 66 28 20 28 75 2e  d.  */.  if( (u.
1ed1d 62 7a 2e 70 52 74 2d 3e 66 6c 61 67 73 26 4d 45  bz.pRt->flags&ME
1ed1e 4d 5f 46 72 61 6d 65 29 3d 3d 30 20 29 7b 0a 20  M_Frame)==0 ){. 
1ed1f 20 20 20 2f 2a 20 53 75 62 50 72 6f 67 72 61 6d     /* SubProgram
1ed20 2e 6e 4d 65 6d 20 69 73 20 73 65 74 20 74 6f 20  .nMem is set to 
1ed21 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 65  the number of me
1ed22 6d 6f 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20  mory cells used 
1ed23 62 79 20 74 68 65 0a 20 20 20 20 2a 2a 20 70 72  by the.    ** pr
1ed24 6f 67 72 61 6d 20 73 74 6f 72 65 64 20 69 6e 20  ogram stored in 
1ed25 53 75 62 50 72 6f 67 72 61 6d 2e 61 4f 70 2e 20  SubProgram.aOp. 
1ed26 41 73 20 77 65 6c 6c 20 61 73 20 74 68 65 73 65  As well as these
1ed27 2c 20 6f 6e 65 20 6d 65 6d 6f 72 79 0a 20 20 20  , one memory.   
1ed28 20 2a 2a 20 63 65 6c 6c 20 69 73 20 72 65 71 75   ** cell is requ
1ed29 69 72 65 64 20 66 6f 72 20 65 61 63 68 20 63 75  ired for each cu
1ed2a 72 73 6f 72 20 75 73 65 64 20 62 79 20 74 68 65  rsor used by the
1ed2b 20 70 72 6f 67 72 61 6d 2e 20 53 65 74 20 6c 6f   program. Set lo
1ed2c 63 61 6c 0a 20 20 20 20 2a 2a 20 76 61 72 69 61  cal.    ** varia
1ed2d 62 6c 65 20 75 2e 62 7a 2e 6e 4d 65 6d 20 28 61  ble u.bz.nMem (a
1ed2e 6e 64 20 6c 61 74 65 72 2c 20 56 64 62 65 46 72  nd later, VdbeFr
1ed2f 61 6d 65 2e 6e 43 68 69 6c 64 4d 65 6d 29 20 74  ame.nChildMem) t
1ed30 6f 20 74 68 69 73 20 76 61 6c 75 65 2e 0a 20 20  o this value..  
1ed31 20 20 2a 2f 0a 20 20 20 20 75 2e 62 7a 2e 6e 4d    */.    u.bz.nM
1ed32 65 6d 20 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72  em = u.bz.pProgr
1ed33 61 6d 2d 3e 6e 4d 65 6d 20 2b 20 75 2e 62 7a 2e  am->nMem + u.bz.
1ed34 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3b 0a  pProgram->nCsr;.
1ed35 20 20 20 20 75 2e 62 7a 2e 6e 42 79 74 65 20 3d      u.bz.nByte =
1ed36 20 52 4f 55 4e 44 38 28 73 69 7a 65 6f 66 28 56   ROUND8(sizeof(V
1ed37 64 62 65 46 72 61 6d 65 29 29 0a 20 20 20 20 20  dbeFrame)).     
1ed38 20 20 20 20 20 20 20 20 20 2b 20 75 2e 62 7a 2e           + u.bz.
1ed39 6e 4d 65 6d 20 2a 20 73 69 7a 65 6f 66 28 4d 65  nMem * sizeof(Me
1ed3a 6d 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  m).             
1ed3b 20 2b 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d   + u.bz.pProgram
1ed3c 2d 3e 6e 43 73 72 20 2a 20 73 69 7a 65 6f 66 28  ->nCsr * sizeof(
1ed3d 56 64 62 65 43 75 72 73 6f 72 20 2a 29 3b 0a 20  VdbeCursor *);. 
1ed3e 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 20 3d     u.bz.pFrame =
1ed3f 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
1ed40 5a 65 72 6f 28 64 62 2c 20 75 2e 62 7a 2e 6e 42  Zero(db, u.bz.nB
1ed41 79 74 65 29 3b 0a 20 20 20 20 69 66 28 20 21 75  yte);.    if( !u
1ed42 2e 62 7a 2e 70 46 72 61 6d 65 20 29 7b 0a 20 20  .bz.pFrame ){.  
1ed43 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b      goto no_mem;
1ed44 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
1ed45 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
1ed46 28 75 2e 62 7a 2e 70 52 74 29 3b 0a 20 20 20 20  (u.bz.pRt);.    
1ed47 75 2e 62 7a 2e 70 52 74 2d 3e 66 6c 61 67 73 20  u.bz.pRt->flags 
1ed48 3d 20 4d 45 4d 5f 46 72 61 6d 65 3b 0a 20 20 20  = MEM_Frame;.   
1ed49 20 75 2e 62 7a 2e 70 52 74 2d 3e 75 2e 70 46 72   u.bz.pRt->u.pFr
1ed4a 61 6d 65 20 3d 20 75 2e 62 7a 2e 70 46 72 61 6d  ame = u.bz.pFram
1ed4b 65 3b 0a 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72  e;..    u.bz.pFr
1ed4c 61 6d 65 2d 3e 76 20 3d 20 70 3b 0a 20 20 20 20  ame->v = p;.    
1ed4d 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43 68  u.bz.pFrame->nCh
1ed4e 69 6c 64 4d 65 6d 20 3d 20 75 2e 62 7a 2e 6e 4d  ildMem = u.bz.nM
1ed4f 65 6d 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72  em;.    u.bz.pFr
1ed50 61 6d 65 2d 3e 6e 43 68 69 6c 64 43 73 72 20 3d  ame->nChildCsr =
1ed51 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e   u.bz.pProgram->
1ed52 6e 43 73 72 3b 0a 20 20 20 20 75 2e 62 7a 2e 70  nCsr;.    u.bz.p
1ed53 46 72 61 6d 65 2d 3e 70 63 20 3d 20 70 63 3b 0a  Frame->pc = pc;.
1ed54 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d      u.bz.pFrame-
1ed55 3e 61 4d 65 6d 20 3d 20 70 2d 3e 61 4d 65 6d 3b  >aMem = p->aMem;
1ed56 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65  .    u.bz.pFrame
1ed57 2d 3e 6e 4d 65 6d 20 3d 20 70 2d 3e 6e 4d 65 6d  ->nMem = p->nMem
1ed58 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46 72 61 6d  ;.    u.bz.pFram
1ed59 65 2d 3e 61 70 43 73 72 20 3d 20 70 2d 3e 61 70  e->apCsr = p->ap
1ed5a 43 73 72 3b 0a 20 20 20 20 75 2e 62 7a 2e 70 46  Csr;.    u.bz.pF
1ed5b 72 61 6d 65 2d 3e 6e 43 75 72 73 6f 72 20 3d 20  rame->nCursor = 
1ed5c 70 2d 3e 6e 43 75 72 73 6f 72 3b 0a 20 20 20 20  p->nCursor;.    
1ed5d 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 61 4f 70  u.bz.pFrame->aOp
1ed5e 20 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 20 20 75   = p->aOp;.    u
1ed5f 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 4f 70 20  .bz.pFrame->nOp 
1ed60 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20 20 20 75 2e  = p->nOp;.    u.
1ed61 62 7a 2e 70 46 72 61 6d 65 2d 3e 74 6f 6b 65 6e  bz.pFrame->token
1ed62 20 3d 20 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d   = u.bz.pProgram
1ed63 2d 3e 74 6f 6b 65 6e 3b 0a 0a 20 20 20 20 75 2e  ->token;..    u.
1ed64 62 7a 2e 70 45 6e 64 20 3d 20 26 56 64 62 65 46  bz.pEnd = &VdbeF
1ed65 72 61 6d 65 4d 65 6d 28 75 2e 62 7a 2e 70 46 72  rameMem(u.bz.pFr
1ed66 61 6d 65 29 5b 75 2e 62 7a 2e 70 46 72 61 6d 65  ame)[u.bz.pFrame
1ed67 2d 3e 6e 43 68 69 6c 64 4d 65 6d 5d 3b 0a 20 20  ->nChildMem];.  
1ed68 20 20 66 6f 72 28 75 2e 62 7a 2e 70 4d 65 6d 3d    for(u.bz.pMem=
1ed69 56 64 62 65 46 72 61 6d 65 4d 65 6d 28 75 2e 62  VdbeFrameMem(u.b
1ed6a 7a 2e 70 46 72 61 6d 65 29 3b 20 75 2e 62 7a 2e  z.pFrame); u.bz.
1ed6b 70 4d 65 6d 21 3d 75 2e 62 7a 2e 70 45 6e 64 3b  pMem!=u.bz.pEnd;
1ed6c 20 75 2e 62 7a 2e 70 4d 65 6d 2b 2b 29 7b 0a 20   u.bz.pMem++){. 
1ed6d 20 20 20 20 20 75 2e 62 7a 2e 70 4d 65 6d 2d 3e       u.bz.pMem->
1ed6e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
1ed6f 3b 0a 20 20 20 20 20 20 75 2e 62 7a 2e 70 4d 65  ;.      u.bz.pMe
1ed70 6d 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20  m->db = db;.    
1ed71 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75  }.  }else{.    u
1ed72 2e 62 7a 2e 70 46 72 61 6d 65 20 3d 20 75 2e 62  .bz.pFrame = u.b
1ed73 7a 2e 70 52 74 2d 3e 75 2e 70 46 72 61 6d 65 3b  z.pRt->u.pFrame;
1ed74 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62  .    assert( u.b
1ed75 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 4d 65 6d  z.pProgram->nMem
1ed76 2b 75 2e 62 7a 2e 70 50 72 6f 67 72 61 6d 2d 3e  +u.bz.pProgram->
1ed77 6e 43 73 72 3d 3d 75 2e 62 7a 2e 70 46 72 61 6d  nCsr==u.bz.pFram
1ed78 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d 20 29 3b 0a  e->nChildMem );.
1ed79 20 20 20 20 61 73 73 65 72 74 28 20 75 2e 62 7a      assert( u.bz
1ed7a 2e 70 50 72 6f 67 72 61 6d 2d 3e 6e 43 73 72 3d  .pProgram->nCsr=
1ed7b 3d 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e 43  =u.bz.pFrame->nC
1ed7c 68 69 6c 64 43 73 72 20 29 3b 0a 20 20 20 20 61  hildCsr );.    a
1ed7d 73 73 65 72 74 28 20 70 63 3d 3d 75 2e 62 7a 2e  ssert( pc==u.bz.
1ed7e 70 46 72 61 6d 65 2d 3e 70 63 20 29 3b 0a 20 20  pFrame->pc );.  
1ed7f 7d 0a 0a 20 20 70 2d 3e 6e 46 72 61 6d 65 2b 2b  }..  p->nFrame++
1ed80 3b 0a 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d  ;.  u.bz.pFrame-
1ed81 3e 70 50 61 72 65 6e 74 20 3d 20 70 2d 3e 70 46  >pParent = p->pF
1ed82 72 61 6d 65 3b 0a 20 20 75 2e 62 7a 2e 70 46 72  rame;.  u.bz.pFr
1ed83 61 6d 65 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d  ame->lastRowid =
1ed84 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a   db->lastRowid;.
1ed85 20 20 75 2e 62 7a 2e 70 46 72 61 6d 65 2d 3e 6e    u.bz.pFrame->n
1ed86 43 68 61 6e 67 65 20 3d 20 70 2d 3e 6e 43 68 61  Change = p->nCha
1ed87 6e 67 65 3b 0a 20 20 70 2d 3e 6e 43 68 61 6e 67  nge;.  p->nChang
1ed88 65 20 3d 20 30 3b 0a 20 20 70 2d 3e 70 46 72 61  e = 0;.  p->pFra
1ed89 6d 65 20 3d 20 75 2e 62 7a 2e 70 46 72 61 6d 65  me = u.bz.pFrame
1ed8a 3b 0a 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 26 56  ;.  p->aMem = &V
1ed8b 64 62 65 46 72 61 6d 65 4d 65 6d 28 75 2e 62 7a  dbeFrameMem(u.bz
1ed8c 2e 70 46 72 61 6d 65 29 5b 2d 31 5d 3b 0a 20 20  .pFrame)[-1];.  
1ed8d 70 2d 3e 6e 4d 65 6d 20 3d 20 75 2e 62 7a 2e 70  p->nMem = u.bz.p
1ed8e 46 72 61 6d 65 2d 3e 6e 43 68 69 6c 64 4d 65 6d  Frame->nChildMem
1ed8f 3b 0a 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d  ;.  p->nCursor =
1ed90 20 28 75 31 36 29 75 2e 62 7a 2e 70 46 72 61 6d   (u16)u.bz.pFram
1ed91 65 2d 3e 6e 43 68 69 6c 64 43 73 72 3b 0a 20 20  e->nChildCsr;.  
1ed92 70 2d 3e 61 70 43 73 72 20 3d 20 28 56 64 62 65  p->apCsr = (Vdbe
1ed93 43 75 72 73 6f 72 20 2a 2a 29 26 70 2d 3e 61 4d  Cursor **)&p->aM
1ed94 65 6d 5b 70 2d 3e 6e 4d 65 6d 2b 31 5d 3b 0a 20  em[p->nMem+1];. 
1ed95 20 70 2d 3e 61 4f 70 20 3d 20 75 2e 62 7a 2e 70   p->aOp = u.bz.p
1ed96 50 72 6f 67 72 61 6d 2d 3e 61 4f 70 3b 0a 20 20  Program->aOp;.  
1ed97 70 2d 3e 6e 4f 70 20 3d 20 75 2e 62 7a 2e 70 50  p->nOp = u.bz.pP
1ed98 72 6f 67 72 61 6d 2d 3e 6e 4f 70 3b 0a 20 20 70  rogram->nOp;.  p
1ed99 63 20 3d 20 2d 31 3b 0a 0a 20 20 62 72 65 61 6b  c = -1;..  break
1ed9a 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1ed9b 50 61 72 61 6d 20 50 31 20 50 32 20 2a 20 2a 20  Param P1 P2 * * 
1ed9c 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63  *.**.** This opc
1ed9d 6f 64 65 20 69 73 20 6f 6e 6c 79 20 65 76 65 72  ode is only ever
1ed9e 20 70 72 65 73 65 6e 74 20 69 6e 20 73 75 62 2d   present in sub-
1ed9f 70 72 6f 67 72 61 6d 73 20 63 61 6c 6c 65 64 20  programs called 
1eda0 76 69 61 20 74 68 65 20 0a 2a 2a 20 4f 50 5f 50  via the .** OP_P
1eda1 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63 74 69  rogram instructi
1eda2 6f 6e 2e 20 43 6f 70 79 20 61 20 76 61 6c 75 65  on. Copy a value
1eda3 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65   currently store
1eda4 64 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 0a 2a  d in a memory .*
1eda5 2a 20 63 65 6c 6c 20 6f 66 20 74 68 65 20 63 61  * cell of the ca
1eda6 6c 6c 69 6e 67 20 28 70 61 72 65 6e 74 29 20 66  lling (parent) f
1eda7 72 61 6d 65 20 74 6f 20 63 65 6c 6c 20 50 32 20  rame to cell P2 
1eda8 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 66  in the current f
1eda9 72 61 6d 65 73 20 0a 2a 2a 20 61 64 64 72 65 73  rames .** addres
1edaa 73 20 73 70 61 63 65 2e 20 54 68 69 73 20 69 73  s space. This is
1edab 20 75 73 65 64 20 62 79 20 74 72 69 67 67 65 72   used by trigger
1edac 20 70 72 6f 67 72 61 6d 73 20 74 6f 20 61 63 63   programs to acc
1edad 65 73 73 20 74 68 65 20 6e 65 77 2e 2a 20 0a 2a  ess the new.* .*
1edae 2a 20 61 6e 64 20 6f 6c 64 2e 2a 20 76 61 6c 75  * and old.* valu
1edaf 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 64  es..**.** The ad
1edb0 64 72 65 73 73 20 6f 66 20 74 68 65 20 63 65 6c  dress of the cel
1edb1 6c 20 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20  l in the parent 
1edb2 66 72 61 6d 65 20 69 73 20 64 65 74 65 72 6d 69  frame is determi
1edb3 6e 65 64 20 62 79 20 61 64 64 69 6e 67 0a 2a 2a  ned by adding.**
1edb4 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
1edb5 65 20 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f  e P1 argument to
1edb6 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
1edb7 65 20 50 31 20 61 72 67 75 6d 65 6e 74 20 74 6f  e P1 argument to
1edb8 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20   the.** calling 
1edb9 4f 50 5f 50 72 6f 67 72 61 6d 20 69 6e 73 74 72  OP_Program instr
1edba 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20  uction..*/.case 
1edbb 4f 50 5f 50 61 72 61 6d 3a 20 7b 20 20 20 20 20  OP_Param: {     
1edbc 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1edbd 65 72 65 6c 65 61 73 65 20 2a 2f 0a 23 69 66 20  erelease */.#if 
1edbe 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69  0  /* local vari
1edbf 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f  ables moved into
1edc0 20 75 2e 63 61 20 2a 2f 0a 20 20 56 64 62 65 46   u.ca */.  VdbeF
1edc1 72 61 6d 65 20 2a 70 46 72 61 6d 65 3b 0a 20 20  rame *pFrame;.  
1edc2 4d 65 6d 20 2a 70 49 6e 3b 0a 23 65 6e 64 69 66  Mem *pIn;.#endif
1edc3 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1edc4 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1edc5 2e 63 61 20 2a 2f 0a 20 20 75 2e 63 61 2e 70 46  .ca */.  u.ca.pF
1edc6 72 61 6d 65 20 3d 20 70 2d 3e 70 46 72 61 6d 65  rame = p->pFrame
1edc7 3b 0a 20 20 75 2e 63 61 2e 70 49 6e 20 3d 20 26  ;.  u.ca.pIn = &
1edc8 75 2e 63 61 2e 70 46 72 61 6d 65 2d 3e 61 4d 65  u.ca.pFrame->aMe
1edc9 6d 5b 70 4f 70 2d 3e 70 31 20 2b 20 75 2e 63 61  m[pOp->p1 + u.ca
1edca 2e 70 46 72 61 6d 65 2d 3e 61 4f 70 5b 75 2e 63  .pFrame->aOp[u.c
1edcb 61 2e 70 46 72 61 6d 65 2d 3e 70 63 5d 2e 70 31  a.pFrame->pc].p1
1edcc 5d 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ];.  sqlite3Vdbe
1edcd 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 70  MemShallowCopy(p
1edce 4f 75 74 2c 20 75 2e 63 61 2e 70 49 6e 2c 20 4d  Out, u.ca.pIn, M
1edcf 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 62 72 65  EM_Ephem);.  bre
1edd0 61 6b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a  ak;.}..#endif /*
1edd1 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f   #ifndef SQLITE_
1edd2 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a  OMIT_TRIGGER */.
1edd3 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1edd4 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
1edd5 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6b 43 6f  ./* Opcode: FkCo
1edd6 75 6e 74 65 72 20 50 31 20 50 32 20 2a 20 2a 20  unter P1 P2 * * 
1edd7 2a 0a 2a 2a 0a 2a 2a 20 49 6e 63 72 65 6d 65 6e  *.**.** Incremen
1edd8 74 20 61 20 22 63 6f 6e 73 74 72 61 69 6e 74 20  t a "constraint 
1edd9 63 6f 75 6e 74 65 72 22 20 62 79 20 50 32 20 28  counter" by P2 (
1edda 50 32 20 6d 61 79 20 62 65 20 6e 65 67 61 74 69  P2 may be negati
1eddb 76 65 20 6f 72 20 70 6f 73 69 74 69 76 65 29 2e  ve or positive).
1eddc 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 6e 6f 6e  .** If P1 is non
1eddd 2d 7a 65 72 6f 2c 20 74 68 65 20 64 61 74 61 62  -zero, the datab
1edde 61 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 63  ase constraint c
1eddf 6f 75 6e 74 65 72 20 69 73 20 69 6e 63 72 65 6d  ounter is increm
1ede0 65 6e 74 65 64 20 0a 2a 2a 20 28 64 65 66 65 72  ented .** (defer
1ede1 72 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  red foreign key 
1ede2 63 6f 6e 73 74 72 61 69 6e 74 73 29 2e 20 4f 74  constraints). Ot
1ede3 68 65 72 77 69 73 65 2c 20 69 66 20 50 31 20 69  herwise, if P1 i
1ede4 73 20 7a 65 72 6f 2c 20 74 68 65 20 0a 2a 2a 20  s zero, the .** 
1ede5 73 74 61 74 65 6d 65 6e 74 20 63 6f 75 6e 74 65  statement counte
1ede6 72 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  r is incremented
1ede7 20 28 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 65   (immediate fore
1ede8 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
1ede9 6e 74 73 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  nts)..*/.case OP
1edea 5f 46 6b 43 6f 75 6e 74 65 72 3a 20 7b 0a 20 20  _FkCounter: {.  
1edeb 69 66 28 20 70 4f 70 2d 3e 70 31 20 29 7b 0a 20  if( pOp->p1 ){. 
1edec 20 20 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64     db->nDeferred
1eded 43 6f 6e 73 20 2b 3d 20 70 4f 70 2d 3e 70 32 3b  Cons += pOp->p2;
1edee 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 2d  .  }else{.    p-
1edef 3e 6e 46 6b 43 6f 6e 73 74 72 61 69 6e 74 20 2b  >nFkConstraint +
1edf0 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 7d 0a 20  = pOp->p2;.  }. 
1edf1 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1edf2 63 6f 64 65 3a 20 46 6b 49 66 5a 65 72 6f 20 50  code: FkIfZero P
1edf3 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1edf4 20 54 68 69 73 20 6f 70 63 6f 64 65 20 74 65 73   This opcode tes
1edf5 74 73 20 69 66 20 61 20 66 6f 72 65 69 67 6e 20  ts if a foreign 
1edf6 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63  key constraint-c
1edf7 6f 75 6e 74 65 72 20 69 73 20 63 75 72 72 65 6e  ounter is curren
1edf8 74 6c 79 20 7a 65 72 6f 2e 0a 2a 2a 20 49 66 20  tly zero..** If 
1edf9 73 6f 2c 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74  so, jump to inst
1edfa 72 75 63 74 69 6f 6e 20 50 32 2e 20 4f 74 68 65  ruction P2. Othe
1edfb 72 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 6f  rwise, fall thro
1edfc 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ugh to the next 
1edfd 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e  .** instruction.
1edfe 0a 2a 2a 0a 2a 2a 20 49 66 20 50 31 20 69 73 20  .**.** If P1 is 
1edff 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74  non-zero, then t
1ee00 68 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e  he jump is taken
1ee01 20 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65   if the database
1ee02 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f 75 6e   constraint-coun
1ee03 74 65 72 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 28  ter.** is zero (
1ee04 74 68 65 20 6f 6e 65 20 74 68 61 74 20 63 6f 75  the one that cou
1ee05 6e 74 73 20 64 65 66 65 72 72 65 64 20 63 6f 6e  nts deferred con
1ee06 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f  straint violatio
1ee07 6e 73 29 2e 20 49 66 20 50 31 20 69 73 0a 2a 2a  ns). If P1 is.**
1ee08 20 7a 65 72 6f 2c 20 74 68 65 20 6a 75 6d 70 20   zero, the jump 
1ee09 69 73 20 74 61 6b 65 6e 20 69 66 20 74 68 65 20  is taken if the 
1ee0a 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e 73 74 72  statement constr
1ee0b 61 69 6e 74 2d 63 6f 75 6e 74 65 72 20 69 73 20  aint-counter is 
1ee0c 7a 65 72 6f 0a 2a 2a 20 28 69 6d 6d 65 64 69 61  zero.** (immedia
1ee0d 74 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63  te foreign key c
1ee0e 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74  onstraint violat
1ee0f 69 6f 6e 73 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f  ions)..*/.case O
1ee10 50 5f 46 6b 49 66 5a 65 72 6f 3a 20 7b 20 20 20  P_FkIfZero: {   
1ee11 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f        /* jump */
1ee12 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 20 29  .  if( pOp->p1 )
1ee13 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6e 44  {.    if( db->nD
1ee14 65 66 65 72 72 65 64 43 6f 6e 73 3d 3d 30 20 29  eferredCons==0 )
1ee15 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b   pc = pOp->p2-1;
1ee16 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66  .  }else{.    if
1ee17 28 20 70 2d 3e 6e 46 6b 43 6f 6e 73 74 72 61 69  ( p->nFkConstrai
1ee18 6e 74 3d 3d 30 20 29 20 70 63 20 3d 20 70 4f 70  nt==0 ) pc = pOp
1ee19 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a 20 20 62 72  ->p2-1;.  }.  br
1ee1a 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  eak;.}.#endif /*
1ee1b 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f   #ifndef SQLITE_
1ee1c 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
1ee1d 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
1ee1e 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
1ee1f 52 45 4d 45 4e 54 0a 2f 2a 20 4f 70 63 6f 64 65  REMENT./* Opcode
1ee20 3a 20 4d 65 6d 4d 61 78 20 50 31 20 50 32 20 2a  : MemMax P1 P2 *
1ee21 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73   * *.**.** P1 is
1ee22 20 61 20 72 65 67 69 73 74 65 72 20 69 6e 20 74   a register in t
1ee23 68 65 20 72 6f 6f 74 20 66 72 61 6d 65 20 6f 66  he root frame of
1ee24 20 74 68 69 73 20 56 4d 20 28 74 68 65 20 72 6f   this VM (the ro
1ee25 6f 74 20 66 72 61 6d 65 20 69 73 0a 2a 2a 20 64  ot frame is.** d
1ee26 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 20 74 68  ifferent from th
1ee27 65 20 63 75 72 72 65 6e 74 20 66 72 61 6d 65 20  e current frame 
1ee28 69 66 20 74 68 69 73 20 69 6e 73 74 72 75 63 74  if this instruct
1ee29 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 65 78 65  ion is being exe
1ee2a 63 75 74 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20  cuted.** within 
1ee2b 61 20 73 75 62 2d 70 72 6f 67 72 61 6d 29 2e 20  a sub-program). 
1ee2c 53 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66  Set the value of
1ee2d 20 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20   register P1 to 
1ee2e 74 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 0a  the maximum of .
1ee2f 2a 2a 20 69 74 73 20 63 75 72 72 65 6e 74 20 76  ** its current v
1ee30 61 6c 75 65 20 61 6e 64 20 74 68 65 20 76 61 6c  alue and the val
1ee31 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1ee32 32 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  2..**.** This in
1ee33 73 74 72 75 63 74 69 6f 6e 20 74 68 72 6f 77 73  struction throws
1ee34 20 61 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65   an error if the
1ee35 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20   memory cell is 
1ee36 6e 6f 74 20 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a  not initially.**
1ee37 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a   an integer..*/.
1ee38 63 61 73 65 20 4f 50 5f 4d 65 6d 4d 61 78 3a 20  case OP_MemMax: 
1ee39 7b 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 32 20  {        /* in2 
1ee3a 2a 2f 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63  */.#if 0  /* loc
1ee3b 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1ee3c 65 64 20 69 6e 74 6f 20 75 2e 63 62 20 2a 2f 0a  ed into u.cb */.
1ee3d 20 20 4d 65 6d 20 2a 70 49 6e 31 3b 0a 20 20 56    Mem *pIn1;.  V
1ee3e 64 62 65 46 72 61 6d 65 20 2a 70 46 72 61 6d 65  dbeFrame *pFrame
1ee3f 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61  ;.#endif /* loca
1ee40 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1ee41 64 20 69 6e 74 6f 20 75 2e 63 62 20 2a 2f 0a 20  d into u.cb */. 
1ee42 20 69 66 28 20 70 2d 3e 70 46 72 61 6d 65 20 29   if( p->pFrame )
1ee43 7b 0a 20 20 20 20 66 6f 72 28 75 2e 63 62 2e 70  {.    for(u.cb.p
1ee44 46 72 61 6d 65 3d 70 2d 3e 70 46 72 61 6d 65 3b  Frame=p->pFrame;
1ee45 20 75 2e 63 62 2e 70 46 72 61 6d 65 2d 3e 70 50   u.cb.pFrame->pP
1ee46 61 72 65 6e 74 3b 20 75 2e 63 62 2e 70 46 72 61  arent; u.cb.pFra
1ee47 6d 65 3d 75 2e 63 62 2e 70 46 72 61 6d 65 2d 3e  me=u.cb.pFrame->
1ee48 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 75 2e  pParent);.    u.
1ee49 63 62 2e 70 49 6e 31 20 3d 20 26 75 2e 63 62 2e  cb.pIn1 = &u.cb.
1ee4a 70 46 72 61 6d 65 2d 3e 61 4d 65 6d 5b 70 4f 70  pFrame->aMem[pOp
1ee4b 2d 3e 70 31 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ->p1];.  }else{.
1ee4c 20 20 20 20 75 2e 63 62 2e 70 49 6e 31 20 3d 20      u.cb.pIn1 = 
1ee4d 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31  &p->aMem[pOp->p1
1ee4e 5d 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  ];.  }.  sqlite3
1ee4f 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66  VdbeMemIntegerif
1ee50 79 28 75 2e 63 62 2e 70 49 6e 31 29 3b 0a 20 20  y(u.cb.pIn1);.  
1ee51 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e  sqlite3VdbeMemIn
1ee52 74 65 67 65 72 69 66 79 28 70 49 6e 32 29 3b 0a  tegerify(pIn2);.
1ee53 20 20 69 66 28 20 75 2e 63 62 2e 70 49 6e 31 2d    if( u.cb.pIn1-
1ee54 3e 75 2e 69 3c 70 49 6e 32 2d 3e 75 2e 69 29 7b  >u.i<pIn2->u.i){
1ee55 0a 20 20 20 20 75 2e 63 62 2e 70 49 6e 31 2d 3e  .    u.cb.pIn1->
1ee56 75 2e 69 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b  u.i = pIn2->u.i;
1ee57 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1ee58 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1ee59 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d  _OMIT_AUTOINCREM
1ee5a 45 4e 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64  ENT */../* Opcod
1ee5b 65 3a 20 49 66 50 6f 73 20 50 31 20 50 32 20 2a  e: IfPos P1 P2 *
1ee5c 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68   * *.**.** If th
1ee5d 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73  e value of regis
1ee5e 74 65 72 20 50 31 20 69 73 20 31 20 6f 72 20 67  ter P1 is 1 or g
1ee5f 72 65 61 74 65 72 2c 20 6a 75 6d 70 20 74 6f 20  reater, jump to 
1ee60 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20  P2..**.** It is 
1ee61 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74  illegal to use t
1ee62 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1ee63 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68  on a register th
1ee64 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63  at does.** not c
1ee65 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65  ontain an intege
1ee66 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e  r.  An assertion
1ee67 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75   fault will resu
1ee68 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a  lt if you try..*
1ee69 2f 0a 63 61 73 65 20 4f 50 5f 49 66 50 6f 73 3a  /.case OP_IfPos:
1ee6a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
1ee6b 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65  p, in1 */.  asse
1ee6c 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26  rt( pIn1->flags&
1ee6d 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28  MEM_Int );.  if(
1ee6e 20 70 49 6e 31 2d 3e 75 2e 69 3e 30 20 29 7b 0a   pIn1->u.i>0 ){.
1ee6f 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70       pc = pOp->p
1ee70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65  2 - 1;.  }.  bre
1ee71 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1ee72 3a 20 49 66 4e 65 67 20 50 31 20 50 32 20 2a 20  : IfNeg P1 P2 * 
1ee73 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  * *.**.** If the
1ee74 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74   value of regist
1ee75 65 72 20 50 31 20 69 73 20 6c 65 73 73 20 74 68  er P1 is less th
1ee76 61 6e 20 7a 65 72 6f 2c 20 6a 75 6d 70 20 74 6f  an zero, jump to
1ee77 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69   P2. .**.** It i
1ee78 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65  s illegal to use
1ee79 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   this instructio
1ee7a 6e 20 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20  n on a register 
1ee7b 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74  that does.** not
1ee7c 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65   contain an inte
1ee7d 67 65 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69  ger.  An asserti
1ee7e 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65  on fault will re
1ee7f 73 75 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e  sult if you try.
1ee80 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 4e 65  .*/.case OP_IfNe
1ee81 67 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a  g: {        /* j
1ee82 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73  ump, in1 */.  as
1ee83 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67  sert( pIn1->flag
1ee84 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69  s&MEM_Int );.  i
1ee85 66 28 20 70 49 6e 31 2d 3e 75 2e 69 3c 30 20 29  f( pIn1->u.i<0 )
1ee86 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  {.     pc = pOp-
1ee87 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p2 - 1;.  }.  b
1ee88 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1ee89 64 65 3a 20 49 66 5a 65 72 6f 20 50 31 20 50 32  de: IfZero P1 P2
1ee8a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20   * * *.**.** If 
1ee8b 74 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67  the value of reg
1ee8c 69 73 74 65 72 20 50 31 20 69 73 20 65 78 61 63  ister P1 is exac
1ee8d 74 6c 79 20 30 2c 20 6a 75 6d 70 20 74 6f 20 50  tly 0, jump to P
1ee8e 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20  2. .**.** It is 
1ee8f 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74  illegal to use t
1ee90 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1ee91 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68  on a register th
1ee92 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63  at does.** not c
1ee93 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65  ontain an intege
1ee94 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e  r.  An assertion
1ee95 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75   fault will resu
1ee96 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a  lt if you try..*
1ee97 2f 0a 63 61 73 65 20 4f 50 5f 49 66 5a 65 72 6f  /.case OP_IfZero
1ee98 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75  : {        /* ju
1ee99 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73  mp, in1 */.  ass
1ee9a 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73  ert( pIn1->flags
1ee9b 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66  &MEM_Int );.  if
1ee9c 28 20 70 49 6e 31 2d 3e 75 2e 69 3d 3d 30 20 29  ( pIn1->u.i==0 )
1ee9d 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  {.     pc = pOp-
1ee9e 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p2 - 1;.  }.  b
1ee9f 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1eea0 64 65 3a 20 41 67 67 53 74 65 70 20 2a 20 50 32  de: AggStep * P2
1eea1 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20   P3 P4 P5.**.** 
1eea2 45 78 65 63 75 74 65 20 74 68 65 20 73 74 65 70  Execute the step
1eea3 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e   function for an
1eea4 20 61 67 67 72 65 67 61 74 65 2e 20 20 54 68 65   aggregate.  The
1eea5 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 73  .** function has
1eea6 20 50 35 20 61 72 67 75 6d 65 6e 74 73 2e 20 20   P5 arguments.  
1eea7 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72   P4 is a pointer
1eea8 20 74 6f 20 74 68 65 20 46 75 6e 63 44 65 66 0a   to the FuncDef.
1eea9 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 68 61  ** structure tha
1eeaa 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20  t specifies the 
1eeab 66 75 6e 63 74 69 6f 6e 2e 20 20 55 73 65 20 72  function.  Use r
1eeac 65 67 69 73 74 65 72 0a 2a 2a 20 50 33 20 61 73  egister.** P3 as
1eead 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72   the accumulator
1eeae 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 35 20 61  ..**.** The P5 a
1eeaf 72 67 75 6d 65 6e 74 73 20 61 72 65 20 74 61 6b  rguments are tak
1eeb0 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72  en from register
1eeb1 20 50 32 20 61 6e 64 20 69 74 73 0a 2a 2a 20 73   P2 and its.** s
1eeb2 75 63 63 65 73 73 6f 72 73 2e 0a 2a 2f 0a 63 61  uccessors..*/.ca
1eeb3 73 65 20 4f 50 5f 41 67 67 53 74 65 70 3a 20 7b  se OP_AggStep: {
1eeb4 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1eeb5 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1eeb6 20 69 6e 74 6f 20 75 2e 63 63 20 2a 2f 0a 20 20   into u.cc */.  
1eeb7 69 6e 74 20 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a  int n;.  int i;.
1eeb8 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 4d    Mem *pMem;.  M
1eeb9 65 6d 20 2a 70 52 65 63 3b 0a 20 20 73 71 6c 69  em *pRec;.  sqli
1eeba 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b  te3_context ctx;
1eebb 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
1eebc 20 2a 2a 61 70 56 61 6c 3b 0a 23 65 6e 64 69 66   **apVal;.#endif
1eebd 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1eebe 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1eebf 2e 63 63 20 2a 2f 0a 0a 20 20 75 2e 63 63 2e 6e  .cc */..  u.cc.n
1eec0 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 20 20 61 73   = pOp->p5;.  as
1eec1 73 65 72 74 28 20 75 2e 63 63 2e 6e 3e 3d 30 20  sert( u.cc.n>=0 
1eec2 29 3b 0a 20 20 75 2e 63 63 2e 70 52 65 63 20 3d  );.  u.cc.pRec =
1eec3 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1eec4 32 5d 3b 0a 20 20 75 2e 63 63 2e 61 70 56 61 6c  2];.  u.cc.apVal
1eec5 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 61   = p->apArg;.  a
1eec6 73 73 65 72 74 28 20 75 2e 63 63 2e 61 70 56 61  ssert( u.cc.apVa
1eec7 6c 20 7c 7c 20 75 2e 63 63 2e 6e 3d 3d 30 20 29  l || u.cc.n==0 )
1eec8 3b 0a 20 20 66 6f 72 28 75 2e 63 63 2e 69 3d 30  ;.  for(u.cc.i=0
1eec9 3b 20 75 2e 63 63 2e 69 3c 75 2e 63 63 2e 6e 3b  ; u.cc.i<u.cc.n;
1eeca 20 75 2e 63 63 2e 69 2b 2b 2c 20 75 2e 63 63 2e   u.cc.i++, u.cc.
1eecb 70 52 65 63 2b 2b 29 7b 0a 20 20 20 20 75 2e 63  pRec++){.    u.c
1eecc 63 2e 61 70 56 61 6c 5b 75 2e 63 63 2e 69 5d 20  c.apVal[u.cc.i] 
1eecd 3d 20 75 2e 63 63 2e 70 52 65 63 3b 0a 20 20 20  = u.cc.pRec;.   
1eece 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1eecf 74 6f 72 65 54 79 70 65 28 75 2e 63 63 2e 70 52  toreType(u.cc.pR
1eed0 65 63 29 3b 0a 20 20 7d 0a 20 20 75 2e 63 63 2e  ec);.  }.  u.cc.
1eed1 63 74 78 2e 70 46 75 6e 63 20 3d 20 70 4f 70 2d  ctx.pFunc = pOp-
1eed2 3e 70 34 2e 70 46 75 6e 63 3b 0a 20 20 61 73 73  >p4.pFunc;.  ass
1eed3 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20 26  ert( pOp->p3>0 &
1eed4 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d  & pOp->p3<=p->nM
1eed5 65 6d 20 29 3b 0a 20 20 75 2e 63 63 2e 63 74 78  em );.  u.cc.ctx
1eed6 2e 70 4d 65 6d 20 3d 20 75 2e 63 63 2e 70 4d 65  .pMem = u.cc.pMe
1eed7 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  m = &p->aMem[pOp
1eed8 2d 3e 70 33 5d 3b 0a 20 20 75 2e 63 63 2e 70 4d  ->p3];.  u.cc.pM
1eed9 65 6d 2d 3e 6e 2b 2b 3b 0a 20 20 75 2e 63 63 2e  em->n++;.  u.cc.
1eeda 63 74 78 2e 73 2e 66 6c 61 67 73 20 3d 20 4d 45  ctx.s.flags = ME
1eedb 4d 5f 4e 75 6c 6c 3b 0a 20 20 75 2e 63 63 2e 63  M_Null;.  u.cc.c
1eedc 74 78 2e 73 2e 7a 20 3d 20 30 3b 0a 20 20 75 2e  tx.s.z = 0;.  u.
1eedd 63 63 2e 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63  cc.ctx.s.zMalloc
1eede 20 3d 20 30 3b 0a 20 20 75 2e 63 63 2e 63 74 78   = 0;.  u.cc.ctx
1eedf 2e 73 2e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 75  .s.xDel = 0;.  u
1eee0 2e 63 63 2e 63 74 78 2e 73 2e 64 62 20 3d 20 64  .cc.ctx.s.db = d
1eee1 62 3b 0a 20 20 75 2e 63 63 2e 63 74 78 2e 69 73  b;.  u.cc.ctx.is
1eee2 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 75 2e 63  Error = 0;.  u.c
1eee3 63 2e 63 74 78 2e 70 43 6f 6c 6c 20 3d 20 30 3b  c.ctx.pColl = 0;
1eee4 0a 20 20 69 66 28 20 75 2e 63 63 2e 63 74 78 2e  .  if( u.cc.ctx.
1eee5 70 46 75 6e 63 2d 3e 66 6c 61 67 73 20 26 20 53  pFunc->flags & S
1eee6 51 4c 49 54 45 5f 46 55 4e 43 5f 4e 45 45 44 43  QLITE_FUNC_NEEDC
1eee7 4f 4c 4c 20 29 7b 0a 20 20 20 20 61 73 73 65 72  OLL ){.    asser
1eee8 74 28 20 70 4f 70 3e 70 2d 3e 61 4f 70 20 29 3b  t( pOp>p->aOp );
1eee9 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70  .    assert( pOp
1eeea 5b 2d 31 5d 2e 70 34 74 79 70 65 3d 3d 50 34 5f  [-1].p4type==P4_
1eeeb 43 4f 4c 4c 53 45 51 20 29 3b 0a 20 20 20 20 61  COLLSEQ );.    a
1eeec 73 73 65 72 74 28 20 70 4f 70 5b 2d 31 5d 2e 6f  ssert( pOp[-1].o
1eeed 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f 6c 6c 53 65  pcode==OP_CollSe
1eeee 71 20 29 3b 0a 20 20 20 20 75 2e 63 63 2e 63 74  q );.    u.cc.ct
1eeef 78 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31  x.pColl = pOp[-1
1eef0 5d 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a  ].p4.pColl;.  }.
1eef1 20 20 28 75 2e 63 63 2e 63 74 78 2e 70 46 75 6e    (u.cc.ctx.pFun
1eef2 63 2d 3e 78 53 74 65 70 29 28 26 75 2e 63 63 2e  c->xStep)(&u.cc.
1eef3 63 74 78 2c 20 75 2e 63 63 2e 6e 2c 20 75 2e 63  ctx, u.cc.n, u.c
1eef4 63 2e 61 70 56 61 6c 29 3b 0a 20 20 69 66 28 20  c.apVal);.  if( 
1eef5 75 2e 63 63 2e 63 74 78 2e 69 73 45 72 72 6f 72  u.cc.ctx.isError
1eef6 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
1eef7 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72  etString(&p->zEr
1eef8 72 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20  rMsg, db, "%s", 
1eef9 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
1eefa 78 74 28 26 75 2e 63 63 2e 63 74 78 2e 73 29 29  xt(&u.cc.ctx.s))
1eefb 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e 63 63 2e  ;.    rc = u.cc.
1eefc 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20 20 7d  ctx.isError;.  }
1eefd 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1eefe 6d 52 65 6c 65 61 73 65 28 26 75 2e 63 63 2e 63  mRelease(&u.cc.c
1eeff 74 78 2e 73 29 3b 0a 20 20 62 72 65 61 6b 3b 0a  tx.s);.  break;.
1ef00 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 67  }../* Opcode: Ag
1ef01 67 46 69 6e 61 6c 20 50 31 20 50 32 20 2a 20 50  gFinal P1 P2 * P
1ef02 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78 65 63 75 74  4 *.**.** Execut
1ef03 65 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 20  e the finalizer 
1ef04 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e 20  function for an 
1ef05 61 67 67 72 65 67 61 74 65 2e 20 20 50 31 20 69  aggregate.  P1 i
1ef06 73 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20  s.** the memory 
1ef07 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20 69 73  location that is
1ef08 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72   the accumulator
1ef09 20 66 6f 72 20 74 68 65 20 61 67 67 72 65 67 61   for the aggrega
1ef0a 74 65 2e 0a 2a 2a 0a 2a 2a 20 50 32 20 69 73 20  te..**.** P2 is 
1ef0b 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72  the number of ar
1ef0c 67 75 6d 65 6e 74 73 20 74 68 61 74 20 74 68 65  guments that the
1ef0d 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 74   step function t
1ef0e 61 6b 65 73 20 61 6e 64 0a 2a 2a 20 50 34 20 69  akes and.** P4 i
1ef0f 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
1ef10 68 65 20 46 75 6e 63 44 65 66 20 66 6f 72 20 74  he FuncDef for t
1ef11 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54  his function.  T
1ef12 68 65 20 50 32 0a 2a 2a 20 61 72 67 75 6d 65 6e  he P2.** argumen
1ef13 74 20 69 73 20 6e 6f 74 20 75 73 65 64 20 62 79  t is not used by
1ef14 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20 49   this opcode.  I
1ef15 74 20 69 73 20 6f 6e 6c 79 20 74 68 65 72 65 20  t is only there 
1ef16 74 6f 20 64 69 73 61 6d 62 69 67 75 61 74 65 0a  to disambiguate.
1ef17 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61  ** functions tha
1ef18 74 20 63 61 6e 20 74 61 6b 65 20 76 61 72 79 69  t can take varyi
1ef19 6e 67 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 72  ng numbers of ar
1ef1a 67 75 6d 65 6e 74 73 2e 20 20 54 68 65 0a 2a 2a  guments.  The.**
1ef1b 20 50 34 20 61 72 67 75 6d 65 6e 74 20 69 73 20   P4 argument is 
1ef1c 6f 6e 6c 79 20 6e 65 65 64 65 64 20 66 6f 72 20  only needed for 
1ef1d 74 68 65 20 64 65 67 65 6e 65 72 61 74 65 20 63  the degenerate c
1ef1e 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 74 68 65  ase where.** the
1ef1f 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 77   step function w
1ef20 61 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c  as not previousl
1ef21 79 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 63 61 73  y called..*/.cas
1ef22 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 3a 20 7b  e OP_AggFinal: {
1ef23 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1ef24 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1ef25 20 69 6e 74 6f 20 75 2e 63 64 20 2a 2f 0a 20 20   into u.cd */.  
1ef26 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 23 65 6e 64 69  Mem *pMem;.#endi
1ef27 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1ef28 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1ef29 75 2e 63 64 20 2a 2f 0a 20 20 61 73 73 65 72 74  u.cd */.  assert
1ef2a 28 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70  ( pOp->p1>0 && p
1ef2b 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20  Op->p1<=p->nMem 
1ef2c 29 3b 0a 20 20 75 2e 63 64 2e 70 4d 65 6d 20 3d  );.  u.cd.pMem =
1ef2d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1ef2e 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 75  1];.  assert( (u
1ef2f 2e 63 64 2e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  .cd.pMem->flags 
1ef30 26 20 7e 28 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d  & ~(MEM_Null|MEM
1ef31 5f 41 67 67 29 29 3d 3d 30 20 29 3b 0a 20 20 72  _Agg))==0 );.  r
1ef32 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
1ef33 65 6d 46 69 6e 61 6c 69 7a 65 28 75 2e 63 64 2e  emFinalize(u.cd.
1ef34 70 4d 65 6d 2c 20 70 4f 70 2d 3e 70 34 2e 70 46  pMem, pOp->p4.pF
1ef35 75 6e 63 29 3b 0a 20 20 69 66 28 20 72 63 20 29  unc);.  if( rc )
1ef36 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74  {.    sqlite3Set
1ef37 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d  String(&p->zErrM
1ef38 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71  sg, db, "%s", sq
1ef39 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1ef3a 28 75 2e 63 64 2e 70 4d 65 6d 29 29 3b 0a 20 20  (u.cd.pMem));.  
1ef3b 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43  }.  sqlite3VdbeC
1ef3c 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 75 2e  hangeEncoding(u.
1ef3d 63 64 2e 70 4d 65 6d 2c 20 65 6e 63 6f 64 69 6e  cd.pMem, encodin
1ef3e 67 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58  g);.  UPDATE_MAX
1ef3f 5f 42 4c 4f 42 53 49 5a 45 28 75 2e 63 64 2e 70  _BLOBSIZE(u.cd.p
1ef40 4d 65 6d 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  Mem);.  if( sqli
1ef41 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67  te3VdbeMemTooBig
1ef42 28 75 2e 63 64 2e 70 4d 65 6d 29 20 29 7b 0a 20  (u.cd.pMem) ){. 
1ef43 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b     goto too_big;
1ef44 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1ef45 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
1ef46 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55  QLITE_OMIT_VACUU
1ef47 4d 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  M) && !defined(S
1ef48 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43  QLITE_OMIT_ATTAC
1ef49 48 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 61  H)./* Opcode: Va
1ef4a 63 75 75 6d 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a  cuum * * * * *.*
1ef4b 2a 0a 2a 2a 20 56 61 63 75 75 6d 20 74 68 65 20  *.** Vacuum the 
1ef4c 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 2e  entire database.
1ef4d 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77 69    This opcode wi
1ef4e 6c 6c 20 63 61 75 73 65 20 6f 74 68 65 72 20 76  ll cause other v
1ef4f 69 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e  irtual.** machin
1ef50 65 73 20 74 6f 20 62 65 20 63 72 65 61 74 65 64  es to be created
1ef51 20 61 6e 64 20 72 75 6e 2e 20 20 49 74 20 6d 61   and run.  It ma
1ef52 79 20 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20  y not be called 
1ef53 66 72 6f 6d 20 77 69 74 68 69 6e 0a 2a 2a 20 61  from within.** a
1ef54 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2f   transaction..*/
1ef55 0a 63 61 73 65 20 4f 50 5f 56 61 63 75 75 6d 3a  .case OP_Vacuum:
1ef56 20 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   {.  if( sqlite3
1ef57 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29 20  SafetyOff(db) ) 
1ef58 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1ef59 6f 5f 6d 69 73 75 73 65 3b 20 0a 20 20 72 63 20  o_misuse; .  rc 
1ef5a 3d 20 73 71 6c 69 74 65 33 52 75 6e 56 61 63 75  = sqlite3RunVacu
1ef5b 75 6d 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  um(&p->zErrMsg, 
1ef5c 64 62 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  db);.  if( sqlit
1ef5d 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29  e3SafetyOn(db) )
1ef5e 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1ef5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 62 72 65  to_misuse;.  bre
1ef60 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  ak;.}.#endif..#i
1ef61 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
1ef62 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
1ef63 4d 29 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e  M)./* Opcode: In
1ef64 63 72 56 61 63 75 75 6d 20 50 31 20 50 32 20 2a  crVacuum P1 P2 *
1ef65 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 50 65 72 66 6f   * *.**.** Perfo
1ef66 72 6d 20 61 20 73 69 6e 67 6c 65 20 73 74 65 70  rm a single step
1ef67 20 6f 66 20 74 68 65 20 69 6e 63 72 65 6d 65 6e   of the incremen
1ef68 74 61 6c 20 76 61 63 75 75 6d 20 70 72 6f 63 65  tal vacuum proce
1ef69 64 75 72 65 20 6f 6e 0a 2a 2a 20 74 68 65 20 50  dure on.** the P
1ef6a 31 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74  1 database. If t
1ef6b 68 65 20 76 61 63 75 75 6d 20 68 61 73 20 66 69  he vacuum has fi
1ef6c 6e 69 73 68 65 64 2c 20 6a 75 6d 70 20 74 6f 20  nished, jump to 
1ef6d 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 50  instruction.** P
1ef6e 32 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 66 61  2. Otherwise, fa
1ef6f 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68  ll through to th
1ef70 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69  e next instructi
1ef71 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49  on..*/.case OP_I
1ef72 6e 63 72 56 61 63 75 75 6d 3a 20 7b 20 20 20 20  ncrVacuum: {    
1ef73 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23      /* jump */.#
1ef74 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1ef75 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1ef76 6e 74 6f 20 75 2e 63 65 20 2a 2f 0a 20 20 42 74  nto u.ce */.  Bt
1ef77 72 65 65 20 2a 70 42 74 3b 0a 23 65 6e 64 69 66  ree *pBt;.#endif
1ef78 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1ef79 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1ef7a 2e 63 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  .ce */..  assert
1ef7b 28 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20  ( pOp->p1>=0 && 
1ef7c 70 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20  pOp->p1<db->nDb 
1ef7d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d  );.  assert( (p-
1ef7e 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c  >btreeMask & (1<
1ef7f 3c 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b  <pOp->p1))!=0 );
1ef80 0a 20 20 75 2e 63 65 2e 70 42 74 20 3d 20 64 62  .  u.ce.pBt = db
1ef81 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31 5d 2e 70  ->aDb[pOp->p1].p
1ef82 42 74 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  Bt;.  rc = sqlit
1ef83 65 33 42 74 72 65 65 49 6e 63 72 56 61 63 75 75  e3BtreeIncrVacuu
1ef84 6d 28 75 2e 63 65 2e 70 42 74 29 3b 0a 20 20 69  m(u.ce.pBt);.  i
1ef85 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
1ef86 4e 45 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70  NE ){.    pc = p
1ef87 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20  Op->p2 - 1;.    
1ef88 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1ef89 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23    }.  break;.}.#
1ef8a 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70 63 6f 64 65  endif../* Opcode
1ef8b 3a 20 45 78 70 69 72 65 20 50 31 20 2a 20 2a 20  : Expire P1 * * 
1ef8c 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 61 75 73 65 20  * *.**.** Cause 
1ef8d 70 72 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74  precompiled stat
1ef8e 65 6d 65 6e 74 73 20 74 6f 20 62 65 63 6f 6d 65  ements to become
1ef8f 20 65 78 70 69 72 65 64 2e 20 41 6e 20 65 78 70   expired. An exp
1ef90 69 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a  ired statement.*
1ef91 2a 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e 20  * fails with an 
1ef92 65 72 72 6f 72 20 63 6f 64 65 20 6f 66 20 53 51  error code of SQ
1ef93 4c 49 54 45 5f 53 43 48 45 4d 41 20 69 66 20 69  LITE_SCHEMA if i
1ef94 74 20 69 73 20 65 76 65 72 20 65 78 65 63 75 74  t is ever execut
1ef95 65 64 20 0a 2a 2a 20 28 76 69 61 20 73 71 6c 69  ed .** (via sqli
1ef96 74 65 33 5f 73 74 65 70 28 29 29 2e 0a 2a 2a 20  te3_step())..** 
1ef97 0a 2a 2a 20 49 66 20 50 31 20 69 73 20 30 2c 20  .** If P1 is 0, 
1ef98 74 68 65 6e 20 61 6c 6c 20 53 51 4c 20 73 74 61  then all SQL sta
1ef99 74 65 6d 65 6e 74 73 20 62 65 63 6f 6d 65 20 65  tements become e
1ef9a 78 70 69 72 65 64 2e 20 49 66 20 50 31 20 69 73  xpired. If P1 is
1ef9b 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a 20 74 68   non-zero,.** th
1ef9c 65 6e 20 6f 6e 6c 79 20 74 68 65 20 63 75 72 72  en only the curr
1ef9d 65 6e 74 6c 79 20 65 78 65 63 75 74 69 6e 67 20  ently executing 
1ef9e 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 66 66  statement is aff
1ef9f 65 63 74 65 64 2e 20 0a 2a 2f 0a 63 61 73 65 20  ected. .*/.case 
1efa0 4f 50 5f 45 78 70 69 72 65 3a 20 7b 0a 20 20 69  OP_Expire: {.  i
1efa1 66 28 20 21 70 4f 70 2d 3e 70 31 20 29 7b 0a 20  f( !pOp->p1 ){. 
1efa2 20 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65     sqlite3Expire
1efa3 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
1efa4 74 73 28 64 62 29 3b 0a 20 20 7d 65 6c 73 65 7b  ts(db);.  }else{
1efa5 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20  .    p->expired 
1efa6 3d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  = 1;.  }.  break
1efa7 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
1efa8 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
1efa9 43 41 43 48 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a  CACHE./* Opcode:
1efaa 20 54 61 62 6c 65 4c 6f 63 6b 20 50 31 20 50 32   TableLock P1 P2
1efab 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 4f   P3 P4 *.**.** O
1efac 62 74 61 69 6e 20 61 20 6c 6f 63 6b 20 6f 6e 20  btain a lock on 
1efad 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 62  a particular tab
1efae 6c 65 2e 20 54 68 69 73 20 69 6e 73 74 72 75 63  le. This instruc
1efaf 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 75 73 65  tion is only use
1efb0 64 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 73 68  d when.** the sh
1efb1 61 72 65 64 2d 63 61 63 68 65 20 66 65 61 74 75  ared-cache featu
1efb2 72 65 20 69 73 20 65 6e 61 62 6c 65 64 2e 20 0a  re is enabled. .
1efb3 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68 65 20  **.** P1 is the 
1efb4 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74  index of the dat
1efb5 61 62 61 73 65 20 69 6e 20 73 71 6c 69 74 65 33  abase in sqlite3
1efb6 2e 61 44 62 5b 5d 20 6f 66 20 74 68 65 20 64 61  .aDb[] of the da
1efb7 74 61 62 61 73 65 0a 2a 2a 20 6f 6e 20 77 68 69  tabase.** on whi
1efb8 63 68 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 61  ch the lock is a
1efb9 63 71 75 69 72 65 64 2e 20 20 41 20 72 65 61 64  cquired.  A read
1efba 6c 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64  lock is obtained
1efbb 20 69 66 20 50 33 3d 3d 30 20 6f 72 0a 2a 2a 20   if P3==0 or.** 
1efbc 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20  a write lock if 
1efbd 50 33 3d 3d 31 2e 0a 2a 2a 0a 2a 2a 20 50 32 20  P3==1..**.** P2 
1efbe 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 72 6f 6f  contains the roo
1efbf 74 2d 70 61 67 65 20 6f 66 20 74 68 65 20 74 61  t-page of the ta
1efc0 62 6c 65 20 74 6f 20 6c 6f 63 6b 2e 0a 2a 2a 0a  ble to lock..**.
1efc1 2a 2a 20 50 34 20 63 6f 6e 74 61 69 6e 73 20 61  ** P4 contains a
1efc2 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
1efc3 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
1efc4 65 20 62 65 69 6e 67 20 6c 6f 63 6b 65 64 2e 20  e being locked. 
1efc5 54 68 69 73 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20  This is only.** 
1efc6 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65  used to generate
1efc7 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
1efc8 65 20 69 66 20 74 68 65 20 6c 6f 63 6b 20 63 61  e if the lock ca
1efc9 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64  nnot be obtained
1efca 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 61 62  ..*/.case OP_Tab
1efcb 6c 65 4c 6f 63 6b 3a 20 7b 0a 20 20 75 38 20 69  leLock: {.  u8 i
1efcc 73 57 72 69 74 65 4c 6f 63 6b 20 3d 20 28 75 38  sWriteLock = (u8
1efcd 29 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 66 28 20  )pOp->p3;.  if( 
1efce 69 73 57 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 30  isWriteLock || 0
1efcf 3d 3d 28 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  ==(db->flags&SQL
1efd0 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74  ITE_ReadUncommit
1efd1 74 65 64 29 20 29 7b 0a 20 20 20 20 69 6e 74 20  ted) ){.    int 
1efd2 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 0a 20  p1 = pOp->p1; . 
1efd3 20 20 20 61 73 73 65 72 74 28 20 70 31 3e 3d 30     assert( p1>=0
1efd4 20 26 26 20 70 31 3c 64 62 2d 3e 6e 44 62 20 29   && p1<db->nDb )
1efd5 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ;.    assert( (p
1efd6 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31  ->btreeMask & (1
1efd7 3c 3c 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 20  <<p1))!=0 );.   
1efd8 20 61 73 73 65 72 74 28 20 69 73 57 72 69 74 65   assert( isWrite
1efd9 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 69  Lock==0 || isWri
1efda 74 65 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 20  teLock==1 );.   
1efdb 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
1efdc 65 65 4c 6f 63 6b 54 61 62 6c 65 28 64 62 2d 3e  eeLockTable(db->
1efdd 61 44 62 5b 70 31 5d 2e 70 42 74 2c 20 70 4f 70  aDb[p1].pBt, pOp
1efde 2d 3e 70 32 2c 20 69 73 57 72 69 74 65 4c 6f 63  ->p2, isWriteLoc
1efdf 6b 29 3b 0a 20 20 20 20 69 66 28 20 28 72 63 26  k);.    if( (rc&
1efe0 30 78 46 46 29 3d 3d 53 51 4c 49 54 45 5f 4c 4f  0xFF)==SQLITE_LO
1efe1 43 4b 45 44 20 29 7b 0a 20 20 20 20 20 20 63 6f  CKED ){.      co
1efe2 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f  nst char *z = pO
1efe3 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20 73  p->p4.z;.      s
1efe4 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
1efe5 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c  &p->zErrMsg, db,
1efe6 20 22 64 61 74 61 62 61 73 65 20 74 61 62 6c 65   "database table
1efe7 20 69 73 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c   is locked: %s",
1efe8 20 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   z);.    }.  }. 
1efe9 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1efea 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
1efeb 53 48 41 52 45 44 5f 43 41 43 48 45 20 2a 2f 0a  SHARED_CACHE */.
1efec 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1efed 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1efee 45 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 42 65  E./* Opcode: VBe
1efef 67 69 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a  gin * * * P4 *.*
1eff0 2a 0a 2a 2a 20 50 34 20 6d 61 79 20 62 65 20 61  *.** P4 may be a
1eff1 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73   pointer to an s
1eff2 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75  qlite3_vtab stru
1eff3 63 74 75 72 65 2e 20 49 66 20 73 6f 2c 20 63 61  cture. If so, ca
1eff4 6c 6c 20 74 68 65 20 0a 2a 2a 20 78 42 65 67 69  ll the .** xBegi
1eff5 6e 20 6d 65 74 68 6f 64 20 66 6f 72 20 74 68 61  n method for tha
1eff6 74 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41  t table..**.** A
1eff7 6c 73 6f 2c 20 77 68 65 74 68 65 72 20 6f 72 20  lso, whether or 
1eff8 6e 6f 74 20 50 34 20 69 73 20 73 65 74 2c 20 63  not P4 is set, c
1eff9 68 65 63 6b 20 74 68 61 74 20 74 68 69 73 20 69  heck that this i
1effa 73 20 6e 6f 74 20 62 65 69 6e 67 20 63 61 6c 6c  s not being call
1effb 65 64 20 66 72 6f 6d 0a 2a 2a 20 77 69 74 68 69  ed from.** withi
1effc 6e 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20  n a callback to 
1effd 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
1effe 78 53 79 6e 63 28 29 20 6d 65 74 68 6f 64 2e 20  xSync() method. 
1efff 49 66 20 69 74 20 69 73 2c 20 74 68 65 20 65 72  If it is, the er
1f000 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c  ror.** code will
1f001 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49 54   be set to SQLIT
1f002 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 63 61 73  E_LOCKED..*/.cas
1f003 65 20 4f 50 5f 56 42 65 67 69 6e 3a 20 7b 0a 23  e OP_VBegin: {.#
1f004 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c 20 76  if 0  /* local v
1f005 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1f006 6e 74 6f 20 75 2e 63 66 20 2a 2f 0a 20 20 56 54  nto u.cf */.  VT
1f007 61 62 6c 65 20 2a 70 56 54 61 62 3b 0a 23 65 6e  able *pVTab;.#en
1f008 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72  dif /* local var
1f009 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74  iables moved int
1f00a 6f 20 75 2e 63 66 20 2a 2f 0a 20 20 75 2e 63 66  o u.cf */.  u.cf
1f00b 2e 70 56 54 61 62 20 3d 20 70 4f 70 2d 3e 70 34  .pVTab = pOp->p4
1f00c 2e 70 56 74 61 62 3b 0a 20 20 72 63 20 3d 20 73  .pVtab;.  rc = s
1f00d 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 28  qlite3VtabBegin(
1f00e 64 62 2c 20 75 2e 63 66 2e 70 56 54 61 62 29 3b  db, u.cf.pVTab);
1f00f 0a 20 20 69 66 28 20 75 2e 63 66 2e 70 56 54 61  .  if( u.cf.pVTa
1f010 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  b ){.    sqlite3
1f011 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45  DbFree(db, p->zE
1f012 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a  rrMsg);.    p->z
1f013 45 72 72 4d 73 67 20 3d 20 75 2e 63 66 2e 70 56  ErrMsg = u.cf.pV
1f014 54 61 62 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72  Tab->pVtab->zErr
1f015 4d 73 67 3b 0a 20 20 20 20 75 2e 63 66 2e 70 56  Msg;.    u.cf.pV
1f016 54 61 62 2d 3e 70 56 74 61 62 2d 3e 7a 45 72 72  Tab->pVtab->zErr
1f017 4d 73 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62  Msg = 0;.  }.  b
1f018 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  reak;.}.#endif /
1f019 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
1f01a 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23  RTUALTABLE */..#
1f01b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1f01c 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
1f01d 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 43 72 65 61  /* Opcode: VCrea
1f01e 74 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a  te P1 * * P4 *.*
1f01f 2a 0a 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e  *.** P4 is the n
1f020 61 6d 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c  ame of a virtual
1f021 20 74 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61   table in databa
1f022 73 65 20 50 31 2e 20 43 61 6c 6c 20 74 68 65 20  se P1. Call the 
1f023 78 43 72 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a  xCreate method.*
1f024 2a 20 66 6f 72 20 74 68 61 74 20 74 61 62 6c 65  * for that table
1f025 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 43 72  ..*/.case OP_VCr
1f026 65 61 74 65 3a 20 7b 0a 20 20 72 63 20 3d 20 73  eate: {.  rc = s
1f027 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 72  qlite3VtabCallCr
1f028 65 61 74 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31  eate(db, pOp->p1
1f029 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 26 70 2d  , pOp->p4.z, &p-
1f02a 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 62 72 65  >zErrMsg);.  bre
1f02b 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
1f02c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1f02d 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66  UALTABLE */..#if
1f02e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1f02f 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a  _VIRTUALTABLE./*
1f030 20 4f 70 63 6f 64 65 3a 20 56 44 65 73 74 72 6f   Opcode: VDestro
1f031 79 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a  y P1 * * P4 *.**
1f032 0a 2a 2a 20 50 34 20 69 73 20 74 68 65 20 6e 61  .** P4 is the na
1f033 6d 65 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20  me of a virtual 
1f034 74 61 62 6c 65 20 69 6e 20 64 61 74 61 62 61 73  table in databas
1f035 65 20 50 31 2e 20 20 43 61 6c 6c 20 74 68 65 20  e P1.  Call the 
1f036 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f 64 0a  xDestroy method.
1f037 2a 2a 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65  ** of that table
1f038 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 44 65  ..*/.case OP_VDe
1f039 73 74 72 6f 79 3a 20 7b 0a 20 20 70 2d 3e 69 6e  stroy: {.  p->in
1f03a 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 32 3b 0a  VtabMethod = 2;.
1f03b 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74    rc = sqlite3Vt
1f03c 61 62 43 61 6c 6c 44 65 73 74 72 6f 79 28 64 62  abCallDestroy(db
1f03d 2c 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e  , pOp->p1, pOp->
1f03e 70 34 2e 7a 29 3b 0a 20 20 70 2d 3e 69 6e 56 74  p4.z);.  p->inVt
1f03f 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20  abMethod = 0;.  
1f040 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
1f041 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
1f042 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a  IRTUALTABLE */..
1f043 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1f044 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1f045 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 56 4f 70 65  ./* Opcode: VOpe
1f046 6e 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a  n P1 * * P4 *.**
1f047 0a 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e  .** P4 is a poin
1f048 74 65 72 20 74 6f 20 61 20 76 69 72 74 75 61 6c  ter to a virtual
1f049 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61   table object, a
1f04a 6e 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73  n sqlite3_vtab s
1f04b 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 50 31 20  tructure..** P1 
1f04c 69 73 20 61 20 63 75 72 73 6f 72 20 6e 75 6d 62  is a cursor numb
1f04d 65 72 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65  er.  This opcode
1f04e 20 6f 70 65 6e 73 20 61 20 63 75 72 73 6f 72 20   opens a cursor 
1f04f 74 6f 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a  to the virtual.*
1f050 2a 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72  * table and stor
1f051 65 73 20 74 68 61 74 20 63 75 72 73 6f 72 20 69  es that cursor i
1f052 6e 20 50 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  n P1..*/.case OP
1f053 5f 56 4f 70 65 6e 3a 20 7b 0a 23 69 66 20 30 20  _VOpen: {.#if 0 
1f054 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   /* local variab
1f055 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75  les moved into u
1f056 2e 63 67 20 2a 2f 0a 20 20 56 64 62 65 43 75 72  .cg */.  VdbeCur
1f057 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 73 71 6c  sor *pCur;.  sql
1f058 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
1f059 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20   *pVtabCursor;. 
1f05a 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
1f05b 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Vtab;.  sqlite3_
1f05c 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b  module *pModule;
1f05d 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1f05e 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1f05f 20 69 6e 74 6f 20 75 2e 63 67 20 2a 2f 0a 0a 20   into u.cg */.. 
1f060 20 75 2e 63 67 2e 70 43 75 72 20 3d 20 30 3b 0a   u.cg.pCur = 0;.
1f061 20 20 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73    u.cg.pVtabCurs
1f062 6f 72 20 3d 20 30 3b 0a 20 20 75 2e 63 67 2e 70  or = 0;.  u.cg.p
1f063 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70  Vtab = pOp->p4.p
1f064 56 74 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 75  Vtab->pVtab;.  u
1f065 2e 63 67 2e 70 4d 6f 64 75 6c 65 20 3d 20 28 73  .cg.pModule = (s
1f066 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29  qlite3_module *)
1f067 75 2e 63 67 2e 70 56 74 61 62 2d 3e 70 4d 6f 64  u.cg.pVtab->pMod
1f068 75 6c 65 3b 0a 20 20 61 73 73 65 72 74 28 75 2e  ule;.  assert(u.
1f069 63 67 2e 70 56 74 61 62 20 26 26 20 75 2e 63 67  cg.pVtab && u.cg
1f06a 2e 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 69 66 28  .pModule);.  if(
1f06b 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
1f06c 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  f(db) ) goto abo
1f06d 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1f06e 3b 0a 20 20 72 63 20 3d 20 75 2e 63 67 2e 70 4d  ;.  rc = u.cg.pM
1f06f 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e 28 75 2e 63  odule->xOpen(u.c
1f070 67 2e 70 56 74 61 62 2c 20 26 75 2e 63 67 2e 70  g.pVtab, &u.cg.p
1f071 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 73  VtabCursor);.  s
1f072 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1f073 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20   p->zErrMsg);.  
1f074 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63  p->zErrMsg = u.c
1f075 67 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  g.pVtab->zErrMsg
1f076 3b 0a 20 20 75 2e 63 67 2e 70 56 74 61 62 2d 3e  ;.  u.cg.pVtab->
1f077 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69  zErrMsg = 0;.  i
1f078 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1f079 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  On(db) ) goto ab
1f07a 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
1f07b 65 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  e;.  if( SQLITE_
1f07c 4f 4b 3d 3d 72 63 20 29 7b 0a 20 20 20 20 2f 2a  OK==rc ){.    /*
1f07d 20 49 6e 69 74 69 61 6c 69 7a 65 20 73 71 6c 69   Initialize sqli
1f07e 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
1f07f 62 61 73 65 20 63 6c 61 73 73 20 2a 2f 0a 20 20  base class */.  
1f080 20 20 75 2e 63 67 2e 70 56 74 61 62 43 75 72 73    u.cg.pVtabCurs
1f081 6f 72 2d 3e 70 56 74 61 62 20 3d 20 75 2e 63 67  or->pVtab = u.cg
1f082 2e 70 56 74 61 62 3b 0a 0a 20 20 20 20 2f 2a 20  .pVtab;..    /* 
1f083 49 6e 69 74 69 61 6c 69 73 65 20 76 64 62 65 20  Initialise vdbe 
1f084 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 20 2a 2f  cursor object */
1f085 0a 20 20 20 20 75 2e 63 67 2e 70 43 75 72 20 3d  .    u.cg.pCur =
1f086 20 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28   allocateCursor(
1f087 70 2c 20 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 2d  p, pOp->p1, 0, -
1f088 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 75  1, 0);.    if( u
1f089 2e 63 67 2e 70 43 75 72 20 29 7b 0a 20 20 20 20  .cg.pCur ){.    
1f08a 20 20 75 2e 63 67 2e 70 43 75 72 2d 3e 70 56 74    u.cg.pCur->pVt
1f08b 61 62 43 75 72 73 6f 72 20 3d 20 75 2e 63 67 2e  abCursor = u.cg.
1f08c 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20  pVtabCursor;.   
1f08d 20 20 20 75 2e 63 67 2e 70 43 75 72 2d 3e 70 4d     u.cg.pCur->pM
1f08e 6f 64 75 6c 65 20 3d 20 75 2e 63 67 2e 70 56 74  odule = u.cg.pVt
1f08f 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 2d  abCursor->pVtab-
1f090 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 7d 65  >pModule;.    }e
1f091 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d  lse{.      db->m
1f092 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
1f093 0a 20 20 20 20 20 20 75 2e 63 67 2e 70 4d 6f 64  .      u.cg.pMod
1f094 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 75 2e 63 67  ule->xClose(u.cg
1f095 2e 70 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20  .pVtabCursor);. 
1f096 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b     }.  }.  break
1f097 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
1f098 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1f099 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64  LTABLE */..#ifnd
1f09a 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
1f09b 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f  IRTUALTABLE./* O
1f09c 70 63 6f 64 65 3a 20 56 46 69 6c 74 65 72 20 50  pcode: VFilter P
1f09d 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a  1 P2 P3 P4 *.**.
1f09e 2a 2a 20 50 31 20 69 73 20 61 20 63 75 72 73 6f  ** P1 is a curso
1f09f 72 20 6f 70 65 6e 65 64 20 75 73 69 6e 67 20 56  r opened using V
1f0a0 4f 70 65 6e 2e 20 20 50 32 20 69 73 20 61 6e 20  Open.  P2 is an 
1f0a1 61 64 64 72 65 73 73 20 74 6f 20 6a 75 6d 70 20  address to jump 
1f0a2 74 6f 20 69 66 0a 2a 2a 20 74 68 65 20 66 69 6c  to if.** the fil
1f0a3 74 65 72 65 64 20 72 65 73 75 6c 74 20 73 65 74  tered result set
1f0a4 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a   is empty..**.**
1f0a5 20 50 34 20 69 73 20 65 69 74 68 65 72 20 4e 55   P4 is either NU
1f0a6 4c 4c 20 6f 72 20 61 20 73 74 72 69 6e 67 20 74  LL or a string t
1f0a7 68 61 74 20 77 61 73 20 67 65 6e 65 72 61 74 65  hat was generate
1f0a8 64 20 62 79 20 74 68 65 20 78 42 65 73 74 49 6e  d by the xBestIn
1f0a9 64 65 78 0a 2a 2a 20 6d 65 74 68 6f 64 20 6f 66  dex.** method of
1f0aa 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68   the module.  Th
1f0ab 65 20 69 6e 74 65 72 70 72 65 74 61 74 69 6f 6e  e interpretation
1f0ac 20 6f 66 20 74 68 65 20 50 34 20 73 74 72 69 6e   of the P4 strin
1f0ad 67 20 69 73 20 6c 65 66 74 0a 2a 2a 20 74 6f 20  g is left.** to 
1f0ae 74 68 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65  the module imple
1f0af 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  mentation..**.**
1f0b0 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69 6e 76   This opcode inv
1f0b1 6f 6b 65 73 20 74 68 65 20 78 46 69 6c 74 65 72  okes the xFilter
1f0b2 20 6d 65 74 68 6f 64 20 6f 6e 20 74 68 65 20 76   method on the v
1f0b3 69 72 74 75 61 6c 20 74 61 62 6c 65 20 73 70 65  irtual table spe
1f0b4 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 50 31 2e  cified.** by P1.
1f0b5 20 20 54 68 65 20 69 6e 74 65 67 65 72 20 71 75    The integer qu
1f0b6 65 72 79 20 70 6c 61 6e 20 70 61 72 61 6d 65 74  ery plan paramet
1f0b7 65 72 20 74 6f 20 78 46 69 6c 74 65 72 20 69 73  er to xFilter is
1f0b8 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69 73   stored in regis
1f0b9 74 65 72 0a 2a 2a 20 50 33 2e 20 52 65 67 69 73  ter.** P3. Regis
1f0ba 74 65 72 20 50 33 2b 31 20 73 74 6f 72 65 73 20  ter P3+1 stores 
1f0bb 74 68 65 20 61 72 67 63 20 70 61 72 61 6d 65 74  the argc paramet
1f0bc 65 72 20 74 6f 20 62 65 20 70 61 73 73 65 64 20  er to be passed 
1f0bd 74 6f 20 74 68 65 0a 2a 2a 20 78 46 69 6c 74 65  to the.** xFilte
1f0be 72 20 6d 65 74 68 6f 64 2e 20 52 65 67 69 73 74  r method. Regist
1f0bf 65 72 73 20 50 33 2b 32 2e 2e 50 33 2b 31 2b 61  ers P3+2..P3+1+a
1f0c0 72 67 63 20 61 72 65 20 74 68 65 20 61 72 67 63  rgc are the argc
1f0c1 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 61 6c 20 70  .** additional p
1f0c2 61 72 61 6d 65 74 65 72 73 20 77 68 69 63 68 20  arameters which 
1f0c3 61 72 65 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a  are passed to.**
1f0c4 20 78 46 69 6c 74 65 72 20 61 73 20 61 72 67 76   xFilter as argv
1f0c5 2e 20 52 65 67 69 73 74 65 72 20 50 33 2b 32 20  . Register P3+2 
1f0c6 62 65 63 6f 6d 65 73 20 61 72 67 76 5b 30 5d 20  becomes argv[0] 
1f0c7 77 68 65 6e 20 70 61 73 73 65 64 20 74 6f 20 78  when passed to x
1f0c8 46 69 6c 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20  Filter..**.** A 
1f0c9 6a 75 6d 70 20 69 73 20 6d 61 64 65 20 74 6f 20  jump is made to 
1f0ca 50 32 20 69 66 20 74 68 65 20 72 65 73 75 6c 74  P2 if the result
1f0cb 20 73 65 74 20 61 66 74 65 72 20 66 69 6c 74 65   set after filte
1f0cc 72 69 6e 67 20 77 6f 75 6c 64 20 62 65 20 65 6d  ring would be em
1f0cd 70 74 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  pty..*/.case OP_
1f0ce 56 46 69 6c 74 65 72 3a 20 7b 20 20 20 2f 2a 20  VFilter: {   /* 
1f0cf 6a 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f  jump */.#if 0  /
1f0d0 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  * local variable
1f0d1 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63  s moved into u.c
1f0d2 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 3b  h */.  int nArg;
1f0d3 0a 20 20 69 6e 74 20 69 51 75 65 72 79 3b 0a 20  .  int iQuery;. 
1f0d4 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
1f0d5 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a  odule *pModule;.
1f0d6 20 20 4d 65 6d 20 2a 70 51 75 65 72 79 3b 0a 20    Mem *pQuery;. 
1f0d7 20 4d 65 6d 20 2a 70 41 72 67 63 3b 0a 20 20 73   Mem *pArgc;.  s
1f0d8 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
1f0d9 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b  or *pVtabCursor;
1f0da 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
1f0db 2a 70 56 74 61 62 3b 0a 20 20 56 64 62 65 43 75  *pVtab;.  VdbeCu
1f0dc 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 69 6e  rsor *pCur;.  in
1f0dd 74 20 72 65 73 3b 0a 20 20 69 6e 74 20 69 3b 0a  t res;.  int i;.
1f0de 20 20 4d 65 6d 20 2a 2a 61 70 41 72 67 3b 0a 23    Mem **apArg;.#
1f0df 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20 76  endif /* local v
1f0e0 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20 69  ariables moved i
1f0e1 6e 74 6f 20 75 2e 63 68 20 2a 2f 0a 0a 20 20 75  nto u.ch */..  u
1f0e2 2e 63 68 2e 70 51 75 65 72 79 20 3d 20 26 70 2d  .ch.pQuery = &p-
1f0e3 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  >aMem[pOp->p3];.
1f0e4 20 20 75 2e 63 68 2e 70 41 72 67 63 20 3d 20 26    u.ch.pArgc = &
1f0e5 75 2e 63 68 2e 70 51 75 65 72 79 5b 31 5d 3b 0a  u.ch.pQuery[1];.
1f0e6 20 20 75 2e 63 68 2e 70 43 75 72 20 3d 20 70 2d    u.ch.pCur = p-
1f0e7 3e 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b  >apCsr[pOp->p1];
1f0e8 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43  .  REGISTER_TRAC
1f0e9 45 28 70 4f 70 2d 3e 70 33 2c 20 75 2e 63 68 2e  E(pOp->p3, u.ch.
1f0ea 70 51 75 65 72 79 29 3b 0a 20 20 61 73 73 65 72  pQuery);.  asser
1f0eb 74 28 20 75 2e 63 68 2e 70 43 75 72 2d 3e 70 56  t( u.ch.pCur->pV
1f0ec 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 75  tabCursor );.  u
1f0ed 2e 63 68 2e 70 56 74 61 62 43 75 72 73 6f 72 20  .ch.pVtabCursor 
1f0ee 3d 20 75 2e 63 68 2e 70 43 75 72 2d 3e 70 56 74  = u.ch.pCur->pVt
1f0ef 61 62 43 75 72 73 6f 72 3b 0a 20 20 75 2e 63 68  abCursor;.  u.ch
1f0f0 2e 70 56 74 61 62 20 3d 20 75 2e 63 68 2e 70 56  .pVtab = u.ch.pV
1f0f1 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62  tabCursor->pVtab
1f0f2 3b 0a 20 20 75 2e 63 68 2e 70 4d 6f 64 75 6c 65  ;.  u.ch.pModule
1f0f3 20 3d 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e 70   = u.ch.pVtab->p
1f0f4 4d 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a 20 47 72  Module;..  /* Gr
1f0f5 61 62 20 74 68 65 20 69 6e 64 65 78 20 6e 75 6d  ab the index num
1f0f6 62 65 72 20 61 6e 64 20 61 72 67 63 20 70 61 72  ber and argc par
1f0f7 61 6d 65 74 65 72 73 20 2a 2f 0a 20 20 61 73 73  ameters */.  ass
1f0f8 65 72 74 28 20 28 75 2e 63 68 2e 70 51 75 65 72  ert( (u.ch.pQuer
1f0f9 79 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 49 6e 74  y->flags&MEM_Int
1f0fa 29 21 3d 30 20 26 26 20 75 2e 63 68 2e 70 41 72  )!=0 && u.ch.pAr
1f0fb 67 63 2d 3e 66 6c 61 67 73 3d 3d 4d 45 4d 5f 49  gc->flags==MEM_I
1f0fc 6e 74 20 29 3b 0a 20 20 75 2e 63 68 2e 6e 41 72  nt );.  u.ch.nAr
1f0fd 67 20 3d 20 28 69 6e 74 29 75 2e 63 68 2e 70 41  g = (int)u.ch.pA
1f0fe 72 67 63 2d 3e 75 2e 69 3b 0a 20 20 75 2e 63 68  rgc->u.i;.  u.ch
1f0ff 2e 69 51 75 65 72 79 20 3d 20 28 69 6e 74 29 75  .iQuery = (int)u
1f100 2e 63 68 2e 70 51 75 65 72 79 2d 3e 75 2e 69 3b  .ch.pQuery->u.i;
1f101 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68  ..  /* Invoke th
1f102 65 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64  e xFilter method
1f103 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 2e 63 68   */.  {.    u.ch
1f104 2e 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 75 2e  .res = 0;.    u.
1f105 63 68 2e 61 70 41 72 67 20 3d 20 70 2d 3e 61 70  ch.apArg = p->ap
1f106 41 72 67 3b 0a 20 20 20 20 66 6f 72 28 75 2e 63  Arg;.    for(u.c
1f107 68 2e 69 20 3d 20 30 3b 20 75 2e 63 68 2e 69 3c  h.i = 0; u.ch.i<
1f108 75 2e 63 68 2e 6e 41 72 67 3b 20 75 2e 63 68 2e  u.ch.nArg; u.ch.
1f109 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 2e 63 68  i++){.      u.ch
1f10a 2e 61 70 41 72 67 5b 75 2e 63 68 2e 69 5d 20 3d  .apArg[u.ch.i] =
1f10b 20 26 75 2e 63 68 2e 70 41 72 67 63 5b 75 2e 63   &u.ch.pArgc[u.c
1f10c 68 2e 69 2b 31 5d 3b 0a 20 20 20 20 20 20 73 71  h.i+1];.      sq
1f10d 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 74 6f 72  lite3VdbeMemStor
1f10e 65 54 79 70 65 28 75 2e 63 68 2e 61 70 41 72 67  eType(u.ch.apArg
1f10f 5b 75 2e 63 68 2e 69 5d 29 3b 0a 20 20 20 20 7d  [u.ch.i]);.    }
1f110 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ..    if( sqlite
1f111 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29  3SafetyOff(db) )
1f112 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1f113 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 70  to_misuse;.    p
1f114 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d  ->inVtabMethod =
1f115 20 31 3b 0a 20 20 20 20 72 63 20 3d 20 75 2e 63   1;.    rc = u.c
1f116 68 2e 70 4d 6f 64 75 6c 65 2d 3e 78 46 69 6c 74  h.pModule->xFilt
1f117 65 72 28 75 2e 63 68 2e 70 56 74 61 62 43 75 72  er(u.ch.pVtabCur
1f118 73 6f 72 2c 20 75 2e 63 68 2e 69 51 75 65 72 79  sor, u.ch.iQuery
1f119 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 75 2e 63  , pOp->p4.z, u.c
1f11a 68 2e 6e 41 72 67 2c 20 75 2e 63 68 2e 61 70 41  h.nArg, u.ch.apA
1f11b 72 67 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74  rg);.    p->inVt
1f11c 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20  abMethod = 0;.  
1f11d 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1f11e 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b  db, p->zErrMsg);
1f11f 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20  .    p->zErrMsg 
1f120 3d 20 75 2e 63 68 2e 70 56 74 61 62 2d 3e 7a 45  = u.ch.pVtab->zE
1f121 72 72 4d 73 67 3b 0a 20 20 20 20 75 2e 63 68 2e  rrMsg;.    u.ch.
1f122 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d  pVtab->zErrMsg =
1f123 20 30 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   0;.    if( rc==
1f124 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1f125 20 20 20 75 2e 63 68 2e 72 65 73 20 3d 20 75 2e     u.ch.res = u.
1f126 63 68 2e 70 4d 6f 64 75 6c 65 2d 3e 78 45 6f 66  ch.pModule->xEof
1f127 28 75 2e 63 68 2e 70 56 74 61 62 43 75 72 73 6f  (u.ch.pVtabCurso
1f128 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  r);.    }.    if
1f129 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1f12a 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  n(db) ) goto abo
1f12b 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1f12c 3b 0a 0a 20 20 20 20 69 66 28 20 75 2e 63 68 2e  ;..    if( u.ch.
1f12d 72 65 73 20 29 7b 0a 20 20 20 20 20 20 70 63 20  res ){.      pc 
1f12e 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1f12f 20 20 20 7d 0a 20 20 7d 0a 20 20 75 2e 63 68 2e     }.  }.  u.ch.
1f130 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20  pCur->nullRow = 
1f131 30 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23  0;..  break;.}.#
1f132 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1f133 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1f134 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  E */..#ifndef SQ
1f135 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1f136 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65  LTABLE./* Opcode
1f137 3a 20 56 43 6f 6c 75 6d 6e 20 50 31 20 50 32 20  : VColumn P1 P2 
1f138 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f  P3 * *.**.** Sto
1f139 72 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  re the value of 
1f13a 74 68 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e  the P2-th column
1f13b 20 6f 66 0a 2a 2a 20 74 68 65 20 72 6f 77 20 6f   of.** the row o
1f13c 66 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61  f the virtual-ta
1f13d 62 6c 65 20 74 68 61 74 20 74 68 65 20 0a 2a 2a  ble that the .**
1f13e 20 50 31 20 63 75 72 73 6f 72 20 69 73 20 70 6f   P1 cursor is po
1f13f 69 6e 74 69 6e 67 20 74 6f 20 69 6e 74 6f 20 72  inting to into r
1f140 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2f 0a 63  egister P3..*/.c
1f141 61 73 65 20 4f 50 5f 56 43 6f 6c 75 6d 6e 3a 20  ase OP_VColumn: 
1f142 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61  {.#if 0  /* loca
1f143 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65  l variables move
1f144 64 20 69 6e 74 6f 20 75 2e 63 69 20 2a 2f 0a 20  d into u.ci */. 
1f145 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70   sqlite3_vtab *p
1f146 56 74 61 62 3b 0a 20 20 63 6f 6e 73 74 20 73 71  Vtab;.  const sq
1f147 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
1f148 6f 64 75 6c 65 3b 0a 20 20 4d 65 6d 20 2a 70 44  odule;.  Mem *pD
1f149 65 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 63  est;.  sqlite3_c
1f14a 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b  ontext sContext;
1f14b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c  .#endif /* local
1f14c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1f14d 20 69 6e 74 6f 20 75 2e 63 69 20 2a 2f 0a 0a 20   into u.ci */.. 
1f14e 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 75   VdbeCursor *pCu
1f14f 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f 70  r = p->apCsr[pOp
1f150 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p1];.  assert(
1f151 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73   pCur->pVtabCurs
1f152 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  or );.  assert( 
1f153 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70  pOp->p3>0 && pOp
1f154 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p3<=p->nMem );
1f155 0a 20 20 75 2e 63 69 2e 70 44 65 73 74 20 3d 20  .  u.ci.pDest = 
1f156 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33  &p->aMem[pOp->p3
1f157 5d 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 6e  ];.  if( pCur->n
1f158 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 73 71  ullRow ){.    sq
1f159 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e  lite3VdbeMemSetN
1f15a 75 6c 6c 28 75 2e 63 69 2e 70 44 65 73 74 29 3b  ull(u.ci.pDest);
1f15b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a  .    break;.  }.
1f15c 20 20 75 2e 63 69 2e 70 56 74 61 62 20 3d 20 70    u.ci.pVtab = p
1f15d 43 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72  Cur->pVtabCursor
1f15e 2d 3e 70 56 74 61 62 3b 0a 20 20 75 2e 63 69 2e  ->pVtab;.  u.ci.
1f15f 70 4d 6f 64 75 6c 65 20 3d 20 75 2e 63 69 2e 70  pModule = u.ci.p
1f160 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20  Vtab->pModule;. 
1f161 20 61 73 73 65 72 74 28 20 75 2e 63 69 2e 70 4d   assert( u.ci.pM
1f162 6f 64 75 6c 65 2d 3e 78 43 6f 6c 75 6d 6e 20 29  odule->xColumn )
1f163 3b 0a 20 20 6d 65 6d 73 65 74 28 26 75 2e 63 69  ;.  memset(&u.ci
1f164 2e 73 43 6f 6e 74 65 78 74 2c 20 30 2c 20 73 69  .sContext, 0, si
1f165 7a 65 6f 66 28 75 2e 63 69 2e 73 43 6f 6e 74 65  zeof(u.ci.sConte
1f166 78 74 29 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  xt));..  /* The 
1f167 6f 75 74 70 75 74 20 63 65 6c 6c 20 6d 61 79 20  output cell may 
1f168 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 20 62  already have a b
1f169 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 2e  uffer allocated.
1f16a 20 4d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 63   Move.  ** the c
1f16b 75 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20  urrent contents 
1f16c 74 6f 20 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74  to u.ci.sContext
1f16d 2e 73 20 73 6f 20 69 6e 20 63 61 73 65 20 74 68  .s so in case th
1f16e 65 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 0a  e user-function.
1f16f 20 20 2a 2a 20 63 61 6e 20 75 73 65 20 74 68 65    ** can use the
1f170 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74   already allocat
1f171 65 64 20 62 75 66 66 65 72 20 69 6e 73 74 65 61  ed buffer instea
1f172 64 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20  d of allocating 
1f173 61 0a 20 20 2a 2a 20 6e 65 77 20 6f 6e 65 2e 0a  a.  ** new one..
1f174 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
1f175 62 65 4d 65 6d 4d 6f 76 65 28 26 75 2e 63 69 2e  beMemMove(&u.ci.
1f176 73 43 6f 6e 74 65 78 74 2e 73 2c 20 75 2e 63 69  sContext.s, u.ci
1f177 2e 70 44 65 73 74 29 3b 0a 20 20 4d 65 6d 53 65  .pDest);.  MemSe
1f178 74 54 79 70 65 46 6c 61 67 28 26 75 2e 63 69 2e  tTypeFlag(&u.ci.
1f179 73 43 6f 6e 74 65 78 74 2e 73 2c 20 4d 45 4d 5f  sContext.s, MEM_
1f17a 4e 75 6c 6c 29 3b 0a 0a 20 20 69 66 28 20 73 71  Null);..  if( sq
1f17b 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
1f17c 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  b) ) goto abort_
1f17d 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20  due_to_misuse;. 
1f17e 20 72 63 20 3d 20 75 2e 63 69 2e 70 4d 6f 64 75   rc = u.ci.pModu
1f17f 6c 65 2d 3e 78 43 6f 6c 75 6d 6e 28 70 43 75 72  le->xColumn(pCur
1f180 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2c 20 26  ->pVtabCursor, &
1f181 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74 2c 20 70  u.ci.sContext, p
1f182 4f 70 2d 3e 70 32 29 3b 0a 20 20 73 71 6c 69 74  Op->p2);.  sqlit
1f183 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
1f184 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a  zErrMsg);.  p->z
1f185 45 72 72 4d 73 67 20 3d 20 75 2e 63 69 2e 70 56  ErrMsg = u.ci.pV
1f186 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20  tab->zErrMsg;.  
1f187 75 2e 63 69 2e 70 56 74 61 62 2d 3e 7a 45 72 72  u.ci.pVtab->zErr
1f188 4d 73 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 75  Msg = 0;.  if( u
1f189 2e 63 69 2e 73 43 6f 6e 74 65 78 74 2e 69 73 45  .ci.sContext.isE
1f18a 72 72 6f 72 20 29 7b 0a 20 20 20 20 72 63 20 3d  rror ){.    rc =
1f18b 20 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74 2e 69   u.ci.sContext.i
1f18c 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f  sError;.  }..  /
1f18d 2a 20 43 6f 70 79 20 74 68 65 20 72 65 73 75 6c  * Copy the resul
1f18e 74 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  t of the functio
1f18f 6e 20 74 6f 20 74 68 65 20 50 33 20 72 65 67 69  n to the P3 regi
1f190 73 74 65 72 2e 20 57 65 0a 20 20 2a 2a 20 64 6f  ster. We.  ** do
1f191 20 74 68 69 73 20 72 65 67 61 72 64 6c 65 73 73   this regardless
1f192 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e   of whether or n
1f193 6f 74 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  ot an error occu
1f194 72 72 65 64 20 74 6f 20 65 6e 73 75 72 65 20 61  rred to ensure a
1f195 6e 79 0a 20 20 2a 2a 20 64 79 6e 61 6d 69 63 20  ny.  ** dynamic 
1f196 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 75 2e  allocation in u.
1f197 63 69 2e 73 43 6f 6e 74 65 78 74 2e 73 20 28 61  ci.sContext.s (a
1f198 20 4d 65 6d 20 73 74 72 75 63 74 29 20 69 73 20   Mem struct) is 
1f199 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a   released..  */.
1f19a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1f19b 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 75 2e 63  ngeEncoding(&u.c
1f19c 69 2e 73 43 6f 6e 74 65 78 74 2e 73 2c 20 65 6e  i.sContext.s, en
1f19d 63 6f 64 69 6e 67 29 3b 0a 20 20 52 45 47 49 53  coding);.  REGIS
1f19e 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70  TER_TRACE(pOp->p
1f19f 33 2c 20 75 2e 63 69 2e 70 44 65 73 74 29 3b 0a  3, u.ci.pDest);.
1f1a0 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
1f1a1 4d 6f 76 65 28 75 2e 63 69 2e 70 44 65 73 74 2c  Move(u.ci.pDest,
1f1a2 20 26 75 2e 63 69 2e 73 43 6f 6e 74 65 78 74 2e   &u.ci.sContext.
1f1a3 73 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d 41 58  s);.  UPDATE_MAX
1f1a4 5f 42 4c 4f 42 53 49 5a 45 28 75 2e 63 69 2e 70  _BLOBSIZE(u.ci.p
1f1a5 44 65 73 74 29 3b 0a 0a 20 20 69 66 28 20 73 71  Dest);..  if( sq
1f1a6 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
1f1a7 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 61 62  ) ){.    goto ab
1f1a8 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
1f1a9 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c  e;.  }.  if( sql
1f1aa 69 74 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69  ite3VdbeMemTooBi
1f1ab 67 28 75 2e 63 69 2e 70 44 65 73 74 29 20 29 7b  g(u.ci.pDest) ){
1f1ac 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f 62 69  .    goto too_bi
1f1ad 67 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  g;.  }.  break;.
1f1ae 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
1f1af 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
1f1b0 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  ABLE */..#ifndef
1f1b1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
1f1b2 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63  TUALTABLE./* Opc
1f1b3 6f 64 65 3a 20 56 4e 65 78 74 20 50 31 20 50 32  ode: VNext P1 P2
1f1b4 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 76   * * *.**.** Adv
1f1b5 61 6e 63 65 20 76 69 72 74 75 61 6c 20 74 61 62  ance virtual tab
1f1b6 6c 65 20 50 31 20 74 6f 20 74 68 65 20 6e 65 78  le P1 to the nex
1f1b7 74 20 72 6f 77 20 69 6e 20 69 74 73 20 72 65 73  t row in its res
1f1b8 75 6c 74 20 73 65 74 20 61 6e 64 0a 2a 2a 20 6a  ult set and.** j
1f1b9 75 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74 69  ump to instructi
1f1ba 6f 6e 20 50 32 2e 20 20 4f 72 2c 20 69 66 20 74  on P2.  Or, if t
1f1bb 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
1f1bc 20 68 61 73 20 72 65 61 63 68 65 64 0a 2a 2a 20   has reached.** 
1f1bd 74 68 65 20 65 6e 64 20 6f 66 20 69 74 73 20 72  the end of its r
1f1be 65 73 75 6c 74 20 73 65 74 2c 20 74 68 65 6e 20  esult set, then 
1f1bf 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74 6f 20  fall through to 
1f1c0 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72 75 63  the next instruc
1f1c1 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  tion..*/.case OP
1f1c2 5f 56 4e 65 78 74 3a 20 7b 20 20 20 2f 2a 20 6a  _VNext: {   /* j
1f1c3 75 6d 70 20 2a 2f 0a 23 69 66 20 30 20 20 2f 2a  ump */.#if 0  /*
1f1c4 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1f1c5 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6a   moved into u.cj
1f1c6 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74   */.  sqlite3_vt
1f1c7 61 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f 6e  ab *pVtab;.  con
1f1c8 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
1f1c9 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 69 6e  e *pModule;.  in
1f1ca 74 20 72 65 73 3b 0a 20 20 56 64 62 65 43 75 72  t res;.  VdbeCur
1f1cb 73 6f 72 20 2a 70 43 75 72 3b 0a 23 65 6e 64 69  sor *pCur;.#endi
1f1cc 66 20 2f 2a 20 6c 6f 63 61 6c 20 76 61 72 69 61  f /* local varia
1f1cd 62 6c 65 73 20 6d 6f 76 65 64 20 69 6e 74 6f 20  bles moved into 
1f1ce 75 2e 63 6a 20 2a 2f 0a 0a 20 20 75 2e 63 6a 2e  u.cj */..  u.cj.
1f1cf 72 65 73 20 3d 20 30 3b 0a 20 20 75 2e 63 6a 2e  res = 0;.  u.cj.
1f1d0 70 43 75 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b  pCur = p->apCsr[
1f1d1 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65  pOp->p1];.  asse
1f1d2 72 74 28 20 75 2e 63 6a 2e 70 43 75 72 2d 3e 70  rt( u.cj.pCur->p
1f1d3 56 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20  VtabCursor );.  
1f1d4 69 66 28 20 75 2e 63 6a 2e 70 43 75 72 2d 3e 6e  if( u.cj.pCur->n
1f1d5 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 62 72  ullRow ){.    br
1f1d6 65 61 6b 3b 0a 20 20 7d 0a 20 20 75 2e 63 6a 2e  eak;.  }.  u.cj.
1f1d7 70 56 74 61 62 20 3d 20 75 2e 63 6a 2e 70 43 75  pVtab = u.cj.pCu
1f1d8 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 2d 3e  r->pVtabCursor->
1f1d9 70 56 74 61 62 3b 0a 20 20 75 2e 63 6a 2e 70 4d  pVtab;.  u.cj.pM
1f1da 6f 64 75 6c 65 20 3d 20 75 2e 63 6a 2e 70 56 74  odule = u.cj.pVt
1f1db 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61  ab->pModule;.  a
1f1dc 73 73 65 72 74 28 20 75 2e 63 6a 2e 70 4d 6f 64  ssert( u.cj.pMod
1f1dd 75 6c 65 2d 3e 78 4e 65 78 74 20 29 3b 0a 0a 20  ule->xNext );.. 
1f1de 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78   /* Invoke the x
1f1df 4e 65 78 74 28 29 20 6d 65 74 68 6f 64 20 6f 66  Next() method of
1f1e0 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 54 68 65   the module. The
1f1e1 72 65 20 69 73 20 6e 6f 20 77 61 79 20 66 6f 72  re is no way for
1f1e2 20 74 68 65 0a 20 20 2a 2a 20 75 6e 64 65 72 6c   the.  ** underl
1f1e3 79 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74  ying implementat
1f1e4 69 6f 6e 20 74 6f 20 72 65 74 75 72 6e 20 61 6e  ion to return an
1f1e5 20 65 72 72 6f 72 20 69 66 20 6f 6e 65 20 6f 63   error if one oc
1f1e6 63 75 72 73 20 64 75 72 69 6e 67 0a 20 20 2a 2a  curs during.  **
1f1e7 20 78 4e 65 78 74 28 29 2e 20 49 6e 73 74 65 61   xNext(). Instea
1f1e8 64 2c 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f  d, if an error o
1f1e9 63 63 75 72 73 2c 20 74 72 75 65 20 69 73 20 72  ccurs, true is r
1f1ea 65 74 75 72 6e 65 64 20 28 69 6e 64 69 63 61 74  eturned (indicat
1f1eb 69 6e 67 20 74 68 61 74 0a 20 20 2a 2a 20 64 61  ing that.  ** da
1f1ec 74 61 20 69 73 20 61 76 61 69 6c 61 62 6c 65 29  ta is available)
1f1ed 20 61 6e 64 20 74 68 65 20 65 72 72 6f 72 20 63   and the error c
1f1ee 6f 64 65 20 72 65 74 75 72 6e 65 64 20 77 68 65  ode returned whe
1f1ef 6e 20 78 43 6f 6c 75 6d 6e 20 6f 72 0a 20 20 2a  n xColumn or.  *
1f1f0 2a 20 73 6f 6d 65 20 6f 74 68 65 72 20 6d 65 74  * some other met
1f1f1 68 6f 64 20 69 73 20 6e 65 78 74 20 69 6e 76 6f  hod is next invo
1f1f2 6b 65 64 20 6f 6e 20 74 68 65 20 73 61 76 65 20  ked on the save 
1f1f3 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75  virtual table cu
1f1f4 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  rsor..  */.  if(
1f1f5 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
1f1f6 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  f(db) ) goto abo
1f1f7 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1f1f8 3b 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d 65 74  ;.  p->inVtabMet
1f1f9 68 6f 64 20 3d 20 31 3b 0a 20 20 72 63 20 3d 20  hod = 1;.  rc = 
1f1fa 75 2e 63 6a 2e 70 4d 6f 64 75 6c 65 2d 3e 78 4e  u.cj.pModule->xN
1f1fb 65 78 74 28 75 2e 63 6a 2e 70 43 75 72 2d 3e 70  ext(u.cj.pCur->p
1f1fc 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 70  VtabCursor);.  p
1f1fd 2d 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d  ->inVtabMethod =
1f1fe 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46   0;.  sqlite3DbF
1f1ff 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d  ree(db, p->zErrM
1f200 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73  sg);.  p->zErrMs
1f201 67 20 3d 20 75 2e 63 6a 2e 70 56 74 61 62 2d 3e  g = u.cj.pVtab->
1f202 7a 45 72 72 4d 73 67 3b 0a 20 20 75 2e 63 6a 2e  zErrMsg;.  u.cj.
1f203 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d  pVtab->zErrMsg =
1f204 20 30 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   0;.  if( rc==SQ
1f205 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75  LITE_OK ){.    u
1f206 2e 63 6a 2e 72 65 73 20 3d 20 75 2e 63 6a 2e 70  .cj.res = u.cj.p
1f207 4d 6f 64 75 6c 65 2d 3e 78 45 6f 66 28 75 2e 63  Module->xEof(u.c
1f208 6a 2e 70 43 75 72 2d 3e 70 56 74 61 62 43 75 72  j.pCur->pVtabCur
1f209 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  sor);.  }.  if( 
1f20a 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
1f20b 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  db) ) goto abort
1f20c 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
1f20d 0a 20 20 69 66 28 20 21 75 2e 63 6a 2e 72 65 73  .  if( !u.cj.res
1f20e 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   ){.    /* If th
1f20f 65 72 65 20 69 73 20 64 61 74 61 2c 20 6a 75 6d  ere is data, jum
1f210 70 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20 20 70  p to P2 */.    p
1f211 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1f212 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1f213 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1f214 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1f215 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  LE */..#ifndef S
1f216 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1f217 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64  ALTABLE./* Opcod
1f218 65 3a 20 56 52 65 6e 61 6d 65 20 50 31 20 2a 20  e: VRename P1 * 
1f219 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20  * P4 *.**.** P4 
1f21a 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
1f21b 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
1f21c 6f 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69 74  object, an sqlit
1f21d 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72  e3_vtab structur
1f21e 65 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64  e..** This opcod
1f21f 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f  e invokes the co
1f220 72 72 65 73 70 6f 6e 64 69 6e 67 20 78 52 65 6e  rresponding xRen
1f221 61 6d 65 20 6d 65 74 68 6f 64 2e 20 54 68 65 20  ame method. The 
1f222 76 61 6c 75 65 0a 2a 2a 20 69 6e 20 72 65 67 69  value.** in regi
1f223 73 74 65 72 20 50 31 20 69 73 20 70 61 73 73 65  ster P1 is passe
1f224 64 20 61 73 20 74 68 65 20 7a 4e 61 6d 65 20 61  d as the zName a
1f225 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 78  rgument to the x
1f226 52 65 6e 61 6d 65 20 6d 65 74 68 6f 64 2e 0a 2a  Rename method..*
1f227 2f 0a 63 61 73 65 20 4f 50 5f 56 52 65 6e 61 6d  /.case OP_VRenam
1f228 65 3a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c  e: {.#if 0  /* l
1f229 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1f22a 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6b 20 2a  oved into u.ck *
1f22b 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  /.  sqlite3_vtab
1f22c 20 2a 70 56 74 61 62 3b 0a 20 20 4d 65 6d 20 2a   *pVtab;.  Mem *
1f22d 70 4e 61 6d 65 3b 0a 23 65 6e 64 69 66 20 2f 2a  pName;.#endif /*
1f22e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
1f22f 20 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6b   moved into u.ck
1f230 20 2a 2f 0a 0a 20 20 75 2e 63 6b 2e 70 56 74 61   */..  u.ck.pVta
1f231 62 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61  b = pOp->p4.pVta
1f232 62 2d 3e 70 56 74 61 62 3b 0a 20 20 75 2e 63 6b  b->pVtab;.  u.ck
1f233 2e 70 4e 61 6d 65 20 3d 20 26 70 2d 3e 61 4d 65  .pName = &p->aMe
1f234 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73  m[pOp->p1];.  as
1f235 73 65 72 74 28 20 75 2e 63 6b 2e 70 56 74 61 62  sert( u.ck.pVtab
1f236 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61  ->pModule->xRena
1f237 6d 65 20 29 3b 0a 20 20 52 45 47 49 53 54 45 52  me );.  REGISTER
1f238 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20  _TRACE(pOp->p1, 
1f239 75 2e 63 6b 2e 70 4e 61 6d 65 29 3b 0a 20 20 61  u.ck.pName);.  a
1f23a 73 73 65 72 74 28 20 75 2e 63 6b 2e 70 4e 61 6d  ssert( u.ck.pNam
1f23b 65 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 53  e->flags & MEM_S
1f23c 74 72 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  tr );.  if( sqli
1f23d 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
1f23e 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1f23f 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 72  e_to_misuse;.  r
1f240 63 20 3d 20 75 2e 63 6b 2e 70 56 74 61 62 2d 3e  c = u.ck.pVtab->
1f241 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65  pModule->xRename
1f242 28 75 2e 63 6b 2e 70 56 74 61 62 2c 20 75 2e 63  (u.ck.pVtab, u.c
1f243 6b 2e 70 4e 61 6d 65 2d 3e 7a 29 3b 0a 20 20 73  k.pName->z);.  s
1f244 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1f245 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20   p->zErrMsg);.  
1f246 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63  p->zErrMsg = u.c
1f247 6b 2e 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  k.pVtab->zErrMsg
1f248 3b 0a 20 20 75 2e 63 6b 2e 70 56 74 61 62 2d 3e  ;.  u.ck.pVtab->
1f249 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69  zErrMsg = 0;.  i
1f24a 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1f24b 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  On(db) ) goto ab
1f24c 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
1f24d 65 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23  e;..  break;.}.#
1f24e 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
1f24f 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1f250 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64  ALTABLE./* Opcod
1f251 65 3a 20 56 55 70 64 61 74 65 20 50 31 20 50 32  e: VUpdate P1 P2
1f252 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50   P3 P4 *.**.** P
1f253 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  4 is a pointer t
1f254 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  o a virtual tabl
1f255 65 20 6f 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c  e object, an sql
1f256 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74  ite3_vtab struct
1f257 75 72 65 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 63  ure..** This opc
1f258 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20  ode invokes the 
1f259 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 78 55  corresponding xU
1f25a 70 64 61 74 65 20 6d 65 74 68 6f 64 2e 20 50 32  pdate method. P2
1f25b 20 76 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 63   values.** are c
1f25c 6f 6e 74 69 67 75 6f 75 73 20 6d 65 6d 6f 72 79  ontiguous memory
1f25d 20 63 65 6c 6c 73 20 73 74 61 72 74 69 6e 67 20   cells starting 
1f25e 61 74 20 50 33 20 74 6f 20 70 61 73 73 20 74 6f  at P3 to pass to
1f25f 20 74 68 65 20 78 55 70 64 61 74 65 20 0a 2a 2a   the xUpdate .**
1f260 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 20 54 68 65   invocation. The
1f261 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
1f262 65 72 20 28 50 33 2b 50 32 2d 31 29 20 63 6f 72  er (P3+P2-1) cor
1f263 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20  responds to the 
1f264 0a 2a 2a 20 70 32 74 68 20 65 6c 65 6d 65 6e 74  .** p2th element
1f265 20 6f 66 20 74 68 65 20 61 72 67 76 20 61 72 72   of the argv arr
1f266 61 79 20 70 61 73 73 65 64 20 74 6f 20 78 55 70  ay passed to xUp
1f267 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  date..**.** The 
1f268 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64 20 77  xUpdate method w
1f269 69 6c 6c 20 64 6f 20 61 20 44 45 4c 45 54 45 20  ill do a DELETE 
1f26a 6f 72 20 61 6e 20 49 4e 53 45 52 54 20 6f 72 20  or an INSERT or 
1f26b 62 6f 74 68 2e 0a 2a 2a 20 54 68 65 20 61 72 67  both..** The arg
1f26c 76 5b 30 5d 20 65 6c 65 6d 65 6e 74 20 28 77 68  v[0] element (wh
1f26d 69 63 68 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  ich corresponds 
1f26e 74 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50  to memory cell P
1f26f 33 29 0a 2a 2a 20 69 73 20 74 68 65 20 72 6f 77  3).** is the row
1f270 69 64 20 6f 66 20 61 20 72 6f 77 20 74 6f 20 64  id of a row to d
1f271 65 6c 65 74 65 2e 20 20 49 66 20 61 72 67 76 5b  elete.  If argv[
1f272 30 5d 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20  0] is NULL then 
1f273 6e 6f 20 0a 2a 2a 20 64 65 6c 65 74 69 6f 6e 20  no .** deletion 
1f274 6f 63 63 75 72 73 2e 20 20 54 68 65 20 61 72 67  occurs.  The arg
1f275 76 5b 31 5d 20 65 6c 65 6d 65 6e 74 20 69 73 20  v[1] element is 
1f276 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65  the rowid of the
1f277 20 6e 65 77 20 0a 2a 2a 20 72 6f 77 2e 20 20 54   new .** row.  T
1f278 68 69 73 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20  his can be NULL 
1f279 74 6f 20 68 61 76 65 20 74 68 65 20 76 69 72 74  to have the virt
1f27a 75 61 6c 20 74 61 62 6c 65 20 73 65 6c 65 63 74  ual table select
1f27b 20 74 68 65 20 6e 65 77 20 0a 2a 2a 20 72 6f 77   the new .** row
1f27c 69 64 20 66 6f 72 20 69 74 73 65 6c 66 2e 20 20  id for itself.  
1f27d 54 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 65  The subsequent e
1f27e 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 61  lements in the a
1f27f 72 72 61 79 20 61 72 65 20 0a 2a 2a 20 74 68 65  rray are .** the
1f280 20 76 61 6c 75 65 73 20 6f 66 20 63 6f 6c 75 6d   values of colum
1f281 6e 73 20 69 6e 20 74 68 65 20 6e 65 77 20 72 6f  ns in the new ro
1f282 77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 3d 3d  w..**.** If P2==
1f283 31 20 74 68 65 6e 20 6e 6f 20 69 6e 73 65 72 74  1 then no insert
1f284 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 20   is performed.  
1f285 61 72 67 76 5b 30 5d 20 69 73 20 74 68 65 20 72  argv[0] is the r
1f286 6f 77 69 64 20 6f 66 0a 2a 2a 20 61 20 72 6f 77  owid of.** a row
1f287 20 74 6f 20 64 65 6c 65 74 65 2e 0a 2a 2a 0a 2a   to delete..**.*
1f288 2a 20 50 31 20 69 73 20 61 20 62 6f 6f 6c 65 61  * P1 is a boolea
1f289 6e 20 66 6c 61 67 2e 20 49 66 20 69 74 20 69 73  n flag. If it is
1f28a 20 73 65 74 20 74 6f 20 74 72 75 65 20 61 6e 64   set to true and
1f28b 20 74 68 65 20 78 55 70 64 61 74 65 20 63 61 6c   the xUpdate cal
1f28c 6c 0a 2a 2a 20 69 73 20 73 75 63 63 65 73 73 66  l.** is successf
1f28d 75 6c 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c  ul, then the val
1f28e 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73  ue returned by s
1f28f 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
1f290 72 74 5f 72 6f 77 69 64 28 29 20 0a 2a 2a 20 69  rt_rowid() .** i
1f291 73 20 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c  s set to the val
1f292 75 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20  ue of the rowid 
1f293 66 6f 72 20 74 68 65 20 72 6f 77 20 6a 75 73 74  for the row just
1f294 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 63 61   inserted..*/.ca
1f295 73 65 20 4f 50 5f 56 55 70 64 61 74 65 3a 20 7b  se OP_VUpdate: {
1f296 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1f297 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1f298 20 69 6e 74 6f 20 75 2e 63 6c 20 2a 2f 0a 20 20   into u.cl */.  
1f299 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
1f29a 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  tab;.  sqlite3_m
1f29b 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a  odule *pModule;.
1f29c 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 69 6e    int nArg;.  in
1f29d 74 20 69 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e  t i;.  sqlite_in
1f29e 74 36 34 20 72 6f 77 69 64 3b 0a 20 20 4d 65 6d  t64 rowid;.  Mem
1f29f 20 2a 2a 61 70 41 72 67 3b 0a 20 20 4d 65 6d 20   **apArg;.  Mem 
1f2a0 2a 70 58 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 6c  *pX;.#endif /* l
1f2a1 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d  ocal variables m
1f2a2 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6c 20 2a  oved into u.cl *
1f2a3 2f 0a 0a 20 20 75 2e 63 6c 2e 70 56 74 61 62 20  /..  u.cl.pVtab 
1f2a4 3d 20 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 2d  = pOp->p4.pVtab-
1f2a5 3e 70 56 74 61 62 3b 0a 20 20 75 2e 63 6c 2e 70  >pVtab;.  u.cl.p
1f2a6 4d 6f 64 75 6c 65 20 3d 20 28 73 71 6c 69 74 65  Module = (sqlite
1f2a7 33 5f 6d 6f 64 75 6c 65 20 2a 29 75 2e 63 6c 2e  3_module *)u.cl.
1f2a8 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a  pVtab->pModule;.
1f2a9 20 20 75 2e 63 6c 2e 6e 41 72 67 20 3d 20 70 4f    u.cl.nArg = pO
1f2aa 70 2d 3e 70 32 3b 0a 20 20 61 73 73 65 72 74 28  p->p2;.  assert(
1f2ab 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34   pOp->p4type==P4
1f2ac 5f 56 54 41 42 20 29 3b 0a 20 20 69 66 28 20 41  _VTAB );.  if( A
1f2ad 4c 57 41 59 53 28 75 2e 63 6c 2e 70 4d 6f 64 75  LWAYS(u.cl.pModu
1f2ae 6c 65 2d 3e 78 55 70 64 61 74 65 29 20 29 7b 0a  le->xUpdate) ){.
1f2af 20 20 20 20 75 2e 63 6c 2e 61 70 41 72 67 20 3d      u.cl.apArg =
1f2b0 20 70 2d 3e 61 70 41 72 67 3b 0a 20 20 20 20 75   p->apArg;.    u
1f2b1 2e 63 6c 2e 70 58 20 3d 20 26 70 2d 3e 61 4d 65  .cl.pX = &p->aMe
1f2b2 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20  m[pOp->p3];.    
1f2b3 66 6f 72 28 75 2e 63 6c 2e 69 3d 30 3b 20 75 2e  for(u.cl.i=0; u.
1f2b4 63 6c 2e 69 3c 75 2e 63 6c 2e 6e 41 72 67 3b 20  cl.i<u.cl.nArg; 
1f2b5 75 2e 63 6c 2e 69 2b 2b 29 7b 0a 20 20 20 20 20  u.cl.i++){.     
1f2b6 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
1f2b7 74 6f 72 65 54 79 70 65 28 75 2e 63 6c 2e 70 58  toreType(u.cl.pX
1f2b8 29 3b 0a 20 20 20 20 20 20 75 2e 63 6c 2e 61 70  );.      u.cl.ap
1f2b9 41 72 67 5b 75 2e 63 6c 2e 69 5d 20 3d 20 75 2e  Arg[u.cl.i] = u.
1f2ba 63 6c 2e 70 58 3b 0a 20 20 20 20 20 20 75 2e 63  cl.pX;.      u.c
1f2bb 6c 2e 70 58 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  l.pX++;.    }.  
1f2bc 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66    if( sqlite3Saf
1f2bd 65 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74  etyOff(db) ) got
1f2be 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d  o abort_due_to_m
1f2bf 69 73 75 73 65 3b 0a 20 20 20 20 72 63 20 3d 20  isuse;.    rc = 
1f2c0 75 2e 63 6c 2e 70 4d 6f 64 75 6c 65 2d 3e 78 55  u.cl.pModule->xU
1f2c1 70 64 61 74 65 28 75 2e 63 6c 2e 70 56 74 61 62  pdate(u.cl.pVtab
1f2c2 2c 20 75 2e 63 6c 2e 6e 41 72 67 2c 20 75 2e 63  , u.cl.nArg, u.c
1f2c3 6c 2e 61 70 41 72 67 2c 20 26 75 2e 63 6c 2e 72  l.apArg, &u.cl.r
1f2c4 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  owid);.    sqlit
1f2c5 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e  e3DbFree(db, p->
1f2c6 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d  zErrMsg);.    p-
1f2c7 3e 7a 45 72 72 4d 73 67 20 3d 20 75 2e 63 6c 2e  >zErrMsg = u.cl.
1f2c8 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a  pVtab->zErrMsg;.
1f2c9 20 20 20 20 75 2e 63 6c 2e 70 56 74 61 62 2d 3e      u.cl.pVtab->
1f2ca 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20  zErrMsg = 0;.   
1f2cb 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1f2cc 74 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20  tyOn(db) ) goto 
1f2cd 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73  abort_due_to_mis
1f2ce 75 73 65 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  use;.    if( rc=
1f2cf 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 4f  =SQLITE_OK && pO
1f2d0 70 2d 3e 70 31 20 29 7b 0a 20 20 20 20 20 20 61  p->p1 ){.      a
1f2d1 73 73 65 72 74 28 20 75 2e 63 6c 2e 6e 41 72 67  ssert( u.cl.nArg
1f2d2 3e 31 20 26 26 20 75 2e 63 6c 2e 61 70 41 72 67  >1 && u.cl.apArg
1f2d3 5b 30 5d 20 26 26 20 28 75 2e 63 6c 2e 61 70 41  [0] && (u.cl.apA
1f2d4 72 67 5b 30 5d 2d 3e 66 6c 61 67 73 26 4d 45 4d  rg[0]->flags&MEM
1f2d5 5f 4e 75 6c 6c 29 20 29 3b 0a 20 20 20 20 20 20  _Null) );.      
1f2d6 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20  db->lastRowid = 
1f2d7 75 2e 63 6c 2e 72 6f 77 69 64 3b 0a 20 20 20 20  u.cl.rowid;.    
1f2d8 7d 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65  }.    p->nChange
1f2d9 2b 2b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  ++;.  }.  break;
1f2da 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1f2db 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1f2dc 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65  TABLE */..#ifnde
1f2dd 66 20 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  f  SQLITE_OMIT_P
1f2de 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a 20  AGER_PRAGMAS./* 
1f2df 4f 70 63 6f 64 65 3a 20 50 61 67 65 63 6f 75 6e  Opcode: Pagecoun
1f2e0 74 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  t P1 P2 * * *.**
1f2e1 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 63 75  .** Write the cu
1f2e2 72 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20  rrent number of 
1f2e3 70 61 67 65 73 20 69 6e 20 64 61 74 61 62 61 73  pages in databas
1f2e4 65 20 50 31 20 74 6f 20 6d 65 6d 6f 72 79 20 63  e P1 to memory c
1f2e5 65 6c 6c 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20  ell P2..*/.case 
1f2e6 4f 50 5f 50 61 67 65 63 6f 75 6e 74 3a 20 7b 20  OP_Pagecount: { 
1f2e7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 75             /* ou
1f2e8 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f  t2-prerelease */
1f2e9 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63 61 6c  .#if 0  /* local
1f2ea 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64   variables moved
1f2eb 20 69 6e 74 6f 20 75 2e 63 6d 20 2a 2f 0a 20 20   into u.cm */.  
1f2ec 69 6e 74 20 70 31 3b 0a 20 20 69 6e 74 20 6e 50  int p1;.  int nP
1f2ed 61 67 65 3b 0a 20 20 50 61 67 65 72 20 2a 70 50  age;.  Pager *pP
1f2ee 61 67 65 72 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  ager;.#endif /* 
1f2ef 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73 20  local variables 
1f2f0 6d 6f 76 65 64 20 69 6e 74 6f 20 75 2e 63 6d 20  moved into u.cm 
1f2f1 2a 2f 0a 0a 20 20 75 2e 63 6d 2e 70 31 20 3d 20  */..  u.cm.p1 = 
1f2f2 70 4f 70 2d 3e 70 31 3b 0a 20 20 75 2e 63 6d 2e  pOp->p1;.  u.cm.
1f2f3 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33  pPager = sqlite3
1f2f4 42 74 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61  BtreePager(db->a
1f2f5 44 62 5b 75 2e 63 6d 2e 70 31 5d 2e 70 42 74 29  Db[u.cm.p1].pBt)
1f2f6 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
1f2f7 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 75  PagerPagecount(u
1f2f8 2e 63 6d 2e 70 50 61 67 65 72 2c 20 26 75 2e 63  .cm.pPager, &u.c
1f2f9 6d 2e 6e 50 61 67 65 29 3b 0a 20 20 2f 2a 20 4f  m.nPage);.  /* O
1f2fa 50 5f 50 61 67 65 63 6f 75 6e 74 20 69 73 20 61  P_Pagecount is a
1f2fb 6c 77 61 79 73 20 63 61 6c 6c 65 64 20 66 72 6f  lways called fro
1f2fc 6d 20 77 69 74 68 69 6e 20 61 20 72 65 61 64 20  m within a read 
1f2fd 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 54 68  transaction.  Th
1f2fe 65 0a 20 20 2a 2a 20 70 61 67 65 20 63 6f 75 6e  e.  ** page coun
1f2ff 74 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  t has already be
1f300 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20  en successfully 
1f301 72 65 61 64 20 61 6e 64 20 63 61 63 68 65 64 2e  read and cached.
1f302 20 20 53 6f 20 74 68 65 0a 20 20 2a 2a 20 73 71    So the.  ** sq
1f303 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f  lite3PagerPageco
1f304 75 6e 74 28 29 20 63 61 6c 6c 20 61 62 6f 76 65  unt() call above
1f305 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 2a 2f   cannot fail. */
1f306 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 72 63  .  if( ALWAYS(rc
1f307 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 7b 0a  ==SQLITE_OK) ){.
1f308 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20      pOut->flags 
1f309 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 70  = MEM_Int;.    p
1f30a 4f 75 74 2d 3e 75 2e 69 20 3d 20 75 2e 63 6d 2e  Out->u.i = u.cm.
1f30b 6e 50 61 67 65 3b 0a 20 20 7d 0a 20 20 62 72 65  nPage;.  }.  bre
1f30c 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  ak;.}.#endif..#i
1f30d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1f30e 54 5f 54 52 41 43 45 0a 2f 2a 20 4f 70 63 6f 64  T_TRACE./* Opcod
1f30f 65 3a 20 54 72 61 63 65 20 2a 20 2a 20 2a 20 50  e: Trace * * * P
1f310 34 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 72 61  4 *.**.** If tra
1f311 63 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64 20  cing is enabled 
1f312 28 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f  (by the sqlite3_
1f313 74 72 61 63 65 28 29 29 20 69 6e 74 65 72 66 61  trace()) interfa
1f314 63 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20  ce, then.** the 
1f315 55 54 46 2d 38 20 73 74 72 69 6e 67 20 63 6f 6e  UTF-8 string con
1f316 74 61 69 6e 65 64 20 69 6e 20 50 34 20 69 73 20  tained in P4 is 
1f317 65 6d 69 74 74 65 64 20 6f 6e 20 74 68 65 20 74  emitted on the t
1f318 72 61 63 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a  race callback..*
1f319 2f 0a 63 61 73 65 20 4f 50 5f 54 72 61 63 65 3a  /.case OP_Trace:
1f31a 20 7b 0a 23 69 66 20 30 20 20 2f 2a 20 6c 6f 63   {.#if 0  /* loc
1f31b 61 6c 20 76 61 72 69 61 62 6c 65 73 20 6d 6f 76  al variables mov
1f31c 65 64 20 69 6e 74 6f 20 75 2e 63 6e 20 2a 2f 0a  ed into u.cn */.
1f31d 20 20 63 68 61 72 20 2a 7a 54 72 61 63 65 3b 0a    char *zTrace;.
1f31e 23 65 6e 64 69 66 20 2f 2a 20 6c 6f 63 61 6c 20  #endif /* local 
1f31f 76 61 72 69 61 62 6c 65 73 20 6d 6f 76 65 64 20  variables moved 
1f320 69 6e 74 6f 20 75 2e 63 6e 20 2a 2f 0a 0a 20 20  into u.cn */..  
1f321 75 2e 63 6e 2e 7a 54 72 61 63 65 20 3d 20 28 70  u.cn.zTrace = (p
1f322 4f 70 2d 3e 70 34 2e 7a 20 3f 20 70 4f 70 2d 3e  Op->p4.z ? pOp->
1f323 70 34 2e 7a 20 3a 20 70 2d 3e 7a 53 71 6c 29 3b  p4.z : p->zSql);
1f324 0a 20 20 69 66 28 20 75 2e 63 6e 2e 7a 54 72 61  .  if( u.cn.zTra
1f325 63 65 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62  ce ){.    if( db
1f326 2d 3e 78 54 72 61 63 65 20 29 7b 0a 20 20 20 20  ->xTrace ){.    
1f327 20 20 64 62 2d 3e 78 54 72 61 63 65 28 64 62 2d    db->xTrace(db-
1f328 3e 70 54 72 61 63 65 41 72 67 2c 20 75 2e 63 6e  >pTraceArg, u.cn
1f329 2e 7a 54 72 61 63 65 29 3b 0a 20 20 20 20 7d 0a  .zTrace);.    }.
1f32a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
1f32b 42 55 47 0a 20 20 20 20 69 66 28 20 28 64 62 2d  BUG.    if( (db-
1f32c 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
1f32d 53 71 6c 54 72 61 63 65 29 21 3d 30 20 29 7b 0a  SqlTrace)!=0 ){.
1f32e 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62        sqlite3Deb
1f32f 75 67 50 72 69 6e 74 66 28 22 53 51 4c 2d 74 72  ugPrintf("SQL-tr
1f330 61 63 65 3a 20 25 73 5c 6e 22 2c 20 75 2e 63 6e  ace: %s\n", u.cn
1f331 2e 7a 54 72 61 63 65 29 3b 0a 20 20 20 20 7d 0a  .zTrace);.    }.
1f332 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1f333 5f 44 45 42 55 47 20 2a 2f 0a 20 20 7d 0a 20 20  _DEBUG */.  }.  
1f334 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 0a  break;.}.#endif.
1f335 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 6f  ../* Opcode: Noo
1f336 70 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a  p * * * * *.**.*
1f337 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 54  * Do nothing.  T
1f338 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1f339 69 73 20 6f 66 74 65 6e 20 75 73 65 66 75 6c 20  is often useful 
1f33a 61 73 20 61 20 6a 75 6d 70 0a 2a 2a 20 64 65 73  as a jump.** des
1f33b 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 0a  tination..*/./*.
1f33c 2a 2a 20 54 68 65 20 6d 61 67 69 63 20 45 78 70  ** The magic Exp
1f33d 6c 61 69 6e 20 6f 70 63 6f 64 65 20 61 72 65 20  lain opcode are 
1f33e 6f 6e 6c 79 20 69 6e 73 65 72 74 65 64 20 77 68  only inserted wh
1f33f 65 6e 20 65 78 70 6c 61 69 6e 3d 3d 32 20 28 77  en explain==2 (w
1f340 68 69 63 68 0a 2a 2a 20 69 73 20 74 6f 20 73 61  hich.** is to sa
1f341 79 20 77 68 65 6e 20 74 68 65 20 45 58 50 4c 41  y when the EXPLA
1f342 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 20 73 79  IN QUERY PLAN sy
1f343 6e 74 61 78 20 69 73 20 75 73 65 64 2e 29 0a 2a  ntax is used.).*
1f344 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 72 65  * This opcode re
1f345 63 6f 72 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f  cords informatio
1f346 6e 20 66 72 6f 6d 20 74 68 65 20 6f 70 74 69 6d  n from the optim
1f347 69 7a 65 72 2e 20 20 49 74 20 69 73 20 74 68 65  izer.  It is the
1f348 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73 20  .** the same as 
1f349 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 69 73 20 6f  a no-op.  This o
1f34a 70 63 6f 64 65 73 6e 65 76 65 72 20 61 70 70 65  pcodesnever appe
1f34b 61 72 73 20 69 6e 20 61 20 72 65 61 6c 20 56 4d  ars in a real VM
1f34c 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 64 65 66   program..*/.def
1f34d 61 75 6c 74 3a 20 7b 20 20 20 20 20 20 20 20 20  ault: {         
1f34e 20 2f 2a 20 54 68 69 73 20 69 73 20 72 65 61 6c   /* This is real
1f34f 6c 79 20 4f 50 5f 4e 6f 6f 70 20 61 6e 64 20 4f  ly OP_Noop and O
1f350 50 5f 45 78 70 6c 61 69 6e 20 2a 2f 0a 20 20 62  P_Explain */.  b
1f351 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  reak;.}../******
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 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f355 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f356 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 63  *******.** The c
1f357 61 73 65 73 20 6f 66 20 74 68 65 20 73 77 69 74  ases of the swit
1f358 63 68 20 73 74 61 74 65 6d 65 6e 74 20 61 62 6f  ch statement abo
1f359 76 65 20 74 68 69 73 20 6c 69 6e 65 20 73 68 6f  ve this line sho
1f35a 75 6c 64 20 61 6c 6c 20 62 65 20 69 6e 64 65 6e  uld all be inden
1f35b 74 65 64 0a 2a 2a 20 62 79 20 36 20 73 70 61 63  ted.** by 6 spac
1f35c 65 73 2e 20 20 42 75 74 20 74 68 65 20 6c 65 66  es.  But the lef
1f35d 74 2d 6d 6f 73 74 20 36 20 73 70 61 63 65 73 20  t-most 6 spaces 
1f35e 68 61 76 65 20 62 65 65 6e 20 72 65 6d 6f 76 65  have been remove
1f35f 64 20 74 6f 20 69 6d 70 72 6f 76 65 20 74 68 65  d to improve the
1f360 0a 2a 2a 20 72 65 61 64 61 62 69 6c 69 74 79 2e  .** readability.
1f361 20 20 46 72 6f 6d 20 74 68 69 73 20 70 6f 69 6e    From this poin
1f362 74 20 6f 6e 20 64 6f 77 6e 2c 20 74 68 65 20 6e  t on down, the n
1f363 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 74 69 6f  ormal indentatio
1f364 6e 20 72 75 6c 65 73 20 61 72 65 0a 2a 2a 20 72  n rules are.** r
1f365 65 73 74 6f 72 65 64 2e 0a 2a 2a 2a 2a 2a 2a 2a  estored..*******
1f366 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 2f 0a 20 20 20 20 7d 0a 0a 23  ******/.    }..#
1f36b 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46 49  ifdef VDBE_PROFI
1f36c 4c 45 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 75  LE.    {.      u
1f36d 36 34 20 65 6c 61 70 73 65 64 20 3d 20 73 71 6c  64 elapsed = sql
1f36e 69 74 65 33 48 77 74 69 6d 65 28 29 20 2d 20 73  ite3Hwtime() - s
1f36f 74 61 72 74 3b 0a 20 20 20 20 20 20 70 4f 70 2d  tart;.      pOp-
1f370 3e 63 79 63 6c 65 73 20 2b 3d 20 65 6c 61 70 73  >cycles += elaps
1f371 65 64 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e 63  ed;.      pOp->c
1f372 6e 74 2b 2b 3b 0a 23 69 66 20 30 0a 20 20 20 20  nt++;.#if 0.    
1f373 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64 6f      fprintf(stdo
1f374 75 74 2c 20 22 25 31 30 6c 6c 75 20 22 2c 20 65  ut, "%10llu ", e
1f375 6c 61 70 73 65 64 29 3b 0a 20 20 20 20 20 20 20  lapsed);.       
1f376 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e   sqlite3VdbePrin
1f377 74 4f 70 28 73 74 64 6f 75 74 2c 20 6f 72 69 67  tOp(stdout, orig
1f378 50 63 2c 20 26 70 2d 3e 61 4f 70 5b 6f 72 69 67  Pc, &p->aOp[orig
1f379 50 63 5d 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  Pc]);.#endif.   
1f37a 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f   }.#endif..    /
1f37b 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
1f37c 63 6f 64 65 20 61 64 64 73 20 6e 6f 74 68 69 6e  code adds nothin
1f37d 67 20 74 6f 20 74 68 65 20 61 63 74 75 61 6c 20  g to the actual 
1f37e 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 0a 20 20  functionality.  
1f37f 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 72 6f 67    ** of the prog
1f380 72 61 6d 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79  ram.  It is only
1f381 20 68 65 72 65 20 66 6f 72 20 74 65 73 74 69 6e   here for testin
1f382 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67 2e  g and debugging.
1f383 0a 20 20 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f  .    ** On the o
1f384 74 68 65 72 20 68 61 6e 64 2c 20 69 74 20 64 6f  ther hand, it do
1f385 65 73 20 62 75 72 6e 20 43 50 55 20 63 79 63 6c  es burn CPU cycl
1f386 65 73 20 65 76 65 72 79 20 74 69 6d 65 20 74 68  es every time th
1f387 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20 74 68 65  rough.    ** the
1f388 20 65 76 61 6c 75 61 74 6f 72 20 6c 6f 6f 70 2e   evaluator loop.
1f389 20 20 53 6f 20 77 65 20 63 61 6e 20 6c 65 61 76    So we can leav
1f38a 65 20 69 74 20 6f 75 74 20 77 68 65 6e 20 4e 44  e it out when ND
1f38b 45 42 55 47 20 69 73 20 64 65 66 69 6e 65 64 2e  EBUG is defined.
1f38c 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20  .    */.#ifndef 
1f38d 4e 44 45 42 55 47 0a 20 20 20 20 61 73 73 65 72  NDEBUG.    asser
1f38e 74 28 20 70 63 3e 3d 2d 31 20 26 26 20 70 63 3c  t( pc>=-1 && pc<
1f38f 70 2d 3e 6e 4f 70 20 29 3b 0a 0a 23 69 66 64 65  p->nOp );..#ifde
1f390 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 20  f SQLITE_DEBUG. 
1f391 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20     if( p->trace 
1f392 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  ){.      if( rc!
1f393 3d 30 20 29 20 66 70 72 69 6e 74 66 28 70 2d 3e  =0 ) fprintf(p->
1f394 74 72 61 63 65 2c 22 72 63 3d 25 64 5c 6e 22 2c  trace,"rc=%d\n",
1f395 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6f  rc);.      if( o
1f396 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c  pProperty & OPFL
1f397 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53  G_OUT2_PRERELEAS
1f398 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 67  E ){.        reg
1f399 69 73 74 65 72 54 72 61 63 65 28 70 2d 3e 74 72  isterTrace(p->tr
1f39a 61 63 65 2c 20 70 4f 70 2d 3e 70 32 2c 20 70 4f  ace, pOp->p2, pO
1f39b 75 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ut);.      }.   
1f39c 20 20 20 69 66 28 20 6f 70 50 72 6f 70 65 72 74     if( opPropert
1f39d 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 20 29  y & OPFLG_OUT3 )
1f39e 7b 0a 20 20 20 20 20 20 20 20 72 65 67 69 73 74  {.        regist
1f39f 65 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63 65  erTrace(p->trace
1f3a0 2c 20 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74 29  , pOp->p3, pOut)
1f3a1 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1f3a2 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54  #endif  /* SQLIT
1f3a3 45 5f 44 45 42 55 47 20 2a 2f 0a 23 65 6e 64 69  E_DEBUG */.#endi
1f3a4 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a  f  /* NDEBUG */.
1f3a5 20 20 7d 20 20 2f 2a 20 54 68 65 20 65 6e 64 20    }  /* The end 
1f3a6 6f 66 20 74 68 65 20 66 6f 72 28 3b 3b 29 20 6c  of the for(;;) l
1f3a7 6f 6f 70 20 74 68 65 20 6c 6f 6f 70 73 20 74 68  oop the loops th
1f3a8 72 6f 75 67 68 20 6f 70 63 6f 64 65 73 20 2a 2f  rough opcodes */
1f3a9 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61  ..  /* If we rea
1f3aa 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 69  ch this point, i
1f3ab 74 20 6d 65 61 6e 73 20 74 68 61 74 20 65 78 65  t means that exe
1f3ac 63 75 74 69 6f 6e 20 69 73 20 66 69 6e 69 73 68  cution is finish
1f3ad 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 61 6e 20  ed with.  ** an 
1f3ae 65 72 72 6f 72 20 6f 66 20 73 6f 6d 65 20 6b 69  error of some ki
1f3af 6e 64 2e 0a 20 20 2a 2f 0a 76 64 62 65 5f 65 72  nd..  */.vdbe_er
1f3b0 72 6f 72 5f 68 61 6c 74 3a 0a 20 20 61 73 73 65  ror_halt:.  asse
1f3b1 72 74 28 20 72 63 20 29 3b 0a 20 20 70 2d 3e 72  rt( rc );.  p->r
1f3b2 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  c = rc;.  sqlite
1f3b3 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20 20  3VdbeHalt(p);.  
1f3b4 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49  if( rc==SQLITE_I
1f3b5 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 64 62 2d  OERR_NOMEM ) db-
1f3b6 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
1f3b7 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45  1;.  rc = SQLITE
1f3b8 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f 2a 20 54 68  _ERROR;..  /* Th
1f3b9 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 77  is is the only w
1f3ba 61 79 20 6f 75 74 20 6f 66 20 74 68 69 73 20 70  ay out of this p
1f3bb 72 6f 63 65 64 75 72 65 2e 20 20 57 65 20 68 61  rocedure.  We ha
1f3bc 76 65 20 74 6f 0a 20 20 2a 2a 20 72 65 6c 65 61  ve to.  ** relea
1f3bd 73 65 20 74 68 65 20 6d 75 74 65 78 65 73 20 6f  se the mutexes o
1f3be 6e 20 62 74 72 65 65 73 20 74 68 61 74 20 77 65  n btrees that we
1f3bf 72 65 20 61 63 71 75 69 72 65 64 20 61 74 20 74  re acquired at t
1f3c0 68 65 0a 20 20 2a 2a 20 74 6f 70 2e 20 2a 2f 0a  he.  ** top. */.
1f3c1 76 64 62 65 5f 72 65 74 75 72 6e 3a 0a 20 20 73  vdbe_return:.  s
1f3c2 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
1f3c3 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e 61  ArrayLeave(&p->a
1f3c4 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  Mutex);.  return
1f3c5 20 72 63 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20   rc;..  /* Jump 
1f3c6 74 6f 20 68 65 72 65 20 69 66 20 61 20 73 74 72  to here if a str
1f3c7 69 6e 67 20 6f 72 20 62 6c 6f 62 20 6c 61 72 67  ing or blob larg
1f3c8 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f 4d  er than SQLITE_M
1f3c9 41 58 5f 4c 45 4e 47 54 48 0a 20 20 2a 2a 20 69  AX_LENGTH.  ** i
1f3ca 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 20  s encountered.. 
1f3cb 20 2a 2f 0a 74 6f 6f 5f 62 69 67 3a 0a 20 20 73   */.too_big:.  s
1f3cc 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
1f3cd 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c  &p->zErrMsg, db,
1f3ce 20 22 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62   "string or blob
1f3cf 20 74 6f 6f 20 62 69 67 22 29 3b 0a 20 20 72 63   too big");.  rc
1f3d0 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47   = SQLITE_TOOBIG
1f3d1 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72  ;.  goto vdbe_er
1f3d2 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20  ror_halt;..  /* 
1f3d3 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66 20  Jump to here if 
1f3d4 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73  a malloc() fails
1f3d5 2e 0a 20 20 2a 2f 0a 6e 6f 5f 6d 65 6d 3a 0a 20  ..  */.no_mem:. 
1f3d6 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1f3d7 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65 33  d = 1;.  sqlite3
1f3d8 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a 45  SetString(&p->zE
1f3d9 72 72 4d 73 67 2c 20 64 62 2c 20 22 6f 75 74 20  rrMsg, db, "out 
1f3da 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 72  of memory");.  r
1f3db 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
1f3dc 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72  ;.  goto vdbe_er
1f3dd 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a 20  ror_halt;..  /* 
1f3de 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 66 6f 72  Jump to here for
1f3df 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53   an SQLITE_MISUS
1f3e0 45 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 61 62  E error..  */.ab
1f3e1 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
1f3e2 65 3a 0a 20 20 72 63 20 3d 20 53 51 4c 49 54 45  e:.  rc = SQLITE
1f3e3 5f 4d 49 53 55 53 45 3b 0a 20 20 2f 2a 20 46 61  _MISUSE;.  /* Fa
1f3e4 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 61 62 6f  ll thru into abo
1f3e5 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 20  rt_due_to_error 
1f3e6 2a 2f 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f  */..  /* Jump to
1f3e7 20 68 65 72 65 20 66 6f 72 20 61 6e 79 20 6f 74   here for any ot
1f3e8 68 65 72 20 6b 69 6e 64 20 6f 66 20 66 61 74 61  her kind of fata
1f3e9 6c 20 65 72 72 6f 72 2e 20 20 54 68 65 20 22 72  l error.  The "r
1f3ea 63 22 20 76 61 72 69 61 62 6c 65 0a 20 20 2a 2a  c" variable.  **
1f3eb 20 73 68 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65   should hold the
1f3ec 20 65 72 72 6f 72 20 6e 75 6d 62 65 72 2e 0a 20   error number.. 
1f3ed 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f   */.abort_due_to
1f3ee 5f 65 72 72 6f 72 3a 0a 20 20 61 73 73 65 72 74  _error:.  assert
1f3ef 28 20 70 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20  ( p->zErrMsg==0 
1f3f0 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  );.  if( db->mal
1f3f1 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 63 20 3d  locFailed ) rc =
1f3f2 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1f3f3 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1f3f4 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20  IOERR_NOMEM ){. 
1f3f5 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
1f3f6 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
1f3f7 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74   db, "%s", sqlit
1f3f8 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20  e3ErrStr(rc));. 
1f3f9 20 7d 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65   }.  goto vdbe_e
1f3fa 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a  rror_halt;..  /*
1f3fb 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66   Jump to here if
1f3fc 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 74   the sqlite3_int
1f3fd 65 72 72 75 70 74 28 29 20 41 50 49 20 73 65 74  errupt() API set
1f3fe 73 20 74 68 65 20 69 6e 74 65 72 72 75 70 74 0a  s the interrupt.
1f3ff 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 2a 2f 0a    ** flag..  */.
1f400 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74  abort_due_to_int
1f401 65 72 72 75 70 74 3a 0a 20 20 61 73 73 65 72 74  errupt:.  assert
1f402 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72  ( db->u1.isInter
1f403 72 75 70 74 65 64 20 29 3b 0a 20 20 72 63 20 3d  rupted );.  rc =
1f404 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50   SQLITE_INTERRUP
1f405 54 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63 3b  T;.  p->rc = rc;
1f406 0a 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72  .  sqlite3SetStr
1f407 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
1f408 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74   db, "%s", sqlit
1f409 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20  e3ErrStr(rc));. 
1f40a 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f 72   goto vdbe_error
1f40b 5f 68 61 6c 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  _halt;.}../*****
1f40c 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
1f40d 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   vdbe.c ********
1f40e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f40f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f410 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
1f411 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
1f412 66 69 6c 65 20 76 64 62 65 62 6c 6f 62 2e 63 20  file vdbeblob.c 
1f413 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f414 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f415 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
1f416 32 30 30 37 20 4d 61 79 20 31 0a 2a 2a 0a 2a 2a  2007 May 1.**.**
1f417 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1f418 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1f419 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1f41a 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1f41b 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1f41c 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1f41d 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1f41e 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1f41f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1f420 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1f421 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1f422 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1f423 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1f424 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1f425 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1f426 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1f427 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1f428 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f429 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f42a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f42b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f42c 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
1f42d 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
1f42e 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20  ns code used to 
1f42f 69 6d 70 6c 65 6d 65 6e 74 20 69 6e 63 72 65 6d  implement increm
1f430 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 2e 0a  ental BLOB I/O..
1f431 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 62  **.** $Id: vdbeb
1f432 6c 6f 62 2e 63 2c 76 20 31 2e 33 35 20 32 30 30  lob.c,v 1.35 200
1f433 39 2f 30 37 2f 30 32 20 30 37 3a 34 37 3a 33 33  9/07/02 07:47:33
1f434 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
1f435 20 24 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20   $.*/...#ifndef 
1f436 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52  SQLITE_OMIT_INCR
1f437 42 4c 4f 42 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 69  BLOB../*.** Vali
1f438 64 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 20  d sqlite3_blob* 
1f439 68 61 6e 64 6c 65 73 20 70 6f 69 6e 74 20 74 6f  handles point to
1f43a 20 49 6e 63 72 62 6c 6f 62 20 73 74 72 75 63 74   Incrblob struct
1f43b 75 72 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ures..*/.typedef
1f43c 20 73 74 72 75 63 74 20 49 6e 63 72 62 6c 6f 62   struct Incrblob
1f43d 20 49 6e 63 72 62 6c 6f 62 3b 0a 73 74 72 75 63   Incrblob;.struc
1f43e 74 20 49 6e 63 72 62 6c 6f 62 20 7b 0a 20 20 69  t Incrblob {.  i
1f43f 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20  nt flags;       
1f440 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f         /* Copy o
1f441 66 20 22 66 6c 61 67 73 22 20 70 61 73 73 65 64  f "flags" passed
1f442 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62   to sqlite3_blob
1f443 5f 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74  _open() */.  int
1f444 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20 20   nByte;         
1f445 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
1f446 6f 70 65 6e 20 62 6c 6f 62 2c 20 69 6e 20 62 79  open blob, in by
1f447 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 4f 66  tes */.  int iOf
1f448 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 20  fset;           
1f449 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74 20   /* Byte offset 
1f44a 6f 66 20 62 6c 6f 62 20 69 6e 20 63 75 72 73 6f  of blob in curso
1f44b 72 20 64 61 74 61 20 2a 2f 0a 20 20 42 74 43 75  r data */.  BtCu
1f44c 72 73 6f 72 20 2a 70 43 73 72 3b 20 20 20 20 20  rsor *pCsr;     
1f44d 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f      /* Cursor po
1f44e 69 6e 74 69 6e 67 20 61 74 20 62 6c 6f 62 20 72  inting at blob r
1f44f 6f 77 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ow */.  sqlite3_
1f450 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20 20  stmt *pStmt;    
1f451 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 68 6f 6c  /* Statement hol
1f452 64 69 6e 67 20 63 75 72 73 6f 72 20 6f 70 65 6e  ding cursor open
1f453 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
1f454 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b;            /*
1f455 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 64 20   The associated 
1f456 64 61 74 61 62 61 73 65 20 2a 2f 0a 7d 3b 0a 0a  database */.};..
1f457 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 62 6c 6f  /*.** Open a blo
1f458 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c  b handle..*/.SQL
1f459 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
1f45a 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a 20  te3_blob_open(. 
1f45b 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 20 20   sqlite3* db,   
1f45c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1f45d 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
1f45e 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ion */.  const c
1f45f 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 20 20  har *zDb,       
1f460 20 2f 2a 20 54 68 65 20 61 74 74 61 63 68 65 64   /* The attached
1f461 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
1f462 6e 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f  ning the blob */
1f463 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1f464 54 61 62 6c 65 2c 20 20 20 20 20 2f 2a 20 54 68  Table,     /* Th
1f465 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69  e table containi
1f466 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20  ng the blob */. 
1f467 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f   const char *zCo
1f468 6c 75 6d 6e 2c 20 20 20 20 2f 2a 20 54 68 65 20  lumn,    /* The 
1f469 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 69 6e  column containin
1f46a 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20 20  g the blob */.  
1f46b 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 52 6f  sqlite_int64 iRo
1f46c 77 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72  w,      /* The r
1f46d 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ow containing th
1f46e 65 20 67 6c 6f 62 20 2a 2f 0a 20 20 69 6e 74 20  e glob */.  int 
1f46f 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20  flags,          
1f470 20 20 20 20 2f 2a 20 54 72 75 65 20 2d 3e 20 72      /* True -> r
1f471 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73  ead/write access
1f472 2c 20 66 61 6c 73 65 20 2d 3e 20 72 65 61 64 2d  , false -> read-
1f473 6f 6e 6c 79 20 2a 2f 0a 20 20 73 71 6c 69 74 65  only */.  sqlite
1f474 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62 20  3_blob **ppBlob 
1f475 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20    /* Handle for 
1f476 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 62 6c  accessing the bl
1f477 6f 62 20 72 65 74 75 72 6e 65 64 20 68 65 72 65  ob returned here
1f478 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 41 74   */.){.  int nAt
1f479 74 65 6d 70 74 20 3d 20 30 3b 0a 20 20 69 6e 74  tempt = 0;.  int
1f47a 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20   iCol;          
1f47b 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
1f47c 20 7a 43 6f 6c 75 6d 6e 20 69 6e 20 72 6f 77 2d   zColumn in row-
1f47d 72 65 63 6f 72 64 20 2a 2f 0a 0a 20 20 2f 2a 20  record */..  /* 
1f47e 54 68 69 73 20 56 44 42 45 20 70 72 6f 67 72 61  This VDBE progra
1f47f 6d 20 73 65 65 6b 73 20 61 20 62 74 72 65 65 20  m seeks a btree 
1f480 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 69 64  cursor to the id
1f481 65 6e 74 69 66 69 65 64 20 0a 20 20 2a 2a 20 64  entified .  ** d
1f482 62 2f 74 61 62 6c 65 2f 72 6f 77 20 65 6e 74 72  b/table/row entr
1f483 79 2e 20 54 68 65 20 72 65 61 73 6f 6e 20 66 6f  y. The reason fo
1f484 72 20 75 73 69 6e 67 20 61 20 76 64 62 65 20 70  r using a vdbe p
1f485 72 6f 67 72 61 6d 20 69 6e 73 74 65 61 64 0a 20  rogram instead. 
1f486 20 2a 2a 20 6f 66 20 77 72 69 74 69 6e 67 20 63   ** of writing c
1f487 6f 64 65 20 74 6f 20 75 73 65 20 74 68 65 20 62  ode to use the b
1f488 2d 74 72 65 65 20 6c 61 79 65 72 20 64 69 72 65  -tree layer dire
1f489 63 74 6c 79 20 69 73 20 74 68 61 74 20 74 68 65  ctly is that the
1f48a 0a 20 20 2a 2a 20 76 64 62 65 20 70 72 6f 67 72  .  ** vdbe progr
1f48b 61 6d 20 77 69 6c 6c 20 74 61 6b 65 20 61 64 76  am will take adv
1f48c 61 6e 74 61 67 65 20 6f 66 20 74 68 65 20 76 61  antage of the va
1f48d 72 69 6f 75 73 20 74 72 61 6e 73 61 63 74 69 6f  rious transactio
1f48e 6e 2c 0a 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67 20  n,.  ** locking 
1f48f 61 6e 64 20 65 72 72 6f 72 20 68 61 6e 64 6c 69  and error handli
1f490 6e 67 20 69 6e 66 72 61 73 74 72 75 63 74 75 72  ng infrastructur
1f491 65 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65  e built into the
1f492 20 76 64 62 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a   vdbe..  **.  **
1f493 20 41 66 74 65 72 20 73 65 65 6b 69 6e 67 20 74   After seeking t
1f494 68 65 20 63 75 72 73 6f 72 2c 20 74 68 65 20 76  he cursor, the v
1f495 64 62 65 20 65 78 65 63 75 74 65 73 20 61 6e 20  dbe executes an 
1f496 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2e 0a 20 20  OP_ResultRow..  
1f497 2a 2a 20 43 6f 64 65 20 65 78 74 65 72 6e 61 6c  ** Code external
1f498 20 74 6f 20 74 68 65 20 56 64 62 65 20 74 68 65   to the Vdbe the
1f499 6e 20 22 62 6f 72 72 6f 77 73 22 20 74 68 65 20  n "borrows" the 
1f49a 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20 61 6e  b-tree cursor an
1f49b 64 0a 20 20 2a 2a 20 75 73 65 73 20 69 74 20 74  d.  ** uses it t
1f49c 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
1f49d 62 6c 6f 62 5f 72 65 61 64 28 29 2c 20 62 6c 6f  blob_read(), blo
1f49e 62 5f 77 72 69 74 65 28 29 20 61 6e 64 20 0a 20  b_write() and . 
1f49f 20 2a 2a 20 62 6c 6f 62 5f 62 79 74 65 73 28 29   ** blob_bytes()
1f4a0 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a   functions..  **
1f4a1 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  .  ** The sqlite
1f4a2 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 20 66  3_blob_close() f
1f4a3 75 6e 63 74 69 6f 6e 20 66 69 6e 61 6c 69 7a 65  unction finalize
1f4a4 73 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72  s the vdbe progr
1f4a5 61 6d 2c 0a 20 20 2a 2a 20 77 68 69 63 68 20 63  am,.  ** which c
1f4a6 6c 6f 73 65 73 20 74 68 65 20 62 2d 74 72 65 65  loses the b-tree
1f4a7 20 63 75 72 73 6f 72 20 61 6e 64 20 28 70 6f 73   cursor and (pos
1f4a8 73 69 62 6c 79 29 20 63 6f 6d 6d 69 74 73 20 74  sibly) commits t
1f4a9 68 65 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63  he .  ** transac
1f4aa 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 74 61  tion..  */.  sta
1f4ab 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70  tic const VdbeOp
1f4ac 4c 69 73 74 20 6f 70 65 6e 42 6c 6f 62 5b 5d 20  List openBlob[] 
1f4ad 3d 20 7b 0a 20 20 20 20 7b 4f 50 5f 54 72 61 6e  = {.    {OP_Tran
1f4ae 73 61 63 74 69 6f 6e 2c 20 30 2c 20 30 2c 20 30  saction, 0, 0, 0
1f4af 7d 2c 20 20 20 20 20 2f 2a 20 30 3a 20 53 74 61  },     /* 0: Sta
1f4b0 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  rt a transaction
1f4b1 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 56 65 72 69   */.    {OP_Veri
1f4b2 66 79 43 6f 6f 6b 69 65 2c 20 30 2c 20 30 2c 20  fyCookie, 0, 0, 
1f4b3 30 7d 2c 20 20 20 20 2f 2a 20 31 3a 20 43 68 65  0},    /* 1: Che
1f4b4 63 6b 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f  ck the schema co
1f4b5 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f  okie */.    {OP_
1f4b6 54 61 62 6c 65 4c 6f 63 6b 2c 20 30 2c 20 30 2c  TableLock, 0, 0,
1f4b7 20 30 7d 2c 20 20 20 20 20 20 20 2f 2a 20 32 3a   0},       /* 2:
1f4b8 20 41 63 71 75 69 72 65 20 61 20 72 65 61 64 20   Acquire a read 
1f4b9 6f 72 20 77 72 69 74 65 20 6c 6f 63 6b 20 2a 2f  or write lock */
1f4ba 0a 0a 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20  ..    /* One of 
1f4bb 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77  the following tw
1f4bc 6f 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20 69  o instructions i
1f4bd 73 20 72 65 70 6c 61 63 65 64 20 62 79 20 61 6e  s replaced by an
1f4be 20 4f 50 5f 4e 6f 6f 70 2e 20 2a 2f 0a 20 20 20   OP_Noop. */.   
1f4bf 20 7b 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 30   {OP_OpenRead, 0
1f4c0 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20 20  , 0, 0},        
1f4c1 2f 2a 20 33 3a 20 4f 70 65 6e 20 63 75 72 73 6f  /* 3: Open curso
1f4c2 72 20 30 20 66 6f 72 20 72 65 61 64 69 6e 67 20  r 0 for reading 
1f4c3 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f 70 65 6e 57  */.    {OP_OpenW
1f4c4 72 69 74 65 2c 20 30 2c 20 30 2c 20 30 7d 2c 20  rite, 0, 0, 0}, 
1f4c5 20 20 20 20 20 20 2f 2a 20 34 3a 20 4f 70 65 6e        /* 4: Open
1f4c6 20 63 75 72 73 6f 72 20 30 20 66 6f 72 20 72 65   cursor 0 for re
1f4c7 61 64 2f 77 72 69 74 65 20 2a 2f 0a 0a 20 20 20  ad/write */..   
1f4c8 20 7b 4f 50 5f 56 61 72 69 61 62 6c 65 2c 20 31   {OP_Variable, 1
1f4c9 2c 20 31 2c 20 31 7d 2c 20 20 20 20 20 20 20 20  , 1, 1},        
1f4ca 2f 2a 20 35 3a 20 50 75 73 68 20 74 68 65 20 72  /* 5: Push the r
1f4cb 6f 77 69 64 20 74 6f 20 74 68 65 20 73 74 61 63  owid to the stac
1f4cc 6b 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4e 6f 74  k */.    {OP_Not
1f4cd 45 78 69 73 74 73 2c 20 30 2c 20 39 2c 20 31 7d  Exists, 0, 9, 1}
1f4ce 2c 20 20 20 20 20 20 20 2f 2a 20 36 3a 20 53 65  ,       /* 6: Se
1f4cf 65 6b 20 74 68 65 20 63 75 72 73 6f 72 20 2a 2f  ek the cursor */
1f4d0 0a 20 20 20 20 7b 4f 50 5f 43 6f 6c 75 6d 6e 2c  .    {OP_Column,
1f4d1 20 30 2c 20 30 2c 20 31 7d 2c 20 20 20 20 20 20   0, 0, 1},      
1f4d2 20 20 20 20 2f 2a 20 37 20 20 2a 2f 0a 20 20 20      /* 7  */.   
1f4d3 20 7b 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20   {OP_ResultRow, 
1f4d4 31 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20 20 20  1, 0, 0},       
1f4d5 2f 2a 20 38 20 20 2a 2f 0a 20 20 20 20 7b 4f 50  /* 8  */.    {OP
1f4d6 5f 43 6c 6f 73 65 2c 20 30 2c 20 30 2c 20 30 7d  _Close, 0, 0, 0}
1f4d7 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 39  ,           /* 9
1f4d8 20 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 48 61 6c    */.    {OP_Hal
1f4d9 74 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20  t, 0, 0, 0},    
1f4da 20 20 20 20 20 20 20 20 2f 2a 20 31 30 20 2a 2f          /* 10 */
1f4db 0a 20 20 7d 3b 0a 0a 20 20 56 64 62 65 20 2a 76  .  };..  Vdbe *v
1f4dc 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d   = 0;.  int rc =
1f4dd 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 68   SQLITE_OK;.  ch
1f4de 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 20 20  ar *zErr = 0;.  
1f4df 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 50  Table *pTab;.  P
1f4e0 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20  arse *pParse;.. 
1f4e1 20 2a 70 70 42 6c 6f 62 20 3d 20 30 3b 0a 20 20   *ppBlob = 0;.  
1f4e2 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1f4e3 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
1f4e4 20 20 70 50 61 72 73 65 20 3d 20 73 71 6c 69 74    pParse = sqlit
1f4e5 65 33 53 74 61 63 6b 41 6c 6c 6f 63 52 61 77 28  e3StackAllocRaw(
1f4e6 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 50 61 72  db, sizeof(*pPar
1f4e7 73 65 29 29 3b 0a 20 20 69 66 28 20 70 50 61 72  se));.  if( pPar
1f4e8 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20  se==0 ){.    rc 
1f4e9 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
1f4ea 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70      goto blob_op
1f4eb 65 6e 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 64 6f  en_out;.  }.  do
1f4ec 20 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50   {.    memset(pP
1f4ed 61 72 73 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28  arse, 0, sizeof(
1f4ee 50 61 72 73 65 29 29 3b 0a 20 20 20 20 70 50 61  Parse));.    pPa
1f4ef 72 73 65 2d 3e 64 62 20 3d 20 64 62 3b 0a 0a 20  rse->db = db;.. 
1f4f0 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61     if( sqlite3Sa
1f4f1 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20  fetyOn(db) ){.  
1f4f2 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1f4f3 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20  e(db, zErr);.   
1f4f4 20 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46     sqlite3StackF
1f4f5 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 29 3b  ree(db, pParse);
1f4f6 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
1f4f7 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
1f4f8 75 74 65 78 29 3b 0a 20 20 20 20 20 20 72 65 74  utex);.      ret
1f4f9 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
1f4fa 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71  E;.    }..    sq
1f4fb 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41  lite3BtreeEnterA
1f4fc 6c 6c 28 64 62 29 3b 0a 20 20 20 20 70 54 61 62  ll(db);.    pTab
1f4fd 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65   = sqlite3Locate
1f4fe 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c  Table(pParse, 0,
1f4ff 20 7a 54 61 62 6c 65 2c 20 7a 44 62 29 3b 0a 20   zTable, zDb);. 
1f500 20 20 20 69 66 28 20 70 54 61 62 20 26 26 20 49     if( pTab && I
1f501 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
1f502 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 30  {.      pTab = 0
1f503 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
1f504 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1f505 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 76 69 72  "cannot open vir
1f506 74 75 61 6c 20 74 61 62 6c 65 3a 20 25 73 22 2c  tual table: %s",
1f507 20 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a   zTable);.    }.
1f508 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1f509 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 69 66 28  MIT_VIEW.    if(
1f50a 20 70 54 61 62 20 26 26 20 70 54 61 62 2d 3e 70   pTab && pTab->p
1f50b 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20  Select ){.      
1f50c 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20  pTab = 0;.      
1f50d 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1f50e 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20  pParse, "cannot 
1f50f 6f 70 65 6e 20 76 69 65 77 3a 20 25 73 22 2c 20  open view: %s", 
1f510 7a 54 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 23  zTable);.    }.#
1f511 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20 21 70  endif.    if( !p
1f512 54 61 62 20 29 7b 0a 20 20 20 20 20 20 69 66 28  Tab ){.      if(
1f513 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
1f514 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1f515 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45  te3DbFree(db, zE
1f516 72 72 29 3b 0a 20 20 20 20 20 20 20 20 7a 45 72  rr);.        zEr
1f517 72 20 3d 20 70 50 61 72 73 65 2d 3e 7a 45 72 72  r = pParse->zErr
1f518 4d 73 67 3b 0a 20 20 20 20 20 20 20 20 70 50 61  Msg;.        pPa
1f519 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30  rse->zErrMsg = 0
1f51a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1f51b 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc = SQLITE_ERRO
1f51c 52 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73  R;.      (void)s
1f51d 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
1f51e 64 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  db);.      sqlit
1f51f 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28  e3BtreeLeaveAll(
1f520 64 62 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  db);.      goto 
1f521 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20  blob_open_out;. 
1f522 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4e 6f 77     }..    /* Now
1f523 20 73 65 61 72 63 68 20 70 54 61 62 20 66 6f 72   search pTab for
1f524 20 74 68 65 20 65 78 61 63 74 20 63 6f 6c 75 6d   the exact colum
1f525 6e 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 43  n. */.    for(iC
1f526 6f 6c 3d 30 3b 20 69 43 6f 6c 20 3c 20 70 54 61  ol=0; iCol < pTa
1f527 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29  b->nCol; iCol++)
1f528 20 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c   {.      if( sql
1f529 69 74 65 33 53 74 72 49 43 6d 70 28 70 54 61 62  ite3StrICmp(pTab
1f52a 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61  ->aCol[iCol].zNa
1f52b 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 29 3d 3d 30 20  me, zColumn)==0 
1f52c 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ){.        break
1f52d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1f52e 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 54      if( iCol==pT
1f52f 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20  ab->nCol ){.    
1f530 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1f531 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20  db, zErr);.     
1f532 20 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d   zErr = sqlite3M
1f533 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f 20 73  Printf(db, "no s
1f534 75 63 68 20 63 6f 6c 75 6d 6e 3a 20 5c 22 25 73  uch column: \"%s
1f535 5c 22 22 2c 20 7a 43 6f 6c 75 6d 6e 29 3b 0a 20  \"", zColumn);. 
1f536 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
1f537 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 28 76  _ERROR;.      (v
1f538 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
1f539 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20  yOff(db);.      
1f53a 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
1f53b 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20  eAll(db);.      
1f53c 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f  goto blob_open_o
1f53d 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  ut;.    }..    /
1f53e 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20 69  * If the value i
1f53f 73 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 66  s being opened f
1f540 6f 72 20 77 72 69 74 69 6e 67 2c 20 63 68 65 63  or writing, chec
1f541 6b 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a  k that the.    *
1f542 2a 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20  * column is not 
1f543 69 6e 64 65 78 65 64 2c 20 61 6e 64 20 74 68 61  indexed, and tha
1f544 74 20 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74  t it is not part
1f545 20 6f 66 20 61 20 66 6f 72 65 69 67 6e 20 6b 65   of a foreign ke
1f546 79 2e 20 0a 20 20 20 20 2a 2a 20 49 74 20 69 73  y. .    ** It is
1f547 20 61 67 61 69 6e 73 74 20 74 68 65 20 72 75 6c   against the rul
1f548 65 73 20 74 6f 20 6f 70 65 6e 20 61 20 63 6f 6c  es to open a col
1f549 75 6d 6e 20 74 6f 20 77 68 69 63 68 20 65 69 74  umn to which eit
1f54a 68 65 72 20 6f 66 20 74 68 65 73 65 0a 20 20 20  her of these.   
1f54b 20 2a 2a 20 64 65 73 63 72 69 70 74 69 6f 6e 73   ** descriptions
1f54c 20 61 70 70 6c 69 65 73 20 66 6f 72 20 77 72 69   applies for wri
1f54d 74 69 6e 67 2e 20 20 2a 2f 0a 20 20 20 20 69 66  ting.  */.    if
1f54e 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20 20 20  ( flags ){.     
1f54f 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 61   const char *zFa
1f550 75 6c 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 49  ult = 0;.      I
1f551 6e 64 65 78 20 2a 70 49 64 78 3b 0a 23 69 66 6e  ndex *pIdx;.#ifn
1f552 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1f553 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 20 20  FOREIGN_KEY.    
1f554 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 26    if( db->flags&
1f555 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65  SQLITE_ForeignKe
1f556 79 73 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  ys ){.        /*
1f557 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20   Check that the 
1f558 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 70 61  column is not pa
1f559 72 74 20 6f 66 20 61 6e 20 46 4b 20 63 68 69 6c  rt of an FK chil
1f55a 64 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f 6e  d key definition
1f55b 2e 20 49 74 0a 20 20 20 20 20 20 20 20 2a 2a 20  . It.        ** 
1f55c 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79  is not necessary
1f55d 20 74 6f 20 63 68 65 63 6b 20 69 66 20 69 74 20   to check if it 
1f55e 69 73 20 70 61 72 74 20 6f 66 20 61 20 70 61 72  is part of a par
1f55f 65 6e 74 20 6b 65 79 2c 20 61 73 20 70 61 72 65  ent key, as pare
1f560 6e 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 6b 65  nt.        ** ke
1f561 79 20 63 6f 6c 75 6d 6e 73 20 6d 75 73 74 20 62  y columns must b
1f562 65 20 69 6e 64 65 78 65 64 2e 20 54 68 65 20 63  e indexed. The c
1f563 68 65 63 6b 20 62 65 6c 6f 77 20 77 69 6c 6c 20  heck below will 
1f564 70 69 63 6b 20 75 70 20 74 68 69 73 20 0a 20 20  pick up this .  
1f565 20 20 20 20 20 20 2a 2a 20 63 61 73 65 2e 20 20        ** case.  
1f566 2a 2f 0a 20 20 20 20 20 20 20 20 46 4b 65 79 20  */.        FKey 
1f567 2a 70 46 4b 65 79 3b 0a 20 20 20 20 20 20 20 20  *pFKey;.        
1f568 66 6f 72 28 70 46 4b 65 79 3d 70 54 61 62 2d 3e  for(pFKey=pTab->
1f569 70 46 4b 65 79 3b 20 70 46 4b 65 79 3b 20 70 46  pFKey; pFKey; pF
1f56a 4b 65 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78 74  Key=pFKey->pNext
1f56b 46 72 6f 6d 29 7b 0a 20 20 20 20 20 20 20 20 20  From){.         
1f56c 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20 20   int j;.        
1f56d 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 46 4b    for(j=0; j<pFK
1f56e 65 79 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a  ey->nCol; j++){.
1f56f 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
1f570 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 6a 5d 2e 69  pFKey->aCol[j].i
1f571 46 72 6f 6d 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20  From==iCol ){.  
1f572 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 61 75              zFau
1f573 6c 74 20 3d 20 22 66 6f 72 65 69 67 6e 20 6b 65  lt = "foreign ke
1f574 79 22 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  y";.            
1f575 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  }.          }.  
1f576 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1f577 23 65 6e 64 69 66 0a 20 20 20 20 20 20 66 6f 72  #endif.      for
1f578 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64  (pIdx=pTab->pInd
1f579 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
1f57a 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Idx->pNext){.   
1f57b 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
1f57c 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
1f57d 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b  Idx->nColumn; j+
1f57e 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
1f57f 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  ( pIdx->aiColumn
1f580 5b 6a 5d 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20  [j]==iCol ){.   
1f581 20 20 20 20 20 20 20 20 20 7a 46 61 75 6c 74 20           zFault 
1f582 3d 20 22 69 6e 64 65 78 65 64 22 3b 0a 20 20 20  = "indexed";.   
1f583 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1f584 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
1f585 20 69 66 28 20 7a 46 61 75 6c 74 20 29 7b 0a 20   if( zFault ){. 
1f586 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
1f587 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a  Free(db, zErr);.
1f588 20 20 20 20 20 20 20 20 7a 45 72 72 20 3d 20 73          zErr = s
1f589 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
1f58a 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20 25  , "cannot open %
1f58b 73 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 72 69  s column for wri
1f58c 74 69 6e 67 22 2c 20 7a 46 61 75 6c 74 29 3b 0a  ting", zFault);.
1f58d 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
1f58e 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
1f58f 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33     (void)sqlite3
1f590 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20  SafetyOff(db);. 
1f591 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74         sqlite3Bt
1f592 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b  reeLeaveAll(db);
1f593 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 6c  .        goto bl
1f594 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20  ob_open_out;.   
1f595 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
1f596 76 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  v = sqlite3VdbeC
1f597 72 65 61 74 65 28 64 62 29 3b 0a 20 20 20 20 69  reate(db);.    i
1f598 66 28 20 76 20 29 7b 0a 20 20 20 20 20 20 69 6e  f( v ){.      in
1f599 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53  t iDb = sqlite3S
1f59a 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
1f59b 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
1f59c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1f59d 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 73  beAddOpList(v, s
1f59e 69 7a 65 6f 66 28 6f 70 65 6e 42 6c 6f 62 29 2f  izeof(openBlob)/
1f59f 73 69 7a 65 6f 66 28 56 64 62 65 4f 70 4c 69 73  sizeof(VdbeOpLis
1f5a0 74 29 2c 20 6f 70 65 6e 42 6c 6f 62 29 3b 0a 20  t), openBlob);. 
1f5a1 20 20 20 20 20 66 6c 61 67 73 20 3d 20 21 21 66       flags = !!f
1f5a2 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20  lags;           
1f5a3 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 3d        /* flags =
1f5a4 20 28 66 6c 61 67 73 20 3f 20 31 20 3a 20 30 29   (flags ? 1 : 0)
1f5a5 3b 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 43  ; */..      /* C
1f5a6 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f  onfigure the OP_
1f5a7 54 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20  Transaction */. 
1f5a8 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f5a9 43 68 61 6e 67 65 50 31 28 76 2c 20 30 2c 20 69  ChangeP1(v, 0, i
1f5aa 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Db);.      sqlit
1f5ab 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76  e3VdbeChangeP2(v
1f5ac 2c 20 30 2c 20 66 6c 61 67 73 29 3b 0a 0a 20 20  , 0, flags);..  
1f5ad 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65      /* Configure
1f5ae 20 74 68 65 20 4f 50 5f 56 65 72 69 66 79 43 6f   the OP_VerifyCo
1f5af 6f 6b 69 65 20 2a 2f 0a 20 20 20 20 20 20 73 71  okie */.      sq
1f5b0 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1f5b1 31 28 76 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20  1(v, 1, iDb);.  
1f5b2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
1f5b3 68 61 6e 67 65 50 32 28 76 2c 20 31 2c 20 70 54  hangeP2(v, 1, pT
1f5b4 61 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68  ab->pSchema->sch
1f5b5 65 6d 61 5f 63 6f 6f 6b 69 65 29 3b 0a 0a 20 20  ema_cookie);..  
1f5b6 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65      /* Make sure
1f5b7 20 61 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64   a mutex is held
1f5b8 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f   on the table to
1f5b9 20 62 65 20 61 63 63 65 73 73 65 64 20 2a 2f 0a   be accessed */.
1f5ba 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f5bb 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44  eUsesBtree(v, iD
1f5bc 62 29 3b 20 0a 0a 20 20 20 20 20 20 2f 2a 20 43  b); ..      /* C
1f5bd 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f  onfigure the OP_
1f5be 54 61 62 6c 65 4c 6f 63 6b 20 69 6e 73 74 72 75  TableLock instru
1f5bf 63 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73  ction */.      s
1f5c0 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1f5c1 50 31 28 76 2c 20 32 2c 20 69 44 62 29 3b 0a 20  P1(v, 2, iDb);. 
1f5c2 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f5c3 43 68 61 6e 67 65 50 32 28 76 2c 20 32 2c 20 70  ChangeP2(v, 2, p
1f5c4 54 61 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20  Tab->tnum);.    
1f5c5 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1f5c6 6e 67 65 50 33 28 76 2c 20 32 2c 20 66 6c 61 67  ngeP3(v, 2, flag
1f5c7 73 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  s);.      sqlite
1f5c8 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
1f5c9 20 32 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c   2, pTab->zName,
1f5ca 20 50 34 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a   P4_TRANSIENT);.
1f5cb 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65  .      /* Remove
1f5cc 20 65 69 74 68 65 72 20 74 68 65 20 4f 50 5f 4f   either the OP_O
1f5cd 70 65 6e 57 72 69 74 65 20 6f 72 20 4f 70 65 6e  penWrite or Open
1f5ce 52 65 61 64 2e 20 53 65 74 20 74 68 65 20 50 32  Read. Set the P2
1f5cf 20 0a 20 20 20 20 20 20 2a 2a 20 70 61 72 61 6d   .      ** param
1f5d0 65 74 65 72 20 6f 66 20 74 68 65 20 6f 74 68 65  eter of the othe
1f5d1 72 20 74 6f 20 70 54 61 62 2d 3e 74 6e 75 6d 2e  r to pTab->tnum.
1f5d2 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74    */.      sqlit
1f5d3 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f  e3VdbeChangeToNo
1f5d4 6f 70 28 76 2c 20 34 20 2d 20 66 6c 61 67 73 2c  op(v, 4 - flags,
1f5d5 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   1);.      sqlit
1f5d6 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76  e3VdbeChangeP2(v
1f5d7 2c 20 33 20 2b 20 66 6c 61 67 73 2c 20 70 54 61  , 3 + flags, pTa
1f5d8 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20  b->tnum);.      
1f5d9 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1f5da 65 50 33 28 76 2c 20 33 20 2b 20 66 6c 61 67 73  eP3(v, 3 + flags
1f5db 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 20 20 2f  , iDb);..      /
1f5dc 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20  * Configure the 
1f5dd 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
1f5de 73 2e 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65  s. Configure the
1f5df 20 63 75 72 73 6f 72 20 74 6f 0a 20 20 20 20 20   cursor to.     
1f5e0 20 2a 2a 20 74 68 69 6e 6b 20 74 68 61 74 20 74   ** think that t
1f5e1 68 65 20 74 61 62 6c 65 20 68 61 73 20 6f 6e 65  he table has one
1f5e2 20 6d 6f 72 65 20 63 6f 6c 75 6d 6e 20 74 68 61   more column tha
1f5e3 6e 20 69 74 20 72 65 61 6c 6c 79 0a 20 20 20 20  n it really.    
1f5e4 20 20 2a 2a 20 64 6f 65 73 2e 20 41 6e 20 4f 50    ** does. An OP
1f5e5 5f 43 6f 6c 75 6d 6e 20 74 6f 20 72 65 74 72 69  _Column to retri
1f5e6 65 76 65 20 74 68 69 73 20 69 6d 61 67 69 6e 61  eve this imagina
1f5e7 72 79 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c 0a 20  ry column will. 
1f5e8 20 20 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 72       ** always r
1f5e9 65 74 75 72 6e 20 61 6e 20 53 51 4c 20 4e 55 4c  eturn an SQL NUL
1f5ea 4c 2e 20 54 68 69 73 20 69 73 20 75 73 65 66 75  L. This is usefu
1f5eb 6c 20 62 65 63 61 75 73 65 20 69 74 20 6d 65 61  l because it mea
1f5ec 6e 73 0a 20 20 20 20 20 20 2a 2a 20 77 65 20 63  ns.      ** we c
1f5ed 61 6e 20 69 6e 76 6f 6b 65 20 4f 50 5f 43 6f 6c  an invoke OP_Col
1f5ee 75 6d 6e 20 74 6f 20 66 69 6c 6c 20 69 6e 20 74  umn to fill in t
1f5ef 68 65 20 76 64 62 65 20 63 75 72 73 6f 72 73 20  he vdbe cursors 
1f5f0 74 79 70 65 20 0a 20 20 20 20 20 20 2a 2a 20 61  type .      ** a
1f5f1 6e 64 20 6f 66 66 73 65 74 20 63 61 63 68 65 20  nd offset cache 
1f5f2 77 69 74 68 6f 75 74 20 63 61 75 73 69 6e 67 20  without causing 
1f5f3 61 6e 79 20 49 4f 2e 0a 20 20 20 20 20 20 2a 2f  any IO..      */
1f5f4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1f5f5 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 33 2b  beChangeP4(v, 3+
1f5f6 66 6c 61 67 73 2c 20 53 51 4c 49 54 45 5f 49 4e  flags, SQLITE_IN
1f5f7 54 5f 54 4f 5f 50 54 52 28 70 54 61 62 2d 3e 6e  T_TO_PTR(pTab->n
1f5f8 43 6f 6c 2b 31 29 2c 50 34 5f 49 4e 54 33 32 29  Col+1),P4_INT32)
1f5f9 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1f5fa 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 37  dbeChangeP2(v, 7
1f5fb 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20  , pTab->nCol);. 
1f5fc 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61       if( !db->ma
1f5fd 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
1f5fe 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1f5ff 65 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 31 2c  eMakeReady(v, 1,
1f600 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 29 3b   1, 1, 0, 0, 0);
1f601 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1f602 20 20 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74    .    sqlite3Bt
1f603 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b  reeLeaveAll(db);
1f604 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
1f605 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
1f606 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 63      if( NEVER(rc
1f607 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 7c 7c 20  !=SQLITE_OK) || 
1f608 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1f609 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 62   ){.      goto b
1f60a 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20  lob_open_out;.  
1f60b 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33    }..    sqlite3
1f60c 5f 62 69 6e 64 5f 69 6e 74 36 34 28 28 73 71 6c  _bind_int64((sql
1f60d 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 2c 20 31  ite3_stmt *)v, 1
1f60e 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 72 63 20  , iRow);.    rc 
1f60f 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 28  = sqlite3_step((
1f610 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76  sqlite3_stmt *)v
1f611 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
1f612 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
1f613 20 20 20 6e 41 74 74 65 6d 70 74 2b 2b 3b 0a 20     nAttempt++;. 
1f614 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
1f615 33 5f 66 69 6e 61 6c 69 7a 65 28 28 73 71 6c 69  3_finalize((sqli
1f616 74 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b 0a 20  te3_stmt *)v);. 
1f617 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
1f618 65 65 28 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20  ee(db, zErr);.  
1f619 20 20 20 20 7a 45 72 72 20 3d 20 73 71 6c 69 74      zErr = sqlit
1f61a 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 73 71  e3MPrintf(db, sq
1f61b 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29  lite3_errmsg(db)
1f61c 29 3b 0a 20 20 20 20 20 20 76 20 3d 20 30 3b 0a  );.      v = 0;.
1f61d 20 20 20 20 7d 0a 20 20 7d 20 77 68 69 6c 65 28      }.  } while(
1f61e 20 6e 41 74 74 65 6d 70 74 3c 35 20 26 26 20 72   nAttempt<5 && r
1f61f 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  c==SQLITE_SCHEMA
1f620 20 29 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53   );..  if( rc==S
1f621 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
1f622 20 2f 2a 20 54 68 65 20 72 6f 77 2d 72 65 63 6f   /* The row-reco
1f623 72 64 20 68 61 73 20 62 65 65 6e 20 6f 70 65 6e  rd has been open
1f624 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e  ed successfully.
1f625 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 0a   Check that the.
1f626 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e      ** column in
1f627 20 71 75 65 73 74 69 6f 6e 20 63 6f 6e 74 61 69   question contai
1f628 6e 73 20 74 65 78 74 20 6f 72 20 61 20 62 6c 6f  ns text or a blo
1f629 62 2e 20 49 66 20 69 74 20 63 6f 6e 74 61 69 6e  b. If it contain
1f62a 73 0a 20 20 20 20 2a 2a 20 74 65 78 74 2c 20 69  s.    ** text, i
1f62b 74 20 69 73 20 75 70 20 74 6f 20 74 68 65 20 63  t is up to the c
1f62c 61 6c 6c 65 72 20 74 6f 20 67 65 74 20 74 68 65  aller to get the
1f62d 20 65 6e 63 6f 64 69 6e 67 20 72 69 67 68 74 2e   encoding right.
1f62e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 49 6e 63 72  .    */.    Incr
1f62f 62 6c 6f 62 20 2a 70 42 6c 6f 62 3b 0a 20 20 20  blob *pBlob;.   
1f630 20 75 33 32 20 74 79 70 65 20 3d 20 76 2d 3e 61   u32 type = v->a
1f631 70 43 73 72 5b 30 5d 2d 3e 61 54 79 70 65 5b 69  pCsr[0]->aType[i
1f632 43 6f 6c 5d 3b 0a 0a 20 20 20 20 69 66 28 20 74  Col];..    if( t
1f633 79 70 65 3c 31 32 20 29 7b 0a 20 20 20 20 20 20  ype<12 ){.      
1f634 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1f635 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20 20 7a  , zErr);.      z
1f636 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Err = sqlite3MPr
1f637 69 6e 74 66 28 64 62 2c 20 22 63 61 6e 6e 6f 74  intf(db, "cannot
1f638 20 6f 70 65 6e 20 76 61 6c 75 65 20 6f 66 20 74   open value of t
1f639 79 70 65 20 25 73 22 2c 0a 20 20 20 20 20 20 20  ype %s",.       
1f63a 20 20 20 74 79 70 65 3d 3d 30 3f 22 6e 75 6c 6c     type==0?"null
1f63b 22 3a 20 74 79 70 65 3d 3d 37 3f 22 72 65 61 6c  ": type==7?"real
1f63c 22 3a 20 22 69 6e 74 65 67 65 72 22 0a 20 20 20  ": "integer".   
1f63d 20 20 20 29 3b 0a 20 20 20 20 20 20 72 63 20 3d     );.      rc =
1f63e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1f63f 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f       goto blob_o
1f640 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  pen_out;.    }. 
1f641 20 20 20 70 42 6c 6f 62 20 3d 20 28 49 6e 63 72     pBlob = (Incr
1f642 62 6c 6f 62 20 2a 29 73 71 6c 69 74 65 33 44 62  blob *)sqlite3Db
1f643 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
1f644 69 7a 65 6f 66 28 49 6e 63 72 62 6c 6f 62 29 29  izeof(Incrblob))
1f645 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61  ;.    if( db->ma
1f646 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
1f647 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1f648 65 28 64 62 2c 20 70 42 6c 6f 62 29 3b 0a 20 20  e(db, pBlob);.  
1f649 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f 70      goto blob_op
1f64a 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  en_out;.    }.  
1f64b 20 20 70 42 6c 6f 62 2d 3e 66 6c 61 67 73 20 3d    pBlob->flags =
1f64c 20 66 6c 61 67 73 3b 0a 20 20 20 20 70 42 6c 6f   flags;.    pBlo
1f64d 62 2d 3e 70 43 73 72 20 3d 20 20 76 2d 3e 61 70  b->pCsr =  v->ap
1f64e 43 73 72 5b 30 5d 2d 3e 70 43 75 72 73 6f 72 3b  Csr[0]->pCursor;
1f64f 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1f650 65 45 6e 74 65 72 43 75 72 73 6f 72 28 70 42 6c  eEnterCursor(pBl
1f651 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 73  ob->pCsr);.    s
1f652 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68 65  qlite3BtreeCache
1f653 4f 76 65 72 66 6c 6f 77 28 70 42 6c 6f 62 2d 3e  Overflow(pBlob->
1f654 70 43 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74  pCsr);.    sqlit
1f655 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72 73  e3BtreeLeaveCurs
1f656 6f 72 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 3b  or(pBlob->pCsr);
1f657 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 53 74 6d  .    pBlob->pStm
1f658 74 20 3d 20 28 73 71 6c 69 74 65 33 5f 73 74 6d  t = (sqlite3_stm
1f659 74 20 2a 29 76 3b 0a 20 20 20 20 70 42 6c 6f 62  t *)v;.    pBlob
1f65a 2d 3e 69 4f 66 66 73 65 74 20 3d 20 76 2d 3e 61  ->iOffset = v->a
1f65b 70 43 73 72 5b 30 5d 2d 3e 61 4f 66 66 73 65 74  pCsr[0]->aOffset
1f65c 5b 69 43 6f 6c 5d 3b 0a 20 20 20 20 70 42 6c 6f  [iCol];.    pBlo
1f65d 62 2d 3e 6e 42 79 74 65 20 3d 20 73 71 6c 69 74  b->nByte = sqlit
1f65e 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
1f65f 4c 65 6e 28 74 79 70 65 29 3b 0a 20 20 20 20 70  Len(type);.    p
1f660 42 6c 6f 62 2d 3e 64 62 20 3d 20 64 62 3b 0a 20  Blob->db = db;. 
1f661 20 20 20 2a 70 70 42 6c 6f 62 20 3d 20 28 73 71     *ppBlob = (sq
1f662 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 29 70 42 6c  lite3_blob *)pBl
1f663 6f 62 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ob;.    rc = SQL
1f664 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 20  ITE_OK;.  }else 
1f665 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1f666 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  K ){.    sqlite3
1f667 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29  DbFree(db, zErr)
1f668 3b 0a 20 20 20 20 7a 45 72 72 20 3d 20 73 71 6c  ;.    zErr = sql
1f669 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
1f66a 22 6e 6f 20 73 75 63 68 20 72 6f 77 69 64 3a 20  "no such rowid: 
1f66b 25 6c 6c 64 22 2c 20 69 52 6f 77 29 3b 0a 20 20  %lld", iRow);.  
1f66c 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
1f66d 52 4f 52 3b 0a 20 20 7d 0a 0a 62 6c 6f 62 5f 6f  ROR;.  }..blob_o
1f66e 70 65 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 20 76  pen_out:.  if( v
1f66f 20 26 26 20 28 72 63 21 3d 53 51 4c 49 54 45 5f   && (rc!=SQLITE_
1f670 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  OK || db->malloc
1f671 46 61 69 6c 65 64 29 20 29 7b 0a 20 20 20 20 73  Failed) ){.    s
1f672 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c 69  qlite3VdbeFinali
1f673 7a 65 28 76 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  ze(v);.  }.  sql
1f674 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63  ite3Error(db, rc
1f675 2c 20 7a 45 72 72 29 3b 0a 20 20 73 71 6c 69 74  , zErr);.  sqlit
1f676 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72  e3DbFree(db, zEr
1f677 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 74 61  r);.  sqlite3Sta
1f678 63 6b 46 72 65 65 28 64 62 2c 20 70 50 61 72 73  ckFree(db, pPars
1f679 65 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  e);.  rc = sqlit
1f67a 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63  e3ApiExit(db, rc
1f67b 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
1f67c 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
1f67d 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
1f67e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65  ;.}../*.** Close
1f67f 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 20 74   a blob handle t
1f680 68 61 74 20 77 61 73 20 70 72 65 76 69 6f 75 73  hat was previous
1f681 6c 79 20 63 72 65 61 74 65 64 20 75 73 69 6e 67  ly created using
1f682 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  .** sqlite3_blob
1f683 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 53 51 4c 49  _open()..*/.SQLI
1f684 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
1f685 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 73 71  e3_blob_close(sq
1f686 6c 69 74 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f  lite3_blob *pBlo
1f687 62 29 7b 0a 20 20 49 6e 63 72 62 6c 6f 62 20 2a  b){.  Incrblob *
1f688 70 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a 29  p = (Incrblob *)
1f689 70 42 6c 6f 62 3b 0a 20 20 69 6e 74 20 72 63 3b  pBlob;.  int rc;
1f68a 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
1f68b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
1f68c 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20  db = p->db;.    
1f68d 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1f68e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
1f68f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1f690 5f 66 69 6e 61 6c 69 7a 65 28 70 2d 3e 70 53 74  _finalize(p->pSt
1f691 6d 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  mt);.    sqlite3
1f692 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20  DbFree(db, p);. 
1f693 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
1f694 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
1f695 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1f696 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1f697 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1f698 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72  .}../*.** Perfor
1f699 6d 20 61 20 72 65 61 64 20 6f 72 20 77 72 69 74  m a read or writ
1f69a 65 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20 61  e operation on a
1f69b 20 62 6c 6f 62 0a 2a 2f 0a 73 74 61 74 69 63 20   blob.*/.static 
1f69c 69 6e 74 20 62 6c 6f 62 52 65 61 64 57 72 69 74  int blobReadWrit
1f69d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f  e(.  sqlite3_blo
1f69e 62 20 2a 70 42 6c 6f 62 2c 20 0a 20 20 76 6f 69  b *pBlob, .  voi
1f69f 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20  d *z, .  int n, 
1f6a0 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 2c 20  .  int iOffset, 
1f6a1 0a 20 20 69 6e 74 20 28 2a 78 43 61 6c 6c 29 28  .  int (*xCall)(
1f6a2 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 2c 20  BtCursor*, u32, 
1f6a3 75 33 32 2c 20 76 6f 69 64 2a 29 0a 29 7b 0a 20  u32, void*).){. 
1f6a4 20 69 6e 74 20 72 63 3b 0a 20 20 49 6e 63 72 62   int rc;.  Incrb
1f6a5 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c  lob *p = (Incrbl
1f6a6 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 56 64  ob *)pBlob;.  Vd
1f6a7 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33  be *v;.  sqlite3
1f6a8 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 70 3d 3d   *db;..  if( p==
1f6a9 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  0 ) return SQLIT
1f6aa 45 5f 4d 49 53 55 53 45 3b 0a 20 20 64 62 20 3d  E_MISUSE;.  db =
1f6ab 20 70 2d 3e 64 62 3b 0a 20 20 73 71 6c 69 74 65   p->db;.  sqlite
1f6ac 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
1f6ad 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 76 20 3d 20  ->mutex);.  v = 
1f6ae 28 56 64 62 65 2a 29 70 2d 3e 70 53 74 6d 74 3b  (Vdbe*)p->pStmt;
1f6af 0a 0a 20 20 69 66 28 20 6e 3c 30 20 7c 7c 20 69  ..  if( n<0 || i
1f6b0 4f 66 66 73 65 74 3c 30 20 7c 7c 20 28 69 4f 66  Offset<0 || (iOf
1f6b1 66 73 65 74 2b 6e 29 3e 70 2d 3e 6e 42 79 74 65  fset+n)>p->nByte
1f6b2 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65 71 75 65   ){.    /* Reque
1f6b3 73 74 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e  st is out of ran
1f6b4 67 65 2e 20 52 65 74 75 72 6e 20 61 20 74 72 61  ge. Return a tra
1f6b5 6e 73 69 65 6e 74 20 65 72 72 6f 72 2e 20 2a 2f  nsient error. */
1f6b6 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1f6b7 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69  _ERROR;.    sqli
1f6b8 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c  te3Error(db, SQL
1f6b9 49 54 45 5f 45 52 52 4f 52 2c 20 30 29 3b 0a 20  ITE_ERROR, 0);. 
1f6ba 20 7d 20 65 6c 73 65 20 69 66 28 20 76 3d 3d 30   } else if( v==0
1f6bb 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   ){.    /* If th
1f6bc 65 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d  ere is no statem
1f6bd 65 6e 74 20 68 61 6e 64 6c 65 2c 20 74 68 65 6e  ent handle, then
1f6be 20 74 68 65 20 62 6c 6f 62 2d 68 61 6e 64 6c 65   the blob-handle
1f6bf 20 68 61 73 0a 20 20 20 20 2a 2a 20 61 6c 72 65   has.    ** alre
1f6c0 61 64 79 20 62 65 65 6e 20 69 6e 76 61 6c 69 64  ady been invalid
1f6c1 61 74 65 64 2e 20 52 65 74 75 72 6e 20 53 51 4c  ated. Return SQL
1f6c2 49 54 45 5f 41 42 4f 52 54 20 69 6e 20 74 68 69  ITE_ABORT in thi
1f6c3 73 20 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20  s case..    */. 
1f6c4 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 41     rc = SQLITE_A
1f6c5 42 4f 52 54 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  BORT;.  }else{. 
1f6c6 20 20 20 2f 2a 20 43 61 6c 6c 20 65 69 74 68 65     /* Call eithe
1f6c7 72 20 42 74 72 65 65 44 61 74 61 28 29 20 6f 72  r BtreeData() or
1f6c8 20 42 74 72 65 65 50 75 74 44 61 74 61 28 29 2e   BtreePutData().
1f6c9 20 49 66 20 53 51 4c 49 54 45 5f 41 42 4f 52 54   If SQLITE_ABORT
1f6ca 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 74 75 72   is.    ** retur
1f6cb 6e 65 64 2c 20 63 6c 65 61 6e 2d 75 70 20 74 68  ned, clean-up th
1f6cc 65 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  e statement hand
1f6cd 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  le..    */.    a
1f6ce 73 73 65 72 74 28 20 64 62 20 3d 3d 20 76 2d 3e  ssert( db == v->
1f6cf 64 62 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  db );.    sqlite
1f6d0 33 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f  3BtreeEnterCurso
1f6d1 72 28 70 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20  r(p->pCsr);.    
1f6d2 72 63 20 3d 20 78 43 61 6c 6c 28 70 2d 3e 70 43  rc = xCall(p->pC
1f6d3 73 72 2c 20 69 4f 66 66 73 65 74 2b 70 2d 3e 69  sr, iOffset+p->i
1f6d4 4f 66 66 73 65 74 2c 20 6e 2c 20 7a 29 3b 0a 20  Offset, n, z);. 
1f6d5 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
1f6d6 65 61 76 65 43 75 72 73 6f 72 28 70 2d 3e 70 43  eaveCursor(p->pC
1f6d7 73 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  sr);.    if( rc=
1f6d8 3d 53 51 4c 49 54 45 5f 41 42 4f 52 54 20 29 7b  =SQLITE_ABORT ){
1f6d9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1f6da 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a 20  beFinalize(v);. 
1f6db 20 20 20 20 20 70 2d 3e 70 53 74 6d 74 20 3d 20       p->pStmt = 
1f6dc 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  0;.    }else{.  
1f6dd 20 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20      db->errCode 
1f6de 3d 20 72 63 3b 0a 20 20 20 20 20 20 76 2d 3e 72  = rc;.      v->r
1f6df 63 20 3d 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  c = rc;.    }.  
1f6e0 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  }.  rc = sqlite3
1f6e1 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b  ApiExit(db, rc);
1f6e2 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
1f6e3 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
1f6e4 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
1f6e5 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61  }../*.** Read da
1f6e6 74 61 20 66 72 6f 6d 20 61 20 62 6c 6f 62 20 68  ta from a blob h
1f6e7 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  andle..*/.SQLITE
1f6e8 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
1f6e9 5f 62 6c 6f 62 5f 72 65 61 64 28 73 71 6c 69 74  _blob_read(sqlit
1f6ea 65 33 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20  e3_blob *pBlob, 
1f6eb 76 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20  void *z, int n, 
1f6ec 69 6e 74 20 69 4f 66 66 73 65 74 29 7b 0a 20 20  int iOffset){.  
1f6ed 72 65 74 75 72 6e 20 62 6c 6f 62 52 65 61 64 57  return blobReadW
1f6ee 72 69 74 65 28 70 42 6c 6f 62 2c 20 7a 2c 20 6e  rite(pBlob, z, n
1f6ef 2c 20 69 4f 66 66 73 65 74 2c 20 73 71 6c 69 74  , iOffset, sqlit
1f6f0 65 33 42 74 72 65 65 44 61 74 61 29 3b 0a 7d 0a  e3BtreeData);.}.
1f6f1 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74  ./*.** Write dat
1f6f2 61 20 74 6f 20 61 20 62 6c 6f 62 20 68 61 6e 64  a to a blob hand
1f6f3 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  le..*/.SQLITE_AP
1f6f4 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
1f6f5 6f 62 5f 77 72 69 74 65 28 73 71 6c 69 74 65 33  ob_write(sqlite3
1f6f6 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 2c 20 63 6f  _blob *pBlob, co
1f6f7 6e 73 74 20 76 6f 69 64 20 2a 7a 2c 20 69 6e 74  nst void *z, int
1f6f8 20 6e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29   n, int iOffset)
1f6f9 7b 0a 20 20 72 65 74 75 72 6e 20 62 6c 6f 62 52  {.  return blobR
1f6fa 65 61 64 57 72 69 74 65 28 70 42 6c 6f 62 2c 20  eadWrite(pBlob, 
1f6fb 28 76 6f 69 64 20 2a 29 7a 2c 20 6e 2c 20 69 4f  (void *)z, n, iO
1f6fc 66 66 73 65 74 2c 20 73 71 6c 69 74 65 33 42 74  ffset, sqlite3Bt
1f6fd 72 65 65 50 75 74 44 61 74 61 29 3b 0a 7d 0a 0a  reePutData);.}..
1f6fe 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 61 20 62 6c  /*.** Query a bl
1f6ff 6f 62 20 68 61 6e 64 6c 65 20 66 6f 72 20 74 68  ob handle for th
1f700 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61  e size of the da
1f701 74 61 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e  ta..**.** The In
1f702 63 72 62 6c 6f 62 2e 6e 42 79 74 65 20 66 69 65  crblob.nByte fie
1f703 6c 64 20 69 73 20 66 69 78 65 64 20 66 6f 72 20  ld is fixed for 
1f704 74 68 65 20 6c 69 66 65 74 69 6d 65 20 6f 66 20  the lifetime of 
1f705 74 68 65 20 49 6e 63 72 62 6c 6f 62 0a 2a 2a 20  the Incrblob.** 
1f706 73 6f 20 6e 6f 20 6d 75 74 65 78 20 69 73 20 72  so no mutex is r
1f707 65 71 75 69 72 65 64 20 66 6f 72 20 61 63 63 65  equired for acce
1f708 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ss..*/.SQLITE_AP
1f709 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c  I int sqlite3_bl
1f70a 6f 62 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33  ob_bytes(sqlite3
1f70b 5f 62 6c 6f 62 20 2a 70 42 6c 6f 62 29 7b 0a 20  _blob *pBlob){. 
1f70c 20 49 6e 63 72 62 6c 6f 62 20 2a 70 20 3d 20 28   Incrblob *p = (
1f70d 49 6e 63 72 62 6c 6f 62 20 2a 29 70 42 6c 6f 62  Incrblob *)pBlob
1f70e 3b 0a 20 20 72 65 74 75 72 6e 20 70 20 3f 20 70  ;.  return p ? p
1f70f 2d 3e 6e 42 79 74 65 20 3a 20 30 3b 0a 7d 0a 0a  ->nByte : 0;.}..
1f710 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65  #endif /* #ifnde
1f711 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e  f SQLITE_OMIT_IN
1f712 43 52 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  CRBLOB */../****
1f713 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
1f714 66 20 76 64 62 65 62 6c 6f 62 2e 63 20 2a 2a 2a  f vdbeblob.c ***
1f715 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f716 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f717 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
1f718 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
1f719 20 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63 20   file journal.c 
1f71a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f71b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f71c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
1f71d 20 32 30 30 37 20 41 75 67 75 73 74 20 32 32 0a   2007 August 22.
1f71e 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
1f71f 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
1f720 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
1f721 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
1f722 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
1f723 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
1f724 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
1f725 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
1f726 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
1f727 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
1f728 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
1f729 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
1f72a 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
1f72b 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
1f72c 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
1f72d 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
1f72e 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
1f72f 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
1f730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f731 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f732 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f733 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
1f734 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 6a  *.** @(#) $Id: j
1f735 6f 75 72 6e 61 6c 2e 63 2c 76 20 31 2e 39 20 32  ournal.c,v 1.9 2
1f736 30 30 39 2f 30 31 2f 32 30 20 31 37 3a 30 36 3a  009/01/20 17:06:
1f737 32 37 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  27 danielk1977 E
1f738 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 64 65 66 20  xp $.*/..#ifdef 
1f739 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 54  SQLITE_ENABLE_AT
1f73a 4f 4d 49 43 5f 57 52 49 54 45 0a 0a 2f 2a 0a 2a  OMIC_WRITE../*.*
1f73b 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c  * This file impl
1f73c 65 6d 65 6e 74 73 20 61 20 73 70 65 63 69 61 6c  ements a special
1f73d 20 6b 69 6e 64 20 6f 66 20 73 71 6c 69 74 65 33   kind of sqlite3
1f73e 5f 66 69 6c 65 20 6f 62 6a 65 63 74 20 75 73 65  _file object use
1f73f 64 0a 2a 2a 20 62 79 20 53 51 4c 69 74 65 20 74  d.** by SQLite t
1f740 6f 20 63 72 65 61 74 65 20 6a 6f 75 72 6e 61 6c  o create journal
1f741 20 66 69 6c 65 73 20 69 66 20 74 68 65 20 61 74   files if the at
1f742 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d  omic-write optim
1f743 69 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 65 6e  ization.** is en
1f744 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  abled..**.** The
1f745 20 64 69 73 74 69 6e 63 74 69 76 65 20 63 68 61   distinctive cha
1f746 72 61 63 74 65 72 69 73 74 69 63 20 6f 66 20 74  racteristic of t
1f747 68 69 73 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  his sqlite3_file
1f748 20 69 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20   is that the.** 
1f749 61 63 74 75 61 6c 20 6f 6e 20 64 69 73 6b 20 66  actual on disk f
1f74a 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 20 6c  ile is created l
1f74b 61 7a 69 6c 79 2e 20 57 68 65 6e 20 74 68 65 20  azily. When the 
1f74c 66 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 2c  file is created,
1f74d 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 65 72 20 73  .** the caller s
1f74e 70 65 63 69 66 69 65 73 20 61 20 62 75 66 66 65  pecifies a buffe
1f74f 72 20 73 69 7a 65 20 66 6f 72 20 61 6e 20 69 6e  r size for an in
1f750 2d 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74  -memory buffer t
1f751 6f 0a 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20  o.** be used to 
1f752 73 65 72 76 69 63 65 20 72 65 61 64 28 29 20 61  service read() a
1f753 6e 64 20 77 72 69 74 65 28 29 20 72 65 71 75 65  nd write() reque
1f754 73 74 73 2e 20 54 68 65 20 61 63 74 75 61 6c 20  sts. The actual 
1f755 66 69 6c 65 0a 2a 2a 20 6f 6e 20 64 69 73 6b 20  file.** on disk 
1f756 69 73 20 6e 6f 74 20 63 72 65 61 74 65 64 20 6f  is not created o
1f757 72 20 70 6f 70 75 6c 61 74 65 64 20 75 6e 74 69  r populated unti
1f758 6c 20 65 69 74 68 65 72 3a 0a 2a 2a 0a 2a 2a 20  l either:.**.** 
1f759 20 20 31 29 20 54 68 65 20 69 6e 2d 6d 65 6d 6f    1) The in-memo
1f75a 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  ry representatio
1f75b 6e 20 67 72 6f 77 73 20 74 6f 6f 20 6c 61 72 67  n grows too larg
1f75c 65 20 66 6f 72 20 74 68 65 20 61 6c 6c 6f 63 61  e for the alloca
1f75d 74 65 64 20 0a 2a 2a 20 20 20 20 20 20 62 75 66  ted .**      buf
1f75e 66 65 72 2c 20 6f 72 0a 2a 2a 20 20 20 32 29 20  fer, or.**   2) 
1f75f 54 68 65 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e  The sqlite3Journ
1f760 61 6c 43 72 65 61 74 65 28 29 20 66 75 6e 63 74  alCreate() funct
1f761 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a  ion is called..*
1f762 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 41 20 4a 6f 75  /..../*.** A Jou
1f763 72 6e 61 6c 46 69 6c 65 20 6f 62 6a 65 63 74 20  rnalFile object 
1f764 69 73 20 61 20 73 75 62 63 6c 61 73 73 20 6f 66  is a subclass of
1f765 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 75 73   sqlite3_file us
1f766 65 64 20 62 79 0a 2a 2a 20 61 73 20 61 6e 20 6f  ed by.** as an o
1f767 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 20  pen file handle 
1f768 66 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  for journal file
1f769 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4a 6f 75  s..*/.struct Jou
1f76a 72 6e 61 6c 46 69 6c 65 20 7b 0a 20 20 73 71 6c  rnalFile {.  sql
1f76b 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
1f76c 2a 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20  *pMethod;    /* 
1f76d 49 2f 4f 20 6d 65 74 68 6f 64 73 20 6f 6e 20 6a  I/O methods on j
1f76e 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20 2a 2f 0a  ournal files */.
1f76f 20 20 69 6e 74 20 6e 42 75 66 3b 20 20 20 20 20    int nBuf;     
1f770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f771 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 42 75    /* Size of zBu
1f772 66 5b 5d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  f[] in bytes */.
1f773 20 20 63 68 61 72 20 2a 7a 42 75 66 3b 20 20 20    char *zBuf;   
1f774 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f775 20 20 2f 2a 20 53 70 61 63 65 20 74 6f 20 62 75    /* Space to bu
1f776 66 66 65 72 20 6a 6f 75 72 6e 61 6c 20 77 72 69  ffer journal wri
1f777 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 53 69  tes */.  int iSi
1f778 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
1f779 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75           /* Amou
1f77a 6e 74 20 6f 66 20 7a 42 75 66 5b 5d 20 63 75 72  nt of zBuf[] cur
1f77b 72 65 6e 74 6c 79 20 75 73 65 64 20 2a 2f 0a 20  rently used */. 
1f77c 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20   int flags;     
1f77d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f77e 20 2f 2a 20 78 4f 70 65 6e 20 66 6c 61 67 73 20   /* xOpen flags 
1f77f 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  */.  sqlite3_vfs
1f780 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20   *pVfs;         
1f781 20 20 20 20 20 2f 2a 20 54 68 65 20 22 72 65 61       /* The "rea
1f782 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 20 56 46  l" underlying VF
1f783 53 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66  S */.  sqlite3_f
1f784 69 6c 65 20 2a 70 52 65 61 6c 3b 20 20 20 20 20  ile *pReal;     
1f785 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 22 72         /* The "r
1f786 65 61 6c 22 20 75 6e 64 65 72 6c 79 69 6e 67 20  eal" underlying 
1f787 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
1f788 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1f789 2a 7a 4a 6f 75 72 6e 61 6c 3b 20 20 20 20 20 20  *zJournal;      
1f78a 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
1f78b 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
1f78c 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73   */.};.typedef s
1f78d 74 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46 69 6c  truct JournalFil
1f78e 65 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 3b 0a 0a  e JournalFile;..
1f78f 2f 2a 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73  /*.** If it does
1f790 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69   not already exi
1f791 73 74 73 2c 20 63 72 65 61 74 65 20 61 6e 64 20  sts, create and 
1f792 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6f 6e 2d  populate the on-
1f793 64 69 73 6b 20 66 69 6c 65 20 0a 2a 2a 20 66 6f  disk file .** fo
1f794 72 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 70 2e  r JournalFile p.
1f795 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
1f796 72 65 61 74 65 46 69 6c 65 28 4a 6f 75 72 6e 61  reateFile(Journa
1f797 6c 46 69 6c 65 20 2a 70 29 7b 0a 20 20 69 6e 74  lFile *p){.  int
1f798 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
1f799 0a 20 20 69 66 28 20 21 70 2d 3e 70 52 65 61 6c  .  if( !p->pReal
1f79a 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
1f79b 66 69 6c 65 20 2a 70 52 65 61 6c 20 3d 20 28 73  file *pReal = (s
1f79c 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 26 70  qlite3_file *)&p
1f79d 5b 31 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  [1];.    rc = sq
1f79e 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 2d 3e 70  lite3OsOpen(p->p
1f79f 56 66 73 2c 20 70 2d 3e 7a 4a 6f 75 72 6e 61 6c  Vfs, p->zJournal
1f7a0 2c 20 70 52 65 61 6c 2c 20 70 2d 3e 66 6c 61 67  , pReal, p->flag
1f7a1 73 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72  s, 0);.    if( r
1f7a2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1f7a3 20 20 20 20 20 20 70 2d 3e 70 52 65 61 6c 20 3d        p->pReal =
1f7a4 20 70 52 65 61 6c 3b 0a 20 20 20 20 20 20 69 66   pReal;.      if
1f7a5 28 20 70 2d 3e 69 53 69 7a 65 3e 30 20 29 7b 0a  ( p->iSize>0 ){.
1f7a6 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70          assert(p
1f7a7 2d 3e 69 53 69 7a 65 3c 3d 70 2d 3e 6e 42 75 66  ->iSize<=p->nBuf
1f7a8 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
1f7a9 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70  sqlite3OsWrite(p
1f7aa 2d 3e 70 52 65 61 6c 2c 20 70 2d 3e 7a 42 75 66  ->pReal, p->zBuf
1f7ab 2c 20 70 2d 3e 69 53 69 7a 65 2c 20 30 29 3b 0a  , p->iSize, 0);.
1f7ac 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1f7ad 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
1f7ae 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68  ../*.** Close th
1f7af 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  e file..*/.stati
1f7b0 63 20 69 6e 74 20 6a 72 6e 6c 43 6c 6f 73 65 28  c int jrnlClose(
1f7b1 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
1f7b2 66 64 29 7b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69  fd){.  JournalFi
1f7b3 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c  le *p = (Journal
1f7b4 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69  File *)pJfd;.  i
1f7b5 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20  f( p->pReal ){. 
1f7b6 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73     sqlite3OsClos
1f7b7 65 28 70 2d 3e 70 52 65 61 6c 29 3b 0a 20 20 7d  e(p->pReal);.  }
1f7b8 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
1f7b9 70 2d 3e 7a 42 75 66 29 3b 0a 20 20 72 65 74 75  p->zBuf);.  retu
1f7ba 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
1f7bb 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61  ./*.** Read data
1f7bc 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 2e 0a   from the file..
1f7bd 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72  */.static int jr
1f7be 6e 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65  nlRead(.  sqlite
1f7bf 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20  3_file *pJfd,   
1f7c0 20 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20   /* The journal 
1f7c1 66 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20  file from which 
1f7c2 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69  to read */.  voi
1f7c3 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 20  d *zBuf,        
1f7c4 20 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 72      /* Put the r
1f7c5 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20  esults here */. 
1f7c6 20 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20   int iAmt,      
1f7c7 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1f7c8 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65  r of bytes to re
1f7c9 61 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69  ad */.  sqlite_i
1f7ca 6e 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f  nt64 iOfst     /
1f7cb 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20  * Begin reading 
1f7cc 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a  at this offset *
1f7cd 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  /.){.  int rc = 
1f7ce 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75  SQLITE_OK;.  Jou
1f7cf 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a  rnalFile *p = (J
1f7d0 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66  ournalFile *)pJf
1f7d1 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61  d;.  if( p->pRea
1f7d2 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  l ){.    rc = sq
1f7d3 6c 69 74 65 33 4f 73 52 65 61 64 28 70 2d 3e 70  lite3OsRead(p->p
1f7d4 52 65 61 6c 2c 20 7a 42 75 66 2c 20 69 41 6d 74  Real, zBuf, iAmt
1f7d5 2c 20 69 4f 66 73 74 29 3b 0a 20 20 7d 65 6c 73  , iOfst);.  }els
1f7d6 65 20 69 66 28 20 28 69 41 6d 74 2b 69 4f 66 73  e if( (iAmt+iOfs
1f7d7 74 29 3e 70 2d 3e 69 53 69 7a 65 20 29 7b 0a 20  t)>p->iSize ){. 
1f7d8 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 49     rc = SQLITE_I
1f7d9 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b  OERR_SHORT_READ;
1f7da 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6d 65  .  }else{.    me
1f7db 6d 63 70 79 28 7a 42 75 66 2c 20 26 70 2d 3e 7a  mcpy(zBuf, &p->z
1f7dc 42 75 66 5b 69 4f 66 73 74 5d 2c 20 69 41 6d 74  Buf[iOfst], iAmt
1f7dd 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1f7de 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69  rc;.}../*.** Wri
1f7df 74 65 20 64 61 74 61 20 74 6f 20 74 68 65 20 66  te data to the f
1f7e0 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ile..*/.static i
1f7e1 6e 74 20 6a 72 6e 6c 57 72 69 74 65 28 0a 20 20  nt jrnlWrite(.  
1f7e2 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
1f7e3 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f  fd,    /* The jo
1f7e4 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20  urnal file into 
1f7e5 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 2a  which to write *
1f7e6 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
1f7e7 7a 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54 61  zBuf,      /* Ta
1f7e8 6b 65 20 64 61 74 61 20 74 6f 20 62 65 20 77 72  ke data to be wr
1f7e9 69 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65 20  itten from here 
1f7ea 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20  */.  int iAmt,  
1f7eb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1f7ec 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
1f7ed 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c  o write */.  sql
1f7ee 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20  ite_int64 iOfst 
1f7ef 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72 69      /* Begin wri
1f7f0 74 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66  ting at this off
1f7f1 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c  set into the fil
1f7f2 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  e */.){.  int rc
1f7f3 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1f7f4 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d  JournalFile *p =
1f7f5 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29   (JournalFile *)
1f7f6 70 4a 66 64 3b 0a 20 20 69 66 28 20 21 70 2d 3e  pJfd;.  if( !p->
1f7f7 70 52 65 61 6c 20 26 26 20 28 69 4f 66 73 74 2b  pReal && (iOfst+
1f7f8 69 41 6d 74 29 3e 70 2d 3e 6e 42 75 66 20 29 7b  iAmt)>p->nBuf ){
1f7f9 0a 20 20 20 20 72 63 20 3d 20 63 72 65 61 74 65  .    rc = create
1f7fa 46 69 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 20 69  File(p);.  }.  i
1f7fb 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1f7fc 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70   ){.    if( p->p
1f7fd 52 65 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63  Real ){.      rc
1f7fe 20 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74   = sqlite3OsWrit
1f7ff 65 28 70 2d 3e 70 52 65 61 6c 2c 20 7a 42 75 66  e(p->pReal, zBuf
1f800 2c 20 69 41 6d 74 2c 20 69 4f 66 73 74 29 3b 0a  , iAmt, iOfst);.
1f801 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1f802 20 6d 65 6d 63 70 79 28 26 70 2d 3e 7a 42 75 66   memcpy(&p->zBuf
1f803 5b 69 4f 66 73 74 5d 2c 20 7a 42 75 66 2c 20 69  [iOfst], zBuf, i
1f804 41 6d 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  Amt);.      if( 
1f805 70 2d 3e 69 53 69 7a 65 3c 28 69 4f 66 73 74 2b  p->iSize<(iOfst+
1f806 69 41 6d 74 29 20 29 7b 0a 20 20 20 20 20 20 20  iAmt) ){.       
1f807 20 70 2d 3e 69 53 69 7a 65 20 3d 20 28 69 4f 66   p->iSize = (iOf
1f808 73 74 2b 69 41 6d 74 29 3b 0a 20 20 20 20 20 20  st+iAmt);.      
1f809 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
1f80a 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1f80b 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 66  * Truncate the f
1f80c 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ile..*/.static i
1f80d 6e 74 20 6a 72 6e 6c 54 72 75 6e 63 61 74 65 28  nt jrnlTruncate(
1f80e 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
1f80f 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  fd, sqlite_int64
1f810 20 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63   size){.  int rc
1f811 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1f812 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d  JournalFile *p =
1f813 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29   (JournalFile *)
1f814 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70  pJfd;.  if( p->p
1f815 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d  Real ){.    rc =
1f816 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61   sqlite3OsTrunca
1f817 74 65 28 70 2d 3e 70 52 65 61 6c 2c 20 73 69 7a  te(p->pReal, siz
1f818 65 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  e);.  }else if( 
1f819 73 69 7a 65 3c 70 2d 3e 69 53 69 7a 65 20 29 7b  size<p->iSize ){
1f81a 0a 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d 20  .    p->iSize = 
1f81b 73 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75  size;.  }.  retu
1f81c 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1f81d 53 79 6e 63 20 74 68 65 20 66 69 6c 65 2e 0a 2a  Sync the file..*
1f81e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e  /.static int jrn
1f81f 6c 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69  lSync(sqlite3_fi
1f820 6c 65 20 2a 70 4a 66 64 2c 20 69 6e 74 20 66 6c  le *pJfd, int fl
1f821 61 67 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  ags){.  int rc;.
1f822 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70    JournalFile *p
1f823 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20   = (JournalFile 
1f824 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d  *)pJfd;.  if( p-
1f825 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63  >pReal ){.    rc
1f826 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63   = sqlite3OsSync
1f827 28 70 2d 3e 70 52 65 61 6c 2c 20 66 6c 61 67 73  (p->pReal, flags
1f828 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
1f829 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1f82a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1f82b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20  .}../*.** Query 
1f82c 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
1f82d 66 69 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a 2a  file in bytes..*
1f82e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72 6e  /.static int jrn
1f82f 6c 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74 65  lFileSize(sqlite
1f830 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71  3_file *pJfd, sq
1f831 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 53 69 7a  lite_int64 *pSiz
1f832 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  e){.  int rc = S
1f833 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 4a 6f 75 72  QLITE_OK;.  Jour
1f834 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a 6f  nalFile *p = (Jo
1f835 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66 64  urnalFile *)pJfd
1f836 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52 65 61 6c  ;.  if( p->pReal
1f837 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
1f838 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70  ite3OsFileSize(p
1f839 2d 3e 70 52 65 61 6c 2c 20 70 53 69 7a 65 29 3b  ->pReal, pSize);
1f83a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70  .  }else{.    *p
1f83b 53 69 7a 65 20 3d 20 28 73 71 6c 69 74 65 5f 69  Size = (sqlite_i
1f83c 6e 74 36 34 29 20 70 2d 3e 69 53 69 7a 65 3b 0a  nt64) p->iSize;.
1f83d 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1f83e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20  .}../*.** Table 
1f83f 6f 66 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 4a  of methods for J
1f840 6f 75 72 6e 61 6c 46 69 6c 65 20 73 71 6c 69 74  ournalFile sqlit
1f841 65 33 5f 66 69 6c 65 20 6f 62 6a 65 63 74 2e 0a  e3_file object..
1f842 2a 2f 0a 73 74 61 74 69 63 20 73 74 72 75 63 74  */.static struct
1f843 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
1f844 6f 64 73 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d  ods JournalFileM
1f845 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 31 2c 20  ethods = {.  1, 
1f846 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69              /* i
1f847 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6a 72 6e  Version */.  jrn
1f848 6c 43 6c 6f 73 65 2c 20 20 20 20 20 2f 2a 20 78  lClose,     /* x
1f849 43 6c 6f 73 65 20 2a 2f 0a 20 20 6a 72 6e 6c 52  Close */.  jrnlR
1f84a 65 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65  ead,      /* xRe
1f84b 61 64 20 2a 2f 0a 20 20 6a 72 6e 6c 57 72 69 74  ad */.  jrnlWrit
1f84c 65 2c 20 20 20 20 20 2f 2a 20 78 57 72 69 74 65  e,     /* xWrite
1f84d 20 2a 2f 0a 20 20 6a 72 6e 6c 54 72 75 6e 63 61   */.  jrnlTrunca
1f84e 74 65 2c 20 20 2f 2a 20 78 54 72 75 6e 63 61 74  te,  /* xTruncat
1f84f 65 20 2a 2f 0a 20 20 6a 72 6e 6c 53 79 6e 63 2c  e */.  jrnlSync,
1f850 20 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a        /* xSync *
1f851 2f 0a 20 20 6a 72 6e 6c 46 69 6c 65 53 69 7a 65  /.  jrnlFileSize
1f852 2c 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65 20  ,  /* xFileSize 
1f853 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20  */.  0,         
1f854 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a      /* xLock */.
1f855 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
1f856 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a 20   /* xUnlock */. 
1f857 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
1f858 2f 2a 20 78 43 68 65 63 6b 52 65 73 65 72 76 65  /* xCheckReserve
1f859 64 4c 6f 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20  dLock */.  0,   
1f85a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69            /* xFi
1f85b 6c 65 43 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 30  leControl */.  0
1f85c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
1f85d 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 0a   xSectorSize */.
1f85e 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20    0             
1f85f 20 2f 2a 20 78 44 65 76 69 63 65 43 68 61 72 61   /* xDeviceChara
1f860 63 74 65 72 69 73 74 69 63 73 20 2a 2f 0a 7d 3b  cteristics */.};
1f861 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20  ../* .** Open a 
1f862 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f  journal file..*/
1f863 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1f864 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e  int sqlite3Journ
1f865 61 6c 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65  alOpen(.  sqlite
1f866 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20 20  3_vfs *pVfs,    
1f867 20 20 20 20 20 2f 2a 20 54 68 65 20 56 46 53 20       /* The VFS 
1f868 74 6f 20 75 73 65 20 66 6f 72 20 61 63 74 75 61  to use for actua
1f869 6c 20 66 69 6c 65 20 49 2f 4f 20 2a 2f 0a 20 20  l file I/O */.  
1f86a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
1f86b 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  e,         /* Na
1f86c 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  me of the journa
1f86d 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 73 71 6c 69  l file */.  sqli
1f86e 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20  te3_file *pJfd, 
1f86f 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c         /* Preall
1f870 6f 63 61 74 65 64 2c 20 62 6c 61 6e 6b 20 66 69  ocated, blank fi
1f871 6c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69  le handle */.  i
1f872 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nt flags,       
1f873 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65            /* Ope
1f874 6e 69 6e 67 20 66 6c 61 67 73 20 2a 2f 0a 20 20  ning flags */.  
1f875 69 6e 74 20 6e 42 75 66 20 20 20 20 20 20 20 20  int nBuf        
1f876 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79             /* By
1f877 74 65 73 20 62 75 66 66 65 72 65 64 20 62 65 66  tes buffered bef
1f878 6f 72 65 20 6f 70 65 6e 69 6e 67 20 74 68 65 20  ore opening the 
1f879 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 4a 6f 75  file */.){.  Jou
1f87a 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20 28 4a  rnalFile *p = (J
1f87b 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70 4a 66  ournalFile *)pJf
1f87c 64 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30  d;.  memset(p, 0
1f87d 2c 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c  , sqlite3Journal
1f87e 53 69 7a 65 28 70 56 66 73 29 29 3b 0a 20 20 69  Size(pVfs));.  i
1f87f 66 28 20 6e 42 75 66 3e 30 20 29 7b 0a 20 20 20  f( nBuf>0 ){.   
1f880 20 70 2d 3e 7a 42 75 66 20 3d 20 73 71 6c 69 74   p->zBuf = sqlit
1f881 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 75  e3MallocZero(nBu
1f882 66 29 3b 0a 20 20 20 20 69 66 28 20 21 70 2d 3e  f);.    if( !p->
1f883 7a 42 75 66 20 29 7b 0a 20 20 20 20 20 20 72 65  zBuf ){.      re
1f884 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
1f885 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  M;.    }.  }else
1f886 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  {.    return sql
1f887 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c  ite3OsOpen(pVfs,
1f888 20 7a 4e 61 6d 65 2c 20 70 4a 66 64 2c 20 66 6c   zName, pJfd, fl
1f889 61 67 73 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70  ags, 0);.  }.  p
1f88a 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4a 6f 75  ->pMethod = &Jou
1f88b 72 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73 3b  rnalFileMethods;
1f88c 0a 20 20 70 2d 3e 6e 42 75 66 20 3d 20 6e 42 75  .  p->nBuf = nBu
1f88d 66 3b 0a 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20  f;.  p->flags = 
1f88e 66 6c 61 67 73 3b 0a 20 20 70 2d 3e 7a 4a 6f 75  flags;.  p->zJou
1f88f 72 6e 61 6c 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20  rnal = zName;.  
1f890 70 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0a  p->pVfs = pVfs;.
1f891 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1f892 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  OK;.}../*.** If 
1f893 74 68 65 20 61 72 67 75 6d 65 6e 74 20 70 20 70  the argument p p
1f894 6f 69 6e 74 73 20 74 6f 20 61 20 4a 6f 75 72 6e  oints to a Journ
1f895 61 6c 46 69 6c 65 20 73 74 72 75 63 74 75 72 65  alFile structure
1f896 2c 20 61 6e 64 20 74 68 65 20 75 6e 64 65 72 6c  , and the underl
1f897 79 69 6e 67 0a 2a 2a 20 66 69 6c 65 20 68 61 73  ying.** file has
1f898 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 63 72   not yet been cr
1f899 65 61 74 65 64 2c 20 63 72 65 61 74 65 20 69 74  eated, create it
1f89a 20 6e 6f 77 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   now..*/.SQLITE_
1f89b 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1f89c 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61 74 65  te3JournalCreate
1f89d 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70  (sqlite3_file *p
1f89e 29 7b 0a 20 20 69 66 28 20 70 2d 3e 70 4d 65 74  ){.  if( p->pMet
1f89f 68 6f 64 73 21 3d 26 4a 6f 75 72 6e 61 6c 46 69  hods!=&JournalFi
1f8a0 6c 65 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20  leMethods ){.   
1f8a1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
1f8a2 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  K;.  }.  return 
1f8a3 63 72 65 61 74 65 46 69 6c 65 28 28 4a 6f 75 72  createFile((Jour
1f8a4 6e 61 6c 46 69 6c 65 20 2a 29 70 29 3b 0a 7d 0a  nalFile *)p);.}.
1f8a5 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ./* .** Return t
1f8a6 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  he number of byt
1f8a7 65 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 73  es required to s
1f8a8 74 6f 72 65 20 61 20 4a 6f 75 72 6e 61 6c 46 69  tore a JournalFi
1f8a9 6c 65 20 74 68 61 74 20 75 73 65 73 20 76 66 73  le that uses vfs
1f8aa 0a 2a 2a 20 70 56 66 73 20 74 6f 20 63 72 65 61  .** pVfs to crea
1f8ab 74 65 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e  te the underlyin
1f8ac 67 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65 73 2e  g on-disk files.
1f8ad 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1f8ae 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4a 6f  TE int sqlite3Jo
1f8af 75 72 6e 61 6c 53 69 7a 65 28 73 71 6c 69 74 65  urnalSize(sqlite
1f8b0 33 5f 76 66 73 20 2a 70 56 66 73 29 7b 0a 20 20  3_vfs *pVfs){.  
1f8b1 72 65 74 75 72 6e 20 28 70 56 66 73 2d 3e 73 7a  return (pVfs->sz
1f8b2 4f 73 46 69 6c 65 2b 73 69 7a 65 6f 66 28 4a 6f  OsFile+sizeof(Jo
1f8b3 75 72 6e 61 6c 46 69 6c 65 29 29 3b 0a 7d 0a 23  urnalFile));.}.#
1f8b4 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  endif../********
1f8b5 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6a 6f  ****** End of jo
1f8b6 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  urnal.c ********
1f8b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8b9 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
1f8ba 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
1f8bb 65 20 6d 65 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a  e memjournal.c *
1f8bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8be 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
1f8bf 38 20 4f 63 74 6f 62 65 72 20 37 0a 2a 2a 0a 2a  8 October 7.**.*
1f8c0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
1f8c1 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
1f8c2 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
1f8c3 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
1f8c4 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
1f8c5 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
1f8c6 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
1f8c7 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
1f8c8 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
1f8c9 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
1f8ca 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
1f8cb 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
1f8cc 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
1f8cd 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
1f8ce 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
1f8cf 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
1f8d0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
1f8d1 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
1f8d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1f8d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
1f8d6 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
1f8d7 69 6e 73 20 63 6f 64 65 20 75 73 65 20 74 6f 20  ins code use to 
1f8d8 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 20 69 6e 2d  implement an in-
1f8d9 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61 63 6b 20  memory rollback 
1f8da 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20  journal..** The 
1f8db 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c 62 61  in-memory rollba
1f8dc 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 75 73  ck journal is us
1f8dd 65 64 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 74 72  ed to journal tr
1f8de 61 6e 73 61 63 74 69 6f 6e 73 20 66 6f 72 0a 2a  ansactions for.*
1f8df 2a 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 64 61 74  * ":memory:" dat
1f8e0 61 62 61 73 65 73 20 61 6e 64 20 77 68 65 6e 20  abases and when 
1f8e1 74 68 65 20 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65  the journal_mode
1f8e2 3d 4d 45 4d 4f 52 59 20 70 72 61 67 6d 61 20 69  =MEMORY pragma i
1f8e3 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 40 28  s used..**.** @(
1f8e4 23 29 20 24 49 64 3a 20 6d 65 6d 6a 6f 75 72 6e  #) $Id: memjourn
1f8e5 61 6c 2e 63 2c 76 20 31 2e 31 32 20 32 30 30 39  al.c,v 1.12 2009
1f8e6 2f 30 35 2f 30 34 20 31 31 3a 34 32 3a 33 30 20  /05/04 11:42:30 
1f8e7 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
1f8e8 24 0a 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  $.*/../* Forward
1f8e9 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 69   references to i
1f8ea 6e 74 65 72 6e 61 6c 20 73 74 72 75 63 74 75 72  nternal structur
1f8eb 65 73 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  es */.typedef st
1f8ec 72 75 63 74 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20  ruct MemJournal 
1f8ed 4d 65 6d 4a 6f 75 72 6e 61 6c 3b 0a 74 79 70 65  MemJournal;.type
1f8ee 64 65 66 20 73 74 72 75 63 74 20 46 69 6c 65 50  def struct FileP
1f8ef 6f 69 6e 74 20 46 69 6c 65 50 6f 69 6e 74 3b 0a  oint FilePoint;.
1f8f0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46  typedef struct F
1f8f1 69 6c 65 43 68 75 6e 6b 20 46 69 6c 65 43 68 75  ileChunk FileChu
1f8f2 6e 6b 3b 0a 0a 2f 2a 20 53 70 61 63 65 20 74 6f  nk;../* Space to
1f8f3 20 68 6f 6c 64 20 74 68 65 20 72 6f 6c 6c 62 61   hold the rollba
1f8f4 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6c  ck journal is al
1f8f5 6c 6f 63 61 74 65 64 20 69 6e 20 69 6e 63 72 65  located in incre
1f8f6 6d 65 6e 74 73 20 6f 66 0a 2a 2a 20 74 68 69 73  ments of.** this
1f8f7 20 6d 61 6e 79 20 62 79 74 65 73 2e 0a 2a 2a 0a   many bytes..**.
1f8f8 2a 2a 20 54 68 65 20 73 69 7a 65 20 63 68 6f 73  ** The size chos
1f8f9 65 6e 20 69 73 20 61 20 6c 69 74 74 6c 65 20 6c  en is a little l
1f8fa 65 73 73 20 74 68 61 6e 20 61 20 70 6f 77 65 72  ess than a power
1f8fb 20 6f 66 20 74 77 6f 2e 20 20 54 68 61 74 20 77   of two.  That w
1f8fc 61 79 2c 0a 2a 2a 20 74 68 65 20 46 69 6c 65 43  ay,.** the FileC
1f8fd 68 75 6e 6b 20 6f 62 6a 65 63 74 20 77 69 6c 6c  hunk object will
1f8fe 20 68 61 76 65 20 61 20 73 69 7a 65 20 74 68 61   have a size tha
1f8ff 74 20 61 6c 6d 6f 73 74 20 65 78 61 63 74 6c 79  t almost exactly
1f900 20 66 69 6c 6c 73 0a 2a 2a 20 61 20 70 6f 77 65   fills.** a powe
1f901 72 2d 6f 66 2d 74 77 6f 20 61 6c 6c 6f 63 61 74  r-of-two allocat
1f902 69 6f 6e 2e 20 20 54 68 69 73 20 6d 69 6d 69 6d  ion.  This mimim
1f903 69 7a 65 73 20 77 61 73 74 65 64 20 73 70 61 63  izes wasted spac
1f904 65 20 69 6e 20 70 6f 77 65 72 2d 6f 66 2d 74 77  e in power-of-tw
1f905 6f 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  o.** memory allo
1f906 63 61 74 6f 72 73 2e 0a 2a 2f 0a 23 64 65 66 69  cators..*/.#defi
1f907 6e 65 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b  ne JOURNAL_CHUNK
1f908 53 49 5a 45 20 28 28 69 6e 74 29 28 31 30 32 34  SIZE ((int)(1024
1f909 2d 73 69 7a 65 6f 66 28 46 69 6c 65 43 68 75 6e  -sizeof(FileChun
1f90a 6b 2a 29 29 29 0a 0a 2f 2a 20 4d 61 63 72 6f 20  k*)))../* Macro 
1f90b 74 6f 20 66 69 6e 64 20 74 68 65 20 6d 69 6e 69  to find the mini
1f90c 6d 75 6d 20 6f 66 20 74 77 6f 20 6e 75 6d 65 72  mum of two numer
1f90d 69 63 20 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23 69  ic values..*/.#i
1f90e 66 6e 64 65 66 20 4d 49 4e 0a 23 20 64 65 66 69  fndef MIN.# defi
1f90f 6e 65 20 4d 49 4e 28 78 2c 79 29 20 28 28 78 29  ne MIN(x,y) ((x)
1f910 3c 28 79 29 3f 28 78 29 3a 28 79 29 29 0a 23 65  <(y)?(x):(y)).#e
1f911 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
1f912 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
1f913 20 69 73 20 63 6f 6d 70 6f 73 65 64 20 6f 66 20   is composed of 
1f914 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  a linked list of
1f915 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72 65   these structure
1f916 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 69 6c  s..*/.struct Fil
1f917 65 43 68 75 6e 6b 20 7b 0a 20 20 46 69 6c 65 43  eChunk {.  FileC
1f918 68 75 6e 6b 20 2a 70 4e 65 78 74 3b 20 20 20 20  hunk *pNext;    
1f919 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
1f91a 78 74 20 63 68 75 6e 6b 20 69 6e 20 74 68 65 20  xt chunk in the 
1f91b 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20  journal */.  u8 
1f91c 7a 43 68 75 6e 6b 5b 4a 4f 55 52 4e 41 4c 5f 43  zChunk[JOURNAL_C
1f91d 48 55 4e 4b 53 49 5a 45 5d 3b 20 20 20 2f 2a 20  HUNKSIZE];   /* 
1f91e 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 20  Content of this 
1f91f 63 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  chunk */.};../*.
1f920 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
1f921 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20 73 65  f this object se
1f922 72 76 65 73 20 61 73 20 61 20 63 75 72 73 6f 72  rves as a cursor
1f923 20 69 6e 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61   into the rollba
1f924 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54  ck journal..** T
1f925 68 65 20 63 75 72 73 6f 72 20 63 61 6e 20 62 65  he cursor can be
1f926 20 65 69 74 68 65 72 20 66 6f 72 20 72 65 61 64   either for read
1f927 69 6e 67 20 6f 72 20 77 72 69 74 69 6e 67 2e 0a  ing or writing..
1f928 2a 2f 0a 73 74 72 75 63 74 20 46 69 6c 65 50 6f  */.struct FilePo
1f929 69 6e 74 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  int {.  sqlite3_
1f92a 69 6e 74 36 34 20 69 4f 66 66 73 65 74 3b 20 20  int64 iOffset;  
1f92b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
1f92c 74 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e  t from the begin
1f92d 6e 69 6e 67 20 6f 66 20 74 68 65 20 66 69 6c 65  ning of the file
1f92e 20 2a 2f 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20   */.  FileChunk 
1f92f 2a 70 43 68 75 6e 6b 3b 20 20 20 20 20 20 20 20  *pChunk;        
1f930 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69 66 69        /* Specifi
1f931 63 20 63 68 75 6e 6b 20 69 6e 74 6f 20 77 68 69  c chunk into whi
1f932 63 68 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 73  ch cursor points
1f933 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68   */.};../*.** Th
1f934 69 73 20 73 75 62 63 6c 61 73 73 20 69 73 20 61  is subclass is a
1f935 20 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c   subclass of sql
1f936 69 74 65 33 5f 66 69 6c 65 2e 20 20 45 61 63 68  ite3_file.  Each
1f937 20 6f 70 65 6e 20 6d 65 6d 6f 72 79 2d 6a 6f 75   open memory-jou
1f938 72 6e 61 6c 0a 2a 2a 20 69 73 20 61 6e 20 69 6e  rnal.** is an in
1f939 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 63  stance of this c
1f93a 6c 61 73 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  lass..*/.struct 
1f93b 4d 65 6d 4a 6f 75 72 6e 61 6c 20 7b 0a 20 20 73  MemJournal {.  s
1f93c 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64  qlite3_io_method
1f93d 73 20 2a 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f  s *pMethod;    /
1f93e 2a 20 50 61 72 65 6e 74 20 63 6c 61 73 73 2e 20  * Parent class. 
1f93f 4d 55 53 54 20 42 45 20 46 49 52 53 54 20 2a 2f  MUST BE FIRST */
1f940 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 46  .  FileChunk *pF
1f941 69 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  irst;           
1f942 20 20 20 2f 2a 20 48 65 61 64 20 6f 66 20 69 6e     /* Head of in
1f943 2d 6d 65 6d 6f 72 79 20 63 68 75 6e 6b 2d 6c 69  -memory chunk-li
1f944 73 74 20 2a 2f 0a 20 20 46 69 6c 65 50 6f 69 6e  st */.  FilePoin
1f945 74 20 65 6e 64 70 6f 69 6e 74 3b 20 20 20 20 20  t endpoint;     
1f946 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
1f947 65 72 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  er to the end of
1f948 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 46   the file */.  F
1f949 69 6c 65 50 6f 69 6e 74 20 72 65 61 64 70 6f 69  ilePoint readpoi
1f94a 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  nt;            /
1f94b 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  * Pointer to the
1f94c 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 61 73 74   end of the last
1f94d 20 78 52 65 61 64 28 29 20 2a 2f 0a 7d 3b 0a 0a   xRead() */.};..
1f94e 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61 20  /*.** Read data 
1f94f 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  from the in-memo
1f950 72 79 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  ry journal file.
1f951 20 20 54 68 69 73 20 69 73 20 74 68 65 20 69 6d    This is the im
1f952 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20  plementation.** 
1f953 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  of the sqlite3_v
1f954 66 73 2e 78 52 65 61 64 20 6d 65 74 68 6f 64 2e  fs.xRead method.
1f955 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
1f956 65 6d 6a 72 6e 6c 52 65 61 64 28 0a 20 20 73 71  emjrnlRead(.  sq
1f957 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64  lite3_file *pJfd
1f958 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f 75 72  ,    /* The jour
1f959 6e 61 6c 20 66 69 6c 65 20 66 72 6f 6d 20 77 68  nal file from wh
1f95a 69 63 68 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20  ich to read */. 
1f95b 20 76 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 20   void *zBuf,    
1f95c 20 20 20 20 20 20 20 20 2f 2a 20 50 75 74 20 74          /* Put t
1f95d 68 65 20 72 65 73 75 6c 74 73 20 68 65 72 65 20  he results here 
1f95e 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20  */.  int iAmt,  
1f95f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1f960 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
1f961 6f 20 72 65 61 64 20 2a 2f 0a 20 20 73 71 6c 69  o read */.  sqli
1f962 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20 20  te_int64 iOfst  
1f963 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61 64     /* Begin read
1f964 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66 73  ing at this offs
1f965 65 74 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 4a 6f  et */.){.  MemJo
1f966 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a  urnal *p = (MemJ
1f967 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20  ournal *)pJfd;. 
1f968 20 75 38 20 2a 7a 4f 75 74 20 3d 20 7a 42 75 66   u8 *zOut = zBuf
1f969 3b 0a 20 20 69 6e 74 20 6e 52 65 61 64 20 3d 20  ;.  int nRead = 
1f96a 69 41 6d 74 3b 0a 20 20 69 6e 74 20 69 43 68 75  iAmt;.  int iChu
1f96b 6e 6b 4f 66 66 73 65 74 3b 0a 20 20 46 69 6c 65  nkOffset;.  File
1f96c 43 68 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 0a 0a  Chunk *pChunk;..
1f96d 20 20 2f 2a 20 53 51 4c 69 74 65 20 6e 65 76 65    /* SQLite neve
1f96e 72 20 74 72 69 65 73 20 74 6f 20 72 65 61 64 20  r tries to read 
1f96f 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20  past the end of 
1f970 61 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  a rollback journ
1f971 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 61 73 73  al file */.  ass
1f972 65 72 74 28 20 69 4f 66 73 74 2b 69 41 6d 74 3c  ert( iOfst+iAmt<
1f973 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66  =p->endpoint.iOf
1f974 66 73 65 74 20 29 3b 0a 0a 20 20 69 66 28 20 70  fset );..  if( p
1f975 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f 66 66  ->readpoint.iOff
1f976 73 65 74 21 3d 69 4f 66 73 74 20 7c 7c 20 69 4f  set!=iOfst || iO
1f977 66 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  fst==0 ){.    sq
1f978 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 66  lite3_int64 iOff
1f979 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 70 43   = 0;.    for(pC
1f97a 68 75 6e 6b 3d 70 2d 3e 70 46 69 72 73 74 3b 20  hunk=p->pFirst; 
1f97b 0a 20 20 20 20 20 20 20 20 41 4c 57 41 59 53 28  .        ALWAYS(
1f97c 70 43 68 75 6e 6b 29 20 26 26 20 28 69 4f 66 66  pChunk) && (iOff
1f97d 2b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49  +JOURNAL_CHUNKSI
1f97e 5a 45 29 3c 3d 69 4f 66 73 74 3b 0a 20 20 20 20  ZE)<=iOfst;.    
1f97f 20 20 20 20 70 43 68 75 6e 6b 3d 70 43 68 75 6e      pChunk=pChun
1f980 6b 2d 3e 70 4e 65 78 74 0a 20 20 20 20 29 7b 0a  k->pNext.    ){.
1f981 20 20 20 20 20 20 69 4f 66 66 20 2b 3d 20 4a 4f        iOff += JO
1f982 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 3b  URNAL_CHUNKSIZE;
1f983 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
1f984 20 20 20 20 70 43 68 75 6e 6b 20 3d 20 70 2d 3e      pChunk = p->
1f985 72 65 61 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b  readpoint.pChunk
1f986 3b 0a 20 20 7d 0a 0a 20 20 69 43 68 75 6e 6b 4f  ;.  }..  iChunkO
1f987 66 66 73 65 74 20 3d 20 28 69 6e 74 29 28 69 4f  ffset = (int)(iO
1f988 66 73 74 25 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e  fst%JOURNAL_CHUN
1f989 4b 53 49 5a 45 29 3b 0a 20 20 64 6f 20 7b 0a 20  KSIZE);.  do {. 
1f98a 20 20 20 69 6e 74 20 69 53 70 61 63 65 20 3d 20     int iSpace = 
1f98b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a  JOURNAL_CHUNKSIZ
1f98c 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65 74  E - iChunkOffset
1f98d 3b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 70 79 20  ;.    int nCopy 
1f98e 3d 20 4d 49 4e 28 6e 52 65 61 64 2c 20 28 4a 4f  = MIN(nRead, (JO
1f98f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20  URNAL_CHUNKSIZE 
1f990 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 29 29  - iChunkOffset))
1f991 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 4f 75  ;.    memcpy(zOu
1f992 74 2c 20 26 70 43 68 75 6e 6b 2d 3e 7a 43 68 75  t, &pChunk->zChu
1f993 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74 5d  nk[iChunkOffset]
1f994 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20 20 7a 4f  , nCopy);.    zO
1f995 75 74 20 2b 3d 20 6e 43 6f 70 79 3b 0a 20 20 20  ut += nCopy;.   
1f996 20 6e 52 65 61 64 20 2d 3d 20 69 53 70 61 63 65   nRead -= iSpace
1f997 3b 0a 20 20 20 20 69 43 68 75 6e 6b 4f 66 66 73  ;.    iChunkOffs
1f998 65 74 20 3d 20 30 3b 0a 20 20 7d 20 77 68 69 6c  et = 0;.  } whil
1f999 65 28 20 6e 52 65 61 64 3e 3d 30 20 26 26 20 28  e( nRead>=0 && (
1f99a 70 43 68 75 6e 6b 3d 70 43 68 75 6e 6b 2d 3e 70  pChunk=pChunk->p
1f99b 4e 65 78 74 29 21 3d 30 20 26 26 20 6e 52 65 61  Next)!=0 && nRea
1f99c 64 3e 30 20 29 3b 0a 20 20 70 2d 3e 72 65 61 64  d>0 );.  p->read
1f99d 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 3d 20  point.iOffset = 
1f99e 69 4f 66 73 74 2b 69 41 6d 74 3b 0a 20 20 70 2d  iOfst+iAmt;.  p-
1f99f 3e 72 65 61 64 70 6f 69 6e 74 2e 70 43 68 75 6e  >readpoint.pChun
1f9a0 6b 20 3d 20 70 43 68 75 6e 6b 3b 0a 0a 20 20 72  k = pChunk;..  r
1f9a1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1f9a2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  .}../*.** Write 
1f9a3 64 61 74 61 20 74 6f 20 74 68 65 20 66 69 6c 65  data to the file
1f9a4 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1f9a5 6d 65 6d 6a 72 6e 6c 57 72 69 74 65 28 0a 20 20  memjrnlWrite(.  
1f9a6 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
1f9a7 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f  fd,    /* The jo
1f9a8 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20  urnal file into 
1f9a9 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 2a  which to write *
1f9aa 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
1f9ab 7a 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54 61  zBuf,      /* Ta
1f9ac 6b 65 20 64 61 74 61 20 74 6f 20 62 65 20 77 72  ke data to be wr
1f9ad 69 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65 20  itten from here 
1f9ae 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20  */.  int iAmt,  
1f9af 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1f9b0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
1f9b1 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c  o write */.  sql
1f9b2 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20  ite_int64 iOfst 
1f9b3 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72 69      /* Begin wri
1f9b4 74 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66  ting at this off
1f9b5 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c  set into the fil
1f9b6 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 4a 6f 75  e */.){.  MemJou
1f9b7 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f  rnal *p = (MemJo
1f9b8 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20  urnal *)pJfd;.  
1f9b9 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 69 41 6d  int nWrite = iAm
1f9ba 74 3b 0a 20 20 75 38 20 2a 7a 57 72 69 74 65 20  t;.  u8 *zWrite 
1f9bb 3d 20 28 75 38 20 2a 29 7a 42 75 66 3b 0a 0a 20  = (u8 *)zBuf;.. 
1f9bc 20 2f 2a 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79   /* An in-memory
1f9bd 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 68   journal file sh
1f9be 6f 75 6c 64 20 6f 6e 6c 79 20 65 76 65 72 20 62  ould only ever b
1f9bf 65 20 61 70 70 65 6e 64 65 64 20 74 6f 2e 20 52  e appended to. R
1f9c0 61 6e 64 6f 6d 0a 20 20 2a 2a 20 61 63 63 65 73  andom.  ** acces
1f9c1 73 20 77 72 69 74 65 73 20 61 72 65 20 6e 6f 74  s writes are not
1f9c2 20 72 65 71 75 69 72 65 64 20 62 79 20 73 71 6c   required by sql
1f9c3 69 74 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ite..  */.  asse
1f9c4 72 74 28 20 69 4f 66 73 74 3d 3d 70 2d 3e 65 6e  rt( iOfst==p->en
1f9c5 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 29  dpoint.iOffset )
1f9c6 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
1f9c7 45 54 45 52 28 69 4f 66 73 74 29 3b 0a 0a 20 20  ETER(iOfst);..  
1f9c8 77 68 69 6c 65 28 20 6e 57 72 69 74 65 3e 30 20  while( nWrite>0 
1f9c9 29 7b 0a 20 20 20 20 46 69 6c 65 43 68 75 6e 6b  ){.    FileChunk
1f9ca 20 2a 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 65 6e   *pChunk = p->en
1f9cb 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 3b 0a 20  dpoint.pChunk;. 
1f9cc 20 20 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66     int iChunkOff
1f9cd 73 65 74 20 3d 20 28 69 6e 74 29 28 70 2d 3e 65  set = (int)(p->e
1f9ce 6e 64 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 25  ndpoint.iOffset%
1f9cf 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a  JOURNAL_CHUNKSIZ
1f9d0 45 29 3b 0a 20 20 20 20 69 6e 74 20 69 53 70 61  E);.    int iSpa
1f9d1 63 65 20 3d 20 4d 49 4e 28 6e 57 72 69 74 65 2c  ce = MIN(nWrite,
1f9d2 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49   JOURNAL_CHUNKSI
1f9d3 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65  ZE - iChunkOffse
1f9d4 74 29 3b 0a 0a 20 20 20 20 69 66 28 20 69 43 68  t);..    if( iCh
1f9d5 75 6e 6b 4f 66 66 73 65 74 3d 3d 30 20 29 7b 0a  unkOffset==0 ){.
1f9d6 20 20 20 20 20 20 2f 2a 20 4e 65 77 20 63 68 75        /* New chu
1f9d7 6e 6b 20 69 73 20 72 65 71 75 69 72 65 64 20 74  nk is required t
1f9d8 6f 20 65 78 74 65 6e 64 20 74 68 65 20 66 69 6c  o extend the fil
1f9d9 65 2e 20 2a 2f 0a 20 20 20 20 20 20 46 69 6c 65  e. */.      File
1f9da 43 68 75 6e 6b 20 2a 70 4e 65 77 20 3d 20 73 71  Chunk *pNew = sq
1f9db 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
1f9dc 65 6f 66 28 46 69 6c 65 43 68 75 6e 6b 29 29 3b  eof(FileChunk));
1f9dd 0a 20 20 20 20 20 20 69 66 28 20 21 70 4e 65 77  .      if( !pNew
1f9de 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
1f9df 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  rn SQLITE_IOERR_
1f9e0 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20  NOMEM;.      }. 
1f9e1 20 20 20 20 20 70 4e 65 77 2d 3e 70 4e 65 78 74       pNew->pNext
1f9e2 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20   = 0;.      if( 
1f9e3 70 43 68 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20  pChunk ){.      
1f9e4 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 46 69    assert( p->pFi
1f9e5 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 70  rst );.        p
1f9e6 43 68 75 6e 6b 2d 3e 70 4e 65 78 74 20 3d 20 70  Chunk->pNext = p
1f9e7 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  New;.      }else
1f9e8 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
1f9e9 28 20 21 70 2d 3e 70 46 69 72 73 74 20 29 3b 0a  ( !p->pFirst );.
1f9ea 20 20 20 20 20 20 20 20 70 2d 3e 70 46 69 72 73          p->pFirs
1f9eb 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20 20  t = pNew;.      
1f9ec 7d 0a 20 20 20 20 20 20 70 2d 3e 65 6e 64 70 6f  }.      p->endpo
1f9ed 69 6e 74 2e 70 43 68 75 6e 6b 20 3d 20 70 4e 65  int.pChunk = pNe
1f9ee 77 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d 65  w;.    }..    me
1f9ef 6d 63 70 79 28 26 70 2d 3e 65 6e 64 70 6f 69 6e  mcpy(&p->endpoin
1f9f0 74 2e 70 43 68 75 6e 6b 2d 3e 7a 43 68 75 6e 6b  t.pChunk->zChunk
1f9f1 5b 69 43 68 75 6e 6b 4f 66 66 73 65 74 5d 2c 20  [iChunkOffset], 
1f9f2 7a 57 72 69 74 65 2c 20 69 53 70 61 63 65 29 3b  zWrite, iSpace);
1f9f3 0a 20 20 20 20 7a 57 72 69 74 65 20 2b 3d 20 69  .    zWrite += i
1f9f4 53 70 61 63 65 3b 0a 20 20 20 20 6e 57 72 69 74  Space;.    nWrit
1f9f5 65 20 2d 3d 20 69 53 70 61 63 65 3b 0a 20 20 20  e -= iSpace;.   
1f9f6 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66   p->endpoint.iOf
1f9f7 66 73 65 74 20 2b 3d 20 69 53 70 61 63 65 3b 0a  fset += iSpace;.
1f9f8 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51    }..  return SQ
1f9f9 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
1f9fa 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 66  * Truncate the f
1f9fb 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ile..*/.static i
1f9fc 6e 74 20 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61  nt memjrnlTrunca
1f9fd 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  te(sqlite3_file 
1f9fe 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f 69 6e  *pJfd, sqlite_in
1f9ff 74 36 34 20 73 69 7a 65 29 7b 0a 20 20 4d 65 6d  t64 size){.  Mem
1fa00 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65  Journal *p = (Me
1fa01 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b  mJournal *)pJfd;
1fa02 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43  .  FileChunk *pC
1fa03 68 75 6e 6b 3b 0a 20 20 61 73 73 65 72 74 28 73  hunk;.  assert(s
1fa04 69 7a 65 3d 3d 30 29 3b 0a 20 20 55 4e 55 53 45  ize==0);.  UNUSE
1fa05 44 5f 50 41 52 41 4d 45 54 45 52 28 73 69 7a 65  D_PARAMETER(size
1fa06 29 3b 0a 20 20 70 43 68 75 6e 6b 20 3d 20 70 2d  );.  pChunk = p-
1fa07 3e 70 46 69 72 73 74 3b 0a 20 20 77 68 69 6c 65  >pFirst;.  while
1fa08 28 20 70 43 68 75 6e 6b 20 29 7b 0a 20 20 20 20  ( pChunk ){.    
1fa09 46 69 6c 65 43 68 75 6e 6b 20 2a 70 54 6d 70 20  FileChunk *pTmp 
1fa0a 3d 20 70 43 68 75 6e 6b 3b 0a 20 20 20 20 70 43  = pChunk;.    pC
1fa0b 68 75 6e 6b 20 3d 20 70 43 68 75 6e 6b 2d 3e 70  hunk = pChunk->p
1fa0c 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  Next;.    sqlite
1fa0d 33 5f 66 72 65 65 28 70 54 6d 70 29 3b 0a 20 20  3_free(pTmp);.  
1fa0e 7d 0a 20 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f  }.  sqlite3MemJo
1fa0f 75 72 6e 61 6c 4f 70 65 6e 28 70 4a 66 64 29 3b  urnalOpen(pJfd);
1fa10 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1fa11 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  _OK;.}../*.** Cl
1fa12 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f  ose the file..*/
1fa13 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a  .static int memj
1fa14 72 6e 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33  rnlClose(sqlite3
1fa15 5f 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20  _file *pJfd){.  
1fa16 6d 65 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65 28  memjrnlTruncate(
1fa17 70 4a 66 64 2c 20 30 29 3b 0a 20 20 72 65 74 75  pJfd, 0);.  retu
1fa18 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
1fa19 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65  ../*.** Sync the
1fa1a 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 79 6e   file..**.** Syn
1fa1b 63 69 6e 67 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  cing an in-memor
1fa1c 79 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 20 6e  y journal is a n
1fa1d 6f 2d 6f 70 2e 20 20 41 6e 64 2c 20 69 6e 20 66  o-op.  And, in f
1fa1e 61 63 74 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  act, this routin
1fa1f 65 0a 2a 2a 20 69 73 20 6e 65 76 65 72 20 63 61  e.** is never ca
1fa20 6c 6c 65 64 20 69 6e 20 61 20 77 6f 72 6b 69 6e  lled in a workin
1fa21 67 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  g implementation
1fa22 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e  .  This implemen
1fa23 74 61 74 69 6f 6e 0a 2a 2a 20 65 78 69 73 74 73  tation.** exists
1fa24 20 70 75 72 65 6c 79 20 61 73 20 61 20 63 6f 6e   purely as a con
1fa25 74 69 6e 67 65 6e 63 79 2c 20 69 6e 20 63 61 73  tingency, in cas
1fa26 65 20 73 6f 6d 65 20 6d 61 6c 66 75 6e 63 74 69  e some malfuncti
1fa27 6f 6e 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72  on in some other
1fa28 0a 2a 2a 20 70 61 72 74 20 6f 66 20 53 51 4c 69  .** part of SQLi
1fa29 74 65 20 63 61 75 73 65 73 20 53 79 6e 63 20 74  te causes Sync t
1fa2a 6f 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20 6d  o be called by m
1fa2b 69 73 74 61 6b 65 2e 0a 2a 2f 0a 73 74 61 74 69  istake..*/.stati
1fa2c 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 53 79 6e  c int memjrnlSyn
1fa2d 63 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a  c(sqlite3_file *
1fa2e 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74  NotUsed, int Not
1fa2f 55 73 65 64 32 29 7b 20 20 20 2f 2a 4e 4f 5f 54  Used2){   /*NO_T
1fa30 45 53 54 2a 2f 0a 20 20 55 4e 55 53 45 44 5f 50  EST*/.  UNUSED_P
1fa31 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65  ARAMETER2(NotUse
1fa32 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 20 20 20  d, NotUsed2);   
1fa33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa34 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a 2f       /*NO_TEST*/
1fa35 0a 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 20  .  assert( 0 ); 
1fa36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa39 2f 2a 4e 4f 5f 54 45 53 54 2a 2f 0a 20 20 72 65  /*NO_TEST*/.  re
1fa3a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
1fa3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa3d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 4e 4f 5f             /*NO_
1fa3e 54 45 53 54 2a 2f 0a 7d 20 20 20 20 20 20 20 20  TEST*/.}        
1fa3f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fa42 20 20 20 20 20 20 2f 2a 4e 4f 5f 54 45 53 54 2a        /*NO_TEST*
1fa43 2f 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 74  /../*.** Query t
1fa44 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66  he size of the f
1fa45 69 6c 65 20 69 6e 20 62 79 74 65 73 2e 0a 2a 2f  ile in bytes..*/
1fa46 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a  .static int memj
1fa47 72 6e 6c 46 69 6c 65 53 69 7a 65 28 73 71 6c 69  rnlFileSize(sqli
1fa48 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20  te3_file *pJfd, 
1fa49 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 53  sqlite_int64 *pS
1fa4a 69 7a 65 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e  ize){.  MemJourn
1fa4b 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72  al *p = (MemJour
1fa4c 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 2a 70  nal *)pJfd;.  *p
1fa4d 53 69 7a 65 20 3d 20 28 73 71 6c 69 74 65 5f 69  Size = (sqlite_i
1fa4e 6e 74 36 34 29 20 70 2d 3e 65 6e 64 70 6f 69 6e  nt64) p->endpoin
1fa4f 74 2e 69 4f 66 66 73 65 74 3b 0a 20 20 72 65 74  t.iOffset;.  ret
1fa50 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1fa51 0a 0a 2f 2a 0a 2a 2a 20 54 61 62 6c 65 20 6f 66  ../*.** Table of
1fa52 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 4d 65 6d   methods for Mem
1fa53 4a 6f 75 72 6e 61 6c 20 73 71 6c 69 74 65 33 5f  Journal sqlite3_
1fa54 66 69 6c 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a  file object..*/.
1fa55 73 74 61 74 69 63 20 73 74 72 75 63 74 20 73 71  static struct sq
1fa56 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
1fa57 20 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 6f   MemJournalMetho
1fa58 64 73 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20  ds = {.  1,     
1fa59 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56             /* iV
1fa5a 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 6d 65 6d 6a  ersion */.  memj
1fa5b 72 6e 6c 43 6c 6f 73 65 2c 20 20 20 20 20 2f 2a  rnlClose,     /*
1fa5c 20 78 43 6c 6f 73 65 20 2a 2f 0a 20 20 6d 65 6d   xClose */.  mem
1fa5d 6a 72 6e 6c 52 65 61 64 2c 20 20 20 20 20 20 2f  jrnlRead,      /
1fa5e 2a 20 78 52 65 61 64 20 2a 2f 0a 20 20 6d 65 6d  * xRead */.  mem
1fa5f 6a 72 6e 6c 57 72 69 74 65 2c 20 20 20 20 20 2f  jrnlWrite,     /
1fa60 2a 20 78 57 72 69 74 65 20 2a 2f 0a 20 20 6d 65  * xWrite */.  me
1fa61 6d 6a 72 6e 6c 54 72 75 6e 63 61 74 65 2c 20 20  mjrnlTruncate,  
1fa62 2f 2a 20 78 54 72 75 6e 63 61 74 65 20 2a 2f 0a  /* xTruncate */.
1fa63 20 20 6d 65 6d 6a 72 6e 6c 53 79 6e 63 2c 20 20    memjrnlSync,  
1fa64 20 20 20 20 2f 2a 20 78 53 79 6e 63 20 2a 2f 0a      /* xSync */.
1fa65 20 20 6d 65 6d 6a 72 6e 6c 46 69 6c 65 53 69 7a    memjrnlFileSiz
1fa66 65 2c 20 20 2f 2a 20 78 46 69 6c 65 53 69 7a 65  e,  /* xFileSize
1fa67 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20   */.  0,        
1fa68 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b          /* xLock
1fa69 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20   */.  0,        
1fa6a 20 20 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f          /* xUnlo
1fa6b 63 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20  ck */.  0,      
1fa6c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 68            /* xCh
1fa6d 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20  eckReservedLock 
1fa6e 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20  */.  0,         
1fa6f 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 43         /* xFileC
1fa70 6f 6e 74 72 6f 6c 20 2a 2f 0a 20 20 30 2c 20 20  ontrol */.  0,  
1fa71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1fa72 20 78 53 65 63 74 6f 72 53 69 7a 65 20 2a 2f 0a   xSectorSize */.
1fa73 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20    0             
1fa74 20 20 20 20 2f 2a 20 78 44 65 76 69 63 65 43 68      /* xDeviceCh
1fa75 61 72 61 63 74 65 72 69 73 74 69 63 73 20 2a 2f  aracteristics */
1fa76 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70 65 6e  .};../* .** Open
1fa77 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e   a journal file.
1fa78 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1fa79 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  TE void sqlite3M
1fa7a 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 71  emJournalOpen(sq
1fa7b 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64  lite3_file *pJfd
1fa7c 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20  ){.  MemJournal 
1fa7d 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c  *p = (MemJournal
1fa7e 20 2a 29 70 4a 66 64 3b 0a 20 20 61 73 73 65 72   *)pJfd;.  asser
1fa7f 74 28 20 45 49 47 48 54 5f 42 59 54 45 5f 41 4c  t( EIGHT_BYTE_AL
1fa80 49 47 4e 4d 45 4e 54 28 70 29 20 29 3b 0a 20 20  IGNMENT(p) );.  
1fa81 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 73 71 6c  memset(p, 0, sql
1fa82 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69  ite3MemJournalSi
1fa83 7a 65 28 29 29 3b 0a 20 20 70 2d 3e 70 4d 65 74  ze());.  p->pMet
1fa84 68 6f 64 20 3d 20 26 4d 65 6d 4a 6f 75 72 6e 61  hod = &MemJourna
1fa85 6c 4d 65 74 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a 0a  lMethods;.}../*.
1fa86 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
1fa87 66 20 74 68 65 20 66 69 6c 65 2d 68 61 6e 64 6c  f the file-handl
1fa88 65 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61  e passed as an a
1fa89 72 67 75 6d 65 6e 74 20 69 73 20 0a 2a 2a 20 61  rgument is .** a
1fa8a 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72  n in-memory jour
1fa8b 6e 61 6c 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nal .*/.SQLITE_P
1fa8c 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1fa8d 65 33 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 73  e3IsMemJournal(s
1fa8e 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66  qlite3_file *pJf
1fa8f 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 4a 66  d){.  return pJf
1fa90 64 2d 3e 70 4d 65 74 68 6f 64 73 3d 3d 26 4d 65  d->pMethods==&Me
1fa91 6d 4a 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 3b  mJournalMethods;
1fa92 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72  .}../* .** Retur
1fa93 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
1fa94 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20 74  bytes required t
1fa95 6f 20 73 74 6f 72 65 20 61 20 4d 65 6d 4a 6f 75  o store a MemJou
1fa96 72 6e 61 6c 20 74 68 61 74 20 75 73 65 73 20 76  rnal that uses v
1fa97 66 73 0a 2a 2a 20 70 56 66 73 20 74 6f 20 63 72  fs.** pVfs to cr
1fa98 65 61 74 65 20 74 68 65 20 75 6e 64 65 72 6c 79  eate the underly
1fa99 69 6e 67 20 6f 6e 2d 64 69 73 6b 20 66 69 6c 65  ing on-disk file
1fa9a 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
1fa9b 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1fa9c 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 76  MemJournalSize(v
1fa9d 6f 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  oid){.  return s
1fa9e 69 7a 65 6f 66 28 4d 65 6d 4a 6f 75 72 6e 61 6c  izeof(MemJournal
1fa9f 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  );.}../*********
1faa0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d  ***** End of mem
1faa1 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a  journal.c ******
1faa2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1faa3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1faa4 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1faa5 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1faa6 20 77 61 6c 6b 65 72 2e 63 20 2a 2a 2a 2a 2a 2a   walker.c ******
1faa7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1faa8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1faa9 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38  ****/./*.** 2008
1faaa 20 41 75 67 75 73 74 20 31 36 0a 2a 2a 0a 2a 2a   August 16.**.**
1faab 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1faac 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1faad 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1faae 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1faaf 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1fab0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1fab1 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1fab2 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1fab3 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1fab4 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1fab5 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1fab6 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1fab7 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1fab8 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1fab9 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1faba 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1fabb 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1fabc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fabd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fabe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fabf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fac0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
1fac1 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
1fac2 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66 6f  routines used fo
1fac3 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70 61  r walking the pa
1fac4 72 73 65 72 20 74 72 65 65 20 66 6f 72 0a 2a 2a  rser tree for.**
1fac5 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   an SQL statemen
1fac6 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 77 61  t..**.** $Id: wa
1fac7 6c 6b 65 72 2e 63 2c 76 20 31 2e 37 20 32 30 30  lker.c,v 1.7 200
1fac8 39 2f 30 36 2f 31 35 20 32 33 3a 31 35 3a 35 39  9/06/15 23:15:59
1fac9 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a   drh Exp $.*/...
1faca 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78  /*.** Walk an ex
1facb 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20  pression tree.  
1facc 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62  Invoke the callb
1facd 61 63 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61 63  ack once for eac
1face 68 20 6e 6f 64 65 0a 2a 2a 20 6f 66 20 74 68 65  h node.** of the
1facf 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 77 68 69   expression, whi
1fad0 6c 65 20 64 65 63 65 6e 64 69 6e 67 2e 20 20 28  le decending.  (
1fad1 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
1fad2 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20  the callback.** 
1fad3 69 73 20 69 6e 76 6f 6b 65 64 20 62 65 66 6f 72  is invoked befor
1fad4 65 20 76 69 73 69 74 69 6e 67 20 63 68 69 6c 64  e visiting child
1fad5 72 65 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ren.).**.** The 
1fad6 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f  return value fro
1fad7 6d 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 73  m the callback s
1fad8 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20 6f 66 20  hould be one of 
1fad9 74 68 65 20 57 52 43 5f 2a 0a 2a 2a 20 63 6f 6e  the WRC_*.** con
1fada 73 74 61 6e 74 73 20 74 6f 20 73 70 65 63 69 66  stants to specif
1fadb 79 20 68 6f 77 20 74 6f 20 70 72 6f 63 65 65 64  y how to proceed
1fadc 20 77 69 74 68 20 74 68 65 20 77 61 6c 6b 2e 0a   with the walk..
1fadd 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 43 6f 6e  **.**    WRC_Con
1fade 74 69 6e 75 65 20 20 20 20 20 20 43 6f 6e 74 69  tinue      Conti
1fadf 6e 75 65 20 64 65 73 63 65 6e 64 69 6e 67 20 64  nue descending d
1fae0 6f 77 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a 2a  own the tree..**
1fae1 0a 2a 2a 20 20 20 20 57 52 43 5f 50 72 75 6e 65  .**    WRC_Prune
1fae2 20 20 20 20 20 20 20 20 20 44 6f 20 6e 6f 74 20           Do not 
1fae3 64 65 73 63 65 6e 64 20 69 6e 74 6f 20 63 68 69  descend into chi
1fae4 6c 64 20 6e 6f 64 65 73 2e 20 20 42 75 74 20 61  ld nodes.  But a
1fae5 6c 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 20  llow.**         
1fae6 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65               the
1fae7 20 77 61 6c 6b 20 74 6f 20 63 6f 6e 74 69 6e 75   walk to continu
1fae8 65 20 77 69 74 68 20 73 69 62 6c 69 6e 67 20 6e  e with sibling n
1fae9 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 57  odes..**.**    W
1faea 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20 20  RC_Abort        
1faeb 20 44 6f 20 6e 6f 20 6d 6f 72 65 20 63 61 6c 6c   Do no more call
1faec 62 61 63 6b 73 2e 20 20 55 6e 77 69 6e 64 20 74  backs.  Unwind t
1faed 68 65 20 73 74 61 63 6b 20 61 6e 64 0a 2a 2a 20  he stack and.** 
1faee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1faef 20 20 20 20 20 72 65 74 75 72 6e 20 74 68 65 20       return the 
1faf0 74 6f 70 2d 6c 65 76 65 6c 20 77 61 6c 6b 20 63  top-level walk c
1faf1 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  all..**.** The r
1faf2 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f 6d  eturn value from
1faf3 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
1faf4 20 57 52 43 5f 41 62 6f 72 74 20 74 6f 20 61 62   WRC_Abort to ab
1faf5 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 65 20 77  andon the tree w
1faf6 61 6c 6b 0a 2a 2a 20 61 6e 64 20 57 52 43 5f 43  alk.** and WRC_C
1faf7 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6e 74 69  ontinue to conti
1faf8 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nue..*/.SQLITE_P
1faf9 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1fafa 65 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65  e3WalkExpr(Walke
1fafb 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72  r *pWalker, Expr
1fafc 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74 20   *pExpr){.  int 
1fafd 72 63 3b 0a 20 20 69 66 28 20 70 45 78 70 72 3d  rc;.  if( pExpr=
1fafe 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  =0 ) return WRC_
1faff 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 74 65 73 74  Continue;.  test
1fb00 63 61 73 65 28 20 45 78 70 72 48 61 73 50 72 6f  case( ExprHasPro
1fb01 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
1fb02 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 3b 0a 20 20  TokenOnly) );.  
1fb03 74 65 73 74 63 61 73 65 28 20 45 78 70 72 48 61  testcase( ExprHa
1fb04 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
1fb05 20 45 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a   EP_Reduced) );.
1fb06 20 20 72 63 20 3d 20 70 57 61 6c 6b 65 72 2d 3e    rc = pWalker->
1fb07 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 28 70 57  xExprCallback(pW
1fb08 61 6c 6b 65 72 2c 20 70 45 78 70 72 29 3b 0a 20  alker, pExpr);. 
1fb09 20 69 66 28 20 72 63 3d 3d 57 52 43 5f 43 6f 6e   if( rc==WRC_Con
1fb0a 74 69 6e 75 65 0a 20 20 20 20 20 20 20 20 20 20  tinue.          
1fb0b 20 20 20 20 26 26 20 21 45 78 70 72 48 61 73 41      && !ExprHasA
1fb0c 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  nyProperty(pExpr
1fb0d 2c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29  ,EP_TokenOnly) )
1fb0e 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
1fb0f 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65  3WalkExpr(pWalke
1fb10 72 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29  r, pExpr->pLeft)
1fb11 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
1fb12 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  ort;.    if( sql
1fb13 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61  ite3WalkExpr(pWa
1fb14 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 52 69  lker, pExpr->pRi
1fb15 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 57 52  ght) ) return WR
1fb16 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 28  C_Abort;.    if(
1fb17 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
1fb18 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65  (pExpr, EP_xIsSe
1fb19 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 69  lect) ){.      i
1fb1a 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65  f( sqlite3WalkSe
1fb1b 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70 45  lect(pWalker, pE
1fb1c 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 29 20  xpr->x.pSelect) 
1fb1d 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
1fb1e 72 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  rt;.    }else{. 
1fb1f 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1fb20 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61  WalkExprList(pWa
1fb21 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 78 2e 70  lker, pExpr->x.p
1fb22 4c 69 73 74 29 20 29 20 72 65 74 75 72 6e 20 57  List) ) return W
1fb23 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a  RC_Abort;.    }.
1fb24 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 20    }.  return rc 
1fb25 26 20 57 52 43 5f 41 62 6f 72 74 3b 0a 7d 0a 0a  & WRC_Abort;.}..
1fb26 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74  /*.** Call sqlit
1fb27 65 33 57 61 6c 6b 45 78 70 72 28 29 20 66 6f 72  e3WalkExpr() for
1fb28 20 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f   every expressio
1fb29 6e 20 69 6e 20 6c 69 73 74 20 70 20 6f 72 20 75  n in list p or u
1fb2a 6e 74 69 6c 0a 2a 2a 20 61 6e 20 61 62 6f 72 74  ntil.** an abort
1fb2b 20 72 65 71 75 65 73 74 20 69 73 20 73 65 65 6e   request is seen
1fb2c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1fb2d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
1fb2e 61 6c 6b 45 78 70 72 4c 69 73 74 28 57 61 6c 6b  alkExprList(Walk
1fb2f 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70  er *pWalker, Exp
1fb30 72 4c 69 73 74 20 2a 70 29 7b 0a 20 20 69 6e 74  rList *p){.  int
1fb31 20 69 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70   i;.  struct Exp
1fb32 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  rList_item *pIte
1fb33 6d 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  m;.  if( p ){.  
1fb34 20 20 66 6f 72 28 69 3d 70 2d 3e 6e 45 78 70 72    for(i=p->nExpr
1fb35 2c 20 70 49 74 65 6d 3d 70 2d 3e 61 3b 20 69 3e  , pItem=p->a; i>
1fb36 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29  0; i--, pItem++)
1fb37 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
1fb38 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c  te3WalkExpr(pWal
1fb39 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70  ker, pItem->pExp
1fb3a 72 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  r) ) return WRC_
1fb3b 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  Abort;.    }.  }
1fb3c 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f  .  return WRC_Co
1fb3d 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ntinue;.}../*.**
1fb3e 20 57 61 6c 6b 20 61 6c 6c 20 65 78 70 72 65 73   Walk all expres
1fb3f 73 69 6f 6e 73 20 61 73 73 6f 63 69 61 74 65 64  sions associated
1fb40 20 77 69 74 68 20 53 45 4c 45 43 54 20 73 74 61   with SELECT sta
1fb41 74 65 6d 65 6e 74 20 70 2e 20 20 44 6f 0a 2a 2a  tement p.  Do.**
1fb42 20 6e 6f 74 20 69 6e 76 6f 6b 65 20 74 68 65 20   not invoke the 
1fb43 53 45 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b 20  SELECT callback 
1fb44 6f 6e 20 70 2c 20 62 75 74 20 64 6f 20 28 6f 66  on p, but do (of
1fb45 20 63 6f 75 72 73 65 29 20 69 6e 76 6f 6b 65 0a   course) invoke.
1fb46 2a 2a 20 61 6e 79 20 65 78 70 72 20 63 61 6c 6c  ** any expr call
1fb47 62 61 63 6b 73 20 61 6e 64 20 53 45 4c 45 43 54  backs and SELECT
1fb48 20 63 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20   callbacks that 
1fb49 63 6f 6d 65 20 66 72 6f 6d 20 73 75 62 71 75 65  come from subque
1fb4a 72 69 65 73 2e 0a 2a 2a 20 52 65 74 75 72 6e 20  ries..** Return 
1fb4b 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43  WRC_Abort or WRC
1fb4c 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51  _Continue..*/.SQ
1fb4d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1fb4e 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65   sqlite3WalkSele
1fb4f 63 74 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70  ctExpr(Walker *p
1fb50 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a  Walker, Select *
1fb51 70 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  p){.  if( sqlite
1fb52 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57  3WalkExprList(pW
1fb53 61 6c 6b 65 72 2c 20 70 2d 3e 70 45 4c 69 73 74  alker, p->pEList
1fb54 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1fb55 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69  bort;.  if( sqli
1fb56 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c  te3WalkExpr(pWal
1fb57 6b 65 72 2c 20 70 2d 3e 70 57 68 65 72 65 29 20  ker, p->pWhere) 
1fb58 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
1fb59 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  rt;.  if( sqlite
1fb5a 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57  3WalkExprList(pW
1fb5b 61 6c 6b 65 72 2c 20 70 2d 3e 70 47 72 6f 75 70  alker, p->pGroup
1fb5c 42 79 29 20 29 20 72 65 74 75 72 6e 20 57 52 43  By) ) return WRC
1fb5d 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71  _Abort;.  if( sq
1fb5e 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57  lite3WalkExpr(pW
1fb5f 61 6c 6b 65 72 2c 20 70 2d 3e 70 48 61 76 69 6e  alker, p->pHavin
1fb60 67 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  g) ) return WRC_
1fb61 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c  Abort;.  if( sql
1fb62 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74  ite3WalkExprList
1fb63 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 72  (pWalker, p->pOr
1fb64 64 65 72 42 79 29 20 29 20 72 65 74 75 72 6e 20  derBy) ) return 
1fb65 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28  WRC_Abort;.  if(
1fb66 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72   sqlite3WalkExpr
1fb67 28 70 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4c 69  (pWalker, p->pLi
1fb68 6d 69 74 29 20 29 20 72 65 74 75 72 6e 20 57 52  mit) ) return WR
1fb69 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73  C_Abort;.  if( s
1fb6a 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70  qlite3WalkExpr(p
1fb6b 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 66 66 73  Walker, p->pOffs
1fb6c 65 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43  et) ) return WRC
1fb6d 5f 41 62 6f 72 74 3b 0a 20 20 72 65 74 75 72 6e  _Abort;.  return
1fb6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d   WRC_Continue;.}
1fb6f 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 74 68 65  ../*.** Walk the
1fb70 20 70 61 72 73 65 20 74 72 65 65 73 20 61 73 73   parse trees ass
1fb71 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 6c 6c  ociated with all
1fb72 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 74   subqueries in t
1fb73 68 65 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73  he.** FROM claus
1fb74 65 20 6f 66 20 53 45 4c 45 43 54 20 73 74 61 74  e of SELECT stat
1fb75 65 6d 65 6e 74 20 70 2e 20 20 44 6f 20 6e 6f 74  ement p.  Do not
1fb76 20 69 6e 76 6f 6b 65 20 74 68 65 20 73 65 6c 65   invoke the sele
1fb77 63 74 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f  ct.** callback o
1fb78 6e 20 70 2c 20 62 75 74 20 64 6f 20 69 6e 76 6f  n p, but do invo
1fb79 6b 65 20 69 74 20 6f 6e 20 65 61 63 68 20 46 52  ke it on each FR
1fb7a 4f 4d 20 63 6c 61 75 73 65 20 73 75 62 71 75 65  OM clause subque
1fb7b 72 79 0a 2a 2a 20 61 6e 64 20 6f 6e 20 61 6e 79  ry.** and on any
1fb7c 20 73 75 62 71 75 65 72 69 65 73 20 66 75 72 74   subqueries furt
1fb7d 68 65 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 20  her down in the 
1fb7e 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 0a 2a  tree.  Return .*
1fb7f 2a 20 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 57  * WRC_Abort or W
1fb80 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 2a 2f 0a  RC_Continue;.*/.
1fb81 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1fb82 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65  nt sqlite3WalkSe
1fb83 6c 65 63 74 46 72 6f 6d 28 57 61 6c 6b 65 72 20  lectFrom(Walker 
1fb84 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74  *pWalker, Select
1fb85 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 20   *p){.  SrcList 
1fb86 2a 70 53 72 63 3b 0a 20 20 69 6e 74 20 69 3b 0a  *pSrc;.  int i;.
1fb87 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
1fb88 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20  _item *pItem;.. 
1fb89 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b   pSrc = p->pSrc;
1fb8a 0a 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 53  .  if( ALWAYS(pS
1fb8b 72 63 29 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  rc) ){.    for(i
1fb8c 3d 70 53 72 63 2d 3e 6e 53 72 63 2c 20 70 49 74  =pSrc->nSrc, pIt
1fb8d 65 6d 3d 70 53 72 63 2d 3e 61 3b 20 69 3e 30 3b  em=pSrc->a; i>0;
1fb8e 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   i--, pItem++){.
1fb8f 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
1fb90 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c  3WalkSelect(pWal
1fb91 6b 65 72 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c  ker, pItem->pSel
1fb92 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ect) ){.        
1fb93 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
1fb94 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
1fb95 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43    }.  return WRC
1fb96 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 20 0a 0a 2f  _Continue;.} ../
1fb97 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65  *.** Call sqlite
1fb98 33 57 61 6c 6b 45 78 70 72 28 29 20 66 6f 72 20  3WalkExpr() for 
1fb99 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e  every expression
1fb9a 20 69 6e 20 53 65 6c 65 63 74 20 73 74 61 74 65   in Select state
1fb9b 6d 65 6e 74 20 70 2e 0a 2a 2a 20 49 6e 76 6f 6b  ment p..** Invok
1fb9c 65 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c  e sqlite3WalkSel
1fb9d 65 63 74 28 29 20 66 6f 72 20 73 75 62 71 75 65  ect() for subque
1fb9e 72 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d  ries in the FROM
1fb9f 20 63 6c 61 75 73 65 20 61 6e 64 0a 2a 2a 20 6f   clause and.** o
1fba0 6e 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73  n the compound s
1fba1 65 6c 65 63 74 20 63 68 61 69 6e 2c 20 70 2d 3e  elect chain, p->
1fba2 70 50 72 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 52 65  pPrior..**.** Re
1fba3 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75  turn WRC_Continu
1fba4 65 20 75 6e 64 65 72 20 6e 6f 72 6d 61 6c 20 63  e under normal c
1fba5 6f 6e 64 69 74 69 6f 6e 73 2e 20 20 52 65 74 75  onditions.  Retu
1fba6 72 6e 20 57 52 43 5f 41 62 6f 72 74 20 69 66 0a  rn WRC_Abort if.
1fba7 2a 2a 20 74 68 65 72 65 20 69 73 20 61 6e 20 61  ** there is an a
1fba8 62 6f 72 74 20 72 65 71 75 65 73 74 2e 0a 2a 2a  bort request..**
1fba9 0a 2a 2a 20 49 66 20 74 68 65 20 57 61 6c 6b 65  .** If the Walke
1fbaa 72 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20  r does not have 
1fbab 61 6e 20 78 53 65 6c 65 63 74 43 61 6c 6c 62 61  an xSelectCallba
1fbac 63 6b 28 29 20 74 68 65 6e 20 74 68 69 73 20 72  ck() then this r
1fbad 6f 75 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e  outine.** is a n
1fbae 6f 2d 6f 70 20 72 65 74 75 72 6e 69 6e 67 20 57  o-op returning W
1fbaf 52 43 5f 43 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a  RC_Continue..*/.
1fbb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1fbb1 6e 74 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65  nt sqlite3WalkSe
1fbb2 6c 65 63 74 28 57 61 6c 6b 65 72 20 2a 70 57 61  lect(Walker *pWa
1fbb3 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29  lker, Select *p)
1fbb4 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 66  {.  int rc;.  if
1fbb5 28 20 70 3d 3d 30 20 7c 7c 20 70 57 61 6c 6b 65  ( p==0 || pWalke
1fbb6 72 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61  r->xSelectCallba
1fbb7 63 6b 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57  ck==0 ) return W
1fbb8 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 72  RC_Continue;.  r
1fbb9 63 20 3d 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65  c = WRC_Continue
1fbba 3b 0a 20 20 77 68 69 6c 65 28 20 70 20 20 29 7b  ;.  while( p  ){
1fbbb 0a 20 20 20 20 72 63 20 3d 20 70 57 61 6c 6b 65  .    rc = pWalke
1fbbc 72 2d 3e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61  r->xSelectCallba
1fbbd 63 6b 28 70 57 61 6c 6b 65 72 2c 20 70 29 3b 0a  ck(pWalker, p);.
1fbbe 20 20 20 20 69 66 28 20 72 63 20 29 20 62 72 65      if( rc ) bre
1fbbf 61 6b 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  ak;.    if( sqli
1fbc0 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 45 78 70  te3WalkSelectExp
1fbc1 72 28 70 57 61 6c 6b 65 72 2c 20 70 29 20 29 20  r(pWalker, p) ) 
1fbc2 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
1fbc3 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
1fbc4 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f 6d 28  3WalkSelectFrom(
1fbc5 70 57 61 6c 6b 65 72 2c 20 70 29 20 29 20 72 65  pWalker, p) ) re
1fbc6 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
1fbc7 20 20 20 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f      p = p->pPrio
1fbc8 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  r;.  }.  return 
1fbc9 72 63 20 26 20 57 52 43 5f 41 62 6f 72 74 3b 0a  rc & WRC_Abort;.
1fbca 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
1fbcb 2a 2a 20 45 6e 64 20 6f 66 20 77 61 6c 6b 65 72  ** End of walker
1fbcc 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1fbcd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbcf 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
1fbd0 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 65  ** Begin file re
1fbd1 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  solve.c ********
1fbd2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbd3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbd4 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75  */./*.** 2008 Au
1fbd5 67 75 73 74 20 31 38 0a 2a 2a 0a 2a 2a 20 54 68  gust 18.**.** Th
1fbd6 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
1fbd7 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
1fbd8 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
1fbd9 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
1fbda 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
1fbdb 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
1fbdc 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
1fbdd 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
1fbde 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
1fbdf 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
1fbe0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
1fbe1 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
1fbe2 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
1fbe3 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
1fbe4 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
1fbe5 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
1fbe6 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
1fbe7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbe8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbe9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1fbeb 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
1fbec 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
1fbed 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66 6f  routines used fo
1fbee 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70 61  r walking the pa
1fbef 72 73 65 72 20 74 72 65 65 20 61 6e 64 0a 2a 2a  rser tree and.**
1fbf0 20 72 65 73 6f 6c 76 65 20 61 6c 6c 20 69 64 65   resolve all ide
1fbf1 6e 74 69 66 69 65 72 73 20 62 79 20 61 73 73 6f  ntifiers by asso
1fbf2 63 69 61 74 69 6e 67 20 74 68 65 6d 20 77 69 74  ciating them wit
1fbf3 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a  h a particular.*
1fbf4 2a 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75  * table and colu
1fbf5 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 72  mn..**.** $Id: r
1fbf6 65 73 6f 6c 76 65 2e 63 2c 76 20 31 2e 33 30 20  esolve.c,v 1.30 
1fbf7 32 30 30 39 2f 30 36 2f 31 35 20 32 33 3a 31 35  2009/06/15 23:15
1fbf8 3a 35 39 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :59 drh Exp $.*/
1fbf9 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 68 65  ../*.** Turn the
1fbfa 20 70 45 78 70 72 20 65 78 70 72 65 73 73 69 6f   pExpr expressio
1fbfb 6e 20 69 6e 74 6f 20 61 6e 20 61 6c 69 61 73 20  n into an alias 
1fbfc 66 6f 72 20 74 68 65 20 69 43 6f 6c 2d 74 68 20  for the iCol-th 
1fbfd 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a  column of the.**
1fbfe 20 72 65 73 75 6c 74 20 73 65 74 20 69 6e 20 70   result set in p
1fbff 45 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  EList..**.** If 
1fc00 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 63  the result set c
1fc01 6f 6c 75 6d 6e 20 69 73 20 61 20 73 69 6d 70 6c  olumn is a simpl
1fc02 65 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e  e column referen
1fc03 63 65 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f  ce, then this ro
1fc04 75 74 69 6e 65 0a 2a 2a 20 6d 61 6b 65 73 20 61  utine.** makes a
1fc05 6e 20 65 78 61 63 74 20 63 6f 70 79 2e 20 20 42  n exact copy.  B
1fc06 75 74 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 72  ut for any other
1fc07 20 6b 69 6e 64 20 6f 66 20 65 78 70 72 65 73 73   kind of express
1fc08 69 6f 6e 2c 20 74 68 69 73 0a 2a 2a 20 72 6f 75  ion, this.** rou
1fc09 74 69 6e 65 20 6d 61 6b 65 20 61 20 63 6f 70 79  tine make a copy
1fc0a 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73   of the result s
1fc0b 65 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65  et column as the
1fc0c 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
1fc0d 0a 2a 2a 20 54 4b 5f 41 53 20 6f 70 65 72 61 74  .** TK_AS operat
1fc0e 6f 72 2e 20 20 54 68 65 20 54 4b 5f 41 53 20 6f  or.  The TK_AS o
1fc0f 70 65 72 61 74 6f 72 20 63 61 75 73 65 73 20 74  perator causes t
1fc10 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f  he expression to
1fc11 20 62 65 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64   be.** evaluated
1fc12 20 6a 75 73 74 20 6f 6e 63 65 20 61 6e 64 20 74   just once and t
1fc13 68 65 6e 20 72 65 75 73 65 64 20 66 6f 72 20 65  hen reused for e
1fc14 61 63 68 20 61 6c 69 61 73 2e 0a 2a 2a 0a 2a 2a  ach alias..**.**
1fc15 20 54 68 65 20 72 65 61 73 6f 6e 20 66 6f 72 20   The reason for 
1fc16 73 75 70 70 72 65 73 73 69 6e 67 20 74 68 65 20  suppressing the 
1fc17 54 4b 5f 41 53 20 74 65 72 6d 20 77 68 65 6e 20  TK_AS term when 
1fc18 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
1fc19 73 20 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 63 6f  s a simple.** co
1fc1a 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 69  lumn reference i
1fc1b 73 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f  s so that the co
1fc1c 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 77  lumn reference w
1fc1d 69 6c 6c 20 62 65 20 72 65 63 6f 67 6e 69 7a 65  ill be recognize
1fc1e 64 20 61 73 0a 2a 2a 20 75 73 61 62 6c 65 20 62  d as.** usable b
1fc1f 79 20 69 6e 64 69 63 65 73 20 77 69 74 68 69 6e  y indices within
1fc20 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
1fc21 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f 67  e processing log
1fc22 69 63 2e 20 0a 2a 2a 0a 2a 2a 20 48 61 63 6b 3a  ic. .**.** Hack:
1fc23 20 20 54 68 65 20 54 4b 5f 41 53 20 6f 70 65 72    The TK_AS oper
1fc24 61 74 6f 72 20 69 73 20 69 6e 68 69 62 69 74 65  ator is inhibite
1fc25 64 20 69 66 20 7a 54 79 70 65 5b 30 5d 3d 3d 27  d if zType[0]=='
1fc26 47 27 2e 20 20 54 68 69 73 20 6d 65 61 6e 73 0a  G'.  This means.
1fc27 2a 2a 20 74 68 61 74 20 69 6e 20 61 20 47 52 4f  ** that in a GRO
1fc28 55 50 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68  UP BY clause, th
1fc29 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
1fc2a 65 76 61 6c 75 61 74 65 64 20 74 77 69 63 65 2e  evaluated twice.
1fc2b 20 20 48 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20 20    Hence:.**.**  
1fc2c 20 20 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d     SELECT random
1fc2d 28 29 25 35 20 41 53 20 78 2c 20 63 6f 75 6e 74  ()%5 AS x, count
1fc2e 28 2a 29 20 46 52 4f 4d 20 74 61 62 20 47 52 4f  (*) FROM tab GRO
1fc2f 55 50 20 42 59 20 78 0a 2a 2a 0a 2a 2a 20 49 73  UP BY x.**.** Is
1fc30 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 3a 0a   equivalent to:.
1fc31 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  **.**     SELECT
1fc32 20 72 61 6e 64 6f 6d 28 29 25 35 20 41 53 20 78   random()%5 AS x
1fc33 2c 20 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20  , count(*) FROM 
1fc34 74 61 62 20 47 52 4f 55 50 20 42 59 20 72 61 6e  tab GROUP BY ran
1fc35 64 6f 6d 28 29 25 35 0a 2a 2a 0a 2a 2a 20 54 68  dom()%5.**.** Th
1fc36 65 20 72 65 73 75 6c 74 20 6f 66 20 72 61 6e 64  e result of rand
1fc37 6f 6d 28 29 25 35 20 69 6e 20 74 68 65 20 47 52  om()%5 in the GR
1fc38 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 69 73  OUP BY clause is
1fc39 20 70 72 6f 62 61 62 6c 79 20 64 69 66 66 65 72   probably differ
1fc3a 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20  ent.** from the 
1fc3b 72 65 73 75 6c 74 20 69 6e 20 74 68 65 20 72 65  result in the re
1fc3c 73 75 6c 74 2d 73 65 74 2e 20 20 57 65 20 6d 69  sult-set.  We mi
1fc3d 67 68 74 20 66 69 78 20 74 68 69 73 20 73 6f 6d  ght fix this som
1fc3e 65 64 61 79 2e 20 20 4f 72 0a 2a 2a 20 74 68 65  eday.  Or.** the
1fc3f 6e 20 61 67 61 69 6e 2c 20 77 65 20 6d 69 67 68  n again, we migh
1fc40 74 20 6e 6f 74 2e 2e 2e 0a 2a 2f 0a 73 74 61 74  t not....*/.stat
1fc41 69 63 20 76 6f 69 64 20 72 65 73 6f 6c 76 65 41  ic void resolveA
1fc42 6c 69 61 73 28 0a 20 20 50 61 72 73 65 20 2a 70  lias(.  Parse *p
1fc43 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f  Parse,         /
1fc44 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
1fc45 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  t */.  ExprList 
1fc46 2a 70 45 4c 69 73 74 2c 20 20 20 20 20 20 2f 2a  *pEList,      /*
1fc47 20 41 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f   A result set */
1fc48 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20 20  .  int iCol,    
1fc49 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 63            /* A c
1fc4a 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73  olumn in the res
1fc4b 75 6c 74 20 73 65 74 2e 20 20 30 2e 2e 70 45 4c  ult set.  0..pEL
1fc4c 69 73 74 2d 3e 6e 45 78 70 72 2d 31 20 2a 2f 0a  ist->nExpr-1 */.
1fc4d 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20    Expr *pExpr,  
1fc4e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 61 6e           /* Tran
1fc4f 73 66 6f 72 6d 20 74 68 69 73 20 69 6e 74 6f 20  sform this into 
1fc50 61 6e 20 61 6c 69 61 73 20 74 6f 20 74 68 65 20  an alias to the 
1fc51 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20  result set */.  
1fc52 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70  const char *zTyp
1fc53 65 20 20 20 20 20 20 2f 2a 20 22 47 52 4f 55 50  e      /* "GROUP
1fc54 22 20 6f 72 20 22 4f 52 44 45 52 22 20 6f 72 20  " or "ORDER" or 
1fc55 22 22 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20  "" */.){.  Expr 
1fc56 2a 70 4f 72 69 67 3b 20 20 20 20 20 20 20 20 20  *pOrig;         
1fc57 20 20 2f 2a 20 54 68 65 20 69 43 6f 6c 2d 74 68    /* The iCol-th
1fc58 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72   column of the r
1fc59 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 45  esult set */.  E
1fc5a 78 70 72 20 2a 70 44 75 70 3b 20 20 20 20 20 20  xpr *pDup;      
1fc5b 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66        /* Copy of
1fc5c 20 70 4f 72 69 67 20 2a 2f 0a 20 20 73 71 6c 69   pOrig */.  sqli
1fc5d 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
1fc5e 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
1fc5f 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
1fc60 0a 0a 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c  ..  assert( iCol
1fc61 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 70 45 4c 69  >=0 && iCol<pELi
1fc62 73 74 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 70  st->nExpr );.  p
1fc63 4f 72 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61  Orig = pEList->a
1fc64 5b 69 43 6f 6c 5d 2e 70 45 78 70 72 3b 0a 20 20  [iCol].pExpr;.  
1fc65 61 73 73 65 72 74 28 20 70 4f 72 69 67 21 3d 30  assert( pOrig!=0
1fc66 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
1fc67 72 69 67 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  rig->flags & EP_
1fc68 52 65 73 6f 6c 76 65 64 20 29 3b 0a 20 20 64 62  Resolved );.  db
1fc69 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
1fc6a 20 69 66 28 20 70 4f 72 69 67 2d 3e 6f 70 21 3d   if( pOrig->op!=
1fc6b 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 7a 54 79  TK_COLUMN && zTy
1fc6c 70 65 5b 30 5d 21 3d 27 47 27 20 29 7b 0a 20 20  pe[0]!='G' ){.  
1fc6d 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33    pDup = sqlite3
1fc6e 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 69  ExprDup(db, pOri
1fc6f 67 2c 20 30 29 3b 0a 20 20 20 20 70 44 75 70 20  g, 0);.    pDup 
1fc70 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
1fc71 50 61 72 73 65 2c 20 54 4b 5f 41 53 2c 20 70 44  Parse, TK_AS, pD
1fc72 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69  up, 0, 0);.    i
1fc73 66 28 20 70 44 75 70 3d 3d 30 20 29 20 72 65 74  f( pDup==0 ) ret
1fc74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 70 45 4c  urn;.    if( pEL
1fc75 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c  ist->a[iCol].iAl
1fc76 69 61 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ias==0 ){.      
1fc77 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e  pEList->a[iCol].
1fc78 69 41 6c 69 61 73 20 3d 20 28 75 31 36 29 28 2b  iAlias = (u16)(+
1fc79 2b 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 29  +pParse->nAlias)
1fc7a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 44 75 70  ;.    }.    pDup
1fc7b 2d 3e 69 54 61 62 6c 65 20 3d 20 70 45 4c 69 73  ->iTable = pELis
1fc7c 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c 69 61  t->a[iCol].iAlia
1fc7d 73 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 45  s;.  }else if( E
1fc7e 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
1fc7f 4f 72 69 67 2c 20 45 50 5f 49 6e 74 56 61 6c 75  Orig, EP_IntValu
1fc80 65 29 20 7c 7c 20 70 4f 72 69 67 2d 3e 75 2e 7a  e) || pOrig->u.z
1fc81 54 6f 6b 65 6e 3d 3d 30 20 29 7b 0a 20 20 20 20  Token==0 ){.    
1fc82 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78  pDup = sqlite3Ex
1fc83 70 72 44 75 70 28 64 62 2c 20 70 4f 72 69 67 2c  prDup(db, pOrig,
1fc84 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 44 75   0);.    if( pDu
1fc85 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  p==0 ) return;. 
1fc86 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72   }else{.    char
1fc87 20 2a 7a 54 6f 6b 65 6e 20 3d 20 70 4f 72 69 67   *zToken = pOrig
1fc88 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20  ->u.zToken;.    
1fc89 61 73 73 65 72 74 28 20 7a 54 6f 6b 65 6e 21 3d  assert( zToken!=
1fc8a 30 20 29 3b 0a 20 20 20 20 70 4f 72 69 67 2d 3e  0 );.    pOrig->
1fc8b 75 2e 7a 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20  u.zToken = 0;.  
1fc8c 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33    pDup = sqlite3
1fc8d 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 72 69  ExprDup(db, pOri
1fc8e 67 2c 20 30 29 3b 0a 20 20 20 20 70 4f 72 69 67  g, 0);.    pOrig
1fc8f 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 3d 20 7a 54 6f  ->u.zToken = zTo
1fc90 6b 65 6e 3b 0a 20 20 20 20 69 66 28 20 70 44 75  ken;.    if( pDu
1fc91 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  p==0 ) return;. 
1fc92 20 20 20 61 73 73 65 72 74 28 20 28 70 44 75 70     assert( (pDup
1fc93 2d 3e 66 6c 61 67 73 20 26 20 28 45 50 5f 52 65  ->flags & (EP_Re
1fc94 64 75 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e  duced|EP_TokenOn
1fc95 6c 79 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70  ly))==0 );.    p
1fc96 44 75 70 2d 3e 66 6c 61 67 73 32 20 7c 3d 20 45  Dup->flags2 |= E
1fc97 50 32 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e  P2_MallocedToken
1fc98 3b 0a 20 20 20 20 70 44 75 70 2d 3e 75 2e 7a 54  ;.    pDup->u.zT
1fc99 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33 44 62  oken = sqlite3Db
1fc9a 53 74 72 44 75 70 28 64 62 2c 20 7a 54 6f 6b 65  StrDup(db, zToke
1fc9b 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 45  n);.  }.  if( pE
1fc9c 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  xpr->flags & EP_
1fc9d 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20  ExpCollate ){.  
1fc9e 20 20 70 44 75 70 2d 3e 70 43 6f 6c 6c 20 3d 20    pDup->pColl = 
1fc9f 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 3b 0a 20 20  pExpr->pColl;.  
1fca0 20 20 70 44 75 70 2d 3e 66 6c 61 67 73 20 7c 3d    pDup->flags |=
1fca1 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b 0a   EP_ExpCollate;.
1fca2 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70    }.  sqlite3Exp
1fca3 72 43 6c 65 61 72 28 64 62 2c 20 70 45 78 70 72  rClear(db, pExpr
1fca4 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 45 78 70  );.  memcpy(pExp
1fca5 72 2c 20 70 44 75 70 2c 20 73 69 7a 65 6f 66 28  r, pDup, sizeof(
1fca6 2a 70 45 78 70 72 29 29 3b 0a 20 20 73 71 6c 69  *pExpr));.  sqli
1fca7 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 44  te3DbFree(db, pD
1fca8 75 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69  up);.}../*.** Gi
1fca9 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  ven the name of 
1fcaa 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  a column of the 
1fcab 66 6f 72 6d 20 58 2e 59 2e 5a 20 6f 72 20 59 2e  form X.Y.Z or Y.
1fcac 5a 20 6f 72 20 6a 75 73 74 20 5a 2c 20 6c 6f 6f  Z or just Z, loo
1fcad 6b 20 75 70 0a 2a 2a 20 74 68 61 74 20 6e 61 6d  k up.** that nam
1fcae 65 20 69 6e 20 74 68 65 20 73 65 74 20 6f 66 20  e in the set of 
1fcaf 73 6f 75 72 63 65 20 74 61 62 6c 65 73 20 69 6e  source tables in
1fcb0 20 70 53 72 63 4c 69 73 74 20 61 6e 64 20 6d 61   pSrcList and ma
1fcb1 6b 65 20 74 68 65 20 70 45 78 70 72 20 0a 2a 2a  ke the pExpr .**
1fcb2 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65   expression node
1fcb3 20 72 65 66 65 72 20 62 61 63 6b 20 74 6f 20 74   refer back to t
1fcb4 68 61 74 20 73 6f 75 72 63 65 20 63 6f 6c 75 6d  hat source colum
1fcb5 6e 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  n.  The followin
1fcb6 67 20 63 68 61 6e 67 65 73 0a 2a 2a 20 61 72 65  g changes.** are
1fcb7 20 6d 61 64 65 20 74 6f 20 70 45 78 70 72 3a 0a   made to pExpr:.
1fcb8 2a 2a 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e  **.**    pExpr->
1fcb9 69 44 62 20 20 20 20 20 20 20 20 20 20 20 53 65  iDb           Se
1fcba 74 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20 64  t the index in d
1fcbb 62 2d 3e 61 44 62 5b 5d 20 6f 66 20 74 68 65 20  b->aDb[] of the 
1fcbc 64 61 74 61 62 61 73 65 20 58 0a 2a 2a 20 20 20  database X.**   
1fcbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fcbe 20 20 20 20 20 20 28 65 76 65 6e 20 69 66 20 58        (even if X
1fcbf 20 69 73 20 69 6d 70 6c 69 65 64 29 2e 0a 2a 2a   is implied)..**
1fcc0 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c      pExpr->iTabl
1fcc1 65 20 20 20 20 20 20 20 20 53 65 74 20 74 6f 20  e        Set to 
1fcc2 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  the cursor numbe
1fcc3 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  r for the table 
1fcc4 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 20 20 20 20  obtained.**     
1fcc5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fcc6 20 20 20 20 66 72 6f 6d 20 70 53 72 63 4c 69 73      from pSrcLis
1fcc7 74 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e  t..**    pExpr->
1fcc8 70 54 61 62 20 20 20 20 20 20 20 20 20 20 50 6f  pTab          Po
1fcc9 69 6e 74 73 20 74 6f 20 74 68 65 20 54 61 62 6c  ints to the Tabl
1fcca 65 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 58  e structure of X
1fccb 2e 59 20 28 65 76 65 6e 20 69 66 0a 2a 2a 20 20  .Y (even if.**  
1fccc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fccd 20 20 20 20 20 20 20 58 20 61 6e 64 2f 6f 72 20         X and/or 
1fcce 59 20 61 72 65 20 69 6d 70 6c 69 65 64 2e 29 0a  Y are implied.).
1fccf 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f  **    pExpr->iCo
1fcd0 6c 75 6d 6e 20 20 20 20 20 20 20 53 65 74 20 74  lumn       Set t
1fcd1 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d  o the column num
1fcd2 62 65 72 20 77 69 74 68 69 6e 20 74 68 65 20 74  ber within the t
1fcd3 61 62 6c 65 2e 0a 2a 2a 20 20 20 20 70 45 78 70  able..**    pExp
1fcd4 72 2d 3e 6f 70 20 20 20 20 20 20 20 20 20 20 20  r->op           
1fcd5 20 53 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d   Set to TK_COLUM
1fcd6 4e 2e 0a 2a 2a 20 20 20 20 70 45 78 70 72 2d 3e  N..**    pExpr->
1fcd7 70 4c 65 66 74 20 20 20 20 20 20 20 20 20 41 6e  pLeft         An
1fcd8 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 69  y expression thi
1fcd9 73 20 70 6f 69 6e 74 73 20 74 6f 20 69 73 20 64  s points to is d
1fcda 65 6c 65 74 65 64 0a 2a 2a 20 20 20 20 70 45 78  eleted.**    pEx
1fcdb 70 72 2d 3e 70 52 69 67 68 74 20 20 20 20 20 20  pr->pRight      
1fcdc 20 20 41 6e 79 20 65 78 70 72 65 73 73 69 6f 6e    Any expression
1fcdd 20 74 68 69 73 20 70 6f 69 6e 74 73 20 74 6f 20   this points to 
1fcde 69 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a  is deleted..**.*
1fcdf 2a 20 54 68 65 20 7a 44 62 20 76 61 72 69 61 62  * The zDb variab
1fce0 6c 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f  le is the name o
1fce1 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 28  f the database (
1fce2 74 68 65 20 22 58 22 29 2e 20 20 54 68 69 73 20  the "X").  This 
1fce3 76 61 6c 75 65 20 6d 61 79 20 62 65 0a 2a 2a 20  value may be.** 
1fce4 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67 20 74 68 61  NULL meaning tha
1fce5 74 20 6e 61 6d 65 20 69 73 20 6f 66 20 74 68 65  t name is of the
1fce6 20 66 6f 72 6d 20 59 2e 5a 20 6f 72 20 5a 2e 20   form Y.Z or Z. 
1fce7 20 41 6e 79 20 61 76 61 69 6c 61 62 6c 65 20 64   Any available d
1fce8 61 74 61 62 61 73 65 0a 2a 2a 20 63 61 6e 20 62  atabase.** can b
1fce9 65 20 75 73 65 64 2e 20 20 54 68 65 20 7a 54 61  e used.  The zTa
1fcea 62 6c 65 20 76 61 72 69 61 62 6c 65 20 69 73 20  ble variable is 
1fceb 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
1fcec 74 61 62 6c 65 20 28 74 68 65 20 22 59 22 29 2e  table (the "Y").
1fced 20 20 54 68 69 73 0a 2a 2a 20 76 61 6c 75 65 20    This.** value 
1fcee 63 61 6e 20 62 65 20 4e 55 4c 4c 20 69 66 20 7a  can be NULL if z
1fcef 44 62 20 69 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e  Db is also NULL.
1fcf0 20 20 49 66 20 7a 54 61 62 6c 65 20 69 73 20 4e    If zTable is N
1fcf1 55 4c 4c 20 69 74 0a 2a 2a 20 6d 65 61 6e 73 20  ULL it.** means 
1fcf2 74 68 61 74 20 74 68 65 20 66 6f 72 6d 20 6f 66  that the form of
1fcf3 20 74 68 65 20 6e 61 6d 65 20 69 73 20 5a 20 61   the name is Z a
1fcf4 6e 64 20 74 68 61 74 20 63 6f 6c 75 6d 6e 73 20  nd that columns 
1fcf5 66 72 6f 6d 20 61 6e 79 20 74 61 62 6c 65 0a 2a  from any table.*
1fcf6 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a 2a  * can be used..*
1fcf7 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6e 61 6d 65  *.** If the name
1fcf8 20 63 61 6e 6e 6f 74 20 62 65 20 72 65 73 6f 6c   cannot be resol
1fcf9 76 65 64 20 75 6e 61 6d 62 69 67 75 6f 75 73 6c  ved unambiguousl
1fcfa 79 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72 6f  y, leave an erro
1fcfb 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20 69 6e 20  r message.** in 
1fcfc 70 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 72  pParse and retur
1fcfd 6e 20 57 52 43 5f 41 62 6f 72 74 2e 20 20 52 65  n WRC_Abort.  Re
1fcfe 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 20 6f  turn WRC_Prune o
1fcff 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74  n success..*/.st
1fd00 61 74 69 63 20 69 6e 74 20 6c 6f 6f 6b 75 70 4e  atic int lookupN
1fd01 61 6d 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ame(.  Parse *pP
1fd02 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 54  arse,       /* T
1fd03 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65  he parsing conte
1fd04 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  xt */.  const ch
1fd05 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 2f 2a 20  ar *zDb,     /* 
1fd06 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  Name of the data
1fd07 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
1fd08 74 61 62 6c 65 2c 20 6f 72 20 4e 55 4c 4c 20 2a  table, or NULL *
1fd09 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1fd0a 7a 54 61 62 2c 20 20 20 20 2f 2a 20 4e 61 6d 65  zTab,    /* Name
1fd0b 20 6f 66 20 74 61 62 6c 65 20 63 6f 6e 74 61 69   of table contai
1fd0c 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2c 20 6f 72 20  ning column, or 
1fd0d 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  NULL */.  const 
1fd0e 63 68 61 72 20 2a 7a 43 6f 6c 2c 20 20 20 20 2f  char *zCol,    /
1fd0f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f  * Name of the co
1fd10 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43  lumn. */.  NameC
1fd11 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 20 20 20  ontext *pNC,    
1fd12 2f 2a 20 54 68 65 20 6e 61 6d 65 20 63 6f 6e 74  /* The name cont
1fd13 65 78 74 20 75 73 65 64 20 74 6f 20 72 65 73 6f  ext used to reso
1fd14 6c 76 65 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a  lve the name */.
1fd15 20 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 20    Expr *pExpr   
1fd16 20 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 74         /* Make t
1fd17 68 69 73 20 45 58 50 52 20 6e 6f 64 65 20 70 6f  his EXPR node po
1fd18 69 6e 74 20 74 6f 20 74 68 65 20 73 65 6c 65 63  int to the selec
1fd19 74 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 29 7b  ted column */.){
1fd1a 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20  .  int i, j;    
1fd1b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
1fd1c 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e  counters */.  in
1fd1d 74 20 63 6e 74 20 3d 20 30 3b 20 20 20 20 20 20  t cnt = 0;      
1fd1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fd1f 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61 74  /* Number of mat
1fd20 63 68 69 6e 67 20 63 6f 6c 75 6d 6e 20 6e 61 6d  ching column nam
1fd21 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 54  es */.  int cntT
1fd22 61 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ab = 0;         
1fd23 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1fd24 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e 67 20  ber of matching 
1fd25 74 61 62 6c 65 20 6e 61 6d 65 73 20 2a 2f 0a 20  table names */. 
1fd26 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
1fd27 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20  Parse->db;      
1fd28 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
1fd29 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
1fd2a 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
1fd2b 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20  t_item *pItem;  
1fd2c 20 20 20 20 20 2f 2a 20 55 73 65 20 66 6f 72 20       /* Use for 
1fd2d 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 70 53 72  looping over pSr
1fd2e 63 4c 69 73 74 20 69 74 65 6d 73 20 2a 2f 0a 20  cList items */. 
1fd2f 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
1fd30 69 74 65 6d 20 2a 70 4d 61 74 63 68 20 3d 20 30  item *pMatch = 0
1fd31 3b 20 20 2f 2a 20 54 68 65 20 6d 61 74 63 68 69  ;  /* The matchi
1fd32 6e 67 20 70 53 72 63 4c 69 73 74 20 69 74 65 6d  ng pSrcList item
1fd33 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78   */.  NameContex
1fd34 74 20 2a 70 54 6f 70 4e 43 20 3d 20 70 4e 43 3b  t *pTopNC = pNC;
1fd35 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
1fd36 20 6e 61 6d 65 63 6f 6e 74 65 78 74 20 69 6e 20   namecontext in 
1fd37 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 53 63  the list */.  Sc
1fd38 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20  hema *pSchema = 
1fd39 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
1fd3a 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20 74 68 65  /* Schema of the
1fd3b 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
1fd3c 20 69 6e 74 20 69 73 54 72 69 67 67 65 72 20 3d   int isTrigger =
1fd3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70   0;..  assert( p
1fd3e 4e 43 20 29 3b 20 20 20 20 20 2f 2a 20 74 68 65  NC );     /* the
1fd3f 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20 63 61   name context ca
1fd40 6e 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 20 2a 2f  nnot be NULL. */
1fd41 0a 20 20 61 73 73 65 72 74 28 20 7a 43 6f 6c 20  .  assert( zCol 
1fd42 29 3b 20 20 20 20 2f 2a 20 54 68 65 20 5a 20 69  );    /* The Z i
1fd43 6e 20 58 2e 59 2e 5a 20 63 61 6e 6e 6f 74 20 62  n X.Y.Z cannot b
1fd44 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 61 73 73 65  e NULL */.  asse
1fd45 72 74 28 20 7e 45 78 70 72 48 61 73 41 6e 79 50  rt( ~ExprHasAnyP
1fd46 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
1fd47 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52  P_TokenOnly|EP_R
1fd48 65 64 75 63 65 64 29 20 29 3b 0a 0a 20 20 2f 2a  educed) );..  /*
1fd49 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20   Initialize the 
1fd4a 6e 6f 64 65 20 74 6f 20 6e 6f 2d 6d 61 74 63 68  node to no-match
1fd4b 20 2a 2f 0a 20 20 70 45 78 70 72 2d 3e 69 54 61   */.  pExpr->iTa
1fd4c 62 6c 65 20 3d 20 2d 31 3b 0a 20 20 70 45 78 70  ble = -1;.  pExp
1fd4d 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20 45  r->pTab = 0;.  E
1fd4e 78 70 72 53 65 74 49 72 72 65 64 75 63 69 62 6c  xprSetIrreducibl
1fd4f 65 28 70 45 78 70 72 29 3b 0a 0a 20 20 2f 2a 20  e(pExpr);..  /* 
1fd50 53 74 61 72 74 20 61 74 20 74 68 65 20 69 6e 6e  Start at the inn
1fd51 65 72 2d 6d 6f 73 74 20 63 6f 6e 74 65 78 74 20  er-most context 
1fd52 61 6e 64 20 6d 6f 76 65 20 6f 75 74 77 61 72 64  and move outward
1fd53 20 75 6e 74 69 6c 20 61 20 6d 61 74 63 68 20 69   until a match i
1fd54 73 20 66 6f 75 6e 64 20 2a 2f 0a 20 20 77 68 69  s found */.  whi
1fd55 6c 65 28 20 70 4e 43 20 26 26 20 63 6e 74 3d 3d  le( pNC && cnt==
1fd56 30 20 29 7b 0a 20 20 20 20 45 78 70 72 4c 69 73  0 ){.    ExprLis
1fd57 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 20 20 53  t *pEList;.    S
1fd58 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74  rcList *pSrcList
1fd59 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74   = pNC->pSrcList
1fd5a 3b 0a 0a 20 20 20 20 69 66 28 20 70 53 72 63 4c  ;..    if( pSrcL
1fd5b 69 73 74 20 29 7b 0a 20 20 20 20 20 20 66 6f 72  ist ){.      for
1fd5c 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 53 72 63  (i=0, pItem=pSrc
1fd5d 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 53 72 63 4c  List->a; i<pSrcL
1fd5e 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20  ist->nSrc; i++, 
1fd5f 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
1fd60 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
1fd61 20 20 20 20 20 20 20 69 6e 74 20 69 44 62 3b 0a         int iDb;.
1fd62 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 2a          Column *
1fd63 70 43 6f 6c 3b 0a 20 20 0a 20 20 20 20 20 20 20  pCol;.  .       
1fd64 20 70 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e 70   pTab = pItem->p
1fd65 54 61 62 3b 0a 20 20 20 20 20 20 20 20 61 73 73  Tab;.        ass
1fd66 65 72 74 28 20 70 54 61 62 21 3d 30 20 26 26 20  ert( pTab!=0 && 
1fd67 70 54 61 62 2d 3e 7a 4e 61 6d 65 21 3d 30 20 29  pTab->zName!=0 )
1fd68 3b 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d 20  ;.        iDb = 
1fd69 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
1fd6a 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e 70  ndex(db, pTab->p
1fd6b 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20 20  Schema);.       
1fd6c 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e 6e   assert( pTab->n
1fd6d 43 6f 6c 3e 30 20 29 3b 0a 20 20 20 20 20 20 20  Col>0 );.       
1fd6e 20 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 20 20   if( zTab ){.   
1fd6f 20 20 20 20 20 20 20 69 66 28 20 70 49 74 65 6d         if( pItem
1fd70 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20 20 20 20  ->zAlias ){.    
1fd71 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54          char *zT
1fd72 61 62 4e 61 6d 65 20 3d 20 70 49 74 65 6d 2d 3e  abName = pItem->
1fd73 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20 20 20 20  zAlias;.        
1fd74 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
1fd75 74 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 2c  trICmp(zTabName,
1fd76 20 7a 54 61 62 29 21 3d 30 20 29 20 63 6f 6e 74   zTab)!=0 ) cont
1fd77 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20  inue;.          
1fd78 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
1fd79 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d     char *zTabNam
1fd7a 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b  e = pTab->zName;
1fd7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
1fd7c 20 4e 45 56 45 52 28 7a 54 61 62 4e 61 6d 65 3d   NEVER(zTabName=
1fd7d 3d 30 29 20 7c 7c 20 73 71 6c 69 74 65 33 53 74  =0) || sqlite3St
1fd7e 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 2c 20  rICmp(zTabName, 
1fd7f 7a 54 61 62 29 21 3d 30 20 29 7b 0a 20 20 20 20  zTab)!=0 ){.    
1fd80 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e            contin
1fd81 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ue;.            
1fd82 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  }.            if
1fd83 28 20 7a 44 62 21 3d 30 20 26 26 20 73 71 6c 69  ( zDb!=0 && sqli
1fd84 74 65 33 53 74 72 49 43 6d 70 28 64 62 2d 3e 61  te3StrICmp(db->a
1fd85 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a  Db[iDb].zName, z
1fd86 44 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  Db)!=0 ){.      
1fd87 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65          continue
1fd88 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
1fd89 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1fd8a 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
1fd8b 28 20 30 3d 3d 28 63 6e 74 54 61 62 2b 2b 29 20  ( 0==(cntTab++) 
1fd8c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 45 78  ){.          pEx
1fd8d 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 74  pr->iTable = pIt
1fd8e 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20  em->iCursor;.   
1fd8f 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 54         pExpr->pT
1fd90 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 20 20 20  ab = pTab;.     
1fd91 20 20 20 20 20 70 53 63 68 65 6d 61 20 3d 20 70       pSchema = p
1fd92 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20  Tab->pSchema;.  
1fd93 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 20 3d          pMatch =
1fd94 20 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 20 20   pItem;.        
1fd95 7d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  }.        for(j=
1fd96 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d 3e 61 43  0, pCol=pTab->aC
1fd97 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c  ol; j<pTab->nCol
1fd98 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a  ; j++, pCol++){.
1fd99 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71            if( sq
1fd9a 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f  lite3StrICmp(pCo
1fd9b 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d  l->zName, zCol)=
1fd9c 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
1fd9d 20 20 49 64 4c 69 73 74 20 2a 70 55 73 69 6e 67    IdList *pUsing
1fd9e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 6e  ;.            cn
1fd9f 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  t++;.           
1fda0 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d   pExpr->iTable =
1fda1 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b   pItem->iCursor;
1fda2 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78  .            pEx
1fda3 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 3b  pr->pTab = pTab;
1fda4 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 4d 61  .            pMa
1fda5 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a 20 20 20  tch = pItem;.   
1fda6 20 20 20 20 20 20 20 20 20 70 53 63 68 65 6d 61           pSchema
1fda7 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61   = pTab->pSchema
1fda8 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;.            /*
1fda9 20 53 75 62 73 74 69 74 75 74 65 20 74 68 65 20   Substitute the 
1fdaa 72 6f 77 69 64 20 28 63 6f 6c 75 6d 6e 20 2d 31  rowid (column -1
1fdab 29 20 66 6f 72 20 74 68 65 20 49 4e 54 45 47 45  ) for the INTEGE
1fdac 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a 2f  R PRIMARY KEY */
1fdad 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78  .            pEx
1fdae 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 6a 3d  pr->iColumn = j=
1fdaf 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 3f 20 2d  =pTab->iPKey ? -
1fdb0 31 20 3a 20 28 69 31 36 29 6a 3b 0a 20 20 20 20  1 : (i16)j;.    
1fdb1 20 20 20 20 20 20 20 20 69 66 28 20 69 3c 70 53          if( i<pS
1fdb2 72 63 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 20 29  rcList->nSrc-1 )
1fdb3 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
1fdb4 69 66 28 20 70 49 74 65 6d 5b 31 5d 2e 6a 6f 69  if( pItem[1].joi
1fdb5 6e 74 79 70 65 20 26 20 4a 54 5f 4e 41 54 55 52  ntype & JT_NATUR
1fdb6 41 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  AL ){.          
1fdb7 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73        /* If this
1fdb8 20 6d 61 74 63 68 20 6f 63 63 75 72 72 65 64 20   match occurred 
1fdb9 69 6e 20 74 68 65 20 6c 65 66 74 20 74 61 62 6c  in the left tabl
1fdba 65 20 6f 66 20 61 20 6e 61 74 75 72 61 6c 20 6a  e of a natural j
1fdbb 6f 69 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20  oin,.           
1fdbc 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 73 6b 69       ** then ski
1fdbd 70 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c  p the right tabl
1fdbe 65 20 74 6f 20 61 76 6f 69 64 20 61 20 64 75 70  e to avoid a dup
1fdbf 6c 69 63 61 74 65 20 6d 61 74 63 68 20 2a 2f 0a  licate match */.
1fdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fdc1 70 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20  pItem++;.       
1fdc2 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20           i++;.  
1fdc3 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73              }els
1fdc4 65 20 69 66 28 20 28 70 55 73 69 6e 67 20 3d 20  e if( (pUsing = 
1fdc5 70 49 74 65 6d 5b 31 5d 2e 70 55 73 69 6e 67 29  pItem[1].pUsing)
1fdc6 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  !=0 ){.         
1fdc7 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69         /* If thi
1fdc8 73 20 6d 61 74 63 68 20 6f 63 63 75 72 73 20 6f  s match occurs o
1fdc9 6e 20 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20  n a column that 
1fdca 69 73 20 69 6e 20 74 68 65 20 55 53 49 4e 47 20  is in the USING 
1fdcb 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20 20  clause.         
1fdcc 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 61 20 6a         ** of a j
1fdcd 6f 69 6e 2c 20 73 6b 69 70 20 74 68 65 20 73 65  oin, skip the se
1fdce 61 72 63 68 20 6f 66 20 74 68 65 20 72 69 67 68  arch of the righ
1fdcf 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20 6a  t table of the j
1fdd0 6f 69 6e 0a 20 20 20 20 20 20 20 20 20 20 20 20  oin.            
1fdd1 20 20 20 20 2a 2a 20 74 6f 20 61 76 6f 69 64 20      ** to avoid 
1fdd2 61 20 64 75 70 6c 69 63 61 74 65 20 6d 61 74 63  a duplicate matc
1fdd3 68 20 74 68 65 72 65 2e 20 2a 2f 0a 20 20 20 20  h there. */.    
1fdd4 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
1fdd5 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  k;.             
1fdd6 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 55     for(k=0; k<pU
1fdd7 73 69 6e 67 2d 3e 6e 49 64 3b 20 6b 2b 2b 29 7b  sing->nId; k++){
1fdd8 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1fdd9 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
1fdda 72 49 43 6d 70 28 70 55 73 69 6e 67 2d 3e 61 5b  rICmp(pUsing->a[
1fddb 6b 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d  k].zName, zCol)=
1fddc 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
1fddd 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2b            pItem+
1fdde 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  +;.             
1fddf 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20         i++;.    
1fde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fde1 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
1fde2 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1fde3 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
1fde4 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
1fde5 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1fde6 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1fde7 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1fde8 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1fde9 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
1fdea 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20  E_OMIT_TRIGGER. 
1fdeb 20 20 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65     /* If we have
1fdec 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 72 65 73   not already res
1fded 6f 6c 76 65 64 20 74 68 65 20 6e 61 6d 65 2c 20  olved the name, 
1fdee 74 68 65 6e 20 6d 61 79 62 65 20 0a 20 20 20 20  then maybe .    
1fdef 2a 2a 20 69 74 20 69 73 20 61 20 6e 65 77 2e 2a  ** it is a new.*
1fdf0 20 6f 72 20 6f 6c 64 2e 2a 20 74 72 69 67 67 65   or old.* trigge
1fdf1 72 20 61 72 67 75 6d 65 6e 74 20 72 65 66 65 72  r argument refer
1fdf2 65 6e 63 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ence.    */.    
1fdf3 69 66 28 20 7a 44 62 3d 3d 30 20 26 26 20 7a 54  if( zDb==0 && zT
1fdf4 61 62 21 3d 30 20 26 26 20 63 6e 74 3d 3d 30 20  ab!=0 && cnt==0 
1fdf5 26 26 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67  && pParse->pTrig
1fdf6 67 65 72 54 61 62 21 3d 30 20 29 7b 0a 20 20 20  gerTab!=0 ){.   
1fdf7 20 20 20 69 6e 74 20 6f 70 20 3d 20 70 50 61 72     int op = pPar
1fdf8 73 65 2d 3e 65 54 72 69 67 67 65 72 4f 70 3b 0a  se->eTriggerOp;.
1fdf9 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61        Table *pTa
1fdfa 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 61 73 73  b = 0;.      ass
1fdfb 65 72 74 28 20 6f 70 3d 3d 54 4b 5f 44 45 4c 45  ert( op==TK_DELE
1fdfc 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55 50 44  TE || op==TK_UPD
1fdfd 41 54 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 4e  ATE || op==TK_IN
1fdfe 53 45 52 54 20 29 3b 0a 20 20 20 20 20 20 69 66  SERT );.      if
1fdff 28 20 6f 70 21 3d 54 4b 5f 44 45 4c 45 54 45 20  ( op!=TK_DELETE 
1fe00 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  && sqlite3StrICm
1fe01 70 28 22 6e 65 77 22 2c 7a 54 61 62 29 20 3d 3d  p("new",zTab) ==
1fe02 20 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 45   0 ){.        pE
1fe03 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 31 3b  xpr->iTable = 1;
1fe04 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20  .        pTab = 
1fe05 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72  pParse->pTrigger
1fe06 54 61 62 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  Tab;.      }else
1fe07 20 69 66 28 20 6f 70 21 3d 54 4b 5f 49 4e 53 45   if( op!=TK_INSE
1fe08 52 54 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  RT && sqlite3Str
1fe09 49 43 6d 70 28 22 6f 6c 64 22 2c 7a 54 61 62 29  ICmp("old",zTab)
1fe0a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ==0 ){.        p
1fe0b 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 30  Expr->iTable = 0
1fe0c 3b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20 3d  ;.        pTab =
1fe0d 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65   pParse->pTrigge
1fe0e 72 54 61 62 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  rTab;.      }.. 
1fe0f 20 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b       if( pTab ){
1fe10 20 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 43   .        int iC
1fe11 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 53 63 68  ol;.        pSch
1fe12 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68  ema = pTab->pSch
1fe13 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 63 6e 74  ema;.        cnt
1fe14 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69  Tab++;.        i
1fe15 66 28 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69  f( sqlite3IsRowi
1fe16 64 28 7a 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20  d(zCol) ){.     
1fe17 20 20 20 20 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a       iCol = -1;.
1fe18 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
1fe19 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 43 6f           for(iCo
1fe1a 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e  l=0; iCol<pTab->
1fe1b 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20  nCol; iCol++){. 
1fe1c 20 20 20 20 20 20 20 20 20 20 20 43 6f 6c 75 6d             Colum
1fe1d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 54 61 62 2d  n *pCol = &pTab-
1fe1e 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20  >aCol[iCol];.   
1fe1f 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c           if( sql
1fe20 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c  ite3StrICmp(pCol
1fe21 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d  ->zName, zCol)==
1fe22 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
1fe23 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 70 54 61     if( iCol==pTa
1fe24 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20  b->iPKey ){.    
1fe25 20 20 20 20 20 20 20 20 20 20 20 20 69 43 6f 6c              iCol
1fe26 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 20   = -1;.         
1fe27 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
1fe28 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1fe29 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1fe2a 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
1fe2b 20 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c          if( iCol
1fe2c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20  <pTab->nCol ){. 
1fe2d 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a           cnt++;.
1fe2e 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 43            if( iC
1fe2f 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ol<0 ){.        
1fe30 20 20 20 20 70 45 78 70 72 2d 3e 61 66 66 69 6e      pExpr->affin
1fe31 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  ity = SQLITE_AFF
1fe32 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20  _INTEGER;.      
1fe33 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 45      }else if( pE
1fe34 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 30 20 29  xpr->iTable==0 )
1fe35 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74 65  {.            te
1fe36 73 74 63 61 73 65 28 20 69 43 6f 6c 3d 3d 33 31  stcase( iCol==31
1fe37 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
1fe38 74 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d 3d  testcase( iCol==
1fe39 33 32 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  32 );.          
1fe3a 20 20 70 50 61 72 73 65 2d 3e 6f 6c 64 6d 61 73    pParse->oldmas
1fe3b 6b 20 7c 3d 20 28 69 43 6f 6c 3e 3d 33 32 20 3f  k |= (iCol>=32 ?
1fe3c 20 30 78 66 66 66 66 66 66 66 66 20 3a 20 28 28   0xffffffff : ((
1fe3d 28 75 33 32 29 31 29 3c 3c 69 43 6f 6c 29 29 3b  (u32)1)<<iCol));
1fe3e 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1fe3f 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43         pExpr->iC
1fe40 6f 6c 75 6d 6e 20 3d 20 28 69 31 36 29 69 43 6f  olumn = (i16)iCo
1fe41 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 70 45 78  l;.          pEx
1fe42 70 72 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 3b  pr->pTab = pTab;
1fe43 0a 20 20 20 20 20 20 20 20 20 20 69 73 54 72 69  .          isTri
1fe44 67 67 65 72 20 3d 20 31 3b 0a 20 20 20 20 20 20  gger = 1;.      
1fe45 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
1fe46 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  }.#endif /* !def
1fe47 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1fe48 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a 20 20  _TRIGGER) */..  
1fe49 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 50 65 72 68    /*.    ** Perh
1fe4a 61 70 73 20 74 68 65 20 6e 61 6d 65 20 69 73 20  aps the name is 
1fe4b 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74  a reference to t
1fe4c 68 65 20 52 4f 57 49 44 0a 20 20 20 20 2a 2f 0a  he ROWID.    */.
1fe4d 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 26      if( cnt==0 &
1fe4e 26 20 63 6e 74 54 61 62 3d 3d 31 20 26 26 20 73  & cntTab==1 && s
1fe4f 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 7a 43  qlite3IsRowid(zC
1fe50 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 63 6e 74  ol) ){.      cnt
1fe51 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 45 78 70   = 1;.      pExp
1fe52 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b  r->iColumn = -1;
1fe53 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 61 66  .      pExpr->af
1fe54 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f  finity = SQLITE_
1fe55 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  AFF_INTEGER;.   
1fe56 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a   }..    /*.    *
1fe57 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 69  * If the input i
1fe58 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 5a 20  s of the form Z 
1fe59 28 6e 6f 74 20 59 2e 5a 20 6f 72 20 58 2e 59 2e  (not Y.Z or X.Y.
1fe5a 5a 29 20 74 68 65 6e 20 74 68 65 20 6e 61 6d 65  Z) then the name
1fe5b 20 5a 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 20   Z.    ** might 
1fe5c 72 65 66 65 72 20 74 6f 20 61 6e 20 72 65 73 75  refer to an resu
1fe5d 6c 74 2d 73 65 74 20 61 6c 69 61 73 2e 20 20 54  lt-set alias.  T
1fe5e 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66 6f 72  his happens, for
1fe5f 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 0a 20   example, when. 
1fe60 20 20 20 2a 2a 20 77 65 20 61 72 65 20 72 65 73     ** we are res
1fe61 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69 6e 20  olving names in 
1fe62 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
1fe63 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
1fe64 67 20 63 6f 6d 6d 61 6e 64 3a 0a 20 20 20 20 2a  g command:.    *
1fe65 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 53 45 4c  *.    **     SEL
1fe66 45 43 54 20 61 2b 62 20 41 53 20 78 20 46 52 4f  ECT a+b AS x FRO
1fe67 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20 78 3c  M table WHERE x<
1fe68 31 30 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  10;.    **.    *
1fe69 2a 20 49 6e 20 63 61 73 65 73 20 6c 69 6b 65 20  * In cases like 
1fe6a 74 68 69 73 2c 20 72 65 70 6c 61 63 65 20 70 45  this, replace pE
1fe6b 78 70 72 20 77 69 74 68 20 61 20 63 6f 70 79 20  xpr with a copy 
1fe6c 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
1fe6d 6e 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 66 6f  n that.    ** fo
1fe6e 72 6d 73 20 74 68 65 20 72 65 73 75 6c 74 20 73  rms the result s
1fe6f 65 74 20 65 6e 74 72 79 20 28 22 61 2b 62 22 20  et entry ("a+b" 
1fe70 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 29 20  in the example) 
1fe71 61 6e 64 20 72 65 74 75 72 6e 20 69 6d 6d 65 64  and return immed
1fe72 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 4e  iately..    ** N
1fe73 6f 74 65 20 74 68 61 74 20 74 68 65 20 65 78 70  ote that the exp
1fe74 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 72  ression in the r
1fe75 65 73 75 6c 74 20 73 65 74 20 73 68 6f 75 6c 64  esult set should
1fe76 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62 65   have already be
1fe77 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 6f 6c 76  en.    ** resolv
1fe78 65 64 20 62 79 20 74 68 65 20 74 69 6d 65 20 74  ed by the time t
1fe79 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
1fe7a 69 73 20 72 65 73 6f 6c 76 65 64 2e 0a 20 20 20  is resolved..   
1fe7b 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d   */.    if( cnt=
1fe7c 3d 30 20 26 26 20 28 70 45 4c 69 73 74 20 3d 20  =0 && (pEList = 
1fe7d 70 4e 43 2d 3e 70 45 4c 69 73 74 29 21 3d 30 20  pNC->pEList)!=0 
1fe7e 26 26 20 7a 54 61 62 3d 3d 30 20 29 7b 0a 20 20  && zTab==0 ){.  
1fe7f 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
1fe80 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 6a 2b  EList->nExpr; j+
1fe81 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72  +){.        char
1fe82 20 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 2d 3e   *zAs = pEList->
1fe83 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  a[j].zName;.    
1fe84 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30 20 26      if( zAs!=0 &
1fe85 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  & sqlite3StrICmp
1fe86 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29  (zAs, zCol)==0 )
1fe87 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72  {.          Expr
1fe88 20 2a 70 4f 72 69 67 3b 0a 20 20 20 20 20 20 20   *pOrig;.       
1fe89 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
1fe8a 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20 70 45  ->pLeft==0 && pE
1fe8b 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29  xpr->pRight==0 )
1fe8c 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  ;.          asse
1fe8d 72 74 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  rt( pExpr->x.pLi
1fe8e 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  st==0 );.       
1fe8f 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
1fe90 2d 3e 78 2e 70 53 65 6c 65 63 74 3d 3d 30 20 29  ->x.pSelect==0 )
1fe91 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 72 69  ;.          pOri
1fe92 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d  g = pEList->a[j]
1fe93 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20  .pExpr;.        
1fe94 20 20 69 66 28 20 21 70 4e 43 2d 3e 61 6c 6c 6f    if( !pNC->allo
1fe95 77 41 67 67 20 26 26 20 45 78 70 72 48 61 73 50  wAgg && ExprHasP
1fe96 72 6f 70 65 72 74 79 28 70 4f 72 69 67 2c 20 45  roperty(pOrig, E
1fe97 50 5f 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20  P_Agg) ){.      
1fe98 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1fe99 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d  orMsg(pParse, "m
1fe9a 69 73 75 73 65 20 6f 66 20 61 6c 69 61 73 65 64  isuse of aliased
1fe9b 20 61 67 67 72 65 67 61 74 65 20 25 73 22 2c 20   aggregate %s", 
1fe9c 7a 41 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20  zAs);.          
1fe9d 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f    return WRC_Abo
1fe9e 72 74 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  rt;.          }.
1fe9f 20 20 20 20 20 20 20 20 20 20 72 65 73 6f 6c 76            resolv
1fea0 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c 20 70  eAlias(pParse, p
1fea1 45 4c 69 73 74 2c 20 6a 2c 20 70 45 78 70 72 2c  EList, j, pExpr,
1fea2 20 22 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20   "");.          
1fea3 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  cnt = 1;.       
1fea4 20 20 20 70 4d 61 74 63 68 20 3d 20 30 3b 0a 20     pMatch = 0;. 
1fea5 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
1fea6 20 7a 54 61 62 3d 3d 30 20 26 26 20 7a 44 62 3d   zTab==0 && zDb=
1fea7 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  =0 );.          
1fea8 67 6f 74 6f 20 6c 6f 6f 6b 75 70 6e 61 6d 65 5f  goto lookupname_
1fea9 65 6e 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  end;.        }. 
1feaa 20 20 20 20 20 7d 20 0a 20 20 20 20 7d 0a 0a 20       } .    }.. 
1feab 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f     /* Advance to
1feac 20 74 68 65 20 6e 65 78 74 20 6e 61 6d 65 20 63   the next name c
1fead 6f 6e 74 65 78 74 2e 20 20 54 68 65 20 6c 6f 6f  ontext.  The loo
1feae 70 20 77 69 6c 6c 20 65 78 69 74 20 77 68 65 6e  p will exit when
1feaf 20 65 69 74 68 65 72 0a 20 20 20 20 2a 2a 20 77   either.    ** w
1feb0 65 20 68 61 76 65 20 61 20 6d 61 74 63 68 20 28  e have a match (
1feb1 63 6e 74 3e 30 29 20 6f 72 20 77 68 65 6e 20 77  cnt>0) or when w
1feb2 65 20 72 75 6e 20 6f 75 74 20 6f 66 20 6e 61 6d  e run out of nam
1feb3 65 20 63 6f 6e 74 65 78 74 73 2e 0a 20 20 20 20  e contexts..    
1feb4 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d  */.    if( cnt==
1feb5 30 20 29 7b 0a 20 20 20 20 20 20 70 4e 43 20 3d  0 ){.      pNC =
1feb6 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20   pNC->pNext;.   
1feb7 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a   }.  }..  /*.  *
1feb8 2a 20 49 66 20 58 20 61 6e 64 20 59 20 61 72 65  * If X and Y are
1feb9 20 4e 55 4c 4c 20 28 69 6e 20 6f 74 68 65 72 20   NULL (in other 
1feba 77 6f 72 64 73 20 69 66 20 6f 6e 6c 79 20 74 68  words if only th
1febb 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 5a 20  e column name Z 
1febc 69 73 0a 20 20 2a 2a 20 73 75 70 70 6c 69 65 64  is.  ** supplied
1febd 29 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65 20  ) and the value 
1febe 6f 66 20 5a 20 69 73 20 65 6e 63 6c 6f 73 65 64  of Z is enclosed
1febf 20 69 6e 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65   in double-quote
1fec0 73 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 5a 20 69  s, then.  ** Z i
1fec1 73 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72  s a string liter
1fec2 61 6c 20 69 66 20 69 74 20 64 6f 65 73 6e 27 74  al if it doesn't
1fec3 20 6d 61 74 63 68 20 61 6e 79 20 63 6f 6c 75 6d   match any colum
1fec4 6e 20 6e 61 6d 65 73 2e 20 20 49 6e 20 74 68 61  n names.  In tha
1fec5 74 0a 20 20 2a 2a 20 63 61 73 65 2c 20 77 65 20  t.  ** case, we 
1fec6 6e 65 65 64 20 74 6f 20 72 65 74 75 72 6e 20 72  need to return r
1fec7 69 67 68 74 20 61 77 61 79 20 61 6e 64 20 6e 6f  ight away and no
1fec8 74 20 6d 61 6b 65 20 61 6e 79 20 63 68 61 6e 67  t make any chang
1fec9 65 73 20 74 6f 0a 20 20 2a 2a 20 70 45 78 70 72  es to.  ** pExpr
1feca 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42 65 63 61  ..  **.  ** Beca
1fecb 75 73 65 20 6e 6f 20 72 65 66 65 72 65 6e 63 65  use no reference
1fecc 20 77 61 73 20 6d 61 64 65 20 74 6f 20 6f 75 74   was made to out
1fecd 65 72 20 63 6f 6e 74 65 78 74 73 2c 20 74 68 65  er contexts, the
1fece 20 70 4e 43 2d 3e 6e 52 65 66 0a 20 20 2a 2a 20   pNC->nRef.  ** 
1fecf 66 69 65 6c 64 73 20 61 72 65 20 6e 6f 74 20 63  fields are not c
1fed0 68 61 6e 67 65 64 20 69 6e 20 61 6e 79 20 63 6f  hanged in any co
1fed1 6e 74 65 78 74 2e 0a 20 20 2a 2f 0a 20 20 69 66  ntext..  */.  if
1fed2 28 20 63 6e 74 3d 3d 30 20 26 26 20 7a 54 61 62  ( cnt==0 && zTab
1fed3 3d 3d 30 20 26 26 20 45 78 70 72 48 61 73 50 72  ==0 && ExprHasPr
1fed4 6f 70 65 72 74 79 28 70 45 78 70 72 2c 45 50 5f  operty(pExpr,EP_
1fed5 44 62 6c 51 75 6f 74 65 64 29 20 29 7b 0a 20 20  DblQuoted) ){.  
1fed6 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b    pExpr->op = TK
1fed7 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20 70 45 78  _STRING;.    pEx
1fed8 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20  pr->pTab = 0;.  
1fed9 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75    return WRC_Pru
1feda 6e 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20  ne;.  }..  /*.  
1fedb 2a 2a 20 63 6e 74 3d 3d 30 20 6d 65 61 6e 73 20  ** cnt==0 means 
1fedc 74 68 65 72 65 20 77 61 73 20 6e 6f 74 20 6d 61  there was not ma
1fedd 74 63 68 2e 20 20 63 6e 74 3e 31 20 6d 65 61 6e  tch.  cnt>1 mean
1fede 73 20 74 68 65 72 65 20 77 65 72 65 20 74 77 6f  s there were two
1fedf 20 6f 72 0a 20 20 2a 2a 20 6d 6f 72 65 20 6d 61   or.  ** more ma
1fee0 74 63 68 65 73 2e 20 20 45 69 74 68 65 72 20 77  tches.  Either w
1fee1 61 79 2c 20 77 65 20 68 61 76 65 20 61 6e 20 65  ay, we have an e
1fee2 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  rror..  */.  if(
1fee3 20 63 6e 74 21 3d 31 20 29 7b 0a 20 20 20 20 63   cnt!=1 ){.    c
1fee4 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 72 72 3b  onst char *zErr;
1fee5 0a 20 20 20 20 7a 45 72 72 20 3d 20 63 6e 74 3d  .    zErr = cnt=
1fee6 3d 30 20 3f 20 22 6e 6f 20 73 75 63 68 20 63 6f  =0 ? "no such co
1fee7 6c 75 6d 6e 22 20 3a 20 22 61 6d 62 69 67 75 6f  lumn" : "ambiguo
1fee8 75 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 22 3b  us column name";
1fee9 0a 20 20 20 20 69 66 28 20 7a 44 62 20 29 7b 0a  .    if( zDb ){.
1feea 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1feeb 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25  orMsg(pParse, "%
1feec 73 3a 20 25 73 2e 25 73 2e 25 73 22 2c 20 7a 45  s: %s.%s.%s", zE
1feed 72 72 2c 20 7a 44 62 2c 20 7a 54 61 62 2c 20 7a  rr, zDb, zTab, z
1feee 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  Col);.    }else 
1feef 69 66 28 20 7a 54 61 62 20 29 7b 0a 20 20 20 20  if( zTab ){.    
1fef0 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1fef1 67 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25  g(pParse, "%s: %
1fef2 73 2e 25 73 22 2c 20 7a 45 72 72 2c 20 7a 54 61  s.%s", zErr, zTa
1fef3 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 65  b, zCol);.    }e
1fef4 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
1fef5 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1fef6 65 2c 20 22 25 73 3a 20 25 73 22 2c 20 7a 45 72  e, "%s: %s", zEr
1fef7 72 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a  r, zCol);.    }.
1fef8 20 20 20 20 70 54 6f 70 4e 43 2d 3e 6e 45 72 72      pTopNC->nErr
1fef9 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66  ++;.  }..  /* If
1fefa 20 61 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 61   a column from a
1fefb 20 74 61 62 6c 65 20 69 6e 20 70 53 72 63 4c 69   table in pSrcLi
1fefc 73 74 20 69 73 20 72 65 66 65 72 65 6e 63 65 64  st is referenced
1fefd 2c 20 74 68 65 6e 20 72 65 63 6f 72 64 0a 20 20  , then record.  
1fefe 2a 2a 20 74 68 69 73 20 66 61 63 74 20 69 6e 20  ** this fact in 
1feff 74 68 65 20 70 53 72 63 4c 69 73 74 2e 61 5b 5d  the pSrcList.a[]
1ff00 2e 63 6f 6c 55 73 65 64 20 62 69 74 6d 61 73 6b  .colUsed bitmask
1ff01 2e 20 20 43 6f 6c 75 6d 6e 20 30 20 63 61 75 73  .  Column 0 caus
1ff02 65 73 0a 20 20 2a 2a 20 62 69 74 20 30 20 74 6f  es.  ** bit 0 to
1ff03 20 62 65 20 73 65 74 2e 20 20 43 6f 6c 75 6d 6e   be set.  Column
1ff04 20 31 20 73 65 74 73 20 62 69 74 20 31 2e 20 20   1 sets bit 1.  
1ff05 41 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 49  And so forth.  I
1ff06 66 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6c 75 6d  f the.  ** colum
1ff07 6e 20 6e 75 6d 62 65 72 20 69 73 20 67 72 65 61  n number is grea
1ff08 74 65 72 20 74 68 61 6e 20 74 68 65 20 6e 75 6d  ter than the num
1ff09 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e 20 74  ber of bits in t
1ff0a 68 65 20 62 69 74 6d 61 73 6b 0a 20 20 2a 2a 20  he bitmask.  ** 
1ff0b 74 68 65 6e 20 73 65 74 20 74 68 65 20 68 69 67  then set the hig
1ff0c 68 2d 6f 72 64 65 72 20 62 69 74 20 6f 66 20 74  h-order bit of t
1ff0d 68 65 20 62 69 74 6d 61 73 6b 2e 0a 20 20 2a 2f  he bitmask..  */
1ff0e 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43  .  if( pExpr->iC
1ff0f 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70 4d 61 74  olumn>=0 && pMat
1ff10 63 68 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ch!=0 ){.    int
1ff11 20 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c   n = pExpr->iCol
1ff12 75 6d 6e 3b 0a 20 20 20 20 74 65 73 74 63 61 73  umn;.    testcas
1ff13 65 28 20 6e 3d 3d 42 4d 53 2d 31 20 29 3b 0a 20  e( n==BMS-1 );. 
1ff14 20 20 20 69 66 28 20 6e 3e 3d 42 4d 53 20 29 7b     if( n>=BMS ){
1ff15 0a 20 20 20 20 20 20 6e 20 3d 20 42 4d 53 2d 31  .      n = BMS-1
1ff16 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  ;.    }.    asse
1ff17 72 74 28 20 70 4d 61 74 63 68 2d 3e 69 43 75 72  rt( pMatch->iCur
1ff18 73 6f 72 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62  sor==pExpr->iTab
1ff19 6c 65 20 29 3b 0a 20 20 20 20 70 4d 61 74 63 68  le );.    pMatch
1ff1a 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d 20 28 28 42  ->colUsed |= ((B
1ff1b 69 74 6d 61 73 6b 29 31 29 3c 3c 6e 3b 0a 20 20  itmask)1)<<n;.  
1ff1c 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 75 70  }..  /* Clean up
1ff1d 20 61 6e 64 20 72 65 74 75 72 6e 0a 20 20 2a 2f   and return.  */
1ff1e 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
1ff1f 6c 65 74 65 28 64 62 2c 20 70 45 78 70 72 2d 3e  lete(db, pExpr->
1ff20 70 4c 65 66 74 29 3b 0a 20 20 70 45 78 70 72 2d  pLeft);.  pExpr-
1ff21 3e 70 4c 65 66 74 20 3d 20 30 3b 0a 20 20 73 71  >pLeft = 0;.  sq
1ff22 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
1ff23 64 62 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  db, pExpr->pRigh
1ff24 74 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52 69  t);.  pExpr->pRi
1ff25 67 68 74 20 3d 20 30 3b 0a 20 20 70 45 78 70 72  ght = 0;.  pExpr
1ff26 2d 3e 6f 70 20 3d 20 28 69 73 54 72 69 67 67 65  ->op = (isTrigge
1ff27 72 20 3f 20 54 4b 5f 54 52 49 47 47 45 52 20 3a  r ? TK_TRIGGER :
1ff28 20 54 4b 5f 43 4f 4c 55 4d 4e 29 3b 0a 6c 6f 6f   TK_COLUMN);.loo
1ff29 6b 75 70 6e 61 6d 65 5f 65 6e 64 3a 0a 20 20 69  kupname_end:.  i
1ff2a 66 28 20 63 6e 74 3d 3d 31 20 29 7b 0a 20 20 20  f( cnt==1 ){.   
1ff2b 20 61 73 73 65 72 74 28 20 70 4e 43 21 3d 30 20   assert( pNC!=0 
1ff2c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75  );.    sqlite3Au
1ff2d 74 68 52 65 61 64 28 70 50 61 72 73 65 2c 20 70  thRead(pParse, p
1ff2e 45 78 70 72 2c 20 70 53 63 68 65 6d 61 2c 20 70  Expr, pSchema, p
1ff2f 4e 43 2d 3e 70 53 72 63 4c 69 73 74 29 3b 0a 20  NC->pSrcList);. 
1ff30 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20     /* Increment 
1ff31 74 68 65 20 6e 52 65 66 20 76 61 6c 75 65 20 6f  the nRef value o
1ff32 6e 20 61 6c 6c 20 6e 61 6d 65 20 63 6f 6e 74 65  n all name conte
1ff33 78 74 73 20 66 72 6f 6d 20 54 6f 70 4e 43 20 75  xts from TopNC u
1ff34 70 20 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20  p to.    ** the 
1ff35 70 6f 69 6e 74 20 77 68 65 72 65 20 74 68 65 20  point where the 
1ff36 6e 61 6d 65 20 6d 61 74 63 68 65 64 2e 20 2a 2f  name matched. */
1ff37 0a 20 20 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20  .    for(;;){.  
1ff38 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 70      assert( pTop
1ff39 4e 43 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70  NC!=0 );.      p
1ff3a 54 6f 70 4e 43 2d 3e 6e 52 65 66 2b 2b 3b 0a 20  TopNC->nRef++;. 
1ff3b 20 20 20 20 20 69 66 28 20 70 54 6f 70 4e 43 3d       if( pTopNC=
1ff3c 3d 70 4e 43 20 29 20 62 72 65 61 6b 3b 0a 20 20  =pNC ) break;.  
1ff3d 20 20 20 20 70 54 6f 70 4e 43 20 3d 20 70 54 6f      pTopNC = pTo
1ff3e 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  pNC->pNext;.    
1ff3f 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43  }.    return WRC
1ff40 5f 50 72 75 6e 65 3b 0a 20 20 7d 20 65 6c 73 65  _Prune;.  } else
1ff41 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52   {.    return WR
1ff42 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 7d 0a 0a  C_Abort;.  }.}..
1ff43 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61  /*.** Allocate a
1ff44 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e  nd return a poin
1ff45 74 65 72 20 74 6f 20 61 6e 20 65 78 70 72 65 73  ter to an expres
1ff46 73 69 6f 6e 20 74 6f 20 6c 6f 61 64 20 74 68 65  sion to load the
1ff47 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 0a 2a 2a 20   column iCol.** 
1ff48 66 72 6f 6d 20 64 61 74 61 73 6f 75 72 63 65 20  from datasource 
1ff49 69 53 72 63 20 64 61 74 61 73 6f 75 72 63 65 20  iSrc datasource 
1ff4a 69 6e 20 53 72 63 4c 69 73 74 20 70 53 72 63 2e  in SrcList pSrc.
1ff4b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1ff4c 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
1ff4d 43 72 65 61 74 65 43 6f 6c 75 6d 6e 45 78 70 72  CreateColumnExpr
1ff4e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 72  (sqlite3 *db, Sr
1ff4f 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 69 6e 74  cList *pSrc, int
1ff50 20 69 53 72 63 2c 20 69 6e 74 20 69 43 6f 6c 29   iSrc, int iCol)
1ff51 7b 0a 20 20 45 78 70 72 20 2a 70 20 3d 20 73 71  {.  Expr *p = sq
1ff52 6c 69 74 65 33 45 78 70 72 41 6c 6c 6f 63 28 64  lite3ExprAlloc(d
1ff53 62 2c 20 54 4b 5f 43 4f 4c 55 4d 4e 2c 20 30 2c  b, TK_COLUMN, 0,
1ff54 20 30 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a   0);.  if( p ){.
1ff55 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69      struct SrcLi
1ff56 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d  st_item *pItem =
1ff57 20 26 70 53 72 63 2d 3e 61 5b 69 53 72 63 5d 3b   &pSrc->a[iSrc];
1ff58 0a 20 20 20 20 70 2d 3e 70 54 61 62 20 3d 20 70  .    p->pTab = p
1ff59 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20  Item->pTab;.    
1ff5a 70 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 74 65  p->iTable = pIte
1ff5b 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20  m->iCursor;.    
1ff5c 69 66 28 20 70 2d 3e 70 54 61 62 2d 3e 69 50 4b  if( p->pTab->iPK
1ff5d 65 79 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20  ey==iCol ){.    
1ff5e 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d    p->iColumn = -
1ff5f 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  1;.    }else{.  
1ff60 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e 20 3d      p->iColumn =
1ff61 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 70 49 74   iCol;.      pIt
1ff62 65 6d 2d 3e 63 6f 6c 55 73 65 64 20 7c 3d 20 28  em->colUsed |= (
1ff63 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 28 69 43  (Bitmask)1)<<(iC
1ff64 6f 6c 3e 3d 42 4d 53 20 3f 20 42 4d 53 2d 31 20  ol>=BMS ? BMS-1 
1ff65 3a 20 69 43 6f 6c 29 3b 0a 20 20 20 20 7d 0a 20  : iCol);.    }. 
1ff66 20 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72     ExprSetProper
1ff67 74 79 28 70 2c 20 45 50 5f 52 65 73 6f 6c 76 65  ty(p, EP_Resolve
1ff68 64 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  d);.  }.  return
1ff69 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69   p;.}../*.** Thi
1ff6a 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
1ff6b 6c 62 61 63 6b 20 66 6f 72 20 73 71 6c 69 74 65  lback for sqlite
1ff6c 33 57 61 6c 6b 45 78 70 72 28 29 2e 0a 2a 2a 0a  3WalkExpr()..**.
1ff6d 2a 2a 20 52 65 73 6f 6c 76 65 20 73 79 6d 62 6f  ** Resolve symbo
1ff6e 6c 69 63 20 6e 61 6d 65 73 20 69 6e 74 6f 20 54  lic names into T
1ff6f 4b 5f 43 4f 4c 55 4d 4e 20 6f 70 65 72 61 74 6f  K_COLUMN operato
1ff70 72 73 20 66 6f 72 20 74 68 65 20 63 75 72 72 65  rs for the curre
1ff71 6e 74 0a 2a 2a 20 6e 6f 64 65 20 69 6e 20 74 68  nt.** node in th
1ff72 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  e expression tre
1ff73 65 2e 20 20 52 65 74 75 72 6e 20 30 20 74 6f 20  e.  Return 0 to 
1ff74 63 6f 6e 74 69 6e 75 65 20 74 68 65 20 73 65 61  continue the sea
1ff75 72 63 68 20 64 6f 77 6e 0a 2a 2a 20 74 68 65 20  rch down.** the 
1ff76 74 72 65 65 20 6f 72 20 32 20 74 6f 20 61 62 6f  tree or 2 to abo
1ff77 72 74 20 74 68 65 20 74 72 65 65 20 77 61 6c 6b  rt the tree walk
1ff78 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1ff79 74 69 6e 65 20 61 6c 73 6f 20 64 6f 65 73 20 65  tine also does e
1ff7a 72 72 6f 72 20 63 68 65 63 6b 69 6e 67 20 61 6e  rror checking an
1ff7b 64 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f  d name resolutio
1ff7c 6e 20 66 6f 72 0a 2a 2a 20 66 75 6e 63 74 69 6f  n for.** functio
1ff7d 6e 20 6e 61 6d 65 73 2e 20 20 54 68 65 20 6f 70  n names.  The op
1ff7e 65 72 61 74 6f 72 20 66 6f 72 20 61 67 67 72 65  erator for aggre
1ff7f 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 69  gate functions i
1ff80 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 74 6f 20  s changed.** to 
1ff81 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 2e  TK_AGG_FUNCTION.
1ff82 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
1ff83 65 73 6f 6c 76 65 45 78 70 72 53 74 65 70 28 57  esolveExprStep(W
1ff84 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20  alker *pWalker, 
1ff85 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20  Expr *pExpr){.  
1ff86 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43  NameContext *pNC
1ff87 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  ;.  Parse *pPars
1ff88 65 3b 0a 0a 20 20 70 4e 43 20 3d 20 70 57 61 6c  e;..  pNC = pWal
1ff89 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20 61 73  ker->u.pNC;.  as
1ff8a 73 65 72 74 28 20 70 4e 43 21 3d 30 20 29 3b 0a  sert( pNC!=0 );.
1ff8b 20 20 70 50 61 72 73 65 20 3d 20 70 4e 43 2d 3e    pParse = pNC->
1ff8c 70 50 61 72 73 65 3b 0a 20 20 61 73 73 65 72 74  pParse;.  assert
1ff8d 28 20 70 50 61 72 73 65 3d 3d 70 57 61 6c 6b 65  ( pParse==pWalke
1ff8e 72 2d 3e 70 50 61 72 73 65 20 29 3b 0a 0a 20 20  r->pParse );..  
1ff8f 69 66 28 20 45 78 70 72 48 61 73 41 6e 79 50 72  if( ExprHasAnyPr
1ff90 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
1ff91 5f 52 65 73 6f 6c 76 65 64 29 20 29 20 72 65 74  _Resolved) ) ret
1ff92 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20  urn WRC_Prune;. 
1ff93 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79   ExprSetProperty
1ff94 28 70 45 78 70 72 2c 20 45 50 5f 52 65 73 6f 6c  (pExpr, EP_Resol
1ff95 76 65 64 29 3b 0a 23 69 66 6e 64 65 66 20 4e 44  ved);.#ifndef ND
1ff96 45 42 55 47 0a 20 20 69 66 28 20 70 4e 43 2d 3e  EBUG.  if( pNC->
1ff97 70 53 72 63 4c 69 73 74 20 26 26 20 70 4e 43 2d  pSrcList && pNC-
1ff98 3e 70 53 72 63 4c 69 73 74 2d 3e 6e 41 6c 6c 6f  >pSrcList->nAllo
1ff99 63 3e 30 20 29 7b 0a 20 20 20 20 53 72 63 4c 69  c>0 ){.    SrcLi
1ff9a 73 74 20 2a 70 53 72 63 4c 69 73 74 20 3d 20 70  st *pSrcList = p
1ff9b 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a 20 20  NC->pSrcList;.  
1ff9c 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72    int i;.    for
1ff9d 28 69 3d 30 3b 20 69 3c 70 4e 43 2d 3e 70 53 72  (i=0; i<pNC->pSr
1ff9e 63 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  cList->nSrc; i++
1ff9f 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
1ffa0 20 70 53 72 63 4c 69 73 74 2d 3e 61 5b 69 5d 2e   pSrcList->a[i].
1ffa1 69 43 75 72 73 6f 72 3e 3d 30 20 26 26 20 70 53  iCursor>=0 && pS
1ffa2 72 63 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75  rcList->a[i].iCu
1ffa3 72 73 6f 72 3c 70 50 61 72 73 65 2d 3e 6e 54 61  rsor<pParse->nTa
1ffa4 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  b);.    }.  }.#e
1ffa5 6e 64 69 66 0a 20 20 73 77 69 74 63 68 28 20 70  ndif.  switch( p
1ffa6 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 0a 23 69 66  Expr->op ){..#if
1ffa7 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
1ffa8 45 4e 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45  ENABLE_UPDATE_DE
1ffa9 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21  LETE_LIMIT) && !
1ffaa 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
1ffab 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a 20 20  MIT_SUBQUERY).  
1ffac 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69 61 6c    /* The special
1ffad 20 6f 70 65 72 61 74 6f 72 20 54 4b 5f 52 4f 57   operator TK_ROW
1ffae 20 6d 65 61 6e 73 20 75 73 65 20 74 68 65 20 72   means use the r
1ffaf 6f 77 69 64 20 66 6f 72 20 74 68 65 20 66 69 72  owid for the fir
1ffb0 73 74 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e  st.    ** column
1ffb1 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
1ffb2 75 73 65 2e 20 20 54 68 69 73 20 69 73 20 75 73  use.  This is us
1ffb3 65 64 20 62 79 20 74 68 65 20 4c 49 4d 49 54 20  ed by the LIMIT 
1ffb4 61 6e 64 20 4f 52 44 45 52 20 42 59 0a 20 20 20  and ORDER BY.   
1ffb5 20 2a 2a 20 63 6c 61 75 73 65 20 70 72 6f 63 65   ** clause proce
1ffb6 73 73 69 6e 67 20 6f 6e 20 55 50 44 41 54 45 20  ssing on UPDATE 
1ffb7 61 6e 64 20 44 45 4c 45 54 45 20 73 74 61 74 65  and DELETE state
1ffb8 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  ments..    */.  
1ffb9 20 20 63 61 73 65 20 54 4b 5f 52 4f 57 3a 20 7b    case TK_ROW: {
1ffba 0a 20 20 20 20 20 20 53 72 63 4c 69 73 74 20 2a  .      SrcList *
1ffbb 70 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e  pSrcList = pNC->
1ffbc 70 53 72 63 4c 69 73 74 3b 0a 20 20 20 20 20 20  pSrcList;.      
1ffbd 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
1ffbe 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20  tem *pItem;.    
1ffbf 20 20 61 73 73 65 72 74 28 20 70 53 72 63 4c 69    assert( pSrcLi
1ffc0 73 74 20 26 26 20 70 53 72 63 4c 69 73 74 2d 3e  st && pSrcList->
1ffc1 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 20 20 20  nSrc==1 );.     
1ffc2 20 70 49 74 65 6d 20 3d 20 70 53 72 63 4c 69 73   pItem = pSrcLis
1ffc3 74 2d 3e 61 3b 20 0a 20 20 20 20 20 20 70 45 78  t->a; .      pEx
1ffc4 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4c 55  pr->op = TK_COLU
1ffc5 4d 4e 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d  MN;.      pExpr-
1ffc6 3e 70 54 61 62 20 3d 20 70 49 74 65 6d 2d 3e 70  >pTab = pItem->p
1ffc7 54 61 62 3b 0a 20 20 20 20 20 20 70 45 78 70 72  Tab;.      pExpr
1ffc8 2d 3e 69 54 61 62 6c 65 20 3d 20 70 49 74 65 6d  ->iTable = pItem
1ffc9 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20  ->iCursor;.     
1ffca 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20   pExpr->iColumn 
1ffcb 3d 20 2d 31 3b 0a 20 20 20 20 20 20 70 45 78 70  = -1;.      pExp
1ffcc 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51  r->affinity = SQ
1ffcd 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
1ffce 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1ffcf 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64     }.#endif /* d
1ffd0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
1ffd1 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45  ABLE_UPDATE_DELE
1ffd2 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65  TE_LIMIT) && !de
1ffd3 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1ffd4 54 5f 53 55 42 51 55 45 52 59 29 20 2a 2f 0a 0a  T_SUBQUERY) */..
1ffd5 20 20 20 20 2f 2a 20 41 20 6c 6f 6e 65 20 69 64      /* A lone id
1ffd6 65 6e 74 69 66 69 65 72 20 69 73 20 74 68 65 20  entifier is the 
1ffd7 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e  name of a column
1ffd8 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
1ffd9 65 20 54 4b 5f 49 44 3a 20 7b 0a 20 20 20 20 20  e TK_ID: {.     
1ffda 20 72 65 74 75 72 6e 20 6c 6f 6f 6b 75 70 4e 61   return lookupNa
1ffdb 6d 65 28 70 50 61 72 73 65 2c 20 30 2c 20 30 2c  me(pParse, 0, 0,
1ffdc 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e   pExpr->u.zToken
1ffdd 2c 20 70 4e 43 2c 20 70 45 78 70 72 29 3b 0a 20  , pNC, pExpr);. 
1ffde 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 41     }.  .    /* A
1ffdf 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20   table name and 
1ffe0 63 6f 6c 75 6d 6e 20 6e 61 6d 65 3a 20 20 20 20  column name:    
1ffe1 20 49 44 2e 49 44 0a 20 20 20 20 2a 2a 20 4f 72   ID.ID.    ** Or
1ffe2 20 61 20 64 61 74 61 62 61 73 65 2c 20 74 61 62   a database, tab
1ffe3 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 3a 20 20  le and column:  
1ffe4 49 44 2e 49 44 2e 49 44 0a 20 20 20 20 2a 2f 0a  ID.ID.ID.    */.
1ffe5 20 20 20 20 63 61 73 65 20 54 4b 5f 44 4f 54 3a      case TK_DOT:
1ffe6 20 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63   {.      const c
1ffe7 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e 3b 0a 20 20  har *zColumn;.  
1ffe8 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
1ffe9 7a 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 63 6f  zTable;.      co
1ffea 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 0a 20  nst char *zDb;. 
1ffeb 20 20 20 20 20 45 78 70 72 20 2a 70 52 69 67 68       Expr *pRigh
1ffec 74 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 69 66 28  t;..      /* if(
1ffed 20 70 53 72 63 4c 69 73 74 3d 3d 30 20 29 20 62   pSrcList==0 ) b
1ffee 72 65 61 6b 3b 20 2a 2f 0a 20 20 20 20 20 20 70  reak; */.      p
1ffef 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 70  Right = pExpr->p
1fff0 52 69 67 68 74 3b 0a 20 20 20 20 20 20 69 66 28  Right;.      if(
1fff1 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f   pRight->op==TK_
1fff2 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 44  ID ){.        zD
1fff3 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7a  b = 0;.        z
1fff4 54 61 62 6c 65 20 3d 20 70 45 78 70 72 2d 3e 70  Table = pExpr->p
1fff5 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a  Left->u.zToken;.
1fff6 20 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d 6e 20          zColumn 
1fff7 3d 20 70 52 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b  = pRight->u.zTok
1fff8 65 6e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  en;.      }else{
1fff9 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1fffa 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f   pRight->op==TK_
1fffb 44 4f 54 20 29 3b 0a 20 20 20 20 20 20 20 20 7a  DOT );.        z
1fffc 44 62 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66  Db = pExpr->pLef
1fffd 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20  t->u.zToken;.   
1fffe 20 20 20 20 20 7a 54 61 62 6c 65 20 3d 20 70 52       zTable = pR
1ffff 69 67 68 74 2d 3e 70 4c 65 66 74 2d 3e 75 2e 7a  ight->pLeft->u.z
20000 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 7a  Token;.        z
20001 43 6f 6c 75 6d 6e 20 3d 20 70 52 69 67 68 74 2d  Column = pRight-
20002 3e 70 52 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65  >pRight->u.zToke
20003 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  n;.      }.     
20004 20 72 65 74 75 72 6e 20 6c 6f 6f 6b 75 70 4e 61   return lookupNa
20005 6d 65 28 70 50 61 72 73 65 2c 20 7a 44 62 2c 20  me(pParse, zDb, 
20006 7a 54 61 62 6c 65 2c 20 7a 43 6f 6c 75 6d 6e 2c  zTable, zColumn,
20007 20 70 4e 43 2c 20 70 45 78 70 72 29 3b 0a 20 20   pNC, pExpr);.  
20008 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f    }..    /* Reso
20009 6c 76 65 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d  lve function nam
2000a 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  es.    */.    ca
2000b 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43  se TK_CONST_FUNC
2000c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55  :.    case TK_FU
2000d 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20  NCTION: {.      
2000e 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20  ExprList *pList 
2000f 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  = pExpr->x.pList
20010 3b 20 20 20 20 2f 2a 20 54 68 65 20 61 72 67 75  ;    /* The argu
20011 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 20  ment list */.   
20012 20 20 20 69 6e 74 20 6e 20 3d 20 70 4c 69 73 74     int n = pList
20013 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20   ? pList->nExpr 
20014 3a 20 30 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65  : 0;    /* Numbe
20015 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 2a  r of arguments *
20016 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 6f 5f 73  /.      int no_s
20017 75 63 68 5f 66 75 6e 63 20 3d 20 30 3b 20 20 20  uch_func = 0;   
20018 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6e      /* True if n
20019 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 20  o such function 
2001a 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 20 20 20  exists */.      
2001b 69 6e 74 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72  int wrong_num_ar
2001c 67 73 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54  gs = 0;     /* T
2001d 72 75 65 20 69 66 20 77 72 6f 6e 67 20 6e 75 6d  rue if wrong num
2001e 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ber of arguments
2001f 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 73   */.      int is
20020 5f 61 67 67 20 3d 20 30 3b 20 20 20 20 20 20 20  _agg = 0;       
20021 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
20022 20 69 73 20 61 6e 20 61 67 67 72 65 67 61 74 65   is an aggregate
20023 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 20   function */.   
20024 20 20 20 69 6e 74 20 61 75 74 68 3b 20 20 20 20     int auth;    
20025 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
20026 2a 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  * Authorization 
20027 74 6f 20 75 73 65 20 74 68 65 20 66 75 6e 63 74  to use the funct
20028 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ion */.      int
20029 20 6e 49 64 3b 20 20 20 20 20 20 20 20 20 20 20   nId;           
2002a 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
2002b 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  er of characters
2002c 20 69 6e 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d   in function nam
2002d 65 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74  e */.      const
2002e 20 63 68 61 72 20 2a 7a 49 64 3b 20 20 20 20 20   char *zId;     
2002f 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75         /* The fu
20030 6e 63 74 69 6f 6e 20 6e 61 6d 65 2e 20 2a 2f 0a  nction name. */.
20031 20 20 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70        FuncDef *p
20032 44 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Def;            
20033 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e    /* Information
20034 20 61 62 6f 75 74 20 74 68 65 20 66 75 6e 63 74   about the funct
20035 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 75 38 20  ion */.      u8 
20036 65 6e 63 20 3d 20 45 4e 43 28 70 50 61 72 73 65  enc = ENC(pParse
20037 2d 3e 64 62 29 3b 20 20 20 2f 2a 20 54 68 65 20  ->db);   /* The 
20038 64 61 74 61 62 61 73 65 20 65 6e 63 6f 64 69 6e  database encodin
20039 67 20 2a 2f 0a 0a 20 20 20 20 20 20 74 65 73 74  g */..      test
2003a 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d  case( pExpr->op=
2003b 3d 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 20 29  =TK_CONST_FUNC )
2003c 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2003d 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
2003e 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65  (pExpr, EP_xIsSe
2003f 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 7a  lect) );.      z
20040 49 64 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a 54  Id = pExpr->u.zT
20041 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 6e 49 64 20  oken;.      nId 
20042 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
20043 30 28 7a 49 64 29 3b 0a 20 20 20 20 20 20 70 44  0(zId);.      pD
20044 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ef = sqlite3Find
20045 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d  Function(pParse-
20046 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 6e  >db, zId, nId, n
20047 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20  , enc, 0);.     
20048 20 69 66 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a   if( pDef==0 ){.
20049 20 20 20 20 20 20 20 20 70 44 65 66 20 3d 20 73          pDef = s
2004a 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
2004b 6f 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a  on(pParse->db, z
2004c 49 64 2c 20 6e 49 64 2c 20 2d 31 2c 20 65 6e 63  Id, nId, -1, enc
2004d 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , 0);.        if
2004e 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a 20 20 20  ( pDef==0 ){.   
2004f 20 20 20 20 20 20 20 6e 6f 5f 73 75 63 68 5f 66         no_such_f
20050 75 6e 63 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  unc = 1;.       
20051 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
20052 20 20 77 72 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73    wrong_num_args
20053 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 1;.        }.
20054 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
20055 20 20 20 20 20 69 73 5f 61 67 67 20 3d 20 70 44       is_agg = pD
20056 65 66 2d 3e 78 46 75 6e 63 3d 3d 30 3b 0a 20 20  ef->xFunc==0;.  
20057 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
20058 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
20059 49 5a 41 54 49 4f 4e 0a 20 20 20 20 20 20 69 66  IZATION.      if
2005a 28 20 70 44 65 66 20 29 7b 0a 20 20 20 20 20 20  ( pDef ){.      
2005b 20 20 61 75 74 68 20 3d 20 73 71 6c 69 74 65 33    auth = sqlite3
2005c 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
2005d 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f  , SQLITE_FUNCTIO
2005e 4e 2c 20 30 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d  N, 0, pDef->zNam
2005f 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69  e, 0);.        i
20060 66 28 20 61 75 74 68 21 3d 53 51 4c 49 54 45 5f  f( auth!=SQLITE_
20061 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
20062 69 66 28 20 61 75 74 68 3d 3d 53 51 4c 49 54 45  if( auth==SQLITE
20063 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 20 20 20  _DENY ){.       
20064 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
20065 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f  rMsg(pParse, "no
20066 74 20 61 75 74 68 6f 72 69 7a 65 64 20 74 6f 20  t authorized to 
20067 75 73 65 20 66 75 6e 63 74 69 6f 6e 3a 20 25 73  use function: %s
20068 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
20069 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2006a 20 20 20 20 20 20 20 70 44 65 66 2d 3e 7a 4e 61         pDef->zNa
2006b 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  me);.           
2006c 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20   pNC->nErr++;.  
2006d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2006e 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20      pExpr->op = 
2006f 54 4b 5f 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20  TK_NULL;.       
20070 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72     return WRC_Pr
20071 75 6e 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  une;.        }. 
20072 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
20073 20 20 20 20 69 66 28 20 69 73 5f 61 67 67 20 26      if( is_agg &
20074 26 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67  & !pNC->allowAgg
20075 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
20076 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
20077 73 65 2c 20 22 6d 69 73 75 73 65 20 6f 66 20 61  se, "misuse of a
20078 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
20079 6e 20 25 2e 2a 73 28 29 22 2c 20 6e 49 64 2c 7a  n %.*s()", nId,z
2007a 49 64 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43  Id);.        pNC
2007b 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20  ->nErr++;.      
2007c 20 20 69 73 5f 61 67 67 20 3d 20 30 3b 0a 20 20    is_agg = 0;.  
2007d 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 6f      }else if( no
2007e 5f 73 75 63 68 5f 66 75 6e 63 20 29 7b 0a 20 20  _such_func ){.  
2007f 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
20080 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
20081 6f 20 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 3a  o such function:
20082 20 25 2e 2a 73 22 2c 20 6e 49 64 2c 20 7a 49 64   %.*s", nId, zId
20083 29 3b 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e  );.        pNC->
20084 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 7d 65  nErr++;.      }e
20085 6c 73 65 20 69 66 28 20 77 72 6f 6e 67 5f 6e 75  lse if( wrong_nu
20086 6d 5f 61 72 67 73 20 29 7b 0a 20 20 20 20 20 20  m_args ){.      
20087 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
20088 67 28 70 50 61 72 73 65 2c 22 77 72 6f 6e 67 20  g(pParse,"wrong 
20089 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
2008a 6e 74 73 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20  nts to function 
2008b 25 2e 2a 73 28 29 22 2c 0a 20 20 20 20 20 20 20  %.*s()",.       
2008c 20 20 20 20 20 20 6e 49 64 2c 20 7a 49 64 29 3b        nId, zId);
2008d 0a 20 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45  .        pNC->nE
2008e 72 72 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rr++;.      }.  
2008f 20 20 20 20 69 66 28 20 69 73 5f 61 67 67 20 29      if( is_agg )
20090 7b 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d  {.        pExpr-
20091 3e 6f 70 20 3d 20 54 4b 5f 41 47 47 5f 46 55 4e  >op = TK_AGG_FUN
20092 43 54 49 4f 4e 3b 0a 20 20 20 20 20 20 20 20 70  CTION;.        p
20093 4e 43 2d 3e 68 61 73 41 67 67 20 3d 20 31 3b 0a  NC->hasAgg = 1;.
20094 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
20095 28 20 69 73 5f 61 67 67 20 29 20 70 4e 43 2d 3e  ( is_agg ) pNC->
20096 61 6c 6c 6f 77 41 67 67 20 3d 20 30 3b 0a 20 20  allowAgg = 0;.  
20097 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45      sqlite3WalkE
20098 78 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c  xprList(pWalker,
20099 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 69   pList);.      i
2009a 66 28 20 69 73 5f 61 67 67 20 29 20 70 4e 43 2d  f( is_agg ) pNC-
2009b 3e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 20  >allowAgg = 1;. 
2009c 20 20 20 20 20 2f 2a 20 46 49 58 20 4d 45 3a 20       /* FIX ME: 
2009d 20 43 6f 6d 70 75 74 65 20 70 45 78 70 72 2d 3e   Compute pExpr->
2009e 61 66 66 69 6e 69 74 79 20 62 61 73 65 64 20 6f  affinity based o
2009f 6e 20 74 68 65 20 65 78 70 65 63 74 65 64 20 72  n the expected r
200a0 65 74 75 72 6e 0a 20 20 20 20 20 20 2a 2a 20 74  eturn.      ** t
200a1 79 70 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74  ype of the funct
200a2 69 6f 6e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ion .      */.  
200a3 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50      return WRC_P
200a4 72 75 6e 65 3b 0a 20 20 20 20 7d 0a 23 69 66 6e  rune;.    }.#ifn
200a5 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
200a6 53 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73  SUBQUERY.    cas
200a7 65 20 54 4b 5f 53 45 4c 45 43 54 3a 0a 20 20 20  e TK_SELECT:.   
200a8 20 63 61 73 65 20 54 4b 5f 45 58 49 53 54 53 3a   case TK_EXISTS:
200a9 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70    testcase( pExp
200aa 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54 53  r->op==TK_EXISTS
200ab 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 63   );.#endif.    c
200ac 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20  ase TK_IN: {.   
200ad 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78     testcase( pEx
200ae 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20 29 3b  pr->op==TK_IN );
200af 0a 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48  .      if( ExprH
200b0 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
200b1 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
200b2 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  ){.        int n
200b3 52 65 66 20 3d 20 70 4e 43 2d 3e 6e 52 65 66 3b  Ref = pNC->nRef;
200b4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
200b5 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 20 20 20  OMIT_CHECK.     
200b6 20 20 20 69 66 28 20 70 4e 43 2d 3e 69 73 43 68     if( pNC->isCh
200b7 65 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  eck ){.         
200b8 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
200b9 28 70 50 61 72 73 65 2c 22 73 75 62 71 75 65 72  (pParse,"subquer
200ba 69 65 73 20 70 72 6f 68 69 62 69 74 65 64 20 69  ies prohibited i
200bb 6e 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69  n CHECK constrai
200bc 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 20 20 7d  nts");.        }
200bd 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20  .#endif.        
200be 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
200bf 74 28 70 57 61 6c 6b 65 72 2c 20 70 45 78 70 72  t(pWalker, pExpr
200c0 2d 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20  ->x.pSelect);.  
200c1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4e        assert( pN
200c2 43 2d 3e 6e 52 65 66 3e 3d 6e 52 65 66 20 29 3b  C->nRef>=nRef );
200c3 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 52 65  .        if( nRe
200c4 66 21 3d 70 4e 43 2d 3e 6e 52 65 66 20 29 7b 0a  f!=pNC->nRef ){.
200c5 20 20 20 20 20 20 20 20 20 20 45 78 70 72 53 65            ExprSe
200c6 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  tProperty(pExpr,
200c7 20 45 50 5f 56 61 72 53 65 6c 65 63 74 29 3b 0a   EP_VarSelect);.
200c8 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
200c9 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
200ca 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
200cb 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20  ITE_OMIT_CHECK. 
200cc 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49 41     case TK_VARIA
200cd 42 4c 45 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  BLE: {.      if(
200ce 20 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 29 7b   pNC->isCheck ){
200cf 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
200d0 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
200d1 22 70 61 72 61 6d 65 74 65 72 73 20 70 72 6f 68  "parameters proh
200d2 69 62 69 74 65 64 20 69 6e 20 43 48 45 43 4b 20  ibited in CHECK 
200d3 63 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b 0a 20  constraints");. 
200d4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
200d5 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  ak;.    }.#endif
200d6 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 70  .  }.  return (p
200d7 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 70  Parse->nErr || p
200d8 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f  Parse->db->mallo
200d9 63 46 61 69 6c 65 64 29 20 3f 20 57 52 43 5f 41  cFailed) ? WRC_A
200da 62 6f 72 74 20 3a 20 57 52 43 5f 43 6f 6e 74 69  bort : WRC_Conti
200db 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45  nue;.}../*.** pE
200dc 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74 20 6f  List is a list o
200dd 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 77 68  f expressions wh
200de 69 63 68 20 61 72 65 20 72 65 61 6c 6c 79 20 74  ich are really t
200df 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
200e0 20 74 68 65 0a 2a 2a 20 61 20 53 45 4c 45 43 54   the.** a SELECT
200e1 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 70 45 20   statement.  pE 
200e2 69 73 20 61 20 74 65 72 6d 20 69 6e 20 61 6e 20  is a term in an 
200e3 4f 52 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55  ORDER BY or GROU
200e4 50 20 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2a 20  P BY clause..** 
200e5 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65  This routine che
200e6 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 70 45  cks to see if pE
200e7 20 69 73 20 61 20 73 69 6d 70 6c 65 20 69 64 65   is a simple ide
200e8 6e 74 69 66 69 65 72 20 77 68 69 63 68 20 63 6f  ntifier which co
200e9 72 72 65 73 70 6f 6e 64 73 0a 2a 2a 20 74 6f 20  rresponds.** to 
200ea 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f 66 20 6f  the AS-name of o
200eb 6e 65 20 6f 66 20 74 68 65 20 74 65 72 6d 73 20  ne of the terms 
200ec 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
200ed 6e 20 6c 69 73 74 2e 20 20 49 66 20 69 74 20 69  n list.  If it i
200ee 73 2c 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69  s,.** this routi
200ef 6e 65 20 72 65 74 75 72 6e 20 61 6e 20 69 6e 74  ne return an int
200f0 65 67 65 72 20 62 65 74 77 65 65 6e 20 31 20 61  eger between 1 a
200f1 6e 64 20 4e 20 77 68 65 72 65 20 4e 20 69 73 20  nd N where N is 
200f2 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  the number of.**
200f3 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 70 45 4c   elements in pEL
200f4 69 73 74 2c 20 63 6f 72 72 65 73 70 6f 6e 64 69  ist, correspondi
200f5 6e 67 20 74 6f 20 74 68 65 20 6d 61 74 63 68 69  ng to the matchi
200f6 6e 67 20 65 6e 74 72 79 2e 20 20 49 66 20 74 68  ng entry.  If th
200f7 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 20 6d 61 74  ere is.** no mat
200f8 63 68 2c 20 6f 72 20 69 66 20 70 45 20 69 73 20  ch, or if pE is 
200f9 6e 6f 74 20 61 20 73 69 6d 70 6c 65 20 69 64 65  not a simple ide
200fa 6e 74 69 66 69 65 72 2c 20 74 68 65 6e 20 74 68  ntifier, then th
200fb 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65  is routine.** re
200fc 74 75 72 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 70 45  turn 0..**.** pE
200fd 4c 69 73 74 20 68 61 73 20 62 65 65 6e 20 72 65  List has been re
200fe 73 6f 6c 76 65 64 2e 20 20 70 45 20 68 61 73 20  solved.  pE has 
200ff 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  not..*/.static i
20100 6e 74 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d 65  nt resolveAsName
20101 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
20102 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e  e,     /* Parsin
20103 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 65 72  g context for er
20104 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a  ror messages */.
20105 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
20106 73 74 2c 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20  st,  /* List of 
20107 65 78 70 72 65 73 73 69 6f 6e 73 20 74 6f 20 73  expressions to s
20108 63 61 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  can */.  Expr *p
20109 45 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45  E           /* E
2010a 78 70 72 65 73 73 69 6f 6e 20 77 65 20 61 72 65  xpression we are
2010b 20 74 72 79 69 6e 67 20 74 6f 20 6d 61 74 63 68   trying to match
2010c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20   */.){.  int i; 
2010d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2010e 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a  oop counter */..
2010f 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
20110 45 52 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 69  ER(pParse);..  i
20111 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 49 44  f( pE->op==TK_ID
20112 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 43   ){.    char *zC
20113 6f 6c 20 3d 20 70 45 2d 3e 75 2e 7a 54 6f 6b 65  ol = pE->u.zToke
20114 6e 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  n;.    for(i=0; 
20115 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b  i<pEList->nExpr;
20116 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61   i++){.      cha
20117 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74 2d  r *zAs = pEList-
20118 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  >a[i].zName;.   
20119 20 20 20 69 66 28 20 7a 41 73 21 3d 30 20 26 26     if( zAs!=0 &&
2011a 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
2011b 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30 20 29 7b  zAs, zCol)==0 ){
2011c 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
2011d 69 2b 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  i+1;.      }.   
2011e 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
2011f 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 20 69  0;.}../*.** pE i
20120 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
20121 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 77 68 69  n expression whi
20122 63 68 20 69 73 20 61 20 73 69 6e 67 6c 65 20 74  ch is a single t
20123 65 72 6d 20 69 6e 20 74 68 65 0a 2a 2a 20 4f 52  erm in the.** OR
20124 44 45 52 20 42 59 20 6f 66 20 61 20 63 6f 6d 70  DER BY of a comp
20125 6f 75 6e 64 20 53 45 4c 45 43 54 2e 20 20 54 68  ound SELECT.  Th
20126 65 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73  e expression has
20127 20 6e 6f 74 20 62 65 65 6e 0a 2a 2a 20 6e 61 6d   not been.** nam
20128 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a 2a 0a 2a  e resolved..**.*
20129 2a 20 41 74 20 74 68 65 20 70 6f 69 6e 74 20 74  * At the point t
2012a 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
2012b 61 6c 6c 65 64 2c 20 77 65 20 61 6c 72 65 61 64  alled, we alread
2012c 79 20 6b 6e 6f 77 20 74 68 61 74 20 74 68 65 0a  y know that the.
2012d 2a 2a 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  ** ORDER BY term
2012e 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67   is not an integ
2012f 65 72 20 69 6e 64 65 78 20 69 6e 74 6f 20 74 68  er index into th
20130 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 54  e result set.  T
20131 68 61 74 0a 2a 2a 20 63 61 73 65 20 69 73 20 68  hat.** case is h
20132 61 6e 64 6c 65 64 20 62 79 20 74 68 65 20 63 61  andled by the ca
20133 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 2e 0a 2a  lling routine..*
20134 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20  *.** Attempt to 
20135 6d 61 74 63 68 20 70 45 20 61 67 61 69 6e 73 74  match pE against
20136 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c 75   result set colu
20137 6d 6e 73 20 69 6e 20 74 68 65 20 6c 65 66 74 2d  mns in the left-
20138 6d 6f 73 74 0a 2a 2a 20 53 45 4c 45 43 54 20 73  most.** SELECT s
20139 74 61 74 65 6d 65 6e 74 2e 20 20 52 65 74 75 72  tatement.  Retur
2013a 6e 20 74 68 65 20 69 6e 64 65 78 20 69 20 6f 66  n the index i of
2013b 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20 63 6f   the matching co
2013c 6c 75 6d 6e 2c 0a 2a 2a 20 61 73 20 61 6e 20 69  lumn,.** as an i
2013d 6e 64 69 63 61 74 69 6f 6e 20 74 6f 20 74 68 65  ndication to the
2013e 20 63 61 6c 6c 65 72 20 74 68 61 74 20 69 74 20   caller that it 
2013f 73 68 6f 75 6c 64 20 73 6f 72 74 20 62 79 20 74  should sort by t
20140 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 2e 0a  he i-th column..
20141 2a 2a 20 54 68 65 20 6c 65 66 74 2d 6d 6f 73 74  ** The left-most
20142 20 63 6f 6c 75 6d 6e 20 69 73 20 31 2e 20 20 49   column is 1.  I
20143 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74  n other words, t
20144 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
20145 64 20 69 73 20 74 68 65 0a 2a 2a 20 73 61 6d 65  d is the.** same
20146 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 74   integer value t
20147 68 61 74 20 77 6f 75 6c 64 20 62 65 20 75 73 65  hat would be use
20148 64 20 69 6e 20 74 68 65 20 53 51 4c 20 73 74 61  d in the SQL sta
20149 74 65 6d 65 6e 74 20 74 6f 20 69 6e 64 69 63 61  tement to indica
2014a 74 65 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e  te.** the column
2014b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65  ..**.** If there
2014c 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c 20 72 65   is no match, re
2014d 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e 20  turn 0.  Return 
2014e 2d 31 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f  -1 if an error o
2014f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  ccurs..*/.static
20150 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f 72 64 65   int resolveOrde
20151 72 42 79 54 65 72 6d 54 6f 45 78 70 72 4c 69 73  rByTermToExprLis
20152 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  t(.  Parse *pPar
20153 73 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69  se,     /* Parsi
20154 6e 67 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 65  ng context for e
20155 72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f  rror messages */
20156 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65  .  Select *pSele
20157 63 74 2c 20 20 20 2f 2a 20 54 68 65 20 53 45 4c  ct,   /* The SEL
20158 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 77 69  ECT statement wi
20159 74 68 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  th the ORDER BY 
2015a 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72  clause */.  Expr
2015b 20 2a 70 45 20 20 20 20 20 20 20 20 20 20 20 2f   *pE           /
2015c 2a 20 54 68 65 20 73 70 65 63 69 66 69 63 20 4f  * The specific O
2015d 52 44 45 52 20 42 59 20 74 65 72 6d 20 2a 2f 0a  RDER BY term */.
2015e 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  ){.  int i;     
2015f 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
20160 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 70  counter */.  Exp
20161 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 20 20  rList *pEList;  
20162 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20 6f  /* The columns o
20163 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  f the result set
20164 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78   */.  NameContex
20165 74 20 6e 63 3b 20 20 20 20 2f 2a 20 4e 61 6d 65  t nc;    /* Name
20166 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 72 65 73   context for res
20167 6f 6c 76 69 6e 67 20 70 45 20 2a 2f 0a 0a 20 20  olving pE */..  
20168 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 45  assert( sqlite3E
20169 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 45 2c  xprIsInteger(pE,
2016a 20 26 69 29 3d 3d 30 20 29 3b 0a 20 20 70 45 4c   &i)==0 );.  pEL
2016b 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70  ist = pSelect->p
2016c 45 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 52 65 73  EList;..  /* Res
2016d 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 73 20 69  olve all names i
2016e 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 74  n the ORDER BY t
2016f 65 72 6d 20 65 78 70 72 65 73 73 69 6f 6e 0a 20  erm expression. 
20170 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 6e 63   */.  memset(&nc
20171 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6e 63 29 29  , 0, sizeof(nc))
20172 3b 0a 20 20 6e 63 2e 70 50 61 72 73 65 20 3d 20  ;.  nc.pParse = 
20173 70 50 61 72 73 65 3b 0a 20 20 6e 63 2e 70 53 72  pParse;.  nc.pSr
20174 63 4c 69 73 74 20 3d 20 70 53 65 6c 65 63 74 2d  cList = pSelect-
20175 3e 70 53 72 63 3b 0a 20 20 6e 63 2e 70 45 4c 69  >pSrc;.  nc.pELi
20176 73 74 20 3d 20 70 45 4c 69 73 74 3b 0a 20 20 6e  st = pEList;.  n
20177 63 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a  c.allowAgg = 1;.
20178 20 20 6e 63 2e 6e 45 72 72 20 3d 20 30 3b 0a 20    nc.nErr = 0;. 
20179 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f   if( sqlite3Reso
2017a 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 6e 63  lveExprNames(&nc
2017b 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 73 71 6c  , pE) ){.    sql
2017c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28 70  ite3ErrorClear(p
2017d 50 61 72 73 65 29 3b 0a 20 20 20 20 72 65 74 75  Parse);.    retu
2017e 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  rn 0;.  }..  /* 
2017f 54 72 79 20 74 6f 20 6d 61 74 63 68 20 74 68 65  Try to match the
20180 20 4f 52 44 45 52 20 42 59 20 65 78 70 72 65 73   ORDER BY expres
20181 73 69 6f 6e 20 61 67 61 69 6e 73 74 20 61 6e 20  sion against an 
20182 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a 20  expression.  ** 
20183 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
20184 74 2e 20 20 52 65 74 75 72 6e 20 61 6e 20 31 2d  t.  Return an 1-
20185 62 61 73 65 64 20 69 6e 64 65 78 20 6f 66 20 74  based index of t
20186 68 65 20 6d 61 74 63 68 69 6e 67 0a 20 20 2a 2a  he matching.  **
20187 20 72 65 73 75 6c 74 2d 73 65 74 20 65 6e 74 72   result-set entr
20188 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  y..  */.  for(i=
20189 30 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78  0; i<pEList->nEx
2018a 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  pr; i++){.    if
2018b 28 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d  ( sqlite3ExprCom
2018c 70 61 72 65 28 70 45 4c 69 73 74 2d 3e 61 5b 69  pare(pEList->a[i
2018d 5d 2e 70 45 78 70 72 2c 20 70 45 29 20 29 7b 0a  ].pExpr, pE) ){.
2018e 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b 31        return i+1
2018f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
20190 2a 20 49 66 20 6e 6f 20 6d 61 74 63 68 2c 20 72  * If no match, r
20191 65 74 75 72 6e 20 30 2e 20 2a 2f 0a 20 20 72 65  eturn 0. */.  re
20192 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
20193 20 47 65 6e 65 72 61 74 65 20 61 6e 20 4f 52 44   Generate an ORD
20194 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42  ER BY or GROUP B
20195 59 20 74 65 72 6d 20 6f 75 74 2d 6f 66 2d 72 61  Y term out-of-ra
20196 6e 67 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a 73 74  nge error..*/.st
20197 61 74 69 63 20 76 6f 69 64 20 72 65 73 6f 6c 76  atic void resolv
20198 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f 72  eOutOfRangeError
20199 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2019a 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  e,         /* Th
2019b 65 20 65 72 72 6f 72 20 63 6f 6e 74 65 78 74 20  e error context 
2019c 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72  into which to wr
2019d 69 74 65 20 74 68 65 20 65 72 72 6f 72 20 2a 2f  ite the error */
2019e 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2019f 54 79 70 65 2c 20 20 20 20 20 2f 2a 20 22 4f 52  Type,     /* "OR
201a0 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50 22 20  DER" or "GROUP" 
201a1 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 20 20 20 20  */.  int i,     
201a2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
201a3 68 65 20 69 6e 64 65 78 20 28 31 2d 62 61 73 65  he index (1-base
201a4 64 29 20 6f 66 20 74 68 65 20 74 65 72 6d 20 6f  d) of the term o
201a5 75 74 20 6f 66 20 72 61 6e 67 65 20 2a 2f 0a 20  ut of range */. 
201a6 20 69 6e 74 20 6d 78 20 20 20 20 20 20 20 20 20   int mx         
201a7 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 72 67 65          /* Large
201a8 73 74 20 70 65 72 6d 69 73 73 69 62 6c 65 20 76  st permissible v
201a9 61 6c 75 65 20 6f 66 20 69 20 2a 2f 0a 29 7b 0a  alue of i */.){.
201aa 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
201ab 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 22  g(pParse, .    "
201ac 25 72 20 25 73 20 42 59 20 74 65 72 6d 20 6f 75  %r %s BY term ou
201ad 74 20 6f 66 20 72 61 6e 67 65 20 2d 20 73 68 6f  t of range - sho
201ae 75 6c 64 20 62 65 20 22 0a 20 20 20 20 22 62 65  uld be ".    "be
201af 74 77 65 65 6e 20 31 20 61 6e 64 20 25 64 22 2c  tween 1 and %d",
201b0 20 69 2c 20 7a 54 79 70 65 2c 20 6d 78 29 3b 0a   i, zType, mx);.
201b1 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a 65  }../*.** Analyze
201b2 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
201b3 61 75 73 65 20 69 6e 20 61 20 63 6f 6d 70 6f 75  ause in a compou
201b4 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  nd SELECT statem
201b5 65 6e 74 2e 20 20 20 4d 6f 64 69 66 79 0a 2a 2a  ent.   Modify.**
201b6 20 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68   each term of th
201b7 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
201b8 65 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 20  e is a constant 
201b9 69 6e 74 65 67 65 72 20 62 65 74 77 65 65 6e 20  integer between 
201ba 31 0a 2a 2a 20 61 6e 64 20 4e 20 77 68 65 72 65  1.** and N where
201bb 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72   N is the number
201bc 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
201bd 68 65 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45  he compound SELE
201be 43 54 2e 0a 2a 2a 0a 2a 2a 20 4f 52 44 45 52 20  CT..**.** ORDER 
201bf 42 59 20 74 65 72 6d 73 20 74 68 61 74 20 61 72  BY terms that ar
201c0 65 20 61 6c 72 65 61 64 79 20 61 6e 20 69 6e 74  e already an int
201c1 65 67 65 72 20 62 65 74 77 65 65 6e 20 31 20 61  eger between 1 a
201c2 6e 64 20 4e 20 61 72 65 0a 2a 2a 20 75 6e 6d 6f  nd N are.** unmo
201c3 64 69 66 69 65 64 2e 20 20 4f 52 44 45 52 20 42  dified.  ORDER B
201c4 59 20 74 65 72 6d 73 20 74 68 61 74 20 61 72 65  Y terms that are
201c5 20 69 6e 74 65 67 65 72 73 20 6f 75 74 73 69 64   integers outsid
201c6 65 20 74 68 65 20 72 61 6e 67 65 20 6f 66 0a 2a  e the range of.*
201c7 2a 20 31 20 74 68 72 6f 75 67 68 20 4e 20 67 65  * 1 through N ge
201c8 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 2e  nerate an error.
201c9 20 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73    ORDER BY terms
201ca 20 74 68 61 74 20 61 72 65 20 65 78 70 72 65 73   that are expres
201cb 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 74  sions.** are mat
201cc 63 68 65 64 20 61 67 61 69 6e 73 74 20 72 65 73  ched against res
201cd 75 6c 74 20 73 65 74 20 65 78 70 72 65 73 73 69  ult set expressi
201ce 6f 6e 73 20 6f 66 20 63 6f 6d 70 6f 75 6e 64 20  ons of compound 
201cf 53 45 4c 45 43 54 0a 2a 2a 20 62 65 67 69 6e 6e  SELECT.** beginn
201d0 69 6e 67 20 77 69 74 68 20 74 68 65 20 6c 65 66  ing with the lef
201d1 74 2d 6d 6f 73 74 20 53 45 4c 45 43 54 20 61 6e  t-most SELECT an
201d2 64 20 77 6f 72 6b 69 6e 67 20 74 6f 77 61 72 64  d working toward
201d3 20 74 68 65 20 72 69 67 68 74 2e 0a 2a 2a 20 41   the right..** A
201d4 74 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 63  t the first matc
201d5 68 2c 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  h, the ORDER BY 
201d6 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 72  expression is tr
201d7 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f 0a 2a  ansformed into.*
201d8 2a 20 74 68 65 20 69 6e 74 65 67 65 72 20 63 6f  * the integer co
201d9 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a  lumn number..**.
201da 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
201db 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20 73  mber of errors s
201dc 65 65 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  een..*/.static i
201dd 6e 74 20 72 65 73 6f 6c 76 65 43 6f 6d 70 6f 75  nt resolveCompou
201de 6e 64 4f 72 64 65 72 42 79 28 0a 20 20 50 61 72  ndOrderBy(.  Par
201df 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
201e0 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
201e1 6e 74 65 78 74 2e 20 20 4c 65 61 76 65 20 65 72  ntext.  Leave er
201e2 72 6f 72 20 6d 65 73 73 61 67 65 73 20 68 65 72  ror messages her
201e3 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  e */.  Select *p
201e4 53 65 6c 65 63 74 20 20 20 20 20 20 20 2f 2a 20  Select       /* 
201e5 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  The SELECT state
201e6 6d 65 6e 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ment containing 
201e7 74 68 65 20 4f 52 44 45 52 20 42 59 20 2a 2f 0a  the ORDER BY */.
201e8 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 45 78  ){.  int i;.  Ex
201e9 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
201ea 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  ;.  ExprList *pE
201eb 4c 69 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 20  List;.  sqlite3 
201ec 2a 64 62 3b 0a 20 20 69 6e 74 20 6d 6f 72 65 54  *db;.  int moreT
201ed 6f 44 6f 20 3d 20 31 3b 0a 0a 20 20 70 4f 72 64  oDo = 1;..  pOrd
201ee 65 72 42 79 20 3d 20 70 53 65 6c 65 63 74 2d 3e  erBy = pSelect->
201ef 70 4f 72 64 65 72 42 79 3b 0a 20 20 69 66 28 20  pOrderBy;.  if( 
201f0 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 20 72 65  pOrderBy==0 ) re
201f1 74 75 72 6e 20 30 3b 0a 20 20 64 62 20 3d 20 70  turn 0;.  db = p
201f2 50 61 72 73 65 2d 3e 64 62 3b 0a 23 69 66 20 53  Parse->db;.#if S
201f3 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e  QLITE_MAX_COLUMN
201f4 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 2d  .  if( pOrderBy-
201f5 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d 69  >nExpr>db->aLimi
201f6 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43  t[SQLITE_LIMIT_C
201f7 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 71  OLUMN] ){.    sq
201f8 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
201f9 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20  arse, "too many 
201fa 74 65 72 6d 73 20 69 6e 20 4f 52 44 45 52 20 42  terms in ORDER B
201fb 59 20 63 6c 61 75 73 65 22 29 3b 0a 20 20 20 20  Y clause");.    
201fc 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65  return 1;.  }.#e
201fd 6e 64 69 66 0a 20 20 66 6f 72 28 69 3d 30 3b 20  ndif.  for(i=0; 
201fe 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  i<pOrderBy->nExp
201ff 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 4f 72  r; i++){.    pOr
20200 64 65 72 42 79 2d 3e 61 5b 69 5d 2e 64 6f 6e 65  derBy->a[i].done
20201 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 53 65 6c   = 0;.  }.  pSel
20202 65 63 74 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a  ect->pNext = 0;.
20203 20 20 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74    while( pSelect
20204 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20  ->pPrior ){.    
20205 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 2d  pSelect->pPrior-
20206 3e 70 4e 65 78 74 20 3d 20 70 53 65 6c 65 63 74  >pNext = pSelect
20207 3b 0a 20 20 20 20 70 53 65 6c 65 63 74 20 3d 20  ;.    pSelect = 
20208 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 3b  pSelect->pPrior;
20209 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70 53  .  }.  while( pS
2020a 65 6c 65 63 74 20 26 26 20 6d 6f 72 65 54 6f 44  elect && moreToD
2020b 6f 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  o ){.    struct 
2020c 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
2020d 49 74 65 6d 3b 0a 20 20 20 20 6d 6f 72 65 54 6f  Item;.    moreTo
2020e 44 6f 20 3d 20 30 3b 0a 20 20 20 20 70 45 4c 69  Do = 0;.    pELi
2020f 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45  st = pSelect->pE
20210 4c 69 73 74 3b 0a 20 20 20 20 61 73 73 65 72 74  List;.    assert
20211 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a 20  ( pEList!=0 );. 
20212 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65     for(i=0, pIte
20213 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69  m=pOrderBy->a; i
20214 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72  <pOrderBy->nExpr
20215 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
20216 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20  .      int iCol 
20217 3d 20 2d 31 3b 0a 20 20 20 20 20 20 45 78 70 72  = -1;.      Expr
20218 20 2a 70 45 2c 20 2a 70 44 75 70 3b 0a 20 20 20   *pE, *pDup;.   
20219 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 64 6f     if( pItem->do
2021a 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  ne ) continue;. 
2021b 20 20 20 20 20 70 45 20 3d 20 70 49 74 65 6d 2d       pE = pItem-
2021c 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66  >pExpr;.      if
2021d 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49  ( sqlite3ExprIsI
2021e 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43 6f 6c  nteger(pE, &iCol
2021f 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  ) ){.        if(
20220 20 69 43 6f 6c 3c 3d 30 20 7c 7c 20 69 43 6f 6c   iCol<=0 || iCol
20221 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 20 29  >pEList->nExpr )
20222 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 73 6f  {.          reso
20223 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72 72  lveOutOfRangeErr
20224 6f 72 28 70 50 61 72 73 65 2c 20 22 4f 52 44 45  or(pParse, "ORDE
20225 52 22 2c 20 69 2b 31 2c 20 70 45 4c 69 73 74 2d  R", i+1, pEList-
20226 3e 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20  >nExpr);.       
20227 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
20228 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
20229 73 65 7b 0a 20 20 20 20 20 20 20 20 69 43 6f 6c  se{.        iCol
2022a 20 3d 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d 65   = resolveAsName
2022b 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74 2c  (pParse, pEList,
2022c 20 70 45 29 3b 0a 20 20 20 20 20 20 20 20 69 66   pE);.        if
2022d 28 20 69 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20  ( iCol==0 ){.   
2022e 20 20 20 20 20 20 20 70 44 75 70 20 3d 20 73 71         pDup = sq
2022f 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
20230 20 70 45 2c 20 30 29 3b 0a 20 20 20 20 20 20 20   pE, 0);.       
20231 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c     if( !db->mall
20232 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
20233 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70          assert(p
20234 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Dup);.          
20235 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65    iCol = resolve
20236 4f 72 64 65 72 42 79 54 65 72 6d 54 6f 45 78 70  OrderByTermToExp
20237 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 53  rList(pParse, pS
20238 65 6c 65 63 74 2c 20 70 44 75 70 29 3b 0a 20 20  elect, pDup);.  
20239 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2023a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
2023b 65 6c 65 74 65 28 64 62 2c 20 70 44 75 70 29 3b  elete(db, pDup);
2023c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2023d 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f   }.      if( iCo
2023e 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 43  l>0 ){.        C
2023f 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20  ollSeq *pColl = 
20240 70 45 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20  pE->pColl;.     
20241 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70     int flags = p
20242 45 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78  E->flags & EP_Ex
20243 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20  pCollate;.      
20244 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
20245 65 74 65 28 64 62 2c 20 70 45 29 3b 0a 20 20 20  ete(db, pE);.   
20246 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70       pItem->pExp
20247 72 20 3d 20 70 45 20 3d 20 73 71 6c 69 74 65 33  r = pE = sqlite3
20248 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 4e 54 45  Expr(db, TK_INTE
20249 47 45 52 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  GER, 0);.       
2024a 20 69 66 28 20 70 45 3d 3d 30 20 29 20 72 65 74   if( pE==0 ) ret
2024b 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 70  urn 1;.        p
2024c 45 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c  E->pColl = pColl
2024d 3b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e 66 6c  ;.        pE->fl
2024e 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c  ags |= EP_IntVal
2024f 75 65 20 7c 20 66 6c 61 67 73 3b 0a 20 20 20 20  ue | flags;.    
20250 20 20 20 20 70 45 2d 3e 75 2e 69 56 61 6c 75 65      pE->u.iValue
20251 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20   = iCol;.       
20252 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28   pItem->iCol = (
20253 75 31 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20  u16)iCol;.      
20254 20 20 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20    pItem->done = 
20255 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  1;.      }else{.
20256 20 20 20 20 20 20 20 20 6d 6f 72 65 54 6f 44 6f          moreToDo
20257 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
20258 20 20 7d 0a 20 20 20 20 70 53 65 6c 65 63 74 20    }.    pSelect 
20259 3d 20 70 53 65 6c 65 63 74 2d 3e 70 4e 65 78 74  = pSelect->pNext
2025a 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b  ;.  }.  for(i=0;
2025b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78   i<pOrderBy->nEx
2025c 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  pr; i++){.    if
2025d 28 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d  ( pOrderBy->a[i]
2025e 2e 64 6f 6e 65 3d 3d 30 20 29 7b 0a 20 20 20 20  .done==0 ){.    
2025f 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
20260 67 28 70 50 61 72 73 65 2c 20 22 25 72 20 4f 52  g(pParse, "%r OR
20261 44 45 52 20 42 59 20 74 65 72 6d 20 64 6f 65 73  DER BY term does
20262 20 6e 6f 74 20 6d 61 74 63 68 20 61 6e 79 20 22   not match any "
20263 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 63 6f  .            "co
20264 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75  lumn in the resu
20265 6c 74 20 73 65 74 22 2c 20 69 2b 31 29 3b 0a 20  lt set", i+1);. 
20266 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
20267 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
20268 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68  n 0;.}../*.** Ch
20269 65 63 6b 20 65 76 65 72 79 20 74 65 72 6d 20 69  eck every term i
2026a 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 6f  n the ORDER BY o
2026b 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  r GROUP BY claus
2026c 65 20 70 4f 72 64 65 72 42 79 20 6f 66 0a 2a 2a  e pOrderBy of.**
2026d 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
2026e 65 6d 65 6e 74 20 70 53 65 6c 65 63 74 2e 20 20  ement pSelect.  
2026f 49 66 20 61 6e 79 20 74 65 72 6d 20 69 73 20 72  If any term is r
20270 65 66 65 72 65 6e 63 65 20 74 6f 20 61 0a 2a 2a  eference to a.**
20271 20 72 65 73 75 6c 74 20 73 65 74 20 65 78 70 72   result set expr
20272 65 73 73 69 6f 6e 20 28 61 73 20 64 65 74 65 72  ession (as deter
20273 6d 69 6e 65 64 20 62 79 20 74 68 65 20 45 78 70  mined by the Exp
20274 72 4c 69 73 74 2e 61 2e 69 43 6f 6c 20 66 69 65  rList.a.iCol fie
20275 6c 64 29 0a 2a 2a 20 74 68 65 6e 20 63 6f 6e 76  ld).** then conv
20276 65 72 74 20 74 68 61 74 20 74 65 72 6d 20 69 6e  ert that term in
20277 74 6f 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  to a copy of the
20278 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 72   corresponding r
20279 65 73 75 6c 74 20 73 65 74 0a 2a 2a 20 63 6f 6c  esult set.** col
2027a 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  umn..**.** If an
2027b 79 20 65 72 72 6f 72 73 20 61 72 65 20 64 65 74  y errors are det
2027c 65 63 74 65 64 2c 20 61 64 64 20 61 6e 20 65 72  ected, add an er
2027d 72 6f 72 20 6d 65 73 73 61 67 65 20 74 6f 20 70  ror message to p
2027e 50 61 72 73 65 20 61 6e 64 0a 2a 2a 20 72 65 74  Parse and.** ret
2027f 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 52  urn non-zero.  R
20280 65 74 75 72 6e 20 7a 65 72 6f 20 69 66 20 6e 6f  eturn zero if no
20281 20 65 72 72 6f 72 73 20 61 72 65 20 73 65 65 6e   errors are seen
20282 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
20283 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
20284 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70  esolveOrderGroup
20285 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  By(.  Parse *pPa
20286 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
20287 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20  arsing context. 
20288 20 4c 65 61 76 65 20 65 72 72 6f 72 20 6d 65 73   Leave error mes
20289 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 20  sages here */.  
2028a 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c  Select *pSelect,
2028b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c        /* The SEL
2028c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 63 6f  ECT statement co
2028d 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 63 6c 61  ntaining the cla
2028e 75 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  use */.  ExprLis
2028f 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f  t *pOrderBy,   /
20290 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 6f  * The ORDER BY o
20291 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  r GROUP BY claus
20292 65 20 74 6f 20 62 65 20 70 72 6f 63 65 73 73 65  e to be processe
20293 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  d */.  const cha
20294 72 20 2a 7a 54 79 70 65 20 20 20 20 20 2f 2a 20  r *zType     /* 
20295 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55  "ORDER" or "GROU
20296 50 22 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  P" */.){.  int i
20297 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
20298 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
20299 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
2029a 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c  ;.  struct ExprL
2029b 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
2029c 0a 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79  ..  if( pOrderBy
2029d 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64  ==0 || pParse->d
2029e 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2029f 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 69 66 20  ) return 0;.#if 
202a0 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d  SQLITE_MAX_COLUM
202a1 4e 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79  N.  if( pOrderBy
202a2 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d  ->nExpr>db->aLim
202a3 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
202a4 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73  COLUMN] ){.    s
202a5 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
202a6 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79  Parse, "too many
202a7 20 74 65 72 6d 73 20 69 6e 20 25 73 20 42 59 20   terms in %s BY 
202a8 63 6c 61 75 73 65 22 2c 20 7a 54 79 70 65 29 3b  clause", zType);
202a9 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
202aa 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 45 4c 69   }.#endif.  pELi
202ab 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45  st = pSelect->pE
202ac 4c 69 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20  List;.  assert( 
202ad 70 45 4c 69 73 74 21 3d 30 20 29 3b 20 20 2f 2a  pEList!=0 );  /*
202ae 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65   sqlite3SelectNe
202af 77 28 29 20 67 75 61 72 61 6e 74 65 65 73 20 74  w() guarantees t
202b0 68 69 73 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  his */.  for(i=0
202b1 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42 79  , pItem=pOrderBy
202b2 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d  ->a; i<pOrderBy-
202b3 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74  >nExpr; i++, pIt
202b4 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70  em++){.    if( p
202b5 49 74 65 6d 2d 3e 69 43 6f 6c 20 29 7b 0a 20 20  Item->iCol ){.  
202b6 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69      if( pItem->i
202b7 43 6f 6c 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70  Col>pEList->nExp
202b8 72 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 73  r ){.        res
202b9 6f 6c 76 65 4f 75 74 4f 66 52 61 6e 67 65 45 72  olveOutOfRangeEr
202ba 72 6f 72 28 70 50 61 72 73 65 2c 20 7a 54 79 70  ror(pParse, zTyp
202bb 65 2c 20 69 2b 31 2c 20 70 45 4c 69 73 74 2d 3e  e, i+1, pEList->
202bc 6e 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20 20  nExpr);.        
202bd 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
202be 7d 0a 20 20 20 20 20 20 72 65 73 6f 6c 76 65 41  }.      resolveA
202bf 6c 69 61 73 28 70 50 61 72 73 65 2c 20 70 45 4c  lias(pParse, pEL
202c0 69 73 74 2c 20 70 49 74 65 6d 2d 3e 69 43 6f 6c  ist, pItem->iCol
202c1 2d 31 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  -1, pItem->pExpr
202c2 2c 20 7a 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a  , zType);.    }.
202c3 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
202c4 7d 0a 0a 2f 2a 0a 2a 2a 20 70 4f 72 64 65 72 42  }../*.** pOrderB
202c5 79 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59  y is an ORDER BY
202c6 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61   or GROUP BY cla
202c7 75 73 65 20 69 6e 20 53 45 4c 45 43 54 20 73 74  use in SELECT st
202c8 61 74 65 6d 65 6e 74 20 70 53 65 6c 65 63 74 2e  atement pSelect.
202c9 0a 2a 2a 20 54 68 65 20 4e 61 6d 65 20 63 6f 6e  .** The Name con
202ca 74 65 78 74 20 6f 66 20 74 68 65 20 53 45 4c 45  text of the SELE
202cb 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  CT statement is 
202cc 70 4e 43 2e 20 20 7a 54 79 70 65 20 69 73 20 65  pNC.  zType is e
202cd 69 74 68 65 72 0a 2a 2a 20 22 4f 52 44 45 52 22  ither.** "ORDER"
202ce 20 6f 72 20 22 47 52 4f 55 50 22 20 64 65 70 65   or "GROUP" depe
202cf 6e 64 69 6e 67 20 6f 6e 20 77 68 69 63 68 20 74  nding on which t
202d0 79 70 65 20 6f 66 20 63 6c 61 75 73 65 20 70 4f  ype of clause pO
202d1 72 64 65 72 42 79 20 69 73 2e 0a 2a 2a 0a 2a 2a  rderBy is..**.**
202d2 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
202d3 73 6f 6c 76 65 73 20 65 61 63 68 20 74 65 72 6d  solves each term
202d4 20 6f 66 20 74 68 65 20 63 6c 61 75 73 65 20 69   of the clause i
202d5 6e 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69 6f  nto an expressio
202d6 6e 2e 0a 2a 2a 20 49 66 20 74 68 65 20 6f 72 64  n..** If the ord
202d7 65 72 2d 62 79 20 74 65 72 6d 20 69 73 20 61 6e  er-by term is an
202d8 20 69 6e 74 65 67 65 72 20 49 20 62 65 74 77 65   integer I betwe
202d9 65 6e 20 31 20 61 6e 64 20 4e 20 28 77 68 65 72  en 1 and N (wher
202da 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75  e N is the.** nu
202db 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
202dc 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  in the result se
202dd 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 29  t of the SELECT)
202de 20 74 68 65 6e 20 74 68 65 20 65 78 70 72 65 73   then the expres
202df 73 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 72  sion.** in the r
202e0 65 73 6f 6c 75 74 69 6f 6e 20 69 73 20 61 20 63  esolution is a c
202e1 6f 70 79 20 6f 66 20 74 68 65 20 49 2d 74 68 20  opy of the I-th 
202e2 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65  result-set expre
202e3 73 73 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 74 68  ssion.  If.** th
202e4 65 20 6f 72 64 65 72 2d 62 79 20 74 65 72 6d 20  e order-by term 
202e5 69 73 20 61 6e 20 69 64 65 6e 74 69 66 79 20 74  is an identify t
202e6 68 61 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  hat corresponds 
202e7 74 6f 20 74 68 65 20 41 53 2d 6e 61 6d 65 20 6f  to the AS-name o
202e8 66 0a 2a 2a 20 61 20 72 65 73 75 6c 74 2d 73 65  f.** a result-se
202e9 74 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 74 68  t expression, th
202ea 65 6e 20 74 68 65 20 74 65 72 6d 20 72 65 73 6f  en the term reso
202eb 6c 76 65 73 20 74 6f 20 61 20 63 6f 70 79 20 6f  lves to a copy o
202ec 66 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d  f the.** result-
202ed 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 2e 20  set expression. 
202ee 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20   Otherwise, the 
202ef 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 72 65  expression is re
202f0 73 6f 6c 76 65 64 20 69 6e 0a 2a 2a 20 74 68 65  solved in.** the
202f1 20 75 73 75 61 6c 20 77 61 79 20 2d 20 75 73 69   usual way - usi
202f2 6e 67 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  ng sqlite3Resolv
202f3 65 45 78 70 72 4e 61 6d 65 73 28 29 2e 0a 2a 2a  eExprNames()..**
202f4 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
202f5 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   returns the num
202f6 62 65 72 20 6f 66 20 65 72 72 6f 72 73 2e 20 20  ber of errors.  
202f7 49 66 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c  If errors occur,
202f8 20 74 68 65 6e 0a 2a 2a 20 61 6e 20 61 70 70 72   then.** an appr
202f9 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65  opriate error me
202fa 73 73 61 67 65 20 6d 69 67 68 74 20 62 65 20 6c  ssage might be l
202fb 65 66 74 20 69 6e 20 70 50 61 72 73 65 2e 20 20  eft in pParse.  
202fc 28 4f 4f 4d 20 65 72 72 6f 72 73 0a 2a 2a 20 65  (OOM errors.** e
202fd 78 63 65 70 74 65 64 2e 29 0a 2a 2f 0a 73 74 61  xcepted.).*/.sta
202fe 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 4f  tic int resolveO
202ff 72 64 65 72 47 72 6f 75 70 42 79 28 0a 20 20 4e  rderGroupBy(.  N
20300 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c  ameContext *pNC,
20301 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65       /* The name
20302 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20   context of the 
20303 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
20304 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53   */.  Select *pS
20305 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 54  elect,      /* T
20306 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
20307 65 6e 74 20 68 6f 6c 64 69 6e 67 20 70 4f 72 64  ent holding pOrd
20308 65 72 42 79 20 2a 2f 0a 20 20 45 78 70 72 4c 69  erBy */.  ExprLi
20309 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20  st *pOrderBy,   
2030a 2f 2a 20 41 6e 20 4f 52 44 45 52 20 42 59 20 6f  /* An ORDER BY o
2030b 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  r GROUP BY claus
2030c 65 20 74 6f 20 72 65 73 6f 6c 76 65 20 2a 2f 0a  e to resolve */.
2030d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
2030e 79 70 65 20 20 20 20 20 2f 2a 20 45 69 74 68 65  ype     /* Eithe
2030f 72 20 22 4f 52 44 45 52 22 20 6f 72 20 22 47 52  r "ORDER" or "GR
20310 4f 55 50 22 2c 20 61 73 20 61 70 70 72 6f 70 72  OUP", as appropr
20311 69 61 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  iate */.){.  int
20312 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
20313 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
20314 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
20315 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20   int iCol;      
20316 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20317 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  /* Column number
20318 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70   */.  struct Exp
20319 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  rList_item *pIte
2031a 6d 3b 20 20 20 2f 2a 20 41 20 74 65 72 6d 20 6f  m;   /* A term o
2031b 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  f the ORDER BY c
2031c 6c 61 75 73 65 20 2a 2f 0a 20 20 50 61 72 73 65  lause */.  Parse
2031d 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20   *pParse;       
2031e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
2031f 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
20320 20 20 69 6e 74 20 6e 52 65 73 75 6c 74 3b 20 20    int nResult;  
20321 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20322 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65   /* Number of te
20323 72 6d 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c  rms in the resul
20324 74 20 73 65 74 20 2a 2f 0a 0a 20 20 69 66 28 20  t set */..  if( 
20325 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 20 72 65  pOrderBy==0 ) re
20326 74 75 72 6e 20 30 3b 0a 20 20 6e 52 65 73 75 6c  turn 0;.  nResul
20327 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c  t = pSelect->pEL
20328 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 50  ist->nExpr;.  pP
20329 61 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72  arse = pNC->pPar
2032a 73 65 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70  se;.  for(i=0, p
2032b 49 74 65 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61  Item=pOrderBy->a
2032c 3b 20 69 3c 70 4f 72 64 65 72 42 79 2d 3e 6e 45  ; i<pOrderBy->nE
2032d 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  xpr; i++, pItem+
2032e 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 45  +){.    Expr *pE
2032f 20 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b   = pItem->pExpr;
20330 0a 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f  .    iCol = reso
20331 6c 76 65 41 73 4e 61 6d 65 28 70 50 61 72 73 65  lveAsName(pParse
20332 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73  , pSelect->pELis
20333 74 2c 20 70 45 29 3b 0a 20 20 20 20 69 66 28 20  t, pE);.    if( 
20334 69 43 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20  iCol>0 ){.      
20335 2f 2a 20 49 66 20 61 6e 20 41 53 2d 6e 61 6d 65  /* If an AS-name
20336 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 2c   match is found,
20337 20 6d 61 72 6b 20 74 68 69 73 20 4f 52 44 45 52   mark this ORDER
20338 20 42 59 20 63 6f 6c 75 6d 6e 20 61 73 20 62 65   BY column as be
20339 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 61 20 63  ing.      ** a c
2033a 6f 70 79 20 6f 66 20 74 68 65 20 69 43 6f 6c 2d  opy of the iCol-
2033b 74 68 20 72 65 73 75 6c 74 2d 73 65 74 20 63 6f  th result-set co
2033c 6c 75 6d 6e 2e 20 20 54 68 65 20 73 75 62 73 65  lumn.  The subse
2033d 71 75 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 20 20  quent call to.  
2033e 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 52 65      ** sqlite3Re
2033f 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42  solveOrderGroupB
20340 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72 74  y() will convert
20341 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
20342 74 6f 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f  to a.      ** co
20343 70 79 20 6f 66 20 74 68 65 20 69 43 6f 6c 2d 74  py of the iCol-t
20344 68 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70  h result-set exp
20345 72 65 73 73 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20  ression. */.    
20346 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20    pItem->iCol = 
20347 28 75 31 36 29 69 43 6f 6c 3b 0a 20 20 20 20 20  (u16)iCol;.     
20348 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d   continue;.    }
20349 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
2034a 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 45  ExprIsInteger(pE
2034b 2c 20 26 69 43 6f 6c 29 20 29 7b 0a 20 20 20 20  , &iCol) ){.    
2034c 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42    /* The ORDER B
2034d 59 20 74 65 72 6d 20 69 73 20 61 6e 20 69 6e 74  Y term is an int
2034e 65 67 65 72 20 63 6f 6e 73 74 61 6e 74 2e 20 20  eger constant.  
2034f 41 67 61 69 6e 2c 20 73 65 74 20 74 68 65 20 63  Again, set the c
20350 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 2a 2a 20 6e  olumn.      ** n
20351 75 6d 62 65 72 20 73 6f 20 74 68 61 74 20 73 71  umber so that sq
20352 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65  lite3ResolveOrde
20353 72 47 72 6f 75 70 42 79 28 29 20 77 69 6c 6c 20  rGroupBy() will 
20354 63 6f 6e 76 65 72 74 20 74 68 65 0a 20 20 20 20  convert the.    
20355 20 20 2a 2a 20 6f 72 64 65 72 2d 62 79 20 74 65    ** order-by te
20356 72 6d 20 74 6f 20 61 20 63 6f 70 79 20 6f 66 20  rm to a copy of 
20357 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 20 65  the result-set e
20358 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20  xpression */.   
20359 20 20 20 69 66 28 20 69 43 6f 6c 3c 31 20 29 7b     if( iCol<1 ){
2035a 0a 20 20 20 20 20 20 20 20 72 65 73 6f 6c 76 65  .        resolve
2035b 4f 75 74 4f 66 52 61 6e 67 65 45 72 72 6f 72 28  OutOfRangeError(
2035c 70 50 61 72 73 65 2c 20 7a 54 79 70 65 2c 20 69  pParse, zType, i
2035d 2b 31 2c 20 6e 52 65 73 75 6c 74 29 3b 0a 20 20  +1, nResult);.  
2035e 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
2035f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 49        }.      pI
20360 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 28 75 31 36  tem->iCol = (u16
20361 29 69 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e  )iCol;.      con
20362 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 20  tinue;.    }..  
20363 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 2c 20    /* Otherwise, 
20364 74 72 65 61 74 20 74 68 65 20 4f 52 44 45 52 20  treat the ORDER 
20365 42 59 20 74 65 72 6d 20 61 73 20 61 6e 20 6f 72  BY term as an or
20366 64 69 6e 61 72 79 20 65 78 70 72 65 73 73 69 6f  dinary expressio
20367 6e 20 2a 2f 0a 20 20 20 20 70 49 74 65 6d 2d 3e  n */.    pItem->
20368 69 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 69 66  iCol = 0;.    if
20369 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65  ( sqlite3Resolve
2036a 45 78 70 72 4e 61 6d 65 73 28 70 4e 43 2c 20 70  ExprNames(pNC, p
2036b 45 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  E) ){.      retu
2036c 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn 1;.    }.  }.
2036d 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
2036e 52 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75  ResolveOrderGrou
2036f 70 42 79 28 70 50 61 72 73 65 2c 20 70 53 65 6c  pBy(pParse, pSel
20370 65 63 74 2c 20 70 4f 72 64 65 72 42 79 2c 20 7a  ect, pOrderBy, z
20371 54 79 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Type);.}../*.** 
20372 52 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e  Resolve names in
20373 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61 74   the SELECT stat
20374 65 6d 65 6e 74 20 70 20 61 6e 64 20 61 6c 6c 20  ement p and all 
20375 6f 66 20 69 74 73 20 64 65 73 63 65 6e 64 65 6e  of its descenden
20376 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ts..*/.static in
20377 74 20 72 65 73 6f 6c 76 65 53 65 6c 65 63 74 53  t resolveSelectS
20378 74 65 70 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c  tep(Walker *pWal
20379 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b  ker, Select *p){
2037a 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a  .  NameContext *
2037b 70 4f 75 74 65 72 4e 43 3b 20 20 2f 2a 20 43 6f  pOuterNC;  /* Co
2037c 6e 74 65 78 74 20 74 68 61 74 20 63 6f 6e 74 61  ntext that conta
2037d 69 6e 73 20 74 68 69 73 20 53 45 4c 45 43 54 20  ins this SELECT 
2037e 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  */.  NameContext
2037f 20 73 4e 43 3b 20 20 20 20 20 20 20 20 2f 2a 20   sNC;        /* 
20380 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66 20  Name context of 
20381 74 68 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20  this SELECT */. 
20382 20 69 6e 74 20 69 73 43 6f 6d 70 6f 75 6e 64 3b   int isCompound;
20383 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
20384 20 69 66 20 70 20 69 73 20 61 20 63 6f 6d 70 6f   if p is a compo
20385 75 6e 64 20 73 65 6c 65 63 74 20 2a 2f 0a 20 20  und select */.  
20386 69 6e 74 20 6e 43 6f 6d 70 6f 75 6e 64 3b 20 20  int nCompound;  
20387 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
20388 72 20 6f 66 20 63 6f 6d 70 6f 75 6e 64 20 74 65  r of compound te
20389 72 6d 73 20 70 72 6f 63 65 73 73 65 64 20 73 6f  rms processed so
2038a 20 66 61 72 20 2a 2f 0a 20 20 50 61 72 73 65 20   far */.  Parse 
2038b 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20  *pParse;        
2038c 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
2038d 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69  text */.  ExprLi
2038e 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20  st *pEList;     
2038f 20 20 2f 2a 20 52 65 73 75 6c 74 20 73 65 74 20    /* Result set 
20390 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20  expression list 
20391 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20  */.  int i;     
20392 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
20393 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
20394 20 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f    ExprList *pGro
20395 75 70 42 79 3b 20 20 20 20 20 2f 2a 20 54 68 65  upBy;     /* The
20396 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
20397 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 4c   */.  Select *pL
20398 65 66 74 6d 6f 73 74 3b 20 20 20 20 20 20 2f 2a  eftmost;      /*
20399 20 4c 65 66 74 2d 6d 6f 73 74 20 6f 66 20 53 45   Left-most of SE
2039a 4c 45 43 54 20 6f 66 20 61 20 63 6f 6d 70 6f 75  LECT of a compou
2039b 6e 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  nd */.  sqlite3 
2039c 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  *db;            
2039d 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e  /* Database conn
2039e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 0a 0a 20 20  ection */.  ..  
2039f 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a  assert( p!=0 );.
203a0 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67    if( p->selFlag
203a1 73 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 20  s & SF_Resolved 
203a2 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52  ){.    return WR
203a3 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 70  C_Prune;.  }.  p
203a4 4f 75 74 65 72 4e 43 20 3d 20 70 57 61 6c 6b 65  OuterNC = pWalke
203a5 72 2d 3e 75 2e 70 4e 43 3b 0a 20 20 70 50 61 72  r->u.pNC;.  pPar
203a6 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50  se = pWalker->pP
203a7 61 72 73 65 3b 0a 20 20 64 62 20 3d 20 70 50 61  arse;.  db = pPa
203a8 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 4e  rse->db;..  /* N
203a9 6f 72 6d 61 6c 6c 79 20 73 71 6c 69 74 65 33 53  ormally sqlite3S
203aa 65 6c 65 63 74 45 78 70 61 6e 64 28 29 20 77 69  electExpand() wi
203ab 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 66 69 72  ll be called fir
203ac 73 74 20 61 6e 64 20 77 69 6c 6c 20 68 61 76 65  st and will have
203ad 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 65 78  .  ** already ex
203ae 70 61 6e 64 65 64 20 74 68 69 73 20 53 45 4c 45  panded this SELE
203af 43 54 2e 20 20 48 6f 77 65 76 65 72 2c 20 69 66  CT.  However, if
203b0 20 74 68 69 73 20 69 73 20 61 20 73 75 62 71 75   this is a subqu
203b1 65 72 79 20 77 69 74 68 69 6e 0a 20 20 2a 2a 20  ery within.  ** 
203b2 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 73  an expression, s
203b3 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
203b4 72 4e 61 6d 65 73 28 29 20 77 69 6c 6c 20 62 65  rNames() will be
203b5 20 63 61 6c 6c 65 64 20 77 69 74 68 6f 75 74 20   called without 
203b6 61 0a 20 20 2a 2a 20 70 72 69 6f 72 20 63 61 6c  a.  ** prior cal
203b7 6c 20 74 6f 20 73 71 6c 69 74 65 33 53 65 6c 65  l to sqlite3Sele
203b8 63 74 45 78 70 61 6e 64 28 29 2e 20 20 57 68 65  ctExpand().  Whe
203b9 6e 20 74 68 61 74 20 68 61 70 70 65 6e 73 2c 20  n that happens, 
203ba 6c 65 74 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33  let.  ** sqlite3
203bb 53 65 6c 65 63 74 50 72 65 70 28 29 20 64 6f 20  SelectPrep() do 
203bc 61 6c 6c 20 6f 66 20 74 68 65 20 70 72 6f 63 65  all of the proce
203bd 73 73 69 6e 67 20 66 6f 72 20 74 68 69 73 20 53  ssing for this S
203be 45 4c 45 43 54 2e 0a 20 20 2a 2a 20 73 71 6c 69  ELECT..  ** sqli
203bf 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 29 20  te3SelectPrep() 
203c0 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 62 6f 74 68  will invoke both
203c1 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78   sqlite3SelectEx
203c2 70 61 6e 64 28 29 20 61 6e 64 0a 20 20 2a 2a 20  pand() and.  ** 
203c3 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6e 20  this routine in 
203c4 74 68 65 20 63 6f 72 72 65 63 74 20 6f 72 64 65  the correct orde
203c5 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70  r..  */.  if( (p
203c6 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f  ->selFlags & SF_
203c7 45 78 70 61 6e 64 65 64 29 3d 3d 30 20 29 7b 0a  Expanded)==0 ){.
203c8 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
203c9 74 50 72 65 70 28 70 50 61 72 73 65 2c 20 70 2c  tPrep(pParse, p,
203ca 20 70 4f 75 74 65 72 4e 43 29 3b 0a 20 20 20 20   pOuterNC);.    
203cb 72 65 74 75 72 6e 20 28 70 50 61 72 73 65 2d 3e  return (pParse->
203cc 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  nErr || db->mall
203cd 6f 63 46 61 69 6c 65 64 29 20 3f 20 57 52 43 5f  ocFailed) ? WRC_
203ce 41 62 6f 72 74 20 3a 20 57 52 43 5f 50 72 75 6e  Abort : WRC_Prun
203cf 65 3b 0a 20 20 7d 0a 0a 20 20 69 73 43 6f 6d 70  e;.  }..  isComp
203d0 6f 75 6e 64 20 3d 20 70 2d 3e 70 50 72 69 6f 72  ound = p->pPrior
203d1 21 3d 30 3b 0a 20 20 6e 43 6f 6d 70 6f 75 6e 64  !=0;.  nCompound
203d2 20 3d 20 30 3b 0a 20 20 70 4c 65 66 74 6d 6f 73   = 0;.  pLeftmos
203d3 74 20 3d 20 70 3b 0a 20 20 77 68 69 6c 65 28 20  t = p;.  while( 
203d4 70 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  p ){.    assert(
203d5 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20   (p->selFlags & 
203d6 53 46 5f 45 78 70 61 6e 64 65 64 29 21 3d 30 20  SF_Expanded)!=0 
203d7 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  );.    assert( (
203d8 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  p->selFlags & SF
203d9 5f 52 65 73 6f 6c 76 65 64 29 3d 3d 30 20 29 3b  _Resolved)==0 );
203da 0a 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73  .    p->selFlags
203db 20 7c 3d 20 53 46 5f 52 65 73 6f 6c 76 65 64 3b   |= SF_Resolved;
203dc 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65  ..    /* Resolve
203dd 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 73   the expressions
203de 20 69 6e 20 74 68 65 20 4c 49 4d 49 54 20 61 6e   in the LIMIT an
203df 64 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65 73  d OFFSET clauses
203e0 2e 20 54 68 65 73 65 0a 20 20 20 20 2a 2a 20 61  . These.    ** a
203e1 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74  re not allowed t
203e2 6f 20 72 65 66 65 72 20 74 6f 20 61 6e 79 20 6e  o refer to any n
203e3 61 6d 65 73 2c 20 73 6f 20 70 61 73 73 20 61 6e  ames, so pass an
203e4 20 65 6d 70 74 79 20 4e 61 6d 65 43 6f 6e 74 65   empty NameConte
203e5 78 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d  xt..    */.    m
203e6 65 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73  emset(&sNC, 0, s
203e7 69 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 20  izeof(sNC));.   
203e8 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50   sNC.pParse = pP
203e9 61 72 73 65 3b 0a 20 20 20 20 69 66 28 20 73 71  arse;.    if( sq
203ea 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
203eb 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70  Names(&sNC, p->p
203ec 4c 69 6d 69 74 29 20 7c 7c 0a 20 20 20 20 20 20  Limit) ||.      
203ed 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65    sqlite3Resolve
203ee 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20  ExprNames(&sNC, 
203ef 70 2d 3e 70 4f 66 66 73 65 74 29 20 29 7b 0a 20  p->pOffset) ){. 
203f0 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
203f1 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 0a  Abort;.    }.  .
203f2 20 20 20 20 2f 2a 20 53 65 74 20 75 70 20 74 68      /* Set up th
203f3 65 20 6c 6f 63 61 6c 20 6e 61 6d 65 2d 63 6f 6e  e local name-con
203f4 74 65 78 74 20 74 6f 20 70 61 73 73 20 74 6f 20  text to pass to 
203f5 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
203f6 70 72 4e 61 6d 65 73 28 29 20 74 6f 0a 20 20 20  prNames() to.   
203f7 20 2a 2a 20 72 65 73 6f 6c 76 65 20 74 68 65 20   ** resolve the 
203f8 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65  result-set expre
203f9 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 20 20 20 20  ssion list..    
203fa 2a 2f 0a 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77  */.    sNC.allow
203fb 41 67 67 20 3d 20 31 3b 0a 20 20 20 20 73 4e 43  Agg = 1;.    sNC
203fc 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 2d 3e 70  .pSrcList = p->p
203fd 53 72 63 3b 0a 20 20 20 20 73 4e 43 2e 70 4e 65  Src;.    sNC.pNe
203fe 78 74 20 3d 20 70 4f 75 74 65 72 4e 43 3b 0a 20  xt = pOuterNC;. 
203ff 20 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65   .    /* Resolve
20400 20 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 72 65   names in the re
20401 73 75 6c 74 20 73 65 74 2e 20 2a 2f 0a 20 20 20  sult set. */.   
20402 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c   pEList = p->pEL
20403 69 73 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ist;.    assert(
20404 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20   pEList!=0 );.  
20405 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c    for(i=0; i<pEL
20406 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ist->nExpr; i++)
20407 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 58  {.      Expr *pX
20408 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e   = pEList->a[i].
20409 70 45 78 70 72 3b 0a 20 20 20 20 20 20 69 66 28  pExpr;.      if(
2040a 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
2040b 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70  xprNames(&sNC, p
2040c 58 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  X) ){.        re
2040d 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
2040e 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2040f 0a 20 20 20 20 2f 2a 20 52 65 63 75 72 73 69 76  .    /* Recursiv
20410 65 6c 79 20 72 65 73 6f 6c 76 65 20 6e 61 6d 65  ely resolve name
20411 73 20 69 6e 20 61 6c 6c 20 73 75 62 71 75 65 72  s in all subquer
20412 69 65 73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66  ies.    */.    f
20413 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 70 53 72  or(i=0; i<p->pSr
20414 63 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20  c->nSrc; i++){. 
20415 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c       struct SrcL
20416 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20  ist_item *pItem 
20417 3d 20 26 70 2d 3e 70 53 72 63 2d 3e 61 5b 69 5d  = &p->pSrc->a[i]
20418 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65  ;.      if( pIte
20419 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  m->pSelect ){.  
2041a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
2041b 20 2a 7a 53 61 76 65 64 43 6f 6e 74 65 78 74 20   *zSavedContext 
2041c 3d 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43  = pParse->zAuthC
2041d 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20 20 20 20  ontext;.        
2041e 69 66 28 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65  if( pItem->zName
2041f 20 29 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68   ) pParse->zAuth
20420 43 6f 6e 74 65 78 74 20 3d 20 70 49 74 65 6d 2d  Context = pItem-
20421 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20  >zName;.        
20422 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 53 65  sqlite3ResolveSe
20423 6c 65 63 74 4e 61 6d 65 73 28 70 50 61 72 73 65  lectNames(pParse
20424 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74  , pItem->pSelect
20425 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0a 20 20 20  , pOuterNC);.   
20426 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 41 75       pParse->zAu
20427 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a 53 61 76  thContext = zSav
20428 65 64 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20 20  edContext;.     
20429 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e     if( pParse->n
2042a 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Err || db->mallo
2042b 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e  cFailed ) return
2042c 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20   WRC_Abort;.    
2042d 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20    }.    }.  .   
2042e 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65   /* If there are
2042f 20 6e 6f 20 61 67 67 72 65 67 61 74 65 20 66 75   no aggregate fu
20430 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 72  nctions in the r
20431 65 73 75 6c 74 2d 73 65 74 2c 20 61 6e 64 20 6e  esult-set, and n
20432 6f 20 47 52 4f 55 50 20 42 59 20 0a 20 20 20 20  o GROUP BY .    
20433 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 64  ** expression, d
20434 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 67 67 72  o not allow aggr
20435 65 67 61 74 65 73 20 69 6e 20 61 6e 79 20 6f 66  egates in any of
20436 20 74 68 65 20 6f 74 68 65 72 20 65 78 70 72 65   the other expre
20437 73 73 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20  ssions..    */. 
20438 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 73     assert( (p->s
20439 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 41 67 67  elFlags & SF_Agg
2043a 72 65 67 61 74 65 29 3d 3d 30 20 29 3b 0a 20 20  regate)==0 );.  
2043b 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d 3e    pGroupBy = p->
2043c 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 69 66  pGroupBy;.    if
2043d 28 20 70 47 72 6f 75 70 42 79 20 7c 7c 20 73 4e  ( pGroupBy || sN
2043e 43 2e 68 61 73 41 67 67 20 29 7b 0a 20 20 20 20  C.hasAgg ){.    
2043f 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d    p->selFlags |=
20440 20 53 46 5f 41 67 67 72 65 67 61 74 65 3b 0a 20   SF_Aggregate;. 
20441 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
20442 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20 30  sNC.allowAgg = 0
20443 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  ;.    }.  .    /
20444 2a 20 49 66 20 61 20 48 41 56 49 4e 47 20 63 6c  * If a HAVING cl
20445 61 75 73 65 20 69 73 20 70 72 65 73 65 6e 74 2c  ause is present,
20446 20 74 68 65 6e 20 74 68 65 72 65 20 6d 75 73 74   then there must
20447 20 62 65 20 61 20 47 52 4f 55 50 20 42 59 20 63   be a GROUP BY c
20448 6c 61 75 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  lause..    */.  
20449 20 20 69 66 28 20 70 2d 3e 70 48 61 76 69 6e 67    if( p->pHaving
2044a 20 26 26 20 21 70 47 72 6f 75 70 42 79 20 29 7b   && !pGroupBy ){
2044b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
2044c 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2044d 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  a GROUP BY claus
2044e 65 20 69 73 20 72 65 71 75 69 72 65 64 20 62 65  e is required be
2044f 66 6f 72 65 20 48 41 56 49 4e 47 22 29 3b 0a 20  fore HAVING");. 
20450 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
20451 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 0a  Abort;.    }.  .
20452 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 65      /* Add the e
20453 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 74  xpression list t
20454 6f 20 74 68 65 20 6e 61 6d 65 2d 63 6f 6e 74 65  o the name-conte
20455 78 74 20 62 65 66 6f 72 65 20 70 61 72 73 69 6e  xt before parsin
20456 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f 74 68  g the.    ** oth
20457 65 72 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69  er expressions i
20458 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  n the SELECT sta
20459 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20  tement. This is 
2045a 73 6f 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 65  so that.    ** e
2045b 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68  xpressions in th
2045c 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 28  e WHERE clause (
2045d 65 74 63 2e 29 20 63 61 6e 20 72 65 66 65 72 20  etc.) can refer 
2045e 74 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 20 62  to expressions b
2045f 79 0a 20 20 20 20 2a 2a 20 61 6c 69 61 73 65 73  y.    ** aliases
20460 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
20461 65 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  et..    **.    *
20462 2a 20 4d 69 6e 6f 72 20 70 6f 69 6e 74 3a 20 49  * Minor point: I
20463 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61  f this is the ca
20464 73 65 2c 20 74 68 65 6e 20 74 68 65 20 65 78 70  se, then the exp
20465 72 65 73 73 69 6f 6e 20 77 69 6c 6c 20 62 65 0a  ression will be.
20466 20 20 20 20 2a 2a 20 72 65 2d 65 76 61 6c 75 61      ** re-evalua
20467 74 65 64 20 66 6f 72 20 65 61 63 68 20 72 65 66  ted for each ref
20468 65 72 65 6e 63 65 20 74 6f 20 69 74 2e 0a 20 20  erence to it..  
20469 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 45 4c    */.    sNC.pEL
2046a 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b  ist = p->pEList;
2046b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
2046c 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
2046d 28 26 73 4e 43 2c 20 70 2d 3e 70 57 68 65 72 65  (&sNC, p->pWhere
2046e 29 20 7c 7c 0a 20 20 20 20 20 20 20 73 71 6c 69  ) ||.       sqli
2046f 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
20470 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 48 61  mes(&sNC, p->pHa
20471 76 69 6e 67 29 0a 20 20 20 20 29 7b 0a 20 20 20  ving).    ){.   
20472 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
20473 6f 72 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ort;.    }..    
20474 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20  /* The ORDER BY 
20475 61 6e 64 20 47 52 4f 55 50 20 42 59 20 63 6c 61  and GROUP BY cla
20476 75 73 65 73 20 6d 61 79 20 6e 6f 74 20 72 65 66  uses may not ref
20477 65 72 20 74 6f 20 74 65 72 6d 73 20 69 6e 0a 20  er to terms in. 
20478 20 20 20 2a 2a 20 6f 75 74 65 72 20 71 75 65 72     ** outer quer
20479 69 65 73 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ies .    */.    
2047a 73 4e 43 2e 70 4e 65 78 74 20 3d 20 30 3b 0a 20  sNC.pNext = 0;. 
2047b 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20     sNC.allowAgg 
2047c 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a 20 50 72 6f  = 1;..    /* Pro
2047d 63 65 73 73 20 74 68 65 20 4f 52 44 45 52 20 42  cess the ORDER B
2047e 59 20 63 6c 61 75 73 65 20 66 6f 72 20 73 69 6e  Y clause for sin
2047f 67 6c 65 74 6f 6e 20 53 45 4c 45 43 54 20 73 74  gleton SELECT st
20480 61 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a 2a  atements..    **
20481 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   The ORDER BY cl
20482 61 75 73 65 20 66 6f 72 20 63 6f 6d 70 6f 75 6e  ause for compoun
20483 64 73 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  ds SELECT statem
20484 65 6e 74 73 20 69 73 20 68 61 6e 64 6c 65 64 0a  ents is handled.
20485 20 20 20 20 2a 2a 20 62 65 6c 6f 77 2c 20 61 66      ** below, af
20486 74 65 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 72  ter all of the r
20487 65 73 75 6c 74 2d 73 65 74 73 20 66 6f 72 20 61  esult-sets for a
20488 6c 6c 20 6f 66 20 74 68 65 20 65 6c 65 6d 65 6e  ll of the elemen
20489 74 73 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65  ts of.    ** the
2048a 20 63 6f 6d 70 6f 75 6e 64 20 68 61 76 65 20 62   compound have b
2048b 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 0a 20 20  een resolved..  
2048c 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 69 73    */.    if( !is
2048d 43 6f 6d 70 6f 75 6e 64 20 26 26 20 72 65 73 6f  Compound && reso
2048e 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28  lveOrderGroupBy(
2048f 26 73 4e 43 2c 20 70 2c 20 70 2d 3e 70 4f 72 64  &sNC, p, p->pOrd
20490 65 72 42 79 2c 20 22 4f 52 44 45 52 22 29 20 29  erBy, "ORDER") )
20491 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 57  {.      return W
20492 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a  RC_Abort;.    }.
20493 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c      if( db->mall
20494 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
20495 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f    return WRC_Abo
20496 72 74 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20  rt;.    }.  .   
20497 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20   /* Resolve the 
20498 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 2e  GROUP BY clause.
20499 20 20 41 74 20 74 68 65 20 73 61 6d 65 20 74 69    At the same ti
2049a 6d 65 2c 20 6d 61 6b 65 20 73 75 72 65 20 0a 20  me, make sure . 
2049b 20 20 20 2a 2a 20 74 68 65 20 47 52 4f 55 50 20     ** the GROUP 
2049c 42 59 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e  BY clause does n
2049d 6f 74 20 63 6f 6e 74 61 69 6e 20 61 67 67 72 65  ot contain aggre
2049e 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a  gate functions..
2049f 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
204a0 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 20  GroupBy ){.     
204a1 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
204a2 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20  _item *pItem;.  
204a3 20 20 0a 20 20 20 20 20 20 69 66 28 20 72 65 73    .      if( res
204a4 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79  olveOrderGroupBy
204a5 28 26 73 4e 43 2c 20 70 2c 20 70 47 72 6f 75 70  (&sNC, p, pGroup
204a6 42 79 2c 20 22 47 52 4f 55 50 22 29 20 7c 7c 20  By, "GROUP") || 
204a7 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
204a8 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
204a9 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
204aa 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28      }.      for(
204ab 69 3d 30 2c 20 70 49 74 65 6d 3d 70 47 72 6f 75  i=0, pItem=pGrou
204ac 70 42 79 2d 3e 61 3b 20 69 3c 70 47 72 6f 75 70  pBy->a; i<pGroup
204ad 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20  By->nExpr; i++, 
204ae 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
204af 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f    if( ExprHasPro
204b0 70 65 72 74 79 28 70 49 74 65 6d 2d 3e 70 45 78  perty(pItem->pEx
204b1 70 72 2c 20 45 50 5f 41 67 67 29 20 29 7b 0a 20  pr, EP_Agg) ){. 
204b2 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
204b3 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
204b4 20 22 61 67 67 72 65 67 61 74 65 20 66 75 6e 63   "aggregate func
204b5 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61 6c  tions are not al
204b6 6c 6f 77 65 64 20 69 6e 20 22 0a 20 20 20 20 20  lowed in ".     
204b7 20 20 20 20 20 20 20 20 20 22 74 68 65 20 47 52           "the GR
204b8 4f 55 50 20 42 59 20 63 6c 61 75 73 65 22 29 3b  OUP BY clause");
204b9 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
204ba 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20  n WRC_Abort;.   
204bb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
204bc 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 76     }..    /* Adv
204bd 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78 74  ance to the next
204be 20 74 65 72 6d 20 6f 66 20 74 68 65 20 63 6f 6d   term of the com
204bf 70 6f 75 6e 64 0a 20 20 20 20 2a 2f 0a 20 20 20  pound.    */.   
204c0 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a   p = p->pPrior;.
204c1 20 20 20 20 6e 43 6f 6d 70 6f 75 6e 64 2b 2b 3b      nCompound++;
204c2 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c  .  }..  /* Resol
204c3 76 65 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  ve the ORDER BY 
204c4 6f 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45  on a compound SE
204c5 4c 45 43 54 20 61 66 74 65 72 20 61 6c 6c 20 74  LECT after all t
204c6 65 72 6d 73 20 6f 66 0a 20 20 2a 2a 20 74 68 65  erms of.  ** the
204c7 20 63 6f 6d 70 6f 75 6e 64 20 68 61 76 65 20 62   compound have b
204c8 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 0a 20 20  een resolved..  
204c9 2a 2f 0a 20 20 69 66 28 20 69 73 43 6f 6d 70 6f  */.  if( isCompo
204ca 75 6e 64 20 26 26 20 72 65 73 6f 6c 76 65 43 6f  und && resolveCo
204cb 6d 70 6f 75 6e 64 4f 72 64 65 72 42 79 28 70 50  mpoundOrderBy(pP
204cc 61 72 73 65 2c 20 70 4c 65 66 74 6d 6f 73 74 29  arse, pLeftmost)
204cd 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57   ){.    return W
204ce 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20  RC_Abort;.  }.. 
204cf 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e   return WRC_Prun
204d0 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  e;.}../*.** This
204d1 20 72 6f 75 74 69 6e 65 20 77 61 6c 6b 73 20 61   routine walks a
204d2 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  n expression tre
204d3 65 20 61 6e 64 20 72 65 73 6f 6c 76 65 73 20 72  e and resolves r
204d4 65 66 65 72 65 6e 63 65 73 20 74 6f 0a 2a 2a 20  eferences to.** 
204d5 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 61 6e  table columns an
204d6 64 20 72 65 73 75 6c 74 2d 73 65 74 20 63 6f 6c  d result-set col
204d7 75 6d 6e 73 2e 20 20 41 74 20 74 68 65 20 73 61  umns.  At the sa
204d8 6d 65 20 74 69 6d 65 2c 20 64 6f 20 65 72 72 6f  me time, do erro
204d9 72 0a 2a 2a 20 63 68 65 63 6b 69 6e 67 20 6f 6e  r.** checking on
204da 20 66 75 6e 63 74 69 6f 6e 20 75 73 61 67 65 20   function usage 
204db 61 6e 64 20 73 65 74 20 61 20 66 6c 61 67 20 69  and set a flag i
204dc 66 20 61 6e 79 20 61 67 67 72 65 67 61 74 65 20  f any aggregate 
204dd 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 61 72 65  functions.** are
204de 20 73 65 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20   seen..**.** To 
204df 72 65 73 6f 6c 76 65 20 74 61 62 6c 65 20 63 6f  resolve table co
204e0 6c 75 6d 6e 73 20 72 65 66 65 72 65 6e 63 65 73  lumns references
204e1 20 77 65 20 6c 6f 6f 6b 20 66 6f 72 20 6e 6f 64   we look for nod
204e2 65 73 20 28 6f 72 20 73 75 62 74 72 65 65 73 29  es (or subtrees)
204e3 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66 6f 72 6d   of the .** form
204e4 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 6f 72   X.Y.Z or Y.Z or
204e5 20 6a 75 73 74 20 5a 20 77 68 65 72 65 0a 2a 2a   just Z where.**
204e6 0a 2a 2a 20 20 20 20 20 20 58 3a 20 20 20 54 68  .**      X:   Th
204e7 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74 61  e name of a data
204e8 62 61 73 65 2e 20 20 45 78 3a 20 20 22 6d 61 69  base.  Ex:  "mai
204e9 6e 22 20 6f 72 20 22 74 65 6d 70 22 20 6f 72 0a  n" or "temp" or.
204ea 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 68 65  **           the
204eb 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 61   symbolic name a
204ec 73 73 69 67 6e 65 64 20 74 6f 20 61 6e 20 41 54  ssigned to an AT
204ed 54 41 43 48 2d 65 64 20 64 61 74 61 62 61 73 65  TACH-ed database
204ee 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 3a 20  ..**.**      Y: 
204ef 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20    The name of a 
204f0 74 61 62 6c 65 20 69 6e 20 61 20 46 52 4f 4d 20  table in a FROM 
204f1 63 6c 61 75 73 65 2e 20 20 4f 72 20 69 6e 20 61  clause.  Or in a
204f2 20 74 72 69 67 67 65 72 0a 2a 2a 20 20 20 20 20   trigger.**     
204f3 20 20 20 20 20 20 6f 6e 65 20 6f 66 20 74 68 65        one of the
204f4 20 73 70 65 63 69 61 6c 20 6e 61 6d 65 73 20 22   special names "
204f5 6f 6c 64 22 20 6f 72 20 22 6e 65 77 22 2e 0a 2a  old" or "new"..*
204f6 2a 0a 2a 2a 20 20 20 20 20 20 5a 3a 20 20 20 54  *.**      Z:   T
204f7 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c  he name of a col
204f8 75 6d 6e 20 69 6e 20 74 61 62 6c 65 20 59 2e 0a  umn in table Y..
204f9 2a 2a 0a 2a 2a 20 54 68 65 20 6e 6f 64 65 20 61  **.** The node a
204fa 74 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74 68  t the root of th
204fb 65 20 73 75 62 74 72 65 65 20 69 73 20 6d 6f 64  e subtree is mod
204fc 69 66 69 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73  ified as follows
204fd 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 45 78 70 72 2e  :.**.**    Expr.
204fe 6f 70 20 20 20 20 20 20 20 20 43 68 61 6e 67 65  op        Change
204ff 64 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 0a 2a  d to TK_COLUMN.*
20500 2a 20 20 20 20 45 78 70 72 2e 70 54 61 62 20 20  *    Expr.pTab  
20501 20 20 20 20 50 6f 69 6e 74 73 20 74 6f 20 74 68      Points to th
20502 65 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 20 66  e Table object f
20503 6f 72 20 58 2e 59 0a 2a 2a 20 20 20 20 45 78 70  or X.Y.**    Exp
20504 72 2e 69 43 6f 6c 75 6d 6e 20 20 20 54 68 65 20  r.iColumn   The 
20505 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 69 6e 20  column index in 
20506 58 2e 59 2e 20 20 2d 31 20 66 6f 72 20 74 68 65  X.Y.  -1 for the
20507 20 72 6f 77 69 64 2e 0a 2a 2a 20 20 20 20 45 78   rowid..**    Ex
20508 70 72 2e 69 54 61 62 6c 65 20 20 20 20 54 68 65  pr.iTable    The
20509 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d   VDBE cursor num
2050a 62 65 72 20 66 6f 72 20 58 2e 59 0a 2a 2a 0a 2a  ber for X.Y.**.*
2050b 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c 76 65 20  *.** To resolve 
2050c 72 65 73 75 6c 74 2d 73 65 74 20 72 65 66 65 72  result-set refer
2050d 65 6e 63 65 73 2c 20 6c 6f 6f 6b 20 66 6f 72 20  ences, look for 
2050e 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 73  expression nodes
2050f 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 72 6d 20   of the.** form 
20510 5a 20 28 77 69 74 68 20 6e 6f 20 58 20 61 6e 64  Z (with no X and
20511 20 59 20 70 72 65 66 69 78 29 20 77 68 65 72 65   Y prefix) where
20512 20 74 68 65 20 5a 20 6d 61 74 63 68 65 73 20 74   the Z matches t
20513 68 65 20 72 69 67 68 74 2d 68 61 6e 64 0a 2a 2a  he right-hand.**
20514 20 73 69 7a 65 20 6f 66 20 61 6e 20 41 53 20 63   size of an AS c
20515 6c 61 75 73 65 20 69 6e 20 74 68 65 20 72 65 73  lause in the res
20516 75 6c 74 2d 73 65 74 20 6f 66 20 61 20 53 45 4c  ult-set of a SEL
20517 45 43 54 2e 20 20 54 68 65 20 5a 20 65 78 70 72  ECT.  The Z expr
20518 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 72 65 70  ession.** is rep
20519 6c 61 63 65 64 20 62 79 20 61 20 63 6f 70 79 20  laced by a copy 
2051a 6f 66 20 74 68 65 20 6c 65 66 74 2d 68 61 6e 64  of the left-hand
2051b 20 73 69 64 65 20 6f 66 20 74 68 65 20 72 65 73   side of the res
2051c 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69  ult-set expressi
2051d 6f 6e 2e 0a 2a 2a 20 54 61 62 6c 65 2d 6e 61 6d  on..** Table-nam
2051e 65 20 61 6e 64 20 66 75 6e 63 74 69 6f 6e 20 72  e and function r
2051f 65 73 6f 6c 75 74 69 6f 6e 20 6f 63 63 75 72 73  esolution occurs
20520 20 6f 6e 20 74 68 65 20 73 75 62 73 74 69 74 75   on the substitu
20521 74 65 64 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a  ted expression.*
20522 2a 20 74 72 65 65 2e 20 20 46 6f 72 20 65 78 61  * tree.  For exa
20523 6d 70 6c 65 2c 20 69 6e 3a 0a 2a 2a 0a 2a 2a 20  mple, in:.**.** 
20524 20 20 20 20 20 53 45 4c 45 43 54 20 61 2b 62 20       SELECT a+b 
20525 41 53 20 78 2c 20 63 2b 64 20 41 53 20 79 20 46  AS x, c+d AS y F
20526 52 4f 4d 20 74 31 20 4f 52 44 45 52 20 42 59 20  ROM t1 ORDER BY 
20527 78 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 78 22  x;.**.** The "x"
20528 20 74 65 72 6d 20 6f 66 20 74 68 65 20 6f 72 64   term of the ord
20529 65 72 20 62 79 20 69 73 20 72 65 70 6c 61 63 65  er by is replace
2052a 64 20 62 79 20 22 61 2b 62 22 20 74 6f 20 72 65  d by "a+b" to re
2052b 6e 64 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  nder:.**.**     
2052c 20 53 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78   SELECT a+b AS x
2052d 2c 20 63 2b 64 20 41 53 20 79 20 46 52 4f 4d 20  , c+d AS y FROM 
2052e 74 31 20 4f 52 44 45 52 20 42 59 20 61 2b 62 3b  t1 ORDER BY a+b;
2052f 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20  .**.** Function 
20530 63 61 6c 6c 73 20 61 72 65 20 63 68 65 63 6b 65  calls are checke
20531 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  d to make sure t
20532 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  hat the function
20533 20 69 73 20 0a 2a 2a 20 64 65 66 69 6e 65 64 20   is .** defined 
20534 61 6e 64 20 74 68 61 74 20 74 68 65 20 63 6f 72  and that the cor
20535 72 65 63 74 20 6e 75 6d 62 65 72 20 6f 66 20 61  rect number of a
20536 72 67 75 6d 65 6e 74 73 20 61 72 65 20 73 70 65  rguments are spe
20537 63 69 66 69 65 64 2e 0a 2a 2a 20 49 66 20 74 68  cified..** If th
20538 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 6e  e function is an
20539 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
2053a 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 70 4e  ion, then the pN
2053b 43 2d 3e 68 61 73 41 67 67 20 69 73 0a 2a 2a 20  C->hasAgg is.** 
2053c 73 65 74 20 61 6e 64 20 74 68 65 20 6f 70 63 6f  set and the opco
2053d 64 65 20 69 73 20 63 68 61 6e 67 65 64 20 66 72  de is changed fr
2053e 6f 6d 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 74  om TK_FUNCTION t
2053f 6f 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  o TK_AGG_FUNCTIO
20540 4e 2e 0a 2a 2a 20 49 66 20 61 6e 20 65 78 70 72  N..** If an expr
20541 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73 20  ession contains 
20542 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
20543 6f 6e 73 20 74 68 65 6e 20 74 68 65 20 45 50 5f  ons then the EP_
20544 41 67 67 0a 2a 2a 20 70 72 6f 70 65 72 74 79 20  Agg.** property 
20545 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  on the expressio
20546 6e 20 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20  n is set..**.** 
20547 41 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  An error message
20548 20 69 73 20 6c 65 66 74 20 69 6e 20 70 50 61 72   is left in pPar
20549 73 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20 69  se if anything i
2054a 73 20 61 6d 69 73 73 2e 20 20 54 68 65 20 6e 75  s amiss.  The nu
2054b 6d 62 65 72 0a 2a 2a 20 69 66 20 65 72 72 6f 72  mber.** if error
2054c 73 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  s is returned..*
2054d 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2054e 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73 6f   int sqlite3Reso
2054f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 20 0a 20  lveExprNames( . 
20550 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e   NameContext *pN
20551 43 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  C,       /* Name
20552 73 70 61 63 65 20 74 6f 20 72 65 73 6f 6c 76 65  space to resolve
20553 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 2e   expressions in.
20554 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70   */.  Expr *pExp
20555 72 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  r             /*
20556 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
20557 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e 20  to be analyzed. 
20558 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 73 61 76 65  */.){.  int save
20559 64 48 61 73 41 67 67 3b 0a 20 20 57 61 6c 6b 65  dHasAgg;.  Walke
2055a 72 20 77 3b 0a 0a 20 20 69 66 28 20 70 45 78 70  r w;..  if( pExp
2055b 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  r==0 ) return 0;
2055c 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
2055d 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 7b  EXPR_DEPTH>0.  {
2055e 0a 20 20 20 20 50 61 72 73 65 20 2a 70 50 61 72  .    Parse *pPar
2055f 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65  se = pNC->pParse
20560 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
20561 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74  3ExprCheckHeight
20562 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
20563 6e 48 65 69 67 68 74 2b 70 4e 43 2d 3e 70 50 61  nHeight+pNC->pPa
20564 72 73 65 2d 3e 6e 48 65 69 67 68 74 29 20 29 7b  rse->nHeight) ){
20565 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
20566 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73  .    }.    pPars
20567 65 2d 3e 6e 48 65 69 67 68 74 20 2b 3d 20 70 45  e->nHeight += pE
20568 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a 20 20  xpr->nHeight;.  
20569 7d 0a 23 65 6e 64 69 66 0a 20 20 73 61 76 65 64  }.#endif.  saved
2056a 48 61 73 41 67 67 20 3d 20 70 4e 43 2d 3e 68 61  HasAgg = pNC->ha
2056b 73 41 67 67 3b 0a 20 20 70 4e 43 2d 3e 68 61 73  sAgg;.  pNC->has
2056c 41 67 67 20 3d 20 30 3b 0a 20 20 77 2e 78 45 78  Agg = 0;.  w.xEx
2056d 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73  prCallback = res
2056e 6f 6c 76 65 45 78 70 72 53 74 65 70 3b 0a 20 20  olveExprStep;.  
2056f 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63  w.xSelectCallbac
20570 6b 20 3d 20 72 65 73 6f 6c 76 65 53 65 6c 65 63  k = resolveSelec
20571 74 53 74 65 70 3b 0a 20 20 77 2e 70 50 61 72 73  tStep;.  w.pPars
20572 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b  e = pNC->pParse;
20573 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4e 43  .  w.u.pNC = pNC
20574 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45  ;.  sqlite3WalkE
20575 78 70 72 28 26 77 2c 20 70 45 78 70 72 29 3b 0a  xpr(&w, pExpr);.
20576 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45  #if SQLITE_MAX_E
20577 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 70 4e  XPR_DEPTH>0.  pN
20578 43 2d 3e 70 50 61 72 73 65 2d 3e 6e 48 65 69 67  C->pParse->nHeig
20579 68 74 20 2d 3d 20 70 45 78 70 72 2d 3e 6e 48 65  ht -= pExpr->nHe
2057a 69 67 68 74 3b 0a 23 65 6e 64 69 66 0a 20 20 69  ight;.#endif.  i
2057b 66 28 20 70 4e 43 2d 3e 6e 45 72 72 3e 30 20 7c  f( pNC->nErr>0 |
2057c 7c 20 77 2e 70 50 61 72 73 65 2d 3e 6e 45 72 72  | w.pParse->nErr
2057d 3e 30 20 29 7b 0a 20 20 20 20 45 78 70 72 53 65  >0 ){.    ExprSe
2057e 74 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  tProperty(pExpr,
2057f 20 45 50 5f 45 72 72 6f 72 29 3b 0a 20 20 7d 0a   EP_Error);.  }.
20580 20 20 69 66 28 20 70 4e 43 2d 3e 68 61 73 41 67    if( pNC->hasAg
20581 67 20 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74  g ){.    ExprSet
20582 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
20583 45 50 5f 41 67 67 29 3b 0a 20 20 7d 65 6c 73 65  EP_Agg);.  }else
20584 20 69 66 28 20 73 61 76 65 64 48 61 73 41 67 67   if( savedHasAgg
20585 20 29 7b 0a 20 20 20 20 70 4e 43 2d 3e 68 61 73   ){.    pNC->has
20586 41 67 67 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72  Agg = 1;.  }.  r
20587 65 74 75 72 6e 20 45 78 70 72 48 61 73 50 72 6f  eturn ExprHasPro
20588 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
20589 45 72 72 6f 72 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  Error);.}.../*.*
2058a 2a 20 52 65 73 6f 6c 76 65 20 61 6c 6c 20 6e 61  * Resolve all na
2058b 6d 65 73 20 69 6e 20 61 6c 6c 20 65 78 70 72 65  mes in all expre
2058c 73 73 69 6f 6e 73 20 6f 66 20 61 20 53 45 4c 45  ssions of a SELE
2058d 43 54 20 61 6e 64 20 69 6e 20 61 6c 6c 0a 2a 2a  CT and in all.**
2058e 20 64 65 63 65 6e 64 65 6e 74 73 20 6f 66 20 74   decendents of t
2058f 68 65 20 53 45 4c 45 43 54 2c 20 69 6e 63 6c 75  he SELECT, inclu
20590 64 69 6e 67 20 63 6f 6d 70 6f 75 6e 64 73 20 6f  ding compounds o
20591 66 66 20 6f 66 20 70 2d 3e 70 50 72 69 6f 72 2c  ff of p->pPrior,
20592 0a 2a 2a 20 73 75 62 71 75 65 72 69 65 73 20 69  .** subqueries i
20593 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 20 61  n expressions, a
20594 6e 64 20 73 75 62 71 75 65 72 69 65 73 20 75 73  nd subqueries us
20595 65 64 20 61 73 20 46 52 4f 4d 20 63 6c 61 75 73  ed as FROM claus
20596 65 0a 2a 2a 20 74 65 72 6d 73 2e 0a 2a 2a 0a 2a  e.** terms..**.*
20597 2a 20 53 65 65 20 73 71 6c 69 74 65 33 52 65 73  * See sqlite3Res
20598 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20  olveExprNames() 
20599 66 6f 72 20 61 20 64 65 73 63 72 69 70 74 69 6f  for a descriptio
2059a 6e 20 6f 66 20 74 68 65 20 6b 69 6e 64 73 20 6f  n of the kinds o
2059b 66 0a 2a 2a 20 74 72 61 6e 73 66 6f 72 6d 61 74  f.** transformat
2059c 69 6f 6e 73 20 74 68 61 74 20 6f 63 63 75 72 2e  ions that occur.
2059d 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 53 45 4c 45 43  .**.** All SELEC
2059e 54 20 73 74 61 74 65 6d 65 6e 74 73 20 73 68 6f  T statements sho
2059f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 65 78  uld have been ex
205a0 70 61 6e 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20  panded using.** 
205a1 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70  sqlite3SelectExp
205a2 61 6e 64 28 29 20 70 72 69 6f 72 20 74 6f 20 69  and() prior to i
205a3 6e 76 6f 6b 69 6e 67 20 74 68 69 73 20 72 6f 75  nvoking this rou
205a4 74 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tine..*/.SQLITE_
205a5 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
205a6 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 63  ite3ResolveSelec
205a7 74 4e 61 6d 65 73 28 0a 20 20 50 61 72 73 65 20  tNames(.  Parse 
205a8 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
205a9 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
205aa 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65  ontext */.  Sele
205ab 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  ct *p,          
205ac 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54     /* The SELECT
205ad 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67   statement being
205ae 20 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 4e 61 6d   coded. */.  Nam
205af 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75 74 65 72  eContext *pOuter
205b0 4e 43 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74  NC  /* Name cont
205b1 65 78 74 20 66 6f 72 20 70 61 72 65 6e 74 20 53  ext for parent S
205b2 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
205b3 2a 2f 0a 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77  */.){.  Walker w
205b4 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d  ;..  assert( p!=
205b5 30 20 29 3b 0a 20 20 77 2e 78 45 78 70 72 43 61  0 );.  w.xExprCa
205b6 6c 6c 62 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65  llback = resolve
205b7 45 78 70 72 53 74 65 70 3b 0a 20 20 77 2e 78 53  ExprStep;.  w.xS
205b8 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20  electCallback = 
205b9 72 65 73 6f 6c 76 65 53 65 6c 65 63 74 53 74 65  resolveSelectSte
205ba 70 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20  p;.  w.pParse = 
205bb 70 50 61 72 73 65 3b 0a 20 20 77 2e 75 2e 70 4e  pParse;.  w.u.pN
205bc 43 20 3d 20 70 4f 75 74 65 72 4e 43 3b 0a 20 20  C = pOuterNC;.  
205bd 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
205be 74 28 26 77 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 2a  t(&w, p);.}../**
205bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
205c0 20 6f 66 20 72 65 73 6f 6c 76 65 2e 63 20 2a 2a   of resolve.c **
205c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
205c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
205c5 69 6e 20 66 69 6c 65 20 65 78 70 72 2e 63 20 2a  in file expr.c *
205c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205c7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
205c9 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
205ca 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
205cb 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
205cc 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
205cd 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
205ce 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
205cf 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
205d0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
205d1 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
205d2 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
205d3 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
205d4 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
205d5 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
205d6 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
205d7 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
205d8 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
205d9 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
205da 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
205db 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
205dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
205e0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
205e1 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75 74 69 6e   contains routin
205e2 65 73 20 75 73 65 64 20 66 6f 72 20 61 6e 61 6c  es used for anal
205e3 79 7a 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e  yzing expression
205e4 73 20 61 6e 64 0a 2a 2a 20 66 6f 72 20 67 65 6e  s and.** for gen
205e5 65 72 61 74 69 6e 67 20 56 44 42 45 20 63 6f 64  erating VDBE cod
205e6 65 20 74 68 61 74 20 65 76 61 6c 75 61 74 65 73  e that evaluates
205e7 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20   expressions in 
205e8 53 51 4c 69 74 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  SQLite..*/../*.*
205e9 2a 20 52 65 74 75 72 6e 20 74 68 65 20 27 61 66  * Return the 'af
205ea 66 69 6e 69 74 79 27 20 6f 66 20 74 68 65 20 65  finity' of the e
205eb 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20  xpression pExpr 
205ec 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 49 66  if any..**.** If
205ed 20 70 45 78 70 72 20 69 73 20 61 20 63 6f 6c 75   pExpr is a colu
205ee 6d 6e 2c 20 61 20 72 65 66 65 72 65 6e 63 65 20  mn, a reference 
205ef 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 76 69 61 20  to a column via 
205f0 61 6e 20 27 41 53 27 20 61 6c 69 61 73 2c 0a 2a  an 'AS' alias,.*
205f1 2a 20 6f 72 20 61 20 73 75 62 2d 73 65 6c 65 63  * or a sub-selec
205f2 74 20 77 69 74 68 20 61 20 63 6f 6c 75 6d 6e 20  t with a column 
205f3 61 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  as the return va
205f4 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a  lue, then the .*
205f5 2a 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74 68  * affinity of th
205f6 61 74 20 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74  at column is ret
205f7 75 72 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65  urned. Otherwise
205f8 2c 20 30 78 30 30 20 69 73 20 72 65 74 75 72 6e  , 0x00 is return
205f9 65 64 2c 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e  ed,.** indicatin
205fa 67 20 6e 6f 20 61 66 66 69 6e 69 74 79 20 66 6f  g no affinity fo
205fb 72 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  r the expression
205fc 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e 20 74 68 65  ..**.** i.e. the
205fd 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 65 78   WHERE clause ex
205fe 70 72 65 73 73 73 69 6f 6e 73 20 69 6e 20 74 68  presssions in th
205ff 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74  e following stat
20600 65 6d 65 6e 74 73 20 61 6c 6c 0a 2a 2a 20 68 61  ements all.** ha
20601 76 65 20 61 6e 20 61 66 66 69 6e 69 74 79 3a 0a  ve an affinity:.
20602 2a 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42  **.** CREATE TAB
20603 4c 45 20 74 31 28 61 29 3b 0a 2a 2a 20 53 45 4c  LE t1(a);.** SEL
20604 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48  ECT * FROM t1 WH
20605 45 52 45 20 61 3b 0a 2a 2a 20 53 45 4c 45 43 54  ERE a;.** SELECT
20606 20 61 20 41 53 20 62 20 46 52 4f 4d 20 74 31 20   a AS b FROM t1 
20607 57 48 45 52 45 20 62 3b 0a 2a 2a 20 53 45 4c 45  WHERE b;.** SELE
20608 43 54 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45  CT * FROM t1 WHE
20609 52 45 20 28 73 65 6c 65 63 74 20 61 20 66 72 6f  RE (select a fro
2060a 6d 20 74 31 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45  m t1);.*/.SQLITE
2060b 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71  _PRIVATE char sq
2060c 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74  lite3ExprAffinit
2060d 79 28 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a  y(Expr *pExpr){.
2060e 20 20 69 6e 74 20 6f 70 20 3d 20 70 45 78 70 72    int op = pExpr
2060f 2d 3e 6f 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d  ->op;.  if( op==
20610 54 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 20  TK_SELECT ){.   
20611 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
20612 66 6c 61 67 73 26 45 50 5f 78 49 73 53 65 6c 65  flags&EP_xIsSele
20613 63 74 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ct );.    return
20614 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69   sqlite3ExprAffi
20615 6e 69 74 79 28 70 45 78 70 72 2d 3e 78 2e 70 53  nity(pExpr->x.pS
20616 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d 3e 61  elect->pEList->a
20617 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 7d 0a  [0].pExpr);.  }.
20618 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
20619 4d 49 54 5f 43 41 53 54 0a 20 20 69 66 28 20 6f  MIT_CAST.  if( o
2061a 70 3d 3d 54 4b 5f 43 41 53 54 20 29 7b 0a 20 20  p==TK_CAST ){.  
2061b 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
2061c 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  asProperty(pExpr
2061d 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29  , EP_IntValue) )
2061e 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  ;.    return sql
2061f 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70 65  ite3AffinityType
20620 28 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e  (pExpr->u.zToken
20621 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
20622 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 41 47 47 5f  if( (op==TK_AGG_
20623 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b  COLUMN || op==TK
20624 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54  _COLUMN || op==T
20625 4b 5f 52 45 47 49 53 54 45 52 29 20 0a 20 20 20  K_REGISTER) .   
20626 26 26 20 70 45 78 70 72 2d 3e 70 54 61 62 21 3d  && pExpr->pTab!=
20627 30 0a 20 20 29 7b 0a 20 20 20 20 2f 2a 20 6f 70  0.  ){.    /* op
20628 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 26 26  ==TK_REGISTER &&
20629 20 70 45 78 70 72 2d 3e 70 54 61 62 21 3d 30 20   pExpr->pTab!=0 
2062a 68 61 70 70 65 6e 73 20 77 68 65 6e 20 70 45 78  happens when pEx
2062b 70 72 20 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c  pr was originall
2062c 79 0a 20 20 20 20 2a 2a 20 61 20 54 4b 5f 43 4f  y.    ** a TK_CO
2062d 4c 55 4d 4e 20 62 75 74 20 77 61 73 20 70 72 65  LUMN but was pre
2062e 76 69 6f 75 73 6c 79 20 65 76 61 6c 75 61 74 65  viously evaluate
2062f 64 20 61 6e 64 20 63 61 63 68 65 64 20 69 6e 20  d and cached in 
20630 61 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20  a register */.  
20631 20 20 69 6e 74 20 6a 20 3d 20 70 45 78 70 72 2d    int j = pExpr-
20632 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 69 66  >iColumn;.    if
20633 28 20 6a 3c 30 20 29 20 72 65 74 75 72 6e 20 53  ( j<0 ) return S
20634 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
20635 52 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  R;.    assert( p
20636 45 78 70 72 2d 3e 70 54 61 62 20 26 26 20 6a 3c  Expr->pTab && j<
20637 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e 6e 43 6f  pExpr->pTab->nCo
20638 6c 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  l );.    return 
20639 70 45 78 70 72 2d 3e 70 54 61 62 2d 3e 61 43 6f  pExpr->pTab->aCo
2063a 6c 5b 6a 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20  l[j].affinity;. 
2063b 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 45 78 70   }.  return pExp
2063c 72 2d 3e 61 66 66 69 6e 69 74 79 3b 0a 7d 0a 0a  r->affinity;.}..
2063d 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 6f  /*.** Set the co
2063e 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
2063f 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 20   for expression 
20640 70 45 78 70 72 20 74 6f 20 62 65 20 74 68 65 20  pExpr to be the 
20641 63 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71  collating.** seq
20642 75 65 6e 63 65 20 6e 61 6d 65 64 20 62 79 20 70  uence named by p
20643 54 6f 6b 65 6e 2e 20 20 20 52 65 74 75 72 6e 20  Token.   Return 
20644 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
20645 20 72 65 76 69 73 65 64 20 65 78 70 72 65 73 73   revised express
20646 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 63 6f 6c 6c  ion..** The coll
20647 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69  ating sequence i
20648 73 20 6d 61 72 6b 65 64 20 61 73 20 22 65 78 70  s marked as "exp
20649 6c 69 63 69 74 22 20 75 73 69 6e 67 20 74 68 65  licit" using the
2064a 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 0a 2a   EP_ExpCollate.*
2064b 2a 20 66 6c 61 67 2e 20 20 41 6e 20 65 78 70 6c  * flag.  An expl
2064c 69 63 69 74 20 63 6f 6c 6c 61 74 69 6e 67 20 73  icit collating s
2064d 65 71 75 65 6e 63 65 20 77 69 6c 6c 20 6f 76 65  equence will ove
2064e 72 72 69 64 65 20 69 6d 70 6c 69 63 69 74 0a 2a  rride implicit.*
2064f 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  * collating sequ
20650 65 6e 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ences..*/.SQLITE
20651 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73  _PRIVATE Expr *s
20652 71 6c 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c  qlite3ExprSetCol
20653 6c 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  l(Parse *pParse,
20654 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 54 6f   Expr *pExpr, To
20655 6b 65 6e 20 2a 70 43 6f 6c 6c 4e 61 6d 65 29 7b  ken *pCollName){
20656 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 20 3d  .  char *zColl =
20657 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   0;            /
20658 2a 20 44 65 71 75 6f 74 65 64 20 6e 61 6d 65 20  * Dequoted name 
20659 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  of collation seq
2065a 75 65 6e 63 65 20 2a 2f 0a 20 20 43 6f 6c 6c 53  uence */.  CollS
2065b 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 73 71 6c  eq *pColl;.  sql
2065c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2065d 65 2d 3e 64 62 3b 0a 20 20 7a 43 6f 6c 6c 20 3d  e->db;.  zColl =
2065e 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
2065f 54 6f 6b 65 6e 28 64 62 2c 20 70 43 6f 6c 6c 4e  Token(db, pCollN
20660 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 45 78 70  ame);.  if( pExp
20661 72 20 26 26 20 7a 43 6f 6c 6c 20 29 7b 0a 20 20  r && zColl ){.  
20662 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
20663 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70  3LocateCollSeq(p
20664 50 61 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0a 20  Parse, zColl);. 
20665 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a     if( pColl ){.
20666 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 43 6f        pExpr->pCo
20667 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20  ll = pColl;.    
20668 20 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c    pExpr->flags |
20669 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b  = EP_ExpCollate;
2066a 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
2066b 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2066c 43 6f 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20  Coll);.  return 
2066d 70 45 78 70 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  pExpr;.}../*.** 
2066e 52 65 74 75 72 6e 20 74 68 65 20 64 65 66 61 75  Return the defau
2066f 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  lt collation seq
20670 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 65 78  uence for the ex
20671 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20  pression pExpr. 
20672 49 66 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e  If.** there is n
20673 6f 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74  o default collat
20674 69 6f 6e 20 74 79 70 65 2c 20 72 65 74 75 72 6e  ion type, return
20675 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52   0..*/.SQLITE_PR
20676 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73  IVATE CollSeq *s
20677 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65  qlite3ExprCollSe
20678 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  q(Parse *pParse,
20679 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20   Expr *pExpr){. 
2067a 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20   CollSeq *pColl 
2067b 3d 20 30 3b 0a 20 20 45 78 70 72 20 2a 70 20 3d  = 0;.  Expr *p =
2067c 20 70 45 78 70 72 3b 0a 20 20 77 68 69 6c 65 28   pExpr;.  while(
2067d 20 41 4c 57 41 59 53 28 70 29 20 29 7b 0a 20 20   ALWAYS(p) ){.  
2067e 20 20 69 6e 74 20 6f 70 3b 0a 20 20 20 20 70 43    int op;.    pC
2067f 6f 6c 6c 20 3d 20 70 2d 3e 70 43 6f 6c 6c 3b 0a  oll = p->pColl;.
20680 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 20      if( pColl ) 
20681 62 72 65 61 6b 3b 0a 20 20 20 20 6f 70 20 3d 20  break;.    op = 
20682 70 2d 3e 6f 70 3b 0a 20 20 20 20 69 66 28 20 70  p->op;.    if( p
20683 2d 3e 70 54 61 62 21 3d 30 20 26 26 20 28 0a 20  ->pTab!=0 && (. 
20684 20 20 20 20 20 20 20 6f 70 3d 3d 54 4b 5f 41 47         op==TK_AG
20685 47 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d 3d  G_COLUMN || op==
20686 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d  TK_COLUMN || op=
20687 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 7c 7c 20  =TK_REGISTER || 
20688 6f 70 3d 3d 54 4b 5f 54 52 49 47 47 45 52 0a 20  op==TK_TRIGGER. 
20689 20 20 20 29 29 7b 0a 20 20 20 20 20 20 2f 2a 20     )){.      /* 
2068a 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20  op==TK_REGISTER 
2068b 26 26 20 70 2d 3e 70 54 61 62 21 3d 30 20 68 61  && p->pTab!=0 ha
2068c 70 70 65 6e 73 20 77 68 65 6e 20 70 45 78 70 72  ppens when pExpr
2068d 20 77 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 0a   was originally.
2068e 20 20 20 20 20 20 2a 2a 20 61 20 54 4b 5f 43 4f        ** a TK_CO
2068f 4c 55 4d 4e 20 62 75 74 20 77 61 73 20 70 72 65  LUMN but was pre
20690 76 69 6f 75 73 6c 79 20 65 76 61 6c 75 61 74 65  viously evaluate
20691 64 20 61 6e 64 20 63 61 63 68 65 64 20 69 6e 20  d and cached in 
20692 61 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20  a register */.  
20693 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
20694 7a 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 69 6e 74  zColl;.      int
20695 20 6a 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3b   j = p->iColumn;
20696 0a 20 20 20 20 20 20 69 66 28 20 6a 3e 3d 30 20  .      if( j>=0 
20697 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
20698 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
20699 3e 64 62 3b 0a 20 20 20 20 20 20 20 20 7a 43 6f  >db;.        zCo
2069a 6c 6c 20 3d 20 70 2d 3e 70 54 61 62 2d 3e 61 43  ll = p->pTab->aC
2069b 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20  ol[j].zColl;.   
2069c 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c       pColl = sql
2069d 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
2069e 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a 43 6f  db, ENC(db), zCo
2069f 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  ll, 0);.        
206a0 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 70  pExpr->pColl = p
206a1 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Coll;.      }.  
206a2 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
206a3 0a 20 20 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f  .    if( op!=TK_
206a4 43 41 53 54 20 26 26 20 6f 70 21 3d 54 4b 5f 55  CAST && op!=TK_U
206a5 50 4c 55 53 20 29 7b 0a 20 20 20 20 20 20 62 72  PLUS ){.      br
206a6 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  eak;.    }.    p
206a7 20 3d 20 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 7d   = p->pLeft;.  }
206a8 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 43 68  .  if( sqlite3Ch
206a9 65 63 6b 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  eckCollSeq(pPars
206aa 65 2c 20 70 43 6f 6c 6c 29 20 29 7b 20 0a 20 20  e, pColl) ){ .  
206ab 20 20 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 7d    pColl = 0;.  }
206ac 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b  .  return pColl;
206ad 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20  .}../*.** pExpr 
206ae 69 73 20 61 6e 20 6f 70 65 72 61 6e 64 20 6f 66  is an operand of
206af 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70   a comparison op
206b0 65 72 61 74 6f 72 2e 20 20 61 66 66 32 20 69 73  erator.  aff2 is
206b1 20 74 68 65 0a 2a 2a 20 74 79 70 65 20 61 66 66   the.** type aff
206b2 69 6e 69 74 79 20 6f 66 20 74 68 65 20 6f 74 68  inity of the oth
206b3 65 72 20 6f 70 65 72 61 6e 64 2e 20 20 54 68 69  er operand.  Thi
206b4 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
206b5 73 20 74 68 65 0a 2a 2a 20 74 79 70 65 20 61 66  s the.** type af
206b6 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75  finity that shou
206b7 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 20 74  ld be used for t
206b8 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70  he comparison op
206b9 65 72 61 74 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54  erator..*/.SQLIT
206ba 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73  E_PRIVATE char s
206bb 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66  qlite3CompareAff
206bc 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70  inity(Expr *pExp
206bd 72 2c 20 63 68 61 72 20 61 66 66 32 29 7b 0a 20  r, char aff2){. 
206be 20 63 68 61 72 20 61 66 66 31 20 3d 20 73 71 6c   char aff1 = sql
206bf 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79  ite3ExprAffinity
206c0 28 70 45 78 70 72 29 3b 0a 20 20 69 66 28 20 61  (pExpr);.  if( a
206c1 66 66 31 20 26 26 20 61 66 66 32 20 29 7b 0a 20  ff1 && aff2 ){. 
206c2 20 20 20 2f 2a 20 42 6f 74 68 20 73 69 64 65 73     /* Both sides
206c3 20 6f 66 20 74 68 65 20 63 6f 6d 70 61 72 69 73   of the comparis
206c4 6f 6e 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2e 20  on are columns. 
206c5 49 66 20 6f 6e 65 20 68 61 73 20 6e 75 6d 65 72  If one has numer
206c6 69 63 0a 20 20 20 20 2a 2a 20 61 66 66 69 6e 69  ic.    ** affini
206c7 74 79 2c 20 75 73 65 20 74 68 61 74 2e 20 4f 74  ty, use that. Ot
206c8 68 65 72 77 69 73 65 20 75 73 65 20 6e 6f 20 61  herwise use no a
206c9 66 66 69 6e 69 74 79 2e 0a 20 20 20 20 2a 2f 0a  ffinity..    */.
206ca 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49      if( sqlite3I
206cb 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79  sNumericAffinity
206cc 28 61 66 66 31 29 20 7c 7c 20 73 71 6c 69 74 65  (aff1) || sqlite
206cd 33 49 73 4e 75 6d 65 72 69 63 41 66 66 69 6e 69  3IsNumericAffini
206ce 74 79 28 61 66 66 32 29 20 29 7b 0a 20 20 20 20  ty(aff2) ){.    
206cf 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
206d0 41 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 20  AFF_NUMERIC;.   
206d1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65   }else{.      re
206d2 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f  turn SQLITE_AFF_
206d3 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  NONE;.    }.  }e
206d4 6c 73 65 20 69 66 28 20 21 61 66 66 31 20 26 26  lse if( !aff1 &&
206d5 20 21 61 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a   !aff2 ){.    /*
206d6 20 4e 65 69 74 68 65 72 20 73 69 64 65 20 6f 66   Neither side of
206d7 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
206d8 69 73 20 61 20 63 6f 6c 75 6d 6e 2e 20 20 43 6f  is a column.  Co
206d9 6d 70 61 72 65 20 74 68 65 0a 20 20 20 20 2a 2a  mpare the.    **
206da 20 72 65 73 75 6c 74 73 20 64 69 72 65 63 74 6c   results directl
206db 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 65  y..    */.    re
206dc 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f  turn SQLITE_AFF_
206dd 4e 4f 4e 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  NONE;.  }else{. 
206de 20 20 20 2f 2a 20 4f 6e 65 20 73 69 64 65 20 69     /* One side i
206df 73 20 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 20  s a column, the 
206e0 6f 74 68 65 72 20 69 73 20 6e 6f 74 2e 20 55 73  other is not. Us
206e1 65 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 61 66  e the columns af
206e2 66 69 6e 69 74 79 2e 20 2a 2f 0a 20 20 20 20 61  finity. */.    a
206e3 73 73 65 72 74 28 20 61 66 66 31 3d 3d 30 20 7c  ssert( aff1==0 |
206e4 7c 20 61 66 66 32 3d 3d 30 20 29 3b 0a 20 20 20  | aff2==0 );.   
206e5 20 72 65 74 75 72 6e 20 28 61 66 66 31 20 2b 20   return (aff1 + 
206e6 61 66 66 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  aff2);.  }.}../*
206e7 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61 20 63  .** pExpr is a c
206e8 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74  omparison operat
206e9 6f 72 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20  or.  Return the 
206ea 74 79 70 65 20 61 66 66 69 6e 69 74 79 20 74 68  type affinity th
206eb 61 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20  at should.** be 
206ec 61 70 70 6c 69 65 64 20 74 6f 20 62 6f 74 68 20  applied to both 
206ed 6f 70 65 72 61 6e 64 73 20 70 72 69 6f 72 20 74  operands prior t
206ee 6f 20 64 6f 69 6e 67 20 74 68 65 20 63 6f 6d 70  o doing the comp
206ef 61 72 69 73 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  arison..*/.stati
206f0 63 20 63 68 61 72 20 63 6f 6d 70 61 72 69 73 6f  c char compariso
206f1 6e 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a  nAffinity(Expr *
206f2 70 45 78 70 72 29 7b 0a 20 20 63 68 61 72 20 61  pExpr){.  char a
206f3 66 66 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45  ff;.  assert( pE
206f4 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 51 20 7c  xpr->op==TK_EQ |
206f5 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  | pExpr->op==TK_
206f6 49 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d  IN || pExpr->op=
206f7 3d 54 4b 5f 4c 54 20 7c 7c 0a 20 20 20 20 20 20  =TK_LT ||.      
206f8 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54      pExpr->op==T
206f9 4b 5f 47 54 20 7c 7c 20 70 45 78 70 72 2d 3e 6f  K_GT || pExpr->o
206fa 70 3d 3d 54 4b 5f 47 45 20 7c 7c 20 70 45 78 70  p==TK_GE || pExp
206fb 72 2d 3e 6f 70 3d 3d 54 4b 5f 4c 45 20 7c 7c 0a  r->op==TK_LE ||.
206fc 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
206fd 3e 6f 70 3d 3d 54 4b 5f 4e 45 20 7c 7c 20 70 45  >op==TK_NE || pE
206fe 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 20 7c  xpr->op==TK_IS |
206ff 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  | pExpr->op==TK_
20700 49 53 4e 4f 54 20 29 3b 0a 20 20 61 73 73 65 72  ISNOT );.  asser
20701 74 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20  t( pExpr->pLeft 
20702 29 3b 0a 20 20 61 66 66 20 3d 20 73 71 6c 69 74  );.  aff = sqlit
20703 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70  e3ExprAffinity(p
20704 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20  Expr->pLeft);.  
20705 69 66 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68  if( pExpr->pRigh
20706 74 20 29 7b 0a 20 20 20 20 61 66 66 20 3d 20 73  t ){.    aff = s
20707 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66  qlite3CompareAff
20708 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70 52 69  inity(pExpr->pRi
20709 67 68 74 2c 20 61 66 66 29 3b 0a 20 20 7d 65 6c  ght, aff);.  }el
2070a 73 65 20 69 66 28 20 45 78 70 72 48 61 73 50 72  se if( ExprHasPr
2070b 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
2070c 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20  _xIsSelect) ){. 
2070d 20 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33     aff = sqlite3
2070e 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28  CompareAffinity(
2070f 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74  pExpr->x.pSelect
20710 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70  ->pEList->a[0].p
20711 45 78 70 72 2c 20 61 66 66 29 3b 0a 20 20 7d 65  Expr, aff);.  }e
20712 6c 73 65 20 69 66 28 20 21 61 66 66 20 29 7b 0a  lse if( !aff ){.
20713 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45      aff = SQLITE
20714 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20  _AFF_NONE;.  }. 
20715 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a   return aff;.}..
20716 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61  /*.** pExpr is a
20717 20 63 6f 6d 70 61 72 69 73 6f 6e 20 65 78 70 72   comparison expr
20718 65 73 73 69 6f 6e 2c 20 65 67 2e 20 27 3d 27 2c  ession, eg. '=',
20719 20 27 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20 65 74   '<', IN(...) et
2071a 63 2e 0a 2a 2a 20 69 64 78 5f 61 66 66 69 6e 69  c..** idx_affini
2071b 74 79 20 69 73 20 74 68 65 20 61 66 66 69 6e 69  ty is the affini
2071c 74 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 65 64  ty of an indexed
2071d 20 63 6f 6c 75 6d 6e 2e 20 52 65 74 75 72 6e 20   column. Return 
2071e 74 72 75 65 0a 2a 2a 20 69 66 20 74 68 65 20 69  true.** if the i
2071f 6e 64 65 78 20 77 69 74 68 20 61 66 66 69 6e 69  ndex with affini
20720 74 79 20 69 64 78 5f 61 66 66 69 6e 69 74 79 20  ty idx_affinity 
20721 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f 20 69  may be used to i
20722 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68 65 20  mplement.** the 
20723 63 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 20 70 45  comparison in pE
20724 78 70 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  xpr..*/.SQLITE_P
20725 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
20726 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f  e3IndexAffinityO
20727 6b 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63  k(Expr *pExpr, c
20728 68 61 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79  har idx_affinity
20729 29 7b 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20  ){.  char aff = 
2072a 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69  comparisonAffini
2072b 74 79 28 70 45 78 70 72 29 3b 0a 20 20 73 77 69  ty(pExpr);.  swi
2072c 74 63 68 28 20 61 66 66 20 29 7b 0a 20 20 20 20  tch( aff ){.    
2072d 63 61 73 65 20 53 51 4c 49 54 45 5f 41 46 46 5f  case SQLITE_AFF_
2072e 4e 4f 4e 45 3a 0a 20 20 20 20 20 20 72 65 74 75  NONE:.      retu
2072f 72 6e 20 31 3b 0a 20 20 20 20 63 61 73 65 20 53  rn 1;.    case S
20730 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a  QLITE_AFF_TEXT:.
20731 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 64 78        return idx
20732 5f 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54  _affinity==SQLIT
20733 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20  E_AFF_TEXT;.    
20734 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 72  default:.      r
20735 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49 73 4e  eturn sqlite3IsN
20736 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 69  umericAffinity(i
20737 64 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a 20 20  dx_affinity);.  
20738 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  }.}../*.** Retur
20739 6e 20 74 68 65 20 50 35 20 76 61 6c 75 65 20 74  n the P5 value t
2073a 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73  hat should be us
2073b 65 64 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20  ed for a binary 
2073c 63 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 6f 70  comparison.** op
2073d 63 6f 64 65 20 28 4f 50 5f 45 71 2c 20 4f 50 5f  code (OP_Eq, OP_
2073e 47 65 20 65 74 63 2e 29 20 75 73 65 64 20 74 6f  Ge etc.) used to
2073f 20 63 6f 6d 70 61 72 65 20 70 45 78 70 72 31 20   compare pExpr1 
20740 61 6e 64 20 70 45 78 70 72 32 2e 0a 2a 2f 0a 73  and pExpr2..*/.s
20741 74 61 74 69 63 20 75 38 20 62 69 6e 61 72 79 43  tatic u8 binaryC
20742 6f 6d 70 61 72 65 50 35 28 45 78 70 72 20 2a 70  ompareP5(Expr *p
20743 45 78 70 72 31 2c 20 45 78 70 72 20 2a 70 45 78  Expr1, Expr *pEx
20744 70 72 32 2c 20 69 6e 74 20 6a 75 6d 70 49 66 4e  pr2, int jumpIfN
20745 75 6c 6c 29 7b 0a 20 20 75 38 20 61 66 66 20 3d  ull){.  u8 aff =
20746 20 28 63 68 61 72 29 73 71 6c 69 74 65 33 45 78   (char)sqlite3Ex
20747 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72  prAffinity(pExpr
20748 32 29 3b 0a 20 20 61 66 66 20 3d 20 28 75 38 29  2);.  aff = (u8)
20749 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66  sqlite3CompareAf
2074a 66 69 6e 69 74 79 28 70 45 78 70 72 31 2c 20 61  finity(pExpr1, a
2074b 66 66 29 20 7c 20 28 75 38 29 6a 75 6d 70 49 66  ff) | (u8)jumpIf
2074c 4e 75 6c 6c 3b 0a 20 20 72 65 74 75 72 6e 20 61  Null;.  return a
2074d 66 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ff;.}../*.** Ret
2074e 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
2074f 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   the collation s
20750 65 71 75 65 6e 63 65 20 74 68 61 74 20 73 68 6f  equence that sho
20751 75 6c 64 20 62 65 20 75 73 65 64 20 62 79 0a 2a  uld be used by.*
20752 2a 20 61 20 62 69 6e 61 72 79 20 63 6f 6d 70 61  * a binary compa
20753 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 20 63  rison operator c
20754 6f 6d 70 61 72 69 6e 67 20 70 4c 65 66 74 20 61  omparing pLeft a
20755 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2a 0a 2a 2a  nd pRight..**.**
20756 20 49 66 20 74 68 65 20 6c 65 66 74 20 68 61 6e   If the left han
20757 64 20 65 78 70 72 65 73 73 69 6f 6e 20 68 61 73  d expression has
20758 20 61 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71   a collating seq
20759 75 65 6e 63 65 20 74 79 70 65 2c 20 74 68 65 6e  uence type, then
2075a 20 69 74 20 69 73 0a 2a 2a 20 75 73 65 64 2e 20   it is.** used. 
2075b 4f 74 68 65 72 77 69 73 65 20 74 68 65 20 63 6f  Otherwise the co
2075c 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
2075d 20 66 6f 72 20 74 68 65 20 72 69 67 68 74 20 68   for the right h
2075e 61 6e 64 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a  and expression.*
2075f 2a 20 69 73 20 75 73 65 64 2c 20 6f 72 20 74 68  * is used, or th
20760 65 20 64 65 66 61 75 6c 74 20 28 42 49 4e 41 52  e default (BINAR
20761 59 29 20 69 66 20 6e 65 69 74 68 65 72 20 65 78  Y) if neither ex
20762 70 72 65 73 73 69 6f 6e 20 68 61 73 20 61 20 63  pression has a c
20763 6f 6c 6c 61 74 69 6e 67 0a 2a 2a 20 74 79 70 65  ollating.** type
20764 2e 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74  ..**.** Argument
20765 20 70 52 69 67 68 74 20 28 62 75 74 20 6e 6f 74   pRight (but not
20766 20 70 4c 65 66 74 29 20 6d 61 79 20 62 65 20 61   pLeft) may be a
20767 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 2e 20 49   null pointer. I
20768 6e 20 74 68 69 73 20 63 61 73 65 2c 0a 2a 2a 20  n this case,.** 
20769 69 74 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64  it is not consid
2076a 65 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ered..*/.SQLITE_
2076b 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20  PRIVATE CollSeq 
2076c 2a 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f  *sqlite3BinaryCo
2076d 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 0a 20 20  mpareCollSeq(.  
2076e 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a  Parse *pParse, .
2076f 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 0a    Expr *pLeft, .
20770 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 0a 29    Expr *pRight.)
20771 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  {.  CollSeq *pCo
20772 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c  ll;.  assert( pL
20773 65 66 74 20 29 3b 0a 20 20 69 66 28 20 70 4c 65  eft );.  if( pLe
20774 66 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45  ft->flags & EP_E
20775 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20  xpCollate ){.   
20776 20 61 73 73 65 72 74 28 20 70 4c 65 66 74 2d 3e   assert( pLeft->
20777 70 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f  pColl );.    pCo
20778 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e 70 43 6f 6c  ll = pLeft->pCol
20779 6c 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  l;.  }else if( p
2077a 52 69 67 68 74 20 26 26 20 70 52 69 67 68 74 2d  Right && pRight-
2077b 3e 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43  >flags & EP_ExpC
2077c 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20 20 61 73  ollate ){.    as
2077d 73 65 72 74 28 20 70 52 69 67 68 74 2d 3e 70 43  sert( pRight->pC
2077e 6f 6c 6c 20 29 3b 0a 20 20 20 20 70 43 6f 6c 6c  oll );.    pColl
2077f 20 3d 20 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c   = pRight->pColl
20780 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
20781 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78  Coll = sqlite3Ex
20782 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  prCollSeq(pParse
20783 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 66  , pLeft);.    if
20784 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  ( !pColl ){.    
20785 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
20786 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61  3ExprCollSeq(pPa
20787 72 73 65 2c 20 70 52 69 67 68 74 29 3b 0a 20 20  rse, pRight);.  
20788 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
20789 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   pColl;.}../*.**
2078a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 6f 70   Generate the op
2078b 65 72 61 6e 64 73 20 66 6f 72 20 61 20 63 6f 6d  erands for a com
2078c 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 69 6f  parison operatio
2078d 6e 2e 20 20 42 65 66 6f 72 65 0a 2a 2a 20 67 65  n.  Before.** ge
2078e 6e 65 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64  nerating the cod
2078f 65 20 66 6f 72 20 65 61 63 68 20 6f 70 65 72 61  e for each opera
20790 6e 64 2c 20 73 65 74 20 74 68 65 20 45 50 5f 41  nd, set the EP_A
20791 6e 79 41 66 66 0a 2a 2a 20 66 6c 61 67 20 6f 6e  nyAff.** flag on
20792 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
20793 73 6f 20 74 68 61 74 20 69 74 20 77 69 6c 6c 20  so that it will 
20794 62 65 20 61 62 6c 65 20 74 6f 20 75 73 65 64 20  be able to used 
20795 61 0a 2a 2a 20 63 61 63 68 65 64 20 63 6f 6c 75  a.** cached colu
20796 6d 6e 20 76 61 6c 75 65 20 74 68 61 74 20 68 61  mn value that ha
20797 73 20 70 72 65 76 69 6f 75 73 6c 79 20 75 6e 64  s previously und
20798 65 72 67 6f 6e 65 20 61 6e 0a 2a 2a 20 61 66 66  ergone an.** aff
20799 69 6e 69 74 79 20 63 68 61 6e 67 65 2e 0a 2a 2f  inity change..*/
2079a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64  .static void cod
2079b 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73  eCompareOperands
2079c 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2079d 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67  e,    /* Parsing
2079e 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72 61   and code genera
2079f 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  ting context */.
207a0 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20    Expr *pLeft,  
207a1 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20      /* The left 
207a2 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74  operand */.  int
207a3 20 2a 70 52 65 67 4c 65 66 74 2c 20 20 20 20 2f   *pRegLeft,    /
207a4 2a 20 52 65 67 69 73 74 65 72 20 77 68 65 72 65  * Register where
207a5 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 69 73   left operand is
207a6 20 73 74 6f 72 65 64 20 2a 2f 0a 20 20 69 6e 74   stored */.  int
207a7 20 2a 70 46 72 65 65 4c 65 66 74 2c 20 20 20 2f   *pFreeLeft,   /
207a8 2a 20 46 72 65 65 20 74 68 69 73 20 72 65 67 69  * Free this regi
207a9 73 74 65 72 20 77 68 65 6e 20 64 6f 6e 65 20 2a  ster when done *
207aa 2f 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74  /.  Expr *pRight
207ab 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67  ,     /* The rig
207ac 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20  ht operand */.  
207ad 69 6e 74 20 2a 70 52 65 67 52 69 67 68 74 2c 20  int *pRegRight, 
207ae 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 77 68    /* Register wh
207af 65 72 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e  ere right operan
207b0 64 20 69 73 20 73 74 6f 72 65 64 20 2a 2f 0a 20  d is stored */. 
207b1 20 69 6e 74 20 2a 70 46 72 65 65 52 69 67 68 74   int *pFreeRight
207b2 20 20 20 2f 2a 20 57 72 69 74 65 20 74 65 6d 70     /* Write temp
207b3 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 72 69   register for ri
207b4 67 68 74 20 6f 70 65 72 61 6e 64 20 74 68 65 72  ght operand ther
207b5 65 20 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28  e */.){.  while(
207b6 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 55   pLeft->op==TK_U
207b7 50 4c 55 53 20 29 20 70 4c 65 66 74 20 3d 20 70  PLUS ) pLeft = p
207b8 4c 65 66 74 2d 3e 70 4c 65 66 74 3b 0a 20 20 70  Left->pLeft;.  p
207b9 4c 65 66 74 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  Left->flags |= E
207ba 50 5f 41 6e 79 41 66 66 3b 0a 20 20 2a 70 52 65  P_AnyAff;.  *pRe
207bb 67 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45  gLeft = sqlite3E
207bc 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
207bd 73 65 2c 20 70 4c 65 66 74 2c 20 70 46 72 65 65  se, pLeft, pFree
207be 4c 65 66 74 29 3b 0a 20 20 77 68 69 6c 65 28 20  Left);.  while( 
207bf 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 55  pRight->op==TK_U
207c0 50 4c 55 53 20 29 20 70 52 69 67 68 74 20 3d 20  PLUS ) pRight = 
207c1 70 52 69 67 68 74 2d 3e 70 4c 65 66 74 3b 0a 20  pRight->pLeft;. 
207c2 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 7c   pRight->flags |
207c3 3d 20 45 50 5f 41 6e 79 41 66 66 3b 0a 20 20 2a  = EP_AnyAff;.  *
207c4 70 52 65 67 52 69 67 68 74 20 3d 20 73 71 6c 69  pRegRight = sqli
207c5 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
207c6 70 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20  pParse, pRight, 
207c7 70 46 72 65 65 52 69 67 68 74 29 3b 0a 7d 0a 0a  pFreeRight);.}..
207c8 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
207c9 6f 64 65 20 66 6f 72 20 61 20 63 6f 6d 70 61 72  ode for a compar
207ca 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e 0a 2a  ison operator..*
207cb 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64  /.static int cod
207cc 65 43 6f 6d 70 61 72 65 28 0a 20 20 50 61 72 73  eCompare(.  Pars
207cd 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a  e *pParse,    /*
207ce 20 54 68 65 20 70 61 72 73 69 6e 67 20 28 61 6e   The parsing (an
207cf 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e  d code generatin
207d0 67 29 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  g) context */.  
207d1 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20  Expr *pLeft,    
207d2 20 20 2f 2a 20 54 68 65 20 6c 65 66 74 20 6f 70    /* The left op
207d3 65 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70 72 20  erand */.  Expr 
207d4 2a 70 52 69 67 68 74 2c 20 20 20 20 20 2f 2a 20  *pRight,     /* 
207d5 54 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e  The right operan
207d6 64 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 6f 64  d */.  int opcod
207d7 65 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  e,       /* The 
207d8 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 63 6f 64  comparison opcod
207d9 65 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 31 2c 20  e */.  int in1, 
207da 69 6e 74 20 69 6e 32 2c 20 2f 2a 20 52 65 67 69  int in2, /* Regi
207db 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 6f 70 65  ster holding ope
207dc 72 61 6e 64 73 20 2a 2f 0a 20 20 69 6e 74 20 64  rands */.  int d
207dd 65 73 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  est,         /* 
207de 4a 75 6d 70 20 68 65 72 65 20 69 66 20 74 72 75  Jump here if tru
207df 65 2e 20 20 2a 2f 0a 20 20 69 6e 74 20 6a 75 6d  e.  */.  int jum
207e0 70 49 66 4e 75 6c 6c 20 20 20 20 2f 2a 20 49 66  pIfNull    /* If
207e1 20 74 72 75 65 2c 20 6a 75 6d 70 20 69 66 20 65   true, jump if e
207e2 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69 73  ither operand is
207e3 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 69 6e   NULL */.){.  in
207e4 74 20 70 35 3b 0a 20 20 69 6e 74 20 61 64 64 72  t p5;.  int addr
207e5 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 34 3b  ;.  CollSeq *p4;
207e6 0a 0a 20 20 70 34 20 3d 20 73 71 6c 69 74 65 33  ..  p4 = sqlite3
207e7 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c  BinaryCompareCol
207e8 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65  lSeq(pParse, pLe
207e9 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 70  ft, pRight);.  p
207ea 35 20 3d 20 62 69 6e 61 72 79 43 6f 6d 70 61 72  5 = binaryCompar
207eb 65 50 35 28 70 4c 65 66 74 2c 20 70 52 69 67 68  eP5(pLeft, pRigh
207ec 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a  t, jumpIfNull);.
207ed 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
207ee 56 64 62 65 41 64 64 4f 70 34 28 70 50 61 72 73  VdbeAddOp4(pPars
207ef 65 2d 3e 70 56 64 62 65 2c 20 6f 70 63 6f 64 65  e->pVdbe, opcode
207f0 2c 20 69 6e 32 2c 20 64 65 73 74 2c 20 69 6e 31  , in2, dest, in1
207f1 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
207f2 20 20 20 20 20 20 20 20 20 20 20 20 20 28 76 6f               (vo
207f3 69 64 2a 29 70 34 2c 20 50 34 5f 43 4f 4c 4c 53  id*)p4, P4_COLLS
207f4 45 51 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  EQ);.  sqlite3Vd
207f5 62 65 43 68 61 6e 67 65 50 35 28 70 50 61 72 73  beChangeP5(pPars
207f6 65 2d 3e 70 56 64 62 65 2c 20 28 75 38 29 70 35  e->pVdbe, (u8)p5
207f7 29 3b 0a 20 20 69 66 28 20 28 70 35 20 26 20 53  );.  if( (p5 & S
207f8 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53 4b 29 21  QLITE_AFF_MASK)!
207f9 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45  =SQLITE_AFF_NONE
207fa 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
207fb 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79  xprCacheAffinity
207fc 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 69  Change(pParse, i
207fd 6e 31 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  n1, 1);.    sqli
207fe 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69  te3ExprCacheAffi
207ff 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73  nityChange(pPars
20800 65 2c 20 69 6e 32 2c 20 31 29 3b 0a 20 20 7d 0a  e, in2, 1);.  }.
20801 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b 0a 7d    return addr;.}
20802 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  ..#if SQLITE_MAX
20803 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 2f 2a  _EXPR_DEPTH>0./*
20804 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74 20 61  .** Check that a
20805 72 67 75 6d 65 6e 74 20 6e 48 65 69 67 68 74 20  rgument nHeight 
20806 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20  is less than or 
20807 65 71 75 61 6c 20 74 6f 20 74 68 65 20 6d 61 78  equal to the max
20808 69 6d 75 6d 0a 2a 2a 20 65 78 70 72 65 73 73 69  imum.** expressi
20809 6f 6e 20 64 65 70 74 68 20 61 6c 6c 6f 77 65 64  on depth allowed
2080a 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20  . If it is not, 
2080b 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 6d  leave an error m
2080c 65 73 73 61 67 65 20 69 6e 0a 2a 2a 20 70 50 61  essage in.** pPa
2080d 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  rse..*/.SQLITE_P
2080e 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2080f 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68  e3ExprCheckHeigh
20810 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  t(Parse *pParse,
20811 20 69 6e 74 20 6e 48 65 69 67 68 74 29 7b 0a 20   int nHeight){. 
20812 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
20813 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6d 78 48 65 69  _OK;.  int mxHei
20814 67 68 74 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  ght = pParse->db
20815 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
20816 4c 49 4d 49 54 5f 45 58 50 52 5f 44 45 50 54 48  LIMIT_EXPR_DEPTH
20817 5d 3b 0a 20 20 69 66 28 20 6e 48 65 69 67 68 74  ];.  if( nHeight
20818 3e 6d 78 48 65 69 67 68 74 20 29 7b 0a 20 20 20  >mxHeight ){.   
20819 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2081a 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20  (pParse, .      
2081b 20 22 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65   "Expression tre
2081c 65 20 69 73 20 74 6f 6f 20 6c 61 72 67 65 20 28  e is too large (
2081d 6d 61 78 69 6d 75 6d 20 64 65 70 74 68 20 25 64  maximum depth %d
2081e 29 22 2c 20 6d 78 48 65 69 67 68 74 0a 20 20 20  )", mxHeight.   
2081f 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   );.    rc = SQL
20820 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20  ITE_ERROR;.  }. 
20821 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
20822 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
20823 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 2c  three functions,
20824 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 29 2c   heightOfExpr(),
20825 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73   heightOfExprLis
20826 74 28 29 0a 2a 2a 20 61 6e 64 20 68 65 69 67 68  t().** and heigh
20827 74 4f 66 53 65 6c 65 63 74 28 29 2c 20 61 72 65  tOfSelect(), are
20828 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69   used to determi
20829 6e 65 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68  ne the maximum h
2082a 65 69 67 68 74 0a 2a 2a 20 6f 66 20 61 6e 79 20  eight.** of any 
2082b 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20  expression tree 
2082c 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 74 68  referenced by th
2082d 65 20 73 74 72 75 63 74 75 72 65 20 70 61 73 73  e structure pass
2082e 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 66 69 72  ed as the.** fir
2082f 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a  st argument..**.
20830 2a 2a 20 49 66 20 74 68 69 73 20 6d 61 78 69 6d  ** If this maxim
20831 75 6d 20 68 65 69 67 68 74 20 69 73 20 67 72 65  um height is gre
20832 61 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 75  ater than the cu
20833 72 72 65 6e 74 20 76 61 6c 75 65 20 70 6f 69 6e  rrent value poin
20834 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 70 6e 48  ted.** to by pnH
20835 65 69 67 68 74 2c 20 74 68 65 20 73 65 63 6f 6e  eight, the secon
20836 64 20 70 61 72 61 6d 65 74 65 72 2c 20 74 68 65  d parameter, the
20837 6e 20 73 65 74 20 2a 70 6e 48 65 69 67 68 74 20  n set *pnHeight 
20838 74 6f 20 74 68 61 74 0a 2a 2a 20 76 61 6c 75 65  to that.** value
20839 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2083a 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 45 78   heightOfExpr(Ex
2083b 70 72 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65  pr *p, int *pnHe
2083c 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29  ight){.  if( p )
2083d 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 6e 48 65  {.    if( p->nHe
2083e 69 67 68 74 3e 2a 70 6e 48 65 69 67 68 74 20 29  ight>*pnHeight )
2083f 7b 0a 20 20 20 20 20 20 2a 70 6e 48 65 69 67 68  {.      *pnHeigh
20840 74 20 3d 20 70 2d 3e 6e 48 65 69 67 68 74 3b 0a  t = p->nHeight;.
20841 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74      }.  }.}.stat
20842 69 63 20 76 6f 69 64 20 68 65 69 67 68 74 4f 66  ic void heightOf
20843 45 78 70 72 4c 69 73 74 28 45 78 70 72 4c 69 73  ExprList(ExprLis
20844 74 20 2a 70 2c 20 69 6e 74 20 2a 70 6e 48 65 69  t *p, int *pnHei
20845 67 68 74 29 7b 0a 20 20 69 66 28 20 70 20 29 7b  ght){.  if( p ){
20846 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
20847 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45  for(i=0; i<p->nE
20848 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  xpr; i++){.     
20849 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d   heightOfExpr(p-
2084a 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 70 6e 48  >a[i].pExpr, pnH
2084b 65 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20  eight);.    }.  
2084c 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  }.}.static void 
2084d 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 53  heightOfSelect(S
2084e 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 2a 70  elect *p, int *p
2084f 6e 48 65 69 67 68 74 29 7b 0a 20 20 69 66 28 20  nHeight){.  if( 
20850 70 20 29 7b 0a 20 20 20 20 68 65 69 67 68 74 4f  p ){.    heightO
20851 66 45 78 70 72 28 70 2d 3e 70 57 68 65 72 65 2c  fExpr(p->pWhere,
20852 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20   pnHeight);.    
20853 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e  heightOfExpr(p->
20854 70 48 61 76 69 6e 67 2c 20 70 6e 48 65 69 67 68  pHaving, pnHeigh
20855 74 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66  t);.    heightOf
20856 45 78 70 72 28 70 2d 3e 70 4c 69 6d 69 74 2c 20  Expr(p->pLimit, 
20857 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68  pnHeight);.    h
20858 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70  eightOfExpr(p->p
20859 4f 66 66 73 65 74 2c 20 70 6e 48 65 69 67 68 74  Offset, pnHeight
2085a 29 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45  );.    heightOfE
2085b 78 70 72 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73  xprList(p->pELis
2085c 74 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20  t, pnHeight);.  
2085d 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c 69    heightOfExprLi
2085e 73 74 28 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20  st(p->pGroupBy, 
2085f 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20 68  pnHeight);.    h
20860 65 69 67 68 74 4f 66 45 78 70 72 4c 69 73 74 28  eightOfExprList(
20861 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 70 6e 48  p->pOrderBy, pnH
20862 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67  eight);.    heig
20863 68 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e 70 50  htOfSelect(p->pP
20864 72 69 6f 72 2c 20 70 6e 48 65 69 67 68 74 29 3b  rior, pnHeight);
20865 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  .  }.}../*.** Se
20866 74 20 74 68 65 20 45 78 70 72 2e 6e 48 65 69 67  t the Expr.nHeig
20867 68 74 20 76 61 72 69 61 62 6c 65 20 69 6e 20 74  ht variable in t
20868 68 65 20 73 74 72 75 63 74 75 72 65 20 70 61 73  he structure pas
20869 73 65 64 20 61 73 20 61 6e 20 0a 2a 2a 20 61 72  sed as an .** ar
2086a 67 75 6d 65 6e 74 2e 20 41 6e 20 65 78 70 72 65  gument. An expre
2086b 73 73 69 6f 6e 20 77 69 74 68 20 6e 6f 20 63 68  ssion with no ch
2086c 69 6c 64 72 65 6e 2c 20 45 78 70 72 2e 70 4c 69  ildren, Expr.pLi
2086d 73 74 20 6f 72 20 0a 2a 2a 20 45 78 70 72 2e 70  st or .** Expr.p
2086e 53 65 6c 65 63 74 20 6d 65 6d 62 65 72 20 68 61  Select member ha
2086f 73 20 61 20 68 65 69 67 68 74 20 6f 66 20 31 2e  s a height of 1.
20870 20 41 6e 79 20 6f 74 68 65 72 20 65 78 70 72 65   Any other expre
20871 73 73 69 6f 6e 0a 2a 2a 20 68 61 73 20 61 20 68  ssion.** has a h
20872 65 69 67 68 74 20 65 71 75 61 6c 20 74 6f 20 74  eight equal to t
20873 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69 67 68  he maximum heigh
20874 74 20 6f 66 20 61 6e 79 20 6f 74 68 65 72 20 0a  t of any other .
20875 2a 2a 20 72 65 66 65 72 65 6e 63 65 64 20 45 78  ** referenced Ex
20876 70 72 20 70 6c 75 73 20 6f 6e 65 2e 0a 2a 2f 0a  pr plus one..*/.
20877 73 74 61 74 69 63 20 76 6f 69 64 20 65 78 70 72  static void expr
20878 53 65 74 48 65 69 67 68 74 28 45 78 70 72 20 2a  SetHeight(Expr *
20879 70 29 7b 0a 20 20 69 6e 74 20 6e 48 65 69 67 68  p){.  int nHeigh
2087a 74 20 3d 20 30 3b 0a 20 20 68 65 69 67 68 74 4f  t = 0;.  heightO
2087b 66 45 78 70 72 28 70 2d 3e 70 4c 65 66 74 2c 20  fExpr(p->pLeft, 
2087c 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 68 65 69  &nHeight);.  hei
2087d 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 52 69  ghtOfExpr(p->pRi
2087e 67 68 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a  ght, &nHeight);.
2087f 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72 6f    if( ExprHasPro
20880 70 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73 53  perty(p, EP_xIsS
20881 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 68 65  elect) ){.    he
20882 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e  ightOfSelect(p->
20883 78 2e 70 53 65 6c 65 63 74 2c 20 26 6e 48 65 69  x.pSelect, &nHei
20884 67 68 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ght);.  }else{. 
20885 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c     heightOfExprL
20886 69 73 74 28 70 2d 3e 78 2e 70 4c 69 73 74 2c 20  ist(p->x.pList, 
20887 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 20  &nHeight);.  }. 
20888 20 70 2d 3e 6e 48 65 69 67 68 74 20 3d 20 6e 48   p->nHeight = nH
20889 65 69 67 68 74 20 2b 20 31 3b 0a 7d 0a 0a 2f 2a  eight + 1;.}../*
2088a 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72  .** Set the Expr
2088b 2e 6e 48 65 69 67 68 74 20 76 61 72 69 61 62 6c  .nHeight variabl
2088c 65 20 75 73 69 6e 67 20 74 68 65 20 65 78 70 72  e using the expr
2088d 53 65 74 48 65 69 67 68 74 28 29 20 66 75 6e 63  SetHeight() func
2088e 74 69 6f 6e 2e 20 49 66 0a 2a 2a 20 74 68 65 20  tion. If.** the 
2088f 68 65 69 67 68 74 20 69 73 20 67 72 65 61 74 65  height is greate
20890 72 20 74 68 61 6e 20 74 68 65 20 6d 61 78 69 6d  r than the maxim
20891 75 6d 20 61 6c 6c 6f 77 65 64 20 65 78 70 72 65  um allowed expre
20892 73 73 69 6f 6e 20 64 65 70 74 68 2c 0a 2a 2a 20  ssion depth,.** 
20893 6c 65 61 76 65 20 61 6e 20 65 72 72 6f 72 20 69  leave an error i
20894 6e 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c  n pParse..*/.SQL
20895 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
20896 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48   sqlite3ExprSetH
20897 65 69 67 68 74 28 50 61 72 73 65 20 2a 70 50 61  eight(Parse *pPa
20898 72 73 65 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20  rse, Expr *p){. 
20899 20 65 78 70 72 53 65 74 48 65 69 67 68 74 28 70   exprSetHeight(p
2089a 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2089b 43 68 65 63 6b 48 65 69 67 68 74 28 70 50 61 72  CheckHeight(pPar
2089c 73 65 2c 20 70 2d 3e 6e 48 65 69 67 68 74 29 3b  se, p->nHeight);
2089d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
2089e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68 65 69   the maximum hei
2089f 67 68 74 20 6f 66 20 61 6e 79 20 65 78 70 72 65  ght of any expre
208a0 73 73 69 6f 6e 20 74 72 65 65 20 72 65 66 65 72  ssion tree refer
208a1 65 6e 63 65 64 0a 2a 2a 20 62 79 20 74 68 65 20  enced.** by the 
208a2 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65 6e 74  select statement
208a3 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 61 72   passed as an ar
208a4 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  gument..*/.SQLIT
208a5 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
208a6 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48  lite3SelectExprH
208a7 65 69 67 68 74 28 53 65 6c 65 63 74 20 2a 70 29  eight(Select *p)
208a8 7b 0a 20 20 69 6e 74 20 6e 48 65 69 67 68 74 20  {.  int nHeight 
208a9 3d 20 30 3b 0a 20 20 68 65 69 67 68 74 4f 66 53  = 0;.  heightOfS
208aa 65 6c 65 63 74 28 70 2c 20 26 6e 48 65 69 67 68  elect(p, &nHeigh
208ab 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 6e 48 65  t);.  return nHe
208ac 69 67 68 74 3b 0a 7d 0a 23 65 6c 73 65 0a 20 20  ight;.}.#else.  
208ad 23 64 65 66 69 6e 65 20 65 78 70 72 53 65 74 48  #define exprSetH
208ae 65 69 67 68 74 28 79 29 0a 23 65 6e 64 69 66 20  eight(y).#endif 
208af 2f 2a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58  /* SQLITE_MAX_EX
208b0 50 52 5f 44 45 50 54 48 3e 30 20 2a 2f 0a 0a 2f  PR_DEPTH>0 */../
208b1 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
208b2 65 20 69 73 20 74 68 65 20 63 6f 72 65 20 61 6c  e is the core al
208b3 6c 6f 63 61 74 6f 72 20 66 6f 72 20 45 78 70 72  locator for Expr
208b4 20 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f   nodes..**.** Co
208b5 6e 73 74 72 75 63 74 20 61 20 6e 65 77 20 65 78  nstruct a new ex
208b6 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 61 6e  pression node an
208b7 64 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  d return a point
208b8 65 72 20 74 6f 20 69 74 2e 20 20 4d 65 6d 6f 72  er to it.  Memor
208b9 79 0a 2a 2a 20 66 6f 72 20 74 68 69 73 20 6e 6f  y.** for this no
208ba 64 65 20 61 6e 64 20 66 6f 72 20 74 68 65 20 70  de and for the p
208bb 54 6f 6b 65 6e 20 61 72 67 75 6d 65 6e 74 20 69  Token argument i
208bc 73 20 61 20 73 69 6e 67 6c 65 20 61 6c 6c 6f 63  s a single alloc
208bd 61 74 69 6f 6e 0a 2a 2a 20 6f 62 74 61 69 6e 65  ation.** obtaine
208be 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 44 62  d from sqlite3Db
208bf 4d 61 6c 6c 6f 63 28 29 2e 20 20 54 68 65 20 63  Malloc().  The c
208c0 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 0a  alling function.
208c1 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  ** is responsibl
208c2 65 20 66 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72  e for making sur
208c3 65 20 74 68 65 20 6e 6f 64 65 20 65 76 65 6e 74  e the node event
208c4 75 61 6c 6c 79 20 67 65 74 73 20 66 72 65 65 64  ually gets freed
208c5 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 64 65 71 75 6f  ..**.** If dequo
208c6 74 65 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e  te is true, then
208c7 20 74 68 65 20 74 6f 6b 65 6e 20 28 69 66 20 69   the token (if i
208c8 74 20 65 78 69 73 74 73 29 20 69 73 20 64 65 71  t exists) is deq
208c9 75 6f 74 65 64 2e 0a 2a 2a 20 49 66 20 64 65 71  uoted..** If deq
208ca 75 6f 74 65 20 69 73 20 66 61 6c 73 65 2c 20 6e  uote is false, n
208cb 6f 20 64 65 71 75 6f 74 69 6e 67 20 69 73 20 70  o dequoting is p
208cc 65 72 66 6f 72 6d 61 6e 63 65 2e 20 20 54 68 65  erformance.  The
208cd 20 64 65 51 75 6f 74 65 0a 2a 2a 20 70 61 72 61   deQuote.** para
208ce 6d 65 74 65 72 20 69 73 20 69 67 6e 6f 72 65 64  meter is ignored
208cf 20 69 66 20 70 54 6f 6b 65 6e 20 69 73 20 4e 55   if pToken is NU
208d0 4c 4c 20 6f 72 20 69 66 20 74 68 65 20 74 6f 6b  LL or if the tok
208d1 65 6e 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 61  en does not.** a
208d2 70 70 65 61 72 20 74 6f 20 62 65 20 71 75 6f 74  ppear to be quot
208d3 65 64 2e 20 20 49 66 20 74 68 65 20 71 75 6f 74  ed.  If the quot
208d4 65 73 20 77 65 72 65 20 6f 66 20 74 68 65 20 66  es were of the f
208d5 6f 72 6d 20 22 2e 2e 2e 22 20 28 64 6f 75 62 6c  orm "..." (doubl
208d6 65 2d 71 75 6f 74 65 73 29 0a 2a 2a 20 74 68 65  e-quotes).** the
208d7 6e 20 74 68 65 20 45 50 5f 44 62 6c 51 75 6f 74  n the EP_DblQuot
208d8 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 6f  ed flag is set o
208d9 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  n the expression
208da 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 70 65   node..**.** Spe
208db 63 69 61 6c 20 63 61 73 65 3a 20 20 49 66 20 6f  cial case:  If o
208dc 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 61 6e  p==TK_INTEGER an
208dd 64 20 70 54 6f 6b 65 6e 20 70 6f 69 6e 74 73 20  d pToken points 
208de 74 6f 20 61 20 73 74 72 69 6e 67 20 74 68 61 74  to a string that
208df 0a 2a 2a 20 63 61 6e 20 62 65 20 74 72 61 6e 73  .** can be trans
208e0 6c 61 74 65 64 20 69 6e 74 6f 20 61 20 33 32 2d  lated into a 32-
208e1 62 69 74 20 69 6e 74 65 67 65 72 2c 20 74 68 65  bit integer, the
208e2 6e 20 74 68 65 20 74 6f 6b 65 6e 20 69 73 20 6e  n the token is n
208e3 6f 74 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20  ot.** stored in 
208e4 75 2e 7a 54 6f 6b 65 6e 2e 20 20 49 6e 73 74 65  u.zToken.  Inste
208e5 61 64 2c 20 74 68 65 20 69 6e 74 65 67 65 72 20  ad, the integer 
208e6 76 61 6c 75 65 73 20 69 73 20 77 72 69 74 74 65  values is writte
208e7 6e 0a 2a 2a 20 69 6e 74 6f 20 75 2e 69 56 61 6c  n.** into u.iVal
208e8 75 65 20 61 6e 64 20 74 68 65 20 45 50 5f 49 6e  ue and the EP_In
208e9 74 56 61 6c 75 65 20 66 6c 61 67 20 69 73 20 73  tValue flag is s
208ea 65 74 2e 20 20 4e 6f 20 65 78 74 72 61 20 73 74  et.  No extra st
208eb 6f 72 61 67 65 0a 2a 2a 20 69 73 20 61 6c 6c 6f  orage.** is allo
208ec 63 61 74 65 64 20 74 6f 20 68 6f 6c 64 20 74 68  cated to hold th
208ed 65 20 69 6e 74 65 67 65 72 20 74 65 78 74 20 61  e integer text a
208ee 6e 64 20 74 68 65 20 64 65 71 75 6f 74 65 20 66  nd the dequote f
208ef 6c 61 67 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a  lag is ignored..
208f0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
208f1 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45  E Expr *sqlite3E
208f2 78 70 72 41 6c 6c 6f 63 28 0a 20 20 73 71 6c 69  xprAlloc(.  sqli
208f3 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
208f4 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f      /* Handle fo
208f5 72 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  r sqlite3DbMallo
208f6 63 5a 65 72 6f 28 29 20 28 6d 61 79 20 62 65 20  cZero() (may be 
208f7 6e 75 6c 6c 29 20 2a 2f 0a 20 20 69 6e 74 20 6f  null) */.  int o
208f8 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
208f9 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e     /* Expression
208fa 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 63 6f 6e   opcode */.  con
208fb 73 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e  st Token *pToken
208fc 2c 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 72  ,    /* Token ar
208fd 67 75 6d 65 6e 74 2e 20 20 4d 69 67 68 74 20 62  gument.  Might b
208fe 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20  e NULL */.  int 
208ff 64 65 71 75 6f 74 65 20 20 20 20 20 20 20 20 20  dequote         
20900 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 64      /* True to d
20901 65 71 75 6f 74 65 20 2a 2f 0a 29 7b 0a 20 20 45  equote */.){.  E
20902 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74  xpr *pNew;.  int
20903 20 6e 45 78 74 72 61 20 3d 20 30 3b 0a 20 20 69   nExtra = 0;.  i
20904 6e 74 20 69 56 61 6c 75 65 20 3d 20 30 3b 0a 0a  nt iValue = 0;..
20905 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a    if( pToken ){.
20906 20 20 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f 49      if( op!=TK_I
20907 4e 54 45 47 45 52 20 7c 7c 20 70 54 6f 6b 65 6e  NTEGER || pToken
20908 2d 3e 7a 3d 3d 30 0a 20 20 20 20 20 20 20 20 20  ->z==0.         
20909 20 7c 7c 20 73 71 6c 69 74 65 33 47 65 74 49 6e   || sqlite3GetIn
2090a 74 33 32 28 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 26  t32(pToken->z, &
2090b 69 56 61 6c 75 65 29 3d 3d 30 20 29 7b 0a 20 20  iValue)==0 ){.  
2090c 20 20 20 20 6e 45 78 74 72 61 20 3d 20 70 54 6f      nExtra = pTo
2090d 6b 65 6e 2d 3e 6e 2b 31 3b 0a 20 20 20 20 7d 0a  ken->n+1;.    }.
2090e 20 20 7d 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c    }.  pNew = sql
2090f 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
20910 28 64 62 2c 20 73 69 7a 65 6f 66 28 45 78 70 72  (db, sizeof(Expr
20911 29 2b 6e 45 78 74 72 61 29 3b 0a 20 20 69 66 28  )+nExtra);.  if(
20912 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 70 4e 65   pNew ){.    pNe
20913 77 2d 3e 6f 70 20 3d 20 28 75 38 29 6f 70 3b 0a  w->op = (u8)op;.
20914 20 20 20 20 70 4e 65 77 2d 3e 69 41 67 67 20 3d      pNew->iAgg =
20915 20 2d 31 3b 0a 20 20 20 20 69 66 28 20 70 54 6f   -1;.    if( pTo
20916 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28  ken ){.      if(
20917 20 6e 45 78 74 72 61 3d 3d 30 20 29 7b 0a 20 20   nExtra==0 ){.  
20918 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67        pNew->flag
20919 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65  s |= EP_IntValue
2091a 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e  ;.        pNew->
2091b 75 2e 69 56 61 6c 75 65 20 3d 20 69 56 61 6c 75  u.iValue = iValu
2091c 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  e;.      }else{.
2091d 20 20 20 20 20 20 20 20 69 6e 74 20 63 3b 0a 20          int c;. 
2091e 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 7a         pNew->u.z
2091f 54 6f 6b 65 6e 20 3d 20 28 63 68 61 72 2a 29 26  Token = (char*)&
20920 70 4e 65 77 5b 31 5d 3b 0a 20 20 20 20 20 20 20  pNew[1];.       
20921 20 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e 75 2e   memcpy(pNew->u.
20922 7a 54 6f 6b 65 6e 2c 20 70 54 6f 6b 65 6e 2d 3e  zToken, pToken->
20923 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a 20  z, pToken->n);. 
20924 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e 7a         pNew->u.z
20925 54 6f 6b 65 6e 5b 70 54 6f 6b 65 6e 2d 3e 6e 5d  Token[pToken->n]
20926 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66   = 0;.        if
20927 28 20 64 65 71 75 6f 74 65 20 26 26 20 6e 45 78  ( dequote && nEx
20928 74 72 61 3e 3d 33 20 0a 20 20 20 20 20 20 20 20  tra>=3 .        
20929 20 20 20 20 20 26 26 20 28 28 63 20 3d 20 70 54       && ((c = pT
2092a 6f 6b 65 6e 2d 3e 7a 5b 30 5d 29 3d 3d 27 5c 27  oken->z[0])=='\'
2092b 27 20 7c 7c 20 63 3d 3d 27 22 27 20 7c 7c 20 63  ' || c=='"' || c
2092c 3d 3d 27 5b 27 20 7c 7c 20 63 3d 3d 27 60 27 29  =='[' || c=='`')
2092d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ){.          sq
2092e 6c 69 74 65 33 44 65 71 75 6f 74 65 28 70 4e 65  lite3Dequote(pNe
2092f 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20  w->u.zToken);.  
20930 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27          if( c=='
20931 22 27 20 29 20 70 4e 65 77 2d 3e 66 6c 61 67 73  "' ) pNew->flags
20932 20 7c 3d 20 45 50 5f 44 62 6c 51 75 6f 74 65 64   |= EP_DblQuoted
20933 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
20934 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 20 53 51    }.    }.#if SQ
20935 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45  LITE_MAX_EXPR_DE
20936 50 54 48 3e 30 0a 20 20 20 20 70 4e 65 77 2d 3e  PTH>0.    pNew->
20937 6e 48 65 69 67 68 74 20 3d 20 31 3b 0a 23 65 6e  nHeight = 1;.#en
20938 64 69 66 20 20 0a 20 20 7d 0a 20 20 72 65 74 75  dif  .  }.  retu
20939 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pNew;.}../*.*
2093a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  * Allocate a new
2093b 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65   expression node
2093c 20 66 72 6f 6d 20 61 20 7a 65 72 6f 2d 74 65 72   from a zero-ter
2093d 6d 69 6e 61 74 65 64 20 74 6f 6b 65 6e 20 74 68  minated token th
2093e 61 74 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64  at has.** alread
2093f 79 20 62 65 65 6e 20 64 65 71 75 6f 74 65 64 2e  y been dequoted.
20940 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20941 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
20942 45 78 70 72 28 0a 20 20 73 71 6c 69 74 65 33 20  Expr(.  sqlite3 
20943 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
20944 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 73 71  /* Handle for sq
20945 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
20946 6f 28 29 20 28 6d 61 79 20 62 65 20 6e 75 6c 6c  o() (may be null
20947 29 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 2c 20 20  ) */.  int op,  
20948 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
20949 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6f 70 63  * Expression opc
2094a 6f 64 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ode */.  const c
2094b 68 61 72 20 2a 7a 54 6f 6b 65 6e 20 20 20 20 20  har *zToken     
2094c 20 2f 2a 20 54 6f 6b 65 6e 20 61 72 67 75 6d 65   /* Token argume
2094d 6e 74 2e 20 20 4d 69 67 68 74 20 62 65 20 4e 55  nt.  Might be NU
2094e 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 54 6f 6b 65 6e  LL */.){.  Token
2094f 20 78 3b 0a 20 20 78 2e 7a 20 3d 20 7a 54 6f 6b   x;.  x.z = zTok
20950 65 6e 3b 0a 20 20 78 2e 6e 20 3d 20 7a 54 6f 6b  en;.  x.n = zTok
20951 65 6e 20 3f 20 73 71 6c 69 74 65 33 53 74 72 6c  en ? sqlite3Strl
20952 65 6e 33 30 28 7a 54 6f 6b 65 6e 29 20 3a 20 30  en30(zToken) : 0
20953 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ;.  return sqlit
20954 65 33 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c 20  e3ExprAlloc(db, 
20955 6f 70 2c 20 26 78 2c 20 30 29 3b 0a 7d 0a 0a 2f  op, &x, 0);.}../
20956 2a 0a 2a 2a 20 41 74 74 61 63 68 20 73 75 62 74  *.** Attach subt
20957 72 65 65 73 20 70 4c 65 66 74 20 61 6e 64 20 70  rees pLeft and p
20958 52 69 67 68 74 20 74 6f 20 74 68 65 20 45 78 70  Right to the Exp
20959 72 20 6e 6f 64 65 20 70 52 6f 6f 74 2e 0a 2a 2a  r node pRoot..**
2095a 0a 2a 2a 20 49 66 20 70 52 6f 6f 74 3d 3d 4e 55  .** If pRoot==NU
2095b 4c 4c 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  LL that means th
2095c 61 74 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  at a memory allo
2095d 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 68 61 73  cation error has
2095e 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 20 49 6e   occurred..** In
2095f 20 74 68 61 74 20 63 61 73 65 2c 20 64 65 6c 65   that case, dele
20960 74 65 20 74 68 65 20 73 75 62 74 72 65 65 73 20  te the subtrees 
20961 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74  pLeft and pRight
20962 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
20963 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
20964 45 78 70 72 41 74 74 61 63 68 53 75 62 74 72 65  ExprAttachSubtre
20965 65 73 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  es(.  sqlite3 *d
20966 62 2c 0a 20 20 45 78 70 72 20 2a 70 52 6f 6f 74  b,.  Expr *pRoot
20967 2c 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c  ,.  Expr *pLeft,
20968 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 0a  .  Expr *pRight.
20969 29 7b 0a 20 20 69 66 28 20 70 52 6f 6f 74 3d 3d  ){.  if( pRoot==
2096a 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
2096b 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2096c 64 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  d );.    sqlite3
2096d 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
2096e 4c 65 66 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  Left);.    sqlit
2096f 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
20970 20 70 52 69 67 68 74 29 3b 0a 20 20 7d 65 6c 73   pRight);.  }els
20971 65 7b 0a 20 20 20 20 69 66 28 20 70 52 69 67 68  e{.    if( pRigh
20972 74 20 29 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74  t ){.      pRoot
20973 2d 3e 70 52 69 67 68 74 20 3d 20 70 52 69 67 68  ->pRight = pRigh
20974 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 52 69  t;.      if( pRi
20975 67 68 74 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  ght->flags & EP_
20976 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20  ExpCollate ){.  
20977 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 66 6c 61        pRoot->fla
20978 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c  gs |= EP_ExpColl
20979 61 74 65 3b 0a 20 20 20 20 20 20 20 20 70 52 6f  ate;.        pRo
2097a 6f 74 2d 3e 70 43 6f 6c 6c 20 3d 20 70 52 69 67  ot->pColl = pRig
2097b 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20  ht->pColl;.     
2097c 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
2097d 20 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20   pLeft ){.      
2097e 70 52 6f 6f 74 2d 3e 70 4c 65 66 74 20 3d 20 70  pRoot->pLeft = p
2097f 4c 65 66 74 3b 0a 20 20 20 20 20 20 69 66 28 20  Left;.      if( 
20980 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 45  pLeft->flags & E
20981 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a  P_ExpCollate ){.
20982 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 66          pRoot->f
20983 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f  lags |= EP_ExpCo
20984 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20 20 20 70  llate;.        p
20985 52 6f 6f 74 2d 3e 70 43 6f 6c 6c 20 3d 20 70 4c  Root->pColl = pL
20986 65 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20  eft->pColl;.    
20987 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 65 78    }.    }.    ex
20988 70 72 53 65 74 48 65 69 67 68 74 28 70 52 6f 6f  prSetHeight(pRoo
20989 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  t);.  }.}../*.**
2098a 20 41 6c 6c 6f 63 61 74 65 20 61 20 45 78 70 72   Allocate a Expr
2098b 20 6e 6f 64 65 20 77 68 69 63 68 20 6a 6f 69 6e   node which join
2098c 73 20 61 73 20 6d 61 6e 79 20 61 73 20 74 77 6f  s as many as two
2098d 20 73 75 62 74 72 65 65 73 2e 0a 2a 2a 0a 2a 2a   subtrees..**.**
2098e 20 4f 6e 65 20 6f 72 20 62 6f 74 68 20 6f 66 20   One or both of 
2098f 74 68 65 20 73 75 62 74 72 65 65 73 20 63 61 6e  the subtrees can
20990 20 62 65 20 4e 55 4c 4c 2e 20 20 52 65 74 75 72   be NULL.  Retur
20991 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
20992 68 65 20 6e 65 77 0a 2a 2a 20 45 78 70 72 20 6e  he new.** Expr n
20993 6f 64 65 2e 20 20 4f 72 2c 20 69 66 20 61 6e 20  ode.  Or, if an 
20994 4f 4f 4d 20 65 72 72 6f 72 20 6f 63 63 75 72 73  OOM error occurs
20995 2c 20 73 65 74 20 70 50 61 72 73 65 2d 3e 64 62  , set pParse->db
20996 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 2c 0a  ->mallocFailed,.
20997 2a 2a 20 66 72 65 65 20 74 68 65 20 73 75 62 74  ** free the subt
20998 72 65 65 73 20 61 6e 64 20 72 65 74 75 72 6e 20  rees and return 
20999 4e 55 4c 4c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  NULL..*/.SQLITE_
2099a 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
2099b 6c 69 74 65 33 50 45 78 70 72 28 0a 20 20 50 61  lite3PExpr(.  Pa
2099c 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2099d 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
2099e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e   context */.  in
2099f 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20  t op,           
209a0 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73        /* Express
209a1 69 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20  ion opcode */.  
209a2 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20 20 20  Expr *pLeft,    
209a3 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66 74 20          /* Left 
209a4 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 45 78 70  operand */.  Exp
209a5 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20 20  r *pRight,      
209a6 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6f 70       /* Right op
209a7 65 72 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  erand */.  const
209a8 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 20 20   Token *pToken  
209a9 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74     /* Argument t
209aa 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70  oken */.){.  Exp
209ab 72 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 45 78  r *p = sqlite3Ex
209ac 70 72 41 6c 6c 6f 63 28 70 50 61 72 73 65 2d 3e  prAlloc(pParse->
209ad 64 62 2c 20 6f 70 2c 20 70 54 6f 6b 65 6e 2c 20  db, op, pToken, 
209ae 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  1);.  sqlite3Exp
209af 72 41 74 74 61 63 68 53 75 62 74 72 65 65 73 28  rAttachSubtrees(
209b0 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 2c 20 70  pParse->db, p, p
209b1 4c 65 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20  Left, pRight);. 
209b2 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
209b3 0a 2a 2a 20 4a 6f 69 6e 20 74 77 6f 20 65 78 70  .** Join two exp
209b4 72 65 73 73 69 6f 6e 73 20 75 73 69 6e 67 20 61  ressions using a
209b5 6e 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 20  n AND operator. 
209b6 20 49 66 20 65 69 74 68 65 72 20 65 78 70 72 65   If either expre
209b7 73 73 69 6f 6e 20 69 73 0a 2a 2a 20 4e 55 4c 4c  ssion is.** NULL
209b8 2c 20 74 68 65 6e 20 6a 75 73 74 20 72 65 74 75  , then just retu
209b9 72 6e 20 74 68 65 20 6f 74 68 65 72 20 65 78 70  rn the other exp
209ba 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  ression..*/.SQLI
209bb 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
209bc 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28  *sqlite3ExprAnd(
209bd 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70  sqlite3 *db, Exp
209be 72 20 2a 70 4c 65 66 74 2c 20 45 78 70 72 20 2a  r *pLeft, Expr *
209bf 70 52 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70  pRight){.  if( p
209c0 4c 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72  Left==0 ){.    r
209c1 65 74 75 72 6e 20 70 52 69 67 68 74 3b 0a 20 20  eturn pRight;.  
209c2 7d 65 6c 73 65 20 69 66 28 20 70 52 69 67 68 74  }else if( pRight
209c3 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
209c4 6e 20 70 4c 65 66 74 3b 0a 20 20 7d 65 6c 73 65  n pLeft;.  }else
209c5 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77  {.    Expr *pNew
209c6 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6c   = sqlite3ExprAl
209c7 6c 6f 63 28 64 62 2c 20 54 4b 5f 41 4e 44 2c 20  loc(db, TK_AND, 
209c8 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
209c9 65 33 45 78 70 72 41 74 74 61 63 68 53 75 62 74  e3ExprAttachSubt
209ca 72 65 65 73 28 64 62 2c 20 70 4e 65 77 2c 20 70  rees(db, pNew, p
209cb 4c 65 66 74 2c 20 70 52 69 67 68 74 29 3b 0a 20  Left, pRight);. 
209cc 20 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a     return pNew;.
209cd 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e    }.}../*.** Con
209ce 73 74 72 75 63 74 20 61 20 6e 65 77 20 65 78 70  struct a new exp
209cf 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72  ression node for
209d0 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68   a function with
209d1 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 61 72 67   multiple.** arg
209d2 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54  uments..*/.SQLIT
209d3 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
209d4 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74  sqlite3ExprFunct
209d5 69 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73  ion(Parse *pPars
209d6 65 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  e, ExprList *pLi
209d7 73 74 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65  st, Token *pToke
209d8 6e 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 77  n){.  Expr *pNew
209d9 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
209da 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
209db 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 20 29  assert( pToken )
209dc 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74  ;.  pNew = sqlit
209dd 65 33 45 78 70 72 41 6c 6c 6f 63 28 64 62 2c 20  e3ExprAlloc(db, 
209de 54 4b 5f 46 55 4e 43 54 49 4f 4e 2c 20 70 54 6f  TK_FUNCTION, pTo
209df 6b 65 6e 2c 20 31 29 3b 0a 20 20 69 66 28 20 70  ken, 1);.  if( p
209e0 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  New==0 ){.    sq
209e1 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
209e2 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 20  ete(db, pList); 
209e3 2f 2a 20 41 76 6f 69 64 20 6d 65 6d 6f 72 79 20  /* Avoid memory 
209e4 6c 65 61 6b 20 77 68 65 6e 20 6d 61 6c 6c 6f 63  leak when malloc
209e5 20 66 61 69 6c 73 20 2a 2f 0a 20 20 20 20 72 65   fails */.    re
209e6 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4e  turn 0;.  }.  pN
209e7 65 77 2d 3e 78 2e 70 4c 69 73 74 20 3d 20 70 4c  ew->x.pList = pL
209e8 69 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 21  ist;.  assert( !
209e9 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
209ea 70 4e 65 77 2c 20 45 50 5f 78 49 73 53 65 6c 65  pNew, EP_xIsSele
209eb 63 74 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ct) );.  sqlite3
209ec 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70 50  ExprSetHeight(pP
209ed 61 72 73 65 2c 20 70 4e 65 77 29 3b 0a 20 20 72  arse, pNew);.  r
209ee 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f  eturn pNew;.}../
209ef 2a 0a 2a 2a 20 41 73 73 69 67 6e 20 61 20 76 61  *.** Assign a va
209f0 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 74 6f  riable number to
209f1 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74   an expression t
209f2 68 61 74 20 65 6e 63 6f 64 65 73 20 61 20 77 69  hat encodes a wi
209f3 6c 64 63 61 72 64 0a 2a 2a 20 69 6e 20 74 68 65  ldcard.** in the
209f4 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74   original SQL st
209f5 61 74 65 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a  atement.  .**.**
209f6 20 57 69 6c 64 63 61 72 64 73 20 63 6f 6e 73 69   Wildcards consi
209f7 73 74 69 6e 67 20 6f 66 20 61 20 73 69 6e 67 6c  sting of a singl
209f8 65 20 22 3f 22 20 61 72 65 20 61 73 73 69 67 6e  e "?" are assign
209f9 65 64 20 74 68 65 20 6e 65 78 74 20 73 65 71 75  ed the next sequ
209fa 65 6e 74 69 61 6c 0a 2a 2a 20 76 61 72 69 61 62  ential.** variab
209fb 6c 65 20 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a  le number..**.**
209fc 20 57 69 6c 64 63 61 72 64 73 20 6f 66 20 74 68   Wildcards of th
209fd 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e 22 20 61 72  e form "?nnn" ar
209fe 65 20 61 73 73 69 67 6e 65 64 20 74 68 65 20 6e  e assigned the n
209ff 75 6d 62 65 72 20 22 6e 6e 6e 22 2e 20 20 57 65  umber "nnn".  We
20a00 20 6d 61 6b 65 0a 2a 2a 20 73 75 72 65 20 22 6e   make.** sure "n
20a01 6e 6e 22 20 69 73 20 6e 6f 74 20 74 6f 6f 20 62  nn" is not too b
20a02 65 20 74 6f 20 61 76 6f 69 64 20 61 20 64 65 6e  e to avoid a den
20a03 69 61 6c 20 6f 66 20 73 65 72 76 69 63 65 20 61  ial of service a
20a04 74 74 61 63 6b 20 77 68 65 6e 0a 2a 2a 20 74 68  ttack when.** th
20a05 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
20a06 63 6f 6d 65 73 20 66 72 6f 6d 20 61 6e 20 65 78  comes from an ex
20a07 74 65 72 6e 61 6c 20 73 6f 75 72 63 65 2e 0a 2a  ternal source..*
20a08 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 6f  *.** Wildcards o
20a09 66 20 74 68 65 20 66 6f 72 6d 20 22 3a 61 61 61  f the form ":aaa
20a0a 22 2c 20 22 40 61 61 61 22 2c 20 6f 72 20 22 24  ", "@aaa", or "$
20a0b 61 61 61 22 20 61 72 65 20 61 73 73 69 67 6e 65  aaa" are assigne
20a0c 64 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65  d the same numbe
20a0d 72 0a 2a 2a 20 61 73 20 74 68 65 20 70 72 65 76  r.** as the prev
20a0e 69 6f 75 73 20 69 6e 73 74 61 6e 63 65 20 6f 66  ious instance of
20a0f 20 74 68 65 20 73 61 6d 65 20 77 69 6c 64 63 61   the same wildca
20a10 72 64 2e 20 20 4f 72 20 69 66 20 74 68 69 73 20  rd.  Or if this 
20a11 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20  is the first.** 
20a12 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
20a13 77 69 6c 64 63 61 72 64 2c 20 74 68 65 20 6e 65  wildcard, the ne
20a14 78 74 20 73 65 71 75 65 6e 69 61 6c 20 76 61 72  xt sequenial var
20a15 69 61 62 6c 65 20 6e 75 6d 62 65 72 20 69 73 0a  iable number is.
20a16 2a 2a 20 61 73 73 69 67 6e 65 64 2e 0a 2a 2f 0a  ** assigned..*/.
20a17 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
20a18 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41  oid sqlite3ExprA
20a19 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 72 28 50  ssignVarNumber(P
20a1a 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78  arse *pParse, Ex
20a1b 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 73 71  pr *pExpr){.  sq
20a1c 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
20a1d 73 65 2d 3e 64 62 3b 0a 20 20 63 6f 6e 73 74 20  se->db;.  const 
20a1e 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 69 66 28 20  char *z;..  if( 
20a1f 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72  pExpr==0 ) retur
20a20 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 21 45 78  n;.  assert( !Ex
20a21 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
20a22 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61  (pExpr, EP_IntVa
20a23 6c 75 65 7c 45 50 5f 52 65 64 75 63 65 64 7c 45  lue|EP_Reduced|E
20a24 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 3b 0a  P_TokenOnly) );.
20a25 20 20 7a 20 3d 20 70 45 78 70 72 2d 3e 75 2e 7a    z = pExpr->u.z
20a26 54 6f 6b 65 6e 3b 0a 20 20 61 73 73 65 72 74 28  Token;.  assert(
20a27 20 7a 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72   z!=0 );.  asser
20a28 74 28 20 7a 5b 30 5d 21 3d 30 20 29 3b 0a 20 20  t( z[0]!=0 );.  
20a29 69 66 28 20 7a 5b 31 5d 3d 3d 30 20 29 7b 0a 20  if( z[1]==0 ){. 
20a2a 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 20 6f     /* Wildcard o
20a2b 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22 2e 20  f the form "?". 
20a2c 20 41 73 73 69 67 6e 20 74 68 65 20 6e 65 78 74   Assign the next
20a2d 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72   variable number
20a2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
20a2f 7a 5b 30 5d 3d 3d 27 3f 27 20 29 3b 0a 20 20 20  z[0]=='?' );.   
20a30 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20   pExpr->iColumn 
20a31 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 72  = ++pParse->nVar
20a32 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 5b  ;.  }else if( z[
20a33 30 5d 3d 3d 27 3f 27 20 29 7b 0a 20 20 20 20 2f  0]=='?' ){.    /
20a34 2a 20 57 69 6c 64 63 61 72 64 20 6f 66 20 74 68  * Wildcard of th
20a35 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e 22 2e 20 20  e form "?nnn".  
20a36 43 6f 6e 76 65 72 74 20 22 6e 6e 6e 22 20 74 6f  Convert "nnn" to
20a37 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 0a   an integer and.
20a38 20 20 20 20 2a 2a 20 75 73 65 20 69 74 20 61 73      ** use it as
20a39 20 74 68 65 20 76 61 72 69 61 62 6c 65 20 6e 75   the variable nu
20a3a 6d 62 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20  mber */.    int 
20a3b 69 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 43  i;.    pExpr->iC
20a3c 6f 6c 75 6d 6e 20 3d 20 69 20 3d 20 61 74 6f 69  olumn = i = atoi
20a3d 28 28 63 68 61 72 2a 29 26 7a 5b 31 5d 29 3b 0a  ((char*)&z[1]);.
20a3e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
20a3f 3d 30 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  =0 );.    testca
20a40 73 65 28 20 69 3d 3d 31 20 29 3b 0a 20 20 20 20  se( i==1 );.    
20a41 74 65 73 74 63 61 73 65 28 20 69 3d 3d 64 62 2d  testcase( i==db-
20a42 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
20a43 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55  IMIT_VARIABLE_NU
20a44 4d 42 45 52 5d 2d 31 20 29 3b 0a 20 20 20 20 74  MBER]-1 );.    t
20a45 65 73 74 63 61 73 65 28 20 69 3d 3d 64 62 2d 3e  estcase( i==db->
20a46 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
20a47 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d  MIT_VARIABLE_NUM
20a48 42 45 52 5d 20 29 3b 0a 20 20 20 20 69 66 28 20  BER] );.    if( 
20a49 69 3c 31 20 7c 7c 20 69 3e 64 62 2d 3e 61 4c 69  i<1 || i>db->aLi
20a4a 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
20a4b 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
20a4c 5d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ] ){.      sqlit
20a4d 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
20a4e 65 2c 20 22 76 61 72 69 61 62 6c 65 20 6e 75 6d  e, "variable num
20a4f 62 65 72 20 6d 75 73 74 20 62 65 20 62 65 74 77  ber must be betw
20a50 65 65 6e 20 3f 31 20 61 6e 64 20 3f 25 64 22 2c  een ?1 and ?%d",
20a51 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 61  .          db->a
20a52 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
20a53 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42  IT_VARIABLE_NUMB
20a54 45 52 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ER]);.    }.    
20a55 69 66 28 20 69 3e 70 50 61 72 73 65 2d 3e 6e 56  if( i>pParse->nV
20a56 61 72 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72  ar ){.      pPar
20a57 73 65 2d 3e 6e 56 61 72 20 3d 20 69 3b 0a 20 20  se->nVar = i;.  
20a58 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
20a59 20 2f 2a 20 57 69 6c 64 63 61 72 64 73 20 6c 69   /* Wildcards li
20a5a 6b 65 20 22 3a 61 61 61 22 2c 20 22 24 61 61 61  ke ":aaa", "$aaa
20a5b 22 20 6f 72 20 22 40 61 61 61 22 2e 20 20 52 65  " or "@aaa".  Re
20a5c 75 73 65 20 74 68 65 20 73 61 6d 65 20 76 61 72  use the same var
20a5d 69 61 62 6c 65 0a 20 20 20 20 2a 2a 20 6e 75 6d  iable.    ** num
20a5e 62 65 72 20 61 73 20 74 68 65 20 70 72 69 6f 72  ber as the prior
20a5f 20 61 70 70 65 61 72 61 6e 63 65 20 6f 66 20 74   appearance of t
20a60 68 65 20 73 61 6d 65 20 6e 61 6d 65 2c 20 6f 72  he same name, or
20a61 20 69 66 20 74 68 65 20 6e 61 6d 65 0a 20 20 20   if the name.   
20a62 20 2a 2a 20 68 61 73 20 6e 65 76 65 72 20 61 70   ** has never ap
20a63 70 65 61 72 65 64 20 62 65 66 6f 72 65 2c 20 72  peared before, r
20a64 65 75 73 65 20 74 68 65 20 73 61 6d 65 20 76 61  euse the same va
20a65 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 0a 20 20  riable number.  
20a66 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b 0a    */.    int i;.
20a67 20 20 20 20 75 33 32 20 6e 3b 0a 20 20 20 20 6e      u32 n;.    n
20a68 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
20a69 33 30 28 7a 29 3b 0a 20 20 20 20 66 6f 72 28 69  30(z);.    for(i
20a6a 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 56  =0; i<pParse->nV
20a6b 61 72 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20  arExpr; i++){.  
20a6c 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70      Expr *pE = p
20a6d 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72  Parse->apVarExpr
20a6e 5b 69 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72  [i];.      asser
20a6f 74 28 20 70 45 21 3d 30 20 29 3b 0a 20 20 20 20  t( pE!=0 );.    
20a70 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 70 45 2d    if( memcmp(pE-
20a71 3e 75 2e 7a 54 6f 6b 65 6e 2c 20 7a 2c 20 6e 29  >u.zToken, z, n)
20a72 3d 3d 30 20 26 26 20 70 45 2d 3e 75 2e 7a 54 6f  ==0 && pE->u.zTo
20a73 6b 65 6e 5b 6e 5d 3d 3d 30 20 29 7b 0a 20 20 20  ken[n]==0 ){.   
20a74 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c       pExpr->iCol
20a75 75 6d 6e 20 3d 20 70 45 2d 3e 69 43 6f 6c 75 6d  umn = pE->iColum
20a76 6e 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  n;.        break
20a77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
20a78 20 20 20 20 69 66 28 20 69 3e 3d 70 50 61 72 73      if( i>=pPars
20a79 65 2d 3e 6e 56 61 72 45 78 70 72 20 29 7b 0a 20  e->nVarExpr ){. 
20a7a 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c       pExpr->iCol
20a7b 75 6d 6e 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  umn = ++pParse->
20a7c 6e 56 61 72 3b 0a 20 20 20 20 20 20 69 66 28 20  nVar;.      if( 
20a7d 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72  pParse->nVarExpr
20a7e 3e 3d 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78  >=pParse->nVarEx
20a7f 70 72 41 6c 6c 6f 63 2d 31 20 29 7b 0a 20 20 20  prAlloc-1 ){.   
20a80 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56 61       pParse->nVa
20a81 72 45 78 70 72 41 6c 6c 6f 63 20 2b 3d 20 70 50  rExprAlloc += pP
20a82 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c  arse->nVarExprAl
20a83 6c 6f 63 20 2b 20 31 30 3b 0a 20 20 20 20 20 20  loc + 10;.      
20a84 20 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45    pParse->apVarE
20a85 78 70 72 20 3d 0a 20 20 20 20 20 20 20 20 20 20  xpr =.          
20a86 20 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c    sqlite3DbReall
20a87 6f 63 4f 72 46 72 65 65 28 0a 20 20 20 20 20 20  ocOrFree(.      
20a88 20 20 20 20 20 20 20 20 64 62 2c 0a 20 20 20 20          db,.    
20a89 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65            pParse
20a8a 2d 3e 61 70 56 61 72 45 78 70 72 2c 0a 20 20 20  ->apVarExpr,.   
20a8b 20 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73             pPars
20a8c 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63  e->nVarExprAlloc
20a8d 2a 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e  *sizeof(pParse->
20a8e 61 70 56 61 72 45 78 70 72 5b 30 5d 29 0a 20 20  apVarExpr[0]).  
20a8f 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20            );.   
20a90 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 21     }.      if( !
20a91 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
20a92 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
20a93 72 74 28 20 70 50 61 72 73 65 2d 3e 61 70 56 61  rt( pParse->apVa
20a94 72 45 78 70 72 21 3d 30 20 29 3b 0a 20 20 20 20  rExpr!=0 );.    
20a95 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56 61      pParse->apVa
20a96 72 45 78 70 72 5b 70 50 61 72 73 65 2d 3e 6e 56  rExpr[pParse->nV
20a97 61 72 45 78 70 72 2b 2b 5d 20 3d 20 70 45 78 70  arExpr++] = pExp
20a98 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  r;.      }.    }
20a99 0a 20 20 7d 20 0a 20 20 69 66 28 20 21 70 50 61  .  } .  if( !pPa
20a9a 72 73 65 2d 3e 6e 45 72 72 20 26 26 20 70 50 61  rse->nErr && pPa
20a9b 72 73 65 2d 3e 6e 56 61 72 3e 64 62 2d 3e 61 4c  rse->nVar>db->aL
20a9c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
20a9d 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45  T_VARIABLE_NUMBE
20a9e 52 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  R] ){.    sqlite
20a9f 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
20aa0 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 53 51 4c 20  , "too many SQL 
20aa1 76 61 72 69 61 62 6c 65 73 22 29 3b 0a 20 20 7d  variables");.  }
20aa2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  .}../*.** Clear 
20aa3 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 73 74  an expression st
20aa4 72 75 63 74 75 72 65 20 77 69 74 68 6f 75 74 20  ructure without 
20aa5 64 65 6c 65 74 69 6e 67 20 74 68 65 20 73 74 72  deleting the str
20aa6 75 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0a 2a  ucture itself..*
20aa7 2a 20 53 75 62 73 74 72 75 63 74 75 72 65 20 69  * Substructure i
20aa8 73 20 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 53 51  s deleted..*/.SQ
20aa9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
20aaa 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65  d sqlite3ExprCle
20aab 61 72 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ar(sqlite3 *db, 
20aac 45 78 70 72 20 2a 70 29 7b 0a 20 20 61 73 73 65  Expr *p){.  asse
20aad 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 69 66  rt( p!=0 );.  if
20aae 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f  ( !ExprHasAnyPro
20aaf 70 65 72 74 79 28 70 2c 20 45 50 5f 54 6f 6b 65  perty(p, EP_Toke
20ab0 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 73 71  nOnly) ){.    sq
20ab1 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
20ab2 64 62 2c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20  db, p->pLeft);. 
20ab3 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
20ab4 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 52 69 67  lete(db, p->pRig
20ab5 68 74 29 3b 0a 20 20 20 20 69 66 28 20 21 45 78  ht);.    if( !Ex
20ab6 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c  prHasProperty(p,
20ab7 20 45 50 5f 52 65 64 75 63 65 64 29 20 26 26 20   EP_Reduced) && 
20ab8 28 70 2d 3e 66 6c 61 67 73 32 20 26 20 45 50 32  (p->flags2 & EP2
20ab9 5f 4d 61 6c 6c 6f 63 65 64 54 6f 6b 65 6e 29 21  _MallocedToken)!
20aba 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
20abb 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
20abc 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20  >u.zToken);.    
20abd 7d 0a 20 20 20 20 69 66 28 20 45 78 70 72 48 61  }.    if( ExprHa
20abe 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f  sProperty(p, EP_
20abf 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  xIsSelect) ){.  
20ac0 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
20ac1 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 78  tDelete(db, p->x
20ac2 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 7d  .pSelect);.    }
20ac3 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
20ac4 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
20ac5 65 28 64 62 2c 20 70 2d 3e 78 2e 70 4c 69 73 74  e(db, p->x.pList
20ac6 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
20ac7 2f 2a 0a 2a 2a 20 52 65 63 75 72 73 69 76 65 6c  /*.** Recursivel
20ac8 79 20 64 65 6c 65 74 65 20 61 6e 20 65 78 70 72  y delete an expr
20ac9 65 73 73 69 6f 6e 20 74 72 65 65 2e 0a 2a 2f 0a  ession tree..*/.
20aca 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
20acb 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 44  oid sqlite3ExprD
20acc 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64  elete(sqlite3 *d
20acd 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20 69  b, Expr *p){.  i
20ace 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
20acf 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43  ;.  sqlite3ExprC
20ad0 6c 65 61 72 28 64 62 2c 20 70 29 3b 0a 20 20 69  lear(db, p);.  i
20ad1 66 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  f( !ExprHasPrope
20ad2 72 74 79 28 70 2c 20 45 50 5f 53 74 61 74 69 63  rty(p, EP_Static
20ad3 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
20ad4 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20  DbFree(db, p);. 
20ad5 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75   }.}../*.** Retu
20ad6 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
20ad7 20 62 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64   bytes allocated
20ad8 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 73 73   for the express
20ad9 69 6f 6e 20 73 74 72 75 63 74 75 72 65 20 0a 2a  ion structure .*
20ada 2a 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  * passed as the 
20adb 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20  first argument. 
20adc 54 68 69 73 20 69 73 20 61 6c 77 61 79 73 20 6f  This is always o
20add 6e 65 20 6f 66 20 45 58 50 52 5f 46 55 4c 4c 53  ne of EXPR_FULLS
20ade 49 5a 45 2c 0a 2a 2a 20 45 58 50 52 5f 52 45 44  IZE,.** EXPR_RED
20adf 55 43 45 44 53 49 5a 45 20 6f 72 20 45 58 50 52  UCEDSIZE or EXPR
20ae0 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49 5a 45 2e 0a  _TOKENONLYSIZE..
20ae1 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78  */.static int ex
20ae2 70 72 53 74 72 75 63 74 53 69 7a 65 28 45 78 70  prStructSize(Exp
20ae3 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 45 78 70  r *p){.  if( Exp
20ae4 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20  rHasProperty(p, 
20ae5 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29 20 29 20  EP_TokenOnly) ) 
20ae6 72 65 74 75 72 6e 20 45 58 50 52 5f 54 4f 4b 45  return EXPR_TOKE
20ae7 4e 4f 4e 4c 59 53 49 5a 45 3b 0a 20 20 69 66 28  NONLYSIZE;.  if(
20ae8 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
20ae9 28 70 2c 20 45 50 5f 52 65 64 75 63 65 64 29 20  (p, EP_Reduced) 
20aea 29 20 72 65 74 75 72 6e 20 45 58 50 52 5f 52 45  ) return EXPR_RE
20aeb 44 55 43 45 44 53 49 5a 45 3b 0a 20 20 72 65 74  DUCEDSIZE;.  ret
20aec 75 72 6e 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a  urn EXPR_FULLSIZ
20aed 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  E;.}../*.** The 
20aee 64 75 70 65 64 45 78 70 72 2a 53 69 7a 65 28 29  dupedExpr*Size()
20aef 20 72 6f 75 74 69 6e 65 73 20 65 61 63 68 20 72   routines each r
20af0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
20af1 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 72   of bytes requir
20af2 65 64 0a 2a 2a 20 74 6f 20 73 74 6f 72 65 20 61  ed.** to store a
20af3 20 63 6f 70 79 20 6f 66 20 61 6e 20 65 78 70 72   copy of an expr
20af4 65 73 73 69 6f 6e 20 6f 72 20 65 78 70 72 65 73  ession or expres
20af5 73 69 6f 6e 20 74 72 65 65 2e 20 20 54 68 65 79  sion tree.  They
20af6 20 64 69 66 66 65 72 20 69 6e 0a 2a 2a 20 68 6f   differ in.** ho
20af7 77 20 6d 75 63 68 20 6f 66 20 74 68 65 20 74 72  w much of the tr
20af8 65 65 20 69 73 20 6d 65 61 73 75 72 65 64 2e 0a  ee is measured..
20af9 2a 2a 0a 2a 2a 20 20 20 20 20 64 75 70 65 64 45  **.**     dupedE
20afa 78 70 72 53 74 72 75 63 74 53 69 7a 65 28 29 20  xprStructSize() 
20afb 20 20 20 20 53 69 7a 65 20 6f 66 20 6f 6e 6c 79      Size of only
20afc 20 74 68 65 20 45 78 70 72 20 73 74 72 75 63 74   the Expr struct
20afd 75 72 65 20 0a 2a 2a 20 20 20 20 20 64 75 70 65  ure .**     dupe
20afe 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 29 20  dExprNodeSize() 
20aff 20 20 20 20 20 20 53 69 7a 65 20 6f 66 20 45 78        Size of Ex
20b00 70 72 20 2b 20 73 70 61 63 65 20 66 6f 72 20 74  pr + space for t
20b01 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 64 75 70 65  oken.**     dupe
20b02 64 45 78 70 72 53 69 7a 65 28 29 20 20 20 20 20  dExprSize()     
20b03 20 20 20 20 20 20 45 78 70 72 20 2b 20 74 6f 6b        Expr + tok
20b04 65 6e 20 2b 20 73 75 62 74 72 65 65 20 63 6f 6d  en + subtree com
20b05 70 6f 6e 65 6e 74 73 0a 2a 2a 0a 2a 2a 2a 2a 2a  ponents.**.*****
20b06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20b0a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65  ******.**.** The
20b0b 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63 74   dupedExprStruct
20b0c 53 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e 20  Size() function 
20b0d 72 65 74 75 72 6e 73 20 74 77 6f 20 76 61 6c 75  returns two valu
20b0e 65 73 20 4f 52 2d 65 64 20 74 6f 67 65 74 68 65  es OR-ed togethe
20b0f 72 3a 20 20 0a 2a 2a 20 28 31 29 20 74 68 65 20  r:  .** (1) the 
20b10 73 70 61 63 65 20 72 65 71 75 69 72 65 64 20 66  space required f
20b11 6f 72 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  or a copy of the
20b12 20 45 78 70 72 20 73 74 72 75 63 74 75 72 65 20   Expr structure 
20b13 6f 6e 6c 79 20 61 6e 64 20 0a 2a 2a 20 28 32 29  only and .** (2)
20b14 20 74 68 65 20 45 50 5f 78 78 78 20 66 6c 61 67   the EP_xxx flag
20b15 73 20 74 68 61 74 20 69 6e 64 69 63 61 74 65 20  s that indicate 
20b16 77 68 61 74 20 74 68 65 20 73 74 72 75 63 74 75  what the structu
20b17 72 65 20 73 69 7a 65 20 73 68 6f 75 6c 64 20 62  re size should b
20b18 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  e..** The return
20b19 20 76 61 6c 75 65 73 20 69 73 20 61 6c 77 61 79   values is alway
20b1a 73 20 6f 6e 65 20 6f 66 3a 0a 2a 2a 0a 2a 2a 20  s one of:.**.** 
20b1b 20 20 20 20 20 45 58 50 52 5f 46 55 4c 4c 53 49       EXPR_FULLSI
20b1c 5a 45 0a 2a 2a 20 20 20 20 20 20 45 58 50 52 5f  ZE.**      EXPR_
20b1d 52 45 44 55 43 45 44 53 49 5a 45 20 20 20 7c 20  REDUCEDSIZE   | 
20b1e 45 50 5f 52 65 64 75 63 65 64 0a 2a 2a 20 20 20  EP_Reduced.**   
20b1f 20 20 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c     EXPR_TOKENONL
20b20 59 53 49 5a 45 20 7c 20 45 50 5f 54 6f 6b 65 6e  YSIZE | EP_Token
20b21 4f 6e 6c 79 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  Only.**.** The s
20b22 69 7a 65 20 6f 66 20 74 68 65 20 73 74 72 75 63  ize of the struc
20b23 74 75 72 65 20 63 61 6e 20 62 65 20 66 6f 75 6e  ture can be foun
20b24 64 20 62 79 20 6d 61 73 6b 69 6e 67 20 74 68 65  d by masking the
20b25 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a   return value.**
20b26 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
20b27 20 77 69 74 68 20 30 78 66 66 66 2e 20 20 54 68   with 0xfff.  Th
20b28 65 20 66 6c 61 67 73 20 63 61 6e 20 62 65 20 66  e flags can be f
20b29 6f 75 6e 64 20 62 79 20 6d 61 73 6b 69 6e 67 20  ound by masking 
20b2a 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20 76 61  the.** return va
20b2b 6c 75 65 20 77 69 74 68 20 45 50 5f 52 65 64 75  lue with EP_Redu
20b2c 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  ced|EP_TokenOnly
20b2d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
20b2e 74 20 77 69 74 68 20 66 6c 61 67 73 3d 3d 45 58  t with flags==EX
20b2f 50 52 44 55 50 5f 52 45 44 55 43 45 2c 20 74 68  PRDUP_REDUCE, th
20b30 69 73 20 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b  is routines work
20b31 73 20 6f 6e 20 66 75 6c 6c 2d 73 69 7a 65 0a 2a  s on full-size.*
20b32 2a 20 28 75 6e 72 65 64 75 63 65 64 29 20 45 78  * (unreduced) Ex
20b33 70 72 20 6f 62 6a 65 63 74 73 20 61 73 20 74 68  pr objects as th
20b34 65 79 20 6f 72 20 6f 72 69 67 69 6e 61 6c 6c 79  ey or originally
20b35 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62 79 20   constructed by 
20b36 74 68 65 20 70 61 72 73 65 72 2e 0a 2a 2a 20 44  the parser..** D
20b37 75 72 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e  uring expression
20b38 20 61 6e 61 6c 79 73 69 73 2c 20 65 78 74 72 61   analysis, extra
20b39 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20   information is 
20b3a 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 6d 6f 76  computed and mov
20b3b 65 64 20 69 6e 74 6f 0a 2a 2a 20 6c 61 74 65 72  ed into.** later
20b3c 20 70 61 72 74 73 20 6f 66 20 74 65 68 20 45 78   parts of teh Ex
20b3d 70 72 20 6f 62 6a 65 63 74 20 61 6e 64 20 74 68  pr object and th
20b3e 61 74 20 65 78 74 72 61 20 69 6e 66 6f 72 6d 61  at extra informa
20b3f 74 69 6f 6e 20 6d 69 67 68 74 20 67 65 74 20 63  tion might get c
20b40 68 6f 70 70 65 64 0a 2a 2a 20 6f 66 66 20 69 66  hopped.** off if
20b41 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
20b42 69 73 20 72 65 64 75 63 65 64 2e 20 20 4e 6f 74  is reduced.  Not
20b43 65 20 61 6c 73 6f 20 74 68 61 74 20 69 74 20 64  e also that it d
20b44 6f 65 73 20 6e 6f 74 20 77 6f 72 6b 20 74 6f 0a  oes not work to.
20b45 2a 2a 20 6d 61 6b 65 20 61 20 45 58 50 52 44 55  ** make a EXPRDU
20b46 50 5f 52 45 44 55 43 45 20 63 6f 70 79 20 6f 66  P_REDUCE copy of
20b47 20 61 20 72 65 64 75 63 65 64 20 65 78 70 72 65   a reduced expre
20b48 73 73 69 6f 6e 2e 20 20 49 74 20 69 73 20 6f 6e  ssion.  It is on
20b49 6c 79 20 6c 65 67 61 6c 0a 2a 2a 20 74 6f 20 72  ly legal.** to r
20b4a 65 64 75 63 65 20 61 20 70 72 69 73 74 69 6e 65  educe a pristine
20b4b 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   expression tree
20b4c 20 66 72 6f 6d 20 74 68 65 20 70 61 72 73 65 72   from the parser
20b4d 2e 20 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74  .  The implement
20b4e 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 64 75 70 65  ation.** of dupe
20b4f 64 45 78 70 72 53 74 72 75 63 74 53 69 7a 65 28  dExprStructSize(
20b50 29 20 63 6f 6e 74 61 69 6e 20 6d 75 6c 74 69 70  ) contain multip
20b51 6c 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74  le assert() stat
20b52 65 6d 65 6e 74 73 20 74 68 61 74 20 61 74 74 65  ements that atte
20b53 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 66 6f 72 63  mpt.** to enforc
20b54 65 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e  e this constrain
20b55 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
20b56 20 64 75 70 65 64 45 78 70 72 53 74 72 75 63 74   dupedExprStruct
20b57 53 69 7a 65 28 45 78 70 72 20 2a 70 2c 20 69 6e  Size(Expr *p, in
20b58 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20  t flags){.  int 
20b59 6e 53 69 7a 65 3b 0a 20 20 61 73 73 65 72 74 28  nSize;.  assert(
20b5a 20 66 6c 61 67 73 3d 3d 45 58 50 52 44 55 50 5f   flags==EXPRDUP_
20b5b 52 45 44 55 43 45 20 7c 7c 20 66 6c 61 67 73 3d  REDUCE || flags=
20b5c 3d 30 20 29 3b 20 2f 2a 20 4f 6e 6c 79 20 6f 6e  =0 ); /* Only on
20b5d 65 20 66 6c 61 67 20 76 61 6c 75 65 20 61 6c 6c  e flag value all
20b5e 6f 77 65 64 20 2a 2f 0a 20 20 69 66 28 20 30 3d  owed */.  if( 0=
20b5f 3d 28 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f  =(flags&EXPRDUP_
20b60 52 45 44 55 43 45 29 20 29 7b 0a 20 20 20 20 6e  REDUCE) ){.    n
20b61 53 69 7a 65 20 3d 20 45 58 50 52 5f 46 55 4c 4c  Size = EXPR_FULL
20b62 53 49 5a 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  SIZE;.  }else{. 
20b63 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
20b64 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70  HasAnyProperty(p
20b65 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45  , EP_TokenOnly|E
20b66 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20 20  P_Reduced) );.  
20b67 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
20b68 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50  asProperty(p, EP
20b69 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 3b 20 0a 20  _FromJoin) ); . 
20b6a 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66     assert( (p->f
20b6b 6c 61 67 73 32 20 26 20 45 50 32 5f 4d 61 6c 6c  lags2 & EP2_Mall
20b6c 6f 63 65 64 54 6f 6b 65 6e 29 3d 3d 30 20 29 3b  ocedToken)==0 );
20b6d 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d  .    assert( (p-
20b6e 3e 66 6c 61 67 73 32 20 26 20 45 50 32 5f 49 72  >flags2 & EP2_Ir
20b6f 72 65 64 75 63 69 62 6c 65 29 3d 3d 30 20 29 3b  reducible)==0 );
20b70 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 65 66  .    if( p->pLef
20b71 74 20 7c 7c 20 70 2d 3e 70 52 69 67 68 74 20 7c  t || p->pRight |
20b72 7c 20 70 2d 3e 70 43 6f 6c 6c 20 7c 7c 20 70 2d  | p->pColl || p-
20b73 3e 78 2e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20  >x.pList ){.    
20b74 20 20 6e 53 69 7a 65 20 3d 20 45 58 50 52 5f 52    nSize = EXPR_R
20b75 45 44 55 43 45 44 53 49 5a 45 20 7c 20 45 50 5f  EDUCEDSIZE | EP_
20b76 52 65 64 75 63 65 64 3b 0a 20 20 20 20 7d 65 6c  Reduced;.    }el
20b77 73 65 7b 0a 20 20 20 20 20 20 6e 53 69 7a 65 20  se{.      nSize 
20b78 3d 20 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59  = EXPR_TOKENONLY
20b79 53 49 5a 45 20 7c 20 45 50 5f 54 6f 6b 65 6e 4f  SIZE | EP_TokenO
20b7a 6e 6c 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  nly;.    }.  }. 
20b7b 20 72 65 74 75 72 6e 20 6e 53 69 7a 65 3b 0a 7d   return nSize;.}
20b7c 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
20b7d 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
20b7e 65 20 73 70 61 63 65 20 69 6e 20 62 79 74 65 73  e space in bytes
20b7f 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f   required to sto
20b80 72 65 20 74 68 65 20 63 6f 70 79 20 0a 2a 2a 20  re the copy .** 
20b81 6f 66 20 74 68 65 20 45 78 70 72 20 73 74 72 75  of the Expr stru
20b82 63 74 75 72 65 20 61 6e 64 20 61 20 63 6f 70 79  cture and a copy
20b83 20 6f 66 20 74 68 65 20 45 78 70 72 2e 75 2e 7a   of the Expr.u.z
20b84 54 6f 6b 65 6e 20 73 74 72 69 6e 67 20 28 69 66  Token string (if
20b85 20 74 68 61 74 0a 2a 2a 20 73 74 72 69 6e 67 20   that.** string 
20b86 69 73 20 64 65 66 69 6e 65 64 2e 29 0a 2a 2f 0a  is defined.).*/.
20b87 73 74 61 74 69 63 20 69 6e 74 20 64 75 70 65 64  static int duped
20b88 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 45 78 70  ExprNodeSize(Exp
20b89 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29  r *p, int flags)
20b8a 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d 20  {.  int nByte = 
20b8b 64 75 70 65 64 45 78 70 72 53 74 72 75 63 74 53  dupedExprStructS
20b8c 69 7a 65 28 70 2c 20 66 6c 61 67 73 29 20 26 20  ize(p, flags) & 
20b8d 30 78 66 66 66 3b 0a 20 20 69 66 28 20 21 45 78  0xfff;.  if( !Ex
20b8e 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c  prHasProperty(p,
20b8f 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 26 26   EP_IntValue) &&
20b90 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 29 7b 0a   p->u.zToken ){.
20b91 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 73 71 6c      nByte += sql
20b92 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 2d 3e  ite3Strlen30(p->
20b93 75 2e 7a 54 6f 6b 65 6e 29 2b 31 3b 0a 20 20 7d  u.zToken)+1;.  }
20b94 0a 20 20 72 65 74 75 72 6e 20 52 4f 55 4e 44 38  .  return ROUND8
20b95 28 6e 42 79 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  (nByte);.}../*.*
20b96 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
20b97 62 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 71  ber of bytes req
20b98 75 69 72 65 64 20 74 6f 20 63 72 65 61 74 65 20  uired to create 
20b99 61 20 64 75 70 6c 69 63 61 74 65 20 6f 66 20 74  a duplicate of t
20b9a 68 65 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f  he .** expressio
20b9b 6e 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  n passed as the 
20b9c 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e 20  first argument. 
20b9d 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
20b9e 65 6e 74 20 69 73 20 61 0a 2a 2a 20 6d 61 73 6b  ent is a.** mask
20b9f 20 63 6f 6e 74 61 69 6e 69 6e 67 20 45 58 50 52   containing EXPR
20ba0 44 55 50 5f 58 58 58 20 66 6c 61 67 73 2e 0a 2a  DUP_XXX flags..*
20ba1 2a 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 72  *.** The value r
20ba2 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64 65 73  eturned includes
20ba3 20 73 70 61 63 65 20 74 6f 20 63 72 65 61 74 65   space to create
20ba4 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 45   a copy of the E
20ba5 78 70 72 20 73 74 72 75 63 74 0a 2a 2a 20 69 74  xpr struct.** it
20ba6 73 65 6c 66 20 61 6e 64 20 74 68 65 20 62 75 66  self and the buf
20ba7 66 65 72 20 72 65 66 65 72 72 65 64 20 74 6f 20  fer referred to 
20ba8 62 79 20 45 78 70 72 2e 75 2e 7a 54 6f 6b 65 6e  by Expr.u.zToken
20ba9 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20  , if any..**.** 
20baa 49 66 20 74 68 65 20 45 58 50 52 44 55 50 5f 52  If the EXPRDUP_R
20bab 45 44 55 43 45 20 66 6c 61 67 20 69 73 20 73 65  EDUCE flag is se
20bac 74 2c 20 74 68 65 6e 20 74 68 65 20 72 65 74 75  t, then the retu
20bad 72 6e 20 76 61 6c 75 65 20 69 6e 63 6c 75 64 65  rn value include
20bae 73 20 0a 2a 2a 20 73 70 61 63 65 20 74 6f 20 64  s .** space to d
20baf 75 70 6c 69 63 61 74 65 20 61 6c 6c 20 45 78 70  uplicate all Exp
20bb0 72 20 6e 6f 64 65 73 20 69 6e 20 74 68 65 20 74  r nodes in the t
20bb1 72 65 65 20 66 6f 72 6d 65 64 20 62 79 20 45 78  ree formed by Ex
20bb2 70 72 2e 70 4c 65 66 74 20 0a 2a 2a 20 61 6e 64  pr.pLeft .** and
20bb3 20 45 78 70 72 2e 70 52 69 67 68 74 20 76 61 72   Expr.pRight var
20bb4 69 61 62 6c 65 73 20 28 62 75 74 20 6e 6f 74 20  iables (but not 
20bb5 66 6f 72 20 61 6e 79 20 73 74 72 75 63 74 75 72  for any structur
20bb6 65 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 6f 72  es pointed to or
20bb7 20 0a 2a 2a 20 64 65 73 63 65 6e 64 65 64 20 66   .** descended f
20bb8 72 6f 6d 20 74 68 65 20 45 78 70 72 2e 78 2e 70  rom the Expr.x.p
20bb9 4c 69 73 74 20 6f 72 20 45 78 70 72 2e 78 2e 70  List or Expr.x.p
20bba 53 65 6c 65 63 74 20 76 61 72 69 61 62 6c 65 73  Select variables
20bbb 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
20bbc 20 64 75 70 65 64 45 78 70 72 53 69 7a 65 28 45   dupedExprSize(E
20bbd 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67  xpr *p, int flag
20bbe 73 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20  s){.  int nByte 
20bbf 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a  = 0;.  if( p ){.
20bc0 20 20 20 20 6e 42 79 74 65 20 3d 20 64 75 70 65      nByte = dupe
20bc1 64 45 78 70 72 4e 6f 64 65 53 69 7a 65 28 70 2c  dExprNodeSize(p,
20bc2 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 69 66 28   flags);.    if(
20bc3 20 66 6c 61 67 73 26 45 58 50 52 44 55 50 5f 52   flags&EXPRDUP_R
20bc4 45 44 55 43 45 20 29 7b 0a 20 20 20 20 20 20 6e  EDUCE ){.      n
20bc5 42 79 74 65 20 2b 3d 20 64 75 70 65 64 45 78 70  Byte += dupedExp
20bc6 72 53 69 7a 65 28 70 2d 3e 70 4c 65 66 74 2c 20  rSize(p->pLeft, 
20bc7 66 6c 61 67 73 29 20 2b 20 64 75 70 65 64 45 78  flags) + dupedEx
20bc8 70 72 53 69 7a 65 28 70 2d 3e 70 52 69 67 68 74  prSize(p->pRight
20bc9 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 7d 0a  , flags);.    }.
20bca 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 42 79    }.  return nBy
20bcb 74 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  te;.}../*.** Thi
20bcc 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 73 69  s function is si
20bcd 6d 69 6c 61 72 20 74 6f 20 73 71 6c 69 74 65 33  milar to sqlite3
20bce 45 78 70 72 44 75 70 28 29 2c 20 65 78 63 65 70  ExprDup(), excep
20bcf 74 20 74 68 61 74 20 69 66 20 70 7a 42 75 66 66  t that if pzBuff
20bd0 65 72 20 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55  er .** is not NU
20bd1 4c 4c 20 74 68 65 6e 20 2a 70 7a 42 75 66 66 65  LL then *pzBuffe
20bd2 72 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20  r is assumed to 
20bd3 70 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65  point to a buffe
20bd4 72 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 0a  r large enough .
20bd5 2a 2a 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  ** to store the 
20bd6 63 6f 70 79 20 6f 66 20 65 78 70 72 65 73 73 69  copy of expressi
20bd7 6f 6e 20 70 2c 20 74 68 65 20 63 6f 70 69 65 73  on p, the copies
20bd8 20 6f 66 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 0a   of p->u.zToken.
20bd9 2a 2a 20 28 69 66 20 61 70 70 6c 69 63 61 62 6c  ** (if applicabl
20bda 65 29 2c 20 61 6e 64 20 74 68 65 20 63 6f 70 69  e), and the copi
20bdb 65 73 20 6f 66 20 74 68 65 20 70 2d 3e 70 4c 65  es of the p->pLe
20bdc 66 74 20 61 6e 64 20 70 2d 3e 70 52 69 67 68 74  ft and p->pRight
20bdd 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 0a 2a 2a   expressions,.**
20bde 20 69 66 20 61 6e 79 2e 20 42 65 66 6f 72 65 20   if any. Before 
20bdf 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70 7a 42 75  returning, *pzBu
20be0 66 66 65 72 20 69 73 20 73 65 74 20 74 6f 20 74  ffer is set to t
20be1 68 65 20 66 69 72 73 74 20 62 79 74 65 20 70 61  he first byte pa
20be2 73 73 65 64 20 74 68 65 0a 2a 2a 20 70 6f 72 74  ssed the.** port
20be3 69 6f 6e 20 6f 66 20 74 68 65 20 62 75 66 66 65  ion of the buffe
20be4 72 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 62 79  r copied into by
20be5 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a   this function..
20be6 2a 2f 0a 73 74 61 74 69 63 20 45 78 70 72 20 2a  */.static Expr *
20be7 65 78 70 72 44 75 70 28 73 71 6c 69 74 65 33 20  exprDup(sqlite3 
20be8 2a 64 62 2c 20 45 78 70 72 20 2a 70 2c 20 69 6e  *db, Expr *p, in
20be9 74 20 66 6c 61 67 73 2c 20 75 38 20 2a 2a 70 7a  t flags, u8 **pz
20bea 42 75 66 66 65 72 29 7b 0a 20 20 45 78 70 72 20  Buffer){.  Expr 
20beb 2a 70 4e 65 77 20 3d 20 30 3b 20 20 20 20 20 20  *pNew = 0;      
20bec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20bed 2f 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75  /* Value to retu
20bee 72 6e 20 2a 2f 0a 20 20 69 66 28 20 70 20 29 7b  rn */.  if( p ){
20bef 0a 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 69  .    const int i
20bf0 73 52 65 64 75 63 65 64 20 3d 20 28 66 6c 61 67  sReduced = (flag
20bf1 73 26 45 58 50 52 44 55 50 5f 52 45 44 55 43 45  s&EXPRDUP_REDUCE
20bf2 29 3b 0a 20 20 20 20 75 38 20 2a 7a 41 6c 6c 6f  );.    u8 *zAllo
20bf3 63 3b 0a 20 20 20 20 75 33 32 20 73 74 61 74 69  c;.    u32 stati
20bf4 63 46 6c 61 67 20 3d 20 30 3b 0a 0a 20 20 20 20  cFlag = 0;..    
20bf5 61 73 73 65 72 74 28 20 70 7a 42 75 66 66 65 72  assert( pzBuffer
20bf6 3d 3d 30 20 7c 7c 20 69 73 52 65 64 75 63 65 64  ==0 || isReduced
20bf7 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75   );..    /* Figu
20bf8 72 65 20 6f 75 74 20 77 68 65 72 65 20 74 6f 20  re out where to 
20bf9 77 72 69 74 65 20 74 68 65 20 6e 65 77 20 45 78  write the new Ex
20bfa 70 72 20 73 74 72 75 63 74 75 72 65 2e 20 2a 2f  pr structure. */
20bfb 0a 20 20 20 20 69 66 28 20 70 7a 42 75 66 66 65  .    if( pzBuffe
20bfc 72 20 29 7b 0a 20 20 20 20 20 20 7a 41 6c 6c 6f  r ){.      zAllo
20bfd 63 20 3d 20 2a 70 7a 42 75 66 66 65 72 3b 0a 20  c = *pzBuffer;. 
20bfe 20 20 20 20 20 73 74 61 74 69 63 46 6c 61 67 20       staticFlag 
20bff 3d 20 45 50 5f 53 74 61 74 69 63 3b 0a 20 20 20  = EP_Static;.   
20c00 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 41   }else{.      zA
20c01 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62  lloc = sqlite3Db
20c02 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 64 75  MallocRaw(db, du
20c03 70 65 64 45 78 70 72 53 69 7a 65 28 70 2c 20 66  pedExprSize(p, f
20c04 6c 61 67 73 29 29 3b 0a 20 20 20 20 7d 0a 20 20  lags));.    }.  
20c05 20 20 70 4e 65 77 20 3d 20 28 45 78 70 72 20 2a    pNew = (Expr *
20c06 29 7a 41 6c 6c 6f 63 3b 0a 0a 20 20 20 20 69 66  )zAlloc;..    if
20c07 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20  ( pNew ){.      
20c08 2f 2a 20 53 65 74 20 6e 4e 65 77 53 69 7a 65 20  /* Set nNewSize 
20c09 74 6f 20 74 68 65 20 73 69 7a 65 20 61 6c 6c 6f  to the size allo
20c0a 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 73 74  cated for the st
20c0b 72 75 63 74 75 72 65 20 70 6f 69 6e 74 65 64 20  ructure pointed 
20c0c 74 6f 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 70  to.      ** by p
20c0d 4e 65 77 2e 20 54 68 69 73 20 69 73 20 65 69 74  New. This is eit
20c0e 68 65 72 20 45 58 50 52 5f 46 55 4c 4c 53 49 5a  her EXPR_FULLSIZ
20c0f 45 2c 20 45 58 50 52 5f 52 45 44 55 43 45 44 53  E, EXPR_REDUCEDS
20c10 49 5a 45 20 6f 72 0a 20 20 20 20 20 20 2a 2a 20  IZE or.      ** 
20c11 45 58 50 52 5f 54 4f 4b 45 4e 4f 4e 4c 59 53 49  EXPR_TOKENONLYSI
20c12 5a 45 2e 20 6e 54 6f 6b 65 6e 20 69 73 20 73 65  ZE. nToken is se
20c13 74 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20  t to the number 
20c14 6f 66 20 62 79 74 65 73 20 63 6f 6e 73 75 6d 65  of bytes consume
20c15 64 0a 20 20 20 20 20 20 2a 2a 20 62 79 20 74 68  d.      ** by th
20c16 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 70 2d  e copy of the p-
20c17 3e 75 2e 7a 54 6f 6b 65 6e 20 73 74 72 69 6e 67  >u.zToken string
20c18 20 28 69 66 20 61 6e 79 29 2e 0a 20 20 20 20 20   (if any)..     
20c19 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20   */.      const 
20c1a 75 6e 73 69 67 6e 65 64 20 6e 53 74 72 75 63 74  unsigned nStruct
20c1b 53 69 7a 65 20 3d 20 64 75 70 65 64 45 78 70 72  Size = dupedExpr
20c1c 53 74 72 75 63 74 53 69 7a 65 28 70 2c 20 66 6c  StructSize(p, fl
20c1d 61 67 73 29 3b 0a 20 20 20 20 20 20 63 6f 6e 73  ags);.      cons
20c1e 74 20 69 6e 74 20 6e 4e 65 77 53 69 7a 65 20 3d  t int nNewSize =
20c1f 20 6e 53 74 72 75 63 74 53 69 7a 65 20 26 20 30   nStructSize & 0
20c20 78 66 66 66 3b 0a 20 20 20 20 20 20 69 6e 74 20  xfff;.      int 
20c21 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 69 66  nToken;.      if
20c22 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
20c23 74 79 28 70 2c 20 45 50 5f 49 6e 74 56 61 6c 75  ty(p, EP_IntValu
20c24 65 29 20 26 26 20 70 2d 3e 75 2e 7a 54 6f 6b 65  e) && p->u.zToke
20c25 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 54 6f  n ){.        nTo
20c26 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72  ken = sqlite3Str
20c27 6c 65 6e 33 30 28 70 2d 3e 75 2e 7a 54 6f 6b 65  len30(p->u.zToke
20c28 6e 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 7d 65  n) + 1;.      }e
20c29 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6e 54 6f  lse{.        nTo
20c2a 6b 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  ken = 0;.      }
20c2b 0a 20 20 20 20 20 20 69 66 28 20 69 73 52 65 64  .      if( isRed
20c2c 75 63 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20  uced ){.        
20c2d 61 73 73 65 72 74 28 20 45 78 70 72 48 61 73 50  assert( ExprHasP
20c2e 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 52 65  roperty(p, EP_Re
20c2f 64 75 63 65 64 29 3d 3d 30 20 29 3b 0a 20 20 20  duced)==0 );.   
20c30 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 41 6c 6c       memcpy(zAll
20c31 6f 63 2c 20 70 2c 20 6e 4e 65 77 53 69 7a 65 29  oc, p, nNewSize)
20c32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
20c33 20 20 20 20 20 20 20 69 6e 74 20 6e 53 69 7a 65         int nSize
20c34 20 3d 20 65 78 70 72 53 74 72 75 63 74 53 69 7a   = exprStructSiz
20c35 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 6d 65  e(p);.        me
20c36 6d 63 70 79 28 7a 41 6c 6c 6f 63 2c 20 70 2c 20  mcpy(zAlloc, p, 
20c37 6e 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20  nSize);.        
20c38 6d 65 6d 73 65 74 28 26 7a 41 6c 6c 6f 63 5b 6e  memset(&zAlloc[n
20c39 53 69 7a 65 5d 2c 20 30 2c 20 45 58 50 52 5f 46  Size], 0, EXPR_F
20c3a 55 4c 4c 53 49 5a 45 2d 6e 53 69 7a 65 29 3b 0a  ULLSIZE-nSize);.
20c3b 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
20c3c 2a 20 53 65 74 20 74 68 65 20 45 50 5f 52 65 64  * Set the EP_Red
20c3d 75 63 65 64 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e  uced, EP_TokenOn
20c3e 6c 79 2c 20 61 6e 64 20 45 50 5f 53 74 61 74 69  ly, and EP_Stati
20c3f 63 20 66 6c 61 67 73 20 61 70 70 72 6f 70 72 69  c flags appropri
20c40 61 74 65 6c 79 2e 20 2a 2f 0a 20 20 20 20 20 20  ately. */.      
20c41 70 4e 65 77 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  pNew->flags &= ~
20c42 28 45 50 5f 52 65 64 75 63 65 64 7c 45 50 5f 54  (EP_Reduced|EP_T
20c43 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 53 74 61 74  okenOnly|EP_Stat
20c44 69 63 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d  ic);.      pNew-
20c45 3e 66 6c 61 67 73 20 7c 3d 20 6e 53 74 72 75 63  >flags |= nStruc
20c46 74 53 69 7a 65 20 26 20 28 45 50 5f 52 65 64 75  tSize & (EP_Redu
20c47 63 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79  ced|EP_TokenOnly
20c48 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 66  );.      pNew->f
20c49 6c 61 67 73 20 7c 3d 20 73 74 61 74 69 63 46 6c  lags |= staticFl
20c4a 61 67 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f  ag;..      /* Co
20c4b 70 79 20 74 68 65 20 70 2d 3e 75 2e 7a 54 6f 6b  py the p->u.zTok
20c4c 65 6e 20 73 74 72 69 6e 67 2c 20 69 66 20 61 6e  en string, if an
20c4d 79 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  y. */.      if( 
20c4e 6e 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 20 20  nToken ){.      
20c4f 20 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 20 3d    char *zToken =
20c50 20 70 4e 65 77 2d 3e 75 2e 7a 54 6f 6b 65 6e 20   pNew->u.zToken 
20c51 3d 20 28 63 68 61 72 2a 29 26 7a 41 6c 6c 6f 63  = (char*)&zAlloc
20c52 5b 6e 4e 65 77 53 69 7a 65 5d 3b 0a 20 20 20 20  [nNewSize];.    
20c53 20 20 20 20 6d 65 6d 63 70 79 28 7a 54 6f 6b 65      memcpy(zToke
20c54 6e 2c 20 70 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20  n, p->u.zToken, 
20c55 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d  nToken);.      }
20c56 0a 0a 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28  ..      if( 0==(
20c57 28 70 2d 3e 66 6c 61 67 73 7c 70 4e 65 77 2d 3e  (p->flags|pNew->
20c58 66 6c 61 67 73 29 20 26 20 45 50 5f 54 6f 6b 65  flags) & EP_Toke
20c59 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20 20  nOnly) ){.      
20c5a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74 68 65    /* Fill in the
20c5b 20 70 4e 65 77 2d 3e 78 2e 70 53 65 6c 65 63 74   pNew->x.pSelect
20c5c 20 6f 72 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73   or pNew->x.pLis
20c5d 74 20 6d 65 6d 62 65 72 2e 20 2a 2f 0a 20 20 20  t member. */.   
20c5e 20 20 20 20 20 69 66 28 20 45 78 70 72 48 61 73       if( ExprHas
20c5f 50 72 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 78  Property(p, EP_x
20c60 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20  IsSelect) ){.   
20c61 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 78 2e 70         pNew->x.p
20c62 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33  Select = sqlite3
20c63 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70 2d  SelectDup(db, p-
20c64 3e 78 2e 70 53 65 6c 65 63 74 2c 20 69 73 52 65  >x.pSelect, isRe
20c65 64 75 63 65 64 29 3b 0a 20 20 20 20 20 20 20 20  duced);.        
20c66 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
20c67 20 70 4e 65 77 2d 3e 78 2e 70 4c 69 73 74 20 3d   pNew->x.pList =
20c68 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
20c69 44 75 70 28 64 62 2c 20 70 2d 3e 78 2e 70 4c 69  Dup(db, p->x.pLi
20c6a 73 74 2c 20 69 73 52 65 64 75 63 65 64 29 3b 0a  st, isReduced);.
20c6b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
20c6c 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 46 69 6c 6c  }..      /* Fill
20c6d 20 69 6e 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20   in pNew->pLeft 
20c6e 61 6e 64 20 70 4e 65 77 2d 3e 70 52 69 67 68 74  and pNew->pRight
20c6f 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 45  . */.      if( E
20c70 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74  xprHasAnyPropert
20c71 79 28 70 4e 65 77 2c 20 45 50 5f 52 65 64 75 63  y(pNew, EP_Reduc
20c72 65 64 7c 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 29  ed|EP_TokenOnly)
20c73 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 41 6c 6c   ){.        zAll
20c74 6f 63 20 2b 3d 20 64 75 70 65 64 45 78 70 72 4e  oc += dupedExprN
20c75 6f 64 65 53 69 7a 65 28 70 2c 20 66 6c 61 67 73  odeSize(p, flags
20c76 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 45  );.        if( E
20c77 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
20c78 4e 65 77 2c 20 45 50 5f 52 65 64 75 63 65 64 29  New, EP_Reduced)
20c79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e   ){.          pN
20c7a 65 77 2d 3e 70 4c 65 66 74 20 3d 20 65 78 70 72  ew->pLeft = expr
20c7b 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74  Dup(db, p->pLeft
20c7c 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45  , EXPRDUP_REDUCE
20c7d 2c 20 26 7a 41 6c 6c 6f 63 29 3b 0a 20 20 20 20  , &zAlloc);.    
20c7e 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 52 69 67        pNew->pRig
20c7f 68 74 20 3d 20 65 78 70 72 44 75 70 28 64 62 2c  ht = exprDup(db,
20c80 20 70 2d 3e 70 52 69 67 68 74 2c 20 45 58 50 52   p->pRight, EXPR
20c81 44 55 50 5f 52 45 44 55 43 45 2c 20 26 7a 41 6c  DUP_REDUCE, &zAl
20c82 6c 6f 63 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  loc);.        }.
20c83 20 20 20 20 20 20 20 20 69 66 28 20 70 7a 42 75          if( pzBu
20c84 66 66 65 72 20 29 7b 0a 20 20 20 20 20 20 20 20  ffer ){.        
20c85 20 20 2a 70 7a 42 75 66 66 65 72 20 3d 20 7a 41    *pzBuffer = zA
20c86 6c 6c 6f 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  lloc;.        }.
20c87 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
20c88 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73       pNew->flags
20c89 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69  2 = 0;.        i
20c8a 66 28 20 21 45 78 70 72 48 61 73 41 6e 79 50 72  f( !ExprHasAnyPr
20c8b 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 54 6f 6b  operty(p, EP_Tok
20c8c 65 6e 4f 6e 6c 79 29 20 29 7b 0a 20 20 20 20 20  enOnly) ){.     
20c8d 20 20 20 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74       pNew->pLeft
20c8e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
20c8f 70 28 64 62 2c 20 70 2d 3e 70 4c 65 66 74 2c 20  p(db, p->pLeft, 
20c90 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4e  0);.          pN
20c91 65 77 2d 3e 70 52 69 67 68 74 20 3d 20 73 71 6c  ew->pRight = sql
20c92 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
20c93 70 2d 3e 70 52 69 67 68 74 2c 20 30 29 3b 0a 20  p->pRight, 0);. 
20c94 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
20c95 0a 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ..    }.  }.  re
20c96 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a  turn pNew;.}../*
20c97 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
20c98 67 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69  g group of routi
20c99 6e 65 73 20 6d 61 6b 65 20 64 65 65 70 20 63 6f  nes make deep co
20c9a 70 69 65 73 20 6f 66 20 65 78 70 72 65 73 73 69  pies of expressi
20c9b 6f 6e 73 2c 0a 2a 2a 20 65 78 70 72 65 73 73 69  ons,.** expressi
20c9c 6f 6e 20 6c 69 73 74 73 2c 20 49 44 20 6c 69 73  on lists, ID lis
20c9d 74 73 2c 20 61 6e 64 20 73 65 6c 65 63 74 20 73  ts, and select s
20c9e 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 65 20  tatements.  The 
20c9f 63 6f 70 69 65 73 20 63 61 6e 0a 2a 2a 20 62 65  copies can.** be
20ca0 20 64 65 6c 65 74 65 64 20 28 62 79 20 62 65 69   deleted (by bei
20ca1 6e 67 20 70 61 73 73 65 64 20 74 6f 20 74 68 65  ng passed to the
20ca2 69 72 20 72 65 73 70 65 63 74 69 76 65 20 2e 2e  ir respective ..
20ca3 2e 44 65 6c 65 74 65 28 29 20 72 6f 75 74 69 6e  .Delete() routin
20ca4 65 73 29 0a 2a 2a 20 77 69 74 68 6f 75 74 20 65  es).** without e
20ca5 66 66 65 63 74 69 6e 67 20 74 68 65 20 6f 72 69  ffecting the ori
20ca6 67 69 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ginals..**.** Th
20ca7 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73  e expression lis
20ca8 74 2c 20 49 44 2c 20 61 6e 64 20 73 6f 75 72 63  t, ID, and sourc
20ca9 65 20 6c 69 73 74 73 20 72 65 74 75 72 6e 20 62  e lists return b
20caa 79 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  y sqlite3ExprLis
20cab 74 44 75 70 28 29 2c 0a 2a 2a 20 73 71 6c 69 74  tDup(),.** sqlit
20cac 65 33 49 64 4c 69 73 74 44 75 70 28 29 2c 20 61  e3IdListDup(), a
20cad 6e 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  nd sqlite3SrcLis
20cae 74 44 75 70 28 29 20 63 61 6e 20 6e 6f 74 20 62  tDup() can not b
20caf 65 20 66 75 72 74 68 65 72 20 65 78 70 61 6e 64  e further expand
20cb0 65 64 20 0a 2a 2a 20 62 79 20 73 75 62 73 65 71  ed .** by subseq
20cb1 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20 73 71  uent calls to sq
20cb2 6c 69 74 65 2a 4c 69 73 74 41 70 70 65 6e 64 28  lite*ListAppend(
20cb3 29 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a  ) routines..**.*
20cb4 2a 20 41 6e 79 20 74 61 62 6c 65 73 20 74 68 61  * Any tables tha
20cb5 74 20 74 68 65 20 53 72 63 4c 69 73 74 20 6d 69  t the SrcList mi
20cb6 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 61 72 65  ght point to are
20cb7 20 6e 6f 74 20 64 75 70 6c 69 63 61 74 65 64 2e   not duplicated.
20cb8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73  .**.** The flags
20cb9 20 70 61 72 61 6d 65 74 65 72 20 63 6f 6e 74 61   parameter conta
20cba 69 6e 73 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f  ins a combinatio
20cbb 6e 20 6f 66 20 74 68 65 20 45 58 50 52 44 55 50  n of the EXPRDUP
20cbc 5f 58 58 58 20 66 6c 61 67 73 2e 0a 2a 2a 20 49  _XXX flags..** I
20cbd 66 20 74 68 65 20 45 58 50 52 44 55 50 5f 52 45  f the EXPRDUP_RE
20cbe 44 55 43 45 20 66 6c 61 67 20 69 73 20 73 65 74  DUCE flag is set
20cbf 2c 20 74 68 65 6e 20 74 68 65 20 73 74 72 75 63  , then the struc
20cc0 74 75 72 65 20 72 65 74 75 72 6e 65 64 20 69 73  ture returned is
20cc1 20 61 0a 2a 2a 20 74 72 75 6e 63 61 74 65 64 20   a.** truncated 
20cc2 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 75  version of the u
20cc3 73 75 61 6c 20 45 78 70 72 20 73 74 72 75 63 74  sual Expr struct
20cc4 75 72 65 20 74 68 61 74 20 77 69 6c 6c 20 62 65  ure that will be
20cc5 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 70 61   stored as.** pa
20cc6 72 74 20 6f 66 20 74 68 65 20 69 6e 2d 6d 65 6d  rt of the in-mem
20cc7 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74 69  ory representati
20cc8 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  on of the databa
20cc9 73 65 20 73 63 68 65 6d 61 2e 0a 2a 2f 0a 53 51  se schema..*/.SQ
20cca 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
20ccb 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 44 75  r *sqlite3ExprDu
20ccc 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45  p(sqlite3 *db, E
20ccd 78 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67  xpr *p, int flag
20cce 73 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70  s){.  return exp
20ccf 72 44 75 70 28 64 62 2c 20 70 2c 20 66 6c 61 67  rDup(db, p, flag
20cd0 73 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  s, 0);.}.SQLITE_
20cd1 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74  PRIVATE ExprList
20cd2 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73   *sqlite3ExprLis
20cd3 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62  tDup(sqlite3 *db
20cd4 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 2c 20 69  , ExprList *p, i
20cd5 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 45 78 70  nt flags){.  Exp
20cd6 72 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20 20 73  rList *pNew;.  s
20cd7 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
20cd8 74 65 6d 20 2a 70 49 74 65 6d 2c 20 2a 70 4f 6c  tem *pItem, *pOl
20cd9 64 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a  dItem;.  int i;.
20cda 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
20cdb 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20 3d 20  urn 0;.  pNew = 
20cdc 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
20cdd 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70  aw(db, sizeof(*p
20cde 4e 65 77 29 20 29 3b 0a 20 20 69 66 28 20 70 4e  New) );.  if( pN
20cdf 65 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  ew==0 ) return 0
20ce0 3b 0a 20 20 70 4e 65 77 2d 3e 69 45 43 75 72 73  ;.  pNew->iECurs
20ce1 6f 72 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e  or = 0;.  pNew->
20ce2 6e 45 78 70 72 20 3d 20 70 4e 65 77 2d 3e 6e 41  nExpr = pNew->nA
20ce3 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 45 78 70 72 3b  lloc = p->nExpr;
20ce4 0a 20 20 70 4e 65 77 2d 3e 61 20 3d 20 70 49 74  .  pNew->a = pIt
20ce5 65 6d 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  em = sqlite3DbMa
20ce6 6c 6c 6f 63 52 61 77 28 64 62 2c 20 20 70 2d 3e  llocRaw(db,  p->
20ce7 6e 45 78 70 72 2a 73 69 7a 65 6f 66 28 70 2d 3e  nExpr*sizeof(p->
20ce8 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 70  a[0]) );.  if( p
20ce9 49 74 65 6d 3d 3d 30 20 29 7b 0a 20 20 20 20 73  Item==0 ){.    s
20cea 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
20ceb 20 70 4e 65 77 29 3b 0a 20 20 20 20 72 65 74 75   pNew);.    retu
20cec 72 6e 20 30 3b 0a 20 20 7d 20 0a 20 20 70 4f 6c  rn 0;.  } .  pOl
20ced 64 49 74 65 6d 20 3d 20 70 2d 3e 61 3b 0a 20 20  dItem = p->a;.  
20cee 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 45  for(i=0; i<p->nE
20cef 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b  xpr; i++, pItem+
20cf0 2b 2c 20 70 4f 6c 64 49 74 65 6d 2b 2b 29 7b 0a  +, pOldItem++){.
20cf1 20 20 20 20 45 78 70 72 20 2a 70 4f 6c 64 45 78      Expr *pOldEx
20cf2 70 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 70  pr = pOldItem->p
20cf3 45 78 70 72 3b 0a 20 20 20 20 70 49 74 65 6d 2d  Expr;.    pItem-
20cf4 3e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  >pExpr = sqlite3
20cf5 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f 6c 64  ExprDup(db, pOld
20cf6 45 78 70 72 2c 20 66 6c 61 67 73 29 3b 0a 20 20  Expr, flags);.  
20cf7 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d    pItem->zName =
20cf8 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
20cf9 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a  (db, pOldItem->z
20cfa 4e 61 6d 65 29 3b 0a 20 20 20 20 70 49 74 65 6d  Name);.    pItem
20cfb 2d 3e 7a 53 70 61 6e 20 3d 20 73 71 6c 69 74 65  ->zSpan = sqlite
20cfc 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f  3DbStrDup(db, pO
20cfd 6c 64 49 74 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a  ldItem->zSpan);.
20cfe 20 20 20 20 70 49 74 65 6d 2d 3e 73 6f 72 74 4f      pItem->sortO
20cff 72 64 65 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d  rder = pOldItem-
20d00 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20  >sortOrder;.    
20d01 70 49 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 30 3b  pItem->done = 0;
20d02 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c  .    pItem->iCol
20d03 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 6f   = pOldItem->iCo
20d04 6c 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 41  l;.    pItem->iA
20d05 6c 69 61 73 20 3d 20 70 4f 6c 64 49 74 65 6d 2d  lias = pOldItem-
20d06 3e 69 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 72  >iAlias;.  }.  r
20d07 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f  eturn pNew;.}../
20d08 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 73 2c  *.** If cursors,
20d09 20 74 72 69 67 67 65 72 73 2c 20 76 69 65 77 73   triggers, views
20d0a 20 61 6e 64 20 73 75 62 71 75 65 72 69 65 73 20   and subqueries 
20d0b 61 72 65 20 61 6c 6c 20 6f 6d 69 74 74 65 64 20  are all omitted 
20d0c 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 62 75 69 6c  from.** the buil
20d0d 64 2c 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20  d, then none of 
20d0e 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  the following ro
20d0f 75 74 69 6e 65 73 2c 20 65 78 63 65 70 74 20 66  utines, except f
20d10 6f 72 20 0a 2a 2a 20 73 71 6c 69 74 65 33 53 65  or .** sqlite3Se
20d11 6c 65 63 74 44 75 70 28 29 2c 20 63 61 6e 20 62  lectDup(), can b
20d12 65 20 63 61 6c 6c 65 64 2e 20 73 71 6c 69 74 65  e called. sqlite
20d13 33 53 65 6c 65 63 74 44 75 70 28 29 20 69 73 20  3SelectDup() is 
20d14 73 6f 6d 65 74 69 6d 65 73 0a 2a 2a 20 63 61 6c  sometimes.** cal
20d15 6c 65 64 20 77 69 74 68 20 61 20 4e 55 4c 4c 20  led with a NULL 
20d16 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 69 66  argument..*/.#if
20d17 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
20d18 5f 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21  _OMIT_VIEW) || !
20d19 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
20d1a 4d 49 54 5f 54 52 49 47 47 45 52 29 20 5c 0a 20  MIT_TRIGGER) \. 
20d1b 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  || !defined(SQLI
20d1c 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
20d1d 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ).SQLITE_PRIVATE
20d1e 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65   SrcList *sqlite
20d1f 33 53 72 63 4c 69 73 74 44 75 70 28 73 71 6c 69  3SrcListDup(sqli
20d20 74 65 33 20 2a 64 62 2c 20 53 72 63 4c 69 73 74  te3 *db, SrcList
20d21 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b   *p, int flags){
20d22 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 4e 65 77  .  SrcList *pNew
20d23 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  ;.  int i;.  int
20d24 20 6e 42 79 74 65 3b 0a 20 20 69 66 28 20 70 3d   nByte;.  if( p=
20d25 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
20d26 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28   nByte = sizeof(
20d27 2a 70 29 20 2b 20 28 70 2d 3e 6e 53 72 63 3e 30  *p) + (p->nSrc>0
20d28 20 3f 20 73 69 7a 65 6f 66 28 70 2d 3e 61 5b 30   ? sizeof(p->a[0
20d29 5d 29 20 2a 20 28 70 2d 3e 6e 53 72 63 2d 31 29  ]) * (p->nSrc-1)
20d2a 20 3a 20 30 29 3b 0a 20 20 70 4e 65 77 20 3d 20   : 0);.  pNew = 
20d2b 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
20d2c 61 77 28 64 62 2c 20 6e 42 79 74 65 20 29 3b 0a  aw(db, nByte );.
20d2d 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20    if( pNew==0 ) 
20d2e 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77  return 0;.  pNew
20d2f 2d 3e 6e 53 72 63 20 3d 20 70 4e 65 77 2d 3e 6e  ->nSrc = pNew->n
20d30 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 53 72 63 3b  Alloc = p->nSrc;
20d31 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d  .  for(i=0; i<p-
20d32 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nSrc; i++){.   
20d33 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
20d34 69 74 65 6d 20 2a 70 4e 65 77 49 74 65 6d 20 3d  item *pNewItem =
20d35 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b 0a 20 20   &pNew->a[i];.  
20d36 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
20d37 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74 65 6d 20  _item *pOldItem 
20d38 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20  = &p->a[i];.    
20d39 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20  Table *pTab;.   
20d3a 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 44 61 74 61   pNewItem->zData
20d3b 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33 44 62  base = sqlite3Db
20d3c 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49  StrDup(db, pOldI
20d3d 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b  tem->zDatabase);
20d3e 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a  .    pNewItem->z
20d3f 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62  Name = sqlite3Db
20d40 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49  StrDup(db, pOldI
20d41 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  tem->zName);.   
20d42 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 41 6c 69 61   pNewItem->zAlia
20d43 73 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  s = sqlite3DbStr
20d44 44 75 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d  Dup(db, pOldItem
20d45 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 20 20 70  ->zAlias);.    p
20d46 4e 65 77 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70  NewItem->jointyp
20d47 65 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 6a 6f  e = pOldItem->jo
20d48 69 6e 74 79 70 65 3b 0a 20 20 20 20 70 4e 65 77  intype;.    pNew
20d49 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20 3d 20  Item->iCursor = 
20d4a 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 75 72 73 6f  pOldItem->iCurso
20d4b 72 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d  r;.    pNewItem-
20d4c 3e 69 73 50 6f 70 75 6c 61 74 65 64 20 3d 20 70  >isPopulated = p
20d4d 4f 6c 64 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c  OldItem->isPopul
20d4e 61 74 65 64 3b 0a 20 20 20 20 70 4e 65 77 49 74  ated;.    pNewIt
20d4f 65 6d 2d 3e 7a 49 6e 64 65 78 20 3d 20 73 71 6c  em->zIndex = sql
20d50 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
20d51 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 49 6e 64 65   pOldItem->zInde
20d52 78 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d  x);.    pNewItem
20d53 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 20 3d 20 70  ->notIndexed = p
20d54 4f 6c 64 49 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65  OldItem->notInde
20d55 78 65 64 3b 0a 20 20 20 20 70 4e 65 77 49 74 65  xed;.    pNewIte
20d56 6d 2d 3e 70 49 6e 64 65 78 20 3d 20 70 4f 6c 64  m->pIndex = pOld
20d57 49 74 65 6d 2d 3e 70 49 6e 64 65 78 3b 0a 20 20  Item->pIndex;.  
20d58 20 20 70 54 61 62 20 3d 20 70 4e 65 77 49 74 65    pTab = pNewIte
20d59 6d 2d 3e 70 54 61 62 20 3d 20 70 4f 6c 64 49 74  m->pTab = pOldIt
20d5a 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69 66  em->pTab;.    if
20d5b 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20  ( pTab ){.      
20d5c 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20  pTab->nRef++;.  
20d5d 20 20 7d 0a 20 20 20 20 70 4e 65 77 49 74 65 6d    }.    pNewItem
20d5e 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69  ->pSelect = sqli
20d5f 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c  te3SelectDup(db,
20d60 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 53 65 6c 65   pOldItem->pSele
20d61 63 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20  ct, flags);.    
20d62 70 4e 65 77 49 74 65 6d 2d 3e 70 4f 6e 20 3d 20  pNewItem->pOn = 
20d63 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
20d64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 4f 6e  b, pOldItem->pOn
20d65 2c 20 66 6c 61 67 73 29 3b 0a 20 20 20 20 70 4e  , flags);.    pN
20d66 65 77 49 74 65 6d 2d 3e 70 55 73 69 6e 67 20 3d  ewItem->pUsing =
20d67 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75   sqlite3IdListDu
20d68 70 28 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e  p(db, pOldItem->
20d69 70 55 73 69 6e 67 29 3b 0a 20 20 20 20 70 4e 65  pUsing);.    pNe
20d6a 77 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 20 3d  wItem->colUsed =
20d6b 20 70 4f 6c 64 49 74 65 6d 2d 3e 63 6f 6c 55 73   pOldItem->colUs
20d6c 65 64 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ed;.  }.  return
20d6d 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f   pNew;.}.SQLITE_
20d6e 50 52 49 56 41 54 45 20 49 64 4c 69 73 74 20 2a  PRIVATE IdList *
20d6f 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70  sqlite3IdListDup
20d70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 49 64  (sqlite3 *db, Id
20d71 4c 69 73 74 20 2a 70 29 7b 0a 20 20 49 64 4c 69  List *p){.  IdLi
20d72 73 74 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20  st *pNew;.  int 
20d73 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  i;.  if( p==0 ) 
20d74 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77  return 0;.  pNew
20d75 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
20d76 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66  ocRaw(db, sizeof
20d77 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20 69 66 28  (*pNew) );.  if(
20d78 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74 75 72   pNew==0 ) retur
20d79 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 6e 49 64  n 0;.  pNew->nId
20d7a 20 3d 20 70 4e 65 77 2d 3e 6e 41 6c 6c 6f 63 20   = pNew->nAlloc 
20d7b 3d 20 70 2d 3e 6e 49 64 3b 0a 20 20 70 4e 65 77  = p->nId;.  pNew
20d7c 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  ->a = sqlite3DbM
20d7d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 70 2d 3e  allocRaw(db, p->
20d7e 6e 49 64 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 5b  nId*sizeof(p->a[
20d7f 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65  0]) );.  if( pNe
20d80 77 2d 3e 61 3d 3d 30 20 29 7b 0a 20 20 20 20 73  w->a==0 ){.    s
20d81 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
20d82 20 70 4e 65 77 29 3b 0a 20 20 20 20 72 65 74 75   pNew);.    retu
20d83 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28  rn 0;.  }.  for(
20d84 69 3d 30 3b 20 69 3c 70 2d 3e 6e 49 64 3b 20 69  i=0; i<p->nId; i
20d85 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  ++){.    struct 
20d86 49 64 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4e 65  IdList_item *pNe
20d87 77 49 74 65 6d 20 3d 20 26 70 4e 65 77 2d 3e 61  wItem = &pNew->a
20d88 5b 69 5d 3b 0a 20 20 20 20 73 74 72 75 63 74 20  [i];.    struct 
20d89 49 64 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4f 6c  IdList_item *pOl
20d8a 64 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b 69 5d  dItem = &p->a[i]
20d8b 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e  ;.    pNewItem->
20d8c 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44  zName = sqlite3D
20d8d 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64  bStrDup(db, pOld
20d8e 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  Item->zName);.  
20d8f 20 20 70 4e 65 77 49 74 65 6d 2d 3e 69 64 78 20    pNewItem->idx 
20d90 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 64 78 3b  = pOldItem->idx;
20d91 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e  .  }.  return pN
20d92 65 77 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  ew;.}.SQLITE_PRI
20d93 56 41 54 45 20 53 65 6c 65 63 74 20 2a 73 71 6c  VATE Select *sql
20d94 69 74 65 33 53 65 6c 65 63 74 44 75 70 28 73 71  ite3SelectDup(sq
20d95 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63  lite3 *db, Selec
20d96 74 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73 29  t *p, int flags)
20d97 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 77  {.  Select *pNew
20d98 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72  ;.  if( p==0 ) r
20d99 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 20  eturn 0;.  pNew 
20d9a 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
20d9b 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28  cRaw(db, sizeof(
20d9c 2a 70 29 20 29 3b 0a 20 20 69 66 28 20 70 4e 65  *p) );.  if( pNe
20d9d 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b  w==0 ) return 0;
20d9e 0a 20 20 70 4e 65 77 2d 3e 70 45 4c 69 73 74 20  .  pNew->pEList 
20d9f 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
20da0 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 45 4c 69  tDup(db, p->pELi
20da1 73 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e  st, flags);.  pN
20da2 65 77 2d 3e 70 53 72 63 20 3d 20 73 71 6c 69 74  ew->pSrc = sqlit
20da3 65 33 53 72 63 4c 69 73 74 44 75 70 28 64 62 2c  e3SrcListDup(db,
20da4 20 70 2d 3e 70 53 72 63 2c 20 66 6c 61 67 73 29   p->pSrc, flags)
20da5 3b 0a 20 20 70 4e 65 77 2d 3e 70 57 68 65 72 65  ;.  pNew->pWhere
20da6 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
20da7 70 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 2c  p(db, p->pWhere,
20da8 20 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d   flags);.  pNew-
20da9 3e 70 47 72 6f 75 70 42 79 20 3d 20 73 71 6c 69  >pGroupBy = sqli
20daa 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64  te3ExprListDup(d
20dab 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79 2c 20  b, p->pGroupBy, 
20dac 66 6c 61 67 73 29 3b 0a 20 20 70 4e 65 77 2d 3e  flags);.  pNew->
20dad 70 48 61 76 69 6e 67 20 3d 20 73 71 6c 69 74 65  pHaving = sqlite
20dae 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e  3ExprDup(db, p->
20daf 70 48 61 76 69 6e 67 2c 20 66 6c 61 67 73 29 3b  pHaving, flags);
20db0 0a 20 20 70 4e 65 77 2d 3e 70 4f 72 64 65 72 42  .  pNew->pOrderB
20db1 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  y = sqlite3ExprL
20db2 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 4f  istDup(db, p->pO
20db3 72 64 65 72 42 79 2c 20 66 6c 61 67 73 29 3b 0a  rderBy, flags);.
20db4 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 70 2d 3e    pNew->op = p->
20db5 6f 70 3b 0a 20 20 70 4e 65 77 2d 3e 70 50 72 69  op;.  pNew->pPri
20db6 6f 72 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  or = sqlite3Sele
20db7 63 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 50 72  ctDup(db, p->pPr
20db8 69 6f 72 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70  ior, flags);.  p
20db9 4e 65 77 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 71  New->pLimit = sq
20dba 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
20dbb 20 70 2d 3e 70 4c 69 6d 69 74 2c 20 66 6c 61 67   p->pLimit, flag
20dbc 73 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f 66 66  s);.  pNew->pOff
20dbd 73 65 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  set = sqlite3Exp
20dbe 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4f 66 66  rDup(db, p->pOff
20dbf 73 65 74 2c 20 66 6c 61 67 73 29 3b 0a 20 20 70  set, flags);.  p
20dc0 4e 65 77 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b  New->iLimit = 0;
20dc1 0a 20 20 70 4e 65 77 2d 3e 69 4f 66 66 73 65 74  .  pNew->iOffset
20dc2 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 73 65   = 0;.  pNew->se
20dc3 6c 46 6c 61 67 73 20 3d 20 70 2d 3e 73 65 6c 46  lFlags = p->selF
20dc4 6c 61 67 73 20 26 20 7e 53 46 5f 55 73 65 73 45  lags & ~SF_UsesE
20dc5 70 68 65 6d 65 72 61 6c 3b 0a 20 20 70 4e 65 77  phemeral;.  pNew
20dc6 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 30  ->pRightmost = 0
20dc7 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70  ;.  pNew->addrOp
20dc8 65 6e 45 70 68 6d 5b 30 5d 20 3d 20 2d 31 3b 0a  enEphm[0] = -1;.
20dc9 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e    pNew->addrOpen
20dca 45 70 68 6d 5b 31 5d 20 3d 20 2d 31 3b 0a 20 20  Ephm[1] = -1;.  
20dcb 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70  pNew->addrOpenEp
20dcc 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a 20 20 72 65  hm[2] = -1;.  re
20dcd 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 23 65 6c  turn pNew;.}.#el
20dce 73 65 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  se.SQLITE_PRIVAT
20dcf 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65  E Select *sqlite
20dd0 33 53 65 6c 65 63 74 44 75 70 28 73 71 6c 69 74  3SelectDup(sqlit
20dd1 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a  e3 *db, Select *
20dd2 70 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20  p, int flags){. 
20dd3 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 29 3b   assert( p==0 );
20dd4 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23  .  return 0;.}.#
20dd5 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64  endif.../*.** Ad
20dd6 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20  d a new element 
20dd7 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 61 6e  to the end of an
20dd8 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
20dd9 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73 0a 2a  .  If pList is.*
20dda 2a 20 69 6e 69 74 69 61 6c 6c 79 20 4e 55 4c 4c  * initially NULL
20ddb 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 20  , then create a 
20ddc 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6c  new expression l
20ddd 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  ist..**.** If a 
20dde 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
20ddf 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  n error occurs, 
20de0 74 68 65 20 65 6e 74 69 72 65 20 6c 69 73 74 20  the entire list 
20de1 69 73 20 66 72 65 65 64 20 61 6e 64 0a 2a 2a 20  is freed and.** 
20de2 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64  NULL is returned
20de3 2e 20 20 49 66 20 6e 6f 6e 2d 4e 55 4c 4c 20 69  .  If non-NULL i
20de4 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e  s returned, then
20de5 20 69 74 20 69 73 20 67 75 61 72 61 6e 74 65 65   it is guarantee
20de6 64 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 6e 65  d.** that the ne
20de7 77 20 65 6e 74 72 79 20 77 61 73 20 73 75 63 63  w entry was succ
20de8 65 73 73 66 75 6c 6c 79 20 61 70 70 65 6e 64 65  essfully appende
20de9 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
20dea 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73  VATE ExprList *s
20deb 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
20dec 70 65 6e 64 28 0a 20 20 50 61 72 73 65 20 2a 70  pend(.  Parse *p
20ded 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
20dee 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
20def 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  xt */.  ExprList
20df0 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 20 20   *pList,        
20df1 2f 2a 20 4c 69 73 74 20 74 6f 20 77 68 69 63 68  /* List to which
20df2 20 74 6f 20 61 70 70 65 6e 64 2e 20 4d 69 67 68   to append. Migh
20df3 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20 45  t be NULL */.  E
20df4 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20 20  xpr *pExpr      
20df5 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73         /* Expres
20df6 73 69 6f 6e 20 74 6f 20 62 65 20 61 70 70 65 6e  sion to be appen
20df7 64 65 64 2e 20 4d 69 67 68 74 20 62 65 20 4e 55  ded. Might be NU
20df8 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  LL */.){.  sqlit
20df9 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
20dfa 3e 64 62 3b 0a 20 20 69 66 28 20 70 4c 69 73 74  >db;.  if( pList
20dfb 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69 73 74  ==0 ){.    pList
20dfc 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
20dfd 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
20dfe 66 28 45 78 70 72 4c 69 73 74 29 20 29 3b 0a 20  f(ExprList) );. 
20dff 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20     if( pList==0 
20e00 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f  ){.      goto no
20e01 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _mem;.    }.    
20e02 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e  assert( pList->n
20e03 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20 7d 0a  Alloc==0 );.  }.
20e04 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e 41 6c    if( pList->nAl
20e05 6c 6f 63 3c 3d 70 4c 69 73 74 2d 3e 6e 45 78 70  loc<=pList->nExp
20e06 72 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  r ){.    struct 
20e07 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 61  ExprList_item *a
20e08 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70 4c  ;.    int n = pL
20e09 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b 20  ist->nAlloc*2 + 
20e0a 34 3b 0a 20 20 20 20 61 20 3d 20 73 71 6c 69 74  4;.    a = sqlit
20e0b 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20  e3DbRealloc(db, 
20e0c 70 4c 69 73 74 2d 3e 61 2c 20 6e 2a 73 69 7a 65  pList->a, n*size
20e0d 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30 5d 29 29  of(pList->a[0]))
20e0e 3b 0a 20 20 20 20 69 66 28 20 61 3d 3d 30 20 29  ;.    if( a==0 )
20e0f 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f  {.      goto no_
20e10 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  mem;.    }.    p
20e11 4c 69 73 74 2d 3e 61 20 3d 20 61 3b 0a 20 20 20  List->a = a;.   
20e12 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 3d   pList->nAlloc =
20e13 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
20e14 53 69 7a 65 28 64 62 2c 20 61 29 2f 73 69 7a 65  Size(db, a)/size
20e15 6f 66 28 61 5b 30 5d 29 3b 0a 20 20 7d 0a 20 20  of(a[0]);.  }.  
20e16 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 61  assert( pList->a
20e17 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 31 20 29  !=0 );.  if( 1 )
20e18 7b 0a 20 20 20 20 73 74 72 75 63 74 20 45 78 70  {.    struct Exp
20e19 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65  rList_item *pIte
20e1a 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c  m = &pList->a[pL
20e1b 69 73 74 2d 3e 6e 45 78 70 72 2b 2b 5d 3b 0a 20  ist->nExpr++];. 
20e1c 20 20 20 6d 65 6d 73 65 74 28 70 49 74 65 6d 2c     memset(pItem,
20e1d 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 49 74 65   0, sizeof(*pIte
20e1e 6d 29 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e  m));.    pItem->
20e1f 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0a 20  pExpr = pExpr;. 
20e20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4c 69 73   }.  return pLis
20e21 74 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 20 20 20 20 20  t;..no_mem:     
20e22 0a 20 20 2f 2a 20 41 76 6f 69 64 20 6c 65 61 6b  .  /* Avoid leak
20e23 69 6e 67 20 6d 65 6d 6f 72 79 20 69 66 20 6d 61  ing memory if ma
20e24 6c 6c 6f 63 20 68 61 73 20 66 61 69 6c 65 64 2e  lloc has failed.
20e25 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78 70   */.  sqlite3Exp
20e26 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45 78 70  rDelete(db, pExp
20e27 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  r);.  sqlite3Exp
20e28 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
20e29 70 4c 69 73 74 29 3b 0a 20 20 72 65 74 75 72 6e  pList);.  return
20e2a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74   0;.}../*.** Set
20e2b 20 74 68 65 20 45 78 70 72 4c 69 73 74 2e 61 5b   the ExprList.a[
20e2c 5d 2e 7a 4e 61 6d 65 20 65 6c 65 6d 65 6e 74 20  ].zName element 
20e2d 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  of the most rece
20e2e 6e 74 6c 79 20 61 64 64 65 64 20 69 74 65 6d 0a  ntly added item.
20e2f 2a 2a 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73  ** on the expres
20e30 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a  sion list..**.**
20e31 20 70 4c 69 73 74 20 6d 69 67 68 74 20 62 65 20   pList might be 
20e32 4e 55 4c 4c 20 66 6f 6c 6c 6f 77 69 6e 67 20 61  NULL following a
20e33 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20 20 42 75  n OOM error.  Bu
20e34 74 20 70 4e 61 6d 65 20 73 68 6f 75 6c 64 20 6e  t pName should n
20e35 65 76 65 72 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2e  ever be.** NULL.
20e36 20 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c    If a memory al
20e37 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c 20  location fails, 
20e38 74 68 65 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  the pParse->db->
20e39 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61  mallocFailed fla
20e3a 67 0a 2a 2a 20 69 73 20 73 65 74 2e 0a 2a 2f 0a  g.** is set..*/.
20e3b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
20e3c 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 4c  oid sqlite3ExprL
20e3d 69 73 74 53 65 74 4e 61 6d 65 28 0a 20 20 50 61  istSetName(.  Pa
20e3e 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
20e3f 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
20e40 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78   context */.  Ex
20e41 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20  prList *pList,  
20e42 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f        /* List to
20e43 20 77 68 69 63 68 20 74 6f 20 61 64 64 20 74 68   which to add th
20e44 65 20 73 70 61 6e 2e 20 2a 2f 0a 20 20 54 6f 6b  e span. */.  Tok
20e45 65 6e 20 2a 70 4e 61 6d 65 2c 20 20 20 20 20 20  en *pName,      
20e46 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 74 6f 20       /* Name to 
20e47 62 65 20 61 64 64 65 64 20 2a 2f 0a 20 20 69 6e  be added */.  in
20e48 74 20 64 65 71 75 6f 74 65 20 20 20 20 20 20 20  t dequote       
20e49 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f        /* True to
20e4a 20 63 61 75 73 65 20 74 68 65 20 6e 61 6d 65 20   cause the name 
20e4b 74 6f 20 62 65 20 64 65 71 75 6f 74 65 64 20 2a  to be dequoted *
20e4c 2f 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  /.){.  assert( p
20e4d 4c 69 73 74 21 3d 30 20 7c 7c 20 70 50 61 72 73  List!=0 || pPars
20e4e 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
20e4f 6c 65 64 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  led!=0 );.  if( 
20e50 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 73 74 72  pList ){.    str
20e51 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
20e52 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 61 73  m *pItem;.    as
20e53 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45 78  sert( pList->nEx
20e54 70 72 3e 30 20 29 3b 0a 20 20 20 20 70 49 74 65  pr>0 );.    pIte
20e55 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c  m = &pList->a[pL
20e56 69 73 74 2d 3e 6e 45 78 70 72 2d 31 5d 3b 0a 20  ist->nExpr-1];. 
20e57 20 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d     assert( pItem
20e58 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 29 3b 0a 20 20  ->zName==0 );.  
20e59 20 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d    pItem->zName =
20e5a 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75   sqlite3DbStrNDu
20e5b 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4e  p(pParse->db, pN
20e5c 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e  ame->z, pName->n
20e5d 29 3b 0a 20 20 20 20 69 66 28 20 64 65 71 75 6f  );.    if( dequo
20e5e 74 65 20 26 26 20 70 49 74 65 6d 2d 3e 7a 4e 61  te && pItem->zNa
20e5f 6d 65 20 29 20 73 71 6c 69 74 65 33 44 65 71 75  me ) sqlite3Dequ
20e60 6f 74 65 28 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65  ote(pItem->zName
20e61 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
20e62 53 65 74 20 74 68 65 20 45 78 70 72 4c 69 73 74  Set the ExprList
20e63 2e 61 5b 5d 2e 7a 53 70 61 6e 20 65 6c 65 6d 65  .a[].zSpan eleme
20e64 6e 74 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72  nt of the most r
20e65 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 69 74  ecently added it
20e66 65 6d 0a 2a 2a 20 6f 6e 20 74 68 65 20 65 78 70  em.** on the exp
20e67 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a  ression list..**
20e68 0a 2a 2a 20 70 4c 69 73 74 20 6d 69 67 68 74 20  .** pList might 
20e69 62 65 20 4e 55 4c 4c 20 66 6f 6c 6c 6f 77 69 6e  be NULL followin
20e6a 67 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e 20  g an OOM error. 
20e6b 20 42 75 74 20 70 53 70 61 6e 20 73 68 6f 75 6c   But pSpan shoul
20e6c 64 20 6e 65 76 65 72 20 62 65 0a 2a 2a 20 4e 55  d never be.** NU
20e6d 4c 4c 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72 79  LL.  If a memory
20e6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c   allocation fail
20e6f 73 2c 20 74 68 65 20 70 50 61 72 73 65 2d 3e 64  s, the pParse->d
20e70 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
20e71 66 6c 61 67 0a 2a 2a 20 69 73 20 73 65 74 2e 0a  flag.** is set..
20e72 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
20e73 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
20e74 70 72 4c 69 73 74 53 65 74 53 70 61 6e 28 0a 20  prListSetSpan(. 
20e75 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
20e76 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
20e77 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
20e78 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
20e79 2c 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74  ,        /* List
20e7a 20 74 6f 20 77 68 69 63 68 20 74 6f 20 61 64 64   to which to add
20e7b 20 74 68 65 20 73 70 61 6e 2e 20 2a 2f 0a 20 20   the span. */.  
20e7c 45 78 70 72 53 70 61 6e 20 2a 70 53 70 61 6e 20  ExprSpan *pSpan 
20e7d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73          /* The s
20e7e 70 61 6e 20 74 6f 20 62 65 20 61 64 64 65 64 20  pan to be added 
20e7f 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  */.){.  sqlite3 
20e80 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
20e81 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73  ;.  assert( pLis
20e82 74 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  t!=0 || db->mall
20e83 6f 63 46 61 69 6c 65 64 21 3d 30 20 29 3b 0a 20  ocFailed!=0 );. 
20e84 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20   if( pList ){.  
20e85 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
20e86 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20  t_item *pItem = 
20e87 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73 74 2d  &pList->a[pList-
20e88 3e 6e 45 78 70 72 2d 31 5d 3b 0a 20 20 20 20 61  >nExpr-1];.    a
20e89 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45  ssert( pList->nE
20e8a 78 70 72 3e 30 20 29 3b 0a 20 20 20 20 61 73 73  xpr>0 );.    ass
20e8b 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ert( db->mallocF
20e8c 61 69 6c 65 64 20 7c 7c 20 70 49 74 65 6d 2d 3e  ailed || pItem->
20e8d 70 45 78 70 72 3d 3d 70 53 70 61 6e 2d 3e 70 45  pExpr==pSpan->pE
20e8e 78 70 72 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  xpr );.    sqlit
20e8f 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 74  e3DbFree(db, pIt
20e90 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20 20 20  em->zSpan);.    
20e91 70 49 74 65 6d 2d 3e 7a 53 70 61 6e 20 3d 20 73  pItem->zSpan = s
20e92 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
20e93 64 62 2c 20 28 63 68 61 72 2a 29 70 53 70 61 6e  db, (char*)pSpan
20e94 2d 3e 7a 53 74 61 72 74 2c 0a 20 20 20 20 20 20  ->zStart,.      
20e95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20e96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 69                (i
20e97 6e 74 29 28 70 53 70 61 6e 2d 3e 7a 45 6e 64 20  nt)(pSpan->zEnd 
20e98 2d 20 70 53 70 61 6e 2d 3e 7a 53 74 61 72 74 29  - pSpan->zStart)
20e99 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
20e9a 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  If the expressio
20e9b 6e 20 6c 69 73 74 20 70 45 4c 69 73 74 20 63 6f  n list pEList co
20e9c 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 61 6e  ntains more than
20e9d 20 69 4c 69 6d 69 74 20 65 6c 65 6d 65 6e 74 73   iLimit elements
20e9e 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 6e 20 65 72  ,.** leave an er
20e9f 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70  ror message in p
20ea0 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  Parse..*/.SQLITE
20ea1 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
20ea2 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 68 65  lite3ExprListChe
20ea3 63 6b 4c 65 6e 67 74 68 28 0a 20 20 50 61 72 73  ckLength(.  Pars
20ea4 65 20 2a 70 50 61 72 73 65 2c 0a 20 20 45 78 70  e *pParse,.  Exp
20ea5 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 0a 20  rList *pEList,. 
20ea6 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 62   const char *zOb
20ea7 6a 65 63 74 0a 29 7b 0a 20 20 69 6e 74 20 6d 78  ject.){.  int mx
20ea8 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61   = pParse->db->a
20ea9 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
20eaa 49 54 5f 43 4f 4c 55 4d 4e 5d 3b 0a 20 20 74 65  IT_COLUMN];.  te
20eab 73 74 63 61 73 65 28 20 70 45 4c 69 73 74 20 26  stcase( pEList &
20eac 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d  & pEList->nExpr=
20ead 3d 6d 78 20 29 3b 0a 20 20 74 65 73 74 63 61 73  =mx );.  testcas
20eae 65 28 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c  e( pEList && pEL
20eaf 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 2b 31  ist->nExpr==mx+1
20eb0 20 29 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74   );.  if( pEList
20eb1 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70   && pEList->nExp
20eb2 72 3e 6d 78 20 29 7b 0a 20 20 20 20 73 71 6c 69  r>mx ){.    sqli
20eb3 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
20eb4 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f  se, "too many co
20eb5 6c 75 6d 6e 73 20 69 6e 20 25 73 22 2c 20 7a 4f  lumns in %s", zO
20eb6 62 6a 65 63 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  bject);.  }.}../
20eb7 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65  *.** Delete an e
20eb8 6e 74 69 72 65 20 65 78 70 72 65 73 73 69 6f 6e  ntire expression
20eb9 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45   list..*/.SQLITE
20eba 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
20ebb 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
20ebc 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ete(sqlite3 *db,
20ebd 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
20ebe 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  ){.  int i;.  st
20ebf 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
20ec0 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28  em *pItem;.  if(
20ec1 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75   pList==0 ) retu
20ec2 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c  rn;.  assert( pL
20ec3 69 73 74 2d 3e 61 21 3d 30 20 7c 7c 20 28 70 4c  ist->a!=0 || (pL
20ec4 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 30 20 26 26  ist->nExpr==0 &&
20ec5 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d   pList->nAlloc==
20ec6 30 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  0) );.  assert( 
20ec7 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3c 3d 70 4c  pList->nExpr<=pL
20ec8 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20  ist->nAlloc );. 
20ec9 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74   for(pItem=pList
20eca 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73  ->a, i=0; i<pLis
20ecb 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70  t->nExpr; i++, p
20ecc 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  Item++){.    sql
20ecd 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
20ece 62 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29  b, pItem->pExpr)
20ecf 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
20ed0 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a  ree(db, pItem->z
20ed1 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  Name);.    sqlit
20ed2 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49 74  e3DbFree(db, pIt
20ed3 65 6d 2d 3e 7a 53 70 61 6e 29 3b 0a 20 20 7d 0a  em->zSpan);.  }.
20ed4 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
20ed5 64 62 2c 20 70 4c 69 73 74 2d 3e 61 29 3b 0a 20  db, pList->a);. 
20ed6 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
20ed7 62 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a  b, pList);.}../*
20ed8 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
20ed9 65 73 20 61 72 65 20 57 61 6c 6b 65 72 20 63 61  es are Walker ca
20eda 6c 6c 62 61 63 6b 73 2e 20 20 57 61 6c 6b 65 72  llbacks.  Walker
20edb 2e 75 2e 70 69 20 69 73 20 61 20 70 6f 69 6e 74  .u.pi is a point
20edc 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 69 6e 74 65  er.** to an inte
20edd 67 65 72 2e 20 20 54 68 65 73 65 20 72 6f 75 74  ger.  These rout
20ede 69 6e 65 73 20 61 72 65 20 63 68 65 63 6b 69 6e  ines are checkin
20edf 67 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  g an expression 
20ee0 74 6f 20 73 65 65 0a 2a 2a 20 69 66 20 69 74 20  to see.** if it 
20ee1 69 73 20 61 20 63 6f 6e 73 74 61 6e 74 2e 20 20  is a constant.  
20ee2 53 65 74 20 2a 57 61 6c 6b 65 72 2e 75 2e 70 69  Set *Walker.u.pi
20ee3 20 74 6f 20 30 20 69 66 20 74 68 65 20 65 78 70   to 0 if the exp
20ee4 72 65 73 73 69 6f 6e 20 69 73 0a 2a 2a 20 6e 6f  ression is.** no
20ee5 74 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a 2a 0a 2a  t constant..**.*
20ee6 2a 20 54 68 65 73 65 20 63 61 6c 6c 62 61 63 6b  * These callback
20ee7 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73   routines are us
20ee8 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
20ee9 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a  the following:.*
20eea 2a 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  *.**     sqlite3
20eeb 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 29  ExprIsConstant()
20eec 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 45  .**     sqlite3E
20eed 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74  xprIsConstantNot
20eee 4a 6f 69 6e 28 29 0a 2a 2a 20 20 20 20 20 73 71  Join().**     sq
20eef 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
20ef0 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28 29 0a  antOrFunction().
20ef1 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  **.*/.static int
20ef2 20 65 78 70 72 4e 6f 64 65 49 73 43 6f 6e 73 74   exprNodeIsConst
20ef3 61 6e 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c  ant(Walker *pWal
20ef4 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72  ker, Expr *pExpr
20ef5 29 7b 0a 0a 20 20 2f 2a 20 49 66 20 70 57 61 6c  ){..  /* If pWal
20ef6 6b 65 72 2d 3e 75 2e 69 20 69 73 20 33 20 74 68  ker->u.i is 3 th
20ef7 65 6e 20 61 6e 79 20 74 65 72 6d 20 6f 66 20 74  en any term of t
20ef8 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  he expression th
20ef9 61 74 20 63 6f 6d 65 73 20 66 72 6f 6d 0a 20 20  at comes from.  
20efa 2a 2a 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49  ** the ON or USI
20efb 4e 47 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20  NG clauses of a 
20efc 6a 6f 69 6e 20 64 69 73 71 75 61 6c 69 66 69 65  join disqualifie
20efd 73 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  s the expression
20efe 0a 20 20 2a 2a 20 66 72 6f 6d 20 62 65 69 6e 67  .  ** from being
20eff 20 63 6f 6e 73 69 64 65 72 65 64 20 63 6f 6e 73   considered cons
20f00 74 61 6e 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70  tant. */.  if( p
20f01 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 33 20 26  Walker->u.i==3 &
20f02 26 20 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70  & ExprHasAnyProp
20f03 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 46  erty(pExpr, EP_F
20f04 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20  romJoin) ){.    
20f05 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 30  pWalker->u.i = 0
20f06 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52 43  ;.    return WRC
20f07 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a 20 20 73  _Abort;.  }..  s
20f08 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70  witch( pExpr->op
20f09 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 6e 73 69   ){.    /* Consi
20f0a 64 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f  der functions to
20f0b 20 62 65 20 63 6f 6e 73 74 61 6e 74 20 69 66 20   be constant if 
20f0c 61 6c 6c 20 74 68 65 69 72 20 61 72 67 75 6d 65  all their argume
20f0d 6e 74 73 20 61 72 65 20 63 6f 6e 73 74 61 6e 74  nts are constant
20f0e 0a 20 20 20 20 2a 2a 20 61 6e 64 20 70 57 61 6c  .    ** and pWal
20f0f 6b 65 72 2d 3e 75 2e 69 3d 3d 32 20 2a 2f 0a 20  ker->u.i==2 */. 
20f10 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 54     case TK_FUNCT
20f11 49 4f 4e 3a 0a 20 20 20 20 20 20 69 66 28 20 70  ION:.      if( p
20f12 57 61 6c 6b 65 72 2d 3e 75 2e 69 3d 3d 32 20 29  Walker->u.i==2 )
20f13 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
20f14 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68   /* Fall through
20f15 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   */.    case TK_
20f16 49 44 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  ID:.    case TK_
20f17 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65  COLUMN:.    case
20f18 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e   TK_AGG_FUNCTION
20f19 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47  :.    case TK_AG
20f1a 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 20 20  G_COLUMN:.      
20f1b 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d  testcase( pExpr-
20f1c 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 3b 0a 20 20  >op==TK_ID );.  
20f1d 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45      testcase( pE
20f1e 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55  xpr->op==TK_COLU
20f1f 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  MN );.      test
20f20 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d  case( pExpr->op=
20f21 3d 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e  =TK_AGG_FUNCTION
20f22 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
20f23 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  se( pExpr->op==T
20f24 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a  K_AGG_COLUMN );.
20f25 20 20 20 20 20 20 70 57 61 6c 6b 65 72 2d 3e 75        pWalker->u
20f26 2e 69 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65  .i = 0;.      re
20f27 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
20f28 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20      default:.   
20f29 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78     testcase( pEx
20f2a 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43  pr->op==TK_SELEC
20f2b 54 20 29 3b 20 2f 2a 20 73 65 6c 65 63 74 4e 6f  T ); /* selectNo
20f2c 64 65 49 73 43 6f 6e 73 74 61 6e 74 20 77 69 6c  deIsConstant wil
20f2d 6c 20 64 69 73 61 6c 6c 6f 77 20 2a 2f 0a 20 20  l disallow */.  
20f2e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45      testcase( pE
20f2f 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53  xpr->op==TK_EXIS
20f30 54 53 20 29 3b 20 2f 2a 20 73 65 6c 65 63 74 4e  TS ); /* selectN
20f31 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 20 77 69  odeIsConstant wi
20f32 6c 6c 20 64 69 73 61 6c 6c 6f 77 20 2a 2f 0a 20  ll disallow */. 
20f33 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
20f34 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 7d 0a 7d 0a  Continue;.  }.}.
20f35 73 74 61 74 69 63 20 69 6e 74 20 73 65 6c 65 63  static int selec
20f36 74 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 28  tNodeIsConstant(
20f37 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c  Walker *pWalker,
20f38 20 53 65 6c 65 63 74 20 2a 4e 6f 74 55 73 65 64   Select *NotUsed
20f39 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
20f3a 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
20f3b 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20 3d    pWalker->u.i =
20f3c 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 57 52 43   0;.  return WRC
20f3d 5f 41 62 6f 72 74 3b 0a 7d 0a 73 74 61 74 69 63  _Abort;.}.static
20f3e 20 69 6e 74 20 65 78 70 72 49 73 43 6f 6e 73 74   int exprIsConst
20f3f 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 69 6e  (Expr *p, int in
20f40 69 74 46 6c 61 67 29 7b 0a 20 20 57 61 6c 6b 65  itFlag){.  Walke
20f41 72 20 77 3b 0a 20 20 77 2e 75 2e 69 20 3d 20 69  r w;.  w.u.i = i
20f42 6e 69 74 46 6c 61 67 3b 0a 20 20 77 2e 78 45 78  nitFlag;.  w.xEx
20f43 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65 78 70  prCallback = exp
20f44 72 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 3b  rNodeIsConstant;
20f45 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c  .  w.xSelectCall
20f46 62 61 63 6b 20 3d 20 73 65 6c 65 63 74 4e 6f 64  back = selectNod
20f47 65 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20 73  eIsConstant;.  s
20f48 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26  qlite3WalkExpr(&
20f49 77 2c 20 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  w, p);.  return 
20f4a 77 2e 75 2e 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  w.u.i;.}../*.** 
20f4b 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73 69  Walk an expressi
20f4c 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e  on tree.  Return
20f4d 20 31 20 69 66 20 74 68 65 20 65 78 70 72 65 73   1 if the expres
20f4e 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74  sion is constant
20f4f 0a 2a 2a 20 61 6e 64 20 30 20 69 66 20 69 74 20  .** and 0 if it 
20f50 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61 62 6c  involves variabl
20f51 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e 20 63  es or function c
20f52 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  alls..**.** For 
20f53 74 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20  the purposes of 
20f54 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61  this function, a
20f55 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 64 20 73   double-quoted s
20f56 74 72 69 6e 67 20 28 65 78 3a 20 22 61 62 63 22  tring (ex: "abc"
20f57 29 0a 2a 2a 20 69 73 20 63 6f 6e 73 69 64 65 72  ).** is consider
20f58 65 64 20 61 20 76 61 72 69 61 62 6c 65 20 62 75  ed a variable bu
20f59 74 20 61 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65  t a single-quote
20f5a 64 20 73 74 72 69 6e 67 20 28 65 78 3a 20 27 61  d string (ex: 'a
20f5b 62 63 27 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e  bc') is.** a con
20f5c 73 74 61 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  stant..*/.SQLITE
20f5d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
20f5e 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61  ite3ExprIsConsta
20f5f 6e 74 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 72  nt(Expr *p){.  r
20f60 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e 73  eturn exprIsCons
20f61 74 28 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  t(p, 1);.}../*.*
20f62 2a 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73  * Walk an expres
20f63 73 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75  sion tree.  Retu
20f64 72 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 72  rn 1 if the expr
20f65 65 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61  ession is consta
20f66 6e 74 0a 2a 2a 20 74 68 61 74 20 64 6f 65 73 20  nt.** that does 
20f67 6e 6f 20 6f 72 69 67 69 6e 61 74 65 20 66 72 6f  no originate fro
20f68 6d 20 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e  m the ON or USIN
20f69 47 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20 6a  G clauses of a j
20f6a 6f 69 6e 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 30  oin..** Return 0
20f6b 20 69 66 20 69 74 20 69 6e 76 6f 6c 76 65 73 20   if it involves 
20f6c 76 61 72 69 61 62 6c 65 73 20 6f 72 20 66 75 6e  variables or fun
20f6d 63 74 69 6f 6e 20 63 61 6c 6c 73 20 6f 72 20 74  ction calls or t
20f6e 65 72 6d 73 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20  erms from.** an 
20f6f 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75  ON or USING clau
20f70 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se..*/.SQLITE_PR
20f71 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
20f72 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e  3ExprIsConstantN
20f73 6f 74 4a 6f 69 6e 28 45 78 70 72 20 2a 70 29 7b  otJoin(Expr *p){
20f74 0a 20 20 72 65 74 75 72 6e 20 65 78 70 72 49 73  .  return exprIs
20f75 43 6f 6e 73 74 28 70 2c 20 33 29 3b 0a 7d 0a 0a  Const(p, 3);.}..
20f76 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65 78  /*.** Walk an ex
20f77 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 20  pression tree.  
20f78 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20  Return 1 if the 
20f79 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 63 6f  expression is co
20f7a 6e 73 74 61 6e 74 0a 2a 2a 20 6f 72 20 61 20 66  nstant.** or a f
20f7b 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 20 77 69 74  unction call wit
20f7c 68 20 63 6f 6e 73 74 61 6e 74 20 61 72 67 75 6d  h constant argum
20f7d 65 6e 74 73 2e 20 20 52 65 74 75 72 6e 20 61 6e  ents.  Return an
20f7e 64 20 30 20 69 66 20 74 68 65 72 65 0a 2a 2a 20  d 0 if there.** 
20f7f 61 72 65 20 61 6e 79 20 76 61 72 69 61 62 6c 65  are any variable
20f80 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65  s..**.** For the
20f81 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69   purposes of thi
20f82 73 20 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64 6f  s function, a do
20f83 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69  uble-quoted stri
20f84 6e 67 20 28 65 78 3a 20 22 61 62 63 22 29 0a 2a  ng (ex: "abc").*
20f85 2a 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  * is considered 
20f86 61 20 76 61 72 69 61 62 6c 65 20 62 75 74 20 61  a variable but a
20f87 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20 73   single-quoted s
20f88 74 72 69 6e 67 20 28 65 78 3a 20 27 61 62 63 27  tring (ex: 'abc'
20f89 29 20 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 61  ) is.** a consta
20f8a 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
20f8b 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
20f8c 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f  3ExprIsConstantO
20f8d 72 46 75 6e 63 74 69 6f 6e 28 45 78 70 72 20 2a  rFunction(Expr *
20f8e 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 65 78 70  p){.  return exp
20f8f 72 49 73 43 6f 6e 73 74 28 70 2c 20 32 29 3b 0a  rIsConst(p, 2);.
20f90 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  }../*.** If the 
20f91 65 78 70 72 65 73 73 69 6f 6e 20 70 20 63 6f 64  expression p cod
20f92 65 73 20 61 20 63 6f 6e 73 74 61 6e 74 20 69 6e  es a constant in
20f93 74 65 67 65 72 20 74 68 61 74 20 69 73 20 73 6d  teger that is sm
20f94 61 6c 6c 20 65 6e 6f 75 67 68 0a 2a 2a 20 74 6f  all enough.** to
20f95 20 66 69 74 20 69 6e 20 61 20 33 32 2d 62 69 74   fit in a 32-bit
20f96 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e   integer, return
20f97 20 31 20 61 6e 64 20 70 75 74 20 74 68 65 20 76   1 and put the v
20f98 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e 74 65  alue of the inte
20f99 67 65 72 0a 2a 2a 20 69 6e 20 2a 70 56 61 6c 75  ger.** in *pValu
20f9a 65 2e 20 20 49 66 20 74 68 65 20 65 78 70 72 65  e.  If the expre
20f9b 73 73 69 6f 6e 20 69 73 20 6e 6f 74 20 61 6e 20  ssion is not an 
20f9c 69 6e 74 65 67 65 72 20 6f 72 20 69 66 20 69 74  integer or if it
20f9d 20 69 73 20 74 6f 6f 20 62 69 67 0a 2a 2a 20 74   is too big.** t
20f9e 6f 20 66 69 74 20 69 6e 20 61 20 73 69 67 6e 65  o fit in a signe
20f9f 64 20 33 32 2d 62 69 74 20 69 6e 74 65 67 65 72  d 32-bit integer
20fa0 2c 20 72 65 74 75 72 6e 20 30 20 61 6e 64 20 6c  , return 0 and l
20fa1 65 61 76 65 20 2a 70 56 61 6c 75 65 20 75 6e 63  eave *pValue unc
20fa2 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  hanged..*/.SQLIT
20fa3 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
20fa4 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65 67  lite3ExprIsInteg
20fa5 65 72 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20  er(Expr *p, int 
20fa6 2a 70 56 61 6c 75 65 29 7b 0a 20 20 69 6e 74 20  *pValue){.  int 
20fa7 72 63 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 2d  rc = 0;.  if( p-
20fa8 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 56  >flags & EP_IntV
20fa9 61 6c 75 65 20 29 7b 0a 20 20 20 20 2a 70 56 61  alue ){.    *pVa
20faa 6c 75 65 20 3d 20 70 2d 3e 75 2e 69 56 61 6c 75  lue = p->u.iValu
20fab 65 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  e;.    return 1;
20fac 0a 20 20 7d 0a 20 20 73 77 69 74 63 68 28 20 70  .  }.  switch( p
20fad 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65  ->op ){.    case
20fae 20 54 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20   TK_INTEGER: {. 
20faf 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
20fb0 33 47 65 74 49 6e 74 33 32 28 70 2d 3e 75 2e 7a  3GetInt32(p->u.z
20fb1 54 6f 6b 65 6e 2c 20 70 56 61 6c 75 65 29 3b 0a  Token, pValue);.
20fb2 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
20fb3 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65  ==0 );.      bre
20fb4 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
20fb5 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20  se TK_UPLUS: {. 
20fb6 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
20fb7 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70  3ExprIsInteger(p
20fb8 2d 3e 70 4c 65 66 74 2c 20 70 56 61 6c 75 65 29  ->pLeft, pValue)
20fb9 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
20fba 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
20fbb 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20 20  _UMINUS: {.     
20fbc 20 69 6e 74 20 76 3b 0a 20 20 20 20 20 20 69 66   int v;.      if
20fbd 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49  ( sqlite3ExprIsI
20fbe 6e 74 65 67 65 72 28 70 2d 3e 70 4c 65 66 74 2c  nteger(p->pLeft,
20fbf 20 26 76 29 20 29 7b 0a 20 20 20 20 20 20 20 20   &v) ){.        
20fc0 2a 70 56 61 6c 75 65 20 3d 20 2d 76 3b 0a 20 20  *pValue = -v;.  
20fc1 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20        rc = 1;.  
20fc2 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
20fc3 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
20fc4 61 75 6c 74 3a 20 62 72 65 61 6b 3b 0a 20 20 7d  ault: break;.  }
20fc5 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20  .  if( rc ){.   
20fc6 20 61 73 73 65 72 74 28 20 45 78 70 72 48 61 73   assert( ExprHas
20fc7 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c 20 45  AnyProperty(p, E
20fc8 50 5f 52 65 64 75 63 65 64 7c 45 50 5f 54 6f 6b  P_Reduced|EP_Tok
20fc9 65 6e 4f 6e 6c 79 29 0a 20 20 20 20 20 20 20 20  enOnly).        
20fca 20 20 20 20 20 20 20 7c 7c 20 28 70 2d 3e 66 6c         || (p->fl
20fcb 61 67 73 32 20 26 20 45 50 32 5f 4d 61 6c 6c 6f  ags2 & EP2_Mallo
20fcc 63 65 64 54 6f 6b 65 6e 29 3d 3d 30 20 29 3b 0a  cedToken)==0 );.
20fcd 20 20 20 20 70 2d 3e 6f 70 20 3d 20 54 4b 5f 49      p->op = TK_I
20fce 4e 54 45 47 45 52 3b 0a 20 20 20 20 70 2d 3e 66  NTEGER;.    p->f
20fcf 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e 74 56 61  lags |= EP_IntVa
20fd0 6c 75 65 3b 0a 20 20 20 20 70 2d 3e 75 2e 69 56  lue;.    p->u.iV
20fd1 61 6c 75 65 20 3d 20 2a 70 56 61 6c 75 65 3b 0a  alue = *pValue;.
20fd2 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
20fd3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
20fd4 20 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76   TRUE if the giv
20fd5 65 6e 20 73 74 72 69 6e 67 20 69 73 20 61 20 72  en string is a r
20fd6 6f 77 2d 69 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d  ow-id column nam
20fd7 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
20fd8 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
20fd9 49 73 52 6f 77 69 64 28 63 6f 6e 73 74 20 63 68  IsRowid(const ch
20fda 61 72 20 2a 7a 29 7b 0a 20 20 69 66 28 20 73 71  ar *z){.  if( sq
20fdb 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20  lite3StrICmp(z, 
20fdc 22 5f 52 4f 57 49 44 5f 22 29 3d 3d 30 20 29 20  "_ROWID_")==0 ) 
20fdd 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 66 28 20  return 1;.  if( 
20fde 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
20fdf 2c 20 22 52 4f 57 49 44 22 29 3d 3d 30 20 29 20  , "ROWID")==0 ) 
20fe0 72 65 74 75 72 6e 20 31 3b 0a 20 20 69 66 28 20  return 1;.  if( 
20fe1 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
20fe2 2c 20 22 4f 49 44 22 29 3d 3d 30 20 29 20 72 65  , "OID")==0 ) re
20fe3 74 75 72 6e 20 31 3b 0a 20 20 72 65 74 75 72 6e  turn 1;.  return
20fe4 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74   0;.}../*.** Ret
20fe5 75 72 6e 20 74 72 75 65 20 69 66 20 77 65 20 61  urn true if we a
20fe6 72 65 20 61 62 6c 65 20 74 6f 20 74 68 65 20 49  re able to the I
20fe7 4e 20 6f 70 65 72 61 74 6f 72 20 6f 70 74 69 6d  N operator optim
20fe8 69 7a 61 74 69 6f 6e 20 6f 6e 20 61 0a 2a 2a 20  ization on a.** 
20fe9 71 75 65 72 79 20 6f 66 20 74 68 65 20 66 6f 72  query of the for
20fea 6d 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 78 20  m.**.**       x 
20feb 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 0a  IN (SELECT ...).
20fec 2a 2a 0a 2a 2a 20 57 68 65 72 65 20 74 68 65 20  **.** Where the 
20fed 53 45 4c 45 43 54 2e 2e 2e 20 63 6c 61 75 73 65  SELECT... clause
20fee 20 69 73 20 61 73 20 73 70 65 63 69 66 69 65 64   is as specified
20fef 20 62 79 20 74 68 65 20 70 61 72 61 6d 65 74 65   by the paramete
20ff0 72 20 74 6f 20 74 68 69 73 0a 2a 2a 20 72 6f 75  r to this.** rou
20ff1 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  tine..**.** The 
20ff2 53 65 6c 65 63 74 20 6f 62 6a 65 63 74 20 70 61  Select object pa
20ff3 73 73 65 64 20 69 6e 20 68 61 73 20 61 6c 72 65  ssed in has alre
20ff4 61 64 79 20 62 65 65 6e 20 70 72 65 70 72 6f 63  ady been preproc
20ff5 65 73 73 65 64 20 61 6e 64 20 6e 6f 0a 2a 2a 20  essed and no.** 
20ff6 65 72 72 6f 72 73 20 68 61 76 65 20 62 65 65 6e  errors have been
20ff7 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 23 69 66 6e 64   found..*/.#ifnd
20ff8 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
20ff9 55 42 51 55 45 52 59 0a 73 74 61 74 69 63 20 69  UBQUERY.static i
20ffa 6e 74 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f  nt isCandidateFo
20ffb 72 49 6e 4f 70 74 28 53 65 6c 65 63 74 20 2a 70  rInOpt(Select *p
20ffc 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  ){.  SrcList *pS
20ffd 72 63 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a  rc;.  ExprList *
20ffe 70 45 4c 69 73 74 3b 0a 20 20 54 61 62 6c 65 20  pEList;.  Table 
20fff 2a 70 54 61 62 3b 0a 20 20 69 66 28 20 70 3d 3d  *pTab;.  if( p==
21000 30 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20  0 ) return 0;   
21001 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21002 2f 2a 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69  /* right-hand si
21003 64 65 20 6f 66 20 49 4e 20 69 73 20 53 45 4c 45  de of IN is SELE
21004 43 54 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70  CT */.  if( p->p
21005 50 72 69 6f 72 20 29 20 72 65 74 75 72 6e 20 30  Prior ) return 0
21006 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
21007 2a 20 4e 6f 74 20 61 20 63 6f 6d 70 6f 75 6e 64  * Not a compound
21008 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 66 28   SELECT */.  if(
21009 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 28   p->selFlags & (
2100a 53 46 5f 44 69 73 74 69 6e 63 74 7c 53 46 5f 41  SF_Distinct|SF_A
2100b 67 67 72 65 67 61 74 65 29 20 29 7b 0a 20 20 20  ggregate) ){.   
2100c 20 74 65 73 74 63 61 73 65 28 20 28 70 2d 3e 73   testcase( (p->s
2100d 65 6c 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69  elFlags & (SF_Di
2100e 73 74 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67  stinct|SF_Aggreg
2100f 61 74 65 29 29 3d 3d 53 46 5f 44 69 73 74 69 6e  ate))==SF_Distin
21010 63 74 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61  ct );.    testca
21011 73 65 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73  se( (p->selFlags
21012 20 26 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c   & (SF_Distinct|
21013 53 46 5f 41 67 67 72 65 67 61 74 65 29 29 3d 3d  SF_Aggregate))==
21014 53 46 5f 41 67 67 72 65 67 61 74 65 20 29 3b 0a  SF_Aggregate );.
21015 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 2f 2a      return 0; /*
21016 20 4e 6f 20 44 49 53 54 49 4e 43 54 20 6b 65 79   No DISTINCT key
21017 77 6f 72 64 20 61 6e 64 20 6e 6f 20 61 67 67 72  word and no aggr
21018 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20  egate functions 
21019 2a 2f 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  */.  }.  assert(
2101a 20 70 2d 3e 70 47 72 6f 75 70 42 79 3d 3d 30 20   p->pGroupBy==0 
2101b 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
2101c 2f 2a 20 48 61 73 20 6e 6f 20 47 52 4f 55 50 20  /* Has no GROUP 
2101d 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69  BY clause */.  i
2101e 66 28 20 70 2d 3e 70 4c 69 6d 69 74 20 29 20 72  f( p->pLimit ) r
2101f 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20  eturn 0;        
21020 20 20 20 20 20 20 2f 2a 20 48 61 73 20 6e 6f 20        /* Has no 
21021 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 2a 2f 0a  LIMIT clause */.
21022 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f 66    assert( p->pOf
21023 66 73 65 74 3d 3d 30 20 29 3b 20 20 20 20 20 20  fset==0 );      
21024 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20 4c           /* No L
21025 49 4d 49 54 20 6d 65 61 6e 73 20 6e 6f 20 4f 46  IMIT means no OF
21026 46 53 45 54 20 2a 2f 0a 20 20 69 66 28 20 70 2d  FSET */.  if( p-
21027 3e 70 57 68 65 72 65 20 29 20 72 65 74 75 72 6e  >pWhere ) return
21028 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
21029 20 2f 2a 20 48 61 73 20 6e 6f 20 57 48 45 52 45   /* Has no WHERE
2102a 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 70 53 72   clause */.  pSr
2102b 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 61  c = p->pSrc;.  a
2102c 73 73 65 72 74 28 20 70 53 72 63 21 3d 30 20 29  ssert( pSrc!=0 )
2102d 3b 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 6e 53  ;.  if( pSrc->nS
2102e 72 63 21 3d 31 20 29 20 72 65 74 75 72 6e 20 30  rc!=1 ) return 0
2102f 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69  ;          /* Si
21030 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20 46 52 4f  ngle term in FRO
21031 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66  M clause */.  if
21032 28 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65  ( pSrc->a[0].pSe
21033 6c 65 63 74 20 29 20 72 65 74 75 72 6e 20 30 3b  lect ) return 0;
21034 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 69 73 20       /* FROM is 
21035 6e 6f 74 20 61 20 73 75 62 71 75 65 72 79 20 6f  not a subquery o
21036 72 20 76 69 65 77 20 2a 2f 0a 20 20 70 54 61 62  r view */.  pTab
21037 20 3d 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54   = pSrc->a[0].pT
21038 61 62 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28  ab;.  if( NEVER(
21039 70 54 61 62 3d 3d 30 29 20 29 20 72 65 74 75 72  pTab==0) ) retur
2103a 6e 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70  n 0;.  assert( p
2103b 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20  Tab->pSelect==0 
2103c 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  );            /*
2103d 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20   FROM clause is 
2103e 6e 6f 74 20 61 20 76 69 65 77 20 2a 2f 0a 20 20  not a view */.  
2103f 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
21040 61 62 29 20 29 20 72 65 74 75 72 6e 20 30 3b 20  ab) ) return 0; 
21041 20 20 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63         /* FROM c
21042 6c 61 75 73 65 20 6e 6f 74 20 61 20 76 69 72 74  lause not a virt
21043 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 70  ual table */.  p
21044 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73  EList = p->pELis
21045 74 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d  t;.  if( pEList-
21046 3e 6e 45 78 70 72 21 3d 31 20 29 20 72 65 74 75  >nExpr!=1 ) retu
21047 72 6e 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f  rn 0;       /* O
21048 6e 65 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65  ne column in the
21049 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20   result set */. 
2104a 20 69 66 28 20 70 45 4c 69 73 74 2d 3e 61 5b 30   if( pEList->a[0
2104b 5d 2e 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ].pExpr->op!=TK_
2104c 43 4f 4c 55 4d 4e 20 29 20 72 65 74 75 72 6e 20  COLUMN ) return 
2104d 30 3b 20 2f 2a 20 52 65 73 75 6c 74 20 69 73 20  0; /* Result is 
2104e 61 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 72 65  a column */.  re
2104f 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66  turn 1;.}.#endif
21050 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
21051 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a  SUBQUERY */../*.
21052 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
21053 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20   is used by the 
21054 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
21055 66 20 74 68 65 20 49 4e 20 28 2e 2e 2e 29 20 6f  f the IN (...) o
21056 70 65 72 61 74 6f 72 2e 0a 2a 2a 20 49 74 27 73  perator..** It's
21057 20 6a 6f 62 20 69 73 20 74 6f 20 66 69 6e 64 20   job is to find 
21058 6f 72 20 63 72 65 61 74 65 20 61 20 62 2d 74 72  or create a b-tr
21059 65 65 20 73 74 72 75 63 74 75 72 65 20 74 68 61  ee structure tha
2105a 74 20 6d 61 79 20 62 65 20 75 73 65 64 0a 2a 2a  t may be used.**
2105b 20 65 69 74 68 65 72 20 74 6f 20 74 65 73 74 20   either to test 
2105c 66 6f 72 20 6d 65 6d 62 65 72 73 68 69 70 20 6f  for membership o
2105d 66 20 74 68 65 20 28 2e 2e 2e 29 20 73 65 74 20  f the (...) set 
2105e 6f 72 20 74 6f 20 69 74 65 72 61 74 65 20 74 68  or to iterate th
2105f 72 6f 75 67 68 0a 2a 2a 20 69 74 73 20 6d 65 6d  rough.** its mem
21060 62 65 72 73 2c 20 73 6b 69 70 70 69 6e 67 20 64  bers, skipping d
21061 75 70 6c 69 63 61 74 65 73 2e 0a 2a 2a 0a 2a 2a  uplicates..**.**
21062 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   The index of th
21063 65 20 63 75 72 73 6f 72 20 6f 70 65 6e 65 64 20  e cursor opened 
21064 6f 6e 20 74 68 65 20 62 2d 74 72 65 65 20 28 64  on the b-tree (d
21065 61 74 61 62 61 73 65 20 74 61 62 6c 65 2c 20 64  atabase table, d
21066 61 74 61 62 61 73 65 20 69 6e 64 65 78 20 0a 2a  atabase index .*
21067 2a 20 6f 72 20 65 70 68 65 72 6d 61 6c 20 74 61  * or ephermal ta
21068 62 6c 65 29 20 69 73 20 73 74 6f 72 65 64 20 69  ble) is stored i
21069 6e 20 70 58 2d 3e 69 54 61 62 6c 65 20 62 65 66  n pX->iTable bef
2106a 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ore this functio
2106b 6e 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 20 54 68  n returns..** Th
2106c 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65  e returned value
2106d 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   of this functio
2106e 6e 20 69 6e 64 69 63 61 74 65 73 20 74 68 65 20  n indicates the 
2106f 62 2d 74 72 65 65 20 74 79 70 65 2c 20 61 73 20  b-tree type, as 
21070 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20  follows:.**.**  
21071 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20   IN_INDEX_ROWID 
21072 2d 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 73  - The cursor was
21073 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61 74   opened on a dat
21074 61 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2a 20  abase table..** 
21075 20 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58    IN_INDEX_INDEX
21076 20 2d 20 54 68 65 20 63 75 72 73 6f 72 20 77 61   - The cursor wa
21077 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61  s opened on a da
21078 74 61 62 61 73 65 20 69 6e 64 65 78 2e 0a 2a 2a  tabase index..**
21079 20 20 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20     IN_INDEX_EPH 
2107a 2d 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 77  -   The cursor w
2107b 61 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 73  as opened on a s
2107c 70 65 63 69 61 6c 6c 79 20 63 72 65 61 74 65 64  pecially created
2107d 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20   and.**         
2107e 20 20 20 20 20 20 20 20 20 20 20 70 6f 70 75 6c             popul
2107f 61 74 65 64 20 65 70 68 65 72 65 6d 61 6c 20 74  ated epheremal t
21080 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65  able..**.** An e
21081 78 69 73 74 69 6e 67 20 62 2d 74 72 65 65 20 6d  xisting b-tree m
21082 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20  ay only be used 
21083 69 66 20 74 68 65 20 53 45 4c 45 43 54 20 69 73  if the SELECT is
21084 20 6f 66 20 74 68 65 20 73 69 6d 70 6c 65 0a 2a   of the simple.*
21085 2a 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20  * form:.**.**   
21086 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c 75 6d 6e    SELECT <column
21087 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65 3e 0a 2a  > FROM <table>.*
21088 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 4e 6f  *.** If the prNo
21089 74 46 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72  tFound parameter
2108a 20 69 73 20 30 2c 20 74 68 65 6e 20 74 68 65 20   is 0, then the 
2108b 62 2d 74 72 65 65 20 77 69 6c 6c 20 62 65 20 75  b-tree will be u
2108c 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 0a 2a  sed to iterate.*
2108d 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20 73 65  * through the se
2108e 74 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69 70 70  t members, skipp
2108f 69 6e 67 20 61 6e 79 20 64 75 70 6c 69 63 61 74  ing any duplicat
21090 65 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  es. In this case
21091 20 61 6e 0a 2a 2a 20 65 70 68 65 72 65 6d 61 6c   an.** epheremal
21092 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20 75   table must be u
21093 73 65 64 20 75 6e 6c 65 73 73 20 74 68 65 20 73  sed unless the s
21094 65 6c 65 63 74 65 64 20 3c 63 6f 6c 75 6d 6e 3e  elected <column>
21095 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 0a 2a   is guaranteed.*
21096 2a 20 74 6f 20 62 65 20 75 6e 69 71 75 65 20 2d  * to be unique -
21097 20 65 69 74 68 65 72 20 62 65 63 61 75 73 65 20   either because 
21098 69 74 20 69 73 20 61 6e 20 49 4e 54 45 47 45 52  it is an INTEGER
21099 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20   PRIMARY KEY or 
2109a 69 74 0a 2a 2a 20 68 61 73 20 61 20 55 4e 49 51  it.** has a UNIQ
2109b 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 72  UE constraint or
2109c 20 55 4e 49 51 55 45 20 69 6e 64 65 78 2e 0a 2a   UNIQUE index..*
2109d 2a 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 4e 6f  *.** If the prNo
2109e 74 46 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72  tFound parameter
2109f 20 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20   is not 0, then 
210a0 74 68 65 20 62 2d 74 72 65 65 20 77 69 6c 6c 20  the b-tree will 
210a1 62 65 20 75 73 65 64 20 0a 2a 2a 20 66 6f 72 20  be used .** for 
210a2 66 61 73 74 20 73 65 74 20 6d 65 6d 62 65 72 73  fast set members
210a3 68 69 70 20 74 65 73 74 73 2e 20 49 6e 20 74 68  hip tests. In th
210a4 69 73 20 63 61 73 65 20 61 6e 20 65 70 68 65 72  is case an epher
210a5 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73 74 20  emal table must 
210a6 0a 2a 2a 20 62 65 20 75 73 65 64 20 75 6e 6c 65  .** be used unle
210a7 73 73 20 3c 63 6f 6c 75 6d 6e 3e 20 69 73 20 61  ss <column> is a
210a8 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  n INTEGER PRIMAR
210a9 59 20 4b 45 59 20 6f 72 20 61 6e 20 69 6e 64 65  Y KEY or an inde
210aa 78 20 63 61 6e 20 0a 2a 2a 20 62 65 20 66 6f 75  x can .** be fou
210ab 6e 64 20 77 69 74 68 20 3c 63 6f 6c 75 6d 6e 3e  nd with <column>
210ac 20 61 73 20 69 74 73 20 6c 65 66 74 2d 6d 6f 73   as its left-mos
210ad 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20  t column..**.** 
210ae 57 68 65 6e 20 74 68 65 20 62 2d 74 72 65 65 20  When the b-tree 
210af 69 73 20 62 65 69 6e 67 20 75 73 65 64 20 66 6f  is being used fo
210b0 72 20 6d 65 6d 62 65 72 73 68 69 70 20 74 65 73  r membership tes
210b1 74 73 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 20  ts, the calling 
210b2 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6e 65 65 64  function.** need
210b3 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 74 68 65  s to know whethe
210b4 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 73 74 72  r or not the str
210b5 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20  ucture contains 
210b6 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 0a 2a 2a 20  an SQL NULL .** 
210b7 76 61 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 74  value in order t
210b8 6f 20 63 6f 72 72 65 63 74 6c 79 20 65 76 61 6c  o correctly eval
210b9 75 61 74 65 20 65 78 70 72 65 73 73 69 6f 6e 73  uate expressions
210ba 20 6c 69 6b 65 20 22 58 20 49 4e 20 28 59 2c 20   like "X IN (Y, 
210bb 5a 29 22 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65  Z)"..** If there
210bc 20 69 73 20 61 20 63 68 61 6e 63 65 20 74 68 61   is a chance tha
210bd 74 20 74 68 65 20 62 2d 74 72 65 65 20 6d 69 67  t the b-tree mig
210be 68 74 20 63 6f 6e 74 61 69 6e 20 61 20 4e 55 4c  ht contain a NUL
210bf 4c 20 76 61 6c 75 65 20 61 74 0a 2a 2a 20 72 75  L value at.** ru
210c0 6e 74 69 6d 65 2c 20 74 68 65 6e 20 61 20 72 65  ntime, then a re
210c1 67 69 73 74 65 72 20 69 73 20 61 6c 6c 6f 63 61  gister is alloca
210c2 74 65 64 20 61 6e 64 20 74 68 65 20 72 65 67 69  ted and the regi
210c3 73 74 65 72 20 6e 75 6d 62 65 72 20 77 72 69 74  ster number writ
210c4 74 65 6e 0a 2a 2a 20 74 6f 20 2a 70 72 4e 6f 74  ten.** to *prNot
210c5 46 6f 75 6e 64 2e 20 49 66 20 74 68 65 72 65 20  Found. If there 
210c6 69 73 20 6e 6f 20 63 68 61 6e 63 65 20 74 68 61  is no chance tha
210c7 74 20 74 68 65 20 62 2d 74 72 65 65 20 63 6f 6e  t the b-tree con
210c8 74 61 69 6e 73 20 61 0a 2a 2a 20 4e 55 4c 4c 20  tains a.** NULL 
210c9 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a 70 72 4e  value, then *prN
210ca 6f 74 46 6f 75 6e 64 20 69 73 20 6c 65 66 74 20  otFound is left 
210cb 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a  unchanged..**.**
210cc 20 49 66 20 61 20 72 65 67 69 73 74 65 72 20 69   If a register i
210cd 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20  s allocated and 
210ce 69 74 73 20 6c 6f 63 61 74 69 6f 6e 20 73 74 6f  its location sto
210cf 72 65 64 20 69 6e 20 2a 70 72 4e 6f 74 46 6f 75  red in *prNotFou
210d0 6e 64 2c 20 74 68 65 6e 0a 2a 2a 20 69 74 73 20  nd, then.** its 
210d1 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 69 73  initial value is
210d2 20 4e 55 4c 4c 2e 20 49 66 20 74 68 65 20 62 2d   NULL. If the b-
210d3 74 72 65 65 20 64 6f 65 73 20 6e 6f 74 20 72 65  tree does not re
210d4 6d 61 69 6e 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a  main constant.**
210d5 20 66 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f   for the duratio
210d6 6e 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 28  n of the query (
210d7 69 2e 65 2e 20 74 68 65 20 53 45 4c 45 43 54 20  i.e. the SELECT 
210d8 74 68 61 74 20 67 65 6e 65 72 61 74 65 73 20 74  that generates t
210d9 68 65 20 62 2d 74 72 65 65 0a 2a 2a 20 69 73 20  he b-tree.** is 
210da 61 20 63 6f 72 72 65 6c 61 74 65 64 20 73 75 62  a correlated sub
210db 71 75 65 72 79 29 20 74 68 65 6e 20 74 68 65 20  query) then the 
210dc 76 61 6c 75 65 20 6f 66 20 74 68 65 20 61 6c 6c  value of the all
210dd 6f 63 61 74 65 64 20 72 65 67 69 73 74 65 72 20  ocated register 
210de 69 73 0a 2a 2a 20 72 65 73 65 74 20 74 6f 20 4e  is.** reset to N
210df 55 4c 4c 20 65 61 63 68 20 74 69 6d 65 20 74 68  ULL each time th
210e0 65 20 62 2d 74 72 65 65 20 69 73 20 72 65 70 6f  e b-tree is repo
210e1 70 75 6c 61 74 65 64 2e 20 54 68 69 73 20 61 6c  pulated. This al
210e2 6c 6f 77 73 20 74 68 65 0a 2a 2a 20 63 61 6c 6c  lows the.** call
210e3 65 72 20 74 6f 20 75 73 65 20 76 64 62 65 20 63  er to use vdbe c
210e4 6f 64 65 20 65 71 75 69 76 61 6c 65 6e 74 20 74  ode equivalent t
210e5 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  o the following:
210e6 0a 2a 2a 0a 2a 2a 20 20 20 69 66 28 20 72 65 67  .**.**   if( reg
210e7 69 73 74 65 72 3d 3d 4e 55 4c 4c 20 29 7b 0a 2a  ister==NULL ){.*
210e8 2a 20 20 20 20 20 68 61 73 5f 6e 75 6c 6c 20 3d  *     has_null =
210e9 20 3c 74 65 73 74 20 69 66 20 64 61 74 61 20 73   <test if data s
210ea 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
210eb 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 20 20 20 20 72  s null>.**     r
210ec 65 67 69 73 74 65 72 20 3d 20 31 0a 2a 2a 20 20  egister = 1.**  
210ed 20 7d 0a 2a 2a 0a 2a 2a 20 69 6e 20 6f 72 64 65   }.**.** in orde
210ee 72 20 74 6f 20 61 76 6f 69 64 20 72 75 6e 6e 69  r to avoid runni
210ef 6e 67 20 74 68 65 20 3c 74 65 73 74 20 69 66 20  ng the <test if 
210f0 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 63  data structure c
210f1 6f 6e 74 61 69 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a  ontains null>.**
210f2 20 74 65 73 74 20 6d 6f 72 65 20 6f 66 74 65 6e   test more often
210f3 20 74 68 61 6e 20 69 73 20 6e 65 63 65 73 73 61   than is necessa
210f4 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  ry..*/.#ifndef S
210f5 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
210f6 45 52 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ERY.SQLITE_PRIVA
210f7 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69  TE int sqlite3Fi
210f8 6e 64 49 6e 49 6e 64 65 78 28 50 61 72 73 65 20  ndInIndex(Parse 
210f9 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
210fa 58 2c 20 69 6e 74 20 2a 70 72 4e 6f 74 46 6f 75  X, int *prNotFou
210fb 6e 64 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70  nd){.  Select *p
210fc 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
210fd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
210fe 53 45 4c 45 43 54 20 74 6f 20 74 68 65 20 72 69  SELECT to the ri
210ff 67 68 74 20 6f 66 20 49 4e 20 6f 70 65 72 61 74  ght of IN operat
21100 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 65 54 79 70  or */.  int eTyp
21101 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  e = 0;          
21102 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
21103 20 54 79 70 65 20 6f 66 20 52 48 53 20 74 61 62   Type of RHS tab
21104 6c 65 2e 20 49 4e 5f 49 4e 44 45 58 5f 2a 20 2a  le. IN_INDEX_* *
21105 2f 0a 20 20 69 6e 74 20 69 54 61 62 20 3d 20 70  /.  int iTab = p
21106 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 20 20  Parse->nTab++;  
21107 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
21108 73 6f 72 20 6f 66 20 74 68 65 20 52 48 53 20 74  sor of the RHS t
21109 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 75  able */.  int mu
2110a 73 74 42 65 55 6e 69 71 75 65 20 3d 20 28 70 72  stBeUnique = (pr
2110b 4e 6f 74 46 6f 75 6e 64 3d 3d 30 29 3b 20 20 20  NotFound==0);   
2110c 2f 2a 20 54 72 75 65 20 69 66 20 52 48 53 20 6d  /* True if RHS m
2110d 75 73 74 20 62 65 20 75 6e 69 71 75 65 20 2a 2f  ust be unique */
2110e 0a 0a 20 20 61 73 73 65 72 74 28 20 70 58 2d 3e  ..  assert( pX->
2110f 6f 70 3d 3d 54 4b 5f 49 4e 20 29 3b 0a 0a 20 20  op==TK_IN );..  
21110 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  /* Check to see 
21111 69 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20 74  if an existing t
21112 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 63 61  able or index ca
21113 6e 20 62 65 20 75 73 65 64 20 74 6f 0a 20 20 2a  n be used to.  *
21114 2a 20 73 61 74 69 73 66 79 20 74 68 65 20 71 75  * satisfy the qu
21115 65 72 79 2e 20 20 54 68 69 73 20 69 73 20 70 72  ery.  This is pr
21116 65 66 65 72 61 62 6c 65 20 74 6f 20 67 65 6e 65  eferable to gene
21117 72 61 74 69 6e 67 20 61 20 6e 65 77 20 0a 20 20  rating a new .  
21118 2a 2a 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62  ** ephemeral tab
21119 6c 65 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d 20 28  le..  */.  p = (
2111a 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
2111b 70 58 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pX, EP_xIsSelect
2111c 29 20 3f 20 70 58 2d 3e 78 2e 70 53 65 6c 65 63  ) ? pX->x.pSelec
2111d 74 20 3a 20 30 29 3b 0a 20 20 69 66 28 20 41 4c  t : 0);.  if( AL
2111e 57 41 59 53 28 70 50 61 72 73 65 2d 3e 6e 45 72  WAYS(pParse->nEr
2111f 72 3d 3d 30 29 20 26 26 20 69 73 43 61 6e 64 69  r==0) && isCandi
21120 64 61 74 65 46 6f 72 49 6e 4f 70 74 28 70 29 20  dateForInOpt(p) 
21121 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a  ){.    sqlite3 *
21122 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
21123 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
21124 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
21125 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 45 78 70 72  tion */.    Expr
21126 20 2a 70 45 78 70 72 20 3d 20 70 2d 3e 70 45 4c   *pExpr = p->pEL
21127 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b  ist->a[0].pExpr;
21128 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e     /* Expression
21129 20 3c 63 6f 6c 75 6d 6e 3e 20 2a 2f 0a 20 20 20   <column> */.   
2112a 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 45 78 70   int iCol = pExp
2112b 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  r->iColumn;     
2112c 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
2112d 20 6f 66 20 63 6f 6c 75 6d 6e 20 3c 63 6f 6c 75   of column <colu
2112e 6d 6e 3e 20 2a 2f 0a 20 20 20 20 56 64 62 65 20  mn> */.    Vdbe 
2112f 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  *v = sqlite3GetV
21130 64 62 65 28 70 50 61 72 73 65 29 3b 20 20 20 20  dbe(pParse);    
21131 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 6d 61 63    /* Virtual mac
21132 68 69 6e 65 20 62 65 69 6e 67 20 63 6f 64 65 64  hine being coded
21133 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65 20 2a 70   */.    Table *p
21134 54 61 62 20 3d 20 70 2d 3e 70 53 72 63 2d 3e 61  Tab = p->pSrc->a
21135 5b 30 5d 2e 70 54 61 62 3b 20 20 20 20 20 20 2f  [0].pTab;      /
21136 2a 20 54 61 62 6c 65 20 3c 74 61 62 6c 65 3e 2e  * Table <table>.
21137 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44 62 3b   */.    int iDb;
21138 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21139 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2113a 2a 20 44 61 74 61 62 61 73 65 20 69 64 78 20 66  * Database idx f
2113b 6f 72 20 70 54 61 62 20 2a 2f 0a 20 20 20 0a 20  or pTab */.   . 
2113c 20 20 20 2f 2a 20 43 6f 64 65 20 61 6e 20 4f 50     /* Code an OP
2113d 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 61 6e  _VerifyCookie an
2113e 64 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 66  d OP_TableLock f
2113f 6f 72 20 3c 74 61 62 6c 65 3e 2e 20 2a 2f 0a 20  or <table>. */. 
21140 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33     iDb = sqlite3
21141 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62  SchemaToIndex(db
21142 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
21143 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64  ;.    sqlite3Cod
21144 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50  eVerifySchema(pP
21145 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20  arse, iDb);.    
21146 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
21147 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54  (pParse, iDb, pT
21148 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 61  ab->tnum, 0, pTa
21149 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20 20  b->zName);..    
2114a 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  /* This function
2114b 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20   is only called 
2114c 66 72 6f 6d 20 74 77 6f 20 70 6c 61 63 65 73 2e  from two places.
2114d 20 49 6e 20 62 6f 74 68 20 63 61 73 65 73 20 74   In both cases t
2114e 68 65 20 76 64 62 65 0a 20 20 20 20 2a 2a 20 68  he vdbe.    ** h
2114f 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
21150 61 6c 6c 6f 63 61 74 65 64 2e 20 53 6f 20 61 73  allocated. So as
21151 73 75 6d 65 20 73 71 6c 69 74 65 33 47 65 74 56  sume sqlite3GetV
21152 64 62 65 28 29 20 69 73 20 61 6c 77 61 79 73 0a  dbe() is always.
21153 20 20 20 20 2a 2a 20 73 75 63 63 65 73 73 66 75      ** successfu
21154 6c 20 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20  l here..    */. 
21155 20 20 20 61 73 73 65 72 74 28 76 29 3b 0a 20 20     assert(v);.  
21156 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a    if( iCol<0 ){.
21157 20 20 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d        int iMem =
21158 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
21159 0a 20 20 20 20 20 20 69 6e 74 20 69 41 64 64 72  .      int iAddr
2115a 3b 0a 0a 20 20 20 20 20 20 69 41 64 64 72 20 3d  ;..      iAddr =
2115b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2115c 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 69 4d 65  p1(v, OP_If, iMe
2115d 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  m);.      sqlite
2115e 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2115f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 4d  P_Integer, 1, iM
21160 65 6d 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69  em);..      sqli
21161 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61  te3OpenTable(pPa
21162 72 73 65 2c 20 69 54 61 62 2c 20 69 44 62 2c 20  rse, iTab, iDb, 
21163 70 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61  pTab, OP_OpenRea
21164 64 29 3b 0a 20 20 20 20 20 20 65 54 79 70 65 20  d);.      eType 
21165 3d 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44  = IN_INDEX_ROWID
21166 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
21167 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
21168 69 41 64 64 72 29 3b 0a 20 20 20 20 7d 65 6c 73  iAddr);.    }els
21169 65 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a  e{.      Index *
2116a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20  pIdx;           
2116b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2116c 20 49 74 65 72 61 74 6f 72 20 76 61 72 69 61 62   Iterator variab
2116d 6c 65 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20  le */..      /* 
2116e 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  The collation se
2116f 71 75 65 6e 63 65 20 75 73 65 64 20 62 79 20 74  quence used by t
21170 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 49  he comparison. I
21171 66 20 61 6e 20 69 6e 64 65 78 20 69 73 20 74 6f  f an index is to
21172 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 75 73 65  .      ** be use
21173 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 20  d in place of a 
21174 74 65 6d 70 2d 74 61 62 6c 65 2c 20 69 74 20 6d  temp-table, it m
21175 75 73 74 20 62 65 20 6f 72 64 65 72 65 64 20 61  ust be ordered a
21176 63 63 6f 72 64 69 6e 67 0a 20 20 20 20 20 20 2a  ccording.      *
21177 2a 20 74 6f 20 74 68 69 73 20 63 6f 6c 6c 61 74  * to this collat
21178 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e 20 20 2a  ion sequence.  *
21179 2f 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20  /.      CollSeq 
2117a 2a 70 52 65 71 20 3d 20 73 71 6c 69 74 65 33 42  *pReq = sqlite3B
2117b 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c  inaryCompareColl
2117c 53 65 71 28 70 50 61 72 73 65 2c 20 70 58 2d 3e  Seq(pParse, pX->
2117d 70 4c 65 66 74 2c 20 70 45 78 70 72 29 3b 0a 0a  pLeft, pExpr);..
2117e 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74        /* Check t
2117f 68 61 74 20 74 68 65 20 61 66 66 69 6e 69 74 79  hat the affinity
21180 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 75 73   that will be us
21181 65 64 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68  ed to perform th
21182 65 20 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6d 70  e .      ** comp
21183 61 72 69 73 6f 6e 20 69 73 20 74 68 65 20 73 61  arison is the sa
21184 6d 65 20 61 73 20 74 68 65 20 61 66 66 69 6e 69  me as the affini
21185 74 79 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e  ty of the column
21186 2e 20 49 66 0a 20 20 20 20 20 20 2a 2a 20 69 74  . If.      ** it
21187 20 69 73 20 6e 6f 74 2c 20 69 74 20 69 73 20 6e   is not, it is n
21188 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75  ot possible to u
21189 73 65 20 61 6e 79 20 69 6e 64 65 78 2e 0a 20 20  se any index..  
2118a 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 68 61      */.      cha
2118b 72 20 61 66 66 20 3d 20 63 6f 6d 70 61 72 69 73  r aff = comparis
2118c 6f 6e 41 66 66 69 6e 69 74 79 28 70 58 29 3b 0a  onAffinity(pX);.
2118d 20 20 20 20 20 20 69 6e 74 20 61 66 66 69 6e 69        int affini
2118e 74 79 5f 6f 6b 20 3d 20 28 70 54 61 62 2d 3e 61  ty_ok = (pTab->a
2118f 43 6f 6c 5b 69 43 6f 6c 5d 2e 61 66 66 69 6e 69  Col[iCol].affini
21190 74 79 3d 3d 61 66 66 7c 7c 61 66 66 3d 3d 53 51  ty==aff||aff==SQ
21191 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 29 3b 0a  LITE_AFF_NONE);.
21192 0a 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d  .      for(pIdx=
21193 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
21194 64 78 20 26 26 20 65 54 79 70 65 3d 3d 30 20 26  dx && eType==0 &
21195 26 20 61 66 66 69 6e 69 74 79 5f 6f 6b 3b 20 70  & affinity_ok; p
21196 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
21197 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28 70  {.        if( (p
21198 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d  Idx->aiColumn[0]
21199 3d 3d 69 43 6f 6c 29 0a 20 20 20 20 20 20 20 20  ==iCol).        
2119a 20 26 26 20 73 71 6c 69 74 65 33 46 69 6e 64 43   && sqlite3FindC
2119b 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64  ollSeq(db, ENC(d
2119c 62 29 2c 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c  b), pIdx->azColl
2119d 5b 30 5d 2c 20 30 29 3d 3d 70 52 65 71 0a 20 20  [0], 0)==pReq.  
2119e 20 20 20 20 20 20 20 26 26 20 28 21 6d 75 73 74         && (!must
2119f 42 65 55 6e 69 71 75 65 20 7c 7c 20 28 70 49 64  BeUnique || (pId
211a0 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 26 26  x->nColumn==1 &&
211a1 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d   pIdx->onError!=
211a2 4f 45 5f 4e 6f 6e 65 29 29 0a 20 20 20 20 20 20  OE_None)).      
211a3 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69    ){.          i
211a4 6e 74 20 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72  nt iMem = ++pPar
211a5 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20  se->nMem;.      
211a6 20 20 20 20 69 6e 74 20 69 41 64 64 72 3b 0a 20      int iAddr;. 
211a7 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 70           char *p
211a8 4b 65 79 3b 0a 20 20 0a 20 20 20 20 20 20 20 20  Key;.  .        
211a9 20 20 70 4b 65 79 20 3d 20 28 63 68 61 72 20 2a    pKey = (char *
211aa 29 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79  )sqlite3IndexKey
211ab 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64  info(pParse, pId
211ac 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 41  x);.          iA
211ad 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
211ae 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
211af 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20  , iMem);.       
211b0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
211b1 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
211b2 65 72 2c 20 31 2c 20 69 4d 65 6d 29 3b 0a 20 20  er, 1, iMem);.  
211b3 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
211b4 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
211b5 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 54 61  OP_OpenRead, iTa
211b6 62 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69  b, pIdx->tnum, i
211b7 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  Db,.            
211b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
211b9 20 20 20 70 4b 65 79 2c 50 34 5f 4b 45 59 49 4e     pKey,P4_KEYIN
211ba 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20  FO_HANDOFF);.   
211bb 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65         VdbeComme
211bc 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70 49 64  nt((v, "%s", pId
211bd 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20  x->zName));.    
211be 20 20 20 20 20 20 65 54 79 70 65 20 3d 20 49 4e        eType = IN
211bf 5f 49 4e 44 45 58 5f 49 4e 44 45 58 3b 0a 0a 20  _INDEX_INDEX;.. 
211c0 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
211c1 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
211c2 69 41 64 64 72 29 3b 0a 20 20 20 20 20 20 20 20  iAddr);.        
211c3 20 20 69 66 28 20 70 72 4e 6f 74 46 6f 75 6e 64    if( prNotFound
211c4 20 26 26 20 21 70 54 61 62 2d 3e 61 43 6f 6c 5b   && !pTab->aCol[
211c5 69 43 6f 6c 5d 2e 6e 6f 74 4e 75 6c 6c 20 29 7b  iCol].notNull ){
211c6 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 70 72  .            *pr
211c7 4e 6f 74 46 6f 75 6e 64 20 3d 20 2b 2b 70 50 61  NotFound = ++pPa
211c8 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20  rse->nMem;.     
211c9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
211ca 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
211cb 20 7d 0a 0a 20 20 69 66 28 20 65 54 79 70 65 3d   }..  if( eType=
211cc 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 75  =0 ){.    /* Cou
211cd 6c 64 20 6e 6f 74 20 66 6f 75 6e 64 20 61 6e 20  ld not found an 
211ce 65 78 69 73 74 69 6e 67 20 74 61 62 6c 65 20 6f  existing table o
211cf 72 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20 61  r index to use a
211d0 73 20 74 68 65 20 52 48 53 20 62 2d 74 72 65 65  s the RHS b-tree
211d1 2e 0a 20 20 20 20 2a 2a 20 57 65 20 77 69 6c 6c  ..    ** We will
211d2 20 68 61 76 65 20 74 6f 20 67 65 6e 65 72 61 74   have to generat
211d3 65 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 74  e an ephemeral t
211d4 61 62 6c 65 20 74 6f 20 64 6f 20 74 68 65 20 6a  able to do the j
211d5 6f 62 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ob..    */.    i
211d6 6e 74 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20  nt rMayHaveNull 
211d7 3d 20 30 3b 0a 20 20 20 20 65 54 79 70 65 20 3d  = 0;.    eType =
211d8 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 3b 0a 20   IN_INDEX_EPH;. 
211d9 20 20 20 69 66 28 20 70 72 4e 6f 74 46 6f 75 6e     if( prNotFoun
211da 64 20 29 7b 0a 20 20 20 20 20 20 2a 70 72 4e 6f  d ){.      *prNo
211db 74 46 6f 75 6e 64 20 3d 20 72 4d 61 79 48 61 76  tFound = rMayHav
211dc 65 4e 75 6c 6c 20 3d 20 2b 2b 70 50 61 72 73 65  eNull = ++pParse
211dd 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d 65 6c 73  ->nMem;.    }els
211de 65 20 69 66 28 20 70 58 2d 3e 70 4c 65 66 74 2d  e if( pX->pLeft-
211df 3e 69 43 6f 6c 75 6d 6e 3c 30 20 26 26 20 21 45  >iColumn<0 && !E
211e0 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74  xprHasAnyPropert
211e1 79 28 70 58 2c 20 45 50 5f 78 49 73 53 65 6c 65  y(pX, EP_xIsSele
211e2 63 74 29 20 29 7b 0a 20 20 20 20 20 20 65 54 79  ct) ){.      eTy
211e3 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 52 4f  pe = IN_INDEX_RO
211e4 57 49 44 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  WID;.    }.    s
211e5 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c  qlite3CodeSubsel
211e6 65 63 74 28 70 50 61 72 73 65 2c 20 70 58 2c 20  ect(pParse, pX, 
211e7 72 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20 65 54  rMayHaveNull, eT
211e8 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f  ype==IN_INDEX_RO
211e9 57 49 44 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  WID);.  }else{. 
211ea 20 20 20 70 58 2d 3e 69 54 61 62 6c 65 20 3d 20     pX->iTable = 
211eb 69 54 61 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75  iTab;.  }.  retu
211ec 72 6e 20 65 54 79 70 65 3b 0a 7d 0a 23 65 6e 64  rn eType;.}.#end
211ed 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  if../*.** Genera
211ee 74 65 20 63 6f 64 65 20 66 6f 72 20 73 63 61 6c  te code for scal
211ef 61 72 20 73 75 62 71 75 65 72 69 65 73 20 75 73  ar subqueries us
211f0 65 64 20 61 73 20 61 6e 20 65 78 70 72 65 73 73  ed as an express
211f1 69 6f 6e 0a 2a 2a 20 61 6e 64 20 49 4e 20 6f 70  ion.** and IN op
211f2 65 72 61 74 6f 72 73 2e 20 20 45 78 61 6d 70 6c  erators.  Exampl
211f3 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 53  es:.**.**     (S
211f4 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 62 29 20  ELECT a FROM b) 
211f5 20 20 20 20 20 20 20 20 20 2d 2d 20 73 75 62 71           -- subq
211f6 75 65 72 79 0a 2a 2a 20 20 20 20 20 45 58 49 53  uery.**     EXIS
211f7 54 53 20 28 53 45 4c 45 43 54 20 61 20 46 52 4f  TS (SELECT a FRO
211f8 4d 20 62 29 20 20 20 2d 2d 20 45 58 49 53 54 53  M b)   -- EXISTS
211f9 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 20 20 20   subquery.**    
211fa 20 78 20 49 4e 20 28 34 2c 35 2c 31 31 29 20 20   x IN (4,5,11)  
211fb 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 49              -- I
211fc 4e 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20  N operator with 
211fd 6c 69 73 74 20 6f 6e 20 72 69 67 68 74 2d 68 61  list on right-ha
211fe 6e 64 20 73 69 64 65 0a 2a 2a 20 20 20 20 20 78  nd side.**     x
211ff 20 49 4e 20 28 53 45 4c 45 43 54 20 61 20 46 52   IN (SELECT a FR
21200 4f 4d 20 62 29 20 20 20 20 20 2d 2d 20 49 4e 20  OM b)     -- IN 
21201 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20 73 75  operator with su
21202 62 71 75 65 72 79 20 6f 6e 20 74 68 65 20 72 69  bquery on the ri
21203 67 68 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 45  ght.**.** The pE
21204 78 70 72 20 70 61 72 61 6d 65 74 65 72 20 64 65  xpr parameter de
21205 73 63 72 69 62 65 73 20 74 68 65 20 65 78 70 72  scribes the expr
21206 65 73 73 69 6f 6e 20 74 68 61 74 20 63 6f 6e 74  ession that cont
21207 61 69 6e 73 20 74 68 65 20 49 4e 0a 2a 2a 20 6f  ains the IN.** o
21208 70 65 72 61 74 6f 72 20 6f 72 20 73 75 62 71 75  perator or subqu
21209 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61  ery..**.** If pa
2120a 72 61 6d 65 74 65 72 20 69 73 52 6f 77 69 64 20  rameter isRowid 
2120b 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
2120c 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78  n expression pEx
2120d 70 72 20 69 73 20 67 75 61 72 61 6e 74 65 65 64  pr is guaranteed
2120e 0a 2a 2a 20 74 6f 20 62 65 20 6f 66 20 74 68 65  .** to be of the
2120f 20 66 6f 72 6d 20 22 3c 72 6f 77 69 64 3e 20 49   form "<rowid> I
21210 4e 20 28 3f 2c 20 3f 2c 20 3f 29 22 2c 20 77 68  N (?, ?, ?)", wh
21211 65 72 65 20 3c 72 6f 77 69 64 3e 20 69 73 20 61  ere <rowid> is a
21212 20 72 65 66 65 72 65 6e 63 65 0a 2a 2a 20 74 6f   reference.** to
21213 20 73 6f 6d 65 20 69 6e 74 65 67 65 72 20 6b 65   some integer ke
21214 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 74 61  y column of a ta
21215 62 6c 65 20 42 2d 54 72 65 65 2e 20 49 6e 20 74  ble B-Tree. In t
21216 68 69 73 20 63 61 73 65 2c 20 75 73 65 20 61 6e  his case, use an
21217 0a 2a 2a 20 69 6e 74 6b 65 79 20 42 2d 54 72 65  .** intkey B-Tre
21218 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73  e to store the s
21219 65 74 20 6f 66 20 49 4e 28 2e 2e 2e 29 20 76 61  et of IN(...) va
2121a 6c 75 65 73 20 69 6e 73 74 65 61 64 20 6f 66 20  lues instead of 
2121b 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20 28 73 6c  the usual.** (sl
2121c 6f 77 65 72 29 20 76 61 72 69 61 62 6c 65 20 6c  ower) variable l
2121d 65 6e 67 74 68 20 6b 65 79 73 20 42 2d 54 72 65  ength keys B-Tre
2121e 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 4d 61 79  e..**.** If rMay
2121f 48 61 76 65 4e 75 6c 6c 20 69 73 20 6e 6f 6e 2d  HaveNull is non-
21220 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73  zero, that means
21221 20 74 68 61 74 20 74 68 65 20 6f 70 65 72 61 74   that the operat
21222 69 6f 6e 20 69 73 20 61 6e 20 49 4e 0a 2a 2a 20  ion is an IN.** 
21223 28 6e 6f 74 20 61 20 53 45 4c 45 43 54 20 6f 72  (not a SELECT or
21224 20 45 58 49 53 54 53 29 20 61 6e 64 20 74 68 61   EXISTS) and tha
21225 74 20 74 68 65 20 52 48 53 20 6d 69 67 68 74 20  t the RHS might 
21226 63 6f 6e 74 61 69 6e 73 20 4e 55 4c 4c 73 2e 0a  contains NULLs..
21227 2a 2a 20 46 75 72 74 68 65 72 6d 6f 72 65 2c 20  ** Furthermore, 
21228 74 68 65 20 49 4e 20 69 73 20 69 6e 20 61 20 57  the IN is in a W
21229 48 45 52 45 20 63 6c 61 75 73 65 20 61 6e 64 20  HERE clause and 
2122a 74 68 61 74 20 77 65 20 72 65 61 6c 6c 79 20 77  that we really w
2122b 61 6e 74 0a 2a 2a 20 74 6f 20 69 74 65 72 61 74  ant.** to iterat
2122c 65 20 6f 76 65 72 20 74 68 65 20 52 48 53 20 6f  e over the RHS o
2122d 66 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f  f the IN operato
2122e 72 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 71 75  r in order to qu
2122f 69 63 6b 6c 79 20 6c 6f 63 61 74 65 0a 2a 2a 20  ickly locate.** 
21230 61 6c 6c 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e  all correspondin
21231 67 20 4c 48 53 20 65 6c 65 6d 65 6e 74 73 2e 20  g LHS elements. 
21232 20 41 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e   All this routin
21233 65 20 64 6f 65 73 20 69 73 20 69 6e 69 74 69 61  e does is initia
21234 6c 69 7a 65 0a 2a 2a 20 74 68 65 20 72 65 67 69  lize.** the regi
21235 73 74 65 72 20 67 69 76 65 6e 20 62 79 20 72 4d  ster given by rM
21236 61 79 48 61 76 65 4e 75 6c 6c 20 74 6f 20 4e 55  ayHaveNull to NU
21237 4c 4c 2e 20 20 43 61 6c 6c 69 6e 67 20 72 6f 75  LL.  Calling rou
21238 74 69 6e 65 73 20 77 69 6c 6c 20 74 61 6b 65 0a  tines will take.
21239 2a 2a 20 63 61 72 65 20 6f 66 20 63 68 61 6e 67  ** care of chang
2123a 69 6e 67 20 74 68 69 73 20 72 65 67 69 73 74 65  ing this registe
2123b 72 20 76 61 6c 75 65 20 74 6f 20 6e 6f 6e 2d 4e  r value to non-N
2123c 55 4c 4c 20 69 66 20 74 68 65 20 52 48 53 20 69  ULL if the RHS i
2123d 73 20 4e 55 4c 4c 2d 66 72 65 65 2e 0a 2a 2a 0a  s NULL-free..**.
2123e 2a 2a 20 49 66 20 72 4d 61 79 48 61 76 65 4e 75  ** If rMayHaveNu
2123f 6c 6c 20 69 73 20 7a 65 72 6f 2c 20 74 68 61 74  ll is zero, that
21240 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
21241 73 75 62 71 75 65 72 79 20 69 73 20 62 65 69 6e  subquery is bein
21242 67 20 75 73 65 64 0a 2a 2a 20 66 6f 72 20 6d 65  g used.** for me
21243 6d 62 65 72 73 68 69 70 20 74 65 73 74 69 6e 67  mbership testing
21244 20 6f 6e 6c 79 2e 20 20 54 68 65 72 65 20 69 73   only.  There is
21245 20 6e 6f 20 6e 65 65 64 20 74 6f 20 69 6e 69 74   no need to init
21246 69 61 6c 69 7a 65 20 61 6e 79 0a 2a 2a 20 72 65  ialize any.** re
21247 67 69 73 74 65 72 73 20 74 6f 20 69 6e 64 69 63  gisters to indic
21248 61 74 65 20 74 68 65 20 70 72 65 73 65 6e 73 65  ate the presense
21249 20 6f 72 20 61 62 73 65 6e 63 65 20 6f 66 20 4e   or absence of N
2124a 55 4c 4c 73 20 6f 6e 20 74 68 65 20 52 48 53 2e  ULLs on the RHS.
2124b 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 53 45 4c  .**.** For a SEL
2124c 45 43 54 20 6f 72 20 45 58 49 53 54 53 20 6f 70  ECT or EXISTS op
2124d 65 72 61 74 6f 72 2c 20 72 65 74 75 72 6e 20 74  erator, return t
2124e 68 65 20 72 65 67 69 73 74 65 72 20 74 68 61 74  he register that
2124f 20 68 6f 6c 64 73 20 74 68 65 0a 2a 2a 20 72 65   holds the.** re
21250 73 75 6c 74 2e 20 20 46 6f 72 20 49 4e 20 6f 70  sult.  For IN op
21251 65 72 61 74 6f 72 73 20 6f 72 20 69 66 20 61 6e  erators or if an
21252 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74   error occurs, t
21253 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
21254 69 73 20 30 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  is 0..*/.#ifndef
21255 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
21256 51 55 45 52 59 0a 53 51 4c 49 54 45 5f 50 52 49  QUERY.SQLITE_PRI
21257 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
21258 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28 0a 20  CodeSubselect(. 
21259 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2125a 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
2125b 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
2125c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20   Expr *pExpr,   
2125d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2125e 49 4e 2c 20 53 45 4c 45 43 54 2c 20 6f 72 20 45  IN, SELECT, or E
2125f 58 49 53 54 53 20 6f 70 65 72 61 74 6f 72 20 2a  XISTS operator *
21260 2f 0a 20 20 69 6e 74 20 72 4d 61 79 48 61 76 65  /.  int rMayHave
21261 4e 75 6c 6c 2c 20 20 20 20 20 20 20 2f 2a 20 52  Null,       /* R
21262 65 67 69 73 74 65 72 20 74 68 61 74 20 72 65 63  egister that rec
21263 6f 72 64 73 20 77 68 65 74 68 65 72 20 4e 55 4c  ords whether NUL
21264 4c 73 20 65 78 69 73 74 20 69 6e 20 52 48 53 20  Ls exist in RHS 
21265 2a 2f 0a 20 20 69 6e 74 20 69 73 52 6f 77 69 64  */.  int isRowid
21266 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
21267 49 66 20 74 72 75 65 2c 20 4c 48 53 20 6f 66 20  If true, LHS of 
21268 49 4e 20 6f 70 65 72 61 74 6f 72 20 69 73 20 61  IN operator is a
21269 20 72 6f 77 69 64 20 2a 2f 0a 29 7b 0a 20 20 69   rowid */.){.  i
2126a 6e 74 20 74 65 73 74 41 64 64 72 20 3d 20 30 3b  nt testAddr = 0;
2126b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2126c 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 2d 74 69         /* One-ti
2126d 6d 65 20 74 65 73 74 20 61 64 64 72 65 73 73 20  me test address 
2126e 2a 2f 0a 20 20 69 6e 74 20 72 52 65 67 20 3d 20  */.  int rReg = 
2126f 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
21270 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
21271 52 65 67 69 73 74 65 72 20 73 74 6f 72 69 6e 67  Register storing
21272 20 72 65 73 75 6c 74 69 6e 67 20 2a 2f 0a 20 20   resulting */.  
21273 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65  Vdbe *v = sqlite
21274 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
21275 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 76 3d  ;.  if( NEVER(v=
21276 3d 30 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  =0) ) return 0;.
21277 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
21278 68 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a  hePush(pParse);.
21279 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f 64 65 20  .  /* This code 
2127a 6d 75 73 74 20 62 65 20 72 75 6e 20 69 6e 20 69  must be run in i
2127b 74 73 20 65 6e 74 69 72 65 74 79 20 65 76 65 72  ts entirety ever
2127c 79 20 74 69 6d 65 20 69 74 20 69 73 20 65 6e 63  y time it is enc
2127d 6f 75 6e 74 65 72 65 64 0a 20 20 2a 2a 20 69 66  ountered.  ** if
2127e 20 61 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c   any of the foll
2127f 6f 77 69 6e 67 20 69 73 20 74 72 75 65 3a 0a 20  owing is true:. 
21280 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 2a 20 20 54   **.  **    *  T
21281 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69  he right-hand si
21282 64 65 20 69 73 20 61 20 63 6f 72 72 65 6c 61 74  de is a correlat
21283 65 64 20 73 75 62 71 75 65 72 79 0a 20 20 2a 2a  ed subquery.  **
21284 20 20 20 20 2a 20 20 54 68 65 20 72 69 67 68 74      *  The right
21285 2d 68 61 6e 64 20 73 69 64 65 20 69 73 20 61 6e  -hand side is an
21286 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
21287 20 63 6f 6e 74 61 69 6e 69 6e 67 20 76 61 72 69   containing vari
21288 61 62 6c 65 73 0a 20 20 2a 2a 20 20 20 20 2a 20  ables.  **    * 
21289 20 57 65 20 61 72 65 20 69 6e 73 69 64 65 20 61   We are inside a
2128a 20 74 72 69 67 67 65 72 0a 20 20 2a 2a 0a 20 20   trigger.  **.  
2128b 2a 2a 20 49 66 20 61 6c 6c 20 6f 66 20 74 68 65  ** If all of the
2128c 20 61 62 6f 76 65 20 61 72 65 20 66 61 6c 73 65   above are false
2128d 2c 20 74 68 65 6e 20 77 65 20 63 61 6e 20 72 75  , then we can ru
2128e 6e 20 74 68 69 73 20 63 6f 64 65 20 6a 75 73 74  n this code just
2128f 20 6f 6e 63 65 0a 20 20 2a 2a 20 73 61 76 65 20   once.  ** save 
21290 74 68 65 20 72 65 73 75 6c 74 73 2c 20 61 6e 64  the results, and
21291 20 72 65 75 73 65 20 74 68 65 20 73 61 6d 65 20   reuse the same 
21292 72 65 73 75 6c 74 20 6f 6e 20 73 75 62 73 65 71  result on subseq
21293 75 65 6e 74 20 69 6e 76 6f 63 61 74 69 6f 6e 73  uent invocations
21294 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 45 78  ..  */.  if( !Ex
21295 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
21296 28 70 45 78 70 72 2c 20 45 50 5f 56 61 72 53 65  (pExpr, EP_VarSe
21297 6c 65 63 74 29 20 26 26 20 21 70 50 61 72 73 65  lect) && !pParse
21298 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20 29 7b  ->pTriggerTab ){
21299 0a 20 20 20 20 69 6e 74 20 6d 65 6d 20 3d 20 2b  .    int mem = +
2129a 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
2129b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2129c 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 6d  dOp1(v, OP_If, m
2129d 65 6d 29 3b 0a 20 20 20 20 74 65 73 74 41 64 64  em);.    testAdd
2129e 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
2129f 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
212a0 67 65 72 2c 20 31 2c 20 6d 65 6d 29 3b 0a 20 20  ger, 1, mem);.  
212a1 20 20 61 73 73 65 72 74 28 20 74 65 73 74 41 64    assert( testAd
212a2 64 72 3e 30 20 7c 7c 20 70 50 61 72 73 65 2d 3e  dr>0 || pParse->
212a3 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
212a4 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63   );.  }..  switc
212a5 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a  h( pExpr->op ){.
212a6 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20      case TK_IN: 
212a7 7b 0a 20 20 20 20 20 20 63 68 61 72 20 61 66 66  {.      char aff
212a8 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 4b 65 79  inity;.      Key
212a9 49 6e 66 6f 20 6b 65 79 49 6e 66 6f 3b 0a 20 20  Info keyInfo;.  
212aa 20 20 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20      int addr;   
212ab 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
212ac 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  of OP_OpenEpheme
212ad 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  ral instruction 
212ae 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  */.      Expr *p
212af 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c  Left = pExpr->pL
212b0 65 66 74 3b 0a 0a 20 20 20 20 20 20 69 66 28 20  eft;..      if( 
212b1 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 29 7b 0a  rMayHaveNull ){.
212b2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
212b3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
212b4 4e 75 6c 6c 2c 20 30 2c 20 72 4d 61 79 48 61 76  Null, 0, rMayHav
212b5 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 7d 0a  eNull);.      }.
212b6 0a 20 20 20 20 20 20 61 66 66 69 6e 69 74 79 20  .      affinity 
212b7 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66  = sqlite3ExprAff
212b8 69 6e 69 74 79 28 70 4c 65 66 74 29 3b 0a 0a 20  inity(pLeft);.. 
212b9 20 20 20 20 20 2f 2a 20 57 68 65 74 68 65 72 20       /* Whether 
212ba 74 68 69 73 20 69 73 20 61 6e 20 27 78 20 49 4e  this is an 'x IN
212bb 28 53 45 4c 45 43 54 2e 2e 2e 29 27 20 6f 72 20  (SELECT...)' or 
212bc 61 6e 20 27 78 20 49 4e 28 3c 65 78 70 72 6c 69  an 'x IN(<exprli
212bd 73 74 3e 29 27 0a 20 20 20 20 20 20 2a 2a 20 65  st>)'.      ** e
212be 78 70 72 65 73 73 69 6f 6e 20 69 74 20 69 73 20  xpression it is 
212bf 68 61 6e 64 6c 65 64 20 74 68 65 20 73 61 6d 65  handled the same
212c0 20 77 61 79 2e 20 41 20 76 69 72 74 75 61 6c 20   way. A virtual 
212c1 74 61 62 6c 65 20 69 73 20 0a 20 20 20 20 20 20  table is .      
212c2 2a 2a 20 66 69 6c 6c 65 64 20 77 69 74 68 20 73  ** filled with s
212c3 69 6e 67 6c 65 2d 66 69 65 6c 64 20 69 6e 64 65  ingle-field inde
212c4 78 20 6b 65 79 73 20 72 65 70 72 65 73 65 6e 74  x keys represent
212c5 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 73 0a  ing the results.
212c6 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68        ** from th
212c7 65 20 53 45 4c 45 43 54 20 6f 72 20 74 68 65 20  e SELECT or the 
212c8 3c 65 78 70 72 6c 69 73 74 3e 2e 0a 20 20 20 20  <exprlist>..    
212c9 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 66    **.      ** If
212ca 20 74 68 65 20 27 78 27 20 65 78 70 72 65 73 73   the 'x' express
212cb 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20  ion is a column 
212cc 76 61 6c 75 65 2c 20 6f 72 20 74 68 65 20 53 45  value, or the SE
212cd 4c 45 43 54 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a  LECT....      **
212ce 20 73 74 61 74 65 6d 65 6e 74 20 72 65 74 75 72   statement retur
212cf 6e 73 20 61 20 63 6f 6c 75 6d 6e 20 76 61 6c 75  ns a column valu
212d0 65 2c 20 74 68 65 6e 20 74 68 65 20 61 66 66 69  e, then the affi
212d1 6e 69 74 79 20 6f 66 20 74 68 61 74 0a 20 20 20  nity of that.   
212d2 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 73 20     ** column is 
212d3 75 73 65 64 20 74 6f 20 62 75 69 6c 64 20 74 68  used to build th
212d4 65 20 69 6e 64 65 78 20 6b 65 79 73 2e 20 49 66  e index keys. If
212d5 20 62 6f 74 68 20 27 78 27 20 61 6e 64 20 74 68   both 'x' and th
212d6 65 0a 20 20 20 20 20 20 2a 2a 20 53 45 4c 45 43  e.      ** SELEC
212d7 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74 20 61  T... statement a
212d8 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e  re columns, then
212d9 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 74   numeric affinit
212da 79 20 69 73 20 75 73 65 64 0a 20 20 20 20 20 20  y is used.      
212db 2a 2a 20 69 66 20 65 69 74 68 65 72 20 63 6f 6c  ** if either col
212dc 75 6d 6e 20 68 61 73 20 4e 55 4d 45 52 49 43 20  umn has NUMERIC 
212dd 6f 72 20 49 4e 54 45 47 45 52 20 61 66 66 69 6e  or INTEGER affin
212de 69 74 79 2e 20 49 66 20 6e 65 69 74 68 65 72 0a  ity. If neither.
212df 20 20 20 20 20 20 2a 2a 20 27 78 27 20 6e 6f 72        ** 'x' nor
212e0 20 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e 20 73   the SELECT... s
212e1 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63 6f 6c  tatement are col
212e2 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d 65 72  umns, then numer
212e3 69 63 20 61 66 66 69 6e 69 74 79 0a 20 20 20 20  ic affinity.    
212e4 20 20 2a 2a 20 69 73 20 75 73 65 64 2e 0a 20 20    ** is used..  
212e5 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70 45 78      */.      pEx
212e6 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70 50 61  pr->iTable = pPa
212e7 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20  rse->nTab++;.   
212e8 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
212e9 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
212ea 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c  P_OpenEphemeral,
212eb 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20   pExpr->iTable, 
212ec 21 69 73 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  !isRowid);.     
212ed 20 6d 65 6d 73 65 74 28 26 6b 65 79 49 6e 66 6f   memset(&keyInfo
212ee 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6b 65 79 49  , 0, sizeof(keyI
212ef 6e 66 6f 29 29 3b 0a 20 20 20 20 20 20 6b 65 79  nfo));.      key
212f0 49 6e 66 6f 2e 6e 46 69 65 6c 64 20 3d 20 31 3b  Info.nField = 1;
212f1 0a 0a 20 20 20 20 20 20 69 66 28 20 45 78 70 72  ..      if( Expr
212f2 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
212f3 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
212f4 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43   ){.        /* C
212f5 61 73 65 20 31 3a 20 20 20 20 20 65 78 70 72 20  ase 1:     expr 
212f6 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 0a  IN (SELECT ...).
212f7 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
212f8 20 20 20 2a 2a 20 47 65 6e 65 72 61 74 65 20 63     ** Generate c
212f9 6f 64 65 20 74 6f 20 77 72 69 74 65 20 74 68 65  ode to write the
212fa 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20   results of the 
212fb 73 65 6c 65 63 74 20 69 6e 74 6f 20 74 68 65 20  select into the 
212fc 74 65 6d 70 6f 72 61 72 79 0a 20 20 20 20 20 20  temporary.      
212fd 20 20 2a 2a 20 74 61 62 6c 65 20 61 6c 6c 6f 63    ** table alloc
212fe 61 74 65 64 20 61 6e 64 20 6f 70 65 6e 65 64 20  ated and opened 
212ff 61 62 6f 76 65 2e 0a 20 20 20 20 20 20 20 20 2a  above..        *
21300 2f 0a 20 20 20 20 20 20 20 20 53 65 6c 65 63 74  /.        Select
21301 44 65 73 74 20 64 65 73 74 3b 0a 20 20 20 20 20  Dest dest;.     
21302 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c     ExprList *pEL
21303 69 73 74 3b 0a 0a 20 20 20 20 20 20 20 20 61 73  ist;..        as
21304 73 65 72 74 28 20 21 69 73 52 6f 77 69 64 20 29  sert( !isRowid )
21305 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
21306 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
21307 26 64 65 73 74 2c 20 53 52 54 5f 53 65 74 2c 20  &dest, SRT_Set, 
21308 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 29 3b 0a  pExpr->iTable);.
21309 20 20 20 20 20 20 20 20 64 65 73 74 2e 61 66 66          dest.aff
2130a 69 6e 69 74 79 20 3d 20 28 75 38 29 61 66 66 69  inity = (u8)affi
2130b 6e 69 74 79 3b 0a 20 20 20 20 20 20 20 20 61 73  nity;.        as
2130c 73 65 72 74 28 20 28 70 45 78 70 72 2d 3e 69 54  sert( (pExpr->iT
2130d 61 62 6c 65 26 30 78 30 30 30 30 46 46 46 46 29  able&0x0000FFFF)
2130e 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20  ==pExpr->iTable 
2130f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  );.        if( s
21310 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61  qlite3Select(pPa
21311 72 73 65 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53  rse, pExpr->x.pS
21312 65 6c 65 63 74 2c 20 26 64 65 73 74 29 20 29 7b  elect, &dest) ){
21313 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
21314 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  n 0;.        }. 
21315 20 20 20 20 20 20 20 70 45 4c 69 73 74 20 3d 20         pEList = 
21316 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74  pExpr->x.pSelect
21317 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 20 20  ->pEList;.      
21318 20 20 69 66 28 20 41 4c 57 41 59 53 28 70 45 4c    if( ALWAYS(pEL
21319 69 73 74 21 3d 30 20 26 26 20 70 45 4c 69 73 74  ist!=0 && pEList
2131a 2d 3e 6e 45 78 70 72 3e 30 29 20 29 7b 20 0a 20  ->nExpr>0) ){ . 
2131b 20 20 20 20 20 20 20 20 20 6b 65 79 49 6e 66 6f           keyInfo
2131c 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20 73 71 6c 69  .aColl[0] = sqli
2131d 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72 65  te3BinaryCompare
2131e 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
2131f 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 0a 20 20  pExpr->pLeft,.  
21320 20 20 20 20 20 20 20 20 20 20 20 20 70 45 4c 69              pELi
21321 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b  st->a[0].pExpr);
21322 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
21323 20 7d 65 6c 73 65 20 69 66 28 20 70 45 78 70 72   }else if( pExpr
21324 2d 3e 78 2e 70 4c 69 73 74 21 3d 30 20 29 7b 0a  ->x.pList!=0 ){.
21325 20 20 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20          /* Case 
21326 32 3a 20 20 20 20 20 65 78 70 72 20 49 4e 20 28  2:     expr IN (
21327 65 78 70 72 6c 69 73 74 29 0a 20 20 20 20 20 20  exprlist).      
21328 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20    **.        ** 
21329 46 6f 72 20 65 61 63 68 20 65 78 70 72 65 73 73  For each express
2132a 69 6f 6e 2c 20 62 75 69 6c 64 20 61 6e 20 69 6e  ion, build an in
2132b 64 65 78 20 6b 65 79 20 66 72 6f 6d 20 74 68 65  dex key from the
2132c 20 65 76 61 6c 75 61 74 69 6f 6e 20 61 6e 64 0a   evaluation and.
2132d 20 20 20 20 20 20 20 20 2a 2a 20 73 74 6f 72 65          ** store
2132e 20 69 74 20 69 6e 20 74 68 65 20 74 65 6d 70 6f   it in the tempo
2132f 72 61 72 79 20 74 61 62 6c 65 2e 20 49 66 20 3c  rary table. If <
21330 65 78 70 72 3e 20 69 73 20 61 20 63 6f 6c 75 6d  expr> is a colum
21331 6e 2c 20 74 68 65 6e 20 75 73 65 0a 20 20 20 20  n, then use.    
21332 20 20 20 20 2a 2a 20 74 68 61 74 20 63 6f 6c 75      ** that colu
21333 6d 6e 73 20 61 66 66 69 6e 69 74 79 20 77 68 65  mns affinity whe
21334 6e 20 62 75 69 6c 64 69 6e 67 20 69 6e 64 65 78  n building index
21335 20 6b 65 79 73 2e 20 49 66 20 3c 65 78 70 72 3e   keys. If <expr>
21336 20 69 73 20 6e 6f 74 0a 20 20 20 20 20 20 20 20   is not.        
21337 2a 2a 20 61 20 63 6f 6c 75 6d 6e 2c 20 75 73 65  ** a column, use
21338 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e 69 74   numeric affinit
21339 79 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  y..        */.  
2133a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
2133b 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70       ExprList *p
2133c 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e  List = pExpr->x.
2133d 70 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20 73  pList;.        s
2133e 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
2133f 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20  tem *pItem;.    
21340 20 20 20 20 69 6e 74 20 72 31 2c 20 72 32 2c 20      int r1, r2, 
21341 72 33 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28  r3;..        if(
21342 20 21 61 66 66 69 6e 69 74 79 20 29 7b 0a 20 20   !affinity ){.  
21343 20 20 20 20 20 20 20 20 61 66 66 69 6e 69 74 79          affinity
21344 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f   = SQLITE_AFF_NO
21345 4e 45 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  NE;.        }.  
21346 20 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43        keyInfo.aC
21347 6f 6c 6c 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33  oll[0] = sqlite3
21348 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72  ExprCollSeq(pPar
21349 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  se, pExpr->pLeft
2134a 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4c  );..        /* L
2134b 6f 6f 70 20 74 68 72 6f 75 67 68 20 65 61 63 68  oop through each
2134c 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 3c   expression in <
2134d 65 78 70 72 6c 69 73 74 3e 2e 20 2a 2f 0a 20 20  exprlist>. */.  
2134e 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74        r1 = sqlit
2134f 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
21350 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 72 32  rse);.        r2
21351 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
21352 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
21353 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21354 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
21355 6c 6c 2c 20 30 2c 20 72 32 29 3b 0a 20 20 20 20  ll, 0, r2);.    
21356 20 20 20 20 66 6f 72 28 69 3d 70 4c 69 73 74 2d      for(i=pList-
21357 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d 3d 70 4c  >nExpr, pItem=pL
21358 69 73 74 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d  ist->a; i>0; i--
21359 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
2135a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 32 20        Expr *pE2 
2135b 3d 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a  = pItem->pExpr;.
2135c 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 56            int iV
2135d 61 6c 54 6f 49 6e 73 3b 0a 0a 20 20 20 20 20 20  alToIns;..      
2135e 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 65 78      /* If the ex
2135f 70 72 65 73 73 69 6f 6e 20 69 73 20 6e 6f 74 20  pression is not 
21360 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20 77 65  constant then we
21361 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 0a 20 20   will need to.  
21362 20 20 20 20 20 20 20 20 2a 2a 20 64 69 73 61 62          ** disab
21363 6c 65 20 74 68 65 20 74 65 73 74 20 74 68 61 74  le the test that
21364 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 61   was generated a
21365 62 6f 76 65 20 74 68 61 74 20 6d 61 6b 65 73 20  bove that makes 
21366 73 75 72 65 0a 20 20 20 20 20 20 20 20 20 20 2a  sure.          *
21367 2a 20 74 68 69 73 20 63 6f 64 65 20 6f 6e 6c 79  * this code only
21368 20 65 78 65 63 75 74 65 73 20 6f 6e 63 65 2e 20   executes once. 
21369 20 42 65 63 61 75 73 65 20 66 6f 72 20 61 20 6e   Because for a n
2136a 6f 6e 2d 63 6f 6e 73 74 61 6e 74 0a 20 20 20 20  on-constant.    
2136b 20 20 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73        ** express
2136c 69 6f 6e 20 77 65 20 6e 65 65 64 20 74 6f 20 72  ion we need to r
2136d 65 72 75 6e 20 74 68 69 73 20 63 6f 64 65 20 65  erun this code e
2136e 61 63 68 20 74 69 6d 65 2e 0a 20 20 20 20 20 20  ach time..      
2136f 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20      */.         
21370 20 69 66 28 20 74 65 73 74 41 64 64 72 20 26 26   if( testAddr &&
21371 20 21 73 71 6c 69 74 65 33 45 78 70 72 49 73 43   !sqlite3ExprIsC
21372 6f 6e 73 74 61 6e 74 28 70 45 32 29 20 29 7b 0a  onstant(pE2) ){.
21373 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
21374 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e  te3VdbeChangeToN
21375 6f 6f 70 28 76 2c 20 74 65 73 74 41 64 64 72 2d  oop(v, testAddr-
21376 31 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 20  1, 2);.         
21377 20 20 20 74 65 73 74 41 64 64 72 20 3d 20 30 3b     testAddr = 0;
21378 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20  .          }..  
21379 20 20 20 20 20 20 20 20 2f 2a 20 45 76 61 6c 75          /* Evalu
2137a 61 74 65 20 74 68 65 20 65 78 70 72 65 73 73 69  ate the expressi
2137b 6f 6e 20 61 6e 64 20 69 6e 73 65 72 74 20 69 74  on and insert it
2137c 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70 20 74   into the temp t
2137d 61 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20  able */.        
2137e 20 20 69 66 28 20 69 73 52 6f 77 69 64 20 26 26    if( isRowid &&
2137f 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e   sqlite3ExprIsIn
21380 74 65 67 65 72 28 70 45 32 2c 20 26 69 56 61 6c  teger(pE2, &iVal
21381 54 6f 49 6e 73 29 20 29 7b 0a 20 20 20 20 20 20  ToIns) ){.      
21382 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21383 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e  eAddOp3(v, OP_In
21384 73 65 72 74 49 6e 74 2c 20 70 45 78 70 72 2d 3e  sertInt, pExpr->
21385 69 54 61 62 6c 65 2c 20 72 32 2c 20 69 56 61 6c  iTable, r2, iVal
21386 54 6f 49 6e 73 29 3b 0a 20 20 20 20 20 20 20 20  ToIns);.        
21387 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
21388 20 20 20 20 20 72 33 20 3d 20 73 71 6c 69 74 65       r3 = sqlite
21389 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28  3ExprCodeTarget(
2138a 70 50 61 72 73 65 2c 20 70 45 32 2c 20 72 31 29  pParse, pE2, r1)
2138b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  ;.            if
2138c 28 20 69 73 52 6f 77 69 64 20 29 7b 0a 20 20 20  ( isRowid ){.   
2138d 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2138e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2138f 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72 33  OP_MustBeInt, r3
21390 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
21391 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21392 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72    sqlite3VdbeCur
21393 72 65 6e 74 41 64 64 72 28 76 29 2b 32 29 3b 0a  rentAddr(v)+2);.
21394 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
21395 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
21396 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 70 45  v, OP_Insert, pE
21397 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20 72 32 2c  xpr->iTable, r2,
21398 20 72 33 29 3b 0a 20 20 20 20 20 20 20 20 20 20   r3);.          
21399 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2139a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2139b 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d  beAddOp4(v, OP_M
2139c 61 6b 65 52 65 63 6f 72 64 2c 20 72 33 2c 20 31  akeRecord, r3, 1
2139d 2c 20 72 32 2c 20 26 61 66 66 69 6e 69 74 79 2c  , r2, &affinity,
2139e 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   1);.           
2139f 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
213a0 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67  cheAffinityChang
213a1 65 28 70 50 61 72 73 65 2c 20 72 33 2c 20 31 29  e(pParse, r3, 1)
213a2 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
213a3 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
213a4 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65 72  2(v, OP_IdxInser
213a5 74 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  t, pExpr->iTable
213a6 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20 20  , r2);.         
213a7 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
213a8 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
213a9 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
213aa 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
213ab 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71   r1);.        sq
213ac 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
213ad 52 65 67 28 70 50 61 72 73 65 2c 20 72 32 29 3b  Reg(pParse, r2);
213ae 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
213af 66 28 20 21 69 73 52 6f 77 69 64 20 29 7b 0a 20  f( !isRowid ){. 
213b0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
213b1 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64  beChangeP4(v, ad
213b2 64 72 2c 20 28 76 6f 69 64 20 2a 29 26 6b 65 79  dr, (void *)&key
213b3 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f  Info, P4_KEYINFO
213b4 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
213b5 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
213b6 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49 53 54     case TK_EXIST
213b7 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53  S:.    case TK_S
213b8 45 4c 45 43 54 3a 0a 20 20 20 20 64 65 66 61 75  ELECT:.    defau
213b9 6c 74 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 49  lt: {.      /* I
213ba 66 20 74 68 69 73 20 68 61 73 20 74 6f 20 62 65  f this has to be
213bb 20 61 20 73 63 61 6c 61 72 20 53 45 4c 45 43 54   a scalar SELECT
213bc 2e 20 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  .  Generate code
213bd 20 74 6f 20 70 75 74 20 74 68 65 0a 20 20 20 20   to put the.    
213be 20 20 2a 2a 20 76 61 6c 75 65 20 6f 66 20 74 68    ** value of th
213bf 69 73 20 73 65 6c 65 63 74 20 69 6e 20 61 20 6d  is select in a m
213c0 65 6d 6f 72 79 20 63 65 6c 6c 20 61 6e 64 20 72  emory cell and r
213c1 65 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 65 72  ecord the number
213c2 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65  .      ** of the
213c3 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69 6e 20   memory cell in 
213c4 69 43 6f 6c 75 6d 6e 2e 20 20 49 66 20 74 68 69  iColumn.  If thi
213c5 73 20 69 73 20 61 6e 20 45 58 49 53 54 53 2c 20  s is an EXISTS, 
213c6 77 72 69 74 65 0a 20 20 20 20 20 20 2a 2a 20 61  write.      ** a
213c7 6e 20 69 6e 74 65 67 65 72 20 30 20 28 6e 6f 74  n integer 0 (not
213c8 20 65 78 69 73 74 73 29 20 6f 72 20 31 20 28 65   exists) or 1 (e
213c9 78 69 73 74 73 29 20 69 6e 74 6f 20 61 20 6d 65  xists) into a me
213ca 6d 6f 72 79 20 63 65 6c 6c 0a 20 20 20 20 20 20  mory cell.      
213cb 2a 2a 20 61 6e 64 20 72 65 63 6f 72 64 20 74 68  ** and record th
213cc 61 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69  at memory cell i
213cd 6e 20 69 43 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20  n iColumn..     
213ce 20 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63   */.      static
213cf 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 6f 6e 65   const Token one
213d0 20 3d 20 7b 20 22 31 22 2c 20 31 20 7d 3b 20 20   = { "1", 1 };  
213d1 2f 2a 20 54 6f 6b 65 6e 20 66 6f 72 20 6c 69 74  /* Token for lit
213d2 65 72 61 6c 20 76 61 6c 75 65 20 31 20 2a 2f 0a  eral value 1 */.
213d3 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53        Select *pS
213d4 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  el;             
213d5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
213d6 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
213d7 74 6f 20 65 6e 63 6f 64 65 20 2a 2f 0a 20 20 20  to encode */.   
213d8 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65     SelectDest de
213d9 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  st;             
213da 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20           /* How 
213db 74 6f 20 64 65 61 6c 20 77 69 74 68 20 53 45 4c  to deal with SEL
213dc 45 43 74 20 72 65 73 75 6c 74 20 2a 2f 0a 0a 20  ECt result */.. 
213dd 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
213de 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49  Expr->op==TK_EXI
213df 53 54 53 20 29 3b 0a 20 20 20 20 20 20 74 65 73  STS );.      tes
213e0 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70  tcase( pExpr->op
213e1 3d 3d 54 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 20  ==TK_SELECT );. 
213e2 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78       assert( pEx
213e3 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 49 53 54  pr->op==TK_EXIST
213e4 53 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d  S || pExpr->op==
213e5 54 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 0a 20 20  TK_SELECT );..  
213e6 20 20 20 20 61 73 73 65 72 74 28 20 45 78 70 72      assert( Expr
213e7 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
213e8 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
213e9 20 29 3b 0a 20 20 20 20 20 20 70 53 65 6c 20 3d   );.      pSel =
213ea 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63   pExpr->x.pSelec
213eb 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  t;.      sqlite3
213ec 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26  SelectDestInit(&
213ed 64 65 73 74 2c 20 30 2c 20 2b 2b 70 50 61 72 73  dest, 0, ++pPars
213ee 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 20 20  e->nMem);.      
213ef 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  if( pExpr->op==T
213f0 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 20  K_SELECT ){.    
213f1 20 20 20 20 64 65 73 74 2e 65 44 65 73 74 20 3d      dest.eDest =
213f2 20 53 52 54 5f 4d 65 6d 3b 0a 20 20 20 20 20 20   SRT_Mem;.      
213f3 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
213f4 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
213f5 30 2c 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 0a  0, dest.iParm);.
213f6 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d          VdbeComm
213f7 65 6e 74 28 28 76 2c 20 22 49 6e 69 74 20 73 75  ent((v, "Init su
213f8 62 71 75 65 72 79 20 72 65 73 75 6c 74 22 29 29  bquery result"))
213f9 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
213fa 20 20 20 20 20 20 20 64 65 73 74 2e 65 44 65 73         dest.eDes
213fb 74 20 3d 20 53 52 54 5f 45 78 69 73 74 73 3b 0a  t = SRT_Exists;.
213fc 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
213fd 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
213fe 49 6e 74 65 67 65 72 2c 20 30 2c 20 64 65 73 74  Integer, 0, dest
213ff 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 20  .iParm);.       
21400 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
21401 20 22 49 6e 69 74 20 45 58 49 53 54 53 20 72 65   "Init EXISTS re
21402 73 75 6c 74 22 29 29 3b 0a 20 20 20 20 20 20 7d  sult"));.      }
21403 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
21404 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  prDelete(pParse-
21405 3e 64 62 2c 20 70 53 65 6c 2d 3e 70 4c 69 6d 69  >db, pSel->pLimi
21406 74 29 3b 0a 20 20 20 20 20 20 70 53 65 6c 2d 3e  t);.      pSel->
21407 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74 65 33  pLimit = sqlite3
21408 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
21409 5f 49 4e 54 45 47 45 52 2c 20 30 2c 20 30 2c 20  _INTEGER, 0, 0, 
2140a 26 6f 6e 65 29 3b 0a 20 20 20 20 20 20 69 66 28  &one);.      if(
2140b 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
2140c 50 61 72 73 65 2c 20 70 53 65 6c 2c 20 26 64 65  Parse, pSel, &de
2140d 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 72  st) ){.        r
2140e 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d  eturn 0;.      }
2140f 0a 20 20 20 20 20 20 72 52 65 67 20 3d 20 64 65  .      rReg = de
21410 73 74 2e 69 50 61 72 6d 3b 0a 20 20 20 20 20 20  st.iParm;.      
21411 45 78 70 72 53 65 74 49 72 72 65 64 75 63 69 62  ExprSetIrreducib
21412 6c 65 28 70 45 78 70 72 29 3b 0a 20 20 20 20 20  le(pExpr);.     
21413 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
21414 7d 0a 0a 20 20 69 66 28 20 74 65 73 74 41 64 64  }..  if( testAdd
21415 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  r ){.    sqlite3
21416 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
21417 74 65 73 74 41 64 64 72 2d 31 29 3b 0a 20 20 7d  testAddr-1);.  }
21418 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61  .  sqlite3ExprCa
21419 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31  chePop(pParse, 1
2141a 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 72 52 65  );..  return rRe
2141b 67 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  g;.}.#endif /* S
2141c 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
2141d 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 75  ERY */../*.** Du
2141e 70 6c 69 63 61 74 65 20 61 6e 20 38 2d 62 79 74  plicate an 8-byt
2141f 65 20 76 61 6c 75 65 0a 2a 2f 0a 73 74 61 74 69  e value.*/.stati
21420 63 20 63 68 61 72 20 2a 64 75 70 38 62 79 74 65  c char *dup8byte
21421 73 28 56 64 62 65 20 2a 76 2c 20 63 6f 6e 73 74  s(Vdbe *v, const
21422 20 63 68 61 72 20 2a 69 6e 29 7b 0a 20 20 63 68   char *in){.  ch
21423 61 72 20 2a 6f 75 74 20 3d 20 73 71 6c 69 74 65  ar *out = sqlite
21424 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73 71 6c  3DbMallocRaw(sql
21425 69 74 65 33 56 64 62 65 44 62 28 76 29 2c 20 38  ite3VdbeDb(v), 8
21426 29 3b 0a 20 20 69 66 28 20 6f 75 74 20 29 7b 0a  );.  if( out ){.
21427 20 20 20 20 6d 65 6d 63 70 79 28 6f 75 74 2c 20      memcpy(out, 
21428 69 6e 2c 20 38 29 3b 0a 20 20 7d 0a 20 20 72 65  in, 8);.  }.  re
21429 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a  turn out;.}../*.
2142a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20 69  ** Generate an i
2142b 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61 74 20  nstruction that 
2142c 77 69 6c 6c 20 70 75 74 20 74 68 65 20 66 6c 6f  will put the flo
2142d 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 76  ating point.** v
2142e 61 6c 75 65 20 64 65 73 63 72 69 62 65 64 20 62  alue described b
2142f 79 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e 74 6f  y z[0..n-1] into
21430 20 72 65 67 69 73 74 65 72 20 69 4d 65 6d 2e 0a   register iMem..
21431 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20 73 74  **.** The z[] st
21432 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62 61 62  ring will probab
21433 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f 2d 74  ly not be zero-t
21434 65 72 6d 69 6e 61 74 65 64 2e 20 20 42 75 74 20  erminated.  But 
21435 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63 68 61  the .** z[n] cha
21436 72 61 63 74 65 72 20 69 73 20 67 75 61 72 61 6e  racter is guaran
21437 74 65 65 64 20 74 6f 20 62 65 20 73 6f 6d 65 74  teed to be somet
21438 68 69 6e 67 20 74 68 61 74 20 64 6f 65 73 20 6e  hing that does n
21439 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b 65 20  ot look.** like 
2143a 74 68 65 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e  the continuation
2143b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 2e 0a   of the number..
2143c 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
2143d 6f 64 65 52 65 61 6c 28 56 64 62 65 20 2a 76 2c  odeReal(Vdbe *v,
2143e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
2143f 69 6e 74 20 6e 65 67 61 74 65 46 6c 61 67 2c 20  int negateFlag, 
21440 69 6e 74 20 69 4d 65 6d 29 7b 0a 20 20 69 66 28  int iMem){.  if(
21441 20 41 4c 57 41 59 53 28 7a 21 3d 30 29 20 29 7b   ALWAYS(z!=0) ){
21442 0a 20 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 75  .    double valu
21443 65 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 56 3b  e;.    char *zV;
21444 0a 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46  .    sqlite3AtoF
21445 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 20  (z, &value);.   
21446 20 61 73 73 65 72 74 28 20 21 73 71 6c 69 74 65   assert( !sqlite
21447 33 49 73 4e 61 4e 28 76 61 6c 75 65 29 20 29 3b  3IsNaN(value) );
21448 20 2f 2a 20 54 68 65 20 6e 65 77 20 41 74 6f 46   /* The new AtoF
21449 20 6e 65 76 65 72 20 72 65 74 75 72 6e 73 20 4e   never returns N
2144a 61 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 65  aN */.    if( ne
2144b 67 61 74 65 46 6c 61 67 20 29 20 76 61 6c 75 65  gateFlag ) value
2144c 20 3d 20 2d 76 61 6c 75 65 3b 0a 20 20 20 20 7a   = -value;.    z
2144d 56 20 3d 20 64 75 70 38 62 79 74 65 73 28 76 2c  V = dup8bytes(v,
2144e 20 28 63 68 61 72 2a 29 26 76 61 6c 75 65 29 3b   (char*)&value);
2144f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
21450 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 52 65 61  AddOp4(v, OP_Rea
21451 6c 2c 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a  l, 0, iMem, 0, z
21452 56 2c 20 50 34 5f 52 45 41 4c 29 3b 0a 20 20 7d  V, P4_REAL);.  }
21453 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  .}.../*.** Gener
21454 61 74 65 20 61 6e 20 69 6e 73 74 72 75 63 74 69  ate an instructi
21455 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 70 75 74  on that will put
21456 20 74 68 65 20 69 6e 74 65 67 65 72 20 64 65 73   the integer des
21457 63 72 69 62 65 20 62 79 0a 2a 2a 20 74 65 78 74  cribe by.** text
21458 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e 74 6f 20   z[0..n-1] into 
21459 72 65 67 69 73 74 65 72 20 69 4d 65 6d 2e 0a 2a  register iMem..*
2145a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20 73 74 72  *.** The z[] str
2145b 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62 61 62 6c  ing will probabl
2145c 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f 2d 74 65  y not be zero-te
2145d 72 6d 69 6e 61 74 65 64 2e 20 20 42 75 74 20 74  rminated.  But t
2145e 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63 68 61 72  he .** z[n] char
2145f 61 63 74 65 72 20 69 73 20 67 75 61 72 61 6e 74  acter is guarant
21460 65 65 64 20 74 6f 20 62 65 20 73 6f 6d 65 74 68  eed to be someth
21461 69 6e 67 20 74 68 61 74 20 64 6f 65 73 20 6e 6f  ing that does no
21462 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b 65 20 74  t look.** like t
21463 68 65 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e 20  he continuation 
21464 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 2e 0a 2a  of the number..*
21465 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  /.static void co
21466 64 65 49 6e 74 65 67 65 72 28 56 64 62 65 20 2a  deInteger(Vdbe *
21467 76 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20  v, Expr *pExpr, 
21468 69 6e 74 20 6e 65 67 46 6c 61 67 2c 20 69 6e 74  int negFlag, int
21469 20 69 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70 45   iMem){.  if( pE
2146a 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f  xpr->flags & EP_
2146b 49 6e 74 56 61 6c 75 65 20 29 7b 0a 20 20 20 20  IntValue ){.    
2146c 69 6e 74 20 69 20 3d 20 70 45 78 70 72 2d 3e 75  int i = pExpr->u
2146d 2e 69 56 61 6c 75 65 3b 0a 20 20 20 20 69 66 28  .iValue;.    if(
2146e 20 6e 65 67 46 6c 61 67 20 29 20 69 20 3d 20 2d   negFlag ) i = -
2146f 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  i;.    sqlite3Vd
21470 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
21471 6e 74 65 67 65 72 2c 20 69 2c 20 69 4d 65 6d 29  nteger, i, iMem)
21472 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63  ;.  }else{.    c
21473 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70  onst char *z = p
21474 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a  Expr->u.zToken;.
21475 20 20 20 20 61 73 73 65 72 74 28 20 7a 21 3d 30      assert( z!=0
21476 20 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   );.    if( sqli
21477 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28  te3FitsIn64Bits(
21478 7a 2c 20 6e 65 67 46 6c 61 67 29 20 29 7b 0a 20  z, negFlag) ){. 
21479 20 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a       i64 value;.
2147a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 56 3b 0a        char *zV;.
2147b 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f        sqlite3Ato
2147c 69 36 34 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a  i64(z, &value);.
2147d 20 20 20 20 20 20 69 66 28 20 6e 65 67 46 6c 61        if( negFla
2147e 67 20 29 20 76 61 6c 75 65 20 3d 20 2d 76 61 6c  g ) value = -val
2147f 75 65 3b 0a 20 20 20 20 20 20 7a 56 20 3d 20 64  ue;.      zV = d
21480 75 70 38 62 79 74 65 73 28 76 2c 20 28 63 68 61  up8bytes(v, (cha
21481 72 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20 20 20  r*)&value);.    
21482 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
21483 4f 70 34 28 76 2c 20 4f 50 5f 49 6e 74 36 34 2c  Op4(v, OP_Int64,
21484 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a 56 2c   0, iMem, 0, zV,
21485 20 50 34 5f 49 4e 54 36 34 29 3b 0a 20 20 20 20   P4_INT64);.    
21486 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 6f 64  }else{.      cod
21487 65 52 65 61 6c 28 76 2c 20 7a 2c 20 6e 65 67 46  eReal(v, z, negF
21488 6c 61 67 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20  lag, iMem);.    
21489 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  }.  }.}../*.** C
2148a 6c 65 61 72 20 61 20 63 61 63 68 65 20 65 6e 74  lear a cache ent
2148b 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ry..*/.static vo
2148c 69 64 20 63 61 63 68 65 45 6e 74 72 79 43 6c 65  id cacheEntryCle
2148d 61 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ar(Parse *pParse
2148e 2c 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63  , struct yColCac
2148f 68 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d  he *p){.  if( p-
21490 3e 74 65 6d 70 52 65 67 20 29 7b 0a 20 20 20 20  >tempReg ){.    
21491 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 65 6d  if( pParse->nTem
21492 70 52 65 67 3c 41 72 72 61 79 53 69 7a 65 28 70  pReg<ArraySize(p
21493 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 29  Parse->aTempReg)
21494 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65   ){.      pParse
21495 2d 3e 61 54 65 6d 70 52 65 67 5b 70 50 61 72 73  ->aTempReg[pPars
21496 65 2d 3e 6e 54 65 6d 70 52 65 67 2b 2b 5d 20 3d  e->nTempReg++] =
21497 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 7d 0a   p->iReg;.    }.
21498 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d      p->tempReg =
21499 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a   0;.  }.}.../*.*
2149a 2a 20 52 65 63 6f 72 64 20 69 6e 20 74 68 65 20  * Record in the 
2149b 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 74 68 61  column cache tha
2149c 74 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 63  t a particular c
2149d 6f 6c 75 6d 6e 20 66 72 6f 6d 20 61 0a 2a 2a 20  olumn from a.** 
2149e 70 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65  particular table
2149f 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61 20   is stored in a 
214a0 70 61 72 74 69 63 75 6c 61 72 20 72 65 67 69 73  particular regis
214a1 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ter..*/.SQLITE_P
214a2 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
214a3 74 65 33 45 78 70 72 43 61 63 68 65 53 74 6f 72  te3ExprCacheStor
214a4 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
214a5 20 69 6e 74 20 69 54 61 62 2c 20 69 6e 74 20 69   int iTab, int i
214a6 43 6f 6c 2c 20 69 6e 74 20 69 52 65 67 29 7b 0a  Col, int iReg){.
214a7 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6d    int i;.  int m
214a8 69 6e 4c 72 75 3b 0a 20 20 69 6e 74 20 69 64 78  inLru;.  int idx
214a9 4c 72 75 3b 0a 20 20 73 74 72 75 63 74 20 79 43  Lru;.  struct yC
214aa 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 0a 20 20 61  olCache *p;..  a
214ab 73 73 65 72 74 28 20 69 52 65 67 3e 30 20 29 3b  ssert( iReg>0 );
214ac 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 6e 75    /* Register nu
214ad 6d 62 65 72 73 20 61 72 65 20 61 6c 77 61 79 73  mbers are always
214ae 20 70 6f 73 69 74 69 76 65 20 2a 2f 0a 20 20 61   positive */.  a
214af 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d 2d 31 20  ssert( iCol>=-1 
214b0 26 26 20 69 43 6f 6c 3c 33 32 37 36 38 20 29 3b  && iCol<32768 );
214b1 20 20 2f 2a 20 46 69 6e 69 74 65 20 63 6f 6c 75    /* Finite colu
214b2 6d 6e 20 6e 75 6d 62 65 72 73 20 2a 2f 0a 0a 20  mn numbers */.. 
214b3 20 2f 2a 20 46 69 72 73 74 20 72 65 70 6c 61 63   /* First replac
214b4 65 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 65  e any existing e
214b5 6e 74 72 79 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  ntry */.  for(i=
214b6 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f  0, p=pParse->aCo
214b7 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45  lCache; i<SQLITE
214b8 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b  _N_COLCACHE; i++
214b9 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  , p++){.    if( 
214ba 70 2d 3e 69 52 65 67 20 26 26 20 70 2d 3e 69 54  p->iReg && p->iT
214bb 61 62 6c 65 3d 3d 69 54 61 62 20 26 26 20 70 2d  able==iTab && p-
214bc 3e 69 43 6f 6c 75 6d 6e 3d 3d 69 43 6f 6c 20 29  >iColumn==iCol )
214bd 7b 0a 20 20 20 20 20 20 63 61 63 68 65 45 6e 74  {.      cacheEnt
214be 72 79 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20  ryClear(pParse, 
214bf 70 29 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4c 65  p);.      p->iLe
214c0 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69 43  vel = pParse->iC
214c1 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20 20 20  acheLevel;.     
214c2 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b   p->iReg = iReg;
214c3 0a 20 20 20 20 20 20 70 2d 3e 61 66 66 43 68 61  .      p->affCha
214c4 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  nge = 0;.      p
214c5 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e  ->lru = pParse->
214c6 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20  iCacheCnt++;.   
214c7 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
214c8 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 64 20  .  }..  /* Find 
214c9 61 6e 20 65 6d 70 74 79 20 73 6c 6f 74 20 61 6e  an empty slot an
214ca 64 20 72 65 70 6c 61 63 65 20 69 74 20 2a 2f 0a  d replace it */.
214cb 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61    for(i=0, p=pPa
214cc 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20  rse->aColCache; 
214cd 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41  i<SQLITE_N_COLCA
214ce 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a  CHE; i++, p++){.
214cf 20 20 20 20 69 66 28 20 70 2d 3e 69 52 65 67 3d      if( p->iReg=
214d0 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69  =0 ){.      p->i
214d1 4c 65 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e  Level = pParse->
214d2 69 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20  iCacheLevel;.   
214d3 20 20 20 70 2d 3e 69 54 61 62 6c 65 20 3d 20 69     p->iTable = i
214d4 54 61 62 3b 0a 20 20 20 20 20 20 70 2d 3e 69 43  Tab;.      p->iC
214d5 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20  olumn = iCol;.  
214d6 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 69 52      p->iReg = iR
214d7 65 67 3b 0a 20 20 20 20 20 20 70 2d 3e 61 66 66  eg;.      p->aff
214d8 43 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 20 20  Change = 0;.    
214d9 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d 20 30    p->tempReg = 0
214da 3b 0a 20 20 20 20 20 20 70 2d 3e 6c 72 75 20 3d  ;.      p->lru =
214db 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 43   pParse->iCacheC
214dc 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75  nt++;.      retu
214dd 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  rn;.    }.  }.. 
214de 20 2f 2a 20 52 65 70 6c 61 63 65 20 74 68 65 20   /* Replace the 
214df 6c 61 73 74 20 72 65 63 65 6e 74 6c 79 20 75 73  last recently us
214e0 65 64 20 2a 2f 0a 20 20 6d 69 6e 4c 72 75 20 3d  ed */.  minLru =
214e1 20 30 78 37 66 66 66 66 66 66 66 3b 0a 20 20 69   0x7fffffff;.  i
214e2 64 78 4c 72 75 20 3d 20 2d 31 3b 0a 20 20 66 6f  dxLru = -1;.  fo
214e3 72 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d  r(i=0, p=pParse-
214e4 3e 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51  >aColCache; i<SQ
214e5 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b  LITE_N_COLCACHE;
214e6 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20   i++, p++){.    
214e7 69 66 28 20 70 2d 3e 6c 72 75 3c 6d 69 6e 4c 72  if( p->lru<minLr
214e8 75 20 29 7b 0a 20 20 20 20 20 20 69 64 78 4c 72  u ){.      idxLr
214e9 75 20 3d 20 69 3b 0a 20 20 20 20 20 20 6d 69 6e  u = i;.      min
214ea 4c 72 75 20 3d 20 70 2d 3e 6c 72 75 3b 0a 20 20  Lru = p->lru;.  
214eb 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 41 4c    }.  }.  if( AL
214ec 57 41 59 53 28 69 64 78 4c 72 75 3e 3d 30 29 20  WAYS(idxLru>=0) 
214ed 29 7b 0a 20 20 20 20 70 20 3d 20 26 70 50 61 72  ){.    p = &pPar
214ee 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 69 64  se->aColCache[id
214ef 78 4c 72 75 5d 3b 0a 20 20 20 20 70 2d 3e 69 4c  xLru];.    p->iL
214f0 65 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69  evel = pParse->i
214f1 43 61 63 68 65 4c 65 76 65 6c 3b 0a 20 20 20 20  CacheLevel;.    
214f2 70 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62  p->iTable = iTab
214f3 3b 0a 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d 6e  ;.    p->iColumn
214f4 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 70 2d 3e   = iCol;.    p->
214f5 69 52 65 67 20 3d 20 69 52 65 67 3b 0a 20 20 20  iReg = iReg;.   
214f6 20 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d 20   p->affChange = 
214f7 30 3b 0a 20 20 20 20 70 2d 3e 74 65 6d 70 52 65  0;.    p->tempRe
214f8 67 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 6c 72  g = 0;.    p->lr
214f9 75 20 3d 20 70 50 61 72 73 65 2d 3e 69 43 61 63  u = pParse->iCac
214fa 68 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 72 65 74  heCnt++;.    ret
214fb 75 72 6e 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  urn;.  }.}../*.*
214fc 2a 20 49 6e 64 69 63 61 74 65 20 74 68 61 74 20  * Indicate that 
214fd 61 20 72 65 67 69 73 74 65 72 20 69 73 20 62 65  a register is be
214fe 69 6e 67 20 6f 76 65 72 77 72 69 74 74 65 6e 2e  ing overwritten.
214ff 20 20 50 75 72 67 65 20 74 68 65 20 72 65 67 69    Purge the regi
21500 73 74 65 72 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  ster.** from the
21501 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 2e 0a 2a   column cache..*
21502 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
21503 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
21504 72 43 61 63 68 65 52 65 6d 6f 76 65 28 50 61 72  rCacheRemove(Par
21505 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
21506 69 52 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  iReg){.  int i;.
21507 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63    struct yColCac
21508 68 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30  he *p;.  for(i=0
21509 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c  , p=pParse->aCol
2150a 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f  Cache; i<SQLITE_
2150b 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c  N_COLCACHE; i++,
2150c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70   p++){.    if( p
2150d 2d 3e 69 52 65 67 3d 3d 69 52 65 67 20 29 7b 0a  ->iReg==iReg ){.
2150e 20 20 20 20 20 20 63 61 63 68 65 45 6e 74 72 79        cacheEntry
2150f 43 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70 29  Clear(pParse, p)
21510 3b 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20  ;.      p->iReg 
21511 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  = 0;.    }.  }.}
21512 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62 65 72  ../*.** Remember
21513 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 6f 6c   the current col
21514 75 6d 6e 20 63 61 63 68 65 20 63 6f 6e 74 65 78  umn cache contex
21515 74 2e 20 20 41 6e 79 20 6e 65 77 20 65 6e 74 72  t.  Any new entr
21516 69 65 73 20 61 64 64 65 64 0a 2a 2a 20 61 64 64  ies added.** add
21517 65 64 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e  ed to the column
21518 20 63 61 63 68 65 20 61 66 74 65 72 20 74 68 69   cache after thi
21519 73 20 63 61 6c 6c 20 61 72 65 20 72 65 6d 6f 76  s call are remov
2151a 65 64 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 63  ed when the.** c
2151b 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 70 6f 70  orresponding pop
2151c 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49   occurs..*/.SQLI
2151d 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2151e 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
2151f 50 75 73 68 28 50 61 72 73 65 20 2a 70 50 61 72  Push(Parse *pPar
21520 73 65 29 7b 0a 20 20 70 50 61 72 73 65 2d 3e 69  se){.  pParse->i
21521 43 61 63 68 65 4c 65 76 65 6c 2b 2b 3b 0a 7d 0a  CacheLevel++;.}.
21522 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 66 72  ./*.** Remove fr
21523 6f 6d 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61  om the column ca
21524 63 68 65 20 61 6e 79 20 65 6e 74 72 69 65 73 20  che any entries 
21525 74 68 61 74 20 77 65 72 65 20 61 64 64 65 64 20  that were added 
21526 73 69 6e 63 65 20 74 68 65 0a 2a 2a 20 74 68 65  since the.** the
21527 20 70 72 65 76 69 6f 75 73 20 4e 20 50 75 73 68   previous N Push
21528 20 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 49 6e   operations.  In
21529 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 72 65   other words, re
2152a 73 74 6f 72 65 20 74 68 65 20 63 61 63 68 65 0a  store the cache.
2152b 2a 2a 20 74 6f 20 74 68 65 20 73 74 61 74 65 20  ** to the state 
2152c 69 74 20 77 61 73 20 69 6e 20 4e 20 50 75 73 68  it was in N Push
2152d 65 73 20 61 67 6f 2e 0a 2a 2f 0a 53 51 4c 49 54  es ago..*/.SQLIT
2152e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2152f 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50  qlite3ExprCacheP
21530 6f 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  op(Parse *pParse
21531 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 69 6e 74 20  , int N){.  int 
21532 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c  i;.  struct yCol
21533 43 61 63 68 65 20 2a 70 3b 0a 20 20 61 73 73 65  Cache *p;.  asse
21534 72 74 28 20 4e 3e 30 20 29 3b 0a 20 20 61 73 73  rt( N>0 );.  ass
21535 65 72 74 28 20 70 50 61 72 73 65 2d 3e 69 43 61  ert( pParse->iCa
21536 63 68 65 4c 65 76 65 6c 3e 3d 4e 20 29 3b 0a 20  cheLevel>=N );. 
21537 20 70 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c   pParse->iCacheL
21538 65 76 65 6c 20 2d 3d 20 4e 3b 0a 20 20 66 6f 72  evel -= N;.  for
21539 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e  (i=0, p=pParse->
2153a 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c  aColCache; i<SQL
2153b 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20  ITE_N_COLCACHE; 
2153c 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69  i++, p++){.    i
2153d 66 28 20 70 2d 3e 69 52 65 67 20 26 26 20 70 2d  f( p->iReg && p-
2153e 3e 69 4c 65 76 65 6c 3e 70 50 61 72 73 65 2d 3e  >iLevel>pParse->
2153f 69 43 61 63 68 65 4c 65 76 65 6c 20 29 7b 0a 20  iCacheLevel ){. 
21540 20 20 20 20 20 63 61 63 68 65 45 6e 74 72 79 43       cacheEntryC
21541 6c 65 61 72 28 70 50 61 72 73 65 2c 20 70 29 3b  lear(pParse, p);
21542 0a 20 20 20 20 20 20 70 2d 3e 69 52 65 67 20 3d  .      p->iReg =
21543 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a   0;.    }.  }.}.
21544 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 63 61  ./*.** When a ca
21545 63 68 65 64 20 63 6f 6c 75 6d 6e 20 69 73 20 72  ched column is r
21546 65 75 73 65 64 2c 20 6d 61 6b 65 20 73 75 72 65  eused, make sure
21547 20 74 68 61 74 20 69 74 73 20 72 65 67 69 73 74   that its regist
21548 65 72 20 69 73 0a 2a 2a 20 6e 6f 20 6c 6f 6e 67  er is.** no long
21549 65 72 20 61 76 61 69 6c 61 62 6c 65 20 61 73 20  er available as 
2154a 61 20 74 65 6d 70 20 72 65 67 69 73 74 65 72 2e  a temp register.
2154b 20 20 74 69 63 6b 65 74 20 23 33 38 37 39 3a 20    ticket #3879: 
2154c 20 74 68 61 74 20 73 61 6d 65 0a 2a 2a 20 72 65   that same.** re
2154d 67 69 73 74 65 72 20 6d 69 67 68 74 20 62 65 20  gister might be 
2154e 69 6e 20 74 68 65 20 63 61 63 68 65 20 69 6e 20  in the cache in 
2154f 6d 75 6c 74 69 70 6c 65 20 70 6c 61 63 65 73 2c  multiple places,
21550 20 73 6f 20 62 65 20 73 75 72 65 20 74 6f 0a 2a   so be sure to.*
21551 2a 20 67 65 74 20 74 68 65 6d 20 61 6c 6c 2e 0a  * get them all..
21552 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
21553 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 50  qlite3ExprCacheP
21554 69 6e 52 65 67 69 73 74 65 72 28 50 61 72 73 65  inRegister(Parse
21555 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 52   *pParse, int iR
21556 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  eg){.  int i;.  
21557 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65  struct yColCache
21558 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20   *p;.  for(i=0, 
21559 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61  p=pParse->aColCa
2155a 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e 5f  che; i<SQLITE_N_
2155b 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20 70  COLCACHE; i++, p
2155c 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  ++){.    if( p->
2155d 69 52 65 67 3d 3d 69 52 65 67 20 29 7b 0a 20 20  iReg==iReg ){.  
2155e 20 20 20 20 70 2d 3e 74 65 6d 70 52 65 67 20 3d      p->tempReg =
2155f 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a   0;.    }.  }.}.
21560 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
21561 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65  code that will e
21562 78 74 72 61 63 74 20 74 68 65 20 69 43 6f 6c 75  xtract the iColu
21563 6d 6e 2d 74 68 20 63 6f 6c 75 6d 6e 20 66 72 6f  mn-th column fro
21564 6d 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 20  m.** table pTab 
21565 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 63 6f  and store the co
21566 6c 75 6d 6e 20 76 61 6c 75 65 20 69 6e 20 61 20  lumn value in a 
21567 72 65 67 69 73 74 65 72 2e 20 20 41 6e 20 65 66  register.  An ef
21568 66 6f 72 74 0a 2a 2a 20 69 73 20 6d 61 64 65 20  fort.** is made 
21569 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6c  to store the col
2156a 75 6d 6e 20 76 61 6c 75 65 20 69 6e 20 72 65 67  umn value in reg
2156b 69 73 74 65 72 20 69 52 65 67 2c 20 62 75 74 20  ister iReg, but 
2156c 74 68 69 73 20 69 73 0a 2a 2a 20 6e 6f 74 20 67  this is.** not g
2156d 75 61 72 61 6e 74 65 65 64 2e 20 20 54 68 65 20  uaranteed.  The 
2156e 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  location of the 
2156f 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20 69 73 20  column value is 
21570 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
21571 54 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 6e  There must be an
21572 20 6f 70 65 6e 20 63 75 72 73 6f 72 20 74 6f 20   open cursor to 
21573 70 54 61 62 20 69 6e 20 69 54 61 62 6c 65 20 77  pTab in iTable w
21574 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
21575 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e 20 20  .** is called.  
21576 49 66 20 69 43 6f 6c 75 6d 6e 3c 30 20 74 68 65  If iColumn<0 the
21577 6e 20 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61  n code is genera
21578 74 65 64 20 74 68 61 74 20 65 78 74 72 61 63 74  ted that extract
21579 73 20 74 68 65 20 72 6f 77 69 64 2e 0a 2a 2a 0a  s the rowid..**.
2157a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2157b 6d 69 67 68 74 20 61 74 74 65 6d 70 74 20 74 6f  might attempt to
2157c 20 72 65 75 73 65 20 74 68 65 20 76 61 6c 75 65   reuse the value
2157d 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 74   of the column t
2157e 68 61 74 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61  hat.** has alrea
2157f 64 79 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 69  dy been loaded i
21580 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72 2e 20  nto a register. 
21581 20 54 68 65 20 76 61 6c 75 65 20 77 69 6c 6c 20   The value will 
21582 61 6c 77 61 79 73 0a 2a 2a 20 62 65 20 75 73 65  always.** be use
21583 64 20 69 66 20 69 74 20 68 61 73 20 6e 6f 74 20  d if it has not 
21584 75 6e 64 65 72 67 6f 6e 65 20 61 6e 79 20 61 66  undergone any af
21585 66 69 6e 69 74 79 20 63 68 61 6e 67 65 73 2e 20  finity changes. 
21586 20 42 75 74 20 69 66 0a 2a 2a 20 61 6e 20 61 66   But if.** an af
21587 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20 68 61  finity change ha
21588 73 20 6f 63 63 75 72 72 65 64 2c 20 74 68 65 6e  s occurred, then
21589 20 74 68 65 20 63 61 63 68 65 64 20 76 61 6c 75   the cached valu
2158a 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65 0a 2a  e will only be.*
2158b 2a 20 75 73 65 64 20 69 66 20 61 6c 6c 6f 77 41  * used if allowA
2158c 66 66 43 68 6e 67 20 69 73 20 74 72 75 65 2e 0a  ffChng is true..
2158d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2158e 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
2158f 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 0a  rCodeGetColumn(.
21590 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
21591 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 61 6e     /* Parsing an
21592 64 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e  d code generatin
21593 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  g context */.  T
21594 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
21595 2f 2a 20 44 65 73 63 72 69 70 74 69 6f 6e 20 6f  /* Description o
21596 66 20 74 68 65 20 74 61 62 6c 65 20 77 65 20 61  f the table we a
21597 72 65 20 72 65 61 64 69 6e 67 20 66 72 6f 6d 20  re reading from 
21598 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  */.  int iColumn
21599 2c 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f  ,     /* Index o
2159a 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75  f the table colu
2159b 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62  mn */.  int iTab
2159c 6c 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  le,      /* The 
2159d 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20  cursor pointing 
2159e 74 6f 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  to the table */.
2159f 20 20 69 6e 74 20 69 52 65 67 2c 20 20 20 20 20    int iReg,     
215a0 20 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73 75     /* Store resu
215a1 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e  lts here */.  in
215a2 74 20 61 6c 6c 6f 77 41 66 66 43 68 6e 67 20 2f  t allowAffChng /
215a3 2a 20 54 72 75 65 20 69 66 20 70 72 69 6f 72 20  * True if prior 
215a4 61 66 66 69 6e 69 74 79 20 63 68 61 6e 67 65 73  affinity changes
215a5 20 61 72 65 20 4f 4b 20 2a 2f 0a 29 7b 0a 20 20   are OK */.){.  
215a6 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
215a7 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 69  ->pVdbe;.  int i
215a8 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43  ;.  struct yColC
215a9 61 63 68 65 20 2a 70 3b 0a 0a 20 20 66 6f 72 28  ache *p;..  for(
215aa 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61  i=0, p=pParse->a
215ab 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49  ColCache; i<SQLI
215ac 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69  TE_N_COLCACHE; i
215ad 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66  ++, p++){.    if
215ae 28 20 70 2d 3e 69 52 65 67 3e 30 20 26 26 20 70  ( p->iReg>0 && p
215af 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65  ->iTable==iTable
215b0 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d   && p->iColumn==
215b1 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 20 20  iColumn.        
215b2 20 20 20 26 26 20 28 21 70 2d 3e 61 66 66 43 68     && (!p->affCh
215b3 61 6e 67 65 20 7c 7c 20 61 6c 6c 6f 77 41 66 66  ange || allowAff
215b4 43 68 6e 67 29 20 29 7b 0a 20 20 20 20 20 20 70  Chng) ){.      p
215b5 2d 3e 6c 72 75 20 3d 20 70 50 61 72 73 65 2d 3e  ->lru = pParse->
215b6 69 43 61 63 68 65 43 6e 74 2b 2b 3b 0a 20 20 20  iCacheCnt++;.   
215b7 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
215b8 63 68 65 50 69 6e 52 65 67 69 73 74 65 72 28 70  chePinRegister(p
215b9 50 61 72 73 65 2c 20 70 2d 3e 69 52 65 67 29 3b  Parse, p->iReg);
215ba 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 2d  .      return p-
215bb 3e 69 52 65 67 3b 0a 20 20 20 20 7d 0a 20 20 7d  >iReg;.    }.  }
215bc 20 20 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d    .  assert( v!=
215bd 30 20 29 3b 0a 20 20 69 66 28 20 69 43 6f 6c 75  0 );.  if( iColu
215be 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  mn<0 ){.    sqli
215bf 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
215c0 20 4f 50 5f 52 6f 77 69 64 2c 20 69 54 61 62 6c   OP_Rowid, iTabl
215c1 65 2c 20 69 52 65 67 29 3b 0a 20 20 7d 65 6c 73  e, iReg);.  }els
215c2 65 20 69 66 28 20 41 4c 57 41 59 53 28 70 54 61  e if( ALWAYS(pTa
215c3 62 21 3d 30 29 20 29 7b 0a 20 20 20 20 69 6e 74  b!=0) ){.    int
215c4 20 6f 70 20 3d 20 49 73 56 69 72 74 75 61 6c 28   op = IsVirtual(
215c5 70 54 61 62 29 20 3f 20 4f 50 5f 56 43 6f 6c 75  pTab) ? OP_VColu
215c6 6d 6e 20 3a 20 4f 50 5f 43 6f 6c 75 6d 6e 3b 0a  mn : OP_Column;.
215c7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
215c8 64 64 4f 70 33 28 76 2c 20 6f 70 2c 20 69 54 61  ddOp3(v, op, iTa
215c9 62 6c 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 52  ble, iColumn, iR
215ca 65 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  eg);.    sqlite3
215cb 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c  ColumnDefault(v,
215cc 20 70 54 61 62 2c 20 69 43 6f 6c 75 6d 6e 2c 20   pTab, iColumn, 
215cd 69 52 65 67 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  iReg);.  }.  sql
215ce 69 74 65 33 45 78 70 72 43 61 63 68 65 53 74 6f  ite3ExprCacheSto
215cf 72 65 28 70 50 61 72 73 65 2c 20 69 54 61 62 6c  re(pParse, iTabl
215d0 65 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67  e, iColumn, iReg
215d1 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 52 65 67  );.  return iReg
215d2 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72  ;.}../*.** Clear
215d3 20 61 6c 6c 20 63 6f 6c 75 6d 6e 20 63 61 63 68   all column cach
215d4 65 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 53 51  e entries..*/.SQ
215d5 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
215d6 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63  d sqlite3ExprCac
215d7 68 65 43 6c 65 61 72 28 50 61 72 73 65 20 2a 70  heClear(Parse *p
215d8 50 61 72 73 65 29 7b 0a 20 20 69 6e 74 20 69 3b  Parse){.  int i;
215d9 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61  .  struct yColCa
215da 63 68 65 20 2a 70 3b 0a 0a 20 20 66 6f 72 28 69  che *p;..  for(i
215db 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61 43  =0, p=pParse->aC
215dc 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49 54  olCache; i<SQLIT
215dd 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b  E_N_COLCACHE; i+
215de 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28  +, p++){.    if(
215df 20 70 2d 3e 69 52 65 67 20 29 7b 0a 20 20 20 20   p->iReg ){.    
215e0 20 20 63 61 63 68 65 45 6e 74 72 79 43 6c 65 61    cacheEntryClea
215e1 72 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20 20  r(pParse, p);.  
215e2 20 20 20 20 70 2d 3e 69 52 65 67 20 3d 20 30 3b      p->iReg = 0;
215e3 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
215e4 0a 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 20 66  .** Record the f
215e5 61 63 74 20 74 68 61 74 20 61 6e 20 61 66 66 69  act that an affi
215e6 6e 69 74 79 20 63 68 61 6e 67 65 20 68 61 73 20  nity change has 
215e7 6f 63 63 75 72 72 65 64 20 6f 6e 20 69 43 6f 75  occurred on iCou
215e8 6e 74 0a 2a 2a 20 72 65 67 69 73 74 65 72 73 20  nt.** registers 
215e9 73 74 61 72 74 69 6e 67 20 77 69 74 68 20 69 53  starting with iS
215ea 74 61 72 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tart..*/.SQLITE_
215eb 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
215ec 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
215ed 69 6e 69 74 79 43 68 61 6e 67 65 28 50 61 72 73  inityChange(Pars
215ee 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
215ef 53 74 61 72 74 2c 20 69 6e 74 20 69 43 6f 75 6e  Start, int iCoun
215f0 74 29 7b 0a 20 20 69 6e 74 20 69 45 6e 64 20 3d  t){.  int iEnd =
215f1 20 69 53 74 61 72 74 20 2b 20 69 43 6f 75 6e 74   iStart + iCount
215f2 20 2d 20 31 3b 0a 20 20 69 6e 74 20 69 3b 0a 20   - 1;.  int i;. 
215f3 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68   struct yColCach
215f4 65 20 2a 70 3b 0a 20 20 66 6f 72 28 69 3d 30 2c  e *p;.  for(i=0,
215f5 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43   p=pParse->aColC
215f6 61 63 68 65 3b 20 69 3c 53 51 4c 49 54 45 5f 4e  ache; i<SQLITE_N
215f7 5f 43 4f 4c 43 41 43 48 45 3b 20 69 2b 2b 2c 20  _COLCACHE; i++, 
215f8 70 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72 20  p++){.    int r 
215f9 3d 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20 20 69  = p->iReg;.    i
215fa 66 28 20 72 3e 3d 69 53 74 61 72 74 20 26 26 20  f( r>=iStart && 
215fb 72 3c 3d 69 45 6e 64 20 29 7b 0a 20 20 20 20 20  r<=iEnd ){.     
215fc 20 70 2d 3e 61 66 66 43 68 61 6e 67 65 20 3d 20   p->affChange = 
215fd 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  1;.    }.  }.}..
215fe 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
215ff 6f 64 65 20 74 6f 20 6d 6f 76 65 20 63 6f 6e 74  ode to move cont
21600 65 6e 74 20 66 72 6f 6d 20 72 65 67 69 73 74 65  ent from registe
21601 72 73 20 69 46 72 6f 6d 2e 2e 2e 69 46 72 6f 6d  rs iFrom...iFrom
21602 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f 76 65 72 20  +nReg-1.** over 
21603 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b 6e 52 65 67  to iTo..iTo+nReg
21604 2d 31 2e 20 4b 65 65 70 20 74 68 65 20 63 6f 6c  -1. Keep the col
21605 75 6d 6e 20 63 61 63 68 65 20 75 70 2d 74 6f 2d  umn cache up-to-
21606 64 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  date..*/.SQLITE_
21607 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
21608 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65  ite3ExprCodeMove
21609 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2160a 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 69  int iFrom, int i
2160b 54 6f 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20  To, int nReg){. 
2160c 20 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74   int i;.  struct
2160d 20 79 43 6f 6c 43 61 63 68 65 20 2a 70 3b 0a 20   yColCache *p;. 
2160e 20 69 66 28 20 4e 45 56 45 52 28 69 46 72 6f 6d   if( NEVER(iFrom
2160f 3d 3d 69 54 6f 29 20 29 20 72 65 74 75 72 6e 3b  ==iTo) ) return;
21610 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
21611 64 4f 70 33 28 70 50 61 72 73 65 2d 3e 70 56 64  dOp3(pParse->pVd
21612 62 65 2c 20 4f 50 5f 4d 6f 76 65 2c 20 69 46 72  be, OP_Move, iFr
21613 6f 6d 2c 20 69 54 6f 2c 20 6e 52 65 67 29 3b 0a  om, iTo, nReg);.
21614 20 20 66 6f 72 28 69 3d 30 2c 20 70 3d 70 50 61    for(i=0, p=pPa
21615 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 3b 20  rse->aColCache; 
21616 69 3c 53 51 4c 49 54 45 5f 4e 5f 43 4f 4c 43 41  i<SQLITE_N_COLCA
21617 43 48 45 3b 20 69 2b 2b 2c 20 70 2b 2b 29 7b 0a  CHE; i++, p++){.
21618 20 20 20 20 69 6e 74 20 78 20 3d 20 70 2d 3e 69      int x = p->i
21619 52 65 67 3b 0a 20 20 20 20 69 66 28 20 78 3e 3d  Reg;.    if( x>=
2161a 69 46 72 6f 6d 20 26 26 20 78 3c 69 46 72 6f 6d  iFrom && x<iFrom
2161b 2b 6e 52 65 67 20 29 7b 0a 20 20 20 20 20 20 70  +nReg ){.      p
2161c 2d 3e 69 52 65 67 20 2b 3d 20 69 54 6f 2d 69 46  ->iReg += iTo-iF
2161d 72 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  rom;.    }.  }.}
2161e 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
2161f 20 63 6f 64 65 20 74 6f 20 63 6f 70 79 20 63 6f   code to copy co
21620 6e 74 65 6e 74 20 66 72 6f 6d 20 72 65 67 69 73  ntent from regis
21621 74 65 72 73 20 69 46 72 6f 6d 2e 2e 2e 69 46 72  ters iFrom...iFr
21622 6f 6d 2b 6e 52 65 67 2d 31 0a 2a 2a 20 6f 76 65  om+nReg-1.** ove
21623 72 20 74 6f 20 69 54 6f 2e 2e 69 54 6f 2b 6e 52  r to iTo..iTo+nR
21624 65 67 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eg-1..*/.SQLITE_
21625 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
21626 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f 70 79  ite3ExprCodeCopy
21627 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
21628 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20 69  int iFrom, int i
21629 54 6f 2c 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20  To, int nReg){. 
2162a 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 4e 45   int i;.  if( NE
2162b 56 45 52 28 69 46 72 6f 6d 3d 3d 69 54 6f 29 20  VER(iFrom==iTo) 
2162c 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28  ) return;.  for(
2162d 69 3d 30 3b 20 69 3c 6e 52 65 67 3b 20 69 2b 2b  i=0; i<nReg; i++
2162e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
2162f 62 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d  beAddOp2(pParse-
21630 3e 70 56 64 62 65 2c 20 4f 50 5f 43 6f 70 79 2c  >pVdbe, OP_Copy,
21631 20 69 46 72 6f 6d 2b 69 2c 20 69 54 6f 2b 69 29   iFrom+i, iTo+i)
21632 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
21633 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 61 6e  eturn true if an
21634 79 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 68  y register in th
21635 65 20 72 61 6e 67 65 20 69 46 72 6f 6d 2e 2e 69  e range iFrom..i
21636 54 6f 20 28 69 6e 63 6c 75 73 69 76 65 29 0a 2a  To (inclusive).*
21637 2a 20 69 73 20 75 73 65 64 20 61 73 20 70 61 72  * is used as par
21638 74 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20  t of the column 
21639 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  cache..*/.static
2163a 20 69 6e 74 20 75 73 65 64 41 73 43 6f 6c 75 6d   int usedAsColum
2163b 6e 43 61 63 68 65 28 50 61 72 73 65 20 2a 70 50  nCache(Parse *pP
2163c 61 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c  arse, int iFrom,
2163d 20 69 6e 74 20 69 54 6f 29 7b 0a 20 20 69 6e 74   int iTo){.  int
2163e 20 69 3b 0a 20 20 73 74 72 75 63 74 20 79 43 6f   i;.  struct yCo
2163f 6c 43 61 63 68 65 20 2a 70 3b 0a 20 20 66 6f 72  lCache *p;.  for
21640 28 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e  (i=0, p=pParse->
21641 61 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c  aColCache; i<SQL
21642 49 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20  ITE_N_COLCACHE; 
21643 69 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 69  i++, p++){.    i
21644 6e 74 20 72 20 3d 20 70 2d 3e 69 52 65 67 3b 0a  nt r = p->iReg;.
21645 20 20 20 20 69 66 28 20 72 3e 3d 69 46 72 6f 6d      if( r>=iFrom
21646 20 26 26 20 72 3c 3d 69 54 6f 20 29 20 72 65 74   && r<=iTo ) ret
21647 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74  urn 1;.  }.  ret
21648 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
21649 49 66 20 74 68 65 20 6c 61 73 74 20 69 6e 73 74  If the last inst
2164a 72 75 63 74 69 6f 6e 20 63 6f 64 65 64 20 69 73  ruction coded is
2164b 20 61 6e 20 65 70 68 65 6d 65 72 61 6c 20 63 6f   an ephemeral co
2164c 70 79 20 6f 66 20 61 6e 79 20 6f 66 0a 2a 2a 20  py of any of.** 
2164d 74 68 65 20 72 65 67 69 73 74 65 72 73 20 69 6e  the registers in
2164e 20 74 68 65 20 6e 52 65 67 20 72 65 67 69 73 74   the nReg regist
2164f 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20 77 69  ers beginning wi
21650 74 68 20 69 52 65 67 2c 20 74 68 65 6e 0a 2a 2a  th iReg, then.**
21651 20 63 6f 6e 76 65 72 74 20 74 68 65 20 6c 61 73   convert the las
21652 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 66 72  t instruction fr
21653 6f 6d 20 4f 50 5f 53 43 6f 70 79 20 74 6f 20 4f  om OP_SCopy to O
21654 50 5f 43 6f 70 79 2e 0a 2a 2f 0a 53 51 4c 49 54  P_Copy..*/.SQLIT
21655 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
21656 71 6c 69 74 65 33 45 78 70 72 48 61 72 64 43 6f  qlite3ExprHardCo
21657 70 79 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  py(Parse *pParse
21658 2c 20 69 6e 74 20 69 52 65 67 2c 20 69 6e 74 20  , int iReg, int 
21659 6e 52 65 67 29 7b 0a 20 20 56 64 62 65 4f 70 20  nReg){.  VdbeOp 
2165a 2a 70 4f 70 3b 0a 20 20 56 64 62 65 20 2a 76 3b  *pOp;.  Vdbe *v;
2165b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ..  assert( pPar
2165c 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  se->db->mallocFa
2165d 69 6c 65 64 3d 3d 30 20 29 3b 0a 20 20 76 20 3d  iled==0 );.  v =
2165e 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
2165f 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29    assert( v!=0 )
21660 3b 0a 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 65  ;.  pOp = sqlite
21661 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 2d 31  3VdbeGetOp(v, -1
21662 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
21663 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 4f 70  !=0 );.  if( pOp
21664 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 43 6f  ->opcode==OP_SCo
21665 70 79 20 26 26 20 70 4f 70 2d 3e 70 31 3e 3d 69  py && pOp->p1>=i
21666 52 65 67 20 26 26 20 70 4f 70 2d 3e 70 31 3c 69  Reg && pOp->p1<i
21667 52 65 67 2b 6e 52 65 67 20 29 7b 0a 20 20 20 20  Reg+nReg ){.    
21668 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50  pOp->opcode = OP
21669 5f 43 6f 70 79 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  _Copy;.  }.}../*
2166a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
2166b 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 76  e to store the v
2166c 61 6c 75 65 20 6f 66 20 74 68 65 20 69 41 6c 69  alue of the iAli
2166d 61 73 2d 74 68 20 61 6c 69 61 73 20 69 6e 20 72  as-th alias in r
2166e 65 67 69 73 74 65 72 0a 2a 2a 20 74 61 72 67 65  egister.** targe
2166f 74 2e 20 20 54 68 65 20 66 69 72 73 74 20 74 69  t.  The first ti
21670 6d 65 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65  me this is calle
21671 64 2c 20 70 45 78 70 72 20 69 73 20 65 76 61 6c  d, pExpr is eval
21672 75 61 74 65 64 20 74 6f 20 63 6f 6d 70 75 74 65  uated to compute
21673 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66  .** the value of
21674 20 74 68 65 20 61 6c 69 61 73 2e 20 20 54 68 65   the alias.  The
21675 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64   value is stored
21676 20 69 6e 20 61 6e 20 61 75 78 69 6c 69 61 72 79   in an auxiliary
21677 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 61 6e 64   register.** and
21678 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
21679 68 61 74 20 72 65 67 69 73 74 65 72 20 69 73 20  hat register is 
2167a 72 65 74 75 72 6e 65 64 2e 20 20 4f 6e 20 73 75  returned.  On su
2167b 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 2c 0a  bsequent calls,.
2167c 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65 72 20  ** the register 
2167d 6e 75 6d 62 65 72 20 69 73 20 72 65 74 75 72 6e  number is return
2167e 65 64 20 77 69 74 68 6f 75 74 20 67 65 6e 65 72  ed without gener
2167f 61 74 69 6e 67 20 61 6e 79 20 63 6f 64 65 2e 0a  ating any code..
21680 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
21681 69 6e 20 6f 72 64 65 72 20 66 6f 72 20 74 68 69  in order for thi
21682 73 20 74 6f 20 77 6f 72 6b 2c 20 63 6f 64 65 20  s to work, code 
21683 6d 75 73 74 20 62 65 20 67 65 6e 65 72 61 74 65  must be generate
21684 64 20 69 6e 20 74 68 65 0a 2a 2a 20 73 61 6d 65  d in the.** same
21685 20 6f 72 64 65 72 20 74 68 61 74 20 69 74 20 69   order that it i
21686 73 20 65 78 65 63 75 74 65 64 2e 0a 2a 2a 0a 2a  s executed..**.*
21687 2a 20 41 6c 69 61 73 65 73 20 61 72 65 20 6e 75  * Aliases are nu
21688 6d 62 65 72 65 64 20 73 74 61 72 74 69 6e 67 20  mbered starting 
21689 77 69 74 68 20 31 2e 20 20 53 6f 20 69 41 6c 69  with 1.  So iAli
2168a 61 73 20 69 73 20 69 6e 20 74 68 65 20 72 61 6e  as is in the ran
2168b 67 65 0a 2a 2a 20 6f 66 20 31 20 74 6f 20 70 50  ge.** of 1 to pP
2168c 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 69 6e 63  arse->nAlias inc
2168d 6c 75 73 69 76 65 2e 20 20 0a 2a 2a 0a 2a 2a 20  lusive.  .**.** 
2168e 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 69  pParse->aAlias[i
2168f 41 6c 69 61 73 2d 31 5d 20 72 65 63 6f 72 64 73  Alias-1] records
21690 20 74 68 65 20 72 65 67 69 73 74 65 72 20 6e 75   the register nu
21691 6d 62 65 72 20 77 68 65 72 65 20 74 68 65 20 76  mber where the v
21692 61 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 69  alue.** of the i
21693 41 6c 69 61 73 2d 74 68 20 61 6c 69 61 73 20 69  Alias-th alias i
21694 73 20 73 74 6f 72 65 64 2e 20 20 49 66 20 7a 65  s stored.  If ze
21695 72 6f 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74  ro, that means t
21696 68 61 74 20 74 68 65 0a 2a 2a 20 61 6c 69 61 73  hat the.** alias
21697 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65   has not yet bee
21698 6e 20 63 6f 6d 70 75 74 65 64 2e 0a 2a 2f 0a 73  n computed..*/.s
21699 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 41 6c  tatic int codeAl
2169a 69 61 73 28 50 61 72 73 65 20 2a 70 50 61 72 73  ias(Parse *pPars
2169b 65 2c 20 69 6e 74 20 69 41 6c 69 61 73 2c 20 45  e, int iAlias, E
2169c 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20  xpr *pExpr, int 
2169d 74 61 72 67 65 74 29 7b 0a 23 69 66 20 30 0a 20  target){.#if 0. 
2169e 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
2169f 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74  Parse->db;.  int
216a0 20 69 52 65 67 3b 0a 20 20 69 66 28 20 70 50 61   iReg;.  if( pPa
216a1 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63  rse->nAliasAlloc
216a2 3c 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20  <pParse->nAlias 
216a3 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61  ){.    pParse->a
216a4 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33 44  Alias = sqlite3D
216a5 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 64  bReallocOrFree(d
216a6 62 2c 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61  b, pParse->aAlia
216a7 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  s,.             
216a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
216a9 20 20 20 20 73 69 7a 65 6f 66 28 70 50 61 72 73      sizeof(pPars
216aa 65 2d 3e 61 41 6c 69 61 73 5b 30 5d 29 2a 70 50  e->aAlias[0])*pP
216ab 61 72 73 65 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a  arse->nAlias );.
216ac 20 20 20 20 74 65 73 74 63 61 73 65 28 20 64 62      testcase( db
216ad 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26  ->mallocFailed &
216ae 26 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73  & pParse->nAlias
216af 41 6c 6c 6f 63 3e 30 20 29 3b 0a 20 20 20 20 69  Alloc>0 );.    i
216b0 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
216b1 6c 65 64 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  led ) return 0;.
216b2 20 20 20 20 6d 65 6d 73 65 74 28 26 70 50 61 72      memset(&pPar
216b3 73 65 2d 3e 61 41 6c 69 61 73 5b 70 50 61 72 73  se->aAlias[pPars
216b4 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c 6f 63 5d 2c  e->nAliasAlloc],
216b5 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 28   0,.           (
216b6 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 2d 70  pParse->nAlias-p
216b7 50 61 72 73 65 2d 3e 6e 41 6c 69 61 73 41 6c 6c  Parse->nAliasAll
216b8 6f 63 29 2a 73 69 7a 65 6f 66 28 70 50 61 72 73  oc)*sizeof(pPars
216b9 65 2d 3e 61 41 6c 69 61 73 5b 30 5d 29 29 3b 0a  e->aAlias[0]));.
216ba 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69      pParse->nAli
216bb 61 73 41 6c 6c 6f 63 20 3d 20 70 50 61 72 73 65  asAlloc = pParse
216bc 2d 3e 6e 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 20  ->nAlias;.  }.  
216bd 61 73 73 65 72 74 28 20 69 41 6c 69 61 73 3e 30  assert( iAlias>0
216be 20 26 26 20 69 41 6c 69 61 73 3c 3d 70 50 61 72   && iAlias<=pPar
216bf 73 65 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a 20 20  se->nAlias );.  
216c0 69 52 65 67 20 3d 20 70 50 61 72 73 65 2d 3e 61  iReg = pParse->a
216c1 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d 3b  Alias[iAlias-1];
216c2 0a 20 20 69 66 28 20 69 52 65 67 3d 3d 30 20 29  .  if( iReg==0 )
216c3 7b 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65  {.    if( pParse
216c4 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3e 30 20  ->iCacheLevel>0 
216c5 29 7b 0a 20 20 20 20 20 20 69 52 65 67 20 3d 20  ){.      iReg = 
216c6 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
216c7 61 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45  arget(pParse, pE
216c8 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  xpr, target);.  
216c9 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
216ca 52 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  Reg = ++pParse->
216cb 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69  nMem;.      sqli
216cc 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
216cd 73 65 2c 20 70 45 78 70 72 2c 20 69 52 65 67 29  se, pExpr, iReg)
216ce 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
216cf 61 41 6c 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d  aAlias[iAlias-1]
216d0 20 3d 20 69 52 65 67 3b 0a 20 20 20 20 7d 0a 20   = iReg;.    }. 
216d1 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 52 65 67   }.  return iReg
216d2 3b 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44  ;.#else.  UNUSED
216d3 5f 50 41 52 41 4d 45 54 45 52 28 69 41 6c 69 61  _PARAMETER(iAlia
216d4 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  s);.  return sql
216d5 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67  ite3ExprCodeTarg
216d6 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  et(pParse, pExpr
216d7 2c 20 74 61 72 67 65 74 29 3b 0a 23 65 6e 64 69  , target);.#endi
216d8 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  f.}../*.** Gener
216d9 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74 68  ate code into th
216da 65 20 63 75 72 72 65 6e 74 20 56 64 62 65 20 74  e current Vdbe t
216db 6f 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 67  o evaluate the g
216dc 69 76 65 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69  iven.** expressi
216dd 6f 6e 2e 20 20 41 74 74 65 6d 70 74 20 74 6f 20  on.  Attempt to 
216de 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
216df 73 20 69 6e 20 72 65 67 69 73 74 65 72 20 22 74  s in register "t
216e0 61 72 67 65 74 22 2e 0a 2a 2a 20 52 65 74 75 72  arget"..** Retur
216e1 6e 20 74 68 65 20 72 65 67 69 73 74 65 72 20 77  n the register w
216e2 68 65 72 65 20 72 65 73 75 6c 74 73 20 61 72 65  here results are
216e3 20 73 74 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 57   stored..**.** W
216e4 69 74 68 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ith this routine
216e5 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 20 67 75  , there is no gu
216e6 61 72 61 6e 74 65 65 20 74 68 61 74 20 72 65 73  arantee that res
216e7 75 6c 74 73 20 77 69 6c 6c 0a 2a 2a 20 62 65 20  ults will.** be 
216e8 73 74 6f 72 65 64 20 69 6e 20 74 61 72 67 65 74  stored in target
216e9 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 6d 69  .  The result mi
216ea 67 68 74 20 62 65 20 73 74 6f 72 65 64 20 69 6e  ght be stored in
216eb 20 73 6f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 72   some other.** r
216ec 65 67 69 73 74 65 72 20 69 66 20 69 74 20 69 73  egister if it is
216ed 20 63 6f 6e 76 65 6e 69 65 6e 74 20 74 6f 20 64   convenient to d
216ee 6f 20 73 6f 2e 20 20 54 68 65 20 63 61 6c 6c 69  o so.  The calli
216ef 6e 67 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d  ng function.** m
216f0 75 73 74 20 63 68 65 63 6b 20 74 68 65 20 72 65  ust check the re
216f1 74 75 72 6e 20 63 6f 64 65 20 61 6e 64 20 6d 6f  turn code and mo
216f2 76 65 20 74 68 65 20 72 65 73 75 6c 74 73 20 74  ve the results t
216f3 6f 20 74 68 65 20 64 65 73 69 72 65 64 0a 2a 2a  o the desired.**
216f4 20 72 65 67 69 73 74 65 72 2e 0a 2a 2f 0a 53 51   register..*/.SQ
216f5 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
216f6 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
216f7 54 61 72 67 65 74 28 50 61 72 73 65 20 2a 70 50  Target(Parse *pP
216f8 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70  arse, Expr *pExp
216f9 72 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a  r, int target){.
216fa 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
216fb 73 65 2d 3e 70 56 64 62 65 3b 20 20 2f 2a 20 54  se->pVdbe;  /* T
216fc 68 65 20 56 4d 20 75 6e 64 65 72 20 63 6f 6e 73  he VM under cons
216fd 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  truction */.  in
216fe 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20  t op;           
216ff 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f          /* The o
21700 70 63 6f 64 65 20 62 65 69 6e 67 20 63 6f 64 65  pcode being code
21701 64 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 52 65 67  d */.  int inReg
21702 20 3d 20 74 61 72 67 65 74 3b 20 20 20 20 20 20   = target;      
21703 20 2f 2a 20 52 65 73 75 6c 74 73 20 73 74 6f 72   /* Results stor
21704 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20 69  ed in register i
21705 6e 52 65 67 20 2a 2f 0a 20 20 69 6e 74 20 72 65  nReg */.  int re
21706 67 46 72 65 65 31 20 3d 20 30 3b 20 20 20 20 20  gFree1 = 0;     
21707 20 20 20 20 2f 2a 20 49 66 20 6e 6f 6e 2d 7a 65      /* If non-ze
21708 72 6f 20 66 72 65 65 20 74 68 69 73 20 74 65 6d  ro free this tem
21709 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72 20  porary register 
2170a 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65  */.  int regFree
2170b 32 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f  2 = 0;         /
2170c 2a 20 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72  * If non-zero fr
2170d 65 65 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72  ee this temporar
2170e 79 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20  y register */.  
2170f 69 6e 74 20 72 31 2c 20 72 32 2c 20 72 33 2c 20  int r1, r2, r3, 
21710 72 34 3b 20 20 20 20 20 20 20 2f 2a 20 56 61 72  r4;       /* Var
21711 69 6f 75 73 20 72 65 67 69 73 74 65 72 20 6e 75  ious register nu
21712 6d 62 65 72 73 20 2a 2f 0a 20 20 73 71 6c 69 74  mbers */.  sqlit
21713 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
21714 3e 64 62 3b 20 2f 2a 20 54 68 65 20 64 61 74 61  >db; /* The data
21715 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
21716 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 74 61  */..  assert( ta
21717 72 67 65 74 3e 30 20 26 26 20 74 61 72 67 65 74  rget>0 && target
21718 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 29  <=pParse->nMem )
21719 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a  ;.  if( v==0 ){.
2171a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72      assert( pPar
2171b 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  se->db->mallocFa
2171c 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65 74 75  iled );.    retu
2171d 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  rn 0;.  }..  if(
2171e 20 70 45 78 70 72 3d 3d 30 20 29 7b 0a 20 20 20   pExpr==0 ){.   
2171f 20 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20   op = TK_NULL;. 
21720 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6f 70 20 3d   }else{.    op =
21721 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 7d 0a   pExpr->op;.  }.
21722 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a    switch( op ){.
21723 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f      case TK_AGG_
21724 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20  COLUMN: {.      
21725 41 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66  AggInfo *pAggInf
21726 6f 20 3d 20 70 45 78 70 72 2d 3e 70 41 67 67 49  o = pExpr->pAggI
21727 6e 66 6f 3b 0a 20 20 20 20 20 20 73 74 72 75 63  nfo;.      struc
21728 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70  t AggInfo_col *p
21729 43 6f 6c 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d  Col = &pAggInfo-
2172a 3e 61 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 41 67  >aCol[pExpr->iAg
2172b 67 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70  g];.      if( !p
2172c 41 67 67 49 6e 66 6f 2d 3e 64 69 72 65 63 74 4d  AggInfo->directM
2172d 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 20 20 61  ode ){.        a
2172e 73 73 65 72 74 28 20 70 43 6f 6c 2d 3e 69 4d 65  ssert( pCol->iMe
2172f 6d 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69  m>0 );.        i
21730 6e 52 65 67 20 3d 20 70 43 6f 6c 2d 3e 69 4d 65  nReg = pCol->iMe
21731 6d 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  m;.        break
21732 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
21733 28 20 70 41 67 67 49 6e 66 6f 2d 3e 75 73 65 53  ( pAggInfo->useS
21734 6f 72 74 69 6e 67 49 64 78 20 29 7b 0a 20 20 20  ortingIdx ){.   
21735 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21736 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
21737 75 6d 6e 2c 20 70 41 67 67 49 6e 66 6f 2d 3e 73  umn, pAggInfo->s
21738 6f 72 74 69 6e 67 49 64 78 2c 0a 20 20 20 20 20  ortingIdx,.     
21739 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2173a 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69           pCol->i
2173b 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 2c 20 74 61  SorterColumn, ta
2173c 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 62  rget);.        b
2173d 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2173e 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65      /* Otherwise
2173f 2c 20 66 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f  , fall thru into
21740 20 74 68 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20 63   the TK_COLUMN c
21741 61 73 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20  ase */.    }.   
21742 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a   case TK_COLUMN:
21743 20 7b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78   {.      if( pEx
21744 70 72 2d 3e 69 54 61 62 6c 65 3c 30 20 29 7b 0a  pr->iTable<0 ){.
21745 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
21746 6f 6e 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65  only happens whe
21747 6e 20 63 6f 64 69 6e 67 20 63 68 65 63 6b 20 63  n coding check c
21748 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20  onstraints */.  
21749 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
2174a 61 72 73 65 2d 3e 63 6b 42 61 73 65 3e 30 20 29  arse->ckBase>0 )
2174b 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67 20  ;.        inReg 
2174c 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  = pExpr->iColumn
2174d 20 2b 20 70 50 61 72 73 65 2d 3e 63 6b 42 61 73   + pParse->ckBas
2174e 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  e;.      }else{.
2174f 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
21750 28 20 28 70 45 78 70 72 2d 3e 66 6c 61 67 73 20  ( (pExpr->flags 
21751 26 20 45 50 5f 41 6e 79 41 66 66 29 21 3d 30 20  & EP_AnyAff)!=0 
21752 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 52 65 67  );.        inReg
21753 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
21754 64 65 47 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72  deGetColumn(pPar
21755 73 65 2c 20 70 45 78 70 72 2d 3e 70 54 61 62 2c  se, pExpr->pTab,
21756 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
21757 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21758 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e    pExpr->iColumn
21759 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c  , pExpr->iTable,
2175a 20 74 61 72 67 65 74 2c 0a 20 20 20 20 20 20 20   target,.       
2175b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2175c 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
2175d 3e 66 6c 61 67 73 20 26 20 45 50 5f 41 6e 79 41  >flags & EP_AnyA
2175e 66 66 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ff);.      }.   
2175f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
21760 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45      case TK_INTE
21761 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 63 6f 64  GER: {.      cod
21762 65 49 6e 74 65 67 65 72 28 76 2c 20 70 45 78 70  eInteger(v, pExp
21763 72 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20  r, 0, target);. 
21764 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21765 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 4c  }.    case TK_FL
21766 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  OAT: {.      ass
21767 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f  ert( !ExprHasPro
21768 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
21769 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20  IntValue) );.   
2176a 20 20 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 70     codeReal(v, p
2176b 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20  Expr->u.zToken, 
2176c 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  0, target);.    
2176d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
2176e 20 20 20 63 61 73 65 20 54 4b 5f 53 54 52 49 4e     case TK_STRIN
2176f 47 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72  G: {.      asser
21770 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  t( !ExprHasPrope
21771 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e  rty(pExpr, EP_In
21772 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20  tValue) );.     
21773 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21774 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
21775 2c 20 30 2c 20 74 61 72 67 65 74 2c 20 30 2c 20  , 0, target, 0, 
21776 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c  pExpr->u.zToken,
21777 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   0);.      break
21778 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
21779 20 54 4b 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20   TK_NULL: {.    
2177a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2177b 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
2177c 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  0, target);.    
2177d 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
2177e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2177f 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a  IT_BLOB_LITERAL.
21780 20 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42      case TK_BLOB
21781 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b  : {.      int n;
21782 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
21783 72 20 2a 7a 3b 0a 20 20 20 20 20 20 63 68 61 72  r *z;.      char
21784 20 2a 7a 42 6c 6f 62 3b 0a 20 20 20 20 20 20 61   *zBlob;.      a
21785 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50  ssert( !ExprHasP
21786 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
21787 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20  P_IntValue) );. 
21788 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78       assert( pEx
21789 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d  pr->u.zToken[0]=
2178a 3d 27 78 27 20 7c 7c 20 70 45 78 70 72 2d 3e 75  ='x' || pExpr->u
2178b 2e 7a 54 6f 6b 65 6e 5b 30 5d 3d 3d 27 58 27 20  .zToken[0]=='X' 
2178c 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
2178d 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e   pExpr->u.zToken
2178e 5b 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20  [1]=='\'' );.   
2178f 20 20 20 7a 20 3d 20 26 70 45 78 70 72 2d 3e 75     z = &pExpr->u
21790 2e 7a 54 6f 6b 65 6e 5b 32 5d 3b 0a 20 20 20 20  .zToken[2];.    
21791 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72    n = sqlite3Str
21792 6c 65 6e 33 30 28 7a 29 20 2d 20 31 3b 0a 20 20  len30(z) - 1;.  
21793 20 20 20 20 61 73 73 65 72 74 28 20 7a 5b 6e 5d      assert( z[n]
21794 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20 20 20 20  =='\'' );.      
21795 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33 48  zBlob = sqlite3H
21796 65 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33  exToBlob(sqlite3
21797 56 64 62 65 44 62 28 76 29 2c 20 7a 2c 20 6e 29  VdbeDb(v), z, n)
21798 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
21799 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2179a 42 6c 6f 62 2c 20 6e 2f 32 2c 20 74 61 72 67 65  Blob, n/2, targe
2179b 74 2c 20 30 2c 20 7a 42 6c 6f 62 2c 20 50 34 5f  t, 0, zBlob, P4_
2179c 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20  DYNAMIC);.      
2179d 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e  break;.    }.#en
2179e 64 69 66 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  dif.    case TK_
2179f 56 41 52 49 41 42 4c 45 3a 20 7b 0a 20 20 20 20  VARIABLE: {.    
217a0 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20    VdbeOp *pOp;. 
217a1 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
217a2 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
217a3 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  xpr, EP_IntValue
217a4 29 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ) );.      asser
217a5 74 28 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  t( pExpr->u.zTok
217a6 65 6e 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61  en!=0 );.      a
217a7 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 75 2e  ssert( pExpr->u.
217a8 7a 54 6f 6b 65 6e 5b 30 5d 21 3d 30 20 29 3b 0a  zToken[0]!=0 );.
217a9 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
217aa 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 3d 3d 30 0a  >u.zToken[1]==0.
217ab 20 20 20 20 20 20 20 20 20 26 26 20 28 70 4f 70           && (pOp
217ac 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 47 65   = sqlite3VdbeGe
217ad 74 4f 70 28 76 2c 20 2d 31 29 29 2d 3e 6f 70 63  tOp(v, -1))->opc
217ae 6f 64 65 3d 3d 4f 50 5f 56 61 72 69 61 62 6c 65  ode==OP_Variable
217af 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 4f 70  .         && pOp
217b0 2d 3e 70 31 2b 70 4f 70 2d 3e 70 33 3d 3d 70 45  ->p1+pOp->p3==pE
217b1 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 0a 20 20 20  xpr->iColumn.   
217b2 20 20 20 20 20 20 26 26 20 70 4f 70 2d 3e 70 32        && pOp->p2
217b3 2b 70 4f 70 2d 3e 70 33 3d 3d 74 61 72 67 65 74  +pOp->p3==target
217b4 0a 20 20 20 20 20 20 20 20 20 26 26 20 70 4f 70  .         && pOp
217b5 2d 3e 70 34 2e 7a 3d 3d 30 0a 20 20 20 20 20 20  ->p4.z==0.      
217b6 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  ){.        /* If
217b7 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 69 6e   the previous in
217b8 73 74 72 75 63 74 69 6f 6e 20 77 61 73 20 61 20  struction was a 
217b9 63 6f 70 79 20 6f 66 20 74 68 65 20 70 72 65 76  copy of the prev
217ba 69 6f 75 73 20 75 6e 6e 61 6d 65 64 0a 20 20 20  ious unnamed.   
217bb 20 20 20 20 20 2a 2a 20 70 61 72 61 6d 65 74 65       ** paramete
217bc 72 20 69 6e 74 6f 20 74 68 65 20 70 72 65 76 69  r into the previ
217bd 6f 75 73 20 72 65 67 69 73 74 65 72 2c 20 74 68  ous register, th
217be 65 6e 20 73 69 6d 70 6c 79 20 69 6e 63 72 65 6d  en simply increm
217bf 65 6e 74 20 74 68 65 0a 20 20 20 20 20 20 20 20  ent the.        
217c0 2a 2a 20 72 65 70 65 61 74 20 63 6f 75 6e 74 20  ** repeat count 
217c1 6f 6e 20 74 68 65 20 70 72 69 6f 72 20 69 6e 73  on the prior ins
217c2 74 72 75 63 74 69 6f 6e 20 72 61 74 68 65 72 20  truction rather 
217c3 74 68 61 6e 20 6d 61 6b 69 6e 67 20 61 20 6e 65  than making a ne
217c4 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 73  w.        ** ins
217c5 74 72 75 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20  truction..      
217c6 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 4f 70    */.        pOp
217c7 2d 3e 70 33 2b 2b 3b 0a 20 20 20 20 20 20 7d 65  ->p3++;.      }e
217c8 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
217c9 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
217ca 2c 20 4f 50 5f 56 61 72 69 61 62 6c 65 2c 20 70  , OP_Variable, p
217cb 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 74  Expr->iColumn, t
217cc 61 72 67 65 74 2c 20 31 29 3b 0a 20 20 20 20 20  arget, 1);.     
217cd 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 75 2e     if( pExpr->u.
217ce 7a 54 6f 6b 65 6e 5b 31 5d 21 3d 30 20 29 7b 0a  zToken[1]!=0 ){.
217cf 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
217d0 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
217d1 20 2d 31 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54   -1, pExpr->u.zT
217d2 6f 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20  oken, 0);.      
217d3 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
217d4 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
217d5 20 20 20 63 61 73 65 20 54 4b 5f 52 45 47 49 53     case TK_REGIS
217d6 54 45 52 3a 20 7b 0a 20 20 20 20 20 20 69 6e 52  TER: {.      inR
217d7 65 67 20 3d 20 70 45 78 70 72 2d 3e 69 54 61 62  eg = pExpr->iTab
217d8 6c 65 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  le;.      break;
217d9 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
217da 54 4b 5f 41 53 3a 20 7b 0a 20 20 20 20 20 20 69  TK_AS: {.      i
217db 6e 52 65 67 20 3d 20 63 6f 64 65 41 6c 69 61 73  nReg = codeAlias
217dc 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
217dd 69 54 61 62 6c 65 2c 20 70 45 78 70 72 2d 3e 70  iTable, pExpr->p
217de 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b 0a 20  Left, target);. 
217df 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
217e0 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
217e1 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 20 20 63  _OMIT_CAST.    c
217e2 61 73 65 20 54 4b 5f 43 41 53 54 3a 20 7b 0a 20  ase TK_CAST: {. 
217e3 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69       /* Expressi
217e4 6f 6e 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a  ons of the form:
217e5 20 20 20 43 41 53 54 28 70 4c 65 66 74 20 41 53     CAST(pLeft AS
217e6 20 74 6f 6b 65 6e 29 20 2a 2f 0a 20 20 20 20 20   token) */.     
217e7 20 69 6e 74 20 61 66 66 2c 20 74 6f 5f 6f 70 3b   int aff, to_op;
217e8 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73  .      inReg = s
217e9 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61  qlite3ExprCodeTa
217ea 72 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78  rget(pParse, pEx
217eb 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65  pr->pLeft, targe
217ec 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  t);.      assert
217ed 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
217ee 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74  ty(pExpr, EP_Int
217ef 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20  Value) );.      
217f0 61 66 66 20 3d 20 73 71 6c 69 74 65 33 41 66 66  aff = sqlite3Aff
217f1 69 6e 69 74 79 54 79 70 65 28 70 45 78 70 72 2d  inityType(pExpr-
217f2 3e 75 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20  >u.zToken);.    
217f3 20 20 74 6f 5f 6f 70 20 3d 20 61 66 66 20 2d 20    to_op = aff - 
217f4 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20  SQLITE_AFF_TEXT 
217f5 2b 20 4f 50 5f 54 6f 54 65 78 74 3b 0a 20 20 20  + OP_ToText;.   
217f6 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70     assert( to_op
217f7 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 7c  ==OP_ToText    |
217f8 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46  | aff!=SQLITE_AF
217f9 46 5f 54 45 58 54 20 20 20 20 29 3b 0a 20 20 20  F_TEXT    );.   
217fa 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70     assert( to_op
217fb 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20 7c  ==OP_ToBlob    |
217fc 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46  | aff!=SQLITE_AF
217fd 46 5f 4e 4f 4e 45 20 20 20 20 29 3b 0a 20 20 20  F_NONE    );.   
217fe 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70     assert( to_op
217ff 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20 7c  ==OP_ToNumeric |
21800 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46  | aff!=SQLITE_AF
21801 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20 20  F_NUMERIC );.   
21802 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70     assert( to_op
21803 3d 3d 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20 7c  ==OP_ToInt     |
21804 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46  | aff!=SQLITE_AF
21805 46 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 20 20  F_INTEGER );.   
21806 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70     assert( to_op
21807 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20 7c  ==OP_ToReal    |
21808 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46  | aff!=SQLITE_AF
21809 46 5f 52 45 41 4c 20 20 20 20 29 3b 0a 20 20 20  F_REAL    );.   
2180a 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f     testcase( to_
2180b 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 29 3b  op==OP_ToText );
2180c 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2180d 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f   to_op==OP_ToBlo
2180e 62 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  b );.      testc
2180f 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54  ase( to_op==OP_T
21810 6f 4e 75 6d 65 72 69 63 20 29 3b 0a 20 20 20 20  oNumeric );.    
21811 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f    testcase( to_o
21812 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 29 3b 0a 20  p==OP_ToInt );. 
21813 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74       testcase( t
21814 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20  o_op==OP_ToReal 
21815 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 6e 52  );.      if( inR
21816 65 67 21 3d 74 61 72 67 65 74 20 29 7b 0a 20 20  eg!=target ){.  
21817 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21818 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43  eAddOp2(v, OP_SC
21819 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74 61 72 67  opy, inReg, targ
2181a 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 52  et);.        inR
2181b 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20 20 20  eg = target;.   
2181c 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
2181d 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2181e 74 6f 5f 6f 70 2c 20 69 6e 52 65 67 29 3b 0a 20  to_op, inReg);. 
2181f 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 75       testcase( u
21820 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65  sedAsColumnCache
21821 28 70 50 61 72 73 65 2c 20 69 6e 52 65 67 2c 20  (pParse, inReg, 
21822 69 6e 52 65 67 29 20 29 3b 0a 20 20 20 20 20 20  inReg) );.      
21823 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
21824 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
21825 50 61 72 73 65 2c 20 69 6e 52 65 67 2c 20 31 29  Parse, inReg, 1)
21826 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
21827 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53     }.#endif /* S
21828 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20  QLITE_OMIT_CAST 
21829 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c  */.    case TK_L
2182a 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c  T:.    case TK_L
2182b 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47  E:.    case TK_G
2182c 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47  T:.    case TK_G
2182d 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e  E:.    case TK_N
2182e 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45  E:.    case TK_E
2182f 51 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72  Q: {.      asser
21830 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74 20  t( TK_LT==OP_Lt 
21831 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
21832 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 29 3b   TK_LE==OP_Le );
21833 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
21834 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20  K_GT==OP_Gt );. 
21835 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
21836 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 20  GE==OP_Ge );.   
21837 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 45 51     assert( TK_EQ
21838 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20 20 20  ==OP_Eq );.     
21839 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 45 3d 3d   assert( TK_NE==
2183a 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20 74  OP_Ne );.      t
2183b 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
2183c 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  LT );.      test
2183d 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20  case( op==TK_LE 
2183e 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
2183f 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b 0a  e( op==TK_GT );.
21840 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21841 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 20 20  op==TK_GE );.   
21842 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
21843 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20 20 20  =TK_EQ );.      
21844 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
21845 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 63 6f 64  _NE );.      cod
21846 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73  eCompareOperands
21847 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
21848 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65 67  pLeft, &r1, &reg
21849 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20 20  Free1,.         
2184a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2184b 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
2184c 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72 65  pRight, &r2, &re
2184d 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 63  gFree2);.      c
2184e 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73  odeCompare(pPars
2184f 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  e, pExpr->pLeft,
21850 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20   pExpr->pRight, 
21851 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  op,.            
21852 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20 69 6e        r1, r2, in
21853 52 65 67 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52  Reg, SQLITE_STOR
21854 45 50 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74  EP2);.      test
21855 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
21856 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  0 );.      testc
21857 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30  ase( regFree2==0
21858 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
21859 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
2185a 54 4b 5f 49 53 3a 0a 20 20 20 20 63 61 73 65 20  TK_IS:.    case 
2185b 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a 20 20 20 20  TK_ISNOT: {.    
2185c 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
2185d 54 4b 5f 49 53 20 29 3b 0a 20 20 20 20 20 20 74  TK_IS );.      t
2185e 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
2185f 49 53 4e 4f 54 20 29 3b 0a 20 20 20 20 20 20 63  ISNOT );.      c
21860 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e  odeCompareOperan
21861 64 73 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  ds(pParse, pExpr
21862 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72  ->pLeft, &r1, &r
21863 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20  egFree1,.       
21864 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21865 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
21866 2d 3e 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26  ->pRight, &r2, &
21867 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20  regFree2);.     
21868 20 6f 70 20 3d 20 28 6f 70 3d 3d 54 4b 5f 49 53   op = (op==TK_IS
21869 29 20 3f 20 54 4b 5f 45 51 20 3a 20 54 4b 5f 4e  ) ? TK_EQ : TK_N
2186a 45 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d  E;.      codeCom
2186b 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78  pare(pParse, pEx
2186c 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72  pr->pLeft, pExpr
2186d 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20  ->pRight, op,.  
2186e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2186f 72 31 2c 20 72 32 2c 20 69 6e 52 65 67 2c 20 53  r1, r2, inReg, S
21870 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 7c 20  QLITE_STOREP2 | 
21871 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 29 3b 0a  SQLITE_NULLEQ);.
21872 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21873 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20  regFree1==0 );. 
21874 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
21875 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20  egFree2==0 );.  
21876 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
21877 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4e 44  .    case TK_AND
21878 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52  :.    case TK_OR
21879 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 50 4c  :.    case TK_PL
2187a 55 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  US:.    case TK_
2187b 53 54 41 52 3a 0a 20 20 20 20 63 61 73 65 20 54  STAR:.    case T
2187c 4b 5f 4d 49 4e 55 53 3a 0a 20 20 20 20 63 61 73  K_MINUS:.    cas
2187d 65 20 54 4b 5f 52 45 4d 3a 0a 20 20 20 20 63 61  e TK_REM:.    ca
2187e 73 65 20 54 4b 5f 42 49 54 41 4e 44 3a 0a 20 20  se TK_BITAND:.  
2187f 20 20 63 61 73 65 20 54 4b 5f 42 49 54 4f 52 3a    case TK_BITOR:
21880 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 4c 41  .    case TK_SLA
21881 53 48 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  SH:.    case TK_
21882 4c 53 48 49 46 54 3a 0a 20 20 20 20 63 61 73 65  LSHIFT:.    case
21883 20 54 4b 5f 52 53 48 49 46 54 3a 20 0a 20 20 20   TK_RSHIFT: .   
21884 20 63 61 73 65 20 54 4b 5f 43 4f 4e 43 41 54 3a   case TK_CONCAT:
21885 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28   {.      assert(
21886 20 54 4b 5f 41 4e 44 3d 3d 4f 50 5f 41 6e 64 20   TK_AND==OP_And 
21887 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
21888 20 54 4b 5f 4f 52 3d 3d 4f 50 5f 4f 72 20 29 3b   TK_OR==OP_Or );
21889 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
2188a 4b 5f 50 4c 55 53 3d 3d 4f 50 5f 41 64 64 20 29  K_PLUS==OP_Add )
2188b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2188c 54 4b 5f 4d 49 4e 55 53 3d 3d 4f 50 5f 53 75 62  TK_MINUS==OP_Sub
2188d 74 72 61 63 74 20 29 3b 0a 20 20 20 20 20 20 61  tract );.      a
2188e 73 73 65 72 74 28 20 54 4b 5f 52 45 4d 3d 3d 4f  ssert( TK_REM==O
2188f 50 5f 52 65 6d 61 69 6e 64 65 72 20 29 3b 0a 20  P_Remainder );. 
21890 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
21891 42 49 54 41 4e 44 3d 3d 4f 50 5f 42 69 74 41 6e  BITAND==OP_BitAn
21892 64 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  d );.      asser
21893 74 28 20 54 4b 5f 42 49 54 4f 52 3d 3d 4f 50 5f  t( TK_BITOR==OP_
21894 42 69 74 4f 72 20 29 3b 0a 20 20 20 20 20 20 61  BitOr );.      a
21895 73 73 65 72 74 28 20 54 4b 5f 53 4c 41 53 48 3d  ssert( TK_SLASH=
21896 3d 4f 50 5f 44 69 76 69 64 65 20 29 3b 0a 20 20  =OP_Divide );.  
21897 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c      assert( TK_L
21898 53 48 49 46 54 3d 3d 4f 50 5f 53 68 69 66 74 4c  SHIFT==OP_ShiftL
21899 65 66 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73  eft );.      ass
2189a 65 72 74 28 20 54 4b 5f 52 53 48 49 46 54 3d 3d  ert( TK_RSHIFT==
2189b 4f 50 5f 53 68 69 66 74 52 69 67 68 74 20 29 3b  OP_ShiftRight );
2189c 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
2189d 4b 5f 43 4f 4e 43 41 54 3d 3d 4f 50 5f 43 6f 6e  K_CONCAT==OP_Con
2189e 63 61 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73  cat );.      tes
2189f 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 41 4e  tcase( op==TK_AN
218a0 44 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  D );.      testc
218a1 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4f 52 20 29  ase( op==TK_OR )
218a2 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
218a3 28 20 6f 70 3d 3d 54 4b 5f 50 4c 55 53 20 29 3b  ( op==TK_PLUS );
218a4 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
218a5 20 6f 70 3d 3d 54 4b 5f 4d 49 4e 55 53 20 29 3b   op==TK_MINUS );
218a6 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
218a7 20 6f 70 3d 3d 54 4b 5f 52 45 4d 20 29 3b 0a 20   op==TK_REM );. 
218a8 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
218a9 70 3d 3d 54 4b 5f 42 49 54 41 4e 44 20 29 3b 0a  p==TK_BITAND );.
218aa 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
218ab 6f 70 3d 3d 54 4b 5f 42 49 54 4f 52 20 29 3b 0a  op==TK_BITOR );.
218ac 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
218ad 6f 70 3d 3d 54 4b 5f 53 4c 41 53 48 20 29 3b 0a  op==TK_SLASH );.
218ae 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
218af 6f 70 3d 3d 54 4b 5f 4c 53 48 49 46 54 20 29 3b  op==TK_LSHIFT );
218b0 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
218b1 20 6f 70 3d 3d 54 4b 5f 52 53 48 49 46 54 20 29   op==TK_RSHIFT )
218b2 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
218b3 28 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 43 41 54 20  ( op==TK_CONCAT 
218b4 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71  );.      r1 = sq
218b5 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
218b6 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  p(pParse, pExpr-
218b7 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65  >pLeft, &regFree
218b8 31 29 3b 0a 20 20 20 20 20 20 72 32 20 3d 20 73  1);.      r2 = s
218b9 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65  qlite3ExprCodeTe
218ba 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  mp(pParse, pExpr
218bb 2d 3e 70 52 69 67 68 74 2c 20 26 72 65 67 46 72  ->pRight, &regFr
218bc 65 65 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ee2);.      sqli
218bd 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
218be 20 6f 70 2c 20 72 32 2c 20 72 31 2c 20 74 61 72   op, r2, r1, tar
218bf 67 65 74 29 3b 0a 20 20 20 20 20 20 74 65 73 74  get);.      test
218c0 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
218c1 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  0 );.      testc
218c2 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30  ase( regFree2==0
218c3 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
218c4 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
218c5 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20  TK_UMINUS: {.   
218c6 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d     Expr *pLeft =
218c7 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20   pExpr->pLeft;. 
218c8 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 65       assert( pLe
218c9 66 74 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ft );.      if( 
218ca 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c  pLeft->op==TK_FL
218cb 4f 41 54 20 29 7b 0a 20 20 20 20 20 20 20 20 61  OAT ){.        a
218cc 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50  ssert( !ExprHasP
218cd 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
218ce 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20  P_IntValue) );. 
218cf 20 20 20 20 20 20 20 63 6f 64 65 52 65 61 6c 28         codeReal(
218d0 76 2c 20 70 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b  v, pLeft->u.zTok
218d1 65 6e 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a  en, 1, target);.
218d2 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
218d3 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e  pLeft->op==TK_IN
218d4 54 45 47 45 52 20 29 7b 0a 20 20 20 20 20 20 20  TEGER ){.       
218d5 20 63 6f 64 65 49 6e 74 65 67 65 72 28 76 2c 20   codeInteger(v, 
218d6 70 4c 65 66 74 2c 20 31 2c 20 74 61 72 67 65 74  pLeft, 1, target
218d7 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
218d8 20 20 20 20 20 20 20 20 72 65 67 46 72 65 65 31          regFree1
218d9 20 3d 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47   = r1 = sqlite3G
218da 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
218db 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
218dc 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
218dd 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 72  OP_Integer, 0, r
218de 31 29 3b 0a 20 20 20 20 20 20 20 20 72 32 20 3d  1);.        r2 =
218df 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
218e0 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78  Temp(pParse, pEx
218e1 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46  pr->pLeft, &regF
218e2 72 65 65 32 29 3b 0a 20 20 20 20 20 20 20 20 73  ree2);.        s
218e3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
218e4 28 76 2c 20 4f 50 5f 53 75 62 74 72 61 63 74 2c  (v, OP_Subtract,
218e5 20 72 32 2c 20 72 31 2c 20 74 61 72 67 65 74 29   r2, r1, target)
218e6 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  ;.        testca
218e7 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20  se( regFree2==0 
218e8 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
218e9 20 69 6e 52 65 67 20 3d 20 74 61 72 67 65 74 3b   inReg = target;
218ea 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
218eb 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
218ec 42 49 54 4e 4f 54 3a 0a 20 20 20 20 63 61 73 65  BITNOT:.    case
218ed 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20 20 20 20   TK_NOT: {.     
218ee 20 61 73 73 65 72 74 28 20 54 4b 5f 42 49 54 4e   assert( TK_BITN
218ef 4f 54 3d 3d 4f 50 5f 42 69 74 4e 6f 74 20 29 3b  OT==OP_BitNot );
218f0 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
218f1 4b 5f 4e 4f 54 3d 3d 4f 50 5f 4e 6f 74 20 29 3b  K_NOT==OP_Not );
218f2 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
218f3 20 6f 70 3d 3d 54 4b 5f 42 49 54 4e 4f 54 20 29   op==TK_BITNOT )
218f4 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
218f5 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 20 29 3b 0a  ( op==TK_NOT );.
218f6 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74        r1 = sqlit
218f7 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70  e3ExprCodeTemp(p
218f8 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
218f9 65 66 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b  eft, &regFree1);
218fa 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
218fb 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a   regFree1==0 );.
218fc 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61        inReg = ta
218fd 72 67 65 74 3b 0a 20 20 20 20 20 20 73 71 6c 69  rget;.      sqli
218fe 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
218ff 20 6f 70 2c 20 72 31 2c 20 69 6e 52 65 67 29 3b   op, r1, inReg);
21900 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
21901 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
21902 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65  ISNULL:.    case
21903 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20   TK_NOTNULL: {. 
21904 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20       int addr;. 
21905 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
21906 49 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 6c  ISNULL==OP_IsNul
21907 6c 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  l );.      asser
21908 74 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f  t( TK_NOTNULL==O
21909 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 20  P_NotNull );.   
2190a 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
2190b 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20  =TK_ISNULL );.  
2190c 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
2190d 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a  ==TK_NOTNULL );.
2190e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2190f 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
21910 74 65 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74  teger, 1, target
21911 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71  );.      r1 = sq
21912 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
21913 70 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  p(pParse, pExpr-
21914 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72 65 65  >pLeft, &regFree
21915 31 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  1);.      testca
21916 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20  se( regFree1==0 
21917 29 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20  );.      addr = 
21918 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21919 31 28 76 2c 20 6f 70 2c 20 72 31 29 3b 0a 20 20  1(v, op, r1);.  
2191a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2191b 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
2191c 6d 6d 2c 20 74 61 72 67 65 74 2c 20 2d 31 29 3b  mm, target, -1);
2191d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2191e 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
2191f 64 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  dr);.      break
21920 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
21921 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e   TK_AGG_FUNCTION
21922 3a 20 7b 0a 20 20 20 20 20 20 41 67 67 49 6e 66  : {.      AggInf
21923 6f 20 2a 70 49 6e 66 6f 20 3d 20 70 45 78 70 72  o *pInfo = pExpr
21924 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20  ->pAggInfo;.    
21925 20 20 69 66 28 20 70 49 6e 66 6f 3d 3d 30 20 29    if( pInfo==0 )
21926 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
21927 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
21928 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74  ty(pExpr, EP_Int
21929 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20  Value) );.      
2192a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2192b 67 28 70 50 61 72 73 65 2c 20 22 6d 69 73 75 73  g(pParse, "misus
2192c 65 20 6f 66 20 61 67 67 72 65 67 61 74 65 3a 20  e of aggregate: 
2192d 25 73 28 29 22 2c 20 70 45 78 70 72 2d 3e 75 2e  %s()", pExpr->u.
2192e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d  zToken);.      }
2192f 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e  else{.        in
21930 52 65 67 20 3d 20 70 49 6e 66 6f 2d 3e 61 46 75  Reg = pInfo->aFu
21931 6e 63 5b 70 45 78 70 72 2d 3e 69 41 67 67 5d 2e  nc[pExpr->iAgg].
21932 69 4d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  iMem;.      }.  
21933 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
21934 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e  .    case TK_CON
21935 53 54 5f 46 55 4e 43 3a 0a 20 20 20 20 63 61 73  ST_FUNC:.    cas
21936 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b  e TK_FUNCTION: {
21937 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74 20  .      ExprList 
21938 2a 70 46 61 72 67 3b 20 20 20 20 20 20 20 2f 2a  *pFarg;       /*
21939 20 4c 69 73 74 20 6f 66 20 66 75 6e 63 74 69 6f   List of functio
2193a 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20  n arguments */. 
2193b 20 20 20 20 20 69 6e 74 20 6e 46 61 72 67 3b 20       int nFarg; 
2193c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2193d 75 6d 62 65 72 20 6f 66 20 66 75 6e 63 74 69 6f  umber of functio
2193e 6e 20 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20  n arguments */. 
2193f 20 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44       FuncDef *pD
21940 65 66 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ef;         /* T
21941 68 65 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69  he function defi
21942 6e 69 74 69 6f 6e 20 6f 62 6a 65 63 74 20 2a 2f  nition object */
21943 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20  .      int nId; 
21944 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
21945 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 66   Length of the f
21946 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 69 6e 20  unction name in 
21947 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 20 20 63  bytes */.      c
21948 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 3b 20  onst char *zId; 
21949 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6e        /* The fun
2194a 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20  ction name */.  
2194b 20 20 20 20 69 6e 74 20 63 6f 6e 73 74 4d 61 73      int constMas
2194c 6b 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4d 61  k = 0;     /* Ma
2194d 73 6b 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20 61  sk of function a
2194e 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20 61 72  rguments that ar
2194f 65 20 63 6f 6e 73 74 61 6e 74 20 2a 2f 0a 20 20  e constant */.  
21950 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20      int i;      
21951 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
21952 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
21953 20 20 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43      u8 enc = ENC
21954 28 64 62 29 3b 20 20 20 20 20 20 2f 2a 20 54 68  (db);      /* Th
21955 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  e text encoding 
21956 75 73 65 64 20 62 79 20 74 68 69 73 20 64 61 74  used by this dat
21957 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 43  abase */.      C
21958 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20  ollSeq *pColl = 
21959 30 3b 20 20 20 20 2f 2a 20 41 20 63 6f 6c 6c 61  0;    /* A colla
2195a 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f  ting sequence */
2195b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ..      assert( 
2195c 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79  !ExprHasProperty
2195d 28 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65  (pExpr, EP_xIsSe
2195e 6c 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 74  lect) );.      t
2195f 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
21960 43 4f 4e 53 54 5f 46 55 4e 43 20 29 3b 0a 20 20  CONST_FUNC );.  
21961 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
21962 3d 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20 29 3b  ==TK_FUNCTION );
21963 0a 20 20 20 20 20 20 69 66 28 20 45 78 70 72 48  .      if( ExprH
21964 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45  asAnyProperty(pE
21965 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c  xpr, EP_TokenOnl
21966 79 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46  y) ){.        pF
21967 61 72 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  arg = 0;.      }
21968 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 46  else{.        pF
21969 61 72 67 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70  arg = pExpr->x.p
2196a 4c 69 73 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  List;.      }.  
2196b 20 20 20 20 6e 46 61 72 67 20 3d 20 70 46 61 72      nFarg = pFar
2196c 67 20 3f 20 70 46 61 72 67 2d 3e 6e 45 78 70 72  g ? pFarg->nExpr
2196d 20 3a 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65   : 0;.      asse
2196e 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
2196f 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49  erty(pExpr, EP_I
21970 6e 74 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20  ntValue) );.    
21971 20 20 7a 49 64 20 3d 20 70 45 78 70 72 2d 3e 75    zId = pExpr->u
21972 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 6e  .zToken;.      n
21973 49 64 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c  Id = sqlite3Strl
21974 65 6e 33 30 28 7a 49 64 29 3b 0a 20 20 20 20 20  en30(zId);.     
21975 20 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46   pDef = sqlite3F
21976 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20  indFunction(db, 
21977 7a 49 64 2c 20 6e 49 64 2c 20 6e 46 61 72 67 2c  zId, nId, nFarg,
21978 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20   enc, 0);.      
21979 69 66 28 20 70 44 65 66 3d 3d 30 20 29 7b 0a 20  if( pDef==0 ){. 
2197a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
2197b 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2197c 75 6e 6b 6e 6f 77 6e 20 66 75 6e 63 74 69 6f 6e  unknown function
2197d 3a 20 25 2e 2a 73 28 29 22 2c 20 6e 49 64 2c 20  : %.*s()", nId, 
2197e 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 20 62 72  zId);.        br
2197f 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
21980 20 20 20 69 66 28 20 70 46 61 72 67 20 29 7b 0a     if( pFarg ){.
21981 20 20 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c          r1 = sql
21982 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65  ite3GetTempRange
21983 28 70 50 61 72 73 65 2c 20 6e 46 61 72 67 29 3b  (pParse, nFarg);
21984 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21985 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50  ExprCachePush(pP
21986 61 72 73 65 29 3b 20 20 20 20 20 2f 2a 20 54 69  arse);     /* Ti
21987 63 6b 65 74 20 32 65 61 32 34 32 35 64 33 34 62  cket 2ea2425d34b
21988 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c  e */.        sql
21989 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 72  ite3ExprCodeExpr
2198a 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 46 61  List(pParse, pFa
2198b 72 67 2c 20 72 31 2c 20 31 29 3b 0a 20 20 20 20  rg, r1, 1);.    
2198c 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2198d 61 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20  achePop(pParse, 
2198e 31 29 3b 20 20 20 2f 2a 20 54 69 63 6b 65 74 20  1);   /* Ticket 
2198f 32 65 61 32 34 32 35 64 33 34 62 65 20 2a 2f 0a  2ea2425d34be */.
21990 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
21991 20 20 20 20 20 72 31 20 3d 20 30 3b 0a 20 20 20       r1 = 0;.   
21992 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
21993 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
21994 54 41 42 4c 45 0a 20 20 20 20 20 20 2f 2a 20 50  TABLE.      /* P
21995 6f 73 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61 64  ossibly overload
21996 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 66   the function if
21997 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
21998 65 6e 74 20 69 73 0a 20 20 20 20 20 20 2a 2a 20  ent is.      ** 
21999 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
2199a 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a 2a  column..      **
2199b 0a 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 69 6e  .      ** For in
2199c 66 69 78 20 66 75 6e 63 74 69 6f 6e 73 20 28 4c  fix functions (L
2199d 49 4b 45 2c 20 47 4c 4f 42 2c 20 52 45 47 45 58  IKE, GLOB, REGEX
2199e 50 2c 20 61 6e 64 20 4d 41 54 43 48 29 20 75 73  P, and MATCH) us
2199f 65 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 73  e the.      ** s
219a0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20  econd argument, 
219a1 6e 6f 74 20 74 68 65 20 66 69 72 73 74 2c 20 61  not the first, a
219a2 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 74  s the argument t
219a3 6f 20 74 65 73 74 20 74 6f 0a 20 20 20 20 20 20  o test to.      
219a4 2a 2a 20 73 65 65 20 69 66 20 69 74 20 69 73 20  ** see if it is 
219a5 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69  a column in a vi
219a6 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68  rtual table.  Th
219a7 69 73 20 69 73 20 64 6f 6e 65 20 62 65 63 61 75  is is done becau
219a8 73 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  se.      ** the 
219a9 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 6f 66 20  left operand of 
219aa 69 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e 73 20  infix functions 
219ab 28 74 68 65 20 6f 70 65 72 61 6e 64 20 77 65 20  (the operand we 
219ac 77 61 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a  want to.      **
219ad 20 63 6f 6e 74 72 6f 6c 20 6f 76 65 72 6c 6f 61   control overloa
219ae 64 69 6e 67 29 20 65 6e 64 73 20 75 70 20 61 73  ding) ends up as
219af 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
219b0 6d 65 6e 74 20 74 6f 20 74 68 65 0a 20 20 20 20  ment to the.    
219b1 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20 20    ** function.  
219b2 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 22  The expression "
219b3 41 20 67 6c 6f 62 20 42 22 20 69 73 20 65 71 75  A glob B" is equ
219b4 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20  ivalent to .    
219b5 20 20 2a 2a 20 22 67 6c 6f 62 28 42 2c 41 29 2e    ** "glob(B,A).
219b6 20 20 57 65 20 77 61 6e 74 20 74 6f 20 75 73 65    We want to use
219b7 20 74 68 65 20 41 20 69 6e 20 22 41 20 67 6c 6f   the A in "A glo
219b8 62 20 42 22 20 74 6f 20 74 65 73 74 0a 20 20 20  b B" to test.   
219b9 20 20 20 2a 2a 20 66 6f 72 20 66 75 6e 63 74 69     ** for functi
219ba 6f 6e 20 6f 76 65 72 6c 6f 61 64 69 6e 67 2e 20  on overloading. 
219bb 20 42 75 74 20 77 65 20 75 73 65 20 74 68 65 20   But we use the 
219bc 42 20 74 65 72 6d 20 69 6e 20 22 67 6c 6f 62 28  B term in "glob(
219bd 42 2c 41 29 22 2e 0a 20 20 20 20 20 20 2a 2f 0a  B,A)"..      */.
219be 20 20 20 20 20 20 69 66 28 20 6e 46 61 72 67 3e        if( nFarg>
219bf 3d 32 20 26 26 20 28 70 45 78 70 72 2d 3e 66 6c  =2 && (pExpr->fl
219c0 61 67 73 20 26 20 45 50 5f 49 6e 66 69 78 46 75  ags & EP_InfixFu
219c1 6e 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70  nc) ){.        p
219c2 44 65 66 20 3d 20 73 71 6c 69 74 65 33 56 74 61  Def = sqlite3Vta
219c3 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f  bOverloadFunctio
219c4 6e 28 64 62 2c 20 70 44 65 66 2c 20 6e 46 61 72  n(db, pDef, nFar
219c5 67 2c 20 70 46 61 72 67 2d 3e 61 5b 31 5d 2e 70  g, pFarg->a[1].p
219c6 45 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c  Expr);.      }el
219c7 73 65 20 69 66 28 20 6e 46 61 72 67 3e 30 20 29  se if( nFarg>0 )
219c8 7b 0a 20 20 20 20 20 20 20 20 70 44 65 66 20 3d  {.        pDef =
219c9 20 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65 72   sqlite3VtabOver
219ca 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c  loadFunction(db,
219cb 20 70 44 65 66 2c 20 6e 46 61 72 67 2c 20 70 46   pDef, nFarg, pF
219cc 61 72 67 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 29  arg->a[0].pExpr)
219cd 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  ;.      }.#endif
219ce 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
219cf 69 3c 6e 46 61 72 67 3b 20 69 2b 2b 29 7b 0a 20  i<nFarg; i++){. 
219d0 20 20 20 20 20 20 20 69 66 28 20 69 3c 33 32 20         if( i<32 
219d1 26 26 20 73 71 6c 69 74 65 33 45 78 70 72 49 73  && sqlite3ExprIs
219d2 43 6f 6e 73 74 61 6e 74 28 70 46 61 72 67 2d 3e  Constant(pFarg->
219d3 61 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a 20  a[i].pExpr) ){. 
219d4 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 4d 61           constMa
219d5 73 6b 20 7c 3d 20 28 31 3c 3c 69 29 3b 0a 20 20  sk |= (1<<i);.  
219d6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
219d7 69 66 28 20 28 70 44 65 66 2d 3e 66 6c 61 67 73  if( (pDef->flags
219d8 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e   & SQLITE_FUNC_N
219d9 45 45 44 43 4f 4c 4c 29 21 3d 30 20 26 26 20 21  EEDCOLL)!=0 && !
219da 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20  pColl ){.       
219db 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74     pColl = sqlit
219dc 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50  e3ExprCollSeq(pP
219dd 61 72 73 65 2c 20 70 46 61 72 67 2d 3e 61 5b 69  arse, pFarg->a[i
219de 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20  ].pExpr);.      
219df 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
219e0 20 20 69 66 28 20 70 44 65 66 2d 3e 66 6c 61 67    if( pDef->flag
219e1 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  s & SQLITE_FUNC_
219e2 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20  NEEDCOLL ){.    
219e3 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20 29      if( !pColl )
219e4 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66   pColl = db->pDf
219e5 6c 74 43 6f 6c 6c 3b 20 0a 20 20 20 20 20 20 20  ltColl; .       
219e6 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
219e7 70 34 28 76 2c 20 4f 50 5f 43 6f 6c 6c 53 65 71  p4(v, OP_CollSeq
219e8 2c 20 30 2c 20 30 2c 20 30 2c 20 28 63 68 61 72  , 0, 0, 0, (char
219e9 20 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f 4c   *)pColl, P4_COL
219ea 4c 53 45 51 29 3b 0a 20 20 20 20 20 20 7d 0a 20  LSEQ);.      }. 
219eb 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
219ec 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 46 75 6e  AddOp4(v, OP_Fun
219ed 63 74 69 6f 6e 2c 20 63 6f 6e 73 74 4d 61 73 6b  ction, constMask
219ee 2c 20 72 31 2c 20 74 61 72 67 65 74 2c 0a 20 20  , r1, target,.  
219ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
219f0 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 44 65        (char*)pDe
219f1 66 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a  f, P4_FUNCDEF);.
219f2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
219f3 65 43 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38  eChangeP5(v, (u8
219f4 29 6e 46 61 72 67 29 3b 0a 20 20 20 20 20 20 69  )nFarg);.      i
219f5 66 28 20 6e 46 61 72 67 20 29 7b 0a 20 20 20 20  f( nFarg ){.    
219f6 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
219f7 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  seTempRange(pPar
219f8 73 65 2c 20 72 31 2c 20 6e 46 61 72 67 29 3b 0a  se, r1, nFarg);.
219f9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
219fa 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66  lite3ExprCacheAf
219fb 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61  finityChange(pPa
219fc 72 73 65 2c 20 72 31 2c 20 6e 46 61 72 67 29 3b  rse, r1, nFarg);
219fd 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
219fe 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
219ff 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
21a00 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58 49  .    case TK_EXI
21a01 53 54 53 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  STS:.    case TK
21a02 5f 53 45 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20  _SELECT: {.     
21a03 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
21a04 4b 5f 45 58 49 53 54 53 20 29 3b 0a 20 20 20 20  K_EXISTS );.    
21a05 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
21a06 54 4b 5f 53 45 4c 45 43 54 20 29 3b 0a 20 20 20  TK_SELECT );.   
21a07 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74     inReg = sqlit
21a08 65 33 43 6f 64 65 53 75 62 73 65 6c 65 63 74 28  e3CodeSubselect(
21a09 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 30  pParse, pExpr, 0
21a0a 2c 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61  , 0);.      brea
21a0b 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
21a0c 65 20 54 4b 5f 49 4e 3a 20 7b 0a 20 20 20 20 20  e TK_IN: {.     
21a0d 20 69 6e 74 20 72 4e 6f 74 46 6f 75 6e 64 20 3d   int rNotFound =
21a0e 20 30 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 4d   0;.      int rM
21a0f 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a  ayHaveNull = 0;.
21a10 20 20 20 20 20 20 69 6e 74 20 6a 32 2c 20 6a 33        int j2, j3
21a11 2c 20 6a 34 2c 20 6a 35 3b 0a 20 20 20 20 20 20  , j4, j5;.      
21a12 63 68 61 72 20 61 66 66 69 6e 69 74 79 3b 0a 20  char affinity;. 
21a13 20 20 20 20 20 69 6e 74 20 65 54 79 70 65 3b 0a       int eType;.
21a14 0a 20 20 20 20 20 20 56 64 62 65 4e 6f 6f 70 43  .      VdbeNoopC
21a15 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 62 65 67 69  omment((v, "begi
21a16 6e 20 49 4e 20 65 78 70 72 20 72 25 64 22 2c 20  n IN expr r%d", 
21a17 74 61 72 67 65 74 29 29 3b 0a 20 20 20 20 20 20  target));.      
21a18 65 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 46  eType = sqlite3F
21a19 69 6e 64 49 6e 49 6e 64 65 78 28 70 50 61 72 73  indInIndex(pPars
21a1a 65 2c 20 70 45 78 70 72 2c 20 26 72 4d 61 79 48  e, pExpr, &rMayH
21a1b 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  aveNull);.      
21a1c 69 66 28 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c  if( rMayHaveNull
21a1d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 4e 6f 74   ){.        rNot
21a1e 46 6f 75 6e 64 20 3d 20 2b 2b 70 50 61 72 73 65  Found = ++pParse
21a1f 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 7d 0a  ->nMem;.      }.
21a20 0a 20 20 20 20 20 20 2f 2a 20 46 69 67 75 72 65  .      /* Figure
21a21 20 6f 75 74 20 74 68 65 20 61 66 66 69 6e 69 74   out the affinit
21a22 79 20 74 6f 20 75 73 65 20 74 6f 20 63 72 65 61  y to use to crea
21a23 74 65 20 61 20 6b 65 79 20 66 72 6f 6d 20 74 68  te a key from th
21a24 65 20 72 65 73 75 6c 74 73 0a 20 20 20 20 20 20  e results.      
21a25 2a 2a 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  ** of the expres
21a26 73 69 6f 6e 2e 20 61 66 66 69 6e 69 74 79 53 74  sion. affinitySt
21a27 72 20 73 74 6f 72 65 73 20 61 20 73 74 61 74 69  r stores a stati
21a28 63 20 73 74 72 69 6e 67 20 73 75 69 74 61 62 6c  c string suitabl
21a29 65 20 66 6f 72 0a 20 20 20 20 20 20 2a 2a 20 50  e for.      ** P
21a2a 34 20 6f 66 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  4 of OP_MakeReco
21a2b 72 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  rd..      */.   
21a2c 20 20 20 61 66 66 69 6e 69 74 79 20 3d 20 63 6f     affinity = co
21a2d 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69 74 79  mparisonAffinity
21a2e 28 70 45 78 70 72 29 3b 0a 0a 0a 20 20 20 20 20  (pExpr);...     
21a2f 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 3c 65 78   /* Code the <ex
21a30 70 72 3e 20 66 72 6f 6d 20 22 3c 65 78 70 72 3e  pr> from "<expr>
21a31 20 49 4e 20 28 2e 2e 2e 29 22 2e 20 54 68 65 20   IN (...)". The 
21a32 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 0a  temporary table.
21a33 20 20 20 20 20 20 2a 2a 20 70 45 78 70 72 2d 3e        ** pExpr->
21a34 69 54 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20  iTable contains 
21a35 74 68 65 20 76 61 6c 75 65 73 20 74 68 61 74 20  the values that 
21a36 6d 61 6b 65 20 75 70 20 74 68 65 20 28 2e 2e 2e  make up the (...
21a37 29 20 73 65 74 2e 0a 20 20 20 20 20 20 2a 2f 0a  ) set..      */.
21a38 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
21a39 72 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73  rCachePush(pPars
21a3a 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e);.      sqlite
21a3b 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
21a3c 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
21a3d 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 6a  target);.      j
21a3e 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  2 = sqlite3VdbeA
21a3f 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 73 4e 75  ddOp1(v, OP_IsNu
21a40 6c 6c 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20  ll, target);.   
21a41 20 20 20 69 66 28 20 65 54 79 70 65 3d 3d 49 4e     if( eType==IN
21a42 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 29 7b 0a  _INDEX_ROWID ){.
21a43 20 20 20 20 20 20 20 20 6a 33 20 3d 20 73 71 6c          j3 = sql
21a44 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
21a45 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20  , OP_MustBeInt, 
21a46 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20  target);.       
21a47 20 6a 34 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j4 = sqlite3Vdb
21a48 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f  eAddOp3(v, OP_No
21a49 74 45 78 69 73 74 73 2c 20 70 45 78 70 72 2d 3e  tExists, pExpr->
21a4a 69 54 61 62 6c 65 2c 20 30 2c 20 74 61 72 67 65  iTable, 0, targe
21a4b 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  t);.        sqli
21a4c 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
21a4d 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20   OP_Integer, 1, 
21a4e 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20  target);.       
21a4f 20 6a 35 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j5 = sqlite3Vdb
21a50 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f  eAddOp0(v, OP_Go
21a51 74 6f 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  to);.        sql
21a52 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
21a53 28 76 2c 20 6a 33 29 3b 0a 20 20 20 20 20 20 20  (v, j3);.       
21a54 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
21a55 48 65 72 65 28 76 2c 20 6a 34 29 3b 0a 20 20 20  Here(v, j4);.   
21a56 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21a57 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
21a58 65 67 65 72 2c 20 30 2c 20 74 61 72 67 65 74 29  eger, 0, target)
21a59 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
21a5a 20 20 20 20 20 20 20 72 32 20 3d 20 72 65 67 46         r2 = regF
21a5b 72 65 65 32 20 3d 20 73 71 6c 69 74 65 33 47 65  ree2 = sqlite3Ge
21a5c 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
21a5d 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 72  ;..        /* Cr
21a5e 65 61 74 65 20 61 20 72 65 63 6f 72 64 20 61 6e  eate a record an
21a5f 64 20 74 65 73 74 20 66 6f 72 20 73 65 74 20 6d  d test for set m
21a60 65 6d 62 65 72 73 68 69 70 2e 20 49 66 20 74 68  embership. If th
21a61 65 20 73 65 74 20 63 6f 6e 74 61 69 6e 73 0a 20  e set contains. 
21a62 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 76 61         ** the va
21a63 6c 75 65 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74  lue, then jump t
21a64 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  o the end of the
21a65 20 74 65 73 74 20 63 6f 64 65 2e 20 54 68 65 20   test code. The 
21a66 74 61 72 67 65 74 0a 20 20 20 20 20 20 20 20 2a  target.        *
21a67 2a 20 72 65 67 69 73 74 65 72 20 73 74 69 6c 6c  * register still
21a68 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 72   contains the tr
21a69 75 65 20 28 31 29 20 76 61 6c 75 65 20 77 72 69  ue (1) value wri
21a6a 74 74 65 6e 20 74 6f 20 69 74 20 65 61 72 6c 69  tten to it earli
21a6b 65 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  er..        */. 
21a6c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
21a6d 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d  beAddOp4(v, OP_M
21a6e 61 6b 65 52 65 63 6f 72 64 2c 20 74 61 72 67 65  akeRecord, targe
21a6f 74 2c 20 31 2c 20 72 32 2c 20 26 61 66 66 69 6e  t, 1, r2, &affin
21a70 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  ity, 1);.       
21a71 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21a72 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
21a73 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  , 1, target);.  
21a74 20 20 20 20 20 20 6a 35 20 3d 20 73 71 6c 69 74        j5 = sqlit
21a75 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
21a76 4f 50 5f 46 6f 75 6e 64 2c 20 70 45 78 70 72 2d  OP_Found, pExpr-
21a77 3e 69 54 61 62 6c 65 2c 20 30 2c 20 72 32 29 3b  >iTable, 0, r2);
21a78 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20  ..        /* If 
21a79 74 68 65 20 73 65 74 20 6d 65 6d 62 65 72 73 68  the set membersh
21a7a 69 70 20 74 65 73 74 20 66 61 69 6c 73 2c 20 74  ip test fails, t
21a7b 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f  hen the result o
21a7c 66 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a  f the .        *
21a7d 2a 20 22 78 20 49 4e 20 28 2e 2e 2e 29 22 20 65  * "x IN (...)" e
21a7e 78 70 72 65 73 73 69 6f 6e 20 6d 75 73 74 20 62  xpression must b
21a7f 65 20 65 69 74 68 65 72 20 30 20 6f 72 20 4e 55  e either 0 or NU
21a80 4c 4c 2e 20 49 66 20 74 68 65 20 73 65 74 0a 20  LL. If the set. 
21a81 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69         ** contai
21a82 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76 61 6c 75 65  ns no NULL value
21a83 73 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  s, then the resu
21a84 6c 74 20 69 73 20 30 2e 20 49 66 20 74 68 65 20  lt is 0. If the 
21a85 73 65 74 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  set .        ** 
21a86 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20  contains one or 
21a87 6d 6f 72 65 20 4e 55 4c 4c 20 76 61 6c 75 65 73  more NULL values
21a88 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  , then the resul
21a89 74 20 6f 66 20 74 68 65 0a 20 20 20 20 20 20 20  t of the.       
21a8a 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 69   ** expression i
21a8b 73 20 61 6c 73 6f 20 4e 55 4c 4c 2e 0a 20 20 20  s also NULL..   
21a8c 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
21a8d 69 66 28 20 72 4e 6f 74 46 6f 75 6e 64 3d 3d 30  if( rNotFound==0
21a8e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
21a8f 20 54 68 69 73 20 62 72 61 6e 63 68 20 72 75 6e   This branch run
21a90 73 20 69 66 20 69 74 20 69 73 20 6b 6e 6f 77 6e  s if it is known
21a91 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65   at compile time
21a92 20 28 6e 6f 77 29 20 74 68 61 74 20 0a 20 20 20   (now) that .   
21a93 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 73 65         ** the se
21a94 74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 4e 55  t contains no NU
21a95 4c 4c 20 76 61 6c 75 65 73 2e 20 54 68 69 73 20  LL values. This 
21a96 68 61 70 70 65 6e 73 20 61 73 20 74 68 65 20 72  happens as the r
21a97 65 73 75 6c 74 0a 20 20 20 20 20 20 20 20 20 20  esult.          
21a98 2a 2a 20 6f 66 20 61 20 22 4e 4f 54 20 4e 55 4c  ** of a "NOT NUL
21a99 4c 22 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 6e  L" constraint in
21a9a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
21a9b 68 65 6d 61 2e 20 4e 6f 20 6e 65 65 64 0a 20 20  hema. No need.  
21a9c 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 65          ** to te
21a9d 73 74 20 74 68 65 20 64 61 74 61 20 73 74 72 75  st the data stru
21a9e 63 74 75 72 65 20 61 74 20 72 75 6e 74 69 6d 65  cture at runtime
21a9f 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20   in this case.. 
21aa0 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
21aa1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21aa2 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
21aa3 74 65 67 65 72 2c 20 30 2c 20 74 61 72 67 65 74  teger, 0, target
21aa4 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  );.        }else
21aa5 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  {.          /* T
21aa6 68 69 73 20 62 6c 6f 63 6b 20 70 6f 70 75 6c 61  his block popula
21aa7 74 65 73 20 74 68 65 20 72 4e 6f 74 46 6f 75 6e  tes the rNotFoun
21aa8 64 20 72 65 67 69 73 74 65 72 20 77 69 74 68 20  d register with 
21aa9 65 69 74 68 65 72 20 4e 55 4c 4c 0a 20 20 20 20  either NULL.    
21aaa 20 20 20 20 20 20 2a 2a 20 6f 72 20 30 20 28 61        ** or 0 (a
21aab 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 29  n integer value)
21aac 2e 20 49 66 20 74 68 65 20 64 61 74 61 20 73 74  . If the data st
21aad 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73  ructure contains
21aae 20 6f 6e 65 0a 20 20 20 20 20 20 20 20 20 20 2a   one.          *
21aaf 2a 20 6f 72 20 6d 6f 72 65 20 4e 55 4c 4c 73 2c  * or more NULLs,
21ab0 20 74 68 65 6e 20 73 65 74 20 72 4e 6f 74 46 6f   then set rNotFo
21ab1 75 6e 64 20 74 6f 20 4e 55 4c 4c 2e 20 4f 74 68  und to NULL. Oth
21ab2 65 72 77 69 73 65 2c 20 73 65 74 20 69 74 0a 20  erwise, set it. 
21ab3 20 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 30           ** to 0
21ab4 2e 20 49 66 20 72 65 67 69 73 74 65 72 20 72 4d  . If register rM
21ab5 61 79 48 61 76 65 4e 75 6c 6c 20 69 73 20 61 6c  ayHaveNull is al
21ab6 72 65 61 64 79 20 73 65 74 20 74 6f 20 73 6f 6d  ready set to som
21ab7 65 20 76 61 6c 75 65 0a 20 20 20 20 20 20 20 20  e value.        
21ab8 20 20 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e 20    ** other than 
21ab9 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20 74  NULL, then the t
21aba 65 73 74 20 68 61 73 20 61 6c 72 65 61 64 79 20  est has already 
21abb 62 65 65 6e 20 72 75 6e 20 61 6e 64 20 0a 20 20  been run and .  
21abc 20 20 20 20 20 20 20 20 2a 2a 20 72 4e 6f 74 46          ** rNotF
21abd 6f 75 6e 64 20 69 73 20 61 6c 72 65 61 64 79 20  ound is already 
21abe 70 6f 70 75 6c 61 74 65 64 2e 0a 20 20 20 20 20  populated..     
21abf 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
21ac0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
21ac1 68 61 72 20 6e 75 6c 6c 52 65 63 6f 72 64 5b 5d  har nullRecord[]
21ac2 20 3d 20 7b 20 30 78 30 32 2c 20 30 78 30 30 20   = { 0x02, 0x00 
21ac3 7d 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 33 20  };.          j3 
21ac4 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
21ac5 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c  Op1(v, OP_NotNul
21ac6 6c 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 29  l, rMayHaveNull)
21ac7 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
21ac8 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
21ac9 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 4e 6f   OP_Null, 0, rNo
21aca 74 46 6f 75 6e 64 29 3b 0a 20 20 20 20 20 20 20  tFound);.       
21acb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21acc 64 4f 70 34 28 76 2c 20 4f 50 5f 42 6c 6f 62 2c  dOp4(v, OP_Blob,
21acd 20 32 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c   2, rMayHaveNull
21ace 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20  , 0, .          
21acf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21ad0 20 20 20 6e 75 6c 6c 52 65 63 6f 72 64 2c 20 50     nullRecord, P
21ad1 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  4_STATIC);.     
21ad2 20 20 20 20 20 6a 34 20 3d 20 73 71 6c 69 74 65       j4 = sqlite
21ad3 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
21ad4 50 5f 46 6f 75 6e 64 2c 20 70 45 78 70 72 2d 3e  P_Found, pExpr->
21ad5 69 54 61 62 6c 65 2c 20 30 2c 20 72 4d 61 79 48  iTable, 0, rMayH
21ad6 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  aveNull);.      
21ad7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21ad8 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
21ad9 67 65 72 2c 20 30 2c 20 72 4e 6f 74 46 6f 75 6e  ger, 0, rNotFoun
21ada 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  d);.          sq
21adb 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
21adc 65 28 76 2c 20 6a 34 29 3b 0a 20 20 20 20 20 20  e(v, j4);.      
21add 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
21ade 75 6d 70 48 65 72 65 28 76 2c 20 6a 33 29 3b 0a  umpHere(v, j3);.
21adf 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  .          /* Co
21ae0 70 79 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  py the value of 
21ae1 72 65 67 69 73 74 65 72 20 72 4e 6f 74 46 6f 75  register rNotFou
21ae2 6e 64 20 28 77 68 69 63 68 20 69 73 20 65 69 74  nd (which is eit
21ae3 68 65 72 20 4e 55 4c 4c 20 6f 72 20 30 29 0a 20  her NULL or 0). 
21ae4 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f           ** into
21ae5 20 74 68 65 20 74 61 72 67 65 74 20 72 65 67 69   the target regi
21ae6 73 74 65 72 2e 20 54 68 69 73 20 77 69 6c 6c 20  ster. This will 
21ae7 62 65 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66  be the result of
21ae8 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a   the.          *
21ae9 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 20 20  * expression..  
21aea 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
21aeb 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21aec 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70  AddOp2(v, OP_Cop
21aed 79 2c 20 72 4e 6f 74 46 6f 75 6e 64 2c 20 74 61  y, rNotFound, ta
21aee 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  rget);.        }
21aef 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
21af0 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
21af1 72 65 28 76 2c 20 6a 32 29 3b 0a 20 20 20 20 20  re(v, j2);.     
21af2 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
21af3 48 65 72 65 28 76 2c 20 6a 35 29 3b 0a 20 20 20  Here(v, j5);.   
21af4 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
21af5 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31  chePop(pParse, 1
21af6 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
21af7 6d 65 6e 74 28 28 76 2c 20 22 65 6e 64 20 49 4e  ment((v, "end IN
21af8 20 65 78 70 72 20 72 25 64 22 2c 20 74 61 72 67   expr r%d", targ
21af9 65 74 29 29 3b 0a 20 20 20 20 20 20 62 72 65 61  et));.      brea
21afa 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  k;.    }.#endif.
21afb 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20 20      /*.    **   
21afc 20 78 20 42 45 54 57 45 45 4e 20 79 20 41 4e 44   x BETWEEN y AND
21afd 20 7a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a   z.    **.    **
21afe 20 54 68 69 73 20 69 73 20 65 71 75 69 76 61 6c   This is equival
21aff 65 6e 74 20 74 6f 0a 20 20 20 20 2a 2a 0a 20 20  ent to.    **.  
21b00 20 20 2a 2a 20 20 20 20 78 3e 3d 79 20 41 4e 44    **    x>=y AND
21b01 20 78 3c 3d 7a 0a 20 20 20 20 2a 2a 0a 20 20 20   x<=z.    **.   
21b02 20 2a 2a 20 58 20 69 73 20 73 74 6f 72 65 64 20   ** X is stored 
21b03 69 6e 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2e  in pExpr->pLeft.
21b04 0a 20 20 20 20 2a 2a 20 59 20 69 73 20 73 74 6f  .    ** Y is sto
21b05 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c  red in pExpr->pL
21b06 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2e  ist->a[0].pExpr.
21b07 0a 20 20 20 20 2a 2a 20 5a 20 69 73 20 73 74 6f  .    ** Z is sto
21b08 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c  red in pExpr->pL
21b09 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 2e  ist->a[1].pExpr.
21b0a 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
21b0b 20 54 4b 5f 42 45 54 57 45 45 4e 3a 20 7b 0a 20   TK_BETWEEN: {. 
21b0c 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74       Expr *pLeft
21b0d 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b   = pExpr->pLeft;
21b0e 0a 20 20 20 20 20 20 73 74 72 75 63 74 20 45 78  .      struct Ex
21b0f 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c 49  prList_item *pLI
21b10 74 65 6d 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70  tem = pExpr->x.p
21b11 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 45  List->a;.      E
21b12 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20 70 4c  xpr *pRight = pL
21b13 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 0a 20 20  Item->pExpr;..  
21b14 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f      codeCompareO
21b15 70 65 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20  perands(pParse, 
21b16 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65 67  pLeft, &r1, &reg
21b17 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20 20  Free1,.         
21b18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21b19 20 20 20 20 20 20 20 20 20 70 52 69 67 68 74 2c           pRight,
21b1a 20 26 72 32 2c 20 26 72 65 67 46 72 65 65 32 29   &r2, &regFree2)
21b1b 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21b1c 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b  ( regFree1==0 );
21b1d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21b1e 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a   regFree2==0 );.
21b1f 20 20 20 20 20 20 72 33 20 3d 20 73 71 6c 69 74        r3 = sqlit
21b20 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
21b21 72 73 65 29 3b 0a 20 20 20 20 20 20 72 34 20 3d  rse);.      r4 =
21b22 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
21b23 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
21b24 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28 70 50    codeCompare(pP
21b25 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69  arse, pLeft, pRi
21b26 67 68 74 2c 20 4f 50 5f 47 65 2c 0a 20 20 20 20  ght, OP_Ge,.    
21b27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 31                r1
21b28 2c 20 72 32 2c 20 72 33 2c 20 53 51 4c 49 54 45  , r2, r3, SQLITE
21b29 5f 53 54 4f 52 45 50 32 29 3b 0a 20 20 20 20 20  _STOREP2);.     
21b2a 20 70 4c 49 74 65 6d 2b 2b 3b 0a 20 20 20 20 20   pLItem++;.     
21b2b 20 70 52 69 67 68 74 20 3d 20 70 4c 49 74 65 6d   pRight = pLItem
21b2c 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 73  ->pExpr;.      s
21b2d 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
21b2e 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
21b2f 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 72 32  Free2);.      r2
21b30 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
21b31 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70  deTemp(pParse, p
21b32 52 69 67 68 74 2c 20 26 72 65 67 46 72 65 65 32  Right, &regFree2
21b33 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
21b34 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29  e( regFree2==0 )
21b35 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70  ;.      codeComp
21b36 61 72 65 28 70 50 61 72 73 65 2c 20 70 4c 65 66  are(pParse, pLef
21b37 74 2c 20 70 52 69 67 68 74 2c 20 4f 50 5f 4c 65  t, pRight, OP_Le
21b38 2c 20 72 31 2c 20 72 32 2c 20 72 34 2c 20 53 51  , r1, r2, r4, SQ
21b39 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a 20  LITE_STOREP2);. 
21b3a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21b3b 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 6e 64  AddOp3(v, OP_And
21b3c 2c 20 72 33 2c 20 72 34 2c 20 74 61 72 67 65 74  , r3, r4, target
21b3d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
21b3e 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
21b3f 50 61 72 73 65 2c 20 72 33 29 3b 0a 20 20 20 20  Parse, r3);.    
21b40 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
21b41 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
21b42 72 34 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  r4);.      break
21b43 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
21b44 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20 20 20   TK_UPLUS: {.   
21b45 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74     inReg = sqlit
21b46 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74  e3ExprCodeTarget
21b47 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
21b48 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29 3b 0a  pLeft, target);.
21b49 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21b4a 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   }..    case TK_
21b4b 54 52 49 47 47 45 52 3a 20 7b 0a 20 20 20 20 20  TRIGGER: {.     
21b4c 20 2f 2a 20 49 66 20 74 68 65 20 6f 70 63 6f 64   /* If the opcod
21b4d 65 20 69 73 20 54 4b 5f 54 52 49 47 47 45 52 2c  e is TK_TRIGGER,
21b4e 20 74 68 65 6e 20 74 68 65 20 65 78 70 72 65 73   then the expres
21b4f 73 69 6f 6e 20 69 73 20 61 20 72 65 66 65 72 65  sion is a refere
21b50 6e 63 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  nce.      ** to 
21b51 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  a column in the 
21b52 6e 65 77 2e 2a 20 6f 72 20 6f 6c 64 2e 2a 20 70  new.* or old.* p
21b53 73 65 75 64 6f 2d 74 61 62 6c 65 73 20 61 76 61  seudo-tables ava
21b54 69 6c 61 62 6c 65 20 74 6f 0a 20 20 20 20 20 20  ilable to.      
21b55 2a 2a 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  ** trigger progr
21b56 61 6d 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73  ams. In this cas
21b57 65 20 45 78 70 72 2e 69 54 61 62 6c 65 20 69 73  e Expr.iTable is
21b58 20 73 65 74 20 74 6f 20 31 20 66 6f 72 20 74 68   set to 1 for th
21b59 65 0a 20 20 20 20 20 20 2a 2a 20 6e 65 77 2e 2a  e.      ** new.*
21b5a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20 6f   pseudo-table, o
21b5b 72 20 30 20 66 6f 72 20 74 68 65 20 6f 6c 64 2e  r 0 for the old.
21b5c 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2e 20  * pseudo-table. 
21b5d 45 78 70 72 2e 69 43 6f 6c 75 6d 6e 0a 20 20 20  Expr.iColumn.   
21b5e 20 20 20 2a 2a 20 69 73 20 73 65 74 20 74 6f 20     ** is set to 
21b5f 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  the column of th
21b60 65 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 74  e pseudo-table t
21b61 6f 20 72 65 61 64 2c 20 6f 72 20 74 6f 20 2d 31  o read, or to -1
21b62 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72 65 61   to.      ** rea
21b63 64 20 74 68 65 20 72 6f 77 69 64 20 66 69 65 6c  d the rowid fiel
21b64 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  d..      **.    
21b65 20 20 2a 2a 20 54 68 65 20 65 78 70 72 65 73 73    ** The express
21b66 69 6f 6e 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74  ion is implement
21b67 65 64 20 75 73 69 6e 67 20 61 6e 20 4f 50 5f 50  ed using an OP_P
21b68 61 72 61 6d 20 6f 70 63 6f 64 65 2e 20 54 68 65  aram opcode. The
21b69 20 70 31 0a 20 20 20 20 20 20 2a 2a 20 70 61 72   p1.      ** par
21b6a 61 6d 65 74 65 72 20 69 73 20 73 65 74 20 74 6f  ameter is set to
21b6b 20 30 20 66 6f 72 20 61 6e 20 6f 6c 64 2e 72 6f   0 for an old.ro
21b6c 77 69 64 20 72 65 66 65 72 65 6e 63 65 2c 20 6f  wid reference, o
21b6d 72 20 74 6f 20 28 69 2b 31 29 0a 20 20 20 20 20  r to (i+1).     
21b6e 20 2a 2a 20 74 6f 20 72 65 66 65 72 65 6e 63 65   ** to reference
21b6f 20 61 6e 6f 74 68 65 72 20 63 6f 6c 75 6d 6e 20   another column 
21b70 6f 66 20 74 68 65 20 6f 6c 64 2e 2a 20 70 73 65  of the old.* pse
21b71 75 64 6f 2d 74 61 62 6c 65 2c 20 77 68 65 72 65  udo-table, where
21b72 20 0a 20 20 20 20 20 20 2a 2a 20 69 20 69 73 20   .      ** i is 
21b73 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
21b74 20 63 6f 6c 75 6d 6e 2e 20 46 6f 72 20 61 20 6e   column. For a n
21b75 65 77 2e 72 6f 77 69 64 20 72 65 66 65 72 65 6e  ew.rowid referen
21b76 63 65 2c 20 70 31 20 69 73 0a 20 20 20 20 20 20  ce, p1 is.      
21b77 2a 2a 20 73 65 74 20 74 6f 20 28 6e 2b 31 29 2c  ** set to (n+1),
21b78 20 77 68 65 72 65 20 6e 20 69 73 20 74 68 65 20   where n is the 
21b79 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
21b7a 73 20 69 6e 20 65 61 63 68 20 70 73 65 75 64 6f  s in each pseudo
21b7b 2d 74 61 62 6c 65 2e 0a 20 20 20 20 20 20 2a 2a  -table..      **
21b7c 20 46 6f 72 20 61 20 72 65 66 65 72 65 6e 63 65   For a reference
21b7d 20 74 6f 20 61 6e 79 20 6f 74 68 65 72 20 63 6f   to any other co
21b7e 6c 75 6d 6e 20 69 6e 20 74 68 65 20 6e 65 77 2e  lumn in the new.
21b7f 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 2c 20  * pseudo-table, 
21b80 70 31 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 73  p1.      ** is s
21b81 65 74 20 74 6f 20 28 6e 2b 32 2b 69 29 2c 20 77  et to (n+2+i), w
21b82 68 65 72 65 20 6e 20 61 6e 64 20 69 20 61 72 65  here n and i are
21b83 20 61 73 20 64 65 66 69 6e 65 64 20 70 72 65 76   as defined prev
21b84 69 6f 75 73 6c 79 2e 20 46 6f 72 0a 20 20 20 20  iously. For.    
21b85 20 20 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 69 66    ** example, if
21b86 20 74 68 65 20 74 61 62 6c 65 20 6f 6e 20 77 68   the table on wh
21b87 69 63 68 20 74 72 69 67 67 65 72 73 20 61 72 65  ich triggers are
21b88 20 62 65 69 6e 67 20 66 69 72 65 64 20 69 73 0a   being fired is.
21b89 20 20 20 20 20 20 2a 2a 20 64 65 63 6c 61 72 65        ** declare
21b8a 64 20 61 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20  d as:.      **. 
21b8b 20 20 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45       **   CREATE
21b8c 20 54 41 42 4c 45 20 74 31 28 61 2c 20 62 29 3b   TABLE t1(a, b);
21b8d 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
21b8e 2a 2a 20 54 68 65 6e 20 70 31 20 69 73 20 69 6e  ** Then p1 is in
21b8f 74 65 72 70 72 65 74 65 64 20 61 73 20 66 6f 6c  terpreted as fol
21b90 6c 6f 77 73 3a 0a 20 20 20 20 20 20 2a 2a 0a 20  lows:.      **. 
21b91 20 20 20 20 20 2a 2a 20 20 20 70 31 3d 3d 30 20       **   p1==0 
21b92 20 20 2d 3e 20 20 20 20 6f 6c 64 2e 72 6f 77 69    ->    old.rowi
21b93 64 20 20 20 20 20 70 31 3d 3d 33 20 20 20 2d 3e  d     p1==3   ->
21b94 20 20 20 20 6e 65 77 2e 72 6f 77 69 64 0a 20 20      new.rowid.  
21b95 20 20 20 20 2a 2a 20 20 20 70 31 3d 3d 31 20 20      **   p1==1  
21b96 20 2d 3e 20 20 20 20 6f 6c 64 2e 61 20 20 20 20   ->    old.a    
21b97 20 20 20 20 20 70 31 3d 3d 34 20 20 20 2d 3e 20       p1==4   -> 
21b98 20 20 20 6e 65 77 2e 61 0a 20 20 20 20 20 20 2a     new.a.      *
21b99 2a 20 20 20 70 31 3d 3d 32 20 20 20 2d 3e 20 20  *   p1==2   ->  
21b9a 20 20 6f 6c 64 2e 62 20 20 20 20 20 20 20 20 20    old.b         
21b9b 70 31 3d 3d 35 20 20 20 2d 3e 20 20 20 20 6e 65  p1==5   ->    ne
21b9c 77 2e 62 20 20 20 20 20 20 20 0a 20 20 20 20 20  w.b       .     
21b9d 20 2a 2f 0a 20 20 20 20 20 20 54 61 62 6c 65 20   */.      Table 
21b9e 2a 70 54 61 62 20 3d 20 70 45 78 70 72 2d 3e 70  *pTab = pExpr->p
21b9f 54 61 62 3b 0a 20 20 20 20 20 20 69 6e 74 20 70  Tab;.      int p
21ba0 31 20 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  1 = pExpr->iTabl
21ba1 65 20 2a 20 28 70 54 61 62 2d 3e 6e 43 6f 6c 2b  e * (pTab->nCol+
21ba2 31 29 20 2b 20 31 20 2b 20 70 45 78 70 72 2d 3e  1) + 1 + pExpr->
21ba3 69 43 6f 6c 75 6d 6e 3b 0a 0a 20 20 20 20 20 20  iColumn;..      
21ba4 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 69  assert( pExpr->i
21ba5 54 61 62 6c 65 3d 3d 30 20 7c 7c 20 70 45 78 70  Table==0 || pExp
21ba6 72 2d 3e 69 54 61 62 6c 65 3d 3d 31 20 29 3b 0a  r->iTable==1 );.
21ba7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45        assert( pE
21ba8 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 2d 31  xpr->iColumn>=-1
21ba9 20 26 26 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75   && pExpr->iColu
21baa 6d 6e 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b  mn<pTab->nCol );
21bab 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
21bac 54 61 62 2d 3e 69 50 4b 65 79 3c 30 20 7c 7c 20  Tab->iPKey<0 || 
21bad 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 21 3d  pExpr->iColumn!=
21bae 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 3b 0a 20  pTab->iPKey );. 
21baf 20 20 20 20 20 61 73 73 65 72 74 28 20 70 31 3e       assert( p1>
21bb0 3d 30 20 26 26 20 70 31 3c 28 70 54 61 62 2d 3e  =0 && p1<(pTab->
21bb1 6e 43 6f 6c 2a 32 2b 32 29 20 29 3b 0a 0a 20 20  nCol*2+2) );..  
21bb2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21bb3 64 64 4f 70 32 28 76 2c 20 4f 50 5f 50 61 72 61  ddOp2(v, OP_Para
21bb4 6d 2c 20 70 31 2c 20 74 61 72 67 65 74 29 3b 0a  m, p1, target);.
21bb5 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e        VdbeCommen
21bb6 74 28 28 76 2c 20 22 25 73 2e 25 73 20 2d 3e 20  t((v, "%s.%s -> 
21bb7 24 25 64 22 2c 0a 20 20 20 20 20 20 20 20 28 70  $%d",.        (p
21bb8 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3f 20 22  Expr->iTable ? "
21bb9 6e 65 77 22 20 3a 20 22 6f 6c 64 22 29 2c 0a 20  new" : "old"),. 
21bba 20 20 20 20 20 20 20 28 70 45 78 70 72 2d 3e 69         (pExpr->i
21bbb 43 6f 6c 75 6d 6e 3c 30 20 3f 20 22 72 6f 77 69  Column<0 ? "rowi
21bbc 64 22 20 3a 20 70 45 78 70 72 2d 3e 70 54 61 62  d" : pExpr->pTab
21bbd 2d 3e 61 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 43  ->aCol[pExpr->iC
21bbe 6f 6c 75 6d 6e 5d 2e 7a 4e 61 6d 65 29 2c 0a 20  olumn].zName),. 
21bbf 20 20 20 20 20 20 20 74 61 72 67 65 74 0a 20 20         target.  
21bc0 20 20 20 20 29 29 3b 0a 0a 20 20 20 20 20 20 2f      ));..      /
21bc1 2a 20 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20  * If the column 
21bc2 68 61 73 20 52 45 41 4c 20 61 66 66 69 6e 69 74  has REAL affinit
21bc3 79 2c 20 69 74 20 6d 61 79 20 63 75 72 72 65 6e  y, it may curren
21bc4 74 6c 79 20 62 65 20 73 74 6f 72 65 64 20 61 73  tly be stored as
21bc5 20 61 6e 0a 20 20 20 20 20 20 2a 2a 20 69 6e 74   an.      ** int
21bc6 65 67 65 72 2e 20 55 73 65 20 4f 50 5f 52 65 61  eger. Use OP_Rea
21bc7 6c 41 66 66 69 6e 69 74 79 20 74 6f 20 6d 61 6b  lAffinity to mak
21bc8 65 20 73 75 72 65 20 69 74 20 69 73 20 72 65 61  e sure it is rea
21bc9 6c 6c 79 20 72 65 61 6c 2e 20 20 2a 2f 0a 20 20  lly real.  */.  
21bca 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69      if( pExpr->i
21bcb 43 6f 6c 75 6d 6e 3e 3d 30 20 0a 20 20 20 20 20  Column>=0 .     
21bcc 20 20 26 26 20 70 54 61 62 2d 3e 61 43 6f 6c 5b    && pTab->aCol[
21bcd 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 5d 2e  pExpr->iColumn].
21bce 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45  affinity==SQLITE
21bcf 5f 41 46 46 5f 52 45 41 4c 0a 20 20 20 20 20 20  _AFF_REAL.      
21bd0 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
21bd1 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
21bd2 4f 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 2c  OP_RealAffinity,
21bd3 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20   target);.      
21bd4 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
21bd5 20 20 20 7d 0a 0a 0a 20 20 20 20 2f 2a 0a 20 20     }...    /*.  
21bd6 20 20 2a 2a 20 46 6f 72 6d 20 41 3a 0a 20 20 20    ** Form A:.   
21bd7 20 2a 2a 20 20 20 43 41 53 45 20 78 20 57 48 45   **   CASE x WHE
21bd8 4e 20 65 31 20 54 48 45 4e 20 72 31 20 57 48 45  N e1 THEN r1 WHE
21bd9 4e 20 65 32 20 54 48 45 4e 20 72 32 20 2e 2e 2e  N e2 THEN r2 ...
21bda 20 57 48 45 4e 20 65 4e 20 54 48 45 4e 20 72 4e   WHEN eN THEN rN
21bdb 20 45 4c 53 45 20 79 20 45 4e 44 0a 20 20 20 20   ELSE y END.    
21bdc 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 42  **.    ** Form B
21bdd 3a 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20  :.    **   CASE 
21bde 57 48 45 4e 20 65 31 20 54 48 45 4e 20 72 31 20  WHEN e1 THEN r1 
21bdf 57 48 45 4e 20 65 32 20 54 48 45 4e 20 72 32 20  WHEN e2 THEN r2 
21be0 2e 2e 2e 20 57 48 45 4e 20 65 4e 20 54 48 45 4e  ... WHEN eN THEN
21be1 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a 20   rN ELSE y END. 
21be2 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72     **.    ** For
21be3 6d 20 41 20 69 73 20 63 61 6e 20 62 65 20 74 72  m A is can be tr
21be4 61 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f 20 74  ansformed into t
21be5 68 65 20 65 71 75 69 76 61 6c 65 6e 74 20 66 6f  he equivalent fo
21be6 72 6d 20 42 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  rm B as follows:
21be7 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 57  .    **   CASE W
21be8 48 45 4e 20 78 3d 65 31 20 54 48 45 4e 20 72 31  HEN x=e1 THEN r1
21be9 20 57 48 45 4e 20 78 3d 65 32 20 54 48 45 4e 20   WHEN x=e2 THEN 
21bea 72 32 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20  r2 ....    **   
21beb 20 20 20 20 20 57 48 45 4e 20 78 3d 65 4e 20 54       WHEN x=eN T
21bec 48 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e  HEN rN ELSE y EN
21bed 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  D.    **.    ** 
21bee 58 20 28 69 66 20 69 74 20 65 78 69 73 74 73 29  X (if it exists)
21bef 20 69 73 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c   is in pExpr->pL
21bf0 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69 73  eft..    ** Y is
21bf1 20 69 6e 20 70 45 78 70 72 2d 3e 70 52 69 67 68   in pExpr->pRigh
21bf2 74 2e 20 20 54 68 65 20 59 20 69 73 20 61 6c 73  t.  The Y is als
21bf3 6f 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66 20  o optional.  If 
21bf4 74 68 65 72 65 20 69 73 20 6e 6f 0a 20 20 20 20  there is no.    
21bf5 2a 2a 20 45 4c 53 45 20 63 6c 61 75 73 65 20 61  ** ELSE clause a
21bf6 6e 64 20 6e 6f 20 6f 74 68 65 72 20 74 65 72 6d  nd no other term
21bf7 20 6d 61 74 63 68 65 73 2c 20 74 68 65 6e 20 74   matches, then t
21bf8 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65  he result of the
21bf9 0a 20 20 20 20 2a 2a 20 65 78 70 72 73 73 69 6f  .    ** exprssio
21bfa 6e 20 69 73 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a  n is NULL..    *
21bfb 2a 20 45 69 20 69 73 20 69 6e 20 70 45 78 70 72  * Ei is in pExpr
21bfc 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 5d 20  ->pList->a[i*2] 
21bfd 61 6e 64 20 52 69 20 69 73 20 70 45 78 70 72 2d  and Ri is pExpr-
21bfe 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 2b 31 5d  >pList->a[i*2+1]
21bff 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
21c00 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68  The result of th
21c01 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
21c02 74 68 65 20 52 69 20 66 6f 72 20 74 68 65 20 66  the Ri for the f
21c03 69 72 73 74 20 6d 61 74 63 68 69 6e 67 20 45 69  irst matching Ei
21c04 2c 0a 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 74  ,.    ** or if t
21c05 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68  here is no match
21c06 69 6e 67 20 45 69 2c 20 74 68 65 20 45 4c 53 45  ing Ei, the ELSE
21c07 20 74 65 72 6d 20 59 2c 20 6f 72 20 69 66 20 74   term Y, or if t
21c08 68 65 72 65 20 69 73 0a 20 20 20 20 2a 2a 20 6e  here is.    ** n
21c09 6f 20 45 4c 53 45 20 74 65 72 6d 2c 20 4e 55 4c  o ELSE term, NUL
21c0a 4c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 64 65  L..    */.    de
21c0b 66 61 75 6c 74 3a 20 61 73 73 65 72 74 28 20 6f  fault: assert( o
21c0c 70 3d 3d 54 4b 5f 43 41 53 45 20 29 3b 20 7b 0a  p==TK_CASE ); {.
21c0d 20 20 20 20 20 20 69 6e 74 20 65 6e 64 4c 61 62        int endLab
21c0e 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  el;             
21c0f 20 20 20 20 20 20 20 20 2f 2a 20 47 4f 54 4f 20          /* GOTO 
21c10 6c 61 62 65 6c 20 66 6f 72 20 65 6e 64 20 6f 66  label for end of
21c11 20 43 41 53 45 20 73 74 6d 74 20 2a 2f 0a 20 20   CASE stmt */.  
21c12 20 20 20 20 69 6e 74 20 6e 65 78 74 43 61 73 65      int nextCase
21c13 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
21c14 20 20 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61        /* GOTO la
21c15 62 65 6c 20 66 6f 72 20 6e 65 78 74 20 57 48 45  bel for next WHE
21c16 4e 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20  N clause */.    
21c17 20 20 69 6e 74 20 6e 45 78 70 72 3b 20 20 20 20    int nExpr;    
21c18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c19 20 20 20 20 2f 2a 20 32 78 20 6e 75 6d 62 65 72      /* 2x number
21c1a 20 6f 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a   of WHEN terms *
21c1b 2f 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20  /.      int i;  
21c1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21c1d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
21c1e 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20  p counter */.   
21c1f 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c     ExprList *pEL
21c20 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ist;            
21c21 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
21c22 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20  WHEN terms */.  
21c23 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
21c24 69 73 74 5f 69 74 65 6d 20 2a 61 4c 69 73 74 65  ist_item *aListe
21c25 6c 65 6d 3b 20 20 2f 2a 20 41 72 72 61 79 20 6f  lem;  /* Array o
21c26 66 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a  f WHEN terms */.
21c27 20 20 20 20 20 20 45 78 70 72 20 6f 70 43 6f 6d        Expr opCom
21c28 70 61 72 65 3b 20 20 20 20 20 20 20 20 20 20 20  pare;           
21c29 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 58          /* The X
21c2a 3d 3d 45 69 20 65 78 70 72 65 73 73 69 6f 6e 20  ==Ei expression 
21c2b 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 63 61  */.      Expr ca
21c2c 63 68 65 58 3b 20 20 20 20 20 20 20 20 20 20 20  cheX;           
21c2d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61             /* Ca
21c2e 63 68 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20  ched expression 
21c2f 58 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20  X */.      Expr 
21c30 2a 70 58 3b 20 20 20 20 20 20 20 20 20 20 20 20  *pX;            
21c31 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
21c32 54 68 65 20 58 20 65 78 70 72 65 73 73 69 6f 6e  The X expression
21c33 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a   */.      Expr *
21c34 70 54 65 73 74 20 3d 20 30 3b 20 20 20 20 20 20  pTest = 0;      
21c35 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 58              /* X
21c36 3d 3d 45 69 20 28 66 6f 72 6d 20 41 29 20 6f 72  ==Ei (form A) or
21c37 20 6a 75 73 74 20 45 69 20 28 66 6f 72 6d 20 42   just Ei (form B
21c38 29 20 2a 2f 0a 20 20 20 20 20 20 56 56 41 5f 4f  ) */.      VVA_O
21c39 4e 4c 59 28 20 69 6e 74 20 69 43 61 63 68 65 4c  NLY( int iCacheL
21c3a 65 76 65 6c 20 3d 20 70 50 61 72 73 65 2d 3e 69  evel = pParse->i
21c3b 43 61 63 68 65 4c 65 76 65 6c 3b 20 29 0a 0a 20  CacheLevel; ).. 
21c3c 20 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78       assert( !Ex
21c3d 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
21c3e 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
21c3f 74 29 20 26 26 20 70 45 78 70 72 2d 3e 78 2e 70  t) && pExpr->x.p
21c40 4c 69 73 74 20 29 3b 0a 20 20 20 20 20 20 61 73  List );.      as
21c41 73 65 72 74 28 28 70 45 78 70 72 2d 3e 78 2e 70  sert((pExpr->x.p
21c42 4c 69 73 74 2d 3e 6e 45 78 70 72 20 25 20 32 29  List->nExpr % 2)
21c43 20 3d 3d 20 30 29 3b 0a 20 20 20 20 20 20 61 73   == 0);.      as
21c44 73 65 72 74 28 70 45 78 70 72 2d 3e 78 2e 70 4c  sert(pExpr->x.pL
21c45 69 73 74 2d 3e 6e 45 78 70 72 20 3e 20 30 29 3b  ist->nExpr > 0);
21c46 0a 20 20 20 20 20 20 70 45 4c 69 73 74 20 3d 20  .      pEList = 
21c47 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a  pExpr->x.pList;.
21c48 20 20 20 20 20 20 61 4c 69 73 74 65 6c 65 6d 20        aListelem 
21c49 3d 20 70 45 4c 69 73 74 2d 3e 61 3b 0a 20 20 20  = pEList->a;.   
21c4a 20 20 20 6e 45 78 70 72 20 3d 20 70 45 4c 69 73     nExpr = pELis
21c4b 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20  t->nExpr;.      
21c4c 65 6e 64 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74  endLabel = sqlit
21c4d 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
21c4e 76 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70  v);.      if( (p
21c4f 58 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  X = pExpr->pLeft
21c50 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
21c51 63 61 63 68 65 58 20 3d 20 2a 70 58 3b 0a 20 20  cacheX = *pX;.  
21c52 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21c53 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d  pX->op==TK_COLUM
21c54 4e 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73  N );.        tes
21c55 74 63 61 73 65 28 20 70 58 2d 3e 6f 70 3d 3d 54  tcase( pX->op==T
21c56 4b 5f 52 45 47 49 53 54 45 52 20 29 3b 0a 20 20  K_REGISTER );.  
21c57 20 20 20 20 20 20 63 61 63 68 65 58 2e 69 54 61        cacheX.iTa
21c58 62 6c 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ble = sqlite3Exp
21c59 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65  rCodeTemp(pParse
21c5a 2c 20 70 58 2c 20 26 72 65 67 46 72 65 65 31 29  , pX, &regFree1)
21c5b 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61  ;.        testca
21c5c 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20  se( regFree1==0 
21c5d 29 3b 0a 20 20 20 20 20 20 20 20 63 61 63 68 65  );.        cache
21c5e 58 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 54  X.op = TK_REGIST
21c5f 45 52 3b 0a 20 20 20 20 20 20 20 20 6f 70 43 6f  ER;.        opCo
21c60 6d 70 61 72 65 2e 6f 70 20 3d 20 54 4b 5f 45 51  mpare.op = TK_EQ
21c61 3b 0a 20 20 20 20 20 20 20 20 6f 70 43 6f 6d 70  ;.        opComp
21c62 61 72 65 2e 70 4c 65 66 74 20 3d 20 26 63 61 63  are.pLeft = &cac
21c63 68 65 58 3b 0a 20 20 20 20 20 20 20 20 70 54 65  heX;.        pTe
21c64 73 74 20 3d 20 26 6f 70 43 6f 6d 70 61 72 65 3b  st = &opCompare;
21c65 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66  .      }.      f
21c66 6f 72 28 69 3d 30 3b 20 69 3c 6e 45 78 70 72 3b  or(i=0; i<nExpr;
21c67 20 69 3d 69 2b 32 29 7b 0a 20 20 20 20 20 20 20   i=i+2){.       
21c68 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
21c69 65 50 75 73 68 28 70 50 61 72 73 65 29 3b 0a 20  ePush(pParse);. 
21c6a 20 20 20 20 20 20 20 69 66 28 20 70 58 20 29 7b         if( pX ){
21c6b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
21c6c 74 28 20 70 54 65 73 74 21 3d 30 20 29 3b 0a 20  t( pTest!=0 );. 
21c6d 20 20 20 20 20 20 20 20 20 6f 70 43 6f 6d 70 61           opCompa
21c6e 72 65 2e 70 52 69 67 68 74 20 3d 20 61 4c 69 73  re.pRight = aLis
21c6f 74 65 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b 0a  telem[i].pExpr;.
21c70 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
21c71 20 20 20 20 20 20 20 20 20 70 54 65 73 74 20 3d           pTest =
21c72 20 61 4c 69 73 74 65 6c 65 6d 5b 69 5d 2e 70 45   aListelem[i].pE
21c73 78 70 72 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  xpr;.        }. 
21c74 20 20 20 20 20 20 20 6e 65 78 74 43 61 73 65 20         nextCase 
21c75 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
21c76 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
21c77 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65     testcase( pTe
21c78 73 74 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d  st->op==TK_COLUM
21c79 4e 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  N );.        sql
21c7a 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
21c7b 70 50 61 72 73 65 2c 20 70 54 65 73 74 2c 20 6e  pParse, pTest, n
21c7c 65 78 74 43 61 73 65 2c 20 53 51 4c 49 54 45 5f  extCase, SQLITE_
21c7d 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20  JUMPIFNULL);.   
21c7e 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 61       testcase( a
21c7f 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45  Listelem[i+1].pE
21c80 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55  xpr->op==TK_COLU
21c81 4d 4e 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65  MN );.        te
21c82 73 74 63 61 73 65 28 20 61 4c 69 73 74 65 6c 65  stcase( aListele
21c83 6d 5b 69 2b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70  m[i+1].pExpr->op
21c84 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 20 29 3b  ==TK_REGISTER );
21c85 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21c86 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
21c87 20 61 4c 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e   aListelem[i+1].
21c88 70 45 78 70 72 2c 20 74 61 72 67 65 74 29 3b 0a  pExpr, target);.
21c89 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
21c8a 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
21c8b 47 6f 74 6f 2c 20 30 2c 20 65 6e 64 4c 61 62 65  Goto, 0, endLabe
21c8c 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  l);.        sqli
21c8d 74 65 33 45 78 70 72 43 61 63 68 65 50 6f 70 28  te3ExprCachePop(
21c8e 70 50 61 72 73 65 2c 20 31 29 3b 0a 20 20 20 20  pParse, 1);.    
21c8f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
21c90 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 6e  esolveLabel(v, n
21c91 65 78 74 43 61 73 65 29 3b 0a 20 20 20 20 20 20  extCase);.      
21c92 7d 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70  }.      if( pExp
21c93 72 2d 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 20  r->pRight ){.   
21c94 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
21c95 43 61 63 68 65 50 75 73 68 28 70 50 61 72 73 65  CachePush(pParse
21c96 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
21c97 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
21c98 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  e, pExpr->pRight
21c99 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
21c9a 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61     sqlite3ExprCa
21c9b 63 68 65 50 6f 70 28 70 50 61 72 73 65 2c 20 31  chePop(pParse, 1
21c9c 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
21c9d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
21c9e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
21c9f 4e 75 6c 6c 2c 20 30 2c 20 74 61 72 67 65 74 29  Null, 0, target)
21ca0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
21ca1 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61 6c 6c  assert( db->mall
21ca2 6f 63 46 61 69 6c 65 64 20 7c 7c 20 70 50 61 72  ocFailed || pPar
21ca3 73 65 2d 3e 6e 45 72 72 3e 30 20 0a 20 20 20 20  se->nErr>0 .    
21ca4 20 20 20 20 20 20 20 7c 7c 20 70 50 61 72 73 65         || pParse
21ca5 2d 3e 69 43 61 63 68 65 4c 65 76 65 6c 3d 3d 69  ->iCacheLevel==i
21ca6 43 61 63 68 65 4c 65 76 65 6c 20 29 3b 0a 20 20  CacheLevel );.  
21ca7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
21ca8 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65  esolveLabel(v, e
21ca9 6e 64 4c 61 62 65 6c 29 3b 0a 20 20 20 20 20 20  ndLabel);.      
21caa 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66  break;.    }.#if
21cab 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
21cac 5f 54 52 49 47 47 45 52 0a 20 20 20 20 63 61 73  _TRIGGER.    cas
21cad 65 20 54 4b 5f 52 41 49 53 45 3a 20 7b 0a 20 20  e TK_RAISE: {.  
21cae 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
21caf 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f 45 5f  r->affinity==OE_
21cb0 52 6f 6c 6c 62 61 63 6b 20 0a 20 20 20 20 20 20  Rollback .      
21cb1 20 20 20 20 20 7c 7c 20 70 45 78 70 72 2d 3e 61       || pExpr->a
21cb2 66 66 69 6e 69 74 79 3d 3d 4f 45 5f 41 62 6f 72  ffinity==OE_Abor
21cb3 74 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  t.           || 
21cb4 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d  pExpr->affinity=
21cb5 3d 4f 45 5f 46 61 69 6c 0a 20 20 20 20 20 20 20  =OE_Fail.       
21cb6 20 20 20 20 7c 7c 20 70 45 78 70 72 2d 3e 61 66      || pExpr->af
21cb7 66 69 6e 69 74 79 3d 3d 4f 45 5f 49 67 6e 6f 72  finity==OE_Ignor
21cb8 65 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20  e.      );.     
21cb9 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e 70 54   if( !pParse->pT
21cba 72 69 67 67 65 72 54 61 62 20 29 7b 0a 20 20 20  riggerTab ){.   
21cbb 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
21cbc 72 4d 73 67 28 70 50 61 72 73 65 2c 0a 20 20 20  rMsg(pParse,.   
21cbd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21cbe 20 20 20 20 22 52 41 49 53 45 28 29 20 6d 61 79      "RAISE() may
21cbf 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 77 69   only be used wi
21cc0 74 68 69 6e 20 61 20 74 72 69 67 67 65 72 2d 70  thin a trigger-p
21cc1 72 6f 67 72 61 6d 22 29 3b 0a 20 20 20 20 20 20  rogram");.      
21cc2 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
21cc3 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 45    }.      if( pE
21cc4 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 3d 3d 4f  xpr->affinity==O
21cc5 45 5f 41 62 6f 72 74 20 29 7b 0a 20 20 20 20 20  E_Abort ){.     
21cc6 20 20 20 73 71 6c 69 74 65 33 4d 61 79 41 62 6f     sqlite3MayAbo
21cc7 72 74 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  rt(pParse);.    
21cc8 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65 72 74    }.      assert
21cc9 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72  ( !ExprHasProper
21cca 74 79 28 70 45 78 70 72 2c 20 45 50 5f 49 6e 74  ty(pExpr, EP_Int
21ccb 56 61 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20  Value) );.      
21ccc 69 66 28 20 70 45 78 70 72 2d 3e 61 66 66 69 6e  if( pExpr->affin
21ccd 69 74 79 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20 29  ity==OE_Ignore )
21cce 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
21ccf 33 56 64 62 65 41 64 64 4f 70 34 28 0a 20 20 20  3VdbeAddOp4(.   
21cd0 20 20 20 20 20 20 20 20 20 76 2c 20 4f 50 5f 48           v, OP_H
21cd1 61 6c 74 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20  alt, SQLITE_OK, 
21cd2 4f 45 5f 49 67 6e 6f 72 65 2c 20 30 2c 20 70 45  OE_Ignore, 0, pE
21cd3 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 30 29  xpr->u.zToken,0)
21cd4 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
21cd5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 48 61         sqlite3Ha
21cd6 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61  ltConstraint(pPa
21cd7 72 73 65 2c 20 70 45 78 70 72 2d 3e 61 66 66 69  rse, pExpr->affi
21cd8 6e 69 74 79 2c 20 70 45 78 70 72 2d 3e 75 2e 7a  nity, pExpr->u.z
21cd9 54 6f 6b 65 6e 2c 20 30 29 3b 0a 20 20 20 20 20  Token, 0);.     
21cda 20 7d 0a 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   }..      break;
21cdb 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
21cdc 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61  }.  sqlite3Relea
21cdd 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
21cde 2c 20 72 65 67 46 72 65 65 31 29 3b 0a 20 20 73  , regFree1);.  s
21cdf 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
21ce0 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
21ce1 46 72 65 65 32 29 3b 0a 20 20 72 65 74 75 72 6e  Free2);.  return
21ce2 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   inReg;.}../*.**
21ce3 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
21ce4 6f 20 65 76 61 6c 75 61 74 65 20 61 6e 20 65 78  o evaluate an ex
21ce5 70 72 65 73 73 69 6f 6e 20 61 6e 64 20 73 74 6f  pression and sto
21ce6 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a  re the results.*
21ce7 2a 20 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65  * into a registe
21ce8 72 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 72  r.  Return the r
21ce9 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 77  egister number w
21cea 68 65 72 65 20 74 68 65 20 72 65 73 75 6c 74 73  here the results
21ceb 0a 2a 2a 20 61 72 65 20 73 74 6f 72 65 64 2e 0a  .** are stored..
21cec 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 65 67  **.** If the reg
21ced 69 73 74 65 72 20 69 73 20 61 20 74 65 6d 70 6f  ister is a tempo
21cee 72 61 72 79 20 72 65 67 69 73 74 65 72 20 74 68  rary register th
21cef 61 74 20 63 61 6e 20 62 65 20 64 65 61 6c 6c 6f  at can be deallo
21cf0 63 61 74 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 77  cated,.** then w
21cf1 72 69 74 65 20 69 74 73 20 6e 75 6d 62 65 72 20  rite its number 
21cf2 69 6e 74 6f 20 2a 70 52 65 67 2e 20 20 49 66 20  into *pReg.  If 
21cf3 74 68 65 20 72 65 73 75 6c 74 20 72 65 67 69 73  the result regis
21cf4 74 65 72 20 69 73 20 6e 6f 74 0a 2a 2a 20 61 20  ter is not.** a 
21cf5 74 65 6d 70 6f 72 61 72 79 2c 20 74 68 65 6e 20  temporary, then 
21cf6 73 65 74 20 2a 70 52 65 67 20 74 6f 20 7a 65 72  set *pReg to zer
21cf7 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  o..*/.SQLITE_PRI
21cf8 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
21cf9 45 78 70 72 43 6f 64 65 54 65 6d 70 28 50 61 72  ExprCodeTemp(Par
21cfa 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
21cfb 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 2a 70 52   *pExpr, int *pR
21cfc 65 67 29 7b 0a 20 20 69 6e 74 20 72 31 20 3d 20  eg){.  int r1 = 
21cfd 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
21cfe 67 28 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74  g(pParse);.  int
21cff 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70   r2 = sqlite3Exp
21d00 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72  rCodeTarget(pPar
21d01 73 65 2c 20 70 45 78 70 72 2c 20 72 31 29 3b 0a  se, pExpr, r1);.
21d02 20 20 69 66 28 20 72 32 3d 3d 72 31 20 29 7b 0a    if( r2==r1 ){.
21d03 20 20 20 20 2a 70 52 65 67 20 3d 20 72 31 3b 0a      *pReg = r1;.
21d04 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
21d05 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
21d06 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a  eg(pParse, r1);.
21d07 20 20 20 20 2a 70 52 65 67 20 3d 20 30 3b 0a 20      *pReg = 0;. 
21d08 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 32 3b 0a   }.  return r2;.
21d09 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
21d0a 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c  e code that will
21d0b 20 65 76 61 6c 75 61 74 65 20 65 78 70 72 65 73   evaluate expres
21d0c 73 69 6f 6e 20 70 45 78 70 72 20 61 6e 64 20 73  sion pExpr and s
21d0d 74 6f 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 75  tore the.** resu
21d0e 6c 74 73 20 69 6e 20 72 65 67 69 73 74 65 72 20  lts in register 
21d0f 74 61 72 67 65 74 2e 20 20 54 68 65 20 72 65 73  target.  The res
21d10 75 6c 74 73 20 61 72 65 20 67 75 61 72 61 6e 74  ults are guarant
21d11 65 65 64 20 74 6f 20 61 70 70 65 61 72 0a 2a 2a  eed to appear.**
21d12 20 69 6e 20 72 65 67 69 73 74 65 72 20 74 61 72   in register tar
21d13 67 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  get..*/.SQLITE_P
21d14 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
21d15 65 33 45 78 70 72 43 6f 64 65 28 50 61 72 73 65  e3ExprCode(Parse
21d16 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
21d17 70 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65  pExpr, int targe
21d18 74 29 7b 0a 20 20 69 6e 74 20 69 6e 52 65 67 3b  t){.  int inReg;
21d19 0a 0a 20 20 61 73 73 65 72 74 28 20 74 61 72 67  ..  assert( targ
21d1a 65 74 3e 30 20 26 26 20 74 61 72 67 65 74 3c 3d  et>0 && target<=
21d1b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 29 3b 0a  pParse->nMem );.
21d1c 20 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65    inReg = sqlite
21d1d 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28  3ExprCodeTarget(
21d1e 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74  pParse, pExpr, t
21d1f 61 72 67 65 74 29 3b 0a 20 20 61 73 73 65 72 74  arget);.  assert
21d20 28 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20  ( pParse->pVdbe 
21d21 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d  || pParse->db->m
21d22 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
21d23 20 69 66 28 20 69 6e 52 65 67 21 3d 74 61 72 67   if( inReg!=targ
21d24 65 74 20 26 26 20 70 50 61 72 73 65 2d 3e 70 56  et && pParse->pV
21d25 64 62 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  dbe ){.    sqlit
21d26 65 33 56 64 62 65 41 64 64 4f 70 32 28 70 50 61  e3VdbeAddOp2(pPa
21d27 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 53  rse->pVdbe, OP_S
21d28 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74 61 72  Copy, inReg, tar
21d29 67 65 74 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  get);.  }.  retu
21d2a 72 6e 20 74 61 72 67 65 74 3b 0a 7d 0a 0a 2f 2a  rn target;.}../*
21d2b 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
21d2c 65 20 74 68 61 74 20 65 76 61 6c 75 74 65 73 20  e that evalutes 
21d2d 74 68 65 20 67 69 76 65 6e 20 65 78 70 72 65 73  the given expres
21d2e 73 69 6f 6e 20 61 6e 64 20 70 75 74 73 20 74 68  sion and puts th
21d2f 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 20 72  e result.** in r
21d30 65 67 69 73 74 65 72 20 74 61 72 67 65 74 2e 0a  egister target..
21d31 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 6d 61 6b 65 20  **.** Also make 
21d32 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 65 78  a copy of the ex
21d33 70 72 65 73 73 69 6f 6e 20 72 65 73 75 6c 74 73  pression results
21d34 20 69 6e 74 6f 20 61 6e 6f 74 68 65 72 20 22 63   into another "c
21d35 61 63 68 65 22 20 72 65 67 69 73 74 65 72 0a 2a  ache" register.*
21d36 2a 20 61 6e 64 20 6d 6f 64 69 66 79 20 74 68 65  * and modify the
21d37 20 65 78 70 72 65 73 73 69 6f 6e 20 73 6f 20 74   expression so t
21d38 68 61 74 20 74 68 65 20 6e 65 78 74 20 74 69 6d  hat the next tim
21d39 65 20 69 74 20 69 73 20 65 76 61 6c 75 61 74 65  e it is evaluate
21d3a 64 2c 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74  d,.** the result
21d3b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68   is a copy of th
21d3c 65 20 63 61 63 68 65 20 72 65 67 69 73 74 65 72  e cache register
21d3d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
21d3e 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f 72  tine is used for
21d3f 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61   expressions tha
21d40 74 20 61 72 65 20 75 73 65 64 20 6d 75 6c 74 69  t are used multi
21d41 70 6c 65 20 0a 2a 2a 20 74 69 6d 65 73 2e 20 20  ple .** times.  
21d42 54 68 65 79 20 61 72 65 20 65 76 61 6c 75 61 74  They are evaluat
21d43 65 64 20 6f 6e 63 65 20 61 6e 64 20 74 68 65 20  ed once and the 
21d44 72 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 65  results of the e
21d45 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 61 72 65  xpression.** are
21d46 20 72 65 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49   reused..*/.SQLI
21d47 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
21d48 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41 6e  qlite3ExprCodeAn
21d49 64 43 61 63 68 65 28 50 61 72 73 65 20 2a 70 50  dCache(Parse *pP
21d4a 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70  arse, Expr *pExp
21d4b 72 2c 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a  r, int target){.
21d4c 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
21d4d 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74  se->pVdbe;.  int
21d4e 20 69 6e 52 65 67 3b 0a 20 20 69 6e 52 65 67 20   inReg;.  inReg 
21d4f 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
21d50 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c  e(pParse, pExpr,
21d51 20 74 61 72 67 65 74 29 3b 0a 20 20 61 73 73 65   target);.  asse
21d52 72 74 28 20 74 61 72 67 65 74 3e 30 20 29 3b 0a  rt( target>0 );.
21d53 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e    /* This routin
21d54 65 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20  e is called for 
21d55 74 65 72 6d 73 20 74 6f 20 49 4e 53 45 52 54 20  terms to INSERT 
21d56 6f 72 20 55 50 44 41 54 45 2e 20 20 41 6e 64 20  or UPDATE.  And 
21d57 74 68 65 20 6f 6e 6c 79 0a 20 20 2a 2a 20 6f 74  the only.  ** ot
21d58 68 65 72 20 70 6c 61 63 65 20 77 68 65 72 65 20  her place where 
21d59 65 78 70 72 65 73 73 69 6f 6e 73 20 63 61 6e 20  expressions can 
21d5a 62 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74  be converted int
21d5b 6f 20 54 4b 5f 52 45 47 49 53 54 45 52 20 69 73  o TK_REGISTER is
21d5c 0a 20 20 2a 2a 20 69 6e 20 57 48 45 52 45 20 63  .  ** in WHERE c
21d5d 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67  lause processing
21d5e 2e 20 20 53 6f 20 61 73 20 63 75 72 72 65 6e 74  .  So as current
21d5f 6c 79 20 69 6d 70 6c 65 6d 65 6e 74 65 64 2c 20  ly implemented, 
21d60 74 68 65 72 65 20 69 73 0a 20 20 2a 2a 20 6e 6f  there is.  ** no
21d61 20 77 61 79 20 66 6f 72 20 61 20 54 4b 5f 52 45   way for a TK_RE
21d62 47 49 53 54 45 52 20 74 6f 20 65 78 69 73 74 20  GISTER to exist 
21d63 68 65 72 65 2e 20 20 42 75 74 20 69 74 20 73 65  here.  But it se
21d64 65 6d 73 20 70 72 75 64 65 6e 74 20 74 6f 0a 20  ems prudent to. 
21d65 20 2a 2a 20 6b 65 65 70 20 74 68 65 20 41 4c 57   ** keep the ALW
21d66 41 59 53 28 29 20 69 6e 20 63 61 73 65 20 74 68  AYS() in case th
21d67 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 62 6f  e conditions abo
21d68 76 65 20 63 68 61 6e 67 65 20 77 69 74 68 20 66  ve change with f
21d69 75 74 75 72 65 0a 20 20 2a 2a 20 6d 6f 64 69 66  uture.  ** modif
21d6a 69 63 61 74 69 6f 6e 73 20 6f 72 20 65 6e 68 61  ications or enha
21d6b 6e 63 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20 69  ncements. */.  i
21d6c 66 28 20 41 4c 57 41 59 53 28 70 45 78 70 72 2d  f( ALWAYS(pExpr-
21d6d 3e 6f 70 21 3d 54 4b 5f 52 45 47 49 53 54 45 52  >op!=TK_REGISTER
21d6e 29 20 29 7b 20 20 0a 20 20 20 20 69 6e 74 20 69  ) ){  .    int i
21d6f 4d 65 6d 3b 0a 20 20 20 20 69 4d 65 6d 20 3d 20  Mem;.    iMem = 
21d70 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
21d71 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21d72 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79  ddOp2(v, OP_Copy
21d73 2c 20 69 6e 52 65 67 2c 20 69 4d 65 6d 29 3b 0a  , inReg, iMem);.
21d74 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c      pExpr->iTabl
21d75 65 20 3d 20 69 4d 65 6d 3b 0a 20 20 20 20 70 45  e = iMem;.    pE
21d76 78 70 72 2d 3e 6f 70 32 20 3d 20 70 45 78 70 72  xpr->op2 = pExpr
21d77 2d 3e 6f 70 3b 0a 20 20 20 20 70 45 78 70 72 2d  ->op;.    pExpr-
21d78 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45  >op = TK_REGISTE
21d79 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  R;.  }.  return 
21d7a 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  inReg;.}../*.** 
21d7b 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 70  Return TRUE if p
21d7c 45 78 70 72 20 69 73 20 61 6e 20 63 6f 6e 73 74  Expr is an const
21d7d 61 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 20 74  ant expression t
21d7e 68 61 74 20 69 73 20 61 70 70 72 6f 70 72 69 61  hat is appropria
21d7f 74 65 0a 2a 2a 20 66 6f 72 20 66 61 63 74 6f 72  te.** for factor
21d80 69 6e 67 20 6f 75 74 20 6f 66 20 61 20 6c 6f 6f  ing out of a loo
21d81 70 2e 20 20 41 70 70 72 6f 70 72 69 61 74 65 20  p.  Appropriate 
21d82 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 3a  expressions are:
21d83 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 41 6e 79  .**.**    *  Any
21d84 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74   expression that
21d85 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 74 77   evaluates to tw
21d86 6f 20 6f 72 20 6d 6f 72 65 20 6f 70 63 6f 64 65  o or more opcode
21d87 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 41  s..**.**    *  A
21d88 6e 79 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 4f  ny OP_Integer, O
21d89 50 5f 52 65 61 6c 2c 20 4f 50 5f 53 74 72 69 6e  P_Real, OP_Strin
21d8a 67 2c 20 4f 50 5f 42 6c 6f 62 2c 20 4f 50 5f 4e  g, OP_Blob, OP_N
21d8b 75 6c 6c 2c 20 0a 2a 2a 20 20 20 20 20 20 20 6f  ull, .**       o
21d8c 72 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 74 68  r OP_Variable th
21d8d 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64  at does not need
21d8e 20 74 6f 20 62 65 20 70 6c 61 63 65 64 20 69 6e   to be placed in
21d8f 20 61 20 0a 2a 2a 20 20 20 20 20 20 20 73 70 65   a .**       spe
21d90 63 69 66 69 63 20 72 65 67 69 73 74 65 72 2e 0a  cific register..
21d91 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e  **.** There is n
21d92 6f 20 70 6f 69 6e 74 20 69 6e 20 66 61 63 74 6f  o point in facto
21d93 72 69 6e 67 20 6f 75 74 20 73 69 6e 67 6c 65 2d  ring out single-
21d94 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f 6e 73  instruction cons
21d95 74 61 6e 74 0a 2a 2a 20 65 78 70 72 65 73 73 69  tant.** expressi
21d96 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f  ons that need to
21d97 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20 61 20   be placed in a 
21d98 70 61 72 74 69 63 75 6c 61 72 20 72 65 67 69 73  particular regis
21d99 74 65 72 2e 20 20 0a 2a 2a 20 57 65 20 63 6f 75  ter.  .** We cou
21d9a 6c 64 20 66 61 63 74 6f 72 20 74 68 65 6d 20 6f  ld factor them o
21d9b 75 74 2c 20 62 75 74 20 74 68 65 6e 20 77 65 20  ut, but then we 
21d9c 77 6f 75 6c 64 20 65 6e 64 20 75 70 20 61 64 64  would end up add
21d9d 69 6e 67 20 61 6e 0a 2a 2a 20 4f 50 5f 53 43 6f  ing an.** OP_SCo
21d9e 70 79 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74  py instruction t
21d9f 6f 20 6d 6f 76 65 20 74 68 65 20 76 61 6c 75 65  o move the value
21da0 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 72 65 63   into the correc
21da1 74 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6c 61  t register.** la
21da2 74 65 72 2e 20 20 57 65 20 6d 69 67 68 74 20 61  ter.  We might a
21da3 73 20 77 65 6c 6c 20 6a 75 73 74 20 75 73 65 20  s well just use 
21da4 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 69 6e 73  the original ins
21da5 74 72 75 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20  truction and.** 
21da6 61 76 6f 69 64 20 74 68 65 20 4f 50 5f 53 43 6f  avoid the OP_SCo
21da7 70 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  py..*/.static in
21da8 74 20 69 73 41 70 70 72 6f 70 72 69 61 74 65 46  t isAppropriateF
21da9 6f 72 46 61 63 74 6f 72 69 6e 67 28 45 78 70 72  orFactoring(Expr
21daa 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 73 71 6c   *p){.  if( !sql
21dab 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61  ite3ExprIsConsta
21dac 6e 74 4e 6f 74 4a 6f 69 6e 28 70 29 20 29 7b 0a  ntNotJoin(p) ){.
21dad 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 2f      return 0;  /
21dae 2a 20 4f 6e 6c 79 20 63 6f 6e 73 74 61 6e 74 20  * Only constant 
21daf 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72 65 20  expressions are 
21db0 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20  appropriate for 
21db1 66 61 63 74 6f 72 69 6e 67 20 2a 2f 0a 20 20 7d  factoring */.  }
21db2 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73  .  if( (p->flags
21db3 20 26 20 45 50 5f 46 69 78 65 64 44 65 73 74 29   & EP_FixedDest)
21db4 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
21db5 6e 20 31 3b 20 20 2f 2a 20 41 6e 79 20 63 6f 6e  n 1;  /* Any con
21db6 73 74 61 6e 74 20 77 69 74 68 6f 75 74 20 61 20  stant without a 
21db7 66 69 78 65 64 20 64 65 73 74 69 6e 61 74 69 6f  fixed destinatio
21db8 6e 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65  n is appropriate
21db9 20 2a 2f 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28   */.  }.  while(
21dba 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53   p->op==TK_UPLUS
21dbb 20 29 20 70 20 3d 20 70 2d 3e 70 4c 65 66 74 3b   ) p = p->pLeft;
21dbc 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70  .  switch( p->op
21dbd 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   ){.#ifndef SQLI
21dbe 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54  TE_OMIT_BLOB_LIT
21dbf 45 52 41 4c 0a 20 20 20 20 63 61 73 65 20 54 4b  ERAL.    case TK
21dc0 5f 42 4c 4f 42 3a 0a 23 65 6e 64 69 66 0a 20 20  _BLOB:.#endif.  
21dc1 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49 41 42    case TK_VARIAB
21dc2 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  LE:.    case TK_
21dc3 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63 61 73  INTEGER:.    cas
21dc4 65 20 54 4b 5f 46 4c 4f 41 54 3a 0a 20 20 20 20  e TK_FLOAT:.    
21dc5 63 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a 0a 20 20  case TK_NULL:.  
21dc6 20 20 63 61 73 65 20 54 4b 5f 53 54 52 49 4e 47    case TK_STRING
21dc7 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61  : {.      testca
21dc8 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 42 4c  se( p->op==TK_BL
21dc9 4f 42 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  OB );.      test
21dca 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f  case( p->op==TK_
21dcb 56 41 52 49 41 42 4c 45 20 29 3b 0a 20 20 20 20  VARIABLE );.    
21dcc 20 20 74 65 73 74 63 61 73 65 28 20 70 2d 3e 6f    testcase( p->o
21dcd 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 3b  p==TK_INTEGER );
21dce 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21dcf 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54   p->op==TK_FLOAT
21dd0 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
21dd1 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55  se( p->op==TK_NU
21dd2 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  LL );.      test
21dd3 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f  case( p->op==TK_
21dd4 53 54 52 49 4e 47 20 29 3b 0a 20 20 20 20 20 20  STRING );.      
21dd5 2f 2a 20 53 69 6e 67 6c 65 2d 69 6e 73 74 72 75  /* Single-instru
21dd6 63 74 69 6f 6e 20 63 6f 6e 73 74 61 6e 74 73 20  ction constants 
21dd7 77 69 74 68 20 61 20 66 69 78 65 64 20 64 65 73  with a fixed des
21dd8 74 69 6e 61 74 69 6f 6e 20 61 72 65 0a 20 20 20  tination are.   
21dd9 20 20 20 2a 2a 20 62 65 74 74 65 72 20 64 6f 6e     ** better don
21dda 65 20 69 6e 2d 6c 69 6e 65 2e 20 20 49 66 20 77  e in-line.  If w
21ddb 65 20 66 61 63 74 6f 72 20 74 68 65 6d 2c 20 74  e factor them, t
21ddc 68 65 79 20 77 69 6c 6c 20 6a 75 73 74 20 65 6e  hey will just en
21ddd 64 0a 20 20 20 20 20 20 2a 2a 20 75 70 20 67 65  d.      ** up ge
21dde 6e 65 72 61 74 69 6e 67 20 61 6e 20 4f 50 5f 53  nerating an OP_S
21ddf 43 6f 70 79 20 74 6f 20 6d 6f 76 65 20 74 68 65  Copy to move the
21de0 20 76 61 6c 75 65 20 74 6f 20 74 68 65 20 64 65   value to the de
21de1 73 74 69 6e 61 74 69 6f 6e 0a 20 20 20 20 20 20  stination.      
21de2 2a 2a 20 72 65 67 69 73 74 65 72 2e 20 2a 2f 0a  ** register. */.
21de3 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
21de4 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
21de5 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20 20 20  K_UMINUS: {.    
21de6 20 20 69 66 28 20 70 2d 3e 70 4c 65 66 74 2d 3e    if( p->pLeft->
21de7 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20  op==TK_FLOAT || 
21de8 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b  p->pLeft->op==TK
21de9 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20 20 20 20  _INTEGER ){.    
21dea 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
21deb 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
21dec 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
21ded 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 62 72  ault: {.      br
21dee 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
21def 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
21df0 0a 2a 2a 20 49 66 20 70 45 78 70 72 20 69 73 20  .** If pExpr is 
21df1 61 20 63 6f 6e 73 74 61 6e 74 20 65 78 70 72 65  a constant expre
21df2 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 61 70  ssion that is ap
21df3 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a 2a 2a  propriate for.**
21df4 20 66 61 63 74 6f 72 69 6e 67 20 6f 75 74 20 6f   factoring out o
21df5 66 20 61 20 6c 6f 6f 70 2c 20 74 68 65 6e 20 65  f a loop, then e
21df6 76 61 6c 75 61 74 65 20 74 68 65 20 65 78 70 72  valuate the expr
21df7 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e 74 6f 20 61  ession.** into a
21df8 20 72 65 67 69 73 74 65 72 20 61 6e 64 20 63 6f   register and co
21df9 6e 76 65 72 74 20 74 68 65 20 65 78 70 72 65 73  nvert the expres
21dfa 73 69 6f 6e 20 69 6e 74 6f 20 61 20 54 4b 5f 52  sion into a TK_R
21dfb 45 47 49 53 54 45 52 0a 2a 2a 20 65 78 70 72 65  EGISTER.** expre
21dfc 73 73 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ssion..*/.static
21dfd 20 69 6e 74 20 65 76 61 6c 43 6f 6e 73 74 45 78   int evalConstEx
21dfe 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b  pr(Walker *pWalk
21dff 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29  er, Expr *pExpr)
21e00 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  {.  Parse *pPars
21e01 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61  e = pWalker->pPa
21e02 72 73 65 3b 0a 20 20 73 77 69 74 63 68 28 20 70  rse;.  switch( p
21e03 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20  Expr->op ){.    
21e04 63 61 73 65 20 54 4b 5f 49 4e 3a 0a 20 20 20 20  case TK_IN:.    
21e05 63 61 73 65 20 54 4b 5f 52 45 47 49 53 54 45 52  case TK_REGISTER
21e06 3a 20 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  : {.      return
21e07 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20   WRC_Prune;.    
21e08 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55  }.    case TK_FU
21e09 4e 43 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65  NCTION:.    case
21e0a 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e   TK_AGG_FUNCTION
21e0b 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f  :.    case TK_CO
21e0c 4e 53 54 5f 46 55 4e 43 3a 20 7b 0a 20 20 20 20  NST_FUNC: {.    
21e0d 20 20 2f 2a 20 54 68 65 20 61 72 67 75 6d 65 6e    /* The argumen
21e0e 74 73 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e  ts to a function
21e0f 20 68 61 76 65 20 61 20 66 69 78 65 64 20 64 65   have a fixed de
21e10 73 74 69 6e 61 74 69 6f 6e 2e 0a 20 20 20 20 20  stination..     
21e11 20 2a 2a 20 4d 61 72 6b 20 74 68 65 6d 20 74 68   ** Mark them th
21e12 69 73 20 77 61 79 20 74 6f 20 61 76 6f 69 64 20  is way to avoid 
21e13 67 65 6e 65 72 61 74 65 64 20 75 6e 6e 65 65 64  generated unneed
21e14 65 64 20 4f 50 5f 53 43 6f 70 79 0a 20 20 20 20  ed OP_SCopy.    
21e15 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e    ** instruction
21e16 73 2e 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  s. .      */.   
21e17 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69     ExprList *pLi
21e18 73 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c  st = pExpr->x.pL
21e19 69 73 74 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ist;.      asser
21e1a 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70 65  t( !ExprHasPrope
21e1b 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49  rty(pExpr, EP_xI
21e1c 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20 20 20  sSelect) );.    
21e1d 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20    if( pList ){. 
21e1e 20 20 20 20 20 20 20 69 6e 74 20 69 20 3d 20 70         int i = p
21e1f 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20  List->nExpr;.   
21e20 20 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72       struct Expr
21e21 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
21e22 20 3d 20 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20   = pList->a;.   
21e23 20 20 20 20 20 66 6f 72 28 3b 20 69 3e 30 3b 20       for(; i>0; 
21e24 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i--, pItem++){. 
21e25 20 20 20 20 20 20 20 20 20 69 66 28 20 41 4c 57           if( ALW
21e26 41 59 53 28 70 49 74 65 6d 2d 3e 70 45 78 70 72  AYS(pItem->pExpr
21e27 29 20 29 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  ) ) pItem->pExpr
21e28 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 46 69  ->flags |= EP_Fi
21e29 78 65 64 44 65 73 74 3b 0a 20 20 20 20 20 20 20  xedDest;.       
21e2a 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
21e2b 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
21e2c 7d 0a 20 20 69 66 28 20 69 73 41 70 70 72 6f 70  }.  if( isApprop
21e2d 72 69 61 74 65 46 6f 72 46 61 63 74 6f 72 69 6e  riateForFactorin
21e2e 67 28 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20  g(pExpr) ){.    
21e2f 69 6e 74 20 72 31 20 3d 20 2b 2b 70 50 61 72 73  int r1 = ++pPars
21e30 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 69 6e 74  e->nMem;.    int
21e31 20 72 32 3b 0a 20 20 20 20 72 32 20 3d 20 73 71   r2;.    r2 = sq
21e32 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72  lite3ExprCodeTar
21e33 67 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70  get(pParse, pExp
21e34 72 2c 20 72 31 29 3b 0a 20 20 20 20 69 66 28 20  r, r1);.    if( 
21e35 4e 45 56 45 52 28 72 31 21 3d 72 32 29 20 29 20  NEVER(r1!=r2) ) 
21e36 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
21e37 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
21e38 29 3b 0a 20 20 20 20 70 45 78 70 72 2d 3e 6f 70  );.    pExpr->op
21e39 32 20 3d 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20  2 = pExpr->op;. 
21e3a 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54     pExpr->op = T
21e3b 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20 20  K_REGISTER;.    
21e3c 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20  pExpr->iTable = 
21e3d 72 32 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 57  r2;.    return W
21e3e 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20  RC_Prune;.  }.  
21e3f 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69  return WRC_Conti
21e40 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72  nue;.}../*.** Pr
21e41 65 65 76 61 6c 75 61 74 65 20 63 6f 6e 73 74 61  eevaluate consta
21e42 6e 74 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e  nt subexpression
21e43 73 20 77 69 74 68 69 6e 20 70 45 78 70 72 20 61  s within pExpr a
21e44 6e 64 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20  nd store the.** 
21e45 72 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73  results in regis
21e46 74 65 72 73 2e 20 20 4d 6f 64 69 66 79 20 70 45  ters.  Modify pE
21e47 78 70 72 20 73 6f 20 74 68 61 74 20 74 68 65 20  xpr so that the 
21e48 63 6f 6e 73 74 61 6e 74 20 73 75 62 65 78 70 72  constant subexpr
21e49 65 73 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 54 4b  esions.** are TK
21e4a 5f 52 45 47 49 53 54 45 52 20 6f 70 63 6f 64 65  _REGISTER opcode
21e4b 73 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20  s that refer to 
21e4c 74 68 65 20 70 72 65 63 6f 6d 70 75 74 65 64 20  the precomputed 
21e4d 76 61 6c 75 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54  values..*/.SQLIT
21e4e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
21e4f 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 43 6f  qlite3ExprCodeCo
21e50 6e 73 74 61 6e 74 73 28 50 61 72 73 65 20 2a 70  nstants(Parse *p
21e51 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78  Parse, Expr *pEx
21e52 70 72 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b  pr){.  Walker w;
21e53 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61  .  w.xExprCallba
21e54 63 6b 20 3d 20 65 76 61 6c 43 6f 6e 73 74 45 78  ck = evalConstEx
21e55 70 72 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43  pr;.  w.xSelectC
21e56 61 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 77  allback = 0;.  w
21e57 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
21e58 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45  ;.  sqlite3WalkE
21e59 78 70 72 28 26 77 2c 20 70 45 78 70 72 29 3b 0a  xpr(&w, pExpr);.
21e5a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  }.../*.** Genera
21e5b 74 65 20 63 6f 64 65 20 74 68 61 74 20 70 75 73  te code that pus
21e5c 68 65 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66  hes the value of
21e5d 20 65 76 65 72 79 20 65 6c 65 6d 65 6e 74 20 6f   every element o
21e5e 66 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 65  f the given.** e
21e5f 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20 69  xpression list i
21e60 6e 74 6f 20 61 20 73 65 71 75 65 6e 63 65 20 6f  nto a sequence o
21e61 66 20 72 65 67 69 73 74 65 72 73 20 62 65 67 69  f registers begi
21e62 6e 6e 69 6e 67 20 61 74 20 74 61 72 67 65 74 2e  nning at target.
21e63 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  .**.** Return th
21e64 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d  e number of elem
21e65 65 6e 74 73 20 65 76 61 6c 75 61 74 65 64 2e 0a  ents evaluated..
21e66 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
21e67 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
21e68 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28 0a 20  rCodeExprList(. 
21e69 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
21e6a 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
21e6b 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72  ontext */.  Expr
21e6c 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 2f  List *pList,   /
21e6d 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e  * The expression
21e6e 20 6c 69 73 74 20 74 6f 20 62 65 20 63 6f 64 65   list to be code
21e6f 64 20 2a 2f 0a 20 20 69 6e 74 20 74 61 72 67 65  d */.  int targe
21e70 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65  t,        /* Whe
21e71 72 65 20 74 6f 20 77 72 69 74 65 20 72 65 73 75  re to write resu
21e72 6c 74 73 20 2a 2f 0a 20 20 69 6e 74 20 64 6f 48  lts */.  int doH
21e73 61 72 64 43 6f 70 79 20 20 20 20 20 2f 2a 20 4d  ardCopy     /* M
21e74 61 6b 65 20 61 20 68 61 72 64 20 63 6f 70 79 20  ake a hard copy 
21e75 6f 66 20 65 76 65 72 79 20 65 6c 65 6d 65 6e 74  of every element
21e76 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20   */.){.  struct 
21e77 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
21e78 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 2c 20 6e  Item;.  int i, n
21e79 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69 73  ;.  assert( pLis
21e7a 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  t!=0 );.  assert
21e7b 28 20 74 61 72 67 65 74 3e 30 20 29 3b 0a 20 20  ( target>0 );.  
21e7c 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  n = pList->nExpr
21e7d 3b 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c  ;.  for(pItem=pL
21e7e 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 6e  ist->a, i=0; i<n
21e7f 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
21e80 0a 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e  .    if( pItem->
21e81 69 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 20 20  iAlias ){.      
21e82 69 6e 74 20 69 52 65 67 20 3d 20 63 6f 64 65 41  int iReg = codeA
21e83 6c 69 61 73 28 70 50 61 72 73 65 2c 20 70 49 74  lias(pParse, pIt
21e84 65 6d 2d 3e 69 41 6c 69 61 73 2c 20 70 49 74 65  em->iAlias, pIte
21e85 6d 2d 3e 70 45 78 70 72 2c 20 74 61 72 67 65 74  m->pExpr, target
21e86 2b 69 29 3b 0a 20 20 20 20 20 20 56 64 62 65 20  +i);.      Vdbe 
21e87 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  *v = sqlite3GetV
21e88 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20  dbe(pParse);.   
21e89 20 20 20 69 66 28 20 69 52 65 67 21 3d 74 61 72     if( iReg!=tar
21e8a 67 65 74 2b 69 20 29 7b 0a 20 20 20 20 20 20 20  get+i ){.       
21e8b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21e8c 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20  p2(v, OP_SCopy, 
21e8d 69 52 65 67 2c 20 74 61 72 67 65 74 2b 69 29 3b  iReg, target+i);
21e8e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
21e8f 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
21e90 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
21e91 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20  , pItem->pExpr, 
21e92 74 61 72 67 65 74 2b 69 29 3b 0a 20 20 20 20 7d  target+i);.    }
21e93 0a 20 20 20 20 69 66 28 20 64 6f 48 61 72 64 43  .    if( doHardC
21e94 6f 70 79 20 26 26 20 21 70 50 61 72 73 65 2d 3e  opy && !pParse->
21e95 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
21e96 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
21e97 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 70 50  3ExprHardCopy(pP
21e98 61 72 73 65 2c 20 74 61 72 67 65 74 2c 20 6e 29  arse, target, n)
21e99 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
21e9a 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn n;.}../*.**
21e9b 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66   Generate code f
21e9c 6f 72 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70  or a boolean exp
21e9d 72 65 73 73 69 6f 6e 20 73 75 63 68 20 74 68 61  ression such tha
21e9e 74 20 61 20 6a 75 6d 70 20 69 73 20 6d 61 64 65  t a jump is made
21e9f 0a 2a 2a 20 74 6f 20 74 68 65 20 6c 61 62 65 6c  .** to the label
21ea0 20 22 64 65 73 74 22 20 69 66 20 74 68 65 20 65   "dest" if the e
21ea1 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 72 75  xpression is tru
21ea2 65 20 62 75 74 20 65 78 65 63 75 74 69 6f 6e 0a  e but execution.
21ea3 2a 2a 20 63 6f 6e 74 69 6e 75 65 73 20 73 74 72  ** continues str
21ea4 61 69 67 68 74 20 74 68 72 75 20 69 66 20 74 68  aight thru if th
21ea5 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
21ea6 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  false..**.** If 
21ea7 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 65  the expression e
21ea8 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c  valuates to NULL
21ea9 20 28 6e 65 69 74 68 65 72 20 74 72 75 65 20 6e   (neither true n
21eaa 6f 72 20 66 61 6c 73 65 29 2c 20 74 68 65 6e 0a  or false), then.
21eab 2a 2a 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70  ** take the jump
21eac 20 69 66 20 74 68 65 20 6a 75 6d 70 49 66 4e 75   if the jumpIfNu
21ead 6c 6c 20 66 6c 61 67 20 69 73 20 53 51 4c 49 54  ll flag is SQLIT
21eae 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 2e 0a 2a 2a  E_JUMPIFNULL..**
21eaf 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 64 65  .** This code de
21eb0 70 65 6e 64 73 20 6f 6e 20 74 68 65 20 66 61 63  pends on the fac
21eb1 74 20 74 68 61 74 20 63 65 72 74 61 69 6e 20 74  t that certain t
21eb2 6f 6b 65 6e 20 76 61 6c 75 65 73 20 28 65 78 3a  oken values (ex:
21eb3 20 54 4b 5f 45 51 29 0a 2a 2a 20 61 72 65 20 74   TK_EQ).** are t
21eb4 68 65 20 73 61 6d 65 20 61 73 20 6f 70 63 6f 64  he same as opcod
21eb5 65 20 76 61 6c 75 65 73 20 28 65 78 3a 20 4f 50  e values (ex: OP
21eb6 5f 45 71 29 20 74 68 61 74 20 69 6d 70 6c 65 6d  _Eq) that implem
21eb7 65 6e 74 20 74 68 65 20 63 6f 72 72 65 73 70 6f  ent the correspo
21eb8 6e 64 69 6e 67 0a 2a 2a 20 6f 70 65 72 61 74 69  nding.** operati
21eb9 6f 6e 2e 20 20 53 70 65 63 69 61 6c 20 63 6f 6d  on.  Special com
21eba 6d 65 6e 74 73 20 69 6e 20 76 64 62 65 2e 63 20  ments in vdbe.c 
21ebb 61 6e 64 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65  and the mkopcode
21ebc 68 2e 61 77 6b 20 73 63 72 69 70 74 20 69 6e 0a  h.awk script in.
21ebd 2a 2a 20 74 68 65 20 6d 61 6b 65 20 70 72 6f 63  ** the make proc
21ebe 65 73 73 20 63 61 75 73 65 20 74 68 65 73 65 20  ess cause these 
21ebf 76 61 6c 75 65 73 20 74 6f 20 61 6c 69 67 6e 2e  values to align.
21ec0 20 20 41 73 73 65 72 74 28 29 73 20 69 6e 20 74    Assert()s in t
21ec1 68 65 20 63 6f 64 65 0a 2a 2a 20 62 65 6c 6f 77  he code.** below
21ec2 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68 65   verify that the
21ec3 20 6e 75 6d 62 65 72 73 20 61 72 65 20 61 6c 69   numbers are ali
21ec4 67 6e 65 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a  gned correctly..
21ec5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
21ec6 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
21ec7 70 72 49 66 54 72 75 65 28 50 61 72 73 65 20 2a  prIfTrue(Parse *
21ec8 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45  pParse, Expr *pE
21ec9 78 70 72 2c 20 69 6e 74 20 64 65 73 74 2c 20 69  xpr, int dest, i
21eca 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a  nt jumpIfNull){.
21ecb 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
21ecc 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74  se->pVdbe;.  int
21ecd 20 6f 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72   op = 0;.  int r
21ece 65 67 46 72 65 65 31 20 3d 20 30 3b 0a 20 20 69  egFree1 = 0;.  i
21ecf 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20 30 3b  nt regFree2 = 0;
21ed0 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 3b 0a 0a  .  int r1, r2;..
21ed1 20 20 61 73 73 65 72 74 28 20 6a 75 6d 70 49 66    assert( jumpIf
21ed2 4e 75 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d  Null==SQLITE_JUM
21ed3 50 49 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49  PIFNULL || jumpI
21ed4 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66  fNull==0 );.  if
21ed5 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20  ( NEVER(v==0) ) 
21ed6 20 20 20 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20      return;  /* 
21ed7 45 78 69 73 74 61 6e 63 65 20 6f 66 20 56 44 42  Existance of VDB
21ed8 45 20 63 68 65 63 6b 65 64 20 62 79 20 63 61 6c  E checked by cal
21ed9 6c 65 72 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56  ler */.  if( NEV
21eda 45 52 28 70 45 78 70 72 3d 3d 30 29 20 29 20 72  ER(pExpr==0) ) r
21edb 65 74 75 72 6e 3b 20 20 2f 2a 20 4e 6f 20 77 61  eturn;  /* No wa
21edc 79 20 74 68 69 73 20 63 61 6e 20 68 61 70 70 65  y this can happe
21edd 6e 20 2a 2f 0a 20 20 6f 70 20 3d 20 70 45 78 70  n */.  op = pExp
21ede 72 2d 3e 6f 70 3b 0a 20 20 73 77 69 74 63 68 28  r->op;.  switch(
21edf 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20   op ){.    case 
21ee0 54 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20 20 20 20  TK_AND: {.      
21ee1 69 6e 74 20 64 32 20 3d 20 73 71 6c 69 74 65 33  int d2 = sqlite3
21ee2 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
21ee3 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21ee4 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20  ( jumpIfNull==0 
21ee5 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
21ee6 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70 50  ExprCachePush(pP
21ee7 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71 6c  arse);.      sql
21ee8 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
21ee9 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
21eea 4c 65 66 74 2c 20 64 32 2c 6a 75 6d 70 49 66 4e  Left, d2,jumpIfN
21eeb 75 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55 4d 50 49  ull^SQLITE_JUMPI
21eec 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 73 71  FNULL);.      sq
21eed 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
21eee 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
21eef 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d  Right, dest, jum
21ef0 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  pIfNull);.      
21ef1 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
21ef2 76 65 4c 61 62 65 6c 28 76 2c 20 64 32 29 3b 0a  veLabel(v, d2);.
21ef3 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
21ef4 72 43 61 63 68 65 50 6f 70 28 70 50 61 72 73 65  rCachePop(pParse
21ef5 2c 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61  , 1);.      brea
21ef6 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
21ef7 65 20 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20 20 20  e TK_OR: {.     
21ef8 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49   testcase( jumpI
21ef9 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  fNull==0 );.    
21efa 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 54    sqlite3ExprIfT
21efb 72 75 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  rue(pParse, pExp
21efc 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c 20  r->pLeft, dest, 
21efd 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20  jumpIfNull);.   
21efe 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
21eff 54 72 75 65 28 70 50 61 72 73 65 2c 20 70 45 78  True(pParse, pEx
21f00 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74  pr->pRight, dest
21f01 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20  , jumpIfNull);. 
21f02 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21f03 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f  }.    case TK_NO
21f04 54 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63  T: {.      testc
21f05 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d  ase( jumpIfNull=
21f06 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =0 );.      sqli
21f07 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70  te3ExprIfFalse(p
21f08 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
21f09 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49  eft, dest, jumpI
21f0a 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72  fNull);.      br
21f0b 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
21f0c 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63  ase TK_LT:.    c
21f0d 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63  ase TK_LE:.    c
21f0e 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63  ase TK_GT:.    c
21f0f 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63  ase TK_GE:.    c
21f10 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63  ase TK_NE:.    c
21f11 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20  ase TK_EQ: {.   
21f12 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54     assert( TK_LT
21f13 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 20 20 20 20 20  ==OP_Lt );.     
21f14 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 3d 3d   assert( TK_LE==
21f15 4f 50 5f 4c 65 20 29 3b 0a 20 20 20 20 20 20 61  OP_Le );.      a
21f16 73 73 65 72 74 28 20 54 4b 5f 47 54 3d 3d 4f 50  ssert( TK_GT==OP
21f17 5f 47 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73  _Gt );.      ass
21f18 65 72 74 28 20 54 4b 5f 47 45 3d 3d 4f 50 5f 47  ert( TK_GE==OP_G
21f19 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  e );.      asser
21f1a 74 28 20 54 4b 5f 45 51 3d 3d 4f 50 5f 45 71 20  t( TK_EQ==OP_Eq 
21f1b 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
21f1c 20 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 29 3b   TK_NE==OP_Ne );
21f1d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21f1e 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20   op==TK_LT );.  
21f1f 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
21f20 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 20  ==TK_LE );.     
21f21 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
21f22 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20 20 74 65  K_GT );.      te
21f23 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47  stcase( op==TK_G
21f24 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  E );.      testc
21f25 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51 20 29  ase( op==TK_EQ )
21f26 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21f27 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20  ( op==TK_NE );. 
21f28 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a       testcase( j
21f29 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a  umpIfNull==0 );.
21f2a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72        codeCompar
21f2b 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73 65  eOperands(pParse
21f2c 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
21f2d 26 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c 0a  &r1, &regFree1,.
21f2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f30 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c    pExpr->pRight,
21f31 20 26 72 32 2c 20 26 72 65 67 46 72 65 65 32 29   &r2, &regFree2)
21f32 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70  ;.      codeComp
21f33 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78 70  are(pParse, pExp
21f34 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72 2d  r->pLeft, pExpr-
21f35 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20  >pRight, op,.   
21f36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
21f37 31 2c 20 72 32 2c 20 64 65 73 74 2c 20 6a 75 6d  1, r2, dest, jum
21f38 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  pIfNull);.      
21f39 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
21f3a 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74  e1==0 );.      t
21f3b 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
21f3c 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72  2==0 );.      br
21f3d 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
21f3e 61 73 65 20 54 4b 5f 49 53 3a 0a 20 20 20 20 63  ase TK_IS:.    c
21f3f 61 73 65 20 54 4b 5f 49 53 4e 4f 54 3a 20 7b 0a  ase TK_ISNOT: {.
21f40 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
21f41 6f 70 3d 3d 54 4b 5f 49 53 20 29 3b 0a 20 20 20  op==TK_IS );.   
21f42 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
21f43 3d 54 4b 5f 49 53 4e 4f 54 20 29 3b 0a 20 20 20  =TK_ISNOT );.   
21f44 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70     codeCompareOp
21f45 65 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70  erands(pParse, p
21f46 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31  Expr->pLeft, &r1
21f47 2c 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20  , &regFree1,.   
21f48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21f49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
21f4a 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72  Expr->pRight, &r
21f4b 32 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20  2, &regFree2);. 
21f4c 20 20 20 20 20 6f 70 20 3d 20 28 6f 70 3d 3d 54       op = (op==T
21f4d 4b 5f 49 53 29 20 3f 20 54 4b 5f 45 51 20 3a 20  K_IS) ? TK_EQ : 
21f4e 54 4b 5f 4e 45 3b 0a 20 20 20 20 20 20 63 6f 64  TK_NE;.      cod
21f4f 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c  eCompare(pParse,
21f50 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70   pExpr->pLeft, p
21f51 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70  Expr->pRight, op
21f52 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
21f53 20 20 20 20 72 31 2c 20 72 32 2c 20 64 65 73 74      r1, r2, dest
21f54 2c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 45 51 29  , SQLITE_NULLEQ)
21f55 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
21f56 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b  ( regFree1==0 );
21f57 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21f58 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29 3b 0a   regFree2==0 );.
21f59 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21f5a 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49   }.    case TK_I
21f5b 53 4e 55 4c 4c 3a 0a 20 20 20 20 63 61 73 65 20  SNULL:.    case 
21f5c 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20  TK_NOTNULL: {.  
21f5d 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 49      assert( TK_I
21f5e 53 4e 55 4c 4c 3d 3d 4f 50 5f 49 73 4e 75 6c 6c  SNULL==OP_IsNull
21f5f 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
21f60 28 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50  ( TK_NOTNULL==OP
21f61 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 20 20  _NotNull );.    
21f62 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
21f63 54 4b 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20  TK_ISNULL );.   
21f64 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
21f65 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20  =TK_NOTNULL );. 
21f66 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65       r1 = sqlite
21f67 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
21f68 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65  arse, pExpr->pLe
21f69 66 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a  ft, &regFree1);.
21f6a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
21f6b 65 41 64 64 4f 70 32 28 76 2c 20 6f 70 2c 20 72  eAddOp2(v, op, r
21f6c 31 2c 20 64 65 73 74 29 3b 0a 20 20 20 20 20 20  1, dest);.      
21f6d 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
21f6e 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62  e1==0 );.      b
21f6f 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
21f70 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a  case TK_BETWEEN:
21f71 20 7b 0a 20 20 20 20 20 20 2f 2a 20 20 20 20 78   {.      /*    x
21f72 20 42 45 54 57 45 45 4e 20 79 20 41 4e 44 20 7a   BETWEEN y AND z
21f73 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
21f74 2a 2a 20 49 73 20 65 71 75 69 76 61 6c 65 6e 74  ** Is equivalent
21f75 20 74 6f 20 0a 20 20 20 20 20 20 2a 2a 0a 20 20   to .      **.  
21f76 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d 79 20 41      **    x>=y A
21f77 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 20 20 2a 2a  ND x<=z.      **
21f78 0a 20 20 20 20 20 20 2a 2a 20 43 6f 64 65 20 69  .      ** Code i
21f79 74 20 61 73 20 73 75 63 68 2c 20 74 61 6b 69 6e  t as such, takin
21f7a 67 20 63 61 72 65 20 74 6f 20 64 6f 20 74 68 65  g care to do the
21f7b 20 63 6f 6d 6d 6f 6e 20 73 75 62 65 78 70 72 65   common subexpre
21f7c 73 73 69 6f 6e 0a 20 20 20 20 20 20 2a 2a 20 65  ssion.      ** e
21f7d 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 78  lementation of x
21f7e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
21f7f 20 45 78 70 72 20 65 78 70 72 41 6e 64 3b 0a 20   Expr exprAnd;. 
21f80 20 20 20 20 20 45 78 70 72 20 63 6f 6d 70 4c 65       Expr compLe
21f81 66 74 3b 0a 20 20 20 20 20 20 45 78 70 72 20 63  ft;.      Expr c
21f82 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20  ompRight;.      
21f83 45 78 70 72 20 65 78 70 72 58 3b 0a 0a 20 20 20  Expr exprX;..   
21f84 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
21f85 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
21f86 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
21f87 20 29 3b 0a 20 20 20 20 20 20 65 78 70 72 58 20   );.      exprX 
21f88 3d 20 2a 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b  = *pExpr->pLeft;
21f89 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64 2e 6f  .      exprAnd.o
21f8a 70 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20 20 20  p = TK_AND;.    
21f8b 20 20 65 78 70 72 41 6e 64 2e 70 4c 65 66 74 20    exprAnd.pLeft 
21f8c 3d 20 26 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20  = &compLeft;.   
21f8d 20 20 20 65 78 70 72 41 6e 64 2e 70 52 69 67 68     exprAnd.pRigh
21f8e 74 20 3d 20 26 63 6f 6d 70 52 69 67 68 74 3b 0a  t = &compRight;.
21f8f 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e 6f        compLeft.o
21f90 70 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 20 20 20  p = TK_GE;.     
21f91 20 63 6f 6d 70 4c 65 66 74 2e 70 4c 65 66 74 20   compLeft.pLeft 
21f92 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20  = &exprX;.      
21f93 63 6f 6d 70 4c 65 66 74 2e 70 52 69 67 68 74 20  compLeft.pRight 
21f94 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  = pExpr->x.pList
21f95 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[0].pExpr;.  
21f96 20 20 20 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70      compRight.op
21f97 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20   = TK_LE;.      
21f98 63 6f 6d 70 52 69 67 68 74 2e 70 4c 65 66 74 20  compRight.pLeft 
21f99 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20  = &exprX;.      
21f9a 63 6f 6d 70 52 69 67 68 74 2e 70 52 69 67 68 74  compRight.pRight
21f9b 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73   = pExpr->x.pLis
21f9c 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20  t->a[1].pExpr;. 
21f9d 20 20 20 20 20 65 78 70 72 58 2e 69 54 61 62 6c       exprX.iTabl
21f9e 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  e = sqlite3ExprC
21f9f 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20  odeTemp(pParse, 
21fa0 26 65 78 70 72 58 2c 20 26 72 65 67 46 72 65 65  &exprX, &regFree
21fa1 31 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  1);.      testca
21fa2 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20  se( regFree1==0 
21fa3 29 3b 0a 20 20 20 20 20 20 65 78 70 72 58 2e 6f  );.      exprX.o
21fa4 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b  p = TK_REGISTER;
21fa5 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
21fa6 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29   jumpIfNull==0 )
21fa7 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
21fa8 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65  xprIfTrue(pParse
21fa9 2c 20 26 65 78 70 72 41 6e 64 2c 20 64 65 73 74  , &exprAnd, dest
21faa 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20  , jumpIfNull);. 
21fab 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21fac 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  }.    default: {
21fad 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69  .      r1 = sqli
21fae 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
21faf 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26  pParse, pExpr, &
21fb0 72 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20  regFree1);.     
21fb1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21fb2 70 33 28 76 2c 20 4f 50 5f 49 66 2c 20 72 31 2c  p3(v, OP_If, r1,
21fb3 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c   dest, jumpIfNul
21fb4 6c 21 3d 30 29 3b 0a 20 20 20 20 20 20 74 65 73  l!=0);.      tes
21fb5 74 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d  tcase( regFree1=
21fb6 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  =0 );.      test
21fb7 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c  case( jumpIfNull
21fb8 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65  ==0 );.      bre
21fb9 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
21fba 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
21fbb 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
21fbc 67 46 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74  gFree1);.  sqlit
21fbd 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
21fbe 28 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65  (pParse, regFree
21fbf 32 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  2);  .}../*.** G
21fc0 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
21fc1 20 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65   a boolean expre
21fc2 73 73 69 6f 6e 20 73 75 63 68 20 74 68 61 74 20  ssion such that 
21fc3 61 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 0a 2a  a jump is made.*
21fc4 2a 20 74 6f 20 74 68 65 20 6c 61 62 65 6c 20 22  * to the label "
21fc5 64 65 73 74 22 20 69 66 20 74 68 65 20 65 78 70  dest" if the exp
21fc6 72 65 73 73 69 6f 6e 20 69 73 20 66 61 6c 73 65  ression is false
21fc7 20 62 75 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a   but execution.*
21fc8 2a 20 63 6f 6e 74 69 6e 75 65 73 20 73 74 72 61  * continues stra
21fc9 69 67 68 74 20 74 68 72 75 20 69 66 20 74 68 65  ight thru if the
21fca 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74   expression is t
21fcb 72 75 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  rue..**.** If th
21fcc 65 20 65 78 70 72 65 73 73 69 6f 6e 20 65 76 61  e expression eva
21fcd 6c 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c 20 28  luates to NULL (
21fce 6e 65 69 74 68 65 72 20 74 72 75 65 20 6e 6f 72  neither true nor
21fcf 20 66 61 6c 73 65 29 20 74 68 65 6e 0a 2a 2a 20   false) then.** 
21fd0 6a 75 6d 70 20 69 66 20 6a 75 6d 70 49 66 4e 75  jump if jumpIfNu
21fd1 6c 6c 20 69 73 20 53 51 4c 49 54 45 5f 4a 55 4d  ll is SQLITE_JUM
21fd2 50 49 46 4e 55 4c 4c 20 6f 72 20 66 61 6c 6c 20  PIFNULL or fall 
21fd3 74 68 72 6f 75 67 68 20 69 66 20 6a 75 6d 70 49  through if jumpI
21fd4 66 4e 75 6c 6c 0a 2a 2a 20 69 73 20 30 2e 0a 2a  fNull.** is 0..*
21fd5 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
21fd6 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
21fd7 72 49 66 46 61 6c 73 65 28 50 61 72 73 65 20 2a  rIfFalse(Parse *
21fd8 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45  pParse, Expr *pE
21fd9 78 70 72 2c 20 69 6e 74 20 64 65 73 74 2c 20 69  xpr, int dest, i
21fda 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a  nt jumpIfNull){.
21fdb 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
21fdc 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74  se->pVdbe;.  int
21fdd 20 6f 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72   op = 0;.  int r
21fde 65 67 46 72 65 65 31 20 3d 20 30 3b 0a 20 20 69  egFree1 = 0;.  i
21fdf 6e 74 20 72 65 67 46 72 65 65 32 20 3d 20 30 3b  nt regFree2 = 0;
21fe0 0a 20 20 69 6e 74 20 72 31 2c 20 72 32 3b 0a 0a  .  int r1, r2;..
21fe1 20 20 61 73 73 65 72 74 28 20 6a 75 6d 70 49 66    assert( jumpIf
21fe2 4e 75 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d  Null==SQLITE_JUM
21fe3 50 49 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49  PIFNULL || jumpI
21fe4 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66  fNull==0 );.  if
21fe5 28 20 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20  ( NEVER(v==0) ) 
21fe6 72 65 74 75 72 6e 3b 20 2f 2a 20 45 78 69 73 74  return; /* Exist
21fe7 61 6e 63 65 20 6f 66 20 56 44 42 45 20 63 68 65  ance of VDBE che
21fe8 63 6b 65 64 20 62 79 20 63 61 6c 6c 65 72 20 2a  cked by caller *
21fe9 2f 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30  /.  if( pExpr==0
21fea 20 29 20 20 20 20 72 65 74 75 72 6e 3b 0a 0a 20   )    return;.. 
21feb 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66   /* The value of
21fec 20 70 45 78 70 72 2d 3e 6f 70 20 61 6e 64 20 6f   pExpr->op and o
21fed 70 20 61 72 65 20 72 65 6c 61 74 65 64 20 61 73  p are related as
21fee 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20   follows:.  **. 
21fef 20 2a 2a 20 20 20 20 20 20 20 70 45 78 70 72 2d   **       pExpr-
21ff0 3e 6f 70 20 20 20 20 20 20 20 20 20 20 20 20 6f  >op            o
21ff1 70 0a 20 20 2a 2a 20 20 20 20 20 20 20 2d 2d 2d  p.  **       ---
21ff2 2d 2d 2d 2d 2d 2d 20 20 20 20 20 20 20 20 20 20  ------          
21ff3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20  ----------.  ** 
21ff4 20 20 20 20 20 20 54 4b 5f 49 53 4e 55 4c 4c 20        TK_ISNULL 
21ff5 20 20 20 20 20 20 20 20 20 4f 50 5f 4e 6f 74 4e           OP_NotN
21ff6 75 6c 6c 0a 20 20 2a 2a 20 20 20 20 20 20 20 54  ull.  **       T
21ff7 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20  K_NOTNULL       
21ff8 20 20 4f 50 5f 49 73 4e 75 6c 6c 0a 20 20 2a 2a    OP_IsNull.  **
21ff9 20 20 20 20 20 20 20 54 4b 5f 4e 45 20 20 20 20         TK_NE    
21ffa 20 20 20 20 20 20 20 20 20 20 4f 50 5f 45 71 0a            OP_Eq.
21ffb 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 45 51    **       TK_EQ
21ffc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50                OP
21ffd 5f 4e 65 0a 20 20 2a 2a 20 20 20 20 20 20 20 54  _Ne.  **       T
21ffe 4b 5f 47 54 20 20 20 20 20 20 20 20 20 20 20 20  K_GT            
21fff 20 20 4f 50 5f 4c 65 0a 20 20 2a 2a 20 20 20 20    OP_Le.  **    
22000 20 20 20 54 4b 5f 4c 45 20 20 20 20 20 20 20 20     TK_LE        
22001 20 20 20 20 20 20 4f 50 5f 47 74 0a 20 20 2a 2a        OP_Gt.  **
22002 20 20 20 20 20 20 20 54 4b 5f 47 45 20 20 20 20         TK_GE    
22003 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4c 74 0a            OP_Lt.
22004 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4c 54    **       TK_LT
22005 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 50                OP
22006 5f 47 65 0a 20 20 2a 2a 0a 20 20 2a 2a 20 46 6f  _Ge.  **.  ** Fo
22007 72 20 6f 74 68 65 72 20 76 61 6c 75 65 73 20 6f  r other values o
22008 66 20 70 45 78 70 72 2d 3e 6f 70 2c 20 6f 70 20  f pExpr->op, op 
22009 69 73 20 75 6e 64 65 66 69 6e 65 64 20 61 6e 64  is undefined and
2200a 20 75 6e 75 73 65 64 2e 0a 20 20 2a 2a 20 54 68   unused..  ** Th
2200b 65 20 76 61 6c 75 65 20 6f 66 20 54 4b 5f 20 61  e value of TK_ a
2200c 6e 64 20 4f 50 5f 20 63 6f 6e 73 74 61 6e 74 73  nd OP_ constants
2200d 20 61 72 65 20 61 72 72 61 6e 67 65 64 20 73 75   are arranged su
2200e 63 68 20 74 68 61 74 20 77 65 0a 20 20 2a 2a 20  ch that we.  ** 
2200f 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 20  can compute the 
22010 6d 61 70 70 69 6e 67 20 61 62 6f 76 65 20 75 73  mapping above us
22011 69 6e 67 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ing the followin
22012 67 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 20 20  g expression..  
22013 2a 2a 20 41 73 73 65 72 74 28 29 73 20 76 65 72  ** Assert()s ver
22014 69 66 79 20 74 68 61 74 20 74 68 65 20 63 6f 6d  ify that the com
22015 70 75 74 61 74 69 6f 6e 20 69 73 20 63 6f 72 72  putation is corr
22016 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 6f 70 20 3d  ect..  */.  op =
22017 20 28 28 70 45 78 70 72 2d 3e 6f 70 2b 28 54 4b   ((pExpr->op+(TK
22018 5f 49 53 4e 55 4c 4c 26 31 29 29 5e 31 29 2d 28  _ISNULL&1))^1)-(
22019 54 4b 5f 49 53 4e 55 4c 4c 26 31 29 3b 0a 0a 20  TK_ISNULL&1);.. 
2201a 20 2f 2a 20 56 65 72 69 66 79 20 63 6f 72 72 65   /* Verify corre
2201b 63 74 20 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20  ct alignment of 
2201c 54 4b 5f 20 61 6e 64 20 4f 50 5f 20 63 6f 6e 73  TK_ and OP_ cons
2201d 74 61 6e 74 73 0a 20 20 2a 2f 0a 20 20 61 73 73  tants.  */.  ass
2201e 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d  ert( pExpr->op!=
2201f 54 4b 5f 49 53 4e 55 4c 4c 20 7c 7c 20 6f 70 3d  TK_ISNULL || op=
22020 3d 4f 50 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20  =OP_NotNull );. 
22021 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
22022 6f 70 21 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 7c  op!=TK_NOTNULL |
22023 7c 20 6f 70 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20  | op==OP_IsNull 
22024 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78  );.  assert( pEx
22025 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4e 45 20 7c 7c  pr->op!=TK_NE ||
22026 20 6f 70 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20   op==OP_Eq );.  
22027 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f  assert( pExpr->o
22028 70 21 3d 54 4b 5f 45 51 20 7c 7c 20 6f 70 3d 3d  p!=TK_EQ || op==
22029 4f 50 5f 4e 65 20 29 3b 0a 20 20 61 73 73 65 72  OP_Ne );.  asser
2202a 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  t( pExpr->op!=TK
2202b 5f 4c 54 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 47 65  _LT || op==OP_Ge
2202c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45   );.  assert( pE
2202d 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 45 20 7c  xpr->op!=TK_LE |
2202e 7c 20 6f 70 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20  | op==OP_Gt );. 
2202f 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
22030 6f 70 21 3d 54 4b 5f 47 54 20 7c 7c 20 6f 70 3d  op!=TK_GT || op=
22031 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20 61 73 73 65  =OP_Le );.  asse
22032 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  rt( pExpr->op!=T
22033 4b 5f 47 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c  K_GE || op==OP_L
22034 74 20 29 3b 0a 0a 20 20 73 77 69 74 63 68 28 20  t );..  switch( 
22035 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20  pExpr->op ){.   
22036 20 63 61 73 65 20 54 4b 5f 41 4e 44 3a 20 7b 0a   case TK_AND: {.
22037 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
22038 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b  jumpIfNull==0 );
22039 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2203a 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65  prIfFalse(pParse
2203b 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20  , pExpr->pLeft, 
2203c 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c  dest, jumpIfNull
2203d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2203e 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72  ExprIfFalse(pPar
2203f 73 65 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68  se, pExpr->pRigh
22040 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  t, dest, jumpIfN
22041 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ull);.      brea
22042 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
22043 65 20 54 4b 5f 4f 52 3a 20 7b 0a 20 20 20 20 20  e TK_OR: {.     
22044 20 69 6e 74 20 64 32 20 3d 20 73 71 6c 69 74 65   int d2 = sqlite
22045 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
22046 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
22047 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30  e( jumpIfNull==0
22048 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
22049 33 45 78 70 72 43 61 63 68 65 50 75 73 68 28 70  3ExprCachePush(p
2204a 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71  Parse);.      sq
2204b 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
2204c 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
2204d 4c 65 66 74 2c 20 64 32 2c 20 6a 75 6d 70 49 66  Left, d2, jumpIf
2204e 4e 75 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55 4d 50  Null^SQLITE_JUMP
2204f 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 73  IFNULL);.      s
22050 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
22051 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
22052 3e 70 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a  >pRight, dest, j
22053 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20  umpIfNull);.    
22054 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
22055 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 32 29  olveLabel(v, d2)
22056 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
22057 78 70 72 43 61 63 68 65 50 6f 70 28 70 50 61 72  xprCachePop(pPar
22058 73 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 62 72  se, 1);.      br
22059 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
2205a 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20 20  ase TK_NOT: {.  
2205b 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
2205c 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 70 45  fTrue(pParse, pE
2205d 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74  xpr->pLeft, dest
2205e 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20  , jumpIfNull);. 
2205f 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
22060 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 54  }.    case TK_LT
22061 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c 45  :.    case TK_LE
22062 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 54  :.    case TK_GT
22063 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47 45  :.    case TK_GE
22064 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 45  :.    case TK_NE
22065 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 51  :.    case TK_EQ
22066 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61  : {.      testca
22067 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b  se( op==TK_LT );
22068 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
22069 20 6f 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20   op==TK_LE );.  
2206a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
2206b 3d 3d 54 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20  ==TK_GT );.     
2206c 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
2206d 4b 5f 47 45 20 29 3b 0a 20 20 20 20 20 20 74 65  K_GE );.      te
2206e 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45  stcase( op==TK_E
2206f 51 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  Q );.      testc
22070 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29  ase( op==TK_NE )
22071 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
22072 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20  ( jumpIfNull==0 
22073 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d  );.      codeCom
22074 70 61 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61  pareOperands(pPa
22075 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
22076 74 2c 20 26 72 31 2c 20 26 72 65 67 46 72 65 65  t, &r1, &regFree
22077 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1,.             
22078 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22079 20 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67       pExpr->pRig
2207a 68 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65  ht, &r2, &regFre
2207b 65 32 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43  e2);.      codeC
2207c 6f 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70  ompare(pParse, p
2207d 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78  Expr->pLeft, pEx
2207e 70 72 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a  pr->pRight, op,.
2207f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22080 20 20 72 31 2c 20 72 32 2c 20 64 65 73 74 2c 20    r1, r2, dest, 
22081 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20  jumpIfNull);.   
22082 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67     testcase( reg
22083 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20  Free1==0 );.    
22084 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46    testcase( regF
22085 72 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ree2==0 );.     
22086 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
22087 20 20 63 61 73 65 20 54 4b 5f 49 53 3a 0a 20 20    case TK_IS:.  
22088 20 20 63 61 73 65 20 54 4b 5f 49 53 4e 4f 54 3a    case TK_ISNOT:
22089 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73   {.      testcas
2208a 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  e( pExpr->op==TK
2208b 5f 49 53 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _IS );.      tes
2208c 74 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70  tcase( pExpr->op
2208d 3d 3d 54 4b 5f 49 53 4e 4f 54 20 29 3b 0a 20 20  ==TK_ISNOT );.  
2208e 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f      codeCompareO
2208f 70 65 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20  perands(pParse, 
22090 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72  pExpr->pLeft, &r
22091 31 2c 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20  1, &regFree1,.  
22092 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22093 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22094 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26  pExpr->pRight, &
22095 72 32 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a  r2, &regFree2);.
22096 20 20 20 20 20 20 6f 70 20 3d 20 28 70 45 78 70        op = (pExp
22097 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 53 29 20 3f 20  r->op==TK_IS) ? 
22098 54 4b 5f 4e 45 20 3a 20 54 4b 5f 45 51 3b 0a 20  TK_NE : TK_EQ;. 
22099 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65       codeCompare
2209a 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
2209b 70 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52  pLeft, pExpr->pR
2209c 69 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20  ight, op,.      
2209d 20 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20              r1, 
2209e 72 32 2c 20 64 65 73 74 2c 20 53 51 4c 49 54 45  r2, dest, SQLITE
2209f 5f 4e 55 4c 4c 45 51 29 3b 0a 20 20 20 20 20 20  _NULLEQ);.      
220a0 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
220a1 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74  e1==0 );.      t
220a2 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65  estcase( regFree
220a3 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72  2==0 );.      br
220a4 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
220a5 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20  ase TK_ISNULL:. 
220a6 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 4e 55     case TK_NOTNU
220a7 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  LL: {.      test
220a8 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e  case( op==TK_ISN
220a9 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ULL );.      tes
220aa 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f  tcase( op==TK_NO
220ab 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 72  TNULL );.      r
220ac 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  1 = sqlite3ExprC
220ad 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20  odeTemp(pParse, 
220ae 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72  pExpr->pLeft, &r
220af 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20  egFree1);.      
220b0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
220b1 32 28 76 2c 20 6f 70 2c 20 72 31 2c 20 64 65 73  2(v, op, r1, des
220b2 74 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  t);.      testca
220b3 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20  se( regFree1==0 
220b4 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
220b5 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
220b6 4b 5f 42 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20  K_BETWEEN: {.   
220b7 20 20 20 2f 2a 20 20 20 20 78 20 42 45 54 57 45     /*    x BETWE
220b8 45 4e 20 79 20 41 4e 44 20 7a 0a 20 20 20 20 20  EN y AND z.     
220b9 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 73 20   **.      ** Is 
220ba 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20  equivalent to . 
220bb 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
220bc 20 20 20 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d      x>=y AND x<=
220bd 7a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  z.      **.     
220be 20 2a 2a 20 43 6f 64 65 20 69 74 20 61 73 20 73   ** Code it as s
220bf 75 63 68 2c 20 74 61 6b 69 6e 67 20 63 61 72 65  uch, taking care
220c0 20 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 6d 6f   to do the commo
220c1 6e 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 0a  n subexpression.
220c2 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74        ** element
220c3 61 74 69 6f 6e 20 6f 66 20 78 2e 0a 20 20 20 20  ation of x..    
220c4 20 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20    */.      Expr 
220c5 65 78 70 72 41 6e 64 3b 0a 20 20 20 20 20 20 45  exprAnd;.      E
220c6 78 70 72 20 63 6f 6d 70 4c 65 66 74 3b 0a 20 20  xpr compLeft;.  
220c7 20 20 20 20 45 78 70 72 20 63 6f 6d 70 52 69 67      Expr compRig
220c8 68 74 3b 0a 20 20 20 20 20 20 45 78 70 72 20 65  ht;.      Expr e
220c9 78 70 72 58 3b 0a 0a 20 20 20 20 20 20 61 73 73  xprX;..      ass
220ca 65 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f  ert( !ExprHasPro
220cb 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
220cc 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20  xIsSelect) );.  
220cd 20 20 20 20 65 78 70 72 58 20 3d 20 2a 70 45 78      exprX = *pEx
220ce 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20  pr->pLeft;.     
220cf 20 65 78 70 72 41 6e 64 2e 6f 70 20 3d 20 54 4b   exprAnd.op = TK
220d0 5f 41 4e 44 3b 0a 20 20 20 20 20 20 65 78 70 72  _AND;.      expr
220d1 41 6e 64 2e 70 4c 65 66 74 20 3d 20 26 63 6f 6d  And.pLeft = &com
220d2 70 4c 65 66 74 3b 0a 20 20 20 20 20 20 65 78 70  pLeft;.      exp
220d3 72 41 6e 64 2e 70 52 69 67 68 74 20 3d 20 26 63  rAnd.pRight = &c
220d4 6f 6d 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20  ompRight;.      
220d5 63 6f 6d 70 4c 65 66 74 2e 6f 70 20 3d 20 54 4b  compLeft.op = TK
220d6 5f 47 45 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c  _GE;.      compL
220d7 65 66 74 2e 70 4c 65 66 74 20 3d 20 26 65 78 70  eft.pLeft = &exp
220d8 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65  rX;.      compLe
220d9 66 74 2e 70 52 69 67 68 74 20 3d 20 70 45 78 70  ft.pRight = pExp
220da 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 30 5d  r->x.pList->a[0]
220db 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 63 6f  .pExpr;.      co
220dc 6d 70 52 69 67 68 74 2e 6f 70 20 3d 20 54 4b 5f  mpRight.op = TK_
220dd 4c 45 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52 69  LE;.      compRi
220de 67 68 74 2e 70 4c 65 66 74 20 3d 20 26 65 78 70  ght.pLeft = &exp
220df 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d 70 52 69  rX;.      compRi
220e0 67 68 74 2e 70 52 69 67 68 74 20 3d 20 70 45 78  ght.pRight = pEx
220e1 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61 5b 31  pr->x.pList->a[1
220e2 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 65  ].pExpr;.      e
220e3 78 70 72 58 2e 69 54 61 62 6c 65 20 3d 20 73 71  xprX.iTable = sq
220e4 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d  lite3ExprCodeTem
220e5 70 28 70 50 61 72 73 65 2c 20 26 65 78 70 72 58  p(pParse, &exprX
220e6 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20 20  , &regFree1);.  
220e7 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72 65      testcase( re
220e8 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20  gFree1==0 );.   
220e9 20 20 20 65 78 70 72 58 2e 6f 70 20 3d 20 54 4b     exprX.op = TK
220ea 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20 20 20  _REGISTER;.     
220eb 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49   testcase( jumpI
220ec 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  fNull==0 );.    
220ed 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
220ee 61 6c 73 65 28 70 50 61 72 73 65 2c 20 26 65 78  alse(pParse, &ex
220ef 70 72 41 6e 64 2c 20 64 65 73 74 2c 20 6a 75 6d  prAnd, dest, jum
220f0 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  pIfNull);.      
220f1 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
220f2 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20   default: {.    
220f3 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45 78    r1 = sqlite3Ex
220f4 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73  prCodeTemp(pPars
220f5 65 2c 20 70 45 78 70 72 2c 20 26 72 65 67 46 72  e, pExpr, &regFr
220f6 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ee1);.      sqli
220f7 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
220f8 20 4f 50 5f 49 66 4e 6f 74 2c 20 72 31 2c 20 64   OP_IfNot, r1, d
220f9 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 21  est, jumpIfNull!
220fa 3d 30 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  =0);.      testc
220fb 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30  ase( regFree1==0
220fc 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
220fd 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d  se( jumpIfNull==
220fe 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  0 );.      break
220ff 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
22100 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
22101 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46  Reg(pParse, regF
22102 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 65 33  ree1);.  sqlite3
22103 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
22104 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32 29  Parse, regFree2)
22105 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 20  ;.}../*.** Do a 
22106 64 65 65 70 20 63 6f 6d 70 61 72 69 73 6f 6e 20  deep comparison 
22107 6f 66 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f  of two expressio
22108 6e 20 74 72 65 65 73 2e 20 20 52 65 74 75 72 6e  n trees.  Return
22109 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f 29   TRUE (non-zero)
2210a 0a 2a 2a 20 69 66 20 74 68 65 79 20 61 72 65 20  .** if they are 
2210b 69 64 65 6e 74 69 63 61 6c 20 61 6e 64 20 72 65  identical and re
2210c 74 75 72 6e 20 46 41 4c 53 45 20 69 66 20 74 68  turn FALSE if th
2210d 65 79 20 64 69 66 66 65 72 20 69 6e 20 61 6e 79  ey differ in any
2210e 20 77 61 79 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65   way..**.** Some
2210f 74 69 6d 65 73 20 74 68 69 73 20 72 6f 75 74 69  times this routi
22110 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 46  ne will return F
22111 41 4c 53 45 20 65 76 65 6e 20 69 66 20 74 68 65  ALSE even if the
22112 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e 73   two expressions
22113 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 72 65 20 65  .** really are e
22114 71 75 69 76 61 6c 65 6e 74 2e 20 20 49 66 20 77  quivalent.  If w
22115 65 20 63 61 6e 6e 6f 74 20 70 72 6f 76 65 20 74  e cannot prove t
22116 68 61 74 20 74 68 65 20 65 78 70 72 65 73 73 69  hat the expressi
22117 6f 6e 73 20 61 72 65 0a 2a 2a 20 69 64 65 6e 74  ons are.** ident
22118 69 63 61 6c 2c 20 77 65 20 72 65 74 75 72 6e 20  ical, we return 
22119 46 41 4c 53 45 20 6a 75 73 74 20 74 6f 20 62 65  FALSE just to be
2211a 20 73 61 66 65 2e 20 20 53 6f 20 69 66 20 74 68   safe.  So if th
2211b 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65  is routine.** re
2211c 74 75 72 6e 73 20 66 61 6c 73 65 2c 20 74 68 65  turns false, the
2211d 6e 20 79 6f 75 20 64 6f 20 6e 6f 74 20 72 65 61  n you do not rea
2211e 6c 6c 79 20 6b 6e 6f 77 20 66 6f 72 20 63 65 72  lly know for cer
2211f 74 61 69 6e 20 69 66 20 74 68 65 20 74 77 6f 0a  tain if the two.
22120 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61  ** expressions a
22121 72 65 20 74 68 65 20 73 61 6d 65 2e 20 20 42 75  re the same.  Bu
22122 74 20 69 66 20 79 6f 75 20 67 65 74 20 61 20 54  t if you get a T
22123 52 55 45 20 72 65 74 75 72 6e 2c 20 74 68 65 6e  RUE return, then
22124 20 79 6f 75 0a 2a 2a 20 63 61 6e 20 62 65 20 73   you.** can be s
22125 75 72 65 20 74 68 65 20 65 78 70 72 65 73 73 69  ure the expressi
22126 6f 6e 73 20 61 72 65 20 74 68 65 20 73 61 6d 65  ons are the same
22127 2e 20 20 49 6e 20 74 68 65 20 70 6c 61 63 65 73  .  In the places
22128 20 77 68 65 72 65 0a 2a 2a 20 74 68 69 73 20 72   where.** this r
22129 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 2c 20  outine is used, 
2212a 69 74 20 64 6f 65 73 20 6e 6f 74 20 68 75 72 74  it does not hurt
2212b 20 74 6f 20 67 65 74 20 61 6e 20 65 78 74 72 61   to get an extra
2212c 20 46 41 4c 53 45 20 2d 20 74 68 61 74 0a 2a 2a   FALSE - that.**
2212d 20 6a 75 73 74 20 6d 69 67 68 74 20 72 65 73 75   just might resu
2212e 6c 74 20 69 6e 20 73 6f 6d 65 20 73 6c 69 67 68  lt in some sligh
2212f 74 6c 79 20 73 6c 6f 77 65 72 20 63 6f 64 65 2e  tly slower code.
22130 20 20 42 75 74 20 72 65 74 75 72 6e 69 6e 67 0a    But returning.
22131 2a 2a 20 61 6e 20 69 6e 63 6f 72 72 65 63 74 20  ** an incorrect 
22132 54 52 55 45 20 63 6f 75 6c 64 20 6c 65 61 64 20  TRUE could lead 
22133 74 6f 20 61 20 6d 61 6c 66 75 6e 63 74 69 6f 6e  to a malfunction
22134 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
22135 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
22136 78 70 72 43 6f 6d 70 61 72 65 28 45 78 70 72 20  xprCompare(Expr 
22137 2a 70 41 2c 20 45 78 70 72 20 2a 70 42 29 7b 0a  *pA, Expr *pB){.
22138 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70    int i;.  if( p
22139 41 3d 3d 30 7c 7c 70 42 3d 3d 30 20 29 7b 0a 20  A==0||pB==0 ){. 
2213a 20 20 20 72 65 74 75 72 6e 20 70 42 3d 3d 70 41     return pB==pA
2213b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
2213c 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65  !ExprHasAnyPrope
2213d 72 74 79 28 70 41 2c 20 45 50 5f 54 6f 6b 65 6e  rty(pA, EP_Token
2213e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29  Only|EP_Reduced)
2213f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 45   );.  assert( !E
22140 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74  xprHasAnyPropert
22141 79 28 70 42 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e  y(pB, EP_TokenOn
22142 6c 79 7c 45 50 5f 52 65 64 75 63 65 64 29 20 29  ly|EP_Reduced) )
22143 3b 0a 20 20 69 66 28 20 45 78 70 72 48 61 73 50  ;.  if( ExprHasP
22144 72 6f 70 65 72 74 79 28 70 41 2c 20 45 50 5f 78  roperty(pA, EP_x
22145 49 73 53 65 6c 65 63 74 29 20 7c 7c 20 45 78 70  IsSelect) || Exp
22146 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 42 2c  rHasProperty(pB,
22147 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
22148 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
22149 20 20 7d 0a 20 20 69 66 28 20 28 70 41 2d 3e 66    }.  if( (pA->f
2214a 6c 61 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e  lags & EP_Distin
2214b 63 74 29 21 3d 28 70 42 2d 3e 66 6c 61 67 73 20  ct)!=(pB->flags 
2214c 26 20 45 50 5f 44 69 73 74 69 6e 63 74 29 20 29  & EP_Distinct) )
2214d 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
2214e 20 70 41 2d 3e 6f 70 21 3d 70 42 2d 3e 6f 70 20   pA->op!=pB->op 
2214f 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66  ) return 0;.  if
22150 28 20 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f  ( !sqlite3ExprCo
22151 6d 70 61 72 65 28 70 41 2d 3e 70 4c 65 66 74 2c  mpare(pA->pLeft,
22152 20 70 42 2d 3e 70 4c 65 66 74 29 20 29 20 72 65   pB->pLeft) ) re
22153 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 21 73  turn 0;.  if( !s
22154 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72  qlite3ExprCompar
22155 65 28 70 41 2d 3e 70 52 69 67 68 74 2c 20 70 42  e(pA->pRight, pB
22156 2d 3e 70 52 69 67 68 74 29 20 29 20 72 65 74 75  ->pRight) ) retu
22157 72 6e 20 30 3b 0a 0a 20 20 69 66 28 20 70 41 2d  rn 0;..  if( pA-
22158 3e 78 2e 70 4c 69 73 74 20 26 26 20 70 42 2d 3e  >x.pList && pB->
22159 78 2e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 69  x.pList ){.    i
2215a 66 28 20 70 41 2d 3e 78 2e 70 4c 69 73 74 2d 3e  f( pA->x.pList->
2215b 6e 45 78 70 72 21 3d 70 42 2d 3e 78 2e 70 4c 69  nExpr!=pB->x.pLi
2215c 73 74 2d 3e 6e 45 78 70 72 20 29 20 72 65 74 75  st->nExpr ) retu
2215d 72 6e 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d  rn 0;.    for(i=
2215e 30 3b 20 69 3c 70 41 2d 3e 78 2e 70 4c 69 73 74  0; i<pA->x.pList
2215f 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20  ->nExpr; i++){. 
22160 20 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72       Expr *pExpr
22161 41 20 3d 20 70 41 2d 3e 78 2e 70 4c 69 73 74 2d  A = pA->x.pList-
22162 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[i].pExpr;.   
22163 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 42 20     Expr *pExprB 
22164 3d 20 70 42 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61  = pB->x.pList->a
22165 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [i].pExpr;.     
22166 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 70   if( !sqlite3Exp
22167 72 43 6f 6d 70 61 72 65 28 70 45 78 70 72 41 2c  rCompare(pExprA,
22168 20 70 45 78 70 72 42 29 20 29 20 72 65 74 75 72   pExprB) ) retur
22169 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  n 0;.    }.  }el
2216a 73 65 20 69 66 28 20 70 41 2d 3e 78 2e 70 4c 69  se if( pA->x.pLi
2216b 73 74 20 7c 7c 20 70 42 2d 3e 78 2e 70 4c 69 73  st || pB->x.pLis
2216c 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
2216d 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 41  0;.  }..  if( pA
2216e 2d 3e 69 54 61 62 6c 65 21 3d 70 42 2d 3e 69 54  ->iTable!=pB->iT
2216f 61 62 6c 65 20 7c 7c 20 70 41 2d 3e 69 43 6f 6c  able || pA->iCol
22170 75 6d 6e 21 3d 70 42 2d 3e 69 43 6f 6c 75 6d 6e  umn!=pB->iColumn
22171 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
22172 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  f( ExprHasProper
22173 74 79 28 70 41 2c 20 45 50 5f 49 6e 74 56 61 6c  ty(pA, EP_IntVal
22174 75 65 29 20 29 7b 0a 20 20 20 20 69 66 28 20 21  ue) ){.    if( !
22175 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
22176 70 42 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65 29  pB, EP_IntValue)
22177 20 7c 7c 20 70 41 2d 3e 75 2e 69 56 61 6c 75 65   || pA->u.iValue
22178 21 3d 70 42 2d 3e 75 2e 69 56 61 6c 75 65 20 29  !=pB->u.iValue )
22179 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
2217a 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
2217b 69 66 28 20 70 41 2d 3e 6f 70 21 3d 54 4b 5f 43  if( pA->op!=TK_C
2217c 4f 4c 55 4d 4e 20 26 26 20 70 41 2d 3e 75 2e 7a  OLUMN && pA->u.z
2217d 54 6f 6b 65 6e 20 29 7b 0a 20 20 20 20 69 66 28  Token ){.    if(
2217e 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
2217f 28 70 42 2c 20 45 50 5f 49 6e 74 56 61 6c 75 65  (pB, EP_IntValue
22180 29 20 7c 7c 20 4e 45 56 45 52 28 70 42 2d 3e 75  ) || NEVER(pB->u
22181 2e 7a 54 6f 6b 65 6e 3d 3d 30 29 20 29 20 72 65  .zToken==0) ) re
22182 74 75 72 6e 20 30 3b 0a 20 20 20 20 69 66 28 20  turn 0;.    if( 
22183 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
22184 41 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 70 42 2d 3e  A->u.zToken,pB->
22185 75 2e 7a 54 6f 6b 65 6e 29 21 3d 30 20 29 7b 0a  u.zToken)!=0 ){.
22186 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
22187 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
22188 72 6e 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  rn 1;.}.../*.** 
22189 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e  Add a new elemen
2218a 74 20 74 6f 20 74 68 65 20 70 41 67 67 49 6e 66  t to the pAggInf
2218b 6f 2d 3e 61 43 6f 6c 5b 5d 20 61 72 72 61 79 2e  o->aCol[] array.
2218c 20 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64    Return the ind
2218d 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77  ex of.** the new
2218e 20 65 6c 65 6d 65 6e 74 2e 20 20 52 65 74 75 72   element.  Retur
2218f 6e 20 61 20 6e 65 67 61 74 69 76 65 20 6e 75 6d  n a negative num
22190 62 65 72 20 69 66 20 6d 61 6c 6c 6f 63 20 66 61  ber if malloc fa
22191 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ils..*/.static i
22192 6e 74 20 61 64 64 41 67 67 49 6e 66 6f 43 6f 6c  nt addAggInfoCol
22193 75 6d 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  umn(sqlite3 *db,
22194 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66 6f 29   AggInfo *pInfo)
22195 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 70 49 6e  {.  int i;.  pIn
22196 66 6f 2d 3e 61 43 6f 6c 20 3d 20 73 71 6c 69 74  fo->aCol = sqlit
22197 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74 65 28  e3ArrayAllocate(
22198 0a 20 20 20 20 20 20 20 64 62 2c 0a 20 20 20 20  .       db,.    
22199 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c 2c 0a     pInfo->aCol,.
2219a 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 49         sizeof(pI
2219b 6e 66 6f 2d 3e 61 43 6f 6c 5b 30 5d 29 2c 0a 20  nfo->aCol[0]),. 
2219c 20 20 20 20 20 20 33 2c 0a 20 20 20 20 20 20 20        3,.       
2219d 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 2c  &pInfo->nColumn,
2219e 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f 2d 3e  .       &pInfo->
2219f 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 2c 0a 20 20  nColumnAlloc,.  
221a0 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20 20 72       &i.  );.  r
221a1 65 74 75 72 6e 20 69 3b 0a 7d 20 20 20 20 0a 0a  eturn i;.}    ..
221a2 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65 77 20  /*.** Add a new 
221a3 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65 20 70  element to the p
221a4 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d  AggInfo->aFunc[]
221a5 20 61 72 72 61 79 2e 20 20 52 65 74 75 72 6e 20   array.  Return 
221a6 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a 2a 20  the index of.** 
221a7 74 68 65 20 6e 65 77 20 65 6c 65 6d 65 6e 74 2e  the new element.
221a8 20 20 52 65 74 75 72 6e 20 61 20 6e 65 67 61 74    Return a negat
221a9 69 76 65 20 6e 75 6d 62 65 72 20 69 66 20 6d 61  ive number if ma
221aa 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73  lloc fails..*/.s
221ab 74 61 74 69 63 20 69 6e 74 20 61 64 64 41 67 67  tatic int addAgg
221ac 49 6e 66 6f 46 75 6e 63 28 73 71 6c 69 74 65 33  InfoFunc(sqlite3
221ad 20 2a 64 62 2c 20 41 67 67 49 6e 66 6f 20 2a 70   *db, AggInfo *p
221ae 49 6e 66 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  Info){.  int i;.
221af 20 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 20 3d    pInfo->aFunc =
221b0 20 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c   sqlite3ArrayAll
221b1 6f 63 61 74 65 28 0a 20 20 20 20 20 20 20 64 62  ocate(.       db
221b2 2c 20 0a 20 20 20 20 20 20 20 70 49 6e 66 6f 2d  , .       pInfo-
221b3 3e 61 46 75 6e 63 2c 0a 20 20 20 20 20 20 20 73  >aFunc,.       s
221b4 69 7a 65 6f 66 28 70 49 6e 66 6f 2d 3e 61 46 75  izeof(pInfo->aFu
221b5 6e 63 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20 33  nc[0]),.       3
221b6 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f 2d  ,.       &pInfo-
221b7 3e 6e 46 75 6e 63 2c 0a 20 20 20 20 20 20 20 26  >nFunc,.       &
221b8 70 49 6e 66 6f 2d 3e 6e 46 75 6e 63 41 6c 6c 6f  pInfo->nFuncAllo
221b9 63 2c 0a 20 20 20 20 20 20 20 26 69 0a 20 20 29  c,.       &i.  )
221ba 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 20  ;.  return i;.} 
221bb 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20     ../*.** This 
221bc 69 73 20 74 68 65 20 78 45 78 70 72 43 61 6c 6c  is the xExprCall
221bd 62 61 63 6b 20 66 6f 72 20 61 20 74 72 65 65 20  back for a tree 
221be 77 61 6c 6b 65 72 2e 20 20 49 74 20 69 73 20 75  walker.  It is u
221bf 73 65 64 20 74 6f 0a 2a 2a 20 69 6d 70 6c 65 6d  sed to.** implem
221c0 65 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 41  ent sqlite3ExprA
221c1 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73  nalyzeAggregates
221c2 28 29 2e 20 20 53 65 65 20 73 71 6c 69 74 65 33  ().  See sqlite3
221c3 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72 65  ExprAnalyzeAggre
221c4 67 61 74 65 73 0a 2a 2a 20 66 6f 72 20 61 64 64  gates.** for add
221c5 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
221c6 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ion..*/.static i
221c7 6e 74 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67  nt analyzeAggreg
221c8 61 74 65 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c  ate(Walker *pWal
221c9 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70 72  ker, Expr *pExpr
221ca 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4e 61  ){.  int i;.  Na
221cb 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 20 3d  meContext *pNC =
221cc 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b   pWalker->u.pNC;
221cd 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
221ce 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a   = pNC->pParse;.
221cf 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c    SrcList *pSrcL
221d0 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c  ist = pNC->pSrcL
221d1 69 73 74 3b 0a 20 20 41 67 67 49 6e 66 6f 20 2a  ist;.  AggInfo *
221d2 70 41 67 67 49 6e 66 6f 20 3d 20 70 4e 43 2d 3e  pAggInfo = pNC->
221d3 70 41 67 67 49 6e 66 6f 3b 0a 0a 20 20 73 77 69  pAggInfo;..  swi
221d4 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29  tch( pExpr->op )
221d5 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47  {.    case TK_AG
221d6 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61  G_COLUMN:.    ca
221d7 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a  se TK_COLUMN: {.
221d8 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
221d9 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41 47  pExpr->op==TK_AG
221da 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20  G_COLUMN );.    
221db 20 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70    testcase( pExp
221dc 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e  r->op==TK_COLUMN
221dd 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 43 68 65   );.      /* Che
221de 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ck to see if the
221df 20 63 6f 6c 75 6d 6e 20 69 73 20 69 6e 20 6f 6e   column is in on
221e0 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 73 20  e of the tables 
221e1 69 6e 20 74 68 65 20 46 52 4f 4d 0a 20 20 20 20  in the FROM.    
221e2 20 20 2a 2a 20 63 6c 61 75 73 65 20 6f 66 20 74    ** clause of t
221e3 68 65 20 61 67 67 72 65 67 61 74 65 20 71 75 65  he aggregate que
221e4 72 79 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  ry */.      if( 
221e5 41 4c 57 41 59 53 28 70 53 72 63 4c 69 73 74 21  ALWAYS(pSrcList!
221e6 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73  =0) ){.        s
221e7 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
221e8 65 6d 20 2a 70 49 74 65 6d 20 3d 20 70 53 72 63  em *pItem = pSrc
221e9 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20 20 20  List->a;.       
221ea 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53 72 63   for(i=0; i<pSrc
221eb 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c  List->nSrc; i++,
221ec 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
221ed 20 20 20 20 20 73 74 72 75 63 74 20 41 67 67 49       struct AggI
221ee 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 3b 0a 20  nfo_col *pCol;. 
221ef 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
221f0 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70   !ExprHasAnyProp
221f1 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 54  erty(pExpr, EP_T
221f2 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64 75  okenOnly|EP_Redu
221f3 63 65 64 29 20 29 3b 0a 20 20 20 20 20 20 20 20  ced) );.        
221f4 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 54 61    if( pExpr->iTa
221f5 62 6c 65 3d 3d 70 49 74 65 6d 2d 3e 69 43 75 72  ble==pItem->iCur
221f6 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20  sor ){.         
221f7 20 20 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63     /* If we reac
221f8 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 69 74  h this point, it
221f9 20 6d 65 61 6e 73 20 74 68 61 74 20 70 45 78 70   means that pExp
221fa 72 20 72 65 66 65 72 73 20 74 6f 20 61 20 74 61  r refers to a ta
221fb 62 6c 65 0a 20 20 20 20 20 20 20 20 20 20 20 20  ble.            
221fc 2a 2a 20 74 68 61 74 20 69 73 20 69 6e 20 74 68  ** that is in th
221fd 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  e FROM clause of
221fe 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 71   the aggregate q
221ff 75 65 72 79 2e 20 20 0a 20 20 20 20 20 20 20 20  uery.  .        
22200 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 20      **.         
22201 20 20 20 2a 2a 20 4d 61 6b 65 20 61 6e 20 65 6e     ** Make an en
22202 74 72 79 20 66 6f 72 20 74 68 65 20 63 6f 6c 75  try for the colu
22203 6d 6e 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e  mn in pAggInfo->
22204 61 43 6f 6c 5b 5d 20 69 66 20 74 68 65 72 65 0a  aCol[] if there.
22205 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69              ** i
22206 73 20 6e 6f 74 20 61 6e 20 65 6e 74 72 79 20 74  s not an entry t
22207 68 65 72 65 20 61 6c 72 65 61 64 79 2e 0a 20 20  here already..  
22208 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
22209 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a           int k;.
2220a 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c              pCol
2220b 20 3d 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f   = pAggInfo->aCo
2220c 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 66  l;.            f
2220d 6f 72 28 6b 3d 30 3b 20 6b 3c 70 41 67 67 49 6e  or(k=0; k<pAggIn
2220e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b  fo->nColumn; k++
2220f 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20  , pCol++){.     
22210 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f           if( pCo
22211 6c 2d 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70 72  l->iTable==pExpr
22212 2d 3e 69 54 61 62 6c 65 20 26 26 0a 20 20 20 20  ->iTable &&.    
22213 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43                pC
22214 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78  ol->iColumn==pEx
22215 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20  pr->iColumn ){. 
22216 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62                 b
22217 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
22218 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
22219 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
2221a 69 66 28 20 28 6b 3e 3d 70 41 67 67 49 6e 66 6f  if( (k>=pAggInfo
2221b 2d 3e 6e 43 6f 6c 75 6d 6e 29 0a 20 20 20 20 20  ->nColumn).     
2221c 20 20 20 20 20 20 20 20 26 26 20 28 6b 20 3d 20          && (k = 
2221d 61 64 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e  addAggInfoColumn
2221e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 41 67  (pParse->db, pAg
2221f 67 49 6e 66 6f 29 29 3e 3d 30 20 0a 20 20 20 20  gInfo))>=0 .    
22220 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20          ){.     
22221 20 20 20 20 20 20 20 20 20 70 43 6f 6c 20 3d 20           pCol = 
22222 26 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b  &pAggInfo->aCol[
22223 6b 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  k];.            
22224 20 20 70 43 6f 6c 2d 3e 70 54 61 62 20 3d 20 70    pCol->pTab = p
22225 45 78 70 72 2d 3e 70 54 61 62 3b 0a 20 20 20 20  Expr->pTab;.    
22226 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e            pCol->
22227 69 54 61 62 6c 65 20 3d 20 70 45 78 70 72 2d 3e  iTable = pExpr->
22228 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 20 20  iTable;.        
22229 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 43 6f 6c        pCol->iCol
2222a 75 6d 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f  umn = pExpr->iCo
2222b 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 20 20  lumn;.          
2222c 20 20 20 20 70 43 6f 6c 2d 3e 69 4d 65 6d 20 3d      pCol->iMem =
2222d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2222e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
2222f 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75  Col->iSorterColu
22230 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20  mn = -1;.       
22231 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70 45 78         pCol->pEx
22232 70 72 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20  pr = pExpr;.    
22233 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 41            if( pA
22234 67 67 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79  ggInfo->pGroupBy
22235 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
22236 20 20 20 20 69 6e 74 20 6a 2c 20 6e 3b 0a 20 20      int j, n;.  
22237 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45 78                Ex
22238 70 72 4c 69 73 74 20 2a 70 47 42 20 3d 20 70 41  prList *pGB = pA
22239 67 67 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79  ggInfo->pGroupBy
2223a 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
2223b 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
2223c 74 5f 69 74 65 6d 20 2a 70 54 65 72 6d 20 3d 20  t_item *pTerm = 
2223d 70 47 42 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20  pGB->a;.        
2223e 20 20 20 20 20 20 20 20 6e 20 3d 20 70 47 42 2d          n = pGB-
2223f 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20  >nExpr;.        
22240 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b          for(j=0;
22241 20 6a 3c 6e 3b 20 6a 2b 2b 2c 20 70 54 65 72 6d   j<n; j++, pTerm
22242 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ++){.           
22243 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 20         Expr *pE 
22244 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a  = pTerm->pExpr;.
22245 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22246 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b    if( pE->op==TK
22247 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 45 2d 3e 69  _COLUMN && pE->i
22248 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d 3e 69 54  Table==pExpr->iT
22249 61 62 6c 65 20 26 26 0a 20 20 20 20 20 20 20 20  able &&.        
2224a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
2224b 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72  ->iColumn==pExpr
2224c 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->iColumn ){.   
2224d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2224e 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f   pCol->iSorterCo
2224f 6c 75 6d 6e 20 3d 20 6a 3b 0a 20 20 20 20 20 20  lumn = j;.      
22250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72                br
22251 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  eak;.           
22252 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
22253 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
22254 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
22255 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f           if( pCo
22256 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e  l->iSorterColumn
22257 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  <0 ){.          
22258 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72        pCol->iSor
22259 74 65 72 43 6f 6c 75 6d 6e 20 3d 20 70 41 67 67  terColumn = pAgg
2225a 49 6e 66 6f 2d 3e 6e 53 6f 72 74 69 6e 67 43 6f  Info->nSortingCo
2225b 6c 75 6d 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20  lumn++;.        
2225c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2225d 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
2225e 20 20 2f 2a 20 54 68 65 72 65 20 69 73 20 6e 6f    /* There is no
2225f 77 20 61 6e 20 65 6e 74 72 79 20 66 6f 72 20 70  w an entry for p
22260 45 78 70 72 20 69 6e 20 70 41 67 67 49 6e 66 6f  Expr in pAggInfo
22261 2d 3e 61 43 6f 6c 5b 5d 20 28 65 69 74 68 65 72  ->aCol[] (either
22262 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20  .            ** 
22263 62 65 63 61 75 73 65 20 69 74 20 77 61 73 20 74  because it was t
22264 68 65 72 65 20 62 65 66 6f 72 65 20 6f 72 20 62  here before or b
22265 65 63 61 75 73 65 20 77 65 20 6a 75 73 74 20 63  ecause we just c
22266 72 65 61 74 65 64 20 69 74 29 2e 0a 20 20 20 20  reated it)..    
22267 20 20 20 20 20 20 20 20 2a 2a 20 43 6f 6e 76 65          ** Conve
22268 72 74 20 74 68 65 20 70 45 78 70 72 20 74 6f 20  rt the pExpr to 
22269 62 65 20 61 20 54 4b 5f 41 47 47 5f 43 4f 4c 55  be a TK_AGG_COLU
2226a 4d 4e 20 72 65 66 65 72 72 69 6e 67 20 74 6f 20  MN referring to 
2226b 74 68 61 74 0a 20 20 20 20 20 20 20 20 20 20 20  that.           
2226c 20 2a 2a 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43   ** pAggInfo->aC
2226d 6f 6c 5b 5d 20 65 6e 74 72 79 2e 0a 20 20 20 20  ol[] entry..    
2226e 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
2226f 20 20 20 20 20 20 20 45 78 70 72 53 65 74 49 72         ExprSetIr
22270 72 65 64 75 63 69 62 6c 65 28 70 45 78 70 72 29  reducible(pExpr)
22271 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45  ;.            pE
22272 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f 20 3d 20  xpr->pAggInfo = 
22273 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20  pAggInfo;.      
22274 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20        pExpr->op 
22275 3d 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3b  = TK_AGG_COLUMN;
22276 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78  .            pEx
22277 70 72 2d 3e 69 41 67 67 20 3d 20 28 69 31 36 29  pr->iAgg = (i16)
22278 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 62  k;.            b
22279 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
2227a 7d 20 2f 2a 20 65 6e 64 69 66 20 70 45 78 70 72  } /* endif pExpr
2227b 2d 3e 69 54 61 62 6c 65 3d 3d 70 49 74 65 6d 2d  ->iTable==pItem-
2227c 3e 69 43 75 72 73 6f 72 20 2a 2f 0a 20 20 20 20  >iCursor */.    
2227d 20 20 20 20 7d 20 2f 2a 20 65 6e 64 20 6c 6f 6f      } /* end loo
2227e 70 20 6f 76 65 72 20 70 53 72 63 4c 69 73 74 20  p over pSrcList 
2227f 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  */.      }.     
22280 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e   return WRC_Prun
22281 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  e;.    }.    cas
22282 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  e TK_AGG_FUNCTIO
22283 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  N: {.      /* Th
22284 65 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d 30  e pNC->nDepth==0
22285 20 74 65 73 74 20 63 61 75 73 65 73 20 61 67 67   test causes agg
22286 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73  regate functions
22287 20 69 6e 20 73 75 62 71 75 65 72 69 65 73 0a 20   in subqueries. 
22288 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20 69 67       ** to be ig
22289 6e 6f 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 69  nored */.      i
2228a 66 28 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d 3d  f( pNC->nDepth==
2228b 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  0 ){.        /* 
2228c 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20  Check to see if 
2228d 70 45 78 70 72 20 69 73 20 61 20 64 75 70 6c 69  pExpr is a dupli
2228e 63 61 74 65 20 6f 66 20 61 6e 6f 74 68 65 72 20  cate of another 
2228f 61 67 67 72 65 67 61 74 65 20 0a 20 20 20 20 20  aggregate .     
22290 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74     ** function t
22291 68 61 74 20 69 73 20 61 6c 72 65 61 64 79 20 69  hat is already i
22292 6e 20 74 68 65 20 70 41 67 67 49 6e 66 6f 20 73  n the pAggInfo s
22293 74 72 75 63 74 75 72 65 0a 20 20 20 20 20 20 20  tructure.       
22294 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 74 72 75   */.        stru
22295 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20  ct AggInfo_func 
22296 2a 70 49 74 65 6d 20 3d 20 70 41 67 67 49 6e 66  *pItem = pAggInf
22297 6f 2d 3e 61 46 75 6e 63 3b 0a 20 20 20 20 20 20  o->aFunc;.      
22298 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 67    for(i=0; i<pAg
22299 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b  gInfo->nFunc; i+
2229a 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  +, pItem++){.   
2229b 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
2229c 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28 70 49  e3ExprCompare(pI
2229d 74 65 6d 2d 3e 70 45 78 70 72 2c 20 70 45 78 70  tem->pExpr, pExp
2229e 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  r) ){.          
2229f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
222a0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
222a1 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d 70 41         if( i>=pA
222a2 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 20 29 7b  ggInfo->nFunc ){
222a3 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 45  .          /* pE
222a4 78 70 72 20 69 73 20 6f 72 69 67 69 6e 61 6c 2e  xpr is original.
222a5 20 20 4d 61 6b 65 20 61 20 6e 65 77 20 65 6e 74    Make a new ent
222a6 72 79 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e  ry in pAggInfo->
222a7 61 46 75 6e 63 5b 5d 0a 20 20 20 20 20 20 20 20  aFunc[].        
222a8 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 75    */.          u
222a9 38 20 65 6e 63 20 3d 20 45 4e 43 28 70 50 61 72  8 enc = ENC(pPar
222aa 73 65 2d 3e 64 62 29 3b 0a 20 20 20 20 20 20 20  se->db);.       
222ab 20 20 20 69 20 3d 20 61 64 64 41 67 67 49 6e 66     i = addAggInf
222ac 6f 46 75 6e 63 28 70 50 61 72 73 65 2d 3e 64 62  oFunc(pParse->db
222ad 2c 20 70 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20  , pAggInfo);.   
222ae 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d 30 20         if( i>=0 
222af 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61  ){.            a
222b0 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 50  ssert( !ExprHasP
222b1 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45  roperty(pExpr, E
222b2 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a  P_xIsSelect) );.
222b3 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65              pIte
222b4 6d 20 3d 20 26 70 41 67 67 49 6e 66 6f 2d 3e 61  m = &pAggInfo->a
222b5 46 75 6e 63 5b 69 5d 3b 0a 20 20 20 20 20 20 20  Func[i];.       
222b6 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70       pItem->pExp
222b7 72 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20  r = pExpr;.     
222b8 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 4d         pItem->iM
222b9 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  em = ++pParse->n
222ba 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 20  Mem;.           
222bb 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61   assert( !ExprHa
222bc 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
222bd 20 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b   EP_IntValue) );
222be 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74  .            pIt
222bf 65 6d 2d 3e 70 46 75 6e 63 20 3d 20 73 71 6c 69  em->pFunc = sqli
222c0 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28  te3FindFunction(
222c1 70 50 61 72 73 65 2d 3e 64 62 2c 0a 20 20 20 20  pParse->db,.    
222c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
222c3 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 20  Expr->u.zToken, 
222c4 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
222c5 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 29  pExpr->u.zToken)
222c6 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
222c7 20 20 20 20 20 70 45 78 70 72 2d 3e 78 2e 70 4c       pExpr->x.pL
222c8 69 73 74 20 3f 20 70 45 78 70 72 2d 3e 78 2e 70  ist ? pExpr->x.p
222c9 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 2c  List->nExpr : 0,
222ca 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20   enc, 0);.      
222cb 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
222cc 3e 66 6c 61 67 73 20 26 20 45 50 5f 44 69 73 74  >flags & EP_Dist
222cd 69 6e 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20  inct ){.        
222ce 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 44 69        pItem->iDi
222cf 73 74 69 6e 63 74 20 3d 20 70 50 61 72 73 65 2d  stinct = pParse-
222d0 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20  >nTab++;.       
222d1 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
222d2 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d            pItem-
222d3 3e 69 44 69 73 74 69 6e 63 74 20 3d 20 2d 31 3b  >iDistinct = -1;
222d4 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
222d5 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
222d6 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20     }.        /* 
222d7 4d 61 6b 65 20 70 45 78 70 72 20 70 6f 69 6e 74  Make pExpr point
222d8 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69   to the appropri
222d9 61 74 65 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46  ate pAggInfo->aF
222da 75 6e 63 5b 5d 20 65 6e 74 72 79 0a 20 20 20 20  unc[] entry.    
222db 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 61      */.        a
222dc 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73 41  ssert( !ExprHasA
222dd 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78 70 72  nyProperty(pExpr
222de 2c 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45  , EP_TokenOnly|E
222df 50 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20 20  P_Reduced) );.  
222e0 20 20 20 20 20 20 45 78 70 72 53 65 74 49 72 72        ExprSetIrr
222e1 65 64 75 63 69 62 6c 65 28 70 45 78 70 72 29 3b  educible(pExpr);
222e2 0a 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e  .        pExpr->
222e3 69 41 67 67 20 3d 20 28 69 31 36 29 69 3b 0a 20  iAgg = (i16)i;. 
222e4 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 41         pExpr->pA
222e5 67 67 49 6e 66 6f 20 3d 20 70 41 67 67 49 6e 66  ggInfo = pAggInf
222e6 6f 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  o;.        retur
222e7 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20  n WRC_Prune;.   
222e8 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
222e9 20 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74   return WRC_Cont
222ea 69 6e 75 65 3b 0a 7d 0a 73 74 61 74 69 63 20 69  inue;.}.static i
222eb 6e 74 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67  nt analyzeAggreg
222ec 61 74 65 73 49 6e 53 65 6c 65 63 74 28 57 61 6c  atesInSelect(Wal
222ed 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65  ker *pWalker, Se
222ee 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a  lect *pSelect){.
222ef 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70    NameContext *p
222f0 4e 43 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e  NC = pWalker->u.
222f1 70 4e 43 3b 0a 20 20 69 66 28 20 70 4e 43 2d 3e  pNC;.  if( pNC->
222f2 6e 44 65 70 74 68 3d 3d 30 20 29 7b 0a 20 20 20  nDepth==0 ){.   
222f3 20 70 4e 43 2d 3e 6e 44 65 70 74 68 2b 2b 3b 0a   pNC->nDepth++;.
222f4 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53      sqlite3WalkS
222f5 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20 70  elect(pWalker, p
222f6 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 70 4e 43  Select);.    pNC
222f7 2d 3e 6e 44 65 70 74 68 2d 2d 3b 0a 20 20 20 20  ->nDepth--;.    
222f8 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65  return WRC_Prune
222f9 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
222fa 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e  eturn WRC_Contin
222fb 75 65 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ue;.  }.}../*.**
222fc 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 67 69 76   Analyze the giv
222fd 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 6f  en expression lo
222fe 6f 6b 69 6e 67 20 66 6f 72 20 61 67 67 72 65 67  oking for aggreg
222ff 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e  ate functions an
22300 64 0a 2a 2a 20 66 6f 72 20 76 61 72 69 61 62 6c  d.** for variabl
22301 65 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20  es that need to 
22302 62 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  be added to the 
22303 70 50 61 72 73 65 2d 3e 61 41 67 67 5b 5d 20 61  pParse->aAgg[] a
22304 72 72 61 79 2e 0a 2a 2a 20 4d 61 6b 65 20 61 64  rray..** Make ad
22305 64 69 74 69 6f 6e 61 6c 20 65 6e 74 72 69 65 73  ditional entries
22306 20 74 6f 20 74 68 65 20 70 50 61 72 73 65 2d 3e   to the pParse->
22307 61 41 67 67 5b 5d 20 61 72 72 61 79 20 61 73 20  aAgg[] array as 
22308 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a 2a  necessary..**.**
22309 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68   This routine sh
2230a 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20 63 61 6c  ould only be cal
2230b 6c 65 64 20 61 66 74 65 72 20 74 68 65 20 65 78  led after the ex
2230c 70 72 65 73 73 69 6f 6e 20 68 61 73 20 62 65 65  pression has bee
2230d 6e 0a 2a 2a 20 61 6e 61 6c 79 7a 65 64 20 62 79  n.** analyzed by
2230e 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
2230f 78 70 72 4e 61 6d 65 73 28 29 2e 0a 2a 2f 0a 53  xprNames()..*/.S
22310 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
22311 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e  id sqlite3ExprAn
22312 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 28  alyzeAggregates(
22313 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43  NameContext *pNC
22314 2c 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a  , Expr *pExpr){.
22315 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e    Walker w;.  w.
22316 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20  xExprCallback = 
22317 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65  analyzeAggregate
22318 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74 43 61 6c  ;.  w.xSelectCal
22319 6c 62 61 63 6b 20 3d 20 61 6e 61 6c 79 7a 65 41  lback = analyzeA
2231a 67 67 72 65 67 61 74 65 73 49 6e 53 65 6c 65 63  ggregatesInSelec
2231b 74 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70  t;.  w.u.pNC = p
2231c 4e 43 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e  NC;.  assert( pN
2231d 43 2d 3e 70 53 72 63 4c 69 73 74 21 3d 30 20 29  C->pSrcList!=0 )
2231e 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 45  ;.  sqlite3WalkE
2231f 78 70 72 28 26 77 2c 20 70 45 78 70 72 29 3b 0a  xpr(&w, pExpr);.
22320 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71  }../*.** Call sq
22321 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65  lite3ExprAnalyze
22322 41 67 67 72 65 67 61 74 65 73 28 29 20 66 6f 72  Aggregates() for
22323 20 65 76 65 72 79 20 65 78 70 72 65 73 73 69 6f   every expressio
22324 6e 20 69 6e 20 61 6e 0a 2a 2a 20 65 78 70 72 65  n in an.** expre
22325 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20 52 65 74  ssion list.  Ret
22326 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
22327 66 20 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20  f errors..**.** 
22328 49 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 66  If an error is f
22329 6f 75 6e 64 2c 20 74 68 65 20 61 6e 61 6c 79 73  ound, the analys
2232a 69 73 20 69 73 20 63 75 74 20 73 68 6f 72 74 2e  is is cut short.
2232b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2232c 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
2232d 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73  xprAnalyzeAggLis
2232e 74 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70  t(NameContext *p
2232f 4e 43 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  NC, ExprList *pL
22330 69 73 74 29 7b 0a 20 20 73 74 72 75 63 74 20 45  ist){.  struct E
22331 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  xprList_item *pI
22332 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  tem;.  int i;.  
22333 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20 20 20  if( pList ){.   
22334 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74   for(pItem=pList
22335 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73  ->a, i=0; i<pLis
22336 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70  t->nExpr; i++, p
22337 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 73  Item++){.      s
22338 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a  qlite3ExprAnalyz
22339 65 41 67 67 72 65 67 61 74 65 73 28 70 4e 43 2c  eAggregates(pNC,
2233a 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29 3b 0a   pItem->pExpr);.
2233b 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
2233c 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 73 69  ** Allocate a si
2233d 6e 67 6c 65 20 6e 65 77 20 72 65 67 69 73 74 65  ngle new registe
2233e 72 20 66 6f 72 20 75 73 65 20 74 6f 20 68 6f 6c  r for use to hol
2233f 64 20 73 6f 6d 65 20 69 6e 74 65 72 6d 65 64 69  d some intermedi
22340 61 74 65 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a 53  ate result..*/.S
22341 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
22342 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  t sqlite3GetTemp
22343 52 65 67 28 50 61 72 73 65 20 2a 70 50 61 72 73  Reg(Parse *pPars
22344 65 29 7b 0a 20 20 69 66 28 20 70 50 61 72 73 65  e){.  if( pParse
22345 2d 3e 6e 54 65 6d 70 52 65 67 3d 3d 30 20 29 7b  ->nTempReg==0 ){
22346 0a 20 20 20 20 72 65 74 75 72 6e 20 2b 2b 70 50  .    return ++pP
22347 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 7d 0a  arse->nMem;.  }.
22348 20 20 72 65 74 75 72 6e 20 70 50 61 72 73 65 2d    return pParse-
22349 3e 61 54 65 6d 70 52 65 67 5b 2d 2d 70 50 61 72  >aTempReg[--pPar
2234a 73 65 2d 3e 6e 54 65 6d 70 52 65 67 5d 3b 0a 7d  se->nTempReg];.}
2234b 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f 63 61  ../*.** Dealloca
2234c 74 65 20 61 20 72 65 67 69 73 74 65 72 2c 20 6d  te a register, m
2234d 61 6b 69 6e 67 20 61 76 61 69 6c 61 62 6c 65 20  aking available 
2234e 66 6f 72 20 72 65 75 73 65 20 66 6f 72 20 73 6f  for reuse for so
2234f 6d 65 20 6f 74 68 65 72 0a 2a 2a 20 70 75 72 70  me other.** purp
22350 6f 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20  ose..**.** If a 
22351 72 65 67 69 73 74 65 72 20 69 73 20 63 75 72 72  register is curr
22352 65 6e 74 6c 79 20 62 65 69 6e 67 20 75 73 65 64  ently being used
22353 20 62 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63   by the column c
22354 61 63 68 65 2c 20 74 68 65 6e 0a 2a 2a 20 74 68  ache, then.** th
22355 65 20 64 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73  e dallocation is
22356 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20   deferred until 
22357 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65  the column cache
22358 20 6c 69 6e 65 20 74 68 61 74 20 75 73 65 73 0a   line that uses.
22359 2a 2a 20 74 68 65 20 72 65 67 69 73 74 65 72 20  ** the register 
2235a 62 65 63 6f 6d 65 73 20 73 74 61 6c 65 2e 0a 2a  becomes stale..*
2235b 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2235c 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 6c   void sqlite3Rel
2235d 65 61 73 65 54 65 6d 70 52 65 67 28 50 61 72 73  easeTempReg(Pars
2235e 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
2235f 52 65 67 29 7b 0a 20 20 69 66 28 20 69 52 65 67  Reg){.  if( iReg
22360 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 54 65 6d   && pParse->nTem
22361 70 52 65 67 3c 41 72 72 61 79 53 69 7a 65 28 70  pReg<ArraySize(p
22362 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 29  Parse->aTempReg)
22363 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
22364 20 20 20 73 74 72 75 63 74 20 79 43 6f 6c 43 61     struct yColCa
22365 63 68 65 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28  che *p;.    for(
22366 69 3d 30 2c 20 70 3d 70 50 61 72 73 65 2d 3e 61  i=0, p=pParse->a
22367 43 6f 6c 43 61 63 68 65 3b 20 69 3c 53 51 4c 49  ColCache; i<SQLI
22368 54 45 5f 4e 5f 43 4f 4c 43 41 43 48 45 3b 20 69  TE_N_COLCACHE; i
22369 2b 2b 2c 20 70 2b 2b 29 7b 0a 20 20 20 20 20 20  ++, p++){.      
2236a 69 66 28 20 70 2d 3e 69 52 65 67 3d 3d 69 52 65  if( p->iReg==iRe
2236b 67 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  g ){.        p->
2236c 74 65 6d 70 52 65 67 20 3d 20 31 3b 0a 20 20 20  tempReg = 1;.   
2236d 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
2236e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70     }.    }.    p
2236f 50 61 72 73 65 2d 3e 61 54 65 6d 70 52 65 67 5b  Parse->aTempReg[
22370 70 50 61 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67  pParse->nTempReg
22371 2b 2b 5d 20 3d 20 69 52 65 67 3b 0a 20 20 7d 0a  ++] = iReg;.  }.
22372 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  }../*.** Allocat
22373 65 20 6f 72 20 64 65 61 6c 6c 6f 63 61 74 65 20  e or deallocate 
22374 61 20 62 6c 6f 63 6b 20 6f 66 20 6e 52 65 67 20  a block of nReg 
22375 63 6f 6e 73 65 63 75 74 69 76 65 20 72 65 67 69  consecutive regi
22376 73 74 65 72 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f  sters.*/.SQLITE_
22377 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
22378 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28  te3GetTempRange(
22379 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
2237a 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20  nt nReg){.  int 
2237b 69 2c 20 6e 3b 0a 20 20 69 20 3d 20 70 50 61 72  i, n;.  i = pPar
2237c 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 3b 0a 20  se->iRangeReg;. 
2237d 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e 6e 52 61   n = pParse->nRa
2237e 6e 67 65 52 65 67 3b 0a 20 20 69 66 28 20 6e 52  ngeReg;.  if( nR
2237f 65 67 3c 3d 6e 20 26 26 20 21 75 73 65 64 41 73  eg<=n && !usedAs
22380 43 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61 72  ColumnCache(pPar
22381 73 65 2c 20 69 2c 20 69 2b 6e 2d 31 29 20 29 7b  se, i, i+n-1) ){
22382 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 52 61  .    pParse->iRa
22383 6e 67 65 52 65 67 20 2b 3d 20 6e 52 65 67 3b 0a  ngeReg += nReg;.
22384 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 52 61 6e      pParse->nRan
22385 67 65 52 65 67 20 2d 3d 20 6e 52 65 67 3b 0a 20  geReg -= nReg;. 
22386 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 20 3d 20   }else{.    i = 
22387 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a  pParse->nMem+1;.
22388 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d      pParse->nMem
22389 20 2b 3d 20 6e 52 65 67 3b 0a 20 20 7d 0a 20 20   += nReg;.  }.  
2238a 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 53 51 4c 49  return i;.}.SQLI
2238b 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2238c 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2238d 6d 70 52 61 6e 67 65 28 50 61 72 73 65 20 2a 70  mpRange(Parse *p
2238e 50 61 72 73 65 2c 20 69 6e 74 20 69 52 65 67 2c  Parse, int iReg,
2238f 20 69 6e 74 20 6e 52 65 67 29 7b 0a 20 20 69 66   int nReg){.  if
22390 28 20 6e 52 65 67 3e 70 50 61 72 73 65 2d 3e 6e  ( nReg>pParse->n
22391 52 61 6e 67 65 52 65 67 20 29 7b 0a 20 20 20 20  RangeReg ){.    
22392 70 50 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65  pParse->nRangeRe
22393 67 20 3d 20 6e 52 65 67 3b 0a 20 20 20 20 70 50  g = nReg;.    pP
22394 61 72 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 20  arse->iRangeReg 
22395 3d 20 69 52 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f  = iReg;.  }.}../
22396 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
22397 6e 64 20 6f 66 20 65 78 70 72 2e 63 20 2a 2a 2a  nd of expr.c ***
22398 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22399 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2239a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2239b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
2239c 65 67 69 6e 20 66 69 6c 65 20 61 6c 74 65 72 2e  egin file alter.
2239d 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2239e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2239f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
223a0 2a 0a 2a 2a 20 32 30 30 35 20 46 65 62 72 75 61  *.** 2005 Februa
223a1 72 79 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ry 15.**.** The 
223a2 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
223a3 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
223a4 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
223a5 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
223a6 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
223a7 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
223a8 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
223a9 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
223aa 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
223ab 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
223ac 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
223ad 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
223ae 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
223af 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
223b0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
223b1 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
223b2 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
223b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
223b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
223b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
223b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
223b7 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
223b8 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64  e contains C cod
223b9 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20  e routines that 
223ba 75 73 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65  used to generate
223bb 20 56 44 42 45 20 63 6f 64 65 0a 2a 2a 20 74 68   VDBE code.** th
223bc 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  at implements th
223bd 65 20 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f  e ALTER TABLE co
223be 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  mmand..**.** $Id
223bf 3a 20 61 6c 74 65 72 2e 63 2c 76 20 31 2e 36 32  : alter.c,v 1.62
223c0 20 32 30 30 39 2f 30 37 2f 32 34 20 31 37 3a 35   2009/07/24 17:5
223c1 38 3a 35 33 20 64 61 6e 69 65 6c 6b 31 39 37 37  8:53 danielk1977
223c2 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
223c3 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
223c4 73 20 66 69 6c 65 20 6f 6e 6c 79 20 65 78 69 73  s file only exis
223c5 74 73 20 69 66 20 77 65 20 61 72 65 20 6e 6f 74  ts if we are not
223c6 20 6f 6d 69 74 74 69 6e 67 20 74 68 65 0a 2a 2a   omitting the.**
223c7 20 41 4c 54 45 52 20 54 41 42 4c 45 20 6c 6f 67   ALTER TABLE log
223c8 69 63 20 66 72 6f 6d 20 74 68 65 20 62 75 69 6c  ic from the buil
223c9 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  d..*/.#ifndef SQ
223ca 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54  LITE_OMIT_ALTERT
223cb 41 42 4c 45 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ABLE.../*.** Thi
223cc 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  s function is us
223cd 65 64 20 62 79 20 53 51 4c 20 67 65 6e 65 72 61  ed by SQL genera
223ce 74 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ted to implement
223cf 20 74 68 65 20 0a 2a 2a 20 41 4c 54 45 52 20 54   the .** ALTER T
223d0 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 20 54 68  ABLE command. Th
223d1 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
223d2 20 69 73 20 74 68 65 20 74 65 78 74 20 6f 66 20   is the text of 
223d3 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f  a CREATE TABLE o
223d4 72 0a 2a 2a 20 43 52 45 41 54 45 20 49 4e 44 45  r.** CREATE INDE
223d5 58 20 63 6f 6d 6d 61 6e 64 2e 20 54 68 65 20 73  X command. The s
223d6 65 63 6f 6e 64 20 69 73 20 61 20 74 61 62 6c 65  econd is a table
223d7 20 6e 61 6d 65 2e 20 54 68 65 20 74 61 62 6c 65   name. The table
223d8 20 6e 61 6d 65 20 69 6e 20 0a 2a 2a 20 74 68 65   name in .** the
223d9 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72   CREATE TABLE or
223da 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 74   CREATE INDEX st
223db 61 74 65 6d 65 6e 74 20 69 73 20 72 65 70 6c 61  atement is repla
223dc 63 65 64 20 77 69 74 68 20 74 68 65 20 74 68 69  ced with the thi
223dd 72 64 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 61  rd.** argument a
223de 6e 64 20 74 68 65 20 72 65 73 75 6c 74 20 72 65  nd the result re
223df 74 75 72 6e 65 64 2e 20 45 78 61 6d 70 6c 65 73  turned. Examples
223e0 3a 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 5f 72  :.**.** sqlite_r
223e1 65 6e 61 6d 65 5f 74 61 62 6c 65 28 27 43 52 45  ename_table('CRE
223e2 41 54 45 20 54 41 42 4c 45 20 61 62 63 28 61 2c  ATE TABLE abc(a,
223e3 20 62 2c 20 63 29 27 2c 20 27 64 65 66 27 29 0a   b, c)', 'def').
223e4 2a 2a 20 20 20 20 20 2d 3e 20 27 43 52 45 41 54  **     -> 'CREAT
223e5 45 20 54 41 42 4c 45 20 64 65 66 28 61 2c 20 62  E TABLE def(a, b
223e6 2c 20 63 29 27 0a 2a 2a 0a 2a 2a 20 73 71 6c 69  , c)'.**.** sqli
223e7 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28  te_rename_table(
223e8 27 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 20  'CREATE INDEX i 
223e9 4f 4e 20 61 62 63 28 61 29 27 2c 20 27 64 65 66  ON abc(a)', 'def
223ea 27 29 0a 2a 2a 20 20 20 20 20 2d 3e 20 27 43 52  ').**     -> 'CR
223eb 45 41 54 45 20 49 4e 44 45 58 20 69 20 4f 4e 20  EATE INDEX i ON 
223ec 64 65 66 28 61 2c 20 62 2c 20 63 29 27 0a 2a 2f  def(a, b, c)'.*/
223ed 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6e  .static void ren
223ee 61 6d 65 54 61 62 6c 65 46 75 6e 63 28 0a 20 20  ameTableFunc(.  
223ef 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
223f0 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
223f1 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74  NotUsed,.  sqlit
223f2 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a  e3_value **argv.
223f3 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ){.  unsigned ch
223f4 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71 6c 20 3d  ar const *zSql =
223f5 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
223f6 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
223f7 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f  unsigned char co
223f8 6e 73 74 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 20  nst *zTableName 
223f9 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
223fa 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 0a  text(argv[1]);..
223fb 20 20 69 6e 74 20 74 6f 6b 65 6e 3b 0a 20 20 54    int token;.  T
223fc 6f 6b 65 6e 20 74 6e 61 6d 65 3b 0a 20 20 75 6e  oken tname;.  un
223fd 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73  signed char cons
223fe 74 20 2a 7a 43 73 72 20 3d 20 7a 53 71 6c 3b 0a  t *zCsr = zSql;.
223ff 20 20 69 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20    int len = 0;. 
22400 20 63 68 61 72 20 2a 7a 52 65 74 3b 0a 0a 20 20   char *zRet;..  
22401 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71  sqlite3 *db = sq
22402 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62  lite3_context_db
22403 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29  _handle(context)
22404 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ;..  UNUSED_PARA
22405 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
22406 0a 20 20 2f 2a 20 54 68 65 20 70 72 69 6e 63 69  .  /* The princi
22407 70 6c 65 20 75 73 65 64 20 74 6f 20 6c 6f 63 61  ple used to loca
22408 74 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d  te the table nam
22409 65 20 69 6e 20 74 68 65 20 43 52 45 41 54 45 20  e in the CREATE 
2240a 54 41 42 4c 45 20 0a 20 20 2a 2a 20 73 74 61 74  TABLE .  ** stat
2240b 65 6d 65 6e 74 20 69 73 20 74 68 61 74 20 74 68  ement is that th
2240c 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20  e table name is 
2240d 74 68 65 20 66 69 72 73 74 20 6e 6f 6e 2d 73 70  the first non-sp
2240e 61 63 65 20 74 6f 6b 65 6e 20 74 68 61 74 0a 20  ace token that. 
2240f 20 2a 2a 20 69 73 20 69 6d 6d 65 64 69 61 74 65   ** is immediate
22410 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61  ly followed by a
22411 20 54 4b 5f 4c 50 20 6f 72 20 54 4b 5f 55 53 49   TK_LP or TK_USI
22412 4e 47 20 74 6f 6b 65 6e 2e 0a 20 20 2a 2f 0a 20  NG token..  */. 
22413 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20   if( zSql ){.   
22414 20 64 6f 20 7b 0a 20 20 20 20 20 20 69 66 28 20   do {.      if( 
22415 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20 20 20 20  !*zCsr ){.      
22416 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20 6f 66 20    /* Ran out of 
22417 69 6e 70 75 74 20 62 65 66 6f 72 65 20 66 69 6e  input before fin
22418 64 69 6e 67 20 61 6e 20 6f 70 65 6e 69 6e 67 20  ding an opening 
22419 62 72 61 63 6b 65 74 2e 20 52 65 74 75 72 6e 20  bracket. Return 
2241a 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20 20  NULL. */.       
2241b 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
2241c 0a 0a 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65  ..      /* Store
2241d 20 74 68 65 20 74 6f 6b 65 6e 20 74 68 61 74 20   the token that 
2241e 7a 43 73 72 20 70 6f 69 6e 74 73 20 74 6f 20 69  zCsr points to i
2241f 6e 20 74 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20  n tname. */.    
22420 20 20 74 6e 61 6d 65 2e 7a 20 3d 20 28 63 68 61    tname.z = (cha
22421 72 2a 29 7a 43 73 72 3b 0a 20 20 20 20 20 20 74  r*)zCsr;.      t
22422 6e 61 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a 20  name.n = len;.. 
22423 20 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20       /* Advance 
22424 7a 43 73 72 20 74 6f 20 74 68 65 20 6e 65 78 74  zCsr to the next
22425 20 74 6f 6b 65 6e 2e 20 53 74 6f 72 65 20 74 68   token. Store th
22426 61 74 20 74 6f 6b 65 6e 20 74 79 70 65 20 69 6e  at token type in
22427 20 27 74 6f 6b 65 6e 27 2c 0a 20 20 20 20 20 20   'token',.      
22428 2a 2a 20 61 6e 64 20 69 74 73 20 6c 65 6e 67 74  ** and its lengt
22429 68 20 69 6e 20 27 6c 65 6e 27 20 28 74 6f 20 62  h in 'len' (to b
2242a 65 20 75 73 65 64 20 6e 65 78 74 20 69 74 65 72  e used next iter
2242b 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6c 6f  ation of this lo
2242c 6f 70 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  op)..      */.  
2242d 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20      do {.       
2242e 20 7a 43 73 72 20 2b 3d 20 6c 65 6e 3b 0a 20 20   zCsr += len;.  
2242f 20 20 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69        len = sqli
22430 74 65 33 47 65 74 54 6f 6b 65 6e 28 7a 43 73 72  te3GetToken(zCsr
22431 2c 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20  , &token);.     
22432 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b 65 6e 3d   } while( token=
22433 3d 54 4b 5f 53 50 41 43 45 20 29 3b 0a 20 20 20  =TK_SPACE );.   
22434 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3e 30     assert( len>0
22435 20 29 3b 0a 20 20 20 20 7d 20 77 68 69 6c 65 28   );.    } while(
22436 20 74 6f 6b 65 6e 21 3d 54 4b 5f 4c 50 20 26 26   token!=TK_LP &&
22437 20 74 6f 6b 65 6e 21 3d 54 4b 5f 55 53 49 4e 47   token!=TK_USING
22438 20 29 3b 0a 0a 20 20 20 20 7a 52 65 74 20 3d 20   );..    zRet = 
22439 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
2243a 62 2c 20 22 25 2e 2a 73 5c 22 25 77 5c 22 25 73  b, "%.*s\"%w\"%s
2243b 22 2c 20 28 28 75 38 2a 29 74 6e 61 6d 65 2e 7a  ", ((u8*)tname.z
2243c 29 20 2d 20 7a 53 71 6c 2c 20 7a 53 71 6c 2c 20  ) - zSql, zSql, 
2243d 0a 20 20 20 20 20 20 20 7a 54 61 62 6c 65 4e 61  .       zTableNa
2243e 6d 65 2c 20 74 6e 61 6d 65 2e 7a 2b 74 6e 61 6d  me, tname.z+tnam
2243f 65 2e 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  e.n);.    sqlite
22440 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
22441 6e 74 65 78 74 2c 20 7a 52 65 74 2c 20 2d 31 2c  ntext, zRet, -1,
22442 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29   SQLITE_DYNAMIC)
22443 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
22444 68 69 73 20 43 20 66 75 6e 63 74 69 6f 6e 20 69  his C function i
22445 6d 70 6c 65 6d 65 6e 74 73 20 61 6e 20 53 51 4c  mplements an SQL
22446 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 74   user function t
22447 68 61 74 20 69 73 20 75 73 65 64 20 62 79 20 53  hat is used by S
22448 51 4c 20 63 6f 64 65 0a 2a 2a 20 67 65 6e 65 72  QL code.** gener
22449 61 74 65 64 20 62 79 20 74 68 65 20 41 4c 54 45  ated by the ALTE
2244a 52 20 54 41 42 4c 45 20 2e 2e 2e 20 52 45 4e 41  R TABLE ... RENA
2244b 4d 45 20 63 6f 6d 6d 61 6e 64 20 74 6f 20 6d 6f  ME command to mo
2244c 64 69 66 79 20 74 68 65 20 64 65 66 69 6e 69 74  dify the definit
2244d 69 6f 6e 0a 2a 2a 20 6f 66 20 61 6e 79 20 66 6f  ion.** of any fo
2244e 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72  reign key constr
2244f 61 69 6e 74 73 20 74 68 61 74 20 75 73 65 20 74  aints that use t
22450 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 72  he table being r
22451 65 6e 61 6d 65 64 20 61 73 20 74 68 65 20 0a 2a  enamed as the .*
22452 2a 20 70 61 72 65 6e 74 20 74 61 62 6c 65 2e 20  * parent table. 
22453 49 74 20 69 73 20 70 61 73 73 65 64 20 74 68 72  It is passed thr
22454 65 65 20 61 72 67 75 6d 65 6e 74 73 3a 0a 2a 2a  ee arguments:.**
22455 0a 2a 2a 20 20 20 31 29 20 54 68 65 20 63 6f 6d  .**   1) The com
22456 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68  plete text of th
22457 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  e CREATE TABLE s
22458 74 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 6d  tatement being m
22459 6f 64 69 66 69 65 64 2c 0a 2a 2a 20 20 20 32 29  odified,.**   2)
2245a 20 54 68 65 20 6f 6c 64 20 6e 61 6d 65 20 6f 66   The old name of
2245b 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
2245c 20 72 65 6e 61 6d 65 64 2c 20 61 6e 64 0a 2a 2a   renamed, and.**
2245d 20 20 20 33 29 20 54 68 65 20 6e 65 77 20 6e 61     3) The new na
2245e 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
2245f 62 65 69 6e 67 20 72 65 6e 61 6d 65 64 2e 0a 2a  being renamed..*
22460 2a 0a 2a 2a 20 49 74 20 72 65 74 75 72 6e 73 20  *.** It returns 
22461 74 68 65 20 6e 65 77 20 43 52 45 41 54 45 20 54  the new CREATE T
22462 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 20  ABLE statement. 
22463 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a  For example:.**.
22464 2a 2a 20 20 20 73 71 6c 69 74 65 5f 72 65 6e 61  **   sqlite_rena
22465 6d 65 5f 70 61 72 65 6e 74 28 27 43 52 45 41 54  me_parent('CREAT
22466 45 20 54 41 42 4c 45 20 74 31 28 61 20 52 45 46  E TABLE t1(a REF
22467 45 52 45 4e 43 45 53 20 74 32 29 27 2c 20 27 74  ERENCES t2)', 't
22468 32 27 2c 20 27 74 33 27 29 0a 2a 2a 20 20 20 20  2', 't3').**    
22469 20 20 20 2d 3e 20 27 43 52 45 41 54 45 20 54 41     -> 'CREATE TA
2246a 42 4c 45 20 74 31 28 61 20 52 45 46 45 52 45 4e  BLE t1(a REFEREN
2246b 43 45 53 20 74 33 29 27 0a 2a 2f 0a 23 69 66 6e  CES t3)'.*/.#ifn
2246c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2246d 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 73 74 61 74  FOREIGN_KEY.stat
2246e 69 63 20 76 6f 69 64 20 72 65 6e 61 6d 65 50 61  ic void renamePa
2246f 72 65 6e 74 46 75 6e 63 28 0a 20 20 73 71 6c 69  rentFunc(.  sqli
22470 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
22471 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55  text,.  int NotU
22472 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  sed,.  sqlite3_v
22473 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
22474 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
22475 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
22476 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
22477 29 3b 0a 20 20 63 68 61 72 20 2a 7a 4f 75 74 70  );.  char *zOutp
22478 75 74 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a  ut = 0;.  char *
22479 7a 52 65 73 75 6c 74 3b 0a 20 20 75 6e 73 69 67  zResult;.  unsig
2247a 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ned char const *
2247b 7a 49 6e 70 75 74 20 3d 20 73 71 6c 69 74 65 33  zInput = sqlite3
2247c 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
2247d 5b 30 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65 64  [0]);.  unsigned
2247e 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4f 6c   char const *zOl
2247f 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  d = sqlite3_valu
22480 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b  e_text(argv[1]);
22481 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
22482 20 63 6f 6e 73 74 20 2a 7a 4e 65 77 20 3d 20 73   const *zNew = s
22483 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
22484 74 28 61 72 67 76 5b 32 5d 29 3b 0a 0a 20 20 75  t(argv[2]);..  u
22485 6e 73 69 67 6e 65 64 20 63 6f 6e 73 74 20 63 68  nsigned const ch
22486 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 2f  ar *z;         /
22487 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 6f 6b  * Pointer to tok
22488 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20  en */.  int n;  
22489 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2248a 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
2248b 68 20 6f 66 20 74 6f 6b 65 6e 20 7a 20 2a 2f 0a  h of token z */.
2248c 20 20 69 6e 74 20 74 6f 6b 65 6e 3b 20 20 20 20    int token;    
2248d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2248e 20 20 2f 2a 20 54 79 70 65 20 6f 66 20 74 6f 6b    /* Type of tok
2248f 65 6e 20 2a 2f 0a 0a 20 20 55 4e 55 53 45 44 5f  en */..  UNUSED_
22490 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
22491 64 29 3b 0a 20 20 66 6f 72 28 7a 3d 7a 49 6e 70  d);.  for(z=zInp
22492 75 74 3b 20 2a 7a 3b 20 7a 3d 7a 2b 6e 29 7b 0a  ut; *z; z=z+n){.
22493 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 47      n = sqlite3G
22494 65 74 54 6f 6b 65 6e 28 7a 2c 20 26 74 6f 6b 65  etToken(z, &toke
22495 6e 29 3b 0a 20 20 20 20 69 66 28 20 74 6f 6b 65  n);.    if( toke
22496 6e 3d 3d 54 4b 5f 52 45 46 45 52 45 4e 43 45 53  n==TK_REFERENCES
22497 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a   ){.      char *
22498 7a 50 61 72 65 6e 74 3b 0a 20 20 20 20 20 20 64  zParent;.      d
22499 6f 20 7b 0a 20 20 20 20 20 20 20 20 7a 20 2b 3d  o {.        z +=
2249a 20 6e 3b 0a 20 20 20 20 20 20 20 20 6e 20 3d 20   n;.        n = 
2249b 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65 6e 28  sqlite3GetToken(
2249c 7a 2c 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20 20  z, &token);.    
2249d 20 20 7d 77 68 69 6c 65 28 20 74 6f 6b 65 6e 3d    }while( token=
2249e 3d 54 4b 5f 53 50 41 43 45 20 29 3b 0a 0a 20 20  =TK_SPACE );..  
2249f 20 20 20 20 7a 50 61 72 65 6e 74 20 3d 20 73 71      zParent = sq
224a0 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64  lite3DbStrNDup(d
224a1 62 2c 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  b, (const char *
224a2 29 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69 66  )z, n);.      if
224a3 28 20 7a 50 61 72 65 6e 74 3d 3d 30 20 29 20 62  ( zParent==0 ) b
224a4 72 65 61 6b 3b 0a 20 20 20 20 20 20 73 71 6c 69  reak;.      sqli
224a5 74 65 33 44 65 71 75 6f 74 65 28 7a 50 61 72 65  te3Dequote(zPare
224a6 6e 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 30  nt);.      if( 0
224a7 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ==sqlite3StrICmp
224a8 28 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 7a  ((const char *)z
224a9 4f 6c 64 2c 20 7a 50 61 72 65 6e 74 29 20 29 7b  Old, zParent) ){
224aa 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a  .        char *z
224ab 4f 75 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Out = sqlite3MPr
224ac 69 6e 74 66 28 64 62 2c 20 22 25 73 25 2e 2a 73  intf(db, "%s%.*s
224ad 5c 22 25 77 5c 22 22 2c 20 0a 20 20 20 20 20 20  \"%w\"", .      
224ae 20 20 20 20 20 20 28 7a 4f 75 74 70 75 74 3f 7a        (zOutput?z
224af 4f 75 74 70 75 74 3a 22 22 29 2c 20 7a 2d 7a 49  Output:""), z-zI
224b0 6e 70 75 74 2c 20 7a 49 6e 70 75 74 2c 20 28 63  nput, zInput, (c
224b1 6f 6e 73 74 20 63 68 61 72 20 2a 29 7a 4e 65 77  onst char *)zNew
224b2 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20  .        );.    
224b3 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
224b4 65 28 64 62 2c 20 7a 4f 75 74 70 75 74 29 3b 0a  e(db, zOutput);.
224b5 20 20 20 20 20 20 20 20 7a 4f 75 74 70 75 74 20          zOutput 
224b6 3d 20 7a 4f 75 74 3b 0a 20 20 20 20 20 20 20 20  = zOut;.        
224b7 7a 49 6e 70 75 74 20 3d 20 26 7a 5b 6e 5d 3b 0a  zInput = &z[n];.
224b8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
224b9 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
224ba 7a 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d 0a  zParent);.    }.
224bb 20 20 7d 0a 0a 20 20 7a 52 65 73 75 6c 74 20 3d    }..  zResult =
224bc 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
224bd 64 62 2c 20 22 25 73 25 73 22 2c 20 28 7a 4f 75  db, "%s%s", (zOu
224be 74 70 75 74 3f 7a 4f 75 74 70 75 74 3a 22 22 29  tput?zOutput:"")
224bf 2c 20 7a 49 6e 70 75 74 29 2c 20 0a 20 20 73 71  , zInput), .  sq
224c0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
224c1 74 28 63 6f 6e 74 65 78 74 2c 20 7a 52 65 73 75  t(context, zResu
224c2 6c 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 44  lt, -1, SQLITE_D
224c3 59 4e 41 4d 49 43 29 3b 0a 20 20 73 71 6c 69 74  YNAMIC);.  sqlit
224c4 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4f 75  e3DbFree(db, zOu
224c5 74 70 75 74 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  tput);.}.#endif.
224c6 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
224c7 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 2f 2a 20  OMIT_TRIGGER./* 
224c8 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
224c9 20 75 73 65 64 20 62 79 20 53 51 4c 20 67 65 6e   used by SQL gen
224ca 65 72 61 74 65 64 20 74 6f 20 69 6d 70 6c 65 6d  erated to implem
224cb 65 6e 74 20 74 68 65 0a 2a 2a 20 41 4c 54 45 52  ent the.** ALTER
224cc 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 20   TABLE command. 
224cd 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  The first argume
224ce 6e 74 20 69 73 20 74 68 65 20 74 65 78 74 20 6f  nt is the text o
224cf 66 20 61 20 43 52 45 41 54 45 20 54 52 49 47 47  f a CREATE TRIGG
224d0 45 52 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  ER .** statement
224d1 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69 73 20  . The second is 
224d2 61 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68  a table name. Th
224d3 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20  e table name in 
224d4 74 68 65 20 43 52 45 41 54 45 20 0a 2a 2a 20 54  the CREATE .** T
224d5 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74  RIGGER statement
224d6 20 69 73 20 72 65 70 6c 61 63 65 64 20 77 69 74   is replaced wit
224d7 68 20 74 68 65 20 74 68 69 72 64 20 61 72 67 75  h the third argu
224d8 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 72 65 73  ment and the res
224d9 75 6c 74 20 0a 2a 2a 20 72 65 74 75 72 6e 65 64  ult .** returned
224da 2e 20 54 68 69 73 20 69 73 20 61 6e 61 6c 61 67  . This is analag
224db 6f 75 73 20 74 6f 20 72 65 6e 61 6d 65 54 61 62  ous to renameTab
224dc 6c 65 46 75 6e 63 28 29 20 61 62 6f 76 65 2c 20  leFunc() above, 
224dd 65 78 63 65 70 74 20 66 6f 72 20 43 52 45 41 54  except for CREAT
224de 45 0a 2a 2a 20 54 52 49 47 47 45 52 2c 20 6e 6f  E.** TRIGGER, no
224df 74 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 61  t CREATE INDEX a
224e0 6e 64 20 43 52 45 41 54 45 20 54 41 42 4c 45 2e  nd CREATE TABLE.
224e1 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
224e2 72 65 6e 61 6d 65 54 72 69 67 67 65 72 46 75 6e  renameTriggerFun
224e3 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
224e4 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
224e5 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20   int NotUsed,.  
224e6 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
224e7 61 72 67 76 0a 29 7b 0a 20 20 75 6e 73 69 67 6e  argv.){.  unsign
224e8 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  ed char const *z
224e9 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  Sql = sqlite3_va
224ea 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
224eb 29 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  );.  unsigned ch
224ec 61 72 20 63 6f 6e 73 74 20 2a 7a 54 61 62 6c 65  ar const *zTable
224ed 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f 76  Name = sqlite3_v
224ee 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31  alue_text(argv[1
224ef 5d 29 3b 0a 0a 20 20 69 6e 74 20 74 6f 6b 65 6e  ]);..  int token
224f0 3b 0a 20 20 54 6f 6b 65 6e 20 74 6e 61 6d 65 3b  ;.  Token tname;
224f1 0a 20 20 69 6e 74 20 64 69 73 74 20 3d 20 33 3b  .  int dist = 3;
224f2 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
224f3 20 63 6f 6e 73 74 20 2a 7a 43 73 72 20 3d 20 7a   const *zCsr = z
224f4 53 71 6c 3b 0a 20 20 69 6e 74 20 6c 65 6e 20 3d  Sql;.  int len =
224f5 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 52 65 74   0;.  char *zRet
224f6 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
224f7 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  = sqlite3_contex
224f8 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
224f9 65 78 74 29 3b 0a 0a 20 20 55 4e 55 53 45 44 5f  ext);..  UNUSED_
224fa 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
224fb 64 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 70 72  d);..  /* The pr
224fc 69 6e 63 69 70 6c 65 20 75 73 65 64 20 74 6f 20  inciple used to 
224fd 6c 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c 65  locate the table
224fe 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 43 52 45   name in the CRE
224ff 41 54 45 20 54 52 49 47 47 45 52 20 0a 20 20 2a  ATE TRIGGER .  *
22500 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 74  * statement is t
22501 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 6e 61  hat the table na
22502 6d 65 20 69 73 20 74 68 65 20 66 69 72 73 74 20  me is the first 
22503 74 6f 6b 65 6e 20 74 68 61 74 20 69 73 20 69 6d  token that is im
22504 6d 65 64 69 61 74 65 64 6c 79 0a 20 20 2a 2a 20  mediatedly.  ** 
22505 70 72 65 63 65 64 65 64 20 62 79 20 65 69 74 68  preceded by eith
22506 65 72 20 54 4b 5f 4f 4e 20 6f 72 20 54 4b 5f 44  er TK_ON or TK_D
22507 4f 54 20 61 6e 64 20 69 6d 6d 65 64 69 61 74 65  OT and immediate
22508 64 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  dly followed by 
22509 6f 6e 65 0a 20 20 2a 2a 20 6f 66 20 54 4b 5f 57  one.  ** of TK_W
2250a 48 45 4e 2c 20 54 4b 5f 42 45 47 49 4e 20 6f 72  HEN, TK_BEGIN or
2250b 20 54 4b 5f 46 4f 52 2e 0a 20 20 2a 2f 0a 20 20   TK_FOR..  */.  
2250c 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20  if( zSql ){.    
2250d 64 6f 20 7b 0a 0a 20 20 20 20 20 20 69 66 28 20  do {..      if( 
2250e 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20 20 20 20  !*zCsr ){.      
2250f 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20 6f 66 20    /* Ran out of 
22510 69 6e 70 75 74 20 62 65 66 6f 72 65 20 66 69 6e  input before fin
22511 64 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 6e  ding the table n
22512 61 6d 65 2e 20 52 65 74 75 72 6e 20 4e 55 4c 4c  ame. Return NULL
22513 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74  . */.        ret
22514 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  urn;.      }..  
22515 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68 65      /* Store the
22516 20 74 6f 6b 65 6e 20 74 68 61 74 20 7a 43 73 72   token that zCsr
22517 20 70 6f 69 6e 74 73 20 74 6f 20 69 6e 20 74 6e   points to in tn
22518 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 74 6e  ame. */.      tn
22519 61 6d 65 2e 7a 20 3d 20 28 63 68 61 72 2a 29 7a  ame.z = (char*)z
2251a 43 73 72 3b 0a 20 20 20 20 20 20 74 6e 61 6d 65  Csr;.      tname
2251b 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a 20 20 20 20 20  .n = len;..     
2251c 20 2f 2a 20 41 64 76 61 6e 63 65 20 7a 43 73 72   /* Advance zCsr
2251d 20 74 6f 20 74 68 65 20 6e 65 78 74 20 74 6f 6b   to the next tok
2251e 65 6e 2e 20 53 74 6f 72 65 20 74 68 61 74 20 74  en. Store that t
2251f 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20 27 74 6f  oken type in 'to
22520 6b 65 6e 27 2c 0a 20 20 20 20 20 20 2a 2a 20 61  ken',.      ** a
22521 6e 64 20 69 74 73 20 6c 65 6e 67 74 68 20 69 6e  nd its length in
22522 20 27 6c 65 6e 27 20 28 74 6f 20 62 65 20 75 73   'len' (to be us
22523 65 64 20 6e 65 78 74 20 69 74 65 72 61 74 69 6f  ed next iteratio
22524 6e 20 6f 66 20 74 68 69 73 20 6c 6f 6f 70 29 2e  n of this loop).
22525 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
22526 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 7a 43 73  do {.        zCs
22527 72 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20  r += len;.      
22528 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 47    len = sqlite3G
22529 65 74 54 6f 6b 65 6e 28 7a 43 73 72 2c 20 26 74  etToken(zCsr, &t
2252a 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 7d 77 68  oken);.      }wh
2252b 69 6c 65 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 53  ile( token==TK_S
2252c 50 41 43 45 20 29 3b 0a 20 20 20 20 20 20 61 73  PACE );.      as
2252d 73 65 72 74 28 20 6c 65 6e 3e 30 20 29 3b 0a 0a  sert( len>0 );..
2252e 20 20 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c        /* Variabl
2252f 65 20 27 64 69 73 74 27 20 73 74 6f 72 65 73 20  e 'dist' stores 
22530 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 6f  the number of to
22531 6b 65 6e 73 20 72 65 61 64 20 73 69 6e 63 65 20  kens read since 
22532 74 68 65 20 6d 6f 73 74 0a 20 20 20 20 20 20 2a  the most.      *
22533 2a 20 72 65 63 65 6e 74 20 54 4b 5f 44 4f 54 20  * recent TK_DOT 
22534 6f 72 20 54 4b 5f 4f 4e 2e 20 54 68 69 73 20 6d  or TK_ON. This m
22535 65 61 6e 73 20 74 68 61 74 20 77 68 65 6e 20 61  eans that when a
22536 20 57 48 45 4e 2c 20 46 4f 52 20 6f 72 20 42 45   WHEN, FOR or BE
22537 47 49 4e 20 0a 20 20 20 20 20 20 2a 2a 20 74 6f  GIN .      ** to
22538 6b 65 6e 20 69 73 20 72 65 61 64 20 61 6e 64 20  ken is read and 
22539 27 64 69 73 74 27 20 65 71 75 61 6c 73 20 32 2c  'dist' equals 2,
2253a 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 73   the condition s
2253b 74 61 74 65 64 20 61 62 6f 76 65 0a 20 20 20 20  tated above.    
2253c 20 20 2a 2a 20 74 6f 20 62 65 20 6d 65 74 2e 0a    ** to be met..
2253d 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
2253e 2a 20 4e 6f 74 65 20 74 68 61 74 20 4f 4e 20 63  * Note that ON c
2253f 61 6e 6e 6f 74 20 62 65 20 61 20 64 61 74 61 62  annot be a datab
22540 61 73 65 2c 20 74 61 62 6c 65 20 6f 72 20 63 6f  ase, table or co
22541 6c 75 6d 6e 20 6e 61 6d 65 2c 20 73 6f 0a 20 20  lumn name, so.  
22542 20 20 20 20 2a 2a 20 74 68 65 72 65 20 69 73 20      ** there is 
22543 6e 6f 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79  no need to worry
22544 20 61 62 6f 75 74 20 73 79 6e 74 61 78 20 6c 69   about syntax li
22545 6b 65 20 0a 20 20 20 20 20 20 2a 2a 20 22 43 52  ke .      ** "CR
22546 45 41 54 45 20 54 52 49 47 47 45 52 20 2e 2e 2e  EATE TRIGGER ...
22547 20 4f 4e 20 4f 4e 2e 4f 4e 20 42 45 47 49 4e 20   ON ON.ON BEGIN 
22548 2e 2e 2e 22 20 65 74 63 2e 0a 20 20 20 20 20 20  ..." etc..      
22549 2a 2f 0a 20 20 20 20 20 20 64 69 73 74 2b 2b 3b  */.      dist++;
2254a 0a 20 20 20 20 20 20 69 66 28 20 74 6f 6b 65 6e  .      if( token
2254b 3d 3d 54 4b 5f 44 4f 54 20 7c 7c 20 74 6f 6b 65  ==TK_DOT || toke
2254c 6e 3d 3d 54 4b 5f 4f 4e 20 29 7b 0a 20 20 20 20  n==TK_ON ){.    
2254d 20 20 20 20 64 69 73 74 20 3d 20 30 3b 0a 20 20      dist = 0;.  
2254e 20 20 20 20 7d 0a 20 20 20 20 7d 20 77 68 69 6c      }.    } whil
2254f 65 28 20 64 69 73 74 21 3d 32 20 7c 7c 20 28 74  e( dist!=2 || (t
22550 6f 6b 65 6e 21 3d 54 4b 5f 57 48 45 4e 20 26 26  oken!=TK_WHEN &&
22551 20 74 6f 6b 65 6e 21 3d 54 4b 5f 46 4f 52 20 26   token!=TK_FOR &
22552 26 20 74 6f 6b 65 6e 21 3d 54 4b 5f 42 45 47 49  & token!=TK_BEGI
22553 4e 29 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 56 61  N) );..    /* Va
22554 72 69 61 62 6c 65 20 74 6e 61 6d 65 20 6e 6f 77  riable tname now
22555 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 6f   contains the to
22556 6b 65 6e 20 74 68 61 74 20 69 73 20 74 68 65 20  ken that is the 
22557 6f 6c 64 20 74 61 62 6c 65 2d 6e 61 6d 65 0a 20  old table-name. 
22558 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 43 52 45     ** in the CRE
22559 41 54 45 20 54 52 49 47 47 45 52 20 73 74 61 74  ATE TRIGGER stat
2255a 65 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  ement..    */.  
2255b 20 20 7a 52 65 74 20 3d 20 73 71 6c 69 74 65 33    zRet = sqlite3
2255c 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 2e 2a  MPrintf(db, "%.*
2255d 73 5c 22 25 77 5c 22 25 73 22 2c 20 28 28 75 38  s\"%w\"%s", ((u8
2255e 2a 29 74 6e 61 6d 65 2e 7a 29 20 2d 20 7a 53 71  *)tname.z) - zSq
2255f 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20 20 20 20 20  l, zSql, .      
22560 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 74 6e 61   zTableName, tna
22561 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e 29 3b 0a 20  me.z+tname.n);. 
22562 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
22563 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
22564 7a 52 65 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45  zRet, -1, SQLITE
22565 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d  _DYNAMIC);.  }.}
22566 0a 23 65 6e 64 69 66 20 20 20 2f 2a 20 21 53 51  .#endif   /* !SQ
22567 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
22568 52 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69  R */../*.** Regi
22569 73 74 65 72 20 62 75 69 6c 74 2d 69 6e 20 66 75  ster built-in fu
2256a 6e 63 74 69 6f 6e 73 20 75 73 65 64 20 74 6f 20  nctions used to 
2256b 68 65 6c 70 20 69 6d 70 6c 65 6d 65 6e 74 20 41  help implement A
2256c 4c 54 45 52 20 54 41 42 4c 45 0a 2a 2f 0a 53 51  LTER TABLE.*/.SQ
2256d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2256e 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 75  d sqlite3AlterFu
2256f 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20  nctions(sqlite3 
22570 2a 64 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 43  *db){.  sqlite3C
22571 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 73  reateFunc(db, "s
22572 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62  qlite_rename_tab
22573 6c 65 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f 55  le", 2, SQLITE_U
22574 54 46 38 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  TF8, 0,.        
22575 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22576 20 72 65 6e 61 6d 65 54 61 62 6c 65 46 75 6e 63   renameTableFunc
22577 2c 20 30 2c 20 30 29 3b 0a 23 69 66 6e 64 65 66  , 0, 0);.#ifndef
22578 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
22579 47 47 45 52 0a 20 20 73 71 6c 69 74 65 33 43 72  GGER.  sqlite3Cr
2257a 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 73 71  eateFunc(db, "sq
2257b 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69 67  lite_rename_trig
2257c 67 65 72 22 2c 20 32 2c 20 53 51 4c 49 54 45 5f  ger", 2, SQLITE_
2257d 55 54 46 38 2c 20 30 2c 0a 20 20 20 20 20 20 20  UTF8, 0,.       
2257e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2257f 20 20 72 65 6e 61 6d 65 54 72 69 67 67 65 72 46    renameTriggerF
22580 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 23 65 6e 64  unc, 0, 0);.#end
22581 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  if.#ifndef SQLIT
22582 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b  E_OMIT_FOREIGN_K
22583 45 59 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61  EY.  sqlite3Crea
22584 74 65 46 75 6e 63 28 64 62 2c 20 22 73 71 6c 69  teFunc(db, "sqli
22585 74 65 5f 72 65 6e 61 6d 65 5f 70 61 72 65 6e 74  te_rename_parent
22586 22 2c 20 33 2c 20 53 51 4c 49 54 45 5f 55 54 46  ", 3, SQLITE_UTF
22587 38 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  8, 0,.          
22588 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
22589 65 6e 61 6d 65 50 61 72 65 6e 74 46 75 6e 63 2c  enameParentFunc,
2258a 20 30 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 7d   0, 0);.#endif.}
2258b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
2258c 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f  ction is used to
2258d 20 63 72 65 61 74 65 20 74 68 65 20 74 65 78 74   create the text
2258e 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20   of expressions 
2258f 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a  of the form:.**.
22590 2a 2a 20 20 20 6e 61 6d 65 3d 3c 63 6f 6e 73 74  **   name=<const
22591 61 6e 74 31 3e 20 4f 52 20 6e 61 6d 65 3d 3c 63  ant1> OR name=<c
22592 6f 6e 73 74 61 6e 74 32 3e 20 4f 52 20 2e 2e 2e  onstant2> OR ...
22593 0a 2a 2a 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65  .**.** If argume
22594 6e 74 20 7a 57 68 65 72 65 20 69 73 20 4e 55 4c  nt zWhere is NUL
22595 4c 2c 20 74 68 65 6e 20 61 20 70 6f 69 6e 74 65  L, then a pointe
22596 72 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e  r string contain
22597 69 6e 67 20 74 68 65 20 74 65 78 74 20 0a 2a 2a  ing the text .**
22598 20 22 6e 61 6d 65 3d 3c 63 6f 6e 73 74 61 6e 74   "name=<constant
22599 3e 22 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20  >" is returned, 
2259a 77 68 65 72 65 20 3c 63 6f 6e 73 74 61 6e 74 3e  where <constant>
2259b 20 69 73 20 74 68 65 20 71 75 6f 74 65 64 20 76   is the quoted v
2259c 65 72 73 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65  ersion.** of the
2259d 20 73 74 72 69 6e 67 20 70 61 73 73 65 64 20 61   string passed a
2259e 73 20 61 72 67 75 6d 65 6e 74 20 7a 43 6f 6e 73  s argument zCons
2259f 74 61 6e 74 2e 20 54 68 65 20 72 65 74 75 72 6e  tant. The return
225a0 65 64 20 62 75 66 66 65 72 20 69 73 0a 2a 2a 20  ed buffer is.** 
225a1 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20  allocated using 
225a2 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 28  sqlite3DbMalloc(
225a3 29 2e 20 49 74 20 69 73 20 74 68 65 20 72 65 73  ). It is the res
225a4 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20 74  ponsibility of t
225a5 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 20 74 6f 20  he.** caller to 
225a6 65 6e 73 75 72 65 20 74 68 61 74 20 69 74 20 69  ensure that it i
225a7 73 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65  s eventually fre
225a8 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 72 67  ed..**.** If arg
225a9 75 6d 65 6e 74 20 7a 57 68 65 72 65 20 69 73 20  ument zWhere is 
225aa 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74  not NULL, then t
225ab 68 65 20 73 74 72 69 6e 67 20 72 65 74 75 72 6e  he string return
225ac 65 64 20 69 73 20 0a 2a 2a 20 22 3c 77 68 65 72  ed is .** "<wher
225ad 65 3e 20 4f 52 20 6e 61 6d 65 3d 3c 63 6f 6e 73  e> OR name=<cons
225ae 74 61 6e 74 3e 22 2c 20 77 68 65 72 65 20 3c 77  tant>", where <w
225af 68 65 72 65 3e 20 69 73 20 74 68 65 20 63 6f 6e  here> is the con
225b0 74 65 6e 74 73 20 6f 66 20 7a 57 68 65 72 65 2e  tents of zWhere.
225b1 0a 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65  .** In this case
225b2 20 7a 57 68 65 72 65 20 69 73 20 70 61 73 73 65   zWhere is passe
225b3 64 20 74 6f 20 73 71 6c 69 74 65 33 44 62 46 72  d to sqlite3DbFr
225b4 65 65 28 29 20 62 65 66 6f 72 65 20 72 65 74 75  ee() before retu
225b5 72 6e 69 6e 67 2e 0a 2a 2a 20 0a 2a 2f 0a 73 74  rning..** .*/.st
225b6 61 74 69 63 20 63 68 61 72 20 2a 77 68 65 72 65  atic char *where
225b7 4f 72 4e 61 6d 65 28 73 71 6c 69 74 65 33 20 2a  OrName(sqlite3 *
225b8 64 62 2c 20 63 68 61 72 20 2a 7a 57 68 65 72 65  db, char *zWhere
225b9 2c 20 63 68 61 72 20 2a 7a 43 6f 6e 73 74 61 6e  , char *zConstan
225ba 74 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 65 77  t){.  char *zNew
225bb 3b 0a 20 20 69 66 28 20 21 7a 57 68 65 72 65 20  ;.  if( !zWhere 
225bc 29 7b 0a 20 20 20 20 7a 4e 65 77 20 3d 20 73 71  ){.    zNew = sq
225bd 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
225be 20 22 6e 61 6d 65 3d 25 51 22 2c 20 7a 43 6f 6e   "name=%Q", zCon
225bf 73 74 61 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b  stant);.  }else{
225c0 0a 20 20 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69  .    zNew = sqli
225c1 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
225c2 25 73 20 4f 52 20 6e 61 6d 65 3d 25 51 22 2c 20  %s OR name=%Q", 
225c3 7a 57 68 65 72 65 2c 20 7a 43 6f 6e 73 74 61 6e  zWhere, zConstan
225c4 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  t);.    sqlite3D
225c5 62 46 72 65 65 28 64 62 2c 20 7a 57 68 65 72 65  bFree(db, zWhere
225c6 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
225c7 7a 4e 65 77 3b 0a 7d 0a 0a 23 69 66 20 21 64 65  zNew;.}..#if !de
225c8 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
225c9 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20 26  T_FOREIGN_KEY) &
225ca 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
225cb 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a  E_OMIT_TRIGGER).
225cc 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74  /*.** Generate t
225cd 68 65 20 74 65 78 74 20 6f 66 20 61 20 57 48 45  he text of a WHE
225ce 52 45 20 65 78 70 72 65 73 73 69 6f 6e 20 77 68  RE expression wh
225cf 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 20  ich can be used 
225d0 74 6f 20 73 65 6c 65 63 74 20 61 6c 6c 0a 2a 2a  to select all.**
225d1 20 74 61 62 6c 65 73 20 74 68 61 74 20 68 61 76   tables that hav
225d2 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f  e foreign key co
225d3 6e 73 74 72 61 69 6e 74 73 20 74 68 61 74 20 72  nstraints that r
225d4 65 66 65 72 20 74 6f 20 74 61 62 6c 65 20 70 54  efer to table pT
225d5 61 62 20 28 69 2e 65 2e 0a 2a 2a 20 63 6f 6e 73  ab (i.e..** cons
225d6 74 72 61 69 6e 74 73 20 66 6f 72 20 77 68 69 63  traints for whic
225d7 68 20 70 54 61 62 20 69 73 20 74 68 65 20 70 61  h pTab is the pa
225d8 72 65 6e 74 20 74 61 62 6c 65 29 20 66 72 6f 6d  rent table) from
225d9 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74   the sqlite_mast
225da 65 72 0a 2a 2a 20 74 61 62 6c 65 2e 0a 2a 2f 0a  er.** table..*/.
225db 73 74 61 74 69 63 20 63 68 61 72 20 2a 77 68 65  static char *whe
225dc 72 65 46 6f 72 65 69 67 6e 4b 65 79 73 28 50 61  reForeignKeys(Pa
225dd 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62  rse *pParse, Tab
225de 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 46 4b 65  le *pTab){.  FKe
225df 79 20 2a 70 3b 0a 20 20 63 68 61 72 20 2a 7a 57  y *p;.  char *zW
225e0 68 65 72 65 20 3d 20 30 3b 0a 20 20 66 6f 72 28  here = 0;.  for(
225e1 70 3d 73 71 6c 69 74 65 33 46 6b 52 65 66 65 72  p=sqlite3FkRefer
225e2 65 6e 63 65 73 28 70 54 61 62 29 3b 20 70 3b 20  ences(pTab); p; 
225e3 70 3d 70 2d 3e 70 4e 65 78 74 54 6f 29 7b 0a 20  p=p->pNextTo){. 
225e4 20 20 20 7a 57 68 65 72 65 20 3d 20 77 68 65 72     zWhere = wher
225e5 65 4f 72 4e 61 6d 65 28 70 50 61 72 73 65 2d 3e  eOrName(pParse->
225e6 64 62 2c 20 7a 57 68 65 72 65 2c 20 70 2d 3e 70  db, zWhere, p->p
225e7 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  From->zName);.  
225e8 7d 0a 20 20 72 65 74 75 72 6e 20 7a 57 68 65 72  }.  return zWher
225e9 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  e;.}.#endif../*.
225ea 2a 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65 20  ** Generate the 
225eb 74 65 78 74 20 6f 66 20 61 20 57 48 45 52 45 20  text of a WHERE 
225ec 65 78 70 72 65 73 73 69 6f 6e 20 77 68 69 63 68  expression which
225ed 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
225ee 73 65 6c 65 63 74 20 61 6c 6c 0a 2a 2a 20 74 65  select all.** te
225ef 6d 70 6f 72 61 72 79 20 74 72 69 67 67 65 72 73  mporary triggers
225f0 20 6f 6e 20 74 61 62 6c 65 20 70 54 61 62 20 66   on table pTab f
225f1 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 74  rom the sqlite_t
225f2 65 6d 70 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  emp_master table
225f3 2e 20 49 66 0a 2a 2a 20 74 61 62 6c 65 20 70 54  . If.** table pT
225f4 61 62 20 68 61 73 20 6e 6f 20 74 65 6d 70 6f 72  ab has no tempor
225f5 61 72 79 20 74 72 69 67 67 65 72 73 2c 20 6f 72  ary triggers, or
225f6 20 69 73 20 69 74 73 65 6c 66 20 73 74 6f 72 65   is itself store
225f7 64 20 69 6e 20 74 68 65 20 0a 2a 2a 20 74 65 6d  d in the .** tem
225f8 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 2c  porary database,
225f9 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65   NULL is returne
225fa 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61  d..*/.static cha
225fb 72 20 2a 77 68 65 72 65 54 65 6d 70 54 72 69 67  r *whereTempTrig
225fc 67 65 72 73 28 50 61 72 73 65 20 2a 70 50 61 72  gers(Parse *pPar
225fd 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29  se, Table *pTab)
225fe 7b 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72  {.  Trigger *pTr
225ff 69 67 3b 0a 20 20 63 68 61 72 20 2a 7a 57 68 65  ig;.  char *zWhe
22600 72 65 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20  re = 0;.  const 
22601 53 63 68 65 6d 61 20 2a 70 54 65 6d 70 53 63 68  Schema *pTempSch
22602 65 6d 61 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  ema = pParse->db
22603 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61  ->aDb[1].pSchema
22604 3b 20 2f 2a 20 54 65 6d 70 20 64 62 20 73 63 68  ; /* Temp db sch
22605 65 6d 61 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20  ema */..  /* If 
22606 74 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74  the table is not
22607 20 6c 6f 63 61 74 65 64 20 69 6e 20 74 68 65 20   located in the 
22608 74 65 6d 70 2d 64 62 20 28 69 6e 20 77 68 69 63  temp-db (in whic
22609 68 20 63 61 73 65 20 4e 55 4c 4c 20 69 73 20 0a  h case NULL is .
2260a 20 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20 6c    ** returned, l
2260b 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20  oop through the 
2260c 74 61 62 6c 65 73 20 6c 69 73 74 20 6f 66 20 74  tables list of t
2260d 72 69 67 67 65 72 73 2e 20 46 6f 72 20 65 61 63  riggers. For eac
2260e 68 20 74 72 69 67 67 65 72 0a 20 20 2a 2a 20 74  h trigger.  ** t
2260f 68 61 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20  hat is not part 
22610 6f 66 20 74 68 65 20 74 65 6d 70 2d 64 62 20 73  of the temp-db s
22611 63 68 65 6d 61 2c 20 61 64 64 20 61 20 63 6c 61  chema, add a cla
22612 75 73 65 20 74 6f 20 74 68 65 20 57 48 45 52 45  use to the WHERE
22613 20 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f   .  ** expressio
22614 6e 20 62 65 69 6e 67 20 62 75 69 6c 74 20 75 70  n being built up
22615 20 69 6e 20 7a 57 68 65 72 65 2e 0a 20 20 2a 2f   in zWhere..  */
22616 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 63  .  if( pTab->pSc
22617 68 65 6d 61 21 3d 70 54 65 6d 70 53 63 68 65 6d  hema!=pTempSchem
22618 61 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  a ){.    sqlite3
22619 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2261a 62 3b 0a 20 20 20 20 66 6f 72 28 70 54 72 69 67  b;.    for(pTrig
2261b 3d 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c  =sqlite3TriggerL
2261c 69 73 74 28 70 50 61 72 73 65 2c 20 70 54 61 62  ist(pParse, pTab
2261d 29 3b 20 70 54 72 69 67 3b 20 70 54 72 69 67 3d  ); pTrig; pTrig=
2261e 70 54 72 69 67 2d 3e 70 4e 65 78 74 29 7b 0a 20  pTrig->pNext){. 
2261f 20 20 20 20 20 69 66 28 20 70 54 72 69 67 2d 3e       if( pTrig->
22620 70 53 63 68 65 6d 61 3d 3d 70 54 65 6d 70 53 63  pSchema==pTempSc
22621 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 20  hema ){.        
22622 7a 57 68 65 72 65 20 3d 20 77 68 65 72 65 4f 72  zWhere = whereOr
22623 4e 61 6d 65 28 64 62 2c 20 7a 57 68 65 72 65 2c  Name(db, zWhere,
22624 20 70 54 72 69 67 2d 3e 7a 4e 61 6d 65 29 3b 0a   pTrig->zName);.
22625 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
22626 7d 0a 20 20 72 65 74 75 72 6e 20 7a 57 68 65 72  }.  return zWher
22627 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  e;.}../*.** Gene
22628 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64 72 6f  rate code to dro
22629 70 20 61 6e 64 20 72 65 6c 6f 61 64 20 74 68 65  p and reload the
2262a 20 69 6e 74 65 72 6e 61 6c 20 72 65 70 72 65 73   internal repres
2262b 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 61 62 6c  entation of tabl
2262c 65 0a 2a 2a 20 70 54 61 62 20 66 72 6f 6d 20 74  e.** pTab from t
2262d 68 65 20 64 61 74 61 62 61 73 65 2c 20 69 6e 63  he database, inc
2262e 6c 75 64 69 6e 67 20 74 72 69 67 67 65 72 73 20  luding triggers 
2262f 61 6e 64 20 74 65 6d 70 6f 72 61 72 79 20 74 72  and temporary tr
22630 69 67 67 65 72 73 2e 0a 2a 2a 20 41 72 67 75 6d  iggers..** Argum
22631 65 6e 74 20 7a 4e 61 6d 65 20 69 73 20 74 68 65  ent zName is the
22632 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   name of the tab
22633 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  le in the databa
22634 73 65 20 73 63 68 65 6d 61 20 61 74 0a 2a 2a 20  se schema at.** 
22635 74 68 65 20 74 69 6d 65 20 74 68 65 20 67 65 6e  the time the gen
22636 65 72 61 74 65 64 20 63 6f 64 65 20 69 73 20 65  erated code is e
22637 78 65 63 75 74 65 64 2e 20 54 68 69 73 20 63 61  xecuted. This ca
22638 6e 20 62 65 20 64 69 66 66 65 72 65 6e 74 20 66  n be different f
22639 72 6f 6d 0a 2a 2a 20 70 54 61 62 2d 3e 7a 4e 61  rom.** pTab->zNa
2263a 6d 65 20 69 66 20 74 68 69 73 20 66 75 6e 63 74  me if this funct
2263b 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 63 61 6c  ion is being cal
2263c 6c 65 64 20 74 6f 20 63 6f 64 65 20 70 61 72 74  led to code part
2263d 20 6f 66 20 61 6e 20 0a 2a 2a 20 22 41 4c 54 45   of an .** "ALTE
2263e 52 20 54 41 42 4c 45 20 52 45 4e 41 4d 45 20 54  R TABLE RENAME T
2263f 4f 22 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f  O" statement..*/
22640 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 6c  .static void rel
22641 6f 61 64 54 61 62 6c 65 53 63 68 65 6d 61 28 50  oadTableSchema(P
22642 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61  arse *pParse, Ta
22643 62 6c 65 20 2a 70 54 61 62 2c 20 63 6f 6e 73 74  ble *pTab, const
22644 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20   char *zName){. 
22645 20 56 64 62 65 20 2a 76 3b 0a 20 20 63 68 61 72   Vdbe *v;.  char
22646 20 2a 7a 57 68 65 72 65 3b 0a 20 20 69 6e 74 20   *zWhere;.  int 
22647 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
22648 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
22649 6f 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74  of database cont
2264a 61 69 6e 69 6e 67 20 70 54 61 62 20 2a 2f 0a 23  aining pTab */.#
2264b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2264c 49 54 5f 54 52 49 47 47 45 52 0a 20 20 54 72 69  IT_TRIGGER.  Tri
2264d 67 67 65 72 20 2a 70 54 72 69 67 3b 0a 23 65 6e  gger *pTrig;.#en
2264e 64 69 66 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74  dif..  v = sqlit
2264f 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
22650 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 76  );.  if( NEVER(v
22651 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 20  ==0) ) return;. 
22652 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
22653 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74  BtreeHoldsAllMut
22654 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29  exes(pParse->db)
22655 20 29 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69   );.  iDb = sqli
22656 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
22657 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61  (pParse->db, pTa
22658 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61  b->pSchema);.  a
22659 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 29 3b  ssert( iDb>=0 );
2265a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2265b 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20  _OMIT_TRIGGER.  
2265c 2f 2a 20 44 72 6f 70 20 61 6e 79 20 74 61 62 6c  /* Drop any tabl
2265d 65 20 74 72 69 67 67 65 72 73 20 66 72 6f 6d 20  e triggers from 
2265e 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68  the internal sch
2265f 65 6d 61 2e 20 2a 2f 0a 20 20 66 6f 72 28 70 54  ema. */.  for(pT
22660 72 69 67 3d 73 71 6c 69 74 65 33 54 72 69 67 67  rig=sqlite3Trigg
22661 65 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70  erList(pParse, p
22662 54 61 62 29 3b 20 70 54 72 69 67 3b 20 70 54 72  Tab); pTrig; pTr
22663 69 67 3d 70 54 72 69 67 2d 3e 70 4e 65 78 74 29  ig=pTrig->pNext)
22664 7b 0a 20 20 20 20 69 6e 74 20 69 54 72 69 67 44  {.    int iTrigD
22665 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
22666 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
22667 3e 64 62 2c 20 70 54 72 69 67 2d 3e 70 53 63 68  >db, pTrig->pSch
22668 65 6d 61 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ema);.    assert
22669 28 20 69 54 72 69 67 44 62 3d 3d 69 44 62 20 7c  ( iTrigDb==iDb |
2266a 7c 20 69 54 72 69 67 44 62 3d 3d 31 20 29 3b 0a  | iTrigDb==1 );.
2266b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2266c 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70  ddOp4(v, OP_Drop
2266d 54 72 69 67 67 65 72 2c 20 69 54 72 69 67 44 62  Trigger, iTrigDb
2266e 2c 20 30 2c 20 30 2c 20 70 54 72 69 67 2d 3e 7a  , 0, 0, pTrig->z
2266f 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 23 65  Name, 0);.  }.#e
22670 6e 64 69 66 0a 0a 20 20 2f 2a 20 44 72 6f 70 20  ndif..  /* Drop 
22671 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69 6e  the table and in
22672 64 65 78 20 66 72 6f 6d 20 74 68 65 20 69 6e 74  dex from the int
22673 65 72 6e 61 6c 20 73 63 68 65 6d 61 2e 20 20 2a  ernal schema.  *
22674 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  /.  sqlite3VdbeA
22675 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70  ddOp4(v, OP_Drop
22676 54 61 62 6c 65 2c 20 69 44 62 2c 20 30 2c 20 30  Table, iDb, 0, 0
22677 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30  , pTab->zName, 0
22678 29 3b 0a 0a 20 20 2f 2a 20 52 65 6c 6f 61 64 20  );..  /* Reload 
22679 74 68 65 20 74 61 62 6c 65 2c 20 69 6e 64 65 78  the table, index
2267a 20 61 6e 64 20 70 65 72 6d 61 6e 65 6e 74 20 74   and permanent t
2267b 72 69 67 67 65 72 20 73 63 68 65 6d 61 73 2e 20  rigger schemas. 
2267c 2a 2f 0a 20 20 7a 57 68 65 72 65 20 3d 20 73 71  */.  zWhere = sq
2267d 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 70 50 61  lite3MPrintf(pPa
2267e 72 73 65 2d 3e 64 62 2c 20 22 74 62 6c 5f 6e 61  rse->db, "tbl_na
2267f 6d 65 3d 25 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a  me=%Q", zName);.
22680 20 20 69 66 28 20 21 7a 57 68 65 72 65 20 29 20    if( !zWhere ) 
22681 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65  return;.  sqlite
22682 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
22683 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69  P_ParseSchema, i
22684 44 62 2c 20 30 2c 20 30 2c 20 7a 57 68 65 72 65  Db, 0, 0, zWhere
22685 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 0a  , P4_DYNAMIC);..
22686 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
22687 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a  MIT_TRIGGER.  /*
22688 20 4e 6f 77 2c 20 69 66 20 74 68 65 20 74 61 62   Now, if the tab
22689 6c 65 20 69 73 20 6e 6f 74 20 73 74 6f 72 65 64  le is not stored
2268a 20 69 6e 20 74 68 65 20 74 65 6d 70 20 64 61 74   in the temp dat
2268b 61 62 61 73 65 2c 20 72 65 6c 6f 61 64 20 61 6e  abase, reload an
2268c 79 20 74 65 6d 70 20 0a 20 20 2a 2a 20 74 72 69  y temp .  ** tri
2268d 67 67 65 72 73 2e 20 44 6f 6e 27 74 20 75 73 65  ggers. Don't use
2268e 20 49 4e 28 2e 2e 2e 29 20 69 6e 20 63 61 73 65   IN(...) in case
2268f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
22690 51 55 45 52 59 20 69 73 20 64 65 66 69 6e 65 64  QUERY is defined
22691 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 7a  . .  */.  if( (z
22692 57 68 65 72 65 3d 77 68 65 72 65 54 65 6d 70 54  Where=whereTempT
22693 72 69 67 67 65 72 73 28 70 50 61 72 73 65 2c 20  riggers(pParse, 
22694 70 54 61 62 29 29 21 3d 30 20 29 7b 0a 20 20 20  pTab))!=0 ){.   
22695 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22696 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65 53 63  p4(v, OP_ParseSc
22697 68 65 6d 61 2c 20 31 2c 20 30 2c 20 30 2c 20 7a  hema, 1, 0, 0, z
22698 57 68 65 72 65 2c 20 50 34 5f 44 59 4e 41 4d 49  Where, P4_DYNAMI
22699 43 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d  C);.  }.#endif.}
2269a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
2269b 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65   code to impleme
2269c 6e 74 20 74 68 65 20 22 41 4c 54 45 52 20 54 41  nt the "ALTER TA
2269d 42 4c 45 20 78 78 78 20 52 45 4e 41 4d 45 20 54  BLE xxx RENAME T
2269e 4f 20 79 79 79 22 20 0a 2a 2a 20 63 6f 6d 6d 61  O yyy" .** comma
2269f 6e 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nd. .*/.SQLITE_P
226a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
226a1 74 65 33 41 6c 74 65 72 52 65 6e 61 6d 65 54 61  te3AlterRenameTa
226a2 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ble(.  Parse *pP
226a3 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  arse,           
226a4 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65   /* Parser conte
226a5 78 74 2e 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  xt. */.  SrcList
226a6 20 2a 70 53 72 63 2c 20 20 20 20 20 20 20 20 20   *pSrc,         
226a7 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
226a8 74 6f 20 72 65 6e 61 6d 65 2e 20 2a 2f 0a 20 20  to rename. */.  
226a9 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 20 20 20  Token *pName    
226aa 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
226ab 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e   new table name.
226ac 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62   */.){.  int iDb
226ad 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
226ae 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74     /* Database t
226af 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  hat contains the
226b0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72   table */.  char
226b1 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 20 20 20   *zDb;          
226b2 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
226b3 20 64 61 74 61 62 61 73 65 20 69 44 62 20 2a 2f   database iDb */
226b4 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20  .  Table *pTab; 
226b5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
226b6 54 61 62 6c 65 20 62 65 69 6e 67 20 72 65 6e 61  Table being rena
226b7 6d 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  med */.  char *z
226b8 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 20 20  Name = 0;       
226b9 20 20 20 2f 2a 20 4e 55 4c 4c 2d 74 65 72 6d 69     /* NULL-termi
226ba 6e 61 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66  nated version of
226bb 20 70 4e 61 6d 65 20 2a 2f 20 0a 20 20 73 71 6c   pName */ .  sql
226bc 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
226bd 65 2d 3e 64 62 3b 20 2f 2a 20 44 61 74 61 62 61  e->db; /* Databa
226be 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
226bf 0a 20 20 69 6e 74 20 6e 54 61 62 4e 61 6d 65 3b  .  int nTabName;
226c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
226c1 4e 75 6d 62 65 72 20 6f 66 20 55 54 46 2d 38 20  Number of UTF-8 
226c2 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 54  characters in zT
226c3 61 62 4e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73  abName */.  cons
226c4 74 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65  t char *zTabName
226c5 3b 20 20 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61  ;     /* Origina
226c6 6c 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61  l name of the ta
226c7 62 6c 65 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76  ble */.  Vdbe *v
226c8 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
226c9 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20  _OMIT_TRIGGER.  
226ca 63 68 61 72 20 2a 7a 57 68 65 72 65 20 3d 20 30  char *zWhere = 0
226cb 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65  ;         /* Whe
226cc 72 65 20 63 6c 61 75 73 65 20 74 6f 20 6c 6f 63  re clause to loc
226cd 61 74 65 20 74 65 6d 70 20 74 72 69 67 67 65 72  ate temp trigger
226ce 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 56 54  s */.#endif.  VT
226cf 61 62 6c 65 20 2a 70 56 54 61 62 20 3d 20 30 3b  able *pVTab = 0;
226d0 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 6e 2d 7a          /* Non-z
226d1 65 72 6f 20 69 66 20 74 68 69 73 20 69 73 20 61  ero if this is a
226d2 20 76 2d 74 61 62 20 77 69 74 68 20 61 6e 20 78   v-tab with an x
226d3 52 65 6e 61 6d 65 28 29 20 2a 2f 0a 20 20 0a 20  Rename() */.  . 
226d4 20 69 66 28 20 4e 45 56 45 52 28 64 62 2d 3e 6d   if( NEVER(db->m
226d5 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20 29 20 67  allocFailed) ) g
226d6 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f  oto exit_rename_
226d7 74 61 62 6c 65 3b 0a 20 20 61 73 73 65 72 74 28  table;.  assert(
226d8 20 70 53 72 63 2d 3e 6e 53 72 63 3d 3d 31 20 29   pSrc->nSrc==1 )
226d9 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
226da 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c  te3BtreeHoldsAll
226db 4d 75 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e  Mutexes(pParse->
226dc 64 62 29 20 29 3b 0a 0a 20 20 70 54 61 62 20 3d  db) );..  pTab =
226dd 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61   sqlite3LocateTa
226de 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70  ble(pParse, 0, p
226df 53 72 63 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c  Src->a[0].zName,
226e0 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 44 61 74   pSrc->a[0].zDat
226e1 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 21 70  abase);.  if( !p
226e2 54 61 62 20 29 20 67 6f 74 6f 20 65 78 69 74 5f  Tab ) goto exit_
226e3 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20  rename_table;.  
226e4 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
226e5 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73  emaToIndex(pPars
226e6 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  e->db, pTab->pSc
226e7 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20 3d 20 64  hema);.  zDb = d
226e8 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
226e9 65 3b 0a 0a 20 20 2f 2a 20 47 65 74 20 61 20 4e  e;..  /* Get a N
226ea 55 4c 4c 20 74 65 72 6d 69 6e 61 74 65 64 20 76  ULL terminated v
226eb 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 6e 65  ersion of the ne
226ec 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f  w table name. */
226ed 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  .  zName = sqlit
226ee 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
226ef 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66  db, pName);.  if
226f0 28 20 21 7a 4e 61 6d 65 20 29 20 67 6f 74 6f 20  ( !zName ) goto 
226f1 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c  exit_rename_tabl
226f2 65 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74  e;..  /* Check t
226f3 68 61 74 20 61 20 74 61 62 6c 65 20 6f 72 20 69  hat a table or i
226f4 6e 64 65 78 20 6e 61 6d 65 64 20 27 7a 4e 61 6d  ndex named 'zNam
226f5 65 27 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72 65  e' does not alre
226f6 61 64 79 20 65 78 69 73 74 0a 20 20 2a 2a 20 69  ady exist.  ** i
226f7 6e 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20  n database iDb. 
226f8 49 66 20 73 6f 2c 20 74 68 69 73 20 69 73 20 61  If so, this is a
226f9 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20  n error..  */.  
226fa 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54  if( sqlite3FindT
226fb 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  able(db, zName, 
226fc 7a 44 62 29 20 7c 7c 20 73 71 6c 69 74 65 33 46  zDb) || sqlite3F
226fd 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 4e 61  indIndex(db, zNa
226fe 6d 65 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20  me, zDb) ){.    
226ff 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
22700 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
22701 20 22 74 68 65 72 65 20 69 73 20 61 6c 72 65 61   "there is alrea
22702 64 79 20 61 6e 6f 74 68 65 72 20 74 61 62 6c 65  dy another table
22703 20 6f 72 20 69 6e 64 65 78 20 77 69 74 68 20 74   or index with t
22704 68 69 73 20 6e 61 6d 65 3a 20 25 73 22 2c 20 7a  his name: %s", z
22705 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20  Name);.    goto 
22706 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c  exit_rename_tabl
22707 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b  e;.  }..  /* Mak
22708 65 20 73 75 72 65 20 69 74 20 69 73 20 6e 6f 74  e sure it is not
22709 20 61 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20   a system table 
2270a 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2c 20 6f  being altered, o
2270b 72 20 61 20 72 65 73 65 72 76 65 64 20 6e 61 6d  r a reserved nam
2270c 65 0a 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20  e.  ** that the 
2270d 74 61 62 6c 65 20 69 73 20 62 65 69 6e 67 20 72  table is being r
2270e 65 6e 61 6d 65 64 20 74 6f 2e 0a 20 20 2a 2f 0a  enamed to..  */.
2270f 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
22710 6c 65 6e 33 30 28 70 54 61 62 2d 3e 7a 4e 61 6d  len30(pTab->zNam
22711 65 29 3e 36 20 0a 20 20 20 26 26 20 30 3d 3d 73  e)>6 .   && 0==s
22712 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70  qlite3StrNICmp(p
22713 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c  Tab->zName, "sql
22714 69 74 65 5f 22 2c 20 37 29 0a 20 20 29 7b 0a 20  ite_", 7).  ){. 
22715 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
22716 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c  sg(pParse, "tabl
22717 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20  e %s may not be 
22718 61 6c 74 65 72 65 64 22 2c 20 70 54 61 62 2d 3e  altered", pTab->
22719 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f  zName);.    goto
2271a 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62   exit_rename_tab
2271b 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53 51  le;.  }.  if( SQ
2271c 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33  LITE_OK!=sqlite3
2271d 43 68 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28  CheckObjectName(
2271e 70 50 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29  pParse, zName) )
2271f 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  {.    goto exit_
22720 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20  rename_table;.  
22721 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
22722 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 69 66  E_OMIT_VIEW.  if
22723 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  ( pTab->pSelect 
22724 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
22725 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
22726 76 69 65 77 20 25 73 20 6d 61 79 20 6e 6f 74 20  view %s may not 
22727 62 65 20 61 6c 74 65 72 65 64 22 2c 20 70 54 61  be altered", pTa
22728 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67  b->zName);.    g
22729 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f  oto exit_rename_
2272a 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69  table;.  }.#endi
2272b 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
2272c 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
2272d 54 49 4f 4e 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65  TION.  /* Invoke
2272e 20 74 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69   the authorizati
2272f 6f 6e 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a  on callback. */.
22730 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
22731 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
22732 51 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c  QLITE_ALTER_TABL
22733 45 2c 20 7a 44 62 2c 20 70 54 61 62 2d 3e 7a 4e  E, zDb, pTab->zN
22734 61 6d 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 67  ame, 0) ){.    g
22735 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f  oto exit_rename_
22736 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69  table;.  }.#endi
22737 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
22738 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
22739 42 4c 45 0a 20 20 69 66 28 20 73 71 6c 69 74 65  BLE.  if( sqlite
2273a 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61  3ViewGetColumnNa
2273b 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  mes(pParse, pTab
2273c 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78  ) ){.    goto ex
2273d 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b  it_rename_table;
2273e 0a 20 20 7d 0a 20 20 69 66 28 20 49 73 56 69 72  .  }.  if( IsVir
2273f 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
22740 20 20 70 56 54 61 62 20 3d 20 73 71 6c 69 74 65    pVTab = sqlite
22741 33 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70  3GetVTable(db, p
22742 54 61 62 29 3b 0a 20 20 20 20 69 66 28 20 70 56  Tab);.    if( pV
22743 54 61 62 2d 3e 70 56 74 61 62 2d 3e 70 4d 6f 64  Tab->pVtab->pMod
22744 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 3d 3d 30 20  ule->xRename==0 
22745 29 7b 0a 20 20 20 20 20 20 70 56 54 61 62 20 3d  ){.      pVTab =
22746 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65   0;.    }.  }.#e
22747 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65 67 69 6e  ndif..  /* Begin
22748 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61   a transaction a
22749 6e 64 20 63 6f 64 65 20 74 68 65 20 56 65 72 69  nd code the Veri
2274a 66 79 43 6f 6f 6b 69 65 20 66 6f 72 20 64 61 74  fyCookie for dat
2274b 61 62 61 73 65 20 69 44 62 2e 20 0a 20 20 2a 2a  abase iDb. .  **
2274c 20 54 68 65 6e 20 6d 6f 64 69 66 79 20 74 68 65   Then modify the
2274d 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 28   schema cookie (
2274e 73 69 6e 63 65 20 74 68 65 20 41 4c 54 45 52 20  since the ALTER 
2274f 54 41 42 4c 45 20 6d 6f 64 69 66 69 65 73 20 74  TABLE modifies t
22750 68 65 0a 20 20 2a 2a 20 73 63 68 65 6d 61 29 2e  he.  ** schema).
22751 20 4f 70 65 6e 20 61 20 73 74 61 74 65 6d 65 6e   Open a statemen
22752 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 66  t transaction if
22753 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 61 20   the table is a 
22754 76 69 72 74 75 61 6c 0a 20 20 2a 2a 20 74 61 62  virtual.  ** tab
22755 6c 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73  le..  */.  v = s
22756 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
22757 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d  arse);.  if( v==
22758 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78  0 ){.    goto ex
22759 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3b  it_rename_table;
2275a 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 65  .  }.  sqlite3Be
2275b 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
2275c 6e 28 70 50 61 72 73 65 2c 20 70 56 54 61 62 21  n(pParse, pVTab!
2275d 3d 30 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c 69  =0, iDb);.  sqli
2275e 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28  te3ChangeCookie(
2275f 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 0a 20  pParse, iDb);.. 
22760 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
22761 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2c 20   virtual table, 
22762 69 6e 76 6f 6b 65 20 74 68 65 20 78 52 65 6e 61  invoke the xRena
22763 6d 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 66  me() function if
22764 0a 20 20 2a 2a 20 6f 6e 65 20 69 73 20 64 65 66  .  ** one is def
22765 69 6e 65 64 2e 20 54 68 65 20 78 52 65 6e 61 6d  ined. The xRenam
22766 65 28 29 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c  e() callback wil
22767 6c 20 6d 6f 64 69 66 79 20 74 68 65 20 6e 61 6d  l modify the nam
22768 65 73 0a 20 20 2a 2a 20 6f 66 20 61 6e 79 20 72  es.  ** of any r
22769 65 73 6f 75 72 63 65 73 20 75 73 65 64 20 62 79  esources used by
2276a 20 74 68 65 20 76 2d 74 61 62 6c 65 20 69 6d 70   the v-table imp
2276b 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 28 69 6e 63  lementation (inc
2276c 6c 75 64 69 6e 67 20 6f 74 68 65 72 0a 20 20 2a  luding other.  *
2276d 2a 20 53 51 4c 69 74 65 20 74 61 62 6c 65 73 29  * SQLite tables)
2276e 20 74 68 61 74 20 61 72 65 20 69 64 65 6e 74 69   that are identi
2276f 66 69 65 64 20 62 79 20 74 68 65 20 6e 61 6d 65  fied by the name
22770 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   of the virtual 
22771 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 23 69 66 6e  table..  */.#ifn
22772 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
22773 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69  VIRTUALTABLE.  i
22774 66 28 20 70 56 54 61 62 20 29 7b 0a 20 20 20 20  f( pVTab ){.    
22775 69 6e 74 20 69 20 3d 20 2b 2b 70 50 61 72 73 65  int i = ++pParse
22776 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69  ->nMem;.    sqli
22777 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
22778 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
22779 69 2c 20 30 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b  i, 0, zName, 0);
2277a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2277b 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 52 65  AddOp4(v, OP_VRe
2277c 6e 61 6d 65 2c 20 69 2c 20 30 2c 20 30 2c 28 63  name, i, 0, 0,(c
2277d 6f 6e 73 74 20 63 68 61 72 2a 29 70 56 54 61 62  onst char*)pVTab
2277e 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20 20 20 20  , P4_VTAB);.    
2277f 73 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28  sqlite3MayAbort(
22780 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 23 65 6e  pParse);.  }.#en
22781 64 69 66 0a 0a 20 20 2f 2a 20 66 69 67 75 72 65  dif..  /* figure
22782 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 55 54   out how many UT
22783 46 2d 38 20 63 68 61 72 61 63 74 65 72 73 20 61  F-8 characters a
22784 72 65 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20  re in zName */. 
22785 20 7a 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62   zTabName = pTab
22786 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 6e 54 61 62 4e  ->zName;.  nTabN
22787 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 55 74 66  ame = sqlite3Utf
22788 38 43 68 61 72 4c 65 6e 28 7a 54 61 62 4e 61 6d  8CharLen(zTabNam
22789 65 2c 20 2d 31 29 3b 0a 0a 23 69 66 20 21 64 65  e, -1);..#if !de
2278a 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2278b 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20 26  T_FOREIGN_KEY) &
2278c 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
2278d 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a  E_OMIT_TRIGGER).
2278e 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 26    if( db->flags&
2278f 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65  SQLITE_ForeignKe
22790 79 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  ys ){.    /* If 
22791 66 6f 72 65 69 67 6e 2d 6b 65 79 20 73 75 70 70  foreign-key supp
22792 6f 72 74 20 69 73 20 65 6e 61 62 6c 65 64 2c 20  ort is enabled, 
22793 72 65 77 72 69 74 65 20 74 68 65 20 43 52 45 41  rewrite the CREA
22794 54 45 20 54 41 42 4c 45 20 0a 20 20 20 20 2a 2a  TE TABLE .    **
22795 20 73 74 61 74 65 6d 65 6e 74 73 20 63 6f 72 72   statements corr
22796 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 61 6c 6c  esponding to all
22797 20 63 68 69 6c 64 20 74 61 62 6c 65 73 20 6f 66   child tables of
22798 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e   foreign key con
22799 73 74 72 61 69 6e 74 73 0a 20 20 20 20 2a 2a 20  straints.    ** 
2279a 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 72 65  for which the re
2279b 6e 61 6d 65 64 20 74 61 62 6c 65 20 69 73 20 74  named table is t
2279c 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 2e  he parent table.
2279d 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 28 7a 57    */.    if( (zW
2279e 68 65 72 65 3d 77 68 65 72 65 46 6f 72 65 69 67  here=whereForeig
2279f 6e 4b 65 79 73 28 70 50 61 72 73 65 2c 20 70 54  nKeys(pParse, pT
227a0 61 62 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ab))!=0 ){.     
227a1 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
227a2 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20  rse(pParse, .   
227a3 20 20 20 20 20 20 20 22 55 50 44 41 54 45 20 73         "UPDATE s
227a4 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 53 45 54  qlite_master SET
227a5 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   ".             
227a6 20 22 73 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72   "sql = sqlite_r
227a7 65 6e 61 6d 65 5f 70 61 72 65 6e 74 28 73 71 6c  ename_parent(sql
227a8 2c 20 25 51 2c 20 25 51 29 20 22 0a 20 20 20 20  , %Q, %Q) ".    
227a9 20 20 20 20 20 20 20 20 20 20 22 57 48 45 52 45            "WHERE
227aa 20 25 73 3b 22 2c 20 7a 54 61 62 4e 61 6d 65 2c   %s;", zTabName,
227ab 20 7a 4e 61 6d 65 2c 20 7a 57 68 65 72 65 29 3b   zName, zWhere);
227ac 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
227ad 46 72 65 65 28 64 62 2c 20 7a 57 68 65 72 65 29  Free(db, zWhere)
227ae 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
227af 69 66 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79 20  if..  /* Modify 
227b0 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  the sqlite_maste
227b1 72 20 74 61 62 6c 65 20 74 6f 20 75 73 65 20 74  r table to use t
227b2 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d  he new table nam
227b3 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4e  e. */.  sqlite3N
227b4 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
227b5 65 2c 0a 20 20 20 20 20 20 22 55 50 44 41 54 45  e,.      "UPDATE
227b6 20 25 51 2e 25 73 20 53 45 54 20 22 0a 23 69 66   %Q.%s SET ".#if
227b7 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
227b8 54 52 49 47 47 45 52 0a 20 20 20 20 20 20 20 20  TRIGGER.        
227b9 20 20 22 73 71 6c 20 3d 20 73 71 6c 69 74 65 5f    "sql = sqlite_
227ba 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 73 71 6c  rename_table(sql
227bb 2c 20 25 51 29 2c 20 22 0a 23 65 6c 73 65 0a 20  , %Q), ".#else. 
227bc 20 20 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20           "sql = 
227bd 43 41 53 45 20 22 0a 20 20 20 20 20 20 20 20 20  CASE ".         
227be 20 20 20 22 57 48 45 4e 20 74 79 70 65 20 3d 20     "WHEN type = 
227bf 27 74 72 69 67 67 65 72 27 20 54 48 45 4e 20 73  'trigger' THEN s
227c0 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72 69  qlite_rename_tri
227c1 67 67 65 72 28 73 71 6c 2c 20 25 51 29 22 0a 20  gger(sql, %Q)". 
227c2 20 20 20 20 20 20 20 20 20 20 20 22 45 4c 53 45             "ELSE
227c3 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74   sqlite_rename_t
227c4 61 62 6c 65 28 73 71 6c 2c 20 25 51 29 20 45 4e  able(sql, %Q) EN
227c5 44 2c 20 22 0a 23 65 6e 64 69 66 0a 20 20 20 20  D, ".#endif.    
227c6 20 20 20 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20        "tbl_name 
227c7 3d 20 25 51 2c 20 22 0a 20 20 20 20 20 20 20 20  = %Q, ".        
227c8 20 20 22 6e 61 6d 65 20 3d 20 43 41 53 45 20 22    "name = CASE "
227c9 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 57 48  .            "WH
227ca 45 4e 20 74 79 70 65 3d 27 74 61 62 6c 65 27 20  EN type='table' 
227cb 54 48 45 4e 20 25 51 20 22 0a 20 20 20 20 20 20  THEN %Q ".      
227cc 20 20 20 20 20 20 22 57 48 45 4e 20 6e 61 6d 65        "WHEN name
227cd 20 4c 49 4b 45 20 27 73 71 6c 69 74 65 5f 61 75   LIKE 'sqlite_au
227ce 74 6f 69 6e 64 65 78 25 25 27 20 41 4e 44 20 74  toindex%%' AND t
227cf 79 70 65 3d 27 69 6e 64 65 78 27 20 54 48 45 4e  ype='index' THEN
227d0 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   ".             
227d1 22 27 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e 64  "'sqlite_autoind
227d2 65 78 5f 27 20 7c 7c 20 25 51 20 7c 7c 20 73 75  ex_' || %Q || su
227d3 62 73 74 72 28 6e 61 6d 65 2c 25 64 2b 31 38 29  bstr(name,%d+18)
227d4 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22   ".            "
227d5 45 4c 53 45 20 6e 61 6d 65 20 45 4e 44 20 22 0a  ELSE name END ".
227d6 20 20 20 20 20 20 22 57 48 45 52 45 20 74 62 6c        "WHERE tbl
227d7 5f 6e 61 6d 65 3d 25 51 20 41 4e 44 20 22 0a 20  _name=%Q AND ". 
227d8 20 20 20 20 20 20 20 20 20 22 28 74 79 70 65 3d           "(type=
227d9 27 74 61 62 6c 65 27 20 4f 52 20 74 79 70 65 3d  'table' OR type=
227da 27 69 6e 64 65 78 27 20 4f 52 20 74 79 70 65 3d  'index' OR type=
227db 27 74 72 69 67 67 65 72 27 29 3b 22 2c 20 0a 20  'trigger');", . 
227dc 20 20 20 20 20 7a 44 62 2c 20 53 43 48 45 4d 41       zDb, SCHEMA
227dd 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 7a 4e 61  _TABLE(iDb), zNa
227de 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65  me, zName, zName
227df 2c 20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  , .#ifndef SQLIT
227e0 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20  E_OMIT_TRIGGER. 
227e1 20 20 20 20 20 7a 4e 61 6d 65 2c 0a 23 65 6e 64       zName,.#end
227e2 69 66 0a 20 20 20 20 20 20 7a 4e 61 6d 65 2c 20  if.      zName, 
227e3 6e 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62 4e 61  nTabName, zTabNa
227e4 6d 65 0a 20 20 29 3b 0a 0a 23 69 66 6e 64 65 66  me.  );..#ifndef
227e5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
227e6 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 2f 2a 20  OINCREMENT.  /* 
227e7 49 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65  If the sqlite_se
227e8 71 75 65 6e 63 65 20 74 61 62 6c 65 20 65 78 69  quence table exi
227e9 73 74 73 20 69 6e 20 74 68 69 73 20 64 61 74 61  sts in this data
227ea 62 61 73 65 2c 20 74 68 65 6e 20 75 70 64 61 74  base, then updat
227eb 65 20 0a 20 20 2a 2a 20 69 74 20 77 69 74 68 20  e .  ** it with 
227ec 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61  the new table na
227ed 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  me..  */.  if( s
227ee 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
227ef 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 65 71 75  db, "sqlite_sequ
227f0 65 6e 63 65 22 2c 20 7a 44 62 29 20 29 7b 0a 20  ence", zDb) ){. 
227f1 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64     sqlite3Nested
227f2 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20  Parse(pParse,.  
227f3 20 20 20 20 20 20 22 55 50 44 41 54 45 20 5c 22        "UPDATE \"
227f4 25 77 5c 22 2e 73 71 6c 69 74 65 5f 73 65 71 75  %w\".sqlite_sequ
227f5 65 6e 63 65 20 73 65 74 20 6e 61 6d 65 20 3d 20  ence set name = 
227f6 25 51 20 57 48 45 52 45 20 6e 61 6d 65 20 3d 20  %Q WHERE name = 
227f7 25 51 22 2c 0a 20 20 20 20 20 20 20 20 7a 44 62  %Q",.        zDb
227f8 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a  , zName, pTab->z
227f9 4e 61 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  Name);.  }.#endi
227fa 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
227fb 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20  E_OMIT_TRIGGER. 
227fc 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65   /* If there are
227fd 20 54 45 4d 50 20 74 72 69 67 67 65 72 73 20 6f   TEMP triggers o
227fe 6e 20 74 68 69 73 20 74 61 62 6c 65 2c 20 6d 6f  n this table, mo
227ff 64 69 66 79 20 74 68 65 20 73 71 6c 69 74 65 5f  dify the sqlite_
22800 74 65 6d 70 5f 6d 61 73 74 65 72 0a 20 20 2a 2a  temp_master.  **
22801 20 74 61 62 6c 65 2e 20 44 6f 6e 27 74 20 64 6f   table. Don't do
22802 20 74 68 69 73 20 69 66 20 74 68 65 20 74 61 62   this if the tab
22803 6c 65 20 62 65 69 6e 67 20 41 4c 54 45 52 65 64  le being ALTERed
22804 20 69 73 20 69 74 73 65 6c 66 20 6c 6f 63 61 74   is itself locat
22805 65 64 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20 74  ed in.  ** the t
22806 65 6d 70 20 64 61 74 61 62 61 73 65 2e 0a 20 20  emp database..  
22807 2a 2f 0a 20 20 69 66 28 20 28 7a 57 68 65 72 65  */.  if( (zWhere
22808 3d 77 68 65 72 65 54 65 6d 70 54 72 69 67 67 65  =whereTempTrigge
22809 72 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 29  rs(pParse, pTab)
2280a 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  )!=0 ){.    sqli
2280b 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70  te3NestedParse(p
2280c 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20  Parse, .        
2280d 22 55 50 44 41 54 45 20 73 71 6c 69 74 65 5f 74  "UPDATE sqlite_t
2280e 65 6d 70 5f 6d 61 73 74 65 72 20 53 45 54 20 22  emp_master SET "
2280f 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 73 71  .            "sq
22810 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d  l = sqlite_renam
22811 65 5f 74 72 69 67 67 65 72 28 73 71 6c 2c 20 25  e_trigger(sql, %
22812 51 29 2c 20 22 0a 20 20 20 20 20 20 20 20 20 20  Q), ".          
22813 20 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 25 51    "tbl_name = %Q
22814 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22   ".            "
22815 57 48 45 52 45 20 25 73 3b 22 2c 20 7a 4e 61 6d  WHERE %s;", zNam
22816 65 2c 20 7a 4e 61 6d 65 2c 20 7a 57 68 65 72 65  e, zName, zWhere
22817 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
22818 46 72 65 65 28 64 62 2c 20 7a 57 68 65 72 65 29  Free(db, zWhere)
22819 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69  ;.  }.#endif..#i
2281a 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
2281b 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b  E_OMIT_FOREIGN_K
2281c 45 59 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  EY) && !defined(
2281d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
2281e 47 45 52 29 0a 20 20 69 66 28 20 64 62 2d 3e 66  GER).  if( db->f
2281f 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 6f 72 65  lags&SQLITE_Fore
22820 69 67 6e 4b 65 79 73 20 29 7b 0a 20 20 20 20 46  ignKeys ){.    F
22821 4b 65 79 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28  Key *p;.    for(
22822 70 3d 73 71 6c 69 74 65 33 46 6b 52 65 66 65 72  p=sqlite3FkRefer
22823 65 6e 63 65 73 28 70 54 61 62 29 3b 20 70 3b 20  ences(pTab); p; 
22824 70 3d 70 2d 3e 70 4e 65 78 74 54 6f 29 7b 0a 20  p=p->pNextTo){. 
22825 20 20 20 20 20 54 61 62 6c 65 20 2a 70 46 72 6f       Table *pFro
22826 6d 20 3d 20 70 2d 3e 70 46 72 6f 6d 3b 0a 20 20  m = p->pFrom;.  
22827 20 20 20 20 69 66 28 20 70 46 72 6f 6d 21 3d 70      if( pFrom!=p
22828 54 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 72  Tab ){.        r
22829 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65 6d 61  eloadTableSchema
2282a 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 46 72 6f  (pParse, p->pFro
2282b 6d 2c 20 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 29  m, pFrom->zName)
2282c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2282d 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
2282e 20 44 72 6f 70 20 61 6e 64 20 72 65 6c 6f 61 64   Drop and reload
2282f 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 74 61   the internal ta
22830 62 6c 65 20 73 63 68 65 6d 61 2e 20 2a 2f 0a 20  ble schema. */. 
22831 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65   reloadTableSche
22832 6d 61 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ma(pParse, pTab,
22833 20 7a 4e 61 6d 65 29 3b 0a 0a 65 78 69 74 5f 72   zName);..exit_r
22834 65 6e 61 6d 65 5f 74 61 62 6c 65 3a 0a 20 20 73  ename_table:.  s
22835 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c  qlite3SrcListDel
22836 65 74 65 28 64 62 2c 20 70 53 72 63 29 3b 0a 20  ete(db, pSrc);. 
22837 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
22838 62 2c 20 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a 0a 2f  b, zName);.}.../
22839 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
2283a 64 65 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  de to make sure 
2283b 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20  the file format 
2283c 6e 75 6d 62 65 72 20 69 73 20 61 74 20 6c 65 61  number is at lea
2283d 73 74 20 6d 69 6e 46 6f 72 6d 61 74 2e 0a 2a 2a  st minFormat..**
2283e 20 54 68 65 20 67 65 6e 65 72 61 74 65 64 20 63   The generated c
2283f 6f 64 65 20 77 69 6c 6c 20 69 6e 63 72 65 61 73  ode will increas
22840 65 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d 61  e the file forma
22841 74 20 6e 75 6d 62 65 72 20 69 66 20 6e 65 63 65  t number if nece
22842 73 73 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ssary..*/.SQLITE
22843 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
22844 6c 69 74 65 33 4d 69 6e 69 6d 75 6d 46 69 6c 65  lite3MinimumFile
22845 46 6f 72 6d 61 74 28 50 61 72 73 65 20 2a 70 50  Format(Parse *pP
22846 61 72 73 65 2c 20 69 6e 74 20 69 44 62 2c 20 69  arse, int iDb, i
22847 6e 74 20 6d 69 6e 46 6f 72 6d 61 74 29 7b 0a 20  nt minFormat){. 
22848 20 56 64 62 65 20 2a 76 3b 0a 20 20 76 20 3d 20   Vdbe *v;.  v = 
22849 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2284a 50 61 72 73 65 29 3b 0a 20 20 2f 2a 20 54 68 65  Parse);.  /* The
2284b 20 56 44 42 45 20 73 68 6f 75 6c 64 20 68 61 76   VDBE should hav
2284c 65 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  e been allocated
2284d 20 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75   before this rou
2284e 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a  tine is called..
2284f 20 20 2a 2a 20 49 66 20 74 68 61 74 20 61 6c 6c    ** If that all
22850 6f 63 61 74 69 6f 6e 20 66 61 69 6c 65 64 2c 20  ocation failed, 
22851 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 71 75  we would have qu
22852 69 74 20 62 65 66 6f 72 65 20 72 65 61 63 68 69  it before reachi
22853 6e 67 20 74 68 69 73 0a 20 20 2a 2a 20 70 6f 69  ng this.  ** poi
22854 6e 74 20 2a 2f 0a 20 20 69 66 28 20 41 4c 57 41  nt */.  if( ALWA
22855 59 53 28 76 29 20 29 7b 0a 20 20 20 20 69 6e 74  YS(v) ){.    int
22856 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
22857 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
22858 0a 20 20 20 20 69 6e 74 20 72 32 20 3d 20 73 71  .    int r2 = sq
22859 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
2285a 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 6e 74  pParse);.    int
2285b 20 6a 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   j1;.    sqlite3
2285c 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2285d 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69 44 62  _ReadCookie, iDb
2285e 2c 20 72 31 2c 20 42 54 52 45 45 5f 46 49 4c 45  , r1, BTREE_FILE
2285f 5f 46 4f 52 4d 41 54 29 3b 0a 20 20 20 20 73 71  _FORMAT);.    sq
22860 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72  lite3VdbeUsesBtr
22861 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20  ee(v, iDb);.    
22862 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22863 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
22864 20 6d 69 6e 46 6f 72 6d 61 74 2c 20 72 32 29 3b   minFormat, r2);
22865 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65  .    j1 = sqlite
22866 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
22867 50 5f 47 65 2c 20 72 32 2c 20 30 2c 20 72 31 29  P_Ge, r2, 0, r1)
22868 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
22869 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65  eAddOp3(v, OP_Se
2286a 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 42 54  tCookie, iDb, BT
2286b 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 2c  REE_FILE_FORMAT,
2286c 20 72 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   r2);.    sqlite
2286d 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2286e 20 6a 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   j1);.    sqlite
2286f 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
22870 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20  pParse, r1);.   
22871 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
22872 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
22873 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  2);.  }.}../*.**
22874 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
22875 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 61  s called after a
22876 6e 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20 2e  n "ALTER TABLE .
22877 2e 2e 20 41 44 44 22 20 73 74 61 74 65 6d 65 6e  .. ADD" statemen
22878 74 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 70 61  t.** has been pa
22879 72 73 65 64 2e 20 41 72 67 75 6d 65 6e 74 20 70  rsed. Argument p
2287a 43 6f 6c 44 65 66 20 63 6f 6e 74 61 69 6e 73 20  ColDef contains 
2287b 74 68 65 20 74 65 78 74 20 6f 66 20 74 68 65 20  the text of the 
2287c 6e 65 77 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 64 65  new.** column de
2287d 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  finition..**.** 
2287e 54 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  The Table struct
2287f 75 72 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  ure pParse->pNew
22880 54 61 62 6c 65 20 77 61 73 20 65 78 74 65 6e 64  Table was extend
22881 65 64 20 74 6f 20 69 6e 63 6c 75 64 65 0a 2a 2a  ed to include.**
22882 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20   the new column 
22883 64 75 72 69 6e 67 20 70 61 72 73 69 6e 67 2e 0a  during parsing..
22884 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
22885 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c  E void sqlite3Al
22886 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75  terFinishAddColu
22887 6d 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  mn(Parse *pParse
22888 2c 20 54 6f 6b 65 6e 20 2a 70 43 6f 6c 44 65 66  , Token *pColDef
22889 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77  ){.  Table *pNew
2288a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2288b 2a 20 43 6f 70 79 20 6f 66 20 70 50 61 72 73 65  * Copy of pParse
2288c 2d 3e 70 4e 65 77 54 61 62 6c 65 20 2a 2f 0a 20  ->pNewTable */. 
2288d 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20   Table *pTab;   
2288e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
2288f 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65  ble being altere
22890 64 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  d */.  int iDb; 
22891 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22892 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d   /* Database num
22893 62 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ber */.  const c
22894 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20  har *zDb;       
22895 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e     /* Database n
22896 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ame */.  const c
22897 68 61 72 20 2a 7a 54 61 62 3b 20 20 20 20 20 20  har *zTab;      
22898 20 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65     /* Table name
22899 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c   */.  char *zCol
2289a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2289b 2f 2a 20 4e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74  /* Null-terminat
2289c 65 64 20 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69  ed column defini
2289d 74 69 6f 6e 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e  tion */.  Column
2289e 20 2a 70 43 6f 6c 3b 20 20 20 20 20 20 20 20 20   *pCol;         
2289f 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 63      /* The new c
228a0 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 78 70 72 20  olumn */.  Expr 
228a1 2a 70 44 66 6c 74 3b 20 20 20 20 20 20 20 20 20  *pDflt;         
228a2 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20       /* Default 
228a3 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 6e 65  value for the ne
228a4 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 73 71  w column */.  sq
228a5 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
228a6 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
228a7 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
228a8 6f 6e 3b 20 2a 2f 0a 0a 20 20 64 62 20 3d 20 70  on; */..  db = p
228a9 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
228aa 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c   pParse->nErr ||
228ab 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
228ac 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 4e  d ) return;.  pN
228ad 65 77 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65  ew = pParse->pNe
228ae 77 54 61 62 6c 65 3b 0a 20 20 61 73 73 65 72 74  wTable;.  assert
228af 28 20 70 4e 65 77 20 29 3b 0a 0a 20 20 61 73 73  ( pNew );..  ass
228b0 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
228b1 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73  eHoldsAllMutexes
228b2 28 64 62 29 20 29 3b 0a 20 20 69 44 62 20 3d 20  (db) );.  iDb = 
228b3 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
228b4 6e 64 65 78 28 64 62 2c 20 70 4e 65 77 2d 3e 70  ndex(db, pNew->p
228b5 53 63 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20 3d  Schema);.  zDb =
228b6 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
228b7 61 6d 65 3b 0a 20 20 7a 54 61 62 20 3d 20 26 70  ame;.  zTab = &p
228b8 4e 65 77 2d 3e 7a 4e 61 6d 65 5b 31 36 5d 3b 20  New->zName[16]; 
228b9 20 2f 2a 20 53 6b 69 70 20 74 68 65 20 22 73 71   /* Skip the "sq
228ba 6c 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 22 20  lite_altertab_" 
228bb 70 72 65 66 69 78 20 6f 6e 20 74 68 65 20 6e 61  prefix on the na
228bc 6d 65 20 2a 2f 0a 20 20 70 43 6f 6c 20 3d 20 26  me */.  pCol = &
228bd 70 4e 65 77 2d 3e 61 43 6f 6c 5b 70 4e 65 77 2d  pNew->aCol[pNew-
228be 3e 6e 43 6f 6c 2d 31 5d 3b 0a 20 20 70 44 66 6c  >nCol-1];.  pDfl
228bf 74 20 3d 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 3b  t = pCol->pDflt;
228c0 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
228c1 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
228c2 54 61 62 2c 20 7a 44 62 29 3b 0a 20 20 61 73 73  Tab, zDb);.  ass
228c3 65 72 74 28 20 70 54 61 62 20 29 3b 0a 0a 23 69  ert( pTab );..#i
228c4 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
228c5 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
228c6 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20    /* Invoke the 
228c7 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61  authorization ca
228c8 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 20 20 69 66 28  llback. */.  if(
228c9 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
228ca 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
228cb 5f 41 4c 54 45 52 5f 54 41 42 4c 45 2c 20 7a 44  _ALTER_TABLE, zD
228cc 62 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  b, pTab->zName, 
228cd 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  0) ){.    return
228ce 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
228cf 2f 2a 20 49 66 20 74 68 65 20 64 65 66 61 75 6c  /* If the defaul
228d0 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  t value for the 
228d1 6e 65 77 20 63 6f 6c 75 6d 6e 20 77 61 73 20 73  new column was s
228d2 70 65 63 69 66 69 65 64 20 77 69 74 68 20 61 20  pecified with a 
228d3 0a 20 20 2a 2a 20 6c 69 74 65 72 61 6c 20 4e 55  .  ** literal NU
228d4 4c 4c 2c 20 74 68 65 6e 20 73 65 74 20 70 44 66  LL, then set pDf
228d5 6c 74 20 74 6f 20 30 2e 20 54 68 69 73 20 73 69  lt to 0. This si
228d6 6d 70 6c 69 66 69 65 73 20 63 68 65 63 6b 69 6e  mplifies checkin
228d7 67 0a 20 20 2a 2a 20 66 6f 72 20 61 6e 20 53 51  g.  ** for an SQ
228d8 4c 20 4e 55 4c 4c 20 64 65 66 61 75 6c 74 20 62  L NULL default b
228d9 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  elow..  */.  if(
228da 20 70 44 66 6c 74 20 26 26 20 70 44 66 6c 74 2d   pDflt && pDflt-
228db 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 7b 0a  >op==TK_NULL ){.
228dc 20 20 20 20 70 44 66 6c 74 20 3d 20 30 3b 0a 20      pDflt = 0;. 
228dd 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74   }..  /* Check t
228de 68 61 74 20 74 68 65 20 6e 65 77 20 63 6f 6c 75  hat the new colu
228df 6d 6e 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66  mn is not specif
228e0 69 65 64 20 61 73 20 50 52 49 4d 41 52 59 20 4b  ied as PRIMARY K
228e1 45 59 20 6f 72 20 55 4e 49 51 55 45 2e 0a 20 20  EY or UNIQUE..  
228e2 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  ** If there is a
228e3 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72   NOT NULL constr
228e4 61 69 6e 74 2c 20 74 68 65 6e 20 74 68 65 20 64  aint, then the d
228e5 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72  efault value for
228e6 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e   the.  ** column
228e7 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 4e 55 4c   must not be NUL
228e8 4c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 43  L..  */.  if( pC
228e9 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65 79 20 29 7b  ol->isPrimKey ){
228ea 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
228eb 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 43 61  rMsg(pParse, "Ca
228ec 6e 6e 6f 74 20 61 64 64 20 61 20 50 52 49 4d 41  nnot add a PRIMA
228ed 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 22 29 3b  RY KEY column");
228ee 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
228ef 0a 20 20 69 66 28 20 70 4e 65 77 2d 3e 70 49 6e  .  if( pNew->pIn
228f0 64 65 78 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  dex ){.    sqlit
228f1 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
228f2 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61  e, "Cannot add a
228f3 20 55 4e 49 51 55 45 20 63 6f 6c 75 6d 6e 22 29   UNIQUE column")
228f4 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
228f5 7d 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61  }.  if( (db->fla
228f6 67 73 26 53 51 4c 49 54 45 5f 46 6f 72 65 69 67  gs&SQLITE_Foreig
228f7 6e 4b 65 79 73 29 20 26 26 20 70 4e 65 77 2d 3e  nKeys) && pNew->
228f8 70 46 4b 65 79 20 26 26 20 70 44 66 6c 74 20 29  pFKey && pDflt )
228f9 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
228fa 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20  orMsg(pParse, . 
228fb 20 20 20 20 20 20 20 22 43 61 6e 6e 6f 74 20 61         "Cannot a
228fc 64 64 20 61 20 52 45 46 45 52 45 4e 43 45 53 20  dd a REFERENCES 
228fd 63 6f 6c 75 6d 6e 20 77 69 74 68 20 6e 6f 6e 2d  column with non-
228fe 4e 55 4c 4c 20 64 65 66 61 75 6c 74 20 76 61 6c  NULL default val
228ff 75 65 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ue");.    return
22900 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c  ;.  }.  if( pCol
22901 2d 3e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21 70 44  ->notNull && !pD
22902 66 6c 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  flt ){.    sqlit
22903 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
22904 65 2c 20 0a 20 20 20 20 20 20 20 20 22 43 61 6e  e, .        "Can
22905 6e 6f 74 20 61 64 64 20 61 20 4e 4f 54 20 4e 55  not add a NOT NU
22906 4c 4c 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 64  LL column with d
22907 65 66 61 75 6c 74 20 76 61 6c 75 65 20 4e 55 4c  efault value NUL
22908 4c 22 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  L");.    return;
22909 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72  .  }..  /* Ensur
2290a 65 20 74 68 65 20 64 65 66 61 75 6c 74 20 65 78  e the default ex
2290b 70 72 65 73 73 69 6f 6e 20 69 73 20 73 6f 6d 65  pression is some
2290c 74 68 69 6e 67 20 74 68 61 74 20 73 71 6c 69 74  thing that sqlit
2290d 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28  e3ValueFromExpr(
2290e 29 0a 20 20 2a 2a 20 63 61 6e 20 68 61 6e 64 6c  ).  ** can handl
2290f 65 20 28 69 2e 65 2e 20 6e 6f 74 20 43 55 52 52  e (i.e. not CURR
22910 45 4e 54 5f 54 49 4d 45 20 65 74 63 2e 29 0a 20  ENT_TIME etc.). 
22911 20 2a 2f 0a 20 20 69 66 28 20 70 44 66 6c 74 20   */.  if( pDflt 
22912 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  ){.    sqlite3_v
22913 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20 20 20  alue *pVal;.    
22914 69 66 28 20 73 71 6c 69 74 65 33 56 61 6c 75 65  if( sqlite3Value
22915 46 72 6f 6d 45 78 70 72 28 64 62 2c 20 70 44 66  FromExpr(db, pDf
22916 6c 74 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  lt, SQLITE_UTF8,
22917 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45   SQLITE_AFF_NONE
22918 2c 20 26 70 56 61 6c 29 20 29 7b 0a 20 20 20 20  , &pVal) ){.    
22919 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
2291a 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65  ed = 1;.      re
2291b 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  turn;.    }.    
2291c 69 66 28 20 21 70 56 61 6c 20 29 7b 0a 20 20 20  if( !pVal ){.   
2291d 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2291e 73 67 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e  sg(pParse, "Cann
2291f 6f 74 20 61 64 64 20 61 20 63 6f 6c 75 6d 6e 20  ot add a column 
22920 77 69 74 68 20 6e 6f 6e 2d 63 6f 6e 73 74 61 6e  with non-constan
22921 74 20 64 65 66 61 75 6c 74 22 29 3b 0a 20 20 20  t default");.   
22922 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
22923 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75  .    sqlite3Valu
22924 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 7d  eFree(pVal);.  }
22925 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79 20 74 68  ..  /* Modify th
22926 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  e CREATE TABLE s
22927 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 7a  tatement. */.  z
22928 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 53  Col = sqlite3DbS
22929 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68 61 72  trNDup(db, (char
2292a 2a 29 70 43 6f 6c 44 65 66 2d 3e 7a 2c 20 70 43  *)pColDef->z, pC
2292b 6f 6c 44 65 66 2d 3e 6e 29 3b 0a 20 20 69 66 28  olDef->n);.  if(
2292c 20 7a 43 6f 6c 20 29 7b 0a 20 20 20 20 63 68 61   zCol ){.    cha
2292d 72 20 2a 7a 45 6e 64 20 3d 20 26 7a 43 6f 6c 5b  r *zEnd = &zCol[
2292e 70 43 6f 6c 44 65 66 2d 3e 6e 2d 31 5d 3b 0a 20  pColDef->n-1];. 
2292f 20 20 20 77 68 69 6c 65 28 20 7a 45 6e 64 3e 7a     while( zEnd>z
22930 43 6f 6c 20 26 26 20 28 2a 7a 45 6e 64 3d 3d 27  Col && (*zEnd=='
22931 3b 27 20 7c 7c 20 73 71 6c 69 74 65 33 49 73 73  ;' || sqlite3Iss
22932 70 61 63 65 28 2a 7a 45 6e 64 29 29 20 29 7b 0a  pace(*zEnd)) ){.
22933 20 20 20 20 20 20 2a 7a 45 6e 64 2d 2d 20 3d 20        *zEnd-- = 
22934 27 5c 30 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20  '\0';.    }.    
22935 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
22936 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  se(pParse, .    
22937 20 20 20 20 22 55 50 44 41 54 45 20 5c 22 25 77      "UPDATE \"%w
22938 5c 22 2e 25 73 20 53 45 54 20 22 0a 20 20 20 20  \".%s SET ".    
22939 20 20 20 20 20 20 22 73 71 6c 20 3d 20 73 75 62        "sql = sub
2293a 73 74 72 28 73 71 6c 2c 31 2c 25 64 29 20 7c 7c  str(sql,1,%d) ||
2293b 20 27 2c 20 27 20 7c 7c 20 25 51 20 7c 7c 20 73   ', ' || %Q || s
2293c 75 62 73 74 72 28 73 71 6c 2c 25 64 29 20 22 0a  ubstr(sql,%d) ".
2293d 20 20 20 20 20 20 20 20 22 57 48 45 52 45 20 74          "WHERE t
2293e 79 70 65 20 3d 20 27 74 61 62 6c 65 27 20 41 4e  ype = 'table' AN
2293f 44 20 6e 61 6d 65 20 3d 20 25 51 22 2c 20 0a 20  D name = %Q", . 
22940 20 20 20 20 20 7a 44 62 2c 20 53 43 48 45 4d 41       zDb, SCHEMA
22941 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 70 4e 65  _TABLE(iDb), pNe
22942 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 2c  w->addColOffset,
22943 20 7a 43 6f 6c 2c 20 70 4e 65 77 2d 3e 61 64 64   zCol, pNew->add
22944 43 6f 6c 4f 66 66 73 65 74 2b 31 2c 0a 20 20 20  ColOffset+1,.   
22945 20 20 20 7a 54 61 62 0a 20 20 20 20 29 3b 0a 20     zTab.    );. 
22946 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
22947 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 7d 0a  (db, zCol);.  }.
22948 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64 65 66  .  /* If the def
22949 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20 74 68  ault value of th
2294a 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69 73 20  e new column is 
2294b 4e 55 4c 4c 2c 20 74 68 65 6e 20 73 65 74 20 74  NULL, then set t
2294c 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 66 6f 72  he file.  ** for
2294d 6d 61 74 20 74 6f 20 32 2e 20 49 66 20 74 68 65  mat to 2. If the
2294e 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f   default value o
2294f 66 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e  f the new column
22950 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 0a 20 20   is not NULL,.  
22951 2a 2a 20 74 68 65 20 66 69 6c 65 20 66 6f 72 6d  ** the file form
22952 61 74 20 62 65 63 6f 6d 65 73 20 33 2e 0a 20 20  at becomes 3..  
22953 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4d 69 6e 69  */.  sqlite3Mini
22954 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28 70 50  mumFileFormat(pP
22955 61 72 73 65 2c 20 69 44 62 2c 20 70 44 66 6c 74  arse, iDb, pDflt
22956 20 3f 20 33 20 3a 20 32 29 3b 0a 0a 20 20 2f 2a   ? 3 : 2);..  /*
22957 20 52 65 6c 6f 61 64 20 74 68 65 20 73 63 68 65   Reload the sche
22958 6d 61 20 6f 66 20 74 68 65 20 6d 6f 64 69 66 69  ma of the modifi
22959 65 64 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 72  ed table. */.  r
2295a 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65 6d 61  eloadTableSchema
2295b 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70  (pParse, pTab, p
2295c 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 7d 0a 0a  Tab->zName);.}..
2295d 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
2295e 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79  ion is called by
2295f 20 74 68 65 20 70 61 72 73 65 72 20 61 66 74 65   the parser afte
22960 72 20 74 68 65 20 74 61 62 6c 65 2d 6e 61 6d 65  r the table-name
22961 20 69 6e 0a 2a 2a 20 61 6e 20 22 41 4c 54 45 52   in.** an "ALTER
22962 20 54 41 42 4c 45 20 3c 74 61 62 6c 65 2d 6e 61   TABLE <table-na
22963 6d 65 3e 20 41 44 44 22 20 73 74 61 74 65 6d 65  me> ADD" stateme
22964 6e 74 20 69 73 20 70 61 72 73 65 64 2e 20 41 72  nt is parsed. Ar
22965 67 75 6d 65 6e 74 20 0a 2a 2a 20 70 53 72 63 20  gument .** pSrc 
22966 69 73 20 74 68 65 20 66 75 6c 6c 2d 6e 61 6d 65  is the full-name
22967 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 62 65   of the table be
22968 69 6e 67 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a  ing altered..**.
22969 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2296a 6d 61 6b 65 73 20 61 20 28 70 61 72 74 69 61 6c  makes a (partial
2296b 29 20 63 6f 70 79 20 6f 66 20 74 68 65 20 54 61  ) copy of the Ta
2296c 62 6c 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a  ble structure.**
2296d 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 62   for the table b
2296e 65 69 6e 67 20 61 6c 74 65 72 65 64 20 61 6e 64  eing altered and
2296f 20 73 65 74 73 20 50 61 72 73 65 2e 70 4e 65 77   sets Parse.pNew
22970 54 61 62 6c 65 20 74 6f 20 70 6f 69 6e 74 0a 2a  Table to point.*
22971 2a 20 74 6f 20 69 74 2e 20 52 6f 75 74 69 6e 65  * to it. Routine
22972 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20  s called by the 
22973 70 61 72 73 65 72 20 61 73 20 74 68 65 20 63 6f  parser as the co
22974 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 0a  lumn definition.
22975 2a 2a 20 69 73 20 70 61 72 73 65 64 20 28 69 2e  ** is parsed (i.
22976 65 2e 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c  e. sqlite3AddCol
22977 75 6d 6e 28 29 29 20 61 64 64 20 74 68 65 20 6e  umn()) add the n
22978 65 77 20 43 6f 6c 75 6d 6e 20 64 61 74 61 20 74  ew Column data t
22979 6f 20 0a 2a 2a 20 74 68 65 20 63 6f 70 79 2e 20  o .** the copy. 
2297a 54 68 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20  The copy of the 
2297b 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  Table structure 
2297c 69 73 20 64 65 6c 65 74 65 64 20 62 79 20 74 6f  is deleted by to
2297d 6b 65 6e 69 7a 65 2e 63 20 0a 2a 2a 20 61 66 74  kenize.c .** aft
2297e 65 72 20 70 61 72 73 69 6e 67 20 69 73 20 66 69  er parsing is fi
2297f 6e 69 73 68 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 6f  nished..**.** Ro
22980 75 74 69 6e 65 20 73 71 6c 69 74 65 33 41 6c 74  utine sqlite3Alt
22981 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d  erFinishAddColum
22982 6e 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c  n() will be call
22983 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65 0a 2a  ed to complete.*
22984 2a 20 63 6f 64 69 6e 67 20 74 68 65 20 22 41 4c  * coding the "AL
22985 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20 41 44  TER TABLE ... AD
22986 44 22 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f  D" statement..*/
22987 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
22988 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65  void sqlite3Alte
22989 72 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28  rBeginAddColumn(
2298a 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53  Parse *pParse, S
2298b 72 63 4c 69 73 74 20 2a 70 53 72 63 29 7b 0a 20  rcList *pSrc){. 
2298c 20 54 61 62 6c 65 20 2a 70 4e 65 77 3b 0a 20 20   Table *pNew;.  
2298d 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56  Table *pTab;.  V
2298e 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69 44  dbe *v;.  int iD
2298f 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  b;.  int i;.  in
22990 74 20 6e 41 6c 6c 6f 63 3b 0a 20 20 73 71 6c 69  t nAlloc;.  sqli
22991 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
22992 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 6b  ->db;..  /* Look
22993 20 75 70 20 74 68 65 20 74 61 62 6c 65 20 62 65   up the table be
22994 69 6e 67 20 61 6c 74 65 72 65 64 2e 20 2a 2f 0a  ing altered. */.
22995 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
22996 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 30 20 29  ->pNewTable==0 )
22997 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ;.  assert( sqli
22998 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c  te3BtreeHoldsAll
22999 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b 0a 20  Mutexes(db) );. 
2299a 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
2299b 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 65 78 69  ailed ) goto exi
2299c 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75  t_begin_add_colu
2299d 6d 6e 3b 0a 20 20 70 54 61 62 20 3d 20 73 71 6c  mn;.  pTab = sql
2299e 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
2299f 70 50 61 72 73 65 2c 20 30 2c 20 70 53 72 63 2d  pParse, 0, pSrc-
229a0 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 53 72  >a[0].zName, pSr
229a1 63 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73  c->a[0].zDatabas
229a2 65 29 3b 0a 20 20 69 66 28 20 21 70 54 61 62 20  e);.  if( !pTab 
229a3 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69  ) goto exit_begi
229a4 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 0a 23  n_add_column;..#
229a5 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
229a6 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
229a7 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
229a8 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73 71 6c  pTab) ){.    sql
229a9 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
229aa 72 73 65 2c 20 22 76 69 72 74 75 61 6c 20 74 61  rse, "virtual ta
229ab 62 6c 65 73 20 6d 61 79 20 6e 6f 74 20 62 65 20  bles may not be 
229ac 61 6c 74 65 72 65 64 22 29 3b 0a 20 20 20 20 67  altered");.    g
229ad 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61  oto exit_begin_a
229ae 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 23  dd_column;.  }.#
229af 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d 61 6b 65  endif..  /* Make
229b0 20 73 75 72 65 20 74 68 69 73 20 69 73 20 6e 6f   sure this is no
229b1 74 20 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20  t an attempt to 
229b2 41 4c 54 45 52 20 61 20 76 69 65 77 2e 20 2a 2f  ALTER a view. */
229b3 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65  .  if( pTab->pSe
229b4 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  lect ){.    sqli
229b5 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
229b6 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20  se, "Cannot add 
229b7 61 20 63 6f 6c 75 6d 6e 20 74 6f 20 61 20 76 69  a column to a vi
229b8 65 77 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ew");.    goto e
229b9 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f  xit_begin_add_co
229ba 6c 75 6d 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73  lumn;.  }..  ass
229bb 65 72 74 28 20 70 54 61 62 2d 3e 61 64 64 43 6f  ert( pTab->addCo
229bc 6c 4f 66 66 73 65 74 3e 30 20 29 3b 0a 20 20 69  lOffset>0 );.  i
229bd 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
229be 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54  maToIndex(db, pT
229bf 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 20  ab->pSchema);.. 
229c0 20 2f 2a 20 50 75 74 20 61 20 63 6f 70 79 20 6f   /* Put a copy o
229c1 66 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75  f the Table stru
229c2 63 74 20 69 6e 20 50 61 72 73 65 2e 70 4e 65 77  ct in Parse.pNew
229c3 54 61 62 6c 65 20 66 6f 72 20 74 68 65 0a 20 20  Table for the.  
229c4 2a 2a 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c  ** sqlite3AddCol
229c5 75 6d 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20 61  umn() function a
229c6 6e 64 20 66 72 69 65 6e 64 73 20 74 6f 20 6d 6f  nd friends to mo
229c7 64 69 66 79 2e 20 20 42 75 74 20 6d 6f 64 69 66  dify.  But modif
229c8 79 0a 20 20 2a 2a 20 74 68 65 20 6e 61 6d 65 20  y.  ** the name 
229c9 62 79 20 61 64 64 69 6e 67 20 61 6e 20 22 73 71  by adding an "sq
229ca 6c 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 22 20  lite_altertab_" 
229cb 70 72 65 66 69 78 2e 20 20 42 79 20 61 64 64 69  prefix.  By addi
229cc 6e 67 20 74 68 69 73 0a 20 20 2a 2a 20 70 72 65  ng this.  ** pre
229cd 66 69 78 2c 20 77 65 20 69 6e 73 75 72 65 20 74  fix, we insure t
229ce 68 61 74 20 74 68 65 20 6e 61 6d 65 20 77 69 6c  hat the name wil
229cf 6c 20 6e 6f 74 20 63 6f 6c 6c 69 64 65 20 77 69  l not collide wi
229d0 74 68 20 61 6e 20 65 78 69 73 74 69 6e 67 0a 20  th an existing. 
229d1 20 2a 2a 20 74 61 62 6c 65 20 62 65 63 61 75 73   ** table becaus
229d2 65 20 75 73 65 72 20 74 61 62 6c 65 20 61 72 65  e user table are
229d3 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20   not allowed to 
229d4 68 61 76 65 20 74 68 65 20 22 73 71 6c 69 74 65  have the "sqlite
229d5 5f 22 0a 20 20 2a 2a 20 70 72 65 66 69 78 20 6f  _".  ** prefix o
229d6 6e 20 74 68 65 69 72 20 6e 61 6d 65 2e 0a 20 20  n their name..  
229d7 2a 2f 0a 20 20 70 4e 65 77 20 3d 20 28 54 61 62  */.  pNew = (Tab
229d8 6c 65 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c  le*)sqlite3DbMal
229d9 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
229da 6f 66 28 54 61 62 6c 65 29 29 3b 0a 20 20 69 66  of(Table));.  if
229db 28 20 21 70 4e 65 77 20 29 20 67 6f 74 6f 20 65  ( !pNew ) goto e
229dc 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f  xit_begin_add_co
229dd 6c 75 6d 6e 3b 0a 20 20 70 50 61 72 73 65 2d 3e  lumn;.  pParse->
229de 70 4e 65 77 54 61 62 6c 65 20 3d 20 70 4e 65 77  pNewTable = pNew
229df 3b 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d  ;.  pNew->nRef =
229e0 20 31 3b 0a 20 20 70 4e 65 77 2d 3e 64 62 4d 65   1;.  pNew->dbMe
229e1 6d 20 3d 20 70 54 61 62 2d 3e 64 62 4d 65 6d 3b  m = pTab->dbMem;
229e2 0a 20 20 70 4e 65 77 2d 3e 6e 43 6f 6c 20 3d 20  .  pNew->nCol = 
229e3 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 61 73  pTab->nCol;.  as
229e4 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e 43 6f 6c  sert( pNew->nCol
229e5 3e 30 20 29 3b 0a 20 20 6e 41 6c 6c 6f 63 20 3d  >0 );.  nAlloc =
229e6 20 28 28 28 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31   (((pNew->nCol-1
229e7 29 2f 38 29 2a 38 29 2b 38 3b 0a 20 20 61 73 73  )/8)*8)+8;.  ass
229e8 65 72 74 28 20 6e 41 6c 6c 6f 63 3e 3d 70 4e 65  ert( nAlloc>=pNe
229e9 77 2d 3e 6e 43 6f 6c 20 26 26 20 6e 41 6c 6c 6f  w->nCol && nAllo
229ea 63 25 38 3d 3d 30 20 26 26 20 6e 41 6c 6c 6f 63  c%8==0 && nAlloc
229eb 2d 70 4e 65 77 2d 3e 6e 43 6f 6c 3c 38 20 29 3b  -pNew->nCol<8 );
229ec 0a 20 20 70 4e 65 77 2d 3e 61 43 6f 6c 20 3d 20  .  pNew->aCol = 
229ed 28 43 6f 6c 75 6d 6e 2a 29 73 71 6c 69 74 65 33  (Column*)sqlite3
229ee 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
229ef 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a   sizeof(Column)*
229f0 6e 41 6c 6c 6f 63 29 3b 0a 20 20 70 4e 65 77 2d  nAlloc);.  pNew-
229f1 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  >zName = sqlite3
229f2 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 73 71 6c  MPrintf(db, "sql
229f3 69 74 65 5f 61 6c 74 65 72 74 61 62 5f 25 73 22  ite_altertab_%s"
229f4 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
229f5 20 20 69 66 28 20 21 70 4e 65 77 2d 3e 61 43 6f    if( !pNew->aCo
229f6 6c 20 7c 7c 20 21 70 4e 65 77 2d 3e 7a 4e 61 6d  l || !pNew->zNam
229f7 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c  e ){.    db->mal
229f8 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
229f9 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67     goto exit_beg
229fa 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20  in_add_column;. 
229fb 20 7d 0a 20 20 6d 65 6d 63 70 79 28 70 4e 65 77   }.  memcpy(pNew
229fc 2d 3e 61 43 6f 6c 2c 20 70 54 61 62 2d 3e 61 43  ->aCol, pTab->aC
229fd 6f 6c 2c 20 73 69 7a 65 6f 66 28 43 6f 6c 75 6d  ol, sizeof(Colum
229fe 6e 29 2a 70 4e 65 77 2d 3e 6e 43 6f 6c 29 3b 0a  n)*pNew->nCol);.
229ff 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4e 65    for(i=0; i<pNe
22a00 77 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20  w->nCol; i++){. 
22a01 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20     Column *pCol 
22a02 3d 20 26 70 4e 65 77 2d 3e 61 43 6f 6c 5b 69 5d  = &pNew->aCol[i]
22a03 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d  ;.    pCol->zNam
22a04 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  e = sqlite3DbStr
22a05 44 75 70 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 4e  Dup(db, pCol->zN
22a06 61 6d 65 29 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e  ame);.    pCol->
22a07 7a 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 70  zColl = 0;.    p
22a08 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 30 3b 0a  Col->zType = 0;.
22a09 20 20 20 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 20      pCol->pDflt 
22a0a 3d 20 30 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a  = 0;.    pCol->z
22a0b 44 66 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Dflt = 0;.  }.  
22a0c 70 4e 65 77 2d 3e 70 53 63 68 65 6d 61 20 3d 20  pNew->pSchema = 
22a0d 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
22a0e 68 65 6d 61 3b 0a 20 20 70 4e 65 77 2d 3e 61 64  hema;.  pNew->ad
22a0f 64 43 6f 6c 4f 66 66 73 65 74 20 3d 20 70 54 61  dColOffset = pTa
22a10 62 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 3b  b->addColOffset;
22a11 0a 20 20 70 4e 65 77 2d 3e 6e 52 65 66 20 3d 20  .  pNew->nRef = 
22a12 31 3b 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 61  1;..  /* Begin a
22a13 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6e 64   transaction and
22a14 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 73   increment the s
22a15 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 20 20 2a  chema cookie.  *
22a16 2f 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e  /.  sqlite3Begin
22a17 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70  WriteOperation(p
22a18 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a  Parse, 0, iDb);.
22a19 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
22a1a 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
22a1b 69 66 28 20 21 76 20 29 20 67 6f 74 6f 20 65 78  if( !v ) goto ex
22a1c 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c  it_begin_add_col
22a1d 75 6d 6e 3b 0a 20 20 73 71 6c 69 74 65 33 43 68  umn;.  sqlite3Ch
22a1e 61 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73  angeCookie(pPars
22a1f 65 2c 20 69 44 62 29 3b 0a 0a 65 78 69 74 5f 62  e, iDb);..exit_b
22a20 65 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3a  egin_add_column:
22a21 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
22a22 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63  tDelete(db, pSrc
22a23 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23  );.  return;.}.#
22a24 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45  endif  /* SQLITE
22a25 5f 41 4c 54 45 52 5f 54 41 42 4c 45 20 2a 2f 0a  _ALTER_TABLE */.
22a26 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
22a27 20 45 6e 64 20 6f 66 20 61 6c 74 65 72 2e 63 20   End of alter.c 
22a28 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a29 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
22a2b 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
22a2c 20 42 65 67 69 6e 20 66 69 6c 65 20 61 6e 61 6c   Begin file anal
22a2d 79 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  yze.c **********
22a2e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
22a30 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4a 75 6c 79  ./*.** 2005 July
22a31 20 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74   8.**.** The aut
22a32 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
22a33 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
22a34 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
22a35 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
22a36 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
22a37 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
22a38 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
22a39 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
22a3a 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
22a3b 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
22a3c 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
22a3d 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
22a3e 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
22a3f 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
22a40 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
22a41 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
22a42 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
22a43 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a44 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a45 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a47 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
22a48 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 61 73 73  ontains code ass
22a49 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
22a4a 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64   ANALYZE command
22a4b 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64  ..**.** @(#) $Id
22a4c 3a 20 61 6e 61 6c 79 7a 65 2e 63 2c 76 20 31 2e  : analyze.c,v 1.
22a4d 35 32 20 32 30 30 39 2f 30 34 2f 31 36 20 31 37  52 2009/04/16 17
22a4e 3a 34 35 3a 34 38 20 64 72 68 20 45 78 70 20 24  :45:48 drh Exp $
22a4f 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
22a50 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a  TE_OMIT_ANALYZE.
22a51 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
22a52 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f  ine generates co
22a53 64 65 20 74 68 61 74 20 6f 70 65 6e 73 20 74 68  de that opens th
22a54 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74  e sqlite_stat1 t
22a55 61 62 6c 65 20 66 6f 72 0a 2a 2a 20 77 72 69 74  able for.** writ
22a56 69 6e 67 20 77 69 74 68 20 63 75 72 73 6f 72 20  ing with cursor 
22a57 69 53 74 61 74 43 75 72 2e 20 49 66 20 74 68 65  iStatCur. If the
22a58 20 6c 69 62 72 61 72 79 20 77 61 73 20 62 75 69   library was bui
22a59 6c 74 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 53  lt with the.** S
22a5a 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41  QLITE_ENABLE_STA
22a5b 54 32 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64  T2 macro defined
22a5c 2c 20 74 68 65 6e 20 74 68 65 20 73 71 6c 69 74  , then the sqlit
22a5d 65 5f 73 74 61 74 32 20 74 61 62 6c 65 20 69 73  e_stat2 table is
22a5e 0a 2a 2a 20 6f 70 65 6e 65 64 20 66 6f 72 20 77  .** opened for w
22a5f 72 69 74 69 6e 67 20 75 73 69 6e 67 20 63 75 72  riting using cur
22a60 73 6f 72 20 28 69 53 74 61 74 43 75 72 2b 31 29  sor (iStatCur+1)
22a61 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 73 71  .**.** If the sq
22a62 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65  lite_stat1 table
22a63 73 20 64 6f 65 73 20 6e 6f 74 20 70 72 65 76 69  s does not previ
22a64 6f 75 73 6c 79 20 65 78 69 73 74 2c 20 69 74 20  ously exist, it 
22a65 69 73 20 63 72 65 61 74 65 64 2e 0a 2a 2a 20 53  is created..** S
22a66 69 6d 69 6c 61 72 6c 79 2c 20 69 66 20 74 68 65  imilarly, if the
22a67 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20 74 61   sqlite_stat2 ta
22a68 62 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  ble does not exi
22a69 73 74 20 61 6e 64 20 74 68 65 20 6c 69 62 72 61  st and the libra
22a6a 72 79 0a 2a 2a 20 69 73 20 63 6f 6d 70 69 6c 65  ry.** is compile
22a6b 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 45 4e  d with SQLITE_EN
22a6c 41 42 4c 45 5f 53 54 41 54 32 20 64 65 66 69 6e  ABLE_STAT2 defin
22a6d 65 64 2c 20 69 74 20 69 73 20 63 72 65 61 74 65  ed, it is create
22a6e 64 2e 20 0a 2a 2a 0a 2a 2a 20 41 72 67 75 6d 65  d. .**.** Argume
22a6f 6e 74 20 7a 57 68 65 72 65 20 6d 61 79 20 62 65  nt zWhere may be
22a70 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
22a71 62 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e  buffer containin
22a72 67 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 2c 0a  g a table name,.
22a73 2a 2a 20 6f 72 20 69 74 20 6d 61 79 20 62 65 20  ** or it may be 
22a74 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20  a NULL pointer. 
22a75 49 66 20 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c  If it is not NUL
22a76 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 65 6e 74 72  L, then all entr
22a77 69 65 73 20 69 6e 0a 2a 2a 20 74 68 65 20 73 71  ies in.** the sq
22a78 6c 69 74 65 5f 73 74 61 74 31 20 61 6e 64 20 28  lite_stat1 and (
22a79 69 66 20 61 70 70 6c 69 63 61 62 6c 65 29 20 73  if applicable) s
22a7a 71 6c 69 74 65 5f 73 74 61 74 32 20 74 61 62 6c  qlite_stat2 tabl
22a7b 65 73 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a  es associated.**
22a7c 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65 64 20   with the named 
22a7d 74 61 62 6c 65 20 61 72 65 20 64 65 6c 65 74 65  table are delete
22a7e 64 2e 20 49 66 20 7a 57 68 65 72 65 3d 3d 30 2c  d. If zWhere==0,
22a7f 20 74 68 65 6e 20 63 6f 64 65 20 69 73 20 67 65   then code is ge
22a80 6e 65 72 61 74 65 64 0a 2a 2a 20 74 6f 20 64 65  nerated.** to de
22a81 6c 65 74 65 20 61 6c 6c 20 73 74 61 74 20 74 61  lete all stat ta
22a82 62 6c 65 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a  ble entries..*/.
22a83 73 74 61 74 69 63 20 76 6f 69 64 20 6f 70 65 6e  static void open
22a84 53 74 61 74 54 61 62 6c 65 28 0a 20 20 50 61 72  StatTable(.  Par
22a85 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
22a86 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
22a87 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74  context */.  int
22a88 20 69 44 62 2c 20 20 20 20 20 20 20 20 20 20 20   iDb,           
22a89 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
22a8a 62 61 73 65 20 77 65 20 61 72 65 20 6c 6f 6f 6b  base we are look
22a8b 69 6e 67 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20  ing in */.  int 
22a8c 69 53 74 61 74 43 75 72 2c 20 20 20 20 20 20 20  iStatCur,       
22a8d 20 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20      /* Open the 
22a8e 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62  sqlite_stat1 tab
22a8f 6c 65 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f  le on this curso
22a90 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  r */.  const cha
22a91 72 20 2a 7a 57 68 65 72 65 20 20 20 20 20 20 2f  r *zWhere      /
22a92 2a 20 44 65 6c 65 74 65 20 65 6e 74 72 69 65 73  * Delete entries
22a93 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
22a94 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 29   this table */.)
22a95 7b 0a 20 20 73 74 61 74 69 63 20 73 74 72 75 63  {.  static struc
22a96 74 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  t {.    const ch
22a97 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 20 20 63  ar *zName;.    c
22a98 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 73  onst char *zCols
22a99 3b 0a 20 20 7d 20 61 54 61 62 6c 65 5b 5d 20 3d  ;.  } aTable[] =
22a9a 20 7b 0a 20 20 20 20 7b 20 22 73 71 6c 69 74 65   {.    { "sqlite
22a9b 5f 73 74 61 74 31 22 2c 20 22 74 62 6c 2c 69 64  _stat1", "tbl,id
22a9c 78 2c 73 74 61 74 22 20 7d 2c 0a 23 69 66 64 65  x,stat" },.#ifde
22a9d 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
22a9e 53 54 41 54 32 0a 20 20 20 20 7b 20 22 73 71 6c  STAT2.    { "sql
22a9f 69 74 65 5f 73 74 61 74 32 22 2c 20 22 74 62 6c  ite_stat2", "tbl
22aa0 2c 69 64 78 2c 73 61 6d 70 6c 65 6e 6f 2c 73 61  ,idx,sampleno,sa
22aa1 6d 70 6c 65 22 20 7d 2c 0a 23 65 6e 64 69 66 0a  mple" },.#endif.
22aa2 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 61 52 6f 6f    };..  int aRoo
22aa3 74 5b 5d 20 3d 20 7b 30 2c 20 30 7d 3b 0a 20 20  t[] = {0, 0};.  
22aa4 75 38 20 61 43 72 65 61 74 65 54 62 6c 5b 5d 20  u8 aCreateTbl[] 
22aa5 3d 20 7b 30 2c 20 30 7d 3b 0a 0a 20 20 69 6e 74  = {0, 0};..  int
22aa6 20 69 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   i;.  sqlite3 *d
22aa7 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
22aa8 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 56 64 62    Db *pDb;.  Vdb
22aa9 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65  e *v = sqlite3Ge
22aaa 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
22aab 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75   if( v==0 ) retu
22aac 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  rn;.  assert( sq
22aad 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41  lite3BtreeHoldsA
22aae 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b  llMutexes(db) );
22aaf 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
22ab0 65 33 56 64 62 65 44 62 28 76 29 3d 3d 64 62 20  e3VdbeDb(v)==db 
22ab1 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e  );.  pDb = &db->
22ab2 61 44 62 5b 69 44 62 5d 3b 0a 0a 20 20 66 6f 72  aDb[iDb];..  for
22ab3 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a  (i=0; i<ArraySiz
22ab4 65 28 61 54 61 62 6c 65 29 3b 20 69 2b 2b 29 7b  e(aTable); i++){
22ab5 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
22ab6 2a 7a 54 61 62 20 3d 20 61 54 61 62 6c 65 5b 69  *zTab = aTable[i
22ab7 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 54 61 62  ].zName;.    Tab
22ab8 6c 65 20 2a 70 53 74 61 74 3b 0a 20 20 20 20 69  le *pStat;.    i
22ab9 66 28 20 28 70 53 74 61 74 20 3d 20 73 71 6c 69  f( (pStat = sqli
22aba 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c  te3FindTable(db,
22abb 20 7a 54 61 62 2c 20 70 44 62 2d 3e 7a 4e 61 6d   zTab, pDb->zNam
22abc 65 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  e))==0 ){.      
22abd 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74  /* The sqlite_st
22abe 61 74 5b 31 32 5d 20 74 61 62 6c 65 20 64 6f 65  at[12] table doe
22abf 73 20 6e 6f 74 20 65 78 69 73 74 2e 20 43 72 65  s not exist. Cre
22ac0 61 74 65 20 69 74 2e 20 4e 6f 74 65 20 74 68 61  ate it. Note tha
22ac1 74 20 61 20 0a 20 20 20 20 20 20 2a 2a 20 73 69  t a .      ** si
22ac2 64 65 2d 65 66 66 65 63 74 20 6f 66 20 74 68 65  de-effect of the
22ac3 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
22ac4 61 74 65 6d 65 6e 74 20 69 73 20 74 6f 20 6c 65  atement is to le
22ac5 61 76 65 20 74 68 65 20 72 6f 6f 74 70 61 67 65  ave the rootpage
22ac6 20 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68   .      ** of th
22ac7 65 20 6e 65 77 20 74 61 62 6c 65 20 69 6e 20 72  e new table in r
22ac8 65 67 69 73 74 65 72 20 70 50 61 72 73 65 2d 3e  egister pParse->
22ac9 72 65 67 52 6f 6f 74 2e 20 54 68 69 73 20 69 73  regRoot. This is
22aca 20 69 6d 70 6f 72 74 61 6e 74 20 0a 20 20 20 20   important .    
22acb 20 20 2a 2a 20 62 65 63 61 75 73 65 20 74 68 65    ** because the
22acc 20 4f 70 65 6e 57 72 69 74 65 20 6f 70 63 6f 64   OpenWrite opcod
22acd 65 20 62 65 6c 6f 77 20 77 69 6c 6c 20 62 65 20  e below will be 
22ace 6e 65 65 64 69 6e 67 20 69 74 2e 20 2a 2f 0a 20  needing it. */. 
22acf 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74       sqlite3Nest
22ad0 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a  edParse(pParse,.
22ad1 20 20 20 20 20 20 20 20 20 20 22 43 52 45 41 54            "CREAT
22ad2 45 20 54 41 42 4c 45 20 25 51 2e 25 73 28 25 73  E TABLE %Q.%s(%s
22ad3 29 22 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20  )", pDb->zName, 
22ad4 7a 54 61 62 2c 20 61 54 61 62 6c 65 5b 69 5d 2e  zTab, aTable[i].
22ad5 7a 43 6f 6c 73 0a 20 20 20 20 20 20 29 3b 0a 20  zCols.      );. 
22ad6 20 20 20 20 20 61 52 6f 6f 74 5b 69 5d 20 3d 20       aRoot[i] = 
22ad7 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 3b  pParse->regRoot;
22ad8 0a 20 20 20 20 20 20 61 43 72 65 61 74 65 54 62  .      aCreateTb
22ad9 6c 5b 69 5d 20 3d 20 31 3b 0a 20 20 20 20 7d 65  l[i] = 1;.    }e
22ada 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68  lse{.      /* Th
22adb 65 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20  e table already 
22adc 65 78 69 73 74 73 2e 20 49 66 20 7a 57 68 65 72  exists. If zWher
22add 65 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 64  e is not NULL, d
22ade 65 6c 65 74 65 20 61 6c 6c 20 65 6e 74 72 69 65  elete all entrie
22adf 73 20 0a 20 20 20 20 20 20 2a 2a 20 61 73 73 6f  s .      ** asso
22ae0 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
22ae1 74 61 62 6c 65 20 7a 57 68 65 72 65 2e 20 49 66  table zWhere. If
22ae2 20 7a 57 68 65 72 65 20 69 73 20 4e 55 4c 4c 2c   zWhere is NULL,
22ae3 20 64 65 6c 65 74 65 20 74 68 65 0a 20 20 20 20   delete the.    
22ae4 20 20 2a 2a 20 65 6e 74 69 72 65 20 63 6f 6e 74    ** entire cont
22ae5 65 6e 74 73 20 6f 66 20 74 68 65 20 74 61 62 6c  ents of the tabl
22ae6 65 2e 20 2a 2f 0a 20 20 20 20 20 20 61 52 6f 6f  e. */.      aRoo
22ae7 74 5b 69 5d 20 3d 20 70 53 74 61 74 2d 3e 74 6e  t[i] = pStat->tn
22ae8 75 6d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  um;.      sqlite
22ae9 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73  3TableLock(pPars
22aea 65 2c 20 69 44 62 2c 20 61 52 6f 6f 74 5b 69 5d  e, iDb, aRoot[i]
22aeb 2c 20 31 2c 20 7a 54 61 62 29 3b 0a 20 20 20 20  , 1, zTab);.    
22aec 20 20 69 66 28 20 7a 57 68 65 72 65 20 29 7b 0a    if( zWhere ){.
22aed 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e          sqlite3N
22aee 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
22aef 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 22 44  e,.           "D
22af0 45 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 25 73  ELETE FROM %Q.%s
22af1 20 57 48 45 52 45 20 74 62 6c 3d 25 51 22 2c 20   WHERE tbl=%Q", 
22af2 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 54 61 62  pDb->zName, zTab
22af3 2c 20 7a 57 68 65 72 65 0a 20 20 20 20 20 20 20  , zWhere.       
22af4 20 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   );.      }else{
22af5 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
22af6 73 71 6c 69 74 65 5f 73 74 61 74 5b 31 32 5d 20  sqlite_stat[12] 
22af7 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78  table already ex
22af8 69 73 74 73 2e 20 20 44 65 6c 65 74 65 20 61 6c  ists.  Delete al
22af9 6c 20 72 6f 77 73 2e 20 2a 2f 0a 20 20 20 20 20  l rows. */.     
22afa 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22afb 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 65 61 72  dOp2(v, OP_Clear
22afc 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 69 44 62 29  , aRoot[i], iDb)
22afd 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
22afe 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74    }..  /* Open t
22aff 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 5b 31  he sqlite_stat[1
22b00 32 5d 20 74 61 62 6c 65 73 20 66 6f 72 20 77 72  2] tables for wr
22b01 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 66 6f 72 28  iting. */.  for(
22b02 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65  i=0; i<ArraySize
22b03 28 61 54 61 62 6c 65 29 3b 20 69 2b 2b 29 7b 0a  (aTable); i++){.
22b04 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22b05 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp3(v, OP_Open
22b06 57 72 69 74 65 2c 20 69 53 74 61 74 43 75 72 2b  Write, iStatCur+
22b07 69 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20 69 44 62  i, aRoot[i], iDb
22b08 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
22b09 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31  beChangeP4(v, -1
22b0a 2c 20 28 63 68 61 72 20 2a 29 33 2c 20 50 34 5f  , (char *)3, P4_
22b0b 49 4e 54 33 32 29 3b 0a 20 20 20 20 73 71 6c 69  INT32);.    sqli
22b0c 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
22b0d 76 2c 20 61 43 72 65 61 74 65 54 62 6c 5b 69 5d  v, aCreateTbl[i]
22b0e 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
22b0f 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
22b10 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20   do an analysis 
22b11 6f 66 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 61  of all indices a
22b12 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a  ssociated with.*
22b13 2a 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65  * a single table
22b14 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
22b15 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65   analyzeOneTable
22b16 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
22b17 65 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63  e,   /* Parser c
22b18 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c  ontext */.  Tabl
22b19 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a 20  e *pTab,     /* 
22b1a 54 61 62 6c 65 20 77 68 6f 73 65 20 69 6e 64 69  Table whose indi
22b1b 63 65 73 20 61 72 65 20 74 6f 20 62 65 20 61 6e  ces are to be an
22b1c 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20  alyzed */.  int 
22b1d 69 53 74 61 74 43 75 72 2c 20 20 20 20 2f 2a 20  iStatCur,    /* 
22b1e 49 6e 64 65 78 20 6f 66 20 56 64 62 65 43 75 72  Index of VdbeCur
22b1f 73 6f 72 20 74 68 61 74 20 77 72 69 74 65 73 20  sor that writes 
22b20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31  the sqlite_stat1
22b21 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
22b22 69 4d 65 6d 20 20 20 20 20 20 20 20 20 2f 2a 20  iMem         /* 
22b23 41 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79  Available memory
22b24 20 6c 6f 63 61 74 69 6f 6e 73 20 62 65 67 69 6e   locations begin
22b25 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 73 71   here */.){.  sq
22b26 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
22b27 73 65 2d 3e 64 62 3b 20 20 20 20 2f 2a 20 44 61  se->db;    /* Da
22b28 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f  tabase handle */
22b29 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20  .  Index *pIdx; 
22b2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22b2b 2f 2a 20 41 6e 20 69 6e 64 65 78 20 74 6f 20 62  /* An index to b
22b2c 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 2a 2f  eing analyzed */
22b2d 0a 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b 20  .  int iIdxCur; 
22b2e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22b2f 2f 2a 20 43 75 72 73 6f 72 20 6f 70 65 6e 20 6f  /* Cursor open o
22b30 6e 20 69 6e 64 65 78 20 62 65 69 6e 67 20 61 6e  n index being an
22b31 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 56 64 62 65  alyzed */.  Vdbe
22b32 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20   *v;            
22b33 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
22b34 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  virtual machine 
22b35 62 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20 2a  being built up *
22b36 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20  /.  int i;      
22b37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22b38 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
22b39 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 4f 66 4c   */.  int topOfL
22b3a 6f 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  oop;            
22b3b 20 20 20 2f 2a 20 54 68 65 20 74 6f 70 20 6f 66     /* The top of
22b3c 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69   the loop */.  i
22b3d 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20 20  nt endOfLoop;   
22b3e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
22b3f 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 6f  he end of the lo
22b40 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  op */.  int addr
22b41 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
22b42 20 20 20 20 20 2f 2a 20 54 68 65 20 61 64 64 72       /* The addr
22b43 65 73 73 20 6f 66 20 61 6e 20 69 6e 73 74 72 75  ess of an instru
22b44 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  ction */.  int i
22b45 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
22b46 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
22b47 20 6f 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e   of database con
22b48 74 61 69 6e 69 6e 67 20 70 54 61 62 20 2a 2f 0a  taining pTab */.
22b49 20 20 69 6e 74 20 72 65 67 54 61 62 6e 61 6d 65    int regTabname
22b4a 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 2f   = iMem++;     /
22b4b 2a 20 52 65 67 69 73 74 65 72 20 63 6f 6e 74 61  * Register conta
22b4c 69 6e 69 6e 67 20 74 61 62 6c 65 20 6e 61 6d 65  ining table name
22b4d 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 49 64 78   */.  int regIdx
22b4e 6e 61 6d 65 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20  name = iMem++;  
22b4f 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 63     /* Register c
22b50 6f 6e 74 61 69 6e 69 6e 67 20 69 6e 64 65 78 20  ontaining index 
22b51 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65  name */.  int re
22b52 67 53 61 6d 70 6c 65 6e 6f 20 3d 20 69 4d 65 6d  gSampleno = iMem
22b53 2b 2b 3b 20 20 20 20 2f 2a 20 52 65 67 69 73 74  ++;    /* Regist
22b54 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6e 65  er containing ne
22b55 78 74 20 73 61 6d 70 6c 65 20 6e 75 6d 62 65 72  xt sample number
22b56 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 43 6f 6c   */.  int regCol
22b57 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20   = iMem++;      
22b58 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66     /* Content of
22b59 20 61 20 63 6f 6c 75 6d 6e 20 61 6e 61 6c 79 7a   a column analyz
22b5a 65 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  ed table */.  in
22b5b 74 20 72 65 67 52 65 63 20 3d 20 69 4d 65 6d 2b  t regRec = iMem+
22b5c 2b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  +;         /* Re
22b5d 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 63  gister holding c
22b5e 6f 6d 70 6c 65 74 65 64 20 72 65 63 6f 72 64 20  ompleted record 
22b5f 2a 2f 0a 20 20 69 6e 74 20 72 65 67 54 65 6d 70  */.  int regTemp
22b60 20 3d 20 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20   = iMem++;      
22b61 20 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 75    /* Temporary u
22b62 73 65 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20  se register */. 
22b63 20 69 6e 74 20 72 65 67 52 6f 77 69 64 20 3d 20   int regRowid = 
22b64 69 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 2f 2a  iMem++;       /*
22b65 20 52 6f 77 69 64 20 66 6f 72 20 74 68 65 20 69   Rowid for the i
22b66 6e 73 65 72 74 65 64 20 72 65 63 6f 72 64 20 2a  nserted record *
22b67 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  /..#ifdef SQLITE
22b68 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 20 20  _ENABLE_STAT2.  
22b69 69 6e 74 20 72 65 67 54 65 6d 70 32 20 3d 20 69  int regTemp2 = i
22b6a 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 2f 2a 20  Mem++;       /* 
22b6b 54 65 6d 70 6f 72 61 72 79 20 75 73 65 20 72 65  Temporary use re
22b6c 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  gister */.  int 
22b6d 72 65 67 53 61 6d 70 6c 65 72 65 63 6e 6f 20 3d  regSamplerecno =
22b6e 20 69 4d 65 6d 2b 2b 3b 20 2f 2a 20 49 6e 64 65   iMem++; /* Inde
22b6f 78 20 6f 66 20 6e 65 78 74 20 73 61 6d 70 6c 65  x of next sample
22b70 20 74 6f 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20   to record */.  
22b71 69 6e 74 20 72 65 67 52 65 63 6e 6f 20 3d 20 69  int regRecno = i
22b72 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 2f 2a 20  Mem++;       /* 
22b73 43 75 72 72 65 6e 74 20 73 61 6d 70 6c 65 20 69  Current sample i
22b74 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65  ndex */.  int re
22b75 67 4c 61 73 74 20 3d 20 69 4d 65 6d 2b 2b 3b 20  gLast = iMem++; 
22b76 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
22b77 6f 66 20 6c 61 73 74 20 73 61 6d 70 6c 65 20 74  of last sample t
22b78 6f 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e  o record */.  in
22b79 74 20 72 65 67 46 69 72 73 74 20 3d 20 69 4d 65  t regFirst = iMe
22b7a 6d 2b 2b 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e  m++;       /* In
22b7b 64 65 78 20 6f 66 20 66 69 72 73 74 20 73 61 6d  dex of first sam
22b7c 70 6c 65 20 74 6f 20 72 65 63 6f 72 64 20 2a 2f  ple to record */
22b7d 0a 23 65 6e 64 69 66 0a 0a 20 20 76 20 3d 20 73  .#endif..  v = s
22b7e 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
22b7f 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d  arse);.  if( v==
22b80 30 20 7c 7c 20 4e 45 56 45 52 28 70 54 61 62 3d  0 || NEVER(pTab=
22b81 3d 30 29 20 7c 7c 20 70 54 61 62 2d 3e 70 49 6e  =0) || pTab->pIn
22b82 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  dex==0 ){.    /*
22b83 20 44 6f 20 6e 6f 20 61 6e 61 6c 79 73 69 73 20   Do no analysis 
22b84 66 6f 72 20 74 61 62 6c 65 73 20 74 68 61 74 20  for tables that 
22b85 68 61 76 65 20 6e 6f 20 69 6e 64 69 63 65 73 20  have no indices 
22b86 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  */.    return;. 
22b87 20 7d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c   }.  assert( sql
22b88 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c  ite3BtreeHoldsAl
22b89 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b 0a  lMutexes(db) );.
22b8a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
22b8b 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
22b8c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
22b8d 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
22b8e 30 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  0 );.#ifndef SQL
22b8f 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49  ITE_OMIT_AUTHORI
22b90 5a 41 54 49 4f 4e 0a 20 20 69 66 28 20 73 71 6c  ZATION.  if( sql
22b91 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
22b92 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4e 41  arse, SQLITE_ANA
22b93 4c 59 5a 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  LYZE, pTab->zNam
22b94 65 2c 20 30 2c 0a 20 20 20 20 20 20 64 62 2d 3e  e, 0,.      db->
22b95 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 20 29  aDb[iDb].zName )
22b96 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
22b97 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
22b98 20 45 73 74 61 62 6c 69 73 68 20 61 20 72 65 61   Establish a rea
22b99 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61  d-lock on the ta
22b9a 62 6c 65 20 61 74 20 74 68 65 20 73 68 61 72 65  ble at the share
22b9b 64 2d 63 61 63 68 65 20 6c 65 76 65 6c 2e 20 2a  d-cache level. *
22b9c 2f 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65  /.  sqlite3Table
22b9d 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69 44 62  Lock(pParse, iDb
22b9e 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 30 2c  , pTab->tnum, 0,
22b9f 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a   pTab->zName);..
22ba0 20 20 69 49 64 78 43 75 72 20 3d 20 70 50 61 72    iIdxCur = pPar
22ba1 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 66 6f  se->nTab++;.  fo
22ba2 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
22ba3 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
22ba4 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
22ba5 20 20 69 6e 74 20 6e 43 6f 6c 20 3d 20 70 49 64    int nCol = pId
22ba6 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  x->nColumn;.    
22ba7 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20  KeyInfo *pKey = 
22ba8 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69  sqlite3IndexKeyi
22ba9 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64 78  nfo(pParse, pIdx
22baa 29 3b 0a 0a 20 20 20 20 69 66 28 20 69 4d 65 6d  );..    if( iMem
22bab 2b 31 2b 28 6e 43 6f 6c 2a 32 29 3e 70 50 61 72  +1+(nCol*2)>pPar
22bac 73 65 2d 3e 6e 4d 65 6d 20 29 7b 0a 20 20 20 20  se->nMem ){.    
22bad 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d    pParse->nMem =
22bae 20 69 4d 65 6d 2b 31 2b 28 6e 43 6f 6c 2a 32 29   iMem+1+(nCol*2)
22baf 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
22bb0 4f 70 65 6e 20 61 20 63 75 72 73 6f 72 20 74 6f  Open a cursor to
22bb1 20 74 68 65 20 69 6e 64 65 78 20 74 6f 20 62 65   the index to be
22bb2 20 61 6e 61 6c 79 7a 65 64 2e 20 2a 2f 0a 20 20   analyzed. */.  
22bb3 20 20 61 73 73 65 72 74 28 20 69 44 62 3d 3d 73    assert( iDb==s
22bb4 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
22bb5 64 65 78 28 64 62 2c 20 70 49 64 78 2d 3e 70 53  dex(db, pIdx->pS
22bb6 63 68 65 6d 61 29 20 29 3b 0a 20 20 20 20 73 71  chema) );.    sq
22bb7 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
22bb8 76 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20  v, OP_OpenRead, 
22bb9 69 49 64 78 43 75 72 2c 20 70 49 64 78 2d 3e 74  iIdxCur, pIdx->t
22bba 6e 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20  num, iDb,.      
22bbb 20 20 28 63 68 61 72 20 2a 29 70 4b 65 79 2c 20    (char *)pKey, 
22bbc 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
22bbd 46 46 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d  FF);.    VdbeCom
22bbe 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70  ment((v, "%s", p
22bbf 49 64 78 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 0a 20  Idx->zName));.. 
22bc0 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20 74     /* Populate t
22bc1 68 65 20 72 65 67 69 73 74 65 72 73 20 63 6f 6e  he registers con
22bc2 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c  taining the tabl
22bc3 65 20 61 6e 64 20 69 6e 64 65 78 20 6e 61 6d 65  e and index name
22bc4 73 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54  s. */.    if( pT
22bc5 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 70 49 64 78  ab->pIndex==pIdx
22bc6 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
22bc7 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
22bc8 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65  P_String8, 0, re
22bc9 67 54 61 62 6e 61 6d 65 2c 20 30 2c 20 70 54 61  gTabname, 0, pTa
22bca 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  b->zName, 0);.  
22bcb 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
22bcc 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
22bcd 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67 49  String8, 0, regI
22bce 64 78 6e 61 6d 65 2c 20 30 2c 20 70 49 64 78 2d  dxname, 0, pIdx-
22bcf 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 0a 23 69 66  >zName, 0);..#if
22bd0 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
22bd1 45 5f 53 54 41 54 32 0a 0a 20 20 20 20 2f 2a 20  E_STAT2..    /* 
22bd2 49 66 20 74 68 69 73 20 69 74 65 72 61 74 69 6f  If this iteratio
22bd3 6e 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69 73  n of the loop is
22bd4 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65   generating code
22bd5 20 74 6f 20 61 6e 61 6c 79 7a 65 20 74 68 65 0a   to analyze the.
22bd6 20 20 20 20 2a 2a 20 66 69 72 73 74 20 69 6e 64      ** first ind
22bd7 65 78 20 69 6e 20 74 68 65 20 70 54 61 62 2d 3e  ex in the pTab->
22bd8 70 49 6e 64 65 78 20 6c 69 73 74 2c 20 74 68 65  pIndex list, the
22bd9 6e 20 72 65 67 69 73 74 65 72 20 72 65 67 4c 61  n register regLa
22bda 73 74 20 68 61 73 0a 20 20 20 20 2a 2a 20 6e 6f  st has.    ** no
22bdb 74 20 62 65 65 6e 20 70 6f 70 75 6c 61 74 65 64  t been populated
22bdc 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 70  . In this case p
22bdd 6f 70 75 6c 61 74 65 20 69 74 20 6e 6f 77 2e 20  opulate it now. 
22bde 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54 61 62   */.    if( pTab
22bdf 2d 3e 70 49 6e 64 65 78 3d 3d 70 49 64 78 20 29  ->pIndex==pIdx )
22be0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
22be1 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
22be2 49 6e 74 65 67 65 72 2c 20 53 51 4c 49 54 45 5f  Integer, SQLITE_
22be3 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 2c 20 72  INDEX_SAMPLES, r
22be4 65 67 53 61 6d 70 6c 65 72 65 63 6e 6f 29 3b 0a  egSamplerecno);.
22be5 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22be6 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
22be7 74 65 67 65 72 2c 20 53 51 4c 49 54 45 5f 49 4e  teger, SQLITE_IN
22be8 44 45 58 5f 53 41 4d 50 4c 45 53 2a 32 2d 31 2c  DEX_SAMPLES*2-1,
22be9 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 20   regTemp);.     
22bea 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22beb 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
22bec 2c 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53  , SQLITE_INDEX_S
22bed 41 4d 50 4c 45 53 2a 32 2c 20 72 65 67 54 65 6d  AMPLES*2, regTem
22bee 70 32 29 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69  p2);..      sqli
22bef 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
22bf0 20 4f 50 5f 43 6f 75 6e 74 2c 20 69 49 64 78 43   OP_Count, iIdxC
22bf1 75 72 2c 20 72 65 67 4c 61 73 74 29 3b 0a 20 20  ur, regLast);.  
22bf2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22bf3 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
22bf4 2c 20 30 2c 20 72 65 67 46 69 72 73 74 29 3b 0a  , 0, regFirst);.
22bf5 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
22bf6 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
22bf7 2c 20 4f 50 5f 4c 74 2c 20 72 65 67 53 61 6d 70  , OP_Lt, regSamp
22bf8 6c 65 72 65 63 6e 6f 2c 20 30 2c 20 72 65 67 4c  lerecno, 0, regL
22bf9 61 73 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ast);.      sqli
22bfa 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
22bfb 20 4f 50 5f 44 69 76 69 64 65 2c 20 72 65 67 54   OP_Divide, regT
22bfc 65 6d 70 32 2c 20 72 65 67 4c 61 73 74 2c 20 72  emp2, regLast, r
22bfd 65 67 46 69 72 73 74 29 3b 0a 20 20 20 20 20 20  egFirst);.      
22bfe 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22bff 33 28 76 2c 20 4f 50 5f 4d 75 6c 74 69 70 6c 79  3(v, OP_Multiply
22c00 2c 20 72 65 67 4c 61 73 74 2c 20 72 65 67 54 65  , regLast, regTe
22c01 6d 70 2c 20 72 65 67 4c 61 73 74 29 3b 0a 20 20  mp, regLast);.  
22c02 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22c03 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49  ddOp2(v, OP_AddI
22c04 6d 6d 2c 20 72 65 67 4c 61 73 74 2c 20 53 51 4c  mm, regLast, SQL
22c05 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45  ITE_INDEX_SAMPLE
22c06 53 2a 32 2d 32 29 3b 0a 20 20 20 20 20 20 73 71  S*2-2);.      sq
22c07 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
22c08 76 2c 20 4f 50 5f 44 69 76 69 64 65 2c 20 20 72  v, OP_Divide,  r
22c09 65 67 54 65 6d 70 32 2c 20 72 65 67 4c 61 73 74  egTemp2, regLast
22c0a 2c 20 72 65 67 4c 61 73 74 29 3b 0a 20 20 20 20  , regLast);.    
22c0b 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
22c0c 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a  pHere(v, addr);.
22c0d 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 5a 65      }..    /* Ze
22c0e 72 6f 20 74 68 65 20 72 65 67 53 61 6d 70 6c 65  ro the regSample
22c0f 6e 6f 20 61 6e 64 20 72 65 67 52 65 63 6e 6f 20  no and regRecno 
22c10 72 65 67 69 73 74 65 72 73 2e 20 2a 2f 0a 20 20  registers. */.  
22c11 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22c12 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
22c13 72 2c 20 30 2c 20 72 65 67 53 61 6d 70 6c 65 6e  r, 0, regSamplen
22c14 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  o);.    sqlite3V
22c15 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
22c16 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 52  Integer, 0, regR
22c17 65 63 6e 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74  ecno);.    sqlit
22c18 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
22c19 4f 50 5f 43 6f 70 79 2c 20 72 65 67 46 69 72 73  OP_Copy, regFirs
22c1a 74 2c 20 72 65 67 53 61 6d 70 6c 65 72 65 63 6e  t, regSamplerecn
22c1b 6f 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20  o);.#endif..    
22c1c 2f 2a 20 54 68 65 20 62 6c 6f 63 6b 20 6f 66 20  /* The block of 
22c1d 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 69 6e 69  memory cells ini
22c1e 74 69 61 6c 69 7a 65 64 20 68 65 72 65 20 69 73  tialized here is
22c1f 20 75 73 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73   used as follows
22c20 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
22c21 20 20 20 69 4d 65 6d 3a 20 20 20 20 20 20 20 20     iMem:        
22c22 20 20 20 20 20 20 20 20 0a 20 20 20 20 2a 2a 20          .    ** 
22c23 20 20 20 20 20 20 20 54 68 65 20 74 6f 74 61 6c         The total
22c24 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
22c25 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20  in the table..  
22c26 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 69    **.    **    i
22c27 4d 65 6d 2b 31 20 2e 2e 20 69 4d 65 6d 2b 6e 43  Mem+1 .. iMem+nC
22c28 6f 6c 3a 20 0a 20 20 20 20 2a 2a 20 20 20 20 20  ol: .    **     
22c29 20 20 20 4e 75 6d 62 65 72 20 6f 66 20 64 69 73     Number of dis
22c2a 74 69 6e 63 74 20 65 6e 74 72 69 65 73 20 69 6e  tinct entries in
22c2b 20 69 6e 64 65 78 20 63 6f 6e 73 69 64 65 72 69   index consideri
22c2c 6e 67 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 20  ng the .    **  
22c2d 20 20 20 20 20 20 6c 65 66 74 2d 6d 6f 73 74 20        left-most 
22c2e 4e 20 63 6f 6c 75 6d 6e 73 20 6f 6e 6c 79 2c 20  N columns only, 
22c2f 77 68 65 72 65 20 4e 20 69 73 20 62 65 74 77 65  where N is betwe
22c30 65 6e 20 31 20 61 6e 64 20 6e 43 6f 6c 2c 20 0a  en 1 and nCol, .
22c31 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 69 6e      **        in
22c32 63 6c 75 73 69 76 65 2e 0a 20 20 20 20 2a 2a 0a  clusive..    **.
22c33 20 20 20 20 2a 2a 20 20 20 20 69 4d 65 6d 2b 6e      **    iMem+n
22c34 43 6f 6c 2b 31 20 2e 2e 20 4d 65 6d 2b 32 2a 6e  Col+1 .. Mem+2*n
22c35 43 6f 6c 3a 20 20 0a 20 20 20 20 2a 2a 20 20 20  Col:  .    **   
22c36 20 20 20 20 20 50 72 65 76 69 6f 75 73 20 76 61       Previous va
22c37 6c 75 65 20 6f 66 20 69 6e 64 65 78 65 64 20 63  lue of indexed c
22c38 6f 6c 75 6d 6e 73 2c 20 66 72 6f 6d 20 6c 65 66  olumns, from lef
22c39 74 20 74 6f 20 72 69 67 68 74 2e 0a 20 20 20 20  t to right..    
22c3a 2a 2a 0a 20 20 20 20 2a 2a 20 43 65 6c 6c 73 20  **.    ** Cells 
22c3b 69 4d 65 6d 20 74 68 72 6f 75 67 68 20 69 4d 65  iMem through iMe
22c3c 6d 2b 6e 43 6f 6c 20 61 72 65 20 69 6e 69 74 69  m+nCol are initi
22c3d 61 6c 69 7a 65 64 20 74 6f 20 30 2e 20 54 68 65  alized to 0. The
22c3e 20 6f 74 68 65 72 73 20 61 72 65 20 0a 20 20 20   others are .   
22c3f 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 20   ** initialized 
22c40 74 6f 20 63 6f 6e 74 61 69 6e 20 61 6e 20 53 51  to contain an SQ
22c41 4c 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f 0a 20  L NULL..    */. 
22c42 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 6e     for(i=0; i<=n
22c43 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
22c44 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22c45 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
22c46 2c 20 30 2c 20 69 4d 65 6d 2b 69 29 3b 0a 20 20  , 0, iMem+i);.  
22c47 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b    }.    for(i=0;
22c48 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20   i<nCol; i++){. 
22c49 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
22c4a 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
22c4b 6c 2c 20 30 2c 20 69 4d 65 6d 2b 6e 43 6f 6c 2b  l, 0, iMem+nCol+
22c4c 69 2b 31 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  i+1);.    }..   
22c4d 20 2f 2a 20 53 74 61 72 74 20 74 68 65 20 61 6e   /* Start the an
22c4e 61 6c 79 73 69 73 20 6c 6f 6f 70 2e 20 54 68 69  alysis loop. Thi
22c4f 73 20 6c 6f 6f 70 20 72 75 6e 73 20 74 68 72 6f  s loop runs thro
22c50 75 67 68 20 61 6c 6c 20 74 68 65 20 65 6e 74 72  ugh all the entr
22c51 69 65 73 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68  ies in.    ** th
22c52 65 20 69 6e 64 65 78 20 62 2d 74 72 65 65 2e 20  e index b-tree. 
22c53 20 2a 2f 0a 20 20 20 20 65 6e 64 4f 66 4c 6f 6f   */.    endOfLoo
22c54 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  p = sqlite3VdbeM
22c55 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
22c56 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22c57 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c  p2(v, OP_Rewind,
22c58 20 69 49 64 78 43 75 72 2c 20 65 6e 64 4f 66 4c   iIdxCur, endOfL
22c59 6f 6f 70 29 3b 0a 20 20 20 20 74 6f 70 4f 66 4c  oop);.    topOfL
22c5a 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  oop = sqlite3Vdb
22c5b 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
22c5c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
22c5d 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64  AddOp2(v, OP_Add
22c5e 49 6d 6d 2c 20 69 4d 65 6d 2c 20 31 29 3b 0a 0a  Imm, iMem, 1);..
22c5f 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
22c60 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
22c61 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22c62 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
22c63 20 69 49 64 78 43 75 72 2c 20 69 2c 20 72 65 67   iIdxCur, i, reg
22c64 43 6f 6c 29 3b 0a 23 69 66 64 65 66 20 53 51 4c  Col);.#ifdef SQL
22c65 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32  ITE_ENABLE_STAT2
22c66 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d 30 20  .      if( i==0 
22c67 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 43 68  ){.        /* Ch
22c68 65 63 6b 20 69 66 20 74 68 65 20 72 65 63 6f 72  eck if the recor
22c69 64 20 74 68 61 74 20 63 75 72 73 6f 72 20 69 49  d that cursor iI
22c6a 64 78 43 75 72 20 70 6f 69 6e 74 73 20 74 6f 20  dxCur points to 
22c6b 63 6f 6e 74 61 69 6e 73 20 61 0a 20 20 20 20 20  contains a.     
22c6c 20 20 20 2a 2a 20 76 61 6c 75 65 20 74 68 61 74     ** value that
22c6d 20 73 68 6f 75 6c 64 20 62 65 20 73 74 6f 72 65   should be store
22c6e 64 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 5f  d in the sqlite_
22c6f 73 74 61 74 32 20 74 61 62 6c 65 2e 20 49 66 20  stat2 table. If 
22c70 73 6f 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 73  so,.        ** s
22c71 74 6f 72 65 20 69 74 2e 20 20 2a 2f 0a 20 20 20  tore it.  */.   
22c72 20 20 20 20 20 69 6e 74 20 6e 65 20 3d 20 73 71       int ne = sq
22c73 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
22c74 76 2c 20 4f 50 5f 4e 65 2c 20 72 65 67 52 65 63  v, OP_Ne, regRec
22c75 6e 6f 2c 20 30 2c 20 72 65 67 53 61 6d 70 6c 65  no, 0, regSample
22c76 72 65 63 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20  recno);.        
22c77 61 73 73 65 72 74 28 20 72 65 67 54 61 62 6e 61  assert( regTabna
22c78 6d 65 2b 31 3d 3d 72 65 67 49 64 78 6e 61 6d 65  me+1==regIdxname
22c79 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 26   .             &
22c7a 26 20 72 65 67 54 61 62 6e 61 6d 65 2b 32 3d 3d  & regTabname+2==
22c7b 72 65 67 53 61 6d 70 6c 65 6e 6f 0a 20 20 20 20  regSampleno.    
22c7c 20 20 20 20 20 20 20 20 20 26 26 20 72 65 67 54           && regT
22c7d 61 62 6e 61 6d 65 2b 33 3d 3d 72 65 67 43 6f 6c  abname+3==regCol
22c7e 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20  .        );.    
22c7f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
22c80 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54  hangeP5(v, SQLIT
22c81 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20  E_JUMPIFNULL);. 
22c82 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
22c83 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d  beAddOp4(v, OP_M
22c84 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 54 61  akeRecord, regTa
22c85 62 6e 61 6d 65 2c 20 34 2c 20 72 65 67 52 65 63  bname, 4, regRec
22c86 2c 20 22 61 61 61 62 22 2c 20 30 29 3b 0a 20 20  , "aaab", 0);.  
22c87 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22c88 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
22c89 77 52 6f 77 69 64 2c 20 69 53 74 61 74 43 75 72  wRowid, iStatCur
22c8a 2b 31 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  +1, regRowid);. 
22c8b 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
22c8c 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49  beAddOp3(v, OP_I
22c8d 6e 73 65 72 74 2c 20 69 53 74 61 74 43 75 72 2b  nsert, iStatCur+
22c8e 31 2c 20 72 65 67 52 65 63 2c 20 72 65 67 52 6f  1, regRec, regRo
22c8f 77 69 64 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f  wid);..        /
22c90 2a 20 43 61 6c 63 75 6c 61 74 65 20 6e 65 77 20  * Calculate new 
22c91 76 61 6c 75 65 73 20 66 6f 72 20 72 65 67 53 61  values for regSa
22c92 6d 70 6c 65 72 65 63 6e 6f 20 61 6e 64 20 72 65  mplerecno and re
22c93 67 53 61 6d 70 6c 65 6e 6f 2e 0a 20 20 20 20 20  gSampleno..     
22c94 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
22c95 20 20 20 73 61 6d 70 6c 65 6e 6f 20 3d 20 73 61     sampleno = sa
22c96 6d 70 6c 65 6e 6f 20 2b 20 31 0a 20 20 20 20 20  mpleno + 1.     
22c97 20 20 20 2a 2a 20 20 20 73 61 6d 70 6c 65 72 65     **   samplere
22c98 63 6e 6f 20 3d 20 73 61 6d 70 6c 65 72 65 63 6e  cno = samplerecn
22c99 6f 2b 28 72 65 6d 61 69 6e 69 6e 67 20 72 65 63  o+(remaining rec
22c9a 6f 72 64 73 29 2f 28 72 65 6d 61 69 6e 69 6e 67  ords)/(remaining
22c9b 20 73 61 6d 70 6c 65 73 29 0a 20 20 20 20 20 20   samples).      
22c9c 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c    */.        sql
22c9d 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
22c9e 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67  , OP_AddImm, reg
22c9f 53 61 6d 70 6c 65 6e 6f 2c 20 31 29 3b 0a 20 20  Sampleno, 1);.  
22ca0 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22ca1 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 75  eAddOp3(v, OP_Su
22ca2 62 74 72 61 63 74 2c 20 72 65 67 52 65 63 6e 6f  btract, regRecno
22ca3 2c 20 72 65 67 4c 61 73 74 2c 20 72 65 67 54 65  , regLast, regTe
22ca4 6d 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  mp);.        sql
22ca5 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
22ca6 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72 65 67  , OP_AddImm, reg
22ca7 54 65 6d 70 2c 20 2d 31 29 3b 0a 20 20 20 20 20  Temp, -1);.     
22ca8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22ca9 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
22caa 65 72 2c 20 53 51 4c 49 54 45 5f 49 4e 44 45 58  er, SQLITE_INDEX
22cab 5f 53 41 4d 50 4c 45 53 2c 20 72 65 67 54 65 6d  _SAMPLES, regTem
22cac 70 32 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  p2);.        sql
22cad 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
22cae 2c 20 4f 50 5f 53 75 62 74 72 61 63 74 2c 20 72  , OP_Subtract, r
22caf 65 67 53 61 6d 70 6c 65 6e 6f 2c 20 72 65 67 54  egSampleno, regT
22cb0 65 6d 70 32 2c 20 72 65 67 54 65 6d 70 32 29 3b  emp2, regTemp2);
22cb1 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
22cb2 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
22cb3 5f 44 69 76 69 64 65 2c 20 72 65 67 54 65 6d 70  _Divide, regTemp
22cb4 32 2c 20 72 65 67 54 65 6d 70 2c 20 72 65 67 54  2, regTemp, regT
22cb5 65 6d 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71  emp);.        sq
22cb6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
22cb7 76 2c 20 4f 50 5f 41 64 64 2c 20 72 65 67 53 61  v, OP_Add, regSa
22cb8 6d 70 6c 65 72 65 63 6e 6f 2c 20 72 65 67 54 65  mplerecno, regTe
22cb9 6d 70 2c 20 72 65 67 53 61 6d 70 6c 65 72 65 63  mp, regSamplerec
22cba 6e 6f 29 3b 0a 0a 20 20 20 20 20 20 20 20 73 71  no);..        sq
22cbb 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
22cbc 65 28 76 2c 20 6e 65 29 3b 0a 20 20 20 20 20 20  e(v, ne);.      
22cbd 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22cbe 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d  Op2(v, OP_AddImm
22cbf 2c 20 72 65 67 52 65 63 6e 6f 2c 20 31 29 3b 0a  , regRecno, 1);.
22cc0 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a        }.#endif..
22cc1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
22cc2 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65  eAddOp3(v, OP_Ne
22cc3 2c 20 72 65 67 43 6f 6c 2c 20 30 2c 20 69 4d 65  , regCol, 0, iMe
22cc4 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20  m+nCol+i+1);.   
22cc5 20 20 20 2f 2a 2a 2a 2a 20 54 4f 44 4f 3a 20 20     /**** TODO:  
22cc6 61 64 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  add collating se
22cc7 71 75 65 6e 63 65 20 2a 2a 2a 2a 2a 2f 0a 20 20  quence *****/.  
22cc8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
22cc9 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54  hangeP5(v, SQLIT
22cca 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20  E_JUMPIFNULL);. 
22ccb 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64 62 2d     }.    if( db-
22ccc 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
22ccd 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20 6d  .      /* If a m
22cce 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 20 68 61  alloc failure ha
22ccf 73 20 6f 63 63 75 72 72 65 64 2c 20 74 68 65 6e  s occurred, then
22cd0 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74   the result of t
22cd1 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 0a 20  he expression . 
22cd2 20 20 20 20 20 2a 2a 20 70 61 73 73 65 64 20 61       ** passed a
22cd3 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
22cd4 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 63 61 6c  ument to the cal
22cd5 6c 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 65  l to sqlite3Vdbe
22cd6 4a 75 6d 70 48 65 72 65 28 29 20 0a 20 20 20 20  JumpHere() .    
22cd7 20 20 2a 2a 20 62 65 6c 6f 77 20 6d 61 79 20 62    ** below may b
22cd8 65 20 6e 65 67 61 74 69 76 65 2e 20 57 68 69 63  e negative. Whic
22cd9 68 20 63 61 75 73 65 73 20 61 6e 20 61 73 73 65  h causes an asse
22cda 72 74 28 29 20 74 6f 20 66 61 69 6c 20 28 6f 72  rt() to fail (or
22cdb 20 61 6e 0a 20 20 20 20 20 20 2a 2a 20 6f 75 74   an.      ** out
22cdc 2d 6f 66 2d 62 6f 75 6e 64 73 20 77 72 69 74 65  -of-bounds write
22cdd 20 69 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47   if SQLITE_DEBUG
22cde 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29   is not defined)
22cdf 2e 20 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75  .  */.      retu
22ce0 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  rn;.    }.    sq
22ce1 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
22ce2 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 65  v, OP_Goto, 0, e
22ce3 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 66  ndOfLoop);.    f
22ce4 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20  or(i=0; i<nCol; 
22ce5 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  i++){.      sqli
22ce6 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
22ce7 76 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43 75  v, sqlite3VdbeCu
22ce8 72 72 65 6e 74 41 64 64 72 28 76 29 2d 28 6e 43  rrentAddr(v)-(nC
22ce9 6f 6c 2a 32 29 29 3b 0a 20 20 20 20 20 20 73 71  ol*2));.      sq
22cea 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
22ceb 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 4d  v, OP_AddImm, iM
22cec 65 6d 2b 69 2b 31 2c 20 31 29 3b 0a 20 20 20 20  em+i+1, 1);.    
22ced 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22cee 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
22cef 2c 20 69 49 64 78 43 75 72 2c 20 69 2c 20 69 4d  , iIdxCur, i, iM
22cf0 65 6d 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20  em+nCol+i+1);.  
22cf1 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 45 6e 64 20    }..    /* End 
22cf2 6f 66 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20  of the analysis 
22cf3 6c 6f 6f 70 2e 20 2a 2f 0a 20 20 20 20 73 71 6c  loop. */.    sql
22cf4 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
22cf5 61 62 65 6c 28 76 2c 20 65 6e 64 4f 66 4c 6f 6f  abel(v, endOfLoo
22cf6 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  p);.    sqlite3V
22cf7 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
22cf8 4e 65 78 74 2c 20 69 49 64 78 43 75 72 2c 20 74  Next, iIdxCur, t
22cf9 6f 70 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20 73  opOfLoop);.    s
22cfa 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
22cfb 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 49  (v, OP_Close, iI
22cfc 64 78 43 75 72 29 3b 0a 0a 20 20 20 20 2f 2a 20  dxCur);..    /* 
22cfd 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  Store the result
22cfe 73 20 69 6e 20 73 71 6c 69 74 65 5f 73 74 61 74  s in sqlite_stat
22cff 31 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  1..    **.    **
22d00 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 61   The result is a
22d01 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74   single row of t
22d02 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20  he sqlite_stat1 
22d03 74 61 62 6c 65 2e 20 20 54 68 65 20 66 69 72 73  table.  The firs
22d04 74 0a 20 20 20 20 2a 2a 20 74 77 6f 20 63 6f 6c  t.    ** two col
22d05 75 6d 6e 73 20 61 72 65 20 74 68 65 20 6e 61 6d  umns are the nam
22d06 65 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  es of the table 
22d07 61 6e 64 20 69 6e 64 65 78 2e 20 20 54 68 65 20  and index.  The 
22d08 74 68 69 72 64 20 63 6f 6c 75 6d 6e 0a 20 20 20  third column.   
22d09 20 2a 2a 20 69 73 20 61 20 73 74 72 69 6e 67 20   ** is a string 
22d0a 63 6f 6d 70 6f 73 65 64 20 6f 66 20 61 20 6c 69  composed of a li
22d0b 73 74 20 6f 66 20 69 6e 74 65 67 65 72 20 73 74  st of integer st
22d0c 61 74 69 73 74 69 63 73 20 61 62 6f 75 74 20 74  atistics about t
22d0d 68 65 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78 2e  he.    ** index.
22d0e 20 20 54 68 65 20 66 69 72 73 74 20 69 6e 74 65    The first inte
22d0f 67 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74 20  ger in the list 
22d10 69 73 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d  is the total num
22d11 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 0a 20  ber of entries. 
22d12 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 69 6e 64     ** in the ind
22d13 65 78 2e 20 20 54 68 65 72 65 20 69 73 20 6f 6e  ex.  There is on
22d14 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 74  e additional int
22d15 65 67 65 72 20 69 6e 20 74 68 65 20 6c 69 73 74  eger in the list
22d16 20 66 6f 72 20 65 61 63 68 0a 20 20 20 20 2a 2a   for each.    **
22d17 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74   column of the t
22d18 61 62 6c 65 2e 20 20 54 68 69 73 20 61 64 64 69  able.  This addi
22d19 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65 72 20 69  tional integer i
22d1a 73 20 61 20 67 75 65 73 73 20 6f 66 20 68 6f 77  s a guess of how
22d1b 20 6d 61 6e 79 0a 20 20 20 20 2a 2a 20 72 6f 77   many.    ** row
22d1c 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  s of the table t
22d1d 68 65 20 69 6e 64 65 78 20 77 69 6c 6c 20 73 65  he index will se
22d1e 6c 65 63 74 2e 20 20 49 66 20 44 20 69 73 20 74  lect.  If D is t
22d1f 68 65 20 63 6f 75 6e 74 20 6f 66 20 64 69 73 74  he count of dist
22d20 69 6e 63 74 0a 20 20 20 20 2a 2a 20 76 61 6c 75  inct.    ** valu
22d21 65 73 20 61 6e 64 20 4b 20 69 73 20 74 68 65 20  es and K is the 
22d22 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
22d23 72 6f 77 73 2c 20 74 68 65 6e 20 74 68 65 20 69  rows, then the i
22d24 6e 74 65 67 65 72 20 69 73 20 63 6f 6d 70 75 74  nteger is comput
22d25 65 64 0a 20 20 20 20 2a 2a 20 61 73 3a 0a 20 20  ed.    ** as:.  
22d26 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20    **.    **     
22d27 20 20 20 49 20 3d 20 28 4b 2b 44 2d 31 29 2f 44     I = (K+D-1)/D
22d28 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49  .    **.    ** I
22d29 66 20 4b 3d 3d 30 20 74 68 65 6e 20 6e 6f 20 65  f K==0 then no e
22d2a 6e 74 72 79 20 69 73 20 6d 61 64 65 20 69 6e 74  ntry is made int
22d2b 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  o the sqlite_sta
22d2c 74 31 20 74 61 62 6c 65 2e 20 20 0a 20 20 20 20  t1 table.  .    
22d2d 2a 2a 20 49 66 20 4b 3e 30 20 74 68 65 6e 20 69  ** If K>0 then i
22d2e 74 20 69 73 20 61 6c 77 61 79 73 20 74 68 65 20  t is always the 
22d2f 63 61 73 65 20 74 68 65 20 44 3e 30 20 73 6f 20  case the D>0 so 
22d30 64 69 76 69 73 69 6f 6e 20 62 79 20 7a 65 72 6f  division by zero
22d31 0a 20 20 20 20 2a 2a 20 69 73 20 6e 65 76 65 72  .    ** is never
22d32 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20 20 20 2a   possible..    *
22d33 2f 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c  /.    addr = sql
22d34 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
22d35 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 69 4d 65 6d  , OP_IfNot, iMem
22d36 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
22d37 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
22d38 43 6f 70 79 2c 20 69 4d 65 6d 2c 20 72 65 67 53  Copy, iMem, regS
22d39 61 6d 70 6c 65 6e 6f 29 3b 0a 20 20 20 20 66 6f  ampleno);.    fo
22d3a 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69  r(i=0; i<nCol; i
22d3b 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
22d3c 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
22d3d 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 72  OP_String8, 0, r
22d3e 65 67 54 65 6d 70 2c 20 30 2c 20 22 20 22 2c 20  egTemp, 0, " ", 
22d3f 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
22d40 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
22d41 50 5f 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d  P_Concat, regTem
22d42 70 2c 20 72 65 67 53 61 6d 70 6c 65 6e 6f 2c 20  p, regSampleno, 
22d43 72 65 67 53 61 6d 70 6c 65 6e 6f 29 3b 0a 20 20  regSampleno);.  
22d44 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22d45 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64 64 2c  ddOp3(v, OP_Add,
22d46 20 69 4d 65 6d 2c 20 69 4d 65 6d 2b 69 2b 31 2c   iMem, iMem+i+1,
22d47 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 20   regTemp);.     
22d48 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22d49 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c  p2(v, OP_AddImm,
22d4a 20 72 65 67 54 65 6d 70 2c 20 2d 31 29 3b 0a 20   regTemp, -1);. 
22d4b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
22d4c 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 44 69 76  AddOp3(v, OP_Div
22d4d 69 64 65 2c 20 69 4d 65 6d 2b 69 2b 31 2c 20 72  ide, iMem+i+1, r
22d4e 65 67 54 65 6d 70 2c 20 72 65 67 54 65 6d 70 29  egTemp, regTemp)
22d4f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
22d50 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
22d51 54 6f 49 6e 74 2c 20 72 65 67 54 65 6d 70 29 3b  ToInt, regTemp);
22d52 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
22d53 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
22d54 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d 70 2c 20  oncat, regTemp, 
22d55 72 65 67 53 61 6d 70 6c 65 6e 6f 2c 20 72 65 67  regSampleno, reg
22d56 53 61 6d 70 6c 65 6e 6f 29 3b 0a 20 20 20 20 7d  Sampleno);.    }
22d57 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
22d58 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp4(v, OP_Mak
22d59 65 52 65 63 6f 72 64 2c 20 72 65 67 54 61 62 6e  eRecord, regTabn
22d5a 61 6d 65 2c 20 33 2c 20 72 65 67 52 65 63 2c 20  ame, 3, regRec, 
22d5b 22 61 61 61 22 2c 20 30 29 3b 0a 20 20 20 20 73  "aaa", 0);.    s
22d5c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
22d5d 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c  (v, OP_NewRowid,
22d5e 20 69 53 74 61 74 43 75 72 2c 20 72 65 67 52 6f   iStatCur, regRo
22d5f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  wid);.    sqlite
22d60 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
22d61 50 5f 49 6e 73 65 72 74 2c 20 69 53 74 61 74 43  P_Insert, iStatC
22d62 75 72 2c 20 72 65 67 52 65 63 2c 20 72 65 67 52  ur, regRec, regR
22d63 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  owid);.    sqlit
22d64 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
22d65 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29  , OPFLAG_APPEND)
22d66 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
22d67 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
22d68 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  r);.  }.}../*.**
22d69 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
22d6a 68 61 74 20 77 69 6c 6c 20 63 61 75 73 65 20 74  hat will cause t
22d6b 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 69  he most recent i
22d6c 6e 64 65 78 20 61 6e 61 6c 79 73 69 73 20 74 6f  ndex analysis to
22d6d 0a 2a 2a 20 62 65 20 6c 61 6f 64 65 64 20 69 6e  .** be laoded in
22d6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68  to internal hash
22d6f 20 74 61 62 6c 65 73 20 77 68 65 72 65 20 69 73   tables where is
22d70 20 63 61 6e 20 62 65 20 75 73 65 64 2e 0a 2a 2f   can be used..*/
22d71 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 61  .static void loa
22d72 64 41 6e 61 6c 79 73 69 73 28 50 61 72 73 65 20  dAnalysis(Parse 
22d73 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44 62  *pParse, int iDb
22d74 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73  ){.  Vdbe *v = s
22d75 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
22d76 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29  arse);.  if( v )
22d77 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
22d78 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4c 6f  eAddOp1(v, OP_Lo
22d79 61 64 41 6e 61 6c 79 73 69 73 2c 20 69 44 62 29  adAnalysis, iDb)
22d7a 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  ;.  }.}../*.** G
22d7b 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
22d7c 74 20 77 69 6c 6c 20 64 6f 20 61 6e 20 61 6e 61  t will do an ana
22d7d 6c 79 73 69 73 20 6f 66 20 61 6e 20 65 6e 74 69  lysis of an enti
22d7e 72 65 20 64 61 74 61 62 61 73 65 0a 2a 2f 0a 73  re database.*/.s
22d7f 74 61 74 69 63 20 76 6f 69 64 20 61 6e 61 6c 79  tatic void analy
22d80 7a 65 44 61 74 61 62 61 73 65 28 50 61 72 73 65  zeDatabase(Parse
22d81 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 44   *pParse, int iD
22d82 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  b){.  sqlite3 *d
22d83 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
22d84 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
22d85 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  a = db->aDb[iDb]
22d86 2e 70 53 63 68 65 6d 61 3b 20 20 20 20 2f 2a 20  .pSchema;    /* 
22d87 53 63 68 65 6d 61 20 6f 66 20 64 61 74 61 62 61  Schema of databa
22d88 73 65 20 69 44 62 20 2a 2f 0a 20 20 48 61 73 68  se iDb */.  Hash
22d89 45 6c 65 6d 20 2a 6b 3b 0a 20 20 69 6e 74 20 69  Elem *k;.  int i
22d8a 53 74 61 74 43 75 72 3b 0a 20 20 69 6e 74 20 69  StatCur;.  int i
22d8b 4d 65 6d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42  Mem;..  sqlite3B
22d8c 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
22d8d 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44  on(pParse, 0, iD
22d8e 62 29 3b 0a 20 20 69 53 74 61 74 43 75 72 20 3d  b);.  iStatCur =
22d8f 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20   pParse->nTab;. 
22d90 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 2b 3d   pParse->nTab +=
22d91 20 32 3b 0a 20 20 6f 70 65 6e 53 74 61 74 54 61   2;.  openStatTa
22d92 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44 62 2c  ble(pParse, iDb,
22d93 20 69 53 74 61 74 43 75 72 2c 20 30 29 3b 0a 20   iStatCur, 0);. 
22d94 20 69 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e   iMem = pParse->
22d95 6e 4d 65 6d 2b 31 3b 0a 20 20 66 6f 72 28 6b 3d  nMem+1;.  for(k=
22d96 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28  sqliteHashFirst(
22d97 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73  &pSchema->tblHas
22d98 68 29 3b 20 6b 3b 20 6b 3d 73 71 6c 69 74 65 48  h); k; k=sqliteH
22d99 61 73 68 4e 65 78 74 28 6b 29 29 7b 0a 20 20 20  ashNext(k)){.   
22d9a 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 28   Table *pTab = (
22d9b 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 48 61 73  Table*)sqliteHas
22d9c 68 44 61 74 61 28 6b 29 3b 0a 20 20 20 20 61 6e  hData(k);.    an
22d9d 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 70 50  alyzeOneTable(pP
22d9e 61 72 73 65 2c 20 70 54 61 62 2c 20 69 53 74 61  arse, pTab, iSta
22d9f 74 43 75 72 2c 20 69 4d 65 6d 29 3b 0a 20 20 7d  tCur, iMem);.  }
22da0 0a 20 20 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28  .  loadAnalysis(
22da1 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 7d 0a  pParse, iDb);.}.
22da2 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
22da3 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 64  code that will d
22da4 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66  o an analysis of
22da5 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 20   a single table 
22da6 69 6e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65  in.** a database
22da7 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
22da8 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 50 61   analyzeTable(Pa
22da9 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62  rse *pParse, Tab
22daa 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 69 6e 74  le *pTab){.  int
22dab 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 53 74 61   iDb;.  int iSta
22dac 74 43 75 72 3b 0a 0a 20 20 61 73 73 65 72 74 28  tCur;..  assert(
22dad 20 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 61 73   pTab!=0 );.  as
22dae 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72  sert( sqlite3Btr
22daf 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65  eeHoldsAllMutexe
22db0 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b  s(pParse->db) );
22db1 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  .  iDb = sqlite3
22db2 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50  SchemaToIndex(pP
22db3 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e  arse->db, pTab->
22db4 70 53 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c 69  pSchema);.  sqli
22db5 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
22db6 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30  ration(pParse, 0
22db7 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 61 74 43  , iDb);.  iStatC
22db8 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  ur = pParse->nTa
22db9 62 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 54 61  b;.  pParse->nTa
22dba 62 20 2b 3d 20 32 3b 0a 20 20 6f 70 65 6e 53 74  b += 2;.  openSt
22dbb 61 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  atTable(pParse, 
22dbc 69 44 62 2c 20 69 53 74 61 74 43 75 72 2c 20 70  iDb, iStatCur, p
22dbd 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 61  Tab->zName);.  a
22dbe 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 70  nalyzeOneTable(p
22dbf 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 53 74  Parse, pTab, iSt
22dc0 61 74 43 75 72 2c 20 70 50 61 72 73 65 2d 3e 6e  atCur, pParse->n
22dc1 4d 65 6d 2b 31 29 3b 0a 20 20 6c 6f 61 64 41 6e  Mem+1);.  loadAn
22dc2 61 6c 79 73 69 73 28 70 50 61 72 73 65 2c 20 69  alysis(pParse, i
22dc3 44 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  Db);.}../*.** Ge
22dc4 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
22dc5 74 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d  the ANALYZE comm
22dc6 61 6e 64 2e 20 20 54 68 65 20 70 61 72 73 65 72  and.  The parser
22dc7 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74   calls this rout
22dc8 69 6e 65 0a 2a 2a 20 77 68 65 6e 20 69 74 20 72  ine.** when it r
22dc9 65 63 6f 67 6e 69 7a 65 73 20 61 6e 20 41 4e 41  ecognizes an ANA
22dca 4c 59 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a  LYZE command..**
22dcb 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c 59  .**        ANALY
22dcc 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ZE              
22dcd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d                --
22dce 20 31 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41   1.**        ANA
22dcf 4c 59 5a 45 20 20 3c 64 61 74 61 62 61 73 65 3e  LYZE  <database>
22dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22dd1 2d 2d 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 41  -- 2.**        A
22dd2 4e 41 4c 59 5a 45 20 20 3f 3c 64 61 74 61 62 61  NALYZE  ?<databa
22dd3 73 65 3e 2e 3f 3c 74 61 62 6c 65 6e 61 6d 65 3e  se>.?<tablename>
22dd4 20 20 2d 2d 20 33 0a 2a 2a 0a 2a 2a 20 46 6f 72    -- 3.**.** For
22dd5 6d 20 31 20 63 61 75 73 65 73 20 61 6c 6c 20 69  m 1 causes all i
22dd6 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c 20 61 74  ndices in all at
22dd7 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
22dd8 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e   to be analyzed.
22dd9 0a 2a 2a 20 46 6f 72 6d 20 32 20 61 6e 61 6c 79  .** Form 2 analy
22dda 7a 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20  zes all indices 
22ddb 74 68 65 20 73 69 6e 67 6c 65 20 64 61 74 61 62  the single datab
22ddc 61 73 65 20 6e 61 6d 65 64 2e 0a 2a 2a 20 46 6f  ase named..** Fo
22ddd 72 6d 20 33 20 61 6e 61 6c 79 7a 65 73 20 61 6c  rm 3 analyzes al
22dde 6c 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69  l indices associ
22ddf 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6e 61  ated with the na
22de0 6d 65 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51  med table..*/.SQ
22de1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
22de2 64 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65  d sqlite3Analyze
22de3 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
22de4 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54  Token *pName1, T
22de5 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20  oken *pName2){. 
22de6 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
22de7 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74  Parse->db;.  int
22de8 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20   iDb;.  int i;. 
22de9 20 63 68 61 72 20 2a 7a 2c 20 2a 7a 44 62 3b 0a   char *z, *zDb;.
22dea 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
22deb 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61   Token *pTableNa
22dec 6d 65 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74  me;..  /* Read t
22ded 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
22dee 6d 61 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  ma. If an error 
22def 6f 63 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e  occurs, leave an
22df0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20   error message. 
22df1 20 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20   ** and code in 
22df2 70 50 61 72 73 65 20 61 6e 64 20 72 65 74 75 72  pParse and retur
22df3 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 73  n NULL. */.  ass
22df4 65 72 74 28 20 73 71 6c 69 74 65 33 42 74 72 65  ert( sqlite3Btre
22df5 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73  eHoldsAllMutexes
22df6 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a  (pParse->db) );.
22df7 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
22df8 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65  =sqlite3ReadSche
22df9 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20  ma(pParse) ){.  
22dfa 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
22dfb 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 32 21   assert( pName2!
22dfc 3d 30 20 7c 7c 20 70 4e 61 6d 65 31 3d 3d 30 20  =0 || pName1==0 
22dfd 29 3b 0a 20 20 69 66 28 20 70 4e 61 6d 65 31 3d  );.  if( pName1=
22dfe 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 46 6f 72  =0 ){.    /* For
22dff 6d 20 31 3a 20 20 41 6e 61 6c 79 7a 65 20 65 76  m 1:  Analyze ev
22e00 65 72 79 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20  erything */.    
22e01 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
22e02 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  Db; i++){.      
22e03 69 66 28 20 69 3d 3d 31 20 29 20 63 6f 6e 74 69  if( i==1 ) conti
22e04 6e 75 65 3b 20 20 2f 2a 20 44 6f 20 6e 6f 74 20  nue;  /* Do not 
22e05 61 6e 61 6c 79 7a 65 20 74 68 65 20 54 45 4d 50  analyze the TEMP
22e06 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20   database */.   
22e07 20 20 20 61 6e 61 6c 79 7a 65 44 61 74 61 62 61     analyzeDataba
22e08 73 65 28 70 50 61 72 73 65 2c 20 69 29 3b 0a 20  se(pParse, i);. 
22e09 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
22e0a 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20 29 7b   pName2->n==0 ){
22e0b 0a 20 20 20 20 2f 2a 20 46 6f 72 6d 20 32 3a 20  .    /* Form 2: 
22e0c 20 41 6e 61 6c 79 7a 65 20 74 68 65 20 64 61 74   Analyze the dat
22e0d 61 62 61 73 65 20 6f 72 20 74 61 62 6c 65 20 6e  abase or table n
22e0e 61 6d 65 64 20 2a 2f 0a 20 20 20 20 69 44 62 20  amed */.    iDb 
22e0f 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28  = sqlite3FindDb(
22e10 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20  db, pName1);.   
22e11 20 69 66 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20   if( iDb>=0 ){. 
22e12 20 20 20 20 20 61 6e 61 6c 79 7a 65 44 61 74 61       analyzeData
22e13 62 61 73 65 28 70 50 61 72 73 65 2c 20 69 44 62  base(pParse, iDb
22e14 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
22e15 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e      z = sqlite3N
22e16 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
22e17 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 20 20   pName1);.      
22e18 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 20  if( z ){.       
22e19 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33 4c   pTab = sqlite3L
22e1a 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73  ocateTable(pPars
22e1b 65 2c 20 30 2c 20 7a 2c 20 30 29 3b 0a 20 20 20  e, 0, z, 0);.   
22e1c 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
22e1d 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 20  ee(db, z);.     
22e1e 20 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20     if( pTab ){. 
22e1f 20 20 20 20 20 20 20 20 20 61 6e 61 6c 79 7a 65           analyze
22e20 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54  Table(pParse, pT
22e21 61 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ab);.        }. 
22e22 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
22e23 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 46 6f 72  else{.    /* For
22e24 6d 20 33 3a 20 41 6e 61 6c 79 7a 65 20 74 68 65  m 3: Analyze the
22e25 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64   fully qualified
22e26 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20   table name */. 
22e27 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33     iDb = sqlite3
22e28 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61 72  TwoPartName(pPar
22e29 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d  se, pName1, pNam
22e2a 65 32 2c 20 26 70 54 61 62 6c 65 4e 61 6d 65 29  e2, &pTableName)
22e2b 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3e 3d 30  ;.    if( iDb>=0
22e2c 20 29 7b 0a 20 20 20 20 20 20 7a 44 62 20 3d 20   ){.      zDb = 
22e2d 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
22e2e 6d 65 3b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71  me;.      z = sq
22e2f 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
22e30 65 6e 28 64 62 2c 20 70 54 61 62 6c 65 4e 61 6d  en(db, pTableNam
22e31 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 20  e);.      if( z 
22e32 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61 62 20  ){.        pTab 
22e33 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54  = sqlite3LocateT
22e34 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20  able(pParse, 0, 
22e35 7a 2c 20 7a 44 62 29 3b 0a 20 20 20 20 20 20 20  z, zDb);.       
22e36 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
22e37 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20 20 20 69  b, z);.        i
22e38 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 20  f( pTab ){.     
22e39 20 20 20 20 20 61 6e 61 6c 79 7a 65 54 61 62 6c       analyzeTabl
22e3a 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b  e(pParse, pTab);
22e3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
22e3c 20 7d 0a 20 20 20 20 7d 20 20 20 0a 20 20 7d 0a   }.    }   .  }.
22e3d 7d 0a 0a 2f 2a 0a 2a 2a 20 55 73 65 64 20 74 6f  }../*.** Used to
22e3e 20 70 61 73 73 20 69 6e 66 6f 72 6d 61 74 69 6f   pass informatio
22e3f 6e 20 66 72 6f 6d 20 74 68 65 20 61 6e 61 6c 79  n from the analy
22e40 7a 65 72 20 72 65 61 64 65 72 20 74 68 72 6f 75  zer reader throu
22e41 67 68 20 74 6f 20 74 68 65 0a 2a 2a 20 63 61 6c  gh to the.** cal
22e42 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 2e 0a 2a  lback routine..*
22e43 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
22e44 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 61 6e   analysisInfo an
22e45 61 6c 79 73 69 73 49 6e 66 6f 3b 0a 73 74 72 75  alysisInfo;.stru
22e46 63 74 20 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20  ct analysisInfo 
22e47 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
22e48 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
22e49 44 61 74 61 62 61 73 65 3b 0a 7d 3b 0a 0a 2f 2a  Database;.};../*
22e4a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 62 61 63  .** This callbac
22e4b 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63  k is invoked onc
22e4c 65 20 66 6f 72 20 65 61 63 68 20 69 6e 64 65 78  e for each index
22e4d 20 77 68 65 6e 20 72 65 61 64 69 6e 67 20 74 68   when reading th
22e4e 65 0a 2a 2a 20 73 71 6c 69 74 65 5f 73 74 61 74  e.** sqlite_stat
22e4f 31 20 74 61 62 6c 65 2e 20 20 0a 2a 2a 0a 2a 2a  1 table.  .**.**
22e50 20 20 20 20 20 61 72 67 76 5b 30 5d 20 3d 20 6e       argv[0] = n
22e51 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78  ame of the index
22e52 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b 31 5d 20  .**     argv[1] 
22e53 3d 20 72 65 73 75 6c 74 73 20 6f 66 20 61 6e 61  = results of ana
22e54 6c 79 73 69 73 20 2d 20 6f 6e 20 69 6e 74 65 67  lysis - on integ
22e55 65 72 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75  er for each colu
22e56 6d 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  mn.*/.static int
22e57 20 61 6e 61 6c 79 73 69 73 4c 6f 61 64 65 72 28   analysisLoader(
22e58 76 6f 69 64 20 2a 70 44 61 74 61 2c 20 69 6e 74  void *pData, int
22e59 20 61 72 67 63 2c 20 63 68 61 72 20 2a 2a 61 72   argc, char **ar
22e5a 67 76 2c 20 63 68 61 72 20 2a 2a 4e 6f 74 55 73  gv, char **NotUs
22e5b 65 64 29 7b 0a 20 20 61 6e 61 6c 79 73 69 73 49  ed){.  analysisI
22e5c 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 28 61 6e  nfo *pInfo = (an
22e5d 61 6c 79 73 69 73 49 6e 66 6f 2a 29 70 44 61 74  alysisInfo*)pDat
22e5e 61 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64  a;.  Index *pInd
22e5f 65 78 3b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a  ex;.  int i, c;.
22e60 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 76    unsigned int v
22e61 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
22e62 7a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72  z;..  assert( ar
22e63 67 63 3d 3d 32 20 29 3b 0a 20 20 55 4e 55 53 45  gc==2 );.  UNUSE
22e64 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74  D_PARAMETER2(Not
22e65 55 73 65 64 2c 20 61 72 67 63 29 3b 0a 0a 20 20  Used, argc);..  
22e66 69 66 28 20 61 72 67 76 3d 3d 30 20 7c 7c 20 61  if( argv==0 || a
22e67 72 67 76 5b 30 5d 3d 3d 30 20 7c 7c 20 61 72 67  rgv[0]==0 || arg
22e68 76 5b 31 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 72  v[1]==0 ){.    r
22e69 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70  eturn 0;.  }.  p
22e6a 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 46  Index = sqlite3F
22e6b 69 6e 64 49 6e 64 65 78 28 70 49 6e 66 6f 2d 3e  indIndex(pInfo->
22e6c 64 62 2c 20 61 72 67 76 5b 30 5d 2c 20 70 49 6e  db, argv[0], pIn
22e6d 66 6f 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a  fo->zDatabase);.
22e6e 20 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20    if( pIndex==0 
22e6f 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
22e70 0a 20 20 7d 0a 20 20 7a 20 3d 20 61 72 67 76 5b  .  }.  z = argv[
22e71 31 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 2a  1];.  for(i=0; *
22e72 7a 20 26 26 20 69 3c 3d 70 49 6e 64 65 78 2d 3e  z && i<=pIndex->
22e73 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
22e74 20 20 20 76 20 3d 20 30 3b 0a 20 20 20 20 77 68     v = 0;.    wh
22e75 69 6c 65 28 20 28 63 3d 7a 5b 30 5d 29 3e 3d 27  ile( (c=z[0])>='
22e76 30 27 20 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a  0' && c<='9' ){.
22e77 20 20 20 20 20 20 76 20 3d 20 76 2a 31 30 20 2b        v = v*10 +
22e78 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20   c - '0';.      
22e79 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  z++;.    }.    p
22e7a 49 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73 74 5b  Index->aiRowEst[
22e7b 69 5d 20 3d 20 76 3b 0a 20 20 20 20 69 66 28 20  i] = v;.    if( 
22e7c 2a 7a 3d 3d 27 20 27 20 29 20 7a 2b 2b 3b 0a 20  *z==' ' ) z++;. 
22e7d 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
22e7e 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 49  ../*.** If the I
22e7f 6e 64 65 78 2e 61 53 61 6d 70 6c 65 20 76 61 72  ndex.aSample var
22e80 69 61 62 6c 65 20 69 73 20 6e 6f 74 20 4e 55 4c  iable is not NUL
22e81 4c 2c 20 64 65 6c 65 74 65 20 74 68 65 20 61 53  L, delete the aS
22e82 61 6d 70 6c 65 5b 5d 20 61 72 72 61 79 0a 2a 2a  ample[] array.**
22e83 20 61 6e 64 20 69 74 73 20 63 6f 6e 74 65 6e 74   and its content
22e84 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
22e85 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
22e86 33 44 65 6c 65 74 65 49 6e 64 65 78 53 61 6d 70  3DeleteIndexSamp
22e87 6c 65 73 28 49 6e 64 65 78 20 2a 70 49 64 78 29  les(Index *pIdx)
22e88 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  {.#ifdef SQLITE_
22e89 45 4e 41 42 4c 45 5f 53 54 41 54 32 0a 20 20 69  ENABLE_STAT2.  i
22e8a 66 28 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65  f( pIdx->aSample
22e8b 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 3b 0a 20   ){.    int j;. 
22e8c 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 4d 65     sqlite3 *dbMe
22e8d 6d 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65  m = pIdx->pTable
22e8e 2d 3e 64 62 4d 65 6d 3b 0a 20 20 20 20 66 6f 72  ->dbMem;.    for
22e8f 28 6a 3d 30 3b 20 6a 3c 53 51 4c 49 54 45 5f 49  (j=0; j<SQLITE_I
22e90 4e 44 45 58 5f 53 41 4d 50 4c 45 53 3b 20 6a 2b  NDEX_SAMPLES; j+
22e91 2b 29 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 53  +){.      IndexS
22e92 61 6d 70 6c 65 20 2a 70 20 3d 20 26 70 49 64 78  ample *p = &pIdx
22e93 2d 3e 61 53 61 6d 70 6c 65 5b 6a 5d 3b 0a 20 20  ->aSample[j];.  
22e94 20 20 20 20 69 66 28 20 70 2d 3e 65 54 79 70 65      if( p->eType
22e95 3d 3d 53 51 4c 49 54 45 5f 54 45 58 54 20 7c 7c  ==SQLITE_TEXT ||
22e96 20 70 2d 3e 65 54 79 70 65 3d 3d 53 51 4c 49 54   p->eType==SQLIT
22e97 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 20 20  E_BLOB ){.      
22e98 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
22e99 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d 3e 64 62  pIdx->pTable->db
22e9a 4d 65 6d 2c 20 70 2d 3e 75 2e 7a 29 3b 0a 20 20  Mem, p->u.z);.  
22e9b 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
22e9c 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
22e9d 4d 65 6d 2c 20 70 49 64 78 2d 3e 61 53 61 6d 70  Mem, pIdx->aSamp
22e9e 6c 65 29 3b 0a 20 20 20 20 70 49 64 78 2d 3e 61  le);.    pIdx->a
22e9f 53 61 6d 70 6c 65 20 3d 20 30 3b 0a 20 20 7d 0a  Sample = 0;.  }.
22ea0 23 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50  #else.  UNUSED_P
22ea1 41 52 41 4d 45 54 45 52 28 70 49 64 78 29 3b 0a  ARAMETER(pIdx);.
22ea2 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
22ea3 4c 6f 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74  Load the content
22ea4 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73   of the sqlite_s
22ea5 74 61 74 31 20 61 6e 64 20 73 71 6c 69 74 65 5f  tat1 and sqlite_
22ea6 73 74 61 74 32 20 74 61 62 6c 65 73 2e 20 54 68  stat2 tables. Th
22ea7 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 73 20 6f 66  e.** contents of
22ea8 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 61 72   sqlite_stat1 ar
22ea9 65 20 75 73 65 64 20 74 6f 20 70 6f 70 75 6c 61  e used to popula
22eaa 74 65 20 74 68 65 20 49 6e 64 65 78 2e 61 69 52  te the Index.aiR
22eab 6f 77 45 73 74 5b 5d 0a 2a 2a 20 61 72 72 61 79  owEst[].** array
22eac 73 2e 20 54 68 65 20 63 6f 6e 74 65 6e 74 73 20  s. The contents 
22ead 6f 66 20 73 71 6c 69 74 65 5f 73 74 61 74 32 20  of sqlite_stat2 
22eae 61 72 65 20 75 73 65 64 20 74 6f 20 70 6f 70 75  are used to popu
22eaf 6c 61 74 65 20 74 68 65 0a 2a 2a 20 49 6e 64 65  late the.** Inde
22eb0 78 2e 61 53 61 6d 70 6c 65 5b 5d 20 61 72 72 61  x.aSample[] arra
22eb1 79 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ys..**.** If the
22eb2 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61   sqlite_stat1 ta
22eb3 62 6c 65 20 69 73 20 6e 6f 74 20 70 72 65 73 65  ble is not prese
22eb4 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  nt in the databa
22eb5 73 65 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  se, SQLITE_ERROR
22eb6 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e  .** is returned.
22eb7 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 65   In this case, e
22eb8 76 65 6e 20 69 66 20 53 51 4c 49 54 45 5f 45 4e  ven if SQLITE_EN
22eb9 41 42 4c 45 5f 53 54 41 54 32 20 77 61 73 20 64  ABLE_STAT2 was d
22eba 65 66 69 6e 65 64 20 0a 2a 2a 20 64 75 72 69 6e  efined .** durin
22ebb 67 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 61 6e  g compilation an
22ebc 64 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  d the sqlite_sta
22ebd 74 32 20 74 61 62 6c 65 20 69 73 20 70 72 65 73  t2 table is pres
22ebe 65 6e 74 2c 20 6e 6f 20 64 61 74 61 20 69 73 20  ent, no data is 
22ebf 0a 2a 2a 20 72 65 61 64 20 66 72 6f 6d 20 69 74  .** read from it
22ec0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54  ..**.** If SQLIT
22ec1 45 5f 45 4e 41 42 4c 45 5f 53 54 41 54 32 20 77  E_ENABLE_STAT2 w
22ec2 61 73 20 64 65 66 69 6e 65 64 20 64 75 72 69 6e  as defined durin
22ec3 67 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 61 6e  g compilation an
22ec4 64 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65  d the .** sqlite
22ec5 5f 73 74 61 74 32 20 74 61 62 6c 65 20 69 73 20  _stat2 table is 
22ec6 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e 20 74  not present in t
22ec7 68 65 20 64 61 74 61 62 61 73 65 2c 20 53 51 4c  he database, SQL
22ec8 49 54 45 5f 45 52 52 4f 52 20 69 73 0a 2a 2a 20  ITE_ERROR is.** 
22ec9 72 65 74 75 72 6e 65 64 2e 20 48 6f 77 65 76 65  returned. Howeve
22eca 72 2c 20 69 6e 20 74 68 69 73 20 63 61 73 65 2c  r, in this case,
22ecb 20 64 61 74 61 20 69 73 20 72 65 61 64 20 66 72   data is read fr
22ecc 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  om the sqlite_st
22ecd 61 74 31 0a 2a 2a 20 74 61 62 6c 65 20 28 69 66  at1.** table (if
22ece 20 69 74 20 69 73 20 70 72 65 73 65 6e 74 29 20   it is present) 
22ecf 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67  before returning
22ed0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 4f 4f  ..**.** If an OO
22ed1 4d 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20  M error occurs, 
22ed2 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6c  this function al
22ed3 77 61 79 73 20 73 65 74 73 20 64 62 2d 3e 6d 61  ways sets db->ma
22ed4 6c 6c 6f 63 46 61 69 6c 65 64 2e 0a 2a 2a 20 54  llocFailed..** T
22ed5 68 69 73 20 6d 65 61 6e 73 20 69 66 20 74 68 65  his means if the
22ed6 20 63 61 6c 6c 65 72 20 64 6f 65 73 20 6e 6f 74   caller does not
22ed7 20 63 61 72 65 20 61 62 6f 75 74 20 6f 74 68 65   care about othe
22ed8 72 20 65 72 72 6f 72 73 2c 20 74 68 65 20 72 65  r errors, the re
22ed9 74 75 72 6e 0a 2a 2a 20 63 6f 64 65 20 6d 61 79  turn.** code may
22eda 20 62 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2f 0a   be ignored..*/.
22edb 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
22edc 6e 74 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73  nt sqlite3Analys
22edd 69 73 4c 6f 61 64 28 73 71 6c 69 74 65 33 20 2a  isLoad(sqlite3 *
22ede 64 62 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20  db, int iDb){.  
22edf 61 6e 61 6c 79 73 69 73 49 6e 66 6f 20 73 49 6e  analysisInfo sIn
22ee0 66 6f 3b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a  fo;.  HashElem *
22ee1 69 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b  i;.  char *zSql;
22ee2 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73  .  int rc;..  as
22ee3 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20  sert( iDb>=0 && 
22ee4 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  iDb<db->nDb );. 
22ee5 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62   assert( db->aDb
22ee6 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20 29 3b 0a  [iDb].pBt!=0 );.
22ee7 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
22ee8 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78  3BtreeHoldsMutex
22ee9 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42  (db->aDb[iDb].pB
22eea 74 29 20 29 3b 0a 0a 20 20 2f 2a 20 43 6c 65 61  t) );..  /* Clea
22eeb 72 20 61 6e 79 20 70 72 69 6f 72 20 73 74 61 74  r any prior stat
22eec 69 73 74 69 63 73 20 2a 2f 0a 20 20 66 6f 72 28  istics */.  for(
22eed 69 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  i=sqliteHashFirs
22eee 74 28 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  t(&db->aDb[iDb].
22eef 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68  pSchema->idxHash
22ef0 29 3b 69 3b 69 3d 73 71 6c 69 74 65 48 61 73 68  );i;i=sqliteHash
22ef1 4e 65 78 74 28 69 29 29 7b 0a 20 20 20 20 49 6e  Next(i)){.    In
22ef2 64 65 78 20 2a 70 49 64 78 20 3d 20 73 71 6c 69  dex *pIdx = sqli
22ef3 74 65 48 61 73 68 44 61 74 61 28 69 29 3b 0a 20  teHashData(i);. 
22ef4 20 20 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c     sqlite3Defaul
22ef5 74 52 6f 77 45 73 74 28 70 49 64 78 29 3b 0a 20  tRowEst(pIdx);. 
22ef6 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65     sqlite3Delete
22ef7 49 6e 64 65 78 53 61 6d 70 6c 65 73 28 70 49 64  IndexSamples(pId
22ef8 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68  x);.  }..  /* Ch
22ef9 65 63 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  eck to make sure
22efa 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
22efb 31 20 74 61 62 6c 65 20 65 78 69 73 74 73 20 2a  1 table exists *
22efc 2f 0a 20 20 73 49 6e 66 6f 2e 64 62 20 3d 20 64  /.  sInfo.db = d
22efd 62 3b 0a 20 20 73 49 6e 66 6f 2e 7a 44 61 74 61  b;.  sInfo.zData
22efe 62 61 73 65 20 3d 20 64 62 2d 3e 61 44 62 5b 69  base = db->aDb[i
22eff 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28  Db].zName;.  if(
22f00 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c   sqlite3FindTabl
22f01 65 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 74  e(db, "sqlite_st
22f02 61 74 31 22 2c 20 73 49 6e 66 6f 2e 7a 44 61 74  at1", sInfo.zDat
22f03 61 62 61 73 65 29 3d 3d 30 20 29 7b 0a 20 20 20  abase)==0 ){.   
22f04 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
22f05 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  RROR;.  }..  /* 
22f06 4c 6f 61 64 20 6e 65 77 20 73 74 61 74 69 73 74  Load new statist
22f07 69 63 73 20 6f 75 74 20 6f 66 20 74 68 65 20 73  ics out of the s
22f08 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c  qlite_stat1 tabl
22f09 65 20 2a 2f 0a 20 20 7a 53 71 6c 20 3d 20 73 71  e */.  zSql = sq
22f0a 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
22f0b 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54 20   .      "SELECT 
22f0c 69 64 78 2c 20 73 74 61 74 20 46 52 4f 4d 20 25  idx, stat FROM %
22f0d 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c  Q.sqlite_stat1",
22f0e 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65   sInfo.zDatabase
22f0f 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 3d 3d 30  );.  if( zSql==0
22f10 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
22f11 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c  ITE_NOMEM;.  }el
22f12 73 65 7b 0a 20 20 20 20 28 76 6f 69 64 29 73 71  se{.    (void)sq
22f13 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
22f14 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  b);.    rc = sql
22f15 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53  ite3_exec(db, zS
22f16 71 6c 2c 20 61 6e 61 6c 79 73 69 73 4c 6f 61 64  ql, analysisLoad
22f17 65 72 2c 20 26 73 49 6e 66 6f 2c 20 30 29 3b 0a  er, &sInfo, 0);.
22f18 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
22f19 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20  3SafetyOn(db);. 
22f1a 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
22f1b 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 7d 0a  (db, zSql);.  }.
22f1c 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20  ..  /* Load the 
22f1d 73 74 61 74 69 73 74 69 63 73 20 66 72 6f 6d 20  statistics from 
22f1e 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 32  the sqlite_stat2
22f1f 20 74 61 62 6c 65 2e 20 2a 2f 0a 23 69 66 64 65   table. */.#ifde
22f20 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
22f21 53 54 41 54 32 0a 20 20 69 66 28 20 72 63 3d 3d  STAT2.  if( rc==
22f22 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 73 71  SQLITE_OK && !sq
22f23 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
22f24 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74 32  b, "sqlite_stat2
22f25 22 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61  ", sInfo.zDataba
22f26 73 65 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  se) ){.    rc = 
22f27 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
22f28 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
22f29 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
22f2a 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
22f2b 20 3d 20 30 3b 0a 0a 20 20 20 20 7a 53 71 6c 20   = 0;..    zSql 
22f2c 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
22f2d 28 64 62 2c 20 0a 20 20 20 20 20 20 20 20 22 53  (db, .        "S
22f2e 45 4c 45 43 54 20 69 64 78 2c 73 61 6d 70 6c 65  ELECT idx,sample
22f2f 6e 6f 2c 73 61 6d 70 6c 65 20 46 52 4f 4d 20 25  no,sample FROM %
22f30 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 32 22 2c  Q.sqlite_stat2",
22f31 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73 65   sInfo.zDatabase
22f32 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 53 71 6c  );.    if( !zSql
22f33 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
22f34 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
22f35 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 28 76   }else{.      (v
22f36 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
22f37 79 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 20 20  yOff(db);.      
22f38 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65  rc = sqlite3_pre
22f39 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d  pare(db, zSql, -
22f3a 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20  1, &pStmt, 0);. 
22f3b 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74       (void)sqlit
22f3c 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a  e3SafetyOn(db);.
22f3d 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
22f3e 72 65 65 28 64 62 2c 20 7a 53 71 6c 29 3b 0a 20  ree(db, zSql);. 
22f3f 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63     }..    if( rc
22f40 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
22f41 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74       (void)sqlit
22f42 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
22f43 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 73 71  .      while( sq
22f44 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74  lite3_step(pStmt
22f45 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  )==SQLITE_ROW ){
22f46 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a  .        char *z
22f47 49 6e 64 65 78 20 3d 20 28 63 68 61 72 20 2a 29  Index = (char *)
22f48 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
22f49 65 78 74 28 70 53 74 6d 74 2c 20 30 29 3b 0a 20  ext(pStmt, 0);. 
22f4a 20 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49         Index *pI
22f4b 64 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  dx = sqlite3Find
22f4c 49 6e 64 65 78 28 64 62 2c 20 7a 49 6e 64 65 78  Index(db, zIndex
22f4d 2c 20 73 49 6e 66 6f 2e 7a 44 61 74 61 62 61 73  , sInfo.zDatabas
22f4e 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  e);.        if( 
22f4f 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20 20 20  pIdx ){.        
22f50 20 20 69 6e 74 20 69 53 61 6d 70 6c 65 20 3d 20    int iSample = 
22f51 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
22f52 6e 74 28 70 53 74 6d 74 2c 20 31 29 3b 0a 20 20  nt(pStmt, 1);.  
22f53 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 20          sqlite3 
22f54 2a 64 62 4d 65 6d 20 3d 20 70 49 64 78 2d 3e 70  *dbMem = pIdx->p
22f55 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 3b 0a 20 20  Table->dbMem;.  
22f56 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
22f57 64 62 4d 65 6d 3d 3d 64 62 20 7c 7c 20 64 62 4d  dbMem==db || dbM
22f58 65 6d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  em==0 );.       
22f59 20 20 20 69 66 28 20 69 53 61 6d 70 6c 65 3c 53     if( iSample<S
22f5a 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50  QLITE_INDEX_SAMP
22f5b 4c 45 53 20 26 26 20 69 53 61 6d 70 6c 65 3e 3d  LES && iSample>=
22f5c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
22f5d 20 69 6e 74 20 65 54 79 70 65 20 3d 20 73 71 6c   int eType = sql
22f5e 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65  ite3_column_type
22f5f 28 70 53 74 6d 74 2c 20 32 29 3b 0a 0a 20 20 20  (pStmt, 2);..   
22f60 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64           if( pId
22f61 78 2d 3e 61 53 61 6d 70 6c 65 3d 3d 30 20 29 7b  x->aSample==0 ){
22f62 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  .              s
22f63 74 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20  tatic const int 
22f64 73 7a 20 3d 20 73 69 7a 65 6f 66 28 49 6e 64 65  sz = sizeof(Inde
22f65 78 53 61 6d 70 6c 65 29 2a 53 51 4c 49 54 45 5f  xSample)*SQLITE_
22f66 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 3b 0a 20  INDEX_SAMPLES;. 
22f67 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49 64               pId
22f68 78 2d 3e 61 53 61 6d 70 6c 65 20 3d 20 28 49 6e  x->aSample = (In
22f69 64 65 78 53 61 6d 70 6c 65 20 2a 29 73 71 6c 69  dexSample *)sqli
22f6a 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
22f6b 64 62 4d 65 6d 2c 20 73 7a 29 3b 0a 20 20 20 20  dbMem, sz);.    
22f6c 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49            if( pI
22f6d 64 78 2d 3e 61 53 61 6d 70 6c 65 3d 3d 30 20 29  dx->aSample==0 )
22f6e 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
22f6f 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
22f70 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ed = 1;.        
22f71 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
22f72 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20               }. 
22f73 20 20 20 20 20 20 20 20 20 20 20 7d 0a 0a 20 20             }..  
22f74 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
22f75 28 20 70 49 64 78 2d 3e 61 53 61 6d 70 6c 65 20  ( pIdx->aSample 
22f76 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  );.            {
22f77 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 49  .              I
22f78 6e 64 65 78 53 61 6d 70 6c 65 20 2a 70 53 61 6d  ndexSample *pSam
22f79 70 6c 65 20 3d 20 26 70 49 64 78 2d 3e 61 53 61  ple = &pIdx->aSa
22f7a 6d 70 6c 65 5b 69 53 61 6d 70 6c 65 5d 3b 0a 20  mple[iSample];. 
22f7b 20 20 20 20 20 20 20 20 20 20 20 20 20 70 53 61               pSa
22f7c 6d 70 6c 65 2d 3e 65 54 79 70 65 20 3d 20 28 75  mple->eType = (u
22f7d 38 29 65 54 79 70 65 3b 0a 20 20 20 20 20 20 20  8)eType;.       
22f7e 20 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65         if( eType
22f7f 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  ==SQLITE_INTEGER
22f80 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54   || eType==SQLIT
22f81 45 5f 46 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20  E_FLOAT ){.     
22f82 20 20 20 20 20 20 20 20 20 20 20 70 53 61 6d 70             pSamp
22f83 6c 65 2d 3e 75 2e 72 20 3d 20 73 71 6c 69 74 65  le->u.r = sqlite
22f84 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28  3_column_double(
22f85 70 53 74 6d 74 2c 20 32 29 3b 0a 20 20 20 20 20  pStmt, 2);.     
22f86 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69           }else i
22f87 66 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45  f( eType==SQLITE
22f88 5f 54 45 58 54 20 7c 7c 20 65 54 79 70 65 3d 3d  _TEXT || eType==
22f89 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20  SQLITE_BLOB ){. 
22f8a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
22f8b 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28  onst char *z = (
22f8c 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 28 0a 20  const char *)(. 
22f8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22f8e 20 20 20 28 65 54 79 70 65 3d 3d 53 51 4c 49 54     (eType==SQLIT
22f8f 45 5f 42 4c 4f 42 29 20 3f 0a 20 20 20 20 20 20  E_BLOB) ?.      
22f90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
22f91 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
22f92 62 28 70 53 74 6d 74 2c 20 32 29 3a 0a 20 20 20  b(pStmt, 2):.   
22f93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22f94 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
22f95 74 65 78 74 28 70 53 74 6d 74 2c 20 32 29 0a 20  text(pStmt, 2). 
22f96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 29                 )
22f97 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
22f98 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74 65    int n = sqlite
22f99 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 70  3_column_bytes(p
22f9a 53 74 6d 74 2c 20 32 29 3b 0a 20 20 20 20 20 20  Stmt, 2);.      
22f9b 20 20 20 20 20 20 20 20 20 20 69 66 28 20 6e 3e            if( n>
22f9c 32 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  24 ){.          
22f9d 20 20 20 20 20 20 20 20 6e 20 3d 20 32 34 3b 0a          n = 24;.
22f9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22f9f 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }.              
22fa0 20 20 70 53 61 6d 70 6c 65 2d 3e 6e 42 79 74 65    pSample->nByte
22fa1 20 3d 20 28 75 38 29 6e 3b 0a 20 20 20 20 20 20   = (u8)n;.      
22fa2 20 20 20 20 20 20 20 20 20 20 70 53 61 6d 70 6c            pSampl
22fa3 65 2d 3e 75 2e 7a 20 3d 20 73 71 6c 69 74 65 33  e->u.z = sqlite3
22fa4 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 4d 65  DbMallocRaw(dbMe
22fa5 6d 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20 20 20  m, n);.         
22fa6 20 20 20 20 20 20 20 69 66 28 20 70 53 61 6d 70         if( pSamp
22fa7 6c 65 2d 3e 75 2e 7a 20 29 7b 0a 20 20 20 20 20  le->u.z ){.     
22fa8 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d               mem
22fa9 63 70 79 28 70 53 61 6d 70 6c 65 2d 3e 75 2e 7a  cpy(pSample->u.z
22faa 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20  , z, n);.       
22fab 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
22fac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22fad 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
22fae 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ed = 1;.        
22faf 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
22fb0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
22fb1 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   }.             
22fb2 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d   }.            }
22fb3 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
22fb4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
22fb5 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
22fb6 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74  3_finalize(pStmt
22fb7 29 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73  );.      (void)s
22fb8 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
22fb9 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  b);.    }.  }.#e
22fba 6e 64 69 66 0a 0a 20 20 69 66 28 20 72 63 3d 3d  ndif..  if( rc==
22fbb 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a  SQLITE_NOMEM ){.
22fbc 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
22fbd 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  iled = 1;.  }.  
22fbe 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23  return rc;.}...#
22fbf 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
22fc0 4f 4d 49 54 5f 41 4e 41 4c 59 5a 45 20 2a 2f 0a  OMIT_ANALYZE */.
22fc1 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
22fc2 20 45 6e 64 20 6f 66 20 61 6e 61 6c 79 7a 65 2e   End of analyze.
22fc3 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
22fc4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22fc5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
22fc6 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
22fc7 20 42 65 67 69 6e 20 66 69 6c 65 20 61 74 74 61   Begin file atta
22fc8 63 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ch.c ***********
22fc9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22fca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
22fcb 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 41 70 72 69  ./*.** 2003 Apri
22fcc 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  l 6.**.** The au
22fcd 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
22fce 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
22fcf 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
22fd0 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
22fd1 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
22fd2 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
22fd3 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
22fd4 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
22fd5 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
22fd6 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
22fd7 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
22fd8 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
22fd9 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
22fda 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
22fdb 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
22fdc 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
22fdd 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
22fde 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22fdf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22fe1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22fe2 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
22fe3 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73  contains code us
22fe4 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
22fe5 74 68 65 20 41 54 54 41 43 48 20 61 6e 64 20 44  the ATTACH and D
22fe6 45 54 41 43 48 20 63 6f 6d 6d 61 6e 64 73 2e 0a  ETACH commands..
22fe7 2a 2a 0a 2a 2a 20 24 49 64 3a 20 61 74 74 61 63  **.** $Id: attac
22fe8 68 2e 63 2c 76 20 31 2e 39 33 20 32 30 30 39 2f  h.c,v 1.93 2009/
22fe9 30 35 2f 33 31 20 32 31 3a 32 31 3a 34 31 20 64  05/31 21:21:41 d
22fea 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66  rh Exp $.*/..#if
22feb 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
22fec 5f 41 54 54 41 43 48 0a 2f 2a 0a 2a 2a 20 52 65  _ATTACH./*.** Re
22fed 73 6f 6c 76 65 20 61 6e 20 65 78 70 72 65 73 73  solve an express
22fee 69 6f 6e 20 74 68 61 74 20 77 61 73 20 70 61 72  ion that was par
22fef 74 20 6f 66 20 61 6e 20 41 54 54 41 43 48 20 6f  t of an ATTACH o
22ff0 72 20 44 45 54 41 43 48 20 73 74 61 74 65 6d 65  r DETACH stateme
22ff1 6e 74 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 73  nt. This.** is s
22ff2 6c 69 67 68 74 6c 79 20 64 69 66 66 65 72 65 6e  lightly differen
22ff3 74 20 66 72 6f 6d 20 72 65 73 6f 6c 76 69 6e 67  t from resolving
22ff4 20 61 20 6e 6f 72 6d 61 6c 20 53 51 4c 20 65 78   a normal SQL ex
22ff5 70 72 65 73 73 69 6f 6e 2c 20 62 65 63 61 75 73  pression, becaus
22ff6 65 20 73 69 6d 70 6c 65 0a 2a 2a 20 69 64 65 6e  e simple.** iden
22ff7 74 69 66 69 65 72 73 20 61 72 65 20 74 72 65 61  tifiers are trea
22ff8 74 65 64 20 61 73 20 73 74 72 69 6e 67 73 2c 20  ted as strings, 
22ff9 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 63 6f 6c  not possible col
22ffa 75 6d 6e 20 6e 61 6d 65 73 20 6f 72 20 61 6c 69  umn names or ali
22ffb 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 2e  ases..**.** i.e.
22ffc 20 69 66 20 74 68 65 20 70 61 72 73 65 72 20 73   if the parser s
22ffd 65 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 41  ees:.**.**     A
22ffe 54 54 41 43 48 20 44 41 54 41 42 41 53 45 20 61  TTACH DATABASE a
22fff 62 63 20 41 53 20 64 65 66 0a 2a 2a 0a 2a 2a 20  bc AS def.**.** 
23000 69 74 20 74 72 65 61 74 73 20 74 68 65 20 74 77  it treats the tw
23001 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 73  o expressions as
23002 20 6c 69 74 65 72 61 6c 20 73 74 72 69 6e 67 73   literal strings
23003 20 27 61 62 63 27 20 61 6e 64 20 27 64 65 66 27   'abc' and 'def'
23004 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 6c   instead of.** l
23005 6f 6f 6b 69 6e 67 20 66 6f 72 20 63 6f 6c 75 6d  ooking for colum
23006 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e  ns of the same n
23007 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ame..**.** This 
23008 6f 6e 6c 79 20 61 70 70 6c 69 65 73 20 74 6f 20  only applies to 
23009 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66  the root node of
2300a 20 70 45 78 70 72 2c 20 73 6f 20 74 68 65 20 73   pExpr, so the s
2300b 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20  tatement:.**.** 
2300c 20 20 20 20 41 54 54 41 43 48 20 44 41 54 41 42      ATTACH DATAB
2300d 41 53 45 20 61 62 63 7c 7c 64 65 66 20 41 53 20  ASE abc||def AS 
2300e 27 64 62 32 27 0a 2a 2a 0a 2a 2a 20 77 69 6c 6c  'db2'.**.** will
2300f 20 66 61 69 6c 20 62 65 63 61 75 73 65 20 6e 65   fail because ne
23010 69 74 68 65 72 20 61 62 63 20 6f 72 20 64 65 66  ither abc or def
23011 20 63 61 6e 20 62 65 20 72 65 73 6f 6c 76 65 64   can be resolved
23012 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
23013 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78 70  resolveAttachExp
23014 72 28 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70  r(NameContext *p
23015 4e 61 6d 65 2c 20 45 78 70 72 20 2a 70 45 78 70  Name, Expr *pExp
23016 72 29 0a 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  r).{.  int rc = 
23017 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
23018 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20 69 66   pExpr ){.    if
23019 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ( pExpr->op!=TK_
2301a 49 44 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ID ){.      rc =
2301b 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
2301c 78 70 72 4e 61 6d 65 73 28 70 4e 61 6d 65 2c 20  xprNames(pName, 
2301d 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 69 66  pExpr);.      if
2301e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
2301f 26 26 20 21 73 71 6c 69 74 65 33 45 78 70 72 49  && !sqlite3ExprI
23020 73 43 6f 6e 73 74 61 6e 74 28 70 45 78 70 72 29  sConstant(pExpr)
23021 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
23022 74 65 33 45 72 72 6f 72 4d 73 67 28 70 4e 61 6d  te3ErrorMsg(pNam
23023 65 2d 3e 70 50 61 72 73 65 2c 20 22 69 6e 76 61  e->pParse, "inva
23024 6c 69 64 20 6e 61 6d 65 3a 20 5c 22 25 73 5c 22  lid name: \"%s\"
23025 22 2c 20 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b  ", pExpr->u.zTok
23026 65 6e 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  en);.        ret
23027 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
23028 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
23029 6c 73 65 7b 0a 20 20 20 20 20 20 70 45 78 70 72  lse{.      pExpr
2302a 2d 3e 6f 70 20 3d 20 54 4b 5f 53 54 52 49 4e 47  ->op = TK_STRING
2302b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
2302c 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
2302d 2a 20 41 6e 20 53 51 4c 20 75 73 65 72 2d 66 75  * An SQL user-fu
2302e 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65  nction registere
2302f 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b  d to do the work
23030 20 6f 66 20 61 6e 20 41 54 54 41 43 48 20 73 74   of an ATTACH st
23031 61 74 65 6d 65 6e 74 2e 20 54 68 65 0a 2a 2a 20  atement. The.** 
23032 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 20  three arguments 
23033 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
23034 63 6f 6d 65 20 64 69 72 65 63 74 6c 79 20 66 72  come directly fr
23035 6f 6d 20 61 6e 20 61 74 74 61 63 68 20 73 74 61  om an attach sta
23036 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20  tement:.**.**   
23037 20 20 41 54 54 41 43 48 20 44 41 54 41 42 41 53    ATTACH DATABAS
23038 45 20 78 20 41 53 20 79 20 4b 45 59 20 7a 0a 2a  E x AS y KEY z.*
23039 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  *.**     SELECT 
2303a 73 71 6c 69 74 65 5f 61 74 74 61 63 68 28 78 2c  sqlite_attach(x,
2303b 20 79 2c 20 7a 29 0a 2a 2a 0a 2a 2a 20 49 66 20   y, z).**.** If 
2303c 74 68 65 20 6f 70 74 69 6f 6e 61 6c 20 22 4b 45  the optional "KE
2303d 59 20 7a 22 20 73 79 6e 74 61 78 20 69 73 20 6f  Y z" syntax is o
2303e 6d 69 74 74 65 64 2c 20 61 6e 20 53 51 4c 20 4e  mitted, an SQL N
2303f 55 4c 4c 20 69 73 20 70 61 73 73 65 64 20 61 73  ULL is passed as
23040 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72   the.** third ar
23041 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69  gument..*/.stati
23042 63 20 76 6f 69 64 20 61 74 74 61 63 68 46 75 6e  c void attachFun
23043 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
23044 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
23045 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20   int NotUsed,.  
23046 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
23047 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  argv.){.  int i;
23048 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20  .  int rc = 0;. 
23049 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
2304a 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
2304b 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
2304c 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  );.  const char 
2304d 2a 7a 4e 61 6d 65 3b 0a 20 20 63 6f 6e 73 74 20  *zName;.  const 
2304e 63 68 61 72 20 2a 7a 46 69 6c 65 3b 0a 20 20 44  char *zFile;.  D
2304f 62 20 2a 61 4e 65 77 3b 0a 20 20 63 68 61 72 20  b *aNew;.  char 
23050 2a 7a 45 72 72 44 79 6e 20 3d 20 30 3b 0a 0a 20  *zErrDyn = 0;.. 
23051 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
23052 52 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 7a  R(NotUsed);..  z
23053 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68  File = (const ch
23054 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  ar *)sqlite3_val
23055 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
23056 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e  ;.  zName = (con
23057 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65  st char *)sqlite
23058 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
23059 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20 7a 46 69  v[1]);.  if( zFi
2305a 6c 65 3d 3d 30 20 29 20 7a 46 69 6c 65 20 3d 20  le==0 ) zFile = 
2305b 22 22 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 3d  "";.  if( zName=
2305c 3d 30 20 29 20 7a 4e 61 6d 65 20 3d 20 22 22 3b  =0 ) zName = "";
2305d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72  ..  /* Check for
2305e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65   the following e
2305f 72 72 6f 72 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  rrors:.  **.  **
23060 20 20 20 20 20 2a 20 54 6f 6f 20 6d 61 6e 79 20       * Too many 
23061 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
23062 65 73 2c 0a 20 20 2a 2a 20 20 20 20 20 2a 20 54  es,.  **     * T
23063 72 61 6e 73 61 63 74 69 6f 6e 20 63 75 72 72 65  ransaction curre
23064 6e 74 6c 79 20 6f 70 65 6e 0a 20 20 2a 2a 20 20  ntly open.  **  
23065 20 20 20 2a 20 53 70 65 63 69 66 69 65 64 20 64     * Specified d
23066 61 74 61 62 61 73 65 20 6e 61 6d 65 20 61 6c 72  atabase name alr
23067 65 61 64 79 20 62 65 69 6e 67 20 75 73 65 64 2e  eady being used.
23068 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  .  */.  if( db->
23069 6e 44 62 3e 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b  nDb>=db->aLimit[
2306a 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54  SQLITE_LIMIT_ATT
2306b 41 43 48 45 44 5d 2b 32 20 29 7b 0a 20 20 20 20  ACHED]+2 ){.    
2306c 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65  zErrDyn = sqlite
2306d 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 74 6f  3MPrintf(db, "to
2306e 6f 20 6d 61 6e 79 20 61 74 74 61 63 68 65 64 20  o many attached 
2306f 64 61 74 61 62 61 73 65 73 20 2d 20 6d 61 78 20  databases - max 
23070 25 64 22 2c 20 0a 20 20 20 20 20 20 64 62 2d 3e  %d", .      db->
23071 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
23072 4d 49 54 5f 41 54 54 41 43 48 45 44 5d 0a 20 20  MIT_ATTACHED].  
23073 20 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20 61 74    );.    goto at
23074 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a  tach_error;.  }.
23075 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43    if( !db->autoC
23076 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 7a 45 72  ommit ){.    zEr
23077 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d 50  rDyn = sqlite3MP
23078 72 69 6e 74 66 28 64 62 2c 20 22 63 61 6e 6e 6f  rintf(db, "canno
23079 74 20 41 54 54 41 43 48 20 64 61 74 61 62 61 73  t ATTACH databas
2307a 65 20 77 69 74 68 69 6e 20 74 72 61 6e 73 61 63  e within transac
2307b 74 69 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f  tion");.    goto
2307c 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   attach_error;. 
2307d 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
2307e 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
2307f 20 20 20 63 68 61 72 20 2a 7a 20 3d 20 64 62 2d     char *z = db-
23080 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20  >aDb[i].zName;. 
23081 20 20 20 61 73 73 65 72 74 28 20 7a 20 26 26 20     assert( z && 
23082 7a 4e 61 6d 65 20 29 3b 0a 20 20 20 20 69 66 28  zName );.    if(
23083 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
23084 7a 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a  z, zName)==0 ){.
23085 20 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20        zErrDyn = 
23086 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
23087 62 2c 20 22 64 61 74 61 62 61 73 65 20 25 73 20  b, "database %s 
23088 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 75 73  is already in us
23089 65 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  e", zName);.    
2308a 20 20 67 6f 74 6f 20 61 74 74 61 63 68 5f 65 72    goto attach_er
2308b 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ror;.    }.  }..
2308c 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68    /* Allocate th
2308d 65 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 20 74  e new entry in t
2308e 68 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61 72 72  he db->aDb[] arr
2308f 61 79 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 73  ay and initialis
23090 65 20 74 68 65 20 73 63 68 65 6d 61 0a 20 20 2a  e the schema.  *
23091 2a 20 68 61 73 68 20 74 61 62 6c 65 73 2e 0a 20  * hash tables.. 
23092 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 61 44   */.  if( db->aD
23093 62 3d 3d 64 62 2d 3e 61 44 62 53 74 61 74 69 63  b==db->aDbStatic
23094 20 29 7b 0a 20 20 20 20 61 4e 65 77 20 3d 20 73   ){.    aNew = s
23095 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
23096 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d  w(db, sizeof(db-
23097 3e 61 44 62 5b 30 5d 29 2a 33 20 29 3b 0a 20 20  >aDb[0])*3 );.  
23098 20 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 20    if( aNew==0 ) 
23099 72 65 74 75 72 6e 3b 0a 20 20 20 20 6d 65 6d 63  return;.    memc
2309a 70 79 28 61 4e 65 77 2c 20 64 62 2d 3e 61 44 62  py(aNew, db->aDb
2309b 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62  , sizeof(db->aDb
2309c 5b 30 5d 29 2a 32 29 3b 0a 20 20 7d 65 6c 73 65  [0])*2);.  }else
2309d 7b 0a 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c  {.    aNew = sql
2309e 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62  ite3DbRealloc(db
2309f 2c 20 64 62 2d 3e 61 44 62 2c 20 73 69 7a 65 6f  , db->aDb, sizeo
230a0 66 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 28 64  f(db->aDb[0])*(d
230a1 62 2d 3e 6e 44 62 2b 31 29 20 29 3b 0a 20 20 20  b->nDb+1) );.   
230a2 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 20 72   if( aNew==0 ) r
230a3 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 64 62 2d  eturn;.  }.  db-
230a4 3e 61 44 62 20 3d 20 61 4e 65 77 3b 0a 20 20 61  >aDb = aNew;.  a
230a5 4e 65 77 20 3d 20 26 64 62 2d 3e 61 44 62 5b 64  New = &db->aDb[d
230a6 62 2d 3e 6e 44 62 5d 3b 0a 20 20 6d 65 6d 73 65  b->nDb];.  memse
230a7 74 28 61 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f  t(aNew, 0, sizeo
230a8 66 28 2a 61 4e 65 77 29 29 3b 0a 0a 20 20 2f 2a  f(*aNew));..  /*
230a9 20 4f 70 65 6e 20 74 68 65 20 64 61 74 61 62 61   Open the databa
230aa 73 65 20 66 69 6c 65 2e 20 49 66 20 74 68 65 20  se file. If the 
230ab 62 74 72 65 65 20 69 73 20 73 75 63 63 65 73 73  btree is success
230ac 66 75 6c 6c 79 20 6f 70 65 6e 65 64 2c 20 75 73  fully opened, us
230ad 65 0a 20 20 2a 2a 20 69 74 20 74 6f 20 6f 62 74  e.  ** it to obt
230ae 61 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  ain the database
230af 20 73 63 68 65 6d 61 2e 20 41 74 20 74 68 69 73   schema. At this
230b0 20 70 6f 69 6e 74 20 74 68 65 20 73 63 68 65 6d   point the schem
230b1 61 20 6d 61 79 0a 20 20 2a 2a 20 6f 72 20 6d 61  a may.  ** or ma
230b2 79 20 6e 6f 74 20 62 65 20 69 6e 69 74 69 61 6c  y not be initial
230b3 69 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 72 63 20  ised..  */.  rc 
230b4 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 61  = sqlite3BtreeFa
230b5 63 74 6f 72 79 28 64 62 2c 20 7a 46 69 6c 65 2c  ctory(db, zFile,
230b6 20 30 2c 20 53 51 4c 49 54 45 5f 44 45 46 41 55   0, SQLITE_DEFAU
230b7 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 2c 0a 20  LT_CACHE_SIZE,. 
230b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
230b9 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6f 70            db->op
230ba 65 6e 46 6c 61 67 73 20 7c 20 53 51 4c 49 54 45  enFlags | SQLITE
230bb 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 2c 0a 20  _OPEN_MAIN_DB,. 
230bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
230bd 20 20 20 20 20 20 20 20 20 20 26 61 4e 65 77 2d            &aNew-
230be 3e 70 42 74 29 3b 0a 20 20 64 62 2d 3e 6e 44 62  >pBt);.  db->nDb
230bf 2b 2b 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  ++;.  if( rc==SQ
230c0 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20  LITE_CONSTRAINT 
230c1 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
230c2 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7a 45  TE_ERROR;.    zE
230c3 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 4d  rrDyn = sqlite3M
230c4 50 72 69 6e 74 66 28 64 62 2c 20 22 64 61 74 61  Printf(db, "data
230c5 62 61 73 65 20 69 73 20 61 6c 72 65 61 64 79 20  base is already 
230c6 61 74 74 61 63 68 65 64 22 29 3b 0a 20 20 7d 65  attached");.  }e
230c7 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  lse if( rc==SQLI
230c8 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 50 61 67  TE_OK ){.    Pag
230c9 65 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20  er *pPager;.    
230ca 61 4e 65 77 2d 3e 70 53 63 68 65 6d 61 20 3d 20  aNew->pSchema = 
230cb 73 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65 74  sqlite3SchemaGet
230cc 28 64 62 2c 20 61 4e 65 77 2d 3e 70 42 74 29 3b  (db, aNew->pBt);
230cd 0a 20 20 20 20 69 66 28 20 21 61 4e 65 77 2d 3e  .    if( !aNew->
230ce 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20  pSchema ){.     
230cf 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
230d0 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  EM;.    }else if
230d1 28 20 61 4e 65 77 2d 3e 70 53 63 68 65 6d 61 2d  ( aNew->pSchema-
230d2 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20 26 26 20  >file_format && 
230d3 61 4e 65 77 2d 3e 70 53 63 68 65 6d 61 2d 3e 65  aNew->pSchema->e
230d4 6e 63 21 3d 45 4e 43 28 64 62 29 20 29 7b 0a 20  nc!=ENC(db) ){. 
230d5 20 20 20 20 20 7a 45 72 72 44 79 6e 20 3d 20 73       zErrDyn = s
230d6 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
230d7 2c 20 0a 20 20 20 20 20 20 20 20 22 61 74 74 61  , .        "atta
230d8 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20 6d  ched databases m
230d9 75 73 74 20 75 73 65 20 74 68 65 20 73 61 6d 65  ust use the same
230da 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 61   text encoding a
230db 73 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 22  s main database"
230dc 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  );.      rc = SQ
230dd 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
230de 7d 0a 20 20 20 20 70 50 61 67 65 72 20 3d 20 73  }.    pPager = s
230df 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
230e0 28 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 20  (aNew->pBt);.   
230e1 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63   sqlite3PagerLoc
230e2 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c  kingMode(pPager,
230e3 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64   db->dfltLockMod
230e4 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50  e);.    sqlite3P
230e5 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28  agerJournalMode(
230e6 70 50 61 67 65 72 2c 20 64 62 2d 3e 64 66 6c 74  pPager, db->dflt
230e7 4a 6f 75 72 6e 61 6c 4d 6f 64 65 29 3b 0a 20 20  JournalMode);.  
230e8 7d 0a 20 20 61 4e 65 77 2d 3e 7a 4e 61 6d 65 20  }.  aNew->zName 
230e9 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75  = sqlite3DbStrDu
230ea 70 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  p(db, zName);.  
230eb 61 4e 65 77 2d 3e 73 61 66 65 74 79 5f 6c 65 76  aNew->safety_lev
230ec 65 6c 20 3d 20 33 3b 0a 0a 23 69 66 20 53 51 4c  el = 3;..#if SQL
230ed 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20  ITE_HAS_CODEC.  
230ee 7b 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74  {.    extern int
230ef 20 73 71 6c 69 74 65 33 43 6f 64 65 63 41 74 74   sqlite3CodecAtt
230f0 61 63 68 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e  ach(sqlite3*, in
230f1 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20  t, const void*, 
230f2 69 6e 74 29 3b 0a 20 20 20 20 65 78 74 65 72 6e  int);.    extern
230f3 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64   void sqlite3Cod
230f4 65 63 47 65 74 4b 65 79 28 73 71 6c 69 74 65 33  ecGetKey(sqlite3
230f5 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 2a 2a 2c 20  *, int, void**, 
230f6 69 6e 74 2a 29 3b 0a 20 20 20 20 69 6e 74 20 6e  int*);.    int n
230f7 4b 65 79 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a  Key;.    char *z
230f8 4b 65 79 3b 0a 20 20 20 20 69 6e 74 20 74 20 3d  Key;.    int t =
230f9 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
230fa 79 70 65 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20  ype(argv[2]);.  
230fb 20 20 73 77 69 74 63 68 28 20 74 20 29 7b 0a 20    switch( t ){. 
230fc 20 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45       case SQLITE
230fd 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 20 20  _INTEGER:.      
230fe 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f 41  case SQLITE_FLOA
230ff 54 3a 0a 20 20 20 20 20 20 20 20 7a 45 72 72 44  T:.        zErrD
23100 79 6e 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  yn = sqlite3DbSt
23101 72 44 75 70 28 64 62 2c 20 22 49 6e 76 61 6c 69  rDup(db, "Invali
23102 64 20 6b 65 79 20 76 61 6c 75 65 22 29 3b 0a 20  d key value");. 
23103 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
23104 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20  TE_ERROR;.      
23105 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
23106 20 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c   .      case SQL
23107 49 54 45 5f 54 45 58 54 3a 0a 20 20 20 20 20 20  ITE_TEXT:.      
23108 63 61 73 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42  case SQLITE_BLOB
23109 3a 0a 20 20 20 20 20 20 20 20 6e 4b 65 79 20 3d  :.        nKey =
2310a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
2310b 79 74 65 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20  ytes(argv[2]);. 
2310c 20 20 20 20 20 20 20 7a 4b 65 79 20 3d 20 28 63         zKey = (c
2310d 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
2310e 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 32 5d  lue_blob(argv[2]
2310f 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
23110 65 33 43 6f 64 65 63 41 74 74 61 63 68 28 64 62  e3CodecAttach(db
23111 2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65  , db->nDb-1, zKe
23112 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20  y, nKey);.      
23113 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20 20 20    break;..      
23114 63 61 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c  case SQLITE_NULL
23115 3a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20  :.        /* No 
23116 6b 65 79 20 73 70 65 63 69 66 69 65 64 2e 20 20  key specified.  
23117 55 73 65 20 74 68 65 20 6b 65 79 20 66 72 6f 6d  Use the key from
23118 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
23119 73 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71  se */.        sq
2311a 6c 69 74 65 33 43 6f 64 65 63 47 65 74 4b 65 79  lite3CodecGetKey
2311b 28 64 62 2c 20 30 2c 20 28 76 6f 69 64 2a 2a 29  (db, 0, (void**)
2311c 26 7a 4b 65 79 2c 20 26 6e 4b 65 79 29 3b 0a 20  &zKey, &nKey);. 
2311d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f         sqlite3Co
2311e 64 65 63 41 74 74 61 63 68 28 64 62 2c 20 64 62  decAttach(db, db
2311f 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79 2c 20 6e  ->nDb-1, zKey, n
23120 4b 65 79 29 3b 0a 20 20 20 20 20 20 20 20 62 72  Key);.        br
23121 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  eak;.    }.  }.#
23122 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74  endif..  /* If t
23123 68 65 20 66 69 6c 65 20 77 61 73 20 6f 70 65 6e  he file was open
23124 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c  ed successfully,
23125 20 72 65 61 64 20 74 68 65 20 73 63 68 65 6d 61   read the schema
23126 20 66 6f 72 20 74 68 65 20 6e 65 77 20 64 61 74   for the new dat
23127 61 62 61 73 65 2e 0a 20 20 2a 2a 20 49 66 20 74  abase..  ** If t
23128 68 69 73 20 66 61 69 6c 73 2c 20 6f 72 20 69 66  his fails, or if
23129 20 6f 70 65 6e 69 6e 67 20 74 68 65 20 66 69 6c   opening the fil
2312a 65 20 66 61 69 6c 65 64 2c 20 74 68 65 6e 20 63  e failed, then c
2312b 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 61 6e  lose the file an
2312c 64 20 0a 20 20 2a 2a 20 72 65 6d 6f 76 65 20 74  d .  ** remove t
2312d 68 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68  he entry from th
2312e 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61 72 72 61  e db->aDb[] arra
2312f 79 2e 20 69 2e 65 2e 20 70 75 74 20 65 76 65 72  y. i.e. put ever
23130 79 74 68 69 6e 67 20 62 61 63 6b 20 74 68 65 20  ything back the 
23131 77 61 79 0a 20 20 2a 2a 20 77 65 20 66 6f 75 6e  way.  ** we foun
23132 64 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  d it..  */.  if(
23133 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
23134 7b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69  {.    (void)sqli
23135 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b  te3SafetyOn(db);
23136 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
23137 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20  eEnterAll(db);. 
23138 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49     rc = sqlite3I
23139 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 44 79 6e  nit(db, &zErrDyn
2313a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  );.    sqlite3Bt
2313b 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b  reeLeaveAll(db);
2313c 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74  .    (void)sqlit
2313d 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
2313e 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20 29 7b  .  }.  if( rc ){
2313f 0a 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 64  .    int iDb = d
23140 62 2d 3e 6e 44 62 20 2d 20 31 3b 0a 20 20 20 20  b->nDb - 1;.    
23141 61 73 73 65 72 74 28 20 69 44 62 3e 3d 32 20 29  assert( iDb>=2 )
23142 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44  ;.    if( db->aD
23143 62 5b 69 44 62 5d 2e 70 42 74 20 29 7b 0a 20 20  b[iDb].pBt ){.  
23144 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
23145 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62 5b 69 44  Close(db->aDb[iD
23146 62 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 20 64  b].pBt);.      d
23147 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 20  b->aDb[iDb].pBt 
23148 3d 20 30 3b 0a 20 20 20 20 20 20 64 62 2d 3e 61  = 0;.      db->a
23149 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 20  Db[iDb].pSchema 
2314a 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  = 0;.    }.    s
2314b 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
2314c 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29  nalSchema(db, 0)
2314d 3b 0a 20 20 20 20 64 62 2d 3e 6e 44 62 20 3d 20  ;.    db->nDb = 
2314e 69 44 62 3b 0a 20 20 20 20 69 66 28 20 72 63 3d  iDb;.    if( rc=
2314f 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c  =SQLITE_NOMEM ||
23150 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52   rc==SQLITE_IOER
23151 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20  R_NOMEM ){.     
23152 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
23153 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 73 71 6c  d = 1;.      sql
23154 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
23155 45 72 72 44 79 6e 29 3b 0a 20 20 20 20 20 20 7a  ErrDyn);.      z
23156 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33  ErrDyn = sqlite3
23157 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6f 75 74  MPrintf(db, "out
23158 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20   of memory");.  
23159 20 20 7d 65 6c 73 65 20 69 66 28 20 7a 45 72 72    }else if( zErr
2315a 44 79 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Dyn==0 ){.      
2315b 7a 45 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65  zErrDyn = sqlite
2315c 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 75 6e  3MPrintf(db, "un
2315d 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 64 61 74  able to open dat
2315e 61 62 61 73 65 3a 20 25 73 22 2c 20 7a 46 69 6c  abase: %s", zFil
2315f 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f  e);.    }.    go
23160 74 6f 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b  to attach_error;
23161 0a 20 20 7d 0a 20 20 0a 20 20 72 65 74 75 72 6e  .  }.  .  return
23162 3b 0a 0a 61 74 74 61 63 68 5f 65 72 72 6f 72 3a  ;..attach_error:
23163 0a 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 6e 20  .  /* Return an 
23164 65 72 72 6f 72 20 69 66 20 77 65 20 67 65 74 20  error if we get 
23165 68 65 72 65 20 2a 2f 0a 20 20 69 66 28 20 7a 45  here */.  if( zE
23166 72 72 44 79 6e 20 29 7b 0a 20 20 20 20 73 71 6c  rrDyn ){.    sql
23167 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
23168 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 44  r(context, zErrD
23169 79 6e 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c  yn, -1);.    sql
2316a 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2316b 45 72 72 44 79 6e 29 3b 0a 20 20 7d 0a 20 20 69  ErrDyn);.  }.  i
2316c 66 28 20 72 63 20 29 20 73 71 6c 69 74 65 33 5f  f( rc ) sqlite3_
2316d 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64  result_error_cod
2316e 65 28 63 6f 6e 74 65 78 74 2c 20 72 63 29 3b 0a  e(context, rc);.
2316f 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51 4c 20  }../*.** An SQL 
23170 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 72 65  user-function re
23171 67 69 73 74 65 72 65 64 20 74 6f 20 64 6f 20 74  gistered to do t
23172 68 65 20 77 6f 72 6b 20 6f 66 20 61 6e 20 44 45  he work of an DE
23173 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 20  TACH statement. 
23174 54 68 65 0a 2a 2a 20 74 68 72 65 65 20 61 72 67  The.** three arg
23175 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20 66 75  uments to the fu
23176 6e 63 74 69 6f 6e 20 63 6f 6d 65 20 64 69 72 65  nction come dire
23177 63 74 6c 79 20 66 72 6f 6d 20 61 20 64 65 74 61  ctly from a deta
23178 63 68 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a  ch statement:.**
23179 0a 2a 2a 20 20 20 20 20 44 45 54 41 43 48 20 44  .**     DETACH D
2317a 41 54 41 42 41 53 45 20 78 0a 2a 2a 0a 2a 2a 20  ATABASE x.**.** 
2317b 20 20 20 20 53 45 4c 45 43 54 20 73 71 6c 69 74      SELECT sqlit
2317c 65 5f 64 65 74 61 63 68 28 78 29 0a 2a 2f 0a 73  e_detach(x).*/.s
2317d 74 61 74 69 63 20 76 6f 69 64 20 64 65 74 61 63  tatic void detac
2317e 68 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  hFunc(.  sqlite3
2317f 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
23180 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  t,.  int NotUsed
23181 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
23182 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f  e **argv.){.  co
23183 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20  nst char *zName 
23184 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
23185 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
23186 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 73  xt(argv[0]);.  s
23187 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c  qlite3 *db = sql
23188 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
23189 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b  handle(context);
2318a 0a 20 20 69 6e 74 20 69 3b 0a 20 20 44 62 20 2a  .  int i;.  Db *
2318b 70 44 62 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  pDb = 0;.  char 
2318c 7a 45 72 72 5b 31 32 38 5d 3b 0a 0a 20 20 55 4e  zErr[128];..  UN
2318d 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
2318e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 69 66 28 20  otUsed);..  if( 
2318f 7a 4e 61 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d 65  zName==0 ) zName
23190 20 3d 20 22 22 3b 0a 20 20 66 6f 72 28 69 3d 30   = "";.  for(i=0
23191 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
23192 29 7b 0a 20 20 20 20 70 44 62 20 3d 20 26 64 62  ){.    pDb = &db
23193 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66  ->aDb[i];.    if
23194 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 20  ( pDb->pBt==0 ) 
23195 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66  continue;.    if
23196 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
23197 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61  (pDb->zName, zNa
23198 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a  me)==0 ) break;.
23199 20 20 7d 0a 0a 20 20 69 66 28 20 69 3e 3d 64 62    }..  if( i>=db
2319a 2d 3e 6e 44 62 20 29 7b 0a 20 20 20 20 73 71 6c  ->nDb ){.    sql
2319b 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
2319c 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c  zeof(zErr),zErr,
2319d 20 22 6e 6f 20 73 75 63 68 20 64 61 74 61 62 61   "no such databa
2319e 73 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b  se: %s", zName);
2319f 0a 20 20 20 20 67 6f 74 6f 20 64 65 74 61 63 68  .    goto detach
231a0 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 69 66  _error;.  }.  if
231a1 28 20 69 3c 32 20 29 7b 0a 20 20 20 20 73 71 6c  ( i<2 ){.    sql
231a2 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69  ite3_snprintf(si
231a3 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72 72 2c  zeof(zErr),zErr,
231a4 20 22 63 61 6e 6e 6f 74 20 64 65 74 61 63 68 20   "cannot detach 
231a5 64 61 74 61 62 61 73 65 20 25 73 22 2c 20 7a 4e  database %s", zN
231a6 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64  ame);.    goto d
231a7 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d  etach_error;.  }
231a8 0a 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f  .  if( !db->auto
231a9 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 71  Commit ){.    sq
231aa 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
231ab 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72  izeof(zErr), zEr
231ac 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  r,.             
231ad 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f 74 20          "cannot 
231ae 44 45 54 41 43 48 20 64 61 74 61 62 61 73 65 20  DETACH database 
231af 77 69 74 68 69 6e 20 74 72 61 6e 73 61 63 74 69  within transacti
231b0 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64  on");.    goto d
231b1 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d  etach_error;.  }
231b2 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74  .  if( sqlite3Bt
231b3 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73  reeIsInReadTrans
231b4 28 70 44 62 2d 3e 70 42 74 29 20 7c 7c 20 73 71  (pDb->pBt) || sq
231b5 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 42 61  lite3BtreeIsInBa
231b6 63 6b 75 70 28 70 44 62 2d 3e 70 42 74 29 20 29  ckup(pDb->pBt) )
231b7 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  {.    sqlite3_sn
231b8 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45  printf(sizeof(zE
231b9 72 72 29 2c 7a 45 72 72 2c 20 22 64 61 74 61 62  rr),zErr, "datab
231ba 61 73 65 20 25 73 20 69 73 20 6c 6f 63 6b 65 64  ase %s is locked
231bb 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67  ", zName);.    g
231bc 6f 74 6f 20 64 65 74 61 63 68 5f 65 72 72 6f 72  oto detach_error
231bd 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33  ;.  }..  sqlite3
231be 42 74 72 65 65 43 6c 6f 73 65 28 70 44 62 2d 3e  BtreeClose(pDb->
231bf 70 42 74 29 3b 0a 20 20 70 44 62 2d 3e 70 42 74  pBt);.  pDb->pBt
231c0 20 3d 20 30 3b 0a 20 20 70 44 62 2d 3e 70 53 63   = 0;.  pDb->pSc
231c1 68 65 6d 61 20 3d 20 30 3b 0a 20 20 73 71 6c 69  hema = 0;.  sqli
231c2 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c  te3ResetInternal
231c3 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20  Schema(db, 0);. 
231c4 20 72 65 74 75 72 6e 3b 0a 0a 64 65 74 61 63 68   return;..detach
231c5 5f 65 72 72 6f 72 3a 0a 20 20 73 71 6c 69 74 65  _error:.  sqlite
231c6 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63  3_result_error(c
231c7 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31  ontext, zErr, -1
231c8 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
231c9 20 70 72 6f 63 65 64 75 72 65 20 67 65 6e 65 72   procedure gener
231ca 61 74 65 73 20 56 44 42 45 20 63 6f 64 65 20 66  ates VDBE code f
231cb 6f 72 20 61 20 73 69 6e 67 6c 65 20 69 6e 76 6f  or a single invo
231cc 63 61 74 69 6f 6e 20 6f 66 20 65 69 74 68 65 72  cation of either
231cd 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 5f 64   the.** sqlite_d
231ce 65 74 61 63 68 28 29 20 6f 72 20 73 71 6c 69 74  etach() or sqlit
231cf 65 5f 61 74 74 61 63 68 28 29 20 53 51 4c 20 75  e_attach() SQL u
231d0 73 65 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a  ser functions..*
231d1 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f  /.static void co
231d2 64 65 41 74 74 61 63 68 28 0a 20 20 50 61 72 73  deAttach(.  Pars
231d3 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
231d4 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
231d5 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  ontext */.  int 
231d6 74 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 20  type,           
231d7 20 2f 2a 20 45 69 74 68 65 72 20 53 51 4c 49 54   /* Either SQLIT
231d8 45 5f 41 54 54 41 43 48 20 6f 72 20 53 51 4c 49  E_ATTACH or SQLI
231d9 54 45 5f 44 45 54 41 43 48 20 2a 2f 0a 20 20 46  TE_DETACH */.  F
231da 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 2c 20 20  uncDef *pFunc,  
231db 20 20 20 20 2f 2a 20 46 75 6e 63 44 65 66 20 77      /* FuncDef w
231dc 72 61 70 70 65 72 20 66 6f 72 20 64 65 74 61 63  rapper for detac
231dd 68 46 75 6e 63 28 29 20 6f 72 20 61 74 74 61 63  hFunc() or attac
231de 68 46 75 6e 63 28 29 20 2a 2f 0a 20 20 45 78 70  hFunc() */.  Exp
231df 72 20 2a 70 41 75 74 68 41 72 67 2c 20 20 20 20  r *pAuthArg,    
231e0 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20    /* Expression 
231e1 74 6f 20 70 61 73 73 20 74 6f 20 61 75 74 68 6f  to pass to autho
231e2 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63  rization callbac
231e3 6b 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 46 69  k */.  Expr *pFi
231e4 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e  lename,     /* N
231e5 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20  ame of database 
231e6 66 69 6c 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a  file */.  Expr *
231e7 70 44 62 6e 61 6d 65 2c 20 20 20 20 20 20 20 2f  pDbname,       /
231e8 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  * Name of the da
231e9 74 61 62 61 73 65 20 74 6f 20 75 73 65 20 69 6e  tabase to use in
231ea 74 65 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 45 78  ternally */.  Ex
231eb 70 72 20 2a 70 4b 65 79 20 20 20 20 20 20 20 20  pr *pKey        
231ec 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6b     /* Database k
231ed 65 79 20 66 6f 72 20 65 6e 63 72 79 70 74 69 6f  ey for encryptio
231ee 6e 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 29  n extension */.)
231ef 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4e 61  {.  int rc;.  Na
231f0 6d 65 43 6f 6e 74 65 78 74 20 73 4e 61 6d 65 3b  meContext sName;
231f1 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71  .  Vdbe *v;.  sq
231f2 6c 69 74 65 33 2a 20 64 62 20 3d 20 70 50 61 72  lite3* db = pPar
231f3 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 65  se->db;.  int re
231f4 67 41 72 67 73 3b 0a 0a 20 20 6d 65 6d 73 65 74  gArgs;..  memset
231f5 28 26 73 4e 61 6d 65 2c 20 30 2c 20 73 69 7a 65  (&sName, 0, size
231f6 6f 66 28 4e 61 6d 65 43 6f 6e 74 65 78 74 29 29  of(NameContext))
231f7 3b 0a 20 20 73 4e 61 6d 65 2e 70 50 61 72 73 65  ;.  sName.pParse
231f8 20 3d 20 70 50 61 72 73 65 3b 0a 0a 20 20 69 66   = pParse;..  if
231f9 28 20 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f  ( .      SQLITE_
231fa 4f 4b 21 3d 28 72 63 20 3d 20 72 65 73 6f 6c 76  OK!=(rc = resolv
231fb 65 41 74 74 61 63 68 45 78 70 72 28 26 73 4e 61  eAttachExpr(&sNa
231fc 6d 65 2c 20 70 46 69 6c 65 6e 61 6d 65 29 29 20  me, pFilename)) 
231fd 7c 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f  ||.      SQLITE_
231fe 4f 4b 21 3d 28 72 63 20 3d 20 72 65 73 6f 6c 76  OK!=(rc = resolv
231ff 65 41 74 74 61 63 68 45 78 70 72 28 26 73 4e 61  eAttachExpr(&sNa
23200 6d 65 2c 20 70 44 62 6e 61 6d 65 29 29 20 7c 7c  me, pDbname)) ||
23201 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b  .      SQLITE_OK
23202 21 3d 28 72 63 20 3d 20 72 65 73 6f 6c 76 65 41  !=(rc = resolveA
23203 74 74 61 63 68 45 78 70 72 28 26 73 4e 61 6d 65  ttachExpr(&sName
23204 2c 20 70 4b 65 79 29 29 0a 20 20 29 7b 0a 20 20  , pKey)).  ){.  
23205 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b    pParse->nErr++
23206 3b 0a 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63  ;.    goto attac
23207 68 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 23 69 66 6e  h_end;.  }..#ifn
23208 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
23209 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20  AUTHORIZATION.  
2320a 69 66 28 20 70 41 75 74 68 41 72 67 20 29 7b 0a  if( pAuthArg ){.
2320b 20 20 20 20 63 68 61 72 20 2a 7a 41 75 74 68 41      char *zAuthA
2320c 72 67 20 3d 20 70 41 75 74 68 41 72 67 2d 3e 75  rg = pAuthArg->u
2320d 2e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20 69 66 28  .zToken;.    if(
2320e 20 4e 45 56 45 52 28 7a 41 75 74 68 41 72 67 3d   NEVER(zAuthArg=
2320f 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  =0) ){.      got
23210 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20  o attach_end;.  
23211 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c    }.    rc = sql
23212 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
23213 61 72 73 65 2c 20 74 79 70 65 2c 20 7a 41 75 74  arse, type, zAut
23214 68 41 72 67 2c 20 30 2c 20 30 29 3b 0a 20 20 20  hArg, 0, 0);.   
23215 20 69 66 28 72 63 21 3d 53 51 4c 49 54 45 5f 4f   if(rc!=SQLITE_O
23216 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  K ){.      goto 
23217 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20 20 20 20  attach_end;.    
23218 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  }.  }.#endif /* 
23219 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
2321a 4f 52 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 0a 20  ORIZATION */... 
2321b 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
2321c 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 72  dbe(pParse);.  r
2321d 65 67 41 72 67 73 20 3d 20 73 71 6c 69 74 65 33  egArgs = sqlite3
2321e 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61  GetTempRange(pPa
2321f 72 73 65 2c 20 34 29 3b 0a 20 20 73 71 6c 69 74  rse, 4);.  sqlit
23220 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
23221 65 2c 20 70 46 69 6c 65 6e 61 6d 65 2c 20 72 65  e, pFilename, re
23222 67 41 72 67 73 29 3b 0a 20 20 73 71 6c 69 74 65  gArgs);.  sqlite
23223 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65  3ExprCode(pParse
23224 2c 20 70 44 62 6e 61 6d 65 2c 20 72 65 67 41 72  , pDbname, regAr
23225 67 73 2b 31 29 3b 0a 20 20 73 71 6c 69 74 65 33  gs+1);.  sqlite3
23226 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
23227 20 70 4b 65 79 2c 20 72 65 67 41 72 67 73 2b 32   pKey, regArgs+2
23228 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 76 20  );..  assert( v 
23229 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
2322a 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 76 20 29  led );.  if( v )
2322b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
2322c 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 75  eAddOp3(v, OP_Fu
2322d 6e 63 74 69 6f 6e 2c 20 30 2c 20 72 65 67 41 72  nction, 0, regAr
2322e 67 73 2b 33 2d 70 46 75 6e 63 2d 3e 6e 41 72 67  gs+3-pFunc->nArg
2322f 2c 20 72 65 67 41 72 67 73 2b 33 29 3b 0a 20 20  , regArgs+3);.  
23230 20 20 61 73 73 65 72 74 28 20 70 46 75 6e 63 2d    assert( pFunc-
23231 3e 6e 41 72 67 3d 3d 2d 31 20 7c 7c 20 28 70 46  >nArg==-1 || (pF
23232 75 6e 63 2d 3e 6e 41 72 67 26 30 78 66 66 29 3d  unc->nArg&0xff)=
23233 3d 70 46 75 6e 63 2d 3e 6e 41 72 67 20 29 3b 0a  =pFunc->nArg );.
23234 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
23235 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38 29 28  hangeP5(v, (u8)(
23236 70 46 75 6e 63 2d 3e 6e 41 72 67 29 29 3b 0a 20  pFunc->nArg));. 
23237 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
23238 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63  angeP4(v, -1, (c
23239 68 61 72 20 2a 29 70 46 75 6e 63 2c 20 50 34 5f  har *)pFunc, P4_
2323a 46 55 4e 43 44 45 46 29 3b 0a 0a 20 20 20 20 2f  FUNCDEF);..    /
2323b 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 45 78 70  * Code an OP_Exp
2323c 69 72 65 2e 20 46 6f 72 20 61 6e 20 41 54 54 41  ire. For an ATTA
2323d 43 48 20 73 74 61 74 65 6d 65 6e 74 2c 20 73 65  CH statement, se
2323e 74 20 50 31 20 74 6f 20 74 72 75 65 20 28 65 78  t P1 to true (ex
2323f 70 69 72 65 20 74 68 69 73 0a 20 20 20 20 2a 2a  pire this.    **
23240 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e 6c 79 29   statement only)
23241 2e 20 46 6f 72 20 44 45 54 41 43 48 2c 20 73 65  . For DETACH, se
23242 74 20 69 74 20 74 6f 20 66 61 6c 73 65 20 28 65  t it to false (e
23243 78 70 69 72 65 20 61 6c 6c 20 65 78 69 73 74 69  xpire all existi
23244 6e 67 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d  ng.    ** statem
23245 65 6e 74 73 29 2e 0a 20 20 20 20 2a 2f 0a 20 20  ents)..    */.  
23246 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
23247 4f 70 31 28 76 2c 20 4f 50 5f 45 78 70 69 72 65  Op1(v, OP_Expire
23248 2c 20 28 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f  , (type==SQLITE_
23249 41 54 54 41 43 48 29 29 3b 0a 20 20 7d 0a 20 20  ATTACH));.  }.  
2324a 0a 61 74 74 61 63 68 5f 65 6e 64 3a 0a 20 20 73  .attach_end:.  s
2324b 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
2324c 28 64 62 2c 20 70 46 69 6c 65 6e 61 6d 65 29 3b  (db, pFilename);
2324d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
2324e 6c 65 74 65 28 64 62 2c 20 70 44 62 6e 61 6d 65  lete(db, pDbname
2324f 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
23250 44 65 6c 65 74 65 28 64 62 2c 20 70 4b 65 79 29  Delete(db, pKey)
23251 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65  ;.}../*.** Calle
23252 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20  d by the parser 
23253 74 6f 20 63 6f 6d 70 69 6c 65 20 61 20 44 45 54  to compile a DET
23254 41 43 48 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ACH statement..*
23255 2a 0a 2a 2a 20 20 20 20 20 44 45 54 41 43 48 20  *.**     DETACH 
23256 70 44 62 6e 61 6d 65 0a 2a 2f 0a 53 51 4c 49 54  pDbname.*/.SQLIT
23257 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
23258 71 6c 69 74 65 33 44 65 74 61 63 68 28 50 61 72  qlite3Detach(Par
23259 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
2325a 20 2a 70 44 62 6e 61 6d 65 29 7b 0a 20 20 73 74   *pDbname){.  st
2325b 61 74 69 63 20 46 75 6e 63 44 65 66 20 64 65 74  atic FuncDef det
2325c 61 63 68 5f 66 75 6e 63 20 3d 20 7b 0a 20 20 20  ach_func = {.   
2325d 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   1,             
2325e 20 20 20 2f 2a 20 6e 41 72 67 20 2a 2f 0a 20 20     /* nArg */.  
2325f 20 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20    SQLITE_UTF8,  
23260 20 20 20 20 2f 2a 20 69 50 72 65 66 45 6e 63 20      /* iPrefEnc 
23261 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20  */.    0,       
23262 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67           /* flag
23263 73 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20  s */.    0,     
23264 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 55             /* pU
23265 73 65 72 44 61 74 61 20 2a 2f 0a 20 20 20 20 30  serData */.    0
23266 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
23267 20 2f 2a 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20   /* pNext */.   
23268 20 64 65 74 61 63 68 46 75 6e 63 2c 20 20 20 20   detachFunc,    
23269 20 20 20 2f 2a 20 78 46 75 6e 63 20 2a 2f 0a 20     /* xFunc */. 
2326a 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
2326b 20 20 20 20 20 2f 2a 20 78 53 74 65 70 20 2a 2f       /* xStep */
2326c 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  .    0,         
2326d 20 20 20 20 20 20 20 2f 2a 20 78 46 69 6e 61 6c         /* xFinal
2326e 69 7a 65 20 2a 2f 0a 20 20 20 20 22 73 71 6c 69  ize */.    "sqli
2326f 74 65 5f 64 65 74 61 63 68 22 2c 20 20 2f 2a 20  te_detach",  /* 
23270 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20 30 20 20  zName */.    0  
23271 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
23272 2a 20 70 48 61 73 68 20 2a 2f 0a 20 20 7d 3b 0a  * pHash */.  };.
23273 20 20 63 6f 64 65 41 74 74 61 63 68 28 70 50 61    codeAttach(pPa
23274 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 54 41  rse, SQLITE_DETA
23275 43 48 2c 20 26 64 65 74 61 63 68 5f 66 75 6e 63  CH, &detach_func
23276 2c 20 70 44 62 6e 61 6d 65 2c 20 30 2c 20 30 2c  , pDbname, 0, 0,
23277 20 70 44 62 6e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a   pDbname);.}../*
23278 0a 2a 2a 20 43 61 6c 6c 65 64 20 62 79 20 74 68  .** Called by th
23279 65 20 70 61 72 73 65 72 20 74 6f 20 63 6f 6d 70  e parser to comp
2327a 69 6c 65 20 61 6e 20 41 54 54 41 43 48 20 73 74  ile an ATTACH st
2327b 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20  atement..**.**  
2327c 20 20 20 41 54 54 41 43 48 20 70 20 41 53 20 70     ATTACH p AS p
2327d 44 62 6e 61 6d 65 20 4b 45 59 20 70 4b 65 79 0a  Dbname KEY pKey.
2327e 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2327f 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 74  E void sqlite3At
23280 74 61 63 68 28 50 61 72 73 65 20 2a 70 50 61 72  tach(Parse *pPar
23281 73 65 2c 20 45 78 70 72 20 2a 70 2c 20 45 78 70  se, Expr *p, Exp
23282 72 20 2a 70 44 62 6e 61 6d 65 2c 20 45 78 70 72  r *pDbname, Expr
23283 20 2a 70 4b 65 79 29 7b 0a 20 20 73 74 61 74 69   *pKey){.  stati
23284 63 20 46 75 6e 63 44 65 66 20 61 74 74 61 63 68  c FuncDef attach
23285 5f 66 75 6e 63 20 3d 20 7b 0a 20 20 20 20 33 2c  _func = {.    3,
23286 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23287 2f 2a 20 6e 41 72 67 20 2a 2f 0a 20 20 20 20 53  /* nArg */.    S
23288 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 20  QLITE_UTF8,     
23289 20 2f 2a 20 69 50 72 65 66 45 6e 63 20 2a 2f 0a   /* iPrefEnc */.
2328a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20      0,          
2328b 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 2a        /* flags *
2328c 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20  /.    0,        
2328d 20 20 20 20 20 20 20 20 2f 2a 20 70 55 73 65 72          /* pUser
2328e 44 61 74 61 20 2a 2f 0a 20 20 20 20 30 2c 20 20  Data */.    0,  
2328f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
23290 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 61 74   pNext */.    at
23291 74 61 63 68 46 75 6e 63 2c 20 20 20 20 20 20 20  tachFunc,       
23292 2f 2a 20 78 46 75 6e 63 20 2a 2f 0a 20 20 20 20  /* xFunc */.    
23293 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
23294 20 20 2f 2a 20 78 53 74 65 70 20 2a 2f 0a 20 20    /* xStep */.  
23295 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
23296 20 20 20 20 2f 2a 20 78 46 69 6e 61 6c 69 7a 65      /* xFinalize
23297 20 2a 2f 0a 20 20 20 20 22 73 71 6c 69 74 65 5f   */.    "sqlite_
23298 61 74 74 61 63 68 22 2c 20 20 2f 2a 20 7a 4e 61  attach",  /* zNa
23299 6d 65 20 2a 2f 0a 20 20 20 20 30 20 20 20 20 20  me */.    0     
2329a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70              /* p
2329b 48 61 73 68 20 2a 2f 0a 20 20 7d 3b 0a 20 20 63  Hash */.  };.  c
2329c 6f 64 65 41 74 74 61 63 68 28 70 50 61 72 73 65  odeAttach(pParse
2329d 2c 20 53 51 4c 49 54 45 5f 41 54 54 41 43 48 2c  , SQLITE_ATTACH,
2329e 20 26 61 74 74 61 63 68 5f 66 75 6e 63 2c 20 70   &attach_func, p
2329f 2c 20 70 2c 20 70 44 62 6e 61 6d 65 2c 20 70 4b  , p, pDbname, pK
232a0 65 79 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  ey);.}.#endif /*
232a1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54   SQLITE_OMIT_ATT
232a2 41 43 48 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e  ACH */../*.** In
232a3 69 74 69 61 6c 69 7a 65 20 61 20 44 62 46 69 78  itialize a DbFix
232a4 65 72 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  er structure.  T
232a5 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74  his routine must
232a6 20 62 65 20 63 61 6c 6c 65 64 20 70 72 69 6f 72   be called prior
232a7 0a 2a 2a 20 74 6f 20 70 61 73 73 69 6e 67 20 74  .** to passing t
232a8 68 65 20 73 74 72 75 63 74 75 72 65 20 74 6f 20  he structure to 
232a9 6f 6e 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74  one of the sqlit
232aa 65 46 69 78 41 41 41 41 28 29 20 72 6f 75 74 69  eFixAAAA() routi
232ab 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a 2a 0a 2a 2a  nes below..**.**
232ac 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
232ad 65 20 69 6e 64 69 63 61 74 65 73 20 77 68 65 74  e indicates whet
232ae 68 65 72 20 6f 72 20 6e 6f 74 20 66 69 78 61 74  her or not fixat
232af 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64 2e  ion is required.
232b0 20 20 54 52 55 45 0a 2a 2a 20 6d 65 61 6e 73 20    TRUE.** means 
232b1 77 65 20 64 6f 20 6e 65 65 64 20 74 6f 20 66 69  we do need to fi
232b2 78 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72  x the database r
232b3 65 66 65 72 65 6e 63 65 73 2c 20 46 41 4c 53 45  eferences, FALSE
232b4 20 6d 65 61 6e 73 20 77 65 20 64 6f 20 6e 6f 74   means we do not
232b5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
232b6 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
232b7 69 78 49 6e 69 74 28 0a 20 20 44 62 46 69 78 65  ixInit(.  DbFixe
232b8 72 20 2a 70 46 69 78 2c 20 20 20 20 20 20 2f 2a  r *pFix,      /*
232b9 20 54 68 65 20 66 69 78 65 72 20 74 6f 20 62 65   The fixer to be
232ba 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
232bb 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
232bc 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d        /* Error m
232bd 65 73 73 61 67 65 73 20 77 69 6c 6c 20 62 65 20  essages will be 
232be 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a  written here */.
232bf 20 20 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20    int iDb,      
232c0 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73        /* This is
232c1 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68   the database th
232c2 61 74 20 6d 75 73 74 20 62 65 20 75 73 65 64 20  at must be used 
232c3 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
232c4 2a 7a 54 79 70 65 2c 20 20 2f 2a 20 22 76 69 65  *zType,  /* "vie
232c5 77 22 2c 20 22 74 72 69 67 67 65 72 22 2c 20 6f  w", "trigger", o
232c6 72 20 22 69 6e 64 65 78 22 20 2a 2f 0a 20 20 63  r "index" */.  c
232c7 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  onst Token *pNam
232c8 65 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68  e  /* Name of th
232c9 65 20 76 69 65 77 2c 20 74 72 69 67 67 65 72 2c  e view, trigger,
232ca 20 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 29 7b 0a   or index */.){.
232cb 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a    sqlite3 *db;..
232cc 20 20 69 66 28 20 4e 45 56 45 52 28 69 44 62 3c    if( NEVER(iDb<
232cd 30 29 20 7c 7c 20 69 44 62 3d 3d 31 20 29 20 72  0) || iDb==1 ) r
232ce 65 74 75 72 6e 20 30 3b 0a 20 20 64 62 20 3d 20  eturn 0;.  db = 
232cf 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73  pParse->db;.  as
232d0 73 65 72 74 28 20 64 62 2d 3e 6e 44 62 3e 69 44  sert( db->nDb>iD
232d1 62 20 29 3b 0a 20 20 70 46 69 78 2d 3e 70 50 61  b );.  pFix->pPa
232d2 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20  rse = pParse;.  
232d3 70 46 69 78 2d 3e 7a 44 62 20 3d 20 64 62 2d 3e  pFix->zDb = db->
232d4 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a  aDb[iDb].zName;.
232d5 20 20 70 46 69 78 2d 3e 7a 54 79 70 65 20 3d 20    pFix->zType = 
232d6 7a 54 79 70 65 3b 0a 20 20 70 46 69 78 2d 3e 70  zType;.  pFix->p
232d7 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 3b 0a 20 20  Name = pName;.  
232d8 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a  return 1;.}../*.
232d9 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
232da 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73   set of routines
232db 20 77 61 6c 6b 20 74 68 72 6f 75 67 68 20 74 68   walk through th
232dc 65 20 70 61 72 73 65 20 74 72 65 65 20 61 6e 64  e parse tree and
232dd 20 61 73 73 69 67 6e 0a 2a 2a 20 61 20 73 70 65   assign.** a spe
232de 63 69 66 69 63 20 64 61 74 61 62 61 73 65 20 74  cific database t
232df 6f 20 61 6c 6c 20 74 61 62 6c 65 20 72 65 66 65  o all table refe
232e0 72 65 6e 63 65 73 20 77 68 65 72 65 20 74 68 65  rences where the
232e1 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a   database name.*
232e2 2a 20 77 61 73 20 6c 65 66 74 20 75 6e 73 70 65  * was left unspe
232e3 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 6f 72  cified in the or
232e4 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74 65  iginal SQL state
232e5 6d 65 6e 74 2e 20 20 54 68 65 20 70 46 69 78 20  ment.  The pFix 
232e6 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 6d 75 73  structure.** mus
232e7 74 20 68 61 76 65 20 62 65 65 6e 20 69 6e 69 74  t have been init
232e8 69 61 6c 69 7a 65 64 20 62 79 20 61 20 70 72 69  ialized by a pri
232e9 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  or call to sqlit
232ea 65 33 46 69 78 49 6e 69 74 28 29 2e 0a 2a 2a 0a  e3FixInit()..**.
232eb 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
232ec 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 6d 61  s are used to ma
232ed 6b 65 20 73 75 72 65 20 74 68 61 74 20 61 6e 20  ke sure that an 
232ee 69 6e 64 65 78 2c 20 74 72 69 67 67 65 72 2c 20  index, trigger, 
232ef 6f 72 0a 2a 2a 20 76 69 65 77 20 69 6e 20 6f 6e  or.** view in on
232f0 65 20 64 61 74 61 62 61 73 65 20 64 6f 65 73 20  e database does 
232f1 6e 6f 74 20 72 65 66 65 72 20 74 6f 20 6f 62 6a  not refer to obj
232f2 65 63 74 73 20 69 6e 20 61 20 64 69 66 66 65 72  ects in a differ
232f3 65 6e 74 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a  ent database..**
232f4 20 28 45 78 63 65 70 74 69 6f 6e 3a 20 69 6e 64   (Exception: ind
232f5 69 63 65 73 2c 20 74 72 69 67 67 65 72 73 2c 20  ices, triggers, 
232f6 61 6e 64 20 76 69 65 77 73 20 69 6e 20 74 68 65  and views in the
232f7 20 54 45 4d 50 20 64 61 74 61 62 61 73 65 20 61   TEMP database a
232f8 72 65 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 74 6f  re.** allowed to
232f9 20 72 65 66 65 72 20 74 6f 20 61 6e 79 74 68 69   refer to anythi
232fa 6e 67 2e 29 20 20 49 66 20 61 20 72 65 66 65 72  ng.)  If a refer
232fb 65 6e 63 65 20 69 73 20 65 78 70 6c 69 63 69 74  ence is explicit
232fc 6c 79 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 61 6e  ly made.** to an
232fd 20 6f 62 6a 65 63 74 20 69 6e 20 61 20 64 69 66   object in a dif
232fe 66 65 72 65 6e 74 20 64 61 74 61 62 61 73 65 2c  ferent database,
232ff 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
23300 65 20 69 73 20 61 64 64 65 64 20 74 6f 0a 2a 2a  e is added to.**
23301 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
23302 20 61 6e 64 20 74 68 65 73 65 20 72 6f 75 74 69   and these routi
23303 6e 65 73 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a  nes return non-z
23304 65 72 6f 2e 20 20 49 66 20 65 76 65 72 79 74 68  ero.  If everyth
23305 69 6e 67 0a 2a 2a 20 63 68 65 63 6b 73 20 6f 75  ing.** checks ou
23306 74 2c 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  t, these routine
23307 73 20 72 65 74 75 72 6e 20 30 2e 0a 2a 2f 0a 53  s return 0..*/.S
23308 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
23309 74 20 73 71 6c 69 74 65 33 46 69 78 53 72 63 4c  t sqlite3FixSrcL
2330a 69 73 74 28 0a 20 20 44 62 46 69 78 65 72 20 2a  ist(.  DbFixer *
2330b 70 46 69 78 2c 20 20 20 20 20 20 20 2f 2a 20 43  pFix,       /* C
2330c 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69  ontext of the fi
2330d 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 53 72 63 4c  xation */.  SrcL
2330e 69 73 74 20 2a 70 4c 69 73 74 20 20 20 20 20 20  ist *pList      
2330f 20 2f 2a 20 54 68 65 20 53 6f 75 72 63 65 20 6c   /* The Source l
23310 69 73 74 20 74 6f 20 63 68 65 63 6b 20 61 6e 64  ist to check and
23311 20 6d 6f 64 69 66 79 20 2a 2f 0a 29 7b 0a 20 20   modify */.){.  
23312 69 6e 74 20 69 3b 0a 20 20 63 6f 6e 73 74 20 63  int i;.  const c
23313 68 61 72 20 2a 7a 44 62 3b 0a 20 20 73 74 72 75  har *zDb;.  stru
23314 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
23315 2a 70 49 74 65 6d 3b 0a 0a 20 20 69 66 28 20 4e  *pItem;..  if( N
23316 45 56 45 52 28 70 4c 69 73 74 3d 3d 30 29 20 29  EVER(pList==0) )
23317 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7a 44 62   return 0;.  zDb
23318 20 3d 20 70 46 69 78 2d 3e 7a 44 62 3b 0a 20 20   = pFix->zDb;.  
23319 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70  for(i=0, pItem=p
2331a 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74  List->a; i<pList
2331b 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74  ->nSrc; i++, pIt
2331c 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70  em++){.    if( p
2331d 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 3d  Item->zDatabase=
2331e 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 49 74 65  =0 ){.      pIte
2331f 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 73  m->zDatabase = s
23320 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 70  qlite3DbStrDup(p
23321 46 69 78 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2c  Fix->pParse->db,
23322 20 7a 44 62 29 3b 0a 20 20 20 20 7d 65 6c 73 65   zDb);.    }else
23323 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
23324 43 6d 70 28 70 49 74 65 6d 2d 3e 7a 44 61 74 61  Cmp(pItem->zData
23325 62 61 73 65 2c 7a 44 62 29 21 3d 30 20 29 7b 0a  base,zDb)!=0 ){.
23326 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
23327 6f 72 4d 73 67 28 70 46 69 78 2d 3e 70 50 61 72  orMsg(pFix->pPar
23328 73 65 2c 0a 20 20 20 20 20 20 20 20 20 22 25 73  se,.         "%s
23329 20 25 54 20 63 61 6e 6e 6f 74 20 72 65 66 65 72   %T cannot refer
2332a 65 6e 63 65 20 6f 62 6a 65 63 74 73 20 69 6e 20  ence objects in 
2332b 64 61 74 61 62 61 73 65 20 25 73 22 2c 0a 20 20  database %s",.  
2332c 20 20 20 20 20 20 20 70 46 69 78 2d 3e 7a 54 79         pFix->zTy
2332d 70 65 2c 20 70 46 69 78 2d 3e 70 4e 61 6d 65 2c  pe, pFix->pName,
2332e 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73   pItem->zDatabas
2332f 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  e);.      return
23330 20 31 3b 0a 20 20 20 20 7d 0a 23 69 66 20 21 64   1;.    }.#if !d
23331 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
23332 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66  IT_VIEW) || !def
23333 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
23334 5f 54 52 49 47 47 45 52 29 0a 20 20 20 20 69 66  _TRIGGER).    if
23335 28 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65  ( sqlite3FixSele
23336 63 74 28 70 46 69 78 2c 20 70 49 74 65 6d 2d 3e  ct(pFix, pItem->
23337 70 53 65 6c 65 63 74 29 20 29 20 72 65 74 75 72  pSelect) ) retur
23338 6e 20 31 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  n 1;.    if( sql
23339 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78  ite3FixExpr(pFix
2333a 2c 20 70 49 74 65 6d 2d 3e 70 4f 6e 29 20 29 20  , pItem->pOn) ) 
2333b 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66  return 1;.#endif
2333c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
2333d 0a 7d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  .}.#if !defined(
2333e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
2333f 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  ) || !defined(SQ
23340 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
23341 52 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  R).SQLITE_PRIVAT
23342 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78  E int sqlite3Fix
23343 53 65 6c 65 63 74 28 0a 20 20 44 62 46 69 78 65  Select(.  DbFixe
23344 72 20 2a 70 46 69 78 2c 20 20 20 20 20 20 20 2f  r *pFix,       /
23345 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65  * Context of the
23346 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 53   fixation */.  S
23347 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 20  elect *pSelect  
23348 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43      /* The SELEC
23349 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62  T statement to b
2334a 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 20 64  e fixed to one d
2334b 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20  atabase */.){.  
2334c 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74 20 29  while( pSelect )
2334d 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
2334e 33 46 69 78 45 78 70 72 4c 69 73 74 28 70 46 69  3FixExprList(pFi
2334f 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69  x, pSelect->pELi
23350 73 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  st) ){.      ret
23351 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
23352 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 53   if( sqlite3FixS
23353 72 63 4c 69 73 74 28 70 46 69 78 2c 20 70 53 65  rcList(pFix, pSe
23354 6c 65 63 74 2d 3e 70 53 72 63 29 20 29 7b 0a 20  lect->pSrc) ){. 
23355 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
23356 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
23357 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78  ite3FixExpr(pFix
23358 2c 20 70 53 65 6c 65 63 74 2d 3e 70 57 68 65 72  , pSelect->pWher
23359 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  e) ){.      retu
2335a 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
2335b 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78  if( sqlite3FixEx
2335c 70 72 28 70 46 69 78 2c 20 70 53 65 6c 65 63 74  pr(pFix, pSelect
2335d 2d 3e 70 48 61 76 69 6e 67 29 20 29 7b 0a 20 20  ->pHaving) ){.  
2335e 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2335f 20 20 7d 0a 20 20 20 20 70 53 65 6c 65 63 74 20    }.    pSelect 
23360 3d 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f  = pSelect->pPrio
23361 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  r;.  }.  return 
23362 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  0;.}.SQLITE_PRIV
23363 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
23364 69 78 45 78 70 72 28 0a 20 20 44 62 46 69 78 65  ixExpr(.  DbFixe
23365 72 20 2a 70 46 69 78 2c 20 20 20 20 20 2f 2a 20  r *pFix,     /* 
23366 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66  Context of the f
23367 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70  ixation */.  Exp
23368 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 20 20  r *pExpr        
23369 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f  /* The expressio
2336a 6e 20 74 6f 20 62 65 20 66 69 78 65 64 20 74 6f  n to be fixed to
2336b 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 2a 2f   one database */
2336c 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 45 78  .){.  while( pEx
2336d 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20 45 78  pr ){.    if( Ex
2336e 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79  prHasAnyProperty
2336f 28 70 45 78 70 72 2c 20 45 50 5f 54 6f 6b 65 6e  (pExpr, EP_Token
23370 4f 6e 6c 79 29 20 29 20 62 72 65 61 6b 3b 0a 20  Only) ) break;. 
23371 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72     if( ExprHasPr
23372 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
23373 5f 78 49 73 53 65 6c 65 63 74 29 20 29 7b 0a 20  _xIsSelect) ){. 
23374 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
23375 46 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c 20  FixSelect(pFix, 
23376 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74  pExpr->x.pSelect
23377 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  ) ) return 1;.  
23378 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
23379 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78 70  f( sqlite3FixExp
2337a 72 4c 69 73 74 28 70 46 69 78 2c 20 70 45 78 70  rList(pFix, pExp
2337b 72 2d 3e 78 2e 70 4c 69 73 74 29 20 29 20 72 65  r->x.pList) ) re
2337c 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
2337d 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
2337e 45 78 70 72 28 70 46 69 78 2c 20 70 45 78 70 72  Expr(pFix, pExpr
2337f 2d 3e 70 52 69 67 68 74 29 20 29 7b 0a 20 20 20  ->pRight) ){.   
23380 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
23381 20 7d 0a 20 20 20 20 70 45 78 70 72 20 3d 20 70   }.    pExpr = p
23382 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 7d  Expr->pLeft;.  }
23383 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53  .  return 0;.}.S
23384 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
23385 74 20 73 71 6c 69 74 65 33 46 69 78 45 78 70 72  t sqlite3FixExpr
23386 4c 69 73 74 28 0a 20 20 44 62 46 69 78 65 72 20  List(.  DbFixer 
23387 2a 70 46 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f  *pFix,     /* Co
23388 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66 69 78  ntext of the fix
23389 61 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 4c  ation */.  ExprL
2338a 69 73 74 20 2a 70 4c 69 73 74 20 20 20 20 2f 2a  ist *pList    /*
2338b 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
2338c 74 6f 20 62 65 20 66 69 78 65 64 20 74 6f 20 6f  to be fixed to o
2338d 6e 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 29  ne database */.)
2338e 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  {.  int i;.  str
2338f 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
23390 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20  m *pItem;.  if( 
23391 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72  pList==0 ) retur
23392 6e 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20  n 0;.  for(i=0, 
23393 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20  pItem=pList->a; 
23394 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  i<pList->nExpr; 
23395 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
23396 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
23397 78 45 78 70 72 28 70 46 69 78 2c 20 70 49 74 65  xExpr(pFix, pIte
23398 6d 2d 3e 70 45 78 70 72 29 20 29 7b 0a 20 20 20  m->pExpr) ){.   
23399 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
2339a 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
2339b 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  0;.}.#endif..#if
2339c 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2339d 5f 54 52 49 47 47 45 52 0a 53 51 4c 49 54 45 5f  _TRIGGER.SQLITE_
2339e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2339f 74 65 33 46 69 78 54 72 69 67 67 65 72 53 74 65  te3FixTriggerSte
233a0 70 28 0a 20 20 44 62 46 69 78 65 72 20 2a 70 46  p(.  DbFixer *pF
233a1 69 78 2c 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65  ix,     /* Conte
233a2 78 74 20 6f 66 20 74 68 65 20 66 69 78 61 74 69  xt of the fixati
233a3 6f 6e 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53  on */.  TriggerS
233a4 74 65 70 20 2a 70 53 74 65 70 20 2f 2a 20 54 68  tep *pStep /* Th
233a5 65 20 74 72 69 67 67 65 72 20 73 74 65 70 20 62  e trigger step b
233a6 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 20 64  e fixed to one d
233a7 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20  atabase */.){.  
233a8 77 68 69 6c 65 28 20 70 53 74 65 70 20 29 7b 0a  while( pStep ){.
233a9 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
233aa 69 78 53 65 6c 65 63 74 28 70 46 69 78 2c 20 70  ixSelect(pFix, p
233ab 53 74 65 70 2d 3e 70 53 65 6c 65 63 74 29 20 29  Step->pSelect) )
233ac 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
233ad 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
233ae 73 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70  sqlite3FixExpr(p
233af 46 69 78 2c 20 70 53 74 65 70 2d 3e 70 57 68 65  Fix, pStep->pWhe
233b0 72 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  re) ){.      ret
233b1 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
233b2 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45   if( sqlite3FixE
233b3 78 70 72 4c 69 73 74 28 70 46 69 78 2c 20 70 53  xprList(pFix, pS
233b4 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 29 20  tep->pExprList) 
233b5 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
233b6 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 74  1;.    }.    pSt
233b7 65 70 20 3d 20 70 53 74 65 70 2d 3e 70 4e 65 78  ep = pStep->pNex
233b8 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  t;.  }.  return 
233b9 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  0;.}.#endif../**
233ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
233bb 20 6f 66 20 61 74 74 61 63 68 2e 63 20 2a 2a 2a   of attach.c ***
233bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
233bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
233be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
233bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
233c0 69 6e 20 66 69 6c 65 20 61 75 74 68 2e 63 20 2a  in file auth.c *
233c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
233c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
233c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
233c4 2a 2a 20 32 30 30 33 20 4a 61 6e 75 61 72 79 20  ** 2003 January 
233c5 31 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  11.**.** The aut
233c6 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
233c7 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
233c8 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
233c9 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
233ca 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
233cb 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
233cc 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
233cd 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
233ce 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
233cf 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
233d0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
233d1 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
233d2 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
233d3 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
233d4 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
233d5 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
233d6 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
233d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
233d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
233d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
233da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
233db 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
233dc 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65  ontains code use
233dd 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  d to implement t
233de 68 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  he sqlite3_set_a
233df 75 74 68 6f 72 69 7a 65 72 28 29 0a 2a 2a 20 41  uthorizer().** A
233e0 50 49 2e 20 20 54 68 69 73 20 66 61 63 69 6c 69  PI.  This facili
233e1 74 79 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e 61  ty is an optiona
233e2 6c 20 66 65 61 74 75 72 65 20 6f 66 20 74 68 65  l feature of the
233e3 20 6c 69 62 72 61 72 79 2e 20 20 45 6d 62 65 64   library.  Embed
233e4 64 65 64 0a 2a 2a 20 73 79 73 74 65 6d 73 20 74  ded.** systems t
233e5 68 61 74 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20  hat do not need 
233e6 74 68 69 73 20 66 61 63 69 6c 69 74 79 20 6d 61  this facility ma
233e7 79 20 6f 6d 69 74 20 69 74 20 62 79 20 72 65 63  y omit it by rec
233e8 6f 6d 70 69 6c 69 6e 67 0a 2a 2a 20 74 68 65 20  ompiling.** the 
233e9 6c 69 62 72 61 72 79 20 77 69 74 68 20 2d 44 53  library with -DS
233ea 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
233eb 52 49 5a 41 54 49 4f 4e 3d 31 0a 2a 2a 0a 2a 2a  RIZATION=1.**.**
233ec 20 24 49 64 3a 20 61 75 74 68 2e 63 2c 76 20 31   $Id: auth.c,v 1
233ed 2e 33 32 20 32 30 30 39 2f 30 37 2f 30 32 20 31  .32 2009/07/02 1
233ee 38 3a 34 30 3a 33 35 20 64 61 6e 69 65 6c 6b 31  8:40:35 danielk1
233ef 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  977 Exp $.*/../*
233f0 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 63  .** All of the c
233f1 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ode in this file
233f2 20 6d 61 79 20 62 65 20 6f 6d 69 74 74 65 64 20   may be omitted 
233f3 62 79 20 64 65 66 69 6e 69 6e 67 20 61 20 73 69  by defining a si
233f4 6e 67 6c 65 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a  ngle.** macro..*
233f5 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
233f6 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
233f7 49 4f 4e 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f  ION../*.** Set o
233f8 72 20 63 6c 65 61 72 20 74 68 65 20 61 63 63 65  r clear the acce
233f9 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  ss authorization
233fa 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
233fb 20 54 68 65 20 61 63 63 65 73 73 20 61 75 74 68   The access auth
233fc 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69  orization functi
233fd 6f 6e 20 69 73 20 62 65 20 63 61 6c 6c 65 64 20  on is be called 
233fe 64 75 72 69 6e 67 20 74 68 65 20 63 6f 6d 70 69  during the compi
233ff 6c 61 74 69 6f 6e 0a 2a 2a 20 70 68 61 73 65 20  lation.** phase 
23400 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 74  to verify that t
23401 68 65 20 75 73 65 72 20 68 61 73 20 72 65 61 64  he user has read
23402 20 61 6e 64 2f 6f 72 20 77 72 69 74 65 20 61 63   and/or write ac
23403 63 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 20  cess permission 
23404 6f 6e 0a 2a 2a 20 76 61 72 69 6f 75 73 20 66 69  on.** various fi
23405 65 6c 64 73 20 6f 66 20 74 68 65 20 64 61 74 61  elds of the data
23406 62 61 73 65 2e 20 20 54 68 65 20 66 69 72 73 74  base.  The first
23407 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
23408 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 0a 2a   auth function.*
23409 2a 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74  * is a copy of t
2340a 68 65 20 33 72 64 20 61 72 67 75 6d 65 6e 74 20  he 3rd argument 
2340b 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  to this routine.
2340c 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67    The second arg
2340d 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20  ument.** to the 
2340e 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 69 73  auth function is
2340f 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 63 6f   one of these co
23410 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20  nstants:.**.**  
23411 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41       SQLITE_CREA
23412 54 45 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20  TE_INDEX.**     
23413 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f    SQLITE_CREATE_
23414 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53  TABLE.**       S
23415 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d  QLITE_CREATE_TEM
23416 50 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20  P_INDEX.**      
23417 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54   SQLITE_CREATE_T
23418 45 4d 50 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20  EMP_TABLE.**    
23419 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45     SQLITE_CREATE
2341a 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 0a 2a 2a  _TEMP_TRIGGER.**
2341b 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52         SQLITE_CR
2341c 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 0a 2a  EATE_TEMP_VIEW.*
2341d 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43  *       SQLITE_C
2341e 52 45 41 54 45 5f 54 52 49 47 47 45 52 0a 2a 2a  REATE_TRIGGER.**
2341f 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52         SQLITE_CR
23420 45 41 54 45 5f 56 49 45 57 0a 2a 2a 20 20 20 20  EATE_VIEW.**    
23421 20 20 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45     SQLITE_DELETE
23422 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
23423 5f 44 52 4f 50 5f 49 4e 44 45 58 0a 2a 2a 20 20  _DROP_INDEX.**  
23424 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50       SQLITE_DROP
23425 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20  _TABLE.**       
23426 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
23427 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 20  _INDEX.**       
23428 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
23429 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20  _TABLE.**       
2342a 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
2342b 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 20 20 20  _TRIGGER.**     
2342c 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45    SQLITE_DROP_TE
2342d 4d 50 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20  MP_VIEW.**      
2342e 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49   SQLITE_DROP_TRI
2342f 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51  GGER.**       SQ
23430 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 0a 2a  LITE_DROP_VIEW.*
23431 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49  *       SQLITE_I
23432 4e 53 45 52 54 0a 2a 2a 20 20 20 20 20 20 20 53  NSERT.**       S
23433 51 4c 49 54 45 5f 50 52 41 47 4d 41 0a 2a 2a 20  QLITE_PRAGMA.** 
23434 20 20 20 20 20 20 53 51 4c 49 54 45 5f 52 45 41        SQLITE_REA
23435 44 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  D.**       SQLIT
23436 45 5f 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20  E_SELECT.**     
23437 20 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43    SQLITE_TRANSAC
23438 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 20 53 51  TION.**       SQ
23439 4c 49 54 45 5f 55 50 44 41 54 45 0a 2a 2a 0a 2a  LITE_UPDATE.**.*
2343a 2a 20 54 68 65 20 74 68 69 72 64 20 61 6e 64 20  * The third and 
2343b 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 73  fourth arguments
2343c 20 74 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e   to the auth fun
2343d 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 6e 61  ction are the na
2343e 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62  me of.** the tab
2343f 6c 65 20 61 6e 64 20 74 68 65 20 63 6f 6c 75 6d  le and the colum
23440 6e 20 74 68 61 74 20 61 72 65 20 62 65 69 6e 67  n that are being
23441 20 61 63 63 65 73 73 65 64 2e 20 20 54 68 65 20   accessed.  The 
23442 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  auth function.**
23443 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 65   should return e
23444 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 2c  ither SQLITE_OK,
23445 20 53 51 4c 49 54 45 5f 44 45 4e 59 2c 20 6f 72   SQLITE_DENY, or
23446 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 2e 20   SQLITE_IGNORE. 
23447 20 49 66 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b   If.** SQLITE_OK
23448 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 69 74   is returned, it
23449 20 6d 65 61 6e 73 20 74 68 61 74 20 61 63 63 65   means that acce
2344a 73 73 20 69 73 20 61 6c 6c 6f 77 65 64 2e 20 20  ss is allowed.  
2344b 53 51 4c 49 54 45 5f 44 45 4e 59 0a 2a 2a 20 6d  SQLITE_DENY.** m
2344c 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 53 51  eans that the SQ
2344d 4c 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c 6c  L statement will
2344e 20 6e 65 76 65 72 2d 72 75 6e 20 2d 20 74 68 65   never-run - the
2344f 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
23450 63 61 6c 6c 0a 2a 2a 20 77 69 6c 6c 20 72 65 74  call.** will ret
23451 75 72 6e 20 77 69 74 68 20 61 6e 20 65 72 72 6f  urn with an erro
23452 72 2e 20 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52  r.  SQLITE_IGNOR
23453 45 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  E means that the
23454 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 2a   SQL statement.*
23455 2a 20 73 68 6f 75 6c 64 20 72 75 6e 20 62 75 74  * should run but
23456 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 61   attempts to rea
23457 64 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  d the specified 
23458 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 72 65 74 75  column will retu
23459 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 61 6e 64 20 61  rn NULL.** and a
2345a 74 74 65 6d 70 74 73 20 74 6f 20 77 72 69 74 65  ttempts to write
2345b 20 74 68 65 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c   the column will
2345c 20 62 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a   be ignored..**.
2345d 2a 2a 20 53 65 74 74 69 6e 67 20 74 68 65 20 61  ** Setting the a
2345e 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  uth function to 
2345f 4e 55 4c 4c 20 64 69 73 61 62 6c 65 73 20 74 68  NULL disables th
23460 69 73 20 68 6f 6f 6b 2e 20 20 54 68 65 20 64 65  is hook.  The de
23461 66 61 75 6c 74 0a 2a 2a 20 73 65 74 74 69 6e 67  fault.** setting
23462 20 6f 66 20 74 68 65 20 61 75 74 68 20 66 75 6e   of the auth fun
23463 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a 2a  ction is NULL..*
23464 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
23465 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74   sqlite3_set_aut
23466 68 6f 72 69 7a 65 72 28 0a 20 20 73 71 6c 69 74  horizer(.  sqlit
23467 65 33 20 2a 64 62 2c 0a 20 20 69 6e 74 20 28 2a  e3 *db,.  int (*
23468 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74  xAuth)(void*,int
23469 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
2346a 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
2346b 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
2346c 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a  ),.  void *pArg.
2346d 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ){.  sqlite3_mut
2346e 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
2346f 65 78 29 3b 0a 20 20 64 62 2d 3e 78 41 75 74 68  ex);.  db->xAuth
23470 20 3d 20 78 41 75 74 68 3b 0a 20 20 64 62 2d 3e   = xAuth;.  db->
23471 70 41 75 74 68 41 72 67 20 3d 20 70 41 72 67 3b  pAuthArg = pArg;
23472 0a 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65  .  sqlite3Expire
23473 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
23474 74 73 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65  ts(db);.  sqlite
23475 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
23476 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
23477 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
23478 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20  ./*.** Write an 
23479 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e  error message in
2347a 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  to pParse->zErrM
2347b 73 67 20 74 68 61 74 20 65 78 70 6c 61 69 6e 73  sg that explains
2347c 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 75 73 65   that the.** use
2347d 72 2d 73 75 70 70 6c 69 65 64 20 61 75 74 68 6f  r-supplied autho
2347e 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  rization functio
2347f 6e 20 72 65 74 75 72 6e 65 64 20 61 6e 20 69 6c  n returned an il
23480 6c 65 67 61 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a  legal value..*/.
23481 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
23482 74 65 41 75 74 68 42 61 64 52 65 74 75 72 6e 43  teAuthBadReturnC
23483 6f 64 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ode(Parse *pPars
23484 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 45 72 72  e){.  sqlite3Err
23485 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61  orMsg(pParse, "a
23486 75 74 68 6f 72 69 7a 65 72 20 6d 61 6c 66 75 6e  uthorizer malfun
23487 63 74 69 6f 6e 22 29 3b 0a 20 20 70 50 61 72 73  ction");.  pPars
23488 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45  e->rc = SQLITE_E
23489 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  RROR;.}../*.** I
2348a 6e 76 6f 6b 65 20 74 68 65 20 61 75 74 68 6f 72  nvoke the author
2348b 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b  ization callback
2348c 20 66 6f 72 20 70 65 72 6d 69 73 73 69 6f 6e 20   for permission 
2348d 74 6f 20 72 65 61 64 20 63 6f 6c 75 6d 6e 20 7a  to read column z
2348e 43 6f 6c 20 66 72 6f 6d 0a 2a 2a 20 74 61 62 6c  Col from.** tabl
2348f 65 20 7a 54 61 62 20 69 6e 20 64 61 74 61 62 61  e zTab in databa
23490 73 65 20 7a 44 62 2e 20 54 68 69 73 20 66 75 6e  se zDb. This fun
23491 63 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68  ction assumes th
23492 61 74 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74  at an authorizat
23493 69 6f 6e 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20  ion.** callback 
23494 68 61 73 20 62 65 65 6e 20 72 65 67 69 73 74 65  has been registe
23495 72 65 64 20 28 69 2e 65 2e 20 74 68 61 74 20 73  red (i.e. that s
23496 71 6c 69 74 65 33 2e 78 41 75 74 68 20 69 73 20  qlite3.xAuth is 
23497 6e 6f 74 20 4e 55 4c 4c 29 2e 0a 2a 2a 0a 2a 2a  not NULL)..**.**
23498 20 49 66 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52   If SQLITE_IGNOR
23499 45 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  E is returned an
2349a 64 20 70 45 78 70 72 20 69 73 20 6e 6f 74 20 4e  d pExpr is not N
2349b 55 4c 4c 2c 20 74 68 65 6e 20 70 45 78 70 72 20  ULL, then pExpr 
2349c 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 74 6f  is changed.** to
2349d 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 65 78 70   an SQL NULL exp
2349e 72 65 73 73 69 6f 6e 2e 20 4f 74 68 65 72 77 69  ression. Otherwi
2349f 73 65 2c 20 69 66 20 70 45 78 70 72 20 69 73 20  se, if pExpr is 
234a0 4e 55 4c 4c 2c 20 74 68 65 6e 20 53 51 4c 49 54  NULL, then SQLIT
234a1 45 5f 49 47 4e 4f 52 45 0a 2a 2a 20 69 73 20 74  E_IGNORE.** is t
234a2 72 65 61 74 65 64 20 61 73 20 53 51 4c 49 54 45  reated as SQLITE
234a3 5f 44 45 4e 59 2e 20 49 6e 20 74 68 69 73 20 63  _DENY. In this c
234a4 61 73 65 20 61 6e 20 65 72 72 6f 72 20 69 73 20  ase an error is 
234a5 6c 65 66 74 20 69 6e 20 70 50 61 72 73 65 2e 0a  left in pParse..
234a6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
234a7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74  E int sqlite3Aut
234a8 68 52 65 61 64 43 6f 6c 28 0a 20 20 50 61 72 73  hReadCol(.  Pars
234a9 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
234aa 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
234ab 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78  he parser contex
234ac 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  t */.  const cha
234ad 72 20 2a 7a 54 61 62 2c 20 20 20 20 20 20 20 20  r *zTab,        
234ae 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
234af 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  name */.  const 
234b0 63 68 61 72 20 2a 7a 43 6f 6c 2c 20 20 20 20 20  char *zCol,     
234b1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
234b2 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e  umn name */.  in
234b3 74 20 69 44 62 20 20 20 20 20 20 20 20 20 20 20  t iDb           
234b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
234b5 20 49 6e 64 65 78 20 6f 66 20 63 6f 6e 74 61 69   Index of contai
234b6 6e 69 6e 67 20 64 61 74 61 62 61 73 65 2e 20 2a  ning database. *
234b7 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  /.){.  sqlite3 *
234b8 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
234b9 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
234ba 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63  se handle */.  c
234bb 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61  har *zDb = db->a
234bc 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 20 2f  Db[iDb].zName; /
234bd 2a 20 4e 61 6d 65 20 6f 66 20 61 74 74 61 63 68  * Name of attach
234be 65 64 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  ed database */. 
234bf 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
234c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
234c1 20 2f 2a 20 41 75 74 68 20 63 61 6c 6c 62 61 63   /* Auth callbac
234c2 6b 20 72 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  k return code */
234c3 0a 0a 20 20 72 63 20 3d 20 64 62 2d 3e 78 41 75  ..  rc = db->xAu
234c4 74 68 28 64 62 2d 3e 70 41 75 74 68 41 72 67 2c  th(db->pAuthArg,
234c5 20 53 51 4c 49 54 45 5f 52 45 41 44 2c 20 7a 54   SQLITE_READ, zT
234c6 61 62 2c 7a 43 6f 6c 2c 7a 44 62 2c 70 50 61 72  ab,zCol,zDb,pPar
234c7 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74  se->zAuthContext
234c8 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
234c9 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20  ITE_DENY ){.    
234ca 69 66 28 20 64 62 2d 3e 6e 44 62 3e 32 20 7c 7c  if( db->nDb>2 ||
234cb 20 69 44 62 21 3d 30 20 29 7b 0a 20 20 20 20 20   iDb!=0 ){.     
234cc 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
234cd 28 70 50 61 72 73 65 2c 20 22 61 63 63 65 73 73  (pParse, "access
234ce 20 74 6f 20 25 73 2e 25 73 2e 25 73 20 69 73 20   to %s.%s.%s is 
234cf 70 72 6f 68 69 62 69 74 65 64 22 2c 7a 44 62 2c  prohibited",zDb,
234d0 7a 54 61 62 2c 7a 43 6f 6c 29 3b 0a 20 20 20 20  zTab,zCol);.    
234d1 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
234d2 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
234d3 72 73 65 2c 20 22 61 63 63 65 73 73 20 74 6f 20  rse, "access to 
234d4 25 73 2e 25 73 20 69 73 20 70 72 6f 68 69 62 69  %s.%s is prohibi
234d5 74 65 64 22 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c  ted", zTab, zCol
234d6 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61  );.    }.    pPa
234d7 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45  rse->rc = SQLITE
234d8 5f 41 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20 69  _AUTH;.  }else i
234d9 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 49 47  f( rc!=SQLITE_IG
234da 4e 4f 52 45 20 26 26 20 72 63 21 3d 53 51 4c 49  NORE && rc!=SQLI
234db 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c  TE_OK ){.    sql
234dc 69 74 65 41 75 74 68 42 61 64 52 65 74 75 72 6e  iteAuthBadReturn
234dd 43 6f 64 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Code(pParse);.  
234de 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
234df 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 45 78 70  ../*.** The pExp
234e0 72 20 73 68 6f 75 6c 64 20 62 65 20 61 20 54 4b  r should be a TK
234e1 5f 43 4f 4c 55 4d 4e 20 65 78 70 72 65 73 73 69  _COLUMN expressi
234e2 6f 6e 2e 20 20 54 68 65 20 74 61 62 6c 65 20 72  on.  The table r
234e3 65 66 65 72 72 65 64 20 74 6f 0a 2a 2a 20 69 73  eferred to.** is
234e4 20 69 6e 20 70 54 61 62 4c 69 73 74 20 6f 72 20   in pTabList or 
234e5 65 6c 73 65 20 69 74 20 69 73 20 74 68 65 20 4e  else it is the N
234e6 45 57 20 6f 72 20 4f 4c 44 20 74 61 62 6c 65 20  EW or OLD table 
234e7 6f 66 20 61 20 74 72 69 67 67 65 72 2e 20 20 0a  of a trigger.  .
234e8 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  ** Check to see 
234e9 69 66 20 69 74 20 69 73 20 4f 4b 20 74 6f 20 72  if it is OK to r
234ea 65 61 64 20 74 68 69 73 20 70 61 72 74 69 63 75  ead this particu
234eb 6c 61 72 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a  lar column..**.*
234ec 2a 20 49 66 20 74 68 65 20 61 75 74 68 20 66 75  * If the auth fu
234ed 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 53  nction returns S
234ee 51 4c 49 54 45 5f 49 47 4e 4f 52 45 2c 20 63 68  QLITE_IGNORE, ch
234ef 61 6e 67 65 20 74 68 65 20 54 4b 5f 43 4f 4c 55  ange the TK_COLU
234f0 4d 4e 20 0a 2a 2a 20 69 6e 73 74 72 75 63 74 69  MN .** instructi
234f1 6f 6e 20 69 6e 74 6f 20 61 20 54 4b 5f 4e 55 4c  on into a TK_NUL
234f2 4c 2e 20 20 49 66 20 74 68 65 20 61 75 74 68 20  L.  If the auth 
234f3 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
234f4 20 53 51 4c 49 54 45 5f 44 45 4e 59 2c 0a 2a 2a   SQLITE_DENY,.**
234f5 20 74 68 65 6e 20 67 65 6e 65 72 61 74 65 20 61   then generate a
234f6 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49  n error..*/.SQLI
234f7 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
234f8 73 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 28  sqlite3AuthRead(
234f9 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
234fa 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ,        /* The 
234fb 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  parser context *
234fc 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c  /.  Expr *pExpr,
234fd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
234fe 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 63   expression to c
234ff 68 65 63 6b 20 61 75 74 68 6f 72 69 7a 61 74 69  heck authorizati
23500 6f 6e 20 6f 6e 20 2a 2f 0a 20 20 53 63 68 65 6d  on on */.  Schem
23501 61 20 2a 70 53 63 68 65 6d 61 2c 20 20 20 20 20  a *pSchema,     
23502 20 2f 2a 20 54 68 65 20 73 63 68 65 6d 61 20 6f   /* The schema o
23503 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
23504 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
23505 54 61 62 4c 69 73 74 20 20 20 20 20 2f 2a 20 41  TabList     /* A
23506 6c 6c 20 74 61 62 6c 65 20 74 68 61 74 20 70 45  ll table that pE
23507 78 70 72 20 6d 69 67 68 74 20 72 65 66 65 72 20  xpr might refer 
23508 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  to */.){.  sqlit
23509 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2350a 3e 64 62 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54  >db;.  Table *pT
2350b 61 62 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20  ab = 0;      /* 
2350c 54 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20  The table being 
2350d 72 65 61 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  read */.  const 
2350e 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20  char *zCol;     
2350f 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63  /* Name of the c
23510 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62  olumn of the tab
23511 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53 72 63  le */.  int iSrc
23512 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
23513 20 49 6e 64 65 78 20 69 6e 20 70 54 61 62 4c 69   Index in pTabLi
23514 73 74 2d 3e 61 5b 5d 20 6f 66 20 74 61 62 6c 65  st->a[] of table
23515 20 62 65 69 6e 67 20 72 65 61 64 20 2a 2f 0a 20   being read */. 
23516 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
23517 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e         /* The in
23518 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62  dex of the datab
23519 61 73 65 20 74 68 65 20 65 78 70 72 65 73 73 69  ase the expressi
2351a 6f 6e 20 72 65 66 65 72 73 20 74 6f 20 2a 2f 0a  on refers to */.
2351b 20 20 69 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20    int iCol;     
2351c 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
2351d 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 61   of column in ta
2351e 62 6c 65 20 2a 2f 0a 0a 20 20 69 66 28 20 64 62  ble */..  if( db
2351f 2d 3e 78 41 75 74 68 3d 3d 30 20 29 20 72 65 74  ->xAuth==0 ) ret
23520 75 72 6e 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c  urn;.  iDb = sql
23521 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
23522 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53  x(pParse->db, pS
23523 63 68 65 6d 61 29 3b 0a 20 20 69 66 28 20 69 44  chema);.  if( iD
23524 62 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6e  b<0 ){.    /* An
23525 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64   attempt to read
23526 20 61 20 63 6f 6c 75 6d 6e 20 6f 75 74 20 6f 66   a column out of
23527 20 61 20 73 75 62 71 75 65 72 79 20 6f 72 20 6f   a subquery or o
23528 74 68 65 72 0a 20 20 20 20 2a 2a 20 74 65 6d 70  ther.    ** temp
23529 6f 72 61 72 79 20 74 61 62 6c 65 2e 20 2a 2f 0a  orary table. */.
2352a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
2352b 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72  .  assert( pExpr
2352c 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20  ->op==TK_COLUMN 
2352d 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  || pExpr->op==TK
2352e 5f 54 52 49 47 47 45 52 20 29 3b 0a 20 20 69 66  _TRIGGER );.  if
2352f 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
23530 54 52 49 47 47 45 52 20 29 7b 0a 20 20 20 20 70  TRIGGER ){.    p
23531 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 54  Tab = pParse->pT
23532 72 69 67 67 65 72 54 61 62 3b 0a 20 20 7d 65 6c  riggerTab;.  }el
23533 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
23534 70 54 61 62 4c 69 73 74 20 29 3b 0a 20 20 20 20  pTabList );.    
23535 66 6f 72 28 69 53 72 63 3d 30 3b 20 41 4c 57 41  for(iSrc=0; ALWA
23536 59 53 28 69 53 72 63 3c 70 54 61 62 4c 69 73 74  YS(iSrc<pTabList
23537 2d 3e 6e 53 72 63 29 3b 20 69 53 72 63 2b 2b 29  ->nSrc); iSrc++)
23538 7b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78 70  {.      if( pExp
23539 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 54 61 62 4c  r->iTable==pTabL
2353a 69 73 74 2d 3e 61 5b 69 53 72 63 5d 2e 69 43 75  ist->a[iSrc].iCu
2353b 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20 20 20  rsor ){.        
2353c 70 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74 2d  pTab = pTabList-
2353d 3e 61 5b 69 53 72 63 5d 2e 70 54 61 62 3b 0a 20  >a[iSrc].pTab;. 
2353e 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2353f 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
23540 20 20 69 43 6f 6c 20 3d 20 70 45 78 70 72 2d 3e    iCol = pExpr->
23541 69 43 6f 6c 75 6d 6e 3b 0a 20 20 69 66 28 20 4e  iColumn;.  if( N
23542 45 56 45 52 28 70 54 61 62 3d 3d 30 29 20 29 20  EVER(pTab==0) ) 
23543 72 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20 69  return;..  if( i
23544 43 6f 6c 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73  Col>=0 ){.    as
23545 73 65 72 74 28 20 69 43 6f 6c 3c 70 54 61 62 2d  sert( iCol<pTab-
23546 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 7a 43 6f  >nCol );.    zCo
23547 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69  l = pTab->aCol[i
23548 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65  Col].zName;.  }e
23549 6c 73 65 20 69 66 28 20 70 54 61 62 2d 3e 69 50  lse if( pTab->iP
2354a 4b 65 79 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73  Key>=0 ){.    as
2354b 73 65 72 74 28 20 70 54 61 62 2d 3e 69 50 4b 65  sert( pTab->iPKe
2354c 79 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a  y<pTab->nCol );.
2354d 20 20 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d      zCol = pTab-
2354e 3e 61 43 6f 6c 5b 70 54 61 62 2d 3e 69 50 4b 65  >aCol[pTab->iPKe
2354f 79 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73  y].zName;.  }els
23550 65 7b 0a 20 20 20 20 7a 43 6f 6c 20 3d 20 22 52  e{.    zCol = "R
23551 4f 57 49 44 22 3b 0a 20 20 7d 0a 20 20 61 73 73  OWID";.  }.  ass
23552 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69  ert( iDb>=0 && i
23553 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  Db<db->nDb );.  
23554 69 66 28 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52  if( SQLITE_IGNOR
23555 45 3d 3d 73 71 6c 69 74 65 33 41 75 74 68 52 65  E==sqlite3AuthRe
23556 61 64 43 6f 6c 28 70 50 61 72 73 65 2c 20 70 54  adCol(pParse, pT
23557 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 2c  ab->zName, zCol,
23558 20 69 44 62 29 20 29 7b 0a 20 20 20 20 70 45 78   iDb) ){.    pEx
23559 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c  pr->op = TK_NULL
2355a 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  ;.  }.}../*.** D
2355b 6f 20 61 6e 20 61 75 74 68 6f 72 69 7a 61 74 69  o an authorizati
2355c 6f 6e 20 63 68 65 63 6b 20 75 73 69 6e 67 20 74  on check using t
2355d 68 65 20 63 6f 64 65 20 61 6e 64 20 61 72 67 75  he code and argu
2355e 6d 65 6e 74 73 20 67 69 76 65 6e 2e 20 20 52 65  ments given.  Re
2355f 74 75 72 6e 0a 2a 2a 20 65 69 74 68 65 72 20 53  turn.** either S
23560 51 4c 49 54 45 5f 4f 4b 20 28 7a 65 72 6f 29 20  QLITE_OK (zero) 
23561 6f 72 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  or SQLITE_IGNORE
23562 20 6f 72 20 53 51 4c 49 54 45 5f 44 45 4e 59 2e   or SQLITE_DENY.
23563 20 20 49 66 20 53 51 4c 49 54 45 5f 44 45 4e 59    If SQLITE_DENY
23564 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2c  .** is returned,
23565 20 74 68 65 6e 20 74 68 65 20 65 72 72 6f 72 20   then the error 
23566 63 6f 75 6e 74 20 61 6e 64 20 65 72 72 6f 72 20  count and error 
23567 6d 65 73 73 61 67 65 20 69 6e 20 70 50 61 72 73  message in pPars
23568 65 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65  e are.** modifie
23569 64 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e  d appropriately.
2356a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2356b 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 75  TE int sqlite3Au
2356c 74 68 43 68 65 63 6b 28 0a 20 20 50 61 72 73 65  thCheck(.  Parse
2356d 20 2a 70 50 61 72 73 65 2c 0a 20 20 69 6e 74 20   *pParse,.  int 
2356e 63 6f 64 65 2c 0a 20 20 63 6f 6e 73 74 20 63 68  code,.  const ch
2356f 61 72 20 2a 7a 41 72 67 31 2c 0a 20 20 63 6f 6e  ar *zArg1,.  con
23570 73 74 20 63 68 61 72 20 2a 7a 41 72 67 32 2c 0a  st char *zArg2,.
23571 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41    const char *zA
23572 72 67 33 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33  rg3.){.  sqlite3
23573 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
23574 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  b;.  int rc;..  
23575 2f 2a 20 44 6f 6e 27 74 20 64 6f 20 61 6e 79 20  /* Don't do any 
23576 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 68  authorization ch
23577 65 63 6b 73 20 69 66 20 74 68 65 20 64 61 74 61  ecks if the data
23578 62 61 73 65 20 69 73 20 69 6e 69 74 69 61 6c 69  base is initiali
23579 73 69 6e 67 0a 20 20 2a 2a 20 6f 72 20 69 66 20  sing.  ** or if 
2357a 74 68 65 20 70 61 72 73 65 72 20 69 73 20 62 65  the parser is be
2357b 69 6e 67 20 69 6e 76 6f 6b 65 64 20 66 72 6f 6d  ing invoked from
2357c 20 77 69 74 68 69 6e 20 73 71 6c 69 74 65 33 5f   within sqlite3_
2357d 64 65 63 6c 61 72 65 5f 76 74 61 62 2e 0a 20 20  declare_vtab..  
2357e 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69  */.  if( db->ini
2357f 74 2e 62 75 73 79 20 7c 7c 20 49 4e 5f 44 45 43  t.busy || IN_DEC
23580 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a 20 20 20  LARE_VTAB ){.   
23581 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
23582 4b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 64 62  K;.  }..  if( db
23583 2d 3e 78 41 75 74 68 3d 3d 30 20 29 7b 0a 20 20  ->xAuth==0 ){.  
23584 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
23585 4f 4b 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 64  OK;.  }.  rc = d
23586 62 2d 3e 78 41 75 74 68 28 64 62 2d 3e 70 41 75  b->xAuth(db->pAu
23587 74 68 41 72 67 2c 20 63 6f 64 65 2c 20 7a 41 72  thArg, code, zAr
23588 67 31 2c 20 7a 41 72 67 32 2c 20 7a 41 72 67 33  g1, zArg2, zArg3
23589 2c 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43  , pParse->zAuthC
2358a 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66 28 20 72  ontext);.  if( r
2358b 63 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20 29  c==SQLITE_DENY )
2358c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
2358d 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
2358e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 22 29 3b  ot authorized");
2358f 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20  .    pParse->rc 
23590 3d 20 53 51 4c 49 54 45 5f 41 55 54 48 3b 0a 20  = SQLITE_AUTH;. 
23591 20 7d 65 6c 73 65 20 69 66 28 20 72 63 21 3d 53   }else if( rc!=S
23592 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d  QLITE_OK && rc!=
23593 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 29 7b  SQLITE_IGNORE ){
23594 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
23595 5f 44 45 4e 59 3b 0a 20 20 20 20 73 71 6c 69 74  _DENY;.    sqlit
23596 65 41 75 74 68 42 61 64 52 65 74 75 72 6e 43 6f  eAuthBadReturnCo
23597 64 65 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a  de(pParse);.  }.
23598 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
23599 2f 2a 0a 2a 2a 20 50 75 73 68 20 61 6e 20 61 75  /*.** Push an au
2359a 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74  thorization cont
2359b 65 78 74 2e 20 20 41 66 74 65 72 20 74 68 69 73  ext.  After this
2359c 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
2359d 65 64 2c 20 74 68 65 0a 2a 2a 20 7a 41 72 67 33  ed, the.** zArg3
2359e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 75 74   argument to aut
2359f 68 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62  horization callb
235a0 61 63 6b 73 20 77 69 6c 6c 20 62 65 20 7a 43 6f  acks will be zCo
235a1 6e 74 65 78 74 20 75 6e 74 69 6c 0a 2a 2a 20 70  ntext until.** p
235a2 6f 70 70 65 64 2e 20 20 4f 72 20 69 66 20 70 50  opped.  Or if pP
235a3 61 72 73 65 3d 3d 30 2c 20 74 68 69 73 20 72 6f  arse==0, this ro
235a4 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
235a5 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
235a6 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
235a7 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28  AuthContextPush(
235a8 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
235a9 2c 0a 20 20 41 75 74 68 43 6f 6e 74 65 78 74 20  ,.  AuthContext 
235aa 2a 70 43 6f 6e 74 65 78 74 2c 20 0a 20 20 63 6f  *pContext, .  co
235ab 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65  nst char *zConte
235ac 78 74 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20  xt.){.  assert( 
235ad 70 50 61 72 73 65 20 29 3b 0a 20 20 70 43 6f 6e  pParse );.  pCon
235ae 74 65 78 74 2d 3e 70 50 61 72 73 65 20 3d 20 70  text->pParse = p
235af 50 61 72 73 65 3b 0a 20 20 70 43 6f 6e 74 65 78  Parse;.  pContex
235b0 74 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20  t->zAuthContext 
235b1 3d 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43  = pParse->zAuthC
235b2 6f 6e 74 65 78 74 3b 0a 20 20 70 50 61 72 73 65  ontext;.  pParse
235b3 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d  ->zAuthContext =
235b4 20 7a 43 6f 6e 74 65 78 74 3b 0a 7d 0a 0a 2f 2a   zContext;.}../*
235b5 0a 2a 2a 20 50 6f 70 20 61 6e 20 61 75 74 68 6f  .** Pop an autho
235b6 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74  rization context
235b7 20 74 68 61 74 20 77 61 73 20 70 72 65 76 69 6f   that was previo
235b8 75 73 6c 79 20 70 75 73 68 65 64 0a 2a 2a 20 62  usly pushed.** b
235b9 79 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e  y sqlite3AuthCon
235ba 74 65 78 74 50 75 73 68 0a 2a 2f 0a 53 51 4c 49  textPush.*/.SQLI
235bb 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
235bc 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65  sqlite3AuthConte
235bd 78 74 50 6f 70 28 41 75 74 68 43 6f 6e 74 65 78  xtPop(AuthContex
235be 74 20 2a 70 43 6f 6e 74 65 78 74 29 7b 0a 20 20  t *pContext){.  
235bf 69 66 28 20 70 43 6f 6e 74 65 78 74 2d 3e 70 50  if( pContext->pP
235c0 61 72 73 65 20 29 7b 0a 20 20 20 20 70 43 6f 6e  arse ){.    pCon
235c1 74 65 78 74 2d 3e 70 50 61 72 73 65 2d 3e 7a 41  text->pParse->zA
235c2 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 43 6f  uthContext = pCo
235c3 6e 74 65 78 74 2d 3e 7a 41 75 74 68 43 6f 6e 74  ntext->zAuthCont
235c4 65 78 74 3b 0a 20 20 20 20 70 43 6f 6e 74 65 78  ext;.    pContex
235c5 74 2d 3e 70 50 61 72 73 65 20 3d 20 30 3b 0a 20  t->pParse = 0;. 
235c6 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20   }.}..#endif /* 
235c7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
235c8 4f 52 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 2f 2a  ORIZATION */../*
235c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
235ca 64 20 6f 66 20 61 75 74 68 2e 63 20 2a 2a 2a 2a  d of auth.c ****
235cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
235ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
235cf 67 69 6e 20 66 69 6c 65 20 62 75 69 6c 64 2e 63  gin file build.c
235d0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
235d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
235d3 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
235d4 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
235d5 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
235d6 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
235d7 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
235d8 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
235d9 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
235da 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
235db 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
235dc 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
235dd 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
235de 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
235df 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
235e0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
235e1 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
235e2 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
235e3 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
235e4 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
235e5 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
235e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235e9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
235ea 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
235eb 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64  e contains C cod
235ec 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20  e routines that 
235ed 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20 74 68  are called by th
235ee 65 20 53 51 4c 69 74 65 20 70 61 72 73 65 72 0a  e SQLite parser.
235ef 2a 2a 20 77 68 65 6e 20 73 79 6e 74 61 78 20 72  ** when syntax r
235f0 75 6c 65 73 20 61 72 65 20 72 65 64 75 63 65 64  ules are reduced
235f1 2e 20 20 54 68 65 20 72 6f 75 74 69 6e 65 73 20  .  The routines 
235f2 69 6e 20 74 68 69 73 20 66 69 6c 65 20 68 61 6e  in this file han
235f3 64 6c 65 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f  dle the.** follo
235f4 77 69 6e 67 20 6b 69 6e 64 73 20 6f 66 20 53 51  wing kinds of SQ
235f5 4c 20 73 79 6e 74 61 78 3a 0a 2a 2a 0a 2a 2a 20  L syntax:.**.** 
235f6 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45      CREATE TABLE
235f7 0a 2a 2a 20 20 20 20 20 44 52 4f 50 20 54 41 42  .**     DROP TAB
235f8 4c 45 0a 2a 2a 20 20 20 20 20 43 52 45 41 54 45  LE.**     CREATE
235f9 20 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 44 52   INDEX.**     DR
235fa 4f 50 20 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20  OP INDEX.**     
235fb 63 72 65 61 74 69 6e 67 20 49 44 20 6c 69 73 74  creating ID list
235fc 73 0a 2a 2a 20 20 20 20 20 42 45 47 49 4e 20 54  s.**     BEGIN T
235fd 52 41 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20 20 20  RANSACTION.**   
235fe 20 20 43 4f 4d 4d 49 54 0a 2a 2a 20 20 20 20 20    COMMIT.**     
235ff 52 4f 4c 4c 42 41 43 4b 0a 2a 2a 0a 2a 2a 20 24  ROLLBACK.**.** $
23600 49 64 3a 20 62 75 69 6c 64 2e 63 2c 76 20 31 2e  Id: build.c,v 1.
23601 35 35 37 20 32 30 30 39 2f 30 37 2f 32 34 20 31  557 2009/07/24 1
23602 37 3a 35 38 3a 35 33 20 64 61 6e 69 65 6c 6b 31  7:58:53 danielk1
23603 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  977 Exp $.*/../*
23604 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
23605 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20   is called when 
23606 61 20 6e 65 77 20 53 51 4c 20 73 74 61 74 65 6d  a new SQL statem
23607 65 6e 74 20 69 73 20 62 65 67 69 6e 6e 69 6e 67  ent is beginning
23608 20 74 6f 0a 2a 2a 20 62 65 20 70 61 72 73 65 64   to.** be parsed
23609 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  .  Initialize th
2360a 65 20 70 50 61 72 73 65 20 73 74 72 75 63 74 75  e pParse structu
2360b 72 65 20 61 73 20 6e 65 65 64 65 64 2e 0a 2a 2f  re as needed..*/
2360c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2360d 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69  void sqlite3Begi
2360e 6e 50 61 72 73 65 28 50 61 72 73 65 20 2a 70 50  nParse(Parse *pP
2360f 61 72 73 65 2c 20 69 6e 74 20 65 78 70 6c 61 69  arse, int explai
23610 6e 46 6c 61 67 29 7b 0a 20 20 70 50 61 72 73 65  nFlag){.  pParse
23611 2d 3e 65 78 70 6c 61 69 6e 20 3d 20 28 75 38 29  ->explain = (u8)
23612 65 78 70 6c 61 69 6e 46 6c 61 67 3b 0a 20 20 70  explainFlag;.  p
23613 50 61 72 73 65 2d 3e 6e 56 61 72 20 3d 20 30 3b  Parse->nVar = 0;
23614 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
23615 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
23616 41 43 48 45 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54  ACHE./*.** The T
23617 61 62 6c 65 4c 6f 63 6b 20 73 74 72 75 63 74 75  ableLock structu
23618 72 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20  re is only used 
23619 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 54 61  by the sqlite3Ta
2361a 62 6c 65 4c 6f 63 6b 28 29 20 61 6e 64 0a 2a 2a  bleLock() and.**
2361b 20 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28   codeTableLocks(
2361c 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a  ) functions..*/.
2361d 73 74 72 75 63 74 20 54 61 62 6c 65 4c 6f 63 6b  struct TableLock
2361e 20 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20   {.  int iDb;   
2361f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
23620 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
23621 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 74  ning the table t
23622 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20  o be locked */. 
23623 20 69 6e 74 20 69 54 61 62 3b 20 20 20 20 20 20   int iTab;      
23624 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f        /* The roo
23625 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 74 61  t page of the ta
23626 62 6c 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64  ble to be locked
23627 20 2a 2f 0a 20 20 75 38 20 69 73 57 72 69 74 65   */.  u8 isWrite
23628 4c 6f 63 6b 3b 20 20 20 20 20 20 2f 2a 20 54 72  Lock;      /* Tr
23629 75 65 20 66 6f 72 20 77 72 69 74 65 20 6c 6f 63  ue for write loc
2362a 6b 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 61 20  k.  False for a 
2362b 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63  read lock */.  c
2362c 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2362d 3b 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74  ;   /* Name of t
2362e 68 65 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a  he table */.};..
2362f 2f 2a 0a 2a 2a 20 52 65 63 6f 72 64 20 74 68 65  /*.** Record the
23630 20 66 61 63 74 20 74 68 61 74 20 77 65 20 77 61   fact that we wa
23631 6e 74 20 74 6f 20 6c 6f 63 6b 20 61 20 74 61 62  nt to lock a tab
23632 6c 65 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20  le at run-time. 
23633 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c   .**.** The tabl
23634 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 68  e to be locked h
23635 61 73 20 72 6f 6f 74 20 70 61 67 65 20 69 54 61  as root page iTa
23636 62 20 61 6e 64 20 69 73 20 66 6f 75 6e 64 20 69  b and is found i
23637 6e 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 0a  n database iDb..
23638 2a 2a 20 41 20 72 65 61 64 20 6f 72 20 61 20 77  ** A read or a w
23639 72 69 74 65 20 6c 6f 63 6b 20 63 61 6e 20 62 65  rite lock can be
2363a 20 74 61 6b 65 6e 20 64 65 70 65 6e 64 69 6e 67   taken depending
2363b 20 6f 6e 20 69 73 57 72 69 74 65 6c 6f 63 6b 2e   on isWritelock.
2363c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
2363d 69 6e 65 20 6a 75 73 74 20 72 65 63 6f 72 64 73  ine just records
2363e 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74   the fact that t
2363f 68 65 20 6c 6f 63 6b 20 69 73 20 64 65 73 69 72  he lock is desir
23640 65 64 2e 20 20 54 68 65 0a 2a 2a 20 63 6f 64 65  ed.  The.** code
23641 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 6c 6f 63   to make the loc
23642 6b 20 6f 63 63 75 72 20 69 73 20 67 65 6e 65 72  k occur is gener
23643 61 74 65 64 20 62 79 20 61 20 6c 61 74 65 72 20  ated by a later 
23644 63 61 6c 6c 20 74 6f 0a 2a 2a 20 63 6f 64 65 54  call to.** codeT
23645 61 62 6c 65 4c 6f 63 6b 73 28 29 20 77 68 69 63  ableLocks() whic
23646 68 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20  h occurs during 
23647 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64  sqlite3FinishCod
23648 69 6e 67 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ing()..*/.SQLITE
23649 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2364a 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 0a  lite3TableLock(.
2364b 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2364c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2364d 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74  context */.  int
2364e 20 69 44 62 2c 20 20 20 20 20 20 20 20 20 20 20   iDb,           
2364f 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20  /* Index of the 
23650 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
23651 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20 74 6f  ing the table to
23652 20 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69   lock */.  int i
23653 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Tab,          /*
23654 20 52 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65   Root page numbe
23655 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  r of the table t
23656 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20  o be locked */. 
23657 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 2c   u8 isWriteLock,
23658 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
23659 61 20 77 72 69 74 65 20 6c 6f 63 6b 20 2a 2f 0a  a write lock */.
2365a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
2365b 61 6d 65 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20  ame  /* Name of 
2365c 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20  the table to be 
2365d 6c 6f 63 6b 65 64 20 2a 2f 0a 29 7b 0a 20 20 50  locked */.){.  P
2365e 61 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20  arse *pToplevel 
2365f 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f  = sqlite3ParseTo
23660 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0a  plevel(pParse);.
23661 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e    int i;.  int n
23662 42 79 74 65 73 3b 0a 20 20 54 61 62 6c 65 4c 6f  Bytes;.  TableLo
23663 63 6b 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28  ck *p;.  assert(
23664 20 69 44 62 3e 3d 30 20 29 3b 0a 0a 20 20 66 6f   iDb>=0 );..  fo
23665 72 28 69 3d 30 3b 20 69 3c 70 54 6f 70 6c 65 76  r(i=0; i<pToplev
23666 65 6c 2d 3e 6e 54 61 62 6c 65 4c 6f 63 6b 3b 20  el->nTableLock; 
23667 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 26 70  i++){.    p = &p
23668 54 6f 70 6c 65 76 65 6c 2d 3e 61 54 61 62 6c 65  Toplevel->aTable
23669 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20 20 69 66 28  Lock[i];.    if(
2366a 20 70 2d 3e 69 44 62 3d 3d 69 44 62 20 26 26 20   p->iDb==iDb && 
2366b 70 2d 3e 69 54 61 62 3d 3d 69 54 61 62 20 29 7b  p->iTab==iTab ){
2366c 0a 20 20 20 20 20 20 70 2d 3e 69 73 57 72 69 74  .      p->isWrit
2366d 65 4c 6f 63 6b 20 3d 20 28 70 2d 3e 69 73 57 72  eLock = (p->isWr
2366e 69 74 65 4c 6f 63 6b 20 7c 7c 20 69 73 57 72 69  iteLock || isWri
2366f 74 65 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20 72  teLock);.      r
23670 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d  eturn;.    }.  }
23671 0a 0a 20 20 6e 42 79 74 65 73 20 3d 20 73 69 7a  ..  nBytes = siz
23672 65 6f 66 28 54 61 62 6c 65 4c 6f 63 6b 29 20 2a  eof(TableLock) *
23673 20 28 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 54 61   (pToplevel->nTa
23674 62 6c 65 4c 6f 63 6b 2b 31 29 3b 0a 20 20 70 54  bleLock+1);.  pT
23675 6f 70 6c 65 76 65 6c 2d 3e 61 54 61 62 6c 65 4c  oplevel->aTableL
23676 6f 63 6b 20 3d 0a 20 20 20 20 20 20 73 71 6c 69  ock =.      sqli
23677 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72  te3DbReallocOrFr
23678 65 65 28 70 54 6f 70 6c 65 76 65 6c 2d 3e 64 62  ee(pToplevel->db
23679 2c 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 54 61  , pToplevel->aTa
2367a 62 6c 65 4c 6f 63 6b 2c 20 6e 42 79 74 65 73 29  bleLock, nBytes)
2367b 3b 0a 20 20 69 66 28 20 70 54 6f 70 6c 65 76 65  ;.  if( pTopleve
2367c 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 20 29 7b  l->aTableLock ){
2367d 0a 20 20 20 20 70 20 3d 20 26 70 54 6f 70 6c 65  .    p = &pTople
2367e 76 65 6c 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b  vel->aTableLock[
2367f 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 54 61 62 6c  pToplevel->nTabl
23680 65 4c 6f 63 6b 2b 2b 5d 3b 0a 20 20 20 20 70 2d  eLock++];.    p-
23681 3e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20  >iDb = iDb;.    
23682 70 2d 3e 69 54 61 62 20 3d 20 69 54 61 62 3b 0a  p->iTab = iTab;.
23683 20 20 20 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f      p->isWriteLo
23684 63 6b 20 3d 20 69 73 57 72 69 74 65 4c 6f 63 6b  ck = isWriteLock
23685 3b 0a 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 20 3d  ;.    p->zName =
23686 20 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b   zName;.  }else{
23687 0a 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e  .    pToplevel->
23688 6e 54 61 62 6c 65 4c 6f 63 6b 20 3d 20 30 3b 0a  nTableLock = 0;.
23689 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 64      pToplevel->d
2368a 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2368b 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  = 1;.  }.}../*.*
2368c 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f 54 61 62  * Code an OP_Tab
2368d 6c 65 4c 6f 63 6b 20 69 6e 73 74 72 75 63 74 69  leLock instructi
2368e 6f 6e 20 66 6f 72 20 65 61 63 68 20 74 61 62 6c  on for each tabl
2368f 65 20 6c 6f 63 6b 65 64 20 62 79 20 74 68 65 0a  e locked by the.
23690 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 28 63 6f  ** statement (co
23691 6e 66 69 67 75 72 65 64 20 62 79 20 63 61 6c 6c  nfigured by call
23692 73 20 74 6f 20 73 71 6c 69 74 65 33 54 61 62 6c  s to sqlite3Tabl
23693 65 4c 6f 63 6b 28 29 29 2e 0a 2a 2f 0a 73 74 61  eLock())..*/.sta
23694 74 69 63 20 76 6f 69 64 20 63 6f 64 65 54 61 62  tic void codeTab
23695 6c 65 4c 6f 63 6b 73 28 50 61 72 73 65 20 2a 70  leLocks(Parse *p
23696 50 61 72 73 65 29 7b 0a 20 20 69 6e 74 20 69 3b  Parse){.  int i;
23697 0a 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b 20  .  Vdbe *pVdbe; 
23698 0a 0a 20 20 70 56 64 62 65 20 3d 20 73 71 6c 69  ..  pVdbe = sqli
23699 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
2369a 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56  e);.  assert( pV
2369b 64 62 65 21 3d 30 20 29 3b 20 2f 2a 20 73 71 6c  dbe!=0 ); /* sql
2369c 69 74 65 33 47 65 74 56 64 62 65 20 63 61 6e 6e  ite3GetVdbe cann
2369d 6f 74 20 66 61 69 6c 3a 20 56 44 42 45 20 61 6c  ot fail: VDBE al
2369e 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 20  ready allocated 
2369f 2a 2f 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  */..  for(i=0; i
236a0 3c 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c  <pParse->nTableL
236a1 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 54  ock; i++){.    T
236a2 61 62 6c 65 4c 6f 63 6b 20 2a 70 20 3d 20 26 70  ableLock *p = &p
236a3 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63  Parse->aTableLoc
236a4 6b 5b 69 5d 3b 0a 20 20 20 20 69 6e 74 20 70 31  k[i];.    int p1
236a5 20 3d 20 70 2d 3e 69 44 62 3b 0a 20 20 20 20 73   = p->iDb;.    s
236a6 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
236a7 28 70 56 64 62 65 2c 20 4f 50 5f 54 61 62 6c 65  (pVdbe, OP_Table
236a8 4c 6f 63 6b 2c 20 70 31 2c 20 70 2d 3e 69 54 61  Lock, p1, p->iTa
236a9 62 2c 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63  b, p->isWriteLoc
236aa 6b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  k,.             
236ab 20 20 20 20 20 20 20 20 20 70 2d 3e 7a 4e 61 6d           p->zNam
236ac 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  e, P4_STATIC);. 
236ad 20 7d 0a 7d 0a 23 65 6c 73 65 0a 20 20 23 64 65   }.}.#else.  #de
236ae 66 69 6e 65 20 63 6f 64 65 54 61 62 6c 65 4c 6f  fine codeTableLo
236af 63 6b 73 28 78 29 0a 23 65 6e 64 69 66 0a 0a 2f  cks(x).#endif../
236b0 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
236b1 65 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65  e is called afte
236b2 72 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20 73  r a single SQL s
236b3 74 61 74 65 6d 65 6e 74 20 68 61 73 20 62 65 65  tatement has bee
236b4 6e 0a 2a 2a 20 70 61 72 73 65 64 20 61 6e 64 20  n.** parsed and 
236b5 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 74  a VDBE program t
236b6 6f 20 65 78 65 63 75 74 65 20 74 68 61 74 20 73  o execute that s
236b7 74 61 74 65 6d 65 6e 74 20 68 61 73 20 62 65 65  tatement has bee
236b8 6e 0a 2a 2a 20 70 72 65 70 61 72 65 64 2e 20 20  n.** prepared.  
236b9 54 68 69 73 20 72 6f 75 74 69 6e 65 20 70 75 74  This routine put
236ba 73 20 74 68 65 20 66 69 6e 69 73 68 69 6e 67 20  s the finishing 
236bb 74 6f 75 63 68 65 73 20 6f 6e 20 74 68 65 0a 2a  touches on the.*
236bc 2a 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 61  * VDBE program a
236bd 6e 64 20 72 65 73 65 74 73 20 74 68 65 20 70 50  nd resets the pP
236be 61 72 73 65 20 73 74 72 75 63 74 75 72 65 20 66  arse structure f
236bf 6f 72 20 74 68 65 20 6e 65 78 74 0a 2a 2a 20 70  or the next.** p
236c0 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65  arse..**.** Note
236c1 20 74 68 61 74 20 69 66 20 61 6e 20 65 72 72 6f   that if an erro
236c2 72 20 6f 63 63 75 72 72 65 64 2c 20 69 74 20 6d  r occurred, it m
236c3 69 67 68 74 20 62 65 20 74 68 65 20 63 61 73 65  ight be the case
236c4 20 74 68 61 74 0a 2a 2a 20 6e 6f 20 56 44 42 45   that.** no VDBE
236c5 20 63 6f 64 65 20 77 61 73 20 67 65 6e 65 72 61   code was genera
236c6 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
236c7 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
236c8 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e 67 28  te3FinishCoding(
236c9 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a  Parse *pParse){.
236ca 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20    sqlite3 *db;. 
236cb 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 64 62 20   Vdbe *v;..  db 
236cc 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
236cd 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
236ce 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20  iled ) return;. 
236cf 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73   if( pParse->nes
236d0 74 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ted ) return;.  
236d1 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
236d2 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a   ) return;..  /*
236d3 20 42 65 67 69 6e 20 62 79 20 67 65 6e 65 72 61   Begin by genera
236d4 74 69 6e 67 20 73 6f 6d 65 20 74 65 72 6d 69 6e  ting some termin
236d5 61 74 69 6f 6e 20 63 6f 64 65 20 61 74 20 74 68  ation code at th
236d6 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 20 20 2a  e end of the.  *
236d7 2a 20 76 64 62 65 20 70 72 6f 67 72 61 6d 0a 20  * vdbe program. 
236d8 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65   */.  v = sqlite
236d9 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
236da 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61  ;.  assert( !pPa
236db 72 73 65 2d 3e 69 73 4d 75 6c 74 69 57 72 69 74  rse->isMultiWrit
236dc 65 20 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c  e .       || sql
236dd 69 74 65 33 56 64 62 65 41 73 73 65 72 74 4d 61  ite3VdbeAssertMa
236de 79 41 62 6f 72 74 28 76 2c 20 70 50 61 72 73 65  yAbort(v, pParse
236df 2d 3e 6d 61 79 41 62 6f 72 74 29 29 3b 0a 20 20  ->mayAbort));.  
236e0 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c  if( v ){.    sql
236e1 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76  ite3VdbeAddOp0(v
236e2 2c 20 4f 50 5f 48 61 6c 74 29 3b 0a 0a 20 20 20  , OP_Halt);..   
236e3 20 2f 2a 20 54 68 65 20 63 6f 6f 6b 69 65 20 6d   /* The cookie m
236e4 61 73 6b 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65  ask contains one
236e5 20 62 69 74 20 66 6f 72 20 65 61 63 68 20 64 61   bit for each da
236e6 74 61 62 61 73 65 20 66 69 6c 65 20 6f 70 65 6e  tabase file open
236e7 2e 0a 20 20 20 20 2a 2a 20 28 42 69 74 20 30 20  ..    ** (Bit 0 
236e8 69 73 20 66 6f 72 20 6d 61 69 6e 2c 20 62 69 74  is for main, bit
236e9 20 31 20 69 73 20 66 6f 72 20 74 65 6d 70 2c 20   1 is for temp, 
236ea 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 29 20 20  and so forth.)  
236eb 42 69 74 73 20 61 72 65 0a 20 20 20 20 2a 2a 20  Bits are.    ** 
236ec 73 65 74 20 66 6f 72 20 65 61 63 68 20 64 61 74  set for each dat
236ed 61 62 61 73 65 20 74 68 61 74 20 69 73 20 75 73  abase that is us
236ee 65 64 2e 20 20 47 65 6e 65 72 61 74 65 20 63 6f  ed.  Generate co
236ef 64 65 20 74 6f 20 73 74 61 72 74 20 61 0a 20 20  de to start a.  
236f0 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e    ** transaction
236f1 20 6f 6e 20 65 61 63 68 20 75 73 65 64 20 64 61   on each used da
236f2 74 61 62 61 73 65 20 61 6e 64 20 74 6f 20 76 65  tabase and to ve
236f3 72 69 66 79 20 74 68 65 20 73 63 68 65 6d 61 20  rify the schema 
236f4 63 6f 6f 6b 69 65 0a 20 20 20 20 2a 2a 20 6f 6e  cookie.    ** on
236f5 20 65 61 63 68 20 75 73 65 64 20 64 61 74 61 62   each used datab
236f6 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ase..    */.    
236f7 69 66 28 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b  if( pParse->cook
236f8 69 65 47 6f 74 6f 3e 30 20 29 7b 0a 20 20 20 20  ieGoto>0 ){.    
236f9 20 20 75 33 32 20 6d 61 73 6b 3b 0a 20 20 20 20    u32 mask;.    
236fa 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20    int iDb;.     
236fb 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
236fc 48 65 72 65 28 76 2c 20 70 50 61 72 73 65 2d 3e  Here(v, pParse->
236fd 63 6f 6f 6b 69 65 47 6f 74 6f 2d 31 29 3b 0a 20  cookieGoto-1);. 
236fe 20 20 20 20 20 66 6f 72 28 69 44 62 3d 30 2c 20       for(iDb=0, 
236ff 6d 61 73 6b 3d 31 3b 20 69 44 62 3c 64 62 2d 3e  mask=1; iDb<db->
23700 6e 44 62 3b 20 6d 61 73 6b 3c 3c 3d 31 2c 20 69  nDb; mask<<=1, i
23701 44 62 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69  Db++){.        i
23702 66 28 20 28 6d 61 73 6b 20 26 20 70 50 61 72 73  f( (mask & pPars
23703 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 29 3d 3d  e->cookieMask)==
23704 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
23705 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
23706 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44  eUsesBtree(v, iD
23707 62 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  b);.        sqli
23708 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
23709 4f 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20  OP_Transaction, 
2370a 69 44 62 2c 20 28 6d 61 73 6b 20 26 20 70 50 61  iDb, (mask & pPa
2370b 72 73 65 2d 3e 77 72 69 74 65 4d 61 73 6b 29 21  rse->writeMask)!
2370c 3d 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  =0);.        if(
2370d 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d   db->init.busy==
2370e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  0 ){.          s
2370f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
23710 28 76 2c 4f 50 5f 56 65 72 69 66 79 43 6f 6f 6b  (v,OP_VerifyCook
23711 69 65 2c 20 69 44 62 2c 20 70 50 61 72 73 65 2d  ie, iDb, pParse-
23712 3e 63 6f 6f 6b 69 65 56 61 6c 75 65 5b 69 44 62  >cookieValue[iDb
23713 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ]);.        }.  
23714 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
23715 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
23716 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 7b 0a 20  LTABLE.      {. 
23717 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20         int i;.  
23718 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
23719 3c 70 50 61 72 73 65 2d 3e 6e 56 74 61 62 4c 6f  <pParse->nVtabLo
2371a 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ck; i++){.      
2371b 20 20 20 20 63 68 61 72 20 2a 76 74 61 62 20 3d      char *vtab =
2371c 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33   (char *)sqlite3
2371d 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70 50  GetVTable(db, pP
2371e 61 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f 63 6b  arse->apVtabLock
2371f 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20  [i]);.          
23720 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23721 34 28 76 2c 20 4f 50 5f 56 42 65 67 69 6e 2c 20  4(v, OP_VBegin, 
23722 30 2c 20 30 2c 20 30 2c 20 76 74 61 62 2c 20 50  0, 0, 0, vtab, P
23723 34 5f 56 54 41 42 29 3b 0a 20 20 20 20 20 20 20  4_VTAB);.       
23724 20 7d 0a 20 20 20 20 20 20 20 20 70 50 61 72 73   }.        pPars
23725 65 2d 3e 6e 56 74 61 62 4c 6f 63 6b 20 3d 20 30  e->nVtabLock = 0
23726 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  ;.      }.#endif
23727 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 6e 63 65 20  ..      /* Once 
23728 61 6c 6c 20 74 68 65 20 63 6f 6f 6b 69 65 73 20  all the cookies 
23729 68 61 76 65 20 62 65 65 6e 20 76 65 72 69 66 69  have been verifi
2372a 65 64 20 61 6e 64 20 74 72 61 6e 73 61 63 74 69  ed and transacti
2372b 6f 6e 73 20 6f 70 65 6e 65 64 2c 20 0a 20 20 20  ons opened, .   
2372c 20 20 20 2a 2a 20 6f 62 74 61 69 6e 20 74 68 65     ** obtain the
2372d 20 72 65 71 75 69 72 65 64 20 74 61 62 6c 65 2d   required table-
2372e 6c 6f 63 6b 73 2e 20 54 68 69 73 20 69 73 20 61  locks. This is a
2372f 20 6e 6f 2d 6f 70 20 75 6e 6c 65 73 73 20 74 68   no-op unless th
23730 65 20 0a 20 20 20 20 20 20 2a 2a 20 73 68 61 72  e .      ** shar
23731 65 64 2d 63 61 63 68 65 20 66 65 61 74 75 72 65  ed-cache feature
23732 20 69 73 20 65 6e 61 62 6c 65 64 2e 0a 20 20 20   is enabled..   
23733 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f 64 65     */.      code
23734 54 61 62 6c 65 4c 6f 63 6b 73 28 70 50 61 72 73  TableLocks(pPars
23735 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 6e  e);..      /* In
23736 69 74 69 61 6c 69 7a 65 20 61 6e 79 20 41 55 54  itialize any AUT
23737 4f 49 4e 43 52 45 4d 45 4e 54 20 64 61 74 61 20  OINCREMENT data 
23738 73 74 72 75 63 74 75 72 65 73 20 72 65 71 75 69  structures requi
23739 72 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  red..      */.  
2373a 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 6f 69      sqlite3Autoi
2373b 6e 63 72 65 6d 65 6e 74 42 65 67 69 6e 28 70 50  ncrementBegin(pP
2373c 61 72 73 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  arse);..      /*
2373d 20 46 69 6e 61 6c 6c 79 2c 20 6a 75 6d 70 20 62   Finally, jump b
2373e 61 63 6b 20 74 6f 20 74 68 65 20 62 65 67 69 6e  ack to the begin
2373f 6e 69 6e 67 20 6f 66 20 74 68 65 20 65 78 65 63  ning of the exec
23740 75 74 61 62 6c 65 20 63 6f 64 65 2e 20 2a 2f 0a  utable code. */.
23741 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
23742 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
23743 74 6f 2c 20 30 2c 20 70 50 61 72 73 65 2d 3e 63  to, 0, pParse->c
23744 6f 6f 6b 69 65 47 6f 74 6f 29 3b 0a 20 20 20 20  ookieGoto);.    
23745 7d 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20 47 65 74  }.  }...  /* Get
23746 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61   the VDBE progra
23747 6d 20 72 65 61 64 79 20 66 6f 72 20 65 78 65 63  m ready for exec
23748 75 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20 69 66 28  ution.  */.  if(
23749 20 76 20 26 26 20 41 4c 57 41 59 53 28 70 50 61   v && ALWAYS(pPa
2374a 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 29 20 26 26  rse->nErr==0) &&
2374b 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
2374c 65 64 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c  ed ){.#ifdef SQL
2374d 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 46 49  ITE_DEBUG.    FI
2374e 4c 45 20 2a 74 72 61 63 65 20 3d 20 28 64 62 2d  LE *trace = (db-
2374f 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
23750 56 64 62 65 54 72 61 63 65 29 21 3d 30 20 3f 20  VdbeTrace)!=0 ? 
23751 73 74 64 6f 75 74 20 3a 20 30 3b 0a 20 20 20 20  stdout : 0;.    
23752 73 71 6c 69 74 65 33 56 64 62 65 54 72 61 63 65  sqlite3VdbeTrace
23753 28 76 2c 20 74 72 61 63 65 29 3b 0a 23 65 6e 64  (v, trace);.#end
23754 69 66 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  if.    assert( p
23755 50 61 72 73 65 2d 3e 69 43 61 63 68 65 4c 65 76  Parse->iCacheLev
23756 65 6c 3d 3d 30 20 29 3b 20 20 2f 2a 20 44 69 73  el==0 );  /* Dis
23757 61 62 6c 65 73 20 61 6e 64 20 72 65 2d 65 6e 61  ables and re-ena
23758 62 6c 65 73 20 6d 61 74 63 68 20 2a 2f 0a 20 20  bles match */.  
23759 20 20 2f 2a 20 41 20 6d 69 6e 69 6d 75 6d 20 6f    /* A minimum o
2375a 66 20 6f 6e 65 20 63 75 72 73 6f 72 20 69 73 20  f one cursor is 
2375b 72 65 71 75 69 72 65 64 20 69 66 20 61 75 74 6f  required if auto
2375c 69 6e 63 72 65 6d 65 6e 74 20 69 73 20 75 73 65  increment is use
2375d 64 0a 20 20 20 20 2a 20 20 53 65 65 20 74 69 63  d.    *  See tic
2375e 6b 65 74 20 5b 61 36 39 36 33 37 39 63 31 66 30  ket [a696379c1f0
2375f 38 38 36 36 5d 20 2a 2f 0a 20 20 20 20 69 66 28  8866] */.    if(
23760 20 70 50 61 72 73 65 2d 3e 70 41 69 6e 63 21 3d   pParse->pAinc!=
23761 30 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 54 61  0 && pParse->nTa
23762 62 3d 3d 30 20 29 20 70 50 61 72 73 65 2d 3e 6e  b==0 ) pParse->n
23763 54 61 62 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c  Tab = 1;.    sql
23764 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64  ite3VdbeMakeRead
23765 79 28 76 2c 20 70 50 61 72 73 65 2d 3e 6e 56 61  y(v, pParse->nVa
23766 72 2c 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2c  r, pParse->nMem,
23767 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
23768 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65            pParse
23769 2d 3e 6e 54 61 62 2c 20 70 50 61 72 73 65 2d 3e  ->nTab, pParse->
2376a 6e 4d 61 78 41 72 67 2c 20 70 50 61 72 73 65 2d  nMaxArg, pParse-
2376b 3e 65 78 70 6c 61 69 6e 2c 0a 20 20 20 20 20 20  >explain,.      
2376c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2376d 20 20 20 70 50 61 72 73 65 2d 3e 69 73 4d 75 6c     pParse->isMul
2376e 74 69 57 72 69 74 65 20 26 26 20 70 50 61 72 73  tiWrite && pPars
2376f 65 2d 3e 6d 61 79 41 62 6f 72 74 29 3b 0a 20 20  e->mayAbort);.  
23770 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
23771 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20  QLITE_DONE;.    
23772 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61 6d 65 73  pParse->colNames
23773 53 65 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  Set = 0;.  }else
23774 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63 3d   if( pParse->rc=
23775 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
23776 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 53    pParse->rc = S
23777 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
23778 0a 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 20  .  pParse->nTab 
23779 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e  = 0;.  pParse->n
2377a 4d 65 6d 20 3d 20 30 3b 0a 20 20 70 50 61 72 73  Mem = 0;.  pPars
2377b 65 2d 3e 6e 53 65 74 20 3d 20 30 3b 0a 20 20 70  e->nSet = 0;.  p
2377c 50 61 72 73 65 2d 3e 6e 56 61 72 20 3d 20 30 3b  Parse->nVar = 0;
2377d 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69  .  pParse->cooki
2377e 65 4d 61 73 6b 20 3d 20 30 3b 0a 20 20 70 50 61  eMask = 0;.  pPa
2377f 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 20  rse->cookieGoto 
23780 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 75  = 0;.}../*.** Ru
23781 6e 20 74 68 65 20 70 61 72 73 65 72 20 61 6e 64  n the parser and
23782 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f 72 20   code generator 
23783 72 65 63 75 72 73 69 76 65 6c 79 20 69 6e 20 6f  recursively in o
23784 72 64 65 72 20 74 6f 20 67 65 6e 65 72 61 74 65  rder to generate
23785 0a 2a 2a 20 63 6f 64 65 20 66 6f 72 20 74 68 65  .** code for the
23786 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 67   SQL statement g
23787 69 76 65 6e 20 6f 6e 74 6f 20 74 68 65 20 65 6e  iven onto the en
23788 64 20 6f 66 20 74 68 65 20 70 50 61 72 73 65 20  d of the pParse 
23789 63 6f 6e 74 65 78 74 0a 2a 2a 20 63 75 72 72 65  context.** curre
2378a 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74  ntly under const
2378b 72 75 63 74 69 6f 6e 2e 20 20 57 68 65 6e 20 74  ruction.  When t
2378c 68 65 20 70 61 72 73 65 72 20 69 73 20 72 75 6e  he parser is run
2378d 20 72 65 63 75 72 73 69 76 65 6c 79 0a 2a 2a 20   recursively.** 
2378e 74 68 69 73 20 77 61 79 2c 20 74 68 65 20 66 69  this way, the fi
2378f 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 69 73 20 6e  nal OP_Halt is n
23790 6f 74 20 61 70 70 65 6e 64 65 64 20 61 6e 64 20  ot appended and 
23791 6f 74 68 65 72 20 69 6e 69 74 69 61 6c 69 7a 61  other initializa
23792 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 66 69 6e 61  tion.** and fina
23793 6c 69 7a 61 74 69 6f 6e 20 73 74 65 70 73 20 61  lization steps a
23794 72 65 20 6f 6d 69 74 74 65 64 20 62 65 63 61 75  re omitted becau
23795 73 65 20 74 68 6f 73 65 20 61 72 65 20 68 61 6e  se those are han
23796 64 6c 69 6e 67 20 62 79 20 74 68 65 0a 2a 2a 20  dling by the.** 
23797 6f 75 74 65 72 6d 6f 73 74 20 70 61 72 73 65 72  outermost parser
23798 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 20 65 76 65 72  ..**.** Not ever
23799 79 74 68 69 6e 67 20 69 73 20 6e 65 73 74 61 62  ything is nestab
2379a 6c 65 2e 20 20 54 68 69 73 20 66 61 63 69 6c 69  le.  This facili
2379b 74 79 20 69 73 20 64 65 73 69 67 6e 65 64 20 74  ty is designed t
2379c 6f 20 70 65 72 6d 69 74 0a 2a 2a 20 49 4e 53 45  o permit.** INSE
2379d 52 54 2c 20 55 50 44 41 54 45 2c 20 61 6e 64 20  RT, UPDATE, and 
2379e 44 45 4c 45 54 45 20 6f 70 65 72 61 74 69 6f 6e  DELETE operation
2379f 73 20 61 67 61 69 6e 73 74 20 53 51 4c 49 54 45  s against SQLITE
237a0 5f 4d 41 53 54 45 52 2e 20 20 55 73 65 0a 2a 2a  _MASTER.  Use.**
237a1 20 63 61 72 65 20 69 66 20 79 6f 75 20 64 65 63   care if you dec
237a2 69 64 65 20 74 6f 20 74 72 79 20 74 6f 20 75 73  ide to try to us
237a3 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66  e this routine f
237a4 6f 72 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 75  or some other pu
237a5 72 70 6f 73 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54  rposes..*/.SQLIT
237a6 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
237a7 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
237a8 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
237a9 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
237aa 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  rmat, ...){.  va
237ab 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63 68 61 72  _list ap;.  char
237ac 20 2a 7a 53 71 6c 3b 0a 20 20 63 68 61 72 20 2a   *zSql;.  char *
237ad 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 73  zErrMsg = 0;.  s
237ae 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
237af 72 73 65 2d 3e 64 62 3b 0a 23 20 64 65 66 69 6e  rse->db;.# defin
237b0 65 20 53 41 56 45 5f 53 5a 20 20 28 73 69 7a 65  e SAVE_SZ  (size
237b1 6f 66 28 50 61 72 73 65 29 20 2d 20 6f 66 66 73  of(Parse) - offs
237b2 65 74 6f 66 28 50 61 72 73 65 2c 6e 56 61 72 29  etof(Parse,nVar)
237b3 29 0a 20 20 63 68 61 72 20 73 61 76 65 42 75 66  ).  char saveBuf
237b4 5b 53 41 56 45 5f 53 5a 5d 3b 0a 0a 20 20 69 66  [SAVE_SZ];..  if
237b5 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29  ( pParse->nErr )
237b6 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72   return;.  asser
237b7 74 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  t( pParse->neste
237b8 64 3c 31 30 20 29 3b 20 20 2f 2a 20 4e 65 73 74  d<10 );  /* Nest
237b9 69 6e 67 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20  ing should only 
237ba 62 65 20 6f 66 20 6c 69 6d 69 74 65 64 20 64 65  be of limited de
237bb 70 74 68 20 2a 2f 0a 20 20 76 61 5f 73 74 61 72  pth */.  va_star
237bc 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a  t(ap, zFormat);.
237bd 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33    zSql = sqlite3
237be 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f  VMPrintf(db, zFo
237bf 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f  rmat, ap);.  va_
237c0 65 6e 64 28 61 70 29 3b 0a 20 20 69 66 28 20 7a  end(ap);.  if( z
237c1 53 71 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  Sql==0 ){.    re
237c2 74 75 72 6e 3b 20 20 20 2f 2a 20 41 20 6d 61 6c  turn;   /* A mal
237c3 6c 6f 63 20 6d 75 73 74 20 68 61 76 65 20 66 61  loc must have fa
237c4 69 6c 65 64 20 2a 2f 0a 20 20 7d 0a 20 20 70 50  iled */.  }.  pP
237c5 61 72 73 65 2d 3e 6e 65 73 74 65 64 2b 2b 3b 0a  arse->nested++;.
237c6 20 20 6d 65 6d 63 70 79 28 73 61 76 65 42 75 66    memcpy(saveBuf
237c7 2c 20 26 70 50 61 72 73 65 2d 3e 6e 56 61 72 2c  , &pParse->nVar,
237c8 20 53 41 56 45 5f 53 5a 29 3b 0a 20 20 6d 65 6d   SAVE_SZ);.  mem
237c9 73 65 74 28 26 70 50 61 72 73 65 2d 3e 6e 56 61  set(&pParse->nVa
237ca 72 2c 20 30 2c 20 53 41 56 45 5f 53 5a 29 3b 0a  r, 0, SAVE_SZ);.
237cb 20 20 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73    sqlite3RunPars
237cc 65 72 28 70 50 61 72 73 65 2c 20 7a 53 71 6c 2c  er(pParse, zSql,
237cd 20 26 7a 45 72 72 4d 73 67 29 3b 0a 20 20 73 71   &zErrMsg);.  sq
237ce 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
237cf 7a 45 72 72 4d 73 67 29 3b 0a 20 20 73 71 6c 69  zErrMsg);.  sqli
237d0 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53  te3DbFree(db, zS
237d1 71 6c 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 70  ql);.  memcpy(&p
237d2 50 61 72 73 65 2d 3e 6e 56 61 72 2c 20 73 61 76  Parse->nVar, sav
237d3 65 42 75 66 2c 20 53 41 56 45 5f 53 5a 29 3b 0a  eBuf, SAVE_SZ);.
237d4 20 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64    pParse->nested
237d5 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63  --;.}../*.** Loc
237d6 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72  ate the in-memor
237d7 79 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  y structure that
237d8 20 64 65 73 63 72 69 62 65 73 20 61 20 70 61 72   describes a par
237d9 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65  ticular database
237da 0a 2a 2a 20 74 61 62 6c 65 20 67 69 76 65 6e 20  .** table given 
237db 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 61 74  the name of that
237dc 20 74 61 62 6c 65 20 61 6e 64 20 28 6f 70 74 69   table and (opti
237dd 6f 6e 61 6c 6c 79 29 20 74 68 65 20 6e 61 6d 65  onally) the name
237de 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62   of the.** datab
237df 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ase containing t
237e0 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75 72  he table.  Retur
237e1 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f  n NULL if not fo
237e2 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 44  und..**.** If zD
237e3 61 74 61 62 61 73 65 20 69 73 20 30 2c 20 61 6c  atabase is 0, al
237e4 6c 20 64 61 74 61 62 61 73 65 73 20 61 72 65 20  l databases are 
237e5 73 65 61 72 63 68 65 64 20 66 6f 72 20 74 68 65  searched for the
237e6 20 74 61 62 6c 65 20 61 6e 64 20 74 68 65 0a 2a   table and the.*
237e7 2a 20 66 69 72 73 74 20 6d 61 74 63 68 69 6e 67  * first matching
237e8 20 74 61 62 6c 65 20 69 73 20 72 65 74 75 72 6e   table is return
237e9 65 64 2e 20 20 28 4e 6f 20 63 68 65 63 6b 69 6e  ed.  (No checkin
237ea 67 20 66 6f 72 20 64 75 70 6c 69 63 61 74 65 20  g for duplicate 
237eb 74 61 62 6c 65 0a 2a 2a 20 6e 61 6d 65 73 20 69  table.** names i
237ec 73 20 64 6f 6e 65 2e 29 20 20 54 68 65 20 73 65  s done.)  The se
237ed 61 72 63 68 20 6f 72 64 65 72 20 69 73 20 54 45  arch order is TE
237ee 4d 50 20 66 69 72 73 74 2c 20 74 68 65 6e 20 4d  MP first, then M
237ef 41 49 4e 2c 20 74 68 65 6e 20 61 6e 79 0a 2a 2a  AIN, then any.**
237f0 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62   auxiliary datab
237f1 61 73 65 73 20 61 64 64 65 64 20 75 73 69 6e 67  ases added using
237f2 20 74 68 65 20 41 54 54 41 43 48 20 63 6f 6d 6d   the ATTACH comm
237f3 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  and..**.** See a
237f4 6c 73 6f 20 73 71 6c 69 74 65 33 4c 6f 63 61 74  lso sqlite3Locat
237f5 65 54 61 62 6c 65 28 29 2e 0a 2a 2f 0a 53 51 4c  eTable()..*/.SQL
237f6 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c  ITE_PRIVATE Tabl
237f7 65 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 54 61  e *sqlite3FindTa
237f8 62 6c 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ble(sqlite3 *db,
237f9 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
237fa 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  me, const char *
237fb 7a 44 61 74 61 62 61 73 65 29 7b 0a 20 20 54 61  zDatabase){.  Ta
237fc 62 6c 65 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e  ble *p = 0;.  in
237fd 74 20 69 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65  t i;.  int nName
237fe 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 4e 61 6d  ;.  assert( zNam
237ff 65 21 3d 30 20 29 3b 0a 20 20 6e 4e 61 6d 65 20  e!=0 );.  nName 
23800 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
23801 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 28  0(zName);.  for(
23802 69 3d 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69  i=OMIT_TEMPDB; i
23803 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
23804 20 20 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32      int j = (i<2
23805 29 20 3f 20 69 5e 31 20 3a 20 69 3b 20 20 20 2f  ) ? i^1 : i;   /
23806 2a 20 53 65 61 72 63 68 20 54 45 4d 50 20 62 65  * Search TEMP be
23807 66 6f 72 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20  fore MAIN */.   
23808 20 69 66 28 20 7a 44 61 74 61 62 61 73 65 21 3d   if( zDatabase!=
23809 30 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49  0 && sqlite3StrI
2380a 43 6d 70 28 7a 44 61 74 61 62 61 73 65 2c 20 64  Cmp(zDatabase, d
2380b 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29  b->aDb[j].zName)
2380c 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
2380d 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68   p = sqlite3Hash
2380e 46 69 6e 64 28 26 64 62 2d 3e 61 44 62 5b 6a 5d  Find(&db->aDb[j]
2380f 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73  .pSchema->tblHas
23810 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  h, zName, nName)
23811 3b 0a 20 20 20 20 69 66 28 20 70 20 29 20 62 72  ;.    if( p ) br
23812 65 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  eak;.  }.  retur
23813 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f  n p;.}../*.** Lo
23814 63 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f  cate the in-memo
23815 72 79 20 73 74 72 75 63 74 75 72 65 20 74 68 61  ry structure tha
23816 74 20 64 65 73 63 72 69 62 65 73 20 61 20 70 61  t describes a pa
23817 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73  rticular databas
23818 65 0a 2a 2a 20 74 61 62 6c 65 20 67 69 76 65 6e  e.** table given
23819 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 61   the name of tha
2381a 74 20 74 61 62 6c 65 20 61 6e 64 20 28 6f 70 74  t table and (opt
2381b 69 6f 6e 61 6c 6c 79 29 20 74 68 65 20 6e 61 6d  ionally) the nam
2381c 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61  e of the.** data
2381d 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
2381e 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74 75  the table.  Retu
2381f 72 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66  rn NULL if not f
23820 6f 75 6e 64 2e 20 20 41 6c 73 6f 20 6c 65 61 76  ound.  Also leav
23821 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6d 65  e an.** error me
23822 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2d  ssage in pParse-
23823 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20  >zErrMsg..**.** 
23824 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 62  The difference b
23825 65 74 77 65 65 6e 20 74 68 69 73 20 72 6f 75 74  etween this rout
23826 69 6e 65 20 61 6e 64 20 73 71 6c 69 74 65 33 46  ine and sqlite3F
23827 69 6e 64 54 61 62 6c 65 28 29 20 69 73 20 74 68  indTable() is th
23828 61 74 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69  at this.** routi
23829 6e 65 20 6c 65 61 76 65 73 20 61 6e 20 65 72 72  ne leaves an err
2382a 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50  or message in pP
2382b 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 77 68  arse->zErrMsg wh
2382c 65 72 65 0a 2a 2a 20 73 71 6c 69 74 65 33 46 69  ere.** sqlite3Fi
2382d 6e 64 54 61 62 6c 65 28 29 20 64 6f 65 73 20 6e  ndTable() does n
2382e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ot..*/.SQLITE_PR
2382f 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c  IVATE Table *sql
23830 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28  ite3LocateTable(
23831 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
23832 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6e  ,         /* con
23833 74 65 78 74 20 69 6e 20 77 68 69 63 68 20 74 6f  text in which to
23834 20 72 65 70 6f 72 74 20 65 72 72 6f 72 73 20 2a   report errors *
23835 2f 0a 20 20 69 6e 74 20 69 73 56 69 65 77 2c 20  /.  int isView, 
23836 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
23837 75 65 20 69 66 20 6c 6f 6f 6b 69 6e 67 20 66 6f  ue if looking fo
23838 72 20 61 20 56 49 45 57 20 72 61 74 68 65 72 20  r a VIEW rather 
23839 74 68 61 6e 20 61 20 54 41 42 4c 45 20 2a 2f 0a  than a TABLE */.
2383a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
2383b 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ame,     /* Name
2383c 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 77 65   of the table we
2383d 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72   are looking for
2383e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
2383f 20 2a 7a 44 62 61 73 65 20 20 20 20 20 2f 2a 20   *zDbase     /* 
23840 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  Name of the data
23841 62 61 73 65 2e 20 20 4d 69 67 68 74 20 62 65 20  base.  Might be 
23842 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 54 61 62  NULL */.){.  Tab
23843 6c 65 20 2a 70 3b 0a 0a 20 20 2f 2a 20 52 65 61  le *p;..  /* Rea
23844 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  d the database s
23845 63 68 65 6d 61 2e 20 49 66 20 61 6e 20 65 72 72  chema. If an err
23846 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65 61 76 65  or occurs, leave
23847 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
23848 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64 65 20  e.  ** and code 
23849 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72 65  in pParse and re
2384a 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20  turn NULL. */.  
2384b 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
2384c 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
2384d 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20  (pParse) ){.    
2384e 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
2384f 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64   p = sqlite3Find
23850 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 64 62  Table(pParse->db
23851 2c 20 7a 4e 61 6d 65 2c 20 7a 44 62 61 73 65 29  , zName, zDbase)
23852 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a  ;.  if( p==0 ){.
23853 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
23854 7a 4d 73 67 20 3d 20 69 73 56 69 65 77 20 3f 20  zMsg = isView ? 
23855 22 6e 6f 20 73 75 63 68 20 76 69 65 77 22 20 3a  "no such view" :
23856 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 22   "no such table"
23857 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 61 73 65  ;.    if( zDbase
23858 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
23859 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2385a 2c 20 22 25 73 3a 20 25 73 2e 25 73 22 2c 20 7a  , "%s: %s.%s", z
2385b 4d 73 67 2c 20 7a 44 62 61 73 65 2c 20 7a 4e 61  Msg, zDbase, zNa
2385c 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  me);.    }else{.
2385d 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
2385e 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25  orMsg(pParse, "%
2385f 73 3a 20 25 73 22 2c 20 7a 4d 73 67 2c 20 7a 4e  s: %s", zMsg, zN
23860 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ame);.    }.    
23861 70 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68  pParse->checkSch
23862 65 6d 61 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72  ema = 1;.  }.  r
23863 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
23864 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 2d  * Locate the in-
23865 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65  memory structure
23866 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20   that describes 
23867 0a 2a 2a 20 61 20 70 61 72 74 69 63 75 6c 61 72  .** a particular
23868 20 69 6e 64 65 78 20 67 69 76 65 6e 20 74 68 65   index given the
23869 20 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 69 6e   name of that in
2386a 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 6e  dex.** and the n
2386b 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
2386c 61 73 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e  ase that contain
2386d 73 20 74 68 65 20 69 6e 64 65 78 2e 0a 2a 2a 20  s the index..** 
2386e 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e  Return NULL if n
2386f 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20  ot found..**.** 
23870 49 66 20 7a 44 61 74 61 62 61 73 65 20 69 73 20  If zDatabase is 
23871 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73  0, all databases
23872 20 61 72 65 20 73 65 61 72 63 68 65 64 20 66 6f   are searched fo
23873 72 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 61  r the.** table a
23874 6e 64 20 74 68 65 20 66 69 72 73 74 20 6d 61 74  nd the first mat
23875 63 68 69 6e 67 20 69 6e 64 65 78 20 69 73 20 72  ching index is r
23876 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63 68  eturned.  (No ch
23877 65 63 6b 69 6e 67 0a 2a 2a 20 66 6f 72 20 64 75  ecking.** for du
23878 70 6c 69 63 61 74 65 20 69 6e 64 65 78 20 6e 61  plicate index na
23879 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 20 20 54  mes is done.)  T
2387a 68 65 20 73 65 61 72 63 68 20 6f 72 64 65 72 20  he search order 
2387b 69 73 0a 2a 2a 20 54 45 4d 50 20 66 69 72 73 74  is.** TEMP first
2387c 2c 20 74 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65  , then MAIN, the
2387d 6e 20 61 6e 79 20 61 75 78 69 6c 69 61 72 79 20  n any auxiliary 
2387e 64 61 74 61 62 61 73 65 73 20 61 64 64 65 64 0a  databases added.
2387f 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 41 54 54  ** using the ATT
23880 41 43 48 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a  ACH command..*/.
23881 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49  SQLITE_PRIVATE I
23882 6e 64 65 78 20 2a 73 71 6c 69 74 65 33 46 69 6e  ndex *sqlite3Fin
23883 64 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a  dIndex(sqlite3 *
23884 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
23885 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63 68 61  zName, const cha
23886 72 20 2a 7a 44 62 29 7b 0a 20 20 49 6e 64 65 78  r *zDb){.  Index
23887 20 2a 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69   *p = 0;.  int i
23888 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20  ;.  int nName = 
23889 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2388a 7a 4e 61 6d 65 29 3b 0a 20 20 66 6f 72 28 69 3d  zName);.  for(i=
2388b 4f 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64  OMIT_TEMPDB; i<d
2388c 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
2388d 20 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32 29 20    int j = (i<2) 
2388e 3f 20 69 5e 31 20 3a 20 69 3b 20 20 2f 2a 20 53  ? i^1 : i;  /* S
2388f 65 61 72 63 68 20 54 45 4d 50 20 62 65 66 6f 72  earch TEMP befor
23890 65 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 53 63  e MAIN */.    Sc
23891 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20  hema *pSchema = 
23892 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63 68 65  db->aDb[j].pSche
23893 6d 61 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ma;.    assert( 
23894 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 69  pSchema );.    i
23895 66 28 20 7a 44 62 20 26 26 20 73 71 6c 69 74 65  f( zDb && sqlite
23896 33 53 74 72 49 43 6d 70 28 7a 44 62 2c 20 64 62  3StrICmp(zDb, db
23897 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d 65 29 20  ->aDb[j].zName) 
23898 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
23899 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46  p = sqlite3HashF
2389a 69 6e 64 28 26 70 53 63 68 65 6d 61 2d 3e 69 64  ind(&pSchema->id
2389b 78 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e  xHash, zName, nN
2389c 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70 20  ame);.    if( p 
2389d 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 72  ) break;.  }.  r
2389e 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn p;.}../*.*
2389f 2a 20 52 65 63 6c 61 69 6d 20 74 68 65 20 6d 65  * Reclaim the me
238a0 6d 6f 72 79 20 75 73 65 64 20 62 79 20 61 6e 20  mory used by an 
238a1 69 6e 64 65 78 0a 2a 2f 0a 73 74 61 74 69 63 20  index.*/.static 
238a2 76 6f 69 64 20 66 72 65 65 49 6e 64 65 78 28 49  void freeIndex(I
238a3 6e 64 65 78 20 2a 70 29 7b 0a 20 20 73 71 6c 69  ndex *p){.  sqli
238a4 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 70 54 61  te3 *db = p->pTa
238a5 62 6c 65 2d 3e 64 62 4d 65 6d 3b 0a 23 69 66 6e  ble->dbMem;.#ifn
238a6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
238a7 41 4e 41 4c 59 5a 45 0a 20 20 73 71 6c 69 74 65  ANALYZE.  sqlite
238a8 33 44 65 6c 65 74 65 49 6e 64 65 78 53 61 6d 70  3DeleteIndexSamp
238a9 6c 65 73 28 70 29 3b 0a 23 65 6e 64 69 66 0a 20  les(p);.#endif. 
238aa 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
238ab 62 2c 20 70 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0a  b, p->zColAff);.
238ac 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
238ad 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  db, p);.}../*.**
238ae 20 52 65 6d 6f 76 65 20 74 68 65 20 67 69 76 65   Remove the give
238af 6e 20 69 6e 64 65 78 20 66 72 6f 6d 20 74 68 65  n index from the
238b0 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 62 6c   index hash tabl
238b1 65 2c 20 61 6e 64 20 66 72 65 65 0a 2a 2a 20 69  e, and free.** i
238b2 74 73 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74  ts memory struct
238b3 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ures..**.** The 
238b4 69 6e 64 65 78 20 69 73 20 72 65 6d 6f 76 65 64  index is removed
238b5 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
238b6 73 65 20 68 61 73 68 20 74 61 62 6c 65 73 20 62  se hash tables b
238b7 75 74 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74 20  ut.** it is not 
238b8 75 6e 6c 69 6e 6b 65 64 20 66 72 6f 6d 20 74 68  unlinked from th
238b9 65 20 54 61 62 6c 65 20 74 68 61 74 20 69 74 20  e Table that it 
238ba 69 6e 64 65 78 65 73 2e 0a 2a 2a 20 55 6e 6c 69  indexes..** Unli
238bb 6e 6b 69 6e 67 20 66 72 6f 6d 20 74 68 65 20 54  nking from the T
238bc 61 62 6c 65 20 6d 75 73 74 20 62 65 20 64 6f 6e  able must be don
238bd 65 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e 67  e by the calling
238be 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74   function..*/.st
238bf 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65  atic void sqlite
238c0 33 44 65 6c 65 74 65 49 6e 64 65 78 28 49 6e 64  3DeleteIndex(Ind
238c1 65 78 20 2a 70 29 7b 0a 20 20 49 6e 64 65 78 20  ex *p){.  Index 
238c2 2a 70 4f 6c 64 3b 0a 20 20 63 6f 6e 73 74 20 63  *pOld;.  const c
238c3 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 2d 3e  har *zName = p->
238c4 7a 4e 61 6d 65 3b 0a 0a 20 20 70 4f 6c 64 20 3d  zName;..  pOld =
238c5 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65   sqlite3HashInse
238c6 72 74 28 26 70 2d 3e 70 53 63 68 65 6d 61 2d 3e  rt(&p->pSchema->
238c7 69 64 78 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 0a  idxHash, zName,.
238c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
238c9 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
238ca 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65  e3Strlen30(zName
238cb 29 2c 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28  ), 0);.  assert(
238cc 20 70 4f 6c 64 3d 3d 30 20 7c 7c 20 70 4f 6c 64   pOld==0 || pOld
238cd 3d 3d 70 20 29 3b 0a 20 20 66 72 65 65 49 6e 64  ==p );.  freeInd
238ce 65 78 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ex(p);.}../*.** 
238cf 46 6f 72 20 74 68 65 20 69 6e 64 65 78 20 63 61  For the index ca
238d0 6c 6c 65 64 20 7a 49 64 78 4e 61 6d 65 20 77 68  lled zIdxName wh
238d1 69 63 68 20 69 73 20 66 6f 75 6e 64 20 69 6e 20  ich is found in 
238d2 74 68 65 20 64 61 74 61 62 61 73 65 20 69 44 62  the database iDb
238d3 2c 0a 2a 2a 20 75 6e 6c 69 6b 65 20 74 68 61 74  ,.** unlike that
238d4 20 69 6e 64 65 78 20 66 72 6f 6d 20 69 74 73 20   index from its 
238d5 54 61 62 6c 65 20 74 68 65 6e 20 72 65 6d 6f 76  Table then remov
238d6 65 20 74 68 65 20 69 6e 64 65 78 20 66 72 6f 6d  e the index from
238d7 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 68 61  .** the index ha
238d8 73 68 20 74 61 62 6c 65 20 61 6e 64 20 66 72 65  sh table and fre
238d9 65 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 73 74 72  e all memory str
238da 75 63 74 75 72 65 73 20 61 73 73 6f 63 69 61 74  uctures associat
238db 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 69  ed.** with the i
238dc 6e 64 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ndex..*/.SQLITE_
238dd 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
238de 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c  ite3UnlinkAndDel
238df 65 74 65 49 6e 64 65 78 28 73 71 6c 69 74 65 33  eteIndex(sqlite3
238e0 20 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63   *db, int iDb, c
238e1 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 64 78 4e  onst char *zIdxN
238e2 61 6d 65 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70  ame){.  Index *p
238e3 49 6e 64 65 78 3b 0a 20 20 69 6e 74 20 6c 65 6e  Index;.  int len
238e4 3b 0a 20 20 48 61 73 68 20 2a 70 48 61 73 68 20  ;.  Hash *pHash 
238e5 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  = &db->aDb[iDb].
238e6 70 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68  pSchema->idxHash
238e7 3b 0a 0a 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74  ;..  len = sqlit
238e8 65 33 53 74 72 6c 65 6e 33 30 28 7a 49 64 78 4e  e3Strlen30(zIdxN
238e9 61 6d 65 29 3b 0a 20 20 70 49 6e 64 65 78 20 3d  ame);.  pIndex =
238ea 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65   sqlite3HashInse
238eb 72 74 28 70 48 61 73 68 2c 20 7a 49 64 78 4e 61  rt(pHash, zIdxNa
238ec 6d 65 2c 20 6c 65 6e 2c 20 30 29 3b 0a 20 20 69  me, len, 0);.  i
238ed 66 28 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20  f( pIndex ){.   
238ee 20 69 66 28 20 70 49 6e 64 65 78 2d 3e 70 54 61   if( pIndex->pTa
238ef 62 6c 65 2d 3e 70 49 6e 64 65 78 3d 3d 70 49 6e  ble->pIndex==pIn
238f0 64 65 78 20 29 7b 0a 20 20 20 20 20 20 70 49 6e  dex ){.      pIn
238f1 64 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49 6e  dex->pTable->pIn
238f2 64 65 78 20 3d 20 70 49 6e 64 65 78 2d 3e 70 4e  dex = pIndex->pN
238f3 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ext;.    }else{.
238f4 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 3b 0a        Index *p;.
238f5 20 20 20 20 20 20 2f 2a 20 4a 75 73 74 69 66 69        /* Justifi
238f6 63 61 74 69 6f 6e 20 6f 66 20 41 4c 57 41 59 53  cation of ALWAYS
238f7 28 29 3b 20 20 54 68 65 20 69 6e 64 65 78 20 6d  ();  The index m
238f8 75 73 74 20 62 65 20 6f 6e 20 74 68 65 20 6c 69  ust be on the li
238f9 73 74 20 6f 66 0a 20 20 20 20 20 20 2a 2a 20 69  st of.      ** i
238fa 6e 64 69 63 65 73 2e 20 2a 2f 0a 20 20 20 20 20  ndices. */.     
238fb 20 70 20 3d 20 70 49 6e 64 65 78 2d 3e 70 54 61   p = pIndex->pTa
238fc 62 6c 65 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20  ble->pIndex;.   
238fd 20 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53     while( ALWAYS
238fe 28 70 29 20 26 26 20 70 2d 3e 70 4e 65 78 74 21  (p) && p->pNext!
238ff 3d 70 49 6e 64 65 78 20 29 7b 20 70 20 3d 20 70  =pIndex ){ p = p
23900 2d 3e 70 4e 65 78 74 3b 20 7d 0a 20 20 20 20 20  ->pNext; }.     
23901 20 69 66 28 20 41 4c 57 41 59 53 28 70 20 26 26   if( ALWAYS(p &&
23902 20 70 2d 3e 70 4e 65 78 74 3d 3d 70 49 6e 64 65   p->pNext==pInde
23903 78 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d  x) ){.        p-
23904 3e 70 4e 65 78 74 20 3d 20 70 49 6e 64 65 78 2d  >pNext = pIndex-
23905 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a  >pNext;.      }.
23906 20 20 20 20 7d 0a 20 20 20 20 66 72 65 65 49 6e      }.    freeIn
23907 64 65 78 28 70 49 6e 64 65 78 29 3b 0a 20 20 7d  dex(pIndex);.  }
23908 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20  .  db->flags |= 
23909 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61  SQLITE_InternCha
2390a 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  nges;.}../*.** E
2390b 72 61 73 65 20 61 6c 6c 20 73 63 68 65 6d 61 20  rase all schema 
2390c 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d  information from
2390d 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 68   the in-memory h
2390e 61 73 68 20 74 61 62 6c 65 73 20 6f 66 0a 2a 2a  ash tables of.**
2390f 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61   a single databa
23910 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  se.  This routin
23911 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 72  e is called to r
23912 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 0a 2a 2a  eclaim memory.**
23913 20 62 65 66 6f 72 65 20 74 68 65 20 64 61 74 61   before the data
23914 62 61 73 65 20 63 6c 6f 73 65 73 2e 20 20 49 74  base closes.  It
23915 20 69 73 20 61 6c 73 6f 20 63 61 6c 6c 65 64 20   is also called 
23916 64 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62 61 63  during a rollbac
23917 6b 0a 2a 2a 20 69 66 20 74 68 65 72 65 20 77 65  k.** if there we
23918 72 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65  re schema change
23919 73 20 64 75 72 69 6e 67 20 74 68 65 20 74 72 61  s during the tra
2391a 6e 73 61 63 74 69 6f 6e 20 6f 72 20 69 66 20 61  nsaction or if a
2391b 0a 2a 2a 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69  .** schema-cooki
2391c 65 20 6d 69 73 6d 61 74 63 68 20 6f 63 63 75 72  e mismatch occur
2391d 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 44 62 3d  s..**.** If iDb=
2391e 3d 30 20 74 68 65 6e 20 72 65 73 65 74 20 74 68  =0 then reset th
2391f 65 20 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d  e internal schem
23920 61 20 74 61 62 6c 65 73 20 66 6f 72 20 61 6c 6c  a tables for all
23921 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c   database.** fil
23922 65 73 2e 20 20 49 66 20 69 44 62 3e 3d 31 20 74  es.  If iDb>=1 t
23923 68 65 6e 20 72 65 73 65 74 20 74 68 65 20 69 6e  hen reset the in
23924 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 66 6f  ternal schema fo
23925 72 20 6f 6e 6c 79 20 74 68 65 0a 2a 2a 20 73 69  r only the.** si
23926 6e 67 6c 65 20 66 69 6c 65 20 69 6e 64 69 63 61  ngle file indica
23927 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
23928 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
23929 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c  te3ResetInternal
2392a 53 63 68 65 6d 61 28 73 71 6c 69 74 65 33 20 2a  Schema(sqlite3 *
2392b 64 62 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20  db, int iDb){.  
2392c 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 61 73 73 65  int i, j;.  asse
2392d 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44  rt( iDb>=0 && iD
2392e 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20  b<db->nDb );..  
2392f 69 66 28 20 69 44 62 3d 3d 30 20 29 7b 0a 20 20  if( iDb==0 ){.  
23930 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
23931 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 7d 0a  terAll(db);.  }.
23932 20 20 66 6f 72 28 69 3d 69 44 62 3b 20 69 3c 64    for(i=iDb; i<d
23933 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
23934 20 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d    Db *pDb = &db-
23935 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20 69 66 28  >aDb[i];.    if(
23936 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 29 7b   pDb->pSchema ){
23937 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 69 3d  .      assert(i=
23938 3d 31 20 7c 7c 20 28 70 44 62 2d 3e 70 42 74 20  =1 || (pDb->pBt 
23939 26 26 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  && sqlite3BtreeH
2393a 6f 6c 64 73 4d 75 74 65 78 28 70 44 62 2d 3e 70  oldsMutex(pDb->p
2393b 42 74 29 29 29 3b 0a 20 20 20 20 20 20 73 71 6c  Bt)));.      sql
2393c 69 74 65 33 53 63 68 65 6d 61 46 72 65 65 28 70  ite3SchemaFree(p
2393d 44 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  Db->pSchema);.  
2393e 20 20 7d 0a 20 20 20 20 69 66 28 20 69 44 62 3e    }.    if( iDb>
2393f 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a  0 ) return;.  }.
23940 20 20 61 73 73 65 72 74 28 20 69 44 62 3d 3d 30    assert( iDb==0
23941 20 29 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20   );.  db->flags 
23942 26 3d 20 7e 53 51 4c 49 54 45 5f 49 6e 74 65 72  &= ~SQLITE_Inter
23943 6e 43 68 61 6e 67 65 73 3b 0a 20 20 73 71 6c 69  nChanges;.  sqli
23944 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 73  te3VtabUnlockLis
23945 74 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33  t(db);.  sqlite3
23946 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62  BtreeLeaveAll(db
23947 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20  );..  /* If one 
23948 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 61  or more of the a
23949 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61 73  uxiliary databas
2394a 65 20 66 69 6c 65 73 20 68 61 73 20 62 65 65 6e  e files has been
2394b 20 63 6c 6f 73 65 64 2c 0a 20 20 2a 2a 20 74 68   closed,.  ** th
2394c 65 6e 20 72 65 6d 6f 76 65 20 74 68 65 6d 20 66  en remove them f
2394d 72 6f 6d 20 74 68 65 20 61 75 78 69 6c 69 61 72  rom the auxiliar
2394e 79 20 64 61 74 61 62 61 73 65 20 6c 69 73 74 2e  y database list.
2394f 20 20 57 65 20 74 61 6b 65 20 74 68 65 0a 20 20    We take the.  
23950 2a 2a 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74  ** opportunity t
23951 6f 20 64 6f 20 74 68 69 73 20 68 65 72 65 20 73  o do this here s
23952 69 6e 63 65 20 77 65 20 68 61 76 65 20 6a 75 73  ince we have jus
23953 74 20 64 65 6c 65 74 65 64 20 61 6c 6c 20 6f 66  t deleted all of
23954 20 74 68 65 0a 20 20 2a 2a 20 73 63 68 65 6d 61   the.  ** schema
23955 20 68 61 73 68 20 74 61 62 6c 65 73 20 61 6e 64   hash tables and
23956 20 74 68 65 72 65 66 6f 72 65 20 64 6f 20 6e 6f   therefore do no
23957 74 20 68 61 76 65 20 74 6f 20 6d 61 6b 65 20 61  t have to make a
23958 6e 79 20 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20  ny changes.  ** 
23959 74 6f 20 61 6e 79 20 6f 66 20 74 68 6f 73 65 20  to any of those 
2395a 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 66  tables..  */.  f
2395b 6f 72 28 69 3d 6a 3d 32 3b 20 69 3c 64 62 2d 3e  or(i=j=2; i<db->
2395c 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  nDb; i++){.    s
2395d 74 72 75 63 74 20 44 62 20 2a 70 44 62 20 3d 20  truct Db *pDb = 
2395e 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20  &db->aDb[i];.   
2395f 20 69 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30   if( pDb->pBt==0
23960 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
23961 33 44 62 46 72 65 65 28 64 62 2c 20 70 44 62 2d  3DbFree(db, pDb-
23962 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70  >zName);.      p
23963 44 62 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20  Db->zName = 0;. 
23964 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
23965 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6a 3c 69     }.    if( j<i
23966 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 61 44   ){.      db->aD
23967 62 5b 6a 5d 20 3d 20 64 62 2d 3e 61 44 62 5b 69  b[j] = db->aDb[i
23968 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6a 2b 2b  ];.    }.    j++
23969 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26  ;.  }.  memset(&
2396a 64 62 2d 3e 61 44 62 5b 6a 5d 2c 20 30 2c 20 28  db->aDb[j], 0, (
2396b 64 62 2d 3e 6e 44 62 2d 6a 29 2a 73 69 7a 65 6f  db->nDb-j)*sizeo
2396c 66 28 64 62 2d 3e 61 44 62 5b 6a 5d 29 29 3b 0a  f(db->aDb[j]));.
2396d 20 20 64 62 2d 3e 6e 44 62 20 3d 20 6a 3b 0a 20    db->nDb = j;. 
2396e 20 69 66 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20   if( db->nDb<=2 
2396f 26 26 20 64 62 2d 3e 61 44 62 21 3d 64 62 2d 3e  && db->aDb!=db->
23970 61 44 62 53 74 61 74 69 63 20 29 7b 0a 20 20 20  aDbStatic ){.   
23971 20 6d 65 6d 63 70 79 28 64 62 2d 3e 61 44 62 53   memcpy(db->aDbS
23972 74 61 74 69 63 2c 20 64 62 2d 3e 61 44 62 2c 20  tatic, db->aDb, 
23973 32 2a 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62  2*sizeof(db->aDb
23974 5b 30 5d 29 29 3b 0a 20 20 20 20 73 71 6c 69 74  [0]));.    sqlit
23975 65 33 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d  e3DbFree(db, db-
23976 3e 61 44 62 29 3b 0a 20 20 20 20 64 62 2d 3e 61  >aDb);.    db->a
23977 44 62 20 3d 20 64 62 2d 3e 61 44 62 53 74 61 74  Db = db->aDbStat
23978 69 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ic;.  }.}../*.**
23979 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
2397a 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 63   called when a c
2397b 6f 6d 6d 69 74 20 6f 63 63 75 72 73 2e 0a 2a 2f  ommit occurs..*/
2397c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2397d 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d  void sqlite3Comm
2397e 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65  itInternalChange
2397f 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  s(sqlite3 *db){.
23980 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e    db->flags &= ~
23981 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61  SQLITE_InternCha
23982 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  nges;.}../*.** C
23983 6c 65 61 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20  lear the column 
23984 6e 61 6d 65 73 20 66 72 6f 6d 20 61 20 74 61 62  names from a tab
23985 6c 65 20 6f 72 20 76 69 65 77 2e 0a 2a 2f 0a 73  le or view..*/.s
23986 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
23987 65 52 65 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65  eResetColumnName
23988 73 28 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 29  s(Table *pTable)
23989 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 43 6f 6c  {.  int i;.  Col
2398a 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c  umn *pCol;.  sql
2398b 69 74 65 33 20 2a 64 62 20 3d 20 70 54 61 62 6c  ite3 *db = pTabl
2398c 65 2d 3e 64 62 4d 65 6d 3b 0a 20 20 74 65 73 74  e->dbMem;.  test
2398d 63 61 73 65 28 20 64 62 3d 3d 30 20 29 3b 0a 20  case( db==0 );. 
2398e 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 21   assert( pTable!
2398f 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43 6f  =0 );.  if( (pCo
23990 6c 20 3d 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c  l = pTable->aCol
23991 29 21 3d 30 20 29 7b 0a 20 20 20 20 66 6f 72 28  )!=0 ){.    for(
23992 69 3d 30 3b 20 69 3c 70 54 61 62 6c 65 2d 3e 6e  i=0; i<pTable->n
23993 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b  Col; i++, pCol++
23994 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
23995 44 62 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d  DbFree(db, pCol-
23996 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 73  >zName);.      s
23997 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
23998 28 64 62 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74  (db, pCol->pDflt
23999 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2399a 44 62 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d  DbFree(db, pCol-
2399b 3e 7a 44 66 6c 74 29 3b 0a 20 20 20 20 20 20 73  >zDflt);.      s
2399c 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2399d 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b 0a 20   pCol->zType);. 
2399e 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
2399f 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a 43 6f  ee(db, pCol->zCo
239a0 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ll);.    }.    s
239a1 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
239a2 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 29 3b 0a   pTable->aCol);.
239a3 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e 61 43    }.  pTable->aC
239a4 6f 6c 20 3d 20 30 3b 0a 20 20 70 54 61 62 6c 65  ol = 0;.  pTable
239a5 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 7d 0a 0a 2f  ->nCol = 0;.}../
239a6 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65 20  *.** Remove the 
239a7 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 74 72 75  memory data stru
239a8 63 74 75 72 65 73 20 61 73 73 6f 63 69 61 74 65  ctures associate
239a9 64 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e  d with the given
239aa 0a 2a 2a 20 54 61 62 6c 65 2e 20 20 4e 6f 20 63  .** Table.  No c
239ab 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20  hanges are made 
239ac 74 6f 20 64 69 73 6b 20 62 79 20 74 68 69 73 20  to disk by this 
239ad 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54  routine..**.** T
239ae 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 73 74  his routine just
239af 20 64 65 6c 65 74 65 73 20 74 68 65 20 64 61 74   deletes the dat
239b0 61 20 73 74 72 75 63 74 75 72 65 2e 20 20 49 74  a structure.  It
239b1 20 64 6f 65 73 20 6e 6f 74 20 75 6e 6c 69 6e 6b   does not unlink
239b2 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20 64 61  .** the table da
239b3 74 61 20 73 74 72 75 63 74 75 72 65 20 66 72 6f  ta structure fro
239b4 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  m the hash table
239b5 2e 20 20 42 75 74 20 69 74 20 64 6f 65 73 20 64  .  But it does d
239b6 65 73 74 72 6f 79 0a 2a 2a 20 6d 65 6d 6f 72 79  estroy.** memory
239b7 20 73 74 72 75 63 74 75 72 65 73 20 6f 66 20 74   structures of t
239b8 68 65 20 69 6e 64 69 63 65 73 20 61 6e 64 20 66  he indices and f
239b9 6f 72 65 69 67 6e 20 6b 65 79 73 20 61 73 73 6f  oreign keys asso
239ba 63 69 61 74 65 64 20 77 69 74 68 20 0a 2a 2a 20  ciated with .** 
239bb 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51  the table..*/.SQ
239bc 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
239bd 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54  d sqlite3DeleteT
239be 61 62 6c 65 28 54 61 62 6c 65 20 2a 70 54 61 62  able(Table *pTab
239bf 6c 65 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49  le){.  Index *pI
239c0 6e 64 65 78 2c 20 2a 70 4e 65 78 74 3b 0a 20 20  ndex, *pNext;.  
239c1 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20  sqlite3 *db;..  
239c2 69 66 28 20 70 54 61 62 6c 65 3d 3d 30 20 29 20  if( pTable==0 ) 
239c3 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d 20 70  return;.  db = p
239c4 54 61 62 6c 65 2d 3e 64 62 4d 65 6d 3b 0a 20 20  Table->dbMem;.  
239c5 74 65 73 74 63 61 73 65 28 20 64 62 3d 3d 30 20  testcase( db==0 
239c6 29 3b 0a 0a 20 20 2f 2a 20 44 6f 20 6e 6f 74 20  );..  /* Do not 
239c7 64 65 6c 65 74 65 20 74 68 65 20 74 61 62 6c 65  delete the table
239c8 20 75 6e 74 69 6c 20 74 68 65 20 72 65 66 65 72   until the refer
239c9 65 6e 63 65 20 63 6f 75 6e 74 20 72 65 61 63 68  ence count reach
239ca 65 73 20 7a 65 72 6f 2e 20 2a 2f 0a 20 20 70 54  es zero. */.  pT
239cb 61 62 6c 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20  able->nRef--;.  
239cc 69 66 28 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66  if( pTable->nRef
239cd 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  >0 ){.    return
239ce 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
239cf 70 54 61 62 6c 65 2d 3e 6e 52 65 66 3d 3d 30 20  pTable->nRef==0 
239d0 29 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20  );..  /* Delete 
239d1 61 6c 6c 20 69 6e 64 69 63 65 73 20 61 73 73 6f  all indices asso
239d2 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ciated with this
239d3 20 74 61 62 6c 65 0a 20 20 2a 2f 0a 20 20 66 6f   table.  */.  fo
239d4 72 28 70 49 6e 64 65 78 20 3d 20 70 54 61 62 6c  r(pIndex = pTabl
239d5 65 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e 64 65  e->pIndex; pInde
239d6 78 3b 20 70 49 6e 64 65 78 3d 70 4e 65 78 74 29  x; pIndex=pNext)
239d7 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70 49  {.    pNext = pI
239d8 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  ndex->pNext;.   
239d9 20 61 73 73 65 72 74 28 20 70 49 6e 64 65 78 2d   assert( pIndex-
239da 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 6c 65  >pSchema==pTable
239db 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20  ->pSchema );.   
239dc 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 49 6e   sqlite3DeleteIn
239dd 64 65 78 28 70 49 6e 64 65 78 29 3b 0a 20 20 7d  dex(pIndex);.  }
239de 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 6e  ..  /* Delete an
239df 79 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61  y foreign keys a
239e0 74 74 61 63 68 65 64 20 74 6f 20 74 68 69 73 20  ttached to this 
239e1 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 73 71 6c 69  table. */.  sqli
239e2 74 65 33 46 6b 44 65 6c 65 74 65 28 70 54 61 62  te3FkDelete(pTab
239e3 6c 65 29 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74  le);..  /* Delet
239e4 65 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75  e the Table stru
239e5 63 74 75 72 65 20 69 74 73 65 6c 66 2e 0a 20 20  cture itself..  
239e6 2a 2f 0a 20 20 73 71 6c 69 74 65 52 65 73 65 74  */.  sqliteReset
239e7 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 54 61 62  ColumnNames(pTab
239e8 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  le);.  sqlite3Db
239e9 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d  Free(db, pTable-
239ea 3e 7a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74  >zName);.  sqlit
239eb 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 61  e3DbFree(db, pTa
239ec 62 6c 65 2d 3e 7a 43 6f 6c 41 66 66 29 3b 0a 20  ble->zColAff);. 
239ed 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
239ee 6c 65 74 65 28 64 62 2c 20 70 54 61 62 6c 65 2d  lete(db, pTable-
239ef 3e 70 53 65 6c 65 63 74 29 3b 0a 23 69 66 6e 64  >pSelect);.#ifnd
239f0 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
239f1 48 45 43 4b 0a 20 20 73 71 6c 69 74 65 33 45 78  HECK.  sqlite3Ex
239f2 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61  prDelete(db, pTa
239f3 62 6c 65 2d 3e 70 43 68 65 63 6b 29 3b 0a 23 65  ble->pCheck);.#e
239f4 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 56 74  ndif.  sqlite3Vt
239f5 61 62 43 6c 65 61 72 28 70 54 61 62 6c 65 29 3b  abClear(pTable);
239f6 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
239f7 28 64 62 2c 20 70 54 61 62 6c 65 29 3b 0a 7d 0a  (db, pTable);.}.
239f8 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68  ./*.** Unlink th
239f9 65 20 67 69 76 65 6e 20 74 61 62 6c 65 20 66 72  e given table fr
239fa 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  om the hash tabl
239fb 65 73 20 61 6e 64 20 74 68 65 20 64 65 6c 65 74  es and the delet
239fc 65 20 74 68 65 0a 2a 2a 20 74 61 62 6c 65 20 73  e the.** table s
239fd 74 72 75 63 74 75 72 65 20 77 69 74 68 20 61 6c  tructure with al
239fe 6c 20 69 74 73 20 69 6e 64 69 63 65 73 20 61 6e  l its indices an
239ff 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 2e 0a  d foreign keys..
23a00 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
23a01 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e  E void sqlite3Un
23a02 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 61 62  linkAndDeleteTab
23a03 6c 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  le(sqlite3 *db, 
23a04 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63  int iDb, const c
23a05 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 29 7b 0a  har *zTabName){.
23a06 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 44 62    Table *p;.  Db
23a07 20 2a 70 44 62 3b 0a 0a 20 20 61 73 73 65 72 74   *pDb;..  assert
23a08 28 20 64 62 21 3d 30 20 29 3b 0a 20 20 61 73 73  ( db!=0 );.  ass
23a09 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69  ert( iDb>=0 && i
23a0a 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  Db<db->nDb );.  
23a0b 61 73 73 65 72 74 28 20 7a 54 61 62 4e 61 6d 65  assert( zTabName
23a0c 20 26 26 20 7a 54 61 62 4e 61 6d 65 5b 30 5d 20   && zTabName[0] 
23a0d 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e  );.  pDb = &db->
23a0e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 70 20 3d 20  aDb[iDb];.  p = 
23a0f 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72  sqlite3HashInser
23a10 74 28 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d  t(&pDb->pSchema-
23a11 3e 74 62 6c 48 61 73 68 2c 20 7a 54 61 62 4e 61  >tblHash, zTabNa
23a12 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  me,.            
23a13 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
23a14 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 54 61 62  te3Strlen30(zTab
23a15 4e 61 6d 65 29 2c 30 29 3b 0a 20 20 73 71 6c 69  Name),0);.  sqli
23a16 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70  te3DeleteTable(p
23a17 29 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c  );.  db->flags |
23a18 3d 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43  = SQLITE_InternC
23a19 68 61 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  hanges;.}../*.**
23a1a 20 47 69 76 65 6e 20 61 20 74 6f 6b 65 6e 2c 20   Given a token, 
23a1b 72 65 74 75 72 6e 20 61 20 73 74 72 69 6e 67 20  return a string 
23a1c 74 68 61 74 20 63 6f 6e 73 69 73 74 73 20 6f 66  that consists of
23a1d 20 74 68 65 20 74 65 78 74 20 6f 66 20 74 68 61   the text of tha
23a1e 74 0a 2a 2a 20 74 6f 6b 65 6e 2e 20 20 53 70 61  t.** token.  Spa
23a1f 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ce to hold the r
23a20 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 0a 2a  eturned string.*
23a21 2a 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72  * is obtained fr
23a22 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  om sqliteMalloc(
23a23 29 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72  ) and must be fr
23a24 65 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69  eed by the calli
23a25 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a  ng.** function..
23a26 2a 2a 0a 2a 2a 20 41 6e 79 20 71 75 6f 74 61 74  **.** Any quotat
23a27 69 6f 6e 20 6d 61 72 6b 73 20 28 65 78 3a 20 20  ion marks (ex:  
23a28 22 6e 61 6d 65 22 2c 20 27 6e 61 6d 65 27 2c 20  "name", 'name', 
23a29 5b 6e 61 6d 65 5d 2c 20 6f 72 20 60 6e 61 6d 65  [name], or `name
23a2a 60 29 20 74 68 61 74 0a 2a 2a 20 73 75 72 72 6f  `) that.** surro
23a2b 75 6e 64 20 74 68 65 20 62 6f 64 79 20 6f 66 20  und the body of 
23a2c 74 68 65 20 74 6f 6b 65 6e 20 61 72 65 20 72 65  the token are re
23a2d 6d 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 6f 6b  moved..**.** Tok
23a2e 65 6e 73 20 61 72 65 20 6f 66 74 65 6e 20 6a 75  ens are often ju
23a2f 73 74 20 70 6f 69 6e 74 65 72 73 20 69 6e 74 6f  st pointers into
23a30 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51   the original SQ
23a31 4c 20 74 65 78 74 20 61 6e 64 20 73 6f 0a 2a 2a  L text and so.**
23a32 20 61 72 65 20 6e 6f 74 20 5c 30 30 30 20 74 65   are not \000 te
23a33 72 6d 69 6e 61 74 65 64 20 61 6e 64 20 61 72 65  rminated and are
23a34 20 6e 6f 74 20 70 65 72 73 69 73 74 65 6e 74 2e   not persistent.
23a35 20 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73    The returned s
23a36 74 72 69 6e 67 0a 2a 2a 20 69 73 20 5c 30 30 30  tring.** is \000
23a37 20 74 65 72 6d 69 6e 61 74 65 64 20 61 6e 64 20   terminated and 
23a38 69 73 20 70 65 72 73 69 73 74 65 6e 74 2e 0a 2a  is persistent..*
23a39 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
23a3a 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4e 61   char *sqlite3Na
23a3b 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69  meFromToken(sqli
23a3c 74 65 33 20 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a  te3 *db, Token *
23a3d 70 4e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a  pName){.  char *
23a3e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 70 4e 61  zName;.  if( pNa
23a3f 6d 65 20 29 7b 0a 20 20 20 20 7a 4e 61 6d 65 20  me ){.    zName 
23a40 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44  = sqlite3DbStrND
23a41 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70 4e  up(db, (char*)pN
23a42 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e  ame->z, pName->n
23a43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  );.    sqlite3De
23a44 71 75 6f 74 65 28 7a 4e 61 6d 65 29 3b 0a 20 20  quote(zName);.  
23a45 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 4e 61 6d 65  }else{.    zName
23a46 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
23a47 72 6e 20 7a 4e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a  rn zName;.}../*.
23a48 2a 2a 20 4f 70 65 6e 20 74 68 65 20 73 71 6c 69  ** Open the sqli
23a49 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20  te_master table 
23a4a 73 74 6f 72 65 64 20 69 6e 20 64 61 74 61 62 61  stored in databa
23a4b 73 65 20 6e 75 6d 62 65 72 20 69 44 62 20 66 6f  se number iDb fo
23a4c 72 0a 2a 2a 20 77 72 69 74 69 6e 67 2e 20 54 68  r.** writing. Th
23a4d 65 20 74 61 62 6c 65 20 69 73 20 6f 70 65 6e 65  e table is opene
23a4e 64 20 75 73 69 6e 67 20 63 75 72 73 6f 72 20 30  d using cursor 0
23a4f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
23a50 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
23a51 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 6c 65 28  OpenMasterTable(
23a52 50 61 72 73 65 20 2a 70 2c 20 69 6e 74 20 69 44  Parse *p, int iD
23a53 62 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  b){.  Vdbe *v = 
23a54 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
23a55 29 3b 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c  );.  sqlite3Tabl
23a56 65 4c 6f 63 6b 28 70 2c 20 69 44 62 2c 20 4d 41  eLock(p, iDb, MA
23a57 53 54 45 52 5f 52 4f 4f 54 2c 20 31 2c 20 53 43  STER_ROOT, 1, SC
23a58 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 29  HEMA_TABLE(iDb))
23a59 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
23a5a 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp3(v, OP_Open
23a5b 57 72 69 74 65 2c 20 30 2c 20 4d 41 53 54 45 52  Write, 0, MASTER
23a5c 5f 52 4f 4f 54 2c 20 69 44 62 29 3b 0a 20 20 73  _ROOT, iDb);.  s
23a5d 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
23a5e 50 34 28 76 2c 20 2d 31 2c 20 28 63 68 61 72 20  P4(v, -1, (char 
23a5f 2a 29 35 2c 20 50 34 5f 49 4e 54 33 32 29 3b 20  *)5, P4_INT32); 
23a60 20 2f 2a 20 35 20 63 6f 6c 75 6d 6e 20 74 61 62   /* 5 column tab
23a61 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 6e  le */.  if( p->n
23a62 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d  Tab==0 ){.    p-
23a63 3e 6e 54 61 62 20 3d 20 31 3b 0a 20 20 7d 0a 7d  >nTab = 1;.  }.}
23a64 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74 65  ../*.** Paramete
23a65 72 20 7a 4e 61 6d 65 20 70 6f 69 6e 74 73 20 74  r zName points t
23a66 6f 20 61 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74  o a nul-terminat
23a67 65 64 20 62 75 66 66 65 72 20 63 6f 6e 74 61 69  ed buffer contai
23a68 6e 69 6e 67 20 74 68 65 20 6e 61 6d 65 0a 2a 2a  ning the name.**
23a69 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 28   of a database (
23a6a 22 6d 61 69 6e 22 2c 20 22 74 65 6d 70 22 20 6f  "main", "temp" o
23a6b 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e  r the name of an
23a6c 20 61 74 74 61 63 68 65 64 20 64 62 29 2e 20 54   attached db). T
23a6d 68 69 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20  his.** function 
23a6e 72 65 74 75 72 6e 73 20 74 68 65 20 69 6e 64 65  returns the inde
23a6f 78 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 64  x of the named d
23a70 61 74 61 62 61 73 65 20 69 6e 20 64 62 2d 3e 61  atabase in db->a
23a71 44 62 5b 5d 2c 20 6f 72 0a 2a 2a 20 2d 31 20 69  Db[], or.** -1 i
23a72 66 20 74 68 65 20 6e 61 6d 65 64 20 64 62 20 63  f the named db c
23a73 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a  annot be found..
23a74 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
23a75 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 6e  E int sqlite3Fin
23a76 64 44 62 4e 61 6d 65 28 73 71 6c 69 74 65 33 20  dDbName(sqlite3 
23a77 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *db, const char 
23a78 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 6e 74 20 69  *zName){.  int i
23a79 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 2f   = -1;         /
23a7a 2a 20 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65  * Database numbe
23a7b 72 20 2a 2f 0a 20 20 69 66 28 20 7a 4e 61 6d 65  r */.  if( zName
23a7c 20 29 7b 0a 20 20 20 20 44 62 20 2a 70 44 62 3b   ){.    Db *pDb;
23a7d 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c  .    int n = sql
23a7e 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61  ite3Strlen30(zNa
23a7f 6d 65 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 28  me);.    for(i=(
23a80 64 62 2d 3e 6e 44 62 2d 31 29 2c 20 70 44 62 3d  db->nDb-1), pDb=
23a81 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 20 69 3e 3d  &db->aDb[i]; i>=
23a82 30 3b 20 69 2d 2d 2c 20 70 44 62 2d 2d 29 7b 0a  0; i--, pDb--){.
23a83 20 20 20 20 20 20 69 66 28 20 28 21 4f 4d 49 54        if( (!OMIT
23a84 5f 54 45 4d 50 44 42 20 7c 7c 20 69 21 3d 31 20  _TEMPDB || i!=1 
23a85 29 20 26 26 20 6e 3d 3d 73 71 6c 69 74 65 33 53  ) && n==sqlite3S
23a86 74 72 6c 65 6e 33 30 28 70 44 62 2d 3e 7a 4e 61  trlen30(pDb->zNa
23a87 6d 65 29 20 26 26 20 0a 20 20 20 20 20 20 20 20  me) && .        
23a88 20 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49    0==sqlite3StrI
23a89 43 6d 70 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20  Cmp(pDb->zName, 
23a8a 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20  zName) ){.      
23a8b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
23a8c 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
23a8d 75 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn i;.}../*.** 
23a8e 54 68 65 20 74 6f 6b 65 6e 20 2a 70 4e 61 6d 65  The token *pName
23a8f 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 61   contains the na
23a90 6d 65 20 6f 66 20 61 20 64 61 74 61 62 61 73 65  me of a database
23a91 20 28 65 69 74 68 65 72 20 22 6d 61 69 6e 22 20   (either "main" 
23a92 6f 72 0a 2a 2a 20 22 74 65 6d 70 22 20 6f 72 20  or.** "temp" or 
23a93 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 20 61  the name of an a
23a94 74 74 61 63 68 65 64 20 64 62 29 2e 20 54 68 69  ttached db). Thi
23a95 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
23a96 73 20 74 68 65 0a 2a 2a 20 69 6e 64 65 78 20 6f  s the.** index o
23a97 66 20 74 68 65 20 6e 61 6d 65 64 20 64 61 74 61  f the named data
23a98 62 61 73 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b  base in db->aDb[
23a99 5d 2c 20 6f 72 20 2d 31 20 69 66 20 74 68 65 20  ], or -1 if the 
23a9a 6e 61 6d 65 64 20 64 62 20 0a 2a 2a 20 64 6f 65  named db .** doe
23a9b 73 20 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2f 0a  s not exist..*/.
23a9c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
23a9d 6e 74 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62  nt sqlite3FindDb
23a9e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 6f  (sqlite3 *db, To
23a9f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b 0a 20 20 69  ken *pName){.  i
23aa0 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
23aa1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23aa2 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
23aa3 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 63 68 61 72  number */.  char
23aa4 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20   *zName;        
23aa5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23aa6 20 2f 2a 20 4e 61 6d 65 20 77 65 20 61 72 65 20   /* Name we are 
23aa7 73 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f  searching for */
23aa8 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  .  zName = sqlit
23aa9 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
23aaa 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 20  db, pName);.  i 
23aab 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 4e  = sqlite3FindDbN
23aac 61 6d 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a  ame(db, zName);.
23aad 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
23aae 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65  db, zName);.  re
23aaf 74 75 72 6e 20 69 3b 0a 7d 0a 0a 2f 2a 20 54 68  turn i;.}../* Th
23ab0 65 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20  e table or view 
23ab1 6f 72 20 74 72 69 67 67 65 72 20 6e 61 6d 65 20  or trigger name 
23ab2 69 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 69  is passed to thi
23ab3 73 20 72 6f 75 74 69 6e 65 20 76 69 61 20 74 6f  s routine via to
23ab4 6b 65 6e 73 0a 2a 2a 20 70 4e 61 6d 65 31 20 61  kens.** pName1 a
23ab5 6e 64 20 70 4e 61 6d 65 32 2e 20 49 66 20 74 68  nd pName2. If th
23ab6 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 77 61 73  e table name was
23ab7 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64   fully qualified
23ab8 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a  , for example:.*
23ab9 2a 0a 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c  *.** CREATE TABL
23aba 45 20 78 78 78 2e 79 79 79 20 28 2e 2e 2e 29 3b  E xxx.yyy (...);
23abb 0a 2a 2a 20 0a 2a 2a 20 54 68 65 6e 20 70 4e 61  .** .** Then pNa
23abc 6d 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 78  me1 is set to "x
23abd 78 78 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 22  xx" and pName2 "
23abe 79 79 79 22 2e 20 4f 6e 20 74 68 65 20 6f 74 68  yyy". On the oth
23abf 65 72 20 68 61 6e 64 20 69 66 0a 2a 2a 20 74 68  er hand if.** th
23ac0 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73 20  e table name is 
23ac1 6e 6f 74 20 66 75 6c 6c 79 20 71 75 61 6c 69 66  not fully qualif
23ac2 69 65 64 2c 20 69 2e 65 2e 3a 0a 2a 2a 0a 2a 2a  ied, i.e.:.**.**
23ac3 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 79 79   CREATE TABLE yy
23ac4 79 28 2e 2e 2e 29 3b 0a 2a 2a 0a 2a 2a 20 54 68  y(...);.**.** Th
23ac5 65 6e 20 70 4e 61 6d 65 31 20 69 73 20 73 65 74  en pName1 is set
23ac6 20 74 6f 20 22 79 79 79 22 20 61 6e 64 20 70 4e   to "yyy" and pN
23ac7 61 6d 65 32 20 69 73 20 22 22 2e 0a 2a 2a 0a 2a  ame2 is ""..**.*
23ac8 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
23ac9 65 74 73 20 74 68 65 20 2a 70 70 55 6e 71 75 61  ets the *ppUnqua
23aca 6c 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 6f 69  l pointer to poi
23acb 6e 74 20 61 74 20 74 68 65 20 74 6f 6b 65 6e 20  nt at the token 
23acc 28 70 4e 61 6d 65 31 20 6f 72 0a 2a 2a 20 70 4e  (pName1 or.** pN
23acd 61 6d 65 32 29 20 74 68 61 74 20 73 74 6f 72 65  ame2) that store
23ace 73 20 74 68 65 20 75 6e 71 75 61 6c 69 66 69 65  s the unqualifie
23acf 64 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 20 54  d table name.  T
23ad0 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 0a  he index of the.
23ad1 2a 2a 20 64 61 74 61 62 61 73 65 20 22 78 78 78  ** database "xxx
23ad2 22 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  " is returned..*
23ad3 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
23ad4 20 69 6e 74 20 73 71 6c 69 74 65 33 54 77 6f 50   int sqlite3TwoP
23ad5 61 72 74 4e 61 6d 65 28 0a 20 20 50 61 72 73 65  artName(.  Parse
23ad6 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f   *pParse,      /
23ad7 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f  * Parsing and co
23ad8 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  de generating co
23ad9 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ntext */.  Token
23ada 20 2a 70 4e 61 6d 65 31 2c 20 20 20 20 20 20 2f   *pName1,      /
23adb 2a 20 54 68 65 20 22 78 78 78 22 20 69 6e 20 74  * The "xxx" in t
23adc 68 65 20 6e 61 6d 65 20 22 78 78 78 2e 79 79 79  he name "xxx.yyy
23add 22 20 6f 72 20 22 78 78 78 22 20 2a 2f 0a 20 20  " or "xxx" */.  
23ade 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20  Token *pName2,  
23adf 20 20 20 20 2f 2a 20 54 68 65 20 22 79 79 79 22      /* The "yyy"
23ae0 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 22 78 78   in the name "xx
23ae1 78 2e 79 79 79 22 20 2a 2f 0a 20 20 54 6f 6b 65  x.yyy" */.  Toke
23ae2 6e 20 2a 2a 70 55 6e 71 75 61 6c 20 20 20 20 20  n **pUnqual     
23ae3 2f 2a 20 57 72 69 74 65 20 74 68 65 20 75 6e 71  /* Write the unq
23ae4 75 61 6c 69 66 69 65 64 20 6f 62 6a 65 63 74 20  ualified object 
23ae5 6e 61 6d 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  name here */.){.
23ae6 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
23ae7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
23ae8 20 44 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e   Database holdin
23ae9 67 20 74 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a  g the object */.
23aea 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
23aeb 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 69  pParse->db;..  i
23aec 66 28 20 41 4c 57 41 59 53 28 70 4e 61 6d 65 32  f( ALWAYS(pName2
23aed 21 3d 30 29 20 26 26 20 70 4e 61 6d 65 32 2d 3e  !=0) && pName2->
23aee 6e 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 64  n>0 ){.    if( d
23aef 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 20 7b  b->init.busy ) {
23af0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
23af1 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
23af2 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65  corrupt database
23af3 22 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65  ");.      pParse
23af4 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20  ->nErr++;.      
23af5 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d  return -1;.    }
23af6 0a 20 20 20 20 2a 70 55 6e 71 75 61 6c 20 3d 20  .    *pUnqual = 
23af7 70 4e 61 6d 65 32 3b 0a 20 20 20 20 69 44 62 20  pName2;.    iDb 
23af8 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28  = sqlite3FindDb(
23af9 64 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20  db, pName1);.   
23afa 20 69 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 20   if( iDb<0 ){.  
23afb 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
23afc 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e 6b  Msg(pParse, "unk
23afd 6e 6f 77 6e 20 64 61 74 61 62 61 73 65 20 25 54  nown database %T
23afe 22 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20  ", pName1);.    
23aff 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b    pParse->nErr++
23b00 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d  ;.      return -
23b01 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  1;.    }.  }else
23b02 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62  {.    assert( db
23b03 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d 30 20 7c 7c  ->init.iDb==0 ||
23b04 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29   db->init.busy )
23b05 3b 0a 20 20 20 20 69 44 62 20 3d 20 64 62 2d 3e  ;.    iDb = db->
23b06 69 6e 69 74 2e 69 44 62 3b 0a 20 20 20 20 2a 70  init.iDb;.    *p
23b07 55 6e 71 75 61 6c 20 3d 20 70 4e 61 6d 65 31 3b  Unqual = pName1;
23b08 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 44  .  }.  return iD
23b09 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  b;.}../*.** This
23b0a 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
23b0b 20 74 6f 20 63 68 65 63 6b 20 69 66 20 74 68 65   to check if the
23b0c 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 7a 4e   UTF-8 string zN
23b0d 61 6d 65 20 69 73 20 61 20 6c 65 67 61 6c 0a 2a  ame is a legal.*
23b0e 2a 20 75 6e 71 75 61 6c 69 66 69 65 64 20 6e 61  * unqualified na
23b0f 6d 65 20 66 6f 72 20 61 20 6e 65 77 20 73 63 68  me for a new sch
23b10 65 6d 61 20 6f 62 6a 65 63 74 20 28 74 61 62 6c  ema object (tabl
23b11 65 2c 20 69 6e 64 65 78 2c 20 76 69 65 77 20 6f  e, index, view o
23b12 72 0a 2a 2a 20 74 72 69 67 67 65 72 29 2e 20 41  r.** trigger). A
23b13 6c 6c 20 6e 61 6d 65 73 20 61 72 65 20 6c 65 67  ll names are leg
23b14 61 6c 20 65 78 63 65 70 74 20 74 68 6f 73 65 20  al except those 
23b15 74 68 61 74 20 62 65 67 69 6e 20 77 69 74 68 20  that begin with 
23b16 74 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 22 73  the string.** "s
23b17 71 6c 69 74 65 5f 22 20 28 69 6e 20 75 70 70 65  qlite_" (in uppe
23b18 72 2c 20 6c 6f 77 65 72 20 6f 72 20 6d 69 78 65  r, lower or mixe
23b19 64 20 63 61 73 65 29 2e 20 54 68 69 73 20 70 6f  d case). This po
23b1a 72 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 61 6d  rtion of the nam
23b1b 65 73 70 61 63 65 0a 2a 2a 20 69 73 20 72 65 73  espace.** is res
23b1c 65 72 76 65 64 20 66 6f 72 20 69 6e 74 65 72 6e  erved for intern
23b1d 61 6c 20 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  al use..*/.SQLIT
23b1e 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
23b1f 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74  lite3CheckObject
23b20 4e 61 6d 65 28 50 61 72 73 65 20 2a 70 50 61 72  Name(Parse *pPar
23b21 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  se, const char *
23b22 7a 4e 61 6d 65 29 7b 0a 20 20 69 66 28 20 21 70  zName){.  if( !p
23b23 50 61 72 73 65 2d 3e 64 62 2d 3e 69 6e 69 74 2e  Parse->db->init.
23b24 62 75 73 79 20 26 26 20 70 50 61 72 73 65 2d 3e  busy && pParse->
23b25 6e 65 73 74 65 64 3d 3d 30 20 0a 20 20 20 20 20  nested==0 .     
23b26 20 20 20 20 20 26 26 20 28 70 50 61 72 73 65 2d       && (pParse-
23b27 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  >db->flags & SQL
23b28 49 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61 29  ITE_WriteSchema)
23b29 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 26 26  ==0.          &&
23b2a 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 4e 49   0==sqlite3StrNI
23b2b 43 6d 70 28 7a 4e 61 6d 65 2c 20 22 73 71 6c 69  Cmp(zName, "sqli
23b2c 74 65 5f 22 2c 20 37 29 20 29 7b 0a 20 20 20 20  te_", 7) ){.    
23b2d 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
23b2e 70 50 61 72 73 65 2c 20 22 6f 62 6a 65 63 74 20  pParse, "object 
23b2f 6e 61 6d 65 20 72 65 73 65 72 76 65 64 20 66 6f  name reserved fo
23b30 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 3a 20  r internal use: 
23b31 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  %s", zName);.   
23b32 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
23b33 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75  RROR;.  }.  retu
23b34 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
23b35 0a 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 63 6f 6e  ./*.** Begin con
23b36 73 74 72 75 63 74 69 6e 67 20 61 20 6e 65 77 20  structing a new 
23b37 74 61 62 6c 65 20 72 65 70 72 65 73 65 6e 74 61  table representa
23b38 74 69 6f 6e 20 69 6e 20 6d 65 6d 6f 72 79 2e 20  tion in memory. 
23b39 20 54 68 69 73 20 69 73 0a 2a 2a 20 74 68 65 20   This is.** the 
23b3a 66 69 72 73 74 20 6f 66 20 73 65 76 65 72 61 6c  first of several
23b3b 20 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73   action routines
23b3c 20 74 68 61 74 20 67 65 74 20 63 61 6c 6c 65 64   that get called
23b3d 20 69 6e 20 72 65 73 70 6f 6e 73 65 0a 2a 2a 20   in response.** 
23b3e 74 6f 20 61 20 43 52 45 41 54 45 20 54 41 42 4c  to a CREATE TABL
23b3f 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e  E statement.  In
23b40 20 70 61 72 74 69 63 75 6c 61 72 2c 20 74 68 69   particular, thi
23b41 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
23b42 6c 65 64 0a 2a 2a 20 61 66 74 65 72 20 73 65 65  led.** after see
23b43 69 6e 67 20 74 6f 6b 65 6e 73 20 22 43 52 45 41  ing tokens "CREA
23b44 54 45 22 20 61 6e 64 20 22 54 41 42 4c 45 22 20  TE" and "TABLE" 
23b45 61 6e 64 20 74 68 65 20 74 61 62 6c 65 20 6e 61  and the table na
23b46 6d 65 2e 20 54 68 65 20 69 73 54 65 6d 70 0a 2a  me. The isTemp.*
23b47 2a 20 66 6c 61 67 20 69 73 20 74 72 75 65 20 69  * flag is true i
23b48 66 20 74 68 65 20 74 61 62 6c 65 20 73 68 6f 75  f the table shou
23b49 6c 64 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20  ld be stored in 
23b4a 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 61  the auxiliary da
23b4b 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69  tabase.** file i
23b4c 6e 73 74 65 61 64 20 6f 66 20 69 6e 20 74 68 65  nstead of in the
23b4d 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
23b4e 69 6c 65 2e 20 20 54 68 69 73 20 69 73 20 6e 6f  ile.  This is no
23b4f 72 6d 61 6c 6c 79 20 74 68 65 20 63 61 73 65 0a  rmally the case.
23b50 2a 2a 20 77 68 65 6e 20 74 68 65 20 22 54 45 4d  ** when the "TEM
23b51 50 22 20 6f 72 20 22 54 45 4d 50 4f 52 41 52 59  P" or "TEMPORARY
23b52 22 20 6b 65 79 77 6f 72 64 20 6f 63 63 75 72 73  " keyword occurs
23b53 20 69 6e 20 62 65 74 77 65 65 6e 0a 2a 2a 20 43   in between.** C
23b54 52 45 41 54 45 20 61 6e 64 20 54 41 42 4c 45 2e  REATE and TABLE.
23b55 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 77 20 74  .**.** The new t
23b56 61 62 6c 65 20 72 65 63 6f 72 64 20 69 73 20 69  able record is i
23b57 6e 69 74 69 61 6c 69 7a 65 64 20 61 6e 64 20 70  nitialized and p
23b58 75 74 20 69 6e 20 70 50 61 72 73 65 2d 3e 70 4e  ut in pParse->pN
23b59 65 77 54 61 62 6c 65 2e 0a 2a 2a 20 41 73 20 6d  ewTable..** As m
23b5a 6f 72 65 20 6f 66 20 74 68 65 20 43 52 45 41 54  ore of the CREAT
23b5b 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  E TABLE statemen
23b5c 74 20 69 73 20 70 61 72 73 65 64 2c 20 61 64 64  t is parsed, add
23b5d 69 74 69 6f 6e 61 6c 20 61 63 74 69 6f 6e 0a 2a  itional action.*
23b5e 2a 20 72 6f 75 74 69 6e 65 73 20 77 69 6c 6c 20  * routines will 
23b5f 62 65 20 63 61 6c 6c 65 64 20 74 6f 20 61 64 64  be called to add
23b60 20 6d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f   more informatio
23b61 6e 20 74 6f 20 74 68 69 73 20 72 65 63 6f 72 64  n to this record
23b62 2e 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e 64 20  ..** At the end 
23b63 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 54 41  of the CREATE TA
23b64 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2c 20 74  BLE statement, t
23b65 68 65 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62  he sqlite3EndTab
23b66 6c 65 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  le() routine.** 
23b67 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d  is called to com
23b68 70 6c 65 74 65 20 74 68 65 20 63 6f 6e 73 74 72  plete the constr
23b69 75 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 65  uction of the ne
23b6a 77 20 74 61 62 6c 65 20 72 65 63 6f 72 64 2e 0a  w table record..
23b6b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
23b6c 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74  E void sqlite3St
23b6d 61 72 74 54 61 62 6c 65 28 0a 20 20 50 61 72 73  artTable(.  Pars
23b6e 65 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20  e *pParse,   /* 
23b6f 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  Parser context *
23b70 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65  /.  Token *pName
23b71 31 2c 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61  1,   /* First pa
23b72 72 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 6f  rt of the name o
23b73 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76  f the table or v
23b74 69 65 77 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  iew */.  Token *
23b75 70 4e 61 6d 65 32 2c 20 20 20 2f 2a 20 53 65 63  pName2,   /* Sec
23b76 6f 6e 64 20 70 61 72 74 20 6f 66 20 74 68 65 20  ond part of the 
23b77 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c  name of the tabl
23b78 65 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 69  e or view */.  i
23b79 6e 74 20 69 73 54 65 6d 70 2c 20 20 20 20 20 20  nt isTemp,      
23b7a 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
23b7b 69 73 20 61 20 54 45 4d 50 20 74 61 62 6c 65 20  is a TEMP table 
23b7c 2a 2f 0a 20 20 69 6e 74 20 69 73 56 69 65 77 2c  */.  int isView,
23b7d 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
23b7e 20 74 68 69 73 20 69 73 20 61 20 56 49 45 57 20   this is a VIEW 
23b7f 2a 2f 0a 20 20 69 6e 74 20 69 73 56 69 72 74 75  */.  int isVirtu
23b80 61 6c 2c 20 20 20 2f 2a 20 54 72 75 65 20 69 66  al,   /* True if
23b81 20 74 68 69 73 20 69 73 20 61 20 56 49 52 54 55   this is a VIRTU
23b82 41 4c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  AL table */.  in
23b83 74 20 6e 6f 45 72 72 20 20 20 20 20 20 20 20 2f  t noErr        /
23b84 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20 69 66 20  * Do nothing if 
23b85 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78  table already ex
23b86 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 54 61 62  ists */.){.  Tab
23b87 6c 65 20 2a 70 54 61 62 6c 65 3b 0a 20 20 63 68  le *pTable;.  ch
23b88 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 2f  ar *zName = 0; /
23b89 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  * The name of th
23b8a 65 20 6e 65 77 20 74 61 62 6c 65 20 2a 2f 0a 20  e new table */. 
23b8b 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
23b8c 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 56 64 62  Parse->db;.  Vdb
23b8d 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69 44 62 3b  e *v;.  int iDb;
23b8e 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
23b8f 62 61 73 65 20 6e 75 6d 62 65 72 20 74 6f 20 63  base number to c
23b90 72 65 61 74 65 20 74 68 65 20 74 61 62 6c 65 20  reate the table 
23b91 69 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  in */.  Token *p
23b92 4e 61 6d 65 3b 20 20 20 20 2f 2a 20 55 6e 71 75  Name;    /* Unqu
23b93 61 6c 69 66 69 65 64 20 6e 61 6d 65 20 6f 66 20  alified name of 
23b94 74 68 65 20 74 61 62 6c 65 20 74 6f 20 63 72 65  the table to cre
23b95 61 74 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65  ate */..  /* The
23b96 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 6e   table or view n
23b97 61 6d 65 20 74 6f 20 63 72 65 61 74 65 20 69 73  ame to create is
23b98 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20   passed to this 
23b99 72 6f 75 74 69 6e 65 20 76 69 61 20 74 6f 6b 65  routine via toke
23b9a 6e 73 0a 20 20 2a 2a 20 70 4e 61 6d 65 31 20 61  ns.  ** pName1 a
23b9b 6e 64 20 70 4e 61 6d 65 32 2e 20 49 66 20 74 68  nd pName2. If th
23b9c 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 77 61 73  e table name was
23b9d 20 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64   fully qualified
23b9e 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 20  , for example:. 
23b9f 20 2a 2a 0a 20 20 2a 2a 20 43 52 45 41 54 45 20   **.  ** CREATE 
23ba0 54 41 42 4c 45 20 78 78 78 2e 79 79 79 20 28 2e  TABLE xxx.yyy (.
23ba1 2e 2e 29 3b 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20  ..);.  ** .  ** 
23ba2 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 20 73  Then pName1 is s
23ba3 65 74 20 74 6f 20 22 78 78 78 22 20 61 6e 64 20  et to "xxx" and 
23ba4 70 4e 61 6d 65 32 20 22 79 79 79 22 2e 20 4f 6e  pName2 "yyy". On
23ba5 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 20   the other hand 
23ba6 69 66 0a 20 20 2a 2a 20 74 68 65 20 74 61 62 6c  if.  ** the tabl
23ba7 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 66 75  e name is not fu
23ba8 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20 69  lly qualified, i
23ba9 2e 65 2e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43  .e.:.  **.  ** C
23baa 52 45 41 54 45 20 54 41 42 4c 45 20 79 79 79 28  REATE TABLE yyy(
23bab 2e 2e 2e 29 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ...);.  **.  ** 
23bac 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 20 73  Then pName1 is s
23bad 65 74 20 74 6f 20 22 79 79 79 22 20 61 6e 64 20  et to "yyy" and 
23bae 70 4e 61 6d 65 32 20 69 73 20 22 22 2e 0a 20 20  pName2 is ""..  
23baf 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c  **.  ** The call
23bb0 20 62 65 6c 6f 77 20 73 65 74 73 20 74 68 65 20   below sets the 
23bb1 70 4e 61 6d 65 20 70 6f 69 6e 74 65 72 20 74 6f  pName pointer to
23bb2 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 74 6f   point at the to
23bb3 6b 65 6e 20 28 70 4e 61 6d 65 31 20 6f 72 0a 20  ken (pName1 or. 
23bb4 20 2a 2a 20 70 4e 61 6d 65 32 29 20 74 68 61 74   ** pName2) that
23bb5 20 73 74 6f 72 65 73 20 74 68 65 20 75 6e 71 75   stores the unqu
23bb6 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e 61  alified table na
23bb7 6d 65 2e 20 54 68 65 20 76 61 72 69 61 62 6c 65  me. The variable
23bb8 20 69 44 62 20 69 73 0a 20 20 2a 2a 20 73 65 74   iDb is.  ** set
23bb9 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66   to the index of
23bba 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68   the database th
23bbb 61 74 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20  at the table or 
23bbc 76 69 65 77 20 69 73 20 74 6f 20 62 65 0a 20 20  view is to be.  
23bbd 2a 2a 20 63 72 65 61 74 65 64 20 69 6e 2e 0a 20  ** created in.. 
23bbe 20 2a 2f 0a 20 20 69 44 62 20 3d 20 73 71 6c 69   */.  iDb = sqli
23bbf 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70  te3TwoPartName(p
23bc0 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70  Parse, pName1, p
23bc1 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b 0a  Name2, &pName);.
23bc2 20 20 69 66 28 20 69 44 62 3c 30 20 29 20 72 65    if( iDb<0 ) re
23bc3 74 75 72 6e 3b 0a 20 20 69 66 28 20 21 4f 4d 49  turn;.  if( !OMI
23bc4 54 5f 54 45 4d 50 44 42 20 26 26 20 69 73 54 65  T_TEMPDB && isTe
23bc5 6d 70 20 26 26 20 69 44 62 3e 31 20 29 7b 0a 20  mp && iDb>1 ){. 
23bc6 20 20 20 2f 2a 20 49 66 20 63 72 65 61 74 69 6e     /* If creatin
23bc7 67 20 61 20 74 65 6d 70 20 74 61 62 6c 65 2c 20  g a temp table, 
23bc8 74 68 65 20 6e 61 6d 65 20 6d 61 79 20 6e 6f 74  the name may not
23bc9 20 62 65 20 71 75 61 6c 69 66 69 65 64 20 2a 2f   be qualified */
23bca 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
23bcb 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 65  rMsg(pParse, "te
23bcc 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 6e 61  mporary table na
23bcd 6d 65 20 6d 75 73 74 20 62 65 20 75 6e 71 75 61  me must be unqua
23bce 6c 69 66 69 65 64 22 29 3b 0a 20 20 20 20 72 65  lified");.    re
23bcf 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20  turn;.  }.  if( 
23bd0 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20  !OMIT_TEMPDB && 
23bd1 69 73 54 65 6d 70 20 29 20 69 44 62 20 3d 20 31  isTemp ) iDb = 1
23bd2 3b 0a 0a 20 20 70 50 61 72 73 65 2d 3e 73 4e 61  ;..  pParse->sNa
23bd3 6d 65 54 6f 6b 65 6e 20 3d 20 2a 70 4e 61 6d 65  meToken = *pName
23bd4 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69  ;.  zName = sqli
23bd5 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
23bd6 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69  (db, pName);.  i
23bd7 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 72 65  f( zName==0 ) re
23bd8 74 75 72 6e 3b 0a 20 20 69 66 28 20 53 51 4c 49  turn;.  if( SQLI
23bd9 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68  TE_OK!=sqlite3Ch
23bda 65 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50  eckObjectName(pP
23bdb 61 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a  arse, zName) ){.
23bdc 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74      goto begin_t
23bdd 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a  able_error;.  }.
23bde 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 69    if( db->init.i
23bdf 44 62 3d 3d 31 20 29 20 69 73 54 65 6d 70 20 3d  Db==1 ) isTemp =
23be0 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   1;.#ifndef SQLI
23be1 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a  TE_OMIT_AUTHORIZ
23be2 41 54 49 4f 4e 0a 20 20 61 73 73 65 72 74 28 20  ATION.  assert( 
23be3 28 69 73 54 65 6d 70 20 26 20 31 29 3d 3d 69 73  (isTemp & 1)==is
23be4 54 65 6d 70 20 29 3b 0a 20 20 7b 0a 20 20 20 20  Temp );.  {.    
23be5 69 6e 74 20 63 6f 64 65 3b 0a 20 20 20 20 63 68  int code;.    ch
23be6 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44  ar *zDb = db->aD
23be7 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  b[iDb].zName;.  
23be8 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
23be9 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
23bea 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20 53 43  QLITE_INSERT, SC
23beb 48 45 4d 41 5f 54 41 42 4c 45 28 69 73 54 65 6d  HEMA_TABLE(isTem
23bec 70 29 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20  p), 0, zDb) ){. 
23bed 20 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f       goto begin_
23bee 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20  table_error;.   
23bef 20 7d 0a 20 20 20 20 69 66 28 20 69 73 56 69 65   }.    if( isVie
23bf0 77 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21  w ){.      if( !
23bf1 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69  OMIT_TEMPDB && i
23bf2 73 54 65 6d 70 20 29 7b 0a 20 20 20 20 20 20 20  sTemp ){.       
23bf3 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43   code = SQLITE_C
23bf4 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 3b  REATE_TEMP_VIEW;
23bf5 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
23bf6 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c        code = SQL
23bf7 49 54 45 5f 43 52 45 41 54 45 5f 56 49 45 57 3b  ITE_CREATE_VIEW;
23bf8 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
23bf9 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 21 4f  se{.      if( !O
23bfa 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 73  MIT_TEMPDB && is
23bfb 54 65 6d 70 20 29 7b 0a 20 20 20 20 20 20 20 20  Temp ){.        
23bfc 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52  code = SQLITE_CR
23bfd 45 41 54 45 5f 54 45 4d 50 5f 54 41 42 4c 45 3b  EATE_TEMP_TABLE;
23bfe 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
23bff 20 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c        code = SQL
23c00 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c 45  ITE_CREATE_TABLE
23c01 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
23c02 20 20 20 20 69 66 28 20 21 69 73 56 69 72 74 75      if( !isVirtu
23c03 61 6c 20 26 26 20 73 71 6c 69 74 65 33 41 75 74  al && sqlite3Aut
23c04 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63  hCheck(pParse, c
23c05 6f 64 65 2c 20 7a 4e 61 6d 65 2c 20 30 2c 20 7a  ode, zName, 0, z
23c06 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  Db) ){.      got
23c07 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72  o begin_table_er
23c08 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  ror;.    }.  }.#
23c09 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d 61 6b 65  endif..  /* Make
23c0a 20 73 75 72 65 20 74 68 65 20 6e 65 77 20 74 61   sure the new ta
23c0b 62 6c 65 20 6e 61 6d 65 20 64 6f 65 73 20 6e 6f  ble name does no
23c0c 74 20 63 6f 6c 6c 69 64 65 20 77 69 74 68 20 61  t collide with a
23c0d 6e 20 65 78 69 73 74 69 6e 67 0a 20 20 2a 2a 20  n existing.  ** 
23c0e 69 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 20 6e  index or table n
23c0f 61 6d 65 20 69 6e 20 74 68 65 20 73 61 6d 65 20  ame in the same 
23c10 64 61 74 61 62 61 73 65 2e 20 20 49 73 73 75 65  database.  Issue
23c11 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
23c12 65 20 69 66 0a 20 20 2a 2a 20 69 74 20 64 6f 65  e if.  ** it doe
23c13 73 2e 20 54 68 65 20 65 78 63 65 70 74 69 6f 6e  s. The exception
23c14 20 69 73 20 69 66 20 74 68 65 20 73 74 61 74 65   is if the state
23c15 6d 65 6e 74 20 62 65 69 6e 67 20 70 61 72 73 65  ment being parse
23c16 64 20 77 61 73 20 70 61 73 73 65 64 0a 20 20 2a  d was passed.  *
23c17 2a 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f  * to an sqlite3_
23c18 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20 63  declare_vtab() c
23c19 61 6c 6c 2e 20 49 6e 20 74 68 61 74 20 63 61 73  all. In that cas
23c1a 65 20 6f 6e 6c 79 20 74 68 65 20 63 6f 6c 75 6d  e only the colum
23c1b 6e 20 6e 61 6d 65 73 0a 20 20 2a 2a 20 61 6e 64  n names.  ** and
23c1c 20 74 79 70 65 73 20 77 69 6c 6c 20 62 65 20 75   types will be u
23c1d 73 65 64 2c 20 73 6f 20 74 68 65 72 65 20 69 73  sed, so there is
23c1e 20 6e 6f 20 6e 65 65 64 20 74 6f 20 74 65 73 74   no need to test
23c1f 20 66 6f 72 20 6e 61 6d 65 73 70 61 63 65 0a 20   for namespace. 
23c20 20 2a 2a 20 63 6f 6c 6c 69 73 69 6f 6e 73 2e 0a   ** collisions..
23c21 20 20 2a 2f 0a 20 20 69 66 28 20 21 49 4e 5f 44    */.  if( !IN_D
23c22 45 43 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a 20  ECLARE_VTAB ){. 
23c23 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b     if( SQLITE_OK
23c24 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  !=sqlite3ReadSch
23c25 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20  ema(pParse) ){. 
23c26 20 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f       goto begin_
23c27 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20  table_error;.   
23c28 20 7d 0a 20 20 20 20 70 54 61 62 6c 65 20 3d 20   }.    pTable = 
23c29 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
23c2a 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 64 62 2d 3e  (db, zName, db->
23c2b 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b  aDb[iDb].zName);
23c2c 0a 20 20 20 20 69 66 28 20 70 54 61 62 6c 65 20  .    if( pTable 
23c2d 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 6e 6f  ){.      if( !no
23c2e 45 72 72 20 29 7b 0a 20 20 20 20 20 20 20 20 73  Err ){.        s
23c2f 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
23c30 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 54  Parse, "table %T
23c31 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 22   already exists"
23c32 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  , pName);.      
23c33 7d 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67  }.      goto beg
23c34 69 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a  in_table_error;.
23c35 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71      }.    if( sq
23c36 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64  lite3FindIndex(d
23c37 62 2c 20 7a 4e 61 6d 65 2c 20 30 29 21 3d 30 20  b, zName, 0)!=0 
23c38 26 26 20 28 69 44 62 3d 3d 30 20 7c 7c 20 21 64  && (iDb==0 || !d
23c39 62 2d 3e 69 6e 69 74 2e 62 75 73 79 29 20 29 7b  b->init.busy) ){
23c3a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
23c3b 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
23c3c 74 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79  there is already
23c3d 20 61 6e 20 69 6e 64 65 78 20 6e 61 6d 65 64 20   an index named 
23c3e 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20  %s", zName);.   
23c3f 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61     goto begin_ta
23c40 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d  ble_error;.    }
23c41 0a 20 20 7d 0a 0a 20 20 70 54 61 62 6c 65 20 3d  .  }..  pTable =
23c42 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
23c43 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
23c44 54 61 62 6c 65 29 29 3b 0a 20 20 69 66 28 20 70  Table));.  if( p
23c45 54 61 62 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20  Table==0 ){.    
23c46 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
23c47 20 3d 20 31 3b 0a 20 20 20 20 70 50 61 72 73 65   = 1;.    pParse
23c48 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  ->rc = SQLITE_NO
23c49 4d 45 4d 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  MEM;.    pParse-
23c4a 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 67 6f 74  >nErr++;.    got
23c4b 6f 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72  o begin_table_er
23c4c 72 6f 72 3b 0a 20 20 7d 0a 20 20 70 54 61 62 6c  ror;.  }.  pTabl
23c4d 65 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65  e->zName = zName
23c4e 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 69 50 4b 65  ;.  pTable->iPKe
23c4f 79 20 3d 20 2d 31 3b 0a 20 20 70 54 61 62 6c 65  y = -1;.  pTable
23c50 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e  ->pSchema = db->
23c51 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61  aDb[iDb].pSchema
23c52 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66  ;.  pTable->nRef
23c53 20 3d 20 31 3b 0a 20 20 70 54 61 62 6c 65 2d 3e   = 1;.  pTable->
23c54 64 62 4d 65 6d 20 3d 20 30 3b 0a 20 20 61 73 73  dbMem = 0;.  ass
23c55 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65  ert( pParse->pNe
23c56 77 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 70  wTable==0 );.  p
23c57 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
23c58 20 3d 20 70 54 61 62 6c 65 3b 0a 0a 20 20 2f 2a   = pTable;..  /*
23c59 20 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20   If this is the 
23c5a 6d 61 67 69 63 20 73 71 6c 69 74 65 5f 73 65 71  magic sqlite_seq
23c5b 75 65 6e 63 65 20 74 61 62 6c 65 20 75 73 65 64  uence table used
23c5c 20 62 79 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e   by autoincremen
23c5d 74 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 72 65 63  t,.  ** then rec
23c5e 6f 72 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ord a pointer to
23c5f 20 74 68 69 73 20 74 61 62 6c 65 20 69 6e 20 74   this table in t
23c60 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
23c61 20 73 74 72 75 63 74 75 72 65 0a 20 20 2a 2a 20   structure.  ** 
23c62 73 6f 20 74 68 61 74 20 49 4e 53 45 52 54 20 63  so that INSERT c
23c63 61 6e 20 66 69 6e 64 20 74 68 65 20 74 61 62 6c  an find the tabl
23c64 65 20 65 61 73 69 6c 79 2e 0a 20 20 2a 2f 0a 23  e easily..  */.#
23c65 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
23c66 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  IT_AUTOINCREMENT
23c67 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e  .  if( !pParse->
23c68 6e 65 73 74 65 64 20 26 26 20 73 74 72 63 6d 70  nested && strcmp
23c69 28 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f  (zName, "sqlite_
23c6a 73 65 71 75 65 6e 63 65 22 29 3d 3d 30 20 29 7b  sequence")==0 ){
23c6b 0a 20 20 20 20 70 54 61 62 6c 65 2d 3e 70 53 63  .    pTable->pSc
23c6c 68 65 6d 61 2d 3e 70 53 65 71 54 61 62 20 3d 20  hema->pSeqTab = 
23c6d 70 54 61 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64  pTable;.  }.#end
23c6e 69 66 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67  if..  /* Begin g
23c6f 65 6e 65 72 61 74 69 6e 67 20 74 68 65 20 63 6f  enerating the co
23c70 64 65 20 74 68 61 74 20 77 69 6c 6c 20 69 6e 73  de that will ins
23c71 65 72 74 20 74 68 65 20 74 61 62 6c 65 20 72 65  ert the table re
23c72 63 6f 72 64 20 69 6e 74 6f 0a 20 20 2a 2a 20 74  cord into.  ** t
23c73 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52  he SQLITE_MASTER
23c74 20 74 61 62 6c 65 2e 20 20 4e 6f 74 65 20 69 6e   table.  Note in
23c75 20 70 61 72 74 69 63 75 6c 61 72 20 74 68 61 74   particular that
23c76 20 77 65 20 6d 75 73 74 20 67 6f 20 61 68 65 61   we must go ahea
23c77 64 0a 20 20 2a 2a 20 61 6e 64 20 61 6c 6c 6f 63  d.  ** and alloc
23c78 61 74 65 20 74 68 65 20 72 65 63 6f 72 64 20 6e  ate the record n
23c79 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 61  umber for the ta
23c7a 62 6c 65 20 65 6e 74 72 79 20 6e 6f 77 2e 20 20  ble entry now.  
23c7b 42 65 66 6f 72 65 20 61 6e 79 0a 20 20 2a 2a 20  Before any.  ** 
23c7c 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 55  PRIMARY KEY or U
23c7d 4e 49 51 55 45 20 6b 65 79 77 6f 72 64 73 20 61  NIQUE keywords a
23c7e 72 65 20 70 61 72 73 65 64 2e 20 20 54 68 6f 73  re parsed.  Thos
23c7f 65 20 6b 65 79 77 6f 72 64 73 20 77 69 6c 6c 20  e keywords will 
23c80 63 61 75 73 65 0a 20 20 2a 2a 20 69 6e 64 69 63  cause.  ** indic
23c81 65 73 20 74 6f 20 62 65 20 63 72 65 61 74 65 64  es to be created
23c82 20 61 6e 64 20 74 68 65 20 74 61 62 6c 65 20 72   and the table r
23c83 65 63 6f 72 64 20 6d 75 73 74 20 63 6f 6d 65 20  ecord must come 
23c84 62 65 66 6f 72 65 20 74 68 65 20 0a 20 20 2a 2a  before the .  **
23c85 20 69 6e 64 69 63 65 73 2e 20 20 48 65 6e 63 65   indices.  Hence
23c86 2c 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d  , the record num
23c87 62 65 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c  ber for the tabl
23c88 65 20 6d 75 73 74 20 62 65 20 61 6c 6c 6f 63 61  e must be alloca
23c89 74 65 64 0a 20 20 2a 2a 20 6e 6f 77 2e 0a 20 20  ted.  ** now..  
23c8a 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 6e  */.  if( !db->in
23c8b 69 74 2e 62 75 73 79 20 26 26 20 28 76 20 3d 20  it.busy && (v = 
23c8c 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
23c8d 50 61 72 73 65 29 29 21 3d 30 20 29 7b 0a 20 20  Parse))!=0 ){.  
23c8e 20 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20 69 6e    int j1;.    in
23c8f 74 20 66 69 6c 65 46 6f 72 6d 61 74 3b 0a 20 20  t fileFormat;.  
23c90 20 20 69 6e 74 20 72 65 67 31 2c 20 72 65 67 32    int reg1, reg2
23c91 2c 20 72 65 67 33 3b 0a 20 20 20 20 73 71 6c 69  , reg3;.    sqli
23c92 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
23c93 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30  ration(pParse, 0
23c94 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66  , iDb);..#ifndef
23c95 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
23c96 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66  TUALTABLE.    if
23c97 28 20 69 73 56 69 72 74 75 61 6c 20 29 7b 0a 20  ( isVirtual ){. 
23c98 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23c99 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 56 42 65  AddOp0(v, OP_VBe
23c9a 67 69 6e 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  gin);.    }.#end
23c9b 69 66 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  if..    /* If th
23c9c 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 61 6e  e file format an
23c9d 64 20 65 6e 63 6f 64 69 6e 67 20 69 6e 20 74 68  d encoding in th
23c9e 65 20 64 61 74 61 62 61 73 65 20 68 61 76 65 20  e database have 
23c9f 6e 6f 74 20 62 65 65 6e 20 73 65 74 2c 20 0a 20  not been set, . 
23ca0 20 20 20 2a 2a 20 73 65 74 20 74 68 65 6d 20 6e     ** set them n
23ca1 6f 77 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  ow..    */.    r
23ca2 65 67 31 20 3d 20 70 50 61 72 73 65 2d 3e 72 65  eg1 = pParse->re
23ca3 67 52 6f 77 69 64 20 3d 20 2b 2b 70 50 61 72 73  gRowid = ++pPars
23ca4 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 72 65 67  e->nMem;.    reg
23ca5 32 20 3d 20 70 50 61 72 73 65 2d 3e 72 65 67 52  2 = pParse->regR
23ca6 6f 6f 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  oot = ++pParse->
23ca7 6e 4d 65 6d 3b 0a 20 20 20 20 72 65 67 33 20 3d  nMem;.    reg3 =
23ca8 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
23ca9 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
23caa 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 52 65 61  AddOp3(v, OP_Rea
23cab 64 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 72 65  dCookie, iDb, re
23cac 67 33 2c 20 42 54 52 45 45 5f 46 49 4c 45 5f 46  g3, BTREE_FILE_F
23cad 4f 52 4d 41 54 29 3b 0a 20 20 20 20 73 71 6c 69  ORMAT);.    sqli
23cae 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65  te3VdbeUsesBtree
23caf 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 6a 31  (v, iDb);.    j1
23cb0 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
23cb1 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 72  dOp1(v, OP_If, r
23cb2 65 67 33 29 3b 0a 20 20 20 20 66 69 6c 65 46 6f  eg3);.    fileFo
23cb3 72 6d 61 74 20 3d 20 28 64 62 2d 3e 66 6c 61 67  rmat = (db->flag
23cb4 73 20 26 20 53 51 4c 49 54 45 5f 4c 65 67 61 63  s & SQLITE_Legac
23cb5 79 46 69 6c 65 46 6d 74 29 21 3d 30 20 3f 0a 20  yFileFmt)!=0 ?. 
23cb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23cb7 20 31 20 3a 20 53 51 4c 49 54 45 5f 4d 41 58 5f   1 : SQLITE_MAX_
23cb8 46 49 4c 45 5f 46 4f 52 4d 41 54 3b 0a 20 20 20  FILE_FORMAT;.   
23cb9 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
23cba 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
23cbb 2c 20 66 69 6c 65 46 6f 72 6d 61 74 2c 20 72 65  , fileFormat, re
23cbc 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  g3);.    sqlite3
23cbd 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
23cbe 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c  _SetCookie, iDb,
23cbf 20 42 54 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d   BTREE_FILE_FORM
23cc0 41 54 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73  AT, reg3);.    s
23cc1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
23cc2 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
23cc3 45 4e 43 28 64 62 29 2c 20 72 65 67 33 29 3b 0a  ENC(db), reg3);.
23cc4 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23cc5 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43  ddOp3(v, OP_SetC
23cc6 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 42 54 52 45  ookie, iDb, BTRE
23cc7 45 5f 54 45 58 54 5f 45 4e 43 4f 44 49 4e 47 2c  E_TEXT_ENCODING,
23cc8 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69   reg3);.    sqli
23cc9 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
23cca 76 2c 20 6a 31 29 3b 0a 0a 20 20 20 20 2f 2a 20  v, j1);..    /* 
23ccb 54 68 69 73 20 6a 75 73 74 20 63 72 65 61 74 65  This just create
23ccc 73 20 61 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72  s a place-holder
23ccd 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65 20 73   record in the s
23cce 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
23ccf 6c 65 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 72  le..    ** The r
23cd0 65 63 6f 72 64 20 63 72 65 61 74 65 64 20 64 6f  ecord created do
23cd1 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61  es not contain a
23cd2 6e 79 74 68 69 6e 67 20 79 65 74 2e 20 20 49 74  nything yet.  It
23cd3 20 77 69 6c 6c 20 62 65 20 72 65 70 6c 61 63 65   will be replace
23cd4 64 0a 20 20 20 20 2a 2a 20 62 79 20 74 68 65 20  d.    ** by the 
23cd5 72 65 61 6c 20 65 6e 74 72 79 20 69 6e 20 63 6f  real entry in co
23cd6 64 65 20 67 65 6e 65 72 61 74 65 64 20 61 74 20  de generated at 
23cd7 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28  sqlite3EndTable(
23cd8 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  )..    **.    **
23cd9 20 54 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74   The rowid for t
23cda 68 65 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20  he new entry is 
23cdb 6c 65 66 74 20 69 6e 20 72 65 67 69 73 74 65 72  left in register
23cdc 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 77 69   pParse->regRowi
23cdd 64 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 6f  d..    ** The ro
23cde 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  ot page number o
23cdf 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20  f the new table 
23ce0 69 73 20 6c 65 66 74 20 69 6e 20 72 65 67 20 70  is left in reg p
23ce1 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 2e 0a  Parse->regRoot..
23ce2 20 20 20 20 2a 2a 20 54 68 65 20 72 6f 77 69 64      ** The rowid
23ce3 20 61 6e 64 20 72 6f 6f 74 20 70 61 67 65 20 6e   and root page n
23ce4 75 6d 62 65 72 20 76 61 6c 75 65 73 20 61 72 65  umber values are
23ce5 20 6e 65 65 64 65 64 20 62 79 20 74 68 65 20 63   needed by the c
23ce6 6f 64 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20  ode that.    ** 
23ce7 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 20  sqlite3EndTable 
23ce8 77 69 6c 6c 20 67 65 6e 65 72 61 74 65 2e 0a 20  will generate.. 
23ce9 20 20 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e     */.#if !defin
23cea 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
23ceb 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64  IEW) || !defined
23cec 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52  (SQLITE_OMIT_VIR
23ced 54 55 41 4c 54 41 42 4c 45 29 0a 20 20 20 20 69  TUALTABLE).    i
23cee 66 28 20 69 73 56 69 65 77 20 7c 7c 20 69 73 56  f( isView || isV
23cef 69 72 74 75 61 6c 20 29 7b 0a 20 20 20 20 20 20  irtual ){.      
23cf0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23cf1 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
23cf2 20 30 2c 20 72 65 67 32 29 3b 0a 20 20 20 20 7d   0, reg2);.    }
23cf3 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20  else.#endif.    
23cf4 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
23cf5 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
23cf6 43 72 65 61 74 65 54 61 62 6c 65 2c 20 69 44 62  CreateTable, iDb
23cf7 2c 20 72 65 67 32 29 3b 0a 20 20 20 20 7d 0a 20  , reg2);.    }. 
23cf8 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61     sqlite3OpenMa
23cf9 73 74 65 72 54 61 62 6c 65 28 70 50 61 72 73 65  sterTable(pParse
23cfa 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69  , iDb);.    sqli
23cfb 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
23cfc 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 30 2c   OP_NewRowid, 0,
23cfd 20 72 65 67 31 29 3b 0a 20 20 20 20 73 71 6c 69   reg1);.    sqli
23cfe 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
23cff 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67   OP_Null, 0, reg
23d00 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  3);.    sqlite3V
23d01 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
23d02 49 6e 73 65 72 74 2c 20 30 2c 20 72 65 67 33 2c  Insert, 0, reg3,
23d03 20 72 65 67 31 29 3b 0a 20 20 20 20 73 71 6c 69   reg1);.    sqli
23d04 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
23d05 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44  v, OPFLAG_APPEND
23d06 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
23d07 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 43  beAddOp0(v, OP_C
23d08 6c 6f 73 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  lose);.  }..  /*
23d09 20 4e 6f 72 6d 61 6c 20 28 6e 6f 6e 2d 65 72 72   Normal (non-err
23d0a 6f 72 29 20 72 65 74 75 72 6e 2e 20 2a 2f 0a 20  or) return. */. 
23d0b 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49   return;..  /* I
23d0c 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
23d0d 73 2c 20 77 65 20 6a 75 6d 70 20 68 65 72 65 20  s, we jump here 
23d0e 2a 2f 0a 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65  */.begin_table_e
23d0f 72 72 6f 72 3a 0a 20 20 73 71 6c 69 74 65 33 44  rror:.  sqlite3D
23d10 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29  bFree(db, zName)
23d11 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  ;.  return;.}../
23d12 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63 72 6f 20  *.** This macro 
23d13 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 61  is used to compa
23d14 72 65 20 74 77 6f 20 73 74 72 69 6e 67 73 20 69  re two strings i
23d15 6e 20 61 20 63 61 73 65 2d 69 6e 73 65 6e 73 69  n a case-insensi
23d16 74 69 76 65 20 6d 61 6e 6e 65 72 2e 0a 2a 2a 20  tive manner..** 
23d17 49 74 20 69 73 20 73 6c 69 67 68 74 6c 79 20 66  It is slightly f
23d18 61 73 74 65 72 20 74 68 61 6e 20 63 61 6c 6c 69  aster than calli
23d19 6e 67 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  ng sqlite3StrICm
23d1a 70 28 29 20 64 69 72 65 63 74 6c 79 2c 20 62 75  p() directly, bu
23d1b 74 0a 2a 2a 20 70 72 6f 64 75 63 65 73 20 6c 61  t.** produces la
23d1c 72 67 65 72 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a  rger code..**.**
23d1d 20 57 41 52 4e 49 4e 47 3a 20 54 68 69 73 20 6d   WARNING: This m
23d1e 61 63 72 6f 20 69 73 20 6e 6f 74 20 63 6f 6d 70  acro is not comp
23d1f 61 74 69 62 6c 65 20 77 69 74 68 20 74 68 65 20  atible with the 
23d20 73 74 72 63 6d 70 28 29 20 66 61 6d 69 6c 79 2e  strcmp() family.
23d21 20 49 74 0a 2a 2a 20 72 65 74 75 72 6e 73 20 74   It.** returns t
23d22 72 75 65 20 69 66 20 74 68 65 20 74 77 6f 20 73  rue if the two s
23d23 74 72 69 6e 67 73 20 61 72 65 20 65 71 75 61 6c  trings are equal
23d24 2c 20 6f 74 68 65 72 77 69 73 65 20 66 61 6c 73  , otherwise fals
23d25 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 54  e..*/.#define ST
23d26 52 49 43 4d 50 28 78 2c 20 79 29 20 28 5c 0a 73  RICMP(x, y) (\.s
23d27 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77  qlite3UpperToLow
23d28 65 72 5b 2a 28 75 6e 73 69 67 6e 65 64 20 63 68  er[*(unsigned ch
23d29 61 72 20 2a 29 28 78 29 5d 3d 3d 20 20 20 5c 0a  ar *)(x)]==   \.
23d2a 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f  sqlite3UpperToLo
23d2b 77 65 72 5b 2a 28 75 6e 73 69 67 6e 65 64 20 63  wer[*(unsigned c
23d2c 68 61 72 20 2a 29 28 79 29 5d 20 20 20 20 20 5c  har *)(y)]     \
23d2d 0a 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43  .&& sqlite3StrIC
23d2e 6d 70 28 28 78 29 2b 31 2c 28 79 29 2b 31 29 3d  mp((x)+1,(y)+1)=
23d2f 3d 30 20 29 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20  =0 )../*.** Add 
23d30 61 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 74 6f 20  a new column to 
23d31 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e  the table curren
23d32 74 6c 79 20 62 65 69 6e 67 20 63 6f 6e 73 74 72  tly being constr
23d33 75 63 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ucted..**.** The
23d34 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68   parser calls th
23d35 69 73 20 72 6f 75 74 69 6e 65 20 6f 6e 63 65 20  is routine once 
23d36 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20  for each column 
23d37 64 65 63 6c 61 72 61 74 69 6f 6e 0a 2a 2a 20 69  declaration.** i
23d38 6e 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45  n a CREATE TABLE
23d39 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 73 71 6c   statement.  sql
23d3a 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28 29  ite3StartTable()
23d3b 20 67 65 74 73 20 63 61 6c 6c 65 64 0a 2a 2a 20   gets called.** 
23d3c 66 69 72 73 74 20 74 6f 20 67 65 74 20 74 68 69  first to get thi
23d3d 6e 67 73 20 67 6f 69 6e 67 2e 20 20 54 68 65 6e  ngs going.  Then
23d3e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
23d3f 20 63 61 6c 6c 65 64 20 66 6f 72 20 65 61 63 68   called for each
23d40 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53  .** column..*/.S
23d41 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
23d42 69 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c  id sqlite3AddCol
23d43 75 6d 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73  umn(Parse *pPars
23d44 65 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29  e, Token *pName)
23d45 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20  {.  Table *p;.  
23d46 69 6e 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a  int i;.  char *z
23d47 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c  ;.  Column *pCol
23d48 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
23d49 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
23d4a 69 66 28 20 28 70 20 3d 20 70 50 61 72 73 65 2d  if( (p = pParse-
23d4b 3e 70 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 29  >pNewTable)==0 )
23d4c 20 72 65 74 75 72 6e 3b 0a 23 69 66 20 53 51 4c   return;.#if SQL
23d4d 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 0a 20  ITE_MAX_COLUMN. 
23d4e 20 69 66 28 20 70 2d 3e 6e 43 6f 6c 2b 31 3e 64   if( p->nCol+1>d
23d4f 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
23d50 5f 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29  _LIMIT_COLUMN] )
23d51 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
23d52 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74  orMsg(pParse, "t
23d53 6f 6f 20 6d 61 6e 79 20 63 6f 6c 75 6d 6e 73 20  oo many columns 
23d54 6f 6e 20 25 73 22 2c 20 70 2d 3e 7a 4e 61 6d 65  on %s", p->zName
23d55 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
23d56 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7a 20 3d 20   }.#endif.  z = 
23d57 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
23d58 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b  oken(db, pName);
23d59 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65  .  if( z==0 ) re
23d5a 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  turn;.  for(i=0;
23d5b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29   i<p->nCol; i++)
23d5c 7b 0a 20 20 20 20 69 66 28 20 53 54 52 49 43 4d  {.    if( STRICM
23d5d 50 28 7a 2c 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e  P(z, p->aCol[i].
23d5e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20  zName) ){.      
23d5f 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
23d60 70 50 61 72 73 65 2c 20 22 64 75 70 6c 69 63 61  pParse, "duplica
23d61 74 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 3a 20  te column name: 
23d62 25 73 22 2c 20 7a 29 3b 0a 20 20 20 20 20 20 73  %s", z);.      s
23d63 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
23d64 20 7a 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72   z);.      retur
23d65 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  n;.    }.  }.  i
23d66 66 28 20 28 70 2d 3e 6e 43 6f 6c 20 26 20 30 78  f( (p->nCol & 0x
23d67 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 43 6f 6c  7)==0 ){.    Col
23d68 75 6d 6e 20 2a 61 4e 65 77 3b 0a 20 20 20 20 61  umn *aNew;.    a
23d69 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 52  New = sqlite3DbR
23d6a 65 61 6c 6c 6f 63 28 64 62 2c 70 2d 3e 61 43 6f  ealloc(db,p->aCo
23d6b 6c 2c 28 70 2d 3e 6e 43 6f 6c 2b 38 29 2a 73 69  l,(p->nCol+8)*si
23d6c 7a 65 6f 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d 29  zeof(p->aCol[0])
23d6d 29 3b 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d  );.    if( aNew=
23d6e 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
23d6f 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29  te3DbFree(db, z)
23d70 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
23d71 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 61 43 6f      }.    p->aCo
23d72 6c 20 3d 20 61 4e 65 77 3b 0a 20 20 7d 0a 20 20  l = aNew;.  }.  
23d73 70 43 6f 6c 20 3d 20 26 70 2d 3e 61 43 6f 6c 5b  pCol = &p->aCol[
23d74 70 2d 3e 6e 43 6f 6c 5d 3b 0a 20 20 6d 65 6d 73  p->nCol];.  mems
23d75 65 74 28 70 43 6f 6c 2c 20 30 2c 20 73 69 7a 65  et(pCol, 0, size
23d76 6f 66 28 70 2d 3e 61 43 6f 6c 5b 30 5d 29 29 3b  of(p->aCol[0]));
23d77 0a 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d  .  pCol->zName =
23d78 20 7a 3b 0a 20 0a 20 20 2f 2a 20 49 66 20 74 68   z;. .  /* If th
23d79 65 72 65 20 69 73 20 6e 6f 20 74 79 70 65 20 73  ere is no type s
23d7a 70 65 63 69 66 69 65 64 2c 20 63 6f 6c 75 6d 6e  pecified, column
23d7b 73 20 68 61 76 65 20 74 68 65 20 64 65 66 61 75  s have the defau
23d7c 6c 74 20 61 66 66 69 6e 69 74 79 0a 20 20 2a 2a  lt affinity.  **
23d7d 20 27 4e 4f 4e 45 27 2e 20 49 66 20 74 68 65 72   'NONE'. If ther
23d7e 65 20 69 73 20 61 20 74 79 70 65 20 73 70 65 63  e is a type spec
23d7f 69 66 69 65 64 2c 20 74 68 65 6e 20 73 71 6c 69  ified, then sqli
23d80 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65  te3AddColumnType
23d81 28 29 20 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20  () will.  ** be 
23d82 63 61 6c 6c 65 64 20 6e 65 78 74 20 74 6f 20 73  called next to s
23d83 65 74 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74  et pCol->affinit
23d84 79 20 63 6f 72 72 65 63 74 6c 79 2e 0a 20 20 2a  y correctly..  *
23d85 2f 0a 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69  /.  pCol->affini
23d86 74 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  ty = SQLITE_AFF_
23d87 4e 4f 4e 45 3b 0a 20 20 70 2d 3e 6e 43 6f 6c 2b  NONE;.  p->nCol+
23d88 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  +;.}../*.** This
23d89 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
23d8a 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72  ed by the parser
23d8b 20 77 68 69 6c 65 20 69 6e 20 74 68 65 20 6d 69   while in the mi
23d8c 64 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69  ddle of.** parsi
23d8d 6e 67 20 61 20 43 52 45 41 54 45 20 54 41 42 4c  ng a CREATE TABL
23d8e 45 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 20  E statement.  A 
23d8f 22 4e 4f 54 20 4e 55 4c 4c 22 20 63 6f 6e 73 74  "NOT NULL" const
23d90 72 61 69 6e 74 20 68 61 73 0a 2a 2a 20 62 65 65  raint has.** bee
23d91 6e 20 73 65 65 6e 20 6f 6e 20 61 20 63 6f 6c 75  n seen on a colu
23d92 6d 6e 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  mn.  This routin
23d93 65 20 73 65 74 73 20 74 68 65 20 6e 6f 74 4e 75  e sets the notNu
23d94 6c 6c 20 66 6c 61 67 20 6f 6e 0a 2a 2a 20 74 68  ll flag on.** th
23d95 65 20 63 6f 6c 75 6d 6e 20 63 75 72 72 65 6e 74  e column current
23d96 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  ly under constru
23d97 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
23d98 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
23d99 6c 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28  lite3AddNotNull(
23d9a 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
23d9b 6e 74 20 6f 6e 45 72 72 6f 72 29 7b 0a 20 20 54  nt onError){.  T
23d9c 61 62 6c 65 20 2a 70 3b 0a 20 20 70 20 3d 20 70  able *p;.  p = p
23d9d 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
23d9e 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20  ;.  if( p==0 || 
23d9f 4e 45 56 45 52 28 70 2d 3e 6e 43 6f 6c 3c 31 29  NEVER(p->nCol<1)
23da0 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 2d 3e   ) return;.  p->
23da1 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31 5d 2e  aCol[p->nCol-1].
23da2 6e 6f 74 4e 75 6c 6c 20 3d 20 28 75 38 29 6f 6e  notNull = (u8)on
23da3 45 72 72 6f 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Error;.}../*.** 
23da4 53 63 61 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20  Scan the column 
23da5 74 79 70 65 20 6e 61 6d 65 20 7a 54 79 70 65 20  type name zType 
23da6 28 6c 65 6e 67 74 68 20 6e 54 79 70 65 29 20 61  (length nType) a
23da7 6e 64 20 72 65 74 75 72 6e 20 74 68 65 0a 2a 2a  nd return the.**
23da8 20 61 73 73 6f 63 69 61 74 65 64 20 61 66 66 69   associated affi
23da9 6e 69 74 79 20 74 79 70 65 2e 0a 2a 2a 0a 2a 2a  nity type..**.**
23daa 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f   This routine do
23dab 65 73 20 61 20 63 61 73 65 2d 69 6e 64 65 70 65  es a case-indepe
23dac 6e 64 65 6e 74 20 73 65 61 72 63 68 20 6f 66 20  ndent search of 
23dad 7a 54 79 70 65 20 66 6f 72 20 74 68 65 20 0a 2a  zType for the .*
23dae 2a 20 73 75 62 73 74 72 69 6e 67 73 20 69 6e 20  * substrings in 
23daf 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61  the following ta
23db0 62 6c 65 2e 20 49 66 20 6f 6e 65 20 6f 66 20 74  ble. If one of t
23db1 68 65 20 73 75 62 73 74 72 69 6e 67 73 20 69 73  he substrings is
23db2 0a 2a 2a 20 66 6f 75 6e 64 2c 20 74 68 65 20 63  .** found, the c
23db3 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 61 66 66  orresponding aff
23db4 69 6e 69 74 79 20 69 73 20 72 65 74 75 72 6e 65  inity is returne
23db5 64 2e 20 49 66 20 7a 54 79 70 65 20 63 6f 6e 74  d. If zType cont
23db6 61 69 6e 73 0a 2a 2a 20 6d 6f 72 65 20 74 68 61  ains.** more tha
23db7 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 73 75 62  n one of the sub
23db8 73 74 72 69 6e 67 73 2c 20 65 6e 74 72 69 65 73  strings, entries
23db9 20 74 6f 77 61 72 64 20 74 68 65 20 74 6f 70 20   toward the top 
23dba 6f 66 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65  of .** the table
23dbb 20 74 61 6b 65 20 70 72 69 6f 72 69 74 79 2e 20   take priority. 
23dbc 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 20  For example, if 
23dbd 7a 54 79 70 65 20 69 73 20 27 42 4c 4f 42 49 4e  zType is 'BLOBIN
23dbe 54 27 2c 20 0a 2a 2a 20 53 51 4c 49 54 45 5f 41  T', .** SQLITE_A
23dbf 46 46 5f 49 4e 54 45 47 45 52 20 69 73 20 72 65  FF_INTEGER is re
23dc0 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 75  turned..**.** Su
23dc1 62 73 74 72 69 6e 67 20 20 20 20 20 7c 20 41 66  bstring     | Af
23dc2 66 69 6e 69 74 79 0a 2a 2a 20 2d 2d 2d 2d 2d 2d  finity.** ------
23dc3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
23dc4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 27 49  ----------.** 'I
23dc5 4e 54 27 20 20 20 20 20 20 20 20 20 7c 20 53 51  NT'         | SQ
23dc6 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52  LITE_AFF_INTEGER
23dc7 0a 2a 2a 20 27 43 48 41 52 27 20 20 20 20 20 20  .** 'CHAR'      
23dc8 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 54    | SQLITE_AFF_T
23dc9 45 58 54 0a 2a 2a 20 27 43 4c 4f 42 27 20 20 20  EXT.** 'CLOB'   
23dca 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46       | SQLITE_AF
23dcb 46 5f 54 45 58 54 0a 2a 2a 20 27 54 45 58 54 27  F_TEXT.** 'TEXT'
23dcc 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45          | SQLITE
23dcd 5f 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27 42 4c  _AFF_TEXT.** 'BL
23dce 4f 42 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c  OB'        | SQL
23dcf 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 0a 2a 2a 20  ITE_AFF_NONE.** 
23dd0 27 52 45 41 4c 27 20 20 20 20 20 20 20 20 7c 20  'REAL'        | 
23dd1 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a  SQLITE_AFF_REAL.
23dd2 2a 2a 20 27 46 4c 4f 41 27 20 20 20 20 20 20 20  ** 'FLOA'       
23dd3 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45   | SQLITE_AFF_RE
23dd4 41 4c 0a 2a 2a 20 27 44 4f 55 42 27 20 20 20 20  AL.** 'DOUB'    
23dd5 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46      | SQLITE_AFF
23dd6 5f 52 45 41 4c 0a 2a 2a 0a 2a 2a 20 49 66 20 6e  _REAL.**.** If n
23dd7 6f 6e 65 20 6f 66 20 74 68 65 20 73 75 62 73 74  one of the subst
23dd8 72 69 6e 67 73 20 69 6e 20 74 68 65 20 61 62 6f  rings in the abo
23dd9 76 65 20 74 61 62 6c 65 20 61 72 65 20 66 6f 75  ve table are fou
23dda 6e 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46  nd,.** SQLITE_AF
23ddb 46 5f 4e 55 4d 45 52 49 43 20 69 73 20 72 65 74  F_NUMERIC is ret
23ddc 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  urned..*/.SQLITE
23ddd 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 73 71  _PRIVATE char sq
23dde 6c 69 74 65 33 41 66 66 69 6e 69 74 79 54 79 70  lite3AffinityTyp
23ddf 65 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49  e(const char *zI
23de0 6e 29 7b 0a 20 20 75 33 32 20 68 20 3d 20 30 3b  n){.  u32 h = 0;
23de1 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20 53 51  .  char aff = SQ
23de2 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
23de3 3b 0a 0a 20 20 69 66 28 20 7a 49 6e 20 29 20 77  ;..  if( zIn ) w
23de4 68 69 6c 65 28 20 7a 49 6e 5b 30 5d 20 29 7b 0a  hile( zIn[0] ){.
23de5 20 20 20 20 68 20 3d 20 28 68 3c 3c 38 29 20 2b      h = (h<<8) +
23de6 20 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c   sqlite3UpperToL
23de7 6f 77 65 72 5b 28 2a 7a 49 6e 29 26 30 78 66 66  ower[(*zIn)&0xff
23de8 5d 3b 0a 20 20 20 20 7a 49 6e 2b 2b 3b 0a 20 20  ];.    zIn++;.  
23de9 20 20 69 66 28 20 68 3d 3d 28 28 27 63 27 3c 3c    if( h==(('c'<<
23dea 32 34 29 2b 28 27 68 27 3c 3c 31 36 29 2b 28 27  24)+('h'<<16)+('
23deb 61 27 3c 3c 38 29 2b 27 72 27 29 20 29 7b 20 20  a'<<8)+'r') ){  
23dec 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 48             /* CH
23ded 41 52 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20  AR */.      aff 
23dee 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58  = SQLITE_AFF_TEX
23def 54 3b 20 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  T; .    }else if
23df0 28 20 68 3d 3d 28 28 27 63 27 3c 3c 32 34 29 2b  ( h==(('c'<<24)+
23df1 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c  ('l'<<16)+('o'<<
23df2 38 29 2b 27 62 27 29 20 29 7b 20 20 20 20 20 20  8)+'b') ){      
23df3 20 2f 2a 20 43 4c 4f 42 20 2a 2f 0a 20 20 20 20   /* CLOB */.    
23df4 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41    aff = SQLITE_A
23df5 46 46 5f 54 45 58 54 3b 0a 20 20 20 20 7d 65 6c  FF_TEXT;.    }el
23df6 73 65 20 69 66 28 20 68 3d 3d 28 28 27 74 27 3c  se if( h==(('t'<
23df7 3c 32 34 29 2b 28 27 65 27 3c 3c 31 36 29 2b 28  <24)+('e'<<16)+(
23df8 27 78 27 3c 3c 38 29 2b 27 74 27 29 20 29 7b 20  'x'<<8)+'t') ){ 
23df9 20 20 20 20 20 20 2f 2a 20 54 45 58 54 20 2a 2f        /* TEXT */
23dfa 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c  .      aff = SQL
23dfb 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20  ITE_AFF_TEXT;.  
23dfc 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28    }else if( h==(
23dfd 28 27 62 27 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c  ('b'<<24)+('l'<<
23dfe 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b 27 62 27  16)+('o'<<8)+'b'
23dff 29 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 4c  )          /* BL
23e00 4f 42 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26  OB */.        &&
23e01 20 28 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46   (aff==SQLITE_AF
23e02 46 5f 4e 55 4d 45 52 49 43 20 7c 7c 20 61 66 66  F_NUMERIC || aff
23e03 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41  ==SQLITE_AFF_REA
23e04 4c 29 20 29 7b 0a 20 20 20 20 20 20 61 66 66 20  L) ){.      aff 
23e05 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  = SQLITE_AFF_NON
23e06 45 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  E;.#ifndef SQLIT
23e07 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
23e08 50 4f 49 4e 54 0a 20 20 20 20 7d 65 6c 73 65 20  POINT.    }else 
23e09 69 66 28 20 68 3d 3d 28 28 27 72 27 3c 3c 32 34  if( h==(('r'<<24
23e0a 29 2b 28 27 65 27 3c 3c 31 36 29 2b 28 27 61 27  )+('e'<<16)+('a'
23e0b 3c 3c 38 29 2b 27 6c 27 29 20 20 20 20 20 20 20  <<8)+'l')       
23e0c 20 20 20 2f 2a 20 52 45 41 4c 20 2a 2f 0a 20 20     /* REAL */.  
23e0d 20 20 20 20 20 20 26 26 20 61 66 66 3d 3d 53 51        && aff==SQ
23e0e 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
23e0f 20 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d 20   ){.      aff = 
23e10 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 3b  SQLITE_AFF_REAL;
23e11 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68  .    }else if( h
23e12 3d 3d 28 28 27 66 27 3c 3c 32 34 29 2b 28 27 6c  ==(('f'<<24)+('l
23e13 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c 38 29 2b  '<<16)+('o'<<8)+
23e14 27 61 27 29 20 20 20 20 20 20 20 20 20 20 2f 2a  'a')          /*
23e15 20 46 4c 4f 41 20 2a 2f 0a 20 20 20 20 20 20 20   FLOA */.       
23e16 20 26 26 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f   && aff==SQLITE_
23e17 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 7b 0a 20  AFF_NUMERIC ){. 
23e18 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54       aff = SQLIT
23e19 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 20 20 20 20  E_AFF_REAL;.    
23e1a 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27  }else if( h==(('
23e1b 64 27 3c 3c 32 34 29 2b 28 27 6f 27 3c 3c 31 36  d'<<24)+('o'<<16
23e1c 29 2b 28 27 75 27 3c 3c 38 29 2b 27 62 27 29 20  )+('u'<<8)+'b') 
23e1d 20 20 20 20 20 20 20 20 20 2f 2a 20 44 4f 55 42           /* DOUB
23e1e 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 20 61   */.        && a
23e1f 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ff==SQLITE_AFF_N
23e20 55 4d 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20  UMERIC ){.      
23e21 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  aff = SQLITE_AFF
23e22 5f 52 45 41 4c 3b 0a 23 65 6e 64 69 66 0a 20 20  _REAL;.#endif.  
23e23 20 20 7d 65 6c 73 65 20 69 66 28 20 28 68 26 30    }else if( (h&0
23e24 78 30 30 46 46 46 46 46 46 29 3d 3d 28 28 27 69  x00FFFFFF)==(('i
23e25 27 3c 3c 31 36 29 2b 28 27 6e 27 3c 3c 38 29 2b  '<<16)+('n'<<8)+
23e26 27 74 27 29 20 29 7b 20 20 20 20 2f 2a 20 49 4e  't') ){    /* IN
23e27 54 20 2a 2f 0a 20 20 20 20 20 20 61 66 66 20 3d  T */.      aff =
23e28 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45   SQLITE_AFF_INTE
23e29 47 45 52 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  GER;.      break
23e2a 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  ;.    }.  }..  r
23e2b 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f 2a  eturn aff;.}../*
23e2c 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
23e2d 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
23e2e 65 20 70 61 72 73 65 72 20 77 68 69 6c 65 20 69  e parser while i
23e2f 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a  n the middle of.
23e30 2a 2a 20 70 61 72 73 69 6e 67 20 61 20 43 52 45  ** parsing a CRE
23e31 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d  ATE TABLE statem
23e32 65 6e 74 2e 20 20 54 68 65 20 70 46 69 72 73 74  ent.  The pFirst
23e33 20 74 6f 6b 65 6e 20 69 73 20 74 68 65 20 66 69   token is the fi
23e34 72 73 74 0a 2a 2a 20 74 6f 6b 65 6e 20 69 6e 20  rst.** token in 
23e35 74 68 65 20 73 65 71 75 65 6e 63 65 20 6f 66 20  the sequence of 
23e36 74 6f 6b 65 6e 73 20 74 68 61 74 20 64 65 73 63  tokens that desc
23e37 72 69 62 65 20 74 68 65 20 74 79 70 65 20 6f 66  ribe the type of
23e38 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 63   the.** column c
23e39 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63  urrently under c
23e3a 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 20 70  onstruction.   p
23e3b 4c 61 73 74 20 69 73 20 74 68 65 20 6c 61 73 74  Last is the last
23e3c 20 74 6f 6b 65 6e 0a 2a 2a 20 69 6e 20 74 68 65   token.** in the
23e3d 20 73 65 71 75 65 6e 63 65 2e 20 20 55 73 65 20   sequence.  Use 
23e3e 74 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  this information
23e3f 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 61 20   to construct a 
23e40 73 74 72 69 6e 67 0a 2a 2a 20 74 68 61 74 20 63  string.** that c
23e41 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 79 70 65  ontains the type
23e42 6e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 75  name of the colu
23e43 6d 6e 20 61 6e 64 20 73 74 6f 72 65 20 74 68 61  mn and store tha
23e44 74 20 73 74 72 69 6e 67 0a 2a 2a 20 69 6e 20 7a  t string.** in z
23e45 54 79 70 65 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45  Type..*/ .SQLITE
23e46 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
23e47 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79  lite3AddColumnTy
23e48 70 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  pe(Parse *pParse
23e49 2c 20 54 6f 6b 65 6e 20 2a 70 54 79 70 65 29 7b  , Token *pType){
23e4a 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20 43  .  Table *p;.  C
23e4b 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 0a 20 20  olumn *pCol;..  
23e4c 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  p = pParse->pNew
23e4d 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 3d 3d  Table;.  if( p==
23e4e 30 20 7c 7c 20 4e 45 56 45 52 28 70 2d 3e 6e 43  0 || NEVER(p->nC
23e4f 6f 6c 3c 31 29 20 29 20 72 65 74 75 72 6e 3b 0a  ol<1) ) return;.
23e50 20 20 70 43 6f 6c 20 3d 20 26 70 2d 3e 61 43 6f    pCol = &p->aCo
23e51 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31 5d 3b 0a 20 20  l[p->nCol-1];.  
23e52 61 73 73 65 72 74 28 20 70 43 6f 6c 2d 3e 7a 54  assert( pCol->zT
23e53 79 70 65 3d 3d 30 20 29 3b 0a 20 20 70 43 6f 6c  ype==0 );.  pCol
23e54 2d 3e 7a 54 79 70 65 20 3d 20 73 71 6c 69 74 65  ->zType = sqlite
23e55 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 70  3NameFromToken(p
23e56 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 79 70 65  Parse->db, pType
23e57 29 3b 0a 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e  );.  pCol->affin
23e58 69 74 79 20 3d 20 73 71 6c 69 74 65 33 41 66 66  ity = sqlite3Aff
23e59 69 6e 69 74 79 54 79 70 65 28 70 43 6f 6c 2d 3e  inityType(pCol->
23e5a 7a 54 79 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  zType);.}../*.**
23e5b 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
23e5c 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 76  is the default v
23e5d 61 6c 75 65 20 66 6f 72 20 74 68 65 20 6d 6f 73  alue for the mos
23e5e 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64  t recently added
23e5f 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 74 68   column.** of th
23e60 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c  e table currentl
23e61 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  y under construc
23e62 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44 65 66 61  tion..**.** Defa
23e63 75 6c 74 20 76 61 6c 75 65 20 65 78 70 72 65 73  ult value expres
23e64 73 69 6f 6e 73 20 6d 75 73 74 20 62 65 20 63 6f  sions must be co
23e65 6e 73 74 61 6e 74 2e 20 20 52 61 69 73 65 20 61  nstant.  Raise a
23e66 6e 20 65 78 63 65 70 74 69 6f 6e 20 69 66 20 74  n exception if t
23e67 68 69 73 0a 2a 2a 20 69 73 20 6e 6f 74 20 74 68  his.** is not th
23e68 65 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  e case..**.** Th
23e69 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
23e6a 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
23e6b 65 72 20 77 68 69 6c 65 20 69 6e 20 74 68 65 20  er while in the 
23e6c 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72  middle of.** par
23e6d 73 69 6e 67 20 61 20 43 52 45 41 54 45 20 54 41  sing a CREATE TA
23e6e 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  BLE statement..*
23e6f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
23e70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64   void sqlite3Add
23e71 44 65 66 61 75 6c 74 56 61 6c 75 65 28 50 61 72  DefaultValue(Par
23e72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72  se *pParse, Expr
23e73 53 70 61 6e 20 2a 70 53 70 61 6e 29 7b 0a 20 20  Span *pSpan){.  
23e74 54 61 62 6c 65 20 2a 70 3b 0a 20 20 43 6f 6c 75  Table *p;.  Colu
23e75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69  mn *pCol;.  sqli
23e76 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
23e77 2d 3e 64 62 3b 0a 20 20 70 20 3d 20 70 50 61 72  ->db;.  p = pPar
23e78 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20  se->pNewTable;. 
23e79 20 69 66 28 20 70 21 3d 30 20 29 7b 0a 20 20 20   if( p!=0 ){.   
23e7a 20 70 43 6f 6c 20 3d 20 26 28 70 2d 3e 61 43 6f   pCol = &(p->aCo
23e7b 6c 5b 70 2d 3e 6e 43 6f 6c 2d 31 5d 29 3b 0a 20  l[p->nCol-1]);. 
23e7c 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 45     if( !sqlite3E
23e7d 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46  xprIsConstantOrF
23e7e 75 6e 63 74 69 6f 6e 28 70 53 70 61 6e 2d 3e 70  unction(pSpan->p
23e7f 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 73  Expr) ){.      s
23e80 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
23e81 50 61 72 73 65 2c 20 22 64 65 66 61 75 6c 74 20  Parse, "default 
23e82 76 61 6c 75 65 20 6f 66 20 63 6f 6c 75 6d 6e 20  value of column 
23e83 5b 25 73 5d 20 69 73 20 6e 6f 74 20 63 6f 6e 73  [%s] is not cons
23e84 74 61 6e 74 22 2c 0a 20 20 20 20 20 20 20 20 20  tant",.         
23e85 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pCol->zName);. 
23e86 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
23e87 2f 2a 20 41 20 63 6f 70 79 20 6f 66 20 70 45 78  /* A copy of pEx
23e88 70 72 20 69 73 20 75 73 65 64 20 69 6e 73 74 65  pr is used inste
23e89 61 64 20 6f 66 20 74 68 65 20 6f 72 69 67 69 6e  ad of the origin
23e8a 61 6c 2c 20 61 73 20 70 45 78 70 72 20 63 6f 6e  al, as pExpr con
23e8b 74 61 69 6e 73 0a 20 20 20 20 20 20 2a 2a 20 74  tains.      ** t
23e8c 6f 6b 65 6e 73 20 74 68 61 74 20 70 6f 69 6e 74  okens that point
23e8d 20 74 6f 20 76 6f 6c 61 74 69 6c 65 20 6d 65 6d   to volatile mem
23e8e 6f 72 79 2e 20 54 68 65 20 27 73 70 61 6e 27 20  ory. The 'span' 
23e8f 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  of the expressio
23e90 6e 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 72 65  n.      ** is re
23e91 71 75 69 72 65 64 20 62 79 20 70 72 61 67 6d 61  quired by pragma
23e92 20 74 61 62 6c 65 5f 69 6e 66 6f 2e 0a 20 20 20   table_info..   
23e93 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69     */.      sqli
23e94 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
23e95 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 29 3b 0a  , pCol->pDflt);.
23e96 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70 44 66 6c        pCol->pDfl
23e97 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  t = sqlite3ExprD
23e98 75 70 28 64 62 2c 20 70 53 70 61 6e 2d 3e 70 45  up(db, pSpan->pE
23e99 78 70 72 2c 20 45 58 50 52 44 55 50 5f 52 45 44  xpr, EXPRDUP_RED
23e9a 55 43 45 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  UCE);.      sqli
23e9b 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43  te3DbFree(db, pC
23e9c 6f 6c 2d 3e 7a 44 66 6c 74 29 3b 0a 20 20 20 20  ol->zDflt);.    
23e9d 20 20 70 43 6f 6c 2d 3e 7a 44 66 6c 74 20 3d 20    pCol->zDflt = 
23e9e 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70  sqlite3DbStrNDup
23e9f 28 64 62 2c 20 28 63 68 61 72 2a 29 70 53 70 61  (db, (char*)pSpa
23ea0 6e 2d 3e 7a 53 74 61 72 74 2c 0a 20 20 20 20 20  n->zStart,.     
23ea1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23ea2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23ea3 28 69 6e 74 29 28 70 53 70 61 6e 2d 3e 7a 45 6e  (int)(pSpan->zEn
23ea4 64 20 2d 20 70 53 70 61 6e 2d 3e 7a 53 74 61 72  d - pSpan->zStar
23ea5 74 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  t));.    }.  }. 
23ea6 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
23ea7 74 65 28 64 62 2c 20 70 53 70 61 6e 2d 3e 70 45  te(db, pSpan->pE
23ea8 78 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  xpr);.}../*.** D
23ea9 65 73 69 67 6e 61 74 65 20 74 68 65 20 50 52 49  esignate the PRI
23eaa 4d 41 52 59 20 4b 45 59 20 66 6f 72 20 74 68 65  MARY KEY for the
23eab 20 74 61 62 6c 65 2e 20 20 70 4c 69 73 74 20 69   table.  pList i
23eac 73 20 61 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65  s a list of name
23ead 73 20 0a 2a 2a 20 6f 66 20 63 6f 6c 75 6d 6e 73  s .** of columns
23eae 20 74 68 61 74 20 66 6f 72 6d 20 74 68 65 20 70   that form the p
23eaf 72 69 6d 61 72 79 20 6b 65 79 2e 20 20 49 66 20  rimary key.  If 
23eb0 70 4c 69 73 74 20 69 73 20 4e 55 4c 4c 2c 20 74  pList is NULL, t
23eb1 68 65 6e 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20  hen the.** most 
23eb2 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20 63  recently added c
23eb3 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 74 61 62  olumn of the tab
23eb4 6c 65 20 69 73 20 74 68 65 20 70 72 69 6d 61 72  le is the primar
23eb5 79 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 74  y key..**.** A t
23eb6 61 62 6c 65 20 63 61 6e 20 68 61 76 65 20 61 74  able can have at
23eb7 20 6d 6f 73 74 20 6f 6e 65 20 70 72 69 6d 61 72   most one primar
23eb8 79 20 6b 65 79 2e 20 20 49 66 20 74 68 65 20 74  y key.  If the t
23eb9 61 62 6c 65 20 61 6c 72 65 61 64 79 20 68 61 73  able already has
23eba 0a 2a 2a 20 61 20 70 72 69 6d 61 72 79 20 6b 65  .** a primary ke
23ebb 79 20 28 61 6e 64 20 74 68 69 73 20 69 73 20 74  y (and this is t
23ebc 68 65 20 73 65 63 6f 6e 64 20 70 72 69 6d 61 72  he second primar
23ebd 79 20 6b 65 79 29 20 74 68 65 6e 20 63 72 65 61  y key) then crea
23ebe 74 65 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 2e 0a  te an.** error..
23ebf 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 52 49  **.** If the PRI
23ec0 4d 41 52 59 20 4b 45 59 20 69 73 20 6f 6e 20 61  MARY KEY is on a
23ec1 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 77   single column w
23ec2 68 6f 73 65 20 64 61 74 61 74 79 70 65 20 69 73  hose datatype is
23ec3 20 49 4e 54 45 47 45 52 2c 0a 2a 2a 20 74 68 65   INTEGER,.** the
23ec4 6e 20 77 65 20 77 69 6c 6c 20 74 72 79 20 74 6f  n we will try to
23ec5 20 75 73 65 20 74 68 61 74 20 63 6f 6c 75 6d 6e   use that column
23ec6 20 61 73 20 74 68 65 20 72 6f 77 69 64 2e 20 20   as the rowid.  
23ec7 53 65 74 20 74 68 65 20 54 61 62 6c 65 2e 69 50  Set the Table.iP
23ec8 4b 65 79 0a 2a 2a 20 66 69 65 6c 64 20 6f 66 20  Key.** field of 
23ec9 74 68 65 20 74 61 62 6c 65 20 75 6e 64 65 72 20  the table under 
23eca 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20  construction to 
23ecb 62 65 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  be the index of 
23ecc 74 68 65 0a 2a 2a 20 49 4e 54 45 47 45 52 20 50  the.** INTEGER P
23ecd 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d  RIMARY KEY colum
23ece 6e 2e 20 20 54 61 62 6c 65 2e 69 50 4b 65 79 20  n.  Table.iPKey 
23ecf 69 73 20 73 65 74 20 74 6f 20 2d 31 20 69 66 20  is set to -1 if 
23ed0 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e 6f 20 49  there is.** no I
23ed1 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
23ed2 45 59 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  EY..**.** If the
23ed3 20 6b 65 79 20 69 73 20 6e 6f 74 20 61 6e 20 49   key is not an I
23ed4 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
23ed5 45 59 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20  EY, then create 
23ed6 61 20 75 6e 69 71 75 65 0a 2a 2a 20 69 6e 64 65  a unique.** inde
23ed7 78 20 66 6f 72 20 74 68 65 20 6b 65 79 2e 20 20  x for the key.  
23ed8 4e 6f 20 69 6e 64 65 78 20 69 73 20 63 72 65 61  No index is crea
23ed9 74 65 64 20 66 6f 72 20 49 4e 54 45 47 45 52 20  ted for INTEGER 
23eda 50 52 49 4d 41 52 59 20 4b 45 59 73 2e 0a 2a 2f  PRIMARY KEYs..*/
23edb 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
23edc 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 50  void sqlite3AddP
23edd 72 69 6d 61 72 79 4b 65 79 28 0a 20 20 50 61 72  rimaryKey(.  Par
23ede 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f  se *pParse,    /
23edf 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
23ee0 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  t */.  ExprList 
23ee1 2a 70 4c 69 73 74 2c 20 20 2f 2a 20 4c 69 73 74  *pList,  /* List
23ee2 20 6f 66 20 66 69 65 6c 64 20 6e 61 6d 65 73 20   of field names 
23ee3 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f  to be indexed */
23ee4 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20  .  int onError, 
23ee5 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f 20       /* What to 
23ee6 64 6f 20 77 69 74 68 20 61 20 75 6e 69 71 75 65  do with a unique
23ee7 6e 65 73 73 20 63 6f 6e 66 6c 69 63 74 20 2a 2f  ness conflict */
23ee8 0a 20 20 69 6e 74 20 61 75 74 6f 49 6e 63 2c 20  .  int autoInc, 
23ee9 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
23eea 74 68 65 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e  the AUTOINCREMEN
23eeb 54 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65  T keyword is pre
23eec 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 73 6f  sent */.  int so
23eed 72 74 4f 72 64 65 72 20 20 20 20 20 2f 2a 20 53  rtOrder     /* S
23eee 51 4c 49 54 45 5f 53 4f 5f 41 53 43 20 6f 72 20  QLITE_SO_ASC or 
23eef 53 51 4c 49 54 45 5f 53 4f 5f 44 45 53 43 20 2a  SQLITE_SO_DESC *
23ef0 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54  /.){.  Table *pT
23ef1 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65  ab = pParse->pNe
23ef2 77 54 61 62 6c 65 3b 0a 20 20 63 68 61 72 20 2a  wTable;.  char *
23ef3 7a 54 79 70 65 20 3d 20 30 3b 0a 20 20 69 6e 74  zType = 0;.  int
23ef4 20 69 43 6f 6c 20 3d 20 2d 31 2c 20 69 3b 0a 20   iCol = -1, i;. 
23ef5 20 69 66 28 20 70 54 61 62 3d 3d 30 20 7c 7c 20   if( pTab==0 || 
23ef6 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20  IN_DECLARE_VTAB 
23ef7 29 20 67 6f 74 6f 20 70 72 69 6d 61 72 79 5f 6b  ) goto primary_k
23ef8 65 79 5f 65 78 69 74 3b 0a 20 20 69 66 28 20 70  ey_exit;.  if( p
23ef9 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  Tab->tabFlags & 
23efa 54 46 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79  TF_HasPrimaryKey
23efb 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
23efc 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
23efd 0a 20 20 20 20 20 20 22 74 61 62 6c 65 20 5c 22  .      "table \"
23efe 25 73 5c 22 20 68 61 73 20 6d 6f 72 65 20 74 68  %s\" has more th
23eff 61 6e 20 6f 6e 65 20 70 72 69 6d 61 72 79 20 6b  an one primary k
23f00 65 79 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ey", pTab->zName
23f01 29 3b 0a 20 20 20 20 67 6f 74 6f 20 70 72 69 6d  );.    goto prim
23f02 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b 0a 20 20  ary_key_exit;.  
23f03 7d 0a 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61  }.  pTab->tabFla
23f04 67 73 20 7c 3d 20 54 46 5f 48 61 73 50 72 69 6d  gs |= TF_HasPrim
23f05 61 72 79 4b 65 79 3b 0a 20 20 69 66 28 20 70 4c  aryKey;.  if( pL
23f06 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 43  ist==0 ){.    iC
23f07 6f 6c 20 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 20  ol = pTab->nCol 
23f08 2d 20 31 3b 0a 20 20 20 20 70 54 61 62 2d 3e 61  - 1;.    pTab->a
23f09 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73 50 72 69 6d  Col[iCol].isPrim
23f0a 4b 65 79 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  Key = 1;.  }else
23f0b 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  {.    for(i=0; i
23f0c 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69  <pList->nExpr; i
23f0d 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69  ++){.      for(i
23f0e 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62  Col=0; iCol<pTab
23f0f 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b  ->nCol; iCol++){
23f10 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
23f11 69 74 65 33 53 74 72 49 43 6d 70 28 70 4c 69 73  ite3StrICmp(pLis
23f12 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 70  t->a[i].zName, p
23f13 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e  Tab->aCol[iCol].
23f14 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20  zName)==0 ){.   
23f15 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
23f16 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
23f17 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 70        if( iCol<p
23f18 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20  Tab->nCol ){.   
23f19 20 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b       pTab->aCol[
23f1a 69 43 6f 6c 5d 2e 69 73 50 72 69 6d 4b 65 79 20  iCol].isPrimKey 
23f1b 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
23f1c 20 7d 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74   }.    if( pList
23f1d 2d 3e 6e 45 78 70 72 3e 31 20 29 20 69 43 6f 6c  ->nExpr>1 ) iCol
23f1e 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 69 66 28   = -1;.  }.  if(
23f1f 20 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c   iCol>=0 && iCol
23f20 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20  <pTab->nCol ){. 
23f21 20 20 20 7a 54 79 70 65 20 3d 20 70 54 61 62 2d     zType = pTab-
23f22 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54 79 70  >aCol[iCol].zTyp
23f23 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 54 79  e;.  }.  if( zTy
23f24 70 65 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  pe && sqlite3Str
23f25 49 43 6d 70 28 7a 54 79 70 65 2c 20 22 49 4e 54  ICmp(zType, "INT
23f26 45 47 45 52 22 29 3d 3d 30 0a 20 20 20 20 20 20  EGER")==0.      
23f27 20 20 26 26 20 73 6f 72 74 4f 72 64 65 72 3d 3d    && sortOrder==
23f28 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 20 29 7b  SQLITE_SO_ASC ){
23f29 0a 20 20 20 20 70 54 61 62 2d 3e 69 50 4b 65 79  .    pTab->iPKey
23f2a 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20 70 54 61   = iCol;.    pTa
23f2b 62 2d 3e 6b 65 79 43 6f 6e 66 20 3d 20 28 75 38  b->keyConf = (u8
23f2c 29 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 20 61 73  )onError;.    as
23f2d 73 65 72 74 28 20 61 75 74 6f 49 6e 63 3d 3d 30  sert( autoInc==0
23f2e 20 7c 7c 20 61 75 74 6f 49 6e 63 3d 3d 31 20 29   || autoInc==1 )
23f2f 3b 0a 20 20 20 20 70 54 61 62 2d 3e 74 61 62 46  ;.    pTab->tabF
23f30 6c 61 67 73 20 7c 3d 20 61 75 74 6f 49 6e 63 2a  lags |= autoInc*
23f31 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74  TF_Autoincrement
23f32 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 61 75  ;.  }else if( au
23f33 74 6f 49 6e 63 20 29 7b 0a 23 69 66 6e 64 65 66  toInc ){.#ifndef
23f34 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
23f35 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20 73  OINCREMENT.    s
23f36 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
23f37 50 61 72 73 65 2c 20 22 41 55 54 4f 49 4e 43 52  Parse, "AUTOINCR
23f38 45 4d 45 4e 54 20 69 73 20 6f 6e 6c 79 20 61 6c  EMENT is only al
23f39 6c 6f 77 65 64 20 6f 6e 20 61 6e 20 22 0a 20 20  lowed on an ".  
23f3a 20 20 20 20 20 22 49 4e 54 45 47 45 52 20 50 52       "INTEGER PR
23f3b 49 4d 41 52 59 20 4b 45 59 22 29 3b 0a 23 65 6e  IMARY KEY");.#en
23f3c 64 69 66 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  dif.  }else{.   
23f3d 20 49 6e 64 65 78 20 2a 70 3b 0a 20 20 20 20 70   Index *p;.    p
23f3e 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74 65   = sqlite3Create
23f3f 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 30 2c  Index(pParse, 0,
23f40 20 30 2c 20 30 2c 20 70 4c 69 73 74 2c 20 6f 6e   0, 0, pList, on
23f41 45 72 72 6f 72 2c 20 30 2c 20 30 2c 20 73 6f 72  Error, 0, 0, sor
23f42 74 4f 72 64 65 72 2c 20 30 29 3b 0a 20 20 20 20  tOrder, 0);.    
23f43 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20 20 70  if( p ){.      p
23f44 2d 3e 61 75 74 6f 49 6e 64 65 78 20 3d 20 32 3b  ->autoIndex = 2;
23f45 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 69 73 74  .    }.    pList
23f46 20 3d 20 30 3b 0a 20 20 7d 0a 0a 70 72 69 6d 61   = 0;.  }..prima
23f47 72 79 5f 6b 65 79 5f 65 78 69 74 3a 0a 20 20 73  ry_key_exit:.  s
23f48 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
23f49 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
23f4a 20 70 4c 69 73 74 29 3b 0a 20 20 72 65 74 75 72   pList);.  retur
23f4b 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20  n;.}../*.** Add 
23f4c 61 20 6e 65 77 20 43 48 45 43 4b 20 63 6f 6e 73  a new CHECK cons
23f4d 74 72 61 69 6e 74 20 74 6f 20 74 68 65 20 74 61  traint to the ta
23f4e 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 75 6e  ble currently un
23f4f 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
23f50 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
23f51 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
23f52 41 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69  AddCheckConstrai
23f53 6e 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  nt(.  Parse *pPa
23f54 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69  rse,    /* Parsi
23f55 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
23f56 45 78 70 72 20 2a 70 43 68 65 63 6b 45 78 70 72  Expr *pCheckExpr
23f57 20 20 2f 2a 20 54 68 65 20 63 68 65 63 6b 20 65    /* The check e
23f58 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a  xpression */.){.
23f59 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
23f5a 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23 69 66 6e  pParse->db;.#ifn
23f5b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
23f5c 43 48 45 43 4b 0a 20 20 54 61 62 6c 65 20 2a 70  CHECK.  Table *p
23f5d 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e  Tab = pParse->pN
23f5e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66 28 20 70  ewTable;.  if( p
23f5f 54 61 62 20 26 26 20 21 49 4e 5f 44 45 43 4c 41  Tab && !IN_DECLA
23f60 52 45 5f 56 54 41 42 20 29 7b 0a 20 20 20 20 70  RE_VTAB ){.    p
23f61 54 61 62 2d 3e 70 43 68 65 63 6b 20 3d 20 73 71  Tab->pCheck = sq
23f62 6c 69 74 65 33 45 78 70 72 41 6e 64 28 64 62 2c  lite3ExprAnd(db,
23f63 20 70 54 61 62 2d 3e 70 43 68 65 63 6b 2c 20 70   pTab->pCheck, p
23f64 43 68 65 63 6b 45 78 70 72 29 3b 0a 20 20 7d 65  CheckExpr);.  }e
23f65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20  lse.#endif.  {. 
23f66 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65     sqlite3ExprDe
23f67 6c 65 74 65 28 64 62 2c 20 70 43 68 65 63 6b 45  lete(db, pCheckE
23f68 78 70 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  xpr);.  }.}../*.
23f69 2a 2a 20 53 65 74 20 74 68 65 20 63 6f 6c 6c 61  ** Set the colla
23f6a 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 6f 66  tion function of
23f6b 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
23f6c 6c 79 20 70 61 72 73 65 64 20 74 61 62 6c 65 20  ly parsed table 
23f6d 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 6f 20 74 68 65  column.** to the
23f6e 20 43 6f 6c 6c 53 65 71 20 67 69 76 65 6e 2e 0a   CollSeq given..
23f6f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
23f70 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64  E void sqlite3Ad
23f71 64 43 6f 6c 6c 61 74 65 54 79 70 65 28 50 61 72  dCollateType(Par
23f72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65  se *pParse, Toke
23f73 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 54 61  n *pToken){.  Ta
23f74 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20 69 3b  ble *p;.  int i;
23f75 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20  .  char *zColl; 
23f76 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
23f77 44 65 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66  Dequoted name of
23f78 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
23f79 6e 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  nce */.  sqlite3
23f7a 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 28 70 20   *db;..  if( (p 
23f7b 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  = pParse->pNewTa
23f7c 62 6c 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e  ble)==0 ) return
23f7d 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 43 6f 6c 2d  ;.  i = p->nCol-
23f7e 31 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  1;.  db = pParse
23f7f 2d 3e 64 62 3b 0a 20 20 7a 43 6f 6c 6c 20 3d 20  ->db;.  zColl = 
23f80 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
23f81 6f 6b 65 6e 28 64 62 2c 20 70 54 6f 6b 65 6e 29  oken(db, pToken)
23f82 3b 0a 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29  ;.  if( !zColl )
23f83 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28 20   return;..  if( 
23f84 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c  sqlite3LocateCol
23f85 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43 6f  lSeq(pParse, zCo
23f86 6c 6c 29 20 29 7b 0a 20 20 20 20 49 6e 64 65 78  ll) ){.    Index
23f87 20 2a 70 49 64 78 3b 0a 20 20 20 20 70 2d 3e 61   *pIdx;.    p->a
23f88 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 20 3d 20 7a  Col[i].zColl = z
23f89 43 6f 6c 6c 3b 0a 20 20 0a 20 20 20 20 2f 2a 20  Coll;.  .    /* 
23f8a 49 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73  If the column is
23f8b 20 64 65 63 6c 61 72 65 64 20 61 73 20 22 3c 6e   declared as "<n
23f8c 61 6d 65 3e 20 50 52 49 4d 41 52 59 20 4b 45 59  ame> PRIMARY KEY
23f8d 20 43 4f 4c 4c 41 54 45 20 3c 74 79 70 65 3e 22   COLLATE <type>"
23f8e 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 61 6e  ,.    ** then an
23f8f 20 69 6e 64 65 78 20 6d 61 79 20 68 61 76 65 20   index may have 
23f90 62 65 65 6e 20 63 72 65 61 74 65 64 20 6f 6e 20  been created on 
23f91 74 68 69 73 20 63 6f 6c 75 6d 6e 20 62 65 66 6f  this column befo
23f92 72 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f  re the.    ** co
23f93 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 77 61 73  llation type was
23f94 20 61 64 64 65 64 2e 20 43 6f 72 72 65 63 74 20   added. Correct 
23f95 74 68 69 73 20 69 66 20 69 74 20 69 73 20 74 68  this if it is th
23f96 65 20 63 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20  e case..    */. 
23f97 20 20 20 66 6f 72 28 70 49 64 78 3d 70 2d 3e 70     for(pIdx=p->p
23f98 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
23f99 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  x=pIdx->pNext){.
23f9a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
23f9b 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29  dx->nColumn==1 )
23f9c 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 64 78  ;.      if( pIdx
23f9d 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69  ->aiColumn[0]==i
23f9e 20 29 7b 0a 20 20 20 20 20 20 20 20 70 49 64 78   ){.        pIdx
23f9f 2d 3e 61 7a 43 6f 6c 6c 5b 30 5d 20 3d 20 70 2d  ->azColl[0] = p-
23fa0 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 3b 0a  >aCol[i].zColl;.
23fa1 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
23fa2 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74  }else{.    sqlit
23fa3 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f  e3DbFree(db, zCo
23fa4 6c 6c 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ll);.  }.}../*.*
23fa5 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
23fa6 72 65 74 75 72 6e 73 20 74 68 65 20 63 6f 6c 6c  returns the coll
23fa7 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66  ation sequence f
23fa8 6f 72 20 64 61 74 61 62 61 73 65 20 6e 61 74 69  or database nati
23fa9 76 65 20 74 65 78 74 0a 2a 2a 20 65 6e 63 6f 64  ve text.** encod
23faa 69 6e 67 20 69 64 65 6e 74 69 66 69 65 64 20 62  ing identified b
23fab 79 20 74 68 65 20 73 74 72 69 6e 67 20 7a 4e 61  y the string zNa
23fac 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65  me, length nName
23fad 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72  ..**.** If the r
23fae 65 71 75 65 73 74 65 64 20 63 6f 6c 6c 61 74 69  equested collati
23faf 6f 6e 20 73 65 71 75 65 6e 63 65 20 69 73 20 6e  on sequence is n
23fb0 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72  ot available, or
23fb1 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a   not available.*
23fb2 2a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  * in the databas
23fb3 65 20 6e 61 74 69 76 65 20 65 6e 63 6f 64 69 6e  e native encodin
23fb4 67 2c 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  g, the collation
23fb5 20 66 61 63 74 6f 72 79 20 69 73 20 69 6e 76 6f   factory is invo
23fb6 6b 65 64 20 74 6f 0a 2a 2a 20 72 65 71 75 65 73  ked to.** reques
23fb7 74 20 69 74 2e 20 49 66 20 74 68 65 20 63 6f 6c  t it. If the col
23fb8 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 64  lation factory d
23fb9 6f 65 73 20 6e 6f 74 20 73 75 70 70 6c 79 20 73  oes not supply s
23fba 75 63 68 20 61 20 73 65 71 75 65 6e 63 65 2c 0a  uch a sequence,.
23fbb 2a 2a 20 61 6e 64 20 74 68 65 20 73 65 71 75 65  ** and the seque
23fbc 6e 63 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65  nce is available
23fbd 20 69 6e 20 61 6e 6f 74 68 65 72 20 74 65 78 74   in another text
23fbe 20 65 6e 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20   encoding, then 
23fbf 74 68 61 74 20 69 73 0a 2a 2a 20 72 65 74 75 72  that is.** retur
23fc0 6e 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a  ned instead..**.
23fc1 2a 2a 20 49 66 20 6e 6f 20 76 65 72 73 69 6f 6e  ** If no version
23fc2 73 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74  s of the request
23fc3 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 73 20 73 65  ed collations se
23fc4 71 75 65 6e 63 65 20 61 72 65 20 61 76 61 69 6c  quence are avail
23fc5 61 62 6c 65 2c 20 6f 72 0a 2a 2a 20 61 6e 6f 74  able, or.** anot
23fc6 68 65 72 20 65 72 72 6f 72 20 6f 63 63 75 72 73  her error occurs
23fc7 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e  , NULL is return
23fc8 65 64 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20  ed and an error 
23fc9 6d 65 73 73 61 67 65 20 77 72 69 74 74 65 6e 20  message written 
23fca 69 6e 74 6f 0a 2a 2a 20 70 50 61 72 73 65 2e 0a  into.** pParse..
23fcb 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
23fcc 6e 65 20 69 73 20 61 20 77 72 61 70 70 65 72 20  ne is a wrapper 
23fcd 61 72 6f 75 6e 64 20 73 71 6c 69 74 65 33 46 69  around sqlite3Fi
23fce 6e 64 43 6f 6c 6c 53 65 71 28 29 2e 20 20 54 68  ndCollSeq().  Th
23fcf 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 6e  is routine.** in
23fd0 76 6f 6b 65 73 20 74 68 65 20 63 6f 6c 6c 61 74  vokes the collat
23fd1 69 6f 6e 20 66 61 63 74 6f 72 79 20 69 66 20 74  ion factory if t
23fd2 68 65 20 6e 61 6d 65 64 20 63 6f 6c 6c 61 74 69  he named collati
23fd3 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75  on cannot be fou
23fd4 6e 64 0a 2a 2a 20 61 6e 64 20 67 65 6e 65 72 61  nd.** and genera
23fd5 74 65 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73  tes an error mes
23fd6 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  sage..**.** See 
23fd7 61 6c 73 6f 3a 20 73 71 6c 69 74 65 33 46 69 6e  also: sqlite3Fin
23fd8 64 43 6f 6c 6c 53 65 71 28 29 2c 20 73 71 6c 69  dCollSeq(), sqli
23fd9 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 29 0a  te3GetCollSeq().
23fda 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
23fdb 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74  E CollSeq *sqlit
23fdc 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28  e3LocateCollSeq(
23fdd 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63  Parse *pParse, c
23fde 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
23fdf 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
23fe0 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
23fe1 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 64 62   u8 enc = ENC(db
23fe2 29 3b 0a 20 20 75 38 20 69 6e 69 74 62 75 73 79  );.  u8 initbusy
23fe3 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79   = db->init.busy
23fe4 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  ;.  CollSeq *pCo
23fe5 6c 6c 3b 0a 0a 20 20 70 43 6f 6c 6c 20 3d 20 73  ll;..  pColl = s
23fe6 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65  qlite3FindCollSe
23fe7 71 28 64 62 2c 20 65 6e 63 2c 20 7a 4e 61 6d 65  q(db, enc, zName
23fe8 2c 20 69 6e 69 74 62 75 73 79 29 3b 0a 20 20 69  , initbusy);.  i
23fe9 66 28 20 21 69 6e 69 74 62 75 73 79 20 26 26 20  f( !initbusy && 
23fea 28 21 70 43 6f 6c 6c 20 7c 7c 20 21 70 43 6f 6c  (!pColl || !pCol
23feb 6c 2d 3e 78 43 6d 70 29 20 29 7b 0a 20 20 20 20  l->xCmp) ){.    
23fec 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47  pColl = sqlite3G
23fed 65 74 43 6f 6c 6c 53 65 71 28 64 62 2c 20 65 6e  etCollSeq(db, en
23fee 63 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61 6d 65 29  c, pColl, zName)
23fef 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c  ;.    if( !pColl
23ff0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
23ff1 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
23ff2 2c 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 6c 61  , "no such colla
23ff3 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20 25  tion sequence: %
23ff4 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  s", zName);.    
23ff5 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  }.  }..  return 
23ff6 70 43 6f 6c 6c 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  pColl;.}.../*.**
23ff7 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
23ff8 68 61 74 20 77 69 6c 6c 20 69 6e 63 72 65 6d 65  hat will increme
23ff9 6e 74 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f  nt the schema co
23ffa 6f 6b 69 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  okie..**.** The 
23ffb 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 69 73  schema cookie is
23ffc 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69   used to determi
23ffd 6e 65 20 77 68 65 6e 20 74 68 65 20 73 63 68 65  ne when the sche
23ffe 6d 61 20 66 6f 72 20 74 68 65 0a 2a 2a 20 64 61  ma for the.** da
23fff 74 61 62 61 73 65 20 63 68 61 6e 67 65 73 2e 20  tabase changes. 
24000 20 41 66 74 65 72 20 65 61 63 68 20 73 63 68 65   After each sche
24001 6d 61 20 63 68 61 6e 67 65 2c 20 74 68 65 20 63  ma change, the c
24002 6f 6f 6b 69 65 20 76 61 6c 75 65 0a 2a 2a 20 63  ookie value.** c
24003 68 61 6e 67 65 73 2e 20 20 57 68 65 6e 20 61 20  hanges.  When a 
24004 70 72 6f 63 65 73 73 20 66 69 72 73 74 20 72 65  process first re
24005 61 64 73 20 74 68 65 20 73 63 68 65 6d 61 20 69  ads the schema i
24006 74 20 72 65 63 6f 72 64 73 20 74 68 65 0a 2a 2a  t records the.**
24007 20 63 6f 6f 6b 69 65 2e 20 20 54 68 65 72 65 61   cookie.  Therea
24008 66 74 65 72 2c 20 77 68 65 6e 65 76 65 72 20 69  fter, whenever i
24009 74 20 67 6f 65 73 20 74 6f 20 61 63 63 65 73 73  t goes to access
2400a 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 0a 2a   the database,.*
2400b 2a 20 69 74 20 63 68 65 63 6b 73 20 74 68 65 20  * it checks the 
2400c 63 6f 6f 6b 69 65 20 74 6f 20 6d 61 6b 65 20 73  cookie to make s
2400d 75 72 65 20 74 68 65 20 73 63 68 65 6d 61 20 68  ure the schema h
2400e 61 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 0a 2a  as not changed.*
2400f 2a 20 73 69 6e 63 65 20 69 74 20 77 61 73 20 6c  * since it was l
24010 61 73 74 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20  ast read..**.** 
24011 54 68 69 73 20 70 6c 61 6e 20 69 73 20 6e 6f 74  This plan is not
24012 20 63 6f 6d 70 6c 65 74 65 6c 79 20 62 75 6c 6c   completely bull
24013 65 74 2d 70 72 6f 6f 66 2e 20 20 49 74 20 69 73  et-proof.  It is
24014 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 0a 2a 2a   possible for.**
24015 20 74 68 65 20 73 63 68 65 6d 61 20 74 6f 20 63   the schema to c
24016 68 61 6e 67 65 20 6d 75 6c 74 69 70 6c 65 20 74  hange multiple t
24017 69 6d 65 73 20 61 6e 64 20 66 6f 72 20 74 68 65  imes and for the
24018 20 63 6f 6f 6b 69 65 20 74 6f 20 62 65 0a 2a 2a   cookie to be.**
24019 20 73 65 74 20 62 61 63 6b 20 74 6f 20 70 72 69   set back to pri
2401a 6f 72 20 76 61 6c 75 65 2e 20 20 42 75 74 20 73  or value.  But s
2401b 63 68 65 6d 61 20 63 68 61 6e 67 65 73 20 61 72  chema changes ar
2401c 65 20 69 6e 66 72 65 71 75 65 6e 74 0a 2a 2a 20  e infrequent.** 
2401d 61 6e 64 20 74 68 65 20 70 72 6f 62 61 62 69 6c  and the probabil
2401e 69 74 79 20 6f 66 20 68 69 74 74 69 6e 67 20 74  ity of hitting t
2401f 68 65 20 73 61 6d 65 20 63 6f 6f 6b 69 65 20 76  he same cookie v
24020 61 6c 75 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20  alue is only.** 
24021 31 20 63 68 61 6e 63 65 20 69 6e 20 32 5e 33 32  1 chance in 2^32
24022 2e 20 20 53 6f 20 77 65 27 72 65 20 73 61 66 65  .  So we're safe
24023 20 65 6e 6f 75 67 68 2e 0a 2a 2f 0a 53 51 4c 49   enough..*/.SQLI
24024 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
24025 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f  sqlite3ChangeCoo
24026 6b 69 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  kie(Parse *pPars
24027 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 69  e, int iDb){.  i
24028 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47  nt r1 = sqlite3G
24029 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
2402a 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  );.  sqlite3 *db
2402b 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
2402c 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
2402d 65 2d 3e 70 56 64 62 65 3b 0a 20 20 73 71 6c 69  e->pVdbe;.  sqli
2402e 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2402f 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 64 62 2d   OP_Integer, db-
24030 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d  >aDb[iDb].pSchem
24031 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65  a->schema_cookie
24032 2b 31 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69 74  +1, r1);.  sqlit
24033 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
24034 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44  OP_SetCookie, iD
24035 62 2c 20 42 54 52 45 45 5f 53 43 48 45 4d 41 5f  b, BTREE_SCHEMA_
24036 56 45 52 53 49 4f 4e 2c 20 72 31 29 3b 0a 20 20  VERSION, r1);.  
24037 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
24038 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
24039 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 61 73  );.}../*.** Meas
2403a 75 72 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ure the number o
2403b 66 20 63 68 61 72 61 63 74 65 72 73 20 6e 65 65  f characters nee
2403c 64 65 64 20 74 6f 20 6f 75 74 70 75 74 20 74 68  ded to output th
2403d 65 20 67 69 76 65 6e 0a 2a 2a 20 69 64 65 6e 74  e given.** ident
2403e 69 66 69 65 72 2e 20 20 54 68 65 20 6e 75 6d 62  ifier.  The numb
2403f 65 72 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c  er returned incl
24040 75 64 65 73 20 61 6e 79 20 71 75 6f 74 65 73 20  udes any quotes 
24041 75 73 65 64 0a 2a 2a 20 62 75 74 20 64 6f 65 73  used.** but does
24042 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 74 68 65   not include the
24043 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74 6f 72   null terminator
24044 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 74 69  ..**.** The esti
24045 6d 61 74 65 20 69 73 20 63 6f 6e 73 65 72 76 61  mate is conserva
24046 74 69 76 65 2e 20 20 49 74 20 6d 69 67 68 74 20  tive.  It might 
24047 62 65 20 6c 61 72 67 65 72 20 74 68 61 74 20 77  be larger that w
24048 68 61 74 20 69 73 0a 2a 2a 20 72 65 61 6c 6c 79  hat is.** really
24049 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74   needed..*/.stat
2404a 69 63 20 69 6e 74 20 69 64 65 6e 74 4c 65 6e 67  ic int identLeng
2404b 74 68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  th(const char *z
2404c 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 66 6f  ){.  int n;.  fo
2404d 72 28 6e 3d 30 3b 20 2a 7a 3b 20 6e 2b 2b 2c 20  r(n=0; *z; n++, 
2404e 7a 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 2a 7a  z++){.    if( *z
2404f 3d 3d 27 22 27 20 29 7b 20 6e 2b 2b 3b 20 7d 0a  =='"' ){ n++; }.
24050 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 20 2b    }.  return n +
24051 20 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65   2;.}../*.** The
24052 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
24053 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
24054 20 61 6e 20 6f 75 74 70 75 74 20 62 75 66 66 65   an output buffe
24055 72 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 0a 2a  r. The second .*
24056 2a 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61  * parameter is a
24057 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69   pointer to an i
24058 6e 74 65 67 65 72 20 74 68 61 74 20 63 6f 6e 74  nteger that cont
24059 61 69 6e 73 20 74 68 65 20 6f 66 66 73 65 74 20  ains the offset 
2405a 61 74 0a 2a 2a 20 77 68 69 63 68 20 74 6f 20 77  at.** which to w
2405b 72 69 74 65 20 69 6e 74 6f 20 74 68 65 20 6f 75  rite into the ou
2405c 74 70 75 74 20 62 75 66 66 65 72 2e 20 54 68 69  tput buffer. Thi
2405d 73 20 66 75 6e 63 74 69 6f 6e 20 63 6f 70 69 65  s function copie
2405e 73 20 74 68 65 0a 2a 2a 20 6e 75 6c 2d 74 65 72  s the.** nul-ter
2405f 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 20 70  minated string p
24060 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68 65  ointed to by the
24061 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72   third parameter
24062 2c 20 7a 53 69 67 6e 65 64 49 64 65 6e 74 2c 0a  , zSignedIdent,.
24063 2a 2a 20 74 6f 20 74 68 65 20 73 70 65 63 69 66  ** to the specif
24064 69 65 64 20 6f 66 66 73 65 74 20 69 6e 20 74 68  ied offset in th
24065 65 20 62 75 66 66 65 72 20 61 6e 64 20 75 70 64  e buffer and upd
24066 61 74 65 73 20 2a 70 49 64 78 20 74 6f 20 72 65  ates *pIdx to re
24067 66 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 66 69  fer.** to the fi
24068 72 73 74 20 62 79 74 65 20 61 66 74 65 72 20 74  rst byte after t
24069 68 65 20 6c 61 73 74 20 62 79 74 65 20 77 72 69  he last byte wri
2406a 74 74 65 6e 20 62 65 66 6f 72 65 20 72 65 74 75  tten before retu
2406b 72 6e 69 6e 67 2e 0a 2a 2a 20 0a 2a 2a 20 49 66  rning..** .** If
2406c 20 74 68 65 20 73 74 72 69 6e 67 20 7a 53 69 67   the string zSig
2406d 6e 65 64 49 64 65 6e 74 20 63 6f 6e 73 69 73 74  nedIdent consist
2406e 73 20 65 6e 74 69 72 65 6c 79 20 6f 66 20 61 6c  s entirely of al
2406f 70 68 61 2d 6e 75 6d 65 72 69 63 0a 2a 2a 20 63  pha-numeric.** c
24070 68 61 72 61 63 74 65 72 73 2c 20 64 6f 65 73 20  haracters, does 
24071 6e 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20 61  not begin with a
24072 20 64 69 67 69 74 20 61 6e 64 20 69 73 20 6e 6f   digit and is no
24073 74 20 61 6e 20 53 51 4c 20 6b 65 79 77 6f 72 64  t an SQL keyword
24074 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20  ,.** then it is 
24075 63 6f 70 69 65 64 20 74 6f 20 74 68 65 20 6f 75  copied to the ou
24076 74 70 75 74 20 62 75 66 66 65 72 20 65 78 61 63  tput buffer exac
24077 74 6c 79 20 61 73 20 69 74 20 69 73 2e 20 4f 74  tly as it is. Ot
24078 68 65 72 77 69 73 65 2c 0a 2a 2a 20 69 74 20 69  herwise,.** it i
24079 73 20 71 75 6f 74 65 64 20 75 73 69 6e 67 20 64  s quoted using d
2407a 6f 75 62 6c 65 2d 71 75 6f 74 65 73 2e 0a 2a 2f  ouble-quotes..*/
2407b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 64 65  .static void ide
2407c 6e 74 50 75 74 28 63 68 61 72 20 2a 7a 2c 20 69  ntPut(char *z, i
2407d 6e 74 20 2a 70 49 64 78 2c 20 63 68 61 72 20 2a  nt *pIdx, char *
2407e 7a 53 69 67 6e 65 64 49 64 65 6e 74 29 7b 0a 20  zSignedIdent){. 
2407f 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
24080 7a 49 64 65 6e 74 20 3d 20 28 75 6e 73 69 67 6e  zIdent = (unsign
24081 65 64 20 63 68 61 72 2a 29 7a 53 69 67 6e 65 64  ed char*)zSigned
24082 49 64 65 6e 74 3b 0a 20 20 69 6e 74 20 69 2c 20  Ident;.  int i, 
24083 6a 2c 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20 20  j, needQuote;.  
24084 69 20 3d 20 2a 70 49 64 78 3b 0a 0a 20 20 66 6f  i = *pIdx;..  fo
24085 72 28 6a 3d 30 3b 20 7a 49 64 65 6e 74 5b 6a 5d  r(j=0; zIdent[j]
24086 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; j++){.    if( 
24087 21 73 71 6c 69 74 65 33 49 73 61 6c 6e 75 6d 28  !sqlite3Isalnum(
24088 7a 49 64 65 6e 74 5b 6a 5d 29 20 26 26 20 7a 49  zIdent[j]) && zI
24089 64 65 6e 74 5b 6a 5d 21 3d 27 5f 27 20 29 20 62  dent[j]!='_' ) b
2408a 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 6e 65 65 64  reak;.  }.  need
2408b 51 75 6f 74 65 20 3d 20 73 71 6c 69 74 65 33 49  Quote = sqlite3I
2408c 73 64 69 67 69 74 28 7a 49 64 65 6e 74 5b 30 5d  sdigit(zIdent[0]
2408d 29 20 7c 7c 20 73 71 6c 69 74 65 33 4b 65 79 77  ) || sqlite3Keyw
2408e 6f 72 64 43 6f 64 65 28 7a 49 64 65 6e 74 2c 20  ordCode(zIdent, 
2408f 6a 29 21 3d 54 4b 5f 49 44 3b 0a 20 20 69 66 28  j)!=TK_ID;.  if(
24090 20 21 6e 65 65 64 51 75 6f 74 65 20 29 7b 0a 20   !needQuote ){. 
24091 20 20 20 6e 65 65 64 51 75 6f 74 65 20 3d 20 7a     needQuote = z
24092 49 64 65 6e 74 5b 6a 5d 3b 0a 20 20 7d 0a 0a 20  Ident[j];.  }.. 
24093 20 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29   if( needQuote )
24094 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20   z[i++] = '"';. 
24095 20 66 6f 72 28 6a 3d 30 3b 20 7a 49 64 65 6e 74   for(j=0; zIdent
24096 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 7a  [j]; j++){.    z
24097 5b 69 2b 2b 5d 20 3d 20 7a 49 64 65 6e 74 5b 6a  [i++] = zIdent[j
24098 5d 3b 0a 20 20 20 20 69 66 28 20 7a 49 64 65 6e  ];.    if( zIden
24099 74 5b 6a 5d 3d 3d 27 22 27 20 29 20 7a 5b 69 2b  t[j]=='"' ) z[i+
2409a 2b 5d 20 3d 20 27 22 27 3b 0a 20 20 7d 0a 20 20  +] = '"';.  }.  
2409b 69 66 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20  if( needQuote ) 
2409c 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b 0a 20 20  z[i++] = '"';.  
2409d 7a 5b 69 5d 20 3d 20 30 3b 0a 20 20 2a 70 49 64  z[i] = 0;.  *pId
2409e 78 20 3d 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  x = i;.}../*.** 
2409f 47 65 6e 65 72 61 74 65 20 61 20 43 52 45 41 54  Generate a CREAT
240a0 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  E TABLE statemen
240a1 74 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f  t appropriate fo
240a2 72 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 74  r the given.** t
240a3 61 62 6c 65 2e 20 20 4d 65 6d 6f 72 79 20 74 6f  able.  Memory to
240a4 20 68 6f 6c 64 20 74 68 65 20 74 65 78 74 20 6f   hold the text o
240a5 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  f the statement 
240a6 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66  is obtained.** f
240a7 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  rom sqliteMalloc
240a8 28 29 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66  () and must be f
240a9 72 65 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c  reed by the call
240aa 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f  ing function..*/
240ab 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 63 72  .static char *cr
240ac 65 61 74 65 54 61 62 6c 65 53 74 6d 74 28 73 71  eateTableStmt(sq
240ad 6c 69 74 65 33 20 2a 64 62 2c 20 54 61 62 6c 65  lite3 *db, Table
240ae 20 2a 70 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6b   *p){.  int i, k
240af 2c 20 6e 3b 0a 20 20 63 68 61 72 20 2a 7a 53 74  , n;.  char *zSt
240b0 6d 74 3b 0a 20 20 63 68 61 72 20 2a 7a 53 65 70  mt;.  char *zSep
240b1 2c 20 2a 7a 53 65 70 32 2c 20 2a 7a 45 6e 64 3b  , *zSep2, *zEnd;
240b2 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b  .  Column *pCol;
240b3 0a 20 20 6e 20 3d 20 30 3b 0a 20 20 66 6f 72 28  .  n = 0;.  for(
240b4 70 43 6f 6c 20 3d 20 70 2d 3e 61 43 6f 6c 2c 20  pCol = p->aCol, 
240b5 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20  i=0; i<p->nCol; 
240b6 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20  i++, pCol++){.  
240b7 20 20 6e 20 2b 3d 20 69 64 65 6e 74 4c 65 6e 67    n += identLeng
240b8 74 68 28 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 20  th(pCol->zName) 
240b9 2b 20 35 3b 0a 20 20 7d 0a 20 20 6e 20 2b 3d 20  + 5;.  }.  n += 
240ba 69 64 65 6e 74 4c 65 6e 67 74 68 28 70 2d 3e 7a  identLength(p->z
240bb 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 6e 3c 35  Name);.  if( n<5
240bc 30 20 29 7b 20 0a 20 20 20 20 7a 53 65 70 20 3d  0 ){ .    zSep =
240bd 20 22 22 3b 0a 20 20 20 20 7a 53 65 70 32 20 3d   "";.    zSep2 =
240be 20 22 2c 22 3b 0a 20 20 20 20 7a 45 6e 64 20 3d   ",";.    zEnd =
240bf 20 22 29 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   ")";.  }else{. 
240c0 20 20 20 7a 53 65 70 20 3d 20 22 5c 6e 20 20 22     zSep = "\n  "
240c1 3b 0a 20 20 20 20 7a 53 65 70 32 20 3d 20 22 2c  ;.    zSep2 = ",
240c2 5c 6e 20 20 22 3b 0a 20 20 20 20 7a 45 6e 64 20  \n  ";.    zEnd 
240c3 3d 20 22 5c 6e 29 22 3b 0a 20 20 7d 0a 20 20 6e  = "\n)";.  }.  n
240c4 20 2b 3d 20 33 35 20 2b 20 36 2a 70 2d 3e 6e 43   += 35 + 6*p->nC
240c5 6f 6c 3b 0a 20 20 7a 53 74 6d 74 20 3d 20 73 71  ol;.  zStmt = sq
240c6 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 6e 20 29  lite3Malloc( n )
240c7 3b 0a 20 20 69 66 28 20 7a 53 74 6d 74 3d 3d 30  ;.  if( zStmt==0
240c8 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c   ){.    db->mall
240c9 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
240ca 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
240cb 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
240cc 74 66 28 6e 2c 20 7a 53 74 6d 74 2c 20 22 43 52  tf(n, zStmt, "CR
240cd 45 41 54 45 20 54 41 42 4c 45 20 22 29 3b 0a 20  EATE TABLE ");. 
240ce 20 6b 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   k = sqlite3Strl
240cf 65 6e 33 30 28 7a 53 74 6d 74 29 3b 0a 20 20 69  en30(zStmt);.  i
240d0 64 65 6e 74 50 75 74 28 7a 53 74 6d 74 2c 20 26  dentPut(zStmt, &
240d1 6b 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  k, p->zName);.  
240d2 7a 53 74 6d 74 5b 6b 2b 2b 5d 20 3d 20 27 28 27  zStmt[k++] = '('
240d3 3b 0a 20 20 66 6f 72 28 70 43 6f 6c 3d 70 2d 3e  ;.  for(pCol=p->
240d4 61 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c 70 2d 3e  aCol, i=0; i<p->
240d5 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b  nCol; i++, pCol+
240d6 2b 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20 63  +){.    static c
240d7 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e 73  onst char * cons
240d8 74 20 61 7a 54 79 70 65 5b 5d 20 3d 20 7b 0a 20  t azType[] = {. 
240d9 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45         /* SQLITE
240da 5f 41 46 46 5f 54 45 58 54 20 20 20 20 2a 2f 20  _AFF_TEXT    */ 
240db 22 20 54 45 58 54 22 2c 0a 20 20 20 20 20 20 20  " TEXT",.       
240dc 20 2f 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e   /* SQLITE_AFF_N
240dd 4f 4e 45 20 20 20 20 2a 2f 20 22 22 2c 0a 20 20  ONE    */ "",.  
240de 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f        /* SQLITE_
240df 41 46 46 5f 4e 55 4d 45 52 49 43 20 2a 2f 20 22  AFF_NUMERIC */ "
240e0 20 4e 55 4d 22 2c 0a 20 20 20 20 20 20 20 20 2f   NUM",.        /
240e1 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54  * SQLITE_AFF_INT
240e2 45 47 45 52 20 2a 2f 20 22 20 49 4e 54 22 2c 0a  EGER */ " INT",.
240e3 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54          /* SQLIT
240e4 45 5f 41 46 46 5f 52 45 41 4c 20 20 20 20 2a 2f  E_AFF_REAL    */
240e5 20 22 20 52 45 41 4c 22 0a 20 20 20 20 7d 3b 0a   " REAL".    };.
240e6 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20      int len;.   
240e7 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79   const char *zTy
240e8 70 65 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33  pe;..    sqlite3
240e9 5f 73 6e 70 72 69 6e 74 66 28 6e 2d 6b 2c 20 26  _snprintf(n-k, &
240ea 7a 53 74 6d 74 5b 6b 5d 2c 20 7a 53 65 70 29 3b  zStmt[k], zSep);
240eb 0a 20 20 20 20 6b 20 2b 3d 20 73 71 6c 69 74 65  .    k += sqlite
240ec 33 53 74 72 6c 65 6e 33 30 28 26 7a 53 74 6d 74  3Strlen30(&zStmt
240ed 5b 6b 5d 29 3b 0a 20 20 20 20 7a 53 65 70 20 3d  [k]);.    zSep =
240ee 20 7a 53 65 70 32 3b 0a 20 20 20 20 69 64 65 6e   zSep2;.    iden
240ef 74 50 75 74 28 7a 53 74 6d 74 2c 20 26 6b 2c 20  tPut(zStmt, &k, 
240f0 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pCol->zName);.  
240f1 20 20 61 73 73 65 72 74 28 20 70 43 6f 6c 2d 3e    assert( pCol->
240f2 61 66 66 69 6e 69 74 79 2d 53 51 4c 49 54 45 5f  affinity-SQLITE_
240f3 41 46 46 5f 54 45 58 54 20 3e 3d 20 30 20 29 3b  AFF_TEXT >= 0 );
240f4 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 6f  .    assert( pCo
240f5 6c 2d 3e 61 66 66 69 6e 69 74 79 2d 53 51 4c 49  l->affinity-SQLI
240f6 54 45 5f 41 46 46 5f 54 45 58 54 20 3c 20 73 69  TE_AFF_TEXT < si
240f7 7a 65 6f 66 28 61 7a 54 79 70 65 29 2f 73 69 7a  zeof(azType)/siz
240f8 65 6f 66 28 61 7a 54 79 70 65 5b 30 5d 29 20 29  eof(azType[0]) )
240f9 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
240fa 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d 3d  pCol->affinity==
240fb 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20  SQLITE_AFF_TEXT 
240fc 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
240fd 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3d   pCol->affinity=
240fe 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45  =SQLITE_AFF_NONE
240ff 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
24100 28 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79  ( pCol->affinity
24101 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d  ==SQLITE_AFF_NUM
24102 45 52 49 43 20 29 3b 0a 20 20 20 20 74 65 73 74  ERIC );.    test
24103 63 61 73 65 28 20 70 43 6f 6c 2d 3e 61 66 66 69  case( pCol->affi
24104 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46  nity==SQLITE_AFF
24105 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 20 20 20  _INTEGER );.    
24106 74 65 73 74 63 61 73 65 28 20 70 43 6f 6c 2d 3e  testcase( pCol->
24107 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54 45  affinity==SQLITE
24108 5f 41 46 46 5f 52 45 41 4c 20 29 3b 0a 20 20 20  _AFF_REAL );.   
24109 20 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 61 7a   .    zType = az
2410a 54 79 70 65 5b 70 43 6f 6c 2d 3e 61 66 66 69 6e  Type[pCol->affin
2410b 69 74 79 20 2d 20 53 51 4c 49 54 45 5f 41 46 46  ity - SQLITE_AFF
2410c 5f 54 45 58 54 5d 3b 0a 20 20 20 20 6c 65 6e 20  _TEXT];.    len 
2410d 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
2410e 30 28 7a 54 79 70 65 29 3b 0a 20 20 20 20 61 73  0(zType);.    as
2410f 73 65 72 74 28 20 70 43 6f 6c 2d 3e 61 66 66 69  sert( pCol->affi
24110 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46  nity==SQLITE_AFF
24111 5f 4e 4f 4e 45 20 0a 20 20 20 20 20 20 20 20 20  _NONE .         
24112 20 20 20 7c 7c 20 70 43 6f 6c 2d 3e 61 66 66 69     || pCol->affi
24113 6e 69 74 79 3d 3d 73 71 6c 69 74 65 33 41 66 66  nity==sqlite3Aff
24114 69 6e 69 74 79 54 79 70 65 28 7a 54 79 70 65 29  inityType(zType)
24115 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26   );.    memcpy(&
24116 7a 53 74 6d 74 5b 6b 5d 2c 20 7a 54 79 70 65 2c  zStmt[k], zType,
24117 20 6c 65 6e 29 3b 0a 20 20 20 20 6b 20 2b 3d 20   len);.    k += 
24118 6c 65 6e 3b 0a 20 20 20 20 61 73 73 65 72 74 28  len;.    assert(
24119 20 6b 3c 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20 73   k<=n );.  }.  s
2411a 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
2411b 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b 6b 5d 2c 20  n-k, &zStmt[k], 
2411c 22 25 73 22 2c 20 7a 45 6e 64 29 3b 0a 20 20 72  "%s", zEnd);.  r
2411d 65 74 75 72 6e 20 7a 53 74 6d 74 3b 0a 7d 0a 0a  eturn zStmt;.}..
2411e 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
2411f 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  ne is called to 
24120 72 65 70 6f 72 74 20 74 68 65 20 66 69 6e 61 6c  report the final
24121 20 22 29 22 20 74 68 61 74 20 74 65 72 6d 69 6e   ")" that termin
24122 61 74 65 73 0a 2a 2a 20 61 20 43 52 45 41 54 45  ates.** a CREATE
24123 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
24124 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c  ..**.** The tabl
24125 65 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  e structure that
24126 20 6f 74 68 65 72 20 61 63 74 69 6f 6e 20 72 6f   other action ro
24127 75 74 69 6e 65 73 20 68 61 76 65 20 62 65 65 6e  utines have been
24128 20 62 75 69 6c 64 69 6e 67 0a 2a 2a 20 69 73 20   building.** is 
24129 61 64 64 65 64 20 74 6f 20 74 68 65 20 69 6e 74  added to the int
2412a 65 72 6e 61 6c 20 68 61 73 68 20 74 61 62 6c 65  ernal hash table
2412b 73 2c 20 61 73 73 75 6d 69 6e 67 20 6e 6f 20 65  s, assuming no e
2412c 72 72 6f 72 73 20 68 61 76 65 0a 2a 2a 20 6f 63  rrors have.** oc
2412d 63 75 72 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e  curred..**.** An
2412e 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 74   entry for the t
2412f 61 62 6c 65 20 69 73 20 6d 61 64 65 20 69 6e 20  able is made in 
24130 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65  the master table
24131 20 6f 6e 20 64 69 73 6b 2c 20 75 6e 6c 65 73 73   on disk, unless
24132 0a 2a 2a 20 74 68 69 73 20 69 73 20 61 20 74 65  .** this is a te
24133 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 6f 72  mporary table or
24134 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d   db->init.busy==
24135 31 2e 20 20 57 68 65 6e 20 64 62 2d 3e 69 6e 69  1.  When db->ini
24136 74 2e 62 75 73 79 3d 3d 31 0a 2a 2a 20 69 74 20  t.busy==1.** it 
24137 6d 65 61 6e 73 20 77 65 20 61 72 65 20 72 65 61  means we are rea
24138 64 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 5f  ding the sqlite_
24139 6d 61 73 74 65 72 20 74 61 62 6c 65 20 62 65 63  master table bec
2413a 61 75 73 65 20 77 65 20 6a 75 73 74 0a 2a 2a 20  ause we just.** 
2413b 63 6f 6e 6e 65 63 74 65 64 20 74 6f 20 74 68 65  connected to the
2413c 20 64 61 74 61 62 61 73 65 20 6f 72 20 62 65 63   database or bec
2413d 61 75 73 65 20 74 68 65 20 73 71 6c 69 74 65 5f  ause the sqlite_
2413e 6d 61 73 74 65 72 20 74 61 62 6c 65 20 68 61 73  master table has
2413f 0a 2a 2a 20 72 65 63 65 6e 74 6c 79 20 63 68 61  .** recently cha
24140 6e 67 65 64 2c 20 73 6f 20 74 68 65 20 65 6e 74  nged, so the ent
24141 72 79 20 66 6f 72 20 74 68 69 73 20 74 61 62 6c  ry for this tabl
24142 65 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  e already exists
24143 20 69 6e 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74   in.** the sqlit
24144 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20  e_master table. 
24145 20 57 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20   We do not want 
24146 74 6f 20 63 72 65 61 74 65 20 69 74 20 61 67 61  to create it aga
24147 69 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  in..**.** If the
24148 20 70 53 65 6c 65 63 74 20 61 72 67 75 6d 65 6e   pSelect argumen
24149 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 69  t is not NULL, i
2414a 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 69  t means that thi
2414b 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77 61 73  s routine.** was
2414c 20 63 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 74   called to creat
2414d 65 20 61 20 74 61 62 6c 65 20 67 65 6e 65 72 61  e a table genera
2414e 74 65 64 20 66 72 6f 6d 20 61 20 0a 2a 2a 20 22  ted from a .** "
2414f 43 52 45 41 54 45 20 54 41 42 4c 45 20 2e 2e 2e  CREATE TABLE ...
24150 20 41 53 20 53 45 4c 45 43 54 20 2e 2e 2e 22 20   AS SELECT ..." 
24151 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20  statement.  The 
24152 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 66 0a  column names of.
24153 2a 2a 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  ** the new table
24154 20 77 69 6c 6c 20 6d 61 74 63 68 20 74 68 65 20   will match the 
24155 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68  result set of th
24156 65 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a 53 51 4c  e SELECT..*/.SQL
24157 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
24158 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c 65   sqlite3EndTable
24159 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2415a 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  e,          /* P
2415b 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  arse context */.
2415c 20 20 54 6f 6b 65 6e 20 2a 70 43 6f 6e 73 2c 20    Token *pCons, 
2415d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2415e 20 27 2c 27 20 74 6f 6b 65 6e 20 61 66 74 65 72   ',' token after
2415f 20 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e   the last column
24160 20 64 65 66 6e 2e 20 2a 2f 0a 20 20 54 6f 6b 65   defn. */.  Toke
24161 6e 20 2a 70 45 6e 64 2c 20 20 20 20 20 20 20 20  n *pEnd,        
24162 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6e 61 6c      /* The final
24163 20 27 29 27 20 74 6f 6b 65 6e 20 69 6e 20 74 68   ')' token in th
24164 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 2a  e CREATE TABLE *
24165 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
24166 65 63 74 20 20 20 20 20 20 20 20 20 2f 2a 20 53  ect         /* S
24167 65 6c 65 63 74 20 66 72 6f 6d 20 61 20 22 43 52  elect from a "CR
24168 45 41 54 45 20 2e 2e 2e 20 41 53 20 53 45 4c 45  EATE ... AS SELE
24169 43 54 22 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c  CT" */.){.  Tabl
2416a 65 20 2a 70 3b 0a 20 20 73 71 6c 69 74 65 33 20  e *p;.  sqlite3 
2416b 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2416c 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20  ;.  int iDb;..  
2416d 69 66 28 20 28 70 45 6e 64 3d 3d 30 20 26 26 20  if( (pEnd==0 && 
2416e 70 53 65 6c 65 63 74 3d 3d 30 29 20 7c 7c 20 64  pSelect==0) || d
2416f 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
24170 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
24171 20 7d 0a 20 20 70 20 3d 20 70 50 61 72 73 65 2d   }.  p = pParse-
24172 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 66  >pNewTable;.  if
24173 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( p==0 ) return;
24174 0a 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d  ..  assert( !db-
24175 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c 20 21 70  >init.busy || !p
24176 53 65 6c 65 63 74 20 29 3b 0a 0a 20 20 69 44 62  Select );..  iDb
24177 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61   = sqlite3Schema
24178 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 2d 3e 70  ToIndex(db, p->p
24179 53 63 68 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65  Schema);..#ifnde
2417a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48  f SQLITE_OMIT_CH
2417b 45 43 4b 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65  ECK.  /* Resolve
2417c 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 43 48   names in all CH
2417d 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 65  ECK constraint e
2417e 78 70 72 65 73 73 69 6f 6e 73 2e 0a 20 20 2a 2f  xpressions..  */
2417f 0a 20 20 69 66 28 20 70 2d 3e 70 43 68 65 63 6b  .  if( p->pCheck
24180 20 29 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20   ){.    SrcList 
24181 73 53 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  sSrc;           
24182 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6b 65 20          /* Fake 
24183 53 72 63 4c 69 73 74 20 66 6f 72 20 70 50 61 72  SrcList for pPar
24184 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 2a 2f  se->pNewTable */
24185 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  .    NameContext
24186 20 73 4e 43 3b 20 20 20 20 20 20 20 20 20 20 20   sNC;           
24187 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e       /* Name con
24188 74 65 78 74 20 66 6f 72 20 70 50 61 72 73 65 2d  text for pParse-
24189 3e 70 4e 65 77 54 61 62 6c 65 20 2a 2f 0a 0a 20  >pNewTable */.. 
2418a 20 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c 20     memset(&sNC, 
2418b 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29 3b  0, sizeof(sNC));
2418c 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 73 53 72  .    memset(&sSr
2418d 63 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 53 72  c, 0, sizeof(sSr
2418e 63 29 29 3b 0a 20 20 20 20 73 53 72 63 2e 6e 53  c));.    sSrc.nS
2418f 72 63 20 3d 20 31 3b 0a 20 20 20 20 73 53 72 63  rc = 1;.    sSrc
24190 2e 61 5b 30 5d 2e 7a 4e 61 6d 65 20 3d 20 70 2d  .a[0].zName = p-
24191 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 73 53 72 63  >zName;.    sSrc
24192 2e 61 5b 30 5d 2e 70 54 61 62 20 3d 20 70 3b 0a  .a[0].pTab = p;.
24193 20 20 20 20 73 53 72 63 2e 61 5b 30 5d 2e 69 43      sSrc.a[0].iC
24194 75 72 73 6f 72 20 3d 20 2d 31 3b 0a 20 20 20 20  ursor = -1;.    
24195 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61  sNC.pParse = pPa
24196 72 73 65 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72  rse;.    sNC.pSr
24197 63 4c 69 73 74 20 3d 20 26 73 53 72 63 3b 0a 20  cList = &sSrc;. 
24198 20 20 20 73 4e 43 2e 69 73 43 68 65 63 6b 20 3d     sNC.isCheck =
24199 20 31 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   1;.    if( sqli
2419a 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
2419b 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 43 68  mes(&sNC, p->pCh
2419c 65 63 6b 29 20 29 7b 0a 20 20 20 20 20 20 72 65  eck) ){.      re
2419d 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  turn;.    }.  }.
2419e 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
2419f 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ed(SQLITE_OMIT_C
241a0 48 45 43 4b 29 20 2a 2f 0a 0a 20 20 2f 2a 20 49  HECK) */..  /* I
241a1 66 20 74 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62  f the db->init.b
241a2 75 73 79 20 69 73 20 31 20 69 74 20 6d 65 61 6e  usy is 1 it mean
241a3 73 20 77 65 20 61 72 65 20 72 65 61 64 69 6e 67  s we are reading
241a4 20 74 68 65 20 53 51 4c 20 6f 66 66 20 74 68 65   the SQL off the
241a5 0a 20 20 2a 2a 20 22 73 71 6c 69 74 65 5f 6d 61  .  ** "sqlite_ma
241a6 73 74 65 72 22 20 6f 72 20 22 73 71 6c 69 74 65  ster" or "sqlite
241a7 5f 74 65 6d 70 5f 6d 61 73 74 65 72 22 20 74 61  _temp_master" ta
241a8 62 6c 65 20 6f 6e 20 74 68 65 20 64 69 73 6b 2e  ble on the disk.
241a9 0a 20 20 2a 2a 20 53 6f 20 64 6f 20 6e 6f 74 20  .  ** So do not 
241aa 77 72 69 74 65 20 74 6f 20 74 68 65 20 64 69 73  write to the dis
241ab 6b 20 61 67 61 69 6e 2e 20 20 45 78 74 72 61 63  k again.  Extrac
241ac 74 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  t the root page 
241ad 6e 75 6d 62 65 72 0a 20 20 2a 2a 20 66 6f 72 20  number.  ** for 
241ae 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 74  the table from t
241af 68 65 20 64 62 2d 3e 69 6e 69 74 2e 6e 65 77 54  he db->init.newT
241b0 6e 75 6d 20 66 69 65 6c 64 2e 20 20 28 54 68 65  num field.  (The
241b1 20 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a   page number.  *
241b2 2a 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65  * should have be
241b3 65 6e 20 70 75 74 20 74 68 65 72 65 20 62 79 20  en put there by 
241b4 74 68 65 20 73 71 6c 69 74 65 4f 70 65 6e 43 62  the sqliteOpenCb
241b5 20 72 6f 75 74 69 6e 65 2e 29 0a 20 20 2a 2f 0a   routine.).  */.
241b6 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62    if( db->init.b
241b7 75 73 79 20 29 7b 0a 20 20 20 20 70 2d 3e 74 6e  usy ){.    p->tn
241b8 75 6d 20 3d 20 64 62 2d 3e 69 6e 69 74 2e 6e 65  um = db->init.ne
241b9 77 54 6e 75 6d 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  wTnum;.  }..  /*
241ba 20 49 66 20 6e 6f 74 20 69 6e 69 74 69 61 6c 69   If not initiali
241bb 7a 69 6e 67 2c 20 74 68 65 6e 20 63 72 65 61 74  zing, then creat
241bc 65 20 61 20 72 65 63 6f 72 64 20 66 6f 72 20 74  e a record for t
241bd 68 65 20 6e 65 77 20 74 61 62 6c 65 0a 20 20 2a  he new table.  *
241be 2a 20 69 6e 20 74 68 65 20 53 51 4c 49 54 45 5f  * in the SQLITE_
241bf 4d 41 53 54 45 52 20 74 61 62 6c 65 20 6f 66 20  MASTER table of 
241c0 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 20 20  the database..  
241c1 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20  **.  ** If this 
241c2 69 73 20 61 20 54 45 4d 50 4f 52 41 52 59 20 74  is a TEMPORARY t
241c3 61 62 6c 65 2c 20 77 72 69 74 65 20 74 68 65 20  able, write the 
241c4 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20 61  entry into the a
241c5 75 78 69 6c 69 61 72 79 0a 20 20 2a 2a 20 66 69  uxiliary.  ** fi
241c6 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20 69 6e  le instead of in
241c7 74 6f 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  to the main data
241c8 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a  base file..  */.
241c9 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e    if( !db->init.
241ca 62 75 73 79 20 29 7b 0a 20 20 20 20 69 6e 74 20  busy ){.    int 
241cb 6e 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 3b 0a  n;.    Vdbe *v;.
241cc 20 20 20 20 63 68 61 72 20 2a 7a 54 79 70 65 3b      char *zType;
241cd 20 20 20 20 2f 2a 20 22 76 69 65 77 22 20 6f 72      /* "view" or
241ce 20 22 74 61 62 6c 65 22 20 2a 2f 0a 20 20 20 20   "table" */.    
241cf 63 68 61 72 20 2a 7a 54 79 70 65 32 3b 20 20 20  char *zType2;   
241d0 2f 2a 20 22 56 49 45 57 22 20 6f 72 20 22 54 41  /* "VIEW" or "TA
241d1 42 4c 45 22 20 2a 2f 0a 20 20 20 20 63 68 61 72  BLE" */.    char
241d2 20 2a 7a 53 74 6d 74 3b 20 20 20 20 2f 2a 20 54   *zStmt;    /* T
241d3 65 78 74 20 6f 66 20 74 68 65 20 43 52 45 41 54  ext of the CREAT
241d4 45 20 54 41 42 4c 45 20 6f 72 20 43 52 45 41 54  E TABLE or CREAT
241d5 45 20 56 49 45 57 20 73 74 61 74 65 6d 65 6e 74  E VIEW statement
241d6 20 2a 2f 0a 0a 20 20 20 20 76 20 3d 20 73 71 6c   */..    v = sql
241d7 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
241d8 73 65 29 3b 0a 20 20 20 20 69 66 28 20 4e 45 56  se);.    if( NEV
241d9 45 52 28 76 3d 3d 30 29 20 29 20 72 65 74 75 72  ER(v==0) ) retur
241da 6e 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56  n;..    sqlite3V
241db 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
241dc 43 6c 6f 73 65 2c 20 30 29 3b 0a 0a 20 20 20 20  Close, 0);..    
241dd 2f 2a 20 0a 20 20 20 20 2a 2a 20 49 6e 69 74 69  /* .    ** Initi
241de 61 6c 69 7a 65 20 7a 54 79 70 65 20 66 6f 72 20  alize zType for 
241df 74 68 65 20 6e 65 77 20 76 69 65 77 20 6f 72 20  the new view or 
241e0 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  table..    */.  
241e1 20 20 69 66 28 20 70 2d 3e 70 53 65 6c 65 63 74    if( p->pSelect
241e2 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ==0 ){.      /* 
241e3 41 20 72 65 67 75 6c 61 72 20 74 61 62 6c 65 20  A regular table 
241e4 2a 2f 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d  */.      zType =
241e5 20 22 74 61 62 6c 65 22 3b 0a 20 20 20 20 20 20   "table";.      
241e6 7a 54 79 70 65 32 20 3d 20 22 54 41 42 4c 45 22  zType2 = "TABLE"
241e7 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
241e8 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 20 20 7d  _OMIT_VIEW.    }
241e9 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41  else{.      /* A
241ea 20 76 69 65 77 20 2a 2f 0a 20 20 20 20 20 20 7a   view */.      z
241eb 54 79 70 65 20 3d 20 22 76 69 65 77 22 3b 0a 20  Type = "view";. 
241ec 20 20 20 20 20 7a 54 79 70 65 32 20 3d 20 22 56       zType2 = "V
241ed 49 45 57 22 3b 0a 23 65 6e 64 69 66 0a 20 20 20  IEW";.#endif.   
241ee 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   }..    /* If th
241ef 69 73 20 69 73 20 61 20 43 52 45 41 54 45 20 54  is is a CREATE T
241f0 41 42 4c 45 20 78 78 20 41 53 20 53 45 4c 45 43  ABLE xx AS SELEC
241f1 54 20 2e 2e 2e 2c 20 65 78 65 63 75 74 65 20 74  T ..., execute t
241f2 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a  he SELECT.    **
241f3 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 70 6f   statement to po
241f4 70 75 6c 61 74 65 20 74 68 65 20 6e 65 77 20 74  pulate the new t
241f5 61 62 6c 65 2e 20 54 68 65 20 72 6f 6f 74 2d 70  able. The root-p
241f6 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  age number for t
241f7 68 65 0a 20 20 20 20 2a 2a 20 6e 65 77 20 74 61  he.    ** new ta
241f8 62 6c 65 20 69 73 20 69 6e 20 72 65 67 69 73 74  ble is in regist
241f9 65 72 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f  er pParse->regRo
241fa 6f 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ot..    **.    *
241fb 2a 20 4f 6e 63 65 20 74 68 65 20 53 45 4c 45 43  * Once the SELEC
241fc 54 20 68 61 73 20 62 65 65 6e 20 63 6f 64 65 64  T has been coded
241fd 20 62 79 20 73 71 6c 69 74 65 33 53 65 6c 65 63   by sqlite3Selec
241fe 74 28 29 2c 20 69 74 20 69 73 20 69 6e 20 61 0a  t(), it is in a.
241ff 20 20 20 20 2a 2a 20 73 75 69 74 61 62 6c 65 20      ** suitable 
24200 73 74 61 74 65 20 74 6f 20 71 75 65 72 79 20 66  state to query f
24201 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  or the column na
24202 6d 65 73 20 61 6e 64 20 74 79 70 65 73 20 74 6f  mes and types to
24203 20 62 65 20 75 73 65 64 0a 20 20 20 20 2a 2a 20   be used.    ** 
24204 62 79 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  by the new table
24205 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
24206 41 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 77  A shared-cache w
24207 72 69 74 65 2d 6c 6f 63 6b 20 69 73 20 6e 6f 74  rite-lock is not
24208 20 72 65 71 75 69 72 65 64 20 74 6f 20 77 72 69   required to wri
24209 74 65 20 74 6f 20 74 68 65 20 6e 65 77 20 74 61  te to the new ta
2420a 62 6c 65 2c 0a 20 20 20 20 2a 2a 20 61 73 20 61  ble,.    ** as a
2420b 20 73 63 68 65 6d 61 2d 6c 6f 63 6b 20 6d 75 73   schema-lock mus
2420c 74 20 68 61 76 65 20 61 6c 72 65 61 64 79 20 62  t have already b
2420d 65 65 6e 20 6f 62 74 61 69 6e 65 64 20 74 6f 20  een obtained to 
2420e 63 72 65 61 74 65 20 69 74 2e 20 53 69 6e 63 65  create it. Since
2420f 0a 20 20 20 20 2a 2a 20 61 20 73 63 68 65 6d 61  .    ** a schema
24210 2d 6c 6f 63 6b 20 65 78 63 6c 75 64 65 73 20 61  -lock excludes a
24211 6c 6c 20 6f 74 68 65 72 20 64 61 74 61 62 61 73  ll other databas
24212 65 20 75 73 65 72 73 2c 20 74 68 65 20 77 72 69  e users, the wri
24213 74 65 2d 6c 6f 63 6b 20 77 6f 75 6c 64 0a 20 20  te-lock would.  
24214 20 20 2a 2a 20 62 65 20 72 65 64 75 6e 64 61 6e    ** be redundan
24215 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  t..    */.    if
24216 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ( pSelect ){.   
24217 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65     SelectDest de
24218 73 74 3b 0a 20 20 20 20 20 20 54 61 62 6c 65 20  st;.      Table 
24219 2a 70 53 65 6c 54 61 62 3b 0a 0a 20 20 20 20 20  *pSelTab;..     
2421a 20 61 73 73 65 72 74 28 70 50 61 72 73 65 2d 3e   assert(pParse->
2421b 6e 54 61 62 3d 3d 31 29 3b 0a 20 20 20 20 20 20  nTab==1);.      
2421c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2421d 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74  3(v, OP_OpenWrit
2421e 65 2c 20 31 2c 20 70 50 61 72 73 65 2d 3e 72 65  e, 1, pParse->re
2421f 67 52 6f 6f 74 2c 20 69 44 62 29 3b 0a 20 20 20  gRoot, iDb);.   
24220 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
24221 61 6e 67 65 50 35 28 76 2c 20 31 29 3b 0a 20 20  angeP5(v, 1);.  
24222 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62      pParse->nTab
24223 20 3d 20 32 3b 0a 20 20 20 20 20 20 73 71 6c 69   = 2;.      sqli
24224 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69  te3SelectDestIni
24225 74 28 26 64 65 73 74 2c 20 53 52 54 5f 54 61 62  t(&dest, SRT_Tab
24226 6c 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71  le, 1);.      sq
24227 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72  lite3Select(pPar
24228 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 26 64 65  se, pSelect, &de
24229 73 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  st);.      sqlit
2422a 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2422b 4f 50 5f 43 6c 6f 73 65 2c 20 31 29 3b 0a 20 20  OP_Close, 1);.  
2422c 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e      if( pParse->
2422d 6e 45 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20  nErr==0 ){.     
2422e 20 20 20 70 53 65 6c 54 61 62 20 3d 20 73 71 6c     pSelTab = sql
2422f 69 74 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53  ite3ResultSetOfS
24230 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53  elect(pParse, pS
24231 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20  elect);.        
24232 69 66 28 20 70 53 65 6c 54 61 62 3d 3d 30 20 29  if( pSelTab==0 )
24233 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20   return;.       
24234 20 61 73 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c   assert( p->aCol
24235 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 70  ==0 );.        p
24236 2d 3e 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62  ->nCol = pSelTab
24237 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20  ->nCol;.        
24238 70 2d 3e 61 43 6f 6c 20 3d 20 70 53 65 6c 54 61  p->aCol = pSelTa
24239 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20 20  b->aCol;.       
2423a 20 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d   pSelTab->nCol =
2423b 20 30 3b 0a 20 20 20 20 20 20 20 20 70 53 65 6c   0;.        pSel
2423c 54 61 62 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20  Tab->aCol = 0;. 
2423d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65         sqlite3De
2423e 6c 65 74 65 54 61 62 6c 65 28 70 53 65 6c 54 61  leteTable(pSelTa
2423f 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  b);.      }.    
24240 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d 70 75 74  }..    /* Comput
24241 65 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74  e the complete t
24242 65 78 74 20 6f 66 20 74 68 65 20 43 52 45 41 54  ext of the CREAT
24243 45 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  E statement */. 
24244 20 20 20 69 66 28 20 70 53 65 6c 65 63 74 20 29     if( pSelect )
24245 7b 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20  {.      zStmt = 
24246 63 72 65 61 74 65 54 61 62 6c 65 53 74 6d 74 28  createTableStmt(
24247 64 62 2c 20 70 29 3b 0a 20 20 20 20 7d 65 6c 73  db, p);.    }els
24248 65 7b 0a 20 20 20 20 20 20 6e 20 3d 20 28 69 6e  e{.      n = (in
24249 74 29 28 70 45 6e 64 2d 3e 7a 20 2d 20 70 50 61  t)(pEnd->z - pPa
2424a 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e  rse->sNameToken.
2424b 7a 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 7a 53  z) + 1;.      zS
2424c 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  tmt = sqlite3MPr
2424d 69 6e 74 66 28 64 62 2c 20 0a 20 20 20 20 20 20  intf(db, .      
2424e 20 20 20 20 22 43 52 45 41 54 45 20 25 73 20 25      "CREATE %s %
2424f 2e 2a 73 22 2c 20 7a 54 79 70 65 32 2c 20 6e 2c  .*s", zType2, n,
24250 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f   pParse->sNameTo
24251 6b 65 6e 2e 7a 0a 20 20 20 20 20 20 29 3b 0a 20  ken.z.      );. 
24252 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 20 73     }..    /* A s
24253 6c 6f 74 20 66 6f 72 20 74 68 65 20 72 65 63 6f  lot for the reco
24254 72 64 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  rd has already b
24255 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e  een allocated in
24256 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c   the .    ** SQL
24257 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65  ITE_MASTER table
24258 2e 20 20 57 65 20 6a 75 73 74 20 6e 65 65 64 20  .  We just need 
24259 74 6f 20 75 70 64 61 74 65 20 74 68 61 74 20 73  to update that s
2425a 6c 6f 74 20 77 69 74 68 20 61 6c 6c 0a 20 20 20  lot with all.   
2425b 20 2a 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61 74   ** the informat
2425c 69 6f 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65 63  ion we've collec
2425d 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ted..    */.    
2425e 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72  sqlite3NestedPar
2425f 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20  se(pParse,.     
24260 20 22 55 50 44 41 54 45 20 25 51 2e 25 73 20 22   "UPDATE %Q.%s "
24261 0a 20 20 20 20 20 20 20 20 20 22 53 45 54 20 74  .         "SET t
24262 79 70 65 3d 27 25 73 27 2c 20 6e 61 6d 65 3d 25  ype='%s', name=%
24263 51 2c 20 74 62 6c 5f 6e 61 6d 65 3d 25 51 2c 20  Q, tbl_name=%Q, 
24264 72 6f 6f 74 70 61 67 65 3d 23 25 64 2c 20 73 71  rootpage=#%d, sq
24265 6c 3d 25 51 20 22 0a 20 20 20 20 20 20 20 22 57  l=%Q ".       "W
24266 48 45 52 45 20 72 6f 77 69 64 3d 23 25 64 22 2c  HERE rowid=#%d",
24267 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69  .      db->aDb[i
24268 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d  Db].zName, SCHEM
24269 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 0a 20 20  A_TABLE(iDb),.  
2426a 20 20 20 20 7a 54 79 70 65 2c 0a 20 20 20 20 20      zType,.     
2426b 20 70 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20   p->zName,.     
2426c 20 70 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20   p->zName,.     
2426d 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74   pParse->regRoot
2426e 2c 0a 20 20 20 20 20 20 7a 53 74 6d 74 2c 0a 20  ,.      zStmt,. 
2426f 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 65 67       pParse->reg
24270 52 6f 77 69 64 0a 20 20 20 20 29 3b 0a 20 20 20  Rowid.    );.   
24271 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
24272 62 2c 20 7a 53 74 6d 74 29 3b 0a 20 20 20 20 73  b, zStmt);.    s
24273 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b  qlite3ChangeCook
24274 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b  ie(pParse, iDb);
24275 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
24276 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d  _OMIT_AUTOINCREM
24277 45 4e 54 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b  ENT.    /* Check
24278 20 74 6f 20 73 65 65 20 69 66 20 77 65 20 6e 65   to see if we ne
24279 65 64 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20  ed to create an 
2427a 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20  sqlite_sequence 
2427b 74 61 62 6c 65 20 66 6f 72 0a 20 20 20 20 2a 2a  table for.    **
2427c 20 6b 65 65 70 69 6e 67 20 74 72 61 63 6b 20 6f   keeping track o
2427d 66 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20  f autoincrement 
2427e 6b 65 79 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  keys..    */.   
2427f 20 69 66 28 20 70 2d 3e 74 61 62 46 6c 61 67 73   if( p->tabFlags
24280 20 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d   & TF_Autoincrem
24281 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 44 62 20  ent ){.      Db 
24282 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b  *pDb = &db->aDb[
24283 69 44 62 5d 3b 0a 20 20 20 20 20 20 69 66 28 20  iDb];.      if( 
24284 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53  pDb->pSchema->pS
24285 65 71 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20  eqTab==0 ){.    
24286 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65      sqlite3Neste
24287 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20  dParse(pParse,. 
24288 20 20 20 20 20 20 20 20 20 22 43 52 45 41 54 45           "CREATE
24289 20 54 41 42 4c 45 20 25 51 2e 73 71 6c 69 74 65   TABLE %Q.sqlite
2428a 5f 73 65 71 75 65 6e 63 65 28 6e 61 6d 65 2c 73  _sequence(name,s
2428b 65 71 29 22 2c 0a 20 20 20 20 20 20 20 20 20 20  eq)",.          
2428c 70 44 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20  pDb->zName.     
2428d 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20     );.      }.  
2428e 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
2428f 2f 2a 20 52 65 70 61 72 73 65 20 65 76 65 72 79  /* Reparse every
24290 74 68 69 6e 67 20 74 6f 20 75 70 64 61 74 65 20  thing to update 
24291 6f 75 72 20 69 6e 74 65 72 6e 61 6c 20 64 61 74  our internal dat
24292 61 20 73 74 72 75 63 74 75 72 65 73 20 2a 2f 0a  a structures */.
24293 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24294 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73  ddOp4(v, OP_Pars
24295 65 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 30 2c  eSchema, iDb, 0,
24296 20 30 2c 0a 20 20 20 20 20 20 20 20 73 71 6c 69   0,.        sqli
24297 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
24298 74 62 6c 5f 6e 61 6d 65 3d 27 25 71 27 22 2c 70  tbl_name='%q'",p
24299 2d 3e 7a 4e 61 6d 65 29 2c 20 50 34 5f 44 59 4e  ->zName), P4_DYN
2429a 41 4d 49 43 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f  AMIC);.  }...  /
2429b 2a 20 41 64 64 20 74 68 65 20 74 61 62 6c 65 20  * Add the table 
2429c 74 6f 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79  to the in-memory
2429d 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
2429e 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  of the database.
2429f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  .  */.  if( db->
242a0 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20  init.busy ){.   
242a1 20 54 61 62 6c 65 20 2a 70 4f 6c 64 3b 0a 20 20   Table *pOld;.  
242a2 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
242a3 61 20 3d 20 70 2d 3e 70 53 63 68 65 6d 61 3b 0a  a = p->pSchema;.
242a4 20 20 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74      pOld = sqlit
242a5 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70 53  e3HashInsert(&pS
242a6 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20  chema->tblHash, 
242a7 70 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20  p->zName,.      
242a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
242a9 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
242aa 72 6c 65 6e 33 30 28 70 2d 3e 7a 4e 61 6d 65 29  rlen30(p->zName)
242ab 2c 70 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c  ,p);.    if( pOl
242ac 64 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  d ){.      asser
242ad 74 28 20 70 3d 3d 70 4f 6c 64 20 29 3b 20 20 2f  t( p==pOld );  /
242ae 2a 20 4d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61  * Malloc must ha
242af 76 65 20 66 61 69 6c 65 64 20 69 6e 73 69 64 65  ve failed inside
242b0 20 48 61 73 68 49 6e 73 65 72 74 28 29 20 2a 2f   HashInsert() */
242b1 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  .      db->mallo
242b2 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20  cFailed = 1;.   
242b3 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
242b4 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70 4e 65  .    pParse->pNe
242b5 77 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20  wTable = 0;.    
242b6 64 62 2d 3e 6e 54 61 62 6c 65 2b 2b 3b 0a 20 20  db->nTable++;.  
242b7 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53    db->flags |= S
242b8 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e  QLITE_InternChan
242b9 67 65 73 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  ges;..#ifndef SQ
242ba 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54  LITE_OMIT_ALTERT
242bb 41 42 4c 45 0a 20 20 20 20 69 66 28 20 21 70 2d  ABLE.    if( !p-
242bc 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
242bd 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
242be 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  ame = (const cha
242bf 72 20 2a 29 70 50 61 72 73 65 2d 3e 73 4e 61 6d  r *)pParse->sNam
242c0 65 54 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20 20 20  eToken.z;.      
242c1 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 20 20 20  int nName;.     
242c2 20 61 73 73 65 72 74 28 20 21 70 53 65 6c 65 63   assert( !pSelec
242c3 74 20 26 26 20 70 43 6f 6e 73 20 26 26 20 70 45  t && pCons && pE
242c4 6e 64 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  nd );.      if( 
242c5 70 43 6f 6e 73 2d 3e 7a 3d 3d 30 20 29 7b 0a 20  pCons->z==0 ){. 
242c6 20 20 20 20 20 20 20 70 43 6f 6e 73 20 3d 20 70         pCons = p
242c7 45 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  End;.      }.   
242c8 20 20 20 6e 4e 61 6d 65 20 3d 20 28 69 6e 74 29     nName = (int)
242c9 28 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70  ((const char *)p
242ca 43 6f 6e 73 2d 3e 7a 20 2d 20 7a 4e 61 6d 65 29  Cons->z - zName)
242cb 3b 0a 20 20 20 20 20 20 70 2d 3e 61 64 64 43 6f  ;.      p->addCo
242cc 6c 4f 66 66 73 65 74 20 3d 20 31 33 20 2b 20 73  lOffset = 13 + s
242cd 71 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65  qlite3Utf8CharLe
242ce 6e 28 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b  n(zName, nName);
242cf 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
242d0 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  }.}..#ifndef SQL
242d1 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f 2a  ITE_OMIT_VIEW./*
242d2 0a 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63  .** The parser c
242d3 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e  alls this routin
242d4 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63 72  e in order to cr
242d5 65 61 74 65 20 61 20 6e 65 77 20 56 49 45 57 0a  eate a new VIEW.
242d6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
242d7 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72  E void sqlite3Cr
242d8 65 61 74 65 56 69 65 77 28 0a 20 20 50 61 72 73  eateView(.  Pars
242d9 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f  e *pParse,     /
242da 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f  * The parsing co
242db 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ntext */.  Token
242dc 20 2a 70 42 65 67 69 6e 2c 20 20 20 20 20 2f 2a   *pBegin,     /*
242dd 20 54 68 65 20 43 52 45 41 54 45 20 74 6f 6b 65   The CREATE toke
242de 6e 20 74 68 61 74 20 62 65 67 69 6e 73 20 74 68  n that begins th
242df 65 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  e statement */. 
242e0 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20   Token *pName1, 
242e1 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e      /* The token
242e2 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20   that holds the 
242e3 6e 61 6d 65 20 6f 66 20 74 68 65 20 76 69 65 77  name of the view
242e4 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
242e5 6d 65 32 2c 20 20 20 20 20 2f 2a 20 54 68 65 20  me2,     /* The 
242e6 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f 6c 64 73  token that holds
242e7 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
242e8 20 76 69 65 77 20 2a 2f 0a 20 20 53 65 6c 65 63   view */.  Selec
242e9 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 2f 2a  t *pSelect,   /*
242ea 20 41 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   A SELECT statem
242eb 65 6e 74 20 74 68 61 74 20 77 69 6c 6c 20 62 65  ent that will be
242ec 63 6f 6d 65 20 74 68 65 20 6e 65 77 20 76 69 65  come the new vie
242ed 77 20 2a 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d  w */.  int isTem
242ee 70 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 52 55  p,        /* TRU
242ef 45 20 66 6f 72 20 61 20 54 45 4d 50 4f 52 41 52  E for a TEMPORAR
242f0 59 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20  Y view */.  int 
242f1 6e 6f 45 72 72 20 20 20 20 20 20 20 20 20 20 2f  noErr          /
242f2 2a 20 53 75 70 70 72 65 73 73 20 65 72 72 6f 72  * Suppress error
242f3 20 6d 65 73 73 61 67 65 73 20 69 66 20 56 49 45   messages if VIE
242f4 57 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  W already exists
242f5 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a   */.){.  Table *
242f6 70 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 63 6f  p;.  int n;.  co
242f7 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20 54  nst char *z;.  T
242f8 6f 6b 65 6e 20 73 45 6e 64 3b 0a 20 20 44 62 46  oken sEnd;.  DbF
242f9 69 78 65 72 20 73 46 69 78 3b 0a 20 20 54 6f 6b  ixer sFix;.  Tok
242fa 65 6e 20 2a 70 4e 61 6d 65 3b 0a 20 20 69 6e 74  en *pName;.  int
242fb 20 69 44 62 3b 0a 20 20 73 71 6c 69 74 65 33 20   iDb;.  sqlite3 
242fc 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
242fd 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  ;..  if( pParse-
242fe 3e 6e 56 61 72 3e 30 20 29 7b 0a 20 20 20 20 73  >nVar>0 ){.    s
242ff 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
24300 50 61 72 73 65 2c 20 22 70 61 72 61 6d 65 74 65  Parse, "paramete
24301 72 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77  rs are not allow
24302 65 64 20 69 6e 20 76 69 65 77 73 22 29 3b 0a 20  ed in views");. 
24303 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
24304 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65  Delete(db, pSele
24305 63 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ct);.    return;
24306 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 74  .  }.  sqlite3St
24307 61 72 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c  artTable(pParse,
24308 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c   pName1, pName2,
24309 20 69 73 54 65 6d 70 2c 20 31 2c 20 30 2c 20 6e   isTemp, 1, 0, n
2430a 6f 45 72 72 29 3b 0a 20 20 70 20 3d 20 70 50 61  oErr);.  p = pPa
2430b 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
2430c 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20    if( p==0 ){.  
2430d 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2430e 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63  elete(db, pSelec
2430f 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  t);.    return;.
24310 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 50    }.  assert( pP
24311 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 3b  arse->nErr==0 );
24312 20 2f 2a 20 49 66 20 73 71 6c 69 74 65 33 53 74   /* If sqlite3St
24313 61 72 74 54 61 62 6c 65 20 72 65 74 75 72 6e 20  artTable return 
24314 6e 6f 6e 2d 4e 55 4c 4c 20 74 68 65 6e 0a 20 20  non-NULL then.  
24315 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24316 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68             ** th
24317 65 72 65 20 63 6f 75 6c 64 20 6e 6f 74 20 68 61  ere could not ha
24318 76 65 20 62 65 65 6e 20 61 6e 20 65 72 72 6f 72  ve been an error
24319 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54 77 6f   */.  sqlite3Two
2431a 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c  PartName(pParse,
2431b 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c   pName1, pName2,
2431c 20 26 70 4e 61 6d 65 29 3b 0a 20 20 69 44 62 20   &pName);.  iDb 
2431d 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
2431e 6f 49 6e 64 65 78 28 64 62 2c 20 70 2d 3e 70 53  oIndex(db, p->pS
2431f 63 68 65 6d 61 29 3b 0a 20 20 69 66 28 20 73 71  chema);.  if( sq
24320 6c 69 74 65 33 46 69 78 49 6e 69 74 28 26 73 46  lite3FixInit(&sF
24321 69 78 2c 20 70 50 61 72 73 65 2c 20 69 44 62 2c  ix, pParse, iDb,
24322 20 22 76 69 65 77 22 2c 20 70 4e 61 6d 65 29 0a   "view", pName).
24323 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 46 69      && sqlite3Fi
24324 78 53 65 6c 65 63 74 28 26 73 46 69 78 2c 20 70  xSelect(&sFix, p
24325 53 65 6c 65 63 74 29 0a 20 20 29 7b 0a 20 20 20  Select).  ){.   
24326 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
24327 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74  lete(db, pSelect
24328 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
24329 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 20   }..  /* Make a 
2432a 63 6f 70 79 20 6f 66 20 74 68 65 20 65 6e 74 69  copy of the enti
2432b 72 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  re SELECT statem
2432c 65 6e 74 20 74 68 61 74 20 64 65 66 69 6e 65 73  ent that defines
2432d 20 74 68 65 20 76 69 65 77 2e 0a 20 20 2a 2a 20   the view..  ** 
2432e 54 68 69 73 20 77 69 6c 6c 20 66 6f 72 63 65 20  This will force 
2432f 61 6c 6c 20 74 68 65 20 45 78 70 72 2e 74 6f 6b  all the Expr.tok
24330 65 6e 2e 7a 20 76 61 6c 75 65 73 20 74 6f 20 62  en.z values to b
24331 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 20 20  e dynamically.  
24332 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 72 61 74  ** allocated rat
24333 68 65 72 20 74 68 61 6e 20 70 6f 69 6e 74 20 74  her than point t
24334 6f 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69  o the input stri
24335 6e 67 20 2d 20 77 68 69 63 68 20 6d 65 61 6e 73  ng - which means
24336 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 79 20   that.  ** they 
24337 77 69 6c 6c 20 70 65 72 73 69 73 74 20 61 66 74  will persist aft
24338 65 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  er the current s
24339 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63 61  qlite3_exec() ca
2433a 6c 6c 20 72 65 74 75 72 6e 73 2e 0a 20 20 2a 2f  ll returns..  */
2433b 0a 20 20 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20  .  p->pSelect = 
2433c 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
2433d 28 64 62 2c 20 70 53 65 6c 65 63 74 2c 20 45 58  (db, pSelect, EX
2433e 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20  PRDUP_REDUCE);. 
2433f 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
24340 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74  lete(db, pSelect
24341 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  );.  if( db->mal
24342 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
24343 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69   return;.  }.  i
24344 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73  f( !db->init.bus
24345 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  y ){.    sqlite3
24346 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d  ViewGetColumnNam
24347 65 73 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20  es(pParse, p);. 
24348 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20   }..  /* Locate 
24349 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 43  the end of the C
2434a 52 45 41 54 45 20 56 49 45 57 20 73 74 61 74 65  REATE VIEW state
2434b 6d 65 6e 74 2e 20 20 4d 61 6b 65 20 73 45 6e 64  ment.  Make sEnd
2434c 20 70 6f 69 6e 74 20 74 6f 0a 20 20 2a 2a 20 74   point to.  ** t
2434d 68 65 20 65 6e 64 2e 0a 20 20 2a 2f 0a 20 20 73  he end..  */.  s
2434e 45 6e 64 20 3d 20 70 50 61 72 73 65 2d 3e 73 4c  End = pParse->sL
2434f 61 73 74 54 6f 6b 65 6e 3b 0a 20 20 69 66 28 20  astToken;.  if( 
24350 41 4c 57 41 59 53 28 73 45 6e 64 2e 7a 5b 30 5d  ALWAYS(sEnd.z[0]
24351 21 3d 30 29 20 26 26 20 73 45 6e 64 2e 7a 5b 30  !=0) && sEnd.z[0
24352 5d 21 3d 27 3b 27 20 29 7b 0a 20 20 20 20 73 45  ]!=';' ){.    sE
24353 6e 64 2e 7a 20 2b 3d 20 73 45 6e 64 2e 6e 3b 0a  nd.z += sEnd.n;.
24354 20 20 7d 0a 20 20 73 45 6e 64 2e 6e 20 3d 20 30    }.  sEnd.n = 0
24355 3b 0a 20 20 6e 20 3d 20 28 69 6e 74 29 28 73 45  ;.  n = (int)(sE
24356 6e 64 2e 7a 20 2d 20 70 42 65 67 69 6e 2d 3e 7a  nd.z - pBegin->z
24357 29 3b 0a 20 20 7a 20 3d 20 70 42 65 67 69 6e 2d  );.  z = pBegin-
24358 3e 7a 3b 0a 20 20 77 68 69 6c 65 28 20 41 4c 57  >z;.  while( ALW
24359 41 59 53 28 6e 3e 30 29 20 26 26 20 73 71 6c 69  AYS(n>0) && sqli
2435a 74 65 33 49 73 73 70 61 63 65 28 7a 5b 6e 2d 31  te3Isspace(z[n-1
2435b 5d 29 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 73  ]) ){ n--; }.  s
2435c 45 6e 64 2e 7a 20 3d 20 26 7a 5b 6e 2d 31 5d 3b  End.z = &z[n-1];
2435d 0a 20 20 73 45 6e 64 2e 6e 20 3d 20 31 3b 0a 0a  .  sEnd.n = 1;..
2435e 20 20 2f 2a 20 55 73 65 20 73 71 6c 69 74 65 33    /* Use sqlite3
2435f 45 6e 64 54 61 62 6c 65 28 29 20 74 6f 20 61 64  EndTable() to ad
24360 64 20 74 68 65 20 76 69 65 77 20 74 6f 20 74 68  d the view to th
24361 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20  e SQLITE_MASTER 
24362 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74  table */.  sqlit
24363 65 33 45 6e 64 54 61 62 6c 65 28 70 50 61 72 73  e3EndTable(pPars
24364 65 2c 20 30 2c 20 26 73 45 6e 64 2c 20 30 29 3b  e, 0, &sEnd, 0);
24365 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 23 65 6e  .  return;.}.#en
24366 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
24367 49 54 5f 56 49 45 57 20 2a 2f 0a 0a 23 69 66 20  IT_VIEW */..#if 
24368 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
24369 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64  OMIT_VIEW) || !d
2436a 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
2436b 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29  IT_VIRTUALTABLE)
2436c 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65  ./*.** The Table
2436d 20 73 74 72 75 63 74 75 72 65 20 70 54 61 62 6c   structure pTabl
2436e 65 20 69 73 20 72 65 61 6c 6c 79 20 61 20 56 49  e is really a VI
2436f 45 57 2e 20 20 46 69 6c 6c 20 69 6e 20 74 68 65  EW.  Fill in the
24370 20 6e 61 6d 65 73 20 6f 66 0a 2a 2a 20 74 68 65   names of.** the
24371 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20   columns of the 
24372 76 69 65 77 20 69 6e 20 74 68 65 20 70 54 61 62  view in the pTab
24373 6c 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 52  le structure.  R
24374 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
24375 0a 2a 2a 20 6f 66 20 65 72 72 6f 72 73 2e 20 20  .** of errors.  
24376 49 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 73  If an error is s
24377 65 65 6e 20 6c 65 61 76 65 20 61 6e 20 65 72 72  een leave an err
24378 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50  or message in pP
24379 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a  arse->zErrMsg..*
2437a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2437b 20 69 6e 74 20 73 71 6c 69 74 65 33 56 69 65 77   int sqlite3View
2437c 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50  GetColumnNames(P
2437d 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61  arse *pParse, Ta
2437e 62 6c 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20  ble *pTable){.  
2437f 54 61 62 6c 65 20 2a 70 53 65 6c 54 61 62 3b 20  Table *pSelTab; 
24380 20 20 2f 2a 20 41 20 66 61 6b 65 20 74 61 62 6c    /* A fake tabl
24381 65 20 66 72 6f 6d 20 77 68 69 63 68 20 77 65 20  e from which we 
24382 67 65 74 20 74 68 65 20 72 65 73 75 6c 74 20 73  get the result s
24383 65 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  et */.  Select *
24384 70 53 65 6c 3b 20 20 20 20 20 2f 2a 20 43 6f 70  pSel;     /* Cop
24385 79 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20  y of the SELECT 
24386 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20  that implements 
24387 74 68 65 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e  the view */.  in
24388 74 20 6e 45 72 72 20 3d 20 30 3b 20 20 20 20 20  t nErr = 0;     
24389 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72  /* Number of err
2438a 6f 72 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20  ors encountered 
2438b 2a 2f 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20  */.  int n;     
2438c 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72         /* Tempor
2438d 61 72 69 6c 79 20 68 6f 6c 64 73 20 74 68 65 20  arily holds the 
2438e 6e 75 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72  number of cursor
2438f 73 20 61 73 73 69 67 6e 65 64 20 2a 2f 0a 20 20  s assigned */.  
24390 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
24391 61 72 73 65 2d 3e 64 62 3b 20 20 2f 2a 20 44 61  arse->db;  /* Da
24392 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
24393 6e 20 66 6f 72 20 6d 61 6c 6c 6f 63 20 65 72 72  n for malloc err
24394 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78  ors */.  int (*x
24395 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  Auth)(void*,int,
24396 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
24397 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
24398 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  ar*,const char*)
24399 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54 61  ;..  assert( pTa
2439a 62 6c 65 20 29 3b 0a 0a 23 69 66 6e 64 65 66 20  ble );..#ifndef 
2439b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2439c 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 73  UALTABLE.  if( s
2439d 71 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 43 6f  qlite3VtabCallCo
2439e 6e 6e 65 63 74 28 70 50 61 72 73 65 2c 20 70 54  nnect(pParse, pT
2439f 61 62 6c 65 29 20 29 7b 0a 20 20 20 20 72 65 74  able) ){.    ret
243a0 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
243a1 3b 0a 20 20 7d 0a 20 20 69 66 28 20 49 73 56 69  ;.  }.  if( IsVi
243a2 72 74 75 61 6c 28 70 54 61 62 6c 65 29 20 29 20  rtual(pTable) ) 
243a3 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66  return 0;.#endif
243a4 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
243a5 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 2f 2a 20  _OMIT_VIEW.  /* 
243a6 41 20 70 6f 73 69 74 69 76 65 20 6e 43 6f 6c 20  A positive nCol 
243a7 6d 65 61 6e 73 20 74 68 65 20 63 6f 6c 75 6d 6e  means the column
243a8 73 20 6e 61 6d 65 73 20 66 6f 72 20 74 68 69 73  s names for this
243a9 20 76 69 65 77 20 61 72 65 0a 20 20 2a 2a 20 61   view are.  ** a
243aa 6c 72 65 61 64 79 20 6b 6e 6f 77 6e 2e 0a 20 20  lready known..  
243ab 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 6c 65 2d  */.  if( pTable-
243ac 3e 6e 43 6f 6c 3e 30 20 29 20 72 65 74 75 72 6e  >nCol>0 ) return
243ad 20 30 3b 0a 0a 20 20 2f 2a 20 41 20 6e 65 67 61   0;..  /* A nega
243ae 74 69 76 65 20 6e 43 6f 6c 20 69 73 20 61 20 73  tive nCol is a s
243af 70 65 63 69 61 6c 20 6d 61 72 6b 65 72 20 6d 65  pecial marker me
243b0 61 6e 69 6e 67 20 74 68 61 74 20 77 65 20 61 72  aning that we ar
243b1 65 20 63 75 72 72 65 6e 74 6c 79 0a 20 20 2a 2a  e currently.  **
243b2 20 74 72 79 69 6e 67 20 74 6f 20 63 6f 6d 70 75   trying to compu
243b3 74 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  te the column na
243b4 6d 65 73 2e 20 20 49 66 20 77 65 20 65 6e 74 65  mes.  If we ente
243b5 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  r this routine w
243b6 69 74 68 0a 20 20 2a 2a 20 61 20 6e 65 67 61 74  ith.  ** a negat
243b7 69 76 65 20 6e 43 6f 6c 2c 20 69 74 20 6d 65 61  ive nCol, it mea
243b8 6e 73 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 76  ns two or more v
243b9 69 65 77 73 20 66 6f 72 6d 20 61 20 6c 6f 6f 70  iews form a loop
243ba 2c 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20 2a  , like this:.  *
243bb 2a 0a 20 20 2a 2a 20 20 20 20 20 43 52 45 41 54  *.  **     CREAT
243bc 45 20 56 49 45 57 20 6f 6e 65 20 41 53 20 53 45  E VIEW one AS SE
243bd 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 77 6f 3b  LECT * FROM two;
243be 0a 20 20 2a 2a 20 20 20 20 20 43 52 45 41 54 45  .  **     CREATE
243bf 20 56 49 45 57 20 74 77 6f 20 41 53 20 53 45 4c   VIEW two AS SEL
243c0 45 43 54 20 2a 20 46 52 4f 4d 20 6f 6e 65 3b 0a  ECT * FROM one;.
243c1 20 20 2a 2a 0a 20 20 2a 2a 20 41 63 74 75 61 6c    **.  ** Actual
243c2 6c 79 2c 20 74 68 65 20 65 72 72 6f 72 20 61 62  ly, the error ab
243c3 6f 76 65 20 69 73 20 6e 6f 77 20 63 61 75 67 68  ove is now caugh
243c4 74 20 70 72 69 6f 72 20 74 6f 20 72 65 61 63 68  t prior to reach
243c5 69 6e 67 20 74 68 69 73 20 70 6f 69 6e 74 2e 0a  ing this point..
243c6 20 20 2a 2a 20 42 75 74 20 74 68 65 20 66 6f 6c    ** But the fol
243c7 6c 6f 77 69 6e 67 20 74 65 73 74 20 69 73 20 73  lowing test is s
243c8 74 69 6c 6c 20 69 6d 70 6f 72 74 61 6e 74 20 61  till important a
243c9 73 20 69 74 20 64 6f 65 73 20 63 6f 6d 65 20 75  s it does come u
243ca 70 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 66 6f  p.  ** in the fo
243cb 6c 6c 6f 77 69 6e 67 3a 0a 20 20 2a 2a 20 0a 20  llowing:.  ** . 
243cc 20 2a 2a 20 20 20 20 20 43 52 45 41 54 45 20 54   **     CREATE T
243cd 41 42 4c 45 20 6d 61 69 6e 2e 65 78 31 28 61 29  ABLE main.ex1(a)
243ce 3b 0a 20 20 2a 2a 20 20 20 20 20 43 52 45 41 54  ;.  **     CREAT
243cf 45 20 54 45 4d 50 20 56 49 45 57 20 65 78 31 20  E TEMP VIEW ex1 
243d0 41 53 20 53 45 4c 45 43 54 20 61 20 46 52 4f 4d  AS SELECT a FROM
243d1 20 65 78 31 3b 0a 20 20 2a 2a 20 20 20 20 20 53   ex1;.  **     S
243d2 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 65 6d  ELECT * FROM tem
243d3 70 2e 65 78 31 3b 0a 20 20 2a 2f 0a 20 20 69 66  p.ex1;.  */.  if
243d4 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3c 30  ( pTable->nCol<0
243d5 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
243d6 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
243d7 22 76 69 65 77 20 25 73 20 69 73 20 63 69 72 63  "view %s is circ
243d8 75 6c 61 72 6c 79 20 64 65 66 69 6e 65 64 22 2c  ularly defined",
243d9 20 70 54 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29 3b   pTable->zName);
243da 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
243db 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61   }.  assert( pTa
243dc 62 6c 65 2d 3e 6e 43 6f 6c 3e 3d 30 20 29 3b 0a  ble->nCol>=0 );.
243dd 0a 20 20 2f 2a 20 49 66 20 77 65 20 67 65 74 20  .  /* If we get 
243de 74 68 69 73 20 66 61 72 2c 20 69 74 20 6d 65 61  this far, it mea
243df 6e 73 20 77 65 20 6e 65 65 64 20 74 6f 20 63 6f  ns we need to co
243e0 6d 70 75 74 65 20 74 68 65 20 74 61 62 6c 65 20  mpute the table 
243e1 6e 61 6d 65 73 2e 0a 20 20 2a 2a 20 4e 6f 74 65  names..  ** Note
243e2 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 20 74   that the call t
243e3 6f 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53  o sqlite3ResultS
243e4 65 74 4f 66 53 65 6c 65 63 74 28 29 20 77 69 6c  etOfSelect() wil
243e5 6c 20 65 78 70 61 6e 64 20 61 6e 79 0a 20 20 2a  l expand any.  *
243e6 2a 20 22 2a 22 20 65 6c 65 6d 65 6e 74 73 20 69  * "*" elements i
243e7 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 73 65  n the results se
243e8 74 20 6f 66 20 74 68 65 20 76 69 65 77 20 61 6e  t of the view an
243e9 64 20 77 69 6c 6c 20 61 73 73 69 67 6e 20 63 75  d will assign cu
243ea 72 73 6f 72 73 0a 20 20 2a 2a 20 74 6f 20 74 68  rsors.  ** to th
243eb 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68  e elements of th
243ec 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20  e FROM clause.  
243ed 42 75 74 20 77 65 20 64 6f 20 6e 6f 74 20 77 61  But we do not wa
243ee 6e 74 20 74 68 65 73 65 20 63 68 61 6e 67 65 73  nt these changes
243ef 0a 20 20 2a 2a 20 74 6f 20 62 65 20 70 65 72 6d  .  ** to be perm
243f0 61 6e 65 6e 74 2e 20 20 53 6f 20 74 68 65 20 63  anent.  So the c
243f1 6f 6d 70 75 74 61 74 69 6f 6e 20 69 73 20 64 6f  omputation is do
243f2 6e 65 20 6f 6e 20 61 20 63 6f 70 79 20 6f 66 20  ne on a copy of 
243f3 74 68 65 20 53 45 4c 45 43 54 0a 20 20 2a 2a 20  the SELECT.  ** 
243f4 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 64  statement that d
243f5 65 66 69 6e 65 73 20 74 68 65 20 76 69 65 77 2e  efines the view.
243f6 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
243f7 70 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 20  pTable->pSelect 
243f8 29 3b 0a 20 20 70 53 65 6c 20 3d 20 73 71 6c 69  );.  pSel = sqli
243f9 74 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c  te3SelectDup(db,
243fa 20 70 54 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74   pTable->pSelect
243fb 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 53 65 6c  , 0);.  if( pSel
243fc 20 29 7b 0a 20 20 20 20 75 38 20 65 6e 61 62 6c   ){.    u8 enabl
243fd 65 4c 6f 6f 6b 61 73 69 64 65 20 3d 20 64 62 2d  eLookaside = db-
243fe 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62  >lookaside.bEnab
243ff 6c 65 64 3b 0a 20 20 20 20 6e 20 3d 20 70 50 61  led;.    n = pPa
24400 72 73 65 2d 3e 6e 54 61 62 3b 0a 20 20 20 20 73  rse->nTab;.    s
24401 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 73 73  qlite3SrcListAss
24402 69 67 6e 43 75 72 73 6f 72 73 28 70 50 61 72 73  ignCursors(pPars
24403 65 2c 20 70 53 65 6c 2d 3e 70 53 72 63 29 3b 0a  e, pSel->pSrc);.
24404 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c      pTable->nCol
24405 20 3d 20 2d 31 3b 0a 20 20 20 20 64 62 2d 3e 6c   = -1;.    db->l
24406 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65  ookaside.bEnable
24407 64 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53  d = 0;.#ifndef S
24408 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
24409 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 78 41 75  RIZATION.    xAu
2440a 74 68 20 3d 20 64 62 2d 3e 78 41 75 74 68 3b 0a  th = db->xAuth;.
2440b 20 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20      db->xAuth = 
2440c 30 3b 0a 20 20 20 20 70 53 65 6c 54 61 62 20 3d  0;.    pSelTab =
2440d 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65   sqlite3ResultSe
2440e 74 4f 66 53 65 6c 65 63 74 28 70 50 61 72 73 65  tOfSelect(pParse
2440f 2c 20 70 53 65 6c 29 3b 0a 20 20 20 20 64 62 2d  , pSel);.    db-
24410 3e 78 41 75 74 68 20 3d 20 78 41 75 74 68 3b 0a  >xAuth = xAuth;.
24411 23 65 6c 73 65 0a 20 20 20 20 70 53 65 6c 54 61  #else.    pSelTa
24412 62 20 3d 20 73 71 6c 69 74 65 33 52 65 73 75 6c  b = sqlite3Resul
24413 74 53 65 74 4f 66 53 65 6c 65 63 74 28 70 50 61  tSetOfSelect(pPa
24414 72 73 65 2c 20 70 53 65 6c 29 3b 0a 23 65 6e 64  rse, pSel);.#end
24415 69 66 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61  if.    db->looka
24416 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20  side.bEnabled = 
24417 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b  enableLookaside;
24418 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61  .    pParse->nTa
24419 62 20 3d 20 6e 3b 0a 20 20 20 20 69 66 28 20 70  b = n;.    if( p
2441a 53 65 6c 54 61 62 20 29 7b 0a 20 20 20 20 20 20  SelTab ){.      
2441b 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e  assert( pTable->
2441c 61 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20  aCol==0 );.     
2441d 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20   pTable->nCol = 
2441e 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20  pSelTab->nCol;. 
2441f 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 61 43 6f       pTable->aCo
24420 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f  l = pSelTab->aCo
24421 6c 3b 0a 20 20 20 20 20 20 70 53 65 6c 54 61 62  l;.      pSelTab
24422 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20  ->nCol = 0;.    
24423 20 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 20    pSelTab->aCol 
24424 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 0;.      sqlit
24425 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 53  e3DeleteTable(pS
24426 65 6c 54 61 62 29 3b 0a 20 20 20 20 20 20 70 54  elTab);.      pT
24427 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 2d 3e 66  able->pSchema->f
24428 6c 61 67 73 20 7c 3d 20 44 42 5f 55 6e 72 65 73  lags |= DB_Unres
24429 65 74 56 69 65 77 73 3b 0a 20 20 20 20 7d 65 6c  etViews;.    }el
2442a 73 65 7b 0a 20 20 20 20 20 20 70 54 61 62 6c 65  se{.      pTable
2442b 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20  ->nCol = 0;.    
2442c 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 7d 0a    nErr++;.    }.
2442d 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
2442e 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c  tDelete(db, pSel
2442f 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20  );.  } else {.  
24430 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 23 65    nErr++;.  }.#e
24431 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
24432 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 20 20 72 65  MIT_VIEW */.  re
24433 74 75 72 6e 20 6e 45 72 72 3b 20 20 0a 7d 0a 23  turn nErr;  .}.#
24434 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
24435 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  d(SQLITE_OMIT_VI
24436 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28  EW) || !defined(
24437 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
24438 55 41 4c 54 41 42 4c 45 29 20 2a 2f 0a 0a 23 69  UALTABLE) */..#i
24439 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2443a 54 5f 56 49 45 57 0a 2f 2a 0a 2a 2a 20 43 6c 65  T_VIEW./*.** Cle
2443b 61 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  ar the column na
2443c 6d 65 73 20 66 72 6f 6d 20 65 76 65 72 79 20 56  mes from every V
2443d 49 45 57 20 69 6e 20 64 61 74 61 62 61 73 65 20  IEW in database 
2443e 69 64 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  idx..*/.static v
2443f 6f 69 64 20 73 71 6c 69 74 65 56 69 65 77 52 65  oid sqliteViewRe
24440 73 65 74 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a  setAll(sqlite3 *
24441 64 62 2c 20 69 6e 74 20 69 64 78 29 7b 0a 20 20  db, int idx){.  
24442 48 61 73 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 69  HashElem *i;.  i
24443 66 28 20 21 44 62 48 61 73 50 72 6f 70 65 72 74  f( !DbHasPropert
24444 79 28 64 62 2c 20 69 64 78 2c 20 44 42 5f 55 6e  y(db, idx, DB_Un
24445 72 65 73 65 74 56 69 65 77 73 29 20 29 20 72 65  resetViews) ) re
24446 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 73 71  turn;.  for(i=sq
24447 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26 64  liteHashFirst(&d
24448 62 2d 3e 61 44 62 5b 69 64 78 5d 2e 70 53 63 68  b->aDb[idx].pSch
24449 65 6d 61 2d 3e 74 62 6c 48 61 73 68 29 3b 20 69  ema->tblHash); i
2444a 3b 69 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78  ;i=sqliteHashNex
2444b 74 28 69 29 29 7b 0a 20 20 20 20 54 61 62 6c 65  t(i)){.    Table
2444c 20 2a 70 54 61 62 20 3d 20 73 71 6c 69 74 65 48   *pTab = sqliteH
2444d 61 73 68 44 61 74 61 28 69 29 3b 0a 20 20 20 20  ashData(i);.    
2444e 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63  if( pTab->pSelec
2444f 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t ){.      sqlit
24450 65 52 65 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65  eResetColumnName
24451 73 28 70 54 61 62 29 3b 0a 20 20 20 20 7d 0a 20  s(pTab);.    }. 
24452 20 7d 0a 20 20 44 62 43 6c 65 61 72 50 72 6f 70   }.  DbClearProp
24453 65 72 74 79 28 64 62 2c 20 69 64 78 2c 20 44 42  erty(db, idx, DB
24454 5f 55 6e 72 65 73 65 74 56 69 65 77 73 29 3b 0a  _UnresetViews);.
24455 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  }.#else.# define
24456 20 73 71 6c 69 74 65 56 69 65 77 52 65 73 65 74   sqliteViewReset
24457 41 6c 6c 28 41 2c 42 29 0a 23 65 6e 64 69 66 20  All(A,B).#endif 
24458 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
24459 49 45 57 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  IEW */../*.** Th
2445a 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
2445b 61 6c 6c 65 64 20 62 79 20 74 68 65 20 56 44 42  alled by the VDB
2445c 45 20 74 6f 20 61 64 6a 75 73 74 20 74 68 65 20  E to adjust the 
2445d 69 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a  internal schema.
2445e 2a 2a 20 75 73 65 64 20 62 79 20 53 51 4c 69 74  ** used by SQLit
2445f 65 20 77 68 65 6e 20 74 68 65 20 62 74 72 65 65  e when the btree
24460 20 6c 61 79 65 72 20 6d 6f 76 65 73 20 61 20 74   layer moves a t
24461 61 62 6c 65 20 72 6f 6f 74 20 70 61 67 65 2e 20  able root page. 
24462 54 68 65 0a 2a 2a 20 72 6f 6f 74 2d 70 61 67 65  The.** root-page
24463 20 6f 66 20 61 20 74 61 62 6c 65 20 6f 72 20 69   of a table or i
24464 6e 64 65 78 20 69 6e 20 64 61 74 61 62 61 73 65  ndex in database
24465 20 69 44 62 20 68 61 73 20 63 68 61 6e 67 65 64   iDb has changed
24466 20 66 72 6f 6d 20 69 46 72 6f 6d 0a 2a 2a 20 74   from iFrom.** t
24467 6f 20 69 54 6f 2e 0a 2a 2a 0a 2a 2a 20 54 69 63  o iTo..**.** Tic
24468 6b 65 74 20 23 31 37 32 38 3a 20 20 54 68 65 20  ket #1728:  The 
24469 73 79 6d 62 6f 6c 20 74 61 62 6c 65 20 6d 69 67  symbol table mig
2446a 68 74 20 73 74 69 6c 6c 20 63 6f 6e 74 61 69 6e  ht still contain
2446b 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20   information.** 
2446c 6f 6e 20 74 61 62 6c 65 73 20 61 6e 64 2f 6f 72  on tables and/or
2446d 20 69 6e 64 69 63 65 73 20 74 68 61 74 20 61 72   indices that ar
2446e 65 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f 66  e the process of
2446f 20 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a   being deleted..
24470 2a 2a 20 49 66 20 79 6f 75 20 61 72 65 20 75 6e  ** If you are un
24471 6c 75 63 6b 79 2c 20 6f 6e 65 20 6f 66 20 74 68  lucky, one of th
24472 6f 73 65 20 64 65 6c 65 74 65 64 20 69 6e 64 69  ose deleted indi
24473 63 65 73 20 6f 72 20 74 61 62 6c 65 73 20 6d 69  ces or tables mi
24474 67 68 74 0a 2a 2a 20 68 61 76 65 20 74 68 65 20  ght.** have the 
24475 73 61 6d 65 20 72 6f 6f 74 70 61 67 65 20 6e 75  same rootpage nu
24476 6d 62 65 72 20 61 73 20 74 68 65 20 72 65 61 6c  mber as the real
24477 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
24478 74 68 61 74 20 69 73 0a 2a 2a 20 62 65 69 6e 67  that is.** being
24479 20 6d 6f 76 65 64 2e 20 20 53 6f 20 77 65 20 63   moved.  So we c
2447a 61 6e 6e 6f 74 20 73 74 6f 70 20 73 65 61 72 63  annot stop searc
2447b 68 69 6e 67 20 61 66 74 65 72 20 74 68 65 20 66  hing after the f
2447c 69 72 73 74 20 6d 61 74 63 68 20 0a 2a 2a 20 62  irst match .** b
2447d 65 63 61 75 73 65 20 74 68 65 20 66 69 72 73 74  ecause the first
2447e 20 6d 61 74 63 68 20 6d 69 67 68 74 20 62 65 20   match might be 
2447f 66 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 64  for one of the d
24480 65 6c 65 74 65 64 20 69 6e 64 69 63 65 73 0a 2a  eleted indices.*
24481 2a 20 6f 72 20 74 61 62 6c 65 73 20 61 6e 64 20  * or tables and 
24482 6e 6f 74 20 74 68 65 20 74 61 62 6c 65 2f 69 6e  not the table/in
24483 64 65 78 20 74 68 61 74 20 69 73 20 61 63 74 75  dex that is actu
24484 61 6c 6c 79 20 62 65 69 6e 67 20 6d 6f 76 65 64  ally being moved
24485 2e 0a 2a 2a 20 57 65 20 6d 75 73 74 20 63 6f 6e  ..** We must con
24486 74 69 6e 75 65 20 6c 6f 6f 70 69 6e 67 20 75 6e  tinue looping un
24487 74 69 6c 20 61 6c 6c 20 74 61 62 6c 65 73 20 61  til all tables a
24488 6e 64 20 69 6e 64 69 63 65 73 20 77 69 74 68 0a  nd indices with.
24489 2a 2a 20 72 6f 6f 74 70 61 67 65 3d 3d 69 46 72  ** rootpage==iFr
2448a 6f 6d 20 68 61 76 65 20 62 65 65 6e 20 63 6f 6e  om have been con
2448b 76 65 72 74 65 64 20 74 6f 20 68 61 76 65 20 61  verted to have a
2448c 20 72 6f 6f 74 70 61 67 65 20 6f 66 20 69 54 6f   rootpage of iTo
2448d 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  .** in order to 
2448e 62 65 20 63 65 72 74 61 69 6e 20 74 68 61 74 20  be certain that 
2448f 77 65 20 67 6f 74 20 74 68 65 20 72 69 67 68 74  we got the right
24490 20 6f 6e 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66   one..*/.#ifndef
24491 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
24492 4f 56 41 43 55 55 4d 0a 53 51 4c 49 54 45 5f 50  OVACUUM.SQLITE_P
24493 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
24494 74 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64  te3RootPageMoved
24495 28 44 62 20 2a 70 44 62 2c 20 69 6e 74 20 69 46  (Db *pDb, int iF
24496 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 29 7b 0a 20  rom, int iTo){. 
24497 20 48 61 73 68 45 6c 65 6d 20 2a 70 45 6c 65 6d   HashElem *pElem
24498 3b 0a 20 20 48 61 73 68 20 2a 70 48 61 73 68 3b  ;.  Hash *pHash;
24499 0a 0a 20 20 70 48 61 73 68 20 3d 20 26 70 44 62  ..  pHash = &pDb
2449a 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61  ->pSchema->tblHa
2449b 73 68 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d  sh;.  for(pElem=
2449c 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28  sqliteHashFirst(
2449d 70 48 61 73 68 29 3b 20 70 45 6c 65 6d 3b 20 70  pHash); pElem; p
2449e 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e  Elem=sqliteHashN
2449f 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20  ext(pElem)){.   
244a0 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73   Table *pTab = s
244a1 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 45  qliteHashData(pE
244a2 6c 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 70 54  lem);.    if( pT
244a3 61 62 2d 3e 74 6e 75 6d 3d 3d 69 46 72 6f 6d 20  ab->tnum==iFrom 
244a4 29 7b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 74  ){.      pTab->t
244a5 6e 75 6d 20 3d 20 69 54 6f 3b 0a 20 20 20 20 7d  num = iTo;.    }
244a6 0a 20 20 7d 0a 20 20 70 48 61 73 68 20 3d 20 26  .  }.  pHash = &
244a7 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 64  pDb->pSchema->id
244a8 78 48 61 73 68 3b 0a 20 20 66 6f 72 28 70 45 6c  xHash;.  for(pEl
244a9 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72  em=sqliteHashFir
244aa 73 74 28 70 48 61 73 68 29 3b 20 70 45 6c 65 6d  st(pHash); pElem
244ab 3b 20 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61  ; pElem=sqliteHa
244ac 73 68 4e 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a  shNext(pElem)){.
244ad 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20      Index *pIdx 
244ae 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61  = sqliteHashData
244af 28 70 45 6c 65 6d 29 3b 0a 20 20 20 20 69 66 28  (pElem);.    if(
244b0 20 70 49 64 78 2d 3e 74 6e 75 6d 3d 3d 69 46 72   pIdx->tnum==iFr
244b1 6f 6d 20 29 7b 0a 20 20 20 20 20 20 70 49 64 78  om ){.      pIdx
244b2 2d 3e 74 6e 75 6d 20 3d 20 69 54 6f 3b 0a 20 20  ->tnum = iTo;.  
244b3 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66    }.  }.}.#endif
244b4 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 63 6f  ../*.** Write co
244b5 64 65 20 74 6f 20 65 72 61 73 65 20 74 68 65 20  de to erase the 
244b6 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74 2d  table with root-
244b7 70 61 67 65 20 69 54 61 62 6c 65 20 66 72 6f 6d  page iTable from
244b8 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 0a 2a   database iDb..*
244b9 2a 20 41 6c 73 6f 20 77 72 69 74 65 20 63 6f 64  * Also write cod
244ba 65 20 74 6f 20 6d 6f 64 69 66 79 20 74 68 65 20  e to modify the 
244bb 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
244bc 62 6c 65 20 61 6e 64 20 69 6e 74 65 72 6e 61 6c  ble and internal
244bd 20 73 63 68 65 6d 61 0a 2a 2a 20 69 66 20 61 20   schema.** if a 
244be 72 6f 6f 74 2d 70 61 67 65 20 6f 66 20 61 6e 6f  root-page of ano
244bf 74 68 65 72 20 74 61 62 6c 65 20 69 73 20 6d 6f  ther table is mo
244c0 76 65 64 20 62 79 20 74 68 65 20 62 74 72 65 65  ved by the btree
244c1 2d 6c 61 79 65 72 20 77 68 69 6c 73 74 0a 2a 2a  -layer whilst.**
244c2 20 65 72 61 73 69 6e 67 20 69 54 61 62 6c 65 20   erasing iTable 
244c3 28 74 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e  (this can happen
244c4 20 77 69 74 68 20 61 6e 20 61 75 74 6f 2d 76 61   with an auto-va
244c5 63 75 75 6d 20 64 61 74 61 62 61 73 65 29 2e 0a  cuum database)..
244c6 2a 2f 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20  */ .static void 
244c7 64 65 73 74 72 6f 79 52 6f 6f 74 50 61 67 65 28  destroyRootPage(
244c8 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69  Parse *pParse, i
244c9 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 69  nt iTable, int i
244ca 44 62 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  Db){.  Vdbe *v =
244cb 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
244cc 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 20 72  pParse);.  int r
244cd 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  1 = sqlite3GetTe
244ce 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
244cf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
244d0 70 33 28 76 2c 20 4f 50 5f 44 65 73 74 72 6f 79  p3(v, OP_Destroy
244d1 2c 20 69 54 61 62 6c 65 2c 20 72 31 2c 20 69 44  , iTable, r1, iD
244d2 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 4d 61 79  b);.  sqlite3May
244d3 41 62 6f 72 74 28 70 50 61 72 73 65 29 3b 0a 23  Abort(pParse);.#
244d4 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
244d5 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
244d6 2f 2a 20 4f 50 5f 44 65 73 74 72 6f 79 20 73 74  /* OP_Destroy st
244d7 6f 72 65 73 20 61 6e 20 69 6e 20 69 6e 74 65 67  ores an in integ
244d8 65 72 20 72 31 2e 20 49 66 20 74 68 69 73 20 69  er r1. If this i
244d9 6e 74 65 67 65 72 0a 20 20 2a 2a 20 69 73 20 6e  nteger.  ** is n
244da 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 69 74  on-zero, then it
244db 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70 61 67   is the root pag
244dc 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 20 74 61  e number of a ta
244dd 62 6c 65 20 6d 6f 76 65 64 20 74 6f 0a 20 20 2a  ble moved to.  *
244de 2a 20 6c 6f 63 61 74 69 6f 6e 20 69 54 61 62 6c  * location iTabl
244df 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  e. The following
244e0 20 63 6f 64 65 20 6d 6f 64 69 66 69 65 73 20 74   code modifies t
244e1 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  he sqlite_master
244e2 20 74 61 62 6c 65 20 74 6f 0a 20 20 2a 2a 20 72   table to.  ** r
244e3 65 66 6c 65 63 74 20 74 68 69 73 2e 0a 20 20 2a  eflect this..  *
244e4 2a 0a 20 20 2a 2a 20 54 68 65 20 22 23 4e 4e 4e  *.  ** The "#NNN
244e5 22 20 69 6e 20 74 68 65 20 53 51 4c 20 69 73 20  " in the SQL is 
244e6 61 20 73 70 65 63 69 61 6c 20 63 6f 6e 73 74 61  a special consta
244e7 6e 74 20 74 68 61 74 20 6d 65 61 6e 73 20 77 68  nt that means wh
244e8 61 74 65 76 65 72 20 76 61 6c 75 65 0a 20 20 2a  atever value.  *
244e9 2a 20 69 73 20 69 6e 20 72 65 67 69 73 74 65 72  * is in register
244ea 20 4e 4e 4e 2e 20 20 53 65 65 20 67 72 61 6d 6d   NNN.  See gramm
244eb 61 72 20 72 75 6c 65 73 20 61 73 73 6f 63 69 61  ar rules associa
244ec 74 65 64 20 77 69 74 68 20 74 68 65 20 54 4b 5f  ted with the TK_
244ed 52 45 47 49 53 54 45 52 0a 20 20 2a 2a 20 74 6f  REGISTER.  ** to
244ee 6b 65 6e 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  ken for addition
244ef 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
244f0 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4e 65    */.  sqlite3Ne
244f1 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65  stedParse(pParse
244f2 2c 20 0a 20 20 20 20 20 22 55 50 44 41 54 45 20  , .     "UPDATE 
244f3 25 51 2e 25 73 20 53 45 54 20 72 6f 6f 74 70 61  %Q.%s SET rootpa
244f4 67 65 3d 25 64 20 57 48 45 52 45 20 23 25 64 20  ge=%d WHERE #%d 
244f5 41 4e 44 20 72 6f 6f 74 70 61 67 65 3d 23 25 64  AND rootpage=#%d
244f6 22 2c 0a 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ",.     pParse->
244f7 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
244f8 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45  me, SCHEMA_TABLE
244f9 28 69 44 62 29 2c 20 69 54 61 62 6c 65 2c 20 72  (iDb), iTable, r
244fa 31 2c 20 72 31 29 3b 0a 23 65 6e 64 69 66 0a 20  1, r1);.#endif. 
244fb 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
244fc 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
244fd 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69  1);.}../*.** Wri
244fe 74 65 20 56 44 42 45 20 63 6f 64 65 20 74 6f 20  te VDBE code to 
244ff 65 72 61 73 65 20 74 61 62 6c 65 20 70 54 61 62  erase table pTab
24500 20 61 6e 64 20 61 6c 6c 20 61 73 73 6f 63 69 61   and all associa
24501 74 65 64 20 69 6e 64 69 63 65 73 20 6f 6e 20 64  ted indices on d
24502 69 73 6b 2e 0a 2a 2a 20 43 6f 64 65 20 74 6f 20  isk..** Code to 
24503 75 70 64 61 74 65 20 74 68 65 20 73 71 6c 69 74  update the sqlit
24504 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 73 20  e_master tables 
24505 61 6e 64 20 69 6e 74 65 72 6e 61 6c 20 73 63 68  and internal sch
24506 65 6d 61 20 64 65 66 69 6e 69 74 69 6f 6e 73 0a  ema definitions.
24507 2a 2a 20 69 6e 20 63 61 73 65 20 61 20 72 6f 6f  ** in case a roo
24508 74 2d 70 61 67 65 20 62 65 6c 6f 6e 67 69 6e 67  t-page belonging
24509 20 74 6f 20 61 6e 6f 74 68 65 72 20 74 61 62 6c   to another tabl
2450a 65 20 69 73 20 6d 6f 76 65 64 20 62 79 20 74 68  e is moved by th
2450b 65 20 62 74 72 65 65 20 6c 61 79 65 72 0a 2a 2a  e btree layer.**
2450c 20 69 73 20 61 6c 73 6f 20 61 64 64 65 64 20 28   is also added (
2450d 74 68 69 73 20 63 61 6e 20 68 61 70 70 65 6e 20  this can happen 
2450e 77 69 74 68 20 61 6e 20 61 75 74 6f 2d 76 61 63  with an auto-vac
2450f 75 75 6d 20 64 61 74 61 62 61 73 65 29 2e 0a 2a  uum database)..*
24510 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65  /.static void de
24511 73 74 72 6f 79 54 61 62 6c 65 28 50 61 72 73 65  stroyTable(Parse
24512 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20   *pParse, Table 
24513 2a 70 54 61 62 29 7b 0a 23 69 66 64 65 66 20 53  *pTab){.#ifdef S
24514 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
24515 41 43 55 55 4d 0a 20 20 49 6e 64 65 78 20 2a 70  ACUUM.  Index *p
24516 49 64 78 3b 0a 20 20 69 6e 74 20 69 44 62 20 3d  Idx;.  int iDb =
24517 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
24518 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
24519 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
2451a 3b 0a 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 50  ;.  destroyRootP
2451b 61 67 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  age(pParse, pTab
2451c 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20  ->tnum, iDb);.  
2451d 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70  for(pIdx=pTab->p
2451e 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
2451f 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  x=pIdx->pNext){.
24520 20 20 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 50      destroyRootP
24521 61 67 65 28 70 50 61 72 73 65 2c 20 70 49 64 78  age(pParse, pIdx
24522 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20 20  ->tnum, iDb);.  
24523 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 49 66 20  }.#else.  /* If 
24524 74 68 65 20 64 61 74 61 62 61 73 65 20 6d 61 79  the database may
24525 20 62 65 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   be auto-vacuum 
24526 63 61 70 61 62 6c 65 20 28 69 66 20 53 51 4c 49  capable (if SQLI
24527 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
24528 55 4d 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 64  UM.  ** is not d
24529 65 66 69 6e 65 64 29 2c 20 74 68 65 6e 20 69 74  efined), then it
2452a 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f   is important to
2452b 20 63 61 6c 6c 20 4f 50 5f 44 65 73 74 72 6f 79   call OP_Destroy
2452c 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 74 61 62   on the.  ** tab
2452d 6c 65 20 61 6e 64 20 69 6e 64 65 78 20 72 6f 6f  le and index roo
2452e 74 2d 70 61 67 65 73 20 69 6e 20 6f 72 64 65 72  t-pages in order
2452f 2c 20 73 74 61 72 74 69 6e 67 20 77 69 74 68 20  , starting with 
24530 74 68 65 20 6e 75 6d 65 72 69 63 61 6c 6c 79 20  the numerically 
24531 0a 20 20 2a 2a 20 6c 61 72 67 65 73 74 20 72 6f  .  ** largest ro
24532 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20  ot-page number. 
24533 54 68 69 73 20 67 75 61 72 61 6e 74 65 65 73 20  This guarantees 
24534 74 68 61 74 20 6e 6f 6e 65 20 6f 66 20 74 68 65  that none of the
24535 20 72 6f 6f 74 2d 70 61 67 65 73 0a 20 20 2a 2a   root-pages.  **
24536 20 74 6f 20 62 65 20 64 65 73 74 72 6f 79 65 64   to be destroyed
24537 20 69 73 20 72 65 6c 6f 63 61 74 65 64 20 62 79   is relocated by
24538 20 61 6e 20 65 61 72 6c 69 65 72 20 4f 50 5f 44   an earlier OP_D
24539 65 73 74 72 6f 79 2e 20 69 2e 65 2e 20 69 66 20  estroy. i.e. if 
2453a 74 68 65 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69  the.  ** followi
2453b 6e 67 20 77 65 72 65 20 63 6f 64 65 64 3a 0a 20  ng were coded:. 
2453c 20 2a 2a 0a 20 20 2a 2a 20 4f 50 5f 44 65 73 74   **.  ** OP_Dest
2453d 72 6f 79 20 34 20 30 0a 20 20 2a 2a 20 2e 2e 2e  roy 4 0.  ** ...
2453e 0a 20 20 2a 2a 20 4f 50 5f 44 65 73 74 72 6f 79  .  ** OP_Destroy
2453f 20 35 20 30 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61   5 0.  **.  ** a
24540 6e 64 20 72 6f 6f 74 20 70 61 67 65 20 35 20 68  nd root page 5 h
24541 61 70 70 65 6e 65 64 20 74 6f 20 62 65 20 74 68  appened to be th
24542 65 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d 70  e largest root-p
24543 61 67 65 20 6e 75 6d 62 65 72 20 69 6e 20 74 68  age number in th
24544 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2c  e.  ** database,
24545 20 74 68 65 6e 20 72 6f 6f 74 20 70 61 67 65 20   then root page 
24546 35 20 77 6f 75 6c 64 20 62 65 20 6d 6f 76 65 64  5 would be moved
24547 20 74 6f 20 70 61 67 65 20 34 20 62 79 20 74 68   to page 4 by th
24548 65 20 0a 20 20 2a 2a 20 22 4f 50 5f 44 65 73 74  e .  ** "OP_Dest
24549 72 6f 79 20 34 20 30 22 20 6f 70 63 6f 64 65 2e  roy 4 0" opcode.
2454a 20 54 68 65 20 73 75 62 73 65 71 75 65 6e 74 20   The subsequent 
2454b 22 4f 50 5f 44 65 73 74 72 6f 79 20 35 20 30 22  "OP_Destroy 5 0"
2454c 20 77 6f 75 6c 64 20 68 69 74 0a 20 20 2a 2a 20   would hit.  ** 
2454d 61 20 66 72 65 65 2d 6c 69 73 74 20 70 61 67 65  a free-list page
2454e 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  ..  */.  int iTa
2454f 62 20 3d 20 70 54 61 62 2d 3e 74 6e 75 6d 3b 0a  b = pTab->tnum;.
24550 20 20 69 6e 74 20 69 44 65 73 74 72 6f 79 65 64    int iDestroyed
24551 20 3d 20 30 3b 0a 0a 20 20 77 68 69 6c 65 28 20   = 0;..  while( 
24552 31 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a  1 ){.    Index *
24553 70 49 64 78 3b 0a 20 20 20 20 69 6e 74 20 69 4c  pIdx;.    int iL
24554 61 72 67 65 73 74 20 3d 20 30 3b 0a 0a 20 20 20  argest = 0;..   
24555 20 69 66 28 20 69 44 65 73 74 72 6f 79 65 64 3d   if( iDestroyed=
24556 3d 30 20 7c 7c 20 69 54 61 62 3c 69 44 65 73 74  =0 || iTab<iDest
24557 72 6f 79 65 64 20 29 7b 0a 20 20 20 20 20 20 69  royed ){.      i
24558 4c 61 72 67 65 73 74 20 3d 20 69 54 61 62 3b 0a  Largest = iTab;.
24559 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 70 49      }.    for(pI
2455a 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
2455b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
2455c 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20  ->pNext){.      
2455d 69 6e 74 20 69 49 64 78 20 3d 20 70 49 64 78 2d  int iIdx = pIdx-
2455e 3e 74 6e 75 6d 3b 0a 20 20 20 20 20 20 61 73 73  >tnum;.      ass
2455f 65 72 74 28 20 70 49 64 78 2d 3e 70 53 63 68 65  ert( pIdx->pSche
24560 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d  ma==pTab->pSchem
24561 61 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28  a );.      if( (
24562 69 44 65 73 74 72 6f 79 65 64 3d 3d 30 20 7c 7c  iDestroyed==0 ||
24563 20 28 69 49 64 78 3c 69 44 65 73 74 72 6f 79 65   (iIdx<iDestroye
24564 64 29 29 20 26 26 20 69 49 64 78 3e 69 4c 61 72  d)) && iIdx>iLar
24565 67 65 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20  gest ){.        
24566 69 4c 61 72 67 65 73 74 20 3d 20 69 49 64 78 3b  iLargest = iIdx;
24567 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
24568 20 20 20 69 66 28 20 69 4c 61 72 67 65 73 74 3d     if( iLargest=
24569 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =0 ){.      retu
2456a 72 6e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  rn;.    }else{. 
2456b 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 73       int iDb = s
2456c 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
2456d 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  dex(pParse->db, 
2456e 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
2456f 20 20 20 20 20 20 64 65 73 74 72 6f 79 52 6f 6f        destroyRoo
24570 74 50 61 67 65 28 70 50 61 72 73 65 2c 20 69 4c  tPage(pParse, iL
24571 61 72 67 65 73 74 2c 20 69 44 62 29 3b 0a 20 20  argest, iDb);.  
24572 20 20 20 20 69 44 65 73 74 72 6f 79 65 64 20 3d      iDestroyed =
24573 20 69 4c 61 72 67 65 73 74 3b 0a 20 20 20 20 7d   iLargest;.    }
24574 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  .  }.#endif.}../
24575 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
24576 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 64  e is called to d
24577 6f 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 61 20  o the work of a 
24578 44 52 4f 50 20 54 41 42 4c 45 20 73 74 61 74 65  DROP TABLE state
24579 6d 65 6e 74 2e 0a 2a 2a 20 70 4e 61 6d 65 20 69  ment..** pName i
2457a 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
2457b 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 64 72  e table to be dr
2457c 6f 70 70 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  opped..*/.SQLITE
2457d 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2457e 6c 69 74 65 33 44 72 6f 70 54 61 62 6c 65 28 50  lite3DropTable(P
2457f 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72  arse *pParse, Sr
24580 63 4c 69 73 74 20 2a 70 4e 61 6d 65 2c 20 69 6e  cList *pName, in
24581 74 20 69 73 56 69 65 77 2c 20 69 6e 74 20 6e 6f  t isView, int no
24582 45 72 72 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  Err){.  Table *p
24583 54 61 62 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  Tab;.  Vdbe *v;.
24584 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
24585 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e  pParse->db;.  in
24586 74 20 69 44 62 3b 0a 0a 20 20 69 66 28 20 64 62  t iDb;..  if( db
24587 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
24588 7b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f  {.    goto exit_
24589 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a  drop_table;.  }.
2458a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
2458b 2d 3e 6e 45 72 72 3d 3d 30 20 29 3b 0a 20 20 61  ->nErr==0 );.  a
2458c 73 73 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53  ssert( pName->nS
2458d 72 63 3d 3d 31 20 29 3b 0a 20 20 70 54 61 62 20  rc==1 );.  pTab 
2458e 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54  = sqlite3LocateT
2458f 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 73 56  able(pParse, isV
24590 69 65 77 2c 20 0a 20 20 20 20 20 20 20 20 20 20  iew, .          
24591 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24592 20 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e    pName->a[0].zN
24593 61 6d 65 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d  ame, pName->a[0]
24594 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 0a 20 20  .zDatabase);..  
24595 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20  if( pTab==0 ){. 
24596 20 20 20 69 66 28 20 6e 6f 45 72 72 20 29 7b 0a     if( noErr ){.
24597 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
24598 6f 72 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b  orClear(pParse);
24599 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20  .    }.    goto 
2459a 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b  exit_drop_table;
2459b 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c  .  }.  iDb = sql
2459c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
2459d 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  x(db, pTab->pSch
2459e 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ema);.  assert( 
2459f 69 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62  iDb>=0 && iDb<db
245a0 2d 3e 6e 44 62 20 29 3b 0a 0a 20 20 2f 2a 20 49  ->nDb );..  /* I
245a1 66 20 70 54 61 62 20 69 73 20 61 20 76 69 72 74  f pTab is a virt
245a2 75 61 6c 20 74 61 62 6c 65 2c 20 63 61 6c 6c 20  ual table, call 
245a3 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d  ViewGetColumnNam
245a4 65 73 28 29 20 74 6f 20 65 6e 73 75 72 65 0a 20  es() to ensure. 
245a5 20 2a 2a 20 69 74 20 69 73 20 69 6e 69 74 69 61   ** it is initia
245a6 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  lized..  */.  if
245a7 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  ( IsVirtual(pTab
245a8 29 20 26 26 20 73 71 6c 69 74 65 33 56 69 65 77  ) && sqlite3View
245a9 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70  GetColumnNames(p
245aa 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b 0a  Parse, pTab) ){.
245ab 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
245ac 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 23 69  op_table;.  }.#i
245ad 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
245ae 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a  T_AUTHORIZATION.
245af 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f 64 65    {.    int code
245b0 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
245b1 20 2a 7a 54 61 62 20 3d 20 53 43 48 45 4d 41 5f   *zTab = SCHEMA_
245b2 54 41 42 4c 45 28 69 44 62 29 3b 0a 20 20 20 20  TABLE(iDb);.    
245b3 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20  const char *zDb 
245b4 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
245b5 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20  Name;.    const 
245b6 63 68 61 72 20 2a 7a 41 72 67 32 20 3d 20 30 3b  char *zArg2 = 0;
245b7 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
245b8 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
245b9 2c 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c  , SQLITE_DELETE,
245ba 20 7a 54 61 62 2c 20 30 2c 20 7a 44 62 29 29 7b   zTab, 0, zDb)){
245bb 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
245bc 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20  _drop_table;.   
245bd 20 7d 0a 20 20 20 20 69 66 28 20 69 73 56 69 65   }.    if( isVie
245be 77 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21  w ){.      if( !
245bf 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69  OMIT_TEMPDB && i
245c0 44 62 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20  Db==1 ){.       
245c1 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44   code = SQLITE_D
245c2 52 4f 50 5f 54 45 4d 50 5f 56 49 45 57 3b 0a 20  ROP_TEMP_VIEW;. 
245c3 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
245c4 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54      code = SQLIT
245c5 45 5f 44 52 4f 50 5f 56 49 45 57 3b 0a 20 20 20  E_DROP_VIEW;.   
245c6 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
245c7 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
245c8 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65 20  TABLE.    }else 
245c9 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54  if( IsVirtual(pT
245ca 61 62 29 20 29 7b 0a 20 20 20 20 20 20 63 6f 64  ab) ){.      cod
245cb 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  e = SQLITE_DROP_
245cc 56 54 41 42 4c 45 3b 0a 20 20 20 20 20 20 7a 41  VTABLE;.      zA
245cd 72 67 32 20 3d 20 73 71 6c 69 74 65 33 47 65 74  rg2 = sqlite3Get
245ce 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29  VTable(db, pTab)
245cf 2d 3e 70 4d 6f 64 2d 3e 7a 4e 61 6d 65 3b 0a 23  ->pMod->zName;.#
245d0 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 7b  endif.    }else{
245d1 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49 54  .      if( !OMIT
245d2 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d  _TEMPDB && iDb==
245d3 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 64  1 ){.        cod
245d4 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  e = SQLITE_DROP_
245d5 54 45 4d 50 5f 54 41 42 4c 45 3b 0a 20 20 20 20  TEMP_TABLE;.    
245d6 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
245d7 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44   code = SQLITE_D
245d8 52 4f 50 5f 54 41 42 4c 45 3b 0a 20 20 20 20 20  ROP_TABLE;.     
245d9 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
245da 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
245db 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20  k(pParse, code, 
245dc 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 41 72  pTab->zName, zAr
245dd 67 32 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20  g2, zDb) ){.    
245de 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
245df 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20  _table;.    }.  
245e0 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
245e1 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
245e2 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 70 54  QLITE_DELETE, pT
245e3 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20 7a 44  ab->zName, 0, zD
245e4 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  b) ){.      goto
245e5 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65   exit_drop_table
245e6 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64  ;.    }.  }.#end
245e7 69 66 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  if.  if( sqlite3
245e8 53 74 72 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a  StrNICmp(pTab->z
245e9 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c  Name, "sqlite_",
245ea 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71   7)==0 ){.    sq
245eb 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
245ec 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20  arse, "table %s 
245ed 6d 61 79 20 6e 6f 74 20 62 65 20 64 72 6f 70 70  may not be dropp
245ee 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ed", pTab->zName
245ef 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  );.    goto exit
245f0 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 7d  _drop_table;.  }
245f1 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
245f2 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 2f 2a 20  _OMIT_VIEW.  /* 
245f3 45 6e 73 75 72 65 20 44 52 4f 50 20 54 41 42 4c  Ensure DROP TABL
245f4 45 20 69 73 20 6e 6f 74 20 75 73 65 64 20 6f 6e  E is not used on
245f5 20 61 20 76 69 65 77 2c 20 61 6e 64 20 44 52 4f   a view, and DRO
245f6 50 20 56 49 45 57 20 69 73 20 6e 6f 74 20 75 73  P VIEW is not us
245f7 65 64 0a 20 20 2a 2a 20 6f 6e 20 61 20 74 61 62  ed.  ** on a tab
245f8 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69  le..  */.  if( i
245f9 73 56 69 65 77 20 26 26 20 70 54 61 62 2d 3e 70  sView && pTab->p
245fa 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20  Select==0 ){.   
245fb 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
245fc 28 70 50 61 72 73 65 2c 20 22 75 73 65 20 44 52  (pParse, "use DR
245fd 4f 50 20 54 41 42 4c 45 20 74 6f 20 64 65 6c 65  OP TABLE to dele
245fe 74 65 20 74 61 62 6c 65 20 25 73 22 2c 20 70 54  te table %s", pT
245ff 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  ab->zName);.    
24600 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74  goto exit_drop_t
24601 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20  able;.  }.  if( 
24602 21 69 73 56 69 65 77 20 26 26 20 70 54 61 62 2d  !isView && pTab-
24603 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
24604 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
24605 70 50 61 72 73 65 2c 20 22 75 73 65 20 44 52 4f  pParse, "use DRO
24606 50 20 56 49 45 57 20 74 6f 20 64 65 6c 65 74 65  P VIEW to delete
24607 20 76 69 65 77 20 25 73 22 2c 20 70 54 61 62 2d   view %s", pTab-
24608 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74  >zName);.    got
24609 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c  o exit_drop_tabl
2460a 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  e;.  }.#endif.. 
2460b 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
2460c 65 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 20  e to remove the 
2460d 74 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 6d  table from the m
2460e 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20 2a 2a  aster table.  **
2460f 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 2a 2f 0a 20   on disk..  */. 
24610 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
24611 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
24612 66 28 20 76 20 29 7b 0a 20 20 20 20 54 72 69 67  f( v ){.    Trig
24613 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 0a 20  ger *pTrigger;. 
24614 20 20 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62     Db *pDb = &db
24615 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20  ->aDb[iDb];.    
24616 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
24617 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73  eOperation(pPars
24618 65 2c 20 31 2c 20 69 44 62 29 3b 0a 0a 23 69 66  e, 1, iDb);..#if
24619 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2461a 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
2461b 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
2461c 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 73  pTab) ){.      s
2461d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30  qlite3VdbeAddOp0
2461e 28 76 2c 20 4f 50 5f 56 42 65 67 69 6e 29 3b 0a  (v, OP_VBegin);.
2461f 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
24620 20 73 71 6c 69 74 65 33 46 6b 44 72 6f 70 54 61   sqlite3FkDropTa
24621 62 6c 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d  ble(pParse, pNam
24622 65 2c 20 70 54 61 62 29 3b 0a 0a 20 20 20 20 2f  e, pTab);..    /
24623 2a 20 44 72 6f 70 20 61 6c 6c 20 74 72 69 67 67  * Drop all trigg
24624 65 72 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ers associated w
24625 69 74 68 20 74 68 65 20 74 61 62 6c 65 20 62 65  ith the table be
24626 69 6e 67 20 64 72 6f 70 70 65 64 2e 20 43 6f 64  ing dropped. Cod
24627 65 0a 20 20 20 20 2a 2a 20 69 73 20 67 65 6e 65  e.    ** is gene
24628 72 61 74 65 64 20 74 6f 20 72 65 6d 6f 76 65 20  rated to remove 
24629 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 73 71 6c  entries from sql
2462a 69 74 65 5f 6d 61 73 74 65 72 20 61 6e 64 2f 6f  ite_master and/o
2462b 72 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 5f  r.    ** sqlite_
2462c 74 65 6d 70 5f 6d 61 73 74 65 72 20 69 66 20 72  temp_master if r
2462d 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a  equired..    */.
2462e 20 20 20 20 70 54 72 69 67 67 65 72 20 3d 20 73      pTrigger = s
2462f 71 6c 69 74 65 33 54 72 69 67 67 65 72 4c 69 73  qlite3TriggerLis
24630 74 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b  t(pParse, pTab);
24631 0a 20 20 20 20 77 68 69 6c 65 28 20 70 54 72 69  .    while( pTri
24632 67 67 65 72 20 29 7b 0a 20 20 20 20 20 20 61 73  gger ){.      as
24633 73 65 72 74 28 20 70 54 72 69 67 67 65 72 2d 3e  sert( pTrigger->
24634 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70  pSchema==pTab->p
24635 53 63 68 65 6d 61 20 7c 7c 20 0a 20 20 20 20 20  Schema || .     
24636 20 20 20 20 20 70 54 72 69 67 67 65 72 2d 3e 70       pTrigger->p
24637 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b  Schema==db->aDb[
24638 31 5d 2e 70 53 63 68 65 6d 61 20 29 3b 0a 20 20  1].pSchema );.  
24639 20 20 20 20 73 71 6c 69 74 65 33 44 72 6f 70 54      sqlite3DropT
2463a 72 69 67 67 65 72 50 74 72 28 70 50 61 72 73 65  riggerPtr(pParse
2463b 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20  , pTrigger);.   
2463c 20 20 20 70 54 72 69 67 67 65 72 20 3d 20 70 54     pTrigger = pT
2463d 72 69 67 67 65 72 2d 3e 70 4e 65 78 74 3b 0a 20  rigger->pNext;. 
2463e 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51     }..#ifndef SQ
2463f 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
24640 43 52 45 4d 45 4e 54 0a 20 20 20 20 2f 2a 20 52  CREMENT.    /* R
24641 65 6d 6f 76 65 20 61 6e 79 20 65 6e 74 72 69 65  emove any entrie
24642 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f  s of the sqlite_
24643 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 61  sequence table a
24644 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 20  ssociated with. 
24645 20 20 20 2a 2a 20 74 68 65 20 74 61 62 6c 65 20     ** the table 
24646 62 65 69 6e 67 20 64 72 6f 70 70 65 64 2e 20 54  being dropped. T
24647 68 69 73 20 69 73 20 64 6f 6e 65 20 62 65 66 6f  his is done befo
24648 72 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  re the table is 
24649 64 72 6f 70 70 65 64 0a 20 20 20 20 2a 2a 20 61  dropped.    ** a
2464a 74 20 74 68 65 20 62 74 72 65 65 20 6c 65 76 65  t the btree leve
2464b 6c 2c 20 69 6e 20 63 61 73 65 20 74 68 65 20 73  l, in case the s
2464c 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74  qlite_sequence t
2464d 61 62 6c 65 20 6e 65 65 64 73 20 74 6f 0a 20 20  able needs to.  
2464e 20 20 2a 2a 20 6d 6f 76 65 20 61 73 20 61 20 72    ** move as a r
2464f 65 73 75 6c 74 20 6f 66 20 74 68 65 20 64 72 6f  esult of the dro
24650 70 20 28 63 61 6e 20 68 61 70 70 65 6e 20 69 6e  p (can happen in
24651 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64   auto-vacuum mod
24652 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  e)..    */.    i
24653 66 28 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67  f( pTab->tabFlag
24654 73 20 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 65  s & TF_Autoincre
24655 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 73 71  ment ){.      sq
24656 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
24657 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  (pParse,.       
24658 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 73   "DELETE FROM %s
24659 2e 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65  .sqlite_sequence
2465a 20 57 48 45 52 45 20 6e 61 6d 65 3d 25 51 22 2c   WHERE name=%Q",
2465b 0a 20 20 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e  .        pDb->zN
2465c 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ame, pTab->zName
2465d 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a  .      );.    }.
2465e 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 44  #endif..    /* D
2465f 72 6f 70 20 61 6c 6c 20 53 51 4c 49 54 45 5f 4d  rop all SQLITE_M
24660 41 53 54 45 52 20 74 61 62 6c 65 20 61 6e 64 20  ASTER table and 
24661 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20 74 68  index entries th
24662 61 74 20 72 65 66 65 72 20 74 6f 20 74 68 65 0a  at refer to the.
24663 20 20 20 20 2a 2a 20 74 61 62 6c 65 2e 20 54 68      ** table. Th
24664 65 20 70 72 6f 67 72 61 6d 20 6e 61 6d 65 20 6c  e program name l
24665 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 74 68 65  oops through the
24666 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20 61 6e   master table an
24667 64 20 64 65 6c 65 74 65 73 0a 20 20 20 20 2a 2a  d deletes.    **
24668 20 65 76 65 72 79 20 72 6f 77 20 74 68 61 74 20   every row that 
24669 72 65 66 65 72 73 20 74 6f 20 61 20 74 61 62 6c  refers to a tabl
2466a 65 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61  e of the same na
2466b 6d 65 20 61 73 20 74 68 65 20 6f 6e 65 20 62 65  me as the one be
2466c 69 6e 67 0a 20 20 20 20 2a 2a 20 64 72 6f 70 70  ing.    ** dropp
2466d 65 64 2e 20 54 72 69 67 67 65 72 73 20 61 72 65  ed. Triggers are
2466e 20 68 61 6e 64 6c 65 64 20 73 65 70 65 72 61 74   handled seperat
2466f 65 6c 79 20 62 65 63 61 75 73 65 20 61 20 74 72  ely because a tr
24670 69 67 67 65 72 20 63 61 6e 20 62 65 0a 20 20 20  igger can be.   
24671 20 2a 2a 20 63 72 65 61 74 65 64 20 69 6e 20 74   ** created in t
24672 68 65 20 74 65 6d 70 20 64 61 74 61 62 61 73 65  he temp database
24673 20 74 68 61 74 20 72 65 66 65 72 73 20 74 6f 20   that refers to 
24674 61 20 74 61 62 6c 65 20 69 6e 20 61 6e 6f 74 68  a table in anoth
24675 65 72 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61  er.    ** databa
24676 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  se..    */.    s
24677 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
24678 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  e(pParse, .     
24679 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20     "DELETE FROM 
2467a 25 51 2e 25 73 20 57 48 45 52 45 20 74 62 6c 5f  %Q.%s WHERE tbl_
2467b 6e 61 6d 65 3d 25 51 20 61 6e 64 20 74 79 70 65  name=%Q and type
2467c 21 3d 27 74 72 69 67 67 65 72 27 22 2c 0a 20 20  !='trigger'",.  
2467d 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65        pDb->zName
2467e 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
2467f 44 62 29 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  Db), pTab->zName
24680 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20  );..    /* Drop 
24681 61 6e 79 20 73 74 61 74 69 73 74 69 63 73 20 66  any statistics f
24682 72 6f 6d 20 74 68 65 20 73 71 6c 69 74 65 5f 73  rom the sqlite_s
24683 74 61 74 31 20 74 61 62 6c 65 2c 20 69 66 20 69  tat1 table, if i
24684 74 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 20 20  t exists */.    
24685 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 54  if( sqlite3FindT
24686 61 62 6c 65 28 64 62 2c 20 22 73 71 6c 69 74 65  able(db, "sqlite
24687 5f 73 74 61 74 31 22 2c 20 64 62 2d 3e 61 44 62  _stat1", db->aDb
24688 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a  [iDb].zName) ){.
24689 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73        sqlite3Nes
2468a 74 65 64 50 61 72 73 65 28 70 50 61 72 73 65 2c  tedParse(pParse,
2468b 0a 20 20 20 20 20 20 20 20 22 44 45 4c 45 54 45  .        "DELETE
2468c 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f   FROM %Q.sqlite_
2468d 73 74 61 74 31 20 57 48 45 52 45 20 74 62 6c 3d  stat1 WHERE tbl=
2468e 25 51 22 2c 20 70 44 62 2d 3e 7a 4e 61 6d 65 2c  %Q", pDb->zName,
2468f 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 0a 20 20 20   pTab->zName.   
24690 20 20 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20     );.    }..   
24691 20 69 66 28 20 21 69 73 56 69 65 77 20 26 26 20   if( !isView && 
24692 21 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29  !IsVirtual(pTab)
24693 20 29 7b 0a 20 20 20 20 20 20 64 65 73 74 72 6f   ){.      destro
24694 79 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70  yTable(pParse, p
24695 54 61 62 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  Tab);.    }..   
24696 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 74   /* Remove the t
24697 61 62 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d 20  able entry from 
24698 53 51 4c 69 74 65 27 73 20 69 6e 74 65 72 6e 61  SQLite's interna
24699 6c 20 73 63 68 65 6d 61 20 61 6e 64 20 6d 6f 64  l schema and mod
2469a 69 66 79 0a 20 20 20 20 2a 2a 20 74 68 65 20 73  ify.    ** the s
2469b 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 0a 20 20  chema cookie..  
2469c 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 49 73 56    */.    if( IsV
2469d 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
2469e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2469f 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 44  eAddOp4(v, OP_VD
246a0 65 73 74 72 6f 79 2c 20 69 44 62 2c 20 30 2c 20  estroy, iDb, 0, 
246a1 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  0, pTab->zName, 
246a2 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  0);.    }.    sq
246a3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
246a4 76 2c 20 4f 50 5f 44 72 6f 70 54 61 62 6c 65 2c  v, OP_DropTable,
246a5 20 69 44 62 2c 20 30 2c 20 30 2c 20 70 54 61 62   iDb, 0, 0, pTab
246a6 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ->zName, 0);.   
246a7 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f   sqlite3ChangeCo
246a8 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 62  okie(pParse, iDb
246a9 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 56  );.  }.  sqliteV
246aa 69 65 77 52 65 73 65 74 41 6c 6c 28 64 62 2c 20  iewResetAll(db, 
246ab 69 44 62 29 3b 0a 0a 65 78 69 74 5f 64 72 6f 70  iDb);..exit_drop
246ac 5f 74 61 62 6c 65 3a 0a 20 20 73 71 6c 69 74 65  _table:.  sqlite
246ad 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64  3SrcListDelete(d
246ae 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a  b, pName);.}../*
246af 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
246b0 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 72   is called to cr
246b1 65 61 74 65 20 61 20 6e 65 77 20 66 6f 72 65 69  eate a new forei
246b2 67 6e 20 6b 65 79 20 6f 6e 20 74 68 65 20 74 61  gn key on the ta
246b3 62 6c 65 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79  ble.** currently
246b4 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74   under construct
246b5 69 6f 6e 2e 20 20 70 46 72 6f 6d 43 6f 6c 20 64  ion.  pFromCol d
246b6 65 74 65 72 6d 69 6e 65 73 20 77 68 69 63 68 20  etermines which 
246b7 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68  columns.** in th
246b8 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65 20  e current table 
246b9 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 66 6f 72  point to the for
246ba 65 69 67 6e 20 6b 65 79 2e 20 20 49 66 20 70 46  eign key.  If pF
246bb 72 6f 6d 43 6f 6c 3d 3d 30 20 74 68 65 6e 0a 2a  romCol==0 then.*
246bc 2a 20 63 6f 6e 6e 65 63 74 20 74 68 65 20 6b 65  * connect the ke
246bd 79 20 74 6f 20 74 68 65 20 6c 61 73 74 20 63 6f  y to the last co
246be 6c 75 6d 6e 20 69 6e 73 65 72 74 65 64 2e 20 20  lumn inserted.  
246bf 70 54 6f 20 69 73 20 74 68 65 20 6e 61 6d 65 20  pTo is the name 
246c0 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20  of.** the table 
246c1 72 65 66 65 72 72 65 64 20 74 6f 2e 20 20 70 54  referred to.  pT
246c2 6f 43 6f 6c 20 69 73 20 61 20 6c 69 73 74 20 6f  oCol is a list o
246c3 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  f tables in the 
246c4 6f 74 68 65 72 0a 2a 2a 20 70 54 6f 20 74 61 62  other.** pTo tab
246c5 6c 65 20 74 68 61 74 20 74 68 65 20 66 6f 72 65  le that the fore
246c6 69 67 6e 20 6b 65 79 20 70 6f 69 6e 74 73 20 74  ign key points t
246c7 6f 2e 20 20 66 6c 61 67 73 20 63 6f 6e 74 61 69  o.  flags contai
246c8 6e 73 20 61 6c 6c 0a 2a 2a 20 69 6e 66 6f 72 6d  ns all.** inform
246c9 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20  ation about the 
246ca 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74  conflict resolut
246cb 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20 73  ion algorithms s
246cc 70 65 63 69 66 69 65 64 0a 2a 2a 20 69 6e 20 74  pecified.** in t
246cd 68 65 20 4f 4e 20 44 45 4c 45 54 45 2c 20 4f 4e  he ON DELETE, ON
246ce 20 55 50 44 41 54 45 20 61 6e 64 20 4f 4e 20 49   UPDATE and ON I
246cf 4e 53 45 52 54 20 63 6c 61 75 73 65 73 2e 0a 2a  NSERT clauses..*
246d0 2a 0a 2a 2a 20 41 6e 20 46 4b 65 79 20 73 74 72  *.** An FKey str
246d1 75 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65  ucture is create
246d2 64 20 61 6e 64 20 61 64 64 65 64 20 74 6f 20 74  d and added to t
246d3 68 65 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74  he table current
246d4 6c 79 0a 2a 2a 20 75 6e 64 65 72 20 63 6f 6e 73  ly.** under cons
246d5 74 72 75 63 74 69 6f 6e 20 69 6e 20 74 68 65 20  truction in the 
246d6 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
246d7 65 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54  e field..**.** T
246d8 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69  he foreign key i
246d9 73 20 73 65 74 20 66 6f 72 20 49 4d 4d 45 44 49  s set for IMMEDI
246da 41 54 45 20 70 72 6f 63 65 73 73 69 6e 67 2e 20  ATE processing. 
246db 20 41 20 73 75 62 73 65 71 75 65 6e 74 20 63 61   A subsequent ca
246dc 6c 6c 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33  ll.** to sqlite3
246dd 44 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28  DeferForeignKey(
246de 29 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 74  ) might change t
246df 68 69 73 20 74 6f 20 44 45 46 45 52 52 45 44 2e  his to DEFERRED.
246e0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
246e1 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  TE void sqlite3C
246e2 72 65 61 74 65 46 6f 72 65 69 67 6e 4b 65 79 28  reateForeignKey(
246e3 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
246e4 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69  ,       /* Parsi
246e5 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
246e6 45 78 70 72 4c 69 73 74 20 2a 70 46 72 6f 6d 43  ExprList *pFromC
246e7 6f 6c 2c 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20  ol,  /* Columns 
246e8 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 74 68  in this table th
246e9 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 74 68 65  at point to othe
246ea 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 54 6f 6b  r table */.  Tok
246eb 65 6e 20 2a 70 54 6f 2c 20 20 20 20 20 20 20 20  en *pTo,        
246ec 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
246ed 20 6f 74 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a   other table */.
246ee 20 20 45 78 70 72 4c 69 73 74 20 2a 70 54 6f 43    ExprList *pToC
246ef 6f 6c 2c 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e  ol,    /* Column
246f0 73 20 69 6e 20 74 68 65 20 6f 74 68 65 72 20 74  s in the other t
246f1 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  able */.  int fl
246f2 61 67 73 20 20 20 20 20 20 20 20 20 20 20 20 2f  ags            /
246f3 2a 20 43 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c  * Conflict resol
246f4 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73  ution algorithms
246f5 2e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  . */.){.  sqlite
246f6 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
246f7 64 62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  db;.#ifndef SQLI
246f8 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f  TE_OMIT_FOREIGN_
246f9 4b 45 59 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65  KEY.  FKey *pFKe
246fa 79 20 3d 20 30 3b 0a 20 20 46 4b 65 79 20 2a 70  y = 0;.  FKey *p
246fb 4e 65 78 74 54 6f 3b 0a 20 20 54 61 62 6c 65 20  NextTo;.  Table 
246fc 2a 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65  *p = pParse->pNe
246fd 77 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20 6e 42  wTable;.  int nB
246fe 79 74 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  yte;.  int i;.  
246ff 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 63 68 61 72  int nCol;.  char
24700 20 2a 7a 3b 0a 0a 20 20 61 73 73 65 72 74 28 20   *z;..  assert( 
24701 70 54 6f 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  pTo!=0 );.  if( 
24702 70 3d 3d 30 20 7c 7c 20 49 4e 5f 44 45 43 4c 41  p==0 || IN_DECLA
24703 52 45 5f 56 54 41 42 20 29 20 67 6f 74 6f 20 66  RE_VTAB ) goto f
24704 6b 5f 65 6e 64 3b 0a 20 20 69 66 28 20 70 46 72  k_end;.  if( pFr
24705 6f 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20 20 20  omCol==0 ){.    
24706 69 6e 74 20 69 43 6f 6c 20 3d 20 70 2d 3e 6e 43  int iCol = p->nC
24707 6f 6c 2d 31 3b 0a 20 20 20 20 69 66 28 20 4e 45  ol-1;.    if( NE
24708 56 45 52 28 69 43 6f 6c 3c 30 29 20 29 20 67 6f  VER(iCol<0) ) go
24709 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 20 20 69  to fk_end;.    i
2470a 66 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 54 6f  f( pToCol && pTo
2470b 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 31 20 29 7b  Col->nExpr!=1 ){
2470c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
2470d 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2470e 66 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 25  foreign key on %
2470f 73 22 0a 20 20 20 20 20 20 20 20 20 22 20 73 68  s".         " sh
24710 6f 75 6c 64 20 72 65 66 65 72 65 6e 63 65 20 6f  ould reference o
24711 6e 6c 79 20 6f 6e 65 20 63 6f 6c 75 6d 6e 20 6f  nly one column o
24712 66 20 74 61 62 6c 65 20 25 54 22 2c 0a 20 20 20  f table %T",.   
24713 20 20 20 20 20 20 70 2d 3e 61 43 6f 6c 5b 69 43        p->aCol[iC
24714 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 70 54 6f 29 3b  ol].zName, pTo);
24715 0a 20 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65  .      goto fk_e
24716 6e 64 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 43  nd;.    }.    nC
24717 6f 6c 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 20  ol = 1;.  }else 
24718 69 66 28 20 70 54 6f 43 6f 6c 20 26 26 20 70 54  if( pToCol && pT
24719 6f 43 6f 6c 2d 3e 6e 45 78 70 72 21 3d 70 46 72  oCol->nExpr!=pFr
2471a 6f 6d 43 6f 6c 2d 3e 6e 45 78 70 72 20 29 7b 0a  omCol->nExpr ){.
2471b 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2471c 4d 73 67 28 70 50 61 72 73 65 2c 0a 20 20 20 20  Msg(pParse,.    
2471d 20 20 20 20 22 6e 75 6d 62 65 72 20 6f 66 20 63      "number of c
2471e 6f 6c 75 6d 6e 73 20 69 6e 20 66 6f 72 65 69 67  olumns in foreig
2471f 6e 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20 6d  n key does not m
24720 61 74 63 68 20 74 68 65 20 6e 75 6d 62 65 72 20  atch the number 
24721 6f 66 20 22 0a 20 20 20 20 20 20 20 20 22 63 6f  of ".        "co
24722 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 66  lumns in the ref
24723 65 72 65 6e 63 65 64 20 74 61 62 6c 65 22 29 3b  erenced table");
24724 0a 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64  .    goto fk_end
24725 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e  ;.  }else{.    n
24726 43 6f 6c 20 3d 20 70 46 72 6f 6d 43 6f 6c 2d 3e  Col = pFromCol->
24727 6e 45 78 70 72 3b 0a 20 20 7d 0a 20 20 6e 42 79  nExpr;.  }.  nBy
24728 74 65 20 3d 20 73 69 7a 65 6f 66 28 2a 70 46 4b  te = sizeof(*pFK
24729 65 79 29 20 2b 20 28 6e 43 6f 6c 2d 31 29 2a 73  ey) + (nCol-1)*s
2472a 69 7a 65 6f 66 28 70 46 4b 65 79 2d 3e 61 43 6f  izeof(pFKey->aCo
2472b 6c 5b 30 5d 29 20 2b 20 70 54 6f 2d 3e 6e 20 2b  l[0]) + pTo->n +
2472c 20 31 3b 0a 20 20 69 66 28 20 70 54 6f 43 6f 6c   1;.  if( pToCol
2472d 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
2472e 20 69 3c 70 54 6f 43 6f 6c 2d 3e 6e 45 78 70 72   i<pToCol->nExpr
2472f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6e 42  ; i++){.      nB
24730 79 74 65 20 2b 3d 20 73 71 6c 69 74 65 33 53 74  yte += sqlite3St
24731 72 6c 65 6e 33 30 28 70 54 6f 43 6f 6c 2d 3e 61  rlen30(pToCol->a
24732 5b 69 5d 2e 7a 4e 61 6d 65 29 20 2b 20 31 3b 0a  [i].zName) + 1;.
24733 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 46 4b 65      }.  }.  pFKe
24734 79 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  y = sqlite3DbMal
24735 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79 74  locZero(db, nByt
24736 65 20 29 3b 0a 20 20 69 66 28 20 70 46 4b 65 79  e );.  if( pFKey
24737 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ==0 ){.    goto 
24738 66 6b 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 70 46  fk_end;.  }.  pF
24739 4b 65 79 2d 3e 70 46 72 6f 6d 20 3d 20 70 3b 0a  Key->pFrom = p;.
2473a 20 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72    pFKey->pNextFr
2473b 6f 6d 20 3d 20 70 2d 3e 70 46 4b 65 79 3b 0a 20  om = p->pFKey;. 
2473c 20 7a 20 3d 20 28 63 68 61 72 2a 29 26 70 46 4b   z = (char*)&pFK
2473d 65 79 2d 3e 61 43 6f 6c 5b 6e 43 6f 6c 5d 3b 0a  ey->aCol[nCol];.
2473e 20 20 70 46 4b 65 79 2d 3e 7a 54 6f 20 3d 20 7a    pFKey->zTo = z
2473f 3b 0a 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54  ;.  memcpy(z, pT
24740 6f 2d 3e 7a 2c 20 70 54 6f 2d 3e 6e 29 3b 0a 20  o->z, pTo->n);. 
24741 20 7a 5b 70 54 6f 2d 3e 6e 5d 20 3d 20 30 3b 0a   z[pTo->n] = 0;.
24742 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65    sqlite3Dequote
24743 28 7a 29 3b 0a 20 20 7a 20 2b 3d 20 70 54 6f 2d  (z);.  z += pTo-
24744 3e 6e 2b 31 3b 0a 20 20 70 46 4b 65 79 2d 3e 6e  >n+1;.  pFKey->n
24745 43 6f 6c 20 3d 20 6e 43 6f 6c 3b 0a 20 20 69 66  Col = nCol;.  if
24746 28 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b  ( pFromCol==0 ){
24747 0a 20 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c  .    pFKey->aCol
24748 5b 30 5d 2e 69 46 72 6f 6d 20 3d 20 70 2d 3e 6e  [0].iFrom = p->n
24749 43 6f 6c 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Col-1;.  }else{.
2474a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
2474b 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
2474c 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f   int j;.      fo
2474d 72 28 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e 43 6f 6c  r(j=0; j<p->nCol
2474e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
2474f 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
24750 6d 70 28 70 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e  mp(p->aCol[j].zN
24751 61 6d 65 2c 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61  ame, pFromCol->a
24752 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b  [i].zName)==0 ){
24753 0a 20 20 20 20 20 20 20 20 20 20 70 46 4b 65 79  .          pFKey
24754 2d 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 20  ->aCol[i].iFrom 
24755 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 62  = j;.          b
24756 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
24757 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
24758 28 20 6a 3e 3d 70 2d 3e 6e 43 6f 6c 20 29 7b 0a  ( j>=p->nCol ){.
24759 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2475a 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2475b 0a 20 20 20 20 20 20 20 20 20 20 22 75 6e 6b 6e  .          "unkn
2475c 6f 77 6e 20 63 6f 6c 75 6d 6e 20 5c 22 25 73 5c  own column \"%s\
2475d 22 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79  " in foreign key
2475e 20 64 65 66 69 6e 69 74 69 6f 6e 22 2c 20 0a 20   definition", . 
2475f 20 20 20 20 20 20 20 20 20 70 46 72 6f 6d 43 6f           pFromCo
24760 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a  l->a[i].zName);.
24761 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f          goto fk_
24762 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  end;.      }.   
24763 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 54 6f   }.  }.  if( pTo
24764 43 6f 6c 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  Col ){.    for(i
24765 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
24766 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20  {.      int n = 
24767 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
24768 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d 2e 7a 4e 61  pToCol->a[i].zNa
24769 6d 65 29 3b 0a 20 20 20 20 20 20 70 46 4b 65 79  me);.      pFKey
2476a 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 20 3d  ->aCol[i].zCol =
2476b 20 7a 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   z;.      memcpy
2476c 28 7a 2c 20 70 54 6f 43 6f 6c 2d 3e 61 5b 69 5d  (z, pToCol->a[i]
2476d 2e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20 20 20  .zName, n);.    
2476e 20 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20 20 20 20    z[n] = 0;.    
2476f 20 20 7a 20 2b 3d 20 6e 2b 31 3b 0a 20 20 20 20    z += n+1;.    
24770 7d 0a 20 20 7d 0a 20 20 70 46 4b 65 79 2d 3e 69  }.  }.  pFKey->i
24771 73 44 65 66 65 72 72 65 64 20 3d 20 30 3b 0a 20  sDeferred = 0;. 
24772 20 70 46 4b 65 79 2d 3e 61 41 63 74 69 6f 6e 5b   pFKey->aAction[
24773 30 5d 20 3d 20 28 75 38 29 28 66 6c 61 67 73 20  0] = (u8)(flags 
24774 26 20 30 78 66 66 29 3b 20 20 20 20 20 20 20 20  & 0xff);        
24775 20 20 20 20 2f 2a 20 4f 4e 20 44 45 4c 45 54 45      /* ON DELETE
24776 20 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 70 46 4b   action */.  pFK
24777 65 79 2d 3e 61 41 63 74 69 6f 6e 5b 31 5d 20 3d  ey->aAction[1] =
24778 20 28 75 38 29 28 28 66 6c 61 67 73 20 3e 3e 20   (u8)((flags >> 
24779 38 20 29 20 26 20 30 78 66 66 29 3b 20 20 20 20  8 ) & 0xff);    
2477a 2f 2a 20 4f 4e 20 55 50 44 41 54 45 20 61 63 74  /* ON UPDATE act
2477b 69 6f 6e 20 2a 2f 0a 0a 20 20 70 4e 65 78 74 54  ion */..  pNextT
2477c 6f 20 3d 20 28 46 4b 65 79 20 2a 29 73 71 6c 69  o = (FKey *)sqli
2477d 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 70  te3HashInsert(&p
2477e 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 6b 65 79 48  ->pSchema->fkeyH
2477f 61 73 68 2c 20 0a 20 20 20 20 20 20 70 46 4b 65  ash, .      pFKe
24780 79 2d 3e 7a 54 6f 2c 20 73 71 6c 69 74 65 33 53  y->zTo, sqlite3S
24781 74 72 6c 65 6e 33 30 28 70 46 4b 65 79 2d 3e 7a  trlen30(pFKey->z
24782 54 6f 29 2c 20 28 76 6f 69 64 20 2a 29 70 46 4b  To), (void *)pFK
24783 65 79 0a 20 20 29 3b 0a 20 20 69 66 28 20 70 4e  ey.  );.  if( pN
24784 65 78 74 54 6f 3d 3d 70 46 4b 65 79 20 29 7b 0a  extTo==pFKey ){.
24785 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
24786 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 67 6f  iled = 1;.    go
24787 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 7d 0a 20  to fk_end;.  }. 
24788 20 69 66 28 20 70 4e 65 78 74 54 6f 20 29 7b 0a   if( pNextTo ){.
24789 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 78      assert( pNex
2478a 74 54 6f 2d 3e 70 50 72 65 76 54 6f 3d 3d 30 20  tTo->pPrevTo==0 
2478b 29 3b 0a 20 20 20 20 70 46 4b 65 79 2d 3e 70 4e  );.    pFKey->pN
2478c 65 78 74 54 6f 20 3d 20 70 4e 65 78 74 54 6f 3b  extTo = pNextTo;
2478d 0a 20 20 20 20 70 4e 65 78 74 54 6f 2d 3e 70 50  .    pNextTo->pP
2478e 72 65 76 54 6f 20 3d 20 70 46 4b 65 79 3b 0a 20  revTo = pFKey;. 
2478f 20 7d 0a 0a 20 20 2f 2a 20 4c 69 6e 6b 20 74 68   }..  /* Link th
24790 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 74 6f  e foreign key to
24791 20 74 68 65 20 74 61 62 6c 65 20 61 73 20 74 68   the table as th
24792 65 20 6c 61 73 74 20 73 74 65 70 2e 0a 20 20 2a  e last step..  *
24793 2f 0a 20 20 70 2d 3e 70 46 4b 65 79 20 3d 20 70  /.  p->pFKey = p
24794 46 4b 65 79 3b 0a 20 20 70 46 4b 65 79 20 3d 20  FKey;.  pFKey = 
24795 30 3b 0a 0a 66 6b 5f 65 6e 64 3a 0a 20 20 73 71  0;..fk_end:.  sq
24796 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
24797 70 46 4b 65 79 29 3b 0a 23 65 6e 64 69 66 20 2f  pFKey);.#endif /
24798 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
24799 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b  E_OMIT_FOREIGN_K
2479a 45 59 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  EY) */.  sqlite3
2479b 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64  ExprListDelete(d
2479c 62 2c 20 70 46 72 6f 6d 43 6f 6c 29 3b 0a 20 20  b, pFromCol);.  
2479d 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2479e 65 6c 65 74 65 28 64 62 2c 20 70 54 6f 43 6f 6c  elete(db, pToCol
2479f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
247a0 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
247a1 65 64 20 77 68 65 6e 20 61 6e 20 49 4e 49 54 49  ed when an INITI
247a2 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45 20 6f  ALLY IMMEDIATE o
247a3 72 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45  r INITIALLY DEFE
247a4 52 52 45 44 0a 2a 2a 20 63 6c 61 75 73 65 20 69  RRED.** clause i
247a5 73 20 73 65 65 6e 20 61 73 20 70 61 72 74 20 6f  s seen as part o
247a6 66 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  f a foreign key 
247a7 64 65 66 69 6e 69 74 69 6f 6e 2e 20 20 54 68 65  definition.  The
247a8 20 69 73 44 65 66 65 72 72 65 64 0a 2a 2a 20 70   isDeferred.** p
247a9 61 72 61 6d 65 74 65 72 20 69 73 20 31 20 66 6f  arameter is 1 fo
247aa 72 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45  r INITIALLY DEFE
247ab 52 52 45 44 20 61 6e 64 20 30 20 66 6f 72 20 49  RRED and 0 for I
247ac 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41  NITIALLY IMMEDIA
247ad 54 45 2e 0a 2a 2a 20 54 68 65 20 62 65 68 61 76  TE..** The behav
247ae 69 6f 72 20 6f 66 20 74 68 65 20 6d 6f 73 74 20  ior of the most 
247af 72 65 63 65 6e 74 6c 79 20 63 72 65 61 74 65 64  recently created
247b0 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 20   foreign key is 
247b1 61 64 6a 75 73 74 65 64 0a 2a 2a 20 61 63 63 6f  adjusted.** acco
247b2 72 64 69 6e 67 6c 79 2e 0a 2a 2f 0a 53 51 4c 49  rdingly..*/.SQLI
247b3 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
247b4 73 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65  sqlite3DeferFore
247b5 69 67 6e 4b 65 79 28 50 61 72 73 65 20 2a 70 50  ignKey(Parse *pP
247b6 61 72 73 65 2c 20 69 6e 74 20 69 73 44 65 66 65  arse, int isDefe
247b7 72 72 65 64 29 7b 0a 23 69 66 6e 64 65 66 20 53  rred){.#ifndef S
247b8 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
247b9 47 4e 5f 4b 45 59 0a 20 20 54 61 62 6c 65 20 2a  GN_KEY.  Table *
247ba 70 54 61 62 3b 0a 20 20 46 4b 65 79 20 2a 70 46  pTab;.  FKey *pF
247bb 4b 65 79 3b 0a 20 20 69 66 28 20 28 70 54 61 62  Key;.  if( (pTab
247bc 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
247bd 61 62 6c 65 29 3d 3d 30 20 7c 7c 20 28 70 46 4b  able)==0 || (pFK
247be 65 79 20 3d 20 70 54 61 62 2d 3e 70 46 4b 65 79  ey = pTab->pFKey
247bf 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  )==0 ) return;. 
247c0 20 61 73 73 65 72 74 28 20 69 73 44 65 66 65 72   assert( isDefer
247c1 72 65 64 3d 3d 30 20 7c 7c 20 69 73 44 65 66 65  red==0 || isDefe
247c2 72 72 65 64 3d 3d 31 20 29 3b 20 2f 2a 20 45 56  rred==1 ); /* EV
247c3 3a 20 52 2d 33 30 33 32 33 2d 32 31 39 31 37 20  : R-30323-21917 
247c4 2a 2f 0a 20 20 70 46 4b 65 79 2d 3e 69 73 44 65  */.  pFKey->isDe
247c5 66 65 72 72 65 64 20 3d 20 28 75 38 29 69 73 44  ferred = (u8)isD
247c6 65 66 65 72 72 65 64 3b 0a 23 65 6e 64 69 66 0a  eferred;.#endif.
247c7 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
247c8 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c  e code that will
247c9 20 65 72 61 73 65 20 61 6e 64 20 72 65 66 69 6c   erase and refil
247ca 6c 20 69 6e 64 65 78 20 2a 70 49 64 78 2e 20 20  l index *pIdx.  
247cb 54 68 69 73 20 69 73 0a 2a 2a 20 75 73 65 64 20  This is.** used 
247cc 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 20  to initialize a 
247cd 6e 65 77 6c 79 20 63 72 65 61 74 65 64 20 69 6e  newly created in
247ce 64 65 78 20 6f 72 20 74 6f 20 72 65 63 6f 6d 70  dex or to recomp
247cf 75 74 65 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65  ute the.** conte
247d0 6e 74 20 6f 66 20 61 6e 20 69 6e 64 65 78 20 69  nt of an index i
247d1 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 61 20  n response to a 
247d2 52 45 49 4e 44 45 58 20 63 6f 6d 6d 61 6e 64 2e  REINDEX command.
247d3 0a 2a 2a 0a 2a 2a 20 69 66 20 6d 65 6d 52 6f 6f  .**.** if memRoo
247d4 74 50 61 67 65 20 69 73 20 6e 6f 74 20 6e 65 67  tPage is not neg
247d5 61 74 69 76 65 2c 20 69 74 20 6d 65 61 6e 73 20  ative, it means 
247d6 74 68 61 74 20 74 68 65 20 69 6e 64 65 78 20 69  that the index i
247d7 73 20 6e 65 77 6c 79 0a 2a 2a 20 63 72 65 61 74  s newly.** creat
247d8 65 64 2e 20 20 54 68 65 20 72 65 67 69 73 74 65  ed.  The registe
247d9 72 20 73 70 65 63 69 66 69 65 64 20 62 79 20 6d  r specified by m
247da 65 6d 52 6f 6f 74 50 61 67 65 20 63 6f 6e 74 61  emRootPage conta
247db 69 6e 73 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20  ins the.** root 
247dc 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74  page number of t
247dd 68 65 20 69 6e 64 65 78 2e 20 20 49 66 20 6d 65  he index.  If me
247de 6d 52 6f 6f 74 50 61 67 65 20 69 73 20 6e 65 67  mRootPage is neg
247df 61 74 69 76 65 2c 20 74 68 65 6e 0a 2a 2a 20 74  ative, then.** t
247e0 68 65 20 69 6e 64 65 78 20 61 6c 72 65 61 64 79  he index already
247e1 20 65 78 69 73 74 73 20 61 6e 64 20 6d 75 73 74   exists and must
247e2 20 62 65 20 63 6c 65 61 72 65 64 20 62 65 66 6f   be cleared befo
247e3 72 65 20 62 65 69 6e 67 20 72 65 66 69 6c 6c 65  re being refille
247e4 64 20 61 6e 64 0a 2a 2a 20 74 68 65 20 72 6f 6f  d and.** the roo
247e5 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66  t page number of
247e6 20 74 68 65 20 69 6e 64 65 78 20 69 73 20 74 61   the index is ta
247e7 6b 65 6e 20 66 72 6f 6d 20 70 49 6e 64 65 78 2d  ken from pIndex-
247e8 3e 74 6e 75 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63  >tnum..*/.static
247e9 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65 66   void sqlite3Ref
247ea 69 6c 6c 49 6e 64 65 78 28 50 61 72 73 65 20 2a  illIndex(Parse *
247eb 70 50 61 72 73 65 2c 20 49 6e 64 65 78 20 2a 70  pParse, Index *p
247ec 49 6e 64 65 78 2c 20 69 6e 74 20 6d 65 6d 52 6f  Index, int memRo
247ed 6f 74 50 61 67 65 29 7b 0a 20 20 54 61 62 6c 65  otPage){.  Table
247ee 20 2a 70 54 61 62 20 3d 20 70 49 6e 64 65 78 2d   *pTab = pIndex-
247ef 3e 70 54 61 62 6c 65 3b 20 20 2f 2a 20 54 68 65  >pTable;  /* The
247f0 20 74 61 62 6c 65 20 74 68 61 74 20 69 73 20 69   table that is i
247f1 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20  ndexed */.  int 
247f2 69 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 6e  iTab = pParse->n
247f3 54 61 62 2b 2b 3b 20 20 20 20 20 2f 2a 20 42 74  Tab++;     /* Bt
247f4 72 65 65 20 63 75 72 73 6f 72 20 75 73 65 64 20  ree cursor used 
247f5 66 6f 72 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e  for pTab */.  in
247f6 74 20 69 49 64 78 20 3d 20 70 50 61 72 73 65 2d  t iIdx = pParse-
247f7 3e 6e 54 61 62 2b 2b 3b 20 20 20 20 20 2f 2a 20  >nTab++;     /* 
247f8 42 74 72 65 65 20 63 75 72 73 6f 72 20 75 73 65  Btree cursor use
247f9 64 20 66 6f 72 20 70 49 6e 64 65 78 20 2a 2f 0a  d for pIndex */.
247fa 20 20 69 6e 74 20 61 64 64 72 31 3b 20 20 20 20    int addr1;    
247fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
247fc 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74   /* Address of t
247fd 6f 70 20 6f 66 20 6c 6f 6f 70 20 2a 2f 0a 20 20  op of loop */.  
247fe 69 6e 74 20 74 6e 75 6d 3b 20 20 20 20 20 20 20  int tnum;       
247ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
24800 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 69  * Root page of i
24801 6e 64 65 78 20 2a 2f 0a 20 20 56 64 62 65 20 2a  ndex */.  Vdbe *
24802 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  v;              
24803 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65           /* Gene
24804 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74  rate code into t
24805 68 69 73 20 76 69 72 74 75 61 6c 20 6d 61 63 68  his virtual mach
24806 69 6e 65 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f  ine */.  KeyInfo
24807 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 20 20 20   *pKey;         
24808 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79 49 6e          /* KeyIn
24809 66 6f 20 66 6f 72 20 69 6e 64 65 78 20 2a 2f 0a  fo for index */.
2480a 20 20 69 6e 74 20 72 65 67 49 64 78 4b 65 79 3b    int regIdxKey;
2480b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2480c 20 2f 2a 20 52 65 67 69 73 74 65 72 73 20 63 6f   /* Registers co
2480d 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 69 6e 64  ntaining the ind
2480e 65 78 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20  ex key */.  int 
2480f 72 65 67 52 65 63 6f 72 64 3b 20 20 20 20 20 20  regRecord;      
24810 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
24811 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 61  gister holding a
24812 73 73 65 6d 62 6c 69 65 64 20 69 6e 64 65 78 20  ssemblied index 
24813 72 65 63 6f 72 64 20 2a 2f 0a 20 20 73 71 6c 69  record */.  sqli
24814 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
24815 2d 3e 64 62 3b 20 20 20 20 20 20 2f 2a 20 54 68  ->db;      /* Th
24816 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
24817 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  ction */.  int i
24818 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
24819 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 49  maToIndex(db, pI
2481a 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  ndex->pSchema);.
2481b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2481c 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
2481d 4f 4e 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  ON.  if( sqlite3
2481e 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
2481f 2c 20 53 51 4c 49 54 45 5f 52 45 49 4e 44 45 58  , SQLITE_REINDEX
24820 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c  , pIndex->zName,
24821 20 30 2c 0a 20 20 20 20 20 20 64 62 2d 3e 61 44   0,.      db->aD
24822 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 20 29 20 29  b[iDb].zName ) )
24823 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
24824 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52  }.#endif..  /* R
24825 65 71 75 69 72 65 20 61 20 77 72 69 74 65 2d 6c  equire a write-l
24826 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  ock on the table
24827 20 74 6f 20 70 65 72 66 6f 72 6d 20 74 68 69 73   to perform this
24828 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20   operation */.  
24829 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
2482a 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54  (pParse, iDb, pT
2482b 61 62 2d 3e 74 6e 75 6d 2c 20 31 2c 20 70 54 61  ab->tnum, 1, pTa
2482c 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 76 20  b->zName);..  v 
2482d 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
2482e 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
2482f 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  v==0 ) return;. 
24830 20 69 66 28 20 6d 65 6d 52 6f 6f 74 50 61 67 65   if( memRootPage
24831 3e 3d 30 20 29 7b 0a 20 20 20 20 74 6e 75 6d 20  >=0 ){.    tnum 
24832 3d 20 6d 65 6d 52 6f 6f 74 50 61 67 65 3b 0a 20  = memRootPage;. 
24833 20 7d 65 6c 73 65 7b 0a 20 20 20 20 74 6e 75 6d   }else{.    tnum
24834 20 3d 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 3b   = pIndex->tnum;
24835 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
24836 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 65  AddOp2(v, OP_Cle
24837 61 72 2c 20 74 6e 75 6d 2c 20 69 44 62 29 3b 0a  ar, tnum, iDb);.
24838 20 20 7d 0a 20 20 70 4b 65 79 20 3d 20 73 71 6c    }.  pKey = sql
24839 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f  ite3IndexKeyinfo
2483a 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65 78 29  (pParse, pIndex)
2483b 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2483c 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp4(v, OP_Open
2483d 57 72 69 74 65 2c 20 69 49 64 78 2c 20 74 6e 75  Write, iIdx, tnu
2483e 6d 2c 20 69 44 62 2c 20 0a 20 20 20 20 20 20 20  m, iDb, .       
2483f 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68               (ch
24840 61 72 20 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45  ar *)pKey, P4_KE
24841 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a  YINFO_HANDOFF);.
24842 20 20 69 66 28 20 6d 65 6d 52 6f 6f 74 50 61 67    if( memRootPag
24843 65 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  e>=0 ){.    sqli
24844 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
24845 76 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  v, 1);.  }.  sql
24846 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50  ite3OpenTable(pP
24847 61 72 73 65 2c 20 69 54 61 62 2c 20 69 44 62 2c  arse, iTab, iDb,
24848 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65   pTab, OP_OpenRe
24849 61 64 29 3b 0a 20 20 61 64 64 72 31 20 3d 20 73  ad);.  addr1 = s
2484a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2484b 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69  (v, OP_Rewind, i
2484c 54 61 62 2c 20 30 29 3b 0a 20 20 72 65 67 52 65  Tab, 0);.  regRe
2484d 63 6f 72 64 20 3d 20 73 71 6c 69 74 65 33 47 65  cord = sqlite3Ge
2484e 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
2484f 3b 0a 20 20 72 65 67 49 64 78 4b 65 79 20 3d 20  ;.  regIdxKey = 
24850 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49  sqlite3GenerateI
24851 6e 64 65 78 4b 65 79 28 70 50 61 72 73 65 2c 20  ndexKey(pParse, 
24852 70 49 6e 64 65 78 2c 20 69 54 61 62 2c 20 72 65  pIndex, iTab, re
24853 67 52 65 63 6f 72 64 2c 20 31 29 3b 0a 20 20 69  gRecord, 1);.  i
24854 66 28 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72  f( pIndex->onErr
24855 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20  or!=OE_None ){. 
24856 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 72 65 67     const int reg
24857 52 6f 77 69 64 20 3d 20 72 65 67 49 64 78 4b 65  Rowid = regIdxKe
24858 79 20 2b 20 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c  y + pIndex->nCol
24859 75 6d 6e 3b 0a 20 20 20 20 63 6f 6e 73 74 20 69  umn;.    const i
2485a 6e 74 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 56  nt j2 = sqlite3V
2485b 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
2485c 29 20 2b 20 32 3b 0a 20 20 20 20 76 6f 69 64 20  ) + 2;.    void 
2485d 2a 20 63 6f 6e 73 74 20 70 52 65 67 4b 65 79 20  * const pRegKey 
2485e 3d 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f  = SQLITE_INT_TO_
2485f 50 54 52 28 72 65 67 49 64 78 4b 65 79 29 3b 0a  PTR(regIdxKey);.
24860 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 67 69  .    /* The regi
24861 73 74 65 72 73 20 61 63 63 65 73 73 65 64 20 62  sters accessed b
24862 79 20 74 68 65 20 4f 50 5f 49 73 55 6e 69 71 75  y the OP_IsUniqu
24863 65 20 6f 70 63 6f 64 65 20 77 65 72 65 20 61 6c  e opcode were al
24864 6c 6f 63 61 74 65 64 0a 20 20 20 20 2a 2a 20 75  located.    ** u
24865 73 69 6e 67 20 73 71 6c 69 74 65 33 47 65 74 54  sing sqlite3GetT
24866 65 6d 70 52 61 6e 67 65 28 29 20 69 6e 73 69 64  empRange() insid
24867 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  e of the sqlite3
24868 47 65 6e 65 72 61 74 65 49 6e 64 65 78 4b 65 79  GenerateIndexKey
24869 28 29 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 20 61  ().    ** call a
2486a 62 6f 76 65 2e 20 4a 75 73 74 20 62 65 66 6f 72  bove. Just befor
2486b 65 20 74 68 61 74 20 66 75 6e 63 74 69 6f 6e 20  e that function 
2486c 77 61 73 20 66 72 65 65 64 20 74 68 65 79 20 77  was freed they w
2486d 65 72 65 20 72 65 6c 65 61 73 65 64 0a 20 20 20  ere released.   
2486e 20 2a 2a 20 28 6d 61 64 65 20 61 76 61 69 6c 61   ** (made availa
2486f 62 6c 65 20 74 6f 20 74 68 65 20 63 6f 6d 70 69  ble to the compi
24870 6c 65 72 20 66 6f 72 20 72 65 75 73 65 29 20 75  ler for reuse) u
24871 73 69 6e 67 20 0a 20 20 20 20 2a 2a 20 73 71 6c  sing .    ** sql
24872 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
24873 61 6e 67 65 28 29 2e 20 53 6f 20 69 6e 20 73 6f  ange(). So in so
24874 6d 65 20 77 61 79 73 20 68 61 76 69 6e 67 20 74  me ways having t
24875 68 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 0a 20  he OP_IsUnique. 
24876 20 20 20 2a 2a 20 6f 70 63 6f 64 65 20 75 73 65     ** opcode use
24877 20 74 68 65 20 76 61 6c 75 65 73 20 73 74 6f 72   the values stor
24878 65 64 20 77 69 74 68 69 6e 20 73 65 65 6d 73 20  ed within seems 
24879 64 61 6e 67 65 72 6f 75 73 2e 20 48 6f 77 65 76  dangerous. Howev
2487a 65 72 2c 20 73 69 6e 63 65 0a 20 20 20 20 2a 2a  er, since.    **
2487b 20 77 65 20 63 61 6e 20 62 65 20 73 75 72 65 20   we can be sure 
2487c 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 74 65  that no other te
2487d 6d 70 20 72 65 67 69 73 74 65 72 73 20 68 61 76  mp registers hav
2487e 65 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  e been allocated
2487f 0a 20 20 20 20 2a 2a 20 73 69 6e 63 65 20 73 71  .    ** since sq
24880 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
24881 52 61 6e 67 65 28 29 20 77 61 73 20 63 61 6c 6c  Range() was call
24882 65 64 2c 20 69 74 20 69 73 20 73 61 66 65 20 74  ed, it is safe t
24883 6f 20 64 6f 20 73 6f 2e 0a 20 20 20 20 2a 2f 0a  o do so..    */.
24884 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24885 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 73 55 6e  ddOp4(v, OP_IsUn
24886 69 71 75 65 2c 20 69 49 64 78 2c 20 6a 32 2c 20  ique, iIdx, j2, 
24887 72 65 67 52 6f 77 69 64 2c 20 70 52 65 67 4b 65  regRowid, pRegKe
24888 79 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20  y, P4_INT32);.  
24889 20 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e    sqlite3HaltCon
2488a 73 74 72 61 69 6e 74 28 0a 20 20 20 20 20 20 20  straint(.       
2488b 20 70 50 61 72 73 65 2c 20 4f 45 5f 41 62 6f 72   pParse, OE_Abor
2488c 74 2c 20 22 69 6e 64 65 78 65 64 20 63 6f 6c 75  t, "indexed colu
2488d 6d 6e 73 20 61 72 65 20 6e 6f 74 20 75 6e 69 71  mns are not uniq
2488e 75 65 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b  ue", P4_STATIC);
2488f 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64  .  }.  sqlite3Vd
24890 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
24891 64 78 49 6e 73 65 72 74 2c 20 69 49 64 78 2c 20  dxInsert, iIdx, 
24892 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71  regRecord);.  sq
24893 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
24894 35 28 76 2c 20 4f 50 46 4c 41 47 5f 55 53 45 53  5(v, OPFLAG_USES
24895 45 45 4b 52 45 53 55 4c 54 29 3b 0a 20 20 73 71  EEKRESULT);.  sq
24896 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
24897 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 52  Reg(pParse, regR
24898 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69 74 65  ecord);.  sqlite
24899 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2489a 50 5f 4e 65 78 74 2c 20 69 54 61 62 2c 20 61 64  P_Next, iTab, ad
2489b 64 72 31 2b 31 29 3b 0a 20 20 73 71 6c 69 74 65  dr1+1);.  sqlite
2489c 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2489d 20 61 64 64 72 31 29 3b 0a 20 20 73 71 6c 69 74   addr1);.  sqlit
2489e 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2489f 4f 50 5f 43 6c 6f 73 65 2c 20 69 54 61 62 29 3b  OP_Close, iTab);
248a0 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
248a1 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp1(v, OP_Close
248a2 2c 20 69 49 64 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  , iIdx);.}../*.*
248a3 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 69  * Create a new i
248a4 6e 64 65 78 20 66 6f 72 20 61 6e 20 53 51 4c 20  ndex for an SQL 
248a5 74 61 62 6c 65 2e 20 20 70 4e 61 6d 65 31 2e 70  table.  pName1.p
248a6 4e 61 6d 65 32 20 69 73 20 74 68 65 20 6e 61 6d  Name2 is the nam
248a7 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 0a  e of the index .
248a8 2a 2a 20 61 6e 64 20 70 54 62 6c 4c 69 73 74 20  ** and pTblList 
248a9 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  is the name of t
248aa 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 69 73  he table that is
248ab 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20   to be indexed. 
248ac 20 42 6f 74 68 20 77 69 6c 6c 20 0a 2a 2a 20 62   Both will .** b
248ad 65 20 4e 55 4c 4c 20 66 6f 72 20 61 20 70 72 69  e NULL for a pri
248ae 6d 61 72 79 20 6b 65 79 20 6f 72 20 61 6e 20 69  mary key or an i
248af 6e 64 65 78 20 74 68 61 74 20 69 73 20 63 72 65  ndex that is cre
248b0 61 74 65 64 20 74 6f 20 73 61 74 69 73 66 79 20  ated to satisfy 
248b1 61 0a 2a 2a 20 55 4e 49 51 55 45 20 63 6f 6e 73  a.** UNIQUE cons
248b2 74 72 61 69 6e 74 2e 20 20 49 66 20 70 54 61 62  traint.  If pTab
248b3 6c 65 20 61 6e 64 20 70 49 6e 64 65 78 20 61 72  le and pIndex ar
248b4 65 20 4e 55 4c 4c 2c 20 75 73 65 20 70 50 61 72  e NULL, use pPar
248b5 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 0a 2a 2a  se->pNewTable.**
248b6 20 61 73 20 74 68 65 20 74 61 62 6c 65 20 74 6f   as the table to
248b7 20 62 65 20 69 6e 64 65 78 65 64 2e 20 20 70 50   be indexed.  pP
248b8 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20  arse->pNewTable 
248b9 69 73 20 61 20 74 61 62 6c 65 20 74 68 61 74 20  is a table that 
248ba 69 73 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20  is.** currently 
248bb 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65  being constructe
248bc 64 20 62 79 20 61 20 43 52 45 41 54 45 20 54 41  d by a CREATE TA
248bd 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  BLE statement..*
248be 2a 0a 2a 2a 20 70 4c 69 73 74 20 69 73 20 61 20  *.** pList is a 
248bf 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  list of columns 
248c0 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20 20  to be indexed.  
248c1 70 4c 69 73 74 20 77 69 6c 6c 20 62 65 20 4e 55  pList will be NU
248c2 4c 4c 20 69 66 20 74 68 69 73 0a 2a 2a 20 69 73  LL if this.** is
248c3 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20 6f   a primary key o
248c4 72 20 75 6e 69 71 75 65 2d 63 6f 6e 73 74 72 61  r unique-constra
248c5 69 6e 74 20 6f 6e 20 74 68 65 20 6d 6f 73 74 20  int on the most 
248c6 72 65 63 65 6e 74 20 63 6f 6c 75 6d 6e 20 61 64  recent column ad
248c7 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20 74 61  ded.** to the ta
248c8 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 75 6e  ble currently un
248c9 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
248ca 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  .  .**.** If the
248cb 20 69 6e 64 65 78 20 69 73 20 63 72 65 61 74 65   index is create
248cc 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20  d successfully, 
248cd 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  return a pointer
248ce 20 74 6f 20 74 68 65 20 6e 65 77 20 49 6e 64 65   to the new Inde
248cf 78 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20  x.** structure. 
248d0 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20  This is used by 
248d1 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72  sqlite3AddPrimar
248d2 79 4b 65 79 28 29 20 74 6f 20 6d 61 72 6b 20 74  yKey() to mark t
248d3 68 65 20 69 6e 64 65 78 0a 2a 2a 20 61 73 20 74  he index.** as t
248d4 68 65 20 74 61 62 6c 65 73 20 70 72 69 6d 61 72  he tables primar
248d5 79 20 6b 65 79 20 28 49 6e 64 65 78 2e 61 75 74  y key (Index.aut
248d6 6f 49 6e 64 65 78 3d 3d 32 29 2e 0a 2a 2f 0a 53  oIndex==2)..*/.S
248d7 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 6e  QLITE_PRIVATE In
248d8 64 65 78 20 2a 73 71 6c 69 74 65 33 43 72 65 61  dex *sqlite3Crea
248d9 74 65 49 6e 64 65 78 28 0a 20 20 50 61 72 73 65  teIndex(.  Parse
248da 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a   *pParse,     /*
248db 20 41 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e   All information
248dc 20 61 62 6f 75 74 20 74 68 69 73 20 70 61 72 73   about this pars
248dd 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e  e */.  Token *pN
248de 61 6d 65 31 2c 20 20 20 20 20 2f 2a 20 46 69 72  ame1,     /* Fir
248df 73 74 20 70 61 72 74 20 6f 66 20 69 6e 64 65 78  st part of index
248e0 20 6e 61 6d 65 2e 20 4d 61 79 20 62 65 20 4e 55   name. May be NU
248e1 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  LL */.  Token *p
248e2 4e 61 6d 65 32 2c 20 20 20 20 20 2f 2a 20 53 65  Name2,     /* Se
248e3 63 6f 6e 64 20 70 61 72 74 20 6f 66 20 69 6e 64  cond part of ind
248e4 65 78 20 6e 61 6d 65 2e 20 4d 61 79 20 62 65 20  ex name. May be 
248e5 4e 55 4c 4c 20 2a 2f 0a 20 20 53 72 63 4c 69 73  NULL */.  SrcLis
248e6 74 20 2a 70 54 62 6c 4e 61 6d 65 2c 20 2f 2a 20  t *pTblName, /* 
248e7 54 61 62 6c 65 20 74 6f 20 69 6e 64 65 78 2e 20  Table to index. 
248e8 55 73 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  Use pParse->pNew
248e9 54 61 62 6c 65 20 69 66 20 30 20 2a 2f 0a 20 20  Table if 0 */.  
248ea 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c  ExprList *pList,
248eb 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20     /* A list of 
248ec 63 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e  columns to be in
248ed 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f  dexed */.  int o
248ee 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 20 2f 2a  nError,       /*
248ef 20 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67   OE_Abort, OE_Ig
248f0 6e 6f 72 65 2c 20 4f 45 5f 52 65 70 6c 61 63 65  nore, OE_Replace
248f1 2c 20 6f 72 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a  , or OE_None */.
248f2 20 20 54 6f 6b 65 6e 20 2a 70 53 74 61 72 74 2c    Token *pStart,
248f3 20 20 20 20 20 2f 2a 20 54 68 65 20 43 52 45 41       /* The CREA
248f4 54 45 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 65  TE token that be
248f5 67 69 6e 73 20 74 68 69 73 20 73 74 61 74 65 6d  gins this statem
248f6 65 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ent */.  Token *
248f7 70 45 6e 64 2c 20 20 20 20 20 20 20 2f 2a 20 54  pEnd,       /* T
248f8 68 65 20 22 29 22 20 74 68 61 74 20 63 6c 6f 73  he ")" that clos
248f9 65 73 20 74 68 65 20 43 52 45 41 54 45 20 49 4e  es the CREATE IN
248fa 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  DEX statement */
248fb 0a 20 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72  .  int sortOrder
248fc 2c 20 20 20 20 20 2f 2a 20 53 6f 72 74 20 6f 72  ,     /* Sort or
248fd 64 65 72 20 6f 66 20 70 72 69 6d 61 72 79 20 6b  der of primary k
248fe 65 79 20 77 68 65 6e 20 70 4c 69 73 74 3d 3d 4e  ey when pList==N
248ff 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 69 66 4e  ULL */.  int ifN
24900 6f 74 45 78 69 73 74 20 20 20 20 20 2f 2a 20 4f  otExist     /* O
24901 6d 69 74 20 65 72 72 6f 72 20 69 66 20 69 6e 64  mit error if ind
24902 65 78 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ex already exist
24903 73 20 2a 2f 0a 29 7b 0a 20 20 49 6e 64 65 78 20  s */.){.  Index 
24904 2a 70 52 65 74 20 3d 20 30 3b 20 20 20 20 20 2f  *pRet = 0;     /
24905 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 72 65 74  * Pointer to ret
24906 75 72 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  urn */.  Table *
24907 70 54 61 62 20 3d 20 30 3b 20 20 20 20 20 2f 2a  pTab = 0;     /*
24908 20 54 61 62 6c 65 20 74 6f 20 62 65 20 69 6e 64   Table to be ind
24909 65 78 65 64 20 2a 2f 0a 20 20 49 6e 64 65 78 20  exed */.  Index 
2490a 2a 70 49 6e 64 65 78 20 3d 20 30 3b 20 20 20 2f  *pIndex = 0;   /
2490b 2a 20 54 68 65 20 69 6e 64 65 78 20 74 6f 20 62  * The index to b
2490c 65 20 63 72 65 61 74 65 64 20 2a 2f 0a 20 20 63  e created */.  c
2490d 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20  har *zName = 0; 
2490e 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
2490f 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  he index */.  in
24910 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 20  t nName;        
24911 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
24912 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 4e  characters in zN
24913 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20  ame */.  int i, 
24914 6a 3b 0a 20 20 54 6f 6b 65 6e 20 6e 75 6c 6c 49  j;.  Token nullI
24915 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6b  d;        /* Fak
24916 65 20 74 6f 6b 65 6e 20 66 6f 72 20 61 6e 20 65  e token for an e
24917 6d 70 74 79 20 49 44 20 6c 69 73 74 20 2a 2f 0a  mpty ID list */.
24918 20 20 44 62 46 69 78 65 72 20 73 46 69 78 3b 20    DbFixer sFix; 
24919 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 61 73         /* For as
2491a 73 69 67 6e 69 6e 67 20 64 61 74 61 62 61 73 65  signing database
2491b 20 6e 61 6d 65 73 20 74 6f 20 70 54 61 62 6c 65   names to pTable
2491c 20 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 4f 72   */.  int sortOr
2491d 64 65 72 4d 61 73 6b 3b 20 20 20 2f 2a 20 31 20  derMask;   /* 1 
2491e 74 6f 20 68 6f 6e 6f 72 20 44 45 53 43 20 69 6e  to honor DESC in
2491f 20 69 6e 64 65 78 2e 20 20 30 20 74 6f 20 69 67   index.  0 to ig
24920 6e 6f 72 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  nore. */.  sqlit
24921 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
24922 3e 64 62 3b 0a 20 20 44 62 20 2a 70 44 62 3b 20  >db;.  Db *pDb; 
24923 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
24924 68 65 20 73 70 65 63 69 66 69 63 20 74 61 62 6c  he specific tabl
24925 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  e containing the
24926 20 69 6e 64 65 78 65 64 20 64 61 74 61 62 61 73   indexed databas
24927 65 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  e */.  int iDb; 
24928 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
24929 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
2492a 62 61 73 65 20 74 68 61 74 20 69 73 20 62 65 69  base that is bei
2492b 6e 67 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20  ng written */.  
2492c 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20 3d 20 30  Token *pName = 0
2492d 3b 20 20 20 20 2f 2a 20 55 6e 71 75 61 6c 69 66  ;    /* Unqualif
2492e 69 65 64 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  ied name of the 
2492f 69 6e 64 65 78 20 74 6f 20 63 72 65 61 74 65 20  index to create 
24930 2a 2f 0a 20 20 73 74 72 75 63 74 20 45 78 70 72  */.  struct Expr
24931 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c 69 73 74  List_item *pList
24932 49 74 65 6d 3b 20 2f 2a 20 46 6f 72 20 6c 6f 6f  Item; /* For loo
24933 70 69 6e 67 20 6f 76 65 72 20 70 4c 69 73 74 20  ping over pList 
24934 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20  */.  int nCol;. 
24935 20 69 6e 74 20 6e 45 78 74 72 61 20 3d 20 30 3b   int nExtra = 0;
24936 0a 20 20 63 68 61 72 20 2a 7a 45 78 74 72 61 3b  .  char *zExtra;
24937 0a 0a 20 20 61 73 73 65 72 74 28 20 70 53 74 61  ..  assert( pSta
24938 72 74 3d 3d 30 20 7c 7c 20 70 45 6e 64 21 3d 30  rt==0 || pEnd!=0
24939 20 29 3b 20 2f 2a 20 70 45 6e 64 20 6d 75 73 74   ); /* pEnd must
2493a 20 62 65 20 6e 6f 6e 2d 4e 55 4c 4c 20 69 66 20   be non-NULL if 
2493b 70 53 74 61 72 74 20 69 73 20 2a 2f 0a 20 20 61  pStart is */.  a
2493c 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e  ssert( pParse->n
2493d 45 72 72 3d 3d 30 20 29 3b 20 20 20 20 20 20 2f  Err==0 );      /
2493e 2a 20 4e 65 76 65 72 20 63 61 6c 6c 65 64 20 77  * Never called w
2493f 69 74 68 20 70 72 69 6f 72 20 65 72 72 6f 72 73  ith prior errors
24940 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61   */.  if( db->ma
24941 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 49 4e  llocFailed || IN
24942 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29 7b  _DECLARE_VTAB ){
24943 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63  .    goto exit_c
24944 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d  reate_index;.  }
24945 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
24946 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  !=sqlite3ReadSch
24947 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20  ema(pParse) ){. 
24948 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65     goto exit_cre
24949 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 0a  ate_index;.  }..
2494a 20 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 74    /*.  ** Find t
2494b 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 69 73  he table that is
2494c 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20   to be indexed. 
2494d 20 52 65 74 75 72 6e 20 65 61 72 6c 79 20 69 66   Return early if
2494e 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 20 20 2a 2f   not found..  */
2494f 0a 20 20 69 66 28 20 70 54 62 6c 4e 61 6d 65 21  .  if( pTblName!
24950 3d 30 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 55 73  =0 ){..    /* Us
24951 65 20 74 68 65 20 74 77 6f 2d 70 61 72 74 20 69  e the two-part i
24952 6e 64 65 78 20 6e 61 6d 65 20 74 6f 20 64 65 74  ndex name to det
24953 65 72 6d 69 6e 65 20 74 68 65 20 64 61 74 61 62  ermine the datab
24954 61 73 65 20 0a 20 20 20 20 2a 2a 20 74 6f 20 73  ase .    ** to s
24955 65 61 72 63 68 20 66 6f 72 20 74 68 65 20 74 61  earch for the ta
24956 62 6c 65 2e 20 27 46 69 78 27 20 74 68 65 20 74  ble. 'Fix' the t
24957 61 62 6c 65 20 6e 61 6d 65 20 74 6f 20 74 68 69  able name to thi
24958 73 20 64 62 0a 20 20 20 20 2a 2a 20 62 65 66 6f  s db.    ** befo
24959 72 65 20 6c 6f 6f 6b 69 6e 67 20 75 70 20 74 68  re looking up th
2495a 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a  e table..    */.
2495b 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d      assert( pNam
2495c 65 31 20 26 26 20 70 4e 61 6d 65 32 20 29 3b 0a  e1 && pName2 );.
2495d 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65      iDb = sqlite
2495e 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50 61  3TwoPartName(pPa
2495f 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e 61  rse, pName1, pNa
24960 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b 0a 20 20  me2, &pName);.  
24961 20 20 69 66 28 20 69 44 62 3c 30 20 29 20 67 6f    if( iDb<0 ) go
24962 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
24963 6e 64 65 78 3b 0a 0a 23 69 66 6e 64 65 66 20 53  ndex;..#ifndef S
24964 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44  QLITE_OMIT_TEMPD
24965 42 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20  B.    /* If the 
24966 69 6e 64 65 78 20 6e 61 6d 65 20 77 61 73 20 75  index name was u
24967 6e 71 75 61 6c 69 66 69 65 64 2c 20 63 68 65 63  nqualified, chec
24968 6b 20 69 66 20 74 68 65 20 74 68 65 20 74 61 62  k if the the tab
24969 6c 65 0a 20 20 20 20 2a 2a 20 69 73 20 61 20 74  le.    ** is a t
2496a 65 6d 70 20 74 61 62 6c 65 2e 20 49 66 20 73 6f  emp table. If so
2496b 2c 20 73 65 74 20 74 68 65 20 64 61 74 61 62 61  , set the databa
2496c 73 65 20 74 6f 20 31 2e 20 44 6f 20 6e 6f 74 20  se to 1. Do not 
2496d 64 6f 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69  do this.    ** i
2496e 66 20 69 6e 69 74 69 61 6c 69 73 69 6e 67 20 61  f initialising a
2496f 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
24970 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
24971 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20   !db->init.busy 
24972 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20  ){.      pTab = 
24973 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f  sqlite3SrcListLo
24974 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54 62  okup(pParse, pTb
24975 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66  lName);.      if
24976 28 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d 30 20 26  ( pName2->n==0 &
24977 26 20 70 54 61 62 20 26 26 20 70 54 61 62 2d 3e  & pTab && pTab->
24978 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62  pSchema==db->aDb
24979 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 7b 0a 20  [1].pSchema ){. 
2497a 20 20 20 20 20 20 20 69 44 62 20 3d 20 31 3b 0a         iDb = 1;.
2497b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65        }.    }.#e
2497c 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20 73 71  ndif..    if( sq
2497d 6c 69 74 65 33 46 69 78 49 6e 69 74 28 26 73 46  lite3FixInit(&sF
2497e 69 78 2c 20 70 50 61 72 73 65 2c 20 69 44 62 2c  ix, pParse, iDb,
2497f 20 22 69 6e 64 65 78 22 2c 20 70 4e 61 6d 65 29   "index", pName)
24980 20 26 26 0a 20 20 20 20 20 20 20 20 73 71 6c 69   &&.        sqli
24981 74 65 33 46 69 78 53 72 63 4c 69 73 74 28 26 73  te3FixSrcList(&s
24982 46 69 78 2c 20 70 54 62 6c 4e 61 6d 65 29 0a 20  Fix, pTblName). 
24983 20 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 42     ){.      /* B
24984 65 63 61 75 73 65 20 74 68 65 20 70 61 72 73 65  ecause the parse
24985 72 20 63 6f 6e 73 74 72 75 63 74 73 20 70 54 62  r constructs pTb
24986 6c 4e 61 6d 65 20 66 72 6f 6d 20 61 20 73 69 6e  lName from a sin
24987 67 6c 65 20 69 64 65 6e 74 69 66 69 65 72 2c 0a  gle identifier,.
24988 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33        ** sqlite3
24989 46 69 78 53 72 63 4c 69 73 74 20 63 61 6e 20 6e  FixSrcList can n
2498a 65 76 65 72 20 66 61 69 6c 2e 20 2a 2f 0a 20 20  ever fail. */.  
2498b 20 20 20 20 61 73 73 65 72 74 28 30 29 3b 0a 20      assert(0);. 
2498c 20 20 20 7d 0a 20 20 20 20 70 54 61 62 20 3d 20     }.    pTab = 
2498d 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62  sqlite3LocateTab
2498e 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 54  le(pParse, 0, pT
2498f 62 6c 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61  blName->a[0].zNa
24990 6d 65 2c 20 0a 20 20 20 20 20 20 20 20 70 54 62  me, .        pTb
24991 6c 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74  lName->a[0].zDat
24992 61 62 61 73 65 29 3b 0a 20 20 20 20 69 66 28 20  abase);.    if( 
24993 21 70 54 61 62 20 7c 7c 20 64 62 2d 3e 6d 61 6c  !pTab || db->mal
24994 6c 6f 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f  locFailed ) goto
24995 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
24996 65 78 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ex;.    assert( 
24997 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63  db->aDb[iDb].pSc
24998 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68  hema==pTab->pSch
24999 65 6d 61 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  ema );.  }else{.
2499a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d      assert( pNam
2499b 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70 54 61 62  e==0 );.    pTab
2499c 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
2499d 61 62 6c 65 3b 0a 20 20 20 20 69 66 28 20 21 70  able;.    if( !p
2499e 54 61 62 20 29 20 67 6f 74 6f 20 65 78 69 74 5f  Tab ) goto exit_
2499f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
249a0 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
249a1 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
249a2 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
249a3 0a 20 20 7d 0a 20 20 70 44 62 20 3d 20 26 64 62  .  }.  pDb = &db
249a4 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a 20 20 61  ->aDb[iDb];..  a
249a5 73 73 65 72 74 28 20 70 54 61 62 21 3d 30 20 29  ssert( pTab!=0 )
249a6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ;.  assert( pPar
249a7 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 3b 0a 20  se->nErr==0 );. 
249a8 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e   if( sqlite3StrN
249a9 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d 65  ICmp(pTab->zName
249aa 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29 3d  , "sqlite_", 7)=
249ab 3d 30 20 0a 20 20 20 20 20 20 20 26 26 20 6d 65  =0 .       && me
249ac 6d 63 6d 70 28 26 70 54 61 62 2d 3e 7a 4e 61 6d  mcmp(&pTab->zNam
249ad 65 5b 37 5d 2c 22 61 6c 74 65 72 74 61 62 5f 22  e[7],"altertab_"
249ae 2c 39 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71  ,9)!=0 ){.    sq
249af 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
249b0 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20  arse, "table %s 
249b1 6d 61 79 20 6e 6f 74 20 62 65 20 69 6e 64 65 78  may not be index
249b2 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ed", pTab->zName
249b3 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74  );.    goto exit
249b4 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
249b5 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
249b6 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 69 66  E_OMIT_VIEW.  if
249b7 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  ( pTab->pSelect 
249b8 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
249b9 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
249ba 76 69 65 77 73 20 6d 61 79 20 6e 6f 74 20 62 65  views may not be
249bb 20 69 6e 64 65 78 65 64 22 29 3b 0a 20 20 20 20   indexed");.    
249bc 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
249bd 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 23 65 6e 64  _index;.  }.#end
249be 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  if.#ifndef SQLIT
249bf 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
249c0 42 4c 45 0a 20 20 69 66 28 20 49 73 56 69 72 74  BLE.  if( IsVirt
249c1 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
249c2 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
249c3 28 70 50 61 72 73 65 2c 20 22 76 69 72 74 75 61  (pParse, "virtua
249c4 6c 20 74 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74  l tables may not
249c5 20 62 65 20 69 6e 64 65 78 65 64 22 29 3b 0a 20   be indexed");. 
249c6 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65     goto exit_cre
249c7 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 23  ate_index;.  }.#
249c8 65 6e 64 69 66 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  endif..  /*.  **
249c9 20 46 69 6e 64 20 74 68 65 20 6e 61 6d 65 20 6f   Find the name o
249ca 66 20 74 68 65 20 69 6e 64 65 78 2e 20 20 4d 61  f the index.  Ma
249cb 6b 65 20 73 75 72 65 20 74 68 65 72 65 20 69 73  ke sure there is
249cc 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 6e 6f   not already ano
249cd 74 68 65 72 0a 20 20 2a 2a 20 69 6e 64 65 78 20  ther.  ** index 
249ce 6f 72 20 74 61 62 6c 65 20 77 69 74 68 20 74 68  or table with th
249cf 65 20 73 61 6d 65 20 6e 61 6d 65 2e 20 20 0a 20  e same name.  . 
249d0 20 2a 2a 0a 20 20 2a 2a 20 45 78 63 65 70 74 69   **.  ** Excepti
249d1 6f 6e 3a 20 20 49 66 20 77 65 20 61 72 65 20 72  on:  If we are r
249d2 65 61 64 69 6e 67 20 74 68 65 20 6e 61 6d 65 73  eading the names
249d3 20 6f 66 20 70 65 72 6d 61 6e 65 6e 74 20 69 6e   of permanent in
249d4 64 69 63 65 73 20 66 72 6f 6d 20 74 68 65 0a 20  dices from the. 
249d5 20 2a 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65   ** sqlite_maste
249d6 72 20 74 61 62 6c 65 20 28 62 65 63 61 75 73 65  r table (because
249d7 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63   some other proc
249d8 65 73 73 20 63 68 61 6e 67 65 64 20 74 68 65 20  ess changed the 
249d9 73 63 68 65 6d 61 29 20 61 6e 64 0a 20 20 2a 2a  schema) and.  **
249da 20 6f 6e 65 20 6f 66 20 74 68 65 20 69 6e 64 65   one of the inde
249db 78 20 6e 61 6d 65 73 20 63 6f 6c 6c 69 64 65 73  x names collides
249dc 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65 20 6f   with the name o
249dd 66 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61  f a temporary ta
249de 62 6c 65 20 6f 72 0a 20 20 2a 2a 20 69 6e 64 65  ble or.  ** inde
249df 78 2c 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20  x, then we will 
249e0 63 6f 6e 74 69 6e 75 65 20 74 6f 20 70 72 6f 63  continue to proc
249e1 65 73 73 20 74 68 69 73 20 69 6e 64 65 78 2e 0a  ess this index..
249e2 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70 4e 61    **.  ** If pNa
249e3 6d 65 3d 3d 30 20 69 74 20 6d 65 61 6e 73 20 74  me==0 it means t
249e4 68 61 74 20 77 65 20 61 72 65 0a 20 20 2a 2a 20  hat we are.  ** 
249e5 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20 70  dealing with a p
249e6 72 69 6d 61 72 79 20 6b 65 79 20 6f 72 20 55 4e  rimary key or UN
249e7 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e  IQUE constraint.
249e8 20 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e 76    We have to inv
249e9 65 6e 74 20 6f 75 72 0a 20 20 2a 2a 20 6f 77 6e  ent our.  ** own
249ea 20 6e 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   name..  */.  if
249eb 28 20 70 4e 61 6d 65 20 29 7b 0a 20 20 20 20 7a  ( pName ){.    z
249ec 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61  Name = sqlite3Na
249ed 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
249ee 70 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20  pName);.    if( 
249ef 7a 4e 61 6d 65 3d 3d 30 20 29 20 67 6f 74 6f 20  zName==0 ) goto 
249f0 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
249f1 78 3b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54  x;.    if( SQLIT
249f2 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68 65  E_OK!=sqlite3Che
249f3 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50 61  ckObjectName(pPa
249f4 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20  rse, zName) ){. 
249f5 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63       goto exit_c
249f6 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20  reate_index;.   
249f7 20 7d 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e   }.    if( !db->
249f8 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20  init.busy ){.   
249f9 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69     if( sqlite3Fi
249fa 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61 6d  ndTable(db, zNam
249fb 65 2c 20 30 29 21 3d 30 20 29 7b 0a 20 20 20 20  e, 0)!=0 ){.    
249fc 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
249fd 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 68 65  Msg(pParse, "the
249fe 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  re is already a 
249ff 74 61 62 6c 65 20 6e 61 6d 65 64 20 25 73 22 2c  table named %s",
24a00 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20   zName);.       
24a01 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
24a02 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 20 20 7d  e_index;.      }
24a03 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
24a04 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
24a05 64 62 2c 20 7a 4e 61 6d 65 2c 20 70 44 62 2d 3e  db, zName, pDb->
24a06 7a 4e 61 6d 65 29 21 3d 30 20 29 7b 0a 20 20 20  zName)!=0 ){.   
24a07 20 20 20 69 66 28 20 21 69 66 4e 6f 74 45 78 69     if( !ifNotExi
24a08 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  st ){.        sq
24a09 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
24a0a 61 72 73 65 2c 20 22 69 6e 64 65 78 20 25 73 20  arse, "index %s 
24a0b 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 22 2c  already exists",
24a0c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d   zName);.      }
24a0d 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
24a0e 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
24a0f 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20     }.  }else{.  
24a10 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20 49 6e 64    int n;.    Ind
24a11 65 78 20 2a 70 4c 6f 6f 70 3b 0a 20 20 20 20 66  ex *pLoop;.    f
24a12 6f 72 28 70 4c 6f 6f 70 3d 70 54 61 62 2d 3e 70  or(pLoop=pTab->p
24a13 49 6e 64 65 78 2c 20 6e 3d 31 3b 20 70 4c 6f 6f  Index, n=1; pLoo
24a14 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f 6f 70 2d 3e  p; pLoop=pLoop->
24a15 70 4e 65 78 74 2c 20 6e 2b 2b 29 7b 7d 0a 20 20  pNext, n++){}.  
24a16 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
24a17 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 73 71  3MPrintf(db, "sq
24a18 6c 69 74 65 5f 61 75 74 6f 69 6e 64 65 78 5f 25  lite_autoindex_%
24a19 73 5f 25 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61  s_%d", pTab->zNa
24a1a 6d 65 2c 20 6e 29 3b 0a 20 20 20 20 69 66 28 20  me, n);.    if( 
24a1b 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20  zName==0 ){.    
24a1c 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
24a1d 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a  te_index;.    }.
24a1e 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20    }..  /* Check 
24a1f 66 6f 72 20 61 75 74 68 6f 72 69 7a 61 74 69 6f  for authorizatio
24a20 6e 20 74 6f 20 63 72 65 61 74 65 20 61 6e 20 69  n to create an i
24a21 6e 64 65 78 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64  ndex..  */.#ifnd
24a22 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
24a23 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 7b  UTHORIZATION.  {
24a24 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
24a25 2a 7a 44 62 20 3d 20 70 44 62 2d 3e 7a 4e 61 6d  *zDb = pDb->zNam
24a26 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  e;.    if( sqlit
24a27 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
24a28 73 65 2c 20 53 51 4c 49 54 45 5f 49 4e 53 45 52  se, SQLITE_INSER
24a29 54 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28  T, SCHEMA_TABLE(
24a2a 69 44 62 29 2c 20 30 2c 20 7a 44 62 29 20 29 7b  iDb), 0, zDb) ){
24a2b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
24a2c 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
24a2d 20 20 20 7d 0a 20 20 20 20 69 20 3d 20 53 51 4c     }.    i = SQL
24a2e 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44 45 58  ITE_CREATE_INDEX
24a2f 3b 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f  ;.    if( !OMIT_
24a30 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31  TEMPDB && iDb==1
24a31 20 29 20 69 20 3d 20 53 51 4c 49 54 45 5f 43 52   ) i = SQLITE_CR
24a32 45 41 54 45 5f 54 45 4d 50 5f 49 4e 44 45 58 3b  EATE_TEMP_INDEX;
24a33 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
24a34 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
24a35 2c 20 69 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62  , i, zName, pTab
24a36 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b  ->zName, zDb) ){
24a37 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
24a38 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
24a39 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a     }.  }.#endif.
24a3a 0a 20 20 2f 2a 20 49 66 20 70 4c 69 73 74 3d 3d  .  /* If pList==
24a3b 30 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 69 73  0, it means this
24a3c 20 72 6f 75 74 69 6e 65 20 77 61 73 20 63 61 6c   routine was cal
24a3d 6c 65 64 20 74 6f 20 6d 61 6b 65 20 61 20 70 72  led to make a pr
24a3e 69 6d 61 72 79 0a 20 20 2a 2a 20 6b 65 79 20 6f  imary.  ** key o
24a3f 75 74 20 6f 66 20 74 68 65 20 6c 61 73 74 20 63  ut of the last c
24a40 6f 6c 75 6d 6e 20 61 64 64 65 64 20 74 6f 20 74  olumn added to t
24a41 68 65 20 74 61 62 6c 65 20 75 6e 64 65 72 20 63  he table under c
24a42 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 20 20 2a  onstruction..  *
24a43 2a 20 53 6f 20 63 72 65 61 74 65 20 61 20 66 61  * So create a fa
24a44 6b 65 20 6c 69 73 74 20 74 6f 20 73 69 6d 75 6c  ke list to simul
24a45 61 74 65 20 74 68 69 73 2e 0a 20 20 2a 2f 0a 20  ate this..  */. 
24a46 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b   if( pList==0 ){
24a47 0a 20 20 20 20 6e 75 6c 6c 49 64 2e 7a 20 3d 20  .    nullId.z = 
24a48 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 54 61 62 2d  pTab->aCol[pTab-
24a49 3e 6e 43 6f 6c 2d 31 5d 2e 7a 4e 61 6d 65 3b 0a  >nCol-1].zName;.
24a4a 20 20 20 20 6e 75 6c 6c 49 64 2e 6e 20 3d 20 73      nullId.n = s
24a4b 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 28  qlite3Strlen30((
24a4c 63 68 61 72 2a 29 6e 75 6c 6c 49 64 2e 7a 29 3b  char*)nullId.z);
24a4d 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c  .    pList = sql
24a4e 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
24a4f 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20 30 29  nd(pParse, 0, 0)
24a50 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d  ;.    if( pList=
24a51 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 63  =0 ) goto exit_c
24a52 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20  reate_index;.   
24a53 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
24a54 53 65 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  SetName(pParse, 
24a55 70 4c 69 73 74 2c 20 26 6e 75 6c 6c 49 64 2c 20  pList, &nullId, 
24a56 30 29 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 61  0);.    pList->a
24a57 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20  [0].sortOrder = 
24a58 28 75 38 29 73 6f 72 74 4f 72 64 65 72 3b 0a 20  (u8)sortOrder;. 
24a59 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20   }..  /* Figure 
24a5a 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20 62 79 74  out how many byt
24a5b 65 73 20 6f 66 20 73 70 61 63 65 20 61 72 65 20  es of space are 
24a5c 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72  required to stor
24a5d 65 20 65 78 70 6c 69 63 69 74 6c 79 0a 20 20 2a  e explicitly.  *
24a5e 2a 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c 6c  * specified coll
24a5f 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e  ation sequence n
24a60 61 6d 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  ames..  */.  for
24a61 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  (i=0; i<pList->n
24a62 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
24a63 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 4c  Expr *pExpr = pL
24a64 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b  ist->a[i].pExpr;
24a65 0a 20 20 20 20 69 66 28 20 70 45 78 70 72 20 29  .    if( pExpr )
24a66 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20  {.      CollSeq 
24a67 2a 70 43 6f 6c 6c 20 3d 20 70 45 78 70 72 2d 3e  *pColl = pExpr->
24a68 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 2f 2a 20  pColl;.      /* 
24a69 45 69 74 68 65 72 20 70 43 6f 6c 6c 21 3d 30 20  Either pColl!=0 
24a6a 6f 72 20 74 68 65 72 65 20 77 61 73 20 61 6e 20  or there was an 
24a6b 4f 4f 4d 20 66 61 69 6c 75 72 65 2e 20 20 42 75  OOM failure.  Bu
24a6c 74 20 69 66 20 61 6e 20 4f 4f 4d 0a 20 20 20 20  t if an OOM.    
24a6d 20 20 2a 2a 20 66 61 69 6c 75 72 65 20 77 65 20    ** failure we 
24a6e 68 61 76 65 20 71 75 69 74 20 62 65 66 6f 72 65  have quit before
24a6f 20 72 65 61 63 68 69 6e 67 20 74 68 69 73 20 70   reaching this p
24a70 6f 69 6e 74 2e 20 2a 2f 0a 20 20 20 20 20 20 69  oint. */.      i
24a71 66 28 20 41 4c 57 41 59 53 28 70 43 6f 6c 6c 29  f( ALWAYS(pColl)
24a72 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 45 78 74   ){.        nExt
24a73 72 61 20 2b 3d 20 28 31 20 2b 20 73 71 6c 69 74  ra += (1 + sqlit
24a74 65 33 53 74 72 6c 65 6e 33 30 28 70 43 6f 6c 6c  e3Strlen30(pColl
24a75 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20  ->zName));.     
24a76 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
24a77 2f 2a 20 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74  /* .  ** Allocat
24a78 65 20 74 68 65 20 69 6e 64 65 78 20 73 74 72 75  e the index stru
24a79 63 74 75 72 65 2e 20 0a 20 20 2a 2f 0a 20 20 6e  cture. .  */.  n
24a7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74  Name = sqlite3St
24a7b 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a 20  rlen30(zName);. 
24a7c 20 6e 43 6f 6c 20 3d 20 70 4c 69 73 74 2d 3e 6e   nCol = pList->n
24a7d 45 78 70 72 3b 0a 20 20 70 49 6e 64 65 78 20 3d  Expr;.  pIndex =
24a7e 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
24a7f 5a 65 72 6f 28 64 62 2c 20 0a 20 20 20 20 20 20  Zero(db, .      
24a80 73 69 7a 65 6f 66 28 49 6e 64 65 78 29 20 2b 20  sizeof(Index) + 
24a81 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
24a82 49 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20  Index structure 
24a83 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 6f 66   */.      sizeof
24a84 28 69 6e 74 29 2a 6e 43 6f 6c 20 2b 20 20 20 20  (int)*nCol +    
24a85 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e         /* Index.
24a86 61 69 43 6f 6c 75 6d 6e 20 20 20 2a 2f 0a 20 20  aiColumn   */.  
24a87 20 20 20 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a      sizeof(int)*
24a88 28 6e 43 6f 6c 2b 31 29 20 2b 20 20 20 20 20 20  (nCol+1) +      
24a89 20 2f 2a 20 49 6e 64 65 78 2e 61 69 52 6f 77 45   /* Index.aiRowE
24a8a 73 74 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 69  st   */.      si
24a8b 7a 65 6f 66 28 63 68 61 72 20 2a 29 2a 6e 43 6f  zeof(char *)*nCo
24a8c 6c 20 2b 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  l +        /* In
24a8d 64 65 78 2e 61 7a 43 6f 6c 6c 20 20 20 20 20 2a  dex.azColl     *
24a8e 2f 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 75  /.      sizeof(u
24a8f 38 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 20 20  8)*nCol +       
24a90 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 61 53       /* Index.aS
24a91 6f 72 74 4f 72 64 65 72 20 2a 2f 0a 20 20 20 20  ortOrder */.    
24a92 20 20 6e 4e 61 6d 65 20 2b 20 31 20 2b 20 20 20    nName + 1 +   
24a93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
24a94 2a 20 49 6e 64 65 78 2e 7a 4e 61 6d 65 20 20 20  * Index.zName   
24a95 20 20 20 2a 2f 0a 20 20 20 20 20 20 6e 45 78 74     */.      nExt
24a96 72 61 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ra              
24a97 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c           /* Coll
24a98 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e  ation sequence n
24a99 61 6d 65 73 20 2a 2f 0a 20 20 29 3b 0a 20 20 69  ames */.  );.  i
24a9a 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
24a9b 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  led ){.    goto 
24a9c 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65  exit_create_inde
24a9d 78 3b 0a 20 20 7d 0a 20 20 70 49 6e 64 65 78 2d  x;.  }.  pIndex-
24a9e 3e 61 7a 43 6f 6c 6c 20 3d 20 28 63 68 61 72 2a  >azColl = (char*
24a9f 2a 29 28 26 70 49 6e 64 65 78 5b 31 5d 29 3b 0a  *)(&pIndex[1]);.
24aa0 20 20 70 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75    pIndex->aiColu
24aa1 6d 6e 20 3d 20 28 69 6e 74 20 2a 29 28 26 70 49  mn = (int *)(&pI
24aa2 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 6e 43 6f  ndex->azColl[nCo
24aa3 6c 5d 29 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 61  l]);.  pIndex->a
24aa4 69 52 6f 77 45 73 74 20 3d 20 28 75 6e 73 69 67  iRowEst = (unsig
24aa5 6e 65 64 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e  ned *)(&pIndex->
24aa6 61 69 43 6f 6c 75 6d 6e 5b 6e 43 6f 6c 5d 29 3b  aiColumn[nCol]);
24aa7 0a 20 20 70 49 6e 64 65 78 2d 3e 61 53 6f 72 74  .  pIndex->aSort
24aa8 4f 72 64 65 72 20 3d 20 28 75 38 20 2a 29 28 26  Order = (u8 *)(&
24aa9 70 49 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73 74  pIndex->aiRowEst
24aaa 5b 6e 43 6f 6c 2b 31 5d 29 3b 0a 20 20 70 49 6e  [nCol+1]);.  pIn
24aab 64 65 78 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68  dex->zName = (ch
24aac 61 72 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61  ar *)(&pIndex->a
24aad 53 6f 72 74 4f 72 64 65 72 5b 6e 43 6f 6c 5d 29  SortOrder[nCol])
24aae 3b 0a 20 20 7a 45 78 74 72 61 20 3d 20 28 63 68  ;.  zExtra = (ch
24aaf 61 72 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 7a  ar *)(&pIndex->z
24ab0 4e 61 6d 65 5b 6e 4e 61 6d 65 2b 31 5d 29 3b 0a  Name[nName+1]);.
24ab1 20 20 6d 65 6d 63 70 79 28 70 49 6e 64 65 78 2d    memcpy(pIndex-
24ab2 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e  >zName, zName, n
24ab3 4e 61 6d 65 2b 31 29 3b 0a 20 20 70 49 6e 64 65  Name+1);.  pInde
24ab4 78 2d 3e 70 54 61 62 6c 65 20 3d 20 70 54 61 62  x->pTable = pTab
24ab5 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c  ;.  pIndex->nCol
24ab6 75 6d 6e 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78  umn = pList->nEx
24ab7 70 72 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 6f 6e  pr;.  pIndex->on
24ab8 45 72 72 6f 72 20 3d 20 28 75 38 29 6f 6e 45 72  Error = (u8)onEr
24ab9 72 6f 72 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 61  ror;.  pIndex->a
24aba 75 74 6f 49 6e 64 65 78 20 3d 20 28 75 38 29 28  utoIndex = (u8)(
24abb 70 4e 61 6d 65 3d 3d 30 29 3b 0a 20 20 70 49 6e  pName==0);.  pIn
24abc 64 65 78 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64  dex->pSchema = d
24abd 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68  b->aDb[iDb].pSch
24abe 65 6d 61 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  ema;..  /* Check
24abf 20 74 6f 20 73 65 65 20 69 66 20 77 65 20 73 68   to see if we sh
24ac0 6f 75 6c 64 20 68 6f 6e 6f 72 20 44 45 53 43 20  ould honor DESC 
24ac1 72 65 71 75 65 73 74 73 20 6f 6e 20 69 6e 64 65  requests on inde
24ac2 78 20 63 6f 6c 75 6d 6e 73 0a 20 20 2a 2f 0a 20  x columns.  */. 
24ac3 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d   if( pDb->pSchem
24ac4 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 3e 3d  a->file_format>=
24ac5 34 20 29 7b 0a 20 20 20 20 73 6f 72 74 4f 72 64  4 ){.    sortOrd
24ac6 65 72 4d 61 73 6b 20 3d 20 2d 31 3b 20 20 20 2f  erMask = -1;   /
24ac7 2a 20 48 6f 6e 6f 72 20 44 45 53 43 20 2a 2f 0a  * Honor DESC */.
24ac8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 6f 72    }else{.    sor
24ac9 74 4f 72 64 65 72 4d 61 73 6b 20 3d 20 30 3b 20  tOrderMask = 0; 
24aca 20 20 20 2f 2a 20 49 67 6e 6f 72 65 20 44 45 53     /* Ignore DES
24acb 43 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53  C */.  }..  /* S
24acc 63 61 6e 20 74 68 65 20 6e 61 6d 65 73 20 6f 66  can the names of
24acd 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20   the columns of 
24ace 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20  the table to be 
24acf 69 6e 64 65 78 65 64 20 61 6e 64 0a 20 20 2a 2a  indexed and.  **
24ad0 20 6c 6f 61 64 20 74 68 65 20 63 6f 6c 75 6d 6e   load the column
24ad1 20 69 6e 64 69 63 65 73 20 69 6e 74 6f 20 74 68   indices into th
24ad2 65 20 49 6e 64 65 78 20 73 74 72 75 63 74 75 72  e Index structur
24ad3 65 2e 20 20 52 65 70 6f 72 74 20 61 6e 20 65 72  e.  Report an er
24ad4 72 6f 72 0a 20 20 2a 2a 20 69 66 20 61 6e 79 20  ror.  ** if any 
24ad5 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 66 6f  column is not fo
24ad6 75 6e 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  und..  **.  ** T
24ad7 4f 44 4f 3a 20 20 41 64 64 20 61 20 74 65 73 74  ODO:  Add a test
24ad8 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
24ad9 61 74 20 74 68 65 20 73 61 6d 65 20 63 6f 6c 75  at the same colu
24ada 6d 6e 20 69 73 20 6e 6f 74 20 6e 61 6d 65 64 0a  mn is not named.
24adb 20 20 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f    ** more than o
24adc 6e 63 65 20 77 69 74 68 69 6e 20 74 68 65 20 73  nce within the s
24add 61 6d 65 20 69 6e 64 65 78 2e 20 20 4f 6e 6c 79  ame index.  Only
24ade 20 74 68 65 20 66 69 72 73 74 20 69 6e 73 74 61   the first insta
24adf 6e 63 65 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20  nce of.  ** the 
24ae0 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 65 76 65 72  column will ever
24ae1 20 62 65 20 75 73 65 64 20 62 79 20 74 68 65 20   be used by the 
24ae2 6f 70 74 69 6d 69 7a 65 72 2e 20 20 4e 6f 74 65  optimizer.  Note
24ae3 20 74 68 61 74 20 75 73 69 6e 67 20 74 68 65 0a   that using the.
24ae4 20 20 2a 2a 20 73 61 6d 65 20 63 6f 6c 75 6d 6e    ** same column
24ae5 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 20   more than once 
24ae6 63 61 6e 6e 6f 74 20 62 65 20 61 6e 20 65 72 72  cannot be an err
24ae7 6f 72 20 62 65 63 61 75 73 65 20 74 68 61 74 20  or because that 
24ae8 77 6f 75 6c 64 20 0a 20 20 2a 2a 20 62 72 65 61  would .  ** brea
24ae9 6b 20 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70  k backwards comp
24aea 61 74 69 62 69 6c 69 74 79 20 2d 20 69 74 20 6e  atibility - it n
24aeb 65 65 64 73 20 74 6f 20 62 65 20 61 20 77 61 72  eeds to be a war
24aec 6e 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  ning..  */.  for
24aed 28 69 3d 30 2c 20 70 4c 69 73 74 49 74 65 6d 3d  (i=0, pListItem=
24aee 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73  pList->a; i<pLis
24aef 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70  t->nExpr; i++, p
24af0 4c 69 73 74 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  ListItem++){.   
24af1 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f   const char *zCo
24af2 6c 4e 61 6d 65 20 3d 20 70 4c 69 73 74 49 74 65  lName = pListIte
24af3 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 43 6f  m->zName;.    Co
24af4 6c 75 6d 6e 20 2a 70 54 61 62 43 6f 6c 3b 0a 20  lumn *pTabCol;. 
24af5 20 20 20 69 6e 74 20 72 65 71 75 65 73 74 65 64     int requested
24af6 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 63  SortOrder;.    c
24af7 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20  har *zColl;     
24af8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
24af9 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   Collation seque
24afa 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a 0a 20 20 20  nce name */..   
24afb 20 66 6f 72 28 6a 3d 30 2c 20 70 54 61 62 43 6f   for(j=0, pTabCo
24afc 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b 20 6a 3c  l=pTab->aCol; j<
24afd 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 2c  pTab->nCol; j++,
24afe 20 70 54 61 62 43 6f 6c 2b 2b 29 7b 0a 20 20 20   pTabCol++){.   
24aff 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
24b00 72 49 43 6d 70 28 7a 43 6f 6c 4e 61 6d 65 2c 20  rICmp(zColName, 
24b01 70 54 61 62 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3d  pTabCol->zName)=
24b02 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
24b03 7d 0a 20 20 20 20 69 66 28 20 6a 3e 3d 70 54 61  }.    if( j>=pTa
24b04 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20  b->nCol ){.     
24b05 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
24b06 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20  (pParse, "table 
24b07 25 73 20 68 61 73 20 6e 6f 20 63 6f 6c 75 6d 6e  %s has no column
24b08 20 6e 61 6d 65 64 20 25 73 22 2c 0a 20 20 20 20   named %s",.    
24b09 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c      pTab->zName,
24b0a 20 7a 43 6f 6c 4e 61 6d 65 29 3b 0a 20 20 20 20   zColName);.    
24b0b 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
24b0c 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a  te_index;.    }.
24b0d 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 69 43 6f      pIndex->aiCo
24b0e 6c 75 6d 6e 5b 69 5d 20 3d 20 6a 3b 0a 20 20 20  lumn[i] = j;.   
24b0f 20 2f 2a 20 4a 75 73 74 69 66 69 63 61 74 69 6f   /* Justificatio
24b10 6e 20 6f 66 20 74 68 65 20 41 4c 57 41 59 53 28  n of the ALWAYS(
24b11 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 72  pListItem->pExpr
24b12 2d 3e 70 43 6f 6c 6c 29 3a 20 20 42 65 63 61 75  ->pColl):  Becau
24b13 73 65 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65  se of.    ** the
24b14 20 77 61 79 20 74 68 65 20 22 69 64 78 6c 69 73   way the "idxlis
24b15 74 22 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 20  t" non-terminal 
24b16 69 73 20 63 6f 6e 73 74 72 75 63 74 65 64 20 62  is constructed b
24b17 79 20 74 68 65 20 70 61 72 73 65 72 2c 0a 20 20  y the parser,.  
24b18 20 20 2a 2a 20 69 66 20 70 4c 69 73 74 49 74 65    ** if pListIte
24b19 6d 2d 3e 70 45 78 70 72 20 69 73 20 6e 6f 74 20  m->pExpr is not 
24b1a 6e 75 6c 6c 20 74 68 65 6e 20 65 69 74 68 65 72  null then either
24b1b 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70   pListItem->pExp
24b1c 72 2d 3e 70 43 6f 6c 6c 0a 20 20 20 20 2a 2a 20  r->pColl.    ** 
24b1d 6d 75 73 74 20 65 78 69 73 74 20 6f 72 20 65 6c  must exist or el
24b1e 73 65 20 74 68 65 72 65 20 6d 75 73 74 20 68 61  se there must ha
24b1f 76 65 20 62 65 65 6e 20 61 6e 20 4f 4f 4d 20 65  ve been an OOM e
24b20 72 72 6f 72 2e 20 20 42 75 74 20 69 66 20 74 68  rror.  But if th
24b21 65 72 65 0a 20 20 20 20 2a 2a 20 77 61 73 20 61  ere.    ** was a
24b22 6e 20 4f 4f 4d 20 65 72 72 6f 72 2c 20 77 65 20  n OOM error, we 
24b23 77 6f 75 6c 64 20 6e 65 76 65 72 20 72 65 61 63  would never reac
24b24 68 20 74 68 69 73 20 70 6f 69 6e 74 2e 20 2a 2f  h this point. */
24b25 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 49 74  .    if( pListIt
24b26 65 6d 2d 3e 70 45 78 70 72 20 26 26 20 41 4c 57  em->pExpr && ALW
24b27 41 59 53 28 70 4c 69 73 74 49 74 65 6d 2d 3e 70  AYS(pListItem->p
24b28 45 78 70 72 2d 3e 70 43 6f 6c 6c 29 20 29 7b 0a  Expr->pColl) ){.
24b29 20 20 20 20 20 20 69 6e 74 20 6e 43 6f 6c 6c 3b        int nColl;
24b2a 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70  .      zColl = p
24b2b 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 2d  ListItem->pExpr-
24b2c 3e 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20  >pColl->zName;. 
24b2d 20 20 20 20 20 6e 43 6f 6c 6c 20 3d 20 73 71 6c       nColl = sql
24b2e 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 43 6f  ite3Strlen30(zCo
24b2f 6c 6c 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 61  ll) + 1;.      a
24b30 73 73 65 72 74 28 20 6e 45 78 74 72 61 3e 3d 6e  ssert( nExtra>=n
24b31 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 20 20 6d 65  Coll );.      me
24b32 6d 63 70 79 28 7a 45 78 74 72 61 2c 20 7a 43 6f  mcpy(zExtra, zCo
24b33 6c 6c 2c 20 6e 43 6f 6c 6c 29 3b 0a 20 20 20 20  ll, nColl);.    
24b34 20 20 7a 43 6f 6c 6c 20 3d 20 7a 45 78 74 72 61    zColl = zExtra
24b35 3b 0a 20 20 20 20 20 20 7a 45 78 74 72 61 20 2b  ;.      zExtra +
24b36 3d 20 6e 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 6e  = nColl;.      n
24b37 45 78 74 72 61 20 2d 3d 20 6e 43 6f 6c 6c 3b 0a  Extra -= nColl;.
24b38 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
24b39 20 7a 43 6f 6c 6c 20 3d 20 70 54 61 62 2d 3e 61   zColl = pTab->a
24b3a 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20  Col[j].zColl;.  
24b3b 20 20 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29      if( !zColl )
24b3c 7b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 6c 20  {.        zColl 
24b3d 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 2d  = db->pDfltColl-
24b3e 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 7d 0a  >zName;.      }.
24b3f 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 64      }.    if( !d
24b40 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20  b->init.busy && 
24b41 21 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f  !sqlite3LocateCo
24b42 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43  llSeq(pParse, zC
24b43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  oll) ){.      go
24b44 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
24b45 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ndex;.    }.    
24b46 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 69  pIndex->azColl[i
24b47 5d 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20 20 20 72  ] = zColl;.    r
24b48 65 71 75 65 73 74 65 64 53 6f 72 74 4f 72 64 65  equestedSortOrde
24b49 72 20 3d 20 70 4c 69 73 74 49 74 65 6d 2d 3e 73  r = pListItem->s
24b4a 6f 72 74 4f 72 64 65 72 20 26 20 73 6f 72 74 4f  ortOrder & sortO
24b4b 72 64 65 72 4d 61 73 6b 3b 0a 20 20 20 20 70 49  rderMask;.    pI
24b4c 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72  ndex->aSortOrder
24b4d 5b 69 5d 20 3d 20 28 75 38 29 72 65 71 75 65 73  [i] = (u8)reques
24b4e 74 65 64 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20  tedSortOrder;.  
24b4f 7d 0a 20 20 73 71 6c 69 74 65 33 44 65 66 61 75  }.  sqlite3Defau
24b50 6c 74 52 6f 77 45 73 74 28 70 49 6e 64 65 78 29  ltRowEst(pIndex)
24b51 3b 0a 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 70  ;..  if( pTab==p
24b52 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65  Parse->pNewTable
24b53 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20   ){.    /* This 
24b54 72 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e  routine has been
24b55 20 63 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 74   called to creat
24b56 65 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 69  e an automatic i
24b57 6e 64 65 78 20 61 73 20 61 0a 20 20 20 20 2a 2a  ndex as a.    **
24b58 20 72 65 73 75 6c 74 20 6f 66 20 61 20 50 52 49   result of a PRI
24b59 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51  MARY KEY or UNIQ
24b5a 55 45 20 63 6c 61 75 73 65 20 6f 6e 20 61 20 63  UE clause on a c
24b5b 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e  olumn definition
24b5c 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 61 20 50 52  , or.    ** a PR
24b5d 49 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49  IMARY KEY or UNI
24b5e 51 55 45 20 63 6c 61 75 73 65 20 66 6f 6c 6c 6f  QUE clause follo
24b5f 77 69 6e 67 20 74 68 65 20 63 6f 6c 75 6d 6e 20  wing the column 
24b60 64 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 20 20 20  definitions..   
24b61 20 2a 2a 20 69 2e 65 2e 20 6f 6e 65 20 6f 66 3a   ** i.e. one of:
24b62 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43  .    **.    ** C
24b63 52 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 20  REATE TABLE t(x 
24b64 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 79 29 3b  PRIMARY KEY, y);
24b65 0a 20 20 20 20 2a 2a 20 43 52 45 41 54 45 20 54  .    ** CREATE T
24b66 41 42 4c 45 20 74 28 78 2c 20 79 2c 20 55 4e 49  ABLE t(x, y, UNI
24b67 51 55 45 28 78 2c 20 79 29 29 3b 0a 20 20 20 20  QUE(x, y));.    
24b68 2a 2a 0a 20 20 20 20 2a 2a 20 45 69 74 68 65 72  **.    ** Either
24b69 20 77 61 79 2c 20 63 68 65 63 6b 20 74 6f 20 73   way, check to s
24b6a 65 65 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  ee if the table 
24b6b 61 6c 72 65 61 64 79 20 68 61 73 20 73 75 63 68  already has such
24b6c 20 61 6e 20 69 6e 64 65 78 2e 20 49 66 0a 20 20   an index. If.  
24b6d 20 20 2a 2a 20 73 6f 2c 20 64 6f 6e 27 74 20 62    ** so, don't b
24b6e 6f 74 68 65 72 20 63 72 65 61 74 69 6e 67 20 74  other creating t
24b6f 68 69 73 20 6f 6e 65 2e 20 54 68 69 73 20 6f 6e  his one. This on
24b70 6c 79 20 61 70 70 6c 69 65 73 20 74 6f 0a 20 20  ly applies to.  
24b71 20 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c    ** automatical
24b72 6c 79 20 63 72 65 61 74 65 64 20 69 6e 64 69 63  ly created indic
24b73 65 73 2e 20 55 73 65 72 73 20 63 61 6e 20 64 6f  es. Users can do
24b74 20 61 73 20 74 68 65 79 20 77 69 73 68 20 77 69   as they wish wi
24b75 74 68 0a 20 20 20 20 2a 2a 20 65 78 70 6c 69 63  th.    ** explic
24b76 69 74 20 69 6e 64 69 63 65 73 2e 0a 20 20 20 20  it indices..    
24b77 2a 2a 0a 20 20 20 20 2a 2a 20 54 77 6f 20 55 4e  **.    ** Two UN
24b78 49 51 55 45 20 6f 72 20 50 52 49 4d 41 52 59 20  IQUE or PRIMARY 
24b79 4b 45 59 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  KEY constraints 
24b7a 61 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 65  are considered e
24b7b 71 75 69 76 61 6c 65 6e 74 0a 20 20 20 20 2a 2a  quivalent.    **
24b7c 20 28 61 6e 64 20 74 68 75 73 20 73 75 70 70 72   (and thus suppr
24b7d 65 73 73 69 6e 67 20 74 68 65 20 73 65 63 6f 6e  essing the secon
24b7e 64 20 6f 6e 65 29 20 65 76 65 6e 20 69 66 20 74  d one) even if t
24b7f 68 65 79 20 68 61 76 65 20 64 69 66 66 65 72 65  hey have differe
24b80 6e 74 0a 20 20 20 20 2a 2a 20 73 6f 72 74 20 6f  nt.    ** sort o
24b81 72 64 65 72 73 2e 0a 20 20 20 20 2a 2a 0a 20 20  rders..    **.  
24b82 20 20 2a 2a 20 49 66 20 74 68 65 72 65 20 61 72    ** If there ar
24b83 65 20 64 69 66 66 65 72 65 6e 74 20 63 6f 6c 6c  e different coll
24b84 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20  ating sequences 
24b85 6f 72 20 69 66 20 74 68 65 20 63 6f 6c 75 6d 6e  or if the column
24b86 73 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68 65 20  s of.    ** the 
24b87 63 6f 6e 73 74 72 61 69 6e 74 20 6f 63 63 75 72  constraint occur
24b88 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 6f 72   in different or
24b89 64 65 72 73 2c 20 74 68 65 6e 20 74 68 65 20 63  ders, then the c
24b8a 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65 0a 20  onstraints are. 
24b8b 20 20 20 2a 2a 20 63 6f 6e 73 69 64 65 72 65 64     ** considered
24b8c 20 64 69 73 74 69 6e 63 74 20 61 6e 64 20 62 6f   distinct and bo
24b8d 74 68 20 72 65 73 75 6c 74 20 69 6e 20 73 65 70  th result in sep
24b8e 61 72 61 74 65 20 69 6e 64 69 63 65 73 2e 0a 20  arate indices.. 
24b8f 20 20 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20     */.    Index 
24b90 2a 70 49 64 78 3b 0a 20 20 20 20 66 6f 72 28 70  *pIdx;.    for(p
24b91 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
24b92 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
24b93 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20  x->pNext){.     
24b94 20 69 6e 74 20 6b 3b 0a 20 20 20 20 20 20 61 73   int k;.      as
24b95 73 65 72 74 28 20 70 49 64 78 2d 3e 6f 6e 45 72  sert( pIdx->onEr
24b96 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 3b 0a  ror!=OE_None );.
24b97 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
24b98 64 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 29 3b  dx->autoIndex );
24b99 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
24b9a 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d  Index->onError!=
24b9b 4f 45 5f 4e 6f 6e 65 20 29 3b 0a 0a 20 20 20 20  OE_None );..    
24b9c 20 20 69 66 28 20 70 49 64 78 2d 3e 6e 43 6f 6c    if( pIdx->nCol
24b9d 75 6d 6e 21 3d 70 49 6e 64 65 78 2d 3e 6e 43 6f  umn!=pIndex->nCo
24b9e 6c 75 6d 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b  lumn ) continue;
24b9f 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20  .      for(k=0; 
24ba0 6b 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  k<pIdx->nColumn;
24ba1 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63   k++){.        c
24ba2 6f 6e 73 74 20 63 68 61 72 20 2a 7a 31 3b 0a 20  onst char *z1;. 
24ba3 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
24ba4 72 20 2a 7a 32 3b 0a 20 20 20 20 20 20 20 20 69  r *z2;.        i
24ba5 66 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d  f( pIdx->aiColum
24ba6 6e 5b 6b 5d 21 3d 70 49 6e 64 65 78 2d 3e 61 69  n[k]!=pIndex->ai
24ba7 43 6f 6c 75 6d 6e 5b 6b 5d 20 29 20 62 72 65 61  Column[k] ) brea
24ba8 6b 3b 0a 20 20 20 20 20 20 20 20 7a 31 20 3d 20  k;.        z1 = 
24ba9 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b  pIdx->azColl[k];
24baa 0a 20 20 20 20 20 20 20 20 7a 32 20 3d 20 70 49  .        z2 = pI
24bab 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b  ndex->azColl[k];
24bac 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 31 21  .        if( z1!
24bad 3d 7a 32 20 26 26 20 73 71 6c 69 74 65 33 53 74  =z2 && sqlite3St
24bae 72 49 43 6d 70 28 7a 31 2c 20 7a 32 29 20 29 20  rICmp(z1, z2) ) 
24baf 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
24bb0 20 20 20 20 20 69 66 28 20 6b 3d 3d 70 49 64 78       if( k==pIdx
24bb1 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->nColumn ){.   
24bb2 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 6f       if( pIdx->o
24bb3 6e 45 72 72 6f 72 21 3d 70 49 6e 64 65 78 2d 3e  nError!=pIndex->
24bb4 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20  onError ){.     
24bb5 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 6f 6e       /* This con
24bb6 73 74 72 61 69 6e 74 20 63 72 65 61 74 65 73 20  straint creates 
24bb7 74 68 65 20 73 61 6d 65 20 69 6e 64 65 78 20 61  the same index a
24bb8 73 20 61 20 70 72 65 76 69 6f 75 73 0a 20 20 20  s a previous.   
24bb9 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72         ** constr
24bba 61 69 6e 74 20 73 70 65 63 69 66 69 65 64 20 73  aint specified s
24bbb 6f 6d 65 77 68 65 72 65 20 69 6e 20 74 68 65 20  omewhere in the 
24bbc 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
24bbd 74 65 6d 65 6e 74 2e 0a 20 20 20 20 20 20 20 20  tement..        
24bbe 20 20 2a 2a 20 48 6f 77 65 76 65 72 20 74 68 65    ** However the
24bbf 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61   ON CONFLICT cla
24bc0 75 73 65 73 20 61 72 65 20 64 69 66 66 65 72 65  uses are differe
24bc1 6e 74 2e 20 49 66 20 62 6f 74 68 20 74 68 69 73  nt. If both this
24bc2 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63   .          ** c
24bc3 6f 6e 73 74 72 61 69 6e 74 20 61 6e 64 20 74 68  onstraint and th
24bc4 65 20 70 72 65 76 69 6f 75 73 20 65 71 75 69 76  e previous equiv
24bc5 61 6c 65 6e 74 20 63 6f 6e 73 74 72 61 69 6e 74  alent constraint
24bc6 20 68 61 76 65 20 65 78 70 6c 69 63 69 74 0a 20   have explicit. 
24bc7 20 20 20 20 20 20 20 20 20 2a 2a 20 4f 4e 20 43           ** ON C
24bc8 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 73 20  ONFLICT clauses 
24bc9 74 68 69 73 20 69 73 20 61 6e 20 65 72 72 6f 72  this is an error
24bca 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 75 73 65  . Otherwise, use
24bcb 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a   the.          *
24bcc 2a 20 65 78 70 6c 69 63 69 74 6c 79 20 73 70 65  * explicitly spe
24bcd 63 69 66 69 65 64 20 62 65 68 61 76 69 6f 75 72  cified behaviour
24bce 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78 2e 0a   for the index..
24bcf 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
24bd0 20 20 20 20 20 20 20 69 66 28 20 21 28 70 49 64         if( !(pId
24bd1 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44  x->onError==OE_D
24bd2 65 66 61 75 6c 74 20 7c 7c 20 70 49 6e 64 65 78  efault || pIndex
24bd3 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65  ->onError==OE_De
24bd4 66 61 75 6c 74 29 20 29 7b 0a 20 20 20 20 20 20  fault) ){.      
24bd5 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
24bd6 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20  orMsg(pParse, . 
24bd7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
24bd8 63 6f 6e 66 6c 69 63 74 69 6e 67 20 4f 4e 20 43  conflicting ON C
24bd9 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 73 20  ONFLICT clauses 
24bda 73 70 65 63 69 66 69 65 64 22 2c 20 30 29 3b 0a  specified", 0);.
24bdb 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
24bdc 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e        if( pIdx->
24bdd 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61  onError==OE_Defa
24bde 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ult ){.         
24bdf 20 20 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72     pIdx->onError
24be0 20 3d 20 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72   = pIndex->onErr
24be1 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  or;.          }.
24be2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
24be3 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
24be4 74 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 20 20  te_index;.      
24be5 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  }.    }.  }..  /
24be6 2a 20 4c 69 6e 6b 20 74 68 65 20 6e 65 77 20 49  * Link the new I
24be7 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 20 74  ndex structure t
24be8 6f 20 69 74 73 20 74 61 62 6c 65 20 61 6e 64 20  o its table and 
24be9 74 6f 20 74 68 65 20 6f 74 68 65 72 0a 20 20 2a  to the other.  *
24bea 2a 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61  * in-memory data
24beb 62 61 73 65 20 73 74 72 75 63 74 75 72 65 73 2e  base structures.
24bec 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d   .  */.  if( db-
24bed 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20  >init.busy ){.  
24bee 20 20 49 6e 64 65 78 20 2a 70 3b 0a 20 20 20 20    Index *p;.    
24bef 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49  p = sqlite3HashI
24bf0 6e 73 65 72 74 28 26 70 49 6e 64 65 78 2d 3e 70  nsert(&pIndex->p
24bf1 53 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 2c  Schema->idxHash,
24bf2 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
24bf3 20 20 20 20 20 20 20 20 20 20 20 20 70 49 6e 64              pInd
24bf4 65 78 2d 3e 7a 4e 61 6d 65 2c 20 73 71 6c 69 74  ex->zName, sqlit
24bf5 65 33 53 74 72 6c 65 6e 33 30 28 70 49 6e 64 65  e3Strlen30(pInde
24bf6 78 2d 3e 7a 4e 61 6d 65 29 2c 0a 20 20 20 20 20  x->zName),.     
24bf7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24bf8 20 20 20 20 20 70 49 6e 64 65 78 29 3b 0a 20 20       pIndex);.  
24bf9 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20    if( p ){.     
24bfa 20 61 73 73 65 72 74 28 20 70 3d 3d 70 49 6e 64   assert( p==pInd
24bfb 65 78 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63  ex );  /* Malloc
24bfc 20 6d 75 73 74 20 68 61 76 65 20 66 61 69 6c 65   must have faile
24bfd 64 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d 3e 6d  d */.      db->m
24bfe 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
24bff 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
24c00 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
24c01 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 66 6c 61     }.    db->fla
24c02 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74  gs |= SQLITE_Int
24c03 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20 20 20 20  ernChanges;.    
24c04 69 66 28 20 70 54 62 6c 4e 61 6d 65 21 3d 30 20  if( pTblName!=0 
24c05 29 7b 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d  ){.      pIndex-
24c06 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69 6e 69 74  >tnum = db->init
24c07 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 20 20 7d 0a  .newTnum;.    }.
24c08 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
24c09 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69   db->init.busy i
24c0a 73 20 30 20 74 68 65 6e 20 63 72 65 61 74 65 20  s 0 then create 
24c0b 74 68 65 20 69 6e 64 65 78 20 6f 6e 20 64 69 73  the index on dis
24c0c 6b 2e 20 20 54 68 69 73 0a 20 20 2a 2a 20 69 6e  k.  This.  ** in
24c0d 76 6f 6c 76 65 73 20 77 72 69 74 69 6e 67 20 74  volves writing t
24c0e 68 65 20 69 6e 64 65 78 20 69 6e 74 6f 20 74 68  he index into th
24c0f 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20 61  e master table a
24c10 6e 64 20 66 69 6c 6c 69 6e 67 20 69 6e 20 74 68  nd filling in th
24c11 65 0a 20 20 2a 2a 20 69 6e 64 65 78 20 77 69 74  e.  ** index wit
24c12 68 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61  h the current ta
24c13 62 6c 65 20 63 6f 6e 74 65 6e 74 73 2e 0a 20 20  ble contents..  
24c14 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 64 62 2d 3e  **.  ** The db->
24c15 69 6e 69 74 2e 62 75 73 79 20 69 73 20 30 20 77  init.busy is 0 w
24c16 68 65 6e 20 74 68 65 20 75 73 65 72 20 66 69 72  hen the user fir
24c17 73 74 20 65 6e 74 65 72 73 20 61 20 43 52 45 41  st enters a CREA
24c18 54 45 20 49 4e 44 45 58 20 0a 20 20 2a 2a 20 63  TE INDEX .  ** c
24c19 6f 6d 6d 61 6e 64 2e 20 20 64 62 2d 3e 69 6e 69  ommand.  db->ini
24c1a 74 2e 62 75 73 79 20 69 73 20 31 20 77 68 65 6e  t.busy is 1 when
24c1b 20 61 20 64 61 74 61 62 61 73 65 20 69 73 20 6f   a database is o
24c1c 70 65 6e 65 64 20 61 6e 64 20 0a 20 20 2a 2a 20  pened and .  ** 
24c1d 43 52 45 41 54 45 20 49 4e 44 45 58 20 73 74 61  CREATE INDEX sta
24c1e 74 65 6d 65 6e 74 73 20 61 72 65 20 72 65 61 64  tements are read
24c1f 20 6f 75 74 20 6f 66 20 74 68 65 20 6d 61 73 74   out of the mast
24c20 65 72 20 74 61 62 6c 65 2e 20 20 49 6e 0a 20 20  er table.  In.  
24c21 2a 2a 20 74 68 65 20 6c 61 74 74 65 72 20 63 61  ** the latter ca
24c22 73 65 20 74 68 65 20 69 6e 64 65 78 20 61 6c 72  se the index alr
24c23 65 61 64 79 20 65 78 69 73 74 73 20 6f 6e 20 64  eady exists on d
24c24 69 73 6b 2c 20 77 68 69 63 68 20 69 73 20 77 68  isk, which is wh
24c25 79 0a 20 20 2a 2a 20 77 65 20 64 6f 6e 27 74 20  y.  ** we don't 
24c26 77 61 6e 74 20 74 6f 20 72 65 63 72 65 61 74 65  want to recreate
24c27 20 69 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49   it..  **.  ** I
24c28 66 20 70 54 62 6c 4e 61 6d 65 3d 3d 30 20 69 74  f pTblName==0 it
24c29 20 6d 65 61 6e 73 20 74 68 69 73 20 69 6e 64 65   means this inde
24c2a 78 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 61  x is generated a
24c2b 73 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 0a  s a primary key.
24c2c 20 20 2a 2a 20 6f 72 20 55 4e 49 51 55 45 20 63    ** or UNIQUE c
24c2d 6f 6e 73 74 72 61 69 6e 74 20 6f 66 20 61 20 43  onstraint of a C
24c2e 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74  REATE TABLE stat
24c2f 65 6d 65 6e 74 2e 20 20 53 69 6e 63 65 20 74 68  ement.  Since th
24c30 65 20 74 61 62 6c 65 0a 20 20 2a 2a 20 68 61 73  e table.  ** has
24c31 20 6a 75 73 74 20 62 65 65 6e 20 63 72 65 61 74   just been creat
24c32 65 64 2c 20 69 74 20 63 6f 6e 74 61 69 6e 73 20  ed, it contains 
24c33 6e 6f 20 64 61 74 61 20 61 6e 64 20 74 68 65 20  no data and the 
24c34 69 6e 64 65 78 20 69 6e 69 74 69 61 6c 69 7a 61  index initializa
24c35 74 69 6f 6e 0a 20 20 2a 2a 20 73 74 65 70 20 63  tion.  ** step c
24c36 61 6e 20 62 65 20 73 6b 69 70 70 65 64 2e 0a 20  an be skipped.. 
24c37 20 2a 2f 0a 20 20 65 6c 73 65 7b 20 2f 2a 20 69   */.  else{ /* i
24c38 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  f( db->init.busy
24c39 3d 3d 30 20 29 20 2a 2f 0a 20 20 20 20 56 64 62  ==0 ) */.    Vdb
24c3a 65 20 2a 76 3b 0a 20 20 20 20 63 68 61 72 20 2a  e *v;.    char *
24c3b 7a 53 74 6d 74 3b 0a 20 20 20 20 69 6e 74 20 69  zStmt;.    int i
24c3c 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  Mem = ++pParse->
24c3d 6e 4d 65 6d 3b 0a 0a 20 20 20 20 76 20 3d 20 73  nMem;..    v = s
24c3e 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
24c3f 61 72 73 65 29 3b 0a 20 20 20 20 69 66 28 20 76  arse);.    if( v
24c40 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 74 5f  ==0 ) goto exit_
24c41 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 0a 0a  create_index;...
24c42 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68      /* Create th
24c43 65 20 72 6f 6f 74 70 61 67 65 20 66 6f 72 20 74  e rootpage for t
24c44 68 65 20 69 6e 64 65 78 0a 20 20 20 20 2a 2f 0a  he index.    */.
24c45 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e      sqlite3Begin
24c46 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70  WriteOperation(p
24c47 50 61 72 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a  Parse, 1, iDb);.
24c48 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
24c49 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 72 65 61  ddOp2(v, OP_Crea
24c4a 74 65 49 6e 64 65 78 2c 20 69 44 62 2c 20 69 4d  teIndex, iDb, iM
24c4b 65 6d 29 3b 0a 0a 20 20 20 20 2f 2a 20 47 61 74  em);..    /* Gat
24c4c 68 65 72 20 74 68 65 20 63 6f 6d 70 6c 65 74 65  her the complete
24c4d 20 74 65 78 74 20 6f 66 20 74 68 65 20 43 52 45   text of the CRE
24c4e 41 54 45 20 49 4e 44 45 58 20 73 74 61 74 65 6d  ATE INDEX statem
24c4f 65 6e 74 20 69 6e 74 6f 0a 20 20 20 20 2a 2a 20  ent into.    ** 
24c50 74 68 65 20 7a 53 74 6d 74 20 76 61 72 69 61 62  the zStmt variab
24c51 6c 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  le.    */.    if
24c52 28 20 70 53 74 61 72 74 20 29 7b 0a 20 20 20 20  ( pStart ){.    
24c53 20 20 61 73 73 65 72 74 28 20 70 45 6e 64 21 3d    assert( pEnd!=
24c54 30 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 41 20  0 );.      /* A 
24c55 6e 61 6d 65 64 20 69 6e 64 65 78 20 77 69 74 68  named index with
24c56 20 61 6e 20 65 78 70 6c 69 63 69 74 20 43 52 45   an explicit CRE
24c57 41 54 45 20 49 4e 44 45 58 20 73 74 61 74 65 6d  ATE INDEX statem
24c58 65 6e 74 20 2a 2f 0a 20 20 20 20 20 20 7a 53 74  ent */.      zSt
24c59 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  mt = sqlite3MPri
24c5a 6e 74 66 28 64 62 2c 20 22 43 52 45 41 54 45 25  ntf(db, "CREATE%
24c5b 73 20 49 4e 44 45 58 20 25 2e 2a 73 22 2c 0a 20  s INDEX %.*s",. 
24c5c 20 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 3d 3d         onError==
24c5d 4f 45 5f 4e 6f 6e 65 20 3f 20 22 22 20 3a 20 22  OE_None ? "" : "
24c5e 20 55 4e 49 51 55 45 22 2c 0a 20 20 20 20 20 20   UNIQUE",.      
24c5f 20 20 70 45 6e 64 2d 3e 7a 20 2d 20 70 4e 61 6d    pEnd->z - pNam
24c60 65 2d 3e 7a 20 2b 20 31 2c 0a 20 20 20 20 20 20  e->z + 1,.      
24c61 20 20 70 4e 61 6d 65 2d 3e 7a 29 3b 0a 20 20 20    pName->z);.   
24c62 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a   }else{.      /*
24c63 20 41 6e 20 61 75 74 6f 6d 61 74 69 63 20 69 6e   An automatic in
24c64 64 65 78 20 63 72 65 61 74 65 64 20 62 79 20 61  dex created by a
24c65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72 20   PRIMARY KEY or 
24c66 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e  UNIQUE constrain
24c67 74 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 7a 53  t */.      /* zS
24c68 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  tmt = sqlite3MPr
24c69 69 6e 74 66 28 22 22 29 3b 20 2a 2f 0a 20 20 20  intf(""); */.   
24c6a 20 20 20 7a 53 74 6d 74 20 3d 20 30 3b 0a 20 20     zStmt = 0;.  
24c6b 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64 64 20    }..    /* Add 
24c6c 61 6e 20 65 6e 74 72 79 20 69 6e 20 73 71 6c 69  an entry in sqli
24c6d 74 65 5f 6d 61 73 74 65 72 20 66 6f 72 20 74 68  te_master for th
24c6e 69 73 20 69 6e 64 65 78 0a 20 20 20 20 2a 2f 0a  is index.    */.
24c6f 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65      sqlite3Neste
24c70 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a  dParse(pParse, .
24c71 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20          "INSERT 
24c72 49 4e 54 4f 20 25 51 2e 25 73 20 56 41 4c 55 45  INTO %Q.%s VALUE
24c73 53 28 27 69 6e 64 65 78 27 2c 25 51 2c 25 51 2c  S('index',%Q,%Q,
24c74 23 25 64 2c 25 51 29 3b 22 2c 0a 20 20 20 20 20  #%d,%Q);",.     
24c75 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e     db->aDb[iDb].
24c76 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41  zName, SCHEMA_TA
24c77 42 4c 45 28 69 44 62 29 2c 0a 20 20 20 20 20 20  BLE(iDb),.      
24c78 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c    pIndex->zName,
24c79 0a 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 7a  .        pTab->z
24c7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 69 4d  Name,.        iM
24c7b 65 6d 2c 0a 20 20 20 20 20 20 20 20 7a 53 74 6d  em,.        zStm
24c7c 74 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 71 6c  t.    );.    sql
24c7d 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
24c7e 53 74 6d 74 29 3b 0a 0a 20 20 20 20 2f 2a 20 46  Stmt);..    /* F
24c7f 69 6c 6c 20 74 68 65 20 69 6e 64 65 78 20 77 69  ill the index wi
24c80 74 68 20 64 61 74 61 20 61 6e 64 20 72 65 70 61  th data and repa
24c81 72 73 65 20 74 68 65 20 73 63 68 65 6d 61 2e 20  rse the schema. 
24c82 43 6f 64 65 20 61 6e 20 4f 50 5f 45 78 70 69 72  Code an OP_Expir
24c83 65 0a 20 20 20 20 2a 2a 20 74 6f 20 69 6e 76 61  e.    ** to inva
24c84 6c 69 64 61 74 65 20 61 6c 6c 20 70 72 65 2d 63  lidate all pre-c
24c85 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e  ompiled statemen
24c86 74 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ts..    */.    i
24c87 66 28 20 70 54 62 6c 4e 61 6d 65 20 29 7b 0a 20  f( pTblName ){. 
24c88 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 66 69       sqlite3Refi
24c89 6c 6c 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  llIndex(pParse, 
24c8a 70 49 6e 64 65 78 2c 20 69 4d 65 6d 29 3b 0a 20  pIndex, iMem);. 
24c8b 20 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e       sqlite3Chan
24c8c 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c  geCookie(pParse,
24c8d 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c   iDb);.      sql
24c8e 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
24c8f 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61  , OP_ParseSchema
24c90 2c 20 69 44 62 2c 20 30 2c 20 30 2c 0a 20 20 20  , iDb, 0, 0,.   
24c91 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 50 72        sqlite3MPr
24c92 69 6e 74 66 28 64 62 2c 20 22 6e 61 6d 65 3d 27  intf(db, "name='
24c93 25 71 27 22 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e  %q'", pIndex->zN
24c94 61 6d 65 29 2c 20 50 34 5f 44 59 4e 41 4d 49 43  ame), P4_DYNAMIC
24c95 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
24c96 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
24c97 5f 45 78 70 69 72 65 2c 20 30 29 3b 0a 20 20 20  _Expire, 0);.   
24c98 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 68 65   }.  }..  /* Whe
24c99 6e 20 61 64 64 69 6e 67 20 61 6e 20 69 6e 64 65  n adding an inde
24c9a 78 20 74 6f 20 74 68 65 20 6c 69 73 74 20 6f 66  x to the list of
24c9b 20 69 6e 64 69 63 65 73 20 66 6f 72 20 61 20 74   indices for a t
24c9c 61 62 6c 65 2c 20 6d 61 6b 65 0a 20 20 2a 2a 20  able, make.  ** 
24c9d 73 75 72 65 20 61 6c 6c 20 69 6e 64 69 63 65 73  sure all indices
24c9e 20 6c 61 62 65 6c 65 64 20 4f 45 5f 52 65 70 6c   labeled OE_Repl
24c9f 61 63 65 20 63 6f 6d 65 20 61 66 74 65 72 20 61  ace come after a
24ca0 6c 6c 20 74 68 6f 73 65 20 6c 61 62 65 6c 65 64  ll those labeled
24ca1 0a 20 20 2a 2a 20 4f 45 5f 49 67 6e 6f 72 65 2e  .  ** OE_Ignore.
24ca2 20 20 54 68 69 73 20 69 73 20 6e 65 63 65 73 73    This is necess
24ca3 61 72 79 20 66 6f 72 20 74 68 65 20 63 6f 72 72  ary for the corr
24ca4 65 63 74 20 63 6f 6e 73 74 72 61 69 6e 74 20 63  ect constraint c
24ca5 68 65 63 6b 0a 20 20 2a 2a 20 70 72 6f 63 65 73  heck.  ** proces
24ca6 73 69 6e 67 20 28 69 6e 20 73 71 6c 69 74 65 33  sing (in sqlite3
24ca7 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69  GenerateConstrai
24ca8 6e 74 43 68 65 63 6b 73 28 29 29 20 61 73 20 70  ntChecks()) as p
24ca9 61 72 74 20 6f 66 0a 20 20 2a 2a 20 55 50 44 41  art of.  ** UPDA
24caa 54 45 20 61 6e 64 20 49 4e 53 45 52 54 20 73 74  TE and INSERT st
24cab 61 74 65 6d 65 6e 74 73 2e 20 20 0a 20 20 2a 2f  atements.  .  */
24cac 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e  .  if( db->init.
24cad 62 75 73 79 20 7c 7c 20 70 54 62 6c 4e 61 6d 65  busy || pTblName
24cae 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6f  ==0 ){.    if( o
24caf 6e 45 72 72 6f 72 21 3d 4f 45 5f 52 65 70 6c 61  nError!=OE_Repla
24cb0 63 65 20 7c 7c 20 70 54 61 62 2d 3e 70 49 6e 64  ce || pTab->pInd
24cb1 65 78 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 7c  ex==0.         |
24cb2 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65 78 2d 3e  | pTab->pIndex->
24cb3 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c  onError==OE_Repl
24cb4 61 63 65 29 7b 0a 20 20 20 20 20 20 70 49 6e 64  ace){.      pInd
24cb5 65 78 2d 3e 70 4e 65 78 74 20 3d 20 70 54 61 62  ex->pNext = pTab
24cb6 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 20  ->pIndex;.      
24cb7 70 54 61 62 2d 3e 70 49 6e 64 65 78 20 3d 20 70  pTab->pIndex = p
24cb8 49 6e 64 65 78 3b 0a 20 20 20 20 7d 65 6c 73 65  Index;.    }else
24cb9 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70  {.      Index *p
24cba 4f 74 68 65 72 20 3d 20 70 54 61 62 2d 3e 70 49  Other = pTab->pI
24cbb 6e 64 65 78 3b 0a 20 20 20 20 20 20 77 68 69 6c  ndex;.      whil
24cbc 65 28 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74  e( pOther->pNext
24cbd 20 26 26 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78   && pOther->pNex
24cbe 74 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 52  t->onError!=OE_R
24cbf 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20 20 20  eplace ){.      
24cc0 20 20 70 4f 74 68 65 72 20 3d 20 70 4f 74 68 65    pOther = pOthe
24cc1 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  r->pNext;.      
24cc2 7d 0a 20 20 20 20 20 20 70 49 6e 64 65 78 2d 3e  }.      pIndex->
24cc3 70 4e 65 78 74 20 3d 20 70 4f 74 68 65 72 2d 3e  pNext = pOther->
24cc4 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 70 4f 74  pNext;.      pOt
24cc5 68 65 72 2d 3e 70 4e 65 78 74 20 3d 20 70 49 6e  her->pNext = pIn
24cc6 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  dex;.    }.    p
24cc7 52 65 74 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20  Ret = pIndex;.  
24cc8 20 20 70 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20    pIndex = 0;.  
24cc9 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 75 70  }..  /* Clean up
24cca 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20   before exiting 
24ccb 2a 2f 0a 65 78 69 74 5f 63 72 65 61 74 65 5f 69  */.exit_create_i
24ccc 6e 64 65 78 3a 0a 20 20 69 66 28 20 70 49 6e 64  ndex:.  if( pInd
24ccd 65 78 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  ex ){.    sqlite
24cce 33 5f 66 72 65 65 28 70 49 6e 64 65 78 2d 3e 7a  3_free(pIndex->z
24ccf 43 6f 6c 41 66 66 29 3b 0a 20 20 20 20 73 71 6c  ColAff);.    sql
24cd0 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
24cd1 49 6e 64 65 78 29 3b 0a 20 20 7d 0a 20 20 73 71  Index);.  }.  sq
24cd2 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
24cd3 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a  ete(db, pList);.
24cd4 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
24cd5 44 65 6c 65 74 65 28 64 62 2c 20 70 54 62 6c 4e  Delete(db, pTblN
24cd6 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ame);.  sqlite3D
24cd7 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29  bFree(db, zName)
24cd8 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b  ;.  return pRet;
24cd9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 6c 20 74  .}../*.** Fill t
24cda 68 65 20 49 6e 64 65 78 2e 61 69 52 6f 77 45 73  he Index.aiRowEs
24cdb 74 5b 5d 20 61 72 72 61 79 20 77 69 74 68 20 64  t[] array with d
24cdc 65 66 61 75 6c 74 20 69 6e 66 6f 72 6d 61 74 69  efault informati
24cdd 6f 6e 20 2d 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  on - information
24cde 0a 2a 2a 20 74 6f 20 62 65 20 75 73 65 64 20 77  .** to be used w
24cdf 68 65 6e 20 77 65 20 68 61 76 65 20 6e 6f 74 20  hen we have not 
24ce0 72 75 6e 20 74 68 65 20 41 4e 41 4c 59 5a 45 20  run the ANALYZE 
24ce1 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 61  command..**.** a
24ce2 69 52 6f 77 45 73 74 5b 30 5d 20 69 73 20 73 75  iRowEst[0] is su
24ce3 70 70 6f 73 65 20 74 6f 20 63 6f 6e 74 61 69 6e  ppose to contain
24ce4 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
24ce5 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 69  lements in the i
24ce6 6e 64 65 78 2e 0a 2a 2a 20 53 69 6e 63 65 20 77  ndex..** Since w
24ce7 65 20 64 6f 20 6e 6f 74 20 6b 6e 6f 77 2c 20 67  e do not know, g
24ce8 75 65 73 73 20 31 20 6d 69 6c 6c 69 6f 6e 2e 20  uess 1 million. 
24ce9 20 61 69 52 6f 77 45 73 74 5b 31 5d 20 69 73 20   aiRowEst[1] is 
24cea 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66 20 74  an estimate of t
24ceb 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  he.** number of 
24cec 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c  rows in the tabl
24ced 65 20 74 68 61 74 20 6d 61 74 63 68 20 61 6e 79  e that match any
24cee 20 70 61 72 74 69 63 75 6c 61 72 20 76 61 6c 75   particular valu
24cef 65 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 72 73  e of the.** firs
24cf0 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  t column of the 
24cf1 69 6e 64 65 78 2e 20 20 61 69 52 6f 77 45 73 74  index.  aiRowEst
24cf2 5b 32 5d 20 69 73 20 61 6e 20 65 73 74 69 6d 61  [2] is an estima
24cf3 74 65 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  te of the number
24cf4 0a 2a 2a 20 6f 66 20 72 6f 77 73 20 74 68 61 74  .** of rows that
24cf5 20 6d 61 74 63 68 20 61 6e 79 20 70 61 72 74 69   match any parti
24cf6 63 75 6c 61 72 20 63 6f 6d 62 69 6e 69 61 74 69  cular combiniati
24cf7 6f 6e 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  on of the first 
24cf8 32 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 6f 66 20  2 columns.** of 
24cf9 74 68 65 20 69 6e 64 65 78 2e 20 20 41 6e 64 20  the index.  And 
24cfa 73 6f 20 66 6f 72 74 68 2e 20 20 49 74 20 6d 75  so forth.  It mu
24cfb 73 74 20 61 6c 77 61 79 73 20 62 65 20 74 68 65  st always be the
24cfc 20 63 61 73 65 20 74 68 61 74 0a 2a 0a 2a 2a 20   case that.*.** 
24cfd 20 20 20 20 20 20 20 20 20 20 61 69 52 6f 77 45            aiRowE
24cfe 73 74 5b 4e 5d 3c 3d 61 69 52 6f 77 45 73 74 5b  st[N]<=aiRowEst[
24cff 4e 2d 31 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20  N-1].**         
24d00 20 20 61 69 52 6f 77 45 73 74 5b 4e 5d 3e 3d 31    aiRowEst[N]>=1
24d01 0a 2a 2a 0a 2a 2a 20 41 70 61 72 74 20 66 72 6f  .**.** Apart fro
24d02 6d 20 74 68 61 74 2c 20 77 65 20 68 61 76 65 20  m that, we have 
24d03 6c 69 74 74 6c 65 20 74 6f 20 67 6f 20 6f 6e 20  little to go on 
24d04 62 65 73 69 64 65 73 20 69 6e 74 75 69 74 69 6f  besides intuitio
24d05 6e 20 61 73 20 74 6f 0a 2a 2a 20 68 6f 77 20 61  n as to.** how a
24d06 69 52 6f 77 45 73 74 5b 5d 20 73 68 6f 75 6c 64  iRowEst[] should
24d07 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e   be initialized.
24d08 20 20 54 68 65 20 6e 75 6d 62 65 72 73 20 67 65    The numbers ge
24d09 6e 65 72 61 74 65 64 20 68 65 72 65 0a 2a 2a 20  nerated here.** 
24d0a 61 72 65 20 62 61 73 65 64 20 6f 6e 20 74 79 70  are based on typ
24d0b 69 63 61 6c 20 76 61 6c 75 65 73 20 66 6f 75 6e  ical values foun
24d0c 64 20 69 6e 20 61 63 74 75 61 6c 20 69 6e 64 69  d in actual indi
24d0d 63 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ces..*/.SQLITE_P
24d0e 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
24d0f 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73 74  te3DefaultRowEst
24d10 28 49 6e 64 65 78 20 2a 70 49 64 78 29 7b 0a 20  (Index *pIdx){. 
24d11 20 75 6e 73 69 67 6e 65 64 20 2a 61 20 3d 20 70   unsigned *a = p
24d12 49 64 78 2d 3e 61 69 52 6f 77 45 73 74 3b 0a 20  Idx->aiRowEst;. 
24d13 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74   int i;.  assert
24d14 28 20 61 21 3d 30 20 29 3b 0a 20 20 61 5b 30 5d  ( a!=0 );.  a[0]
24d15 20 3d 20 31 30 30 30 30 30 30 3b 0a 20 20 66 6f   = 1000000;.  fo
24d16 72 28 69 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  r(i=pIdx->nColum
24d17 6e 3b 20 69 3e 3d 35 3b 20 69 2d 2d 29 7b 0a 20  n; i>=5; i--){. 
24d18 20 20 20 61 5b 69 5d 20 3d 20 35 3b 0a 20 20 7d     a[i] = 5;.  }
24d19 0a 20 20 77 68 69 6c 65 28 20 69 3e 3d 31 20 29  .  while( i>=1 )
24d1a 7b 0a 20 20 20 20 61 5b 69 5d 20 3d 20 31 31 20  {.    a[i] = 11 
24d1b 2d 20 69 3b 0a 20 20 20 20 69 2d 2d 3b 0a 20 20  - i;.    i--;.  
24d1c 7d 0a 20 20 69 66 28 20 70 49 64 78 2d 3e 6f 6e  }.  if( pIdx->on
24d1d 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29  Error!=OE_None )
24d1e 7b 0a 20 20 20 20 61 5b 70 49 64 78 2d 3e 6e 43  {.    a[pIdx->nC
24d1f 6f 6c 75 6d 6e 5d 20 3d 20 31 3b 0a 20 20 7d 0a  olumn] = 1;.  }.
24d20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
24d21 75 74 69 6e 65 20 77 69 6c 6c 20 64 72 6f 70 20  utine will drop 
24d22 61 6e 20 65 78 69 73 74 69 6e 67 20 6e 61 6d 65  an existing name
24d23 64 20 69 6e 64 65 78 2e 20 20 54 68 69 73 20 72  d index.  This r
24d24 6f 75 74 69 6e 65 0a 2a 2a 20 69 6d 70 6c 65 6d  outine.** implem
24d25 65 6e 74 73 20 74 68 65 20 44 52 4f 50 20 49 4e  ents the DROP IN
24d26 44 45 58 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  DEX statement..*
24d27 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
24d28 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f   void sqlite3Dro
24d29 70 49 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50  pIndex(Parse *pP
24d2a 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70  arse, SrcList *p
24d2b 4e 61 6d 65 2c 20 69 6e 74 20 69 66 45 78 69 73  Name, int ifExis
24d2c 74 73 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49  ts){.  Index *pI
24d2d 6e 64 65 78 3b 0a 20 20 56 64 62 65 20 2a 76 3b  ndex;.  Vdbe *v;
24d2e 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
24d2f 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
24d30 6e 74 20 69 44 62 3b 0a 0a 20 20 61 73 73 65 72  nt iDb;..  asser
24d31 74 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d  t( pParse->nErr=
24d32 3d 30 20 29 3b 20 20 20 2f 2a 20 4e 65 76 65 72  =0 );   /* Never
24d33 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70 72 69   called with pri
24d34 6f 72 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69  or errors */.  i
24d35 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
24d36 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  led ){.    goto 
24d37 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b  exit_drop_index;
24d38 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
24d39 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b  Name->nSrc==1 );
24d3a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
24d3b 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  !=sqlite3ReadSch
24d3c 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20  ema(pParse) ){. 
24d3d 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f     goto exit_dro
24d3e 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 70  p_index;.  }.  p
24d3f 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 46  Index = sqlite3F
24d40 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 70 4e 61  indIndex(db, pNa
24d41 6d 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20  me->a[0].zName, 
24d42 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74  pName->a[0].zDat
24d43 61 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 49  abase);.  if( pI
24d44 6e 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 69  ndex==0 ){.    i
24d45 66 28 20 21 69 66 45 78 69 73 74 73 20 29 7b 0a  f( !ifExists ){.
24d46 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
24d47 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
24d48 6f 20 73 75 63 68 20 69 6e 64 65 78 3a 20 25 53  o such index: %S
24d49 22 2c 20 70 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  ", pName, 0);.  
24d4a 20 20 7d 0a 20 20 20 20 70 50 61 72 73 65 2d 3e    }.    pParse->
24d4b 63 68 65 63 6b 53 63 68 65 6d 61 20 3d 20 31 3b  checkSchema = 1;
24d4c 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64  .    goto exit_d
24d4d 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20  rop_index;.  }. 
24d4e 20 69 66 28 20 70 49 6e 64 65 78 2d 3e 61 75 74   if( pIndex->aut
24d4f 6f 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 73 71  oIndex ){.    sq
24d50 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
24d51 61 72 73 65 2c 20 22 69 6e 64 65 78 20 61 73 73  arse, "index ass
24d52 6f 63 69 61 74 65 64 20 77 69 74 68 20 55 4e 49  ociated with UNI
24d53 51 55 45 20 22 0a 20 20 20 20 20 20 22 6f 72 20  QUE ".      "or 
24d54 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6e 73  PRIMARY KEY cons
24d55 74 72 61 69 6e 74 20 63 61 6e 6e 6f 74 20 62 65  traint cannot be
24d56 20 64 72 6f 70 70 65 64 22 2c 20 30 29 3b 0a 20   dropped", 0);. 
24d57 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f     goto exit_dro
24d58 70 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 69  p_index;.  }.  i
24d59 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
24d5a 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 49  maToIndex(db, pI
24d5b 6e 64 65 78 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  ndex->pSchema);.
24d5c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
24d5d 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
24d5e 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63 6f  N.  {.    int co
24d5f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50  de = SQLITE_DROP
24d60 5f 49 4e 44 45 58 3b 0a 20 20 20 20 54 61 62 6c  _INDEX;.    Tabl
24d61 65 20 2a 70 54 61 62 20 3d 20 70 49 6e 64 65 78  e *pTab = pIndex
24d62 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 20 20 63 6f  ->pTable;.    co
24d63 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20  nst char *zDb = 
24d64 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
24d65 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  me;.    const ch
24d66 61 72 20 2a 7a 54 61 62 20 3d 20 53 43 48 45 4d  ar *zTab = SCHEM
24d67 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 20 20  A_TABLE(iDb);.  
24d68 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
24d69 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53  hCheck(pParse, S
24d6a 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a 54  QLITE_DELETE, zT
24d6b 61 62 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20  ab, 0, zDb) ){. 
24d6c 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64       goto exit_d
24d6d 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d  rop_index;.    }
24d6e 0a 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54  .    if( !OMIT_T
24d6f 45 4d 50 44 42 20 26 26 20 69 44 62 20 29 20 63  EMPDB && iDb ) c
24d70 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f  ode = SQLITE_DRO
24d71 50 5f 54 45 4d 50 5f 49 4e 44 45 58 3b 0a 20 20  P_TEMP_INDEX;.  
24d72 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74    if( sqlite3Aut
24d73 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63  hCheck(pParse, c
24d74 6f 64 65 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61  ode, pIndex->zNa
24d75 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  me, pTab->zName,
24d76 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67   zDb) ){.      g
24d77 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e  oto exit_drop_in
24d78 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  dex;.    }.  }.#
24d79 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47 65 6e 65  endif..  /* Gene
24d7a 72 61 74 65 20 63 6f 64 65 20 74 6f 20 72 65 6d  rate code to rem
24d7b 6f 76 65 20 74 68 65 20 69 6e 64 65 78 20 61 6e  ove the index an
24d7c 64 20 66 72 6f 6d 20 74 68 65 20 6d 61 73 74 65  d from the maste
24d7d 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76 20 3d  r table */.  v =
24d7e 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
24d7f 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  pParse);.  if( v
24d80 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   ){.    sqlite3B
24d81 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
24d82 6f 6e 28 70 50 61 72 73 65 2c 20 31 2c 20 69 44  on(pParse, 1, iD
24d83 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4e  b);.    sqlite3N
24d84 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
24d85 65 2c 0a 20 20 20 20 20 20 20 22 44 45 4c 45 54  e,.       "DELET
24d86 45 20 46 52 4f 4d 20 25 51 2e 25 73 20 57 48 45  E FROM %Q.%s WHE
24d87 52 45 20 6e 61 6d 65 3d 25 51 22 2c 0a 20 20 20  RE name=%Q",.   
24d88 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d      db->aDb[iDb]
24d89 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54  .zName, SCHEMA_T
24d8a 41 42 4c 45 28 69 44 62 29 2c 0a 20 20 20 20 20  ABLE(iDb),.     
24d8b 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 0a    pIndex->zName.
24d8c 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 73      );.    if( s
24d8d 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
24d8e 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 74 61 74  db, "sqlite_stat
24d8f 31 22 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  1", db->aDb[iDb]
24d90 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20  .zName) ){.     
24d91 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
24d92 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20  rse(pParse,.    
24d93 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d      "DELETE FROM
24d94 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 31   %Q.sqlite_stat1
24d95 20 57 48 45 52 45 20 69 64 78 3d 25 51 22 2c 0a   WHERE idx=%Q",.
24d96 20 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b          db->aDb[
24d97 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 70 49 6e 64  iDb].zName, pInd
24d98 65 78 2d 3e 7a 4e 61 6d 65 0a 20 20 20 20 20 20  ex->zName.      
24d99 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
24d9a 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65  ite3ChangeCookie
24d9b 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20  (pParse, iDb);. 
24d9c 20 20 20 64 65 73 74 72 6f 79 52 6f 6f 74 50 61     destroyRootPa
24d9d 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65  ge(pParse, pInde
24d9e 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20  x->tnum, iDb);. 
24d9f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24da0 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 49  dOp4(v, OP_DropI
24da1 6e 64 65 78 2c 20 69 44 62 2c 20 30 2c 20 30 2c  ndex, iDb, 0, 0,
24da2 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20   pIndex->zName, 
24da3 30 29 3b 0a 20 20 7d 0a 0a 65 78 69 74 5f 64 72  0);.  }..exit_dr
24da4 6f 70 5f 69 6e 64 65 78 3a 0a 20 20 73 71 6c 69  op_index:.  sqli
24da5 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
24da6 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d 0a 0a  (db, pName);.}..
24da7 2f 2a 0a 2a 2a 20 70 41 72 72 61 79 20 69 73 20  /*.** pArray is 
24da8 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
24da9 61 72 72 61 79 20 6f 66 20 6f 62 6a 65 63 74 73  array of objects
24daa 2e 20 20 45 61 63 68 20 6f 62 6a 65 63 74 20 69  .  Each object i
24dab 6e 20 74 68 65 0a 2a 2a 20 61 72 72 61 79 20 69  n the.** array i
24dac 73 20 73 7a 45 6e 74 72 79 20 62 79 74 65 73 20  s szEntry bytes 
24dad 69 6e 20 73 69 7a 65 2e 20 20 54 68 69 73 20 72  in size.  This r
24dae 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73  outine allocates
24daf 20 61 20 6e 65 77 0a 2a 2a 20 6f 62 6a 65 63 74   a new.** object
24db0 20 6f 6e 20 74 68 65 20 65 6e 64 20 6f 66 20 74   on the end of t
24db1 68 65 20 61 72 72 61 79 2e 0a 2a 2a 0a 2a 2a 20  he array..**.** 
24db2 2a 70 6e 45 6e 74 72 79 20 69 73 20 74 68 65 20  *pnEntry is the 
24db3 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  number of entrie
24db4 73 20 61 6c 72 65 61 64 79 20 69 6e 20 75 73 65  s already in use
24db5 2e 20 20 2a 70 6e 41 6c 6c 6f 63 20 69 73 0a 2a  .  *pnAlloc is.*
24db6 2a 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79  * the previously
24db7 20 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20   allocated size 
24db8 6f 66 20 74 68 65 20 61 72 72 61 79 2e 20 20 69  of the array.  i
24db9 6e 69 74 53 69 7a 65 20 69 73 20 74 68 65 0a 2a  nitSize is the.*
24dba 2a 20 73 75 67 67 65 73 74 65 64 20 69 6e 69 74  * suggested init
24dbb 69 61 6c 20 61 72 72 61 79 20 73 69 7a 65 20 61  ial array size a
24dbc 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  llocation..**.**
24dbd 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   The index of th
24dbe 65 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20 72  e new entry is r
24dbf 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 49 64 78  eturned in *pIdx
24dc0 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
24dc1 74 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 70  tine returns a p
24dc2 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 72  ointer to the ar
24dc3 72 61 79 20 6f 66 20 6f 62 6a 65 63 74 73 2e 20  ray of objects. 
24dc4 20 54 68 69 73 0a 2a 2a 20 6d 69 67 68 74 20 62   This.** might b
24dc5 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68  e the same as th
24dc6 65 20 70 41 72 72 61 79 20 70 61 72 61 6d 65 74  e pArray paramet
24dc7 65 72 20 6f 72 20 69 74 20 6d 69 67 68 74 20 62  er or it might b
24dc8 65 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a  e a different.**
24dc9 20 70 6f 69 6e 74 65 72 20 69 66 20 74 68 65 20   pointer if the 
24dca 61 72 72 61 79 20 77 61 73 20 72 65 73 69 7a 65  array was resize
24dcb 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
24dcc 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
24dcd 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74 65 28  e3ArrayAllocate(
24dce 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
24dcf 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69       /* Connecti
24dd0 6f 6e 20 74 6f 20 6e 6f 74 69 66 79 20 6f 66 20  on to notify of 
24dd1 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20  malloc failures 
24dd2 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 72 61  */.  void *pArra
24dd3 79 2c 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20  y,     /* Array 
24dd4 6f 66 20 6f 62 6a 65 63 74 73 2e 20 20 4d 69 67  of objects.  Mig
24dd5 68 74 20 62 65 20 72 65 61 6c 6c 6f 63 61 74 65  ht be reallocate
24dd6 64 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45 6e 74  d */.  int szEnt
24dd7 72 79 2c 20 20 20 20 20 20 2f 2a 20 53 69 7a 65  ry,      /* Size
24dd8 20 6f 66 20 65 61 63 68 20 6f 62 6a 65 63 74 20   of each object 
24dd9 69 6e 20 74 68 65 20 61 72 72 61 79 20 2a 2f 0a  in the array */.
24dda 20 20 69 6e 74 20 69 6e 69 74 53 69 7a 65 2c 20    int initSize, 
24ddb 20 20 20 20 2f 2a 20 53 75 67 67 65 73 74 65 64      /* Suggested
24ddc 20 69 6e 69 74 69 61 6c 20 61 6c 6c 6f 63 61 74   initial allocat
24ddd 69 6f 6e 2c 20 69 6e 20 65 6c 65 6d 65 6e 74 73  ion, in elements
24dde 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 45 6e 74   */.  int *pnEnt
24ddf 72 79 2c 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ry,     /* Numbe
24de0 72 20 6f 66 20 6f 62 6a 65 63 74 73 20 63 75 72  r of objects cur
24de1 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f  rently in use */
24de2 0a 20 20 69 6e 74 20 2a 70 6e 41 6c 6c 6f 63 2c  .  int *pnAlloc,
24de3 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
24de4 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f  size of the allo
24de5 63 61 74 69 6f 6e 2c 20 69 6e 20 65 6c 65 6d 65  cation, in eleme
24de6 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 49  nts */.  int *pI
24de7 64 78 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72  dx         /* Wr
24de8 69 74 65 20 74 68 65 20 69 6e 64 65 78 20 6f 66  ite the index of
24de9 20 61 20 6e 65 77 20 73 6c 6f 74 20 68 65 72 65   a new slot here
24dea 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a   */.){.  char *z
24deb 3b 0a 20 20 69 66 28 20 2a 70 6e 45 6e 74 72 79  ;.  if( *pnEntry
24dec 20 3e 3d 20 2a 70 6e 41 6c 6c 6f 63 20 29 7b 0a   >= *pnAlloc ){.
24ded 20 20 20 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a      void *pNew;.
24dee 20 20 20 20 69 6e 74 20 6e 65 77 53 69 7a 65 3b      int newSize;
24def 0a 20 20 20 20 6e 65 77 53 69 7a 65 20 3d 20 28  .    newSize = (
24df0 2a 70 6e 41 6c 6c 6f 63 29 2a 32 20 2b 20 69 6e  *pnAlloc)*2 + in
24df1 69 74 53 69 7a 65 3b 0a 20 20 20 20 70 4e 65 77  itSize;.    pNew
24df2 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c   = sqlite3DbReal
24df3 6c 6f 63 28 64 62 2c 20 70 41 72 72 61 79 2c 20  loc(db, pArray, 
24df4 6e 65 77 53 69 7a 65 2a 73 7a 45 6e 74 72 79 29  newSize*szEntry)
24df5 3b 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d  ;.    if( pNew==
24df6 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 49 64 78  0 ){.      *pIdx
24df7 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72 65 74   = -1;.      ret
24df8 75 72 6e 20 70 41 72 72 61 79 3b 0a 20 20 20 20  urn pArray;.    
24df9 7d 0a 20 20 20 20 2a 70 6e 41 6c 6c 6f 63 20 3d  }.    *pnAlloc =
24dfa 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
24dfb 53 69 7a 65 28 64 62 2c 20 70 4e 65 77 29 2f 73  Size(db, pNew)/s
24dfc 7a 45 6e 74 72 79 3b 0a 20 20 20 20 70 41 72 72  zEntry;.    pArr
24dfd 61 79 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20  ay = pNew;.  }. 
24dfe 20 7a 20 3d 20 28 63 68 61 72 2a 29 70 41 72 72   z = (char*)pArr
24dff 61 79 3b 0a 20 20 6d 65 6d 73 65 74 28 26 7a 5b  ay;.  memset(&z[
24e00 2a 70 6e 45 6e 74 72 79 20 2a 20 73 7a 45 6e 74  *pnEntry * szEnt
24e01 72 79 5d 2c 20 30 2c 20 73 7a 45 6e 74 72 79 29  ry], 0, szEntry)
24e02 3b 0a 20 20 2a 70 49 64 78 20 3d 20 2a 70 6e 45  ;.  *pIdx = *pnE
24e03 6e 74 72 79 3b 0a 20 20 2b 2b 2a 70 6e 45 6e 74  ntry;.  ++*pnEnt
24e04 72 79 3b 0a 20 20 72 65 74 75 72 6e 20 70 41 72  ry;.  return pAr
24e05 72 61 79 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70  ray;.}../*.** Ap
24e06 70 65 6e 64 20 61 20 6e 65 77 20 65 6c 65 6d 65  pend a new eleme
24e07 6e 74 20 74 6f 20 74 68 65 20 67 69 76 65 6e 20  nt to the given 
24e08 49 64 4c 69 73 74 2e 20 20 43 72 65 61 74 65 20  IdList.  Create 
24e09 61 20 6e 65 77 20 49 64 4c 69 73 74 20 69 66 0a  a new IdList if.
24e0a 2a 2a 20 6e 65 65 64 20 62 65 2e 0a 2a 2a 0a 2a  ** need be..**.*
24e0b 2a 20 41 20 6e 65 77 20 49 64 4c 69 73 74 20 69  * A new IdList i
24e0c 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 72 20 4e  s returned, or N
24e0d 55 4c 4c 20 69 66 20 6d 61 6c 6c 6f 63 28 29 20  ULL if malloc() 
24e0e 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  fails..*/.SQLITE
24e0f 5f 50 52 49 56 41 54 45 20 49 64 4c 69 73 74 20  _PRIVATE IdList 
24e10 2a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 41 70  *sqlite3IdListAp
24e11 70 65 6e 64 28 73 71 6c 69 74 65 33 20 2a 64 62  pend(sqlite3 *db
24e12 2c 20 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 2c  , IdList *pList,
24e13 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b   Token *pToken){
24e14 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
24e15 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20  pList==0 ){.    
24e16 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 44  pList = sqlite3D
24e17 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
24e18 73 69 7a 65 6f 66 28 49 64 4c 69 73 74 29 20 29  sizeof(IdList) )
24e19 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d  ;.    if( pList=
24e1a 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
24e1b 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63     pList->nAlloc
24e1c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 70 4c 69 73   = 0;.  }.  pLis
24e1d 74 2d 3e 61 20 3d 20 73 71 6c 69 74 65 33 41 72  t->a = sqlite3Ar
24e1e 72 61 79 41 6c 6c 6f 63 61 74 65 28 0a 20 20 20  rayAllocate(.   
24e1f 20 20 20 64 62 2c 0a 20 20 20 20 20 20 70 4c 69     db,.      pLi
24e20 73 74 2d 3e 61 2c 0a 20 20 20 20 20 20 73 69 7a  st->a,.      siz
24e21 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30 5d 29  eof(pList->a[0])
24e22 2c 0a 20 20 20 20 20 20 35 2c 0a 20 20 20 20 20  ,.      5,.     
24e23 20 26 70 4c 69 73 74 2d 3e 6e 49 64 2c 0a 20 20   &pList->nId,.  
24e24 20 20 20 20 26 70 4c 69 73 74 2d 3e 6e 41 6c 6c      &pList->nAll
24e25 6f 63 2c 0a 20 20 20 20 20 20 26 69 0a 20 20 29  oc,.      &i.  )
24e26 3b 0a 20 20 69 66 28 20 69 3c 30 20 29 7b 0a 20  ;.  if( i<0 ){. 
24e27 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74     sqlite3IdList
24e28 44 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74  Delete(db, pList
24e29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  );.    return 0;
24e2a 0a 20 20 7d 0a 20 20 70 4c 69 73 74 2d 3e 61 5b  .  }.  pList->a[
24e2b 69 5d 2e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  i].zName = sqlit
24e2c 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
24e2d 64 62 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 72  db, pToken);.  r
24e2e 65 74 75 72 6e 20 70 4c 69 73 74 3b 0a 7d 0a 0a  eturn pList;.}..
24e2f 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20  /*.** Delete an 
24e30 49 64 4c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54  IdList..*/.SQLIT
24e31 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
24e32 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c 65  qlite3IdListDele
24e33 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  te(sqlite3 *db, 
24e34 49 64 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a  IdList *pList){.
24e35 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70    int i;.  if( p
24e36 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e  List==0 ) return
24e37 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
24e38 4c 69 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b  List->nId; i++){
24e39 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
24e3a 65 65 28 64 62 2c 20 70 4c 69 73 74 2d 3e 61 5b  ee(db, pList->a[
24e3b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20  i].zName);.  }. 
24e3c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
24e3d 62 2c 20 70 4c 69 73 74 2d 3e 61 29 3b 0a 20 20  b, pList->a);.  
24e3e 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
24e3f 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a  , pList);.}../*.
24e40 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e  ** Return the in
24e41 64 65 78 20 69 6e 20 70 4c 69 73 74 20 6f 66 20  dex in pList of 
24e42 74 68 65 20 69 64 65 6e 74 69 66 69 65 72 20 6e  the identifier n
24e43 61 6d 65 64 20 7a 49 64 2e 20 20 52 65 74 75 72  amed zId.  Retur
24e44 6e 20 2d 31 0a 2a 2a 20 69 66 20 6e 6f 74 20 66  n -1.** if not f
24e45 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ound..*/.SQLITE_
24e46 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
24e47 74 65 33 49 64 4c 69 73 74 49 6e 64 65 78 28 49  te3IdListIndex(I
24e48 64 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 63 6f  dList *pList, co
24e49 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 29  nst char *zName)
24e4a 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  {.  int i;.  if(
24e4b 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75   pList==0 ) retu
24e4c 72 6e 20 2d 31 3b 0a 20 20 66 6f 72 28 69 3d 30  rn -1;.  for(i=0
24e4d 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 49 64 3b 20  ; i<pList->nId; 
24e4e 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71  i++){.    if( sq
24e4f 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 4c 69  lite3StrICmp(pLi
24e50 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20  st->a[i].zName, 
24e51 7a 4e 61 6d 65 29 3d 3d 30 20 29 20 72 65 74 75  zName)==0 ) retu
24e52 72 6e 20 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn i;.  }.  retu
24e53 72 6e 20 2d 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn -1;.}../*.** 
24e54 45 78 70 61 6e 64 20 74 68 65 20 73 70 61 63 65  Expand the space
24e55 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 74   allocated for t
24e56 68 65 20 67 69 76 65 6e 20 53 72 63 4c 69 73 74  he given SrcList
24e57 20 6f 62 6a 65 63 74 20 62 79 0a 2a 2a 20 63 72   object by.** cr
24e58 65 61 74 69 6e 67 20 6e 45 78 74 72 61 20 6e 65  eating nExtra ne
24e59 77 20 73 6c 6f 74 73 20 62 65 67 69 6e 6e 69 6e  w slots beginnin
24e5a 67 20 61 74 20 69 53 74 61 72 74 2e 20 20 69 53  g at iStart.  iS
24e5b 74 61 72 74 20 69 73 20 7a 65 72 6f 20 62 61 73  tart is zero bas
24e5c 65 64 2e 0a 2a 2a 20 4e 65 77 20 73 6c 6f 74 73  ed..** New slots
24e5d 20 61 72 65 20 7a 65 72 6f 65 64 2e 0a 2a 2a 0a   are zeroed..**.
24e5e 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  ** For example, 
24e5f 73 75 70 70 6f 73 65 20 61 20 53 72 63 4c 69 73  suppose a SrcLis
24e60 74 20 69 6e 69 74 69 61 6c 6c 79 20 63 6f 6e 74  t initially cont
24e61 61 69 6e 73 20 74 77 6f 20 65 6e 74 72 69 65 73  ains two entries
24e62 3a 20 41 2c 42 2e 0a 2a 2a 20 54 6f 20 61 70 70  : A,B..** To app
24e63 65 6e 64 20 33 20 6e 65 77 20 65 6e 74 72 69 65  end 3 new entrie
24e64 73 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 2c 20  s onto the end, 
24e65 64 6f 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  do this:.**.**  
24e66 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
24e67 45 6e 6c 61 72 67 65 28 64 62 2c 20 70 53 72 63  Enlarge(db, pSrc
24e68 6c 69 73 74 2c 20 33 2c 20 32 29 3b 0a 2a 2a 0a  list, 3, 2);.**.
24e69 2a 2a 20 41 66 74 65 72 20 74 68 65 20 63 61 6c  ** After the cal
24e6a 6c 20 61 62 6f 76 65 20 69 74 20 77 6f 75 6c 64  l above it would
24e6b 20 63 6f 6e 74 61 69 6e 3a 20 20 41 2c 20 42 2c   contain:  A, B,
24e6c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2e 0a   nil, nil, nil..
24e6d 2a 2a 20 49 66 20 74 68 65 20 69 53 74 61 72 74  ** If the iStart
24e6e 20 61 72 67 75 6d 65 6e 74 20 68 61 64 20 62 65   argument had be
24e6f 65 6e 20 31 20 69 6e 73 74 65 61 64 20 6f 66 20  en 1 instead of 
24e70 32 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75  2, then the resu
24e71 6c 74 0a 2a 2a 20 77 6f 75 6c 64 20 68 61 76 65  lt.** would have
24e72 20 62 65 65 6e 3a 20 20 41 2c 20 6e 69 6c 2c 20   been:  A, nil, 
24e73 6e 69 6c 2c 20 6e 69 6c 2c 20 42 2e 20 20 54 6f  nil, nil, B.  To
24e74 20 70 72 65 70 65 6e 64 20 74 68 65 20 6e 65 77   prepend the new
24e75 20 73 6c 6f 74 73 2c 0a 2a 2a 20 74 68 65 20 69   slots,.** the i
24e76 53 74 61 72 74 20 76 61 6c 75 65 20 77 6f 75 6c  Start value woul
24e77 64 20 62 65 20 30 2e 20 20 54 68 65 20 72 65 73  d be 0.  The res
24e78 75 6c 74 20 74 68 65 6e 20 77 6f 75 6c 64 0a 2a  ult then would.*
24e79 2a 20 62 65 3a 20 6e 69 6c 2c 20 6e 69 6c 2c 20  * be: nil, nil, 
24e7a 6e 69 6c 2c 20 41 2c 20 42 2e 0a 2a 2a 0a 2a 2a  nil, A, B..**.**
24e7b 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c   If a memory all
24e7c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 20 74 68  ocation fails th
24e7d 65 20 53 72 63 4c 69 73 74 20 69 73 20 75 6e 63  e SrcList is unc
24e7e 68 61 6e 67 65 64 2e 20 20 54 68 65 0a 2a 2a 20  hanged.  The.** 
24e7f 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
24e80 20 66 6c 61 67 20 77 69 6c 6c 20 62 65 20 73 65   flag will be se
24e81 74 20 74 6f 20 74 72 75 65 2e 0a 2a 2f 0a 53 51  t to true..*/.SQ
24e82 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63  LITE_PRIVATE Src
24e83 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63  List *sqlite3Src
24e84 4c 69 73 74 45 6e 6c 61 72 67 65 28 0a 20 20 73  ListEnlarge(.  s
24e85 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
24e86 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f    /* Database co
24e87 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 69  nnection to noti
24e88 66 79 20 6f 66 20 4f 4f 4d 20 65 72 72 6f 72 73  fy of OOM errors
24e89 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
24e8a 53 72 63 2c 20 20 20 20 20 2f 2a 20 54 68 65 20  Src,     /* The 
24e8b 53 72 63 4c 69 73 74 20 74 6f 20 62 65 20 65 6e  SrcList to be en
24e8c 6c 61 72 67 65 64 20 2a 2f 0a 20 20 69 6e 74 20  larged */.  int 
24e8d 6e 45 78 74 72 61 2c 20 20 20 20 20 20 20 20 2f  nExtra,        /
24e8e 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 77 20  * Number of new 
24e8f 73 6c 6f 74 73 20 74 6f 20 61 64 64 20 74 6f 20  slots to add to 
24e90 70 53 72 63 2d 3e 61 5b 5d 20 2a 2f 0a 20 20 69  pSrc->a[] */.  i
24e91 6e 74 20 69 53 74 61 72 74 20 20 20 20 20 20 20  nt iStart       
24e92 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 70 53    /* Index in pS
24e93 72 63 2d 3e 61 5b 5d 20 6f 66 20 66 69 72 73 74  rc->a[] of first
24e94 20 6e 65 77 20 73 6c 6f 74 20 2a 2f 0a 29 7b 0a   new slot */.){.
24e95 20 20 69 6e 74 20 69 3b 0a 0a 20 20 2f 2a 20 53    int i;..  /* S
24e96 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 6f  anity checking o
24e97 6e 20 63 61 6c 6c 69 6e 67 20 70 61 72 61 6d 65  n calling parame
24e98 74 65 72 73 20 2a 2f 0a 20 20 61 73 73 65 72 74  ters */.  assert
24e99 28 20 69 53 74 61 72 74 3e 3d 30 20 29 3b 0a 20  ( iStart>=0 );. 
24e9a 20 61 73 73 65 72 74 28 20 6e 45 78 74 72 61 3e   assert( nExtra>
24e9b 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =1 );.  assert( 
24e9c 70 53 72 63 21 3d 30 20 29 3b 0a 20 20 61 73 73  pSrc!=0 );.  ass
24e9d 65 72 74 28 20 69 53 74 61 72 74 3c 3d 70 53 72  ert( iStart<=pSr
24e9e 63 2d 3e 6e 53 72 63 20 29 3b 0a 0a 20 20 2f 2a  c->nSrc );..  /*
24e9f 20 41 6c 6c 6f 63 61 74 65 20 61 64 64 69 74 69   Allocate additi
24ea0 6f 6e 61 6c 20 73 70 61 63 65 20 69 66 20 6e 65  onal space if ne
24ea1 65 64 65 64 20 2a 2f 0a 20 20 69 66 28 20 70 53  eded */.  if( pS
24ea2 72 63 2d 3e 6e 53 72 63 2b 6e 45 78 74 72 61 3e  rc->nSrc+nExtra>
24ea3 70 53 72 63 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a  pSrc->nAlloc ){.
24ea4 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70 4e 65      SrcList *pNe
24ea5 77 3b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c 6f  w;.    int nAllo
24ea6 63 20 3d 20 70 53 72 63 2d 3e 6e 53 72 63 2b 6e  c = pSrc->nSrc+n
24ea7 45 78 74 72 61 3b 0a 20 20 20 20 69 6e 74 20 6e  Extra;.    int n
24ea8 47 6f 74 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20  Got;.    pNew = 
24ea9 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
24eaa 28 64 62 2c 20 70 53 72 63 2c 0a 20 20 20 20 20  (db, pSrc,.     
24eab 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66            sizeof
24eac 28 2a 70 53 72 63 29 20 2b 20 28 6e 41 6c 6c 6f  (*pSrc) + (nAllo
24ead 63 2d 31 29 2a 73 69 7a 65 6f 66 28 70 53 72 63  c-1)*sizeof(pSrc
24eae 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 20 20 69  ->a[0]) );.    i
24eaf 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20  f( pNew==0 ){.  
24eb0 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
24eb1 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
24eb2 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 53 72        return pSr
24eb3 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 72  c;.    }.    pSr
24eb4 63 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 6e 47  c = pNew;.    nG
24eb5 6f 74 20 3d 20 28 73 71 6c 69 74 65 33 44 62 4d  ot = (sqlite3DbM
24eb6 61 6c 6c 6f 63 53 69 7a 65 28 64 62 2c 20 70 4e  allocSize(db, pN
24eb7 65 77 29 20 2d 20 73 69 7a 65 6f 66 28 2a 70 53  ew) - sizeof(*pS
24eb8 72 63 29 29 2f 73 69 7a 65 6f 66 28 70 53 72 63  rc))/sizeof(pSrc
24eb9 2d 3e 61 5b 30 5d 29 2b 31 3b 0a 20 20 20 20 70  ->a[0])+1;.    p
24eba 53 72 63 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 28 75  Src->nAlloc = (u
24ebb 31 36 29 6e 47 6f 74 3b 0a 20 20 7d 0a 0a 20 20  16)nGot;.  }..  
24ebc 2f 2a 20 4d 6f 76 65 20 65 78 69 73 74 69 6e 67  /* Move existing
24ebd 20 73 6c 6f 74 73 20 74 68 61 74 20 63 6f 6d 65   slots that come
24ebe 20 61 66 74 65 72 20 74 68 65 20 6e 65 77 6c 79   after the newly
24ebf 20 69 6e 73 65 72 74 65 64 20 73 6c 6f 74 73 0a   inserted slots.
24ec0 20 20 2a 2a 20 6f 75 74 20 6f 66 20 74 68 65 20    ** out of the 
24ec1 77 61 79 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 70  way */.  for(i=p
24ec2 53 72 63 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 3d  Src->nSrc-1; i>=
24ec3 69 53 74 61 72 74 3b 20 69 2d 2d 29 7b 0a 20 20  iStart; i--){.  
24ec4 20 20 70 53 72 63 2d 3e 61 5b 69 2b 6e 45 78 74    pSrc->a[i+nExt
24ec5 72 61 5d 20 3d 20 70 53 72 63 2d 3e 61 5b 69 5d  ra] = pSrc->a[i]
24ec6 3b 0a 20 20 7d 0a 20 20 70 53 72 63 2d 3e 6e 53  ;.  }.  pSrc->nS
24ec7 72 63 20 2b 3d 20 28 69 31 36 29 6e 45 78 74 72  rc += (i16)nExtr
24ec8 61 3b 0a 0a 20 20 2f 2a 20 5a 65 72 6f 20 74 68  a;..  /* Zero th
24ec9 65 20 6e 65 77 6c 79 20 61 6c 6c 6f 63 61 74 65  e newly allocate
24eca 64 20 73 6c 6f 74 73 20 2a 2f 0a 20 20 6d 65 6d  d slots */.  mem
24ecb 73 65 74 28 26 70 53 72 63 2d 3e 61 5b 69 53 74  set(&pSrc->a[iSt
24ecc 61 72 74 5d 2c 20 30 2c 20 73 69 7a 65 6f 66 28  art], 0, sizeof(
24ecd 70 53 72 63 2d 3e 61 5b 30 5d 29 2a 6e 45 78 74  pSrc->a[0])*nExt
24ece 72 61 29 3b 0a 20 20 66 6f 72 28 69 3d 69 53 74  ra);.  for(i=iSt
24ecf 61 72 74 3b 20 69 3c 69 53 74 61 72 74 2b 6e 45  art; i<iStart+nE
24ed0 78 74 72 61 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  xtra; i++){.    
24ed1 70 53 72 63 2d 3e 61 5b 69 5d 2e 69 43 75 72 73  pSrc->a[i].iCurs
24ed2 6f 72 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a 20 20  or = -1;.  }..  
24ed3 2f 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  /* Return a poin
24ed4 74 65 72 20 74 6f 20 74 68 65 20 65 6e 6c 61 72  ter to the enlar
24ed5 67 65 64 20 53 72 63 4c 69 73 74 20 2a 2f 0a 20  ged SrcList */. 
24ed6 20 72 65 74 75 72 6e 20 70 53 72 63 3b 0a 7d 0a   return pSrc;.}.
24ed7 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61  ../*.** Append a
24ed8 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65 20   new table name 
24ed9 74 6f 20 74 68 65 20 67 69 76 65 6e 20 53 72 63  to the given Src
24eda 4c 69 73 74 2e 20 20 43 72 65 61 74 65 20 61 20  List.  Create a 
24edb 6e 65 77 20 53 72 63 4c 69 73 74 20 69 66 0a 2a  new SrcList if.*
24edc 2a 20 6e 65 65 64 20 62 65 2e 20 20 41 20 6e 65  * need be.  A ne
24edd 77 20 65 6e 74 72 79 20 69 73 20 63 72 65 61 74  w entry is creat
24ede 65 64 20 69 6e 20 74 68 65 20 53 72 63 4c 69 73  ed in the SrcLis
24edf 74 20 65 76 65 6e 20 69 66 20 70 54 61 62 6c 65  t even if pTable
24ee0 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   is NULL..**.** 
24ee1 41 20 53 72 63 4c 69 73 74 20 69 73 20 72 65 74  A SrcList is ret
24ee2 75 72 6e 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 69  urned, or NULL i
24ee3 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f 4f  f there is an OO
24ee4 4d 20 65 72 72 6f 72 2e 20 20 54 68 65 20 72 65  M error.  The re
24ee5 74 75 72 6e 65 64 0a 2a 2a 20 53 72 63 4c 69 73  turned.** SrcLis
24ee6 74 20 6d 69 67 68 74 20 62 65 20 74 68 65 20 73  t might be the s
24ee7 61 6d 65 20 61 73 20 74 68 65 20 53 72 63 4c 69  ame as the SrcLi
24ee8 73 74 20 74 68 61 74 20 77 61 73 20 69 6e 70 75  st that was inpu
24ee9 74 20 6f 72 20 69 74 20 6d 69 67 68 74 20 62 65  t or it might be
24eea 0a 2a 2a 20 61 20 6e 65 77 20 6f 6e 65 2e 20 20  .** a new one.  
24eeb 49 66 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 20  If an OOM error 
24eec 64 6f 65 73 20 6f 63 63 75 72 73 2c 20 74 68 65  does occurs, the
24eed 6e 20 74 68 65 20 70 72 69 6f 72 20 76 61 6c 75  n the prior valu
24eee 65 20 6f 66 20 70 4c 69 73 74 0a 2a 2a 20 74 68  e of pList.** th
24eef 61 74 20 69 73 20 69 6e 70 75 74 20 74 6f 20 74  at is input to t
24ef0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61  his routine is a
24ef1 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 66 72 65  utomatically fre
24ef2 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 44 61  ed..**.** If pDa
24ef3 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 6e 75  tabase is not nu
24ef4 6c 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  ll, it means tha
24ef5 74 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20  t the table has 
24ef6 61 6e 20 6f 70 74 69 6f 6e 61 6c 0a 2a 2a 20 64  an optional.** d
24ef7 61 74 61 62 61 73 65 20 6e 61 6d 65 20 70 72 65  atabase name pre
24ef8 66 69 78 2e 20 20 4c 69 6b 65 20 74 68 69 73 3a  fix.  Like this:
24ef9 20 20 22 64 61 74 61 62 61 73 65 2e 74 61 62 6c    "database.tabl
24efa 65 22 2e 20 20 54 68 65 20 70 44 61 74 61 62 61  e".  The pDataba
24efb 73 65 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f 20  se.** points to 
24efc 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 61  the table name a
24efd 6e 64 20 74 68 65 20 70 54 61 62 6c 65 20 70 6f  nd the pTable po
24efe 69 6e 74 73 20 74 6f 20 74 68 65 20 64 61 74 61  ints to the data
24eff 62 61 73 65 20 6e 61 6d 65 2e 0a 2a 2a 20 54 68  base name..** Th
24f00 65 20 53 72 63 4c 69 73 74 2e 61 5b 5d 2e 7a 4e  e SrcList.a[].zN
24f01 61 6d 65 20 66 69 65 6c 64 20 69 73 20 66 69 6c  ame field is fil
24f02 6c 65 64 20 77 69 74 68 20 74 68 65 20 74 61 62  led with the tab
24f03 6c 65 20 6e 61 6d 65 20 77 68 69 63 68 20 6d 69  le name which mi
24f04 67 68 74 0a 2a 2a 20 63 6f 6d 65 20 66 72 6f 6d  ght.** come from
24f05 20 70 54 61 62 6c 65 20 28 69 66 20 70 44 61 74   pTable (if pDat
24f06 61 62 61 73 65 20 69 73 20 4e 55 4c 4c 29 20 6f  abase is NULL) o
24f07 72 20 66 72 6f 6d 20 70 44 61 74 61 62 61 73 65  r from pDatabase
24f08 2e 20 20 0a 2a 2a 20 53 72 63 4c 69 73 74 2e 61  .  .** SrcList.a
24f09 5b 5d 2e 7a 44 61 74 61 62 61 73 65 20 69 73 20  [].zDatabase is 
24f0a 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20  filled with the 
24f0b 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 66 72  database name fr
24f0c 6f 6d 20 70 54 61 62 6c 65 2c 0a 2a 2a 20 6f 72  om pTable,.** or
24f0d 20 77 69 74 68 20 4e 55 4c 4c 20 69 66 20 6e 6f   with NULL if no
24f0e 20 64 61 74 61 62 61 73 65 20 69 73 20 73 70 65   database is spe
24f0f 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 6e  cified..**.** In
24f10 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66   other words, if
24f11 20 63 61 6c 6c 20 6c 69 6b 65 20 74 68 69 73 3a   call like this:
24f12 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 73  .**.**         s
24f13 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
24f14 65 6e 64 28 44 2c 41 2c 42 2c 30 29 3b 0a 2a 2a  end(D,A,B,0);.**
24f15 0a 2a 2a 20 54 68 65 6e 20 42 20 69 73 20 61 20  .** Then B is a 
24f16 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e 64 20 74  table name and t
24f17 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  he database name
24f18 20 69 73 20 75 6e 73 70 65 63 69 66 69 65 64 2e   is unspecified.
24f19 20 20 49 66 20 63 61 6c 6c 65 64 0a 2a 2a 20 6c    If called.** l
24f1a 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
24f1b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
24f1c 72 63 4c 69 73 74 41 70 70 65 6e 64 28 44 2c 41  rcListAppend(D,A
24f1d 2c 42 2c 43 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65  ,B,C);.**.** The
24f1e 6e 20 43 20 69 73 20 74 68 65 20 74 61 62 6c 65  n C is the table
24f1f 20 6e 61 6d 65 20 61 6e 64 20 42 20 69 73 20 74   name and B is t
24f20 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  he database name
24f21 2e 20 20 49 66 20 43 20 69 73 20 64 65 66 69 6e  .  If C is defin
24f22 65 64 0a 2a 2a 20 74 68 65 6e 20 73 6f 20 69 73  ed.** then so is
24f23 20 42 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f   B.  In other wo
24f24 72 64 73 2c 20 77 65 20 6e 65 76 65 72 20 68 61  rds, we never ha
24f25 76 65 20 61 20 63 61 73 65 20 77 68 65 72 65 3a  ve a case where:
24f26 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 73  .**.**         s
24f27 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70  qlite3SrcListApp
24f28 65 6e 64 28 44 2c 41 2c 30 2c 43 29 3b 0a 2a 2a  end(D,A,0,C);.**
24f29 0a 2a 2a 20 42 6f 74 68 20 70 54 61 62 6c 65 20  .** Both pTable 
24f2a 61 6e 64 20 70 44 61 74 61 62 61 73 65 20 61 72  and pDatabase ar
24f2b 65 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 20  e assumed to be 
24f2c 71 75 6f 74 65 64 2e 20 20 54 68 65 79 20 61 72  quoted.  They ar
24f2d 65 20 64 65 71 75 6f 74 65 64 0a 2a 2a 20 62 65  e dequoted.** be
24f2e 66 6f 72 65 20 62 65 69 6e 67 20 61 64 64 65 64  fore being added
24f2f 20 74 6f 20 74 68 65 20 53 72 63 4c 69 73 74 2e   to the SrcList.
24f30 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
24f31 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c 69  TE SrcList *sqli
24f32 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
24f33 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
24f34 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65          /* Conne
24f35 63 74 69 6f 6e 20 74 6f 20 6e 6f 74 69 66 79 20  ction to notify 
24f36 6f 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72  of malloc failur
24f37 65 73 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  es */.  SrcList 
24f38 2a 70 4c 69 73 74 2c 20 20 20 20 20 2f 2a 20 41  *pList,     /* A
24f39 70 70 65 6e 64 20 74 6f 20 74 68 69 73 20 53 72  ppend to this Sr
24f3a 63 4c 69 73 74 2e 20 4e 55 4c 4c 20 63 72 65 61  cList. NULL crea
24f3b 74 65 73 20 61 20 6e 65 77 20 53 72 63 4c 69 73  tes a new SrcLis
24f3c 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54  t */.  Token *pT
24f3d 61 62 6c 65 2c 20 20 20 20 20 20 2f 2a 20 54 61  able,      /* Ta
24f3e 62 6c 65 20 74 6f 20 61 70 70 65 6e 64 20 2a 2f  ble to append */
24f3f 0a 20 20 54 6f 6b 65 6e 20 2a 70 44 61 74 61 62  .  Token *pDatab
24f40 61 73 65 20 20 20 20 2f 2a 20 44 61 74 61 62 61  ase    /* Databa
24f41 73 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  se of the table 
24f42 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 53  */.){.  struct S
24f43 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
24f44 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44  em;.  assert( pD
24f45 61 74 61 62 61 73 65 3d 3d 30 20 7c 7c 20 70 54  atabase==0 || pT
24f46 61 62 6c 65 21 3d 30 20 29 3b 20 20 2f 2a 20 43  able!=0 );  /* C
24f47 61 6e 6e 6f 74 20 68 61 76 65 20 43 20 77 69 74  annot have C wit
24f48 68 6f 75 74 20 42 20 2a 2f 0a 20 20 69 66 28 20  hout B */.  if( 
24f49 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20  pList==0 ){.    
24f4a 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 44  pList = sqlite3D
24f4b 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
24f4c 73 69 7a 65 6f 66 28 53 72 63 4c 69 73 74 29 20  sizeof(SrcList) 
24f4d 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 69 73 74  );.    if( pList
24f4e 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
24f4f 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f      pList->nAllo
24f50 63 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 70 4c 69  c = 1;.  }.  pLi
24f51 73 74 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c  st = sqlite3SrcL
24f52 69 73 74 45 6e 6c 61 72 67 65 28 64 62 2c 20 70  istEnlarge(db, p
24f53 4c 69 73 74 2c 20 31 2c 20 70 4c 69 73 74 2d 3e  List, 1, pList->
24f54 6e 53 72 63 29 3b 0a 20 20 69 66 28 20 64 62 2d  nSrc);.  if( db-
24f55 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
24f56 0a 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c  .    sqlite3SrcL
24f57 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4c  istDelete(db, pL
24f58 69 73 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ist);.    return
24f59 20 30 3b 0a 20 20 7d 0a 20 20 70 49 74 65 6d 20   0;.  }.  pItem 
24f5a 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70 4c 69 73  = &pList->a[pLis
24f5b 74 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20 69 66  t->nSrc-1];.  if
24f5c 28 20 70 44 61 74 61 62 61 73 65 20 26 26 20 70  ( pDatabase && p
24f5d 44 61 74 61 62 61 73 65 2d 3e 7a 3d 3d 30 20 29  Database->z==0 )
24f5e 7b 0a 20 20 20 20 70 44 61 74 61 62 61 73 65 20  {.    pDatabase 
24f5f 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  = 0;.  }.  if( p
24f60 44 61 74 61 62 61 73 65 20 29 7b 0a 20 20 20 20  Database ){.    
24f61 54 6f 6b 65 6e 20 2a 70 54 65 6d 70 20 3d 20 70  Token *pTemp = p
24f62 44 61 74 61 62 61 73 65 3b 0a 20 20 20 20 70 44  Database;.    pD
24f63 61 74 61 62 61 73 65 20 3d 20 70 54 61 62 6c 65  atabase = pTable
24f64 3b 0a 20 20 20 20 70 54 61 62 6c 65 20 3d 20 70  ;.    pTable = p
24f65 54 65 6d 70 3b 0a 20 20 7d 0a 20 20 70 49 74 65  Temp;.  }.  pIte
24f66 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  m->zName = sqlit
24f67 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
24f68 64 62 2c 20 70 54 61 62 6c 65 29 3b 0a 20 20 70  db, pTable);.  p
24f69 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20  Item->zDatabase 
24f6a 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
24f6b 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 44 61 74 61  mToken(db, pData
24f6c 62 61 73 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  base);.  return 
24f6d 70 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  pList;.}../*.** 
24f6e 41 73 73 69 67 6e 20 56 64 62 65 43 75 72 73 6f  Assign VdbeCurso
24f6f 72 20 69 6e 64 65 78 20 6e 75 6d 62 65 72 73 20  r index numbers 
24f70 74 6f 20 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e  to all tables in
24f71 20 61 20 53 72 63 4c 69 73 74 0a 2a 2f 0a 53 51   a SrcList.*/.SQ
24f72 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
24f73 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  d sqlite3SrcList
24f74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28 50 61  AssignCursors(Pa
24f75 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72 63  rse *pParse, Src
24f76 4c 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20  List *pList){.  
24f77 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
24f78 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
24f79 74 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 70 4c  tem;.  assert(pL
24f7a 69 73 74 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64  ist || pParse->d
24f7b 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
24f7c 29 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 20 29  );.  if( pList )
24f7d 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70  {.    for(i=0, p
24f7e 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69  Item=pList->a; i
24f7f 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  <pList->nSrc; i+
24f80 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  +, pItem++){.   
24f81 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43     if( pItem->iC
24f82 75 72 73 6f 72 3e 3d 30 20 29 20 62 72 65 61 6b  ursor>=0 ) break
24f83 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69  ;.      pItem->i
24f84 43 75 72 73 6f 72 20 3d 20 70 50 61 72 73 65 2d  Cursor = pParse-
24f85 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 69  >nTab++;.      i
24f86 66 28 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63  f( pItem->pSelec
24f87 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  t ){.        sql
24f88 69 74 65 33 53 72 63 4c 69 73 74 41 73 73 69 67  ite3SrcListAssig
24f89 6e 43 75 72 73 6f 72 73 28 70 50 61 72 73 65 2c  nCursors(pParse,
24f8a 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2d   pItem->pSelect-
24f8b 3e 70 53 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a  >pSrc);.      }.
24f8c 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
24f8d 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74  ** Delete an ent
24f8e 69 72 65 20 53 72 63 4c 69 73 74 20 69 6e 63 6c  ire SrcList incl
24f8f 75 64 69 6e 67 20 61 6c 6c 20 69 74 73 20 73 75  uding all its su
24f90 62 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53  bstructure..*/.S
24f91 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
24f92 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  id sqlite3SrcLis
24f93 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20  tDelete(sqlite3 
24f94 2a 64 62 2c 20 53 72 63 4c 69 73 74 20 2a 70 4c  *db, SrcList *pL
24f95 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  ist){.  int i;. 
24f96 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
24f97 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69  item *pItem;.  i
24f98 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65  f( pList==0 ) re
24f99 74 75 72 6e 3b 0a 20 20 66 6f 72 28 70 49 74 65  turn;.  for(pIte
24f9a 6d 3d 70 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b  m=pList->a, i=0;
24f9b 20 69 3c 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20   i<pList->nSrc; 
24f9c 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
24f9d 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
24f9e 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74  (db, pItem->zDat
24f9f 61 62 61 73 65 29 3b 0a 20 20 20 20 73 71 6c 69  abase);.    sqli
24fa0 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 49  te3DbFree(db, pI
24fa1 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  tem->zName);.   
24fa2 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
24fa3 62 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73  b, pItem->zAlias
24fa4 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
24fa5 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e  Free(db, pItem->
24fa6 7a 49 6e 64 65 78 29 3b 0a 20 20 20 20 73 71 6c  zIndex);.    sql
24fa7 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28  ite3DeleteTable(
24fa8 70 49 74 65 6d 2d 3e 70 54 61 62 29 3b 0a 20 20  pItem->pTab);.  
24fa9 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
24faa 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65 6d 2d  elete(db, pItem-
24fab 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 73  >pSelect);.    s
24fac 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
24fad 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 4f 6e 29  (db, pItem->pOn)
24fae 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c  ;.    sqlite3IdL
24faf 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 49  istDelete(db, pI
24fb0 74 65 6d 2d 3e 70 55 73 69 6e 67 29 3b 0a 20 20  tem->pUsing);.  
24fb1 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  }.  sqlite3DbFre
24fb2 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a  e(db, pList);.}.
24fb3 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
24fb4 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79  ine is called by
24fb5 20 74 68 65 20 70 61 72 73 65 72 20 74 6f 20 61   the parser to a
24fb6 64 64 20 61 20 6e 65 77 20 74 65 72 6d 20 74 6f  dd a new term to
24fb7 20 74 68 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 61   the.** end of a
24fb8 20 67 72 6f 77 69 6e 67 20 46 52 4f 4d 20 63 6c   growing FROM cl
24fb9 61 75 73 65 2e 20 20 54 68 65 20 22 70 22 20 70  ause.  The "p" p
24fba 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20  arameter is the 
24fbb 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 46  part of.** the F
24fbc 52 4f 4d 20 63 6c 61 75 73 65 20 74 68 61 74 20  ROM clause that 
24fbd 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
24fbe 20 63 6f 6e 73 74 72 75 63 74 65 64 2e 20 20 22   constructed.  "
24fbf 70 22 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 69 66  p" is NULL.** if
24fc0 20 74 68 69 73 20 69 73 20 74 68 65 20 66 69 72   this is the fir
24fc1 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 46  st term of the F
24fc2 52 4f 4d 20 63 6c 61 75 73 65 2e 20 20 70 54 61  ROM clause.  pTa
24fc3 62 6c 65 20 61 6e 64 20 70 44 61 74 61 62 61 73  ble and pDatabas
24fc4 65 0a 2a 2a 20 61 72 65 20 74 68 65 20 6e 61 6d  e.** are the nam
24fc5 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 61  e of the table a
24fc6 6e 64 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  nd database name
24fc7 64 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  d in the FROM cl
24fc8 61 75 73 65 20 74 65 72 6d 2e 0a 2a 2a 20 70 44  ause term..** pD
24fc9 61 74 61 62 61 73 65 20 69 73 20 4e 55 4c 4c 20  atabase is NULL 
24fca 69 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  if the database 
24fcb 6e 61 6d 65 20 71 75 61 6c 69 66 69 65 72 20 69  name qualifier i
24fcc 73 20 6d 69 73 73 69 6e 67 20 2d 20 74 68 65 0a  s missing - the.
24fcd 2a 2a 20 75 73 75 61 6c 20 63 61 73 65 2e 20 20  ** usual case.  
24fce 49 66 20 74 68 65 20 74 65 72 6d 20 68 61 73 20  If the term has 
24fcf 61 20 61 6c 69 61 73 2c 20 74 68 65 6e 20 70 41  a alias, then pA
24fd0 6c 69 61 73 20 70 6f 69 6e 74 73 20 74 6f 20 74  lias points to t
24fd1 68 65 0a 2a 2a 20 61 6c 69 61 73 20 74 6f 6b 65  he.** alias toke
24fd2 6e 2e 20 20 49 66 20 74 68 65 20 74 65 72 6d 20  n.  If the term 
24fd3 69 73 20 61 20 73 75 62 71 75 65 72 79 2c 20 74  is a subquery, t
24fd4 68 65 6e 20 70 53 75 62 71 75 65 72 79 20 69 73  hen pSubquery is
24fd5 20 74 68 65 0a 2a 2a 20 53 45 4c 45 43 54 20 73   the.** SELECT s
24fd6 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 74 68  tatement that th
24fd7 65 20 73 75 62 71 75 65 72 79 20 65 6e 63 6f 64  e subquery encod
24fd8 65 73 2e 20 20 54 68 65 20 70 54 61 62 6c 65 20  es.  The pTable 
24fd9 61 6e 64 0a 2a 2a 20 70 44 61 74 61 62 61 73 65  and.** pDatabase
24fda 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20   parameters are 
24fdb 4e 55 4c 4c 20 66 6f 72 20 73 75 62 71 75 65 72  NULL for subquer
24fdc 69 65 73 2e 20 20 54 68 65 20 70 4f 6e 20 61 6e  ies.  The pOn an
24fdd 64 20 70 55 73 69 6e 67 0a 2a 2a 20 70 61 72 61  d pUsing.** para
24fde 6d 65 74 65 72 73 20 61 72 65 20 74 68 65 20 63  meters are the c
24fdf 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 4f 4e  ontent of the ON
24fe0 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73   and USING claus
24fe1 65 73 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  es..**.** Return
24fe2 20 61 20 6e 65 77 20 53 72 63 4c 69 73 74 20 77   a new SrcList w
24fe3 68 69 63 68 20 65 6e 63 6f 64 65 73 20 69 73 20  hich encodes is 
24fe4 74 68 65 20 46 52 4f 4d 20 77 69 74 68 20 74 68  the FROM with th
24fe5 65 20 6e 65 77 0a 2a 2a 20 74 65 72 6d 20 61 64  e new.** term ad
24fe6 64 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ded..*/.SQLITE_P
24fe7 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a  RIVATE SrcList *
24fe8 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70  sqlite3SrcListAp
24fe9 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 0a 20 20  pendFromTerm(.  
24fea 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
24feb 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69          /* Parsi
24fec 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
24fed 53 72 63 4c 69 73 74 20 2a 70 2c 20 20 20 20 20  SrcList *p,     
24fee 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c          /* The l
24fef 65 66 74 20 70 61 72 74 20 6f 66 20 74 68 65 20  eft part of the 
24ff0 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 6c 72 65  FROM clause alre
24ff1 61 64 79 20 73 65 65 6e 20 2a 2f 0a 20 20 54 6f  ady seen */.  To
24ff2 6b 65 6e 20 2a 70 54 61 62 6c 65 2c 20 20 20 20  ken *pTable,    
24ff3 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
24ff4 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 61 64   the table to ad
24ff5 64 20 74 6f 20 74 68 65 20 46 52 4f 4d 20 63 6c  d to the FROM cl
24ff6 61 75 73 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  ause */.  Token 
24ff7 2a 70 44 61 74 61 62 61 73 65 2c 20 20 20 20 20  *pDatabase,     
24ff8 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
24ff9 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
24ffa 6e 69 6e 67 20 70 54 61 62 6c 65 20 2a 2f 0a 20  ning pTable */. 
24ffb 20 54 6f 6b 65 6e 20 2a 70 41 6c 69 61 73 2c 20   Token *pAlias, 
24ffc 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
24ffd 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20  right-hand side 
24ffe 6f 66 20 74 68 65 20 41 53 20 73 75 62 65 78 70  of the AS subexp
24fff 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c  ression */.  Sel
25000 65 63 74 20 2a 70 53 75 62 71 75 65 72 79 2c 20  ect *pSubquery, 
25001 20 20 20 20 20 2f 2a 20 41 20 73 75 62 71 75 65       /* A subque
25002 72 79 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65  ry used in place
25003 20 6f 66 20 61 20 74 61 62 6c 65 20 6e 61 6d 65   of a table name
25004 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f 6e 2c   */.  Expr *pOn,
25005 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
25006 20 54 68 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f   The ON clause o
25007 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 49 64  f a join */.  Id
25008 4c 69 73 74 20 2a 70 55 73 69 6e 67 20 20 20 20  List *pUsing    
25009 20 20 20 20 20 20 2f 2a 20 54 68 65 20 55 53 49        /* The USI
2500a 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a  NG clause of a j
2500b 6f 69 6e 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75  oin */.){.  stru
2500c 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
2500d 2a 70 49 74 65 6d 3b 0a 20 20 73 71 6c 69 74 65  *pItem;.  sqlite
2500e 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2500f 64 62 3b 0a 20 20 69 66 28 20 21 70 20 26 26 20  db;.  if( !p && 
25010 28 70 4f 6e 20 7c 7c 20 70 55 73 69 6e 67 29 20  (pOn || pUsing) 
25011 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
25012 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
25013 61 20 4a 4f 49 4e 20 63 6c 61 75 73 65 20 69 73  a JOIN clause is
25014 20 72 65 71 75 69 72 65 64 20 62 65 66 6f 72 65   required before
25015 20 25 73 22 2c 20 0a 20 20 20 20 20 20 28 70 4f   %s", .      (pO
25016 6e 20 3f 20 22 4f 4e 22 20 3a 20 22 55 53 49 4e  n ? "ON" : "USIN
25017 47 22 29 0a 20 20 20 20 29 3b 0a 20 20 20 20 67  G").    );.    g
25018 6f 74 6f 20 61 70 70 65 6e 64 5f 66 72 6f 6d 5f  oto append_from_
25019 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 70 20 3d  error;.  }.  p =
2501a 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
2501b 70 70 65 6e 64 28 64 62 2c 20 70 2c 20 70 54 61  ppend(db, p, pTa
2501c 62 6c 65 2c 20 70 44 61 74 61 62 61 73 65 29 3b  ble, pDatabase);
2501d 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 4e  .  if( p==0 || N
2501e 45 56 45 52 28 70 2d 3e 6e 53 72 63 3d 3d 30 29  EVER(p->nSrc==0)
2501f 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 61 70 70   ){.    goto app
25020 65 6e 64 5f 66 72 6f 6d 5f 65 72 72 6f 72 3b 0a  end_from_error;.
25021 20 20 7d 0a 20 20 70 49 74 65 6d 20 3d 20 26 70    }.  pItem = &p
25022 2d 3e 61 5b 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0a  ->a[p->nSrc-1];.
25023 20 20 61 73 73 65 72 74 28 20 70 41 6c 69 61 73    assert( pAlias
25024 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 41 6c  !=0 );.  if( pAl
25025 69 61 73 2d 3e 6e 20 29 7b 0a 20 20 20 20 70 49  ias->n ){.    pI
25026 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 73 71  tem->zAlias = sq
25027 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
25028 65 6e 28 64 62 2c 20 70 41 6c 69 61 73 29 3b 0a  en(db, pAlias);.
25029 20 20 7d 0a 20 20 70 49 74 65 6d 2d 3e 70 53 65    }.  pItem->pSe
2502a 6c 65 63 74 20 3d 20 70 53 75 62 71 75 65 72 79  lect = pSubquery
2502b 3b 0a 20 20 70 49 74 65 6d 2d 3e 70 4f 6e 20 3d  ;.  pItem->pOn =
2502c 20 70 4f 6e 3b 0a 20 20 70 49 74 65 6d 2d 3e 70   pOn;.  pItem->p
2502d 55 73 69 6e 67 20 3d 20 70 55 73 69 6e 67 3b 0a  Using = pUsing;.
2502e 20 20 72 65 74 75 72 6e 20 70 3b 0a 0a 20 61 70    return p;.. ap
2502f 70 65 6e 64 5f 66 72 6f 6d 5f 65 72 72 6f 72 3a  pend_from_error:
25030 0a 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20  .  assert( p==0 
25031 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
25032 44 65 6c 65 74 65 28 64 62 2c 20 70 4f 6e 29 3b  Delete(db, pOn);
25033 0a 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74  .  sqlite3IdList
25034 44 65 6c 65 74 65 28 64 62 2c 20 70 55 73 69 6e  Delete(db, pUsin
25035 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c  g);.  sqlite3Sel
25036 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53  ectDelete(db, pS
25037 75 62 71 75 65 72 79 29 3b 0a 20 20 72 65 74 75  ubquery);.  retu
25038 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  rn 0;.}../*.** A
25039 64 64 20 61 6e 20 49 4e 44 45 58 45 44 20 42 59  dd an INDEXED BY
2503a 20 6f 72 20 4e 4f 54 20 49 4e 44 45 58 45 44 20   or NOT INDEXED 
2503b 63 6c 61 75 73 65 20 74 6f 20 74 68 65 20 6d 6f  clause to the mo
2503c 73 74 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65  st recently adde
2503d 64 20 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 6f 66  d .** element of
2503e 20 74 68 65 20 73 6f 75 72 63 65 2d 6c 69 73 74   the source-list
2503f 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73   passed as the s
25040 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a  econd argument..
25041 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
25042 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 72  E void sqlite3Sr
25043 63 4c 69 73 74 49 6e 64 65 78 65 64 42 79 28 50  cListIndexedBy(P
25044 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72  arse *pParse, Sr
25045 63 4c 69 73 74 20 2a 70 2c 20 54 6f 6b 65 6e 20  cList *p, Token 
25046 2a 70 49 6e 64 65 78 65 64 42 79 29 7b 0a 20 20  *pIndexedBy){.  
25047 61 73 73 65 72 74 28 20 70 49 6e 64 65 78 65 64  assert( pIndexed
25048 42 79 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  By!=0 );.  if( p
25049 20 26 26 20 41 4c 57 41 59 53 28 70 2d 3e 6e 53   && ALWAYS(p->nS
2504a 72 63 3e 30 29 20 29 7b 0a 20 20 20 20 73 74 72  rc>0) ){.    str
2504b 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
2504c 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e 61 5b   *pItem = &p->a[
2504d 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20 20 20  p->nSrc-1];.    
2504e 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 6e  assert( pItem->n
2504f 6f 74 49 6e 64 65 78 65 64 3d 3d 30 20 26 26 20  otIndexed==0 && 
25050 70 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 3d 3d 30  pItem->zIndex==0
25051 20 29 3b 0a 20 20 20 20 69 66 28 20 70 49 6e 64   );.    if( pInd
25052 65 78 65 64 42 79 2d 3e 6e 3d 3d 31 20 26 26 20  exedBy->n==1 && 
25053 21 70 49 6e 64 65 78 65 64 42 79 2d 3e 7a 20 29  !pIndexedBy->z )
25054 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 22 4e 4f  {.      /* A "NO
25055 54 20 49 4e 44 45 58 45 44 22 20 63 6c 61 75 73  T INDEXED" claus
25056 65 20 77 61 73 20 73 75 70 70 6c 69 65 64 2e 20  e was supplied. 
25057 53 65 65 20 70 61 72 73 65 2e 79 20 0a 20 20 20  See parse.y .   
25058 20 20 20 2a 2a 20 63 6f 6e 73 74 72 75 63 74 20     ** construct 
25059 22 69 6e 64 65 78 65 64 5f 6f 70 74 22 20 66 6f  "indexed_opt" fo
2505a 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a 20 20  r details. */.  
2505b 20 20 20 20 70 49 74 65 6d 2d 3e 6e 6f 74 49 6e      pItem->notIn
2505c 64 65 78 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d  dexed = 1;.    }
2505d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 74 65  else{.      pIte
2505e 6d 2d 3e 7a 49 6e 64 65 78 20 3d 20 73 71 6c 69  m->zIndex = sqli
2505f 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
25060 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 49 6e  (pParse->db, pIn
25061 64 65 78 65 64 42 79 29 3b 0a 20 20 20 20 7d 0a  dexedBy);.    }.
25062 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 68 65    }.}../*.** Whe
25063 6e 20 62 75 69 6c 64 69 6e 67 20 75 70 20 61 20  n building up a 
25064 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 6e 20 74  FROM clause in t
25065 68 65 20 70 61 72 73 65 72 2c 20 74 68 65 20 6a  he parser, the j
25066 6f 69 6e 20 6f 70 65 72 61 74 6f 72 0a 2a 2a 20  oin operator.** 
25067 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 61 74 74  is initially att
25068 61 63 68 65 64 20 74 6f 20 74 68 65 20 6c 65 66  ached to the lef
25069 74 20 6f 70 65 72 61 6e 64 2e 20 20 42 75 74 20  t operand.  But 
2506a 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74  the code generat
2506b 6f 72 0a 2a 2a 20 65 78 70 65 63 74 73 20 74 68  or.** expects th
2506c 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 20  e join operator 
2506d 74 6f 20 62 65 20 6f 6e 20 74 68 65 20 72 69 67  to be on the rig
2506e 68 74 20 6f 70 65 72 61 6e 64 2e 20 20 54 68 69  ht operand.  Thi
2506f 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 53 68 69  s routine.** Shi
25070 66 74 73 20 61 6c 6c 20 6a 6f 69 6e 20 6f 70 65  fts all join ope
25071 72 61 74 6f 72 73 20 66 72 6f 6d 20 6c 65 66 74  rators from left
25072 20 74 6f 20 72 69 67 68 74 20 66 6f 72 20 61 6e   to right for an
25073 20 65 6e 74 69 72 65 20 46 52 4f 4d 0a 2a 2a 20   entire FROM.** 
25074 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 45 78  clause..**.** Ex
25075 61 6d 70 6c 65 3a 20 53 75 70 70 6f 73 65 20 74  ample: Suppose t
25076 68 65 20 6a 6f 69 6e 20 69 73 20 6c 69 6b 65 20  he join is like 
25077 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  this:.**.**     
25078 20 20 20 20 20 20 41 20 6e 61 74 75 72 61 6c 20        A natural 
25079 63 72 6f 73 73 20 6a 6f 69 6e 20 42 0a 2a 2a 0a  cross join B.**.
2507a 2a 2a 20 54 68 65 20 6f 70 65 72 61 74 6f 72 20  ** The operator 
2507b 69 73 20 22 6e 61 74 75 72 61 6c 20 63 72 6f 73  is "natural cros
2507c 73 20 6a 6f 69 6e 22 2e 20 20 54 68 65 20 41 20  s join".  The A 
2507d 61 6e 64 20 42 20 6f 70 65 72 61 6e 64 73 20 61  and B operands a
2507e 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20  re stored.** in 
2507f 70 2d 3e 61 5b 30 5d 20 61 6e 64 20 70 2d 3e 61  p->a[0] and p->a
25080 5b 31 5d 2c 20 72 65 73 70 65 63 74 69 76 65 6c  [1], respectivel
25081 79 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 69  y.  The parser i
25082 6e 69 74 69 61 6c 6c 79 20 73 74 6f 72 65 73 20  nitially stores 
25083 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20  the.** operator 
25084 77 69 74 68 20 41 2e 20 20 54 68 69 73 20 72 6f  with A.  This ro
25085 75 74 69 6e 65 20 73 68 69 66 74 73 20 74 68 61  utine shifts tha
25086 74 20 6f 70 65 72 61 74 6f 72 20 6f 76 65 72 20  t operator over 
25087 74 6f 20 42 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  to B..*/.SQLITE_
25088 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
25089 69 74 65 33 53 72 63 4c 69 73 74 53 68 69 66 74  ite3SrcListShift
2508a 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c 69 73 74  JoinType(SrcList
2508b 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 26 26   *p){.  if( p &&
2508c 20 70 2d 3e 61 20 29 7b 0a 20 20 20 20 69 6e 74   p->a ){.    int
2508d 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 70 2d   i;.    for(i=p-
2508e 3e 6e 53 72 63 2d 31 3b 20 69 3e 30 3b 20 69 2d  >nSrc-1; i>0; i-
2508f 2d 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61 5b 69  -){.      p->a[i
25090 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 70 2d 3e  ].jointype = p->
25091 61 5b 69 2d 31 5d 2e 6a 6f 69 6e 74 79 70 65 3b  a[i-1].jointype;
25092 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 61 5b  .    }.    p->a[
25093 30 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 30 3b  0].jointype = 0;
25094 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 65  .  }.}../*.** Be
25095 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f  gin a transactio
25096 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  n.*/.SQLITE_PRIV
25097 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
25098 42 65 67 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  BeginTransaction
25099 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2509a 69 6e 74 20 74 79 70 65 29 7b 0a 20 20 73 71 6c  int type){.  sql
2509b 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62 65  ite3 *db;.  Vdbe
2509c 20 2a 76 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20   *v;.  int i;.. 
2509d 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 21   assert( pParse!
2509e 3d 30 20 29 3b 0a 20 20 64 62 20 3d 20 70 50 61  =0 );.  db = pPa
2509f 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72  rse->db;.  asser
250a0 74 28 20 64 62 21 3d 30 20 29 3b 0a 2f 2a 20 20  t( db!=0 );./*  
250a1 69 66 28 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70  if( db->aDb[0].p
250a2 42 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 20  Bt==0 ) return; 
250a3 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
250a4 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
250a5 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43  , SQLITE_TRANSAC
250a6 54 49 4f 4e 2c 20 22 42 45 47 49 4e 22 2c 20 30  TION, "BEGIN", 0
250a7 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65 74 75  , 0) ){.    retu
250a8 72 6e 3b 0a 20 20 7d 0a 20 20 76 20 3d 20 73 71  rn;.  }.  v = sq
250a9 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
250aa 72 73 65 29 3b 0a 20 20 69 66 28 20 21 76 20 29  rse);.  if( !v )
250ab 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 74   return;.  if( t
250ac 79 70 65 21 3d 54 4b 5f 44 45 46 45 52 52 45 44  ype!=TK_DEFERRED
250ad 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
250ae 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29   i<db->nDb; i++)
250af 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
250b0 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
250b1 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 69 2c 20  Transaction, i, 
250b2 28 74 79 70 65 3d 3d 54 4b 5f 45 58 43 4c 55 53  (type==TK_EXCLUS
250b3 49 56 45 29 2b 31 29 3b 0a 20 20 20 20 20 20 73  IVE)+1);.      s
250b4 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74  qlite3VdbeUsesBt
250b5 72 65 65 28 76 2c 20 69 29 3b 0a 20 20 20 20 7d  ree(v, i);.    }
250b6 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64  .  }.  sqlite3Vd
250b7 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
250b8 75 74 6f 43 6f 6d 6d 69 74 2c 20 30 2c 20 30 29  utoCommit, 0, 0)
250b9 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69  ;.}../*.** Commi
250ba 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a  t a transaction.
250bb 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
250bc 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
250bd 6d 6d 69 74 54 72 61 6e 73 61 63 74 69 6f 6e 28  mmitTransaction(
250be 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a  Parse *pParse){.
250bf 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20    sqlite3 *db;. 
250c0 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 61 73 73   Vdbe *v;..  ass
250c1 65 72 74 28 20 70 50 61 72 73 65 21 3d 30 20 29  ert( pParse!=0 )
250c2 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ;.  db = pParse-
250c3 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 64  >db;.  assert( d
250c4 62 21 3d 30 20 29 3b 0a 2f 2a 20 20 69 66 28 20  b!=0 );./*  if( 
250c5 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3d 3d  db->aDb[0].pBt==
250c6 30 20 29 20 72 65 74 75 72 6e 3b 20 2a 2f 0a 20  0 ) return; */. 
250c7 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68   if( sqlite3Auth
250c8 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51  Check(pParse, SQ
250c9 4c 49 54 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e  LITE_TRANSACTION
250ca 2c 20 22 43 4f 4d 4d 49 54 22 2c 20 30 2c 20 30  , "COMMIT", 0, 0
250cb 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ) ){.    return;
250cc 0a 20 20 7d 0a 20 20 76 20 3d 20 73 71 6c 69 74  .  }.  v = sqlit
250cd 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
250ce 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20  );.  if( v ){.  
250cf 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
250d0 4f 70 32 28 76 2c 20 4f 50 5f 41 75 74 6f 43 6f  Op2(v, OP_AutoCo
250d1 6d 6d 69 74 2c 20 31 2c 20 30 29 3b 0a 20 20 7d  mmit, 1, 0);.  }
250d2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61  .}../*.** Rollba
250d3 63 6b 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  ck a transaction
250d4 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
250d5 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
250d6 6f 6c 6c 62 61 63 6b 54 72 61 6e 73 61 63 74 69  ollbackTransacti
250d7 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  on(Parse *pParse
250d8 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
250d9 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20  ;.  Vdbe *v;..  
250da 61 73 73 65 72 74 28 20 70 50 61 72 73 65 21 3d  assert( pParse!=
250db 30 20 29 3b 0a 20 20 64 62 20 3d 20 70 50 61 72  0 );.  db = pPar
250dc 73 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74  se->db;.  assert
250dd 28 20 64 62 21 3d 30 20 29 3b 0a 2f 2a 20 20 69  ( db!=0 );./*  i
250de 66 28 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42  f( db->aDb[0].pB
250df 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 20 2a  t==0 ) return; *
250e0 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41  /.  if( sqlite3A
250e1 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
250e2 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43 54   SQLITE_TRANSACT
250e3 49 4f 4e 2c 20 22 52 4f 4c 4c 42 41 43 4b 22 2c  ION, "ROLLBACK",
250e4 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 65   0, 0) ){.    re
250e5 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 76 20 3d 20  turn;.  }.  v = 
250e6 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
250e7 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20  Parse);.  if( v 
250e8 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
250e9 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
250ea 75 74 6f 43 6f 6d 6d 69 74 2c 20 31 2c 20 31 29  utoCommit, 1, 1)
250eb 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
250ec 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
250ed 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61  called by the pa
250ee 72 73 65 72 20 77 68 65 6e 20 69 74 20 70 61 72  rser when it par
250ef 73 65 73 20 61 20 63 6f 6d 6d 61 6e 64 20 74 6f  ses a command to
250f0 20 63 72 65 61 74 65 2c 0a 2a 2a 20 72 65 6c 65   create,.** rele
250f1 61 73 65 20 6f 72 20 72 6f 6c 6c 62 61 63 6b 20  ase or rollback 
250f2 61 6e 20 53 51 4c 20 73 61 76 65 70 6f 69 6e 74  an SQL savepoint
250f3 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  . .*/.SQLITE_PRI
250f4 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
250f5 33 53 61 76 65 70 6f 69 6e 74 28 50 61 72 73 65  3Savepoint(Parse
250f6 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 6f 70   *pParse, int op
250f7 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 29 7b  , Token *pName){
250f8 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d  .  char *zName =
250f9 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d   sqlite3NameFrom
250fa 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e 64 62  Token(pParse->db
250fb 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  , pName);.  if( 
250fc 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 56 64 62  zName ){.    Vdb
250fd 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65  e *v = sqlite3Ge
250fe 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 23  tVdbe(pParse);.#
250ff 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
25100 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
25101 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
25102 74 20 63 68 61 72 20 2a 61 7a 5b 5d 20 3d 20 7b  t char *az[] = {
25103 20 22 42 45 47 49 4e 22 2c 20 22 52 45 4c 45 41   "BEGIN", "RELEA
25104 53 45 22 2c 20 22 52 4f 4c 4c 42 41 43 4b 22 20  SE", "ROLLBACK" 
25105 7d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  };.    assert( !
25106 53 41 56 45 50 4f 49 4e 54 5f 42 45 47 49 4e 20  SAVEPOINT_BEGIN 
25107 26 26 20 53 41 56 45 50 4f 49 4e 54 5f 52 45 4c  && SAVEPOINT_REL
25108 45 41 53 45 3d 3d 31 20 26 26 20 53 41 56 45 50  EASE==1 && SAVEP
25109 4f 49 4e 54 5f 52 4f 4c 4c 42 41 43 4b 3d 3d 32  OINT_ROLLBACK==2
2510a 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69   );.#endif.    i
2510b 66 28 20 21 76 20 7c 7c 20 73 71 6c 69 74 65 33  f( !v || sqlite3
2510c 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
2510d 2c 20 53 51 4c 49 54 45 5f 53 41 56 45 50 4f 49  , SQLITE_SAVEPOI
2510e 4e 54 2c 20 61 7a 5b 6f 70 5d 2c 20 7a 4e 61 6d  NT, az[op], zNam
2510f 65 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 73  e, 0) ){.      s
25110 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 50 61  qlite3DbFree(pPa
25111 72 73 65 2d 3e 64 62 2c 20 7a 4e 61 6d 65 29 3b  rse->db, zName);
25112 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
25113 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
25114 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
25115 5f 53 61 76 65 70 6f 69 6e 74 2c 20 6f 70 2c 20  _Savepoint, op, 
25116 30 2c 20 30 2c 20 7a 4e 61 6d 65 2c 20 50 34 5f  0, 0, zName, P4_
25117 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d 0a  DYNAMIC);.  }.}.
25118 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
25119 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 61   the TEMP databa
2511a 73 65 20 69 73 20 6f 70 65 6e 20 61 6e 64 20 61  se is open and a
2511b 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75 73 65  vailable for use
2511c 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 74 68 65  .  Return.** the
2511d 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   number of error
2511e 73 2e 20 20 4c 65 61 76 65 20 61 6e 79 20 65 72  s.  Leave any er
2511f 72 6f 72 20 6d 65 73 73 61 67 65 73 20 69 6e 20  ror messages in 
25120 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75 63  the pParse struc
25121 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ture..*/.SQLITE_
25122 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
25123 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62  te3OpenTempDatab
25124 61 73 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ase(Parse *pPars
25125 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  e){.  sqlite3 *d
25126 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
25127 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 31 5d    if( db->aDb[1]
25128 2e 70 42 74 3d 3d 30 20 26 26 20 21 70 50 61 72  .pBt==0 && !pPar
25129 73 65 2d 3e 65 78 70 6c 61 69 6e 20 29 7b 0a 20  se->explain ){. 
2512a 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 73     int rc;.    s
2512b 74 61 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20  tatic const int 
2512c 66 6c 61 67 73 20 3d 20 0a 20 20 20 20 20 20 20  flags = .       
2512d 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52     SQLITE_OPEN_R
2512e 45 41 44 57 52 49 54 45 20 7c 0a 20 20 20 20 20  EADWRITE |.     
2512f 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
25130 5f 43 52 45 41 54 45 20 7c 0a 20 20 20 20 20 20  _CREATE |.      
25131 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
25132 45 58 43 4c 55 53 49 56 45 20 7c 0a 20 20 20 20  EXCLUSIVE |.    
25133 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45        SQLITE_OPE
25134 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20  N_DELETEONCLOSE 
25135 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49  |.          SQLI
25136 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 3b  TE_OPEN_TEMP_DB;
25137 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ..    rc = sqlit
25138 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28 64  e3BtreeFactory(d
25139 62 2c 20 30 2c 20 30 2c 20 53 51 4c 49 54 45 5f  b, 0, 0, SQLITE_
2513a 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49  DEFAULT_CACHE_SI
2513b 5a 45 2c 20 66 6c 61 67 73 2c 0a 20 20 20 20 20  ZE, flags,.     
2513c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2513d 20 20 20 20 20 20 20 20 20 20 20 20 26 64 62 2d              &db-
2513e 3e 61 44 62 5b 31 5d 2e 70 42 74 29 3b 0a 20 20  >aDb[1].pBt);.  
2513f 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
25140 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
25141 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
25142 72 73 65 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20  rse, "unable to 
25143 6f 70 65 6e 20 61 20 74 65 6d 70 6f 72 61 72 79  open a temporary
25144 20 64 61 74 61 62 61 73 65 20 22 0a 20 20 20 20   database ".    
25145 20 20 20 20 22 66 69 6c 65 20 66 6f 72 20 73 74      "file for st
25146 6f 72 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20  oring temporary 
25147 74 61 62 6c 65 73 22 29 3b 0a 20 20 20 20 20 20  tables");.      
25148 70 50 61 72 73 65 2d 3e 72 63 20 3d 20 72 63 3b  pParse->rc = rc;
25149 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
2514a 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
2514b 74 28 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20  t( (db->flags & 
2514c 53 51 4c 49 54 45 5f 49 6e 54 72 61 6e 73 29 3d  SQLITE_InTrans)=
2514d 3d 30 20 7c 7c 20 64 62 2d 3e 61 75 74 6f 43 6f  =0 || db->autoCo
2514e 6d 6d 69 74 20 29 3b 0a 20 20 20 20 61 73 73 65  mmit );.    asse
2514f 72 74 28 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70  rt( db->aDb[1].p
25150 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 73 71  Schema );.    sq
25151 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61  lite3PagerJourna
25152 6c 4d 6f 64 65 28 73 71 6c 69 74 65 33 42 74 72  lMode(sqlite3Btr
25153 65 65 50 61 67 65 72 28 64 62 2d 3e 61 44 62 5b  eePager(db->aDb[
25154 31 5d 2e 70 42 74 29 2c 0a 20 20 20 20 20 20 20  1].pBt),.       
25155 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25156 20 20 20 20 20 64 62 2d 3e 64 66 6c 74 4a 6f 75       db->dfltJou
25157 72 6e 61 6c 4d 6f 64 65 29 3b 0a 20 20 7d 0a 20  rnalMode);.  }. 
25158 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
25159 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 56 44 42  .** Generate VDB
2515a 45 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c  E code that will
2515b 20 76 65 72 69 66 79 20 74 68 65 20 73 63 68 65   verify the sche
2515c 6d 61 20 63 6f 6f 6b 69 65 20 61 6e 64 20 73 74  ma cookie and st
2515d 61 72 74 0a 2a 2a 20 61 20 72 65 61 64 2d 74 72  art.** a read-tr
2515e 61 6e 73 61 63 74 69 6f 6e 20 66 6f 72 20 61 6c  ansaction for al
2515f 6c 20 6e 61 6d 65 64 20 64 61 74 61 62 61 73 65  l named database
25160 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 74   files..**.** It
25161 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 68   is important th
25162 61 74 20 61 6c 6c 20 73 63 68 65 6d 61 20 63 6f  at all schema co
25163 6f 6b 69 65 73 20 62 65 20 76 65 72 69 66 69 65  okies be verifie
25164 64 20 61 6e 64 20 61 6c 6c 0a 2a 2a 20 72 65 61  d and all.** rea
25165 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20 62  d transactions b
25166 65 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 65  e started before
25167 20 61 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 68   anything else h
25168 61 70 70 65 6e 73 20 69 6e 0a 2a 2a 20 74 68 65  appens in.** the
25169 20 56 44 42 45 20 70 72 6f 67 72 61 6d 2e 20 20   VDBE program.  
2516a 42 75 74 20 74 68 69 73 20 72 6f 75 74 69 6e 65  But this routine
2516b 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 61   can be called a
2516c 66 74 65 72 20 6d 75 63 68 20 6f 74 68 65 72 0a  fter much other.
2516d 2a 2a 20 63 6f 64 65 20 68 61 73 20 62 65 65 6e  ** code has been
2516e 20 67 65 6e 65 72 61 74 65 64 2e 20 20 53 6f 20   generated.  So 
2516f 68 65 72 65 20 69 73 20 77 68 61 74 20 77 65 20  here is what we 
25170 64 6f 3a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  do:.**.** The fi
25171 72 73 74 20 74 69 6d 65 20 74 68 69 73 20 72 6f  rst time this ro
25172 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c  utine is called,
25173 20 77 65 20 63 6f 64 65 20 61 6e 20 4f 50 5f 47   we code an OP_G
25174 6f 74 6f 20 74 68 61 74 0a 2a 2a 20 77 69 6c 6c  oto that.** will
25175 20 6a 75 6d 70 20 74 6f 20 61 20 73 75 62 72 6f   jump to a subro
25176 75 74 69 6e 65 20 61 74 20 74 68 65 20 65 6e 64  utine at the end
25177 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61 6d 2e   of the program.
25178 20 20 54 68 65 6e 20 77 65 0a 2a 2a 20 72 65 63    Then we.** rec
25179 6f 72 64 20 65 76 65 72 79 20 64 61 74 61 62 61  ord every databa
2517a 73 65 20 74 68 61 74 20 6e 65 65 64 73 20 69 74  se that needs it
2517b 73 20 73 63 68 65 6d 61 20 76 65 72 69 66 69 65  s schema verifie
2517c 64 20 69 6e 20 74 68 65 0a 2a 2a 20 70 50 61 72  d in the.** pPar
2517d 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 66  se->cookieMask f
2517e 69 65 6c 64 2e 20 20 4c 61 74 65 72 2c 20 61 66  ield.  Later, af
2517f 74 65 72 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f  ter all other co
25180 64 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 67  de has been.** g
25181 65 6e 65 72 61 74 65 64 2c 20 74 68 65 20 73 75  enerated, the su
25182 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20 64 6f  broutine that do
25183 65 73 20 74 68 65 20 63 6f 6f 6b 69 65 20 76 65  es the cookie ve
25184 72 69 66 69 63 61 74 69 6f 6e 73 20 61 6e 64 0a  rifications and.
25185 2a 2a 20 73 74 61 72 74 73 20 74 68 65 20 74 72  ** starts the tr
25186 61 6e 73 61 63 74 69 6f 6e 73 20 77 69 6c 6c 20  ansactions will 
25187 62 65 20 63 6f 64 65 64 20 61 6e 64 20 74 68 65  be coded and the
25188 20 4f 50 5f 47 6f 74 6f 20 50 32 20 76 61 6c 75   OP_Goto P2 valu
25189 65 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 6d 61 64  e.** will be mad
2518a 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68  e to point to th
2518b 61 74 20 73 75 62 72 6f 75 74 69 6e 65 2e 20 20  at subroutine.  
2518c 54 68 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f  The generation o
2518d 66 20 74 68 65 0a 2a 2a 20 63 6f 6f 6b 69 65 20  f the.** cookie 
2518e 76 65 72 69 66 69 63 61 74 69 6f 6e 20 73 75 62  verification sub
2518f 72 6f 75 74 69 6e 65 20 63 6f 64 65 20 68 61 70  routine code hap
25190 70 65 6e 73 20 69 6e 20 73 71 6c 69 74 65 33 46  pens in sqlite3F
25191 69 6e 69 73 68 43 6f 64 69 6e 67 28 29 2e 0a 2a  inishCoding()..*
25192 2a 0a 2a 2a 20 49 66 20 69 44 62 3c 30 20 74 68  *.** If iDb<0 th
25193 65 6e 20 63 6f 64 65 20 74 68 65 20 4f 50 5f 47  en code the OP_G
25194 6f 74 6f 20 6f 6e 6c 79 20 2d 20 64 6f 6e 27 74  oto only - don't
25195 20 73 65 74 20 66 6c 61 67 20 74 6f 20 76 65 72   set flag to ver
25196 69 66 79 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d  ify the.** schem
25197 61 20 6f 6e 20 61 6e 79 20 64 61 74 61 62 61 73  a on any databas
25198 65 73 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65  es.  This can be
25199 20 75 73 65 64 20 74 6f 20 70 6f 73 69 74 69 6f   used to positio
2519a 6e 20 74 68 65 20 4f 50 5f 47 6f 74 6f 0a 2a 2a  n the OP_Goto.**
2519b 20 65 61 72 6c 79 20 69 6e 20 74 68 65 20 63 6f   early in the co
2519c 64 65 2c 20 62 65 66 6f 72 65 20 77 65 20 6b 6e  de, before we kn
2519d 6f 77 20 69 66 20 61 6e 79 20 64 61 74 61 62 61  ow if any databa
2519e 73 65 20 74 61 62 6c 65 73 20 77 69 6c 6c 20 62  se tables will b
2519f 65 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  e used..*/.SQLIT
251a0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
251a1 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79  qlite3CodeVerify
251a2 53 63 68 65 6d 61 28 50 61 72 73 65 20 2a 70 50  Schema(Parse *pP
251a3 61 72 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a  arse, int iDb){.
251a4 20 20 50 61 72 73 65 20 2a 70 54 6f 70 6c 65 76    Parse *pToplev
251a5 65 6c 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73  el = sqlite3Pars
251a6 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73 65  eToplevel(pParse
251a7 29 3b 0a 0a 20 20 69 66 28 20 70 54 6f 70 6c 65  );..  if( pTople
251a8 76 65 6c 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 3d  vel->cookieGoto=
251a9 3d 30 20 29 7b 0a 20 20 20 20 56 64 62 65 20 2a  =0 ){.    Vdbe *
251aa 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
251ab 62 65 28 70 54 6f 70 6c 65 76 65 6c 29 3b 0a 20  be(pToplevel);. 
251ac 20 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65     if( v==0 ) re
251ad 74 75 72 6e 3b 20 20 2f 2a 20 54 68 69 73 20 6f  turn;  /* This o
251ae 6e 6c 79 20 68 61 70 70 65 6e 73 20 69 66 20 74  nly happens if t
251af 68 65 72 65 20 77 61 73 20 61 20 70 72 69 6f 72  here was a prior
251b0 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20 70 54   error */.    pT
251b1 6f 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b 69 65 47  oplevel->cookieG
251b2 6f 74 6f 20 3d 20 73 71 6c 69 74 65 33 56 64 62  oto = sqlite3Vdb
251b3 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
251b4 74 6f 2c 20 30 2c 20 30 29 2b 31 3b 0a 20 20 7d  to, 0, 0)+1;.  }
251b5 0a 20 20 69 66 28 20 69 44 62 3e 3d 30 20 29 7b  .  if( iDb>=0 ){
251b6 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
251b7 20 3d 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 64 62   = pToplevel->db
251b8 3b 0a 20 20 20 20 69 6e 74 20 6d 61 73 6b 3b 0a  ;.    int mask;.
251b9 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 44 62  .    assert( iDb
251ba 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 20  <db->nDb );.    
251bb 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44 62 5b  assert( db->aDb[
251bc 69 44 62 5d 2e 70 42 74 21 3d 30 20 7c 7c 20 69  iDb].pBt!=0 || i
251bd 44 62 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73  Db==1 );.    ass
251be 65 72 74 28 20 69 44 62 3c 53 51 4c 49 54 45 5f  ert( iDb<SQLITE_
251bf 4d 41 58 5f 41 54 54 41 43 48 45 44 2b 32 20 29  MAX_ATTACHED+2 )
251c0 3b 0a 20 20 20 20 6d 61 73 6b 20 3d 20 31 3c 3c  ;.    mask = 1<<
251c1 69 44 62 3b 0a 20 20 20 20 69 66 28 20 28 70 54  iDb;.    if( (pT
251c2 6f 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b 69 65 4d  oplevel->cookieM
251c3 61 73 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29  ask & mask)==0 )
251c4 7b 0a 20 20 20 20 20 20 70 54 6f 70 6c 65 76 65  {.      pTopleve
251c5 6c 2d 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 7c 3d  l->cookieMask |=
251c6 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20 70 54 6f   mask;.      pTo
251c7 70 6c 65 76 65 6c 2d 3e 63 6f 6f 6b 69 65 56 61  plevel->cookieVa
251c8 6c 75 65 5b 69 44 62 5d 20 3d 20 64 62 2d 3e 61  lue[iDb] = db->a
251c9 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d  Db[iDb].pSchema-
251ca 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b 0a  >schema_cookie;.
251cb 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49 54 5f        if( !OMIT_
251cc 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31  TEMPDB && iDb==1
251cd 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
251ce 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62  te3OpenTempDatab
251cf 61 73 65 28 70 54 6f 70 6c 65 76 65 6c 29 3b 0a  ase(pToplevel);.
251d0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
251d1 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72  }.}../*.** Gener
251d2 61 74 65 20 56 44 42 45 20 63 6f 64 65 20 74 68  ate VDBE code th
251d3 61 74 20 70 72 65 70 61 72 65 73 20 66 6f 72 20  at prepares for 
251d4 64 6f 69 6e 67 20 61 6e 20 6f 70 65 72 61 74 69  doing an operati
251d5 6f 6e 20 74 68 61 74 0a 2a 2a 20 6d 69 67 68 74  on that.** might
251d6 20 63 68 61 6e 67 65 20 74 68 65 20 64 61 74 61   change the data
251d7 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  base..**.** This
251d8 20 72 6f 75 74 69 6e 65 20 73 74 61 72 74 73 20   routine starts 
251d9 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f  a new transactio
251da 6e 20 69 66 20 77 65 20 61 72 65 20 6e 6f 74 20  n if we are not 
251db 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 0a 2a  already within.*
251dc 2a 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  * a transaction.
251dd 20 20 49 66 20 77 65 20 61 72 65 20 61 6c 72 65    If we are alre
251de 61 64 79 20 77 69 74 68 69 6e 20 61 20 74 72 61  ady within a tra
251df 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 61  nsaction, then a
251e0 20 63 68 65 63 6b 70 6f 69 6e 74 0a 2a 2a 20 69   checkpoint.** i
251e1 73 20 73 65 74 20 69 66 20 74 68 65 20 73 65 74  s set if the set
251e2 53 74 61 74 65 6d 65 6e 74 20 70 61 72 61 6d 65  Statement parame
251e3 74 65 72 20 69 73 20 74 72 75 65 2e 20 20 41 20  ter is true.  A 
251e4 63 68 65 63 6b 70 6f 69 6e 74 20 73 68 6f 75 6c  checkpoint shoul
251e5 64 0a 2a 2a 20 62 65 20 73 65 74 20 66 6f 72 20  d.** be set for 
251e6 6f 70 65 72 61 74 69 6f 6e 73 20 74 68 61 74 20  operations that 
251e7 6d 69 67 68 74 20 66 61 69 6c 20 28 64 75 65 20  might fail (due 
251e8 74 6f 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 29  to a constraint)
251e9 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20   part of.** the 
251ea 77 61 79 20 74 68 72 6f 75 67 68 20 61 6e 64 20  way through and 
251eb 77 68 69 63 68 20 77 69 6c 6c 20 6e 65 65 64 20  which will need 
251ec 74 6f 20 75 6e 64 6f 20 73 6f 6d 65 20 77 72 69  to undo some wri
251ed 74 65 73 20 77 69 74 68 6f 75 74 20 68 61 76 69  tes without havi
251ee 6e 67 20 74 6f 0a 2a 2a 20 72 6f 6c 6c 62 61 63  ng to.** rollbac
251ef 6b 20 74 68 65 20 77 68 6f 6c 65 20 74 72 61 6e  k the whole tran
251f0 73 61 63 74 69 6f 6e 2e 20 20 46 6f 72 20 6f 70  saction.  For op
251f1 65 72 61 74 69 6f 6e 73 20 77 68 65 72 65 20 61  erations where a
251f2 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 2a  ll constraints.*
251f3 2a 20 63 61 6e 20 62 65 20 63 68 65 63 6b 65 64  * can be checked
251f4 20 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61 6e   before any chan
251f5 67 65 73 20 61 72 65 20 6d 61 64 65 20 74 6f 20  ges are made to 
251f6 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 69 74  the database, it
251f7 20 69 73 20 6e 65 76 65 72 0a 2a 2a 20 6e 65 63   is never.** nec
251f8 65 73 73 61 72 79 20 74 6f 20 75 6e 64 6f 20 61  essary to undo a
251f9 20 77 72 69 74 65 20 61 6e 64 20 74 68 65 20 63   write and the c
251fa 68 65 63 6b 70 6f 69 6e 74 20 73 68 6f 75 6c 64  heckpoint should
251fb 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a 2a 2f 0a   not be set..*/.
251fc 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
251fd 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e  oid sqlite3Begin
251fe 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 50  WriteOperation(P
251ff 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
25200 74 20 73 65 74 53 74 61 74 65 6d 65 6e 74 2c 20  t setStatement, 
25201 69 6e 74 20 69 44 62 29 7b 0a 20 20 50 61 72 73  int iDb){.  Pars
25202 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73  e *pToplevel = s
25203 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65  qlite3ParseTople
25204 76 65 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 73  vel(pParse);.  s
25205 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79  qlite3CodeVerify
25206 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69  Schema(pParse, i
25207 44 62 29 3b 0a 20 20 70 54 6f 70 6c 65 76 65 6c  Db);.  pToplevel
25208 2d 3e 77 72 69 74 65 4d 61 73 6b 20 7c 3d 20 31  ->writeMask |= 1
25209 3c 3c 69 44 62 3b 0a 20 20 70 54 6f 70 6c 65 76  <<iDb;.  pToplev
2520a 65 6c 2d 3e 69 73 4d 75 6c 74 69 57 72 69 74 65  el->isMultiWrite
2520b 20 7c 3d 20 73 65 74 53 74 61 74 65 6d 65 6e 74   |= setStatement
2520c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 64 69 63  ;.}../*.** Indic
2520d 61 74 65 20 74 68 61 74 20 74 68 65 20 73 74 61  ate that the sta
2520e 74 65 6d 65 6e 74 20 63 75 72 72 65 6e 74 6c 79  tement currently
2520f 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74   under construct
25210 69 6f 6e 20 6d 69 67 68 74 20 77 72 69 74 65 0a  ion might write.
25211 2a 2a 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65  ** more than one
25212 20 65 6e 74 72 79 20 28 65 78 61 6d 70 6c 65 3a   entry (example:
25213 20 64 65 6c 65 74 69 6e 67 20 6f 6e 65 20 72 6f   deleting one ro
25214 77 20 74 68 65 6e 20 69 6e 73 65 72 74 69 6e 67  w then inserting
25215 20 61 6e 6f 74 68 65 72 2c 0a 2a 2a 20 69 6e 73   another,.** ins
25216 65 72 74 69 6e 67 20 6d 75 6c 74 69 70 6c 65 20  erting multiple 
25217 72 6f 77 73 20 69 6e 20 61 20 74 61 62 6c 65 2c  rows in a table,
25218 20 6f 72 20 69 6e 73 65 72 74 69 6e 67 20 61 20   or inserting a 
25219 72 6f 77 20 61 6e 64 20 69 6e 64 65 78 20 65 6e  row and index en
2521a 74 72 69 65 73 2e 29 0a 2a 2a 20 49 66 20 61 6e  tries.).** If an
2521b 20 61 62 6f 72 74 20 6f 63 63 75 72 73 20 61 66   abort occurs af
2521c 74 65 72 20 73 6f 6d 65 20 6f 66 20 74 68 65 73  ter some of thes
2521d 65 20 77 72 69 74 65 73 20 68 61 76 65 20 63 6f  e writes have co
2521e 6d 70 6c 65 74 65 64 2c 20 74 68 65 6e 20 69 74  mpleted, then it
2521f 20 77 69 6c 6c 0a 2a 2a 20 62 65 20 6e 65 63 65   will.** be nece
25220 73 73 61 72 79 20 74 6f 20 75 6e 64 6f 20 74 68  ssary to undo th
25221 65 20 63 6f 6d 70 6c 65 74 65 64 20 77 72 69 74  e completed writ
25222 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  es..*/.SQLITE_PR
25223 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
25224 65 33 4d 75 6c 74 69 57 72 69 74 65 28 50 61 72  e3MultiWrite(Par
25225 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 50  se *pParse){.  P
25226 61 72 73 65 20 2a 70 54 6f 70 6c 65 76 65 6c 20  arse *pToplevel 
25227 3d 20 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f  = sqlite3ParseTo
25228 70 6c 65 76 65 6c 28 70 50 61 72 73 65 29 3b 0a  plevel(pParse);.
25229 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 69 73 4d    pToplevel->isM
2522a 75 6c 74 69 57 72 69 74 65 20 3d 20 31 3b 0a 7d  ultiWrite = 1;.}
2522b 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 65 20 63 6f 64  ../* .** The cod
2522c 65 20 67 65 6e 65 72 61 74 6f 72 20 63 61 6c 6c  e generator call
2522d 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  s this routine i
2522e 66 20 69 73 20 64 69 73 63 6f 76 65 72 73 20 74  f is discovers t
2522f 68 61 74 20 69 74 20 69 73 0a 2a 2a 20 70 6f 73  hat it is.** pos
25230 73 69 62 6c 65 20 74 6f 20 61 62 6f 72 74 20 61  sible to abort a
25231 20 73 74 61 74 65 6d 65 6e 74 20 70 72 69 6f 72   statement prior
25232 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 20   to completion. 
25233 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 0a 2a 2a   In order to .**
25234 20 70 65 72 66 6f 72 6d 20 74 68 69 73 20 61 62   perform this ab
25235 6f 72 74 20 77 69 74 68 6f 75 74 20 63 6f 72 72  ort without corr
25236 75 70 74 69 6e 67 20 74 68 65 20 64 61 74 61 62  upting the datab
25237 61 73 65 2c 20 77 65 20 6e 65 65 64 20 74 6f 20  ase, we need to 
25238 6d 61 6b 65 0a 2a 2a 20 73 75 72 65 20 74 68 61  make.** sure tha
25239 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  t the statement 
2523a 69 73 20 70 72 6f 74 65 63 74 65 64 20 62 79 20  is protected by 
2523b 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e  a statement tran
2523c 73 61 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  saction..**.** T
2523d 65 63 68 6e 69 63 61 6c 6c 79 2c 20 77 65 20 6f  echnically, we o
2523e 6e 6c 79 20 6e 65 65 64 20 74 6f 20 73 65 74 20  nly need to set 
2523f 74 68 65 20 6d 61 79 41 62 6f 72 74 20 66 6c 61  the mayAbort fla
25240 67 20 69 66 20 74 68 65 0a 2a 2a 20 69 73 4d 75  g if the.** isMu
25241 6c 74 69 57 72 69 74 65 20 66 6c 61 67 20 77 61  ltiWrite flag wa
25242 73 20 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74  s previously set
25243 2e 20 20 54 68 65 72 65 20 69 73 20 61 20 74 69  .  There is a ti
25244 6d 65 20 64 65 70 65 6e 64 65 6e 63 79 0a 2a 2a  me dependency.**
25245 20 73 75 63 68 20 74 68 61 74 20 74 68 65 20 61   such that the a
25246 62 6f 72 74 20 6d 75 73 74 20 6f 63 63 75 72 20  bort must occur 
25247 61 66 74 65 72 20 74 68 65 20 6d 75 6c 74 69 77  after the multiw
25248 72 69 74 65 2e 20 20 54 68 69 73 20 6d 61 6b 65  rite.  This make
25249 73 0a 2a 2a 20 73 6f 6d 65 20 73 74 61 74 65 6d  s.** some statem
2524a 65 6e 74 73 20 69 6e 76 6f 6c 76 69 6e 67 20 74  ents involving t
2524b 68 65 20 52 45 50 4c 41 43 45 20 63 6f 6e 66 6c  he REPLACE confl
2524c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61  ict resolution a
2524d 6c 67 6f 72 69 74 68 6d 0a 2a 2a 20 67 6f 20 61  lgorithm.** go a
2524e 20 6c 69 74 74 6c 65 20 66 61 73 74 65 72 2e 20   little faster. 
2524f 20 42 75 74 20 74 61 6b 69 6e 67 20 61 64 76 61   But taking adva
25250 6e 74 61 67 65 20 6f 66 20 74 68 69 73 20 74 69  ntage of this ti
25251 6d 65 20 64 65 70 65 6e 64 65 6e 63 79 0a 2a 2a  me dependency.**
25252 20 6d 61 6b 65 73 20 69 74 20 6d 6f 72 65 20 64   makes it more d
25253 69 66 66 69 63 75 6c 74 20 74 6f 20 70 72 6f 76  ifficult to prov
25254 65 20 74 68 61 74 20 74 68 65 20 63 6f 64 65 20  e that the code 
25255 69 73 20 63 6f 72 72 65 63 74 20 28 69 6e 20 0a  is correct (in .
25256 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 2c 20 69  ** particular, i
25257 74 20 70 72 65 76 65 6e 74 73 20 75 73 20 66 72  t prevents us fr
25258 6f 6d 20 77 72 69 74 69 6e 67 20 61 6e 20 65 66  om writing an ef
25259 66 65 63 74 69 76 65 0a 2a 2a 20 69 6d 70 6c 65  fective.** imple
2525a 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73 71 6c  mentation of sql
2525b 69 74 65 33 41 73 73 65 72 74 4d 61 79 41 62 6f  ite3AssertMayAbo
2525c 72 74 28 29 29 20 61 6e 64 20 73 6f 20 77 65 20  rt()) and so we 
2525d 68 61 76 65 20 63 68 6f 73 65 6e 0a 2a 2a 20 74  have chosen.** t
2525e 6f 20 74 61 6b 65 20 74 68 65 20 73 61 66 65 20  o take the safe 
2525f 72 6f 75 74 65 20 61 6e 64 20 73 6b 69 70 20 74  route and skip t
25260 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e  he optimization.
25261 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
25262 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  TE void sqlite3M
25263 61 79 41 62 6f 72 74 28 50 61 72 73 65 20 2a 70  ayAbort(Parse *p
25264 50 61 72 73 65 29 7b 0a 20 20 50 61 72 73 65 20  Parse){.  Parse 
25265 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c  *pToplevel = sql
25266 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65  ite3ParseTopleve
25267 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 70 54 6f  l(pParse);.  pTo
25268 70 6c 65 76 65 6c 2d 3e 6d 61 79 41 62 6f 72 74  plevel->mayAbort
25269 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43   = 1;.}../*.** C
2526a 6f 64 65 20 61 6e 20 4f 50 5f 48 61 6c 74 20 74  ode an OP_Halt t
2526b 68 61 74 20 63 61 75 73 65 73 20 74 68 65 20 76  hat causes the v
2526c 64 62 65 20 74 6f 20 72 65 74 75 72 6e 20 61 6e  dbe to return an
2526d 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49   SQLITE_CONSTRAI
2526e 4e 54 0a 2a 2a 20 65 72 72 6f 72 2e 20 54 68 65  NT.** error. The
2526f 20 6f 6e 45 72 72 6f 72 20 70 61 72 61 6d 65 74   onError paramet
25270 65 72 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68  er determines wh
25271 69 63 68 20 28 69 66 20 61 6e 79 29 20 6f 66 20  ich (if any) of 
25272 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  the statement.**
25273 20 61 6e 64 2f 6f 72 20 63 75 72 72 65 6e 74 20   and/or current 
25274 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72  transaction is r
25275 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2f 0a 53  olled back..*/.S
25276 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
25277 69 64 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f  id sqlite3HaltCo
25278 6e 73 74 72 61 69 6e 74 28 50 61 72 73 65 20 2a  nstraint(Parse *
25279 70 50 61 72 73 65 2c 20 69 6e 74 20 6f 6e 45 72  pParse, int onEr
2527a 72 6f 72 2c 20 63 68 61 72 20 2a 70 34 2c 20 69  ror, char *p4, i
2527b 6e 74 20 70 34 74 79 70 65 29 7b 0a 20 20 56 64  nt p4type){.  Vd
2527c 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47  be *v = sqlite3G
2527d 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
2527e 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f    if( onError==O
2527f 45 5f 41 62 6f 72 74 20 29 7b 0a 20 20 20 20 73  E_Abort ){.    s
25280 71 6c 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70  qlite3MayAbort(p
25281 50 61 72 73 65 29 3b 0a 20 20 7d 0a 20 20 73 71  Parse);.  }.  sq
25282 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
25283 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49  v, OP_Halt, SQLI
25284 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 6f  TE_CONSTRAINT, o
25285 6e 45 72 72 6f 72 2c 20 30 2c 20 70 34 2c 20 70  nError, 0, p4, p
25286 34 74 79 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  4type);.}../*.**
25287 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
25288 20 70 49 6e 64 65 78 20 75 73 65 73 20 74 68 65   pIndex uses the
25289 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
2528a 6e 63 65 20 70 43 6f 6c 6c 2e 20 20 52 65 74 75  nce pColl.  Retu
2528b 72 6e 0a 2a 2a 20 74 72 75 65 20 69 66 20 69 74  rn.** true if it
2528c 20 64 6f 65 73 20 61 6e 64 20 66 61 6c 73 65 20   does and false 
2528d 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e 0a  if it does not..
2528e 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
2528f 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 73  E_OMIT_REINDEX.s
25290 74 61 74 69 63 20 69 6e 74 20 63 6f 6c 6c 61 74  tatic int collat
25291 69 6f 6e 4d 61 74 63 68 28 63 6f 6e 73 74 20 63  ionMatch(const c
25292 68 61 72 20 2a 7a 43 6f 6c 6c 2c 20 49 6e 64 65  har *zColl, Inde
25293 78 20 2a 70 49 6e 64 65 78 29 7b 0a 20 20 69 6e  x *pIndex){.  in
25294 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 7a  t i;.  assert( z
25295 43 6f 6c 6c 21 3d 30 20 29 3b 0a 20 20 66 6f 72  Coll!=0 );.  for
25296 28 69 3d 30 3b 20 69 3c 70 49 6e 64 65 78 2d 3e  (i=0; i<pIndex->
25297 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
25298 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
25299 20 3d 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c   = pIndex->azCol
2529a 6c 5b 69 5d 3b 0a 20 20 20 20 61 73 73 65 72 74  l[i];.    assert
2529b 28 20 7a 21 3d 30 20 29 3b 0a 20 20 20 20 69 66  ( z!=0 );.    if
2529c 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49  ( 0==sqlite3StrI
2529d 43 6d 70 28 7a 2c 20 7a 43 6f 6c 6c 29 20 29 7b  Cmp(z, zColl) ){
2529e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
2529f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
252a0 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a  urn 0;.}.#endif.
252a1 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d 70 75 74 65  ./*.** Recompute
252a2 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20   all indices of 
252a3 70 54 61 62 20 74 68 61 74 20 75 73 65 20 74 68  pTab that use th
252a4 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  e collating sequ
252a5 65 6e 63 65 20 70 43 6f 6c 6c 2e 0a 2a 2a 20 49  ence pColl..** I
252a6 66 20 70 43 6f 6c 6c 3d 3d 30 20 74 68 65 6e 20  f pColl==0 then 
252a7 72 65 63 6f 6d 70 75 74 65 20 61 6c 6c 20 69 6e  recompute all in
252a8 64 69 63 65 73 20 6f 66 20 70 54 61 62 2e 0a 2a  dices of pTab..*
252a9 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
252aa 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a 73 74  _OMIT_REINDEX.st
252ab 61 74 69 63 20 76 6f 69 64 20 72 65 69 6e 64 65  atic void reinde
252ac 78 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50  xTable(Parse *pP
252ad 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61  arse, Table *pTa
252ae 62 2c 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  b, char const *z
252af 43 6f 6c 6c 29 7b 0a 20 20 49 6e 64 65 78 20 2a  Coll){.  Index *
252b0 70 49 6e 64 65 78 3b 20 20 20 20 20 20 20 20 20  pIndex;         
252b1 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e 64 65 78       /* An index
252b2 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
252b3 20 70 54 61 62 20 2a 2f 0a 0a 20 20 66 6f 72 28   pTab */..  for(
252b4 70 49 6e 64 65 78 3d 70 54 61 62 2d 3e 70 49 6e  pIndex=pTab->pIn
252b5 64 65 78 3b 20 70 49 6e 64 65 78 3b 20 70 49 6e  dex; pIndex; pIn
252b6 64 65 78 3d 70 49 6e 64 65 78 2d 3e 70 4e 65 78  dex=pIndex->pNex
252b7 74 29 7b 0a 20 20 20 20 69 66 28 20 7a 43 6f 6c  t){.    if( zCol
252b8 6c 3d 3d 30 20 7c 7c 20 63 6f 6c 6c 61 74 69 6f  l==0 || collatio
252b9 6e 4d 61 74 63 68 28 7a 43 6f 6c 6c 2c 20 70 49  nMatch(zColl, pI
252ba 6e 64 65 78 29 20 29 7b 0a 20 20 20 20 20 20 69  ndex) ){.      i
252bb 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33  nt iDb = sqlite3
252bc 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50  SchemaToIndex(pP
252bd 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e  arse->db, pTab->
252be 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 20 20  pSchema);.      
252bf 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
252c0 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73  eOperation(pPars
252c1 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20  e, 0, iDb);.    
252c2 20 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c 49    sqlite3RefillI
252c3 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 49 6e  ndex(pParse, pIn
252c4 64 65 78 2c 20 2d 31 29 3b 0a 20 20 20 20 7d 0a  dex, -1);.    }.
252c5 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a    }.}.#endif../*
252c6 0a 2a 2a 20 52 65 63 6f 6d 70 75 74 65 20 61 6c  .** Recompute al
252c7 6c 20 69 6e 64 69 63 65 73 20 6f 66 20 61 6c 6c  l indices of all
252c8 20 74 61 62 6c 65 73 20 69 6e 20 61 6c 6c 20 64   tables in all d
252c9 61 74 61 62 61 73 65 73 20 77 68 65 72 65 20 74  atabases where t
252ca 68 65 0a 2a 2a 20 69 6e 64 69 63 65 73 20 75 73  he.** indices us
252cb 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20  e the collating 
252cc 73 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 2e 20  sequence pColl. 
252cd 20 49 66 20 70 43 6f 6c 6c 3d 3d 30 20 74 68 65   If pColl==0 the
252ce 6e 20 72 65 63 6f 6d 70 75 74 65 0a 2a 2a 20 61  n recompute.** a
252cf 6c 6c 20 69 6e 64 69 63 65 73 20 65 76 65 72 79  ll indices every
252d0 77 68 65 72 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65  where..*/.#ifnde
252d1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 52 45  f SQLITE_OMIT_RE
252d2 49 4e 44 45 58 0a 73 74 61 74 69 63 20 76 6f 69  INDEX.static voi
252d3 64 20 72 65 69 6e 64 65 78 44 61 74 61 62 61 73  d reindexDatabas
252d4 65 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  es(Parse *pParse
252d5 2c 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 43  , char const *zC
252d6 6f 6c 6c 29 7b 0a 20 20 44 62 20 2a 70 44 62 3b  oll){.  Db *pDb;
252d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
252d8 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20      /* A single 
252d9 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e  database */.  in
252da 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
252db 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
252dc 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78 20   database index 
252dd 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73 71 6c 69  number */.  sqli
252de 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
252df 2d 3e 64 62 3b 20 20 20 2f 2a 20 54 68 65 20 64  ->db;   /* The d
252e0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
252e1 6f 6e 20 2a 2f 0a 20 20 48 61 73 68 45 6c 65 6d  on */.  HashElem
252e2 20 2a 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20   *k;            
252e3 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69      /* For loopi
252e4 6e 67 20 6f 76 65 72 20 74 61 62 6c 65 73 20 69  ng over tables i
252e5 6e 20 70 44 62 20 2a 2f 0a 20 20 54 61 62 6c 65  n pDb */.  Table
252e6 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20   *pTab;         
252e7 20 20 20 20 20 20 20 2f 2a 20 41 20 74 61 62 6c         /* A tabl
252e8 65 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  e in the databas
252e9 65 20 2a 2f 0a 0a 20 20 66 6f 72 28 69 44 62 3d  e */..  for(iDb=
252ea 30 2c 20 70 44 62 3d 64 62 2d 3e 61 44 62 3b 20  0, pDb=db->aDb; 
252eb 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 69 44 62  iDb<db->nDb; iDb
252ec 2b 2b 2c 20 70 44 62 2b 2b 29 7b 0a 20 20 20 20  ++, pDb++){.    
252ed 61 73 73 65 72 74 28 20 70 44 62 21 3d 30 20 29  assert( pDb!=0 )
252ee 3b 0a 20 20 20 20 66 6f 72 28 6b 3d 73 71 6c 69  ;.    for(k=sqli
252ef 74 65 48 61 73 68 46 69 72 73 74 28 26 70 44 62  teHashFirst(&pDb
252f0 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61  ->pSchema->tblHa
252f1 73 68 29 3b 20 20 6b 3b 20 6b 3d 73 71 6c 69 74  sh);  k; k=sqlit
252f2 65 48 61 73 68 4e 65 78 74 28 6b 29 29 7b 0a 20  eHashNext(k)){. 
252f3 20 20 20 20 20 70 54 61 62 20 3d 20 28 54 61 62       pTab = (Tab
252f4 6c 65 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61  le*)sqliteHashDa
252f5 74 61 28 6b 29 3b 0a 20 20 20 20 20 20 72 65 69  ta(k);.      rei
252f6 6e 64 65 78 54 61 62 6c 65 28 70 50 61 72 73 65  ndexTable(pParse
252f7 2c 20 70 54 61 62 2c 20 7a 43 6f 6c 6c 29 3b 0a  , pTab, zColl);.
252f8 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64      }.  }.}.#end
252f9 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  if../*.** Genera
252fa 74 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20  te code for the 
252fb 52 45 49 4e 44 45 58 20 63 6f 6d 6d 61 6e 64 2e  REINDEX command.
252fc 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 52 45  .**.**        RE
252fd 49 4e 44 45 58 20 20 20 20 20 20 20 20 20 20 20  INDEX           
252fe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
252ff 20 2d 2d 20 31 0a 2a 2a 20 20 20 20 20 20 20 20   -- 1.**        
25300 52 45 49 4e 44 45 58 20 20 3c 63 6f 6c 6c 61 74  REINDEX  <collat
25301 69 6f 6e 3e 20 20 20 20 20 20 20 20 20 20 20 20  ion>            
25302 20 20 20 2d 2d 20 32 0a 2a 2a 20 20 20 20 20 20     -- 2.**      
25303 20 20 52 45 49 4e 44 45 58 20 20 3f 3c 64 61 74    REINDEX  ?<dat
25304 61 62 61 73 65 3e 2e 3f 3c 74 61 62 6c 65 6e 61  abase>.?<tablena
25305 6d 65 3e 20 20 2d 2d 20 33 0a 2a 2a 20 20 20 20  me>  -- 3.**    
25306 20 20 20 20 52 45 49 4e 44 45 58 20 20 3f 3c 64      REINDEX  ?<d
25307 61 74 61 62 61 73 65 3e 2e 3f 3c 69 6e 64 65 78  atabase>.?<index
25308 6e 61 6d 65 3e 20 20 2d 2d 20 34 0a 2a 2a 0a 2a  name>  -- 4.**.*
25309 2a 20 46 6f 72 6d 20 31 20 63 61 75 73 65 73 20  * Form 1 causes 
2530a 61 6c 6c 20 69 6e 64 69 63 65 73 20 69 6e 20 61  all indices in a
2530b 6c 6c 20 61 74 74 61 63 68 65 64 20 64 61 74 61  ll attached data
2530c 62 61 73 65 73 20 74 6f 20 62 65 20 72 65 62 75  bases to be rebu
2530d 69 6c 74 2e 0a 2a 2a 20 46 6f 72 6d 20 32 20 72  ilt..** Form 2 r
2530e 65 62 75 69 6c 64 73 20 61 6c 6c 20 69 6e 64 69  ebuilds all indi
2530f 63 65 73 20 69 6e 20 61 6c 6c 20 64 61 74 61 62  ces in all datab
25310 61 73 65 73 20 74 68 61 74 20 75 73 65 20 74 68  ases that use th
25311 65 20 6e 61 6d 65 64 0a 2a 2a 20 63 6f 6c 6c 61  e named.** colla
25312 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 2e 20 20  ting function.  
25313 46 6f 72 6d 73 20 33 20 61 6e 64 20 34 20 72 65  Forms 3 and 4 re
25314 62 75 69 6c 64 20 74 68 65 20 6e 61 6d 65 64 20  build the named 
25315 69 6e 64 65 78 20 6f 72 20 61 6c 6c 0a 2a 2a 20  index or all.** 
25316 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61 74  indices associat
25317 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65  ed with the name
25318 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23 69 66 6e  d table..*/.#ifn
25319 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2531a 52 45 49 4e 44 45 58 0a 53 51 4c 49 54 45 5f 50  REINDEX.SQLITE_P
2531b 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2531c 74 65 33 52 65 69 6e 64 65 78 28 50 61 72 73 65  te3Reindex(Parse
2531d 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20   *pParse, Token 
2531e 2a 70 4e 61 6d 65 31 2c 20 54 6f 6b 65 6e 20 2a  *pName1, Token *
2531f 70 4e 61 6d 65 32 29 7b 0a 20 20 43 6f 6c 6c 53  pName2){.  CollS
25320 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20 20 20 20  eq *pColl;      
25321 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74         /* Collat
25322 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74 6f 20  ing sequence to 
25323 62 65 20 72 65 69 6e 64 65 78 65 64 2c 20 6f 72  be reindexed, or
25324 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68 61 72 20   NULL */.  char 
25325 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *z;             
25326 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
25327 66 20 61 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  f a table or ind
25328 65 78 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ex */.  const ch
25329 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 20  ar *zDb;        
2532a 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
2532b 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  he database */. 
2532c 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20   Table *pTab;   
2532d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2532e 41 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 64  A table in the d
2532f 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 49 6e 64  atabase */.  Ind
25330 65 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 20 20  ex *pIndex;     
25331 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 69           /* An i
25332 6e 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 20  ndex associated 
25333 77 69 74 68 20 70 54 61 62 20 2a 2f 0a 20 20 69  with pTab */.  i
25334 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
25335 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
25336 65 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78  e database index
25337 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73 71 6c   number */.  sql
25338 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
25339 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 54 68 65 20  e->db;   /* The 
2533a 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
2533b 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ion */.  Token *
2533c 70 4f 62 6a 4e 61 6d 65 3b 20 20 20 20 20 20 20  pObjName;       
2533d 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
2533e 74 68 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64  the table or ind
2533f 65 78 20 74 6f 20 62 65 20 72 65 69 6e 64 65 78  ex to be reindex
25340 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 61 64  ed */..  /* Read
25341 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
25342 68 65 6d 61 2e 20 49 66 20 61 6e 20 65 72 72 6f  hema. If an erro
25343 72 20 6f 63 63 75 72 73 2c 20 6c 65 61 76 65 20  r occurs, leave 
25344 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
25345 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64 65 20 69  .  ** and code i
25346 6e 20 70 50 61 72 73 65 20 61 6e 64 20 72 65 74  n pParse and ret
25347 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69  urn NULL. */.  i
25348 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
25349 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
2534a 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 72  pParse) ){.    r
2534b 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66  eturn;.  }..  if
2534c 28 20 70 4e 61 6d 65 31 3d 3d 30 20 29 7b 0a 20  ( pName1==0 ){. 
2534d 20 20 20 72 65 69 6e 64 65 78 44 61 74 61 62 61     reindexDataba
2534e 73 65 73 28 70 50 61 72 73 65 2c 20 30 29 3b 0a  ses(pParse, 0);.
2534f 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65      return;.  }e
25350 6c 73 65 20 69 66 28 20 4e 45 56 45 52 28 70 4e  lse if( NEVER(pN
25351 61 6d 65 32 3d 3d 30 29 20 7c 7c 20 70 4e 61 6d  ame2==0) || pNam
25352 65 32 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20 20  e2->z==0 ){.    
25353 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 0a 20 20 20  char *zColl;.   
25354 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 31 2d   assert( pName1-
25355 3e 7a 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 6c 20  >z );.    zColl 
25356 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
25357 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65 2d 3e 64  mToken(pParse->d
25358 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20  b, pName1);.    
25359 69 66 28 20 21 7a 43 6f 6c 6c 20 29 20 72 65 74  if( !zColl ) ret
2535a 75 72 6e 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d  urn;.    pColl =
2535b 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
2535c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29 2c  Seq(db, ENC(db),
2535d 20 7a 43 6f 6c 6c 2c 20 30 29 3b 0a 20 20 20 20   zColl, 0);.    
2535e 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  if( pColl ){.   
2535f 20 20 20 72 65 69 6e 64 65 78 44 61 74 61 62 61     reindexDataba
25360 73 65 73 28 70 50 61 72 73 65 2c 20 7a 43 6f 6c  ses(pParse, zCol
25361 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  l);.      sqlite
25362 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f 6c  3DbFree(db, zCol
25363 6c 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  l);.      return
25364 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
25365 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43  te3DbFree(db, zC
25366 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 20 69 44 62 20  oll);.  }.  iDb 
25367 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74  = sqlite3TwoPart
25368 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61  Name(pParse, pNa
25369 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4f  me1, pName2, &pO
2536a 62 6a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 69  bjName);.  if( i
2536b 44 62 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  Db<0 ) return;. 
2536c 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65   z = sqlite3Name
2536d 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4f  FromToken(db, pO
2536e 62 6a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a  bjName);.  if( z
2536f 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
25370 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44  zDb = db->aDb[iD
25371 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 70 54 61 62  b].zName;.  pTab
25372 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61   = sqlite3FindTa
25373 62 6c 65 28 64 62 2c 20 7a 2c 20 7a 44 62 29 3b  ble(db, z, zDb);
25374 0a 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20  .  if( pTab ){. 
25375 20 20 20 72 65 69 6e 64 65 78 54 61 62 6c 65 28     reindexTable(
25376 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30 29  pParse, pTab, 0)
25377 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
25378 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20  ree(db, z);.    
25379 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 49  return;.  }.  pI
2537a 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 46 69  ndex = sqlite3Fi
2537b 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 2c 20 7a  ndIndex(db, z, z
2537c 44 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  Db);.  sqlite3Db
2537d 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 69  Free(db, z);.  i
2537e 66 28 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20  f( pIndex ){.   
2537f 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69   sqlite3BeginWri
25380 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72  teOperation(pPar
25381 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20  se, 0, iDb);.   
25382 20 73 71 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e   sqlite3RefillIn
25383 64 65 78 28 70 50 61 72 73 65 2c 20 70 49 6e 64  dex(pParse, pInd
25384 65 78 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74  ex, -1);.    ret
25385 75 72 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  urn;.  }.  sqlit
25386 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
25387 65 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20 69 64  e, "unable to id
25388 65 6e 74 69 66 79 20 74 68 65 20 6f 62 6a 65 63  entify the objec
25389 74 20 74 6f 20 62 65 20 72 65 69 6e 64 65 78 65  t to be reindexe
2538a 64 22 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  d");.}.#endif../
2538b 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 64 79  *.** Return a dy
2538c 6e 61 6d 69 63 6c 79 20 61 6c 6c 6f 63 61 74 65  namicly allocate
2538d 64 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74  d KeyInfo struct
2538e 75 72 65 20 74 68 61 74 20 63 61 6e 20 62 65 20  ure that can be 
2538f 75 73 65 64 0a 2a 2a 20 77 69 74 68 20 4f 50 5f  used.** with OP_
25390 4f 70 65 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f  OpenRead or OP_O
25391 70 65 6e 57 72 69 74 65 20 74 6f 20 61 63 63 65  penWrite to acce
25392 73 73 20 64 61 74 61 62 61 73 65 20 69 6e 64 65  ss database inde
25393 78 20 70 49 64 78 2e 0a 2a 2a 0a 2a 2a 20 49 66  x pIdx..**.** If
25394 20 73 75 63 63 65 73 73 66 75 6c 2c 20 61 20 70   successful, a p
25395 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65  ointer to the ne
25396 77 20 73 74 72 75 63 74 75 72 65 20 69 73 20 72  w structure is r
25397 65 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73  eturned. In this
25398 20 63 61 73 65 0a 2a 2a 20 74 68 65 20 63 61 6c   case.** the cal
25399 6c 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62  ler is responsib
2539a 6c 65 20 66 6f 72 20 63 61 6c 6c 69 6e 67 20 73  le for calling s
2539b 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2539c 20 29 20 6f 6e 20 74 68 65 20 72 65 74 75 72 6e   ) on the return
2539d 65 64 20 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20  ed .** pointer. 
2539e 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
2539f 72 73 20 28 6f 75 74 20 6f 66 20 6d 65 6d 6f 72  rs (out of memor
253a0 79 20 6f 72 20 6d 69 73 73 69 6e 67 20 63 6f 6c  y or missing col
253a1 6c 61 74 69 6f 6e 20 0a 2a 2a 20 73 65 71 75 65  lation .** seque
253a2 6e 63 65 29 2c 20 4e 55 4c 4c 20 69 73 20 72 65  nce), NULL is re
253a3 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 73  turned and the s
253a4 74 61 74 65 20 6f 66 20 70 50 61 72 73 65 20 75  tate of pParse u
253a5 70 64 61 74 65 64 20 74 6f 20 72 65 66 6c 65 63  pdated to reflec
253a6 74 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 2e 0a  t.** the error..
253a7 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
253a8 45 20 4b 65 79 49 6e 66 6f 20 2a 73 71 6c 69 74  E KeyInfo *sqlit
253a9 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 50  e3IndexKeyinfo(P
253aa 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 49 6e  arse *pParse, In
253ab 64 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 69 6e  dex *pIdx){.  in
253ac 74 20 69 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 20  t i;.  int nCol 
253ad 3d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  = pIdx->nColumn;
253ae 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 20 3d 20  .  int nBytes = 
253af 73 69 7a 65 6f 66 28 4b 65 79 49 6e 66 6f 29 20  sizeof(KeyInfo) 
253b0 2b 20 28 6e 43 6f 6c 2d 31 29 2a 73 69 7a 65 6f  + (nCol-1)*sizeo
253b1 66 28 43 6f 6c 6c 53 65 71 2a 29 20 2b 20 6e 43  f(CollSeq*) + nC
253b2 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ol;.  sqlite3 *d
253b3 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
253b4 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20    KeyInfo *pKey 
253b5 3d 20 28 4b 65 79 49 6e 66 6f 20 2a 29 73 71 6c  = (KeyInfo *)sql
253b6 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
253b7 28 64 62 2c 20 6e 42 79 74 65 73 29 3b 0a 0a 20  (db, nBytes);.. 
253b8 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20   if( pKey ){.   
253b9 20 70 4b 65 79 2d 3e 64 62 20 3d 20 70 50 61 72   pKey->db = pPar
253ba 73 65 2d 3e 64 62 3b 0a 20 20 20 20 70 4b 65 79  se->db;.    pKey
253bb 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28  ->aSortOrder = (
253bc 75 38 20 2a 29 26 28 70 4b 65 79 2d 3e 61 43 6f  u8 *)&(pKey->aCo
253bd 6c 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 20 20 61  ll[nCol]);.    a
253be 73 73 65 72 74 28 20 26 70 4b 65 79 2d 3e 61 53  ssert( &pKey->aS
253bf 6f 72 74 4f 72 64 65 72 5b 6e 43 6f 6c 5d 3d 3d  ortOrder[nCol]==
253c0 26 28 28 28 75 38 20 2a 29 70 4b 65 79 29 5b 6e  &(((u8 *)pKey)[n
253c1 42 79 74 65 73 5d 29 20 29 3b 0a 20 20 20 20 66  Bytes]) );.    f
253c2 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20  or(i=0; i<nCol; 
253c3 69 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72  i++){.      char
253c4 20 2a 7a 43 6f 6c 6c 20 3d 20 70 49 64 78 2d 3e   *zColl = pIdx->
253c5 61 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20  azColl[i];.     
253c6 20 61 73 73 65 72 74 28 20 7a 43 6f 6c 6c 20 29   assert( zColl )
253c7 3b 0a 20 20 20 20 20 20 70 4b 65 79 2d 3e 61 43  ;.      pKey->aC
253c8 6f 6c 6c 5b 69 5d 20 3d 20 73 71 6c 69 74 65 33  oll[i] = sqlite3
253c9 4c 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50  LocateCollSeq(pP
253ca 61 72 73 65 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20  arse, zColl);.  
253cb 20 20 20 20 70 4b 65 79 2d 3e 61 53 6f 72 74 4f      pKey->aSortO
253cc 72 64 65 72 5b 69 5d 20 3d 20 70 49 64 78 2d 3e  rder[i] = pIdx->
253cd 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 3b 0a 20  aSortOrder[i];. 
253ce 20 20 20 7d 0a 20 20 20 20 70 4b 65 79 2d 3e 6e     }.    pKey->n
253cf 46 69 65 6c 64 20 3d 20 28 75 31 36 29 6e 43 6f  Field = (u16)nCo
253d0 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50  l;.  }..  if( pP
253d1 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20  arse->nErr ){.  
253d2 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
253d3 64 62 2c 20 70 4b 65 79 29 3b 0a 20 20 20 20 70  db, pKey);.    p
253d4 4b 65 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  Key = 0;.  }.  r
253d5 65 74 75 72 6e 20 70 4b 65 79 3b 0a 7d 0a 0a 2f  eturn pKey;.}../
253d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
253d7 6e 64 20 6f 66 20 62 75 69 6c 64 2e 63 20 2a 2a  nd of build.c **
253d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
253d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
253da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
253db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
253dc 65 67 69 6e 20 66 69 6c 65 20 63 61 6c 6c 62 61  egin file callba
253dd 63 6b 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ck.c ***********
253de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
253df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
253e0 2a 0a 2a 2a 20 32 30 30 35 20 4d 61 79 20 32 33  *.** 2005 May 23
253e1 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68   .**.** The auth
253e2 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
253e3 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
253e4 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
253e5 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
253e6 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
253e7 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
253e8 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
253e9 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
253ea 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
253eb 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
253ec 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
253ed 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
253ee 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
253ef 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
253f0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
253f1 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
253f2 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
253f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
253f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
253f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
253f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
253f7 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
253f8 20 63 6f 6e 74 61 69 6e 73 20 66 75 6e 63 74 69   contains functi
253f9 6f 6e 73 20 75 73 65 64 20 74 6f 20 61 63 63 65  ons used to acce
253fa 73 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  ss the internal 
253fb 68 61 73 68 20 74 61 62 6c 65 73 0a 2a 2a 20 6f  hash tables.** o
253fc 66 20 75 73 65 72 20 64 65 66 69 6e 65 64 20 66  f user defined f
253fd 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 63 6f 6c  unctions and col
253fe 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73  lation sequences
253ff 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 63 61 6c  ..**.** $Id: cal
25400 6c 62 61 63 6b 2e 63 2c 76 20 31 2e 34 32 20 32  lback.c,v 1.42 2
25401 30 30 39 2f 30 36 2f 31 37 20 30 30 3a 33 35 3a  009/06/17 00:35:
25402 33 31 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  31 drh Exp $.*/.
25403 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74  ../*.** Invoke t
25404 68 65 20 27 63 6f 6c 6c 61 74 69 6f 6e 20 6e 65  he 'collation ne
25405 65 64 65 64 27 20 63 61 6c 6c 62 61 63 6b 20 74  eded' callback t
25406 6f 20 72 65 71 75 65 73 74 20 61 20 63 6f 6c 6c  o request a coll
25407 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a 2a  ation sequence.*
25408 2a 20 69 6e 20 74 68 65 20 65 6e 63 6f 64 69 6e  * in the encodin
25409 67 20 65 6e 63 20 6f 66 20 6e 61 6d 65 20 7a 4e  g enc of name zN
2540a 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d  ame, length nNam
2540b 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
2540c 64 20 63 61 6c 6c 43 6f 6c 6c 4e 65 65 64 65 64  d callCollNeeded
2540d 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
2540e 74 20 65 6e 63 2c 20 63 6f 6e 73 74 20 63 68 61  t enc, const cha
2540f 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 61 73 73  r *zName){.  ass
25410 65 72 74 28 20 21 64 62 2d 3e 78 43 6f 6c 6c 4e  ert( !db->xCollN
25411 65 65 64 65 64 20 7c 7c 20 21 64 62 2d 3e 78 43  eeded || !db->xC
25412 6f 6c 6c 4e 65 65 64 65 64 31 36 20 29 3b 0a 20  ollNeeded16 );. 
25413 20 69 66 28 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65   if( db->xCollNe
25414 65 64 65 64 20 29 7b 0a 20 20 20 20 63 68 61 72  eded ){.    char
25415 20 2a 7a 45 78 74 65 72 6e 61 6c 20 3d 20 73 71   *zExternal = sq
25416 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
25417 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66  , zName);.    if
25418 28 20 21 7a 45 78 74 65 72 6e 61 6c 20 29 20 72  ( !zExternal ) r
25419 65 74 75 72 6e 3b 0a 20 20 20 20 64 62 2d 3e 78  eturn;.    db->x
2541a 43 6f 6c 6c 4e 65 65 64 65 64 28 64 62 2d 3e 70  CollNeeded(db->p
2541b 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 2c 20 64  CollNeededArg, d
2541c 62 2c 20 65 6e 63 2c 20 7a 45 78 74 65 72 6e 61  b, enc, zExterna
2541d 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  l);.    sqlite3D
2541e 62 46 72 65 65 28 64 62 2c 20 7a 45 78 74 65 72  bFree(db, zExter
2541f 6e 61 6c 29 3b 0a 20 20 7d 0a 23 69 66 6e 64 65  nal);.  }.#ifnde
25420 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
25421 46 31 36 0a 20 20 69 66 28 20 64 62 2d 3e 78 43  F16.  if( db->xC
25422 6f 6c 6c 4e 65 65 64 65 64 31 36 20 29 7b 0a 20  ollNeeded16 ){. 
25423 20 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a     char const *z
25424 45 78 74 65 72 6e 61 6c 3b 0a 20 20 20 20 73 71  External;.    sq
25425 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 54 6d  lite3_value *pTm
25426 70 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65  p = sqlite3Value
25427 4e 65 77 28 64 62 29 3b 0a 20 20 20 20 73 71 6c  New(db);.    sql
25428 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72 28  ite3ValueSetStr(
25429 70 54 6d 70 2c 20 2d 31 2c 20 7a 4e 61 6d 65 2c  pTmp, -1, zName,
2542a 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51   SQLITE_UTF8, SQ
2542b 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
2542c 20 20 7a 45 78 74 65 72 6e 61 6c 20 3d 20 73 71    zExternal = sq
2542d 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70  lite3ValueText(p
2542e 54 6d 70 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  Tmp, SQLITE_UTF1
2542f 36 4e 41 54 49 56 45 29 3b 0a 20 20 20 20 69 66  6NATIVE);.    if
25430 28 20 7a 45 78 74 65 72 6e 61 6c 20 29 7b 0a 20  ( zExternal ){. 
25431 20 20 20 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65       db->xCollNe
25432 65 64 65 64 31 36 28 64 62 2d 3e 70 43 6f 6c 6c  eded16(db->pColl
25433 4e 65 65 64 65 64 41 72 67 2c 20 64 62 2c 20 28  NeededArg, db, (
25434 69 6e 74 29 45 4e 43 28 64 62 29 2c 20 7a 45 78  int)ENC(db), zEx
25435 74 65 72 6e 61 6c 29 3b 0a 20 20 20 20 7d 0a 20  ternal);.    }. 
25436 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46     sqlite3ValueF
25437 72 65 65 28 70 54 6d 70 29 3b 0a 20 20 7d 0a 23  ree(pTmp);.  }.#
25438 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  endif.}../*.** T
25439 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
2543a 61 6c 6c 65 64 20 69 66 20 74 68 65 20 63 6f 6c  alled if the col
2543b 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 66  lation factory f
2543c 61 69 6c 73 20 74 6f 20 64 65 6c 69 76 65 72 20  ails to deliver 
2543d 61 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 66  a.** collation f
2543e 75 6e 63 74 69 6f 6e 20 69 6e 20 74 68 65 20 62  unction in the b
2543f 65 73 74 20 65 6e 63 6f 64 69 6e 67 20 62 75 74  est encoding but
25440 20 74 68 65 72 65 20 6d 61 79 20 62 65 20 6f 74   there may be ot
25441 68 65 72 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20  her versions.** 
25442 6f 66 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f  of this collatio
25443 6e 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20  n function (for 
25444 6f 74 68 65 72 20 74 65 78 74 20 65 6e 63 6f 64  other text encod
25445 69 6e 67 73 29 20 61 76 61 69 6c 61 62 6c 65 2e  ings) available.
25446 20 55 73 65 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74   Use one.** of t
25447 68 65 73 65 20 69 6e 73 74 65 61 64 20 69 66 20  hese instead if 
25448 74 68 65 79 20 65 78 69 73 74 2e 20 41 76 6f 69  they exist. Avoi
25449 64 20 61 20 55 54 46 2d 38 20 3c 2d 3e 20 55 54  d a UTF-8 <-> UT
2544a 46 2d 31 36 20 63 6f 6e 76 65 72 73 69 6f 6e 20  F-16 conversion 
2544b 69 66 0a 2a 2a 20 70 6f 73 73 69 62 6c 65 2e 0a  if.** possible..
2544c 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 79  */.static int sy
2544d 6e 74 68 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74  nthCollSeq(sqlit
2544e 65 33 20 2a 64 62 2c 20 43 6f 6c 6c 53 65 71 20  e3 *db, CollSeq 
2544f 2a 70 43 6f 6c 6c 29 7b 0a 20 20 43 6f 6c 6c 53  *pColl){.  CollS
25450 65 71 20 2a 70 43 6f 6c 6c 32 3b 0a 20 20 63 68  eq *pColl2;.  ch
25451 61 72 20 2a 7a 20 3d 20 70 43 6f 6c 6c 2d 3e 7a  ar *z = pColl->z
25452 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  Name;.  int i;. 
25453 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38   static const u8
25454 20 61 45 6e 63 5b 5d 20 3d 20 7b 20 53 51 4c 49   aEnc[] = { SQLI
25455 54 45 5f 55 54 46 31 36 42 45 2c 20 53 51 4c 49  TE_UTF16BE, SQLI
25456 54 45 5f 55 54 46 31 36 4c 45 2c 20 53 51 4c 49  TE_UTF16LE, SQLI
25457 54 45 5f 55 54 46 38 20 7d 3b 0a 20 20 66 6f 72  TE_UTF8 };.  for
25458 28 69 3d 30 3b 20 69 3c 33 3b 20 69 2b 2b 29 7b  (i=0; i<3; i++){
25459 0a 20 20 20 20 70 43 6f 6c 6c 32 20 3d 20 73 71  .    pColl2 = sq
2545a 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
2545b 28 64 62 2c 20 61 45 6e 63 5b 69 5d 2c 20 7a 2c  (db, aEnc[i], z,
2545c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 43 6f   0);.    if( pCo
2545d 6c 6c 32 2d 3e 78 43 6d 70 21 3d 30 20 29 7b 0a  ll2->xCmp!=0 ){.
2545e 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 43 6f        memcpy(pCo
2545f 6c 6c 2c 20 70 43 6f 6c 6c 32 2c 20 73 69 7a 65  ll, pColl2, size
25460 6f 66 28 43 6f 6c 6c 53 65 71 29 29 3b 0a 20 20  of(CollSeq));.  
25461 20 20 20 20 70 43 6f 6c 6c 2d 3e 78 44 65 6c 20      pColl->xDel 
25462 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  = 0;         /* 
25463 44 6f 20 6e 6f 74 20 63 6f 70 79 20 74 68 65 20  Do not copy the 
25464 64 65 73 74 72 75 63 74 6f 72 20 2a 2f 0a 20 20  destructor */.  
25465 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
25466 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  E_OK;.    }.  }.
25467 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
25468 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ERROR;.}../*.** 
25469 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
2546a 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
2546b 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 6f   invoking the co
2546c 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20  llation factory 
2546d 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 72 20 73  callback.** or s
2546e 75 62 73 74 69 74 75 74 69 6e 67 20 61 20 63 6f  ubstituting a co
2546f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
25470 20 6f 66 20 61 20 64 69 66 66 65 72 65 6e 74 20   of a different 
25471 65 6e 63 6f 64 69 6e 67 20 77 68 65 6e 20 74 68  encoding when th
25472 65 0a 2a 2a 20 72 65 71 75 65 73 74 65 64 20 63  e.** requested c
25473 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
25474 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62  e is not availab
25475 6c 65 20 69 6e 20 74 68 65 20 64 65 73 69 72 65  le in the desire
25476 64 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 20 0a  d encoding..** .
25477 2a 2a 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20  ** If it is not 
25478 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 43 6f 6c 6c  NULL, then pColl
25479 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 74   must point to t
2547a 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 74 69  he database nati
2547b 76 65 20 65 6e 63 6f 64 69 6e 67 20 0a 2a 2a 20  ve encoding .** 
2547c 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
2547d 63 65 20 77 69 74 68 20 6e 61 6d 65 20 7a 4e 61  ce with name zNa
2547e 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65  me, length nName
2547f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75  ..**.** The retu
25480 72 6e 20 76 61 6c 75 65 20 69 73 20 65 69 74 68  rn value is eith
25481 65 72 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  er the collation
25482 20 73 65 71 75 65 6e 63 65 20 74 6f 20 62 65 20   sequence to be 
25483 75 73 65 64 20 69 6e 20 64 61 74 61 62 61 73 65  used in database
25484 0a 2a 2a 20 64 62 20 66 6f 72 20 63 6f 6c 6c 61  .** db for colla
25485 74 69 6f 6e 20 74 79 70 65 20 6e 61 6d 65 20 7a  tion type name z
25486 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e 61  Name, length nNa
25487 6d 65 2c 20 6f 72 20 4e 55 4c 4c 2c 20 69 66 20  me, or NULL, if 
25488 6e 6f 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20  no collation.** 
25489 73 65 71 75 65 6e 63 65 20 63 61 6e 20 62 65 20  sequence can be 
2548a 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  found..**.** See
2548b 20 61 6c 73 6f 3a 20 73 71 6c 69 74 65 33 4c 6f   also: sqlite3Lo
2548c 63 61 74 65 43 6f 6c 6c 53 65 71 28 29 2c 20 73  cateCollSeq(), s
2548d 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65  qlite3FindCollSe
2548e 71 28 29 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  q().*/.SQLITE_PR
2548f 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73  IVATE CollSeq *s
25490 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c 53 65 71  qlite3GetCollSeq
25491 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c  (.  sqlite3* db,
25492 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
25493 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
25494 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20 65 6e 63  tion */.  u8 enc
25495 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
25496 2f 2a 20 54 68 65 20 64 65 73 69 72 65 64 20 65  /* The desired e
25497 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20  ncoding for the 
25498 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
25499 63 65 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20  ce */.  CollSeq 
2549a 2a 70 43 6f 6c 6c 2c 20 20 20 20 20 20 20 2f 2a  *pColl,       /*
2549b 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   Collating seque
2549c 6e 63 65 20 77 69 74 68 20 6e 61 74 69 76 65 20  nce with native 
2549d 65 6e 63 6f 64 69 6e 67 2c 20 6f 72 20 4e 55 4c  encoding, or NUL
2549e 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  L */.  const cha
2549f 72 20 2a 7a 4e 61 6d 65 20 20 20 20 20 2f 2a 20  r *zName     /* 
254a0 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  Collating sequen
254a1 63 65 20 6e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20  ce name */.){.  
254a2 43 6f 6c 6c 53 65 71 20 2a 70 3b 0a 0a 20 20 70  CollSeq *p;..  p
254a3 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 69 66 28 20   = pColl;.  if( 
254a4 21 70 20 29 7b 0a 20 20 20 20 70 20 3d 20 73 71  !p ){.    p = sq
254a5 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
254a6 28 64 62 2c 20 65 6e 63 2c 20 7a 4e 61 6d 65 2c  (db, enc, zName,
254a7 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21   0);.  }.  if( !
254a8 70 20 7c 7c 20 21 70 2d 3e 78 43 6d 70 20 29 7b  p || !p->xCmp ){
254a9 0a 20 20 20 20 2f 2a 20 4e 6f 20 63 6f 6c 6c 61  .    /* No colla
254aa 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6f 66  tion sequence of
254ab 20 74 68 69 73 20 74 79 70 65 20 66 6f 72 20 74   this type for t
254ac 68 69 73 20 65 6e 63 6f 64 69 6e 67 20 69 73 20  his encoding is 
254ad 72 65 67 69 73 74 65 72 65 64 2e 0a 20 20 20 20  registered..    
254ae 2a 2a 20 43 61 6c 6c 20 74 68 65 20 63 6f 6c 6c  ** Call the coll
254af 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 74 6f  ation factory to
254b0 20 73 65 65 20 69 66 20 69 74 20 63 61 6e 20 73   see if it can s
254b1 75 70 70 6c 79 20 75 73 20 77 69 74 68 20 6f 6e  upply us with on
254b2 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  e..    */.    ca
254b3 6c 6c 43 6f 6c 6c 4e 65 65 64 65 64 28 64 62 2c  llCollNeeded(db,
254b4 20 65 6e 63 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20   enc, zName);.  
254b5 20 20 70 20 3d 20 73 71 6c 69 74 65 33 46 69 6e    p = sqlite3Fin
254b6 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 65 6e 63  dCollSeq(db, enc
254b7 2c 20 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d  , zName, 0);.  }
254b8 0a 20 20 69 66 28 20 70 20 26 26 20 21 70 2d 3e  .  if( p && !p->
254b9 78 43 6d 70 20 26 26 20 73 79 6e 74 68 43 6f 6c  xCmp && synthCol
254ba 6c 53 65 71 28 64 62 2c 20 70 29 20 29 7b 0a 20  lSeq(db, p) ){. 
254bb 20 20 20 70 20 3d 20 30 3b 0a 20 20 7d 0a 20 20     p = 0;.  }.  
254bc 61 73 73 65 72 74 28 20 21 70 20 7c 7c 20 70 2d  assert( !p || p-
254bd 3e 78 43 6d 70 20 29 3b 0a 20 20 72 65 74 75 72  >xCmp );.  retur
254be 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  n p;.}../*.** Th
254bf 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
254c0 6c 6c 65 64 20 6f 6e 20 61 20 63 6f 6c 6c 61 74  lled on a collat
254c1 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 62 65 66  ion sequence bef
254c2 6f 72 65 20 69 74 20 69 73 20 75 73 65 64 20 74  ore it is used t
254c3 6f 0a 2a 2a 20 63 68 65 63 6b 20 74 68 61 74 20  o.** check that 
254c4 69 74 20 69 73 20 64 65 66 69 6e 65 64 2e 20 41  it is defined. A
254c5 6e 20 75 6e 64 65 66 69 6e 65 64 20 63 6f 6c 6c  n undefined coll
254c6 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 65  ation sequence e
254c7 78 69 73 74 73 20 77 68 65 6e 0a 2a 2a 20 61 20  xists when.** a 
254c8 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 61 64  database is load
254c9 65 64 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ed that contains
254ca 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 63   references to c
254cb 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
254cc 65 73 0a 2a 2a 20 74 68 61 74 20 68 61 76 65 20  es.** that have 
254cd 6e 6f 74 20 62 65 65 6e 20 64 65 66 69 6e 65 64  not been defined
254ce 20 62 79 20 73 71 6c 69 74 65 33 5f 63 72 65 61   by sqlite3_crea
254cf 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 20 65  te_collation() e
254d0 74 63 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 72 65 71  tc..**.** If req
254d1 75 69 72 65 64 2c 20 74 68 69 73 20 72 6f 75 74  uired, this rout
254d2 69 6e 65 20 63 61 6c 6c 73 20 74 68 65 20 27 63  ine calls the 'c
254d3 6f 6c 6c 61 74 69 6f 6e 20 6e 65 65 64 65 64 27  ollation needed'
254d4 20 63 61 6c 6c 62 61 63 6b 20 74 6f 0a 2a 2a 20   callback to.** 
254d5 72 65 71 75 65 73 74 20 61 20 64 65 66 69 6e 69  request a defini
254d6 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6c 6c  tion of the coll
254d7 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 20  ating sequence. 
254d8 49 66 20 74 68 69 73 20 64 6f 65 73 6e 27 74 20  If this doesn't 
254d9 77 6f 72 6b 2c 20 0a 2a 2a 20 61 6e 20 65 71 75  work, .** an equ
254da 69 76 61 6c 65 6e 74 20 63 6f 6c 6c 61 74 69 6e  ivalent collatin
254db 67 20 73 65 71 75 65 6e 63 65 20 74 68 61 74 20  g sequence that 
254dc 75 73 65 73 20 61 20 74 65 78 74 20 65 6e 63 6f  uses a text enco
254dd 64 69 6e 67 20 64 69 66 66 65 72 65 6e 74 0a 2a  ding different.*
254de 2a 20 66 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20  * from the main 
254df 64 61 74 61 62 61 73 65 20 69 73 20 73 75 62 73  database is subs
254e0 74 69 74 75 74 65 64 2c 20 69 66 20 6f 6e 65 20  tituted, if one 
254e1 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2f  is available..*/
254e2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
254e3 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b  int sqlite3Check
254e4 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 70  CollSeq(Parse *p
254e5 50 61 72 73 65 2c 20 43 6f 6c 6c 53 65 71 20 2a  Parse, CollSeq *
254e6 70 43 6f 6c 6c 29 7b 0a 20 20 69 66 28 20 70 43  pColl){.  if( pC
254e7 6f 6c 6c 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  oll ){.    const
254e8 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70   char *zName = p
254e9 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20  Coll->zName;.   
254ea 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
254eb 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 43  Parse->db;.    C
254ec 6f 6c 6c 53 65 71 20 2a 70 20 3d 20 73 71 6c 69  ollSeq *p = sqli
254ed 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 64 62  te3GetCollSeq(db
254ee 2c 20 45 4e 43 28 64 62 29 2c 20 70 43 6f 6c 6c  , ENC(db), pColl
254ef 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66  , zName);.    if
254f0 28 20 21 70 20 29 7b 0a 20 20 20 20 20 20 73 71  ( !p ){.      sq
254f1 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
254f2 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 63  arse, "no such c
254f3 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
254f4 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a  e: %s", zName);.
254f5 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45        pParse->nE
254f6 72 72 2b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75  rr++;.      retu
254f7 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
254f8 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
254f9 74 28 20 70 3d 3d 70 43 6f 6c 6c 20 29 3b 0a 20  t( p==pColl );. 
254fa 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
254fb 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a  TE_OK;.}..../*.*
254fc 2a 20 4c 6f 63 61 74 65 20 61 6e 64 20 72 65 74  * Locate and ret
254fd 75 72 6e 20 61 6e 20 65 6e 74 72 79 20 66 72 6f  urn an entry fro
254fe 6d 20 74 68 65 20 64 62 2e 61 43 6f 6c 6c 53 65  m the db.aCollSe
254ff 71 20 68 61 73 68 20 74 61 62 6c 65 2e 20 49 66  q hash table. If
25500 20 74 68 65 20 65 6e 74 72 79 0a 2a 2a 20 73 70   the entry.** sp
25501 65 63 69 66 69 65 64 20 62 79 20 7a 4e 61 6d 65  ecified by zName
25502 20 61 6e 64 20 6e 4e 61 6d 65 20 69 73 20 6e 6f   and nName is no
25503 74 20 66 6f 75 6e 64 20 61 6e 64 20 70 61 72 61  t found and para
25504 6d 65 74 65 72 20 27 63 72 65 61 74 65 27 20 69  meter 'create' i
25505 73 0a 2a 2a 20 74 72 75 65 2c 20 74 68 65 6e 20  s.** true, then 
25506 63 72 65 61 74 65 20 61 20 6e 65 77 20 65 6e 74  create a new ent
25507 72 79 2e 20 4f 74 68 65 72 77 69 73 65 20 72 65  ry. Otherwise re
25508 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  turn NULL..**.**
25509 20 45 61 63 68 20 70 6f 69 6e 74 65 72 20 73 74   Each pointer st
2550a 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69  ored in the sqli
2550b 74 65 33 2e 61 43 6f 6c 6c 53 65 71 20 68 61 73  te3.aCollSeq has
2550c 68 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73  h table contains
2550d 20 61 6e 0a 2a 2a 20 61 72 72 61 79 20 6f 66 20   an.** array of 
2550e 74 68 72 65 65 20 43 6f 6c 6c 53 65 71 20 73 74  three CollSeq st
2550f 72 75 63 74 75 72 65 73 2e 20 54 68 65 20 66 69  ructures. The fi
25510 72 73 74 20 69 73 20 74 68 65 20 63 6f 6c 6c 61  rst is the colla
25511 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a 2a 2a  tion sequence.**
25512 20 70 72 65 66 66 65 72 72 65 64 20 66 6f 72 20   prefferred for 
25513 55 54 46 2d 38 2c 20 74 68 65 20 73 65 63 6f 6e  UTF-8, the secon
25514 64 20 55 54 46 2d 31 36 6c 65 2c 20 61 6e 64 20  d UTF-16le, and 
25515 74 68 65 20 74 68 69 72 64 20 55 54 46 2d 31 36  the third UTF-16
25516 62 65 2e 0a 2a 2a 0a 2a 2a 20 53 74 6f 72 65 64  be..**.** Stored
25517 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74   immediately aft
25518 65 72 20 74 68 65 20 74 68 72 65 65 20 63 6f 6c  er the three col
25519 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73  lation sequences
2551a 20 69 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a 2a   is a copy of.**
2551b 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   the collation s
2551c 65 71 75 65 6e 63 65 20 6e 61 6d 65 2e 20 41 20  equence name. A 
2551d 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20  pointer to this 
2551e 73 74 72 69 6e 67 20 69 73 20 73 74 6f 72 65 64  string is stored
2551f 20 69 6e 0a 2a 2a 20 65 61 63 68 20 63 6f 6c 6c   in.** each coll
25520 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 73  ation sequence s
25521 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 61  tructure..*/.sta
25522 74 69 63 20 43 6f 6c 6c 53 65 71 20 2a 66 69 6e  tic CollSeq *fin
25523 64 43 6f 6c 6c 53 65 71 45 6e 74 72 79 28 0a 20  dCollSeqEntry(. 
25524 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
25525 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
25526 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
25527 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
25528 4e 61 6d 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65  Name,    /* Name
25529 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e   of the collatin
2552a 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 20 20  g sequence */.  
2552b 69 6e 74 20 63 72 65 61 74 65 20 20 20 20 20 20  int create      
2552c 20 20 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20        /* Create 
2552d 61 20 6e 65 77 20 65 6e 74 72 79 20 69 66 20 74  a new entry if t
2552e 72 75 65 20 2a 2f 0a 29 7b 0a 20 20 43 6f 6c 6c  rue */.){.  Coll
2552f 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 6e  Seq *pColl;.  in
25530 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65  t nName = sqlite
25531 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29  3Strlen30(zName)
25532 3b 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69  ;.  pColl = sqli
25533 74 65 33 48 61 73 68 46 69 6e 64 28 26 64 62 2d  te3HashFind(&db-
25534 3e 61 43 6f 6c 6c 53 65 71 2c 20 7a 4e 61 6d 65  >aCollSeq, zName
25535 2c 20 6e 4e 61 6d 65 29 3b 0a 0a 20 20 69 66 28  , nName);..  if(
25536 20 30 3d 3d 70 43 6f 6c 6c 20 26 26 20 63 72 65   0==pColl && cre
25537 61 74 65 20 29 7b 0a 20 20 20 20 70 43 6f 6c 6c  ate ){.    pColl
25538 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
25539 6f 63 5a 65 72 6f 28 64 62 2c 20 33 2a 73 69 7a  ocZero(db, 3*siz
2553a 65 6f 66 28 2a 70 43 6f 6c 6c 29 20 2b 20 6e 4e  eof(*pColl) + nN
2553b 61 6d 65 20 2b 20 31 20 29 3b 0a 20 20 20 20 69  ame + 1 );.    i
2553c 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  f( pColl ){.    
2553d 20 20 43 6f 6c 6c 53 65 71 20 2a 70 44 65 6c 20    CollSeq *pDel 
2553e 3d 20 30 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c  = 0;.      pColl
2553f 5b 30 5d 2e 7a 4e 61 6d 65 20 3d 20 28 63 68 61  [0].zName = (cha
25540 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d 3b 0a 20 20  r*)&pColl[3];.  
25541 20 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e 65 6e 63      pColl[0].enc
25542 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a   = SQLITE_UTF8;.
25543 20 20 20 20 20 20 70 43 6f 6c 6c 5b 31 5d 2e 7a        pColl[1].z
25544 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29 26 70  Name = (char*)&p
25545 43 6f 6c 6c 5b 33 5d 3b 0a 20 20 20 20 20 20 70  Coll[3];.      p
25546 43 6f 6c 6c 5b 31 5d 2e 65 6e 63 20 3d 20 53 51  Coll[1].enc = SQ
25547 4c 49 54 45 5f 55 54 46 31 36 4c 45 3b 0a 20 20  LITE_UTF16LE;.  
25548 20 20 20 20 70 43 6f 6c 6c 5b 32 5d 2e 7a 4e 61      pColl[2].zNa
25549 6d 65 20 3d 20 28 63 68 61 72 2a 29 26 70 43 6f  me = (char*)&pCo
2554a 6c 6c 5b 33 5d 3b 0a 20 20 20 20 20 20 70 43 6f  ll[3];.      pCo
2554b 6c 6c 5b 32 5d 2e 65 6e 63 20 3d 20 53 51 4c 49  ll[2].enc = SQLI
2554c 54 45 5f 55 54 46 31 36 42 45 3b 0a 20 20 20 20  TE_UTF16BE;.    
2554d 20 20 6d 65 6d 63 70 79 28 70 43 6f 6c 6c 5b 30    memcpy(pColl[0
2554e 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20  ].zName, zName, 
2554f 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70 43  nName);.      pC
25550 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 5b 6e 4e 61  oll[0].zName[nNa
25551 6d 65 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  me] = 0;.      p
25552 44 65 6c 20 3d 20 73 71 6c 69 74 65 33 48 61 73  Del = sqlite3Has
25553 68 49 6e 73 65 72 74 28 26 64 62 2d 3e 61 43 6f  hInsert(&db->aCo
25554 6c 6c 53 65 71 2c 20 70 43 6f 6c 6c 5b 30 5d 2e  llSeq, pColl[0].
25555 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70 43  zName, nName, pC
25556 6f 6c 6c 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  oll);..      /* 
25557 49 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61  If a malloc() fa
25558 69 6c 75 72 65 20 6f 63 63 75 72 72 65 64 20 69  ilure occurred i
25559 6e 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  n sqlite3HashIns
2555a 65 72 74 28 29 2c 20 69 74 20 77 69 6c 6c 20 0a  ert(), it will .
2555b 20 20 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 20        ** return 
2555c 74 68 65 20 70 43 6f 6c 6c 20 70 6f 69 6e 74 65  the pColl pointe
2555d 72 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20  r to be deleted 
2555e 28 62 65 63 61 75 73 65 20 69 74 20 77 61 73 6e  (because it wasn
2555f 27 74 20 61 64 64 65 64 0a 20 20 20 20 20 20 2a  't added.      *
25560 2a 20 74 6f 20 74 68 65 20 68 61 73 68 20 74 61  * to the hash ta
25561 62 6c 65 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ble)..      */. 
25562 20 20 20 20 20 61 73 73 65 72 74 28 20 70 44 65       assert( pDe
25563 6c 3d 3d 30 20 7c 7c 20 70 44 65 6c 3d 3d 70 43  l==0 || pDel==pC
25564 6f 6c 6c 20 29 3b 0a 20 20 20 20 20 20 69 66 28  oll );.      if(
25565 20 70 44 65 6c 21 3d 30 20 29 7b 0a 20 20 20 20   pDel!=0 ){.    
25566 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
25567 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
25568 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
25569 64 62 2c 20 70 44 65 6c 29 3b 0a 20 20 20 20 20  db, pDel);.     
2556a 20 20 20 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20     pColl = 0;.  
2556b 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
2556c 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a    return pColl;.
2556d 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 61 6d 65 74  }../*.** Paramet
2556e 65 72 20 7a 4e 61 6d 65 20 70 6f 69 6e 74 73 20  er zName points 
2556f 74 6f 20 61 20 55 54 46 2d 38 20 65 6e 63 6f 64  to a UTF-8 encod
25570 65 64 20 73 74 72 69 6e 67 20 6e 4e 61 6d 65 20  ed string nName 
25571 62 79 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2a 20 52  bytes long..** R
25572 65 74 75 72 6e 20 74 68 65 20 43 6f 6c 6c 53 65  eturn the CollSe
25573 71 2a 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 74  q* pointer for t
25574 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
25575 75 65 6e 63 65 20 6e 61 6d 65 64 20 7a 4e 61 6d  uence named zNam
25576 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 65 6e 63  e.** for the enc
25577 6f 64 69 6e 67 20 27 65 6e 63 27 20 66 72 6f 6d  oding 'enc' from
25578 20 74 68 65 20 64 61 74 61 62 61 73 65 20 27 64   the database 'd
25579 62 27 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  b'..**.** If the
2557a 20 65 6e 74 72 79 20 73 70 65 63 69 66 69 65 64   entry specified
2557b 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 20 61 6e   is not found an
2557c 64 20 27 63 72 65 61 74 65 27 20 69 73 20 74 72  d 'create' is tr
2557d 75 65 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20  ue, then create 
2557e 61 0a 2a 2a 20 6e 65 77 20 65 6e 74 72 79 2e 20  a.** new entry. 
2557f 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72   Otherwise retur
25580 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 41 20  n NULL..**.** A 
25581 73 65 70 61 72 61 74 65 20 66 75 6e 63 74 69 6f  separate functio
25582 6e 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43  n sqlite3LocateC
25583 6f 6c 6c 53 65 71 28 29 20 69 73 20 61 20 77 72  ollSeq() is a wr
25584 61 70 70 65 72 20 61 72 6f 75 6e 64 0a 2a 2a 20  apper around.** 
25585 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 73  this routine.  s
25586 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c  qlite3LocateColl
25587 53 65 71 28 29 20 69 6e 76 6f 6b 65 73 20 74 68  Seq() invokes th
25588 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74  e collation fact
25589 6f 72 79 0a 2a 2a 20 69 66 20 6e 65 63 65 73 73  ory.** if necess
2558a 61 72 79 20 61 6e 64 20 67 65 6e 65 72 61 74 65  ary and generate
2558b 73 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  s an error messa
2558c 67 65 20 69 66 20 74 68 65 20 63 6f 6c 6c 61 74  ge if the collat
2558d 69 6e 67 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20  ing sequence.** 
2558e 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e  cannot be found.
2558f 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
25590 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f   sqlite3LocateCo
25591 6c 6c 53 65 71 28 29 2c 20 73 71 6c 69 74 65 33  llSeq(), sqlite3
25592 47 65 74 43 6f 6c 6c 53 65 71 28 29 0a 2a 2f 0a  GetCollSeq().*/.
25593 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43  SQLITE_PRIVATE C
25594 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 46  ollSeq *sqlite3F
25595 69 6e 64 43 6f 6c 6c 53 65 71 28 0a 20 20 73 71  indCollSeq(.  sq
25596 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 75 38 20  lite3 *db,.  u8 
25597 65 6e 63 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  enc,.  const cha
25598 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  r *zName,.  int 
25599 63 72 65 61 74 65 0a 29 7b 0a 20 20 43 6f 6c 6c  create.){.  Coll
2559a 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 66  Seq *pColl;.  if
2559b 28 20 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 70  ( zName ){.    p
2559c 43 6f 6c 6c 20 3d 20 66 69 6e 64 43 6f 6c 6c 53  Coll = findCollS
2559d 65 71 45 6e 74 72 79 28 64 62 2c 20 7a 4e 61 6d  eqEntry(db, zNam
2559e 65 2c 20 63 72 65 61 74 65 29 3b 0a 20 20 7d 65  e, create);.  }e
2559f 6c 73 65 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d  lse{.    pColl =
255a0 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a   db->pDfltColl;.
255a1 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 53 51    }.  assert( SQ
255a2 4c 49 54 45 5f 55 54 46 38 3d 3d 31 20 26 26 20  LITE_UTF8==1 && 
255a3 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3d 3d  SQLITE_UTF16LE==
255a4 32 20 26 26 20 53 51 4c 49 54 45 5f 55 54 46 31  2 && SQLITE_UTF1
255a5 36 42 45 3d 3d 33 20 29 3b 0a 20 20 61 73 73 65  6BE==3 );.  asse
255a6 72 74 28 20 65 6e 63 3e 3d 53 51 4c 49 54 45 5f  rt( enc>=SQLITE_
255a7 55 54 46 38 20 26 26 20 65 6e 63 3c 3d 53 51 4c  UTF8 && enc<=SQL
255a8 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 20  ITE_UTF16BE );. 
255a9 20 69 66 28 20 70 43 6f 6c 6c 20 29 20 70 43 6f   if( pColl ) pCo
255aa 6c 6c 20 2b 3d 20 65 6e 63 2d 31 3b 0a 20 20 72  ll += enc-1;.  r
255ab 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a  eturn pColl;.}..
255ac 2f 2a 20 44 75 72 69 6e 67 20 74 68 65 20 73 65  /* During the se
255ad 61 72 63 68 20 66 6f 72 20 74 68 65 20 62 65 73  arch for the bes
255ae 74 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e  t function defin
255af 69 74 69 6f 6e 2c 20 74 68 69 73 20 70 72 6f 63  ition, this proc
255b0 65 64 75 72 65 0a 2a 2a 20 69 73 20 63 61 6c 6c  edure.** is call
255b1 65 64 20 74 6f 20 74 65 73 74 20 68 6f 77 20 77  ed to test how w
255b2 65 6c 6c 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  ell the function
255b3 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 66   passed as the f
255b4 69 72 73 74 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  irst argument.**
255b5 20 6d 61 74 63 68 65 73 20 74 68 65 20 72 65 71   matches the req
255b6 75 65 73 74 20 66 6f 72 20 61 20 66 75 6e 63 74  uest for a funct
255b7 69 6f 6e 20 77 69 74 68 20 6e 41 72 67 20 61 72  ion with nArg ar
255b8 67 75 6d 65 6e 74 73 20 69 6e 20 61 20 73 79 73  guments in a sys
255b9 74 65 6d 0a 2a 2a 20 74 68 61 74 20 75 73 65 73  tem.** that uses
255ba 20 65 6e 63 6f 64 69 6e 67 20 65 6e 63 2e 20 54   encoding enc. T
255bb 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65  he value returne
255bc 64 20 69 6e 64 69 63 61 74 65 73 20 68 6f 77 20  d indicates how 
255bd 77 65 6c 6c 20 74 68 65 0a 2a 2a 20 72 65 71 75  well the.** requ
255be 65 73 74 20 69 73 20 6d 61 74 63 68 65 64 2e 20  est is matched. 
255bf 41 20 68 69 67 68 65 72 20 76 61 6c 75 65 20 69  A higher value i
255c0 6e 64 69 63 61 74 65 73 20 61 20 62 65 74 74 65  ndicates a bette
255c1 72 20 6d 61 74 63 68 2e 0a 2a 2a 0a 2a 2a 20 54  r match..**.** T
255c2 68 65 20 72 65 74 75 72 6e 65 64 20 76 61 6c 75  he returned valu
255c3 65 20 69 73 20 61 6c 77 61 79 73 20 62 65 74 77  e is always betw
255c4 65 65 6e 20 30 20 61 6e 64 20 36 2c 20 61 73 20  een 0 and 6, as 
255c5 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 30  follows:.**.** 0
255c6 3a 20 4e 6f 74 20 61 20 6d 61 74 63 68 2c 20 6f  : Not a match, o
255c7 72 20 69 66 20 6e 41 72 67 3c 30 20 61 6e 64 20  r if nArg<0 and 
255c8 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
255c9 68 61 73 20 6e 6f 20 69 6d 70 6c 65 6d 65 6e 74  has no implement
255ca 61 74 69 6f 6e 2e 0a 2a 2a 20 31 3a 20 41 20 76  ation..** 1: A v
255cb 61 72 69 61 62 6c 65 20 61 72 67 75 6d 65 6e 74  ariable argument
255cc 73 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  s function that 
255cd 70 72 65 66 65 72 73 20 55 54 46 2d 38 20 77 68  prefers UTF-8 wh
255ce 65 6e 20 61 20 55 54 46 2d 31 36 0a 2a 2a 20 20  en a UTF-16.**  
255cf 20 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 72 65    encoding is re
255d0 71 75 65 73 74 65 64 2c 20 6f 72 20 76 69 63 65  quested, or vice
255d1 20 76 65 72 73 61 2e 0a 2a 2a 20 32 3a 20 41 20   versa..** 2: A 
255d2 76 61 72 69 61 62 6c 65 20 61 72 67 75 6d 65 6e  variable argumen
255d3 74 73 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74  ts function that
255d4 20 75 73 65 73 20 55 54 46 2d 31 36 42 45 20 77   uses UTF-16BE w
255d5 68 65 6e 20 55 54 46 2d 31 36 4c 45 20 69 73 0a  hen UTF-16LE is.
255d6 2a 2a 20 20 20 20 72 65 71 75 65 73 74 65 64 2c  **    requested,
255d7 20 6f 72 20 76 69 63 65 20 76 65 72 73 61 2e 0a   or vice versa..
255d8 2a 2a 20 33 3a 20 41 20 76 61 72 69 61 62 6c 65  ** 3: A variable
255d9 20 61 72 67 75 6d 65 6e 74 73 20 66 75 6e 63 74   arguments funct
255da 69 6f 6e 20 75 73 69 6e 67 20 74 68 65 20 73 61  ion using the sa
255db 6d 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  me text encoding
255dc 2e 0a 2a 2a 20 34 3a 20 41 20 66 75 6e 63 74 69  ..** 4: A functi
255dd 6f 6e 20 77 69 74 68 20 74 68 65 20 65 78 61 63  on with the exac
255de 74 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  t number of argu
255df 6d 65 6e 74 73 20 72 65 71 75 65 73 74 65 64 20  ments requested 
255e0 74 68 61 74 0a 2a 2a 20 20 20 20 70 72 65 66 65  that.**    prefe
255e1 72 73 20 55 54 46 2d 38 20 77 68 65 6e 20 61 20  rs UTF-8 when a 
255e2 55 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e 67 20  UTF-16 encoding 
255e3 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 6f 72  is requested, or
255e4 20 76 69 63 65 20 76 65 72 73 61 2e 0a 2a 2a 20   vice versa..** 
255e5 35 3a 20 41 20 66 75 6e 63 74 69 6f 6e 20 77 69  5: A function wi
255e6 74 68 20 74 68 65 20 65 78 61 63 74 20 6e 75 6d  th the exact num
255e7 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ber of arguments
255e8 20 72 65 71 75 65 73 74 65 64 20 74 68 61 74 0a   requested that.
255e9 2a 2a 20 20 20 20 70 72 65 66 65 72 73 20 55 54  **    prefers UT
255ea 46 2d 31 36 4c 45 20 77 68 65 6e 20 55 54 46 2d  F-16LE when UTF-
255eb 31 36 42 45 20 69 73 20 72 65 71 75 65 73 74 65  16BE is requeste
255ec 64 2c 20 6f 72 20 76 69 63 65 20 76 65 72 73 61  d, or vice versa
255ed 2e 0a 2a 2a 20 36 3a 20 41 6e 20 65 78 61 63 74  ..** 6: An exact
255ee 20 6d 61 74 63 68 2e 0a 2a 2a 0a 2a 2f 0a 73 74   match..**.*/.st
255ef 61 74 69 63 20 69 6e 74 20 6d 61 74 63 68 51 75  atic int matchQu
255f0 61 6c 69 74 79 28 46 75 6e 63 44 65 66 20 2a 70  ality(FuncDef *p
255f1 2c 20 69 6e 74 20 6e 41 72 67 2c 20 75 38 20 65  , int nArg, u8 e
255f2 6e 63 29 7b 0a 20 20 69 6e 74 20 6d 61 74 63 68  nc){.  int match
255f3 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 2d 3e 6e   = 0;.  if( p->n
255f4 41 72 67 3d 3d 2d 31 20 7c 7c 20 70 2d 3e 6e 41  Arg==-1 || p->nA
255f5 72 67 3d 3d 6e 41 72 67 20 0a 20 20 20 7c 7c 20  rg==nArg .   || 
255f6 28 6e 41 72 67 3d 3d 2d 31 20 26 26 20 28 70 2d  (nArg==-1 && (p-
255f7 3e 78 46 75 6e 63 21 3d 30 20 7c 7c 20 70 2d 3e  >xFunc!=0 || p->
255f8 78 53 74 65 70 21 3d 30 29 29 0a 20 20 29 7b 0a  xStep!=0)).  ){.
255f9 20 20 20 20 6d 61 74 63 68 20 3d 20 31 3b 0a 20      match = 1;. 
255fa 20 20 20 69 66 28 20 70 2d 3e 6e 41 72 67 3d 3d     if( p->nArg==
255fb 6e 41 72 67 20 7c 7c 20 6e 41 72 67 3d 3d 2d 31  nArg || nArg==-1
255fc 20 29 7b 0a 20 20 20 20 20 20 6d 61 74 63 68 20   ){.      match 
255fd 3d 20 34 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 4;.    }.    i
255fe 66 28 20 65 6e 63 3d 3d 70 2d 3e 69 50 72 65 66  f( enc==p->iPref
255ff 45 6e 63 20 29 7b 0a 20 20 20 20 20 20 6d 61 74  Enc ){.      mat
25600 63 68 20 2b 3d 20 32 3b 0a 20 20 20 20 7d 0a 20  ch += 2;.    }. 
25601 20 20 20 65 6c 73 65 20 69 66 28 20 28 65 6e 63     else if( (enc
25602 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45  ==SQLITE_UTF16LE
25603 20 26 26 20 70 2d 3e 69 50 72 65 66 45 6e 63 3d   && p->iPrefEnc=
25604 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 29  =SQLITE_UTF16BE)
25605 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20   ||.            
25606 20 28 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54   (enc==SQLITE_UT
25607 46 31 36 42 45 20 26 26 20 70 2d 3e 69 50 72 65  F16BE && p->iPre
25608 66 45 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  fEnc==SQLITE_UTF
25609 31 36 4c 45 29 20 29 7b 0a 20 20 20 20 20 20 6d  16LE) ){.      m
2560a 61 74 63 68 20 2b 3d 20 31 3b 0a 20 20 20 20 7d  atch += 1;.    }
2560b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d 61  .  }.  return ma
2560c 74 63 68 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  tch;.}../*.** Se
2560d 61 72 63 68 20 61 20 46 75 6e 63 44 65 66 48 61  arch a FuncDefHa
2560e 73 68 20 66 6f 72 20 61 20 66 75 6e 63 74 69 6f  sh for a functio
2560f 6e 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e  n with the given
25610 20 6e 61 6d 65 2e 20 20 52 65 74 75 72 6e 0a 2a   name.  Return.*
25611 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  * a pointer to t
25612 68 65 20 6d 61 74 63 68 69 6e 67 20 46 75 6e 63  he matching Func
25613 44 65 66 20 69 66 20 66 6f 75 6e 64 2c 20 6f 72  Def if found, or
25614 20 30 20 69 66 20 74 68 65 72 65 20 69 73 20 6e   0 if there is n
25615 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74 61 74  o match..*/.stat
25616 69 63 20 46 75 6e 63 44 65 66 20 2a 66 75 6e 63  ic FuncDef *func
25617 74 69 6f 6e 53 65 61 72 63 68 28 0a 20 20 46 75  tionSearch(.  Fu
25618 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68  ncDefHash *pHash
25619 2c 20 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65  ,  /* Hash table
2561a 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0a 20 20   to search */.  
2561b 69 6e 74 20 68 2c 20 20 20 20 20 20 20 20 20 20  int h,          
2561c 20 20 20 20 20 2f 2a 20 48 61 73 68 20 6f 66 20       /* Hash of 
2561d 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f  the name */.  co
2561e 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63 2c  nst char *zFunc,
2561f 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66 75     /* Name of fu
25620 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  nction */.  int 
25621 6e 46 75 6e 63 20 20 20 20 20 20 20 20 20 20 20  nFunc           
25622 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
25623 74 65 73 20 69 6e 20 7a 46 75 6e 63 20 2a 2f 0a  tes in zFunc */.
25624 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 3b  ){.  FuncDef *p;
25625 0a 20 20 66 6f 72 28 70 3d 70 48 61 73 68 2d 3e  .  for(p=pHash->
25626 61 5b 68 5d 3b 20 70 3b 20 70 3d 70 2d 3e 70 48  a[h]; p; p=p->pH
25627 61 73 68 29 7b 0a 20 20 20 20 69 66 28 20 73 71  ash){.    if( sq
25628 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70 2d  lite3StrNICmp(p-
25629 3e 7a 4e 61 6d 65 2c 20 7a 46 75 6e 63 2c 20 6e  >zName, zFunc, n
2562a 46 75 6e 63 29 3d 3d 30 20 26 26 20 70 2d 3e 7a  Func)==0 && p->z
2562b 4e 61 6d 65 5b 6e 46 75 6e 63 5d 3d 3d 30 20 29  Name[nFunc]==0 )
2562c 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70  {.      return p
2562d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
2562e 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 0;.}../*.**
2562f 20 49 6e 73 65 72 74 20 61 20 6e 65 77 20 46 75   Insert a new Fu
25630 6e 63 44 65 66 20 69 6e 74 6f 20 61 20 46 75 6e  ncDef into a Fun
25631 63 44 65 66 48 61 73 68 20 68 61 73 68 20 74 61  cDefHash hash ta
25632 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
25633 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
25634 74 65 33 46 75 6e 63 44 65 66 49 6e 73 65 72 74  te3FuncDefInsert
25635 28 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68 20  (.  FuncDefHash 
25636 2a 70 48 61 73 68 2c 20 20 2f 2a 20 54 68 65 20  *pHash,  /* The 
25637 68 61 73 68 20 74 61 62 6c 65 20 69 6e 74 6f 20  hash table into 
25638 77 68 69 63 68 20 74 6f 20 69 6e 73 65 72 74 20  which to insert 
25639 2a 2f 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 44  */.  FuncDef *pD
2563a 65 66 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ef        /* The
2563b 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69   function defini
2563c 74 69 6f 6e 20 74 6f 20 69 6e 73 65 72 74 20 2a  tion to insert *
2563d 2f 0a 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a  /.){.  FuncDef *
2563e 70 4f 74 68 65 72 3b 0a 20 20 69 6e 74 20 6e 4e  pOther;.  int nN
2563f 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74 72  ame = sqlite3Str
25640 6c 65 6e 33 30 28 70 44 65 66 2d 3e 7a 4e 61 6d  len30(pDef->zNam
25641 65 29 3b 0a 20 20 75 38 20 63 31 20 3d 20 28 75  e);.  u8 c1 = (u
25642 38 29 70 44 65 66 2d 3e 7a 4e 61 6d 65 5b 30 5d  8)pDef->zName[0]
25643 3b 0a 20 20 69 6e 74 20 68 20 3d 20 28 73 71 6c  ;.  int h = (sql
25644 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72  ite3UpperToLower
25645 5b 63 31 5d 20 2b 20 6e 4e 61 6d 65 29 20 25 20  [c1] + nName) % 
25646 41 72 72 61 79 53 69 7a 65 28 70 48 61 73 68 2d  ArraySize(pHash-
25647 3e 61 29 3b 0a 20 20 70 4f 74 68 65 72 20 3d 20  >a);.  pOther = 
25648 66 75 6e 63 74 69 6f 6e 53 65 61 72 63 68 28 70  functionSearch(p
25649 48 61 73 68 2c 20 68 2c 20 70 44 65 66 2d 3e 7a  Hash, h, pDef->z
2564a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20  Name, nName);.  
2564b 69 66 28 20 70 4f 74 68 65 72 20 29 7b 0a 20 20  if( pOther ){.  
2564c 20 20 61 73 73 65 72 74 28 20 70 4f 74 68 65 72    assert( pOther
2564d 21 3d 70 44 65 66 20 26 26 20 70 4f 74 68 65 72  !=pDef && pOther
2564e 2d 3e 70 4e 65 78 74 21 3d 70 44 65 66 20 29 3b  ->pNext!=pDef );
2564f 0a 20 20 20 20 70 44 65 66 2d 3e 70 4e 65 78 74  .    pDef->pNext
25650 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74   = pOther->pNext
25651 3b 0a 20 20 20 20 70 4f 74 68 65 72 2d 3e 70 4e  ;.    pOther->pN
25652 65 78 74 20 3d 20 70 44 65 66 3b 0a 20 20 7d 65  ext = pDef;.  }e
25653 6c 73 65 7b 0a 20 20 20 20 70 44 65 66 2d 3e 70  lse{.    pDef->p
25654 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 44  Next = 0;.    pD
25655 65 66 2d 3e 70 48 61 73 68 20 3d 20 70 48 61 73  ef->pHash = pHas
25656 68 2d 3e 61 5b 68 5d 3b 0a 20 20 20 20 70 48 61  h->a[h];.    pHa
25657 73 68 2d 3e 61 5b 68 5d 20 3d 20 70 44 65 66 3b  sh->a[h] = pDef;
25658 0a 20 20 7d 0a 7d 0a 20 20 0a 20 20 0a 0a 2f 2a  .  }.}.  .  ../*
25659 0a 2a 2a 20 4c 6f 63 61 74 65 20 61 20 75 73 65  .** Locate a use
2565a 72 20 66 75 6e 63 74 69 6f 6e 20 67 69 76 65 6e  r function given
2565b 20 61 20 6e 61 6d 65 2c 20 61 20 6e 75 6d 62 65   a name, a numbe
2565c 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 61  r of arguments a
2565d 6e 64 20 61 20 66 6c 61 67 0a 2a 2a 20 69 6e 64  nd a flag.** ind
2565e 69 63 61 74 69 6e 67 20 77 68 65 74 68 65 72 20  icating whether 
2565f 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 72 65  the function pre
25660 66 65 72 73 20 55 54 46 2d 31 36 20 6f 76 65 72  fers UTF-16 over
25661 20 55 54 46 2d 38 2e 20 20 52 65 74 75 72 6e 20   UTF-8.  Return 
25662 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20  a.** pointer to 
25663 74 68 65 20 46 75 6e 63 44 65 66 20 73 74 72 75  the FuncDef stru
25664 63 74 75 72 65 20 74 68 61 74 20 64 65 66 69 6e  cture that defin
25665 65 73 20 74 68 61 74 20 66 75 6e 63 74 69 6f 6e  es that function
25666 2c 20 6f 72 20 72 65 74 75 72 6e 0a 2a 2a 20 4e  , or return.** N
25667 55 4c 4c 20 69 66 20 74 68 65 20 66 75 6e 63 74  ULL if the funct
25668 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  ion does not exi
25669 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  st..**.** If the
2566a 20 63 72 65 61 74 65 46 6c 61 67 20 61 72 67 75   createFlag argu
2566b 6d 65 6e 74 20 69 73 20 74 72 75 65 2c 20 74 68  ment is true, th
2566c 65 6e 20 61 20 6e 65 77 20 28 62 6c 61 6e 6b 29  en a new (blank)
2566d 20 46 75 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75   FuncDef.** stru
2566e 63 74 75 72 65 20 69 73 20 63 72 65 61 74 65 64  cture is created
2566f 20 61 6e 64 20 6c 69 6b 65 64 20 69 6e 74 6f 20   and liked into 
25670 74 68 65 20 22 64 62 22 20 73 74 72 75 63 74 75  the "db" structu
25671 72 65 20 69 66 20 61 0a 2a 2a 20 6e 6f 20 6d 61  re if a.** no ma
25672 74 63 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  tching function 
25673 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73 74  previously exist
25674 65 64 2e 20 20 57 68 65 6e 20 63 72 65 61 74 65  ed.  When create
25675 46 6c 61 67 20 69 73 20 74 72 75 65 0a 2a 2a 20  Flag is true.** 
25676 61 6e 64 20 74 68 65 20 6e 41 72 67 20 70 61 72  and the nArg par
25677 61 6d 65 74 65 72 20 69 73 20 2d 31 2c 20 74 68  ameter is -1, th
25678 65 6e 20 6f 6e 6c 79 20 61 20 66 75 6e 63 74 69  en only a functi
25679 6f 6e 20 74 68 61 74 20 61 63 63 65 70 74 73 0a  on that accepts.
2567a 2a 2a 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66  ** any number of
2567b 20 61 72 67 75 6d 65 6e 74 73 20 77 69 6c 6c 20   arguments will 
2567c 62 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  be returned..**.
2567d 2a 2a 20 49 66 20 63 72 65 61 74 65 46 6c 61 67  ** If createFlag
2567e 20 69 73 20 66 61 6c 73 65 20 61 6e 64 20 6e 41   is false and nA
2567f 72 67 20 69 73 20 2d 31 2c 20 74 68 65 6e 20 74  rg is -1, then t
25680 68 65 20 66 69 72 73 74 20 76 61 6c 69 64 0a 2a  he first valid.*
25681 2a 20 66 75 6e 63 74 69 6f 6e 20 66 6f 75 6e 64  * function found
25682 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 41   is returned.  A
25683 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 76 61 6c   function is val
25684 69 64 20 69 66 20 65 69 74 68 65 72 20 78 46 75  id if either xFu
25685 6e 63 0a 2a 2a 20 6f 72 20 78 53 74 65 70 20 69  nc.** or xStep i
25686 73 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a  s non-zero..**.*
25687 2a 20 49 66 20 63 72 65 61 74 65 46 6c 61 67 20  * If createFlag 
25688 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20 61  is false, then a
25689 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74   function with t
2568a 68 65 20 72 65 71 75 69 72 65 64 20 6e 61 6d 65  he required name
2568b 20 61 6e 64 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f   and.** number o
2568c 66 20 61 72 67 75 6d 65 6e 74 73 20 6d 61 79 20  f arguments may 
2568d 62 65 20 72 65 74 75 72 6e 65 64 20 65 76 65 6e  be returned even
2568e 20 69 66 20 74 68 65 20 65 54 65 78 74 52 65 70   if the eTextRep
2568f 20 66 6c 61 67 20 64 6f 65 73 20 6e 6f 74 0a 2a   flag does not.*
25690 2a 20 6d 61 74 63 68 20 74 68 61 74 20 72 65 71  * match that req
25691 75 65 73 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  uested..*/.SQLIT
25692 45 5f 50 52 49 56 41 54 45 20 46 75 6e 63 44 65  E_PRIVATE FuncDe
25693 66 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 46 75  f *sqlite3FindFu
25694 6e 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65  nction(.  sqlite
25695 33 20 2a 64 62 2c 20 20 20 20 20 20 20 2f 2a 20  3 *db,       /* 
25696 41 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65  An open database
25697 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
25698 20 2a 7a 4e 61 6d 65 2c 20 2f 2a 20 4e 61 6d 65   *zName, /* Name
25699 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
2569a 2e 20 20 4e 6f 74 20 6e 75 6c 6c 2d 74 65 72 6d  .  Not null-term
2569b 69 6e 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20  inated */.  int 
2569c 6e 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20 2f  nName,         /
2569d 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72  * Number of char
2569e 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 6e 61  acters in the na
2569f 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67  me */.  int nArg
256a0 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ,          /* Nu
256a1 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
256a2 73 2e 20 20 2d 31 20 6d 65 61 6e 73 20 61 6e 79  s.  -1 means any
256a3 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 75 38 20   number */.  u8 
256a4 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  enc,            
256a5 2f 2a 20 50 72 65 66 65 72 72 65 64 20 74 65 78  /* Preferred tex
256a6 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20  t encoding */.  
256a7 69 6e 74 20 63 72 65 61 74 65 46 6c 61 67 20 20  int createFlag  
256a8 20 20 20 2f 2a 20 43 72 65 61 74 65 20 6e 65 77     /* Create new
256a9 20 65 6e 74 72 79 20 69 66 20 74 72 75 65 20 61   entry if true a
256aa 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6f 74 68 65  nd does not othe
256ab 72 77 69 73 65 20 65 78 69 73 74 20 2a 2f 0a 29  rwise exist */.)
256ac 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 3b 20  {.  FuncDef *p; 
256ad 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61          /* Itera
256ae 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a  tor variable */.
256af 20 20 46 75 6e 63 44 65 66 20 2a 70 42 65 73 74    FuncDef *pBest
256b0 20 3d 20 30 3b 20 2f 2a 20 42 65 73 74 20 6d 61   = 0; /* Best ma
256b1 74 63 68 20 66 6f 75 6e 64 20 73 6f 20 66 61 72  tch found so far
256b2 20 2a 2f 0a 20 20 69 6e 74 20 62 65 73 74 53 63   */.  int bestSc
256b3 6f 72 65 20 3d 20 30 3b 20 20 2f 2a 20 53 63 6f  ore = 0;  /* Sco
256b4 72 65 20 6f 66 20 62 65 73 74 20 6d 61 74 63 68  re of best match
256b5 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20 20   */.  int h;    
256b6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73            /* Has
256b7 68 20 76 61 6c 75 65 20 2a 2f 0a 0a 0a 20 20 61  h value */...  a
256b8 73 73 65 72 74 28 20 65 6e 63 3d 3d 53 51 4c 49  ssert( enc==SQLI
256b9 54 45 5f 55 54 46 38 20 7c 7c 20 65 6e 63 3d 3d  TE_UTF8 || enc==
256ba 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 7c  SQLITE_UTF16LE |
256bb 7c 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  | enc==SQLITE_UT
256bc 46 31 36 42 45 20 29 3b 0a 20 20 68 20 3d 20 28  F16BE );.  h = (
256bd 73 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f  sqlite3UpperToLo
256be 77 65 72 5b 28 75 38 29 7a 4e 61 6d 65 5b 30 5d  wer[(u8)zName[0]
256bf 5d 20 2b 20 6e 4e 61 6d 65 29 20 25 20 41 72 72  ] + nName) % Arr
256c0 61 79 53 69 7a 65 28 64 62 2d 3e 61 46 75 6e 63  aySize(db->aFunc
256c1 2e 61 29 3b 0a 0a 20 20 2f 2a 20 46 69 72 73 74  .a);..  /* First
256c2 20 73 65 61 72 63 68 20 66 6f 72 20 61 20 6d 61   search for a ma
256c3 74 63 68 20 61 6d 6f 6e 67 73 74 20 74 68 65 20  tch amongst the 
256c4 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
256c5 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20  ned functions.. 
256c6 20 2a 2f 0a 20 20 70 20 3d 20 66 75 6e 63 74 69   */.  p = functi
256c7 6f 6e 53 65 61 72 63 68 28 26 64 62 2d 3e 61 46  onSearch(&db->aF
256c8 75 6e 63 2c 20 68 2c 20 7a 4e 61 6d 65 2c 20 6e  unc, h, zName, n
256c9 4e 61 6d 65 29 3b 0a 20 20 77 68 69 6c 65 28 20  Name);.  while( 
256ca 70 20 29 7b 0a 20 20 20 20 69 6e 74 20 73 63 6f  p ){.    int sco
256cb 72 65 20 3d 20 6d 61 74 63 68 51 75 61 6c 69 74  re = matchQualit
256cc 79 28 70 2c 20 6e 41 72 67 2c 20 65 6e 63 29 3b  y(p, nArg, enc);
256cd 0a 20 20 20 20 69 66 28 20 73 63 6f 72 65 3e 62  .    if( score>b
256ce 65 73 74 53 63 6f 72 65 20 29 7b 0a 20 20 20 20  estScore ){.    
256cf 20 20 70 42 65 73 74 20 3d 20 70 3b 0a 20 20 20    pBest = p;.   
256d0 20 20 20 62 65 73 74 53 63 6f 72 65 20 3d 20 73     bestScore = s
256d1 63 6f 72 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20  core;.    }.    
256d2 70 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20  p = p->pNext;.  
256d3 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 6d 61  }..  /* If no ma
256d4 74 63 68 20 69 73 20 66 6f 75 6e 64 2c 20 73 65  tch is found, se
256d5 61 72 63 68 20 74 68 65 20 62 75 69 6c 74 2d 69  arch the built-i
256d6 6e 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a  n functions..  *
256d7 2a 0a 20 20 2a 2a 20 45 78 63 65 70 74 2c 20 69  *.  ** Except, i
256d8 66 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 20  f createFlag is 
256d9 74 72 75 65 2c 20 74 68 61 74 20 6d 65 61 6e 73  true, that means
256da 20 74 68 61 74 20 77 65 20 61 72 65 20 74 72 79   that we are try
256db 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 69 6e 73 74  ing to.  ** inst
256dc 61 6c 6c 20 61 20 6e 65 77 20 66 75 6e 63 74 69  all a new functi
256dd 6f 6e 2e 20 20 57 68 61 74 65 76 65 72 20 46 75  on.  Whatever Fu
256de 6e 63 44 65 66 20 73 74 72 75 63 74 75 72 65 20  ncDef structure 
256df 69 73 20 72 65 74 75 72 6e 65 64 20 77 69 6c 6c  is returned will
256e0 0a 20 20 2a 2a 20 68 61 76 65 20 66 69 65 6c 64  .  ** have field
256e1 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69  s overwritten wi
256e2 74 68 20 6e 65 77 20 69 6e 66 6f 72 6d 61 74 69  th new informati
256e3 6f 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 66  on appropriate f
256e4 6f 72 20 74 68 65 0a 20 20 2a 2a 20 6e 65 77 20  or the.  ** new 
256e5 66 75 6e 63 74 69 6f 6e 2e 20 20 42 75 74 20 74  function.  But t
256e6 68 65 20 46 75 6e 63 44 65 66 73 20 66 6f 72 20  he FuncDefs for 
256e7 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f  built-in functio
256e8 6e 73 20 61 72 65 20 72 65 61 64 2d 6f 6e 6c 79  ns are read-only
256e9 2e 0a 20 20 2a 2a 20 53 6f 20 77 65 20 6d 75 73  ..  ** So we mus
256ea 74 20 6e 6f 74 20 73 65 61 72 63 68 20 66 6f 72  t not search for
256eb 20 62 75 69 6c 74 2d 69 6e 73 20 77 68 65 6e 20   built-ins when 
256ec 63 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20 66  creating a new f
256ed 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 20 0a 20  unction..  */ . 
256ee 20 69 66 28 20 21 63 72 65 61 74 65 46 6c 61 67   if( !createFlag
256ef 20 26 26 20 21 70 42 65 73 74 20 29 7b 0a 20 20   && !pBest ){.  
256f0 20 20 46 75 6e 63 44 65 66 48 61 73 68 20 2a 70    FuncDefHash *p
256f1 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c 28 46  Hash = &GLOBAL(F
256f2 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71 6c 69  uncDefHash, sqli
256f3 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74 69 6f  te3GlobalFunctio
256f4 6e 73 29 3b 0a 20 20 20 20 70 20 3d 20 66 75 6e  ns);.    p = fun
256f5 63 74 69 6f 6e 53 65 61 72 63 68 28 70 48 61 73  ctionSearch(pHas
256f6 68 2c 20 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  h, h, zName, nNa
256f7 6d 65 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  me);.    while( 
256f8 70 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73  p ){.      int s
256f9 63 6f 72 65 20 3d 20 6d 61 74 63 68 51 75 61 6c  core = matchQual
256fa 69 74 79 28 70 2c 20 6e 41 72 67 2c 20 65 6e 63  ity(p, nArg, enc
256fb 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73 63 6f  );.      if( sco
256fc 72 65 3e 62 65 73 74 53 63 6f 72 65 20 29 7b 0a  re>bestScore ){.
256fd 20 20 20 20 20 20 20 20 70 42 65 73 74 20 3d 20          pBest = 
256fe 70 3b 0a 20 20 20 20 20 20 20 20 62 65 73 74 53  p;.        bestS
256ff 63 6f 72 65 20 3d 20 73 63 6f 72 65 3b 0a 20 20  core = score;.  
25700 20 20 20 20 7d 0a 20 20 20 20 20 20 70 20 3d 20      }.      p = 
25701 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a  p->pNext;.    }.
25702 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
25703 20 63 72 65 61 74 65 46 6c 61 67 20 70 61 72 61   createFlag para
25704 6d 65 74 65 72 20 69 73 20 74 72 75 65 20 61 6e  meter is true an
25705 64 20 74 68 65 20 73 65 61 72 63 68 20 64 69 64  d the search did
25706 20 6e 6f 74 20 72 65 76 65 61 6c 20 61 6e 0a 20   not reveal an. 
25707 20 2a 2a 20 65 78 61 63 74 20 6d 61 74 63 68 20   ** exact match 
25708 66 6f 72 20 74 68 65 20 6e 61 6d 65 2c 20 6e 75  for the name, nu
25709 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
2570a 73 20 61 6e 64 20 65 6e 63 6f 64 69 6e 67 2c 20  s and encoding, 
2570b 74 68 65 6e 20 61 64 64 20 61 0a 20 20 2a 2a 20  then add a.  ** 
2570c 6e 65 77 20 65 6e 74 72 79 20 74 6f 20 74 68 65  new entry to the
2570d 20 68 61 73 68 20 74 61 62 6c 65 20 61 6e 64 20   hash table and 
2570e 72 65 74 75 72 6e 20 69 74 2e 0a 20 20 2a 2f 0a  return it..  */.
2570f 20 20 69 66 28 20 63 72 65 61 74 65 46 6c 61 67    if( createFlag
25710 20 26 26 20 28 62 65 73 74 53 63 6f 72 65 3c 36   && (bestScore<6
25711 20 7c 7c 20 70 42 65 73 74 2d 3e 6e 41 72 67 21   || pBest->nArg!
25712 3d 6e 41 72 67 29 20 26 26 20 0a 20 20 20 20 20  =nArg) && .     
25713 20 28 70 42 65 73 74 20 3d 20 73 71 6c 69 74 65   (pBest = sqlite
25714 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
25715 2c 20 73 69 7a 65 6f 66 28 2a 70 42 65 73 74 29  , sizeof(*pBest)
25716 2b 6e 4e 61 6d 65 2b 31 29 29 21 3d 30 20 29 7b  +nName+1))!=0 ){
25717 0a 20 20 20 20 70 42 65 73 74 2d 3e 7a 4e 61 6d  .    pBest->zNam
25718 65 20 3d 20 28 63 68 61 72 20 2a 29 26 70 42 65  e = (char *)&pBe
25719 73 74 5b 31 5d 3b 0a 20 20 20 20 70 42 65 73 74  st[1];.    pBest
2571a 2d 3e 6e 41 72 67 20 3d 20 28 75 31 36 29 6e 41  ->nArg = (u16)nA
2571b 72 67 3b 0a 20 20 20 20 70 42 65 73 74 2d 3e 69  rg;.    pBest->i
2571c 50 72 65 66 45 6e 63 20 3d 20 65 6e 63 3b 0a 20  PrefEnc = enc;. 
2571d 20 20 20 6d 65 6d 63 70 79 28 70 42 65 73 74 2d     memcpy(pBest-
2571e 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 6e  >zName, zName, n
2571f 4e 61 6d 65 29 3b 0a 20 20 20 20 70 42 65 73 74  Name);.    pBest
25720 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d  ->zName[nName] =
25721 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 46   0;.    sqlite3F
25722 75 6e 63 44 65 66 49 6e 73 65 72 74 28 26 64 62  uncDefInsert(&db
25723 2d 3e 61 46 75 6e 63 2c 20 70 42 65 73 74 29 3b  ->aFunc, pBest);
25724 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 42 65 73  .  }..  if( pBes
25725 74 20 26 26 20 28 70 42 65 73 74 2d 3e 78 53 74  t && (pBest->xSt
25726 65 70 20 7c 7c 20 70 42 65 73 74 2d 3e 78 46 75  ep || pBest->xFu
25727 6e 63 20 7c 7c 20 63 72 65 61 74 65 46 6c 61 67  nc || createFlag
25728 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
25729 70 42 65 73 74 3b 0a 20 20 7d 0a 20 20 72 65 74  pBest;.  }.  ret
2572a 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
2572b 46 72 65 65 20 61 6c 6c 20 72 65 73 6f 75 72 63  Free all resourc
2572c 65 73 20 68 65 6c 64 20 62 79 20 74 68 65 20 73  es held by the s
2572d 63 68 65 6d 61 20 73 74 72 75 63 74 75 72 65 2e  chema structure.
2572e 20 54 68 65 20 76 6f 69 64 2a 20 61 72 67 75 6d   The void* argum
2572f 65 6e 74 20 70 6f 69 6e 74 73 0a 2a 2a 20 61 74  ent points.** at
25730 20 61 20 53 63 68 65 6d 61 20 73 74 72 75 63 74   a Schema struct
25731 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
25732 64 6f 65 73 20 6e 6f 74 20 63 61 6c 6c 20 73 71  does not call sq
25733 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
25734 29 20 6f 6e 20 74 68 65 20 0a 2a 2a 20 70 6f 69  ) on the .** poi
25735 6e 74 65 72 20 69 74 73 65 6c 66 2c 20 69 74 20  nter itself, it 
25736 6a 75 73 74 20 63 6c 65 61 6e 73 20 75 70 20 73  just cleans up s
25737 75 62 73 69 64 75 61 72 79 20 72 65 73 6f 75 72  ubsiduary resour
25738 63 65 73 20 28 69 2e 65 2e 20 74 68 65 20 63 6f  ces (i.e. the co
25739 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65  ntents.** of the
2573a 20 73 63 68 65 6d 61 20 68 61 73 68 20 74 61 62   schema hash tab
2573b 6c 65 73 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  les)..**.** The 
2573c 53 63 68 65 6d 61 2e 63 61 63 68 65 5f 73 69 7a  Schema.cache_siz
2573d 65 20 76 61 72 69 61 62 6c 65 20 69 73 20 6e 6f  e variable is no
2573e 74 20 63 6c 65 61 72 65 64 2e 0a 2a 2f 0a 53 51  t cleared..*/.SQ
2573f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
25740 64 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 46  d sqlite3SchemaF
25741 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20  ree(void *p){.  
25742 48 61 73 68 20 74 65 6d 70 31 3b 0a 20 20 48 61  Hash temp1;.  Ha
25743 73 68 20 74 65 6d 70 32 3b 0a 20 20 48 61 73 68  sh temp2;.  Hash
25744 45 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0a 20 20 53  Elem *pElem;.  S
25745 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d  chema *pSchema =
25746 20 28 53 63 68 65 6d 61 20 2a 29 70 3b 0a 0a 20   (Schema *)p;.. 
25747 20 74 65 6d 70 31 20 3d 20 70 53 63 68 65 6d 61   temp1 = pSchema
25748 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20 74 65 6d  ->tblHash;.  tem
25749 70 32 20 3d 20 70 53 63 68 65 6d 61 2d 3e 74 72  p2 = pSchema->tr
2574a 69 67 48 61 73 68 3b 0a 20 20 73 71 6c 69 74 65  igHash;.  sqlite
2574b 33 48 61 73 68 49 6e 69 74 28 26 70 53 63 68 65  3HashInit(&pSche
2574c 6d 61 2d 3e 74 72 69 67 48 61 73 68 29 3b 0a 20  ma->trigHash);. 
2574d 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61   sqlite3HashClea
2574e 72 28 26 70 53 63 68 65 6d 61 2d 3e 69 64 78 48  r(&pSchema->idxH
2574f 61 73 68 29 3b 0a 20 20 66 6f 72 28 70 45 6c 65  ash);.  for(pEle
25750 6d 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73  m=sqliteHashFirs
25751 74 28 26 74 65 6d 70 32 29 3b 20 70 45 6c 65 6d  t(&temp2); pElem
25752 3b 20 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61  ; pElem=sqliteHa
25753 73 68 4e 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a  shNext(pElem)){.
25754 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74      sqlite3Delet
25755 65 54 72 69 67 67 65 72 28 30 2c 20 28 54 72 69  eTrigger(0, (Tri
25756 67 67 65 72 2a 29 73 71 6c 69 74 65 48 61 73 68  gger*)sqliteHash
25757 44 61 74 61 28 70 45 6c 65 6d 29 29 3b 0a 20 20  Data(pElem));.  
25758 7d 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 43  }.  sqlite3HashC
25759 6c 65 61 72 28 26 74 65 6d 70 32 29 3b 0a 20 20  lear(&temp2);.  
2575a 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28  sqlite3HashInit(
2575b 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73  &pSchema->tblHas
2575c 68 29 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d  h);.  for(pElem=
2575d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28  sqliteHashFirst(
2575e 26 74 65 6d 70 31 29 3b 20 70 45 6c 65 6d 3b 20  &temp1); pElem; 
2575f 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68  pElem=sqliteHash
25760 4e 65 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20  Next(pElem)){.  
25761 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
25762 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70  sqliteHashData(p
25763 45 6c 65 6d 29 3b 0a 20 20 20 20 61 73 73 65 72  Elem);.    asser
25764 74 28 20 70 54 61 62 2d 3e 64 62 4d 65 6d 3d 3d  t( pTab->dbMem==
25765 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 );.    sqlite3
25766 44 65 6c 65 74 65 54 61 62 6c 65 28 70 54 61 62  DeleteTable(pTab
25767 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
25768 48 61 73 68 43 6c 65 61 72 28 26 74 65 6d 70 31  HashClear(&temp1
25769 29 3b 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68  );.  sqlite3Hash
2576a 43 6c 65 61 72 28 26 70 53 63 68 65 6d 61 2d 3e  Clear(&pSchema->
2576b 66 6b 65 79 48 61 73 68 29 3b 0a 20 20 70 53 63  fkeyHash);.  pSc
2576c 68 65 6d 61 2d 3e 70 53 65 71 54 61 62 20 3d 20  hema->pSeqTab = 
2576d 30 3b 0a 20 20 70 53 63 68 65 6d 61 2d 3e 66 6c  0;.  pSchema->fl
2576e 61 67 73 20 26 3d 20 7e 44 42 5f 53 63 68 65 6d  ags &= ~DB_Schem
2576f 61 4c 6f 61 64 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a  aLoaded;.}../*.*
25770 2a 20 46 69 6e 64 20 61 6e 64 20 72 65 74 75 72  * Find and retur
25771 6e 20 74 68 65 20 73 63 68 65 6d 61 20 61 73 73  n the schema ass
25772 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20 42  ociated with a B
25773 54 72 65 65 2e 20 20 43 72 65 61 74 65 0a 2a 2a  Tree.  Create.**
25774 20 61 20 6e 65 77 20 6f 6e 65 20 69 66 20 6e 65   a new one if ne
25775 63 65 73 73 61 72 79 2e 0a 2a 2f 0a 53 51 4c 49  cessary..*/.SQLI
25776 54 45 5f 50 52 49 56 41 54 45 20 53 63 68 65 6d  TE_PRIVATE Schem
25777 61 20 2a 73 71 6c 69 74 65 33 53 63 68 65 6d 61  a *sqlite3Schema
25778 47 65 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  Get(sqlite3 *db,
25779 20 42 74 72 65 65 20 2a 70 42 74 29 7b 0a 20 20   Btree *pBt){.  
2577a 53 63 68 65 6d 61 20 2a 20 70 3b 0a 20 20 69 66  Schema * p;.  if
2577b 28 20 70 42 74 20 29 7b 0a 20 20 20 20 70 20 3d  ( pBt ){.    p =
2577c 20 28 53 63 68 65 6d 61 20 2a 29 73 71 6c 69 74   (Schema *)sqlit
2577d 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 70 42  e3BtreeSchema(pB
2577e 74 2c 20 73 69 7a 65 6f 66 28 53 63 68 65 6d 61  t, sizeof(Schema
2577f 29 2c 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61  ), sqlite3Schema
25780 46 72 65 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Free);.  }else{.
25781 20 20 20 20 70 20 3d 20 28 53 63 68 65 6d 61 20      p = (Schema 
25782 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a  *)sqlite3MallocZ
25783 65 72 6f 28 73 69 7a 65 6f 66 28 53 63 68 65 6d  ero(sizeof(Schem
25784 61 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  a));.  }.  if( !
25785 70 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c  p ){.    db->mal
25786 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
25787 20 7d 65 6c 73 65 20 69 66 20 28 20 30 3d 3d 70   }else if ( 0==p
25788 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20 29 7b  ->file_format ){
25789 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68  .    sqlite3Hash
2578a 49 6e 69 74 28 26 70 2d 3e 74 62 6c 48 61 73 68  Init(&p->tblHash
2578b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61  );.    sqlite3Ha
2578c 73 68 49 6e 69 74 28 26 70 2d 3e 69 64 78 48 61  shInit(&p->idxHa
2578d 73 68 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  sh);.    sqlite3
2578e 48 61 73 68 49 6e 69 74 28 26 70 2d 3e 74 72 69  HashInit(&p->tri
2578f 67 48 61 73 68 29 3b 0a 20 20 20 20 73 71 6c 69  gHash);.    sqli
25790 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 2d 3e  te3HashInit(&p->
25791 66 6b 65 79 48 61 73 68 29 3b 0a 20 20 20 20 70  fkeyHash);.    p
25792 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55  ->enc = SQLITE_U
25793 54 46 38 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  TF8;.  }.  retur
25794 6e 20 70 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  n p;.}../*******
25795 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 63  ******* End of c
25796 61 6c 6c 62 61 63 6b 2e 63 20 2a 2a 2a 2a 2a 2a  allback.c ******
25797 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25798 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25799 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
2579a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
2579b 6c 65 20 64 65 6c 65 74 65 2e 63 20 2a 2a 2a 2a  le delete.c ****
2579c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2579d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2579e 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
2579f 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
257a0 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
257a1 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
257a2 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
257a3 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
257a4 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
257a5 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
257a6 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
257a7 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
257a8 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
257a9 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
257aa 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
257ab 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
257ac 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
257ad 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
257ae 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
257af 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
257b0 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
257b1 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
257b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
257b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
257b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
257b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
257b6 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
257b7 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75 74  ains C code rout
257b8 69 6e 65 73 20 74 68 61 74 20 61 72 65 20 63 61  ines that are ca
257b9 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73  lled by the pars
257ba 65 72 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74  er.** in order t
257bb 6f 20 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20  o generate code 
257bc 66 6f 72 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  for DELETE FROM 
257bd 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a  statements..**.*
257be 2a 20 24 49 64 3a 20 64 65 6c 65 74 65 2e 63 2c  * $Id: delete.c,
257bf 76 20 31 2e 32 30 37 20 32 30 30 39 2f 30 38 2f  v 1.207 2009/08/
257c0 30 38 20 31 38 3a 30 31 3a 30 38 20 64 72 68 20  08 18:01:08 drh 
257c1 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
257c2 4c 6f 6f 6b 20 75 70 20 65 76 65 72 79 20 74 61  Look up every ta
257c3 62 6c 65 20 74 68 61 74 20 69 73 20 6e 61 6d 65  ble that is name
257c4 64 20 69 6e 20 70 53 72 63 2e 20 20 49 66 20 61  d in pSrc.  If a
257c5 6e 79 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20  ny table is not 
257c6 66 6f 75 6e 64 2c 0a 2a 2a 20 61 64 64 20 61 6e  found,.** add an
257c7 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74   error message t
257c8 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73  o pParse->zErrMs
257c9 67 20 61 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c  g and return NUL
257ca 4c 2e 20 20 49 66 20 61 6c 6c 20 74 61 62 6c 65  L.  If all table
257cb 73 0a 2a 2a 20 61 72 65 20 66 6f 75 6e 64 2c 20  s.** are found, 
257cc 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  return a pointer
257cd 20 74 6f 20 74 68 65 20 6c 61 73 74 20 74 61 62   to the last tab
257ce 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
257cf 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c  IVATE Table *sql
257d0 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75  ite3SrcListLooku
257d1 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  p(Parse *pParse,
257d2 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 29 7b   SrcList *pSrc){
257d3 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
257d4 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20  t_item *pItem = 
257d5 70 53 72 63 2d 3e 61 3b 0a 20 20 54 61 62 6c 65  pSrc->a;.  Table
257d6 20 2a 70 54 61 62 3b 0a 20 20 61 73 73 65 72 74   *pTab;.  assert
257d7 28 20 70 49 74 65 6d 20 26 26 20 70 53 72 63 2d  ( pItem && pSrc-
257d8 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 70 54  >nSrc==1 );.  pT
257d9 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61  ab = sqlite3Loca
257da 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  teTable(pParse, 
257db 30 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 2c  0, pItem->zName,
257dc 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73   pItem->zDatabas
257dd 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 6c  e);.  sqlite3Del
257de 65 74 65 54 61 62 6c 65 28 70 49 74 65 6d 2d 3e  eteTable(pItem->
257df 70 54 61 62 29 3b 0a 20 20 70 49 74 65 6d 2d 3e  pTab);.  pItem->
257e0 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20 69  pTab = pTab;.  i
257e1 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20 20 70  f( pTab ){.    p
257e2 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 7d  Tab->nRef++;.  }
257e3 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 6e  .  if( sqlite3In
257e4 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70 28 70 50  dexedByLookup(pP
257e5 61 72 73 65 2c 20 70 49 74 65 6d 29 20 29 7b 0a  arse, pItem) ){.
257e6 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20      pTab = 0;.  
257e7 7d 0a 20 20 72 65 74 75 72 6e 20 70 54 61 62 3b  }.  return pTab;
257e8 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  .}../*.** Check 
257e9 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65  to make sure the
257ea 20 67 69 76 65 6e 20 74 61 62 6c 65 20 69 73 20   given table is 
257eb 77 72 69 74 61 62 6c 65 2e 20 20 49 66 20 69 74  writable.  If it
257ec 20 69 73 20 6e 6f 74 0a 2a 2a 20 77 72 69 74 61   is not.** writa
257ed 62 6c 65 2c 20 67 65 6e 65 72 61 74 65 20 61 6e  ble, generate an
257ee 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 61   error message a
257ef 6e 64 20 72 65 74 75 72 6e 20 31 2e 20 20 49 66  nd return 1.  If
257f0 20 69 74 20 69 73 0a 2a 2a 20 77 72 69 74 61 62   it is.** writab
257f1 6c 65 20 72 65 74 75 72 6e 20 30 3b 0a 2a 2f 0a  le return 0;.*/.
257f2 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
257f3 6e 74 20 73 71 6c 69 74 65 33 49 73 52 65 61 64  nt sqlite3IsRead
257f4 4f 6e 6c 79 28 50 61 72 73 65 20 2a 70 50 61 72  Only(Parse *pPar
257f5 73 65 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  se, Table *pTab,
257f6 20 69 6e 74 20 76 69 65 77 4f 6b 29 7b 0a 20 20   int viewOk){.  
257f7 2f 2a 20 41 20 74 61 62 6c 65 20 69 73 20 6e 6f  /* A table is no
257f8 74 20 77 72 69 74 61 62 6c 65 20 75 6e 64 65 72  t writable under
257f9 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   the following c
257fa 69 72 63 75 6d 73 74 61 6e 63 65 73 3a 0a 20 20  ircumstances:.  
257fb 2a 2a 0a 20 20 2a 2a 20 20 20 31 29 20 49 74 20  **.  **   1) It 
257fc 69 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  is a virtual tab
257fd 6c 65 20 61 6e 64 20 6e 6f 20 69 6d 70 6c 65 6d  le and no implem
257fe 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
257ff 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64 0a 20  xUpdate method. 
25800 20 2a 2a 20 20 20 20 20 20 68 61 73 20 62 65 65   **      has bee
25801 6e 20 70 72 6f 76 69 64 65 64 2c 20 6f 72 0a 20  n provided, or. 
25802 20 2a 2a 20 20 20 32 29 20 49 74 20 69 73 20 61   **   2) It is a
25803 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 28 69   system table (i
25804 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  .e. sqlite_maste
25805 72 29 2c 20 74 68 69 73 20 63 61 6c 6c 20 69 73  r), this call is
25806 20 6e 6f 74 0a 20 20 2a 2a 20 20 20 20 20 20 70   not.  **      p
25807 61 72 74 20 6f 66 20 61 20 6e 65 73 74 65 64 20  art of a nested 
25808 70 61 72 73 65 20 61 6e 64 20 77 72 69 74 61 62  parse and writab
25809 6c 65 5f 73 63 68 65 6d 61 20 70 72 61 67 6d 61  le_schema pragma
2580a 20 68 61 73 20 6e 6f 74 20 0a 20 20 2a 2a 20 20   has not .  **  
2580b 20 20 20 20 62 65 65 6e 20 73 70 65 63 69 66 69      been specifi
2580c 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e  ed..  **.  ** In
2580d 20 65 69 74 68 65 72 20 63 61 73 65 20 6c 65 61   either case lea
2580e 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  ve an error mess
2580f 61 67 65 20 69 6e 20 70 50 61 72 73 65 20 61 6e  age in pParse an
25810 64 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72  d return non-zer
25811 6f 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 20  o..  */.  if( ( 
25812 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
25813 0a 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33  .     && sqlite3
25814 47 65 74 56 54 61 62 6c 65 28 70 50 61 72 73 65  GetVTable(pParse
25815 2d 3e 64 62 2c 20 70 54 61 62 29 2d 3e 70 4d 6f  ->db, pTab)->pMo
25816 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64  d->pModule->xUpd
25817 61 74 65 3d 3d 30 20 29 0a 20 20 20 7c 7c 20 28  ate==0 ).   || (
25818 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73   (pTab->tabFlags
25819 20 26 20 54 46 5f 52 65 61 64 6f 6e 6c 79 29 21   & TF_Readonly)!
2581a 3d 30 0a 20 20 20 20 20 26 26 20 28 70 50 61 72  =0.     && (pPar
2581b 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20  se->db->flags & 
2581c 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65  SQLITE_WriteSche
2581d 6d 61 29 3d 3d 30 0a 20 20 20 20 20 26 26 20 70  ma)==0.     && p
2581e 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30  Parse->nested==0
2581f 20 29 0a 20 20 29 7b 0a 20 20 20 20 73 71 6c 69   ).  ){.    sqli
25820 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
25821 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20 6d 61  se, "table %s ma
25822 79 20 6e 6f 74 20 62 65 20 6d 6f 64 69 66 69 65  y not be modifie
25823 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  d", pTab->zName)
25824 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  ;.    return 1;.
25825 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
25826 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20  ITE_OMIT_VIEW.  
25827 69 66 28 20 21 76 69 65 77 4f 6b 20 26 26 20 70  if( !viewOk && p
25828 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  Tab->pSelect ){.
25829 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2582a 4d 73 67 28 70 50 61 72 73 65 2c 22 63 61 6e 6e  Msg(pParse,"cann
2582b 6f 74 20 6d 6f 64 69 66 79 20 25 73 20 62 65 63  ot modify %s bec
2582c 61 75 73 65 20 69 74 20 69 73 20 61 20 76 69 65  ause it is a vie
2582d 77 22 2c 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  w",pTab->zName);
2582e 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
2582f 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75   }.#endif.  retu
25830 72 6e 20 30 3b 0a 7d 0a 0a 0a 23 69 66 20 21 64  rn 0;.}...#if !d
25831 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
25832 49 54 5f 56 49 45 57 29 20 26 26 20 21 64 65 66  IT_VIEW) && !def
25833 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
25834 5f 54 52 49 47 47 45 52 29 0a 2f 2a 0a 2a 2a 20  _TRIGGER)./*.** 
25835 45 76 61 6c 75 61 74 65 20 61 20 76 69 65 77 20  Evaluate a view 
25836 61 6e 64 20 73 74 6f 72 65 20 69 74 73 20 72 65  and store its re
25837 73 75 6c 74 20 69 6e 20 61 6e 20 65 70 68 65 6d  sult in an ephem
25838 65 72 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65  eral table.  The
25839 0a 2a 2a 20 70 57 68 65 72 65 20 61 72 67 75 6d  .** pWhere argum
2583a 65 6e 74 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e  ent is an option
2583b 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  al WHERE clause 
2583c 74 68 61 74 20 72 65 73 74 72 69 63 74 73 20 74  that restricts t
2583d 68 65 0a 2a 2a 20 73 65 74 20 6f 66 20 72 6f 77  he.** set of row
2583e 73 20 69 6e 20 74 68 65 20 76 69 65 77 20 74 68  s in the view th
2583f 61 74 20 61 72 65 20 74 6f 20 62 65 20 61 64 64  at are to be add
25840 65 64 20 74 6f 20 74 68 65 20 65 70 68 65 6d 65  ed to the epheme
25841 72 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51  ral table..*/.SQ
25842 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
25843 64 20 73 71 6c 69 74 65 33 4d 61 74 65 72 69 61  d sqlite3Materia
25844 6c 69 7a 65 56 69 65 77 28 0a 20 20 50 61 72 73  lizeView(.  Pars
25845 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
25846 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
25847 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
25848 70 56 69 65 77 2c 20 20 20 20 20 20 20 20 2f 2a  pView,        /*
25849 20 56 69 65 77 20 64 65 66 69 6e 69 74 69 6f 6e   View definition
2584a 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65   */.  Expr *pWhe
2584b 72 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 4f 70  re,        /* Op
2584c 74 69 6f 6e 61 6c 20 57 48 45 52 45 20 63 6c 61  tional WHERE cla
2584d 75 73 65 20 74 6f 20 62 65 20 61 64 64 65 64 20  use to be added 
2584e 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 20 20 20  */.  int iCur   
2584f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
25850 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 65  sor number for e
25851 70 68 65 6d 65 72 69 61 6c 20 74 61 62 6c 65 20  phemerial table 
25852 2a 2f 0a 29 7b 0a 20 20 53 65 6c 65 63 74 44 65  */.){.  SelectDe
25853 73 74 20 64 65 73 74 3b 0a 20 20 53 65 6c 65 63  st dest;.  Selec
25854 74 20 2a 70 44 75 70 3b 0a 20 20 73 71 6c 69 74  t *pDup;.  sqlit
25855 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
25856 3e 64 62 3b 0a 0a 20 20 70 44 75 70 20 3d 20 73  >db;..  pDup = s
25857 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28  qlite3SelectDup(
25858 64 62 2c 20 70 56 69 65 77 2d 3e 70 53 65 6c 65  db, pView->pSele
25859 63 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 57  ct, 0);.  if( pW
2585a 68 65 72 65 20 29 7b 0a 20 20 20 20 53 72 63 4c  here ){.    SrcL
2585b 69 73 74 20 2a 70 46 72 6f 6d 3b 0a 20 20 20 20  ist *pFrom;.    
2585c 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20 73 71  .    pWhere = sq
2585d 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
2585e 20 70 57 68 65 72 65 2c 20 30 29 3b 0a 20 20 20   pWhere, 0);.   
2585f 20 70 46 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33   pFrom = sqlite3
25860 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28 64 62  SrcListAppend(db
25861 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
25862 69 66 28 20 70 46 72 6f 6d 20 29 7b 0a 20 20 20  if( pFrom ){.   
25863 20 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d     assert( pFrom
25864 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 20  ->nSrc==1 );.   
25865 20 20 20 70 46 72 6f 6d 2d 3e 61 5b 30 5d 2e 7a     pFrom->a[0].z
25866 41 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33 44  Alias = sqlite3D
25867 62 53 74 72 44 75 70 28 64 62 2c 20 70 56 69 65  bStrDup(db, pVie
25868 77 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  w->zName);.     
25869 20 70 46 72 6f 6d 2d 3e 61 5b 30 5d 2e 70 53 65   pFrom->a[0].pSe
2586a 6c 65 63 74 20 3d 20 70 44 75 70 3b 0a 20 20 20  lect = pDup;.   
2586b 20 20 20 61 73 73 65 72 74 28 20 70 46 72 6f 6d     assert( pFrom
2586c 2d 3e 61 5b 30 5d 2e 70 4f 6e 3d 3d 30 20 29 3b  ->a[0].pOn==0 );
2586d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2586e 46 72 6f 6d 2d 3e 61 5b 30 5d 2e 70 55 73 69 6e  From->a[0].pUsin
2586f 67 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73  g==0 );.    }els
25870 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
25871 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
25872 20 70 44 75 70 29 3b 0a 20 20 20 20 7d 0a 20 20   pDup);.    }.  
25873 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74 65 33    pDup = sqlite3
25874 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65  SelectNew(pParse
25875 2c 20 30 2c 20 70 46 72 6f 6d 2c 20 70 57 68 65  , 0, pFrom, pWhe
25876 72 65 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20  re, 0, 0, 0, 0, 
25877 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  0, 0);.  }.  sql
25878 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e  ite3SelectDestIn
25879 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f 45 70  it(&dest, SRT_Ep
2587a 68 65 6d 54 61 62 2c 20 69 43 75 72 29 3b 0a 20  hemTab, iCur);. 
2587b 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
2587c 50 61 72 73 65 2c 20 70 44 75 70 2c 20 26 64 65  Parse, pDup, &de
2587d 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  st);.  sqlite3Se
2587e 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
2587f 44 75 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  Dup);.}.#endif /
25880 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  * !defined(SQLIT
25881 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26 26 20  E_OMIT_VIEW) && 
25882 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
25883 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20 2a 2f  OMIT_TRIGGER) */
25884 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ..#if defined(SQ
25885 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44 41  LITE_ENABLE_UPDA
25886 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54 29  TE_DELETE_LIMIT)
25887 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
25888 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
25889 59 29 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  Y)./*.** Generat
2588a 65 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  e an expression 
2588b 74 72 65 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  tree to implemen
2588c 74 20 74 68 65 20 57 48 45 52 45 2c 20 4f 52 44  t the WHERE, ORD
2588d 45 52 20 42 59 2c 0a 2a 2a 20 61 6e 64 20 4c 49  ER BY,.** and LI
2588e 4d 49 54 2f 4f 46 46 53 45 54 20 70 6f 72 74 69  MIT/OFFSET porti
2588f 6f 6e 20 6f 66 20 44 45 4c 45 54 45 20 61 6e 64  on of DELETE and
25890 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
25891 74 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 45  ts..**.**     DE
25892 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f  LETE FROM table_
25893 77 78 79 7a 20 57 48 45 52 45 20 61 3c 35 20 4f  wxyz WHERE a<5 O
25894 52 44 45 52 20 42 59 20 61 20 4c 49 4d 49 54 20  RDER BY a LIMIT 
25895 31 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  1;.**           
25896 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25897 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f   \______________
25898 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a  ____________/.**
25899 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2589a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
2589b 4c 69 6d 69 74 57 68 65 72 65 20 28 70 49 6e 43  LimitWhere (pInC
2589c 6c 61 75 73 65 29 0a 2a 2f 0a 53 51 4c 49 54 45  lause).*/.SQLITE
2589d 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73  _PRIVATE Expr *s
2589e 71 6c 69 74 65 33 4c 69 6d 69 74 57 68 65 72 65  qlite3LimitWhere
2589f 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
258a0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
258a1 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
258a2 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c  ontext */.  SrcL
258a3 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20 20  ist *pSrc,      
258a4 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20           /* the 
258a5 46 52 4f 4d 20 63 6c 61 75 73 65 20 2d 2d 20 77  FROM clause -- w
258a6 68 69 63 68 20 74 61 62 6c 65 73 20 74 6f 20 73  hich tables to s
258a7 63 61 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  can */.  Expr *p
258a8 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 20 20  Where,          
258a9 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45        /* The WHE
258aa 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20  RE clause.  May 
258ab 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 45 78 70  be null */.  Exp
258ac 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c  rList *pOrderBy,
258ad 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
258ae 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
258af 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a  .  May be null *
258b0 2f 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74  /.  Expr *pLimit
258b1 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
258b2 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63 6c   /* The LIMIT cl
258b3 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75  ause.  May be nu
258b4 6c 6c 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f  ll */.  Expr *pO
258b5 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20  ffset,          
258b6 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 46 46 53       /* The OFFS
258b7 45 54 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20  ET clause.  May 
258b8 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 63 68 61  be null */.  cha
258b9 72 20 2a 7a 53 74 6d 74 54 79 70 65 20 20 20 20  r *zStmtType    
258ba 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 69 74            /* Eit
258bb 68 65 72 20 44 45 4c 45 54 45 20 6f 72 20 55 50  her DELETE or UP
258bc 44 41 54 45 2e 20 20 46 6f 72 20 65 72 72 6f 72  DATE.  For error
258bd 20 6d 65 73 73 61 67 65 73 2e 20 2a 2f 0a 29 7b   messages. */.){
258be 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 52  .  Expr *pWhereR
258bf 6f 77 69 64 20 3d 20 4e 55 4c 4c 3b 20 20 20 20  owid = NULL;    
258c0 2f 2a 20 57 48 45 52 45 20 72 6f 77 69 64 20 2e  /* WHERE rowid .
258c1 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 49 6e  . */.  Expr *pIn
258c2 43 6c 61 75 73 65 20 3d 20 4e 55 4c 4c 3b 20 20  Clause = NULL;  
258c3 20 20 20 20 2f 2a 20 57 48 45 52 45 20 72 6f 77      /* WHERE row
258c4 69 64 20 49 4e 20 28 20 73 65 6c 65 63 74 20 29  id IN ( select )
258c5 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 53 65 6c   */.  Expr *pSel
258c6 65 63 74 52 6f 77 69 64 20 3d 20 4e 55 4c 4c 3b  ectRowid = NULL;
258c7 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 72 6f 77     /* SELECT row
258c8 69 64 20 2e 2e 2e 20 2a 2f 0a 20 20 45 78 70 72  id ... */.  Expr
258c9 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 3d 20 4e  List *pEList = N
258ca 55 4c 4c 3b 20 20 20 20 20 2f 2a 20 45 78 70 72  ULL;     /* Expr
258cb 65 73 73 69 6f 6e 20 6c 69 73 74 20 63 6f 6e 74  ession list cont
258cc 61 6e 69 6e 67 20 6f 6e 6c 79 20 70 53 65 6c 65  aning only pSele
258cd 63 74 52 6f 77 69 64 20 2a 2f 0a 20 20 53 72 63  ctRowid */.  Src
258ce 4c 69 73 74 20 2a 70 53 65 6c 65 63 74 53 72 63  List *pSelectSrc
258cf 20 3d 20 4e 55 4c 4c 3b 20 20 2f 2a 20 53 45 4c   = NULL;  /* SEL
258d0 45 43 54 20 72 6f 77 69 64 20 46 52 4f 4d 20 78  ECT rowid FROM x
258d1 20 2e 2e 2e 20 28 64 75 70 20 6f 66 20 70 53 72   ... (dup of pSr
258d2 63 29 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  c) */.  Select *
258d3 70 53 65 6c 65 63 74 20 3d 20 4e 55 4c 4c 3b 20  pSelect = NULL; 
258d4 20 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65 74 65       /* Complete
258d5 20 53 45 4c 45 43 54 20 74 72 65 65 20 2a 2f 0a   SELECT tree */.
258d6 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74  .  /* Check that
258d7 20 74 68 65 72 65 20 69 73 6e 27 74 20 61 6e 20   there isn't an 
258d8 4f 52 44 45 52 20 42 59 20 77 69 74 68 6f 75 74  ORDER BY without
258d9 20 61 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e   a LIMIT clause.
258da 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f 72 64  .  */.  if( pOrd
258db 65 72 42 79 20 26 26 20 28 70 4c 69 6d 69 74 20  erBy && (pLimit 
258dc 3d 3d 20 30 29 20 29 20 7b 0a 20 20 20 20 73 71  == 0) ) {.    sq
258dd 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
258de 61 72 73 65 2c 20 22 4f 52 44 45 52 20 42 59 20  arse, "ORDER BY 
258df 77 69 74 68 6f 75 74 20 4c 49 4d 49 54 20 6f 6e  without LIMIT on
258e0 20 25 73 22 2c 20 7a 53 74 6d 74 54 79 70 65 29   %s", zStmtType)
258e1 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70 61  ;.    pParse->pa
258e2 72 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20 20  rseError = 1;.  
258e3 20 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65    goto limit_whe
258e4 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a 20 20  re_cleanup_2;.  
258e5 7d 0a 0a 20 20 2f 2a 20 57 65 20 6f 6e 6c 79 20  }..  /* We only 
258e6 6e 65 65 64 20 74 6f 20 67 65 6e 65 72 61 74 65  need to generate
258e7 20 61 20 73 65 6c 65 63 74 20 65 78 70 72 65 73   a select expres
258e8 73 69 6f 6e 20 69 66 20 74 68 65 72 65 0a 20 20  sion if there.  
258e9 2a 2a 20 69 73 20 61 20 6c 69 6d 69 74 2f 6f 66  ** is a limit/of
258ea 66 73 65 74 20 74 65 72 6d 20 74 6f 20 65 6e 66  fset term to enf
258eb 6f 72 63 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  orce..  */.  if(
258ec 20 70 4c 69 6d 69 74 20 3d 3d 20 30 20 29 20 7b   pLimit == 0 ) {
258ed 0a 20 20 20 20 2f 2a 20 69 66 20 70 4c 69 6d 69  .    /* if pLimi
258ee 74 20 69 73 20 6e 75 6c 6c 2c 20 70 4f 66 66 73  t is null, pOffs
258ef 65 74 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62  et will always b
258f0 65 20 6e 75 6c 6c 20 61 73 20 77 65 6c 6c 2e 20  e null as well. 
258f1 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  */.    assert( p
258f2 4f 66 66 73 65 74 20 3d 3d 20 30 20 29 3b 0a 20  Offset == 0 );. 
258f3 20 20 20 72 65 74 75 72 6e 20 70 57 68 65 72 65     return pWhere
258f4 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65  ;.  }..  /* Gene
258f5 72 61 74 65 20 61 20 73 65 6c 65 63 74 20 65 78  rate a select ex
258f6 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20 74 6f  pression tree to
258f7 20 65 6e 66 6f 72 63 65 20 74 68 65 20 6c 69 6d   enforce the lim
258f8 69 74 2f 6f 66 66 73 65 74 20 0a 20 20 2a 2a 20  it/offset .  ** 
258f9 74 65 72 6d 20 66 6f 72 20 74 68 65 20 44 45 4c  term for the DEL
258fa 45 54 45 20 6f 72 20 55 50 44 41 54 45 20 73 74  ETE or UPDATE st
258fb 61 74 65 6d 65 6e 74 2e 20 20 46 6f 72 20 65 78  atement.  For ex
258fc 61 6d 70 6c 65 3a 0a 20 20 2a 2a 20 20 20 44 45  ample:.  **   DE
258fd 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f  LETE FROM table_
258fe 61 20 57 48 45 52 45 20 63 6f 6c 31 3d 31 20 4f  a WHERE col1=1 O
258ff 52 44 45 52 20 42 59 20 63 6f 6c 32 20 4c 49 4d  RDER BY col2 LIM
25900 49 54 20 31 20 4f 46 46 53 45 54 20 31 0a 20 20  IT 1 OFFSET 1.  
25901 2a 2a 20 62 65 63 6f 6d 65 73 3a 0a 20 20 2a 2a  ** becomes:.  **
25902 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74     DELETE FROM t
25903 61 62 6c 65 5f 61 20 57 48 45 52 45 20 72 6f 77  able_a WHERE row
25904 69 64 20 49 4e 20 28 20 0a 20 20 2a 2a 20 20 20  id IN ( .  **   
25905 20 20 53 45 4c 45 43 54 20 72 6f 77 69 64 20 46    SELECT rowid F
25906 52 4f 4d 20 74 61 62 6c 65 5f 61 20 57 48 45 52  ROM table_a WHER
25907 45 20 63 6f 6c 31 3d 31 20 4f 52 44 45 52 20 42  E col1=1 ORDER B
25908 59 20 63 6f 6c 32 20 4c 49 4d 49 54 20 31 20 4f  Y col2 LIMIT 1 O
25909 46 46 53 45 54 20 31 0a 20 20 2a 2a 20 20 20 29  FFSET 1.  **   )
2590a 3b 0a 20 20 2a 2f 0a 0a 20 20 70 53 65 6c 65 63  ;.  */..  pSelec
2590b 74 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33  tRowid = sqlite3
2590c 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2590d 5f 52 4f 57 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  _ROW, 0, 0, 0);.
2590e 20 20 69 66 28 20 70 53 65 6c 65 63 74 52 6f 77    if( pSelectRow
2590f 69 64 20 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c  id == 0 ) goto l
25910 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e  imit_where_clean
25911 75 70 5f 32 3b 0a 20 20 70 45 4c 69 73 74 20 3d  up_2;.  pEList =
25912 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
25913 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30  Append(pParse, 0
25914 2c 20 70 53 65 6c 65 63 74 52 6f 77 69 64 29 3b  , pSelectRowid);
25915 0a 20 20 69 66 28 20 70 45 4c 69 73 74 20 3d 3d  .  if( pEList ==
25916 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f   0 ) goto limit_
25917 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b  where_cleanup_2;
25918 0a 0a 20 20 2f 2a 20 64 75 70 6c 69 63 61 74 65  ..  /* duplicate
25919 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2591a 20 61 73 20 69 74 20 69 73 20 6e 65 65 64 65 64   as it is needed
2591b 20 62 79 20 62 6f 74 68 20 74 68 65 20 44 45 4c   by both the DEL
2591c 45 54 45 2f 55 50 44 41 54 45 20 74 72 65 65 0a  ETE/UPDATE tree.
2591d 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 53 45 4c    ** and the SEL
2591e 45 43 54 20 73 75 62 74 72 65 65 2e 20 2a 2f 0a  ECT subtree. */.
2591f 20 20 70 53 65 6c 65 63 74 53 72 63 20 3d 20 73    pSelectSrc = s
25920 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70  qlite3SrcListDup
25921 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 72  (pParse->db, pSr
25922 63 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 53 65  c, 0);.  if( pSe
25923 6c 65 63 74 53 72 63 20 3d 3d 20 30 20 29 20 7b  lectSrc == 0 ) {
25924 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
25925 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73  ListDelete(pPars
25926 65 2d 3e 64 62 2c 20 70 45 4c 69 73 74 29 3b 0a  e->db, pEList);.
25927 20 20 20 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77      goto limit_w
25928 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a  here_cleanup_2;.
25929 20 20 7d 0a 0a 20 20 2f 2a 20 67 65 6e 65 72 61    }..  /* genera
2592a 74 65 20 74 68 65 20 53 45 4c 45 43 54 20 65 78  te the SELECT ex
2592b 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20 2a  pression tree. *
2592c 2f 0a 20 20 70 53 65 6c 65 63 74 20 3d 20 73 71  /.  pSelect = sq
2592d 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70  lite3SelectNew(p
2592e 50 61 72 73 65 2c 70 45 4c 69 73 74 2c 70 53 65  Parse,pEList,pSe
2592f 6c 65 63 74 53 72 63 2c 70 57 68 65 72 65 2c 30  lectSrc,pWhere,0
25930 2c 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ,0,.            
25931 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25932 20 70 4f 72 64 65 72 42 79 2c 30 2c 70 4c 69 6d   pOrderBy,0,pLim
25933 69 74 2c 70 4f 66 66 73 65 74 29 3b 0a 20 20 69  it,pOffset);.  i
25934 66 28 20 70 53 65 6c 65 63 74 20 3d 3d 20 30 20  f( pSelect == 0 
25935 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f  ) return 0;..  /
25936 2a 20 6e 6f 77 20 67 65 6e 65 72 61 74 65 20 74  * now generate t
25937 68 65 20 6e 65 77 20 57 48 45 52 45 20 72 6f 77  he new WHERE row
25938 69 64 20 49 4e 20 63 6c 61 75 73 65 20 66 6f 72  id IN clause for
25939 20 74 68 65 20 44 45 4c 45 54 45 2f 55 44 50 41   the DELETE/UDPA
2593a 54 45 20 2a 2f 0a 20 20 70 57 68 65 72 65 52 6f  TE */.  pWhereRo
2593b 77 69 64 20 3d 20 73 71 6c 69 74 65 33 50 45 78  wid = sqlite3PEx
2593c 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 52 4f  pr(pParse, TK_RO
2593d 57 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 69  W, 0, 0, 0);.  i
2593e 66 28 20 70 57 68 65 72 65 52 6f 77 69 64 20 3d  f( pWhereRowid =
2593f 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74  = 0 ) goto limit
25940 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 31  _where_cleanup_1
25941 3b 0a 20 20 70 49 6e 43 6c 61 75 73 65 20 3d 20  ;.  pInClause = 
25942 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
25943 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 70 57 68 65  rse, TK_IN, pWhe
25944 72 65 52 6f 77 69 64 2c 20 30 2c 20 30 29 3b 0a  reRowid, 0, 0);.
25945 20 20 69 66 28 20 70 49 6e 43 6c 61 75 73 65 20    if( pInClause 
25946 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69  == 0 ) goto limi
25947 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f  t_where_cleanup_
25948 31 3b 0a 0a 20 20 70 49 6e 43 6c 61 75 73 65 2d  1;..  pInClause-
25949 3e 78 2e 70 53 65 6c 65 63 74 20 3d 20 70 53 65  >x.pSelect = pSe
2594a 6c 65 63 74 3b 0a 20 20 70 49 6e 43 6c 61 75 73  lect;.  pInClaus
2594b 65 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 78  e->flags |= EP_x
2594c 49 73 53 65 6c 65 63 74 3b 0a 20 20 73 71 6c 69  IsSelect;.  sqli
2594d 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68 74  te3ExprSetHeight
2594e 28 70 50 61 72 73 65 2c 20 70 49 6e 43 6c 61 75  (pParse, pInClau
2594f 73 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 49  se);.  return pI
25950 6e 43 6c 61 75 73 65 3b 0a 0a 20 20 2f 2a 20 73  nClause;..  /* s
25951 6f 6d 65 74 68 69 6e 67 20 77 65 6e 74 20 77 72  omething went wr
25952 6f 6e 67 2e 20 63 6c 65 61 6e 20 75 70 20 61 6e  ong. clean up an
25953 79 74 68 69 6e 67 20 61 6c 6c 6f 63 61 74 65 64  ything allocated
25954 2e 20 2a 2f 0a 6c 69 6d 69 74 5f 77 68 65 72 65  . */.limit_where
25955 5f 63 6c 65 61 6e 75 70 5f 31 3a 0a 20 20 73 71  _cleanup_1:.  sq
25956 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
25957 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53  e(pParse->db, pS
25958 65 6c 65 63 74 29 3b 0a 20 20 72 65 74 75 72 6e  elect);.  return
25959 20 30 3b 0a 0a 6c 69 6d 69 74 5f 77 68 65 72 65   0;..limit_where
2595a 5f 63 6c 65 61 6e 75 70 5f 32 3a 0a 20 20 73 71  _cleanup_2:.  sq
2595b 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
2595c 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 57 68 65  pParse->db, pWhe
2595d 72 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  re);.  sqlite3Ex
2595e 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61  prListDelete(pPa
2595f 72 73 65 2d 3e 64 62 2c 20 70 4f 72 64 65 72 42  rse->db, pOrderB
25960 79 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  y);.  sqlite3Exp
25961 72 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  rDelete(pParse->
25962 64 62 2c 20 70 4c 69 6d 69 74 29 3b 0a 20 20 73  db, pLimit);.  s
25963 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
25964 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4f 66  (pParse->db, pOf
25965 66 73 65 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  fset);.  return 
25966 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 64  0;.}.#endif /* d
25967 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
25968 41 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45  ABLE_UPDATE_DELE
25969 54 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65  TE_LIMIT) && !de
2596a 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2596b 54 5f 53 55 42 51 55 45 52 59 29 20 2a 2f 0a 0a  T_SUBQUERY) */..
2596c 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
2596d 6f 64 65 20 66 6f 72 20 61 20 44 45 4c 45 54 45  ode for a DELETE
2596e 20 46 52 4f 4d 20 73 74 61 74 65 6d 65 6e 74 2e   FROM statement.
2596f 0a 2a 2a 0a 2a 2a 20 20 20 20 20 44 45 4c 45 54  .**.**     DELET
25970 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f 77 78 79  E FROM table_wxy
25971 7a 20 57 48 45 52 45 20 61 3c 35 20 41 4e 44 20  z WHERE a<5 AND 
25972 62 20 4e 4f 54 20 4e 55 4c 4c 3b 0a 2a 2a 20 20  b NOT NULL;.**  
25973 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
25974 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20  ________/       
25975 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  \_______________
25976 5f 2f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  _/.**           
25977 20 20 20 20 20 20 20 70 54 61 62 4c 69 73 74 20         pTabList 
25978 20 20 20 20 20 20 20 20 20 20 20 20 20 70 57 68               pWh
25979 65 72 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ere.*/.SQLITE_PR
2597a 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2597b 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 0a 20 20  e3DeleteFrom(.  
2597c 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2597d 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61         /* The pa
2597e 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
2597f 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c    SrcList *pTabL
25980 69 73 74 2c 20 20 20 20 20 2f 2a 20 54 68 65 20  ist,     /* The 
25981 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68  table from which
25982 20 77 65 20 73 68 6f 75 6c 64 20 64 65 6c 65 74   we should delet
25983 65 20 74 68 69 6e 67 73 20 2a 2f 0a 20 20 45 78  e things */.  Ex
25984 70 72 20 2a 70 57 68 65 72 65 20 20 20 20 20 20  pr *pWhere      
25985 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52       /* The WHER
25986 45 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62  E clause.  May b
25987 65 20 6e 75 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 56  e null */.){.  V
25988 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
25989 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72        /* The vir
2598a 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e  tual database en
2598b 67 69 6e 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20  gine */.  Table 
2598c 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 20  *pTab;          
2598d 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 66 72   /* The table fr
2598e 6f 6d 20 77 68 69 63 68 20 72 65 63 6f 72 64 73  om which records
2598f 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 65 64   will be deleted
25990 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
25991 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 2f 2a 20   *zDb;       /* 
25992 4e 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65  Name of database
25993 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f   holding pTab */
25994 0a 20 20 69 6e 74 20 65 6e 64 2c 20 61 64 64 72  .  int end, addr
25995 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 41 20 63   = 0;     /* A c
25996 6f 75 70 6c 65 20 61 64 64 72 65 73 73 65 73 20  ouple addresses 
25997 6f 66 20 67 65 6e 65 72 61 74 65 64 20 63 6f 64  of generated cod
25998 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  e */.  int i;   
25999 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2599a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
2599b 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a 70 57  .  WhereInfo *pW
2599c 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 49 6e 66  Info;     /* Inf
2599d 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
2599e 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2599f 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  */.  Index *pIdx
259a0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  ;           /* F
259a1 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
259a2 69 6e 64 69 63 65 73 20 6f 66 20 74 68 65 20 74  indices of the t
259a3 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43  able */.  int iC
259a4 75 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ur;             
259a5 20 2f 2a 20 56 44 42 45 20 43 75 72 73 6f 72 20   /* VDBE Cursor 
259a6 6e 75 6d 62 65 72 20 66 6f 72 20 70 54 61 62 20  number for pTab 
259a7 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
259a8 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  ;           /* M
259a9 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74 72  ain database str
259aa 75 63 74 75 72 65 20 2a 2f 0a 20 20 41 75 74 68  ucture */.  Auth
259ab 43 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74  Context sContext
259ac 3b 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74  ;  /* Authorizat
259ad 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ion context */. 
259ae 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43   NameContext sNC
259af 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  ;       /* Name 
259b0 63 6f 6e 74 65 78 74 20 74 6f 20 72 65 73 6f 6c  context to resol
259b1 76 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69  ve expressions i
259b2 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20  n */.  int iDb; 
259b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
259b4 20 44 61 74 61 62 61 73 65 20 6e 75 6d 62 65 72   Database number
259b5 20 2a 2f 0a 20 20 69 6e 74 20 6d 65 6d 43 6e 74   */.  int memCnt
259b6 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 2f 2a 20   = -1;       /* 
259b7 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64  Memory cell used
259b8 20 66 6f 72 20 63 68 61 6e 67 65 20 63 6f 75 6e   for change coun
259b9 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 72 63  ting */.  int rc
259ba 61 75 74 68 3b 20 20 20 20 20 20 20 20 20 20 20  auth;           
259bb 20 2f 2a 20 56 61 6c 75 65 20 72 65 74 75 72 6e   /* Value return
259bc 65 64 20 62 79 20 61 75 74 68 6f 72 69 7a 61 74  ed by authorizat
259bd 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a  ion callback */.
259be 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
259bf 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 69  OMIT_TRIGGER.  i
259c0 6e 74 20 69 73 56 69 65 77 3b 20 20 20 20 20 20  nt isView;      
259c1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
259c2 72 75 65 20 69 66 20 61 74 74 65 6d 70 74 69 6e  rue if attemptin
259c3 67 20 74 6f 20 64 65 6c 65 74 65 20 66 72 6f 6d  g to delete from
259c4 20 61 20 76 69 65 77 20 2a 2f 0a 20 20 54 72 69   a view */.  Tri
259c5 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20  gger *pTrigger; 
259c6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 73            /* Lis
259c7 74 20 6f 66 20 74 61 62 6c 65 20 74 72 69 67 67  t of table trigg
259c8 65 72 73 2c 20 69 66 20 72 65 71 75 69 72 65 64  ers, if required
259c9 20 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 6d 65   */.#endif..  me
259ca 6d 73 65 74 28 26 73 43 6f 6e 74 65 78 74 2c 20  mset(&sContext, 
259cb 30 2c 20 73 69 7a 65 6f 66 28 73 43 6f 6e 74 65  0, sizeof(sConte
259cc 78 74 29 29 3b 0a 20 20 64 62 20 3d 20 70 50 61  xt));.  db = pPa
259cd 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70  rse->db;.  if( p
259ce 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64  Parse->nErr || d
259cf 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
259d0 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65  ){.    goto dele
259d1 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b  te_from_cleanup;
259d2 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
259d3 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31  TabList->nSrc==1
259d4 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65   );..  /* Locate
259d5 20 74 68 65 20 74 61 62 6c 65 20 77 68 69 63 68   the table which
259d6 20 77 65 20 77 61 6e 74 20 74 6f 20 64 65 6c 65   we want to dele
259d7 74 65 2e 20 20 54 68 69 73 20 74 61 62 6c 65 20  te.  This table 
259d8 68 61 73 20 74 6f 20 62 65 0a 20 20 2a 2a 20 70  has to be.  ** p
259d9 75 74 20 69 6e 20 61 6e 20 53 72 63 4c 69 73 74  ut in an SrcList
259da 20 73 74 72 75 63 74 75 72 65 20 62 65 63 61 75   structure becau
259db 73 65 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 73  se some of the s
259dc 75 62 72 6f 75 74 69 6e 65 73 20 77 65 0a 20 20  ubroutines we.  
259dd 2a 2a 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 69  ** will be calli
259de 6e 67 20 61 72 65 20 64 65 73 69 67 6e 65 64 20  ng are designed 
259df 74 6f 20 77 6f 72 6b 20 77 69 74 68 20 6d 75 6c  to work with mul
259e0 74 69 70 6c 65 20 74 61 62 6c 65 73 20 61 6e 64  tiple tables and
259e1 20 65 78 70 65 63 74 0a 20 20 2a 2a 20 61 6e 20   expect.  ** an 
259e2 53 72 63 4c 69 73 74 2a 20 70 61 72 61 6d 65 74  SrcList* paramet
259e3 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20 6a 75  er instead of ju
259e4 73 74 20 61 20 54 61 62 6c 65 2a 20 70 61 72 61  st a Table* para
259e5 6d 65 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 54  meter..  */.  pT
259e6 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c  ab = sqlite3SrcL
259e7 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65  istLookup(pParse
259e8 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 69  , pTabList);.  i
259e9 66 28 20 70 54 61 62 3d 3d 30 20 29 20 20 67 6f  f( pTab==0 )  go
259ea 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63  to delete_from_c
259eb 6c 65 61 6e 75 70 3b 0a 0a 20 20 2f 2a 20 46 69  leanup;..  /* Fi
259ec 67 75 72 65 20 6f 75 74 20 69 66 20 77 65 20 68  gure out if we h
259ed 61 76 65 20 61 6e 79 20 74 72 69 67 67 65 72 73  ave any triggers
259ee 20 61 6e 64 20 69 66 20 74 68 65 20 74 61 62 6c   and if the tabl
259ef 65 20 62 65 69 6e 67 0a 20 20 2a 2a 20 64 65 6c  e being.  ** del
259f0 65 74 65 64 20 66 72 6f 6d 20 69 73 20 61 20 76  eted from is a v
259f1 69 65 77 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66  iew.  */.#ifndef
259f2 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
259f3 47 47 45 52 0a 20 20 70 54 72 69 67 67 65 72 20  GGER.  pTrigger 
259f4 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  = sqlite3Trigger
259f5 73 45 78 69 73 74 28 70 50 61 72 73 65 2c 20 70  sExist(pParse, p
259f6 54 61 62 2c 20 54 4b 5f 44 45 4c 45 54 45 2c 20  Tab, TK_DELETE, 
259f7 30 2c 20 30 29 3b 0a 20 20 69 73 56 69 65 77 20  0, 0);.  isView 
259f8 3d 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 21  = pTab->pSelect!
259f9 3d 30 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  =0;.#else.# defi
259fa 6e 65 20 70 54 72 69 67 67 65 72 20 30 0a 23 20  ne pTrigger 0.# 
259fb 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a  define isView 0.
259fc 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51  #endif.#ifdef SQ
259fd 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 23  LITE_OMIT_VIEW.#
259fe 20 75 6e 64 65 66 20 69 73 56 69 65 77 0a 23 20   undef isView.# 
259ff 64 65 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a  define isView 0.
25a00 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20  #endif..  /* If 
25a01 70 54 61 62 20 69 73 20 72 65 61 6c 6c 79 20 61  pTab is really a
25a02 20 76 69 65 77 2c 20 6d 61 6b 65 20 73 75 72 65   view, make sure
25a03 20 69 74 20 68 61 73 20 62 65 65 6e 20 69 6e 69   it has been ini
25a04 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20  tialized..  */. 
25a05 20 69 66 28 20 73 71 6c 69 74 65 33 56 69 65 77   if( sqlite3View
25a06 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70  GetColumnNames(p
25a07 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b 0a  Parse, pTab) ){.
25a08 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f      goto delete_
25a09 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  from_cleanup;.  
25a0a 7d 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  }..  if( sqlite3
25a0b 49 73 52 65 61 64 4f 6e 6c 79 28 70 50 61 72 73  IsReadOnly(pPars
25a0c 65 2c 20 70 54 61 62 2c 20 28 70 54 72 69 67 67  e, pTab, (pTrigg
25a0d 65 72 3f 31 3a 30 29 29 20 29 7b 0a 20 20 20 20  er?1:0)) ){.    
25a0e 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d  goto delete_from
25a0f 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20  _cleanup;.  }.  
25a10 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
25a11 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
25a12 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  Tab->pSchema);. 
25a13 20 61 73 73 65 72 74 28 20 69 44 62 3c 64 62 2d   assert( iDb<db-
25a14 3e 6e 44 62 20 29 3b 0a 20 20 7a 44 62 20 3d 20  >nDb );.  zDb = 
25a15 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
25a16 6d 65 3b 0a 20 20 72 63 61 75 74 68 20 3d 20 73  me;.  rcauth = s
25a17 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
25a18 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44  pParse, SQLITE_D
25a19 45 4c 45 54 45 2c 20 70 54 61 62 2d 3e 7a 4e 61  ELETE, pTab->zNa
25a1a 6d 65 2c 20 30 2c 20 7a 44 62 29 3b 0a 20 20 61  me, 0, zDb);.  a
25a1b 73 73 65 72 74 28 20 72 63 61 75 74 68 3d 3d 53  ssert( rcauth==S
25a1c 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 61 75  QLITE_OK || rcau
25a1d 74 68 3d 3d 53 51 4c 49 54 45 5f 44 45 4e 59 20  th==SQLITE_DENY 
25a1e 7c 7c 20 72 63 61 75 74 68 3d 3d 53 51 4c 49 54  || rcauth==SQLIT
25a1f 45 5f 49 47 4e 4f 52 45 20 29 3b 0a 20 20 69 66  E_IGNORE );.  if
25a20 28 20 72 63 61 75 74 68 3d 3d 53 51 4c 49 54 45  ( rcauth==SQLITE
25a21 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20 67 6f 74  _DENY ){.    got
25a22 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c  o delete_from_cl
25a23 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61 73 73  eanup;.  }.  ass
25a24 65 72 74 28 21 69 73 56 69 65 77 20 7c 7c 20 70  ert(!isView || p
25a25 54 72 69 67 67 65 72 29 3b 0a 0a 20 20 2f 2a 20  Trigger);..  /* 
25a26 41 73 73 69 67 6e 20 20 63 75 72 73 6f 72 20 6e  Assign  cursor n
25a27 75 6d 62 65 72 20 74 6f 20 74 68 65 20 74 61 62  umber to the tab
25a28 6c 65 20 61 6e 64 20 61 6c 6c 20 69 74 73 20 69  le and all its i
25a29 6e 64 69 63 65 73 2e 0a 20 20 2a 2f 0a 20 20 61  ndices..  */.  a
25a2a 73 73 65 72 74 28 20 70 54 61 62 4c 69 73 74 2d  ssert( pTabList-
25a2b 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 69 43  >nSrc==1 );.  iC
25a2c 75 72 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61  ur = pTabList->a
25a2d 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20 70 50  [0].iCursor = pP
25a2e 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
25a2f 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70  for(pIdx=pTab->p
25a30 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64  Index; pIdx; pId
25a31 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  x=pIdx->pNext){.
25a32 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62      pParse->nTab
25a33 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74  ++;.  }..  /* St
25a34 61 72 74 20 74 68 65 20 76 69 65 77 20 63 6f 6e  art the view con
25a35 74 65 78 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20  text.  */.  if( 
25a36 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 73 71  isView ){.    sq
25a37 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
25a38 50 75 73 68 28 70 50 61 72 73 65 2c 20 26 73 43  Push(pParse, &sC
25a39 6f 6e 74 65 78 74 2c 20 70 54 61 62 2d 3e 7a 4e  ontext, pTab->zN
25a3a 61 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ame);.  }..  /* 
25a3b 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67  Begin generating
25a3c 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 76 20   code..  */.  v 
25a3d 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
25a3e 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20  (pParse);.  if( 
25a3f 76 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f  v==0 ){.    goto
25a40 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65   delete_from_cle
25a41 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20  anup;.  }.  if( 
25a42 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
25a43 30 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 43  0 ) sqlite3VdbeC
25a44 6f 75 6e 74 43 68 61 6e 67 65 73 28 76 29 3b 0a  ountChanges(v);.
25a45 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72    sqlite3BeginWr
25a46 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61  iteOperation(pPa
25a47 72 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 0a 20  rse, 1, iDb);.. 
25a48 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 74 72   /* If we are tr
25a49 79 69 6e 67 20 74 6f 20 64 65 6c 65 74 65 20 66  ying to delete f
25a4a 72 6f 6d 20 61 20 76 69 65 77 2c 20 72 65 61 6c  rom a view, real
25a4b 69 7a 65 20 74 68 61 74 20 76 69 65 77 20 69 6e  ize that view in
25a4c 74 6f 0a 20 20 2a 2a 20 61 20 65 70 68 65 6d 65  to.  ** a epheme
25a4d 72 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  ral table..  */.
25a4e 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
25a4f 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26  ITE_OMIT_VIEW) &
25a50 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
25a51 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a  E_OMIT_TRIGGER).
25a52 20 20 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a    if( isView ){.
25a53 20 20 20 20 73 71 6c 69 74 65 33 4d 61 74 65 72      sqlite3Mater
25a54 69 61 6c 69 7a 65 56 69 65 77 28 70 50 61 72 73  ializeView(pPars
25a55 65 2c 20 70 54 61 62 2c 20 70 57 68 65 72 65 2c  e, pTab, pWhere,
25a56 20 69 43 75 72 29 3b 0a 20 20 7d 0a 23 65 6e 64   iCur);.  }.#end
25a57 69 66 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65  if..  /* Resolve
25a58 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   the column name
25a59 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63  s in the WHERE c
25a5a 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 6d 65  lause..  */.  me
25a5b 6d 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69  mset(&sNC, 0, si
25a5c 7a 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e  zeof(sNC));.  sN
25a5d 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73  C.pParse = pPars
25a5e 65 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c 69 73  e;.  sNC.pSrcLis
25a5f 74 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20 20  t = pTabList;.  
25a60 69 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  if( sqlite3Resol
25a61 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43  veExprNames(&sNC
25a62 2c 20 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20  , pWhere) ){.   
25a63 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f   goto delete_fro
25a64 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a  m_cleanup;.  }..
25a65 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
25a66 74 68 65 20 63 6f 75 6e 74 65 72 20 6f 66 20 74  the counter of t
25a67 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
25a68 73 20 64 65 6c 65 74 65 64 2c 20 69 66 0a 20 20  s deleted, if.  
25a69 2a 2a 20 77 65 20 61 72 65 20 63 6f 75 6e 74 69  ** we are counti
25a6a 6e 67 20 72 6f 77 73 2e 0a 20 20 2a 2f 0a 20 20  ng rows..  */.  
25a6b 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20  if( db->flags & 
25a6c 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73  SQLITE_CountRows
25a6d 20 29 7b 0a 20 20 20 20 6d 65 6d 43 6e 74 20 3d   ){.    memCnt =
25a6e 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
25a6f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
25a70 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
25a71 65 67 65 72 2c 20 30 2c 20 6d 65 6d 43 6e 74 29  eger, 0, memCnt)
25a72 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53  ;.  }..#ifndef S
25a73 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 55 4e 43  QLITE_OMIT_TRUNC
25a74 41 54 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e  ATE_OPTIMIZATION
25a75 0a 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61  .  /* Special ca
25a76 73 65 3a 20 41 20 44 45 4c 45 54 45 20 77 69 74  se: A DELETE wit
25a77 68 6f 75 74 20 61 20 57 48 45 52 45 20 63 6c 61  hout a WHERE cla
25a78 75 73 65 20 64 65 6c 65 74 65 73 20 65 76 65 72  use deletes ever
25a79 79 74 68 69 6e 67 2e 0a 20 20 2a 2a 20 49 74 20  ything..  ** It 
25a7a 69 73 20 65 61 73 69 65 72 20 6a 75 73 74 20 74  is easier just t
25a7b 6f 20 65 72 61 73 65 20 74 68 65 20 77 68 6f 6c  o erase the whol
25a7c 65 20 74 61 62 6c 65 2e 20 50 72 69 6f 72 20 74  e table. Prior t
25a7d 6f 20 76 65 72 73 69 6f 6e 20 33 2e 36 2e 35 2c  o version 3.6.5,
25a7e 0a 20 20 2a 2a 20 74 68 69 73 20 6f 70 74 69 6d  .  ** this optim
25a7f 69 7a 61 74 69 6f 6e 20 63 61 75 73 65 64 20 74  ization caused t
25a80 68 65 20 72 6f 77 20 63 68 61 6e 67 65 20 63 6f  he row change co
25a81 75 6e 74 20 28 74 68 65 20 76 61 6c 75 65 20 72  unt (the value r
25a82 65 74 75 72 6e 65 64 20 62 79 20 0a 20 20 2a 2a  eturned by .  **
25a83 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 20 73 71   API function sq
25a84 6c 69 74 65 33 5f 63 6f 75 6e 74 5f 63 68 61 6e  lite3_count_chan
25a85 67 65 73 29 20 74 6f 20 62 65 20 73 65 74 20 69  ges) to be set i
25a86 6e 63 6f 72 72 65 63 74 6c 79 2e 20 20 2a 2f 0a  ncorrectly.  */.
25a87 20 20 69 66 28 20 72 63 61 75 74 68 3d 3d 53 51    if( rcauth==SQ
25a88 4c 49 54 45 5f 4f 4b 20 26 26 20 70 57 68 65 72  LITE_OK && pWher
25a89 65 3d 3d 30 20 26 26 20 21 70 54 72 69 67 67 65  e==0 && !pTrigge
25a8a 72 20 26 26 20 21 49 73 56 69 72 74 75 61 6c 28  r && !IsVirtual(
25a8b 70 54 61 62 29 20 0a 20 20 20 26 26 20 30 3d 3d  pTab) .   && 0==
25a8c 73 71 6c 69 74 65 33 46 6b 52 65 71 75 69 72 65  sqlite3FkRequire
25a8d 64 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  d(pParse, pTab, 
25a8e 30 2c 20 30 29 0a 20 20 29 7b 0a 20 20 20 20 61  0, 0).  ){.    a
25a8f 73 73 65 72 74 28 20 21 69 73 56 69 65 77 20 29  ssert( !isView )
25a90 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
25a91 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6c  eAddOp4(v, OP_Cl
25a92 65 61 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c  ear, pTab->tnum,
25a93 20 69 44 62 2c 20 6d 65 6d 43 6e 74 2c 0a 20 20   iDb, memCnt,.  
25a94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25a95 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c      pTab->zName,
25a96 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20   P4_STATIC);.   
25a97 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e   for(pIdx=pTab->
25a98 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
25a99 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b  dx=pIdx->pNext){
25a9a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
25a9b 49 64 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54  Idx->pSchema==pT
25a9c 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20  ab->pSchema );. 
25a9d 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25a9e 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 65  AddOp2(v, OP_Cle
25a9f 61 72 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20  ar, pIdx->tnum, 
25aa0 69 44 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  iDb);.    }.  }e
25aa1 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  lse.#endif /* SQ
25aa2 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 55 4e 43 41  LITE_OMIT_TRUNCA
25aa3 54 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20  TE_OPTIMIZATION 
25aa4 2a 2f 0a 20 20 2f 2a 20 54 68 65 20 75 73 75 61  */.  /* The usua
25aa5 6c 20 63 61 73 65 3a 20 54 68 65 72 65 20 69 73  l case: There is
25aa6 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20   a WHERE clause 
25aa7 73 6f 20 77 65 20 68 61 76 65 20 74 6f 20 73 63  so we have to sc
25aa8 61 6e 20 74 68 72 6f 75 67 68 0a 20 20 2a 2a 20  an through.  ** 
25aa9 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 70 69  the table and pi
25aaa 63 6b 20 77 68 69 63 68 20 72 65 63 6f 72 64 73  ck which records
25aab 20 74 6f 20 64 65 6c 65 74 65 2e 0a 20 20 2a 2f   to delete..  */
25aac 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 52 6f  .  {.    int iRo
25aad 77 53 65 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d  wSet = ++pParse-
25aae 3e 6e 4d 65 6d 3b 20 20 20 2f 2a 20 52 65 67 69  >nMem;   /* Regi
25aaf 73 74 65 72 20 66 6f 72 20 72 6f 77 73 65 74 20  ster for rowset 
25ab0 6f 66 20 72 6f 77 73 20 74 6f 20 64 65 6c 65 74  of rows to delet
25ab1 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 52 6f  e */.    int iRo
25ab2 77 69 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  wid = ++pParse->
25ab3 6e 4d 65 6d 3b 20 20 20 20 2f 2a 20 55 73 65 64  nMem;    /* Used
25ab4 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 72 6f 77   for storing row
25ab5 69 64 20 76 61 6c 75 65 73 2e 20 2a 2f 0a 20 20  id values. */.  
25ab6 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20    int regRowid; 
25ab7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25ab8 20 20 2f 2a 20 41 63 74 75 61 6c 20 72 65 67 69    /* Actual regi
25ab9 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ster containing 
25aba 72 6f 77 69 64 73 20 2a 2f 0a 0a 20 20 20 20 2f  rowids */..    /
25abb 2a 20 43 6f 6c 6c 65 63 74 20 72 6f 77 69 64 73  * Collect rowids
25abc 20 6f 66 20 65 76 65 72 79 20 72 6f 77 20 74 6f   of every row to
25abd 20 62 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 20   be deleted..   
25abe 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
25abf 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
25ac0 4e 75 6c 6c 2c 20 30 2c 20 69 52 6f 77 53 65 74  Null, 0, iRowSet
25ac1 29 3b 0a 20 20 20 20 70 57 49 6e 66 6f 20 3d 20  );.    pWInfo = 
25ac2 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69  sqlite3WhereBegi
25ac3 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69  n(pParse, pTabLi
25ac4 73 74 2c 20 70 57 68 65 72 65 2c 30 2c 57 48 45  st, pWhere,0,WHE
25ac5 52 45 5f 44 55 50 4c 49 43 41 54 45 53 5f 4f 4b  RE_DUPLICATES_OK
25ac6 29 3b 0a 20 20 20 20 69 66 28 20 70 57 49 6e 66  );.    if( pWInf
25ac7 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 64 65 6c 65  o==0 ) goto dele
25ac8 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b  te_from_cleanup;
25ac9 0a 20 20 20 20 72 65 67 52 6f 77 69 64 20 3d 20  .    regRowid = 
25aca 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 47  sqlite3ExprCodeG
25acb 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c  etColumn(pParse,
25acc 20 70 54 61 62 2c 20 2d 31 2c 20 69 43 75 72 2c   pTab, -1, iCur,
25acd 20 69 52 6f 77 69 64 2c 20 30 29 3b 0a 20 20 20   iRowid, 0);.   
25ace 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
25acf 70 32 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74 41  p2(v, OP_RowSetA
25ad0 64 64 2c 20 69 52 6f 77 53 65 74 2c 20 72 65 67  dd, iRowSet, reg
25ad1 52 6f 77 69 64 29 3b 0a 20 20 20 20 69 66 28 20  Rowid);.    if( 
25ad2 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
25ad3 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 7b 0a  TE_CountRows ){.
25ad4 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
25ad5 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64  eAddOp2(v, OP_Ad
25ad6 64 49 6d 6d 2c 20 6d 65 6d 43 6e 74 2c 20 31 29  dImm, memCnt, 1)
25ad7 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
25ad8 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e  te3WhereEnd(pWIn
25ad9 66 6f 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 6c  fo);..    /* Del
25ada 65 74 65 20 65 76 65 72 79 20 69 74 65 6d 20 77  ete every item w
25adb 68 6f 73 65 20 6b 65 79 20 77 61 73 20 77 72 69  hose key was wri
25adc 74 74 65 6e 20 74 6f 20 74 68 65 20 6c 69 73 74  tten to the list
25add 20 64 75 72 69 6e 67 20 74 68 65 0a 20 20 20 20   during the.    
25ade 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63 61 6e  ** database scan
25adf 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 64 65  .  We have to de
25ae0 6c 65 74 65 20 69 74 65 6d 73 20 61 66 74 65 72  lete items after
25ae1 20 74 68 65 20 73 63 61 6e 20 69 73 20 63 6f 6d   the scan is com
25ae2 70 6c 65 74 65 0a 20 20 20 20 2a 2a 20 62 65 63  plete.    ** bec
25ae3 61 75 73 65 20 64 65 6c 65 74 69 6e 67 20 61 6e  ause deleting an
25ae4 20 69 74 65 6d 20 63 61 6e 20 63 68 61 6e 67 65   item can change
25ae5 20 74 68 65 20 73 63 61 6e 20 6f 72 64 65 72 2e   the scan order.
25ae6 20 20 2a 2f 0a 20 20 20 20 65 6e 64 20 3d 20 73    */.    end = s
25ae7 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
25ae8 62 65 6c 28 76 29 3b 0a 0a 20 20 20 20 2f 2a 20  bel(v);..    /* 
25ae9 55 6e 6c 65 73 73 20 74 68 69 73 20 69 73 20 61  Unless this is a
25aea 20 76 69 65 77 2c 20 6f 70 65 6e 20 63 75 72 73   view, open curs
25aeb 6f 72 73 20 66 6f 72 20 74 68 65 20 74 61 62 6c  ors for the tabl
25aec 65 20 77 65 20 61 72 65 20 0a 20 20 20 20 2a 2a  e we are .    **
25aed 20 64 65 6c 65 74 69 6e 67 20 66 72 6f 6d 20 61   deleting from a
25aee 6e 64 20 61 6c 6c 20 69 74 73 20 69 6e 64 69 63  nd all its indic
25aef 65 73 2e 20 49 66 20 74 68 69 73 20 69 73 20 61  es. If this is a
25af0 20 76 69 65 77 2c 20 74 68 65 6e 20 74 68 65 0a   view, then the.
25af1 20 20 20 20 2a 2a 20 6f 6e 6c 79 20 65 66 66 65      ** only effe
25af2 63 74 20 74 68 69 73 20 73 74 61 74 65 6d 65 6e  ct this statemen
25af3 74 20 68 61 73 20 69 73 20 74 6f 20 66 69 72 65  t has is to fire
25af4 20 74 68 65 20 49 4e 53 54 45 41 44 20 4f 46 20   the INSTEAD OF 
25af5 0a 20 20 20 20 2a 2a 20 74 72 69 67 67 65 72 73  .    ** triggers
25af6 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 69  .  */.    if( !i
25af7 73 56 69 65 77 20 29 7b 0a 20 20 20 20 20 20 73  sView ){.      s
25af8 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41  qlite3OpenTableA
25af9 6e 64 49 6e 64 69 63 65 73 28 70 50 61 72 73 65  ndIndices(pParse
25afa 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 4f 50  , pTab, iCur, OP
25afb 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20 20  _OpenWrite);.   
25afc 20 7d 0a 0a 20 20 20 20 61 64 64 72 20 3d 20 73   }..    addr = s
25afd 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
25afe 28 76 2c 20 4f 50 5f 52 6f 77 53 65 74 52 65 61  (v, OP_RowSetRea
25aff 64 2c 20 69 52 6f 77 53 65 74 2c 20 65 6e 64 2c  d, iRowSet, end,
25b00 20 69 52 6f 77 69 64 29 3b 0a 0a 20 20 20 20 2f   iRowid);..    /
25b01 2a 20 44 65 6c 65 74 65 20 74 68 65 20 72 6f 77  * Delete the row
25b02 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
25b03 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
25b04 41 42 4c 45 0a 20 20 20 20 69 66 28 20 49 73 56  ABLE.    if( IsV
25b05 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
25b06 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
25b07 20 2a 70 56 54 61 62 20 3d 20 28 63 6f 6e 73 74   *pVTab = (const
25b08 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 47   char *)sqlite3G
25b09 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54 61  etVTable(db, pTa
25b0a 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
25b0b 33 56 74 61 62 4d 61 6b 65 57 72 69 74 61 62 6c  3VtabMakeWritabl
25b0c 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b  e(pParse, pTab);
25b0d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
25b0e 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56  beAddOp4(v, OP_V
25b0f 55 70 64 61 74 65 2c 20 30 2c 20 31 2c 20 69 52  Update, 0, 1, iR
25b10 6f 77 69 64 2c 20 70 56 54 61 62 2c 20 50 34 5f  owid, pVTab, P4_
25b11 56 54 41 42 29 3b 0a 20 20 20 20 20 20 73 71 6c  VTAB);.      sql
25b12 69 74 65 33 4d 61 79 41 62 6f 72 74 28 70 50 61  ite3MayAbort(pPa
25b13 72 73 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a  rse);.    }else.
25b14 23 65 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20 20  #endif.    {.   
25b15 20 20 20 69 6e 74 20 63 6f 75 6e 74 20 3d 20 28     int count = (
25b16 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
25b17 30 29 3b 20 20 20 20 2f 2a 20 54 72 75 65 20 74  0);    /* True t
25b18 6f 20 63 6f 75 6e 74 20 63 68 61 6e 67 65 73 20  o count changes 
25b19 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
25b1a 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65 74  GenerateRowDelet
25b1b 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  e(pParse, pTab, 
25b1c 69 43 75 72 2c 20 69 52 6f 77 69 64 2c 20 63 6f  iCur, iRowid, co
25b1d 75 6e 74 2c 20 70 54 72 69 67 67 65 72 2c 20 4f  unt, pTrigger, O
25b1e 45 5f 44 65 66 61 75 6c 74 29 3b 0a 20 20 20 20  E_Default);.    
25b1f 7d 0a 0a 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66  }..    /* End of
25b20 20 74 68 65 20 64 65 6c 65 74 65 20 6c 6f 6f 70   the delete loop
25b21 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
25b22 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
25b23 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 29 3b 0a  Goto, 0, addr);.
25b24 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
25b25 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 65  esolveLabel(v, e
25b26 6e 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 6c 6f  nd);..    /* Clo
25b27 73 65 20 74 68 65 20 63 75 72 73 6f 72 73 20 6f  se the cursors o
25b28 70 65 6e 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  pen on the table
25b29 20 61 6e 64 20 69 74 73 20 69 6e 64 65 78 65 73   and its indexes
25b2a 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 69 73  . */.    if( !is
25b2b 56 69 65 77 20 26 26 20 21 49 73 56 69 72 74 75  View && !IsVirtu
25b2c 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
25b2d 20 20 66 6f 72 28 69 3d 31 2c 20 70 49 64 78 3d    for(i=1, pIdx=
25b2e 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
25b2f 64 78 3b 20 69 2b 2b 2c 20 70 49 64 78 3d 70 49  dx; i++, pIdx=pI
25b30 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  dx->pNext){.    
25b31 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
25b32 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp2(v, OP_Clos
25b33 65 2c 20 69 43 75 72 20 2b 20 69 2c 20 70 49 64  e, iCur + i, pId
25b34 78 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20  x->tnum);.      
25b35 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  }.      sqlite3V
25b36 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
25b37 43 6c 6f 73 65 2c 20 69 43 75 72 29 3b 0a 20 20  Close, iCur);.  
25b38 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 70    }.  }..  /* Up
25b39 64 61 74 65 20 74 68 65 20 73 71 6c 69 74 65 5f  date the sqlite_
25b3a 73 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 62  sequence table b
25b3b 79 20 73 74 6f 72 69 6e 67 20 74 68 65 20 63 6f  y storing the co
25b3c 6e 74 65 6e 74 20 6f 66 20 74 68 65 0a 20 20 2a  ntent of the.  *
25b3d 2a 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20  * maximum rowid 
25b3e 63 6f 75 6e 74 65 72 20 76 61 6c 75 65 73 20 72  counter values r
25b3f 65 63 6f 72 64 65 64 20 77 68 69 6c 65 20 69 6e  ecorded while in
25b40 73 65 72 74 69 6e 67 20 69 6e 74 6f 0a 20 20 2a  serting into.  *
25b41 2a 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20  * autoincrement 
25b42 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69  tables..  */.  i
25b43 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  f( pParse->neste
25b44 64 3d 3d 30 20 26 26 20 70 50 61 72 73 65 2d 3e  d==0 && pParse->
25b45 70 54 72 69 67 67 65 72 54 61 62 3d 3d 30 20 29  pTriggerTab==0 )
25b46 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75 74  {.    sqlite3Aut
25b47 6f 69 6e 63 72 65 6d 65 6e 74 45 6e 64 28 70 50  oincrementEnd(pP
25b48 61 72 73 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  arse);.  }..  /*
25b49 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
25b4a 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20  er of rows that 
25b4b 77 65 72 65 20 64 65 6c 65 74 65 64 2e 20 49 66  were deleted. If
25b4c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
25b4d 20 0a 20 20 2a 2a 20 67 65 6e 65 72 61 74 69 6e   .  ** generatin
25b4e 67 20 63 6f 64 65 20 62 65 63 61 75 73 65 20 6f  g code because o
25b4f 66 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  f a call to sqli
25b50 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 29  te3NestedParse()
25b51 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 69 6e  , do not.  ** in
25b52 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63  voke the callbac
25b53 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f  k function..  */
25b54 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67  .  if( (db->flag
25b55 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f  s&SQLITE_CountRo
25b56 77 73 29 20 26 26 20 21 70 50 61 72 73 65 2d 3e  ws) && !pParse->
25b57 6e 65 73 74 65 64 20 26 26 20 21 70 50 61 72 73  nested && !pPars
25b58 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20 29  e->pTriggerTab )
25b59 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
25b5a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
25b5b 73 75 6c 74 52 6f 77 2c 20 6d 65 6d 43 6e 74 2c  sultRow, memCnt,
25b5c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   1);.    sqlite3
25b5d 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
25b5e 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 1);.    sqlite
25b5f 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
25b60 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 0, COLNAME_NA
25b61 4d 45 2c 20 22 72 6f 77 73 20 64 65 6c 65 74 65  ME, "rows delete
25b62 64 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  d", SQLITE_STATI
25b63 43 29 3b 0a 20 20 7d 0a 0a 64 65 6c 65 74 65 5f  C);.  }..delete_
25b64 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3a 0a 20 20  from_cleanup:.  
25b65 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65  sqlite3AuthConte
25b66 78 74 50 6f 70 28 26 73 43 6f 6e 74 65 78 74 29  xtPop(&sContext)
25b67 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  ;.  sqlite3SrcLi
25b68 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54 61  stDelete(db, pTa
25b69 62 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65  bList);.  sqlite
25b6a 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
25b6b 70 57 68 65 72 65 29 3b 0a 20 20 72 65 74 75 72  pWhere);.  retur
25b6c 6e 3b 0a 7d 0a 2f 2a 20 4d 61 6b 65 20 73 75 72  n;.}./* Make sur
25b6d 65 20 22 69 73 56 69 65 77 22 20 61 6e 64 20 6f  e "isView" and o
25b6e 74 68 65 72 20 6d 61 63 72 6f 73 20 64 65 66 69  ther macros defi
25b6f 6e 65 64 20 61 62 6f 76 65 20 61 72 65 20 75 6e  ned above are un
25b70 64 65 66 69 6e 65 64 2e 20 4f 74 68 65 72 77 69  defined. Otherwi
25b71 73 65 0a 2a 2a 20 74 68 65 6c 79 20 6d 61 79 20  se.** thely may 
25b72 69 6e 74 65 72 66 65 72 65 20 77 69 74 68 20 63  interfere with c
25b73 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20 6f 74  ompilation of ot
25b74 68 65 72 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e  her functions in
25b75 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 28 6f   this file.** (o
25b76 72 20 69 6e 20 61 6e 6f 74 68 65 72 20 66 69 6c  r in another fil
25b77 65 2c 20 69 66 20 74 68 69 73 20 66 69 6c 65 20  e, if this file 
25b78 62 65 63 6f 6d 65 73 20 70 61 72 74 20 6f 66 20  becomes part of 
25b79 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e  the amalgamation
25b7a 29 2e 20 20 2a 2f 0a 23 69 66 64 65 66 20 69 73  ).  */.#ifdef is
25b7b 56 69 65 77 0a 20 23 75 6e 64 65 66 20 69 73 56  View. #undef isV
25b7c 69 65 77 0a 23 65 6e 64 69 66 0a 23 69 66 64 65  iew.#endif.#ifde
25b7d 66 20 70 54 72 69 67 67 65 72 0a 20 23 75 6e 64  f pTrigger. #und
25b7e 65 66 20 70 54 72 69 67 67 65 72 0a 23 65 6e 64  ef pTrigger.#end
25b7f 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  if../*.** This r
25b80 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73  outine generates
25b81 20 56 44 42 45 20 63 6f 64 65 20 74 68 61 74 20   VDBE code that 
25b82 63 61 75 73 65 73 20 61 20 73 69 6e 67 6c 65 20  causes a single 
25b83 72 6f 77 20 6f 66 20 61 0a 2a 2a 20 73 69 6e 67  row of a.** sing
25b84 6c 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 64  le table to be d
25b85 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  eleted..**.** Th
25b86 65 20 56 44 42 45 20 6d 75 73 74 20 62 65 20 69  e VDBE must be i
25b87 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 73  n a particular s
25b88 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 20 72  tate when this r
25b89 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
25b8a 2e 0a 2a 2a 20 54 68 65 73 65 20 61 72 65 20 74  ..** These are t
25b8b 68 65 20 72 65 71 75 69 72 65 6d 65 6e 74 73 3a  he requirements:
25b8c 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 20 41 20 72  .**.**   1.  A r
25b8d 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72  ead/write cursor
25b8e 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 70 54 61   pointing to pTa
25b8f 62 2c 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6e  b, the table con
25b90 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 0a  taining the row.
25b91 2a 2a 20 20 20 20 20 20 20 74 6f 20 62 65 20 64  **       to be d
25b92 65 6c 65 74 65 64 2c 20 6d 75 73 74 20 62 65 20  eleted, must be 
25b93 6f 70 65 6e 65 64 20 61 73 20 63 75 72 73 6f 72  opened as cursor
25b94 20 6e 75 6d 62 65 72 20 24 69 43 75 72 2e 0a 2a   number $iCur..*
25b95 2a 0a 2a 2a 20 20 20 32 2e 20 20 52 65 61 64 2f  *.**   2.  Read/
25b96 77 72 69 74 65 20 63 75 72 73 6f 72 73 20 66 6f  write cursors fo
25b97 72 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66  r all indices of
25b98 20 70 54 61 62 20 6d 75 73 74 20 62 65 20 6f 70   pTab must be op
25b99 65 6e 20 61 73 0a 2a 2a 20 20 20 20 20 20 20 63  en as.**       c
25b9a 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 62 61 73  ursor number bas
25b9b 65 2b 69 20 66 6f 72 20 74 68 65 20 69 2d 74 68  e+i for the i-th
25b9c 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 20 20   index..**.**   
25b9d 33 2e 20 20 54 68 65 20 72 65 63 6f 72 64 20 6e  3.  The record n
25b9e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72 6f 77  umber of the row
25b9f 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20 6d   to be deleted m
25ba0 75 73 74 20 62 65 20 73 74 6f 72 65 64 20 69 6e  ust be stored in
25ba1 0a 2a 2a 20 20 20 20 20 20 20 6d 65 6d 6f 72 79  .**       memory
25ba2 20 63 65 6c 6c 20 69 52 6f 77 69 64 2e 0a 2a 2a   cell iRowid..**
25ba3 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
25ba4 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20   generates code 
25ba5 74 6f 20 72 65 6d 6f 76 65 20 62 6f 74 68 20 74  to remove both t
25ba6 68 65 20 74 61 62 6c 65 20 72 65 63 6f 72 64 20  he table record 
25ba7 61 6e 64 20 61 6c 6c 20 0a 2a 2a 20 69 6e 64 65  and all .** inde
25ba8 78 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 70  x entries that p
25ba9 6f 69 6e 74 20 74 6f 20 74 68 61 74 20 72 65 63  oint to that rec
25baa 6f 72 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ord..*/.SQLITE_P
25bab 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
25bac 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65  te3GenerateRowDe
25bad 6c 65 74 65 28 0a 20 20 50 61 72 73 65 20 2a 70  lete(.  Parse *p
25bae 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50 61  Parse,     /* Pa
25baf 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
25bb0 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20  .  Table *pTab, 
25bb1 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63        /* Table c
25bb2 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f  ontaining the ro
25bb3 77 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 20  w to be deleted 
25bb4 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20  */.  int iCur,  
25bb5 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f          /* Curso
25bb6 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65  r number for the
25bb7 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
25bb8 69 52 6f 77 69 64 2c 20 20 20 20 20 20 20 20 2f  iRowid,        /
25bb9 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68  * Memory cell th
25bba 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  at contains the 
25bbb 72 6f 77 69 64 20 74 6f 20 64 65 6c 65 74 65 20  rowid to delete 
25bbc 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74 2c 20  */.  int count, 
25bbd 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f          /* If no
25bbe 6e 2d 7a 65 72 6f 2c 20 69 6e 63 72 65 6d 65 6e  n-zero, incremen
25bbf 74 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65  t the row change
25bc0 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 54 72   counter */.  Tr
25bc1 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 2c  igger *pTrigger,
25bc2 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 72 69 67   /* List of trig
25bc3 67 65 72 73 20 74 6f 20 28 70 6f 74 65 6e 74 69  gers to (potenti
25bc4 61 6c 6c 79 29 20 66 69 72 65 20 2a 2f 0a 20 20  ally) fire */.  
25bc5 69 6e 74 20 6f 6e 63 6f 6e 66 20 20 20 20 20 20  int onconf      
25bc6 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 4f 4e     /* Default ON
25bc7 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79   CONFLICT policy
25bc8 20 66 6f 72 20 74 72 69 67 67 65 72 73 20 2a 2f   for triggers */
25bc9 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  .){.  Vdbe *v = 
25bca 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20  pParse->pVdbe;  
25bcb 20 20 20 20 20 20 2f 2a 20 56 64 62 65 20 2a 2f        /* Vdbe */
25bcc 0a 20 20 69 6e 74 20 69 4f 6c 64 20 3d 20 30 3b  .  int iOld = 0;
25bcd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25bce 20 20 20 2f 2a 20 46 69 72 73 74 20 72 65 67 69     /* First regi
25bcf 73 74 65 72 20 69 6e 20 4f 4c 44 2e 2a 20 61 72  ster in OLD.* ar
25bd0 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 61  ray */.  int iLa
25bd1 62 65 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  bel;            
25bd2 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62 65           /* Labe
25bd3 6c 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 65 6e  l resolved to en
25bd4 64 20 6f 66 20 67 65 6e 65 72 61 74 65 64 20 63  d of generated c
25bd5 6f 64 65 20 2a 2f 0a 0a 20 20 2f 2a 20 56 64 62  ode */..  /* Vdb
25bd6 65 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20  e is guaranteed 
25bd7 74 6f 20 68 61 76 65 20 62 65 65 6e 20 61 6c 6c  to have been all
25bd8 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 20 73  ocated by this s
25bd9 74 61 67 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72  tage. */.  asser
25bda 74 28 20 76 20 29 3b 0a 0a 20 20 2f 2a 20 53 65  t( v );..  /* Se
25bdb 65 6b 20 63 75 72 73 6f 72 20 69 43 75 72 20 74  ek cursor iCur t
25bdc 6f 20 74 68 65 20 72 6f 77 20 74 6f 20 64 65 6c  o the row to del
25bdd 65 74 65 2e 20 49 66 20 74 68 69 73 20 72 6f 77  ete. If this row
25bde 20 6e 6f 20 6c 6f 6e 67 65 72 20 65 78 69 73 74   no longer exist
25bdf 73 20 0a 20 20 2a 2a 20 28 74 68 69 73 20 63 61  s .  ** (this ca
25be0 6e 20 68 61 70 70 65 6e 20 69 66 20 61 20 74 72  n happen if a tr
25be1 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 68 61  igger program ha
25be2 73 20 61 6c 72 65 61 64 79 20 64 65 6c 65 74 65  s already delete
25be3 64 20 69 74 29 2c 20 64 6f 0a 20 20 2a 2a 20 6e  d it), do.  ** n
25be4 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 64 65  ot attempt to de
25be5 6c 65 74 65 20 69 74 20 6f 72 20 66 69 72 65 20  lete it or fire 
25be6 61 6e 79 20 44 45 4c 45 54 45 20 74 72 69 67 67  any DELETE trigg
25be7 65 72 73 2e 20 20 2a 2f 0a 20 20 69 4c 61 62 65  ers.  */.  iLabe
25be8 6c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  l = sqlite3VdbeM
25be9 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 73  akeLabel(v);.  s
25bea 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
25beb 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73  (v, OP_NotExists
25bec 2c 20 69 43 75 72 2c 20 69 4c 61 62 65 6c 2c 20  , iCur, iLabel, 
25bed 69 52 6f 77 69 64 29 3b 0a 20 0a 20 20 2f 2a 20  iRowid);. .  /* 
25bee 49 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79  If there are any
25bef 20 74 72 69 67 67 65 72 73 20 74 6f 20 66 69 72   triggers to fir
25bf0 65 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 72 61  e, allocate a ra
25bf1 6e 67 65 20 6f 66 20 72 65 67 69 73 74 65 72 73  nge of registers
25bf2 20 74 6f 0a 20 20 2a 2a 20 75 73 65 20 66 6f 72   to.  ** use for
25bf3 20 74 68 65 20 6f 6c 64 2e 2a 20 72 65 66 65 72   the old.* refer
25bf4 65 6e 63 65 73 20 69 6e 20 74 68 65 20 74 72 69  ences in the tri
25bf5 67 67 65 72 73 2e 20 20 2a 2f 0a 20 20 69 66 28  ggers.  */.  if(
25bf6 20 73 71 6c 69 74 65 33 46 6b 52 65 71 75 69 72   sqlite3FkRequir
25bf7 65 64 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ed(pParse, pTab,
25bf8 20 30 2c 20 30 29 20 7c 7c 20 70 54 72 69 67 67   0, 0) || pTrigg
25bf9 65 72 20 29 7b 0a 20 20 20 20 75 33 32 20 6d 61  er ){.    u32 ma
25bfa 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  sk;             
25bfb 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
25bfc 6f 66 20 4f 4c 44 2e 2a 20 63 6f 6c 75 6d 6e 73  of OLD.* columns
25bfd 20 69 6e 20 75 73 65 20 2a 2f 0a 20 20 20 20 69   in use */.    i
25bfe 6e 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20  nt iCol;        
25bff 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
25c00 49 74 65 72 61 74 6f 72 20 75 73 65 64 20 77 68  Iterator used wh
25c01 69 6c 65 20 70 6f 70 75 6c 61 74 69 6e 67 20 4f  ile populating O
25c02 4c 44 2e 2a 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20  LD.* */..    /* 
25c03 54 4f 44 4f 3a 20 43 6f 75 6c 64 20 75 73 65 20  TODO: Could use 
25c04 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74  temporary regist
25c05 65 72 73 20 68 65 72 65 2e 20 41 6c 73 6f 20 63  ers here. Also c
25c06 6f 75 6c 64 20 61 74 74 65 6d 70 74 20 74 6f 0a  ould attempt to.
25c07 20 20 20 20 2a 2a 20 61 76 6f 69 64 20 63 6f 70      ** avoid cop
25c08 79 69 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74  ying the content
25c09 73 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 72  s of the rowid r
25c0a 65 67 69 73 74 65 72 2e 20 20 2a 2f 0a 20 20 20  egister.  */.   
25c0b 20 6d 61 73 6b 20 3d 20 73 71 6c 69 74 65 33 54   mask = sqlite3T
25c0c 72 69 67 67 65 72 4f 6c 64 6d 61 73 6b 28 70 50  riggerOldmask(pP
25c0d 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20  arse, pTrigger, 
25c0e 30 2c 20 70 54 61 62 2c 20 6f 6e 63 6f 6e 66 29  0, pTab, onconf)
25c0f 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 73 71  ;.    mask |= sq
25c10 6c 69 74 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 70  lite3FkOldmask(p
25c11 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20  Parse, pTab);.  
25c12 20 20 69 4f 6c 64 20 3d 20 70 50 61 72 73 65 2d    iOld = pParse-
25c13 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 20 20 70 50 61  >nMem+1;.    pPa
25c14 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 28 31 20  rse->nMem += (1 
25c15 2b 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 0a  + pTab->nCol);..
25c16 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61 74 65 20      /* Populate 
25c17 74 68 65 20 4f 4c 44 2e 2a 20 70 73 65 75 64 6f  the OLD.* pseudo
25c18 2d 74 61 62 6c 65 20 72 65 67 69 73 74 65 72 20  -table register 
25c19 61 72 72 61 79 2e 20 54 68 65 73 65 20 76 61 6c  array. These val
25c1a 75 65 73 20 77 69 6c 6c 20 62 65 20 0a 20 20 20  ues will be .   
25c1b 20 2a 2a 20 75 73 65 64 20 62 79 20 61 6e 79 20   ** used by any 
25c1c 42 45 46 4f 52 45 20 61 6e 64 20 41 46 54 45 52  BEFORE and AFTER
25c1d 20 74 72 69 67 67 65 72 73 20 74 68 61 74 20 65   triggers that e
25c1e 78 69 73 74 2e 20 20 2a 2f 0a 20 20 20 20 73 71  xist.  */.    sq
25c1f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
25c20 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 69 52 6f 77  v, OP_Copy, iRow
25c21 69 64 2c 20 69 4f 6c 64 29 3b 0a 20 20 20 20 66  id, iOld);.    f
25c22 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c  or(iCol=0; iCol<
25c23 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c  pTab->nCol; iCol
25c24 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6d  ++){.      if( m
25c25 61 73 6b 3d 3d 30 78 66 66 66 66 66 66 66 66 20  ask==0xffffffff 
25c26 7c 7c 20 6d 61 73 6b 26 28 31 3c 3c 69 43 6f 6c  || mask&(1<<iCol
25c27 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  ) ){.        int
25c28 20 69 54 61 72 67 65 74 20 3d 20 69 4f 6c 64 20   iTarget = iOld 
25c29 2b 20 69 43 6f 6c 20 2b 20 31 3b 0a 20 20 20 20  + iCol + 1;.    
25c2a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
25c2b 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
25c2c 6d 6e 2c 20 69 43 75 72 2c 20 69 43 6f 6c 2c 20  mn, iCur, iCol, 
25c2d 69 54 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20  iTarget);.      
25c2e 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44    sqlite3ColumnD
25c2f 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62 2c 20  efault(v, pTab, 
25c30 69 43 6f 6c 2c 20 69 54 61 72 67 65 74 29 3b 0a  iCol, iTarget);.
25c31 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
25c32 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 42 45 46     /* Invoke BEF
25c33 4f 52 45 20 44 45 4c 45 54 45 20 74 72 69 67 67  ORE DELETE trigg
25c34 65 72 20 70 72 6f 67 72 61 6d 73 2e 20 2a 2f 0a  er programs. */.
25c35 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 52      sqlite3CodeR
25c36 6f 77 54 72 69 67 67 65 72 28 70 50 61 72 73 65  owTrigger(pParse
25c37 2c 20 70 54 72 69 67 67 65 72 2c 20 0a 20 20 20  , pTrigger, .   
25c38 20 20 20 20 20 54 4b 5f 44 45 4c 45 54 45 2c 20       TK_DELETE, 
25c39 30 2c 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52  0, TRIGGER_BEFOR
25c3a 45 2c 20 70 54 61 62 2c 20 69 4f 6c 64 2c 20 6f  E, pTab, iOld, o
25c3b 6e 63 6f 6e 66 2c 20 69 4c 61 62 65 6c 0a 20 20  nconf, iLabel.  
25c3c 20 20 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 65    );..    /* See
25c3d 6b 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20  k the cursor to 
25c3e 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20 64 65  the row to be de
25c3f 6c 65 74 65 64 20 61 67 61 69 6e 2e 20 49 74 20  leted again. It 
25c40 6d 61 79 20 62 65 20 74 68 61 74 0a 20 20 20 20  may be that.    
25c41 2a 2a 20 74 68 65 20 42 45 46 4f 52 45 20 74 72  ** the BEFORE tr
25c42 69 67 67 65 72 73 20 63 6f 64 65 64 20 61 62 6f  iggers coded abo
25c43 76 65 20 68 61 76 65 20 61 6c 72 65 61 64 79 20  ve have already 
25c44 72 65 6d 6f 76 65 64 20 74 68 65 20 72 6f 77 0a  removed the row.
25c45 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 64 65 6c      ** being del
25c46 65 74 65 64 2e 20 44 6f 20 6e 6f 74 20 61 74 74  eted. Do not att
25c47 65 6d 70 74 20 74 6f 20 64 65 6c 65 74 65 20 74  empt to delete t
25c48 68 65 20 72 6f 77 20 61 20 73 65 63 6f 6e 64 20  he row a second 
25c49 74 69 6d 65 2c 20 61 6e 64 20 0a 20 20 20 20 2a  time, and .    *
25c4a 2a 20 64 6f 20 6e 6f 74 20 66 69 72 65 20 41 46  * do not fire AF
25c4b 54 45 52 20 74 72 69 67 67 65 72 73 2e 20 20 2a  TER triggers.  *
25c4c 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
25c4d 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f  eAddOp3(v, OP_No
25c4e 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 69  tExists, iCur, i
25c4f 4c 61 62 65 6c 2c 20 69 52 6f 77 69 64 29 3b 0a  Label, iRowid);.
25c50 0a 20 20 20 20 2f 2a 20 44 6f 20 46 4b 20 70 72  .    /* Do FK pr
25c51 6f 63 65 73 73 69 6e 67 2e 20 54 68 69 73 20 63  ocessing. This c
25c52 61 6c 6c 20 63 68 65 63 6b 73 20 74 68 61 74 20  all checks that 
25c53 61 6e 79 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e  any FK constrain
25c54 74 73 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 72  ts that.    ** r
25c55 65 66 65 72 20 74 6f 20 74 68 69 73 20 74 61 62  efer to this tab
25c56 6c 65 20 28 69 2e 65 2e 20 63 6f 6e 73 74 72 61  le (i.e. constra
25c57 69 6e 74 73 20 61 74 74 61 63 68 65 64 20 74 6f  ints attached to
25c58 20 6f 74 68 65 72 20 74 61 62 6c 65 73 29 20 0a   other tables) .
25c59 20 20 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 76      ** are not v
25c5a 69 6f 6c 61 74 65 64 20 62 79 20 64 65 6c 65 74  iolated by delet
25c5b 69 6e 67 20 74 68 69 73 20 72 6f 77 2e 20 20 2a  ing this row.  *
25c5c 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 46 6b 43  /.    sqlite3FkC
25c5d 68 65 63 6b 28 70 50 61 72 73 65 2c 20 70 54 61  heck(pParse, pTa
25c5e 62 2c 20 69 4f 6c 64 2c 20 30 29 3b 0a 20 20 7d  b, iOld, 0);.  }
25c5f 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68  ..  /* Delete th
25c60 65 20 69 6e 64 65 78 20 61 6e 64 20 74 61 62 6c  e index and tabl
25c61 65 20 65 6e 74 72 69 65 73 2e 20 53 6b 69 70 20  e entries. Skip 
25c62 74 68 69 73 20 73 74 65 70 20 69 66 20 70 54 61  this step if pTa
25c63 62 20 69 73 20 72 65 61 6c 6c 79 0a 20 20 2a 2a  b is really.  **
25c64 20 61 20 76 69 65 77 20 28 69 6e 20 77 68 69 63   a view (in whic
25c65 68 20 63 61 73 65 20 74 68 65 20 6f 6e 6c 79 20  h case the only 
25c66 65 66 66 65 63 74 20 6f 66 20 74 68 65 20 44 45  effect of the DE
25c67 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69  LETE statement i
25c68 73 20 74 6f 0a 20 20 2a 2a 20 66 69 72 65 20 74  s to.  ** fire t
25c69 68 65 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72  he INSTEAD OF tr
25c6a 69 67 67 65 72 73 29 2e 20 20 2a 2f 20 0a 20 20  iggers).  */ .  
25c6b 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63  if( pTab->pSelec
25c6c 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  t==0 ){.    sqli
25c6d 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e  te3GenerateRowIn
25c6e 64 65 78 44 65 6c 65 74 65 28 70 50 61 72 73 65  dexDelete(pParse
25c6f 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 30 29  , pTab, iCur, 0)
25c70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
25c71 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 44 65  eAddOp2(v, OP_De
25c72 6c 65 74 65 2c 20 69 43 75 72 2c 20 28 63 6f 75  lete, iCur, (cou
25c73 6e 74 3f 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47  nt?OPFLAG_NCHANG
25c74 45 3a 30 29 29 3b 0a 20 20 20 20 69 66 28 20 63  E:0));.    if( c
25c75 6f 75 6e 74 20 29 7b 0a 20 20 20 20 20 20 73 71  ount ){.      sq
25c76 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
25c77 34 28 76 2c 20 2d 31 2c 20 70 54 61 62 2d 3e 7a  4(v, -1, pTab->z
25c78 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29  Name, P4_STATIC)
25c79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
25c7a 2a 20 44 6f 20 61 6e 79 20 4f 4e 20 43 41 53 43  * Do any ON CASC
25c7b 41 44 45 2c 20 53 45 54 20 4e 55 4c 4c 20 6f 72  ADE, SET NULL or
25c7c 20 53 45 54 20 44 45 46 41 55 4c 54 20 6f 70 65   SET DEFAULT ope
25c7d 72 61 74 69 6f 6e 73 20 72 65 71 75 69 72 65 64  rations required
25c7e 20 74 6f 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20   to.  ** handle 
25c7f 72 6f 77 73 20 28 70 6f 73 73 69 62 6c 79 20 69  rows (possibly i
25c80 6e 20 6f 74 68 65 72 20 74 61 62 6c 65 73 29 20  n other tables) 
25c81 74 68 61 74 20 72 65 66 65 72 20 76 69 61 20 61  that refer via a
25c82 20 66 6f 72 65 69 67 6e 20 6b 65 79 0a 20 20 2a   foreign key.  *
25c83 2a 20 74 6f 20 74 68 65 20 72 6f 77 20 6a 75 73  * to the row jus
25c84 74 20 64 65 6c 65 74 65 64 2e 20 2a 2f 20 0a 20  t deleted. */ . 
25c85 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69 6f 6e   sqlite3FkAction
25c86 73 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  s(pParse, pTab, 
25c87 30 2c 20 69 4f 6c 64 29 3b 0a 0a 20 20 2f 2a 20  0, iOld);..  /* 
25c88 49 6e 76 6f 6b 65 20 41 46 54 45 52 20 44 45 4c  Invoke AFTER DEL
25c89 45 54 45 20 74 72 69 67 67 65 72 20 70 72 6f 67  ETE trigger prog
25c8a 72 61 6d 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  rams. */.  sqlit
25c8b 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72  e3CodeRowTrigger
25c8c 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67 65  (pParse, pTrigge
25c8d 72 2c 20 0a 20 20 20 20 20 20 54 4b 5f 44 45 4c  r, .      TK_DEL
25c8e 45 54 45 2c 20 30 2c 20 54 52 49 47 47 45 52 5f  ETE, 0, TRIGGER_
25c8f 41 46 54 45 52 2c 20 70 54 61 62 2c 20 69 4f 6c  AFTER, pTab, iOl
25c90 64 2c 20 6f 6e 63 6f 6e 66 2c 20 69 4c 61 62 65  d, onconf, iLabe
25c91 6c 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 4a 75 6d  l.  );..  /* Jum
25c92 70 20 68 65 72 65 20 69 66 20 74 68 65 20 72 6f  p here if the ro
25c93 77 20 68 61 64 20 61 6c 72 65 61 64 79 20 62 65  w had already be
25c94 65 6e 20 64 65 6c 65 74 65 64 20 62 65 66 6f 72  en deleted befor
25c95 65 20 61 6e 79 20 42 45 46 4f 52 45 0a 20 20 2a  e any BEFORE.  *
25c96 2a 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  * trigger progra
25c97 6d 73 20 77 65 72 65 20 69 6e 76 6f 6b 65 64 2e  ms were invoked.
25c98 20 4f 72 20 69 66 20 61 20 74 72 69 67 67 65 72   Or if a trigger
25c99 20 70 72 6f 67 72 61 6d 20 74 68 72 6f 77 73 20   program throws 
25c9a 61 20 0a 20 20 2a 2a 20 52 41 49 53 45 28 49 47  a .  ** RAISE(IG
25c9b 4e 4f 52 45 29 20 65 78 63 65 70 74 69 6f 6e 2e  NORE) exception.
25c9c 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64    */.  sqlite3Vd
25c9d 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
25c9e 2c 20 69 4c 61 62 65 6c 29 3b 0a 7d 0a 0a 2f 2a  , iLabel);.}../*
25c9f 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
25ca0 20 67 65 6e 65 72 61 74 65 73 20 56 44 42 45 20   generates VDBE 
25ca1 63 6f 64 65 20 74 68 61 74 20 63 61 75 73 65 73  code that causes
25ca2 20 74 68 65 20 64 65 6c 65 74 69 6f 6e 20 6f 66   the deletion of
25ca3 20 61 6c 6c 0a 2a 2a 20 69 6e 64 65 78 20 65 6e   all.** index en
25ca4 74 72 69 65 73 20 61 73 73 6f 63 69 61 74 65 64  tries associated
25ca5 20 77 69 74 68 20 61 20 73 69 6e 67 6c 65 20 72   with a single r
25ca6 6f 77 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 74  ow of a single t
25ca7 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  able..**.** The 
25ca8 56 44 42 45 20 6d 75 73 74 20 62 65 20 69 6e 20  VDBE must be in 
25ca9 61 20 70 61 72 74 69 63 75 6c 61 72 20 73 74 61  a particular sta
25caa 74 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  te when this rou
25cab 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a  tine is called..
25cac 2a 2a 20 54 68 65 73 65 20 61 72 65 20 74 68 65  ** These are the
25cad 20 72 65 71 75 69 72 65 6d 65 6e 74 73 3a 0a 2a   requirements:.*
25cae 2a 0a 2a 2a 20 20 20 31 2e 20 20 41 20 72 65 61  *.**   1.  A rea
25caf 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 20 70  d/write cursor p
25cb0 6f 69 6e 74 69 6e 67 20 74 6f 20 70 54 61 62 2c  ointing to pTab,
25cb1 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61   the table conta
25cb2 69 6e 69 6e 67 20 74 68 65 20 72 6f 77 0a 2a 2a  ining the row.**
25cb3 20 20 20 20 20 20 20 74 6f 20 62 65 20 64 65 6c         to be del
25cb4 65 74 65 64 2c 20 6d 75 73 74 20 62 65 20 6f 70  eted, must be op
25cb5 65 6e 65 64 20 61 73 20 63 75 72 73 6f 72 20 6e  ened as cursor n
25cb6 75 6d 62 65 72 20 22 69 43 75 72 22 2e 0a 2a 2a  umber "iCur"..**
25cb7 0a 2a 2a 20 20 20 32 2e 20 20 52 65 61 64 2f 77  .**   2.  Read/w
25cb8 72 69 74 65 20 63 75 72 73 6f 72 73 20 66 6f 72  rite cursors for
25cb9 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20   all indices of 
25cba 70 54 61 62 20 6d 75 73 74 20 62 65 20 6f 70 65  pTab must be ope
25cbb 6e 20 61 73 0a 2a 2a 20 20 20 20 20 20 20 63 75  n as.**       cu
25cbc 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 43 75 72  rsor number iCur
25cbd 2b 69 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20  +i for the i-th 
25cbe 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 20 20 33  index..**.**   3
25cbf 2e 20 20 54 68 65 20 22 69 43 75 72 22 20 63 75  .  The "iCur" cu
25cc0 72 73 6f 72 20 6d 75 73 74 20 62 65 20 70 6f 69  rsor must be poi
25cc1 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 72 6f 77  nting to the row
25cc2 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 0a 2a   that is to be.*
25cc3 2a 20 20 20 20 20 20 20 64 65 6c 65 74 65 64 2e  *       deleted.
25cc4 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
25cc5 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47  TE void sqlite3G
25cc6 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44  enerateRowIndexD
25cc7 65 6c 65 74 65 28 0a 20 20 50 61 72 73 65 20 2a  elete(.  Parse *
25cc8 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 50  pParse,     /* P
25cc9 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64 65 20  arsing and code 
25cca 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e 74 65  generating conte
25ccb 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  xt */.  Table *p
25ccc 54 61 62 2c 20 20 20 20 20 20 20 2f 2a 20 54 61  Tab,       /* Ta
25ccd 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ble containing t
25cce 68 65 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c  he row to be del
25ccf 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43  eted */.  int iC
25cd0 75 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ur,          /* 
25cd1 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f  Cursor number fo
25cd2 72 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20  r the table */. 
25cd3 20 69 6e 74 20 2a 61 52 65 67 49 64 78 20 20 20   int *aRegIdx   
25cd4 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 64 65 6c 65      /* Only dele
25cd5 74 65 20 69 66 20 61 52 65 67 49 64 78 21 3d 30  te if aRegIdx!=0
25cd6 20 26 26 20 61 52 65 67 49 64 78 5b 69 5d 3e 30   && aRegIdx[i]>0
25cd7 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
25cd8 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20    Index *pIdx;. 
25cd9 20 69 6e 74 20 72 31 3b 0a 0a 20 20 66 6f 72 28   int r1;..  for(
25cda 69 3d 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e  i=1, pIdx=pTab->
25cdb 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 69 2b  pIndex; pIdx; i+
25cdc 2b 2c 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  +, pIdx=pIdx->pN
25cdd 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 61 52  ext){.    if( aR
25cde 65 67 49 64 78 21 3d 30 20 26 26 20 61 52 65 67  egIdx!=0 && aReg
25cdf 49 64 78 5b 69 2d 31 5d 3d 3d 30 20 29 20 63 6f  Idx[i-1]==0 ) co
25ce0 6e 74 69 6e 75 65 3b 0a 20 20 20 20 72 31 20 3d  ntinue;.    r1 =
25ce1 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
25ce2 49 6e 64 65 78 4b 65 79 28 70 50 61 72 73 65 2c  IndexKey(pParse,
25ce3 20 70 49 64 78 2c 20 69 43 75 72 2c 20 30 2c 20   pIdx, iCur, 0, 
25ce4 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
25ce5 64 62 65 41 64 64 4f 70 33 28 70 50 61 72 73 65  dbeAddOp3(pParse
25ce6 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 49 64 78 44  ->pVdbe, OP_IdxD
25ce7 65 6c 65 74 65 2c 20 69 43 75 72 2b 69 2c 20 72  elete, iCur+i, r
25ce8 31 2c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b  1,pIdx->nColumn+
25ce9 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  1);.  }.}../*.**
25cea 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
25ceb 68 61 74 20 77 69 6c 6c 20 61 73 73 65 6d 62 6c  hat will assembl
25cec 65 20 61 6e 20 69 6e 64 65 78 20 6b 65 79 20 61  e an index key a
25ced 6e 64 20 70 75 74 20 69 74 20 69 6e 20 72 65 67  nd put it in reg
25cee 69 73 74 65 72 0a 2a 2a 20 72 65 67 4f 75 74 2e  ister.** regOut.
25cef 20 20 54 68 65 20 6b 65 79 20 77 69 74 68 20 62    The key with b
25cf0 65 20 66 6f 72 20 69 6e 64 65 78 20 70 49 64 78  e for index pIdx
25cf1 20 77 68 69 63 68 20 69 73 20 61 6e 20 69 6e 64   which is an ind
25cf2 65 78 20 6f 6e 20 70 54 61 62 2e 0a 2a 2a 20 69  ex on pTab..** i
25cf3 43 75 72 20 69 73 20 74 68 65 20 69 6e 64 65 78  Cur is the index
25cf4 20 6f 66 20 61 20 63 75 72 73 6f 72 20 6f 70 65   of a cursor ope
25cf5 6e 20 6f 6e 20 74 68 65 20 70 54 61 62 20 74 61  n on the pTab ta
25cf6 62 6c 65 20 61 6e 64 20 70 6f 69 6e 74 69 6e 67  ble and pointing
25cf7 20 74 6f 0a 2a 2a 20 74 68 65 20 65 6e 74 72 79   to.** the entry
25cf8 20 74 68 61 74 20 6e 65 65 64 73 20 69 6e 64 65   that needs inde
25cf9 78 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  xing..**.** Retu
25cfa 72 6e 20 61 20 72 65 67 69 73 74 65 72 20 6e 75  rn a register nu
25cfb 6d 62 65 72 20 77 68 69 63 68 20 69 73 20 74 68  mber which is th
25cfc 65 20 66 69 72 73 74 20 69 6e 20 61 20 62 6c 6f  e first in a blo
25cfd 63 6b 20 6f 66 0a 2a 2a 20 72 65 67 69 73 74 65  ck of.** registe
25cfe 72 73 20 74 68 61 74 20 68 6f 6c 64 73 20 74 68  rs that holds th
25cff 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68  e elements of th
25d00 65 20 69 6e 64 65 78 20 6b 65 79 2e 20 20 54 68  e index key.  Th
25d01 65 0a 2a 2a 20 62 6c 6f 63 6b 20 6f 66 20 72 65  e.** block of re
25d02 67 69 73 74 65 72 73 20 68 61 73 20 61 6c 72 65  gisters has alre
25d03 61 64 79 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63  ady been dealloc
25d04 61 74 65 64 20 62 79 20 74 68 65 20 74 69 6d 65  ated by the time
25d05 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
25d06 20 72 65 74 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c   returns..*/.SQL
25d07 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
25d08 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 49  sqlite3GenerateI
25d09 6e 64 65 78 4b 65 79 28 0a 20 20 50 61 72 73 65  ndexKey(.  Parse
25d0a 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a   *pParse,     /*
25d0b 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
25d0c 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64   */.  Index *pId
25d0d 78 2c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  x,       /* The 
25d0e 69 6e 64 65 78 20 66 6f 72 20 77 68 69 63 68 20  index for which 
25d0f 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 6b 65  to generate a ke
25d10 79 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c  y */.  int iCur,
25d11 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72            /* Cur
25d12 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  sor number for t
25d13 68 65 20 70 49 64 78 2d 3e 70 54 61 62 6c 65 20  he pIdx->pTable 
25d14 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 72  table */.  int r
25d15 65 67 4f 75 74 2c 20 20 20 20 20 20 20 20 2f 2a  egOut,        /*
25d16 20 57 72 69 74 65 20 74 68 65 20 6e 65 77 20 69   Write the new i
25d17 6e 64 65 78 20 6b 65 79 20 74 6f 20 74 68 69 73  ndex key to this
25d18 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69   register */.  i
25d19 6e 74 20 64 6f 4d 61 6b 65 52 65 63 20 20 20 20  nt doMakeRec    
25d1a 20 20 2f 2a 20 52 75 6e 20 74 68 65 20 4f 50 5f    /* Run the OP_
25d1b 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e 73 74 72  MakeRecord instr
25d1c 75 63 74 69 6f 6e 20 69 66 20 74 72 75 65 20 2a  uction if true *
25d1d 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d  /.){.  Vdbe *v =
25d1e 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
25d1f 20 20 69 6e 74 20 6a 3b 0a 20 20 54 61 62 6c 65    int j;.  Table
25d20 20 2a 70 54 61 62 20 3d 20 70 49 64 78 2d 3e 70   *pTab = pIdx->p
25d21 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20 72 65 67  Table;.  int reg
25d22 42 61 73 65 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c  Base;.  int nCol
25d23 3b 0a 0a 20 20 6e 43 6f 6c 20 3d 20 70 49 64 78  ;..  nCol = pIdx
25d24 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 72 65 67  ->nColumn;.  reg
25d25 42 61 73 65 20 3d 20 73 71 6c 69 74 65 33 47 65  Base = sqlite3Ge
25d26 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  tTempRange(pPars
25d27 65 2c 20 6e 43 6f 6c 2b 31 29 3b 0a 20 20 73 71  e, nCol+1);.  sq
25d28 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
25d29 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75  v, OP_Rowid, iCu
25d2a 72 2c 20 72 65 67 42 61 73 65 2b 6e 43 6f 6c 29  r, regBase+nCol)
25d2b 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e  ;.  for(j=0; j<n
25d2c 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 69  Col; j++){.    i
25d2d 6e 74 20 69 64 78 20 3d 20 70 49 64 78 2d 3e 61  nt idx = pIdx->a
25d2e 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20 20 20  iColumn[j];.    
25d2f 69 66 28 20 69 64 78 3d 3d 70 54 61 62 2d 3e 69  if( idx==pTab->i
25d30 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 73 71  PKey ){.      sq
25d31 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
25d32 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67  v, OP_SCopy, reg
25d33 42 61 73 65 2b 6e 43 6f 6c 2c 20 72 65 67 42 61  Base+nCol, regBa
25d34 73 65 2b 6a 29 3b 0a 20 20 20 20 7d 65 6c 73 65  se+j);.    }else
25d35 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
25d36 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
25d37 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69 64  Column, iCur, id
25d38 78 2c 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20  x, regBase+j);. 
25d39 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75       sqlite3Colu
25d3a 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61  mnDefault(v, pTa
25d3b 62 2c 20 69 64 78 2c 20 2d 31 29 3b 0a 20 20 20  b, idx, -1);.   
25d3c 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 64 6f 4d   }.  }.  if( doM
25d3d 61 6b 65 52 65 63 20 29 7b 0a 20 20 20 20 73 71  akeRec ){.    sq
25d3e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
25d3f 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  v, OP_MakeRecord
25d40 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2b  , regBase, nCol+
25d41 31 2c 20 72 65 67 4f 75 74 29 3b 0a 20 20 20 20  1, regOut);.    
25d42 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
25d43 65 50 34 28 76 2c 20 2d 31 2c 20 73 71 6c 69 74  eP4(v, -1, sqlit
25d44 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 53  e3IndexAffinityS
25d45 74 72 28 76 2c 20 70 49 64 78 29 2c 20 30 29 3b  tr(v, pIdx), 0);
25d46 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
25d47 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
25d48 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 42  nge(pParse, regB
25d49 61 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b 0a 20 20  ase, nCol+1);.  
25d4a 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61  }.  sqlite3Relea
25d4b 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  seTempRange(pPar
25d4c 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f  se, regBase, nCo
25d4d 6c 2b 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  l+1);.  return r
25d4e 65 67 42 61 73 65 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a  egBase;.}.../***
25d4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
25d50 6f 66 20 64 65 6c 65 74 65 2e 63 20 2a 2a 2a 2a  of delete.c ****
25d51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
25d54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
25d55 6e 20 66 69 6c 65 20 66 75 6e 63 2e 63 20 2a 2a  n file func.c **
25d56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d57 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
25d59 2a 20 32 30 30 32 20 46 65 62 72 75 61 72 79 20  * 2002 February 
25d5a 32 33 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  23.**.** The aut
25d5b 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
25d5c 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
25d5d 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
25d5e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
25d5f 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
25d60 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
25d61 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
25d62 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
25d63 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
25d64 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
25d65 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
25d66 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
25d67 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
25d68 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
25d69 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
25d6a 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
25d6b 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
25d6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
25d70 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
25d71 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75  ontains the C fu
25d72 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70  nctions that imp
25d73 6c 65 6d 65 6e 74 20 76 61 72 69 6f 75 73 20 53  lement various S
25d74 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20  QL.** functions 
25d75 6f 66 20 53 51 4c 69 74 65 2e 20 20 0a 2a 2a 0a  of SQLite.  .**.
25d76 2a 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 6c 79  ** There is only
25d77 20 6f 6e 65 20 65 78 70 6f 72 74 65 64 20 73 79   one exported sy
25d78 6d 62 6f 6c 20 69 6e 20 74 68 69 73 20 66 69 6c  mbol in this fil
25d79 65 20 2d 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  e - the function
25d7a 0a 2a 2a 20 73 71 6c 69 74 65 52 65 67 69 73 74  .** sqliteRegist
25d7b 65 72 42 75 69 6c 64 69 6e 46 75 6e 63 74 69 6f  erBuildinFunctio
25d7c 6e 73 28 29 20 66 6f 75 6e 64 20 61 74 20 74 68  ns() found at th
25d7d 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20  e bottom of the 
25d7e 66 69 6c 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68  file..** All oth
25d7f 65 72 20 63 6f 64 65 20 68 61 73 20 66 69 6c 65  er code has file
25d80 20 73 63 6f 70 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a   scope..*/../*.*
25d81 2a 20 52 65 74 75 72 6e 20 74 68 65 20 63 6f 6c  * Return the col
25d82 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  lating function 
25d83 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
25d84 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  a function..*/.s
25d85 74 61 74 69 63 20 43 6f 6c 6c 53 65 71 20 2a 73  tatic CollSeq *s
25d86 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c  qlite3GetFuncCol
25d87 6c 53 65 71 28 73 71 6c 69 74 65 33 5f 63 6f 6e  lSeq(sqlite3_con
25d88 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a  text *context){.
25d89 20 20 72 65 74 75 72 6e 20 63 6f 6e 74 65 78 74    return context
25d8a 2d 3e 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a  ->pColl;.}../*.*
25d8b 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
25d8c 20 6f 66 20 74 68 65 20 6e 6f 6e 2d 61 67 67 72   of the non-aggr
25d8d 65 67 61 74 65 20 6d 69 6e 28 29 20 61 6e 64 20  egate min() and 
25d8e 6d 61 78 28 29 20 66 75 6e 63 74 69 6f 6e 73 0a  max() functions.
25d8f 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  */.static void m
25d90 69 6e 6d 61 78 46 75 6e 63 28 0a 20 20 73 71 6c  inmaxFunc(.  sql
25d91 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
25d92 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
25d93 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
25d94 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69  ue **argv.){.  i
25d95 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6d 61 73 6b  nt i;.  int mask
25d96 3b 20 20 20 20 2f 2a 20 30 20 66 6f 72 20 6d 69  ;    /* 0 for mi
25d97 6e 28 29 20 6f 72 20 30 78 66 66 66 66 66 66 66  n() or 0xfffffff
25d98 66 20 66 6f 72 20 6d 61 78 28 29 20 2a 2f 0a 20  f for max() */. 
25d99 20 69 6e 74 20 69 42 65 73 74 3b 0a 20 20 43 6f   int iBest;.  Co
25d9a 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20  llSeq *pColl;.. 
25d9b 20 61 73 73 65 72 74 28 20 61 72 67 63 3e 31 20   assert( argc>1 
25d9c 29 3b 0a 20 20 6d 61 73 6b 20 3d 20 73 71 6c 69  );.  mask = sqli
25d9d 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f  te3_user_data(co
25d9e 6e 74 65 78 74 29 3d 3d 30 20 3f 20 30 20 3a 20  ntext)==0 ? 0 : 
25d9f 2d 31 3b 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71  -1;.  pColl = sq
25da0 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f 6c 6c  lite3GetFuncColl
25da1 53 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  Seq(context);.  
25da2 61 73 73 65 72 74 28 20 70 43 6f 6c 6c 20 29 3b  assert( pColl );
25da3 0a 20 20 61 73 73 65 72 74 28 20 6d 61 73 6b 3d  .  assert( mask=
25da4 3d 2d 31 20 7c 7c 20 6d 61 73 6b 3d 3d 30 20 29  =-1 || mask==0 )
25da5 3b 0a 20 20 69 42 65 73 74 20 3d 20 30 3b 0a 20  ;.  iBest = 0;. 
25da6 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
25da7 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29  ue_type(argv[0])
25da8 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20  ==SQLITE_NULL ) 
25da9 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69 3d  return;.  for(i=
25daa 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b  1; i<argc; i++){
25dab 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
25dac 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
25dad 5b 69 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c  [i])==SQLITE_NUL
25dae 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  L ) return;.    
25daf 69 66 28 20 28 73 71 6c 69 74 65 33 4d 65 6d 43  if( (sqlite3MemC
25db0 6f 6d 70 61 72 65 28 61 72 67 76 5b 69 42 65 73  ompare(argv[iBes
25db1 74 5d 2c 20 61 72 67 76 5b 69 5d 2c 20 70 43 6f  t], argv[i], pCo
25db2 6c 6c 29 5e 6d 61 73 6b 29 3e 3d 30 20 29 7b 0a  ll)^mask)>=0 ){.
25db3 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
25db4 6d 61 73 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 20  mask==0 );.     
25db5 20 69 42 65 73 74 20 3d 20 69 3b 0a 20 20 20 20   iBest = i;.    
25db6 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  }.  }.  sqlite3_
25db7 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e  result_value(con
25db8 74 65 78 74 2c 20 61 72 67 76 5b 69 42 65 73 74  text, argv[iBest
25db9 5d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ]);.}../*.** Ret
25dba 75 72 6e 20 74 68 65 20 74 79 70 65 20 6f 66 20  urn the type of 
25dbb 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f  the argument..*/
25dbc 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 79 70  .static void typ
25dbd 65 6f 66 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  eofFunc(.  sqlit
25dbe 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
25dbf 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73  ext,.  int NotUs
25dc0 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  ed,.  sqlite3_va
25dc1 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
25dc2 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20  const char *z = 
25dc3 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  0;.  UNUSED_PARA
25dc4 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
25dc5 20 20 73 77 69 74 63 68 28 20 73 71 6c 69 74 65    switch( sqlite
25dc6 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
25dc7 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63 61 73  v[0]) ){.    cas
25dc8 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  e SQLITE_INTEGER
25dc9 3a 20 7a 20 3d 20 22 69 6e 74 65 67 65 72 22 3b  : z = "integer";
25dca 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
25dcb 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 20 20   SQLITE_TEXT:   
25dcc 20 7a 20 3d 20 22 74 65 78 74 22 3b 20 20 20 20   z = "text";    
25dcd 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
25dce 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 20 20  SQLITE_FLOAT:   
25dcf 7a 20 3d 20 22 72 65 61 6c 22 3b 20 20 20 20 62  z = "real";    b
25dd0 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
25dd1 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 20 20 20 7a  QLITE_BLOB:    z
25dd2 20 3d 20 22 62 6c 6f 62 22 3b 20 20 20 20 62 72   = "blob";    br
25dd3 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74  eak;.    default
25dd4 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 20  :             z 
25dd5 3d 20 22 6e 75 6c 6c 22 3b 20 20 20 20 62 72 65  = "null";    bre
25dd6 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ak;.  }.  sqlite
25dd7 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f  3_result_text(co
25dd8 6e 74 65 78 74 2c 20 7a 2c 20 2d 31 2c 20 53 51  ntext, z, -1, SQ
25dd9 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a  LITE_STATIC);.}.
25dda 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
25ddb 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 65  tation of the le
25ddc 6e 67 74 68 28 29 20 66 75 6e 63 74 69 6f 6e 0a  ngth() function.
25ddd 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c  */.static void l
25dde 65 6e 67 74 68 46 75 6e 63 28 0a 20 20 73 71 6c  engthFunc(.  sql
25ddf 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
25de0 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
25de1 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
25de2 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69  ue **argv.){.  i
25de3 6e 74 20 6c 65 6e 3b 0a 0a 20 20 61 73 73 65 72  nt len;..  asser
25de4 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20  t( argc==1 );.  
25de5 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
25de6 28 61 72 67 63 29 3b 0a 20 20 73 77 69 74 63 68  (argc);.  switch
25de7 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
25de8 74 79 70 65 28 61 72 67 76 5b 30 5d 29 20 29 7b  type(argv[0]) ){
25de9 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
25dea 5f 42 4c 4f 42 3a 0a 20 20 20 20 63 61 73 65 20  _BLOB:.    case 
25deb 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a 0a  SQLITE_INTEGER:.
25dec 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
25ded 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20 73  FLOAT: {.      s
25dee 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
25def 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74  t(context, sqlit
25df0 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
25df1 72 67 76 5b 30 5d 29 29 3b 0a 20 20 20 20 20 20  rgv[0]));.      
25df2 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
25df3 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 58   case SQLITE_TEX
25df4 54 3a 20 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74  T: {.      const
25df5 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
25df6 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  z = sqlite3_valu
25df7 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
25df8 0a 20 20 20 20 20 20 69 66 28 20 7a 3d 3d 30 20  .      if( z==0 
25df9 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20  ) return;.      
25dfa 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 77  len = 0;.      w
25dfb 68 69 6c 65 28 20 2a 7a 20 29 7b 0a 20 20 20 20  hile( *z ){.    
25dfc 20 20 20 20 6c 65 6e 2b 2b 3b 0a 20 20 20 20 20      len++;.     
25dfd 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55     SQLITE_SKIP_U
25dfe 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20 7d 0a  TF8(z);.      }.
25dff 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
25e00 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74  sult_int(context
25e01 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 20 20 62 72  , len);.      br
25e02 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
25e03 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
25e04 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
25e05 75 6c 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  ull(context);.  
25e06 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
25e07 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  .  }.}../*.** Im
25e08 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
25e09 74 68 65 20 61 62 73 28 29 20 66 75 6e 63 74 69  the abs() functi
25e0a 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  on.*/.static voi
25e0b 64 20 61 62 73 46 75 6e 63 28 73 71 6c 69 74 65  d absFunc(sqlite
25e0c 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
25e0d 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
25e0e 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
25e0f 67 76 29 7b 0a 20 20 61 73 73 65 72 74 28 20 61  gv){.  assert( a
25e10 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53  rgc==1 );.  UNUS
25e11 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67  ED_PARAMETER(arg
25e12 63 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73 71  c);.  switch( sq
25e13 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
25e14 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20  (argv[0]) ){.   
25e15 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e 54   case SQLITE_INT
25e16 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 69 36  EGER: {.      i6
25e17 34 20 69 56 61 6c 20 3d 20 73 71 6c 69 74 65 33  4 iVal = sqlite3
25e18 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72 67  _value_int64(arg
25e19 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 66 28  v[0]);.      if(
25e1a 20 69 56 61 6c 3c 30 20 29 7b 0a 20 20 20 20 20   iVal<0 ){.     
25e1b 20 20 20 69 66 28 20 28 69 56 61 6c 3c 3c 31 29     if( (iVal<<1)
25e1c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
25e1d 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
25e1e 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22  error(context, "
25e1f 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f 77  integer overflow
25e20 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20  ", -1);.        
25e21 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20    return;.      
25e22 20 20 7d 0a 20 20 20 20 20 20 20 20 69 56 61 6c    }.        iVal
25e23 20 3d 20 2d 69 56 61 6c 3b 0a 20 20 20 20 20 20   = -iVal;.      
25e24 7d 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  } .      sqlite3
25e25 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f  _result_int64(co
25e26 6e 74 65 78 74 2c 20 69 56 61 6c 29 3b 0a 20 20  ntext, iVal);.  
25e27 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
25e28 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
25e29 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 73  _NULL: {.      s
25e2a 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
25e2b 6c 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  ll(context);.   
25e2c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
25e2d 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
25e2e 20 20 20 20 20 64 6f 75 62 6c 65 20 72 56 61 6c       double rVal
25e2f 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
25e30 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d 29  _double(argv[0])
25e31 3b 0a 20 20 20 20 20 20 69 66 28 20 72 56 61 6c  ;.      if( rVal
25e32 3c 30 20 29 20 72 56 61 6c 20 3d 20 2d 72 56 61  <0 ) rVal = -rVa
25e33 6c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  l;.      sqlite3
25e34 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63  _result_double(c
25e35 6f 6e 74 65 78 74 2c 20 72 56 61 6c 29 3b 0a 20  ontext, rVal);. 
25e36 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
25e37 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  }.  }.}../*.** I
25e38 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
25e39 20 74 68 65 20 73 75 62 73 74 72 28 29 20 66 75   the substr() fu
25e3a 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 73 75  nction..**.** su
25e3b 62 73 74 72 28 78 2c 70 31 2c 70 32 29 20 20 72  bstr(x,p1,p2)  r
25e3c 65 74 75 72 6e 73 20 70 32 20 63 68 61 72 61 63  eturns p2 charac
25e3d 74 65 72 73 20 6f 66 20 78 5b 5d 20 62 65 67 69  ters of x[] begi
25e3e 6e 6e 69 6e 67 20 77 69 74 68 20 70 31 2e 0a 2a  nning with p1..*
25e3f 2a 20 70 31 20 69 73 20 31 2d 69 6e 64 65 78 65  * p1 is 1-indexe
25e40 64 2e 20 20 53 6f 20 73 75 62 73 74 72 28 78 2c  d.  So substr(x,
25e41 31 2c 31 29 20 72 65 74 75 72 6e 73 20 74 68 65  1,1) returns the
25e42 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65 72   first character
25e43 0a 2a 2a 20 6f 66 20 78 2e 20 20 49 66 20 78 20  .** of x.  If x 
25e44 69 73 20 74 65 78 74 2c 20 74 68 65 6e 20 77 65  is text, then we
25e45 20 61 63 74 75 61 6c 6c 79 20 63 6f 75 6e 74 20   actually count 
25e46 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72 73  UTF-8 characters
25e47 2e 0a 2a 2a 20 49 66 20 78 20 69 73 20 61 20 62  ..** If x is a b
25e48 6c 6f 62 2c 20 74 68 65 6e 20 77 65 20 63 6f 75  lob, then we cou
25e49 6e 74 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20  nt bytes..**.** 
25e4a 49 66 20 70 31 20 69 73 20 6e 65 67 61 74 69 76  If p1 is negativ
25e4b 65 2c 20 74 68 65 6e 20 77 65 20 62 65 67 69 6e  e, then we begin
25e4c 20 61 62 73 28 70 31 29 20 66 72 6f 6d 20 74 68   abs(p1) from th
25e4d 65 20 65 6e 64 20 6f 66 20 78 5b 5d 2e 0a 2a 2f  e end of x[]..*/
25e4e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75 62  .static void sub
25e4f 73 74 72 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  strFunc(.  sqlit
25e50 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
25e51 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c  ext,.  int argc,
25e52 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
25e53 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e   **argv.){.  con
25e54 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
25e55 20 2a 7a 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73   *z;.  const uns
25e56 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 32 3b 0a  igned char *z2;.
25e57 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e 74    int len;.  int
25e58 20 70 30 74 79 70 65 3b 0a 20 20 69 36 34 20 70   p0type;.  i64 p
25e59 31 2c 20 70 32 3b 0a 20 20 69 6e 74 20 6e 65 67  1, p2;.  int neg
25e5a 50 32 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72  P2 = 0;..  asser
25e5b 74 28 20 61 72 67 63 3d 3d 33 20 7c 7c 20 61 72  t( argc==3 || ar
25e5c 67 63 3d 3d 32 20 29 3b 0a 20 20 69 66 28 20 73  gc==2 );.  if( s
25e5d 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
25e5e 65 28 61 72 67 76 5b 31 5d 29 3d 3d 53 51 4c 49  e(argv[1])==SQLI
25e5f 54 45 5f 4e 55 4c 4c 0a 20 20 20 7c 7c 20 28 61  TE_NULL.   || (a
25e60 72 67 63 3d 3d 33 20 26 26 20 73 71 6c 69 74 65  rgc==3 && sqlite
25e61 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
25e62 76 5b 32 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55  v[2])==SQLITE_NU
25e63 4c 4c 29 0a 20 20 29 7b 0a 20 20 20 20 72 65 74  LL).  ){.    ret
25e64 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 30 74 79 70  urn;.  }.  p0typ
25e65 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  e = sqlite3_valu
25e66 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3b  e_type(argv[0]);
25e67 0a 20 20 69 66 28 20 70 30 74 79 70 65 3d 3d 53  .  if( p0type==S
25e68 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20  QLITE_BLOB ){.  
25e69 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f    len = sqlite3_
25e6a 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
25e6b 5b 30 5d 29 3b 0a 20 20 20 20 7a 20 3d 20 73 71  [0]);.    z = sq
25e6c 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
25e6d 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69  (argv[0]);.    i
25e6e 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( z==0 ) return
25e6f 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c 65  ;.    assert( le
25e70 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  n==sqlite3_value
25e71 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 20  _bytes(argv[0]) 
25e72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
25e73 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  z = sqlite3_valu
25e74 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
25e75 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20  .    if( z==0 ) 
25e76 72 65 74 75 72 6e 3b 0a 20 20 20 20 6c 65 6e 20  return;.    len 
25e77 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 7a 32 3d  = 0;.    for(z2=
25e78 7a 3b 20 2a 7a 32 3b 20 6c 65 6e 2b 2b 29 7b 0a  z; *z2; len++){.
25e79 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49        SQLITE_SKI
25e7a 50 5f 55 54 46 38 28 7a 32 29 3b 0a 20 20 20 20  P_UTF8(z2);.    
25e7b 7d 0a 20 20 7d 0a 20 20 70 31 20 3d 20 73 71 6c  }.  }.  p1 = sql
25e7c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61  ite3_value_int(a
25e7d 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20 61  rgv[1]);.  if( a
25e7e 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20 70 32  rgc==3 ){.    p2
25e7f 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
25e80 5f 69 6e 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20  _int(argv[2]);. 
25e81 20 20 20 69 66 28 20 70 32 3c 30 20 29 7b 0a 20     if( p2<0 ){. 
25e82 20 20 20 20 20 70 32 20 3d 20 2d 70 32 3b 0a 20       p2 = -p2;. 
25e83 20 20 20 20 20 6e 65 67 50 32 20 3d 20 31 3b 0a       negP2 = 1;.
25e84 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
25e85 20 20 20 70 32 20 3d 20 73 71 6c 69 74 65 33 5f     p2 = sqlite3_
25e86 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
25e87 65 28 63 6f 6e 74 65 78 74 29 2d 3e 61 4c 69 6d  e(context)->aLim
25e88 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
25e89 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a 20 20 69  LENGTH];.  }.  i
25e8a 66 28 20 70 31 3c 30 20 29 7b 0a 20 20 20 20 70  f( p1<0 ){.    p
25e8b 31 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 69 66  1 += len;.    if
25e8c 28 20 70 31 3c 30 20 29 7b 0a 20 20 20 20 20 20  ( p1<0 ){.      
25e8d 70 32 20 2b 3d 20 70 31 3b 0a 20 20 20 20 20 20  p2 += p1;.      
25e8e 69 66 28 20 70 32 3c 30 20 29 20 70 32 20 3d 20  if( p2<0 ) p2 = 
25e8f 30 3b 0a 20 20 20 20 20 20 70 31 20 3d 20 30 3b  0;.      p1 = 0;
25e90 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
25e91 66 28 20 70 31 3e 30 20 29 7b 0a 20 20 20 20 70  f( p1>0 ){.    p
25e92 31 2d 2d 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  1--;.  }else if(
25e93 20 70 32 3e 30 20 29 7b 0a 20 20 20 20 70 32 2d   p2>0 ){.    p2-
25e94 2d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 67  -;.  }.  if( neg
25e95 50 32 20 29 7b 0a 20 20 20 20 70 31 20 2d 3d 20  P2 ){.    p1 -= 
25e96 70 32 3b 0a 20 20 20 20 69 66 28 20 70 31 3c 30  p2;.    if( p1<0
25e97 20 29 7b 0a 20 20 20 20 20 20 70 32 20 2b 3d 20   ){.      p2 += 
25e98 70 31 3b 0a 20 20 20 20 20 20 70 31 20 3d 20 30  p1;.      p1 = 0
25e99 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73  ;.    }.  }.  as
25e9a 73 65 72 74 28 20 70 31 3e 3d 30 20 26 26 20 70  sert( p1>=0 && p
25e9b 32 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 31  2>=0 );.  if( p1
25e9c 2b 70 32 3e 6c 65 6e 20 29 7b 0a 20 20 20 20 70  +p2>len ){.    p
25e9d 32 20 3d 20 6c 65 6e 2d 70 31 3b 0a 20 20 20 20  2 = len-p1;.    
25e9e 69 66 28 20 70 32 3c 30 20 29 20 70 32 20 3d 20  if( p2<0 ) p2 = 
25e9f 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 30 74  0;.  }.  if( p0t
25ea0 79 70 65 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42  ype!=SQLITE_BLOB
25ea1 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 2a   ){.    while( *
25ea2 7a 20 26 26 20 70 31 20 29 7b 0a 20 20 20 20 20  z && p1 ){.     
25ea3 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46   SQLITE_SKIP_UTF
25ea4 38 28 7a 29 3b 0a 20 20 20 20 20 20 70 31 2d 2d  8(z);.      p1--
25ea5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  ;.    }.    for(
25ea6 7a 32 3d 7a 3b 20 2a 7a 32 20 26 26 20 70 32 3b  z2=z; *z2 && p2;
25ea7 20 70 32 2d 2d 29 7b 0a 20 20 20 20 20 20 53 51   p2--){.      SQ
25ea8 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a  LITE_SKIP_UTF8(z
25ea9 32 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  2);.    }.    sq
25eaa 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
25eab 74 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72  t(context, (char
25eac 2a 29 7a 2c 20 28 69 6e 74 29 28 7a 32 2d 7a 29  *)z, (int)(z2-z)
25ead 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
25eae 4e 54 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  NT);.  }else{.  
25eaf 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
25eb0 5f 62 6c 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28  _blob(context, (
25eb1 63 68 61 72 2a 29 26 7a 5b 70 31 5d 2c 20 28 69  char*)&z[p1], (i
25eb2 6e 74 29 70 32 2c 20 53 51 4c 49 54 45 5f 54 52  nt)p2, SQLITE_TR
25eb3 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 7d 0a  ANSIENT);.  }.}.
25eb4 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
25eb5 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 72 6f 75  ation of the rou
25eb6 6e 64 28 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2f  nd() function.*/
25eb7 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
25eb8 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
25eb9 49 4e 54 0a 73 74 61 74 69 63 20 76 6f 69 64 20  INT.static void 
25eba 72 6f 75 6e 64 46 75 6e 63 28 73 71 6c 69 74 65  roundFunc(sqlite
25ebb 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
25ebc 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
25ebd 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
25ebe 67 76 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 30  gv){.  int n = 0
25ebf 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a 20 20  ;.  double r;.  
25ec0 63 68 61 72 20 2a 7a 42 75 66 3b 0a 20 20 61 73  char *zBuf;.  as
25ec1 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c  sert( argc==1 ||
25ec2 20 61 72 67 63 3d 3d 32 20 29 3b 0a 20 20 69 66   argc==2 );.  if
25ec3 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a 20 20 20  ( argc==2 ){.   
25ec4 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c   if( SQLITE_NULL
25ec5 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ==sqlite3_value_
25ec6 74 79 70 65 28 61 72 67 76 5b 31 5d 29 20 29 20  type(argv[1]) ) 
25ec7 72 65 74 75 72 6e 3b 0a 20 20 20 20 6e 20 3d 20  return;.    n = 
25ec8 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
25ec9 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20  t(argv[1]);.    
25eca 69 66 28 20 6e 3e 33 30 20 29 20 6e 20 3d 20 33  if( n>30 ) n = 3
25ecb 30 3b 0a 20 20 20 20 69 66 28 20 6e 3c 30 20 29  0;.    if( n<0 )
25ecc 20 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66   n = 0;.  }.  if
25ecd 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
25ece 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53  type(argv[0])==S
25ecf 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74  QLITE_NULL ) ret
25ed0 75 72 6e 3b 0a 20 20 72 20 3d 20 73 71 6c 69 74  urn;.  r = sqlit
25ed1 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28  e3_value_double(
25ed2 61 72 67 76 5b 30 5d 29 3b 0a 20 20 7a 42 75 66  argv[0]);.  zBuf
25ed3 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
25ed4 74 66 28 22 25 2e 2a 66 22 2c 6e 2c 72 29 3b 0a  tf("%.*f",n,r);.
25ed5 20 20 69 66 28 20 7a 42 75 66 3d 3d 30 20 29 7b    if( zBuf==0 ){
25ed6 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
25ed7 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28  ult_error_nomem(
25ed8 63 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 65 6c 73  context);.  }els
25ed9 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 74  e{.    sqlite3At
25eda 6f 46 28 7a 42 75 66 2c 20 26 72 29 3b 0a 20 20  oF(zBuf, &r);.  
25edb 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
25edc 42 75 66 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Buf);.    sqlite
25edd 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28  3_result_double(
25ede 63 6f 6e 74 65 78 74 2c 20 72 29 3b 0a 20 20 7d  context, r);.  }
25edf 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
25ee0 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 20   Allocate nByte 
25ee1 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20 75  bytes of space u
25ee2 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 6d 61 6c  sing sqlite3_mal
25ee3 6c 6f 63 28 29 2e 20 49 66 20 74 68 65 0a 2a 2a  loc(). If the.**
25ee4 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c   allocation fail
25ee5 73 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f  s, call sqlite3_
25ee6 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
25ee7 65 6d 28 29 20 74 6f 20 6e 6f 74 69 66 79 0a 2a  em() to notify.*
25ee8 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  * the database h
25ee9 61 6e 64 6c 65 20 74 68 61 74 20 6d 61 6c 6c 6f  andle that mallo
25eea 63 28 29 20 68 61 73 20 66 61 69 6c 65 64 20 61  c() has failed a
25eeb 6e 64 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a  nd return NULL..
25eec 2a 2a 20 49 66 20 6e 42 79 74 65 20 69 73 20 6c  ** If nByte is l
25eed 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 6d  arger than the m
25eee 61 78 69 6d 75 6d 20 73 74 72 69 6e 67 20 6f 72  aximum string or
25eef 20 62 6c 6f 62 20 6c 65 6e 67 74 68 2c 20 74 68   blob length, th
25ef0 65 6e 0a 2a 2a 20 72 61 69 73 65 20 61 6e 20 53  en.** raise an S
25ef1 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 65 78 63  QLITE_TOOBIG exc
25ef2 65 70 74 69 6f 6e 20 61 6e 64 20 72 65 74 75 72  eption and retur
25ef3 6e 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69  n NULL..*/.stati
25ef4 63 20 76 6f 69 64 20 2a 63 6f 6e 74 65 78 74 4d  c void *contextM
25ef5 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 5f 63 6f  alloc(sqlite3_co
25ef6 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20  ntext *context, 
25ef7 69 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 63 68  i64 nByte){.  ch
25ef8 61 72 20 2a 7a 3b 0a 20 20 73 71 6c 69 74 65 33  ar *z;.  sqlite3
25ef9 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63   *db = sqlite3_c
25efa 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
25efb 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 61 73 73  (context);.  ass
25efc 65 72 74 28 20 6e 42 79 74 65 3e 30 20 29 3b 0a  ert( nByte>0 );.
25efd 20 20 74 65 73 74 63 61 73 65 28 20 6e 42 79 74    testcase( nByt
25efe 65 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  e==db->aLimit[SQ
25eff 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
25f00 48 5d 20 29 3b 0a 20 20 74 65 73 74 63 61 73 65  H] );.  testcase
25f01 28 20 6e 42 79 74 65 3d 3d 64 62 2d 3e 61 4c 69  ( nByte==db->aLi
25f02 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
25f03 5f 4c 45 4e 47 54 48 5d 2b 31 20 29 3b 0a 20 20  _LENGTH]+1 );.  
25f04 69 66 28 20 6e 42 79 74 65 3e 64 62 2d 3e 61 4c  if( nByte>db->aL
25f05 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
25f06 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20  T_LENGTH] ){.   
25f07 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
25f08 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e  error_toobig(con
25f09 74 65 78 74 29 3b 0a 20 20 20 20 7a 20 3d 20 30  text);.    z = 0
25f0a 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a  ;.  }else{.    z
25f0b 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
25f0c 28 28 69 6e 74 29 6e 42 79 74 65 29 3b 0a 20 20  ((int)nByte);.  
25f0d 20 20 69 66 28 20 21 7a 20 29 7b 0a 20 20 20 20    if( !z ){.    
25f0e 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
25f0f 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e  _error_nomem(con
25f10 74 65 78 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  text);.    }.  }
25f11 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a  .  return z;.}..
25f12 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
25f13 74 69 6f 6e 20 6f 66 20 74 68 65 20 75 70 70 65  tion of the uppe
25f14 72 28 29 20 61 6e 64 20 6c 6f 77 65 72 28 29 20  r() and lower() 
25f15 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a  SQL functions..*
25f16 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 70  /.static void up
25f17 70 65 72 46 75 6e 63 28 73 71 6c 69 74 65 33 5f  perFunc(sqlite3_
25f18 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
25f19 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
25f1a 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
25f1b 29 7b 0a 20 20 63 68 61 72 20 2a 7a 31 3b 0a 20  ){.  char *z1;. 
25f1c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 3b   const char *z2;
25f1d 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 55  .  int i, n;.  U
25f1e 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
25f1f 61 72 67 63 29 3b 0a 20 20 7a 32 20 3d 20 28 63  argc);.  z2 = (c
25f20 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  har*)sqlite3_val
25f21 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
25f22 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f  ;.  n = sqlite3_
25f23 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
25f24 5b 30 5d 29 3b 0a 20 20 2f 2a 20 56 65 72 69 66  [0]);.  /* Verif
25f25 79 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 20  y that the call 
25f26 74 6f 20 5f 62 79 74 65 73 28 29 20 64 6f 65 73  to _bytes() does
25f27 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 20   not invalidate 
25f28 74 68 65 20 5f 74 65 78 74 28 29 20 70 6f 69 6e  the _text() poin
25f29 74 65 72 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ter */.  assert(
25f2a 20 7a 32 3d 3d 28 63 68 61 72 2a 29 73 71 6c 69   z2==(char*)sqli
25f2b 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
25f2c 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28  rgv[0]) );.  if(
25f2d 20 7a 32 20 29 7b 0a 20 20 20 20 7a 31 20 3d 20   z2 ){.    z1 = 
25f2e 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f  contextMalloc(co
25f2f 6e 74 65 78 74 2c 20 28 28 69 36 34 29 6e 29 2b  ntext, ((i64)n)+
25f30 31 29 3b 0a 20 20 20 20 69 66 28 20 7a 31 20 29  1);.    if( z1 )
25f31 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a  {.      memcpy(z
25f32 31 2c 20 7a 32 2c 20 6e 2b 31 29 3b 0a 20 20 20  1, z2, n+1);.   
25f33 20 20 20 66 6f 72 28 69 3d 30 3b 20 7a 31 5b 69     for(i=0; z1[i
25f34 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  ]; i++){.       
25f35 20 7a 31 5b 69 5d 20 3d 20 28 63 68 61 72 29 73   z1[i] = (char)s
25f36 71 6c 69 74 65 33 54 6f 75 70 70 65 72 28 7a 31  qlite3Toupper(z1
25f37 5b 69 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  [i]);.      }.  
25f38 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
25f39 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
25f3a 20 7a 31 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33   z1, -1, sqlite3
25f3b 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20 20  _free);.    }.  
25f3c 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  }.}.static void 
25f3d 6c 6f 77 65 72 46 75 6e 63 28 73 71 6c 69 74 65  lowerFunc(sqlite
25f3e 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
25f3f 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
25f40 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
25f41 67 76 29 7b 0a 20 20 75 38 20 2a 7a 31 3b 0a 20  gv){.  u8 *z1;. 
25f42 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 3b   const char *z2;
25f43 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 55  .  int i, n;.  U
25f44 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
25f45 61 72 67 63 29 3b 0a 20 20 7a 32 20 3d 20 28 63  argc);.  z2 = (c
25f46 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  har*)sqlite3_val
25f47 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
25f48 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f  ;.  n = sqlite3_
25f49 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
25f4a 5b 30 5d 29 3b 0a 20 20 2f 2a 20 56 65 72 69 66  [0]);.  /* Verif
25f4b 79 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c 20  y that the call 
25f4c 74 6f 20 5f 62 79 74 65 73 28 29 20 64 6f 65 73  to _bytes() does
25f4d 20 6e 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 20   not invalidate 
25f4e 74 68 65 20 5f 74 65 78 74 28 29 20 70 6f 69 6e  the _text() poin
25f4f 74 65 72 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ter */.  assert(
25f50 20 7a 32 3d 3d 28 63 68 61 72 2a 29 73 71 6c 69   z2==(char*)sqli
25f51 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
25f52 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28  rgv[0]) );.  if(
25f53 20 7a 32 20 29 7b 0a 20 20 20 20 7a 31 20 3d 20   z2 ){.    z1 = 
25f54 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f  contextMalloc(co
25f55 6e 74 65 78 74 2c 20 28 28 69 36 34 29 6e 29 2b  ntext, ((i64)n)+
25f56 31 29 3b 0a 20 20 20 20 69 66 28 20 7a 31 20 29  1);.    if( z1 )
25f57 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a  {.      memcpy(z
25f58 31 2c 20 7a 32 2c 20 6e 2b 31 29 3b 0a 20 20 20  1, z2, n+1);.   
25f59 20 20 20 66 6f 72 28 69 3d 30 3b 20 7a 31 5b 69     for(i=0; z1[i
25f5a 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  ]; i++){.       
25f5b 20 7a 31 5b 69 5d 20 3d 20 73 71 6c 69 74 65 33   z1[i] = sqlite3
25f5c 54 6f 6c 6f 77 65 72 28 7a 31 5b 69 5d 29 3b 0a  Tolower(z1[i]);.
25f5d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
25f5e 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
25f5f 74 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72  t(context, (char
25f60 20 2a 29 7a 31 2c 20 2d 31 2c 20 73 71 6c 69 74   *)z1, -1, sqlit
25f61 65 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d 0a  e3_free);.    }.
25f62 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70    }.}../*.** Imp
25f63 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
25f64 68 65 20 49 46 4e 55 4c 4c 28 29 2c 20 4e 56 4c  he IFNULL(), NVL
25f65 28 29 2c 20 61 6e 64 20 43 4f 41 4c 45 53 43 45  (), and COALESCE
25f66 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 0a  () functions.  .
25f67 2a 2a 20 41 6c 6c 20 74 68 72 65 65 20 64 6f 20  ** All three do 
25f68 74 68 65 20 73 61 6d 65 20 74 68 69 6e 67 2e 20  the same thing. 
25f69 20 54 68 65 79 20 72 65 74 75 72 6e 20 74 68 65   They return the
25f6a 20 66 69 72 73 74 20 6e 6f 6e 2d 4e 55 4c 4c 0a   first non-NULL.
25f6b 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a  ** argument..*/.
25f6c 73 74 61 74 69 63 20 76 6f 69 64 20 69 66 6e 75  static void ifnu
25f6d 6c 6c 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  llFunc(.  sqlite
25f6e 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
25f6f 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
25f70 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
25f71 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20  **argv.){.  int 
25f72 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  i;.  for(i=0; i<
25f73 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  argc; i++){.    
25f74 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 21  if( SQLITE_NULL!
25f75 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  =sqlite3_value_t
25f76 79 70 65 28 61 72 67 76 5b 69 5d 29 20 29 7b 0a  ype(argv[i]) ){.
25f77 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
25f78 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65  sult_value(conte
25f79 78 74 2c 20 61 72 67 76 5b 69 5d 29 3b 0a 20 20  xt, argv[i]);.  
25f7a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
25f7b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  .  }.}../*.** Im
25f7c 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
25f7d 72 61 6e 64 6f 6d 28 29 2e 20 20 52 65 74 75 72  random().  Retur
25f7e 6e 20 61 20 72 61 6e 64 6f 6d 20 69 6e 74 65 67  n a random integ
25f7f 65 72 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20  er.  .*/.static 
25f80 76 6f 69 64 20 72 61 6e 64 6f 6d 46 75 6e 63 28  void randomFunc(
25f81 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
25f82 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
25f83 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71  nt NotUsed,.  sq
25f84 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f  lite3_value **No
25f85 74 55 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c 69  tUsed2.){.  sqli
25f86 74 65 5f 69 6e 74 36 34 20 72 3b 0a 20 20 55 4e  te_int64 r;.  UN
25f87 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28  USED_PARAMETER2(
25f88 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64  NotUsed, NotUsed
25f89 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 61  2);.  sqlite3_ra
25f8a 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28  ndomness(sizeof(
25f8b 72 29 2c 20 26 72 29 3b 0a 20 20 69 66 28 20 72  r), &r);.  if( r
25f8c 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20  <0 ){.    /* We 
25f8d 6e 65 65 64 20 74 6f 20 70 72 65 76 65 6e 74 20  need to prevent 
25f8e 61 20 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20  a random number 
25f8f 6f 66 20 30 78 38 30 30 30 30 30 30 30 30 30 30  of 0x80000000000
25f90 30 30 30 30 30 20 0a 20 20 20 20 2a 2a 20 28 6f  00000 .    ** (o
25f91 72 20 2d 39 32 32 33 33 37 32 30 33 36 38 35 34  r -9223372036854
25f92 37 37 35 38 30 38 29 20 73 69 6e 63 65 20 77 68  775808) since wh
25f93 65 6e 20 79 6f 75 20 64 6f 20 61 62 73 28 29 20  en you do abs() 
25f94 6f 66 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 6e  of that.    ** n
25f95 75 6d 62 65 72 20 6f 66 20 79 6f 75 20 67 65 74  umber of you get
25f96 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20   the same value 
25f97 62 61 63 6b 20 61 67 61 69 6e 2e 20 20 54 6f 20  back again.  To 
25f98 64 6f 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69  do this.    ** i
25f99 6e 20 61 20 77 61 79 20 74 68 61 74 20 69 73 20  n a way that is 
25f9a 74 65 73 74 61 62 6c 65 2c 20 6d 61 73 6b 20 74  testable, mask t
25f9b 68 65 20 73 69 67 6e 20 62 69 74 20 6f 66 66 20  he sign bit off 
25f9c 6f 66 20 6e 65 67 61 74 69 76 65 0a 20 20 20 20  of negative.    
25f9d 2a 2a 20 76 61 6c 75 65 73 2c 20 72 65 73 75 6c  ** values, resul
25f9e 74 69 6e 67 20 69 6e 20 61 20 70 6f 73 69 74 69  ting in a positi
25f9f 76 65 20 76 61 6c 75 65 2e 20 20 54 68 65 6e 20  ve value.  Then 
25fa0 74 61 6b 65 20 74 68 65 20 0a 20 20 20 20 2a 2a  take the .    **
25fa1 20 32 73 20 63 6f 6d 70 6c 65 6d 65 6e 74 20 6f   2s complement o
25fa2 66 20 74 68 61 74 20 70 6f 73 69 74 69 76 65 20  f that positive 
25fa3 76 61 6c 75 65 2e 20 20 54 68 65 20 65 6e 64 20  value.  The end 
25fa4 72 65 73 75 6c 74 20 63 61 6e 0a 20 20 20 20 2a  result can.    *
25fa5 2a 20 74 68 65 72 65 66 6f 72 65 20 62 65 20 6e  * therefore be n
25fa6 6f 20 6c 65 73 73 20 74 68 61 6e 20 2d 39 32 32  o less than -922
25fa7 33 33 37 32 30 33 36 38 35 34 37 37 35 38 30 37  3372036854775807
25fa8 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 20 3d  ..    */.    r =
25fa9 20 2d 28 72 20 5e 20 28 28 28 73 71 6c 69 74 65   -(r ^ (((sqlite
25faa 33 5f 69 6e 74 36 34 29 31 29 3c 3c 36 33 29 29  3_int64)1)<<63))
25fab 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
25fac 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e  result_int64(con
25fad 74 65 78 74 2c 20 72 29 3b 0a 7d 0a 0a 2f 2a 0a  text, r);.}../*.
25fae 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
25faf 6e 20 6f 66 20 72 61 6e 64 6f 6d 62 6c 6f 62 28  n of randomblob(
25fb0 4e 29 2e 20 20 52 65 74 75 72 6e 20 61 20 72 61  N).  Return a ra
25fb1 6e 64 6f 6d 20 62 6c 6f 62 0a 2a 2a 20 74 68 61  ndom blob.** tha
25fb2 74 20 69 73 20 4e 20 62 79 74 65 73 20 6c 6f 6e  t is N bytes lon
25fb3 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  g..*/.static voi
25fb4 64 20 72 61 6e 64 6f 6d 42 6c 6f 62 28 0a 20 20  d randomBlob(.  
25fb5 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
25fb6 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
25fb7 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
25fb8 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
25fb9 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73 69 67    int n;.  unsig
25fba 6e 65 64 20 63 68 61 72 20 2a 70 3b 0a 20 20 61  ned char *p;.  a
25fbb 73 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29  ssert( argc==1 )
25fbc 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
25fbd 45 54 45 52 28 61 72 67 63 29 3b 0a 20 20 6e 20  ETER(argc);.  n 
25fbe 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
25fbf 69 6e 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  int(argv[0]);.  
25fc0 69 66 28 20 6e 3c 31 20 29 7b 0a 20 20 20 20 6e  if( n<1 ){.    n
25fc1 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 70 20 3d 20   = 1;.  }.  p = 
25fc2 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f  contextMalloc(co
25fc3 6e 74 65 78 74 2c 20 6e 29 3b 0a 20 20 69 66 28  ntext, n);.  if(
25fc4 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65   p ){.    sqlite
25fc5 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 6e 2c 20  3_randomness(n, 
25fc6 70 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  p);.    sqlite3_
25fc7 72 65 73 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74  result_blob(cont
25fc8 65 78 74 2c 20 28 63 68 61 72 2a 29 70 2c 20 6e  ext, (char*)p, n
25fc9 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b  , sqlite3_free);
25fca 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  .  }.}../*.** Im
25fcb 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
25fcc 74 68 65 20 6c 61 73 74 5f 69 6e 73 65 72 74 5f  the last_insert_
25fcd 72 6f 77 69 64 28 29 20 53 51 4c 20 66 75 6e 63  rowid() SQL func
25fce 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 74 75 72  tion.  The retur
25fcf 6e 0a 2a 2a 20 76 61 6c 75 65 20 69 73 20 74 68  n.** value is th
25fd0 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 73 71  e same as the sq
25fd1 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
25fd2 74 5f 72 6f 77 69 64 28 29 20 41 50 49 20 66 75  t_rowid() API fu
25fd3 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69  nction..*/.stati
25fd4 63 20 76 6f 69 64 20 6c 61 73 74 5f 69 6e 73 65  c void last_inse
25fd5 72 74 5f 72 6f 77 69 64 28 0a 20 20 73 71 6c 69  rt_rowid(.  sqli
25fd6 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
25fd7 74 65 78 74 2c 20 0a 20 20 69 6e 74 20 4e 6f 74  text, .  int Not
25fd8 55 73 65 64 2c 20 0a 20 20 73 71 6c 69 74 65 33  Used, .  sqlite3
25fd9 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64  _value **NotUsed
25fda 32 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a  2.){.  sqlite3 *
25fdb 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  db = sqlite3_con
25fdc 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
25fdd 6f 6e 74 65 78 74 29 3b 0a 20 20 55 4e 55 53 45  ontext);.  UNUSE
25fde 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74  D_PARAMETER2(Not
25fdf 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b  Used, NotUsed2);
25fe0 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
25fe1 74 5f 69 6e 74 36 34 28 63 6f 6e 74 65 78 74 2c  t_int64(context,
25fe2 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e   sqlite3_last_in
25fe3 73 65 72 74 5f 72 6f 77 69 64 28 64 62 29 29 3b  sert_rowid(db));
25fe4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
25fe5 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
25fe6 63 68 61 6e 67 65 73 28 29 20 53 51 4c 20 66 75  changes() SQL fu
25fe7 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 74  nction.  The ret
25fe8 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65  urn value is the
25fe9 0a 2a 2a 20 73 61 6d 65 20 61 73 20 74 68 65 20  .** same as the 
25fea 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28  sqlite3_changes(
25feb 29 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a  ) API function..
25fec 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
25fed 68 61 6e 67 65 73 28 0a 20 20 73 71 6c 69 74 65  hanges(.  sqlite
25fee 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
25fef 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65  xt,.  int NotUse
25ff0 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  d,.  sqlite3_val
25ff1 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b  ue **NotUsed2.){
25ff2 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
25ff3 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
25ff4 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
25ff5 78 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  xt);.  UNUSED_PA
25ff6 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64  RAMETER2(NotUsed
25ff7 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 73  , NotUsed2);.  s
25ff8 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
25ff9 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74  t(context, sqlit
25ffa 65 33 5f 63 68 61 6e 67 65 73 28 64 62 29 29 3b  e3_changes(db));
25ffb 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
25ffc 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
25ffd 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 20  total_changes() 
25ffe 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54  SQL function.  T
25fff 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
26000 69 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61  is.** the same a
26001 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 74 6f  s the sqlite3_to
26002 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 20 41 50  tal_changes() AP
26003 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  I function..*/.s
26004 74 61 74 69 63 20 76 6f 69 64 20 74 6f 74 61 6c  tatic void total
26005 5f 63 68 61 6e 67 65 73 28 0a 20 20 73 71 6c 69  _changes(.  sqli
26006 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
26007 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55  text,.  int NotU
26008 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  sed,.  sqlite3_v
26009 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a  alue **NotUsed2.
2600a 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
2600b 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
2600c 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
2600d 74 65 78 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f  text);.  UNUSED_
2600e 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55 73  PARAMETER2(NotUs
2600f 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20  ed, NotUsed2);. 
26010 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
26011 69 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c  int(context, sql
26012 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67  ite3_total_chang
26013 65 73 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  es(db));.}../*.*
26014 2a 20 41 20 73 74 72 75 63 74 75 72 65 20 64 65  * A structure de
26015 66 69 6e 69 6e 67 20 68 6f 77 20 74 6f 20 64 6f  fining how to do
26016 20 47 4c 4f 42 2d 73 74 79 6c 65 20 63 6f 6d 70   GLOB-style comp
26017 61 72 69 73 6f 6e 73 2e 0a 2a 2f 0a 73 74 72 75  arisons..*/.stru
26018 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 7b  ct compareInfo {
26019 0a 20 20 75 38 20 6d 61 74 63 68 41 6c 6c 3b 0a  .  u8 matchAll;.
2601a 20 20 75 38 20 6d 61 74 63 68 4f 6e 65 3b 0a 20    u8 matchOne;. 
2601b 20 75 38 20 6d 61 74 63 68 53 65 74 3b 0a 20 20   u8 matchSet;.  
2601c 75 38 20 6e 6f 43 61 73 65 3b 0a 7d 3b 0a 0a 2f  u8 noCase;.};../
2601d 2a 0a 2a 2a 20 46 6f 72 20 4c 49 4b 45 20 61 6e  *.** For LIKE an
2601e 64 20 47 4c 4f 42 20 6d 61 74 63 68 69 6e 67 20  d GLOB matching 
2601f 6f 6e 20 45 42 43 44 49 43 20 6d 61 63 68 69 6e  on EBCDIC machin
26020 65 73 2c 20 61 73 73 75 6d 65 20 74 68 61 74 20  es, assume that 
26021 65 76 65 72 79 0a 2a 2a 20 63 68 61 72 61 63 74  every.** charact
26022 65 72 20 69 73 20 65 78 61 63 74 6c 79 20 6f 6e  er is exactly on
26023 65 20 62 79 74 65 20 69 6e 20 73 69 7a 65 2e 20  e byte in size. 
26024 20 41 6c 73 6f 2c 20 61 6c 6c 20 63 68 61 72 61   Also, all chara
26025 63 74 65 72 73 20 61 72 65 0a 2a 2a 20 61 62 6c  cters are.** abl
26026 65 20 74 6f 20 70 61 72 74 69 63 69 70 61 74 65  e to participate
26027 20 69 6e 20 75 70 70 65 72 2d 63 61 73 65 2d 74   in upper-case-t
26028 6f 2d 6c 6f 77 65 72 2d 63 61 73 65 20 6d 61 70  o-lower-case map
26029 70 69 6e 67 73 20 69 6e 20 45 42 43 44 49 43 0a  pings in EBCDIC.
2602a 2a 2a 20 77 68 65 72 65 61 73 20 6f 6e 6c 79 20  ** whereas only 
2602b 63 68 61 72 61 63 74 65 72 73 20 6c 65 73 73 20  characters less 
2602c 74 68 61 6e 20 30 78 38 30 20 64 6f 20 69 6e 20  than 0x80 do in 
2602d 41 53 43 49 49 2e 0a 2a 2f 0a 23 69 66 20 64 65  ASCII..*/.#if de
2602e 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 42 43  fined(SQLITE_EBC
2602f 44 49 43 29 0a 23 20 64 65 66 69 6e 65 20 73 71  DIC).# define sq
26030 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 41 2c  lite3Utf8Read(A,
26031 43 29 20 20 20 20 28 2a 28 41 2b 2b 29 29 0a 23  C)    (*(A++)).#
26032 20 64 65 66 69 6e 65 20 47 6c 6f 67 55 70 70 65   define GlogUppe
26033 72 54 6f 4c 6f 77 65 72 28 41 29 20 20 20 20 20  rToLower(A)     
26034 41 20 3d 20 73 71 6c 69 74 65 33 55 70 70 65 72  A = sqlite3Upper
26035 54 6f 4c 6f 77 65 72 5b 41 5d 0a 23 65 6c 73 65  ToLower[A].#else
26036 0a 23 20 64 65 66 69 6e 65 20 47 6c 6f 67 55 70  .# define GlogUp
26037 70 65 72 54 6f 4c 6f 77 65 72 28 41 29 20 20 20  perToLower(A)   
26038 20 20 69 66 28 20 41 3c 30 78 38 30 20 29 7b 20    if( A<0x80 ){ 
26039 41 20 3d 20 73 71 6c 69 74 65 33 55 70 70 65 72  A = sqlite3Upper
2603a 54 6f 4c 6f 77 65 72 5b 41 5d 3b 20 7d 0a 23 65  ToLower[A]; }.#e
2603b 6e 64 69 66 0a 0a 73 74 61 74 69 63 20 63 6f 6e  ndif..static con
2603c 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72  st struct compar
2603d 65 49 6e 66 6f 20 67 6c 6f 62 49 6e 66 6f 20 3d  eInfo globInfo =
2603e 20 7b 20 27 2a 27 2c 20 27 3f 27 2c 20 27 5b 27   { '*', '?', '['
2603f 2c 20 30 20 7d 3b 0a 2f 2a 20 54 68 65 20 63 6f  , 0 };./* The co
26040 72 72 65 63 74 20 53 51 4c 2d 39 32 20 62 65 68  rrect SQL-92 beh
26041 61 76 69 6f 72 20 69 73 20 66 6f 72 20 74 68 65  avior is for the
26042 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 74   LIKE operator t
26043 6f 20 69 67 6e 6f 72 65 0a 2a 2a 20 63 61 73 65  o ignore.** case
26044 2e 20 20 54 68 75 73 20 20 27 61 27 20 4c 49 4b  .  Thus  'a' LIK
26045 45 20 27 41 27 20 77 6f 75 6c 64 20 62 65 20 74  E 'A' would be t
26046 72 75 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63  rue. */.static c
26047 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f 6d 70  onst struct comp
26048 61 72 65 49 6e 66 6f 20 6c 69 6b 65 49 6e 66 6f  areInfo likeInfo
26049 4e 6f 72 6d 20 3d 20 7b 20 27 25 27 2c 20 27 5f  Norm = { '%', '_
2604a 27 2c 20 20 20 30 2c 20 31 20 7d 3b 0a 2f 2a 20  ',   0, 1 };./* 
2604b 49 66 20 53 51 4c 49 54 45 5f 43 41 53 45 5f 53  If SQLITE_CASE_S
2604c 45 4e 53 49 54 49 56 45 5f 4c 49 4b 45 20 69 73  ENSITIVE_LIKE is
2604d 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74   defined, then t
2604e 68 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72  he LIKE operator
2604f 0a 2a 2a 20 69 73 20 63 61 73 65 20 73 65 6e 73  .** is case sens
26050 69 74 69 76 65 20 63 61 75 73 69 6e 67 20 27 61  itive causing 'a
26051 27 20 4c 49 4b 45 20 27 41 27 20 74 6f 20 62 65  ' LIKE 'A' to be
26052 20 66 61 6c 73 65 20 2a 2f 0a 73 74 61 74 69 63   false */.static
26053 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 63 6f   const struct co
26054 6d 70 61 72 65 49 6e 66 6f 20 6c 69 6b 65 49 6e  mpareInfo likeIn
26055 66 6f 41 6c 74 20 3d 20 7b 20 27 25 27 2c 20 27  foAlt = { '%', '
26056 5f 27 2c 20 20 20 30 2c 20 30 20 7d 3b 0a 0a 2f  _',   0, 0 };../
26057 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 77 6f  *.** Compare two
26058 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73 20 66   UTF-8 strings f
26059 6f 72 20 65 71 75 61 6c 69 74 79 20 77 68 65 72  or equality wher
2605a 65 20 74 68 65 20 66 69 72 73 74 20 73 74 72 69  e the first stri
2605b 6e 67 20 63 61 6e 0a 2a 2a 20 70 6f 74 65 6e 74  ng can.** potent
2605c 69 61 6c 6c 79 20 62 65 20 61 20 22 67 6c 6f 62  ially be a "glob
2605d 22 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 52  " expression.  R
2605e 65 74 75 72 6e 20 74 72 75 65 20 28 31 29 20 69  eturn true (1) i
2605f 66 20 74 68 65 79 0a 2a 2a 20 61 72 65 20 74 68  f they.** are th
26060 65 20 73 61 6d 65 20 61 6e 64 20 66 61 6c 73 65  e same and false
26061 20 28 30 29 20 69 66 20 74 68 65 79 20 61 72 65   (0) if they are
26062 20 64 69 66 66 65 72 65 6e 74 2e 0a 2a 2a 0a 2a   different..**.*
26063 2a 20 47 6c 6f 62 62 69 6e 67 20 72 75 6c 65 73  * Globbing rules
26064 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 27 2a 27  :.**.**      '*'
26065 20 20 20 20 20 20 20 4d 61 74 63 68 65 73 20 61         Matches a
26066 6e 79 20 73 65 71 75 65 6e 63 65 20 6f 66 20 7a  ny sequence of z
26067 65 72 6f 20 6f 72 20 6d 6f 72 65 20 63 68 61 72  ero or more char
26068 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 20  acters..**.**   
26069 20 20 20 27 3f 27 20 20 20 20 20 20 20 4d 61 74     '?'       Mat
2606a 63 68 65 73 20 65 78 61 63 74 6c 79 20 6f 6e 65  ches exactly one
2606b 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 0a 2a   character..**.*
2606c 2a 20 20 20 20 20 5b 2e 2e 2e 5d 20 20 20 20 20  *     [...]     
2606d 20 4d 61 74 63 68 65 73 20 6f 6e 65 20 63 68 61   Matches one cha
2606e 72 61 63 74 65 72 20 66 72 6f 6d 20 74 68 65 20  racter from the 
2606f 65 6e 63 6c 6f 73 65 64 20 6c 69 73 74 20 6f 66  enclosed list of
26070 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
26071 20 20 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a     characters..*
26072 2a 0a 2a 2a 20 20 20 20 20 5b 5e 2e 2e 2e 5d 20  *.**     [^...] 
26073 20 20 20 20 4d 61 74 63 68 65 73 20 6f 6e 65 20      Matches one 
26074 63 68 61 72 61 63 74 65 72 20 6e 6f 74 20 69 6e  character not in
26075 20 74 68 65 20 65 6e 63 6c 6f 73 65 64 20 6c 69   the enclosed li
26076 73 74 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74  st..**.** With t
26077 68 65 20 5b 2e 2e 2e 5d 20 61 6e 64 20 5b 5e 2e  he [...] and [^.
26078 2e 2e 5d 20 6d 61 74 63 68 69 6e 67 2c 20 61 20  ..] matching, a 
26079 27 5d 27 20 63 68 61 72 61 63 74 65 72 20 63 61  ']' character ca
2607a 6e 20 62 65 20 69 6e 63 6c 75 64 65 64 0a 2a 2a  n be included.**
2607b 20 69 6e 20 74 68 65 20 6c 69 73 74 20 62 79 20   in the list by 
2607c 6d 61 6b 69 6e 67 20 69 74 20 74 68 65 20 66 69  making it the fi
2607d 72 73 74 20 63 68 61 72 61 63 74 65 72 20 61 66  rst character af
2607e 74 65 72 20 27 5b 27 20 6f 72 20 27 5e 27 2e 20  ter '[' or '^'. 
2607f 20 41 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 20 63   A.** range of c
26080 68 61 72 61 63 74 65 72 73 20 63 61 6e 20 62 65  haracters can be
26081 20 73 70 65 63 69 66 69 65 64 20 75 73 69 6e 67   specified using
26082 20 27 2d 27 2e 20 20 45 78 61 6d 70 6c 65 3a 0a   '-'.  Example:.
26083 2a 2a 20 22 5b 61 2d 7a 5d 22 20 6d 61 74 63 68  ** "[a-z]" match
26084 65 73 20 61 6e 79 20 73 69 6e 67 6c 65 20 6c 6f  es any single lo
26085 77 65 72 2d 63 61 73 65 20 6c 65 74 74 65 72 2e  wer-case letter.
26086 20 20 54 6f 20 6d 61 74 63 68 20 61 20 27 2d 27    To match a '-'
26087 2c 20 6d 61 6b 65 0a 2a 2a 20 69 74 20 74 68 65  , make.** it the
26088 20 6c 61 73 74 20 63 68 61 72 61 63 74 65 72 20   last character 
26089 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2a 0a  in the list..**.
2608a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2608b 69 73 20 75 73 75 61 6c 6c 79 20 71 75 69 63 6b  is usually quick
2608c 2c 20 62 75 74 20 63 61 6e 20 62 65 20 4e 2a 2a  , but can be N**
2608d 32 20 69 6e 20 74 68 65 20 77 6f 72 73 74 20 63  2 in the worst c
2608e 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 48 69 6e 74 73  ase..**.** Hints
2608f 3a 20 74 6f 20 6d 61 74 63 68 20 27 2a 27 20 6f  : to match '*' o
26090 72 20 27 3f 27 2c 20 70 75 74 20 74 68 65 6d 20  r '?', put them 
26091 69 6e 20 22 5b 5d 22 2e 20 20 4c 69 6b 65 20 74  in "[]".  Like t
26092 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  his:.**.**      
26093 20 20 20 61 62 63 5b 2a 5d 78 79 7a 20 20 20 20     abc[*]xyz    
26094 20 20 20 20 4d 61 74 63 68 65 73 20 22 61 62 63      Matches "abc
26095 2a 78 79 7a 22 20 6f 6e 6c 79 0a 2a 2f 0a 73 74  *xyz" only.*/.st
26096 61 74 69 63 20 69 6e 74 20 70 61 74 74 65 72 6e  atic int pattern
26097 43 6f 6d 70 61 72 65 28 0a 20 20 63 6f 6e 73 74  Compare(.  const
26098 20 75 38 20 2a 7a 50 61 74 74 65 72 6e 2c 20 20   u8 *zPattern,  
26099 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2609a 68 65 20 67 6c 6f 62 20 70 61 74 74 65 72 6e 20  he glob pattern 
2609b 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a  */.  const u8 *z
2609c 53 74 72 69 6e 67 2c 20 20 20 20 20 20 20 20 20  String,         
2609d 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 74 72        /* The str
2609e 69 6e 67 20 74 6f 20 63 6f 6d 70 61 72 65 20 61  ing to compare a
2609f 67 61 69 6e 73 74 20 74 68 65 20 67 6c 6f 62 20  gainst the glob 
260a0 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 74 72 75 63  */.  const struc
260a1 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70  t compareInfo *p
260a2 49 6e 66 6f 2c 20 2f 2a 20 49 6e 66 6f 72 6d 61  Info, /* Informa
260a3 74 69 6f 6e 20 61 62 6f 75 74 20 68 6f 77 20 74  tion about how t
260a4 6f 20 64 6f 20 74 68 65 20 63 6f 6d 70 61 72 65  o do the compare
260a5 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20   */.  const int 
260a6 65 73 63 20 20 20 20 20 20 20 20 20 20 20 20 20  esc             
260a7 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 73         /* The es
260a8 63 61 70 65 20 63 68 61 72 61 63 74 65 72 20 2a  cape character *
260a9 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 2c 20 63 32  /.){.  int c, c2
260aa 3b 0a 20 20 69 6e 74 20 69 6e 76 65 72 74 3b 0a  ;.  int invert;.
260ab 20 20 69 6e 74 20 73 65 65 6e 3b 0a 20 20 75 38    int seen;.  u8
260ac 20 6d 61 74 63 68 4f 6e 65 20 3d 20 70 49 6e 66   matchOne = pInf
260ad 6f 2d 3e 6d 61 74 63 68 4f 6e 65 3b 0a 20 20 75  o->matchOne;.  u
260ae 38 20 6d 61 74 63 68 41 6c 6c 20 3d 20 70 49 6e  8 matchAll = pIn
260af 66 6f 2d 3e 6d 61 74 63 68 41 6c 6c 3b 0a 20 20  fo->matchAll;.  
260b0 75 38 20 6d 61 74 63 68 53 65 74 20 3d 20 70 49  u8 matchSet = pI
260b1 6e 66 6f 2d 3e 6d 61 74 63 68 53 65 74 3b 0a 20  nfo->matchSet;. 
260b2 20 75 38 20 6e 6f 43 61 73 65 20 3d 20 70 49 6e   u8 noCase = pIn
260b3 66 6f 2d 3e 6e 6f 43 61 73 65 3b 20 0a 20 20 69  fo->noCase; .  i
260b4 6e 74 20 70 72 65 76 45 73 63 61 70 65 20 3d 20  nt prevEscape = 
260b5 30 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  0;     /* True i
260b6 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63  f the previous c
260b7 68 61 72 61 63 74 65 72 20 77 61 73 20 27 65 73  haracter was 'es
260b8 63 61 70 65 27 20 2a 2f 0a 0a 20 20 77 68 69 6c  cape' */..  whil
260b9 65 28 20 28 63 20 3d 20 73 71 6c 69 74 65 33 55  e( (c = sqlite3U
260ba 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e  tf8Read(zPattern
260bb 2c 26 7a 50 61 74 74 65 72 6e 29 29 21 3d 30 20  ,&zPattern))!=0 
260bc 29 7b 0a 20 20 20 20 69 66 28 20 21 70 72 65 76  ){.    if( !prev
260bd 45 73 63 61 70 65 20 26 26 20 63 3d 3d 6d 61 74  Escape && c==mat
260be 63 68 41 6c 6c 20 29 7b 0a 20 20 20 20 20 20 77  chAll ){.      w
260bf 68 69 6c 65 28 20 28 63 3d 73 71 6c 69 74 65 33  hile( (c=sqlite3
260c0 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65 72  Utf8Read(zPatter
260c1 6e 2c 26 7a 50 61 74 74 65 72 6e 29 29 20 3d 3d  n,&zPattern)) ==
260c2 20 6d 61 74 63 68 41 6c 6c 0a 20 20 20 20 20 20   matchAll.      
260c3 20 20 20 20 20 20 20 20 20 7c 7c 20 63 20 3d 3d           || c ==
260c4 20 6d 61 74 63 68 4f 6e 65 20 29 7b 0a 20 20 20   matchOne ){.   
260c5 20 20 20 20 20 69 66 28 20 63 3d 3d 6d 61 74 63       if( c==matc
260c6 68 4f 6e 65 20 26 26 20 73 71 6c 69 74 65 33 55  hOne && sqlite3U
260c7 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c  tf8Read(zString,
260c8 20 26 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29 7b   &zString)==0 ){
260c9 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
260ca 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  n 0;.        }. 
260cb 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
260cc 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20   c==0 ){.       
260cd 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
260ce 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 65 73   }else if( c==es
260cf 63 20 29 7b 0a 20 20 20 20 20 20 20 20 63 20 3d  c ){.        c =
260d0 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
260d1 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a 50 61 74  (zPattern, &zPat
260d2 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 20 20 69  tern);.        i
260d3 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20  f( c==0 ){.     
260d4 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
260d5 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
260d6 65 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74 63  else if( c==matc
260d7 68 53 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20  hSet ){.        
260d8 61 73 73 65 72 74 28 20 65 73 63 3d 3d 30 20 29  assert( esc==0 )
260d9 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69  ;         /* Thi
260da 73 20 69 73 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c  s is GLOB, not L
260db 49 4b 45 20 2a 2f 0a 20 20 20 20 20 20 20 20 61  IKE */.        a
260dc 73 73 65 72 74 28 20 6d 61 74 63 68 53 65 74 3c  ssert( matchSet<
260dd 30 78 38 30 20 29 3b 20 20 2f 2a 20 27 5b 27 20  0x80 );  /* '[' 
260de 69 73 20 61 20 73 69 6e 67 6c 65 2d 62 79 74 65  is a single-byte
260df 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20   character */.  
260e0 20 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 53        while( *zS
260e1 74 72 69 6e 67 20 26 26 20 70 61 74 74 65 72 6e  tring && pattern
260e2 43 6f 6d 70 61 72 65 28 26 7a 50 61 74 74 65 72  Compare(&zPatter
260e3 6e 5b 2d 31 5d 2c 7a 53 74 72 69 6e 67 2c 70 49  n[-1],zString,pI
260e4 6e 66 6f 2c 65 73 63 29 3d 3d 30 20 29 7b 0a 20  nfo,esc)==0 ){. 
260e5 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
260e6 53 4b 49 50 5f 55 54 46 38 28 7a 53 74 72 69 6e  SKIP_UTF8(zStrin
260e7 67 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  g);.        }.  
260e8 20 20 20 20 20 20 72 65 74 75 72 6e 20 2a 7a 53        return *zS
260e9 74 72 69 6e 67 21 3d 30 3b 0a 20 20 20 20 20 20  tring!=0;.      
260ea 7d 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28  }.      while( (
260eb 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38  c2 = sqlite3Utf8
260ec 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 26 7a 53  Read(zString,&zS
260ed 74 72 69 6e 67 29 29 21 3d 30 20 29 7b 0a 20 20  tring))!=0 ){.  
260ee 20 20 20 20 20 20 69 66 28 20 6e 6f 43 61 73 65        if( noCase
260ef 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 47 6c   ){.          Gl
260f0 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 63  ogUpperToLower(c
260f1 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 47 6c  2);.          Gl
260f2 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28 63  ogUpperToLower(c
260f3 29 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69  );.          whi
260f4 6c 65 28 20 63 32 20 21 3d 20 30 20 26 26 20 63  le( c2 != 0 && c
260f5 32 20 21 3d 20 63 20 29 7b 0a 20 20 20 20 20 20  2 != c ){.      
260f6 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74        c2 = sqlit
260f7 65 33 55 74 66 38 52 65 61 64 28 7a 53 74 72 69  e3Utf8Read(zStri
260f8 6e 67 2c 20 26 7a 53 74 72 69 6e 67 29 3b 0a 20  ng, &zString);. 
260f9 20 20 20 20 20 20 20 20 20 20 20 47 6c 6f 67 55             GlogU
260fa 70 70 65 72 54 6f 4c 6f 77 65 72 28 63 32 29 3b  pperToLower(c2);
260fb 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
260fc 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
260fd 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 32 20        while( c2 
260fe 21 3d 20 30 20 26 26 20 63 32 20 21 3d 20 63 20  != 0 && c2 != c 
260ff 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63  ){.            c
26100 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  2 = sqlite3Utf8R
26101 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a 53  ead(zString, &zS
26102 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 20  tring);.        
26103 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
26104 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d 30 20        if( c2==0 
26105 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
26106 20 20 20 20 69 66 28 20 70 61 74 74 65 72 6e 43      if( patternC
26107 6f 6d 70 61 72 65 28 7a 50 61 74 74 65 72 6e 2c  ompare(zPattern,
26108 7a 53 74 72 69 6e 67 2c 70 49 6e 66 6f 2c 65 73  zString,pInfo,es
26109 63 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  c) ) return 1;. 
2610a 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74       }.      ret
2610b 75 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  urn 0;.    }else
2610c 20 69 66 28 20 21 70 72 65 76 45 73 63 61 70 65   if( !prevEscape
2610d 20 26 26 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20   && c==matchOne 
2610e 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ){.      if( sql
2610f 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 53 74  ite3Utf8Read(zSt
26110 72 69 6e 67 2c 20 26 7a 53 74 72 69 6e 67 29 3d  ring, &zString)=
26111 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  =0 ){.        re
26112 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a  turn 0;.      }.
26113 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d      }else if( c=
26114 3d 6d 61 74 63 68 53 65 74 20 29 7b 0a 20 20 20  =matchSet ){.   
26115 20 20 20 69 6e 74 20 70 72 69 6f 72 5f 63 20 3d     int prior_c =
26116 20 30 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   0;.      assert
26117 28 20 65 73 63 3d 3d 30 20 29 3b 20 20 20 20 2f  ( esc==0 );    /
26118 2a 20 54 68 69 73 20 6f 6e 6c 79 20 6f 63 63 75  * This only occu
26119 72 73 20 66 6f 72 20 47 4c 4f 42 2c 20 6e 6f 74  rs for GLOB, not
2611a 20 4c 49 4b 45 20 2a 2f 0a 20 20 20 20 20 20 73   LIKE */.      s
2611b 65 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 69  een = 0;.      i
2611c 6e 76 65 72 74 20 3d 20 30 3b 0a 20 20 20 20 20  nvert = 0;.     
2611d 20 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38   c = sqlite3Utf8
2611e 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a  Read(zString, &z
2611f 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 69  String);.      i
26120 66 28 20 63 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( c==0 ) return
26121 20 30 3b 0a 20 20 20 20 20 20 63 32 20 3d 20 73   0;.      c2 = s
26122 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
26123 50 61 74 74 65 72 6e 2c 20 26 7a 50 61 74 74 65  Pattern, &zPatte
26124 72 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 63  rn);.      if( c
26125 32 3d 3d 27 5e 27 20 29 7b 0a 20 20 20 20 20 20  2=='^' ){.      
26126 20 20 69 6e 76 65 72 74 20 3d 20 31 3b 0a 20 20    invert = 1;.  
26127 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74        c2 = sqlit
26128 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74  e3Utf8Read(zPatt
26129 65 72 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b  ern, &zPattern);
2612a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
2612b 66 28 20 63 32 3d 3d 27 5d 27 20 29 7b 0a 20 20  f( c2==']' ){.  
2612c 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 5d 27        if( c==']'
2612d 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20 20   ) seen = 1;.   
2612e 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65       c2 = sqlite
2612f 33 55 74 66 38 52 65 61 64 28 7a 50 61 74 74 65  3Utf8Read(zPatte
26130 72 6e 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a  rn, &zPattern);.
26131 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 77 68        }.      wh
26132 69 6c 65 28 20 63 32 20 26 26 20 63 32 21 3d 27  ile( c2 && c2!='
26133 5d 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ]' ){.        if
26134 28 20 63 32 3d 3d 27 2d 27 20 26 26 20 7a 50 61  ( c2=='-' && zPa
26135 74 74 65 72 6e 5b 30 5d 21 3d 27 5d 27 20 26 26  ttern[0]!=']' &&
26136 20 7a 50 61 74 74 65 72 6e 5b 30 5d 21 3d 30 20   zPattern[0]!=0 
26137 26 26 20 70 72 69 6f 72 5f 63 3e 30 20 29 7b 0a  && prior_c>0 ){.
26138 20 20 20 20 20 20 20 20 20 20 63 32 20 3d 20 73            c2 = s
26139 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
2613a 50 61 74 74 65 72 6e 2c 20 26 7a 50 61 74 74 65  Pattern, &zPatte
2613b 72 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  rn);.          i
2613c 66 28 20 63 3e 3d 70 72 69 6f 72 5f 63 20 26 26  f( c>=prior_c &&
2613d 20 63 3c 3d 63 32 20 29 20 73 65 65 6e 20 3d 20   c<=c2 ) seen = 
2613e 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72 69  1;.          pri
2613f 6f 72 5f 63 20 3d 20 30 3b 0a 20 20 20 20 20 20  or_c = 0;.      
26140 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
26141 20 20 20 69 66 28 20 63 3d 3d 63 32 20 29 7b 0a     if( c==c2 ){.
26142 20 20 20 20 20 20 20 20 20 20 20 20 73 65 65 6e              seen
26143 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20   = 1;.          
26144 7d 0a 20 20 20 20 20 20 20 20 20 20 70 72 69 6f  }.          prio
26145 72 5f 63 20 3d 20 63 32 3b 0a 20 20 20 20 20 20  r_c = c2;.      
26146 20 20 7d 0a 20 20 20 20 20 20 20 20 63 32 20 3d    }.        c2 =
26147 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
26148 28 7a 50 61 74 74 65 72 6e 2c 20 26 7a 50 61 74  (zPattern, &zPat
26149 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20  tern);.      }. 
2614a 20 20 20 20 20 69 66 28 20 63 32 3d 3d 30 20 7c       if( c2==0 |
2614b 7c 20 28 73 65 65 6e 20 5e 20 69 6e 76 65 72 74  | (seen ^ invert
2614c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
2614d 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
2614e 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  }.    }else if( 
2614f 65 73 63 3d 3d 63 20 26 26 20 21 70 72 65 76 45  esc==c && !prevE
26150 73 63 61 70 65 20 29 7b 0a 20 20 20 20 20 20 70  scape ){.      p
26151 72 65 76 45 73 63 61 70 65 20 3d 20 31 3b 0a 20  revEscape = 1;. 
26152 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
26153 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38  c2 = sqlite3Utf8
26154 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 26 7a  Read(zString, &z
26155 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 69  String);.      i
26156 66 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20 20  f( noCase ){.   
26157 20 20 20 20 20 47 6c 6f 67 55 70 70 65 72 54 6f       GlogUpperTo
26158 4c 6f 77 65 72 28 63 29 3b 0a 20 20 20 20 20 20  Lower(c);.      
26159 20 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77    GlogUpperToLow
2615a 65 72 28 63 32 29 3b 0a 20 20 20 20 20 20 7d 0a  er(c2);.      }.
2615b 20 20 20 20 20 20 69 66 28 20 63 21 3d 63 32 20        if( c!=c2 
2615c 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
2615d 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
2615e 20 20 20 70 72 65 76 45 73 63 61 70 65 20 3d 20     prevEscape = 
2615f 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  0;.    }.  }.  r
26160 65 74 75 72 6e 20 2a 7a 53 74 72 69 6e 67 3d 3d  eturn *zString==
26161 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e  0;.}../*.** Coun
26162 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
26163 74 69 6d 65 73 20 74 68 61 74 20 74 68 65 20 4c  times that the L
26164 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 28 6f 72  IKE operator (or
26165 20 47 4c 4f 42 20 77 68 69 63 68 20 69 73 0a 2a   GLOB which is.*
26166 2a 20 6a 75 73 74 20 61 20 76 61 72 69 61 74 69  * just a variati
26167 6f 6e 20 6f 66 20 4c 49 4b 45 29 20 67 65 74 73  on of LIKE) gets
26168 20 63 61 6c 6c 65 64 2e 20 20 54 68 69 73 20 69   called.  This i
26169 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69  s used for testi
2616a 6e 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23  ng.** only..*/.#
2616b 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
2616c 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
2616d 20 73 71 6c 69 74 65 33 5f 6c 69 6b 65 5f 63 6f   sqlite3_like_co
2616e 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  unt = 0;.#endif.
2616f 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
26170 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69  tation of the li
26171 6b 65 28 29 20 53 51 4c 20 66 75 6e 63 74 69 6f  ke() SQL functio
26172 6e 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69 6f  n.  This functio
26173 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20  n implements.** 
26174 74 68 65 20 62 75 69 6c 64 2d 69 6e 20 4c 49 4b  the build-in LIK
26175 45 20 6f 70 65 72 61 74 6f 72 2e 20 20 54 68 65  E operator.  The
26176 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
26177 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
26178 69 73 20 74 68 65 0a 2a 2a 20 70 61 74 74 65 72  is the.** patter
26179 6e 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64  n and the second
2617a 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
2617b 20 73 74 72 69 6e 67 2e 20 20 53 6f 2c 20 74 68   string.  So, th
2617c 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  e SQL statements
2617d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 41 20  :.**.**       A 
2617e 4c 49 4b 45 20 42 0a 2a 2a 0a 2a 2a 20 69 73 20  LIKE B.**.** is 
2617f 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20 6c  implemented as l
26180 69 6b 65 28 42 2c 41 29 2e 0a 2a 2a 0a 2a 2a 20  ike(B,A)..**.** 
26181 54 68 69 73 20 73 61 6d 65 20 66 75 6e 63 74 69  This same functi
26182 6f 6e 20 28 77 69 74 68 20 61 20 64 69 66 66 65  on (with a diffe
26183 72 65 6e 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f  rent compareInfo
26184 20 73 74 72 75 63 74 75 72 65 29 20 63 6f 6d 70   structure) comp
26185 75 74 65 73 0a 2a 2a 20 74 68 65 20 47 4c 4f 42  utes.** the GLOB
26186 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74   operator..*/.st
26187 61 74 69 63 20 76 6f 69 64 20 6c 69 6b 65 46 75  atic void likeFu
26188 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
26189 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20  ntext *context, 
2618a 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20  .  int argc, .  
2618b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
2618c 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  argv.){.  const 
2618d 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
2618e 41 2c 20 2a 7a 42 3b 0a 20 20 69 6e 74 20 65 73  A, *zB;.  int es
2618f 63 61 70 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20  cape = 0;.  int 
26190 6e 50 61 74 3b 0a 20 20 73 71 6c 69 74 65 33 20  nPat;.  sqlite3 
26191 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  *db = sqlite3_co
26192 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
26193 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 7a 42 20  context);..  zB 
26194 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
26195 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  text(argv[0]);. 
26196 20 7a 41 20 3d 20 73 71 6c 69 74 65 33 5f 76 61   zA = sqlite3_va
26197 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d  lue_text(argv[1]
26198 29 3b 0a 0a 20 20 2f 2a 20 4c 69 6d 69 74 20 74  );..  /* Limit t
26199 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65  he length of the
2619a 20 4c 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61   LIKE or GLOB pa
2619b 74 74 65 72 6e 20 74 6f 20 61 76 6f 69 64 20 70  ttern to avoid p
2619c 72 6f 62 6c 65 6d 73 0a 20 20 2a 2a 20 6f 66 20  roblems.  ** of 
2619d 64 65 65 70 20 72 65 63 75 72 73 69 6f 6e 20 61  deep recursion a
2619e 6e 64 20 4e 2a 4e 20 62 65 68 61 76 69 6f 72 20  nd N*N behavior 
2619f 69 6e 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72  in patternCompar
261a0 65 28 29 2e 0a 20 20 2a 2f 0a 20 20 6e 50 61 74  e()..  */.  nPat
261a1 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
261a2 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b  _bytes(argv[0]);
261a3 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e 50 61  .  testcase( nPa
261a4 74 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  t==db->aLimit[SQ
261a5 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45 5f  LITE_LIMIT_LIKE_
261a6 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 5d 20  PATTERN_LENGTH] 
261a7 29 3b 0a 20 20 74 65 73 74 63 61 73 65 28 20 6e  );.  testcase( n
261a8 50 61 74 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b  Pat==db->aLimit[
261a9 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b  SQLITE_LIMIT_LIK
261aa 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48  E_PATTERN_LENGTH
261ab 5d 2b 31 20 29 3b 0a 20 20 69 66 28 20 6e 50 61  ]+1 );.  if( nPa
261ac 74 20 3e 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  t > db->aLimit[S
261ad 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 49 4b 45  QLITE_LIMIT_LIKE
261ae 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 5d  _PATTERN_LENGTH]
261af 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
261b0 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e  result_error(con
261b1 74 65 78 74 2c 20 22 4c 49 4b 45 20 6f 72 20 47  text, "LIKE or G
261b2 4c 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 6f 20  LOB pattern too 
261b3 63 6f 6d 70 6c 65 78 22 2c 20 2d 31 29 3b 0a 20  complex", -1);. 
261b4 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20     return;.  }. 
261b5 20 61 73 73 65 72 74 28 20 7a 42 3d 3d 73 71 6c   assert( zB==sql
261b6 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
261b7 61 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20  argv[0]) );  /* 
261b8 45 6e 63 6f 64 69 6e 67 20 64 69 64 20 6e 6f 74  Encoding did not
261b9 20 63 68 61 6e 67 65 20 2a 2f 0a 0a 20 20 69 66   change */..  if
261ba 28 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20  ( argc==3 ){.   
261bb 20 2f 2a 20 54 68 65 20 65 73 63 61 70 65 20 63   /* The escape c
261bc 68 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 20  haracter string 
261bd 6d 75 73 74 20 63 6f 6e 73 69 73 74 20 6f 66 20  must consist of 
261be 61 20 73 69 6e 67 6c 65 20 55 54 46 2d 38 20 63  a single UTF-8 c
261bf 68 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a 2a  haracter..    **
261c0 20 4f 74 68 65 72 77 69 73 65 2c 20 72 65 74 75   Otherwise, retu
261c1 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20  rn an error..   
261c2 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e   */.    const un
261c3 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 45 73  signed char *zEs
261c4 63 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  c = sqlite3_valu
261c5 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29 3b  e_text(argv[2]);
261c6 0a 20 20 20 20 69 66 28 20 7a 45 73 63 3d 3d 30  .    if( zEsc==0
261c7 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 69   ) return;.    i
261c8 66 28 20 73 71 6c 69 74 65 33 55 74 66 38 43 68  f( sqlite3Utf8Ch
261c9 61 72 4c 65 6e 28 28 63 68 61 72 2a 29 7a 45 73  arLen((char*)zEs
261ca 63 2c 20 2d 31 29 21 3d 31 20 29 7b 0a 20 20 20  c, -1)!=1 ){.   
261cb 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
261cc 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c  t_error(context,
261cd 20 0a 20 20 20 20 20 20 20 20 20 20 22 45 53 43   .          "ESC
261ce 41 50 45 20 65 78 70 72 65 73 73 69 6f 6e 20 6d  APE expression m
261cf 75 73 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20  ust be a single 
261d0 63 68 61 72 61 63 74 65 72 22 2c 20 2d 31 29 3b  character", -1);
261d1 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
261d2 20 20 20 7d 0a 20 20 20 20 65 73 63 61 70 65 20     }.    escape 
261d3 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  = sqlite3Utf8Rea
261d4 64 28 7a 45 73 63 2c 20 26 7a 45 73 63 29 3b 0a  d(zEsc, &zEsc);.
261d5 20 20 7d 0a 20 20 69 66 28 20 7a 41 20 26 26 20    }.  if( zA && 
261d6 7a 42 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74  zB ){.    struct
261d7 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 2a 70 49   compareInfo *pI
261d8 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 5f 75 73  nfo = sqlite3_us
261d9 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29  er_data(context)
261da 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
261db 54 45 53 54 0a 20 20 20 20 73 71 6c 69 74 65 33  TEST.    sqlite3
261dc 5f 6c 69 6b 65 5f 63 6f 75 6e 74 2b 2b 3b 0a 23  _like_count++;.#
261dd 65 6e 64 69 66 0a 20 20 20 20 0a 20 20 20 20 73  endif.    .    s
261de 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
261df 74 28 63 6f 6e 74 65 78 74 2c 20 70 61 74 74 65  t(context, patte
261e0 72 6e 43 6f 6d 70 61 72 65 28 7a 42 2c 20 7a 41  rnCompare(zB, zA
261e1 2c 20 70 49 6e 66 6f 2c 20 65 73 63 61 70 65 29  , pInfo, escape)
261e2 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
261e3 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
261e4 66 20 74 68 65 20 4e 55 4c 4c 49 46 28 78 2c 79  f the NULLIF(x,y
261e5 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65  ) function.  The
261e6 20 72 65 73 75 6c 74 20 69 73 20 74 68 65 20 66   result is the f
261e7 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  irst.** argument
261e8 20 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74   if the argument
261e9 73 20 61 72 65 20 64 69 66 66 65 72 65 6e 74 2e  s are different.
261ea 20 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20    The result is 
261eb 4e 55 4c 4c 20 69 66 20 74 68 65 0a 2a 2a 20 61  NULL if the.** a
261ec 72 67 75 6d 65 6e 74 73 20 61 72 65 20 65 71 75  rguments are equ
261ed 61 6c 20 74 6f 20 65 61 63 68 20 6f 74 68 65 72  al to each other
261ee 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
261ef 20 6e 75 6c 6c 69 66 46 75 6e 63 28 0a 20 20 73   nullifFunc(.  s
261f0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
261f1 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e  context,.  int N
261f2 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65  otUsed,.  sqlite
261f3 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
261f4 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f  {.  CollSeq *pCo
261f5 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 46  ll = sqlite3GetF
261f6 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65  uncCollSeq(conte
261f7 78 74 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  xt);.  UNUSED_PA
261f8 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
261f9 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 4d  ;.  if( sqlite3M
261fa 65 6d 43 6f 6d 70 61 72 65 28 61 72 67 76 5b 30  emCompare(argv[0
261fb 5d 2c 20 61 72 67 76 5b 31 5d 2c 20 70 43 6f 6c  ], argv[1], pCol
261fc 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  l)!=0 ){.    sql
261fd 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75  ite3_result_valu
261fe 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b  e(context, argv[
261ff 30 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  0]);.  }.}../*.*
26200 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
26201 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 76   of the sqlite_v
26202 65 72 73 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f  ersion() functio
26203 6e 2e 20 20 54 68 65 20 72 65 73 75 6c 74 20 69  n.  The result i
26204 73 20 74 68 65 20 76 65 72 73 69 6f 6e 0a 2a 2a  s the version.**
26205 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20 6c   of the SQLite l
26206 69 62 72 61 72 79 20 74 68 61 74 20 69 73 20 72  ibrary that is r
26207 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69  unning..*/.stati
26208 63 20 76 6f 69 64 20 76 65 72 73 69 6f 6e 46 75  c void versionFu
26209 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
2620a 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
2620b 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20    int NotUsed,. 
2620c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2620d 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55  *NotUsed2.){.  U
2620e 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32  NUSED_PARAMETER2
2620f 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65  (NotUsed, NotUse
26210 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72  d2);.  sqlite3_r
26211 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
26212 78 74 2c 20 73 71 6c 69 74 65 33 5f 76 65 72 73  xt, sqlite3_vers
26213 69 6f 6e 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  ion, -1, SQLITE_
26214 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  STATIC);.}../*.*
26215 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
26216 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73   of the sqlite_s
26217 6f 75 72 63 65 5f 69 64 28 29 20 66 75 6e 63 74  ource_id() funct
26218 69 6f 6e 2e 20 54 68 65 20 72 65 73 75 6c 74 20  ion. The result 
26219 69 73 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 74  is a string.** t
2621a 68 61 74 20 69 64 65 6e 74 69 66 69 65 73 20 74  hat identifies t
2621b 68 65 20 70 61 72 74 69 63 75 6c 61 72 20 76 65  he particular ve
2621c 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 73 6f 75  rsion of the sou
2621d 72 63 65 20 63 6f 64 65 20 75 73 65 64 20 74 6f  rce code used to
2621e 20 62 75 69 6c 64 0a 2a 2a 20 53 51 4c 69 74 65   build.** SQLite
2621f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
26220 20 73 6f 75 72 63 65 69 64 46 75 6e 63 28 0a 20   sourceidFunc(. 
26221 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
26222 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
26223 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69   NotUsed,.  sqli
26224 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55  te3_value **NotU
26225 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55 53 45 44  sed2.){.  UNUSED
26226 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
26227 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29 3b 0a  sed, NotUsed2);.
26228 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
26229 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 53  _text(context, S
2622a 51 4c 49 54 45 5f 53 4f 55 52 43 45 5f 49 44 2c  QLITE_SOURCE_ID,
2622b 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54   -1, SQLITE_STAT
2622c 49 43 29 3b 0a 7d 0a 0a 2f 2a 20 41 72 72 61 79  IC);.}../* Array
2622d 20 66 6f 72 20 63 6f 6e 76 65 72 74 69 6e 67 20   for converting 
2622e 66 72 6f 6d 20 68 61 6c 66 2d 62 79 74 65 73 20  from half-bytes 
2622f 28 6e 79 62 62 6c 65 73 29 20 69 6e 74 6f 20 41  (nybbles) into A
26230 53 43 49 49 20 68 65 78 0a 2a 2a 20 64 69 67 69  SCII hex.** digi
26231 74 73 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f  ts. */.static co
26232 6e 73 74 20 63 68 61 72 20 68 65 78 64 69 67 69  nst char hexdigi
26233 74 73 5b 5d 20 3d 20 7b 0a 20 20 27 30 27 2c 20  ts[] = {.  '0', 
26234 27 31 27 2c 20 27 32 27 2c 20 27 33 27 2c 20 27  '1', '2', '3', '
26235 34 27 2c 20 27 35 27 2c 20 27 36 27 2c 20 27 37  4', '5', '6', '7
26236 27 2c 0a 20 20 27 38 27 2c 20 27 39 27 2c 20 27  ',.  '8', '9', '
26237 41 27 2c 20 27 42 27 2c 20 27 43 27 2c 20 27 44  A', 'B', 'C', 'D
26238 27 2c 20 27 45 27 2c 20 27 46 27 20 0a 7d 3b 0a  ', 'E', 'F' .};.
26239 0a 2f 2a 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  ./*.** EXPERIMEN
2623a 54 41 4c 20 2d 20 54 68 69 73 20 69 73 20 6e 6f  TAL - This is no
2623b 74 20 61 6e 20 6f 66 66 69 63 69 61 6c 20 66 75  t an official fu
2623c 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 69 6e 74  nction.  The int
2623d 65 72 66 61 63 65 20 6d 61 79 0a 2a 2a 20 63 68  erface may.** ch
2623e 61 6e 67 65 2e 20 20 54 68 69 73 20 66 75 6e 63  ange.  This func
2623f 74 69 6f 6e 20 6d 61 79 20 64 69 73 61 70 70 65  tion may disappe
26240 61 72 2e 20 20 44 6f 20 6e 6f 74 20 77 72 69 74  ar.  Do not writ
26241 65 20 63 6f 64 65 20 74 68 61 74 20 64 65 70 65  e code that depe
26242 6e 64 73 0a 2a 2a 20 6f 6e 20 74 68 69 73 20 66  nds.** on this f
26243 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49  unction..**.** I
26244 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
26245 20 74 68 65 20 51 55 4f 54 45 28 29 20 66 75 6e   the QUOTE() fun
26246 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 66 75 6e  ction.  This fun
26247 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69  ction takes a si
26248 6e 67 6c 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  ngle.** argument
26249 2e 20 20 49 66 20 74 68 65 20 61 72 67 75 6d 65  .  If the argume
2624a 6e 74 20 69 73 20 6e 75 6d 65 72 69 63 2c 20 74  nt is numeric, t
2624b 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
2624c 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 0a 2a  is the same as.*
2624d 2a 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 20  * the argument. 
2624e 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74   If the argument
2624f 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65   is NULL, the re
26250 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68  turn value is th
26251 65 20 73 74 72 69 6e 67 0a 2a 2a 20 22 4e 55 4c  e string.** "NUL
26252 4c 22 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20  L".  Otherwise, 
26253 74 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20  the argument is 
26254 65 6e 63 6c 6f 73 65 64 20 69 6e 20 73 69 6e 67  enclosed in sing
26255 6c 65 20 71 75 6f 74 65 73 20 77 69 74 68 0a 2a  le quotes with.*
26256 2a 20 73 69 6e 67 6c 65 2d 71 75 6f 74 65 20 65  * single-quote e
26257 73 63 61 70 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  scapes..*/.stati
26258 63 20 76 6f 69 64 20 71 75 6f 74 65 46 75 6e 63  c void quoteFunc
26259 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2625a 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61   *context, int a
2625b 72 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  rgc, sqlite3_val
2625c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 61 73  ue **argv){.  as
2625d 73 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b  sert( argc==1 );
2625e 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
2625f 54 45 52 28 61 72 67 63 29 3b 0a 20 20 73 77 69  TER(argc);.  swi
26260 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61 6c  tch( sqlite3_val
26261 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29  ue_type(argv[0])
26262 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c   ){.    case SQL
26263 49 54 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20  ITE_INTEGER:.   
26264 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 4c 4f   case SQLITE_FLO
26265 41 54 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69  AT: {.      sqli
26266 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
26267 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 30  (context, argv[0
26268 5d 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ]);.      break;
26269 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
2626a 53 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 7b 0a 20  SQLITE_BLOB: {. 
2626b 20 20 20 20 20 63 68 61 72 20 2a 7a 54 65 78 74       char *zText
2626c 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 68 61 72   = 0;.      char
2626d 20 63 6f 6e 73 74 20 2a 7a 42 6c 6f 62 20 3d 20   const *zBlob = 
2626e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
2626f 6f 62 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  ob(argv[0]);.   
26270 20 20 20 69 6e 74 20 6e 42 6c 6f 62 20 3d 20 73     int nBlob = s
26271 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
26272 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  es(argv[0]);.   
26273 20 20 20 61 73 73 65 72 74 28 20 7a 42 6c 6f 62     assert( zBlob
26274 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ==sqlite3_value_
26275 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 20 29 3b  blob(argv[0]) );
26276 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20   /* No encoding 
26277 63 68 61 6e 67 65 20 2a 2f 0a 20 20 20 20 20 20  change */.      
26278 7a 54 65 78 74 20 3d 20 28 63 68 61 72 20 2a 29  zText = (char *)
26279 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f  contextMalloc(co
2627a 6e 74 65 78 74 2c 20 28 32 2a 28 69 36 34 29 6e  ntext, (2*(i64)n
2627b 42 6c 6f 62 29 2b 34 29 3b 20 0a 20 20 20 20 20  Blob)+4); .     
2627c 20 69 66 28 20 7a 54 65 78 74 20 29 7b 0a 20 20   if( zText ){.  
2627d 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
2627e 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
2627f 6e 42 6c 6f 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  nBlob; i++){.   
26280 20 20 20 20 20 20 20 7a 54 65 78 74 5b 28 69 2a         zText[(i*
26281 32 29 2b 32 5d 20 3d 20 68 65 78 64 69 67 69 74  2)+2] = hexdigit
26282 73 5b 28 7a 42 6c 6f 62 5b 69 5d 3e 3e 34 29 26  s[(zBlob[i]>>4)&
26283 30 78 30 46 5d 3b 0a 20 20 20 20 20 20 20 20 20  0x0F];.         
26284 20 7a 54 65 78 74 5b 28 69 2a 32 29 2b 33 5d 20   zText[(i*2)+3] 
26285 3d 20 68 65 78 64 69 67 69 74 73 5b 28 7a 42 6c  = hexdigits[(zBl
26286 6f 62 5b 69 5d 29 26 30 78 30 46 5d 3b 0a 20 20  ob[i])&0x0F];.  
26287 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
26288 7a 54 65 78 74 5b 28 6e 42 6c 6f 62 2a 32 29 2b  zText[(nBlob*2)+
26289 32 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20  2] = '\'';.     
2628a 20 20 20 7a 54 65 78 74 5b 28 6e 42 6c 6f 62 2a     zText[(nBlob*
2628b 32 29 2b 33 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  2)+3] = '\0';.  
2628c 20 20 20 20 20 20 7a 54 65 78 74 5b 30 5d 20 3d        zText[0] =
2628d 20 27 58 27 3b 0a 20 20 20 20 20 20 20 20 7a 54   'X';.        zT
2628e 65 78 74 5b 31 5d 20 3d 20 27 5c 27 27 3b 0a 20  ext[1] = '\'';. 
2628f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72         sqlite3_r
26290 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
26291 78 74 2c 20 7a 54 65 78 74 2c 20 2d 31 2c 20 53  xt, zText, -1, S
26292 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
26293 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
26294 33 5f 66 72 65 65 28 7a 54 65 78 74 29 3b 0a 20  3_free(zText);. 
26295 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
26296 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
26297 73 65 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 20  se SQLITE_TEXT: 
26298 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 2c 6a 3b  {.      int i,j;
26299 0a 20 20 20 20 20 20 75 36 34 20 6e 3b 0a 20 20  .      u64 n;.  
2629a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e      const unsign
2629b 65 64 20 63 68 61 72 20 2a 7a 41 72 67 20 3d 20  ed char *zArg = 
2629c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2629d 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  xt(argv[0]);.   
2629e 20 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 20     char *z;..   
2629f 20 20 20 69 66 28 20 7a 41 72 67 3d 3d 30 20 29     if( zArg==0 )
262a0 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 66   return;.      f
262a1 6f 72 28 69 3d 30 2c 20 6e 3d 30 3b 20 7a 41 72  or(i=0, n=0; zAr
262a2 67 5b 69 5d 3b 20 69 2b 2b 29 7b 20 69 66 28 20  g[i]; i++){ if( 
262a3 7a 41 72 67 5b 69 5d 3d 3d 27 5c 27 27 20 29 20  zArg[i]=='\'' ) 
262a4 6e 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 7a 20 3d  n++; }.      z =
262a5 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63   contextMalloc(c
262a6 6f 6e 74 65 78 74 2c 20 28 28 69 36 34 29 69 29  ontext, ((i64)i)
262a7 2b 28 28 69 36 34 29 6e 29 2b 33 29 3b 0a 20 20  +((i64)n)+3);.  
262a8 20 20 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20      if( z ){.   
262a9 20 20 20 20 20 7a 5b 30 5d 20 3d 20 27 5c 27 27       z[0] = '\''
262aa 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  ;.        for(i=
262ab 30 2c 20 6a 3d 31 3b 20 7a 41 72 67 5b 69 5d 3b  0, j=1; zArg[i];
262ac 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
262ad 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 41 72 67 5b 69   z[j++] = zArg[i
262ae 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ];.          if(
262af 20 7a 41 72 67 5b 69 5d 3d 3d 27 5c 27 27 20 29   zArg[i]=='\'' )
262b0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a 5b  {.            z[
262b1 6a 2b 2b 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20  j++] = '\'';.   
262b2 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
262b3 20 7d 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b   }.        z[j++
262b4 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20  ] = '\'';.      
262b5 20 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20    z[j] = 0;.    
262b6 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
262b7 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  lt_text(context,
262b8 20 7a 2c 20 6a 2c 20 73 71 6c 69 74 65 33 5f 66   z, j, sqlite3_f
262b9 72 65 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ree);.      }.  
262ba 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
262bb 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
262bc 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73 71        assert( sq
262bd 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
262be 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54  (argv[0])==SQLIT
262bf 45 5f 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20  E_NULL );.      
262c0 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
262c1 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 22 4e 55  ext(context, "NU
262c2 4c 4c 22 2c 20 34 2c 20 53 51 4c 49 54 45 5f 53  LL", 4, SQLITE_S
262c3 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 62 72  TATIC);.      br
262c4 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  eak;.    }.  }.}
262c5 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 78 28  ../*.** The hex(
262c6 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20 49 6e 74  ) function.  Int
262c7 65 72 70 72 65 74 20 74 68 65 20 61 72 67 75 6d  erpret the argum
262c8 65 6e 74 20 61 73 20 61 20 62 6c 6f 62 2e 20 20  ent as a blob.  
262c9 52 65 74 75 72 6e 0a 2a 2a 20 61 20 68 65 78 61  Return.** a hexa
262ca 64 65 63 69 6d 61 6c 20 72 65 6e 64 65 72 69 6e  decimal renderin
262cb 67 20 61 73 20 74 65 78 74 2e 0a 2a 2f 0a 73 74  g as text..*/.st
262cc 61 74 69 63 20 76 6f 69 64 20 68 65 78 46 75 6e  atic void hexFun
262cd 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
262ce 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
262cf 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
262d0 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
262d1 76 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b  v.){.  int i, n;
262d2 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
262d3 64 20 63 68 61 72 20 2a 70 42 6c 6f 62 3b 0a 20  d char *pBlob;. 
262d4 20 63 68 61 72 20 2a 7a 48 65 78 2c 20 2a 7a 3b   char *zHex, *z;
262d5 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
262d6 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  =1 );.  UNUSED_P
262d7 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a  ARAMETER(argc);.
262d8 20 20 70 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65    pBlob = sqlite
262d9 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67  3_value_blob(arg
262da 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c  v[0]);.  n = sql
262db 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
262dc 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 61 73 73  (argv[0]);.  ass
262dd 65 72 74 28 20 70 42 6c 6f 62 3d 3d 73 71 6c 69  ert( pBlob==sqli
262de 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61  te3_value_blob(a
262df 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 4e  rgv[0]) );  /* N
262e0 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67  o encoding chang
262e1 65 20 2a 2f 0a 20 20 7a 20 3d 20 7a 48 65 78 20  e */.  z = zHex 
262e2 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28  = contextMalloc(
262e3 63 6f 6e 74 65 78 74 2c 20 28 28 69 36 34 29 6e  context, ((i64)n
262e4 29 2a 32 20 2b 20 31 29 3b 0a 20 20 69 66 28 20  )*2 + 1);.  if( 
262e5 7a 48 65 78 20 29 7b 0a 20 20 20 20 66 6f 72 28  zHex ){.    for(
262e6 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c 20 70  i=0; i<n; i++, p
262e7 42 6c 6f 62 2b 2b 29 7b 0a 20 20 20 20 20 20 75  Blob++){.      u
262e8 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 20 3d  nsigned char c =
262e9 20 2a 70 42 6c 6f 62 3b 0a 20 20 20 20 20 20 2a   *pBlob;.      *
262ea 28 7a 2b 2b 29 20 3d 20 68 65 78 64 69 67 69 74  (z++) = hexdigit
262eb 73 5b 28 63 3e 3e 34 29 26 30 78 66 5d 3b 0a 20  s[(c>>4)&0xf];. 
262ec 20 20 20 20 20 2a 28 7a 2b 2b 29 20 3d 20 68 65       *(z++) = he
262ed 78 64 69 67 69 74 73 5b 63 26 30 78 66 5d 3b 0a  xdigits[c&0xf];.
262ee 20 20 20 20 7d 0a 20 20 20 20 2a 7a 20 3d 20 30      }.    *z = 0
262ef 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
262f0 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
262f1 74 2c 20 7a 48 65 78 2c 20 6e 2a 32 2c 20 73 71  t, zHex, n*2, sq
262f2 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 7d  lite3_free);.  }
262f3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 7a 65  .}../*.** The ze
262f4 72 6f 62 6c 6f 62 28 4e 29 20 66 75 6e 63 74 69  roblob(N) functi
262f5 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 7a 65 72  on returns a zer
262f6 6f 2d 66 69 6c 6c 65 64 20 62 6c 6f 62 20 6f 66  o-filled blob of
262f7 20 73 69 7a 65 20 4e 20 62 79 74 65 73 2e 0a 2a   size N bytes..*
262f8 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 7a 65  /.static void ze
262f9 72 6f 62 6c 6f 62 46 75 6e 63 28 0a 20 20 73 71  roblobFunc(.  sq
262fa 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
262fb 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
262fc 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
262fd 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
262fe 69 36 34 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33  i64 n;.  sqlite3
262ff 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63   *db = sqlite3_c
26300 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
26301 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 61 73 73  (context);.  ass
26302 65 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a  ert( argc==1 );.
26303 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
26304 45 52 28 61 72 67 63 29 3b 0a 20 20 6e 20 3d 20  ER(argc);.  n = 
26305 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
26306 74 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  t64(argv[0]);.  
26307 74 65 73 74 63 61 73 65 28 20 6e 3d 3d 64 62 2d  testcase( n==db-
26308 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
26309 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a  IMIT_LENGTH] );.
2630a 20 20 74 65 73 74 63 61 73 65 28 20 6e 3d 3d 64    testcase( n==d
2630b 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
2630c 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 2b 31  _LIMIT_LENGTH]+1
2630d 20 29 3b 0a 20 20 69 66 28 20 6e 3e 64 62 2d 3e   );.  if( n>db->
2630e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
2630f 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20  MIT_LENGTH] ){. 
26310 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
26311 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28 63  t_error_toobig(c
26312 6f 6e 74 65 78 74 29 3b 0a 20 20 7d 65 6c 73 65  ontext);.  }else
26313 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
26314 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 28 63 6f  sult_zeroblob(co
26315 6e 74 65 78 74 2c 20 28 69 6e 74 29 6e 29 3b 0a  ntext, (int)n);.
26316 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65    }.}../*.** The
26317 20 72 65 70 6c 61 63 65 28 29 20 66 75 6e 63 74   replace() funct
26318 69 6f 6e 2e 20 20 54 68 72 65 65 20 61 72 67 75  ion.  Three argu
26319 6d 65 6e 74 73 20 61 72 65 20 61 6c 6c 20 73 74  ments are all st
2631a 72 69 6e 67 73 3a 20 63 61 6c 6c 0a 2a 2a 20 74  rings: call.** t
2631b 68 65 6d 20 41 2c 20 42 2c 20 61 6e 64 20 43 2e  hem A, B, and C.
2631c 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20 61   The result is a
2631d 6c 73 6f 20 61 20 73 74 72 69 6e 67 20 77 68 69  lso a string whi
2631e 63 68 20 69 73 20 64 65 72 69 76 65 64 0a 2a 2a  ch is derived.**
2631f 20 66 72 6f 6d 20 41 20 62 79 20 72 65 70 6c 61   from A by repla
26320 63 69 6e 67 20 65 76 65 72 79 20 6f 63 63 75 72  cing every occur
26321 61 6e 63 65 20 6f 66 20 42 20 77 69 74 68 20 43  ance of B with C
26322 2e 20 20 54 68 65 20 6d 61 74 63 68 0a 2a 2a 20  .  The match.** 
26323 6d 75 73 74 20 62 65 20 65 78 61 63 74 2e 20 20  must be exact.  
26324 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  Collating sequen
26325 63 65 73 20 61 72 65 20 6e 6f 74 20 75 73 65 64  ces are not used
26326 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
26327 20 72 65 70 6c 61 63 65 46 75 6e 63 28 0a 20 20   replaceFunc(.  
26328 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
26329 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
2632a 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
2632b 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
2632c 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
2632d 20 63 68 61 72 20 2a 7a 53 74 72 3b 20 20 20 20   char *zStr;    
2632e 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 70 75 74      /* The input
2632f 20 73 74 72 69 6e 67 20 41 20 2a 2f 0a 20 20 63   string A */.  c
26330 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
26331 61 72 20 2a 7a 50 61 74 74 65 72 6e 3b 20 20 20  ar *zPattern;   
26332 20 2f 2a 20 54 68 65 20 70 61 74 74 65 72 6e 20   /* The pattern 
26333 73 74 72 69 6e 67 20 42 20 2a 2f 0a 20 20 63 6f  string B */.  co
26334 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
26335 72 20 2a 7a 52 65 70 3b 20 20 20 20 20 20 20 20  r *zRep;        
26336 2f 2a 20 54 68 65 20 72 65 70 6c 61 63 65 6d 65  /* The replaceme
26337 6e 74 20 73 74 72 69 6e 67 20 43 20 2a 2f 0a 20  nt string C */. 
26338 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
26339 7a 4f 75 74 3b 20 20 20 20 20 20 20 20 20 20 20  zOut;           
2633a 20 20 20 2f 2a 20 54 68 65 20 6f 75 74 70 75 74     /* The output
2633b 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 74 72 3b 20   */.  int nStr; 
2633c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2633d 2a 20 53 69 7a 65 20 6f 66 20 7a 53 74 72 20 2a  * Size of zStr *
2633e 2f 0a 20 20 69 6e 74 20 6e 50 61 74 74 65 72 6e  /.  int nPattern
2633f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
26340 53 69 7a 65 20 6f 66 20 7a 50 61 74 74 65 72 6e  Size of zPattern
26341 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 70 3b 20   */.  int nRep; 
26342 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26343 2a 20 53 69 7a 65 20 6f 66 20 7a 52 65 70 20 2a  * Size of zRep *
26344 2f 0a 20 20 69 36 34 20 6e 4f 75 74 3b 20 20 20  /.  i64 nOut;   
26345 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26346 4d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20  Maximum size of 
26347 7a 4f 75 74 20 2a 2f 0a 20 20 69 6e 74 20 6c 6f  zOut */.  int lo
26348 6f 70 4c 69 6d 69 74 3b 20 20 20 20 20 20 20 20  opLimit;        
26349 20 20 20 2f 2a 20 4c 61 73 74 20 7a 53 74 72 5b     /* Last zStr[
2634a 5d 20 74 68 61 74 20 6d 69 67 68 74 20 6d 61 74  ] that might mat
2634b 63 68 20 7a 50 61 74 74 65 72 6e 5b 5d 20 2a 2f  ch zPattern[] */
2634c 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20  .  int i, j;    
2634d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2634e 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a  oop counters */.
2634f 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
26350 3d 33 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50  =3 );.  UNUSED_P
26351 41 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a  ARAMETER(argc);.
26352 20 20 7a 53 74 72 20 3d 20 73 71 6c 69 74 65 33    zStr = sqlite3
26353 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
26354 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a 53 74 72  [0]);.  if( zStr
26355 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
26356 6e 53 74 72 20 3d 20 73 71 6c 69 74 65 33 5f 76  nStr = sqlite3_v
26357 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
26358 30 5d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 7a  0]);.  assert( z
26359 53 74 72 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c  Str==sqlite3_val
2635a 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
2635b 20 29 3b 20 20 2f 2a 20 4e 6f 20 65 6e 63 6f 64   );  /* No encod
2635c 69 6e 67 20 63 68 61 6e 67 65 20 2a 2f 0a 20 20  ing change */.  
2635d 7a 50 61 74 74 65 72 6e 20 3d 20 73 71 6c 69 74  zPattern = sqlit
2635e 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
2635f 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20 7a 50  gv[1]);.  if( zP
26360 61 74 74 65 72 6e 3d 3d 30 20 29 7b 0a 20 20 20  attern==0 ){.   
26361 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
26362 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
26363 5b 31 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c  [1])==SQLITE_NUL
26364 4c 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c  L.            ||
26365 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
26366 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
26367 78 74 29 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  xt)->mallocFaile
26368 64 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  d );.    return;
26369 0a 20 20 7d 0a 20 20 69 66 28 20 7a 50 61 74 74  .  }.  if( zPatt
2636a 65 72 6e 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20  ern[0]==0 ){.   
2636b 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
2636c 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
2636d 5b 31 5d 29 21 3d 53 51 4c 49 54 45 5f 4e 55 4c  [1])!=SQLITE_NUL
2636e 4c 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  L );.    sqlite3
2636f 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f  _result_value(co
26370 6e 74 65 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b  ntext, argv[0]);
26371 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
26372 0a 20 20 6e 50 61 74 74 65 72 6e 20 3d 20 73 71  .  nPattern = sq
26373 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
26374 73 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 61 73  s(argv[1]);.  as
26375 73 65 72 74 28 20 7a 50 61 74 74 65 72 6e 3d 3d  sert( zPattern==
26376 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
26377 78 74 28 61 72 67 76 5b 31 5d 29 20 29 3b 20 20  xt(argv[1]) );  
26378 2f 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63  /* No encoding c
26379 68 61 6e 67 65 20 2a 2f 0a 20 20 7a 52 65 70 20  hange */.  zRep 
2637a 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
2637b 74 65 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20  text(argv[2]);. 
2637c 20 69 66 28 20 7a 52 65 70 3d 3d 30 20 29 20 72   if( zRep==0 ) r
2637d 65 74 75 72 6e 3b 0a 20 20 6e 52 65 70 20 3d 20  eturn;.  nRep = 
2637e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
2637f 74 65 73 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20  tes(argv[2]);.  
26380 61 73 73 65 72 74 28 20 7a 52 65 70 3d 3d 73 71  assert( zRep==sq
26381 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
26382 28 61 72 67 76 5b 32 5d 29 20 29 3b 0a 20 20 6e  (argv[2]) );.  n
26383 4f 75 74 20 3d 20 6e 53 74 72 20 2b 20 31 3b 0a  Out = nStr + 1;.
26384 20 20 61 73 73 65 72 74 28 20 6e 4f 75 74 3c 53    assert( nOut<S
26385 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48  QLITE_MAX_LENGTH
26386 20 29 3b 0a 20 20 7a 4f 75 74 20 3d 20 63 6f 6e   );.  zOut = con
26387 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65  textMalloc(conte
26388 78 74 2c 20 28 69 36 34 29 6e 4f 75 74 29 3b 0a  xt, (i64)nOut);.
26389 20 20 69 66 28 20 7a 4f 75 74 3d 3d 30 20 29 7b    if( zOut==0 ){
2638a 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
2638b 0a 20 20 6c 6f 6f 70 4c 69 6d 69 74 20 3d 20 6e  .  loopLimit = n
2638c 53 74 72 20 2d 20 6e 50 61 74 74 65 72 6e 3b 20  Str - nPattern; 
2638d 20 0a 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 69   .  for(i=j=0; i
2638e 3c 3d 6c 6f 6f 70 4c 69 6d 69 74 3b 20 69 2b 2b  <=loopLimit; i++
2638f 29 7b 0a 20 20 20 20 69 66 28 20 7a 53 74 72 5b  ){.    if( zStr[
26390 69 5d 21 3d 7a 50 61 74 74 65 72 6e 5b 30 5d 20  i]!=zPattern[0] 
26391 7c 7c 20 6d 65 6d 63 6d 70 28 26 7a 53 74 72 5b  || memcmp(&zStr[
26392 69 5d 2c 20 7a 50 61 74 74 65 72 6e 2c 20 6e 50  i], zPattern, nP
26393 61 74 74 65 72 6e 29 20 29 7b 0a 20 20 20 20 20  attern) ){.     
26394 20 7a 4f 75 74 5b 6a 2b 2b 5d 20 3d 20 7a 53 74   zOut[j++] = zSt
26395 72 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  r[i];.    }else{
26396 0a 20 20 20 20 20 20 75 38 20 2a 7a 4f 6c 64 3b  .      u8 *zOld;
26397 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a  .      sqlite3 *
26398 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  db = sqlite3_con
26399 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
2639a 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20 6e  ontext);.      n
2639b 4f 75 74 20 2b 3d 20 6e 52 65 70 20 2d 20 6e 50  Out += nRep - nP
2639c 61 74 74 65 72 6e 3b 0a 20 20 20 20 20 20 74 65  attern;.      te
2639d 73 74 63 61 73 65 28 20 6e 4f 75 74 2d 31 3d 3d  stcase( nOut-1==
2639e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
2639f 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20  E_LIMIT_LENGTH] 
263a0 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
263a1 65 28 20 6e 4f 75 74 2d 32 3d 3d 64 62 2d 3e 61  e( nOut-2==db->a
263a2 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
263a3 49 54 5f 4c 45 4e 47 54 48 5d 20 29 3b 0a 20 20  IT_LENGTH] );.  
263a4 20 20 20 20 69 66 28 20 6e 4f 75 74 2d 31 3e 64      if( nOut-1>d
263a5 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
263a6 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29  _LIMIT_LENGTH] )
263a7 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
263a8 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  3_result_error_t
263a9 6f 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a  oobig(context);.
263aa 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
263ab 62 46 72 65 65 28 64 62 2c 20 7a 4f 75 74 29 3b  bFree(db, zOut);
263ac 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b  .        return;
263ad 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a  .      }.      z
263ae 4f 6c 64 20 3d 20 7a 4f 75 74 3b 0a 20 20 20 20  Old = zOut;.    
263af 20 20 7a 4f 75 74 20 3d 20 73 71 6c 69 74 65 33    zOut = sqlite3
263b0 5f 72 65 61 6c 6c 6f 63 28 7a 4f 75 74 2c 20 28  _realloc(zOut, (
263b1 69 6e 74 29 6e 4f 75 74 29 3b 0a 20 20 20 20 20  int)nOut);.     
263b2 20 69 66 28 20 7a 4f 75 74 3d 3d 30 20 29 7b 0a   if( zOut==0 ){.
263b3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
263b4 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
263b5 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  em(context);.   
263b6 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72       sqlite3DbFr
263b7 65 65 28 64 62 2c 20 7a 4f 6c 64 29 3b 0a 20 20  ee(db, zOld);.  
263b8 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
263b9 20 20 20 20 7d 0a 20 20 20 20 20 20 6d 65 6d 63      }.      memc
263ba 70 79 28 26 7a 4f 75 74 5b 6a 5d 2c 20 7a 52 65  py(&zOut[j], zRe
263bb 70 2c 20 6e 52 65 70 29 3b 0a 20 20 20 20 20 20  p, nRep);.      
263bc 6a 20 2b 3d 20 6e 52 65 70 3b 0a 20 20 20 20 20  j += nRep;.     
263bd 20 69 20 2b 3d 20 6e 50 61 74 74 65 72 6e 2d 31   i += nPattern-1
263be 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73  ;.    }.  }.  as
263bf 73 65 72 74 28 20 6a 2b 6e 53 74 72 2d 69 2b 31  sert( j+nStr-i+1
263c0 3d 3d 6e 4f 75 74 20 29 3b 0a 20 20 6d 65 6d 63  ==nOut );.  memc
263c1 70 79 28 26 7a 4f 75 74 5b 6a 5d 2c 20 26 7a 53  py(&zOut[j], &zS
263c2 74 72 5b 69 5d 2c 20 6e 53 74 72 2d 69 29 3b 0a  tr[i], nStr-i);.
263c3 20 20 6a 20 2b 3d 20 6e 53 74 72 20 2d 20 69 3b    j += nStr - i;
263c4 0a 20 20 61 73 73 65 72 74 28 20 6a 3c 3d 6e 4f  .  assert( j<=nO
263c5 75 74 20 29 3b 0a 20 20 7a 4f 75 74 5b 6a 5d 20  ut );.  zOut[j] 
263c6 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72  = 0;.  sqlite3_r
263c7 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
263c8 78 74 2c 20 28 63 68 61 72 2a 29 7a 4f 75 74 2c  xt, (char*)zOut,
263c9 20 6a 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65   j, sqlite3_free
263ca 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c  );.}../*.** Impl
263cb 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
263cc 65 20 54 52 49 4d 28 29 2c 20 4c 54 52 49 4d 28  e TRIM(), LTRIM(
263cd 29 2c 20 61 6e 64 20 52 54 52 49 4d 28 29 20 66  ), and RTRIM() f
263ce 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65  unctions..** The
263cf 20 75 73 65 72 64 61 74 61 20 69 73 20 30 78 31   userdata is 0x1
263d0 20 66 6f 72 20 6c 65 66 74 20 74 72 69 6d 2c 20   for left trim, 
263d1 30 78 32 20 66 6f 72 20 72 69 67 68 74 20 74 72  0x2 for right tr
263d2 69 6d 2c 20 30 78 33 20 66 6f 72 20 62 6f 74 68  im, 0x3 for both
263d3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
263d4 20 74 72 69 6d 46 75 6e 63 28 0a 20 20 73 71 6c   trimFunc(.  sql
263d5 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
263d6 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67  ntext,.  int arg
263d7 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  c,.  sqlite3_val
263d8 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63  ue **argv.){.  c
263d9 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
263da 61 72 20 2a 7a 49 6e 3b 20 20 20 20 20 20 20 20  ar *zIn;        
263db 20 2f 2a 20 49 6e 70 75 74 20 73 74 72 69 6e 67   /* Input string
263dc 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69   */.  const unsi
263dd 67 6e 65 64 20 63 68 61 72 20 2a 7a 43 68 61 72  gned char *zChar
263de 53 65 74 3b 20 20 20 20 2f 2a 20 53 65 74 20 6f  Set;    /* Set o
263df 66 20 63 68 61 72 61 63 74 65 72 73 20 74 6f 20  f characters to 
263e0 74 72 69 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 49  trim */.  int nI
263e1 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
263e2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
263e3 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
263e4 6e 20 69 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74  n input */.  int
263e5 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20   flags;         
263e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
263e7 2a 20 31 3a 20 74 72 69 6d 6c 65 66 74 20 20 32  * 1: trimleft  2
263e8 3a 20 74 72 69 6d 72 69 67 68 74 20 20 33 3a 20  : trimright  3: 
263e9 74 72 69 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  trim */.  int i;
263ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
263eb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
263ec 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  oop counter */. 
263ed 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
263ee 61 4c 65 6e 20 3d 20 30 3b 20 20 20 20 20 20 20  aLen = 0;       
263ef 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
263f0 65 61 63 68 20 63 68 61 72 61 63 74 65 72 20 69  each character i
263f1 6e 20 7a 43 68 61 72 53 65 74 20 2a 2f 0a 20 20  n zCharSet */.  
263f2 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2a  unsigned char **
263f3 61 7a 43 68 61 72 20 3d 20 30 3b 20 20 20 20 20  azChar = 0;     
263f4 20 20 2f 2a 20 49 6e 64 69 76 69 64 75 61 6c 20    /* Individual 
263f5 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 43  characters in zC
263f6 68 61 72 53 65 74 20 2a 2f 0a 20 20 69 6e 74 20  harSet */.  int 
263f7 6e 43 68 61 72 3b 20 20 20 20 20 20 20 20 20 20  nChar;          
263f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
263f9 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61   Number of chara
263fa 63 74 65 72 73 20 69 6e 20 7a 43 68 61 72 53 65  cters in zCharSe
263fb 74 20 2a 2f 0a 0a 20 20 69 66 28 20 73 71 6c 69  t */..  if( sqli
263fc 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
263fd 72 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f  rgv[0])==SQLITE_
263fe 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 72 65 74 75  NULL ){.    retu
263ff 72 6e 3b 0a 20 20 7d 0a 20 20 7a 49 6e 20 3d 20  rn;.  }.  zIn = 
26400 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
26401 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69  xt(argv[0]);.  i
26402 66 28 20 7a 49 6e 3d 3d 30 20 29 20 72 65 74 75  f( zIn==0 ) retu
26403 72 6e 3b 0a 20 20 6e 49 6e 20 3d 20 73 71 6c 69  rn;.  nIn = sqli
26404 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
26405 61 72 67 76 5b 30 5d 29 3b 0a 20 20 61 73 73 65  argv[0]);.  asse
26406 72 74 28 20 7a 49 6e 3d 3d 73 71 6c 69 74 65 33  rt( zIn==sqlite3
26407 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
26408 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 61 72  [0]) );.  if( ar
26409 67 63 3d 3d 31 20 29 7b 0a 20 20 20 20 73 74 61  gc==1 ){.    sta
2640a 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  tic const unsign
2640b 65 64 20 63 68 61 72 20 6c 65 6e 4f 6e 65 5b 5d  ed char lenOne[]
2640c 20 3d 20 7b 20 31 20 7d 3b 0a 20 20 20 20 73 74   = { 1 };.    st
2640d 61 74 69 63 20 75 6e 73 69 67 6e 65 64 20 63 68  atic unsigned ch
2640e 61 72 20 2a 20 63 6f 6e 73 74 20 61 7a 4f 6e 65  ar * const azOne
2640f 5b 5d 20 3d 20 7b 20 28 75 38 2a 29 22 20 22 20  [] = { (u8*)" " 
26410 7d 3b 0a 20 20 20 20 6e 43 68 61 72 20 3d 20 31  };.    nChar = 1
26411 3b 0a 20 20 20 20 61 4c 65 6e 20 3d 20 28 75 38  ;.    aLen = (u8
26412 2a 29 6c 65 6e 4f 6e 65 3b 0a 20 20 20 20 61 7a  *)lenOne;.    az
26413 43 68 61 72 20 3d 20 28 75 6e 73 69 67 6e 65 64  Char = (unsigned
26414 20 63 68 61 72 20 2a 2a 29 61 7a 4f 6e 65 3b 0a   char **)azOne;.
26415 20 20 20 20 7a 43 68 61 72 53 65 74 20 3d 20 30      zCharSet = 0
26416 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 28 7a  ;.  }else if( (z
26417 43 68 61 72 53 65 74 20 3d 20 73 71 6c 69 74 65  CharSet = sqlite
26418 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
26419 76 5b 31 5d 29 29 3d 3d 30 20 29 7b 0a 20 20 20  v[1]))==0 ){.   
2641a 20 72 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65   return;.  }else
2641b 7b 0a 20 20 20 20 63 6f 6e 73 74 20 75 6e 73 69  {.    const unsi
2641c 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20  gned char *z;.  
2641d 20 20 66 6f 72 28 7a 3d 7a 43 68 61 72 53 65 74    for(z=zCharSet
2641e 2c 20 6e 43 68 61 72 3d 30 3b 20 2a 7a 3b 20 6e  , nChar=0; *z; n
2641f 43 68 61 72 2b 2b 29 7b 0a 20 20 20 20 20 20 53  Char++){.      S
26420 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28  QLITE_SKIP_UTF8(
26421 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  z);.    }.    if
26422 28 20 6e 43 68 61 72 3e 30 20 29 7b 0a 20 20 20  ( nChar>0 ){.   
26423 20 20 20 61 7a 43 68 61 72 20 3d 20 63 6f 6e 74     azChar = cont
26424 65 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78  extMalloc(contex
26425 74 2c 20 28 28 69 36 34 29 6e 43 68 61 72 29 2a  t, ((i64)nChar)*
26426 28 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2b 31  (sizeof(char*)+1
26427 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 61 7a  ));.      if( az
26428 43 68 61 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Char==0 ){.     
26429 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20     return;.     
2642a 20 7d 0a 20 20 20 20 20 20 61 4c 65 6e 20 3d 20   }.      aLen = 
2642b 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29  (unsigned char*)
2642c 26 61 7a 43 68 61 72 5b 6e 43 68 61 72 5d 3b 0a  &azChar[nChar];.
2642d 20 20 20 20 20 20 66 6f 72 28 7a 3d 7a 43 68 61        for(z=zCha
2642e 72 53 65 74 2c 20 6e 43 68 61 72 3d 30 3b 20 2a  rSet, nChar=0; *
2642f 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b 0a 20 20 20  z; nChar++){.   
26430 20 20 20 20 20 61 7a 43 68 61 72 5b 6e 43 68 61       azChar[nCha
26431 72 5d 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63  r] = (unsigned c
26432 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20 20 20 20  har *)z;.       
26433 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55 54 46   SQLITE_SKIP_UTF
26434 38 28 7a 29 3b 0a 20 20 20 20 20 20 20 20 61 4c  8(z);.        aL
26435 65 6e 5b 6e 43 68 61 72 5d 20 3d 20 28 75 38 29  en[nChar] = (u8)
26436 28 7a 20 2d 20 61 7a 43 68 61 72 5b 6e 43 68 61  (z - azChar[nCha
26437 72 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  r]);.      }.   
26438 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6e 43 68   }.  }.  if( nCh
26439 61 72 3e 30 20 29 7b 0a 20 20 20 20 66 6c 61 67  ar>0 ){.    flag
2643a 73 20 3d 20 53 51 4c 49 54 45 5f 50 54 52 5f 54  s = SQLITE_PTR_T
2643b 4f 5f 49 4e 54 28 73 71 6c 69 74 65 33 5f 75 73  O_INT(sqlite3_us
2643c 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29  er_data(context)
2643d 29 3b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73  );.    if( flags
2643e 20 26 20 31 20 29 7b 0a 20 20 20 20 20 20 77 68   & 1 ){.      wh
2643f 69 6c 65 28 20 6e 49 6e 3e 30 20 29 7b 0a 20 20  ile( nIn>0 ){.  
26440 20 20 20 20 20 20 69 6e 74 20 6c 65 6e 20 3d 20        int len = 
26441 30 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69  0;.        for(i
26442 3d 30 3b 20 69 3c 6e 43 68 61 72 3b 20 69 2b 2b  =0; i<nChar; i++
26443 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e  ){.          len
26444 20 3d 20 61 4c 65 6e 5b 69 5d 3b 0a 20 20 20 20   = aLen[i];.    
26445 20 20 20 20 20 20 69 66 28 20 6c 65 6e 3c 3d 6e        if( len<=n
26446 49 6e 20 26 26 20 6d 65 6d 63 6d 70 28 7a 49 6e  In && memcmp(zIn
26447 2c 20 61 7a 43 68 61 72 5b 69 5d 2c 20 6c 65 6e  , azChar[i], len
26448 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20  )==0 ) break;.  
26449 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2644a 69 66 28 20 69 3e 3d 6e 43 68 61 72 20 29 20 62  if( i>=nChar ) b
2644b 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7a 49  reak;.        zI
2644c 6e 20 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20  n += len;.      
2644d 20 20 6e 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20    nIn -= len;.  
2644e 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2644f 69 66 28 20 66 6c 61 67 73 20 26 20 32 20 29 7b  if( flags & 2 ){
26450 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 6e 49  .      while( nI
26451 6e 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  n>0 ){.        i
26452 6e 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20  nt len = 0;.    
26453 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
26454 43 68 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Char; i++){.    
26455 20 20 20 20 20 20 6c 65 6e 20 3d 20 61 4c 65 6e        len = aLen
26456 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69  [i];.          i
26457 66 28 20 6c 65 6e 3c 3d 6e 49 6e 20 26 26 20 6d  f( len<=nIn && m
26458 65 6d 63 6d 70 28 26 7a 49 6e 5b 6e 49 6e 2d 6c  emcmp(&zIn[nIn-l
26459 65 6e 5d 2c 61 7a 43 68 61 72 5b 69 5d 2c 6c 65  en],azChar[i],le
2645a 6e 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20  n)==0 ) break;. 
2645b 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2645c 20 69 66 28 20 69 3e 3d 6e 43 68 61 72 20 29 20   if( i>=nChar ) 
2645d 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 6e  break;.        n
2645e 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20 20 20  In -= len;.     
2645f 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
26460 20 7a 43 68 61 72 53 65 74 20 29 7b 0a 20 20 20   zCharSet ){.   
26461 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
26462 61 7a 43 68 61 72 29 3b 0a 20 20 20 20 7d 0a 20  azChar);.    }. 
26463 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73   }.  sqlite3_res
26464 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
26465 2c 20 28 63 68 61 72 2a 29 7a 49 6e 2c 20 6e 49  , (char*)zIn, nI
26466 6e 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  n, SQLITE_TRANSI
26467 45 4e 54 29 3b 0a 7d 0a 0a 0a 23 69 66 64 65 66  ENT);.}...#ifdef
26468 20 53 51 4c 49 54 45 5f 53 4f 55 4e 44 45 58 0a   SQLITE_SOUNDEX.
26469 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68  /*.** Compute th
2646a 65 20 73 6f 75 6e 64 65 78 20 65 6e 63 6f 64 69  e soundex encodi
2646b 6e 67 20 6f 66 20 61 20 77 6f 72 64 2e 0a 2a 2f  ng of a word..*/
2646c 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6f 75  .static void sou
2646d 6e 64 65 78 46 75 6e 63 28 0a 20 20 73 71 6c 69  ndexFunc(.  sqli
2646e 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
2646f 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
26470 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
26471 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 68  e **argv.){.  ch
26472 61 72 20 7a 52 65 73 75 6c 74 5b 38 5d 3b 0a 20  ar zResult[8];. 
26473 20 63 6f 6e 73 74 20 75 38 20 2a 7a 49 6e 3b 0a   const u8 *zIn;.
26474 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74    int i, j;.  st
26475 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
26476 6e 65 64 20 63 68 61 72 20 69 43 6f 64 65 5b 5d  ned char iCode[]
26477 20 3d 20 7b 0a 20 20 20 20 30 2c 20 30 2c 20 30   = {.    0, 0, 0
26478 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
26479 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2647a 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20  0, 0, 0,.    0, 
2647b 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2647c 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
2647d 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20   0, 0, 0, 0,.   
2647e 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2647f 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
26480 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
26481 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c  .    0, 0, 0, 0,
26482 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
26483 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
26484 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 31  , 0,.    0, 0, 1
26485 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20 32 2c  , 2, 3, 0, 1, 2,
26486 20 30 2c 20 30 2c 20 32 2c 20 32 2c 20 34 2c 20   0, 0, 2, 2, 4, 
26487 35 2c 20 35 2c 20 30 2c 0a 20 20 20 20 31 2c 20  5, 5, 0,.    1, 
26488 32 2c 20 36 2c 20 32 2c 20 33 2c 20 30 2c 20 31  2, 6, 2, 3, 0, 1
26489 2c 20 30 2c 20 32 2c 20 30 2c 20 32 2c 20 30 2c  , 0, 2, 0, 2, 0,
2648a 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20   0, 0, 0, 0,.   
2648b 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20 33 2c 20   0, 0, 1, 2, 3, 
2648c 30 2c 20 31 2c 20 32 2c 20 30 2c 20 30 2c 20 32  0, 1, 2, 0, 0, 2
2648d 2c 20 32 2c 20 34 2c 20 35 2c 20 35 2c 20 30 2c  , 2, 4, 5, 5, 0,
2648e 0a 20 20 20 20 31 2c 20 32 2c 20 36 2c 20 32 2c  .    1, 2, 6, 2,
2648f 20 33 2c 20 30 2c 20 31 2c 20 30 2c 20 32 2c 20   3, 0, 1, 0, 2, 
26490 30 2c 20 32 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 2, 0, 0, 0, 0
26491 2c 20 30 2c 0a 20 20 7d 3b 0a 20 20 61 73 73 65  , 0,.  };.  asse
26492 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20  rt( argc==1 );. 
26493 20 7a 49 6e 20 3d 20 28 75 38 2a 29 73 71 6c 69   zIn = (u8*)sqli
26494 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
26495 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 7a  rgv[0]);.  if( z
26496 49 6e 3d 3d 30 20 29 20 7a 49 6e 20 3d 20 28 75  In==0 ) zIn = (u
26497 38 2a 29 22 22 3b 0a 20 20 66 6f 72 28 69 3d 30  8*)"";.  for(i=0
26498 3b 20 7a 49 6e 5b 69 5d 20 26 26 20 21 73 71 6c  ; zIn[i] && !sql
26499 69 74 65 33 49 73 61 6c 70 68 61 28 7a 49 6e 5b  ite3Isalpha(zIn[
2649a 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 69 66  i]); i++){}.  if
2649b 28 20 7a 49 6e 5b 69 5d 20 29 7b 0a 20 20 20 20  ( zIn[i] ){.    
2649c 75 38 20 70 72 65 76 63 6f 64 65 20 3d 20 69 43  u8 prevcode = iC
2649d 6f 64 65 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d  ode[zIn[i]&0x7f]
2649e 3b 0a 20 20 20 20 7a 52 65 73 75 6c 74 5b 30 5d  ;.    zResult[0]
2649f 20 3d 20 73 71 6c 69 74 65 33 54 6f 75 70 70 65   = sqlite3Touppe
264a0 72 28 7a 49 6e 5b 69 5d 29 3b 0a 20 20 20 20 66  r(zIn[i]);.    f
264a1 6f 72 28 6a 3d 31 3b 20 6a 3c 34 20 26 26 20 7a  or(j=1; j<4 && z
264a2 49 6e 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20  In[i]; i++){.   
264a3 20 20 20 69 6e 74 20 63 6f 64 65 20 3d 20 69 43     int code = iC
264a4 6f 64 65 5b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d  ode[zIn[i]&0x7f]
264a5 3b 0a 20 20 20 20 20 20 69 66 28 20 63 6f 64 65  ;.      if( code
264a6 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  >0 ){.        if
264a7 28 20 63 6f 64 65 21 3d 70 72 65 76 63 6f 64 65  ( code!=prevcode
264a8 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 72   ){.          pr
264a9 65 76 63 6f 64 65 20 3d 20 63 6f 64 65 3b 0a 20  evcode = code;. 
264aa 20 20 20 20 20 20 20 20 20 7a 52 65 73 75 6c 74           zResult
264ab 5b 6a 2b 2b 5d 20 3d 20 63 6f 64 65 20 2b 20 27  [j++] = code + '
264ac 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  0';.        }.  
264ad 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
264ae 20 20 20 70 72 65 76 63 6f 64 65 20 3d 20 30 3b     prevcode = 0;
264af 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
264b0 20 20 20 77 68 69 6c 65 28 20 6a 3c 34 20 29 7b     while( j<4 ){
264b1 0a 20 20 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a  .      zResult[j
264b2 2b 2b 5d 20 3d 20 27 30 27 3b 0a 20 20 20 20 7d  ++] = '0';.    }
264b3 0a 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 5d 20  .    zResult[j] 
264b4 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
264b5 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
264b6 74 65 78 74 2c 20 7a 52 65 73 75 6c 74 2c 20 34  text, zResult, 4
264b7 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
264b8 4e 54 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  NT);.  }else{.  
264b9 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
264ba 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 22  _text(context, "
264bb 3f 30 30 30 22 2c 20 34 2c 20 53 51 4c 49 54 45  ?000", 4, SQLITE
264bc 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a  _STATIC);.  }.}.
264bd 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
264be 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44  SQLITE_OMIT_LOAD
264bf 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a  _EXTENSION./*.**
264c0 20 41 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74   A function that
264c1 20 6c 6f 61 64 73 20 61 20 73 68 61 72 65 64 2d   loads a shared-
264c2 6c 69 62 72 61 72 79 20 65 78 74 65 6e 73 69 6f  library extensio
264c3 6e 20 74 68 65 6e 20 72 65 74 75 72 6e 73 20 4e  n then returns N
264c4 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ULL..*/.static v
264c5 6f 69 64 20 6c 6f 61 64 45 78 74 28 73 71 6c 69  oid loadExt(sqli
264c6 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
264c7 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
264c8 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
264c9 61 72 67 76 29 7b 0a 20 20 63 6f 6e 73 74 20 63  argv){.  const c
264ca 68 61 72 20 2a 7a 46 69 6c 65 20 3d 20 28 63 6f  har *zFile = (co
264cb 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74  nst char *)sqlit
264cc 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
264cd 67 76 5b 30 5d 29 3b 0a 20 20 63 6f 6e 73 74 20  gv[0]);.  const 
264ce 63 68 61 72 20 2a 7a 50 72 6f 63 3b 0a 20 20 73  char *zProc;.  s
264cf 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c  qlite3 *db = sql
264d0 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
264d1 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b  handle(context);
264d2 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67  .  char *zErrMsg
264d3 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 61 72 67   = 0;..  if( arg
264d4 63 3d 3d 32 20 29 7b 0a 20 20 20 20 7a 50 72 6f  c==2 ){.    zPro
264d5 63 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  c = (const char 
264d6 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
264d7 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20  text(argv[1]);. 
264d8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 50 72 6f   }else{.    zPro
264d9 63 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  c = 0;.  }.  if(
264da 20 7a 46 69 6c 65 20 26 26 20 73 71 6c 69 74 65   zFile && sqlite
264db 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  3_load_extension
264dc 28 64 62 2c 20 7a 46 69 6c 65 2c 20 7a 50 72 6f  (db, zFile, zPro
264dd 63 2c 20 26 7a 45 72 72 4d 73 67 29 20 29 7b 0a  c, &zErrMsg) ){.
264de 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
264df 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
264e0 2c 20 7a 45 72 72 4d 73 67 2c 20 2d 31 29 3b 0a  , zErrMsg, -1);.
264e1 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
264e2 28 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 7d  (zErrMsg);.  }.}
264e3 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
264e4 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
264e5 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
264e6 75 63 74 75 72 65 20 68 6f 6c 64 73 20 74 68 65  ucture holds the
264e7 20 63 6f 6e 74 65 78 74 20 6f 66 20 61 0a 2a 2a   context of a.**
264e8 20 73 75 6d 28 29 20 6f 72 20 61 76 67 28 29 20   sum() or avg() 
264e9 61 67 67 72 65 67 61 74 65 20 63 6f 6d 70 75 74  aggregate comput
264ea 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65  ation..*/.typede
264eb 66 20 73 74 72 75 63 74 20 53 75 6d 43 74 78 20  f struct SumCtx 
264ec 53 75 6d 43 74 78 3b 0a 73 74 72 75 63 74 20 53  SumCtx;.struct S
264ed 75 6d 43 74 78 20 7b 0a 20 20 64 6f 75 62 6c 65  umCtx {.  double
264ee 20 72 53 75 6d 3b 20 20 20 20 20 20 2f 2a 20 46   rSum;      /* F
264ef 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75  loating point su
264f0 6d 20 2a 2f 0a 20 20 69 36 34 20 69 53 75 6d 3b  m */.  i64 iSum;
264f1 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 74 65           /* Inte
264f2 67 65 72 20 73 75 6d 20 2a 2f 20 20 20 0a 20 20  ger sum */   .  
264f3 69 36 34 20 63 6e 74 3b 20 20 20 20 20 20 20 20  i64 cnt;        
264f4 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
264f5 6c 65 6d 65 6e 74 73 20 73 75 6d 6d 65 64 20 2a  lements summed *
264f6 2f 0a 20 20 75 38 20 6f 76 65 72 66 6c 6f 77 3b  /.  u8 overflow;
264f7 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
264f8 20 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f   integer overflo
264f9 77 20 73 65 65 6e 20 2a 2f 0a 20 20 75 38 20 61  w seen */.  u8 a
264fa 70 70 72 6f 78 3b 20 20 20 20 20 20 20 20 2f 2a  pprox;        /*
264fb 20 54 72 75 65 20 69 66 20 6e 6f 6e 2d 69 6e 74   True if non-int
264fc 65 67 65 72 20 76 61 6c 75 65 20 77 61 73 20 69  eger value was i
264fd 6e 70 75 74 20 74 6f 20 74 68 65 20 73 75 6d 20  nput to the sum 
264fe 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75  */.};../*.** Rou
264ff 74 69 6e 65 73 20 75 73 65 64 20 74 6f 20 63 6f  tines used to co
26500 6d 70 75 74 65 20 74 68 65 20 73 75 6d 2c 20 61  mpute the sum, a
26501 76 65 72 61 67 65 2c 20 61 6e 64 20 74 6f 74 61  verage, and tota
26502 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 55 4d  l..**.** The SUM
26503 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 6c 6c  () function foll
26504 6f 77 73 20 74 68 65 20 28 62 72 6f 6b 65 6e 29  ows the (broken)
26505 20 53 51 4c 20 73 74 61 6e 64 61 72 64 20 77 68   SQL standard wh
26506 69 63 68 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61  ich means.** tha
26507 74 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c  t it returns NUL
26508 4c 20 69 66 20 69 74 20 73 75 6d 73 20 6f 76 65  L if it sums ove
26509 72 20 6e 6f 20 69 6e 70 75 74 73 2e 20 20 54 4f  r no inputs.  TO
2650a 54 41 4c 20 72 65 74 75 72 6e 73 0a 2a 2a 20 30  TAL returns.** 0
2650b 2e 30 20 69 6e 20 74 68 61 74 20 63 61 73 65 2e  .0 in that case.
2650c 20 20 49 6e 20 61 64 64 69 74 69 6f 6e 2c 20 54    In addition, T
2650d 4f 54 41 4c 20 61 6c 77 61 79 73 20 72 65 74 75  OTAL always retu
2650e 72 6e 73 20 61 20 66 6c 6f 61 74 20 77 68 65 72  rns a float wher
2650f 65 0a 2a 2a 20 53 55 4d 20 6d 69 67 68 74 20 72  e.** SUM might r
26510 65 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72  eturn an integer
26511 20 69 66 20 69 74 20 6e 65 76 65 72 20 65 6e 63   if it never enc
26512 6f 75 6e 74 65 72 73 20 61 20 66 6c 6f 61 74 69  ounters a floati
26513 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 76 61 6c 75  ng point.** valu
26514 65 2e 20 20 54 4f 54 41 4c 20 6e 65 76 65 72 20  e.  TOTAL never 
26515 66 61 69 6c 73 2c 20 62 75 74 20 53 55 4d 20 6d  fails, but SUM m
26516 69 67 68 74 20 74 68 72 6f 75 67 68 20 61 6e 20  ight through an 
26517 65 78 63 65 70 74 69 6f 6e 20 69 66 0a 2a 2a 20  exception if.** 
26518 69 74 20 6f 76 65 72 66 6c 6f 77 73 20 61 6e 20  it overflows an 
26519 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 73 74 61 74  integer..*/.stat
2651a 69 63 20 76 6f 69 64 20 73 75 6d 53 74 65 70 28  ic void sumStep(
2651b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
2651c 2a 63 6f 6e 74 65 78 74 2c 20 69 6e 74 20 61 72  *context, int ar
2651d 67 63 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  gc, sqlite3_valu
2651e 65 20 2a 2a 61 72 67 76 29 7b 0a 20 20 53 75 6d  e **argv){.  Sum
2651f 43 74 78 20 2a 70 3b 0a 20 20 69 6e 74 20 74 79  Ctx *p;.  int ty
26520 70 65 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72  pe;.  assert( ar
26521 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45  gc==1 );.  UNUSE
26522 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63  D_PARAMETER(argc
26523 29 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33  );.  p = sqlite3
26524 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
26525 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 69 7a 65  xt(context, size
26526 6f 66 28 2a 70 29 29 3b 0a 20 20 74 79 70 65 20  of(*p));.  type 
26527 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
26528 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 61 72 67  numeric_type(arg
26529 76 5b 30 5d 29 3b 0a 20 20 69 66 28 20 70 20 26  v[0]);.  if( p &
2652a 26 20 74 79 70 65 21 3d 53 51 4c 49 54 45 5f 4e  & type!=SQLITE_N
2652b 55 4c 4c 20 29 7b 0a 20 20 20 20 70 2d 3e 63 6e  ULL ){.    p->cn
2652c 74 2b 2b 3b 0a 20 20 20 20 69 66 28 20 74 79 70  t++;.    if( typ
2652d 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  e==SQLITE_INTEGE
2652e 52 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 76  R ){.      i64 v
2652f 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
26530 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d 29 3b  _int64(argv[0]);
26531 0a 20 20 20 20 20 20 70 2d 3e 72 53 75 6d 20 2b  .      p->rSum +
26532 3d 20 76 3b 0a 20 20 20 20 20 20 69 66 28 20 28  = v;.      if( (
26533 70 2d 3e 61 70 70 72 6f 78 7c 70 2d 3e 6f 76 65  p->approx|p->ove
26534 72 66 6c 6f 77 29 3d 3d 30 20 29 7b 0a 20 20 20  rflow)==0 ){.   
26535 20 20 20 20 20 69 36 34 20 69 4e 65 77 53 75 6d       i64 iNewSum
26536 20 3d 20 70 2d 3e 69 53 75 6d 20 2b 20 76 3b 0a   = p->iSum + v;.
26537 20 20 20 20 20 20 20 20 69 6e 74 20 73 31 20 3d          int s1 =
26538 20 28 69 6e 74 29 28 70 2d 3e 69 53 75 6d 20 3e   (int)(p->iSum >
26539 3e 20 28 73 69 7a 65 6f 66 28 69 36 34 29 2a 38  > (sizeof(i64)*8
2653a 2d 31 29 29 3b 0a 20 20 20 20 20 20 20 20 69 6e  -1));.        in
2653b 74 20 73 32 20 3d 20 28 69 6e 74 29 28 76 20 20  t s2 = (int)(v  
2653c 20 20 20 20 20 3e 3e 20 28 73 69 7a 65 6f 66 28       >> (sizeof(
2653d 69 36 34 29 2a 38 2d 31 29 29 3b 0a 20 20 20 20  i64)*8-1));.    
2653e 20 20 20 20 69 6e 74 20 73 33 20 3d 20 28 69 6e      int s3 = (in
2653f 74 29 28 69 4e 65 77 53 75 6d 20 3e 3e 20 28 73  t)(iNewSum >> (s
26540 69 7a 65 6f 66 28 69 36 34 29 2a 38 2d 31 29 29  izeof(i64)*8-1))
26541 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6f 76 65  ;.        p->ove
26542 72 66 6c 6f 77 20 3d 20 28 28 73 31 26 73 32 26  rflow = ((s1&s2&
26543 7e 73 33 29 20 7c 20 28 7e 73 31 26 7e 73 32 26  ~s3) | (~s1&~s2&
26544 73 33 29 29 3f 31 3a 30 3b 0a 20 20 20 20 20 20  s3))?1:0;.      
26545 20 20 70 2d 3e 69 53 75 6d 20 3d 20 69 4e 65 77    p->iSum = iNew
26546 53 75 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  Sum;.      }.   
26547 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 2d   }else{.      p-
26548 3e 72 53 75 6d 20 2b 3d 20 73 71 6c 69 74 65 33  >rSum += sqlite3
26549 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 72  _value_double(ar
2654a 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 70 2d  gv[0]);.      p-
2654b 3e 61 70 70 72 6f 78 20 3d 20 31 3b 0a 20 20 20  >approx = 1;.   
2654c 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20   }.  }.}.static 
2654d 76 6f 69 64 20 73 75 6d 46 69 6e 61 6c 69 7a 65  void sumFinalize
2654e 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
2654f 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75   *context){.  Su
26550 6d 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73  mCtx *p;.  p = s
26551 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
26552 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74  _context(context
26553 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 20 26 26  , 0);.  if( p &&
26554 20 70 2d 3e 63 6e 74 3e 30 20 29 7b 0a 20 20 20   p->cnt>0 ){.   
26555 20 69 66 28 20 70 2d 3e 6f 76 65 72 66 6c 6f 77   if( p->overflow
26556 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
26557 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63  3_result_error(c
26558 6f 6e 74 65 78 74 2c 22 69 6e 74 65 67 65 72 20  ontext,"integer 
26559 6f 76 65 72 66 6c 6f 77 22 2c 2d 31 29 3b 0a 20  overflow",-1);. 
2655a 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e     }else if( p->
2655b 61 70 70 72 6f 78 20 29 7b 0a 20 20 20 20 20 20  approx ){.      
2655c 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64  sqlite3_result_d
2655d 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70  ouble(context, p
2655e 2d 3e 72 53 75 6d 29 3b 0a 20 20 20 20 7d 65 6c  ->rSum);.    }el
2655f 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
26560 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63  3_result_int64(c
26561 6f 6e 74 65 78 74 2c 20 70 2d 3e 69 53 75 6d 29  ontext, p->iSum)
26562 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74  ;.    }.  }.}.st
26563 61 74 69 63 20 76 6f 69 64 20 61 76 67 46 69 6e  atic void avgFin
26564 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f  alize(sqlite3_co
26565 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b  ntext *context){
26566 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20 20  .  SumCtx *p;.  
26567 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72  p = sqlite3_aggr
26568 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f  egate_context(co
26569 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 69 66 28  ntext, 0);.  if(
2656a 20 70 20 26 26 20 70 2d 3e 63 6e 74 3e 30 20 29   p && p->cnt>0 )
2656b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
2656c 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74  sult_double(cont
2656d 65 78 74 2c 20 70 2d 3e 72 53 75 6d 2f 28 64 6f  ext, p->rSum/(do
2656e 75 62 6c 65 29 70 2d 3e 63 6e 74 29 3b 0a 20 20  uble)p->cnt);.  
2656f 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  }.}.static void 
26570 74 6f 74 61 6c 46 69 6e 61 6c 69 7a 65 28 73 71  totalFinalize(sq
26571 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
26572 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d 43 74  ontext){.  SumCt
26573 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69  x *p;.  p = sqli
26574 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
26575 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30  ntext(context, 0
26576 29 3b 0a 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29  );.  /* (double)
26577 30 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c  0 In case of SQL
26578 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
26579 47 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20  G_POINT... */.  
2657a 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64  sqlite3_result_d
2657b 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 70  ouble(context, p
2657c 20 3f 20 70 2d 3e 72 53 75 6d 20 3a 20 28 64 6f   ? p->rSum : (do
2657d 75 62 6c 65 29 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  uble)0);.}../*.*
2657e 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
2657f 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 20  structure keeps 
26580 74 72 61 63 6b 20 6f 66 20 73 74 61 74 65 20 69  track of state i
26581 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74  nformation for t
26582 68 65 0a 2a 2a 20 63 6f 75 6e 74 28 29 20 61 67  he.** count() ag
26583 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
26584 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
26585 75 63 74 20 43 6f 75 6e 74 43 74 78 20 43 6f 75  uct CountCtx Cou
26586 6e 74 43 74 78 3b 0a 73 74 72 75 63 74 20 43 6f  ntCtx;.struct Co
26587 75 6e 74 43 74 78 20 7b 0a 20 20 69 36 34 20 6e  untCtx {.  i64 n
26588 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74  ;.};../*.** Rout
26589 69 6e 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  ines to implemen
2658a 74 20 74 68 65 20 63 6f 75 6e 74 28 29 20 61 67  t the count() ag
2658b 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
2658c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2658d 20 63 6f 75 6e 74 53 74 65 70 28 73 71 6c 69 74   countStep(sqlit
2658e 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
2658f 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
26590 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
26591 72 67 76 29 7b 0a 20 20 43 6f 75 6e 74 43 74 78  rgv){.  CountCtx
26592 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74   *p;.  p = sqlit
26593 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
26594 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 69  text(context, si
26595 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 69 66 28  zeof(*p));.  if(
26596 20 28 61 72 67 63 3d 3d 30 20 7c 7c 20 53 51 4c   (argc==0 || SQL
26597 49 54 45 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65  ITE_NULL!=sqlite
26598 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
26599 76 5b 30 5d 29 29 20 26 26 20 70 20 29 7b 0a 20  v[0])) && p ){. 
2659a 20 20 20 70 2d 3e 6e 2b 2b 3b 0a 20 20 7d 0a 0a     p->n++;.  }..
2659b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2659c 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 20  MIT_DEPRECATED. 
2659d 20 2f 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f   /* The sqlite3_
2659e 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 28  aggregate_count(
2659f 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 65  ) function is de
265a0 70 72 65 63 61 74 65 64 2e 20 20 42 75 74 20 6a  precated.  But j
265a1 75 73 74 20 74 6f 20 6d 61 6b 65 0a 20 20 2a 2a  ust to make.  **
265a2 20 73 75 72 65 20 69 74 20 73 74 69 6c 6c 20 6f   sure it still o
265a3 70 65 72 61 74 65 73 20 63 6f 72 72 65 63 74 6c  perates correctl
265a4 79 2c 20 76 65 72 69 66 79 20 74 68 61 74 20 69  y, verify that i
265a5 74 73 20 63 6f 75 6e 74 20 61 67 72 65 65 73 20  ts count agrees 
265a6 77 69 74 68 20 6f 75 72 20 0a 20 20 2a 2a 20 69  with our .  ** i
265a7 6e 74 65 72 6e 61 6c 20 63 6f 75 6e 74 20 77 68  nternal count wh
265a8 65 6e 20 75 73 69 6e 67 20 63 6f 75 6e 74 28 2a  en using count(*
265a9 29 20 61 6e 64 20 77 68 65 6e 20 74 68 65 20 74  ) and when the t
265aa 6f 74 61 6c 20 63 6f 75 6e 74 20 63 61 6e 20 62  otal count can b
265ab 65 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 65 64  e.  ** expressed
265ac 20 61 73 20 61 20 33 32 2d 62 69 74 20 69 6e 74   as a 32-bit int
265ad 65 67 65 72 2e 20 2a 2f 0a 20 20 61 73 73 65 72  eger. */.  asser
265ae 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c 20 70 3d  t( argc==1 || p=
265af 3d 30 20 7c 7c 20 70 2d 3e 6e 3e 30 78 37 66 66  =0 || p->n>0x7ff
265b0 66 66 66 66 66 0a 20 20 20 20 20 20 20 20 20 20  fffff.          
265b1 7c 7c 20 70 2d 3e 6e 3d 3d 73 71 6c 69 74 65 33  || p->n==sqlite3
265b2 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74  _aggregate_count
265b3 28 63 6f 6e 74 65 78 74 29 20 29 3b 0a 23 65 6e  (context) );.#en
265b4 64 69 66 0a 7d 20 20 20 0a 73 74 61 74 69 63 20  dif.}   .static 
265b5 76 6f 69 64 20 63 6f 75 6e 74 46 69 6e 61 6c 69  void countFinali
265b6 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ze(sqlite3_conte
265b7 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20  xt *context){.  
265b8 43 6f 75 6e 74 43 74 78 20 2a 70 3b 0a 20 20 70  CountCtx *p;.  p
265b9 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65   = sqlite3_aggre
265ba 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e  gate_context(con
265bb 74 65 78 74 2c 20 30 29 3b 0a 20 20 73 71 6c 69  text, 0);.  sqli
265bc 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34  te3_result_int64
265bd 28 63 6f 6e 74 65 78 74 2c 20 70 20 3f 20 70 2d  (context, p ? p-
265be 3e 6e 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  >n : 0);.}../*.*
265bf 2a 20 52 6f 75 74 69 6e 65 73 20 74 6f 20 69 6d  * Routines to im
265c0 70 6c 65 6d 65 6e 74 20 6d 69 6e 28 29 20 61 6e  plement min() an
265c1 64 20 6d 61 78 28 29 20 61 67 67 72 65 67 61 74  d max() aggregat
265c2 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a  e functions..*/.
265c3 73 74 61 74 69 63 20 76 6f 69 64 20 6d 69 6e 6d  static void minm
265c4 61 78 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65  axStep(.  sqlite
265c5 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
265c6 78 74 2c 20 0a 20 20 69 6e 74 20 4e 6f 74 55 73  xt, .  int NotUs
265c7 65 64 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76  ed, .  sqlite3_v
265c8 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
265c9 20 4d 65 6d 20 2a 70 41 72 67 20 20 3d 20 28 4d   Mem *pArg  = (M
265ca 65 6d 20 2a 29 61 72 67 76 5b 30 5d 3b 0a 20 20  em *)argv[0];.  
265cb 4d 65 6d 20 2a 70 42 65 73 74 3b 0a 20 20 55 4e  Mem *pBest;.  UN
265cc 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e  USED_PARAMETER(N
265cd 6f 74 55 73 65 64 29 3b 0a 0a 20 20 69 66 28 20  otUsed);..  if( 
265ce 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
265cf 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c  pe(argv[0])==SQL
265d0 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75 72  ITE_NULL ) retur
265d1 6e 3b 0a 20 20 70 42 65 73 74 20 3d 20 28 4d 65  n;.  pBest = (Me
265d2 6d 20 2a 29 73 71 6c 69 74 65 33 5f 61 67 67 72  m *)sqlite3_aggr
265d3 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f  egate_context(co
265d4 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70  ntext, sizeof(*p
265d5 42 65 73 74 29 29 3b 0a 20 20 69 66 28 20 21 70  Best));.  if( !p
265d6 42 65 73 74 20 29 20 72 65 74 75 72 6e 3b 0a 0a  Best ) return;..
265d7 20 20 69 66 28 20 70 42 65 73 74 2d 3e 66 6c 61    if( pBest->fla
265d8 67 73 20 29 7b 0a 20 20 20 20 69 6e 74 20 6d 61  gs ){.    int ma
265d9 78 3b 0a 20 20 20 20 69 6e 74 20 63 6d 70 3b 0a  x;.    int cmp;.
265da 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
265db 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74 46  ll = sqlite3GetF
265dc 75 6e 63 43 6f 6c 6c 53 65 71 28 63 6f 6e 74 65  uncCollSeq(conte
265dd 78 74 29 3b 0a 20 20 20 20 2f 2a 20 54 68 69 73  xt);.    /* This
265de 20 73 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 69   step function i
265df 73 20 75 73 65 64 20 66 6f 72 20 62 6f 74 68 20  s used for both 
265e0 74 68 65 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61  the min() and ma
265e1 78 28 29 20 61 67 67 72 65 67 61 74 65 73 2c 0a  x() aggregates,.
265e2 20 20 20 20 2a 2a 20 74 68 65 20 6f 6e 6c 79 20      ** the only 
265e3 64 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65  difference betwe
265e4 65 6e 20 74 68 65 20 74 77 6f 20 62 65 69 6e 67  en the two being
265e5 20 74 68 61 74 20 74 68 65 20 73 65 6e 73 65 20   that the sense 
265e6 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 6f  of the.    ** co
265e7 6d 70 61 72 69 73 6f 6e 20 69 73 20 69 6e 76 65  mparison is inve
265e8 72 74 65 64 2e 20 46 6f 72 20 74 68 65 20 6d 61  rted. For the ma
265e9 78 28 29 20 61 67 67 72 65 67 61 74 65 2c 20 74  x() aggregate, t
265ea 68 65 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  he.    ** sqlite
265eb 33 5f 75 73 65 72 5f 64 61 74 61 28 29 20 66 75  3_user_data() fu
265ec 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 28  nction returns (
265ed 76 6f 69 64 20 2a 29 2d 31 2e 20 46 6f 72 20 6d  void *)-1. For m
265ee 69 6e 28 29 20 69 74 0a 20 20 20 20 2a 2a 20 72  in() it.    ** r
265ef 65 74 75 72 6e 73 20 28 76 6f 69 64 20 2a 29 64  eturns (void *)d
265f0 62 2c 20 77 68 65 72 65 20 64 62 20 69 73 20 74  b, where db is t
265f1 68 65 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61  he sqlite3* data
265f2 62 61 73 65 20 70 6f 69 6e 74 65 72 2e 0a 20 20  base pointer..  
265f3 20 20 2a 2a 20 54 68 65 72 65 66 6f 72 65 20 74    ** Therefore t
265f4 68 65 20 6e 65 78 74 20 73 74 61 74 65 6d 65 6e  he next statemen
265f5 74 20 73 65 74 73 20 76 61 72 69 61 62 6c 65 20  t sets variable 
265f6 27 6d 61 78 27 20 74 6f 20 31 20 66 6f 72 20 74  'max' to 1 for t
265f7 68 65 20 6d 61 78 28 29 0a 20 20 20 20 2a 2a 20  he max().    ** 
265f8 61 67 67 72 65 67 61 74 65 2c 20 6f 72 20 30 20  aggregate, or 0 
265f9 66 6f 72 20 6d 69 6e 28 29 2e 0a 20 20 20 20 2a  for min()..    *
265fa 2f 0a 20 20 20 20 6d 61 78 20 3d 20 73 71 6c 69  /.    max = sqli
265fb 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 63 6f  te3_user_data(co
265fc 6e 74 65 78 74 29 21 3d 30 3b 0a 20 20 20 20 63  ntext)!=0;.    c
265fd 6d 70 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43  mp = sqlite3MemC
265fe 6f 6d 70 61 72 65 28 70 42 65 73 74 2c 20 70 41  ompare(pBest, pA
265ff 72 67 2c 20 70 43 6f 6c 6c 29 3b 0a 20 20 20 20  rg, pColl);.    
26600 69 66 28 20 28 6d 61 78 20 26 26 20 63 6d 70 3c  if( (max && cmp<
26601 30 29 20 7c 7c 20 28 21 6d 61 78 20 26 26 20 63  0) || (!max && c
26602 6d 70 3e 30 29 20 29 7b 0a 20 20 20 20 20 20 73  mp>0) ){.      s
26603 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70  qlite3VdbeMemCop
26604 79 28 70 42 65 73 74 2c 20 70 41 72 67 29 3b 0a  y(pBest, pArg);.
26605 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
26606 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
26607 6d 43 6f 70 79 28 70 42 65 73 74 2c 20 70 41 72  mCopy(pBest, pAr
26608 67 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63  g);.  }.}.static
26609 20 76 6f 69 64 20 6d 69 6e 4d 61 78 46 69 6e 61   void minMaxFina
2660a 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e  lize(sqlite3_con
2660b 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a  text *context){.
2660c 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
2660d 2a 70 52 65 73 3b 0a 20 20 70 52 65 73 20 3d 20  *pRes;.  pRes = 
2660e 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  (sqlite3_value *
2660f 29 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61  )sqlite3_aggrega
26610 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65  te_context(conte
26611 78 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 52  xt, 0);.  if( pR
26612 65 73 20 29 7b 0a 20 20 20 20 69 66 28 20 41 4c  es ){.    if( AL
26613 57 41 59 53 28 70 52 65 73 2d 3e 66 6c 61 67 73  WAYS(pRes->flags
26614 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ){.      sqlit
26615 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28  e3_result_value(
26616 63 6f 6e 74 65 78 74 2c 20 70 52 65 73 29 3b 0a  context, pRes);.
26617 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
26618 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
26619 70 52 65 73 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  pRes);.  }.}../*
2661a 0a 2a 2a 20 67 72 6f 75 70 5f 63 6f 6e 63 61 74  .** group_concat
2661b 28 45 58 50 52 2c 20 3f 53 45 50 41 52 41 54 4f  (EXPR, ?SEPARATO
2661c 52 3f 29 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  R?).*/.static vo
2661d 69 64 20 67 72 6f 75 70 43 6f 6e 63 61 74 53 74  id groupConcatSt
2661e 65 70 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  ep(.  sqlite3_co
2661f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
26620 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
26621 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
26622 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68  gv.){.  const ch
26623 61 72 20 2a 7a 56 61 6c 3b 0a 20 20 53 74 72 41  ar *zVal;.  StrA
26624 63 63 75 6d 20 2a 70 41 63 63 75 6d 3b 0a 20 20  ccum *pAccum;.  
26625 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 65 70  const char *zSep
26626 3b 0a 20 20 69 6e 74 20 6e 56 61 6c 2c 20 6e 53  ;.  int nVal, nS
26627 65 70 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72  ep;.  assert( ar
26628 67 63 3d 3d 31 20 7c 7c 20 61 72 67 63 3d 3d 32  gc==1 || argc==2
26629 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65   );.  if( sqlite
2662a 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
2662b 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55  v[0])==SQLITE_NU
2662c 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70  LL ) return;.  p
2662d 41 63 63 75 6d 20 3d 20 28 53 74 72 41 63 63 75  Accum = (StrAccu
2662e 6d 2a 29 73 71 6c 69 74 65 33 5f 61 67 67 72 65  m*)sqlite3_aggre
2662f 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e  gate_context(con
26630 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70 41  text, sizeof(*pA
26631 63 63 75 6d 29 29 3b 0a 0a 20 20 69 66 28 20 70  ccum));..  if( p
26632 41 63 63 75 6d 20 29 7b 0a 20 20 20 20 73 71 6c  Accum ){.    sql
26633 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74  ite3 *db = sqlit
26634 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
26635 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ndle(context);. 
26636 20 20 20 69 6e 74 20 66 69 72 73 74 54 65 72 6d     int firstTerm
26637 20 3d 20 70 41 63 63 75 6d 2d 3e 75 73 65 4d 61   = pAccum->useMa
26638 6c 6c 6f 63 3d 3d 30 3b 0a 20 20 20 20 70 41 63  lloc==0;.    pAc
26639 63 75 6d 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20 3d  cum->useMalloc =
2663a 20 31 3b 0a 20 20 20 20 70 41 63 63 75 6d 2d 3e   1;.    pAccum->
2663b 6d 78 41 6c 6c 6f 63 20 3d 20 64 62 2d 3e 61 4c  mxAlloc = db->aL
2663c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
2663d 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 20 20 69  T_LENGTH];.    i
2663e 66 28 20 21 66 69 72 73 74 54 65 72 6d 20 29 7b  f( !firstTerm ){
2663f 0a 20 20 20 20 20 20 69 66 28 20 61 72 67 63 3d  .      if( argc=
26640 3d 32 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 53  =2 ){.        zS
26641 65 70 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69  ep = (char*)sqli
26642 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
26643 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20  rgv[1]);.       
26644 20 6e 53 65 70 20 3d 20 73 71 6c 69 74 65 33 5f   nSep = sqlite3_
26645 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
26646 5b 31 5d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  [1]);.      }els
26647 65 7b 0a 20 20 20 20 20 20 20 20 7a 53 65 70 20  e{.        zSep 
26648 3d 20 22 2c 22 3b 0a 20 20 20 20 20 20 20 20 6e  = ",";.        n
26649 53 65 70 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  Sep = 1;.      }
2664a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74  .      sqlite3St
2664b 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63  rAccumAppend(pAc
2664c 63 75 6d 2c 20 7a 53 65 70 2c 20 6e 53 65 70 29  cum, zSep, nSep)
2664d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 56 61 6c  ;.    }.    zVal
2664e 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65   = (char*)sqlite
2664f 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
26650 76 5b 30 5d 29 3b 0a 20 20 20 20 6e 56 61 6c 20  v[0]);.    nVal 
26651 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
26652 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a  bytes(argv[0]);.
26653 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
26654 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d  cumAppend(pAccum
26655 2c 20 7a 56 61 6c 2c 20 6e 56 61 6c 29 3b 0a 20  , zVal, nVal);. 
26656 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64   }.}.static void
26657 20 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61   groupConcatFina
26658 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e  lize(sqlite3_con
26659 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a  text *context){.
2665a 20 20 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63    StrAccum *pAcc
2665b 75 6d 3b 0a 20 20 70 41 63 63 75 6d 20 3d 20 73  um;.  pAccum = s
2665c 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
2665d 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74  _context(context
2665e 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 41 63 63  , 0);.  if( pAcc
2665f 75 6d 20 29 7b 0a 20 20 20 20 69 66 28 20 70 41  um ){.    if( pA
26660 63 63 75 6d 2d 3e 74 6f 6f 42 69 67 20 29 7b 0a  ccum->tooBig ){.
26661 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
26662 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69  sult_error_toobi
26663 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  g(context);.    
26664 7d 65 6c 73 65 20 69 66 28 20 70 41 63 63 75 6d  }else if( pAccum
26665 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
26666 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
26667 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
26668 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  em(context);.   
26669 20 7d 65 6c 73 65 7b 20 20 20 20 0a 20 20 20 20   }else{    .    
2666a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
2666b 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73  _text(context, s
2666c 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69  qlite3StrAccumFi
2666d 6e 69 73 68 28 70 41 63 63 75 6d 29 2c 20 2d 31  nish(pAccum), -1
2666e 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
2666f 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
26670 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 20 20  ite3_free);.    
26671 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  }.  }.}../*.** T
26672 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 67  his function reg
26673 69 73 74 65 72 65 64 20 61 6c 6c 20 6f 66 20 74  istered all of t
26674 68 65 20 61 62 6f 76 65 20 43 20 66 75 6e 63 74  he above C funct
26675 69 6f 6e 73 20 61 73 20 53 51 4c 0a 2a 2a 20 66  ions as SQL.** f
26676 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20  unctions.  This 
26677 73 68 6f 75 6c 64 20 62 65 20 74 68 65 20 6f 6e  should be the on
26678 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68  ly routine in th
26679 69 73 20 66 69 6c 65 20 77 69 74 68 0a 2a 2a 20  is file with.** 
2667a 65 78 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65  external linkage
2667b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2667c 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2667d 52 65 67 69 73 74 65 72 42 75 69 6c 74 69 6e 46  RegisterBuiltinF
2667e 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33  unctions(sqlite3
2667f 20 2a 64 62 29 7b 0a 23 69 66 6e 64 65 66 20 53   *db){.#ifndef S
26680 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52  QLITE_OMIT_ALTER
26681 54 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 41  TABLE.  sqlite3A
26682 6c 74 65 72 46 75 6e 63 74 69 6f 6e 73 28 64 62  lterFunctions(db
26683 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20  );.#endif.  if( 
26684 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
26685 64 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20  d ){.    int rc 
26686 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f  = sqlite3_overlo
26687 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20  ad_function(db, 
26688 22 4d 41 54 43 48 22 2c 20 32 29 3b 0a 20 20 20  "MATCH", 2);.   
26689 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
2668a 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d  ITE_NOMEM || rc=
2668b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
2668c 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
2668d 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20  _NOMEM ){.      
2668e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2668f 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 1;.    }.  }.
26690 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
26691 20 4c 49 4b 45 4f 50 54 20 66 6c 61 67 20 6f 6e   LIKEOPT flag on
26692 20 74 68 65 20 32 2d 61 72 67 75 6d 65 6e 74 20   the 2-argument 
26693 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68  function with th
26694 65 20 67 69 76 65 6e 20 6e 61 6d 65 2e 0a 2a 2f  e given name..*/
26695 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74  .static void set
26696 4c 69 6b 65 4f 70 74 46 6c 61 67 28 73 71 6c 69  LikeOptFlag(sqli
26697 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
26698 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 75 38 20 66  har *zName, u8 f
26699 6c 61 67 56 61 6c 29 7b 0a 20 20 46 75 6e 63 44  lagVal){.  FuncD
2669a 65 66 20 2a 70 44 65 66 3b 0a 20 20 70 44 65 66  ef *pDef;.  pDef
2669b 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75   = sqlite3FindFu
2669c 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65  nction(db, zName
2669d 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  , sqlite3Strlen3
2669e 30 28 7a 4e 61 6d 65 29 2c 0a 20 20 20 20 20 20  0(zName),.      
2669f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
266a0 20 20 20 20 20 20 20 32 2c 20 53 51 4c 49 54 45         2, SQLITE
266a1 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20 69 66 28  _UTF8, 0);.  if(
266a2 20 41 4c 57 41 59 53 28 70 44 65 66 29 20 29 7b   ALWAYS(pDef) ){
266a3 0a 20 20 20 20 70 44 65 66 2d 3e 66 6c 61 67 73  .    pDef->flags
266a4 20 3d 20 66 6c 61 67 56 61 6c 3b 0a 20 20 7d 0a   = flagVal;.  }.
266a5 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65  }../*.** Registe
266a6 72 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 4c  r the built-in L
266a7 49 4b 45 20 61 6e 64 20 47 4c 4f 42 20 66 75 6e  IKE and GLOB fun
266a8 63 74 69 6f 6e 73 2e 20 20 54 68 65 20 63 61 73  ctions.  The cas
266a9 65 53 65 6e 73 69 74 69 76 65 0a 2a 2a 20 70 61  eSensitive.** pa
266aa 72 61 6d 65 74 65 72 20 64 65 74 65 72 6d 69 6e  rameter determin
266ab 65 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  es whether or no
266ac 74 20 74 68 65 20 4c 49 4b 45 20 6f 70 65 72 61  t the LIKE opera
266ad 74 6f 72 20 69 73 20 63 61 73 65 0a 2a 2a 20 73  tor is case.** s
266ae 65 6e 73 69 74 69 76 65 2e 20 20 47 4c 4f 42 20  ensitive.  GLOB 
266af 69 73 20 61 6c 77 61 79 73 20 63 61 73 65 20 73  is always case s
266b0 65 6e 73 69 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c  ensitive..*/.SQL
266b1 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
266b2 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
266b3 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73 28 73 71  LikeFunctions(sq
266b4 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 63  lite3 *db, int c
266b5 61 73 65 53 65 6e 73 69 74 69 76 65 29 7b 0a 20  aseSensitive){. 
266b6 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49   struct compareI
266b7 6e 66 6f 20 2a 70 49 6e 66 6f 3b 0a 20 20 69 66  nfo *pInfo;.  if
266b8 28 20 63 61 73 65 53 65 6e 73 69 74 69 76 65 20  ( caseSensitive 
266b9 29 7b 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20 28  ){.    pInfo = (
266ba 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e  struct compareIn
266bb 66 6f 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74  fo*)&likeInfoAlt
266bc 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
266bd 49 6e 66 6f 20 3d 20 28 73 74 72 75 63 74 20 63  Info = (struct c
266be 6f 6d 70 61 72 65 49 6e 66 6f 2a 29 26 6c 69 6b  ompareInfo*)&lik
266bf 65 49 6e 66 6f 4e 6f 72 6d 3b 0a 20 20 7d 0a 20  eInfoNorm;.  }. 
266c0 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75   sqlite3CreateFu
266c1 6e 63 28 64 62 2c 20 22 6c 69 6b 65 22 2c 20 32  nc(db, "like", 2
266c2 2c 20 53 51 4c 49 54 45 5f 41 4e 59 2c 20 70 49  , SQLITE_ANY, pI
266c3 6e 66 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30  nfo, likeFunc, 0
266c4 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 43  , 0);.  sqlite3C
266c5 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 22 6c  reateFunc(db, "l
266c6 69 6b 65 22 2c 20 33 2c 20 53 51 4c 49 54 45 5f  ike", 3, SQLITE_
266c7 41 4e 59 2c 20 70 49 6e 66 6f 2c 20 6c 69 6b 65  ANY, pInfo, like
266c8 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 20 73  Func, 0, 0);.  s
266c9 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63  qlite3CreateFunc
266ca 28 64 62 2c 20 22 67 6c 6f 62 22 2c 20 32 2c 20  (db, "glob", 2, 
266cb 53 51 4c 49 54 45 5f 41 4e 59 2c 20 0a 20 20 20  SQLITE_ANY, .   
266cc 20 20 20 28 73 74 72 75 63 74 20 63 6f 6d 70 61     (struct compa
266cd 72 65 49 6e 66 6f 2a 29 26 67 6c 6f 62 49 6e 66  reInfo*)&globInf
266ce 6f 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 30  o, likeFunc, 0,0
266cf 29 3b 0a 20 20 73 65 74 4c 69 6b 65 4f 70 74 46  );.  setLikeOptF
266d0 6c 61 67 28 64 62 2c 20 22 67 6c 6f 62 22 2c 20  lag(db, "glob", 
266d1 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45  SQLITE_FUNC_LIKE
266d2 20 7c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43   | SQLITE_FUNC_C
266d3 41 53 45 29 3b 0a 20 20 73 65 74 4c 69 6b 65 4f  ASE);.  setLikeO
266d4 70 74 46 6c 61 67 28 64 62 2c 20 22 6c 69 6b 65  ptFlag(db, "like
266d5 22 2c 20 0a 20 20 20 20 20 20 63 61 73 65 53 65  ", .      caseSe
266d6 6e 73 69 74 69 76 65 20 3f 20 28 53 51 4c 49 54  nsitive ? (SQLIT
266d7 45 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c 20 53 51  E_FUNC_LIKE | SQ
266d8 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 20  LITE_FUNC_CASE) 
266d9 3a 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49  : SQLITE_FUNC_LI
266da 4b 45 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45  KE);.}../*.** pE
266db 78 70 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e  xpr points to an
266dc 20 65 78 70 72 65 73 73 69 6f 6e 20 77 68 69 63   expression whic
266dd 68 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 66  h implements a f
266de 75 6e 63 74 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20  unction.  If.** 
266df 69 74 20 69 73 20 61 70 70 72 6f 70 72 69 61 74  it is appropriat
266e0 65 20 74 6f 20 61 70 70 6c 79 20 74 68 65 20 4c  e to apply the L
266e1 49 4b 45 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  IKE optimization
266e2 20 74 6f 20 74 68 61 74 20 66 75 6e 63 74 69 6f   to that functio
266e3 6e 0a 2a 2a 20 74 68 65 6e 20 73 65 74 20 61 57  n.** then set aW
266e4 63 5b 30 5d 20 74 68 72 6f 75 67 68 20 61 57 63  c[0] through aWc
266e5 5b 32 5d 20 74 6f 20 74 68 65 20 77 69 6c 64 63  [2] to the wildc
266e6 61 72 64 20 63 68 61 72 61 63 74 65 72 73 20 61  ard characters a
266e7 6e 64 0a 2a 2a 20 72 65 74 75 72 6e 20 54 52 55  nd.** return TRU
266e8 45 2e 20 20 49 66 20 74 68 65 20 66 75 6e 63 74  E.  If the funct
266e9 69 6f 6e 20 69 73 20 6e 6f 74 20 61 20 4c 49 4b  ion is not a LIK
266ea 45 2d 73 74 79 6c 65 20 66 75 6e 63 74 69 6f 6e  E-style function
266eb 20 74 68 65 6e 0a 2a 2a 20 72 65 74 75 72 6e 20   then.** return 
266ec 46 41 4c 53 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45  FALSE..*/.SQLITE
266ed 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
266ee 69 74 65 33 49 73 4c 69 6b 65 46 75 6e 63 74 69  ite3IsLikeFuncti
266ef 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  on(sqlite3 *db, 
266f0 45 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74  Expr *pExpr, int
266f1 20 2a 70 49 73 4e 6f 63 61 73 65 2c 20 63 68 61   *pIsNocase, cha
266f2 72 20 2a 61 57 63 29 7b 0a 20 20 46 75 6e 63 44  r *aWc){.  FuncD
266f3 65 66 20 2a 70 44 65 66 3b 0a 20 20 69 66 28 20  ef *pDef;.  if( 
266f4 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46 55  pExpr->op!=TK_FU
266f5 4e 43 54 49 4f 4e 20 0a 20 20 20 7c 7c 20 21 70  NCTION .   || !p
266f6 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 20 0a 20  Expr->x.pList . 
266f7 20 20 7c 7c 20 70 45 78 70 72 2d 3e 78 2e 70 4c    || pExpr->x.pL
266f8 69 73 74 2d 3e 6e 45 78 70 72 21 3d 32 0a 20 20  ist->nExpr!=2.  
266f9 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
266fa 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21  .  }.  assert( !
266fb 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
266fc 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
266fd 65 63 74 29 20 29 3b 0a 20 20 70 44 65 66 20 3d  ect) );.  pDef =
266fe 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63   sqlite3FindFunc
266ff 74 69 6f 6e 28 64 62 2c 20 70 45 78 70 72 2d 3e  tion(db, pExpr->
26700 75 2e 7a 54 6f 6b 65 6e 2c 20 0a 20 20 20 20 20  u.zToken, .     
26701 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26702 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53          sqlite3S
26703 74 72 6c 65 6e 33 30 28 70 45 78 70 72 2d 3e 75  trlen30(pExpr->u
26704 2e 7a 54 6f 6b 65 6e 29 2c 0a 20 20 20 20 20 20  .zToken),.      
26705 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26706 20 20 20 20 20 20 20 32 2c 20 53 51 4c 49 54 45         2, SQLITE
26707 5f 55 54 46 38 2c 20 30 29 3b 0a 20 20 69 66 28  _UTF8, 0);.  if(
26708 20 4e 45 56 45 52 28 70 44 65 66 3d 3d 30 29 20   NEVER(pDef==0) 
26709 7c 7c 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20  || (pDef->flags 
2670a 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49  & SQLITE_FUNC_LI
2670b 4b 45 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  KE)==0 ){.    re
2670c 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 0;.  }..  /
2670d 2a 20 54 68 65 20 6d 65 6d 63 70 79 28 29 20 73  * The memcpy() s
2670e 74 61 74 65 6d 65 6e 74 20 61 73 73 75 6d 65 73  tatement assumes
2670f 20 74 68 61 74 20 74 68 65 20 77 69 6c 64 63 61   that the wildca
26710 72 64 20 63 68 61 72 61 63 74 65 72 73 20 61 72  rd characters ar
26711 65 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73 74  e.  ** the first
26712 20 74 68 72 65 65 20 73 74 61 74 65 6d 65 6e 74   three statement
26713 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 61 72 65  s in the compare
26714 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20  Info structure. 
26715 20 54 68 65 0a 20 20 2a 2a 20 61 73 73 65 72 74   The.  ** assert
26716 73 28 29 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20  s() that follow 
26717 76 65 72 69 66 79 20 74 68 61 74 20 61 73 73 75  verify that assu
26718 6d 70 74 69 6f 6e 0a 20 20 2a 2f 0a 20 20 6d 65  mption.  */.  me
26719 6d 63 70 79 28 61 57 63 2c 20 70 44 65 66 2d 3e  mcpy(aWc, pDef->
2671a 70 55 73 65 72 44 61 74 61 2c 20 33 29 3b 0a 20  pUserData, 3);. 
2671b 20 61 73 73 65 72 74 28 20 28 63 68 61 72 2a 29   assert( (char*)
2671c 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 20 3d 3d 20  &likeInfoAlt == 
2671d 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f  (char*)&likeInfo
2671e 41 6c 74 2e 6d 61 74 63 68 41 6c 6c 20 29 3b 0a  Alt.matchAll );.
2671f 20 20 61 73 73 65 72 74 28 20 26 28 28 63 68 61    assert( &((cha
26720 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 29  r*)&likeInfoAlt)
26721 5b 31 5d 20 3d 3d 20 28 63 68 61 72 2a 29 26 6c  [1] == (char*)&l
26722 69 6b 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 63 68  ikeInfoAlt.match
26723 4f 6e 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28  One );.  assert(
26724 20 26 28 28 63 68 61 72 2a 29 26 6c 69 6b 65 49   &((char*)&likeI
26725 6e 66 6f 41 6c 74 29 5b 32 5d 20 3d 3d 20 28 63  nfoAlt)[2] == (c
26726 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c  har*)&likeInfoAl
26727 74 2e 6d 61 74 63 68 53 65 74 20 29 3b 0a 20 20  t.matchSet );.  
26728 2a 70 49 73 4e 6f 63 61 73 65 20 3d 20 28 70 44  *pIsNocase = (pD
26729 65 66 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  ef->flags & SQLI
2672a 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 3d 3d 30  TE_FUNC_CASE)==0
2672b 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  ;.  return 1;.}.
2672c 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 61 6c 6c 20 6f  ./*.** All all o
2672d 66 20 74 68 65 20 46 75 6e 63 44 65 66 20 73 74  f the FuncDef st
2672e 72 75 63 74 75 72 65 73 20 69 6e 20 74 68 65 20  ructures in the 
2672f 61 42 75 69 6c 74 69 6e 46 75 6e 63 5b 5d 20 61  aBuiltinFunc[] a
26730 72 72 61 79 20 61 62 6f 76 65 0a 2a 2a 20 74 6f  rray above.** to
26731 20 74 68 65 20 67 6c 6f 62 61 6c 20 66 75 6e 63   the global func
26732 74 69 6f 6e 20 68 61 73 68 20 74 61 62 6c 65 2e  tion hash table.
26733 20 20 54 68 69 73 20 6f 63 63 75 72 73 20 61 74    This occurs at
26734 20 73 74 61 72 74 2d 74 69 6d 65 20 28 61 73 0a   start-time (as.
26735 2a 2a 20 61 20 63 6f 6e 73 65 71 75 65 6e 63 65  ** a consequence
26736 20 6f 66 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69   of calling sqli
26737 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
26738 29 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 74  )..**.** After t
26739 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73  his routine runs
2673a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2673b 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
2673c 65 67 69 73 74 65 72 47 6c 6f 62 61 6c 46 75 6e  egisterGlobalFun
2673d 63 74 69 6f 6e 73 28 76 6f 69 64 29 7b 0a 20 20  ctions(void){.  
2673e 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 66 6f 6c 6c  /*.  ** The foll
2673f 6f 77 69 6e 67 20 61 72 72 61 79 20 68 6f 6c 64  owing array hold
26740 73 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74  s FuncDef struct
26741 75 72 65 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20  ures for all of 
26742 74 68 65 20 66 75 6e 63 74 69 6f 6e 73 0a 20 20  the functions.  
26743 2a 2a 20 64 65 66 69 6e 65 64 20 69 6e 20 74 68  ** defined in th
26744 69 73 20 66 69 6c 65 2e 0a 20 20 2a 2a 0a 20 20  is file..  **.  
26745 2a 2a 20 54 68 65 20 61 72 72 61 79 20 63 61 6e  ** The array can
26746 6e 6f 74 20 62 65 20 63 6f 6e 73 74 61 6e 74 20  not be constant 
26747 73 69 6e 63 65 20 63 68 61 6e 67 65 73 20 61 72  since changes ar
26748 65 20 6d 61 64 65 20 74 6f 20 74 68 65 0a 20 20  e made to the.  
26749 2a 2a 20 46 75 6e 63 44 65 66 2e 70 48 61 73 68  ** FuncDef.pHash
2674a 20 65 6c 65 6d 65 6e 74 73 20 61 74 20 73 74 61   elements at sta
2674b 72 74 2d 74 69 6d 65 2e 20 20 54 68 65 20 65 6c  rt-time.  The el
2674c 65 6d 65 6e 74 73 20 6f 66 20 74 68 69 73 20 61  ements of this a
2674d 72 72 61 79 0a 20 20 2a 2a 20 61 72 65 20 72 65  rray.  ** are re
2674e 61 64 2d 6f 6e 6c 79 20 61 66 74 65 72 20 69 6e  ad-only after in
2674f 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 69 73 20  itialization is 
26750 63 6f 6d 70 6c 65 74 65 2e 0a 20 20 2a 2f 0a 20  complete..  */. 
26751 20 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57   static SQLITE_W
26752 53 44 20 46 75 6e 63 44 65 66 20 61 42 75 69 6c  SD FuncDef aBuil
26753 74 69 6e 46 75 6e 63 5b 5d 20 3d 20 7b 0a 20 20  tinFunc[] = {.  
26754 20 20 46 55 4e 43 54 49 4f 4e 28 6c 74 72 69 6d    FUNCTION(ltrim
26755 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31  ,              1
26756 2c 20 31 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63  , 1, 0, trimFunc
26757 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20           ),.    
26758 46 55 4e 43 54 49 4f 4e 28 6c 74 72 69 6d 2c 20  FUNCTION(ltrim, 
26759 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20               2, 
2675a 31 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20  1, 0, trimFunc  
2675b 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
2675c 4e 43 54 49 4f 4e 28 72 74 72 69 6d 2c 20 20 20  NCTION(rtrim,   
2675d 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 32 2c             1, 2,
2675e 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20   0, trimFunc    
2675f 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
26760 54 49 4f 4e 28 72 74 72 69 6d 2c 20 20 20 20 20  TION(rtrim,     
26761 20 20 20 20 20 20 20 20 20 32 2c 20 32 2c 20 30           2, 2, 0
26762 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20  , trimFunc      
26763 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
26764 4f 4e 28 74 72 69 6d 2c 20 20 20 20 20 20 20 20  ON(trim,        
26765 20 20 20 20 20 20 20 31 2c 20 33 2c 20 30 2c 20         1, 3, 0, 
26766 74 72 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20  trimFunc        
26767 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
26768 28 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20  (trim,          
26769 20 20 20 20 20 32 2c 20 33 2c 20 30 2c 20 74 72       2, 3, 0, tr
2676a 69 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 29  imFunc         )
2676b 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d  ,.    FUNCTION(m
2676c 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  in,             
2676d 20 20 2d 31 2c 20 30 2c 20 31 2c 20 6d 69 6e 6d    -1, 0, 1, minm
2676e 61 78 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a  axFunc       ),.
2676f 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 69 6e      FUNCTION(min
26770 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
26771 20 30 2c 20 30 2c 20 31 2c 20 30 20 20 20 20 20   0, 0, 1, 0     
26772 20 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20             ),.  
26773 20 20 41 47 47 52 45 47 41 54 45 28 6d 69 6e 2c    AGGREGATE(min,
26774 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
26775 2c 20 30 2c 20 31 2c 20 6d 69 6e 6d 61 78 53 74  , 0, 1, minmaxSt
26776 65 70 2c 20 20 20 20 20 20 6d 69 6e 4d 61 78 46  ep,      minMaxF
26777 69 6e 61 6c 69 7a 65 20 29 2c 0a 20 20 20 20 46  inalize ),.    F
26778 55 4e 43 54 49 4f 4e 28 6d 61 78 2c 20 20 20 20  UNCTION(max,    
26779 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 31             -1, 1
2677a 2c 20 31 2c 20 6d 69 6e 6d 61 78 46 75 6e 63 20  , 1, minmaxFunc 
2677b 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
2677c 43 54 49 4f 4e 28 6d 61 78 2c 20 20 20 20 20 20  CTION(max,      
2677d 20 20 20 20 20 20 20 20 20 20 30 2c 20 31 2c 20            0, 1, 
2677e 31 2c 20 30 20 20 20 20 20 20 20 20 20 20 20 20  1, 0            
2677f 20 20 20 20 29 2c 0a 20 20 20 20 41 47 47 52 45      ),.    AGGRE
26780 47 41 54 45 28 6d 61 78 2c 20 20 20 20 20 20 20  GATE(max,       
26781 20 20 20 20 20 20 20 20 31 2c 20 31 2c 20 31 2c          1, 1, 1,
26782 20 6d 69 6e 6d 61 78 53 74 65 70 2c 20 20 20 20   minmaxStep,    
26783 20 20 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65    minMaxFinalize
26784 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
26785 28 74 79 70 65 6f 66 2c 20 20 20 20 20 20 20 20  (typeof,        
26786 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 74 79       1, 0, 0, ty
26787 70 65 6f 66 46 75 6e 63 20 20 20 20 20 20 20 29  peofFunc       )
26788 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c  ,.    FUNCTION(l
26789 65 6e 67 74 68 2c 20 20 20 20 20 20 20 20 20 20  ength,          
2678a 20 20 20 31 2c 20 30 2c 20 30 2c 20 6c 65 6e 67     1, 0, 0, leng
2678b 74 68 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a  thFunc       ),.
2678c 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 75 62      FUNCTION(sub
2678d 73 74 72 2c 20 20 20 20 20 20 20 20 20 20 20 20  str,            
2678e 20 32 2c 20 30 2c 20 30 2c 20 73 75 62 73 74 72   2, 0, 0, substr
2678f 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20  Func       ),.  
26790 20 20 46 55 4e 43 54 49 4f 4e 28 73 75 62 73 74    FUNCTION(subst
26791 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 33  r,             3
26792 2c 20 30 2c 20 30 2c 20 73 75 62 73 74 72 46 75  , 0, 0, substrFu
26793 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20  nc       ),.    
26794 46 55 4e 43 54 49 4f 4e 28 61 62 73 2c 20 20 20  FUNCTION(abs,   
26795 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
26796 30 2c 20 30 2c 20 61 62 73 46 75 6e 63 20 20 20  0, 0, absFunc   
26797 20 20 20 20 20 20 20 29 2c 0a 23 69 66 6e 64 65         ),.#ifnde
26798 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
26799 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20  OATING_POINT.   
2679a 20 46 55 4e 43 54 49 4f 4e 28 72 6f 75 6e 64 2c   FUNCTION(round,
2679b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c                1,
2679c 20 30 2c 20 30 2c 20 72 6f 75 6e 64 46 75 6e 63   0, 0, roundFunc
2679d 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46          ),.    F
2679e 55 4e 43 54 49 4f 4e 28 72 6f 75 6e 64 2c 20 20  UNCTION(round,  
2679f 20 20 20 20 20 20 20 20 20 20 20 20 32 2c 20 30              2, 0
267a0 2c 20 30 2c 20 72 6f 75 6e 64 46 75 6e 63 20 20  , 0, roundFunc  
267a1 20 20 20 20 20 20 29 2c 0a 23 65 6e 64 69 66 0a        ),.#endif.
267a2 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 75 70 70      FUNCTION(upp
267a3 65 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  er,             
267a4 20 31 2c 20 30 2c 20 30 2c 20 75 70 70 65 72 46   1, 0, 0, upperF
267a5 75 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20  unc        ),.  
267a6 20 20 46 55 4e 43 54 49 4f 4e 28 6c 6f 77 65 72    FUNCTION(lower
267a7 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31  ,              1
267a8 2c 20 30 2c 20 30 2c 20 6c 6f 77 65 72 46 75 6e  , 0, 0, lowerFun
267a9 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20  c        ),.    
267aa 46 55 4e 43 54 49 4f 4e 28 63 6f 61 6c 65 73 63  FUNCTION(coalesc
267ab 65 2c 20 20 20 20 20 20 20 20 20 20 20 31 2c 20  e,           1, 
267ac 30 2c 20 30 2c 20 30 20 20 20 20 20 20 20 20 20  0, 0, 0         
267ad 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55         ),.    FU
267ae 4e 43 54 49 4f 4e 28 63 6f 61 6c 65 73 63 65 2c  NCTION(coalesce,
267af 20 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30 2c            -1, 0,
267b0 20 30 2c 20 69 66 6e 75 6c 6c 46 75 6e 63 20 20   0, ifnullFunc  
267b1 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
267b2 54 49 4f 4e 28 63 6f 61 6c 65 73 63 65 2c 20 20  TION(coalesce,  
267b3 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c 20 30           0, 0, 0
267b4 2c 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20  , 0             
267b5 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
267b6 4f 4e 28 68 65 78 2c 20 20 20 20 20 20 20 20 20  ON(hex,         
267b7 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20         1, 0, 0, 
267b8 68 65 78 46 75 6e 63 20 20 20 20 20 20 20 20 20  hexFunc         
267b9 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e   ),.    FUNCTION
267ba 28 69 66 6e 75 6c 6c 2c 20 20 20 20 20 20 20 20  (ifnull,        
267bb 20 20 20 20 20 32 2c 20 30 2c 20 31 2c 20 69 66       2, 0, 1, if
267bc 6e 75 6c 6c 46 75 6e 63 20 20 20 20 20 20 20 29  nullFunc       )
267bd 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 72  ,.    FUNCTION(r
267be 61 6e 64 6f 6d 2c 20 20 20 20 20 20 20 20 20 20  andom,          
267bf 20 20 20 30 2c 20 30 2c 20 30 2c 20 72 61 6e 64     0, 0, 0, rand
267c0 6f 6d 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a  omFunc       ),.
267c1 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 61 6e      FUNCTION(ran
267c2 64 6f 6d 62 6c 6f 62 2c 20 20 20 20 20 20 20 20  domblob,        
267c3 20 31 2c 20 30 2c 20 30 2c 20 72 61 6e 64 6f 6d   1, 0, 0, random
267c4 42 6c 6f 62 20 20 20 20 20 20 20 29 2c 0a 20 20  Blob       ),.  
267c5 20 20 46 55 4e 43 54 49 4f 4e 28 6e 75 6c 6c 69    FUNCTION(nulli
267c6 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 32  f,             2
267c7 2c 20 30 2c 20 31 2c 20 6e 75 6c 6c 69 66 46 75  , 0, 1, nullifFu
267c8 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20  nc       ),.    
267c9 46 55 4e 43 54 49 4f 4e 28 73 71 6c 69 74 65 5f  FUNCTION(sqlite_
267ca 76 65 72 73 69 6f 6e 2c 20 20 20 20 20 30 2c 20  version,     0, 
267cb 30 2c 20 30 2c 20 76 65 72 73 69 6f 6e 46 75 6e  0, 0, versionFun
267cc 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55  c      ),.    FU
267cd 4e 43 54 49 4f 4e 28 73 71 6c 69 74 65 5f 73 6f  NCTION(sqlite_so
267ce 75 72 63 65 5f 69 64 2c 20 20 20 30 2c 20 30 2c  urce_id,   0, 0,
267cf 20 30 2c 20 73 6f 75 72 63 65 69 64 46 75 6e 63   0, sourceidFunc
267d0 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43       ),.    FUNC
267d1 54 49 4f 4e 28 71 75 6f 74 65 2c 20 20 20 20 20  TION(quote,     
267d2 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30           1, 0, 0
267d3 2c 20 71 75 6f 74 65 46 75 6e 63 20 20 20 20 20  , quoteFunc     
267d4 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49     ),.    FUNCTI
267d5 4f 4e 28 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  ON(last_insert_r
267d6 6f 77 69 64 2c 20 20 30 2c 20 30 2c 20 30 2c 20  owid,  0, 0, 0, 
267d7 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
267d8 64 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e  d),.    FUNCTION
267d9 28 63 68 61 6e 67 65 73 2c 20 20 20 20 20 20 20  (changes,       
267da 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 68       0, 0, 0, ch
267db 61 6e 67 65 73 20 20 20 20 20 20 20 20 20 20 29  anges          )
267dc 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 74  ,.    FUNCTION(t
267dd 6f 74 61 6c 5f 63 68 61 6e 67 65 73 2c 20 20 20  otal_changes,   
267de 20 20 20 30 2c 20 30 2c 20 30 2c 20 74 6f 74 61     0, 0, 0, tota
267df 6c 5f 63 68 61 6e 67 65 73 20 20 20 20 29 2c 0a  l_changes    ),.
267e0 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 65 70      FUNCTION(rep
267e1 6c 61 63 65 2c 20 20 20 20 20 20 20 20 20 20 20  lace,           
267e2 20 33 2c 20 30 2c 20 30 2c 20 72 65 70 6c 61 63   3, 0, 0, replac
267e3 65 46 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20  eFunc      ),.  
267e4 20 20 46 55 4e 43 54 49 4f 4e 28 7a 65 72 6f 62    FUNCTION(zerob
267e5 6c 6f 62 2c 20 20 20 20 20 20 20 20 20 20 20 31  lob,           1
267e6 2c 20 30 2c 20 30 2c 20 7a 65 72 6f 62 6c 6f 62  , 0, 0, zeroblob
267e7 46 75 6e 63 20 20 20 20 20 29 2c 0a 20 20 23 69  Func     ),.  #i
267e8 66 64 65 66 20 53 51 4c 49 54 45 5f 53 4f 55 4e  fdef SQLITE_SOUN
267e9 44 45 58 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e  DEX.    FUNCTION
267ea 28 73 6f 75 6e 64 65 78 2c 20 20 20 20 20 20 20  (soundex,       
267eb 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 73 6f       1, 0, 0, so
267ec 75 6e 64 65 78 46 75 6e 63 20 20 20 20 20 20 29  undexFunc      )
267ed 2c 0a 20 20 23 65 6e 64 69 66 0a 20 20 23 69 66  ,.  #endif.  #if
267ee 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
267ef 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a  _LOAD_EXTENSION.
267f0 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 6f 61      FUNCTION(loa
267f1 64 5f 65 78 74 65 6e 73 69 6f 6e 2c 20 20 20 20  d_extension,    
267f2 20 31 2c 20 30 2c 20 30 2c 20 6c 6f 61 64 45 78   1, 0, 0, loadEx
267f3 74 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20  t          ),.  
267f4 20 20 46 55 4e 43 54 49 4f 4e 28 6c 6f 61 64 5f    FUNCTION(load_
267f5 65 78 74 65 6e 73 69 6f 6e 2c 20 20 20 20 20 32  extension,     2
267f6 2c 20 30 2c 20 30 2c 20 6c 6f 61 64 45 78 74 20  , 0, 0, loadExt 
267f7 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 23 65           ),.  #e
267f8 6e 64 69 66 0a 20 20 20 20 41 47 47 52 45 47 41  ndif.    AGGREGA
267f9 54 45 28 73 75 6d 2c 20 20 20 20 20 20 20 20 20  TE(sum,         
267fa 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 73        1, 0, 0, s
267fb 75 6d 53 74 65 70 2c 20 20 20 20 20 20 20 20 20  umStep,         
267fc 73 75 6d 46 69 6e 61 6c 69 7a 65 20 20 20 20 29  sumFinalize    )
267fd 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 28  ,.    AGGREGATE(
267fe 74 6f 74 61 6c 2c 20 20 20 20 20 20 20 20 20 20  total,          
267ff 20 20 20 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53     1, 0, 0, sumS
26800 74 65 70 2c 20 20 20 20 20 20 20 20 20 74 6f 74  tep,         tot
26801 61 6c 46 69 6e 61 6c 69 7a 65 20 20 20 20 29 2c  alFinalize    ),
26802 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 28 61  .    AGGREGATE(a
26803 76 67 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  vg,             
26804 20 20 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74    1, 0, 0, sumSt
26805 65 70 2c 20 20 20 20 20 20 20 20 20 61 76 67 46  ep,         avgF
26806 69 6e 61 6c 69 7a 65 20 20 20 20 29 2c 0a 20 2f  inalize    ),. /
26807 2a 20 41 47 47 52 45 47 41 54 45 28 63 6f 75 6e  * AGGREGATE(coun
26808 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 30  t,             0
26809 2c 20 30 2c 20 30 2c 20 63 6f 75 6e 74 53 74 65  , 0, 0, countSte
2680a 70 2c 20 20 20 20 20 20 20 63 6f 75 6e 74 46 69  p,       countFi
2680b 6e 61 6c 69 7a 65 20 20 29 2c 20 2a 2f 0a 20 20  nalize  ), */.  
2680c 20 20 7b 30 2c 53 51 4c 49 54 45 5f 55 54 46 38    {0,SQLITE_UTF8
2680d 2c 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 4f 55  ,SQLITE_FUNC_COU
2680e 4e 54 2c 30 2c 30 2c 30 2c 63 6f 75 6e 74 53 74  NT,0,0,0,countSt
2680f 65 70 2c 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65  ep,countFinalize
26810 2c 22 63 6f 75 6e 74 22 2c 30 7d 2c 0a 20 20 20  ,"count",0},.   
26811 20 41 47 47 52 45 47 41 54 45 28 63 6f 75 6e 74   AGGREGATE(count
26812 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c  ,             1,
26813 20 30 2c 20 30 2c 20 63 6f 75 6e 74 53 74 65 70   0, 0, countStep
26814 2c 20 20 20 20 20 20 20 63 6f 75 6e 74 46 69 6e  ,       countFin
26815 61 6c 69 7a 65 20 20 29 2c 0a 20 20 20 20 41 47  alize  ),.    AG
26816 47 52 45 47 41 54 45 28 67 72 6f 75 70 5f 63 6f  GREGATE(group_co
26817 6e 63 61 74 2c 20 20 20 20 20 20 31 2c 20 30 2c  ncat,      1, 0,
26818 20 30 2c 20 67 72 6f 75 70 43 6f 6e 63 61 74 53   0, groupConcatS
26819 74 65 70 2c 20 67 72 6f 75 70 43 6f 6e 63 61 74  tep, groupConcat
2681a 46 69 6e 61 6c 69 7a 65 29 2c 0a 20 20 20 20 41  Finalize),.    A
2681b 47 47 52 45 47 41 54 45 28 67 72 6f 75 70 5f 63  GGREGATE(group_c
2681c 6f 6e 63 61 74 2c 20 20 20 20 20 20 32 2c 20 30  oncat,      2, 0
2681d 2c 20 30 2c 20 67 72 6f 75 70 43 6f 6e 63 61 74  , 0, groupConcat
2681e 53 74 65 70 2c 20 67 72 6f 75 70 43 6f 6e 63 61  Step, groupConca
2681f 74 46 69 6e 61 6c 69 7a 65 29 2c 0a 20 20 0a 20  tFinalize),.  . 
26820 20 20 20 4c 49 4b 45 46 55 4e 43 28 67 6c 6f 62     LIKEFUNC(glob
26821 2c 20 32 2c 20 26 67 6c 6f 62 49 6e 66 6f 2c 20  , 2, &globInfo, 
26822 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45  SQLITE_FUNC_LIKE
26823 7c 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53  |SQLITE_FUNC_CAS
26824 45 29 2c 0a 20 20 23 69 66 64 65 66 20 53 51 4c  E),.  #ifdef SQL
26825 49 54 45 5f 43 41 53 45 5f 53 45 4e 53 49 54 49  ITE_CASE_SENSITI
26826 56 45 5f 4c 49 4b 45 0a 20 20 20 20 4c 49 4b 45  VE_LIKE.    LIKE
26827 46 55 4e 43 28 6c 69 6b 65 2c 20 32 2c 20 26 6c  FUNC(like, 2, &l
26828 69 6b 65 49 6e 66 6f 41 6c 74 2c 20 53 51 4c 49  ikeInfoAlt, SQLI
26829 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c  TE_FUNC_LIKE|SQL
2682a 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c 0a  ITE_FUNC_CASE),.
2682b 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c 69 6b      LIKEFUNC(lik
2682c 65 2c 20 33 2c 20 26 6c 69 6b 65 49 6e 66 6f 41  e, 3, &likeInfoA
2682d 6c 74 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  lt, SQLITE_FUNC_
2682e 4c 49 4b 45 7c 53 51 4c 49 54 45 5f 46 55 4e 43  LIKE|SQLITE_FUNC
2682f 5f 43 41 53 45 29 2c 0a 20 20 23 65 6c 73 65 0a  _CASE),.  #else.
26830 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c 69 6b      LIKEFUNC(lik
26831 65 2c 20 32 2c 20 26 6c 69 6b 65 49 6e 66 6f 4e  e, 2, &likeInfoN
26832 6f 72 6d 2c 20 53 51 4c 49 54 45 5f 46 55 4e 43  orm, SQLITE_FUNC
26833 5f 4c 49 4b 45 29 2c 0a 20 20 20 20 4c 49 4b 45  _LIKE),.    LIKE
26834 46 55 4e 43 28 6c 69 6b 65 2c 20 33 2c 20 26 6c  FUNC(like, 3, &l
26835 69 6b 65 49 6e 66 6f 4e 6f 72 6d 2c 20 53 51 4c  ikeInfoNorm, SQL
26836 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29 2c 0a  ITE_FUNC_LIKE),.
26837 20 20 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 0a 20    #endif.  };.. 
26838 20 69 6e 74 20 69 3b 0a 20 20 46 75 6e 63 44 65   int i;.  FuncDe
26839 66 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26  fHash *pHash = &
2683a 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 48 61  GLOBAL(FuncDefHa
2683b 73 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  sh, sqlite3Globa
2683c 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a 20 20 46  lFunctions);.  F
2683d 75 6e 63 44 65 66 20 2a 61 46 75 6e 63 20 3d 20  uncDef *aFunc = 
2683e 28 46 75 6e 63 44 65 66 2a 29 26 47 4c 4f 42 41  (FuncDef*)&GLOBA
2683f 4c 28 46 75 6e 63 44 65 66 2c 20 61 42 75 69 6c  L(FuncDef, aBuil
26840 74 69 6e 46 75 6e 63 29 3b 0a 0a 20 20 66 6f 72  tinFunc);..  for
26841 28 69 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a  (i=0; i<ArraySiz
26842 65 28 61 42 75 69 6c 74 69 6e 46 75 6e 63 29 3b  e(aBuiltinFunc);
26843 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74   i++){.    sqlit
26844 65 33 46 75 6e 63 44 65 66 49 6e 73 65 72 74 28  e3FuncDefInsert(
26845 70 48 61 73 68 2c 20 26 61 46 75 6e 63 5b 69 5d  pHash, &aFunc[i]
26846 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
26847 52 65 67 69 73 74 65 72 44 61 74 65 54 69 6d 65  RegisterDateTime
26848 46 75 6e 63 74 69 6f 6e 73 28 29 3b 0a 7d 0a 0a  Functions();.}..
26849 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2684a 45 6e 64 20 6f 66 20 66 75 6e 63 2e 63 20 2a 2a  End of func.c **
2684b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2684c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2684d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2684e 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2684f 42 65 67 69 6e 20 66 69 6c 65 20 66 6b 65 79 2e  Begin file fkey.
26850 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
26851 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26852 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
26853 2f 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  /*.**.** The aut
26854 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
26855 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
26856 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
26857 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
26858 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
26859 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
2685a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
2685b 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
2685c 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
2685d 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
2685e 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
2685f 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
26860 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
26861 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
26862 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
26863 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
26864 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
26865 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26866 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26867 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26868 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26869 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
2686a 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65  ontains code use
2686b 64 20 62 79 20 74 68 65 20 63 6f 6d 70 69 6c 65  d by the compile
2686c 72 20 74 6f 20 61 64 64 20 66 6f 72 65 69 67 6e  r to add foreign
2686d 20 6b 65 79 0a 2a 2a 20 73 75 70 70 6f 72 74 20   key.** support 
2686e 74 6f 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c 20  to compiled SQL 
2686f 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 0a  statements..*/..
26870 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
26871 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a  MIT_FOREIGN_KEY.
26872 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
26873 4d 49 54 5f 54 52 49 47 47 45 52 0a 0a 2f 2a 0a  MIT_TRIGGER../*.
26874 2a 2a 20 44 65 66 65 72 72 65 64 20 61 6e 64 20  ** Deferred and 
26875 49 6d 6d 65 64 69 61 74 65 20 46 4b 73 0a 2a 2a  Immediate FKs.**
26876 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
26877 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a  -----------.**.*
26878 2a 20 46 6f 72 65 69 67 6e 20 6b 65 79 73 20 69  * Foreign keys i
26879 6e 20 53 51 4c 69 74 65 20 63 6f 6d 65 20 69 6e  n SQLite come in
2687a 20 74 77 6f 20 66 6c 61 76 6f 75 72 73 3a 20 64   two flavours: d
2687b 65 66 65 72 72 65 64 20 61 6e 64 20 69 6d 6d 65  eferred and imme
2687c 64 69 61 74 65 2e 0a 2a 2a 20 49 66 20 61 6e 20  diate..** If an 
2687d 69 6d 6d 65 64 69 61 74 65 20 66 6f 72 65 69 67  immediate foreig
2687e 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74  n key constraint
2687f 20 69 73 20 76 69 6f 6c 61 74 65 64 2c 20 53 51   is violated, SQ
26880 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 0a  LITE_CONSTRAINT.
26881 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  ** is returned a
26882 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  nd the current s
26883 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
26884 74 69 6f 6e 20 72 6f 6c 6c 65 64 20 62 61 63 6b  tion rolled back
26885 2e 20 49 66 20 61 20 0a 2a 2a 20 64 65 66 65 72  . If a .** defer
26886 72 65 64 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  red foreign key 
26887 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 76 69  constraint is vi
26888 6f 6c 61 74 65 64 2c 20 6e 6f 20 61 63 74 69 6f  olated, no actio
26889 6e 20 69 73 20 74 61 6b 65 6e 20 0a 2a 2a 20 69  n is taken .** i
2688a 6d 6d 65 64 69 61 74 65 6c 79 2e 20 48 6f 77 65  mmediately. Howe
2688b 76 65 72 20 69 66 20 74 68 65 20 61 70 70 6c 69  ver if the appli
2688c 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20  cation attempts 
2688d 74 6f 20 63 6f 6d 6d 69 74 20 74 68 65 20 0a 2a  to commit the .*
2688e 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65  * transaction be
2688f 66 6f 72 65 20 66 69 78 69 6e 67 20 74 68 65 20  fore fixing the 
26890 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61  constraint viola
26891 74 69 6f 6e 2c 20 74 68 65 20 61 74 74 65 6d 70  tion, the attemp
26892 74 20 66 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20 44  t fails..**.** D
26893 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69  eferred constrai
26894 6e 74 73 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e  nts are implemen
26895 74 65 64 20 75 73 69 6e 67 20 61 20 73 69 6d 70  ted using a simp
26896 6c 65 20 63 6f 75 6e 74 65 72 20 61 73 73 6f 63  le counter assoc
26897 69 61 74 65 64 0a 2a 2a 20 77 69 74 68 20 74 68  iated.** with th
26898 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
26899 65 2e 20 54 68 65 20 63 6f 75 6e 74 65 72 20 69  e. The counter i
2689a 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 20 65 61  s set to zero ea
2689b 63 68 20 74 69 6d 65 20 61 20 0a 2a 2a 20 64 61  ch time a .** da
2689c 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 69  tabase transacti
2689d 6f 6e 20 69 73 20 6f 70 65 6e 65 64 2e 20 45 61  on is opened. Ea
2689e 63 68 20 74 69 6d 65 20 61 20 73 74 61 74 65 6d  ch time a statem
2689f 65 6e 74 20 69 73 20 65 78 65 63 75 74 65 64 20  ent is executed 
268a0 0a 2a 2a 20 74 68 61 74 20 63 61 75 73 65 73 20  .** that causes 
268a1 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 76 69  a foreign key vi
268a2 6f 6c 61 74 69 6f 6e 2c 20 74 68 65 20 63 6f 75  olation, the cou
268a3 6e 74 65 72 20 69 73 20 69 6e 63 72 65 6d 65 6e  nter is incremen
268a4 74 65 64 2e 20 45 61 63 68 0a 2a 2a 20 74 69 6d  ted. Each.** tim
268a5 65 20 61 20 73 74 61 74 65 6d 65 6e 74 20 69 73  e a statement is
268a6 20 65 78 65 63 75 74 65 64 20 74 68 61 74 20 72   executed that r
268a7 65 6d 6f 76 65 73 20 61 6e 20 65 78 69 73 74 69  emoves an existi
268a8 6e 67 20 76 69 6f 6c 61 74 69 6f 6e 20 66 72 6f  ng violation fro
268a9 6d 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73  m.** the databas
268aa 65 2c 20 74 68 65 20 63 6f 75 6e 74 65 72 20 69  e, the counter i
268ab 73 20 64 65 63 72 65 6d 65 6e 74 65 64 2e 20 57  s decremented. W
268ac 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74  hen the transact
268ad 69 6f 6e 20 69 73 0a 2a 2a 20 63 6f 6d 6d 69 74  ion is.** commit
268ae 74 65 64 2c 20 74 68 65 20 63 6f 6d 6d 69 74 20  ted, the commit 
268af 66 61 69 6c 73 20 69 66 20 74 68 65 20 63 75 72  fails if the cur
268b0 72 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68  rent value of th
268b1 65 20 63 6f 75 6e 74 65 72 20 69 73 0a 2a 2a 20  e counter is.** 
268b2 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72  greater than zer
268b3 6f 2e 20 54 68 69 73 20 73 63 68 65 6d 65 20 68  o. This scheme h
268b4 61 73 20 74 77 6f 20 62 69 67 20 64 72 61 77 62  as two big drawb
268b5 61 63 6b 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20  acks:.**.**   * 
268b6 57 68 65 6e 20 61 20 63 6f 6d 6d 69 74 20 66 61  When a commit fa
268b7 69 6c 73 20 64 75 65 20 74 6f 20 61 20 64 65 66  ils due to a def
268b8 65 72 72 65 64 20 66 6f 72 65 69 67 6e 20 6b 65  erred foreign ke
268b9 79 20 63 6f 6e 73 74 72 61 69 6e 74 2c 20 0a 2a  y constraint, .*
268ba 2a 20 20 20 20 20 74 68 65 72 65 20 69 73 20 6e  *     there is n
268bb 6f 20 77 61 79 20 74 6f 20 74 65 6c 6c 20 77 68  o way to tell wh
268bc 69 63 68 20 66 6f 72 65 69 67 6e 20 63 6f 6e 73  ich foreign cons
268bd 74 72 61 69 6e 74 20 69 73 20 6e 6f 74 20 73 61  traint is not sa
268be 74 69 73 66 69 65 64 2c 0a 2a 2a 20 20 20 20 20  tisfied,.**     
268bf 6f 72 20 77 68 69 63 68 20 72 6f 77 20 69 74 20  or which row it 
268c0 69 73 20 6e 6f 74 20 73 61 74 69 73 66 69 65 64  is not satisfied
268c1 20 66 6f 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20   for..**.**   * 
268c2 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
268c3 63 6f 6e 74 61 69 6e 73 20 66 6f 72 65 69 67 6e  contains foreign
268c4 20 6b 65 79 20 76 69 6f 6c 61 74 69 6f 6e 73 20   key violations 
268c5 77 68 65 6e 20 74 68 65 20 0a 2a 2a 20 20 20 20  when the .**    
268c6 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
268c7 6f 70 65 6e 65 64 2c 20 74 68 69 73 20 6d 61 79  opened, this may
268c8 20 63 61 75 73 65 20 74 68 65 20 6d 65 63 68 61   cause the mecha
268c9 6e 69 73 6d 20 74 6f 20 6d 61 6c 66 75 6e 63 74  nism to malfunct
268ca 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44 65 73 70 69  ion..**.** Despi
268cb 74 65 20 74 68 65 73 65 20 70 72 6f 62 6c 65 6d  te these problem
268cc 73 2c 20 74 68 69 73 20 61 70 70 72 6f 61 63 68  s, this approach
268cd 20 69 73 20 61 64 6f 70 74 65 64 20 61 73 20 69   is adopted as i
268ce 74 20 73 65 65 6d 73 20 73 69 6d 70 6c 65 72 0a  t seems simpler.
268cf 2a 2a 20 74 68 61 6e 20 74 68 65 20 61 6c 74 65  ** than the alte
268d0 72 6e 61 74 69 76 65 73 2e 0a 2a 2a 0a 2a 2a 20  rnatives..**.** 
268d1 49 4e 53 45 52 54 20 6f 70 65 72 61 74 69 6f 6e  INSERT operation
268d2 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 49 2e 31 29 20  s:.**.**   I.1) 
268d3 46 6f 72 20 65 61 63 68 20 46 4b 20 66 6f 72 20  For each FK for 
268d4 77 68 69 63 68 20 74 68 65 20 74 61 62 6c 65 20  which the table 
268d5 69 73 20 74 68 65 20 63 68 69 6c 64 20 74 61 62  is the child tab
268d6 6c 65 2c 20 73 65 61 72 63 68 0a 2a 2a 20 20 20  le, search.**   
268d7 20 20 20 20 20 74 68 65 20 70 61 72 65 6e 74 20       the parent 
268d8 74 61 62 6c 65 20 66 6f 72 20 61 20 6d 61 74 63  table for a matc
268d9 68 2e 20 49 66 20 6e 6f 6e 65 20 69 73 20 66 6f  h. If none is fo
268da 75 6e 64 20 69 6e 63 72 65 6d 65 6e 74 20 74 68  und increment th
268db 65 0a 2a 2a 20 20 20 20 20 20 20 20 63 6f 6e 73  e.**        cons
268dc 74 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 2e 0a  traint counter..
268dd 2a 2a 0a 2a 2a 20 20 20 49 2e 32 29 20 46 6f 72  **.**   I.2) For
268de 20 65 61 63 68 20 46 4b 20 66 6f 72 20 77 68 69   each FK for whi
268df 63 68 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  ch the table is 
268e0 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65  the parent table
268e1 2c 20 0a 2a 2a 20 20 20 20 20 20 20 20 73 65 61  , .**        sea
268e2 72 63 68 20 74 68 65 20 63 68 69 6c 64 20 74 61  rch the child ta
268e3 62 6c 65 20 66 6f 72 20 72 6f 77 73 20 74 68 61  ble for rows tha
268e4 74 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20  t correspond to 
268e5 74 68 65 20 6e 65 77 0a 2a 2a 20 20 20 20 20 20  the new.**      
268e6 20 20 72 6f 77 20 69 6e 20 74 68 65 20 70 61 72    row in the par
268e7 65 6e 74 20 74 61 62 6c 65 2e 20 44 65 63 72 65  ent table. Decre
268e8 6d 65 6e 74 20 74 68 65 20 63 6f 75 6e 74 65 72  ment the counter
268e9 20 66 6f 72 20 65 61 63 68 20 72 6f 77 0a 2a 2a   for each row.**
268ea 20 20 20 20 20 20 20 20 66 6f 75 6e 64 20 28 61          found (a
268eb 73 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74  s the constraint
268ec 20 69 73 20 6e 6f 77 20 73 61 74 69 73 66 69 65   is now satisfie
268ed 64 29 2e 0a 2a 2a 0a 2a 2a 20 44 45 4c 45 54 45  d)..**.** DELETE
268ee 20 6f 70 65 72 61 74 69 6f 6e 73 3a 0a 2a 2a 0a   operations:.**.
268ef 2a 2a 20 20 20 44 2e 31 29 20 46 6f 72 20 65 61  **   D.1) For ea
268f0 63 68 20 46 4b 20 66 6f 72 20 77 68 69 63 68 20  ch FK for which 
268f1 74 68 65 20 74 61 62 6c 65 20 69 73 20 74 68 65  the table is the
268f2 20 63 68 69 6c 64 20 74 61 62 6c 65 2c 20 0a 2a   child table, .*
268f3 2a 20 20 20 20 20 20 20 20 73 65 61 72 63 68 20  *        search 
268f4 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65  the parent table
268f5 20 66 6f 72 20 61 20 72 6f 77 20 74 68 61 74 20   for a row that 
268f6 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74  corresponds to t
268f7 68 65 20 0a 2a 2a 20 20 20 20 20 20 20 20 64 65  he .**        de
268f8 6c 65 74 65 64 20 72 6f 77 20 69 6e 20 74 68 65  leted row in the
268f9 20 63 68 69 6c 64 20 74 61 62 6c 65 2e 20 49 66   child table. If
268fa 20 73 75 63 68 20 61 20 72 6f 77 20 69 73 20 6e   such a row is n
268fb 6f 74 20 66 6f 75 6e 64 2c 20 0a 2a 2a 20 20 20  ot found, .**   
268fc 20 20 20 20 20 64 65 63 72 65 6d 65 6e 74 20 74       decrement t
268fd 68 65 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 0a 2a  he counter..**.*
268fe 2a 20 20 20 44 2e 32 29 20 46 6f 72 20 65 61 63  *   D.2) For eac
268ff 68 20 46 4b 20 66 6f 72 20 77 68 69 63 68 20 74  h FK for which t
26900 68 65 20 74 61 62 6c 65 20 69 73 20 74 68 65 20  he table is the 
26901 70 61 72 65 6e 74 20 74 61 62 6c 65 2c 20 73 65  parent table, se
26902 61 72 63 68 20 0a 2a 2a 20 20 20 20 20 20 20 20  arch .**        
26903 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 20  the child table 
26904 66 6f 72 20 72 6f 77 73 20 74 68 61 74 20 63 6f  for rows that co
26905 72 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20  rrespond to the 
26906 64 65 6c 65 74 65 64 20 72 6f 77 20 0a 2a 2a 20  deleted row .** 
26907 20 20 20 20 20 20 20 69 6e 20 74 68 65 20 70 61         in the pa
26908 72 65 6e 74 20 74 61 62 6c 65 2e 20 46 6f 72 20  rent table. For 
26909 65 61 63 68 20 66 6f 75 6e 64 20 69 6e 63 72 65  each found incre
2690a 6d 65 6e 74 20 74 68 65 20 63 6f 75 6e 74 65 72  ment the counter
2690b 2e 0a 2a 2a 0a 2a 2a 20 55 50 44 41 54 45 20 6f  ..**.** UPDATE o
2690c 70 65 72 61 74 69 6f 6e 73 3a 0a 2a 2a 0a 2a 2a  perations:.**.**
2690d 20 20 20 41 6e 20 55 50 44 41 54 45 20 63 6f 6d     An UPDATE com
2690e 6d 61 6e 64 20 72 65 71 75 69 72 65 73 20 74 68  mand requires th
2690f 61 74 20 61 6c 6c 20 34 20 73 74 65 70 73 20 61  at all 4 steps a
26910 62 6f 76 65 20 61 72 65 20 74 61 6b 65 6e 2c 20  bove are taken, 
26911 62 75 74 20 6f 6e 6c 79 0a 2a 2a 20 20 20 66 6f  but only.**   fo
26912 72 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73  r FK constraints
26913 20 66 6f 72 20 77 68 69 63 68 20 74 68 65 20 61   for which the a
26914 66 66 65 63 74 65 64 20 63 6f 6c 75 6d 6e 73 20  ffected columns 
26915 61 72 65 20 61 63 74 75 61 6c 6c 79 20 0a 2a 2a  are actually .**
26916 20 20 20 6d 6f 64 69 66 69 65 64 20 28 76 61 6c     modified (val
26917 75 65 73 20 6d 75 73 74 20 62 65 20 63 6f 6d 70  ues must be comp
26918 61 72 65 64 20 61 74 20 72 75 6e 74 69 6d 65 29  ared at runtime)
26919 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61  ..**.** Note tha
2691a 74 20 49 2e 31 20 61 6e 64 20 44 2e 31 20 61 72  t I.1 and D.1 ar
2691b 65 20 76 65 72 79 20 73 69 6d 69 6c 61 72 20 6f  e very similar o
2691c 70 65 72 61 74 69 6f 6e 73 2c 20 61 73 20 61 72  perations, as ar
2691d 65 20 49 2e 32 20 61 6e 64 20 44 2e 32 2e 0a 2a  e I.2 and D.2..*
2691e 2a 20 54 68 69 73 20 73 69 6d 70 6c 69 66 69 65  * This simplifie
2691f 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  s the implementa
26920 74 69 6f 6e 20 61 20 62 69 74 2e 0a 2a 2a 0a 2a  tion a bit..**.*
26921 2a 20 46 6f 72 20 74 68 65 20 70 75 72 70 6f 73  * For the purpos
26922 65 73 20 6f 66 20 69 6d 6d 65 64 69 61 74 65 20  es of immediate 
26923 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20  FK constraints, 
26924 74 68 65 20 4f 52 20 52 45 50 4c 41 43 45 20 63  the OR REPLACE c
26925 6f 6e 66 6c 69 63 74 0a 2a 2a 20 72 65 73 6f 6c  onflict.** resol
26926 75 74 69 6f 6e 20 69 73 20 63 6f 6e 73 69 64 65  ution is conside
26927 72 65 64 20 74 6f 20 64 65 6c 65 74 65 20 72 6f  red to delete ro
26928 77 73 20 62 65 66 6f 72 65 20 74 68 65 20 6e 65  ws before the ne
26929 77 20 72 6f 77 20 69 73 20 69 6e 73 65 72 74 65  w row is inserte
2692a 64 2e 0a 2a 2a 20 49 66 20 61 20 64 65 6c 65 74  d..** If a delet
2692b 65 20 63 61 75 73 65 64 20 62 79 20 4f 52 20 52  e caused by OR R
2692c 45 50 4c 41 43 45 20 76 69 6f 6c 61 74 65 73 20  EPLACE violates 
2692d 61 6e 20 46 4b 20 63 6f 6e 73 74 72 61 69 6e 74  an FK constraint
2692e 2c 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 0a 2a  , an exception.*
2692f 2a 20 69 73 20 74 68 72 6f 77 6e 2c 20 65 76 65  * is thrown, eve
26930 6e 20 69 66 20 74 68 65 20 46 4b 20 63 6f 6e 73  n if the FK cons
26931 74 72 61 69 6e 74 20 77 6f 75 6c 64 20 62 65 20  traint would be 
26932 73 61 74 69 73 66 69 65 64 20 61 66 74 65 72 20  satisfied after 
26933 74 68 65 20 6e 65 77 20 0a 2a 2a 20 72 6f 77 20  the new .** row 
26934 69 73 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2a 0a  is inserted..**.
26935 2a 2a 20 49 6d 6d 65 64 69 61 74 65 20 63 6f 6e  ** Immediate con
26936 73 74 72 61 69 6e 74 73 20 61 72 65 20 75 73 75  straints are usu
26937 61 6c 6c 79 20 68 61 6e 64 6c 65 64 20 73 69 6d  ally handled sim
26938 69 6c 61 72 6c 79 2e 20 54 68 65 20 6f 6e 6c 79  ilarly. The only
26939 20 64 69 66 66 65 72 65 6e 63 65 20 0a 2a 2a 20   difference .** 
2693a 69 73 20 74 68 61 74 20 74 68 65 20 63 6f 75 6e  is that the coun
2693b 74 65 72 20 75 73 65 64 20 69 73 20 73 74 6f 72  ter used is stor
2693c 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 65 61  ed as part of ea
2693d 63 68 20 69 6e 64 69 76 69 64 75 61 6c 20 73 74  ch individual st
2693e 61 74 65 6d 65 6e 74 0a 2a 2a 20 6f 62 6a 65 63  atement.** objec
2693f 74 20 28 73 74 72 75 63 74 20 56 64 62 65 29 2e  t (struct Vdbe).
26940 20 49 66 2c 20 61 66 74 65 72 20 74 68 65 20 73   If, after the s
26941 74 61 74 65 6d 65 6e 74 20 68 61 73 20 72 75 6e  tatement has run
26942 2c 20 69 74 73 20 69 6d 6d 65 64 69 61 74 65 0a  , its immediate.
26943 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f  ** constraint co
26944 75 6e 74 65 72 20 69 73 20 67 72 65 61 74 65 72  unter is greater
26945 20 74 68 61 6e 20 7a 65 72 6f 2c 20 69 74 20 72   than zero, it r
26946 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 43 4f  eturns SQLITE_CO
26947 4e 53 54 52 41 49 4e 54 0a 2a 2a 20 61 6e 64 20  NSTRAINT.** and 
26948 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 74 72  the statement tr
26949 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c  ansaction is rol
2694a 6c 65 64 20 62 61 63 6b 2e 20 41 6e 20 65 78 63  led back. An exc
2694b 65 70 74 69 6f 6e 20 69 73 20 61 6e 20 49 4e 53  eption is an INS
2694c 45 52 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  ERT.** statement
2694d 20 74 68 61 74 20 69 6e 73 65 72 74 73 20 61 20   that inserts a 
2694e 73 69 6e 67 6c 65 20 72 6f 77 20 6f 6e 6c 79 20  single row only 
2694f 28 6e 6f 20 74 72 69 67 67 65 72 73 29 2e 20 49  (no triggers). I
26950 6e 20 74 68 69 73 20 63 61 73 65 2c 0a 2a 2a 20  n this case,.** 
26951 69 6e 73 74 65 61 64 20 6f 66 20 75 73 69 6e 67  instead of using
26952 20 61 20 63 6f 75 6e 74 65 72 2c 20 61 6e 20 65   a counter, an e
26953 78 63 65 70 74 69 6f 6e 20 69 73 20 74 68 72 6f  xception is thro
26954 77 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 69  wn immediately i
26955 66 20 74 68 65 0a 2a 2a 20 49 4e 53 45 52 54 20  f the.** INSERT 
26956 76 69 6f 6c 61 74 65 73 20 61 20 66 6f 72 65 69  violates a forei
26957 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
26958 74 2e 20 54 68 69 73 20 69 73 20 6e 65 63 65 73  t. This is neces
26959 73 61 72 79 20 61 73 20 73 75 63 68 0a 2a 2a 20  sary as such.** 
2695a 61 6e 20 49 4e 53 45 52 54 20 64 6f 65 73 20 6e  an INSERT does n
2695b 6f 74 20 6f 70 65 6e 20 61 20 73 74 61 74 65 6d  ot open a statem
2695c 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ent transaction.
2695d 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a 20 48 6f 77  .**.** TODO: How
2695e 20 73 68 6f 75 6c 64 20 64 72 6f 70 70 69 6e 67   should dropping
2695f 20 61 20 74 61 62 6c 65 20 62 65 20 68 61 6e 64   a table be hand
26960 6c 65 64 3f 20 48 6f 77 20 73 68 6f 75 6c 64 20  led? How should 
26961 72 65 6e 61 6d 69 6e 67 20 61 20 0a 2a 2a 20 74  renaming a .** t
26962 61 62 6c 65 20 62 65 20 68 61 6e 64 6c 65 64 3f  able be handled?
26963 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 51 75 65 72 79 20  .**.**.** Query 
26964 41 50 49 20 4e 6f 74 65 73 0a 2a 2a 20 2d 2d 2d  API Notes.** ---
26965 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a  ------------.**.
26966 2a 2a 20 42 65 66 6f 72 65 20 63 6f 64 69 6e 67  ** Before coding
26967 20 61 6e 20 55 50 44 41 54 45 20 6f 72 20 44 45   an UPDATE or DE
26968 4c 45 54 45 20 72 6f 77 20 6f 70 65 72 61 74 69  LETE row operati
26969 6f 6e 2c 20 74 68 65 20 63 6f 64 65 2d 67 65 6e  on, the code-gen
2696a 65 72 61 74 6f 72 0a 2a 2a 20 66 6f 72 20 74 68  erator.** for th
2696b 6f 73 65 20 74 77 6f 20 6f 70 65 72 61 74 69 6f  ose two operatio
2696c 6e 73 20 6e 65 65 64 73 20 74 6f 20 6b 6e 6f 77  ns needs to know
2696d 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
2696e 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a  the operation.**
2696f 20 72 65 71 75 69 72 65 73 20 61 6e 79 20 46 4b   requires any FK
26970 20 70 72 6f 63 65 73 73 69 6e 67 20 61 6e 64 2c   processing and,
26971 20 69 66 20 73 6f 2c 20 77 68 69 63 68 20 63 6f   if so, which co
26972 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 6f 72 69  lumns of the ori
26973 67 69 6e 61 6c 0a 2a 2a 20 72 6f 77 20 61 72 65  ginal.** row are
26974 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65   required by the
26975 20 46 4b 20 70 72 6f 63 65 73 73 69 6e 67 20 56   FK processing V
26976 44 42 45 20 63 6f 64 65 20 28 69 2e 65 2e 20 69  DBE code (i.e. i
26977 66 20 46 4b 73 20 77 65 72 65 0a 2a 2a 20 69 6d  f FKs were.** im
26978 70 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 20  plemented using 
26979 74 72 69 67 67 65 72 73 2c 20 77 68 69 63 68 20  triggers, which 
2697a 6f 66 20 74 68 65 20 6f 6c 64 2e 2a 20 63 6f 6c  of the old.* col
2697b 75 6d 6e 73 20 77 6f 75 6c 64 20 62 65 20 0a 2a  umns would be .*
2697c 2a 20 61 63 63 65 73 73 65 64 29 2e 20 4e 6f 20  * accessed). No 
2697d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72  information is r
2697e 65 71 75 69 72 65 64 20 62 79 20 74 68 65 20 63  equired by the c
2697f 6f 64 65 2d 67 65 6e 65 72 61 74 6f 72 20 62 65  ode-generator be
26980 66 6f 72 65 0a 2a 2a 20 63 6f 64 69 6e 67 20 61  fore.** coding a
26981 6e 20 49 4e 53 45 52 54 20 6f 70 65 72 61 74 69  n INSERT operati
26982 6f 6e 2e 20 54 68 65 20 66 75 6e 63 74 69 6f 6e  on. The function
26983 73 20 75 73 65 64 20 62 79 20 74 68 65 20 55 50  s used by the UP
26984 44 41 54 45 2f 44 45 4c 45 54 45 0a 2a 2a 20 67  DATE/DELETE.** g
26985 65 6e 65 72 61 74 69 6f 6e 20 63 6f 64 65 20 74  eneration code t
26986 6f 20 71 75 65 72 79 20 66 6f 72 20 74 68 69 73  o query for this
26987 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 72 65   information are
26988 3a 0a 2a 2a 0a 2a 2a 20 20 20 73 71 6c 69 74 65  :.**.**   sqlite
26989 33 46 6b 52 65 71 75 69 72 65 64 28 29 20 2d 20  3FkRequired() - 
2698a 54 65 73 74 20 74 6f 20 73 65 65 20 69 66 20 46  Test to see if F
2698b 4b 20 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20  K processing is 
2698c 72 65 71 75 69 72 65 64 2e 0a 2a 2a 20 20 20 73  required..**   s
2698d 71 6c 69 74 65 33 46 6b 4f 6c 64 6d 61 73 6b 28  qlite3FkOldmask(
2698e 29 20 20 2d 20 51 75 65 72 79 20 66 6f 72 20 74  )  - Query for t
2698f 68 65 20 73 65 74 20 6f 66 20 72 65 71 75 69 72  he set of requir
26990 65 64 20 6f 6c 64 2e 2a 20 63 6f 6c 75 6d 6e 73  ed old.* columns
26991 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 45 78 74 65 72  ..**.**.** Exter
26992 6e 61 6c 6c 79 20 61 63 63 65 73 73 69 62 6c 65  nally accessible
26993 20 6d 6f 64 75 6c 65 20 66 75 6e 63 74 69 6f 6e   module function
26994 73 0a 2a 2a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  s.** -----------
26995 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26996 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a  -----------.**.*
26997 2a 20 20 20 73 71 6c 69 74 65 33 46 6b 43 68 65  *   sqlite3FkChe
26998 63 6b 28 29 20 20 20 20 2d 20 43 68 65 63 6b 20  ck()    - Check 
26999 66 6f 72 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  for foreign key 
2699a 76 69 6f 6c 61 74 69 6f 6e 73 2e 0a 2a 2a 20 20  violations..**  
2699b 20 73 71 6c 69 74 65 33 46 6b 41 63 74 69 6f 6e   sqlite3FkAction
2699c 73 28 29 20 20 2d 20 43 6f 64 65 20 74 72 69 67  s()  - Code trig
2699d 67 65 72 73 20 66 6f 72 20 4f 4e 20 55 50 44 41  gers for ON UPDA
2699e 54 45 2f 4f 4e 20 44 45 4c 45 54 45 20 61 63 74  TE/ON DELETE act
2699f 69 6f 6e 73 2e 0a 2a 2a 20 20 20 73 71 6c 69 74  ions..**   sqlit
269a0 65 33 46 6b 44 65 6c 65 74 65 28 29 20 20 20 2d  e3FkDelete()   -
269a1 20 44 65 6c 65 74 65 20 61 6e 20 46 4b 65 79 20   Delete an FKey 
269a2 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 0a 2f  structure..*/../
269a3 2a 0a 2a 2a 20 56 44 42 45 20 43 61 6c 6c 69 6e  *.** VDBE Callin
269a4 67 20 43 6f 6e 76 65 6e 74 69 6f 6e 0a 2a 2a 20  g Convention.** 
269a5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
269a6 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 45 78  -------.**.** Ex
269a7 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 46  ample:.**.**   F
269a8 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  or the following
269a9 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e   INSERT statemen
269aa 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45  t:.**.**     CRE
269ab 41 54 45 20 54 41 42 4c 45 20 74 31 28 61 2c 20  ATE TABLE t1(a, 
269ac 62 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  b INTEGER PRIMAR
269ad 59 20 4b 45 59 2c 20 63 29 3b 0a 2a 2a 20 20 20  Y KEY, c);.**   
269ae 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31    INSERT INTO t1
269af 20 56 41 4c 55 45 53 28 31 2c 20 32 2c 20 33 2e   VALUES(1, 2, 3.
269b0 31 29 3b 0a 2a 2a 0a 2a 2a 20 20 20 52 65 67 69  1);.**.**   Regi
269b1 73 74 65 72 20 28 78 29 3a 20 20 20 20 20 20 20  ster (x):       
269b2 20 32 20 20 20 20 28 74 79 70 65 20 69 6e 74 65   2    (type inte
269b3 67 65 72 29 0a 2a 2a 20 20 20 52 65 67 69 73 74  ger).**   Regist
269b4 65 72 20 28 78 2b 31 29 3a 20 20 20 20 20 20 31  er (x+1):      1
269b5 20 20 20 20 28 74 79 70 65 20 69 6e 74 65 67 65      (type intege
269b6 72 29 0a 2a 2a 20 20 20 52 65 67 69 73 74 65 72  r).**   Register
269b7 20 28 78 2b 32 29 3a 20 20 20 20 20 20 4e 55 4c   (x+2):      NUL
269b8 4c 20 28 74 79 70 65 20 4e 55 4c 4c 29 0a 2a 2a  L (type NULL).**
269b9 20 20 20 52 65 67 69 73 74 65 72 20 28 78 2b 33     Register (x+3
269ba 29 3a 20 20 20 20 20 20 33 2e 31 20 20 28 74 79  ):      3.1  (ty
269bb 70 65 20 72 65 61 6c 29 0a 2a 2f 0a 0a 2f 2a 0a  pe real).*/../*.
269bc 2a 2a 20 41 20 66 6f 72 65 69 67 6e 20 6b 65 79  ** A foreign key
269bd 20 63 6f 6e 73 74 72 61 69 6e 74 20 72 65 71 75   constraint requ
269be 69 72 65 73 20 74 68 61 74 20 74 68 65 20 6b 65  ires that the ke
269bf 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  y columns in the
269c0 20 70 61 72 65 6e 74 0a 2a 2a 20 74 61 62 6c 65   parent.** table
269c1 20 61 72 65 20 63 6f 6c 6c 65 63 74 69 76 65 6c   are collectivel
269c2 79 20 73 75 62 6a 65 63 74 20 74 6f 20 61 20 55  y subject to a U
269c3 4e 49 51 55 45 20 6f 72 20 50 52 49 4d 41 52 59  NIQUE or PRIMARY
269c4 20 4b 45 59 20 63 6f 6e 73 74 72 61 69 6e 74 2e   KEY constraint.
269c5 0a 2a 2a 20 47 69 76 65 6e 20 74 68 61 74 20 70  .** Given that p
269c6 50 61 72 65 6e 74 20 69 73 20 74 68 65 20 70 61  Parent is the pa
269c7 72 65 6e 74 20 74 61 62 6c 65 20 66 6f 72 20 66  rent table for f
269c8 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
269c9 72 61 69 6e 74 20 70 46 4b 65 79 2c 20 0a 2a 2a  raint pFKey, .**
269ca 20 73 65 61 72 63 68 20 74 68 65 20 73 63 68 65   search the sche
269cb 6d 61 20 61 20 75 6e 69 71 75 65 20 69 6e 64 65  ma a unique inde
269cc 78 20 6f 6e 20 74 68 65 20 70 61 72 65 6e 74 20  x on the parent 
269cd 6b 65 79 20 63 6f 6c 75 6d 6e 73 2e 20 0a 2a 2a  key columns. .**
269ce 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66 75  .** If successfu
269cf 6c 2c 20 7a 65 72 6f 20 69 73 20 72 65 74 75 72  l, zero is retur
269d0 6e 65 64 2e 20 49 66 20 74 68 65 20 70 61 72 65  ned. If the pare
269d1 6e 74 20 6b 65 79 20 69 73 20 61 6e 20 49 4e 54  nt key is an INT
269d2 45 47 45 52 20 50 52 49 4d 41 52 59 20 0a 2a 2a  EGER PRIMARY .**
269d3 20 4b 45 59 20 63 6f 6c 75 6d 6e 2c 20 74 68 65   KEY column, the
269d4 6e 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c  n output variabl
269d5 65 20 2a 70 70 49 64 78 20 69 73 20 73 65 74 20  e *ppIdx is set 
269d6 74 6f 20 4e 55 4c 4c 2e 20 4f 74 68 65 72 77 69  to NULL. Otherwi
269d7 73 65 2c 20 2a 70 70 49 64 78 20 0a 2a 2a 20 69  se, *ppIdx .** i
269d8 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 74  s set to point t
269d9 6f 20 74 68 65 20 75 6e 69 71 75 65 20 69 6e 64  o the unique ind
269da 65 78 2e 20 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74  ex. .** .** If t
269db 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f  he parent key co
269dc 6e 73 69 73 74 73 20 6f 66 20 61 20 73 69 6e 67  nsists of a sing
269dd 6c 65 20 63 6f 6c 75 6d 6e 20 28 74 68 65 20 66  le column (the f
269de 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74  oreign key const
269df 72 61 69 6e 74 0a 2a 2a 20 69 73 20 6e 6f 74 20  raint.** is not 
269e0 61 20 63 6f 6d 70 6f 73 69 74 65 20 66 6f 72 65  a composite fore
269e1 69 67 6e 20 6b 65 79 29 2c 20 6f 75 74 70 75 74  ign key), output
269e2 20 76 61 72 69 61 62 6c 65 20 2a 70 61 69 43 6f   variable *paiCo
269e3 6c 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c 4c  l is set to NULL
269e4 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20  ..** Otherwise, 
269e5 69 74 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69  it is set to poi
269e6 6e 74 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74  nt to an allocat
269e7 65 64 20 61 72 72 61 79 20 6f 66 20 73 69 7a 65  ed array of size
269e8 20 4e 2c 20 77 68 65 72 65 0a 2a 2a 20 4e 20 69   N, where.** N i
269e9 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
269ea 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 70  columns in the p
269eb 61 72 65 6e 74 20 6b 65 79 2e 20 54 68 65 20 66  arent key. The f
269ec 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20  irst element of 
269ed 74 68 65 0a 2a 2a 20 61 72 72 61 79 20 69 73 20  the.** array is 
269ee 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
269ef 20 63 68 69 6c 64 20 74 61 62 6c 65 20 63 6f 6c   child table col
269f0 75 6d 6e 20 74 68 61 74 20 69 73 20 6d 61 70 70  umn that is mapp
269f1 65 64 20 62 79 20 74 68 65 20 46 4b 0a 2a 2a 20  ed by the FK.** 
269f2 63 6f 6e 73 74 72 61 69 6e 74 20 74 6f 20 74 68  constraint to th
269f3 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 63  e parent table c
269f4 6f 6c 75 6d 6e 20 73 74 6f 72 65 64 20 69 6e 20  olumn stored in 
269f5 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f  the left-most co
269f6 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 69 6e 64 65 78  lumn.** of index
269f7 20 2a 70 70 49 64 78 2e 20 54 68 65 20 73 65 63   *ppIdx. The sec
269f8 6f 6e 64 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74  ond element of t
269f9 68 65 20 61 72 72 61 79 20 69 73 20 74 68 65 20  he array is the 
269fa 69 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20  index of the.** 
269fb 63 68 69 6c 64 20 74 61 62 6c 65 20 63 6f 6c 75  child table colu
269fc 6d 6e 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f  mn that correspo
269fd 6e 64 73 20 74 6f 20 74 68 65 20 73 65 63 6f 6e  nds to the secon
269fe 64 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75  d left-most colu
269ff 6d 6e 20 6f 66 0a 2a 2a 20 2a 70 70 49 64 78 2c  mn of.** *ppIdx,
26a00 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a   and so on..**.*
26a01 2a 20 49 66 20 74 68 65 20 72 65 71 75 69 72 65  * If the require
26a02 64 20 69 6e 64 65 78 20 63 61 6e 6e 6f 74 20 62  d index cannot b
26a03 65 20 66 6f 75 6e 64 2c 20 65 69 74 68 65 72 20  e found, either 
26a04 62 65 63 61 75 73 65 3a 0a 2a 2a 0a 2a 2a 20 20  because:.**.**  
26a05 20 31 29 20 54 68 65 20 6e 61 6d 65 64 20 70 61   1) The named pa
26a06 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73  rent key columns
26a07 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 2c 20 6f   do not exist, o
26a08 72 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 54 68 65  r.**.**   2) The
26a09 20 6e 61 6d 65 64 20 70 61 72 65 6e 74 20 6b 65   named parent ke
26a0a 79 20 63 6f 6c 75 6d 6e 73 20 64 6f 20 65 78 69  y columns do exi
26a0b 73 74 2c 20 62 75 74 20 61 72 65 20 6e 6f 74 20  st, but are not 
26a0c 73 75 62 6a 65 63 74 20 74 6f 20 61 0a 2a 2a 20  subject to a.** 
26a0d 20 20 20 20 20 55 4e 49 51 55 45 20 6f 72 20 50       UNIQUE or P
26a0e 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6e 73 74  RIMARY KEY const
26a0f 72 61 69 6e 74 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20  raint, or.**.** 
26a10 20 20 33 29 20 4e 6f 20 70 61 72 65 6e 74 20 6b    3) No parent k
26a11 65 79 20 63 6f 6c 75 6d 6e 73 20 77 65 72 65 20  ey columns were 
26a12 70 72 6f 76 69 64 65 64 20 65 78 70 6c 69 63 69  provided explici
26a13 74 6c 79 20 61 73 20 70 61 72 74 20 6f 66 20 74  tly as part of t
26a14 68 65 0a 2a 2a 20 20 20 20 20 20 66 6f 72 65 69  he.**      forei
26a15 67 6e 20 6b 65 79 20 64 65 66 69 6e 69 74 69 6f  gn key definitio
26a16 6e 2c 20 61 6e 64 20 74 68 65 20 70 61 72 65 6e  n, and the paren
26a17 74 20 74 61 62 6c 65 20 64 6f 65 73 20 6e 6f 74  t table does not
26a18 20 68 61 76 65 20 61 0a 2a 2a 20 20 20 20 20 20   have a.**      
26a19 50 52 49 4d 41 52 59 20 4b 45 59 2c 20 6f 72 0a  PRIMARY KEY, or.
26a1a 2a 2a 0a 2a 2a 20 20 20 34 29 20 4e 6f 20 70 61  **.**   4) No pa
26a1b 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73  rent key columns
26a1c 20 77 65 72 65 20 70 72 6f 76 69 64 65 64 20 65   were provided e
26a1d 78 70 6c 69 63 69 74 6c 79 20 61 73 20 70 61 72  xplicitly as par
26a1e 74 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20  t of the.**     
26a1f 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 65 66   foreign key def
26a20 69 6e 69 74 69 6f 6e 2c 20 61 6e 64 20 74 68 65  inition, and the
26a21 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 66 20   PRIMARY KEY of 
26a22 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65  the parent table
26a23 20 0a 2a 2a 20 20 20 20 20 20 63 6f 6e 73 69 73   .**      consis
26a24 74 73 20 6f 66 20 61 20 61 20 64 69 66 66 65 72  ts of a a differ
26a25 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  ent number of co
26a26 6c 75 6d 6e 73 20 74 6f 20 74 68 65 20 63 68 69  lumns to the chi
26a27 6c 64 20 6b 65 79 20 69 6e 20 0a 2a 2a 20 20 20  ld key in .**   
26a28 20 20 20 74 68 65 20 63 68 69 6c 64 20 74 61 62     the child tab
26a29 6c 65 2e 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 6e  le..**.** then n
26a2a 6f 6e 2d 7a 65 72 6f 20 69 73 20 72 65 74 75 72  on-zero is retur
26a2b 6e 65 64 2c 20 61 6e 64 20 61 20 22 66 6f 72 65  ned, and a "fore
26a2c 69 67 6e 20 6b 65 79 20 6d 69 73 6d 61 74 63 68  ign key mismatch
26a2d 22 20 65 72 72 6f 72 20 6c 6f 61 64 65 64 0a 2a  " error loaded.*
26a2e 2a 20 69 6e 74 6f 20 70 50 61 72 73 65 2e 20 49  * into pParse. I
26a2f 66 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 20 6f  f an OOM error o
26a30 63 63 75 72 73 2c 20 6e 6f 6e 2d 7a 65 72 6f 20  ccurs, non-zero 
26a31 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
26a32 74 68 65 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 64  the.** pParse->d
26a33 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
26a34 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2f 0a  flag is set..*/.
26a35 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 61 74  static int locat
26a36 65 46 6b 65 79 49 6e 64 65 78 28 0a 20 20 50 61  eFkeyIndex(.  Pa
26a37 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
26a38 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
26a39 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 74   Parse context t
26a3a 6f 20 73 74 6f 72 65 20 61 6e 79 20 65 72 72 6f  o store any erro
26a3b 72 20 69 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20  r in */.  Table 
26a3c 2a 70 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20  *pParent,       
26a3d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
26a3e 65 6e 74 20 74 61 62 6c 65 20 6f 66 20 46 4b 20  ent table of FK 
26a3f 63 6f 6e 73 74 72 61 69 6e 74 20 70 46 4b 65 79  constraint pFKey
26a40 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65   */.  FKey *pFKe
26a41 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y,              
26a42 20 20 20 20 20 20 2f 2a 20 46 6f 72 65 69 67 6e        /* Foreign
26a43 20 6b 65 79 20 74 6f 20 66 69 6e 64 20 69 6e 64   key to find ind
26a44 65 78 20 66 6f 72 20 2a 2f 0a 20 20 49 6e 64 65  ex for */.  Inde
26a45 78 20 2a 2a 70 70 49 64 78 2c 20 20 20 20 20 20  x **ppIdx,      
26a46 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
26a47 55 54 3a 20 55 6e 69 71 75 65 20 69 6e 64 65 78  UT: Unique index
26a48 20 6f 6e 20 70 61 72 65 6e 74 20 74 61 62 6c 65   on parent table
26a49 20 2a 2f 0a 20 20 69 6e 74 20 2a 2a 70 61 69 43   */.  int **paiC
26a4a 6f 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ol              
26a4b 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 61        /* OUT: Ma
26a4c 70 20 6f 66 20 69 6e 64 65 78 20 63 6f 6c 75 6d  p of index colum
26a4d 6e 73 20 69 6e 20 70 46 4b 65 79 20 2a 2f 0a 29  ns in pFKey */.)
26a4e 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20  {.  Index *pIdx 
26a4f 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
26a50 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
26a51 20 74 6f 20 72 65 74 75 72 6e 20 76 69 61 20 2a   to return via *
26a52 70 70 49 64 78 20 2a 2f 0a 20 20 69 6e 74 20 2a  ppIdx */.  int *
26a53 61 69 43 6f 6c 20 3d 20 30 3b 20 20 20 20 20 20  aiCol = 0;      
26a54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26a55 2a 20 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72  * Value to retur
26a56 6e 20 76 69 61 20 2a 70 61 69 43 6f 6c 20 2a 2f  n via *paiCol */
26a57 0a 20 20 69 6e 74 20 6e 43 6f 6c 20 3d 20 70 46  .  int nCol = pF
26a58 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 20 20 20 20 20  Key->nCol;      
26a59 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
26a5a 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70   of columns in p
26a5b 61 72 65 6e 74 20 6b 65 79 20 2a 2f 0a 20 20 63  arent key */.  c
26a5c 68 61 72 20 2a 7a 4b 65 79 20 3d 20 70 46 4b 65  har *zKey = pFKe
26a5d 79 2d 3e 61 43 6f 6c 5b 30 5d 2e 7a 43 6f 6c 3b  y->aCol[0].zCol;
26a5e 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 6c 65     /* Name of le
26a5f 66 74 2d 6d 6f 73 74 20 70 61 72 65 6e 74 20 6b  ft-most parent k
26a60 65 79 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 0a 20 20  ey column */..  
26a61 2f 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 69 73  /* The caller is
26a62 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
26a63 20 7a 65 72 6f 69 6e 67 20 6f 75 74 70 75 74 20   zeroing output 
26a64 70 61 72 61 6d 65 74 65 72 73 2e 20 2a 2f 0a 20  parameters. */. 
26a65 20 61 73 73 65 72 74 28 20 70 70 49 64 78 20 26   assert( ppIdx &
26a66 26 20 2a 70 70 49 64 78 3d 3d 30 20 29 3b 0a 20  & *ppIdx==0 );. 
26a67 20 61 73 73 65 72 74 28 20 21 70 61 69 43 6f 6c   assert( !paiCol
26a68 20 7c 7c 20 2a 70 61 69 43 6f 6c 3d 3d 30 20 29   || *paiCol==0 )
26a69 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ;.  assert( pPar
26a6a 73 65 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  se );..  /* If t
26a6b 68 69 73 20 69 73 20 61 20 6e 6f 6e 2d 63 6f 6d  his is a non-com
26a6c 70 6f 73 69 74 65 20 28 73 69 6e 67 6c 65 20 63  posite (single c
26a6d 6f 6c 75 6d 6e 29 20 66 6f 72 65 69 67 6e 20 6b  olumn) foreign k
26a6e 65 79 2c 20 63 68 65 63 6b 20 69 66 20 69 74 20  ey, check if it 
26a6f 0a 20 20 2a 2a 20 6d 61 70 73 20 74 6f 20 74 68  .  ** maps to th
26a70 65 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  e INTEGER PRIMAR
26a71 59 20 4b 45 59 20 6f 66 20 74 61 62 6c 65 20 70  Y KEY of table p
26a72 50 61 72 65 6e 74 2e 20 49 66 20 73 6f 2c 20 6c  Parent. If so, l
26a73 65 61 76 65 20 2a 70 70 49 64 78 20 0a 20 20 2a  eave *ppIdx .  *
26a74 2a 20 61 6e 64 20 2a 70 61 69 43 6f 6c 20 73 65  * and *paiCol se
26a75 74 20 74 6f 20 7a 65 72 6f 20 61 6e 64 20 72 65  t to zero and re
26a76 74 75 72 6e 20 65 61 72 6c 79 2e 20 0a 20 20 2a  turn early. .  *
26a77 2a 0a 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65  *.  ** Otherwise
26a78 2c 20 66 6f 72 20 61 20 63 6f 6d 70 6f 73 69 74  , for a composit
26a79 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 28 6d  e foreign key (m
26a7a 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 63 6f 6c  ore than one col
26a7b 75 6d 6e 29 2c 20 61 6c 6c 6f 63 61 74 65 0a 20  umn), allocate. 
26a7c 20 2a 2a 20 73 70 61 63 65 20 66 6f 72 20 74 68   ** space for th
26a7d 65 20 61 69 43 6f 6c 20 61 72 72 61 79 20 28 72  e aiCol array (r
26a7e 65 74 75 72 6e 65 64 20 76 69 61 20 6f 75 74 70  eturned via outp
26a7f 75 74 20 70 61 72 61 6d 65 74 65 72 20 2a 70 61  ut parameter *pa
26a80 69 43 6f 6c 29 2e 0a 20 20 2a 2a 20 4e 6f 6e 2d  iCol)..  ** Non-
26a81 63 6f 6d 70 6f 73 69 74 65 20 66 6f 72 65 69 67  composite foreig
26a82 6e 20 6b 65 79 73 20 64 6f 20 6e 6f 74 20 72 65  n keys do not re
26a83 71 75 69 72 65 20 74 68 65 20 61 69 43 6f 6c 20  quire the aiCol 
26a84 61 72 72 61 79 2e 0a 20 20 2a 2f 0a 20 20 69 66  array..  */.  if
26a85 28 20 6e 43 6f 6c 3d 3d 31 20 29 7b 0a 20 20 20  ( nCol==1 ){.   
26a86 20 2f 2a 20 54 68 65 20 46 4b 20 6d 61 70 73 20   /* The FK maps 
26a87 74 6f 20 74 68 65 20 49 50 4b 20 69 66 20 61 6e  to the IPK if an
26a88 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  y of the followi
26a89 6e 67 20 61 72 65 20 74 72 75 65 3a 0a 20 20 20  ng are true:.   
26a8a 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 31 29 20   **.    **   1) 
26a8b 54 68 65 72 65 20 69 73 20 61 6e 20 49 4e 54 45  There is an INTE
26a8c 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20  GER PRIMARY KEY 
26a8d 63 6f 6c 75 6d 6e 20 61 6e 64 20 74 68 65 20 46  column and the F
26a8e 4b 20 69 73 20 69 6d 70 6c 69 63 69 74 6c 79 20  K is implicitly 
26a8f 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 6d 61 70  .    **      map
26a90 70 65 64 20 74 6f 20 74 68 65 20 70 72 69 6d 61  ped to the prima
26a91 72 79 20 6b 65 79 20 6f 66 20 74 61 62 6c 65 20  ry key of table 
26a92 70 50 61 72 65 6e 74 2c 20 6f 72 0a 20 20 20 20  pParent, or.    
26a93 2a 2a 20 20 20 32 29 20 54 68 65 20 46 4b 20 69  **   2) The FK i
26a94 73 20 65 78 70 6c 69 63 69 74 6c 79 20 6d 61 70  s explicitly map
26a95 70 65 64 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20  ped to a column 
26a96 64 65 63 6c 61 72 65 64 20 61 73 20 49 4e 54 45  declared as INTE
26a97 47 45 52 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  GER.    **      
26a98 50 52 49 4d 41 52 59 20 4b 45 59 2e 0a 20 20 20  PRIMARY KEY..   
26a99 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 72   */.    if( pPar
26a9a 65 6e 74 2d 3e 69 50 4b 65 79 3e 3d 30 20 29 7b  ent->iPKey>=0 ){
26a9b 0a 20 20 20 20 20 20 69 66 28 20 21 7a 4b 65 79  .      if( !zKey
26a9c 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
26a9d 20 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 53     if( !sqlite3S
26a9e 74 72 49 43 6d 70 28 70 50 61 72 65 6e 74 2d 3e  trICmp(pParent->
26a9f 61 43 6f 6c 5b 70 50 61 72 65 6e 74 2d 3e 69 50  aCol[pParent->iP
26aa0 4b 65 79 5d 2e 7a 4e 61 6d 65 2c 20 7a 4b 65 79  Key].zName, zKey
26aa1 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  ) ) return 0;.  
26aa2 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
26aa3 70 61 69 43 6f 6c 20 29 7b 0a 20 20 20 20 61 73  paiCol ){.    as
26aa4 73 65 72 74 28 20 6e 43 6f 6c 3e 31 20 29 3b 0a  sert( nCol>1 );.
26aa5 20 20 20 20 61 69 43 6f 6c 20 3d 20 28 69 6e 74      aiCol = (int
26aa6 20 2a 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   *)sqlite3DbMall
26aa7 6f 63 52 61 77 28 70 50 61 72 73 65 2d 3e 64 62  ocRaw(pParse->db
26aa8 2c 20 6e 43 6f 6c 2a 73 69 7a 65 6f 66 28 69 6e  , nCol*sizeof(in
26aa9 74 29 29 3b 0a 20 20 20 20 69 66 28 20 21 61 69  t));.    if( !ai
26aaa 43 6f 6c 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  Col ) return 1;.
26aab 20 20 20 20 2a 70 61 69 43 6f 6c 20 3d 20 61 69      *paiCol = ai
26aac 43 6f 6c 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28  Col;.  }..  for(
26aad 70 49 64 78 3d 70 50 61 72 65 6e 74 2d 3e 70 49  pIdx=pParent->pI
26aae 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
26aaf 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  =pIdx->pNext){. 
26ab0 20 20 20 69 66 28 20 70 49 64 78 2d 3e 6e 43 6f     if( pIdx->nCo
26ab1 6c 75 6d 6e 3d 3d 6e 43 6f 6c 20 26 26 20 70 49  lumn==nCol && pI
26ab2 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f  dx->onError!=OE_
26ab3 4e 6f 6e 65 20 29 7b 20 0a 20 20 20 20 20 20 2f  None ){ .      /
26ab4 2a 20 70 49 64 78 20 69 73 20 61 20 55 4e 49 51  * pIdx is a UNIQ
26ab5 55 45 20 69 6e 64 65 78 20 28 6f 72 20 61 20 50  UE index (or a P
26ab6 52 49 4d 41 52 59 20 4b 45 59 29 20 61 6e 64 20  RIMARY KEY) and 
26ab7 68 61 73 20 74 68 65 20 72 69 67 68 74 20 6e 75  has the right nu
26ab8 6d 62 65 72 0a 20 20 20 20 20 20 2a 2a 20 6f 66  mber.      ** of
26ab9 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 20 65 61 63   columns. If eac
26aba 68 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e  h indexed column
26abb 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74 6f 20   corresponds to 
26abc 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 0a 20 20  a foreign key.  
26abd 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6f 66      ** column of
26abe 20 70 46 4b 65 79 2c 20 74 68 65 6e 20 74 68 69   pFKey, then thi
26abf 73 20 69 6e 64 65 78 20 69 73 20 61 20 77 69 6e  s index is a win
26ac0 6e 65 72 2e 20 20 2a 2f 0a 0a 20 20 20 20 20 20  ner.  */..      
26ac1 69 66 28 20 7a 4b 65 79 3d 3d 30 20 29 7b 0a 20  if( zKey==0 ){. 
26ac2 20 20 20 20 20 20 20 2f 2a 20 49 66 20 7a 4b 65         /* If zKe
26ac3 79 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20  y is NULL, then 
26ac4 74 68 69 73 20 66 6f 72 65 69 67 6e 20 6b 65 79  this foreign key
26ac5 20 69 73 20 69 6d 70 6c 69 63 69 74 6c 79 20 6d   is implicitly m
26ac6 61 70 70 65 64 20 74 6f 20 0a 20 20 20 20 20 20  apped to .      
26ac7 20 20 2a 2a 20 74 68 65 20 50 52 49 4d 41 52 59    ** the PRIMARY
26ac8 20 4b 45 59 20 6f 66 20 74 61 62 6c 65 20 70 50   KEY of table pP
26ac9 61 72 65 6e 74 2e 20 54 68 65 20 50 52 49 4d 41  arent. The PRIMA
26aca 52 59 20 4b 45 59 20 69 6e 64 65 78 20 6d 61 79  RY KEY index may
26acb 20 62 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20   be .        ** 
26acc 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68  identified by th
26acd 65 20 74 65 73 74 20 28 49 6e 64 65 78 2e 61 75  e test (Index.au
26ace 74 6f 49 6e 64 65 78 3d 3d 32 29 2e 20 20 2a 2f  toIndex==2).  */
26acf 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64  .        if( pId
26ad0 78 2d 3e 61 75 74 6f 49 6e 64 65 78 3d 3d 32 20  x->autoIndex==2 
26ad1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
26ad2 20 61 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20   aiCol ){.      
26ad3 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20        int i;.   
26ad4 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30           for(i=0
26ad5 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 20 61  ; i<nCol; i++) a
26ad6 69 43 6f 6c 5b 69 5d 20 3d 20 70 46 4b 65 79 2d  iCol[i] = pFKey-
26ad7 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 3b 0a  >aCol[i].iFrom;.
26ad8 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
26ad9 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
26ada 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
26adb 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49  se{.        /* I
26adc 66 20 7a 4b 65 79 20 69 73 20 6e 6f 6e 2d 4e 55  f zKey is non-NU
26add 4c 4c 2c 20 74 68 65 6e 20 74 68 69 73 20 66 6f  LL, then this fo
26ade 72 65 69 67 6e 20 6b 65 79 20 77 61 73 20 64 65  reign key was de
26adf 63 6c 61 72 65 64 20 74 6f 0a 20 20 20 20 20 20  clared to.      
26ae0 20 20 2a 2a 20 6d 61 70 20 74 6f 20 61 6e 20 65    ** map to an e
26ae1 78 70 6c 69 63 69 74 20 6c 69 73 74 20 6f 66 20  xplicit list of 
26ae2 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 61 62 6c 65  columns in table
26ae3 20 70 50 61 72 65 6e 74 2e 20 43 68 65 63 6b 20   pParent. Check 
26ae4 69 66 20 74 68 69 73 0a 20 20 20 20 20 20 20 20  if this.        
26ae5 2a 2a 20 69 6e 64 65 78 20 6d 61 74 63 68 65 73  ** index matches
26ae6 20 74 68 6f 73 65 20 63 6f 6c 75 6d 6e 73 2e 20   those columns. 
26ae7 41 6c 73 6f 2c 20 63 68 65 63 6b 20 74 68 61 74  Also, check that
26ae8 20 74 68 65 20 69 6e 64 65 78 20 75 73 65 73 0a   the index uses.
26ae9 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64          ** the d
26aea 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e  efault collation
26aeb 20 73 65 71 75 65 6e 63 65 73 20 66 6f 72 20 65   sequences for e
26aec 61 63 68 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20  ach column. */. 
26aed 20 20 20 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b         int i, j;
26aee 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30  .        for(i=0
26aef 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  ; i<nCol; i++){.
26af0 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 43            int iC
26af1 6f 6c 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c  ol = pIdx->aiCol
26af2 75 6d 6e 5b 69 5d 3b 20 20 20 20 20 2f 2a 20 49  umn[i];     /* I
26af3 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69  ndex of column i
26af4 6e 20 70 61 72 65 6e 74 20 74 62 6c 20 2a 2f 0a  n parent tbl */.
26af5 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
26af6 7a 44 66 6c 74 43 6f 6c 6c 3b 20 20 20 20 20 20  zDfltColl;      
26af7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
26af8 65 66 2e 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 6f  ef. collation fo
26af9 72 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20  r column */.    
26afa 20 20 20 20 20 20 63 68 61 72 20 2a 7a 49 64 78        char *zIdx
26afb 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
26afc 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
26afd 6f 66 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d  of indexed colum
26afe 6e 20 2a 2f 0a 0a 20 20 20 20 20 20 20 20 20 20  n */..          
26aff 2f 2a 20 49 66 20 74 68 65 20 69 6e 64 65 78 20  /* If the index 
26b00 75 73 65 73 20 61 20 63 6f 6c 6c 61 74 69 6f 6e  uses a collation
26b01 20 73 65 71 75 65 6e 63 65 20 74 68 61 74 20 69   sequence that i
26b02 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  s different from
26b03 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68  .          ** th
26b04 65 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74  e default collat
26b05 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 6f 72  ion sequence for
26b06 20 74 68 65 20 63 6f 6c 75 6d 6e 2c 20 74 68 69   the column, thi
26b07 73 20 69 6e 64 65 78 20 69 73 0a 20 20 20 20 20  s index is.     
26b08 20 20 20 20 20 2a 2a 20 75 6e 75 73 61 62 6c 65       ** unusable
26b09 2e 20 42 61 69 6c 20 6f 75 74 20 65 61 72 6c 79  . Bail out early
26b0a 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 20   in this case.  
26b0b 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 7a 44 66  */.          zDf
26b0c 6c 74 43 6f 6c 6c 20 3d 20 70 50 61 72 65 6e 74  ltColl = pParent
26b0d 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 43 6f  ->aCol[iCol].zCo
26b0e 6c 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  ll;.          if
26b0f 28 20 21 7a 44 66 6c 74 43 6f 6c 6c 20 29 7b 0a  ( !zDfltColl ){.
26b10 20 20 20 20 20 20 20 20 20 20 20 20 7a 44 66 6c              zDfl
26b11 74 43 6f 6c 6c 20 3d 20 22 42 49 4e 41 52 59 22  tColl = "BINARY"
26b12 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
26b13 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
26b14 74 65 33 53 74 72 49 43 6d 70 28 70 49 64 78 2d  te3StrICmp(pIdx-
26b15 3e 61 7a 43 6f 6c 6c 5b 69 5d 2c 20 7a 44 66 6c  >azColl[i], zDfl
26b16 74 43 6f 6c 6c 29 20 29 20 62 72 65 61 6b 3b 0a  tColl) ) break;.
26b17 0a 20 20 20 20 20 20 20 20 20 20 7a 49 64 78 43  .          zIdxC
26b18 6f 6c 20 3d 20 70 50 61 72 65 6e 74 2d 3e 61 43  ol = pParent->aC
26b19 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a  ol[iCol].zName;.
26b1a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d            for(j=
26b1b 30 3b 20 6a 3c 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b  0; j<nCol; j++){
26b1c 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
26b1d 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
26b1e 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a  pFKey->aCol[j].z
26b1f 43 6f 6c 2c 20 7a 49 64 78 43 6f 6c 29 3d 3d 30  Col, zIdxCol)==0
26b20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
26b21 20 20 69 66 28 20 61 69 43 6f 6c 20 29 20 61 69    if( aiCol ) ai
26b22 43 6f 6c 5b 69 5d 20 3d 20 70 46 4b 65 79 2d 3e  Col[i] = pFKey->
26b23 61 43 6f 6c 5b 6a 5d 2e 69 46 72 6f 6d 3b 0a 20  aCol[j].iFrom;. 
26b24 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
26b25 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
26b26 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  }.          }.  
26b27 20 20 20 20 20 20 20 20 69 66 28 20 6a 3d 3d 6e          if( j==n
26b28 43 6f 6c 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  Col ) break;.   
26b29 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
26b2a 66 28 20 69 3d 3d 6e 43 6f 6c 20 29 20 62 72 65  f( i==nCol ) bre
26b2b 61 6b 3b 20 20 20 20 20 20 2f 2a 20 70 49 64 78  ak;      /* pIdx
26b2c 20 69 73 20 75 73 61 62 6c 65 20 2a 2f 0a 20 20   is usable */.  
26b2d 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
26b2e 0a 20 20 69 66 28 20 21 70 49 64 78 20 29 7b 0a  .  if( !pIdx ){.
26b2f 20 20 20 20 69 66 28 20 21 70 50 61 72 73 65 2d      if( !pParse-
26b30 3e 64 69 73 61 62 6c 65 54 72 69 67 67 65 72 73  >disableTriggers
26b31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
26b32 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
26b33 2c 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 6d  , "foreign key m
26b34 69 73 6d 61 74 63 68 22 29 3b 0a 20 20 20 20 7d  ismatch");.    }
26b35 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
26b36 65 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 61  ee(pParse->db, a
26b37 69 43 6f 6c 29 3b 0a 20 20 20 20 72 65 74 75 72  iCol);.    retur
26b38 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 49  n 1;.  }..  *ppI
26b39 64 78 20 3d 20 70 49 64 78 3b 0a 20 20 72 65 74  dx = pIdx;.  ret
26b3a 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
26b3b 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
26b3c 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 72   called when a r
26b3d 6f 77 20 69 73 20 69 6e 73 65 72 74 65 64 20 69  ow is inserted i
26b3e 6e 74 6f 20 6f 72 20 64 65 6c 65 74 65 64 20 66  nto or deleted f
26b3f 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 63 68 69 6c  rom the .** chil
26b40 64 20 74 61 62 6c 65 20 6f 66 20 66 6f 72 65 69  d table of forei
26b41 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
26b42 74 20 70 46 4b 65 79 2e 20 49 66 20 61 6e 20 53  t pFKey. If an S
26b43 51 4c 20 55 50 44 41 54 45 20 69 73 20 65 78 65  QL UPDATE is exe
26b44 63 75 74 65 64 20 0a 2a 2a 20 6f 6e 20 74 68 65  cuted .** on the
26b45 20 63 68 69 6c 64 20 74 61 62 6c 65 20 6f 66 20   child table of 
26b46 70 46 4b 65 79 2c 20 74 68 69 73 20 66 75 6e 63  pFKey, this func
26b47 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 20  tion is invoked 
26b48 74 77 69 63 65 20 66 6f 72 20 65 61 63 68 20 72  twice for each r
26b49 6f 77 0a 2a 2a 20 61 66 66 65 63 74 65 64 20 2d  ow.** affected -
26b4a 20 6f 6e 63 65 20 74 6f 20 22 64 65 6c 65 74 65   once to "delete
26b4b 22 20 74 68 65 20 6f 6c 64 20 72 6f 77 2c 20 61  " the old row, a
26b4c 6e 64 20 74 68 65 6e 20 61 67 61 69 6e 20 74 6f  nd then again to
26b4d 20 22 69 6e 73 65 72 74 22 20 74 68 65 0a 2a 2a   "insert" the.**
26b4e 20 6e 65 77 20 72 6f 77 2e 0a 2a 2a 0a 2a 2a 20   new row..**.** 
26b4f 45 61 63 68 20 74 69 6d 65 20 69 74 20 69 73 20  Each time it is 
26b50 63 61 6c 6c 65 64 2c 20 74 68 69 73 20 66 75 6e  called, this fun
26b51 63 74 69 6f 6e 20 67 65 6e 65 72 61 74 65 73 20  ction generates 
26b52 56 44 42 45 20 63 6f 64 65 20 74 6f 20 6c 6f 63  VDBE code to loc
26b53 61 74 65 20 74 68 65 0a 2a 2a 20 72 6f 77 20 69  ate the.** row i
26b54 6e 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62  n the parent tab
26b55 6c 65 20 74 68 61 74 20 63 6f 72 72 65 73 70 6f  le that correspo
26b56 6e 64 73 20 74 6f 20 74 68 65 20 72 6f 77 20 62  nds to the row b
26b57 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20 69 6e  eing inserted in
26b58 74 6f 20 0a 2a 2a 20 6f 72 20 64 65 6c 65 74 65  to .** or delete
26b59 64 20 66 72 6f 6d 20 74 68 65 20 63 68 69 6c 64  d from the child
26b5a 20 74 61 62 6c 65 2e 20 49 66 20 74 68 65 20 70   table. If the p
26b5b 61 72 65 6e 74 20 72 6f 77 20 63 61 6e 20 62 65  arent row can be
26b5c 20 66 6f 75 6e 64 2c 20 6e 6f 20 0a 2a 2a 20 73   found, no .** s
26b5d 70 65 63 69 61 6c 20 61 63 74 69 6f 6e 20 69 73  pecial action is
26b5e 20 74 61 6b 65 6e 2e 20 4f 74 68 65 72 77 69 73   taken. Otherwis
26b5f 65 2c 20 69 66 20 74 68 65 20 70 61 72 65 6e 74  e, if the parent
26b60 20 72 6f 77 20 63 61 6e 20 2a 6e 6f 74 2a 20 62   row can *not* b
26b61 65 0a 2a 2a 20 66 6f 75 6e 64 20 69 6e 20 74 68  e.** found in th
26b62 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 3a 0a  e parent table:.
26b63 2a 2a 0a 2a 2a 20 20 20 4f 70 65 72 61 74 69 6f  **.**   Operatio
26b64 6e 20 7c 20 46 4b 20 74 79 70 65 20 20 20 7c 20  n | FK type   | 
26b65 41 63 74 69 6f 6e 20 74 61 6b 65 6e 0a 2a 2a 20  Action taken.** 
26b66 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
26b67 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26b68 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26b69 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26b6a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20  ------------.** 
26b6b 20 20 49 4e 53 45 52 54 20 20 20 20 20 20 69 6d    INSERT      im
26b6c 6d 65 64 69 61 74 65 20 20 20 49 6e 63 72 65 6d  mediate   Increm
26b6d 65 6e 74 20 74 68 65 20 22 69 6d 6d 65 64 69 61  ent the "immedia
26b6e 74 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f  te constraint co
26b6f 75 6e 74 65 72 22 2e 0a 2a 2a 0a 2a 2a 20 20 20  unter"..**.**   
26b70 44 45 4c 45 54 45 20 20 20 20 20 20 69 6d 6d 65  DELETE      imme
26b71 64 69 61 74 65 20 20 20 44 65 63 72 65 6d 65 6e  diate   Decremen
26b72 74 20 74 68 65 20 22 69 6d 6d 65 64 69 61 74 65  t the "immediate
26b73 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e   constraint coun
26b74 74 65 72 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 4e  ter"..**.**   IN
26b75 53 45 52 54 20 20 20 20 20 20 64 65 66 65 72 72  SERT      deferr
26b76 65 64 20 20 20 20 49 6e 63 72 65 6d 65 6e 74 20  ed    Increment 
26b77 74 68 65 20 22 64 65 66 65 72 72 65 64 20 63 6f  the "deferred co
26b78 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74 65 72  nstraint counter
26b79 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 45 4c 45 54  "..**.**   DELET
26b7a 45 20 20 20 20 20 20 64 65 66 65 72 72 65 64 20  E      deferred 
26b7b 20 20 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65     Decrement the
26b7c 20 22 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74   "deferred const
26b7d 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 22 2e 0a  raint counter"..
26b7e 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 6f 70 65 72  **.** These oper
26b7f 61 74 69 6f 6e 73 20 61 72 65 20 69 64 65 6e 74  ations are ident
26b80 69 66 69 65 64 20 69 6e 20 74 68 65 20 63 6f 6d  ified in the com
26b81 6d 65 6e 74 20 61 74 20 74 68 65 20 74 6f 70 20  ment at the top 
26b82 6f 66 20 74 68 69 73 20 66 69 6c 65 20 0a 2a 2a  of this file .**
26b83 20 28 66 6b 65 79 2e 63 29 20 61 73 20 22 49 2e   (fkey.c) as "I.
26b84 31 22 20 61 6e 64 20 22 44 2e 31 22 2e 0a 2a 2f  1" and "D.1"..*/
26b85 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 6b 4c  .static void fkL
26b86 6f 6f 6b 75 70 50 61 72 65 6e 74 28 0a 20 20 50  ookupParent(.  P
26b87 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
26b88 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f       /* Parse co
26b89 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69  ntext */.  int i
26b8a 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  Db,             
26b8b 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61 74   /* Index of dat
26b8c 61 62 61 73 65 20 68 6f 75 73 69 6e 67 20 70 54  abase housing pT
26b8d 61 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  ab */.  Table *p
26b8e 54 61 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Tab,          /*
26b8f 20 50 61 72 65 6e 74 20 74 61 62 6c 65 20 6f 66   Parent table of
26b90 20 46 4b 20 70 46 4b 65 79 20 2a 2f 0a 20 20 49   FK pFKey */.  I
26b91 6e 64 65 78 20 2a 70 49 64 78 2c 20 20 20 20 20  ndex *pIdx,     
26b92 20 20 20 20 20 2f 2a 20 55 6e 69 71 75 65 20 69       /* Unique i
26b93 6e 64 65 78 20 6f 6e 20 70 61 72 65 6e 74 20 6b  ndex on parent k
26b94 65 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 54  ey columns in pT
26b95 61 62 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 46  ab */.  FKey *pF
26b96 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Key,          /*
26b97 20 46 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e   Foreign key con
26b98 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 69 6e 74  straint */.  int
26b99 20 2a 61 69 43 6f 6c 2c 20 20 20 20 20 20 20 20   *aiCol,        
26b9a 20 20 20 2f 2a 20 4d 61 70 20 66 72 6f 6d 20 70     /* Map from p
26b9b 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e  arent key column
26b9c 73 20 74 6f 20 63 68 69 6c 64 20 74 61 62 6c 65  s to child table
26b9d 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 69 6e   columns */.  in
26b9e 74 20 72 65 67 44 61 74 61 2c 20 20 20 20 20 20  t regData,      
26b9f 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
26ba0 66 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 69  f array containi
26ba1 6e 67 20 63 68 69 6c 64 20 74 61 62 6c 65 20 72  ng child table r
26ba2 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 63  ow */.  int nInc
26ba3 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  r,            /*
26ba4 20 49 6e 63 72 65 6d 65 6e 74 20 63 6f 6e 73 74   Increment const
26ba5 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 20 62 79  raint counter by
26ba6 20 74 68 69 73 20 2a 2f 0a 20 20 69 6e 74 20 69   this */.  int i
26ba7 73 49 67 6e 6f 72 65 20 20 20 20 20 20 20 20 20  sIgnore         
26ba8 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 70 72 65   /* If true, pre
26ba9 74 65 6e 64 20 70 54 61 62 20 63 6f 6e 74 61 69  tend pTab contai
26baa 6e 73 20 61 6c 6c 20 4e 55 4c 4c 20 76 61 6c 75  ns all NULL valu
26bab 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  es */.){.  int i
26bac 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
26bad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26bae 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
26baf 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 56   variable */.  V
26bb0 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33  dbe *v = sqlite3
26bb1 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
26bb2 20 20 20 20 20 20 20 20 20 2f 2a 20 56 64 62 65           /* Vdbe
26bb3 20 74 6f 20 61 64 64 20 63 6f 64 65 20 74 6f 20   to add code to 
26bb4 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 20 3d 20  */.  int iCur = 
26bb5 70 50 61 72 73 65 2d 3e 6e 54 61 62 20 2d 20 31  pParse->nTab - 1
26bb6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
26bb7 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  * Cursor number 
26bb8 74 6f 20 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20  to use */.  int 
26bb9 69 4f 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62  iOk = sqlite3Vdb
26bba 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 20 20  eMakeLabel(v);  
26bbb 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 68 65        /* jump he
26bbc 72 65 20 69 66 20 70 61 72 65 6e 74 20 6b 65 79  re if parent key
26bbd 20 66 6f 75 6e 64 20 2a 2f 0a 0a 20 20 2f 2a 20   found */..  /* 
26bbe 49 66 20 6e 49 6e 63 72 20 69 73 20 6c 65 73 73  If nIncr is less
26bbf 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e   than zero, then
26bc0 20 63 68 65 63 6b 20 61 74 20 72 75 6e 74 69 6d   check at runtim
26bc1 65 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61  e if there are a
26bc2 6e 79 0a 20 20 2a 2a 20 6f 75 74 73 74 61 6e 64  ny.  ** outstand
26bc3 69 6e 67 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  ing constraints 
26bc4 74 6f 20 72 65 73 6f 6c 76 65 2e 20 49 66 20 74  to resolve. If t
26bc5 68 65 72 65 20 61 72 65 20 6e 6f 74 2c 20 74 68  here are not, th
26bc6 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64 0a 20  ere is no need. 
26bc7 20 2a 2a 20 74 6f 20 63 68 65 63 6b 20 69 66 20   ** to check if 
26bc8 64 65 6c 65 74 69 6e 67 20 74 68 69 73 20 72 6f  deleting this ro
26bc9 77 20 72 65 73 6f 6c 76 65 73 20 61 6e 79 20 6f  w resolves any o
26bca 75 74 73 74 61 6e 64 69 6e 67 20 76 69 6f 6c 61  utstanding viola
26bcb 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  tions..  **.  **
26bcc 20 43 68 65 63 6b 20 69 66 20 61 6e 79 20 6f 66   Check if any of
26bcd 20 74 68 65 20 6b 65 79 20 63 6f 6c 75 6d 6e 73   the key columns
26bce 20 69 6e 20 74 68 65 20 63 68 69 6c 64 20 74 61   in the child ta
26bcf 62 6c 65 20 72 6f 77 20 61 72 65 20 4e 55 4c 4c  ble row are NULL
26bd0 2e 20 49 66 20 0a 20 20 2a 2a 20 61 6e 79 20 61  . If .  ** any a
26bd1 72 65 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6e  re, then the con
26bd2 73 74 72 61 69 6e 74 20 69 73 20 63 6f 6e 73 69  straint is consi
26bd3 64 65 72 65 64 20 73 61 74 69 73 66 69 65 64 2e  dered satisfied.
26bd4 20 4e 6f 20 6e 65 65 64 20 74 6f 20 0a 20 20 2a   No need to .  *
26bd5 2a 20 73 65 61 72 63 68 20 66 6f 72 20 61 20 6d  * search for a m
26bd6 61 74 63 68 69 6e 67 20 72 6f 77 20 69 6e 20 74  atching row in t
26bd7 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 2e  he parent table.
26bd8 20 20 2a 2f 0a 20 20 69 66 28 20 6e 49 6e 63 72    */.  if( nIncr
26bd9 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  <0 ){.    sqlite
26bda 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26bdb 50 5f 46 6b 49 66 5a 65 72 6f 2c 20 70 46 4b 65  P_FkIfZero, pFKe
26bdc 79 2d 3e 69 73 44 65 66 65 72 72 65 64 2c 20 69  y->isDeferred, i
26bdd 4f 6b 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  Ok);.  }.  for(i
26bde 3d 30 3b 20 69 3c 70 46 4b 65 79 2d 3e 6e 43 6f  =0; i<pFKey->nCo
26bdf 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  l; i++){.    int
26be0 20 69 52 65 67 20 3d 20 61 69 43 6f 6c 5b 69 5d   iReg = aiCol[i]
26be1 20 2b 20 72 65 67 44 61 74 61 20 2b 20 31 3b 0a   + regData + 1;.
26be2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26be3 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75  ddOp2(v, OP_IsNu
26be4 6c 6c 2c 20 69 52 65 67 2c 20 69 4f 6b 29 3b 0a  ll, iReg, iOk);.
26be5 20 20 7d 0a 0a 20 20 69 66 28 20 69 73 49 67 6e    }..  if( isIgn
26be6 6f 72 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  ore==0 ){.    if
26be7 28 20 70 49 64 78 3d 3d 30 20 29 7b 0a 20 20 20  ( pIdx==0 ){.   
26be8 20 20 20 2f 2a 20 49 66 20 70 49 64 78 20 69 73     /* If pIdx is
26be9 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20   NULL, then the 
26bea 70 61 72 65 6e 74 20 6b 65 79 20 69 73 20 74 68  parent key is th
26beb 65 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  e INTEGER PRIMAR
26bec 59 20 4b 45 59 0a 20 20 20 20 20 20 2a 2a 20 63  Y KEY.      ** c
26bed 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 70 61 72  olumn of the par
26bee 65 6e 74 20 74 61 62 6c 65 20 28 74 61 62 6c 65  ent table (table
26bef 20 70 54 61 62 29 2e 20 20 2a 2f 0a 20 20 20 20   pTab).  */.    
26bf0 20 20 69 6e 74 20 69 4d 75 73 74 42 65 49 6e 74    int iMustBeInt
26bf1 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
26bf2 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 4d 75  /* Address of Mu
26bf3 73 74 42 65 49 6e 74 20 69 6e 73 74 72 75 63 74  stBeInt instruct
26bf4 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ion */.      int
26bf5 20 72 65 67 54 65 6d 70 20 3d 20 73 71 6c 69 74   regTemp = sqlit
26bf6 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
26bf7 72 73 65 29 3b 0a 20 20 0a 20 20 20 20 20 20 2f  rse);.  .      /
26bf8 2a 20 49 6e 76 6f 6b 65 20 4d 75 73 74 42 65 49  * Invoke MustBeI
26bf9 6e 74 20 74 6f 20 63 6f 65 72 63 65 20 74 68 65  nt to coerce the
26bfa 20 63 68 69 6c 64 20 6b 65 79 20 76 61 6c 75 65   child key value
26bfb 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 20 28   to an integer (
26bfc 69 2e 65 2e 20 0a 20 20 20 20 20 20 2a 2a 20 61  i.e. .      ** a
26bfd 70 70 6c 79 20 74 68 65 20 61 66 66 69 6e 69 74  pply the affinit
26bfe 79 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20  y of the parent 
26bff 6b 65 79 29 2e 20 49 66 20 74 68 69 73 20 66 61  key). If this fa
26c00 69 6c 73 2c 20 74 68 65 6e 20 74 68 65 72 65 0a  ils, then there.
26c01 20 20 20 20 20 20 2a 2a 20 69 73 20 6e 6f 20 6d        ** is no m
26c02 61 74 63 68 69 6e 67 20 70 61 72 65 6e 74 20 6b  atching parent k
26c03 65 79 2e 20 42 65 66 6f 72 65 20 75 73 69 6e 67  ey. Before using
26c04 20 4d 75 73 74 42 65 49 6e 74 2c 20 6d 61 6b 65   MustBeInt, make
26c05 20 61 20 63 6f 70 79 20 6f 66 0a 20 20 20 20 20   a copy of.     
26c06 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 2e 20 4f   ** the value. O
26c07 74 68 65 72 77 69 73 65 2c 20 74 68 65 20 76 61  therwise, the va
26c08 6c 75 65 20 69 6e 73 65 72 74 65 64 20 69 6e 74  lue inserted int
26c09 6f 20 74 68 65 20 63 68 69 6c 64 20 6b 65 79 20  o the child key 
26c0a 63 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 2a 2a 20  column.      ** 
26c0b 77 69 6c 6c 20 68 61 76 65 20 49 4e 54 45 47 45  will have INTEGE
26c0c 52 20 61 66 66 69 6e 69 74 79 20 61 70 70 6c 69  R affinity appli
26c0d 65 64 20 74 6f 20 69 74 2c 20 77 68 69 63 68 20  ed to it, which 
26c0e 6d 61 79 20 6e 6f 74 20 62 65 20 63 6f 72 72 65  may not be corre
26c0f 63 74 2e 20 20 2a 2f 0a 20 20 20 20 20 20 73 71  ct.  */.      sq
26c10 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26c11 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 61 69 43  v, OP_SCopy, aiC
26c12 6f 6c 5b 30 5d 2b 31 2b 72 65 67 44 61 74 61 2c  ol[0]+1+regData,
26c13 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20 20   regTemp);.     
26c14 20 69 4d 75 73 74 42 65 49 6e 74 20 3d 20 73 71   iMustBeInt = sq
26c15 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26c16 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c  v, OP_MustBeInt,
26c17 20 72 65 67 54 65 6d 70 2c 20 30 29 3b 0a 20 20   regTemp, 0);.  
26c18 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65  .      /* If the
26c19 20 70 61 72 65 6e 74 20 74 61 62 6c 65 20 69 73   parent table is
26c1a 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
26c1b 20 63 68 69 6c 64 20 74 61 62 6c 65 2c 20 61 6e   child table, an
26c1c 64 20 77 65 20 61 72 65 20 61 62 6f 75 74 0a 20  d we are about. 
26c1d 20 20 20 20 20 2a 2a 20 74 6f 20 69 6e 63 72 65       ** to incre
26c1e 6d 65 6e 74 20 74 68 65 20 63 6f 6e 73 74 72 61  ment the constra
26c1f 69 6e 74 2d 63 6f 75 6e 74 65 72 20 28 69 2e 65  int-counter (i.e
26c20 2e 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53  . this is an INS
26c21 45 52 54 20 6f 70 65 72 61 74 69 6f 6e 29 2c 0a  ERT operation),.
26c22 20 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 63 68        ** then ch
26c23 65 63 6b 20 69 66 20 74 68 65 20 72 6f 77 20 62  eck if the row b
26c24 65 69 6e 67 20 69 6e 73 65 72 74 65 64 20 6d 61  eing inserted ma
26c25 74 63 68 65 73 20 69 74 73 65 6c 66 2e 20 49 66  tches itself. If
26c26 20 73 6f 2c 20 64 6f 20 6e 6f 74 0a 20 20 20 20   so, do not.    
26c27 20 20 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 20 74    ** increment t
26c28 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 2d 63 6f  he constraint-co
26c29 75 6e 74 65 72 2e 20 20 2a 2f 0a 20 20 20 20 20  unter.  */.     
26c2a 20 69 66 28 20 70 54 61 62 3d 3d 70 46 4b 65 79   if( pTab==pFKey
26c2b 2d 3e 70 46 72 6f 6d 20 26 26 20 6e 49 6e 63 72  ->pFrom && nIncr
26c2c 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ==1 ){.        s
26c2d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
26c2e 28 76 2c 20 4f 50 5f 45 71 2c 20 72 65 67 44 61  (v, OP_Eq, regDa
26c2f 74 61 2c 20 69 4f 6b 2c 20 72 65 67 54 65 6d 70  ta, iOk, regTemp
26c30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20 20  );.      }.  .  
26c31 20 20 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54      sqlite3OpenT
26c32 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 43 75  able(pParse, iCu
26c33 72 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50  r, iDb, pTab, OP
26c34 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 20 20  _OpenRead);.    
26c35 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26c36 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69  Op3(v, OP_NotExi
26c37 73 74 73 2c 20 69 43 75 72 2c 20 30 2c 20 72 65  sts, iCur, 0, re
26c38 67 54 65 6d 70 29 3b 0a 20 20 20 20 20 20 73 71  gTemp);.      sq
26c39 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
26c3a 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69  v, OP_Goto, 0, i
26c3b 4f 6b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Ok);.      sqlit
26c3c 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
26c3d 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  , sqlite3VdbeCur
26c3e 72 65 6e 74 41 64 64 72 28 76 29 2d 32 29 3b 0a  rentAddr(v)-2);.
26c3f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26c40 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 4d 75  eJumpHere(v, iMu
26c41 73 74 42 65 49 6e 74 29 3b 0a 20 20 20 20 20 20  stBeInt);.      
26c42 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
26c43 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
26c44 67 54 65 6d 70 29 3b 0a 20 20 20 20 7d 65 6c 73  gTemp);.    }els
26c45 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 43 6f  e{.      int nCo
26c46 6c 20 3d 20 70 46 4b 65 79 2d 3e 6e 43 6f 6c 3b  l = pFKey->nCol;
26c47 0a 20 20 20 20 20 20 69 6e 74 20 72 65 67 54 65  .      int regTe
26c48 6d 70 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  mp = sqlite3GetT
26c49 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
26c4a 20 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20 69 6e   nCol);.      in
26c4b 74 20 72 65 67 52 65 63 20 3d 20 73 71 6c 69 74  t regRec = sqlit
26c4c 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
26c4d 72 73 65 29 3b 0a 20 20 20 20 20 20 4b 65 79 49  rse);.      KeyI
26c4e 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69  nfo *pKey = sqli
26c4f 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28  te3IndexKeyinfo(
26c50 70 50 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 20  pParse, pIdx);. 
26c51 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56   .      sqlite3V
26c52 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
26c53 4f 70 65 6e 52 65 61 64 2c 20 69 43 75 72 2c 20  OpenRead, iCur, 
26c54 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 29  pIdx->tnum, iDb)
26c55 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
26c56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
26c57 31 2c 20 28 63 68 61 72 2a 29 70 4b 65 79 2c 20  1, (char*)pKey, 
26c58 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
26c59 46 46 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  FF);.      for(i
26c5a 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
26c5b 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
26c5c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
26c5d 50 5f 53 43 6f 70 79 2c 20 61 69 43 6f 6c 5b 69  P_SCopy, aiCol[i
26c5e 5d 2b 31 2b 72 65 67 44 61 74 61 2c 20 72 65 67  ]+1+regData, reg
26c5f 54 65 6d 70 2b 69 29 3b 0a 20 20 20 20 20 20 7d  Temp+i);.      }
26c60 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  .  .      /* If 
26c61 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65  the parent table
26c62 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
26c63 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 2c  the child table,
26c64 20 61 6e 64 20 77 65 20 61 72 65 20 61 62 6f 75   and we are abou
26c65 74 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 69 6e  t.      ** to in
26c66 63 72 65 6d 65 6e 74 20 74 68 65 20 63 6f 6e 73  crement the cons
26c67 74 72 61 69 6e 74 2d 63 6f 75 6e 74 65 72 20 28  traint-counter (
26c68 69 2e 65 2e 20 74 68 69 73 20 69 73 20 61 6e 20  i.e. this is an 
26c69 49 4e 53 45 52 54 20 6f 70 65 72 61 74 69 6f 6e  INSERT operation
26c6a 29 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 6e  ),.      ** then
26c6b 20 63 68 65 63 6b 20 69 66 20 74 68 65 20 72 6f   check if the ro
26c6c 77 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64  w being inserted
26c6d 20 6d 61 74 63 68 65 73 20 69 74 73 65 6c 66 2e   matches itself.
26c6e 20 49 66 20 73 6f 2c 20 64 6f 20 6e 6f 74 0a 20   If so, do not. 
26c6f 20 20 20 20 20 2a 2a 20 69 6e 63 72 65 6d 65 6e       ** incremen
26c70 74 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74  t the constraint
26c71 2d 63 6f 75 6e 74 65 72 2e 20 20 2a 2f 0a 20 20  -counter.  */.  
26c72 20 20 20 20 69 66 28 20 70 54 61 62 3d 3d 70 46      if( pTab==pF
26c73 4b 65 79 2d 3e 70 46 72 6f 6d 20 26 26 20 6e 49  Key->pFrom && nI
26c74 6e 63 72 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  ncr==1 ){.      
26c75 20 20 69 6e 74 20 69 4a 75 6d 70 20 3d 20 73 71    int iJump = sq
26c76 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
26c77 41 64 64 72 28 76 29 20 2b 20 6e 43 6f 6c 20 2b  Addr(v) + nCol +
26c78 20 31 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28   1;.        for(
26c79 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b  i=0; i<nCol; i++
26c7a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  ){.          int
26c7b 20 69 43 68 69 6c 64 20 3d 20 61 69 43 6f 6c 5b   iChild = aiCol[
26c7c 69 5d 2b 31 2b 72 65 67 44 61 74 61 3b 0a 20 20  i]+1+regData;.  
26c7d 20 20 20 20 20 20 20 20 69 6e 74 20 69 50 61 72          int iPar
26c7e 65 6e 74 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f  ent = pIdx->aiCo
26c7f 6c 75 6d 6e 5b 69 5d 2b 31 2b 72 65 67 44 61 74  lumn[i]+1+regDat
26c80 61 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  a;.          sql
26c81 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
26c82 2c 20 4f 50 5f 4e 65 2c 20 69 43 68 69 6c 64 2c  , OP_Ne, iChild,
26c83 20 69 4a 75 6d 70 2c 20 69 50 61 72 65 6e 74 29   iJump, iParent)
26c84 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
26c85 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26c86 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
26c87 2c 20 30 2c 20 69 4f 6b 29 3b 0a 20 20 20 20 20  , 0, iOk);.     
26c88 20 7d 0a 20 20 0a 20 20 20 20 20 20 73 71 6c 69   }.  .      sqli
26c89 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
26c8a 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20   OP_MakeRecord, 
26c8b 72 65 67 54 65 6d 70 2c 20 6e 43 6f 6c 2c 20 72  regTemp, nCol, r
26c8c 65 67 52 65 63 29 3b 0a 20 20 20 20 20 20 73 71  egRec);.      sq
26c8d 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
26c8e 34 28 76 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33  4(v, -1, sqlite3
26c8f 49 6e 64 65 78 41 66 66 69 6e 69 74 79 53 74 72  IndexAffinityStr
26c90 28 76 2c 20 70 49 64 78 29 2c 20 30 29 3b 0a 20  (v, pIdx), 0);. 
26c91 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
26c92 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75  AddOp3(v, OP_Fou
26c93 6e 64 2c 20 69 43 75 72 2c 20 69 4f 6b 2c 20 72  nd, iCur, iOk, r
26c94 65 67 52 65 63 29 3b 0a 20 20 0a 20 20 20 20 20  egRec);.  .     
26c95 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
26c96 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
26c97 65 67 52 65 63 29 3b 0a 20 20 20 20 20 20 73 71  egRec);.      sq
26c98 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
26c99 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65  Range(pParse, re
26c9a 67 54 65 6d 70 2c 20 6e 43 6f 6c 29 3b 0a 20 20  gTemp, nCol);.  
26c9b 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21    }.  }..  if( !
26c9c 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65  pFKey->isDeferre
26c9d 64 20 26 26 20 21 70 50 61 72 73 65 2d 3e 70 54  d && !pParse->pT
26c9e 6f 70 6c 65 76 65 6c 20 26 26 20 21 70 50 61 72  oplevel && !pPar
26c9f 73 65 2d 3e 69 73 4d 75 6c 74 69 57 72 69 74 65  se->isMultiWrite
26ca0 20 29 7b 0a 20 20 20 20 2f 2a 20 53 70 65 63 69   ){.    /* Speci
26ca1 61 6c 20 63 61 73 65 3a 20 49 66 20 74 68 69 73  al case: If this
26ca2 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20 73 74   is an INSERT st
26ca3 61 74 65 6d 65 6e 74 20 74 68 61 74 20 77 69 6c  atement that wil
26ca4 6c 20 69 6e 73 65 72 74 20 65 78 61 63 74 6c 79  l insert exactly
26ca5 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 72 6f 77 20  .    ** one row 
26ca6 69 6e 74 6f 20 74 68 65 20 74 61 62 6c 65 2c 20  into the table, 
26ca7 72 61 69 73 65 20 61 20 63 6f 6e 73 74 72 61 69  raise a constrai
26ca8 6e 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 69  nt immediately i
26ca9 6e 73 74 65 61 64 20 6f 66 0a 20 20 20 20 2a 2a  nstead of.    **
26caa 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 61 20   incrementing a 
26cab 63 6f 75 6e 74 65 72 2e 20 54 68 69 73 20 69 73  counter. This is
26cac 20 6e 65 63 65 73 73 61 72 79 20 61 73 20 74 68   necessary as th
26cad 65 20 56 4d 20 63 6f 64 65 20 69 73 20 62 65 69  e VM code is bei
26cae 6e 67 0a 20 20 20 20 2a 2a 20 67 65 6e 65 72 61  ng.    ** genera
26caf 74 65 64 20 66 6f 72 20 77 69 6c 6c 20 6e 6f 74  ted for will not
26cb0 20 6f 70 65 6e 20 61 20 73 74 61 74 65 6d 65 6e   open a statemen
26cb1 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20  t transaction.  
26cb2 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  */.    assert( n
26cb3 49 6e 63 72 3d 3d 31 20 29 3b 0a 20 20 20 20 73  Incr==1 );.    s
26cb4 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72  qlite3HaltConstr
26cb5 61 69 6e 74 28 0a 20 20 20 20 20 20 20 20 70 50  aint(.        pP
26cb6 61 72 73 65 2c 20 4f 45 5f 41 62 6f 72 74 2c 20  arse, OE_Abort, 
26cb7 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e  "foreign key con
26cb8 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 22 2c  straint failed",
26cb9 20 50 34 5f 53 54 41 54 49 43 0a 20 20 20 20 29   P4_STATIC.    )
26cba 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  ;.  }else{.    i
26cbb 66 28 20 6e 49 6e 63 72 3e 30 20 26 26 20 70 46  f( nIncr>0 && pF
26cbc 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64 3d  Key->isDeferred=
26cbd 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
26cbe 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c  te3ParseToplevel
26cbf 28 70 50 61 72 73 65 29 2d 3e 6d 61 79 41 62 6f  (pParse)->mayAbo
26cc0 72 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  rt = 1;.    }.  
26cc1 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26cc2 4f 70 32 28 76 2c 20 4f 50 5f 46 6b 43 6f 75 6e  Op2(v, OP_FkCoun
26cc3 74 65 72 2c 20 70 46 4b 65 79 2d 3e 69 73 44 65  ter, pFKey->isDe
26cc4 66 65 72 72 65 64 2c 20 6e 49 6e 63 72 29 3b 0a  ferred, nIncr);.
26cc5 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 56 64    }..  sqlite3Vd
26cc6 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
26cc7 2c 20 69 4f 6b 29 3b 0a 20 20 73 71 6c 69 74 65  , iOk);.  sqlite
26cc8 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
26cc9 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 29 3b 0a  P_Close, iCur);.
26cca 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
26ccb 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
26ccc 20 74 6f 20 67 65 6e 65 72 61 74 65 20 63 6f 64   to generate cod
26ccd 65 20 65 78 65 63 75 74 65 64 20 77 68 65 6e 20  e executed when 
26cce 61 20 72 6f 77 20 69 73 20 64 65 6c 65 74 65 64  a row is deleted
26ccf 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 70 61 72  .** from the par
26cd0 65 6e 74 20 74 61 62 6c 65 20 6f 66 20 66 6f 72  ent table of for
26cd1 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
26cd2 69 6e 74 20 70 46 4b 65 79 20 61 6e 64 2c 20 69  int pFKey and, i
26cd3 66 20 70 46 4b 65 79 20 69 73 20 0a 2a 2a 20 64  f pFKey is .** d
26cd4 65 66 65 72 72 65 64 2c 20 77 68 65 6e 20 61 20  eferred, when a 
26cd5 72 6f 77 20 69 73 20 69 6e 73 65 72 74 65 64 20  row is inserted 
26cd6 69 6e 74 6f 20 74 68 65 20 73 61 6d 65 20 74 61  into the same ta
26cd7 62 6c 65 2e 20 57 68 65 6e 20 67 65 6e 65 72 61  ble. When genera
26cd8 74 69 6e 67 0a 2a 2a 20 63 6f 64 65 20 66 6f 72  ting.** code for
26cd9 20 61 6e 20 53 51 4c 20 55 50 44 41 54 45 20 6f   an SQL UPDATE o
26cda 70 65 72 61 74 69 6f 6e 2c 20 74 68 69 73 20 66  peration, this f
26cdb 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 63  unction may be c
26cdc 61 6c 6c 65 64 20 74 77 69 63 65 20 2d 0a 2a 2a  alled twice -.**
26cdd 20 6f 6e 63 65 20 74 6f 20 22 64 65 6c 65 74 65   once to "delete
26cde 22 20 74 68 65 20 6f 6c 64 20 72 6f 77 20 61 6e  " the old row an
26cdf 64 20 6f 6e 63 65 20 74 6f 20 22 69 6e 73 65 72  d once to "inser
26ce0 74 22 20 74 68 65 20 6e 65 77 20 72 6f 77 2e 0a  t" the new row..
26ce1 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67  **.** The code g
26ce2 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 69 73  enerated by this
26ce3 20 66 75 6e 63 74 69 6f 6e 20 73 63 61 6e 73 20   function scans 
26ce4 74 68 72 6f 75 67 68 20 74 68 65 20 72 6f 77 73  through the rows
26ce5 20 69 6e 20 74 68 65 20 63 68 69 6c 64 0a 2a 2a   in the child.**
26ce6 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f 72 72   table that corr
26ce7 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 20 70 61  espond to the pa
26ce8 72 65 6e 74 20 74 61 62 6c 65 20 72 6f 77 20 62  rent table row b
26ce9 65 69 6e 67 20 64 65 6c 65 74 65 64 20 6f 72 20  eing deleted or 
26cea 69 6e 73 65 72 74 65 64 2e 0a 2a 2a 20 46 6f 72  inserted..** For
26ceb 20 65 61 63 68 20 63 68 69 6c 64 20 72 6f 77 20   each child row 
26cec 66 6f 75 6e 64 2c 20 6f 6e 65 20 6f 66 20 74 68  found, one of th
26ced 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 63 74 69  e following acti
26cee 6f 6e 73 20 69 73 20 74 61 6b 65 6e 3a 0a 2a 2a  ons is taken:.**
26cef 0a 2a 2a 20 20 20 4f 70 65 72 61 74 69 6f 6e 20  .**   Operation 
26cf0 7c 20 46 4b 20 74 79 70 65 20 20 20 7c 20 41 63  | FK type   | Ac
26cf1 74 69 6f 6e 20 74 61 6b 65 6e 0a 2a 2a 20 20 20  tion taken.**   
26cf2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26cf3 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26cf4 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26cf5 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26cf6 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20  ----------.**   
26cf7 44 45 4c 45 54 45 20 20 20 20 20 20 69 6d 6d 65  DELETE      imme
26cf8 64 69 61 74 65 20 20 20 49 6e 63 72 65 6d 65 6e  diate   Incremen
26cf9 74 20 74 68 65 20 22 69 6d 6d 65 64 69 61 74 65  t the "immediate
26cfa 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e   constraint coun
26cfb 74 65 72 22 2e 0a 2a 2a 20 20 20 20 20 20 20 20  ter"..**        
26cfc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26cfd 20 20 20 4f 72 2c 20 69 66 20 74 68 65 20 4f 4e     Or, if the ON
26cfe 20 28 55 50 44 41 54 45 7c 44 45 4c 45 54 45 29   (UPDATE|DELETE)
26cff 20 61 63 74 69 6f 6e 20 69 73 20 52 45 53 54 52   action is RESTR
26d00 49 43 54 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  ICT,.**         
26d01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d02 20 20 74 68 72 6f 77 20 61 20 22 66 6f 72 65 69    throw a "forei
26d03 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
26d04 74 20 66 61 69 6c 65 64 22 20 65 78 63 65 70 74  t failed" except
26d05 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 53  ion..**.**   INS
26d06 45 52 54 20 20 20 20 20 20 69 6d 6d 65 64 69 61  ERT      immedia
26d07 74 65 20 20 20 44 65 63 72 65 6d 65 6e 74 20 74  te   Decrement t
26d08 68 65 20 22 69 6d 6d 65 64 69 61 74 65 20 63 6f  he "immediate co
26d09 6e 73 74 72 61 69 6e 74 20 63 6f 75 6e 74 65 72  nstraint counter
26d0a 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 44 45 4c 45 54  "..**.**   DELET
26d0b 45 20 20 20 20 20 20 64 65 66 65 72 72 65 64 20  E      deferred 
26d0c 20 20 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65     Increment the
26d0d 20 22 64 65 66 65 72 72 65 64 20 63 6f 6e 73 74   "deferred const
26d0e 72 61 69 6e 74 20 63 6f 75 6e 74 65 72 22 2e 0a  raint counter"..
26d0f 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
26d10 20 20 20 20 20 20 20 20 20 20 20 20 20 4f 72 2c               Or,
26d11 20 69 66 20 74 68 65 20 4f 4e 20 28 55 50 44 41   if the ON (UPDA
26d12 54 45 7c 44 45 4c 45 54 45 29 20 61 63 74 69 6f  TE|DELETE) actio
26d13 6e 20 69 73 20 52 45 53 54 52 49 43 54 2c 0a 2a  n is RESTRICT,.*
26d14 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
26d15 20 20 20 20 20 20 20 20 20 20 20 20 74 68 72 6f              thro
26d16 77 20 61 20 22 66 6f 72 65 69 67 6e 20 6b 65 79  w a "foreign key
26d17 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c   constraint fail
26d18 65 64 22 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a  ed" exception..*
26d19 2a 0a 2a 2a 20 20 20 49 4e 53 45 52 54 20 20 20  *.**   INSERT   
26d1a 20 20 20 64 65 66 65 72 72 65 64 20 20 20 20 44     deferred    D
26d1b 65 63 72 65 6d 65 6e 74 20 74 68 65 20 22 64 65  ecrement the "de
26d1c 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69 6e  ferred constrain
26d1d 74 20 63 6f 75 6e 74 65 72 22 2e 0a 2a 2a 0a 2a  t counter"..**.*
26d1e 2a 20 54 68 65 73 65 20 6f 70 65 72 61 74 69 6f  * These operatio
26d1f 6e 73 20 61 72 65 20 69 64 65 6e 74 69 66 69 65  ns are identifie
26d20 64 20 69 6e 20 74 68 65 20 63 6f 6d 6d 65 6e 74  d in the comment
26d21 20 61 74 20 74 68 65 20 74 6f 70 20 6f 66 20 74   at the top of t
26d22 68 69 73 20 66 69 6c 65 20 0a 2a 2a 20 28 66 6b  his file .** (fk
26d23 65 79 2e 63 29 20 61 73 20 22 49 2e 32 22 20 61  ey.c) as "I.2" a
26d24 6e 64 20 22 44 2e 32 22 2e 0a 2a 2f 0a 73 74 61  nd "D.2"..*/.sta
26d25 74 69 63 20 76 6f 69 64 20 66 6b 53 63 61 6e 43  tic void fkScanC
26d26 68 69 6c 64 72 65 6e 28 0a 20 20 50 61 72 73 65  hildren(.  Parse
26d27 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
26d28 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
26d29 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  rse context */. 
26d2a 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20   SrcList *pSrc, 
26d2b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d2c 20 2f 2a 20 53 72 63 4c 69 73 74 20 63 6f 6e 74   /* SrcList cont
26d2d 61 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65  aining the table
26d2e 20 74 6f 20 73 63 61 6e 20 2a 2f 0a 20 20 54 61   to scan */.  Ta
26d2f 62 6c 65 20 2a 70 54 61 62 2c 0a 20 20 49 6e 64  ble *pTab,.  Ind
26d30 65 78 20 2a 70 49 64 78 2c 20 20 20 20 20 20 20  ex *pIdx,       
26d31 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26d32 46 6f 72 65 69 67 6e 20 6b 65 79 20 69 6e 64 65  Foreign key inde
26d33 78 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 46 4b  x */.  FKey *pFK
26d34 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ey,             
26d35 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 65 69 67         /* Foreig
26d36 6e 20 6b 65 79 20 72 65 6c 61 74 69 6f 6e 73 68  n key relationsh
26d37 69 70 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 69 43  ip */.  int *aiC
26d38 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ol,             
26d39 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 70 20 66          /* Map f
26d3a 72 6f 6d 20 70 49 64 78 20 63 6f 6c 73 20 74 6f  rom pIdx cols to
26d3b 20 63 68 69 6c 64 20 74 61 62 6c 65 20 63 6f 6c   child table col
26d3c 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61  s */.  int regDa
26d3d 74 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ta,             
26d3e 20 20 20 20 20 20 20 2f 2a 20 52 65 66 65 72 65         /* Refere
26d3f 6e 63 65 64 20 74 61 62 6c 65 20 64 61 74 61 20  nced table data 
26d40 73 74 61 72 74 73 20 68 65 72 65 20 2a 2f 0a 20  starts here */. 
26d41 20 69 6e 74 20 6e 49 6e 63 72 20 20 20 20 20 20   int nIncr      
26d42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26d43 20 2f 2a 20 41 6d 6f 75 6e 74 20 74 6f 20 69 6e   /* Amount to in
26d44 63 72 65 6d 65 6e 74 20 64 65 66 65 72 72 65 64  crement deferred
26d45 20 63 6f 75 6e 74 65 72 20 62 79 20 2a 2f 0a 29   counter by */.)
26d46 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
26d47 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20  = pParse->db;   
26d48 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
26d49 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74 20  handle */.  int 
26d4a 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
26d4b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
26d4c 74 65 72 61 74 6f 72 20 76 61 72 69 61 62 6c 65  terator variable
26d4d 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65   */.  Expr *pWhe
26d4e 72 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  re = 0;         
26d4f 20 20 20 20 20 20 2f 2a 20 57 48 45 52 45 20 63        /* WHERE c
26d50 6c 61 75 73 65 20 74 6f 20 73 63 61 6e 20 77 69  lause to scan wi
26d51 74 68 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74  th */.  NameCont
26d52 65 78 74 20 73 4e 61 6d 65 43 6f 6e 74 65 78 74  ext sNameContext
26d53 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65  ;       /* Conte
26d54 78 74 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c  xt used to resol
26d55 76 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  ve WHERE clause 
26d56 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a  */.  WhereInfo *
26d57 70 57 49 6e 66 6f 3b 20 20 20 20 20 20 20 20 20  pWInfo;         
26d58 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20       /* Context 
26d59 75 73 65 64 20 62 79 20 73 71 6c 69 74 65 33 57  used by sqlite3W
26d5a 68 65 72 65 58 58 58 28 29 20 2a 2f 0a 20 20 69  hereXXX() */.  i
26d5b 6e 74 20 69 46 6b 49 66 5a 65 72 6f 20 3d 20 30  nt iFkIfZero = 0
26d5c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
26d5d 2a 20 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f  * Address of OP_
26d5e 46 6b 49 66 5a 65 72 6f 20 2a 2f 0a 20 20 56 64  FkIfZero */.  Vd
26d5f 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47  be *v = sqlite3G
26d60 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
26d61 0a 20 20 61 73 73 65 72 74 28 20 21 70 49 64 78  .  assert( !pIdx
26d62 20 7c 7c 20 70 49 64 78 2d 3e 70 54 61 62 6c 65   || pIdx->pTable
26d63 3d 3d 70 54 61 62 20 29 3b 0a 0a 20 20 69 66 28  ==pTab );..  if(
26d64 20 6e 49 6e 63 72 3c 30 20 29 7b 0a 20 20 20 20   nIncr<0 ){.    
26d65 69 46 6b 49 66 5a 65 72 6f 20 3d 20 73 71 6c 69  iFkIfZero = sqli
26d66 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
26d67 20 4f 50 5f 46 6b 49 66 5a 65 72 6f 2c 20 70 46   OP_FkIfZero, pF
26d68 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64 2c  Key->isDeferred,
26d69 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43   0);.  }..  /* C
26d6a 72 65 61 74 65 20 61 6e 20 45 78 70 72 20 6f 62  reate an Expr ob
26d6b 6a 65 63 74 20 72 65 70 72 65 73 65 6e 74 69 6e  ject representin
26d6c 67 20 61 6e 20 53 51 4c 20 65 78 70 72 65 73 73  g an SQL express
26d6d 69 6f 6e 20 6c 69 6b 65 3a 0a 20 20 2a 2a 0a 20  ion like:.  **. 
26d6e 20 2a 2a 20 20 20 3c 70 61 72 65 6e 74 2d 6b 65   **   <parent-ke
26d6f 79 31 3e 20 3d 20 3c 63 68 69 6c 64 2d 6b 65 79  y1> = <child-key
26d70 31 3e 20 41 4e 44 20 3c 70 61 72 65 6e 74 2d 6b  1> AND <parent-k
26d71 65 79 32 3e 20 3d 20 3c 63 68 69 6c 64 2d 6b 65  ey2> = <child-ke
26d72 79 32 3e 20 2e 2e 2e 0a 20 20 2a 2a 0a 20 20 2a  y2> ....  **.  *
26d73 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  * The collation 
26d74 73 65 71 75 65 6e 63 65 20 75 73 65 64 20 66 6f  sequence used fo
26d75 72 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e  r the comparison
26d76 20 73 68 6f 75 6c 64 20 62 65 20 74 68 61 74 20   should be that 
26d77 6f 66 0a 20 20 2a 2a 20 74 68 65 20 70 61 72 65  of.  ** the pare
26d78 6e 74 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 2e 20  nt key columns. 
26d79 54 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20  The affinity of 
26d7a 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20 63  the parent key c
26d7b 6f 6c 75 6d 6e 20 73 68 6f 75 6c 64 0a 20 20 2a  olumn should.  *
26d7c 2a 20 62 65 20 61 70 70 6c 69 65 64 20 74 6f 20  * be applied to 
26d7d 65 61 63 68 20 63 68 69 6c 64 20 6b 65 79 20 76  each child key v
26d7e 61 6c 75 65 20 62 65 66 6f 72 65 20 74 68 65 20  alue before the 
26d7f 63 6f 6d 70 61 72 69 73 6f 6e 20 74 61 6b 65 73  comparison takes
26d80 20 70 6c 61 63 65 2e 0a 20 20 2a 2f 0a 20 20 66   place..  */.  f
26d81 6f 72 28 69 3d 30 3b 20 69 3c 70 46 4b 65 79 2d  or(i=0; i<pFKey-
26d82 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
26d83 20 45 78 70 72 20 2a 70 4c 65 66 74 3b 20 20 20   Expr *pLeft;   
26d84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
26d85 2a 20 56 61 6c 75 65 20 66 72 6f 6d 20 70 61 72  * Value from par
26d86 65 6e 74 20 74 61 62 6c 65 20 72 6f 77 20 2a 2f  ent table row */
26d87 0a 20 20 20 20 45 78 70 72 20 2a 70 52 69 67 68  .    Expr *pRigh
26d88 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
26d89 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 72 65 66     /* Column ref
26d8a 20 74 6f 20 63 68 69 6c 64 20 74 61 62 6c 65 20   to child table 
26d8b 2a 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 71  */.    Expr *pEq
26d8c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
26d8d 20 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69       /* Expressi
26d8e 6f 6e 20 28 70 4c 65 66 74 20 3d 20 70 52 69 67  on (pLeft = pRig
26d8f 68 74 29 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ht) */.    int i
26d90 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
26d91 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
26d92 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 63  x of column in c
26d93 68 69 6c 64 20 74 61 62 6c 65 20 2a 2f 20 0a 20  hild table */ . 
26d94 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
26d95 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
26d96 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 63 6f 6c 75   /* Name of colu
26d97 6d 6e 20 69 6e 20 63 68 69 6c 64 20 74 61 62 6c  mn in child tabl
26d98 65 20 2a 2f 0a 0a 20 20 20 20 70 4c 65 66 74 20  e */..    pLeft 
26d99 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62  = sqlite3Expr(db
26d9a 2c 20 54 4b 5f 52 45 47 49 53 54 45 52 2c 20 30  , TK_REGISTER, 0
26d9b 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 66 74  );.    if( pLeft
26d9c 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 65 74   ){.      /* Set
26d9d 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   the collation s
26d9e 65 71 75 65 6e 63 65 20 61 6e 64 20 61 66 66 69  equence and affi
26d9f 6e 69 74 79 20 6f 66 20 74 68 65 20 4c 48 53 20  nity of the LHS 
26da0 6f 66 20 65 61 63 68 20 54 4b 5f 45 51 0a 20 20  of each TK_EQ.  
26da1 20 20 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f      ** expressio
26da2 6e 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74 20  n to the parent 
26da3 6b 65 79 20 63 6f 6c 75 6d 6e 20 64 65 66 61 75  key column defau
26da4 6c 74 73 2e 20 20 2a 2f 0a 20 20 20 20 20 20 69  lts.  */.      i
26da5 66 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20  f( pIdx ){.     
26da6 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b     Column *pCol;
26da7 0a 20 20 20 20 20 20 20 20 69 43 6f 6c 20 3d 20  .        iCol = 
26da8 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69  pIdx->aiColumn[i
26da9 5d 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 20  ];.        pCol 
26daa 3d 20 26 70 49 64 78 2d 3e 70 54 61 62 6c 65 2d  = &pIdx->pTable-
26dab 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20  >aCol[iCol];.   
26dac 20 20 20 20 20 70 4c 65 66 74 2d 3e 69 54 61 62       pLeft->iTab
26dad 6c 65 20 3d 20 72 65 67 44 61 74 61 2b 69 43 6f  le = regData+iCo
26dae 6c 2b 31 3b 0a 20 20 20 20 20 20 20 20 70 4c 65  l+1;.        pLe
26daf 66 74 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 70  ft->affinity = p
26db0 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 3b 0a 20  Col->affinity;. 
26db1 20 20 20 20 20 20 20 70 4c 65 66 74 2d 3e 70 43         pLeft->pC
26db2 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63  oll = sqlite3Loc
26db3 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  ateCollSeq(pPars
26db4 65 2c 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 29 3b  e, pCol->zColl);
26db5 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
26db6 20 20 20 20 20 20 70 4c 65 66 74 2d 3e 69 54 61        pLeft->iTa
26db7 62 6c 65 20 3d 20 72 65 67 44 61 74 61 3b 0a 20  ble = regData;. 
26db8 20 20 20 20 20 20 20 70 4c 65 66 74 2d 3e 61 66         pLeft->af
26db9 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f  finity = SQLITE_
26dba 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20  AFF_INTEGER;.   
26dbb 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
26dbc 43 6f 6c 20 3d 20 61 69 43 6f 6c 20 3f 20 61 69  Col = aiCol ? ai
26dbd 43 6f 6c 5b 69 5d 20 3a 20 70 46 4b 65 79 2d 3e  Col[i] : pFKey->
26dbe 61 43 6f 6c 5b 30 5d 2e 69 46 72 6f 6d 3b 0a 20  aCol[0].iFrom;. 
26dbf 20 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3e     assert( iCol>
26dc0 3d 30 20 29 3b 0a 20 20 20 20 7a 43 6f 6c 20 3d  =0 );.    zCol =
26dc1 20 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 2d 3e 61   pFKey->pFrom->a
26dc2 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b  Col[iCol].zName;
26dc3 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20 73 71  .    pRight = sq
26dc4 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b  lite3Expr(db, TK
26dc5 5f 49 44 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20  _ID, zCol);.    
26dc6 70 45 71 20 3d 20 73 71 6c 69 74 65 33 50 45 78  pEq = sqlite3PEx
26dc7 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 45 51  pr(pParse, TK_EQ
26dc8 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74 2c  , pLeft, pRight,
26dc9 20 30 29 3b 0a 20 20 20 20 70 57 68 65 72 65 20   0);.    pWhere 
26dca 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64  = sqlite3ExprAnd
26dcb 28 64 62 2c 20 70 57 68 65 72 65 2c 20 70 45 71  (db, pWhere, pEq
26dcc 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  );.  }..  /* If 
26dcd 74 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 20  the child table 
26dce 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  is the same as t
26dcf 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 2c  he parent table,
26dd0 20 61 6e 64 20 74 68 69 73 20 73 63 61 6e 0a 20   and this scan. 
26dd1 20 2a 2a 20 69 73 20 74 61 6b 69 6e 67 20 70 6c   ** is taking pl
26dd2 61 63 65 20 61 73 20 70 61 72 74 20 6f 66 20 61  ace as part of a
26dd3 20 44 45 4c 45 54 45 20 6f 70 65 72 61 74 69 6f   DELETE operatio
26dd4 6e 20 28 6f 70 65 72 61 74 69 6f 6e 20 44 2e 32  n (operation D.2
26dd5 29 2c 20 6f 6d 69 74 20 74 68 65 0a 20 20 2a 2a  ), omit the.  **
26dd6 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65 74   row being delet
26dd7 65 64 20 66 72 6f 6d 20 74 68 65 20 73 63 61 6e  ed from the scan
26dd8 20 62 79 20 61 64 64 69 6e 67 20 28 24 72 6f 77   by adding ($row
26dd9 69 64 20 21 3d 20 72 6f 77 69 64 29 20 74 6f 20  id != rowid) to 
26dda 74 68 65 20 57 48 45 52 45 20 0a 20 20 2a 2a 20  the WHERE .  ** 
26ddb 63 6c 61 75 73 65 2c 20 77 68 65 72 65 20 24 72  clause, where $r
26ddc 6f 77 69 64 20 69 73 20 74 68 65 20 72 6f 77 69  owid is the rowi
26ddd 64 20 6f 66 20 74 68 65 20 72 6f 77 20 62 65 69  d of the row bei
26dde 6e 67 20 64 65 6c 65 74 65 64 2e 20 20 2a 2f 0a  ng deleted.  */.
26ddf 20 20 69 66 28 20 70 54 61 62 3d 3d 70 46 4b 65    if( pTab==pFKe
26de0 79 2d 3e 70 46 72 6f 6d 20 26 26 20 6e 49 6e 63  y->pFrom && nInc
26de1 72 3e 30 20 29 7b 0a 20 20 20 20 45 78 70 72 20  r>0 ){.    Expr 
26de2 2a 70 45 71 3b 20 20 20 20 20 20 20 20 20 20 20  *pEq;           
26de3 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72           /* Expr
26de4 65 73 73 69 6f 6e 20 28 70 4c 65 66 74 20 3d 20  ession (pLeft = 
26de5 70 52 69 67 68 74 29 20 2a 2f 0a 20 20 20 20 45  pRight) */.    E
26de6 78 70 72 20 2a 70 4c 65 66 74 3b 20 20 20 20 20  xpr *pLeft;     
26de7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
26de8 56 61 6c 75 65 20 66 72 6f 6d 20 70 61 72 65 6e  Value from paren
26de9 74 20 74 61 62 6c 65 20 72 6f 77 20 2a 2f 0a 20  t table row */. 
26dea 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 3b     Expr *pRight;
26deb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26dec 20 2f 2a 20 43 6f 6c 75 6d 6e 20 72 65 66 20 74   /* Column ref t
26ded 6f 20 63 68 69 6c 64 20 74 61 62 6c 65 20 2a 2f  o child table */
26dee 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 73 71 6c  .    pLeft = sql
26def 69 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f  ite3Expr(db, TK_
26df0 52 45 47 49 53 54 45 52 2c 20 30 29 3b 0a 20 20  REGISTER, 0);.  
26df1 20 20 70 52 69 67 68 74 20 3d 20 73 71 6c 69 74    pRight = sqlit
26df2 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 43 4f  e3Expr(db, TK_CO
26df3 4c 55 4d 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66  LUMN, 0);.    if
26df4 28 20 70 4c 65 66 74 20 26 26 20 70 52 69 67 68  ( pLeft && pRigh
26df5 74 20 29 7b 0a 20 20 20 20 20 20 70 4c 65 66 74  t ){.      pLeft
26df6 2d 3e 69 54 61 62 6c 65 20 3d 20 72 65 67 44 61  ->iTable = regDa
26df7 74 61 3b 0a 20 20 20 20 20 20 70 4c 65 66 74 2d  ta;.      pLeft-
26df8 3e 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49  >affinity = SQLI
26df9 54 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a  TE_AFF_INTEGER;.
26dfa 20 20 20 20 20 20 70 52 69 67 68 74 2d 3e 69 54        pRight->iT
26dfb 61 62 6c 65 20 3d 20 70 53 72 63 2d 3e 61 5b 30  able = pSrc->a[0
26dfc 5d 2e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20  ].iCursor;.     
26dfd 20 70 52 69 67 68 74 2d 3e 69 43 6f 6c 75 6d 6e   pRight->iColumn
26dfe 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20   = -1;.    }.   
26dff 20 70 45 71 20 3d 20 73 71 6c 69 74 65 33 50 45   pEq = sqlite3PE
26e00 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e  xpr(pParse, TK_N
26e01 45 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68 74  E, pLeft, pRight
26e02 2c 20 30 29 3b 0a 20 20 20 20 70 57 68 65 72 65  , 0);.    pWhere
26e03 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e   = sqlite3ExprAn
26e04 64 28 64 62 2c 20 70 57 68 65 72 65 2c 20 70 45  d(db, pWhere, pE
26e05 71 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65  q);.  }..  /* Re
26e06 73 6f 6c 76 65 20 74 68 65 20 72 65 66 65 72 65  solve the refere
26e07 6e 63 65 73 20 69 6e 20 74 68 65 20 57 48 45 52  nces in the WHER
26e08 45 20 63 6c 61 75 73 65 2e 20 2a 2f 0a 20 20 6d  E clause. */.  m
26e09 65 6d 73 65 74 28 26 73 4e 61 6d 65 43 6f 6e 74  emset(&sNameCont
26e0a 65 78 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4e  ext, 0, sizeof(N
26e0b 61 6d 65 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20  ameContext));.  
26e0c 73 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 70 53 72  sNameContext.pSr
26e0d 63 4c 69 73 74 20 3d 20 70 53 72 63 3b 0a 20 20  cList = pSrc;.  
26e0e 73 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 70 50 61  sNameContext.pPa
26e0f 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20  rse = pParse;.  
26e10 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
26e11 70 72 4e 61 6d 65 73 28 26 73 4e 61 6d 65 43 6f  prNames(&sNameCo
26e12 6e 74 65 78 74 2c 20 70 57 68 65 72 65 29 3b 0a  ntext, pWhere);.
26e13 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 56 44 42  .  /* Create VDB
26e14 45 20 74 6f 20 6c 6f 6f 70 20 74 68 72 6f 75 67  E to loop throug
26e15 68 20 74 68 65 20 65 6e 74 72 69 65 73 20 69 6e  h the entries in
26e16 20 70 53 72 63 20 74 68 61 74 20 6d 61 74 63 68   pSrc that match
26e17 20 74 68 65 20 57 48 45 52 45 0a 20 20 2a 2a 20   the WHERE.  ** 
26e18 63 6c 61 75 73 65 2e 20 49 66 20 74 68 65 20 63  clause. If the c
26e19 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 6e 6f 74  onstraint is not
26e1a 20 64 65 66 65 72 72 65 64 2c 20 74 68 72 6f 77   deferred, throw
26e1b 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20 66 6f   an exception fo
26e1c 72 0a 20 20 2a 2a 20 65 61 63 68 20 72 6f 77 20  r.  ** each row 
26e1d 66 6f 75 6e 64 2e 20 4f 74 68 65 72 77 69 73 65  found. Otherwise
26e1e 2c 20 66 6f 72 20 64 65 66 65 72 72 65 64 20 63  , for deferred c
26e1f 6f 6e 73 74 72 61 69 6e 74 73 2c 20 69 6e 63 72  onstraints, incr
26e20 65 6d 65 6e 74 20 74 68 65 0a 20 20 2a 2a 20 64  ement the.  ** d
26e21 65 66 65 72 72 65 64 20 63 6f 6e 73 74 72 61 69  eferred constrai
26e22 6e 74 20 63 6f 75 6e 74 65 72 20 62 79 20 6e 49  nt counter by nI
26e23 6e 63 72 20 66 6f 72 20 65 61 63 68 20 72 6f 77  ncr for each row
26e24 20 73 65 6c 65 63 74 65 64 2e 20 20 2a 2f 0a 20   selected.  */. 
26e25 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65   pWInfo = sqlite
26e26 33 57 68 65 72 65 42 65 67 69 6e 28 70 50 61 72  3WhereBegin(pPar
26e27 73 65 2c 20 70 53 72 63 2c 20 70 57 68 65 72 65  se, pSrc, pWhere
26e28 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 6e  , 0, 0);.  if( n
26e29 49 6e 63 72 3e 30 20 26 26 20 70 46 4b 65 79 2d  Incr>0 && pFKey-
26e2a 3e 69 73 44 65 66 65 72 72 65 64 3d 3d 30 20 29  >isDeferred==0 )
26e2b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 72  {.    sqlite3Par
26e2c 73 65 54 6f 70 6c 65 76 65 6c 28 70 50 61 72 73  seToplevel(pPars
26e2d 65 29 2d 3e 6d 61 79 41 62 6f 72 74 20 3d 20 31  e)->mayAbort = 1
26e2e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  ;.  }.  sqlite3V
26e2f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
26e30 46 6b 43 6f 75 6e 74 65 72 2c 20 70 46 4b 65 79  FkCounter, pFKey
26e31 2d 3e 69 73 44 65 66 65 72 72 65 64 2c 20 6e 49  ->isDeferred, nI
26e32 6e 63 72 29 3b 0a 20 20 69 66 28 20 70 57 49 6e  ncr);.  if( pWIn
26e33 66 6f 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  fo ){.    sqlite
26e34 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f  3WhereEnd(pWInfo
26e35 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65  );.  }..  /* Cle
26e36 61 6e 20 75 70 20 74 68 65 20 57 48 45 52 45 20  an up the WHERE 
26e37 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 75 63 74  clause construct
26e38 65 64 20 61 62 6f 76 65 2e 20 2a 2f 0a 20 20 73  ed above. */.  s
26e39 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
26e3a 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20  (db, pWhere);.  
26e3b 69 66 28 20 69 46 6b 49 66 5a 65 72 6f 20 29 7b  if( iFkIfZero ){
26e3c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
26e3d 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 46 6b 49  JumpHere(v, iFkI
26e3e 66 5a 65 72 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  fZero);.  }.}../
26e3f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
26e40 6f 6e 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  on returns a poi
26e41 6e 74 65 72 20 74 6f 20 74 68 65 20 68 65 61 64  nter to the head
26e42 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73   of a linked lis
26e43 74 20 6f 66 20 46 4b 0a 2a 2a 20 63 6f 6e 73 74  t of FK.** const
26e44 72 61 69 6e 74 73 20 66 6f 72 20 77 68 69 63 68  raints for which
26e45 20 74 61 62 6c 65 20 70 54 61 62 20 69 73 20 74   table pTab is t
26e46 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 2e  he parent table.
26e47 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a   For example,.**
26e48 20 67 69 76 65 6e 20 74 68 65 20 66 6f 6c 6c 6f   given the follo
26e49 77 69 6e 67 20 73 63 68 65 6d 61 3a 0a 2a 2a 0a  wing schema:.**.
26e4a 2a 2a 20 20 20 43 52 45 41 54 45 20 54 41 42 4c  **   CREATE TABL
26e4b 45 20 74 31 28 61 20 50 52 49 4d 41 52 59 20 4b  E t1(a PRIMARY K
26e4c 45 59 29 3b 0a 2a 2a 20 20 20 43 52 45 41 54 45  EY);.**   CREATE
26e4d 20 54 41 42 4c 45 20 74 32 28 62 20 52 45 46 45   TABLE t2(b REFE
26e4e 52 45 4e 43 45 53 20 74 31 28 61 29 3b 0a 2a 2a  RENCES t1(a);.**
26e4f 0a 2a 2a 20 43 61 6c 6c 69 6e 67 20 74 68 69 73  .** Calling this
26e50 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74   function with t
26e51 61 62 6c 65 20 22 74 31 22 20 61 73 20 61 6e 20  able "t1" as an 
26e52 61 72 67 75 6d 65 6e 74 20 72 65 74 75 72 6e 73  argument returns
26e53 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f   a pointer.** to
26e54 20 74 68 65 20 46 4b 65 79 20 73 74 72 75 63 74   the FKey struct
26e55 75 72 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67  ure representing
26e56 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79   the foreign key
26e57 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 74   constraint on t
26e58 61 62 6c 65 0a 2a 2a 20 22 74 32 22 2e 20 43 61  able.** "t2". Ca
26e59 6c 6c 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74  lling this funct
26e5a 69 6f 6e 20 77 69 74 68 20 22 74 32 22 20 61 73  ion with "t2" as
26e5b 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 77 6f   the argument wo
26e5c 75 6c 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20  uld return a.** 
26e5d 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 28 61 73  NULL pointer (as
26e5e 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 46 4b   there are no FK
26e5f 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 66 6f 72   constraints for
26e60 20 77 68 69 63 68 20 74 32 20 69 73 20 74 68 65   which t2 is the
26e61 20 70 61 72 65 6e 74 0a 2a 2a 20 74 61 62 6c 65   parent.** table
26e62 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
26e63 56 41 54 45 20 46 4b 65 79 20 2a 73 71 6c 69 74  VATE FKey *sqlit
26e64 65 33 46 6b 52 65 66 65 72 65 6e 63 65 73 28 54  e3FkReferences(T
26e65 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 69  able *pTab){.  i
26e66 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74  nt nName = sqlit
26e67 65 33 53 74 72 6c 65 6e 33 30 28 70 54 61 62 2d  e3Strlen30(pTab-
26e68 3e 7a 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72  >zName);.  retur
26e69 6e 20 28 46 4b 65 79 20 2a 29 73 71 6c 69 74 65  n (FKey *)sqlite
26e6a 33 48 61 73 68 46 69 6e 64 28 26 70 54 61 62 2d  3HashFind(&pTab-
26e6b 3e 70 53 63 68 65 6d 61 2d 3e 66 6b 65 79 48 61  >pSchema->fkeyHa
26e6c 73 68 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  sh, pTab->zName,
26e6d 20 6e 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   nName);.}../*.*
26e6e 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67  * The second arg
26e6f 75 6d 65 6e 74 20 69 73 20 61 20 54 72 69 67 67  ument is a Trigg
26e70 65 72 20 73 74 72 75 63 74 75 72 65 20 61 6c 6c  er structure all
26e71 6f 63 61 74 65 64 20 62 79 20 74 68 65 20 0a 2a  ocated by the .*
26e72 2a 20 66 6b 41 63 74 69 6f 6e 54 72 69 67 67 65  * fkActionTrigge
26e73 72 28 29 20 72 6f 75 74 69 6e 65 2e 20 54 68 69  r() routine. Thi
26e74 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 6c 65 74  s function delet
26e75 65 73 20 74 68 65 20 54 72 69 67 67 65 72 20 73  es the Trigger s
26e76 74 72 75 63 74 75 72 65 0a 2a 2a 20 61 6e 64 20  tructure.** and 
26e77 61 6c 6c 20 6f 66 20 69 74 73 20 73 75 62 2d 63  all of its sub-c
26e78 6f 6d 70 6f 6e 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  omponents..**.**
26e79 20 54 68 65 20 54 72 69 67 67 65 72 20 73 74 72   The Trigger str
26e7a 75 63 74 75 72 65 20 6f 72 20 61 6e 79 20 6f 66  ucture or any of
26e7b 20 69 74 73 20 73 75 62 2d 63 6f 6d 70 6f 6e 65   its sub-compone
26e7c 6e 74 73 20 6d 61 79 20 62 65 20 61 6c 6c 6f 63  nts may be alloc
26e7d 61 74 65 64 20 66 72 6f 6d 0a 2a 2a 20 74 68 65  ated from.** the
26e7e 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65   lookaside buffe
26e7f 72 20 62 65 6c 6f 6e 67 69 6e 67 20 74 6f 20 64  r belonging to d
26e80 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64  atabase handle d
26e81 62 4d 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  bMem..*/.static 
26e82 76 6f 69 64 20 66 6b 54 72 69 67 67 65 72 44 65  void fkTriggerDe
26e83 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62  lete(sqlite3 *db
26e84 4d 65 6d 2c 20 54 72 69 67 67 65 72 20 2a 70 29  Mem, Trigger *p)
26e85 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  {.  if( p ){.   
26e86 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53   TriggerStep *pS
26e87 74 65 70 20 3d 20 70 2d 3e 73 74 65 70 5f 6c 69  tep = p->step_li
26e88 73 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  st;.    sqlite3E
26e89 78 70 72 44 65 6c 65 74 65 28 64 62 4d 65 6d 2c  xprDelete(dbMem,
26e8a 20 70 53 74 65 70 2d 3e 70 57 68 65 72 65 29 3b   pStep->pWhere);
26e8b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
26e8c 4c 69 73 74 44 65 6c 65 74 65 28 64 62 4d 65 6d  ListDelete(dbMem
26e8d 2c 20 70 53 74 65 70 2d 3e 70 45 78 70 72 4c 69  , pStep->pExprLi
26e8e 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  st);.    sqlite3
26e8f 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 4d  SelectDelete(dbM
26e90 65 6d 2c 20 70 53 74 65 70 2d 3e 70 53 65 6c 65  em, pStep->pSele
26e91 63 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ct);.    sqlite3
26e92 45 78 70 72 44 65 6c 65 74 65 28 64 62 4d 65 6d  ExprDelete(dbMem
26e93 2c 20 70 2d 3e 70 57 68 65 6e 29 3b 0a 20 20 20  , p->pWhen);.   
26e94 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
26e95 62 4d 65 6d 2c 20 70 29 3b 0a 20 20 7d 0a 7d 0a  bMem, p);.  }.}.
26e96 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
26e97 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74  tion is called t
26e98 6f 20 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20  o generate code 
26e99 74 68 61 74 20 72 75 6e 73 20 77 68 65 6e 20 74  that runs when t
26e9a 61 62 6c 65 20 70 54 61 62 20 69 73 0a 2a 2a 20  able pTab is.** 
26e9b 62 65 69 6e 67 20 64 72 6f 70 70 65 64 20 66 72  being dropped fr
26e9c 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  om the database.
26e9d 20 54 68 65 20 53 72 63 4c 69 73 74 20 70 61 73   The SrcList pas
26e9e 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
26e9f 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f  d argument.** to
26ea0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63   this function c
26ea1 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67 6c 65  ontains a single
26ea2 20 65 6e 74 72 79 20 67 75 61 72 61 6e 74 65 65   entry guarantee
26ea3 64 20 74 6f 20 72 65 73 6f 6c 76 65 20 74 6f 0a  d to resolve to.
26ea4 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 2e 0a 2a  ** table pTab..*
26ea5 2a 0a 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 6e  *.** Normally, n
26ea6 6f 20 63 6f 64 65 20 69 73 20 72 65 71 75 69 72  o code is requir
26ea7 65 64 2e 20 48 6f 77 65 76 65 72 2c 20 69 66 20  ed. However, if 
26ea8 65 69 74 68 65 72 0a 2a 2a 0a 2a 2a 20 20 20 28  either.**.**   (
26ea9 61 29 20 54 68 65 20 74 61 62 6c 65 20 69 73 20  a) The table is 
26eaa 74 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65  the parent table
26eab 20 6f 66 20 61 20 46 4b 20 63 6f 6e 73 74 72 61   of a FK constra
26eac 69 6e 74 2c 20 6f 72 0a 2a 2a 20 20 20 28 62 29  int, or.**   (b)
26ead 20 54 68 65 20 74 61 62 6c 65 20 69 73 20 74 68   The table is th
26eae 65 20 63 68 69 6c 64 20 74 61 62 6c 65 20 6f 66  e child table of
26eaf 20 61 20 64 65 66 65 72 72 65 64 20 46 4b 20 63   a deferred FK c
26eb0 6f 6e 73 74 72 61 69 6e 74 20 61 6e 64 20 69 74  onstraint and it
26eb1 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 64 65 74   is.**       det
26eb2 65 72 6d 69 6e 65 64 20 61 74 20 72 75 6e 74 69  ermined at runti
26eb3 6d 65 20 74 68 61 74 20 74 68 65 72 65 20 61 72  me that there ar
26eb4 65 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 64 65  e outstanding de
26eb5 66 65 72 72 65 64 20 46 4b 20 0a 2a 2a 20 20 20  ferred FK .**   
26eb6 20 20 20 20 63 6f 6e 73 74 72 61 69 6e 74 20 76      constraint v
26eb7 69 6f 6c 61 74 69 6f 6e 73 20 69 6e 20 74 68 65  iolations in the
26eb8 20 64 61 74 61 62 61 73 65 2c 0a 2a 2a 0a 2a 2a   database,.**.**
26eb9 20 74 68 65 6e 20 74 68 65 20 65 71 75 69 76 61   then the equiva
26eba 6c 65 6e 74 20 6f 66 20 22 44 45 4c 45 54 45 20  lent of "DELETE 
26ebb 46 52 4f 4d 20 3c 74 62 6c 3e 22 20 69 73 20 65  FROM <tbl>" is e
26ebc 78 65 63 75 74 65 64 20 62 65 66 6f 72 65 20 64  xecuted before d
26ebd 72 6f 70 70 69 6e 67 0a 2a 2a 20 74 68 65 20 74  ropping.** the t
26ebe 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 64 61  able from the da
26ebf 74 61 62 61 73 65 2e 20 54 72 69 67 67 65 72 73  tabase. Triggers
26ec0 20 61 72 65 20 64 69 73 61 62 6c 65 64 20 77 68   are disabled wh
26ec1 69 6c 65 20 72 75 6e 6e 69 6e 67 20 74 68 69 73  ile running this
26ec2 0a 2a 2a 20 44 45 4c 45 54 45 2c 20 62 75 74 20  .** DELETE, but 
26ec3 66 6f 72 65 69 67 6e 20 6b 65 79 20 61 63 74 69  foreign key acti
26ec4 6f 6e 73 20 61 72 65 20 6e 6f 74 2e 0a 2a 2f 0a  ons are not..*/.
26ec5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
26ec6 6f 69 64 20 73 71 6c 69 74 65 33 46 6b 44 72 6f  oid sqlite3FkDro
26ec7 70 54 61 62 6c 65 28 50 61 72 73 65 20 2a 70 50  pTable(Parse *pP
26ec8 61 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70  arse, SrcList *p
26ec9 4e 61 6d 65 2c 20 54 61 62 6c 65 20 2a 70 54 61  Name, Table *pTa
26eca 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  b){.  sqlite3 *d
26ecb 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
26ecc 20 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73    if( (db->flags
26ecd 26 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b  &SQLITE_ForeignK
26ece 65 79 73 29 20 26 26 20 21 49 73 56 69 72 74 75  eys) && !IsVirtu
26ecf 61 6c 28 70 54 61 62 29 20 26 26 20 21 70 54 61  al(pTab) && !pTa
26ed0 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  b->pSelect ){.  
26ed1 20 20 69 6e 74 20 69 53 6b 69 70 20 3d 20 30 3b    int iSkip = 0;
26ed2 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73  .    Vdbe *v = s
26ed3 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
26ed4 61 72 73 65 29 3b 0a 0a 20 20 20 20 61 73 73 65  arse);..    asse
26ed5 72 74 28 20 76 20 29 3b 20 20 20 20 20 20 20 20  rt( v );        
26ed6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42            /* VDB
26ed7 45 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65  E has already be
26ed8 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a  en allocated */.
26ed9 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
26eda 6b 52 65 66 65 72 65 6e 63 65 73 28 70 54 61 62  kReferences(pTab
26edb 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  )==0 ){.      /*
26edc 20 53 65 61 72 63 68 20 66 6f 72 20 61 20 64 65   Search for a de
26edd 66 65 72 72 65 64 20 66 6f 72 65 69 67 6e 20 6b  ferred foreign k
26ede 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 6f  ey constraint fo
26edf 72 20 77 68 69 63 68 20 74 68 69 73 20 74 61 62  r which this tab
26ee0 6c 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 74  le.      ** is t
26ee1 68 65 20 63 68 69 6c 64 20 74 61 62 6c 65 2e 20  he child table. 
26ee2 49 66 20 6f 6e 65 20 63 61 6e 6e 6f 74 20 62 65  If one cannot be
26ee3 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20 77   found, return w
26ee4 69 74 68 6f 75 74 20 0a 20 20 20 20 20 20 2a 2a  ithout .      **
26ee5 20 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 79 20   generating any 
26ee6 56 44 42 45 20 63 6f 64 65 2e 20 49 66 20 6f 6e  VDBE code. If on
26ee7 65 20 63 61 6e 20 62 65 20 66 6f 75 6e 64 2c 20  e can be found, 
26ee8 74 68 65 6e 20 6a 75 6d 70 20 6f 76 65 72 0a 20  then jump over. 
26ee9 20 20 20 20 20 2a 2a 20 74 68 65 20 65 6e 74 69       ** the enti
26eea 72 65 20 44 45 4c 45 54 45 20 69 66 20 74 68 65  re DELETE if the
26eeb 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61  re are no outsta
26eec 6e 64 69 6e 67 20 64 65 66 65 72 72 65 64 20 63  nding deferred c
26eed 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 20 20 20  onstraints.     
26eee 20 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 73 74   ** when this st
26eef 61 74 65 6d 65 6e 74 20 69 73 20 72 75 6e 2e 20  atement is run. 
26ef0 20 2a 2f 0a 20 20 20 20 20 20 46 4b 65 79 20 2a   */.      FKey *
26ef1 70 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 3d 70  p;.      for(p=p
26ef2 54 61 62 2d 3e 70 46 4b 65 79 3b 20 70 3b 20 70  Tab->pFKey; p; p
26ef3 3d 70 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a  =p->pNextFrom){.
26ef4 20 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69          if( p->i
26ef5 73 44 65 66 65 72 72 65 64 20 29 20 62 72 65 61  sDeferred ) brea
26ef6 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
26ef7 20 69 66 28 20 21 70 20 29 20 72 65 74 75 72 6e   if( !p ) return
26ef8 3b 0a 20 20 20 20 20 20 69 53 6b 69 70 20 3d 20  ;.      iSkip = 
26ef9 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
26efa 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 73  abel(v);.      s
26efb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
26efc 28 76 2c 20 4f 50 5f 46 6b 49 66 5a 65 72 6f 2c  (v, OP_FkIfZero,
26efd 20 31 2c 20 69 53 6b 69 70 29 3b 0a 20 20 20 20   1, iSkip);.    
26efe 7d 0a 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 64  }..    pParse->d
26eff 69 73 61 62 6c 65 54 72 69 67 67 65 72 73 20 3d  isableTriggers =
26f00 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   1;.    sqlite3D
26f01 65 6c 65 74 65 46 72 6f 6d 28 70 50 61 72 73 65  eleteFrom(pParse
26f02 2c 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  , sqlite3SrcList
26f03 44 75 70 28 64 62 2c 20 70 4e 61 6d 65 2c 20 30  Dup(db, pName, 0
26f04 29 2c 20 30 29 3b 0a 20 20 20 20 70 50 61 72 73  ), 0);.    pPars
26f05 65 2d 3e 64 69 73 61 62 6c 65 54 72 69 67 67 65  e->disableTrigge
26f06 72 73 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20  rs = 0;..    /* 
26f07 49 66 20 74 68 65 20 44 45 4c 45 54 45 20 68 61  If the DELETE ha
26f08 73 20 67 65 6e 65 72 61 74 65 64 20 69 6d 6d 65  s generated imme
26f09 64 69 61 74 65 20 66 6f 72 65 69 67 6e 20 6b 65  diate foreign ke
26f0a 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 0a 20 20  y constraint .  
26f0b 20 20 2a 2a 20 76 69 6f 6c 61 74 69 6f 6e 73 2c    ** violations,
26f0c 20 68 61 6c 74 20 74 68 65 20 56 44 42 45 20 61   halt the VDBE a
26f0d 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  nd return an err
26f0e 6f 72 20 61 74 20 74 68 69 73 20 70 6f 69 6e 74  or at this point
26f0f 2c 20 62 65 66 6f 72 65 0a 20 20 20 20 2a 2a 20  , before.    ** 
26f10 61 6e 79 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e  any modification
26f11 73 20 74 6f 20 74 68 65 20 73 63 68 65 6d 61 20  s to the schema 
26f12 61 72 65 20 6d 61 64 65 2e 20 54 68 69 73 20 69  are made. This i
26f13 73 20 62 65 63 61 75 73 65 20 73 74 61 74 65 6d  s because statem
26f14 65 6e 74 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73  ent.    ** trans
26f15 61 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20  actions are not 
26f16 61 62 6c 65 20 74 6f 20 72 6f 6c 6c 62 61 63 6b  able to rollback
26f17 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 2e   schema changes.
26f18 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33    */.    sqlite3
26f19 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
26f1a 5f 46 6b 49 66 5a 65 72 6f 2c 20 30 2c 20 73 71  _FkIfZero, 0, sq
26f1b 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
26f1c 41 64 64 72 28 76 29 2b 32 29 3b 0a 20 20 20 20  Addr(v)+2);.    
26f1d 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74  sqlite3HaltConst
26f1e 72 61 69 6e 74 28 0a 20 20 20 20 20 20 20 20 70  raint(.        p
26f1f 50 61 72 73 65 2c 20 4f 45 5f 41 62 6f 72 74 2c  Parse, OE_Abort,
26f20 20 22 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f   "foreign key co
26f21 6e 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 22  nstraint failed"
26f22 2c 20 50 34 5f 53 54 41 54 49 43 0a 20 20 20 20  , P4_STATIC.    
26f23 29 3b 0a 0a 20 20 20 20 69 66 28 20 69 53 6b 69  );..    if( iSki
26f24 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  p ){.      sqlit
26f25 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
26f26 65 6c 28 76 2c 20 69 53 6b 69 70 29 3b 0a 20 20  el(v, iSkip);.  
26f27 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
26f28 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
26f29 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 69 6e  s called when in
26f2a 73 65 72 74 69 6e 67 2c 20 64 65 6c 65 74 69 6e  serting, deletin
26f2b 67 20 6f 72 20 75 70 64 61 74 69 6e 67 20 61 20  g or updating a 
26f2c 72 6f 77 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 20  row of.** table 
26f2d 70 54 61 62 20 74 6f 20 67 65 6e 65 72 61 74 65  pTab to generate
26f2e 20 56 44 42 45 20 63 6f 64 65 20 74 6f 20 70 65   VDBE code to pe
26f2f 72 66 6f 72 6d 20 66 6f 72 65 69 67 6e 20 6b 65  rform foreign ke
26f30 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 0a 2a 2a  y constraint .**
26f31 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20   processing for 
26f32 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a  the operation..*
26f33 2a 0a 2a 2a 20 46 6f 72 20 61 20 44 45 4c 45 54  *.** For a DELET
26f34 45 20 6f 70 65 72 61 74 69 6f 6e 2c 20 70 61 72  E operation, par
26f35 61 6d 65 74 65 72 20 72 65 67 4f 6c 64 20 69 73  ameter regOld is
26f36 20 70 61 73 73 65 64 20 74 68 65 20 69 6e 64 65   passed the inde
26f37 78 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 72 73  x of the.** firs
26f38 74 20 72 65 67 69 73 74 65 72 20 69 6e 20 61 6e  t register in an
26f39 20 61 72 72 61 79 20 6f 66 20 28 70 54 61 62 2d   array of (pTab-
26f3a 3e 6e 43 6f 6c 2b 31 29 20 72 65 67 69 73 74 65  >nCol+1) registe
26f3b 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  rs containing th
26f3c 65 0a 2a 2a 20 72 6f 77 69 64 20 6f 66 20 74 68  e.** rowid of th
26f3d 65 20 72 6f 77 20 62 65 69 6e 67 20 64 65 6c 65  e row being dele
26f3e 74 65 64 2c 20 66 6f 6c 6c 6f 77 65 64 20 62 79  ted, followed by
26f3f 20 65 61 63 68 20 6f 66 20 74 68 65 20 63 6f 6c   each of the col
26f40 75 6d 6e 20 76 61 6c 75 65 73 0a 2a 2a 20 6f 66  umn values.** of
26f41 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67 20 64   the row being d
26f42 65 6c 65 74 65 64 2c 20 66 72 6f 6d 20 6c 65 66  eleted, from lef
26f43 74 20 74 6f 20 72 69 67 68 74 2e 20 50 61 72 61  t to right. Para
26f44 6d 65 74 65 72 20 72 65 67 4e 65 77 20 69 73 20  meter regNew is 
26f45 70 61 73 73 65 64 0a 2a 2a 20 7a 65 72 6f 20 69  passed.** zero i
26f46 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a  n this case..**.
26f47 2a 2a 20 46 6f 72 20 61 6e 20 49 4e 53 45 52 54  ** For an INSERT
26f48 20 6f 70 65 72 61 74 69 6f 6e 2c 20 72 65 67 4f   operation, regO
26f49 6c 64 20 69 73 20 70 61 73 73 65 64 20 7a 65 72  ld is passed zer
26f4a 6f 20 61 6e 64 20 72 65 67 4e 65 77 20 69 73 20  o and regNew is 
26f4b 70 61 73 73 65 64 20 74 68 65 0a 2a 2a 20 66 69  passed the.** fi
26f4c 72 73 74 20 72 65 67 69 73 74 65 72 20 6f 66 20  rst register of 
26f4d 61 6e 20 61 72 72 61 79 20 6f 66 20 28 70 54 61  an array of (pTa
26f4e 62 2d 3e 6e 43 6f 6c 2b 31 29 20 72 65 67 69 73  b->nCol+1) regis
26f4f 74 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ters containing 
26f50 74 68 65 20 6e 65 77 0a 2a 2a 20 72 6f 77 20 64  the new.** row d
26f51 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61  ata..**.** For a
26f52 6e 20 55 50 44 41 54 45 20 6f 70 65 72 61 74 69  n UPDATE operati
26f53 6f 6e 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  on, this functio
26f54 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 77 69 63  n is called twic
26f55 65 2e 20 4f 6e 63 65 20 62 65 66 6f 72 65 0a 2a  e. Once before.*
26f56 2a 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 72  * the original r
26f57 65 63 6f 72 64 20 69 73 20 64 65 6c 65 74 65 64  ecord is deleted
26f58 20 66 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 20   from the table 
26f59 75 73 69 6e 67 20 74 68 65 20 63 61 6c 6c 69 6e  using the callin
26f5a 67 20 63 6f 6e 76 65 6e 74 69 6f 6e 0a 2a 2a 20  g convention.** 
26f5b 64 65 73 63 72 69 62 65 64 20 66 6f 72 20 44 45  described for DE
26f5c 4c 45 54 45 2e 20 54 68 65 6e 20 61 67 61 69 6e  LETE. Then again
26f5d 20 61 66 74 65 72 20 74 68 65 20 6f 72 69 67 69   after the origi
26f5e 6e 61 6c 20 72 65 63 6f 72 64 20 69 73 20 64 65  nal record is de
26f5f 6c 65 74 65 64 0a 2a 2a 20 62 75 74 20 62 65 66  leted.** but bef
26f60 6f 72 65 20 74 68 65 20 6e 65 77 20 72 65 63 6f  ore the new reco
26f61 72 64 20 69 73 20 69 6e 73 65 72 74 65 64 20 75  rd is inserted u
26f62 73 69 6e 67 20 74 68 65 20 49 4e 53 45 52 54 20  sing the INSERT 
26f63 63 6f 6e 76 65 6e 74 69 6f 6e 2e 20 0a 2a 2f 0a  convention. .*/.
26f64 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
26f65 6f 69 64 20 73 71 6c 69 74 65 33 46 6b 43 68 65  oid sqlite3FkChe
26f66 63 6b 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ck(.  Parse *pPa
26f67 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  rse,            
26f68 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63        /* Parse c
26f69 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c  ontext */.  Tabl
26f6a 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20  e *pTab,        
26f6b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
26f6c 6f 77 20 69 73 20 62 65 69 6e 67 20 64 65 6c 65  ow is being dele
26f6d 74 65 64 20 66 72 6f 6d 20 74 68 69 73 20 74 61  ted from this ta
26f6e 62 6c 65 20 2a 2f 20 0a 20 20 69 6e 74 20 72 65  ble */ .  int re
26f6f 67 4f 6c 64 2c 20 20 20 20 20 20 20 20 20 20 20  gOld,           
26f70 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65            /* Pre
26f71 76 69 6f 75 73 20 72 6f 77 20 64 61 74 61 20 69  vious row data i
26f72 73 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f  s stored here */
26f73 0a 20 20 69 6e 74 20 72 65 67 4e 65 77 20 20 20  .  int regNew   
26f74 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26f75 20 20 20 2f 2a 20 4e 65 77 20 72 6f 77 20 64 61     /* New row da
26f76 74 61 20 69 73 20 73 74 6f 72 65 64 20 68 65 72  ta is stored her
26f77 65 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  e */.){.  sqlite
26f78 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
26f79 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 44 61 74  db;       /* Dat
26f7a 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f 0a  abase handle */.
26f7b 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20    Vdbe *v;      
26f7c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26f7d 20 20 2f 2a 20 56 4d 20 74 6f 20 77 72 69 74 65    /* VM to write
26f7e 20 63 6f 64 65 20 74 6f 20 2a 2f 0a 20 20 46 4b   code to */.  FK
26f7f 65 79 20 2a 70 46 4b 65 79 3b 20 20 20 20 20 20  ey *pFKey;      
26f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
26f81 20 55 73 65 64 20 74 6f 20 69 74 65 72 61 74 65   Used to iterate
26f82 20 74 68 72 6f 75 67 68 20 46 4b 73 20 2a 2f 0a   through FKs */.
26f83 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
26f84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26f85 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61    /* Index of da
26f86 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
26f87 67 20 70 54 61 62 20 2a 2f 0a 20 20 63 6f 6e 73  g pTab */.  cons
26f88 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20  t char *zDb;    
26f89 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
26f8a 61 6d 65 20 6f 66 20 64 61 74 61 62 61 73 65 20  ame of database 
26f8b 63 6f 6e 74 61 69 6e 69 6e 67 20 70 54 61 62 20  containing pTab 
26f8c 2a 2f 0a 20 20 69 6e 74 20 69 73 49 67 6e 6f 72  */.  int isIgnor
26f8d 65 45 72 72 6f 72 73 20 3d 20 70 50 61 72 73 65  eErrors = pParse
26f8e 2d 3e 64 69 73 61 62 6c 65 54 72 69 67 67 65 72  ->disableTrigger
26f8f 73 3b 0a 0a 20 20 2f 2a 20 45 78 61 63 74 6c 79  s;..  /* Exactly
26f90 20 6f 6e 65 20 6f 66 20 72 65 67 4f 6c 64 20 61   one of regOld a
26f91 6e 64 20 72 65 67 4e 65 77 20 73 68 6f 75 6c 64  nd regNew should
26f92 20 62 65 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 2a 2f   be non-zero. */
26f93 0a 20 20 61 73 73 65 72 74 28 20 28 72 65 67 4f  .  assert( (regO
26f94 6c 64 3d 3d 30 29 21 3d 28 72 65 67 4e 65 77 3d  ld==0)!=(regNew=
26f95 3d 30 29 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  =0) );..  /* If 
26f96 66 6f 72 65 69 67 6e 2d 6b 65 79 73 20 61 72 65  foreign-keys are
26f97 20 64 69 73 61 62 6c 65 64 2c 20 74 68 69 73 20   disabled, this 
26f98 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f  function is a no
26f99 2d 6f 70 2e 20 2a 2f 0a 20 20 69 66 28 20 28 64  -op. */.  if( (d
26f9a 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f  b->flags&SQLITE_
26f9b 46 6f 72 65 69 67 6e 4b 65 79 73 29 3d 3d 30 20  ForeignKeys)==0 
26f9c 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 76 20 3d  ) return;..  v =
26f9d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
26f9e 70 50 61 72 73 65 29 3b 0a 20 20 69 44 62 20 3d  pParse);.  iDb =
26f9f 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
26fa0 49 6e 64 65 78 28 64 62 2c 20 70 54 61 62 2d 3e  Index(db, pTab->
26fa1 70 53 63 68 65 6d 61 29 3b 0a 20 20 7a 44 62 20  pSchema);.  zDb 
26fa2 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
26fa3 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70  Name;..  /* Loop
26fa4 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 74 68 65   through all the
26fa5 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e   foreign key con
26fa6 73 74 72 61 69 6e 74 73 20 66 6f 72 20 77 68 69  straints for whi
26fa7 63 68 20 70 54 61 62 20 69 73 20 74 68 65 0a 20  ch pTab is the. 
26fa8 20 2a 2a 20 63 68 69 6c 64 20 74 61 62 6c 65 20   ** child table 
26fa9 28 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20  (the table that 
26faa 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79 20  the foreign key 
26fab 64 65 66 69 6e 69 74 69 6f 6e 20 69 73 20 70 61  definition is pa
26fac 72 74 20 6f 66 29 2e 20 20 2a 2f 0a 20 20 66 6f  rt of).  */.  fo
26fad 72 28 70 46 4b 65 79 3d 70 54 61 62 2d 3e 70 46  r(pFKey=pTab->pF
26fae 4b 65 79 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65  Key; pFKey; pFKe
26faf 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72  y=pFKey->pNextFr
26fb0 6f 6d 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a  om){.    Table *
26fb1 70 54 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20  pTo;            
26fb2 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74         /* Parent
26fb3 20 74 61 62 6c 65 20 6f 66 20 66 6f 72 65 69 67   table of foreig
26fb4 6e 20 6b 65 79 20 70 46 4b 65 79 20 2a 2f 0a 20  n key pFKey */. 
26fb5 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d     Index *pIdx =
26fb6 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
26fb7 20 2f 2a 20 49 6e 64 65 78 20 6f 6e 20 6b 65 79   /* Index on key
26fb8 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 70 54 6f 20   columns in pTo 
26fb9 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 61 69 46 72  */.    int *aiFr
26fba 65 65 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  ee = 0;.    int 
26fbb 2a 61 69 43 6f 6c 3b 0a 20 20 20 20 69 6e 74 20  *aiCol;.    int 
26fbc 69 43 6f 6c 3b 0a 20 20 20 20 69 6e 74 20 69 3b  iCol;.    int i;
26fbd 0a 20 20 20 20 69 6e 74 20 69 73 49 67 6e 6f 72  .    int isIgnor
26fbe 65 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 46  e = 0;..    /* F
26fbf 69 6e 64 20 74 68 65 20 70 61 72 65 6e 74 20 74  ind the parent t
26fc0 61 62 6c 65 20 6f 66 20 74 68 69 73 20 66 6f 72  able of this for
26fc1 65 69 67 6e 20 6b 65 79 2e 20 41 6c 73 6f 20 66  eign key. Also f
26fc2 69 6e 64 20 61 20 75 6e 69 71 75 65 20 69 6e 64  ind a unique ind
26fc3 65 78 20 0a 20 20 20 20 2a 2a 20 6f 6e 20 74 68  ex .    ** on th
26fc4 65 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c  e parent key col
26fc5 75 6d 6e 73 20 69 6e 20 74 68 65 20 70 61 72 65  umns in the pare
26fc6 6e 74 20 74 61 62 6c 65 2e 20 49 66 20 65 69 74  nt table. If eit
26fc7 68 65 72 20 6f 66 20 74 68 65 73 65 20 0a 20 20  her of these .  
26fc8 20 20 2a 2a 20 73 63 68 65 6d 61 20 69 74 65 6d    ** schema item
26fc9 73 20 63 61 6e 6e 6f 74 20 62 65 20 6c 6f 63 61  s cannot be loca
26fca 74 65 64 2c 20 73 65 74 20 61 6e 20 65 72 72 6f  ted, set an erro
26fcb 72 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20  r in pParse and 
26fcc 72 65 74 75 72 6e 20 0a 20 20 20 20 2a 2a 20 65  return .    ** e
26fcd 61 72 6c 79 2e 20 20 2a 2f 0a 20 20 20 20 69 66  arly.  */.    if
26fce 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c  ( pParse->disabl
26fcf 65 54 72 69 67 67 65 72 73 20 29 7b 0a 20 20 20  eTriggers ){.   
26fd0 20 20 20 70 54 6f 20 3d 20 73 71 6c 69 74 65 33     pTo = sqlite3
26fd1 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 70 46  FindTable(db, pF
26fd2 4b 65 79 2d 3e 7a 54 6f 2c 20 7a 44 62 29 3b 0a  Key->zTo, zDb);.
26fd3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
26fd4 20 70 54 6f 20 3d 20 73 71 6c 69 74 65 33 4c 6f   pTo = sqlite3Lo
26fd5 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65  cateTable(pParse
26fd6 2c 20 30 2c 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c  , 0, pFKey->zTo,
26fd7 20 7a 44 62 29 3b 0a 20 20 20 20 7d 0a 20 20 20   zDb);.    }.   
26fd8 20 69 66 28 20 21 70 54 6f 20 7c 7c 20 6c 6f 63   if( !pTo || loc
26fd9 61 74 65 46 6b 65 79 49 6e 64 65 78 28 70 50 61  ateFkeyIndex(pPa
26fda 72 73 65 2c 20 70 54 6f 2c 20 70 46 4b 65 79 2c  rse, pTo, pFKey,
26fdb 20 26 70 49 64 78 2c 20 26 61 69 46 72 65 65 29   &pIdx, &aiFree)
26fdc 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 69   ){.      if( !i
26fdd 73 49 67 6e 6f 72 65 45 72 72 6f 72 73 20 7c 7c  sIgnoreErrors ||
26fde 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
26fdf 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  d ) return;.    
26fe0 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
26fe1 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46  }.    assert( pF
26fe2 4b 65 79 2d 3e 6e 43 6f 6c 3d 3d 31 20 7c 7c 20  Key->nCol==1 || 
26fe3 28 61 69 46 72 65 65 20 26 26 20 70 49 64 78 29  (aiFree && pIdx)
26fe4 20 29 3b 0a 0a 20 20 20 20 69 66 28 20 61 69 46   );..    if( aiF
26fe5 72 65 65 20 29 7b 0a 20 20 20 20 20 20 61 69 43  ree ){.      aiC
26fe6 6f 6c 20 3d 20 61 69 46 72 65 65 3b 0a 20 20 20  ol = aiFree;.   
26fe7 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 43   }else{.      iC
26fe8 6f 6c 20 3d 20 70 46 4b 65 79 2d 3e 61 43 6f 6c  ol = pFKey->aCol
26fe9 5b 30 5d 2e 69 46 72 6f 6d 3b 0a 20 20 20 20 20  [0].iFrom;.     
26fea 20 61 69 43 6f 6c 20 3d 20 26 69 43 6f 6c 3b 0a   aiCol = &iCol;.
26feb 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d      }.    for(i=
26fec 30 3b 20 69 3c 70 46 4b 65 79 2d 3e 6e 43 6f 6c  0; i<pFKey->nCol
26fed 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; i++){.      if
26fee 28 20 61 69 43 6f 6c 5b 69 5d 3d 3d 70 54 61 62  ( aiCol[i]==pTab
26fef 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20  ->iPKey ){.     
26ff0 20 20 20 61 69 43 6f 6c 5b 69 5d 20 3d 20 2d 31     aiCol[i] = -1
26ff1 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e 64 65  ;.      }.#ifnde
26ff2 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
26ff3 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20  THORIZATION.    
26ff4 20 20 2f 2a 20 52 65 71 75 65 73 74 20 70 65 72    /* Request per
26ff5 6d 69 73 73 69 6f 6e 20 74 6f 20 72 65 61 64 20  mission to read 
26ff6 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20 63  the parent key c
26ff7 6f 6c 75 6d 6e 73 2e 20 49 66 20 74 68 65 20 0a  olumns. If the .
26ff8 20 20 20 20 20 20 2a 2a 20 61 75 74 68 6f 72 69        ** authori
26ff9 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20  zation callback 
26ffa 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 49  returns SQLITE_I
26ffb 47 4e 4f 52 45 2c 20 62 65 68 61 76 65 20 61 73  GNORE, behave as
26ffc 20 69 66 20 61 6e 79 0a 20 20 20 20 20 20 2a 2a   if any.      **
26ffd 20 76 61 6c 75 65 73 20 72 65 61 64 20 66 72 6f   values read fro
26ffe 6d 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62  m the parent tab
26fff 6c 65 20 61 72 65 20 4e 55 4c 4c 2e 20 2a 2f 0a  le are NULL. */.
27000 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 78 41        if( db->xA
27001 75 74 68 20 29 7b 0a 20 20 20 20 20 20 20 20 69  uth ){.        i
27002 6e 74 20 72 63 61 75 74 68 3b 0a 20 20 20 20 20  nt rcauth;.     
27003 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20 3d 20     char *zCol = 
27004 70 54 6f 2d 3e 61 43 6f 6c 5b 70 49 64 78 20 3f  pTo->aCol[pIdx ?
27005 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b   pIdx->aiColumn[
27006 69 5d 20 3a 20 70 54 6f 2d 3e 69 50 4b 65 79 5d  i] : pTo->iPKey]
27007 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20  .zName;.        
27008 72 63 61 75 74 68 20 3d 20 73 71 6c 69 74 65 33  rcauth = sqlite3
27009 41 75 74 68 52 65 61 64 43 6f 6c 28 70 50 61 72  AuthReadCol(pPar
2700a 73 65 2c 20 70 54 6f 2d 3e 7a 4e 61 6d 65 2c 20  se, pTo->zName, 
2700b 7a 43 6f 6c 2c 20 69 44 62 29 3b 0a 20 20 20 20  zCol, iDb);.    
2700c 20 20 20 20 69 73 49 67 6e 6f 72 65 20 3d 20 28      isIgnore = (
2700d 72 63 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 49  rcauth==SQLITE_I
2700e 47 4e 4f 52 45 29 3b 0a 20 20 20 20 20 20 7d 0a  GNORE);.      }.
2700f 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a 0a 20 20  #endif.    }..  
27010 20 20 2f 2a 20 54 61 6b 65 20 61 20 73 68 61 72    /* Take a shar
27011 65 64 2d 63 61 63 68 65 20 61 64 76 69 73 6f 72  ed-cache advisor
27012 79 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74  y read-lock on t
27013 68 65 20 70 61 72 65 6e 74 20 74 61 62 6c 65 2e  he parent table.
27014 20 41 6c 6c 6f 63 61 74 65 20 0a 20 20 20 20 2a   Allocate .    *
27015 2a 20 61 20 63 75 72 73 6f 72 20 74 6f 20 75 73  * a cursor to us
27016 65 20 74 6f 20 73 65 61 72 63 68 20 74 68 65 20  e to search the 
27017 75 6e 69 71 75 65 20 69 6e 64 65 78 20 6f 6e 20  unique index on 
27018 74 68 65 20 70 61 72 65 6e 74 20 6b 65 79 20 63  the parent key c
27019 6f 6c 75 6d 6e 73 20 0a 20 20 20 20 2a 2a 20 69  olumns .    ** i
2701a 6e 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62  n the parent tab
2701b 6c 65 2e 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  le.  */.    sqli
2701c 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61  te3TableLock(pPa
2701d 72 73 65 2c 20 69 44 62 2c 20 70 54 6f 2d 3e 74  rse, iDb, pTo->t
2701e 6e 75 6d 2c 20 30 2c 20 70 54 6f 2d 3e 7a 4e 61  num, 0, pTo->zNa
2701f 6d 65 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  me);.    pParse-
27020 3e 6e 54 61 62 2b 2b 3b 0a 0a 20 20 20 20 69 66  >nTab++;..    if
27021 28 20 72 65 67 4f 6c 64 21 3d 30 20 29 7b 0a 20  ( regOld!=0 ){. 
27022 20 20 20 20 20 2f 2a 20 41 20 72 6f 77 20 69 73       /* A row is
27023 20 62 65 69 6e 67 20 72 65 6d 6f 76 65 64 20 66   being removed f
27024 72 6f 6d 20 74 68 65 20 63 68 69 6c 64 20 74 61  rom the child ta
27025 62 6c 65 2e 20 53 65 61 72 63 68 20 66 6f 72 20  ble. Search for 
27026 74 68 65 20 70 61 72 65 6e 74 2e 0a 20 20 20 20  the parent..    
27027 20 20 2a 2a 20 49 66 20 74 68 65 20 70 61 72 65    ** If the pare
27028 6e 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  nt does not exis
27029 74 2c 20 72 65 6d 6f 76 69 6e 67 20 74 68 65 20  t, removing the 
2702a 63 68 69 6c 64 20 72 6f 77 20 72 65 73 6f 6c 76  child row resolv
2702b 65 73 20 61 6e 20 0a 20 20 20 20 20 20 2a 2a 20  es an .      ** 
2702c 6f 75 74 73 74 61 6e 64 69 6e 67 20 66 6f 72 65  outstanding fore
2702d 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69  ign key constrai
2702e 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 2e 20 2a 2f  nt violation. */
2702f 0a 20 20 20 20 20 20 66 6b 4c 6f 6f 6b 75 70 50  .      fkLookupP
27030 61 72 65 6e 74 28 70 50 61 72 73 65 2c 20 69 44  arent(pParse, iD
27031 62 2c 20 70 54 6f 2c 20 70 49 64 78 2c 20 70 46  b, pTo, pIdx, pF
27032 4b 65 79 2c 20 61 69 43 6f 6c 2c 20 72 65 67 4f  Key, aiCol, regO
27033 6c 64 2c 20 2d 31 2c 69 73 49 67 6e 6f 72 65 29  ld, -1,isIgnore)
27034 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
27035 72 65 67 4e 65 77 21 3d 30 20 29 7b 0a 20 20 20  regNew!=0 ){.   
27036 20 20 20 2f 2a 20 41 20 72 6f 77 20 69 73 20 62     /* A row is b
27037 65 69 6e 67 20 61 64 64 65 64 20 74 6f 20 74 68  eing added to th
27038 65 20 63 68 69 6c 64 20 74 61 62 6c 65 2e 20 49  e child table. I
27039 66 20 61 20 70 61 72 65 6e 74 20 72 6f 77 20 63  f a parent row c
2703a 61 6e 6e 6f 74 0a 20 20 20 20 20 20 2a 2a 20 62  annot.      ** b
2703b 65 20 66 6f 75 6e 64 2c 20 61 64 64 69 6e 67 20  e found, adding 
2703c 74 68 65 20 63 68 69 6c 64 20 72 6f 77 20 68 61  the child row ha
2703d 73 20 76 69 6f 6c 61 74 65 64 20 74 68 65 20 46  s violated the F
2703e 4b 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 2a 2f  K constraint. */
2703f 20 0a 20 20 20 20 20 20 66 6b 4c 6f 6f 6b 75 70   .      fkLookup
27040 50 61 72 65 6e 74 28 70 50 61 72 73 65 2c 20 69  Parent(pParse, i
27041 44 62 2c 20 70 54 6f 2c 20 70 49 64 78 2c 20 70  Db, pTo, pIdx, p
27042 46 4b 65 79 2c 20 61 69 43 6f 6c 2c 20 72 65 67  FKey, aiCol, reg
27043 4e 65 77 2c 20 2b 31 2c 69 73 49 67 6e 6f 72 65  New, +1,isIgnore
27044 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71  );.    }..    sq
27045 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
27046 61 69 46 72 65 65 29 3b 0a 20 20 7d 0a 0a 20 20  aiFree);.  }..  
27047 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20  /* Loop through 
27048 61 6c 6c 20 74 68 65 20 66 6f 72 65 69 67 6e 20  all the foreign 
27049 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  key constraints 
2704a 74 68 61 74 20 72 65 66 65 72 20 74 6f 20 74 68  that refer to th
2704b 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 66 6f  is table */.  fo
2704c 72 28 70 46 4b 65 79 20 3d 20 73 71 6c 69 74 65  r(pFKey = sqlite
2704d 33 46 6b 52 65 66 65 72 65 6e 63 65 73 28 70 54  3FkReferences(pT
2704e 61 62 29 3b 20 70 46 4b 65 79 3b 20 70 46 4b 65  ab); pFKey; pFKe
2704f 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f  y=pFKey->pNextTo
27050 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  ){.    Index *pI
27051 64 78 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  dx = 0;         
27052 20 20 20 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20       /* Foreign 
27053 6b 65 79 20 69 6e 64 65 78 20 66 6f 72 20 70 46  key index for pF
27054 4b 65 79 20 2a 2f 0a 20 20 20 20 53 72 63 4c 69  Key */.    SrcLi
27055 73 74 20 2a 70 53 72 63 3b 0a 20 20 20 20 69 6e  st *pSrc;.    in
27056 74 20 2a 61 69 43 6f 6c 20 3d 20 30 3b 0a 0a 20  t *aiCol = 0;.. 
27057 20 20 20 69 66 28 20 21 70 46 4b 65 79 2d 3e 69     if( !pFKey->i
27058 73 44 65 66 65 72 72 65 64 20 26 26 20 21 70 50  sDeferred && !pP
27059 61 72 73 65 2d 3e 70 54 6f 70 6c 65 76 65 6c 20  arse->pToplevel 
2705a 26 26 20 21 70 50 61 72 73 65 2d 3e 69 73 4d 75  && !pParse->isMu
2705b 6c 74 69 57 72 69 74 65 20 29 7b 0a 20 20 20 20  ltiWrite ){.    
2705c 20 20 61 73 73 65 72 74 28 20 72 65 67 4f 6c 64    assert( regOld
2705d 3d 3d 30 20 26 26 20 72 65 67 4e 65 77 21 3d 30  ==0 && regNew!=0
2705e 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 49 6e 73   );.      /* Ins
2705f 65 72 74 69 6e 67 20 61 20 73 69 6e 67 6c 65 20  erting a single 
27060 72 6f 77 20 69 6e 74 6f 20 61 20 70 61 72 65 6e  row into a paren
27061 74 20 74 61 62 6c 65 20 63 61 6e 6e 6f 74 20 63  t table cannot c
27062 61 75 73 65 20 61 6e 20 69 6d 6d 65 64 69 61 74  ause an immediat
27063 65 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 65 69  e.      ** forei
27064 67 6e 20 6b 65 79 20 76 69 6f 6c 61 74 69 6f 6e  gn key violation
27065 2e 20 53 6f 20 64 6f 20 6e 6f 74 68 69 6e 67 20  . So do nothing 
27066 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 20 2a  in this case.  *
27067 2f 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65  /.      continue
27068 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
27069 20 6c 6f 63 61 74 65 46 6b 65 79 49 6e 64 65 78   locateFkeyIndex
2706a 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70  (pParse, pTab, p
2706b 46 4b 65 79 2c 20 26 70 49 64 78 2c 20 26 61 69  FKey, &pIdx, &ai
2706c 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 69 66  Col) ){.      if
2706d 28 20 21 69 73 49 67 6e 6f 72 65 45 72 72 6f 72  ( !isIgnoreError
2706e 73 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  s || db->mallocF
2706f 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a  ailed ) return;.
27070 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
27071 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
27072 28 20 61 69 43 6f 6c 20 7c 7c 20 70 46 4b 65 79  ( aiCol || pFKey
27073 2d 3e 6e 43 6f 6c 3d 3d 31 20 29 3b 0a 0a 20 20  ->nCol==1 );..  
27074 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 53 72    /* Create a Sr
27075 63 4c 69 73 74 20 73 74 72 75 63 74 75 72 65 20  cList structure 
27076 63 6f 6e 74 61 69 6e 69 6e 67 20 61 20 73 69 6e  containing a sin
27077 67 6c 65 20 74 61 62 6c 65 20 28 74 68 65 20 74  gle table (the t
27078 61 62 6c 65 20 0a 20 20 20 20 2a 2a 20 74 68 65  able .    ** the
27079 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 74 68 61   foreign key tha
2707a 74 20 72 65 66 65 72 73 20 74 6f 20 74 68 69 73  t refers to this
2707b 20 74 61 62 6c 65 20 69 73 20 61 74 74 61 63 68   table is attach
2707c 65 64 20 74 6f 29 2e 20 54 68 69 73 0a 20 20 20  ed to). This.   
2707d 20 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 20   ** is required 
2707e 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65 33 57  for the sqlite3W
2707f 68 65 72 65 58 58 58 28 29 20 69 6e 74 65 72 66  hereXXX() interf
27080 61 63 65 2e 20 20 2a 2f 0a 20 20 20 20 70 53 72  ace.  */.    pSr
27081 63 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69  c = sqlite3SrcLi
27082 73 74 41 70 70 65 6e 64 28 64 62 2c 20 30 2c 20  stAppend(db, 0, 
27083 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70  0, 0);.    if( p
27084 53 72 63 20 29 7b 0a 20 20 20 20 20 20 73 74 72  Src ){.      str
27085 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
27086 20 2a 70 49 74 65 6d 20 3d 20 70 53 72 63 2d 3e   *pItem = pSrc->
27087 61 3b 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e  a;.      pItem->
27088 70 54 61 62 20 3d 20 70 46 4b 65 79 2d 3e 70 46  pTab = pFKey->pF
27089 72 6f 6d 3b 0a 20 20 20 20 20 20 70 49 74 65 6d  rom;.      pItem
2708a 2d 3e 7a 4e 61 6d 65 20 3d 20 70 46 4b 65 79 2d  ->zName = pFKey-
2708b 3e 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20  >pFrom->zName;. 
2708c 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 54 61 62       pItem->pTab
2708d 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20 20  ->nRef++;.      
2708e 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 20 3d  pItem->iCursor =
2708f 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
27090 0a 20 20 0a 20 20 20 20 20 20 69 66 28 20 72 65  .  .      if( re
27091 67 4e 65 77 21 3d 30 20 29 7b 0a 20 20 20 20 20  gNew!=0 ){.     
27092 20 20 20 66 6b 53 63 61 6e 43 68 69 6c 64 72 65     fkScanChildre
27093 6e 28 70 50 61 72 73 65 2c 20 70 53 72 63 2c 20  n(pParse, pSrc, 
27094 70 54 61 62 2c 20 70 49 64 78 2c 20 70 46 4b 65  pTab, pIdx, pFKe
27095 79 2c 20 61 69 43 6f 6c 2c 20 72 65 67 4e 65 77  y, aiCol, regNew
27096 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20  , -1);.      }. 
27097 20 20 20 20 20 69 66 28 20 72 65 67 4f 6c 64 21       if( regOld!
27098 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  =0 ){.        /*
27099 20 49 66 20 74 68 65 72 65 20 69 73 20 61 20 52   If there is a R
2709a 45 53 54 52 49 43 54 20 61 63 74 69 6f 6e 20 63  ESTRICT action c
2709b 6f 6e 66 69 67 75 72 65 64 20 66 6f 72 20 74 68  onfigured for th
2709c 65 20 63 75 72 72 65 6e 74 20 6f 70 65 72 61 74  e current operat
2709d 69 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f  ion.        ** o
2709e 6e 20 74 68 65 20 70 61 72 65 6e 74 20 74 61 62  n the parent tab
2709f 6c 65 20 6f 66 20 74 68 69 73 20 46 4b 2c 20 74  le of this FK, t
270a0 68 65 6e 20 74 68 72 6f 77 20 61 6e 20 65 78 63  hen throw an exc
270a1 65 70 74 69 6f 6e 20 0a 20 20 20 20 20 20 20 20  eption .        
270a2 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 69  ** immediately i
270a3 66 20 74 68 65 20 46 4b 20 63 6f 6e 73 74 72 61  f the FK constra
270a4 69 6e 74 20 69 73 20 76 69 6f 6c 61 74 65 64 2c  int is violated,
270a5 20 65 76 65 6e 20 69 66 20 74 68 69 73 20 69 73   even if this is
270a6 20 61 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 65   a.        ** de
270a7 66 65 72 72 65 64 20 74 72 69 67 67 65 72 2e 20  ferred trigger. 
270a8 54 68 61 74 27 73 20 77 68 61 74 20 52 45 53 54  That's what REST
270a9 52 49 43 54 20 6d 65 61 6e 73 2e 20 54 6f 20 64  RICT means. To d
270aa 65 66 65 72 20 63 68 65 63 6b 69 6e 67 0a 20 20  efer checking.  
270ab 20 20 20 20 20 20 2a 2a 20 74 68 65 20 63 6f 6e        ** the con
270ac 73 74 72 61 69 6e 74 2c 20 74 68 65 20 46 4b 20  straint, the FK 
270ad 73 68 6f 75 6c 64 20 73 70 65 63 69 66 79 20 4e  should specify N
270ae 4f 20 41 43 54 49 4f 4e 20 28 72 65 70 72 65 73  O ACTION (repres
270af 65 6e 74 65 64 0a 20 20 20 20 20 20 20 20 2a 2a  ented.        **
270b0 20 75 73 69 6e 67 20 4f 45 5f 4e 6f 6e 65 29 2e   using OE_None).
270b1 20 4e 4f 20 41 43 54 49 4f 4e 20 69 73 20 74 68   NO ACTION is th
270b2 65 20 64 65 66 61 75 6c 74 2e 20 20 2a 2f 0a 20  e default.  */. 
270b3 20 20 20 20 20 20 20 66 6b 53 63 61 6e 43 68 69         fkScanChi
270b4 6c 64 72 65 6e 28 70 50 61 72 73 65 2c 20 70 53  ldren(pParse, pS
270b5 72 63 2c 20 70 54 61 62 2c 20 70 49 64 78 2c 20  rc, pTab, pIdx, 
270b6 70 46 4b 65 79 2c 20 61 69 43 6f 6c 2c 20 72 65  pFKey, aiCol, re
270b7 67 4f 6c 64 2c 20 31 29 3b 0a 20 20 20 20 20 20  gOld, 1);.      
270b8 7d 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 7a  }.      pItem->z
270b9 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20 20 20  Name = 0;.      
270ba 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
270bb 6c 65 74 65 28 64 62 2c 20 70 53 72 63 29 3b 0a  lete(db, pSrc);.
270bc 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
270bd 33 44 62 46 72 65 65 28 64 62 2c 20 61 69 43 6f  3DbFree(db, aiCo
270be 6c 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 64 65 66 69  l);.  }.}..#defi
270bf 6e 65 20 43 4f 4c 55 4d 4e 5f 4d 41 53 4b 28 78  ne COLUMN_MASK(x
270c0 29 20 28 28 28 78 29 3e 33 31 29 20 3f 20 30 78  ) (((x)>31) ? 0x
270c1 66 66 66 66 66 66 66 66 20 3a 20 28 28 75 33 32  ffffffff : ((u32
270c2 29 31 3c 3c 28 78 29 29 29 0a 0a 2f 2a 0a 2a 2a  )1<<(x)))../*.**
270c3 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
270c4 73 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20  s called before 
270c5 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 20  generating code 
270c6 74 6f 20 75 70 64 61 74 65 20 6f 72 20 64 65 6c  to update or del
270c7 65 74 65 20 61 20 0a 2a 2a 20 72 6f 77 20 63 6f  ete a .** row co
270c8 6e 74 61 69 6e 65 64 20 69 6e 20 74 61 62 6c 65  ntained in table
270c9 20 70 54 61 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45   pTab..*/.SQLITE
270ca 5f 50 52 49 56 41 54 45 20 75 33 32 20 73 71 6c  _PRIVATE u32 sql
270cb 69 74 65 33 46 6b 4f 6c 64 6d 61 73 6b 28 0a 20  ite3FkOldmask(. 
270cc 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
270cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
270ce 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78   /* Parse contex
270cf 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  t */.  Table *pT
270d0 61 62 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ab              
270d1 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
270d2 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 20 2a  being modified *
270d3 2f 0a 29 7b 0a 20 20 75 33 32 20 6d 61 73 6b 20  /.){.  u32 mask 
270d4 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 61 72 73  = 0;.  if( pPars
270d5 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c  e->db->flags&SQL
270d6 49 54 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20  ITE_ForeignKeys 
270d7 29 7b 0a 20 20 20 20 46 4b 65 79 20 2a 70 3b 0a  ){.    FKey *p;.
270d8 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66      int i;.    f
270d9 6f 72 28 70 3d 70 54 61 62 2d 3e 70 46 4b 65 79  or(p=pTab->pFKey
270da 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 46  ; p; p=p->pNextF
270db 72 6f 6d 29 7b 0a 20 20 20 20 20 20 66 6f 72 28  rom){.      for(
270dc 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20  i=0; i<p->nCol; 
270dd 69 2b 2b 29 20 6d 61 73 6b 20 7c 3d 20 43 4f 4c  i++) mask |= COL
270de 55 4d 4e 5f 4d 41 53 4b 28 70 2d 3e 61 43 6f 6c  UMN_MASK(p->aCol
270df 5b 69 5d 2e 69 46 72 6f 6d 29 3b 0a 20 20 20 20  [i].iFrom);.    
270e0 7d 0a 20 20 20 20 66 6f 72 28 70 3d 73 71 6c 69  }.    for(p=sqli
270e1 74 65 33 46 6b 52 65 66 65 72 65 6e 63 65 73 28  te3FkReferences(
270e2 70 54 61 62 29 3b 20 70 3b 20 70 3d 70 2d 3e 70  pTab); p; p=p->p
270e3 4e 65 78 74 54 6f 29 7b 0a 20 20 20 20 20 20 49  NextTo){.      I
270e4 6e 64 65 78 20 2a 70 49 64 78 20 3d 20 30 3b 0a  ndex *pIdx = 0;.
270e5 20 20 20 20 20 20 6c 6f 63 61 74 65 46 6b 65 79        locateFkey
270e6 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 54  Index(pParse, pT
270e7 61 62 2c 20 70 2c 20 26 70 49 64 78 2c 20 30 29  ab, p, &pIdx, 0)
270e8 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 64 78  ;.      if( pIdx
270e9 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28   ){.        for(
270ea 69 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f  i=0; i<pIdx->nCo
270eb 6c 75 6d 6e 3b 20 69 2b 2b 29 20 6d 61 73 6b 20  lumn; i++) mask 
270ec 7c 3d 20 43 4f 4c 55 4d 4e 5f 4d 41 53 4b 28 70  |= COLUMN_MASK(p
270ed 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d  Idx->aiColumn[i]
270ee 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
270ef 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d 61  .  }.  return ma
270f0 73 6b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  sk;.}../*.** Thi
270f1 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
270f2 6c 6c 65 64 20 62 65 66 6f 72 65 20 67 65 6e 65  lled before gene
270f3 72 61 74 69 6e 67 20 63 6f 64 65 20 74 6f 20 75  rating code to u
270f4 70 64 61 74 65 20 6f 72 20 64 65 6c 65 74 65 20  pdate or delete 
270f5 61 20 0a 2a 2a 20 72 6f 77 20 63 6f 6e 74 61 69  a .** row contai
270f6 6e 65 64 20 69 6e 20 74 61 62 6c 65 20 70 54 61  ned in table pTa
270f7 62 2e 20 49 66 20 74 68 65 20 6f 70 65 72 61 74  b. If the operat
270f8 69 6f 6e 20 69 73 20 61 20 44 45 4c 45 54 45 2c  ion is a DELETE,
270f9 20 74 68 65 6e 0a 2a 2a 20 70 61 72 61 6d 65 74   then.** paramet
270fa 65 72 20 61 43 68 61 6e 67 65 20 69 73 20 70 61  er aChange is pa
270fb 73 73 65 64 20 61 20 4e 55 4c 4c 20 76 61 6c 75  ssed a NULL valu
270fc 65 2e 20 46 6f 72 20 61 6e 20 55 50 44 41 54 45  e. For an UPDATE
270fd 2c 20 61 43 68 61 6e 67 65 20 70 6f 69 6e 74 73  , aChange points
270fe 0a 2a 2a 20 74 6f 20 61 6e 20 61 72 72 61 79 20  .** to an array 
270ff 6f 66 20 73 69 7a 65 20 4e 2c 20 77 68 65 72 65  of size N, where
27100 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72   N is the number
27101 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74   of columns in t
27102 61 62 6c 65 20 70 54 61 62 2e 0a 2a 2a 20 49 66  able pTab..** If
27103 20 74 68 65 20 69 27 74 68 20 63 6f 6c 75 6d 6e   the i'th column
27104 20 69 73 20 6e 6f 74 20 6d 6f 64 69 66 69 65 64   is not modified
27105 20 62 79 20 74 68 65 20 55 50 44 41 54 45 2c 20   by the UPDATE, 
27106 74 68 65 6e 20 74 68 65 20 63 6f 72 72 65 73 70  then the corresp
27107 6f 6e 64 69 6e 67 20 0a 2a 2a 20 65 6e 74 72 79  onding .** entry
27108 20 69 6e 20 74 68 65 20 61 43 68 61 6e 67 65 5b   in the aChange[
27109 5d 20 61 72 72 61 79 20 69 73 20 73 65 74 20 74  ] array is set t
2710a 6f 20 2d 31 2e 20 49 66 20 74 68 65 20 63 6f 6c  o -1. If the col
2710b 75 6d 6e 20 69 73 20 6d 6f 64 69 66 69 65 64 2c  umn is modified,
2710c 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 69 73  .** the value is
2710d 20 30 20 6f 72 20 67 72 65 61 74 65 72 2e 20 50   0 or greater. P
2710e 61 72 61 6d 65 74 65 72 20 63 68 6e 67 52 6f 77  arameter chngRow
2710f 69 64 20 69 73 20 73 65 74 20 74 6f 20 74 72 75  id is set to tru
27110 65 20 69 66 20 74 68 65 0a 2a 2a 20 55 50 44 41  e if the.** UPDA
27111 54 45 20 73 74 61 74 65 6d 65 6e 74 20 6d 6f 64  TE statement mod
27112 69 66 69 65 73 20 74 68 65 20 72 6f 77 69 64 20  ifies the rowid 
27113 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 74 61  fields of the ta
27114 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e  ble..**.** If an
27115 79 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 70 72  y foreign key pr
27116 6f 63 65 73 73 69 6e 67 20 77 69 6c 6c 20 62 65  ocessing will be
27117 20 72 65 71 75 69 72 65 64 2c 20 74 68 69 73 20   required, this 
27118 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
27119 0a 2a 2a 20 74 72 75 65 2e 20 49 66 20 74 68 65  .** true. If the
2711a 72 65 20 69 73 20 6e 6f 20 66 6f 72 65 69 67 6e  re is no foreign
2711b 20 6b 65 79 20 72 65 6c 61 74 65 64 20 70 72 6f   key related pro
2711c 63 65 73 73 69 6e 67 2c 20 74 68 69 73 20 66 75  cessing, this fu
2711d 6e 63 74 69 6f 6e 20 0a 2a 2a 20 72 65 74 75 72  nction .** retur
2711e 6e 73 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c  ns false..*/.SQL
2711f 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
27120 73 71 6c 69 74 65 33 46 6b 52 65 71 75 69 72 65  sqlite3FkRequire
27121 64 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  d(.  Parse *pPar
27122 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
27123 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20 63 6f       /* Parse co
27124 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65  ntext */.  Table
27125 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20 20   *pTab,         
27126 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
27127 62 6c 65 20 62 65 69 6e 67 20 6d 6f 64 69 66 69  ble being modifi
27128 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 43 68  ed */.  int *aCh
27129 61 6e 67 65 2c 20 20 20 20 20 20 20 20 20 20 20  ange,           
2712a 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 6e 2d 4e          /* Non-N
2712b 55 4c 4c 20 66 6f 72 20 55 50 44 41 54 45 20 6f  ULL for UPDATE o
2712c 70 65 72 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69  perations */.  i
2712d 6e 74 20 63 68 6e 67 52 6f 77 69 64 20 20 20 20  nt chngRowid    
2712e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2712f 2a 20 54 72 75 65 20 66 6f 72 20 55 50 44 41 54  * True for UPDAT
27130 45 20 74 68 61 74 20 61 66 66 65 63 74 73 20 72  E that affects r
27131 6f 77 69 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28  owid */.){.  if(
27132 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61   pParse->db->fla
27133 67 73 26 53 51 4c 49 54 45 5f 46 6f 72 65 69 67  gs&SQLITE_Foreig
27134 6e 4b 65 79 73 20 29 7b 0a 20 20 20 20 69 66 28  nKeys ){.    if(
27135 20 21 61 43 68 61 6e 67 65 20 29 7b 0a 20 20 20   !aChange ){.   
27136 20 20 20 2f 2a 20 41 20 44 45 4c 45 54 45 20 6f     /* A DELETE o
27137 70 65 72 61 74 69 6f 6e 2e 20 46 6f 72 65 69 67  peration. Foreig
27138 6e 20 6b 65 79 20 70 72 6f 63 65 73 73 69 6e 67  n key processing
27139 20 69 73 20 72 65 71 75 69 72 65 64 20 69 66 20   is required if 
2713a 74 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 74 61  the .      ** ta
2713b 62 6c 65 20 69 6e 20 71 75 65 73 74 69 6f 6e 20  ble in question 
2713c 69 73 20 65 69 74 68 65 72 20 74 68 65 20 63 68  is either the ch
2713d 69 6c 64 20 6f 72 20 70 61 72 65 6e 74 20 74 61  ild or parent ta
2713e 62 6c 65 20 66 6f 72 20 61 6e 79 20 0a 20 20 20  ble for any .   
2713f 20 20 20 2a 2a 20 66 6f 72 65 69 67 6e 20 6b 65     ** foreign ke
27140 79 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20 20 2a  y constraint.  *
27141 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 28  /.      return (
27142 73 71 6c 69 74 65 33 46 6b 52 65 66 65 72 65 6e  sqlite3FkReferen
27143 63 65 73 28 70 54 61 62 29 20 7c 7c 20 70 54 61  ces(pTab) || pTa
27144 62 2d 3e 70 46 4b 65 79 29 3b 0a 20 20 20 20 7d  b->pFKey);.    }
27145 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 54  else{.      /* T
27146 68 69 73 20 69 73 20 61 6e 20 55 50 44 41 54 45  his is an UPDATE
27147 2e 20 46 6f 72 65 69 67 6e 20 6b 65 79 20 70 72  . Foreign key pr
27148 6f 63 65 73 73 69 6e 67 20 69 73 20 6f 6e 6c 79  ocessing is only
27149 20 72 65 71 75 69 72 65 64 20 69 66 20 74 68 65   required if the
2714a 0a 20 20 20 20 20 20 2a 2a 20 6f 70 65 72 61 74  .      ** operat
2714b 69 6f 6e 20 6d 6f 64 69 66 69 65 73 20 6f 6e 65  ion modifies one
2714c 20 6f 72 20 6d 6f 72 65 20 63 68 69 6c 64 20 6f   or more child o
2714d 72 20 70 61 72 65 6e 74 20 6b 65 79 20 63 6f 6c  r parent key col
2714e 75 6d 6e 73 2e 20 2a 2f 0a 20 20 20 20 20 20 69  umns. */.      i
2714f 6e 74 20 69 3b 0a 20 20 20 20 20 20 46 4b 65 79  nt i;.      FKey
27150 20 2a 70 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43   *p;..      /* C
27151 68 65 63 6b 20 69 66 20 61 6e 79 20 63 68 69 6c  heck if any chil
27152 64 20 6b 65 79 20 63 6f 6c 75 6d 6e 73 20 61 72  d key columns ar
27153 65 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64  e being modified
27154 2e 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 70  . */.      for(p
27155 3d 70 54 61 62 2d 3e 70 46 4b 65 79 3b 20 70 3b  =pTab->pFKey; p;
27156 20 70 3d 70 2d 3e 70 4e 65 78 74 46 72 6f 6d 29   p=p->pNextFrom)
27157 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d  {.        for(i=
27158 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b 20 69 2b  0; i<p->nCol; i+
27159 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e  +){.          in
2715a 74 20 69 43 68 69 6c 64 4b 65 79 20 3d 20 70 2d  t iChildKey = p-
2715b 3e 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 3b 0a  >aCol[i].iFrom;.
2715c 20 20 20 20 20 20 20 20 20 20 69 66 28 20 61 43            if( aC
2715d 68 61 6e 67 65 5b 69 43 68 69 6c 64 4b 65 79 5d  hange[iChildKey]
2715e 3e 3d 30 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  >=0 ) return 1;.
2715f 20 20 20 20 20 20 20 20 20 20 69 66 28 20 69 43            if( iC
27160 68 69 6c 64 4b 65 79 3d 3d 70 54 61 62 2d 3e 69  hildKey==pTab->i
27161 50 4b 65 79 20 26 26 20 63 68 6e 67 52 6f 77 69  PKey && chngRowi
27162 64 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  d ) return 1;.  
27163 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
27164 0a 20 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20  .      /* Check 
27165 69 66 20 61 6e 79 20 70 61 72 65 6e 74 20 6b 65  if any parent ke
27166 79 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 62 65  y columns are be
27167 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e 20 2a 2f  ing modified. */
27168 0a 20 20 20 20 20 20 66 6f 72 28 70 3d 73 71 6c  .      for(p=sql
27169 69 74 65 33 46 6b 52 65 66 65 72 65 6e 63 65 73  ite3FkReferences
2716a 28 70 54 61 62 29 3b 20 70 3b 20 70 3d 70 2d 3e  (pTab); p; p=p->
2716b 70 4e 65 78 74 54 6f 29 7b 0a 20 20 20 20 20 20  pNextTo){.      
2716c 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
2716d 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nCol; i++){.    
2716e 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4b 65 79        char *zKey
2716f 20 3d 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43   = p->aCol[i].zC
27170 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20 69 6e  ol;.          in
27171 74 20 69 4b 65 79 3b 0a 20 20 20 20 20 20 20 20  t iKey;.        
27172 20 20 66 6f 72 28 69 4b 65 79 3d 30 3b 20 69 4b    for(iKey=0; iK
27173 65 79 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69  ey<pTab->nCol; i
27174 4b 65 79 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  Key++){.        
27175 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c      Column *pCol
27176 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69   = &pTab->aCol[i
27177 4b 65 79 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  Key];.          
27178 20 20 69 66 28 20 28 7a 4b 65 79 20 3f 20 21 73    if( (zKey ? !s
27179 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43  qlite3StrICmp(pC
2717a 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 4b 65 79 29  ol->zName, zKey)
2717b 20 3a 20 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b   : pCol->isPrimK
2717c 65 79 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ey) ){.         
2717d 20 20 20 20 20 69 66 28 20 61 43 68 61 6e 67 65       if( aChange
2717e 5b 69 4b 65 79 5d 3e 3d 30 20 29 20 72 65 74 75  [iKey]>=0 ) retu
2717f 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20  rn 1;.          
27180 20 20 20 20 69 66 28 20 69 4b 65 79 3d 3d 70 54      if( iKey==pT
27181 61 62 2d 3e 69 50 4b 65 79 20 26 26 20 63 68 6e  ab->iPKey && chn
27182 67 52 6f 77 69 64 20 29 20 72 65 74 75 72 6e 20  gRowid ) return 
27183 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  1;.            }
27184 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
27185 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
27186 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
27187 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  n 0;.}../*.** Th
27188 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  is function is c
27189 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 55 50  alled when an UP
2718a 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20 6f  DATE or DELETE o
2718b 70 65 72 61 74 69 6f 6e 20 69 73 20 62 65 69 6e  peration is bein
2718c 67 20 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 6f  g .** compiled o
2718d 6e 20 74 61 62 6c 65 20 70 54 61 62 2c 20 77 68  n table pTab, wh
2718e 69 63 68 20 69 73 20 74 68 65 20 70 61 72 65 6e  ich is the paren
2718f 74 20 74 61 62 6c 65 20 6f 66 20 66 6f 72 65 69  t table of forei
27190 67 6e 2d 6b 65 79 20 70 46 4b 65 79 2e 0a 2a 2a  gn-key pFKey..**
27191 20 49 66 20 74 68 65 20 63 75 72 72 65 6e 74 20   If the current 
27192 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6e 20  operation is an 
27193 55 50 44 41 54 45 2c 20 74 68 65 6e 20 74 68 65  UPDATE, then the
27194 20 70 43 68 61 6e 67 65 73 20 70 61 72 61 6d 65   pChanges parame
27195 74 65 72 20 69 73 0a 2a 2a 20 70 61 73 73 65 64  ter is.** passed
27196 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
27197 65 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e  e list of column
27198 73 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64  s being modified
27199 2e 20 49 66 20 69 74 20 69 73 20 61 0a 2a 2a 20  . If it is a.** 
2719a 44 45 4c 45 54 45 2c 20 70 43 68 61 6e 67 65 73  DELETE, pChanges
2719b 20 69 73 20 70 61 73 73 65 64 20 61 20 4e 55 4c   is passed a NUL
2719c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a  L pointer..**.**
2719d 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 70 6f   It returns a po
2719e 69 6e 74 65 72 20 74 6f 20 61 20 54 72 69 67 67  inter to a Trigg
2719f 65 72 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e  er structure con
271a0 74 61 69 6e 69 6e 67 20 61 20 74 72 69 67 67 65  taining a trigge
271a1 72 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20  r.** equivalent 
271a2 74 6f 20 74 68 65 20 4f 4e 20 55 50 44 41 54 45  to the ON UPDATE
271a3 20 6f 72 20 4f 4e 20 44 45 4c 45 54 45 20 61 63   or ON DELETE ac
271a4 74 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 62  tion specified b
271a5 79 20 70 46 4b 65 79 2e 0a 2a 2a 20 49 66 20 74  y pFKey..** If t
271a6 68 65 20 61 63 74 69 6f 6e 20 69 73 20 22 4e 4f  he action is "NO
271a7 20 41 43 54 49 4f 4e 22 20 6f 72 20 22 52 45 53   ACTION" or "RES
271a8 54 52 49 43 54 22 2c 20 74 68 65 6e 20 61 20 4e  TRICT", then a N
271a9 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 0a 2a  ULL pointer is.*
271aa 2a 20 72 65 74 75 72 6e 65 64 20 28 74 68 65 73  * returned (thes
271ab 65 20 61 63 74 69 6f 6e 73 20 72 65 71 75 69 72  e actions requir
271ac 65 20 6e 6f 20 73 70 65 63 69 61 6c 20 68 61 6e  e no special han
271ad 64 6c 69 6e 67 20 62 79 20 74 68 65 20 74 72 69  dling by the tri
271ae 67 67 65 72 73 0a 2a 2a 20 73 75 62 2d 73 79 73  ggers.** sub-sys
271af 74 65 6d 2c 20 63 6f 64 65 20 66 6f 72 20 74 68  tem, code for th
271b0 65 6d 20 69 73 20 63 72 65 61 74 65 64 20 62 79  em is created by
271b1 20 66 6b 53 63 61 6e 43 68 69 6c 64 72 65 6e 28   fkScanChildren(
271b2 29 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78  ))..**.** For ex
271b3 61 6d 70 6c 65 2c 20 69 66 20 70 46 4b 65 79 20  ample, if pFKey 
271b4 69 73 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b  is the foreign k
271b5 65 79 20 61 6e 64 20 70 54 61 62 20 69 73 20 74  ey and pTab is t
271b6 61 62 6c 65 20 22 70 22 20 69 6e 20 0a 2a 2a 20  able "p" in .** 
271b7 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 63  the following sc
271b8 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 20 20 43 52  hema:.**.**   CR
271b9 45 41 54 45 20 54 41 42 4c 45 20 70 28 70 6b 20  EATE TABLE p(pk 
271ba 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 0a 2a 2a  PRIMARY KEY);.**
271bb 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
271bc 63 28 63 6b 20 52 45 46 45 52 45 4e 43 45 53 20  c(ck REFERENCES 
271bd 70 20 4f 4e 20 44 45 4c 45 54 45 20 43 41 53 43  p ON DELETE CASC
271be 41 44 45 29 3b 0a 2a 2a 0a 2a 2a 20 74 68 65 6e  ADE);.**.** then
271bf 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 74 72   the returned tr
271c0 69 67 67 65 72 20 73 74 72 75 63 74 75 72 65 20  igger structure 
271c1 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f  is equivalent to
271c2 3a 0a 2a 2a 0a 2a 2a 20 20 20 43 52 45 41 54 45  :.**.**   CREATE
271c3 20 54 52 49 47 47 45 52 20 2e 2e 2e 20 44 45 4c   TRIGGER ... DEL
271c4 45 54 45 20 4f 4e 20 70 20 42 45 47 49 4e 0a 2a  ETE ON p BEGIN.*
271c5 2a 20 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f  *     DELETE FRO
271c6 4d 20 63 20 57 48 45 52 45 20 63 6b 20 3d 20 6f  M c WHERE ck = o
271c7 6c 64 2e 70 6b 3b 0a 2a 2a 20 20 20 45 4e 44 3b  ld.pk;.**   END;
271c8 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  .**.** The retur
271c9 6e 65 64 20 70 6f 69 6e 74 65 72 20 69 73 20 63  ned pointer is c
271ca 61 63 68 65 64 20 61 73 20 70 61 72 74 20 6f 66  ached as part of
271cb 20 74 68 65 20 66 6f 72 65 69 67 6e 20 6b 65 79   the foreign key
271cc 20 6f 62 6a 65 63 74 2e 20 49 74 0a 2a 2a 20 69   object. It.** i
271cd 73 20 65 76 65 6e 74 75 61 6c 6c 79 20 66 72 65  s eventually fre
271ce 65 64 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68  ed along with th
271cf 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 66 6f  e rest of the fo
271d0 72 65 69 67 6e 20 6b 65 79 20 6f 62 6a 65 63 74  reign key object
271d1 20 62 79 20 0a 2a 2a 20 73 71 6c 69 74 65 33 46   by .** sqlite3F
271d2 6b 44 65 6c 65 74 65 28 29 2e 0a 2a 2f 0a 73 74  kDelete()..*/.st
271d3 61 74 69 63 20 54 72 69 67 67 65 72 20 2a 66 6b  atic Trigger *fk
271d4 41 63 74 69 6f 6e 54 72 69 67 67 65 72 28 0a 20  ActionTrigger(. 
271d5 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
271d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
271d7 20 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78   /* Parse contex
271d8 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  t */.  Table *pT
271d9 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ab,             
271da 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
271db 62 65 69 6e 67 20 75 70 64 61 74 65 64 20 6f 72  being updated or
271dc 20 64 65 6c 65 74 65 64 20 66 72 6f 6d 20 2a 2f   deleted from */
271dd 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65 79 2c 20  .  FKey *pFKey, 
271de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
271df 20 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b 65     /* Foreign ke
271e0 79 20 74 6f 20 67 65 74 20 61 63 74 69 6f 6e 20  y to get action 
271e1 66 6f 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  for */.  ExprLis
271e2 74 20 2a 70 43 68 61 6e 67 65 73 20 20 20 20 20  t *pChanges     
271e3 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 61 6e           /* Chan
271e4 67 65 2d 6c 69 73 74 20 66 6f 72 20 55 50 44 41  ge-list for UPDA
271e5 54 45 2c 20 4e 55 4c 4c 20 66 6f 72 20 44 45 4c  TE, NULL for DEL
271e6 45 54 45 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  ETE */.){.  sqli
271e7 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
271e8 2d 3e 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 44  ->db;       /* D
271e9 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a  atabase handle *
271ea 2f 0a 20 20 69 6e 74 20 61 63 74 69 6f 6e 3b 20  /.  int action; 
271eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
271ec 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 4f 45      /* One of OE
271ed 5f 4e 6f 6e 65 2c 20 4f 45 5f 43 61 73 63 61 64  _None, OE_Cascad
271ee 65 20 65 74 63 2e 20 2a 2f 0a 20 20 54 72 69 67  e etc. */.  Trig
271ef 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20 20  ger *pTrigger;  
271f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
271f1 72 69 67 67 65 72 20 64 65 66 69 6e 69 74 69 6f  rigger definitio
271f2 6e 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20  n to return */. 
271f3 20 69 6e 74 20 69 41 63 74 69 6f 6e 20 3d 20 28   int iAction = (
271f4 70 43 68 61 6e 67 65 73 21 3d 30 29 3b 20 20 20  pChanges!=0);   
271f5 20 2f 2a 20 31 20 66 6f 72 20 55 50 44 41 54 45   /* 1 for UPDATE
271f6 2c 20 30 20 66 6f 72 20 44 45 4c 45 54 45 20 2a  , 0 for DELETE *
271f7 2f 0a 0a 20 20 61 63 74 69 6f 6e 20 3d 20 70 46  /..  action = pF
271f8 4b 65 79 2d 3e 61 41 63 74 69 6f 6e 5b 69 41 63  Key->aAction[iAc
271f9 74 69 6f 6e 5d 3b 0a 20 20 70 54 72 69 67 67 65  tion];.  pTrigge
271fa 72 20 3d 20 70 46 4b 65 79 2d 3e 61 70 54 72 69  r = pFKey->apTri
271fb 67 67 65 72 5b 69 41 63 74 69 6f 6e 5d 3b 0a 0a  gger[iAction];..
271fc 20 20 69 66 28 20 61 63 74 69 6f 6e 21 3d 4f 45    if( action!=OE
271fd 5f 4e 6f 6e 65 20 26 26 20 21 70 54 72 69 67 67  _None && !pTrigg
271fe 65 72 20 29 7b 0a 20 20 20 20 75 38 20 65 6e 61  er ){.    u8 ena
271ff 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b 20 20 20  bleLookaside;   
27200 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20          /* Copy 
27201 6f 66 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65  of db->lookaside
27202 2e 62 45 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 20  .bEnabled */.   
27203 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 46 72   char const *zFr
27204 6f 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  om;            /
27205 2a 20 4e 61 6d 65 20 6f 66 20 63 68 69 6c 64 20  * Name of child 
27206 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74  table */.    int
27207 20 6e 46 72 6f 6d 3b 20 20 20 20 20 20 20 20 20   nFrom;         
27208 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
27209 6e 67 74 68 20 69 6e 20 62 79 74 65 73 20 6f 66  ngth in bytes of
2720a 20 7a 46 72 6f 6d 20 2a 2f 0a 20 20 20 20 49 6e   zFrom */.    In
2720b 64 65 78 20 2a 70 49 64 78 20 3d 20 30 3b 20 20  dex *pIdx = 0;  
2720c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
2720d 61 72 65 6e 74 20 6b 65 79 20 69 6e 64 65 78 20  arent key index 
2720e 66 6f 72 20 74 68 69 73 20 46 4b 20 2a 2f 0a 20  for this FK */. 
2720f 20 20 20 69 6e 74 20 2a 61 69 43 6f 6c 20 3d 20     int *aiCol = 
27210 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
27211 20 2f 2a 20 63 68 69 6c 64 20 74 61 62 6c 65 20   /* child table 
27212 63 6f 6c 73 20 2d 3e 20 70 61 72 65 6e 74 20 6b  cols -> parent k
27213 65 79 20 63 6f 6c 73 20 2a 2f 0a 20 20 20 20 54  ey cols */.    T
27214 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 65  riggerStep *pSte
27215 70 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  p = 0;        /*
27216 20 46 69 72 73 74 20 28 6f 6e 6c 79 29 20 73 74   First (only) st
27217 65 70 20 6f 66 20 74 72 69 67 67 65 72 20 70 72  ep of trigger pr
27218 6f 67 72 61 6d 20 2a 2f 0a 20 20 20 20 45 78 70  ogram */.    Exp
27219 72 20 2a 70 57 68 65 72 65 20 3d 20 30 3b 20 20  r *pWhere = 0;  
2721a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 48             /* WH
2721b 45 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 72  ERE clause of tr
2721c 69 67 67 65 72 20 73 74 65 70 20 2a 2f 0a 20 20  igger step */.  
2721d 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
2721e 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
2721f 2f 2a 20 43 68 61 6e 67 65 73 20 6c 69 73 74 20  /* Changes list 
27220 69 66 20 4f 4e 20 55 50 44 41 54 45 20 43 41 53  if ON UPDATE CAS
27221 43 41 44 45 20 2a 2f 0a 20 20 20 20 53 65 6c 65  CADE */.    Sele
27222 63 74 20 2a 70 53 65 6c 65 63 74 20 3d 20 30 3b  ct *pSelect = 0;
27223 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20            /* If 
27224 52 45 53 54 52 49 43 54 2c 20 22 53 45 4c 45 43  RESTRICT, "SELEC
27225 54 20 52 41 49 53 45 28 2e 2e 2e 29 22 20 2a 2f  T RAISE(...)" */
27226 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20  .    int i;     
27227 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27228 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76     /* Iterator v
27229 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 45  ariable */.    E
2722a 78 70 72 20 2a 70 57 68 65 6e 20 3d 20 30 3b 20  xpr *pWhen = 0; 
2722b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2722c 57 48 45 4e 20 63 6c 61 75 73 65 20 66 6f 72 20  WHEN clause for 
2722d 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 0a  the trigger */..
2722e 20 20 20 20 69 66 28 20 6c 6f 63 61 74 65 46 6b      if( locateFk
2722f 65 79 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  eyIndex(pParse, 
27230 70 54 61 62 2c 20 70 46 4b 65 79 2c 20 26 70 49  pTab, pFKey, &pI
27231 64 78 2c 20 26 61 69 43 6f 6c 29 20 29 20 72 65  dx, &aiCol) ) re
27232 74 75 72 6e 20 30 3b 0a 20 20 20 20 61 73 73 65  turn 0;.    asse
27233 72 74 28 20 61 69 43 6f 6c 20 7c 7c 20 70 46 4b  rt( aiCol || pFK
27234 65 79 2d 3e 6e 43 6f 6c 3d 3d 31 20 29 3b 0a 0a  ey->nCol==1 );..
27235 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
27236 46 4b 65 79 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  FKey->nCol; i++)
27237 7b 0a 20 20 20 20 20 20 54 6f 6b 65 6e 20 74 4f  {.      Token tO
27238 6c 64 20 3d 20 7b 20 22 6f 6c 64 22 2c 20 33 20  ld = { "old", 3 
27239 7d 3b 20 20 2f 2a 20 4c 69 74 65 72 61 6c 20 22  };  /* Literal "
2723a 6f 6c 64 22 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20  old" token */.  
2723b 20 20 20 20 54 6f 6b 65 6e 20 74 4e 65 77 20 3d      Token tNew =
2723c 20 7b 20 22 6e 65 77 22 2c 20 33 20 7d 3b 20 20   { "new", 3 };  
2723d 2f 2a 20 4c 69 74 65 72 61 6c 20 22 6e 65 77 22  /* Literal "new"
2723e 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20   token */.      
2723f 54 6f 6b 65 6e 20 74 46 72 6f 6d 43 6f 6c 3b 20  Token tFromCol; 
27240 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
27241 61 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e  ame of column in
27242 20 63 68 69 6c 64 20 74 61 62 6c 65 20 2a 2f 0a   child table */.
27243 20 20 20 20 20 20 54 6f 6b 65 6e 20 74 54 6f 43        Token tToC
27244 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ol;             
27245 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 63 6f 6c    /* Name of col
27246 75 6d 6e 20 69 6e 20 70 61 72 65 6e 74 20 74 61  umn in parent ta
27247 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74  ble */.      int
27248 20 69 46 72 6f 6d 43 6f 6c 3b 20 20 20 20 20 20   iFromCol;      
27249 20 20 20 20 20 20 20 20 20 2f 2a 20 49 64 78 20           /* Idx 
2724a 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 63 68 69  of column in chi
2724b 6c 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  ld table */.    
2724c 20 20 45 78 70 72 20 2a 70 45 71 3b 20 20 20 20    Expr *pEq;    
2724d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2724e 20 74 46 72 6f 6d 43 6f 6c 20 3d 20 4f 4c 44 2e   tFromCol = OLD.
2724f 74 54 6f 43 6f 6c 20 2a 2f 0a 0a 20 20 20 20 20  tToCol */..     
27250 20 69 46 72 6f 6d 43 6f 6c 20 3d 20 61 69 43 6f   iFromCol = aiCo
27251 6c 20 3f 20 61 69 43 6f 6c 5b 69 5d 20 3a 20 70  l ? aiCol[i] : p
27252 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30 5d 2e 69 46  FKey->aCol[0].iF
27253 72 6f 6d 3b 0a 20 20 20 20 20 20 61 73 73 65 72  rom;.      asser
27254 74 28 20 69 46 72 6f 6d 43 6f 6c 3e 3d 30 20 29  t( iFromCol>=0 )
27255 3b 0a 20 20 20 20 20 20 74 54 6f 43 6f 6c 2e 7a  ;.      tToCol.z
27256 20 3d 20 70 49 64 78 20 3f 20 70 54 61 62 2d 3e   = pIdx ? pTab->
27257 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69 43 6f 6c  aCol[pIdx->aiCol
27258 75 6d 6e 5b 69 5d 5d 2e 7a 4e 61 6d 65 20 3a 20  umn[i]].zName : 
27259 22 6f 69 64 22 3b 0a 20 20 20 20 20 20 74 46 72  "oid";.      tFr
2725a 6f 6d 43 6f 6c 2e 7a 20 3d 20 70 46 4b 65 79 2d  omCol.z = pFKey-
2725b 3e 70 46 72 6f 6d 2d 3e 61 43 6f 6c 5b 69 46 72  >pFrom->aCol[iFr
2725c 6f 6d 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 0a 20  omCol].zName;.. 
2725d 20 20 20 20 20 74 54 6f 43 6f 6c 2e 6e 20 3d 20       tToCol.n = 
2725e 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2725f 74 54 6f 43 6f 6c 2e 7a 29 3b 0a 20 20 20 20 20  tToCol.z);.     
27260 20 74 46 72 6f 6d 43 6f 6c 2e 6e 20 3d 20 73 71   tFromCol.n = sq
27261 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 74 46  lite3Strlen30(tF
27262 72 6f 6d 43 6f 6c 2e 7a 29 3b 0a 0a 20 20 20 20  romCol.z);..    
27263 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20    /* Create the 
27264 65 78 70 72 65 73 73 69 6f 6e 20 22 4f 4c 44 2e  expression "OLD.
27265 7a 54 6f 43 6f 6c 20 3d 20 7a 46 72 6f 6d 43 6f  zToCol = zFromCo
27266 6c 22 2e 20 49 74 20 69 73 20 69 6d 70 6f 72 74  l". It is import
27267 61 6e 74 0a 20 20 20 20 20 20 2a 2a 20 74 68 61  ant.      ** tha
27268 74 20 74 68 65 20 22 4f 4c 44 2e 7a 54 6f 43 6f  t the "OLD.zToCo
27269 6c 22 20 74 65 72 6d 20 69 73 20 6f 6e 20 74 68  l" term is on th
2726a 65 20 4c 48 53 20 6f 66 20 74 68 65 20 3d 20 6f  e LHS of the = o
2726b 70 65 72 61 74 6f 72 2c 20 73 6f 0a 20 20 20 20  perator, so.    
2726c 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20 61 66    ** that the af
2726d 66 69 6e 69 74 79 20 61 6e 64 20 63 6f 6c 6c 61  finity and colla
2726e 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 61 73  tion sequence as
2726f 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
27270 65 0a 20 20 20 20 20 20 2a 2a 20 70 61 72 65 6e  e.      ** paren
27271 74 20 74 61 62 6c 65 20 61 72 65 20 75 73 65 64  t table are used
27272 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 61 72 69   for the compari
27273 73 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20 20 70 45  son. */.      pE
27274 71 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  q = sqlite3PExpr
27275 28 70 50 61 72 73 65 2c 20 54 4b 5f 45 51 2c 0a  (pParse, TK_EQ,.
27276 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
27277 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
27278 4b 5f 44 4f 54 2c 20 0a 20 20 20 20 20 20 20 20  K_DOT, .        
27279 20 20 20 20 73 71 6c 69 74 65 33 50 45 78 70 72      sqlite3PExpr
2727a 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20  (pParse, TK_ID, 
2727b 30 2c 20 30 2c 20 26 74 4f 6c 64 29 2c 0a 20 20  0, 0, &tOld),.  
2727c 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2727d 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2727e 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74 54 6f  K_ID, 0, 0, &tTo
2727f 43 6f 6c 29 0a 20 20 20 20 20 20 20 20 20 20 2c  Col).          ,
27280 20 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20 73   0),.          s
27281 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
27282 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c  se, TK_ID, 0, 0,
27283 20 26 74 46 72 6f 6d 43 6f 6c 29 0a 20 20 20 20   &tFromCol).    
27284 20 20 2c 20 30 29 3b 0a 20 20 20 20 20 20 70 57    , 0);.      pW
27285 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78  here = sqlite3Ex
27286 70 72 41 6e 64 28 64 62 2c 20 70 57 68 65 72 65  prAnd(db, pWhere
27287 2c 20 70 45 71 29 3b 0a 0a 20 20 20 20 20 20 2f  , pEq);..      /
27288 2a 20 46 6f 72 20 4f 4e 20 55 50 44 41 54 45 2c  * For ON UPDATE,
27289 20 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20 6e   construct the n
2728a 65 78 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ext term of the 
2728b 57 48 45 4e 20 63 6c 61 75 73 65 2e 0a 20 20 20  WHEN clause..   
2728c 20 20 20 2a 2a 20 54 68 65 20 66 69 6e 61 6c 20     ** The final 
2728d 57 48 45 4e 20 63 6c 61 75 73 65 20 77 69 6c 6c  WHEN clause will
2728e 20 62 65 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20   be like this:. 
2728f 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
27290 20 20 20 20 57 48 45 4e 20 4e 4f 54 28 6f 6c 64      WHEN NOT(old
27291 2e 63 6f 6c 31 20 49 53 20 6e 65 77 2e 63 6f 6c  .col1 IS new.col
27292 31 20 41 4e 44 20 2e 2e 2e 20 41 4e 44 20 6f 6c  1 AND ... AND ol
27293 64 2e 63 6f 6c 4e 20 49 53 20 6e 65 77 2e 63 6f  d.colN IS new.co
27294 6c 4e 29 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  lN).      */.   
27295 20 20 20 69 66 28 20 70 43 68 61 6e 67 65 73 20     if( pChanges 
27296 29 7b 0a 20 20 20 20 20 20 20 20 70 45 71 20 3d  ){.        pEq =
27297 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
27298 61 72 73 65 2c 20 54 4b 5f 49 53 2c 0a 20 20 20  arse, TK_IS,.   
27299 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2729a 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2729b 5f 44 4f 54 2c 20 0a 20 20 20 20 20 20 20 20 20  _DOT, .         
2729c 20 20 20 20 20 73 71 6c 69 74 65 33 50 45 78 70       sqlite3PExp
2729d 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c  r(pParse, TK_ID,
2729e 20 30 2c 20 30 2c 20 26 74 4f 6c 64 29 2c 0a 20   0, 0, &tOld),. 
2729f 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
272a0 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
272a1 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26  , TK_ID, 0, 0, &
272a2 74 54 6f 43 6f 6c 29 2c 0a 20 20 20 20 20 20 20  tToCol),.       
272a3 20 20 20 20 20 20 20 30 29 2c 0a 20 20 20 20 20         0),.     
272a4 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 45         sqlite3PE
272a5 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44  xpr(pParse, TK_D
272a6 4f 54 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  OT, .           
272a7 20 20 20 73 71 6c 69 74 65 33 50 45 78 70 72 28     sqlite3PExpr(
272a8 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30  pParse, TK_ID, 0
272a9 2c 20 30 2c 20 26 74 4e 65 77 29 2c 0a 20 20 20  , 0, &tNew),.   
272aa 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
272ab 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
272ac 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74 54  TK_ID, 0, 0, &tT
272ad 6f 43 6f 6c 29 2c 0a 20 20 20 20 20 20 20 20 20  oCol),.         
272ae 20 20 20 20 20 30 29 2c 0a 20 20 20 20 20 20 20       0),.       
272af 20 20 20 20 20 30 29 3b 0a 20 20 20 20 20 20 20       0);.       
272b0 20 70 57 68 65 6e 20 3d 20 73 71 6c 69 74 65 33   pWhen = sqlite3
272b1 45 78 70 72 41 6e 64 28 64 62 2c 20 70 57 68 65  ExprAnd(db, pWhe
272b2 6e 2c 20 70 45 71 29 3b 0a 20 20 20 20 20 20 7d  n, pEq);.      }
272b3 0a 20 20 0a 20 20 20 20 20 20 69 66 28 20 61 63  .  .      if( ac
272b4 74 69 6f 6e 21 3d 4f 45 5f 52 65 73 74 72 69 63  tion!=OE_Restric
272b5 74 20 26 26 20 28 61 63 74 69 6f 6e 21 3d 4f 45  t && (action!=OE
272b6 5f 43 61 73 63 61 64 65 20 7c 7c 20 70 43 68 61  _Cascade || pCha
272b7 6e 67 65 73 29 20 29 7b 0a 20 20 20 20 20 20 20  nges) ){.       
272b8 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 20   Expr *pNew;.   
272b9 20 20 20 20 20 69 66 28 20 61 63 74 69 6f 6e 3d       if( action=
272ba 3d 4f 45 5f 43 61 73 63 61 64 65 20 29 7b 0a 20  =OE_Cascade ){. 
272bb 20 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20           pNew = 
272bc 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
272bd 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 0a 20 20  rse, TK_DOT, .  
272be 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
272bf 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
272c0 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 74 4e 65  K_ID, 0, 0, &tNe
272c1 77 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  w),.            
272c2 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
272c3 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30  rse, TK_ID, 0, 0
272c4 2c 20 26 74 54 6f 43 6f 6c 29 0a 20 20 20 20 20  , &tToCol).     
272c5 20 20 20 20 20 2c 20 30 29 3b 0a 20 20 20 20 20       , 0);.     
272c6 20 20 20 7d 65 6c 73 65 20 69 66 28 20 61 63 74     }else if( act
272c7 69 6f 6e 3d 3d 4f 45 5f 53 65 74 44 66 6c 74 20  ion==OE_SetDflt 
272c8 29 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78 70  ){.          Exp
272c9 72 20 2a 70 44 66 6c 74 20 3d 20 70 46 4b 65 79  r *pDflt = pFKey
272ca 2d 3e 70 46 72 6f 6d 2d 3e 61 43 6f 6c 5b 69 46  ->pFrom->aCol[iF
272cb 72 6f 6d 43 6f 6c 5d 2e 70 44 66 6c 74 3b 0a 20  romCol].pDflt;. 
272cc 20 20 20 20 20 20 20 20 20 69 66 28 20 70 44 66           if( pDf
272cd 6c 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  lt ){.          
272ce 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
272cf 45 78 70 72 44 75 70 28 64 62 2c 20 70 44 66 6c  ExprDup(db, pDfl
272d0 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20  t, 0);.         
272d1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
272d2 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74      pNew = sqlit
272d3 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
272d4 54 4b 5f 4e 55 4c 4c 2c 20 30 2c 20 30 2c 20 30  TK_NULL, 0, 0, 0
272d5 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
272d6 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
272d7 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73          pNew = s
272d8 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
272d9 73 65 2c 20 54 4b 5f 4e 55 4c 4c 2c 20 30 2c 20  se, TK_NULL, 0, 
272da 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d  0, 0);.        }
272db 0a 20 20 20 20 20 20 20 20 70 4c 69 73 74 20 3d  .        pList =
272dc 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
272dd 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70  Append(pParse, p
272de 4c 69 73 74 2c 20 70 4e 65 77 29 3b 0a 20 20 20  List, pNew);.   
272df 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
272e0 4c 69 73 74 53 65 74 4e 61 6d 65 28 70 50 61 72  ListSetName(pPar
272e1 73 65 2c 20 70 4c 69 73 74 2c 20 26 74 46 72 6f  se, pList, &tFro
272e2 6d 43 6f 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20  mCol, 0);.      
272e3 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
272e4 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 61 69  te3DbFree(db, ai
272e5 43 6f 6c 29 3b 0a 0a 20 20 20 20 7a 46 72 6f 6d  Col);..    zFrom
272e6 20 3d 20 70 46 4b 65 79 2d 3e 70 46 72 6f 6d 2d   = pFKey->pFrom-
272e7 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 46 72 6f  >zName;.    nFro
272e8 6d 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65  m = sqlite3Strle
272e9 6e 33 30 28 7a 46 72 6f 6d 29 3b 0a 0a 20 20 20  n30(zFrom);..   
272ea 20 69 66 28 20 61 63 74 69 6f 6e 3d 3d 4f 45 5f   if( action==OE_
272eb 52 65 73 74 72 69 63 74 20 29 7b 0a 20 20 20 20  Restrict ){.    
272ec 20 20 54 6f 6b 65 6e 20 74 46 72 6f 6d 3b 0a 20    Token tFrom;. 
272ed 20 20 20 20 20 45 78 70 72 20 2a 70 52 61 69 73       Expr *pRais
272ee 65 3b 20 0a 0a 20 20 20 20 20 20 74 46 72 6f 6d  e; ..      tFrom
272ef 2e 7a 20 3d 20 7a 46 72 6f 6d 3b 0a 20 20 20 20  .z = zFrom;.    
272f0 20 20 74 46 72 6f 6d 2e 6e 20 3d 20 6e 46 72 6f    tFrom.n = nFro
272f1 6d 3b 0a 20 20 20 20 20 20 70 52 61 69 73 65 20  m;.      pRaise 
272f2 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62  = sqlite3Expr(db
272f3 2c 20 54 4b 5f 52 41 49 53 45 2c 20 22 66 6f 72  , TK_RAISE, "for
272f4 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61  eign key constra
272f5 69 6e 74 20 66 61 69 6c 65 64 22 29 3b 0a 20 20  int failed");.  
272f6 20 20 20 20 69 66 28 20 70 52 61 69 73 65 20 29      if( pRaise )
272f7 7b 0a 20 20 20 20 20 20 20 20 70 52 61 69 73 65  {.        pRaise
272f8 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 4f 45 5f  ->affinity = OE_
272f9 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20  Abort;.      }. 
272fa 20 20 20 20 20 70 53 65 6c 65 63 74 20 3d 20 73       pSelect = s
272fb 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28  qlite3SelectNew(
272fc 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
272fd 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
272fe 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
272ff 20 30 2c 20 70 52 61 69 73 65 29 2c 0a 20 20 20   0, pRaise),.   
27300 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72         sqlite3Sr
27301 63 4c 69 73 74 41 70 70 65 6e 64 28 64 62 2c 20  cListAppend(db, 
27302 30 2c 20 26 74 46 72 6f 6d 2c 20 30 29 2c 0a 20  0, &tFrom, 0),. 
27303 20 20 20 20 20 20 20 20 20 70 57 68 65 72 65 2c           pWhere,
27304 0a 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 2c  .          0, 0,
27305 20 30 2c 20 30 2c 20 30 2c 20 30 0a 20 20 20 20   0, 0, 0, 0.    
27306 20 20 29 3b 0a 20 20 20 20 20 20 70 57 68 65 72    );.      pWher
27307 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20  e = 0;.    }..  
27308 20 20 2f 2a 20 49 6e 20 74 68 65 20 63 75 72 72    /* In the curr
27309 65 6e 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ent implementati
2730a 6f 6e 2c 20 70 54 61 62 2d 3e 64 62 4d 65 6d 3d  on, pTab->dbMem=
2730b 3d 30 20 66 6f 72 20 61 6c 6c 20 74 61 62 6c 65  =0 for all table
2730c 73 20 65 78 63 65 70 74 0a 20 20 20 20 2a 2a 20  s except.    ** 
2730d 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 74 61  for temporary ta
2730e 62 6c 65 73 20 75 73 65 64 20 74 6f 20 64 65 73  bles used to des
2730f 63 72 69 62 65 20 73 75 62 71 75 65 72 69 65 73  cribe subqueries
27310 2e 20 20 41 6e 64 20 74 65 6d 70 6f 72 61 72 79  .  And temporary
27311 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65 73 20 64  .    ** tables d
27312 6f 20 6e 6f 74 20 68 61 76 65 20 66 6f 72 65 69  o not have forei
27313 67 6e 20 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e  gn key constrain
27314 74 73 2e 20 20 48 65 6e 63 65 2c 20 70 54 61 62  ts.  Hence, pTab
27315 2d 3e 64 62 4d 65 6d 0a 20 20 20 20 2a 2a 20 73  ->dbMem.    ** s
27316 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20  hould always be 
27317 30 20 74 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a  0 there..    */.
27318 20 20 20 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73      enableLookas
27319 69 64 65 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73  ide = db->lookas
2731a 69 64 65 2e 62 45 6e 61 62 6c 65 64 3b 0a 20 20  ide.bEnabled;.  
2731b 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
2731c 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 0a 20  bEnabled = 0;.. 
2731d 20 20 20 70 54 72 69 67 67 65 72 20 3d 20 28 54     pTrigger = (T
2731e 72 69 67 67 65 72 20 2a 29 73 71 6c 69 74 65 33  rigger *)sqlite3
2731f 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
27320 20 0a 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66   .        sizeof
27321 28 54 72 69 67 67 65 72 29 20 2b 20 20 20 20 20  (Trigger) +     
27322 20 20 20 20 2f 2a 20 73 74 72 75 63 74 20 54 72      /* struct Tr
27323 69 67 67 65 72 20 2a 2f 0a 20 20 20 20 20 20 20  igger */.       
27324 20 73 69 7a 65 6f 66 28 54 72 69 67 67 65 72 53   sizeof(TriggerS
27325 74 65 70 29 20 2b 20 20 20 20 20 2f 2a 20 53 69  tep) +     /* Si
27326 6e 67 6c 65 20 73 74 65 70 20 69 6e 20 74 72 69  ngle step in tri
27327 67 67 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a  gger program */.
27328 20 20 20 20 20 20 20 20 6e 46 72 6f 6d 20 2b 20          nFrom + 
27329 31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1               
2732a 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 70    /* Space for p
2732b 53 74 65 70 2d 3e 74 61 72 67 65 74 2e 7a 20 2a  Step->target.z *
2732c 2f 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28  /.    );.    if(
2732d 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20   pTrigger ){.   
2732e 20 20 20 70 53 74 65 70 20 3d 20 70 54 72 69 67     pStep = pTrig
2732f 67 65 72 2d 3e 73 74 65 70 5f 6c 69 73 74 20 3d  ger->step_list =
27330 20 28 54 72 69 67 67 65 72 53 74 65 70 20 2a 29   (TriggerStep *)
27331 26 70 54 72 69 67 67 65 72 5b 31 5d 3b 0a 20 20  &pTrigger[1];.  
27332 20 20 20 20 70 53 74 65 70 2d 3e 74 61 72 67 65      pStep->targe
27333 74 2e 7a 20 3d 20 28 63 68 61 72 20 2a 29 26 70  t.z = (char *)&p
27334 53 74 65 70 5b 31 5d 3b 0a 20 20 20 20 20 20 70  Step[1];.      p
27335 53 74 65 70 2d 3e 74 61 72 67 65 74 2e 6e 20 3d  Step->target.n =
27336 20 6e 46 72 6f 6d 3b 0a 20 20 20 20 20 20 6d 65   nFrom;.      me
27337 6d 63 70 79 28 28 63 68 61 72 20 2a 29 70 53 74  mcpy((char *)pSt
27338 65 70 2d 3e 74 61 72 67 65 74 2e 7a 2c 20 7a 46  ep->target.z, zF
27339 72 6f 6d 2c 20 6e 46 72 6f 6d 29 3b 0a 20 20 0a  rom, nFrom);.  .
2733a 20 20 20 20 20 20 70 53 74 65 70 2d 3e 70 57 68        pStep->pWh
2733b 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ere = sqlite3Exp
2733c 72 44 75 70 28 64 62 2c 20 70 57 68 65 72 65 2c  rDup(db, pWhere,
2733d 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45 29   EXPRDUP_REDUCE)
2733e 3b 0a 20 20 20 20 20 20 70 53 74 65 70 2d 3e 70  ;.      pStep->p
2733f 45 78 70 72 4c 69 73 74 20 3d 20 73 71 6c 69 74  ExprList = sqlit
27340 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62  e3ExprListDup(db
27341 2c 20 70 4c 69 73 74 2c 20 45 58 50 52 44 55 50  , pList, EXPRDUP
27342 5f 52 45 44 55 43 45 29 3b 0a 20 20 20 20 20 20  _REDUCE);.      
27343 70 53 74 65 70 2d 3e 70 53 65 6c 65 63 74 20 3d  pStep->pSelect =
27344 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
27345 70 28 64 62 2c 20 70 53 65 6c 65 63 74 2c 20 45  p(db, pSelect, E
27346 58 50 52 44 55 50 5f 52 45 44 55 43 45 29 3b 0a  XPRDUP_REDUCE);.
27347 20 20 20 20 20 20 69 66 28 20 70 57 68 65 6e 20        if( pWhen 
27348 29 7b 0a 20 20 20 20 20 20 20 20 70 57 68 65 6e  ){.        pWhen
27349 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2734a 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20  pParse, TK_NOT, 
2734b 70 57 68 65 6e 2c 20 30 2c 20 30 29 3b 0a 20 20  pWhen, 0, 0);.  
2734c 20 20 20 20 20 20 70 54 72 69 67 67 65 72 2d 3e        pTrigger->
2734d 70 57 68 65 6e 20 3d 20 73 71 6c 69 74 65 33 45  pWhen = sqlite3E
2734e 78 70 72 44 75 70 28 64 62 2c 20 70 57 68 65 6e  xprDup(db, pWhen
2734f 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55 43 45  , EXPRDUP_REDUCE
27350 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
27351 0a 0a 20 20 20 20 2f 2a 20 52 65 2d 65 6e 61 62  ..    /* Re-enab
27352 6c 65 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65  le the lookaside
27353 20 62 75 66 66 65 72 2c 20 69 66 20 69 74 20 77   buffer, if it w
27354 61 73 20 64 69 73 61 62 6c 65 64 20 65 61 72 6c  as disabled earl
27355 69 65 72 2e 20 2a 2f 0a 20 20 20 20 64 62 2d 3e  ier. */.    db->
27356 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c  lookaside.bEnabl
27357 65 64 20 3d 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61  ed = enableLooka
27358 73 69 64 65 3b 0a 0a 20 20 20 20 73 71 6c 69 74  side;..    sqlit
27359 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
2735a 20 70 57 68 65 72 65 29 3b 0a 20 20 20 20 73 71   pWhere);.    sq
2735b 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
2735c 64 62 2c 20 70 57 68 65 6e 29 3b 0a 20 20 20 20  db, pWhen);.    
2735d 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2735e 65 6c 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29  elete(db, pList)
2735f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c  ;.    sqlite3Sel
27360 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53  ectDelete(db, pS
27361 65 6c 65 63 74 29 3b 0a 20 20 20 20 69 66 28 20  elect);.    if( 
27362 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
27363 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 66 6b 54  ==1 ){.      fkT
27364 72 69 67 67 65 72 44 65 6c 65 74 65 28 64 62 2c  riggerDelete(db,
27365 20 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20   pTrigger);.    
27366 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
27367 7d 0a 0a 20 20 20 20 73 77 69 74 63 68 28 20 61  }..    switch( a
27368 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 63  ction ){.      c
27369 61 73 65 20 4f 45 5f 52 65 73 74 72 69 63 74 3a  ase OE_Restrict:
2736a 0a 20 20 20 20 20 20 20 20 70 53 74 65 70 2d 3e  .        pStep->
2736b 6f 70 20 3d 20 54 4b 5f 53 45 4c 45 43 54 3b 20  op = TK_SELECT; 
2736c 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2736d 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 43 61        case OE_Ca
2736e 73 63 61 64 65 3a 20 0a 20 20 20 20 20 20 20 20  scade: .        
2736f 69 66 28 20 21 70 43 68 61 6e 67 65 73 20 29 7b  if( !pChanges ){
27370 20 0a 20 20 20 20 20 20 20 20 20 20 70 53 74 65   .          pSte
27371 70 2d 3e 6f 70 20 3d 20 54 4b 5f 44 45 4c 45 54  p->op = TK_DELET
27372 45 3b 20 0a 20 20 20 20 20 20 20 20 20 20 62 72  E; .          br
27373 65 61 6b 3b 20 0a 20 20 20 20 20 20 20 20 7d 0a  eak; .        }.
27374 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20        default:. 
27375 20 20 20 20 20 20 20 70 53 74 65 70 2d 3e 6f 70         pStep->op
27376 20 3d 20 54 4b 5f 55 50 44 41 54 45 3b 0a 20 20   = TK_UPDATE;.  
27377 20 20 7d 0a 20 20 20 20 70 53 74 65 70 2d 3e 70    }.    pStep->p
27378 54 72 69 67 20 3d 20 70 54 72 69 67 67 65 72 3b  Trig = pTrigger;
27379 0a 20 20 20 20 70 54 72 69 67 67 65 72 2d 3e 70  .    pTrigger->p
2737a 53 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70  Schema = pTab->p
2737b 53 63 68 65 6d 61 3b 0a 20 20 20 20 70 54 72 69  Schema;.    pTri
2737c 67 67 65 72 2d 3e 70 54 61 62 53 63 68 65 6d 61  gger->pTabSchema
2737d 20 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61   = pTab->pSchema
2737e 3b 0a 20 20 20 20 70 46 4b 65 79 2d 3e 61 70 54  ;.    pFKey->apT
2737f 72 69 67 67 65 72 5b 69 41 63 74 69 6f 6e 5d 20  rigger[iAction] 
27380 3d 20 70 54 72 69 67 67 65 72 3b 0a 20 20 20 20  = pTrigger;.    
27381 70 54 72 69 67 67 65 72 2d 3e 6f 70 20 3d 20 28  pTrigger->op = (
27382 70 43 68 61 6e 67 65 73 20 3f 20 54 4b 5f 55 50  pChanges ? TK_UP
27383 44 41 54 45 20 3a 20 54 4b 5f 44 45 4c 45 54 45  DATE : TK_DELETE
27384 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  );.  }..  return
27385 20 70 54 72 69 67 67 65 72 3b 0a 7d 0a 0a 2f 2a   pTrigger;.}../*
27386 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
27387 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  n is called when
27388 20 64 65 6c 65 74 69 6e 67 20 6f 72 20 75 70 64   deleting or upd
27389 61 74 69 6e 67 20 61 20 72 6f 77 20 74 6f 20 69  ating a row to i
2738a 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 61 6e 79 20  mplement.** any 
2738b 72 65 71 75 69 72 65 64 20 43 41 53 43 41 44 45  required CASCADE
2738c 2c 20 53 45 54 20 4e 55 4c 4c 20 6f 72 20 53 45  , SET NULL or SE
2738d 54 20 44 45 46 41 55 4c 54 20 61 63 74 69 6f 6e  T DEFAULT action
2738e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
2738f 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
27390 33 46 6b 41 63 74 69 6f 6e 73 28 0a 20 20 50 61  3FkActions(.  Pa
27391 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
27392 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
27393 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a   Parse context *
27394 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  /.  Table *pTab,
27395 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27396 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69      /* Table bei
27397 6e 67 20 75 70 64 61 74 65 64 20 6f 72 20 64 65  ng updated or de
27398 6c 65 74 65 64 20 66 72 6f 6d 20 2a 2f 0a 20 20  leted from */.  
27399 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67  ExprList *pChang
2739a 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  es,             
2739b 2f 2a 20 43 68 61 6e 67 65 2d 6c 69 73 74 20 66  /* Change-list f
2739c 6f 72 20 55 50 44 41 54 45 2c 20 4e 55 4c 4c 20  or UPDATE, NULL 
2739d 66 6f 72 20 44 45 4c 45 54 45 20 2a 2f 0a 20 20  for DELETE */.  
2739e 69 6e 74 20 72 65 67 4f 6c 64 20 20 20 20 20 20  int regOld      
2739f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
273a0 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 61 72  /* Address of ar
273a1 72 61 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6f  ray containing o
273a2 6c 64 20 72 6f 77 20 2a 2f 0a 29 7b 0a 20 20 2f  ld row */.){.  /
273a3 2a 20 49 66 20 66 6f 72 65 69 67 6e 2d 6b 65 79  * If foreign-key
273a4 20 73 75 70 70 6f 72 74 20 69 73 20 65 6e 61 62   support is enab
273a5 6c 65 64 2c 20 69 74 65 72 61 74 65 20 74 68 72  led, iterate thr
273a6 6f 75 67 68 20 61 6c 6c 20 46 4b 73 20 74 68 61  ough all FKs tha
273a7 74 20 0a 20 20 2a 2a 20 72 65 66 65 72 20 74 6f  t .  ** refer to
273a8 20 74 61 62 6c 65 20 70 54 61 62 2e 20 49 66 20   table pTab. If 
273a9 74 68 65 72 65 20 69 73 20 61 6e 20 61 63 74 69  there is an acti
273aa 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  on associated wi
273ab 74 68 20 74 68 65 20 46 4b 20 0a 20 20 2a 2a 20  th the FK .  ** 
273ac 66 6f 72 20 74 68 69 73 20 6f 70 65 72 61 74 69  for this operati
273ad 6f 6e 20 28 65 69 74 68 65 72 20 75 70 64 61 74  on (either updat
273ae 65 20 6f 72 20 64 65 6c 65 74 65 29 2c 20 69 6e  e or delete), in
273af 76 6f 6b 65 20 74 68 65 20 61 73 73 6f 63 69 61  voke the associa
273b0 74 65 64 20 0a 20 20 2a 2a 20 74 72 69 67 67 65  ted .  ** trigge
273b1 72 20 73 75 62 2d 70 72 6f 67 72 61 6d 2e 20 20  r sub-program.  
273b2 2a 2f 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  */.  if( pParse-
273b3 3e 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54  >db->flags&SQLIT
273b4 45 5f 46 6f 72 65 69 67 6e 4b 65 79 73 20 29 7b  E_ForeignKeys ){
273b5 0a 20 20 20 20 46 4b 65 79 20 2a 70 46 4b 65 79  .    FKey *pFKey
273b6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
273b7 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72 20 76     /* Iterator v
273b8 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 20 20 66  ariable */.    f
273b9 6f 72 28 70 46 4b 65 79 20 3d 20 73 71 6c 69 74  or(pFKey = sqlit
273ba 65 33 46 6b 52 65 66 65 72 65 6e 63 65 73 28 70  e3FkReferences(p
273bb 54 61 62 29 3b 20 70 46 4b 65 79 3b 20 70 46 4b  Tab); pFKey; pFK
273bc 65 79 3d 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54  ey=pFKey->pNextT
273bd 6f 29 7b 0a 20 20 20 20 20 20 54 72 69 67 67 65  o){.      Trigge
273be 72 20 2a 70 41 63 74 69 6f 6e 20 3d 20 66 6b 41  r *pAction = fkA
273bf 63 74 69 6f 6e 54 72 69 67 67 65 72 28 70 50 61  ctionTrigger(pPa
273c0 72 73 65 2c 20 70 54 61 62 2c 20 70 46 4b 65 79  rse, pTab, pFKey
273c1 2c 20 70 43 68 61 6e 67 65 73 29 3b 0a 20 20 20  , pChanges);.   
273c2 20 20 20 69 66 28 20 70 41 63 74 69 6f 6e 20 29     if( pAction )
273c3 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
273c4 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 44  3CodeRowTriggerD
273c5 69 72 65 63 74 28 70 50 61 72 73 65 2c 20 70 41  irect(pParse, pA
273c6 63 74 69 6f 6e 2c 20 70 54 61 62 2c 20 72 65 67  ction, pTab, reg
273c7 4f 6c 64 2c 20 4f 45 5f 41 62 6f 72 74 2c 20 30  Old, OE_Abort, 0
273c8 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
273c9 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  .  }.}..#endif /
273ca 2a 20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  * ifndef SQLITE_
273cb 4f 4d 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a  OMIT_TRIGGER */.
273cc 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6c 6c 20  ./*.** Free all 
273cd 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65  memory associate
273ce 64 20 77 69 74 68 20 66 6f 72 65 69 67 6e 20 6b  d with foreign k
273cf 65 79 20 64 65 66 69 6e 69 74 69 6f 6e 73 20 61  ey definitions a
273d0 74 74 61 63 68 65 64 20 74 6f 0a 2a 2a 20 74 61  ttached to.** ta
273d1 62 6c 65 20 70 54 61 62 2e 20 52 65 6d 6f 76 65  ble pTab. Remove
273d2 20 74 68 65 20 64 65 6c 65 74 65 64 20 66 6f 72   the deleted for
273d3 65 69 67 6e 20 6b 65 79 73 20 66 72 6f 6d 20 74  eign keys from t
273d4 68 65 20 53 63 68 65 6d 61 2e 66 6b 65 79 48 61  he Schema.fkeyHa
273d5 73 68 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65  sh.** hash table
273d6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
273d7 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
273d8 46 6b 44 65 6c 65 74 65 28 54 61 62 6c 65 20 2a  FkDelete(Table *
273d9 70 54 61 62 29 7b 0a 20 20 46 4b 65 79 20 2a 70  pTab){.  FKey *p
273da 46 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20  FKey;           
273db 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72           /* Iter
273dc 61 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f  ator variable */
273dd 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 3b 20  .  FKey *pNext; 
273de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
273df 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70 46     /* Copy of pF
273e0 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 20 2a  Key->pNextFrom *
273e1 2f 0a 0a 20 20 66 6f 72 28 70 46 4b 65 79 3d 70  /..  for(pFKey=p
273e2 54 61 62 2d 3e 70 46 4b 65 79 3b 20 70 46 4b 65  Tab->pFKey; pFKe
273e3 79 3b 20 70 46 4b 65 79 3d 70 4e 65 78 74 29 7b  y; pFKey=pNext){
273e4 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20  ..    /* Remove 
273e5 74 68 65 20 46 4b 20 66 72 6f 6d 20 74 68 65 20  the FK from the 
273e6 66 6b 65 79 48 61 73 68 20 68 61 73 68 20 74 61  fkeyHash hash ta
273e7 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ble. */.    if( 
273e8 70 46 4b 65 79 2d 3e 70 50 72 65 76 54 6f 20 29  pFKey->pPrevTo )
273e9 7b 0a 20 20 20 20 20 20 70 46 4b 65 79 2d 3e 70  {.      pFKey->p
273ea 50 72 65 76 54 6f 2d 3e 70 4e 65 78 74 54 6f 20  PrevTo->pNextTo 
273eb 3d 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f  = pFKey->pNextTo
273ec 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
273ed 20 20 20 76 6f 69 64 20 2a 64 61 74 61 20 3d 20     void *data = 
273ee 28 76 6f 69 64 20 2a 29 70 46 4b 65 79 2d 3e 70  (void *)pFKey->p
273ef 4e 65 78 74 54 6f 3b 0a 20 20 20 20 20 20 63 6f  NextTo;.      co
273f0 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 64  nst char *z = (d
273f1 61 74 61 20 3f 20 70 46 4b 65 79 2d 3e 70 4e 65  ata ? pFKey->pNe
273f2 78 74 54 6f 2d 3e 7a 54 6f 20 3a 20 70 46 4b 65  xtTo->zTo : pFKe
273f3 79 2d 3e 7a 54 6f 29 3b 0a 20 20 20 20 20 20 73  y->zTo);.      s
273f4 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74  qlite3HashInsert
273f5 28 26 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 2d  (&pTab->pSchema-
273f6 3e 66 6b 65 79 48 61 73 68 2c 20 7a 2c 20 73 71  >fkeyHash, z, sq
273f7 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 29  lite3Strlen30(z)
273f8 2c 20 64 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20  , data);.    }. 
273f9 20 20 20 69 66 28 20 70 46 4b 65 79 2d 3e 70 4e     if( pFKey->pN
273fa 65 78 74 54 6f 20 29 7b 0a 20 20 20 20 20 20 70  extTo ){.      p
273fb 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 2d 3e 70  FKey->pNextTo->p
273fc 50 72 65 76 54 6f 20 3d 20 70 46 4b 65 79 2d 3e  PrevTo = pFKey->
273fd 70 50 72 65 76 54 6f 3b 0a 20 20 20 20 7d 0a 0a  pPrevTo;.    }..
273fe 20 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 6e      /* Delete an
273ff 79 20 74 72 69 67 67 65 72 73 20 63 72 65 61 74  y triggers creat
27400 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
27401 61 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 69 73  actions for this
27402 20 46 4b 2e 20 2a 2f 0a 23 69 66 6e 64 65 66 20   FK. */.#ifndef 
27403 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
27404 47 45 52 0a 20 20 20 20 66 6b 54 72 69 67 67 65  GER.    fkTrigge
27405 72 44 65 6c 65 74 65 28 70 54 61 62 2d 3e 64 62  rDelete(pTab->db
27406 4d 65 6d 2c 20 70 46 4b 65 79 2d 3e 61 70 54 72  Mem, pFKey->apTr
27407 69 67 67 65 72 5b 30 5d 29 3b 0a 20 20 20 20 66  igger[0]);.    f
27408 6b 54 72 69 67 67 65 72 44 65 6c 65 74 65 28 70  kTriggerDelete(p
27409 54 61 62 2d 3e 64 62 4d 65 6d 2c 20 70 46 4b 65  Tab->dbMem, pFKe
2740a 79 2d 3e 61 70 54 72 69 67 67 65 72 5b 31 5d 29  y->apTrigger[1])
2740b 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a  ;.#endif..    /*
2740c 20 45 56 3a 20 52 2d 33 30 33 32 33 2d 32 31 39   EV: R-30323-219
2740d 31 37 20 45 61 63 68 20 66 6f 72 65 69 67 6e 20  17 Each foreign 
2740e 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 69  key constraint i
2740f 6e 20 53 51 4c 69 74 65 20 69 73 0a 20 20 20 20  n SQLite is.    
27410 2a 2a 20 63 6c 61 73 73 69 66 69 65 64 20 61 73  ** classified as
27411 20 65 69 74 68 65 72 20 69 6d 6d 65 64 69 61 74   either immediat
27412 65 20 6f 72 20 64 65 66 65 72 72 65 64 2e 0a 20  e or deferred.. 
27413 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74     */.    assert
27414 28 20 70 46 4b 65 79 2d 3e 69 73 44 65 66 65 72  ( pFKey->isDefer
27415 72 65 64 3d 3d 30 20 7c 7c 20 70 46 4b 65 79 2d  red==0 || pFKey-
27416 3e 69 73 44 65 66 65 72 72 65 64 3d 3d 31 20 29  >isDeferred==1 )
27417 3b 0a 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70  ;..    pNext = p
27418 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d 3b  FKey->pNextFrom;
27419 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2741a 65 65 28 70 54 61 62 2d 3e 64 62 4d 65 6d 2c 20  ee(pTab->dbMem, 
2741b 70 46 4b 65 79 29 3b 0a 20 20 7d 0a 7d 0a 23 65  pFKey);.  }.}.#e
2741c 6e 64 69 66 20 2f 2a 20 69 66 6e 64 65 66 20 53  ndif /* ifndef S
2741d 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
2741e 47 4e 5f 4b 45 59 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  GN_KEY */../****
2741f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
27420 66 20 66 6b 65 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f fkey.c *******
27421 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27422 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27423 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
27424 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
27425 20 66 69 6c 65 20 69 6e 73 65 72 74 2e 63 20 2a   file insert.c *
27426 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27427 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27428 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
27429 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
2742a 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
2742b 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
2742c 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
2742d 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
2742e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
2742f 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
27430 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
27431 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
27432 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
27433 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
27434 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
27435 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
27436 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
27437 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
27438 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
27439 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
2743a 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
2743b 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
2743c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2743d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2743e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2743f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27440 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
27441 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72  ontains C code r
27442 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61 72 65  outines that are
27443 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70   called by the p
27444 61 72 73 65 72 0a 2a 2a 20 74 6f 20 68 61 6e 64  arser.** to hand
27445 6c 65 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d  le INSERT statem
27446 65 6e 74 73 20 69 6e 20 53 51 4c 69 74 65 2e 0a  ents in SQLite..
27447 2a 2a 0a 2a 2a 20 24 49 64 3a 20 69 6e 73 65 72  **.** $Id: inser
27448 74 2e 63 2c 76 20 31 2e 32 37 30 20 32 30 30 39  t.c,v 1.270 2009
27449 2f 30 37 2f 32 34 20 31 37 3a 35 38 3a 35 33 20  /07/24 17:58:53 
2744a 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
2744b 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  $.*/../*.** Gene
2744c 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77  rate code that w
2744d 69 6c 6c 20 6f 70 65 6e 20 61 20 74 61 62 6c 65  ill open a table
2744e 20 66 6f 72 20 72 65 61 64 69 6e 67 2e 0a 2a 2f   for reading..*/
2744f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
27450 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65 6e  void sqlite3Open
27451 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a  Table(.  Parse *
27452 70 2c 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65  p,       /* Gene
27453 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20 74  rate code into t
27454 68 69 73 20 56 44 42 45 20 2a 2f 0a 20 20 69 6e  his VDBE */.  in
27455 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 2f 2a  t iCur,       /*
27456 20 54 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62   The cursor numb
27457 65 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  er of the table 
27458 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20 20  */.  int iDb,   
27459 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
2745a 62 61 73 65 20 69 6e 64 65 78 20 69 6e 20 73 71  base index in sq
2745b 6c 69 74 65 33 2e 61 44 62 5b 5d 20 2a 2f 0a 20  lite3.aDb[] */. 
2745c 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
2745d 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f   /* The table to
2745e 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20 20   be opened */.  
2745f 69 6e 74 20 6f 70 63 6f 64 65 20 20 20 20 20 20  int opcode      
27460 2f 2a 20 4f 50 5f 4f 70 65 6e 52 65 61 64 20 6f  /* OP_OpenRead o
27461 72 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 2a  r OP_OpenWrite *
27462 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  /.){.  Vdbe *v;.
27463 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
27464 70 54 61 62 29 20 29 20 72 65 74 75 72 6e 3b 0a  pTab) ) return;.
27465 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
27466 56 64 62 65 28 70 29 3b 0a 20 20 61 73 73 65 72  Vdbe(p);.  asser
27467 74 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70  t( opcode==OP_Op
27468 65 6e 57 72 69 74 65 20 7c 7c 20 6f 70 63 6f 64  enWrite || opcod
27469 65 3d 3d 4f 50 5f 4f 70 65 6e 52 65 61 64 20 29  e==OP_OpenRead )
2746a 3b 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65  ;.  sqlite3Table
2746b 4c 6f 63 6b 28 70 2c 20 69 44 62 2c 20 70 54 61  Lock(p, iDb, pTa
2746c 62 2d 3e 74 6e 75 6d 2c 20 28 6f 70 63 6f 64 65  b->tnum, (opcode
2746d 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65 29 3f  ==OP_OpenWrite)?
2746e 31 3a 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  1:0, pTab->zName
2746f 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
27470 41 64 64 4f 70 33 28 76 2c 20 6f 70 63 6f 64 65  AddOp3(v, opcode
27471 2c 20 69 43 75 72 2c 20 70 54 61 62 2d 3e 74 6e  , iCur, pTab->tn
27472 75 6d 2c 20 69 44 62 29 3b 0a 20 20 73 71 6c 69  um, iDb);.  sqli
27473 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
27474 76 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 49 4e  v, -1, SQLITE_IN
27475 54 5f 54 4f 5f 50 54 52 28 70 54 61 62 2d 3e 6e  T_TO_PTR(pTab->n
27476 43 6f 6c 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b  Col), P4_INT32);
27477 0a 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28  .  VdbeComment((
27478 76 2c 20 22 25 73 22 2c 20 70 54 61 62 2d 3e 7a  v, "%s", pTab->z
27479 4e 61 6d 65 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Name));.}../*.**
2747a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
2747b 72 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 20  r to the column 
2747c 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20  affinity string 
2747d 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
2747e 69 6e 64 65 78 0a 2a 2a 20 70 49 64 78 2e 20 41  index.** pIdx. A
2747f 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79   column affinity
27480 20 73 74 72 69 6e 67 20 68 61 73 20 6f 6e 65 20   string has one 
27481 63 68 61 72 61 63 74 65 72 20 66 6f 72 20 65 61  character for ea
27482 63 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 0a 2a 2a  ch column in .**
27483 20 74 68 65 20 74 61 62 6c 65 2c 20 61 63 63 6f   the table, acco
27484 72 64 69 6e 67 20 74 6f 20 74 68 65 20 61 66 66  rding to the aff
27485 69 6e 69 74 79 20 6f 66 20 74 68 65 20 63 6f 6c  inity of the col
27486 75 6d 6e 3a 0a 2a 2a 0a 2a 2a 20 20 43 68 61 72  umn:.**.**  Char
27487 61 63 74 65 72 20 20 20 20 20 20 43 6f 6c 75 6d  acter      Colum
27488 6e 20 61 66 66 69 6e 69 74 79 0a 2a 2a 20 20 2d  n affinity.**  -
27489 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2748a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
2748b 20 20 27 61 27 20 20 20 20 20 20 20 20 20 20 20    'a'           
2748c 20 54 45 58 54 0a 2a 2a 20 20 27 62 27 20 20 20   TEXT.**  'b'   
2748d 20 20 20 20 20 20 20 20 20 4e 4f 4e 45 0a 2a 2a           NONE.**
2748e 20 20 27 63 27 20 20 20 20 20 20 20 20 20 20 20    'c'           
2748f 20 4e 55 4d 45 52 49 43 0a 2a 2a 20 20 27 64 27   NUMERIC.**  'd'
27490 20 20 20 20 20 20 20 20 20 20 20 20 49 4e 54 45              INTE
27491 47 45 52 0a 2a 2a 20 20 27 65 27 20 20 20 20 20  GER.**  'e'     
27492 20 20 20 20 20 20 20 52 45 41 4c 0a 2a 2a 0a 2a         REAL.**.*
27493 2a 20 41 6e 20 65 78 74 72 61 20 27 62 27 20 69  * An extra 'b' i
27494 73 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68  s appended to th
27495 65 20 65 6e 64 20 6f 66 20 74 68 65 20 73 74 72  e end of the str
27496 69 6e 67 20 74 6f 20 63 6f 76 65 72 20 74 68 65  ing to cover the
27497 0a 2a 2a 20 72 6f 77 69 64 20 74 68 61 74 20 61  .** rowid that a
27498 70 70 65 61 72 73 20 61 73 20 74 68 65 20 6c 61  ppears as the la
27499 73 74 20 63 6f 6c 75 6d 6e 20 69 6e 20 65 76 65  st column in eve
2749a 72 79 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20  ry index..**.** 
2749b 4d 65 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 62  Memory for the b
2749c 75 66 66 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67  uffer containing
2749d 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 65   the column inde
2749e 78 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e  x affinity strin
2749f 67 0a 2a 2a 20 69 73 20 6d 61 6e 61 67 65 64 20  g.** is managed 
274a0 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20 72  along with the r
274a1 65 73 74 20 6f 66 20 74 68 65 20 49 6e 64 65 78  est of the Index
274a2 20 73 74 72 75 63 74 75 72 65 2e 20 49 74 20 77   structure. It w
274a3 69 6c 6c 20 62 65 0a 2a 2a 20 72 65 6c 65 61 73  ill be.** releas
274a4 65 64 20 77 68 65 6e 20 73 71 6c 69 74 65 33 44  ed when sqlite3D
274a5 65 6c 65 74 65 49 6e 64 65 78 28 29 20 69 73 20  eleteIndex() is 
274a6 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  called..*/.SQLIT
274a7 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
274a8 63 68 61 72 20 2a 73 71 6c 69 74 65 33 49 6e 64  char *sqlite3Ind
274a9 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 56 64  exAffinityStr(Vd
274aa 62 65 20 2a 76 2c 20 49 6e 64 65 78 20 2a 70 49  be *v, Index *pI
274ab 64 78 29 7b 0a 20 20 69 66 28 20 21 70 49 64 78  dx){.  if( !pIdx
274ac 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20  ->zColAff ){.   
274ad 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74 69   /* The first ti
274ae 6d 65 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69  me a column affi
274af 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20  nity string for 
274b0 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e 64  a particular ind
274b1 65 78 20 69 73 0a 20 20 20 20 2a 2a 20 72 65 71  ex is.    ** req
274b2 75 69 72 65 64 2c 20 69 74 20 69 73 20 61 6c 6c  uired, it is all
274b3 6f 63 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c  ocated and popul
274b4 61 74 65 64 20 68 65 72 65 2e 20 49 74 20 69 73  ated here. It is
274b5 20 74 68 65 6e 20 73 74 6f 72 65 64 20 61 73 0a   then stored as.
274b6 20 20 20 20 2a 2a 20 61 20 6d 65 6d 62 65 72 20      ** a member 
274b7 6f 66 20 74 68 65 20 49 6e 64 65 78 20 73 74 72  of the Index str
274b8 75 63 74 75 72 65 20 66 6f 72 20 73 75 62 73 65  ucture for subse
274b9 71 75 65 6e 74 20 75 73 65 2e 0a 20 20 20 20 2a  quent use..    *
274ba 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 63 6f 6c  *.    ** The col
274bb 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72  umn affinity str
274bc 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e 74 75 61  ing will eventua
274bd 6c 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20 62  lly be deleted b
274be 79 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 44  y.    ** sqliteD
274bf 65 6c 65 74 65 49 6e 64 65 78 28 29 20 77 68 65  eleteIndex() whe
274c0 6e 20 74 68 65 20 49 6e 64 65 78 20 73 74 72 75  n the Index stru
274c1 63 74 75 72 65 20 69 74 73 65 6c 66 20 69 73 20  cture itself is 
274c2 63 6c 65 61 6e 65 64 0a 20 20 20 20 2a 2a 20 75  cleaned.    ** u
274c3 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e  p..    */.    in
274c4 74 20 6e 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a  t n;.    Table *
274c5 70 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 61  pTab = pIdx->pTa
274c6 62 6c 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ble;.    sqlite3
274c7 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 56 64   *db = sqlite3Vd
274c8 62 65 44 62 28 76 29 3b 0a 20 20 20 20 70 49 64  beDb(v);.    pId
274c9 78 2d 3e 7a 43 6f 6c 41 66 66 20 3d 20 28 63 68  x->zColAff = (ch
274ca 61 72 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c 6c  ar *)sqlite3Mall
274cb 6f 63 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  oc(pIdx->nColumn
274cc 2b 32 29 3b 0a 20 20 20 20 69 66 28 20 21 70 49  +2);.    if( !pI
274cd 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20  dx->zColAff ){. 
274ce 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46       db->mallocF
274cf 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  ailed = 1;.     
274d0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d   return 0;.    }
274d1 0a 20 20 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c  .    for(n=0; n<
274d2 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6e  pIdx->nColumn; n
274d3 2b 2b 29 7b 0a 20 20 20 20 20 20 70 49 64 78 2d  ++){.      pIdx-
274d4 3e 7a 43 6f 6c 41 66 66 5b 6e 5d 20 3d 20 70 54  >zColAff[n] = pT
274d5 61 62 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e 61  ab->aCol[pIdx->a
274d6 69 43 6f 6c 75 6d 6e 5b 6e 5d 5d 2e 61 66 66 69  iColumn[n]].affi
274d7 6e 69 74 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20  nity;.    }.    
274d8 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 5b 6e 2b  pIdx->zColAff[n+
274d9 2b 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  +] = SQLITE_AFF_
274da 4e 4f 4e 45 3b 0a 20 20 20 20 70 49 64 78 2d 3e  NONE;.    pIdx->
274db 7a 43 6f 6c 41 66 66 5b 6e 5d 20 3d 20 30 3b 0a  zColAff[n] = 0;.
274dc 20 20 7d 0a 20 0a 20 20 72 65 74 75 72 6e 20 70    }. .  return p
274dd 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 3b 0a 7d 0a  Idx->zColAff;.}.
274de 0a 2f 2a 0a 2a 2a 20 53 65 74 20 50 34 20 6f 66  ./*.** Set P4 of
274df 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
274e0 6c 79 20 69 6e 73 65 72 74 65 64 20 6f 70 63 6f  ly inserted opco
274e1 64 65 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 61  de to a column a
274e2 66 66 69 6e 69 74 79 0a 2a 2a 20 73 74 72 69 6e  ffinity.** strin
274e3 67 20 66 6f 72 20 74 61 62 6c 65 20 70 54 61 62  g for table pTab
274e4 2e 20 41 20 63 6f 6c 75 6d 6e 20 61 66 66 69 6e  . A column affin
274e5 69 74 79 20 73 74 72 69 6e 67 20 68 61 73 20 6f  ity string has o
274e6 6e 65 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20  ne character.** 
274e7 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20  for each column 
274e8 69 6e 64 65 78 65 64 20 62 79 20 74 68 65 20 69  indexed by the i
274e9 6e 64 65 78 2c 20 61 63 63 6f 72 64 69 6e 67 20  ndex, according 
274ea 74 6f 20 74 68 65 20 61 66 66 69 6e 69 74 79 20  to the affinity 
274eb 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e  of the.** column
274ec 3a 0a 2a 2a 0a 2a 2a 20 20 43 68 61 72 61 63 74  :.**.**  Charact
274ed 65 72 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20 61  er      Column a
274ee 66 66 69 6e 69 74 79 0a 2a 2a 20 20 2d 2d 2d 2d  ffinity.**  ----
274ef 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
274f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 27  ----------.**  '
274f1 61 27 20 20 20 20 20 20 20 20 20 20 20 20 54 45  a'            TE
274f2 58 54 0a 2a 2a 20 20 27 62 27 20 20 20 20 20 20  XT.**  'b'      
274f3 20 20 20 20 20 20 4e 4f 4e 45 0a 2a 2a 20 20 27        NONE.**  '
274f4 63 27 20 20 20 20 20 20 20 20 20 20 20 20 4e 55  c'            NU
274f5 4d 45 52 49 43 0a 2a 2a 20 20 27 64 27 20 20 20  MERIC.**  'd'   
274f6 20 20 20 20 20 20 20 20 20 49 4e 54 45 47 45 52           INTEGER
274f7 0a 2a 2a 20 20 27 65 27 20 20 20 20 20 20 20 20  .**  'e'        
274f8 20 20 20 20 52 45 41 4c 0a 2a 2f 0a 53 51 4c 49      REAL.*/.SQLI
274f9 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
274fa 73 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66 69  sqlite3TableAffi
274fb 6e 69 74 79 53 74 72 28 56 64 62 65 20 2a 76 2c  nityStr(Vdbe *v,
274fc 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20   Table *pTab){. 
274fd 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74 69   /* The first ti
274fe 6d 65 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66 69  me a column affi
274ff 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20  nity string for 
27500 61 20 70 61 72 74 69 63 75 6c 61 72 20 74 61 62  a particular tab
27501 6c 65 0a 20 20 2a 2a 20 69 73 20 72 65 71 75 69  le.  ** is requi
27502 72 65 64 2c 20 69 74 20 69 73 20 61 6c 6c 6f 63  red, it is alloc
27503 61 74 65 64 20 61 6e 64 20 70 6f 70 75 6c 61 74  ated and populat
27504 65 64 20 68 65 72 65 2e 20 49 74 20 69 73 20 74  ed here. It is t
27505 68 65 6e 20 0a 20 20 2a 2a 20 73 74 6f 72 65 64  hen .  ** stored
27506 20 61 73 20 61 20 6d 65 6d 62 65 72 20 6f 66 20   as a member of 
27507 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  the Table struct
27508 75 72 65 20 66 6f 72 20 73 75 62 73 65 71 75 65  ure for subseque
27509 6e 74 20 75 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a  nt use..  **.  *
2750a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 61 66 66  * The column aff
2750b 69 6e 69 74 79 20 73 74 72 69 6e 67 20 77 69 6c  inity string wil
2750c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 20 62 65 20  l eventually be 
2750d 64 65 6c 65 74 65 64 20 62 79 0a 20 20 2a 2a 20  deleted by.  ** 
2750e 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62  sqlite3DeleteTab
2750f 6c 65 28 29 20 77 68 65 6e 20 74 68 65 20 54 61  le() when the Ta
27510 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 74  ble structure it
27511 73 65 6c 66 20 69 73 20 63 6c 65 61 6e 65 64 20  self is cleaned 
27512 75 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  up..  */.  if( !
27513 70 54 61 62 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b  pTab->zColAff ){
27514 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 41  .    char *zColA
27515 66 66 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20  ff;.    int i;. 
27516 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d     sqlite3 *db =
27517 20 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76   sqlite3VdbeDb(v
27518 29 3b 0a 0a 20 20 20 20 7a 43 6f 6c 41 66 66 20  );..    zColAff 
27519 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65  = (char *)sqlite
2751a 33 4d 61 6c 6c 6f 63 28 70 54 61 62 2d 3e 6e 43  3Malloc(pTab->nC
2751b 6f 6c 2b 31 29 3b 0a 20 20 20 20 69 66 28 20 21  ol+1);.    if( !
2751c 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20 20 20 20  zColAff ){.     
2751d 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2751e 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74  d = 1;.      ret
2751f 75 72 6e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  urn;.    }..    
27520 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d  for(i=0; i<pTab-
27521 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
27522 20 20 20 7a 43 6f 6c 41 66 66 5b 69 5d 20 3d 20     zColAff[i] = 
27523 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 61 66  pTab->aCol[i].af
27524 66 69 6e 69 74 79 3b 0a 20 20 20 20 7d 0a 20 20  finity;.    }.  
27525 20 20 7a 43 6f 6c 41 66 66 5b 70 54 61 62 2d 3e    zColAff[pTab->
27526 6e 43 6f 6c 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20  nCol] = '\0';.. 
27527 20 20 20 70 54 61 62 2d 3e 7a 43 6f 6c 41 66 66     pTab->zColAff
27528 20 3d 20 7a 43 6f 6c 41 66 66 3b 0a 20 20 7d 0a   = zColAff;.  }.
27529 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
2752a 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 54  angeP4(v, -1, pT
2752b 61 62 2d 3e 7a 43 6f 6c 41 66 66 2c 20 30 29 3b  ab->zColAff, 0);
2752c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
2752d 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68 65   non-zero if the
2752e 20 74 61 62 6c 65 20 70 54 61 62 20 69 6e 20 64   table pTab in d
2752f 61 74 61 62 61 73 65 20 69 44 62 20 6f 72 20 61  atabase iDb or a
27530 6e 79 20 6f 66 20 69 74 73 20 69 6e 64 69 63 65  ny of its indice
27531 73 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 6f  s.** have been o
27532 70 65 6e 65 64 20 61 74 20 61 6e 79 20 70 6f 69  pened at any poi
27533 6e 74 20 69 6e 20 74 68 65 20 56 44 42 45 20 70  nt in the VDBE p
27534 72 6f 67 72 61 6d 20 62 65 67 69 6e 6e 69 6e 67  rogram beginning
27535 20 61 74 20 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20   at location.** 
27536 69 53 74 61 72 74 41 64 64 72 20 74 68 72 6f 75  iStartAddr throu
27537 67 68 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74  ght the end of t
27538 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 54 68 69  he program.  Thi
27539 73 20 69 73 20 75 73 65 64 20 74 6f 20 73 65 65  s is used to see
2753a 20 69 66 20 0a 2a 2a 20 61 20 73 74 61 74 65 6d   if .** a statem
2753b 65 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ent of the form 
2753c 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c 69   "INSERT INTO <i
2753d 44 62 2c 20 70 54 61 62 3e 20 53 45 4c 45 43 54  Db, pTab> SELECT
2753e 20 2e 2e 2e 22 20 63 61 6e 20 0a 2a 2a 20 72 75   ..." can .** ru
2753f 6e 20 77 69 74 68 6f 75 74 20 75 73 69 6e 67 20  n without using 
27540 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
27541 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74 73 20  for the results 
27542 6f 66 20 74 68 65 20 53 45 4c 45 43 54 2e 20 0a  of the SELECT. .
27543 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  */.static int re
27544 61 64 73 54 61 62 6c 65 28 50 61 72 73 65 20 2a  adsTable(Parse *
27545 70 2c 20 69 6e 74 20 69 53 74 61 72 74 41 64 64  p, int iStartAdd
27546 72 2c 20 69 6e 74 20 69 44 62 2c 20 54 61 62 6c  r, int iDb, Tabl
27547 65 20 2a 70 54 61 62 29 7b 0a 20 20 56 64 62 65  e *pTab){.  Vdbe
27548 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *v = sqlite3Get
27549 56 64 62 65 28 70 29 3b 0a 20 20 69 6e 74 20 69  Vdbe(p);.  int i
2754a 3b 0a 20 20 69 6e 74 20 69 45 6e 64 20 3d 20 73  ;.  int iEnd = s
2754b 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
2754c 74 41 64 64 72 28 76 29 3b 0a 23 69 66 6e 64 65  tAddr(v);.#ifnde
2754d 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
2754e 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 56 54 61  RTUALTABLE.  VTa
2754f 62 6c 65 20 2a 70 56 54 61 62 20 3d 20 49 73 56  ble *pVTab = IsV
27550 69 72 74 75 61 6c 28 70 54 61 62 29 20 3f 20 73  irtual(pTab) ? s
27551 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28  qlite3GetVTable(
27552 70 2d 3e 64 62 2c 20 70 54 61 62 29 20 3a 20 30  p->db, pTab) : 0
27553 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 66 6f 72 28  ;.#endif..  for(
27554 69 3d 69 53 74 61 72 74 41 64 64 72 3b 20 69 3c  i=iStartAddr; i<
27555 69 45 6e 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  iEnd; i++){.    
27556 56 64 62 65 4f 70 20 2a 70 4f 70 20 3d 20 73 71  VdbeOp *pOp = sq
27557 6c 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76  lite3VdbeGetOp(v
27558 2c 20 69 29 3b 0a 20 20 20 20 61 73 73 65 72 74  , i);.    assert
27559 28 20 70 4f 70 21 3d 30 20 29 3b 0a 20 20 20 20  ( pOp!=0 );.    
2755a 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
2755b 3d 4f 50 5f 4f 70 65 6e 52 65 61 64 20 26 26 20  =OP_OpenRead && 
2755c 70 4f 70 2d 3e 70 33 3d 3d 69 44 62 20 29 7b 0a  pOp->p3==iDb ){.
2755d 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e        Index *pIn
2755e 64 65 78 3b 0a 20 20 20 20 20 20 69 6e 74 20 74  dex;.      int t
2755f 6e 75 6d 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20  num = pOp->p2;. 
27560 20 20 20 20 20 69 66 28 20 74 6e 75 6d 3d 3d 70       if( tnum==p
27561 54 61 62 2d 3e 74 6e 75 6d 20 29 7b 0a 20 20 20  Tab->tnum ){.   
27562 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
27563 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72       }.      for
27564 28 70 49 6e 64 65 78 3d 70 54 61 62 2d 3e 70 49  (pIndex=pTab->pI
27565 6e 64 65 78 3b 20 70 49 6e 64 65 78 3b 20 70 49  ndex; pIndex; pI
27566 6e 64 65 78 3d 70 49 6e 64 65 78 2d 3e 70 4e 65  ndex=pIndex->pNe
27567 78 74 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  xt){.        if(
27568 20 74 6e 75 6d 3d 3d 70 49 6e 64 65 78 2d 3e 74   tnum==pIndex->t
27569 6e 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20  num ){.         
2756a 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
2756b 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
2756c 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
2756d 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
2756e 42 4c 45 0a 20 20 20 20 69 66 28 20 70 4f 70 2d  BLE.    if( pOp-
2756f 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 4f 70 65  >opcode==OP_VOpe
27570 6e 20 26 26 20 70 4f 70 2d 3e 70 34 2e 70 56 74  n && pOp->p4.pVt
27571 61 62 3d 3d 70 56 54 61 62 20 29 7b 0a 20 20 20  ab==pVTab ){.   
27572 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
27573 70 34 2e 70 56 74 61 62 21 3d 30 20 29 3b 0a 20  p4.pVtab!=0 );. 
27574 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
27575 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f 56 54 41  ->p4type==P4_VTA
27576 42 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  B );.      retur
27577 6e 20 31 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  n 1;.    }.#endi
27578 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  f.  }.  return 0
27579 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
2757a 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
2757b 52 45 4d 45 4e 54 0a 2f 2a 0a 2a 2a 20 4c 6f 63  REMENT./*.** Loc
2757c 61 74 65 20 6f 72 20 63 72 65 61 74 65 20 61 6e  ate or create an
2757d 20 41 75 74 6f 69 6e 63 49 6e 66 6f 20 73 74 72   AutoincInfo str
2757e 75 63 74 75 72 65 20 61 73 73 6f 63 69 61 74 65  ucture associate
2757f 64 20 77 69 74 68 20 74 61 62 6c 65 20 70 54 61  d with table pTa
27580 62 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 69 6e  b.** which is in
27581 20 64 61 74 61 62 61 73 65 20 69 44 62 2e 20 20   database iDb.  
27582 52 65 74 75 72 6e 20 74 68 65 20 72 65 67 69 73  Return the regis
27583 74 65 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74  ter number for t
27584 68 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 74  he register.** t
27585 68 61 74 20 68 6f 6c 64 73 20 74 68 65 20 6d 61  hat holds the ma
27586 78 69 6d 75 6d 20 72 6f 77 69 64 2e 0a 2a 2a 0a  ximum rowid..**.
27587 2a 2a 20 54 68 65 72 65 20 69 73 20 61 74 20 6d  ** There is at m
27588 6f 73 74 20 6f 6e 65 20 41 75 74 6f 69 6e 63 49  ost one AutoincI
27589 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 70 65  nfo structure pe
2758a 72 20 74 61 62 6c 65 20 65 76 65 6e 20 69 66 20  r table even if 
2758b 74 68 65 0a 2a 2a 20 73 61 6d 65 20 74 61 62 6c  the.** same tabl
2758c 65 20 69 73 20 61 75 74 6f 69 6e 63 72 65 6d 65  e is autoincreme
2758d 6e 74 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69  nted multiple ti
2758e 6d 65 73 20 64 75 65 20 74 6f 20 69 6e 73 65 72  mes due to inser
2758f 74 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 72 69  ts within.** tri
27590 67 67 65 72 73 2e 20 20 41 20 6e 65 77 20 41 75  ggers.  A new Au
27591 74 6f 69 6e 63 49 6e 66 6f 20 73 74 72 75 63 74  toincInfo struct
27592 75 72 65 20 69 73 20 63 72 65 61 74 65 64 20 69  ure is created i
27593 66 20 74 68 69 73 20 69 73 20 74 68 65 0a 2a 2a  f this is the.**
27594 20 66 69 72 73 74 20 75 73 65 20 6f 66 20 74 61   first use of ta
27595 62 6c 65 20 70 54 61 62 2e 20 20 4f 6e 20 32 6e  ble pTab.  On 2n
27596 64 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74  d and subsequent
27597 20 75 73 65 73 2c 20 74 68 65 20 6f 72 69 67 69   uses, the origi
27598 6e 61 6c 0a 2a 2a 20 41 75 74 6f 69 6e 63 49 6e  nal.** AutoincIn
27599 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20  fo structure is 
2759a 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 72 65  used..**.** Thre
2759b 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  e memory locatio
2759c 6e 73 20 61 72 65 20 61 6c 6c 6f 63 61 74 65 64  ns are allocated
2759d 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 20 52  :.**.**   (1)  R
2759e 65 67 69 73 74 65 72 20 74 6f 20 68 6f 6c 64 20  egister to hold 
2759f 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
275a0 70 54 61 62 20 74 61 62 6c 65 2e 0a 2a 2a 20 20  pTab table..**  
275a1 20 28 32 29 20 20 52 65 67 69 73 74 65 72 20 74   (2)  Register t
275a2 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61 78 69 6d  o hold the maxim
275a3 75 6d 20 52 4f 57 49 44 20 6f 66 20 70 54 61 62  um ROWID of pTab
275a4 2e 0a 2a 2a 20 20 20 28 33 29 20 20 52 65 67 69  ..**   (3)  Regi
275a5 73 74 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65  ster to hold the
275a6 20 72 6f 77 69 64 20 69 6e 20 73 71 6c 69 74 65   rowid in sqlite
275a7 5f 73 65 71 75 65 6e 63 65 20 6f 66 20 70 54 61  _sequence of pTa
275a8 62 0a 2a 2a 0a 2a 2a 20 54 68 65 20 32 6e 64 20  b.**.** The 2nd 
275a9 72 65 67 69 73 74 65 72 20 69 73 20 74 68 65 20  register is the 
275aa 6f 6e 65 20 74 68 61 74 20 69 73 20 72 65 74 75  one that is retu
275ab 72 6e 65 64 2e 20 20 54 68 61 74 20 69 73 20 61  rned.  That is a
275ac 6c 6c 20 74 68 65 0a 2a 2a 20 69 6e 73 65 72 74  ll the.** insert
275ad 20 72 6f 75 74 69 6e 65 20 6e 65 65 64 73 20 74   routine needs t
275ae 6f 20 6b 6e 6f 77 20 61 62 6f 75 74 2e 0a 2a 2f  o know about..*/
275af 0a 73 74 61 74 69 63 20 69 6e 74 20 61 75 74 6f  .static int auto
275b0 49 6e 63 42 65 67 69 6e 28 0a 20 20 50 61 72 73  IncBegin(.  Pars
275b1 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
275b2 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
275b3 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c  xt */.  int iDb,
275b4 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
275b5 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61 74 61  ndex of the data
275b6 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 70 54 61  base holding pTa
275b7 62 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  b */.  Table *pT
275b8 61 62 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ab         /* Th
275b9 65 20 74 61 62 6c 65 20 77 65 20 61 72 65 20 77  e table we are w
275ba 72 69 74 69 6e 67 20 74 6f 20 2a 2f 0a 29 7b 0a  riting to */.){.
275bb 20 20 69 6e 74 20 6d 65 6d 49 64 20 3d 20 30 3b    int memId = 0;
275bc 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
275bd 72 20 68 6f 6c 64 69 6e 67 20 6d 61 78 69 6d 75  r holding maximu
275be 6d 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 66 28  m rowid */.  if(
275bf 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20   pTab->tabFlags 
275c0 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65  & TF_Autoincreme
275c1 6e 74 20 29 7b 0a 20 20 20 20 50 61 72 73 65 20  nt ){.    Parse 
275c2 2a 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c  *pToplevel = sql
275c3 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65  ite3ParseTopleve
275c4 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 41  l(pParse);.    A
275c5 75 74 6f 69 6e 63 49 6e 66 6f 20 2a 70 49 6e 66  utoincInfo *pInf
275c6 6f 3b 0a 0a 20 20 20 20 70 49 6e 66 6f 20 3d 20  o;..    pInfo = 
275c7 70 54 6f 70 6c 65 76 65 6c 2d 3e 70 41 69 6e 63  pToplevel->pAinc
275c8 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 49 6e  ;.    while( pIn
275c9 66 6f 20 26 26 20 70 49 6e 66 6f 2d 3e 70 54 61  fo && pInfo->pTa
275ca 62 21 3d 70 54 61 62 20 29 7b 20 70 49 6e 66 6f  b!=pTab ){ pInfo
275cb 20 3d 20 70 49 6e 66 6f 2d 3e 70 4e 65 78 74 3b   = pInfo->pNext;
275cc 20 7d 0a 20 20 20 20 69 66 28 20 70 49 6e 66 6f   }.    if( pInfo
275cd 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 49 6e  ==0 ){.      pIn
275ce 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  fo = sqlite3DbMa
275cf 6c 6c 6f 63 52 61 77 28 70 50 61 72 73 65 2d 3e  llocRaw(pParse->
275d0 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 49 6e 66  db, sizeof(*pInf
275d1 6f 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  o));.      if( p
275d2 49 6e 66 6f 3d 3d 30 20 29 20 72 65 74 75 72 6e  Info==0 ) return
275d3 20 30 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d   0;.      pInfo-
275d4 3e 70 4e 65 78 74 20 3d 20 70 54 6f 70 6c 65 76  >pNext = pToplev
275d5 65 6c 2d 3e 70 41 69 6e 63 3b 0a 20 20 20 20 20  el->pAinc;.     
275d6 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 70 41 69 6e   pToplevel->pAin
275d7 63 20 3d 20 70 49 6e 66 6f 3b 0a 20 20 20 20 20  c = pInfo;.     
275d8 20 70 49 6e 66 6f 2d 3e 70 54 61 62 20 3d 20 70   pInfo->pTab = p
275d9 54 61 62 3b 0a 20 20 20 20 20 20 70 49 6e 66 6f  Tab;.      pInfo
275da 2d 3e 69 44 62 20 3d 20 69 44 62 3b 0a 20 20 20  ->iDb = iDb;.   
275db 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e 4d     pToplevel->nM
275dc 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20 20 20  em++;           
275dd 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
275de 65 72 20 74 6f 20 68 6f 6c 64 20 6e 61 6d 65 20  er to hold name 
275df 6f 66 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  of table */.    
275e0 20 20 70 49 6e 66 6f 2d 3e 72 65 67 43 74 72 20    pInfo->regCtr 
275e1 3d 20 2b 2b 70 54 6f 70 6c 65 76 65 6c 2d 3e 6e  = ++pToplevel->n
275e2 4d 65 6d 3b 20 20 2f 2a 20 4d 61 78 20 72 6f 77  Mem;  /* Max row
275e3 69 64 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20  id register */. 
275e4 20 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e       pToplevel->
275e5 6e 4d 65 6d 2b 2b 3b 20 20 20 20 20 20 20 20 20  nMem++;         
275e6 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77 69           /* Rowi
275e7 64 20 69 6e 20 73 71 6c 69 74 65 5f 73 65 71 75  d in sqlite_sequ
275e8 65 6e 63 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 20  ence */.    }.  
275e9 20 20 6d 65 6d 49 64 20 3d 20 70 49 6e 66 6f 2d    memId = pInfo-
275ea 3e 72 65 67 43 74 72 3b 0a 20 20 7d 0a 20 20 72  >regCtr;.  }.  r
275eb 65 74 75 72 6e 20 6d 65 6d 49 64 3b 0a 7d 0a 0a  eturn memId;.}..
275ec 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
275ed 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f 64  ne generates cod
275ee 65 20 74 68 61 74 20 77 69 6c 6c 20 69 6e 69 74  e that will init
275ef 69 61 6c 69 7a 65 20 61 6c 6c 20 6f 66 20 74 68  ialize all of th
275f0 65 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 75 73  e.** register us
275f1 65 64 20 62 79 20 74 68 65 20 61 75 74 6f 69 6e  ed by the autoin
275f2 63 72 65 6d 65 6e 74 20 74 72 61 63 6b 65 72 2e  crement tracker.
275f3 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49    .*/.SQLITE_PRI
275f4 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
275f5 33 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 42 65  3AutoincrementBe
275f6 67 69 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73  gin(Parse *pPars
275f7 65 29 7b 0a 20 20 41 75 74 6f 69 6e 63 49 6e 66  e){.  AutoincInf
275f8 6f 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20  o *p;           
275f9 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
275fa 61 62 6f 75 74 20 61 6e 20 41 55 54 4f 49 4e 43  about an AUTOINC
275fb 52 45 4d 45 4e 54 20 2a 2f 0a 20 20 73 71 6c 69  REMENT */.  sqli
275fc 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
275fd 2d 3e 64 62 3b 20 20 2f 2a 20 54 68 65 20 64 61  ->db;  /* The da
275fe 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
275ff 6e 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b 20  n */.  Db *pDb; 
27600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27601 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6f 6e    /* Database on
27602 6c 79 20 61 75 74 6f 69 6e 63 20 74 61 62 6c 65  ly autoinc table
27603 20 2a 2f 0a 20 20 69 6e 74 20 6d 65 6d 49 64 3b   */.  int memId;
27604 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27605 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
27606 64 69 6e 67 20 6d 61 78 20 72 6f 77 69 64 20 2a  ding max rowid *
27607 2f 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20 20  /.  int addr;   
27608 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
27609 2a 20 41 20 56 44 42 45 20 61 64 64 72 65 73 73  * A VDBE address
2760a 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d 20   */.  Vdbe *v = 
2760b 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20  pParse->pVdbe;  
2760c 20 2f 2a 20 56 44 42 45 20 75 6e 64 65 72 20 63   /* VDBE under c
2760d 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 0a  onstruction */..
2760e 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e    /* This routin
2760f 65 20 69 73 20 6e 65 76 65 72 20 63 61 6c 6c 65  e is never calle
27610 64 20 64 75 72 69 6e 67 20 74 72 69 67 67 65 72  d during trigger
27611 2d 67 65 6e 65 72 61 74 69 6f 6e 2e 20 20 49 74  -generation.  It
27612 20 69 73 0a 20 20 2a 2a 20 6f 6e 6c 79 20 63 61   is.  ** only ca
27613 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 74 6f  lled from the to
27614 70 2d 6c 65 76 65 6c 20 2a 2f 0a 20 20 61 73 73  p-level */.  ass
27615 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 54 72  ert( pParse->pTr
27616 69 67 67 65 72 54 61 62 3d 3d 30 20 29 3b 0a 20  iggerTab==0 );. 
27617 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 3d   assert( pParse=
27618 3d 73 71 6c 69 74 65 33 50 61 72 73 65 54 6f 70  =sqlite3ParseTop
27619 6c 65 76 65 6c 28 70 50 61 72 73 65 29 20 29 3b  level(pParse) );
2761a 0a 0a 20 20 61 73 73 65 72 74 28 20 76 20 29 3b  ..  assert( v );
2761b 20 20 20 2f 2a 20 57 65 20 66 61 69 6c 65 64 20     /* We failed 
2761c 6c 6f 6e 67 20 61 67 6f 20 69 66 20 74 68 69 73  long ago if this
2761d 20 69 73 20 6e 6f 74 20 73 6f 20 2a 2f 0a 20 20   is not so */.  
2761e 66 6f 72 28 70 20 3d 20 70 50 61 72 73 65 2d 3e  for(p = pParse->
2761f 70 41 69 6e 63 3b 20 70 3b 20 70 20 3d 20 70 2d  pAinc; p; p = p-
27620 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 44 62  >pNext){.    pDb
27621 20 3d 20 26 64 62 2d 3e 61 44 62 5b 70 2d 3e 69   = &db->aDb[p->i
27622 44 62 5d 3b 0a 20 20 20 20 6d 65 6d 49 64 20 3d  Db];.    memId =
27623 20 70 2d 3e 72 65 67 43 74 72 3b 0a 20 20 20 20   p->regCtr;.    
27624 73 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65  sqlite3OpenTable
27625 28 70 50 61 72 73 65 2c 20 30 2c 20 70 2d 3e 69  (pParse, 0, p->i
27626 44 62 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d 61  Db, pDb->pSchema
27627 2d 3e 70 53 65 71 54 61 62 2c 20 4f 50 5f 4f 70  ->pSeqTab, OP_Op
27628 65 6e 52 65 61 64 29 3b 0a 20 20 20 20 61 64 64  enRead);.    add
27629 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  r = sqlite3VdbeC
2762a 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20  urrentAddr(v);. 
2762b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2762c 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
2762d 67 38 2c 20 30 2c 20 6d 65 6d 49 64 2d 31 2c 20  g8, 0, memId-1, 
2762e 30 2c 20 70 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d  0, p->pTab->zNam
2762f 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  e, 0);.    sqlit
27630 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
27631 4f 50 5f 52 65 77 69 6e 64 2c 20 30 2c 20 61 64  OP_Rewind, 0, ad
27632 64 72 2b 39 29 3b 0a 20 20 20 20 73 71 6c 69 74  dr+9);.    sqlit
27633 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
27634 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c  OP_Column, 0, 0,
27635 20 6d 65 6d 49 64 29 3b 0a 20 20 20 20 73 71 6c   memId);.    sql
27636 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
27637 2c 20 4f 50 5f 4e 65 2c 20 6d 65 6d 49 64 2d 31  , OP_Ne, memId-1
27638 2c 20 61 64 64 72 2b 37 2c 20 6d 65 6d 49 64 29  , addr+7, memId)
27639 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2763a 65 43 68 61 6e 67 65 50 35 28 76 2c 20 53 51 4c  eChangeP5(v, SQL
2763b 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b  ITE_JUMPIFNULL);
2763c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2763d 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77  AddOp2(v, OP_Row
2763e 69 64 2c 20 30 2c 20 6d 65 6d 49 64 2b 31 29 3b  id, 0, memId+1);
2763f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27640 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
27641 75 6d 6e 2c 20 30 2c 20 31 2c 20 6d 65 6d 49 64  umn, 0, 1, memId
27642 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
27643 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
27644 6f 74 6f 2c 20 30 2c 20 61 64 64 72 2b 39 29 3b  oto, 0, addr+9);
27645 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27646 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78  AddOp2(v, OP_Nex
27647 74 2c 20 30 2c 20 61 64 64 72 2b 32 29 3b 0a 20  t, 0, addr+2);. 
27648 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27649 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
2764a 65 72 2c 20 30 2c 20 6d 65 6d 49 64 29 3b 0a 20  er, 0, memId);. 
2764b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2764c 64 4f 70 30 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp0(v, OP_Close
2764d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
2764e 55 70 64 61 74 65 20 74 68 65 20 6d 61 78 69 6d  Update the maxim
2764f 75 6d 20 72 6f 77 69 64 20 66 6f 72 20 61 6e 20  um rowid for an 
27650 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 63 61  autoincrement ca
27651 6c 63 75 6c 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  lculation..**.**
27652 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68   This routine sh
27653 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77  ould be called w
27654 68 65 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74  hen the top of t
27655 68 65 20 73 74 61 63 6b 20 68 6f 6c 64 73 20 61  he stack holds a
27656 0a 2a 2a 20 6e 65 77 20 72 6f 77 69 64 20 74 68  .** new rowid th
27657 61 74 20 69 73 20 61 62 6f 75 74 20 74 6f 20 62  at is about to b
27658 65 20 69 6e 73 65 72 74 65 64 2e 20 20 49 66 20  e inserted.  If 
27659 74 68 61 74 20 6e 65 77 20 72 6f 77 69 64 20 69  that new rowid i
2765a 73 0a 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e  s.** larger than
2765b 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77   the maximum row
2765c 69 64 20 69 6e 20 74 68 65 20 6d 65 6d 49 64 20  id in the memId 
2765d 6d 65 6d 6f 72 79 20 63 65 6c 6c 2c 20 74 68 65  memory cell, the
2765e 6e 20 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20  n the.** memory 
2765f 63 65 6c 6c 20 69 73 20 75 70 64 61 74 65 64 2e  cell is updated.
27660 20 20 54 68 65 20 73 74 61 63 6b 20 69 73 20 75    The stack is u
27661 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74 61  nchanged..*/.sta
27662 74 69 63 20 76 6f 69 64 20 61 75 74 6f 49 6e 63  tic void autoInc
27663 53 74 65 70 28 50 61 72 73 65 20 2a 70 50 61 72  Step(Parse *pPar
27664 73 65 2c 20 69 6e 74 20 6d 65 6d 49 64 2c 20 69  se, int memId, i
27665 6e 74 20 72 65 67 52 6f 77 69 64 29 7b 0a 20 20  nt regRowid){.  
27666 69 66 28 20 6d 65 6d 49 64 3e 30 20 29 7b 0a 20  if( memId>0 ){. 
27667 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27668 64 4f 70 32 28 70 50 61 72 73 65 2d 3e 70 56 64  dOp2(pParse->pVd
27669 62 65 2c 20 4f 50 5f 4d 65 6d 4d 61 78 2c 20 6d  be, OP_MemMax, m
2766a 65 6d 49 64 2c 20 72 65 67 52 6f 77 69 64 29 3b  emId, regRowid);
2766b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
2766c 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72  is routine gener
2766d 61 74 65 73 20 74 68 65 20 63 6f 64 65 20 6e 65  ates the code ne
2766e 65 64 65 64 20 74 6f 20 77 72 69 74 65 20 61 75  eded to write au
2766f 74 6f 69 6e 63 72 65 6d 65 6e 74 0a 2a 2a 20 6d  toincrement.** m
27670 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 76 61 6c  aximum rowid val
27671 75 65 73 20 62 61 63 6b 20 69 6e 74 6f 20 74 68  ues back into th
27672 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  e sqlite_sequenc
27673 65 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a 20 45  e register..** E
27674 76 65 72 79 20 73 74 61 74 65 6d 65 6e 74 20 74  very statement t
27675 68 61 74 20 6d 69 67 68 74 20 64 6f 20 61 6e 20  hat might do an 
27676 49 4e 53 45 52 54 20 69 6e 74 6f 20 61 6e 20 61  INSERT into an a
27677 75 74 6f 69 6e 63 72 65 6d 65 6e 74 0a 2a 2a 20  utoincrement.** 
27678 74 61 62 6c 65 20 28 65 69 74 68 65 72 20 64 69  table (either di
27679 72 65 63 74 6c 79 20 6f 72 20 74 68 72 6f 75 67  rectly or throug
2767a 68 20 74 72 69 67 67 65 72 73 29 20 6e 65 65 64  h triggers) need
2767b 73 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 0a 2a  s to call this.*
2767c 2a 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 20 62  * routine just b
2767d 65 66 6f 72 65 20 74 68 65 20 22 65 78 69 74 22  efore the "exit"
2767e 20 63 6f 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   code..*/.SQLITE
2767f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
27680 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d 65  lite3Autoincreme
27681 6e 74 45 6e 64 28 50 61 72 73 65 20 2a 70 50 61  ntEnd(Parse *pPa
27682 72 73 65 29 7b 0a 20 20 41 75 74 6f 69 6e 63 49  rse){.  AutoincI
27683 6e 66 6f 20 2a 70 3b 0a 20 20 56 64 62 65 20 2a  nfo *p;.  Vdbe *
27684 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
27685 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e;.  sqlite3 *db
27686 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a   = pParse->db;..
27687 20 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20    assert( v );. 
27688 20 66 6f 72 28 70 20 3d 20 70 50 61 72 73 65 2d   for(p = pParse-
27689 3e 70 41 69 6e 63 3b 20 70 3b 20 70 20 3d 20 70  >pAinc; p; p = p
2768a 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 44 62  ->pNext){.    Db
2768b 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62   *pDb = &db->aDb
2768c 5b 70 2d 3e 69 44 62 5d 3b 0a 20 20 20 20 69 6e  [p->iDb];.    in
2768d 74 20 6a 31 2c 20 6a 32 2c 20 6a 33 2c 20 6a 34  t j1, j2, j3, j4
2768e 2c 20 6a 35 3b 0a 20 20 20 20 69 6e 74 20 69 52  , j5;.    int iR
2768f 65 63 3b 0a 20 20 20 20 69 6e 74 20 6d 65 6d 49  ec;.    int memI
27690 64 20 3d 20 70 2d 3e 72 65 67 43 74 72 3b 0a 0a  d = p->regCtr;..
27691 20 20 20 20 69 52 65 63 20 3d 20 73 71 6c 69 74      iRec = sqlit
27692 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
27693 72 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  rse);.    sqlite
27694 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73  3OpenTable(pPars
27695 65 2c 20 30 2c 20 70 2d 3e 69 44 62 2c 20 70 44  e, 0, p->iDb, pD
27696 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71  b->pSchema->pSeq
27697 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74  Tab, OP_OpenWrit
27698 65 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c  e);.    j1 = sql
27699 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2769a 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 6d 65  , OP_NotNull, me
2769b 6d 49 64 2b 31 29 3b 0a 20 20 20 20 6a 32 20 3d  mId+1);.    j2 =
2769c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2769d 70 30 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 29  p0(v, OP_Rewind)
2769e 3b 0a 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 74  ;.    j3 = sqlit
2769f 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
276a0 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 30 2c 20 30 2c  OP_Column, 0, 0,
276a1 20 69 52 65 63 29 3b 0a 20 20 20 20 6a 34 20 3d   iRec);.    j4 =
276a2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
276a3 70 33 28 76 2c 20 4f 50 5f 45 71 2c 20 6d 65 6d  p3(v, OP_Eq, mem
276a4 49 64 2d 31 2c 20 30 2c 20 69 52 65 63 29 3b 0a  Id-1, 0, iRec);.
276a5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
276a6 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74  ddOp2(v, OP_Next
276a7 2c 20 30 2c 20 6a 33 29 3b 0a 20 20 20 20 73 71  , 0, j3);.    sq
276a8 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
276a9 65 28 76 2c 20 6a 32 29 3b 0a 20 20 20 20 73 71  e(v, j2);.    sq
276aa 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
276ab 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20  v, OP_NewRowid, 
276ac 30 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a 20 20 20  0, memId+1);.   
276ad 20 6a 35 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j5 = sqlite3Vdb
276ae 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f  eAddOp0(v, OP_Go
276af 74 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  to);.    sqlite3
276b0 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
276b1 6a 34 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  j4);.    sqlite3
276b2 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
276b3 5f 52 6f 77 69 64 2c 20 30 2c 20 6d 65 6d 49 64  _Rowid, 0, memId
276b4 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  +1);.    sqlite3
276b5 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
276b6 6a 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  j1);.    sqlite3
276b7 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
276b8 6a 35 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  j5);.    sqlite3
276b9 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
276ba 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 6d 65 6d  _MakeRecord, mem
276bb 49 64 2d 31 2c 20 32 2c 20 69 52 65 63 29 3b 0a  Id-1, 2, iRec);.
276bc 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
276bd 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65  ddOp3(v, OP_Inse
276be 72 74 2c 20 30 2c 20 69 52 65 63 2c 20 6d 65 6d  rt, 0, iRec, mem
276bf 49 64 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  Id+1);.    sqlit
276c0 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
276c1 2c 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29  , OPFLAG_APPEND)
276c2 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
276c3 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 43 6c  eAddOp0(v, OP_Cl
276c4 6f 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ose);.    sqlite
276c5 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
276c6 70 50 61 72 73 65 2c 20 69 52 65 63 29 3b 0a 20  pParse, iRec);. 
276c7 20 7d 0a 7d 0a 23 65 6c 73 65 0a 2f 2a 0a 2a 2a   }.}.#else./*.**
276c8 20 49 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   If SQLITE_OMIT_
276c9 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 69 73  AUTOINCREMENT is
276ca 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74   defined, then t
276cb 68 65 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65  he three routine
276cc 73 0a 2a 2a 20 61 62 6f 76 65 20 61 72 65 20 61  s.** above are a
276cd 6c 6c 20 6e 6f 2d 6f 70 73 0a 2a 2f 0a 23 20 64  ll no-ops.*/.# d
276ce 65 66 69 6e 65 20 61 75 74 6f 49 6e 63 42 65 67  efine autoIncBeg
276cf 69 6e 28 41 2c 42 2c 43 29 20 28 30 29 0a 23 20  in(A,B,C) (0).# 
276d0 64 65 66 69 6e 65 20 61 75 74 6f 49 6e 63 53 74  define autoIncSt
276d1 65 70 28 41 2c 42 2c 43 29 0a 23 65 6e 64 69 66  ep(A,B,C).#endif
276d2 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
276d3 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f  AUTOINCREMENT */
276d4 0a 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65  .../* Forward de
276d5 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74 61  claration */.sta
276d6 74 69 63 20 69 6e 74 20 78 66 65 72 4f 70 74 69  tic int xferOpti
276d7 6d 69 7a 61 74 69 6f 6e 28 0a 20 20 50 61 72 73  mization(.  Pars
276d8 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
276d9 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
276da 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  ext */.  Table *
276db 70 44 65 73 74 2c 20 20 20 20 20 20 20 20 20 2f  pDest,         /
276dc 2a 20 54 68 65 20 74 61 62 6c 65 20 77 65 20 61  * The table we a
276dd 72 65 20 69 6e 73 65 72 74 69 6e 67 20 69 6e 74  re inserting int
276de 6f 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  o */.  Select *p
276df 53 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20  Select,      /* 
276e0 41 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  A SELECT stateme
276e1 6e 74 20 74 6f 20 75 73 65 20 61 73 20 74 68 65  nt to use as the
276e2 20 64 61 74 61 20 73 6f 75 72 63 65 20 2a 2f 0a   data source */.
276e3 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20 20    int onError,  
276e4 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20 74          /* How t
276e5 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72 61  o handle constra
276e6 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20  int errors */.  
276e7 69 6e 74 20 69 44 62 44 65 73 74 20 20 20 20 20  int iDbDest     
276e8 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
276e9 61 62 61 73 65 20 6f 66 20 70 44 65 73 74 20 2a  abase of pDest *
276ea 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  /.);../*.** This
276eb 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
276ec 20 74 6f 20 68 61 6e 64 6c 65 20 53 51 4c 20 6f   to handle SQL o
276ed 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
276ee 66 6f 72 6d 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  forms:.**.**    
276ef 69 6e 73 65 72 74 20 69 6e 74 6f 20 54 41 42 4c  insert into TABL
276f0 45 20 28 49 44 4c 49 53 54 29 20 76 61 6c 75 65  E (IDLIST) value
276f1 73 28 45 58 50 52 4c 49 53 54 29 0a 2a 2a 20 20  s(EXPRLIST).**  
276f2 20 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 54 41    insert into TA
276f3 42 4c 45 20 28 49 44 4c 49 53 54 29 20 73 65 6c  BLE (IDLIST) sel
276f4 65 63 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 44  ect.**.** The ID
276f5 4c 49 53 54 20 66 6f 6c 6c 6f 77 69 6e 67 20 74  LIST following t
276f6 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 73  he table name is
276f7 20 61 6c 77 61 79 73 20 6f 70 74 69 6f 6e 61 6c   always optional
276f8 2e 20 20 49 66 20 6f 6d 69 74 74 65 64 2c 0a 2a  .  If omitted,.*
276f9 2a 20 74 68 65 6e 20 61 20 6c 69 73 74 20 6f 66  * then a list of
276fa 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 66 6f 72   all columns for
276fb 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 73 75   the table is su
276fc 62 73 74 69 74 75 74 65 64 2e 20 20 54 68 65 20  bstituted.  The 
276fd 49 44 4c 49 53 54 0a 2a 2a 20 61 70 70 65 61 72  IDLIST.** appear
276fe 73 20 69 6e 20 74 68 65 20 70 43 6f 6c 75 6d 6e  s in the pColumn
276ff 20 70 61 72 61 6d 65 74 65 72 2e 20 20 70 43 6f   parameter.  pCo
27700 6c 75 6d 6e 20 69 73 20 4e 55 4c 4c 20 69 66 20  lumn is NULL if 
27701 49 44 4c 49 53 54 20 69 73 20 6f 6d 69 74 74 65  IDLIST is omitte
27702 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4c 69  d..**.** The pLi
27703 73 74 20 70 61 72 61 6d 65 74 65 72 20 68 6f 6c  st parameter hol
27704 64 73 20 45 58 50 52 4c 49 53 54 20 69 6e 20 74  ds EXPRLIST in t
27705 68 65 20 66 69 72 73 74 20 66 6f 72 6d 20 6f 66  he first form of
27706 20 74 68 65 20 49 4e 53 45 52 54 0a 2a 2a 20 73   the INSERT.** s
27707 74 61 74 65 6d 65 6e 74 20 61 62 6f 76 65 2c 20  tatement above, 
27708 61 6e 64 20 70 53 65 6c 65 63 74 20 69 73 20 4e  and pSelect is N
27709 55 4c 4c 2e 20 20 46 6f 72 20 74 68 65 20 73 65  ULL.  For the se
2770a 63 6f 6e 64 20 66 6f 72 6d 2c 20 70 4c 69 73 74  cond form, pList
2770b 20 69 73 0a 2a 2a 20 4e 55 4c 4c 20 61 6e 64 20   is.** NULL and 
2770c 70 53 65 6c 65 63 74 20 69 73 20 61 20 70 6f 69  pSelect is a poi
2770d 6e 74 65 72 20 74 6f 20 74 68 65 20 73 65 6c 65  nter to the sele
2770e 63 74 20 73 74 61 74 65 6d 65 6e 74 20 75 73 65  ct statement use
2770f 64 20 74 6f 20 67 65 6e 65 72 61 74 65 0a 2a 2a  d to generate.**
27710 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 69 6e   data for the in
27711 73 65 72 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  sert..**.** The 
27712 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 66  code generated f
27713 6f 6c 6c 6f 77 73 20 6f 6e 65 20 6f 66 20 66 6f  ollows one of fo
27714 75 72 20 74 65 6d 70 6c 61 74 65 73 2e 20 20 46  ur templates.  F
27715 6f 72 20 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 73  or a simple.** s
27716 65 6c 65 63 74 20 77 69 74 68 20 64 61 74 61 20  elect with data 
27717 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 61 20 56 41  coming from a VA
27718 4c 55 45 53 20 63 6c 61 75 73 65 2c 20 74 68 65  LUES clause, the
27719 20 63 6f 64 65 20 65 78 65 63 75 74 65 73 0a 2a   code executes.*
2771a 2a 20 6f 6e 63 65 20 73 74 72 61 69 67 68 74 20  * once straight 
2771b 64 6f 77 6e 20 74 68 72 6f 75 67 68 2e 20 20 50  down through.  P
2771c 73 65 75 64 6f 2d 63 6f 64 65 20 66 6f 6c 6c 6f  seudo-code follo
2771d 77 73 20 28 77 65 20 63 61 6c 6c 20 74 68 69 73  ws (we call this
2771e 0a 2a 2a 20 74 68 65 20 22 31 73 74 20 74 65 6d  .** the "1st tem
2771f 70 6c 61 74 65 22 29 3a 0a 2a 2a 0a 2a 2a 20 20  plate"):.**.**  
27720 20 20 20 20 20 20 20 6f 70 65 6e 20 77 72 69 74         open writ
27721 65 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62  e cursor to <tab
27722 6c 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64 69  le> and its indi
27723 63 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 70  ces.**         p
27724 75 74 73 20 56 41 4c 55 45 53 20 63 6c 61 75 73  uts VALUES claus
27725 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e  e expressions on
27726 74 6f 20 74 68 65 20 73 74 61 63 6b 0a 2a 2a 20  to the stack.** 
27727 20 20 20 20 20 20 20 20 77 72 69 74 65 20 74 68          write th
27728 65 20 72 65 73 75 6c 74 69 6e 67 20 72 65 63 6f  e resulting reco
27729 72 64 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a  rd into <table>.
2772a 2a 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e  **         clean
2772b 75 70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 72  up.**.** The thr
2772c 65 65 20 72 65 6d 61 69 6e 69 6e 67 20 74 65 6d  ee remaining tem
2772d 70 6c 61 74 65 73 20 61 73 73 75 6d 65 20 74 68  plates assume th
2772e 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6f  e statement is o
2772f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a  f the form.**.**
27730 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20 3c     INSERT INTO <
27731 74 61 62 6c 65 3e 20 53 45 4c 45 43 54 20 2e 2e  table> SELECT ..
27732 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53  ..**.** If the S
27733 45 4c 45 43 54 20 63 6c 61 75 73 65 20 69 73 20  ELECT clause is 
27734 6f 66 20 74 68 65 20 72 65 73 74 72 69 63 74 65  of the restricte
27735 64 20 66 6f 72 6d 20 22 53 45 4c 45 43 54 20 2a  d form "SELECT *
27736 20 46 52 4f 4d 20 3c 74 61 62 6c 65 32 3e 22 20   FROM <table2>" 
27737 2d 0a 2a 2a 20 69 6e 20 6f 74 68 65 72 20 77 6f  -.** in other wo
27738 72 64 73 20 69 66 20 74 68 65 20 53 45 4c 45 43  rds if the SELEC
27739 54 20 70 75 6c 6c 73 20 61 6c 6c 20 63 6f 6c 75  T pulls all colu
2773a 6d 6e 73 20 66 72 6f 6d 20 61 20 73 69 6e 67 6c  mns from a singl
2773b 65 20 74 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74  e table.** and t
2773c 68 65 72 65 20 69 73 20 6e 6f 20 57 48 45 52 45  here is no WHERE
2773d 20 6f 72 20 4c 49 4d 49 54 20 6f 72 20 47 52 4f   or LIMIT or GRO
2773e 55 50 20 42 59 20 6f 72 20 4f 52 44 45 52 20 42  UP BY or ORDER B
2773f 59 20 63 6c 61 75 73 65 73 2c 20 61 6e 64 0a 2a  Y clauses, and.*
27740 2a 20 69 66 20 3c 74 61 62 6c 65 32 3e 20 61 6e  * if <table2> an
27741 64 20 3c 74 61 62 6c 65 31 3e 20 61 72 65 20 64  d <table1> are d
27742 69 73 74 69 6e 63 74 20 74 61 62 6c 65 73 20 62  istinct tables b
27743 75 74 20 68 61 76 65 20 69 64 65 6e 74 69 63 61  ut have identica
27744 6c 0a 2a 2a 20 73 63 68 65 6d 61 73 2c 20 69 6e  l.** schemas, in
27745 63 6c 75 64 69 6e 67 20 61 6c 6c 20 74 68 65 20  cluding all the 
27746 73 61 6d 65 20 69 6e 64 69 63 65 73 2c 20 74 68  same indices, th
27747 65 6e 20 61 20 73 70 65 63 69 61 6c 20 6f 70 74  en a special opt
27748 69 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 69 73 20  imization.** is 
27749 69 6e 76 6f 6b 65 64 20 74 68 61 74 20 63 6f 70  invoked that cop
2774a 69 65 73 20 72 61 77 20 72 65 63 6f 72 64 73 20  ies raw records 
2774b 66 72 6f 6d 20 3c 74 61 62 6c 65 32 3e 20 6f 76  from <table2> ov
2774c 65 72 20 74 6f 20 3c 74 61 62 6c 65 31 3e 2e 0a  er to <table1>..
2774d 2a 2a 20 53 65 65 20 74 68 65 20 78 66 65 72 4f  ** See the xferO
2774e 70 74 69 6d 69 7a 61 74 69 6f 6e 28 29 20 66 75  ptimization() fu
2774f 6e 63 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 69  nction for the i
27750 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
27751 20 74 68 69 73 0a 2a 2a 20 74 65 6d 70 6c 61 74   this.** templat
27752 65 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20  e.  This is the 
27753 32 6e 64 20 74 65 6d 70 6c 61 74 65 2e 0a 2a 2a  2nd template..**
27754 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 70 65 6e  .**         open
27755 20 61 20 77 72 69 74 65 20 63 75 72 73 6f 72 20   a write cursor 
27756 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20  to <table>.**   
27757 20 20 20 20 20 20 6f 70 65 6e 20 72 65 61 64 20        open read 
27758 63 75 72 73 6f 72 20 6f 6e 20 3c 74 61 62 6c 65  cursor on <table
27759 32 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 74 72  2>.**         tr
2775a 61 6e 73 66 65 72 20 61 6c 6c 20 72 65 63 6f 72  ansfer all recor
2775b 64 73 20 69 6e 20 3c 74 61 62 6c 65 32 3e 20 6f  ds in <table2> o
2775c 76 65 72 20 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a  ver to <table>.*
2775d 2a 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 20  *         close 
2775e 63 75 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20  cursors.**      
2775f 20 20 20 66 6f 72 65 61 63 68 20 69 6e 64 65 78     foreach index
27760 20 6f 6e 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20   on <table>.**  
27761 20 20 20 20 20 20 20 20 20 6f 70 65 6e 20 61 20           open a 
27762 77 72 69 74 65 20 63 75 72 73 6f 72 20 6f 6e 20  write cursor on 
27763 74 68 65 20 3c 74 61 62 6c 65 3e 20 69 6e 64 65  the <table> inde
27764 78 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6f  x.**           o
27765 70 65 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f  pen a read curso
27766 72 20 6f 6e 20 74 68 65 20 63 6f 72 72 65 73 70  r on the corresp
27767 6f 6e 64 69 6e 67 20 3c 74 61 62 6c 65 32 3e 20  onding <table2> 
27768 69 6e 64 65 78 0a 2a 2a 20 20 20 20 20 20 20 20  index.**        
27769 20 20 20 74 72 61 6e 73 66 65 72 20 61 6c 6c 20     transfer all 
2776a 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74 68 65  records from the
2776b 20 72 65 61 64 20 74 6f 20 74 68 65 20 77 72 69   read to the wri
2776c 74 65 20 63 75 72 73 6f 72 73 0a 2a 2a 20 20 20  te cursors.**   
2776d 20 20 20 20 20 20 20 20 63 6c 6f 73 65 20 63 75          close cu
2776e 72 73 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20  rsors.**        
2776f 20 65 6e 64 20 66 6f 72 65 61 63 68 0a 2a 2a 0a   end foreach.**.
27770 2a 2a 20 54 68 65 20 33 72 64 20 74 65 6d 70 6c  ** The 3rd templ
27771 61 74 65 20 69 73 20 66 6f 72 20 77 68 65 6e 20  ate is for when 
27772 74 68 65 20 73 65 63 6f 6e 64 20 74 65 6d 70 6c  the second templ
27773 61 74 65 20 64 6f 65 73 20 6e 6f 74 20 61 70 70  ate does not app
27774 6c 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20 53 45  ly.** and the SE
27775 4c 45 43 54 20 63 6c 61 75 73 65 20 64 6f 65 73  LECT clause does
27776 20 6e 6f 74 20 72 65 61 64 20 66 72 6f 6d 20 3c   not read from <
27777 74 61 62 6c 65 3e 20 61 74 20 61 6e 79 20 74 69  table> at any ti
27778 6d 65 2e 0a 2a 2a 20 54 68 65 20 67 65 6e 65 72  me..** The gener
27779 61 74 65 64 20 63 6f 64 65 20 66 6f 6c 6c 6f 77  ated code follow
2777a 73 20 74 68 69 73 20 74 65 6d 70 6c 61 74 65 3a  s this template:
2777b 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 45  .**.**         E
2777c 4f 46 20 3c 2d 20 30 0a 2a 2a 20 20 20 20 20 20  OF <- 0.**      
2777d 20 20 20 58 20 3c 2d 20 41 0a 2a 2a 20 20 20 20     X <- A.**    
2777e 20 20 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20 20       goto B.**  
2777f 20 20 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72      A: setup for
27780 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20   the SELECT.**  
27781 20 20 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65 72         loop over
27782 20 74 68 65 20 72 6f 77 73 20 69 6e 20 74 68 65   the rows in the
27783 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20   SELECT.**      
27784 20 20 20 20 20 6c 6f 61 64 20 76 61 6c 75 65 73       load values
27785 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 73 20   into registers 
27786 52 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20 20  R..R+n.**       
27787 20 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20      yield X.**  
27788 20 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a         end loop.
27789 2a 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61 6e  **         clean
2778a 75 70 20 61 66 74 65 72 20 74 68 65 20 53 45 4c  up after the SEL
2778b 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 45  ECT.**         E
2778c 4f 46 20 3c 2d 20 31 0a 2a 2a 20 20 20 20 20 20  OF <- 1.**      
2778d 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20     yield X.**   
2778e 20 20 20 20 20 20 67 6f 74 6f 20 41 0a 2a 2a 20        goto A.** 
2778f 20 20 20 20 20 42 3a 20 6f 70 65 6e 20 77 72 69       B: open wri
27790 74 65 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61  te cursor to <ta
27791 62 6c 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64  ble> and its ind
27792 69 63 65 73 0a 2a 2a 20 20 20 20 20 20 43 3a 20  ices.**      C: 
27793 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20  yield X.**      
27794 20 20 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 44     if EOF goto D
27795 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 73 65  .**         inse
27796 72 74 20 74 68 65 20 73 65 6c 65 63 74 20 72 65  rt the select re
27797 73 75 6c 74 20 69 6e 74 6f 20 3c 74 61 62 6c 65  sult into <table
27798 3e 20 66 72 6f 6d 20 52 2e 2e 52 2b 6e 0a 2a 2a  > from R..R+n.**
27799 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 43 0a           goto C.
2779a 2a 2a 20 20 20 20 20 20 44 3a 20 63 6c 65 61 6e  **      D: clean
2779b 75 70 0a 2a 2a 0a 2a 2a 20 54 68 65 20 34 74 68  up.**.** The 4th
2779c 20 74 65 6d 70 6c 61 74 65 20 69 73 20 75 73 65   template is use
2779d 64 20 69 66 20 74 68 65 20 69 6e 73 65 72 74 20  d if the insert 
2779e 73 74 61 74 65 6d 65 6e 74 20 74 61 6b 65 73 20  statement takes 
2779f 69 74 73 0a 2a 2a 20 76 61 6c 75 65 73 20 66 72  its.** values fr
277a0 6f 6d 20 61 20 53 45 4c 45 43 54 20 62 75 74 20  om a SELECT but 
277a1 74 68 65 20 64 61 74 61 20 69 73 20 62 65 69 6e  the data is bein
277a2 67 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  g inserted into 
277a3 61 20 74 61 62 6c 65 0a 2a 2a 20 74 68 61 74 20  a table.** that 
277a4 69 73 20 61 6c 73 6f 20 72 65 61 64 20 61 73 20  is also read as 
277a5 70 61 72 74 20 6f 66 20 74 68 65 20 53 45 4c 45  part of the SELE
277a6 43 54 2e 20 20 49 6e 20 74 68 65 20 74 68 69 72  CT.  In the thir
277a7 64 20 66 6f 72 6d 2c 0a 2a 2a 20 77 65 20 68 61  d form,.** we ha
277a8 76 65 20 74 6f 20 75 73 65 20 61 20 69 6e 74 65  ve to use a inte
277a9 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65 20 74  rmediate table t
277aa 6f 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75  o store the resu
277ab 6c 74 73 20 6f 66 0a 2a 2a 20 74 68 65 20 73 65  lts of.** the se
277ac 6c 65 63 74 2e 20 20 54 68 65 20 74 65 6d 70 6c  lect.  The templ
277ad 61 74 65 20 69 73 20 6c 69 6b 65 20 74 68 69 73  ate is like this
277ae 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  :.**.**         
277af 45 4f 46 20 3c 2d 20 30 0a 2a 2a 20 20 20 20 20  EOF <- 0.**     
277b0 20 20 20 20 58 20 3c 2d 20 41 0a 2a 2a 20 20 20      X <- A.**   
277b1 20 20 20 20 20 20 67 6f 74 6f 20 42 0a 2a 2a 20        goto B.** 
277b2 20 20 20 20 20 41 3a 20 73 65 74 75 70 20 66 6f       A: setup fo
277b3 72 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20  r the SELECT.** 
277b4 20 20 20 20 20 20 20 20 6c 6f 6f 70 20 6f 76 65          loop ove
277b5 72 20 74 68 65 20 74 61 62 6c 65 73 20 69 6e 20  r the tables in 
277b6 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20  the SELECT.**   
277b7 20 20 20 20 20 20 20 20 6c 6f 61 64 20 76 61 6c          load val
277b8 75 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  ue into register
277b9 20 52 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20   R..R+n.**      
277ba 20 20 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20       yield X.** 
277bb 20 20 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70          end loop
277bc 0a 2a 2a 20 20 20 20 20 20 20 20 20 63 6c 65 61  .**         clea
277bd 6e 75 70 20 61 66 74 65 72 20 74 68 65 20 53 45  nup after the SE
277be 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20  LECT.**         
277bf 45 4f 46 20 3c 2d 20 31 0a 2a 2a 20 20 20 20 20  EOF <- 1.**     
277c0 20 20 20 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20      yield X.**  
277c1 20 20 20 20 20 20 20 68 61 6c 74 2d 65 72 72 6f         halt-erro
277c2 72 0a 2a 2a 20 20 20 20 20 20 42 3a 20 6f 70 65  r.**      B: ope
277c3 6e 20 74 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20  n temp table.** 
277c4 20 20 20 20 20 4c 3a 20 79 69 65 6c 64 20 58 0a       L: yield X.
277c5 2a 2a 20 20 20 20 20 20 20 20 20 69 66 20 45 4f  **         if EO
277c6 46 20 67 6f 74 6f 20 4d 0a 2a 2a 20 20 20 20 20  F goto M.**     
277c7 20 20 20 20 69 6e 73 65 72 74 20 72 6f 77 20 66      insert row f
277c8 72 6f 6d 20 52 2e 2e 52 2b 6e 20 69 6e 74 6f 20  rom R..R+n into 
277c9 74 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20  temp table.**   
277ca 20 20 20 20 20 20 67 6f 74 6f 20 4c 0a 2a 2a 20        goto L.** 
277cb 20 20 20 20 20 4d 3a 20 6f 70 65 6e 20 77 72 69       M: open wri
277cc 74 65 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61  te cursor to <ta
277cd 62 6c 65 3e 20 61 6e 64 20 69 74 73 20 69 6e 64  ble> and its ind
277ce 69 63 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  ices.**         
277cf 72 65 77 69 6e 64 20 74 65 6d 70 20 74 61 62 6c  rewind temp tabl
277d0 65 0a 2a 2a 20 20 20 20 20 20 43 3a 20 6c 6f 6f  e.**      C: loo
277d1 70 20 6f 76 65 72 20 72 6f 77 73 20 6f 66 20 69  p over rows of i
277d2 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c  ntermediate tabl
277d3 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74  e.**           t
277d4 72 61 6e 73 66 65 72 20 76 61 6c 75 65 73 20 66  ransfer values f
277d5 6f 72 6d 20 69 6e 74 65 72 6d 65 64 69 61 74 65  orm intermediate
277d6 20 74 61 62 6c 65 20 69 6e 74 6f 20 3c 74 61 62   table into <tab
277d7 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 65  le>.**         e
277d8 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20 20 20 20 20  nd loop.**      
277d9 44 3a 20 63 6c 65 61 6e 75 70 0a 2a 2f 0a 53 51  D: cleanup.*/.SQ
277da 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
277db 64 20 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28  d sqlite3Insert(
277dc 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
277dd 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73  ,        /* Pars
277de 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
277df 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
277e0 74 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  t,    /* Name of
277e1 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63   table into whic
277e2 68 20 77 65 20 61 72 65 20 69 6e 73 65 72 74 69  h we are inserti
277e3 6e 67 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ng */.  ExprList
277e4 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 2f 2a   *pList,      /*
277e5 20 4c 69 73 74 20 6f 66 20 76 61 6c 75 65 73 20   List of values 
277e6 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a  to be inserted *
277e7 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  /.  Select *pSel
277e8 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 41 20 53  ect,      /* A S
277e9 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
277ea 74 6f 20 75 73 65 20 61 73 20 74 68 65 20 64 61  to use as the da
277eb 74 61 20 73 6f 75 72 63 65 20 2a 2f 0a 20 20 49  ta source */.  I
277ec 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d 6e 2c 20  dList *pColumn, 
277ed 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e       /* Column n
277ee 61 6d 65 73 20 63 6f 72 72 65 73 70 6f 6e 64 69  ames correspondi
277ef 6e 67 20 74 6f 20 49 44 4c 49 53 54 2e 20 2a 2f  ng to IDLIST. */
277f0 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 20 20  .  int onError  
277f1 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77 20           /* How 
277f2 74 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74 72  to handle constr
277f3 61 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a 29  aint errors */.)
277f4 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  {.  sqlite3 *db;
277f5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
277f6 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73   main database s
277f7 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 54 61  tructure */.  Ta
277f8 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20  ble *pTab;      
277f9 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65      /* The table
277fa 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 2e   to insert into.
277fb 20 20 61 6b 61 20 54 41 42 4c 45 20 2a 2f 0a 20    aka TABLE */. 
277fc 20 63 68 61 72 20 2a 7a 54 61 62 3b 20 20 20 20   char *zTab;    
277fd 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
277fe 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e 74 6f  f the table into
277ff 20 77 68 69 63 68 20 77 65 20 61 72 65 20 69 6e   which we are in
27800 73 65 72 74 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e  serting */.  con
27801 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20  st char *zDb;   
27802 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
27803 65 20 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69  e database holdi
27804 6e 67 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f  ng this table */
27805 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 69 64 78  .  int i, j, idx
27806 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70  ;        /* Loop
27807 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 56   counters */.  V
27808 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20  dbe *v;         
27809 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
2780a 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20   code into this 
2780b 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20  virtual machine 
2780c 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  */.  Index *pIdx
2780d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f  ;          /* Fo
2780e 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 69  r looping over i
2780f 6e 64 69 63 65 73 20 6f 66 20 74 68 65 20 74 61  ndices of the ta
27810 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  ble */.  int nCo
27811 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 2f  lumn;          /
27812 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  * Number of colu
27813 6d 6e 73 20 69 6e 20 74 68 65 20 64 61 74 61 20  mns in the data 
27814 2a 2f 0a 20 20 69 6e 74 20 6e 48 69 64 64 65 6e  */.  int nHidden
27815 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 75   = 0;      /* Nu
27816 6d 62 65 72 20 6f 66 20 68 69 64 64 65 6e 20 63  mber of hidden c
27817 6f 6c 75 6d 6e 73 20 69 66 20 54 41 42 4c 45 20  olumns if TABLE 
27818 69 73 20 76 69 72 74 75 61 6c 20 2a 2f 0a 20 20  is virtual */.  
27819 69 6e 74 20 62 61 73 65 43 75 72 20 3d 20 30 3b  int baseCur = 0;
2781a 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75        /* VDBE Cu
2781b 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20  rsor number for 
2781c 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 6b 65  pTab */.  int ke
2781d 79 43 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 20 20 20  yColumn = -1;   
2781e 2f 2a 20 43 6f 6c 75 6d 6e 20 74 68 61 74 20 69  /* Column that i
2781f 73 20 74 68 65 20 49 4e 54 45 47 45 52 20 50 52  s the INTEGER PR
27820 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 69  IMARY KEY */.  i
27821 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20 20  nt endOfLoop;   
27822 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20 66 6f       /* Label fo
27823 72 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  r the end of the
27824 20 69 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f 70 20   insertion loop 
27825 2a 2f 0a 20 20 69 6e 74 20 75 73 65 54 65 6d 70  */.  int useTemp
27826 54 61 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 53 74  Table = 0; /* St
27827 6f 72 65 20 53 45 4c 45 43 54 20 72 65 73 75 6c  ore SELECT resul
27828 74 73 20 69 6e 20 69 6e 74 65 72 6d 65 64 69 61  ts in intermedia
27829 74 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  te table */.  in
2782a 74 20 73 72 63 54 61 62 20 3d 20 30 3b 20 20 20  t srcTab = 0;   
2782b 20 20 20 20 2f 2a 20 44 61 74 61 20 63 6f 6d 65      /* Data come
2782c 73 20 66 72 6f 6d 20 74 68 69 73 20 74 65 6d 70  s from this temp
2782d 6f 72 61 72 79 20 63 75 72 73 6f 72 20 69 66 20  orary cursor if 
2782e 3e 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  >=0 */.  int add
2782f 72 49 6e 73 54 6f 70 20 3d 20 30 3b 20 20 20 2f  rInsTop = 0;   /
27830 2a 20 4a 75 6d 70 20 74 6f 20 6c 61 62 65 6c 20  * Jump to label 
27831 22 44 22 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  "D" */.  int add
27832 72 43 6f 6e 74 20 3d 20 30 3b 20 20 20 20 20 2f  rCont = 0;     /
27833 2a 20 54 6f 70 20 6f 66 20 69 6e 73 65 72 74 20  * Top of insert 
27834 6c 6f 6f 70 2e 20 4c 61 62 65 6c 20 22 43 22 20  loop. Label "C" 
27835 69 6e 20 74 65 6d 70 6c 61 74 65 73 20 33 20 61  in templates 3 a
27836 6e 64 20 34 20 2a 2f 0a 20 20 69 6e 74 20 61 64  nd 4 */.  int ad
27837 64 72 53 65 6c 65 63 74 20 3d 20 30 3b 20 20 20  drSelect = 0;   
27838 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 63 6f  /* Address of co
27839 72 6f 75 74 69 6e 65 20 74 68 61 74 20 69 6d 70  routine that imp
2783a 6c 65 6d 65 6e 74 73 20 74 68 65 20 53 45 4c 45  lements the SELE
2783b 43 54 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65  CT */.  SelectDe
2783c 73 74 20 64 65 73 74 3b 20 20 20 20 20 20 2f 2a  st dest;      /*
2783d 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 66 6f 72   Destination for
2783e 20 53 45 4c 45 43 54 20 6f 6e 20 72 68 73 20 6f   SELECT on rhs o
2783f 66 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69 6e  f INSERT */.  in
27840 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
27841 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
27842 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67  database holding
27843 20 54 41 42 4c 45 20 2a 2f 0a 20 20 44 62 20 2a   TABLE */.  Db *
27844 70 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  pDb;            
27845 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
27846 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 61 62  e containing tab
27847 6c 65 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65  le being inserte
27848 64 20 69 6e 74 6f 20 2a 2f 0a 20 20 69 6e 74 20  d into */.  int 
27849 61 70 70 65 6e 64 46 6c 61 67 20 3d 20 30 3b 20  appendFlag = 0; 
2784a 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65    /* True if the
2784b 20 69 6e 73 65 72 74 20 69 73 20 6c 69 6b 65 6c   insert is likel
2784c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65 6e  y to be an appen
2784d 64 20 2a 2f 0a 0a 20 20 2f 2a 20 52 65 67 69 73  d */..  /* Regis
2784e 74 65 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20  ter allocations 
2784f 2a 2f 0a 20 20 69 6e 74 20 72 65 67 46 72 6f 6d  */.  int regFrom
27850 53 65 6c 65 63 74 20 3d 20 30 3b 2f 2a 20 42 61  Select = 0;/* Ba
27851 73 65 20 72 65 67 69 73 74 65 72 20 66 6f 72 20  se register for 
27852 64 61 74 61 20 63 6f 6d 69 6e 67 20 66 72 6f 6d  data coming from
27853 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20 69 6e 74   SELECT */.  int
27854 20 72 65 67 41 75 74 6f 69 6e 63 20 3d 20 30 3b   regAutoinc = 0;
27855 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68     /* Register h
27856 6f 6c 64 69 6e 67 20 74 68 65 20 41 55 54 4f 49  olding the AUTOI
27857 4e 43 52 45 4d 45 4e 54 20 63 6f 75 6e 74 65 72  NCREMENT counter
27858 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f 77   */.  int regRow
27859 43 6f 75 6e 74 20 3d 20 30 3b 20 20 2f 2a 20 4d  Count = 0;  /* M
2785a 65 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20  emory cell used 
2785b 66 6f 72 20 74 68 65 20 72 6f 77 20 63 6f 75 6e  for the row coun
2785c 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ter */.  int reg
2785d 49 6e 73 3b 20 20 20 20 20 20 20 20 20 20 20 2f  Ins;           /
2785e 2a 20 42 6c 6f 63 6b 20 6f 66 20 72 65 67 73 20  * Block of regs 
2785f 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64 2b 64 61  holding rowid+da
27860 74 61 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65  ta being inserte
27861 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 52 6f  d */.  int regRo
27862 77 69 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  wid;         /* 
27863 72 65 67 69 73 74 65 72 73 20 68 6f 6c 64 69 6e  registers holdin
27864 67 20 69 6e 73 65 72 74 20 72 6f 77 69 64 20 2a  g insert rowid *
27865 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61 3b  /.  int regData;
27866 20 20 20 20 20 20 20 20 20 20 2f 2a 20 72 65 67            /* reg
27867 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 66 69  ister holding fi
27868 72 73 74 20 63 6f 6c 75 6d 6e 20 74 6f 20 69 6e  rst column to in
27869 73 65 72 74 20 2a 2f 0a 20 20 69 6e 74 20 72 65  sert */.  int re
2786a 67 52 65 63 6f 72 64 3b 20 20 20 20 20 20 20 20  gRecord;        
2786b 2f 2a 20 48 6f 6c 64 73 20 74 68 65 20 61 73 73  /* Holds the ass
2786c 65 6d 62 6c 69 65 64 20 72 6f 77 20 72 65 63 6f  emblied row reco
2786d 72 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 45  rd */.  int regE
2786e 6f 66 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a  of = 0;       /*
2786f 20 52 65 67 69 73 74 65 72 20 72 65 63 6f 72 64   Register record
27870 69 6e 67 20 65 6e 64 20 6f 66 20 53 45 4c 45 43  ing end of SELEC
27871 54 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20  T data */.  int 
27872 2a 61 52 65 67 49 64 78 20 3d 20 30 3b 20 20 20  *aRegIdx = 0;   
27873 20 20 2f 2a 20 4f 6e 65 20 72 65 67 69 73 74 65    /* One registe
27874 72 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 65  r allocated to e
27875 61 63 68 20 69 6e 64 65 78 20 2a 2f 0a 0a 23 69  ach index */..#i
27876 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
27877 54 5f 54 52 49 47 47 45 52 0a 20 20 69 6e 74 20  T_TRIGGER.  int 
27878 69 73 56 69 65 77 3b 20 20 20 20 20 20 20 20 20  isView;         
27879 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
2787a 69 66 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f  if attempting to
2787b 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 61 20 76   insert into a v
2787c 69 65 77 20 2a 2f 0a 20 20 54 72 69 67 67 65 72  iew */.  Trigger
2787d 20 2a 70 54 72 69 67 67 65 72 3b 20 20 20 20 20   *pTrigger;     
2787e 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
2787f 74 72 69 67 67 65 72 73 20 6f 6e 20 70 54 61 62  triggers on pTab
27880 2c 20 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f  , if required */
27881 0a 20 20 69 6e 74 20 74 6d 61 73 6b 3b 20 20 20  .  int tmask;   
27882 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
27883 2a 20 4d 61 73 6b 20 6f 66 20 74 72 69 67 67 65  * Mask of trigge
27884 72 20 74 69 6d 65 73 20 2a 2f 0a 23 65 6e 64 69  r times */.#endi
27885 66 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  f..  db = pParse
27886 2d 3e 64 62 3b 0a 20 20 6d 65 6d 73 65 74 28 26  ->db;.  memset(&
27887 64 65 73 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28  dest, 0, sizeof(
27888 64 65 73 74 29 29 3b 0a 20 20 69 66 28 20 70 50  dest));.  if( pP
27889 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62  arse->nErr || db
2788a 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2788b 7b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72  {.    goto inser
2788c 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a  t_cleanup;.  }..
2788d 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20    /* Locate the 
2788e 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68  table into which
2788f 20 77 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65   we will be inse
27890 72 74 69 6e 67 20 6e 65 77 20 69 6e 66 6f 72 6d  rting new inform
27891 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73  ation..  */.  as
27892 73 65 72 74 28 20 70 54 61 62 4c 69 73 74 2d 3e  sert( pTabList->
27893 6e 53 72 63 3d 3d 31 20 29 3b 0a 20 20 7a 54 61  nSrc==1 );.  zTa
27894 62 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b  b = pTabList->a[
27895 30 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20  0].zName;.  if( 
27896 4e 45 56 45 52 28 7a 54 61 62 3d 3d 30 29 20 29  NEVER(zTab==0) )
27897 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65   goto insert_cle
27898 61 6e 75 70 3b 0a 20 20 70 54 61 62 20 3d 20 73  anup;.  pTab = s
27899 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f  qlite3SrcListLoo
2789a 6b 75 70 28 70 50 61 72 73 65 2c 20 70 54 61 62  kup(pParse, pTab
2789b 4c 69 73 74 29 3b 0a 20 20 69 66 28 20 70 54 61  List);.  if( pTa
2789c 62 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f  b==0 ){.    goto
2789d 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b   insert_cleanup;
2789e 0a 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c  .  }.  iDb = sql
2789f 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
278a0 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  x(db, pTab->pSch
278a1 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ema);.  assert( 
278a2 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  iDb<db->nDb );. 
278a3 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b   pDb = &db->aDb[
278a4 69 44 62 5d 3b 0a 20 20 7a 44 62 20 3d 20 70 44  iDb];.  zDb = pD
278a5 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20  b->zName;.  if( 
278a6 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
278a7 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
278a8 49 4e 53 45 52 54 2c 20 70 54 61 62 2d 3e 7a 4e  INSERT, pTab->zN
278a9 61 6d 65 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a  ame, 0, zDb) ){.
278aa 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f      goto insert_
278ab 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20  cleanup;.  }..  
278ac 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 69 66  /* Figure out if
278ad 20 77 65 20 68 61 76 65 20 61 6e 79 20 74 72 69   we have any tri
278ae 67 67 65 72 73 20 61 6e 64 20 69 66 20 74 68 65  ggers and if the
278af 20 74 61 62 6c 65 20 62 65 69 6e 67 0a 20 20 2a   table being.  *
278b0 2a 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20  * inserted into 
278b1 69 73 20 61 20 76 69 65 77 0a 20 20 2a 2f 0a 23  is a view.  */.#
278b2 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
278b3 49 54 5f 54 52 49 47 47 45 52 0a 20 20 70 54 72  IT_TRIGGER.  pTr
278b4 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 54  igger = sqlite3T
278b5 72 69 67 67 65 72 73 45 78 69 73 74 28 70 50 61  riggersExist(pPa
278b6 72 73 65 2c 20 70 54 61 62 2c 20 54 4b 5f 49 4e  rse, pTab, TK_IN
278b7 53 45 52 54 2c 20 30 2c 20 26 74 6d 61 73 6b 29  SERT, 0, &tmask)
278b8 3b 0a 20 20 69 73 56 69 65 77 20 3d 20 70 54 61  ;.  isView = pTa
278b9 62 2d 3e 70 53 65 6c 65 63 74 21 3d 30 3b 0a 23  b->pSelect!=0;.#
278ba 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 70 54  else.# define pT
278bb 72 69 67 67 65 72 20 30 0a 23 20 64 65 66 69 6e  rigger 0.# defin
278bc 65 20 74 6d 61 73 6b 20 30 0a 23 20 64 65 66 69  e tmask 0.# defi
278bd 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64  ne isView 0.#end
278be 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
278bf 5f 4f 4d 49 54 5f 56 49 45 57 0a 23 20 75 6e 64  _OMIT_VIEW.# und
278c0 65 66 20 69 73 56 69 65 77 0a 23 20 64 65 66 69  ef isView.# defi
278c1 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64  ne isView 0.#end
278c2 69 66 0a 20 20 61 73 73 65 72 74 28 20 28 70 54  if.  assert( (pT
278c3 72 69 67 67 65 72 20 26 26 20 74 6d 61 73 6b 29  rigger && tmask)
278c4 20 7c 7c 20 28 70 54 72 69 67 67 65 72 3d 3d 30   || (pTrigger==0
278c5 20 26 26 20 74 6d 61 73 6b 3d 3d 30 29 20 29 3b   && tmask==0) );
278c6 0a 0a 20 20 2f 2a 20 49 66 20 70 54 61 62 20 69  ..  /* If pTab i
278c7 73 20 72 65 61 6c 6c 79 20 61 20 76 69 65 77 2c  s really a view,
278c8 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20 68 61   make sure it ha
278c9 73 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a  s been initializ
278ca 65 64 2e 0a 20 20 2a 2a 20 56 69 65 77 47 65 74  ed..  ** ViewGet
278cb 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 69 73  ColumnNames() is
278cc 20 61 20 6e 6f 2d 6f 70 20 69 66 20 70 54 61 62   a no-op if pTab
278cd 20 69 73 20 6e 6f 74 20 61 20 76 69 65 77 20 28   is not a view (
278ce 6f 72 20 76 69 72 74 75 61 6c 20 0a 20 20 2a 2a  or virtual .  **
278cf 20 6d 6f 64 75 6c 65 20 74 61 62 6c 65 29 2e 0a   module table)..
278d0 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
278d1 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e  e3ViewGetColumnN
278d2 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54 61  ames(pParse, pTa
278d3 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 69  b) ){.    goto i
278d4 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20  nsert_cleanup;. 
278d5 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20   }..  /* Ensure 
278d6 74 68 61 74 3a 0a 20 20 2a 20 20 28 61 29 20 74  that:.  *  (a) t
278d7 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20  he table is not 
278d8 72 65 61 64 2d 6f 6e 6c 79 2c 20 0a 20 20 2a 20  read-only, .  * 
278d9 20 28 62 29 20 74 68 61 74 20 69 66 20 69 74 20   (b) that if it 
278da 69 73 20 61 20 76 69 65 77 20 74 68 65 6e 20 4f  is a view then O
278db 4e 20 49 4e 53 45 52 54 20 74 72 69 67 67 65 72  N INSERT trigger
278dc 73 20 65 78 69 73 74 0a 20 20 2a 2f 0a 20 20 69  s exist.  */.  i
278dd 66 28 20 73 71 6c 69 74 65 33 49 73 52 65 61 64  f( sqlite3IsRead
278de 4f 6e 6c 79 28 70 50 61 72 73 65 2c 20 70 54 61  Only(pParse, pTa
278df 62 2c 20 74 6d 61 73 6b 29 20 29 7b 0a 20 20 20  b, tmask) ){.   
278e0 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65   goto insert_cle
278e1 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  anup;.  }..  /* 
278e2 41 6c 6c 6f 63 61 74 65 20 61 20 56 44 42 45 0a  Allocate a VDBE.
278e3 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74    */.  v = sqlit
278e4 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
278e5 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20  );.  if( v==0 ) 
278e6 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61  goto insert_clea
278e7 6e 75 70 3b 0a 20 20 69 66 28 20 70 50 61 72 73  nup;.  if( pPars
278e8 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 20 73  e->nested==0 ) s
278e9 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43  qlite3VdbeCountC
278ea 68 61 6e 67 65 73 28 76 29 3b 0a 20 20 73 71 6c  hanges(v);.  sql
278eb 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
278ec 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
278ed 70 53 65 6c 65 63 74 20 7c 7c 20 70 54 72 69 67  pSelect || pTrig
278ee 67 65 72 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e  ger, iDb);..#ifn
278ef 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
278f0 58 46 45 52 5f 4f 50 54 0a 20 20 2f 2a 20 49 66  XFER_OPT.  /* If
278f1 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69   the statement i
278f2 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20 20  s of the form.  
278f3 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 49 4e  **.  **       IN
278f4 53 45 52 54 20 49 4e 54 4f 20 3c 74 61 62 6c 65  SERT INTO <table
278f5 31 3e 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  1> SELECT * FROM
278f6 20 3c 74 61 62 6c 65 32 3e 3b 0a 20 20 2a 2a 0a   <table2>;.  **.
278f7 20 20 2a 2a 20 54 68 65 6e 20 73 70 65 63 69 61    ** Then specia
278f8 6c 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20  l optimizations 
278f9 63 61 6e 20 62 65 20 61 70 70 6c 69 65 64 20 74  can be applied t
278fa 68 61 74 20 6d 61 6b 65 20 74 68 65 20 74 72 61  hat make the tra
278fb 6e 73 66 65 72 0a 20 20 2a 2a 20 76 65 72 79 20  nsfer.  ** very 
278fc 66 61 73 74 20 61 6e 64 20 77 68 69 63 68 20 72  fast and which r
278fd 65 64 75 63 65 20 66 72 61 67 6d 65 6e 74 61 74  educe fragmentat
278fe 69 6f 6e 20 6f 66 20 69 6e 64 69 63 65 73 2e 0a  ion of indices..
278ff 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 69    **.  ** This i
27900 73 20 74 68 65 20 32 6e 64 20 74 65 6d 70 6c 61  s the 2nd templa
27901 74 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  te..  */.  if( p
27902 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 78 66 65  Column==0 && xfe
27903 72 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 28 70 50  rOptimization(pP
27904 61 72 73 65 2c 20 70 54 61 62 2c 20 70 53 65 6c  arse, pTab, pSel
27905 65 63 74 2c 20 6f 6e 45 72 72 6f 72 2c 20 69 44  ect, onError, iD
27906 62 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  b) ){.    assert
27907 28 20 21 70 54 72 69 67 67 65 72 20 29 3b 0a 20  ( !pTrigger );. 
27908 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74     assert( pList
27909 3d 3d 30 20 29 3b 0a 20 20 20 20 67 6f 74 6f 20  ==0 );.    goto 
2790a 69 6e 73 65 72 74 5f 65 6e 64 3b 0a 20 20 7d 0a  insert_end;.  }.
2790b 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
2790c 5f 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54 20 2a  _OMIT_XFER_OPT *
2790d 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  /..  /* If this 
2790e 69 73 20 61 6e 20 41 55 54 4f 49 4e 43 52 45 4d  is an AUTOINCREM
2790f 45 4e 54 20 74 61 62 6c 65 2c 20 6c 6f 6f 6b 20  ENT table, look 
27910 75 70 20 74 68 65 20 73 65 71 75 65 6e 63 65 20  up the sequence 
27911 6e 75 6d 62 65 72 20 69 6e 20 74 68 65 0a 20 20  number in the.  
27912 2a 2a 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  ** sqlite_sequen
27913 63 65 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f  ce table and sto
27914 72 65 20 69 74 20 69 6e 20 6d 65 6d 6f 72 79 20  re it in memory 
27915 63 65 6c 6c 20 72 65 67 41 75 74 6f 69 6e 63 2e  cell regAutoinc.
27916 0a 20 20 2a 2f 0a 20 20 72 65 67 41 75 74 6f 69  .  */.  regAutoi
27917 6e 63 20 3d 20 61 75 74 6f 49 6e 63 42 65 67 69  nc = autoIncBegi
27918 6e 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70  n(pParse, iDb, p
27919 54 61 62 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75  Tab);..  /* Figu
2791a 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20  re out how many 
2791b 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 74 61 20  columns of data 
2791c 61 72 65 20 73 75 70 70 6c 69 65 64 2e 20 20 49  are supplied.  I
2791d 66 20 74 68 65 20 64 61 74 61 0a 20 20 2a 2a 20  f the data.  ** 
2791e 69 73 20 63 6f 6d 69 6e 67 20 66 72 6f 6d 20 61  is coming from a
2791f 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
27920 74 2c 20 74 68 65 6e 20 67 65 6e 65 72 61 74 65  t, then generate
27921 20 61 20 63 6f 2d 72 6f 75 74 69 6e 65 20 74 68   a co-routine th
27922 61 74 0a 20 20 2a 2a 20 70 72 6f 64 75 63 65 73  at.  ** produces
27923 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66   a single row of
27924 20 74 68 65 20 53 45 4c 45 43 54 20 6f 6e 20 65   the SELECT on e
27925 61 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 20  ach invocation. 
27926 20 54 68 65 0a 20 20 2a 2a 20 63 6f 2d 72 6f 75   The.  ** co-rou
27927 74 69 6e 65 20 69 73 20 74 68 65 20 63 6f 6d 6d  tine is the comm
27928 6f 6e 20 68 65 61 64 65 72 20 74 6f 20 74 68 65  on header to the
27929 20 33 72 64 20 61 6e 64 20 34 74 68 20 74 65 6d   3rd and 4th tem
2792a 70 6c 61 74 65 73 2e 0a 20 20 2a 2f 0a 20 20 69  plates..  */.  i
2792b 66 28 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  f( pSelect ){.  
2792c 20 20 2f 2a 20 44 61 74 61 20 69 73 20 63 6f 6d    /* Data is com
2792d 69 6e 67 20 66 72 6f 6d 20 61 20 53 45 4c 45 43  ing from a SELEC
2792e 54 2e 20 20 47 65 6e 65 72 61 74 65 20 63 6f 64  T.  Generate cod
2792f 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  e to implement t
27930 68 61 74 20 53 45 4c 45 43 54 0a 20 20 20 20 2a  hat SELECT.    *
27931 2a 20 61 73 20 61 20 63 6f 2d 72 6f 75 74 69 6e  * as a co-routin
27932 65 2e 20 20 54 68 65 20 63 6f 64 65 20 69 73 20  e.  The code is 
27933 63 6f 6d 6d 6f 6e 20 74 6f 20 62 6f 74 68 20 74  common to both t
27934 68 65 20 33 72 64 20 61 6e 64 20 34 74 68 0a 20  he 3rd and 4th. 
27935 20 20 20 2a 2a 20 74 65 6d 70 6c 61 74 65 73 3a     ** templates:
27936 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
27937 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 30 0a         EOF <- 0.
27938 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 58      **         X
27939 20 3c 2d 20 41 0a 20 20 20 20 2a 2a 20 20 20 20   <- A.    **    
2793a 20 20 20 20 20 67 6f 74 6f 20 42 0a 20 20 20 20       goto B.    
2793b 2a 2a 20 20 20 20 20 20 41 3a 20 73 65 74 75 70  **      A: setup
2793c 20 66 6f 72 20 74 68 65 20 53 45 4c 45 43 54 0a   for the SELECT.
2793d 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 6c      **         l
2793e 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 74 61 62  oop over the tab
2793f 6c 65 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43  les in the SELEC
27940 54 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  T.    **        
27941 20 20 20 6c 6f 61 64 20 76 61 6c 75 65 20 69 6e     load value in
27942 74 6f 20 72 65 67 69 73 74 65 72 20 52 2e 2e 52  to register R..R
27943 2b 6e 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  +n.    **       
27944 20 20 20 20 79 69 65 6c 64 20 58 0a 20 20 20 20      yield X.    
27945 2a 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20 6c  **         end l
27946 6f 6f 70 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  oop.    **      
27947 20 20 20 63 6c 65 61 6e 75 70 20 61 66 74 65 72     cleanup after
27948 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20   the SELECT.    
27949 2a 2a 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c  **         EOF <
2794a 2d 20 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  - 1.    **      
2794b 20 20 20 79 69 65 6c 64 20 58 0a 20 20 20 20 2a     yield X.    *
2794c 2a 20 20 20 20 20 20 20 20 20 68 61 6c 74 2d 65  *         halt-e
2794d 72 72 6f 72 0a 20 20 20 20 2a 2a 0a 20 20 20 20  rror.    **.    
2794e 2a 2a 20 4f 6e 20 65 61 63 68 20 69 6e 76 6f 63  ** On each invoc
2794f 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 2d  ation of the co-
27950 72 6f 75 74 69 6e 65 2c 20 69 74 20 70 75 74 73  routine, it puts
27951 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66   a single row of
27952 20 74 68 65 0a 20 20 20 20 2a 2a 20 53 45 4c 45   the.    ** SELE
27953 43 54 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 72  CT result into r
27954 65 67 69 73 74 65 72 73 20 64 65 73 74 2e 69 4d  egisters dest.iM
27955 65 6d 2e 2e 2e 64 65 73 74 2e 69 4d 65 6d 2b 64  em...dest.iMem+d
27956 65 73 74 2e 6e 4d 65 6d 2d 31 2e 0a 20 20 20 20  est.nMem-1..    
27957 2a 2a 20 28 54 68 65 73 65 20 6f 75 74 70 75 74  ** (These output
27958 20 72 65 67 69 73 74 65 72 73 20 61 72 65 20 61   registers are a
27959 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c 69  llocated by sqli
2795a 74 65 33 53 65 6c 65 63 74 28 29 2e 29 20 20 57  te3Select().)  W
2795b 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 53  hen.    ** the S
2795c 45 4c 45 43 54 20 63 6f 6d 70 6c 65 74 65 73 2c  ELECT completes,
2795d 20 69 74 20 73 65 74 73 20 74 68 65 20 45 4f 46   it sets the EOF
2795e 20 66 6c 61 67 20 73 74 6f 72 65 64 20 69 6e 20   flag stored in 
2795f 72 65 67 45 6f 66 2e 0a 20 20 20 20 2a 2f 0a 20  regEof..    */. 
27960 20 20 20 69 6e 74 20 72 63 2c 20 6a 31 3b 0a 0a     int rc, j1;..
27961 20 20 20 20 72 65 67 45 6f 66 20 3d 20 2b 2b 70      regEof = ++p
27962 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
27963 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27964 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
27965 2c 20 30 2c 20 72 65 67 45 6f 66 29 3b 20 20 20  , 0, regEof);   
27966 20 20 20 2f 2a 20 45 4f 46 20 3c 2d 20 30 20 2a     /* EOF <- 0 *
27967 2f 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e  /.    VdbeCommen
27968 74 28 28 76 2c 20 22 53 45 4c 45 43 54 20 65 6f  t((v, "SELECT eo
27969 66 20 66 6c 61 67 22 29 29 3b 0a 20 20 20 20 73  f flag"));.    s
2796a 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74  qlite3SelectDest
2796b 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f  Init(&dest, SRT_
2796c 43 6f 72 6f 75 74 69 6e 65 2c 20 2b 2b 70 50 61  Coroutine, ++pPa
2796d 72 73 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20  rse->nMem);.    
2796e 61 64 64 72 53 65 6c 65 63 74 20 3d 20 73 71 6c  addrSelect = sql
2796f 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
27970 64 64 72 28 76 29 2b 32 3b 0a 20 20 20 20 73 71  ddr(v)+2;.    sq
27971 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27972 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 61  v, OP_Integer, a
27973 64 64 72 53 65 6c 65 63 74 2d 31 2c 20 64 65 73  ddrSelect-1, des
27974 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 6a 31  t.iParm);.    j1
27975 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
27976 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
27977 20 30 2c 20 30 29 3b 0a 20 20 20 20 56 64 62 65   0, 0);.    Vdbe
27978 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4a 75 6d  Comment((v, "Jum
27979 70 20 6f 76 65 72 20 53 45 4c 45 43 54 20 63 6f  p over SELECT co
2797a 72 6f 75 74 69 6e 65 22 29 29 3b 0a 0a 20 20 20  routine"));..   
2797b 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20   /* Resolve the 
2797c 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74  expressions in t
2797d 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
2797e 65 6e 74 20 61 6e 64 20 65 78 65 63 75 74 65 20  ent and execute 
2797f 69 74 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  it. */.    rc = 
27980 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50  sqlite3Select(pP
27981 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2c 20 26  arse, pSelect, &
27982 64 65 73 74 29 3b 0a 20 20 20 20 61 73 73 65 72  dest);.    asser
27983 74 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d  t( pParse->nErr=
27984 3d 30 20 7c 7c 20 72 63 20 29 3b 0a 20 20 20 20  =0 || rc );.    
27985 69 66 28 20 72 63 20 7c 7c 20 4e 45 56 45 52 28  if( rc || NEVER(
27986 70 50 61 72 73 65 2d 3e 6e 45 72 72 29 20 7c 7c  pParse->nErr) ||
27987 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
27988 64 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  d ){.      goto 
27989 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
2798a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2798b 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2798c 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 72 65  P_Integer, 1, re
2798d 67 45 6f 66 29 3b 20 20 20 20 20 20 20 20 20 2f  gEof);         /
2798e 2a 20 45 4f 46 20 3c 2d 20 31 20 2a 2f 0a 20 20  * EOF <- 1 */.  
2798f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27990 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
27991 20 64 65 73 74 2e 69 50 61 72 6d 29 3b 20 20 20   dest.iParm);   
27992 2f 2a 20 79 69 65 6c 64 20 58 20 2a 2f 0a 20 20  /* yield X */.  
27993 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27994 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20  Op2(v, OP_Halt, 
27995 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 2c  SQLITE_INTERNAL,
27996 20 4f 45 5f 41 62 6f 72 74 29 3b 0a 20 20 20 20   OE_Abort);.    
27997 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
27998 22 45 6e 64 20 6f 66 20 53 45 4c 45 43 54 20 63  "End of SELECT c
27999 6f 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 20  oroutine"));.   
2799a 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
2799b 48 65 72 65 28 76 2c 20 6a 31 29 3b 20 20 20 20  Here(v, j1);    
2799c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2799d 20 20 20 20 20 20 2f 2a 20 6c 61 62 65 6c 20 42        /* label B
2799e 3a 20 2a 2f 0a 0a 20 20 20 20 72 65 67 46 72 6f  : */..    regFro
2799f 6d 53 65 6c 65 63 74 20 3d 20 64 65 73 74 2e 69  mSelect = dest.i
279a0 4d 65 6d 3b 0a 20 20 20 20 61 73 73 65 72 74 28  Mem;.    assert(
279a1 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74   pSelect->pEList
279a2 20 29 3b 0a 20 20 20 20 6e 43 6f 6c 75 6d 6e 20   );.    nColumn 
279a3 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73  = pSelect->pELis
279a4 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 61 73  t->nExpr;.    as
279a5 73 65 72 74 28 20 64 65 73 74 2e 6e 4d 65 6d 3d  sert( dest.nMem=
279a6 3d 6e 43 6f 6c 75 6d 6e 20 29 3b 0a 0a 20 20 20  =nColumn );..   
279a7 20 2f 2a 20 53 65 74 20 75 73 65 54 65 6d 70 54   /* Set useTempT
279a8 61 62 6c 65 20 74 6f 20 54 52 55 45 20 69 66 20  able to TRUE if 
279a9 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68  the result of th
279aa 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
279ab 6e 74 0a 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64  nt.    ** should
279ac 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f   be written into
279ad 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62   a temporary tab
279ae 6c 65 20 28 74 65 6d 70 6c 61 74 65 20 34 29 2e  le (template 4).
279af 20 20 53 65 74 20 74 6f 0a 20 20 20 20 2a 2a 20    Set to.    ** 
279b0 46 41 4c 53 45 20 69 66 20 65 61 63 68 2a 20 72  FALSE if each* r
279b1 6f 77 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  ow of the SELECT
279b2 20 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20   can be written 
279b3 64 69 72 65 63 74 6c 79 20 69 6e 74 6f 0a 20 20  directly into.  
279b4 20 20 2a 2a 20 74 68 65 20 64 65 73 74 69 6e 61    ** the destina
279b5 74 69 6f 6e 20 74 61 62 6c 65 20 28 74 65 6d 70  tion table (temp
279b6 6c 61 74 65 20 33 29 2e 0a 20 20 20 20 2a 2a 0a  late 3)..    **.
279b7 20 20 20 20 2a 2a 20 41 20 74 65 6d 70 20 74 61      ** A temp ta
279b8 62 6c 65 20 6d 75 73 74 20 62 65 20 75 73 65 64  ble must be used
279b9 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65   if the table be
279ba 69 6e 67 20 75 70 64 61 74 65 64 20 69 73 20 61  ing updated is a
279bb 6c 73 6f 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 6f  lso one.    ** o
279bc 66 20 74 68 65 20 74 61 62 6c 65 73 20 62 65 69  f the tables bei
279bd 6e 67 20 72 65 61 64 20 62 79 20 74 68 65 20 53  ng read by the S
279be 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
279bf 20 20 41 6c 73 6f 20 75 73 65 20 61 20 0a 20 20    Also use a .  
279c0 20 20 2a 2a 20 74 65 6d 70 20 74 61 62 6c 65 20    ** temp table 
279c1 69 6e 20 74 68 65 20 63 61 73 65 20 6f 66 20 72  in the case of r
279c2 6f 77 20 74 72 69 67 67 65 72 73 2e 0a 20 20 20  ow triggers..   
279c3 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54 72 69   */.    if( pTri
279c4 67 67 65 72 20 7c 7c 20 72 65 61 64 73 54 61 62  gger || readsTab
279c5 6c 65 28 70 50 61 72 73 65 2c 20 61 64 64 72 53  le(pParse, addrS
279c6 65 6c 65 63 74 2c 20 69 44 62 2c 20 70 54 61 62  elect, iDb, pTab
279c7 29 20 29 7b 0a 20 20 20 20 20 20 75 73 65 54 65  ) ){.      useTe
279c8 6d 70 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20  mpTable = 1;.   
279c9 20 7d 0a 0a 20 20 20 20 69 66 28 20 75 73 65 54   }..    if( useT
279ca 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  empTable ){.    
279cb 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20    /* Invoke the 
279cc 63 6f 72 6f 75 74 69 6e 65 20 74 6f 20 65 78 74  coroutine to ext
279cd 72 61 63 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ract information
279ce 20 66 72 6f 6d 20 74 68 65 20 53 45 4c 45 43 54   from the SELECT
279cf 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 61 64  .      ** and ad
279d0 64 20 69 74 20 74 6f 20 61 20 74 72 61 6e 73 69  d it to a transi
279d1 65 6e 74 20 74 61 62 6c 65 20 73 72 63 54 61 62  ent table srcTab
279d2 2e 20 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65  .  The code gene
279d3 72 61 74 65 64 0a 20 20 20 20 20 20 2a 2a 20 68  rated.      ** h
279d4 65 72 65 20 69 73 20 66 72 6f 6d 20 74 68 65 20  ere is from the 
279d5 34 74 68 20 74 65 6d 70 6c 61 74 65 3a 0a 20 20  4th template:.  
279d6 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
279d7 20 20 20 20 20 42 3a 20 6f 70 65 6e 20 74 65 6d       B: open tem
279d8 70 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a  p table.      **
279d9 20 20 20 20 20 20 4c 3a 20 79 69 65 6c 64 20 58        L: yield X
279da 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20  .      **       
279db 20 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 4d 0a    if EOF goto M.
279dc 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20        **        
279dd 20 69 6e 73 65 72 74 20 72 6f 77 20 66 72 6f 6d   insert row from
279de 20 52 2e 2e 52 2b 6e 20 69 6e 74 6f 20 74 65 6d   R..R+n into tem
279df 70 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a  p table.      **
279e0 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 4c 0a           goto L.
279e1 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 4d 3a        **      M:
279e2 20 2e 2e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20   ....      */.  
279e3 20 20 20 20 69 6e 74 20 72 65 67 52 65 63 3b 20      int regRec; 
279e4 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69           /* Regi
279e5 73 74 65 72 20 74 6f 20 68 6f 6c 64 20 70 61 63  ster to hold pac
279e6 6b 65 64 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  ked record */.  
279e7 20 20 20 20 69 6e 74 20 72 65 67 54 65 6d 70 52      int regTempR
279e8 6f 77 69 64 3b 20 20 20 20 2f 2a 20 52 65 67 69  owid;    /* Regi
279e9 73 74 65 72 20 74 6f 20 68 6f 6c 64 20 74 65 6d  ster to hold tem
279ea 70 20 74 61 62 6c 65 20 52 4f 57 49 44 20 2a 2f  p table ROWID */
279eb 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 54  .      int addrT
279ec 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  op;         /* L
279ed 61 62 65 6c 20 22 4c 22 20 2a 2f 0a 20 20 20 20  abel "L" */.    
279ee 20 20 69 6e 74 20 61 64 64 72 49 66 3b 20 20 20    int addrIf;   
279ef 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
279f0 73 20 6f 66 20 6a 75 6d 70 20 74 6f 20 4d 20 2a  s of jump to M *
279f1 2f 0a 0a 20 20 20 20 20 20 73 72 63 54 61 62 20  /..      srcTab 
279f2 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
279f3 3b 0a 20 20 20 20 20 20 72 65 67 52 65 63 20 3d  ;.      regRec =
279f4 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
279f5 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
279f6 20 20 72 65 67 54 65 6d 70 52 6f 77 69 64 20 3d    regTempRowid =
279f7 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
279f8 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
279f9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
279fa 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70  Op2(v, OP_OpenEp
279fb 68 65 6d 65 72 61 6c 2c 20 73 72 63 54 61 62 2c  hemeral, srcTab,
279fc 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20   nColumn);.     
279fd 20 61 64 64 72 54 6f 70 20 3d 20 73 71 6c 69 74   addrTop = sqlit
279fe 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
279ff 4f 50 5f 59 69 65 6c 64 2c 20 64 65 73 74 2e 69  OP_Yield, dest.i
27a00 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 61 64 64  Parm);.      add
27a01 72 49 66 20 3d 20 73 71 6c 69 74 65 33 56 64 62  rIf = sqlite3Vdb
27a02 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
27a03 2c 20 72 65 67 45 6f 66 29 3b 0a 20 20 20 20 20  , regEof);.     
27a04 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27a05 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  p3(v, OP_MakeRec
27a06 6f 72 64 2c 20 72 65 67 46 72 6f 6d 53 65 6c 65  ord, regFromSele
27a07 63 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 65 67  ct, nColumn, reg
27a08 52 65 63 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Rec);.      sqli
27a09 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27a0a 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 73 72   OP_NewRowid, sr
27a0b 63 54 61 62 2c 20 72 65 67 54 65 6d 70 52 6f 77  cTab, regTempRow
27a0c 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  id);.      sqlit
27a0d 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
27a0e 4f 50 5f 49 6e 73 65 72 74 2c 20 73 72 63 54 61  OP_Insert, srcTa
27a0f 62 2c 20 72 65 67 52 65 63 2c 20 72 65 67 54 65  b, regRec, regTe
27a10 6d 70 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20  mpRowid);.      
27a11 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27a12 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
27a13 20 61 64 64 72 54 6f 70 29 3b 0a 20 20 20 20 20   addrTop);.     
27a14 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
27a15 48 65 72 65 28 76 2c 20 61 64 64 72 49 66 29 3b  Here(v, addrIf);
27a16 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65  .      sqlite3Re
27a17 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
27a18 72 73 65 2c 20 72 65 67 52 65 63 29 3b 0a 20 20  rse, regRec);.  
27a19 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
27a1a 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
27a1b 2c 20 72 65 67 54 65 6d 70 52 6f 77 69 64 29 3b  , regTempRowid);
27a1c 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
27a1d 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74      /* This is t
27a1e 68 65 20 63 61 73 65 20 69 66 20 74 68 65 20 64  he case if the d
27a1f 61 74 61 20 66 6f 72 20 74 68 65 20 49 4e 53 45  ata for the INSE
27a20 52 54 20 69 73 20 63 6f 6d 69 6e 67 20 66 72 6f  RT is coming fro
27a21 6d 20 61 20 56 41 4c 55 45 53 0a 20 20 20 20 2a  m a VALUES.    *
27a22 2a 20 63 6c 61 75 73 65 0a 20 20 20 20 2a 2f 0a  * clause.    */.
27a23 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20      NameContext 
27a24 73 4e 43 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  sNC;.    memset(
27a25 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28  &sNC, 0, sizeof(
27a26 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70  sNC));.    sNC.p
27a27 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
27a28 20 20 20 20 73 72 63 54 61 62 20 3d 20 2d 31 3b      srcTab = -1;
27a29 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 73 65  .    assert( use
27a2a 54 65 6d 70 54 61 62 6c 65 3d 3d 30 20 29 3b 0a  TempTable==0 );.
27a2b 20 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c      nColumn = pL
27a2c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78  ist ? pList->nEx
27a2d 70 72 20 3a 20 30 3b 0a 20 20 20 20 66 6f 72 28  pr : 0;.    for(
27a2e 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20  i=0; i<nColumn; 
27a2f 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
27a30 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
27a31 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 4c  prNames(&sNC, pL
27a32 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29  ist->a[i].pExpr)
27a33 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
27a34 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b   insert_cleanup;
27a35 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
27a36 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75   }..  /* Make su
27a37 72 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  re the number of
27a38 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
27a39 73 6f 75 72 63 65 20 64 61 74 61 20 6d 61 74 63  source data matc
27a3a 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72 0a 20  hes the number. 
27a3b 20 2a 2a 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74   ** of columns t
27a3c 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e  o be inserted in
27a3d 74 6f 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20  to the table..  
27a3e 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75  */.  if( IsVirtu
27a3f 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
27a40 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d  for(i=0; i<pTab-
27a41 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
27a42 20 20 20 6e 48 69 64 64 65 6e 20 2b 3d 20 28 49     nHidden += (I
27a43 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 26 70  sHiddenColumn(&p
27a44 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 29 20 3f 20  Tab->aCol[i]) ? 
27a45 31 20 3a 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  1 : 0);.    }.  
27a46 7d 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d  }.  if( pColumn=
27a47 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 20 26 26  =0 && nColumn &&
27a48 20 6e 43 6f 6c 75 6d 6e 21 3d 28 70 54 61 62 2d   nColumn!=(pTab-
27a49 3e 6e 43 6f 6c 2d 6e 48 69 64 64 65 6e 29 20 29  >nCol-nHidden) )
27a4a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
27a4b 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20  orMsg(pParse, . 
27a4c 20 20 20 20 20 20 22 74 61 62 6c 65 20 25 53 20        "table %S 
27a4d 68 61 73 20 25 64 20 63 6f 6c 75 6d 6e 73 20 62  has %d columns b
27a4e 75 74 20 25 64 20 76 61 6c 75 65 73 20 77 65 72  ut %d values wer
27a4f 65 20 73 75 70 70 6c 69 65 64 22 2c 0a 20 20 20  e supplied",.   
27a50 20 20 20 20 70 54 61 62 4c 69 73 74 2c 20 30 2c      pTabList, 0,
27a51 20 70 54 61 62 2d 3e 6e 43 6f 6c 2d 6e 48 69 64   pTab->nCol-nHid
27a52 64 65 6e 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20  den, nColumn);. 
27a53 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63     goto insert_c
27a54 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66  leanup;.  }.  if
27a55 28 20 70 43 6f 6c 75 6d 6e 21 3d 30 20 26 26 20  ( pColumn!=0 && 
27a56 6e 43 6f 6c 75 6d 6e 21 3d 70 43 6f 6c 75 6d 6e  nColumn!=pColumn
27a57 2d 3e 6e 49 64 20 29 7b 0a 20 20 20 20 73 71 6c  ->nId ){.    sql
27a58 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
27a59 72 73 65 2c 20 22 25 64 20 76 61 6c 75 65 73 20  rse, "%d values 
27a5a 66 6f 72 20 25 64 20 63 6f 6c 75 6d 6e 73 22 2c  for %d columns",
27a5b 20 6e 43 6f 6c 75 6d 6e 2c 20 70 43 6f 6c 75 6d   nColumn, pColum
27a5c 6e 2d 3e 6e 49 64 29 3b 0a 20 20 20 20 67 6f 74  n->nId);.    got
27a5d 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70  o insert_cleanup
27a5e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  ;.  }..  /* If t
27a5f 68 65 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d  he INSERT statem
27a60 65 6e 74 20 69 6e 63 6c 75 64 65 64 20 61 6e 20  ent included an 
27a61 49 44 4c 49 53 54 20 74 65 72 6d 2c 20 74 68 65  IDLIST term, the
27a62 6e 20 6d 61 6b 65 20 73 75 72 65 0a 20 20 2a 2a  n make sure.  **
27a63 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66   all elements of
27a64 20 74 68 65 20 49 44 4c 49 53 54 20 72 65 61 6c   the IDLIST real
27a65 6c 79 20 61 72 65 20 63 6f 6c 75 6d 6e 73 20 6f  ly are columns o
27a66 66 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20  f the table and 
27a67 0a 20 20 2a 2a 20 72 65 6d 65 6d 62 65 72 20 74  .  ** remember t
27a68 68 65 20 63 6f 6c 75 6d 6e 20 69 6e 64 69 63 65  he column indice
27a69 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20  s..  **.  ** If 
27a6a 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e  the table has an
27a6b 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
27a6c 20 4b 45 59 20 63 6f 6c 75 6d 6e 20 61 6e 64 20   KEY column and 
27a6d 74 68 61 74 20 63 6f 6c 75 6d 6e 0a 20 20 2a 2a  that column.  **
27a6e 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 74 68 65   is named in the
27a6f 20 49 44 4c 49 53 54 2c 20 74 68 65 6e 20 72 65   IDLIST, then re
27a70 63 6f 72 64 20 69 6e 20 74 68 65 20 6b 65 79 43  cord in the keyC
27a71 6f 6c 75 6d 6e 20 76 61 72 69 61 62 6c 65 0a 20  olumn variable. 
27a72 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 69 6e   ** the index in
27a73 74 6f 20 49 44 4c 49 53 54 20 6f 66 20 74 68 65  to IDLIST of the
27a74 20 70 72 69 6d 61 72 79 20 6b 65 79 20 63 6f 6c   primary key col
27a75 75 6d 6e 2e 20 20 6b 65 79 43 6f 6c 75 6d 6e 20  umn.  keyColumn 
27a76 69 73 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65  is.  ** the inde
27a77 78 20 6f 66 20 74 68 65 20 70 72 69 6d 61 72 79  x of the primary
27a78 20 6b 65 79 20 61 73 20 69 74 20 61 70 70 65 61   key as it appea
27a79 72 73 20 69 6e 20 49 44 4c 49 53 54 2c 20 6e 6f  rs in IDLIST, no
27a7a 74 20 61 73 0a 20 20 2a 2a 20 69 73 20 61 70 70  t as.  ** is app
27a7b 65 61 72 73 20 69 6e 20 74 68 65 20 6f 72 69 67  ears in the orig
27a7c 69 6e 61 6c 20 74 61 62 6c 65 2e 20 20 28 54 68  inal table.  (Th
27a7d 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 70  e index of the p
27a7e 72 69 6d 61 72 79 0a 20 20 2a 2a 20 6b 65 79 20  rimary.  ** key 
27a7f 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  in the original 
27a80 74 61 62 6c 65 20 69 73 20 70 54 61 62 2d 3e 69  table is pTab->i
27a81 50 4b 65 79 2e 29 0a 20 20 2a 2f 0a 20 20 69 66  PKey.).  */.  if
27a82 28 20 70 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ( pColumn ){.   
27a83 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 6f 6c   for(i=0; i<pCol
27a84 75 6d 6e 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a  umn->nId; i++){.
27a85 20 20 20 20 20 20 70 43 6f 6c 75 6d 6e 2d 3e 61        pColumn->a
27a86 5b 69 5d 2e 69 64 78 20 3d 20 2d 31 3b 0a 20 20  [i].idx = -1;.  
27a87 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b    }.    for(i=0;
27a88 20 69 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b   i<pColumn->nId;
27a89 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6f 72   i++){.      for
27a8a 28 6a 3d 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43  (j=0; j<pTab->nC
27a8b 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; j++){.      
27a8c 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
27a8d 49 43 6d 70 28 70 43 6f 6c 75 6d 6e 2d 3e 61 5b  ICmp(pColumn->a[
27a8e 69 5d 2e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e  i].zName, pTab->
27a8f 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 29 3d 3d  aCol[j].zName)==
27a90 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  0 ){.          p
27a91 43 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 69 64 78  Column->a[i].idx
27a92 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 20 20   = j;.          
27a93 69 66 28 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b  if( j==pTab->iPK
27a94 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ey ){.          
27a95 20 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 69 3b    keyColumn = i;
27a96 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
27a97 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
27a98 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
27a99 20 20 20 20 20 20 69 66 28 20 6a 3e 3d 70 54 61        if( j>=pTa
27a9a 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 20  b->nCol ){.     
27a9b 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 73     if( sqlite3Is
27a9c 52 6f 77 69 64 28 70 43 6f 6c 75 6d 6e 2d 3e 61  Rowid(pColumn->a
27a9d 5b 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a 20 20  [i].zName) ){.  
27a9e 20 20 20 20 20 20 20 20 6b 65 79 43 6f 6c 75 6d          keyColum
27a9f 6e 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20 7d  n = i;.        }
27aa0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
27aa1 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
27aa2 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25  pParse, "table %
27aa3 53 20 68 61 73 20 6e 6f 20 63 6f 6c 75 6d 6e 20  S has no column 
27aa4 6e 61 6d 65 64 20 25 73 22 2c 0a 20 20 20 20 20  named %s",.     
27aa5 20 20 20 20 20 20 20 20 20 70 54 61 62 4c 69 73           pTabLis
27aa6 74 2c 20 30 2c 20 70 43 6f 6c 75 6d 6e 2d 3e 61  t, 0, pColumn->a
27aa7 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  [i].zName);.    
27aa8 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 45        pParse->nE
27aa9 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  rr++;.          
27aaa 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61  goto insert_clea
27aab 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  nup;.        }. 
27aac 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
27aad 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
27aae 69 73 20 6e 6f 20 49 44 4c 49 53 54 20 74 65 72  is no IDLIST ter
27aaf 6d 20 62 75 74 20 74 68 65 20 74 61 62 6c 65 20  m but the table 
27ab0 68 61 73 20 61 6e 20 69 6e 74 65 67 65 72 20 70  has an integer p
27ab1 72 69 6d 61 72 79 0a 20 20 2a 2a 20 6b 65 79 2c  rimary.  ** key,
27ab2 20 74 68 65 20 73 65 74 20 74 68 65 20 6b 65 79   the set the key
27ab3 43 6f 6c 75 6d 6e 20 76 61 72 69 61 62 6c 65 20  Column variable 
27ab4 74 6f 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  to the primary k
27ab5 65 79 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 0a  ey column index.
27ab6 20 20 2a 2a 20 69 6e 20 74 68 65 20 6f 72 69 67    ** in the orig
27ab7 69 6e 61 6c 20 74 61 62 6c 65 20 64 65 66 69 6e  inal table defin
27ab8 69 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66  ition..  */.  if
27ab9 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20  ( pColumn==0 && 
27aba 6e 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20  nColumn>0 ){.   
27abb 20 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 70 54 61   keyColumn = pTa
27abc 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 7d 0a 20 20  b->iPKey;.  }.  
27abd 20 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69    .  /* Initiali
27abe 7a 65 20 74 68 65 20 63 6f 75 6e 74 20 6f 66 20  ze the count of 
27abf 72 6f 77 73 20 74 6f 20 62 65 20 69 6e 73 65 72  rows to be inser
27ac0 74 65 64 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  ted.  */.  if( d
27ac1 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54  b->flags & SQLIT
27ac2 45 5f 43 6f 75 6e 74 52 6f 77 73 20 29 7b 0a 20  E_CountRows ){. 
27ac3 20 20 20 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d     regRowCount =
27ac4 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
27ac5 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27ac6 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
27ac7 65 67 65 72 2c 20 30 2c 20 72 65 67 52 6f 77 43  eger, 0, regRowC
27ac8 6f 75 6e 74 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ount);.  }..  /*
27ac9 20 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20   If this is not 
27aca 61 20 76 69 65 77 2c 20 6f 70 65 6e 20 74 68 65  a view, open the
27acb 20 74 61 62 6c 65 20 61 6e 64 20 61 6e 64 20 61   table and and a
27acc 6c 6c 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20  ll indices */.  
27acd 69 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20  if( !isView ){. 
27ace 20 20 20 69 6e 74 20 6e 49 64 78 3b 0a 0a 20 20     int nIdx;..  
27acf 20 20 62 61 73 65 43 75 72 20 3d 20 70 50 61 72    baseCur = pPar
27ad0 73 65 2d 3e 6e 54 61 62 3b 0a 20 20 20 20 6e 49  se->nTab;.    nI
27ad1 64 78 20 3d 20 73 71 6c 69 74 65 33 4f 70 65 6e  dx = sqlite3Open
27ad2 54 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73 28  TableAndIndices(
27ad3 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 62 61  pParse, pTab, ba
27ad4 73 65 43 75 72 2c 20 4f 50 5f 4f 70 65 6e 57 72  seCur, OP_OpenWr
27ad5 69 74 65 29 3b 0a 20 20 20 20 61 52 65 67 49 64  ite);.    aRegId
27ad6 78 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  x = sqlite3DbMal
27ad7 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
27ad8 66 28 69 6e 74 29 2a 28 6e 49 64 78 2b 31 29 29  f(int)*(nIdx+1))
27ad9 3b 0a 20 20 20 20 69 66 28 20 61 52 65 67 49 64  ;.    if( aRegId
27ada 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 67 6f  x==0 ){.      go
27adb 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75  to insert_cleanu
27adc 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  p;.    }.    for
27add 28 69 3d 30 3b 20 69 3c 6e 49 64 78 3b 20 69 2b  (i=0; i<nIdx; i+
27ade 2b 29 7b 0a 20 20 20 20 20 20 61 52 65 67 49 64  +){.      aRegId
27adf 78 5b 69 5d 20 3d 20 2b 2b 70 50 61 72 73 65 2d  x[i] = ++pParse-
27ae0 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d  >nMem;.    }.  }
27ae1 0a 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74  ..  /* This is t
27ae2 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 6d 61  he top of the ma
27ae3 69 6e 20 69 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f  in insertion loo
27ae4 70 20 2a 2f 0a 20 20 69 66 28 20 75 73 65 54 65  p */.  if( useTe
27ae5 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f  mpTable ){.    /
27ae6 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 64  * This block cod
27ae7 65 73 20 74 68 65 20 74 6f 70 20 6f 66 20 6c 6f  es the top of lo
27ae8 6f 70 20 6f 6e 6c 79 2e 20 20 54 68 65 20 63 6f  op only.  The co
27ae9 6d 70 6c 65 74 65 20 6c 6f 6f 70 20 69 73 20 74  mplete loop is t
27aea 68 65 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77  he.    ** follow
27aeb 69 6e 67 20 70 73 65 75 64 6f 63 6f 64 65 20 28  ing pseudocode (
27aec 74 65 6d 70 6c 61 74 65 20 34 29 3a 0a 20 20 20  template 4):.   
27aed 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   **.    **      
27aee 20 20 20 72 65 77 69 6e 64 20 74 65 6d 70 20 74     rewind temp t
27aef 61 62 6c 65 0a 20 20 20 20 2a 2a 20 20 20 20 20  able.    **     
27af0 20 43 3a 20 6c 6f 6f 70 20 6f 76 65 72 20 72 6f   C: loop over ro
27af1 77 73 20 6f 66 20 69 6e 74 65 72 6d 65 64 69 61  ws of intermedia
27af2 74 65 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20  te table.    ** 
27af3 20 20 20 20 20 20 20 20 20 20 74 72 61 6e 73 66            transf
27af4 65 72 20 76 61 6c 75 65 73 20 66 6f 72 6d 20 69  er values form i
27af5 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c  ntermediate tabl
27af6 65 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 20  e into <table>. 
27af7 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 65 6e     **         en
27af8 64 20 6c 6f 6f 70 0a 20 20 20 20 2a 2a 20 20 20  d loop.    **   
27af9 20 20 20 44 3a 20 2e 2e 2e 0a 20 20 20 20 2a 2f     D: ....    */
27afa 0a 20 20 20 20 61 64 64 72 49 6e 73 54 6f 70 20  .    addrInsTop 
27afb 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
27afc 4f 70 31 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64  Op1(v, OP_Rewind
27afd 2c 20 73 72 63 54 61 62 29 3b 0a 20 20 20 20 61  , srcTab);.    a
27afe 64 64 72 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65  ddrCont = sqlite
27aff 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
27b00 28 76 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  (v);.  }else if(
27b01 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20   pSelect ){.    
27b02 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f  /* This block co
27b03 64 65 73 20 74 68 65 20 74 6f 70 20 6f 66 20 6c  des the top of l
27b04 6f 6f 70 20 6f 6e 6c 79 2e 20 20 54 68 65 20 63  oop only.  The c
27b05 6f 6d 70 6c 65 74 65 20 6c 6f 6f 70 20 69 73 20  omplete loop is 
27b06 74 68 65 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f  the.    ** follo
27b07 77 69 6e 67 20 70 73 65 75 64 6f 63 6f 64 65 20  wing pseudocode 
27b08 28 74 65 6d 70 6c 61 74 65 20 33 29 3a 0a 20 20  (template 3):.  
27b09 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20    **.    **     
27b0a 20 43 3a 20 79 69 65 6c 64 20 58 0a 20 20 20 20   C: yield X.    
27b0b 2a 2a 20 20 20 20 20 20 20 20 20 69 66 20 45 4f  **         if EO
27b0c 46 20 67 6f 74 6f 20 44 0a 20 20 20 20 2a 2a 20  F goto D.    ** 
27b0d 20 20 20 20 20 20 20 20 69 6e 73 65 72 74 20 74          insert t
27b0e 68 65 20 73 65 6c 65 63 74 20 72 65 73 75 6c 74  he select result
27b0f 20 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 20 66 72   into <table> fr
27b10 6f 6d 20 52 2e 2e 52 2b 6e 0a 20 20 20 20 2a 2a  om R..R+n.    **
27b11 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 43 0a           goto C.
27b12 20 20 20 20 2a 2a 20 20 20 20 20 20 44 3a 20 2e      **      D: .
27b13 2e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 64  ...    */.    ad
27b14 64 72 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33  drCont = sqlite3
27b15 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
27b16 5f 59 69 65 6c 64 2c 20 64 65 73 74 2e 69 50 61  _Yield, dest.iPa
27b17 72 6d 29 3b 0a 20 20 20 20 61 64 64 72 49 6e 73  rm);.    addrIns
27b18 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62  Top = sqlite3Vdb
27b19 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
27b1a 2c 20 72 65 67 45 6f 66 29 3b 0a 20 20 7d 0a 0a  , regEof);.  }..
27b1b 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 72 65    /* Allocate re
27b1c 67 69 73 74 65 72 73 20 66 6f 72 20 68 6f 6c 64  gisters for hold
27b1d 69 6e 67 20 74 68 65 20 72 6f 77 69 64 20 6f 66  ing the rowid of
27b1e 20 74 68 65 20 6e 65 77 20 72 6f 77 2c 0a 20 20   the new row,.  
27b1f 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ** the content o
27b20 66 20 74 68 65 20 6e 65 77 20 72 6f 77 2c 20 61  f the new row, a
27b21 6e 64 20 74 68 65 20 61 73 73 65 6d 62 6c 69 65  nd the assemblie
27b22 64 20 72 6f 77 20 72 65 63 6f 72 64 2e 0a 20 20  d row record..  
27b23 2a 2f 0a 20 20 72 65 67 52 65 63 6f 72 64 20 3d  */.  regRecord =
27b24 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
27b25 0a 20 20 72 65 67 52 6f 77 69 64 20 3d 20 72 65  .  regRowid = re
27b26 67 49 6e 73 20 3d 20 70 50 61 72 73 65 2d 3e 6e  gIns = pParse->n
27b27 4d 65 6d 2b 31 3b 0a 20 20 70 50 61 72 73 65 2d  Mem+1;.  pParse-
27b28 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e  >nMem += pTab->n
27b29 43 6f 6c 20 2b 20 31 3b 0a 20 20 69 66 28 20 49  Col + 1;.  if( I
27b2a 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
27b2b 7b 0a 20 20 20 20 72 65 67 52 6f 77 69 64 2b 2b  {.    regRowid++
27b2c 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d  ;.    pParse->nM
27b2d 65 6d 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 67 44  em++;.  }.  regD
27b2e 61 74 61 20 3d 20 72 65 67 52 6f 77 69 64 2b 31  ata = regRowid+1
27b2f 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20 74 68 65 20  ;..  /* Run the 
27b30 42 45 46 4f 52 45 20 61 6e 64 20 49 4e 53 54 45  BEFORE and INSTE
27b31 41 44 20 4f 46 20 74 72 69 67 67 65 72 73 2c 20  AD OF triggers, 
27b32 69 66 20 74 68 65 72 65 20 61 72 65 20 61 6e 79  if there are any
27b33 0a 20 20 2a 2f 0a 20 20 65 6e 64 4f 66 4c 6f 6f  .  */.  endOfLoo
27b34 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  p = sqlite3VdbeM
27b35 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 69  akeLabel(v);.  i
27b36 66 28 20 74 6d 61 73 6b 20 26 20 54 52 49 47 47  f( tmask & TRIGG
27b37 45 52 5f 42 45 46 4f 52 45 20 29 7b 0a 20 20 20  ER_BEFORE ){.   
27b38 20 69 6e 74 20 72 65 67 43 6f 6c 73 20 3d 20 73   int regCols = s
27b39 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e  qlite3GetTempRan
27b3a 67 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d  ge(pParse, pTab-
27b3b 3e 6e 43 6f 6c 2b 31 29 3b 0a 0a 20 20 20 20 2f  >nCol+1);..    /
27b3c 2a 20 62 75 69 6c 64 20 74 68 65 20 4e 45 57 2e  * build the NEW.
27b3d 2a 20 72 65 66 65 72 65 6e 63 65 20 72 6f 77 2e  * reference row.
27b3e 20 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 74    Note that if t
27b3f 68 65 72 65 20 69 73 20 61 6e 20 49 4e 54 45 47  here is an INTEG
27b40 45 52 0a 20 20 20 20 2a 2a 20 50 52 49 4d 41 52  ER.    ** PRIMAR
27b41 59 20 4b 45 59 20 69 6e 74 6f 20 77 68 69 63 68  Y KEY into which
27b42 20 61 20 4e 55 4c 4c 20 69 73 20 62 65 69 6e 67   a NULL is being
27b43 20 69 6e 73 65 72 74 65 64 2c 20 74 68 61 74 20   inserted, that 
27b44 4e 55 4c 4c 20 77 69 6c 6c 20 62 65 0a 20 20 20  NULL will be.   
27b45 20 2a 2a 20 74 72 61 6e 73 6c 61 74 65 64 20 69   ** translated i
27b46 6e 74 6f 20 61 20 75 6e 69 71 75 65 20 49 44 20  nto a unique ID 
27b47 66 6f 72 20 74 68 65 20 72 6f 77 2e 20 20 42 75  for the row.  Bu
27b48 74 20 6f 6e 20 61 20 42 45 46 4f 52 45 20 74 72  t on a BEFORE tr
27b49 69 67 67 65 72 2c 0a 20 20 20 20 2a 2a 20 77 65  igger,.    ** we
27b4a 20 64 6f 20 6e 6f 74 20 6b 6e 6f 77 20 77 68 61   do not know wha
27b4b 74 20 74 68 65 20 75 6e 69 71 75 65 20 49 44 20  t the unique ID 
27b4c 77 69 6c 6c 20 62 65 20 28 62 65 63 61 75 73 65  will be (because
27b4d 20 74 68 65 20 69 6e 73 65 72 74 20 68 61 73 0a   the insert has.
27b4e 20 20 20 20 2a 2a 20 6e 6f 74 20 68 61 70 70 65      ** not happe
27b4f 6e 65 64 20 79 65 74 29 20 73 6f 20 77 65 20 73  ned yet) so we s
27b50 75 62 73 74 69 74 75 74 65 20 61 20 72 6f 77 69  ubstitute a rowi
27b51 64 20 6f 66 20 2d 31 0a 20 20 20 20 2a 2f 0a 20  d of -1.    */. 
27b52 20 20 20 69 66 28 20 6b 65 79 43 6f 6c 75 6d 6e     if( keyColumn
27b53 3c 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  <0 ){.      sqli
27b54 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27b55 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c   OP_Integer, -1,
27b56 20 72 65 67 43 6f 6c 73 29 3b 0a 20 20 20 20 7d   regCols);.    }
27b57 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20  else{.      int 
27b58 6a 31 3b 0a 20 20 20 20 20 20 69 66 28 20 75 73  j1;.      if( us
27b59 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20  eTempTable ){.  
27b5a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
27b5b 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
27b5c 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6b 65  lumn, srcTab, ke
27b5d 79 43 6f 6c 75 6d 6e 2c 20 72 65 67 43 6f 6c 73  yColumn, regCols
27b5e 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
27b5f 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
27b60 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 20 20 2f  pSelect==0 );  /
27b61 2a 20 4f 74 68 65 72 77 69 73 65 20 75 73 65 54  * Otherwise useT
27b62 65 6d 70 54 61 62 6c 65 20 69 73 20 74 72 75 65  empTable is true
27b63 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69   */.        sqli
27b64 74 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72  te3ExprCode(pPar
27b65 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6b 65 79  se, pList->a[key
27b66 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72 2c 20 72  Column].pExpr, r
27b67 65 67 43 6f 6c 73 29 3b 0a 20 20 20 20 20 20 7d  egCols);.      }
27b68 0a 20 20 20 20 20 20 6a 31 20 3d 20 73 71 6c 69  .      j1 = sqli
27b69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
27b6a 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67   OP_NotNull, reg
27b6b 43 6f 6c 73 29 3b 0a 20 20 20 20 20 20 73 71 6c  Cols);.      sql
27b6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
27b6d 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31  , OP_Integer, -1
27b6e 2c 20 72 65 67 43 6f 6c 73 29 3b 0a 20 20 20 20  , regCols);.    
27b6f 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
27b70 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20  pHere(v, j1);.  
27b71 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27b72 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74  ddOp1(v, OP_Must
27b73 42 65 49 6e 74 2c 20 72 65 67 43 6f 6c 73 29 3b  BeInt, regCols);
27b74 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43  .    }..    /* C
27b75 61 6e 6e 6f 74 20 68 61 76 65 20 74 72 69 67 67  annot have trigg
27b76 65 72 73 20 6f 6e 20 61 20 76 69 72 74 75 61 6c  ers on a virtual
27b77 20 74 61 62 6c 65 2e 20 49 66 20 69 74 20 77 65   table. If it we
27b78 72 65 20 70 6f 73 73 69 62 6c 65 2c 0a 20 20 20  re possible,.   
27b79 20 2a 2a 20 74 68 69 73 20 62 6c 6f 63 6b 20 77   ** this block w
27b7a 6f 75 6c 64 20 68 61 76 65 20 74 6f 20 61 63 63  ould have to acc
27b7b 6f 75 6e 74 20 66 6f 72 20 68 69 64 64 65 6e 20  ount for hidden 
27b7c 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20  column..    */. 
27b7d 20 20 20 61 73 73 65 72 74 28 20 21 49 73 56 69     assert( !IsVi
27b7e 72 74 75 61 6c 28 70 54 61 62 29 20 29 3b 0a 0a  rtual(pTab) );..
27b7f 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68      /* Create th
27b80 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 64 61 74  e new column dat
27b81 61 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  a.    */.    for
27b82 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43  (i=0; i<pTab->nC
27b83 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
27b84 69 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 29  if( pColumn==0 )
27b85 7b 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 69 3b  {.        j = i;
27b86 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
27b87 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
27b88 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 6a  <pColumn->nId; j
27b89 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
27b8a 66 28 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d  f( pColumn->a[j]
27b8b 2e 69 64 78 3d 3d 69 20 29 20 62 72 65 61 6b 3b  .idx==i ) break;
27b8c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
27b8d 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f   }.      if( pCo
27b8e 6c 75 6d 6e 20 26 26 20 6a 3e 3d 70 43 6f 6c 75  lumn && j>=pColu
27b8f 6d 6e 2d 3e 6e 49 64 20 29 7b 0a 20 20 20 20 20  mn->nId ){.     
27b90 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
27b91 64 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2d  de(pParse, pTab-
27b92 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c 20  >aCol[i].pDflt, 
27b93 72 65 67 43 6f 6c 73 2b 69 2b 31 29 3b 0a 20 20  regCols+i+1);.  
27b94 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 73      }else if( us
27b95 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20  eTempTable ){.  
27b96 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
27b97 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
27b98 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6a 2c  lumn, srcTab, j,
27b99 20 72 65 67 43 6f 6c 73 2b 69 2b 31 29 3b 20 0a   regCols+i+1); .
27b9a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
27b9b 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65       assert( pSe
27b9c 6c 65 63 74 3d 3d 30 20 29 3b 20 2f 2a 20 4f 74  lect==0 ); /* Ot
27b9d 68 65 72 77 69 73 65 20 75 73 65 54 65 6d 70 54  herwise useTempT
27b9e 61 62 6c 65 20 69 73 20 74 72 75 65 20 2a 2f 0a  able is true */.
27b9f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
27ba0 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28  xprCodeAndCache(
27ba1 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61  pParse, pList->a
27ba2 5b 6a 5d 2e 70 45 78 70 72 2c 20 72 65 67 43 6f  [j].pExpr, regCo
27ba3 6c 73 2b 69 2b 31 29 3b 0a 20 20 20 20 20 20 7d  ls+i+1);.      }
27ba4 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49  .    }..    /* I
27ba5 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53  f this is an INS
27ba6 45 52 54 20 6f 6e 20 61 20 76 69 65 77 20 77 69  ERT on a view wi
27ba7 74 68 20 61 6e 20 49 4e 53 54 45 41 44 20 4f 46  th an INSTEAD OF
27ba8 20 49 4e 53 45 52 54 20 74 72 69 67 67 65 72 2c   INSERT trigger,
27ba9 0a 20 20 20 20 2a 2a 20 64 6f 20 6e 6f 74 20 61  .    ** do not a
27baa 74 74 65 6d 70 74 20 61 6e 79 20 63 6f 6e 76 65  ttempt any conve
27bab 72 73 69 6f 6e 73 20 62 65 66 6f 72 65 20 61 73  rsions before as
27bac 73 65 6d 62 6c 69 6e 67 20 74 68 65 20 72 65 63  sembling the rec
27bad 6f 72 64 2e 0a 20 20 20 20 2a 2a 20 49 66 20 74  ord..    ** If t
27bae 68 69 73 20 69 73 20 61 20 72 65 61 6c 20 74 61  his is a real ta
27baf 62 6c 65 2c 20 61 74 74 65 6d 70 74 20 63 6f 6e  ble, attempt con
27bb0 76 65 72 73 69 6f 6e 73 20 61 73 20 72 65 71 75  versions as requ
27bb1 69 72 65 64 20 62 79 20 74 68 65 0a 20 20 20 20  ired by the.    
27bb2 2a 2a 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20  ** table column 
27bb3 61 66 66 69 6e 69 74 69 65 73 2e 0a 20 20 20 20  affinities..    
27bb4 2a 2f 0a 20 20 20 20 69 66 28 20 21 69 73 56 69  */.    if( !isVi
27bb5 65 77 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ew ){.      sqli
27bb6 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27bb7 20 4f 50 5f 41 66 66 69 6e 69 74 79 2c 20 72 65   OP_Affinity, re
27bb8 67 43 6f 6c 73 2b 31 2c 20 70 54 61 62 2d 3e 6e  gCols+1, pTab->n
27bb9 43 6f 6c 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Col);.      sqli
27bba 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74 79  te3TableAffinity
27bbb 53 74 72 28 76 2c 20 70 54 61 62 29 3b 0a 20 20  Str(v, pTab);.  
27bbc 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 72 65    }..    /* Fire
27bbd 20 42 45 46 4f 52 45 20 6f 72 20 49 4e 53 54 45   BEFORE or INSTE
27bbe 41 44 20 4f 46 20 74 72 69 67 67 65 72 73 20 2a  AD OF triggers *
27bbf 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64  /.    sqlite3Cod
27bc0 65 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 72  eRowTrigger(pPar
27bc1 73 65 2c 20 70 54 72 69 67 67 65 72 2c 20 54 4b  se, pTrigger, TK
27bc2 5f 49 4e 53 45 52 54 2c 20 30 2c 20 54 52 49 47  _INSERT, 0, TRIG
27bc3 47 45 52 5f 42 45 46 4f 52 45 2c 20 0a 20 20 20  GER_BEFORE, .   
27bc4 20 20 20 20 20 70 54 61 62 2c 20 72 65 67 43 6f       pTab, regCo
27bc5 6c 73 2d 70 54 61 62 2d 3e 6e 43 6f 6c 2d 31 2c  ls-pTab->nCol-1,
27bc6 20 6f 6e 45 72 72 6f 72 2c 20 65 6e 64 4f 66 4c   onError, endOfL
27bc7 6f 6f 70 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74  oop);..    sqlit
27bc8 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e  e3ReleaseTempRan
27bc9 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 43 6f  ge(pParse, regCo
27bca 6c 73 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31  ls, pTab->nCol+1
27bcb 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50 75 73  );.  }..  /* Pus
27bcc 68 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d  h the record num
27bcd 62 65 72 20 66 6f 72 20 74 68 65 20 6e 65 77 20  ber for the new 
27bce 65 6e 74 72 79 20 6f 6e 74 6f 20 74 68 65 20 73  entry onto the s
27bcf 74 61 63 6b 2e 20 20 54 68 65 0a 20 20 2a 2a 20  tack.  The.  ** 
27bd0 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73  record number is
27bd1 20 61 20 72 61 6e 64 6f 6d 6c 79 20 67 65 6e 65   a randomly gene
27bd2 72 61 74 65 20 69 6e 74 65 67 65 72 20 63 72 65  rate integer cre
27bd3 61 74 65 64 20 62 79 20 4e 65 77 52 6f 77 69 64  ated by NewRowid
27bd4 0a 20 20 2a 2a 20 65 78 63 65 70 74 20 77 68 65  .  ** except whe
27bd5 6e 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20  n the table has 
27bd6 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  an INTEGER PRIMA
27bd7 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 2c 20 69  RY KEY column, i
27bd8 6e 20 77 68 69 63 68 0a 20 20 2a 2a 20 63 61 73  n which.  ** cas
27bd9 65 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d  e the record num
27bda 62 65 72 20 69 73 20 74 68 65 20 73 61 6d 65 20  ber is the same 
27bdb 61 73 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 20  as that column. 
27bdc 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 56  .  */.  if( !isV
27bdd 69 65 77 20 29 7b 0a 20 20 20 20 69 66 28 20 49  iew ){.    if( I
27bde 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
27bdf 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72  {.      /* The r
27be0 6f 77 20 74 68 61 74 20 74 68 65 20 56 55 70 64  ow that the VUpd
27be1 61 74 65 20 6f 70 63 6f 64 65 20 77 69 6c 6c 20  ate opcode will 
27be2 64 65 6c 65 74 65 3a 20 6e 6f 6e 65 20 2a 2f 0a  delete: none */.
27be3 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
27be4 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
27be5 6c 6c 2c 20 30 2c 20 72 65 67 49 6e 73 29 3b 0a  ll, 0, regIns);.
27be6 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6b 65      }.    if( ke
27be7 79 43 6f 6c 75 6d 6e 3e 3d 30 20 29 7b 0a 20 20  yColumn>=0 ){.  
27be8 20 20 20 20 69 66 28 20 75 73 65 54 65 6d 70 54      if( useTempT
27be9 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20  able ){.        
27bea 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27beb 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
27bec 73 72 63 54 61 62 2c 20 6b 65 79 43 6f 6c 75 6d  srcTab, keyColum
27bed 6e 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  n, regRowid);.  
27bee 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 53      }else if( pS
27bef 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20  elect ){.       
27bf0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27bf1 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20  p2(v, OP_SCopy, 
27bf2 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 2b 6b 65  regFromSelect+ke
27bf3 79 43 6f 6c 75 6d 6e 2c 20 72 65 67 52 6f 77 69  yColumn, regRowi
27bf4 64 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  d);.      }else{
27bf5 0a 20 20 20 20 20 20 20 20 56 64 62 65 4f 70 20  .        VdbeOp 
27bf6 2a 70 4f 70 3b 0a 20 20 20 20 20 20 20 20 73 71  *pOp;.        sq
27bf7 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
27bf8 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b 6b  arse, pList->a[k
27bf9 65 79 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72 2c  eyColumn].pExpr,
27bfa 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
27bfb 20 20 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 65      pOp = sqlite
27bfc 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 2d 31  3VdbeGetOp(v, -1
27bfd 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 41  );.        if( A
27bfe 4c 57 41 59 53 28 70 4f 70 29 20 26 26 20 70 4f  LWAYS(pOp) && pO
27bff 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e 75  p->opcode==OP_Nu
27c00 6c 6c 20 26 26 20 21 49 73 56 69 72 74 75 61 6c  ll && !IsVirtual
27c01 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
27c02 20 20 20 20 61 70 70 65 6e 64 46 6c 61 67 20 3d      appendFlag =
27c03 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f   1;.          pO
27c04 70 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4e  p->opcode = OP_N
27c05 65 77 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 20  ewRowid;.       
27c06 20 20 20 70 4f 70 2d 3e 70 31 20 3d 20 62 61 73     pOp->p1 = bas
27c07 65 43 75 72 3b 0a 20 20 20 20 20 20 20 20 20 20  eCur;.          
27c08 70 4f 70 2d 3e 70 32 20 3d 20 72 65 67 52 6f 77  pOp->p2 = regRow
27c09 69 64 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f  id;.          pO
27c0a 70 2d 3e 70 33 20 3d 20 72 65 67 41 75 74 6f 69  p->p3 = regAutoi
27c0b 6e 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  nc;.        }.  
27c0c 20 20 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49      }.      /* I
27c0d 66 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45  f the PRIMARY KE
27c0e 59 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  Y expression is 
27c0f 4e 55 4c 4c 2c 20 74 68 65 6e 20 75 73 65 20 4f  NULL, then use O
27c10 50 5f 4e 65 77 52 6f 77 69 64 0a 20 20 20 20 20  P_NewRowid.     
27c11 20 2a 2a 20 74 6f 20 67 65 6e 65 72 61 74 65 20   ** to generate 
27c12 61 20 75 6e 69 71 75 65 20 70 72 69 6d 61 72 79  a unique primary
27c13 20 6b 65 79 20 76 61 6c 75 65 2e 0a 20 20 20 20   key value..    
27c14 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 21    */.      if( !
27c15 61 70 70 65 6e 64 46 6c 61 67 20 29 7b 0a 20 20  appendFlag ){.  
27c16 20 20 20 20 20 20 69 6e 74 20 6a 31 3b 0a 20 20        int j1;.  
27c17 20 20 20 20 20 20 69 66 28 20 21 49 73 56 69 72        if( !IsVir
27c18 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
27c19 20 20 20 20 20 20 20 20 6a 31 20 3d 20 73 71 6c          j1 = sql
27c1a 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
27c1b 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65  , OP_NotNull, re
27c1c 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 20  gRowid);.       
27c1d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27c1e 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 77 52 6f  dOp3(v, OP_NewRo
27c1f 77 69 64 2c 20 62 61 73 65 43 75 72 2c 20 72 65  wid, baseCur, re
27c20 67 52 6f 77 69 64 2c 20 72 65 67 41 75 74 6f 69  gRowid, regAutoi
27c21 6e 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  nc);.          s
27c22 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
27c23 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 20  re(v, j1);.     
27c24 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
27c25 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33      j1 = sqlite3
27c26 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
27c27 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  v);.          sq
27c28 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27c29 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65  v, OP_IsNull, re
27c2a 67 52 6f 77 69 64 2c 20 6a 31 2b 32 29 3b 0a 20  gRowid, j1+2);. 
27c2b 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
27c2c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27c2d 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49  p1(v, OP_MustBeI
27c2e 6e 74 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  nt, regRowid);. 
27c2f 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
27c30 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
27c31 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 73 71  Tab) ){.      sq
27c32 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27c33 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72  v, OP_Null, 0, r
27c34 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 65  egRowid);.    }e
27c35 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
27c36 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
27c37 4f 50 5f 4e 65 77 52 6f 77 69 64 2c 20 62 61 73  OP_NewRowid, bas
27c38 65 43 75 72 2c 20 72 65 67 52 6f 77 69 64 2c 20  eCur, regRowid, 
27c39 72 65 67 41 75 74 6f 69 6e 63 29 3b 0a 20 20 20  regAutoinc);.   
27c3a 20 20 20 61 70 70 65 6e 64 46 6c 61 67 20 3d 20     appendFlag = 
27c3b 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 75 74  1;.    }.    aut
27c3c 6f 49 6e 63 53 74 65 70 28 70 50 61 72 73 65 2c  oIncStep(pParse,
27c3d 20 72 65 67 41 75 74 6f 69 6e 63 2c 20 72 65 67   regAutoinc, reg
27c3e 52 6f 77 69 64 29 3b 0a 0a 20 20 20 20 2f 2a 20  Rowid);..    /* 
27c3f 50 75 73 68 20 6f 6e 74 6f 20 74 68 65 20 73 74  Push onto the st
27c40 61 63 6b 2c 20 64 61 74 61 20 66 6f 72 20 61 6c  ack, data for al
27c41 6c 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65  l columns of the
27c42 20 6e 65 77 20 65 6e 74 72 79 2c 20 62 65 67 69   new entry, begi
27c43 6e 6e 69 6e 67 0a 20 20 20 20 2a 2a 20 77 69 74  nning.    ** wit
27c44 68 20 74 68 65 20 66 69 72 73 74 20 63 6f 6c 75  h the first colu
27c45 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e  mn..    */.    n
27c46 48 69 64 64 65 6e 20 3d 20 30 3b 0a 20 20 20 20  Hidden = 0;.    
27c47 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d  for(i=0; i<pTab-
27c48 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
27c49 20 20 20 69 6e 74 20 69 52 65 67 53 74 6f 72 65     int iRegStore
27c4a 20 3d 20 72 65 67 52 6f 77 69 64 2b 31 2b 69 3b   = regRowid+1+i;
27c4b 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d 70 54  .      if( i==pT
27c4c 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20  ab->iPKey ){.   
27c4d 20 20 20 20 20 2f 2a 20 54 68 65 20 76 61 6c 75       /* The valu
27c4e 65 20 6f 66 20 74 68 65 20 49 4e 54 45 47 45 52  e of the INTEGER
27c4f 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c   PRIMARY KEY col
27c50 75 6d 6e 20 69 73 20 61 6c 77 61 79 73 20 61 20  umn is always a 
27c51 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 20 20 2a 2a  NULL..        **
27c52 20 57 68 65 6e 65 76 65 72 20 74 68 69 73 20 63   Whenever this c
27c53 6f 6c 75 6d 6e 20 69 73 20 72 65 61 64 2c 20 74  olumn is read, t
27c54 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  he record number
27c55 20 77 69 6c 6c 20 62 65 20 73 75 62 73 74 69 74   will be substit
27c56 75 74 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20  uted.        ** 
27c57 69 6e 20 69 74 73 20 70 6c 61 63 65 2e 20 20 53  in its place.  S
27c58 6f 20 77 69 6c 6c 20 66 69 6c 6c 20 74 68 69 73  o will fill this
27c59 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 61 20 4e   column with a N
27c5a 55 4c 4c 20 74 6f 20 61 76 6f 69 64 0a 20 20 20  ULL to avoid.   
27c5b 20 20 20 20 20 2a 2a 20 74 61 6b 69 6e 67 20 75       ** taking u
27c5c 70 20 64 61 74 61 20 73 70 61 63 65 20 77 69 74  p data space wit
27c5d 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 68  h information th
27c5e 61 74 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65  at will never be
27c5f 20 75 73 65 64 2e 20 2a 2f 0a 20 20 20 20 20 20   used. */.      
27c60 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27c61 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
27c62 30 2c 20 69 52 65 67 53 74 6f 72 65 29 3b 0a 20  0, iRegStore);. 
27c63 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
27c64 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
27c65 66 28 20 70 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b  f( pColumn==0 ){
27c66 0a 20 20 20 20 20 20 20 20 69 66 28 20 49 73 48  .        if( IsH
27c67 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 26 70 54 61  iddenColumn(&pTa
27c68 62 2d 3e 61 43 6f 6c 5b 69 5d 29 20 29 7b 0a 20  b->aCol[i]) ){. 
27c69 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
27c6a 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
27c6b 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 20   );.          j 
27c6c 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 20 20  = -1;.          
27c6d 6e 48 69 64 64 65 6e 2b 2b 3b 0a 20 20 20 20 20  nHidden++;.     
27c6e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
27c6f 20 20 20 20 6a 20 3d 20 69 20 2d 20 6e 48 69 64      j = i - nHid
27c70 64 65 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  den;.        }. 
27c71 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
27c72 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
27c73 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 3b 20 6a 2b 2b  Column->nId; j++
27c74 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
27c75 20 70 43 6f 6c 75 6d 6e 2d 3e 61 5b 6a 5d 2e 69   pColumn->a[j].i
27c76 64 78 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20  dx==i ) break;. 
27c77 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
27c78 0a 20 20 20 20 20 20 69 66 28 20 6a 3c 30 20 7c  .      if( j<0 |
27c79 7c 20 6e 43 6f 6c 75 6d 6e 3d 3d 30 20 7c 7c 20  | nColumn==0 || 
27c7a 28 70 43 6f 6c 75 6d 6e 20 26 26 20 6a 3e 3d 70  (pColumn && j>=p
27c7b 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 29 20 29 7b 0a  Column->nId) ){.
27c7c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
27c7d 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20  xprCode(pParse, 
27c7e 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44  pTab->aCol[i].pD
27c7f 66 6c 74 2c 20 69 52 65 67 53 74 6f 72 65 29 3b  flt, iRegStore);
27c80 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
27c81 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29 7b   useTempTable ){
27c82 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
27c83 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
27c84 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c  _Column, srcTab,
27c85 20 6a 2c 20 69 52 65 67 53 74 6f 72 65 29 3b 20   j, iRegStore); 
27c86 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
27c87 20 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20   pSelect ){.    
27c88 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27c89 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70  ddOp2(v, OP_SCop
27c8a 79 2c 20 72 65 67 46 72 6f 6d 53 65 6c 65 63 74  y, regFromSelect
27c8b 2b 6a 2c 20 69 52 65 67 53 74 6f 72 65 29 3b 0a  +j, iRegStore);.
27c8c 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
27c8d 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
27c8e 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 4c 69  Code(pParse, pLi
27c8f 73 74 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72 2c 20  st->a[j].pExpr, 
27c90 69 52 65 67 53 74 6f 72 65 29 3b 0a 20 20 20 20  iRegStore);.    
27c91 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
27c92 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
27c93 74 6f 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61  to check constra
27c94 69 6e 74 73 20 61 6e 64 20 67 65 6e 65 72 61 74  ints and generat
27c95 65 20 69 6e 64 65 78 20 6b 65 79 73 20 61 6e 64  e index keys and
27c96 0a 20 20 20 20 2a 2a 20 64 6f 20 74 68 65 20 69  .    ** do the i
27c97 6e 73 65 72 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f  nsertion..    */
27c98 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
27c99 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
27c9a 45 0a 20 20 20 20 69 66 28 20 49 73 56 69 72 74  E.    if( IsVirt
27c9b 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20  ual(pTab) ){.   
27c9c 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
27c9d 56 54 61 62 20 3d 20 28 63 6f 6e 73 74 20 63 68  VTab = (const ch
27c9e 61 72 20 2a 29 73 71 6c 69 74 65 33 47 65 74 56  ar *)sqlite3GetV
27c9f 54 61 62 6c 65 28 64 62 2c 20 70 54 61 62 29 3b  Table(db, pTab);
27ca0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 74  .      sqlite3Vt
27ca1 61 62 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 70  abMakeWritable(p
27ca2 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20  Parse, pTab);.  
27ca3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27ca4 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 55 70 64  ddOp4(v, OP_VUpd
27ca5 61 74 65 2c 20 31 2c 20 70 54 61 62 2d 3e 6e 43  ate, 1, pTab->nC
27ca6 6f 6c 2b 32 2c 20 72 65 67 49 6e 73 2c 20 70 56  ol+2, regIns, pV
27ca7 54 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20  Tab, P4_VTAB);. 
27ca8 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61 79 41       sqlite3MayA
27ca9 62 6f 72 74 28 70 50 61 72 73 65 29 3b 0a 20 20  bort(pParse);.  
27caa 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20    }else.#endif. 
27cab 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 69     {.      int i
27cac 73 52 65 70 6c 61 63 65 3b 20 20 20 20 2f 2a 20  sReplace;    /* 
27cad 53 65 74 20 74 6f 20 74 72 75 65 20 69 66 20 63  Set to true if c
27cae 6f 6e 73 74 72 61 69 6e 74 73 20 6d 61 79 20 63  onstraints may c
27caf 61 75 73 65 20 61 20 72 65 70 6c 61 63 65 20 2a  ause a replace *
27cb0 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  /.      sqlite3G
27cb1 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e  enerateConstrain
27cb2 74 43 68 65 63 6b 73 28 70 50 61 72 73 65 2c 20  tChecks(pParse, 
27cb3 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 72  pTab, baseCur, r
27cb4 65 67 49 6e 73 2c 20 61 52 65 67 49 64 78 2c 0a  egIns, aRegIdx,.
27cb5 20 20 20 20 20 20 20 20 20 20 6b 65 79 43 6f 6c            keyCol
27cb6 75 6d 6e 3e 3d 30 2c 20 30 2c 20 6f 6e 45 72 72  umn>=0, 0, onErr
27cb7 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 2c 20 26  or, endOfLoop, &
27cb8 69 73 52 65 70 6c 61 63 65 0a 20 20 20 20 20 20  isReplace.      
27cb9 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
27cba 46 6b 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  FkCheck(pParse, 
27cbb 70 54 61 62 2c 20 30 2c 20 72 65 67 49 6e 73 29  pTab, 0, regIns)
27cbc 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 43  ;.      sqlite3C
27cbd 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e  ompleteInsertion
27cbe 28 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 72  (.          pPar
27cbf 73 65 2c 20 70 54 61 62 2c 20 62 61 73 65 43 75  se, pTab, baseCu
27cc0 72 2c 20 72 65 67 49 6e 73 2c 20 61 52 65 67 49  r, regIns, aRegI
27cc1 64 78 2c 20 30 2c 20 61 70 70 65 6e 64 46 6c 61  dx, 0, appendFla
27cc2 67 2c 20 69 73 52 65 70 6c 61 63 65 3d 3d 30 0a  g, isReplace==0.
27cc3 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20        );.    }. 
27cc4 20 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20   }..  /* Update 
27cc5 74 68 65 20 63 6f 75 6e 74 20 6f 66 20 72 6f 77  the count of row
27cc6 73 20 74 68 61 74 20 61 72 65 20 69 6e 73 65 72  s that are inser
27cc7 74 65 64 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  ted.  */.  if( (
27cc8 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
27cc9 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29 21 3d 30  TE_CountRows)!=0
27cca 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
27ccb 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
27ccc 41 64 64 49 6d 6d 2c 20 72 65 67 52 6f 77 43 6f  AddImm, regRowCo
27ccd 75 6e 74 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20 20  unt, 1);.  }..  
27cce 69 66 28 20 70 54 72 69 67 67 65 72 20 29 7b 0a  if( pTrigger ){.
27ccf 20 20 20 20 2f 2a 20 43 6f 64 65 20 41 46 54 45      /* Code AFTE
27cd0 52 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20  R triggers */.  
27cd1 20 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77    sqlite3CodeRow
27cd2 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20  Trigger(pParse, 
27cd3 70 54 72 69 67 67 65 72 2c 20 54 4b 5f 49 4e 53  pTrigger, TK_INS
27cd4 45 52 54 2c 20 30 2c 20 54 52 49 47 47 45 52 5f  ERT, 0, TRIGGER_
27cd5 41 46 54 45 52 2c 20 0a 20 20 20 20 20 20 20 20  AFTER, .        
27cd6 70 54 61 62 2c 20 72 65 67 44 61 74 61 2d 32 2d  pTab, regData-2-
27cd7 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 6f 6e 45 72  pTab->nCol, onEr
27cd8 72 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b  ror, endOfLoop);
27cd9 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 62  .  }..  /* The b
27cda 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 6d 61 69  ottom of the mai
27cdb 6e 20 69 6e 73 65 72 74 69 6f 6e 20 6c 6f 6f 70  n insertion loop
27cdc 2c 20 69 66 20 74 68 65 20 64 61 74 61 20 73 6f  , if the data so
27cdd 75 72 63 65 0a 20 20 2a 2a 20 69 73 20 61 20 53  urce.  ** is a S
27cde 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
27cdf 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  .  */.  sqlite3V
27ce0 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
27ce1 76 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20  v, endOfLoop);. 
27ce2 20 69 66 28 20 75 73 65 54 65 6d 70 54 61 62 6c   if( useTempTabl
27ce3 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
27ce4 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27ce5 5f 4e 65 78 74 2c 20 73 72 63 54 61 62 2c 20 61  _Next, srcTab, a
27ce6 64 64 72 43 6f 6e 74 29 3b 0a 20 20 20 20 73 71  ddrCont);.    sq
27ce7 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
27ce8 65 28 76 2c 20 61 64 64 72 49 6e 73 54 6f 70 29  e(v, addrInsTop)
27ce9 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
27cea 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
27ceb 6f 73 65 2c 20 73 72 63 54 61 62 29 3b 0a 20 20  ose, srcTab);.  
27cec 7d 65 6c 73 65 20 69 66 28 20 70 53 65 6c 65 63  }else if( pSelec
27ced 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
27cee 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27cef 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 43 6f  _Goto, 0, addrCo
27cf0 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  nt);.    sqlite3
27cf1 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
27cf2 61 64 64 72 49 6e 73 54 6f 70 29 3b 0a 20 20 7d  addrInsTop);.  }
27cf3 0a 0a 20 20 69 66 28 20 21 49 73 56 69 72 74 75  ..  if( !IsVirtu
27cf4 61 6c 28 70 54 61 62 29 20 26 26 20 21 69 73 56  al(pTab) && !isV
27cf5 69 65 77 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6c  iew ){.    /* Cl
27cf6 6f 73 65 20 61 6c 6c 20 74 61 62 6c 65 73 20 6f  ose all tables o
27cf7 70 65 6e 65 64 20 2a 2f 0a 20 20 20 20 73 71 6c  pened */.    sql
27cf8 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
27cf9 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 62 61 73 65  , OP_Close, base
27cfa 43 75 72 29 3b 0a 20 20 20 20 66 6f 72 28 69 64  Cur);.    for(id
27cfb 78 3d 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e  x=1, pIdx=pTab->
27cfc 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
27cfd 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20  dx=pIdx->pNext, 
27cfe 69 64 78 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  idx++){.      sq
27cff 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
27d00 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 64 78  v, OP_Close, idx
27d01 2b 62 61 73 65 43 75 72 29 3b 0a 20 20 20 20 7d  +baseCur);.    }
27d02 0a 20 20 7d 0a 0a 69 6e 73 65 72 74 5f 65 6e 64  .  }..insert_end
27d03 3a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68  :.  /* Update th
27d04 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  e sqlite_sequenc
27d05 65 20 74 61 62 6c 65 20 62 79 20 73 74 6f 72 69  e table by stori
27d06 6e 67 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  ng the content o
27d07 66 20 74 68 65 0a 20 20 2a 2a 20 6d 61 78 69 6d  f the.  ** maxim
27d08 75 6d 20 72 6f 77 69 64 20 63 6f 75 6e 74 65 72  um rowid counter
27d09 20 76 61 6c 75 65 73 20 72 65 63 6f 72 64 65 64   values recorded
27d0a 20 77 68 69 6c 65 20 69 6e 73 65 72 74 69 6e 67   while inserting
27d0b 20 69 6e 74 6f 0a 20 20 2a 2a 20 61 75 74 6f 69   into.  ** autoi
27d0c 6e 63 72 65 6d 65 6e 74 20 74 61 62 6c 65 73 2e  ncrement tables.
27d0d 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61 72  .  */.  if( pPar
27d0e 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 26 26  se->nested==0 &&
27d0f 20 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65   pParse->pTrigge
27d10 72 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20 73  rTab==0 ){.    s
27d11 71 6c 69 74 65 33 41 75 74 6f 69 6e 63 72 65 6d  qlite3Autoincrem
27d12 65 6e 74 45 6e 64 28 70 50 61 72 73 65 29 3b 0a  entEnd(pParse);.
27d13 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52    }..  /*.  ** R
27d14 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
27d15 20 6f 66 20 72 6f 77 73 20 69 6e 73 65 72 74 65   of rows inserte
27d16 64 2e 20 49 66 20 74 68 69 73 20 72 6f 75 74 69  d. If this routi
27d17 6e 65 20 69 73 20 0a 20 20 2a 2a 20 67 65 6e 65  ne is .  ** gene
27d18 72 61 74 69 6e 67 20 63 6f 64 65 20 62 65 63 61  rating code beca
27d19 75 73 65 20 6f 66 20 61 20 63 61 6c 6c 20 74 6f  use of a call to
27d1a 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
27d1b 72 73 65 28 29 2c 20 64 6f 20 6e 6f 74 0a 20 20  rse(), do not.  
27d1c 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 61  ** invoke the ca
27d1d 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e  llback function.
27d1e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d  .  */.  if( (db-
27d1f 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 43 6f  >flags&SQLITE_Co
27d20 75 6e 74 52 6f 77 73 29 20 26 26 20 21 70 50 61  untRows) && !pPa
27d21 72 73 65 2d 3e 6e 65 73 74 65 64 20 26 26 20 21  rse->nested && !
27d22 70 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72  pParse->pTrigger
27d23 54 61 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Tab ){.    sqlit
27d24 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
27d25 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 72 65  OP_ResultRow, re
27d26 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a 20  gRowCount, 1);. 
27d27 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
27d28 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a  tNumCols(v, 1);.
27d29 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
27d2a 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
27d2b 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 72  COLNAME_NAME, "r
27d2c 6f 77 73 20 69 6e 73 65 72 74 65 64 22 2c 20 53  ows inserted", S
27d2d 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
27d2e 20 7d 0a 0a 69 6e 73 65 72 74 5f 63 6c 65 61 6e   }..insert_clean
27d2f 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63  up:.  sqlite3Src
27d30 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
27d31 54 61 62 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69  TabList);.  sqli
27d32 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
27d33 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20  e(db, pList);.  
27d34 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
27d35 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29  ete(db, pSelect)
27d36 3b 0a 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73  ;.  sqlite3IdLis
27d37 74 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c  tDelete(db, pCol
27d38 75 6d 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  umn);.  sqlite3D
27d39 62 46 72 65 65 28 64 62 2c 20 61 52 65 67 49 64  bFree(db, aRegId
27d3a 78 29 3b 0a 7d 0a 0a 2f 2a 20 4d 61 6b 65 20 73  x);.}../* Make s
27d3b 75 72 65 20 22 69 73 56 69 65 77 22 20 61 6e 64  ure "isView" and
27d3c 20 6f 74 68 65 72 20 6d 61 63 72 6f 73 20 64 65   other macros de
27d3d 66 69 6e 65 64 20 61 62 6f 76 65 20 61 72 65 20  fined above are 
27d3e 75 6e 64 65 66 69 6e 65 64 2e 20 4f 74 68 65 72  undefined. Other
27d3f 77 69 73 65 0a 2a 2a 20 74 68 65 6c 79 20 6d 61  wise.** thely ma
27d40 79 20 69 6e 74 65 72 66 65 72 65 20 77 69 74 68  y interfere with
27d41 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 66 20   compilation of 
27d42 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 73 20  other functions 
27d43 69 6e 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20  in this file.** 
27d44 28 6f 72 20 69 6e 20 61 6e 6f 74 68 65 72 20 66  (or in another f
27d45 69 6c 65 2c 20 69 66 20 74 68 69 73 20 66 69 6c  ile, if this fil
27d46 65 20 62 65 63 6f 6d 65 73 20 70 61 72 74 20 6f  e becomes part o
27d47 66 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74 69  f the amalgamati
27d48 6f 6e 29 2e 20 20 2a 2f 0a 23 69 66 64 65 66 20  on).  */.#ifdef 
27d49 69 73 56 69 65 77 0a 20 23 75 6e 64 65 66 20 69  isView. #undef i
27d4a 73 56 69 65 77 0a 23 65 6e 64 69 66 0a 23 69 66  sView.#endif.#if
27d4b 64 65 66 20 70 54 72 69 67 67 65 72 0a 20 23 75  def pTrigger. #u
27d4c 6e 64 65 66 20 70 54 72 69 67 67 65 72 0a 23 65  ndef pTrigger.#e
27d4d 6e 64 69 66 0a 23 69 66 64 65 66 20 74 6d 61 73  ndif.#ifdef tmas
27d4e 6b 0a 20 23 75 6e 64 65 66 20 74 6d 61 73 6b 0a  k. #undef tmask.
27d4f 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 47  #endif.../*.** G
27d50 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
27d51 64 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68  do constraint ch
27d52 65 63 6b 73 20 70 72 69 6f 72 20 74 6f 20 61 6e  ecks prior to an
27d53 20 49 4e 53 45 52 54 20 6f 72 20 61 6e 20 55 50   INSERT or an UP
27d54 44 41 54 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  DATE..**.** The 
27d55 69 6e 70 75 74 20 69 73 20 61 20 72 61 6e 67 65  input is a range
27d56 20 6f 66 20 63 6f 6e 73 65 63 75 74 69 76 65 20   of consecutive 
27d57 72 65 67 69 73 74 65 72 73 20 61 73 20 66 6f 6c  registers as fol
27d58 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 31  lows:.**.**    1
27d59 2e 20 20 54 68 65 20 72 6f 77 69 64 20 6f 66 20  .  The rowid of 
27d5a 74 68 65 20 72 6f 77 20 61 66 74 65 72 20 74 68  the row after th
27d5b 65 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20  e update..**.** 
27d5c 20 20 20 32 2e 20 20 54 68 65 20 64 61 74 61 20     2.  The data 
27d5d 69 6e 20 74 68 65 20 66 69 72 73 74 20 63 6f 6c  in the first col
27d5e 75 6d 6e 20 6f 66 20 74 68 65 20 65 6e 74 72 79  umn of the entry
27d5f 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74   after the updat
27d60 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 69 2e 20 20  e..**.**    i.  
27d61 44 61 74 61 20 66 72 6f 6d 20 6d 69 64 64 6c 65  Data from middle
27d62 20 63 6f 6c 75 6d 6e 73 2e 2e 2e 0a 2a 2a 0a 2a   columns....**.*
27d63 2a 20 20 20 20 4e 2e 20 20 54 68 65 20 64 61 74  *    N.  The dat
27d64 61 20 69 6e 20 74 68 65 20 6c 61 73 74 20 63 6f  a in the last co
27d65 6c 75 6d 6e 20 6f 66 20 74 68 65 20 65 6e 74 72  lumn of the entr
27d66 79 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61  y after the upda
27d67 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65  te..**.** The re
27d68 67 52 6f 77 69 64 20 70 61 72 61 6d 65 74 65 72  gRowid parameter
27d69 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66   is the index of
27d6a 20 74 68 65 20 72 65 67 69 73 74 65 72 20 63 6f   the register co
27d6b 6e 74 61 69 6e 69 6e 67 20 28 31 29 2e 0a 2a 2a  ntaining (1)..**
27d6c 0a 2a 2a 20 49 66 20 69 73 55 70 64 61 74 65 20  .** If isUpdate 
27d6d 69 73 20 74 72 75 65 20 61 6e 64 20 72 6f 77 69  is true and rowi
27d6e 64 43 68 6e 67 20 69 73 20 6e 6f 6e 2d 7a 65 72  dChng is non-zer
27d6f 6f 2c 20 74 68 65 6e 20 72 6f 77 69 64 43 68 6e  o, then rowidChn
27d70 67 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68  g contains.** th
27d71 65 20 61 64 64 72 65 73 73 20 6f 66 20 61 20 72  e address of a r
27d72 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e 69  egister containi
27d73 6e 67 20 74 68 65 20 72 6f 77 69 64 20 62 65 66  ng the rowid bef
27d74 6f 72 65 20 74 68 65 20 75 70 64 61 74 65 20 74  ore the update t
27d75 61 6b 65 73 0a 2a 2a 20 70 6c 61 63 65 2e 20 69  akes.** place. i
27d76 73 55 70 64 61 74 65 20 69 73 20 74 72 75 65 20  sUpdate is true 
27d77 66 6f 72 20 55 50 44 41 54 45 73 20 61 6e 64 20  for UPDATEs and 
27d78 66 61 6c 73 65 20 66 6f 72 20 49 4e 53 45 52 54  false for INSERT
27d79 73 2e 20 49 66 20 69 73 55 70 64 61 74 65 0a 2a  s. If isUpdate.*
27d7a 2a 20 69 73 20 66 61 6c 73 65 2c 20 69 6e 64 69  * is false, indi
27d7b 63 61 74 69 6e 67 20 61 6e 20 49 4e 53 45 52 54  cating an INSERT
27d7c 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 6e   statement, then
27d7d 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 6f 77 69   a non-zero rowi
27d7e 64 43 68 6e 67 20 0a 2a 2a 20 69 6e 64 69 63 61  dChng .** indica
27d7f 74 65 73 20 74 68 61 74 20 74 68 65 20 72 6f 77  tes that the row
27d80 69 64 20 77 61 73 20 65 78 70 6c 69 63 69 74 6c  id was explicitl
27d81 79 20 73 70 65 63 69 66 69 65 64 20 61 73 20 70  y specified as p
27d82 61 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 49 4e  art of the.** IN
27d83 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e 20  SERT statement. 
27d84 49 66 20 72 6f 77 69 64 43 68 6e 67 20 69 73 20  If rowidChng is 
27d85 66 61 6c 73 65 2c 20 69 74 20 6d 65 61 6e 73 20  false, it means 
27d86 74 68 61 74 20 20 74 68 65 20 72 6f 77 69 64 20  that  the rowid 
27d87 69 73 0a 2a 2a 20 63 6f 6d 70 75 74 65 64 20 61  is.** computed a
27d88 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69 6e 20  utomatically in 
27d89 61 6e 20 69 6e 73 65 72 74 20 6f 72 20 74 68 61  an insert or tha
27d8a 74 20 74 68 65 20 72 6f 77 69 64 20 76 61 6c 75  t the rowid valu
27d8b 65 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 6d 6f 64  e is not .** mod
27d8c 69 66 69 65 64 20 62 79 20 61 6e 20 75 70 64 61  ified by an upda
27d8d 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  te..**.** The co
27d8e 64 65 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  de generated by 
27d8f 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f  this routine sto
27d90 72 65 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74  re new index ent
27d91 72 69 65 73 20 69 6e 74 6f 0a 2a 2a 20 72 65 67  ries into.** reg
27d92 69 73 74 65 72 73 20 69 64 65 6e 74 69 66 69 65  isters identifie
27d93 64 20 62 79 20 61 52 65 67 49 64 78 5b 5d 2e 20  d by aRegIdx[]. 
27d94 20 4e 6f 20 69 6e 64 65 78 20 65 6e 74 72 79 20   No index entry 
27d95 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 0a 2a  is created for.*
27d96 2a 20 69 6e 64 69 63 65 73 20 77 68 65 72 65 20  * indices where 
27d97 61 52 65 67 49 64 78 5b 69 5d 3d 3d 30 2e 20 20  aRegIdx[i]==0.  
27d98 54 68 65 20 6f 72 64 65 72 20 6f 66 20 69 6e 64  The order of ind
27d99 69 63 65 73 20 69 6e 20 61 52 65 67 49 64 78 5b  ices in aRegIdx[
27d9a 5d 20 69 73 0a 2a 2a 20 74 68 65 20 73 61 6d 65  ] is.** the same
27d9b 20 61 73 20 74 68 65 20 6f 72 64 65 72 20 6f 66   as the order of
27d9c 20 69 6e 64 69 63 65 73 20 6f 6e 20 74 68 65 20   indices on the 
27d9d 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 69  linked list of i
27d9e 6e 64 69 63 65 73 0a 2a 2a 20 61 74 74 61 63 68  ndices.** attach
27d9f 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65 2e  ed to the table.
27da0 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
27da1 69 6e 65 20 61 6c 73 6f 20 67 65 6e 65 72 61 74  ine also generat
27da2 65 73 20 63 6f 64 65 20 74 6f 20 63 68 65 63 6b  es code to check
27da3 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 4e   constraints.  N
27da4 4f 54 20 4e 55 4c 4c 2c 0a 2a 2a 20 43 48 45 43  OT NULL,.** CHEC
27da5 4b 2c 20 61 6e 64 20 55 4e 49 51 55 45 20 63 6f  K, and UNIQUE co
27da6 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20 61 6c  nstraints are al
27da7 6c 20 63 68 65 63 6b 65 64 2e 20 20 49 66 20 61  l checked.  If a
27da8 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c   constraint fail
27da9 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 61  s,.** then the a
27daa 70 70 72 6f 70 72 69 61 74 65 20 61 63 74 69 6f  ppropriate actio
27dab 6e 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20  n is performed. 
27dac 20 54 68 65 72 65 20 61 72 65 20 66 69 76 65 20   There are five 
27dad 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 61 63 74 69  possible.** acti
27dae 6f 6e 73 3a 20 52 4f 4c 4c 42 41 43 4b 2c 20 41  ons: ROLLBACK, A
27daf 42 4f 52 54 2c 20 46 41 49 4c 2c 20 52 45 50 4c  BORT, FAIL, REPL
27db0 41 43 45 2c 20 61 6e 64 20 49 47 4e 4f 52 45 2e  ACE, and IGNORE.
27db1 0a 2a 2a 0a 2a 2a 20 20 43 6f 6e 73 74 72 61 69  .**.**  Constrai
27db2 6e 74 20 74 79 70 65 20 20 41 63 74 69 6f 6e 20  nt type  Action 
27db3 20 20 20 20 20 20 57 68 61 74 20 48 61 70 70 65        What Happe
27db4 6e 73 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  ns.**  ---------
27db5 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d 2d 2d  ------  --------
27db6 2d 2d 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  --   -----------
27db7 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
27db8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
27db9 20 20 61 6e 79 20 20 20 20 20 20 20 20 20 20 20    any           
27dba 20 20 20 52 4f 4c 4c 42 41 43 4b 20 20 20 20 20     ROLLBACK     
27dbb 54 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e  The current tran
27dbc 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c 6c 65  saction is rolle
27dbd 64 20 62 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20  d back and.**   
27dbe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27dbf 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
27dc0 69 74 65 33 5f 65 78 65 63 28 29 20 72 65 74 75  ite3_exec() retu
27dc1 72 6e 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  rns immediately 
27dc2 77 69 74 68 20 61 0a 2a 2a 20 20 20 20 20 20 20  with a.**       
27dc3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27dc4 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
27dc5 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45 5f 43  code of SQLITE_C
27dc6 4f 4e 53 54 52 41 49 4e 54 2e 0a 2a 2a 0a 2a 2a  ONSTRAINT..**.**
27dc7 20 20 61 6e 79 20 20 20 20 20 20 20 20 20 20 20    any           
27dc8 20 20 20 41 42 4f 52 54 20 20 20 20 20 20 20 20     ABORT        
27dc9 42 61 63 6b 20 6f 75 74 20 63 68 61 6e 67 65 73  Back out changes
27dca 20 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65 6e   from the curren
27dcb 74 20 63 6f 6d 6d 61 6e 64 0a 2a 2a 20 20 20 20  t command.**    
27dcc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27dcd 20 20 20 20 20 20 20 20 20 20 20 20 6f 6e 6c 79              only
27dce 20 28 64 6f 20 6e 6f 74 20 64 6f 20 61 20 63 6f   (do not do a co
27dcf 6d 70 6c 65 74 65 20 72 6f 6c 6c 62 61 63 6b 29  mplete rollback)
27dd0 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20   then.**        
27dd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27dd2 20 20 20 20 20 20 20 20 63 61 75 73 65 20 73 71          cause sq
27dd3 6c 69 74 65 33 5f 65 78 65 63 28 29 20 74 6f 20  lite3_exec() to 
27dd4 72 65 74 75 72 6e 20 69 6d 6d 65 64 69 61 74 65  return immediate
27dd5 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ly.**           
27dd6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27dd7 20 20 20 20 20 77 69 74 68 20 53 51 4c 49 54 45       with SQLITE
27dd8 5f 43 4f 4e 53 54 52 41 49 4e 54 2e 0a 2a 2a 0a  _CONSTRAINT..**.
27dd9 2a 2a 20 20 61 6e 79 20 20 20 20 20 20 20 20 20  **  any         
27dda 20 20 20 20 20 46 41 49 4c 20 20 20 20 20 20 20       FAIL       
27ddb 20 20 53 71 6c 69 74 65 5f 65 78 65 63 28 29 20    Sqlite_exec() 
27ddc 72 65 74 75 72 6e 73 20 69 6d 6d 65 64 69 61 74  returns immediat
27ddd 65 6c 79 20 77 69 74 68 20 61 0a 2a 2a 20 20 20  ely with a.**   
27dde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27ddf 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74               ret
27de0 75 72 6e 20 63 6f 64 65 20 6f 66 20 53 51 4c 49  urn code of SQLI
27de1 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2e 20 20  TE_CONSTRAINT.  
27de2 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  The.**          
27de3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27de4 20 20 20 20 20 20 74 72 61 6e 73 61 63 74 69 6f        transactio
27de5 6e 20 69 73 20 6e 6f 74 20 72 6f 6c 6c 65 64 20  n is not rolled 
27de6 62 61 63 6b 20 61 6e 64 20 61 6e 79 0a 2a 2a 20  back and any.** 
27de7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27de8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
27de9 72 69 6f 72 20 63 68 61 6e 67 65 73 20 61 72 65  rior changes are
27dea 20 72 65 74 61 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a   retained..**.**
27deb 20 20 61 6e 79 20 20 20 20 20 20 20 20 20 20 20    any           
27dec 20 20 20 49 47 4e 4f 52 45 20 20 20 20 20 20 20     IGNORE       
27ded 54 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  The record numbe
27dee 72 20 61 6e 64 20 64 61 74 61 20 69 73 20 70 6f  r and data is po
27def 70 70 65 64 20 66 72 6f 6d 0a 2a 2a 20 20 20 20  pped from.**    
27df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27df1 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20              the 
27df2 73 74 61 63 6b 20 61 6e 64 20 74 68 65 72 65 20  stack and there 
27df3 69 73 20 61 6e 20 69 6d 6d 65 64 69 61 74 65 20  is an immediate 
27df4 6a 75 6d 70 0a 2a 2a 20 20 20 20 20 20 20 20 20  jump.**         
27df5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27df6 20 20 20 20 20 20 20 74 6f 20 6c 61 62 65 6c 20         to label 
27df7 69 67 6e 6f 72 65 44 65 73 74 2e 0a 2a 2a 0a 2a  ignoreDest..**.*
27df8 2a 20 20 4e 4f 54 20 4e 55 4c 4c 20 20 20 20 20  *  NOT NULL     
27df9 20 20 20 20 52 45 50 4c 41 43 45 20 20 20 20 20      REPLACE     
27dfa 20 54 68 65 20 4e 55 4c 4c 20 76 61 6c 75 65 20   The NULL value 
27dfb 69 73 20 72 65 70 6c 61 63 65 20 62 79 20 74 68  is replace by th
27dfc 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20  e default.**    
27dfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27dfe 20 20 20 20 20 20 20 20 20 20 20 20 76 61 6c 75              valu
27dff 65 20 66 6f 72 20 74 68 61 74 20 63 6f 6c 75 6d  e for that colum
27e00 6e 2e 20 20 49 66 20 74 68 65 20 64 65 66 61 75  n.  If the defau
27e01 6c 74 20 76 61 6c 75 65 0a 2a 2a 20 20 20 20 20  lt value.**     
27e02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27e03 20 20 20 20 20 20 20 20 20 20 20 69 73 20 4e 55             is NU
27e04 4c 4c 2c 20 74 68 65 20 61 63 74 69 6f 6e 20 69  LL, the action i
27e05 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 41 42  s the same as AB
27e06 4f 52 54 2e 0a 2a 2a 0a 2a 2a 20 20 55 4e 49 51  ORT..**.**  UNIQ
27e07 55 45 20 20 20 20 20 20 20 20 20 20 20 52 45 50  UE           REP
27e08 4c 41 43 45 20 20 20 20 20 20 54 68 65 20 6f 74  LACE      The ot
27e09 68 65 72 20 72 6f 77 20 74 68 61 74 20 63 6f 6e  her row that con
27e0a 66 6c 69 63 74 73 20 77 69 74 68 20 74 68 65 20  flicts with the 
27e0b 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  row.**          
27e0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27e0d 20 20 20 20 20 20 62 65 69 6e 67 20 69 6e 73 65        being inse
27e0e 72 74 65 64 20 69 73 20 72 65 6d 6f 76 65 64 2e  rted is removed.
27e0f 0a 2a 2a 0a 2a 2a 20 20 43 48 45 43 4b 20 20 20  .**.**  CHECK   
27e10 20 20 20 20 20 20 20 20 20 52 45 50 4c 41 43 45           REPLACE
27e11 20 20 20 20 20 20 49 6c 6c 65 67 61 6c 2e 20 20        Illegal.  
27e12 54 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20 61  The results in a
27e13 6e 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a 2a 0a  n exception..**.
27e14 2a 2a 20 57 68 69 63 68 20 61 63 74 69 6f 6e 20  ** Which action 
27e15 74 6f 20 74 61 6b 65 20 69 73 20 64 65 74 65 72  to take is deter
27e16 6d 69 6e 65 64 20 62 79 20 74 68 65 20 6f 76 65  mined by the ove
27e17 72 72 69 64 65 45 72 72 6f 72 20 70 61 72 61 6d  rrideError param
27e18 65 74 65 72 2e 0a 2a 2a 20 4f 72 20 69 66 20 6f  eter..** Or if o
27e19 76 65 72 72 69 64 65 45 72 72 6f 72 3d 3d 4f 45  verrideError==OE
27e1a 5f 44 65 66 61 75 6c 74 2c 20 74 68 65 6e 20 74  _Default, then t
27e1b 68 65 20 70 50 61 72 73 65 2d 3e 6f 6e 45 72 72  he pParse->onErr
27e1c 6f 72 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20  or parameter.** 
27e1d 69 73 20 75 73 65 64 2e 20 20 4f 72 20 69 66 20  is used.  Or if 
27e1e 70 50 61 72 73 65 2d 3e 6f 6e 45 72 72 6f 72 3d  pParse->onError=
27e1f 3d 4f 45 5f 44 65 66 61 75 6c 74 20 74 68 65 6e  =OE_Default then
27e20 20 74 68 65 20 6f 6e 45 72 72 6f 72 20 76 61 6c   the onError val
27e21 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f  ue.** for the co
27e22 6e 73 74 72 61 69 6e 74 20 69 73 20 75 73 65 64  nstraint is used
27e23 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c  ..**.** The call
27e24 69 6e 67 20 72 6f 75 74 69 6e 65 20 6d 75 73 74  ing routine must
27e25 20 6f 70 65 6e 20 61 20 72 65 61 64 2f 77 72 69   open a read/wri
27e26 74 65 20 63 75 72 73 6f 72 20 66 6f 72 20 70 54  te cursor for pT
27e27 61 62 20 77 69 74 68 0a 2a 2a 20 63 75 72 73 6f  ab with.** curso
27e28 72 20 6e 75 6d 62 65 72 20 22 62 61 73 65 43 75  r number "baseCu
27e29 72 22 2e 20 20 41 6c 6c 20 69 6e 64 69 63 65 73  r".  All indices
27e2a 20 6f 66 20 70 54 61 62 20 6d 75 73 74 20 61 6c   of pTab must al
27e2b 73 6f 20 68 61 76 65 20 6f 70 65 6e 0a 2a 2a 20  so have open.** 
27e2c 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f  read/write curso
27e2d 72 73 20 77 69 74 68 20 63 75 72 73 6f 72 20 6e  rs with cursor n
27e2e 75 6d 62 65 72 20 62 61 73 65 43 75 72 2b 69 20  umber baseCur+i 
27e2f 66 6f 72 20 74 68 65 20 69 2d 74 68 20 63 75 72  for the i-th cur
27e30 73 6f 72 2e 0a 2a 2a 20 45 78 63 65 70 74 2c 20  sor..** Except, 
27e31 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 70  if there is no p
27e32 6f 73 73 69 62 69 6c 69 74 79 20 6f 66 20 61 20  ossibility of a 
27e33 52 45 50 4c 41 43 45 20 61 63 74 69 6f 6e 20 74  REPLACE action t
27e34 68 65 6e 0a 2a 2a 20 63 75 72 73 6f 72 73 20 64  hen.** cursors d
27e35 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65  o not need to be
27e36 20 6f 70 65 6e 20 66 6f 72 20 69 6e 64 69 63 65   open for indice
27e37 73 20 77 68 65 72 65 20 61 52 65 67 49 64 78 5b  s where aRegIdx[
27e38 69 5d 3d 3d 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45  i]==0..*/.SQLITE
27e39 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
27e3a 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e  lite3GenerateCon
27e3b 73 74 72 61 69 6e 74 43 68 65 63 6b 73 28 0a 20  straintChecks(. 
27e3c 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
27e3d 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
27e3e 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
27e3f 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20  Table *pTab,    
27e40 20 20 20 20 2f 2a 20 74 68 65 20 74 61 62 6c 65      /* the table
27e41 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61   into which we a
27e42 72 65 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f 0a  re inserting */.
27e43 20 20 69 6e 74 20 62 61 73 65 43 75 72 2c 20 20    int baseCur,  
27e44 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
27e45 66 20 61 20 72 65 61 64 2f 77 72 69 74 65 20 63  f a read/write c
27e46 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20 61  ursor pointing a
27e47 74 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20  t pTab */.  int 
27e48 72 65 67 52 6f 77 69 64 2c 20 20 20 20 20 20 20  regRowid,       
27e49 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20  /* Index of the 
27e4a 72 61 6e 67 65 20 6f 66 20 69 6e 70 75 74 20 72  range of input r
27e4b 65 67 69 73 74 65 72 73 20 2a 2f 0a 20 20 69 6e  egisters */.  in
27e4c 74 20 2a 61 52 65 67 49 64 78 2c 20 20 20 20 20  t *aRegIdx,     
27e4d 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 75 73    /* Register us
27e4e 65 64 20 62 79 20 65 61 63 68 20 69 6e 64 65 78  ed by each index
27e4f 2e 20 20 30 20 66 6f 72 20 75 6e 75 73 65 64 20  .  0 for unused 
27e50 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e 74  indices */.  int
27e51 20 72 6f 77 69 64 43 68 6e 67 2c 20 20 20 20 20   rowidChng,     
27e52 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
27e53 72 6f 77 69 64 20 6d 69 67 68 74 20 63 6f 6c 6c  rowid might coll
27e54 69 64 65 20 77 69 74 68 20 65 78 69 73 74 69 6e  ide with existin
27e55 67 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74  g entry */.  int
27e56 20 69 73 55 70 64 61 74 65 2c 20 20 20 20 20 20   isUpdate,      
27e57 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 55 50 44   /* True for UPD
27e58 41 54 45 2c 20 46 61 6c 73 65 20 66 6f 72 20 49  ATE, False for I
27e59 4e 53 45 52 54 20 2a 2f 0a 20 20 69 6e 74 20 6f  NSERT */.  int o
27e5a 76 65 72 72 69 64 65 45 72 72 6f 72 2c 20 20 2f  verrideError,  /
27e5b 2a 20 4f 76 65 72 72 69 64 65 20 6f 6e 45 72 72  * Override onErr
27e5c 6f 72 20 74 6f 20 74 68 69 73 20 69 66 20 6e 6f  or to this if no
27e5d 74 20 4f 45 5f 44 65 66 61 75 6c 74 20 2a 2f 0a  t OE_Default */.
27e5e 20 20 69 6e 74 20 69 67 6e 6f 72 65 44 65 73 74    int ignoreDest
27e5f 2c 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 74 6f  ,     /* Jump to
27e60 20 74 68 69 73 20 6c 61 62 65 6c 20 6f 6e 20 61   this label on a
27e61 6e 20 4f 45 5f 49 67 6e 6f 72 65 20 72 65 73 6f  n OE_Ignore reso
27e62 6c 75 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  lution */.  int 
27e63 2a 70 62 4d 61 79 52 65 70 6c 61 63 65 20 20 20  *pbMayReplace   
27e64 2f 2a 20 4f 55 54 3a 20 53 65 74 20 74 6f 20 74  /* OUT: Set to t
27e65 72 75 65 20 69 66 20 63 6f 6e 73 74 72 61 69 6e  rue if constrain
27e66 74 20 6d 61 79 20 63 61 75 73 65 20 61 20 72 65  t may cause a re
27e67 70 6c 61 63 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e  place */.){.  in
27e68 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
27e69 20 20 2f 2a 20 6c 6f 6f 70 20 63 6f 75 6e 74 65    /* loop counte
27e6a 72 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20  r */.  Vdbe *v; 
27e6b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44             /* VD
27e6c 42 45 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  BE under constru
27e6d 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  tion */.  int nC
27e6e 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ol;           /*
27e6f 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
27e70 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72  ns */.  int onEr
27e71 72 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 43  ror;        /* C
27e72 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69  onflict resoluti
27e73 6f 6e 20 73 74 72 61 74 65 67 79 20 2a 2f 0a 20  on strategy */. 
27e74 20 69 6e 74 20 6a 31 3b 20 20 20 20 20 20 20 20   int j1;        
27e75 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 73       /* Addresss
27e76 20 6f 66 20 6a 75 6d 70 20 69 6e 73 74 72 75 63   of jump instruc
27e77 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6a 32  tion */.  int j2
27e78 20 3d 20 30 2c 20 6a 33 3b 20 20 20 20 20 2f 2a   = 0, j3;     /*
27e79 20 41 64 64 72 65 73 73 65 73 20 6f 66 20 6a 75   Addresses of ju
27e7a 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20  mp instructions 
27e7b 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61  */.  int regData
27e7c 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69  ;        /* Regi
27e7d 73 74 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ster containing 
27e7e 66 69 72 73 74 20 64 61 74 61 20 63 6f 6c 75 6d  first data colum
27e7f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b  n */.  int iCur;
27e80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61             /* Ta
27e81 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  ble cursor numbe
27e82 72 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49  r */.  Index *pI
27e83 64 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 50  dx;         /* P
27e84 6f 69 6e 74 65 72 20 74 6f 20 6f 6e 65 20 6f 66  ointer to one of
27e85 20 74 68 65 20 69 6e 64 69 63 65 73 20 2a 2f 0a   the indices */.
27e86 20 20 69 6e 74 20 73 65 65 6e 52 65 70 6c 61 63    int seenReplac
27e87 65 20 3d 20 30 3b 20 2f 2a 20 54 72 75 65 20 69  e = 0; /* True i
27e88 66 20 52 45 50 4c 41 43 45 20 69 73 20 75 73 65  f REPLACE is use
27e89 64 20 74 6f 20 72 65 73 6f 6c 76 65 20 49 4e 54  d to resolve INT
27e8a 20 50 4b 20 63 6f 6e 66 6c 69 63 74 20 2a 2f 0a   PK conflict */.
27e8b 20 20 69 6e 74 20 72 65 67 4f 6c 64 52 6f 77 69    int regOldRowi
27e8c 64 20 3d 20 28 72 6f 77 69 64 43 68 6e 67 20 26  d = (rowidChng &
27e8d 26 20 69 73 55 70 64 61 74 65 29 20 3f 20 72 6f  & isUpdate) ? ro
27e8e 77 69 64 43 68 6e 67 20 3a 20 72 65 67 52 6f 77  widChng : regRow
27e8f 69 64 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74  id;..  v = sqlit
27e90 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
27e91 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d  );.  assert( v!=
27e92 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
27e93 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20  Tab->pSelect==0 
27e94 29 3b 20 20 2f 2a 20 54 68 69 73 20 74 61 62 6c  );  /* This tabl
27e95 65 20 69 73 20 6e 6f 74 20 61 20 56 49 45 57 20  e is not a VIEW 
27e96 2a 2f 0a 20 20 6e 43 6f 6c 20 3d 20 70 54 61 62  */.  nCol = pTab
27e97 2d 3e 6e 43 6f 6c 3b 0a 20 20 72 65 67 44 61 74  ->nCol;.  regDat
27e98 61 20 3d 20 72 65 67 52 6f 77 69 64 20 2b 20 31  a = regRowid + 1
27e99 3b 0a 0a 20 20 2f 2a 20 54 65 73 74 20 61 6c 6c  ;..  /* Test all
27e9a 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72   NOT NULL constr
27e9b 61 69 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f  aints..  */.  fo
27e9c 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69  r(i=0; i<nCol; i
27e9d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 3d 3d  ++){.    if( i==
27e9e 70 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20  pTab->iPKey ){. 
27e9f 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
27ea0 20 20 20 7d 0a 20 20 20 20 6f 6e 45 72 72 6f 72     }.    onError
27ea1 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d   = pTab->aCol[i]
27ea2 2e 6e 6f 74 4e 75 6c 6c 3b 0a 20 20 20 20 69 66  .notNull;.    if
27ea3 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f  ( onError==OE_No
27ea4 6e 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  ne ) continue;. 
27ea5 20 20 20 69 66 28 20 6f 76 65 72 72 69 64 65 45     if( overrideE
27ea6 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74  rror!=OE_Default
27ea7 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f   ){.      onErro
27ea8 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72 72 6f  r = overrideErro
27ea9 72 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  r;.    }else if(
27eaa 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66   onError==OE_Def
27eab 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e  ault ){.      on
27eac 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74  Error = OE_Abort
27ead 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
27eae 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c  onError==OE_Repl
27eaf 61 63 65 20 26 26 20 70 54 61 62 2d 3e 61 43 6f  ace && pTab->aCo
27eb0 6c 5b 69 5d 2e 70 44 66 6c 74 3d 3d 30 20 29 7b  l[i].pDflt==0 ){
27eb1 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d  .      onError =
27eb2 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d   OE_Abort;.    }
27eb3 0a 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e 45  .    assert( onE
27eb4 72 72 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63  rror==OE_Rollbac
27eb5 6b 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45  k || onError==OE
27eb6 5f 41 62 6f 72 74 20 7c 7c 20 6f 6e 45 72 72 6f  _Abort || onErro
27eb7 72 3d 3d 4f 45 5f 46 61 69 6c 0a 20 20 20 20 20  r==OE_Fail.     
27eb8 20 20 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f     || onError==O
27eb9 45 5f 49 67 6e 6f 72 65 20 7c 7c 20 6f 6e 45 72  E_Ignore || onEr
27eba 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20  ror==OE_Replace 
27ebb 29 3b 0a 20 20 20 20 73 77 69 74 63 68 28 20 6f  );.    switch( o
27ebc 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20  nError ){.      
27ebd 63 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a 20  case OE_Abort:. 
27ebe 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d 61         sqlite3Ma
27ebf 79 41 62 6f 72 74 28 70 50 61 72 73 65 29 3b 0a  yAbort(pParse);.
27ec0 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52 6f        case OE_Ro
27ec1 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20 20 63 61  llback:.      ca
27ec2 73 65 20 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20 20  se OE_Fail: {.  
27ec3 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4d 73 67        char *zMsg
27ec4 3b 0a 20 20 20 20 20 20 20 20 6a 31 20 3d 20 73  ;.        j1 = s
27ec5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
27ec6 28 76 2c 20 4f 50 5f 48 61 6c 74 49 66 4e 75 6c  (v, OP_HaltIfNul
27ec7 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  l,.             
27ec8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27ec9 20 20 20 20 20 53 51 4c 49 54 45 5f 43 4f 4e 53       SQLITE_CONS
27eca 54 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c  TRAINT, onError,
27ecb 20 72 65 67 44 61 74 61 2b 69 29 3b 0a 20 20 20   regData+i);.   
27ecc 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69       zMsg = sqli
27ecd 74 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72 73  te3MPrintf(pPars
27ece 65 2d 3e 64 62 2c 20 22 25 73 2e 25 73 20 6d 61  e->db, "%s.%s ma
27ecf 79 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 22 2c 0a  y not be NULL",.
27ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27ed1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 54                pT
27ed2 61 62 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d  ab->zName, pTab-
27ed3 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61 6d 65 29 3b  >aCol[i].zName);
27ed4 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
27ed5 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
27ed6 2d 31 2c 20 7a 4d 73 67 2c 20 50 34 5f 44 59 4e  -1, zMsg, P4_DYN
27ed7 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 20 20 62  AMIC);.        b
27ed8 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
27ed9 20 20 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f      case OE_Igno
27eda 72 65 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71  re: {.        sq
27edb 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27edc 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65  v, OP_IsNull, re
27edd 67 44 61 74 61 2b 69 2c 20 69 67 6e 6f 72 65 44  gData+i, ignoreD
27ede 65 73 74 29 3b 0a 20 20 20 20 20 20 20 20 62 72  est);.        br
27edf 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
27ee0 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
27ee1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 6e        assert( on
27ee2 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63  Error==OE_Replac
27ee3 65 20 29 3b 0a 20 20 20 20 20 20 20 20 6a 31 20  e );.        j1 
27ee4 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
27ee5 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c  Op1(v, OP_NotNul
27ee6 6c 2c 20 72 65 67 44 61 74 61 2b 69 29 3b 0a 20  l, regData+i);. 
27ee7 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
27ee8 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
27ee9 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66  Tab->aCol[i].pDf
27eea 6c 74 2c 20 72 65 67 44 61 74 61 2b 69 29 3b 0a  lt, regData+i);.
27eeb 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
27eec 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
27eed 31 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  1);.        brea
27eee 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
27eef 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20  .  }..  /* Test 
27ef0 61 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72  all CHECK constr
27ef1 61 69 6e 74 73 0a 20 20 2a 2f 0a 23 69 66 6e 64  aints.  */.#ifnd
27ef2 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  ef SQLITE_OMIT_C
27ef3 48 45 43 4b 0a 20 20 69 66 28 20 70 54 61 62 2d  HECK.  if( pTab-
27ef4 3e 70 43 68 65 63 6b 20 26 26 20 28 70 50 61 72  >pCheck && (pPar
27ef5 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20  se->db->flags & 
27ef6 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65  SQLITE_IgnoreChe
27ef7 63 6b 73 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69  cks)==0 ){.    i
27ef8 6e 74 20 61 6c 6c 4f 6b 20 3d 20 73 71 6c 69 74  nt allOk = sqlit
27ef9 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
27efa 76 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  v);.    pParse->
27efb 63 6b 42 61 73 65 20 3d 20 72 65 67 44 61 74 61  ckBase = regData
27efc 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
27efd 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20  rIfTrue(pParse, 
27efe 70 54 61 62 2d 3e 70 43 68 65 63 6b 2c 20 61 6c  pTab->pCheck, al
27eff 6c 4f 6b 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50  lOk, SQLITE_JUMP
27f00 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 6f 6e 45  IFNULL);.    onE
27f01 72 72 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45  rror = overrideE
27f02 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74  rror!=OE_Default
27f03 20 3f 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72   ? overrideError
27f04 20 3a 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20   : OE_Abort;.   
27f05 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45   if( onError==OE
27f06 5f 49 67 6e 6f 72 65 20 29 7b 0a 20 20 20 20 20  _Ignore ){.     
27f07 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27f08 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
27f09 2c 20 69 67 6e 6f 72 65 44 65 73 74 29 3b 0a 20  , ignoreDest);. 
27f0a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
27f0b 73 71 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74  sqlite3HaltConst
27f0c 72 61 69 6e 74 28 70 50 61 72 73 65 2c 20 6f 6e  raint(pParse, on
27f0d 45 72 72 6f 72 2c 20 30 2c 20 30 29 3b 0a 20 20  Error, 0, 0);.  
27f0e 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
27f0f 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
27f10 76 2c 20 61 6c 6c 4f 6b 29 3b 0a 20 20 7d 0a 23  v, allOk);.  }.#
27f11 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
27f12 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48  d(SQLITE_OMIT_CH
27f13 45 43 4b 29 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66  ECK) */..  /* If
27f14 20 77 65 20 68 61 76 65 20 61 6e 20 49 4e 54 45   we have an INTE
27f15 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c  GER PRIMARY KEY,
27f16 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 70   make sure the p
27f17 72 69 6d 61 72 79 20 6b 65 79 0a 20 20 2a 2a 20  rimary key.  ** 
27f18 6f 66 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72  of the new recor
27f19 64 20 64 6f 65 73 20 6e 6f 74 20 70 72 65 76 69  d does not previ
27f1a 6f 75 73 6c 79 20 65 78 69 73 74 2e 20 20 45 78  ously exist.  Ex
27f1b 63 65 70 74 2c 20 69 66 20 74 68 69 73 0a 20 20  cept, if this.  
27f1c 2a 2a 20 69 73 20 61 6e 20 55 50 44 41 54 45 20  ** is an UPDATE 
27f1d 61 6e 64 20 74 68 65 20 70 72 69 6d 61 72 79 20  and the primary 
27f1e 6b 65 79 20 69 73 20 6e 6f 74 20 63 68 61 6e 67  key is not chang
27f1f 69 6e 67 2c 20 74 68 61 74 20 69 73 20 4f 4b 2e  ing, that is OK.
27f20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 6f 77 69  .  */.  if( rowi
27f21 64 43 68 6e 67 20 29 7b 0a 20 20 20 20 6f 6e 45  dChng ){.    onE
27f22 72 72 6f 72 20 3d 20 70 54 61 62 2d 3e 6b 65 79  rror = pTab->key
27f23 43 6f 6e 66 3b 0a 20 20 20 20 69 66 28 20 6f 76  Conf;.    if( ov
27f24 65 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45 5f  errideError!=OE_
27f25 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20  Default ){.     
27f26 20 6f 6e 45 72 72 6f 72 20 3d 20 6f 76 65 72 72   onError = overr
27f27 69 64 65 45 72 72 6f 72 3b 0a 20 20 20 20 7d 65  ideError;.    }e
27f28 6c 73 65 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d  lse if( onError=
27f29 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20  =OE_Default ){. 
27f2a 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f       onError = O
27f2b 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20  E_Abort;.    }. 
27f2c 20 20 20 0a 20 20 20 20 69 66 28 20 69 73 55 70     .    if( isUp
27f2d 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20 6a 32  date ){.      j2
27f2e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
27f2f 64 4f 70 33 28 76 2c 20 4f 50 5f 45 71 2c 20 72  dOp3(v, OP_Eq, r
27f30 65 67 52 6f 77 69 64 2c 20 30 2c 20 72 6f 77 69  egRowid, 0, rowi
27f31 64 43 68 6e 67 29 3b 0a 20 20 20 20 7d 0a 20 20  dChng);.    }.  
27f32 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64    j3 = sqlite3Vd
27f33 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e  beAddOp3(v, OP_N
27f34 6f 74 45 78 69 73 74 73 2c 20 62 61 73 65 43 75  otExists, baseCu
27f35 72 2c 20 30 2c 20 72 65 67 52 6f 77 69 64 29 3b  r, 0, regRowid);
27f36 0a 20 20 20 20 73 77 69 74 63 68 28 20 6f 6e 45  .    switch( onE
27f37 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 64 65  rror ){.      de
27f38 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20  fault: {.       
27f39 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62   onError = OE_Ab
27f3a 6f 72 74 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ort;.        /* 
27f3b 46 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 74  Fall thru into t
27f3c 68 65 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a  he next case */.
27f3d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
27f3e 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 0a  se OE_Rollback:.
27f3f 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 41 62        case OE_Ab
27f40 6f 72 74 3a 0a 20 20 20 20 20 20 63 61 73 65 20  ort:.      case 
27f41 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20 20 20 20 20  OE_Fail: {.     
27f42 20 20 20 73 71 6c 69 74 65 33 48 61 6c 74 43 6f     sqlite3HaltCo
27f43 6e 73 74 72 61 69 6e 74 28 0a 20 20 20 20 20 20  nstraint(.      
27f44 20 20 20 20 70 50 61 72 73 65 2c 20 6f 6e 45 72      pParse, onEr
27f45 72 6f 72 2c 20 22 50 52 49 4d 41 52 59 20 4b 45  ror, "PRIMARY KE
27f46 59 20 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65  Y must be unique
27f47 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  ", P4_STATIC);. 
27f48 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
27f49 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
27f4a 20 4f 45 5f 52 65 70 6c 61 63 65 3a 20 7b 0a 20   OE_Replace: {. 
27f4b 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65         /* If the
27f4c 72 65 20 61 72 65 20 44 45 4c 45 54 45 20 74 72  re are DELETE tr
27f4d 69 67 67 65 72 73 20 6f 6e 20 74 68 69 73 20 74  iggers on this t
27f4e 61 62 6c 65 20 61 6e 64 20 74 68 65 0a 20 20 20  able and the.   
27f4f 20 20 20 20 20 2a 2a 20 72 65 63 75 72 73 69 76       ** recursiv
27f50 65 2d 74 72 69 67 67 65 72 73 20 66 6c 61 67 20  e-triggers flag 
27f51 69 73 20 73 65 74 2c 20 63 61 6c 6c 20 47 65 6e  is set, call Gen
27f52 65 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28 29  erateRowDelete()
27f53 20 74 6f 0a 20 20 20 20 20 20 20 20 2a 2a 20 72   to.        ** r
27f54 65 6d 6f 76 65 20 74 68 65 20 63 6f 6e 66 6c 69  emove the confli
27f55 63 74 69 6e 67 20 72 6f 77 20 66 72 6f 6d 20 74  cting row from t
27f56 68 65 20 74 68 65 20 74 61 62 6c 65 2e 20 54 68  he the table. Th
27f57 69 73 20 77 69 6c 6c 20 66 69 72 65 0a 20 20 20  is will fire.   
27f58 20 20 20 20 20 2a 2a 20 74 68 65 20 74 72 69 67       ** the trig
27f59 67 65 72 73 20 61 6e 64 20 72 65 6d 6f 76 65 20  gers and remove 
27f5a 62 6f 74 68 20 74 68 65 20 74 61 62 6c 65 20 61  both the table a
27f5b 6e 64 20 69 6e 64 65 78 20 62 2d 74 72 65 65 20  nd index b-tree 
27f5c 65 6e 74 72 69 65 73 2e 0a 20 20 20 20 20 20 20  entries..       
27f5d 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f   **.        ** O
27f5e 74 68 65 72 77 69 73 65 2c 20 69 66 20 74 68 65  therwise, if the
27f5f 72 65 20 61 72 65 20 6e 6f 20 74 72 69 67 67 65  re are no trigge
27f60 72 73 20 6f 72 20 74 68 65 20 72 65 63 75 72 73  rs or the recurs
27f61 69 76 65 2d 74 72 69 67 67 65 72 73 0a 20 20 20  ive-triggers.   
27f62 20 20 20 20 20 2a 2a 20 66 6c 61 67 20 69 73 20       ** flag is 
27f63 6e 6f 74 20 73 65 74 2c 20 63 61 6c 6c 20 47 65  not set, call Ge
27f64 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78 44 65  nerateRowIndexDe
27f65 6c 65 74 65 28 29 2e 20 54 68 69 73 20 72 65 6d  lete(). This rem
27f66 6f 76 65 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  oves.        ** 
27f67 74 68 65 20 69 6e 64 65 78 20 62 2d 74 72 65 65  the index b-tree
27f68 20 65 6e 74 72 69 65 73 20 6f 6e 6c 79 2e 20 54   entries only. T
27f69 68 65 20 74 61 62 6c 65 20 62 2d 74 72 65 65 20  he table b-tree 
27f6a 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 20 0a 20  entry will be . 
27f6b 20 20 20 20 20 20 20 2a 2a 20 72 65 70 6c 61 63         ** replac
27f6c 65 64 20 62 79 20 74 68 65 20 6e 65 77 20 65 6e  ed by the new en
27f6d 74 72 79 20 77 68 65 6e 20 69 74 20 69 73 20 69  try when it is i
27f6e 6e 73 65 72 74 65 64 2e 20 20 2a 2f 0a 20 20 20  nserted.  */.   
27f6f 20 20 20 20 20 54 72 69 67 67 65 72 20 2a 70 54       Trigger *pT
27f70 72 69 67 67 65 72 20 3d 20 30 3b 0a 20 20 20 20  rigger = 0;.    
27f71 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e      if( pParse->
27f72 64 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45  db->flags&SQLITE
27f73 5f 52 65 63 54 72 69 67 67 65 72 73 20 29 7b 0a  _RecTriggers ){.
27f74 20 20 20 20 20 20 20 20 20 20 70 54 72 69 67 67            pTrigg
27f75 65 72 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67  er = sqlite3Trig
27f76 67 65 72 73 45 78 69 73 74 28 70 50 61 72 73 65  gersExist(pParse
27f77 2c 20 70 54 61 62 2c 20 54 4b 5f 44 45 4c 45 54  , pTab, TK_DELET
27f78 45 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  E, 0, 0);.      
27f79 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69    }.        sqli
27f7a 74 65 33 4d 75 6c 74 69 57 72 69 74 65 28 70 50  te3MultiWrite(pP
27f7b 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 69  arse);.        i
27f7c 66 28 20 70 54 72 69 67 67 65 72 20 7c 7c 20 73  f( pTrigger || s
27f7d 71 6c 69 74 65 33 46 6b 52 65 71 75 69 72 65 64  qlite3FkRequired
27f7e 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30  (pParse, pTab, 0
27f7f 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20  , 0) ){.        
27f80 20 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74    sqlite3Generat
27f81 65 52 6f 77 44 65 6c 65 74 65 28 0a 20 20 20 20  eRowDelete(.    
27f82 20 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65            pParse
27f83 2c 20 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c  , pTab, baseCur,
27f84 20 72 65 67 52 6f 77 69 64 2c 20 30 2c 20 70 54   regRowid, 0, pT
27f85 72 69 67 67 65 72 2c 20 4f 45 5f 52 65 70 6c 61  rigger, OE_Repla
27f86 63 65 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a  ce.          );.
27f87 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
27f88 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
27f89 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65 78  GenerateRowIndex
27f8a 44 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20 70  Delete(pParse, p
27f8b 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 30 29  Tab, baseCur, 0)
27f8c 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
27f8d 20 20 20 20 73 65 65 6e 52 65 70 6c 61 63 65 20      seenReplace 
27f8e 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 72 65  = 1;.        bre
27f8f 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
27f90 20 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65    case OE_Ignore
27f91 3a 20 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65  : {.        asse
27f92 72 74 28 20 73 65 65 6e 52 65 70 6c 61 63 65 3d  rt( seenReplace=
27f93 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71  =0 );.        sq
27f94 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27f95 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69  v, OP_Goto, 0, i
27f96 67 6e 6f 72 65 44 65 73 74 29 3b 0a 20 20 20 20  gnoreDest);.    
27f97 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
27f98 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
27f99 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
27f9a 28 76 2c 20 6a 33 29 3b 0a 20 20 20 20 69 66 28  (v, j3);.    if(
27f9b 20 69 73 55 70 64 61 74 65 20 29 7b 0a 20 20 20   isUpdate ){.   
27f9c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
27f9d 6d 70 48 65 72 65 28 76 2c 20 6a 32 29 3b 0a 20  mpHere(v, j2);. 
27f9e 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54     }.  }..  /* T
27f9f 65 73 74 20 61 6c 6c 20 55 4e 49 51 55 45 20 63  est all UNIQUE c
27fa0 6f 6e 73 74 72 61 69 6e 74 73 20 62 79 20 63 72  onstraints by cr
27fa1 65 61 74 69 6e 67 20 65 6e 74 72 69 65 73 20 66  eating entries f
27fa2 6f 72 20 65 61 63 68 20 55 4e 49 51 55 45 0a 20  or each UNIQUE. 
27fa3 20 2a 2a 20 69 6e 64 65 78 20 61 6e 64 20 6d 61   ** index and ma
27fa4 6b 69 6e 67 20 73 75 72 65 20 74 68 61 74 20 64  king sure that d
27fa5 75 70 6c 69 63 61 74 65 20 65 6e 74 72 69 65 73  uplicate entries
27fa6 20 64 6f 20 6e 6f 74 20 61 6c 72 65 61 64 79 20   do not already 
27fa7 65 78 69 73 74 2e 0a 20 20 2a 2a 20 41 64 64 20  exist..  ** Add 
27fa8 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 73 20  the new records 
27fa9 74 6f 20 74 68 65 20 69 6e 64 69 63 65 73 20 61  to the indices a
27faa 73 20 77 65 20 67 6f 2e 0a 20 20 2a 2f 0a 20 20  s we go..  */.  
27fab 66 6f 72 28 69 43 75 72 3d 30 2c 20 70 49 64 78  for(iCur=0, pIdx
27fac 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
27fad 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
27fae 70 4e 65 78 74 2c 20 69 43 75 72 2b 2b 29 7b 0a  pNext, iCur++){.
27faf 20 20 20 20 69 6e 74 20 72 65 67 49 64 78 3b 0a      int regIdx;.
27fb0 20 20 20 20 69 6e 74 20 72 65 67 52 3b 0a 0a 20      int regR;.. 
27fb1 20 20 20 69 66 28 20 61 52 65 67 49 64 78 5b 69     if( aRegIdx[i
27fb2 43 75 72 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  Cur]==0 ) contin
27fb3 75 65 3b 20 20 2f 2a 20 53 6b 69 70 20 75 6e 75  ue;  /* Skip unu
27fb4 73 65 64 20 69 6e 64 69 63 65 73 20 2a 2f 0a 0a  sed indices */..
27fb5 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20      /* Create a 
27fb6 6b 65 79 20 66 6f 72 20 61 63 63 65 73 73 69 6e  key for accessin
27fb7 67 20 74 68 65 20 69 6e 64 65 78 20 65 6e 74 72  g the index entr
27fb8 79 20 2a 2f 0a 20 20 20 20 72 65 67 49 64 78 20  y */.    regIdx 
27fb9 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
27fba 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70 49  Range(pParse, pI
27fbb 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a  dx->nColumn+1);.
27fbc 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
27fbd 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  Idx->nColumn; i+
27fbe 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 64  +){.      int id
27fbf 78 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75  x = pIdx->aiColu
27fc0 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28  mn[i];.      if(
27fc1 20 69 64 78 3d 3d 70 54 61 62 2d 3e 69 50 4b 65   idx==pTab->iPKe
27fc2 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  y ){.        sql
27fc3 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
27fc4 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 52  , OP_SCopy, regR
27fc5 6f 77 69 64 2c 20 72 65 67 49 64 78 2b 69 29 3b  owid, regIdx+i);
27fc6 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
27fc7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
27fc8 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43  eAddOp2(v, OP_SC
27fc9 6f 70 79 2c 20 72 65 67 44 61 74 61 2b 69 64 78  opy, regData+idx
27fca 2c 20 72 65 67 49 64 78 2b 69 29 3b 0a 20 20 20  , regIdx+i);.   
27fcb 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
27fcc 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
27fcd 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65  (v, OP_SCopy, re
27fce 67 52 6f 77 69 64 2c 20 72 65 67 49 64 78 2b 69  gRowid, regIdx+i
27fcf 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
27fd0 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
27fd1 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 49 64  akeRecord, regId
27fd2 78 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  x, pIdx->nColumn
27fd3 2b 31 2c 20 61 52 65 67 49 64 78 5b 69 43 75 72  +1, aRegIdx[iCur
27fd4 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ]);.    sqlite3V
27fd5 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d  dbeChangeP4(v, -
27fd6 31 2c 20 73 71 6c 69 74 65 33 49 6e 64 65 78 41  1, sqlite3IndexA
27fd7 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70 49  ffinityStr(v, pI
27fd8 64 78 29 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  dx), 0);.    sql
27fd9 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
27fda 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72  inityChange(pPar
27fdb 73 65 2c 20 72 65 67 49 64 78 2c 20 70 49 64 78  se, regIdx, pIdx
27fdc 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 0a 20  ->nColumn+1);.. 
27fdd 20 20 20 2f 2a 20 46 69 6e 64 20 6f 75 74 20 77     /* Find out w
27fde 68 61 74 20 61 63 74 69 6f 6e 20 74 6f 20 74 61  hat action to ta
27fdf 6b 65 20 69 6e 20 63 61 73 65 20 74 68 65 72 65  ke in case there
27fe0 20 69 73 20 61 6e 20 69 6e 64 65 78 69 6e 67 20   is an indexing 
27fe1 63 6f 6e 66 6c 69 63 74 20 2a 2f 0a 20 20 20 20  conflict */.    
27fe2 6f 6e 45 72 72 6f 72 20 3d 20 70 49 64 78 2d 3e  onError = pIdx->
27fe3 6f 6e 45 72 72 6f 72 3b 0a 20 20 20 20 69 66 28  onError;.    if(
27fe4 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f 6e   onError==OE_Non
27fe5 65 20 29 7b 20 0a 20 20 20 20 20 20 73 71 6c 69  e ){ .      sqli
27fe6 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61  te3ReleaseTempRa
27fe7 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 49  nge(pParse, regI
27fe8 64 78 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  dx, pIdx->nColum
27fe9 6e 2b 31 29 3b 0a 20 20 20 20 20 20 63 6f 6e 74  n+1);.      cont
27fea 69 6e 75 65 3b 20 20 2f 2a 20 70 49 64 78 20 69  inue;  /* pIdx i
27feb 73 20 6e 6f 74 20 61 20 55 4e 49 51 55 45 20 69  s not a UNIQUE i
27fec 6e 64 65 78 20 2a 2f 0a 20 20 20 20 7d 0a 20 20  ndex */.    }.  
27fed 20 20 69 66 28 20 6f 76 65 72 72 69 64 65 45 72    if( overrideEr
27fee 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20  ror!=OE_Default 
27fef 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72 72 6f 72  ){.      onError
27ff0 20 3d 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72   = overrideError
27ff1 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
27ff2 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61  onError==OE_Defa
27ff3 75 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45  ult ){.      onE
27ff4 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b  rror = OE_Abort;
27ff5 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73  .    }.    if( s
27ff6 65 65 6e 52 65 70 6c 61 63 65 20 29 7b 0a 20 20  eenReplace ){.  
27ff7 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d      if( onError=
27ff8 3d 4f 45 5f 49 67 6e 6f 72 65 20 29 20 6f 6e 45  =OE_Ignore ) onE
27ff9 72 72 6f 72 20 3d 20 4f 45 5f 52 65 70 6c 61 63  rror = OE_Replac
27ffa 65 3b 0a 20 20 20 20 20 20 65 6c 73 65 20 69 66  e;.      else if
27ffb 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 46 61  ( onError==OE_Fa
27ffc 69 6c 20 29 20 6f 6e 45 72 72 6f 72 20 3d 20 4f  il ) onError = O
27ffd 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20  E_Abort;.    }. 
27ffe 20 20 20 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b     .    /* Check
27fff 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 6e   to see if the n
28000 65 77 20 69 6e 64 65 78 20 65 6e 74 72 79 20 77  ew index entry w
28001 69 6c 6c 20 62 65 20 75 6e 69 71 75 65 20 2a 2f  ill be unique */
28002 0a 20 20 20 20 72 65 67 52 20 3d 20 73 71 6c 69  .    regR = sqli
28003 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
28004 61 72 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  arse);.    sqlit
28005 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
28006 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 4f 6c 64  OP_SCopy, regOld
28007 52 6f 77 69 64 2c 20 72 65 67 52 29 3b 0a 20 20  Rowid, regR);.  
28008 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64    j3 = sqlite3Vd
28009 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49  beAddOp4(v, OP_I
2800a 73 55 6e 69 71 75 65 2c 20 62 61 73 65 43 75 72  sUnique, baseCur
2800b 2b 69 43 75 72 2b 31 2c 20 30 2c 0a 20 20 20 20  +iCur+1, 0,.    
2800c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2800d 20 20 20 20 20 20 20 72 65 67 52 2c 20 53 51 4c         regR, SQL
2800e 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 72  ITE_INT_TO_PTR(r
2800f 65 67 49 64 78 29 2c 0a 20 20 20 20 20 20 20 20  egIdx),.        
28010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28011 20 20 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20     P4_INT32);.  
28012 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
28013 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65  TempRange(pParse
28014 2c 20 72 65 67 49 64 78 2c 20 70 49 64 78 2d 3e  , regIdx, pIdx->
28015 6e 43 6f 6c 75 6d 6e 2b 31 29 3b 0a 0a 20 20 20  nColumn+1);..   
28016 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
28017 65 20 74 68 61 74 20 65 78 65 63 75 74 65 73 20  e that executes 
28018 69 66 20 74 68 65 20 6e 65 77 20 69 6e 64 65 78  if the new index
28019 20 65 6e 74 72 79 20 69 73 20 6e 6f 74 20 75 6e   entry is not un
2801a 69 71 75 65 20 2a 2f 0a 20 20 20 20 61 73 73 65  ique */.    asse
2801b 72 74 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  rt( onError==OE_
2801c 52 6f 6c 6c 62 61 63 6b 20 7c 7c 20 6f 6e 45 72  Rollback || onEr
2801d 72 6f 72 3d 3d 4f 45 5f 41 62 6f 72 74 20 7c 7c  ror==OE_Abort ||
2801e 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 46 61 69   onError==OE_Fai
2801f 6c 0a 20 20 20 20 20 20 20 20 7c 7c 20 6f 6e 45  l.        || onE
28020 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20  rror==OE_Ignore 
28021 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52  || onError==OE_R
28022 65 70 6c 61 63 65 20 29 3b 0a 20 20 20 20 73 77  eplace );.    sw
28023 69 74 63 68 28 20 6f 6e 45 72 72 6f 72 20 29 7b  itch( onError ){
28024 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52  .      case OE_R
28025 6f 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20 20 63  ollback:.      c
28026 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a 20 20  ase OE_Abort:.  
28027 20 20 20 20 63 61 73 65 20 4f 45 5f 46 61 69 6c      case OE_Fail
28028 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  : {.        int 
28029 6a 3b 0a 20 20 20 20 20 20 20 20 53 74 72 41 63  j;.        StrAc
2802a 63 75 6d 20 65 72 72 4d 73 67 3b 0a 20 20 20 20  cum errMsg;.    
2802b 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2802c 7a 53 65 70 3b 0a 20 20 20 20 20 20 20 20 63 68  zSep;.        ch
2802d 61 72 20 2a 7a 45 72 72 3b 0a 0a 20 20 20 20 20  ar *zErr;..     
2802e 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
2802f 75 6d 49 6e 69 74 28 26 65 72 72 4d 73 67 2c 20  umInit(&errMsg, 
28030 30 2c 20 30 2c 20 32 30 30 29 3b 0a 20 20 20 20  0, 0, 200);.    
28031 20 20 20 20 65 72 72 4d 73 67 2e 64 62 20 3d 20      errMsg.db = 
28032 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20  pParse->db;.    
28033 20 20 20 20 7a 53 65 70 20 3d 20 70 49 64 78 2d      zSep = pIdx-
28034 3e 6e 43 6f 6c 75 6d 6e 3e 31 20 3f 20 22 63 6f  >nColumn>1 ? "co
28035 6c 75 6d 6e 73 20 22 20 3a 20 22 63 6f 6c 75 6d  lumns " : "colum
28036 6e 20 22 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  n ";.        for
28037 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43  (j=0; j<pIdx->nC
28038 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20  olumn; j++){.   
28039 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43 6f         char *zCo
2803a 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70  l = pTab->aCol[p
2803b 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d  Idx->aiColumn[j]
2803c 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20  ].zName;.       
2803d 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
2803e 75 6d 41 70 70 65 6e 64 28 26 65 72 72 4d 73 67  umAppend(&errMsg
2803f 2c 20 7a 53 65 70 2c 20 2d 31 29 3b 0a 20 20 20  , zSep, -1);.   
28040 20 20 20 20 20 20 20 7a 53 65 70 20 3d 20 22 2c         zSep = ",
28041 20 22 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71   ";.          sq
28042 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
28043 65 6e 64 28 26 65 72 72 4d 73 67 2c 20 7a 43 6f  end(&errMsg, zCo
28044 6c 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20 20  l, -1);.        
28045 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
28046 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
28047 26 65 72 72 4d 73 67 2c 0a 20 20 20 20 20 20 20  &errMsg,.       
28048 20 20 20 20 20 70 49 64 78 2d 3e 6e 43 6f 6c 75       pIdx->nColu
28049 6d 6e 3e 31 20 3f 20 22 20 61 72 65 20 6e 6f 74  mn>1 ? " are not
2804a 20 75 6e 69 71 75 65 22 20 3a 20 22 20 69 73 20   unique" : " is 
2804b 6e 6f 74 20 75 6e 69 71 75 65 22 2c 20 2d 31 29  not unique", -1)
2804c 3b 0a 20 20 20 20 20 20 20 20 7a 45 72 72 20 3d  ;.        zErr =
2804d 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
2804e 46 69 6e 69 73 68 28 26 65 72 72 4d 73 67 29 3b  Finish(&errMsg);
2804f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
28050 48 61 6c 74 43 6f 6e 73 74 72 61 69 6e 74 28 70  HaltConstraint(p
28051 50 61 72 73 65 2c 20 6f 6e 45 72 72 6f 72 2c 20  Parse, onError, 
28052 7a 45 72 72 2c 20 30 29 3b 0a 20 20 20 20 20 20  zErr, 0);.      
28053 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
28054 65 72 72 4d 73 67 2e 64 62 2c 20 7a 45 72 72 29  errMsg.db, zErr)
28055 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
28056 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
28057 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65 3a 20 7b  ase OE_Ignore: {
28058 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
28059 20 73 65 65 6e 52 65 70 6c 61 63 65 3d 3d 30 20   seenReplace==0 
2805a 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2805b 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2805c 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 67 6e 6f  OP_Goto, 0, igno
2805d 72 65 44 65 73 74 29 3b 0a 20 20 20 20 20 20 20  reDest);.       
2805e 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
2805f 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b        default: {
28060 0a 20 20 20 20 20 20 20 20 54 72 69 67 67 65 72  .        Trigger
28061 20 2a 70 54 72 69 67 67 65 72 20 3d 20 30 3b 0a   *pTrigger = 0;.
28062 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
28063 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c  onError==OE_Repl
28064 61 63 65 20 29 3b 0a 20 20 20 20 20 20 20 20 73  ace );.        s
28065 71 6c 69 74 65 33 4d 75 6c 74 69 57 72 69 74 65  qlite3MultiWrite
28066 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
28067 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62    if( pParse->db
28068 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52  ->flags&SQLITE_R
28069 65 63 54 72 69 67 67 65 72 73 20 29 7b 0a 20 20  ecTriggers ){.  
2806a 20 20 20 20 20 20 20 20 70 54 72 69 67 67 65 72          pTrigger
2806b 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
2806c 72 73 45 78 69 73 74 28 70 50 61 72 73 65 2c 20  rsExist(pParse, 
2806d 70 54 61 62 2c 20 54 4b 5f 44 45 4c 45 54 45 2c  pTab, TK_DELETE,
2806e 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   0, 0);.        
2806f 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
28070 33 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65  3GenerateRowDele
28071 74 65 28 0a 20 20 20 20 20 20 20 20 20 20 20 20  te(.            
28072 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 62 61  pParse, pTab, ba
28073 73 65 43 75 72 2c 20 72 65 67 52 2c 20 30 2c 20  seCur, regR, 0, 
28074 70 54 72 69 67 67 65 72 2c 20 4f 45 5f 52 65 70  pTrigger, OE_Rep
28075 6c 61 63 65 0a 20 20 20 20 20 20 20 20 29 3b 0a  lace.        );.
28076 20 20 20 20 20 20 20 20 73 65 65 6e 52 65 70 6c          seenRepl
28077 61 63 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ace = 1;.       
28078 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
28079 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2807a 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2807b 20 6a 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   j3);.    sqlite
2807c 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
2807d 70 50 61 72 73 65 2c 20 72 65 67 52 29 3b 0a 20  pParse, regR);. 
2807e 20 7d 0a 20 20 0a 20 20 69 66 28 20 70 62 4d 61   }.  .  if( pbMa
2807f 79 52 65 70 6c 61 63 65 20 29 7b 0a 20 20 20 20  yReplace ){.    
28080 2a 70 62 4d 61 79 52 65 70 6c 61 63 65 20 3d 20  *pbMayReplace = 
28081 73 65 65 6e 52 65 70 6c 61 63 65 3b 0a 20 20 7d  seenReplace;.  }
28082 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
28083 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74 65 73  outine generates
28084 20 63 6f 64 65 20 74 6f 20 66 69 6e 69 73 68 20   code to finish 
28085 74 68 65 20 49 4e 53 45 52 54 20 6f 72 20 55 50  the INSERT or UP
28086 44 41 54 45 20 6f 70 65 72 61 74 69 6f 6e 0a 2a  DATE operation.*
28087 2a 20 74 68 61 74 20 77 61 73 20 73 74 61 72 74  * that was start
28088 65 64 20 62 79 20 61 20 70 72 69 6f 72 20 63 61  ed by a prior ca
28089 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 47 65 6e  ll to sqlite3Gen
2808a 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43  erateConstraintC
2808b 68 65 63 6b 73 2e 0a 2a 2a 20 41 20 63 6f 6e 73  hecks..** A cons
2808c 65 63 75 74 69 76 65 20 72 61 6e 67 65 20 6f 66  ecutive range of
2808d 20 72 65 67 69 73 74 65 72 73 20 73 74 61 72 74   registers start
2808e 69 6e 67 20 61 74 20 72 65 67 52 6f 77 69 64 20  ing at regRowid 
2808f 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20  contains the.** 
28090 72 6f 77 69 64 20 61 6e 64 20 74 68 65 20 63 6f  rowid and the co
28091 6e 74 65 6e 74 20 74 6f 20 62 65 20 69 6e 73 65  ntent to be inse
28092 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  rted..**.** The 
28093 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69  arguments to thi
28094 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64  s routine should
28095 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 20   be the same as 
28096 74 68 65 20 66 69 72 73 74 20 73 69 78 0a 2a 2a  the first six.**
28097 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 73 71   arguments to sq
28098 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e  lite3GenerateCon
28099 73 74 72 61 69 6e 74 43 68 65 63 6b 73 2e 0a 2a  straintChecks..*
2809a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2809b 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d   void sqlite3Com
2809c 70 6c 65 74 65 49 6e 73 65 72 74 69 6f 6e 28 0a  pleteInsertion(.
2809d 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2809e 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
2809f 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
280a0 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20   Table *pTab,   
280a1 20 20 20 20 20 2f 2a 20 74 68 65 20 74 61 62 6c       /* the tabl
280a2 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20  e into which we 
280a3 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f  are inserting */
280a4 0a 20 20 69 6e 74 20 62 61 73 65 43 75 72 2c 20  .  int baseCur, 
280a5 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
280a6 6f 66 20 61 20 72 65 61 64 2f 77 72 69 74 65 20  of a read/write 
280a7 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20  cursor pointing 
280a8 61 74 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74  at pTab */.  int
280a9 20 72 65 67 52 6f 77 69 64 2c 20 20 20 20 20 20   regRowid,      
280aa 20 2f 2a 20 52 61 6e 67 65 20 6f 66 20 63 6f 6e   /* Range of con
280ab 74 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 61  tent */.  int *a
280ac 52 65 67 49 64 78 2c 20 20 20 20 20 20 20 2f 2a  RegIdx,       /*
280ad 20 52 65 67 69 73 74 65 72 20 75 73 65 64 20 62   Register used b
280ae 79 20 65 61 63 68 20 69 6e 64 65 78 2e 20 20 30  y each index.  0
280af 20 66 6f 72 20 75 6e 75 73 65 64 20 69 6e 64 69   for unused indi
280b0 63 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 73 55  ces */.  int isU
280b1 70 64 61 74 65 2c 20 20 20 20 20 20 20 2f 2a 20  pdate,       /* 
280b2 54 72 75 65 20 66 6f 72 20 55 50 44 41 54 45 2c  True for UPDATE,
280b3 20 46 61 6c 73 65 20 66 6f 72 20 49 4e 53 45 52   False for INSER
280b4 54 20 2a 2f 0a 20 20 69 6e 74 20 61 70 70 65 6e  T */.  int appen
280b5 64 42 69 61 73 2c 20 20 20 20 20 2f 2a 20 54 72  dBias,     /* Tr
280b6 75 65 20 69 66 20 74 68 69 73 20 69 73 20 6c 69  ue if this is li
280b7 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70  kely to be an ap
280b8 70 65 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 75 73  pend */.  int us
280b9 65 53 65 65 6b 52 65 73 75 6c 74 20 20 20 2f 2a  eSeekResult   /*
280ba 20 54 72 75 65 20 74 6f 20 73 65 74 20 74 68 65   True to set the
280bb 20 55 53 45 53 45 45 4b 52 45 53 55 4c 54 20 66   USESEEKRESULT f
280bc 6c 61 67 20 6f 6e 20 4f 50 5f 5b 49 64 78 5d 49  lag on OP_[Idx]I
280bd 6e 73 65 72 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e  nsert */.){.  in
280be 74 20 69 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a  t i;.  Vdbe *v;.
280bf 20 20 69 6e 74 20 6e 49 64 78 3b 0a 20 20 49 6e    int nIdx;.  In
280c0 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 75 38 20  dex *pIdx;.  u8 
280c1 70 69 6b 5f 66 6c 61 67 73 3b 0a 20 20 69 6e 74  pik_flags;.  int
280c2 20 72 65 67 44 61 74 61 3b 0a 20 20 69 6e 74 20   regData;.  int 
280c3 72 65 67 52 65 63 3b 0a 0a 20 20 76 20 3d 20 73  regRec;..  v = s
280c4 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
280c5 61 72 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28  arse);.  assert(
280c6 20 76 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72   v!=0 );.  asser
280c7 74 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74  t( pTab->pSelect
280c8 3d 3d 30 20 29 3b 20 20 2f 2a 20 54 68 69 73 20  ==0 );  /* This 
280c9 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 61 20 56  table is not a V
280ca 49 45 57 20 2a 2f 0a 20 20 66 6f 72 28 6e 49 64  IEW */.  for(nId
280cb 78 3d 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e  x=0, pIdx=pTab->
280cc 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
280cd 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20  dx=pIdx->pNext, 
280ce 6e 49 64 78 2b 2b 29 7b 7d 0a 20 20 66 6f 72 28  nIdx++){}.  for(
280cf 69 3d 6e 49 64 78 2d 31 3b 20 69 3e 3d 30 3b 20  i=nIdx-1; i>=0; 
280d0 69 2d 2d 29 7b 0a 20 20 20 20 69 66 28 20 61 52  i--){.    if( aR
280d1 65 67 49 64 78 5b 69 5d 3d 3d 30 20 29 20 63 6f  egIdx[i]==0 ) co
280d2 6e 74 69 6e 75 65 3b 0a 20 20 20 20 73 71 6c 69  ntinue;.    sqli
280d3 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
280d4 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 62   OP_IdxInsert, b
280d5 61 73 65 43 75 72 2b 69 2b 31 2c 20 61 52 65 67  aseCur+i+1, aReg
280d6 49 64 78 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28  Idx[i]);.    if(
280d7 20 75 73 65 53 65 65 6b 52 65 73 75 6c 74 20 29   useSeekResult )
280d8 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
280d9 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f  dbeChangeP5(v, O
280da 50 46 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53  PFLAG_USESEEKRES
280db 55 4c 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ULT);.    }.  }.
280dc 20 20 72 65 67 44 61 74 61 20 3d 20 72 65 67 52    regData = regR
280dd 6f 77 69 64 20 2b 20 31 3b 0a 20 20 72 65 67 52  owid + 1;.  regR
280de 65 63 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  ec = sqlite3GetT
280df 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
280e0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
280e1 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op3(v, OP_MakeRe
280e2 63 6f 72 64 2c 20 72 65 67 44 61 74 61 2c 20 70  cord, regData, p
280e3 54 61 62 2d 3e 6e 43 6f 6c 2c 20 72 65 67 52 65  Tab->nCol, regRe
280e4 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 54 61 62  c);.  sqlite3Tab
280e5 6c 65 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c  leAffinityStr(v,
280e6 20 70 54 61 62 29 3b 0a 20 20 73 71 6c 69 74 65   pTab);.  sqlite
280e7 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69  3ExprCacheAffini
280e8 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c  tyChange(pParse,
280e9 20 72 65 67 44 61 74 61 2c 20 70 54 61 62 2d 3e   regData, pTab->
280ea 6e 43 6f 6c 29 3b 0a 20 20 69 66 28 20 70 50 61  nCol);.  if( pPa
280eb 72 73 65 2d 3e 6e 65 73 74 65 64 20 29 7b 0a 20  rse->nested ){. 
280ec 20 20 20 70 69 6b 5f 66 6c 61 67 73 20 3d 20 30     pik_flags = 0
280ed 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
280ee 69 6b 5f 66 6c 61 67 73 20 3d 20 4f 50 46 4c 41  ik_flags = OPFLA
280ef 47 5f 4e 43 48 41 4e 47 45 3b 0a 20 20 20 20 70  G_NCHANGE;.    p
280f0 69 6b 5f 66 6c 61 67 73 20 7c 3d 20 28 69 73 55  ik_flags |= (isU
280f1 70 64 61 74 65 3f 4f 50 46 4c 41 47 5f 49 53 55  pdate?OPFLAG_ISU
280f2 50 44 41 54 45 3a 4f 50 46 4c 41 47 5f 4c 41 53  PDATE:OPFLAG_LAS
280f3 54 52 4f 57 49 44 29 3b 0a 20 20 7d 0a 20 20 69  TROWID);.  }.  i
280f4 66 28 20 61 70 70 65 6e 64 42 69 61 73 20 29 7b  f( appendBias ){
280f5 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67 73 20 7c  .    pik_flags |
280f6 3d 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 3b  = OPFLAG_APPEND;
280f7 0a 20 20 7d 0a 20 20 69 66 28 20 75 73 65 53 65  .  }.  if( useSe
280f8 65 6b 52 65 73 75 6c 74 20 29 7b 0a 20 20 20 20  ekResult ){.    
280f9 70 69 6b 5f 66 6c 61 67 73 20 7c 3d 20 4f 50 46  pik_flags |= OPF
280fa 4c 41 47 5f 55 53 45 53 45 45 4b 52 45 53 55 4c  LAG_USESEEKRESUL
280fb 54 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  T;.  }.  sqlite3
280fc 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
280fd 5f 49 6e 73 65 72 74 2c 20 62 61 73 65 43 75 72  _Insert, baseCur
280fe 2c 20 72 65 67 52 65 63 2c 20 72 65 67 52 6f 77  , regRec, regRow
280ff 69 64 29 3b 0a 20 20 69 66 28 20 21 70 50 61 72  id);.  if( !pPar
28100 73 65 2d 3e 6e 65 73 74 65 64 20 29 7b 0a 20 20  se->nested ){.  
28101 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
28102 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 54 61  ngeP4(v, -1, pTa
28103 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41  b->zName, P4_STA
28104 54 49 43 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  TIC);.  }.  sqli
28105 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
28106 76 2c 20 70 69 6b 5f 66 6c 61 67 73 29 3b 0a 7d  v, pik_flags);.}
28107 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
28108 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20   code that will 
28109 6f 70 65 6e 20 63 75 72 73 6f 72 73 20 66 6f 72  open cursors for
2810a 20 61 20 74 61 62 6c 65 20 61 6e 64 20 66 6f 72   a table and for
2810b 20 61 6c 6c 0a 2a 2a 20 69 6e 64 69 63 65 73 20   all.** indices 
2810c 6f 66 20 74 68 61 74 20 74 61 62 6c 65 2e 20 20  of that table.  
2810d 54 68 65 20 22 62 61 73 65 43 75 72 22 20 70 61  The "baseCur" pa
2810e 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 63  rameter is the c
2810f 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 75 73 65  ursor number use
28110 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62  d.** for the tab
28111 6c 65 2e 20 20 49 6e 64 69 63 65 73 20 61 72 65  le.  Indices are
28112 20 6f 70 65 6e 65 64 20 6f 6e 20 73 75 62 73 65   opened on subse
28113 71 75 65 6e 74 20 63 75 72 73 6f 72 73 2e 0a 2a  quent cursors..*
28114 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
28115 6e 75 6d 62 65 72 20 6f 66 20 69 6e 64 69 63 65  number of indice
28116 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a  s on the table..
28117 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
28118 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65  E int sqlite3Ope
28119 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 63 65 73  nTableAndIndices
2811a 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2811b 65 2c 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20  e,   /* Parsing 
2811c 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62  context */.  Tab
2811d 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a  le *pTab,     /*
2811e 20 54 61 62 6c 65 20 74 6f 20 62 65 20 6f 70 65   Table to be ope
2811f 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73  ned */.  int bas
28120 65 43 75 72 2c 20 20 20 20 20 2f 2a 20 43 75 72  eCur,     /* Cur
28121 73 6f 72 20 6e 75 6d 62 65 72 20 61 73 73 69 67  sor number assig
28122 6e 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65  ned to the table
28123 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 20 20 20 20   */.  int op    
28124 20 20 20 20 20 20 20 2f 2a 20 4f 50 5f 4f 70 65         /* OP_Ope
28125 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70 65 6e  nRead or OP_Open
28126 57 72 69 74 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e  Write */.){.  in
28127 74 20 69 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a  t i;.  int iDb;.
28128 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20    Index *pIdx;. 
28129 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 69 66 28   Vdbe *v;..  if(
2812a 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
2812b 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
2812c 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
2812d 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65  maToIndex(pParse
2812e 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  ->db, pTab->pSch
2812f 65 6d 61 29 3b 0a 20 20 76 20 3d 20 73 71 6c 69  ema);.  v = sqli
28130 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
28131 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21  e);.  assert( v!
28132 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f  =0 );.  sqlite3O
28133 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c  penTable(pParse,
28134 20 62 61 73 65 43 75 72 2c 20 69 44 62 2c 20 70   baseCur, iDb, p
28135 54 61 62 2c 20 6f 70 29 3b 0a 20 20 66 6f 72 28  Tab, op);.  for(
28136 69 3d 31 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e  i=1, pIdx=pTab->
28137 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
28138 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20  dx=pIdx->pNext, 
28139 69 2b 2b 29 7b 0a 20 20 20 20 4b 65 79 49 6e 66  i++){.    KeyInf
2813a 6f 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65  o *pKey = sqlite
2813b 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50  3IndexKeyinfo(pP
2813c 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 20  arse, pIdx);.   
2813d 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e 70   assert( pIdx->p
2813e 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70 53  Schema==pTab->pS
2813f 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 73 71 6c  chema );.    sql
28140 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
28141 2c 20 6f 70 2c 20 69 2b 62 61 73 65 43 75 72 2c  , op, i+baseCur,
28142 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62   pIdx->tnum, iDb
28143 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
28144 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
28145 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  Key, P4_KEYINFO_
28146 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56 64  HANDOFF);.    Vd
28147 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25  beComment((v, "%
28148 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29  s", pIdx->zName)
28149 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  );.  }.  if( pPa
2814a 72 73 65 2d 3e 6e 54 61 62 3c 62 61 73 65 43 75  rse->nTab<baseCu
2814b 72 2b 69 20 29 7b 0a 20 20 20 20 70 50 61 72 73  r+i ){.    pPars
2814c 65 2d 3e 6e 54 61 62 20 3d 20 62 61 73 65 43 75  e->nTab = baseCu
2814d 72 2b 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  r+i;.  }.  retur
2814e 6e 20 69 2d 31 3b 0a 7d 0a 0a 0a 23 69 66 64 65  n i-1;.}...#ifde
2814f 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a  f SQLITE_TEST./*
28150 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
28151 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  g global variabl
28152 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  e is incremented
28153 20 77 68 65 6e 65 76 65 72 20 74 68 65 0a 2a 2a   whenever the.**
28154 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69   transfer optimi
28155 7a 61 74 69 6f 6e 20 69 73 20 75 73 65 64 2e 20  zation is used. 
28156 20 54 68 69 73 20 69 73 20 75 73 65 64 20 66 6f   This is used fo
28157 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20 70 75 72  r testing.** pur
28158 70 6f 73 65 73 20 6f 6e 6c 79 20 2d 20 74 6f 20  poses only - to 
28159 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 74 72  make sure the tr
2815a 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74  ansfer optimizat
2815b 69 6f 6e 20 72 65 61 6c 6c 79 0a 2a 2a 20 69 73  ion really.** is
2815c 20 68 61 70 70 65 6e 69 6e 67 20 77 68 65 6e 20   happening when 
2815d 69 74 20 69 73 20 73 75 70 70 6f 73 65 20 74 6f  it is suppose to
2815e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2815f 69 6e 74 20 73 71 6c 69 74 65 33 5f 78 66 65 72  int sqlite3_xfer
28160 6f 70 74 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69  opt_count;.#endi
28161 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54 45 53 54  f /* SQLITE_TEST
28162 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51   */...#ifndef SQ
28163 4c 49 54 45 5f 4f 4d 49 54 5f 58 46 45 52 5f 4f  LITE_OMIT_XFER_O
28164 50 54 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74  PT./*.** Check t
28165 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20 6e 61 6d 65  o collation name
28166 73 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 79  s to see if they
28167 20 61 72 65 20 63 6f 6d 70 61 74 69 62 6c 65 2e   are compatible.
28168 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 78  .*/.static int x
28169 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 43 6f 6c  ferCompatibleCol
2816a 6c 61 74 69 6f 6e 28 63 6f 6e 73 74 20 63 68 61  lation(const cha
2816b 72 20 2a 7a 31 2c 20 63 6f 6e 73 74 20 63 68 61  r *z1, const cha
2816c 72 20 2a 7a 32 29 7b 0a 20 20 69 66 28 20 7a 31  r *z2){.  if( z1
2816d 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
2816e 6e 20 7a 32 3d 3d 30 3b 0a 20 20 7d 0a 20 20 69  n z2==0;.  }.  i
2816f 66 28 20 7a 32 3d 3d 30 20 29 7b 0a 20 20 20 20  f( z2==0 ){.    
28170 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
28171 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 53 74  return sqlite3St
28172 72 49 43 6d 70 28 7a 31 2c 20 7a 32 29 3d 3d 30  rICmp(z1, z2)==0
28173 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  ;.}.../*.** Chec
28174 6b 20 74 6f 20 73 65 65 20 69 66 20 69 6e 64 65  k to see if inde
28175 78 20 70 53 72 63 20 69 73 20 63 6f 6d 70 61 74  x pSrc is compat
28176 69 62 6c 65 20 61 73 20 61 20 73 6f 75 72 63 65  ible as a source
28177 20 6f 66 20 64 61 74 61 0a 2a 2a 20 66 6f 72 20   of data.** for 
28178 69 6e 64 65 78 20 70 44 65 73 74 20 69 6e 20 61  index pDest in a
28179 6e 20 69 6e 73 65 72 74 20 74 72 61 6e 73 66 65  n insert transfe
2817a 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20  r optimization. 
2817b 20 54 68 65 20 72 75 6c 65 73 0a 2a 2a 20 66 6f   The rules.** fo
2817c 72 20 61 20 63 6f 6d 70 61 74 69 62 6c 65 20 69  r a compatible i
2817d 6e 64 65 78 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a  ndex:.**.**    *
2817e 20 20 20 54 68 65 20 69 6e 64 65 78 20 69 73 20     The index is 
2817f 6f 76 65 72 20 74 68 65 20 73 61 6d 65 20 73 65  over the same se
28180 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20  t of columns.** 
28181 20 20 20 2a 20 20 20 54 68 65 20 73 61 6d 65 20     *   The same 
28182 44 45 53 43 20 61 6e 64 20 41 53 43 20 6d 61 72  DESC and ASC mar
28183 6b 69 6e 67 73 20 6f 63 63 75 72 73 20 6f 6e 20  kings occurs on 
28184 61 6c 6c 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 20  all columns.**  
28185 20 20 2a 20 20 20 54 68 65 20 73 61 6d 65 20 6f    *   The same o
28186 6e 45 72 72 6f 72 20 70 72 6f 63 65 73 73 69 6e  nError processin
28187 67 20 28 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f  g (OE_Abort, OE_
28188 49 67 6e 6f 72 65 2c 20 65 74 63 29 0a 2a 2a 20  Ignore, etc).** 
28189 20 20 20 2a 20 20 20 54 68 65 20 73 61 6d 65 20     *   The same 
2818a 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
2818b 63 65 20 6f 6e 20 65 61 63 68 20 63 6f 6c 75 6d  ce on each colum
2818c 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  n.*/.static int 
2818d 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 49 6e  xferCompatibleIn
2818e 64 65 78 28 49 6e 64 65 78 20 2a 70 44 65 73 74  dex(Index *pDest
2818f 2c 20 49 6e 64 65 78 20 2a 70 53 72 63 29 7b 0a  , Index *pSrc){.
28190 20 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72    int i;.  asser
28191 74 28 20 70 44 65 73 74 20 26 26 20 70 53 72 63  t( pDest && pSrc
28192 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44   );.  assert( pD
28193 65 73 74 2d 3e 70 54 61 62 6c 65 21 3d 70 53 72  est->pTable!=pSr
28194 63 2d 3e 70 54 61 62 6c 65 20 29 3b 0a 20 20 69  c->pTable );.  i
28195 66 28 20 70 44 65 73 74 2d 3e 6e 43 6f 6c 75 6d  f( pDest->nColum
28196 6e 21 3d 70 53 72 63 2d 3e 6e 43 6f 6c 75 6d 6e  n!=pSrc->nColumn
28197 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
28198 3b 20 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74  ;   /* Different
28199 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
2819a 6e 73 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  ns */.  }.  if( 
2819b 70 44 65 73 74 2d 3e 6f 6e 45 72 72 6f 72 21 3d  pDest->onError!=
2819c 70 53 72 63 2d 3e 6f 6e 45 72 72 6f 72 20 29 7b  pSrc->onError ){
2819d 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
2819e 20 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 63 6f   /* Different co
2819f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75 74 69 6f  nflict resolutio
281a0 6e 20 73 74 72 61 74 65 67 69 65 73 20 2a 2f 0a  n strategies */.
281a1 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69    }.  for(i=0; i
281a2 3c 70 53 72 63 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  <pSrc->nColumn; 
281a3 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 53  i++){.    if( pS
281a4 72 63 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 21  rc->aiColumn[i]!
281a5 3d 70 44 65 73 74 2d 3e 61 69 43 6f 6c 75 6d 6e  =pDest->aiColumn
281a6 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 72 65 74  [i] ){.      ret
281a7 75 72 6e 20 30 3b 20 20 20 2f 2a 20 44 69 66 66  urn 0;   /* Diff
281a8 65 72 65 6e 74 20 63 6f 6c 75 6d 6e 73 20 69 6e  erent columns in
281a9 64 65 78 65 64 20 2a 2f 0a 20 20 20 20 7d 0a 20  dexed */.    }. 
281aa 20 20 20 69 66 28 20 70 53 72 63 2d 3e 61 53 6f     if( pSrc->aSo
281ab 72 74 4f 72 64 65 72 5b 69 5d 21 3d 70 44 65 73  rtOrder[i]!=pDes
281ac 74 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d  t->aSortOrder[i]
281ad 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
281ae 20 30 3b 20 20 20 2f 2a 20 44 69 66 66 65 72 65   0;   /* Differe
281af 6e 74 20 73 6f 72 74 20 6f 72 64 65 72 73 20 2a  nt sort orders *
281b0 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  /.    }.    if( 
281b1 21 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65 43  !xferCompatibleC
281b2 6f 6c 6c 61 74 69 6f 6e 28 70 53 72 63 2d 3e 61  ollation(pSrc->a
281b3 7a 43 6f 6c 6c 5b 69 5d 2c 70 44 65 73 74 2d 3e  zColl[i],pDest->
281b4 61 7a 43 6f 6c 6c 5b 69 5d 29 20 29 7b 0a 20 20  azColl[i]) ){.  
281b5 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
281b6 2f 2a 20 44 69 66 66 65 72 65 6e 74 20 63 6f 6c  /* Different col
281b7 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73  lating sequences
281b8 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   */.    }.  }.. 
281b9 20 2f 2a 20 49 66 20 6e 6f 20 74 65 73 74 20 61   /* If no test a
281ba 62 6f 76 65 20 66 61 69 6c 73 20 74 68 65 6e 20  bove fails then 
281bb 74 68 65 20 69 6e 64 69 63 65 73 20 6d 75 73 74  the indices must
281bc 20 62 65 20 63 6f 6d 70 61 74 69 62 6c 65 20 2a   be compatible *
281bd 2f 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a  /.  return 1;.}.
281be 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74  ./*.** Attempt t
281bf 68 65 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69  he transfer opti
281c0 6d 69 7a 61 74 69 6f 6e 20 6f 6e 20 49 4e 53 45  mization on INSE
281c1 52 54 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a  RTs of the form.
281c2 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53 45 52 54  **.**     INSERT
281c3 20 49 4e 54 4f 20 74 61 62 31 20 53 45 4c 45 43   INTO tab1 SELEC
281c4 54 20 2a 20 46 52 4f 4d 20 74 61 62 32 3b 0a 2a  T * FROM tab2;.*
281c5 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 74 69 6d 69  *.** This optimi
281c6 7a 61 74 69 6f 6e 20 69 73 20 6f 6e 6c 79 20 61  zation is only a
281c7 74 74 65 6d 70 74 65 64 20 69 66 0a 2a 2a 0a 2a  ttempted if.**.*
281c8 2a 20 20 20 20 28 31 29 20 20 74 61 62 31 20 61  *    (1)  tab1 a
281c9 6e 64 20 74 61 62 32 20 68 61 76 65 20 69 64 65  nd tab2 have ide
281ca 6e 74 69 63 61 6c 20 73 63 68 65 6d 61 73 20 69  ntical schemas i
281cb 6e 63 6c 75 64 69 6e 67 20 61 6c 6c 20 74 68 65  ncluding all the
281cc 0a 2a 2a 20 20 20 20 20 20 20 20 20 73 61 6d 65  .**         same
281cd 20 69 6e 64 69 63 65 73 20 61 6e 64 20 63 6f 6e   indices and con
281ce 73 74 72 61 69 6e 74 73 0a 2a 2a 0a 2a 2a 20 20  straints.**.**  
281cf 20 20 28 32 29 20 20 74 61 62 31 20 61 6e 64 20    (2)  tab1 and 
281d0 74 61 62 32 20 61 72 65 20 64 69 66 66 65 72 65  tab2 are differe
281d1 6e 74 20 74 61 62 6c 65 73 0a 2a 2a 0a 2a 2a 20  nt tables.**.** 
281d2 20 20 20 28 33 29 20 20 54 68 65 72 65 20 6d 75     (3)  There mu
281d3 73 74 20 62 65 20 6e 6f 20 74 72 69 67 67 65 72  st be no trigger
281d4 73 20 6f 6e 20 74 61 62 31 0a 2a 2a 0a 2a 2a 20  s on tab1.**.** 
281d5 20 20 20 28 34 29 20 20 54 68 65 20 72 65 73 75     (4)  The resu
281d6 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 53 45  lt set of the SE
281d7 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69  LECT statement i
281d8 73 20 22 2a 22 0a 2a 2a 0a 2a 2a 20 20 20 20 28  s "*".**.**    (
281d9 35 29 20 20 54 68 65 20 53 45 4c 45 43 54 20 73  5)  The SELECT s
281da 74 61 74 65 6d 65 6e 74 20 68 61 73 20 6e 6f 20  tatement has no 
281db 57 48 45 52 45 2c 20 48 41 56 49 4e 47 2c 20 4f  WHERE, HAVING, O
281dc 52 44 45 52 20 42 59 2c 20 47 52 4f 55 50 20 42  RDER BY, GROUP B
281dd 59 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 72  Y,.**         or
281de 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 0a 2a   LIMIT clause..*
281df 2a 0a 2a 2a 20 20 20 20 28 36 29 20 20 54 68 65  *.**    (6)  The
281e0 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
281e1 74 20 69 73 20 61 20 73 69 6d 70 6c 65 20 28 6e  t is a simple (n
281e2 6f 74 20 61 20 63 6f 6d 70 6f 75 6e 64 29 20 73  ot a compound) s
281e3 65 6c 65 63 74 20 74 68 61 74 0a 2a 2a 20 20 20  elect that.**   
281e4 20 20 20 20 20 20 63 6f 6e 74 61 69 6e 73 20 6f        contains o
281e5 6e 6c 79 20 74 61 62 32 20 69 6e 20 69 74 73 20  nly tab2 in its 
281e6 46 52 4f 4d 20 63 6c 61 75 73 65 0a 2a 2a 0a 2a  FROM clause.**.*
281e7 2a 20 54 68 69 73 20 6d 65 74 68 6f 64 20 66 6f  * This method fo
281e8 72 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74  r implementing t
281e9 68 65 20 49 4e 53 45 52 54 20 74 72 61 6e 73 66  he INSERT transf
281ea 65 72 73 20 72 61 77 20 72 65 63 6f 72 64 73 20  ers raw records 
281eb 66 72 6f 6d 0a 2a 2a 20 74 61 62 32 20 6f 76 65  from.** tab2 ove
281ec 72 20 74 6f 20 74 61 62 31 2e 20 20 54 68 65 20  r to tab1.  The 
281ed 63 6f 6c 75 6d 6e 73 20 61 72 65 20 6e 6f 74 20  columns are not 
281ee 64 65 63 6f 64 65 64 2e 20 20 52 61 77 20 72 65  decoded.  Raw re
281ef 63 6f 72 64 73 20 66 72 6f 6d 0a 2a 2a 20 74 68  cords from.** th
281f0 65 20 69 6e 64 69 63 65 73 20 6f 66 20 74 61 62  e indices of tab
281f1 32 20 61 72 65 20 74 72 61 6e 73 66 65 72 65 64  2 are transfered
281f2 20 74 6f 20 74 61 62 31 20 61 73 20 77 65 6c 6c   to tab1 as well
281f3 2e 20 20 49 6e 20 73 6f 20 64 6f 69 6e 67 2c 0a  .  In so doing,.
281f4 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67  ** the resulting
281f5 20 74 61 62 31 20 68 61 73 20 6d 75 63 68 20 6c   tab1 has much l
281f6 65 73 73 20 66 72 61 67 6d 65 6e 74 61 74 69 6f  ess fragmentatio
281f7 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  n..**.** This ro
281f8 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 54 52  utine returns TR
281f9 55 45 20 69 66 20 74 68 65 20 6f 70 74 69 6d 69  UE if the optimi
281fa 7a 61 74 69 6f 6e 20 69 73 20 61 74 74 65 6d 70  zation is attemp
281fb 74 65 64 2e 20 20 49 66 20 61 6e 79 0a 2a 2a 20  ted.  If any.** 
281fc 6f 66 20 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e  of the condition
281fd 73 20 61 62 6f 76 65 20 66 61 69 6c 20 73 6f 20  s above fail so 
281fe 74 68 61 74 20 74 68 65 20 6f 70 74 69 6d 69 7a  that the optimiz
281ff 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74  ation should not
28200 0a 2a 2a 20 62 65 20 61 74 74 65 6d 70 74 65 64  .** be attempted
28201 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  , then this rout
28202 69 6e 65 20 72 65 74 75 72 6e 73 20 46 41 4c 53  ine returns FALS
28203 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  E..*/.static int
28204 20 78 66 65 72 4f 70 74 69 6d 69 7a 61 74 69 6f   xferOptimizatio
28205 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  n(.  Parse *pPar
28206 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61  se,        /* Pa
28207 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
28208 20 20 54 61 62 6c 65 20 2a 70 44 65 73 74 2c 20    Table *pDest, 
28209 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
2820a 61 62 6c 65 20 77 65 20 61 72 65 20 69 6e 73 65  able we are inse
2820b 72 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20  rting into */.  
2820c 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c  Select *pSelect,
2820d 20 20 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43        /* A SELEC
2820e 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 75  T statement to u
2820f 73 65 20 61 73 20 74 68 65 20 64 61 74 61 20 73  se as the data s
28210 6f 75 72 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6f  ource */.  int o
28211 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20  nError,         
28212 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c   /* How to handl
28213 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 72 72  e constraint err
28214 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ors */.  int iDb
28215 44 65 73 74 20 20 20 20 20 20 20 20 20 20 20 2f  Dest           /
28216 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6f  * The database o
28217 66 20 70 44 65 73 74 20 2a 2f 0a 29 7b 0a 20 20  f pDest */.){.  
28218 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
28219 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2821a 20 2f 2a 20 54 68 65 20 72 65 73 75 6c 74 20 73   /* The result s
2821b 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  et of the SELECT
2821c 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 53 72   */.  Table *pSr
2821d 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
2821e 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
2821f 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ble in the FROM 
28220 63 6c 61 75 73 65 20 6f 66 20 53 45 4c 45 43 54  clause of SELECT
28221 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 53 72   */.  Index *pSr
28222 63 49 64 78 2c 20 2a 70 44 65 73 74 49 64 78 3b  cIdx, *pDestIdx;
28223 20 20 20 20 20 20 20 2f 2a 20 53 6f 75 72 63 65         /* Source
28224 20 61 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e   and destination
28225 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 73 74   indices */.  st
28226 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
28227 6d 20 2a 70 49 74 65 6d 3b 20 20 20 20 20 20 2f  m *pItem;      /
28228 2a 20 41 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20  * An element of 
28229 70 53 65 6c 65 63 74 2d 3e 70 53 72 63 20 2a 2f  pSelect->pSrc */
2822a 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
2822b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2822c 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2822d 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ter */.  int iDb
2822e 53 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  Src;            
2822f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
28230 20 64 61 74 61 62 61 73 65 20 6f 66 20 70 53 72   database of pSr
28231 63 20 2a 2f 0a 20 20 69 6e 74 20 69 53 72 63 2c  c */.  int iSrc,
28232 20 69 44 65 73 74 3b 20 20 20 20 20 20 20 20 20   iDest;         
28233 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f          /* Curso
28234 72 73 20 66 72 6f 6d 20 73 6f 75 72 63 65 20 61  rs from source a
28235 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 2a  nd destination *
28236 2f 0a 20 20 69 6e 74 20 61 64 64 72 31 2c 20 61  /.  int addr1, a
28237 64 64 72 32 3b 20 20 20 20 20 20 20 20 20 20 20  ddr2;           
28238 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 61 64 64       /* Loop add
28239 72 65 73 73 65 73 20 2a 2f 0a 20 20 69 6e 74 20  resses */.  int 
2823a 65 6d 70 74 79 44 65 73 74 54 65 73 74 3b 20 20  emptyDestTest;  
2823b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2823c 41 64 64 72 65 73 73 20 6f 66 20 74 65 73 74 20  Address of test 
2823d 66 6f 72 20 65 6d 70 74 79 20 70 44 65 73 74 20  for empty pDest 
2823e 2a 2f 0a 20 20 69 6e 74 20 65 6d 70 74 79 53 72  */.  int emptySr
2823f 63 54 65 73 74 3b 20 20 20 20 20 20 20 20 20 20  cTest;          
28240 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
28241 20 6f 66 20 74 65 73 74 20 66 6f 72 20 65 6d 70   of test for emp
28242 74 79 20 70 53 72 63 20 2a 2f 0a 20 20 56 64 62  ty pSrc */.  Vdb
28243 65 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20  e *v;           
28244 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
28245 20 54 68 65 20 56 44 42 45 20 77 65 20 61 72 65   The VDBE we are
28246 20 62 75 69 6c 64 69 6e 67 20 2a 2f 0a 20 20 4b   building */.  K
28247 65 79 49 6e 66 6f 20 2a 70 4b 65 79 3b 20 20 20  eyInfo *pKey;   
28248 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28249 2f 2a 20 4b 65 79 20 69 6e 66 6f 72 6d 61 74 69  /* Key informati
2824a 6f 6e 20 66 6f 72 20 61 6e 20 69 6e 64 65 78 20  on for an index 
2824b 2a 2f 0a 20 20 69 6e 74 20 72 65 67 41 75 74 6f  */.  int regAuto
2824c 69 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  inc;            
2824d 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20        /* Memory 
2824e 72 65 67 69 73 74 65 72 20 75 73 65 64 20 62 79  register used by
2824f 20 41 55 54 4f 49 4e 43 20 2a 2f 0a 20 20 69 6e   AUTOINC */.  in
28250 74 20 64 65 73 74 48 61 73 55 6e 69 71 75 65 49  t destHasUniqueI
28251 64 78 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  dx = 0;        /
28252 2a 20 54 72 75 65 20 69 66 20 70 44 65 73 74 20  * True if pDest 
28253 68 61 73 20 61 20 55 4e 49 51 55 45 20 69 6e 64  has a UNIQUE ind
28254 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44  ex */.  int regD
28255 61 74 61 2c 20 72 65 67 52 6f 77 69 64 3b 20 20  ata, regRowid;  
28256 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69           /* Regi
28257 73 74 65 72 73 20 68 6f 6c 64 69 6e 67 20 64 61  sters holding da
28258 74 61 20 61 6e 64 20 72 6f 77 69 64 20 2a 2f 0a  ta and rowid */.
28259 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 3d 3d  .  if( pSelect==
2825a 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
2825b 30 3b 20 20 20 2f 2a 20 4d 75 73 74 20 62 65 20  0;   /* Must be 
2825c 6f 66 20 74 68 65 20 66 6f 72 6d 20 20 49 4e 53  of the form  INS
2825d 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53 45 4c  ERT INTO ... SEL
2825e 45 43 54 20 2e 2e 2e 20 2a 2f 0a 20 20 7d 0a 20  ECT ... */.  }. 
2825f 20 69 66 28 20 73 71 6c 69 74 65 33 54 72 69 67   if( sqlite3Trig
28260 67 65 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20  gerList(pParse, 
28261 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20 72 65  pDest) ){.    re
28262 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62  turn 0;   /* tab
28263 31 20 6d 75 73 74 20 6e 6f 74 20 68 61 76 65 20  1 must not have 
28264 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 7d 0a  triggers */.  }.
28265 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
28266 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
28267 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 74 61  .  if( pDest->ta
28268 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74  bFlags & TF_Virt
28269 75 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ual ){.    retur
2826a 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 31 20 6d  n 0;   /* tab1 m
2826b 75 73 74 20 6e 6f 74 20 62 65 20 61 20 76 69 72  ust not be a vir
2826c 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
2826d 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 6f  }.#endif.  if( o
2826e 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44 65 66 61 75  nError==OE_Defau
2826f 6c 74 20 29 7b 0a 20 20 20 20 6f 6e 45 72 72 6f  lt ){.    onErro
28270 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20  r = OE_Abort;.  
28271 7d 0a 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 21  }.  if( onError!
28272 3d 4f 45 5f 41 62 6f 72 74 20 26 26 20 6f 6e 45  =OE_Abort && onE
28273 72 72 6f 72 21 3d 4f 45 5f 52 6f 6c 6c 62 61 63  rror!=OE_Rollbac
28274 6b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  k ){.    return 
28275 30 3b 20 20 20 2f 2a 20 43 61 6e 6e 6f 74 20 64  0;   /* Cannot d
28276 6f 20 4f 52 20 52 45 50 4c 41 43 45 20 6f 72 20  o OR REPLACE or 
28277 4f 52 20 49 47 4e 4f 52 45 20 6f 72 20 4f 52 20  OR IGNORE or OR 
28278 46 41 49 4c 20 2a 2f 0a 20 20 7d 0a 20 20 61 73  FAIL */.  }.  as
28279 73 65 72 74 28 70 53 65 6c 65 63 74 2d 3e 70 53  sert(pSelect->pS
2827a 72 63 29 3b 20 20 20 2f 2a 20 61 6c 6c 6f 63 61  rc);   /* alloca
2827b 74 65 64 20 65 76 65 6e 20 69 66 20 74 68 65 72  ted even if ther
2827c 65 20 69 73 20 6e 6f 20 46 52 4f 4d 20 63 6c 61  e is no FROM cla
2827d 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 53 65  use */.  if( pSe
2827e 6c 65 63 74 2d 3e 70 53 72 63 2d 3e 6e 53 72 63  lect->pSrc->nSrc
2827f 21 3d 31 20 29 7b 0a 20 20 20 20 72 65 74 75 72  !=1 ){.    retur
28280 6e 20 30 3b 20 20 20 2f 2a 20 46 52 4f 4d 20 63  n 0;   /* FROM c
28281 6c 61 75 73 65 20 6d 75 73 74 20 68 61 76 65 20  lause must have 
28282 65 78 61 63 74 6c 79 20 6f 6e 65 20 74 65 72 6d  exactly one term
28283 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 53   */.  }.  if( pS
28284 65 6c 65 63 74 2d 3e 70 53 72 63 2d 3e 61 5b 30  elect->pSrc->a[0
28285 5d 2e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20  ].pSelect ){.   
28286 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
28287 46 52 4f 4d 20 63 6c 61 75 73 65 20 63 61 6e 6e  FROM clause cann
28288 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 73 75 62  ot contain a sub
28289 71 75 65 72 79 20 2a 2f 0a 20 20 7d 0a 20 20 69  query */.  }.  i
2828a 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 57 68 65  f( pSelect->pWhe
2828b 72 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  re ){.    return
2828c 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20   0;   /* SELECT 
2828d 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61 20 57  may not have a W
2828e 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
2828f 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74   }.  if( pSelect
28290 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ->pOrderBy ){.  
28291 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
28292 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20   SELECT may not 
28293 68 61 76 65 20 61 6e 20 4f 52 44 45 52 20 42 59  have an ORDER BY
28294 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20   clause */.  }. 
28295 20 2f 2a 20 44 6f 20 6e 6f 74 20 6e 65 65 64 20   /* Do not need 
28296 74 6f 20 74 65 73 74 20 66 6f 72 20 61 20 48 41  to test for a HA
28297 56 49 4e 47 20 63 6c 61 75 73 65 2e 20 20 49 66  VING clause.  If
28298 20 48 41 56 49 4e 47 20 69 73 20 70 72 65 73 65   HAVING is prese
28299 6e 74 20 62 75 74 0a 20 20 2a 2a 20 74 68 65 72  nt but.  ** ther
2829a 65 20 69 73 20 6e 6f 20 4f 52 44 45 52 20 42 59  e is no ORDER BY
2829b 2c 20 77 65 20 77 69 6c 6c 20 67 65 74 20 61 6e  , we will get an
2829c 20 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 69 66 28   error. */.  if(
2829d 20 70 53 65 6c 65 63 74 2d 3e 70 47 72 6f 75 70   pSelect->pGroup
2829e 42 79 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  By ){.    return
2829f 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20   0;   /* SELECT 
282a0 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61 20 47  may not have a G
282a1 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a  ROUP BY clause *
282a2 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c  /.  }.  if( pSel
282a3 65 63 74 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20  ect->pLimit ){. 
282a4 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
282a5 2a 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74  * SELECT may not
282a6 20 68 61 76 65 20 61 20 4c 49 4d 49 54 20 63 6c   have a LIMIT cl
282a7 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20 61 73  ause */.  }.  as
282a8 73 65 72 74 28 20 70 53 65 6c 65 63 74 2d 3e 70  sert( pSelect->p
282a9 4f 66 66 73 65 74 3d 3d 30 20 29 3b 20 20 2f 2a  Offset==0 );  /*
282aa 20 4d 75 73 74 20 62 65 20 73 6f 20 69 66 20 70   Must be so if p
282ab 4c 69 6d 69 74 3d 3d 30 20 2a 2f 0a 20 20 69 66  Limit==0 */.  if
282ac 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f  ( pSelect->pPrio
282ad 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  r ){.    return 
282ae 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d  0;   /* SELECT m
282af 61 79 20 6e 6f 74 20 62 65 20 61 20 63 6f 6d 70  ay not be a comp
282b0 6f 75 6e 64 20 71 75 65 72 79 20 2a 2f 0a 20 20  ound query */.  
282b1 7d 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d  }.  if( pSelect-
282b2 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44  >selFlags & SF_D
282b3 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 72  istinct ){.    r
282b4 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 53 45  eturn 0;   /* SE
282b5 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 62 65 20  LECT may not be 
282b6 44 49 53 54 49 4e 43 54 20 2a 2f 0a 20 20 7d 0a  DISTINCT */.  }.
282b7 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65 6c 65    pEList = pSele
282b8 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 61 73  ct->pEList;.  as
282b9 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20  sert( pEList!=0 
282ba 29 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d  );.  if( pEList-
282bb 3e 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20 20 20  >nExpr!=1 ){.   
282bc 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
282bd 54 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6d  The result set m
282be 75 73 74 20 68 61 76 65 20 65 78 61 63 74 6c 79  ust have exactly
282bf 20 6f 6e 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20   one column */. 
282c0 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c   }.  assert( pEL
282c1 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 20  ist->a[0].pExpr 
282c2 29 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d  );.  if( pEList-
282c3 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 21  >a[0].pExpr->op!
282c4 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 72  =TK_ALL ){.    r
282c5 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54 68  eturn 0;   /* Th
282c6 65 20 72 65 73 75 6c 74 20 73 65 74 20 6d 75 73  e result set mus
282c7 74 20 62 65 20 74 68 65 20 73 70 65 63 69 61 6c  t be the special
282c8 20 6f 70 65 72 61 74 6f 72 20 22 2a 22 20 2a 2f   operator "*" */
282c9 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68  .  }..  /* At th
282ca 69 73 20 70 6f 69 6e 74 20 77 65 20 68 61 76 65  is point we have
282cb 20 65 73 74 61 62 6c 69 73 68 65 64 20 74 68 61   established tha
282cc 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  t the statement 
282cd 69 73 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 63  is of the.  ** c
282ce 6f 72 72 65 63 74 20 73 79 6e 74 61 63 74 69 63  orrect syntactic
282cf 20 66 6f 72 6d 20 74 6f 20 70 61 72 74 69 63 69   form to partici
282d0 70 61 74 65 20 69 6e 20 74 68 69 73 20 6f 70 74  pate in this opt
282d1 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 4e 6f 77 0a  imization.  Now.
282d2 20 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20    ** we have to 
282d3 63 68 65 63 6b 20 74 68 65 20 73 65 6d 61 6e 74  check the semant
282d4 69 63 73 2e 0a 20 20 2a 2f 0a 20 20 70 49 74 65  ics..  */.  pIte
282d5 6d 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72  m = pSelect->pSr
282d6 63 2d 3e 61 3b 0a 20 20 70 53 72 63 20 3d 20 73  c->a;.  pSrc = s
282d7 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c  qlite3LocateTabl
282d8 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 49 74  e(pParse, 0, pIt
282d9 65 6d 2d 3e 7a 4e 61 6d 65 2c 20 70 49 74 65 6d  em->zName, pItem
282da 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20  ->zDatabase);.  
282db 69 66 28 20 70 53 72 63 3d 3d 30 20 29 7b 0a 20  if( pSrc==0 ){. 
282dc 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
282dd 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 64 6f  * FROM clause do
282de 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61  es not contain a
282df 20 72 65 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20   real table */. 
282e0 20 7d 0a 20 20 69 66 28 20 70 53 72 63 3d 3d 70   }.  if( pSrc==p
282e1 44 65 73 74 20 29 7b 0a 20 20 20 20 72 65 74 75  Dest ){.    retu
282e2 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 31 20  rn 0;   /* tab1 
282e3 61 6e 64 20 74 61 62 32 20 6d 61 79 20 6e 6f 74  and tab2 may not
282e4 20 62 65 20 74 68 65 20 73 61 6d 65 20 74 61 62   be the same tab
282e5 6c 65 20 2a 2f 0a 20 20 7d 0a 23 69 66 6e 64 65  le */.  }.#ifnde
282e6 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
282e7 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28  RTUALTABLE.  if(
282e8 20 70 53 72 63 2d 3e 74 61 62 46 6c 61 67 73 20   pSrc->tabFlags 
282e9 26 20 54 46 5f 56 69 72 74 75 61 6c 20 29 7b 0a  & TF_Virtual ){.
282ea 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
282eb 2f 2a 20 74 61 62 32 20 6d 75 73 74 20 6e 6f 74  /* tab2 must not
282ec 20 62 65 20 61 20 76 69 72 74 75 61 6c 20 74 61   be a virtual ta
282ed 62 6c 65 20 2a 2f 0a 20 20 7d 0a 23 65 6e 64 69  ble */.  }.#endi
282ee 66 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 70 53  f.  if( pSrc->pS
282ef 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 72 65 74  elect ){.    ret
282f0 75 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 32  urn 0;   /* tab2
282f1 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 20 76 69   may not be a vi
282f2 65 77 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  ew */.  }.  if( 
282f3 70 44 65 73 74 2d 3e 6e 43 6f 6c 21 3d 70 53 72  pDest->nCol!=pSr
282f4 63 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 72  c->nCol ){.    r
282f5 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 4e 75  eturn 0;   /* Nu
282f6 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
282f7 6d 75 73 74 20 62 65 20 74 68 65 20 73 61 6d 65  must be the same
282f8 20 69 6e 20 74 61 62 31 20 61 6e 64 20 74 61 62   in tab1 and tab
282f9 32 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70  2 */.  }.  if( p
282fa 44 65 73 74 2d 3e 69 50 4b 65 79 21 3d 70 53 72  Dest->iPKey!=pSr
282fb 63 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20  c->iPKey ){.    
282fc 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 42  return 0;   /* B
282fd 6f 74 68 20 74 61 62 6c 65 73 20 6d 75 73 74 20  oth tables must 
282fe 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 49 4e  have the same IN
282ff 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45  TEGER PRIMARY KE
28300 59 20 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 69  Y */.  }.  for(i
28301 3d 30 3b 20 69 3c 70 44 65 73 74 2d 3e 6e 43 6f  =0; i<pDest->nCo
28302 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  l; i++){.    if(
28303 20 70 44 65 73 74 2d 3e 61 43 6f 6c 5b 69 5d 2e   pDest->aCol[i].
28304 61 66 66 69 6e 69 74 79 21 3d 70 53 72 63 2d 3e  affinity!=pSrc->
28305 61 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79  aCol[i].affinity
28306 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
28307 20 30 3b 20 20 20 20 2f 2a 20 41 66 66 69 6e 69   0;    /* Affini
28308 74 79 20 6d 75 73 74 20 62 65 20 74 68 65 20 73  ty must be the s
28309 61 6d 65 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d  ame on all colum
2830a 6e 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20  ns */.    }.    
2830b 69 66 28 20 21 78 66 65 72 43 6f 6d 70 61 74 69  if( !xferCompati
2830c 62 6c 65 43 6f 6c 6c 61 74 69 6f 6e 28 70 44 65  bleCollation(pDe
2830d 73 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c  st->aCol[i].zCol
2830e 6c 2c 20 70 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d  l, pSrc->aCol[i]
2830f 2e 7a 43 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20  .zColl) ){.     
28310 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a   return 0;    /*
28311 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   Collating seque
28312 6e 63 65 20 6d 75 73 74 20 62 65 20 74 68 65 20  nce must be the 
28313 73 61 6d 65 20 6f 6e 20 61 6c 6c 20 63 6f 6c 75  same on all colu
28314 6d 6e 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20  mns */.    }.   
28315 20 69 66 28 20 70 44 65 73 74 2d 3e 61 43 6f 6c   if( pDest->aCol
28316 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c 20 26 26 20 21  [i].notNull && !
28317 70 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f  pSrc->aCol[i].no
28318 74 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 72  tNull ){.      r
28319 65 74 75 72 6e 20 30 3b 20 20 20 20 2f 2a 20 74  eturn 0;    /* t
2831a 61 62 32 20 6d 75 73 74 20 62 65 20 4e 4f 54 20  ab2 must be NOT 
2831b 4e 55 4c 4c 20 69 66 20 74 61 62 31 20 69 73 20  NULL if tab1 is 
2831c 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66  */.    }.  }.  f
2831d 6f 72 28 70 44 65 73 74 49 64 78 3d 70 44 65 73  or(pDestIdx=pDes
2831e 74 2d 3e 70 49 6e 64 65 78 3b 20 70 44 65 73 74  t->pIndex; pDest
2831f 49 64 78 3b 20 70 44 65 73 74 49 64 78 3d 70 44  Idx; pDestIdx=pD
28320 65 73 74 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  estIdx->pNext){.
28321 20 20 20 20 69 66 28 20 70 44 65 73 74 49 64 78      if( pDestIdx
28322 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f  ->onError!=OE_No
28323 6e 65 20 29 7b 0a 20 20 20 20 20 20 64 65 73 74  ne ){.      dest
28324 48 61 73 55 6e 69 71 75 65 49 64 78 20 3d 20 31  HasUniqueIdx = 1
28325 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28  ;.    }.    for(
28326 70 53 72 63 49 64 78 3d 70 53 72 63 2d 3e 70 49  pSrcIdx=pSrc->pI
28327 6e 64 65 78 3b 20 70 53 72 63 49 64 78 3b 20 70  ndex; pSrcIdx; p
28328 53 72 63 49 64 78 3d 70 53 72 63 49 64 78 2d 3e  SrcIdx=pSrcIdx->
28329 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 66  pNext){.      if
2832a 28 20 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65  ( xferCompatible
2832b 49 6e 64 65 78 28 70 44 65 73 74 49 64 78 2c 20  Index(pDestIdx, 
2832c 70 53 72 63 49 64 78 29 20 29 20 62 72 65 61 6b  pSrcIdx) ) break
2832d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
2832e 70 53 72 63 49 64 78 3d 3d 30 20 29 7b 0a 20 20  pSrcIdx==0 ){.  
2832f 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
28330 20 2f 2a 20 70 44 65 73 74 49 64 78 20 68 61 73   /* pDestIdx has
28331 20 6e 6f 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e   no correspondin
28332 67 20 69 6e 64 65 78 20 69 6e 20 70 53 72 63 20  g index in pSrc 
28333 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 69 66  */.    }.  }.#if
28334 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
28335 5f 43 48 45 43 4b 0a 20 20 69 66 28 20 70 44 65  _CHECK.  if( pDe
28336 73 74 2d 3e 70 43 68 65 63 6b 20 26 26 20 21 73  st->pCheck && !s
28337 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72  qlite3ExprCompar
28338 65 28 70 53 72 63 2d 3e 70 43 68 65 63 6b 2c 20  e(pSrc->pCheck, 
28339 70 44 65 73 74 2d 3e 70 43 68 65 63 6b 29 20 29  pDest->pCheck) )
2833a 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
2833b 20 20 2f 2a 20 54 61 62 6c 65 73 20 68 61 76 65    /* Tables have
2833c 20 64 69 66 66 65 72 65 6e 74 20 43 48 45 43 4b   different CHECK
2833d 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 54   constraints.  T
2833e 69 63 6b 65 74 20 23 32 32 35 32 20 2a 2f 0a 20  icket #2252 */. 
2833f 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
28340 49 66 20 77 65 20 67 65 74 20 74 68 69 73 20 66  If we get this f
28341 61 72 2c 20 69 74 20 6d 65 61 6e 73 20 65 69 74  ar, it means eit
28342 68 65 72 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  her:.  **.  **  
28343 20 20 2a 20 20 20 57 65 20 63 61 6e 20 61 6c 77    *   We can alw
28344 61 79 73 20 64 6f 20 74 68 65 20 74 72 61 6e 73  ays do the trans
28345 66 65 72 20 69 66 20 74 68 65 20 74 61 62 6c 65  fer if the table
28346 20 63 6f 6e 74 61 69 6e 73 20 61 6e 0a 20 20 2a   contains an.  *
28347 2a 20 20 20 20 20 20 20 20 61 6e 20 69 6e 74 65  *        an inte
28348 67 65 72 20 70 72 69 6d 61 72 79 20 6b 65 79 0a  ger primary key.
28349 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 2a 20 20    **.  **    *  
2834a 20 57 65 20 63 61 6e 20 63 6f 6e 64 69 74 69 6f   We can conditio
2834b 6e 61 6c 6c 79 20 64 6f 20 74 68 65 20 74 72 61  nally do the tra
2834c 6e 73 66 65 72 20 69 66 20 74 68 65 20 64 65 73  nsfer if the des
2834d 74 69 6e 61 74 69 6f 6e 0a 20 20 2a 2a 20 20 20  tination.  **   
2834e 20 20 20 20 20 74 61 62 6c 65 20 69 73 20 65 6d       table is em
2834f 70 74 79 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66  pty..  */.#ifdef
28350 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 73   SQLITE_TEST.  s
28351 71 6c 69 74 65 33 5f 78 66 65 72 6f 70 74 5f 63  qlite3_xferopt_c
28352 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20  ount++;.#endif. 
28353 20 69 44 62 53 72 63 20 3d 20 73 71 6c 69 74 65   iDbSrc = sqlite
28354 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
28355 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 72 63 2d  Parse->db, pSrc-
28356 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 76 20 3d  >pSchema);.  v =
28357 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
28358 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74  pParse);.  sqlit
28359 65 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65  e3CodeVerifySche
2835a 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62 53 72  ma(pParse, iDbSr
2835b 63 29 3b 0a 20 20 69 53 72 63 20 3d 20 70 50 61  c);.  iSrc = pPa
2835c 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 69  rse->nTab++;.  i
2835d 44 65 73 74 20 3d 20 70 50 61 72 73 65 2d 3e 6e  Dest = pParse->n
2835e 54 61 62 2b 2b 3b 0a 20 20 72 65 67 41 75 74 6f  Tab++;.  regAuto
2835f 69 6e 63 20 3d 20 61 75 74 6f 49 6e 63 42 65 67  inc = autoIncBeg
28360 69 6e 28 70 50 61 72 73 65 2c 20 69 44 62 44 65  in(pParse, iDbDe
28361 73 74 2c 20 70 44 65 73 74 29 3b 0a 20 20 73 71  st, pDest);.  sq
28362 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70  lite3OpenTable(p
28363 50 61 72 73 65 2c 20 69 44 65 73 74 2c 20 69 44  Parse, iDest, iD
28364 62 44 65 73 74 2c 20 70 44 65 73 74 2c 20 4f 50  bDest, pDest, OP
28365 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20 69  _OpenWrite);.  i
28366 66 28 20 28 70 44 65 73 74 2d 3e 69 50 4b 65 79  f( (pDest->iPKey
28367 3c 30 20 26 26 20 70 44 65 73 74 2d 3e 70 49 6e  <0 && pDest->pIn
28368 64 65 78 21 3d 30 29 20 7c 7c 20 64 65 73 74 48  dex!=0) || destH
28369 61 73 55 6e 69 71 75 65 49 64 78 20 29 7b 0a 20  asUniqueIdx ){. 
2836a 20 20 20 2f 2a 20 49 66 20 74 61 62 6c 65 73 20     /* If tables 
2836b 64 6f 20 6e 6f 74 20 68 61 76 65 20 61 6e 20 49  do not have an I
2836c 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
2836d 45 59 20 61 6e 64 20 74 68 65 72 65 0a 20 20 20  EY and there.   
2836e 20 2a 2a 20 61 72 65 20 69 6e 64 69 63 65 73 20   ** are indices 
2836f 74 6f 20 62 65 20 63 6f 70 69 65 64 20 61 6e 64  to be copied and
28370 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
28371 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 0a 20   is not empty,. 
28372 20 20 20 2a 2a 20 77 65 20 68 61 76 65 20 74 6f     ** we have to
28373 20 64 69 73 61 6c 6c 6f 77 20 74 68 65 20 74 72   disallow the tr
28374 61 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74  ansfer optimizat
28375 69 6f 6e 20 62 65 63 61 75 73 65 20 74 68 65 0a  ion because the.
28376 20 20 20 20 2a 2a 20 74 68 65 20 72 6f 77 69 64      ** the rowid
28377 73 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 77  s might change w
28378 68 69 63 68 20 77 69 6c 6c 20 6d 65 73 73 20 75  hich will mess u
28379 70 20 69 6e 64 65 78 69 6e 67 2e 0a 20 20 20 20  p indexing..    
2837a 2a 2a 0a 20 20 20 20 2a 2a 20 4f 72 20 69 66 20  **.    ** Or if 
2837b 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20  the destination 
2837c 68 61 73 20 61 20 55 4e 49 51 55 45 20 69 6e 64  has a UNIQUE ind
2837d 65 78 20 61 6e 64 20 69 73 20 6e 6f 74 20 65 6d  ex and is not em
2837e 70 74 79 2c 0a 20 20 20 20 2a 2a 20 77 65 20 61  pty,.    ** we a
2837f 6c 73 6f 20 64 69 73 61 6c 6c 6f 77 20 74 68 65  lso disallow the
28380 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69   transfer optimi
28381 7a 61 74 69 6f 6e 20 62 65 63 61 75 73 65 20 77  zation because w
28382 65 20 63 61 6e 6e 6f 74 0a 20 20 20 20 2a 2a 20  e cannot.    ** 
28383 69 6e 73 75 72 65 20 74 68 61 74 20 61 6c 6c 20  insure that all 
28384 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 75  entries in the u
28385 6e 69 6f 6e 20 6f 66 20 44 45 53 54 20 61 6e 64  nion of DEST and
28386 20 53 52 43 20 77 69 6c 6c 20 62 65 0a 20 20 20   SRC will be.   
28387 20 2a 2a 20 75 6e 69 71 75 65 2e 0a 20 20 20 20   ** unique..    
28388 2a 2f 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73  */.    addr1 = s
28389 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2838a 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69  (v, OP_Rewind, i
2838b 44 65 73 74 2c 20 30 29 3b 0a 20 20 20 20 65 6d  Dest, 0);.    em
2838c 70 74 79 44 65 73 74 54 65 73 74 20 3d 20 73 71  ptyDestTest = sq
2838d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2838e 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30  v, OP_Goto, 0, 0
2838f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
28390 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
28391 64 72 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  dr1);.  }else{. 
28392 20 20 20 65 6d 70 74 79 44 65 73 74 54 65 73 74     emptyDestTest
28393 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69   = 0;.  }.  sqli
28394 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61  te3OpenTable(pPa
28395 72 73 65 2c 20 69 53 72 63 2c 20 69 44 62 53 72  rse, iSrc, iDbSr
28396 63 2c 20 70 53 72 63 2c 20 4f 50 5f 4f 70 65 6e  c, pSrc, OP_Open
28397 52 65 61 64 29 3b 0a 20 20 65 6d 70 74 79 53 72  Read);.  emptySr
28398 63 54 65 73 74 20 3d 20 73 71 6c 69 74 65 33 56  cTest = sqlite3V
28399 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2839a 52 65 77 69 6e 64 2c 20 69 53 72 63 2c 20 30 29  Rewind, iSrc, 0)
2839b 3b 0a 20 20 72 65 67 44 61 74 61 20 3d 20 73 71  ;.  regData = sq
2839c 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
2839d 70 50 61 72 73 65 29 3b 0a 20 20 72 65 67 52 6f  pParse);.  regRo
2839e 77 69 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74  wid = sqlite3Get
2839f 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
283a0 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69 50  .  if( pDest->iP
283a1 4b 65 79 3e 3d 30 20 29 7b 0a 20 20 20 20 61 64  Key>=0 ){.    ad
283a2 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dr1 = sqlite3Vdb
283a3 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f  eAddOp2(v, OP_Ro
283a4 77 69 64 2c 20 69 53 72 63 2c 20 72 65 67 52 6f  wid, iSrc, regRo
283a5 77 69 64 29 3b 0a 20 20 20 20 61 64 64 72 32 20  wid);.    addr2 
283a6 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
283a7 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69  Op3(v, OP_NotExi
283a8 73 74 73 2c 20 69 44 65 73 74 2c 20 30 2c 20 72  sts, iDest, 0, r
283a9 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71  egRowid);.    sq
283aa 6c 69 74 65 33 48 61 6c 74 43 6f 6e 73 74 72 61  lite3HaltConstra
283ab 69 6e 74 28 0a 20 20 20 20 20 20 20 20 70 50 61  int(.        pPa
283ac 72 73 65 2c 20 6f 6e 45 72 72 6f 72 2c 20 22 50  rse, onError, "P
283ad 52 49 4d 41 52 59 20 4b 45 59 20 6d 75 73 74 20  RIMARY KEY must 
283ae 62 65 20 75 6e 69 71 75 65 22 2c 20 50 34 5f 53  be unique", P4_S
283af 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69  TATIC);.    sqli
283b0 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
283b1 76 2c 20 61 64 64 72 32 29 3b 0a 20 20 20 20 61  v, addr2);.    a
283b2 75 74 6f 49 6e 63 53 74 65 70 28 70 50 61 72 73  utoIncStep(pPars
283b3 65 2c 20 72 65 67 41 75 74 6f 69 6e 63 2c 20 72  e, regAutoinc, r
283b4 65 67 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73  egRowid);.  }els
283b5 65 20 69 66 28 20 70 44 65 73 74 2d 3e 70 49 6e  e if( pDest->pIn
283b6 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 61 64  dex==0 ){.    ad
283b7 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62  dr1 = sqlite3Vdb
283b8 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
283b9 77 52 6f 77 69 64 2c 20 69 44 65 73 74 2c 20 72  wRowid, iDest, r
283ba 65 67 52 6f 77 69 64 29 3b 0a 20 20 7d 65 6c 73  egRowid);.  }els
283bb 65 7b 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73  e{.    addr1 = s
283bc 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
283bd 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 53  (v, OP_Rowid, iS
283be 72 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  rc, regRowid);. 
283bf 20 20 20 61 73 73 65 72 74 28 20 28 70 44 65 73     assert( (pDes
283c0 74 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46  t->tabFlags & TF
283c1 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 29 3d  _Autoincrement)=
283c2 3d 30 20 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  =0 );.  }.  sqli
283c3 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
283c4 20 4f 50 5f 52 6f 77 44 61 74 61 2c 20 69 53 72   OP_RowData, iSr
283c5 63 2c 20 72 65 67 44 61 74 61 29 3b 0a 20 20 73  c, regData);.  s
283c6 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
283c7 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69  (v, OP_Insert, i
283c8 44 65 73 74 2c 20 72 65 67 44 61 74 61 2c 20 72  Dest, regData, r
283c9 65 67 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c 69  egRowid);.  sqli
283ca 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28  te3VdbeChangeP5(
283cb 76 2c 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47  v, OPFLAG_NCHANG
283cc 45 7c 4f 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57  E|OPFLAG_LASTROW
283cd 49 44 7c 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44  ID|OPFLAG_APPEND
283ce 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
283cf 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20  ChangeP4(v, -1, 
283d0 70 44 65 73 74 2d 3e 7a 4e 61 6d 65 2c 20 30 29  pDest->zName, 0)
283d1 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
283d2 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74  ddOp2(v, OP_Next
283d3 2c 20 69 53 72 63 2c 20 61 64 64 72 31 29 3b 0a  , iSrc, addr1);.
283d4 20 20 66 6f 72 28 70 44 65 73 74 49 64 78 3d 70    for(pDestIdx=p
283d5 44 65 73 74 2d 3e 70 49 6e 64 65 78 3b 20 70 44  Dest->pIndex; pD
283d6 65 73 74 49 64 78 3b 20 70 44 65 73 74 49 64 78  estIdx; pDestIdx
283d7 3d 70 44 65 73 74 49 64 78 2d 3e 70 4e 65 78 74  =pDestIdx->pNext
283d8 29 7b 0a 20 20 20 20 66 6f 72 28 70 53 72 63 49  ){.    for(pSrcI
283d9 64 78 3d 70 53 72 63 2d 3e 70 49 6e 64 65 78 3b  dx=pSrc->pIndex;
283da 20 41 4c 57 41 59 53 28 70 53 72 63 49 64 78 29   ALWAYS(pSrcIdx)
283db 3b 20 70 53 72 63 49 64 78 3d 70 53 72 63 49 64  ; pSrcIdx=pSrcId
283dc 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20  x->pNext){.     
283dd 20 69 66 28 20 78 66 65 72 43 6f 6d 70 61 74 69   if( xferCompati
283de 62 6c 65 49 6e 64 65 78 28 70 44 65 73 74 49 64  bleIndex(pDestId
283df 78 2c 20 70 53 72 63 49 64 78 29 20 29 20 62 72  x, pSrcIdx) ) br
283e0 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  eak;.    }.    a
283e1 73 73 65 72 74 28 20 70 53 72 63 49 64 78 20 29  ssert( pSrcIdx )
283e2 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
283e3 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c  eAddOp2(v, OP_Cl
283e4 6f 73 65 2c 20 69 53 72 63 2c 20 30 29 3b 0a 20  ose, iSrc, 0);. 
283e5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
283e6 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp2(v, OP_Close
283e7 2c 20 69 44 65 73 74 2c 20 30 29 3b 0a 20 20 20  , iDest, 0);.   
283e8 20 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49   pKey = sqlite3I
283e9 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72  ndexKeyinfo(pPar
283ea 73 65 2c 20 70 53 72 63 49 64 78 29 3b 0a 20 20  se, pSrcIdx);.  
283eb 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
283ec 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52 65  Op4(v, OP_OpenRe
283ed 61 64 2c 20 69 53 72 63 2c 20 70 53 72 63 49 64  ad, iSrc, pSrcId
283ee 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 53 72 63 2c  x->tnum, iDbSrc,
283ef 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
283f0 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b         (char*)pK
283f1 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48  ey, P4_KEYINFO_H
283f2 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56 64 62  ANDOFF);.    Vdb
283f3 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73  eComment((v, "%s
283f4 22 2c 20 70 53 72 63 49 64 78 2d 3e 7a 4e 61 6d  ", pSrcIdx->zNam
283f5 65 29 29 3b 0a 20 20 20 20 70 4b 65 79 20 3d 20  e));.    pKey = 
283f6 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69  sqlite3IndexKeyi
283f7 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 44 65 73  nfo(pParse, pDes
283f8 74 49 64 78 29 3b 0a 20 20 20 20 73 71 6c 69 74  tIdx);.    sqlit
283f9 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
283fa 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 44  OP_OpenWrite, iD
283fb 65 73 74 2c 20 70 44 65 73 74 49 64 78 2d 3e 74  est, pDestIdx->t
283fc 6e 75 6d 2c 20 69 44 62 44 65 73 74 2c 0a 20 20  num, iDbDest,.  
283fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
283fe 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 2c      (char*)pKey,
283ff 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
28400 4f 46 46 29 3b 0a 20 20 20 20 56 64 62 65 43 6f  OFF);.    VdbeCo
28401 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73 22 2c 20  mment((v, "%s", 
28402 70 44 65 73 74 49 64 78 2d 3e 7a 4e 61 6d 65 29  pDestIdx->zName)
28403 29 3b 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73  );.    addr1 = s
28404 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
28405 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20 69  (v, OP_Rewind, i
28406 53 72 63 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  Src, 0);.    sql
28407 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
28408 2c 20 4f 50 5f 52 6f 77 4b 65 79 2c 20 69 53 72  , OP_RowKey, iSr
28409 63 2c 20 72 65 67 44 61 74 61 29 3b 0a 20 20 20  c, regData);.   
2840a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2840b 70 33 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73 65  p3(v, OP_IdxInse
2840c 72 74 2c 20 69 44 65 73 74 2c 20 72 65 67 44 61  rt, iDest, regDa
2840d 74 61 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69  ta, 1);.    sqli
2840e 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2840f 20 4f 50 5f 4e 65 78 74 2c 20 69 53 72 63 2c 20   OP_Next, iSrc, 
28410 61 64 64 72 31 2b 31 29 3b 0a 20 20 20 20 73 71  addr1+1);.    sq
28411 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
28412 65 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 7d  e(v, addr1);.  }
28413 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75  .  sqlite3VdbeJu
28414 6d 70 48 65 72 65 28 76 2c 20 65 6d 70 74 79 53  mpHere(v, emptyS
28415 72 63 54 65 73 74 29 3b 0a 20 20 73 71 6c 69 74  rcTest);.  sqlit
28416 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
28417 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 69  (pParse, regRowi
28418 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c  d);.  sqlite3Rel
28419 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
2841a 73 65 2c 20 72 65 67 44 61 74 61 29 3b 0a 20 20  se, regData);.  
2841b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2841c 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69  2(v, OP_Close, i
2841d 53 72 63 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  Src, 0);.  sqlit
2841e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2841f 4f 50 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c  OP_Close, iDest,
28420 20 30 29 3b 0a 20 20 69 66 28 20 65 6d 70 74 79   0);.  if( empty
28421 44 65 73 74 54 65 73 74 20 29 7b 0a 20 20 20 20  DestTest ){.    
28422 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28423 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51  2(v, OP_Halt, SQ
28424 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20  LITE_OK, 0);.   
28425 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
28426 48 65 72 65 28 76 2c 20 65 6d 70 74 79 44 65 73  Here(v, emptyDes
28427 74 54 65 73 74 29 3b 0a 20 20 20 20 73 71 6c 69  tTest);.    sqli
28428 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
28429 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 44 65 73 74   OP_Close, iDest
2842a 2c 20 30 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  , 0);.    return
2842b 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
2842c 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 7d   return 1;.  }.}
2842d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2842e 45 5f 4f 4d 49 54 5f 58 46 45 52 5f 4f 50 54 20  E_OMIT_XFER_OPT 
2842f 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
28430 2a 2a 2a 20 45 6e 64 20 6f 66 20 69 6e 73 65 72  *** End of inser
28431 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.c ************
28432 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28433 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28434 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
28435 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6c  *** Begin file l
28436 65 67 61 63 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  egacy.c ********
28437 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28438 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28439 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53  **/./*.** 2001 S
2843a 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a  eptember 15.**.*
2843b 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
2843c 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
2843d 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
2843e 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
2843f 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
28440 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
28441 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
28442 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
28443 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
28444 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
28445 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
28446 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
28447 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
28448 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
28449 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
2844a 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
2844b 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
2844c 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
2844d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2844e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2844f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 4d 61  **********.** Ma
28451 69 6e 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20  in file for the 
28452 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20  SQLite library. 
28453 20 54 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e   The routines in
28454 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 69 6d   this file.** im
28455 70 6c 65 6d 65 6e 74 20 74 68 65 20 70 72 6f 67  plement the prog
28456 72 61 6d 6d 65 72 20 69 6e 74 65 72 66 61 63 65  rammer interface
28457 20 74 6f 20 74 68 65 20 6c 69 62 72 61 72 79 2e   to the library.
28458 20 20 52 6f 75 74 69 6e 65 73 20 69 6e 0a 2a 2a    Routines in.**
28459 20 6f 74 68 65 72 20 66 69 6c 65 73 20 61 72 65   other files are
2845a 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73   for internal us
2845b 65 20 62 79 20 53 51 4c 69 74 65 20 61 6e 64 20  e by SQLite and 
2845c 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a  should not be.**
2845d 20 61 63 63 65 73 73 65 64 20 62 79 20 75 73 65   accessed by use
2845e 72 73 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72  rs of the librar
2845f 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6c 65  y..**.** $Id: le
28460 67 61 63 79 2e 63 2c 76 20 31 2e 33 35 20 32 30  gacy.c,v 1.35 20
28461 30 39 2f 30 38 2f 30 37 20 31 36 3a 35 36 3a 30  09/08/07 16:56:0
28462 30 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  0 danielk1977 Ex
28463 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 45  p $.*/.../*.** E
28464 78 65 63 75 74 65 20 53 51 4c 20 63 6f 64 65 2e  xecute SQL code.
28465 20 20 52 65 74 75 72 6e 20 6f 6e 65 20 6f 66 20    Return one of 
28466 74 68 65 20 53 51 4c 49 54 45 5f 20 73 75 63 63  the SQLITE_ succ
28467 65 73 73 2f 66 61 69 6c 75 72 65 0a 2a 2a 20 63  ess/failure.** c
28468 6f 64 65 73 2e 20 20 41 6c 73 6f 20 77 72 69 74  odes.  Also writ
28469 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  e an error messa
2846a 67 65 20 69 6e 74 6f 20 6d 65 6d 6f 72 79 20 6f  ge into memory o
2846b 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20  btained from.** 
2846c 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 61 6b  malloc() and mak
2846d 65 20 2a 70 7a 45 72 72 4d 73 67 20 70 6f 69 6e  e *pzErrMsg poin
2846e 74 20 74 6f 20 74 68 61 74 20 6d 65 73 73 61 67  t to that messag
2846f 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
28470 53 51 4c 20 69 73 20 61 20 71 75 65 72 79 2c 20  SQL is a query, 
28471 74 68 65 6e 20 66 6f 72 20 65 61 63 68 20 72 6f  then for each ro
28472 77 20 69 6e 20 74 68 65 20 71 75 65 72 79 20 72  w in the query r
28473 65 73 75 6c 74 0a 2a 2a 20 74 68 65 20 78 43 61  esult.** the xCa
28474 6c 6c 62 61 63 6b 28 29 20 66 75 6e 63 74 69 6f  llback() functio
28475 6e 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 70 41  n is called.  pA
28476 72 67 20 62 65 63 6f 6d 65 73 20 74 68 65 20 66  rg becomes the f
28477 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  irst.** argument
28478 20 74 6f 20 78 43 61 6c 6c 62 61 63 6b 28 29 2e   to xCallback().
28479 20 20 49 66 20 78 43 61 6c 6c 62 61 63 6b 3d 4e    If xCallback=N
2847a 55 4c 4c 20 74 68 65 6e 20 6e 6f 20 63 61 6c 6c  ULL then no call
2847b 62 61 63 6b 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b  back.** is invok
2847c 65 64 2c 20 65 76 65 6e 20 66 6f 72 20 71 75 65  ed, even for que
2847d 72 69 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ries..*/.SQLITE_
2847e 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2847f 65 78 65 63 28 0a 20 20 73 71 6c 69 74 65 33 20  exec(.  sqlite3 
28480 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
28481 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
28482 61 73 65 20 6f 6e 20 77 68 69 63 68 20 74 68 65  ase on which the
28483 20 53 51 4c 20 65 78 65 63 75 74 65 73 20 2a 2f   SQL executes */
28484 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
28485 53 71 6c 2c 20 20 20 20 20 20 20 20 20 20 20 2f  Sql,           /
28486 2a 20 54 68 65 20 53 51 4c 20 74 6f 20 62 65 20  * The SQL to be 
28487 65 78 65 63 75 74 65 64 20 2a 2f 0a 20 20 73 71  executed */.  sq
28488 6c 69 74 65 33 5f 63 61 6c 6c 62 61 63 6b 20 78  lite3_callback x
28489 43 61 6c 6c 62 61 63 6b 2c 20 2f 2a 20 49 6e 76  Callback, /* Inv
2848a 6f 6b 65 20 74 68 69 73 20 63 61 6c 6c 62 61 63  oke this callbac
2848b 6b 20 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 76  k routine */.  v
2848c 6f 69 64 20 2a 70 41 72 67 2c 20 20 20 20 20 20  oid *pArg,      
2848d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
2848e 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  rst argument to 
2848f 78 43 61 6c 6c 62 61 63 6b 28 29 20 2a 2f 0a 20  xCallback() */. 
28490 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67   char **pzErrMsg
28491 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
28492 57 72 69 74 65 20 65 72 72 6f 72 20 6d 65 73 73  Write error mess
28493 61 67 65 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  ages here */.){.
28494 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
28495 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 20 2f 2a  E_OK;         /*
28496 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a   Return code */.
28497 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c    const char *zL
28498 65 66 74 6f 76 65 72 3b 20 20 20 20 20 20 2f 2a  eftover;      /*
28499 20 54 61 69 6c 20 6f 66 20 75 6e 70 72 6f 63 65   Tail of unproce
2849a 73 73 65 64 20 53 51 4c 20 2a 2f 0a 20 20 73 71  ssed SQL */.  sq
2849b 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
2849c 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 54 68 65  t = 0;    /* The
2849d 20 63 75 72 72 65 6e 74 20 53 51 4c 20 73 74 61   current SQL sta
2849e 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63 68 61 72  tement */.  char
2849f 20 2a 2a 61 7a 43 6f 6c 73 20 3d 20 30 3b 20 20   **azCols = 0;  
284a0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 73          /* Names
284a1 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d   of result colum
284a2 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 74  ns */.  int nRet
284a3 72 79 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ry = 0;         
284a4 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
284a5 20 72 65 74 72 79 20 61 74 74 65 6d 70 74 73 20   retry attempts 
284a6 2a 2f 0a 20 20 69 6e 74 20 63 61 6c 6c 62 61 63  */.  int callbac
284a7 6b 49 73 49 6e 69 74 3b 20 20 20 20 20 20 20 20  kIsInit;        
284a8 20 2f 2a 20 54 72 75 65 20 69 66 20 63 61 6c 6c   /* True if call
284a9 62 61 63 6b 20 64 61 74 61 20 69 73 20 69 6e 69  back data is ini
284aa 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 0a 20 20 69  tialized */..  i
284ab 66 28 20 7a 53 71 6c 3d 3d 30 20 29 20 7a 53 71  f( zSql==0 ) zSq
284ac 6c 20 3d 20 22 22 3b 0a 0a 20 20 73 71 6c 69 74  l = "";..  sqlit
284ad 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
284ae 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c  b->mutex);.  sql
284af 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
284b0 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 77  LITE_OK, 0);.  w
284b1 68 69 6c 65 28 20 28 72 63 3d 3d 53 51 4c 49 54  hile( (rc==SQLIT
284b2 45 5f 4f 4b 20 7c 7c 20 28 72 63 3d 3d 53 51 4c  E_OK || (rc==SQL
284b3 49 54 45 5f 53 43 48 45 4d 41 20 26 26 20 28 2b  ITE_SCHEMA && (+
284b4 2b 6e 52 65 74 72 79 29 3c 32 29 29 20 26 26 20  +nRetry)<2)) && 
284b5 7a 53 71 6c 5b 30 5d 20 29 7b 0a 20 20 20 20 69  zSql[0] ){.    i
284b6 6e 74 20 6e 43 6f 6c 3b 0a 20 20 20 20 63 68 61  nt nCol;.    cha
284b7 72 20 2a 2a 61 7a 56 61 6c 73 20 3d 20 30 3b 0a  r **azVals = 0;.
284b8 0a 20 20 20 20 70 53 74 6d 74 20 3d 20 30 3b 0a  .    pStmt = 0;.
284b9 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
284ba 5f 70 72 65 70 61 72 65 28 64 62 2c 20 7a 53 71  _prepare(db, zSq
284bb 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 26  l, -1, &pStmt, &
284bc 7a 4c 65 66 74 6f 76 65 72 29 3b 0a 20 20 20 20  zLeftover);.    
284bd 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
284be 54 45 5f 4f 4b 20 7c 7c 20 70 53 74 6d 74 3d 3d  TE_OK || pStmt==
284bf 30 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  0 );.    if( rc!
284c0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
284c1 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
284c2 20 20 7d 0a 20 20 20 20 69 66 28 20 21 70 53 74    }.    if( !pSt
284c3 6d 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 74  mt ){.      /* t
284c4 68 69 73 20 68 61 70 70 65 6e 73 20 66 6f 72 20  his happens for 
284c5 61 20 63 6f 6d 6d 65 6e 74 20 6f 72 20 77 68 69  a comment or whi
284c6 74 65 2d 73 70 61 63 65 20 2a 2f 0a 20 20 20 20  te-space */.    
284c7 20 20 7a 53 71 6c 20 3d 20 7a 4c 65 66 74 6f 76    zSql = zLeftov
284c8 65 72 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e  er;.      contin
284c9 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 63  ue;.    }..    c
284ca 61 6c 6c 62 61 63 6b 49 73 49 6e 69 74 20 3d 20  allbackIsInit = 
284cb 30 3b 0a 20 20 20 20 6e 43 6f 6c 20 3d 20 73 71  0;.    nCol = sq
284cc 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75  lite3_column_cou
284cd 6e 74 28 70 53 74 6d 74 29 3b 0a 0a 20 20 20 20  nt(pStmt);..    
284ce 77 68 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20  while( 1 ){.    
284cf 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 72    int i;.      r
284d0 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
284d1 28 70 53 74 6d 74 29 3b 0a 0a 20 20 20 20 20 20  (pStmt);..      
284d2 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 61  /* Invoke the ca
284d3 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
284d4 69 66 20 72 65 71 75 69 72 65 64 20 2a 2f 0a 20  if required */. 
284d5 20 20 20 20 20 69 66 28 20 78 43 61 6c 6c 62 61       if( xCallba
284d6 63 6b 20 26 26 20 28 53 51 4c 49 54 45 5f 52 4f  ck && (SQLITE_RO
284d7 57 3d 3d 72 63 20 7c 7c 20 0a 20 20 20 20 20 20  W==rc || .      
284d8 20 20 20 20 28 53 51 4c 49 54 45 5f 44 4f 4e 45      (SQLITE_DONE
284d9 3d 3d 72 63 20 26 26 20 21 63 61 6c 6c 62 61 63  ==rc && !callbac
284da 6b 49 73 49 6e 69 74 0a 20 20 20 20 20 20 20 20  kIsInit.        
284db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
284dc 20 20 20 26 26 20 64 62 2d 3e 66 6c 61 67 73 26     && db->flags&
284dd 53 51 4c 49 54 45 5f 4e 75 6c 6c 43 61 6c 6c 62  SQLITE_NullCallb
284de 61 63 6b 29 29 20 29 7b 0a 20 20 20 20 20 20 20  ack)) ){.       
284df 20 69 66 28 20 21 63 61 6c 6c 62 61 63 6b 49 73   if( !callbackIs
284e0 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Init ){.        
284e1 20 20 61 7a 43 6f 6c 73 20 3d 20 73 71 6c 69 74    azCols = sqlit
284e2 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
284e3 62 2c 20 32 2a 6e 43 6f 6c 2a 73 69 7a 65 6f 66  b, 2*nCol*sizeof
284e4 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 20 2b 20  (const char*) + 
284e5 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  1);.          if
284e6 28 20 61 7a 43 6f 6c 73 3d 3d 30 20 29 7b 0a 20  ( azCols==0 ){. 
284e7 20 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20             goto 
284e8 65 78 65 63 5f 6f 75 74 3b 0a 20 20 20 20 20 20  exec_out;.      
284e9 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
284ea 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b  for(i=0; i<nCol;
284eb 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
284ec 20 20 20 61 7a 43 6f 6c 73 5b 69 5d 20 3d 20 28     azCols[i] = (
284ed 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 63  char *)sqlite3_c
284ee 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 70 53 74 6d 74  olumn_name(pStmt
284ef 2c 20 69 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , i);.          
284f0 20 20 2f 2a 20 73 71 6c 69 74 65 33 56 64 62 65    /* sqlite3Vdbe
284f1 53 65 74 43 6f 6c 4e 61 6d 65 28 29 20 69 6e 73  SetColName() ins
284f2 74 61 6c 6c 73 20 63 6f 6c 75 6d 6e 20 6e 61 6d  talls column nam
284f3 65 73 20 61 73 20 55 54 46 38 0a 20 20 20 20 20  es as UTF8.     
284f4 20 20 20 20 20 20 20 2a 2a 20 73 74 72 69 6e 67         ** string
284f5 73 20 73 6f 20 74 68 65 72 65 20 69 73 20 6e 6f  s so there is no
284f6 20 77 61 79 20 66 6f 72 20 73 71 6c 69 74 65 33   way for sqlite3
284f7 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 20 74  _column_name() t
284f8 6f 20 66 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 20  o fail. */.     
284f9 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 61         assert( a
284fa 7a 43 6f 6c 73 5b 69 5d 21 3d 30 20 29 3b 0a 20  zCols[i]!=0 );. 
284fb 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
284fc 20 20 20 20 20 63 61 6c 6c 62 61 63 6b 49 73 49       callbackIsI
284fd 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  nit = 1;.       
284fe 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72   }.        if( r
284ff 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  c==SQLITE_ROW ){
28500 0a 20 20 20 20 20 20 20 20 20 20 61 7a 56 61 6c  .          azVal
28501 73 20 3d 20 26 61 7a 43 6f 6c 73 5b 6e 43 6f 6c  s = &azCols[nCol
28502 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72  ];.          for
28503 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
28504 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  +){.            
28505 61 7a 56 61 6c 73 5b 69 5d 20 3d 20 28 63 68 61  azVals[i] = (cha
28506 72 20 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75  r *)sqlite3_colu
28507 6d 6e 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 69  mn_text(pStmt, i
28508 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  );.            i
28509 66 28 20 21 61 7a 56 61 6c 73 5b 69 5d 20 26 26  f( !azVals[i] &&
2850a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
2850b 74 79 70 65 28 70 53 74 6d 74 2c 20 69 29 21 3d  type(pStmt, i)!=
2850c 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20  SQLITE_NULL ){. 
2850d 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d               db-
2850e 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
2850f 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1;.             
28510 20 67 6f 74 6f 20 65 78 65 63 5f 6f 75 74 3b 0a   goto exec_out;.
28511 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
28512 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
28513 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
28514 78 43 61 6c 6c 62 61 63 6b 28 70 41 72 67 2c 20  xCallback(pArg, 
28515 6e 43 6f 6c 2c 20 61 7a 56 61 6c 73 2c 20 61 7a  nCol, azVals, az
28516 43 6f 6c 73 29 20 29 7b 0a 20 20 20 20 20 20 20  Cols) ){.       
28517 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 41     rc = SQLITE_A
28518 42 4f 52 54 3b 0a 20 20 20 20 20 20 20 20 20 20  BORT;.          
28519 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c  sqlite3VdbeFinal
2851a 69 7a 65 28 28 56 64 62 65 20 2a 29 70 53 74 6d  ize((Vdbe *)pStm
2851b 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 53  t);.          pS
2851c 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  tmt = 0;.       
2851d 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28     sqlite3Error(
2851e 64 62 2c 20 53 51 4c 49 54 45 5f 41 42 4f 52 54  db, SQLITE_ABORT
2851f 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
28520 67 6f 74 6f 20 65 78 65 63 5f 6f 75 74 3b 0a 20  goto exec_out;. 
28521 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
28522 0a 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ..      if( rc!=
28523 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
28524 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
28525 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 28  e3VdbeFinalize((
28526 56 64 62 65 20 2a 29 70 53 74 6d 74 29 3b 0a 20  Vdbe *)pStmt);. 
28527 20 20 20 20 20 20 20 70 53 74 6d 74 20 3d 20 30         pStmt = 0
28528 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
28529 21 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20  !=SQLITE_SCHEMA 
2852a 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 52 65  ){.          nRe
2852b 74 72 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  try = 0;.       
2852c 20 20 20 7a 53 71 6c 20 3d 20 7a 4c 65 66 74 6f     zSql = zLefto
2852d 76 65 72 3b 0a 20 20 20 20 20 20 20 20 20 20 77  ver;.          w
2852e 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 73  hile( sqlite3Iss
2852f 70 61 63 65 28 7a 53 71 6c 5b 30 5d 29 20 29 20  pace(zSql[0]) ) 
28530 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20  zSql++;.        
28531 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
28532 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
28533 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
28534 65 28 64 62 2c 20 61 7a 43 6f 6c 73 29 3b 0a 20  e(db, azCols);. 
28535 20 20 20 61 7a 43 6f 6c 73 20 3d 20 30 3b 0a 20     azCols = 0;. 
28536 20 7d 0a 0a 65 78 65 63 5f 6f 75 74 3a 0a 20 20   }..exec_out:.  
28537 69 66 28 20 70 53 74 6d 74 20 29 20 73 71 6c 69  if( pStmt ) sqli
28538 74 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28  te3VdbeFinalize(
28539 28 56 64 62 65 20 2a 29 70 53 74 6d 74 29 3b 0a  (Vdbe *)pStmt);.
2853a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2853b 64 62 2c 20 61 7a 43 6f 6c 73 29 3b 0a 0a 20 20  db, azCols);..  
2853c 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
2853d 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 69  xit(db, rc);.  i
2853e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2853f 20 26 26 20 41 4c 57 41 59 53 28 72 63 3d 3d 73   && ALWAYS(rc==s
28540 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 64  qlite3_errcode(d
28541 62 29 29 20 26 26 20 70 7a 45 72 72 4d 73 67 20  b)) && pzErrMsg 
28542 29 7b 0a 20 20 20 20 69 6e 74 20 6e 45 72 72 4d  ){.    int nErrM
28543 73 67 20 3d 20 31 20 2b 20 73 71 6c 69 74 65 33  sg = 1 + sqlite3
28544 53 74 72 6c 65 6e 33 30 28 73 71 6c 69 74 65 33  Strlen30(sqlite3
28545 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20  _errmsg(db));.  
28546 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71    *pzErrMsg = sq
28547 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 45 72 72  lite3Malloc(nErr
28548 4d 73 67 29 3b 0a 20 20 20 20 69 66 28 20 2a 70  Msg);.    if( *p
28549 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20  zErrMsg ){.     
2854a 20 6d 65 6d 63 70 79 28 2a 70 7a 45 72 72 4d 73   memcpy(*pzErrMs
2854b 67 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73  g, sqlite3_errms
2854c 67 28 64 62 29 2c 20 6e 45 72 72 4d 73 67 29 3b  g(db), nErrMsg);
2854d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2854e 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
2854f 4d 45 4d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  MEM;.      sqlit
28550 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49  e3Error(db, SQLI
28551 54 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a 20 20  TE_NOMEM, 0);.  
28552 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20    }.  }else if( 
28553 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20  pzErrMsg ){.    
28554 2a 70 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  *pzErrMsg = 0;. 
28555 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 28 72   }..  assert( (r
28556 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d  c&db->errMask)==
28557 72 63 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  rc );.  sqlite3_
28558 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
28559 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
2855a 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a   rc;.}../*******
2855b 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6c  ******* End of l
2855c 65 67 61 63 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  egacy.c ********
2855d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2855e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2855f 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
28560 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
28561 6c 65 20 6c 6f 61 64 65 78 74 2e 63 20 2a 2a 2a  le loadext.c ***
28562 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28563 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28564 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
28565 30 36 20 4a 75 6e 65 20 37 0a 2a 2a 0a 2a 2a 20  06 June 7.**.** 
28566 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
28567 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
28568 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
28569 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
2856a 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
2856b 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
2856c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
2856d 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
2856e 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
2856f 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
28570 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
28571 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
28572 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
28573 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
28574 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
28575 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
28576 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
28577 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28578 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28579 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2857a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2857b 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
2857c 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
2857d 6f 64 65 20 75 73 65 64 20 74 6f 20 64 79 6e 61  ode used to dyna
2857e 6d 69 63 61 6c 6c 79 20 6c 6f 61 64 20 65 78 74  mically load ext
2857f 65 6e 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a 20  ensions into.** 
28580 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
28581 72 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6c  ry..**.** $Id: l
28582 6f 61 64 65 78 74 2e 63 2c 76 20 31 2e 36 30 20  oadext.c,v 1.60 
28583 32 30 30 39 2f 30 36 2f 30 33 20 30 31 3a 32 34  2009/06/03 01:24
28584 3a 35 34 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :54 drh Exp $.*/
28585 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
28586 5f 43 4f 52 45 0a 20 20 23 64 65 66 69 6e 65 20  _CORE.  #define 
28587 53 51 4c 49 54 45 5f 43 4f 52 45 20 31 20 20 2f  SQLITE_CORE 1  /
28588 2a 20 44 69 73 61 62 6c 65 20 74 68 65 20 41 50  * Disable the AP
28589 49 20 72 65 64 65 66 69 6e 69 74 69 6f 6e 20 69  I redefinition i
2858a 6e 20 73 71 6c 69 74 65 33 65 78 74 2e 68 20 2a  n sqlite3ext.h *
2858b 2f 0a 23 65 6e 64 69 66 0a 2f 2a 2a 2a 2a 2a 2a  /.#endif./******
2858c 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
2858d 20 73 71 6c 69 74 65 33 65 78 74 2e 68 20 69 6e   sqlite3ext.h in
2858e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6c   the middle of l
2858f 6f 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  oadext.c *******
28590 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
28591 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
28592 69 6c 65 20 73 71 6c 69 74 65 33 65 78 74 2e 68  ile sqlite3ext.h
28593 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
28594 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28595 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
28596 30 30 36 20 4a 75 6e 65 20 37 0a 2a 2a 0a 2a 2a  006 June 7.**.**
28597 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
28598 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
28599 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
2859a 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
2859b 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
2859c 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
2859d 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
2859e 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
2859f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
285a0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
285a1 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
285a2 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
285a3 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
285a4 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
285a5 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
285a6 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
285a7 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
285a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
285a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
285aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
285ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
285ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
285ad 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65  s header file de
285ae 66 69 6e 65 73 20 74 68 65 20 53 51 4c 69 74 65  fines the SQLite
285af 20 69 6e 74 65 72 66 61 63 65 20 66 6f 72 20 75   interface for u
285b0 73 65 20 62 79 0a 2a 2a 20 73 68 61 72 65 64 20  se by.** shared 
285b1 6c 69 62 72 61 72 69 65 73 20 74 68 61 74 20 77  libraries that w
285b2 61 6e 74 20 74 6f 20 62 65 20 69 6d 70 6f 72 74  ant to be import
285b3 65 64 20 61 73 20 65 78 74 65 6e 73 69 6f 6e 73  ed as extensions
285b4 20 69 6e 74 6f 0a 2a 2a 20 61 6e 20 53 51 4c 69   into.** an SQLi
285b5 74 65 20 69 6e 73 74 61 6e 63 65 2e 20 20 53 68  te instance.  Sh
285b6 61 72 65 64 20 6c 69 62 72 61 72 69 65 73 20 74  ared libraries t
285b7 68 61 74 20 69 6e 74 65 6e 64 20 74 6f 20 62 65  hat intend to be
285b8 20 6c 6f 61 64 65 64 0a 2a 2a 20 61 73 20 65 78   loaded.** as ex
285b9 74 65 6e 73 69 6f 6e 73 20 62 79 20 53 51 4c 69  tensions by SQLi
285ba 74 65 20 73 68 6f 75 6c 64 20 23 69 6e 63 6c 75  te should #inclu
285bb 64 65 20 74 68 69 73 20 66 69 6c 65 20 69 6e 73  de this file ins
285bc 74 65 61 64 20 6f 66 20 0a 2a 2a 20 73 71 6c 69  tead of .** sqli
285bd 74 65 33 2e 68 2e 0a 2a 2a 0a 2a 2a 20 40 28 23  te3.h..**.** @(#
285be 29 20 24 49 64 3a 20 73 71 6c 69 74 65 33 65 78  ) $Id: sqlite3ex
285bf 74 2e 68 2c 76 20 31 2e 32 35 20 32 30 30 38 2f  t.h,v 1.25 2008/
285c0 31 30 2f 31 32 20 30 30 3a 32 37 3a 35 34 20 73  10/12 00:27:54 s
285c1 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69  hane Exp $.*/.#i
285c2 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 33 45 58  fndef _SQLITE3EX
285c3 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51  T_H_.#define _SQ
285c4 4c 49 54 45 33 45 58 54 5f 48 5f 0a 0a 74 79 70  LITE3EXT_H_..typ
285c5 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
285c6 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
285c7 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75   sqlite3_api_rou
285c8 74 69 6e 65 73 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  tines;../*.** Th
285c9 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
285ca 63 74 75 72 65 20 68 6f 6c 64 73 20 70 6f 69 6e  cture holds poin
285cb 74 65 72 73 20 74 6f 20 61 6c 6c 20 6f 66 20 74  ters to all of t
285cc 68 65 20 53 51 4c 69 74 65 20 41 50 49 0a 2a 2a  he SQLite API.**
285cd 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a   routines..**.**
285ce 20 57 41 52 4e 49 4e 47 3a 20 20 49 6e 20 6f 72   WARNING:  In or
285cf 64 65 72 20 74 6f 20 6d 61 69 6e 74 61 69 6e 20  der to maintain 
285d0 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74  backwards compat
285d1 69 62 69 6c 69 74 79 2c 20 61 64 64 20 6e 65 77  ibility, add new
285d2 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 73 20 74  .** interfaces t
285d3 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 69  o the end of thi
285d4 73 20 73 74 72 75 63 74 75 72 65 20 6f 6e 6c 79  s structure only
285d5 2e 20 20 49 66 20 79 6f 75 20 69 6e 73 65 72 74  .  If you insert
285d6 20 6e 65 77 0a 2a 2a 20 69 6e 74 65 72 66 61 63   new.** interfac
285d7 65 73 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  es in the middle
285d8 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
285d9 72 65 2c 20 74 68 65 6e 20 6f 6c 64 65 72 20 64  re, then older d
285da 69 66 66 65 72 65 6e 74 0a 2a 2a 20 76 65 72 73  ifferent.** vers
285db 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 77  ions of SQLite w
285dc 69 6c 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20  ill not be able 
285dd 74 6f 20 6c 6f 61 64 20 65 61 63 68 20 6f 74 68  to load each oth
285de 65 72 73 27 20 73 68 61 72 65 64 0a 2a 2a 20 6c  ers' shared.** l
285df 69 62 72 61 72 69 65 73 21 0a 2a 2f 0a 73 74 72  ibraries!.*/.str
285e0 75 63 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  uct sqlite3_api_
285e1 72 6f 75 74 69 6e 65 73 20 7b 0a 20 20 76 6f 69  routines {.  voi
285e2 64 20 2a 20 28 2a 61 67 67 72 65 67 61 74 65 5f  d * (*aggregate_
285e3 63 6f 6e 74 65 78 74 29 28 73 71 6c 69 74 65 33  context)(sqlite3
285e4 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 20 6e 42  _context*,int nB
285e5 79 74 65 73 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ytes);.  int  (*
285e6 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74 29  aggregate_count)
285e7 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
285e8 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e  *);.  int  (*bin
285e9 64 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f  d_blob)(sqlite3_
285ea 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  stmt*,int,const 
285eb 76 6f 69 64 2a 2c 69 6e 74 20 6e 2c 76 6f 69 64  void*,int n,void
285ec 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69  (*)(void*));.  i
285ed 6e 74 20 20 28 2a 62 69 6e 64 5f 64 6f 75 62 6c  nt  (*bind_doubl
285ee 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  e)(sqlite3_stmt*
285ef 2c 69 6e 74 2c 64 6f 75 62 6c 65 29 3b 0a 20 20  ,int,double);.  
285f0 69 6e 74 20 20 28 2a 62 69 6e 64 5f 69 6e 74 29  int  (*bind_int)
285f1 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
285f2 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20  nt,int);.  int  
285f3 28 2a 62 69 6e 64 5f 69 6e 74 36 34 29 28 73 71  (*bind_int64)(sq
285f4 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c  lite3_stmt*,int,
285f5 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a 20  sqlite_int64);. 
285f6 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 6e 75 6c   int  (*bind_nul
285f7 6c 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  l)(sqlite3_stmt*
285f8 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ,int);.  int  (*
285f9 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63  bind_parameter_c
285fa 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74  ount)(sqlite3_st
285fb 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62  mt*);.  int  (*b
285fc 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e  ind_parameter_in
285fd 64 65 78 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  dex)(sqlite3_stm
285fe 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 7a 4e  t*,const char*zN
285ff 61 6d 65 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68  ame);.  const ch
28600 61 72 20 2a 20 28 2a 62 69 6e 64 5f 70 61 72 61  ar * (*bind_para
28601 6d 65 74 65 72 5f 6e 61 6d 65 29 28 73 71 6c 69  meter_name)(sqli
28602 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a  te3_stmt*,int);.
28603 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 74 65    int  (*bind_te
28604 78 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  xt)(sqlite3_stmt
28605 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72  *,int,const char
28606 2a 2c 69 6e 74 20 6e 2c 76 6f 69 64 28 2a 29 28  *,int n,void(*)(
28607 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20  void*));.  int  
28608 28 2a 62 69 6e 64 5f 74 65 78 74 31 36 29 28 73  (*bind_text16)(s
28609 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
2860a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
2860b 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29  ,void(*)(void*))
2860c 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f  ;.  int  (*bind_
2860d 76 61 6c 75 65 29 28 73 71 6c 69 74 65 33 5f 73  value)(sqlite3_s
2860e 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 73  tmt*,int,const s
2860f 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
28610 20 20 69 6e 74 20 20 28 2a 62 75 73 79 5f 68 61    int  (*busy_ha
28611 6e 64 6c 65 72 29 28 73 71 6c 69 74 65 33 2a 2c  ndler)(sqlite3*,
28612 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74  int(*)(void*,int
28613 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20  ),void*);.  int 
28614 20 28 2a 62 75 73 79 5f 74 69 6d 65 6f 75 74 29   (*busy_timeout)
28615 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 6d 73  (sqlite3*,int ms
28616 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 68 61 6e  );.  int  (*chan
28617 67 65 73 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a  ges)(sqlite3*);.
28618 20 20 69 6e 74 20 20 28 2a 63 6c 6f 73 65 29 28    int  (*close)(
28619 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 69 6e 74  sqlite3*);.  int
2861a 20 20 28 2a 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65    (*collation_ne
2861b 65 64 65 64 29 28 73 71 6c 69 74 65 33 2a 2c 76  eded)(sqlite3*,v
2861c 6f 69 64 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69  oid*,void(*)(voi
2861d 64 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20  d*,sqlite3*,int 
2861e 65 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 63  eTextRep,const c
2861f 68 61 72 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28  har*));.  int  (
28620 2a 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  *collation_neede
28621 64 31 36 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f  d16)(sqlite3*,vo
28622 69 64 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  id*,void(*)(void
28623 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65  *,sqlite3*,int e
28624 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f  TextRep,const vo
28625 69 64 2a 29 29 3b 0a 20 20 63 6f 6e 73 74 20 76  id*));.  const v
28626 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 62  oid * (*column_b
28627 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  lob)(sqlite3_stm
28628 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20  t*,int iCol);.  
28629 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 62 79  int  (*column_by
2862a 74 65 73 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  tes)(sqlite3_stm
2862b 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20  t*,int iCol);.  
2862c 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 62 79  int  (*column_by
2862d 74 65 73 31 36 29 28 73 71 6c 69 74 65 33 5f 73  tes16)(sqlite3_s
2862e 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a  tmt*,int iCol);.
2862f 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f    int  (*column_
28630 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 73  count)(sqlite3_s
28631 74 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20 63 6f  tmt*pStmt);.  co
28632 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c  nst char * (*col
28633 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
28634 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  e)(sqlite3_stmt*
28635 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 76  ,int);.  const v
28636 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64  oid * (*column_d
28637 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 29 28  atabase_name16)(
28638 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
28639 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  t);.  const char
2863a 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 65 63 6c   * (*column_decl
2863b 74 79 70 65 29 28 73 71 6c 69 74 65 33 5f 73 74  type)(sqlite3_st
2863c 6d 74 2a 2c 69 6e 74 20 69 29 3b 0a 20 20 63 6f  mt*,int i);.  co
2863d 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c  nst void * (*col
2863e 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 29 28  umn_decltype16)(
2863f 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
28640 74 29 3b 0a 20 20 64 6f 75 62 6c 65 20 20 28 2a  t);.  double  (*
28641 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 29 28 73  column_double)(s
28642 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
28643 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28   iCol);.  int  (
28644 2a 63 6f 6c 75 6d 6e 5f 69 6e 74 29 28 73 71 6c  *column_int)(sql
28645 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69  ite3_stmt*,int i
28646 43 6f 6c 29 3b 0a 20 20 73 71 6c 69 74 65 5f 69  Col);.  sqlite_i
28647 6e 74 36 34 20 20 28 2a 63 6f 6c 75 6d 6e 5f 69  nt64  (*column_i
28648 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f 73 74  nt64)(sqlite3_st
28649 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20  mt*,int iCol);. 
2864a 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a   const char * (*
2864b 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 29 28 73 71 6c  column_name)(sql
2864c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
2864d 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20  .  const void * 
2864e 28 2a 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 29  (*column_name16)
2864f 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
28650 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  nt);.  const cha
28651 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6f 72 69  r * (*column_ori
28652 67 69 6e 5f 6e 61 6d 65 29 28 73 71 6c 69 74 65  gin_name)(sqlite
28653 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20  3_stmt*,int);.  
28654 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 63  const void * (*c
28655 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d  olumn_origin_nam
28656 65 31 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  e16)(sqlite3_stm
28657 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74  t*,int);.  const
28658 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e   char * (*column
28659 5f 74 61 62 6c 65 5f 6e 61 6d 65 29 28 73 71 6c  _table_name)(sql
2865a 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
2865b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20  .  const void * 
2865c 28 2a 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  (*column_table_n
2865d 61 6d 65 31 36 29 28 73 71 6c 69 74 65 33 5f 73  ame16)(sqlite3_s
2865e 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e  tmt*,int);.  con
2865f 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
28660 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 65 78 74   * (*column_text
28661 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
28662 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 63 6f 6e  int iCol);.  con
28663 73 74 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75  st void * (*colu
28664 6d 6e 5f 74 65 78 74 31 36 29 28 73 71 6c 69 74  mn_text16)(sqlit
28665 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f  e3_stmt*,int iCo
28666 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c  l);.  int  (*col
28667 75 6d 6e 5f 74 79 70 65 29 28 73 71 6c 69 74 65  umn_type)(sqlite
28668 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c  3_stmt*,int iCol
28669 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  );.  sqlite3_val
2866a 75 65 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 76 61 6c  ue* (*column_val
2866b 75 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ue)(sqlite3_stmt
2866c 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 76  *,int iCol);.  v
2866d 6f 69 64 20 2a 20 28 2a 63 6f 6d 6d 69 74 5f 68  oid * (*commit_h
2866e 6f 6f 6b 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e  ook)(sqlite3*,in
2866f 74 28 2a 29 28 76 6f 69 64 2a 29 2c 76 6f 69 64  t(*)(void*),void
28670 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6d  *);.  int  (*com
28671 70 6c 65 74 65 29 28 63 6f 6e 73 74 20 63 68 61  plete)(const cha
28672 72 2a 73 71 6c 29 3b 0a 20 20 69 6e 74 20 20 28  r*sql);.  int  (
28673 2a 63 6f 6d 70 6c 65 74 65 31 36 29 28 63 6f 6e  *complete16)(con
28674 73 74 20 76 6f 69 64 2a 73 71 6c 29 3b 0a 20 20  st void*sql);.  
28675 69 6e 74 20 20 28 2a 63 72 65 61 74 65 5f 63 6f  int  (*create_co
28676 6c 6c 61 74 69 6f 6e 29 28 73 71 6c 69 74 65 33  llation)(sqlite3
28677 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e  *,const char*,in
28678 74 2c 76 6f 69 64 2a 2c 69 6e 74 28 2a 29 28 76  t,void*,int(*)(v
28679 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
2867a 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
2867b 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28  oid*));.  int  (
2867c 2a 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  *create_collatio
2867d 6e 31 36 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  n16)(sqlite3*,co
2867e 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f  nst void*,int,vo
2867f 69 64 2a 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a  id*,int(*)(void*
28680 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
28681 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
28682 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 72 65  ));.  int  (*cre
28683 61 74 65 5f 66 75 6e 63 74 69 6f 6e 29 28 73 71  ate_function)(sq
28684 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
28685 72 2a 2c 69 6e 74 2c 69 6e 74 2c 76 6f 69 64 2a  r*,int,int,void*
28686 2c 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73  ,void (*xFunc)(s
28687 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
28688 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
28689 65 2a 2a 29 2c 76 6f 69 64 20 28 2a 78 53 74 65  e**),void (*xSte
2868a 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  p)(sqlite3_conte
2868b 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
2868c 76 61 6c 75 65 2a 2a 29 2c 76 6f 69 64 20 28 2a  value**),void (*
2868d 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f  xFinal)(sqlite3_
2868e 63 6f 6e 74 65 78 74 2a 29 29 3b 0a 20 20 69 6e  context*));.  in
2868f 74 20 20 28 2a 63 72 65 61 74 65 5f 66 75 6e 63  t  (*create_func
28690 74 69 6f 6e 31 36 29 28 73 71 6c 69 74 65 33 2a  tion16)(sqlite3*
28691 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
28692 2c 69 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69 64 20  ,int,void*,void 
28693 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33  (*xFunc)(sqlite3
28694 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71  _context*,int,sq
28695 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 76  lite3_value**),v
28696 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c  oid (*xStep)(sql
28697 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
28698 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
28699 2a 29 2c 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c  *),void (*xFinal
2869a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
2869b 74 2a 29 29 3b 0a 20 20 69 6e 74 20 28 2a 63 72  t*));.  int (*cr
2869c 65 61 74 65 5f 6d 6f 64 75 6c 65 29 28 73 71 6c  eate_module)(sql
2869d 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
2869e 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  *,const sqlite3_
2869f 6d 6f 64 75 6c 65 2a 2c 76 6f 69 64 2a 29 3b 0a  module*,void*);.
286a0 20 20 69 6e 74 20 20 28 2a 64 61 74 61 5f 63 6f    int  (*data_co
286a1 75 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  unt)(sqlite3_stm
286a2 74 2a 70 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69  t*pStmt);.  sqli
286a3 74 65 33 20 2a 20 28 2a 64 62 5f 68 61 6e 64 6c  te3 * (*db_handl
286a4 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  e)(sqlite3_stmt*
286a5 29 3b 0a 20 20 69 6e 74 20 28 2a 64 65 63 6c 61  );.  int (*decla
286a6 72 65 5f 76 74 61 62 29 28 73 71 6c 69 74 65 33  re_vtab)(sqlite3
286a7 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  *,const char*);.
286a8 20 20 69 6e 74 20 20 28 2a 65 6e 61 62 6c 65 5f    int  (*enable_
286a9 73 68 61 72 65 64 5f 63 61 63 68 65 29 28 69 6e  shared_cache)(in
286aa 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 65 72 72  t);.  int  (*err
286ab 63 6f 64 65 29 28 73 71 6c 69 74 65 33 2a 64 62  code)(sqlite3*db
286ac 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  );.  const char 
286ad 2a 20 28 2a 65 72 72 6d 73 67 29 28 73 71 6c 69  * (*errmsg)(sqli
286ae 74 65 33 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76  te3*);.  const v
286af 6f 69 64 20 2a 20 28 2a 65 72 72 6d 73 67 31 36  oid * (*errmsg16
286b0 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 69  )(sqlite3*);.  i
286b1 6e 74 20 20 28 2a 65 78 65 63 29 28 73 71 6c 69  nt  (*exec)(sqli
286b2 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
286b3 2c 73 71 6c 69 74 65 33 5f 63 61 6c 6c 62 61 63  ,sqlite3_callbac
286b4 6b 2c 76 6f 69 64 2a 2c 63 68 61 72 2a 2a 29 3b  k,void*,char**);
286b5 0a 20 20 69 6e 74 20 20 28 2a 65 78 70 69 72 65  .  int  (*expire
286b6 64 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  d)(sqlite3_stmt*
286b7 29 3b 0a 20 20 69 6e 74 20 20 28 2a 66 69 6e 61  );.  int  (*fina
286b8 6c 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 73 74  lize)(sqlite3_st
286b9 6d 74 2a 70 53 74 6d 74 29 3b 0a 20 20 76 6f 69  mt*pStmt);.  voi
286ba 64 20 20 28 2a 66 72 65 65 29 28 76 6f 69 64 2a  d  (*free)(void*
286bb 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 66 72 65  );.  void  (*fre
286bc 65 5f 74 61 62 6c 65 29 28 63 68 61 72 2a 2a 72  e_table)(char**r
286bd 65 73 75 6c 74 29 3b 0a 20 20 69 6e 74 20 20 28  esult);.  int  (
286be 2a 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 29  *get_autocommit)
286bf 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 76 6f  (sqlite3*);.  vo
286c0 69 64 20 2a 20 28 2a 67 65 74 5f 61 75 78 64 61  id * (*get_auxda
286c1 74 61 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ta)(sqlite3_cont
286c2 65 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74  ext*,int);.  int
286c3 20 20 28 2a 67 65 74 5f 74 61 62 6c 65 29 28 73    (*get_table)(s
286c4 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
286c5 61 72 2a 2c 63 68 61 72 2a 2a 2a 2c 69 6e 74 2a  ar*,char***,int*
286c6 2c 69 6e 74 2a 2c 63 68 61 72 2a 2a 29 3b 0a 20  ,int*,char**);. 
286c7 20 69 6e 74 20 20 28 2a 67 6c 6f 62 61 6c 5f 72   int  (*global_r
286c8 65 63 6f 76 65 72 29 28 76 6f 69 64 29 3b 0a 20  ecover)(void);. 
286c9 20 76 6f 69 64 20 20 28 2a 69 6e 74 65 72 72 75   void  (*interru
286ca 70 74 78 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a  ptx)(sqlite3*);.
286cb 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 20    sqlite_int64  
286cc 28 2a 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f  (*last_insert_ro
286cd 77 69 64 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a  wid)(sqlite3*);.
286ce 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28    const char * (
286cf 2a 6c 69 62 76 65 72 73 69 6f 6e 29 28 76 6f 69  *libversion)(voi
286d0 64 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6c 69 62  d);.  int  (*lib
286d1 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 29 28  version_number)(
286d2 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 2a 28  void);.  void *(
286d3 2a 6d 61 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a 20  *malloc)(int);. 
286d4 20 63 68 61 72 20 2a 20 28 2a 6d 70 72 69 6e 74   char * (*mprint
286d5 66 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e  f)(const char*,.
286d6 2e 2e 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6f 70  ..);.  int  (*op
286d7 65 6e 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  en)(const char*,
286d8 73 71 6c 69 74 65 33 2a 2a 29 3b 0a 20 20 69 6e  sqlite3**);.  in
286d9 74 20 20 28 2a 6f 70 65 6e 31 36 29 28 63 6f 6e  t  (*open16)(con
286da 73 74 20 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33  st void*,sqlite3
286db 2a 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 70 72  **);.  int  (*pr
286dc 65 70 61 72 65 29 28 73 71 6c 69 74 65 33 2a 2c  epare)(sqlite3*,
286dd 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c  const char*,int,
286de 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63  sqlite3_stmt**,c
286df 6f 6e 73 74 20 63 68 61 72 2a 2a 29 3b 0a 20 20  onst char**);.  
286e0 69 6e 74 20 20 28 2a 70 72 65 70 61 72 65 31 36  int  (*prepare16
286e1 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  )(sqlite3*,const
286e2 20 76 6f 69 64 2a 2c 69 6e 74 2c 73 71 6c 69 74   void*,int,sqlit
286e3 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20  e3_stmt**,const 
286e4 76 6f 69 64 2a 2a 29 3b 0a 20 20 76 6f 69 64 20  void**);.  void 
286e5 2a 20 28 2a 70 72 6f 66 69 6c 65 29 28 73 71 6c  * (*profile)(sql
286e6 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28 76 6f  ite3*,void(*)(vo
286e7 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  id*,const char*,
286e8 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 2c 76  sqlite_uint64),v
286e9 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28  oid*);.  void  (
286ea 2a 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65  *progress_handle
286eb 72 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c  r)(sqlite3*,int,
286ec 69 6e 74 28 2a 29 28 76 6f 69 64 2a 29 2c 76 6f  int(*)(void*),vo
286ed 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a  id*);.  void *(*
286ee 72 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c 69  realloc)(void*,i
286ef 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28 2a 72 65  nt);.  int  (*re
286f0 73 65 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  set)(sqlite3_stm
286f1 74 2a 70 53 74 6d 74 29 3b 0a 20 20 76 6f 69 64  t*pStmt);.  void
286f2 20 20 28 2a 72 65 73 75 6c 74 5f 62 6c 6f 62 29    (*result_blob)
286f3 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
286f4 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  *,const void*,in
286f5 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  t,void(*)(void*)
286f6 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73  );.  void  (*res
286f7 75 6c 74 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69  ult_double)(sqli
286f8 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 64 6f 75  te3_context*,dou
286f9 62 6c 65 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  ble);.  void  (*
286fa 72 65 73 75 6c 74 5f 65 72 72 6f 72 29 28 73 71  result_error)(sq
286fb 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63  lite3_context*,c
286fc 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b  onst char*,int);
286fd 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c  .  void  (*resul
286fe 74 5f 65 72 72 6f 72 31 36 29 28 73 71 6c 69 74  t_error16)(sqlit
286ff 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73  e3_context*,cons
28700 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 0a 20 20  t void*,int);.  
28701 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 69  void  (*result_i
28702 6e 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nt)(sqlite3_cont
28703 65 78 74 2a 2c 69 6e 74 29 3b 0a 20 20 76 6f 69  ext*,int);.  voi
28704 64 20 20 28 2a 72 65 73 75 6c 74 5f 69 6e 74 36  d  (*result_int6
28705 34 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  4)(sqlite3_conte
28706 78 74 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34  xt*,sqlite_int64
28707 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73  );.  void  (*res
28708 75 6c 74 5f 6e 75 6c 6c 29 28 73 71 6c 69 74 65  ult_null)(sqlite
28709 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 76  3_context*);.  v
2870a 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74 65  oid  (*result_te
2870b 78 74 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  xt)(sqlite3_cont
2870c 65 78 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  ext*,const char*
2870d 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69  ,int,void(*)(voi
2870e 64 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  d*));.  void  (*
2870f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 29 28 73  result_text16)(s
28710 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
28711 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
28712 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
28713 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75 6c  .  void  (*resul
28714 74 5f 74 65 78 74 31 36 62 65 29 28 73 71 6c 69  t_text16be)(sqli
28715 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e  te3_context*,con
28716 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69  st void*,int,voi
28717 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20  d(*)(void*));.  
28718 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74  void  (*result_t
28719 65 78 74 31 36 6c 65 29 28 73 71 6c 69 74 65 33  ext16le)(sqlite3
2871a 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20  _context*,const 
2871b 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a  void*,int,void(*
2871c 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69  )(void*));.  voi
2871d 64 20 20 28 2a 72 65 73 75 6c 74 5f 76 61 6c 75  d  (*result_valu
2871e 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  e)(sqlite3_conte
2871f 78 74 2a 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  xt*,sqlite3_valu
28720 65 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a  e*);.  void * (*
28721 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 29 28 73  rollback_hook)(s
28722 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a 29 28  qlite3*,void(*)(
28723 76 6f 69 64 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20  void*),void*);. 
28724 20 69 6e 74 20 20 28 2a 73 65 74 5f 61 75 74 68   int  (*set_auth
28725 6f 72 69 7a 65 72 29 28 73 71 6c 69 74 65 33 2a  orizer)(sqlite3*
28726 2c 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e  ,int(*)(void*,in
28727 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  t,const char*,co
28728 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
28729 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
2872a 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69  *),void*);.  voi
2872b 64 20 20 28 2a 73 65 74 5f 61 75 78 64 61 74 61  d  (*set_auxdata
2872c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
2872d 74 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69  t*,int,void*,voi
2872e 64 20 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20  d (*)(void*));. 
2872f 20 63 68 61 72 20 2a 20 28 2a 73 6e 70 72 69 6e   char * (*snprin
28730 74 66 29 28 69 6e 74 2c 63 68 61 72 2a 2c 63 6f  tf)(int,char*,co
28731 6e 73 74 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a  nst char*,...);.
28732 20 20 69 6e 74 20 20 28 2a 73 74 65 70 29 28 73    int  (*step)(s
28733 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20  qlite3_stmt*);. 
28734 20 69 6e 74 20 20 28 2a 74 61 62 6c 65 5f 63 6f   int  (*table_co
28735 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 29 28 73  lumn_metadata)(s
28736 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
28737 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
28738 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 68 61 72  const char*,char
28739 20 63 6f 6e 73 74 2a 2a 2c 63 68 61 72 20 63 6f   const**,char co
2873a 6e 73 74 2a 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c  nst**,int*,int*,
2873b 69 6e 74 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28  int*);.  void  (
2873c 2a 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 29  *thread_cleanup)
2873d 28 76 6f 69 64 29 3b 0a 20 20 69 6e 74 20 20 28  (void);.  int  (
2873e 2a 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 29 28  *total_changes)(
2873f 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 76 6f 69  sqlite3*);.  voi
28740 64 20 2a 20 28 2a 74 72 61 63 65 29 28 73 71 6c  d * (*trace)(sql
28741 69 74 65 33 2a 2c 76 6f 69 64 28 2a 78 54 72 61  ite3*,void(*xTra
28742 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20  ce)(void*,const 
28743 63 68 61 72 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20  char*),void*);. 
28744 20 69 6e 74 20 20 28 2a 74 72 61 6e 73 66 65 72   int  (*transfer
28745 5f 62 69 6e 64 69 6e 67 73 29 28 73 71 6c 69 74  _bindings)(sqlit
28746 65 33 5f 73 74 6d 74 2a 2c 73 71 6c 69 74 65 33  e3_stmt*,sqlite3
28747 5f 73 74 6d 74 2a 29 3b 0a 20 20 76 6f 69 64 20  _stmt*);.  void 
28748 2a 20 28 2a 75 70 64 61 74 65 5f 68 6f 6f 6b 29  * (*update_hook)
28749 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64 28 2a  (sqlite3*,void(*
2874a 29 28 76 6f 69 64 2a 2c 69 6e 74 20 2c 63 68 61  )(void*,int ,cha
2874b 72 20 63 6f 6e 73 74 2a 2c 63 68 61 72 20 63 6f  r const*,char co
2874c 6e 73 74 2a 2c 73 71 6c 69 74 65 5f 69 6e 74 36  nst*,sqlite_int6
2874d 34 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69  4),void*);.  voi
2874e 64 20 2a 20 28 2a 75 73 65 72 5f 64 61 74 61 29  d * (*user_data)
2874f 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
28750 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  *);.  const void
28751 20 2a 20 28 2a 76 61 6c 75 65 5f 62 6c 6f 62 29   * (*value_blob)
28752 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
28753 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75 65  ;.  int  (*value
28754 5f 62 79 74 65 73 29 28 73 71 6c 69 74 65 33 5f  _bytes)(sqlite3_
28755 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20  value*);.  int  
28756 28 2a 76 61 6c 75 65 5f 62 79 74 65 73 31 36 29  (*value_bytes16)
28757 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
28758 3b 0a 20 20 64 6f 75 62 6c 65 20 20 28 2a 76 61  ;.  double  (*va
28759 6c 75 65 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69  lue_double)(sqli
2875a 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69  te3_value*);.  i
2875b 6e 74 20 20 28 2a 76 61 6c 75 65 5f 69 6e 74 29  nt  (*value_int)
2875c 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
2875d 3b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ;.  sqlite_int64
2875e 20 20 28 2a 76 61 6c 75 65 5f 69 6e 74 36 34 29    (*value_int64)
2875f 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
28760 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75 65  ;.  int  (*value
28761 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 29 28 73  _numeric_type)(s
28762 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a  qlite3_value*);.
28763 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
28764 20 63 68 61 72 20 2a 20 28 2a 76 61 6c 75 65 5f   char * (*value_
28765 74 65 78 74 29 28 73 71 6c 69 74 65 33 5f 76 61  text)(sqlite3_va
28766 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76  lue*);.  const v
28767 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 74 65  oid * (*value_te
28768 78 74 31 36 29 28 73 71 6c 69 74 65 33 5f 76 61  xt16)(sqlite3_va
28769 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 76  lue*);.  const v
2876a 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f 74 65  oid * (*value_te
2876b 78 74 31 36 62 65 29 28 73 71 6c 69 74 65 33 5f  xt16be)(sqlite3_
2876c 76 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74  value*);.  const
2876d 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c 75 65 5f   void * (*value_
2876e 74 65 78 74 31 36 6c 65 29 28 73 71 6c 69 74 65  text16le)(sqlite
2876f 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74  3_value*);.  int
28770 20 20 28 2a 76 61 6c 75 65 5f 74 79 70 65 29 28    (*value_type)(
28771 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
28772 0a 20 20 63 68 61 72 20 2a 28 2a 76 6d 70 72 69  .  char *(*vmpri
28773 6e 74 66 29 28 63 6f 6e 73 74 20 63 68 61 72 2a  ntf)(const char*
28774 2c 76 61 5f 6c 69 73 74 29 3b 0a 20 20 2f 2a 20  ,va_list);.  /* 
28775 41 64 64 65 64 20 3f 3f 3f 20 2a 2f 0a 20 20 69  Added ??? */.  i
28776 6e 74 20 28 2a 6f 76 65 72 6c 6f 61 64 5f 66 75  nt (*overload_fu
28777 6e 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 2a  nction)(sqlite3*
28778 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
28779 75 6e 63 4e 61 6d 65 2c 20 69 6e 74 20 6e 41 72  uncName, int nAr
2877a 67 29 3b 0a 20 20 2f 2a 20 41 64 64 65 64 20 62  g);.  /* Added b
2877b 79 20 33 2e 33 2e 31 33 20 2a 2f 0a 20 20 69 6e  y 3.3.13 */.  in
2877c 74 20 28 2a 70 72 65 70 61 72 65 5f 76 32 29 28  t (*prepare_v2)(
2877d 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
2877e 68 61 72 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  har*,int,sqlite3
2877f 5f 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 63 68  _stmt**,const ch
28780 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 70  ar**);.  int (*p
28781 72 65 70 61 72 65 31 36 5f 76 32 29 28 73 71 6c  repare16_v2)(sql
28782 69 74 65 33 2a 2c 63 6f 6e 73 74 20 76 6f 69 64  ite3*,const void
28783 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 73 74  *,int,sqlite3_st
28784 6d 74 2a 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  mt**,const void*
28785 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 63 6c 65 61  *);.  int (*clea
28786 72 5f 62 69 6e 64 69 6e 67 73 29 28 73 71 6c 69  r_bindings)(sqli
28787 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 2f 2a  te3_stmt*);.  /*
28788 20 41 64 64 65 64 20 62 79 20 33 2e 34 2e 31 20   Added by 3.4.1 
28789 2a 2f 0a 20 20 69 6e 74 20 28 2a 63 72 65 61 74  */.  int (*creat
2878a 65 5f 6d 6f 64 75 6c 65 5f 76 32 29 28 73 71 6c  e_module_v2)(sql
2878b 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
2878c 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  *,const sqlite3_
2878d 6d 6f 64 75 6c 65 2a 2c 76 6f 69 64 2a 2c 76 6f  module*,void*,vo
2878e 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29 28 76  id (*xDestroy)(v
2878f 6f 69 64 20 2a 29 29 3b 0a 20 20 2f 2a 20 41 64  oid *));.  /* Ad
28790 64 65 64 20 62 79 20 33 2e 35 2e 30 20 2a 2f 0a  ded by 3.5.0 */.
28791 20 20 69 6e 74 20 28 2a 62 69 6e 64 5f 7a 65 72    int (*bind_zer
28792 6f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 73  oblob)(sqlite3_s
28793 74 6d 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20  tmt*,int,int);. 
28794 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 62 79 74 65   int (*blob_byte
28795 73 29 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a  s)(sqlite3_blob*
28796 29 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f  );.  int (*blob_
28797 63 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 62  close)(sqlite3_b
28798 6c 6f 62 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 62  lob*);.  int (*b
28799 6c 6f 62 5f 6f 70 65 6e 29 28 73 71 6c 69 74 65  lob_open)(sqlite
2879a 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63  3*,const char*,c
2879b 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
2879c 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 69   char*,sqlite3_i
2879d 6e 74 36 34 2c 69 6e 74 2c 73 71 6c 69 74 65 33  nt64,int,sqlite3
2879e 5f 62 6c 6f 62 2a 2a 29 3b 0a 20 20 69 6e 74 20  _blob**);.  int 
2879f 28 2a 62 6c 6f 62 5f 72 65 61 64 29 28 73 71 6c  (*blob_read)(sql
287a0 69 74 65 33 5f 62 6c 6f 62 2a 2c 76 6f 69 64 2a  ite3_blob*,void*
287a1 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74  ,int,int);.  int
287a2 20 28 2a 62 6c 6f 62 5f 77 72 69 74 65 29 28 73   (*blob_write)(s
287a3 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 2c 63 6f 6e  qlite3_blob*,con
287a4 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74  st void*,int,int
287a5 29 3b 0a 20 20 69 6e 74 20 28 2a 63 72 65 61 74  );.  int (*creat
287a6 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 29 28  e_collation_v2)(
287a7 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63  sqlite3*,const c
287a8 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 69  har*,int,void*,i
287a9 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  nt(*)(void*,int,
287aa 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c  const void*,int,
287ab 63 6f 6e 73 74 20 76 6f 69 64 2a 29 2c 76 6f 69  const void*),voi
287ac 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20  d(*)(void*));.  
287ad 69 6e 74 20 28 2a 66 69 6c 65 5f 63 6f 6e 74 72  int (*file_contr
287ae 6f 6c 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  ol)(sqlite3*,con
287af 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69  st char*,int,voi
287b0 64 2a 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69  d*);.  sqlite3_i
287b1 6e 74 36 34 20 28 2a 6d 65 6d 6f 72 79 5f 68 69  nt64 (*memory_hi
287b2 67 68 77 61 74 65 72 29 28 69 6e 74 29 3b 0a 20  ghwater)(int);. 
287b3 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 28   sqlite3_int64 (
287b4 2a 6d 65 6d 6f 72 79 5f 75 73 65 64 29 28 76 6f  *memory_used)(vo
287b5 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  id);.  sqlite3_m
287b6 75 74 65 78 20 2a 28 2a 6d 75 74 65 78 5f 61 6c  utex *(*mutex_al
287b7 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69  loc)(int);.  voi
287b8 64 20 28 2a 6d 75 74 65 78 5f 65 6e 74 65 72 29  d (*mutex_enter)
287b9 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29  (sqlite3_mutex*)
287ba 3b 0a 20 20 76 6f 69 64 20 28 2a 6d 75 74 65 78  ;.  void (*mutex
287bb 5f 66 72 65 65 29 28 73 71 6c 69 74 65 33 5f 6d  _free)(sqlite3_m
287bc 75 74 65 78 2a 29 3b 0a 20 20 76 6f 69 64 20 28  utex*);.  void (
287bd 2a 6d 75 74 65 78 5f 6c 65 61 76 65 29 28 73 71  *mutex_leave)(sq
287be 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 20  lite3_mutex*);. 
287bf 20 69 6e 74 20 28 2a 6d 75 74 65 78 5f 74 72 79   int (*mutex_try
287c0 29 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a  )(sqlite3_mutex*
287c1 29 3b 0a 20 20 69 6e 74 20 28 2a 6f 70 65 6e 5f  );.  int (*open_
287c2 76 32 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  v2)(const char*,
287c3 73 71 6c 69 74 65 33 2a 2a 2c 69 6e 74 2c 63 6f  sqlite3**,int,co
287c4 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20 20 69 6e  nst char*);.  in
287c5 74 20 28 2a 72 65 6c 65 61 73 65 5f 6d 65 6d 6f  t (*release_memo
287c6 72 79 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64  ry)(int);.  void
287c7 20 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f   (*result_error_
287c8 6e 6f 6d 65 6d 29 28 73 71 6c 69 74 65 33 5f 63  nomem)(sqlite3_c
287c9 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 76 6f 69 64  ontext*);.  void
287ca 20 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f   (*result_error_
287cb 74 6f 6f 62 69 67 29 28 73 71 6c 69 74 65 33 5f  toobig)(sqlite3_
287cc 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 69 6e 74  context*);.  int
287cd 20 28 2a 73 6c 65 65 70 29 28 69 6e 74 29 3b 0a   (*sleep)(int);.
287ce 20 20 76 6f 69 64 20 28 2a 73 6f 66 74 5f 68 65    void (*soft_he
287cf 61 70 5f 6c 69 6d 69 74 29 28 69 6e 74 29 3b 0a  ap_limit)(int);.
287d0 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 28    sqlite3_vfs *(
287d1 2a 76 66 73 5f 66 69 6e 64 29 28 63 6f 6e 73 74  *vfs_find)(const
287d2 20 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74 20 28   char*);.  int (
287d3 2a 76 66 73 5f 72 65 67 69 73 74 65 72 29 28 73  *vfs_register)(s
287d4 71 6c 69 74 65 33 5f 76 66 73 2a 2c 69 6e 74 29  qlite3_vfs*,int)
287d5 3b 0a 20 20 69 6e 74 20 28 2a 76 66 73 5f 75 6e  ;.  int (*vfs_un
287d6 72 65 67 69 73 74 65 72 29 28 73 71 6c 69 74 65  register)(sqlite
287d7 33 5f 76 66 73 2a 29 3b 0a 20 20 69 6e 74 20 28  3_vfs*);.  int (
287d8 2a 78 74 68 72 65 61 64 73 61 66 65 29 28 76 6f  *xthreadsafe)(vo
287d9 69 64 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65  id);.  void (*re
287da 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 29 28 73  sult_zeroblob)(s
287db 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
287dc 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28 2a 72  int);.  void (*r
287dd 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65  esult_error_code
287de 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
287df 74 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28  t*,int);.  int (
287e0 2a 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 29 28 69  *test_control)(i
287e1 6e 74 2c 20 2e 2e 2e 29 3b 0a 20 20 76 6f 69 64  nt, ...);.  void
287e2 20 28 2a 72 61 6e 64 6f 6d 6e 65 73 73 29 28 69   (*randomness)(i
287e3 6e 74 2c 76 6f 69 64 2a 29 3b 0a 20 20 73 71 6c  nt,void*);.  sql
287e4 69 74 65 33 20 2a 28 2a 63 6f 6e 74 65 78 74 5f  ite3 *(*context_
287e5 64 62 5f 68 61 6e 64 6c 65 29 28 73 71 6c 69 74  db_handle)(sqlit
287e6 65 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20  e3_context*);.  
287e7 69 6e 74 20 28 2a 65 78 74 65 6e 64 65 64 5f 72  int (*extended_r
287e8 65 73 75 6c 74 5f 63 6f 64 65 73 29 28 73 71 6c  esult_codes)(sql
287e9 69 74 65 33 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e  ite3*,int);.  in
287ea 74 20 28 2a 6c 69 6d 69 74 29 28 73 71 6c 69 74  t (*limit)(sqlit
287eb 65 33 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20  e3*,int,int);.  
287ec 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 28 2a  sqlite3_stmt *(*
287ed 6e 65 78 74 5f 73 74 6d 74 29 28 73 71 6c 69 74  next_stmt)(sqlit
287ee 65 33 2a 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74  e3*,sqlite3_stmt
287ef 2a 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  *);.  const char
287f0 20 2a 28 2a 73 71 6c 29 28 73 71 6c 69 74 65 33   *(*sql)(sqlite3
287f1 5f 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 28  _stmt*);.  int (
287f2 2a 73 74 61 74 75 73 29 28 69 6e 74 2c 69 6e 74  *status)(int,int
287f3 2a 2c 69 6e 74 2a 2c 69 6e 74 29 3b 0a 7d 3b 0a  *,int*,int);.};.
287f4 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
287f5 77 69 6e 67 20 6d 61 63 72 6f 73 20 72 65 64 65  wing macros rede
287f6 66 69 6e 65 20 74 68 65 20 41 50 49 20 72 6f 75  fine the API rou
287f7 74 69 6e 65 73 20 73 6f 20 74 68 61 74 20 74 68  tines so that th
287f8 65 79 20 61 72 65 0a 2a 2a 20 72 65 64 69 72 65  ey are.** redire
287f9 63 74 65 64 20 74 68 72 6f 75 67 68 74 20 74 68  cted throught th
287fa 65 20 67 6c 6f 62 61 6c 20 73 71 6c 69 74 65 33  e global sqlite3
287fb 5f 61 70 69 20 73 74 72 75 63 74 75 72 65 2e 0a  _api structure..
287fc 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65  **.** This heade
287fd 72 20 66 69 6c 65 20 69 73 20 61 6c 73 6f 20 75  r file is also u
287fe 73 65 64 20 62 79 20 74 68 65 20 6c 6f 61 64 65  sed by the loade
287ff 78 74 2e 63 20 73 6f 75 72 63 65 20 66 69 6c 65  xt.c source file
28800 0a 2a 2a 20 28 70 61 72 74 20 6f 66 20 74 68 65  .** (part of the
28801 20 6d 61 69 6e 20 53 51 4c 69 74 65 20 6c 69 62   main SQLite lib
28802 72 61 72 79 20 2d 20 6e 6f 74 20 61 6e 20 65 78  rary - not an ex
28803 74 65 6e 73 69 6f 6e 29 20 73 6f 20 74 68 61 74  tension) so that
28804 0a 2a 2a 20 69 74 20 63 61 6e 20 67 65 74 20 61  .** it can get a
28805 63 63 65 73 73 20 74 6f 20 74 68 65 20 73 71 6c  ccess to the sql
28806 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65  ite3_api_routine
28807 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 64  s structure.** d
28808 65 66 69 6e 69 74 69 6f 6e 2e 20 20 42 75 74 20  efinition.  But 
28809 74 68 65 20 6d 61 69 6e 20 6c 69 62 72 61 72 79  the main library
2880a 20 64 6f 65 73 20 6e 6f 74 20 77 61 6e 74 20 74   does not want t
2880b 6f 20 72 65 64 65 66 69 6e 65 0a 2a 2a 20 74 68  o redefine.** th
2880c 65 20 41 50 49 2e 20 20 53 6f 20 74 68 65 20 72  e API.  So the r
2880d 65 64 65 66 69 6e 69 74 69 6f 6e 20 6d 61 63 72  edefinition macr
2880e 6f 73 20 61 72 65 20 6f 6e 6c 79 20 76 61 6c 69  os are only vali
2880f 64 20 69 66 20 74 68 65 0a 2a 2a 20 53 51 4c 49  d if the.** SQLI
28810 54 45 5f 43 4f 52 45 20 6d 61 63 72 6f 73 20 69  TE_CORE macros i
28811 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a  s undefined..*/.
28812 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43  #ifndef SQLITE_C
28813 4f 52 45 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ORE.#define sqli
28814 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
28815 6e 74 65 78 74 20 20 20 20 20 20 73 71 6c 69 74  ntext      sqlit
28816 65 33 5f 61 70 69 2d 3e 61 67 67 72 65 67 61 74  e3_api->aggregat
28817 65 5f 63 6f 6e 74 65 78 74 0a 23 69 66 6e 64 65  e_context.#ifnde
28818 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
28819 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e 65  PRECATED.#define
2881a 20 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61   sqlite3_aggrega
2881b 74 65 5f 63 6f 75 6e 74 20 20 20 20 20 20 20 20  te_count        
2881c 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 61 67 67  sqlite3_api->agg
2881d 72 65 67 61 74 65 5f 63 6f 75 6e 74 0a 23 65 6e  regate_count.#en
2881e 64 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  dif.#define sqli
2881f 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 20 20 20  te3_bind_blob   
28820 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28821 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 62 6c 6f  e3_api->bind_blo
28822 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  b.#define sqlite
28823 33 5f 62 69 6e 64 5f 64 6f 75 62 6c 65 20 20 20  3_bind_double   
28824 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28825 5f 61 70 69 2d 3e 62 69 6e 64 5f 64 6f 75 62 6c  _api->bind_doubl
28826 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
28827 33 5f 62 69 6e 64 5f 69 6e 74 20 20 20 20 20 20  3_bind_int      
28828 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28829 5f 61 70 69 2d 3e 62 69 6e 64 5f 69 6e 74 0a 23  _api->bind_int.#
2882a 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
2882b 69 6e 64 5f 69 6e 74 36 34 20 20 20 20 20 20 20  ind_int64       
2882c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
2882d 69 2d 3e 62 69 6e 64 5f 69 6e 74 36 34 0a 23 64  i->bind_int64.#d
2882e 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
2882f 6e 64 5f 6e 75 6c 6c 20 20 20 20 20 20 20 20 20  nd_null         
28830 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
28831 2d 3e 62 69 6e 64 5f 6e 75 6c 6c 0a 23 64 65 66  ->bind_null.#def
28832 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
28833 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74  _parameter_count
28834 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28835 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63  bind_parameter_c
28836 6f 75 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c  ount.#define sql
28837 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
28838 74 65 72 5f 69 6e 64 65 78 20 20 20 73 71 6c 69  ter_index   sqli
28839 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70 61  te3_api->bind_pa
2883a 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 0a 23 64  rameter_index.#d
2883b 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
2883c 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d  nd_parameter_nam
2883d 65 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69  e    sqlite3_api
2883e 2d 3e 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72  ->bind_parameter
2883f 5f 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71  _name.#define sq
28840 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 20  lite3_bind_text 
28841 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
28842 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 74  ite3_api->bind_t
28843 65 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ext.#define sqli
28844 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 20  te3_bind_text16 
28845 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28846 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 74 65 78  e3_api->bind_tex
28847 74 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  t16.#define sqli
28848 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 20 20  te3_bind_value  
28849 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2884a 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 76 61 6c  e3_api->bind_val
2884b 75 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ue.#define sqlit
2884c 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 20  e3_busy_handler 
2884d 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2884e 33 5f 61 70 69 2d 3e 62 75 73 79 5f 68 61 6e 64  3_api->busy_hand
2884f 6c 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ler.#define sqli
28850 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74  te3_busy_timeout
28851 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28852 65 33 5f 61 70 69 2d 3e 62 75 73 79 5f 74 69 6d  e3_api->busy_tim
28853 65 6f 75 74 0a 23 64 65 66 69 6e 65 20 73 71 6c  eout.#define sql
28854 69 74 65 33 5f 63 68 61 6e 67 65 73 20 20 20 20  ite3_changes    
28855 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
28856 74 65 33 5f 61 70 69 2d 3e 63 68 61 6e 67 65 73  te3_api->changes
28857 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28858 5f 63 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20  _close          
28859 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2885a 61 70 69 2d 3e 63 6c 6f 73 65 0a 23 64 65 66 69  api->close.#defi
2885b 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61  ne sqlite3_colla
2885c 74 69 6f 6e 5f 6e 65 65 64 65 64 20 20 20 20 20  tion_needed     
2885d 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
2885e 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 0a  ollation_needed.
2885f 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28860 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
28861 31 36 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  16     sqlite3_a
28862 70 69 2d 3e 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65  pi->collation_ne
28863 65 64 65 64 31 36 0a 23 64 65 66 69 6e 65 20 73  eded16.#define s
28864 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
28865 6f 62 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ob            sq
28866 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d  lite3_api->colum
28867 6e 5f 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73  n_blob.#define s
28868 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
28869 74 65 73 20 20 20 20 20 20 20 20 20 20 20 73 71  tes           sq
2886a 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d  lite3_api->colum
2886b 6e 5f 62 79 74 65 73 0a 23 64 65 66 69 6e 65 20  n_bytes.#define 
2886c 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
2886d 79 74 65 73 31 36 20 20 20 20 20 20 20 20 20 73  ytes16         s
2886e 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
2886f 6d 6e 5f 62 79 74 65 73 31 36 0a 23 64 65 66 69  mn_bytes16.#defi
28870 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
28871 6e 5f 63 6f 75 6e 74 20 20 20 20 20 20 20 20 20  n_count         
28872 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
28873 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 0a 23 64 65 66  olumn_count.#def
28874 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
28875 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
28876 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28877 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f  column_database_
28878 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  name.#define sql
28879 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61  ite3_column_data
2887a 62 61 73 65 5f 6e 61 6d 65 31 36 20 73 71 6c 69  base_name16 sqli
2887b 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
2887c 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 0a  database_name16.
2887d 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
2887e 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 20  column_decltype 
2887f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28880 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74  pi->column_declt
28881 79 70 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ype.#define sqli
28882 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74  te3_column_declt
28883 79 70 65 31 36 20 20 20 20 20 20 73 71 6c 69 74  ype16      sqlit
28884 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64  e3_api->column_d
28885 65 63 6c 74 79 70 65 31 36 0a 23 64 65 66 69 6e  ecltype16.#defin
28886 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
28887 5f 64 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20  _double         
28888 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f   sqlite3_api->co
28889 6c 75 6d 6e 5f 64 6f 75 62 6c 65 0a 23 64 65 66  lumn_double.#def
2888a 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
2888b 6d 6e 5f 69 6e 74 20 20 20 20 20 20 20 20 20 20  mn_int          
2888c 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
2888d 63 6f 6c 75 6d 6e 5f 69 6e 74 0a 23 64 65 66 69  column_int.#defi
2888e 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ne sqlite3_colum
2888f 6e 5f 69 6e 74 36 34 20 20 20 20 20 20 20 20 20  n_int64         
28890 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
28891 6f 6c 75 6d 6e 5f 69 6e 74 36 34 0a 23 64 65 66  olumn_int64.#def
28892 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
28893 6d 6e 5f 6e 61 6d 65 20 20 20 20 20 20 20 20 20  mn_name         
28894 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28895 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 0a 23 64 65 66  column_name.#def
28896 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
28897 6d 6e 5f 6e 61 6d 65 31 36 20 20 20 20 20 20 20  mn_name16       
28898 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28899 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 0a 23 64  column_name16.#d
2889a 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
2889b 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
2889c 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2889d 2d 3e 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f  ->column_origin_
2889e 6e 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  name.#define sql
2889f 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
288a0 69 6e 5f 6e 61 6d 65 31 36 20 20 20 73 71 6c 69  in_name16   sqli
288a1 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f  te3_api->column_
288a2 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 0a 23 64  origin_name16.#d
288a3 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
288a4 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 20  lumn_table_name 
288a5 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
288a6 2d 3e 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  ->column_table_n
288a7 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ame.#define sqli
288a8 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65  te3_column_table
288a9 5f 6e 61 6d 65 31 36 20 20 20 20 73 71 6c 69 74  _name16    sqlit
288aa 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74  e3_api->column_t
288ab 61 62 6c 65 5f 6e 61 6d 65 31 36 0a 23 64 65 66  able_name16.#def
288ac 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
288ad 6d 6e 5f 74 65 78 74 20 20 20 20 20 20 20 20 20  mn_text         
288ae 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
288af 63 6f 6c 75 6d 6e 5f 74 65 78 74 0a 23 64 65 66  column_text.#def
288b0 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
288b1 6d 6e 5f 74 65 78 74 31 36 20 20 20 20 20 20 20  mn_text16       
288b2 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
288b3 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 0a 23 64  column_text16.#d
288b4 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
288b5 6c 75 6d 6e 5f 74 79 70 65 20 20 20 20 20 20 20  lumn_type       
288b6 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
288b7 2d 3e 63 6f 6c 75 6d 6e 5f 74 79 70 65 0a 23 64  ->column_type.#d
288b8 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
288b9 6c 75 6d 6e 5f 76 61 6c 75 65 20 20 20 20 20 20  lumn_value      
288ba 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
288bb 2d 3e 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65 0a 23  ->column_value.#
288bc 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
288bd 6f 6d 6d 69 74 5f 68 6f 6f 6b 20 20 20 20 20 20  ommit_hook      
288be 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
288bf 69 2d 3e 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 0a 23  i->commit_hook.#
288c0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
288c1 6f 6d 70 6c 65 74 65 20 20 20 20 20 20 20 20 20  omplete         
288c2 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
288c3 69 2d 3e 63 6f 6d 70 6c 65 74 65 0a 23 64 65 66  i->complete.#def
288c4 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  ine sqlite3_comp
288c5 6c 65 74 65 31 36 20 20 20 20 20 20 20 20 20 20  lete16          
288c6 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
288c7 63 6f 6d 70 6c 65 74 65 31 36 0a 23 64 65 66 69  complete16.#defi
288c8 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
288c9 65 5f 63 6f 6c 6c 61 74 69 6f 6e 20 20 20 20 20  e_collation     
288ca 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
288cb 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 0a  reate_collation.
288cc 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
288cd 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
288ce 31 36 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  16     sqlite3_a
288cf 70 69 2d 3e 63 72 65 61 74 65 5f 63 6f 6c 6c 61  pi->create_colla
288d0 74 69 6f 6e 31 36 0a 23 64 65 66 69 6e 65 20 73  tion16.#define s
288d1 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
288d2 6e 63 74 69 6f 6e 20 20 20 20 20 20 20 20 73 71  nction        sq
288d3 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74  lite3_api->creat
288d4 65 5f 66 75 6e 63 74 69 6f 6e 0a 23 64 65 66 69  e_function.#defi
288d5 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
288d6 65 5f 66 75 6e 63 74 69 6f 6e 31 36 20 20 20 20  e_function16    
288d7 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
288d8 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36  reate_function16
288d9 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
288da 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 20 20  _create_module  
288db 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
288dc 61 70 69 2d 3e 63 72 65 61 74 65 5f 6d 6f 64 75  api->create_modu
288dd 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  le.#define sqlit
288de 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
288df 5f 76 32 20 20 20 20 20 20 20 73 71 6c 69 74 65  _v2       sqlite
288e0 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 6d 6f  3_api->create_mo
288e1 64 75 6c 65 5f 76 32 0a 23 64 65 66 69 6e 65 20  dule_v2.#define 
288e2 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63 6f 75  sqlite3_data_cou
288e3 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73  nt             s
288e4 71 6c 69 74 65 33 5f 61 70 69 2d 3e 64 61 74 61  qlite3_api->data
288e5 5f 63 6f 75 6e 74 0a 23 64 65 66 69 6e 65 20 73  _count.#define s
288e6 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65  qlite3_db_handle
288e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
288e8 6c 69 74 65 33 5f 61 70 69 2d 3e 64 62 5f 68 61  lite3_api->db_ha
288e9 6e 64 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  ndle.#define sql
288ea 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61  ite3_declare_vta
288eb 62 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  b           sqli
288ec 74 65 33 5f 61 70 69 2d 3e 64 65 63 6c 61 72 65  te3_api->declare
288ed 5f 76 74 61 62 0a 23 64 65 66 69 6e 65 20 73 71  _vtab.#define sq
288ee 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61  lite3_enable_sha
288ef 72 65 64 5f 63 61 63 68 65 20 20 20 20 73 71 6c  red_cache    sql
288f0 69 74 65 33 5f 61 70 69 2d 3e 65 6e 61 62 6c 65  ite3_api->enable
288f1 5f 73 68 61 72 65 64 5f 63 61 63 68 65 0a 23 64  _shared_cache.#d
288f2 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 72  efine sqlite3_er
288f3 72 63 6f 64 65 20 20 20 20 20 20 20 20 20 20 20  rcode           
288f4 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
288f5 2d 3e 65 72 72 63 6f 64 65 0a 23 64 65 66 69 6e  ->errcode.#defin
288f6 65 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67  e sqlite3_errmsg
288f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
288f8 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 72   sqlite3_api->er
288f9 72 6d 73 67 0a 23 64 65 66 69 6e 65 20 73 71 6c  rmsg.#define sql
288fa 69 74 65 33 5f 65 72 72 6d 73 67 31 36 20 20 20  ite3_errmsg16   
288fb 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
288fc 74 65 33 5f 61 70 69 2d 3e 65 72 72 6d 73 67 31  te3_api->errmsg1
288fd 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
288fe 33 5f 65 78 65 63 20 20 20 20 20 20 20 20 20 20  3_exec          
288ff 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28900 5f 61 70 69 2d 3e 65 78 65 63 0a 23 69 66 6e 64  _api->exec.#ifnd
28901 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
28902 45 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e  EPRECATED.#defin
28903 65 20 73 71 6c 69 74 65 33 5f 65 78 70 69 72 65  e sqlite3_expire
28904 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
28905 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 78   sqlite3_api->ex
28906 70 69 72 65 64 0a 23 65 6e 64 69 66 0a 23 64 65  pired.#endif.#de
28907 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 69 6e  fine sqlite3_fin
28908 61 6c 69 7a 65 20 20 20 20 20 20 20 20 20 20 20  alize           
28909 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
2890a 3e 66 69 6e 61 6c 69 7a 65 0a 23 64 65 66 69 6e  >finalize.#defin
2890b 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 20 20  e sqlite3_free  
2890c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2890d 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 66 72   sqlite3_api->fr
2890e 65 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ee.#define sqlit
2890f 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 20 20 20  e3_free_table   
28910 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28911 33 5f 61 70 69 2d 3e 66 72 65 65 5f 74 61 62 6c  3_api->free_tabl
28912 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
28913 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74  3_get_autocommit
28914 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28915 5f 61 70 69 2d 3e 67 65 74 5f 61 75 74 6f 63 6f  _api->get_autoco
28916 6d 6d 69 74 0a 23 64 65 66 69 6e 65 20 73 71 6c  mmit.#define sql
28917 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61  ite3_get_auxdata
28918 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
28919 74 65 33 5f 61 70 69 2d 3e 67 65 74 5f 61 75 78  te3_api->get_aux
2891a 64 61 74 61 0a 23 64 65 66 69 6e 65 20 73 71 6c  data.#define sql
2891b 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 20 20  ite3_get_table  
2891c 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2891d 74 65 33 5f 61 70 69 2d 3e 67 65 74 5f 74 61 62  te3_api->get_tab
2891e 6c 65 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  le.#ifndef SQLIT
2891f 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
28920 44 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  D.#define sqlite
28921 33 5f 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72  3_global_recover
28922 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28923 5f 61 70 69 2d 3e 67 6c 6f 62 61 6c 5f 72 65 63  _api->global_rec
28924 6f 76 65 72 0a 23 65 6e 64 69 66 0a 23 64 65 66  over.#endif.#def
28925 69 6e 65 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  ine sqlite3_inte
28926 72 72 75 70 74 20 20 20 20 20 20 20 20 20 20 20  rrupt           
28927 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28928 69 6e 74 65 72 72 75 70 74 78 0a 23 64 65 66 69  interruptx.#defi
28929 6e 65 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f  ne sqlite3_last_
2892a 69 6e 73 65 72 74 5f 72 6f 77 69 64 20 20 20 20  insert_rowid    
2892b 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c    sqlite3_api->l
2892c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64  ast_insert_rowid
2892d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
2892e 5f 6c 69 62 76 65 72 73 69 6f 6e 20 20 20 20 20  _libversion     
2892f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28930 61 70 69 2d 3e 6c 69 62 76 65 72 73 69 6f 6e 0a  api->libversion.
28931 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28932 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65  libversion_numbe
28933 72 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  r      sqlite3_a
28934 70 69 2d 3e 6c 69 62 76 65 72 73 69 6f 6e 5f 6e  pi->libversion_n
28935 75 6d 62 65 72 0a 23 64 65 66 69 6e 65 20 73 71  umber.#define sq
28936 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 20 20 20  lite3_malloc    
28937 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
28938 69 74 65 33 5f 61 70 69 2d 3e 6d 61 6c 6c 6f 63  ite3_api->malloc
28939 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
2893a 5f 6d 70 72 69 6e 74 66 20 20 20 20 20 20 20 20  _mprintf        
2893b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
2893c 61 70 69 2d 3e 6d 70 72 69 6e 74 66 0a 23 64 65  api->mprintf.#de
2893d 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f 70 65  fine sqlite3_ope
2893e 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n               
2893f 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
28940 3e 6f 70 65 6e 0a 23 64 65 66 69 6e 65 20 73 71  >open.#define sq
28941 6c 69 74 65 33 5f 6f 70 65 6e 31 36 20 20 20 20  lite3_open16    
28942 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
28943 69 74 65 33 5f 61 70 69 2d 3e 6f 70 65 6e 31 36  ite3_api->open16
28944 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28945 5f 70 72 65 70 61 72 65 20 20 20 20 20 20 20 20  _prepare        
28946 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28947 61 70 69 2d 3e 70 72 65 70 61 72 65 0a 23 64 65  api->prepare.#de
28948 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65  fine sqlite3_pre
28949 70 61 72 65 31 36 20 20 20 20 20 20 20 20 20 20  pare16          
2894a 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
2894b 3e 70 72 65 70 61 72 65 31 36 0a 23 64 65 66 69  >prepare16.#defi
2894c 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  ne sqlite3_prepa
2894d 72 65 5f 76 32 20 20 20 20 20 20 20 20 20 20 20  re_v2           
2894e 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70    sqlite3_api->p
2894f 72 65 70 61 72 65 5f 76 32 0a 23 64 65 66 69 6e  repare_v2.#defin
28950 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  e sqlite3_prepar
28951 65 31 36 5f 76 32 20 20 20 20 20 20 20 20 20 20  e16_v2          
28952 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72   sqlite3_api->pr
28953 65 70 61 72 65 31 36 5f 76 32 0a 23 64 65 66 69  epare16_v2.#defi
28954 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69  ne sqlite3_profi
28955 6c 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  le              
28956 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70    sqlite3_api->p
28957 72 6f 66 69 6c 65 0a 23 64 65 66 69 6e 65 20 73  rofile.#define s
28958 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f  qlite3_progress_
28959 68 61 6e 64 6c 65 72 20 20 20 20 20 20 20 73 71  handler       sq
2895a 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 6f 67 72  lite3_api->progr
2895b 65 73 73 5f 68 61 6e 64 6c 65 72 0a 23 64 65 66  ess_handler.#def
2895c 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  ine sqlite3_real
2895d 6c 6f 63 20 20 20 20 20 20 20 20 20 20 20 20 20  loc             
2895e 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
2895f 72 65 61 6c 6c 6f 63 0a 23 64 65 66 69 6e 65 20  realloc.#define 
28960 73 71 6c 69 74 65 33 5f 72 65 73 65 74 20 20 20  sqlite3_reset   
28961 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
28962 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 65  qlite3_api->rese
28963 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  t.#define sqlite
28964 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 20 20  3_result_blob   
28965 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28966 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 62 6c 6f  _api->result_blo
28967 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  b.#define sqlite
28968 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 20  3_result_double 
28969 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2896a 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 64 6f 75  _api->result_dou
2896b 62 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ble.#define sqli
2896c 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2896d 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2896e 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65  e3_api->result_e
2896f 72 72 6f 72 0a 23 64 65 66 69 6e 65 20 73 71 6c  rror.#define sql
28970 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
28971 72 31 36 20 20 20 20 20 20 20 20 20 73 71 6c 69  r16         sqli
28972 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f  te3_api->result_
28973 65 72 72 6f 72 31 36 0a 23 64 65 66 69 6e 65 20  error16.#define 
28974 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
28975 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73  nt             s
28976 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75  qlite3_api->resu
28977 6c 74 5f 69 6e 74 0a 23 64 65 66 69 6e 65 20 73  lt_int.#define s
28978 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
28979 74 36 34 20 20 20 20 20 20 20 20 20 20 20 73 71  t64           sq
2897a 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
2897b 74 5f 69 6e 74 36 34 0a 23 64 65 66 69 6e 65 20  t_int64.#define 
2897c 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
2897d 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 73  ull            s
2897e 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75  qlite3_api->resu
2897f 6c 74 5f 6e 75 6c 6c 0a 23 64 65 66 69 6e 65 20  lt_null.#define 
28980 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
28981 65 78 74 20 20 20 20 20 20 20 20 20 20 20 20 73  ext            s
28982 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75  qlite3_api->resu
28983 6c 74 5f 74 65 78 74 0a 23 64 65 66 69 6e 65 20  lt_text.#define 
28984 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
28985 65 78 74 31 36 20 20 20 20 20 20 20 20 20 20 73  ext16          s
28986 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75  qlite3_api->resu
28987 6c 74 5f 74 65 78 74 31 36 0a 23 64 65 66 69 6e  lt_text16.#defin
28988 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
28989 5f 74 65 78 74 31 36 62 65 20 20 20 20 20 20 20  _text16be       
2898a 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65   sqlite3_api->re
2898b 73 75 6c 74 5f 74 65 78 74 31 36 62 65 0a 23 64  sult_text16be.#d
2898c 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
2898d 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 20 20 20  sult_text16le   
2898e 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
2898f 2d 3e 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c  ->result_text16l
28990 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
28991 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 20 20  3_result_value  
28992 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28993 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 76 61 6c  _api->result_val
28994 75 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ue.#define sqlit
28995 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b  e3_rollback_hook
28996 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28997 33 5f 61 70 69 2d 3e 72 6f 6c 6c 62 61 63 6b 5f  3_api->rollback_
28998 68 6f 6f 6b 0a 23 64 65 66 69 6e 65 20 73 71 6c  hook.#define sql
28999 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69  ite3_set_authori
2899a 7a 65 72 20 20 20 20 20 20 20 20 20 73 71 6c 69  zer         sqli
2899b 74 65 33 5f 61 70 69 2d 3e 73 65 74 5f 61 75 74  te3_api->set_aut
2899c 68 6f 72 69 7a 65 72 0a 23 64 65 66 69 6e 65 20  horizer.#define 
2899d 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64  sqlite3_set_auxd
2899e 61 74 61 20 20 20 20 20 20 20 20 20 20 20 20 73  ata            s
2899f 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 65 74 5f  qlite3_api->set_
289a0 61 75 78 64 61 74 61 0a 23 64 65 66 69 6e 65 20  auxdata.#define 
289a1 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
289a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
289a3 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 6e 70 72  qlite3_api->snpr
289a4 69 6e 74 66 0a 23 64 65 66 69 6e 65 20 73 71 6c  intf.#define sql
289a5 69 74 65 33 5f 73 74 65 70 20 20 20 20 20 20 20  ite3_step       
289a6 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
289a7 74 65 33 5f 61 70 69 2d 3e 73 74 65 70 0a 23 64  te3_api->step.#d
289a8 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 61  efine sqlite3_ta
289a9 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64  ble_column_metad
289aa 61 74 61 20 20 73 71 6c 69 74 65 33 5f 61 70 69  ata  sqlite3_api
289ab 2d 3e 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d  ->table_column_m
289ac 65 74 61 64 61 74 61 0a 23 64 65 66 69 6e 65 20  etadata.#define 
289ad 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 5f 63  sqlite3_thread_c
289ae 6c 65 61 6e 75 70 20 20 20 20 20 20 20 20 20 73  leanup         s
289af 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 68 72 65  qlite3_api->thre
289b0 61 64 5f 63 6c 65 61 6e 75 70 0a 23 64 65 66 69  ad_cleanup.#defi
289b1 6e 65 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c  ne sqlite3_total
289b2 5f 63 68 61 6e 67 65 73 20 20 20 20 20 20 20 20  _changes        
289b3 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74    sqlite3_api->t
289b4 6f 74 61 6c 5f 63 68 61 6e 67 65 73 0a 23 64 65  otal_changes.#de
289b5 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 72 61  fine sqlite3_tra
289b6 63 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ce              
289b7 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
289b8 3e 74 72 61 63 65 0a 23 69 66 6e 64 65 66 20 53  >trace.#ifndef S
289b9 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45  QLITE_OMIT_DEPRE
289ba 43 41 54 45 44 0a 23 64 65 66 69 6e 65 20 73 71  CATED.#define sq
289bb 6c 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62  lite3_transfer_b
289bc 69 6e 64 69 6e 67 73 20 20 20 20 20 20 73 71 6c  indings      sql
289bd 69 74 65 33 5f 61 70 69 2d 3e 74 72 61 6e 73 66  ite3_api->transf
289be 65 72 5f 62 69 6e 64 69 6e 67 73 0a 23 65 6e 64  er_bindings.#end
289bf 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  if.#define sqlit
289c0 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 20 20  e3_update_hook  
289c1 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
289c2 33 5f 61 70 69 2d 3e 75 70 64 61 74 65 5f 68 6f  3_api->update_ho
289c3 6f 6b 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ok.#define sqlit
289c4 65 33 5f 75 73 65 72 5f 64 61 74 61 20 20 20 20  e3_user_data    
289c5 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
289c6 33 5f 61 70 69 2d 3e 75 73 65 72 5f 64 61 74 61  3_api->user_data
289c7 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
289c8 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 20 20 20 20  _value_blob     
289c9 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
289ca 61 70 69 2d 3e 76 61 6c 75 65 5f 62 6c 6f 62 0a  api->value_blob.
289cb 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
289cc 76 61 6c 75 65 5f 62 79 74 65 73 20 20 20 20 20  value_bytes     
289cd 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
289ce 70 69 2d 3e 76 61 6c 75 65 5f 62 79 74 65 73 0a  pi->value_bytes.
289cf 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
289d0 76 61 6c 75 65 5f 62 79 74 65 73 31 36 20 20 20  value_bytes16   
289d1 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
289d2 70 69 2d 3e 76 61 6c 75 65 5f 62 79 74 65 73 31  pi->value_bytes1
289d3 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
289d4 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 20 20  3_value_double  
289d5 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
289d6 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 64 6f 75 62  _api->value_doub
289d7 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  le.#define sqlit
289d8 65 33 5f 76 61 6c 75 65 5f 69 6e 74 20 20 20 20  e3_value_int    
289d9 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
289da 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 69 6e 74  3_api->value_int
289db 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
289dc 5f 76 61 6c 75 65 5f 69 6e 74 36 34 20 20 20 20  _value_int64    
289dd 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
289de 61 70 69 2d 3e 76 61 6c 75 65 5f 69 6e 74 36 34  api->value_int64
289df 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
289e0 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74  _value_numeric_t
289e1 79 70 65 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ype     sqlite3_
289e2 61 70 69 2d 3e 76 61 6c 75 65 5f 6e 75 6d 65 72  api->value_numer
289e3 69 63 5f 74 79 70 65 0a 23 64 65 66 69 6e 65 20  ic_type.#define 
289e4 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
289e5 78 74 20 20 20 20 20 20 20 20 20 20 20 20 20 73  xt             s
289e6 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75  qlite3_api->valu
289e7 65 5f 74 65 78 74 0a 23 64 65 66 69 6e 65 20 73  e_text.#define s
289e8 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
289e9 74 31 36 20 20 20 20 20 20 20 20 20 20 20 73 71  t16           sq
289ea 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65  lite3_api->value
289eb 5f 74 65 78 74 31 36 0a 23 64 65 66 69 6e 65 20  _text16.#define 
289ec 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
289ed 78 74 31 36 62 65 20 20 20 20 20 20 20 20 20 73  xt16be         s
289ee 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75  qlite3_api->valu
289ef 65 5f 74 65 78 74 31 36 62 65 0a 23 64 65 66 69  e_text16be.#defi
289f0 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ne sqlite3_value
289f1 5f 74 65 78 74 31 36 6c 65 20 20 20 20 20 20 20  _text16le       
289f2 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76    sqlite3_api->v
289f3 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 0a 23 64  alue_text16le.#d
289f4 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61  efine sqlite3_va
289f5 6c 75 65 5f 74 79 70 65 20 20 20 20 20 20 20 20  lue_type        
289f6 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
289f7 2d 3e 76 61 6c 75 65 5f 74 79 70 65 0a 23 64 65  ->value_type.#de
289f8 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 6d 70  fine sqlite3_vmp
289f9 72 69 6e 74 66 20 20 20 20 20 20 20 20 20 20 20  rintf           
289fa 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
289fb 3e 76 6d 70 72 69 6e 74 66 0a 23 64 65 66 69 6e  >vmprintf.#defin
289fc 65 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f  e sqlite3_overlo
289fd 61 64 5f 66 75 6e 63 74 69 6f 6e 20 20 20 20 20  ad_function     
289fe 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 76   sqlite3_api->ov
289ff 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 0a  erload_function.
28a00 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28a01 70 72 65 70 61 72 65 5f 76 32 20 20 20 20 20 20  prepare_v2      
28a02 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28a03 70 69 2d 3e 70 72 65 70 61 72 65 5f 76 32 0a 23  pi->prepare_v2.#
28a04 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70  define sqlite3_p
28a05 72 65 70 61 72 65 31 36 5f 76 32 20 20 20 20 20  repare16_v2     
28a06 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28a07 69 2d 3e 70 72 65 70 61 72 65 31 36 5f 76 32 0a  i->prepare16_v2.
28a08 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28a09 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 20 20  clear_bindings  
28a0a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28a0b 70 69 2d 3e 63 6c 65 61 72 5f 62 69 6e 64 69 6e  pi->clear_bindin
28a0c 67 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  gs.#define sqlit
28a0d 65 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62  e3_bind_zeroblob
28a0e 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28a0f 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 7a 65 72 6f  3_api->bind_zero
28a10 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20 73 71 6c  blob.#define sql
28a11 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 20  ite3_blob_bytes 
28a12 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
28a13 74 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 62 79  te3_api->blob_by
28a14 74 65 73 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  tes.#define sqli
28a15 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 20  te3_blob_close  
28a16 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
28a17 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 63 6c 6f  e3_api->blob_clo
28a18 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  se.#define sqlit
28a19 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 20 20 20  e3_blob_open    
28a1a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
28a1b 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f 6f 70 65 6e  3_api->blob_open
28a1c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28a1d 5f 62 6c 6f 62 5f 72 65 61 64 20 20 20 20 20 20  _blob_read      
28a1e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28a1f 61 70 69 2d 3e 62 6c 6f 62 5f 72 65 61 64 0a 23  api->blob_read.#
28a20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
28a21 6c 6f 62 5f 77 72 69 74 65 20 20 20 20 20 20 20  lob_write       
28a22 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
28a23 69 2d 3e 62 6c 6f 62 5f 77 72 69 74 65 0a 23 64  i->blob_write.#d
28a24 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72  efine sqlite3_cr
28a25 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76  eate_collation_v
28a26 32 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69  2    sqlite3_api
28a27 2d 3e 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  ->create_collati
28a28 6f 6e 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71  on_v2.#define sq
28a29 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72  lite3_file_contr
28a2a 6f 6c 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  ol           sql
28a2b 69 74 65 33 5f 61 70 69 2d 3e 66 69 6c 65 5f 63  ite3_api->file_c
28a2c 6f 6e 74 72 6f 6c 0a 23 64 65 66 69 6e 65 20 73  ontrol.#define s
28a2d 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69  qlite3_memory_hi
28a2e 67 68 77 61 74 65 72 20 20 20 20 20 20 20 73 71  ghwater       sq
28a2f 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 65 6d 6f 72  lite3_api->memor
28a30 79 5f 68 69 67 68 77 61 74 65 72 0a 23 64 65 66  y_highwater.#def
28a31 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f  ine sqlite3_memo
28a32 72 79 5f 75 73 65 64 20 20 20 20 20 20 20 20 20  ry_used         
28a33 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28a34 6d 65 6d 6f 72 79 5f 75 73 65 64 0a 23 64 65 66  memory_used.#def
28a35 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ine sqlite3_mute
28a36 78 5f 61 6c 6c 6f 63 20 20 20 20 20 20 20 20 20  x_alloc         
28a37 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28a38 6d 75 74 65 78 5f 61 6c 6c 6f 63 0a 23 64 65 66  mutex_alloc.#def
28a39 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ine sqlite3_mute
28a3a 78 5f 65 6e 74 65 72 20 20 20 20 20 20 20 20 20  x_enter         
28a3b 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28a3c 6d 75 74 65 78 5f 65 6e 74 65 72 0a 23 64 65 66  mutex_enter.#def
28a3d 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ine sqlite3_mute
28a3e 78 5f 66 72 65 65 20 20 20 20 20 20 20 20 20 20  x_free          
28a3f 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
28a40 6d 75 74 65 78 5f 66 72 65 65 0a 23 64 65 66 69  mutex_free.#defi
28a41 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
28a42 5f 6c 65 61 76 65 20 20 20 20 20 20 20 20 20 20  _leave          
28a43 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
28a44 75 74 65 78 5f 6c 65 61 76 65 0a 23 64 65 66 69  utex_leave.#defi
28a45 6e 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ne sqlite3_mutex
28a46 5f 74 72 79 20 20 20 20 20 20 20 20 20 20 20 20  _try            
28a47 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d    sqlite3_api->m
28a48 75 74 65 78 5f 74 72 79 0a 23 64 65 66 69 6e 65  utex_try.#define
28a49 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32   sqlite3_open_v2
28a4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28a4b 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6f 70 65  sqlite3_api->ope
28a4c 6e 5f 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c  n_v2.#define sql
28a4d 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d  ite3_release_mem
28a4e 6f 72 79 20 20 20 20 20 20 20 20 20 73 71 6c 69  ory         sqli
28a4f 74 65 33 5f 61 70 69 2d 3e 72 65 6c 65 61 73 65  te3_api->release
28a50 5f 6d 65 6d 6f 72 79 0a 23 64 65 66 69 6e 65 20  _memory.#define 
28a51 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
28a52 72 72 6f 72 5f 6e 6f 6d 65 6d 20 20 20 20 20 73  rror_nomem     s
28a53 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75  qlite3_api->resu
28a54 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 0a 23  lt_error_nomem.#
28a55 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
28a56 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62  esult_error_toob
28a57 69 67 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70  ig    sqlite3_ap
28a58 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  i->result_error_
28a59 74 6f 6f 62 69 67 0a 23 64 65 66 69 6e 65 20 73  toobig.#define s
28a5a 71 6c 69 74 65 33 5f 73 6c 65 65 70 20 20 20 20  qlite3_sleep    
28a5b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
28a5c 6c 69 74 65 33 5f 61 70 69 2d 3e 73 6c 65 65 70  lite3_api->sleep
28a5d 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28a5e 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74  _soft_heap_limit
28a5f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28a60 61 70 69 2d 3e 73 6f 66 74 5f 68 65 61 70 5f 6c  api->soft_heap_l
28a61 69 6d 69 74 0a 23 64 65 66 69 6e 65 20 73 71 6c  imit.#define sql
28a62 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 20 20 20  ite3_vfs_find   
28a63 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
28a64 74 65 33 5f 61 70 69 2d 3e 76 66 73 5f 66 69 6e  te3_api->vfs_fin
28a65 64 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  d.#define sqlite
28a66 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 20 20  3_vfs_register  
28a67 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
28a68 5f 61 70 69 2d 3e 76 66 73 5f 72 65 67 69 73 74  _api->vfs_regist
28a69 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  er.#define sqlit
28a6a 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65  e3_vfs_unregiste
28a6b 72 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  r         sqlite
28a6c 33 5f 61 70 69 2d 3e 76 66 73 5f 75 6e 72 65 67  3_api->vfs_unreg
28a6d 69 73 74 65 72 0a 23 64 65 66 69 6e 65 20 73 71  ister.#define sq
28a6e 6c 69 74 65 33 5f 74 68 72 65 61 64 73 61 66 65  lite3_threadsafe
28a6f 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
28a70 69 74 65 33 5f 61 70 69 2d 3e 78 74 68 72 65 61  ite3_api->xthrea
28a71 64 73 61 66 65 0a 23 64 65 66 69 6e 65 20 73 71  dsafe.#define sq
28a72 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72  lite3_result_zer
28a73 6f 62 6c 6f 62 20 20 20 20 20 20 20 20 73 71 6c  oblob        sql
28a74 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74  ite3_api->result
28a75 5f 7a 65 72 6f 62 6c 6f 62 0a 23 64 65 66 69 6e  _zeroblob.#defin
28a76 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
28a77 5f 65 72 72 6f 72 5f 63 6f 64 65 20 20 20 20 20  _error_code     
28a78 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65   sqlite3_api->re
28a79 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 0a  sult_error_code.
28a7a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28a7b 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 20 20 20 20  test_control    
28a7c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28a7d 70 69 2d 3e 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  pi->test_control
28a7e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
28a7f 5f 72 61 6e 64 6f 6d 6e 65 73 73 20 20 20 20 20  _randomness     
28a80 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
28a81 61 70 69 2d 3e 72 61 6e 64 6f 6d 6e 65 73 73 0a  api->randomness.
28a82 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28a83 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
28a84 65 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  e      sqlite3_a
28a85 70 69 2d 3e 63 6f 6e 74 65 78 74 5f 64 62 5f 68  pi->context_db_h
28a86 61 6e 64 6c 65 0a 23 64 65 66 69 6e 65 20 73 71  andle.#define sq
28a87 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72  lite3_extended_r
28a88 65 73 75 6c 74 5f 63 6f 64 65 73 20 20 73 71 6c  esult_codes  sql
28a89 69 74 65 33 5f 61 70 69 2d 3e 65 78 74 65 6e 64  ite3_api->extend
28a8a 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 0a  ed_result_codes.
28a8b 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28a8c 6c 69 6d 69 74 20 20 20 20 20 20 20 20 20 20 20  limit           
28a8d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28a8e 70 69 2d 3e 6c 69 6d 69 74 0a 23 64 65 66 69 6e  pi->limit.#defin
28a8f 65 20 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73  e sqlite3_next_s
28a90 74 6d 74 20 20 20 20 20 20 20 20 20 20 20 20 20  tmt             
28a91 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6e 65   sqlite3_api->ne
28a92 78 74 5f 73 74 6d 74 0a 23 64 65 66 69 6e 65 20  xt_stmt.#define 
28a93 73 71 6c 69 74 65 33 5f 73 71 6c 20 20 20 20 20  sqlite3_sql     
28a94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
28a95 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 71 6c 0a  qlite3_api->sql.
28a96 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
28a97 73 74 61 74 75 73 20 20 20 20 20 20 20 20 20 20  status          
28a98 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
28a99 70 69 2d 3e 73 74 61 74 75 73 0a 23 65 6e 64 69  pi->status.#endi
28a9a 66 20 2f 2a 20 53 51 4c 49 54 45 5f 43 4f 52 45  f /* SQLITE_CORE
28a9b 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c   */..#define SQL
28a9c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e  ITE_EXTENSION_IN
28a9d 49 54 31 20 20 20 20 20 63 6f 6e 73 74 20 73 71  IT1     const sq
28a9e 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e  lite3_api_routin
28a9f 65 73 20 2a 73 71 6c 69 74 65 33 5f 61 70 69 20  es *sqlite3_api 
28aa0 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c  = 0;.#define SQL
28aa1 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e  ITE_EXTENSION_IN
28aa2 49 54 32 28 76 29 20 20 73 71 6c 69 74 65 33 5f  IT2(v)  sqlite3_
28aa3 61 70 69 20 3d 20 76 3b 0a 0a 23 65 6e 64 69 66  api = v;..#endif
28aa4 20 2f 2a 20 5f 53 51 4c 49 54 45 33 45 58 54 5f   /* _SQLITE3EXT_
28aa5 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  H_ */../********
28aa6 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 73 71  ****** End of sq
28aa7 6c 69 74 65 33 65 78 74 2e 68 20 2a 2a 2a 2a 2a  lite3ext.h *****
28aa8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28aa9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28aaa 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
28aab 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
28aac 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
28aad 6f 66 66 20 69 6e 20 6c 6f 61 64 65 78 74 2e 63  off in loadext.c
28aae 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
28aaf 2a 2a 2a 2a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  *****/..#ifndef 
28ab0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44  SQLITE_OMIT_LOAD
28ab1 5f 45 58 54 45 4e 53 49 4f 4e 0a 0a 2f 2a 0a 2a  _EXTENSION../*.*
28ab2 2a 20 53 6f 6d 65 20 41 50 49 20 72 6f 75 74 69  * Some API routi
28ab3 6e 65 73 20 61 72 65 20 6f 6d 69 74 74 65 64 20  nes are omitted 
28ab4 77 68 65 6e 20 76 61 72 69 6f 75 73 20 66 65 61  when various fea
28ab5 74 75 72 65 73 20 61 72 65 0a 2a 2a 20 65 78 63  tures are.** exc
28ab6 6c 75 64 65 64 20 66 72 6f 6d 20 61 20 62 75 69  luded from a bui
28ab7 6c 64 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 53  ld of SQLite.  S
28ab8 75 62 73 74 69 74 75 74 65 20 61 20 4e 55 4c 4c  ubstitute a NULL
28ab9 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 66 6f 72 20   pointer.** for 
28aba 61 6e 79 20 6d 69 73 73 69 6e 67 20 41 50 49 73  any missing APIs
28abb 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
28abc 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d  ITE_ENABLE_COLUM
28abd 4e 5f 4d 45 54 41 44 41 54 41 0a 23 20 64 65 66  N_METADATA.# def
28abe 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
28abf 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
28ac0 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
28ac1 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74  lite3_column_dat
28ac2 61 62 61 73 65 5f 6e 61 6d 65 31 36 20 30 0a 23  abase_name16 0.#
28ac3 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
28ac4 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d  column_table_nam
28ac5 65 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e  e      0.# defin
28ac6 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
28ac7 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 20 20 20  _table_name16   
28ac8 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
28ac9 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69  te3_column_origi
28aca 6e 5f 6e 61 6d 65 20 20 20 20 20 30 0a 23 20 64  n_name     0.# d
28acb 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
28acc 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
28acd 31 36 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20  16   0.# define 
28ace 73 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f  sqlite3_table_co
28acf 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 20 20 30  lumn_metadata  0
28ad0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
28ad1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
28ad2 4f 52 49 5a 41 54 49 4f 4e 0a 23 20 64 65 66 69  ORIZATION.# defi
28ad3 6e 65 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  ne sqlite3_set_a
28ad4 75 74 68 6f 72 69 7a 65 72 20 20 20 20 20 20 20  uthorizer       
28ad5 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64    0.#endif..#ifd
28ad6 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
28ad7 54 46 31 36 0a 23 20 64 65 66 69 6e 65 20 73 71  TF16.# define sq
28ad8 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31  lite3_bind_text1
28ad9 36 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 23  6            0.#
28ada 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
28adb 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64  collation_needed
28adc 31 36 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e  16     0.# defin
28add 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  e sqlite3_column
28ade 5f 64 65 63 6c 74 79 70 65 31 36 20 20 20 20 20  _decltype16     
28adf 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
28ae0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31  te3_column_name1
28ae1 36 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64  6          0.# d
28ae2 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
28ae3 6c 75 6d 6e 5f 74 65 78 74 31 36 20 20 20 20 20  lumn_text16     
28ae4 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
28ae5 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65  sqlite3_complete
28ae6 31 36 20 20 20 20 20 20 20 20 20 20 20 20 20 30  16             0
28ae7 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
28ae8 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69  3_create_collati
28ae9 6f 6e 31 36 20 20 20 20 20 30 0a 23 20 64 65 66  on16     0.# def
28aea 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61  ine sqlite3_crea
28aeb 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 20 20 20  te_function16   
28aec 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
28aed 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 20 20  lite3_errmsg16  
28aee 20 20 20 20 20 20 20 20 20 20 20 20 20 30 0a 23               0.#
28aef 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
28af0 6f 70 65 6e 31 36 20 20 20 20 20 20 20 20 20 20  open16          
28af1 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e         0.# defin
28af2 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  e sqlite3_prepar
28af3 65 31 36 20 20 20 20 20 20 20 20 20 20 20 20 20  e16             
28af4 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
28af5 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
28af6 20 20 20 20 20 20 20 20 20 20 20 30 0a 23 20 64             0.# d
28af7 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
28af8 73 75 6c 74 5f 65 72 72 6f 72 31 36 20 20 20 20  sult_error16    
28af9 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
28afa 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
28afb 65 78 74 31 36 20 20 20 20 20 20 20 20 20 20 30  ext16          0
28afc 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
28afd 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62  3_result_text16b
28afe 65 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66  e        0.# def
28aff 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ine sqlite3_resu
28b00 6c 74 5f 74 65 78 74 31 36 6c 65 20 20 20 20 20  lt_text16le     
28b01 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
28b02 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
28b03 31 36 20 20 20 20 20 20 20 20 20 20 20 30 0a 23  16           0.#
28b04 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
28b05 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 20 20  value_text16be  
28b06 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e         0.# defin
28b07 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  e sqlite3_value_
28b08 74 65 78 74 31 36 6c 65 20 20 20 20 20 20 20 20  text16le        
28b09 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
28b0a 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
28b0b 61 73 65 5f 6e 61 6d 65 31 36 20 30 0a 23 20 64  ase_name16 0.# d
28b0c 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
28b0d 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31  lumn_table_name1
28b0e 36 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20  6    0.# define 
28b0f 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f  sqlite3_column_o
28b10 72 69 67 69 6e 5f 6e 61 6d 65 31 36 20 20 20 30  rigin_name16   0
28b11 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20  .#endif..#ifdef 
28b12 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50  SQLITE_OMIT_COMP
28b13 4c 45 54 45 0a 23 20 64 65 66 69 6e 65 20 73 71  LETE.# define sq
28b14 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 20 30  lite3_complete 0
28b15 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
28b16 33 5f 63 6f 6d 70 6c 65 74 65 31 36 20 30 0a 23  3_complete16 0.#
28b17 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
28b18 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45  LITE_OMIT_PROGRE
28b19 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 23 20 64 65  SS_CALLBACK.# de
28b1a 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 6f  fine sqlite3_pro
28b1b 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 20 30 0a  gress_handler 0.
28b1c 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53  #endif..#ifdef S
28b1d 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
28b1e 41 4c 54 41 42 4c 45 0a 23 20 64 65 66 69 6e 65  ALTABLE.# define
28b1f 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
28b20 6d 6f 64 75 6c 65 20 30 0a 23 20 64 65 66 69 6e  module 0.# defin
28b21 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
28b22 5f 6d 6f 64 75 6c 65 5f 76 32 20 30 0a 23 20 64  _module_v2 0.# d
28b23 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64 65  efine sqlite3_de
28b24 63 6c 61 72 65 5f 76 74 61 62 20 30 0a 23 65 6e  clare_vtab 0.#en
28b25 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  dif..#ifdef SQLI
28b26 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
28b27 41 43 48 45 0a 23 20 64 65 66 69 6e 65 20 73 71  ACHE.# define sq
28b28 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61  lite3_enable_sha
28b29 72 65 64 5f 63 61 63 68 65 20 30 0a 23 65 6e 64  red_cache 0.#end
28b2a 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
28b2b 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 23 20 64  E_OMIT_TRACE.# d
28b2c 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72  efine sqlite3_pr
28b2d 6f 66 69 6c 65 20 20 20 20 20 20 20 30 0a 23 20  ofile       0.# 
28b2e 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74  define sqlite3_t
28b2f 72 61 63 65 20 20 20 20 20 20 20 20 20 30 0a 23  race         0.#
28b30 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
28b31 4c 49 54 45 5f 4f 4d 49 54 5f 47 45 54 5f 54 41  LITE_OMIT_GET_TA
28b32 42 4c 45 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  BLE.# define sql
28b33 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 20  ite3_free_table 
28b34 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
28b35 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 20  lite3_get_table 
28b36 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69      0.#endif..#i
28b37 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
28b38 5f 49 4e 43 52 42 4c 4f 42 0a 23 64 65 66 69 6e  _INCRBLOB.#defin
28b39 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a  e sqlite3_bind_z
28b3a 65 72 6f 62 6c 6f 62 20 20 30 0a 23 64 65 66 69  eroblob  0.#defi
28b3b 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  ne sqlite3_blob_
28b3c 62 79 74 65 73 20 20 20 20 20 30 0a 23 64 65 66  bytes     0.#def
28b3d 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ine sqlite3_blob
28b3e 5f 63 6c 6f 73 65 20 20 20 20 20 30 0a 23 64 65  _close     0.#de
28b3f 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f  fine sqlite3_blo
28b40 62 5f 6f 70 65 6e 20 20 20 20 20 20 30 0a 23 64  b_open      0.#d
28b41 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c  efine sqlite3_bl
28b42 6f 62 5f 72 65 61 64 20 20 20 20 20 20 30 0a 23  ob_read      0.#
28b43 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62  define sqlite3_b
28b44 6c 6f 62 5f 77 72 69 74 65 20 20 20 20 20 30 0a  lob_write     0.
28b45 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
28b46 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
28b47 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 70  cture contains p
28b48 6f 69 6e 74 65 72 73 20 74 6f 20 61 6c 6c 20 53  ointers to all S
28b49 51 4c 69 74 65 20 41 50 49 20 72 6f 75 74 69 6e  QLite API routin
28b4a 65 73 2e 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72  es..** A pointer
28b4b 20 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75   to this structu
28b4c 72 65 20 69 73 20 70 61 73 73 65 64 20 69 6e 74  re is passed int
28b4d 6f 20 65 78 74 65 6e 73 69 6f 6e 73 20 77 68 65  o extensions whe
28b4e 6e 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 6c 6f  n they are.** lo
28b4f 61 64 65 64 20 73 6f 20 74 68 61 74 20 74 68 65  aded so that the
28b50 20 65 78 74 65 6e 73 69 6f 6e 20 63 61 6e 20 6d   extension can m
28b51 61 6b 65 20 63 61 6c 6c 73 20 62 61 63 6b 20 69  ake calls back i
28b52 6e 74 6f 20 74 68 65 20 53 51 4c 69 74 65 0a 2a  nto the SQLite.*
28b53 2a 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a  * library..**.**
28b54 20 57 68 65 6e 20 61 64 64 69 6e 67 20 6e 65 77   When adding new
28b55 20 41 50 49 73 2c 20 61 64 64 20 74 68 65 6d 20   APIs, add them 
28b56 74 6f 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f 66  to the bottom of
28b57 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a   this structure.
28b58 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70  ** in order to p
28b59 72 65 73 65 72 76 65 20 62 61 63 6b 77 61 72 64  reserve backward
28b5a 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e  s compatibility.
28b5b 0a 2a 2a 0a 2a 2a 20 45 78 74 65 6e 73 69 6f 6e  .**.** Extension
28b5c 73 20 74 68 61 74 20 75 73 65 20 6e 65 77 65 72  s that use newer
28b5d 20 41 50 49 73 20 73 68 6f 75 6c 64 20 66 69 72   APIs should fir
28b5e 73 74 20 63 61 6c 6c 20 74 68 65 0a 2a 2a 20 73  st call the.** s
28b5f 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
28b60 6e 5f 6e 75 6d 62 65 72 28 29 20 74 6f 20 6d 61  n_number() to ma
28b61 6b 65 20 73 75 72 65 20 74 68 61 74 20 74 68 65  ke sure that the
28b62 20 41 50 49 20 74 68 65 79 0a 2a 2a 20 69 6e 74   API they.** int
28b63 65 6e 64 20 74 6f 20 75 73 65 20 69 73 20 73 75  end to use is su
28b64 70 70 6f 72 74 65 64 20 62 79 20 74 68 65 20 6c  pported by the l
28b65 69 62 72 61 72 79 2e 20 20 45 78 74 65 6e 73 69  ibrary.  Extensi
28b66 6f 6e 73 20 73 68 6f 75 6c 64 0a 2a 2a 20 61 6c  ons should.** al
28b67 73 6f 20 63 68 65 63 6b 20 74 6f 20 6d 61 6b 65  so check to make
28b68 20 73 75 72 65 20 74 68 61 74 20 74 68 65 20 70   sure that the p
28b69 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 75  ointer to the fu
28b6a 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74  nction is.** not
28b6b 20 4e 55 4c 4c 20 62 65 66 6f 72 65 20 63 61 6c   NULL before cal
28b6c 6c 69 6e 67 20 69 74 2e 0a 2a 2f 0a 73 74 61 74  ling it..*/.stat
28b6d 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
28b6e 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 20 73 71  _api_routines sq
28b6f 6c 69 74 65 33 41 70 69 73 20 3d 20 7b 0a 20 20  lite3Apis = {.  
28b70 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
28b71 65 5f 63 6f 6e 74 65 78 74 2c 0a 23 69 66 6e 64  e_context,.#ifnd
28b72 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
28b73 45 50 52 45 43 41 54 45 44 0a 20 20 73 71 6c 69  EPRECATED.  sqli
28b74 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
28b75 75 6e 74 2c 0a 23 65 6c 73 65 0a 20 20 30 2c 0a  unt,.#else.  0,.
28b76 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33  #endif.  sqlite3
28b77 5f 62 69 6e 64 5f 62 6c 6f 62 2c 0a 20 20 73 71  _bind_blob,.  sq
28b78 6c 69 74 65 33 5f 62 69 6e 64 5f 64 6f 75 62 6c  lite3_bind_doubl
28b79 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  e,.  sqlite3_bin
28b7a 64 5f 69 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33  d_int,.  sqlite3
28b7b 5f 62 69 6e 64 5f 69 6e 74 36 34 2c 0a 20 20 73  _bind_int64,.  s
28b7c 71 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c  qlite3_bind_null
28b7d 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ,.  sqlite3_bind
28b7e 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75 6e 74  _parameter_count
28b7f 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ,.  sqlite3_bind
28b80 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78  _parameter_index
28b81 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ,.  sqlite3_bind
28b82 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 2c  _parameter_name,
28b83 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
28b84 74 65 78 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  text,.  sqlite3_
28b85 62 69 6e 64 5f 74 65 78 74 31 36 2c 0a 20 20 73  bind_text16,.  s
28b86 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75  qlite3_bind_valu
28b87 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 75 73  e,.  sqlite3_bus
28b88 79 5f 68 61 6e 64 6c 65 72 2c 0a 20 20 73 71 6c  y_handler,.  sql
28b89 69 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75  ite3_busy_timeou
28b8a 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 68 61  t,.  sqlite3_cha
28b8b 6e 67 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f  nges,.  sqlite3_
28b8c 63 6c 6f 73 65 2c 0a 20 20 73 71 6c 69 74 65 33  close,.  sqlite3
28b8d 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
28b8e 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  d,.  sqlite3_col
28b8f 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 2c  lation_needed16,
28b90 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
28b91 6e 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65  n_blob,.  sqlite
28b92 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 2c 0a  3_column_bytes,.
28b93 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
28b94 5f 62 79 74 65 73 31 36 2c 0a 20 20 73 71 6c 69  _bytes16,.  sqli
28b95 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74  te3_column_count
28b96 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
28b97 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
28b98 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
28b99 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
28b9a 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  16,.  sqlite3_co
28b9b 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 2c 0a 20  lumn_decltype,. 
28b9c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
28b9d 64 65 63 6c 74 79 70 65 31 36 2c 0a 20 20 73 71  decltype16,.  sq
28b9e 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75  lite3_column_dou
28b9f 62 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  ble,.  sqlite3_c
28ba0 6f 6c 75 6d 6e 5f 69 6e 74 2c 0a 20 20 73 71 6c  olumn_int,.  sql
28ba1 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36  ite3_column_int6
28ba2 34 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  4,.  sqlite3_col
28ba3 75 6d 6e 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69  umn_name,.  sqli
28ba4 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31  te3_column_name1
28ba5 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  6,.  sqlite3_col
28ba6 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 2c  umn_origin_name,
28ba7 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
28ba8 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 2c  n_origin_name16,
28ba9 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  .  sqlite3_colum
28baa 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 2c 0a 20 20  n_table_name,.  
28bab 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
28bac 61 62 6c 65 5f 6e 61 6d 65 31 36 2c 0a 20 20 73  able_name16,.  s
28bad 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
28bae 78 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  xt,.  sqlite3_co
28baf 6c 75 6d 6e 5f 74 65 78 74 31 36 2c 0a 20 20 73  lumn_text16,.  s
28bb0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79  qlite3_column_ty
28bb1 70 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  pe,.  sqlite3_co
28bb2 6c 75 6d 6e 5f 76 61 6c 75 65 2c 0a 20 20 73 71  lumn_value,.  sq
28bb3 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f  lite3_commit_hoo
28bb4 6b 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6d  k,.  sqlite3_com
28bb5 70 6c 65 74 65 2c 0a 20 20 73 71 6c 69 74 65 33  plete,.  sqlite3
28bb6 5f 63 6f 6d 70 6c 65 74 65 31 36 2c 0a 20 20 73  _complete16,.  s
28bb7 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
28bb8 6c 6c 61 74 69 6f 6e 2c 0a 20 20 73 71 6c 69 74  llation,.  sqlit
28bb9 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
28bba 69 6f 6e 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  ion16,.  sqlite3
28bbb 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
28bbc 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61  ,.  sqlite3_crea
28bbd 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 2c 0a 20  te_function16,. 
28bbe 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
28bbf 6d 6f 64 75 6c 65 2c 0a 20 20 73 71 6c 69 74 65  module,.  sqlite
28bc0 33 5f 64 61 74 61 5f 63 6f 75 6e 74 2c 0a 20 20  3_data_count,.  
28bc1 73 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c  sqlite3_db_handl
28bc2 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 64 65 63  e,.  sqlite3_dec
28bc3 6c 61 72 65 5f 76 74 61 62 2c 0a 20 20 73 71 6c  lare_vtab,.  sql
28bc4 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72  ite3_enable_shar
28bc5 65 64 5f 63 61 63 68 65 2c 0a 20 20 73 71 6c 69  ed_cache,.  sqli
28bc6 74 65 33 5f 65 72 72 63 6f 64 65 2c 0a 20 20 73  te3_errcode,.  s
28bc7 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 2c 0a 20  qlite3_errmsg,. 
28bc8 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31   sqlite3_errmsg1
28bc9 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 65  6,.  sqlite3_exe
28bca 63 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  c,.#ifndef SQLIT
28bcb 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
28bcc 44 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 70 69  D.  sqlite3_expi
28bcd 72 65 64 2c 0a 23 65 6c 73 65 0a 20 20 30 2c 0a  red,.#else.  0,.
28bce 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33  #endif.  sqlite3
28bcf 5f 66 69 6e 61 6c 69 7a 65 2c 0a 20 20 73 71 6c  _finalize,.  sql
28bd0 69 74 65 33 5f 66 72 65 65 2c 0a 20 20 73 71 6c  ite3_free,.  sql
28bd1 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 2c  ite3_free_table,
28bd2 0a 20 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61  .  sqlite3_get_a
28bd3 75 74 6f 63 6f 6d 6d 69 74 2c 0a 20 20 73 71 6c  utocommit,.  sql
28bd4 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61  ite3_get_auxdata
28bd5 2c 0a 20 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  ,.  sqlite3_get_
28bd6 74 61 62 6c 65 2c 0a 20 20 30 2c 20 20 20 20 20  table,.  0,     
28bd7 2f 2a 20 57 61 73 20 73 71 6c 69 74 65 33 5f 67  /* Was sqlite3_g
28bd8 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28 29 2c  lobal_recover(),
28bd9 20 62 75 74 20 74 68 61 74 20 66 75 6e 63 74 69   but that functi
28bda 6f 6e 20 69 73 20 64 65 70 72 65 63 61 74 65 64  on is deprecated
28bdb 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e   */.  sqlite3_in
28bdc 74 65 72 72 75 70 74 2c 0a 20 20 73 71 6c 69 74  terrupt,.  sqlit
28bdd 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
28bde 6f 77 69 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f  owid,.  sqlite3_
28bdf 6c 69 62 76 65 72 73 69 6f 6e 2c 0a 20 20 73 71  libversion,.  sq
28be0 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e  lite3_libversion
28be1 5f 6e 75 6d 62 65 72 2c 0a 20 20 73 71 6c 69 74  _number,.  sqlit
28be2 65 33 5f 6d 61 6c 6c 6f 63 2c 0a 20 20 73 71 6c  e3_malloc,.  sql
28be3 69 74 65 33 5f 6d 70 72 69 6e 74 66 2c 0a 20 20  ite3_mprintf,.  
28be4 73 71 6c 69 74 65 33 5f 6f 70 65 6e 2c 0a 20 20  sqlite3_open,.  
28be5 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 2c 0a  sqlite3_open16,.
28be6 20 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72    sqlite3_prepar
28be7 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 65  e,.  sqlite3_pre
28be8 70 61 72 65 31 36 2c 0a 20 20 73 71 6c 69 74 65  pare16,.  sqlite
28be9 33 5f 70 72 6f 66 69 6c 65 2c 0a 20 20 73 71 6c  3_profile,.  sql
28bea 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61  ite3_progress_ha
28beb 6e 64 6c 65 72 2c 0a 20 20 73 71 6c 69 74 65 33  ndler,.  sqlite3
28bec 5f 72 65 61 6c 6c 6f 63 2c 0a 20 20 73 71 6c 69  _realloc,.  sqli
28bed 74 65 33 5f 72 65 73 65 74 2c 0a 20 20 73 71 6c  te3_reset,.  sql
28bee 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
28bef 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
28bf0 6c 74 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71 6c  lt_double,.  sql
28bf1 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
28bf2 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  r,.  sqlite3_res
28bf3 75 6c 74 5f 65 72 72 6f 72 31 36 2c 0a 20 20 73  ult_error16,.  s
28bf4 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
28bf5 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  t,.  sqlite3_res
28bf6 75 6c 74 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c  ult_int64,.  sql
28bf7 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
28bf8 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
28bf9 6c 74 5f 74 65 78 74 2c 0a 20 20 73 71 6c 69 74  lt_text,.  sqlit
28bfa 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36  e3_result_text16
28bfb 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
28bfc 6c 74 5f 74 65 78 74 31 36 62 65 2c 0a 20 20 73  lt_text16be,.  s
28bfd 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
28bfe 78 74 31 36 6c 65 2c 0a 20 20 73 71 6c 69 74 65  xt16le,.  sqlite
28bff 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 2c 0a  3_result_value,.
28c00 20 20 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61    sqlite3_rollba
28c01 63 6b 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69 74  ck_hook,.  sqlit
28c02 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65  e3_set_authorize
28c03 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 65 74  r,.  sqlite3_set
28c04 5f 61 75 78 64 61 74 61 2c 0a 20 20 73 71 6c 69  _auxdata,.  sqli
28c05 74 65 33 5f 73 6e 70 72 69 6e 74 66 2c 0a 20 20  te3_snprintf,.  
28c06 73 71 6c 69 74 65 33 5f 73 74 65 70 2c 0a 20 20  sqlite3_step,.  
28c07 73 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f  sqlite3_table_co
28c08 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 2c 0a 23  lumn_metadata,.#
28c09 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
28c0a 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 20 20  IT_DEPRECATED.  
28c0b 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 5f 63  sqlite3_thread_c
28c0c 6c 65 61 6e 75 70 2c 0a 23 65 6c 73 65 0a 20 20  leanup,.#else.  
28c0d 30 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69  0,.#endif.  sqli
28c0e 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65  te3_total_change
28c0f 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 74 72 61  s,.  sqlite3_tra
28c10 63 65 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ce,.#ifndef SQLI
28c11 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54  TE_OMIT_DEPRECAT
28c12 45 44 0a 20 20 73 71 6c 69 74 65 33 5f 74 72 61  ED.  sqlite3_tra
28c13 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 2c 0a  nsfer_bindings,.
28c14 23 65 6c 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69  #else.  0,.#endi
28c15 66 0a 20 20 73 71 6c 69 74 65 33 5f 75 70 64 61  f.  sqlite3_upda
28c16 74 65 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c 69 74  te_hook,.  sqlit
28c17 65 33 5f 75 73 65 72 5f 64 61 74 61 2c 0a 20 20  e3_user_data,.  
28c18 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c  sqlite3_value_bl
28c19 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  ob,.  sqlite3_va
28c1a 6c 75 65 5f 62 79 74 65 73 2c 0a 20 20 73 71 6c  lue_bytes,.  sql
28c1b 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
28c1c 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  16,.  sqlite3_va
28c1d 6c 75 65 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71  lue_double,.  sq
28c1e 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 2c  lite3_value_int,
28c1f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
28c20 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c 69 74 65  _int64,.  sqlite
28c21 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f  3_value_numeric_
28c22 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  type,.  sqlite3_
28c23 76 61 6c 75 65 5f 74 65 78 74 2c 0a 20 20 73 71  value_text,.  sq
28c24 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
28c25 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  16,.  sqlite3_va
28c26 6c 75 65 5f 74 65 78 74 31 36 62 65 2c 0a 20 20  lue_text16be,.  
28c27 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
28c28 78 74 31 36 6c 65 2c 0a 20 20 73 71 6c 69 74 65  xt16le,.  sqlite
28c29 33 5f 76 61 6c 75 65 5f 74 79 70 65 2c 0a 20 20  3_value_type,.  
28c2a 73 71 6c 69 74 65 33 5f 76 6d 70 72 69 6e 74 66  sqlite3_vmprintf
28c2b 2c 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20  ,.  /*.  ** The 
28c2c 6f 72 69 67 69 6e 61 6c 20 41 50 49 20 73 65 74  original API set
28c2d 20 65 6e 64 73 20 68 65 72 65 2e 20 20 41 6c 6c   ends here.  All
28c2e 20 65 78 74 65 6e 73 69 6f 6e 73 20 63 61 6e 20   extensions can 
28c2f 63 61 6c 6c 20 61 6e 79 0a 20 20 2a 2a 20 6f 66  call any.  ** of
28c30 20 74 68 65 20 41 50 49 73 20 61 62 6f 76 65 20   the APIs above 
28c31 70 72 6f 76 69 64 65 64 20 74 68 61 74 20 74 68  provided that th
28c32 65 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74  e pointer is not
28c33 20 4e 55 4c 4c 2e 20 20 42 75 74 0a 20 20 2a 2a   NULL.  But.  **
28c34 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20   before calling 
28c35 41 50 49 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77  APIs that follow
28c36 2c 20 65 78 74 65 6e 73 69 6f 6e 20 73 68 6f 75  , extension shou
28c37 6c 64 20 63 68 65 63 6b 20 74 68 65 0a 20 20 2a  ld check the.  *
28c38 2a 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72  * sqlite3_libver
28c39 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 29 20 74 6f  sion_number() to
28c3a 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 79 20   make sure they 
28c3b 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68  are dealing with
28c3c 0a 20 20 2a 2a 20 61 20 6c 69 62 72 61 72 79 20  .  ** a library 
28c3d 74 68 61 74 20 69 73 20 6e 65 77 20 65 6e 6f 75  that is new enou
28c3e 67 68 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68  gh to support th
28c3f 61 74 20 41 50 49 2e 0a 20 20 2a 2a 2a 2a 2a 2a  at API..  ******
28c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28c41 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28c42 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28c43 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28c44 2a 2a 2a 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ***.  */.  sqlit
28c45 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63  e3_overload_func
28c46 74 69 6f 6e 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  tion,..  /*.  **
28c47 20 41 64 64 65 64 20 61 66 74 65 72 20 33 2e 33   Added after 3.3
28c48 2e 31 33 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  .13.  */.  sqlit
28c49 65 33 5f 70 72 65 70 61 72 65 5f 76 32 2c 0a 20  e3_prepare_v2,. 
28c4a 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
28c4b 31 36 5f 76 32 2c 0a 20 20 73 71 6c 69 74 65 33  16_v2,.  sqlite3
28c4c 5f 63 6c 65 61 72 5f 62 69 6e 64 69 6e 67 73 2c  _clear_bindings,
28c4d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65  ..  /*.  ** Adde
28c4e 64 20 66 6f 72 20 33 2e 34 2e 31 0a 20 20 2a 2f  d for 3.4.1.  */
28c4f 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  .  sqlite3_creat
28c50 65 5f 6d 6f 64 75 6c 65 5f 76 32 2c 0a 0a 20 20  e_module_v2,..  
28c51 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64 20 66 6f  /*.  ** Added fo
28c52 72 20 33 2e 35 2e 30 0a 20 20 2a 2f 0a 20 20 73  r 3.5.0.  */.  s
28c53 71 6c 69 74 65 33 5f 62 69 6e 64 5f 7a 65 72 6f  qlite3_bind_zero
28c54 62 6c 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f  blob,.  sqlite3_
28c55 62 6c 6f 62 5f 62 79 74 65 73 2c 0a 20 20 73 71  blob_bytes,.  sq
28c56 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65  lite3_blob_close
28c57 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ,.  sqlite3_blob
28c58 5f 6f 70 65 6e 2c 0a 20 20 73 71 6c 69 74 65 33  _open,.  sqlite3
28c59 5f 62 6c 6f 62 5f 72 65 61 64 2c 0a 20 20 73 71  _blob_read,.  sq
28c5a 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65  lite3_blob_write
28c5b 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61  ,.  sqlite3_crea
28c5c 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 2c  te_collation_v2,
28c5d 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f  .  sqlite3_file_
28c5e 63 6f 6e 74 72 6f 6c 2c 0a 20 20 73 71 6c 69 74  control,.  sqlit
28c5f 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61  e3_memory_highwa
28c60 74 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ter,.  sqlite3_m
28c61 65 6d 6f 72 79 5f 75 73 65 64 2c 0a 23 69 66 64  emory_used,.#ifd
28c62 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ef SQLITE_MUTEX_
28c63 4f 4d 49 54 0a 20 20 30 2c 20 0a 20 20 30 2c 20  OMIT.  0, .  0, 
28c64 0a 20 20 30 2c 0a 20 20 30 2c 0a 20 20 30 2c 0a  .  0,.  0,.  0,.
28c65 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 65 33 5f  #else.  sqlite3_
28c66 6d 75 74 65 78 5f 61 6c 6c 6f 63 2c 0a 20 20 73  mutex_alloc,.  s
28c67 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
28c68 65 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  er,.  sqlite3_mu
28c69 74 65 78 5f 66 72 65 65 2c 0a 20 20 73 71 6c 69  tex_free,.  sqli
28c6a 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 2c  te3_mutex_leave,
28c6b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
28c6c 5f 74 72 79 2c 0a 23 65 6e 64 69 66 0a 20 20 73  _try,.#endif.  s
28c6d 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 2c 0a  qlite3_open_v2,.
28c6e 20 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73    sqlite3_releas
28c6f 65 5f 6d 65 6d 6f 72 79 2c 0a 20 20 73 71 6c 69  e_memory,.  sqli
28c70 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
28c71 5f 6e 6f 6d 65 6d 2c 0a 20 20 73 71 6c 69 74 65  _nomem,.  sqlite
28c72 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74  3_result_error_t
28c73 6f 6f 62 69 67 2c 0a 20 20 73 71 6c 69 74 65 33  oobig,.  sqlite3
28c74 5f 73 6c 65 65 70 2c 0a 20 20 73 71 6c 69 74 65  _sleep,.  sqlite
28c75 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69  3_soft_heap_limi
28c76 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  t,.  sqlite3_vfs
28c77 5f 66 69 6e 64 2c 0a 20 20 73 71 6c 69 74 65 33  _find,.  sqlite3
28c78 5f 76 66 73 5f 72 65 67 69 73 74 65 72 2c 0a 20  _vfs_register,. 
28c79 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72   sqlite3_vfs_unr
28c7a 65 67 69 73 74 65 72 2c 0a 0a 20 20 2f 2a 0a 20  egister,..  /*. 
28c7b 20 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33 2e   ** Added for 3.
28c7c 35 2e 38 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  5.8.  */.  sqlit
28c7d 65 33 5f 74 68 72 65 61 64 73 61 66 65 2c 0a 20  e3_threadsafe,. 
28c7e 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
28c7f 7a 65 72 6f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69  zeroblob,.  sqli
28c80 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
28c81 5f 63 6f 64 65 2c 0a 20 20 73 71 6c 69 74 65 33  _code,.  sqlite3
28c82 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 2c 0a 20  _test_control,. 
28c83 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
28c84 65 73 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  ess,.  sqlite3_c
28c85 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65  ontext_db_handle
28c86 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64  ,..  /*.  ** Add
28c87 65 64 20 66 6f 72 20 33 2e 36 2e 30 0a 20 20 2a  ed for 3.6.0.  *
28c88 2f 0a 20 20 73 71 6c 69 74 65 33 5f 65 78 74 65  /.  sqlite3_exte
28c89 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65  nded_result_code
28c8a 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69 6d  s,.  sqlite3_lim
28c8b 69 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6e 65  it,.  sqlite3_ne
28c8c 78 74 5f 73 74 6d 74 2c 0a 20 20 73 71 6c 69 74  xt_stmt,.  sqlit
28c8d 65 33 5f 73 71 6c 2c 0a 20 20 73 71 6c 69 74 65  e3_sql,.  sqlite
28c8e 33 5f 73 74 61 74 75 73 2c 0a 7d 3b 0a 0a 2f 2a  3_status,.};../*
28c8f 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c  .** Attempt to l
28c90 6f 61 64 20 61 6e 20 53 51 4c 69 74 65 20 65 78  oad an SQLite ex
28c91 74 65 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79 20  tension library 
28c92 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65  contained in the
28c93 20 66 69 6c 65 0a 2a 2a 20 7a 46 69 6c 65 2e 20   file.** zFile. 
28c94 20 54 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74   The entry point
28c95 20 69 73 20 7a 50 72 6f 63 2e 20 20 7a 50 72 6f   is zProc.  zPro
28c96 63 20 6d 61 79 20 62 65 20 30 20 69 6e 20 77 68  c may be 0 in wh
28c97 69 63 68 20 63 61 73 65 20 61 0a 2a 2a 20 64 65  ich case a.** de
28c98 66 61 75 6c 74 20 65 6e 74 72 79 20 70 6f 69 6e  fault entry poin
28c99 74 20 6e 61 6d 65 20 28 73 71 6c 69 74 65 33 5f  t name (sqlite3_
28c9a 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 29 20  extension_init) 
28c9b 69 73 20 75 73 65 64 2e 20 20 55 73 65 0a 2a 2a  is used.  Use.**
28c9c 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74 20   of the default 
28c9d 6e 61 6d 65 20 69 73 20 72 65 63 6f 6d 6d 65 6e  name is recommen
28c9e 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ded..**.** Retur
28c9f 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
28ca0 75 63 63 65 73 73 20 61 6e 64 20 53 51 4c 49 54  uccess and SQLIT
28ca1 45 5f 45 52 52 4f 52 20 69 66 20 73 6f 6d 65 74  E_ERROR if somet
28ca2 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e  hing goes wrong.
28ca3 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72  .**.** If an err
28ca4 6f 72 20 6f 63 63 75 72 73 20 61 6e 64 20 70 7a  or occurs and pz
28ca5 45 72 72 4d 73 67 20 69 73 20 6e 6f 74 20 30 2c  ErrMsg is not 0,
28ca6 20 74 68 65 6e 20 66 69 6c 6c 20 2a 70 7a 45 72   then fill *pzEr
28ca7 72 4d 73 67 20 77 69 74 68 20 0a 2a 2a 20 65 72  rMsg with .** er
28ca8 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74  ror message text
28ca9 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66  .  The calling f
28caa 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 66  unction should f
28cab 72 65 65 20 74 68 69 73 20 6d 65 6d 6f 72 79 0a  ree this memory.
28cac 2a 2a 20 62 79 20 63 61 6c 6c 69 6e 67 20 73 71  ** by calling sq
28cad 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
28cae 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  )..*/.static int
28caf 20 73 71 6c 69 74 65 33 4c 6f 61 64 45 78 74 65   sqlite3LoadExte
28cb0 6e 73 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33  nsion(.  sqlite3
28cb1 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 2f   *db,          /
28cb2 2a 20 4c 6f 61 64 20 74 68 65 20 65 78 74 65 6e  * Load the exten
28cb3 73 69 6f 6e 20 69 6e 74 6f 20 74 68 69 73 20 64  sion into this d
28cb4 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
28cb5 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  on */.  const ch
28cb6 61 72 20 2a 7a 46 69 6c 65 2c 20 20 20 20 2f 2a  ar *zFile,    /*
28cb7 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 73 68 61   Name of the sha
28cb8 72 65 64 20 6c 69 62 72 61 72 79 20 63 6f 6e 74  red library cont
28cb9 61 69 6e 69 6e 67 20 65 78 74 65 6e 73 69 6f 6e  aining extension
28cba 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
28cbb 20 2a 7a 50 72 6f 63 2c 20 20 20 20 2f 2a 20 45   *zProc,    /* E
28cbc 6e 74 72 79 20 70 6f 69 6e 74 2e 20 20 55 73 65  ntry point.  Use
28cbd 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73   "sqlite3_extens
28cbe 69 6f 6e 5f 69 6e 69 74 22 20 69 66 20 30 20 2a  ion_init" if 0 *
28cbf 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  /.  char **pzErr
28cc0 4d 73 67 20 20 20 20 20 20 20 2f 2a 20 50 75 74  Msg       /* Put
28cc1 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68   error message h
28cc2 65 72 65 20 69 66 20 6e 6f 74 20 30 20 2a 2f 0a  ere if not 0 */.
28cc3 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  ){.  sqlite3_vfs
28cc4 20 2a 70 56 66 73 20 3d 20 64 62 2d 3e 70 56 66   *pVfs = db->pVf
28cc5 73 3b 0a 20 20 76 6f 69 64 20 2a 68 61 6e 64 6c  s;.  void *handl
28cc6 65 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e 69 74  e;.  int (*xInit
28cc7 29 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a  )(sqlite3*,char*
28cc8 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  *,const sqlite3_
28cc9 61 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29 3b 0a  api_routines*);.
28cca 20 20 63 68 61 72 20 2a 7a 45 72 72 6d 73 67 20    char *zErrmsg 
28ccb 3d 20 30 3b 0a 20 20 76 6f 69 64 20 2a 2a 61 48  = 0;.  void **aH
28ccc 61 6e 64 6c 65 3b 0a 20 20 63 6f 6e 73 74 20 69  andle;.  const i
28ccd 6e 74 20 6e 4d 73 67 20 3d 20 33 30 30 3b 0a 0a  nt nMsg = 300;..
28cce 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20 29    if( pzErrMsg )
28ccf 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a   *pzErrMsg = 0;.
28cd0 0a 20 20 2f 2a 20 54 69 63 6b 65 74 20 23 31 38  .  /* Ticket #18
28cd1 36 33 2e 20 20 54 6f 20 61 76 6f 69 64 20 61 20  63.  To avoid a 
28cd2 63 72 65 61 74 69 6e 67 20 73 65 63 75 72 69 74  creating securit
28cd3 79 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f  y problems for o
28cd4 6c 64 65 72 0a 20 20 2a 2a 20 61 70 70 6c 69 63  lder.  ** applic
28cd5 61 74 69 6f 6e 73 20 74 68 61 74 20 72 65 6c 69  ations that reli
28cd6 6e 6b 20 61 67 61 69 6e 73 74 20 6e 65 77 65 72  nk against newer
28cd7 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
28cd8 69 74 65 2c 20 74 68 65 0a 20 20 2a 2a 20 61 62  ite, the.  ** ab
28cd9 69 6c 69 74 79 20 74 6f 20 72 75 6e 20 6c 6f 61  ility to run loa
28cda 64 5f 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 74  d_extension is t
28cdb 75 72 6e 65 64 20 6f 66 66 20 62 79 20 64 65 66  urned off by def
28cdc 61 75 6c 74 2e 20 20 4f 6e 65 0a 20 20 2a 2a 20  ault.  One.  ** 
28cdd 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c 69 74 65  must call sqlite
28cde 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78  3_enable_load_ex
28cdf 74 65 6e 73 69 6f 6e 28 29 20 74 6f 20 74 75 72  tension() to tur
28ce0 6e 20 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 20  n on extension. 
28ce1 20 2a 2a 20 6c 6f 61 64 69 6e 67 2e 20 20 4f 74   ** loading.  Ot
28ce2 68 65 72 77 69 73 65 20 79 6f 75 20 67 65 74 20  herwise you get 
28ce3 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 72  the following er
28ce4 72 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ror..  */.  if( 
28ce5 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  (db->flags & SQL
28ce6 49 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f  ITE_LoadExtensio
28ce7 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  n)==0 ){.    if(
28ce8 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20   pzErrMsg ){.   
28ce9 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73     *pzErrMsg = s
28cea 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
28ceb 6e 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 22 29  not authorized")
28cec 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
28ced 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
28cee 0a 20 20 7d 0a 0a 20 20 69 66 28 20 7a 50 72 6f  .  }..  if( zPro
28cef 63 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 50 72 6f  c==0 ){.    zPro
28cf0 63 20 3d 20 22 73 71 6c 69 74 65 33 5f 65 78 74  c = "sqlite3_ext
28cf1 65 6e 73 69 6f 6e 5f 69 6e 69 74 22 3b 0a 20 20  ension_init";.  
28cf2 7d 0a 0a 20 20 68 61 6e 64 6c 65 20 3d 20 73 71  }..  handle = sq
28cf3 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 70 56  lite3OsDlOpen(pV
28cf4 66 73 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 69 66  fs, zFile);.  if
28cf5 28 20 68 61 6e 64 6c 65 3d 3d 30 20 29 7b 0a 20  ( handle==0 ){. 
28cf6 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20     if( pzErrMsg 
28cf7 29 7b 0a 20 20 20 20 20 20 7a 45 72 72 6d 73 67  ){.      zErrmsg
28cf8 20 3d 20 73 71 6c 69 74 65 33 53 74 61 63 6b 41   = sqlite3StackA
28cf9 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 6e 4d 73  llocZero(db, nMs
28cfa 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 45  g);.      if( zE
28cfb 72 72 6d 73 67 20 29 7b 0a 20 20 20 20 20 20 20  rrmsg ){.       
28cfc 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
28cfd 66 28 6e 4d 73 67 2c 20 7a 45 72 72 6d 73 67 2c  f(nMsg, zErrmsg,
28cfe 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 75   .            "u
28cff 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 73 68  nable to open sh
28d00 61 72 65 64 20 6c 69 62 72 61 72 79 20 5b 25 73  ared library [%s
28d01 5d 22 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 20 20  ]", zFile);.    
28d02 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 45      sqlite3OsDlE
28d03 72 72 6f 72 28 70 56 66 73 2c 20 6e 4d 73 67 2d  rror(pVfs, nMsg-
28d04 31 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20  1, zErrmsg);.   
28d05 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d       *pzErrMsg =
28d06 20 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70   sqlite3DbStrDup
28d07 28 30 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20  (0, zErrmsg);.  
28d08 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61        sqlite3Sta
28d09 63 6b 46 72 65 65 28 64 62 2c 20 7a 45 72 72 6d  ckFree(db, zErrm
28d0a 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  sg);.      }.   
28d0b 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51   }.    return SQ
28d0c 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
28d0d 20 20 78 49 6e 69 74 20 3d 20 28 69 6e 74 28 2a    xInit = (int(*
28d0e 29 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a  )(sqlite3*,char*
28d0f 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f  *,const sqlite3_
28d10 61 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29 29 0a  api_routines*)).
28d11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28d12 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 53 79     sqlite3OsDlSy
28d13 6d 28 70 56 66 73 2c 20 68 61 6e 64 6c 65 2c 20  m(pVfs, handle, 
28d14 7a 50 72 6f 63 29 3b 0a 20 20 69 66 28 20 78 49  zProc);.  if( xI
28d15 6e 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  nit==0 ){.    if
28d16 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20  ( pzErrMsg ){.  
28d17 20 20 20 20 7a 45 72 72 6d 73 67 20 3d 20 73 71      zErrmsg = sq
28d18 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a  lite3StackAllocZ
28d19 65 72 6f 28 64 62 2c 20 6e 4d 73 67 29 3b 0a 20  ero(db, nMsg);. 
28d1a 20 20 20 20 20 69 66 28 20 7a 45 72 72 6d 73 67       if( zErrmsg
28d1b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
28d1c 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 4d 73  te3_snprintf(nMs
28d1d 67 2c 20 7a 45 72 72 6d 73 67 2c 0a 20 20 20 20  g, zErrmsg,.    
28d1e 20 20 20 20 20 20 20 20 22 6e 6f 20 65 6e 74 72          "no entr
28d1f 79 20 70 6f 69 6e 74 20 5b 25 73 5d 20 69 6e 20  y point [%s] in 
28d20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20 5b  shared library [
28d21 25 73 5d 22 2c 20 7a 50 72 6f 63 2c 7a 46 69 6c  %s]", zProc,zFil
28d22 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  e);.        sqli
28d23 74 65 33 4f 73 44 6c 45 72 72 6f 72 28 70 56 66  te3OsDlError(pVf
28d24 73 2c 20 6e 4d 73 67 2d 31 2c 20 7a 45 72 72 6d  s, nMsg-1, zErrm
28d25 73 67 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 7a  sg);.        *pz
28d26 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33  ErrMsg = sqlite3
28d27 44 62 53 74 72 44 75 70 28 30 2c 20 7a 45 72 72  DbStrDup(0, zErr
28d28 6d 73 67 29 3b 0a 20 20 20 20 20 20 20 20 73 71  msg);.        sq
28d29 6c 69 74 65 33 53 74 61 63 6b 46 72 65 65 28 64  lite3StackFree(d
28d2a 62 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20  b, zErrmsg);.   
28d2b 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
28d2c 65 33 4f 73 44 6c 43 6c 6f 73 65 28 70 56 66 73  e3OsDlClose(pVfs
28d2d 2c 20 68 61 6e 64 6c 65 29 3b 0a 20 20 20 20 7d  , handle);.    }
28d2e 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
28d2f 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73  TE_ERROR;.  }els
28d30 65 20 69 66 28 20 78 49 6e 69 74 28 64 62 2c 20  e if( xInit(db, 
28d31 26 7a 45 72 72 6d 73 67 2c 20 26 73 71 6c 69 74  &zErrmsg, &sqlit
28d32 65 33 41 70 69 73 29 20 29 7b 0a 20 20 20 20 69  e3Apis) ){.    i
28d33 66 28 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20  f( pzErrMsg ){. 
28d34 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d       *pzErrMsg =
28d35 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
28d36 28 22 65 72 72 6f 72 20 64 75 72 69 6e 67 20 69  ("error during i
28d37 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 3a 20 25  nitialization: %
28d38 73 22 2c 20 7a 45 72 72 6d 73 67 29 3b 0a 20 20  s", zErrmsg);.  
28d39 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
28d3a 66 72 65 65 28 7a 45 72 72 6d 73 67 29 3b 0a 20  free(zErrmsg);. 
28d3b 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 43 6c     sqlite3OsDlCl
28d3c 6f 73 65 28 70 56 66 73 2c 20 68 61 6e 64 6c 65  ose(pVfs, handle
28d3d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
28d3e 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
28d3f 0a 20 20 2f 2a 20 41 70 70 65 6e 64 20 74 68 65  .  /* Append the
28d40 20 6e 65 77 20 73 68 61 72 65 64 20 6c 69 62 72   new shared libr
28d41 61 72 79 20 68 61 6e 64 6c 65 20 74 6f 20 74 68  ary handle to th
28d42 65 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e  e db->aExtension
28d43 20 61 72 72 61 79 2e 20 2a 2f 0a 20 20 61 48 61   array. */.  aHa
28d44 6e 64 6c 65 20 3d 20 73 71 6c 69 74 65 33 44 62  ndle = sqlite3Db
28d45 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
28d46 69 7a 65 6f 66 28 68 61 6e 64 6c 65 29 2a 28 64  izeof(handle)*(d
28d47 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 2b 31 29  b->nExtension+1)
28d48 29 3b 0a 20 20 69 66 28 20 61 48 61 6e 64 6c 65  );.  if( aHandle
28d49 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
28d4a 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
28d4b 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6e 45    }.  if( db->nE
28d4c 78 74 65 6e 73 69 6f 6e 3e 30 20 29 7b 0a 20 20  xtension>0 ){.  
28d4d 20 20 6d 65 6d 63 70 79 28 61 48 61 6e 64 6c 65    memcpy(aHandle
28d4e 2c 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e  , db->aExtension
28d4f 2c 20 73 69 7a 65 6f 66 28 68 61 6e 64 6c 65 29  , sizeof(handle)
28d50 2a 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e 29  *db->nExtension)
28d51 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44  ;.  }.  sqlite3D
28d52 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e 61 45  bFree(db, db->aE
28d53 78 74 65 6e 73 69 6f 6e 29 3b 0a 20 20 64 62 2d  xtension);.  db-
28d54 3e 61 45 78 74 65 6e 73 69 6f 6e 20 3d 20 61 48  >aExtension = aH
28d55 61 6e 64 6c 65 3b 0a 0a 20 20 64 62 2d 3e 61 45  andle;..  db->aE
28d56 78 74 65 6e 73 69 6f 6e 5b 64 62 2d 3e 6e 45 78  xtension[db->nEx
28d57 74 65 6e 73 69 6f 6e 2b 2b 5d 20 3d 20 68 61 6e  tension++] = han
28d58 64 6c 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  dle;.  return SQ
28d59 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 53 51 4c 49 54  LITE_OK;.}.SQLIT
28d5a 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
28d5b 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e  3_load_extension
28d5c 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
28d5d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 61            /* Loa
28d5e 64 20 74 68 65 20 65 78 74 65 6e 73 69 6f 6e 20  d the extension 
28d5f 69 6e 74 6f 20 74 68 69 73 20 64 61 74 61 62 61  into this databa
28d60 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
28d61 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
28d62 46 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65  File,    /* Name
28d63 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 6c   of the shared l
28d64 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e  ibrary containin
28d65 67 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20  g extension */. 
28d66 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 72   const char *zPr
28d67 6f 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72 79 20  oc,    /* Entry 
28d68 70 6f 69 6e 74 2e 20 20 55 73 65 20 22 73 71 6c  point.  Use "sql
28d69 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69  ite3_extension_i
28d6a 6e 69 74 22 20 69 66 20 30 20 2a 2f 0a 20 20 63  nit" if 0 */.  c
28d6b 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20  har **pzErrMsg  
28d6c 20 20 20 20 20 2f 2a 20 50 75 74 20 65 72 72 6f       /* Put erro
28d6d 72 20 6d 65 73 73 61 67 65 20 68 65 72 65 20 69  r message here i
28d6e 66 20 6e 6f 74 20 30 20 2a 2f 0a 29 7b 0a 20 20  f not 0 */.){.  
28d6f 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
28d70 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
28d71 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 63 20 3d  ->mutex);.  rc =
28d72 20 73 71 6c 69 74 65 33 4c 6f 61 64 45 78 74 65   sqlite3LoadExte
28d73 6e 73 69 6f 6e 28 64 62 2c 20 7a 46 69 6c 65 2c  nsion(db, zFile,
28d74 20 7a 50 72 6f 63 2c 20 70 7a 45 72 72 4d 73 67   zProc, pzErrMsg
28d75 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
28d76 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29  3ApiExit(db, rc)
28d77 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
28d78 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
28d79 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
28d7a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 74  .}../*.** Call t
28d7b 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e  his routine when
28d7c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
28d7d 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73  nnection is clos
28d7e 69 6e 67 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20  ing in order.** 
28d7f 74 6f 20 63 6c 65 61 6e 20 75 70 20 6c 6f 61 64  to clean up load
28d80 65 64 20 65 78 74 65 6e 73 69 6f 6e 73 0a 2a 2f  ed extensions.*/
28d81 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
28d82 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73  void sqlite3Clos
28d83 65 45 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69  eExtensions(sqli
28d84 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20  te3 *db){.  int 
28d85 69 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  i;.  assert( sql
28d86 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
28d87 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
28d88 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
28d89 45 78 74 65 6e 73 69 6f 6e 3b 20 69 2b 2b 29 7b  Extension; i++){
28d8a 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c  .    sqlite3OsDl
28d8b 43 6c 6f 73 65 28 64 62 2d 3e 70 56 66 73 2c 20  Close(db->pVfs, 
28d8c 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f 6e 5b 69  db->aExtension[i
28d8d 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ]);.  }.  sqlite
28d8e 33 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d 3e  3DbFree(db, db->
28d8f 61 45 78 74 65 6e 73 69 6f 6e 29 3b 0a 7d 0a 0a  aExtension);.}..
28d90 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f 72 20  /*.** Enable or 
28d91 64 69 73 61 62 6c 65 20 65 78 74 65 6e 73 69 6f  disable extensio
28d92 6e 20 6c 6f 61 64 69 6e 67 2e 20 20 45 78 74 65  n loading.  Exte
28d93 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73  nsion loading is
28d94 20 64 69 73 61 62 6c 65 64 20 62 79 0a 2a 2a 20   disabled by.** 
28d95 64 65 66 61 75 6c 74 20 73 6f 20 61 73 20 6e 6f  default so as no
28d96 74 20 74 6f 20 6f 70 65 6e 20 73 65 63 75 72 69  t to open securi
28d97 74 79 20 68 6f 6c 65 73 20 69 6e 20 6f 6c 64 65  ty holes in olde
28d98 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 2e 0a  r applications..
28d99 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
28d9a 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65  t sqlite3_enable
28d9b 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28  _load_extension(
28d9c 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
28d9d 20 6f 6e 6f 66 66 29 7b 0a 20 20 73 71 6c 69 74   onoff){.  sqlit
28d9e 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
28d9f 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28  b->mutex);.  if(
28da0 20 6f 6e 6f 66 66 20 29 7b 0a 20 20 20 20 64 62   onoff ){.    db
28da1 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54  ->flags |= SQLIT
28da2 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 3b  E_LoadExtension;
28da3 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62  .  }else{.    db
28da4 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49  ->flags &= ~SQLI
28da5 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e  TE_LoadExtension
28da6 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
28da7 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
28da8 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
28da9 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23   SQLITE_OK;.}..#
28daa 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
28dab 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53  OMIT_LOAD_EXTENS
28dac 49 4f 4e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  ION */../*.** Th
28dad 65 20 61 75 74 6f 2d 65 78 74 65 6e 73 69 6f 6e  e auto-extension
28dae 20 63 6f 64 65 20 61 64 64 65 64 20 72 65 67 61   code added rega
28daf 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65  rdless of whethe
28db0 72 20 6f 72 20 6e 6f 74 20 65 78 74 65 6e 73 69  r or not extensi
28db1 6f 6e 0a 2a 2a 20 6c 6f 61 64 69 6e 67 20 69 73  on.** loading is
28db2 20 73 75 70 70 6f 72 74 65 64 2e 20 20 57 65 20   supported.  We 
28db3 6e 65 65 64 20 61 20 64 75 6d 6d 79 20 73 71 6c  need a dummy sql
28db4 69 74 65 33 41 70 69 73 20 70 6f 69 6e 74 65 72  ite3Apis pointer
28db5 20 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64   for that.** cod
28db6 65 20 69 66 20 72 65 67 75 6c 61 72 20 65 78 74  e if regular ext
28db7 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 69  ension loading i
28db8 73 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2e  s not available.
28db9 20 20 54 68 69 73 20 69 73 20 74 68 61 74 0a 2a    This is that.*
28dba 2a 20 64 75 6d 6d 79 20 70 6f 69 6e 74 65 72 2e  * dummy pointer.
28dbb 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
28dbc 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45  E_OMIT_LOAD_EXTE
28dbd 4e 53 49 4f 4e 0a 73 74 61 74 69 63 20 63 6f 6e  NSION.static con
28dbe 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72  st sqlite3_api_r
28dbf 6f 75 74 69 6e 65 73 20 73 71 6c 69 74 65 33 41  outines sqlite3A
28dc0 70 69 73 20 3d 20 7b 20 30 20 7d 3b 0a 23 65 6e  pis = { 0 };.#en
28dc1 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  dif.../*.** The 
28dc2 66 6f 6c 6c 6f 77 69 6e 67 20 6f 62 6a 65 63 74  following object
28dc3 20 68 6f 6c 64 73 20 74 68 65 20 6c 69 73 74 20   holds the list 
28dc4 6f 66 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  of automatically
28dc5 20 6c 6f 61 64 65 64 0a 2a 2a 20 65 78 74 65 6e   loaded.** exten
28dc6 73 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  sions..**.** Thi
28dc7 73 20 6c 69 73 74 20 69 73 20 73 68 61 72 65 64  s list is shared
28dc8 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 2e   across threads.
28dc9 20 20 54 68 65 20 53 51 4c 49 54 45 5f 4d 55 54    The SQLITE_MUT
28dca 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
28dcb 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62  .** mutex must b
28dcc 65 20 68 65 6c 64 20 77 68 69 6c 65 20 61 63 63  e held while acc
28dcd 65 73 73 69 6e 67 20 74 68 69 73 20 6c 69 73 74  essing this list
28dce 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
28dcf 75 63 74 20 73 71 6c 69 74 65 33 41 75 74 6f 45  uct sqlite3AutoE
28dd0 78 74 4c 69 73 74 20 73 71 6c 69 74 65 33 41 75  xtList sqlite3Au
28dd1 74 6f 45 78 74 4c 69 73 74 3b 0a 73 74 61 74 69  toExtList;.stati
28dd2 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72  c SQLITE_WSD str
28dd3 75 63 74 20 73 71 6c 69 74 65 33 41 75 74 6f 45  uct sqlite3AutoE
28dd4 78 74 4c 69 73 74 20 7b 0a 20 20 69 6e 74 20 6e  xtList {.  int n
28dd5 45 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  Ext;            
28dd6 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
28dd7 6e 74 72 69 65 73 20 69 6e 20 61 45 78 74 5b 5d  ntries in aExt[]
28dd8 20 2a 2f 20 20 20 20 20 20 20 20 20 20 0a 20 20   */          .  
28dd9 76 6f 69 64 20 28 2a 2a 61 45 78 74 29 28 76 6f  void (**aExt)(vo
28dda 69 64 29 3b 20 20 20 2f 2a 20 50 6f 69 6e 74 65  id);   /* Pointe
28ddb 72 73 20 74 6f 20 74 68 65 20 65 78 74 65 6e 73  rs to the extens
28ddc 69 6f 6e 20 69 6e 69 74 20 66 75 6e 63 74 69 6f  ion init functio
28ddd 6e 73 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 41  ns */.} sqlite3A
28dde 75 74 6f 65 78 74 20 3d 20 7b 20 30 2c 20 30 20  utoext = { 0, 0 
28ddf 7d 3b 0a 0a 2f 2a 20 54 68 65 20 22 77 73 64 41  };../* The "wsdA
28de0 75 74 6f 65 78 74 22 20 6d 61 63 72 6f 20 77 69  utoext" macro wi
28de1 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68  ll resolve to th
28de2 65 20 61 75 74 6f 65 78 74 65 6e 73 69 6f 6e 0a  e autoextension.
28de3 2a 2a 20 73 74 61 74 65 20 76 65 63 74 6f 72 2e  ** state vector.
28de4 20 20 49 66 20 77 72 69 74 61 62 6c 65 20 73 74    If writable st
28de5 61 74 69 63 20 64 61 74 61 20 69 73 20 75 6e 73  atic data is uns
28de6 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68 65 20  upported on the 
28de7 74 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20 68 61  target,.** we ha
28de8 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65  ve to locate the
28de9 20 73 74 61 74 65 20 76 65 63 74 6f 72 20 61 74   state vector at
28dea 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e 20 74   run-time.  In t
28deb 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 0a 2a  he more common.*
28dec 2a 20 63 61 73 65 20 77 68 65 72 65 20 77 72 69  * case where wri
28ded 74 61 62 6c 65 20 73 74 61 74 69 63 20 64 61 74  table static dat
28dee 61 20 69 73 20 73 75 70 70 6f 72 74 65 64 2c 20  a is supported, 
28def 77 73 64 53 74 61 74 20 63 61 6e 20 72 65 66 65  wsdStat can refe
28df0 72 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20 74 6f  r directly.** to
28df1 20 74 68 65 20 22 73 71 6c 69 74 65 33 41 75 74   the "sqlite3Aut
28df2 6f 65 78 74 22 20 73 74 61 74 65 20 76 65 63 74  oext" state vect
28df3 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f 76  or declared abov
28df4 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e..*/.#ifdef SQL
28df5 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20 64  ITE_OMIT_WSD.# d
28df6 65 66 69 6e 65 20 77 73 64 41 75 74 6f 65 78 74  efine wsdAutoext
28df7 49 6e 69 74 20 5c 0a 20 20 73 71 6c 69 74 65 33  Init \.  sqlite3
28df8 41 75 74 6f 45 78 74 4c 69 73 74 20 2a 78 20 3d  AutoExtList *x =
28df9 20 26 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33   &GLOBAL(sqlite3
28dfa 41 75 74 6f 45 78 74 4c 69 73 74 2c 73 71 6c 69  AutoExtList,sqli
28dfb 74 65 33 41 75 74 6f 65 78 74 29 0a 23 20 64 65  te3Autoext).# de
28dfc 66 69 6e 65 20 77 73 64 41 75 74 6f 65 78 74 20  fine wsdAutoext 
28dfd 78 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66  x[0].#else.# def
28dfe 69 6e 65 20 77 73 64 41 75 74 6f 65 78 74 49 6e  ine wsdAutoextIn
28dff 69 74 0a 23 20 64 65 66 69 6e 65 20 77 73 64 41  it.# define wsdA
28e00 75 74 6f 65 78 74 20 73 71 6c 69 74 65 33 41 75  utoext sqlite3Au
28e01 74 6f 65 78 74 0a 23 65 6e 64 69 66 0a 0a 0a 2f  toext.#endif.../
28e02 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20  *.** Register a 
28e03 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 65  statically linke
28e04 64 20 65 78 74 65 6e 73 69 6f 6e 20 74 68 61 74  d extension that
28e05 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   is automaticall
28e06 79 0a 2a 2a 20 6c 6f 61 64 65 64 20 62 79 20 65  y.** loaded by e
28e07 76 65 72 79 20 6e 65 77 20 64 61 74 61 62 61 73  very new databas
28e08 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f  e connection..*/
28e09 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
28e0a 73 71 6c 69 74 65 33 5f 61 75 74 6f 5f 65 78 74  sqlite3_auto_ext
28e0b 65 6e 73 69 6f 6e 28 76 6f 69 64 20 28 2a 78 49  ension(void (*xI
28e0c 6e 69 74 29 28 76 6f 69 64 29 29 7b 0a 20 20 69  nit)(void)){.  i
28e0d 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
28e0e 4b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  K;.#ifndef SQLIT
28e0f 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a  E_OMIT_AUTOINIT.
28e10 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69    rc = sqlite3_i
28e11 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69  nitialize();.  i
28e12 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74  f( rc ){.    ret
28e13 75 72 6e 20 72 63 3b 0a 20 20 7d 65 6c 73 65 0a  urn rc;.  }else.
28e14 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20 69  #endif.  {.    i
28e15 6e 74 20 69 3b 0a 23 69 66 20 53 51 4c 49 54 45  nt i;.#if SQLITE
28e16 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 20 20  _THREADSAFE.    
28e17 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d  sqlite3_mutex *m
28e18 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75  utex = sqlite3Mu
28e19 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
28e1a 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
28e1b 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20  TER);.#endif.   
28e1c 20 77 73 64 41 75 74 6f 65 78 74 49 6e 69 74 3b   wsdAutoextInit;
28e1d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
28e1e 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b  ex_enter(mutex);
28e1f 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
28e20 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 3b  wsdAutoext.nExt;
28e21 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
28e22 20 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74   wsdAutoext.aExt
28e23 5b 69 5d 3d 3d 78 49 6e 69 74 20 29 20 62 72 65  [i]==xInit ) bre
28e24 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ak;.    }.    if
28e25 28 20 69 3d 3d 77 73 64 41 75 74 6f 65 78 74 2e  ( i==wsdAutoext.
28e26 6e 45 78 74 20 29 7b 0a 20 20 20 20 20 20 69 6e  nExt ){.      in
28e27 74 20 6e 42 79 74 65 20 3d 20 28 77 73 64 41 75  t nByte = (wsdAu
28e28 74 6f 65 78 74 2e 6e 45 78 74 2b 31 29 2a 73 69  toext.nExt+1)*si
28e29 7a 65 6f 66 28 77 73 64 41 75 74 6f 65 78 74 2e  zeof(wsdAutoext.
28e2a 61 45 78 74 5b 30 5d 29 3b 0a 20 20 20 20 20 20  aExt[0]);.      
28e2b 76 6f 69 64 20 28 2a 2a 61 4e 65 77 29 28 76 6f  void (**aNew)(vo
28e2c 69 64 29 3b 0a 20 20 20 20 20 20 61 4e 65 77 20  id);.      aNew 
28e2d 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  = sqlite3_reallo
28e2e 63 28 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78  c(wsdAutoext.aEx
28e2f 74 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20  t, nByte);.     
28e30 20 69 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a   if( aNew==0 ){.
28e31 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
28e32 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
28e33 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
28e34 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74 20  wsdAutoext.aExt 
28e35 3d 20 61 4e 65 77 3b 0a 20 20 20 20 20 20 20 20  = aNew;.        
28e36 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74 5b  wsdAutoext.aExt[
28e37 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 5d  wsdAutoext.nExt]
28e38 20 3d 20 78 49 6e 69 74 3b 0a 20 20 20 20 20 20   = xInit;.      
28e39 20 20 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78    wsdAutoext.nEx
28e3a 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  t++;.      }.   
28e3b 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d   }.    sqlite3_m
28e3c 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78  utex_leave(mutex
28e3d 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28  );.    assert( (
28e3e 72 63 26 30 78 66 66 29 3d 3d 72 63 20 29 3b 0a  rc&0xff)==rc );.
28e3f 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
28e40 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 65   }.}../*.** Rese
28e41 74 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20  t the automatic 
28e42 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e  extension loadin
28e43 67 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a  g mechanism..*/.
28e44 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
28e45 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f 61 75  sqlite3_reset_au
28e46 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 76 6f 69  to_extension(voi
28e47 64 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  d){.#ifndef SQLI
28e48 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
28e49 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69  .  if( sqlite3_i
28e4a 6e 69 74 69 61 6c 69 7a 65 28 29 3d 3d 53 51 4c  nitialize()==SQL
28e4b 49 54 45 5f 4f 4b 20 29 0a 23 65 6e 64 69 66 0a  ITE_OK ).#endif.
28e4c 20 20 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54    {.#if SQLITE_T
28e4d 48 52 45 41 44 53 41 46 45 0a 20 20 20 20 73 71  HREADSAFE.    sq
28e4e 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74  lite3_mutex *mut
28e4f 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  ex = sqlite3Mute
28e50 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
28e51 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
28e52 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 77  R);.#endif.    w
28e53 73 64 41 75 74 6f 65 78 74 49 6e 69 74 3b 0a 20  sdAutoextInit;. 
28e54 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
28e55 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20  _enter(mutex);. 
28e56 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
28e57 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74 29  wsdAutoext.aExt)
28e58 3b 0a 20 20 20 20 77 73 64 41 75 74 6f 65 78 74  ;.    wsdAutoext
28e59 2e 61 45 78 74 20 3d 20 30 3b 0a 20 20 20 20 77  .aExt = 0;.    w
28e5a 73 64 41 75 74 6f 65 78 74 2e 6e 45 78 74 20 3d  sdAutoext.nExt =
28e5b 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   0;.    sqlite3_
28e5c 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65  mutex_leave(mute
28e5d 78 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  x);.  }.}../*.**
28e5e 20 4c 6f 61 64 20 61 6c 6c 20 61 75 74 6f 6d 61   Load all automa
28e5f 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a  tic extensions..
28e60 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 74 68 69 6e  **.** If anythin
28e61 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 73 65  g goes wrong, se
28e62 74 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 74 68  t an error in th
28e63 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
28e64 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
28e65 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
28e66 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64 45 78 74  lite3AutoLoadExt
28e67 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 20  ensions(sqlite3 
28e68 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  *db){.  int i;. 
28e69 20 69 6e 74 20 67 6f 20 3d 20 31 3b 0a 20 20 69   int go = 1;.  i
28e6a 6e 74 20 28 2a 78 49 6e 69 74 29 28 73 71 6c 69  nt (*xInit)(sqli
28e6b 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73  te3*,char**,cons
28e6c 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f  t sqlite3_api_ro
28e6d 75 74 69 6e 65 73 2a 29 3b 0a 0a 20 20 77 73 64  utines*);..  wsd
28e6e 41 75 74 6f 65 78 74 49 6e 69 74 3b 0a 20 20 69  AutoextInit;.  i
28e6f 66 28 20 77 73 64 41 75 74 6f 65 78 74 2e 6e 45  f( wsdAutoext.nE
28e70 78 74 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  xt==0 ){.    /* 
28e71 43 6f 6d 6d 6f 6e 20 63 61 73 65 3a 20 65 61 72  Common case: ear
28e72 6c 79 20 6f 75 74 20 77 69 74 68 6f 75 74 20 65  ly out without e
28e73 76 65 72 79 20 68 61 76 69 6e 67 20 74 6f 20 61  very having to a
28e74 63 71 75 69 72 65 20 61 20 6d 75 74 65 78 20 2a  cquire a mutex *
28e75 2f 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  /.    return;.  
28e76 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 67 6f 3b  }.  for(i=0; go;
28e77 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72 20   i++){.    char 
28e78 2a 7a 45 72 72 6d 73 67 3b 0a 23 69 66 20 53 51  *zErrmsg;.#if SQ
28e79 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a  LITE_THREADSAFE.
28e7a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
28e7b 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  x *mutex = sqlit
28e7c 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
28e7d 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
28e7e 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66  _MASTER);.#endif
28e7f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
28e80 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b  ex_enter(mutex);
28e81 0a 20 20 20 20 69 66 28 20 69 3e 3d 77 73 64 41  .    if( i>=wsdA
28e82 75 74 6f 65 78 74 2e 6e 45 78 74 20 29 7b 0a 20  utoext.nExt ){. 
28e83 20 20 20 20 20 78 49 6e 69 74 20 3d 20 30 3b 0a       xInit = 0;.
28e84 20 20 20 20 20 20 67 6f 20 3d 20 30 3b 0a 20 20        go = 0;.  
28e85 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 78    }else{.      x
28e86 49 6e 69 74 20 3d 20 28 69 6e 74 28 2a 29 28 73  Init = (int(*)(s
28e87 71 6c 69 74 65 33 2a 2c 63 68 61 72 2a 2a 2c 63  qlite3*,char**,c
28e88 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70 69  onst sqlite3_api
28e89 5f 72 6f 75 74 69 6e 65 73 2a 29 29 0a 20 20 20  _routines*)).   
28e8a 20 20 20 20 20 20 20 20 20 20 20 77 73 64 41 75             wsdAu
28e8b 74 6f 65 78 74 2e 61 45 78 74 5b 69 5d 3b 0a 20  toext.aExt[i];. 
28e8c 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
28e8d 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74  _mutex_leave(mut
28e8e 65 78 29 3b 0a 20 20 20 20 7a 45 72 72 6d 73 67  ex);.    zErrmsg
28e8f 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 78 49   = 0;.    if( xI
28e90 6e 69 74 20 26 26 20 78 49 6e 69 74 28 64 62 2c  nit && xInit(db,
28e91 20 26 7a 45 72 72 6d 73 67 2c 20 26 73 71 6c 69   &zErrmsg, &sqli
28e92 74 65 33 41 70 69 73 29 20 29 7b 0a 20 20 20 20  te3Apis) ){.    
28e93 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
28e94 62 2c 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 2c  b, SQLITE_ERROR,
28e95 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 61 75  .            "au
28e96 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f  tomatic extensio
28e97 6e 20 6c 6f 61 64 69 6e 67 20 66 61 69 6c 65 64  n loading failed
28e98 3a 20 25 73 22 2c 20 7a 45 72 72 6d 73 67 29 3b  : %s", zErrmsg);
28e99 0a 20 20 20 20 20 20 67 6f 20 3d 20 30 3b 0a 20  .      go = 0;. 
28e9a 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
28e9b 5f 66 72 65 65 28 7a 45 72 72 6d 73 67 29 3b 0a  _free(zErrmsg);.
28e9c 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a    }.}../********
28e9d 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6c 6f  ****** End of lo
28e9e 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  adext.c ********
28e9f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ea1 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
28ea2 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
28ea3 65 20 70 72 61 67 6d 61 2e 63 20 2a 2a 2a 2a 2a  e pragma.c *****
28ea4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ea5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ea6 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
28ea7 33 20 41 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20  3 April 6.**.** 
28ea8 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
28ea9 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
28eaa 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
28eab 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
28eac 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
28ead 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
28eae 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
28eaf 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
28eb0 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
28eb1 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
28eb2 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
28eb3 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
28eb4 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
28eb5 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
28eb6 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
28eb7 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
28eb8 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
28eb9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28eba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ebb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ebc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28ebd 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
28ebe 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
28ebf 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c  ode used to impl
28ec0 65 6d 65 6e 74 20 74 68 65 20 50 52 41 47 4d 41  ement the PRAGMA
28ec1 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20   command..**.** 
28ec2 24 49 64 3a 20 70 72 61 67 6d 61 2e 63 2c 76 20  $Id: pragma.c,v 
28ec3 31 2e 32 31 34 20 32 30 30 39 2f 30 37 2f 30 32  1.214 2009/07/02
28ec4 20 30 37 3a 34 37 3a 33 33 20 64 61 6e 69 65 6c   07:47:33 daniel
28ec5 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
28ec6 2f 2a 20 49 67 6e 6f 72 65 20 74 68 69 73 20 77  /* Ignore this w
28ec7 68 6f 6c 65 20 66 69 6c 65 20 69 66 20 70 72 61  hole file if pra
28ec8 67 6d 61 73 20 61 72 65 20 64 69 73 61 62 6c 65  gmas are disable
28ec9 64 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65  d.*/.#if !define
28eca 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52  d(SQLITE_OMIT_PR
28ecb 41 47 4d 41 29 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74  AGMA)../*.** Int
28ecc 65 72 70 72 65 74 20 74 68 65 20 67 69 76 65 6e  erpret the given
28ecd 20 73 74 72 69 6e 67 20 61 73 20 61 20 73 61 66   string as a saf
28ece 65 74 79 20 6c 65 76 65 6c 2e 20 20 52 65 74 75  ety level.  Retu
28ecf 72 6e 20 30 20 66 6f 72 20 4f 46 46 2c 0a 2a 2a  rn 0 for OFF,.**
28ed0 20 31 20 66 6f 72 20 4f 4e 20 6f 72 20 4e 4f 52   1 for ON or NOR
28ed1 4d 41 4c 20 61 6e 64 20 32 20 66 6f 72 20 46 55  MAL and 2 for FU
28ed2 4c 4c 2e 20 20 52 65 74 75 72 6e 20 31 20 66 6f  LL.  Return 1 fo
28ed3 72 20 61 6e 20 65 6d 70 74 79 20 6f 72 20 0a 2a  r an empty or .*
28ed4 2a 20 75 6e 72 65 63 6f 67 6e 69 7a 65 64 20 73  * unrecognized s
28ed5 74 72 69 6e 67 20 61 72 67 75 6d 65 6e 74 2e 0a  tring argument..
28ed6 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
28ed7 74 68 65 20 76 61 6c 75 65 73 20 72 65 74 75 72  the values retur
28ed8 6e 65 64 20 61 72 65 20 6f 6e 65 20 6c 65 73 73  ned are one less
28ed9 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 73   that the values
28eda 20 74 68 61 74 0a 2a 2a 20 73 68 6f 75 6c 64 20   that.** should 
28edb 62 65 20 70 61 73 73 65 64 20 69 6e 74 6f 20 73  be passed into s
28edc 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 53 61  qlite3BtreeSetSa
28edd 66 65 74 79 4c 65 76 65 6c 28 29 2e 20 20 54 68  fetyLevel().  Th
28ede 65 20 69 73 20 64 6f 6e 65 0a 2a 2a 20 74 6f 20  e is done.** to 
28edf 73 75 70 70 6f 72 74 20 6c 65 67 61 63 79 20 53  support legacy S
28ee0 51 4c 20 63 6f 64 65 2e 20 20 54 68 65 20 73 61  QL code.  The sa
28ee1 66 65 74 79 20 6c 65 76 65 6c 20 75 73 65 64 20  fety level used 
28ee2 74 6f 20 62 65 20 62 6f 6f 6c 65 61 6e 0a 2a 2a  to be boolean.**
28ee3 20 61 6e 64 20 6f 6c 64 65 72 20 73 63 72 69 70   and older scrip
28ee4 74 73 20 6d 61 79 20 68 61 76 65 20 75 73 65 64  ts may have used
28ee5 20 6e 75 6d 62 65 72 73 20 30 20 66 6f 72 20 4f   numbers 0 for O
28ee6 46 46 20 61 6e 64 20 31 20 66 6f 72 20 4f 4e 2e  FF and 1 for ON.
28ee7 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38 20 67 65  .*/.static u8 ge
28ee8 74 53 61 66 65 74 79 4c 65 76 65 6c 28 63 6f 6e  tSafetyLevel(con
28ee9 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 20  st char *z){.   
28eea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28eeb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 32 33            /* 123
28eec 34 35 36 37 38 39 20 31 32 33 34 35 36 37 38 39  456789 123456789
28eed 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   */.  static con
28eee 73 74 20 63 68 61 72 20 7a 54 65 78 74 5b 5d 20  st char zText[] 
28eef 3d 20 22 6f 6e 6f 66 66 61 6c 73 65 79 65 73 74  = "onoffalseyest
28ef0 72 75 65 66 75 6c 6c 22 3b 0a 20 20 73 74 61 74  ruefull";.  stat
28ef1 69 63 20 63 6f 6e 73 74 20 75 38 20 69 4f 66 66  ic const u8 iOff
28ef2 73 65 74 5b 5d 20 3d 20 7b 30 2c 20 31 2c 20 32  set[] = {0, 1, 2
28ef3 2c 20 34 2c 20 39 2c 20 31 32 2c 20 31 36 7d 3b  , 4, 9, 12, 16};
28ef4 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
28ef5 75 38 20 69 4c 65 6e 67 74 68 5b 5d 20 3d 20 7b  u8 iLength[] = {
28ef6 32 2c 20 32 2c 20 33 2c 20 35 2c 20 33 2c 20 34  2, 2, 3, 5, 3, 4
28ef7 2c 20 34 7d 3b 0a 20 20 73 74 61 74 69 63 20 63  , 4};.  static c
28ef8 6f 6e 73 74 20 75 38 20 69 56 61 6c 75 65 5b 5d  onst u8 iValue[]
28ef9 20 3d 20 20 7b 31 2c 20 30 2c 20 30 2c 20 30 2c   =  {1, 0, 0, 0,
28efa 20 31 2c 20 31 2c 20 32 7d 3b 0a 20 20 69 6e 74   1, 1, 2};.  int
28efb 20 69 2c 20 6e 3b 0a 20 20 69 66 28 20 73 71 6c   i, n;.  if( sql
28efc 69 74 65 33 49 73 64 69 67 69 74 28 2a 7a 29 20  ite3Isdigit(*z) 
28efd 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 75  ){.    return (u
28efe 38 29 61 74 6f 69 28 7a 29 3b 0a 20 20 7d 0a 20  8)atoi(z);.  }. 
28eff 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
28f00 65 6e 33 30 28 7a 29 3b 0a 20 20 66 6f 72 28 69  en30(z);.  for(i
28f01 3d 30 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28  =0; i<ArraySize(
28f02 69 4c 65 6e 67 74 68 29 3b 20 69 2b 2b 29 7b 0a  iLength); i++){.
28f03 20 20 20 20 69 66 28 20 69 4c 65 6e 67 74 68 5b      if( iLength[
28f04 69 5d 3d 3d 6e 20 26 26 20 73 71 6c 69 74 65 33  i]==n && sqlite3
28f05 53 74 72 4e 49 43 6d 70 28 26 7a 54 65 78 74 5b  StrNICmp(&zText[
28f06 69 4f 66 66 73 65 74 5b 69 5d 5d 2c 7a 2c 6e 29  iOffset[i]],z,n)
28f07 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ==0 ){.      ret
28f08 75 72 6e 20 69 56 61 6c 75 65 5b 69 5d 3b 0a 20  urn iValue[i];. 
28f09 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
28f0a 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  n 1;.}../*.** In
28f0b 74 65 72 70 72 65 74 20 74 68 65 20 67 69 76 65  terpret the give
28f0c 6e 20 73 74 72 69 6e 67 20 61 73 20 61 20 62 6f  n string as a bo
28f0d 6f 6c 65 61 6e 20 76 61 6c 75 65 2e 0a 2a 2f 0a  olean value..*/.
28f0e 73 74 61 74 69 63 20 75 38 20 67 65 74 42 6f 6f  static u8 getBoo
28f0f 6c 65 61 6e 28 63 6f 6e 73 74 20 63 68 61 72 20  lean(const char 
28f10 2a 7a 29 7b 0a 20 20 72 65 74 75 72 6e 20 67 65  *z){.  return ge
28f11 74 53 61 66 65 74 79 4c 65 76 65 6c 28 7a 29 26  tSafetyLevel(z)&
28f12 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65  1;.}../*.** Inte
28f13 72 70 72 65 74 20 74 68 65 20 67 69 76 65 6e 20  rpret the given 
28f14 73 74 72 69 6e 67 20 61 73 20 61 20 6c 6f 63 6b  string as a lock
28f15 69 6e 67 20 6d 6f 64 65 20 76 61 6c 75 65 2e 0a  ing mode value..
28f16 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
28f17 74 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 63 6f 6e  tLockingMode(con
28f18 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69  st char *z){.  i
28f19 66 28 20 7a 20 29 7b 0a 20 20 20 20 69 66 28 20  f( z ){.    if( 
28f1a 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
28f1b 70 28 7a 2c 20 22 65 78 63 6c 75 73 69 76 65 22  p(z, "exclusive"
28f1c 29 20 29 20 72 65 74 75 72 6e 20 50 41 47 45 52  ) ) return PAGER
28f1d 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43  _LOCKINGMODE_EXC
28f1e 4c 55 53 49 56 45 3b 0a 20 20 20 20 69 66 28 20  LUSIVE;.    if( 
28f1f 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d  0==sqlite3StrICm
28f20 70 28 7a 2c 20 22 6e 6f 72 6d 61 6c 22 29 20 29  p(z, "normal") )
28f21 20 72 65 74 75 72 6e 20 50 41 47 45 52 5f 4c 4f   return PAGER_LO
28f22 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c  CKINGMODE_NORMAL
28f23 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 50  ;.  }.  return P
28f24 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45  AGER_LOCKINGMODE
28f25 5f 51 55 45 52 59 3b 0a 7d 0a 0a 23 69 66 6e 64  _QUERY;.}..#ifnd
28f26 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
28f27 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20  UTOVACUUM./*.** 
28f28 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 67 69  Interpret the gi
28f29 76 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61 6e  ven string as an
28f2a 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 6d 6f 64   auto-vacuum mod
28f2b 65 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54  e value..**.** T
28f2c 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
28f2d 69 6e 67 73 2c 20 22 6e 6f 6e 65 22 2c 20 22 66  ings, "none", "f
28f2e 75 6c 6c 22 20 61 6e 64 20 22 69 6e 63 72 65 6d  ull" and "increm
28f2f 65 6e 74 61 6c 22 20 61 72 65 20 0a 2a 2a 20 61  ental" are .** a
28f30 63 63 65 70 74 61 62 6c 65 2c 20 61 73 20 61 72  cceptable, as ar
28f31 65 20 74 68 65 69 72 20 6e 75 6d 65 72 69 63 20  e their numeric 
28f32 65 71 75 69 76 61 6c 65 6e 74 73 3a 20 30 2c 20  equivalents: 0, 
28f33 31 20 61 6e 64 20 32 20 72 65 73 70 65 63 74 69  1 and 2 respecti
28f34 76 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  vely..*/.static 
28f35 69 6e 74 20 67 65 74 41 75 74 6f 56 61 63 75 75  int getAutoVacuu
28f36 6d 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29  m(const char *z)
28f37 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28  {.  int i;.  if(
28f38 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43   0==sqlite3StrIC
28f39 6d 70 28 7a 2c 20 22 6e 6f 6e 65 22 29 20 29 20  mp(z, "none") ) 
28f3a 72 65 74 75 72 6e 20 42 54 52 45 45 5f 41 55 54  return BTREE_AUT
28f3b 4f 56 41 43 55 55 4d 5f 4e 4f 4e 45 3b 0a 20 20  OVACUUM_NONE;.  
28f3c 69 66 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74  if( 0==sqlite3St
28f3d 72 49 43 6d 70 28 7a 2c 20 22 66 75 6c 6c 22 29  rICmp(z, "full")
28f3e 20 29 20 72 65 74 75 72 6e 20 42 54 52 45 45 5f   ) return BTREE_
28f3f 41 55 54 4f 56 41 43 55 55 4d 5f 46 55 4c 4c 3b  AUTOVACUUM_FULL;
28f40 0a 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65  .  if( 0==sqlite
28f41 33 53 74 72 49 43 6d 70 28 7a 2c 20 22 69 6e 63  3StrICmp(z, "inc
28f42 72 65 6d 65 6e 74 61 6c 22 29 20 29 20 72 65 74  remental") ) ret
28f43 75 72 6e 20 42 54 52 45 45 5f 41 55 54 4f 56 41  urn BTREE_AUTOVA
28f44 43 55 55 4d 5f 49 4e 43 52 3b 0a 20 20 69 20 3d  CUUM_INCR;.  i =
28f45 20 61 74 6f 69 28 7a 29 3b 0a 20 20 72 65 74 75   atoi(z);.  retu
28f46 72 6e 20 28 75 38 29 28 28 69 3e 3d 30 26 26 69  rn (u8)((i>=0&&i
28f47 3c 3d 32 29 3f 69 3a 30 29 3b 0a 7d 0a 23 65 6e  <=2)?i:0);.}.#en
28f48 64 69 66 20 2f 2a 20 69 66 6e 64 65 66 20 53 51  dif /* ifndef SQ
28f49 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
28f4a 43 55 55 4d 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  CUUM */..#ifndef
28f4b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47   SQLITE_OMIT_PAG
28f4c 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a  ER_PRAGMAS./*.**
28f4d 20 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 67   Interpret the g
28f4e 69 76 65 6e 20 73 74 72 69 6e 67 20 61 73 20 61  iven string as a
28f4f 20 74 65 6d 70 20 64 62 20 6c 6f 63 61 74 69 6f   temp db locatio
28f50 6e 2e 20 52 65 74 75 72 6e 20 31 20 66 6f 72 20  n. Return 1 for 
28f51 66 69 6c 65 0a 2a 2a 20 62 61 63 6b 65 64 20 74  file.** backed t
28f52 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
28f53 65 73 2c 20 32 20 66 6f 72 20 74 68 65 20 52 65  es, 2 for the Re
28f54 64 2d 42 6c 61 63 6b 20 74 72 65 65 20 69 6e 20  d-Black tree in 
28f55 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 0a  memory database.
28f56 2a 2a 20 61 6e 64 20 30 20 74 6f 20 75 73 65 20  ** and 0 to use 
28f57 74 68 65 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  the compile-time
28f58 20 64 65 66 61 75 6c 74 2e 0a 2a 2f 0a 73 74 61   default..*/.sta
28f59 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 53  tic int getTempS
28f5a 74 6f 72 65 28 63 6f 6e 73 74 20 63 68 61 72 20  tore(const char 
28f5b 2a 7a 29 7b 0a 20 20 69 66 28 20 7a 5b 30 5d 3e  *z){.  if( z[0]>
28f5c 3d 27 30 27 20 26 26 20 7a 5b 30 5d 3c 3d 27 32  ='0' && z[0]<='2
28f5d 27 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ' ){.    return 
28f5e 7a 5b 30 5d 20 2d 20 27 30 27 3b 0a 20 20 7d 65  z[0] - '0';.  }e
28f5f 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 53  lse if( sqlite3S
28f60 74 72 49 43 6d 70 28 7a 2c 20 22 66 69 6c 65 22  trICmp(z, "file"
28f61 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )==0 ){.    retu
28f62 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66  rn 1;.  }else if
28f63 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
28f64 28 7a 2c 20 22 6d 65 6d 6f 72 79 22 29 3d 3d 30  (z, "memory")==0
28f65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 32   ){.    return 2
28f66 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
28f67 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 23  eturn 0;.  }.}.#
28f68 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
28f69 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f  PAGER_PRAGMAS */
28f6a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
28f6b 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47  _OMIT_PAGER_PRAG
28f6c 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69  MAS./*.** Invali
28f6d 64 61 74 65 20 74 65 6d 70 20 73 74 6f 72 61 67  date temp storag
28f6e 65 2c 20 65 69 74 68 65 72 20 77 68 65 6e 20 74  e, either when t
28f6f 68 65 20 74 65 6d 70 20 73 74 6f 72 61 67 65 20  he temp storage 
28f70 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 66 72  is changed.** fr
28f71 6f 6d 20 64 65 66 61 75 6c 74 2c 20 6f 72 20 77  om default, or w
28f72 68 65 6e 20 27 66 69 6c 65 27 20 61 6e 64 20 74  hen 'file' and t
28f73 68 65 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69  he temp_store_di
28f74 72 65 63 74 6f 72 79 20 68 61 73 20 63 68 61 6e  rectory has chan
28f75 67 65 64 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ged.*/.static in
28f76 74 20 69 6e 76 61 6c 69 64 61 74 65 54 65 6d 70  t invalidateTemp
28f77 53 74 6f 72 61 67 65 28 50 61 72 73 65 20 2a 70  Storage(Parse *p
28f78 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65  Parse){.  sqlite
28f79 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
28f7a 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 61 44  db;.  if( db->aD
28f7b 62 5b 31 5d 2e 70 42 74 21 3d 30 20 29 7b 0a 20  b[1].pBt!=0 ){. 
28f7c 20 20 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f     if( !db->auto
28f7d 43 6f 6d 6d 69 74 20 7c 7c 20 73 71 6c 69 74 65  Commit || sqlite
28f7e 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72  3BtreeIsInReadTr
28f7f 61 6e 73 28 64 62 2d 3e 61 44 62 5b 31 5d 2e 70  ans(db->aDb[1].p
28f80 42 74 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Bt) ){.      sql
28f81 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
28f82 72 73 65 2c 20 22 74 65 6d 70 6f 72 61 72 79 20  rse, "temporary 
28f83 73 74 6f 72 61 67 65 20 63 61 6e 6e 6f 74 20 62  storage cannot b
28f84 65 20 63 68 61 6e 67 65 64 20 22 0a 20 20 20 20  e changed ".    
28f85 20 20 20 20 22 66 72 6f 6d 20 77 69 74 68 69 6e      "from within
28f86 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 22 29   a transaction")
28f87 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
28f88 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
28f89 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74   }.    sqlite3Bt
28f8a 72 65 65 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62  reeClose(db->aDb
28f8b 5b 31 5d 2e 70 42 74 29 3b 0a 20 20 20 20 64 62  [1].pBt);.    db
28f8c 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 20 3d 20 30  ->aDb[1].pBt = 0
28f8d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73  ;.    sqlite3Res
28f8e 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
28f8f 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72  (db, 0);.  }.  r
28f90 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
28f91 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
28f92 49 54 45 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  ITE_PAGER_PRAGMA
28f93 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  S */..#ifndef SQ
28f94 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
28f95 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 49 66  PRAGMAS./*.** If
28f96 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62 61   the TEMP databa
28f97 73 65 20 69 73 20 6f 70 65 6e 2c 20 63 6c 6f 73  se is open, clos
28f98 65 20 69 74 20 61 6e 64 20 6d 61 72 6b 20 74 68  e it and mark th
28f99 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
28f9a 61 0a 2a 2a 20 61 73 20 6e 65 65 64 69 6e 67 20  a.** as needing 
28f9b 72 65 6c 6f 61 64 69 6e 67 2e 20 20 54 68 69 73  reloading.  This
28f9c 20 6d 75 73 74 20 62 65 20 64 6f 6e 65 20 77 68   must be done wh
28f9d 65 6e 20 75 73 69 6e 67 20 74 68 65 20 53 51 4c  en using the SQL
28f9e 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 0a 2a  ITE_TEMP_STORE.*
28f9f 2a 20 6f 72 20 44 45 46 41 55 4c 54 5f 54 45 4d  * or DEFAULT_TEM
28fa0 50 5f 53 54 4f 52 45 20 70 72 61 67 6d 61 73 2e  P_STORE pragmas.
28fa1 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
28fa2 68 61 6e 67 65 54 65 6d 70 53 74 6f 72 61 67 65  hangeTempStorage
28fa3 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
28fa4 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 6f  const char *zSto
28fa5 72 61 67 65 54 79 70 65 29 7b 0a 20 20 69 6e 74  rageType){.  int
28fa6 20 74 73 20 3d 20 67 65 74 54 65 6d 70 53 74 6f   ts = getTempSto
28fa7 72 65 28 7a 53 74 6f 72 61 67 65 54 79 70 65 29  re(zStorageType)
28fa8 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
28fa9 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
28faa 69 66 28 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f  if( db->temp_sto
28fab 72 65 3d 3d 74 73 20 29 20 72 65 74 75 72 6e 20  re==ts ) return 
28fac 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
28fad 20 69 6e 76 61 6c 69 64 61 74 65 54 65 6d 70 53   invalidateTempS
28fae 74 6f 72 61 67 65 28 20 70 50 61 72 73 65 20 29  torage( pParse )
28faf 20 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 29 7b   != SQLITE_OK ){
28fb0 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
28fb1 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
28fb2 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 20 3d  db->temp_store =
28fb3 20 28 75 38 29 74 73 3b 0a 20 20 72 65 74 75 72   (u8)ts;.  retur
28fb4 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23  n SQLITE_OK;.}.#
28fb5 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
28fb6 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f  PAGER_PRAGMAS */
28fb7 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
28fb8 20 63 6f 64 65 20 74 6f 20 72 65 74 75 72 6e 20   code to return 
28fb9 61 20 73 69 6e 67 6c 65 20 69 6e 74 65 67 65 72  a single integer
28fba 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69   value..*/.stati
28fbb 63 20 76 6f 69 64 20 72 65 74 75 72 6e 53 69 6e  c void returnSin
28fbc 67 6c 65 49 6e 74 28 50 61 72 73 65 20 2a 70 50  gleInt(Parse *pP
28fbd 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  arse, const char
28fbe 20 2a 7a 4c 61 62 65 6c 2c 20 69 36 34 20 76 61   *zLabel, i64 va
28fbf 6c 75 65 29 7b 0a 20 20 56 64 62 65 20 2a 76 20  lue){.  Vdbe *v 
28fc0 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
28fc1 28 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 20  (pParse);.  int 
28fc2 6d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  mem = ++pParse->
28fc3 6e 4d 65 6d 3b 0a 20 20 69 36 34 20 2a 70 49 36  nMem;.  i64 *pI6
28fc4 34 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  4 = sqlite3DbMal
28fc5 6c 6f 63 52 61 77 28 70 50 61 72 73 65 2d 3e 64  locRaw(pParse->d
28fc6 62 2c 20 73 69 7a 65 6f 66 28 76 61 6c 75 65 29  b, sizeof(value)
28fc7 29 3b 0a 20 20 69 66 28 20 70 49 36 34 20 29 7b  );.  if( pI64 ){
28fc8 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 49 36 34  .    memcpy(pI64
28fc9 2c 20 26 76 61 6c 75 65 2c 20 73 69 7a 65 6f 66  , &value, sizeof
28fca 28 76 61 6c 75 65 29 29 3b 0a 20 20 7d 0a 20 20  (value));.  }.  
28fcb 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28fcc 34 28 76 2c 20 4f 50 5f 49 6e 74 36 34 2c 20 30  4(v, OP_Int64, 0
28fcd 2c 20 6d 65 6d 2c 20 30 2c 20 28 63 68 61 72 2a  , mem, 0, (char*
28fce 29 70 49 36 34 2c 20 50 34 5f 49 4e 54 36 34 29  )pI64, P4_INT64)
28fcf 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 53  ;.  sqlite3VdbeS
28fd0 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b  etNumCols(v, 1);
28fd1 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  .  sqlite3VdbeSe
28fd2 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
28fd3 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4c 61  OLNAME_NAME, zLa
28fd4 62 65 6c 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  bel, SQLITE_STAT
28fd5 49 43 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  IC);.  sqlite3Vd
28fd6 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
28fd7 65 73 75 6c 74 52 6f 77 2c 20 6d 65 6d 2c 20 31  esultRow, mem, 1
28fd8 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  );.}..#ifndef SQ
28fd9 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50  LITE_OMIT_FLAG_P
28fda 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a 20 43 68 65  RAGMAS./*.** Che
28fdb 63 6b 20 74 6f 20 73 65 65 20 69 66 20 7a 52 69  ck to see if zRi
28fdc 67 68 74 20 61 6e 64 20 7a 4c 65 66 74 20 72 65  ght and zLeft re
28fdd 66 65 72 20 74 6f 20 61 20 70 72 61 67 6d 61 20  fer to a pragma 
28fde 74 68 61 74 20 71 75 65 72 69 65 73 0a 2a 2a 20  that queries.** 
28fdf 6f 72 20 63 68 61 6e 67 65 73 20 6f 6e 65 20 6f  or changes one o
28fe0 66 20 74 68 65 20 66 6c 61 67 73 20 69 6e 20 64  f the flags in d
28fe1 62 2d 3e 66 6c 61 67 73 2e 20 20 52 65 74 75 72  b->flags.  Retur
28fe2 6e 20 31 20 69 66 20 73 6f 20 61 6e 64 20 30 20  n 1 if so and 0 
28fe3 69 66 20 6e 6f 74 2e 0a 2a 2a 20 41 6c 73 6f 2c  if not..** Also,
28fe4 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 70   implement the p
28fe5 72 61 67 6d 61 2e 0a 2a 2f 0a 73 74 61 74 69 63  ragma..*/.static
28fe6 20 69 6e 74 20 66 6c 61 67 50 72 61 67 6d 61 28   int flagPragma(
28fe7 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63  Parse *pParse, c
28fe8 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74  onst char *zLeft
28fe9 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52  , const char *zR
28fea 69 67 68 74 29 7b 0a 20 20 73 74 61 74 69 63 20  ight){.  static 
28feb 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73 50 72  const struct sPr
28fec 61 67 6d 61 54 79 70 65 20 7b 0a 20 20 20 20 63  agmaType {.    c
28fed 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
28fee 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68  ;  /* Name of th
28fef 65 20 70 72 61 67 6d 61 20 2a 2f 0a 20 20 20 20  e pragma */.    
28ff0 69 6e 74 20 6d 61 73 6b 3b 20 20 20 20 20 20 20  int mask;       
28ff1 20 20 20 20 2f 2a 20 4d 61 73 6b 20 66 6f 72 20      /* Mask for 
28ff2 74 68 65 20 64 62 2d 3e 66 6c 61 67 73 20 76 61  the db->flags va
28ff3 6c 75 65 20 2a 2f 0a 20 20 7d 20 61 50 72 61 67  lue */.  } aPrag
28ff4 6d 61 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22  ma[] = {.    { "
28ff5 66 75 6c 6c 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  full_column_name
28ff6 73 22 2c 20 20 20 20 20 20 20 20 53 51 4c 49 54  s",        SQLIT
28ff7 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73 20 20  E_FullColNames  
28ff8 7d 2c 0a 20 20 20 20 7b 20 22 73 68 6f 72 74 5f  },.    { "short_
28ff9 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 73 22 2c 20 20  column_names",  
28ffa 20 20 20 20 20 53 51 4c 49 54 45 5f 53 68 6f 72       SQLITE_Shor
28ffb 74 43 6f 6c 4e 61 6d 65 73 20 7d 2c 0a 20 20 20  tColNames },.   
28ffc 20 7b 20 22 63 6f 75 6e 74 5f 63 68 61 6e 67 65   { "count_change
28ffd 73 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 53  s",            S
28ffe 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20  QLITE_CountRows 
28fff 20 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 65 6d      },.    { "em
29000 70 74 79 5f 72 65 73 75 6c 74 5f 63 61 6c 6c 62  pty_result_callb
29001 61 63 6b 73 22 2c 20 20 20 53 51 4c 49 54 45 5f  acks",   SQLITE_
29002 4e 75 6c 6c 43 61 6c 6c 62 61 63 6b 20 20 7d 2c  NullCallback  },
29003 0a 20 20 20 20 7b 20 22 6c 65 67 61 63 79 5f 66  .    { "legacy_f
29004 69 6c 65 5f 66 6f 72 6d 61 74 22 2c 20 20 20 20  ile_format",    
29005 20 20 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 79     SQLITE_Legacy
29006 46 69 6c 65 46 6d 74 20 7d 2c 0a 20 20 20 20 7b  FileFmt },.    {
29007 20 22 66 75 6c 6c 66 73 79 6e 63 22 2c 20 20 20   "fullfsync",   
29008 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
29009 49 54 45 5f 46 75 6c 6c 46 53 79 6e 63 20 20 20  ITE_FullFSync   
2900a 20 20 7d 2c 0a 20 20 20 20 7b 20 22 72 65 76 65    },.    { "reve
2900b 72 73 65 5f 75 6e 6f 72 64 65 72 65 64 5f 73 65  rse_unordered_se
2900c 6c 65 63 74 73 22 2c 20 53 51 4c 49 54 45 5f 52  lects", SQLITE_R
2900d 65 76 65 72 73 65 4f 72 64 65 72 20 20 7d 2c 0a  everseOrder  },.
2900e 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
2900f 42 55 47 0a 20 20 20 20 7b 20 22 73 71 6c 5f 74  BUG.    { "sql_t
29010 72 61 63 65 22 2c 20 20 20 20 20 20 20 20 20 20  race",          
29011 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 71 6c        SQLITE_Sql
29012 54 72 61 63 65 20 20 20 20 20 20 7d 2c 0a 20 20  Trace      },.  
29013 20 20 7b 20 22 76 64 62 65 5f 6c 69 73 74 69 6e    { "vdbe_listin
29014 67 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  g",             
29015 53 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69  SQLITE_VdbeListi
29016 6e 67 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 76  ng   },.    { "v
29017 64 62 65 5f 74 72 61 63 65 22 2c 20 20 20 20 20  dbe_trace",     
29018 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
29019 5f 56 64 62 65 54 72 61 63 65 20 20 20 20 20 7d  _VdbeTrace     }
2901a 2c 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  ,.#endif.#ifndef
2901b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45   SQLITE_OMIT_CHE
2901c 43 4b 0a 20 20 20 20 7b 20 22 69 67 6e 6f 72 65  CK.    { "ignore
2901d 5f 63 68 65 63 6b 5f 63 6f 6e 73 74 72 61 69 6e  _check_constrain
2901e 74 73 22 2c 20 53 51 4c 49 54 45 5f 49 67 6e 6f  ts", SQLITE_Igno
2901f 72 65 43 68 65 63 6b 73 20 20 7d 2c 0a 23 65 6e  reChecks  },.#en
29020 64 69 66 0a 20 20 20 20 2f 2a 20 54 68 65 20 66  dif.    /* The f
29021 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 56 45 52 59  ollowing is VERY
29022 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 2a 2f   experimental */
29023 0a 20 20 20 20 7b 20 22 77 72 69 74 61 62 6c 65  .    { "writable
29024 5f 73 63 68 65 6d 61 22 2c 20 20 20 20 20 20 20  _schema",       
29025 20 20 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53     SQLITE_WriteS
29026 63 68 65 6d 61 7c 53 51 4c 49 54 45 5f 52 65 63  chema|SQLITE_Rec
29027 6f 76 65 72 79 4d 6f 64 65 20 7d 2c 0a 20 20 20  overyMode },.   
29028 20 7b 20 22 6f 6d 69 74 5f 72 65 61 64 6c 6f 63   { "omit_readloc
29029 6b 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 53  k",            S
2902a 51 4c 49 54 45 5f 4e 6f 52 65 61 64 6c 6f 63 6b  QLITE_NoReadlock
2902b 20 20 20 20 7d 2c 0a 0a 20 20 20 20 2f 2a 20 54      },..    /* T
2902c 4f 44 4f 3a 20 4d 61 79 62 65 20 69 74 20 73 68  ODO: Maybe it sh
2902d 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69  ouldn't be possi
2902e 62 6c 65 20 74 6f 20 63 68 61 6e 67 65 20 74 68  ble to change th
2902f 65 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65  e ReadUncommitte
29030 64 0a 20 20 20 20 2a 2a 20 66 6c 61 67 20 69 66  d.    ** flag if
29031 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 61   there are any a
29032 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73  ctive statements
29033 2e 20 2a 2f 0a 20 20 20 20 7b 20 22 72 65 61 64  . */.    { "read
29034 5f 75 6e 63 6f 6d 6d 69 74 74 65 64 22 2c 20 20  _uncommitted",  
29035 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 52 65         SQLITE_Re
29036 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 7d 2c  adUncommitted },
29037 0a 20 20 20 20 7b 20 22 72 65 63 75 72 73 69 76  .    { "recursiv
29038 65 5f 74 72 69 67 67 65 72 73 22 2c 20 20 20 20  e_triggers",    
29039 20 20 20 53 51 4c 49 54 45 5f 52 65 63 54 72 69     SQLITE_RecTri
2903a 67 67 65 72 73 20 7d 2c 0a 0a 20 20 20 20 2f 2a  ggers },..    /*
2903b 20 54 68 69 73 20 66 6c 61 67 20 6d 61 79 20 6f   This flag may o
2903c 6e 6c 79 20 62 65 20 73 65 74 20 69 66 20 62 6f  nly be set if bo
2903d 74 68 20 66 6f 72 65 69 67 6e 2d 6b 65 79 20 61  th foreign-key a
2903e 6e 64 20 74 72 69 67 67 65 72 20 73 75 70 70 6f  nd trigger suppo
2903f 72 74 0a 20 20 20 20 2a 2a 20 61 72 65 20 70 72  rt.    ** are pr
29040 65 73 65 6e 74 20 69 6e 20 74 68 65 20 62 75 69  esent in the bui
29041 6c 64 2e 20 20 2a 2f 0a 23 69 66 20 21 64 65 66  ld.  */.#if !def
29042 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
29043 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 29 20 26 26  _FOREIGN_KEY) &&
29044 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
29045 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 20  _OMIT_TRIGGER). 
29046 20 20 20 7b 20 22 66 6f 72 65 69 67 6e 5f 6b 65     { "foreign_ke
29047 79 73 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  ys",            
29048 20 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b   SQLITE_ForeignK
29049 65 79 73 20 7d 2c 0a 23 65 6e 64 69 66 0a 20 20  eys },.#endif.  
2904a 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f  };.  int i;.  co
2904b 6e 73 74 20 73 74 72 75 63 74 20 73 50 72 61 67  nst struct sPrag
2904c 6d 61 54 79 70 65 20 2a 70 3b 0a 20 20 66 6f 72  maType *p;.  for
2904d 28 69 3d 30 2c 20 70 3d 61 50 72 61 67 6d 61 3b  (i=0, p=aPragma;
2904e 20 69 3c 41 72 72 61 79 53 69 7a 65 28 61 50 72   i<ArraySize(aPr
2904f 61 67 6d 61 29 3b 20 69 2b 2b 2c 20 70 2b 2b 29  agma); i++, p++)
29050 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
29051 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
29052 70 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a  p->zName)==0 ){.
29053 20 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64        sqlite3 *d
29054 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
29055 20 20 20 20 20 20 56 64 62 65 20 2a 76 3b 0a 20        Vdbe *v;. 
29056 20 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33       v = sqlite3
29057 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
29058 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 76  .      assert( v
29059 21 3d 30 20 29 3b 20 20 2f 2a 20 41 6c 72 65 61  !=0 );  /* Alrea
2905a 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20  dy allocated by 
2905b 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28 29 20  sqlite3Pragma() 
2905c 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57  */.      if( ALW
2905d 41 59 53 28 76 29 20 29 7b 0a 20 20 20 20 20 20  AYS(v) ){.      
2905e 20 20 69 66 28 20 7a 52 69 67 68 74 3d 3d 30 20    if( zRight==0 
2905f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  ){.          ret
29060 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61  urnSingleInt(pPa
29061 72 73 65 2c 20 70 2d 3e 7a 4e 61 6d 65 2c 20 28  rse, p->zName, (
29062 64 62 2d 3e 66 6c 61 67 73 20 26 20 70 2d 3e 6d  db->flags & p->m
29063 61 73 6b 29 21 3d 30 20 29 3b 0a 20 20 20 20 20  ask)!=0 );.     
29064 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
29065 20 20 20 20 69 6e 74 20 6d 61 73 6b 20 3d 20 70      int mask = p
29066 2d 3e 6d 61 73 6b 3b 20 20 20 20 20 20 20 20 20  ->mask;         
29067 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 62 69 74 73   /* Mask of bits
29068 20 74 6f 20 73 65 74 20 6f 72 20 63 6c 65 61 72   to set or clear
29069 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69  . */.          i
2906a 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  f( db->autoCommi
2906b 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  t==0 ){.        
2906c 20 20 20 20 2f 2a 20 46 6f 72 65 69 67 6e 20 6b      /* Foreign k
2906d 65 79 20 73 75 70 70 6f 72 74 20 6d 61 79 20 6e  ey support may n
2906e 6f 74 20 62 65 20 65 6e 61 62 6c 65 64 20 6f 72  ot be enabled or
2906f 20 64 69 73 61 62 6c 65 64 20 77 68 69 6c 65 20   disabled while 
29070 6e 6f 74 0a 20 20 20 20 20 20 20 20 20 20 20 20  not.            
29071 2a 2a 20 69 6e 20 61 75 74 6f 2d 63 6f 6d 6d 69  ** in auto-commi
29072 74 20 6d 6f 64 65 2e 20 20 2a 2f 0a 20 20 20 20  t mode.  */.    
29073 20 20 20 20 20 20 20 20 6d 61 73 6b 20 26 3d 20          mask &= 
29074 7e 28 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e  ~(SQLITE_Foreign
29075 4b 65 79 73 29 3b 0a 20 20 20 20 20 20 20 20 20  Keys);.         
29076 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 69 66   }..          if
29077 28 20 67 65 74 42 6f 6f 6c 65 61 6e 28 7a 52 69  ( getBoolean(zRi
29078 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ght) ){.        
29079 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d      db->flags |=
2907a 20 6d 61 73 6b 3b 0a 20 20 20 20 20 20 20 20 20   mask;.         
2907b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2907c 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d      db->flags &=
2907d 20 7e 6d 61 73 6b 3b 0a 20 20 20 20 20 20 20 20   ~mask;.        
2907e 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 2f    }..          /
2907f 2a 20 4d 61 6e 79 20 6f 66 20 74 68 65 20 66 6c  * Many of the fl
29080 61 67 2d 70 72 61 67 6d 61 73 20 6d 6f 64 69 66  ag-pragmas modif
29081 79 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72  y the code gener
29082 61 74 65 64 20 62 79 20 74 68 65 20 53 51 4c 20  ated by the SQL 
29083 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f  .          ** co
29084 6d 70 69 6c 65 72 20 28 65 67 2e 20 63 6f 75 6e  mpiler (eg. coun
29085 74 5f 63 68 61 6e 67 65 73 29 2e 20 53 6f 20 61  t_changes). So a
29086 64 64 20 61 6e 20 6f 70 63 6f 64 65 20 74 6f 20  dd an opcode to 
29087 65 78 70 69 72 65 20 61 6c 6c 0a 20 20 20 20 20  expire all.     
29088 20 20 20 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64       ** compiled
29089 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20   SQL statements 
2908a 61 66 74 65 72 20 6d 6f 64 69 66 79 69 6e 67 20  after modifying 
2908b 61 20 70 72 61 67 6d 61 20 76 61 6c 75 65 2e 0a  a pragma value..
2908c 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
2908d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2908e 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 45  beAddOp2(v, OP_E
2908f 78 70 69 72 65 2c 20 30 2c 20 30 29 3b 0a 20 20  xpire, 0, 0);.  
29090 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
29091 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
29092 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
29093 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20  urn 0;.}.#endif 
29094 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  /* SQLITE_OMIT_F
29095 4c 41 47 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a  LAG_PRAGMAS */..
29096 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 68  /*.** Return a h
29097 75 6d 61 6e 2d 72 65 61 64 61 62 6c 65 20 6e 61  uman-readable na
29098 6d 65 20 66 6f 72 20 61 20 63 6f 6e 73 74 72 61  me for a constra
29099 69 6e 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 61  int resolution a
2909a 63 74 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65  ction..*/.#ifnde
2909b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f  f SQLITE_OMIT_FO
2909c 52 45 49 47 4e 5f 4b 45 59 0a 73 74 61 74 69 63  REIGN_KEY.static
2909d 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61 63 74   const char *act
2909e 69 6f 6e 4e 61 6d 65 28 75 38 20 61 63 74 69 6f  ionName(u8 actio
2909f 6e 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  n){.  const char
290a0 20 2a 7a 4e 61 6d 65 3b 0a 20 20 73 77 69 74 63   *zName;.  switc
290a1 68 28 20 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20  h( action ){.   
290a2 20 63 61 73 65 20 4f 45 5f 53 65 74 4e 75 6c 6c   case OE_SetNull
290a3 3a 20 20 7a 4e 61 6d 65 20 3d 20 22 53 45 54 20  :  zName = "SET 
290a4 4e 55 4c 4c 22 3b 20 20 20 20 20 20 20 20 62 72  NULL";        br
290a5 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 45  eak;.    case OE
290a6 5f 53 65 74 44 66 6c 74 3a 20 20 7a 4e 61 6d 65  _SetDflt:  zName
290a7 20 3d 20 22 53 45 54 20 44 45 46 41 55 4c 54 22   = "SET DEFAULT"
290a8 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  ;     break;.   
290a9 20 63 61 73 65 20 4f 45 5f 43 61 73 63 61 64 65   case OE_Cascade
290aa 3a 20 20 7a 4e 61 6d 65 20 3d 20 22 43 41 53 43  :  zName = "CASC
290ab 41 44 45 22 3b 20 20 20 20 20 20 20 20 20 62 72  ADE";         br
290ac 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 45  eak;.    case OE
290ad 5f 52 65 73 74 72 69 63 74 3a 20 7a 4e 61 6d 65  _Restrict: zName
290ae 20 3d 20 22 52 45 53 54 52 49 43 54 22 3b 20 20   = "RESTRICT";  
290af 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
290b0 20 64 65 66 61 75 6c 74 3a 20 20 20 20 20 20 20   default:       
290b1 20 20 20 7a 4e 61 6d 65 20 3d 20 22 4e 4f 20 41     zName = "NO A
290b2 43 54 49 4f 4e 22 3b 20 20 0a 20 20 20 20 20 20  CTION";  .      
290b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
290b4 61 73 73 65 72 74 28 20 61 63 74 69 6f 6e 3d 3d  assert( action==
290b5 4f 45 5f 4e 6f 6e 65 20 29 3b 20 62 72 65 61 6b  OE_None ); break
290b6 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  ;.  }.  return z
290b7 4e 61 6d 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  Name;.}.#endif..
290b8 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 61 20  /*.** Process a 
290b9 70 72 61 67 6d 61 20 73 74 61 74 65 6d 65 6e 74  pragma statement
290ba 2e 20 20 0a 2a 2a 0a 2a 2a 20 50 72 61 67 6d 61  .  .**.** Pragma
290bb 73 20 61 72 65 20 6f 66 20 74 68 69 73 20 66 6f  s are of this fo
290bc 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 50  rm:.**.**      P
290bd 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
290be 5d 69 64 20 5b 3d 20 76 61 6c 75 65 5d 0a 2a 2a  ]id [= value].**
290bf 0a 2a 2a 20 54 68 65 20 69 64 65 6e 74 69 66 69  .** The identifi
290c0 65 72 20 6d 69 67 68 74 20 61 6c 73 6f 20 62 65  er might also be
290c1 20 61 20 73 74 72 69 6e 67 2e 20 20 54 68 65 20   a string.  The 
290c2 76 61 6c 75 65 20 69 73 20 61 20 73 74 72 69 6e  value is a strin
290c3 67 2c 20 61 6e 64 0a 2a 2a 20 69 64 65 6e 74 69  g, and.** identi
290c4 66 69 65 72 2c 20 6f 72 20 61 20 6e 75 6d 62 65  fier, or a numbe
290c5 72 2e 20 20 49 66 20 6d 69 6e 75 73 46 6c 61 67  r.  If minusFlag
290c6 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20 74   is true, then t
290c7 68 65 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61  he value is.** a
290c8 20 6e 75 6d 62 65 72 20 74 68 61 74 20 77 61 73   number that was
290c9 20 70 72 65 63 65 64 65 64 20 62 79 20 61 20 6d   preceded by a m
290ca 69 6e 75 73 20 73 69 67 6e 2e 0a 2a 2a 0a 2a 2a  inus sign..**.**
290cb 20 49 66 20 74 68 65 20 6c 65 66 74 20 73 69 64   If the left sid
290cc 65 20 69 73 20 22 64 61 74 61 62 61 73 65 2e 69  e is "database.i
290cd 64 22 20 74 68 65 6e 20 70 49 64 31 20 69 73 20  d" then pId1 is 
290ce 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  the database nam
290cf 65 0a 2a 2a 20 61 6e 64 20 70 49 64 32 20 69 73  e.** and pId2 is
290d0 20 74 68 65 20 69 64 2e 20 20 49 66 20 74 68 65   the id.  If the
290d1 20 6c 65 66 74 20 73 69 64 65 20 69 73 20 6a 75   left side is ju
290d2 73 74 20 22 69 64 22 20 74 68 65 6e 20 70 49 64  st "id" then pId
290d3 31 20 69 73 20 74 68 65 0a 2a 2a 20 69 64 20 61  1 is the.** id a
290d4 6e 64 20 70 49 64 32 20 69 73 20 61 6e 79 20 65  nd pId2 is any e
290d5 6d 70 74 79 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a  mpty string..*/.
290d6 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
290d7 6f 69 64 20 73 71 6c 69 74 65 33 50 72 61 67 6d  oid sqlite3Pragm
290d8 61 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  a(.  Parse *pPar
290d9 73 65 2c 20 0a 20 20 54 6f 6b 65 6e 20 2a 70 49  se, .  Token *pI
290da 64 31 2c 20 20 20 20 20 20 20 20 2f 2a 20 46 69  d1,        /* Fi
290db 72 73 74 20 70 61 72 74 20 6f 66 20 5b 64 61 74  rst part of [dat
290dc 61 62 61 73 65 2e 5d 69 64 20 66 69 65 6c 64 20  abase.]id field 
290dd 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 49 64 32  */.  Token *pId2
290de 2c 20 20 20 20 20 20 20 20 2f 2a 20 53 65 63 6f  ,        /* Seco
290df 6e 64 20 70 61 72 74 20 6f 66 20 5b 64 61 74 61  nd part of [data
290e0 62 61 73 65 2e 5d 69 64 20 66 69 65 6c 64 2c 20  base.]id field, 
290e1 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b  or NULL */.  Tok
290e2 65 6e 20 2a 70 56 61 6c 75 65 2c 20 20 20 20 20  en *pValue,     
290e3 20 2f 2a 20 54 6f 6b 65 6e 20 66 6f 72 20 3c 76   /* Token for <v
290e4 61 6c 75 65 3e 2c 20 6f 72 20 4e 55 4c 4c 20 2a  alue>, or NULL *
290e5 2f 0a 20 20 69 6e 74 20 6d 69 6e 75 73 46 6c 61  /.  int minusFla
290e6 67 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  g       /* True 
290e7 69 66 20 61 20 27 2d 27 20 73 69 67 6e 20 70 72  if a '-' sign pr
290e8 65 63 65 64 65 64 20 3c 76 61 6c 75 65 3e 20 2a  eceded <value> *
290e9 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4c 65  /.){.  char *zLe
290ea 66 74 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a  ft = 0;       /*
290eb 20 4e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20   Nul-terminated 
290ec 55 54 46 2d 38 20 73 74 72 69 6e 67 20 3c 69 64  UTF-8 string <id
290ed 3e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 52 69  > */.  char *zRi
290ee 67 68 74 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a  ght = 0;      /*
290ef 20 4e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20   Nul-terminated 
290f0 55 54 46 2d 38 20 73 74 72 69 6e 67 20 3c 76 61  UTF-8 string <va
290f1 6c 75 65 3e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f  lue>, or NULL */
290f2 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
290f3 44 62 20 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65  Db = 0;   /* The
290f4 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 2a   database name *
290f5 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 49 64 3b 20  /.  Token *pId; 
290f6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f             /* Po
290f7 69 6e 74 65 72 20 74 6f 20 3c 69 64 3e 20 74 6f  inter to <id> to
290f8 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ken */.  int iDb
290f9 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
290fa 2f 2a 20 44 61 74 61 62 61 73 65 20 69 6e 64 65  /* Database inde
290fb 78 20 66 6f 72 20 3c 64 61 74 61 62 61 73 65 3e  x for <database>
290fc 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
290fd 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
290fe 20 20 44 62 20 2a 70 44 62 3b 0a 20 20 56 64 62    Db *pDb;.  Vdb
290ff 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *v = pParse->p
29100 56 64 62 65 20 3d 20 73 71 6c 69 74 65 33 56 64  Vdbe = sqlite3Vd
29101 62 65 43 72 65 61 74 65 28 64 62 29 3b 0a 20 20  beCreate(db);.  
29102 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72  if( v==0 ) retur
29103 6e 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65  n;.  pParse->nMe
29104 6d 20 3d 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 74  m = 2;..  /* Int
29105 65 72 70 72 65 74 20 74 68 65 20 5b 64 61 74 61  erpret the [data
29106 62 61 73 65 2e 5d 20 70 61 72 74 20 6f 66 20 74  base.] part of t
29107 68 65 20 70 72 61 67 6d 61 20 73 74 61 74 65 6d  he pragma statem
29108 65 6e 74 2e 20 69 44 62 20 69 73 20 74 68 65 0a  ent. iDb is the.
29109 20 20 2a 2a 20 69 6e 64 65 78 20 6f 66 20 74 68    ** index of th
2910a 65 20 64 61 74 61 62 61 73 65 20 74 68 69 73 20  e database this 
2910b 70 72 61 67 6d 61 20 69 73 20 62 65 69 6e 67 20  pragma is being 
2910c 61 70 70 6c 69 65 64 20 74 6f 20 69 6e 20 64 62  applied to in db
2910d 2e 61 44 62 5b 5d 2e 20 2a 2f 0a 20 20 69 44 62  .aDb[]. */.  iDb
2910e 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72   = sqlite3TwoPar
2910f 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 49  tName(pParse, pI
29110 64 31 2c 20 70 49 64 32 2c 20 26 70 49 64 29 3b  d1, pId2, &pId);
29111 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29 20 72  .  if( iDb<0 ) r
29112 65 74 75 72 6e 3b 0a 20 20 70 44 62 20 3d 20 26  eturn;.  pDb = &
29113 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a 20  db->aDb[iDb];.. 
29114 20 2f 2a 20 49 66 20 74 68 65 20 74 65 6d 70 20   /* If the temp 
29115 64 61 74 61 62 61 73 65 20 68 61 73 20 62 65 65  database has bee
29116 6e 20 65 78 70 6c 69 63 69 74 6c 79 20 6e 61 6d  n explicitly nam
29117 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68  ed as part of th
29118 65 20 0a 20 20 2a 2a 20 70 72 61 67 6d 61 2c 20  e .  ** pragma, 
29119 6d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73 20  make sure it is 
2911a 6f 70 65 6e 2e 20 0a 20 20 2a 2f 0a 20 20 69 66  open. .  */.  if
2911b 28 20 69 44 62 3d 3d 31 20 26 26 20 73 71 6c 69  ( iDb==1 && sqli
2911c 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74 61 62  te3OpenTempDatab
2911d 61 73 65 28 70 50 61 72 73 65 29 20 29 7b 0a 20  ase(pParse) ){. 
2911e 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
2911f 20 20 7a 4c 65 66 74 20 3d 20 73 71 6c 69 74 65    zLeft = sqlite
29120 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
29121 62 2c 20 70 49 64 29 3b 0a 20 20 69 66 28 20 21  b, pId);.  if( !
29122 7a 4c 65 66 74 20 29 20 72 65 74 75 72 6e 3b 0a  zLeft ) return;.
29123 20 20 69 66 28 20 6d 69 6e 75 73 46 6c 61 67 20    if( minusFlag 
29124 29 7b 0a 20 20 20 20 7a 52 69 67 68 74 20 3d 20  ){.    zRight = 
29125 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
29126 62 2c 20 22 2d 25 54 22 2c 20 70 56 61 6c 75 65  b, "-%T", pValue
29127 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
29128 7a 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33  zRight = sqlite3
29129 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
2912a 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20 7d 0a 0a  , pValue);.  }..
2912b 20 20 61 73 73 65 72 74 28 20 70 49 64 32 20 29    assert( pId2 )
2912c 3b 0a 20 20 7a 44 62 20 3d 20 70 49 64 32 2d 3e  ;.  zDb = pId2->
2912d 6e 3e 30 20 3f 20 70 44 62 2d 3e 7a 4e 61 6d 65  n>0 ? pDb->zName
2912e 20 3a 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69   : 0;.  if( sqli
2912f 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61  te3AuthCheck(pPa
29130 72 73 65 2c 20 53 51 4c 49 54 45 5f 50 52 41 47  rse, SQLITE_PRAG
29131 4d 41 2c 20 7a 4c 65 66 74 2c 20 7a 52 69 67 68  MA, zLeft, zRigh
29132 74 2c 20 7a 44 62 29 20 29 7b 0a 20 20 20 20 67  t, zDb) ){.    g
29133 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a  oto pragma_out;.
29134 20 20 7d 0a 20 0a 23 69 66 6e 64 65 66 20 53 51    }. .#ifndef SQ
29135 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
29136 50 52 41 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a  PRAGMAS.  /*.  *
29137 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62  *  PRAGMA [datab
29138 61 73 65 2e 5d 64 65 66 61 75 6c 74 5f 63 61 63  ase.]default_cac
29139 68 65 5f 73 69 7a 65 0a 20 20 2a 2a 20 20 50 52  he_size.  **  PR
2913a 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
2913b 64 65 66 61 75 6c 74 5f 63 61 63 68 65 5f 73 69  default_cache_si
2913c 7a 65 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  ze=N.  **.  ** T
2913d 68 65 20 66 69 72 73 74 20 66 6f 72 6d 20 72 65  he first form re
2913e 70 6f 72 74 73 20 74 68 65 20 63 75 72 72 65 6e  ports the curren
2913f 74 20 70 65 72 73 69 73 74 65 6e 74 20 73 65 74  t persistent set
29140 74 69 6e 67 20 66 6f 72 20 74 68 65 0a 20 20 2a  ting for the.  *
29141 2a 20 70 61 67 65 20 63 61 63 68 65 20 73 69 7a  * page cache siz
29142 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20 72 65  e.  The value re
29143 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 6d 61  turned is the ma
29144 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 0a  ximum number of.
29145 20 20 2a 2a 20 70 61 67 65 73 20 69 6e 20 74 68    ** pages in th
29146 65 20 70 61 67 65 20 63 61 63 68 65 2e 20 20 54  e page cache.  T
29147 68 65 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 73  he second form s
29148 65 74 73 20 62 6f 74 68 20 74 68 65 20 63 75 72  ets both the cur
29149 72 65 6e 74 0a 20 20 2a 2a 20 70 61 67 65 20 63  rent.  ** page c
2914a 61 63 68 65 20 73 69 7a 65 20 76 61 6c 75 65 20  ache size value 
2914b 61 6e 64 20 74 68 65 20 70 65 72 73 69 73 74 65  and the persiste
2914c 6e 74 20 70 61 67 65 20 63 61 63 68 65 20 73 69  nt page cache si
2914d 7a 65 20 76 61 6c 75 65 0a 20 20 2a 2a 20 73 74  ze value.  ** st
2914e 6f 72 65 64 20 69 6e 20 74 68 65 20 64 61 74 61  ored in the data
2914f 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 2a 2a 0a  base file..  **.
29150 20 20 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74    ** The default
29151 20 63 61 63 68 65 20 73 69 7a 65 20 69 73 20 73   cache size is s
29152 74 6f 72 65 64 20 69 6e 20 6d 65 74 61 2d 76 61  tored in meta-va
29153 6c 75 65 20 32 20 6f 66 20 70 61 67 65 20 31 20  lue 2 of page 1 
29154 6f 66 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61  of the.  ** data
29155 62 61 73 65 20 66 69 6c 65 2e 20 20 54 68 65 20  base file.  The 
29156 63 61 63 68 65 20 73 69 7a 65 20 69 73 20 61 63  cache size is ac
29157 74 75 61 6c 6c 79 20 74 68 65 20 61 62 73 6f 6c  tually the absol
29158 75 74 65 20 76 61 6c 75 65 20 6f 66 0a 20 20 2a  ute value of.  *
29159 2a 20 74 68 69 73 20 6d 65 6d 6f 72 79 20 6c 6f  * this memory lo
2915a 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 73 69 67  cation.  The sig
2915b 6e 20 6f 66 20 6d 65 74 61 2d 76 61 6c 75 65 20  n of meta-value 
2915c 32 20 64 65 74 65 72 6d 69 6e 65 73 20 74 68 65  2 determines the
2915d 0a 20 20 2a 2a 20 73 79 6e 63 68 72 6f 6e 6f 75  .  ** synchronou
2915e 73 20 73 65 74 74 69 6e 67 2e 20 20 41 20 6e 65  s setting.  A ne
2915f 67 61 74 69 76 65 20 76 61 6c 75 65 20 6d 65 61  gative value mea
29160 6e 73 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69  ns synchronous i
29161 73 20 6f 66 66 0a 20 20 2a 2a 20 61 6e 64 20 61  s off.  ** and a
29162 20 70 6f 73 69 74 69 76 65 20 76 61 6c 75 65 20   positive value 
29163 6d 65 61 6e 73 20 73 79 6e 63 68 72 6f 6e 6f 75  means synchronou
29164 73 20 69 73 20 6f 6e 2e 0a 20 20 2a 2f 0a 20 20  s is on..  */.  
29165 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
29166 6d 70 28 7a 4c 65 66 74 2c 22 64 65 66 61 75 6c  mp(zLeft,"defaul
29167 74 5f 63 61 63 68 65 5f 73 69 7a 65 22 29 3d 3d  t_cache_size")==
29168 30 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63 20  0 ){.    static 
29169 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74  const VdbeOpList
2916a 20 67 65 74 43 61 63 68 65 53 69 7a 65 5b 5d 20   getCacheSize[] 
2916b 3d 20 7b 0a 20 20 20 20 20 20 7b 20 4f 50 5f 54  = {.      { OP_T
2916c 72 61 6e 73 61 63 74 69 6f 6e 2c 20 30 2c 20 30  ransaction, 0, 0
2916d 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20  ,        0},    
2916e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2916f 20 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20       /* 0 */.   
29170 20 20 20 7b 20 4f 50 5f 52 65 61 64 43 6f 6f 6b     { OP_ReadCook
29171 69 65 2c 20 20 30 2c 20 31 2c 20 20 20 20 20 20  ie,  0, 1,      
29172 20 20 42 54 52 45 45 5f 44 45 46 41 55 4c 54 5f    BTREE_DEFAULT_
29173 43 41 43 48 45 5f 53 49 5a 45 7d 2c 20 20 2f 2a  CACHE_SIZE},  /*
29174 20 31 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50   1 */.      { OP
29175 5f 49 66 50 6f 73 2c 20 20 20 20 20 20 20 31 2c  _IfPos,       1,
29176 20 37 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20   7,        0},. 
29177 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65       { OP_Intege
29178 72 2c 20 20 20 20 20 30 2c 20 32 2c 20 20 20 20  r,     0, 2,    
29179 20 20 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b 20      0},.      { 
2917a 4f 50 5f 53 75 62 74 72 61 63 74 2c 20 20 20 20  OP_Subtract,    
2917b 31 2c 20 32 2c 20 20 20 20 20 20 20 20 31 7d 2c  1, 2,        1},
2917c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49 66 50 6f  .      { OP_IfPo
2917d 73 2c 20 20 20 20 20 20 20 31 2c 20 37 2c 20 20  s,       1, 7,  
2917e 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 20 20        0},.      
2917f 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 20 20  { OP_Integer,   
29180 20 20 30 2c 20 31 2c 20 20 20 20 20 20 20 20 30    0, 1,        0
29181 7d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  },              
29182 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 36 20             /* 6 
29183 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 52 65  */.      { OP_Re
29184 73 75 6c 74 52 6f 77 2c 20 20 20 31 2c 20 31 2c  sultRow,   1, 1,
29185 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20          0},.    
29186 7d 3b 0a 20 20 20 20 69 6e 74 20 61 64 64 72 3b  };.    int addr;
29187 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
29188 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
29189 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61  e) ) goto pragma
2918a 5f 6f 75 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  _out;.    sqlite
2918b 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76  3VdbeUsesBtree(v
2918c 2c 20 69 44 62 29 3b 0a 20 20 20 20 69 66 28 20  , iDb);.    if( 
2918d 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  !zRight ){.     
2918e 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
2918f 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20  umCols(v, 1);.  
29190 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
29191 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
29192 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 63  COLNAME_NAME, "c
29193 61 63 68 65 5f 73 69 7a 65 22 2c 20 53 51 4c 49  ache_size", SQLI
29194 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
29195 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b    pParse->nMem +
29196 3d 20 32 3b 0a 20 20 20 20 20 20 61 64 64 72 20  = 2;.      addr 
29197 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
29198 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53  OpList(v, ArrayS
29199 69 7a 65 28 67 65 74 43 61 63 68 65 53 69 7a 65  ize(getCacheSize
2919a 29 2c 20 67 65 74 43 61 63 68 65 53 69 7a 65 29  ), getCacheSize)
2919b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2919c 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61  dbeChangeP1(v, a
2919d 64 64 72 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  ddr, iDb);.     
2919e 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
2919f 67 65 50 31 28 76 2c 20 61 64 64 72 2b 31 2c 20  geP1(v, addr+1, 
291a0 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  iDb);.      sqli
291a1 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28  te3VdbeChangeP1(
291a2 76 2c 20 61 64 64 72 2b 36 2c 20 53 51 4c 49 54  v, addr+6, SQLIT
291a3 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f  E_DEFAULT_CACHE_
291a4 53 49 5a 45 29 3b 0a 20 20 20 20 7d 65 6c 73 65  SIZE);.    }else
291a5 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65  {.      int size
291a6 20 3d 20 61 74 6f 69 28 7a 52 69 67 68 74 29 3b   = atoi(zRight);
291a7 0a 20 20 20 20 20 20 69 66 28 20 73 69 7a 65 3c  .      if( size<
291a8 30 20 29 20 73 69 7a 65 20 3d 20 2d 73 69 7a 65  0 ) size = -size
291a9 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  ;.      sqlite3B
291aa 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
291ab 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44  on(pParse, 0, iD
291ac 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
291ad 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
291ae 50 5f 49 6e 74 65 67 65 72 2c 20 73 69 7a 65 2c  P_Integer, size,
291af 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   1);.      sqlit
291b0 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
291b1 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 69  OP_ReadCookie, i
291b2 44 62 2c 20 32 2c 20 42 54 52 45 45 5f 44 45 46  Db, 2, BTREE_DEF
291b3 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 29  AULT_CACHE_SIZE)
291b4 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73  ;.      addr = s
291b5 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
291b6 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 32 2c  (v, OP_IfPos, 2,
291b7 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   0);.      sqlit
291b8 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
291b9 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 73 69 7a  OP_Integer, -siz
291ba 65 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  e, 1);.      sql
291bb 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
291bc 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 20  (v, addr);.     
291bd 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
291be 70 33 28 76 2c 20 4f 50 5f 53 65 74 43 6f 6f 6b  p3(v, OP_SetCook
291bf 69 65 2c 20 69 44 62 2c 20 42 54 52 45 45 5f 44  ie, iDb, BTREE_D
291c0 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a  EFAULT_CACHE_SIZ
291c1 45 2c 20 31 29 3b 0a 20 20 20 20 20 20 70 44 62  E, 1);.      pDb
291c2 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65  ->pSchema->cache
291c3 5f 73 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20  _size = size;.  
291c4 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
291c5 53 65 74 43 61 63 68 65 53 69 7a 65 28 70 44 62  SetCacheSize(pDb
291c6 2d 3e 70 42 74 2c 20 70 44 62 2d 3e 70 53 63 68  ->pBt, pDb->pSch
291c7 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 29  ema->cache_size)
291c8 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  ;.    }.  }else.
291c9 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47  .  /*.  **  PRAG
291ca 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 70 61  MA [database.]pa
291cb 67 65 5f 73 69 7a 65 0a 20 20 2a 2a 20 20 50 52  ge_size.  **  PR
291cc 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
291cd 70 61 67 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a  page_size=N.  **
291ce 0a 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20  .  ** The first 
291cf 66 6f 72 6d 20 72 65 70 6f 72 74 73 20 74 68 65  form reports the
291d0 20 63 75 72 72 65 6e 74 20 73 65 74 74 69 6e 67   current setting
291d1 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 64 61   for the.  ** da
291d2 74 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65  tabase page size
291d3 20 69 6e 20 62 79 74 65 73 2e 20 20 54 68 65 20   in bytes.  The 
291d4 73 65 63 6f 6e 64 20 66 6f 72 6d 20 73 65 74 73  second form sets
291d5 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61   the.  ** databa
291d6 73 65 20 70 61 67 65 20 73 69 7a 65 20 76 61 6c  se page size val
291d7 75 65 2e 20 20 54 68 65 20 76 61 6c 75 65 20 63  ue.  The value c
291d8 61 6e 20 6f 6e 6c 79 20 62 65 20 73 65 74 20 69  an only be set i
291d9 66 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62  f.  ** the datab
291da 61 73 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20  ase has not yet 
291db 62 65 65 6e 20 63 72 65 61 74 65 64 2e 0a 20 20  been created..  
291dc 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
291dd 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 70  StrICmp(zLeft,"p
291de 61 67 65 5f 73 69 7a 65 22 29 3d 3d 30 20 29 7b  age_size")==0 ){
291df 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20  .    Btree *pBt 
291e0 3d 20 70 44 62 2d 3e 70 42 74 3b 0a 20 20 20 20  = pDb->pBt;.    
291e1 61 73 73 65 72 74 28 20 70 42 74 21 3d 30 20 29  assert( pBt!=0 )
291e2 3b 0a 20 20 20 20 69 66 28 20 21 7a 52 69 67 68  ;.    if( !zRigh
291e3 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 73  t ){.      int s
291e4 69 7a 65 20 3d 20 41 4c 57 41 59 53 28 70 42 74  ize = ALWAYS(pBt
291e5 29 20 3f 20 73 71 6c 69 74 65 33 42 74 72 65 65  ) ? sqlite3Btree
291e6 47 65 74 50 61 67 65 53 69 7a 65 28 70 42 74 29  GetPageSize(pBt)
291e7 20 3a 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75   : 0;.      retu
291e8 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50 61 72  rnSingleInt(pPar
291e9 73 65 2c 20 22 70 61 67 65 5f 73 69 7a 65 22 2c  se, "page_size",
291ea 20 73 69 7a 65 29 3b 0a 20 20 20 20 7d 65 6c 73   size);.    }els
291eb 65 7b 0a 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c  e{.      /* Mall
291ec 6f 63 20 6d 61 79 20 66 61 69 6c 20 77 68 65 6e  oc may fail when
291ed 20 73 65 74 74 69 6e 67 20 74 68 65 20 70 61 67   setting the pag
291ee 65 2d 73 69 7a 65 2c 20 61 73 20 74 68 65 72 65  e-size, as there
291ef 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 0a   is an internal.
291f0 20 20 20 20 20 20 2a 2a 20 62 75 66 66 65 72 20        ** buffer 
291f1 74 68 61 74 20 74 68 65 20 70 61 67 65 72 20 6d  that the pager m
291f2 6f 64 75 6c 65 20 72 65 73 69 7a 65 73 20 75 73  odule resizes us
291f3 69 6e 67 20 73 71 6c 69 74 65 33 5f 72 65 61 6c  ing sqlite3_real
291f4 6c 6f 63 28 29 2e 0a 20 20 20 20 20 20 2a 2f 0a  loc()..      */.
291f5 20 20 20 20 20 20 64 62 2d 3e 6e 65 78 74 50 61        db->nextPa
291f6 67 65 73 69 7a 65 20 3d 20 61 74 6f 69 28 7a 52  gesize = atoi(zR
291f7 69 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66 28  ight);.      if(
291f8 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3d 3d 73   SQLITE_NOMEM==s
291f9 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 50 61  qlite3BtreeSetPa
291fa 67 65 53 69 7a 65 28 70 42 74 2c 20 64 62 2d 3e  geSize(pBt, db->
291fb 6e 65 78 74 50 61 67 65 73 69 7a 65 2c 20 2d 31  nextPagesize, -1
291fc 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20  , 0) ){.        
291fd 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
291fe 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
291ff 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f    }.  }else..  /
29200 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  *.  **  PRAGMA [
29201 64 61 74 61 62 61 73 65 2e 5d 6d 61 78 5f 70 61  database.]max_pa
29202 67 65 5f 63 6f 75 6e 74 0a 20 20 2a 2a 20 20 50  ge_count.  **  P
29203 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
29204 5d 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e 74 3d  ]max_page_count=
29205 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  N.  **.  ** The 
29206 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70 6f 72  first form repor
29207 74 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  ts the current s
29208 65 74 74 69 6e 67 20 66 6f 72 20 74 68 65 0a 20  etting for the. 
29209 20 2a 2a 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62   ** maximum numb
2920a 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74  er of pages in t
2920b 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
2920c 2e 20 20 54 68 65 20 0a 20 20 2a 2a 20 73 65 63  .  The .  ** sec
2920d 6f 6e 64 20 66 6f 72 6d 20 61 74 74 65 6d 70 74  ond form attempt
2920e 73 20 74 6f 20 63 68 61 6e 67 65 20 74 68 69 73  s to change this
2920f 20 73 65 74 74 69 6e 67 2e 20 20 42 6f 74 68 0a   setting.  Both.
29210 20 20 2a 2a 20 66 6f 72 6d 73 20 72 65 74 75 72    ** forms retur
29211 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 65  n the current se
29212 74 74 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 69 66  tting..  */.  if
29213 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
29214 28 7a 4c 65 66 74 2c 22 6d 61 78 5f 70 61 67 65  (zLeft,"max_page
29215 5f 63 6f 75 6e 74 22 29 3d 3d 30 20 29 7b 0a 20  _count")==0 ){. 
29216 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20     Btree *pBt = 
29217 70 44 62 2d 3e 70 42 74 3b 0a 20 20 20 20 69 6e  pDb->pBt;.    in
29218 74 20 6e 65 77 4d 61 78 20 3d 20 30 3b 0a 20 20  t newMax = 0;.  
29219 20 20 61 73 73 65 72 74 28 20 70 42 74 21 3d 30    assert( pBt!=0
2921a 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 52 69 67   );.    if( zRig
2921b 68 74 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4d  ht ){.      newM
2921c 61 78 20 3d 20 61 74 6f 69 28 7a 52 69 67 68 74  ax = atoi(zRight
2921d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
2921e 20 41 4c 57 41 59 53 28 70 42 74 29 20 29 7b 0a   ALWAYS(pBt) ){.
2921f 20 20 20 20 20 20 6e 65 77 4d 61 78 20 3d 20 73        newMax = s
29220 71 6c 69 74 65 33 42 74 72 65 65 4d 61 78 50 61  qlite3BtreeMaxPa
29221 67 65 43 6f 75 6e 74 28 70 42 74 2c 20 6e 65 77  geCount(pBt, new
29222 4d 61 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Max);.    }.    
29223 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28  returnSingleInt(
29224 70 50 61 72 73 65 2c 20 22 6d 61 78 5f 70 61 67  pParse, "max_pag
29225 65 5f 63 6f 75 6e 74 22 2c 20 6e 65 77 4d 61 78  e_count", newMax
29226 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a  );.  }else..  /*
29227 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64  .  **  PRAGMA [d
29228 61 74 61 62 61 73 65 2e 5d 70 61 67 65 5f 63 6f  atabase.]page_co
29229 75 6e 74 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65  unt.  **.  ** Re
2922a 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
2922b 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20  of pages in the 
2922c 73 70 65 63 69 66 69 65 64 20 64 61 74 61 62 61  specified databa
2922d 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  se..  */.  if( s
2922e 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
2922f 65 66 74 2c 22 70 61 67 65 5f 63 6f 75 6e 74 22  eft,"page_count"
29230 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  )==0 ){.    int 
29231 69 52 65 67 3b 0a 20 20 20 20 69 66 28 20 73 71  iReg;.    if( sq
29232 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
29233 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70  pParse) ) goto p
29234 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 73  ragma_out;.    s
29235 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79  qlite3CodeVerify
29236 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69  Schema(pParse, i
29237 44 62 29 3b 0a 20 20 20 20 69 52 65 67 20 3d 20  Db);.    iReg = 
29238 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
29239 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2923a 64 64 4f 70 32 28 76 2c 20 4f 50 5f 50 61 67 65  ddOp2(v, OP_Page
2923b 63 6f 75 6e 74 2c 20 69 44 62 2c 20 69 52 65 67  count, iDb, iReg
2923c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
2923d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
2923e 65 73 75 6c 74 52 6f 77 2c 20 69 52 65 67 2c 20  esultRow, iReg, 
2923f 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
29240 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
29241 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   1);.    sqlite3
29242 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
29243 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
29244 45 2c 20 22 70 61 67 65 5f 63 6f 75 6e 74 22 2c  E, "page_count",
29245 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
29246 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20  .  }else..  /*. 
29247 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74   **  PRAGMA [dat
29248 61 62 61 73 65 2e 5d 6c 6f 63 6b 69 6e 67 5f 6d  abase.]locking_m
29249 6f 64 65 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41  ode.  **  PRAGMA
2924a 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f 63 6b   [database.]lock
2924b 69 6e 67 5f 6d 6f 64 65 20 3d 20 28 6e 6f 72 6d  ing_mode = (norm
2924c 61 6c 7c 65 78 63 6c 75 73 69 76 65 29 0a 20 20  al|exclusive).  
2924d 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
2924e 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 6c  StrICmp(zLeft,"l
2924f 6f 63 6b 69 6e 67 5f 6d 6f 64 65 22 29 3d 3d 30  ocking_mode")==0
29250 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68   ){.    const ch
29251 61 72 20 2a 7a 52 65 74 20 3d 20 22 6e 6f 72 6d  ar *zRet = "norm
29252 61 6c 22 3b 0a 20 20 20 20 69 6e 74 20 65 4d 6f  al";.    int eMo
29253 64 65 20 3d 20 67 65 74 4c 6f 63 6b 69 6e 67 4d  de = getLockingM
29254 6f 64 65 28 7a 52 69 67 68 74 29 3b 0a 0a 20 20  ode(zRight);..  
29255 20 20 69 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30    if( pId2->n==0
29256 20 26 26 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52   && eMode==PAGER
29257 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45  _LOCKINGMODE_QUE
29258 52 59 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53  RY ){.      /* S
29259 69 6d 70 6c 65 20 22 50 52 41 47 4d 41 20 6c 6f  imple "PRAGMA lo
2925a 63 6b 69 6e 67 5f 6d 6f 64 65 3b 22 20 73 74 61  cking_mode;" sta
2925b 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73 20  tement. This is 
2925c 61 20 71 75 65 72 79 20 66 6f 72 0a 20 20 20 20  a query for.    
2925d 20 20 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74    ** the current
2925e 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67   default locking
2925f 20 6d 6f 64 65 20 28 77 68 69 63 68 20 6d 61 79   mode (which may
29260 20 62 65 20 64 69 66 66 65 72 65 6e 74 20 74 6f   be different to
29261 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 6f  .      ** the lo
29262 63 6b 69 6e 67 2d 6d 6f 64 65 20 6f 66 20 74 68  cking-mode of th
29263 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 29  e main database)
29264 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
29265 20 65 4d 6f 64 65 20 3d 20 64 62 2d 3e 64 66 6c   eMode = db->dfl
29266 74 4c 6f 63 6b 4d 6f 64 65 3b 0a 20 20 20 20 7d  tLockMode;.    }
29267 65 6c 73 65 7b 0a 20 20 20 20 20 20 50 61 67 65  else{.      Page
29268 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 20  r *pPager;.     
29269 20 69 66 28 20 70 49 64 32 2d 3e 6e 3d 3d 30 20   if( pId2->n==0 
2926a 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ){.        /* Th
2926b 69 73 20 69 6e 64 69 63 61 74 65 73 20 74 68 61  is indicates tha
2926c 74 20 6e 6f 20 64 61 74 61 62 61 73 65 20 6e 61  t no database na
2926d 6d 65 20 77 61 73 20 73 70 65 63 69 66 69 65 64  me was specified
2926e 20 61 73 20 70 61 72 74 0a 20 20 20 20 20 20 20   as part.       
2926f 20 2a 2a 20 6f 66 20 74 68 65 20 50 52 41 47 4d   ** of the PRAGM
29270 41 20 63 6f 6d 6d 61 6e 64 2e 20 49 6e 20 74 68  A command. In th
29271 69 73 20 63 61 73 65 20 74 68 65 20 6c 6f 63 6b  is case the lock
29272 69 6e 67 2d 6d 6f 64 65 20 6d 75 73 74 20 62 65  ing-mode must be
29273 0a 20 20 20 20 20 20 20 20 2a 2a 20 73 65 74 20  .        ** set 
29274 6f 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20  on all attached 
29275 64 61 74 61 62 61 73 65 73 2c 20 61 73 20 77 65  databases, as we
29276 6c 6c 20 61 73 20 74 68 65 20 6d 61 69 6e 20 64  ll as the main d
29277 62 20 66 69 6c 65 2e 0a 20 20 20 20 20 20 20 20  b file..        
29278 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 6c  **.        ** Al
29279 73 6f 2c 20 74 68 65 20 73 71 6c 69 74 65 33 2e  so, the sqlite3.
2927a 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65 20 76 61 72  dfltLockMode var
2927b 69 61 62 6c 65 20 69 73 20 73 65 74 20 73 6f 20  iable is set so 
2927c 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20  that.        ** 
2927d 61 6e 79 20 73 75 62 73 65 71 75 65 6e 74 6c 79  any subsequently
2927e 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
2927f 73 65 73 20 61 6c 73 6f 20 75 73 65 20 74 68 65  ses also use the
29280 20 73 70 65 63 69 66 69 65 64 0a 20 20 20 20 20   specified.     
29281 20 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67 20 6d 6f     ** locking mo
29282 64 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  de..        */. 
29283 20 20 20 20 20 20 20 69 6e 74 20 69 69 3b 0a 20         int ii;. 
29284 20 20 20 20 20 20 20 61 73 73 65 72 74 28 70 44         assert(pD
29285 62 3d 3d 26 64 62 2d 3e 61 44 62 5b 30 5d 29 3b  b==&db->aDb[0]);
29286 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 69 3d  .        for(ii=
29287 32 3b 20 69 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  2; ii<db->nDb; i
29288 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
29289 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33  pPager = sqlite3
2928a 42 74 72 65 65 50 61 67 65 72 28 64 62 2d 3e 61  BtreePager(db->a
2928b 44 62 5b 69 69 5d 2e 70 42 74 29 3b 0a 20 20 20  Db[ii].pBt);.   
2928c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
2928d 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 70  gerLockingMode(p
2928e 50 61 67 65 72 2c 20 65 4d 6f 64 65 29 3b 0a 20  Pager, eMode);. 
2928f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
29290 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64   db->dfltLockMod
29291 65 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b 0a 20  e = (u8)eMode;. 
29292 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61       }.      pPa
29293 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72  ger = sqlite3Btr
29294 65 65 50 61 67 65 72 28 70 44 62 2d 3e 70 42 74  eePager(pDb->pBt
29295 29 3b 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d  );.      eMode =
29296 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 63   sqlite3PagerLoc
29297 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67 65 72 2c  kingMode(pPager,
29298 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 0a   eMode);.    }..
29299 20 20 20 20 61 73 73 65 72 74 28 65 4d 6f 64 65      assert(eMode
2929a 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  ==PAGER_LOCKINGM
2929b 4f 44 45 5f 4e 4f 52 4d 41 4c 7c 7c 65 4d 6f 64  ODE_NORMAL||eMod
2929c 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  e==PAGER_LOCKING
2929d 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 29 3b  MODE_EXCLUSIVE);
2929e 0a 20 20 20 20 69 66 28 20 65 4d 6f 64 65 3d 3d  .    if( eMode==
2929f 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
292a0 45 5f 45 58 43 4c 55 53 49 56 45 20 29 7b 0a 20  E_EXCLUSIVE ){. 
292a1 20 20 20 20 20 7a 52 65 74 20 3d 20 22 65 78 63       zRet = "exc
292a2 6c 75 73 69 76 65 22 3b 0a 20 20 20 20 7d 0a 20  lusive";.    }. 
292a3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
292a4 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a  tNumCols(v, 1);.
292a5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
292a6 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
292a7 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6c  COLNAME_NAME, "l
292a8 6f 63 6b 69 6e 67 5f 6d 6f 64 65 22 2c 20 53 51  ocking_mode", SQ
292a9 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
292aa 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
292ab 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
292ac 38 2c 20 30 2c 20 31 2c 20 30 2c 20 7a 52 65 74  8, 0, 1, 0, zRet
292ad 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 0);.    sqlite
292ae 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
292af 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
292b0 31 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 2f  1);.  }else..  /
292b1 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  *.  **  PRAGMA [
292b2 64 61 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e 61  database.]journa
292b3 6c 5f 6d 6f 64 65 0a 20 20 2a 2a 20 20 50 52 41  l_mode.  **  PRA
292b4 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6a  GMA [database.]j
292b5 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d 20 28 64  ournal_mode = (d
292b6 65 6c 65 74 65 7c 70 65 72 73 69 73 74 7c 6f 66  elete|persist|of
292b7 66 7c 74 72 75 6e 63 61 74 65 7c 6d 65 6d 6f 72  f|truncate|memor
292b8 79 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  y).  */.  if( sq
292b9 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
292ba 66 74 2c 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65  ft,"journal_mode
292bb 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ")==0 ){.    int
292bc 20 65 4d 6f 64 65 3b 0a 20 20 20 20 73 74 61 74   eMode;.    stat
292bd 69 63 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20  ic char * const 
292be 61 7a 4d 6f 64 65 4e 61 6d 65 5b 5d 20 3d 20 7b  azModeName[] = {
292bf 0a 20 20 20 20 20 20 22 64 65 6c 65 74 65 22 2c  .      "delete",
292c0 20 22 70 65 72 73 69 73 74 22 2c 20 22 6f 66 66   "persist", "off
292c1 22 2c 20 22 74 72 75 6e 63 61 74 65 22 2c 20 22  ", "truncate", "
292c2 6d 65 6d 6f 72 79 22 0a 20 20 20 20 7d 3b 0a 0a  memory".    };..
292c3 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 3d 3d      if( zRight==
292c4 30 20 29 7b 0a 20 20 20 20 20 20 65 4d 6f 64 65  0 ){.      eMode
292c5 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c   = PAGER_JOURNAL
292c6 4d 4f 44 45 5f 51 55 45 52 59 3b 0a 20 20 20 20  MODE_QUERY;.    
292c7 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74  }else{.      int
292c8 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c   n = sqlite3Strl
292c9 65 6e 33 30 28 7a 52 69 67 68 74 29 3b 0a 20 20  en30(zRight);.  
292ca 20 20 20 20 65 4d 6f 64 65 20 3d 20 73 69 7a 65      eMode = size
292cb 6f 66 28 61 7a 4d 6f 64 65 4e 61 6d 65 29 2f 73  of(azModeName)/s
292cc 69 7a 65 6f 66 28 61 7a 4d 6f 64 65 4e 61 6d 65  izeof(azModeName
292cd 5b 30 5d 29 20 2d 20 31 3b 0a 20 20 20 20 20 20  [0]) - 1;.      
292ce 77 68 69 6c 65 28 20 65 4d 6f 64 65 3e 3d 30 20  while( eMode>=0 
292cf 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  && sqlite3StrNIC
292d0 6d 70 28 7a 52 69 67 68 74 2c 20 61 7a 4d 6f 64  mp(zRight, azMod
292d1 65 4e 61 6d 65 5b 65 4d 6f 64 65 5d 2c 20 6e 29  eName[eMode], n)
292d2 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 65  !=0 ){.        e
292d3 4d 6f 64 65 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a  Mode--;.      }.
292d4 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 49      }.    if( pI
292d5 64 32 2d 3e 6e 3d 3d 30 20 26 26 20 65 4d 6f 64  d2->n==0 && eMod
292d6 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
292d7 4d 4f 44 45 5f 51 55 45 52 59 20 29 7b 0a 20 20  MODE_QUERY ){.  
292d8 20 20 20 20 2f 2a 20 53 69 6d 70 6c 65 20 22 50      /* Simple "P
292d9 52 41 47 4d 41 20 6a 6f 75 72 6e 61 6c 5f 6d 6f  RAGMA journal_mo
292da 64 65 3b 22 20 73 74 61 74 65 6d 65 6e 74 2e 20  de;" statement. 
292db 54 68 69 73 20 69 73 20 61 20 71 75 65 72 79 20  This is a query 
292dc 66 6f 72 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  for.      ** the
292dd 20 63 75 72 72 65 6e 74 20 64 65 66 61 75 6c 74   current default
292de 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 20 28 77   journal mode (w
292df 68 69 63 68 20 6d 61 79 20 62 65 20 64 69 66 66  hich may be diff
292e0 65 72 65 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a  erent to.      *
292e1 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d 6f  * the journal-mo
292e2 64 65 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 64  de of the main d
292e3 61 74 61 62 61 73 65 29 2e 0a 20 20 20 20 20 20  atabase)..      
292e4 2a 2f 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d  */.      eMode =
292e5 20 64 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c   db->dfltJournal
292e6 4d 6f 64 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Mode;.    }else{
292e7 0a 20 20 20 20 20 20 50 61 67 65 72 20 2a 70 50  .      Pager *pP
292e8 61 67 65 72 3b 0a 20 20 20 20 20 20 69 66 28 20  ager;.      if( 
292e9 70 49 64 32 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20  pId2->n==0 ){.  
292ea 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69 6e        /* This in
292eb 64 69 63 61 74 65 73 20 74 68 61 74 20 6e 6f 20  dicates that no 
292ec 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20 77 61  database name wa
292ed 73 20 73 70 65 63 69 66 69 65 64 20 61 73 20 70  s specified as p
292ee 61 72 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f  art.        ** o
292ef 66 20 74 68 65 20 50 52 41 47 4d 41 20 63 6f 6d  f the PRAGMA com
292f0 6d 61 6e 64 2e 20 49 6e 20 74 68 69 73 20 63 61  mand. In this ca
292f1 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2d 6d  se the journal-m
292f2 6f 64 65 20 6d 75 73 74 20 62 65 0a 20 20 20 20  ode must be.    
292f3 20 20 20 20 2a 2a 20 73 65 74 20 6f 6e 20 61 6c      ** set on al
292f4 6c 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62  l attached datab
292f5 61 73 65 73 2c 20 61 73 20 77 65 6c 6c 20 61 73  ases, as well as
292f6 20 74 68 65 20 6d 61 69 6e 20 64 62 20 66 69 6c   the main db fil
292f7 65 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  e..        **.  
292f8 20 20 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 74        ** Also, t
292f9 68 65 20 73 71 6c 69 74 65 33 2e 64 66 6c 74 4a  he sqlite3.dfltJ
292fa 6f 75 72 6e 61 6c 4d 6f 64 65 20 76 61 72 69 61  ournalMode varia
292fb 62 6c 65 20 69 73 20 73 65 74 20 73 6f 20 74 68  ble is set so th
292fc 61 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e  at.        ** an
292fd 79 20 73 75 62 73 65 71 75 65 6e 74 6c 79 20 61  y subsequently a
292fe 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
292ff 73 20 61 6c 73 6f 20 75 73 65 20 74 68 65 20 73  s also use the s
29300 70 65 63 69 66 69 65 64 0a 20 20 20 20 20 20 20  pecified.       
29301 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65   ** journal mode
29302 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
29303 20 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20       int ii;.   
29304 20 20 20 20 20 61 73 73 65 72 74 28 70 44 62 3d       assert(pDb=
29305 3d 26 64 62 2d 3e 61 44 62 5b 30 5d 29 3b 0a 20  =&db->aDb[0]);. 
29306 20 20 20 20 20 20 20 66 6f 72 28 69 69 3d 31 3b         for(ii=1;
29307 20 69 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 69 2b   ii<db->nDb; ii+
29308 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
29309 28 20 64 62 2d 3e 61 44 62 5b 69 69 5d 2e 70 42  ( db->aDb[ii].pB
2930a 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  t ){.           
2930b 20 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65   pPager = sqlite
2930c 33 42 74 72 65 65 50 61 67 65 72 28 64 62 2d 3e  3BtreePager(db->
2930d 61 44 62 5b 69 69 5d 2e 70 42 74 29 3b 0a 20 20  aDb[ii].pBt);.  
2930e 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2930f 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64  3PagerJournalMod
29310 65 28 70 50 61 67 65 72 2c 20 65 4d 6f 64 65 29  e(pPager, eMode)
29311 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
29312 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
29313 64 62 2d 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d  db->dfltJournalM
29314 6f 64 65 20 3d 20 28 75 38 29 65 4d 6f 64 65 3b  ode = (u8)eMode;
29315 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
29316 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42  Pager = sqlite3B
29317 74 72 65 65 50 61 67 65 72 28 70 44 62 2d 3e 70  treePager(pDb->p
29318 42 74 29 3b 0a 20 20 20 20 20 20 65 4d 6f 64 65  Bt);.      eMode
29319 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a   = sqlite3PagerJ
2931a 6f 75 72 6e 61 6c 4d 6f 64 65 28 70 50 61 67 65  ournalMode(pPage
2931b 72 2c 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20 7d  r, eMode);.    }
2931c 0a 20 20 20 20 61 73 73 65 72 74 28 20 65 4d 6f  .    assert( eMo
2931d 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
2931e 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 20 20 20  LMODE_DELETE.   
2931f 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d             || eM
29320 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
29321 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a  ALMODE_TRUNCATE.
29322 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c                ||
29323 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f   eMode==PAGER_JO
29324 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53  URNALMODE_PERSIS
29325 54 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  T.              
29326 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  || eMode==PAGER_
29327 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 0a  JOURNALMODE_OFF.
29328 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c                ||
29329 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f   eMode==PAGER_JO
2932a 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59  URNALMODE_MEMORY
2932b 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   );.    sqlite3V
2932c 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
2932d 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   1);.    sqlite3
2932e 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
2932f 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
29330 45 2c 20 22 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65  E, "journal_mode
29331 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
29332 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
29333 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53  beAddOp4(v, OP_S
29334 74 72 69 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c  tring8, 0, 1, 0,
29335 20 0a 20 20 20 20 20 20 20 20 20 20 20 61 7a 4d   .           azM
29336 6f 64 65 4e 61 6d 65 5b 65 4d 6f 64 65 5d 2c 20  odeName[eMode], 
29337 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
29338 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29339 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f  2(v, OP_ResultRo
2933a 77 2c 20 31 2c 20 31 29 3b 0a 20 20 7d 65 6c 73  w, 1, 1);.  }els
2933b 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52  e..  /*.  **  PR
2933c 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d  AGMA [database.]
2933d 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d  journal_size_lim
2933e 69 74 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20  it.  **  PRAGMA 
2933f 5b 64 61 74 61 62 61 73 65 2e 5d 6a 6f 75 72 6e  [database.]journ
29340 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74 3d 4e 0a  al_size_limit=N.
29341 20 20 2a 2a 0a 20 20 2a 2a 20 47 65 74 20 6f 72    **.  ** Get or
29342 20 73 65 74 20 74 68 65 20 73 69 7a 65 20 6c 69   set the size li
29343 6d 69 74 20 6f 6e 20 72 6f 6c 6c 62 61 63 6b 20  mit on rollback 
29344 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 20  journal files.. 
29345 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
29346 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22  3StrICmp(zLeft,"
29347 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d  journal_size_lim
29348 69 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 50  it")==0 ){.    P
29349 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 73  ager *pPager = s
2934a 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
2934b 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20  (pDb->pBt);.    
2934c 69 36 34 20 69 4c 69 6d 69 74 20 3d 20 2d 32 3b  i64 iLimit = -2;
2934d 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 20  .    if( zRight 
2934e 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2934f 41 74 6f 69 36 34 28 7a 52 69 67 68 74 2c 20 26  Atoi64(zRight, &
29350 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 69  iLimit);.      i
29351 66 28 20 69 4c 69 6d 69 74 3c 2d 31 20 29 20 69  f( iLimit<-1 ) i
29352 4c 69 6d 69 74 20 3d 20 2d 31 3b 0a 20 20 20 20  Limit = -1;.    
29353 7d 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 73  }.    iLimit = s
29354 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e  qlite3PagerJourn
29355 61 6c 53 69 7a 65 4c 69 6d 69 74 28 70 50 61 67  alSizeLimit(pPag
29356 65 72 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20  er, iLimit);.   
29357 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74   returnSingleInt
29358 28 70 50 61 72 73 65 2c 20 22 6a 6f 75 72 6e 61  (pParse, "journa
29359 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74 22 2c 20 69  l_size_limit", i
2935a 4c 69 6d 69 74 29 3b 0a 20 20 7d 65 6c 73 65 0a  Limit);.  }else.
2935b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2935c 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f 50 52 41  E_OMIT_PAGER_PRA
2935d 47 4d 41 53 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20  GMAS */..  /*.  
2935e 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
2935f 62 61 73 65 2e 5d 61 75 74 6f 5f 76 61 63 75 75  base.]auto_vacuu
29360 6d 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b  m.  **  PRAGMA [
29361 64 61 74 61 62 61 73 65 2e 5d 61 75 74 6f 5f 76  database.]auto_v
29362 61 63 75 75 6d 3d 4e 0a 20 20 2a 2a 0a 20 20 2a  acuum=N.  **.  *
29363 2a 20 47 65 74 20 6f 72 20 73 65 74 20 74 68 65  * Get or set the
29364 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64 61   value of the da
29365 74 61 62 61 73 65 20 27 61 75 74 6f 2d 76 61 63  tabase 'auto-vac
29366 75 75 6d 27 20 70 61 72 61 6d 65 74 65 72 2e 0a  uum' parameter..
29367 20 20 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 69    ** The value i
29368 73 20 6f 6e 65 20 6f 66 3a 20 20 30 20 4e 4f 4e  s one of:  0 NON
29369 45 20 31 20 46 55 4c 4c 20 32 20 49 4e 43 52 45  E 1 FULL 2 INCRE
2936a 4d 45 4e 54 41 4c 0a 20 20 2a 2f 0a 23 69 66 6e  MENTAL.  */.#ifn
2936b 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2936c 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28  AUTOVACUUM.  if(
2936d 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
2936e 7a 4c 65 66 74 2c 22 61 75 74 6f 5f 76 61 63 75  zLeft,"auto_vacu
2936f 75 6d 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 42  um")==0 ){.    B
29370 74 72 65 65 20 2a 70 42 74 20 3d 20 70 44 62 2d  tree *pBt = pDb-
29371 3e 70 42 74 3b 0a 20 20 20 20 61 73 73 65 72 74  >pBt;.    assert
29372 28 20 70 42 74 21 3d 30 20 29 3b 0a 20 20 20 20  ( pBt!=0 );.    
29373 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53  if( sqlite3ReadS
29374 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b  chema(pParse) ){
29375 0a 20 20 20 20 20 20 67 6f 74 6f 20 70 72 61 67  .      goto prag
29376 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  ma_out;.    }.  
29377 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b    if( !zRight ){
29378 0a 20 20 20 20 20 20 69 6e 74 20 61 75 74 6f 5f  .      int auto_
29379 76 61 63 75 75 6d 3b 0a 20 20 20 20 20 20 69 66  vacuum;.      if
2937a 28 20 41 4c 57 41 59 53 28 70 42 74 29 20 29 7b  ( ALWAYS(pBt) ){
2937b 0a 20 20 20 20 20 20 20 20 20 61 75 74 6f 5f 76  .         auto_v
2937c 61 63 75 75 6d 20 3d 20 73 71 6c 69 74 65 33 42  acuum = sqlite3B
2937d 74 72 65 65 47 65 74 41 75 74 6f 56 61 63 75 75  treeGetAutoVacuu
2937e 6d 28 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 65  m(pBt);.      }e
2937f 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 61 75  lse{.         au
29380 74 6f 5f 76 61 63 75 75 6d 20 3d 20 53 51 4c 49  to_vacuum = SQLI
29381 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56  TE_DEFAULT_AUTOV
29382 41 43 55 55 4d 3b 0a 20 20 20 20 20 20 7d 0a 20  ACUUM;.      }. 
29383 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c       returnSingl
29384 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22 61 75  eInt(pParse, "au
29385 74 6f 5f 76 61 63 75 75 6d 22 2c 20 61 75 74 6f  to_vacuum", auto
29386 5f 76 61 63 75 75 6d 29 3b 0a 20 20 20 20 7d 65  _vacuum);.    }e
29387 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 65  lse{.      int e
29388 41 75 74 6f 20 3d 20 67 65 74 41 75 74 6f 56 61  Auto = getAutoVa
29389 63 75 75 6d 28 7a 52 69 67 68 74 29 3b 0a 20 20  cuum(zRight);.  
2938a 20 20 20 20 61 73 73 65 72 74 28 20 65 41 75 74      assert( eAut
2938b 6f 3e 3d 30 20 26 26 20 65 41 75 74 6f 3c 3d 32  o>=0 && eAuto<=2
2938c 20 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6e 65   );.      db->ne
2938d 78 74 41 75 74 6f 76 61 63 20 3d 20 28 75 38 29  xtAutovac = (u8)
2938e 65 41 75 74 6f 3b 0a 20 20 20 20 20 20 69 66 28  eAuto;.      if(
2938f 20 41 4c 57 41 59 53 28 65 41 75 74 6f 3e 3d 30   ALWAYS(eAuto>=0
29390 29 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ) ){.        /* 
29391 43 61 6c 6c 20 53 65 74 41 75 74 6f 56 61 63 75  Call SetAutoVacu
29392 75 6d 28 29 20 74 6f 20 73 65 74 20 69 6e 69 74  um() to set init
29393 69 61 6c 69 7a 65 20 74 68 65 20 69 6e 74 65 72  ialize the inter
29394 6e 61 6c 20 61 75 74 6f 20 61 6e 64 0a 20 20 20  nal auto and.   
29395 20 20 20 20 20 2a 2a 20 69 6e 63 72 2d 76 61 63       ** incr-vac
29396 75 75 6d 20 66 6c 61 67 73 2e 20 54 68 69 73 20  uum flags. This 
29397 69 73 20 72 65 71 75 69 72 65 64 20 69 6e 20 63  is required in c
29398 61 73 65 20 74 68 69 73 20 63 6f 6e 6e 65 63 74  ase this connect
29399 69 6f 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 63  ion.        ** c
2939a 72 65 61 74 65 73 20 74 68 65 20 64 61 74 61 62  reates the datab
2939b 61 73 65 20 66 69 6c 65 2e 20 49 74 20 69 73 20  ase file. It is 
2939c 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20 69  important that i
2939d 74 20 69 73 20 63 72 65 61 74 65 64 0a 20 20 20  t is created.   
2939e 20 20 20 20 20 2a 2a 20 61 73 20 61 6e 20 61 75       ** as an au
2939f 74 6f 2d 76 61 63 75 75 6d 20 63 61 70 61 62 6c  to-vacuum capabl
293a0 65 20 64 62 2e 0a 20 20 20 20 20 20 20 20 2a 2f  e db..        */
293a1 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 63 20  .        int rc 
293a2 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65  = sqlite3BtreeSe
293a3 74 41 75 74 6f 56 61 63 75 75 6d 28 70 42 74 2c  tAutoVacuum(pBt,
293a4 20 65 41 75 74 6f 29 3b 0a 20 20 20 20 20 20 20   eAuto);.       
293a5 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
293a6 4f 4b 20 26 26 20 28 65 41 75 74 6f 3d 3d 31 20  OK && (eAuto==1 
293a7 7c 7c 20 65 41 75 74 6f 3d 3d 32 29 20 29 7b 0a  || eAuto==2) ){.
293a8 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65            /* Whe
293a9 6e 20 73 65 74 74 69 6e 67 20 74 68 65 20 61 75  n setting the au
293aa 74 6f 5f 76 61 63 75 75 6d 20 6d 6f 64 65 20 74  to_vacuum mode t
293ab 6f 20 65 69 74 68 65 72 20 22 66 75 6c 6c 22 20  o either "full" 
293ac 6f 72 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  or .          **
293ad 20 22 69 6e 63 72 65 6d 65 6e 74 61 6c 22 2c 20   "incremental", 
293ae 77 72 69 74 65 20 74 68 65 20 76 61 6c 75 65 20  write the value 
293af 6f 66 20 6d 65 74 61 5b 36 5d 20 69 6e 20 74 68  of meta[6] in th
293b0 65 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 20  e database.     
293b1 20 20 20 20 20 2a 2a 20 66 69 6c 65 2e 20 42 65       ** file. Be
293b2 66 6f 72 65 20 77 72 69 74 69 6e 67 20 74 6f 20  fore writing to 
293b3 6d 65 74 61 5b 36 5d 2c 20 63 68 65 63 6b 20 74  meta[6], check t
293b4 68 61 74 20 6d 65 74 61 5b 33 5d 20 69 6e 64 69  hat meta[3] indi
293b5 63 61 74 65 73 0a 20 20 20 20 20 20 20 20 20 20  cates.          
293b6 2a 2a 20 74 68 61 74 20 74 68 69 73 20 72 65 61  ** that this rea
293b7 6c 6c 79 20 69 73 20 61 6e 20 61 75 74 6f 2d 76  lly is an auto-v
293b8 61 63 75 75 6d 20 63 61 70 61 62 6c 65 20 64 61  acuum capable da
293b9 74 61 62 61 73 65 2e 0a 20 20 20 20 20 20 20 20  tabase..        
293ba 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73    */.          s
293bb 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65  tatic const Vdbe
293bc 4f 70 4c 69 73 74 20 73 65 74 4d 65 74 61 36 5b  OpList setMeta6[
293bd 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20  ] = {.          
293be 20 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63 74 69    { OP_Transacti
293bf 6f 6e 2c 20 20 20 20 30 2c 20 20 20 20 20 20 20  on,    0,       
293c0 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20    1,            
293c1 20 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30       0},    /* 0
293c2 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
293c3 7b 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c  { OP_ReadCookie,
293c4 20 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20       0,         
293c5 31 2c 20 20 20 20 20 20 20 20 20 42 54 52 45 45  1,         BTREE
293c6 5f 4c 41 52 47 45 53 54 5f 52 4f 4f 54 5f 50 41  _LARGEST_ROOT_PA
293c7 47 45 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20  GE},.           
293c8 20 7b 20 4f 50 5f 49 66 2c 20 20 20 20 20 20 20   { OP_If,       
293c9 20 20 20 20 20 20 31 2c 20 20 20 20 20 20 20 20        1,        
293ca 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
293cb 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 32 20      0},    /* 2 
293cc 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  */.            {
293cd 20 4f 50 5f 48 61 6c 74 2c 20 20 20 20 20 20 20   OP_Halt,       
293ce 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 4f      SQLITE_OK, O
293cf 45 5f 41 62 6f 72 74 2c 20 20 20 20 20 20 20 20  E_Abort,        
293d0 20 20 30 7d 2c 20 20 20 20 2f 2a 20 33 20 2a 2f    0},    /* 3 */
293d1 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
293d2 50 5f 49 6e 74 65 67 65 72 2c 20 20 20 20 20 20  P_Integer,      
293d3 20 20 30 2c 20 20 20 20 20 20 20 20 20 31 2c 20    0,         1, 
293d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
293d5 30 7d 2c 20 20 20 20 2f 2a 20 34 20 2a 2f 0a 20  0},    /* 4 */. 
293d6 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
293d7 53 65 74 43 6f 6f 6b 69 65 2c 20 20 20 20 20 20  SetCookie,      
293d8 30 2c 20 20 20 20 20 20 20 20 20 42 54 52 45 45  0,         BTREE
293d9 5f 49 4e 43 52 5f 56 41 43 55 55 4d 2c 20 31 7d  _INCR_VACUUM, 1}
293da 2c 20 20 20 20 2f 2a 20 35 20 2a 2f 0a 20 20 20  ,    /* 5 */.   
293db 20 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20 20         };.      
293dc 20 20 20 20 69 6e 74 20 69 41 64 64 72 3b 0a 20      int iAddr;. 
293dd 20 20 20 20 20 20 20 20 20 69 41 64 64 72 20 3d           iAddr =
293de 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
293df 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53 69  pList(v, ArraySi
293e0 7a 65 28 73 65 74 4d 65 74 61 36 29 2c 20 73 65  ze(setMeta6), se
293e1 74 4d 65 74 61 36 29 3b 0a 20 20 20 20 20 20 20  tMeta6);.       
293e2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
293e3 61 6e 67 65 50 31 28 76 2c 20 69 41 64 64 72 2c  angeP1(v, iAddr,
293e4 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 20   iDb);.         
293e5 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
293e6 67 65 50 31 28 76 2c 20 69 41 64 64 72 2b 31 2c  geP1(v, iAddr+1,
293e7 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 20   iDb);.         
293e8 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
293e9 67 65 50 32 28 76 2c 20 69 41 64 64 72 2b 32 2c  geP2(v, iAddr+2,
293ea 20 69 41 64 64 72 2b 34 29 3b 0a 20 20 20 20 20   iAddr+4);.     
293eb 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
293ec 43 68 61 6e 67 65 50 31 28 76 2c 20 69 41 64 64  ChangeP1(v, iAdd
293ed 72 2b 34 2c 20 65 41 75 74 6f 2d 31 29 3b 0a 20  r+4, eAuto-1);. 
293ee 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
293ef 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
293f0 69 41 64 64 72 2b 35 2c 20 69 44 62 29 3b 0a 20  iAddr+5, iDb);. 
293f1 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
293f2 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76 2c  VdbeUsesBtree(v,
293f3 20 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 7d   iDb);.        }
293f4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
293f5 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 20   }else.#endif.. 
293f6 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41   /*.  **  PRAGMA
293f7 20 5b 64 61 74 61 62 61 73 65 2e 5d 69 6e 63 72   [database.]incr
293f8 65 6d 65 6e 74 61 6c 5f 76 61 63 75 75 6d 28 4e  emental_vacuum(N
293f9 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 44 6f 20 4e  ).  **.  ** Do N
293fa 20 73 74 65 70 73 20 6f 66 20 69 6e 63 72 65 6d   steps of increm
293fb 65 6e 74 61 6c 20 76 61 63 75 75 6d 69 6e 67 20  ental vacuuming 
293fc 6f 6e 20 61 20 64 61 74 61 62 61 73 65 2e 0a 20  on a database.. 
293fd 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
293fe 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
293ff 55 4d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  UM.  if( sqlite3
29400 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 69  StrICmp(zLeft,"i
29401 6e 63 72 65 6d 65 6e 74 61 6c 5f 76 61 63 75 75  ncremental_vacuu
29402 6d 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e  m")==0 ){.    in
29403 74 20 69 4c 69 6d 69 74 2c 20 61 64 64 72 3b 0a  t iLimit, addr;.
29404 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52      if( sqlite3R
29405 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73 65  eadSchema(pParse
29406 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ) ){.      goto 
29407 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20  pragma_out;.    
29408 7d 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68 74  }.    if( zRight
29409 3d 3d 30 20 7c 7c 20 21 73 71 6c 69 74 65 33 47  ==0 || !sqlite3G
2940a 65 74 49 6e 74 33 32 28 7a 52 69 67 68 74 2c 20  etInt32(zRight, 
2940b 26 69 4c 69 6d 69 74 29 20 7c 7c 20 69 4c 69 6d  &iLimit) || iLim
2940c 69 74 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  it<=0 ){.      i
2940d 4c 69 6d 69 74 20 3d 20 30 78 37 66 66 66 66 66  Limit = 0x7fffff
2940e 66 66 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  ff;.    }.    sq
2940f 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f  lite3BeginWriteO
29410 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c  peration(pParse,
29411 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71   0, iDb);.    sq
29412 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
29413 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69  v, OP_Integer, i
29414 4c 69 6d 69 74 2c 20 31 29 3b 0a 20 20 20 20 61  Limit, 1);.    a
29415 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
29416 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 6e  eAddOp1(v, OP_In
29417 63 72 56 61 63 75 75 6d 2c 20 69 44 62 29 3b 0a  crVacuum, iDb);.
29418 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29419 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp1(v, OP_Resu
2941a 6c 74 52 6f 77 2c 20 31 29 3b 0a 20 20 20 20 73  ltRow, 1);.    s
2941b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2941c 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 31  (v, OP_AddImm, 1
2941d 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  , -1);.    sqlit
2941e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2941f 4f 50 5f 49 66 50 6f 73 2c 20 31 2c 20 61 64 64  OP_IfPos, 1, add
29420 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  r);.    sqlite3V
29421 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
29422 64 64 72 29 3b 0a 20 20 7d 65 6c 73 65 0a 23 65  ddr);.  }else.#e
29423 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
29424 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
29425 50 52 41 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a  PRAGMAS.  /*.  *
29426 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62  *  PRAGMA [datab
29427 61 73 65 2e 5d 63 61 63 68 65 5f 73 69 7a 65 0a  ase.]cache_size.
29428 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
29429 74 61 62 61 73 65 2e 5d 63 61 63 68 65 5f 73 69  tabase.]cache_si
2942a 7a 65 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  ze=N.  **.  ** T
2942b 68 65 20 66 69 72 73 74 20 66 6f 72 6d 20 72 65  he first form re
2942c 70 6f 72 74 73 20 74 68 65 20 63 75 72 72 65 6e  ports the curren
2942d 74 20 6c 6f 63 61 6c 20 73 65 74 74 69 6e 67 20  t local setting 
2942e 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20 70 61 67  for the.  ** pag
2942f 65 20 63 61 63 68 65 20 73 69 7a 65 2e 20 20 54  e cache size.  T
29430 68 65 20 6c 6f 63 61 6c 20 73 65 74 74 69 6e 67  he local setting
29431 20 63 61 6e 20 62 65 20 64 69 66 66 65 72 65 6e   can be differen
29432 74 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20  t from.  ** the 
29433 70 65 72 73 69 73 74 65 6e 74 20 63 61 63 68 65  persistent cache
29434 20 73 69 7a 65 20 76 61 6c 75 65 20 74 68 61 74   size value that
29435 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68   is stored in th
29436 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20  e database.  ** 
29437 66 69 6c 65 20 69 74 73 65 6c 66 2e 20 20 54 68  file itself.  Th
29438 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
29439 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20   is the maximum 
2943a 6e 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20 70  number of.  ** p
2943b 61 67 65 73 20 69 6e 20 74 68 65 20 70 61 67 65  ages in the page
2943c 20 63 61 63 68 65 2e 20 20 54 68 65 20 73 65 63   cache.  The sec
2943d 6f 6e 64 20 66 6f 72 6d 20 73 65 74 73 20 74 68  ond form sets th
2943e 65 20 6c 6f 63 61 6c 0a 20 20 2a 2a 20 70 61 67  e local.  ** pag
2943f 65 20 63 61 63 68 65 20 73 69 7a 65 20 76 61 6c  e cache size val
29440 75 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74  ue.  It does not
29441 20 63 68 61 6e 67 65 20 74 68 65 20 70 65 72 73   change the pers
29442 69 73 74 65 6e 74 0a 20 20 2a 2a 20 63 61 63 68  istent.  ** cach
29443 65 20 73 69 7a 65 20 73 74 6f 72 65 64 20 6f 6e  e size stored on
29444 20 74 68 65 20 64 69 73 6b 20 73 6f 20 74 68 65   the disk so the
29445 20 63 61 63 68 65 20 73 69 7a 65 20 77 69 6c 6c   cache size will
29446 20 72 65 76 65 72 74 0a 20 20 2a 2a 20 74 6f 20   revert.  ** to 
29447 69 74 73 20 64 65 66 61 75 6c 74 20 76 61 6c 75  its default valu
29448 65 20 77 68 65 6e 20 74 68 65 20 64 61 74 61 62  e when the datab
29449 61 73 65 20 69 73 20 63 6c 6f 73 65 64 20 61 6e  ase is closed an
2944a 64 20 72 65 6f 70 65 6e 65 64 2e 0a 20 20 2a 2a  d reopened..  **
2944b 20 4e 20 73 68 6f 75 6c 64 20 62 65 20 61 20 70   N should be a p
2944c 6f 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 2e  ositive integer.
2944d 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
2944e 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
2944f 2c 22 63 61 63 68 65 5f 73 69 7a 65 22 29 3d 3d  ,"cache_size")==
29450 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  0 ){.    if( sql
29451 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
29452 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72  Parse) ) goto pr
29453 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 69 66  agma_out;.    if
29454 28 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20  ( !zRight ){.   
29455 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49     returnSingleI
29456 6e 74 28 70 50 61 72 73 65 2c 20 22 63 61 63 68  nt(pParse, "cach
29457 65 5f 73 69 7a 65 22 2c 20 70 44 62 2d 3e 70 53  e_size", pDb->pS
29458 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a  chema->cache_siz
29459 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  e);.    }else{. 
2945a 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20 3d 20       int size = 
2945b 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20 20  atoi(zRight);.  
2945c 20 20 20 20 69 66 28 20 73 69 7a 65 3c 30 20 29      if( size<0 )
2945d 20 73 69 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a 20   size = -size;. 
2945e 20 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65 6d       pDb->pSchem
2945f 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 20 3d 20  a->cache_size = 
29460 73 69 7a 65 3b 0a 20 20 20 20 20 20 73 71 6c 69  size;.      sqli
29461 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68 65  te3BtreeSetCache
29462 53 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20 70  Size(pDb->pBt, p
29463 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63  Db->pSchema->cac
29464 68 65 5f 73 69 7a 65 29 3b 0a 20 20 20 20 7d 0a  he_size);.    }.
29465 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20    }else..  /*.  
29466 2a 2a 20 20 20 50 52 41 47 4d 41 20 74 65 6d 70  **   PRAGMA temp
29467 5f 73 74 6f 72 65 0a 20 20 2a 2a 20 20 20 50 52  _store.  **   PR
29468 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 20  AGMA temp_store 
29469 3d 20 22 64 65 66 61 75 6c 74 22 7c 22 6d 65 6d  = "default"|"mem
2946a 6f 72 79 22 7c 22 66 69 6c 65 22 0a 20 20 2a 2a  ory"|"file".  **
2946b 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20  .  ** Return or 
2946c 73 65 74 20 74 68 65 20 6c 6f 63 61 6c 20 76 61  set the local va
2946d 6c 75 65 20 6f 66 20 74 68 65 20 74 65 6d 70 5f  lue of the temp_
2946e 73 74 6f 72 65 20 66 6c 61 67 2e 20 20 43 68 61  store flag.  Cha
2946f 6e 67 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20 6c  nging.  ** the l
29470 6f 63 61 6c 20 76 61 6c 75 65 20 64 6f 65 73 20  ocal value does 
29471 6e 6f 74 20 6d 61 6b 65 20 63 68 61 6e 67 65 73  not make changes
29472 20 74 6f 20 74 68 65 20 64 69 73 6b 20 66 69 6c   to the disk fil
29473 65 20 61 6e 64 20 74 68 65 20 64 65 66 61 75 6c  e and the defaul
29474 74 0a 20 20 2a 2a 20 76 61 6c 75 65 20 77 69 6c  t.  ** value wil
29475 6c 20 62 65 20 72 65 73 74 6f 72 65 64 20 74 68  l be restored th
29476 65 20 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20  e next time the 
29477 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e  database is open
29478 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f  ed..  **.  ** No
29479 74 65 20 74 68 61 74 20 69 74 20 69 73 20 70 6f  te that it is po
2947a 73 73 69 62 6c 65 20 66 6f 72 20 74 68 65 20 6c  ssible for the l
2947b 69 62 72 61 72 79 20 63 6f 6d 70 69 6c 65 2d 74  ibrary compile-t
2947c 69 6d 65 20 6f 70 74 69 6f 6e 73 20 74 6f 0a 20  ime options to. 
2947d 20 2a 2a 20 6f 76 65 72 72 69 64 65 20 74 68 69   ** override thi
2947e 73 20 73 65 74 74 69 6e 67 0a 20 20 2a 2f 0a 20  s setting.  */. 
2947f 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
29480 43 6d 70 28 7a 4c 65 66 74 2c 20 22 74 65 6d 70  Cmp(zLeft, "temp
29481 5f 73 74 6f 72 65 22 29 3d 3d 30 20 29 7b 0a 20  _store")==0 ){. 
29482 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29     if( !zRight )
29483 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 53 69  {.      returnSi
29484 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20  ngleInt(pParse, 
29485 22 74 65 6d 70 5f 73 74 6f 72 65 22 2c 20 64 62  "temp_store", db
29486 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 29 3b 0a 20  ->temp_store);. 
29487 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
29488 63 68 61 6e 67 65 54 65 6d 70 53 74 6f 72 61 67  changeTempStorag
29489 65 28 70 50 61 72 73 65 2c 20 7a 52 69 67 68 74  e(pParse, zRight
2948a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
2948b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52  ..  /*.  **   PR
2948c 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f  AGMA temp_store_
2948d 64 69 72 65 63 74 6f 72 79 0a 20 20 2a 2a 20 20  directory.  **  
2948e 20 50 52 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f   PRAGMA temp_sto
2948f 72 65 5f 64 69 72 65 63 74 6f 72 79 20 3d 20 22  re_directory = "
29490 22 7c 22 64 69 72 65 63 74 6f 72 79 5f 6e 61 6d  "|"directory_nam
29491 65 22 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74  e".  **.  ** Ret
29492 75 72 6e 20 6f 72 20 73 65 74 20 74 68 65 20 6c  urn or set the l
29493 6f 63 61 6c 20 76 61 6c 75 65 20 6f 66 20 74 68  ocal value of th
29494 65 20 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72  e temp_store_dir
29495 65 63 74 6f 72 79 20 66 6c 61 67 2e 20 20 43 68  ectory flag.  Ch
29496 61 6e 67 69 6e 67 0a 20 20 2a 2a 20 74 68 65 20  anging.  ** the 
29497 76 61 6c 75 65 20 73 65 74 73 20 61 20 73 70 65  value sets a spe
29498 63 69 66 69 63 20 64 69 72 65 63 74 6f 72 79 20  cific directory 
29499 74 6f 20 62 65 20 75 73 65 64 20 66 6f 72 20 74  to be used for t
2949a 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73 2e 0a  emporary files..
2949b 20 20 2a 2a 20 53 65 74 74 69 6e 67 20 74 6f 20    ** Setting to 
2949c 61 20 6e 75 6c 6c 20 73 74 72 69 6e 67 20 72 65  a null string re
2949d 76 65 72 74 73 20 74 6f 20 74 68 65 20 64 65 66  verts to the def
2949e 61 75 6c 74 20 74 65 6d 70 6f 72 61 72 79 20 64  ault temporary d
2949f 69 72 65 63 74 6f 72 79 20 73 65 61 72 63 68 2e  irectory search.
294a0 0a 20 20 2a 2a 20 49 66 20 74 65 6d 70 6f 72 61  .  ** If tempora
294a1 72 79 20 64 69 72 65 63 74 6f 72 79 20 69 73 20  ry directory is 
294a2 63 68 61 6e 67 65 64 2c 20 74 68 65 6e 20 69 6e  changed, then in
294a3 76 61 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72  validateTempStor
294a4 61 67 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2f 0a 20  age..  **.  */. 
294a5 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
294a6 43 6d 70 28 7a 4c 65 66 74 2c 20 22 74 65 6d 70  Cmp(zLeft, "temp
294a7 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79  _store_directory
294a8 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  ")==0 ){.    if(
294a9 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20   !zRight ){.    
294aa 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74 65    if( sqlite3_te
294ab 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 29 7b 0a  mp_directory ){.
294ac 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
294ad 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
294ae 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   1);.        sql
294af 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
294b0 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45  me(v, 0, COLNAME
294b1 5f 4e 41 4d 45 2c 20 0a 20 20 20 20 20 20 20 20  _NAME, .        
294b2 20 20 20 20 22 74 65 6d 70 5f 73 74 6f 72 65 5f      "temp_store_
294b3 64 69 72 65 63 74 6f 72 79 22 2c 20 53 51 4c 49  directory", SQLI
294b4 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
294b5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
294b6 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
294b7 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20 73 71  ng8, 0, 1, 0, sq
294b8 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63  lite3_temp_direc
294b9 74 6f 72 79 2c 20 30 29 3b 0a 20 20 20 20 20 20  tory, 0);.      
294ba 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
294bb 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
294bc 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20 20 20  Row, 1, 1);.    
294bd 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 23    }.    }else{.#
294be 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
294bf 49 54 5f 57 53 44 0a 20 20 20 20 20 20 69 66 28  IT_WSD.      if(
294c0 20 7a 52 69 67 68 74 5b 30 5d 20 29 7b 0a 20 20   zRight[0] ){.  
294c1 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20        int rc;.  
294c2 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20        int res;. 
294c3 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
294c4 74 65 33 4f 73 41 63 63 65 73 73 28 64 62 2d 3e  te3OsAccess(db->
294c5 70 56 66 73 2c 20 7a 52 69 67 68 74 2c 20 53 51  pVfs, zRight, SQ
294c6 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
294c7 57 52 49 54 45 2c 20 26 72 65 73 29 3b 0a 20 20  WRITE, &res);.  
294c8 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
294c9 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 65 73 3d 3d  LITE_OK || res==
294ca 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  0 ){.          s
294cb 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
294cc 50 61 72 73 65 2c 20 22 6e 6f 74 20 61 20 77 72  Parse, "not a wr
294cd 69 74 61 62 6c 65 20 64 69 72 65 63 74 6f 72 79  itable directory
294ce 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  ");.          go
294cf 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20  to pragma_out;. 
294d0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
294d1 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c 49 54  .      if( SQLIT
294d2 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 30 0a  E_TEMP_STORE==0.
294d3 20 20 20 20 20 20 20 7c 7c 20 28 53 51 4c 49 54         || (SQLIT
294d4 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 31 20  E_TEMP_STORE==1 
294d5 26 26 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72  && db->temp_stor
294d6 65 3c 3d 31 29 0a 20 20 20 20 20 20 20 7c 7c 20  e<=1).       || 
294d7 28 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f  (SQLITE_TEMP_STO
294d8 52 45 3d 3d 32 20 26 26 20 64 62 2d 3e 74 65 6d  RE==2 && db->tem
294d9 70 5f 73 74 6f 72 65 3d 3d 31 29 0a 20 20 20 20  p_store==1).    
294da 20 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 76    ){.        inv
294db 61 6c 69 64 61 74 65 54 65 6d 70 53 74 6f 72 61  alidateTempStora
294dc 67 65 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  ge(pParse);.    
294dd 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
294de 33 5f 66 72 65 65 28 73 71 6c 69 74 65 33 5f 74  3_free(sqlite3_t
294df 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 29 3b 0a  emp_directory);.
294e0 20 20 20 20 20 20 69 66 28 20 7a 52 69 67 68 74        if( zRight
294e1 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 73  [0] ){.        s
294e2 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65  qlite3_temp_dire
294e3 63 74 6f 72 79 20 3d 20 73 71 6c 69 74 65 33 44  ctory = sqlite3D
294e4 62 53 74 72 44 75 70 28 30 2c 20 7a 52 69 67 68  bStrDup(0, zRigh
294e5 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  t);.      }else{
294e6 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
294e7 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20  _temp_directory 
294e8 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  = 0;.      }.#en
294e9 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
294ea 49 54 5f 57 53 44 20 2a 2f 0a 20 20 20 20 7d 0a  IT_WSD */.    }.
294eb 20 20 7d 65 6c 73 65 0a 0a 23 69 66 20 21 64 65    }else..#if !de
294ec 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
294ed 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
294ee 45 29 0a 23 20 20 69 66 20 64 65 66 69 6e 65 64  E).#  if defined
294ef 28 5f 5f 41 50 50 4c 45 5f 5f 29 0a 23 20 20 20  (__APPLE__).#   
294f0 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45   define SQLITE_E
294f1 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54  NABLE_LOCKING_ST
294f2 59 4c 45 20 31 0a 23 20 20 65 6c 73 65 0a 23 20  YLE 1.#  else.# 
294f3 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45     define SQLITE
294f4 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
294f5 53 54 59 4c 45 20 30 0a 23 20 20 65 6e 64 69 66  STYLE 0.#  endif
294f6 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
294f7 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e  TE_ENABLE_LOCKIN
294f8 47 5f 53 54 59 4c 45 0a 20 20 2f 2a 0a 20 20 20  G_STYLE.  /*.   
294f9 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64 61 74  **   PRAGMA [dat
294fa 61 62 61 73 65 2e 5d 6c 6f 63 6b 5f 70 72 6f 78  abase.]lock_prox
294fb 79 5f 66 69 6c 65 0a 20 20 20 2a 2a 20 20 20 50  y_file.   **   P
294fc 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
294fd 5d 6c 6f 63 6b 5f 70 72 6f 78 79 5f 66 69 6c 65  ]lock_proxy_file
294fe 20 3d 20 22 3a 61 75 74 6f 3a 22 7c 22 6c 6f 63   = ":auto:"|"loc
294ff 6b 5f 66 69 6c 65 5f 70 61 74 68 22 0a 20 20 20  k_file_path".   
29500 2a 2a 0a 20 20 20 2a 2a 20 52 65 74 75 72 6e 20  **.   ** Return 
29501 6f 72 20 73 65 74 20 74 68 65 20 76 61 6c 75 65  or set the value
29502 20 6f 66 20 74 68 65 20 6c 6f 63 6b 5f 70 72 6f   of the lock_pro
29503 78 79 5f 66 69 6c 65 20 66 6c 61 67 2e 20 20 43  xy_file flag.  C
29504 68 61 6e 67 69 6e 67 0a 20 20 20 2a 2a 20 74 68  hanging.   ** th
29505 65 20 76 61 6c 75 65 20 73 65 74 73 20 61 20 73  e value sets a s
29506 70 65 63 69 66 69 63 20 66 69 6c 65 20 74 6f 20  pecific file to 
29507 62 65 20 75 73 65 64 20 66 6f 72 20 64 61 74 61  be used for data
29508 62 61 73 65 20 61 63 63 65 73 73 20 6c 6f 63 6b  base access lock
29509 73 2e 0a 20 20 20 2a 2a 0a 20 20 20 2a 2f 0a 20  s..   **.   */. 
2950a 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
2950b 43 6d 70 28 7a 4c 65 66 74 2c 20 22 6c 6f 63 6b  Cmp(zLeft, "lock
2950c 5f 70 72 6f 78 79 5f 66 69 6c 65 22 29 3d 3d 30  _proxy_file")==0
2950d 20 29 7b 0a 20 20 20 20 69 66 28 20 21 7a 52 69   ){.    if( !zRi
2950e 67 68 74 20 29 7b 0a 20 20 20 20 20 20 50 61 67  ght ){.      Pag
2950f 65 72 20 2a 70 50 61 67 65 72 20 3d 20 73 71 6c  er *pPager = sql
29510 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28 70  ite3BtreePager(p
29511 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20 20  Db->pBt);.      
29512 63 68 61 72 20 2a 70 72 6f 78 79 5f 66 69 6c 65  char *proxy_file
29513 5f 70 61 74 68 20 3d 20 4e 55 4c 4c 3b 0a 20 20  _path = NULL;.  
29514 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65      sqlite3_file
29515 20 2a 70 46 69 6c 65 20 3d 20 73 71 6c 69 74 65   *pFile = sqlite
29516 33 50 61 67 65 72 46 69 6c 65 28 70 50 61 67 65  3PagerFile(pPage
29517 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  r);.      sqlite
29518 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 70  3OsFileControl(p
29519 46 69 6c 65 2c 20 53 51 4c 49 54 45 5f 47 45 54  File, SQLITE_GET
2951a 5f 4c 4f 43 4b 50 52 4f 58 59 46 49 4c 45 2c 20  _LOCKPROXYFILE, 
2951b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2951c 20 20 20 20 20 20 20 20 20 20 20 20 26 70 72 6f              &pro
2951d 78 79 5f 66 69 6c 65 5f 70 61 74 68 29 3b 0a 20  xy_file_path);. 
2951e 20 20 20 20 20 0a 20 20 20 20 20 20 69 66 28 20       .      if( 
2951f 70 72 6f 78 79 5f 66 69 6c 65 5f 70 61 74 68 20  proxy_file_path 
29520 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
29521 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
29522 28 76 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20  (v, 1);.        
29523 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
29524 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
29525 41 4d 45 5f 4e 41 4d 45 2c 20 0a 20 20 20 20 20  AME_NAME, .     
29526 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29527 20 20 20 20 20 20 20 20 20 22 6c 6f 63 6b 5f 70           "lock_p
29528 72 6f 78 79 5f 66 69 6c 65 22 2c 20 53 51 4c 49  roxy_file", SQLI
29529 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
2952a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2952b 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
2952c 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20 70 72  ng8, 0, 1, 0, pr
2952d 6f 78 79 5f 66 69 6c 65 5f 70 61 74 68 2c 20 30  oxy_file_path, 0
2952e 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2952f 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
29530 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c  OP_ResultRow, 1,
29531 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   1);.      }.   
29532 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 50 61   }else{.      Pa
29533 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 73 71  ger *pPager = sq
29534 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
29535 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20  pDb->pBt);.     
29536 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
29537 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33 50 61  File = sqlite3Pa
29538 67 65 72 46 69 6c 65 28 70 50 61 67 65 72 29 3b  gerFile(pPager);
29539 0a 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b 0a  .      int res;.
2953a 20 20 20 20 20 20 69 66 28 20 7a 52 69 67 68 74        if( zRight
2953b 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 72  [0] ){.        r
2953c 65 73 3d 73 71 6c 69 74 65 33 4f 73 46 69 6c 65  es=sqlite3OsFile
2953d 43 6f 6e 74 72 6f 6c 28 70 46 69 6c 65 2c 20 53  Control(pFile, S
2953e 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52  QLITE_SET_LOCKPR
2953f 4f 58 59 46 49 4c 45 2c 20 0a 20 20 20 20 20 20  OXYFILE, .      
29540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29541 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7a                 z
29542 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 7d 20  Right);.      } 
29543 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 72  else {.        r
29544 65 73 3d 73 71 6c 69 74 65 33 4f 73 46 69 6c 65  es=sqlite3OsFile
29545 43 6f 6e 74 72 6f 6c 28 70 46 69 6c 65 2c 20 53  Control(pFile, S
29546 51 4c 49 54 45 5f 53 45 54 5f 4c 4f 43 4b 50 52  QLITE_SET_LOCKPR
29547 4f 58 59 46 49 4c 45 2c 20 0a 20 20 20 20 20 20  OXYFILE, .      
29548 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29549 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e                 N
2954a 55 4c 4c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ULL);.      }.  
2954b 20 20 20 20 69 66 28 20 72 65 73 21 3d 53 51 4c      if( res!=SQL
2954c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
2954d 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2954e 67 28 70 50 61 72 73 65 2c 20 22 66 61 69 6c 65  g(pParse, "faile
2954f 64 20 74 6f 20 73 65 74 20 6c 6f 63 6b 20 70 72  d to set lock pr
29550 6f 78 79 20 66 69 6c 65 22 29 3b 0a 20 20 20 20  oxy file");.    
29551 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f      goto pragma_
29552 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  out;.      }.   
29553 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69   }.  }else.#endi
29554 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42  f /* SQLITE_ENAB
29555 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
29556 20 2a 2f 20 20 20 20 20 20 0a 20 20 20 20 0a 20   */      .    . 
29557 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d   /*.  **   PRAGM
29558 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 73 79 6e  A [database.]syn
29559 63 68 72 6f 6e 6f 75 73 0a 20 20 2a 2a 20 20 20  chronous.  **   
2955a 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
2955b 2e 5d 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 4f 46  .]synchronous=OF
2955c 46 7c 4f 4e 7c 4e 4f 52 4d 41 4c 7c 46 55 4c 4c  F|ON|NORMAL|FULL
2955d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72  .  **.  ** Retur
2955e 6e 20 6f 72 20 73 65 74 20 74 68 65 20 6c 6f 63  n or set the loc
2955f 61 6c 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  al value of the 
29560 73 79 6e 63 68 72 6f 6e 6f 75 73 20 66 6c 61 67  synchronous flag
29561 2e 20 20 43 68 61 6e 67 69 6e 67 0a 20 20 2a 2a  .  Changing.  **
29562 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65   the local value
29563 20 64 6f 65 73 20 6e 6f 74 20 6d 61 6b 65 20 63   does not make c
29564 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 64 69  hanges to the di
29565 73 6b 20 66 69 6c 65 20 61 6e 64 20 74 68 65 0a  sk file and the.
29566 20 20 2a 2a 20 64 65 66 61 75 6c 74 20 76 61 6c    ** default val
29567 75 65 20 77 69 6c 6c 20 62 65 20 72 65 73 74 6f  ue will be resto
29568 72 65 64 20 74 68 65 20 6e 65 78 74 20 74 69 6d  red the next tim
29569 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  e the database i
2956a 73 0a 20 20 2a 2a 20 6f 70 65 6e 65 64 2e 0a 20  s.  ** opened.. 
2956b 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
2956c 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22  3StrICmp(zLeft,"
2956d 73 79 6e 63 68 72 6f 6e 6f 75 73 22 29 3d 3d 30  synchronous")==0
2956e 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   ){.    if( sqli
2956f 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
29570 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61  arse) ) goto pra
29571 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 69 66 28  gma_out;.    if(
29572 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20   !zRight ){.    
29573 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e    returnSingleIn
29574 74 28 70 50 61 72 73 65 2c 20 22 73 79 6e 63 68  t(pParse, "synch
29575 72 6f 6e 6f 75 73 22 2c 20 70 44 62 2d 3e 73 61  ronous", pDb->sa
29576 66 65 74 79 5f 6c 65 76 65 6c 2d 31 29 3b 0a 20  fety_level-1);. 
29577 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
29578 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d  if( !db->autoCom
29579 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  mit ){.        s
2957a 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2957b 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20  Parse, .        
2957c 20 20 20 20 22 53 61 66 65 74 79 20 6c 65 76 65      "Safety leve
2957d 6c 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 68 61  l may not be cha
2957e 6e 67 65 64 20 69 6e 73 69 64 65 20 61 20 74 72  nged inside a tr
2957f 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20  ansaction");.   
29580 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
29581 20 20 70 44 62 2d 3e 73 61 66 65 74 79 5f 6c 65    pDb->safety_le
29582 76 65 6c 20 3d 20 67 65 74 53 61 66 65 74 79 4c  vel = getSafetyL
29583 65 76 65 6c 28 7a 52 69 67 68 74 29 2b 31 3b 0a  evel(zRight)+1;.
29584 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
29585 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20  }else.#endif /* 
29586 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45  SQLITE_OMIT_PAGE
29587 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69  R_PRAGMAS */..#i
29588 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
29589 54 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53 0a 20  T_FLAG_PRAGMAS. 
2958a 20 69 66 28 20 66 6c 61 67 50 72 61 67 6d 61 28   if( flagPragma(
2958b 70 50 61 72 73 65 2c 20 7a 4c 65 66 74 2c 20 7a  pParse, zLeft, z
2958c 52 69 67 68 74 29 20 29 7b 0a 20 20 20 20 2f 2a  Right) ){.    /*
2958d 20 54 68 65 20 66 6c 61 67 50 72 61 67 6d 61 28   The flagPragma(
2958e 29 20 73 75 62 72 6f 75 74 69 6e 65 20 61 6c 73  ) subroutine als
2958f 6f 20 67 65 6e 65 72 61 74 65 73 20 61 6e 79 20  o generates any 
29590 6e 65 63 65 73 73 61 72 79 20 63 6f 64 65 0a 20  necessary code. 
29591 20 20 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e     ** there is n
29592 6f 74 68 69 6e 67 20 6d 6f 72 65 20 74 6f 20 64  othing more to d
29593 6f 20 68 65 72 65 20 2a 2f 0a 20 20 7d 65 6c 73  o here */.  }els
29594 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  e.#endif /* SQLI
29595 54 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41  TE_OMIT_FLAG_PRA
29596 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  GMAS */..#ifndef
29597 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48   SQLITE_OMIT_SCH
29598 45 4d 41 5f 50 52 41 47 4d 41 53 0a 20 20 2f 2a  EMA_PRAGMAS.  /*
29599 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 74  .  **   PRAGMA t
2959a 61 62 6c 65 5f 69 6e 66 6f 28 3c 74 61 62 6c 65  able_info(<table
2959b 3e 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74  >).  **.  ** Ret
2959c 75 72 6e 20 61 20 73 69 6e 67 6c 65 20 72 6f 77  urn a single row
2959d 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e   for each column
2959e 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 74 61   of the named ta
2959f 62 6c 65 2e 20 54 68 65 20 63 6f 6c 75 6d 6e 73  ble. The columns
295a0 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 72 65 74   of.  ** the ret
295a1 75 72 6e 65 64 20 64 61 74 61 20 73 65 74 20 61  urned data set a
295a2 72 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 63 69  re:.  **.  ** ci
295a3 64 3a 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e  d:        Column
295a4 20 69 64 20 28 6e 75 6d 62 65 72 65 64 20 66 72   id (numbered fr
295a5 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 68 74  om left to right
295a6 2c 20 73 74 61 72 74 69 6e 67 20 61 74 20 30 29  , starting at 0)
295a7 0a 20 20 2a 2a 20 6e 61 6d 65 3a 20 20 20 20 20  .  ** name:     
295a8 20 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 0a 20 20    Column name.  
295a9 2a 2a 20 74 79 70 65 3a 20 20 20 20 20 20 20 43  ** type:       C
295aa 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f  olumn declaratio
295ab 6e 20 74 79 70 65 2e 0a 20 20 2a 2a 20 6e 6f 74  n type..  ** not
295ac 6e 75 6c 6c 3a 20 20 20 20 54 72 75 65 20 69 66  null:    True if
295ad 20 27 4e 4f 54 20 4e 55 4c 4c 27 20 69 73 20 70   'NOT NULL' is p
295ae 61 72 74 20 6f 66 20 63 6f 6c 75 6d 6e 20 64 65  art of column de
295af 63 6c 61 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 64  claration.  ** d
295b0 66 6c 74 5f 76 61 6c 75 65 3a 20 54 68 65 20 64  flt_value: The d
295b1 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72  efault value for
295b2 20 74 68 65 20 63 6f 6c 75 6d 6e 2c 20 69 66 20   the column, if 
295b3 61 6e 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  any..  */.  if( 
295b4 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
295b5 4c 65 66 74 2c 20 22 74 61 62 6c 65 5f 69 6e 66  Left, "table_inf
295b6 6f 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74  o")==0 && zRight
295b7 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70   ){.    Table *p
295b8 54 61 62 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  Tab;.    if( sql
295b9 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
295ba 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72  Parse) ) goto pr
295bb 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 54  agma_out;.    pT
295bc 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ab = sqlite3Find
295bd 54 61 62 6c 65 28 64 62 2c 20 7a 52 69 67 68 74  Table(db, zRight
295be 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20  , zDb);.    if( 
295bf 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 69 6e  pTab ){.      in
295c0 74 20 69 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  t i;.      int n
295c1 48 69 64 64 65 6e 20 3d 20 30 3b 0a 20 20 20 20  Hidden = 0;.    
295c2 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a    Column *pCol;.
295c3 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
295c4 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 36  eSetNumCols(v, 6
295c5 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  );.      pParse-
295c6 3e 6e 4d 65 6d 20 3d 20 36 3b 0a 20 20 20 20 20  >nMem = 6;.     
295c7 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
295c8 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
295c9 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 69 64 22  NAME_NAME, "cid"
295ca 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
295cb 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
295cc 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
295cd 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   1, COLNAME_NAME
295ce 2c 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45  , "name", SQLITE
295cf 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
295d0 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
295d1 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e  lName(v, 2, COLN
295d2 41 4d 45 5f 4e 41 4d 45 2c 20 22 74 79 70 65 22  AME_NAME, "type"
295d3 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
295d4 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
295d5 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
295d6 20 33 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   3, COLNAME_NAME
295d7 2c 20 22 6e 6f 74 6e 75 6c 6c 22 2c 20 53 51 4c  , "notnull", SQL
295d8 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
295d9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
295da 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 34 2c 20 43  tColName(v, 4, C
295db 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 64 66  OLNAME_NAME, "df
295dc 6c 74 5f 76 61 6c 75 65 22 2c 20 53 51 4c 49 54  lt_value", SQLIT
295dd 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
295de 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
295df 6f 6c 4e 61 6d 65 28 76 2c 20 35 2c 20 43 4f 4c  olName(v, 5, COL
295e0 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 6b 22 2c  NAME_NAME, "pk",
295e1 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
295e2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 69  .      sqlite3Vi
295e3 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73  ewGetColumnNames
295e4 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a  (pParse, pTab);.
295e5 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70        for(i=0, p
295e6 43 6f 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b 20  Col=pTab->aCol; 
295e7 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b  i<pTab->nCol; i+
295e8 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20  +, pCol++){.    
295e9 20 20 20 20 69 66 28 20 49 73 48 69 64 64 65 6e      if( IsHidden
295ea 43 6f 6c 75 6d 6e 28 70 43 6f 6c 29 20 29 7b 0a  Column(pCol) ){.
295eb 20 20 20 20 20 20 20 20 20 20 6e 48 69 64 64 65            nHidde
295ec 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 63  n++;.          c
295ed 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
295ee 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74   }.        sqlit
295ef 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
295f0 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2d 6e 48  OP_Integer, i-nH
295f1 69 64 64 65 6e 2c 20 31 29 3b 0a 20 20 20 20 20  idden, 1);.     
295f2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
295f3 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
295f4 67 38 2c 20 30 2c 20 32 2c 20 30 2c 20 70 43 6f  g8, 0, 2, 0, pCo
295f5 6c 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  l->zName, 0);.  
295f6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
295f7 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
295f8 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c 0a  ring8, 0, 3, 0,.
295f9 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 2d             pCol-
295fa 3e 7a 54 79 70 65 20 3f 20 70 43 6f 6c 2d 3e 7a  >zType ? pCol->z
295fb 54 79 70 65 20 3a 20 22 22 2c 20 30 29 3b 0a 20  Type : "", 0);. 
295fc 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
295fd 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
295fe 6e 74 65 67 65 72 2c 20 28 70 43 6f 6c 2d 3e 6e  nteger, (pCol->n
295ff 6f 74 4e 75 6c 6c 20 3f 20 31 20 3a 20 30 29 2c  otNull ? 1 : 0),
29600 20 34 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   4);.        if(
29601 20 70 43 6f 6c 2d 3e 7a 44 66 6c 74 20 29 7b 0a   pCol->zDflt ){.
29602 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
29603 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
29604 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 35 2c  P_String8, 0, 5,
29605 20 30 2c 20 28 63 68 61 72 2a 29 70 43 6f 6c 2d   0, (char*)pCol-
29606 3e 7a 44 66 6c 74 2c 20 30 29 3b 0a 20 20 20 20  >zDflt, 0);.    
29607 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
29608 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29609 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
2960a 6c 2c 20 30 2c 20 35 29 3b 0a 20 20 20 20 20 20  l, 0, 5);.      
2960b 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69    }.        sqli
2960c 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2960d 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70 43 6f   OP_Integer, pCo
2960e 6c 2d 3e 69 73 50 72 69 6d 4b 65 79 2c 20 36 29  l->isPrimKey, 6)
2960f 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
29610 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29611 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
29612 36 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  6);.      }.    
29613 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20 69 66 28  }.  }else..  if(
29614 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
29615 7a 4c 65 66 74 2c 20 22 69 6e 64 65 78 5f 69 6e  zLeft, "index_in
29616 66 6f 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68  fo")==0 && zRigh
29617 74 20 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a  t ){.    Index *
29618 70 49 64 78 3b 0a 20 20 20 20 54 61 62 6c 65 20  pIdx;.    Table 
29619 2a 70 54 61 62 3b 0a 20 20 20 20 69 66 28 20 73  *pTab;.    if( s
2961a 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
2961b 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20  (pParse) ) goto 
2961c 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20  pragma_out;.    
2961d 70 49 64 78 20 3d 20 73 71 6c 69 74 65 33 46 69  pIdx = sqlite3Fi
2961e 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a 52 69 67  ndIndex(db, zRig
2961f 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66  ht, zDb);.    if
29620 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20  ( pIdx ){.      
29621 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 70 54 61  int i;.      pTa
29622 62 20 3d 20 70 49 64 78 2d 3e 70 54 61 62 6c 65  b = pIdx->pTable
29623 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
29624 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
29625 20 33 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73   3);.      pPars
29626 65 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20  e->nMem = 3;.   
29627 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
29628 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
29629 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65  OLNAME_NAME, "se
2962a 71 6e 6f 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  qno", SQLITE_STA
2962b 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  TIC);.      sqli
2962c 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
2962d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 1, COLNAME_
2962e 4e 41 4d 45 2c 20 22 63 69 64 22 2c 20 53 51 4c  NAME, "cid", SQL
2962f 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
29630 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
29631 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43  tColName(v, 2, C
29632 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61  OLNAME_NAME, "na
29633 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  me", SQLITE_STAT
29634 49 43 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  IC);.      for(i
29635 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c  =0; i<pIdx->nCol
29636 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  umn; i++){.     
29637 20 20 20 69 6e 74 20 63 6e 75 6d 20 3d 20 70 49     int cnum = pI
29638 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b  dx->aiColumn[i];
29639 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2963a 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2963b 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20 31 29 3b  _Integer, i, 1);
2963c 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2963d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2963e 5f 49 6e 74 65 67 65 72 2c 20 63 6e 75 6d 2c 20  _Integer, cnum, 
2963f 32 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  2);.        asse
29640 72 74 28 20 70 54 61 62 2d 3e 6e 43 6f 6c 3e 63  rt( pTab->nCol>c
29641 6e 75 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 73  num );.        s
29642 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
29643 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
29644 30 2c 20 33 2c 20 30 2c 20 70 54 61 62 2d 3e 61  0, 3, 0, pTab->a
29645 43 6f 6c 5b 63 6e 75 6d 5d 2e 7a 4e 61 6d 65 2c  Col[cnum].zName,
29646 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   0);.        sql
29647 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
29648 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  , OP_ResultRow, 
29649 31 2c 20 33 29 3b 0a 20 20 20 20 20 20 7d 0a 20  1, 3);.      }. 
2964a 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20     }.  }else..  
2964b 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
2964c 6d 70 28 7a 4c 65 66 74 2c 20 22 69 6e 64 65 78  mp(zLeft, "index
2964d 5f 6c 69 73 74 22 29 3d 3d 30 20 26 26 20 7a 52  _list")==0 && zR
2964e 69 67 68 74 20 29 7b 0a 20 20 20 20 49 6e 64 65  ight ){.    Inde
2964f 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 54 61 62  x *pIdx;.    Tab
29650 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66  le *pTab;.    if
29651 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  ( sqlite3ReadSch
29652 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f  ema(pParse) ) go
29653 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20  to pragma_out;. 
29654 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65     pTab = sqlite
29655 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a  3FindTable(db, z
29656 52 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20  Right, zDb);.   
29657 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20   if( pTab ){.   
29658 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65     v = sqlite3Ge
29659 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
2965a 20 20 20 20 20 70 49 64 78 20 3d 20 70 54 61 62       pIdx = pTab
2965b 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 20 20  ->pIndex;.      
2965c 69 66 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20  if( pIdx ){.    
2965d 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 20 0a      int i = 0; .
2965e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2965f 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
29660 20 33 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61   3);.        pPa
29661 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20  rse->nMem = 3;. 
29662 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
29663 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
29664 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
29665 20 22 73 65 71 22 2c 20 53 51 4c 49 54 45 5f 53   "seq", SQLITE_S
29666 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
29667 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
29668 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e  lName(v, 1, COLN
29669 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61 6d 65 22  AME_NAME, "name"
2966a 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
2966b 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2966c 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
2966d 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 2, COLNAME_NA
2966e 4d 45 2c 20 22 75 6e 69 71 75 65 22 2c 20 53 51  ME, "unique", SQ
2966f 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
29670 20 20 20 20 20 20 77 68 69 6c 65 28 70 49 64 78        while(pIdx
29671 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
29672 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
29673 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c  , OP_Integer, i,
29674 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73   1);.          s
29675 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
29676 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
29677 30 2c 20 32 2c 20 30 2c 20 70 49 64 78 2d 3e 7a  0, 2, 0, pIdx->z
29678 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Name, 0);.      
29679 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2967a 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
2967b 67 65 72 2c 20 70 49 64 78 2d 3e 6f 6e 45 72 72  ger, pIdx->onErr
2967c 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 2c 20 33 29 3b  or!=OE_None, 3);
2967d 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2967e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2967f 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c  OP_ResultRow, 1,
29680 20 33 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2b   3);.          +
29681 2b 69 3b 0a 20 20 20 20 20 20 20 20 20 20 70 49  +i;.          pI
29682 64 78 20 3d 20 70 49 64 78 2d 3e 70 4e 65 78 74  dx = pIdx->pNext
29683 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
29684 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73    }.    }.  }els
29685 65 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  e..  if( sqlite3
29686 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22  StrICmp(zLeft, "
29687 64 61 74 61 62 61 73 65 5f 6c 69 73 74 22 29 3d  database_list")=
29688 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  =0 ){.    int i;
29689 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
2968a 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
2968b 65 29 20 29 20 67 6f 74 6f 20 70 72 61 67 6d 61  e) ) goto pragma
2968c 5f 6f 75 74 3b 0a 20 20 20 20 73 71 6c 69 74 65  _out;.    sqlite
2968d 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
2968e 76 2c 20 33 29 3b 0a 20 20 20 20 70 50 61 72 73  v, 3);.    pPars
2968f 65 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a 20 20 20  e->nMem = 3;.   
29690 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
29691 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
29692 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22  NAME_NAME, "seq"
29693 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
29694 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
29695 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31  eSetColName(v, 1
29696 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
29697 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53  "name", SQLITE_S
29698 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69  TATIC);.    sqli
29699 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
2969a 65 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 2, COLNAME_
2969b 4e 41 4d 45 2c 20 22 66 69 6c 65 22 2c 20 53 51  NAME, "file", SQ
2969c 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
2969d 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
2969e 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  >nDb; i++){.    
2969f 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d    if( db->aDb[i]
296a0 2e 70 42 74 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  .pBt==0 ) contin
296a1 75 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ue;.      assert
296a2 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61  ( db->aDb[i].zNa
296a3 6d 65 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 73  me!=0 );.      s
296a4 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
296a5 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
296a6 69 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  i, 1);.      sql
296a7 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
296a8 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
296a9 20 32 2c 20 30 2c 20 64 62 2d 3e 61 44 62 5b 69   2, 0, db->aDb[i
296aa 5d 2e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ].zName, 0);.   
296ab 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
296ac 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e  dOp4(v, OP_Strin
296ad 67 38 2c 20 30 2c 20 33 2c 20 30 2c 0a 20 20 20  g8, 0, 3, 0,.   
296ae 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42          sqlite3B
296af 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65 28  treeGetFilename(
296b0 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 2c  db->aDb[i].pBt),
296b1 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   0);.      sqlit
296b2 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
296b3 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c  OP_ResultRow, 1,
296b4 20 33 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c   3);.    }.  }el
296b5 73 65 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65  se..  if( sqlite
296b6 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
296b7 22 63 6f 6c 6c 61 74 69 6f 6e 5f 6c 69 73 74 22  "collation_list"
296b8 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  )==0 ){.    int 
296b9 69 20 3d 20 30 3b 0a 20 20 20 20 48 61 73 68 45  i = 0;.    HashE
296ba 6c 65 6d 20 2a 70 3b 0a 20 20 20 20 73 71 6c 69  lem *p;.    sqli
296bb 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c  te3VdbeSetNumCol
296bc 73 28 76 2c 20 32 29 3b 0a 20 20 20 20 70 50 61  s(v, 2);.    pPa
296bd 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 32 3b 0a 20  rse->nMem = 2;. 
296be 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
296bf 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
296c0 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65  OLNAME_NAME, "se
296c1 71 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  q", SQLITE_STATI
296c2 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  C);.    sqlite3V
296c3 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
296c4 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   1, COLNAME_NAME
296c5 2c 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45  , "name", SQLITE
296c6 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 66 6f  _STATIC);.    fo
296c7 72 28 70 3d 73 71 6c 69 74 65 48 61 73 68 46 69  r(p=sqliteHashFi
296c8 72 73 74 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65  rst(&db->aCollSe
296c9 71 29 3b 20 70 3b 20 70 3d 73 71 6c 69 74 65 48  q); p; p=sqliteH
296ca 61 73 68 4e 65 78 74 28 70 29 29 7b 0a 20 20 20  ashNext(p)){.   
296cb 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
296cc 6c 20 3d 20 28 43 6f 6c 6c 53 65 71 20 2a 29 73  l = (CollSeq *)s
296cd 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 29  qliteHashData(p)
296ce 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
296cf 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
296d0 49 6e 74 65 67 65 72 2c 20 69 2b 2b 2c 20 31 29  Integer, i++, 1)
296d1 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
296d2 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
296d3 53 74 72 69 6e 67 38 2c 20 30 2c 20 32 2c 20 30  String8, 0, 2, 0
296d4 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20  , pColl->zName, 
296d5 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  0);.      sqlite
296d6 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
296d7 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20  P_ResultRow, 1, 
296d8 32 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  2);.    }.  }els
296d9 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  e.#endif /* SQLI
296da 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 50  TE_OMIT_SCHEMA_P
296db 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64  RAGMAS */..#ifnd
296dc 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46  ef SQLITE_OMIT_F
296dd 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 69 66 28  OREIGN_KEY.  if(
296de 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
296df 7a 4c 65 66 74 2c 20 22 66 6f 72 65 69 67 6e 5f  zLeft, "foreign_
296e0 6b 65 79 5f 6c 69 73 74 22 29 3d 3d 30 20 26 26  key_list")==0 &&
296e1 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 46   zRight ){.    F
296e2 4b 65 79 20 2a 70 46 4b 3b 0a 20 20 20 20 54 61  Key *pFK;.    Ta
296e3 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69  ble *pTab;.    i
296e4 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63  f( sqlite3ReadSc
296e5 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67  hema(pParse) ) g
296e6 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a  oto pragma_out;.
296e7 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74      pTab = sqlit
296e8 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
296e9 7a 52 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20  zRight, zDb);.  
296ea 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20    if( pTab ){.  
296eb 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47      v = sqlite3G
296ec 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
296ed 20 20 20 20 20 20 70 46 4b 20 3d 20 70 54 61 62        pFK = pTab
296ee 2d 3e 70 46 4b 65 79 3b 0a 20 20 20 20 20 20 69  ->pFKey;.      i
296ef 66 28 20 70 46 4b 20 29 7b 0a 20 20 20 20 20 20  f( pFK ){.      
296f0 20 20 69 6e 74 20 69 20 3d 20 30 3b 20 0a 20 20    int i = 0; .  
296f1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
296f2 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 38  eSetNumCols(v, 8
296f3 29 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73  );.        pPars
296f4 65 2d 3e 6e 4d 65 6d 20 3d 20 38 3b 0a 20 20 20  e->nMem = 8;.   
296f5 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
296f6 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c  SetColName(v, 0,
296f7 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
296f8 69 64 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  id", SQLITE_STAT
296f9 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  IC);.        sql
296fa 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
296fb 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45  me(v, 1, COLNAME
296fc 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c 20 53 51  _NAME, "seq", SQ
296fd 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
296fe 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
296ff 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32  eSetColName(v, 2
29700 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
29701 22 74 61 62 6c 65 22 2c 20 53 51 4c 49 54 45 5f  "table", SQLITE_
29702 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
29703 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
29704 6f 6c 4e 61 6d 65 28 76 2c 20 33 2c 20 43 4f 4c  olName(v, 3, COL
29705 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 66 72 6f 6d  NAME_NAME, "from
29706 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
29707 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
29708 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
29709 28 76 2c 20 34 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 4, COLNAME_N
2970a 41 4d 45 2c 20 22 74 6f 22 2c 20 53 51 4c 49 54  AME, "to", SQLIT
2970b 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
2970c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
2970d 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 35 2c 20 43  tColName(v, 5, C
2970e 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 6e  OLNAME_NAME, "on
2970f 5f 75 70 64 61 74 65 22 2c 20 53 51 4c 49 54 45  _update", SQLITE
29710 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
29711 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
29712 43 6f 6c 4e 61 6d 65 28 76 2c 20 36 2c 20 43 4f  ColName(v, 6, CO
29713 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f 6e 5f  LNAME_NAME, "on_
29714 64 65 6c 65 74 65 22 2c 20 53 51 4c 49 54 45 5f  delete", SQLITE_
29715 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20  STATIC);.       
29716 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
29717 6f 6c 4e 61 6d 65 28 76 2c 20 37 2c 20 43 4f 4c  olName(v, 7, COL
29718 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6d 61 74 63  NAME_NAME, "matc
29719 68 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  h", SQLITE_STATI
2971a 43 29 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c  C);.        whil
2971b 65 28 70 46 4b 29 7b 0a 20 20 20 20 20 20 20 20  e(pFK){.        
2971c 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20    int j;.       
2971d 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 46     for(j=0; j<pF
2971e 4b 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20  K->nCol; j++){. 
2971f 20 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20             char 
29720 2a 7a 43 6f 6c 20 3d 20 70 46 4b 2d 3e 61 43 6f  *zCol = pFK->aCo
29721 6c 5b 6a 5d 2e 7a 43 6f 6c 3b 0a 20 20 20 20 20  l[j].zCol;.     
29722 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4f 6e         char *zOn
29723 44 65 6c 65 74 65 20 3d 20 28 63 68 61 72 20 2a  Delete = (char *
29724 29 61 63 74 69 6f 6e 4e 61 6d 65 28 70 46 4b 2d  )actionName(pFK-
29725 3e 61 41 63 74 69 6f 6e 5b 30 5d 29 3b 0a 20 20  >aAction[0]);.  
29726 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
29727 7a 4f 6e 55 70 64 61 74 65 20 3d 20 28 63 68 61  zOnUpdate = (cha
29728 72 20 2a 29 61 63 74 69 6f 6e 4e 61 6d 65 28 70  r *)actionName(p
29729 46 4b 2d 3e 61 41 63 74 69 6f 6e 5b 31 5d 29 3b  FK->aAction[1]);
2972a 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
2972b 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2972c 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c  , OP_Integer, i,
2972d 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   1);.           
2972e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2972f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
29730 2c 20 6a 2c 20 32 29 3b 0a 20 20 20 20 20 20 20  , j, 2);.       
29731 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29732 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
29733 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c 20 70  ing8, 0, 3, 0, p
29734 46 4b 2d 3e 7a 54 6f 2c 20 30 29 3b 0a 20 20 20  FK->zTo, 0);.   
29735 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
29736 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
29737 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 34 2c 20  _String8, 0, 4, 
29738 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
29739 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2973a 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70 46 4b 2d   pTab->aCol[pFK-
2973b 3e 61 43 6f 6c 5b 6a 5d 2e 69 46 72 6f 6d 5d 2e  >aCol[j].iFrom].
2973c 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20  zName, 0);.     
2973d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2973e 62 65 41 64 64 4f 70 34 28 76 2c 20 7a 43 6f 6c  beAddOp4(v, zCol
2973f 20 3f 20 4f 50 5f 53 74 72 69 6e 67 38 20 3a 20   ? OP_String8 : 
29740 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 35 2c 20 30  OP_Null, 0, 5, 0
29741 2c 20 7a 43 6f 6c 2c 20 30 29 3b 0a 20 20 20 20  , zCol, 0);.    
29742 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29743 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
29744 53 74 72 69 6e 67 38 2c 20 30 2c 20 36 2c 20 30  String8, 0, 6, 0
29745 2c 20 7a 4f 6e 55 70 64 61 74 65 2c 20 30 29 3b  , zOnUpdate, 0);
29746 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
29747 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
29748 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
29749 20 37 2c 20 30 2c 20 7a 4f 6e 44 65 6c 65 74 65   7, 0, zOnDelete
2974a 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
2974b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2974c 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
2974d 38 2c 20 30 2c 20 38 2c 20 30 2c 20 22 4e 4f 4e  8, 0, 8, 0, "NON
2974e 45 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  E", 0);.        
2974f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29750 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
29751 6c 74 52 6f 77 2c 20 31 2c 20 38 29 3b 0a 20 20  ltRow, 1, 8);.  
29752 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
29753 20 20 20 20 2b 2b 69 3b 0a 20 20 20 20 20 20 20      ++i;.       
29754 20 20 20 70 46 4b 20 3d 20 70 46 4b 2d 3e 70 4e     pFK = pFK->pN
29755 65 78 74 46 72 6f 6d 3b 0a 20 20 20 20 20 20 20  extFrom;.       
29756 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
29757 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20  .  }else.#endif 
29758 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
29759 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f  TE_OMIT_FOREIGN_
2975a 4b 45 59 29 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  KEY) */..#ifndef
2975b 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 73 71   NDEBUG.  if( sq
2975c 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65  lite3StrICmp(zLe
2975d 66 74 2c 20 22 70 61 72 73 65 72 5f 74 72 61 63  ft, "parser_trac
2975e 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  e")==0 ){.    if
2975f 28 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20  ( zRight ){.    
29760 20 20 69 66 28 20 67 65 74 42 6f 6f 6c 65 61 6e    if( getBoolean
29761 28 7a 52 69 67 68 74 29 20 29 7b 0a 20 20 20 20  (zRight) ){.    
29762 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65      sqlite3Parse
29763 72 54 72 61 63 65 28 73 74 64 65 72 72 2c 20 22  rTrace(stderr, "
29764 70 61 72 73 65 72 3a 20 22 29 3b 0a 20 20 20 20  parser: ");.    
29765 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
29766 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 72   sqlite3ParserTr
29767 61 63 65 28 30 2c 20 30 29 3b 0a 20 20 20 20 20  ace(0, 0);.     
29768 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
29769 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65  .#endif..  /* Re
2976a 69 6e 73 74 61 6c 6c 20 74 68 65 20 4c 49 4b 45  install the LIKE
2976b 20 61 6e 64 20 47 4c 4f 42 20 66 75 6e 63 74 69   and GLOB functi
2976c 6f 6e 73 2e 20 20 54 68 65 20 76 61 72 69 61 6e  ons.  The varian
2976d 74 20 6f 66 20 4c 49 4b 45 0a 20 20 2a 2a 20 75  t of LIKE.  ** u
2976e 73 65 64 20 77 69 6c 6c 20 62 65 20 63 61 73 65  sed will be case
2976f 20 73 65 6e 73 69 74 69 76 65 20 6f 72 20 6e 6f   sensitive or no
29770 74 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74  t depending on t
29771 68 65 20 52 48 53 2e 0a 20 20 2a 2f 0a 20 20 69  he RHS..  */.  i
29772 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
29773 70 28 7a 4c 65 66 74 2c 20 22 63 61 73 65 5f 73  p(zLeft, "case_s
29774 65 6e 73 69 74 69 76 65 5f 6c 69 6b 65 22 29 3d  ensitive_like")=
29775 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 7a 52  =0 ){.    if( zR
29776 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 73 71  ight ){.      sq
29777 6c 69 74 65 33 52 65 67 69 73 74 65 72 4c 69 6b  lite3RegisterLik
29778 65 46 75 6e 63 74 69 6f 6e 73 28 64 62 2c 20 67  eFunctions(db, g
29779 65 74 42 6f 6f 6c 65 61 6e 28 7a 52 69 67 68 74  etBoolean(zRight
2977a 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  ));.    }.  }els
2977b 65 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  e..#ifndef SQLIT
2977c 45 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  E_INTEGRITY_CHEC
2977d 4b 5f 45 52 52 4f 52 5f 4d 41 58 0a 23 20 64 65  K_ERROR_MAX.# de
2977e 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54 45  fine SQLITE_INTE
2977f 47 52 49 54 59 5f 43 48 45 43 4b 5f 45 52 52 4f  GRITY_CHECK_ERRO
29780 52 5f 4d 41 58 20 31 30 30 0a 23 65 6e 64 69 66  R_MAX 100.#endif
29781 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
29782 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f  _OMIT_INTEGRITY_
29783 43 48 45 43 4b 0a 20 20 2f 2a 20 50 72 61 67 6d  CHECK.  /* Pragm
29784 61 20 22 71 75 69 63 6b 5f 63 68 65 63 6b 22 20  a "quick_check" 
29785 69 73 20 61 6e 20 65 78 70 65 72 69 6d 65 6e 74  is an experiment
29786 61 6c 20 72 65 64 75 63 65 64 20 76 65 72 73 69  al reduced versi
29787 6f 6e 20 6f 66 20 0a 20 20 2a 2a 20 69 6e 74 65  on of .  ** inte
29788 67 72 69 74 79 5f 63 68 65 63 6b 20 64 65 73 69  grity_check desi
29789 67 6e 65 64 20 74 6f 20 64 65 74 65 63 74 20 6d  gned to detect m
2978a 6f 73 74 20 64 61 74 61 62 61 73 65 20 63 6f 72  ost database cor
2978b 72 75 70 74 69 6f 6e 0a 20 20 2a 2a 20 77 69 74  ruption.  ** wit
2978c 68 6f 75 74 20 6d 6f 73 74 20 6f 66 20 74 68 65  hout most of the
2978d 20 6f 76 65 72 68 65 61 64 20 6f 66 20 61 20 66   overhead of a f
2978e 75 6c 6c 20 69 6e 74 65 67 72 69 74 79 2d 63 68  ull integrity-ch
2978f 65 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  eck..  */.  if( 
29790 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
29791 4c 65 66 74 2c 20 22 69 6e 74 65 67 72 69 74 79  Left, "integrity
29792 5f 63 68 65 63 6b 22 29 3d 3d 30 0a 20 20 20 7c  _check")==0.   |
29793 7c 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  | sqlite3StrICmp
29794 28 7a 4c 65 66 74 2c 20 22 71 75 69 63 6b 5f 63  (zLeft, "quick_c
29795 68 65 63 6b 22 29 3d 3d 30 20 0a 20 20 29 7b 0a  heck")==0 .  ){.
29796 20 20 20 20 69 6e 74 20 69 2c 20 6a 2c 20 61 64      int i, j, ad
29797 64 72 2c 20 6d 78 45 72 72 3b 0a 0a 20 20 20 20  dr, mxErr;..    
29798 2f 2a 20 43 6f 64 65 20 74 68 61 74 20 61 70 70  /* Code that app
29799 65 61 72 73 20 61 74 20 74 68 65 20 65 6e 64 20  ears at the end 
2979a 6f 66 20 74 68 65 20 69 6e 74 65 67 72 69 74 79  of the integrity
2979b 20 63 68 65 63 6b 2e 20 20 49 66 20 6e 6f 20 65   check.  If no e
2979c 72 72 6f 72 0a 20 20 20 20 2a 2a 20 6d 65 73 73  rror.    ** mess
2979d 61 67 65 73 20 68 61 76 65 20 62 65 65 6e 20 67  ages have been g
2979e 65 6e 65 72 61 74 65 64 2c 20 6f 75 74 70 75 74  enerated, output
2979f 20 4f 4b 2e 20 20 4f 74 68 65 72 77 69 73 65 20   OK.  Otherwise 
297a0 6f 75 74 70 75 74 20 74 68 65 0a 20 20 20 20 2a  output the.    *
297a1 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a  * error message.
297a2 20 20 20 20 2a 2f 0a 20 20 20 20 73 74 61 74 69      */.    stati
297a3 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69  c const VdbeOpLi
297a4 73 74 20 65 6e 64 43 6f 64 65 5b 5d 20 3d 20 7b  st endCode[] = {
297a5 0a 20 20 20 20 20 20 7b 20 4f 50 5f 41 64 64 49  .      { OP_AddI
297a6 6d 6d 2c 20 20 20 20 20 20 31 2c 20 30 2c 20 20  mm,      1, 0,  
297a7 20 20 20 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20        0},    /* 
297a8 30 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f  0 */.      { OP_
297a9 49 66 4e 65 67 2c 20 20 20 20 20 20 20 31 2c 20  IfNeg,       1, 
297aa 30 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20  0,        0},   
297ab 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 7b   /* 1 */.      {
297ac 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20   OP_String8,    
297ad 20 30 2c 20 33 2c 20 20 20 20 20 20 20 20 30 7d   0, 3,        0}
297ae 2c 20 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20  ,    /* 2 */.   
297af 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f     { OP_ResultRo
297b0 77 2c 20 20 20 33 2c 20 31 2c 20 20 20 20 20 20  w,   3, 1,      
297b1 20 20 30 7d 2c 0a 20 20 20 20 7d 3b 0a 0a 20 20    0},.    };..  
297b2 20 20 69 6e 74 20 69 73 51 75 69 63 6b 20 3d 20    int isQuick = 
297b3 28 7a 4c 65 66 74 5b 30 5d 3d 3d 27 71 27 29 3b  (zLeft[0]=='q');
297b4 0a 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c  ..    /* Initial
297b5 69 7a 65 20 74 68 65 20 56 44 42 45 20 70 72 6f  ize the VDBE pro
297b6 67 72 61 6d 20 2a 2f 0a 20 20 20 20 69 66 28 20  gram */.    if( 
297b7 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
297b8 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f  a(pParse) ) goto
297b9 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20   pragma_out;.   
297ba 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20   pParse->nMem = 
297bb 36 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  6;.    sqlite3Vd
297bc 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
297bd 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  1);.    sqlite3V
297be 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
297bf 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
297c0 2c 20 22 69 6e 74 65 67 72 69 74 79 5f 63 68 65  , "integrity_che
297c1 63 6b 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  ck", SQLITE_STAT
297c2 49 43 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74  IC);..    /* Set
297c3 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 65 72 72   the maximum err
297c4 6f 72 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20  or count */.    
297c5 6d 78 45 72 72 20 3d 20 53 51 4c 49 54 45 5f 49  mxErr = SQLITE_I
297c6 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 5f 45  NTEGRITY_CHECK_E
297c7 52 52 4f 52 5f 4d 41 58 3b 0a 20 20 20 20 69 66  RROR_MAX;.    if
297c8 28 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20  ( zRight ){.    
297c9 20 20 6d 78 45 72 72 20 3d 20 61 74 6f 69 28 7a    mxErr = atoi(z
297ca 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66  Right);.      if
297cb 28 20 6d 78 45 72 72 3c 3d 30 20 29 7b 0a 20 20  ( mxErr<=0 ){.  
297cc 20 20 20 20 20 20 6d 78 45 72 72 20 3d 20 53 51        mxErr = SQ
297cd 4c 49 54 45 5f 49 4e 54 45 47 52 49 54 59 5f 43  LITE_INTEGRITY_C
297ce 48 45 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 3b 0a  HECK_ERROR_MAX;.
297cf 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
297d0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
297d1 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
297d2 72 2c 20 6d 78 45 72 72 2c 20 31 29 3b 20 20 2f  r, mxErr, 1);  /
297d3 2a 20 72 65 67 5b 31 5d 20 68 6f 6c 64 73 20 65  * reg[1] holds e
297d4 72 72 6f 72 73 20 6c 65 66 74 20 2a 2f 0a 0a 20  rrors left */.. 
297d5 20 20 20 2f 2a 20 44 6f 20 61 6e 20 69 6e 74 65     /* Do an inte
297d6 67 72 69 74 79 20 63 68 65 63 6b 20 6f 6e 20 65  grity check on e
297d7 61 63 68 20 64 61 74 61 62 61 73 65 20 66 69 6c  ach database fil
297d8 65 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 30  e */.    for(i=0
297d9 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
297da 29 7b 0a 20 20 20 20 20 20 48 61 73 68 45 6c 65  ){.      HashEle
297db 6d 20 2a 78 3b 0a 20 20 20 20 20 20 48 61 73 68  m *x;.      Hash
297dc 20 2a 70 54 62 6c 73 3b 0a 20 20 20 20 20 20 69   *pTbls;.      i
297dd 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 0a 20 20 20  nt cnt = 0;..   
297de 20 20 20 69 66 28 20 4f 4d 49 54 5f 54 45 4d 50     if( OMIT_TEMP
297df 44 42 20 26 26 20 69 3d 3d 31 20 29 20 63 6f 6e  DB && i==1 ) con
297e0 74 69 6e 75 65 3b 0a 0a 20 20 20 20 20 20 73 71  tinue;..      sq
297e1 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53  lite3CodeVerifyS
297e2 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 29  chema(pParse, i)
297e3 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73  ;.      addr = s
297e4 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
297e5 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31 29  (v, OP_IfPos, 1)
297e6 3b 20 2f 2a 20 48 61 6c 74 20 69 66 20 6f 75 74  ; /* Halt if out
297e7 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20   of errors */.  
297e8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
297e9 64 64 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74  ddOp2(v, OP_Halt
297ea 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 73  , 0, 0);.      s
297eb 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
297ec 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 0a 20 20  re(v, addr);..  
297ed 20 20 20 20 2f 2a 20 44 6f 20 61 6e 20 69 6e 74      /* Do an int
297ee 65 67 72 69 74 79 20 63 68 65 63 6b 20 6f 66 20  egrity check of 
297ef 74 68 65 20 42 2d 54 72 65 65 0a 20 20 20 20 20  the B-Tree.     
297f0 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 42 65 67   **.      ** Beg
297f1 69 6e 20 62 79 20 66 69 6c 6c 69 6e 67 20 72 65  in by filling re
297f2 67 69 73 74 65 72 73 20 32 2c 20 33 2c 20 2e 2e  gisters 2, 3, ..
297f3 2e 20 77 69 74 68 20 74 68 65 20 72 6f 6f 74 20  . with the root 
297f4 70 61 67 65 73 20 6e 75 6d 62 65 72 73 0a 20 20  pages numbers.  
297f5 20 20 20 20 2a 2a 20 66 6f 72 20 61 6c 6c 20 74      ** for all t
297f6 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65  ables and indice
297f7 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  s in the databas
297f8 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  e..      */.    
297f9 20 20 70 54 62 6c 73 20 3d 20 26 64 62 2d 3e 61    pTbls = &db->a
297fa 44 62 5b 69 5d 2e 70 53 63 68 65 6d 61 2d 3e 74  Db[i].pSchema->t
297fb 62 6c 48 61 73 68 3b 0a 20 20 20 20 20 20 66 6f  blHash;.      fo
297fc 72 28 78 3d 73 71 6c 69 74 65 48 61 73 68 46 69  r(x=sqliteHashFi
297fd 72 73 74 28 70 54 62 6c 73 29 3b 20 78 3b 20 78  rst(pTbls); x; x
297fe 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28  =sqliteHashNext(
297ff 78 29 29 7b 0a 20 20 20 20 20 20 20 20 54 61 62  x)){.        Tab
29800 6c 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69 74  le *pTab = sqlit
29801 65 48 61 73 68 44 61 74 61 28 78 29 3b 0a 20 20  eHashData(x);.  
29802 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64        Index *pId
29803 78 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  x;.        sqlit
29804 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
29805 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70 54 61 62  OP_Integer, pTab
29806 2d 3e 74 6e 75 6d 2c 20 32 2b 63 6e 74 29 3b 0a  ->tnum, 2+cnt);.
29807 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20          cnt++;. 
29808 20 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d         for(pIdx=
29809 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
2980a 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
2980b 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 20  Next){.         
2980c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2980d 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
2980e 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 32 2b  , pIdx->tnum, 2+
2980f 63 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20  cnt);.          
29810 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d  cnt++;.        }
29811 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20  .      }..      
29812 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 73 75 66  /* Make sure suf
29813 66 69 63 69 65 6e 74 20 6e 75 6d 62 65 72 20 6f  ficient number o
29814 66 20 72 65 67 69 73 74 65 72 73 20 68 61 76 65  f registers have
29815 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20   been allocated 
29816 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 50 61  */.      if( pPa
29817 72 73 65 2d 3e 6e 4d 65 6d 20 3c 20 63 6e 74 2b  rse->nMem < cnt+
29818 34 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61  4 ){.        pPa
29819 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 63 6e 74 2b  rse->nMem = cnt+
2981a 34 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  4;.      }..    
2981b 20 20 2f 2a 20 44 6f 20 74 68 65 20 62 2d 74 72    /* Do the b-tr
2981c 65 65 20 69 6e 74 65 67 72 69 74 79 20 63 68 65  ee integrity che
2981d 63 6b 73 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c  cks */.      sql
2981e 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2981f 2c 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b  , OP_IntegrityCk
29820 2c 20 32 2c 20 63 6e 74 2c 20 31 29 3b 0a 20 20  , 2, cnt, 1);.  
29821 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
29822 68 61 6e 67 65 50 35 28 76 2c 20 28 75 38 29 69  hangeP5(v, (u8)i
29823 29 3b 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20  );.      addr = 
29824 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29825 31 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20  1(v, OP_IsNull, 
29826 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  2);.      sqlite
29827 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
29828 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 33 2c  P_String8, 0, 3,
29829 20 30 2c 0a 20 20 20 20 20 20 20 20 20 73 71 6c   0,.         sql
2982a 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
2982b 22 2a 2a 2a 20 69 6e 20 64 61 74 61 62 61 73 65  "*** in database
2982c 20 25 73 20 2a 2a 2a 5c 6e 22 2c 20 64 62 2d 3e   %s ***\n", db->
2982d 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 29 2c 0a 20  aDb[i].zName),. 
2982e 20 20 20 20 20 20 20 20 50 34 5f 44 59 4e 41 4d          P4_DYNAM
2982f 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
29830 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
29831 4f 50 5f 4d 6f 76 65 2c 20 32 2c 20 34 2c 20 31  OP_Move, 2, 4, 1
29832 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29833 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
29834 5f 43 6f 6e 63 61 74 2c 20 34 2c 20 33 2c 20 32  _Concat, 4, 3, 2
29835 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29836 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
29837 5f 52 65 73 75 6c 74 52 6f 77 2c 20 32 2c 20 31  _ResultRow, 2, 1
29838 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29839 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
2983a 61 64 64 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  addr);..      /*
2983b 20 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 74   Make sure all t
2983c 68 65 20 69 6e 64 69 63 65 73 20 61 72 65 20 63  he indices are c
2983d 6f 6e 73 74 72 75 63 74 65 64 20 63 6f 72 72 65  onstructed corre
2983e 63 74 6c 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  ctly..      */. 
2983f 20 20 20 20 20 66 6f 72 28 78 3d 73 71 6c 69 74       for(x=sqlit
29840 65 48 61 73 68 46 69 72 73 74 28 70 54 62 6c 73  eHashFirst(pTbls
29841 29 3b 20 78 20 26 26 20 21 69 73 51 75 69 63 6b  ); x && !isQuick
29842 3b 20 78 3d 73 71 6c 69 74 65 48 61 73 68 4e 65  ; x=sqliteHashNe
29843 78 74 28 78 29 29 7b 0a 20 20 20 20 20 20 20 20  xt(x)){.        
29844 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71  Table *pTab = sq
29845 6c 69 74 65 48 61 73 68 44 61 74 61 28 78 29 3b  liteHashData(x);
29846 0a 20 20 20 20 20 20 20 20 49 6e 64 65 78 20 2a  .        Index *
29847 70 49 64 78 3b 0a 20 20 20 20 20 20 20 20 69 6e  pIdx;.        in
29848 74 20 6c 6f 6f 70 54 6f 70 3b 0a 0a 20 20 20 20  t loopTop;..    
29849 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 70 49      if( pTab->pI
2984a 6e 64 65 78 3d 3d 30 20 29 20 63 6f 6e 74 69 6e  ndex==0 ) contin
2984b 75 65 3b 0a 20 20 20 20 20 20 20 20 61 64 64 72  ue;.        addr
2984c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2984d 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73  dOp1(v, OP_IfPos
2984e 2c 20 31 29 3b 20 20 2f 2a 20 53 74 6f 70 20 69  , 1);  /* Stop i
2984f 66 20 6f 75 74 20 6f 66 20 65 72 72 6f 72 73 20  f out of errors 
29850 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  */.        sqlit
29851 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
29852 4f 50 5f 48 61 6c 74 2c 20 30 2c 20 30 29 3b 0a  OP_Halt, 0, 0);.
29853 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29854 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
29855 64 64 72 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ddr);.        sq
29856 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e  lite3OpenTableAn
29857 64 49 6e 64 69 63 65 73 28 70 50 61 72 73 65 2c  dIndices(pParse,
29858 20 70 54 61 62 2c 20 31 2c 20 4f 50 5f 4f 70 65   pTab, 1, OP_Ope
29859 6e 52 65 61 64 29 3b 0a 20 20 20 20 20 20 20 20  nRead);.        
2985a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2985b 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2985c 20 30 2c 20 32 29 3b 20 20 2f 2a 20 72 65 67 28   0, 2);  /* reg(
2985d 32 29 20 77 69 6c 6c 20 63 6f 75 6e 74 20 65 6e  2) will count en
2985e 74 72 69 65 73 20 2a 2f 0a 20 20 20 20 20 20 20  tries */.       
2985f 20 6c 6f 6f 70 54 6f 70 20 3d 20 73 71 6c 69 74   loopTop = sqlit
29860 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
29861 4f 50 5f 52 65 77 69 6e 64 2c 20 31 2c 20 30 29  OP_Rewind, 1, 0)
29862 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
29863 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29864 50 5f 41 64 64 49 6d 6d 2c 20 32 2c 20 31 29 3b  P_AddImm, 2, 1);
29865 20 20 20 2f 2a 20 69 6e 63 72 65 6d 65 6e 74 20     /* increment 
29866 65 6e 74 72 79 20 63 6f 75 6e 74 20 2a 2f 0a 20  entry count */. 
29867 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20         for(j=0, 
29868 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
29869 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49  x; pIdx; pIdx=pI
2986a 64 78 2d 3e 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b  dx->pNext, j++){
2986b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6a  .          int j
2986c 6d 70 32 3b 0a 20 20 20 20 20 20 20 20 20 20 73  mp2;.          s
2986d 74 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65  tatic const Vdbe
2986e 4f 70 4c 69 73 74 20 69 64 78 45 72 72 5b 5d 20  OpList idxErr[] 
2986f 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  = {.            
29870 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20 20  { OP_AddImm,    
29871 20 20 31 2c 20 2d 31 2c 20 20 30 7d 2c 0a 20 20    1, -1,  0},.  
29872 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53            { OP_S
29873 74 72 69 6e 67 38 2c 20 20 20 20 20 30 2c 20 20  tring8,     0,  
29874 33 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 31 20  3,  0},    /* 1 
29875 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  */.            {
29876 20 4f 50 5f 52 6f 77 69 64 2c 20 20 20 20 20 20   OP_Rowid,      
29877 20 31 2c 20 20 34 2c 20 20 30 7d 2c 0a 20 20 20   1,  4,  0},.   
29878 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53 74           { OP_St
29879 72 69 6e 67 38 2c 20 20 20 20 20 30 2c 20 20 35  ring8,     0,  5
2987a 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 33 20 2a  ,  0},    /* 3 *
2987b 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  /.            { 
2987c 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20 20  OP_String8,     
2987d 30 2c 20 20 36 2c 20 20 30 7d 2c 20 20 20 20 2f  0,  6,  0},    /
2987e 2a 20 34 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  * 4 */.         
2987f 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20     { OP_Concat, 
29880 20 20 20 20 20 34 2c 20 20 33 2c 20 20 33 7d 2c       4,  3,  3},
29881 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
29882 50 5f 43 6f 6e 63 61 74 2c 20 20 20 20 20 20 35  P_Concat,      5
29883 2c 20 20 33 2c 20 20 33 7d 2c 0a 20 20 20 20 20  ,  3,  3},.     
29884 20 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63         { OP_Conc
29885 61 74 2c 20 20 20 20 20 20 36 2c 20 20 33 2c 20  at,      6,  3, 
29886 20 33 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20   3},.           
29887 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c   { OP_ResultRow,
29888 20 20 20 33 2c 20 20 31 2c 20 20 30 7d 2c 0a 20     3,  1,  0},. 
29889 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
2988a 49 66 50 6f 73 2c 20 20 20 20 20 20 20 31 2c 20  IfPos,       1, 
2988b 20 30 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 39   0,  0},    /* 9
2988c 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
2988d 7b 20 4f 50 5f 48 61 6c 74 2c 20 20 20 20 20 20  { OP_Halt,      
2988e 20 20 30 2c 20 20 30 2c 20 20 30 7d 2c 0a 20 20    0,  0,  0},.  
2988f 20 20 20 20 20 20 20 20 7d 3b 0a 20 20 20 20 20          };.     
29890 20 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65       sqlite3Gene
29891 72 61 74 65 49 6e 64 65 78 4b 65 79 28 70 50 61  rateIndexKey(pPa
29892 72 73 65 2c 20 70 49 64 78 2c 20 31 2c 20 33 2c  rse, pIdx, 1, 3,
29893 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a   1);.          j
29894 6d 70 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62  mp2 = sqlite3Vdb
29895 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f  eAddOp3(v, OP_Fo
29896 75 6e 64 2c 20 6a 2b 32 2c 20 30 2c 20 33 29 3b  und, j+2, 0, 3);
29897 0a 20 20 20 20 20 20 20 20 20 20 61 64 64 72 20  .          addr 
29898 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
29899 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53  OpList(v, ArrayS
2989a 69 7a 65 28 69 64 78 45 72 72 29 2c 20 69 64 78  ize(idxErr), idx
2989b 45 72 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Err);.          
2989c 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2989d 65 50 34 28 76 2c 20 61 64 64 72 2b 31 2c 20 22  eP4(v, addr+1, "
2989e 72 6f 77 69 64 20 22 2c 20 50 34 5f 53 54 41 54  rowid ", P4_STAT
2989f 49 43 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  IC);.          s
298a0 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
298a1 50 34 28 76 2c 20 61 64 64 72 2b 33 2c 20 22 20  P4(v, addr+3, " 
298a2 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20 69 6e 64  missing from ind
298a3 65 78 20 22 2c 20 50 34 5f 53 54 41 54 49 43 29  ex ", P4_STATIC)
298a4 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
298a5 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
298a6 76 2c 20 61 64 64 72 2b 34 2c 20 70 49 64 78 2d  v, addr+4, pIdx-
298a7 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49  >zName, P4_STATI
298a8 43 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  C);.          sq
298a9 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
298aa 65 28 76 2c 20 61 64 64 72 2b 39 29 3b 0a 20 20  e(v, addr+9);.  
298ab 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
298ac 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
298ad 6d 70 32 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  mp2);.        }.
298ae 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
298af 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
298b0 4e 65 78 74 2c 20 31 2c 20 6c 6f 6f 70 54 6f 70  Next, 1, loopTop
298b1 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  +1);.        sql
298b2 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
298b3 28 76 2c 20 6c 6f 6f 70 54 6f 70 29 3b 0a 20 20  (v, loopTop);.  
298b4 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70        for(j=0, p
298b5 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
298b6 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
298b7 78 2d 3e 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a  x->pNext, j++){.
298b8 20 20 20 20 20 20 20 20 20 20 73 74 61 74 69 63            static
298b9 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73   const VdbeOpLis
298ba 74 20 63 6e 74 49 64 78 5b 5d 20 3d 20 7b 0a 20  t cntIdx[] = {. 
298bb 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
298bc 5f 49 6e 74 65 67 65 72 2c 20 20 20 20 20 20 30  _Integer,      0
298bd 2c 20 20 33 2c 20 20 30 7d 2c 0a 20 20 20 20 20  ,  3,  0},.     
298be 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 77          { OP_Rew
298bf 69 6e 64 2c 20 20 20 20 20 20 20 30 2c 20 20 30  ind,       0,  0
298c0 2c 20 20 30 7d 2c 20 20 2f 2a 20 31 20 2a 2f 0a  ,  0},  /* 1 */.
298c1 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f               { O
298c2 50 5f 41 64 64 49 6d 6d 2c 20 20 20 20 20 20 20  P_AddImm,       
298c3 33 2c 20 20 31 2c 20 20 30 7d 2c 0a 20 20 20 20  3,  1,  0},.    
298c4 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65           { OP_Ne
298c5 78 74 2c 20 20 20 20 20 20 20 20 20 30 2c 20 20  xt,         0,  
298c6 30 2c 20 20 30 7d 2c 20 20 2f 2a 20 33 20 2a 2f  0,  0},  /* 3 */
298c7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  .             { 
298c8 4f 50 5f 45 71 2c 20 20 20 20 20 20 20 20 20 20  OP_Eq,          
298c9 20 32 2c 20 20 30 2c 20 20 33 7d 2c 20 20 2f 2a   2,  0,  3},  /*
298ca 20 34 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20   4 */.          
298cb 20 20 20 7b 20 4f 50 5f 41 64 64 49 6d 6d 2c 20     { OP_AddImm, 
298cc 20 20 20 20 20 20 31 2c 20 2d 31 2c 20 20 30 7d        1, -1,  0}
298cd 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7b  ,.             {
298ce 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20   OP_String8,    
298cf 20 20 30 2c 20 20 32 2c 20 20 30 7d 2c 20 20 2f    0,  2,  0},  /
298d0 2a 20 36 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  * 6 */.         
298d1 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38      { OP_String8
298d2 2c 20 20 20 20 20 20 30 2c 20 20 33 2c 20 20 30  ,      0,  3,  0
298d3 7d 2c 20 20 2f 2a 20 37 20 2a 2f 0a 20 20 20 20  },  /* 7 */.    
298d4 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f           { OP_Co
298d5 6e 63 61 74 2c 20 20 20 20 20 20 20 33 2c 20 20  ncat,       3,  
298d6 32 2c 20 20 32 7d 2c 0a 20 20 20 20 20 20 20 20  2,  2},.        
298d7 20 20 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74       { OP_Result
298d8 52 6f 77 2c 20 20 20 20 32 2c 20 20 31 2c 20 20  Row,    2,  1,  
298d9 30 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 7d 3b  0},.          };
298da 0a 20 20 20 20 20 20 20 20 20 20 61 64 64 72 20  .          addr 
298db 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
298dc 4f 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c  Op1(v, OP_IfPos,
298dd 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73   1);.          s
298de 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
298df 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 30 2c 20  (v, OP_Halt, 0, 
298e0 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  0);.          sq
298e1 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
298e2 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 20 20  e(v, addr);.    
298e3 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c        addr = sql
298e4 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73  ite3VdbeAddOpLis
298e5 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28 63  t(v, ArraySize(c
298e6 6e 74 49 64 78 29 2c 20 63 6e 74 49 64 78 29 3b  ntIdx), cntIdx);
298e7 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
298e8 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76  e3VdbeChangeP1(v
298e9 2c 20 61 64 64 72 2b 31 2c 20 6a 2b 32 29 3b 0a  , addr+1, j+2);.
298ea 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
298eb 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c  3VdbeChangeP2(v,
298ec 20 61 64 64 72 2b 31 2c 20 61 64 64 72 2b 34 29   addr+1, addr+4)
298ed 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
298ee 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28  te3VdbeChangeP1(
298ef 76 2c 20 61 64 64 72 2b 33 2c 20 6a 2b 32 29 3b  v, addr+3, j+2);
298f0 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
298f1 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76  e3VdbeChangeP2(v
298f2 2c 20 61 64 64 72 2b 33 2c 20 61 64 64 72 2b 32  , addr+3, addr+2
298f3 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
298f4 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
298f5 28 76 2c 20 61 64 64 72 2b 34 29 3b 0a 20 20 20  (v, addr+4);.   
298f6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
298f7 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64  beChangeP4(v, ad
298f8 64 72 2b 36 2c 20 0a 20 20 20 20 20 20 20 20 20  dr+6, .         
298f9 20 20 20 20 20 20 20 20 20 20 20 20 22 77 72 6f              "wro
298fa 6e 67 20 23 20 6f 66 20 65 6e 74 72 69 65 73 20  ng # of entries 
298fb 69 6e 20 69 6e 64 65 78 20 22 2c 20 50 34 5f 53  in index ", P4_S
298fc 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
298fd 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
298fe 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2b 37 2c  ngeP4(v, addr+7,
298ff 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 50 34   pIdx->zName, P4
29900 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
29901 20 20 7d 0a 20 20 20 20 20 20 7d 20 0a 20 20 20    }.      } .   
29902 20 7d 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71   }.    addr = sq
29903 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69  lite3VdbeAddOpLi
29904 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28  st(v, ArraySize(
29905 65 6e 64 43 6f 64 65 29 2c 20 65 6e 64 43 6f 64  endCode), endCod
29906 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  e);.    sqlite3V
29907 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61  dbeChangeP2(v, a
29908 64 64 72 2c 20 2d 6d 78 45 72 72 29 3b 0a 20 20  ddr, -mxErr);.  
29909 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2990a 70 48 65 72 65 28 76 2c 20 61 64 64 72 2b 31 29  pHere(v, addr+1)
2990b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2990c 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64  eChangeP4(v, add
2990d 72 2b 32 2c 20 22 6f 6b 22 2c 20 50 34 5f 53 54  r+2, "ok", P4_ST
2990e 41 54 49 43 29 3b 0a 20 20 7d 65 6c 73 65 0a 23  ATIC);.  }else.#
2990f 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
29910 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43  OMIT_INTEGRITY_C
29911 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  HECK */..#ifndef
29912 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
29913 31 36 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20 50  16.  /*.  **   P
29914 52 41 47 4d 41 20 65 6e 63 6f 64 69 6e 67 0a 20  RAGMA encoding. 
29915 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 65 6e 63   **   PRAGMA enc
29916 6f 64 69 6e 67 20 3d 20 22 75 74 66 2d 38 22 7c  oding = "utf-8"|
29917 22 75 74 66 2d 31 36 22 7c 22 75 74 66 2d 31 36  "utf-16"|"utf-16
29918 6c 65 22 7c 22 75 74 66 2d 31 36 62 65 22 0a 20  le"|"utf-16be". 
29919 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20 69 74 73 20   **.  ** In its 
2991a 66 69 72 73 74 20 66 6f 72 6d 2c 20 74 68 69 73  first form, this
2991b 20 70 72 61 67 6d 61 20 72 65 74 75 72 6e 73 20   pragma returns 
2991c 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20  the encoding of 
2991d 74 68 65 20 6d 61 69 6e 0a 20 20 2a 2a 20 64 61  the main.  ** da
2991e 74 61 62 61 73 65 2e 20 49 66 20 74 68 65 20 64  tabase. If the d
2991f 61 74 61 62 61 73 65 20 69 73 20 6e 6f 74 20 69  atabase is not i
29920 6e 69 74 69 61 6c 69 7a 65 64 2c 20 69 74 20 69  nitialized, it i
29921 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 6e 6f  s initialized no
29922 77 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  w..  **.  ** The
29923 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 6f 66 20   second form of 
29924 74 68 69 73 20 70 72 61 67 6d 61 20 69 73 20 61  this pragma is a
29925 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 6d 61   no-op if the ma
29926 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  in database file
29927 0a 20 20 2a 2a 20 68 61 73 20 6e 6f 74 20 61 6c  .  ** has not al
29928 72 65 61 64 79 20 62 65 65 6e 20 69 6e 69 74 69  ready been initi
29929 61 6c 69 7a 65 64 2e 20 49 6e 20 74 68 69 73 20  alized. In this 
2992a 63 61 73 65 20 69 74 20 73 65 74 73 20 74 68 65  case it sets the
2992b 20 64 65 66 61 75 6c 74 0a 20 20 2a 2a 20 65 6e   default.  ** en
2992c 63 6f 64 69 6e 67 20 74 68 61 74 20 77 69 6c 6c  coding that will
2992d 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65   be used for the
2992e 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66   main database f
2992f 69 6c 65 20 69 66 20 61 20 6e 65 77 20 66 69 6c  ile if a new fil
29930 65 0a 20 20 2a 2a 20 69 73 20 63 72 65 61 74 65  e.  ** is create
29931 64 2e 20 49 66 20 61 6e 20 65 78 69 73 74 69 6e  d. If an existin
29932 67 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  g main database 
29933 66 69 6c 65 20 69 73 20 6f 70 65 6e 65 64 2c 20  file is opened, 
29934 74 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20 64 65  then the.  ** de
29935 66 61 75 6c 74 20 74 65 78 74 20 65 6e 63 6f 64  fault text encod
29936 69 6e 67 20 66 6f 72 20 74 68 65 20 65 78 69 73  ing for the exis
29937 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 69 73  ting database is
29938 20 75 73 65 64 2e 0a 20 20 2a 2a 20 0a 20 20 2a   used..  ** .  *
29939 2a 20 49 6e 20 61 6c 6c 20 63 61 73 65 73 20 6e  * In all cases n
2993a 65 77 20 64 61 74 61 62 61 73 65 73 20 63 72 65  ew databases cre
2993b 61 74 65 64 20 75 73 69 6e 67 20 74 68 65 20 41  ated using the A
2993c 54 54 41 43 48 20 63 6f 6d 6d 61 6e 64 20 61 72  TTACH command ar
2993d 65 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20 74  e.  ** created t
2993e 6f 20 75 73 65 20 74 68 65 20 73 61 6d 65 20 64  o use the same d
2993f 65 66 61 75 6c 74 20 74 65 78 74 20 65 6e 63 6f  efault text enco
29940 64 69 6e 67 20 61 73 20 74 68 65 20 6d 61 69 6e  ding as the main
29941 20 64 61 74 61 62 61 73 65 2e 20 49 66 0a 20 20   database. If.  
29942 2a 2a 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  ** the main data
29943 62 61 73 65 20 68 61 73 20 6e 6f 74 20 62 65 65  base has not bee
29944 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 61 6e  n initialized an
29945 64 2f 6f 72 20 63 72 65 61 74 65 64 20 77 68 65  d/or created whe
29946 6e 20 41 54 54 41 43 48 0a 20 20 2a 2a 20 69 73  n ATTACH.  ** is
29947 20 65 78 65 63 75 74 65 64 2c 20 74 68 69 73 20   executed, this 
29948 69 73 20 64 6f 6e 65 20 62 65 66 6f 72 65 20 74  is done before t
29949 68 65 20 41 54 54 41 43 48 20 6f 70 65 72 61 74  he ATTACH operat
2994a 69 6f 6e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  ion..  **.  ** I
2994b 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 66 6f 72  n the second for
2994c 6d 20 74 68 69 73 20 70 72 61 67 6d 61 20 73 65  m this pragma se
2994d 74 73 20 74 68 65 20 74 65 78 74 20 65 6e 63 6f  ts the text enco
2994e 64 69 6e 67 20 74 6f 20 62 65 20 75 73 65 64 20  ding to be used 
2994f 69 6e 0a 20 20 2a 2a 20 6e 65 77 20 64 61 74 61  in.  ** new data
29950 62 61 73 65 20 66 69 6c 65 73 20 63 72 65 61 74  base files creat
29951 65 64 20 75 73 69 6e 67 20 74 68 69 73 20 64 61  ed using this da
29952 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 49  tabase handle. I
29953 74 20 69 73 20 6f 6e 6c 79 0a 20 20 2a 2a 20 75  t is only.  ** u
29954 73 65 66 75 6c 20 69 66 20 69 6e 76 6f 6b 65 64  seful if invoked
29955 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74   immediately aft
29956 65 72 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  er the main data
29957 62 61 73 65 20 69 0a 20 20 2a 2f 0a 20 20 69 66  base i.  */.  if
29958 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
29959 28 7a 4c 65 66 74 2c 20 22 65 6e 63 6f 64 69 6e  (zLeft, "encodin
2995a 67 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74  g")==0 ){.    st
2995b 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63  atic const struc
2995c 74 20 45 6e 63 4e 61 6d 65 20 7b 0a 20 20 20 20  t EncName {.    
2995d 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20    char *zName;. 
2995e 20 20 20 20 20 75 38 20 65 6e 63 3b 0a 20 20 20       u8 enc;.   
2995f 20 7d 20 65 6e 63 6e 61 6d 65 73 5b 5d 20 3d 20   } encnames[] = 
29960 7b 0a 20 20 20 20 20 20 7b 20 22 55 54 46 38 22  {.      { "UTF8"
29961 2c 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46  ,     SQLITE_UTF
29962 38 20 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20  8        },.    
29963 20 20 7b 20 22 55 54 46 2d 38 22 2c 20 20 20 20    { "UTF-8",    
29964 53 51 4c 49 54 45 5f 55 54 46 38 20 20 20 20 20  SQLITE_UTF8     
29965 20 20 20 7d 2c 20 20 2f 2a 20 4d 75 73 74 20 62     },  /* Must b
29966 65 20 65 6c 65 6d 65 6e 74 20 5b 31 5d 20 2a 2f  e element [1] */
29967 0a 20 20 20 20 20 20 7b 20 22 55 54 46 2d 31 36  .      { "UTF-16
29968 6c 65 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  le", SQLITE_UTF1
29969 36 4c 45 20 20 20 20 20 7d 2c 20 20 2f 2a 20 4d  6LE     },  /* M
2996a 75 73 74 20 62 65 20 65 6c 65 6d 65 6e 74 20 5b  ust be element [
2996b 32 5d 20 2a 2f 0a 20 20 20 20 20 20 7b 20 22 55  2] */.      { "U
2996c 54 46 2d 31 36 62 65 22 2c 20 53 51 4c 49 54 45  TF-16be", SQLITE
2996d 5f 55 54 46 31 36 42 45 20 20 20 20 20 7d 2c 20  _UTF16BE     }, 
2996e 20 2f 2a 20 4d 75 73 74 20 62 65 20 65 6c 65 6d   /* Must be elem
2996f 65 6e 74 20 5b 33 5d 20 2a 2f 0a 20 20 20 20 20  ent [3] */.     
29970 20 7b 20 22 55 54 46 31 36 6c 65 22 2c 20 20 53   { "UTF16le",  S
29971 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20  QLITE_UTF16LE   
29972 20 20 7d 2c 0a 20 20 20 20 20 20 7b 20 22 55 54    },.      { "UT
29973 46 31 36 62 65 22 2c 20 20 53 51 4c 49 54 45 5f  F16be",  SQLITE_
29974 55 54 46 31 36 42 45 20 20 20 20 20 7d 2c 0a 20  UTF16BE     },. 
29975 20 20 20 20 20 7b 20 22 55 54 46 2d 31 36 22 2c       { "UTF-16",
29976 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20     0            
29977 20 20 20 20 20 20 7d 2c 20 2f 2a 20 53 51 4c 49        }, /* SQLI
29978 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20 2a  TE_UTF16NATIVE *
29979 2f 0a 20 20 20 20 20 20 7b 20 22 55 54 46 31 36  /.      { "UTF16
2997a 22 2c 20 20 20 20 30 20 20 20 20 20 20 20 20 20  ",    0         
2997b 20 20 20 20 20 20 20 20 20 7d 2c 20 2f 2a 20 53           }, /* S
2997c 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
2997d 45 20 2a 2f 0a 20 20 20 20 20 20 7b 20 30 2c 20  E */.      { 0, 
2997e 30 20 7d 0a 20 20 20 20 7d 3b 0a 20 20 20 20 63  0 }.    };.    c
2997f 6f 6e 73 74 20 73 74 72 75 63 74 20 45 6e 63 4e  onst struct EncN
29980 61 6d 65 20 2a 70 45 6e 63 3b 0a 20 20 20 20 69  ame *pEnc;.    i
29981 66 28 20 21 7a 52 69 67 68 74 20 29 7b 20 20 20  f( !zRight ){   
29982 20 2f 2a 20 22 50 52 41 47 4d 41 20 65 6e 63 6f   /* "PRAGMA enco
29983 64 69 6e 67 22 20 2a 2f 0a 20 20 20 20 20 20 69  ding" */.      i
29984 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63  f( sqlite3ReadSc
29985 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67  hema(pParse) ) g
29986 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a  oto pragma_out;.
29987 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
29988 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31  eSetNumCols(v, 1
29989 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2998a 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
2998b 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
2998c 45 2c 20 22 65 6e 63 6f 64 69 6e 67 22 2c 20 53  E, "encoding", S
2998d 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
2998e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2998f 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 74 72  AddOp2(v, OP_Str
29990 69 6e 67 38 2c 20 30 2c 20 31 29 3b 0a 20 20 20  ing8, 0, 1);.   
29991 20 20 20 61 73 73 65 72 74 28 20 65 6e 63 6e 61     assert( encna
29992 6d 65 73 5b 53 51 4c 49 54 45 5f 55 54 46 38 5d  mes[SQLITE_UTF8]
29993 2e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46  .enc==SQLITE_UTF
29994 38 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  8 );.      asser
29995 74 28 20 65 6e 63 6e 61 6d 65 73 5b 53 51 4c 49  t( encnames[SQLI
29996 54 45 5f 55 54 46 31 36 4c 45 5d 2e 65 6e 63 3d  TE_UTF16LE].enc=
29997 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20  =SQLITE_UTF16LE 
29998 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
29999 20 65 6e 63 6e 61 6d 65 73 5b 53 51 4c 49 54 45   encnames[SQLITE
2999a 5f 55 54 46 31 36 42 45 5d 2e 65 6e 63 3d 3d 53  _UTF16BE].enc==S
2999b 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b  QLITE_UTF16BE );
2999c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2999d 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31  beChangeP4(v, -1
2999e 2c 20 65 6e 63 6e 61 6d 65 73 5b 45 4e 43 28 70  , encnames[ENC(p
2999f 50 61 72 73 65 2d 3e 64 62 29 5d 2e 7a 4e 61 6d  Parse->db)].zNam
299a0 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  e, P4_STATIC);. 
299a1 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
299a2 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
299a3 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20  ultRow, 1, 1);. 
299a4 20 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20     }else{       
299a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
299a6 20 2f 2a 20 22 50 52 41 47 4d 41 20 65 6e 63 6f   /* "PRAGMA enco
299a7 64 69 6e 67 20 3d 20 58 58 58 22 20 2a 2f 0a 20  ding = XXX" */. 
299a8 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 63 68 61       /* Only cha
299a9 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66  nge the value of
299aa 20 73 71 6c 69 74 65 2e 65 6e 63 20 69 66 20 74   sqlite.enc if t
299ab 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
299ac 6c 65 20 69 73 20 6e 6f 74 0a 20 20 20 20 20 20  le is not.      
299ad 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20  ** initialized. 
299ae 49 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  If the main data
299af 62 61 73 65 20 65 78 69 73 74 73 2c 20 74 68 65  base exists, the
299b0 20 6e 65 77 20 73 71 6c 69 74 65 2e 65 6e 63 20   new sqlite.enc 
299b1 76 61 6c 75 65 0a 20 20 20 20 20 20 2a 2a 20 77  value.      ** w
299b2 69 6c 6c 20 62 65 20 6f 76 65 72 77 72 69 74 74  ill be overwritt
299b3 65 6e 20 77 68 65 6e 20 74 68 65 20 73 63 68 65  en when the sche
299b4 6d 61 20 69 73 20 6e 65 78 74 20 6c 6f 61 64 65  ma is next loade
299b5 64 2e 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f  d. If it does no
299b6 74 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72 65 61  t.      ** alrea
299b7 64 79 20 65 78 69 73 74 73 2c 20 69 74 20 77 69  dy exists, it wi
299b8 6c 6c 20 62 65 20 63 72 65 61 74 65 64 20 74 6f  ll be created to
299b9 20 75 73 65 20 74 68 65 20 6e 65 77 20 65 6e 63   use the new enc
299ba 6f 64 69 6e 67 20 76 61 6c 75 65 2e 0a 20 20 20  oding value..   
299bb 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
299bc 0a 20 20 20 20 20 20 20 20 21 28 44 62 48 61 73  .        !(DbHas
299bd 50 72 6f 70 65 72 74 79 28 64 62 2c 20 30 2c 20  Property(db, 0, 
299be 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29  DB_SchemaLoaded)
299bf 29 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 44 62  ) || .        Db
299c0 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20  HasProperty(db, 
299c1 30 2c 20 44 42 5f 45 6d 70 74 79 29 20 0a 20 20  0, DB_Empty) .  
299c2 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 66      ){.        f
299c3 6f 72 28 70 45 6e 63 3d 26 65 6e 63 6e 61 6d 65  or(pEnc=&encname
299c4 73 5b 30 5d 3b 20 70 45 6e 63 2d 3e 7a 4e 61 6d  s[0]; pEnc->zNam
299c5 65 3b 20 70 45 6e 63 2b 2b 29 7b 0a 20 20 20 20  e; pEnc++){.    
299c6 20 20 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c        if( 0==sql
299c7 69 74 65 33 53 74 72 49 43 6d 70 28 7a 52 69 67  ite3StrICmp(zRig
299c8 68 74 2c 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65 29  ht, pEnc->zName)
299c9 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
299ca 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 20  ENC(pParse->db) 
299cb 3d 20 70 45 6e 63 2d 3e 65 6e 63 20 3f 20 70 45  = pEnc->enc ? pE
299cc 6e 63 2d 3e 65 6e 63 20 3a 20 53 51 4c 49 54 45  nc->enc : SQLITE
299cd 5f 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20 20  _UTF16NATIVE;.  
299ce 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
299cf 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
299d0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
299d1 66 28 20 21 70 45 6e 63 2d 3e 7a 4e 61 6d 65 20  f( !pEnc->zName 
299d2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  ){.          sql
299d3 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
299d4 72 73 65 2c 20 22 75 6e 73 75 70 70 6f 72 74 65  rse, "unsupporte
299d5 64 20 65 6e 63 6f 64 69 6e 67 3a 20 25 73 22 2c  d encoding: %s",
299d6 20 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20   zRight);.      
299d7 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
299d8 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66  }.  }else.#endif
299d9 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
299da 55 54 46 31 36 20 2a 2f 0a 0a 23 69 66 6e 64 65  UTF16 */..#ifnde
299db 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43  f SQLITE_OMIT_SC
299dc 48 45 4d 41 5f 56 45 52 53 49 4f 4e 5f 50 52 41  HEMA_VERSION_PRA
299dd 47 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20  GMAS.  /*.  **  
299de 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
299df 65 2e 5d 73 63 68 65 6d 61 5f 76 65 72 73 69 6f  e.]schema_versio
299e0 6e 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20  n.  **   PRAGMA 
299e1 5b 64 61 74 61 62 61 73 65 2e 5d 73 63 68 65 6d  [database.]schem
299e2 61 5f 76 65 72 73 69 6f 6e 20 3d 20 3c 69 6e 74  a_version = <int
299e3 65 67 65 72 3e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  eger>.  **.  ** 
299e4 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61    PRAGMA [databa
299e5 73 65 2e 5d 75 73 65 72 5f 76 65 72 73 69 6f 6e  se.]user_version
299e6 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b  .  **   PRAGMA [
299e7 64 61 74 61 62 61 73 65 2e 5d 75 73 65 72 5f 76  database.]user_v
299e8 65 72 73 69 6f 6e 20 3d 20 3c 69 6e 74 65 67 65  ersion = <intege
299e9 72 3e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  r>.  **.  ** The
299ea 20 70 72 61 67 6d 61 27 73 20 73 63 68 65 6d 61   pragma's schema
299eb 5f 76 65 72 73 69 6f 6e 20 61 6e 64 20 75 73 65  _version and use
299ec 72 5f 76 65 72 73 69 6f 6e 20 61 72 65 20 75 73  r_version are us
299ed 65 64 20 74 6f 20 73 65 74 20 6f 72 20 67 65 74  ed to set or get
299ee 0a 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20  .  ** the value 
299ef 6f 66 20 74 68 65 20 73 63 68 65 6d 61 2d 76 65  of the schema-ve
299f0 72 73 69 6f 6e 20 61 6e 64 20 75 73 65 72 2d 76  rsion and user-v
299f1 65 72 73 69 6f 6e 2c 20 72 65 73 70 65 63 74 69  ersion, respecti
299f2 76 65 6c 79 2e 20 42 6f 74 68 0a 20 20 2a 2a 20  vely. Both.  ** 
299f3 74 68 65 20 73 63 68 65 6d 61 2d 76 65 72 73 69  the schema-versi
299f4 6f 6e 20 61 6e 64 20 74 68 65 20 75 73 65 72 2d  on and the user-
299f5 76 65 72 73 69 6f 6e 20 61 72 65 20 33 32 2d 62  version are 32-b
299f6 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  it signed intege
299f7 72 73 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20 69  rs.  ** stored i
299f8 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  n the database h
299f9 65 61 64 65 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  eader..  **.  **
299fa 20 54 68 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b   The schema-cook
299fb 69 65 20 69 73 20 75 73 75 61 6c 6c 79 20 6f 6e  ie is usually on
299fc 6c 79 20 6d 61 6e 69 70 75 6c 61 74 65 64 20 69  ly manipulated i
299fd 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20 53 51 4c  nternally by SQL
299fe 69 74 65 2e 20 49 74 0a 20 20 2a 2a 20 69 73 20  ite. It.  ** is 
299ff 69 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 53  incremented by S
29a00 51 4c 69 74 65 20 77 68 65 6e 65 76 65 72 20 74  QLite whenever t
29a01 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
29a02 6d 61 20 69 73 20 6d 6f 64 69 66 69 65 64 20 28  ma is modified (
29a03 62 79 0a 20 20 2a 2a 20 63 72 65 61 74 69 6e 67  by.  ** creating
29a04 20 6f 72 20 64 72 6f 70 70 69 6e 67 20 61 20 74   or dropping a t
29a05 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 29 2e 20  able or index). 
29a06 54 68 65 20 73 63 68 65 6d 61 20 76 65 72 73 69  The schema versi
29a07 6f 6e 20 69 73 20 75 73 65 64 20 62 79 0a 20 20  on is used by.  
29a08 2a 2a 20 53 51 4c 69 74 65 20 65 61 63 68 20 74  ** SQLite each t
29a09 69 6d 65 20 61 20 71 75 65 72 79 20 69 73 20 65  ime a query is e
29a0a 78 65 63 75 74 65 64 20 74 6f 20 65 6e 73 75 72  xecuted to ensur
29a0b 65 20 74 68 61 74 20 74 68 65 20 69 6e 74 65 72  e that the inter
29a0c 6e 61 6c 20 63 61 63 68 65 0a 20 20 2a 2a 20 6f  nal cache.  ** o
29a0d 66 20 74 68 65 20 73 63 68 65 6d 61 20 75 73 65  f the schema use
29a0e 64 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67  d when compiling
29a0f 20 74 68 65 20 53 51 4c 20 71 75 65 72 79 20 6d   the SQL query m
29a10 61 74 63 68 65 73 20 74 68 65 20 73 63 68 65 6d  atches the schem
29a11 61 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 64 61  a of.  ** the da
29a12 74 61 62 61 73 65 20 61 67 61 69 6e 73 74 20 77  tabase against w
29a13 68 69 63 68 20 74 68 65 20 63 6f 6d 70 69 6c 65  hich the compile
29a14 64 20 71 75 65 72 79 20 69 73 20 61 63 74 75 61  d query is actua
29a15 6c 6c 79 20 65 78 65 63 75 74 65 64 2e 0a 20 20  lly executed..  
29a16 2a 2a 20 53 75 62 76 65 72 74 69 6e 67 20 74 68  ** Subverting th
29a17 69 73 20 6d 65 63 68 61 6e 69 73 6d 20 62 79 20  is mechanism by 
29a18 75 73 69 6e 67 20 22 50 52 41 47 4d 41 20 73 63  using "PRAGMA sc
29a19 68 65 6d 61 5f 76 65 72 73 69 6f 6e 22 20 74 6f  hema_version" to
29a1a 20 6d 6f 64 69 66 79 0a 20 20 2a 2a 20 74 68 65   modify.  ** the
29a1b 20 73 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20   schema-version 
29a1c 69 73 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 64  is potentially d
29a1d 61 6e 67 65 72 6f 75 73 20 61 6e 64 20 6d 61 79  angerous and may
29a1e 20 6c 65 61 64 20 74 6f 20 70 72 6f 67 72 61 6d   lead to program
29a1f 0a 20 20 2a 2a 20 63 72 61 73 68 65 73 20 6f 72  .  ** crashes or
29a20 20 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70   database corrup
29a21 74 69 6f 6e 2e 20 55 73 65 20 77 69 74 68 20 63  tion. Use with c
29a22 61 75 74 69 6f 6e 21 0a 20 20 2a 2a 0a 20 20 2a  aution!.  **.  *
29a23 2a 20 54 68 65 20 75 73 65 72 2d 76 65 72 73 69  * The user-versi
29a24 6f 6e 20 69 73 20 6e 6f 74 20 75 73 65 64 20 69  on is not used i
29a25 6e 74 65 72 6e 61 6c 6c 79 20 62 79 20 53 51 4c  nternally by SQL
29a26 69 74 65 2e 20 49 74 20 6d 61 79 20 62 65 20 75  ite. It may be u
29a27 73 65 64 20 62 79 0a 20 20 2a 2a 20 61 70 70 6c  sed by.  ** appl
29a28 69 63 61 74 69 6f 6e 73 20 66 6f 72 20 61 6e 79  ications for any
29a29 20 70 75 72 70 6f 73 65 2e 0a 20 20 2a 2f 0a 20   purpose..  */. 
29a2a 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
29a2b 43 6d 70 28 7a 4c 65 66 74 2c 20 22 73 63 68 65  Cmp(zLeft, "sche
29a2c 6d 61 5f 76 65 72 73 69 6f 6e 22 29 3d 3d 30 20  ma_version")==0 
29a2d 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 53 74  .   || sqlite3St
29a2e 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 75 73  rICmp(zLeft, "us
29a2f 65 72 5f 76 65 72 73 69 6f 6e 22 29 3d 3d 30 20  er_version")==0 
29a30 0a 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 53 74  .   || sqlite3St
29a31 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 66 72  rICmp(zLeft, "fr
29a32 65 65 6c 69 73 74 5f 63 6f 75 6e 74 22 29 3d 3d  eelist_count")==
29a33 30 20 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20  0 .  ){.    int 
29a34 69 43 6f 6f 6b 69 65 3b 20 20 20 2f 2a 20 43 6f  iCookie;   /* Co
29a35 6f 6b 69 65 20 69 6e 64 65 78 2e 20 31 20 66 6f  okie index. 1 fo
29a36 72 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 2c  r schema-cookie,
29a37 20 36 20 66 6f 72 20 75 73 65 72 2d 63 6f 6f 6b   6 for user-cook
29a38 69 65 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  ie. */.    sqlit
29a39 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28  e3VdbeUsesBtree(
29a3a 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 77 69  v, iDb);.    swi
29a3b 74 63 68 28 20 7a 4c 65 66 74 5b 30 5d 20 29 7b  tch( zLeft[0] ){
29a3c 0a 20 20 20 20 20 20 63 61 73 65 20 27 66 27 3a  .      case 'f':
29a3d 20 63 61 73 65 20 27 46 27 3a 0a 20 20 20 20 20   case 'F':.     
29a3e 20 20 20 69 43 6f 6f 6b 69 65 20 3d 20 42 54 52     iCookie = BTR
29a3f 45 45 5f 46 52 45 45 5f 50 41 47 45 5f 43 4f 55  EE_FREE_PAGE_COU
29a40 4e 54 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  NT;.        brea
29a41 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 27 73  k;.      case 's
29a42 27 3a 20 63 61 73 65 20 27 53 27 3a 0a 20 20 20  ': case 'S':.   
29a43 20 20 20 20 20 69 43 6f 6f 6b 69 65 20 3d 20 42       iCookie = B
29a44 54 52 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 53  TREE_SCHEMA_VERS
29a45 49 4f 4e 3b 0a 20 20 20 20 20 20 20 20 62 72 65  ION;.        bre
29a46 61 6b 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c  ak;.      defaul
29a47 74 3a 0a 20 20 20 20 20 20 20 20 69 43 6f 6f 6b  t:.        iCook
29a48 69 65 20 3d 20 42 54 52 45 45 5f 55 53 45 52 5f  ie = BTREE_USER_
29a49 56 45 52 53 49 4f 4e 3b 0a 20 20 20 20 20 20 20  VERSION;.       
29a4a 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
29a4b 20 20 20 69 66 28 20 7a 52 69 67 68 74 20 26 26     if( zRight &&
29a4c 20 69 43 6f 6f 6b 69 65 21 3d 42 54 52 45 45 5f   iCookie!=BTREE_
29a4d 46 52 45 45 5f 50 41 47 45 5f 43 4f 55 4e 54 20  FREE_PAGE_COUNT 
29a4e 29 7b 0a 20 20 20 20 20 20 2f 2a 20 57 72 69 74  ){.      /* Writ
29a4f 65 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  e the specified 
29a50 63 6f 6f 6b 69 65 20 76 61 6c 75 65 20 2a 2f 0a  cookie value */.
29a51 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e        static con
29a52 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 73 65  st VdbeOpList se
29a53 74 43 6f 6f 6b 69 65 5b 5d 20 3d 20 7b 0a 20 20  tCookie[] = {.  
29a54 20 20 20 20 20 20 7b 20 4f 50 5f 54 72 61 6e 73        { OP_Trans
29a55 61 63 74 69 6f 6e 2c 20 20 20 20 30 2c 20 20 31  action,    0,  1
29a56 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a  ,  0},    /* 0 *
29a57 2f 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49  /.        { OP_I
29a58 6e 74 65 67 65 72 2c 20 20 20 20 20 20 20 20 30  nteger,        0
29a59 2c 20 20 31 2c 20 20 30 7d 2c 20 20 20 20 2f 2a  ,  1,  0},    /*
29a5a 20 31 20 2a 2f 0a 20 20 20 20 20 20 20 20 7b 20   1 */.        { 
29a5b 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 20 20  OP_SetCookie,   
29a5c 20 20 20 30 2c 20 20 30 2c 20 20 31 7d 2c 20 20     0,  0,  1},  
29a5d 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20 20 20 20    /* 2 */.      
29a5e 7d 3b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64  };.      int add
29a5f 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
29a60 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61  ddOpList(v, Arra
29a61 79 53 69 7a 65 28 73 65 74 43 6f 6f 6b 69 65 29  ySize(setCookie)
29a62 2c 20 73 65 74 43 6f 6f 6b 69 65 29 3b 0a 20 20  , setCookie);.  
29a63 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
29a64 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2c  hangeP1(v, addr,
29a65 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c   iDb);.      sql
29a66 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31  ite3VdbeChangeP1
29a67 28 76 2c 20 61 64 64 72 2b 31 2c 20 61 74 6f 69  (v, addr+1, atoi
29a68 28 7a 52 69 67 68 74 29 29 3b 0a 20 20 20 20 20  (zRight));.     
29a69 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
29a6a 67 65 50 31 28 76 2c 20 61 64 64 72 2b 32 2c 20  geP1(v, addr+2, 
29a6b 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  iDb);.      sqli
29a6c 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
29a6d 76 2c 20 61 64 64 72 2b 32 2c 20 69 43 6f 6f 6b  v, addr+2, iCook
29a6e 69 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  ie);.    }else{.
29a6f 20 20 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68        /* Read th
29a70 65 20 73 70 65 63 69 66 69 65 64 20 63 6f 6f 6b  e specified cook
29a71 69 65 20 76 61 6c 75 65 20 2a 2f 0a 20 20 20 20  ie value */.    
29a72 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 56    static const V
29a73 64 62 65 4f 70 4c 69 73 74 20 72 65 61 64 43 6f  dbeOpList readCo
29a74 6f 6b 69 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  okie[] = {.     
29a75 20 20 20 7b 20 4f 50 5f 54 72 61 6e 73 61 63 74     { OP_Transact
29a76 69 6f 6e 2c 20 20 20 20 20 30 2c 20 20 30 2c 20  ion,     0,  0, 
29a77 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20 2a 2f 0a   0},    /* 0 */.
29a78 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 61          { OP_Rea
29a79 64 43 6f 6f 6b 69 65 2c 20 20 20 20 20 20 30 2c  dCookie,      0,
29a7a 20 20 31 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20    1,  0},    /* 
29a7b 31 20 2a 2f 0a 20 20 20 20 20 20 20 20 7b 20 4f  1 */.        { O
29a7c 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 20 20 20  P_ResultRow,    
29a7d 20 20 20 31 2c 20 20 31 2c 20 20 30 7d 0a 20 20     1,  1,  0}.  
29a7e 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 69 6e 74      };.      int
29a7f 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
29a80 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20  dbeAddOpList(v, 
29a81 41 72 72 61 79 53 69 7a 65 28 72 65 61 64 43 6f  ArraySize(readCo
29a82 6f 6b 69 65 29 2c 20 72 65 61 64 43 6f 6f 6b 69  okie), readCooki
29a83 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e);.      sqlite
29a84 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c  3VdbeChangeP1(v,
29a85 20 61 64 64 72 2c 20 69 44 62 29 3b 0a 20 20 20   addr, iDb);.   
29a86 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
29a87 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2b 31  angeP1(v, addr+1
29a88 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71  , iDb);.      sq
29a89 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
29a8a 33 28 76 2c 20 61 64 64 72 2b 31 2c 20 69 43 6f  3(v, addr+1, iCo
29a8b 6f 6b 69 65 29 3b 0a 20 20 20 20 20 20 73 71 6c  okie);.      sql
29a8c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
29a8d 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 20 20  ls(v, 1);.      
29a8e 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
29a8f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
29a90 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4c 65 66 74 2c  AME_NAME, zLeft,
29a91 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
29a92 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  T);.    }.  }els
29a93 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  e.#endif /* SQLI
29a94 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f 56  TE_OMIT_SCHEMA_V
29a95 45 52 53 49 4f 4e 5f 50 52 41 47 4d 41 53 20 2a  ERSION_PRAGMAS *
29a96 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  /..#if defined(S
29a97 51 4c 49 54 45 5f 44 45 42 55 47 29 20 7c 7c 20  QLITE_DEBUG) || 
29a98 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 54  defined(SQLITE_T
29a99 45 53 54 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52  EST).  /*.  ** R
29a9a 65 70 6f 72 74 20 74 68 65 20 63 75 72 72 65 6e  eport the curren
29a9b 74 20 73 74 61 74 65 20 6f 66 20 66 69 6c 65 20  t state of file 
29a9c 6c 6f 67 73 20 66 6f 72 20 61 6c 6c 20 64 61 74  logs for all dat
29a9d 61 62 61 73 65 73 0a 20 20 2a 2f 0a 20 20 69 66  abases.  */.  if
29a9e 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
29a9f 28 7a 4c 65 66 74 2c 20 22 6c 6f 63 6b 5f 73 74  (zLeft, "lock_st
29aa0 61 74 75 73 22 29 3d 3d 30 20 29 7b 0a 20 20 20  atus")==0 ){.   
29aa1 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
29aa2 61 72 20 2a 63 6f 6e 73 74 20 61 7a 4c 6f 63 6b  ar *const azLock
29aa3 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  Name[] = {.     
29aa4 20 22 75 6e 6c 6f 63 6b 65 64 22 2c 20 22 73 68   "unlocked", "sh
29aa5 61 72 65 64 22 2c 20 22 72 65 73 65 72 76 65 64  ared", "reserved
29aa6 22 2c 20 22 70 65 6e 64 69 6e 67 22 2c 20 22 65  ", "pending", "e
29aa7 78 63 6c 75 73 69 76 65 22 0a 20 20 20 20 7d 3b  xclusive".    };
29aa8 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
29aa9 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
29aaa 6d 43 6f 6c 73 28 76 2c 20 32 29 3b 0a 20 20 20  mCols(v, 2);.   
29aab 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20   pParse->nMem = 
29aac 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  2;.    sqlite3Vd
29aad 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
29aae 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
29aaf 20 22 64 61 74 61 62 61 73 65 22 2c 20 53 51 4c   "database", SQL
29ab0 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
29ab1 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
29ab2 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c  olName(v, 1, COL
29ab3 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 74 61 74  NAME_NAME, "stat
29ab4 75 73 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  us", SQLITE_STAT
29ab5 49 43 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  IC);.    for(i=0
29ab6 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b  ; i<db->nDb; i++
29ab7 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65 20 2a  ){.      Btree *
29ab8 70 42 74 3b 0a 20 20 20 20 20 20 50 61 67 65 72  pBt;.      Pager
29ab9 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 20 20   *pPager;.      
29aba 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 61  const char *zSta
29abb 74 65 20 3d 20 22 75 6e 6b 6e 6f 77 6e 22 3b 0a  te = "unknown";.
29abc 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20        int j;.   
29abd 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b 69     if( db->aDb[i
29abe 5d 2e 7a 4e 61 6d 65 3d 3d 30 20 29 20 63 6f 6e  ].zName==0 ) con
29abf 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 73 71 6c  tinue;.      sql
29ac0 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
29ac1 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
29ac2 20 31 2c 20 30 2c 20 64 62 2d 3e 61 44 62 5b 69   1, 0, db->aDb[i
29ac3 5d 2e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54  ].zName, P4_STAT
29ac4 49 43 29 3b 0a 20 20 20 20 20 20 70 42 74 20 3d  IC);.      pBt =
29ac5 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b   db->aDb[i].pBt;
29ac6 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 3d 3d  .      if( pBt==
29ac7 30 20 7c 7c 20 28 70 50 61 67 65 72 20 3d 20 73  0 || (pPager = s
29ac8 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72  qlite3BtreePager
29ac9 28 70 42 74 29 29 3d 3d 30 20 29 7b 0a 20 20 20  (pBt))==0 ){.   
29aca 20 20 20 20 20 7a 53 74 61 74 65 20 3d 20 22 63       zState = "c
29acb 6c 6f 73 65 64 22 3b 0a 20 20 20 20 20 20 7d 65  losed";.      }e
29acc 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f  lse if( sqlite3_
29acd 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 64 62 2c  file_control(db,
29ace 20 69 20 3f 20 64 62 2d 3e 61 44 62 5b 69 5d 2e   i ? db->aDb[i].
29acf 7a 4e 61 6d 65 20 3a 20 30 2c 20 0a 20 20 20 20  zName : 0, .    
29ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29ad1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29ad2 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f   SQLITE_FCNTL_LO
29ad3 43 4b 53 54 41 54 45 2c 20 26 6a 29 3d 3d 53 51  CKSTATE, &j)==SQ
29ad4 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
29ad5 20 20 20 20 7a 53 74 61 74 65 20 3d 20 61 7a 4c      zState = azL
29ad6 6f 63 6b 4e 61 6d 65 5b 6a 5d 3b 0a 20 20 20 20  ockName[j];.    
29ad7 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
29ad8 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
29ad9 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 32 2c  P_String8, 0, 2,
29ada 20 30 2c 20 7a 53 74 61 74 65 2c 20 50 34 5f 53   0, zState, P4_S
29adb 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71  TATIC);.      sq
29adc 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
29add 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
29ade 20 31 2c 20 32 29 3b 0a 20 20 20 20 7d 0a 0a 20   1, 2);.    }.. 
29adf 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 23   }else.#endif..#
29ae0 69 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f  if SQLITE_HAS_CO
29ae1 44 45 43 0a 20 20 69 66 28 20 73 71 6c 69 74 65  DEC.  if( sqlite
29ae2 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20  3StrICmp(zLeft, 
29ae3 22 6b 65 79 22 29 3d 3d 30 20 26 26 20 7a 52 69  "key")==0 && zRi
29ae4 67 68 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ght ){.    sqlit
29ae5 65 33 5f 6b 65 79 28 64 62 2c 20 7a 52 69 67 68  e3_key(db, zRigh
29ae6 74 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  t, sqlite3Strlen
29ae7 33 30 28 7a 52 69 67 68 74 29 29 3b 0a 20 20 7d  30(zRight));.  }
29ae8 65 6c 73 65 0a 20 20 69 66 28 20 73 71 6c 69 74  else.  if( sqlit
29ae9 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
29aea 20 22 72 65 6b 65 79 22 29 3d 3d 30 20 26 26 20   "rekey")==0 && 
29aeb 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 73 71  zRight ){.    sq
29aec 6c 69 74 65 33 5f 72 65 6b 65 79 28 64 62 2c 20  lite3_rekey(db, 
29aed 7a 52 69 67 68 74 2c 20 73 71 6c 69 74 65 33 53  zRight, sqlite3S
29aee 74 72 6c 65 6e 33 30 28 7a 52 69 67 68 74 29 29  trlen30(zRight))
29aef 3b 0a 20 20 7d 65 6c 73 65 0a 20 20 69 66 28 20  ;.  }else.  if( 
29af0 7a 52 69 67 68 74 20 26 26 20 28 73 71 6c 69 74  zRight && (sqlit
29af1 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
29af2 20 22 68 65 78 6b 65 79 22 29 3d 3d 30 20 7c 7c   "hexkey")==0 ||
29af3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
29af4 20 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70    sqlite3StrICmp
29af5 28 7a 4c 65 66 74 2c 20 22 68 65 78 72 65 6b 65  (zLeft, "hexreke
29af6 79 22 29 3d 3d 30 29 20 29 7b 0a 20 20 20 20 69  y")==0) ){.    i
29af7 6e 74 20 69 2c 20 68 31 2c 20 68 32 3b 0a 20 20  nt i, h1, h2;.  
29af8 20 20 63 68 61 72 20 7a 4b 65 79 5b 34 30 5d 3b    char zKey[40];
29af9 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 28 68  .    for(i=0; (h
29afa 31 20 3d 20 7a 52 69 67 68 74 5b 69 5d 29 21 3d  1 = zRight[i])!=
29afb 30 20 26 26 20 28 68 32 20 3d 20 7a 52 69 67 68  0 && (h2 = zRigh
29afc 74 5b 69 2b 31 5d 29 21 3d 30 3b 20 69 2b 3d 32  t[i+1])!=0; i+=2
29afd 29 7b 0a 20 20 20 20 20 20 68 31 20 2b 3d 20 39  ){.      h1 += 9
29afe 2a 28 31 26 28 68 31 3e 3e 36 29 29 3b 0a 20 20  *(1&(h1>>6));.  
29aff 20 20 20 20 68 32 20 2b 3d 20 39 2a 28 31 26 28      h2 += 9*(1&(
29b00 68 32 3e 3e 36 29 29 3b 0a 20 20 20 20 20 20 7a  h2>>6));.      z
29b01 4b 65 79 5b 69 2f 32 5d 20 3d 20 28 68 32 20 26  Key[i/2] = (h2 &
29b02 20 30 78 30 66 29 20 7c 20 28 28 68 31 20 26 20   0x0f) | ((h1 & 
29b03 30 78 66 29 3c 3c 34 29 3b 0a 20 20 20 20 7d 0a  0xf)<<4);.    }.
29b04 20 20 20 20 69 66 28 20 28 7a 4c 65 66 74 5b 33      if( (zLeft[3
29b05 5d 20 26 20 30 78 66 29 3d 3d 30 78 62 20 29 7b  ] & 0xf)==0xb ){
29b06 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6b  .      sqlite3_k
29b07 65 79 28 64 62 2c 20 7a 4b 65 79 2c 20 69 2f 32  ey(db, zKey, i/2
29b08 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
29b09 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 6b 65      sqlite3_reke
29b0a 79 28 64 62 2c 20 7a 4b 65 79 2c 20 69 2f 32 29  y(db, zKey, i/2)
29b0b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  ;.    }.  }else.
29b0c 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
29b0d 45 5f 48 41 53 5f 43 4f 44 45 43 20 7c 7c 20 64  E_HAS_CODEC || d
29b0e 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
29b0f 41 42 4c 45 5f 43 45 52 4f 44 29 0a 20 20 69 66  ABLE_CEROD).  if
29b10 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
29b11 28 7a 4c 65 66 74 2c 20 22 61 63 74 69 76 61 74  (zLeft, "activat
29b12 65 5f 65 78 74 65 6e 73 69 6f 6e 73 22 29 3d 3d  e_extensions")==
29b13 30 20 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f  0 ){.#if SQLITE_
29b14 48 41 53 5f 43 4f 44 45 43 0a 20 20 20 20 69 66  HAS_CODEC.    if
29b15 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  ( sqlite3StrNICm
29b16 70 28 7a 52 69 67 68 74 2c 20 22 73 65 65 2d 22  p(zRight, "see-"
29b17 2c 20 34 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  , 4)==0 ){.     
29b18 20 65 78 74 65 72 6e 20 76 6f 69 64 20 73 71 6c   extern void sql
29b19 69 74 65 33 5f 61 63 74 69 76 61 74 65 5f 73 65  ite3_activate_se
29b1a 65 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  e(const char*);.
29b1b 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 63        sqlite3_ac
29b1c 74 69 76 61 74 65 5f 73 65 65 28 26 7a 52 69 67  tivate_see(&zRig
29b1d 68 74 5b 34 5d 29 3b 0a 20 20 20 20 7d 0a 23 65  ht[4]);.    }.#e
29b1e 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
29b1f 54 45 5f 45 4e 41 42 4c 45 5f 43 45 52 4f 44 0a  TE_ENABLE_CEROD.
29b20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
29b21 74 72 4e 49 43 6d 70 28 7a 52 69 67 68 74 2c 20  trNICmp(zRight, 
29b22 22 63 65 72 6f 64 2d 22 2c 20 36 29 3d 3d 30 20  "cerod-", 6)==0 
29b23 29 7b 0a 20 20 20 20 20 20 65 78 74 65 72 6e 20  ){.      extern 
29b24 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 61 63 74  void sqlite3_act
29b25 69 76 61 74 65 5f 63 65 72 6f 64 28 63 6f 6e 73  ivate_cerod(cons
29b26 74 20 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20  t char*);.      
29b27 73 71 6c 69 74 65 33 5f 61 63 74 69 76 61 74 65  sqlite3_activate
29b28 5f 63 65 72 6f 64 28 26 7a 52 69 67 68 74 5b 36  _cerod(&zRight[6
29b29 5d 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  ]);.    }.#endif
29b2a 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a  .  }else.#endif.
29b2b 0a 20 0a 20 20 7b 2f 2a 20 45 6d 70 74 79 20 45  . .  {/* Empty E
29b2c 4c 53 45 20 63 6c 61 75 73 65 20 2a 2f 7d 0a 0a  LSE clause */}..
29b2d 20 20 2f 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f    /* Code an OP_
29b2e 45 78 70 69 72 65 20 61 74 20 74 68 65 20 65 6e  Expire at the en
29b2f 64 20 6f 66 20 65 61 63 68 20 50 52 41 47 4d 41  d of each PRAGMA
29b30 20 70 72 6f 67 72 61 6d 20 74 6f 20 63 61 75 73   program to caus
29b31 65 0a 20 20 2a 2a 20 74 68 65 20 56 44 42 45 20  e.  ** the VDBE 
29b32 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 65  implementing the
29b33 20 70 72 61 67 6d 61 20 74 6f 20 65 78 70 69 72   pragma to expir
29b34 65 2e 20 4d 6f 73 74 20 28 61 6c 6c 3f 29 20 70  e. Most (all?) p
29b35 72 61 67 6d 61 73 0a 20 20 2a 2a 20 61 72 65 20  ragmas.  ** are 
29b36 6f 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72 20 61  only valid for a
29b37 20 73 69 6e 67 6c 65 20 65 78 65 63 75 74 69 6f   single executio
29b38 6e 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  n..  */.  sqlite
29b39 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
29b3a 50 5f 45 78 70 69 72 65 2c 20 31 2c 20 30 29 3b  P_Expire, 1, 0);
29b3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 73 65  ..  /*.  ** Rese
29b3c 74 20 74 68 65 20 73 61 66 65 74 79 20 6c 65 76  t the safety lev
29b3d 65 6c 2c 20 69 6e 20 63 61 73 65 20 74 68 65 20  el, in case the 
29b3e 66 75 6c 6c 66 73 79 6e 63 20 66 6c 61 67 20 6f  fullfsync flag o
29b3f 72 20 73 79 6e 63 68 72 6f 6e 6f 75 73 0a 20 20  r synchronous.  
29b40 2a 2a 20 73 65 74 74 69 6e 67 20 63 68 61 6e 67  ** setting chang
29b41 65 64 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66  ed..  */.#ifndef
29b42 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47   SQLITE_OMIT_PAG
29b43 45 52 5f 50 52 41 47 4d 41 53 0a 20 20 69 66 28  ER_PRAGMAS.  if(
29b44 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20   db->autoCommit 
29b45 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  ){.    sqlite3Bt
29b46 72 65 65 53 65 74 53 61 66 65 74 79 4c 65 76 65  reeSetSafetyLeve
29b47 6c 28 70 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d  l(pDb->pBt, pDb-
29b48 3e 73 61 66 65 74 79 5f 6c 65 76 65 6c 2c 0a 20  >safety_level,. 
29b49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 64                (d
29b4a 62 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f  b->flags&SQLITE_
29b4b 46 75 6c 6c 46 53 79 6e 63 29 21 3d 30 29 3b 0a  FullFSync)!=0);.
29b4c 20 20 7d 0a 23 65 6e 64 69 66 0a 70 72 61 67 6d    }.#endif.pragm
29b4d 61 5f 6f 75 74 3a 0a 20 20 73 71 6c 69 74 65 33  a_out:.  sqlite3
29b4e 44 62 46 72 65 65 28 64 62 2c 20 7a 4c 65 66 74  DbFree(db, zLeft
29b4f 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  );.  sqlite3DbFr
29b50 65 65 28 64 62 2c 20 7a 52 69 67 68 74 29 3b 0a  ee(db, zRight);.
29b51 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
29b52 49 54 45 5f 4f 4d 49 54 5f 50 52 41 47 4d 41 20  ITE_OMIT_PRAGMA 
29b53 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
29b54 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 72 61 67 6d  *** End of pragm
29b55 61 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  a.c ************
29b56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29b57 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29b58 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
29b59 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70  *** Begin file p
29b5a 72 65 70 61 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  repare.c *******
29b5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29b5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29b5d 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4d  **/./*.** 2005 M
29b5e 61 79 20 32 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ay 25.**.** The 
29b5f 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
29b60 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
29b61 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
29b62 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
29b63 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
29b64 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
29b65 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
29b66 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
29b67 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
29b68 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
29b69 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
29b6a 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
29b6b 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
29b6c 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
29b6d 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
29b6e 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
29b6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
29b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29b71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29b72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29b73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29b74 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
29b75 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 69  e contains the i
29b76 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
29b77 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 72 65   the sqlite3_pre
29b78 70 61 72 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66  pare().** interf
29b79 61 63 65 2c 20 61 6e 64 20 72 6f 75 74 69 6e 65  ace, and routine
29b7a 73 20 74 68 61 74 20 63 6f 6e 74 72 69 62 75 74  s that contribut
29b7b 65 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74 68 65  e to loading the
29b7c 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
29b7d 0a 2a 2a 20 66 72 6f 6d 20 64 69 73 6b 2e 0a 2a  .** from disk..*
29b7e 2a 0a 2a 2a 20 24 49 64 3a 20 70 72 65 70 61 72  *.** $Id: prepar
29b7f 65 2e 63 2c 76 20 31 2e 31 33 31 20 32 30 30 39  e.c,v 1.131 2009
29b80 2f 30 38 2f 30 36 20 31 37 3a 34 33 3a 33 31 20  /08/06 17:43:31 
29b81 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  drh Exp $.*/../*
29b82 0a 2a 2a 20 46 69 6c 6c 20 74 68 65 20 49 6e 69  .** Fill the Ini
29b83 74 44 61 74 61 20 73 74 72 75 63 74 75 72 65 20  tData structure 
29b84 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65  with an error me
29b85 73 73 61 67 65 20 74 68 61 74 20 69 6e 64 69 63  ssage that indic
29b86 61 74 65 73 0a 2a 2a 20 74 68 61 74 20 74 68 65  ates.** that the
29b87 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 72   database is cor
29b88 72 75 70 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rupt..*/.static 
29b89 76 6f 69 64 20 63 6f 72 72 75 70 74 53 63 68 65  void corruptSche
29b8a 6d 61 28 0a 20 20 49 6e 69 74 44 61 74 61 20 2a  ma(.  InitData *
29b8b 70 44 61 74 61 2c 20 20 20 20 20 2f 2a 20 49 6e  pData,     /* In
29b8c 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 63 6f 6e  itialization con
29b8d 74 65 78 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  text */.  const 
29b8e 63 68 61 72 20 2a 7a 4f 62 6a 2c 20 20 20 20 2f  char *zObj,    /
29b8f 2a 20 4f 62 6a 65 63 74 20 62 65 69 6e 67 20 70  * Object being p
29b90 61 72 73 65 64 20 61 74 20 74 68 65 20 70 6f 69  arsed at the poi
29b91 6e 74 20 6f 66 20 65 72 72 6f 72 20 2a 2f 0a 20  nt of error */. 
29b92 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 78   const char *zEx
29b93 74 72 61 20 20 20 2f 2a 20 45 72 72 6f 72 20 69  tra   /* Error i
29b94 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 29 7b  nformation */.){
29b95 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
29b96 20 70 44 61 74 61 2d 3e 64 62 3b 0a 20 20 69 66   pData->db;.  if
29b97 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
29b98 6c 65 64 20 26 26 20 28 64 62 2d 3e 66 6c 61 67  led && (db->flag
29b99 73 20 26 20 53 51 4c 49 54 45 5f 52 65 63 6f 76  s & SQLITE_Recov
29b9a 65 72 79 4d 6f 64 65 29 3d 3d 30 20 29 7b 0a 20  eryMode)==0 ){. 
29b9b 20 20 20 69 66 28 20 7a 4f 62 6a 3d 3d 30 20 29     if( zObj==0 )
29b9c 20 7a 4f 62 6a 20 3d 20 22 3f 22 3b 0a 20 20 20   zObj = "?";.   
29b9d 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e   sqlite3SetStrin
29b9e 67 28 70 44 61 74 61 2d 3e 70 7a 45 72 72 4d 73  g(pData->pzErrMs
29b9f 67 2c 20 64 62 2c 0a 20 20 20 20 20 20 22 6d 61  g, db,.      "ma
29ba0 6c 66 6f 72 6d 65 64 20 64 61 74 61 62 61 73 65  lformed database
29ba1 20 73 63 68 65 6d 61 20 28 25 73 29 22 2c 20 7a   schema (%s)", z
29ba2 4f 62 6a 29 3b 0a 20 20 20 20 69 66 28 20 7a 45  Obj);.    if( zE
29ba3 78 74 72 61 20 29 7b 0a 20 20 20 20 20 20 2a 70  xtra ){.      *p
29ba4 44 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67 20 3d  Data->pzErrMsg =
29ba5 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66   sqlite3MAppendf
29ba6 28 64 62 2c 20 2a 70 44 61 74 61 2d 3e 70 7a 45  (db, *pData->pzE
29ba7 72 72 4d 73 67 2c 20 0a 20 20 20 20 20 20 20 20  rrMsg, .        
29ba8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29ba9 20 20 20 20 20 20 20 20 20 22 25 73 20 2d 20 25           "%s - %
29baa 73 22 2c 20 2a 70 44 61 74 61 2d 3e 70 7a 45 72  s", *pData->pzEr
29bab 72 4d 73 67 2c 20 7a 45 78 74 72 61 29 3b 0a 20  rMsg, zExtra);. 
29bac 20 20 20 7d 0a 20 20 7d 0a 20 20 70 44 61 74 61     }.  }.  pData
29bad 2d 3e 72 63 20 3d 20 64 62 2d 3e 6d 61 6c 6c 6f  ->rc = db->mallo
29bae 63 46 61 69 6c 65 64 20 3f 20 53 51 4c 49 54 45  cFailed ? SQLITE
29baf 5f 4e 4f 4d 45 4d 20 3a 20 53 51 4c 49 54 45 5f  _NOMEM : SQLITE_
29bb0 43 4f 52 52 55 50 54 3b 0a 7d 0a 0a 2f 2a 0a 2a  CORRUPT;.}../*.*
29bb1 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 63 61  * This is the ca
29bb2 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 20 66  llback routine f
29bb3 6f 72 20 74 68 65 20 63 6f 64 65 20 74 68 61 74  or the code that
29bb4 20 69 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 65   initializes the
29bb5 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 20 53  .** database.  S
29bb6 65 65 20 73 71 6c 69 74 65 33 49 6e 69 74 28 29  ee sqlite3Init()
29bb7 20 62 65 6c 6f 77 20 66 6f 72 20 61 64 64 69 74   below for addit
29bb8 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
29bb9 6e 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  n..** This routi
29bba 6e 65 20 69 73 20 61 6c 73 6f 20 63 61 6c 6c 65  ne is also calle
29bbb 64 20 66 72 6f 6d 20 74 68 65 20 4f 50 5f 50 61  d from the OP_Pa
29bbc 72 73 65 53 63 68 65 6d 61 20 6f 70 63 6f 64 65  rseSchema opcode
29bbd 20 6f 66 20 74 68 65 20 56 44 42 45 2e 0a 2a 2a   of the VDBE..**
29bbe 0a 2a 2a 20 45 61 63 68 20 63 61 6c 6c 62 61 63  .** Each callbac
29bbf 6b 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 66  k contains the f
29bc0 6f 6c 6c 6f 77 69 6e 67 20 69 6e 66 6f 72 6d 61  ollowing informa
29bc1 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  tion:.**.**     
29bc2 61 72 67 76 5b 30 5d 20 3d 20 6e 61 6d 65 20 6f  argv[0] = name o
29bc3 66 20 74 68 69 6e 67 20 62 65 69 6e 67 20 63 72  f thing being cr
29bc4 65 61 74 65 64 0a 2a 2a 20 20 20 20 20 61 72 67  eated.**     arg
29bc5 76 5b 31 5d 20 3d 20 72 6f 6f 74 20 70 61 67 65  v[1] = root page
29bc6 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 61 62 6c   number for tabl
29bc7 65 20 6f 72 20 69 6e 64 65 78 2e 20 30 20 66 6f  e or index. 0 fo
29bc8 72 20 74 72 69 67 67 65 72 20 6f 72 20 76 69 65  r trigger or vie
29bc9 77 2e 0a 2a 2a 20 20 20 20 20 61 72 67 76 5b 32  w..**     argv[2
29bca 5d 20 3d 20 53 51 4c 20 74 65 78 74 20 66 6f 72  ] = SQL text for
29bcb 20 74 68 65 20 43 52 45 41 54 45 20 73 74 61 74   the CREATE stat
29bcc 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c  ement..**.*/.SQL
29bcd 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
29bce 73 71 6c 69 74 65 33 49 6e 69 74 43 61 6c 6c 62  sqlite3InitCallb
29bcf 61 63 6b 28 76 6f 69 64 20 2a 70 49 6e 69 74 2c  ack(void *pInit,
29bd0 20 69 6e 74 20 61 72 67 63 2c 20 63 68 61 72 20   int argc, char 
29bd1 2a 2a 61 72 67 76 2c 20 63 68 61 72 20 2a 2a 4e  **argv, char **N
29bd2 6f 74 55 73 65 64 29 7b 0a 20 20 49 6e 69 74 44  otUsed){.  InitD
29bd3 61 74 61 20 2a 70 44 61 74 61 20 3d 20 28 49 6e  ata *pData = (In
29bd4 69 74 44 61 74 61 2a 29 70 49 6e 69 74 3b 0a 20  itData*)pInit;. 
29bd5 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
29bd6 44 61 74 61 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  Data->db;.  int 
29bd7 69 44 62 20 3d 20 70 44 61 74 61 2d 3e 69 44 62  iDb = pData->iDb
29bd8 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67  ;..  assert( arg
29bd9 63 3d 3d 33 20 29 3b 0a 20 20 55 4e 55 53 45 44  c==3 );.  UNUSED
29bda 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f 74 55  _PARAMETER2(NotU
29bdb 73 65 64 2c 20 61 72 67 63 29 3b 0a 20 20 61 73  sed, argc);.  as
29bdc 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
29bdd 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
29bde 65 78 29 20 29 3b 0a 20 20 44 62 43 6c 65 61 72  ex) );.  DbClear
29bdf 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 44 62  Property(db, iDb
29be0 2c 20 44 42 5f 45 6d 70 74 79 29 3b 0a 20 20 69  , DB_Empty);.  i
29be1 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
29be2 6c 65 64 20 29 7b 0a 20 20 20 20 63 6f 72 72 75  led ){.    corru
29be3 70 74 53 63 68 65 6d 61 28 70 44 61 74 61 2c 20  ptSchema(pData, 
29be4 61 72 67 76 5b 30 5d 2c 20 30 29 3b 0a 20 20 20  argv[0], 0);.   
29be5 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a   return 1;.  }..
29be6 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30    assert( iDb>=0
29be7 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20   && iDb<db->nDb 
29be8 29 3b 0a 20 20 69 66 28 20 61 72 67 76 3d 3d 30  );.  if( argv==0
29be9 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f   ) return 0;   /
29bea 2a 20 4d 69 67 68 74 20 68 61 70 70 65 6e 20 69  * Might happen i
29beb 66 20 45 4d 50 54 59 5f 52 45 53 55 4c 54 5f 43  f EMPTY_RESULT_C
29bec 41 4c 4c 42 41 43 4b 53 20 61 72 65 20 6f 6e 20  ALLBACKS are on 
29bed 2a 2f 0a 20 20 69 66 28 20 61 72 67 76 5b 31 5d  */.  if( argv[1]
29bee 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 72 72 75  ==0 ){.    corru
29bef 70 74 53 63 68 65 6d 61 28 70 44 61 74 61 2c 20  ptSchema(pData, 
29bf0 61 72 67 76 5b 30 5d 2c 20 30 29 3b 0a 20 20 7d  argv[0], 0);.  }
29bf1 65 6c 73 65 20 69 66 28 20 61 72 67 76 5b 32 5d  else if( argv[2]
29bf2 20 26 26 20 61 72 67 76 5b 32 5d 5b 30 5d 20 29   && argv[2][0] )
29bf3 7b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68  {.    /* Call th
29bf4 65 20 70 61 72 73 65 72 20 74 6f 20 70 72 6f 63  e parser to proc
29bf5 65 73 73 20 61 20 43 52 45 41 54 45 20 54 41 42  ess a CREATE TAB
29bf6 4c 45 2c 20 49 4e 44 45 58 20 6f 72 20 56 49 45  LE, INDEX or VIE
29bf7 57 2e 0a 20 20 20 20 2a 2a 20 42 75 74 20 62 65  W..    ** But be
29bf8 63 61 75 73 65 20 64 62 2d 3e 69 6e 69 74 2e 62  cause db->init.b
29bf9 75 73 79 20 69 73 20 73 65 74 20 74 6f 20 31 2c  usy is set to 1,
29bfa 20 6e 6f 20 56 44 42 45 20 63 6f 64 65 20 69 73   no VDBE code is
29bfb 20 67 65 6e 65 72 61 74 65 64 0a 20 20 20 20 2a   generated.    *
29bfc 2a 20 6f 72 20 65 78 65 63 75 74 65 64 2e 20 20  * or executed.  
29bfd 41 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 64  All the parser d
29bfe 6f 65 73 20 69 73 20 62 75 69 6c 64 20 74 68 65  oes is build the
29bff 20 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 0a 20   internal data. 
29c00 20 20 20 2a 2a 20 73 74 72 75 63 74 75 72 65 73     ** structures
29c01 20 74 68 61 74 20 64 65 73 63 72 69 62 65 20 74   that describe t
29c02 68 65 20 74 61 62 6c 65 2c 20 69 6e 64 65 78 2c  he table, index,
29c03 20 6f 72 20 76 69 65 77 2e 0a 20 20 20 20 2a 2f   or view..    */
29c04 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72 3b  .    char *zErr;
29c05 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20  .    int rc;.   
29c06 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69   assert( db->ini
29c07 74 2e 62 75 73 79 20 29 3b 0a 20 20 20 20 64 62  t.busy );.    db
29c08 2d 3e 69 6e 69 74 2e 69 44 62 20 3d 20 69 44 62  ->init.iDb = iDb
29c09 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 6e  ;.    db->init.n
29c0a 65 77 54 6e 75 6d 20 3d 20 61 74 6f 69 28 61 72  ewTnum = atoi(ar
29c0b 67 76 5b 31 5d 29 3b 0a 20 20 20 20 64 62 2d 3e  gv[1]);.    db->
29c0c 69 6e 69 74 2e 6f 72 70 68 61 6e 54 72 69 67 67  init.orphanTrigg
29c0d 65 72 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d  er = 0;.    rc =
29c0e 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62   sqlite3_exec(db
29c0f 2c 20 61 72 67 76 5b 32 5d 2c 20 30 2c 20 30 2c  , argv[2], 0, 0,
29c10 20 26 7a 45 72 72 29 3b 0a 20 20 20 20 64 62 2d   &zErr);.    db-
29c11 3e 69 6e 69 74 2e 69 44 62 20 3d 20 30 3b 0a 20  >init.iDb = 0;. 
29c12 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53     assert( rc!=S
29c13 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 7a 45 72 72  QLITE_OK || zErr
29c14 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 53  ==0 );.    if( S
29c15 51 4c 49 54 45 5f 4f 4b 21 3d 72 63 20 29 7b 0a  QLITE_OK!=rc ){.
29c16 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 69 6e        if( db->in
29c17 69 74 2e 6f 72 70 68 61 6e 54 72 69 67 67 65 72  it.orphanTrigger
29c18 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
29c19 72 74 28 20 69 44 62 3d 3d 31 20 29 3b 0a 20 20  rt( iDb==1 );.  
29c1a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
29c1b 20 20 20 70 44 61 74 61 2d 3e 72 63 20 3d 20 72     pData->rc = r
29c1c 63 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  c;.        if( r
29c1d 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
29c1e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d  ){.          db-
29c1f 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
29c20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  1;.        }else
29c21 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
29c22 49 4e 54 45 52 52 55 50 54 20 26 26 20 72 63 21  INTERRUPT && rc!
29c23 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 29  =SQLITE_LOCKED )
29c24 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 72 72  {.          corr
29c25 75 70 74 53 63 68 65 6d 61 28 70 44 61 74 61 2c  uptSchema(pData,
29c26 20 61 72 67 76 5b 30 5d 2c 20 7a 45 72 72 29 3b   argv[0], zErr);
29c27 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
29c28 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
29c29 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29  DbFree(db, zErr)
29c2a 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
29c2b 69 66 28 20 61 72 67 76 5b 30 5d 3d 3d 30 20 29  if( argv[0]==0 )
29c2c 7b 0a 20 20 20 20 63 6f 72 72 75 70 74 53 63 68  {.    corruptSch
29c2d 65 6d 61 28 70 44 61 74 61 2c 20 30 2c 20 30 29  ema(pData, 0, 0)
29c2e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f  ;.  }else{.    /
29c2f 2a 20 49 66 20 74 68 65 20 53 51 4c 20 63 6f 6c  * If the SQL col
29c30 75 6d 6e 20 69 73 20 62 6c 61 6e 6b 20 69 74 20  umn is blank it 
29c31 6d 65 61 6e 73 20 74 68 69 73 20 69 73 20 61 6e  means this is an
29c32 20 69 6e 64 65 78 20 74 68 61 74 0a 20 20 20 20   index that.    
29c33 2a 2a 20 77 61 73 20 63 72 65 61 74 65 64 20 74  ** was created t
29c34 6f 20 62 65 20 74 68 65 20 50 52 49 4d 41 52 59  o be the PRIMARY
29c35 20 4b 45 59 20 6f 72 20 74 6f 20 66 75 6c 66 69   KEY or to fulfi
29c36 6c 6c 20 61 20 55 4e 49 51 55 45 0a 20 20 20 20  ll a UNIQUE.    
29c37 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 6f  ** constraint fo
29c38 72 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45  r a CREATE TABLE
29c39 2e 20 20 54 68 65 20 69 6e 64 65 78 20 73 68 6f  .  The index sho
29c3a 75 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64 79  uld have already
29c3b 0a 20 20 20 20 2a 2a 20 62 65 65 6e 20 63 72 65  .    ** been cre
29c3c 61 74 65 64 20 77 68 65 6e 20 77 65 20 70 72 6f  ated when we pro
29c3d 63 65 73 73 65 64 20 74 68 65 20 43 52 45 41 54  cessed the CREAT
29c3e 45 20 54 41 42 4c 45 2e 20 20 41 6c 6c 20 77 65  E TABLE.  All we
29c3f 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 74 6f 20   have.    ** to 
29c40 64 6f 20 68 65 72 65 20 69 73 20 72 65 63 6f 72  do here is recor
29c41 64 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20  d the root page 
29c42 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74 20  number for that 
29c43 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20  index..    */.  
29c44 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b    Index *pIndex;
29c45 0a 20 20 20 20 70 49 6e 64 65 78 20 3d 20 73 71  .    pIndex = sq
29c46 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64  lite3FindIndex(d
29c47 62 2c 20 61 72 67 76 5b 30 5d 2c 20 64 62 2d 3e  b, argv[0], db->
29c48 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b  aDb[iDb].zName);
29c49 0a 20 20 20 20 69 66 28 20 70 49 6e 64 65 78 3d  .    if( pIndex=
29c4a 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  =0 ){.      /* T
29c4b 68 69 73 20 63 61 6e 20 6f 63 63 75 72 20 69 66  his can occur if
29c4c 20 74 68 65 72 65 20 65 78 69 73 74 73 20 61 6e   there exists an
29c4d 20 69 6e 64 65 78 20 6f 6e 20 61 20 54 45 4d 50   index on a TEMP
29c4e 20 74 61 62 6c 65 20 77 68 69 63 68 0a 20 20 20   table which.   
29c4f 20 20 20 2a 2a 20 68 61 73 20 74 68 65 20 73 61     ** has the sa
29c50 6d 65 20 6e 61 6d 65 20 61 73 20 61 6e 6f 74 68  me name as anoth
29c51 65 72 20 69 6e 64 65 78 20 6f 6e 20 61 20 70 65  er index on a pe
29c52 72 6d 61 6e 65 6e 74 20 69 6e 64 65 78 2e 20 20  rmanent index.  
29c53 53 69 6e 63 65 0a 20 20 20 20 20 20 2a 2a 20 74  Since.      ** t
29c54 68 65 20 70 65 72 6d 61 6e 65 6e 74 20 74 61 62  he permanent tab
29c55 6c 65 20 69 73 20 68 69 64 64 65 6e 20 62 79 20  le is hidden by 
29c56 74 68 65 20 54 45 4d 50 20 74 61 62 6c 65 2c 20  the TEMP table, 
29c57 77 65 20 63 61 6e 20 61 6c 73 6f 0a 20 20 20 20  we can also.    
29c58 20 20 2a 2a 20 73 61 66 65 6c 79 20 69 67 6e 6f    ** safely igno
29c59 72 65 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 20  re the index on 
29c5a 74 68 65 20 70 65 72 6d 61 6e 65 6e 74 20 74 61  the permanent ta
29c5b 62 6c 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ble..      */.  
29c5c 20 20 20 20 2f 2a 20 44 6f 20 4e 6f 74 68 69 6e      /* Do Nothin
29c5d 67 20 2a 2f 3b 0a 20 20 20 20 7d 65 6c 73 65 20  g */;.    }else 
29c5e 69 66 28 20 73 71 6c 69 74 65 33 47 65 74 49 6e  if( sqlite3GetIn
29c5f 74 33 32 28 61 72 67 76 5b 31 5d 2c 20 26 70 49  t32(argv[1], &pI
29c60 6e 64 65 78 2d 3e 74 6e 75 6d 29 3d 3d 30 20 29  ndex->tnum)==0 )
29c61 7b 0a 20 20 20 20 20 20 63 6f 72 72 75 70 74 53  {.      corruptS
29c62 63 68 65 6d 61 28 70 44 61 74 61 2c 20 61 72 67  chema(pData, arg
29c63 76 5b 30 5d 2c 20 22 69 6e 76 61 6c 69 64 20 72  v[0], "invalid r
29c64 6f 6f 74 70 61 67 65 22 29 3b 0a 20 20 20 20 7d  ootpage");.    }
29c65 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
29c66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70  .}../*.** Attemp
29c67 74 20 74 6f 20 72 65 61 64 20 74 68 65 20 64 61  t to read the da
29c68 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 61 6e  tabase schema an
29c69 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 6e 74  d initialize int
29c6a 65 72 6e 61 6c 0a 2a 2a 20 64 61 74 61 20 73 74  ernal.** data st
29c6b 72 75 63 74 75 72 65 73 20 66 6f 72 20 61 20 73  ructures for a s
29c6c 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 66  ingle database f
29c6d 69 6c 65 2e 20 20 54 68 65 20 69 6e 64 65 78 20  ile.  The index 
29c6e 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  of the.** databa
29c6f 73 65 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e  se file is given
29c70 20 62 79 20 69 44 62 2e 20 20 69 44 62 3d 3d 30   by iDb.  iDb==0
29c71 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 68 65   is used for the
29c72 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61 73   main.** databas
29c73 65 2e 20 20 69 44 62 3d 3d 31 20 73 68 6f 75 6c  e.  iDb==1 shoul
29c74 64 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 2e  d never be used.
29c75 20 20 69 44 62 3e 3d 32 20 69 73 20 75 73 65 64    iDb>=2 is used
29c76 20 66 6f 72 0a 2a 2a 20 61 75 78 69 6c 69 61 72   for.** auxiliar
29c77 79 20 64 61 74 61 62 61 73 65 73 2e 20 20 52 65  y databases.  Re
29c78 74 75 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20  turn one of the 
29c79 53 51 4c 49 54 45 5f 20 65 72 72 6f 72 20 63 6f  SQLITE_ error co
29c7a 64 65 73 20 74 6f 0a 2a 2a 20 69 6e 64 69 63 61  des to.** indica
29c7b 74 65 20 73 75 63 63 65 73 73 20 6f 72 20 66 61  te success or fa
29c7c 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  ilure..*/.static
29c7d 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74   int sqlite3Init
29c7e 4f 6e 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  One(sqlite3 *db,
29c7f 20 69 6e 74 20 69 44 62 2c 20 63 68 61 72 20 2a   int iDb, char *
29c80 2a 70 7a 45 72 72 4d 73 67 29 7b 0a 20 20 69 6e  *pzErrMsg){.  in
29c81 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  t rc;.  int i;. 
29c82 20 69 6e 74 20 73 69 7a 65 3b 0a 20 20 54 61 62   int size;.  Tab
29c83 6c 65 20 2a 70 54 61 62 3b 0a 20 20 44 62 20 2a  le *pTab;.  Db *
29c84 70 44 62 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73  pDb;.  char cons
29c85 74 20 2a 61 7a 41 72 67 5b 34 5d 3b 0a 20 20 69  t *azArg[4];.  i
29c86 6e 74 20 6d 65 74 61 5b 35 5d 3b 0a 20 20 49 6e  nt meta[5];.  In
29c87 69 74 44 61 74 61 20 69 6e 69 74 44 61 74 61 3b  itData initData;
29c88 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  .  char const *z
29c89 4d 61 73 74 65 72 53 63 68 65 6d 61 3b 0a 20 20  MasterSchema;.  
29c8a 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 73  char const *zMas
29c8b 74 65 72 4e 61 6d 65 20 3d 20 53 43 48 45 4d 41  terName = SCHEMA
29c8c 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 20 20 69  _TABLE(iDb);.  i
29c8d 6e 74 20 6f 70 65 6e 65 64 54 72 61 6e 73 61 63  nt openedTransac
29c8e 74 69 6f 6e 20 3d 20 30 3b 0a 0a 20 20 2f 2a 0a  tion = 0;..  /*.
29c8f 20 20 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20    ** The master 
29c90 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 68  database table h
29c91 61 73 20 61 20 73 74 72 75 63 74 75 72 65 20 6c  as a structure l
29c92 69 6b 65 20 74 68 69 73 0a 20 20 2a 2f 0a 20 20  ike this.  */.  
29c93 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
29c94 72 20 6d 61 73 74 65 72 5f 73 63 68 65 6d 61 5b  r master_schema[
29c95 5d 20 3d 20 0a 20 20 20 20 20 22 43 52 45 41 54  ] = .     "CREAT
29c96 45 20 54 41 42 4c 45 20 73 71 6c 69 74 65 5f 6d  E TABLE sqlite_m
29c97 61 73 74 65 72 28 5c 6e 22 0a 20 20 20 20 20 22  aster(\n".     "
29c98 20 20 74 79 70 65 20 74 65 78 74 2c 5c 6e 22 0a    type text,\n".
29c99 20 20 20 20 20 22 20 20 6e 61 6d 65 20 74 65 78       "  name tex
29c9a 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 74 62  t,\n".     "  tb
29c9b 6c 5f 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a  l_name text,\n".
29c9c 20 20 20 20 20 22 20 20 72 6f 6f 74 70 61 67 65       "  rootpage
29c9d 20 69 6e 74 65 67 65 72 2c 5c 6e 22 0a 20 20 20   integer,\n".   
29c9e 20 20 22 20 20 73 71 6c 20 74 65 78 74 5c 6e 22    "  sql text\n"
29c9f 0a 20 20 20 20 20 22 29 22 0a 20 20 3b 0a 23 69  .     ")".  ;.#i
29ca0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
29ca1 54 5f 54 45 4d 50 44 42 0a 20 20 73 74 61 74 69  T_TEMPDB.  stati
29ca2 63 20 63 6f 6e 73 74 20 63 68 61 72 20 74 65 6d  c const char tem
29ca3 70 5f 6d 61 73 74 65 72 5f 73 63 68 65 6d 61 5b  p_master_schema[
29ca4 5d 20 3d 20 0a 20 20 20 20 20 22 43 52 45 41 54  ] = .     "CREAT
29ca5 45 20 54 45 4d 50 20 54 41 42 4c 45 20 73 71 6c  E TEMP TABLE sql
29ca6 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 28  ite_temp_master(
29ca7 5c 6e 22 0a 20 20 20 20 20 22 20 20 74 79 70 65  \n".     "  type
29ca8 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22   text,\n".     "
29ca9 20 20 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a    name text,\n".
29caa 20 20 20 20 20 22 20 20 74 62 6c 5f 6e 61 6d 65       "  tbl_name
29cab 20 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22   text,\n".     "
29cac 20 20 72 6f 6f 74 70 61 67 65 20 69 6e 74 65 67    rootpage integ
29cad 65 72 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 73  er,\n".     "  s
29cae 71 6c 20 74 65 78 74 5c 6e 22 0a 20 20 20 20 20  ql text\n".     
29caf 22 29 22 0a 20 20 3b 0a 23 65 6c 73 65 0a 20 20  ")".  ;.#else.  
29cb0 23 64 65 66 69 6e 65 20 74 65 6d 70 5f 6d 61 73  #define temp_mas
29cb1 74 65 72 5f 73 63 68 65 6d 61 20 30 0a 23 65 6e  ter_schema 0.#en
29cb2 64 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 69  dif..  assert( i
29cb3 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d  Db>=0 && iDb<db-
29cb4 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74  >nDb );.  assert
29cb5 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  ( db->aDb[iDb].p
29cb6 53 63 68 65 6d 61 20 29 3b 0a 20 20 61 73 73 65  Schema );.  asse
29cb7 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
29cb8 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
29cb9 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  ) );.  assert( i
29cba 44 62 3d 3d 31 20 7c 7c 20 73 71 6c 69 74 65 33  Db==1 || sqlite3
29cbb 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28  BtreeHoldsMutex(
29cbc 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74  db->aDb[iDb].pBt
29cbd 29 20 29 3b 0a 0a 20 20 2f 2a 20 7a 4d 61 73 74  ) );..  /* zMast
29cbe 65 72 53 63 68 65 6d 61 20 61 6e 64 20 7a 49 6e  erSchema and zIn
29cbf 69 74 53 63 72 69 70 74 20 61 72 65 20 73 65 74  itScript are set
29cc0 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65   to point at the
29cc1 20 6d 61 73 74 65 72 20 73 63 68 65 6d 61 0a 20   master schema. 
29cc2 20 2a 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c 69   ** and initiali
29cc3 73 61 74 69 6f 6e 20 73 63 72 69 70 74 20 61 70  sation script ap
29cc4 70 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68  propriate for th
29cc5 65 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67  e database being
29cc6 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 73 65  .  ** initialise
29cc7 64 2e 20 7a 4d 61 73 74 65 72 4e 61 6d 65 20 69  d. zMasterName i
29cc8 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  s the name of th
29cc9 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 0a  e master table..
29cca 20 20 2a 2f 0a 20 20 69 66 28 20 21 4f 4d 49 54    */.  if( !OMIT
29ccb 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d  _TEMPDB && iDb==
29ccc 31 20 29 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72  1 ){.    zMaster
29ccd 53 63 68 65 6d 61 20 3d 20 74 65 6d 70 5f 6d 61  Schema = temp_ma
29cce 73 74 65 72 5f 73 63 68 65 6d 61 3b 0a 20 20 7d  ster_schema;.  }
29ccf 65 6c 73 65 7b 0a 20 20 20 20 7a 4d 61 73 74 65  else{.    zMaste
29cd0 72 53 63 68 65 6d 61 20 3d 20 6d 61 73 74 65 72  rSchema = master
29cd1 5f 73 63 68 65 6d 61 3b 0a 20 20 7d 0a 20 20 7a  _schema;.  }.  z
29cd2 4d 61 73 74 65 72 4e 61 6d 65 20 3d 20 53 43 48  MasterName = SCH
29cd3 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a  EMA_TABLE(iDb);.
29cd4 0a 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20  .  /* Construct 
29cd5 74 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65  the schema table
29cd6 73 2e 20 20 2a 2f 0a 20 20 61 7a 41 72 67 5b 30  s.  */.  azArg[0
29cd7 5d 20 3d 20 7a 4d 61 73 74 65 72 4e 61 6d 65 3b  ] = zMasterName;
29cd8 0a 20 20 61 7a 41 72 67 5b 31 5d 20 3d 20 22 31  .  azArg[1] = "1
29cd9 22 3b 0a 20 20 61 7a 41 72 67 5b 32 5d 20 3d 20  ";.  azArg[2] = 
29cda 7a 4d 61 73 74 65 72 53 63 68 65 6d 61 3b 0a 20  zMasterSchema;. 
29cdb 20 61 7a 41 72 67 5b 33 5d 20 3d 20 30 3b 0a 20   azArg[3] = 0;. 
29cdc 20 69 6e 69 74 44 61 74 61 2e 64 62 20 3d 20 64   initData.db = d
29cdd 62 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 69 44  b;.  initData.iD
29cde 62 20 3d 20 69 44 62 3b 0a 20 20 69 6e 69 74 44  b = iDb;.  initD
29cdf 61 74 61 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f  ata.rc = SQLITE_
29ce0 4f 4b 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 70  OK;.  initData.p
29ce1 7a 45 72 72 4d 73 67 20 3d 20 70 7a 45 72 72 4d  zErrMsg = pzErrM
29ce2 73 67 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69  sg;.  (void)sqli
29ce3 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
29ce4 3b 0a 20 20 73 71 6c 69 74 65 33 49 6e 69 74 43  ;.  sqlite3InitC
29ce5 61 6c 6c 62 61 63 6b 28 26 69 6e 69 74 44 61 74  allback(&initDat
29ce6 61 2c 20 33 2c 20 28 63 68 61 72 20 2a 2a 29 61  a, 3, (char **)a
29ce7 7a 41 72 67 2c 20 30 29 3b 0a 20 20 28 76 6f 69  zArg, 0);.  (voi
29ce8 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
29ce9 6e 28 64 62 29 3b 0a 20 20 69 66 28 20 69 6e 69  n(db);.  if( ini
29cea 74 44 61 74 61 2e 72 63 20 29 7b 0a 20 20 20 20  tData.rc ){.    
29ceb 72 63 20 3d 20 69 6e 69 74 44 61 74 61 2e 72 63  rc = initData.rc
29cec 3b 0a 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72  ;.    goto error
29ced 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62  _out;.  }.  pTab
29cee 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61   = sqlite3FindTa
29cef 62 6c 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 4e  ble(db, zMasterN
29cf0 61 6d 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62  ame, db->aDb[iDb
29cf1 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  ].zName);.  if( 
29cf2 41 4c 57 41 59 53 28 70 54 61 62 29 20 29 7b 0a  ALWAYS(pTab) ){.
29cf3 20 20 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61      pTab->tabFla
29cf4 67 73 20 7c 3d 20 54 46 5f 52 65 61 64 6f 6e 6c  gs |= TF_Readonl
29cf5 79 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65  y;.  }..  /* Cre
29cf6 61 74 65 20 61 20 63 75 72 73 6f 72 20 74 6f 20  ate a cursor to 
29cf7 68 6f 6c 64 20 74 68 65 20 64 61 74 61 62 61 73  hold the databas
29cf8 65 20 6f 70 65 6e 0a 20 20 2a 2f 0a 20 20 70 44  e open.  */.  pD
29cf9 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62  b = &db->aDb[iDb
29cfa 5d 3b 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 42  ];.  if( pDb->pB
29cfb 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  t==0 ){.    if( 
29cfc 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20  !OMIT_TEMPDB && 
29cfd 41 4c 57 41 59 53 28 69 44 62 3d 3d 31 29 20 29  ALWAYS(iDb==1) )
29cfe 7b 0a 20 20 20 20 20 20 44 62 53 65 74 50 72 6f  {.      DbSetPro
29cff 70 65 72 74 79 28 64 62 2c 20 31 2c 20 44 42 5f  perty(db, 1, DB_
29d00 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 3b 0a 20  SchemaLoaded);. 
29d01 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20     }.    return 
29d02 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
29d03 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
29d04 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 61 20 72   not already a r
29d05 65 61 64 2d 6f 6e 6c 79 20 28 6f 72 20 72 65 61  ead-only (or rea
29d06 64 2d 77 72 69 74 65 29 20 74 72 61 6e 73 61 63  d-write) transac
29d07 74 69 6f 6e 20 6f 70 65 6e 65 64 0a 20 20 2a 2a  tion opened.  **
29d08 20 6f 6e 20 74 68 65 20 62 2d 74 72 65 65 20 64   on the b-tree d
29d09 61 74 61 62 61 73 65 2c 20 6f 70 65 6e 20 6f 6e  atabase, open on
29d0a 65 20 6e 6f 77 2e 20 49 66 20 61 20 74 72 61 6e  e now. If a tran
29d0b 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65  saction is opene
29d0c 64 2c 20 69 74 20 0a 20 20 2a 2a 20 77 69 6c 6c  d, it .  ** will
29d0d 20 62 65 20 63 6c 6f 73 65 64 20 62 65 66 6f 72   be closed befor
29d0e 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  e this function 
29d0f 72 65 74 75 72 6e 73 2e 20 20 2a 2f 0a 20 20 73  returns.  */.  s
29d10 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
29d11 28 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 69 66  (pDb->pBt);.  if
29d12 28 20 21 73 71 6c 69 74 65 33 42 74 72 65 65 49  ( !sqlite3BtreeI
29d13 73 49 6e 52 65 61 64 54 72 61 6e 73 28 70 44 62  sInReadTrans(pDb
29d14 2d 3e 70 42 74 29 20 29 7b 0a 20 20 20 20 72 63  ->pBt) ){.    rc
29d15 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42   = sqlite3BtreeB
29d16 65 67 69 6e 54 72 61 6e 73 28 70 44 62 2d 3e 70  eginTrans(pDb->p
29d17 42 74 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  Bt, 0);.    if( 
29d18 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
29d19 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65  .      sqlite3Se
29d1a 74 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67  tString(pzErrMsg
29d1b 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69  , db, "%s", sqli
29d1c 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a  te3ErrStr(rc));.
29d1d 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f        goto inito
29d1e 6e 65 5f 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20  ne_error_out;.  
29d1f 20 20 7d 0a 20 20 20 20 6f 70 65 6e 65 64 54 72    }.    openedTr
29d20 61 6e 73 61 63 74 69 6f 6e 20 3d 20 31 3b 0a 20  ansaction = 1;. 
29d21 20 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65   }..  /* Get the
29d22 20 64 61 74 61 62 61 73 65 20 6d 65 74 61 20 69   database meta i
29d23 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20 2a 2a  nformation..  **
29d24 0a 20 20 2a 2a 20 4d 65 74 61 20 76 61 6c 75 65  .  ** Meta value
29d25 73 20 61 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73  s are as follows
29d26 3a 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 30  :.  **    meta[0
29d27 5d 20 20 20 53 63 68 65 6d 61 20 63 6f 6f 6b 69  ]   Schema cooki
29d28 65 2e 20 20 43 68 61 6e 67 65 73 20 77 69 74 68  e.  Changes with
29d29 20 65 61 63 68 20 73 63 68 65 6d 61 20 63 68 61   each schema cha
29d2a 6e 67 65 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74  nge..  **    met
29d2b 61 5b 31 5d 20 20 20 46 69 6c 65 20 66 6f 72 6d  a[1]   File form
29d2c 61 74 20 6f 66 20 73 63 68 65 6d 61 20 6c 61 79  at of schema lay
29d2d 65 72 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61  er..  **    meta
29d2e 5b 32 5d 20 20 20 53 69 7a 65 20 6f 66 20 74 68  [2]   Size of th
29d2f 65 20 70 61 67 65 20 63 61 63 68 65 2e 0a 20 20  e page cache..  
29d30 2a 2a 20 20 20 20 6d 65 74 61 5b 33 5d 20 20 20  **    meta[3]   
29d31 4c 61 72 67 65 73 74 20 72 6f 6f 74 70 61 67 65  Largest rootpage
29d32 20 28 61 75 74 6f 2f 69 6e 63 72 5f 76 61 63 75   (auto/incr_vacu
29d33 75 6d 20 6d 6f 64 65 29 0a 20 20 2a 2a 20 20 20  um mode).  **   
29d34 20 6d 65 74 61 5b 34 5d 20 20 20 44 62 20 74 65   meta[4]   Db te
29d35 78 74 20 65 6e 63 6f 64 69 6e 67 2e 20 31 3a 55  xt encoding. 1:U
29d36 54 46 2d 38 20 32 3a 55 54 46 2d 31 36 4c 45 20  TF-8 2:UTF-16LE 
29d37 33 3a 55 54 46 2d 31 36 42 45 0a 20 20 2a 2a 20  3:UTF-16BE.  ** 
29d38 20 20 20 6d 65 74 61 5b 35 5d 20 20 20 55 73 65     meta[5]   Use
29d39 72 20 76 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 20  r version.  **  
29d3a 20 20 6d 65 74 61 5b 36 5d 20 20 20 49 6e 63 72    meta[6]   Incr
29d3b 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d 20 6d  emental vacuum m
29d3c 6f 64 65 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61  ode.  **    meta
29d3d 5b 37 5d 20 20 20 75 6e 75 73 65 64 0a 20 20 2a  [7]   unused.  *
29d3e 2a 20 20 20 20 6d 65 74 61 5b 38 5d 20 20 20 75  *    meta[8]   u
29d3f 6e 75 73 65 64 0a 20 20 2a 2a 20 20 20 20 6d 65  nused.  **    me
29d40 74 61 5b 39 5d 20 20 20 75 6e 75 73 65 64 0a 20  ta[9]   unused. 
29d41 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 3a 20 54   **.  ** Note: T
29d42 68 65 20 23 64 65 66 69 6e 65 64 20 53 51 4c 49  he #defined SQLI
29d43 54 45 5f 55 54 46 2a 20 73 79 6d 62 6f 6c 73 20  TE_UTF* symbols 
29d44 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 63  in sqliteInt.h c
29d45 6f 72 72 65 73 70 6f 6e 64 20 74 6f 0a 20 20 2a  orrespond to.  *
29d46 2a 20 74 68 65 20 70 6f 73 73 69 62 6c 65 20 76  * the possible v
29d47 61 6c 75 65 73 20 6f 66 20 6d 65 74 61 5b 34 5d  alues of meta[4]
29d48 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  ..  */.  for(i=0
29d49 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 6d 65  ; i<ArraySize(me
29d4a 74 61 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  ta); i++){.    s
29d4b 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65  qlite3BtreeGetMe
29d4c 74 61 28 70 44 62 2d 3e 70 42 74 2c 20 69 2b 31  ta(pDb->pBt, i+1
29d4d 2c 20 28 75 33 32 20 2a 29 26 6d 65 74 61 5b 69  , (u32 *)&meta[i
29d4e 5d 29 3b 0a 20 20 7d 0a 20 20 70 44 62 2d 3e 70  ]);.  }.  pDb->p
29d4f 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63  Schema->schema_c
29d50 6f 6f 6b 69 65 20 3d 20 6d 65 74 61 5b 42 54 52  ookie = meta[BTR
29d51 45 45 5f 53 43 48 45 4d 41 5f 56 45 52 53 49 4f  EE_SCHEMA_VERSIO
29d52 4e 2d 31 5d 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f  N-1];..  /* If o
29d53 70 65 6e 69 6e 67 20 61 20 6e 6f 6e 2d 65 6d 70  pening a non-emp
29d54 74 79 20 64 61 74 61 62 61 73 65 2c 20 63 68 65  ty database, che
29d55 63 6b 20 74 68 65 20 74 65 78 74 20 65 6e 63 6f  ck the text enco
29d56 64 69 6e 67 2e 20 46 6f 72 20 74 68 65 0a 20 20  ding. For the.  
29d57 2a 2a 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  ** main database
29d58 2c 20 73 65 74 20 73 71 6c 69 74 65 33 2e 65 6e  , set sqlite3.en
29d59 63 20 74 6f 20 74 68 65 20 65 6e 63 6f 64 69 6e  c to the encodin
29d5a 67 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61  g of the main da
29d5b 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20 46 6f 72  tabase..  ** For
29d5c 20 61 6e 20 61 74 74 61 63 68 65 64 20 64 62 2c   an attached db,
29d5d 20 69 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20   it is an error 
29d5e 69 66 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  if the encoding 
29d5f 69 73 20 6e 6f 74 20 74 68 65 20 73 61 6d 65 0a  is not the same.
29d60 20 20 2a 2a 20 61 73 20 73 71 6c 69 74 65 33 2e    ** as sqlite3.
29d61 65 6e 63 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  enc..  */.  if( 
29d62 6d 65 74 61 5b 42 54 52 45 45 5f 54 45 58 54 5f  meta[BTREE_TEXT_
29d63 45 4e 43 4f 44 49 4e 47 2d 31 5d 20 29 7b 20 20  ENCODING-1] ){  
29d64 2f 2a 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  /* text encoding
29d65 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 44 62 3d   */.    if( iDb=
29d66 3d 30 20 29 7b 0a 20 20 20 20 20 20 75 38 20 65  =0 ){.      u8 e
29d67 6e 63 6f 64 69 6e 67 3b 0a 20 20 20 20 20 20 2f  ncoding;.      /
29d68 2a 20 49 66 20 6f 70 65 6e 69 6e 67 20 74 68 65  * If opening the
29d69 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20   main database, 
29d6a 73 65 74 20 45 4e 43 28 64 62 29 2e 20 2a 2f 0a  set ENC(db). */.
29d6b 20 20 20 20 20 20 65 6e 63 6f 64 69 6e 67 20 3d        encoding =
29d6c 20 28 75 38 29 6d 65 74 61 5b 42 54 52 45 45 5f   (u8)meta[BTREE_
29d6d 54 45 58 54 5f 45 4e 43 4f 44 49 4e 47 2d 31 5d  TEXT_ENCODING-1]
29d6e 20 26 20 33 3b 0a 20 20 20 20 20 20 69 66 28 20   & 3;.      if( 
29d6f 65 6e 63 6f 64 69 6e 67 3d 3d 30 20 29 20 65 6e  encoding==0 ) en
29d70 63 6f 64 69 6e 67 20 3d 20 53 51 4c 49 54 45 5f  coding = SQLITE_
29d71 55 54 46 38 3b 0a 20 20 20 20 20 20 45 4e 43 28  UTF8;.      ENC(
29d72 64 62 29 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a  db) = encoding;.
29d73 20 20 20 20 20 20 64 62 2d 3e 70 44 66 6c 74 43        db->pDfltC
29d74 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  oll = sqlite3Fin
29d75 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 53 51 4c  dCollSeq(db, SQL
29d76 49 54 45 5f 55 54 46 38 2c 20 22 42 49 4e 41 52  ITE_UTF8, "BINAR
29d77 59 22 2c 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73  Y", 0);.    }els
29d78 65 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 6f  e{.      /* If o
29d79 70 65 6e 69 6e 67 20 61 6e 20 61 74 74 61 63 68  pening an attach
29d7a 65 64 20 64 61 74 61 62 61 73 65 2c 20 74 68 65  ed database, the
29d7b 20 65 6e 63 6f 64 69 6e 67 20 6d 75 63 68 20 6d   encoding much m
29d7c 61 74 63 68 20 45 4e 43 28 64 62 29 20 2a 2f 0a  atch ENC(db) */.
29d7d 20 20 20 20 20 20 69 66 28 20 6d 65 74 61 5b 42        if( meta[B
29d7e 54 52 45 45 5f 54 45 58 54 5f 45 4e 43 4f 44 49  TREE_TEXT_ENCODI
29d7f 4e 47 2d 31 5d 21 3d 45 4e 43 28 64 62 29 20 29  NG-1]!=ENC(db) )
29d80 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
29d81 33 53 65 74 53 74 72 69 6e 67 28 70 7a 45 72 72  3SetString(pzErr
29d82 4d 73 67 2c 20 64 62 2c 20 22 61 74 74 61 63 68  Msg, db, "attach
29d83 65 64 20 64 61 74 61 62 61 73 65 73 20 6d 75 73  ed databases mus
29d84 74 20 75 73 65 20 74 68 65 20 73 61 6d 65 22 0a  t use the same".
29d85 20 20 20 20 20 20 20 20 20 20 20 20 22 20 74 65              " te
29d86 78 74 20 65 6e 63 6f 64 69 6e 67 20 61 73 20 6d  xt encoding as m
29d87 61 69 6e 20 64 61 74 61 62 61 73 65 22 29 3b 0a  ain database");.
29d88 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
29d89 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
29d8a 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65 5f     goto initone_
29d8b 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 20 20 20  error_out;.     
29d8c 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65   }.    }.  }else
29d8d 7b 0a 20 20 20 20 44 62 53 65 74 50 72 6f 70 65  {.    DbSetPrope
29d8e 72 74 79 28 64 62 2c 20 69 44 62 2c 20 44 42 5f  rty(db, iDb, DB_
29d8f 45 6d 70 74 79 29 3b 0a 20 20 7d 0a 20 20 70 44  Empty);.  }.  pD
29d90 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 65 6e 63 20  b->pSchema->enc 
29d91 3d 20 45 4e 43 28 64 62 29 3b 0a 0a 20 20 69 66  = ENC(db);..  if
29d92 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  ( pDb->pSchema->
29d93 63 61 63 68 65 5f 73 69 7a 65 3d 3d 30 20 29 7b  cache_size==0 ){
29d94 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d 65 74 61  .    size = meta
29d95 5b 42 54 52 45 45 5f 44 45 46 41 55 4c 54 5f 43  [BTREE_DEFAULT_C
29d96 41 43 48 45 5f 53 49 5a 45 2d 31 5d 3b 0a 20 20  ACHE_SIZE-1];.  
29d97 20 20 69 66 28 20 73 69 7a 65 3d 3d 30 20 29 7b    if( size==0 ){
29d98 20 73 69 7a 65 20 3d 20 53 51 4c 49 54 45 5f 44   size = SQLITE_D
29d99 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a  EFAULT_CACHE_SIZ
29d9a 45 3b 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a  E; }.    if( siz
29d9b 65 3c 30 20 29 20 73 69 7a 65 20 3d 20 2d 73 69  e<0 ) size = -si
29d9c 7a 65 3b 0a 20 20 20 20 70 44 62 2d 3e 70 53 63  ze;.    pDb->pSc
29d9d 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65  hema->cache_size
29d9e 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 73 71 6c   = size;.    sql
29d9f 69 74 65 33 42 74 72 65 65 53 65 74 43 61 63 68  ite3BtreeSetCach
29da0 65 53 69 7a 65 28 70 44 62 2d 3e 70 42 74 2c 20  eSize(pDb->pBt, 
29da1 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61  pDb->pSchema->ca
29da2 63 68 65 5f 73 69 7a 65 29 3b 0a 20 20 7d 0a 0a  che_size);.  }..
29da3 20 20 2f 2a 0a 20 20 2a 2a 20 66 69 6c 65 5f 66    /*.  ** file_f
29da4 6f 72 6d 61 74 3d 3d 31 20 20 20 20 56 65 72 73  ormat==1    Vers
29da5 69 6f 6e 20 33 2e 30 2e 30 2e 0a 20 20 2a 2a 20  ion 3.0.0..  ** 
29da6 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 32 20 20  file_format==2  
29da7 20 20 56 65 72 73 69 6f 6e 20 33 2e 31 2e 33 2e    Version 3.1.3.
29da8 20 20 2f 2f 20 41 4c 54 45 52 20 54 41 42 4c 45    // ALTER TABLE
29da9 20 41 44 44 20 43 4f 4c 55 4d 4e 0a 20 20 2a 2a   ADD COLUMN.  **
29daa 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d 33 20   file_format==3 
29dab 20 20 20 56 65 72 73 69 6f 6e 20 33 2e 31 2e 34     Version 3.1.4
29dac 2e 20 20 2f 2f 20 64 69 74 74 6f 20 62 75 74 20  .  // ditto but 
29dad 77 69 74 68 20 6e 6f 6e 2d 4e 55 4c 4c 20 64 65  with non-NULL de
29dae 66 61 75 6c 74 73 0a 20 20 2a 2a 20 66 69 6c 65  faults.  ** file
29daf 5f 66 6f 72 6d 61 74 3d 3d 34 20 20 20 20 56 65  _format==4    Ve
29db0 72 73 69 6f 6e 20 33 2e 33 2e 30 2e 20 20 2f 2f  rsion 3.3.0.  //
29db1 20 44 45 53 43 20 69 6e 64 69 63 65 73 2e 20 20   DESC indices.  
29db2 42 6f 6f 6c 65 61 6e 20 63 6f 6e 73 74 61 6e 74  Boolean constant
29db3 73 0a 20 20 2a 2f 0a 20 20 70 44 62 2d 3e 70 53  s.  */.  pDb->pS
29db4 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d  chema->file_form
29db5 61 74 20 3d 20 28 75 38 29 6d 65 74 61 5b 42 54  at = (u8)meta[BT
29db6 52 45 45 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 2d  REE_FILE_FORMAT-
29db7 31 5d 3b 0a 20 20 69 66 28 20 70 44 62 2d 3e 70  1];.  if( pDb->p
29db8 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72  Schema->file_for
29db9 6d 61 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 44  mat==0 ){.    pD
29dba 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65  b->pSchema->file
29dbb 5f 66 6f 72 6d 61 74 20 3d 20 31 3b 0a 20 20 7d  _format = 1;.  }
29dbc 0a 20 20 69 66 28 20 70 44 62 2d 3e 70 53 63 68  .  if( pDb->pSch
29dbd 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74  ema->file_format
29dbe 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45  >SQLITE_MAX_FILE
29dbf 5f 46 4f 52 4d 41 54 20 29 7b 0a 20 20 20 20 73  _FORMAT ){.    s
29dc0 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28  qlite3SetString(
29dc1 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 75  pzErrMsg, db, "u
29dc2 6e 73 75 70 70 6f 72 74 65 64 20 66 69 6c 65 20  nsupported file 
29dc3 66 6f 72 6d 61 74 22 29 3b 0a 20 20 20 20 72 63  format");.    rc
29dc4 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
29dc5 0a 20 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e  .    goto initon
29dc6 65 5f 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d  e_error_out;.  }
29dc7 0a 0a 20 20 2f 2a 20 54 69 63 6b 65 74 20 23 32  ..  /* Ticket #2
29dc8 38 30 34 3a 20 20 57 68 65 6e 20 77 65 20 6f 70  804:  When we op
29dc9 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 69 6e  en a database in
29dca 20 74 68 65 20 6e 65 77 65 72 20 66 69 6c 65 20   the newer file 
29dcb 66 6f 72 6d 61 74 2c 0a 20 20 2a 2a 20 63 6c 65  format,.  ** cle
29dcc 61 72 20 74 68 65 20 6c 65 67 61 63 79 5f 66 69  ar the legacy_fi
29dcd 6c 65 5f 66 6f 72 6d 61 74 20 70 72 61 67 6d 61  le_format pragma
29dce 20 66 6c 61 67 20 73 6f 20 74 68 61 74 20 61 20   flag so that a 
29dcf 56 41 43 55 55 4d 20 77 69 6c 6c 0a 20 20 2a 2a  VACUUM will.  **
29dd0 20 6e 6f 74 20 64 6f 77 6e 67 72 61 64 65 20 74   not downgrade t
29dd1 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 20  he database and 
29dd2 74 68 75 73 20 69 6e 76 61 6c 69 64 61 74 65 20  thus invalidate 
29dd3 61 6e 79 20 64 65 73 63 65 6e 64 69 6e 67 0a 20  any descending. 
29dd4 20 2a 2a 20 69 6e 64 69 63 65 73 20 74 68 61 74   ** indices that
29dd5 20 74 68 65 20 75 73 65 72 20 6d 69 67 68 74 20   the user might 
29dd6 68 61 76 65 20 63 72 65 61 74 65 64 2e 0a 20 20  have created..  
29dd7 2a 2f 0a 20 20 69 66 28 20 69 44 62 3d 3d 30 20  */.  if( iDb==0 
29dd8 26 26 20 6d 65 74 61 5b 42 54 52 45 45 5f 46 49  && meta[BTREE_FI
29dd9 4c 45 5f 46 4f 52 4d 41 54 2d 31 5d 3e 3d 34 20  LE_FORMAT-1]>=4 
29dda 29 7b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67 73  ){.    db->flags
29ddb 20 26 3d 20 7e 53 51 4c 49 54 45 5f 4c 65 67 61   &= ~SQLITE_Lega
29ddc 63 79 46 69 6c 65 46 6d 74 3b 0a 20 20 7d 0a 0a  cyFileFmt;.  }..
29ddd 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 73 63    /* Read the sc
29dde 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  hema information
29ddf 20 6f 75 74 20 6f 66 20 74 68 65 20 73 63 68 65   out of the sche
29de0 6d 61 20 74 61 62 6c 65 73 0a 20 20 2a 2f 0a 20  ma tables.  */. 
29de1 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e 69   assert( db->ini
29de2 74 2e 62 75 73 79 20 29 3b 0a 20 20 7b 0a 20 20  t.busy );.  {.  
29de3 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20    char *zSql;.  
29de4 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33    zSql = sqlite3
29de5 4d 50 72 69 6e 74 66 28 64 62 2c 20 0a 20 20 20  MPrintf(db, .   
29de6 20 20 20 20 20 22 53 45 4c 45 43 54 20 6e 61 6d       "SELECT nam
29de7 65 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c  e, rootpage, sql
29de8 20 46 52 4f 4d 20 27 25 71 27 2e 25 73 22 2c 0a   FROM '%q'.%s",.
29de9 20 20 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b          db->aDb[
29dea 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 4d 61 73  iDb].zName, zMas
29deb 74 65 72 4e 61 6d 65 29 3b 0a 20 20 20 20 28 76  terName);.    (v
29dec 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
29ded 79 4f 66 66 28 64 62 29 3b 0a 23 69 66 6e 64 65  yOff(db);.#ifnde
29dee 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
29def 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20  THORIZATION.    
29df0 7b 0a 20 20 20 20 20 20 69 6e 74 20 28 2a 78 41  {.      int (*xA
29df1 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  uth)(void*,int,c
29df2 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74  onst char*,const
29df3 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
29df4 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  r*,const char*);
29df5 0a 20 20 20 20 20 20 78 41 75 74 68 20 3d 20 64  .      xAuth = d
29df6 62 2d 3e 78 41 75 74 68 3b 0a 20 20 20 20 20 20  b->xAuth;.      
29df7 64 62 2d 3e 78 41 75 74 68 20 3d 20 30 3b 0a 23  db->xAuth = 0;.#
29df8 65 6e 64 69 66 0a 20 20 20 20 20 20 72 63 20 3d  endif.      rc =
29df9 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62   sqlite3_exec(db
29dfa 2c 20 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33 49  , zSql, sqlite3I
29dfb 6e 69 74 43 61 6c 6c 62 61 63 6b 2c 20 26 69 6e  nitCallback, &in
29dfc 69 74 44 61 74 61 2c 20 30 29 3b 0a 23 69 66 6e  itData, 0);.#ifn
29dfd 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
29dfe 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20  AUTHORIZATION.  
29dff 20 20 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20      db->xAuth = 
29e00 78 41 75 74 68 3b 0a 20 20 20 20 7d 0a 23 65 6e  xAuth;.    }.#en
29e01 64 69 66 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  dif.    if( rc==
29e02 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d  SQLITE_OK ) rc =
29e03 20 69 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20   initData.rc;.  
29e04 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
29e05 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20  afetyOn(db);.   
29e06 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
29e07 62 2c 20 7a 53 71 6c 29 3b 0a 23 69 66 6e 64 65  b, zSql);.#ifnde
29e08 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e  f SQLITE_OMIT_AN
29e09 41 4c 59 5a 45 0a 20 20 20 20 69 66 28 20 72 63  ALYZE.    if( rc
29e0a 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
29e0b 20 20 20 20 20 73 71 6c 69 74 65 33 41 6e 61 6c       sqlite3Anal
29e0c 79 73 69 73 4c 6f 61 64 28 64 62 2c 20 69 44 62  ysisLoad(db, iDb
29e0d 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
29e0e 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61    }.  if( db->ma
29e0f 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
29e10 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
29e11 4d 45 4d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  MEM;.    sqlite3
29e12 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68  ResetInternalSch
29e13 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a  ema(db, 0);.  }.
29e14 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
29e15 5f 4f 4b 20 7c 7c 20 28 64 62 2d 3e 66 6c 61 67  _OK || (db->flag
29e16 73 26 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72  s&SQLITE_Recover
29e17 79 4d 6f 64 65 29 29 7b 0a 20 20 20 20 2f 2a 20  yMode)){.    /* 
29e18 42 6c 61 63 6b 20 6d 61 67 69 63 3a 20 49 66 20  Black magic: If 
29e19 74 68 65 20 53 51 4c 49 54 45 5f 52 65 63 6f 76  the SQLITE_Recov
29e1a 65 72 79 4d 6f 64 65 20 66 6c 61 67 20 69 73 20  eryMode flag is 
29e1b 73 65 74 2c 20 74 68 65 6e 20 63 6f 6e 73 69 64  set, then consid
29e1c 65 72 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 63  er.    ** the sc
29e1d 68 65 6d 61 20 6c 6f 61 64 65 64 2c 20 65 76 65  hema loaded, eve
29e1e 6e 20 69 66 20 65 72 72 6f 72 73 20 6f 63 63 75  n if errors occu
29e1f 72 72 65 64 2e 20 49 6e 20 74 68 69 73 20 73 69  rred. In this si
29e20 74 75 61 74 69 6f 6e 20 74 68 65 20 0a 20 20 20  tuation the .   
29e21 20 2a 2a 20 63 75 72 72 65 6e 74 20 73 71 6c 69   ** current sqli
29e22 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 6f 70  te3_prepare() op
29e23 65 72 61 74 69 6f 6e 20 77 69 6c 6c 20 66 61 69  eration will fai
29e24 6c 2c 20 62 75 74 20 74 68 65 20 66 6f 6c 6c 6f  l, but the follo
29e25 77 69 6e 67 20 6f 6e 65 0a 20 20 20 20 2a 2a 20  wing one.    ** 
29e26 77 69 6c 6c 20 61 74 74 65 6d 70 74 20 74 6f 20  will attempt to 
29e27 63 6f 6d 70 69 6c 65 20 74 68 65 20 73 75 70 70  compile the supp
29e28 6c 69 65 64 20 73 74 61 74 65 6d 65 6e 74 20 61  lied statement a
29e29 67 61 69 6e 73 74 20 77 68 61 74 65 76 65 72 20  gainst whatever 
29e2a 73 75 62 73 65 74 0a 20 20 20 20 2a 2a 20 6f 66  subset.    ** of
29e2b 20 74 68 65 20 73 63 68 65 6d 61 20 77 61 73 20   the schema was 
29e2c 6c 6f 61 64 65 64 20 62 65 66 6f 72 65 20 74 68  loaded before th
29e2d 65 20 65 72 72 6f 72 20 6f 63 63 75 72 72 65 64  e error occurred
29e2e 2e 20 54 68 65 20 70 72 69 6d 61 72 79 0a 20 20  . The primary.  
29e2f 20 20 2a 2a 20 70 75 72 70 6f 73 65 20 6f 66 20    ** purpose of 
29e30 74 68 69 73 20 69 73 20 74 6f 20 61 6c 6c 6f 77  this is to allow
29e31 20 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 73   access to the s
29e32 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
29e33 6c 65 0a 20 20 20 20 2a 2a 20 65 76 65 6e 20 77  le.    ** even w
29e34 68 65 6e 20 69 74 73 20 63 6f 6e 74 65 6e 74 73  hen its contents
29e35 20 68 61 76 65 20 62 65 65 6e 20 63 6f 72 72 75   have been corru
29e36 70 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  pted..    */.   
29e37 20 44 62 53 65 74 50 72 6f 70 65 72 74 79 28 64   DbSetProperty(d
29e38 62 2c 20 69 44 62 2c 20 44 42 5f 53 63 68 65 6d  b, iDb, DB_Schem
29e39 61 4c 6f 61 64 65 64 29 3b 0a 20 20 20 20 72 63  aLoaded);.    rc
29e3a 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
29e3b 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72  }..  /* Jump her
29e3c 65 20 66 6f 72 20 61 6e 20 65 72 72 6f 72 20 74  e for an error t
29e3d 68 61 74 20 6f 63 63 75 72 73 20 61 66 74 65 72  hat occurs after
29e3e 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6c   successfully al
29e3f 6c 6f 63 61 74 69 6e 67 0a 20 20 2a 2a 20 63 75  locating.  ** cu
29e40 72 4d 61 69 6e 20 61 6e 64 20 63 61 6c 6c 69 6e  rMain and callin
29e41 67 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e  g sqlite3BtreeEn
29e42 74 65 72 28 29 2e 20 46 6f 72 20 61 6e 20 65 72  ter(). For an er
29e43 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72 73 0a  ror that occurs.
29e44 20 20 2a 2a 20 62 65 66 6f 72 65 20 74 68 61 74    ** before that
29e45 20 70 6f 69 6e 74 2c 20 6a 75 6d 70 20 74 6f 20   point, jump to 
29e46 65 72 72 6f 72 5f 6f 75 74 2e 0a 20 20 2a 2f 0a  error_out..  */.
29e47 69 6e 69 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f 75  initone_error_ou
29e48 74 3a 0a 20 20 69 66 28 20 6f 70 65 6e 65 64 54  t:.  if( openedT
29e49 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20  ransaction ){.  
29e4a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f    sqlite3BtreeCo
29e4b 6d 6d 69 74 28 70 44 62 2d 3e 70 42 74 29 3b 0a  mmit(pDb->pBt);.
29e4c 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72    }.  sqlite3Btr
29e4d 65 65 4c 65 61 76 65 28 70 44 62 2d 3e 70 42 74  eeLeave(pDb->pBt
29e4e 29 3b 0a 0a 65 72 72 6f 72 5f 6f 75 74 3a 0a 20  );..error_out:. 
29e4f 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
29e50 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d 53 51 4c  NOMEM || rc==SQL
29e51 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20  ITE_IOERR_NOMEM 
29e52 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  ){.    db->mallo
29e53 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d  cFailed = 1;.  }
29e54 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
29e55 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a  ./*.** Initializ
29e56 65 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20 66  e all database f
29e57 69 6c 65 73 20 2d 20 74 68 65 20 6d 61 69 6e 20  iles - the main 
29e58 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74  database file, t
29e59 68 65 20 66 69 6c 65 0a 2a 2a 20 75 73 65 64 20  he file.** used 
29e5a 74 6f 20 73 74 6f 72 65 20 74 65 6d 70 6f 72 61  to store tempora
29e5b 72 79 20 74 61 62 6c 65 73 2c 20 61 6e 64 20 61  ry tables, and a
29e5c 6e 79 20 61 64 64 69 74 69 6f 6e 61 6c 20 64 61  ny additional da
29e5d 74 61 62 61 73 65 20 66 69 6c 65 73 0a 2a 2a 20  tabase files.** 
29e5e 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 41 54  created using AT
29e5f 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74 73 2e  TACH statements.
29e60 20 20 52 65 74 75 72 6e 20 61 20 73 75 63 63 65    Return a succe
29e61 73 73 20 63 6f 64 65 2e 20 20 49 66 20 61 6e 0a  ss code.  If an.
29e62 2a 2a 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  ** error occurs,
29e63 20 77 72 69 74 65 20 61 6e 20 65 72 72 6f 72 20   write an error 
29e64 6d 65 73 73 61 67 65 20 69 6e 74 6f 20 2a 70 7a  message into *pz
29e65 45 72 72 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 41 66  ErrMsg..**.** Af
29e66 74 65 72 20 61 20 64 61 74 61 62 61 73 65 20 69  ter a database i
29e67 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74  s initialized, t
29e68 68 65 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64  he DB_SchemaLoad
29e69 65 64 20 62 69 74 20 69 73 20 73 65 74 0a 2a 2a  ed bit is set.**
29e6a 20 62 69 74 20 69 73 20 73 65 74 20 69 6e 20 74   bit is set in t
29e6b 68 65 20 66 6c 61 67 73 20 66 69 65 6c 64 20 6f  he flags field o
29e6c 66 20 74 68 65 20 44 62 20 73 74 72 75 63 74 75  f the Db structu
29e6d 72 65 2e 20 49 66 20 74 68 65 20 64 61 74 61 62  re. If the datab
29e6e 61 73 65 0a 2a 2a 20 66 69 6c 65 20 77 61 73 20  ase.** file was 
29e6f 6f 66 20 7a 65 72 6f 2d 6c 65 6e 67 74 68 2c 20  of zero-length, 
29e70 74 68 65 6e 20 74 68 65 20 44 42 5f 45 6d 70 74  then the DB_Empt
29e71 79 20 66 6c 61 67 20 69 73 20 61 6c 73 6f 20 73  y flag is also s
29e72 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  et..*/.SQLITE_PR
29e73 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
29e74 33 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64  3Init(sqlite3 *d
29e75 62 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d  b, char **pzErrM
29e76 73 67 29 7b 0a 20 20 69 6e 74 20 69 2c 20 72 63  sg){.  int i, rc
29e77 3b 0a 20 20 69 6e 74 20 63 6f 6d 6d 69 74 5f 69  ;.  int commit_i
29e78 6e 74 65 72 6e 61 6c 20 3d 20 21 28 64 62 2d 3e  nternal = !(db->
29e79 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74  flags&SQLITE_Int
29e7a 65 72 6e 43 68 61 6e 67 65 73 29 3b 0a 20 20 0a  ernChanges);.  .
29e7b 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
29e7c 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
29e7d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20  >mutex) );.  rc 
29e7e 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 64  = SQLITE_OK;.  d
29e7f 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 31  b->init.busy = 1
29e80 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d  ;.  for(i=0; rc=
29e81 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c  =SQLITE_OK && i<
29e82 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
29e83 20 20 20 69 66 28 20 44 62 48 61 73 50 72 6f 70     if( DbHasProp
29e84 65 72 74 79 28 64 62 2c 20 69 2c 20 44 42 5f 53  erty(db, i, DB_S
29e85 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 7c 7c 20  chemaLoaded) || 
29e86 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b  i==1 ) continue;
29e87 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
29e88 33 49 6e 69 74 4f 6e 65 28 64 62 2c 20 69 2c 20  3InitOne(db, i, 
29e89 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 69  pzErrMsg);.    i
29e8a 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 73  f( rc ){.      s
29e8b 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
29e8c 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 69 29  nalSchema(db, i)
29e8d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
29e8e 2a 20 4f 6e 63 65 20 61 6c 6c 20 74 68 65 20 6f  * Once all the o
29e8f 74 68 65 72 20 64 61 74 61 62 61 73 65 73 20 68  ther databases h
29e90 61 76 65 20 62 65 65 6e 20 69 6e 69 74 69 61 6c  ave been initial
29e91 69 73 65 64 2c 20 6c 6f 61 64 20 74 68 65 20 73  ised, load the s
29e92 63 68 65 6d 61 0a 20 20 2a 2a 20 66 6f 72 20 74  chema.  ** for t
29e93 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65  he TEMP database
29e94 2e 20 54 68 69 73 20 69 73 20 6c 6f 61 64 65 64  . This is loaded
29e95 20 6c 61 73 74 2c 20 61 73 20 74 68 65 20 54 45   last, as the TE
29e96 4d 50 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a  MP database.  **
29e97 20 73 63 68 65 6d 61 20 6d 61 79 20 63 6f 6e 74   schema may cont
29e98 61 69 6e 20 72 65 66 65 72 65 6e 63 65 73 20 74  ain references t
29e99 6f 20 6f 62 6a 65 63 74 73 20 69 6e 20 6f 74 68  o objects in oth
29e9a 65 72 20 64 61 74 61 62 61 73 65 73 2e 0a 20 20  er databases..  
29e9b 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
29e9c 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20  E_OMIT_TEMPDB.  
29e9d 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
29e9e 4b 20 26 26 20 41 4c 57 41 59 53 28 64 62 2d 3e  K && ALWAYS(db->
29e9f 6e 44 62 3e 31 29 0a 20 20 20 20 20 20 20 20 20  nDb>1).         
29ea0 20 20 20 20 20 20 20 20 20 20 20 26 26 20 21 44             && !D
29ea1 62 48 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c  bHasProperty(db,
29ea2 20 31 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61   1, DB_SchemaLoa
29ea3 64 65 64 29 20 29 7b 0a 20 20 20 20 72 63 20 3d  ded) ){.    rc =
29ea4 20 73 71 6c 69 74 65 33 49 6e 69 74 4f 6e 65 28   sqlite3InitOne(
29ea5 64 62 2c 20 31 2c 20 70 7a 45 72 72 4d 73 67 29  db, 1, pzErrMsg)
29ea6 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a  ;.    if( rc ){.
29ea7 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73        sqlite3Res
29ea8 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
29ea9 28 64 62 2c 20 31 29 3b 0a 20 20 20 20 7d 0a 20  (db, 1);.    }. 
29eaa 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 64 62 2d   }.#endif..  db-
29eab 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 30 3b 0a  >init.busy = 0;.
29eac 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
29ead 5f 4f 4b 20 26 26 20 63 6f 6d 6d 69 74 5f 69 6e  _OK && commit_in
29eae 74 65 72 6e 61 6c 20 29 7b 0a 20 20 20 20 73 71  ternal ){.    sq
29eaf 6c 69 74 65 33 43 6f 6d 6d 69 74 49 6e 74 65 72  lite3CommitInter
29eb0 6e 61 6c 43 68 61 6e 67 65 73 28 64 62 29 3b 0a  nalChanges(db);.
29eb1 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
29eb2 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ; .}../*.** This
29eb3 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
29eb4 2d 6f 70 20 69 66 20 74 68 65 20 64 61 74 61 62  -op if the datab
29eb5 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 61 6c  ase schema is al
29eb6 72 65 61 64 79 20 69 6e 69 74 69 61 6c 69 73 65  ready initialise
29eb7 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  d..** Otherwise,
29eb8 20 74 68 65 20 73 63 68 65 6d 61 20 69 73 20 6c   the schema is l
29eb9 6f 61 64 65 64 2e 20 41 6e 20 65 72 72 6f 72 20  oaded. An error 
29eba 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
29ebb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
29ebc 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
29ebd 65 61 64 53 63 68 65 6d 61 28 50 61 72 73 65 20  eadSchema(Parse 
29ebe 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 6e 74 20  *pParse){.  int 
29ebf 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
29ec0 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
29ec1 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61 73  pParse->db;.  as
29ec2 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
29ec3 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
29ec4 65 78 29 20 29 3b 0a 20 20 69 66 28 20 21 64 62  ex) );.  if( !db
29ec5 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20  ->init.busy ){. 
29ec6 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49     rc = sqlite3I
29ec7 6e 69 74 28 64 62 2c 20 26 70 50 61 72 73 65 2d  nit(db, &pParse-
29ec8 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 20  >zErrMsg);.  }. 
29ec9 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
29eca 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65  OK ){.    pParse
29ecb 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 70  ->rc = rc;.    p
29ecc 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20  Parse->nErr++;. 
29ecd 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
29ece 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  }.../*.** Check 
29ecf 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 73 20 69  schema cookies i
29ed0 6e 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73 2e  n all databases.
29ed1 20 20 49 66 20 61 6e 79 20 63 6f 6f 6b 69 65 20    If any cookie 
29ed2 69 73 20 6f 75 74 0a 2a 2a 20 6f 66 20 64 61 74  is out.** of dat
29ed3 65 20 73 65 74 20 70 50 61 72 73 65 2d 3e 72 63  e set pParse->rc
29ed4 20 74 6f 20 53 51 4c 49 54 45 5f 53 43 48 45 4d   to SQLITE_SCHEM
29ed5 41 2e 20 20 49 66 20 61 6c 6c 20 73 63 68 65 6d  A.  If all schem
29ed6 61 20 63 6f 6f 6b 69 65 73 0a 2a 2a 20 6d 61 6b  a cookies.** mak
29ed7 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 74 6f 20  e no changes to 
29ed8 70 50 61 72 73 65 2d 3e 72 63 2e 0a 2a 2f 0a 73  pParse->rc..*/.s
29ed9 74 61 74 69 63 20 76 6f 69 64 20 73 63 68 65 6d  tatic void schem
29eda 61 49 73 56 61 6c 69 64 28 50 61 72 73 65 20 2a  aIsValid(Parse *
29edb 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74  pParse){.  sqlit
29edc 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
29edd 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a  >db;.  int iDb;.
29ede 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20    int rc;.  int 
29edf 63 6f 6f 6b 69 65 3b 0a 0a 20 20 61 73 73 65 72  cookie;..  asser
29ee0 74 28 20 70 50 61 72 73 65 2d 3e 63 68 65 63 6b  t( pParse->check
29ee1 53 63 68 65 6d 61 20 29 3b 0a 20 20 61 73 73 65  Schema );.  asse
29ee2 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
29ee3 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78  x_held(db->mutex
29ee4 29 20 29 3b 0a 20 20 66 6f 72 28 69 44 62 3d 30  ) );.  for(iDb=0
29ee5 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 69  ; iDb<db->nDb; i
29ee6 44 62 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6f  Db++){.    int o
29ee7 70 65 6e 65 64 54 72 61 6e 73 61 63 74 69 6f 6e  penedTransaction
29ee8 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a   = 0;         /*
29ee9 20 54 72 75 65 20 69 66 20 61 20 74 72 61 6e 73   True if a trans
29eea 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e 65 64  action is opened
29eeb 20 2a 2f 0a 20 20 20 20 42 74 72 65 65 20 2a 70   */.    Btree *p
29eec 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  Bt = db->aDb[iDb
29eed 5d 2e 70 42 74 3b 20 20 20 20 20 2f 2a 20 42 74  ].pBt;     /* Bt
29eee 72 65 65 20 64 61 74 61 62 61 73 65 20 74 6f 20  ree database to 
29eef 72 65 61 64 20 63 6f 6f 6b 69 65 20 66 72 6f 6d  read cookie from
29ef0 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 42 74 3d   */.    if( pBt=
29ef1 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a  =0 ) continue;..
29ef2 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
29ef3 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 61  is not already a
29ef4 20 72 65 61 64 2d 6f 6e 6c 79 20 28 6f 72 20 72   read-only (or r
29ef5 65 61 64 2d 77 72 69 74 65 29 20 74 72 61 6e 73  ead-write) trans
29ef6 61 63 74 69 6f 6e 20 6f 70 65 6e 65 64 0a 20 20  action opened.  
29ef7 20 20 2a 2a 20 6f 6e 20 74 68 65 20 62 2d 74 72    ** on the b-tr
29ef8 65 65 20 64 61 74 61 62 61 73 65 2c 20 6f 70 65  ee database, ope
29ef9 6e 20 6f 6e 65 20 6e 6f 77 2e 20 49 66 20 61 20  n one now. If a 
29efa 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f  transaction is o
29efb 70 65 6e 65 64 2c 20 69 74 20 0a 20 20 20 20 2a  pened, it .    *
29efc 2a 20 77 69 6c 6c 20 62 65 20 63 6c 6f 73 65 64  * will be closed
29efd 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74   immediately aft
29efe 65 72 20 72 65 61 64 69 6e 67 20 74 68 65 20 6d  er reading the m
29eff 65 74 61 2d 76 61 6c 75 65 2e 20 2a 2f 0a 20 20  eta-value. */.  
29f00 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 42 74    if( !sqlite3Bt
29f01 72 65 65 49 73 49 6e 52 65 61 64 54 72 61 6e 73  reeIsInReadTrans
29f02 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 72  (pBt) ){.      r
29f03 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
29f04 42 65 67 69 6e 54 72 61 6e 73 28 70 42 74 2c 20  BeginTrans(pBt, 
29f05 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  0);.      if( rc
29f06 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c  ==SQLITE_NOMEM |
29f07 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45  | rc==SQLITE_IOE
29f08 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20  RR_NOMEM ){.    
29f09 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
29f0a 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
29f0b 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  }.      if( rc!=
29f0c 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
29f0d 72 6e 3b 0a 20 20 20 20 20 20 6f 70 65 6e 65 64  rn;.      opened
29f0e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20 31 3b  Transaction = 1;
29f0f 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52  .    }..    /* R
29f10 65 61 64 20 74 68 65 20 73 63 68 65 6d 61 20 63  ead the schema c
29f11 6f 6f 6b 69 65 20 66 72 6f 6d 20 74 68 65 20 64  ookie from the d
29f12 61 74 61 62 61 73 65 2e 20 49 66 20 69 74 20 64  atabase. If it d
29f13 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 74 68  oes not match th
29f14 65 20 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 20  e .    ** value 
29f15 73 74 6f 72 65 64 20 61 73 20 70 61 72 74 20 6f  stored as part o
29f16 66 20 74 68 65 20 69 6e 20 74 68 65 20 69 6e 2d  f the in the in-
29f17 6d 65 6d 6f 72 79 20 73 63 68 65 6d 61 20 72 65  memory schema re
29f18 70 72 65 73 65 6e 74 61 74 69 6f 6e 2c 0a 20 20  presentation,.  
29f19 20 20 2a 2a 20 73 65 74 20 50 61 72 73 65 2e 72    ** set Parse.r
29f1a 63 20 74 6f 20 53 51 4c 49 54 45 5f 53 43 48 45  c to SQLITE_SCHE
29f1b 4d 41 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  MA. */.    sqlit
29f1c 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28 70  e3BtreeGetMeta(p
29f1d 42 74 2c 20 42 54 52 45 45 5f 53 43 48 45 4d 41  Bt, BTREE_SCHEMA
29f1e 5f 56 45 52 53 49 4f 4e 2c 20 28 75 33 32 20 2a  _VERSION, (u32 *
29f1f 29 26 63 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 69  )&cookie);.    i
29f20 66 28 20 63 6f 6f 6b 69 65 21 3d 64 62 2d 3e 61  f( cookie!=db->a
29f21 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d  Db[iDb].pSchema-
29f22 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 29  >schema_cookie )
29f23 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  {.      pParse->
29f24 72 63 20 3d 20 53 51 4c 49 54 45 5f 53 43 48 45  rc = SQLITE_SCHE
29f25 4d 41 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  MA;.    }..    /
29f26 2a 20 43 6c 6f 73 65 20 74 68 65 20 74 72 61 6e  * Close the tran
29f27 73 61 63 74 69 6f 6e 2c 20 69 66 20 6f 6e 65 20  saction, if one 
29f28 77 61 73 20 6f 70 65 6e 65 64 2e 20 2a 2f 0a 20  was opened. */. 
29f29 20 20 20 69 66 28 20 6f 70 65 6e 65 64 54 72 61     if( openedTra
29f2a 6e 73 61 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20  nsaction ){.    
29f2b 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f    sqlite3BtreeCo
29f2c 6d 6d 69 74 28 70 42 74 29 3b 0a 20 20 20 20 7d  mmit(pBt);.    }
29f2d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  .  }.}../*.** Co
29f2e 6e 76 65 72 74 20 61 20 73 63 68 65 6d 61 20 70  nvert a schema p
29f2f 6f 69 6e 74 65 72 20 69 6e 74 6f 20 74 68 65 20  ointer into the 
29f30 69 44 62 20 69 6e 64 65 78 20 74 68 61 74 20 69  iDb index that i
29f31 6e 64 69 63 61 74 65 73 0a 2a 2a 20 77 68 69 63  ndicates.** whic
29f32 68 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  h database file 
29f33 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 74 68 65  in db->aDb[] the
29f34 20 73 63 68 65 6d 61 20 72 65 66 65 72 73 20 74   schema refers t
29f35 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  o..**.** If the 
29f36 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 69 73  same database is
29f37 20 61 74 74 61 63 68 65 64 20 6d 6f 72 65 20 74   attached more t
29f38 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 66 69  han once, the fi
29f39 72 73 74 0a 2a 2a 20 61 74 74 61 63 68 65 64 20  rst.** attached 
29f3a 64 61 74 61 62 61 73 65 20 69 73 20 72 65 74 75  database is retu
29f3b 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rned..*/.SQLITE_
29f3c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
29f3d 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
29f3e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 63  (sqlite3 *db, Sc
29f3f 68 65 6d 61 20 2a 70 53 63 68 65 6d 61 29 7b 0a  hema *pSchema){.
29f40 20 20 69 6e 74 20 69 20 3d 20 2d 31 30 30 30 30    int i = -10000
29f41 30 30 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 53 63  00;..  /* If pSc
29f42 68 65 6d 61 20 69 73 20 4e 55 4c 4c 2c 20 74 68  hema is NULL, th
29f43 65 6e 20 72 65 74 75 72 6e 20 2d 31 30 30 30 30  en return -10000
29f44 30 30 2e 20 54 68 69 73 20 68 61 70 70 65 6e 73  00. This happens
29f45 20 77 68 65 6e 20 63 6f 64 65 20 69 6e 20 0a 20   when code in . 
29f46 20 2a 2a 20 65 78 70 72 2e 63 20 69 73 20 74 72   ** expr.c is tr
29f47 79 69 6e 67 20 74 6f 20 72 65 73 6f 6c 76 65 20  ying to resolve 
29f48 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61  a reference to a
29f49 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c 65   transient table
29f4a 20 28 69 2e 65 2e 20 6f 6e 65 0a 20 20 2a 2a 20   (i.e. one.  ** 
29f4b 63 72 65 61 74 65 64 20 62 79 20 61 20 73 75 62  created by a sub
29f4c 2d 73 65 6c 65 63 74 29 2e 20 49 6e 20 74 68 69  -select). In thi
29f4d 73 20 63 61 73 65 20 74 68 65 20 72 65 74 75 72  s case the retur
29f4e 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20  n value of this 
29f4f 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 73  .  ** function s
29f50 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 75  hould never be u
29f51 73 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57  sed..  **.  ** W
29f52 65 20 72 65 74 75 72 6e 20 2d 31 30 30 30 30 30  e return -100000
29f53 30 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65  0 instead of the
29f54 20 6d 6f 72 65 20 75 73 75 61 6c 20 2d 31 20 73   more usual -1 s
29f55 69 6d 70 6c 79 20 62 65 63 61 75 73 65 20 75 73  imply because us
29f56 69 6e 67 0a 20 20 2a 2a 20 2d 31 30 30 30 30 30  ing.  ** -100000
29f57 30 20 61 73 20 74 68 65 20 69 6e 63 6f 72 72 65  0 as the incorre
29f58 63 74 20 69 6e 64 65 78 20 69 6e 74 6f 20 64 62  ct index into db
29f59 2d 3e 61 44 62 5b 5d 20 69 73 20 6d 75 63 68 20  ->aDb[] is much 
29f5a 0a 20 20 2a 2a 20 6d 6f 72 65 20 6c 69 6b 65 6c  .  ** more likel
29f5b 79 20 74 6f 20 63 61 75 73 65 20 61 20 73 65 67  y to cause a seg
29f5c 66 61 75 6c 74 20 74 68 61 6e 20 2d 31 20 28 6f  fault than -1 (o
29f5d 66 20 63 6f 75 72 73 65 20 74 68 65 72 65 20 61  f course there a
29f5e 72 65 20 61 73 73 65 72 74 28 29 0a 20 20 2a 2a  re assert().  **
29f5f 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 6f 2c   statements too,
29f60 20 62 75 74 20 69 74 20 6e 65 76 65 72 20 68 75   but it never hu
29f61 72 74 73 20 74 6f 20 70 6c 61 79 20 74 68 65 20  rts to play the 
29f62 6f 64 64 73 29 2e 0a 20 20 2a 2f 0a 20 20 61 73  odds)..  */.  as
29f63 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
29f64 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
29f65 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 53 63  ex) );.  if( pSc
29f66 68 65 6d 61 20 29 7b 0a 20 20 20 20 66 6f 72 28  hema ){.    for(
29f67 69 3d 30 3b 20 41 4c 57 41 59 53 28 69 3c 64 62  i=0; ALWAYS(i<db
29f68 2d 3e 6e 44 62 29 3b 20 69 2b 2b 29 7b 0a 20 20  ->nDb); i++){.  
29f69 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b      if( db->aDb[
29f6a 69 5d 2e 70 53 63 68 65 6d 61 3d 3d 70 53 63 68  i].pSchema==pSch
29f6b 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 20 20 62  ema ){.        b
29f6c 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
29f6d 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
29f6e 69 3e 3d 30 20 26 26 20 69 3c 64 62 2d 3e 6e 44  i>=0 && i<db->nD
29f6f 62 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  b );.  }.  retur
29f70 6e 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  n i;.}../*.** Co
29f71 6d 70 69 6c 65 20 74 68 65 20 55 54 46 2d 38 20  mpile the UTF-8 
29f72 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74  encoded SQL stat
29f73 65 6d 65 6e 74 20 7a 53 71 6c 20 69 6e 74 6f 20  ement zSql into 
29f74 61 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  a statement hand
29f75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
29f76 74 20 73 71 6c 69 74 65 33 50 72 65 70 61 72 65  t sqlite3Prepare
29f77 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
29f78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
29f79 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
29f7a 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  . */.  const cha
29f7b 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20  r *zSql,        
29f7c 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65   /* UTF-8 encode
29f7d 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  d SQL statement.
29f7e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73   */.  int nBytes
29f7f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
29f80 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71  /* Length of zSq
29f81 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20  l in bytes. */. 
29f82 20 69 6e 74 20 73 61 76 65 53 71 6c 46 6c 61 67   int saveSqlFlag
29f83 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ,          /* Tr
29f84 75 65 20 74 6f 20 63 6f 70 79 20 53 51 4c 20 74  ue to copy SQL t
29f85 65 78 74 20 69 6e 74 6f 20 74 68 65 20 73 71 6c  ext into the sql
29f86 69 74 65 33 5f 73 74 6d 74 20 2a 2f 0a 20 20 56  ite3_stmt */.  V
29f87 64 62 65 20 2a 70 52 65 70 72 65 70 61 72 65 2c  dbe *pReprepare,
29f88 20 20 20 20 20 20 20 20 20 2f 2a 20 56 4d 20 62           /* VM b
29f89 65 69 6e 67 20 72 65 70 72 65 70 61 72 65 64 20  eing reprepared 
29f8a 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
29f8b 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f  t **ppStmt,    /
29f8c 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72  * OUT: A pointer
29f8d 20 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64   to the prepared
29f8e 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
29f8f 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54  const char **pzT
29f90 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54  ail       /* OUT
29f91 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20  : End of parsed 
29f92 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 50  string */.){.  P
29f93 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20  arse *pParse;   
29f94 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
29f95 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
29f96 20 63 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d   char *zErrMsg =
29f97 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 45 72   0;        /* Er
29f98 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20  ror message */. 
29f99 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
29f9a 5f 4f 4b 3b 20 20 20 20 20 20 20 2f 2a 20 52 65  _OK;       /* Re
29f9b 73 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 20 20 69  sult code */.  i
29f9c 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
29f9d 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70           /* Loop
29f9e 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 2f   counter */..  /
29f9f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 70  * Allocate the p
29fa0 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
29fa1 2f 0a 20 20 70 50 61 72 73 65 20 3d 20 73 71 6c  /.  pParse = sql
29fa2 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65  ite3StackAllocZe
29fa3 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70  ro(db, sizeof(*p
29fa4 50 61 72 73 65 29 29 3b 0a 20 20 69 66 28 20 70  Parse));.  if( p
29fa5 50 61 72 73 65 3d 3d 30 20 29 7b 0a 20 20 20 20  Parse==0 ){.    
29fa6 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
29fa7 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  M;.    goto end_
29fa8 70 72 65 70 61 72 65 3b 0a 20 20 7d 0a 20 20 70  prepare;.  }.  p
29fa9 50 61 72 73 65 2d 3e 70 52 65 70 72 65 70 61 72  Parse->pReprepar
29faa 65 20 3d 20 70 52 65 70 72 65 70 61 72 65 3b 0a  e = pReprepare;.
29fab 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 61  .  if( sqlite3Sa
29fac 66 65 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20  fetyOn(db) ){.  
29fad 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49    rc = SQLITE_MI
29fae 53 55 53 45 3b 0a 20 20 20 20 67 6f 74 6f 20 65  SUSE;.    goto e
29faf 6e 64 5f 70 72 65 70 61 72 65 3b 0a 20 20 7d 0a  nd_prepare;.  }.
29fb0 20 20 61 73 73 65 72 74 28 20 70 70 53 74 6d 74    assert( ppStmt
29fb1 20 26 26 20 2a 70 70 53 74 6d 74 3d 3d 30 20 29   && *ppStmt==0 )
29fb2 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d  ;.  assert( !db-
29fb3 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
29fb4 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
29fb5 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
29fb6 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f  ->mutex) );..  /
29fb7 2a 20 43 68 65 63 6b 20 74 6f 20 76 65 72 69 66  * Check to verif
29fb8 79 20 74 68 61 74 20 69 74 20 69 73 20 70 6f 73  y that it is pos
29fb9 73 69 62 6c 65 20 74 6f 20 67 65 74 20 61 20 72  sible to get a r
29fba 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 61 6c 6c 0a  ead lock on all.
29fbb 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63    ** database sc
29fbc 68 65 6d 61 73 2e 20 20 54 68 65 20 69 6e 61 62  hemas.  The inab
29fbd 69 6c 69 74 79 20 74 6f 20 67 65 74 20 61 20 72  ility to get a r
29fbe 65 61 64 20 6c 6f 63 6b 20 69 6e 64 69 63 61 74  ead lock indicat
29fbf 65 73 20 74 68 61 74 0a 20 20 2a 2a 20 73 6f 6d  es that.  ** som
29fc0 65 20 6f 74 68 65 72 20 64 61 74 61 62 61 73 65  e other database
29fc1 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 68   connection is h
29fc2 6f 6c 64 69 6e 67 20 61 20 77 72 69 74 65 2d 6c  olding a write-l
29fc3 6f 63 6b 2c 20 77 68 69 63 68 20 69 6e 0a 20 20  ock, which in.  
29fc4 2a 2a 20 74 75 72 6e 20 6d 65 61 6e 73 20 74 68  ** turn means th
29fc5 61 74 20 74 68 65 20 6f 74 68 65 72 20 63 6f 6e  at the other con
29fc6 6e 65 63 74 69 6f 6e 20 68 61 73 20 6d 61 64 65  nection has made
29fc7 20 75 6e 63 6f 6d 6d 69 74 74 65 64 20 63 68 61   uncommitted cha
29fc8 6e 67 65 73 0a 20 20 2a 2a 20 74 6f 20 74 68 65  nges.  ** to the
29fc9 20 73 63 68 65 6d 61 2e 0a 20 20 2a 2a 0a 20 20   schema..  **.  
29fca 2a 2a 20 57 65 72 65 20 77 65 20 74 6f 20 70 72  ** Were we to pr
29fcb 6f 63 65 65 64 20 61 6e 64 20 70 72 65 70 61 72  oceed and prepar
29fcc 65 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  e the statement 
29fcd 61 67 61 69 6e 73 74 20 74 68 65 20 75 6e 63 6f  against the unco
29fce 6d 6d 69 74 74 65 64 0a 20 20 2a 2a 20 73 63 68  mmitted.  ** sch
29fcf 65 6d 61 20 63 68 61 6e 67 65 73 20 61 6e 64 20  ema changes and 
29fd0 69 66 20 74 68 6f 73 65 20 73 63 68 65 6d 61 20  if those schema 
29fd1 63 68 61 6e 67 65 73 20 61 72 65 20 73 75 62 73  changes are subs
29fd2 65 71 75 65 6e 74 6c 79 20 72 6f 6c 6c 65 64 0a  equently rolled.
29fd3 20 20 2a 2a 20 62 61 63 6b 20 61 6e 64 20 64 69    ** back and di
29fd4 66 66 65 72 65 6e 74 20 63 68 61 6e 67 65 73 20  fferent changes 
29fd5 61 72 65 20 6d 61 64 65 20 69 6e 20 74 68 65 69  are made in thei
29fd6 72 20 70 6c 61 63 65 2c 20 74 68 65 6e 20 77 68  r place, then wh
29fd7 65 6e 20 74 68 69 73 0a 20 20 2a 2a 20 70 72 65  en this.  ** pre
29fd8 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
29fd9 67 6f 65 73 20 74 6f 20 72 75 6e 20 74 68 65 20  goes to run the 
29fda 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 77 6f  schema cookie wo
29fdb 75 6c 64 20 66 61 69 6c 20 74 6f 20 64 65 74 65  uld fail to dete
29fdc 63 74 0a 20 20 2a 2a 20 74 68 65 20 73 63 68 65  ct.  ** the sche
29fdd 6d 61 20 63 68 61 6e 67 65 2e 20 20 44 69 73 61  ma change.  Disa
29fde 73 74 65 72 20 77 6f 75 6c 64 20 66 6f 6c 6c 6f  ster would follo
29fdf 77 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69  w..  **.  ** Thi
29fe0 73 20 74 68 72 65 61 64 20 69 73 20 63 75 72 72  s thread is curr
29fe1 65 6e 74 6c 79 20 68 6f 6c 64 69 6e 67 20 6d 75  ently holding mu
29fe2 74 65 78 65 73 20 6f 6e 20 61 6c 6c 20 42 74 72  texes on all Btr
29fe3 65 65 73 20 28 62 65 63 61 75 73 65 0a 20 20 2a  ees (because.  *
29fe4 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  * of the sqlite3
29fe5 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 29 20  BtreeEnterAll() 
29fe6 69 6e 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e  in sqlite3LockAn
29fe7 64 50 72 65 70 61 72 65 28 29 29 20 73 6f 20 69  dPrepare()) so i
29fe8 74 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 70 6f  t.  ** is not po
29fe9 73 73 69 62 6c 65 20 66 6f 72 20 61 6e 6f 74 68  ssible for anoth
29fea 65 72 20 74 68 72 65 61 64 20 74 6f 20 73 74 61  er thread to sta
29feb 72 74 20 61 20 6e 65 77 20 73 63 68 65 6d 61 20  rt a new schema 
29fec 63 68 61 6e 67 65 0a 20 20 2a 2a 20 77 68 69 6c  change.  ** whil
29fed 65 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  e this routine i
29fee 73 20 72 75 6e 6e 69 6e 67 2e 20 20 48 65 6e 63  s running.  Henc
29fef 65 2c 20 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65  e, we do not nee
29ff0 64 20 74 6f 20 68 6f 6c 64 20 0a 20 20 2a 2a 20  d to hold .  ** 
29ff1 6c 6f 63 6b 73 20 6f 6e 20 74 68 65 20 73 63 68  locks on the sch
29ff2 65 6d 61 2c 20 77 65 20 6a 75 73 74 20 6e 65 65  ema, we just nee
29ff3 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 6e  d to make sure n
29ff4 6f 62 6f 64 79 20 65 6c 73 65 20 69 73 20 0a 20  obody else is . 
29ff5 20 2a 2a 20 68 6f 6c 64 69 6e 67 20 74 68 65 6d   ** holding them
29ff6 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65  ..  **.  ** Note
29ff7 20 74 68 61 74 20 73 65 74 74 69 6e 67 20 52 45   that setting RE
29ff8 41 44 5f 55 4e 43 4f 4d 4d 49 54 54 45 44 20 6f  AD_UNCOMMITTED o
29ff9 76 65 72 72 69 64 65 73 20 6d 6f 73 74 20 6c 6f  verrides most lo
29ffa 63 6b 20 64 65 74 65 63 74 69 6f 6e 2c 0a 20 20  ck detection,.  
29ffb 2a 2a 20 62 75 74 20 69 74 20 64 6f 65 73 20 2a  ** but it does *
29ffc 6e 6f 74 2a 20 6f 76 65 72 72 69 64 65 20 73 63  not* override sc
29ffd 68 65 6d 61 20 6c 6f 63 6b 20 64 65 74 65 63 74  hema lock detect
29ffe 69 6f 6e 2c 20 73 6f 20 74 68 69 73 20 61 6c 6c  ion, so this all
29fff 20 73 74 69 6c 6c 0a 20 20 2a 2a 20 77 6f 72 6b   still.  ** work
2a000 73 20 65 76 65 6e 20 69 66 20 52 45 41 44 5f 55  s even if READ_U
2a001 4e 43 4f 4d 4d 49 54 54 45 44 20 69 73 20 73 65  NCOMMITTED is se
2a002 74 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  t..  */.  for(i=
2a003 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
2a004 2b 29 20 7b 0a 20 20 20 20 42 74 72 65 65 20 2a  +) {.    Btree *
2a005 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d  pBt = db->aDb[i]
2a006 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 42  .pBt;.    if( pB
2a007 74 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  t ){.      asser
2a008 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
2a009 6f 6c 64 73 4d 75 74 65 78 28 70 42 74 29 20 29  oldsMutex(pBt) )
2a00a 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
2a00b 69 74 65 33 42 74 72 65 65 53 63 68 65 6d 61 4c  ite3BtreeSchemaL
2a00c 6f 63 6b 65 64 28 70 42 74 29 3b 0a 20 20 20 20  ocked(pBt);.    
2a00d 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
2a00e 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2a00f 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d  zDb = db->aDb[i]
2a010 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20  .zName;.        
2a011 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
2a012 20 72 63 2c 20 22 64 61 74 61 62 61 73 65 20 73   rc, "database s
2a013 63 68 65 6d 61 20 69 73 20 6c 6f 63 6b 65 64 3a  chema is locked:
2a014 20 25 73 22 2c 20 7a 44 62 29 3b 0a 20 20 20 20   %s", zDb);.    
2a015 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
2a016 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
2a017 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65          testcase
2a018 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ( db->flags & SQ
2a019 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69  LITE_ReadUncommi
2a01a 74 74 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20  tted );.        
2a01b 67 6f 74 6f 20 65 6e 64 5f 70 72 65 70 61 72 65  goto end_prepare
2a01c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2a01d 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 56 74    }..  sqlite3Vt
2a01e 61 62 55 6e 6c 6f 63 6b 4c 69 73 74 28 64 62 29  abUnlockList(db)
2a01f 3b 0a 0a 20 20 70 50 61 72 73 65 2d 3e 64 62 20  ;..  pParse->db 
2a020 3d 20 64 62 3b 0a 20 20 69 66 28 20 6e 42 79 74  = db;.  if( nByt
2a021 65 73 3e 3d 30 20 26 26 20 28 6e 42 79 74 65 73  es>=0 && (nBytes
2a022 3d 3d 30 20 7c 7c 20 7a 53 71 6c 5b 6e 42 79 74  ==0 || zSql[nByt
2a023 65 73 2d 31 5d 21 3d 30 29 20 29 7b 0a 20 20 20  es-1]!=0) ){.   
2a024 20 63 68 61 72 20 2a 7a 53 71 6c 43 6f 70 79 3b   char *zSqlCopy;
2a025 0a 20 20 20 20 69 6e 74 20 6d 78 4c 65 6e 20 3d  .    int mxLen =
2a026 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49   db->aLimit[SQLI
2a027 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e  TE_LIMIT_SQL_LEN
2a028 47 54 48 5d 3b 0a 20 20 20 20 74 65 73 74 63 61  GTH];.    testca
2a029 73 65 28 20 6e 42 79 74 65 73 3d 3d 6d 78 4c 65  se( nBytes==mxLe
2a02a 6e 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  n );.    testcas
2a02b 65 28 20 6e 42 79 74 65 73 3d 3d 6d 78 4c 65 6e  e( nBytes==mxLen
2a02c 2b 31 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 42  +1 );.    if( nB
2a02d 79 74 65 73 3e 6d 78 4c 65 6e 20 29 7b 0a 20 20  ytes>mxLen ){.  
2a02e 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2a02f 28 64 62 2c 20 53 51 4c 49 54 45 5f 54 4f 4f 42  (db, SQLITE_TOOB
2a030 49 47 2c 20 22 73 74 61 74 65 6d 65 6e 74 20 74  IG, "statement t
2a031 6f 6f 20 6c 6f 6e 67 22 29 3b 0a 20 20 20 20 20  oo long");.     
2a032 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
2a033 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20 20  fetyOff(db);.   
2a034 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41     rc = sqlite3A
2a035 70 69 45 78 69 74 28 64 62 2c 20 53 51 4c 49 54  piExit(db, SQLIT
2a036 45 5f 54 4f 4f 42 49 47 29 3b 0a 20 20 20 20 20  E_TOOBIG);.     
2a037 20 67 6f 74 6f 20 65 6e 64 5f 70 72 65 70 61 72   goto end_prepar
2a038 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 53 71  e;.    }.    zSq
2a039 6c 43 6f 70 79 20 3d 20 73 71 6c 69 74 65 33 44  lCopy = sqlite3D
2a03a 62 53 74 72 4e 44 75 70 28 64 62 2c 20 7a 53 71  bStrNDup(db, zSq
2a03b 6c 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20  l, nBytes);.    
2a03c 69 66 28 20 7a 53 71 6c 43 6f 70 79 20 29 7b 0a  if( zSqlCopy ){.
2a03d 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 75 6e        sqlite3Run
2a03e 50 61 72 73 65 72 28 70 50 61 72 73 65 2c 20 7a  Parser(pParse, z
2a03f 53 71 6c 43 6f 70 79 2c 20 26 7a 45 72 72 4d 73  SqlCopy, &zErrMs
2a040 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  g);.      sqlite
2a041 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c  3DbFree(db, zSql
2a042 43 6f 70 79 29 3b 0a 20 20 20 20 20 20 70 50 61  Copy);.      pPa
2a043 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a 53  rse->zTail = &zS
2a044 71 6c 5b 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c  ql[pParse->zTail
2a045 2d 7a 53 71 6c 43 6f 70 79 5d 3b 0a 20 20 20 20  -zSqlCopy];.    
2a046 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 50 61  }else{.      pPa
2a047 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 26 7a 53  rse->zTail = &zS
2a048 71 6c 5b 6e 42 79 74 65 73 5d 3b 0a 20 20 20 20  ql[nBytes];.    
2a049 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  }.  }else{.    s
2a04a 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72 28  qlite3RunParser(
2a04b 70 50 61 72 73 65 2c 20 7a 53 71 6c 2c 20 26 7a  pParse, zSql, &z
2a04c 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 0a 20 20  ErrMsg);.  }..  
2a04d 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
2a04e 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 50 61 72  iled ){.    pPar
2a04f 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f  se->rc = SQLITE_
2a050 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28  NOMEM;.  }.  if(
2a051 20 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c   pParse->rc==SQL
2a052 49 54 45 5f 44 4f 4e 45 20 29 20 70 50 61 72 73  ITE_DONE ) pPars
2a053 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  e->rc = SQLITE_O
2a054 4b 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  K;.  if( pParse-
2a055 3e 63 68 65 63 6b 53 63 68 65 6d 61 20 29 7b 0a  >checkSchema ){.
2a056 20 20 20 20 73 63 68 65 6d 61 49 73 56 61 6c 69      schemaIsVali
2a057 64 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 20  d(pParse);.  }. 
2a058 20 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63 3d   if( pParse->rc=
2a059 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 29  =SQLITE_SCHEMA )
2a05a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73  {.    sqlite3Res
2a05b 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
2a05c 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69  (db, 0);.  }.  i
2a05d 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
2a05e 6c 65 64 20 29 7b 0a 20 20 20 20 70 50 61 72 73  led ){.    pPars
2a05f 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e  e->rc = SQLITE_N
2a060 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20  OMEM;.  }.  if( 
2a061 70 7a 54 61 69 6c 20 29 7b 0a 20 20 20 20 2a 70  pzTail ){.    *p
2a062 7a 54 61 69 6c 20 3d 20 70 50 61 72 73 65 2d 3e  zTail = pParse->
2a063 7a 54 61 69 6c 3b 0a 20 20 7d 0a 20 20 72 63 20  zTail;.  }.  rc 
2a064 3d 20 70 50 61 72 73 65 2d 3e 72 63 3b 0a 0a 23  = pParse->rc;..#
2a065 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2a066 49 54 5f 45 58 50 4c 41 49 4e 0a 20 20 69 66 28  IT_EXPLAIN.  if(
2a067 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
2a068 26 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20  & pParse->pVdbe 
2a069 26 26 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61  && pParse->expla
2a06a 69 6e 20 29 7b 0a 20 20 20 20 73 74 61 74 69 63  in ){.    static
2a06b 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f   const char * co
2a06c 6e 73 74 20 61 7a 43 6f 6c 4e 61 6d 65 5b 5d 20  nst azColName[] 
2a06d 3d 20 7b 0a 20 20 20 20 20 20 20 22 61 64 64 72  = {.       "addr
2a06e 22 2c 20 22 6f 70 63 6f 64 65 22 2c 20 22 70 31  ", "opcode", "p1
2a06f 22 2c 20 22 70 32 22 2c 20 22 70 33 22 2c 20 22  ", "p2", "p3", "
2a070 70 34 22 2c 20 22 70 35 22 2c 20 22 63 6f 6d 6d  p4", "p5", "comm
2a071 65 6e 74 22 2c 0a 20 20 20 20 20 20 20 22 6f 72  ent",.       "or
2a072 64 65 72 22 2c 20 22 66 72 6f 6d 22 2c 20 22 64  der", "from", "d
2a073 65 74 61 69 6c 22 0a 20 20 20 20 7d 3b 0a 20 20  etail".    };.  
2a074 20 20 69 6e 74 20 69 46 69 72 73 74 2c 20 6d 78    int iFirst, mx
2a075 3b 0a 20 20 20 20 69 66 28 20 70 50 61 72 73 65  ;.    if( pParse
2a076 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a  ->explain==2 ){.
2a077 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2a078 65 53 65 74 4e 75 6d 43 6f 6c 73 28 70 50 61 72  eSetNumCols(pPar
2a079 73 65 2d 3e 70 56 64 62 65 2c 20 33 29 3b 0a 20  se->pVdbe, 3);. 
2a07a 20 20 20 20 20 69 46 69 72 73 74 20 3d 20 38 3b       iFirst = 8;
2a07b 0a 20 20 20 20 20 20 6d 78 20 3d 20 31 31 3b 0a  .      mx = 11;.
2a07c 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2a07d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e   sqlite3VdbeSetN
2a07e 75 6d 43 6f 6c 73 28 70 50 61 72 73 65 2d 3e 70  umCols(pParse->p
2a07f 56 64 62 65 2c 20 38 29 3b 0a 20 20 20 20 20 20  Vdbe, 8);.      
2a080 69 46 69 72 73 74 20 3d 20 30 3b 0a 20 20 20 20  iFirst = 0;.    
2a081 20 20 6d 78 20 3d 20 38 3b 0a 20 20 20 20 7d 0a    mx = 8;.    }.
2a082 20 20 20 20 66 6f 72 28 69 3d 69 46 69 72 73 74      for(i=iFirst
2a083 3b 20 69 3c 6d 78 3b 20 69 2b 2b 29 7b 0a 20 20  ; i<mx; i++){.  
2a084 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
2a085 65 74 43 6f 6c 4e 61 6d 65 28 70 50 61 72 73 65  etColName(pParse
2a086 2d 3e 70 56 64 62 65 2c 20 69 2d 69 46 69 72 73  ->pVdbe, i-iFirs
2a087 74 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  t, COLNAME_NAME,
2a088 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2a089 20 20 20 20 20 20 20 20 20 20 20 20 20 61 7a 43               azC
2a08a 6f 6c 4e 61 6d 65 5b 69 5d 2c 20 53 51 4c 49 54  olName[i], SQLIT
2a08b 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d  E_STATIC);.    }
2a08c 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69  .  }.#endif..  i
2a08d 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
2a08e 4f 66 66 28 64 62 29 20 29 7b 0a 20 20 20 20 72  Off(db) ){.    r
2a08f 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  c = SQLITE_MISUS
2a090 45 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  E;.  }..  assert
2a091 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d  ( db->init.busy=
2a092 3d 30 20 7c 7c 20 73 61 76 65 53 71 6c 46 6c 61  =0 || saveSqlFla
2a093 67 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 64 62  g==0 );.  if( db
2a094 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 30 20 29  ->init.busy==0 )
2a095 7b 0a 20 20 20 20 56 64 62 65 20 2a 70 56 64 62  {.    Vdbe *pVdb
2a096 65 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  e = pParse->pVdb
2a097 65 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  e;.    sqlite3Vd
2a098 62 65 53 65 74 53 71 6c 28 70 56 64 62 65 2c 20  beSetSql(pVdbe, 
2a099 7a 53 71 6c 2c 20 28 69 6e 74 29 28 70 50 61 72  zSql, (int)(pPar
2a09a 73 65 2d 3e 7a 54 61 69 6c 2d 7a 53 71 6c 29 2c  se->zTail-zSql),
2a09b 20 73 61 76 65 53 71 6c 46 6c 61 67 29 3b 0a 20   saveSqlFlag);. 
2a09c 20 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d   }.  if( pParse-
2a09d 3e 70 56 64 62 65 20 26 26 20 28 72 63 21 3d 53  >pVdbe && (rc!=S
2a09e 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e  QLITE_OK || db->
2a09f 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20 29 7b  mallocFailed) ){
2a0a0 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2a0a1 46 69 6e 61 6c 69 7a 65 28 70 50 61 72 73 65 2d  Finalize(pParse-
2a0a2 3e 70 56 64 62 65 29 3b 0a 20 20 20 20 61 73 73  >pVdbe);.    ass
2a0a3 65 72 74 28 21 28 2a 70 70 53 74 6d 74 29 29 3b  ert(!(*ppStmt));
2a0a4 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 70  .  }else{.    *p
2a0a5 70 53 74 6d 74 20 3d 20 28 73 71 6c 69 74 65 33  pStmt = (sqlite3
2a0a6 5f 73 74 6d 74 2a 29 70 50 61 72 73 65 2d 3e 70  _stmt*)pParse->p
2a0a7 56 64 62 65 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  Vdbe;.  }..  if(
2a0a8 20 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20   zErrMsg ){.    
2a0a9 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
2a0aa 20 72 63 2c 20 22 25 73 22 2c 20 7a 45 72 72 4d   rc, "%s", zErrM
2a0ab 73 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  sg);.    sqlite3
2a0ac 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 4d  DbFree(db, zErrM
2a0ad 73 67 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  sg);.  }else{.  
2a0ae 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
2a0af 62 2c 20 72 63 2c 20 30 29 3b 0a 20 20 7d 0a 0a  b, rc, 0);.  }..
2a0b0 20 20 2f 2a 20 44 65 6c 65 74 65 20 61 6e 79 20    /* Delete any 
2a0b1 54 72 69 67 67 65 72 50 72 67 20 73 74 72 75 63  TriggerPrg struc
2a0b2 74 75 72 65 73 20 61 6c 6c 6f 63 61 74 65 64 20  tures allocated 
2a0b3 77 68 69 6c 65 20 70 61 72 73 69 6e 67 20 74 68  while parsing th
2a0b4 69 73 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f  is statement. */
2a0b5 0a 20 20 77 68 69 6c 65 28 20 70 50 61 72 73 65  .  while( pParse
2a0b6 2d 3e 70 54 72 69 67 67 65 72 50 72 67 20 29 7b  ->pTriggerPrg ){
2a0b7 0a 20 20 20 20 54 72 69 67 67 65 72 50 72 67 20  .    TriggerPrg 
2a0b8 2a 70 54 20 3d 20 70 50 61 72 73 65 2d 3e 70 54  *pT = pParse->pT
2a0b9 72 69 67 67 65 72 50 72 67 3b 0a 20 20 20 20 70  riggerPrg;.    p
2a0ba 50 61 72 73 65 2d 3e 70 54 72 69 67 67 65 72 50  Parse->pTriggerP
2a0bb 72 67 20 3d 20 70 54 2d 3e 70 4e 65 78 74 3b 0a  rg = pT->pNext;.
2a0bc 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 50      sqlite3VdbeP
2a0bd 72 6f 67 72 61 6d 44 65 6c 65 74 65 28 64 62 2c  rogramDelete(db,
2a0be 20 70 54 2d 3e 70 50 72 6f 67 72 61 6d 2c 20 30   pT->pProgram, 0
2a0bf 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  );.    sqlite3Db
2a0c0 46 72 65 65 28 64 62 2c 20 70 54 29 3b 0a 20 20  Free(db, pT);.  
2a0c1 7d 0a 0a 65 6e 64 5f 70 72 65 70 61 72 65 3a 0a  }..end_prepare:.
2a0c2 0a 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46  .  sqlite3StackF
2a0c3 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 29 3b  ree(db, pParse);
2a0c4 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41  .  rc = sqlite3A
2a0c5 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a  piExit(db, rc);.
2a0c6 20 20 61 73 73 65 72 74 28 20 28 72 63 26 64 62    assert( (rc&db
2a0c7 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d 72 63 20 29  ->errMask)==rc )
2a0c8 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
2a0c9 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69  .static int sqli
2a0ca 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72  te3LockAndPrepar
2a0cb 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
2a0cc 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
2a0cd 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c  * Database handl
2a0ce 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  e. */.  const ch
2a0cf 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20  ar *zSql,       
2a0d0 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64    /* UTF-8 encod
2a0d1 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  ed SQL statement
2a0d2 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  . */.  int nByte
2a0d3 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
2a0d4 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53   /* Length of zS
2a0d5 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a  ql in bytes. */.
2a0d6 20 20 69 6e 74 20 73 61 76 65 53 71 6c 46 6c 61    int saveSqlFla
2a0d7 67 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  g,          /* T
2a0d8 72 75 65 20 74 6f 20 63 6f 70 79 20 53 51 4c 20  rue to copy SQL 
2a0d9 74 65 78 74 20 69 6e 74 6f 20 74 68 65 20 73 71  text into the sq
2a0da 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2f 0a 20 20  lite3_stmt */.  
2a0db 56 64 62 65 20 2a 70 4f 6c 64 2c 20 20 20 20 20  Vdbe *pOld,     
2a0dc 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 4d 20            /* VM 
2a0dd 62 65 69 6e 67 20 72 65 70 72 65 70 61 72 65 64  being reprepared
2a0de 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
2a0df 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20  mt **ppStmt,    
2a0e0 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65  /* OUT: A pointe
2a0e1 72 20 74 6f 20 74 68 65 20 70 72 65 70 61 72 65  r to the prepare
2a0e2 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  d statement */. 
2a0e3 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a   const char **pz
2a0e4 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55  Tail       /* OU
2a0e5 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64  T: End of parsed
2a0e6 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20   string */.){.  
2a0e7 69 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74  int rc;.  assert
2a0e8 28 20 70 70 53 74 6d 74 21 3d 30 20 29 3b 0a 20  ( ppStmt!=0 );. 
2a0e9 20 2a 70 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20   *ppStmt = 0;.  
2a0ea 69 66 28 20 21 73 71 6c 69 74 65 33 53 61 66 65  if( !sqlite3Safe
2a0eb 74 79 43 68 65 63 6b 4f 6b 28 64 62 29 20 29 7b  tyCheckOk(db) ){
2a0ec 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
2a0ed 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20  TE_MISUSE;.  }. 
2a0ee 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
2a0ef 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
2a0f0 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
2a0f1 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20 20 72  nterAll(db);.  r
2a0f2 63 20 3d 20 73 71 6c 69 74 65 33 50 72 65 70 61  c = sqlite3Prepa
2a0f3 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 6e 42 79  re(db, zSql, nBy
2a0f4 74 65 73 2c 20 73 61 76 65 53 71 6c 46 6c 61 67  tes, saveSqlFlag
2a0f5 2c 20 70 4f 6c 64 2c 20 70 70 53 74 6d 74 2c 20  , pOld, ppStmt, 
2a0f6 70 7a 54 61 69 6c 29 3b 0a 20 20 69 66 28 20 72  pzTail);.  if( r
2a0f7 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  c==SQLITE_SCHEMA
2a0f8 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
2a0f9 66 69 6e 61 6c 69 7a 65 28 2a 70 70 53 74 6d 74  finalize(*ppStmt
2a0fa 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
2a0fb 74 65 33 50 72 65 70 61 72 65 28 64 62 2c 20 7a  te3Prepare(db, z
2a0fc 53 71 6c 2c 20 6e 42 79 74 65 73 2c 20 73 61 76  Sql, nBytes, sav
2a0fd 65 53 71 6c 46 6c 61 67 2c 20 70 4f 6c 64 2c 20  eSqlFlag, pOld, 
2a0fe 70 70 53 74 6d 74 2c 20 70 7a 54 61 69 6c 29 3b  ppStmt, pzTail);
2a0ff 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74  .  }.  sqlite3Bt
2a100 72 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b  reeLeaveAll(db);
2a101 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2a102 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
2a103 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
2a104 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 72 75 6e 20 74  }../*.** Rerun t
2a105 68 65 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f  he compilation o
2a106 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20 61 66  f a statement af
2a107 74 65 72 20 61 20 73 63 68 65 6d 61 20 63 68 61  ter a schema cha
2a108 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  nge..**.** If th
2a109 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 73  e statement is s
2a10a 75 63 63 65 73 73 66 75 6c 6c 79 20 72 65 63 6f  uccessfully reco
2a10b 6d 70 69 6c 65 64 2c 20 72 65 74 75 72 6e 20 53  mpiled, return S
2a10c 51 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77  QLITE_OK. Otherw
2a10d 69 73 65 2c 0a 2a 2a 20 69 66 20 74 68 65 20 73  ise,.** if the s
2a10e 74 61 74 65 6d 65 6e 74 20 63 61 6e 6e 6f 74 20  tatement cannot 
2a10f 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 20 62 65  be recompiled be
2a110 63 61 75 73 65 20 61 6e 6f 74 68 65 72 20 63 6f  cause another co
2a111 6e 6e 65 63 74 69 6f 6e 20 68 61 73 0a 2a 2a 20  nnection has.** 
2a112 6c 6f 63 6b 65 64 20 74 68 65 20 73 71 6c 69 74  locked the sqlit
2a113 65 33 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2c  e3_master table,
2a114 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c   return SQLITE_L
2a115 4f 43 4b 45 44 2e 20 49 66 20 61 6e 79 20 6f 74  OCKED. If any ot
2a116 68 65 72 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63  her error.** occ
2a117 75 72 73 2c 20 72 65 74 75 72 6e 20 53 51 4c 49  urs, return SQLI
2a118 54 45 5f 53 43 48 45 4d 41 2e 0a 2a 2f 0a 53 51  TE_SCHEMA..*/.SQ
2a119 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
2a11a 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72   sqlite3Reprepar
2a11b 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e  e(Vdbe *p){.  in
2a11c 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f  t rc;.  sqlite3_
2a11d 73 74 6d 74 20 2a 70 4e 65 77 3b 0a 20 20 63 6f  stmt *pNew;.  co
2a11e 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a  nst char *zSql;.
2a11f 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a    sqlite3 *db;..
2a120 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
2a121 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c  3_mutex_held(sql
2a122 69 74 65 33 56 64 62 65 44 62 28 70 29 2d 3e 6d  ite3VdbeDb(p)->m
2a123 75 74 65 78 29 20 29 3b 0a 20 20 7a 53 71 6c 20  utex) );.  zSql 
2a124 3d 20 73 71 6c 69 74 65 33 5f 73 71 6c 28 28 73  = sqlite3_sql((s
2a125 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 70 29  qlite3_stmt *)p)
2a126 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 53 71 6c  ;.  assert( zSql
2a127 21 3d 30 20 29 3b 20 20 2f 2a 20 52 65 70 72 65  !=0 );  /* Repre
2a128 70 61 72 65 20 6f 6e 6c 79 20 63 61 6c 6c 65 64  pare only called
2a129 20 66 6f 72 20 70 72 65 70 61 72 65 5f 76 32 28   for prepare_v2(
2a12a 29 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a  ) statements */.
2a12b 20 20 64 62 20 3d 20 73 71 6c 69 74 65 33 56 64    db = sqlite3Vd
2a12c 62 65 44 62 28 70 29 3b 0a 20 20 61 73 73 65 72  beDb(p);.  asser
2a12d 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
2a12e 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
2a12f 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   );.  rc = sqlit
2a130 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65  e3LockAndPrepare
2a131 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 30  (db, zSql, -1, 0
2a132 2c 20 70 2c 20 26 70 4e 65 77 2c 20 30 29 3b 0a  , p, &pNew, 0);.
2a133 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
2a134 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  if( rc==SQLITE_N
2a135 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 64 62  OMEM ){.      db
2a136 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
2a137 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73   1;.    }.    as
2a138 73 65 72 74 28 20 70 4e 65 77 3d 3d 30 20 29 3b  sert( pNew==0 );
2a139 0a 20 20 20 20 72 65 74 75 72 6e 20 28 72 63 3d  .    return (rc=
2a13a 3d 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 29 20  =SQLITE_LOCKED) 
2a13b 3f 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20  ? SQLITE_LOCKED 
2a13c 3a 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b  : SQLITE_SCHEMA;
2a13d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
2a13e 73 65 72 74 28 20 70 4e 65 77 21 3d 30 20 29 3b  sert( pNew!=0 );
2a13f 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64  .  }.  sqlite3Vd
2a140 62 65 53 77 61 70 28 28 56 64 62 65 2a 29 70 4e  beSwap((Vdbe*)pN
2a141 65 77 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65  ew, p);.  sqlite
2a142 33 54 72 61 6e 73 66 65 72 42 69 6e 64 69 6e 67  3TransferBinding
2a143 73 28 70 4e 65 77 2c 20 28 73 71 6c 69 74 65 33  s(pNew, (sqlite3
2a144 5f 73 74 6d 74 2a 29 70 29 3b 0a 20 20 73 71 6c  _stmt*)p);.  sql
2a145 69 74 65 33 56 64 62 65 52 65 73 65 74 53 74 65  ite3VdbeResetSte
2a146 70 52 65 73 75 6c 74 28 28 56 64 62 65 2a 29 70  pResult((Vdbe*)p
2a147 4e 65 77 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  New);.  sqlite3V
2a148 64 62 65 46 69 6e 61 6c 69 7a 65 28 28 56 64 62  dbeFinalize((Vdb
2a149 65 2a 29 70 4e 65 77 29 3b 0a 20 20 72 65 74 75  e*)pNew);.  retu
2a14a 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
2a14b 0a 0a 2f 2a 0a 2a 2a 20 54 77 6f 20 76 65 72 73  ../*.** Two vers
2a14c 69 6f 6e 73 20 6f 66 20 74 68 65 20 6f 66 66 69  ions of the offi
2a14d 63 69 61 6c 20 41 50 49 2e 20 20 4c 65 67 61 63  cial API.  Legac
2a14e 79 20 61 6e 64 20 6e 65 77 20 75 73 65 2e 20 20  y and new use.  
2a14f 49 6e 20 74 68 65 20 6c 65 67 61 63 79 0a 2a 2a  In the legacy.**
2a150 20 76 65 72 73 69 6f 6e 2c 20 74 68 65 20 6f 72   version, the or
2a151 69 67 69 6e 61 6c 20 53 51 4c 20 74 65 78 74 20  iginal SQL text 
2a152 69 73 20 6e 6f 74 20 73 61 76 65 64 20 69 6e 20  is not saved in 
2a153 74 68 65 20 70 72 65 70 61 72 65 64 20 73 74 61  the prepared sta
2a154 74 65 6d 65 6e 74 0a 2a 2a 20 61 6e 64 20 73 6f  tement.** and so
2a155 20 69 66 20 61 20 73 63 68 65 6d 61 20 63 68 61   if a schema cha
2a156 6e 67 65 20 6f 63 63 75 72 73 2c 20 53 51 4c 49  nge occurs, SQLI
2a157 54 45 5f 53 43 48 45 4d 41 20 69 73 20 72 65 74  TE_SCHEMA is ret
2a158 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69  urned by.** sqli
2a159 74 65 33 5f 73 74 65 70 28 29 2e 20 20 49 6e 20  te3_step().  In 
2a15a 74 68 65 20 6e 65 77 20 76 65 72 73 69 6f 6e 2c  the new version,
2a15b 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51   the original SQ
2a15c 4c 20 74 65 78 74 20 69 73 20 72 65 74 61 69 6e  L text is retain
2a15d 65 64 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73 74  ed.** and the st
2a15e 61 74 65 6d 65 6e 74 20 69 73 20 61 75 74 6f 6d  atement is autom
2a15f 61 74 69 63 61 6c 6c 79 20 72 65 63 6f 6d 70 69  atically recompi
2a160 6c 65 64 20 69 66 20 61 6e 20 73 63 68 65 6d 61  led if an schema
2a161 20 63 68 61 6e 67 65 0a 2a 2a 20 6f 63 63 75 72   change.** occur
2a162 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
2a163 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 72 65   int sqlite3_pre
2a164 70 61 72 65 28 0a 20 20 73 71 6c 69 74 65 33 20  pare(.  sqlite3 
2a165 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
2a166 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
2a167 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74  ndle. */.  const
2a168 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20   char *zSql,    
2a169 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e       /* UTF-8 en
2a16a 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d  coded SQL statem
2a16b 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  ent. */.  int nB
2a16c 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20  ytes,           
2a16d 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
2a16e 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20   zSql in bytes. 
2a16f 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
2a170 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f  t **ppStmt,    /
2a171 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72  * OUT: A pointer
2a172 20 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64   to the prepared
2a173 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
2a174 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54  const char **pzT
2a175 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54  ail       /* OUT
2a176 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20  : End of parsed 
2a177 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69  string */.){.  i
2a178 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71  nt rc;.  rc = sq
2a179 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70  lite3LockAndPrep
2a17a 61 72 65 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74  are(db,zSql,nByt
2a17b 65 73 2c 30 2c 30 2c 70 70 53 74 6d 74 2c 70 7a  es,0,0,ppStmt,pz
2a17c 54 61 69 6c 29 3b 0a 20 20 61 73 73 65 72 74 28  Tail);.  assert(
2a17d 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc==SQLITE_OK |
2a17e 7c 20 70 70 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a  | ppStmt==0 || *
2a17f 70 70 53 74 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a  ppStmt==0 );  /*
2a180 20 56 45 52 49 46 59 3a 20 46 31 33 30 32 31 20   VERIFY: F13021 
2a181 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  */.  return rc;.
2a182 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
2a183 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
2a184 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  _v2(.  sqlite3 *
2a185 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  db,             
2a186 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61 6e   /* Database han
2a187 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  dle. */.  const 
2a188 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20  char *zSql,     
2a189 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63      /* UTF-8 enc
2a18a 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  oded SQL stateme
2a18b 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79  nt. */.  int nBy
2a18c 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  tes,            
2a18d 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20     /* Length of 
2a18e 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a  zSql in bytes. *
2a18f 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
2a190 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a   **ppStmt,    /*
2a191 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20   OUT: A pointer 
2a192 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20  to the prepared 
2a193 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63  statement */.  c
2a194 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61  onst char **pzTa
2a195 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  il       /* OUT:
2a196 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73   End of parsed s
2a197 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e  tring */.){.  in
2a198 74 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c  t rc;.  rc = sql
2a199 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61  ite3LockAndPrepa
2a19a 72 65 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65  re(db,zSql,nByte
2a19b 73 2c 31 2c 30 2c 70 70 53 74 6d 74 2c 70 7a 54  s,1,0,ppStmt,pzT
2a19c 61 69 6c 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ail);.  assert( 
2a19d 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
2a19e 20 70 70 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70   ppStmt==0 || *p
2a19f 70 53 74 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a 20  pStmt==0 );  /* 
2a1a0 56 45 52 49 46 59 3a 20 46 31 33 30 32 31 20 2a  VERIFY: F13021 *
2a1a1 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  /.  return rc;.}
2a1a2 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ...#ifndef SQLIT
2a1a3 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a  E_OMIT_UTF16./*.
2a1a4 2a 2a 20 43 6f 6d 70 69 6c 65 20 74 68 65 20 55  ** Compile the U
2a1a5 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 53 51  TF-16 encoded SQ
2a1a6 4c 20 73 74 61 74 65 6d 65 6e 74 20 7a 53 71 6c  L statement zSql
2a1a7 20 69 6e 74 6f 20 61 20 73 74 61 74 65 6d 65 6e   into a statemen
2a1a8 74 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61  t handle..*/.sta
2a1a9 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 50  tic int sqlite3P
2a1aa 72 65 70 61 72 65 31 36 28 0a 20 20 73 71 6c 69  repare16(.  sqli
2a1ab 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
2a1ac 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
2a1ad 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 20 0a 20 20  e handle. */ .  
2a1ae 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c  const void *zSql
2a1af 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46  ,         /* UTF
2a1b0 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73  -8 encoded SQL s
2a1b1 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69  tatement. */.  i
2a1b2 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20  nt nBytes,      
2a1b3 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67           /* Leng
2a1b4 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79  th of zSql in by
2a1b5 74 65 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 61  tes. */.  int sa
2a1b6 76 65 53 71 6c 46 6c 61 67 2c 20 20 20 20 20 20  veSqlFlag,      
2a1b7 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 73      /* True to s
2a1b8 61 76 65 20 53 51 4c 20 74 65 78 74 20 69 6e 74  ave SQL text int
2a1b9 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74  o the sqlite3_st
2a1ba 6d 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  mt */.  sqlite3_
2a1bb 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20  stmt **ppStmt,  
2a1bc 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e    /* OUT: A poin
2a1bd 74 65 72 20 74 6f 20 74 68 65 20 70 72 65 70 61  ter to the prepa
2a1be 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  red statement */
2a1bf 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a  .  const void **
2a1c0 70 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20  pzTail       /* 
2a1c1 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73  OUT: End of pars
2a1c2 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a  ed string */.){.
2a1c3 20 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69    /* This functi
2a1c4 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 77 6f 72  on currently wor
2a1c5 6b 73 20 62 79 20 66 69 72 73 74 20 74 72 61 6e  ks by first tran
2a1c6 73 66 6f 72 6d 69 6e 67 20 74 68 65 20 55 54 46  sforming the UTF
2a1c7 2d 31 36 0a 20 20 2a 2a 20 65 6e 63 6f 64 65 64  -16.  ** encoded
2a1c8 20 73 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38   string to UTF-8
2a1c9 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 69 6e 67 20  , then invoking 
2a1ca 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28  sqlite3_prepare(
2a1cb 29 2e 20 54 68 65 0a 20 20 2a 2a 20 74 72 69 63  ). The.  ** tric
2a1cc 6b 79 20 62 69 74 20 69 73 20 66 69 67 75 72 69  ky bit is figuri
2a1cd 6e 67 20 6f 75 74 20 74 68 65 20 70 6f 69 6e 74  ng out the point
2a1ce 65 72 20 74 6f 20 72 65 74 75 72 6e 20 69 6e 20  er to return in 
2a1cf 2a 70 7a 54 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20  *pzTail..  */.  
2a1d0 63 68 61 72 20 2a 7a 53 71 6c 38 3b 0a 20 20 63  char *zSql8;.  c
2a1d1 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 69 6c  onst char *zTail
2a1d2 38 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20  8 = 0;.  int rc 
2a1d3 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
2a1d4 61 73 73 65 72 74 28 20 70 70 53 74 6d 74 20 29  assert( ppStmt )
2a1d5 3b 0a 20 20 2a 70 70 53 74 6d 74 20 3d 20 30 3b  ;.  *ppStmt = 0;
2a1d6 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 53  .  if( !sqlite3S
2a1d7 61 66 65 74 79 43 68 65 63 6b 4f 6b 28 64 62 29  afetyCheckOk(db)
2a1d8 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
2a1d9 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
2a1da 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
2a1db 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
2a1dc 78 29 3b 0a 20 20 7a 53 71 6c 38 20 3d 20 73 71  x);.  zSql8 = sq
2a1dd 6c 69 74 65 33 55 74 66 31 36 74 6f 38 28 64 62  lite3Utf16to8(db
2a1de 2c 20 7a 53 71 6c 2c 20 6e 42 79 74 65 73 29 3b  , zSql, nBytes);
2a1df 0a 20 20 69 66 28 20 7a 53 71 6c 38 20 29 7b 0a  .  if( zSql8 ){.
2a1e0 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2a1e1 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65 28 64  LockAndPrepare(d
2a1e2 62 2c 20 7a 53 71 6c 38 2c 20 2d 31 2c 20 73 61  b, zSql8, -1, sa
2a1e3 76 65 53 71 6c 46 6c 61 67 2c 20 30 2c 20 70 70  veSqlFlag, 0, pp
2a1e4 53 74 6d 74 2c 20 26 7a 54 61 69 6c 38 29 3b 0a  Stmt, &zTail8);.
2a1e5 20 20 7d 0a 0a 20 20 69 66 28 20 7a 54 61 69 6c    }..  if( zTail
2a1e6 38 20 26 26 20 70 7a 54 61 69 6c 20 29 7b 0a 20  8 && pzTail ){. 
2a1e7 20 20 20 2f 2a 20 49 66 20 73 71 6c 69 74 65 33     /* If sqlite3
2a1e8 5f 70 72 65 70 61 72 65 20 72 65 74 75 72 6e 73  _prepare returns
2a1e9 20 61 20 74 61 69 6c 20 70 6f 69 6e 74 65 72 2c   a tail pointer,
2a1ea 20 77 65 20 63 61 6c 63 75 6c 61 74 65 20 74 68   we calculate th
2a1eb 65 0a 20 20 20 20 2a 2a 20 65 71 75 69 76 61 6c  e.    ** equival
2a1ec 65 6e 74 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f  ent pointer into
2a1ed 20 74 68 65 20 55 54 46 2d 31 36 20 73 74 72 69   the UTF-16 stri
2a1ee 6e 67 20 62 79 20 63 6f 75 6e 74 69 6e 67 20 74  ng by counting t
2a1ef 68 65 20 75 6e 69 63 6f 64 65 0a 20 20 20 20 2a  he unicode.    *
2a1f0 2a 20 63 68 61 72 61 63 74 65 72 73 20 62 65 74  * characters bet
2a1f1 77 65 65 6e 20 7a 53 71 6c 38 20 61 6e 64 20 7a  ween zSql8 and z
2a1f2 54 61 69 6c 38 2c 20 61 6e 64 20 74 68 65 6e 20  Tail8, and then 
2a1f3 72 65 74 75 72 6e 69 6e 67 20 61 20 70 6f 69 6e  returning a poin
2a1f4 74 65 72 0a 20 20 20 20 2a 2a 20 74 68 65 20 73  ter.    ** the s
2a1f5 61 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  ame number of ch
2a1f6 61 72 61 63 74 65 72 73 20 69 6e 74 6f 20 74 68  aracters into th
2a1f7 65 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 2e  e UTF-16 string.
2a1f8 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20  .    */.    int 
2a1f9 63 68 61 72 73 5f 70 61 72 73 65 64 20 3d 20 73  chars_parsed = s
2a1fa 71 6c 69 74 65 33 55 74 66 38 43 68 61 72 4c 65  qlite3Utf8CharLe
2a1fb 6e 28 7a 53 71 6c 38 2c 20 28 69 6e 74 29 28 7a  n(zSql8, (int)(z
2a1fc 54 61 69 6c 38 2d 7a 53 71 6c 38 29 29 3b 0a 20  Tail8-zSql8));. 
2a1fd 20 20 20 2a 70 7a 54 61 69 6c 20 3d 20 28 75 38     *pzTail = (u8
2a1fe 20 2a 29 7a 53 71 6c 20 2b 20 73 71 6c 69 74 65   *)zSql + sqlite
2a1ff 33 55 74 66 31 36 42 79 74 65 4c 65 6e 28 7a 53  3Utf16ByteLen(zS
2a200 71 6c 2c 20 63 68 61 72 73 5f 70 61 72 73 65 64  ql, chars_parsed
2a201 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
2a202 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71 6c 38  DbFree(db, zSql8
2a203 29 3b 20 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  ); .  rc = sqlit
2a204 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63  e3ApiExit(db, rc
2a205 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
2a206 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
2a207 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
2a208 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 77 6f 20 76  ;.}../*.** Two v
2a209 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 6f  ersions of the o
2a20a 66 66 69 63 69 61 6c 20 41 50 49 2e 20 20 4c 65  fficial API.  Le
2a20b 67 61 63 79 20 61 6e 64 20 6e 65 77 20 75 73 65  gacy and new use
2a20c 2e 20 20 49 6e 20 74 68 65 20 6c 65 67 61 63 79  .  In the legacy
2a20d 0a 2a 2a 20 76 65 72 73 69 6f 6e 2c 20 74 68 65  .** version, the
2a20e 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65   original SQL te
2a20f 78 74 20 69 73 20 6e 6f 74 20 73 61 76 65 64 20  xt is not saved 
2a210 69 6e 20 74 68 65 20 70 72 65 70 61 72 65 64 20  in the prepared 
2a211 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61 6e 64  statement.** and
2a212 20 73 6f 20 69 66 20 61 20 73 63 68 65 6d 61 20   so if a schema 
2a213 63 68 61 6e 67 65 20 6f 63 63 75 72 73 2c 20 53  change occurs, S
2a214 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 69 73 20  QLITE_SCHEMA is 
2a215 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73  returned by.** s
2a216 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 20  qlite3_step().  
2a217 49 6e 20 74 68 65 20 6e 65 77 20 76 65 72 73 69  In the new versi
2a218 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  on, the original
2a219 20 53 51 4c 20 74 65 78 74 20 69 73 20 72 65 74   SQL text is ret
2a21a 61 69 6e 65 64 0a 2a 2a 20 61 6e 64 20 74 68 65  ained.** and the
2a21b 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 75   statement is au
2a21c 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 65 63 6f  tomatically reco
2a21d 6d 70 69 6c 65 64 20 69 66 20 61 6e 20 73 63 68  mpiled if an sch
2a21e 65 6d 61 20 63 68 61 6e 67 65 0a 2a 2a 20 6f 63  ema change.** oc
2a21f 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  curs..*/.SQLITE_
2a220 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2a221 70 72 65 70 61 72 65 31 36 28 0a 20 20 73 71 6c  prepare16(.  sql
2a222 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
2a223 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2a224 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 20 0a 20  se handle. */ . 
2a225 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71   const void *zSq
2a226 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54  l,         /* UT
2a227 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20  F-8 encoded SQL 
2a228 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20  statement. */.  
2a229 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20  int nBytes,     
2a22a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e            /* Len
2a22b 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62  gth of zSql in b
2a22c 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  ytes. */.  sqlit
2a22d 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74  e3_stmt **ppStmt
2a22e 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70  ,    /* OUT: A p
2a22f 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 72  ointer to the pr
2a230 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
2a231 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
2a232 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20 20 20   **pzTail       
2a233 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70  /* OUT: End of p
2a234 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a  arsed string */.
2a235 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 72  ){.  int rc;.  r
2a236 63 20 3d 20 73 71 6c 69 74 65 33 50 72 65 70 61  c = sqlite3Prepa
2a237 72 65 31 36 28 64 62 2c 7a 53 71 6c 2c 6e 42 79  re16(db,zSql,nBy
2a238 74 65 73 2c 30 2c 70 70 53 74 6d 74 2c 70 7a 54  tes,0,ppStmt,pzT
2a239 61 69 6c 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ail);.  assert( 
2a23a 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
2a23b 20 70 70 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70   ppStmt==0 || *p
2a23c 70 53 74 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a 20  pStmt==0 );  /* 
2a23d 56 45 52 49 46 59 3a 20 46 31 33 30 32 31 20 2a  VERIFY: F13021 *
2a23e 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  /.  return rc;.}
2a23f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2a240 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
2a241 36 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20  6_v2(.  sqlite3 
2a242 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
2a243 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
2a244 6e 64 6c 65 2e 20 2a 2f 20 0a 20 20 63 6f 6e 73  ndle. */ .  cons
2a245 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20  t void *zSql,   
2a246 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65        /* UTF-8 e
2a247 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65  ncoded SQL state
2a248 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e  ment. */.  int n
2a249 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20  Bytes,          
2a24a 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
2a24b 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e  f zSql in bytes.
2a24c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
2a24d 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20  mt **ppStmt,    
2a24e 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65  /* OUT: A pointe
2a24f 72 20 74 6f 20 74 68 65 20 70 72 65 70 61 72 65  r to the prepare
2a250 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  d statement */. 
2a251 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a   const void **pz
2a252 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55  Tail       /* OU
2a253 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64  T: End of parsed
2a254 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20   string */.){.  
2a255 69 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d 20 73  int rc;.  rc = s
2a256 71 6c 69 74 65 33 50 72 65 70 61 72 65 31 36 28  qlite3Prepare16(
2a257 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73 2c 31  db,zSql,nBytes,1
2a258 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c 29 3b  ,ppStmt,pzTail);
2a259 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
2a25a 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 70 53 74  QLITE_OK || ppSt
2a25b 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53 74 6d 74  mt==0 || *ppStmt
2a25c 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45 52 49 46  ==0 );  /* VERIF
2a25d 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a 20 20 72  Y: F13021 */.  r
2a25e 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e  eturn rc;.}..#en
2a25f 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2a260 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 2a  IT_UTF16 */../**
2a261 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
2a262 20 6f 66 20 70 72 65 70 61 72 65 2e 63 20 2a 2a   of prepare.c **
2a263 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a264 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a265 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
2a266 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
2a267 69 6e 20 66 69 6c 65 20 73 65 6c 65 63 74 2e 63  in file select.c
2a268 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2a269 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a26a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
2a26b 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
2a26c 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
2a26d 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
2a26e 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
2a26f 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
2a270 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
2a271 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
2a272 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
2a273 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
2a274 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
2a275 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
2a276 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
2a277 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
2a278 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
2a279 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
2a27a 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
2a27b 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
2a27c 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
2a27d 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
2a27e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a27f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a280 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a281 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2a282 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
2a283 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f 64 65   contains C code
2a284 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 61   routines that a
2a285 72 65 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65  re called by the
2a286 20 70 61 72 73 65 72 0a 2a 2a 20 74 6f 20 68 61   parser.** to ha
2a287 6e 64 6c 65 20 53 45 4c 45 43 54 20 73 74 61 74  ndle SELECT stat
2a288 65 6d 65 6e 74 73 20 69 6e 20 53 51 4c 69 74 65  ements in SQLite
2a289 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 73 65 6c  ..**.** $Id: sel
2a28a 65 63 74 2e 63 2c 76 20 31 2e 35 32 36 20 32 30  ect.c,v 1.526 20
2a28b 30 39 2f 30 38 2f 30 31 20 31 35 3a 30 39 3a 35  09/08/01 15:09:5
2a28c 38 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  8 drh Exp $.*/..
2a28d 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c  ./*.** Delete al
2a28e 6c 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  l the content of
2a28f 20 61 20 53 65 6c 65 63 74 20 73 74 72 75 63 74   a Select struct
2a290 75 72 65 20 62 75 74 20 64 6f 20 6e 6f 74 20 64  ure but do not d
2a291 65 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 74 68 65  eallocate.** the
2a292 20 73 65 6c 65 63 74 20 73 74 72 75 63 74 75 72   select structur
2a293 65 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a 73 74 61  e itself..*/.sta
2a294 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 53 65  tic void clearSe
2a295 6c 65 63 74 28 73 71 6c 69 74 65 33 20 2a 64 62  lect(sqlite3 *db
2a296 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20  , Select *p){.  
2a297 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2a298 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 45 4c  elete(db, p->pEL
2a299 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  ist);.  sqlite3S
2a29a 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  rcListDelete(db,
2a29b 20 70 2d 3e 70 53 72 63 29 3b 0a 20 20 73 71 6c   p->pSrc);.  sql
2a29c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2a29d 62 2c 20 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20  b, p->pWhere);. 
2a29e 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2a29f 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 47  Delete(db, p->pG
2a2a0 72 6f 75 70 42 79 29 3b 0a 20 20 73 71 6c 69 74  roupBy);.  sqlit
2a2a1 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
2a2a2 20 70 2d 3e 70 48 61 76 69 6e 67 29 3b 0a 20 20   p->pHaving);.  
2a2a3 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2a2a4 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f 72  elete(db, p->pOr
2a2a5 64 65 72 42 79 29 3b 0a 20 20 73 71 6c 69 74 65  derBy);.  sqlite
2a2a6 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
2a2a7 2c 20 70 2d 3e 70 50 72 69 6f 72 29 3b 0a 20 20  , p->pPrior);.  
2a2a8 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
2a2a9 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29  e(db, p->pLimit)
2a2aa 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44  ;.  sqlite3ExprD
2a2ab 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f 66  elete(db, p->pOf
2a2ac 66 73 65 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  fset);.}../*.** 
2a2ad 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 53 65 6c  Initialize a Sel
2a2ae 65 63 74 44 65 73 74 20 73 74 72 75 63 74 75 72  ectDest structur
2a2af 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
2a2b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2a2b1 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
2a2b2 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73  SelectDest *pDes
2a2b3 74 2c 20 69 6e 74 20 65 44 65 73 74 2c 20 69 6e  t, int eDest, in
2a2b4 74 20 69 50 61 72 6d 29 7b 0a 20 20 70 44 65 73  t iParm){.  pDes
2a2b5 74 2d 3e 65 44 65 73 74 20 3d 20 28 75 38 29 65  t->eDest = (u8)e
2a2b6 44 65 73 74 3b 0a 20 20 70 44 65 73 74 2d 3e 69  Dest;.  pDest->i
2a2b7 50 61 72 6d 20 3d 20 69 50 61 72 6d 3b 0a 20 20  Parm = iParm;.  
2a2b8 70 44 65 73 74 2d 3e 61 66 66 69 6e 69 74 79 20  pDest->affinity 
2a2b9 3d 20 30 3b 0a 20 20 70 44 65 73 74 2d 3e 69 4d  = 0;.  pDest->iM
2a2ba 65 6d 20 3d 20 30 3b 0a 20 20 70 44 65 73 74 2d  em = 0;.  pDest-
2a2bb 3e 6e 4d 65 6d 20 3d 20 30 3b 0a 7d 0a 0a 0a 2f  >nMem = 0;.}.../
2a2bc 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20  *.** Allocate a 
2a2bd 6e 65 77 20 53 65 6c 65 63 74 20 73 74 72 75 63  new Select struc
2a2be 74 75 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20  ture and return 
2a2bf 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61  a pointer to tha
2a2c0 74 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 0a  t.** structure..
2a2c1 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2a2c2 45 20 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65  E Select *sqlite
2a2c3 33 53 65 6c 65 63 74 4e 65 77 28 0a 20 20 50 61  3SelectNew(.  Pa
2a2c4 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2a2c5 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2a2c6 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72  ontext */.  Expr
2a2c7 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 20  List *pEList,   
2a2c8 20 20 2f 2a 20 77 68 69 63 68 20 63 6f 6c 75 6d    /* which colum
2a2c9 6e 73 20 74 6f 20 69 6e 63 6c 75 64 65 20 69 6e  ns to include in
2a2ca 20 74 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20   the result */. 
2a2cb 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20   SrcList *pSrc, 
2a2cc 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 46 52         /* the FR
2a2cd 4f 4d 20 63 6c 61 75 73 65 20 2d 2d 20 77 68 69  OM clause -- whi
2a2ce 63 68 20 74 61 62 6c 65 73 20 74 6f 20 73 63 61  ch tables to sca
2a2cf 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  n */.  Expr *pWh
2a2d0 65 72 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ere,         /* 
2a2d1 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2a2d2 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
2a2d3 70 47 72 6f 75 70 42 79 2c 20 20 20 2f 2a 20 74  pGroupBy,   /* t
2a2d4 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  he GROUP BY clau
2a2d5 73 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 48  se */.  Expr *pH
2a2d6 61 76 69 6e 67 2c 20 20 20 20 20 20 20 20 2f 2a  aving,        /*
2a2d7 20 74 68 65 20 48 41 56 49 4e 47 20 63 6c 61 75   the HAVING clau
2a2d8 73 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  se */.  ExprList
2a2d9 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 2f 2a   *pOrderBy,   /*
2a2da 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
2a2db 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73  ause */.  int is
2a2dc 44 69 73 74 69 6e 63 74 2c 20 20 20 20 20 20 20  Distinct,       
2a2dd 2f 2a 20 74 72 75 65 20 69 66 20 74 68 65 20 44  /* true if the D
2a2de 49 53 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20  ISTINCT keyword 
2a2df 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20  is present */.  
2a2e0 45 78 70 72 20 2a 70 4c 69 6d 69 74 2c 20 20 20  Expr *pLimit,   
2a2e1 20 20 20 20 20 20 2f 2a 20 4c 49 4d 49 54 20 76        /* LIMIT v
2a2e2 61 6c 75 65 2e 20 20 4e 55 4c 4c 20 6d 65 61 6e  alue.  NULL mean
2a2e3 73 20 6e 6f 74 20 75 73 65 64 20 2a 2f 0a 20 20  s not used */.  
2a2e4 45 78 70 72 20 2a 70 4f 66 66 73 65 74 20 20 20  Expr *pOffset   
2a2e5 20 20 20 20 20 20 2f 2a 20 4f 46 46 53 45 54 20        /* OFFSET 
2a2e6 76 61 6c 75 65 2e 20 20 4e 55 4c 4c 20 6d 65 61  value.  NULL mea
2a2e7 6e 73 20 6e 6f 20 6f 66 66 73 65 74 20 2a 2f 0a  ns no offset */.
2a2e8 29 7b 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65  ){.  Select *pNe
2a2e9 77 3b 0a 20 20 53 65 6c 65 63 74 20 73 74 61 6e  w;.  Select stan
2a2ea 64 69 6e 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  din;.  sqlite3 *
2a2eb 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2a2ec 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
2a2ed 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
2a2ee 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20  , sizeof(*pNew) 
2a2ef 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  );.  assert( db-
2a2f0 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c  >mallocFailed ||
2a2f1 20 21 70 4f 66 66 73 65 74 20 7c 7c 20 70 4c 69   !pOffset || pLi
2a2f2 6d 69 74 20 29 3b 20 2f 2a 20 4f 46 46 53 45 54  mit ); /* OFFSET
2a2f3 20 69 6d 70 6c 69 65 73 20 4c 49 4d 49 54 20 2a   implies LIMIT *
2a2f4 2f 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20  /.  if( pNew==0 
2a2f5 29 7b 0a 20 20 20 20 70 4e 65 77 20 3d 20 26 73  ){.    pNew = &s
2a2f6 74 61 6e 64 69 6e 3b 0a 20 20 20 20 6d 65 6d 73  tandin;.    mems
2a2f7 65 74 28 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65  et(pNew, 0, size
2a2f8 6f 66 28 2a 70 4e 65 77 29 29 3b 0a 20 20 7d 0a  of(*pNew));.  }.
2a2f9 20 20 69 66 28 20 70 45 4c 69 73 74 3d 3d 30 20    if( pEList==0 
2a2fa 29 7b 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20  ){.    pEList = 
2a2fb 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
2a2fc 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c  ppend(pParse, 0,
2a2fd 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c   sqlite3Expr(db,
2a2fe 54 4b 5f 41 4c 4c 2c 30 29 29 3b 0a 20 20 7d 0a  TK_ALL,0));.  }.
2a2ff 20 20 70 4e 65 77 2d 3e 70 45 4c 69 73 74 20 3d    pNew->pEList =
2a300 20 70 45 4c 69 73 74 3b 0a 20 20 70 4e 65 77 2d   pEList;.  pNew-
2a301 3e 70 53 72 63 20 3d 20 70 53 72 63 3b 0a 20 20  >pSrc = pSrc;.  
2a302 70 4e 65 77 2d 3e 70 57 68 65 72 65 20 3d 20 70  pNew->pWhere = p
2a303 57 68 65 72 65 3b 0a 20 20 70 4e 65 77 2d 3e 70  Where;.  pNew->p
2a304 47 72 6f 75 70 42 79 20 3d 20 70 47 72 6f 75 70  GroupBy = pGroup
2a305 42 79 3b 0a 20 20 70 4e 65 77 2d 3e 70 48 61 76  By;.  pNew->pHav
2a306 69 6e 67 20 3d 20 70 48 61 76 69 6e 67 3b 0a 20  ing = pHaving;. 
2a307 20 70 4e 65 77 2d 3e 70 4f 72 64 65 72 42 79 20   pNew->pOrderBy 
2a308 3d 20 70 4f 72 64 65 72 42 79 3b 0a 20 20 70 4e  = pOrderBy;.  pN
2a309 65 77 2d 3e 73 65 6c 46 6c 61 67 73 20 3d 20 69  ew->selFlags = i
2a30a 73 44 69 73 74 69 6e 63 74 20 3f 20 53 46 5f 44  sDistinct ? SF_D
2a30b 69 73 74 69 6e 63 74 20 3a 20 30 3b 0a 20 20 70  istinct : 0;.  p
2a30c 4e 65 77 2d 3e 6f 70 20 3d 20 54 4b 5f 53 45 4c  New->op = TK_SEL
2a30d 45 43 54 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c 69  ECT;.  pNew->pLi
2a30e 6d 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20  mit = pLimit;.  
2a30f 70 4e 65 77 2d 3e 70 4f 66 66 73 65 74 20 3d 20  pNew->pOffset = 
2a310 70 4f 66 66 73 65 74 3b 0a 20 20 61 73 73 65 72  pOffset;.  asser
2a311 74 28 20 70 4f 66 66 73 65 74 3d 3d 30 20 7c 7c  t( pOffset==0 ||
2a312 20 70 4c 69 6d 69 74 21 3d 30 20 29 3b 0a 20 20   pLimit!=0 );.  
2a313 70 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70  pNew->addrOpenEp
2a314 68 6d 5b 30 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e  hm[0] = -1;.  pN
2a315 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d  ew->addrOpenEphm
2a316 5b 31 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77  [1] = -1;.  pNew
2a317 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32  ->addrOpenEphm[2
2a318 5d 20 3d 20 2d 31 3b 0a 20 20 69 66 28 20 64 62  ] = -1;.  if( db
2a319 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2a31a 20 7b 0a 20 20 20 20 63 6c 65 61 72 53 65 6c 65   {.    clearSele
2a31b 63 74 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20  ct(db, pNew);.  
2a31c 20 20 69 66 28 20 70 4e 65 77 21 3d 26 73 74 61    if( pNew!=&sta
2a31d 6e 64 69 6e 20 29 20 73 71 6c 69 74 65 33 44 62  ndin ) sqlite3Db
2a31e 46 72 65 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a  Free(db, pNew);.
2a31f 20 20 20 20 70 4e 65 77 20 3d 20 30 3b 0a 20 20      pNew = 0;.  
2a320 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b  }.  return pNew;
2a321 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
2a322 20 74 68 65 20 67 69 76 65 6e 20 53 65 6c 65 63   the given Selec
2a323 74 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20  t structure and 
2a324 61 6c 6c 20 6f 66 20 69 74 73 20 73 75 62 73 74  all of its subst
2a325 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 53 51 4c  ructures..*/.SQL
2a326 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2a327 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
2a328 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62  lete(sqlite3 *db
2a329 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20  , Select *p){.  
2a32a 69 66 28 20 70 20 29 7b 0a 20 20 20 20 63 6c 65  if( p ){.    cle
2a32b 61 72 53 65 6c 65 63 74 28 64 62 2c 20 70 29 3b  arSelect(db, p);
2a32c 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2a32d 65 65 28 64 62 2c 20 70 29 3b 0a 20 20 7d 0a 7d  ee(db, p);.  }.}
2a32e 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 31 20  ../*.** Given 1 
2a32f 74 6f 20 33 20 69 64 65 6e 74 69 66 69 65 72 73  to 3 identifiers
2a330 20 70 72 65 63 65 65 64 69 6e 67 20 74 68 65 20   preceeding the 
2a331 4a 4f 49 4e 20 6b 65 79 77 6f 72 64 2c 20 64 65  JOIN keyword, de
2a332 74 65 72 6d 69 6e 65 20 74 68 65 0a 2a 2a 20 74  termine the.** t
2a333 79 70 65 20 6f 66 20 6a 6f 69 6e 2e 20 20 52 65  ype of join.  Re
2a334 74 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20  turn an integer 
2a335 63 6f 6e 73 74 61 6e 74 20 74 68 61 74 20 65 78  constant that ex
2a336 70 72 65 73 73 65 73 20 74 68 61 74 20 74 79 70  presses that typ
2a337 65 0a 2a 2a 20 69 6e 20 74 65 72 6d 73 20 6f 66  e.** in terms of
2a338 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62   the following b
2a339 69 74 20 76 61 6c 75 65 73 3a 0a 2a 2a 0a 2a 2a  it values:.**.**
2a33a 20 20 20 20 20 4a 54 5f 49 4e 4e 45 52 0a 2a 2a       JT_INNER.**
2a33b 20 20 20 20 20 4a 54 5f 43 52 4f 53 53 0a 2a 2a       JT_CROSS.**
2a33c 20 20 20 20 20 4a 54 5f 4f 55 54 45 52 0a 2a 2a       JT_OUTER.**
2a33d 20 20 20 20 20 4a 54 5f 4e 41 54 55 52 41 4c 0a       JT_NATURAL.
2a33e 2a 2a 20 20 20 20 20 4a 54 5f 4c 45 46 54 0a 2a  **     JT_LEFT.*
2a33f 2a 20 20 20 20 20 4a 54 5f 52 49 47 48 54 0a 2a  *     JT_RIGHT.*
2a340 2a 0a 2a 2a 20 41 20 66 75 6c 6c 20 6f 75 74 65  *.** A full oute
2a341 72 20 6a 6f 69 6e 20 69 73 20 74 68 65 20 63 6f  r join is the co
2a342 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 4a 54 5f  mbination of JT_
2a343 4c 45 46 54 20 61 6e 64 20 4a 54 5f 52 49 47 48  LEFT and JT_RIGH
2a344 54 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 69  T..**.** If an i
2a345 6c 6c 65 67 61 6c 20 6f 72 20 75 6e 73 75 70 70  llegal or unsupp
2a346 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 20  orted join type 
2a347 69 73 20 73 65 65 6e 2c 20 74 68 65 6e 20 73 74  is seen, then st
2a348 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20  ill return.** a 
2a349 6a 6f 69 6e 20 74 79 70 65 2c 20 62 75 74 20 70  join type, but p
2a34a 75 74 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 74  ut an error in t
2a34b 68 65 20 70 50 61 72 73 65 20 73 74 72 75 63 74  he pParse struct
2a34c 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ure..*/.SQLITE_P
2a34d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2a34e 65 33 4a 6f 69 6e 54 79 70 65 28 50 61 72 73 65  e3JoinType(Parse
2a34f 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20   *pParse, Token 
2a350 2a 70 41 2c 20 54 6f 6b 65 6e 20 2a 70 42 2c 20  *pA, Token *pB, 
2a351 54 6f 6b 65 6e 20 2a 70 43 29 7b 0a 20 20 69 6e  Token *pC){.  in
2a352 74 20 6a 6f 69 6e 74 79 70 65 20 3d 20 30 3b 0a  t jointype = 0;.
2a353 20 20 54 6f 6b 65 6e 20 2a 61 70 41 6c 6c 5b 33    Token *apAll[3
2a354 5d 3b 0a 20 20 54 6f 6b 65 6e 20 2a 70 3b 0a 20  ];.  Token *p;. 
2a355 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a356 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20              /*  
2a357 20 30 31 32 33 34 35 36 37 38 39 20 31 32 33 34   0123456789 1234
2a358 35 36 37 38 39 20 31 32 33 34 35 36 37 38 39 20  56789 123456789 
2a359 31 32 33 20 2a 2f 0a 20 20 73 74 61 74 69 63 20  123 */.  static 
2a35a 63 6f 6e 73 74 20 63 68 61 72 20 7a 4b 65 79 54  const char zKeyT
2a35b 65 78 74 5b 5d 20 3d 20 22 6e 61 74 75 72 61 6c  ext[] = "natural
2a35c 65 66 74 6f 75 74 65 72 69 67 68 74 66 75 6c 6c  eftouterightfull
2a35d 69 6e 6e 65 72 63 72 6f 73 73 22 3b 0a 20 20 73  innercross";.  s
2a35e 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75  tatic const stru
2a35f 63 74 20 7b 0a 20 20 20 20 75 38 20 69 3b 20 20  ct {.    u8 i;  
2a360 20 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 6e 69        /* Beginni
2a361 6e 67 20 6f 66 20 6b 65 79 77 6f 72 64 20 74 65  ng of keyword te
2a362 78 74 20 69 6e 20 7a 4b 65 79 54 65 78 74 5b 5d  xt in zKeyText[]
2a363 20 2a 2f 0a 20 20 20 20 75 38 20 6e 43 68 61 72   */.    u8 nChar
2a364 3b 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f  ;    /* Length o
2a365 66 20 74 68 65 20 6b 65 79 77 6f 72 64 20 69 6e  f the keyword in
2a366 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f 0a 20   characters */. 
2a367 20 20 20 75 38 20 63 6f 64 65 3b 20 20 20 20 20     u8 code;     
2a368 2f 2a 20 4a 6f 69 6e 20 74 79 70 65 20 6d 61 73  /* Join type mas
2a369 6b 20 2a 2f 0a 20 20 7d 20 61 4b 65 79 77 6f 72  k */.  } aKeywor
2a36a 64 5b 5d 20 3d 20 7b 0a 20 20 20 20 2f 2a 20 6e  d[] = {.    /* n
2a36b 61 74 75 72 61 6c 20 2a 2f 20 7b 20 30 2c 20 20  atural */ { 0,  
2a36c 37 2c 20 4a 54 5f 4e 41 54 55 52 41 4c 20 20 20  7, JT_NATURAL   
2a36d 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 2c 0a               },.
2a36e 20 20 20 20 2f 2a 20 6c 65 66 74 20 20 20 20 2a      /* left    *
2a36f 2f 20 7b 20 36 2c 20 20 34 2c 20 4a 54 5f 4c 45  / { 6,  4, JT_LE
2a370 46 54 7c 4a 54 5f 4f 55 54 45 52 20 20 20 20 20  FT|JT_OUTER     
2a371 20 20 20 20 20 7d 2c 0a 20 20 20 20 2f 2a 20 6f       },.    /* o
2a372 75 74 65 72 20 20 20 2a 2f 20 7b 20 31 30 2c 20  uter   */ { 10, 
2a373 35 2c 20 4a 54 5f 4f 55 54 45 52 20 20 20 20 20  5, JT_OUTER     
2a374 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 2c 0a               },.
2a375 20 20 20 20 2f 2a 20 72 69 67 68 74 20 20 20 2a      /* right   *
2a376 2f 20 7b 20 31 34 2c 20 35 2c 20 4a 54 5f 52 49  / { 14, 5, JT_RI
2a377 47 48 54 7c 4a 54 5f 4f 55 54 45 52 20 20 20 20  GHT|JT_OUTER    
2a378 20 20 20 20 20 7d 2c 0a 20 20 20 20 2f 2a 20 66       },.    /* f
2a379 75 6c 6c 20 20 20 20 2a 2f 20 7b 20 31 39 2c 20  ull    */ { 19, 
2a37a 34 2c 20 4a 54 5f 4c 45 46 54 7c 4a 54 5f 52 49  4, JT_LEFT|JT_RI
2a37b 47 48 54 7c 4a 54 5f 4f 55 54 45 52 20 7d 2c 0a  GHT|JT_OUTER },.
2a37c 20 20 20 20 2f 2a 20 69 6e 6e 65 72 20 20 20 2a      /* inner   *
2a37d 2f 20 7b 20 32 33 2c 20 35 2c 20 4a 54 5f 49 4e  / { 23, 5, JT_IN
2a37e 4e 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20  NER             
2a37f 20 20 20 20 20 7d 2c 0a 20 20 20 20 2f 2a 20 63       },.    /* c
2a380 72 6f 73 73 20 20 20 2a 2f 20 7b 20 32 38 2c 20  ross   */ { 28, 
2a381 35 2c 20 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f 43  5, JT_INNER|JT_C
2a382 52 4f 53 53 20 20 20 20 20 20 20 20 20 7d 2c 0a  ROSS         },.
2a383 20 20 7d 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b    };.  int i, j;
2a384 0a 20 20 61 70 41 6c 6c 5b 30 5d 20 3d 20 70 41  .  apAll[0] = pA
2a385 3b 0a 20 20 61 70 41 6c 6c 5b 31 5d 20 3d 20 70  ;.  apAll[1] = p
2a386 42 3b 0a 20 20 61 70 41 6c 6c 5b 32 5d 20 3d 20  B;.  apAll[2] = 
2a387 70 43 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  pC;.  for(i=0; i
2a388 3c 33 20 26 26 20 61 70 41 6c 6c 5b 69 5d 3b 20  <3 && apAll[i]; 
2a389 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 61 70  i++){.    p = ap
2a38a 41 6c 6c 5b 69 5d 3b 0a 20 20 20 20 66 6f 72 28  All[i];.    for(
2a38b 6a 3d 30 3b 20 6a 3c 41 72 72 61 79 53 69 7a 65  j=0; j<ArraySize
2a38c 28 61 4b 65 79 77 6f 72 64 29 3b 20 6a 2b 2b 29  (aKeyword); j++)
2a38d 7b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6e  {.      if( p->n
2a38e 3d 3d 61 4b 65 79 77 6f 72 64 5b 6a 5d 2e 6e 43  ==aKeyword[j].nC
2a38f 68 61 72 20 0a 20 20 20 20 20 20 20 20 20 20 26  har .          &
2a390 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  & sqlite3StrNICm
2a391 70 28 28 63 68 61 72 2a 29 70 2d 3e 7a 2c 20 26  p((char*)p->z, &
2a392 7a 4b 65 79 54 65 78 74 5b 61 4b 65 79 77 6f 72  zKeyText[aKeywor
2a393 64 5b 6a 5d 2e 69 5d 2c 20 70 2d 3e 6e 29 3d 3d  d[j].i], p->n)==
2a394 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6a 6f 69  0 ){.        joi
2a395 6e 74 79 70 65 20 7c 3d 20 61 4b 65 79 77 6f 72  ntype |= aKeywor
2a396 64 5b 6a 5d 2e 63 6f 64 65 3b 0a 20 20 20 20 20  d[j].code;.     
2a397 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2a398 7d 0a 20 20 20 20 7d 0a 20 20 20 20 74 65 73 74  }.    }.    test
2a399 63 61 73 65 28 20 6a 3d 3d 30 20 7c 7c 20 6a 3d  case( j==0 || j=
2a39a 3d 31 20 7c 7c 20 6a 3d 3d 32 20 7c 7c 20 6a 3d  =1 || j==2 || j=
2a39b 3d 33 20 7c 7c 20 6a 3d 3d 34 20 7c 7c 20 6a 3d  =3 || j==4 || j=
2a39c 3d 35 20 7c 7c 20 6a 3d 3d 36 20 29 3b 0a 20 20  =5 || j==6 );.  
2a39d 20 20 69 66 28 20 6a 3e 3d 41 72 72 61 79 53 69    if( j>=ArraySi
2a39e 7a 65 28 61 4b 65 79 77 6f 72 64 29 20 29 7b 0a  ze(aKeyword) ){.
2a39f 20 20 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 7c        jointype |
2a3a0 3d 20 4a 54 5f 45 52 52 4f 52 3b 0a 20 20 20 20  = JT_ERROR;.    
2a3a1 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
2a3a2 20 7d 0a 20 20 69 66 28 0a 20 20 20 20 20 28 6a   }.  if(.     (j
2a3a3 6f 69 6e 74 79 70 65 20 26 20 28 4a 54 5f 49 4e  ointype & (JT_IN
2a3a4 4e 45 52 7c 4a 54 5f 4f 55 54 45 52 29 29 3d 3d  NER|JT_OUTER))==
2a3a5 28 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f 4f 55 54  (JT_INNER|JT_OUT
2a3a6 45 52 29 20 7c 7c 0a 20 20 20 20 20 28 6a 6f 69  ER) ||.     (joi
2a3a7 6e 74 79 70 65 20 26 20 4a 54 5f 45 52 52 4f 52  ntype & JT_ERROR
2a3a8 29 21 3d 30 0a 20 20 29 7b 0a 20 20 20 20 63 6f  )!=0.  ){.    co
2a3a9 6e 73 74 20 63 68 61 72 20 2a 7a 53 70 20 3d 20  nst char *zSp = 
2a3aa 22 20 22 3b 0a 20 20 20 20 61 73 73 65 72 74 28  " ";.    assert(
2a3ab 20 70 42 21 3d 30 20 29 3b 0a 20 20 20 20 69 66   pB!=0 );.    if
2a3ac 28 20 70 43 3d 3d 30 20 29 7b 20 7a 53 70 2b 2b  ( pC==0 ){ zSp++
2a3ad 3b 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45  ; }.    sqlite3E
2a3ae 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2a3af 22 75 6e 6b 6e 6f 77 6e 20 6f 72 20 75 6e 73 75  "unknown or unsu
2a3b0 70 70 6f 72 74 65 64 20 6a 6f 69 6e 20 74 79 70  pported join typ
2a3b1 65 3a 20 22 0a 20 20 20 20 20 20 20 22 25 54 20  e: ".       "%T 
2a3b2 25 54 25 73 25 54 22 2c 20 70 41 2c 20 70 42 2c  %T%s%T", pA, pB,
2a3b3 20 7a 53 70 2c 20 70 43 29 3b 0a 20 20 20 20 6a   zSp, pC);.    j
2a3b4 6f 69 6e 74 79 70 65 20 3d 20 4a 54 5f 49 4e 4e  ointype = JT_INN
2a3b5 45 52 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ER;.  }else if( 
2a3b6 28 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4f  (jointype & JT_O
2a3b7 55 54 45 52 29 21 3d 30 20 0a 20 20 20 20 20 20  UTER)!=0 .      
2a3b8 20 20 20 26 26 20 28 6a 6f 69 6e 74 79 70 65 20     && (jointype 
2a3b9 26 20 28 4a 54 5f 4c 45 46 54 7c 4a 54 5f 52 49  & (JT_LEFT|JT_RI
2a3ba 47 48 54 29 29 21 3d 4a 54 5f 4c 45 46 54 20 29  GHT))!=JT_LEFT )
2a3bb 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
2a3bc 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20  orMsg(pParse, . 
2a3bd 20 20 20 20 20 22 52 49 47 48 54 20 61 6e 64 20       "RIGHT and 
2a3be 46 55 4c 4c 20 4f 55 54 45 52 20 4a 4f 49 4e 73  FULL OUTER JOINs
2a3bf 20 61 72 65 20 6e 6f 74 20 63 75 72 72 65 6e 74   are not current
2a3c0 6c 79 20 73 75 70 70 6f 72 74 65 64 22 29 3b 0a  ly supported");.
2a3c1 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 3d 20 4a      jointype = J
2a3c2 54 5f 49 4e 4e 45 52 3b 0a 20 20 7d 0a 20 20 72  T_INNER;.  }.  r
2a3c3 65 74 75 72 6e 20 6a 6f 69 6e 74 79 70 65 3b 0a  eturn jointype;.
2a3c4 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
2a3c5 74 68 65 20 69 6e 64 65 78 20 6f 66 20 61 20 63  the index of a c
2a3c6 6f 6c 75 6d 6e 20 69 6e 20 61 20 74 61 62 6c 65  olumn in a table
2a3c7 2e 20 20 52 65 74 75 72 6e 20 2d 31 20 69 66 20  .  Return -1 if 
2a3c8 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73  the column.** is
2a3c9 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 65 64 20 69   not contained i
2a3ca 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a  n the table..*/.
2a3cb 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6c 75 6d  static int colum
2a3cc 6e 49 6e 64 65 78 28 54 61 62 6c 65 20 2a 70 54  nIndex(Table *pT
2a3cd 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ab, const char *
2a3ce 7a 43 6f 6c 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  zCol){.  int i;.
2a3cf 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61    for(i=0; i<pTa
2a3d0 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20  b->nCol; i++){. 
2a3d1 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
2a3d2 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f 6c  rICmp(pTab->aCol
2a3d3 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29  [i].zName, zCol)
2a3d4 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 69 3b 0a  ==0 ) return i;.
2a3d5 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31 3b    }.  return -1;
2a3d6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
2a3d7 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
2a3d8 74 6f 20 61 64 64 20 74 65 72 6d 73 20 69 6d 70  to add terms imp
2a3d9 6c 69 65 64 20 62 79 20 4a 4f 49 4e 20 73 79 6e  lied by JOIN syn
2a3da 74 61 78 20 74 6f 20 74 68 65 0a 2a 2a 20 57 48  tax to the.** WH
2a3db 45 52 45 20 63 6c 61 75 73 65 20 65 78 70 72 65  ERE clause expre
2a3dc 73 73 69 6f 6e 20 6f 66 20 61 20 53 45 4c 45 43  ssion of a SELEC
2a3dd 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65  T statement. The
2a3de 20 6e 65 77 20 74 65 72 6d 2c 20 77 68 69 63 68   new term, which
2a3df 0a 2a 2a 20 69 73 20 41 4e 44 65 64 20 77 69 74  .** is ANDed wit
2a3e0 68 20 74 68 65 20 65 78 69 73 74 69 6e 67 20 57  h the existing W
2a3e1 48 45 52 45 20 63 6c 61 75 73 65 2c 20 69 73 20  HERE clause, is 
2a3e2 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a  of the form:.**.
2a3e3 2a 2a 20 20 20 20 28 74 61 62 31 2e 63 6f 6c 31  **    (tab1.col1
2a3e4 20 3d 20 74 61 62 32 2e 63 6f 6c 32 29 0a 2a 2a   = tab2.col2).**
2a3e5 0a 2a 2a 20 77 68 65 72 65 20 74 61 62 31 20 69  .** where tab1 i
2a3e6 73 20 74 68 65 20 69 53 72 63 27 74 68 20 74 61  s the iSrc'th ta
2a3e7 62 6c 65 20 69 6e 20 53 72 63 4c 69 73 74 20 70  ble in SrcList p
2a3e8 53 72 63 20 61 6e 64 20 74 61 62 32 20 69 73 20  Src and tab2 is 
2a3e9 74 68 65 20 0a 2a 2a 20 28 69 53 72 63 2b 31 29  the .** (iSrc+1)
2a3ea 27 74 68 2e 20 43 6f 6c 75 6d 6e 20 63 6f 6c 31  'th. Column col1
2a3eb 20 69 73 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 4c   is column iColL
2a3ec 65 66 74 20 6f 66 20 74 61 62 31 2c 20 61 6e 64  eft of tab1, and
2a3ed 20 63 6f 6c 32 20 69 73 0a 2a 2a 20 63 6f 6c 75   col2 is.** colu
2a3ee 6d 6e 20 69 43 6f 6c 52 69 67 68 74 20 6f 66 20  mn iColRight of 
2a3ef 74 61 62 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tab2..*/.static 
2a3f0 76 6f 69 64 20 61 64 64 57 68 65 72 65 54 65 72  void addWhereTer
2a3f1 6d 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  m(.  Parse *pPar
2a3f2 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  se,             
2a3f3 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2a3f4 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63  context */.  Src
2a3f5 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20 20  List *pSrc,     
2a3f6 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a3f7 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 69  List of tables i
2a3f8 6e 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f  n FROM clause */
2a3f9 0a 20 20 69 6e 74 20 69 53 72 63 2c 20 20 20 20  .  int iSrc,    
2a3fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a3fb 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66     /* Index of f
2a3fc 69 72 73 74 20 74 61 62 6c 65 20 74 6f 20 6a 6f  irst table to jo
2a3fd 69 6e 20 69 6e 20 70 53 72 63 20 2a 2f 0a 20 20  in in pSrc */.  
2a3fe 69 6e 74 20 69 43 6f 6c 4c 65 66 74 2c 20 20 20  int iColLeft,   
2a3ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a400 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75  /* Index of colu
2a401 6d 6e 20 69 6e 20 66 69 72 73 74 20 74 61 62 6c  mn in first tabl
2a402 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 52  e */.  int iColR
2a403 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20  ight,           
2a404 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
2a405 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 73 65 63  of column in sec
2a406 6f 6e 64 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  ond table */.  i
2a407 6e 74 20 69 73 4f 75 74 65 72 4a 6f 69 6e 2c 20  nt isOuterJoin, 
2a408 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2a409 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69  * True if this i
2a40a 73 20 61 6e 20 4f 55 54 45 52 20 6a 6f 69 6e 20  s an OUTER join 
2a40b 2a 2f 0a 20 20 45 78 70 72 20 2a 2a 70 70 57 68  */.  Expr **ppWh
2a40c 65 72 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ere             
2a40d 20 20 20 20 20 2f 2a 20 49 4e 2f 4f 55 54 3a 20       /* IN/OUT: 
2a40e 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  The WHERE clause
2a40f 20 74 6f 20 61 64 64 20 74 6f 20 2a 2f 0a 29 7b   to add to */.){
2a410 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2a411 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 45   pParse->db;.  E
2a412 78 70 72 20 2a 70 45 31 3b 0a 20 20 45 78 70 72  xpr *pE1;.  Expr
2a413 20 2a 70 45 32 3b 0a 20 20 45 78 70 72 20 2a 70   *pE2;.  Expr *p
2a414 45 71 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  Eq;..  assert( p
2a415 53 72 63 2d 3e 6e 53 72 63 3e 28 69 53 72 63 2b  Src->nSrc>(iSrc+
2a416 31 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  1) );.  assert( 
2a417 70 53 72 63 2d 3e 61 5b 69 53 72 63 5d 2e 70 54  pSrc->a[iSrc].pT
2a418 61 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ab );.  assert( 
2a419 70 53 72 63 2d 3e 61 5b 69 53 72 63 2b 31 5d 2e  pSrc->a[iSrc+1].
2a41a 70 54 61 62 20 29 3b 0a 0a 20 20 70 45 31 20 3d  pTab );..  pE1 =
2a41b 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 43 6f   sqlite3CreateCo
2a41c 6c 75 6d 6e 45 78 70 72 28 64 62 2c 20 70 53 72  lumnExpr(db, pSr
2a41d 63 2c 20 69 53 72 63 2c 20 69 43 6f 6c 4c 65 66  c, iSrc, iColLef
2a41e 74 29 3b 0a 20 20 70 45 32 20 3d 20 73 71 6c 69  t);.  pE2 = sqli
2a41f 74 65 33 43 72 65 61 74 65 43 6f 6c 75 6d 6e 45  te3CreateColumnE
2a420 78 70 72 28 64 62 2c 20 70 53 72 63 2c 20 69 53  xpr(db, pSrc, iS
2a421 72 63 2b 31 2c 20 69 43 6f 6c 52 69 67 68 74 29  rc+1, iColRight)
2a422 3b 0a 0a 20 20 70 45 71 20 3d 20 73 71 6c 69 74  ;..  pEq = sqlit
2a423 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2a424 54 4b 5f 45 51 2c 20 70 45 31 2c 20 70 45 32 2c  TK_EQ, pE1, pE2,
2a425 20 30 29 3b 0a 20 20 69 66 28 20 70 45 71 20 26   0);.  if( pEq &
2a426 26 20 69 73 4f 75 74 65 72 4a 6f 69 6e 20 29 7b  & isOuterJoin ){
2a427 0a 20 20 20 20 45 78 70 72 53 65 74 50 72 6f 70  .    ExprSetProp
2a428 65 72 74 79 28 70 45 71 2c 20 45 50 5f 46 72 6f  erty(pEq, EP_Fro
2a429 6d 4a 6f 69 6e 29 3b 0a 20 20 20 20 61 73 73 65  mJoin);.    asse
2a42a 72 74 28 20 21 45 78 70 72 48 61 73 41 6e 79 50  rt( !ExprHasAnyP
2a42b 72 6f 70 65 72 74 79 28 70 45 71 2c 20 45 50 5f  roperty(pEq, EP_
2a42c 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50 5f 52 65 64  TokenOnly|EP_Red
2a42d 75 63 65 64 29 20 29 3b 0a 20 20 20 20 45 78 70  uced) );.    Exp
2a42e 72 53 65 74 49 72 72 65 64 75 63 69 62 6c 65 28  rSetIrreducible(
2a42f 70 45 71 29 3b 0a 20 20 20 20 70 45 71 2d 3e 69  pEq);.    pEq->i
2a430 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 3d  RightJoinTable =
2a431 20 28 69 31 36 29 70 45 32 2d 3e 69 54 61 62 6c   (i16)pE2->iTabl
2a432 65 3b 0a 20 20 7d 0a 20 20 2a 70 70 57 68 65 72  e;.  }.  *ppWher
2a433 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  e = sqlite3ExprA
2a434 6e 64 28 64 62 2c 20 2a 70 70 57 68 65 72 65 2c  nd(db, *ppWhere,
2a435 20 70 45 71 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pEq);.}../*.** 
2a436 53 65 74 20 74 68 65 20 45 50 5f 46 72 6f 6d 4a  Set the EP_FromJ
2a437 6f 69 6e 20 70 72 6f 70 65 72 74 79 20 6f 6e 20  oin property on 
2a438 61 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 65  all terms of the
2a439 20 67 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f   given expressio
2a43a 6e 2e 0a 2a 2a 20 41 6e 64 20 73 65 74 20 74 68  n..** And set th
2a43b 65 20 45 78 70 72 2e 69 52 69 67 68 74 4a 6f 69  e Expr.iRightJoi
2a43c 6e 54 61 62 6c 65 20 74 6f 20 69 54 61 62 6c 65  nTable to iTable
2a43d 20 66 6f 72 20 65 76 65 72 79 20 74 65 72 6d 20   for every term 
2a43e 69 6e 20 74 68 65 0a 2a 2a 20 65 78 70 72 65 73  in the.** expres
2a43f 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  sion..**.** The 
2a440 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20 70 72 6f 70  EP_FromJoin prop
2a441 65 72 74 79 20 69 73 20 75 73 65 64 20 6f 6e 20  erty is used on 
2a442 74 65 72 6d 73 20 6f 66 20 61 6e 20 65 78 70 72  terms of an expr
2a443 65 73 73 69 6f 6e 20 74 6f 20 74 65 6c 6c 0a 2a  ession to tell.*
2a444 2a 20 74 68 65 20 4c 45 46 54 20 4f 55 54 45 52  * the LEFT OUTER
2a445 20 4a 4f 49 4e 20 70 72 6f 63 65 73 73 69 6e 67   JOIN processing
2a446 20 6c 6f 67 69 63 20 74 68 61 74 20 74 68 69 73   logic that this
2a447 20 74 65 72 6d 20 69 73 20 70 61 72 74 20 6f 66   term is part of
2a448 20 74 68 65 0a 2a 2a 20 6a 6f 69 6e 20 72 65 73   the.** join res
2a449 74 72 69 63 74 69 6f 6e 20 73 70 65 63 69 66 69  triction specifi
2a44a 65 64 20 69 6e 20 74 68 65 20 4f 4e 20 6f 72 20  ed in the ON or 
2a44b 55 53 49 4e 47 20 63 6c 61 75 73 65 20 61 6e 64  USING clause and
2a44c 20 6e 6f 74 20 61 20 70 61 72 74 0a 2a 2a 20 6f   not a part.** o
2a44d 66 20 74 68 65 20 6d 6f 72 65 20 67 65 6e 65 72  f the more gener
2a44e 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  al WHERE clause.
2a44f 20 20 54 68 65 73 65 20 74 65 72 6d 73 20 61 72    These terms ar
2a450 65 20 6d 6f 76 65 64 20 6f 76 65 72 20 74 6f 20  e moved over to 
2a451 74 68 65 0a 2a 2a 20 57 48 45 52 45 20 63 6c 61  the.** WHERE cla
2a452 75 73 65 20 64 75 72 69 6e 67 20 6a 6f 69 6e 20  use during join 
2a453 70 72 6f 63 65 73 73 69 6e 67 20 62 75 74 20 77  processing but w
2a454 65 20 6e 65 65 64 20 74 6f 20 72 65 6d 65 6d 62  e need to rememb
2a455 65 72 20 74 68 61 74 20 74 68 65 79 0a 2a 2a 20  er that they.** 
2a456 6f 72 69 67 69 6e 61 74 65 64 20 69 6e 20 74 68  originated in th
2a457 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c  e ON or USING cl
2a458 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ause..**.** The 
2a459 45 78 70 72 2e 69 52 69 67 68 74 4a 6f 69 6e 54  Expr.iRightJoinT
2a45a 61 62 6c 65 20 74 65 6c 6c 73 20 74 68 65 20 57  able tells the W
2a45b 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63  HERE clause proc
2a45c 65 73 73 69 6e 67 20 74 68 61 74 20 74 68 65 0a  essing that the.
2a45d 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 64 65  ** expression de
2a45e 70 65 6e 64 73 20 6f 6e 20 74 61 62 6c 65 20 69  pends on table i
2a45f 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 65  RightJoinTable e
2a460 76 65 6e 20 69 66 20 74 68 61 74 20 74 61 62 6c  ven if that tabl
2a461 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 65 78 70 6c  e is not.** expl
2a462 69 63 69 74 6c 79 20 6d 65 6e 74 69 6f 6e 65 64  icitly mentioned
2a463 20 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69   in the expressi
2a464 6f 6e 2e 20 20 54 68 61 74 20 69 6e 66 6f 72 6d  on.  That inform
2a465 61 74 69 6f 6e 20 69 73 20 6e 65 65 64 65 64 0a  ation is needed.
2a466 2a 2a 20 66 6f 72 20 63 61 73 65 73 20 6c 69 6b  ** for cases lik
2a467 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
2a468 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74   SELECT * FROM t
2a469 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74 32 20 4f  1 LEFT JOIN t2 O
2a46a 4e 20 74 31 2e 61 3d 74 32 2e 62 20 41 4e 44 20  N t1.a=t2.b AND 
2a46b 74 31 2e 78 3d 35 0a 2a 2a 0a 2a 2a 20 54 68 65  t1.x=5.**.** The
2a46c 20 77 68 65 72 65 20 63 6c 61 75 73 65 20 6e 65   where clause ne
2a46d 65 64 73 20 74 6f 20 64 65 66 65 72 20 74 68 65  eds to defer the
2a46e 20 68 61 6e 64 6c 69 6e 67 20 6f 66 20 74 68 65   handling of the
2a46f 20 74 31 2e 78 3d 35 0a 2a 2a 20 74 65 72 6d 20   t1.x=5.** term 
2a470 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65 20  until after the 
2a471 74 32 20 6c 6f 6f 70 20 6f 66 20 74 68 65 20 6a  t2 loop of the j
2a472 6f 69 6e 2e 20 20 49 6e 20 74 68 61 74 20 77 61  oin.  In that wa
2a473 79 2c 20 61 0a 2a 2a 20 4e 55 4c 4c 20 74 32 20  y, a.** NULL t2 
2a474 72 6f 77 20 77 69 6c 6c 20 62 65 20 69 6e 73 65  row will be inse
2a475 72 74 65 64 20 77 68 65 6e 65 76 65 72 20 74 31  rted whenever t1
2a476 2e 78 21 3d 35 2e 20 20 49 66 20 77 65 20 64 6f  .x!=5.  If we do
2a477 20 6e 6f 74 0a 2a 2a 20 64 65 66 65 72 20 74 68   not.** defer th
2a478 65 20 68 61 6e 64 6c 69 6e 67 20 6f 66 20 74 31  e handling of t1
2a479 2e 78 3d 35 2c 20 69 74 20 77 69 6c 6c 20 62 65  .x=5, it will be
2a47a 20 70 72 6f 63 65 73 73 65 64 20 69 6d 6d 65 64   processed immed
2a47b 69 61 74 65 6c 79 0a 2a 2a 20 61 66 74 65 72 20  iately.** after 
2a47c 74 68 65 20 74 31 20 6c 6f 6f 70 20 61 6e 64 20  the t1 loop and 
2a47d 72 6f 77 73 20 77 69 74 68 20 74 31 2e 78 21 3d  rows with t1.x!=
2a47e 35 20 77 69 6c 6c 20 6e 65 76 65 72 20 61 70 70  5 will never app
2a47f 65 61 72 20 69 6e 0a 2a 2a 20 74 68 65 20 6f 75  ear in.** the ou
2a480 74 70 75 74 2c 20 77 68 69 63 68 20 69 73 20 69  tput, which is i
2a481 6e 63 6f 72 72 65 63 74 2e 0a 2a 2f 0a 73 74 61  ncorrect..*/.sta
2a482 74 69 63 20 76 6f 69 64 20 73 65 74 4a 6f 69 6e  tic void setJoin
2a483 45 78 70 72 28 45 78 70 72 20 2a 70 2c 20 69 6e  Expr(Expr *p, in
2a484 74 20 69 54 61 62 6c 65 29 7b 0a 20 20 77 68 69  t iTable){.  whi
2a485 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 45 78 70  le( p ){.    Exp
2a486 72 53 65 74 50 72 6f 70 65 72 74 79 28 70 2c 20  rSetProperty(p, 
2a487 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 3b 0a 20 20  EP_FromJoin);.  
2a488 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
2a489 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c  asAnyProperty(p,
2a48a 20 45 50 5f 54 6f 6b 65 6e 4f 6e 6c 79 7c 45 50   EP_TokenOnly|EP
2a48b 5f 52 65 64 75 63 65 64 29 20 29 3b 0a 20 20 20  _Reduced) );.   
2a48c 20 45 78 70 72 53 65 74 49 72 72 65 64 75 63 69   ExprSetIrreduci
2a48d 62 6c 65 28 70 29 3b 0a 20 20 20 20 70 2d 3e 69  ble(p);.    p->i
2a48e 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 3d  RightJoinTable =
2a48f 20 28 69 31 36 29 69 54 61 62 6c 65 3b 0a 20 20   (i16)iTable;.  
2a490 20 20 73 65 74 4a 6f 69 6e 45 78 70 72 28 70 2d    setJoinExpr(p-
2a491 3e 70 4c 65 66 74 2c 20 69 54 61 62 6c 65 29 3b  >pLeft, iTable);
2a492 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70 52 69 67  .    p = p->pRig
2a493 68 74 3b 0a 20 20 7d 20 0a 7d 0a 0a 2f 2a 0a 2a  ht;.  } .}../*.*
2a494 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 70  * This routine p
2a495 72 6f 63 65 73 73 65 73 20 74 68 65 20 6a 6f 69  rocesses the joi
2a496 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  n information fo
2a497 72 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65  r a SELECT state
2a498 6d 65 6e 74 2e 0a 2a 2a 20 4f 4e 20 61 6e 64 20  ment..** ON and 
2a499 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 61 72  USING clauses ar
2a49a 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f  e converted into
2a49b 20 65 78 74 72 61 20 74 65 72 6d 73 20 6f 66 20   extra terms of 
2a49c 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2a49d 2e 0a 2a 2a 20 4e 41 54 55 52 41 4c 20 6a 6f 69  ..** NATURAL joi
2a49e 6e 73 20 61 6c 73 6f 20 63 72 65 61 74 65 20 65  ns also create e
2a49f 78 74 72 61 20 57 48 45 52 45 20 63 6c 61 75 73  xtra WHERE claus
2a4a0 65 20 74 65 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 54  e terms..**.** T
2a4a1 68 65 20 74 65 72 6d 73 20 6f 66 20 61 20 46 52  he terms of a FR
2a4a2 4f 4d 20 63 6c 61 75 73 65 20 61 72 65 20 63 6f  OM clause are co
2a4a3 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20 53  ntained in the S
2a4a4 65 6c 65 63 74 2e 70 53 72 63 20 73 74 72 75 63  elect.pSrc struc
2a4a5 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20 6c 65 66  ture..** The lef
2a4a6 74 20 6d 6f 73 74 20 74 61 62 6c 65 20 69 73 20  t most table is 
2a4a7 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20  the first entry 
2a4a8 69 6e 20 53 65 6c 65 63 74 2e 70 53 72 63 2e 20  in Select.pSrc. 
2a4a9 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 0a   The right-most.
2a4aa 2a 2a 20 74 61 62 6c 65 20 69 73 20 74 68 65 20  ** table is the 
2a4ab 6c 61 73 74 20 65 6e 74 72 79 2e 20 20 54 68 65  last entry.  The
2a4ac 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 20 69   join operator i
2a4ad 73 20 68 65 6c 64 20 69 6e 20 74 68 65 20 65 6e  s held in the en
2a4ae 74 72 79 20 74 6f 0a 2a 2a 20 74 68 65 20 6c 65  try to.** the le
2a4af 66 74 2e 20 20 54 68 75 73 20 65 6e 74 72 79 20  ft.  Thus entry 
2a4b0 30 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6a  0 contains the j
2a4b1 6f 69 6e 20 6f 70 65 72 61 74 6f 72 20 66 6f 72  oin operator for
2a4b2 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 65   the join betwee
2a4b3 6e 0a 2a 2a 20 65 6e 74 72 69 65 73 20 30 20 61  n.** entries 0 a
2a4b4 6e 64 20 31 2e 20 20 41 6e 79 20 4f 4e 20 6f 72  nd 1.  Any ON or
2a4b5 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 61   USING clauses a
2a4b6 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
2a4b7 68 65 20 6a 6f 69 6e 20 61 72 65 0a 2a 2a 20 61  he join are.** a
2a4b8 6c 73 6f 20 61 74 74 61 63 68 65 64 20 74 6f 20  lso attached to 
2a4b9 74 68 65 20 6c 65 66 74 20 65 6e 74 72 79 2e 0a  the left entry..
2a4ba 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
2a4bb 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  ne returns the n
2a4bc 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20  umber of errors 
2a4bd 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2f 0a  encountered..*/.
2a4be 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74  static int sqlit
2a4bf 65 50 72 6f 63 65 73 73 4a 6f 69 6e 28 50 61 72  eProcessJoin(Par
2a4c0 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c 65  se *pParse, Sele
2a4c1 63 74 20 2a 70 29 7b 0a 20 20 53 72 63 4c 69 73  ct *p){.  SrcLis
2a4c2 74 20 2a 70 53 72 63 3b 20 20 20 20 20 20 20 20  t *pSrc;        
2a4c3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c            /* All
2a4c4 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 46   tables in the F
2a4c5 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ROM clause */.  
2a4c6 69 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20  int i, j;       
2a4c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a4c8 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73  /* Loop counters
2a4c9 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63   */.  struct Src
2a4ca 4c 69 73 74 5f 69 74 65 6d 20 2a 70 4c 65 66 74  List_item *pLeft
2a4cb 3b 20 20 20 20 20 2f 2a 20 4c 65 66 74 20 74 61  ;     /* Left ta
2a4cc 62 6c 65 20 62 65 69 6e 67 20 6a 6f 69 6e 65 64  ble being joined
2a4cd 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63   */.  struct Src
2a4ce 4c 69 73 74 5f 69 74 65 6d 20 2a 70 52 69 67 68  List_item *pRigh
2a4cf 74 3b 20 20 20 20 2f 2a 20 52 69 67 68 74 20 74  t;    /* Right t
2a4d0 61 62 6c 65 20 62 65 69 6e 67 20 6a 6f 69 6e 65  able being joine
2a4d1 64 20 2a 2f 0a 0a 20 20 70 53 72 63 20 3d 20 70  d */..  pSrc = p
2a4d2 2d 3e 70 53 72 63 3b 0a 20 20 70 4c 65 66 74 20  ->pSrc;.  pLeft 
2a4d3 3d 20 26 70 53 72 63 2d 3e 61 5b 30 5d 3b 0a 20  = &pSrc->a[0];. 
2a4d4 20 70 52 69 67 68 74 20 3d 20 26 70 4c 65 66 74   pRight = &pLeft
2a4d5 5b 31 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  [1];.  for(i=0; 
2a4d6 69 3c 70 53 72 63 2d 3e 6e 53 72 63 2d 31 3b 20  i<pSrc->nSrc-1; 
2a4d7 69 2b 2b 2c 20 70 52 69 67 68 74 2b 2b 2c 20 70  i++, pRight++, p
2a4d8 4c 65 66 74 2b 2b 29 7b 0a 20 20 20 20 54 61 62  Left++){.    Tab
2a4d9 6c 65 20 2a 70 4c 65 66 74 54 61 62 20 3d 20 70  le *pLeftTab = p
2a4da 4c 65 66 74 2d 3e 70 54 61 62 3b 0a 20 20 20 20  Left->pTab;.    
2a4db 54 61 62 6c 65 20 2a 70 52 69 67 68 74 54 61 62  Table *pRightTab
2a4dc 20 3d 20 70 52 69 67 68 74 2d 3e 70 54 61 62 3b   = pRight->pTab;
2a4dd 0a 20 20 20 20 69 6e 74 20 69 73 4f 75 74 65 72  .    int isOuter
2a4de 3b 0a 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52  ;..    if( NEVER
2a4df 28 70 4c 65 66 74 54 61 62 3d 3d 30 20 7c 7c 20  (pLeftTab==0 || 
2a4e0 70 52 69 67 68 74 54 61 62 3d 3d 30 29 20 29 20  pRightTab==0) ) 
2a4e1 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 73  continue;.    is
2a4e2 4f 75 74 65 72 20 3d 20 28 70 52 69 67 68 74 2d  Outer = (pRight-
2a4e3 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4f  >jointype & JT_O
2a4e4 55 54 45 52 29 21 3d 30 3b 0a 0a 20 20 20 20 2f  UTER)!=0;..    /
2a4e5 2a 20 57 68 65 6e 20 74 68 65 20 4e 41 54 55 52  * When the NATUR
2a4e6 41 4c 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72  AL keyword is pr
2a4e7 65 73 65 6e 74 2c 20 61 64 64 20 57 48 45 52 45  esent, add WHERE
2a4e8 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 66 6f   clause terms fo
2a4e9 72 0a 20 20 20 20 2a 2a 20 65 76 65 72 79 20 63  r.    ** every c
2a4ea 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 65 20 74  olumn that the t
2a4eb 77 6f 20 74 61 62 6c 65 73 20 68 61 76 65 20 69  wo tables have i
2a4ec 6e 20 63 6f 6d 6d 6f 6e 2e 0a 20 20 20 20 2a 2f  n common..    */
2a4ed 0a 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d  .    if( pRight-
2a4ee 3e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4e  >jointype & JT_N
2a4ef 41 54 55 52 41 4c 20 29 7b 0a 20 20 20 20 20 20  ATURAL ){.      
2a4f0 69 66 28 20 70 52 69 67 68 74 2d 3e 70 4f 6e 20  if( pRight->pOn 
2a4f1 7c 7c 20 70 52 69 67 68 74 2d 3e 70 55 73 69 6e  || pRight->pUsin
2a4f2 67 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  g ){.        sql
2a4f3 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2a4f4 72 73 65 2c 20 22 61 20 4e 41 54 55 52 41 4c 20  rse, "a NATURAL 
2a4f5 6a 6f 69 6e 20 6d 61 79 20 6e 6f 74 20 68 61 76  join may not hav
2a4f6 65 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 22  e ".           "
2a4f7 61 6e 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63  an ON or USING c
2a4f8 6c 61 75 73 65 22 2c 20 30 29 3b 0a 20 20 20 20  lause", 0);.    
2a4f9 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2a4fa 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28      }.      for(
2a4fb 6a 3d 30 3b 20 6a 3c 70 4c 65 66 74 54 61 62 2d  j=0; j<pLeftTab-
2a4fc 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20  >nCol; j++){.   
2a4fd 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65       char *zName
2a4fe 20 3d 20 70 4c 65 66 74 54 61 62 2d 3e 61 43 6f   = pLeftTab->aCo
2a4ff 6c 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  l[j].zName;.    
2a500 20 20 20 20 69 6e 74 20 69 52 69 67 68 74 43 6f      int iRightCo
2a501 6c 20 3d 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28  l = columnIndex(
2a502 70 52 69 67 68 74 54 61 62 2c 20 7a 4e 61 6d 65  pRightTab, zName
2a503 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69  );.        if( i
2a504 52 69 67 68 74 43 6f 6c 3e 3d 30 20 29 7b 0a 20  RightCol>=0 ){. 
2a505 20 20 20 20 20 20 20 20 20 61 64 64 57 68 65 72           addWher
2a506 65 54 65 72 6d 28 70 50 61 72 73 65 2c 20 70 53  eTerm(pParse, pS
2a507 72 63 2c 20 69 2c 20 6a 2c 20 69 52 69 67 68 74  rc, i, j, iRight
2a508 43 6f 6c 2c 20 69 73 4f 75 74 65 72 2c 20 26 70  Col, isOuter, &p
2a509 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 20 20 20  ->pWhere);.     
2a50a 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
2a50b 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 69 73 61 6c   }..    /* Disal
2a50c 6c 6f 77 20 62 6f 74 68 20 4f 4e 20 61 6e 64 20  low both ON and 
2a50d 55 53 49 4e 47 20 63 6c 61 75 73 65 73 20 69 6e  USING clauses in
2a50e 20 74 68 65 20 73 61 6d 65 20 6a 6f 69 6e 0a 20   the same join. 
2a50f 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 52     */.    if( pR
2a510 69 67 68 74 2d 3e 70 4f 6e 20 26 26 20 70 52 69  ight->pOn && pRi
2a511 67 68 74 2d 3e 70 55 73 69 6e 67 20 29 7b 0a 20  ght->pUsing ){. 
2a512 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
2a513 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 61  rMsg(pParse, "ca
2a514 6e 6e 6f 74 20 68 61 76 65 20 62 6f 74 68 20 4f  nnot have both O
2a515 4e 20 61 6e 64 20 55 53 49 4e 47 20 22 0a 20 20  N and USING ".  
2a516 20 20 20 20 20 20 22 63 6c 61 75 73 65 73 20 69        "clauses i
2a517 6e 20 74 68 65 20 73 61 6d 65 20 6a 6f 69 6e 22  n the same join"
2a518 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
2a519 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  1;.    }..    /*
2a51a 20 41 64 64 20 74 68 65 20 4f 4e 20 63 6c 61 75   Add the ON clau
2a51b 73 65 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  se to the end of
2a51c 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2a51d 65 2c 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 0a  e, connected by.
2a51e 20 20 20 20 2a 2a 20 61 6e 20 41 4e 44 20 6f 70      ** an AND op
2a51f 65 72 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20  erator..    */. 
2a520 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 70     if( pRight->p
2a521 4f 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  On ){.      if( 
2a522 69 73 4f 75 74 65 72 20 29 20 73 65 74 4a 6f 69  isOuter ) setJoi
2a523 6e 45 78 70 72 28 70 52 69 67 68 74 2d 3e 70 4f  nExpr(pRight->pO
2a524 6e 2c 20 70 52 69 67 68 74 2d 3e 69 43 75 72 73  n, pRight->iCurs
2a525 6f 72 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 57  or);.      p->pW
2a526 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78  here = sqlite3Ex
2a527 70 72 41 6e 64 28 70 50 61 72 73 65 2d 3e 64 62  prAnd(pParse->db
2a528 2c 20 70 2d 3e 70 57 68 65 72 65 2c 20 70 52 69  , p->pWhere, pRi
2a529 67 68 74 2d 3e 70 4f 6e 29 3b 0a 20 20 20 20 20  ght->pOn);.     
2a52a 20 70 52 69 67 68 74 2d 3e 70 4f 6e 20 3d 20 30   pRight->pOn = 0
2a52b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
2a52c 43 72 65 61 74 65 20 65 78 74 72 61 20 74 65 72  Create extra ter
2a52d 6d 73 20 6f 6e 20 74 68 65 20 57 48 45 52 45 20  ms on the WHERE 
2a52e 63 6c 61 75 73 65 20 66 6f 72 20 65 61 63 68 20  clause for each 
2a52f 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 0a 20 20 20  column named.   
2a530 20 2a 2a 20 69 6e 20 74 68 65 20 55 53 49 4e 47   ** in the USING
2a531 20 63 6c 61 75 73 65 2e 20 20 45 78 61 6d 70 6c   clause.  Exampl
2a532 65 3a 20 49 66 20 74 68 65 20 74 77 6f 20 74 61  e: If the two ta
2a533 62 6c 65 73 20 74 6f 20 62 65 20 6a 6f 69 6e 65  bles to be joine
2a534 64 20 61 72 65 20 0a 20 20 20 20 2a 2a 20 41 20  d are .    ** A 
2a535 61 6e 64 20 42 20 61 6e 64 20 74 68 65 20 55 53  and B and the US
2a536 49 4e 47 20 63 6c 61 75 73 65 20 6e 61 6d 65 73  ING clause names
2a537 20 58 2c 20 59 2c 20 61 6e 64 20 5a 2c 20 74 68   X, Y, and Z, th
2a538 65 6e 20 61 64 64 20 74 68 69 73 0a 20 20 20 20  en add this.    
2a539 2a 2a 20 74 6f 20 74 68 65 20 57 48 45 52 45 20  ** to the WHERE 
2a53a 63 6c 61 75 73 65 3a 20 20 20 20 41 2e 58 3d 42  clause:    A.X=B
2a53b 2e 58 20 41 4e 44 20 41 2e 59 3d 42 2e 59 20 41  .X AND A.Y=B.Y A
2a53c 4e 44 20 41 2e 5a 3d 42 2e 5a 0a 20 20 20 20 2a  ND A.Z=B.Z.    *
2a53d 2a 20 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f  * Report an erro
2a53e 72 20 69 66 20 61 6e 79 20 63 6f 6c 75 6d 6e 20  r if any column 
2a53f 6d 65 6e 74 69 6f 6e 65 64 20 69 6e 20 74 68 65  mentioned in the
2a540 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 69 73   USING clause is
2a541 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 63 6f 6e 74  .    ** not cont
2a542 61 69 6e 65 64 20 69 6e 20 62 6f 74 68 20 74 61  ained in both ta
2a543 62 6c 65 73 20 74 6f 20 62 65 20 6a 6f 69 6e 65  bles to be joine
2a544 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  d..    */.    if
2a545 28 20 70 52 69 67 68 74 2d 3e 70 55 73 69 6e 67  ( pRight->pUsing
2a546 20 29 7b 0a 20 20 20 20 20 20 49 64 4c 69 73 74   ){.      IdList
2a547 20 2a 70 4c 69 73 74 20 3d 20 70 52 69 67 68 74   *pList = pRight
2a548 2d 3e 70 55 73 69 6e 67 3b 0a 20 20 20 20 20 20  ->pUsing;.      
2a549 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4c 69 73 74  for(j=0; j<pList
2a54a 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20  ->nId; j++){.   
2a54b 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65       char *zName
2a54c 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 7a   = pList->a[j].z
2a54d 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69 6e  Name;.        in
2a54e 74 20 69 4c 65 66 74 43 6f 6c 20 3d 20 63 6f 6c  t iLeftCol = col
2a54f 75 6d 6e 49 6e 64 65 78 28 70 4c 65 66 74 54 61  umnIndex(pLeftTa
2a550 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  b, zName);.     
2a551 20 20 20 69 6e 74 20 69 52 69 67 68 74 43 6f 6c     int iRightCol
2a552 20 3d 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70   = columnIndex(p
2a553 52 69 67 68 74 54 61 62 2c 20 7a 4e 61 6d 65 29  RightTab, zName)
2a554 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 4c  ;.        if( iL
2a555 65 66 74 43 6f 6c 3c 30 20 7c 7c 20 69 52 69 67  eftCol<0 || iRig
2a556 68 74 43 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20  htCol<0 ){.     
2a557 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
2a558 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 61  rMsg(pParse, "ca
2a559 6e 6e 6f 74 20 6a 6f 69 6e 20 75 73 69 6e 67 20  nnot join using 
2a55a 63 6f 6c 75 6d 6e 20 25 73 20 2d 20 63 6f 6c 75  column %s - colu
2a55b 6d 6e 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  mn ".           
2a55c 20 22 6e 6f 74 20 70 72 65 73 65 6e 74 20 69 6e   "not present in
2a55d 20 62 6f 74 68 20 74 61 62 6c 65 73 22 2c 20 7a   both tables", z
2a55e 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Name);.         
2a55f 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20   return 1;.     
2a560 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 64 64     }.        add
2a561 57 68 65 72 65 54 65 72 6d 28 70 50 61 72 73 65  WhereTerm(pParse
2a562 2c 20 70 53 72 63 2c 20 69 2c 20 69 4c 65 66 74  , pSrc, i, iLeft
2a563 43 6f 6c 2c 20 69 52 69 67 68 74 43 6f 6c 2c 20  Col, iRightCol, 
2a564 69 73 4f 75 74 65 72 2c 20 26 70 2d 3e 70 57 68  isOuter, &p->pWh
2a565 65 72 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ere);.      }.  
2a566 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
2a567 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73   0;.}../*.** Ins
2a568 65 72 74 20 63 6f 64 65 20 69 6e 74 6f 20 22 76  ert code into "v
2a569 22 20 74 68 61 74 20 77 69 6c 6c 20 70 75 73 68  " that will push
2a56a 20 74 68 65 20 72 65 63 6f 72 64 20 6f 6e 20 74   the record on t
2a56b 68 65 20 74 6f 70 20 6f 66 20 74 68 65 0a 2a 2a  he top of the.**
2a56c 20 73 74 61 63 6b 20 69 6e 74 6f 20 74 68 65 20   stack into the 
2a56d 73 6f 72 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  sorter..*/.stati
2a56e 63 20 76 6f 69 64 20 70 75 73 68 4f 6e 74 6f 53  c void pushOntoS
2a56f 6f 72 74 65 72 28 0a 20 20 50 61 72 73 65 20 2a  orter(.  Parse *
2a570 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2a571 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78  /* Parser contex
2a572 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  t */.  ExprList 
2a573 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 2f 2a  *pOrderBy,    /*
2a574 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   The ORDER BY cl
2a575 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74  ause */.  Select
2a576 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20   *pSelect,      
2a577 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20 53 45   /* The whole SE
2a578 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a  LECT statement *
2a579 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61 20  /.  int regData 
2a57a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
2a57b 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 64  gister holding d
2a57c 61 74 61 20 74 6f 20 62 65 20 73 6f 72 74 65 64  ata to be sorted
2a57d 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76   */.){.  Vdbe *v
2a57e 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
2a57f 3b 0a 20 20 69 6e 74 20 6e 45 78 70 72 20 3d 20  ;.  int nExpr = 
2a580 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b  pOrderBy->nExpr;
2a581 0a 20 20 69 6e 74 20 72 65 67 42 61 73 65 20 3d  .  int regBase =
2a582 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2a583 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 45 78  ange(pParse, nEx
2a584 70 72 2b 32 29 3b 0a 20 20 69 6e 74 20 72 65 67  pr+2);.  int reg
2a585 52 65 63 6f 72 64 20 3d 20 73 71 6c 69 74 65 33  Record = sqlite3
2a586 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
2a587 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  e);.  sqlite3Exp
2a588 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61 72  rCacheClear(pPar
2a589 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  se);.  sqlite3Ex
2a58a 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28 70  prCodeExprList(p
2a58b 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c  Parse, pOrderBy,
2a58c 20 72 65 67 42 61 73 65 2c 20 30 29 3b 0a 20 20   regBase, 0);.  
2a58d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a58e 32 28 76 2c 20 4f 50 5f 53 65 71 75 65 6e 63 65  2(v, OP_Sequence
2a58f 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75  , pOrderBy->iECu
2a590 72 73 6f 72 2c 20 72 65 67 42 61 73 65 2b 6e 45  rsor, regBase+nE
2a591 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  xpr);.  sqlite3E
2a592 78 70 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72  xprCodeMove(pPar
2a593 73 65 2c 20 72 65 67 44 61 74 61 2c 20 72 65 67  se, regData, reg
2a594 42 61 73 65 2b 6e 45 78 70 72 2b 31 2c 20 31 29  Base+nExpr+1, 1)
2a595 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2a596 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65  ddOp3(v, OP_Make
2a597 52 65 63 6f 72 64 2c 20 72 65 67 42 61 73 65 2c  Record, regBase,
2a598 20 6e 45 78 70 72 20 2b 20 32 2c 20 72 65 67 52   nExpr + 2, regR
2a599 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69 74 65  ecord);.  sqlite
2a59a 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a59b 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70 4f 72  P_IdxInsert, pOr
2a59c 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 2c  derBy->iECursor,
2a59d 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73   regRecord);.  s
2a59e 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
2a59f 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
2a5a0 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69 74  Record);.  sqlit
2a5a1 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e  e3ReleaseTempRan
2a5a2 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 42 61  ge(pParse, regBa
2a5a3 73 65 2c 20 6e 45 78 70 72 2b 32 29 3b 0a 20 20  se, nExpr+2);.  
2a5a4 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 69 4c 69  if( pSelect->iLi
2a5a5 6d 69 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 61  mit ){.    int a
2a5a6 64 64 72 31 2c 20 61 64 64 72 32 3b 0a 20 20 20  ddr1, addr2;.   
2a5a7 20 69 6e 74 20 69 4c 69 6d 69 74 3b 0a 20 20 20   int iLimit;.   
2a5a8 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 69 4f   if( pSelect->iO
2a5a9 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20 69  ffset ){.      i
2a5aa 4c 69 6d 69 74 20 3d 20 70 53 65 6c 65 63 74 2d  Limit = pSelect-
2a5ab 3e 69 4f 66 66 73 65 74 2b 31 3b 0a 20 20 20 20  >iOffset+1;.    
2a5ac 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 4c 69  }else{.      iLi
2a5ad 6d 69 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 69  mit = pSelect->i
2a5ae 4c 69 6d 69 74 3b 0a 20 20 20 20 7d 0a 20 20 20  Limit;.    }.   
2a5af 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33   addr1 = sqlite3
2a5b0 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2a5b1 5f 49 66 5a 65 72 6f 2c 20 69 4c 69 6d 69 74 29  _IfZero, iLimit)
2a5b2 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2a5b3 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64  eAddOp2(v, OP_Ad
2a5b4 64 49 6d 6d 2c 20 69 4c 69 6d 69 74 2c 20 2d 31  dImm, iLimit, -1
2a5b5 29 3b 0a 20 20 20 20 61 64 64 72 32 20 3d 20 73  );.    addr2 = s
2a5b6 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30  qlite3VdbeAddOp0
2a5b7 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20 20  (v, OP_Goto);.  
2a5b8 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2a5b9 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b  pHere(v, addr1);
2a5ba 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2a5bb 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4c 61 73  AddOp1(v, OP_Las
2a5bc 74 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43  t, pOrderBy->iEC
2a5bd 75 72 73 6f 72 29 3b 0a 20 20 20 20 73 71 6c 69  ursor);.    sqli
2a5be 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2a5bf 20 4f 50 5f 44 65 6c 65 74 65 2c 20 70 4f 72 64   OP_Delete, pOrd
2a5c0 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 29 3b  erBy->iECursor);
2a5c1 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2a5c2 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
2a5c3 32 29 3b 0a 20 20 20 20 70 53 65 6c 65 63 74 2d  2);.    pSelect-
2a5c4 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 7d  >iLimit = 0;.  }
2a5c5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 63 6f  .}../*.** Add co
2a5c6 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  de to implement 
2a5c7 74 68 65 20 4f 46 46 53 45 54 0a 2a 2f 0a 73 74  the OFFSET.*/.st
2a5c8 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 4f 66  atic void codeOf
2a5c9 66 73 65 74 28 0a 20 20 56 64 62 65 20 2a 76 2c  fset(.  Vdbe *v,
2a5ca 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e            /* Gen
2a5cb 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20  erate code into 
2a5cc 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 53 65 6c  this VM */.  Sel
2a5cd 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 2f  ect *p,        /
2a5ce 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
2a5cf 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64  tement being cod
2a5d0 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6e  ed */.  int iCon
2a5d1 74 69 6e 75 65 20 20 20 20 20 2f 2a 20 4a 75 6d  tinue     /* Jum
2a5d2 70 20 68 65 72 65 20 74 6f 20 73 6b 69 70 20 74  p here to skip t
2a5d3 68 65 20 63 75 72 72 65 6e 74 20 72 65 63 6f 72  he current recor
2a5d4 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 70 2d  d */.){.  if( p-
2a5d5 3e 69 4f 66 66 73 65 74 20 26 26 20 69 43 6f 6e  >iOffset && iCon
2a5d6 74 69 6e 75 65 21 3d 30 20 29 7b 0a 20 20 20 20  tinue!=0 ){.    
2a5d7 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 73 71  int addr;.    sq
2a5d8 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2a5d9 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 70 2d  v, OP_AddImm, p-
2a5da 3e 69 4f 66 66 73 65 74 2c 20 2d 31 29 3b 0a 20  >iOffset, -1);. 
2a5db 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
2a5dc 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2a5dd 50 5f 49 66 4e 65 67 2c 20 70 2d 3e 69 4f 66 66  P_IfNeg, p->iOff
2a5de 73 65 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  set);.    sqlite
2a5df 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2a5e0 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 43 6f 6e 74  P_Goto, 0, iCont
2a5e1 69 6e 75 65 29 3b 0a 20 20 20 20 56 64 62 65 43  inue);.    VdbeC
2a5e2 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 73 6b 69 70  omment((v, "skip
2a5e3 20 4f 46 46 53 45 54 20 72 65 63 6f 72 64 73 22   OFFSET records"
2a5e4 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ));.    sqlite3V
2a5e5 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
2a5e6 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ddr);.  }.}../*.
2a5e7 2a 2a 20 41 64 64 20 63 6f 64 65 20 74 68 61 74  ** Add code that
2a5e8 20 77 69 6c 6c 20 63 68 65 63 6b 20 74 6f 20 6d   will check to m
2a5e9 61 6b 65 20 73 75 72 65 20 74 68 65 20 4e 20 72  ake sure the N r
2a5ea 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e  egisters startin
2a5eb 67 20 61 74 20 69 4d 65 6d 0a 2a 2a 20 66 6f 72  g at iMem.** for
2a5ec 6d 20 61 20 64 69 73 74 69 6e 63 74 20 65 6e 74  m a distinct ent
2a5ed 72 79 2e 20 20 69 54 61 62 20 69 73 20 61 20 73  ry.  iTab is a s
2a5ee 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 74 68 61  orting index tha
2a5ef 74 20 68 6f 6c 64 73 20 70 72 65 76 69 6f 75 73  t holds previous
2a5f0 6c 79 0a 2a 2a 20 73 65 65 6e 20 63 6f 6d 62 69  ly.** seen combi
2a5f1 6e 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 4e  nations of the N
2a5f2 20 76 61 6c 75 65 73 2e 20 20 41 20 6e 65 77 20   values.  A new 
2a5f3 65 6e 74 72 79 20 69 73 20 6d 61 64 65 20 69 6e  entry is made in
2a5f4 20 69 54 61 62 0a 2a 2a 20 69 66 20 74 68 65 20   iTab.** if the 
2a5f5 63 75 72 72 65 6e 74 20 4e 20 76 61 6c 75 65 73  current N values
2a5f6 20 61 72 65 20 6e 65 77 2e 0a 2a 2a 0a 2a 2a 20   are new..**.** 
2a5f7 41 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 52 65  A jump to addrRe
2a5f8 70 65 61 74 20 69 73 20 6d 61 64 65 20 61 6e 64  peat is made and
2a5f9 20 74 68 65 20 4e 2b 31 20 76 61 6c 75 65 73 20   the N+1 values 
2a5fa 61 72 65 20 70 6f 70 70 65 64 20 66 72 6f 6d 20  are popped from 
2a5fb 74 68 65 0a 2a 2a 20 73 74 61 63 6b 20 69 66 20  the.** stack if 
2a5fc 74 68 65 20 74 6f 70 20 4e 20 65 6c 65 6d 65 6e  the top N elemen
2a5fd 74 73 20 61 72 65 20 6e 6f 74 20 64 69 73 74 69  ts are not disti
2a5fe 6e 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  nct..*/.static v
2a5ff 6f 69 64 20 63 6f 64 65 44 69 73 74 69 6e 63 74  oid codeDistinct
2a600 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2a601 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e  e,     /* Parsin
2a602 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72  g and code gener
2a603 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  ating context */
2a604 0a 20 20 69 6e 74 20 69 54 61 62 2c 20 20 20 20  .  int iTab,    
2a605 20 20 20 20 20 20 2f 2a 20 41 20 73 6f 72 74 69        /* A sorti
2a606 6e 67 20 69 6e 64 65 78 20 75 73 65 64 20 74 6f  ng index used to
2a607 20 74 65 73 74 20 66 6f 72 20 64 69 73 74 69 6e   test for distin
2a608 63 74 6e 65 73 73 20 2a 2f 0a 20 20 69 6e 74 20  ctness */.  int 
2a609 61 64 64 72 52 65 70 65 61 74 2c 20 20 20 20 2f  addrRepeat,    /
2a60a 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69  * Jump to here i
2a60b 66 20 6e 6f 74 20 64 69 73 74 69 6e 63 74 20 2a  f not distinct *
2a60c 2f 0a 20 20 69 6e 74 20 4e 2c 20 20 20 20 20 20  /.  int N,      
2a60d 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
2a60e 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a   of elements */.
2a60f 20 20 69 6e 74 20 69 4d 65 6d 20 20 20 20 20 20    int iMem      
2a610 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 65 6c       /* First el
2a611 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 56 64  ement */.){.  Vd
2a612 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 72 31 3b  be *v;.  int r1;
2a613 0a 0a 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e  ..  v = pParse->
2a614 70 56 64 62 65 3b 0a 20 20 72 31 20 3d 20 73 71  pVdbe;.  r1 = sq
2a615 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
2a616 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74  pParse);.  sqlit
2a617 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2a618 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 69  OP_MakeRecord, i
2a619 4d 65 6d 2c 20 4e 2c 20 72 31 29 3b 0a 20 20 73  Mem, N, r1);.  s
2a61a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2a61b 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 69 54  (v, OP_Found, iT
2a61c 61 62 2c 20 61 64 64 72 52 65 70 65 61 74 2c 20  ab, addrRepeat, 
2a61d 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  r1);.  sqlite3Vd
2a61e 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2a61f 64 78 49 6e 73 65 72 74 2c 20 69 54 61 62 2c 20  dxInsert, iTab, 
2a620 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65  r1);.  sqlite3Re
2a621 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2a622 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a  rse, r1);.}../*.
2a623 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20 65  ** Generate an e
2a624 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 68 65  rror message whe
2a625 6e 20 61 20 53 45 4c 45 43 54 20 69 73 20 75 73  n a SELECT is us
2a626 65 64 20 77 69 74 68 69 6e 20 61 20 73 75 62 65  ed within a sube
2a627 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 28 65 78  xpression.** (ex
2a628 61 6d 70 6c 65 3a 20 20 22 61 20 49 4e 20 28 53  ample:  "a IN (S
2a629 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 61 62  ELECT * FROM tab
2a62a 6c 65 29 22 29 20 62 75 74 20 69 74 20 68 61 73  le)") but it has
2a62b 20 6d 6f 72 65 20 74 68 61 6e 20 31 20 72 65 73   more than 1 res
2a62c 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 20 20  ult.** column.  
2a62d 57 65 20 64 6f 20 74 68 69 73 20 69 6e 20 61 20  We do this in a 
2a62e 73 75 62 72 6f 75 74 69 6e 65 20 62 65 63 61 75  subroutine becau
2a62f 73 65 20 74 68 65 20 65 72 72 6f 72 20 6f 63 63  se the error occ
2a630 75 72 73 20 69 6e 20 6d 75 6c 74 69 70 6c 65 0a  urs in multiple.
2a631 2a 2a 20 70 6c 61 63 65 73 2e 0a 2a 2f 0a 73 74  ** places..*/.st
2a632 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 46 6f  atic int checkFo
2a633 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c 65  rMultiColumnSele
2a634 63 74 45 72 72 6f 72 28 0a 20 20 50 61 72 73 65  ctError(.  Parse
2a635 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
2a636 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74  /* Parse context
2a637 2e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73  . */.  SelectDes
2a638 74 20 2a 70 44 65 73 74 2c 20 20 20 2f 2a 20 44  t *pDest,   /* D
2a639 65 73 74 69 6e 61 74 69 6f 6e 20 6f 66 20 53 45  estination of SE
2a63a 4c 45 43 54 20 72 65 73 75 6c 74 73 20 2a 2f 0a  LECT results */.
2a63b 20 20 69 6e 74 20 6e 45 78 70 72 20 20 20 20 20    int nExpr     
2a63c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
2a63d 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d   of result colum
2a63e 6e 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 53  ns returned by S
2a63f 45 4c 45 43 54 20 2a 2f 0a 29 7b 0a 20 20 69 6e  ELECT */.){.  in
2a640 74 20 65 44 65 73 74 20 3d 20 70 44 65 73 74 2d  t eDest = pDest-
2a641 3e 65 44 65 73 74 3b 0a 20 20 69 66 28 20 6e 45  >eDest;.  if( nE
2a642 78 70 72 3e 31 20 26 26 20 28 65 44 65 73 74 3d  xpr>1 && (eDest=
2a643 3d 53 52 54 5f 4d 65 6d 20 7c 7c 20 65 44 65 73  =SRT_Mem || eDes
2a644 74 3d 3d 53 52 54 5f 53 65 74 29 20 29 7b 0a 20  t==SRT_Set) ){. 
2a645 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2a646 73 67 28 70 50 61 72 73 65 2c 20 22 6f 6e 6c 79  sg(pParse, "only
2a647 20 61 20 73 69 6e 67 6c 65 20 72 65 73 75 6c 74   a single result
2a648 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20 22 0a 20   allowed for ". 
2a649 20 20 20 20 20 20 22 61 20 53 45 4c 45 43 54 20        "a SELECT 
2a64a 74 68 61 74 20 69 73 20 70 61 72 74 20 6f 66 20  that is part of 
2a64b 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 22 29 3b  an expression");
2a64c 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
2a64d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
2a64e 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  rn 0;.  }.}../*.
2a64f 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2a650 67 65 6e 65 72 61 74 65 73 20 74 68 65 20 63 6f  generates the co
2a651 64 65 20 66 6f 72 20 74 68 65 20 69 6e 73 69 64  de for the insid
2a652 65 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 20 6c  e of the inner l
2a653 6f 6f 70 0a 2a 2a 20 6f 66 20 61 20 53 45 4c 45  oop.** of a SELE
2a654 43 54 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 72 63  CT..**.** If src
2a655 54 61 62 20 61 6e 64 20 6e 43 6f 6c 75 6d 6e 20  Tab and nColumn 
2a656 61 72 65 20 62 6f 74 68 20 7a 65 72 6f 2c 20 74  are both zero, t
2a657 68 65 6e 20 74 68 65 20 70 45 4c 69 73 74 20 65  hen the pEList e
2a658 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 61 72  xpressions.** ar
2a659 65 20 65 76 61 6c 75 61 74 65 64 20 69 6e 20 6f  e evaluated in o
2a65a 72 64 65 72 20 74 6f 20 67 65 74 20 74 68 65 20  rder to get the 
2a65b 64 61 74 61 20 66 6f 72 20 74 68 69 73 20 72 6f  data for this ro
2a65c 77 2e 20 20 49 66 20 6e 43 6f 6c 75 6d 6e 3e 30  w.  If nColumn>0
2a65d 0a 2a 2a 20 74 68 65 6e 20 64 61 74 61 20 69 73  .** then data is
2a65e 20 70 75 6c 6c 65 64 20 66 72 6f 6d 20 73 72 63   pulled from src
2a65f 54 61 62 20 61 6e 64 20 70 45 4c 69 73 74 20 69  Tab and pEList i
2a660 73 20 75 73 65 64 20 6f 6e 6c 79 20 74 6f 20 67  s used only to g
2a661 65 74 20 74 68 65 0a 2a 2a 20 64 61 74 61 74 79  et the.** dataty
2a662 70 65 73 20 66 6f 72 20 65 61 63 68 20 63 6f 6c  pes for each col
2a663 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  umn..*/.static v
2a664 6f 69 64 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c  oid selectInnerL
2a665 6f 6f 70 28 0a 20 20 50 61 72 73 65 20 2a 70 50  oop(.  Parse *pP
2a666 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
2a667 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e  * The parser con
2a668 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74  text */.  Select
2a669 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
2a66a 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74    /* The complet
2a66b 65 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65  e select stateme
2a66c 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a  nt being coded *
2a66d 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
2a66e 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a 20 4c  List,       /* L
2a66f 69 73 74 20 6f 66 20 76 61 6c 75 65 73 20 62 65  ist of values be
2a670 69 6e 67 20 65 78 74 72 61 63 74 65 64 20 2a 2f  ing extracted */
2a671 0a 20 20 69 6e 74 20 73 72 63 54 61 62 2c 20 20  .  int srcTab,  
2a672 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 75             /* Pu
2a673 6c 6c 20 64 61 74 61 20 66 72 6f 6d 20 74 68 69  ll data from thi
2a674 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  s table */.  int
2a675 20 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20   nColumn,       
2a676 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2a677 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
2a678 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f   source table */
2a679 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72  .  ExprList *pOr
2a67a 64 65 72 42 79 2c 20 20 20 20 20 2f 2a 20 49 66  derBy,     /* If
2a67b 20 6e 6f 74 20 4e 55 4c 4c 2c 20 73 6f 72 74 20   not NULL, sort 
2a67c 72 65 73 75 6c 74 73 20 75 73 69 6e 67 20 74 68  results using th
2a67d 69 73 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20  is key */.  int 
2a67e 64 69 73 74 69 6e 63 74 2c 20 20 20 20 20 20 20  distinct,       
2a67f 20 20 20 20 2f 2a 20 49 66 20 3e 3d 30 2c 20 6d      /* If >=0, m
2a680 61 6b 65 20 73 75 72 65 20 72 65 73 75 6c 74 73  ake sure results
2a681 20 61 72 65 20 64 69 73 74 69 6e 63 74 20 2a 2f   are distinct */
2a682 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70  .  SelectDest *p
2a683 44 65 73 74 2c 20 20 20 20 20 20 2f 2a 20 48 6f  Dest,      /* Ho
2a684 77 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20  w to dispose of 
2a685 74 68 65 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20  the results */. 
2a686 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65 2c 20   int iContinue, 
2a687 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70           /* Jump
2a688 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75   here to continu
2a689 65 20 77 69 74 68 20 6e 65 78 74 20 72 6f 77 20  e with next row 
2a68a 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65 61 6b 20  */.  int iBreak 
2a68b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2a68c 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62 72 65  Jump here to bre
2a68d 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 69 6e  ak out of the in
2a68e 6e 65 72 20 6c 6f 6f 70 20 2a 2f 0a 29 7b 0a 20  ner loop */.){. 
2a68f 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
2a690 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20  e->pVdbe;.  int 
2a691 69 3b 0a 20 20 69 6e 74 20 68 61 73 44 69 73 74  i;.  int hasDist
2a692 69 6e 63 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  inct;        /* 
2a693 54 72 75 65 20 69 66 20 74 68 65 20 44 49 53 54  True if the DIST
2a694 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69 73 20  INCT keyword is 
2a695 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  present */.  int
2a696 20 72 65 67 52 65 73 75 6c 74 3b 20 20 20 20 20   regResult;     
2a697 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72           /* Star
2a698 74 20 6f 66 20 6d 65 6d 6f 72 79 20 68 6f 6c 64  t of memory hold
2a699 69 6e 67 20 72 65 73 75 6c 74 20 73 65 74 20 2a  ing result set *
2a69a 2f 0a 20 20 69 6e 74 20 65 44 65 73 74 20 3d 20  /.  int eDest = 
2a69b 70 44 65 73 74 2d 3e 65 44 65 73 74 3b 20 20 20  pDest->eDest;   
2a69c 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73  /* How to dispos
2a69d 65 20 6f 66 20 72 65 73 75 6c 74 73 20 2a 2f 0a  e of results */.
2a69e 20 20 69 6e 74 20 69 50 61 72 6d 20 3d 20 70 44    int iParm = pD
2a69f 65 73 74 2d 3e 69 50 61 72 6d 3b 20 20 20 2f 2a  est->iParm;   /*
2a6a0 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   First argument 
2a6a1 74 6f 20 64 69 73 70 6f 73 61 6c 20 6d 65 74 68  to disposal meth
2a6a2 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 73  od */.  int nRes
2a6a3 75 6c 74 43 6f 6c 3b 20 20 20 20 20 20 20 20 20  ultCol;         
2a6a4 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
2a6a5 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20   result columns 
2a6a6 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 76 20  */..  assert( v 
2a6a7 29 3b 0a 20 20 69 66 28 20 4e 45 56 45 52 28 76  );.  if( NEVER(v
2a6a8 3d 3d 30 29 20 29 20 72 65 74 75 72 6e 3b 0a 20  ==0) ) return;. 
2a6a9 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21   assert( pEList!
2a6aa 3d 30 20 29 3b 0a 20 20 68 61 73 44 69 73 74 69  =0 );.  hasDisti
2a6ab 6e 63 74 20 3d 20 64 69 73 74 69 6e 63 74 3e 3d  nct = distinct>=
2a6ac 30 3b 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42  0;.  if( pOrderB
2a6ad 79 3d 3d 30 20 26 26 20 21 68 61 73 44 69 73 74  y==0 && !hasDist
2a6ae 69 6e 63 74 20 29 7b 0a 20 20 20 20 63 6f 64 65  inct ){.    code
2a6af 4f 66 66 73 65 74 28 76 2c 20 70 2c 20 69 43 6f  Offset(v, p, iCo
2a6b0 6e 74 69 6e 75 65 29 3b 0a 20 20 7d 0a 0a 20 20  ntinue);.  }..  
2a6b1 2f 2a 20 50 75 6c 6c 20 74 68 65 20 72 65 71 75  /* Pull the requ
2a6b2 65 73 74 65 64 20 63 6f 6c 75 6d 6e 73 2e 0a 20  ested columns.. 
2a6b3 20 2a 2f 0a 20 20 69 66 28 20 6e 43 6f 6c 75 6d   */.  if( nColum
2a6b4 6e 3e 30 20 29 7b 0a 20 20 20 20 6e 52 65 73 75  n>0 ){.    nResu
2a6b5 6c 74 43 6f 6c 20 3d 20 6e 43 6f 6c 75 6d 6e 3b  ltCol = nColumn;
2a6b6 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 52  .  }else{.    nR
2a6b7 65 73 75 6c 74 43 6f 6c 20 3d 20 70 45 4c 69 73  esultCol = pELis
2a6b8 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 7d 0a 20 20  t->nExpr;.  }.  
2a6b9 69 66 28 20 70 44 65 73 74 2d 3e 69 4d 65 6d 3d  if( pDest->iMem=
2a6ba 3d 30 20 29 7b 0a 20 20 20 20 70 44 65 73 74 2d  =0 ){.    pDest-
2a6bb 3e 69 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e  >iMem = pParse->
2a6bc 6e 4d 65 6d 2b 31 3b 0a 20 20 20 20 70 44 65 73  nMem+1;.    pDes
2a6bd 74 2d 3e 6e 4d 65 6d 20 3d 20 6e 52 65 73 75 6c  t->nMem = nResul
2a6be 74 43 6f 6c 3b 0a 20 20 20 20 70 50 61 72 73 65  tCol;.    pParse
2a6bf 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 52 65 73 75 6c  ->nMem += nResul
2a6c0 74 43 6f 6c 3b 0a 20 20 7d 65 6c 73 65 7b 20 0a  tCol;.  }else{ .
2a6c1 20 20 20 20 61 73 73 65 72 74 28 20 70 44 65 73      assert( pDes
2a6c2 74 2d 3e 6e 4d 65 6d 3d 3d 6e 52 65 73 75 6c 74  t->nMem==nResult
2a6c3 43 6f 6c 20 29 3b 0a 20 20 7d 0a 20 20 72 65 67  Col );.  }.  reg
2a6c4 52 65 73 75 6c 74 20 3d 20 70 44 65 73 74 2d 3e  Result = pDest->
2a6c5 69 4d 65 6d 3b 0a 20 20 69 66 28 20 6e 43 6f 6c  iMem;.  if( nCol
2a6c6 75 6d 6e 3e 30 20 29 7b 0a 20 20 20 20 66 6f 72  umn>0 ){.    for
2a6c7 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b  (i=0; i<nColumn;
2a6c8 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c   i++){.      sql
2a6c9 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2a6ca 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63  , OP_Column, src
2a6cb 54 61 62 2c 20 69 2c 20 72 65 67 52 65 73 75 6c  Tab, i, regResul
2a6cc 74 2b 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  t+i);.    }.  }e
2a6cd 6c 73 65 20 69 66 28 20 65 44 65 73 74 21 3d 53  lse if( eDest!=S
2a6ce 52 54 5f 45 78 69 73 74 73 20 29 7b 0a 20 20 20  RT_Exists ){.   
2a6cf 20 2f 2a 20 49 66 20 74 68 65 20 64 65 73 74 69   /* If the desti
2a6d0 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20 45 58 49  nation is an EXI
2a6d1 53 54 53 28 2e 2e 2e 29 20 65 78 70 72 65 73 73  STS(...) express
2a6d2 69 6f 6e 2c 20 74 68 65 20 61 63 74 75 61 6c 0a  ion, the actual.
2a6d3 20 20 20 20 2a 2a 20 76 61 6c 75 65 73 20 72 65      ** values re
2a6d4 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 53 45  turned by the SE
2a6d5 4c 45 43 54 20 61 72 65 20 6e 6f 74 20 72 65 71  LECT are not req
2a6d6 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  uired..    */.  
2a6d7 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2a6d8 68 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b  heClear(pParse);
2a6d9 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2a6da 43 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50 61  CodeExprList(pPa
2a6db 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 72 65 67  rse, pEList, reg
2a6dc 52 65 73 75 6c 74 2c 20 65 44 65 73 74 3d 3d 53  Result, eDest==S
2a6dd 52 54 5f 4f 75 74 70 75 74 29 3b 0a 20 20 7d 0a  RT_Output);.  }.
2a6de 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 52 65 73    nColumn = nRes
2a6df 75 6c 74 43 6f 6c 3b 0a 0a 20 20 2f 2a 20 49 66  ultCol;..  /* If
2a6e0 20 74 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65   the DISTINCT ke
2a6e1 79 77 6f 72 64 20 77 61 73 20 70 72 65 73 65 6e  yword was presen
2a6e2 74 20 6f 6e 20 74 68 65 20 53 45 4c 45 43 54 20  t on the SELECT 
2a6e3 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 61  statement.  ** a
2a6e4 6e 64 20 74 68 69 73 20 72 6f 77 20 68 61 73 20  nd this row has 
2a6e5 62 65 65 6e 20 73 65 65 6e 20 62 65 66 6f 72 65  been seen before
2a6e6 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6d 61  , then do not ma
2a6e7 6b 65 20 74 68 69 73 20 72 6f 77 0a 20 20 2a 2a  ke this row.  **
2a6e8 20 70 61 72 74 20 6f 66 20 74 68 65 20 72 65 73   part of the res
2a6e9 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ult..  */.  if( 
2a6ea 68 61 73 44 69 73 74 69 6e 63 74 20 29 7b 0a 20  hasDistinct ){. 
2a6eb 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73     assert( pELis
2a6ec 74 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  t!=0 );.    asse
2a6ed 72 74 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70  rt( pEList->nExp
2a6ee 72 3d 3d 6e 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20  r==nColumn );.  
2a6ef 20 20 63 6f 64 65 44 69 73 74 69 6e 63 74 28 70    codeDistinct(p
2a6f0 50 61 72 73 65 2c 20 64 69 73 74 69 6e 63 74 2c  Parse, distinct,
2a6f1 20 69 43 6f 6e 74 69 6e 75 65 2c 20 6e 43 6f 6c   iContinue, nCol
2a6f2 75 6d 6e 2c 20 72 65 67 52 65 73 75 6c 74 29 3b  umn, regResult);
2a6f3 0a 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42  .    if( pOrderB
2a6f4 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6f  y==0 ){.      co
2a6f5 64 65 4f 66 66 73 65 74 28 76 2c 20 70 2c 20 69  deOffset(v, p, i
2a6f6 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20 20 20 7d  Continue);.    }
2a6f7 0a 20 20 7d 0a 0a 20 20 69 66 28 20 63 68 65 63  .  }..  if( chec
2a6f8 6b 46 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53  kForMultiColumnS
2a6f9 65 6c 65 63 74 45 72 72 6f 72 28 70 50 61 72 73  electError(pPars
2a6fa 65 2c 20 70 44 65 73 74 2c 20 70 45 4c 69 73 74  e, pDest, pEList
2a6fb 2d 3e 6e 45 78 70 72 29 20 29 7b 0a 20 20 20 20  ->nExpr) ){.    
2a6fc 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 73  return;.  }..  s
2a6fd 77 69 74 63 68 28 20 65 44 65 73 74 20 29 7b 0a  witch( eDest ){.
2a6fe 20 20 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 6d      /* In this m
2a6ff 6f 64 65 2c 20 77 72 69 74 65 20 65 61 63 68 20  ode, write each 
2a700 71 75 65 72 79 20 72 65 73 75 6c 74 20 74 6f 20  query result to 
2a701 74 68 65 20 6b 65 79 20 6f 66 20 74 68 65 20 74  the key of the t
2a702 65 6d 70 6f 72 61 72 79 0a 20 20 20 20 2a 2a 20  emporary.    ** 
2a703 74 61 62 6c 65 20 69 50 61 72 6d 2e 0a 20 20 20  table iParm..   
2a704 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
2a705 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44  TE_OMIT_COMPOUND
2a706 5f 53 45 4c 45 43 54 0a 20 20 20 20 63 61 73 65  _SELECT.    case
2a707 20 53 52 54 5f 55 6e 69 6f 6e 3a 20 7b 0a 20 20   SRT_Union: {.  
2a708 20 20 20 20 69 6e 74 20 72 31 3b 0a 20 20 20 20      int r1;.    
2a709 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65    r1 = sqlite3Ge
2a70a 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
2a70b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2a70c 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2a70d 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 52  MakeRecord, regR
2a70e 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20  esult, nColumn, 
2a70f 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  r1);.      sqlit
2a710 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2a711 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 50  OP_IdxInsert, iP
2a712 61 72 6d 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  arm, r1);.      
2a713 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2a714 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
2a715 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
2a716 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f      }..    /* Co
2a717 6e 73 74 72 75 63 74 20 61 20 72 65 63 6f 72 64  nstruct a record
2a718 20 66 72 6f 6d 20 74 68 65 20 71 75 65 72 79 20   from the query 
2a719 72 65 73 75 6c 74 2c 20 62 75 74 20 69 6e 73 74  result, but inst
2a71a 65 61 64 20 6f 66 0a 20 20 20 20 2a 2a 20 73 61  ead of.    ** sa
2a71b 76 69 6e 67 20 74 68 61 74 20 72 65 63 6f 72 64  ving that record
2a71c 2c 20 75 73 65 20 69 74 20 61 73 20 61 20 6b 65  , use it as a ke
2a71d 79 20 74 6f 20 64 65 6c 65 74 65 20 65 6c 65 6d  y to delete elem
2a71e 65 6e 74 73 20 66 72 6f 6d 0a 20 20 20 20 2a 2a  ents from.    **
2a71f 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74   the temporary t
2a720 61 62 6c 65 20 69 50 61 72 6d 2e 0a 20 20 20 20  able iParm..    
2a721 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  */.    case SRT_
2a722 45 78 63 65 70 74 3a 20 7b 0a 20 20 20 20 20 20  Except: {.      
2a723 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a724 33 28 76 2c 20 4f 50 5f 49 64 78 44 65 6c 65 74  3(v, OP_IdxDelet
2a725 65 2c 20 69 50 61 72 6d 2c 20 72 65 67 52 65 73  e, iParm, regRes
2a726 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20  ult, nColumn);. 
2a727 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2a728 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a  }.#endif..    /*
2a729 20 53 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   Store the resul
2a72a 74 20 61 73 20 64 61 74 61 20 75 73 69 6e 67 20  t as data using 
2a72b 61 20 75 6e 69 71 75 65 20 6b 65 79 2e 0a 20 20  a unique key..  
2a72c 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52    */.    case SR
2a72d 54 5f 54 61 62 6c 65 3a 0a 20 20 20 20 63 61 73  T_Table:.    cas
2a72e 65 20 53 52 54 5f 45 70 68 65 6d 54 61 62 3a 20  e SRT_EphemTab: 
2a72f 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d  {.      int r1 =
2a730 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2a731 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
2a732 20 20 74 65 73 74 63 61 73 65 28 20 65 44 65 73    testcase( eDes
2a733 74 3d 3d 53 52 54 5f 54 61 62 6c 65 20 29 3b 0a  t==SRT_Table );.
2a734 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2a735 65 44 65 73 74 3d 3d 53 52 54 5f 45 70 68 65 6d  eDest==SRT_Ephem
2a736 54 61 62 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  Tab );.      sql
2a737 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2a738 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  , OP_MakeRecord,
2a739 20 72 65 67 52 65 73 75 6c 74 2c 20 6e 43 6f 6c   regResult, nCol
2a73a 75 6d 6e 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  umn, r1);.      
2a73b 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a  if( pOrderBy ){.
2a73c 20 20 20 20 20 20 20 20 70 75 73 68 4f 6e 74 6f          pushOnto
2a73d 53 6f 72 74 65 72 28 70 50 61 72 73 65 2c 20 70  Sorter(pParse, p
2a73e 4f 72 64 65 72 42 79 2c 20 70 2c 20 72 31 29 3b  OrderBy, p, r1);
2a73f 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2a740 20 20 20 20 20 20 69 6e 74 20 72 32 20 3d 20 73        int r2 = s
2a741 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2a742 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
2a743 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2a744 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77  Op2(v, OP_NewRow
2a745 69 64 2c 20 69 50 61 72 6d 2c 20 72 32 29 3b 0a  id, iParm, r2);.
2a746 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2a747 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2a748 49 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c 20 72  Insert, iParm, r
2a749 31 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 20 20  1, r2);.        
2a74a 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2a74b 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50  eP5(v, OPFLAG_AP
2a74c 50 45 4e 44 29 3b 0a 20 20 20 20 20 20 20 20 73  PEND);.        s
2a74d 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
2a74e 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 32 29  pReg(pParse, r2)
2a74f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2a750 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2a751 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
2a752 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
2a753 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53      }..#ifndef S
2a754 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
2a755 45 52 59 0a 20 20 20 20 2f 2a 20 49 66 20 77 65  ERY.    /* If we
2a756 20 61 72 65 20 63 72 65 61 74 69 6e 67 20 61 20   are creating a 
2a757 73 65 74 20 66 6f 72 20 61 6e 20 22 65 78 70 72  set for an "expr
2a758 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29   IN (SELECT ...)
2a759 22 20 63 6f 6e 73 74 72 75 63 74 2c 0a 20 20 20  " construct,.   
2a75a 20 2a 2a 20 74 68 65 6e 20 74 68 65 72 65 20 73   ** then there s
2a75b 68 6f 75 6c 64 20 62 65 20 61 20 73 69 6e 67 6c  hould be a singl
2a75c 65 20 69 74 65 6d 20 6f 6e 20 74 68 65 20 73 74  e item on the st
2a75d 61 63 6b 2e 20 20 57 72 69 74 65 20 74 68 69 73  ack.  Write this
2a75e 0a 20 20 20 20 2a 2a 20 69 74 65 6d 20 69 6e 74  .    ** item int
2a75f 6f 20 74 68 65 20 73 65 74 20 74 61 62 6c 65 20  o the set table 
2a760 77 69 74 68 20 62 6f 67 75 73 20 64 61 74 61 2e  with bogus data.
2a761 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
2a762 20 53 52 54 5f 53 65 74 3a 20 7b 0a 20 20 20 20   SRT_Set: {.    
2a763 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 75 6d    assert( nColum
2a764 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 70 2d  n==1 );.      p-
2a765 3e 61 66 66 69 6e 69 74 79 20 3d 20 73 71 6c 69  >affinity = sqli
2a766 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69  te3CompareAffini
2a767 74 79 28 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e  ty(pEList->a[0].
2a768 70 45 78 70 72 2c 20 70 44 65 73 74 2d 3e 61 66  pExpr, pDest->af
2a769 66 69 6e 69 74 79 29 3b 0a 20 20 20 20 20 20 69  finity);.      i
2a76a 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  f( pOrderBy ){. 
2a76b 20 20 20 20 20 20 20 2f 2a 20 41 74 20 66 69 72         /* At fir
2a76c 73 74 20 67 6c 61 6e 63 65 20 79 6f 75 20 77 6f  st glance you wo
2a76d 75 6c 64 20 74 68 69 6e 6b 20 77 65 20 63 6f 75  uld think we cou
2a76e 6c 64 20 6f 70 74 69 6d 69 7a 65 20 6f 75 74 20  ld optimize out 
2a76f 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f  the.        ** O
2a770 52 44 45 52 20 42 59 20 69 6e 20 74 68 69 73 20  RDER BY in this 
2a771 63 61 73 65 20 73 69 6e 63 65 20 74 68 65 20 6f  case since the o
2a772 72 64 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  rder of entries 
2a773 69 6e 20 74 68 65 20 73 65 74 0a 20 20 20 20 20  in the set.     
2a774 20 20 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 6d     ** does not m
2a775 61 74 74 65 72 2e 20 20 42 75 74 20 74 68 65 72  atter.  But ther
2a776 65 20 6d 69 67 68 74 20 62 65 20 61 20 4c 49 4d  e might be a LIM
2a777 49 54 20 63 6c 61 75 73 65 2c 20 69 6e 20 77 68  IT clause, in wh
2a778 69 63 68 0a 20 20 20 20 20 20 20 20 2a 2a 20 63  ich.        ** c
2a779 61 73 65 20 74 68 65 20 6f 72 64 65 72 20 64 6f  ase the order do
2a77a 65 73 20 6d 61 74 74 65 72 20 2a 2f 0a 20 20 20  es matter */.   
2a77b 20 20 20 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72       pushOntoSor
2a77c 74 65 72 28 70 50 61 72 73 65 2c 20 70 4f 72 64  ter(pParse, pOrd
2a77d 65 72 42 79 2c 20 70 2c 20 72 65 67 52 65 73 75  erBy, p, regResu
2a77e 6c 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  lt);.      }else
2a77f 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 31  {.        int r1
2a780 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2a781 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
2a782 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2a783 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4d 61  eAddOp4(v, OP_Ma
2a784 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 52 65 73  keRecord, regRes
2a785 75 6c 74 2c 20 31 2c 20 72 31 2c 20 26 70 2d 3e  ult, 1, r1, &p->
2a786 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20 20  affinity, 1);.  
2a787 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2a788 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
2a789 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67  ange(pParse, reg
2a78a 52 65 73 75 6c 74 2c 20 31 29 3b 0a 20 20 20 20  Result, 1);.    
2a78b 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2a78c 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49  ddOp2(v, OP_IdxI
2a78d 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c 20 72 31  nsert, iParm, r1
2a78e 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2a78f 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
2a790 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20  (pParse, r1);.  
2a791 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61      }.      brea
2a792 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
2a793 20 49 66 20 61 6e 79 20 72 6f 77 20 65 78 69 73   If any row exis
2a794 74 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  t in the result 
2a795 73 65 74 2c 20 72 65 63 6f 72 64 20 74 68 61 74  set, record that
2a796 20 66 61 63 74 20 61 6e 64 20 61 62 6f 72 74 2e   fact and abort.
2a797 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
2a798 20 53 52 54 5f 45 78 69 73 74 73 3a 20 7b 0a 20   SRT_Exists: {. 
2a799 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2a79a 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
2a79b 65 67 65 72 2c 20 31 2c 20 69 50 61 72 6d 29 3b  eger, 1, iParm);
2a79c 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 49  .      /* The LI
2a79d 4d 49 54 20 63 6c 61 75 73 65 20 77 69 6c 6c 20  MIT clause will 
2a79e 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f  terminate the lo
2a79f 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20 20  op for us */.   
2a7a0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2a7a1 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20  .    /* If this 
2a7a2 69 73 20 61 20 73 63 61 6c 61 72 20 73 65 6c 65  is a scalar sele
2a7a3 63 74 20 74 68 61 74 20 69 73 20 70 61 72 74 20  ct that is part 
2a7a4 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  of an expression
2a7a5 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 73 74  , then.    ** st
2a7a6 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 20  ore the results 
2a7a7 69 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  in the appropria
2a7a8 74 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61  te memory cell a
2a7a9 6e 64 20 62 72 65 61 6b 20 6f 75 74 0a 20 20 20  nd break out.   
2a7aa 20 2a 2a 20 6f 66 20 74 68 65 20 73 63 61 6e 20   ** of the scan 
2a7ab 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  loop..    */.   
2a7ac 20 63 61 73 65 20 53 52 54 5f 4d 65 6d 3a 20 7b   case SRT_Mem: {
2a7ad 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e  .      assert( n
2a7ae 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20  Column==1 );.   
2a7af 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20     if( pOrderBy 
2a7b0 29 7b 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f  ){.        pushO
2a7b1 6e 74 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65  ntoSorter(pParse
2a7b2 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 2c 20 72  , pOrderBy, p, r
2a7b3 65 67 52 65 73 75 6c 74 29 3b 0a 20 20 20 20 20  egResult);.     
2a7b4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2a7b5 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 4d  sqlite3ExprCodeM
2a7b6 6f 76 65 28 70 50 61 72 73 65 2c 20 72 65 67 52  ove(pParse, regR
2a7b7 65 73 75 6c 74 2c 20 69 50 61 72 6d 2c 20 31 29  esult, iParm, 1)
2a7b8 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ;.        /* The
2a7b9 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 77 69   LIMIT clause wi
2a7ba 6c 6c 20 6a 75 6d 70 20 6f 75 74 20 6f 66 20 74  ll jump out of t
2a7bb 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a  he loop for us *
2a7bc 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  /.      }.      
2a7bd 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e  break;.    }.#en
2a7be 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66 20 53  dif /* #ifndef S
2a7bf 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55  QLITE_OMIT_SUBQU
2a7c0 45 52 59 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 53  ERY */..    /* S
2a7c1 65 6e 64 20 74 68 65 20 64 61 74 61 20 74 6f 20  end the data to 
2a7c2 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  the callback fun
2a7c3 63 74 69 6f 6e 20 6f 72 20 74 6f 20 61 20 73 75  ction or to a su
2a7c4 62 72 6f 75 74 69 6e 65 2e 20 20 49 6e 20 74 68  broutine.  In th
2a7c5 65 0a 20 20 20 20 2a 2a 20 63 61 73 65 20 6f 66  e.    ** case of
2a7c6 20 61 20 73 75 62 72 6f 75 74 69 6e 65 2c 20 74   a subroutine, t
2a7c7 68 65 20 73 75 62 72 6f 75 74 69 6e 65 20 69 74  he subroutine it
2a7c8 73 65 6c 66 20 69 73 20 72 65 73 70 6f 6e 73 69  self is responsi
2a7c9 62 6c 65 20 66 6f 72 0a 20 20 20 20 2a 2a 20 70  ble for.    ** p
2a7ca 6f 70 70 69 6e 67 20 74 68 65 20 64 61 74 61 20  opping the data 
2a7cb 66 72 6f 6d 20 74 68 65 20 73 74 61 63 6b 2e 0a  from the stack..
2a7cc 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
2a7cd 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 3a 0a 20  SRT_Coroutine:. 
2a7ce 20 20 20 63 61 73 65 20 53 52 54 5f 4f 75 74 70     case SRT_Outp
2a7cf 75 74 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  ut: {.      test
2a7d0 63 61 73 65 28 20 65 44 65 73 74 3d 3d 53 52 54  case( eDest==SRT
2a7d1 5f 43 6f 72 6f 75 74 69 6e 65 20 29 3b 0a 20 20  _Coroutine );.  
2a7d2 20 20 20 20 74 65 73 74 63 61 73 65 28 20 65 44      testcase( eD
2a7d3 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20  est==SRT_Output 
2a7d4 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 72  );.      if( pOr
2a7d5 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 20  derBy ){.       
2a7d6 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65   int r1 = sqlite
2a7d7 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
2a7d8 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  se);.        sql
2a7d9 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2a7da 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  , OP_MakeRecord,
2a7db 20 72 65 67 52 65 73 75 6c 74 2c 20 6e 43 6f 6c   regResult, nCol
2a7dc 75 6d 6e 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  umn, r1);.      
2a7dd 20 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65 72    pushOntoSorter
2a7de 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42  (pParse, pOrderB
2a7df 79 2c 20 70 2c 20 72 31 29 3b 0a 20 20 20 20 20  y, p, r1);.     
2a7e0 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
2a7e1 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2a7e2 20 72 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73   r1);.      }els
2a7e3 65 20 69 66 28 20 65 44 65 73 74 3d 3d 53 52 54  e if( eDest==SRT
2a7e4 5f 43 6f 72 6f 75 74 69 6e 65 20 29 7b 0a 20 20  _Coroutine ){.  
2a7e5 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2a7e6 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69  eAddOp1(v, OP_Yi
2a7e7 65 6c 64 2c 20 70 44 65 73 74 2d 3e 69 50 61 72  eld, pDest->iPar
2a7e8 6d 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  m);.      }else{
2a7e9 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2a7ea 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2a7eb 5f 52 65 73 75 6c 74 52 6f 77 2c 20 72 65 67 52  _ResultRow, regR
2a7ec 65 73 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b  esult, nColumn);
2a7ed 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2a7ee 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
2a7ef 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
2a7f0 72 65 67 52 65 73 75 6c 74 2c 20 6e 43 6f 6c 75  regResult, nColu
2a7f1 6d 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  mn);.      }.   
2a7f2 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2a7f3 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
2a7f4 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
2a7f5 52 29 0a 20 20 20 20 2f 2a 20 44 69 73 63 61 72  R).    /* Discar
2a7f6 64 20 74 68 65 20 72 65 73 75 6c 74 73 2e 20 20  d the results.  
2a7f7 54 68 69 73 20 69 73 20 75 73 65 64 20 66 6f 72  This is used for
2a7f8 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2a7f9 74 73 20 69 6e 73 69 64 65 0a 20 20 20 20 2a 2a  ts inside.    **
2a7fa 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 54   the body of a T
2a7fb 52 49 47 47 45 52 2e 20 20 54 68 65 20 70 75 72  RIGGER.  The pur
2a7fc 70 6f 73 65 20 6f 66 20 73 75 63 68 20 73 65 6c  pose of such sel
2a7fd 65 63 74 73 20 69 73 20 74 6f 20 63 61 6c 6c 0a  ects is to call.
2a7fe 20 20 20 20 2a 2a 20 75 73 65 72 2d 64 65 66 69      ** user-defi
2a7ff 6e 65 64 20 66 75 6e 63 74 69 6f 6e 73 20 74 68  ned functions th
2a800 61 74 20 68 61 76 65 20 73 69 64 65 20 65 66 66  at have side eff
2a801 65 63 74 73 2e 20 20 57 65 20 64 6f 20 6e 6f 74  ects.  We do not
2a802 20 63 61 72 65 0a 20 20 20 20 2a 2a 20 61 62 6f   care.    ** abo
2a803 75 74 20 74 68 65 20 61 63 74 75 61 6c 20 72 65  ut the actual re
2a804 73 75 6c 74 73 20 6f 66 20 74 68 65 20 73 65 6c  sults of the sel
2a805 65 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ect..    */.    
2a806 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
2a807 20 61 73 73 65 72 74 28 20 65 44 65 73 74 3d 3d   assert( eDest==
2a808 53 52 54 5f 44 69 73 63 61 72 64 20 29 3b 0a 20  SRT_Discard );. 
2a809 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2a80a 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20  }.#endif.  }..  
2a80b 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 65  /* Jump to the e
2a80c 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69  nd of the loop i
2a80d 66 20 74 68 65 20 4c 49 4d 49 54 20 69 73 20 72  f the LIMIT is r
2a80e 65 61 63 68 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  eached..  */.  i
2a80f 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20 29 7b 0a  f( p->iLimit ){.
2a810 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 72 64      assert( pOrd
2a811 65 72 42 79 3d 3d 30 20 29 3b 20 20 2f 2a 20 49  erBy==0 );  /* I
2a812 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f 52  f there is an OR
2a813 44 45 52 20 42 59 2c 20 74 68 65 20 63 61 6c 6c  DER BY, the call
2a814 20 74 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20   to.            
2a815 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a816 2a 2a 20 70 75 73 68 4f 6e 74 6f 53 6f 72 74 65  ** pushOntoSorte
2a817 72 28 29 20 77 6f 75 6c 64 20 68 61 76 65 20 63  r() would have c
2a818 6c 65 61 72 65 64 20 70 2d 3e 69 4c 69 6d 69 74  leared p->iLimit
2a819 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
2a81a 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2a81b 41 64 64 49 6d 6d 2c 20 70 2d 3e 69 4c 69 6d 69  AddImm, p->iLimi
2a81c 74 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69  t, -1);.    sqli
2a81d 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2a81e 20 4f 50 5f 49 66 5a 65 72 6f 2c 20 70 2d 3e 69   OP_IfZero, p->i
2a81f 4c 69 6d 69 74 2c 20 69 42 72 65 61 6b 29 3b 0a  Limit, iBreak);.
2a820 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76    }.}../*.** Giv
2a821 65 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  en an expression
2a822 20 6c 69 73 74 2c 20 67 65 6e 65 72 61 74 65 20   list, generate 
2a823 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74  a KeyInfo struct
2a824 75 72 65 20 74 68 61 74 20 72 65 63 6f 72 64 73  ure that records
2a825 0a 2a 2a 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e  .** the collatin
2a826 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 65  g sequence for e
2a827 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20 69  ach expression i
2a828 6e 20 74 68 61 74 20 65 78 70 72 65 73 73 69 6f  n that expressio
2a829 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66  n list..**.** If
2a82a 20 74 68 65 20 45 78 70 72 4c 69 73 74 20 69 73   the ExprList is
2a82b 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20   an ORDER BY or 
2a82c 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20  GROUP BY clause 
2a82d 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 69  then the resulti
2a82e 6e 67 0a 2a 2a 20 4b 65 79 49 6e 66 6f 20 73 74  ng.** KeyInfo st
2a82f 72 75 63 74 75 72 65 20 69 73 20 61 70 70 72 6f  ructure is appro
2a830 70 72 69 61 74 65 20 66 6f 72 20 69 6e 69 74 69  priate for initi
2a831 61 6c 69 7a 69 6e 67 20 61 20 76 69 72 74 75 61  alizing a virtua
2a832 6c 20 69 6e 64 65 78 20 74 6f 0a 2a 2a 20 69 6d  l index to.** im
2a833 70 6c 65 6d 65 6e 74 20 74 68 61 74 20 63 6c 61  plement that cla
2a834 75 73 65 2e 20 20 49 66 20 74 68 65 20 45 78 70  use.  If the Exp
2a835 72 4c 69 73 74 20 69 73 20 74 68 65 20 72 65 73  rList is the res
2a836 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53 45 4c  ult set of a SEL
2a837 45 43 54 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20  ECT.** then the 
2a838 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
2a839 65 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65  e is appropriate
2a83a 20 66 6f 72 20 69 6e 69 74 69 61 6c 69 7a 69 6e   for initializin
2a83b 67 20 61 20 76 69 72 74 75 61 6c 0a 2a 2a 20 69  g a virtual.** i
2a83c 6e 64 65 78 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  ndex to implemen
2a83d 74 20 61 20 44 49 53 54 49 4e 43 54 20 74 65 73  t a DISTINCT tes
2a83e 74 2e 0a 2a 2a 0a 2a 2a 20 53 70 61 63 65 20 74  t..**.** Space t
2a83f 6f 20 68 6f 6c 64 20 74 68 65 20 4b 65 79 49 6e  o hold the KeyIn
2a840 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 73 20  fo structure is 
2a841 6f 62 74 61 69 6e 20 66 72 6f 6d 20 6d 61 6c 6c  obtain from mall
2a842 6f 63 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67  oc.  The calling
2a843 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  .** function is 
2a844 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20  responsible for 
2a845 73 65 65 69 6e 67 20 74 68 61 74 20 74 68 69 73  seeing that this
2a846 20 73 74 72 75 63 74 75 72 65 20 69 73 20 65 76   structure is ev
2a847 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 66 72 65 65  entually.** free
2a848 64 2e 20 20 41 64 64 20 74 68 65 20 4b 65 79 49  d.  Add the KeyI
2a849 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 6f  nfo structure to
2a84a 20 74 68 65 20 50 34 20 66 69 65 6c 64 20 6f 66   the P4 field of
2a84b 20 61 6e 20 6f 70 63 6f 64 65 20 75 73 69 6e 67   an opcode using
2a84c 0a 2a 2a 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48  .** P4_KEYINFO_H
2a84d 41 4e 44 4f 46 46 20 69 73 20 74 68 65 20 75 73  ANDOFF is the us
2a84e 75 61 6c 20 77 61 79 20 6f 66 20 64 65 61 6c 69  ual way of deali
2a84f 6e 67 20 77 69 74 68 20 74 68 69 73 2e 0a 2a 2f  ng with this..*/
2a850 0a 73 74 61 74 69 63 20 4b 65 79 49 6e 66 6f 20  .static KeyInfo 
2a851 2a 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72  *keyInfoFromExpr
2a852 4c 69 73 74 28 50 61 72 73 65 20 2a 70 50 61 72  List(Parse *pPar
2a853 73 65 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c  se, ExprList *pL
2a854 69 73 74 29 7b 0a 20 20 73 71 6c 69 74 65 33 20  ist){.  sqlite3 
2a855 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
2a856 3b 0a 20 20 69 6e 74 20 6e 45 78 70 72 3b 0a 20  ;.  int nExpr;. 
2a857 20 4b 65 79 49 6e 66 6f 20 2a 70 49 6e 66 6f 3b   KeyInfo *pInfo;
2a858 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69  .  struct ExprLi
2a859 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
2a85a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 6e 45 78 70    int i;..  nExp
2a85b 72 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  r = pList->nExpr
2a85c 3b 0a 20 20 70 49 6e 66 6f 20 3d 20 73 71 6c 69  ;.  pInfo = sqli
2a85d 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
2a85e 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 49 6e 66  db, sizeof(*pInf
2a85f 6f 29 20 2b 20 6e 45 78 70 72 2a 28 73 69 7a 65  o) + nExpr*(size
2a860 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 2b 31 29 20  of(CollSeq*)+1) 
2a861 29 3b 0a 20 20 69 66 28 20 70 49 6e 66 6f 20 29  );.  if( pInfo )
2a862 7b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 61 53 6f  {.    pInfo->aSo
2a863 72 74 4f 72 64 65 72 20 3d 20 28 75 38 2a 29 26  rtOrder = (u8*)&
2a864 70 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e 45 78  pInfo->aColl[nEx
2a865 70 72 5d 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e  pr];.    pInfo->
2a866 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29 6e 45  nField = (u16)nE
2a867 78 70 72 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e  xpr;.    pInfo->
2a868 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20  enc = ENC(db);. 
2a869 20 20 20 70 49 6e 66 6f 2d 3e 64 62 20 3d 20 64     pInfo->db = d
2a86a 62 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 2c 20  b;.    for(i=0, 
2a86b 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20  pItem=pList->a; 
2a86c 69 3c 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49  i<nExpr; i++, pI
2a86d 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 43 6f  tem++){.      Co
2a86e 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20  llSeq *pColl;.  
2a86f 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69      pColl = sqli
2a870 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70  te3ExprCollSeq(p
2a871 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45  Parse, pItem->pE
2a872 78 70 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  xpr);.      if( 
2a873 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20  !pColl ){.      
2a874 20 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44    pColl = db->pD
2a875 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d  fltColl;.      }
2a876 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43  .      pInfo->aC
2a877 6f 6c 6c 5b 69 5d 20 3d 20 70 43 6f 6c 6c 3b 0a  oll[i] = pColl;.
2a878 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 53 6f        pInfo->aSo
2a879 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 70 49 74  rtOrder[i] = pIt
2a87a 65 6d 2d 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20  em->sortOrder;. 
2a87b 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
2a87c 6e 20 70 49 6e 66 6f 3b 0a 7d 0a 0a 0a 2f 2a 0a  n pInfo;.}.../*.
2a87d 2a 2a 20 49 66 20 74 68 65 20 69 6e 6e 65 72 20  ** If the inner 
2a87e 6c 6f 6f 70 20 77 61 73 20 67 65 6e 65 72 61 74  loop was generat
2a87f 65 64 20 75 73 69 6e 67 20 61 20 6e 6f 6e 2d 6e  ed using a non-n
2a880 75 6c 6c 20 70 4f 72 64 65 72 42 79 20 61 72 67  ull pOrderBy arg
2a881 75 6d 65 6e 74 2c 0a 2a 2a 20 74 68 65 6e 20 74  ument,.** then t
2a882 68 65 20 72 65 73 75 6c 74 73 20 77 65 72 65 20  he results were 
2a883 70 6c 61 63 65 64 20 69 6e 20 61 20 73 6f 72 74  placed in a sort
2a884 65 72 2e 20 20 41 66 74 65 72 20 74 68 65 20 6c  er.  After the l
2a885 6f 6f 70 20 69 73 20 74 65 72 6d 69 6e 61 74 65  oop is terminate
2a886 64 0a 2a 2a 20 77 65 20 6e 65 65 64 20 74 6f 20  d.** we need to 
2a887 72 75 6e 20 74 68 65 20 73 6f 72 74 65 72 20 61  run the sorter a
2a888 6e 64 20 6f 75 74 70 75 74 20 74 68 65 20 72 65  nd output the re
2a889 73 75 6c 74 73 2e 20 20 54 68 65 20 66 6f 6c 6c  sults.  The foll
2a88a 6f 77 69 6e 67 0a 2a 2a 20 72 6f 75 74 69 6e 65  owing.** routine
2a88b 20 67 65 6e 65 72 61 74 65 73 20 74 68 65 20 63   generates the c
2a88c 6f 64 65 20 6e 65 65 64 65 64 20 74 6f 20 64 6f  ode needed to do
2a88d 20 74 68 61 74 2e 0a 2a 2f 0a 73 74 61 74 69 63   that..*/.static
2a88e 20 76 6f 69 64 20 67 65 6e 65 72 61 74 65 53 6f   void generateSo
2a88f 72 74 54 61 69 6c 28 0a 20 20 50 61 72 73 65 20  rtTail(.  Parse 
2a890 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a 20 50  *pParse,    /* P
2a891 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2a892 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20  /.  Select *p,  
2a893 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c        /* The SEL
2a894 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ECT statement */
2a895 0a 20 20 56 64 62 65 20 2a 76 2c 20 20 20 20 20  .  Vdbe *v,     
2a896 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
2a897 20 63 6f 64 65 20 69 6e 74 6f 20 74 68 69 73 20   code into this 
2a898 56 44 42 45 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  VDBE */.  int nC
2a899 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 2f 2a 20 4e  olumn,      /* N
2a89a 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
2a89b 20 6f 66 20 64 61 74 61 20 2a 2f 0a 20 20 53 65   of data */.  Se
2a89c 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73 74 20  lectDest *pDest 
2a89d 2f 2a 20 57 72 69 74 65 20 74 68 65 20 73 6f 72  /* Write the sor
2a89e 74 65 64 20 72 65 73 75 6c 74 73 20 68 65 72 65  ted results here
2a89f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 61 64 64   */.){.  int add
2a8a0 72 42 72 65 61 6b 20 3d 20 73 71 6c 69 74 65 33  rBreak = sqlite3
2a8a1 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
2a8a2 3b 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65  ;     /* Jump he
2a8a3 72 65 20 74 6f 20 65 78 69 74 20 6c 6f 6f 70 20  re to exit loop 
2a8a4 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 43 6f 6e  */.  int addrCon
2a8a5 74 69 6e 75 65 20 3d 20 73 71 6c 69 74 65 33 56  tinue = sqlite3V
2a8a6 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
2a8a7 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 66    /* Jump here f
2a8a8 6f 72 20 6e 65 78 74 20 63 79 63 6c 65 20 2a 2f  or next cycle */
2a8a9 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 69  .  int addr;.  i
2a8aa 6e 74 20 69 54 61 62 3b 0a 20 20 69 6e 74 20 70  nt iTab;.  int p
2a8ab 73 65 75 64 6f 54 61 62 20 3d 20 30 3b 0a 20 20  seudoTab = 0;.  
2a8ac 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
2a8ad 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79  By = p->pOrderBy
2a8ae 3b 0a 0a 20 20 69 6e 74 20 65 44 65 73 74 20 3d  ;..  int eDest =
2a8af 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3b 0a 20   pDest->eDest;. 
2a8b0 20 69 6e 74 20 69 50 61 72 6d 20 3d 20 70 44 65   int iParm = pDe
2a8b1 73 74 2d 3e 69 50 61 72 6d 3b 0a 0a 20 20 69 6e  st->iParm;..  in
2a8b2 74 20 72 65 67 52 6f 77 3b 0a 20 20 69 6e 74 20  t regRow;.  int 
2a8b3 72 65 67 52 6f 77 69 64 3b 0a 0a 20 20 69 54 61  regRowid;..  iTa
2a8b4 62 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 69 45  b = pOrderBy->iE
2a8b5 43 75 72 73 6f 72 3b 0a 20 20 72 65 67 52 6f 77  Cursor;.  regRow
2a8b6 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2a8b7 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
2a8b8 69 66 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f  if( eDest==SRT_O
2a8b9 75 74 70 75 74 20 7c 7c 20 65 44 65 73 74 3d 3d  utput || eDest==
2a8ba 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 29 7b  SRT_Coroutine ){
2a8bb 0a 20 20 20 20 70 73 65 75 64 6f 54 61 62 20 3d  .    pseudoTab =
2a8bc 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
2a8bd 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2a8be 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65  AddOp3(v, OP_Ope
2a8bf 6e 50 73 65 75 64 6f 2c 20 70 73 65 75 64 6f 54  nPseudo, pseudoT
2a8c0 61 62 2c 20 72 65 67 52 6f 77 2c 20 6e 43 6f 6c  ab, regRow, nCol
2a8c1 75 6d 6e 29 3b 0a 20 20 20 20 72 65 67 52 6f 77  umn);.    regRow
2a8c2 69 64 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  id = 0;.  }else{
2a8c3 0a 20 20 20 20 72 65 67 52 6f 77 69 64 20 3d 20  .    regRowid = 
2a8c4 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
2a8c5 67 28 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 20  g(pParse);.  }. 
2a8c6 20 61 64 64 72 20 3d 20 31 20 2b 20 73 71 6c 69   addr = 1 + sqli
2a8c7 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2a8c8 20 4f 50 5f 53 6f 72 74 2c 20 69 54 61 62 2c 20   OP_Sort, iTab, 
2a8c9 61 64 64 72 42 72 65 61 6b 29 3b 0a 20 20 63 6f  addrBreak);.  co
2a8ca 64 65 4f 66 66 73 65 74 28 76 2c 20 70 2c 20 61  deOffset(v, p, a
2a8cb 64 64 72 43 6f 6e 74 69 6e 75 65 29 3b 0a 20 20  ddrContinue);.  
2a8cc 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2a8cd 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
2a8ce 69 54 61 62 2c 20 70 4f 72 64 65 72 42 79 2d 3e  iTab, pOrderBy->
2a8cf 6e 45 78 70 72 20 2b 20 31 2c 20 72 65 67 52 6f  nExpr + 1, regRo
2a8d0 77 29 3b 0a 20 20 73 77 69 74 63 68 28 20 65 44  w);.  switch( eD
2a8d1 65 73 74 20 29 7b 0a 20 20 20 20 63 61 73 65 20  est ){.    case 
2a8d2 53 52 54 5f 54 61 62 6c 65 3a 0a 20 20 20 20 63  SRT_Table:.    c
2a8d3 61 73 65 20 53 52 54 5f 45 70 68 65 6d 54 61 62  ase SRT_EphemTab
2a8d4 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61  : {.      testca
2a8d5 73 65 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 54  se( eDest==SRT_T
2a8d6 61 62 6c 65 20 29 3b 0a 20 20 20 20 20 20 74 65  able );.      te
2a8d7 73 74 63 61 73 65 28 20 65 44 65 73 74 3d 3d 53  stcase( eDest==S
2a8d8 52 54 5f 45 70 68 65 6d 54 61 62 20 29 3b 0a 20  RT_EphemTab );. 
2a8d9 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2a8da 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77  AddOp2(v, OP_New
2a8db 52 6f 77 69 64 2c 20 69 50 61 72 6d 2c 20 72 65  Rowid, iParm, re
2a8dc 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73  gRowid);.      s
2a8dd 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2a8de 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69  (v, OP_Insert, i
2a8df 50 61 72 6d 2c 20 72 65 67 52 6f 77 2c 20 72 65  Parm, regRow, re
2a8e0 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73  gRowid);.      s
2a8e1 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
2a8e2 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50  P5(v, OPFLAG_APP
2a8e3 45 4e 44 29 3b 0a 20 20 20 20 20 20 62 72 65 61  END);.      brea
2a8e4 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66  k;.    }.#ifndef
2a8e5 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
2a8e6 51 55 45 52 59 0a 20 20 20 20 63 61 73 65 20 53  QUERY.    case S
2a8e7 52 54 5f 53 65 74 3a 20 7b 0a 20 20 20 20 20 20  RT_Set: {.      
2a8e8 61 73 73 65 72 74 28 20 6e 43 6f 6c 75 6d 6e 3d  assert( nColumn=
2a8e9 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =1 );.      sqli
2a8ea 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2a8eb 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20   OP_MakeRecord, 
2a8ec 72 65 67 52 6f 77 2c 20 31 2c 20 72 65 67 52 6f  regRow, 1, regRo
2a8ed 77 69 64 2c 20 26 70 2d 3e 61 66 66 69 6e 69 74  wid, &p->affinit
2a8ee 79 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  y, 1);.      sql
2a8ef 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66  ite3ExprCacheAff
2a8f0 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72  inityChange(pPar
2a8f1 73 65 2c 20 72 65 67 52 6f 77 2c 20 31 29 3b 0a  se, regRow, 1);.
2a8f2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2a8f3 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64  eAddOp2(v, OP_Id
2a8f4 78 49 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c 20  xInsert, iParm, 
2a8f5 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  regRowid);.     
2a8f6 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
2a8f7 20 20 63 61 73 65 20 53 52 54 5f 4d 65 6d 3a 20    case SRT_Mem: 
2a8f8 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
2a8f9 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20  nColumn==1 );.  
2a8fa 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2a8fb 6f 64 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20  odeMove(pParse, 
2a8fc 72 65 67 52 6f 77 2c 20 69 50 61 72 6d 2c 20 31  regRow, iParm, 1
2a8fd 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20  );.      /* The 
2a8fe 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 77 69 6c  LIMIT clause wil
2a8ff 6c 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20  l terminate the 
2a900 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20  loop for us */. 
2a901 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2a902 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 64 65 66  }.#endif.    def
2a903 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 69 6e  ault: {.      in
2a904 74 20 69 3b 0a 20 20 20 20 20 20 61 73 73 65 72  t i;.      asser
2a905 74 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75  t( eDest==SRT_Ou
2a906 74 70 75 74 20 7c 7c 20 65 44 65 73 74 3d 3d 53  tput || eDest==S
2a907 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 29 3b 20  RT_Coroutine ); 
2a908 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2a909 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70   eDest==SRT_Outp
2a90a 75 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ut );.      test
2a90b 63 61 73 65 28 20 65 44 65 73 74 3d 3d 53 52 54  case( eDest==SRT
2a90c 5f 43 6f 72 6f 75 74 69 6e 65 20 29 3b 0a 20 20  _Coroutine );.  
2a90d 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
2a90e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
2a90f 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 65        assert( re
2a910 67 52 6f 77 21 3d 70 44 65 73 74 2d 3e 69 4d 65  gRow!=pDest->iMe
2a911 6d 2b 69 20 29 3b 0a 20 20 20 20 20 20 20 20 73  m+i );.        s
2a912 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2a913 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 70  (v, OP_Column, p
2a914 73 65 75 64 6f 54 61 62 2c 20 69 2c 20 70 44 65  seudoTab, i, pDe
2a915 73 74 2d 3e 69 4d 65 6d 2b 69 29 3b 0a 20 20 20  st->iMem+i);.   
2a916 20 20 20 20 20 69 66 28 20 69 3d 3d 30 20 29 7b       if( i==0 ){
2a917 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2a918 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
2a919 2c 20 4f 50 46 4c 41 47 5f 43 4c 45 41 52 43 41  , OPFLAG_CLEARCA
2a91a 43 48 45 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  CHE);.        }.
2a91b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
2a91c 28 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74  ( eDest==SRT_Out
2a91d 70 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  put ){.        s
2a91e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2a91f 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
2a920 2c 20 70 44 65 73 74 2d 3e 69 4d 65 6d 2c 20 6e  , pDest->iMem, n
2a921 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20  Column);.       
2a922 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
2a923 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  eAffinityChange(
2a924 70 50 61 72 73 65 2c 20 70 44 65 73 74 2d 3e 69  pParse, pDest->i
2a925 4d 65 6d 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20  Mem, nColumn);. 
2a926 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2a927 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2a928 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c  ddOp1(v, OP_Yiel
2a929 64 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 29  d, pDest->iParm)
2a92a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2a92b 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
2a92c 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  .  sqlite3Releas
2a92d 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
2a92e 20 72 65 67 52 6f 77 29 3b 0a 20 20 73 71 6c 69   regRow);.  sqli
2a92f 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
2a930 67 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77  g(pParse, regRow
2a931 69 64 29 3b 0a 0a 20 20 2f 2a 20 4c 49 4d 49 54  id);..  /* LIMIT
2a932 20 68 61 73 20 62 65 65 6e 20 69 6d 70 6c 65 6d   has been implem
2a933 65 6e 74 65 64 20 62 79 20 74 68 65 20 70 75 73  ented by the pus
2a934 68 4f 6e 74 6f 53 6f 72 74 65 72 28 29 20 72 6f  hOntoSorter() ro
2a935 75 74 69 6e 65 2e 0a 20 20 2a 2f 0a 20 20 61 73  utine..  */.  as
2a936 73 65 72 74 28 20 70 2d 3e 69 4c 69 6d 69 74 3d  sert( p->iLimit=
2a937 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  =0 );..  /* The 
2a938 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65 20 6c 6f  bottom of the lo
2a939 6f 70 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  op.  */.  sqlite
2a93a 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
2a93b 6c 28 76 2c 20 61 64 64 72 43 6f 6e 74 69 6e 75  l(v, addrContinu
2a93c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  e);.  sqlite3Vdb
2a93d 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
2a93e 78 74 2c 20 69 54 61 62 2c 20 61 64 64 72 29 3b  xt, iTab, addr);
2a93f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  .  sqlite3VdbeRe
2a940 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64  solveLabel(v, ad
2a941 64 72 42 72 65 61 6b 29 3b 0a 20 20 69 66 28 20  drBreak);.  if( 
2a942 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75  eDest==SRT_Outpu
2a943 74 20 7c 7c 20 65 44 65 73 74 3d 3d 53 52 54 5f  t || eDest==SRT_
2a944 43 6f 72 6f 75 74 69 6e 65 20 29 7b 0a 20 20 20  Coroutine ){.   
2a945 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2a946 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p2(v, OP_Close, 
2a947 70 73 65 75 64 6f 54 61 62 2c 20 30 29 3b 0a 20  pseudoTab, 0);. 
2a948 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75   }.}../*.** Retu
2a949 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
2a94a 61 20 73 74 72 69 6e 67 20 63 6f 6e 74 61 69 6e  a string contain
2a94b 69 6e 67 20 74 68 65 20 27 64 65 63 6c 61 72 61  ing the 'declara
2a94c 74 69 6f 6e 20 74 79 70 65 27 20 6f 66 20 74 68  tion type' of th
2a94d 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20  e.** expression 
2a94e 70 45 78 70 72 2e 20 54 68 65 20 73 74 72 69 6e  pExpr. The strin
2a94f 67 20 6d 61 79 20 62 65 20 74 72 65 61 74 65 64  g may be treated
2a950 20 61 73 20 73 74 61 74 69 63 20 62 79 20 74 68   as static by th
2a951 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20  e caller..**.** 
2a952 54 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20  The declaration 
2a953 74 79 70 65 20 69 73 20 74 68 65 20 65 78 61 63  type is the exac
2a954 74 20 64 61 74 61 74 79 70 65 20 64 65 66 69 6e  t datatype defin
2a955 69 74 69 6f 6e 20 65 78 74 72 61 63 74 65 64 20  ition extracted 
2a956 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 6f 72 69 67  from the.** orig
2a957 69 6e 61 6c 20 43 52 45 41 54 45 20 54 41 42 4c  inal CREATE TABL
2a958 45 20 73 74 61 74 65 6d 65 6e 74 20 69 66 20 74  E statement if t
2a959 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
2a95a 20 61 20 63 6f 6c 75 6d 6e 2e 20 54 68 65 0a 2a   a column. The.*
2a95b 2a 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79  * declaration ty
2a95c 70 65 20 66 6f 72 20 61 20 52 4f 57 49 44 20 66  pe for a ROWID f
2a95d 69 65 6c 64 20 69 73 20 49 4e 54 45 47 45 52 2e  ield is INTEGER.
2a95e 20 45 78 61 63 74 6c 79 20 77 68 65 6e 20 61 6e   Exactly when an
2a95f 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69   expression.** i
2a960 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61 20 63  s considered a c
2a961 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 63 6f 6d  olumn can be com
2a962 70 6c 65 78 20 69 6e 20 74 68 65 20 70 72 65 73  plex in the pres
2a963 65 6e 63 65 20 6f 66 20 73 75 62 71 75 65 72 69  ence of subqueri
2a964 65 73 2e 20 54 68 65 0a 2a 2a 20 72 65 73 75 6c  es. The.** resul
2a965 74 2d 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e  t-set expression
2a966 20 69 6e 20 61 6c 6c 20 6f 66 20 74 68 65 20 66   in all of the f
2a967 6f 6c 6c 6f 77 69 6e 67 20 53 45 4c 45 43 54 20  ollowing SELECT 
2a968 73 74 61 74 65 6d 65 6e 74 73 20 69 73 20 0a 2a  statements is .*
2a969 2a 20 63 6f 6e 73 69 64 65 72 65 64 20 61 20 63  * considered a c
2a96a 6f 6c 75 6d 6e 20 62 79 20 74 68 69 73 20 66 75  olumn by this fu
2a96b 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20  nction..**.**   
2a96c 53 45 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20  SELECT col FROM 
2a96d 74 62 6c 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54  tbl;.**   SELECT
2a96e 20 28 53 45 4c 45 43 54 20 63 6f 6c 20 46 52 4f   (SELECT col FRO
2a96f 4d 20 74 62 6c 3b 0a 2a 2a 20 20 20 53 45 4c 45  M tbl;.**   SELE
2a970 43 54 20 28 53 45 4c 45 43 54 20 63 6f 6c 20 46  CT (SELECT col F
2a971 52 4f 4d 20 74 62 6c 29 3b 0a 2a 2a 20 20 20 53  ROM tbl);.**   S
2a972 45 4c 45 43 54 20 61 62 63 20 46 52 4f 4d 20 28  ELECT abc FROM (
2a973 53 45 4c 45 43 54 20 63 6f 6c 20 41 53 20 61 62  SELECT col AS ab
2a974 63 20 46 52 4f 4d 20 74 62 6c 29 3b 0a 2a 2a 20  c FROM tbl);.** 
2a975 0a 2a 2a 20 54 68 65 20 64 65 63 6c 61 72 61 74  .** The declarat
2a976 69 6f 6e 20 74 79 70 65 20 66 6f 72 20 61 6e 79  ion type for any
2a977 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 74 68 65   expression othe
2a978 72 20 74 68 61 6e 20 61 20 63 6f 6c 75 6d 6e 20  r than a column 
2a979 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74  is NULL..*/.stat
2a97a 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  ic const char *c
2a97b 6f 6c 75 6d 6e 54 79 70 65 28 0a 20 20 4e 61 6d  olumnType(.  Nam
2a97c 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20 0a  eContext *pNC, .
2a97d 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 0a 20    Expr *pExpr,. 
2a97e 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a   const char **pz
2a97f 4f 72 69 67 69 6e 44 62 2c 0a 20 20 63 6f 6e 73  OriginDb,.  cons
2a980 74 20 63 68 61 72 20 2a 2a 70 7a 4f 72 69 67 69  t char **pzOrigi
2a981 6e 54 61 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68  nTab,.  const ch
2a982 61 72 20 2a 2a 70 7a 4f 72 69 67 69 6e 43 6f 6c  ar **pzOriginCol
2a983 0a 29 7b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74  .){.  char const
2a984 20 2a 7a 54 79 70 65 20 3d 20 30 3b 0a 20 20 63   *zType = 0;.  c
2a985 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4f 72 69 67  har const *zOrig
2a986 69 6e 44 62 20 3d 20 30 3b 0a 20 20 63 68 61 72  inDb = 0;.  char
2a987 20 63 6f 6e 73 74 20 2a 7a 4f 72 69 67 69 6e 54   const *zOriginT
2a988 61 62 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63  ab = 0;.  char c
2a989 6f 6e 73 74 20 2a 7a 4f 72 69 67 69 6e 43 6f 6c  onst *zOriginCol
2a98a 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20   = 0;.  int j;. 
2a98b 20 69 66 28 20 4e 45 56 45 52 28 70 45 78 70 72   if( NEVER(pExpr
2a98c 3d 3d 30 29 20 7c 7c 20 70 4e 43 2d 3e 70 53 72  ==0) || pNC->pSr
2a98d 63 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72  cList==0 ) retur
2a98e 6e 20 30 3b 0a 0a 20 20 73 77 69 74 63 68 28 20  n 0;..  switch( 
2a98f 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20  pExpr->op ){.   
2a990 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f 4c   case TK_AGG_COL
2a991 55 4d 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b  UMN:.    case TK
2a992 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20  _COLUMN: {.     
2a993 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69   /* The expressi
2a994 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2e 20  on is a column. 
2a995 4c 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c 65  Locate the table
2a996 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 62   the column is b
2a997 65 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20 65 78  eing.      ** ex
2a998 74 72 61 63 74 65 64 20 66 72 6f 6d 20 69 6e 20  tracted from in 
2a999 4e 61 6d 65 43 6f 6e 74 65 78 74 2e 70 53 72 63  NameContext.pSrc
2a99a 4c 69 73 74 2e 20 54 68 69 73 20 74 61 62 6c 65  List. This table
2a99b 20 6d 61 79 20 62 65 20 72 65 61 6c 0a 20 20 20   may be real.   
2a99c 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 74     ** database t
2a99d 61 62 6c 65 20 6f 72 20 61 20 73 75 62 71 75 65  able or a subque
2a99e 72 79 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ry..      */.   
2a99f 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
2a9a0 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f   0;            /
2a9a1 2a 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  * Table structur
2a9a2 65 20 63 6f 6c 75 6d 6e 20 69 73 20 65 78 74 72  e column is extr
2a9a3 61 63 74 65 64 20 66 72 6f 6d 20 2a 2f 0a 20 20  acted from */.  
2a9a4 20 20 20 20 53 65 6c 65 63 74 20 2a 70 53 20 3d      Select *pS =
2a9a5 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
2a9a6 2f 2a 20 53 65 6c 65 63 74 20 74 68 65 20 63 6f  /* Select the co
2a9a7 6c 75 6d 6e 20 69 73 20 65 78 74 72 61 63 74 65  lumn is extracte
2a9a8 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20 20 20  d from */.      
2a9a9 69 6e 74 20 69 43 6f 6c 20 3d 20 70 45 78 70 72  int iCol = pExpr
2a9aa 2d 3e 69 43 6f 6c 75 6d 6e 3b 20 20 2f 2a 20 49  ->iColumn;  /* I
2a9ab 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69  ndex of column i
2a9ac 6e 20 70 54 61 62 20 2a 2f 0a 20 20 20 20 20 20  n pTab */.      
2a9ad 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72 2d  testcase( pExpr-
2a9ae 3e 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55  >op==TK_AGG_COLU
2a9af 4d 4e 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  MN );.      test
2a9b0 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d  case( pExpr->op=
2a9b1 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20  =TK_COLUMN );.  
2a9b2 20 20 20 20 77 68 69 6c 65 28 20 70 4e 43 20 26      while( pNC &
2a9b3 26 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20 20  & !pTab ){.     
2a9b4 20 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62     SrcList *pTab
2a9b5 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63  List = pNC->pSrc
2a9b6 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20 66 6f  List;.        fo
2a9b7 72 28 6a 3d 30 3b 6a 3c 70 54 61 62 4c 69 73 74  r(j=0;j<pTabList
2a9b8 2d 3e 6e 53 72 63 20 26 26 20 70 54 61 62 4c 69  ->nSrc && pTabLi
2a9b9 73 74 2d 3e 61 5b 6a 5d 2e 69 43 75 72 73 6f 72  st->a[j].iCursor
2a9ba 21 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3b  !=pExpr->iTable;
2a9bb 6a 2b 2b 29 3b 0a 20 20 20 20 20 20 20 20 69 66  j++);.        if
2a9bc 28 20 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  ( j<pTabList->nS
2a9bd 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  rc ){.          
2a9be 70 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74 2d  pTab = pTabList-
2a9bf 3e 61 5b 6a 5d 2e 70 54 61 62 3b 0a 20 20 20 20  >a[j].pTab;.    
2a9c0 20 20 20 20 20 20 70 53 20 3d 20 70 54 61 62 4c        pS = pTabL
2a9c1 69 73 74 2d 3e 61 5b 6a 5d 2e 70 53 65 6c 65 63  ist->a[j].pSelec
2a9c2 74 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  t;.        }else
2a9c3 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 43 20  {.          pNC 
2a9c4 3d 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20  = pNC->pNext;.  
2a9c5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
2a9c6 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62 3d  .      if( pTab=
2a9c7 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  =0 ){.        /*
2a9c8 20 41 74 20 6f 6e 65 20 74 69 6d 65 2c 20 63 6f   At one time, co
2a9c9 64 65 20 73 75 63 68 20 61 73 20 22 53 45 4c 45  de such as "SELE
2a9ca 43 54 20 6e 65 77 2e 78 22 20 77 69 74 68 69 6e  CT new.x" within
2a9cb 20 61 20 74 72 69 67 67 65 72 20 77 6f 75 6c 64   a trigger would
2a9cc 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 61 75 73  .        ** caus
2a9cd 65 20 74 68 69 73 20 63 6f 6e 64 69 74 69 6f 6e  e this condition
2a9ce 20 74 6f 20 72 75 6e 2e 20 20 53 69 6e 63 65 20   to run.  Since 
2a9cf 74 68 65 6e 2c 20 77 65 20 68 61 76 65 20 72 65  then, we have re
2a9d0 73 74 72 75 63 74 75 72 65 64 20 68 6f 77 0a 20  structured how. 
2a9d1 20 20 20 20 20 20 20 2a 2a 20 74 72 69 67 67 65         ** trigge
2a9d2 72 20 63 6f 64 65 20 69 73 20 67 65 6e 65 72 61  r code is genera
2a9d3 74 65 64 20 61 6e 64 20 73 6f 20 74 68 69 73 20  ted and so this 
2a9d4 63 6f 6e 64 69 74 69 6f 6e 20 69 73 20 6e 6f 20  condition is no 
2a9d5 6c 6f 6e 67 65 72 20 0a 20 20 20 20 20 20 20 20  longer .        
2a9d6 2a 2a 20 70 6f 73 73 69 62 6c 65 2e 20 48 6f 77  ** possible. How
2a9d7 65 76 65 72 2c 20 69 74 20 63 61 6e 20 73 74 69  ever, it can sti
2a9d8 6c 6c 20 62 65 20 74 72 75 65 20 66 6f 72 20 73  ll be true for s
2a9d9 74 61 74 65 6d 65 6e 74 73 20 6c 69 6b 65 0a 20  tatements like. 
2a9da 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 66 6f         ** the fo
2a9db 6c 6c 6f 77 69 6e 67 3a 0a 20 20 20 20 20 20 20  llowing:.       
2a9dc 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20   **.        **  
2a9dd 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31   CREATE TABLE t1
2a9de 28 63 6f 6c 20 49 4e 54 45 47 45 52 29 3b 0a 20  (col INTEGER);. 
2a9df 20 20 20 20 20 20 20 2a 2a 20 20 20 53 45 4c 45         **   SELE
2a9e0 43 54 20 28 53 45 4c 45 43 54 20 74 31 2e 63 6f  CT (SELECT t1.co
2a9e1 6c 29 20 46 52 4f 4d 20 46 52 4f 4d 20 74 31 3b  l) FROM FROM t1;
2a9e2 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
2a9e3 20 20 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6c 75      ** when colu
2a9e4 6d 6e 54 79 70 65 28 29 20 69 73 20 63 61 6c 6c  mnType() is call
2a9e5 65 64 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73  ed on the expres
2a9e6 73 69 6f 6e 20 22 74 31 2e 63 6f 6c 22 20 69 6e  sion "t1.col" in
2a9e7 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a   the .        **
2a9e8 20 73 75 62 2d 73 65 6c 65 63 74 2e 20 49 6e 20   sub-select. In 
2a9e9 74 68 69 73 20 63 61 73 65 2c 20 73 65 74 20 74  this case, set t
2a9ea 68 65 20 63 6f 6c 75 6d 6e 20 74 79 70 65 20 74  he column type t
2a9eb 6f 20 4e 55 4c 4c 2c 20 65 76 65 6e 0a 20 20 20  o NULL, even.   
2a9ec 20 20 20 20 20 2a 2a 20 74 68 6f 75 67 68 20 69       ** though i
2a9ed 74 20 73 68 6f 75 6c 64 20 72 65 61 6c 6c 79 20  t should really 
2a9ee 62 65 20 22 49 4e 54 45 47 45 52 22 2e 0a 20 20  be "INTEGER"..  
2a9ef 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
2a9f0 20 2a 2a 20 54 68 69 73 20 69 73 20 6e 6f 74 20   ** This is not 
2a9f1 61 20 70 72 6f 62 6c 65 6d 2c 20 61 73 20 74 68  a problem, as th
2a9f2 65 20 63 6f 6c 75 6d 6e 20 74 79 70 65 20 6f 66  e column type of
2a9f3 20 22 74 31 2e 63 6f 6c 22 20 69 73 20 6e 65 76   "t1.col" is nev
2a9f4 65 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 75 73  er.        ** us
2a9f5 65 64 2e 20 57 68 65 6e 20 63 6f 6c 75 6d 6e 54  ed. When columnT
2a9f6 79 70 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20  ype() is called 
2a9f7 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  on the expressio
2a9f8 6e 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 22 28  n .        ** "(
2a9f9 53 45 4c 45 43 54 20 74 31 2e 63 6f 6c 29 22 2c  SELECT t1.col)",
2a9fa 20 74 68 65 20 63 6f 72 72 65 63 74 20 74 79 70   the correct typ
2a9fb 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 28 73  e is returned (s
2a9fc 65 65 20 74 68 65 20 54 4b 5f 53 45 4c 45 43 54  ee the TK_SELECT
2a9fd 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 72 61 6e  .        ** bran
2a9fe 63 68 20 62 65 6c 6f 77 2e 20 20 2a 2f 0a 20 20  ch below.  */.  
2a9ff 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2aa00 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65     }..      asse
2aa01 72 74 28 20 70 54 61 62 20 26 26 20 70 45 78 70  rt( pTab && pExp
2aa02 72 2d 3e 70 54 61 62 3d 3d 70 54 61 62 20 29 3b  r->pTab==pTab );
2aa03 0a 20 20 20 20 20 20 69 66 28 20 70 53 20 29 7b  .      if( pS ){
2aa04 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
2aa05 22 74 61 62 6c 65 22 20 69 73 20 61 63 74 75 61  "table" is actua
2aa06 6c 6c 79 20 61 20 73 75 62 2d 73 65 6c 65 63 74  lly a sub-select
2aa07 20 6f 72 20 61 20 76 69 65 77 20 69 6e 20 74 68   or a view in th
2aa08 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a 20 20  e FROM clause.  
2aa09 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20        ** of the 
2aa0a 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2aa0b 2e 20 52 65 74 75 72 6e 20 74 68 65 20 64 65 63  . Return the dec
2aa0c 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 61 6e  laration type an
2aa0d 64 20 6f 72 69 67 69 6e 0a 20 20 20 20 20 20 20  d origin.       
2aa0e 20 2a 2a 20 64 61 74 61 20 66 6f 72 20 74 68 65   ** data for the
2aa0f 20 72 65 73 75 6c 74 2d 73 65 74 20 63 6f 6c 75   result-set colu
2aa10 6d 6e 20 6f 66 20 74 68 65 20 73 75 62 2d 73 65  mn of the sub-se
2aa11 6c 65 63 74 2e 0a 20 20 20 20 20 20 20 20 2a 2f  lect..        */
2aa12 0a 20 20 20 20 20 20 20 20 69 66 28 20 41 4c 57  .        if( ALW
2aa13 41 59 53 28 69 43 6f 6c 3e 3d 30 20 26 26 20 69  AYS(iCol>=0 && i
2aa14 43 6f 6c 3c 70 53 2d 3e 70 45 4c 69 73 74 2d 3e  Col<pS->pEList->
2aa15 6e 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20  nExpr) ){.      
2aa16 20 20 20 20 2f 2a 20 49 66 20 69 43 6f 6c 20 69      /* If iCol i
2aa17 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f  s less than zero
2aa18 2c 20 74 68 65 6e 20 74 68 65 20 65 78 70 72 65  , then the expre
2aa19 73 73 69 6f 6e 20 72 65 71 75 65 73 74 73 20 74  ssion requests t
2aa1a 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  he.          ** 
2aa1b 72 6f 77 69 64 20 6f 66 20 74 68 65 20 73 75 62  rowid of the sub
2aa1c 2d 73 65 6c 65 63 74 20 6f 72 20 76 69 65 77 2e  -select or view.
2aa1d 20 54 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e   This expression
2aa1e 20 69 73 20 6c 65 67 61 6c 20 28 73 65 65 20 0a   is legal (see .
2aa1f 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 65 73            ** tes
2aa20 74 20 63 61 73 65 20 6d 69 73 63 32 2e 32 2e 32  t case misc2.2.2
2aa21 29 20 2d 20 69 74 20 61 6c 77 61 79 73 20 65 76  ) - it always ev
2aa22 61 6c 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c 2e  aluates to NULL.
2aa23 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  .          */.  
2aa24 20 20 20 20 20 20 20 20 4e 61 6d 65 43 6f 6e 74          NameCont
2aa25 65 78 74 20 73 4e 43 3b 0a 20 20 20 20 20 20 20  ext sNC;.       
2aa26 20 20 20 45 78 70 72 20 2a 70 20 3d 20 70 53 2d     Expr *p = pS-
2aa27 3e 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d  >pEList->a[iCol]
2aa28 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20  .pExpr;.        
2aa29 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d    sNC.pSrcList =
2aa2a 20 70 53 2d 3e 70 53 72 63 3b 0a 20 20 20 20 20   pS->pSrc;.     
2aa2b 20 20 20 20 20 73 4e 43 2e 70 4e 65 78 74 20 3d       sNC.pNext =
2aa2c 20 70 4e 43 3b 0a 20 20 20 20 20 20 20 20 20 20   pNC;.          
2aa2d 73 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 4e 43  sNC.pParse = pNC
2aa2e 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 20 20  ->pParse;.      
2aa2f 20 20 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c 75      zType = colu
2aa30 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20  mnType(&sNC, p, 
2aa31 26 7a 4f 72 69 67 69 6e 44 62 2c 20 26 7a 4f 72  &zOriginDb, &zOr
2aa32 69 67 69 6e 54 61 62 2c 20 26 7a 4f 72 69 67 69  iginTab, &zOrigi
2aa33 6e 43 6f 6c 29 3b 20 0a 20 20 20 20 20 20 20 20  nCol); .        
2aa34 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  }.      }else if
2aa35 28 20 41 4c 57 41 59 53 28 70 54 61 62 2d 3e 70  ( ALWAYS(pTab->p
2aa36 53 63 68 65 6d 61 29 20 29 7b 0a 20 20 20 20 20  Schema) ){.     
2aa37 20 20 20 2f 2a 20 41 20 72 65 61 6c 20 74 61 62     /* A real tab
2aa38 6c 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73  le */.        as
2aa39 73 65 72 74 28 20 21 70 53 20 29 3b 0a 20 20 20  sert( !pS );.   
2aa3a 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20       if( iCol<0 
2aa3b 29 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 69  ) iCol = pTab->i
2aa3c 50 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 61 73  PKey;.        as
2aa3d 73 65 72 74 28 20 69 43 6f 6c 3d 3d 2d 31 20 7c  sert( iCol==-1 |
2aa3e 7c 20 28 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43  | (iCol>=0 && iC
2aa3f 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 29 20 29  ol<pTab->nCol) )
2aa40 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 43  ;.        if( iC
2aa41 6f 6c 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ol<0 ){.        
2aa42 20 20 7a 54 79 70 65 20 3d 20 22 49 4e 54 45 47    zType = "INTEG
2aa43 45 52 22 3b 0a 20 20 20 20 20 20 20 20 20 20 7a  ER";.          z
2aa44 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 22 72 6f 77  OriginCol = "row
2aa45 69 64 22 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  id";.        }el
2aa46 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 54  se{.          zT
2aa47 79 70 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c  ype = pTab->aCol
2aa48 5b 69 43 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20  [iCol].zType;.  
2aa49 20 20 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 43          zOriginC
2aa4a 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  ol = pTab->aCol[
2aa4b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  iCol].zName;.   
2aa4c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7a       }.        z
2aa4d 4f 72 69 67 69 6e 54 61 62 20 3d 20 70 54 61 62  OriginTab = pTab
2aa4e 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20  ->zName;.       
2aa4f 20 69 66 28 20 70 4e 43 2d 3e 70 50 61 72 73 65   if( pNC->pParse
2aa50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e   ){.          in
2aa51 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53  t iDb = sqlite3S
2aa52 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 4e 43  chemaToIndex(pNC
2aa53 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54  ->pParse->db, pT
2aa54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
2aa55 20 20 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 44          zOriginD
2aa56 62 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65 2d  b = pNC->pParse-
2aa57 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e  >db->aDb[iDb].zN
2aa58 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ame;.        }. 
2aa59 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
2aa5a 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65  ak;.    }.#ifnde
2aa5b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  f SQLITE_OMIT_SU
2aa5c 42 51 55 45 52 59 0a 20 20 20 20 63 61 73 65 20  BQUERY.    case 
2aa5d 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a 20 20 20  TK_SELECT: {.   
2aa5e 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
2aa5f 73 69 6f 6e 20 69 73 20 61 20 73 75 62 2d 73 65  sion is a sub-se
2aa60 6c 65 63 74 2e 20 52 65 74 75 72 6e 20 74 68 65  lect. Return the
2aa61 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70   declaration typ
2aa62 65 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a 20 6f  e and.      ** o
2aa63 72 69 67 69 6e 20 69 6e 66 6f 20 66 6f 72 20 74  rigin info for t
2aa64 68 65 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e  he single column
2aa65 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
2aa66 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  et of the SELECT
2aa67 0a 20 20 20 20 20 20 2a 2a 20 73 74 61 74 65 6d  .      ** statem
2aa68 65 6e 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ent..      */.  
2aa69 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20      NameContext 
2aa6a 73 4e 43 3b 0a 20 20 20 20 20 20 53 65 6c 65 63  sNC;.      Selec
2aa6b 74 20 2a 70 53 20 3d 20 70 45 78 70 72 2d 3e 78  t *pS = pExpr->x
2aa6c 2e 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 20  .pSelect;.      
2aa6d 45 78 70 72 20 2a 70 20 3d 20 70 53 2d 3e 70 45  Expr *p = pS->pE
2aa6e 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
2aa6f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2aa70 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28  ExprHasProperty(
2aa71 70 45 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c  pExpr, EP_xIsSel
2aa72 65 63 74 29 20 29 3b 0a 20 20 20 20 20 20 73 4e  ect) );.      sN
2aa73 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 53 2d  C.pSrcList = pS-
2aa74 3e 70 53 72 63 3b 0a 20 20 20 20 20 20 73 4e 43  >pSrc;.      sNC
2aa75 2e 70 4e 65 78 74 20 3d 20 70 4e 43 3b 0a 20 20  .pNext = pNC;.  
2aa76 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d      sNC.pParse =
2aa77 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20   pNC->pParse;.  
2aa78 20 20 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c 75      zType = colu
2aa79 6d 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20  mnType(&sNC, p, 
2aa7a 26 7a 4f 72 69 67 69 6e 44 62 2c 20 26 7a 4f 72  &zOriginDb, &zOr
2aa7b 69 67 69 6e 54 61 62 2c 20 26 7a 4f 72 69 67 69  iginTab, &zOrigi
2aa7c 6e 43 6f 6c 29 3b 20 0a 20 20 20 20 20 20 62 72  nCol); .      br
2aa7d 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
2aa7e 66 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 20 70  f.  }.  .  if( p
2aa7f 7a 4f 72 69 67 69 6e 44 62 20 29 7b 0a 20 20 20  zOriginDb ){.   
2aa80 20 61 73 73 65 72 74 28 20 70 7a 4f 72 69 67 69   assert( pzOrigi
2aa81 6e 54 61 62 20 26 26 20 70 7a 4f 72 69 67 69 6e  nTab && pzOrigin
2aa82 43 6f 6c 20 29 3b 0a 20 20 20 20 2a 70 7a 4f 72  Col );.    *pzOr
2aa83 69 67 69 6e 44 62 20 3d 20 7a 4f 72 69 67 69 6e  iginDb = zOrigin
2aa84 44 62 3b 0a 20 20 20 20 2a 70 7a 4f 72 69 67 69  Db;.    *pzOrigi
2aa85 6e 54 61 62 20 3d 20 7a 4f 72 69 67 69 6e 54 61  nTab = zOriginTa
2aa86 62 3b 0a 20 20 20 20 2a 70 7a 4f 72 69 67 69 6e  b;.    *pzOrigin
2aa87 43 6f 6c 20 3d 20 7a 4f 72 69 67 69 6e 43 6f 6c  Col = zOriginCol
2aa88 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a  ;.  }.  return z
2aa89 54 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  Type;.}../*.** G
2aa8a 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
2aa8b 74 20 77 69 6c 6c 20 74 65 6c 6c 20 74 68 65 20  t will tell the 
2aa8c 56 44 42 45 20 74 68 65 20 64 65 63 6c 61 72 61  VDBE the declara
2aa8d 74 69 6f 6e 20 74 79 70 65 73 20 6f 66 20 63 6f  tion types of co
2aa8e 6c 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20  lumns.** in the 
2aa8f 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 73  result set..*/.s
2aa90 74 61 74 69 63 20 76 6f 69 64 20 67 65 6e 65 72  tatic void gener
2aa91 61 74 65 43 6f 6c 75 6d 6e 54 79 70 65 73 28 0a  ateColumnTypes(.
2aa92 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2aa93 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20        /* Parser 
2aa94 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63  context */.  Src
2aa95 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20  List *pTabList, 
2aa96 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c   /* List of tabl
2aa97 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  es */.  ExprList
2aa98 20 2a 70 45 4c 69 73 74 20 20 20 20 2f 2a 20 45   *pEList    /* E
2aa99 78 70 72 65 73 73 69 6f 6e 73 20 64 65 66 69 6e  xpressions defin
2aa9a 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74 20 73  ing the result s
2aa9b 65 74 20 2a 2f 0a 29 7b 0a 23 69 66 6e 64 65 66  et */.){.#ifndef
2aa9c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43   SQLITE_OMIT_DEC
2aa9d 4c 54 59 50 45 0a 20 20 56 64 62 65 20 2a 76 20  LTYPE.  Vdbe *v 
2aa9e 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
2aa9f 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4e 61 6d 65  .  int i;.  Name
2aaa0 43 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 73  Context sNC;.  s
2aaa1 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 54  NC.pSrcList = pT
2aaa2 61 62 4c 69 73 74 3b 0a 20 20 73 4e 43 2e 70 50  abList;.  sNC.pP
2aaa3 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20  arse = pParse;. 
2aaa4 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69   for(i=0; i<pELi
2aaa5 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  st->nExpr; i++){
2aaa6 0a 20 20 20 20 45 78 70 72 20 2a 70 20 3d 20 70  .    Expr *p = p
2aaa7 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70  EList->a[i].pExp
2aaa8 72 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61  r;.    const cha
2aaa9 72 20 2a 7a 54 79 70 65 3b 0a 23 69 66 64 65 66  r *zType;.#ifdef
2aaaa 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43   SQLITE_ENABLE_C
2aaab 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 20  OLUMN_METADATA. 
2aaac 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
2aaad 4f 72 69 67 44 62 20 3d 20 30 3b 0a 20 20 20 20  OrigDb = 0;.    
2aaae 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 72 69  const char *zOri
2aaaf 67 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 63 6f  gTab = 0;.    co
2aab0 6e 73 74 20 63 68 61 72 20 2a 7a 4f 72 69 67 43  nst char *zOrigC
2aab1 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 7a 54 79 70  ol = 0;.    zTyp
2aab2 65 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26  e = columnType(&
2aab3 73 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 67 44 62  sNC, p, &zOrigDb
2aab4 2c 20 26 7a 4f 72 69 67 54 61 62 2c 20 26 7a 4f  , &zOrigTab, &zO
2aab5 72 69 67 43 6f 6c 29 3b 0a 0a 20 20 20 20 2f 2a  rigCol);..    /*
2aab6 20 54 68 65 20 76 64 62 65 20 6d 75 73 74 20 6d   The vdbe must m
2aab7 61 6b 65 20 69 74 73 20 6f 77 6e 20 63 6f 70 79  ake its own copy
2aab8 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2d 74   of the column-t
2aab9 79 70 65 20 61 6e 64 20 6f 74 68 65 72 20 0a 20  ype and other . 
2aaba 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 73 70 65     ** column spe
2aabb 63 69 66 69 63 20 73 74 72 69 6e 67 73 2c 20 69  cific strings, i
2aabc 6e 20 63 61 73 65 20 74 68 65 20 73 63 68 65 6d  n case the schem
2aabd 61 20 69 73 20 72 65 73 65 74 20 62 65 66 6f 72  a is reset befor
2aabe 65 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 76 69  e this.    ** vi
2aabf 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 69 73  rtual machine is
2aac0 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f   deleted..    */
2aac1 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2aac2 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c  SetColName(v, i,
2aac3 20 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53   COLNAME_DATABAS
2aac4 45 2c 20 7a 4f 72 69 67 44 62 2c 20 53 51 4c 49  E, zOrigDb, SQLI
2aac5 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
2aac6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
2aac7 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43  tColName(v, i, C
2aac8 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 2c 20 7a 4f  OLNAME_TABLE, zO
2aac9 72 69 67 54 61 62 2c 20 53 51 4c 49 54 45 5f 54  rigTab, SQLITE_T
2aaca 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 73  RANSIENT);.    s
2aacb 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
2aacc 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41  Name(v, i, COLNA
2aacd 4d 45 5f 43 4f 4c 55 4d 4e 2c 20 7a 4f 72 69 67  ME_COLUMN, zOrig
2aace 43 6f 6c 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  Col, SQLITE_TRAN
2aacf 53 49 45 4e 54 29 3b 0a 23 65 6c 73 65 0a 20 20  SIENT);.#else.  
2aad0 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c 75 6d 6e    zType = column
2aad1 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20 30 2c  Type(&sNC, p, 0,
2aad2 20 30 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20   0, 0);.#endif. 
2aad3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
2aad4 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43  tColName(v, i, C
2aad5 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 2c  OLNAME_DECLTYPE,
2aad6 20 7a 54 79 70 65 2c 20 53 51 4c 49 54 45 5f 54   zType, SQLITE_T
2aad7 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 23  RANSIENT);.  }.#
2aad8 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2aad9 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20 2a 2f  OMIT_DECLTYPE */
2aada 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
2aadb 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c  te code that wil
2aadc 6c 20 74 65 6c 6c 20 74 68 65 20 56 44 42 45 20  l tell the VDBE 
2aadd 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 63 6f 6c  the names of col
2aade 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 72  umns.** in the r
2aadf 65 73 75 6c 74 20 73 65 74 2e 20 20 54 68 69 73  esult set.  This
2aae0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20   information is 
2aae1 75 73 65 64 20 74 6f 20 70 72 6f 76 69 64 65 20  used to provide 
2aae2 74 68 65 0a 2a 2a 20 61 7a 43 6f 6c 5b 5d 20 76  the.** azCol[] v
2aae3 61 6c 75 65 73 20 69 6e 20 74 68 65 20 63 61 6c  alues in the cal
2aae4 6c 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63  lback..*/.static
2aae5 20 76 6f 69 64 20 67 65 6e 65 72 61 74 65 43 6f   void generateCo
2aae6 6c 75 6d 6e 4e 61 6d 65 73 28 0a 20 20 50 61 72  lumnNames(.  Par
2aae7 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2aae8 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65   /* Parser conte
2aae9 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  xt */.  SrcList 
2aaea 2a 70 54 61 62 4c 69 73 74 2c 20 20 2f 2a 20 4c  *pTabList,  /* L
2aaeb 69 73 74 20 6f 66 20 74 61 62 6c 65 73 20 2a 2f  ist of tables */
2aaec 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
2aaed 69 73 74 20 20 20 20 2f 2a 20 45 78 70 72 65 73  ist    /* Expres
2aaee 73 69 6f 6e 73 20 64 65 66 69 6e 69 6e 67 20 74  sions defining t
2aaef 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f  he result set */
2aaf0 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  .){.  Vdbe *v = 
2aaf1 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
2aaf2 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 71 6c   int i, j;.  sql
2aaf3 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
2aaf4 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 66 75 6c  e->db;.  int ful
2aaf5 6c 4e 61 6d 65 73 2c 20 73 68 6f 72 74 4e 61 6d  lNames, shortNam
2aaf6 65 73 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  es;..#ifndef SQL
2aaf7 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e  ITE_OMIT_EXPLAIN
2aaf8 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73  .  /* If this is
2aaf9 20 61 6e 20 45 58 50 4c 41 49 4e 2c 20 73 6b 69   an EXPLAIN, ski
2aafa 70 20 74 68 69 73 20 73 74 65 70 20 2a 2f 0a 20  p this step */. 
2aafb 20 69 66 28 20 70 50 61 72 73 65 2d 3e 65 78 70   if( pParse->exp
2aafc 6c 61 69 6e 20 29 7b 0a 20 20 20 20 72 65 74 75  lain ){.    retu
2aafd 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  rn;.  }.#endif..
2aafe 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 63 6f    if( pParse->co
2aaff 6c 4e 61 6d 65 73 53 65 74 20 7c 7c 20 4e 45 56  lNamesSet || NEV
2ab00 45 52 28 76 3d 3d 30 29 20 7c 7c 20 64 62 2d 3e  ER(v==0) || db->
2ab01 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72  mallocFailed ) r
2ab02 65 74 75 72 6e 3b 0a 20 20 70 50 61 72 73 65 2d  eturn;.  pParse-
2ab03 3e 63 6f 6c 4e 61 6d 65 73 53 65 74 20 3d 20 31  >colNamesSet = 1
2ab04 3b 0a 20 20 66 75 6c 6c 4e 61 6d 65 73 20 3d 20  ;.  fullNames = 
2ab05 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  (db->flags & SQL
2ab06 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d 65 73  ITE_FullColNames
2ab07 29 21 3d 30 3b 0a 20 20 73 68 6f 72 74 4e 61 6d  )!=0;.  shortNam
2ab08 65 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67 73 20  es = (db->flags 
2ab09 26 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f  & SQLITE_ShortCo
2ab0a 6c 4e 61 6d 65 73 29 21 3d 30 3b 0a 20 20 73 71  lNames)!=0;.  sq
2ab0b 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
2ab0c 6f 6c 73 28 76 2c 20 70 45 4c 69 73 74 2d 3e 6e  ols(v, pEList->n
2ab0d 45 78 70 72 29 3b 0a 20 20 66 6f 72 28 69 3d 30  Expr);.  for(i=0
2ab0e 3b 20 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70  ; i<pEList->nExp
2ab0f 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70  r; i++){.    Exp
2ab10 72 20 2a 70 3b 0a 20 20 20 20 70 20 3d 20 70 45  r *p;.    p = pE
2ab11 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  List->a[i].pExpr
2ab12 3b 0a 20 20 20 20 69 66 28 20 4e 45 56 45 52 28  ;.    if( NEVER(
2ab13 70 3d 3d 30 29 20 29 20 63 6f 6e 74 69 6e 75 65  p==0) ) continue
2ab14 3b 0a 20 20 20 20 69 66 28 20 70 45 4c 69 73 74  ;.    if( pEList
2ab15 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20 29 7b 0a  ->a[i].zName ){.
2ab16 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d        char *zNam
2ab17 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d  e = pEList->a[i]
2ab18 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 73 71  .zName;.      sq
2ab19 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
2ab1a 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d  ame(v, i, COLNAM
2ab1b 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d 65 2c 20 53  E_NAME, zName, S
2ab1c 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
2ab1d 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
2ab1e 28 70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d  (p->op==TK_COLUM
2ab1f 4e 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 41  N || p->op==TK_A
2ab20 47 47 5f 43 4f 4c 55 4d 4e 29 20 26 26 20 70 54  GG_COLUMN) && pT
2ab21 61 62 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20  abList ){.      
2ab22 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20  Table *pTab;.   
2ab23 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 0a 20     char *zCol;. 
2ab24 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20       int iCol = 
2ab25 70 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  p->iColumn;.    
2ab26 20 20 66 6f 72 28 6a 3d 30 3b 20 41 4c 57 41 59    for(j=0; ALWAY
2ab27 53 28 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  S(j<pTabList->nS
2ab28 72 63 29 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  rc); j++){.     
2ab29 20 20 20 69 66 28 20 70 54 61 62 4c 69 73 74 2d     if( pTabList-
2ab2a 3e 61 5b 6a 5d 2e 69 43 75 72 73 6f 72 3d 3d 70  >a[j].iCursor==p
2ab2b 2d 3e 69 54 61 62 6c 65 20 29 20 62 72 65 61 6b  ->iTable ) break
2ab2c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2ab2d 61 73 73 65 72 74 28 20 6a 3c 70 54 61 62 4c 69  assert( j<pTabLi
2ab2e 73 74 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 20 20  st->nSrc );.    
2ab2f 20 20 70 54 61 62 20 3d 20 70 54 61 62 4c 69 73    pTab = pTabLis
2ab30 74 2d 3e 61 5b 6a 5d 2e 70 54 61 62 3b 0a 20 20  t->a[j].pTab;.  
2ab31 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29      if( iCol<0 )
2ab32 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 69 50   iCol = pTab->iP
2ab33 4b 65 79 3b 0a 20 20 20 20 20 20 61 73 73 65 72  Key;.      asser
2ab34 74 28 20 69 43 6f 6c 3d 3d 2d 31 20 7c 7c 20 28  t( iCol==-1 || (
2ab35 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c  iCol>=0 && iCol<
2ab36 70 54 61 62 2d 3e 6e 43 6f 6c 29 20 29 3b 0a 20  pTab->nCol) );. 
2ab37 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20       if( iCol<0 
2ab38 29 7b 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 20  ){.        zCol 
2ab39 3d 20 22 72 6f 77 69 64 22 3b 0a 20 20 20 20 20  = "rowid";.     
2ab3a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2ab3b 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43 6f  zCol = pTab->aCo
2ab3c 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b 0a 20  l[iCol].zName;. 
2ab3d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
2ab3e 20 21 73 68 6f 72 74 4e 61 6d 65 73 20 26 26 20   !shortNames && 
2ab3f 21 66 75 6c 6c 4e 61 6d 65 73 20 29 7b 0a 20 20  !fullNames ){.  
2ab40 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2ab41 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69  eSetColName(v, i
2ab42 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
2ab43 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
2ab44 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
2ab45 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 53   pEList->a[i].zS
2ab46 70 61 6e 29 2c 20 53 51 4c 49 54 45 5f 44 59 4e  pan), SQLITE_DYN
2ab47 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 7d 65 6c  AMIC);.      }el
2ab48 73 65 20 69 66 28 20 66 75 6c 6c 4e 61 6d 65 73  se if( fullNames
2ab49 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72   ){.        char
2ab4a 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 20   *zName = 0;.   
2ab4b 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c       zName = sql
2ab4c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
2ab4d 22 25 73 2e 25 73 22 2c 20 70 54 61 62 2d 3e 7a  "%s.%s", pTab->z
2ab4e 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20  Name, zCol);.   
2ab4f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2ab50 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c  SetColName(v, i,
2ab51 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a   COLNAME_NAME, z
2ab52 4e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 44 59 4e  Name, SQLITE_DYN
2ab53 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 7d 65 6c  AMIC);.      }el
2ab54 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  se{.        sqli
2ab55 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
2ab56 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, i, COLNAME_
2ab57 4e 41 4d 45 2c 20 7a 43 6f 6c 2c 20 53 51 4c 49  NAME, zCol, SQLI
2ab58 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
2ab59 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
2ab5a 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
2ab5b 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
2ab5c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   i, COLNAME_NAME
2ab5d 2c 20 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  , .          sql
2ab5e 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
2ab5f 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 53   pEList->a[i].zS
2ab60 70 61 6e 29 2c 20 53 51 4c 49 54 45 5f 44 59 4e  pan), SQLITE_DYN
2ab61 41 4d 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  AMIC);.    }.  }
2ab62 0a 20 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d  .  generateColum
2ab63 6e 54 79 70 65 73 28 70 50 61 72 73 65 2c 20 70  nTypes(pParse, p
2ab64 54 61 62 4c 69 73 74 2c 20 70 45 4c 69 73 74 29  TabList, pEList)
2ab65 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
2ab66 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e  ITE_OMIT_COMPOUN
2ab67 44 5f 53 45 4c 45 43 54 0a 2f 2a 0a 2a 2a 20 4e  D_SELECT./*.** N
2ab68 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6e 6e 65  ame of the conne
2ab69 63 74 69 6f 6e 20 6f 70 65 72 61 74 6f 72 2c 20  ction operator, 
2ab6a 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20 6d  used for error m
2ab6b 65 73 73 61 67 65 73 2e 0a 2a 2f 0a 73 74 61 74  essages..*/.stat
2ab6c 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  ic const char *s
2ab6d 65 6c 65 63 74 4f 70 4e 61 6d 65 28 69 6e 74 20  electOpName(int 
2ab6e 69 64 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a  id){.  char *z;.
2ab6f 20 20 73 77 69 74 63 68 28 20 69 64 20 29 7b 0a    switch( id ){.
2ab70 20 20 20 20 63 61 73 65 20 54 4b 5f 41 4c 4c 3a      case TK_ALL:
2ab71 20 20 20 20 20 20 20 7a 20 3d 20 22 55 4e 49 4f         z = "UNIO
2ab72 4e 20 41 4c 4c 22 3b 20 20 20 62 72 65 61 6b 3b  N ALL";   break;
2ab73 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54  .    case TK_INT
2ab74 45 52 53 45 43 54 3a 20 7a 20 3d 20 22 49 4e 54  ERSECT: z = "INT
2ab75 45 52 53 45 43 54 22 3b 20 20 20 62 72 65 61 6b  ERSECT";   break
2ab76 3b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45 58  ;.    case TK_EX
2ab77 43 45 50 54 3a 20 20 20 20 7a 20 3d 20 22 45 58  CEPT:    z = "EX
2ab78 43 45 50 54 22 3b 20 20 20 20 20 20 62 72 65 61  CEPT";      brea
2ab79 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20  k;.    default: 
2ab7a 20 20 20 20 20 20 20 20 20 20 7a 20 3d 20 22 55            z = "U
2ab7b 4e 49 4f 4e 22 3b 20 20 20 20 20 20 20 62 72 65  NION";       bre
2ab7c 61 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ak;.  }.  return
2ab7d 20 7a 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20   z;.}.#endif /* 
2ab7e 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50  SQLITE_OMIT_COMP
2ab7f 4f 55 4e 44 5f 53 45 4c 45 43 54 20 2a 2f 0a 0a  OUND_SELECT */..
2ab80 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 61 6e  /*.** Given a an
2ab81 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
2ab82 20 28 77 68 69 63 68 20 69 73 20 72 65 61 6c 6c   (which is reall
2ab83 79 20 74 68 65 20 6c 69 73 74 20 6f 66 20 65 78  y the list of ex
2ab84 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 74 68 61  pressions.** tha
2ab85 74 20 66 6f 72 6d 20 74 68 65 20 72 65 73 75 6c  t form the resul
2ab86 74 20 73 65 74 20 6f 66 20 61 20 53 45 4c 45 43  t set of a SELEC
2ab87 54 20 73 74 61 74 65 6d 65 6e 74 29 20 63 6f 6d  T statement) com
2ab88 70 75 74 65 20 61 70 70 72 6f 70 72 69 61 74 65  pute appropriate
2ab89 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  .** column names
2ab8a 20 66 6f 72 20 61 20 74 61 62 6c 65 20 74 68 61   for a table tha
2ab8b 74 20 77 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65  t would hold the
2ab8c 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
2ab8d 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 63 6f 6c 75  ..**.** All colu
2ab8e 6d 6e 20 6e 61 6d 65 73 20 77 69 6c 6c 20 62 65  mn names will be
2ab8f 20 75 6e 69 71 75 65 2e 0a 2a 2a 0a 2a 2a 20 4f   unique..**.** O
2ab90 6e 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e  nly the column n
2ab91 61 6d 65 73 20 61 72 65 20 63 6f 6d 70 75 74 65  ames are compute
2ab92 64 2e 20 20 43 6f 6c 75 6d 6e 2e 7a 54 79 70 65  d.  Column.zType
2ab93 2c 20 43 6f 6c 75 6d 6e 2e 7a 43 6f 6c 6c 2c 0a  , Column.zColl,.
2ab94 2a 2a 20 61 6e 64 20 6f 74 68 65 72 20 66 69 65  ** and other fie
2ab95 6c 64 73 20 6f 66 20 43 6f 6c 75 6d 6e 20 61 72  lds of Column ar
2ab96 65 20 7a 65 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20  e zeroed..**.** 
2ab97 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
2ab98 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 49 66   on success.  If
2ab99 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
2ab9a 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  tion error occur
2ab9b 73 2c 0a 2a 2a 20 73 74 6f 72 65 20 4e 55 4c 4c  s,.** store NULL
2ab9c 20 69 6e 20 2a 70 61 43 6f 6c 20 61 6e 64 20 30   in *paCol and 0
2ab9d 20 69 6e 20 2a 70 6e 43 6f 6c 20 61 6e 64 20 72   in *pnCol and r
2ab9e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
2ab9f 45 4d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  EM..*/.static in
2aba0 74 20 73 65 6c 65 63 74 43 6f 6c 75 6d 6e 73 46  t selectColumnsF
2aba1 72 6f 6d 45 78 70 72 4c 69 73 74 28 0a 20 20 50  romExprList(.  P
2aba2 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2aba3 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
2aba4 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45  g context */.  E
2aba5 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c  xprList *pEList,
2aba6 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 20 6c         /* Expr l
2aba7 69 73 74 20 66 72 6f 6d 20 77 68 69 63 68 20 74  ist from which t
2aba8 6f 20 64 65 72 69 76 65 20 63 6f 6c 75 6d 6e 20  o derive column 
2aba9 6e 61 6d 65 73 20 2a 2f 0a 20 20 69 6e 74 20 2a  names */.  int *
2abaa 70 6e 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20  pnCol,          
2abab 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
2abac 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
2abad 73 20 68 65 72 65 20 2a 2f 0a 20 20 43 6f 6c 75  s here */.  Colu
2abae 6d 6e 20 2a 2a 70 61 43 6f 6c 20 20 20 20 20 20  mn **paCol      
2abaf 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
2abb0 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 6c 69 73 74   new column list
2abb1 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 73 71   here */.){.  sq
2abb2 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2abb3 73 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 44 61 74  se->db;   /* Dat
2abb4 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2abb5 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20   */.  int i, j; 
2abb6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2abb7 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
2abb8 72 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b  rs */.  int cnt;
2abb9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2abba 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 61 64 64      /* Index add
2abbb 65 64 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 6e  ed to make the n
2abbc 61 6d 65 20 75 6e 69 71 75 65 20 2a 2f 0a 20 20  ame unique */.  
2abbd 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 2c 20 2a 70  Column *aCol, *p
2abbe 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 46  Col;        /* F
2abbf 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20  or looping over 
2abc0 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 2a  result columns *
2abc1 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20  /.  int nCol;   
2abc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2abc3 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  /* Number of col
2abc4 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75  umns in the resu
2abc5 6c 74 20 73 65 74 20 2a 2f 0a 20 20 45 78 70 72  lt set */.  Expr
2abc6 20 2a 70 3b 20 20 20 20 20 20 20 20 20 20 20 20   *p;            
2abc7 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65          /* Expre
2abc8 73 73 69 6f 6e 20 66 6f 72 20 61 20 73 69 6e 67  ssion for a sing
2abc9 6c 65 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e  le result column
2abca 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d   */.  char *zNam
2abcb 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
2abcc 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65    /* Column name
2abcd 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b   */.  int nName;
2abce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2abcf 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6e 61 6d    /* Size of nam
2abd0 65 20 69 6e 20 7a 4e 61 6d 65 5b 5d 20 2a 2f 0a  e in zName[] */.
2abd1 0a 20 20 2a 70 6e 43 6f 6c 20 3d 20 6e 43 6f 6c  .  *pnCol = nCol
2abd2 20 3d 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72   = pEList->nExpr
2abd3 3b 0a 20 20 61 43 6f 6c 20 3d 20 2a 70 61 43 6f  ;.  aCol = *paCo
2abd4 6c 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  l = sqlite3DbMal
2abd5 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
2abd6 6f 66 28 61 43 6f 6c 5b 30 5d 29 2a 6e 43 6f 6c  of(aCol[0])*nCol
2abd7 29 3b 0a 20 20 69 66 28 20 61 43 6f 6c 3d 3d 30  );.  if( aCol==0
2abd8 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
2abd9 5f 4e 4f 4d 45 4d 3b 0a 20 20 66 6f 72 28 69 3d  _NOMEM;.  for(i=
2abda 30 2c 20 70 43 6f 6c 3d 61 43 6f 6c 3b 20 69 3c  0, pCol=aCol; i<
2abdb 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c 2b  nCol; i++, pCol+
2abdc 2b 29 7b 0a 20 20 20 20 2f 2a 20 47 65 74 20 61  +){.    /* Get a
2abdd 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 6e 61  n appropriate na
2abde 6d 65 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d  me for the colum
2abdf 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 20 3d  n.    */.    p =
2abe0 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45   pEList->a[i].pE
2abe1 78 70 72 3b 0a 20 20 20 20 61 73 73 65 72 74 28  xpr;.    assert(
2abe2 20 70 2d 3e 70 52 69 67 68 74 3d 3d 30 20 7c 7c   p->pRight==0 ||
2abe3 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74 79   ExprHasProperty
2abe4 28 70 2d 3e 70 52 69 67 68 74 2c 20 45 50 5f 49  (p->pRight, EP_I
2abe5 6e 74 56 61 6c 75 65 29 0a 20 20 20 20 20 20 20  ntValue).       
2abe6 20 20 20 20 20 20 20 20 7c 7c 20 70 2d 3e 70 52          || p->pR
2abe7 69 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3d 3d  ight->u.zToken==
2abe8 30 20 7c 7c 20 70 2d 3e 70 52 69 67 68 74 2d 3e  0 || p->pRight->
2abe9 75 2e 7a 54 6f 6b 65 6e 5b 30 5d 21 3d 30 20 29  u.zToken[0]!=0 )
2abea 3b 0a 20 20 20 20 69 66 28 20 28 7a 4e 61 6d 65  ;.    if( (zName
2abeb 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e   = pEList->a[i].
2abec 7a 4e 61 6d 65 29 21 3d 30 20 29 7b 0a 20 20 20  zName)!=0 ){.   
2abed 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f 6c     /* If the col
2abee 75 6d 6e 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20  umn contains an 
2abef 22 41 53 20 3c 6e 61 6d 65 3e 22 20 70 68 72 61  "AS <name>" phra
2abf0 73 65 2c 20 75 73 65 20 3c 6e 61 6d 65 3e 20 61  se, use <name> a
2abf1 73 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  s the name */.  
2abf2 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69      zName = sqli
2abf3 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
2abf4 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73  zName);.    }els
2abf5 65 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  e{.      Expr *p
2abf6 43 6f 6c 45 78 70 72 20 3d 20 70 3b 20 20 2f 2a  ColExpr = p;  /*
2abf7 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
2abf8 74 68 61 74 20 69 73 20 74 68 65 20 72 65 73 75  that is the resu
2abf9 6c 74 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a  lt column name *
2abfa 2f 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70  /.      Table *p
2abfb 54 61 62 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Tab;         /* 
2abfc 54 61 62 6c 65 20 61 73 73 6f 63 69 61 74 65 64  Table associated
2abfd 20 77 69 74 68 20 74 68 69 73 20 65 78 70 72 65   with this expre
2abfe 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 77  ssion */.      w
2abff 68 69 6c 65 28 20 70 43 6f 6c 45 78 70 72 2d 3e  hile( pColExpr->
2ac00 6f 70 3d 3d 54 4b 5f 44 4f 54 20 29 20 70 43 6f  op==TK_DOT ) pCo
2ac01 6c 45 78 70 72 20 3d 20 70 43 6f 6c 45 78 70 72  lExpr = pColExpr
2ac02 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20  ->pRight;.      
2ac03 69 66 28 20 70 43 6f 6c 45 78 70 72 2d 3e 6f 70  if( pColExpr->op
2ac04 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 41  ==TK_COLUMN && A
2ac05 4c 57 41 59 53 28 70 43 6f 6c 45 78 70 72 2d 3e  LWAYS(pColExpr->
2ac06 70 54 61 62 21 3d 30 29 20 29 7b 0a 20 20 20 20  pTab!=0) ){.    
2ac07 20 20 20 20 2f 2a 20 46 6f 72 20 63 6f 6c 75 6d      /* For colum
2ac08 6e 73 20 75 73 65 20 74 68 65 20 63 6f 6c 75 6d  ns use the colum
2ac09 6e 20 6e 61 6d 65 20 6e 61 6d 65 20 2a 2f 0a 20  n name name */. 
2ac0a 20 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20         int iCol 
2ac0b 3d 20 70 43 6f 6c 45 78 70 72 2d 3e 69 43 6f 6c  = pColExpr->iCol
2ac0c 75 6d 6e 3b 0a 20 20 20 20 20 20 20 20 70 54 61  umn;.        pTa
2ac0d 62 20 3d 20 70 43 6f 6c 45 78 70 72 2d 3e 70 54  b = pColExpr->pT
2ac0e 61 62 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ab;.        if( 
2ac0f 69 43 6f 6c 3c 30 20 29 20 69 43 6f 6c 20 3d 20  iCol<0 ) iCol = 
2ac10 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 20  pTab->iPKey;.   
2ac11 20 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c       zName = sql
2ac12 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
2ac13 22 25 73 22 2c 0a 20 20 20 20 20 20 20 20 20 20  "%s",.          
2ac14 20 20 20 20 20 20 20 69 43 6f 6c 3e 3d 30 20 3f         iCol>=0 ?
2ac15 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c   pTab->aCol[iCol
2ac16 5d 2e 7a 4e 61 6d 65 20 3a 20 22 72 6f 77 69 64  ].zName : "rowid
2ac17 22 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  ");.      }else 
2ac18 69 66 28 20 70 43 6f 6c 45 78 70 72 2d 3e 6f 70  if( pColExpr->op
2ac19 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 20 20 20  ==TK_ID ){.     
2ac1a 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
2ac1b 48 61 73 50 72 6f 70 65 72 74 79 28 70 43 6f 6c  HasProperty(pCol
2ac1c 45 78 70 72 2c 20 45 50 5f 49 6e 74 56 61 6c 75  Expr, EP_IntValu
2ac1d 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20 7a 4e  e) );.        zN
2ac1e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ame = sqlite3MPr
2ac1f 69 6e 74 66 28 64 62 2c 20 22 25 73 22 2c 20 70  intf(db, "%s", p
2ac20 43 6f 6c 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65  ColExpr->u.zToke
2ac21 6e 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  n);.      }else{
2ac22 0a 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 20  .        /* Use 
2ac23 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 65 78  the original tex
2ac24 74 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20  t of the column 
2ac25 65 78 70 72 65 73 73 69 6f 6e 20 61 73 20 69 74  expression as it
2ac26 73 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 20 20  s name */.      
2ac27 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
2ac28 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73  3MPrintf(db, "%s
2ac29 22 2c 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e  ", pEList->a[i].
2ac2a 7a 53 70 61 6e 29 3b 0a 20 20 20 20 20 20 7d 0a  zSpan);.      }.
2ac2b 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64 62      }.    if( db
2ac2c 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2ac2d 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  {.      sqlite3D
2ac2e 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29  bFree(db, zName)
2ac2f 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
2ac30 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b     }..    /* Mak
2ac31 65 20 73 75 72 65 20 74 68 65 20 63 6f 6c 75 6d  e sure the colum
2ac32 6e 20 6e 61 6d 65 20 69 73 20 75 6e 69 71 75 65  n name is unique
2ac33 2e 20 20 49 66 20 74 68 65 20 6e 61 6d 65 20 69  .  If the name i
2ac34 73 20 6e 6f 74 20 75 6e 69 71 75 65 2c 0a 20 20  s not unique,.  
2ac35 20 20 2a 2a 20 61 70 70 65 6e 64 20 61 20 69 6e    ** append a in
2ac36 74 65 67 65 72 20 74 6f 20 74 68 65 20 6e 61 6d  teger to the nam
2ac37 65 20 73 6f 20 74 68 61 74 20 69 74 20 62 65 63  e so that it bec
2ac38 6f 6d 65 73 20 75 6e 69 71 75 65 2e 0a 20 20 20  omes unique..   
2ac39 20 2a 2f 0a 20 20 20 20 6e 4e 61 6d 65 20 3d 20   */.    nName = 
2ac3a 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28  sqlite3Strlen30(
2ac3b 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 66 6f 72 28  zName);.    for(
2ac3c 6a 3d 63 6e 74 3d 30 3b 20 6a 3c 69 3b 20 6a 2b  j=cnt=0; j<i; j+
2ac3d 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71  +){.      if( sq
2ac3e 6c 69 74 65 33 53 74 72 49 43 6d 70 28 61 43 6f  lite3StrICmp(aCo
2ac3f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d  l[j].zName, zNam
2ac40 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  e)==0 ){.       
2ac41 20 63 68 61 72 20 2a 7a 4e 65 77 4e 61 6d 65 3b   char *zNewName;
2ac42 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 5b 6e  .        zName[n
2ac43 4e 61 6d 65 5d 20 3d 20 30 3b 0a 20 20 20 20 20  Name] = 0;.     
2ac44 20 20 20 7a 4e 65 77 4e 61 6d 65 20 3d 20 73 71     zNewName = sq
2ac45 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c  lite3MPrintf(db,
2ac46 20 22 25 73 3a 25 64 22 2c 20 7a 4e 61 6d 65 2c   "%s:%d", zName,
2ac47 20 2b 2b 63 6e 74 29 3b 0a 20 20 20 20 20 20 20   ++cnt);.       
2ac48 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2ac49 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  b, zName);.     
2ac4a 20 20 20 7a 4e 61 6d 65 20 3d 20 7a 4e 65 77 4e     zName = zNewN
2ac4b 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 6a 20 3d  ame;.        j =
2ac4c 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 69 66 28   -1;.        if(
2ac4d 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 62 72 65 61   zName==0 ) brea
2ac4e 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
2ac4f 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65  .    pCol->zName
2ac50 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 7d 0a 20 20   = zName;.  }.  
2ac51 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
2ac52 69 6c 65 64 20 29 7b 0a 20 20 20 20 66 6f 72 28  iled ){.    for(
2ac53 6a 3d 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a  j=0; j<i; j++){.
2ac54 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
2ac55 72 65 65 28 64 62 2c 20 61 43 6f 6c 5b 6a 5d 2e  ree(db, aCol[j].
2ac56 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20  zName);.    }.  
2ac57 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2ac58 64 62 2c 20 61 43 6f 6c 29 3b 0a 20 20 20 20 2a  db, aCol);.    *
2ac59 70 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 2a  paCol = 0;.    *
2ac5a 70 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 72  pnCol = 0;.    r
2ac5b 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
2ac5c 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  EM;.  }.  return
2ac5d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
2ac5e 2a 0a 2a 2a 20 41 64 64 20 74 79 70 65 20 61 6e  *.** Add type an
2ac5f 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 6e 66 6f  d collation info
2ac60 72 6d 61 74 69 6f 6e 20 74 6f 20 61 20 63 6f 6c  rmation to a col
2ac61 75 6d 6e 20 6c 69 73 74 20 62 61 73 65 64 20 6f  umn list based o
2ac62 6e 0a 2a 2a 20 61 20 53 45 4c 45 43 54 20 73 74  n.** a SELECT st
2ac63 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 0a 2a 2a 20  atement..** .** 
2ac64 54 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74 20  The column list 
2ac65 70 72 65 73 75 6d 61 62 6c 79 20 63 61 6d 65 20  presumably came 
2ac66 66 72 6f 6d 20 73 65 6c 65 63 74 43 6f 6c 75 6d  from selectColum
2ac67 6e 4e 61 6d 65 73 46 72 6f 6d 45 78 70 72 4c 69  nNamesFromExprLi
2ac68 73 74 28 29 2e 0a 2a 2a 20 54 68 65 20 63 6f 6c  st()..** The col
2ac69 75 6d 6e 20 6c 69 73 74 20 68 61 73 20 6f 6e 6c  umn list has onl
2ac6a 79 20 6e 61 6d 65 73 2c 20 6e 6f 74 20 74 79 70  y names, not typ
2ac6b 65 73 20 6f 72 20 63 6f 6c 6c 61 74 69 6f 6e 73  es or collations
2ac6c 2e 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69  .  This.** routi
2ac6d 6e 65 20 67 6f 65 73 20 74 68 72 6f 75 67 68 20  ne goes through 
2ac6e 61 6e 64 20 61 64 64 73 20 74 68 65 20 74 79 70  and adds the typ
2ac6f 65 73 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e  es and collation
2ac70 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  s..**.** This ro
2ac71 75 74 69 6e 65 20 72 65 71 75 69 72 65 73 20 74  utine requires t
2ac72 68 61 74 20 61 6c 6c 20 69 64 65 6e 74 69 66 69  hat all identifi
2ac73 65 72 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43  ers in the SELEC
2ac74 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 62  T.** statement b
2ac75 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a 2f 0a 73  e resolved..*/.s
2ac76 74 61 74 69 63 20 76 6f 69 64 20 73 65 6c 65 63  tatic void selec
2ac77 74 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 41 6e  tAddColumnTypeAn
2ac78 64 43 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 50 61  dCollation(.  Pa
2ac79 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2ac7a 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2ac7b 6f 6e 74 65 78 74 73 20 2a 2f 0a 20 20 69 6e 74  ontexts */.  int
2ac7c 20 6e 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20   nCol,          
2ac7d 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2ac7e 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 43 6f 6c  columns */.  Col
2ac7f 75 6d 6e 20 2a 61 43 6f 6c 2c 20 20 20 20 20 20  umn *aCol,      
2ac80 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 63 6f     /* List of co
2ac81 6c 75 6d 6e 73 20 2a 2f 0a 20 20 53 65 6c 65 63  lumns */.  Selec
2ac82 74 20 2a 70 53 65 6c 65 63 74 20 20 20 20 20 20  t *pSelect      
2ac83 20 2f 2a 20 53 45 4c 45 43 54 20 75 73 65 64 20   /* SELECT used 
2ac84 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74 79 70  to determine typ
2ac85 65 73 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e  es and collation
2ac86 73 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  s */.){.  sqlite
2ac87 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2ac88 64 62 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78  db;.  NameContex
2ac89 74 20 73 4e 43 3b 0a 20 20 43 6f 6c 75 6d 6e 20  t sNC;.  Column 
2ac8a 2a 70 43 6f 6c 3b 0a 20 20 43 6f 6c 6c 53 65 71  *pCol;.  CollSeq
2ac8b 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 6e 74 20 69   *pColl;.  int i
2ac8c 3b 0a 20 20 45 78 70 72 20 2a 70 3b 0a 20 20 73  ;.  Expr *p;.  s
2ac8d 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
2ac8e 74 65 6d 20 2a 61 3b 0a 0a 20 20 61 73 73 65 72  tem *a;..  asser
2ac8f 74 28 20 70 53 65 6c 65 63 74 21 3d 30 20 29 3b  t( pSelect!=0 );
2ac90 0a 20 20 61 73 73 65 72 74 28 20 28 70 53 65 6c  .  assert( (pSel
2ac91 65 63 74 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20  ect->selFlags & 
2ac92 53 46 5f 52 65 73 6f 6c 76 65 64 29 21 3d 30 20  SF_Resolved)!=0 
2ac93 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 43 6f  );.  assert( nCo
2ac94 6c 3d 3d 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69  l==pSelect->pELi
2ac95 73 74 2d 3e 6e 45 78 70 72 20 7c 7c 20 64 62 2d  st->nExpr || db-
2ac96 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
2ac97 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
2ac98 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e  cFailed ) return
2ac99 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c  ;.  memset(&sNC,
2ac9a 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29   0, sizeof(sNC))
2ac9b 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74  ;.  sNC.pSrcList
2ac9c 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53 72 63   = pSelect->pSrc
2ac9d 3b 0a 20 20 61 20 3d 20 70 53 65 6c 65 63 74 2d  ;.  a = pSelect-
2ac9e 3e 70 45 4c 69 73 74 2d 3e 61 3b 0a 20 20 66 6f  >pEList->a;.  fo
2ac9f 72 28 69 3d 30 2c 20 70 43 6f 6c 3d 61 43 6f 6c  r(i=0, pCol=aCol
2aca0 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70  ; i<nCol; i++, p
2aca1 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20  Col++){.    p = 
2aca2 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[i].pExpr;.    
2aca3 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20 73 71  pCol->zType = sq
2aca4 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
2aca5 2c 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26 73 4e  , columnType(&sN
2aca6 43 2c 20 70 2c 20 30 2c 20 30 2c 20 30 29 29 3b  C, p, 0, 0, 0));
2aca7 0a 20 20 20 20 70 43 6f 6c 2d 3e 61 66 66 69 6e  .    pCol->affin
2aca8 69 74 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ity = sqlite3Exp
2aca9 72 41 66 66 69 6e 69 74 79 28 70 29 3b 0a 20 20  rAffinity(p);.  
2acaa 20 20 69 66 28 20 70 43 6f 6c 2d 3e 61 66 66 69    if( pCol->affi
2acab 6e 69 74 79 3d 3d 30 20 29 20 70 43 6f 6c 2d 3e  nity==0 ) pCol->
2acac 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54  affinity = SQLIT
2acad 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20  E_AFF_NONE;.    
2acae 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45  pColl = sqlite3E
2acaf 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  xprCollSeq(pPars
2acb0 65 2c 20 70 29 3b 0a 20 20 20 20 69 66 28 20 70  e, p);.    if( p
2acb1 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 43  Coll ){.      pC
2acb2 6f 6c 2d 3e 7a 43 6f 6c 6c 20 3d 20 73 71 6c 69  ol->zColl = sqli
2acb3 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
2acb4 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  pColl->zName);. 
2acb5 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a     }.  }.}../*.*
2acb6 2a 20 47 69 76 65 6e 20 61 20 53 45 4c 45 43 54  * Given a SELECT
2acb7 20 73 74 61 74 65 6d 65 6e 74 2c 20 67 65 6e 65   statement, gene
2acb8 72 61 74 65 20 61 20 54 61 62 6c 65 20 73 74 72  rate a Table str
2acb9 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73 63  ucture that desc
2acba 72 69 62 65 73 0a 2a 2a 20 74 68 65 20 72 65 73  ribes.** the res
2acbb 75 6c 74 20 73 65 74 20 6f 66 20 74 68 61 74 20  ult set of that 
2acbc 53 45 4c 45 43 54 2e 0a 2a 2f 0a 53 51 4c 49 54  SELECT..*/.SQLIT
2acbd 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20  E_PRIVATE Table 
2acbe 2a 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53 65  *sqlite3ResultSe
2acbf 74 4f 66 53 65 6c 65 63 74 28 50 61 72 73 65 20  tOfSelect(Parse 
2acc0 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20  *pParse, Select 
2acc1 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20 54 61 62  *pSelect){.  Tab
2acc2 6c 65 20 2a 70 54 61 62 3b 0a 20 20 73 71 6c 69  le *pTab;.  sqli
2acc3 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2acc4 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 73 61 76 65  ->db;.  int save
2acc5 64 46 6c 61 67 73 3b 0a 0a 20 20 73 61 76 65 64  dFlags;..  saved
2acc6 46 6c 61 67 73 20 3d 20 64 62 2d 3e 66 6c 61 67  Flags = db->flag
2acc7 73 3b 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 26  s;.  db->flags &
2acc8 3d 20 7e 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f  = ~SQLITE_FullCo
2acc9 6c 4e 61 6d 65 73 3b 0a 20 20 64 62 2d 3e 66 6c  lNames;.  db->fl
2acca 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 53 68  ags |= SQLITE_Sh
2accb 6f 72 74 43 6f 6c 4e 61 6d 65 73 3b 0a 20 20 73  ortColNames;.  s
2accc 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70  qlite3SelectPrep
2accd 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74  (pParse, pSelect
2acce 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 50 61 72  , 0);.  if( pPar
2accf 73 65 2d 3e 6e 45 72 72 20 29 20 72 65 74 75 72  se->nErr ) retur
2acd0 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 70 53  n 0;.  while( pS
2acd1 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 20 29 20  elect->pPrior ) 
2acd2 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63  pSelect = pSelec
2acd3 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 64 62 2d  t->pPrior;.  db-
2acd4 3e 66 6c 61 67 73 20 3d 20 73 61 76 65 64 46 6c  >flags = savedFl
2acd5 61 67 73 3b 0a 20 20 70 54 61 62 20 3d 20 73 71  ags;.  pTab = sq
2acd6 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
2acd7 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 61 62  o(db, sizeof(Tab
2acd8 6c 65 29 20 29 3b 0a 20 20 69 66 28 20 70 54 61  le) );.  if( pTa
2acd9 62 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  b==0 ){.    retu
2acda 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 2f 2a 20 54  rn 0;.  }.  /* T
2acdb 68 65 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74  he sqlite3Result
2acdc 53 65 74 4f 66 53 65 6c 65 63 74 28 29 20 69 73  SetOfSelect() is
2acdd 20 6f 6e 6c 79 20 75 73 65 64 20 6e 20 63 6f 6e   only used n con
2acde 74 65 78 74 73 20 77 68 65 72 65 20 6c 6f 6f 6b  texts where look
2acdf 61 73 69 64 65 0a 20 20 2a 2a 20 69 73 20 64 69  aside.  ** is di
2ace0 73 61 62 6c 65 64 2c 20 73 6f 20 77 65 20 6d 69  sabled, so we mi
2ace1 67 68 74 20 61 73 20 77 65 6c 6c 20 68 61 72 64  ght as well hard
2ace2 2d 63 6f 64 65 20 70 54 61 62 2d 3e 64 62 4d 65  -code pTab->dbMe
2ace3 6d 20 74 6f 20 4e 55 4c 4c 2e 20 2a 2f 0a 20 20  m to NULL. */.  
2ace4 61 73 73 65 72 74 28 20 64 62 2d 3e 6c 6f 6f 6b  assert( db->look
2ace5 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 3d 3d  aside.bEnabled==
2ace6 30 20 29 3b 0a 20 20 70 54 61 62 2d 3e 64 62 4d  0 );.  pTab->dbM
2ace7 65 6d 20 3d 20 30 3b 0a 20 20 70 54 61 62 2d 3e  em = 0;.  pTab->
2ace8 6e 52 65 66 20 3d 20 31 3b 0a 20 20 70 54 61 62  nRef = 1;.  pTab
2ace9 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 73  ->zName = 0;.  s
2acea 65 6c 65 63 74 43 6f 6c 75 6d 6e 73 46 72 6f 6d  electColumnsFrom
2aceb 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c  ExprList(pParse,
2acec 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74   pSelect->pEList
2aced 2c 20 26 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 26  , &pTab->nCol, &
2acee 70 54 61 62 2d 3e 61 43 6f 6c 29 3b 0a 20 20 73  pTab->aCol);.  s
2acef 65 6c 65 63 74 41 64 64 43 6f 6c 75 6d 6e 54 79  electAddColumnTy
2acf0 70 65 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e 28 70  peAndCollation(p
2acf1 50 61 72 73 65 2c 20 70 54 61 62 2d 3e 6e 43 6f  Parse, pTab->nCo
2acf2 6c 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 2c 20 70  l, pTab->aCol, p
2acf3 53 65 6c 65 63 74 29 3b 0a 20 20 70 54 61 62 2d  Select);.  pTab-
2acf4 3e 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 69  >iPKey = -1;.  i
2acf5 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
2acf6 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  led ){.    sqlit
2acf7 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 54  e3DeleteTable(pT
2acf8 61 62 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ab);.    return 
2acf9 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
2acfa 70 54 61 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  pTab;.}../*.** G
2acfb 65 74 20 61 20 56 44 42 45 20 66 6f 72 20 74 68  et a VDBE for th
2acfc 65 20 67 69 76 65 6e 20 70 61 72 73 65 72 20 63  e given parser c
2acfd 6f 6e 74 65 78 74 2e 20 20 43 72 65 61 74 65 20  ontext.  Create 
2acfe 61 20 6e 65 77 20 6f 6e 65 20 69 66 20 6e 65 63  a new one if nec
2acff 65 73 73 61 72 79 2e 0a 2a 2a 20 49 66 20 61 6e  essary..** If an
2ad00 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 72   error occurs, r
2ad01 65 74 75 72 6e 20 4e 55 4c 4c 20 61 6e 64 20 6c  eturn NULL and l
2ad02 65 61 76 65 20 61 20 6d 65 73 73 61 67 65 20 69  eave a message i
2ad03 6e 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c  n pParse..*/.SQL
2ad04 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62 65  ITE_PRIVATE Vdbe
2ad05 20 2a 73 71 6c 69 74 65 33 47 65 74 56 64 62 65   *sqlite3GetVdbe
2ad06 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b  (Parse *pParse){
2ad07 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61  .  Vdbe *v = pPa
2ad08 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 66  rse->pVdbe;.  if
2ad09 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20 20 76 20  ( v==0 ){.    v 
2ad0a 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20  = pParse->pVdbe 
2ad0b 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 72 65  = sqlite3VdbeCre
2ad0c 61 74 65 28 70 50 61 72 73 65 2d 3e 64 62 29 3b  ate(pParse->db);
2ad0d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2ad0e 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 20 20 69  OMIT_TRACE.    i
2ad0f 66 28 20 76 20 29 7b 0a 20 20 20 20 20 20 73 71  f( v ){.      sq
2ad10 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28  lite3VdbeAddOp0(
2ad11 76 2c 20 4f 50 5f 54 72 61 63 65 29 3b 0a 20 20  v, OP_Trace);.  
2ad12 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20    }.#endif.  }. 
2ad13 20 72 65 74 75 72 6e 20 76 3b 0a 7d 0a 0a 0a 2f   return v;.}.../
2ad14 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65  *.** Compute the
2ad15 20 69 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66 66   iLimit and iOff
2ad16 73 65 74 20 66 69 65 6c 64 73 20 6f 66 20 74 68  set fields of th
2ad17 65 20 53 45 4c 45 43 54 20 62 61 73 65 64 20 6f  e SELECT based o
2ad18 6e 20 74 68 65 0a 2a 2a 20 70 4c 69 6d 69 74 20  n the.** pLimit 
2ad19 61 6e 64 20 70 4f 66 66 73 65 74 20 65 78 70 72  and pOffset expr
2ad1a 65 73 73 69 6f 6e 73 2e 20 20 70 4c 69 6d 69 74  essions.  pLimit
2ad1b 20 61 6e 64 20 70 4f 66 66 73 65 74 20 68 6f 6c   and pOffset hol
2ad1c 64 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  d the expression
2ad1d 73 0a 2a 2a 20 74 68 61 74 20 61 70 70 65 61 72  s.** that appear
2ad1e 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   in the original
2ad1f 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61   SQL statement a
2ad20 66 74 65 72 20 74 68 65 20 4c 49 4d 49 54 20 61  fter the LIMIT a
2ad21 6e 64 20 4f 46 46 53 45 54 0a 2a 2a 20 6b 65 79  nd OFFSET.** key
2ad22 77 6f 72 64 73 2e 20 20 4f 72 20 4e 55 4c 4c 20  words.  Or NULL 
2ad23 69 66 20 74 68 6f 73 65 20 6b 65 79 77 6f 72 64  if those keyword
2ad24 73 20 61 72 65 20 6f 6d 69 74 74 65 64 2e 20 69  s are omitted. i
2ad25 4c 69 6d 69 74 20 61 6e 64 20 69 4f 66 66 73 65  Limit and iOffse
2ad26 74 20 0a 2a 2a 20 61 72 65 20 74 68 65 20 69 6e  t .** are the in
2ad27 74 65 67 65 72 20 6d 65 6d 6f 72 79 20 72 65 67  teger memory reg
2ad28 69 73 74 65 72 20 6e 75 6d 62 65 72 73 20 66 6f  ister numbers fo
2ad29 72 20 63 6f 75 6e 74 65 72 73 20 75 73 65 64 20  r counters used 
2ad2a 74 6f 20 63 6f 6d 70 75 74 65 20 0a 2a 2a 20 74  to compute .** t
2ad2b 68 65 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66  he limit and off
2ad2c 73 65 74 2e 20 20 49 66 20 74 68 65 72 65 20 69  set.  If there i
2ad2d 73 20 6e 6f 20 6c 69 6d 69 74 20 61 6e 64 2f 6f  s no limit and/o
2ad2e 72 20 6f 66 66 73 65 74 2c 20 74 68 65 6e 20 0a  r offset, then .
2ad2f 2a 2a 20 69 4c 69 6d 69 74 20 61 6e 64 20 69 4f  ** iLimit and iO
2ad30 66 66 73 65 74 20 61 72 65 20 6e 65 67 61 74 69  ffset are negati
2ad31 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ve..**.** This r
2ad32 6f 75 74 69 6e 65 20 63 68 61 6e 67 65 73 20 74  outine changes t
2ad33 68 65 20 76 61 6c 75 65 73 20 6f 66 20 69 4c 69  he values of iLi
2ad34 6d 69 74 20 61 6e 64 20 69 4f 66 66 73 65 74 20  mit and iOffset 
2ad35 6f 6e 6c 79 20 69 66 0a 2a 2a 20 61 20 6c 69 6d  only if.** a lim
2ad36 69 74 20 6f 72 20 6f 66 66 73 65 74 20 69 73 20  it or offset is 
2ad37 64 65 66 69 6e 65 64 20 62 79 20 70 4c 69 6d 69  defined by pLimi
2ad38 74 20 61 6e 64 20 70 4f 66 66 73 65 74 2e 20 20  t and pOffset.  
2ad39 69 4c 69 6d 69 74 20 61 6e 64 0a 2a 2a 20 69 4f  iLimit and.** iO
2ad3a 66 66 73 65 74 20 73 68 6f 75 6c 64 20 68 61 76  ffset should hav
2ad3b 65 20 62 65 65 6e 20 70 72 65 73 65 74 20 74 6f  e been preset to
2ad3c 20 61 70 70 72 6f 70 72 69 61 74 65 20 64 65 66   appropriate def
2ad3d 61 75 6c 74 20 76 61 6c 75 65 73 0a 2a 2a 20 28  ault values.** (
2ad3e 75 73 75 61 6c 6c 79 20 62 75 74 20 6e 6f 74 20  usually but not 
2ad3f 61 6c 77 61 79 73 20 2d 31 29 20 70 72 69 6f 72  always -1) prior
2ad40 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74 68 69 73   to calling this
2ad41 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 20 4f 6e 6c   routine..** Onl
2ad42 79 20 69 66 20 70 4c 69 6d 69 74 21 3d 30 20 6f  y if pLimit!=0 o
2ad43 72 20 70 4f 66 66 73 65 74 21 3d 30 20 64 6f 20  r pOffset!=0 do 
2ad44 74 68 65 20 6c 69 6d 69 74 20 72 65 67 69 73 74  the limit regist
2ad45 65 72 73 20 67 65 74 0a 2a 2a 20 72 65 64 65 66  ers get.** redef
2ad46 69 6e 65 64 2e 20 20 54 68 65 20 55 4e 49 4f 4e  ined.  The UNION
2ad47 20 41 4c 4c 20 6f 70 65 72 61 74 6f 72 20 75 73   ALL operator us
2ad48 65 73 20 74 68 69 73 20 70 72 6f 70 65 72 74 79  es this property
2ad49 20 74 6f 20 66 6f 72 63 65 0a 2a 2a 20 74 68 65   to force.** the
2ad4a 20 72 65 75 73 65 20 6f 66 20 74 68 65 20 73 61   reuse of the sa
2ad4b 6d 65 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66  me limit and off
2ad4c 73 65 74 20 72 65 67 69 73 74 65 72 73 20 61 63  set registers ac
2ad4d 72 6f 73 73 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a  ross multiple.**
2ad4e 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2ad4f 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ts..*/.static vo
2ad50 69 64 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52  id computeLimitR
2ad51 65 67 69 73 74 65 72 73 28 50 61 72 73 65 20 2a  egisters(Parse *
2ad52 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a  pParse, Select *
2ad53 70 2c 20 69 6e 74 20 69 42 72 65 61 6b 29 7b 0a  p, int iBreak){.
2ad54 20 20 56 64 62 65 20 2a 76 20 3d 20 30 3b 0a 20    Vdbe *v = 0;. 
2ad55 20 69 6e 74 20 69 4c 69 6d 69 74 20 3d 20 30 3b   int iLimit = 0;
2ad56 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 0a  .  int iOffset;.
2ad57 20 20 69 6e 74 20 61 64 64 72 31 3b 0a 20 20 69    int addr1;.  i
2ad58 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20 29 20 72  f( p->iLimit ) r
2ad59 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 0a 20 20  eturn;..  /* .  
2ad5a 2a 2a 20 22 4c 49 4d 49 54 20 2d 31 22 20 61 6c  ** "LIMIT -1" al
2ad5b 77 61 79 73 20 73 68 6f 77 73 20 61 6c 6c 20 72  ways shows all r
2ad5c 6f 77 73 2e 20 20 54 68 65 72 65 20 69 73 20 73  ows.  There is s
2ad5d 6f 6d 65 0a 20 20 2a 2a 20 63 6f 6e 74 72 61 76  ome.  ** contrav
2ad5e 65 72 73 79 20 61 62 6f 75 74 20 77 68 61 74 20  ersy about what 
2ad5f 74 68 65 20 63 6f 72 72 65 63 74 20 62 65 68 61  the correct beha
2ad60 76 69 6f 72 20 73 68 6f 75 6c 64 20 62 65 2e 0a  vior should be..
2ad61 20 20 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74    ** The current
2ad62 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
2ad63 69 6e 74 65 72 70 72 65 74 73 20 22 4c 49 4d 49  interprets "LIMI
2ad64 54 20 30 22 20 74 6f 20 6d 65 61 6e 0a 20 20 2a  T 0" to mean.  *
2ad65 2a 20 6e 6f 20 72 6f 77 73 2e 0a 20 20 2a 2f 0a  * no rows..  */.
2ad66 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63    sqlite3ExprCac
2ad67 68 65 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b  heClear(pParse);
2ad68 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f  .  assert( p->pO
2ad69 66 66 73 65 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70  ffset==0 || p->p
2ad6a 4c 69 6d 69 74 21 3d 30 20 29 3b 0a 20 20 69 66  Limit!=0 );.  if
2ad6b 28 20 70 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20  ( p->pLimit ){. 
2ad6c 20 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d 20 69     p->iLimit = i
2ad6d 4c 69 6d 69 74 20 3d 20 2b 2b 70 50 61 72 73 65  Limit = ++pParse
2ad6e 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 76 20 3d 20  ->nMem;.    v = 
2ad6f 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2ad70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66 28 20  Parse);.    if( 
2ad71 4e 45 56 45 52 28 76 3d 3d 30 29 20 29 20 72 65  NEVER(v==0) ) re
2ad72 74 75 72 6e 3b 20 20 2f 2a 20 56 44 42 45 20 73  turn;  /* VDBE s
2ad73 68 6f 75 6c 64 20 68 61 76 65 20 61 6c 72 65 61  hould have alrea
2ad74 64 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65  dy been allocate
2ad75 64 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  d */.    sqlite3
2ad76 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
2ad77 20 70 2d 3e 70 4c 69 6d 69 74 2c 20 69 4c 69 6d   p->pLimit, iLim
2ad78 69 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  it);.    sqlite3
2ad79 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2ad7a 5f 4d 75 73 74 42 65 49 6e 74 2c 20 69 4c 69 6d  _MustBeInt, iLim
2ad7b 69 74 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d  it);.    VdbeCom
2ad7c 6d 65 6e 74 28 28 76 2c 20 22 4c 49 4d 49 54 20  ment((v, "LIMIT 
2ad7d 63 6f 75 6e 74 65 72 22 29 29 3b 0a 20 20 20 20  counter"));.    
2ad7e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2ad7f 32 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f 2c 20  2(v, OP_IfZero, 
2ad80 69 4c 69 6d 69 74 2c 20 69 42 72 65 61 6b 29 3b  iLimit, iBreak);
2ad81 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 4f 66 66  .    if( p->pOff
2ad82 73 65 74 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  set ){.      p->
2ad83 69 4f 66 66 73 65 74 20 3d 20 69 4f 66 66 73 65  iOffset = iOffse
2ad84 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  t = ++pParse->nM
2ad85 65 6d 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65  em;.      pParse
2ad86 2d 3e 6e 4d 65 6d 2b 2b 3b 20 20 20 2f 2a 20 41  ->nMem++;   /* A
2ad87 6c 6c 6f 63 61 74 65 20 61 6e 20 65 78 74 72 61  llocate an extra
2ad88 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 6c 69   register for li
2ad89 6d 69 74 2b 6f 66 66 73 65 74 20 2a 2f 0a 20 20  mit+offset */.  
2ad8a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2ad8b 6f 64 65 28 70 50 61 72 73 65 2c 20 70 2d 3e 70  ode(pParse, p->p
2ad8c 4f 66 66 73 65 74 2c 20 69 4f 66 66 73 65 74 29  Offset, iOffset)
2ad8d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2ad8e 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2ad8f 4d 75 73 74 42 65 49 6e 74 2c 20 69 4f 66 66 73  MustBeInt, iOffs
2ad90 65 74 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43  et);.      VdbeC
2ad91 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4f 46 46 53  omment((v, "OFFS
2ad92 45 54 20 63 6f 75 6e 74 65 72 22 29 29 3b 0a 20  ET counter"));. 
2ad93 20 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c       addr1 = sql
2ad94 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2ad95 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 69 4f 66 66  , OP_IfPos, iOff
2ad96 73 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  set);.      sqli
2ad97 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2ad98 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20   OP_Integer, 0, 
2ad99 69 4f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20  iOffset);.      
2ad9a 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
2ad9b 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b 0a 20  ere(v, addr1);. 
2ad9c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2ad9d 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64 64  AddOp3(v, OP_Add
2ad9e 2c 20 69 4c 69 6d 69 74 2c 20 69 4f 66 66 73 65  , iLimit, iOffse
2ad9f 74 2c 20 69 4f 66 66 73 65 74 2b 31 29 3b 0a 20  t, iOffset+1);. 
2ada0 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
2ada1 28 28 76 2c 20 22 4c 49 4d 49 54 2b 4f 46 46 53  ((v, "LIMIT+OFFS
2ada2 45 54 22 29 29 3b 0a 20 20 20 20 20 20 61 64 64  ET"));.      add
2ada3 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  r1 = sqlite3Vdbe
2ada4 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 50  AddOp1(v, OP_IfP
2ada5 6f 73 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20  os, iLimit);.   
2ada6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2ada7 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
2ada8 65 72 2c 20 2d 31 2c 20 69 4f 66 66 73 65 74 2b  er, -1, iOffset+
2ada9 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
2adaa 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2adab 20 61 64 64 72 31 29 3b 0a 20 20 20 20 7d 0a 20   addr1);.    }. 
2adac 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51   }.}..#ifndef SQ
2adad 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55  LITE_OMIT_COMPOU
2adae 4e 44 5f 53 45 4c 45 43 54 0a 2f 2a 0a 2a 2a 20  ND_SELECT./*.** 
2adaf 52 65 74 75 72 6e 20 74 68 65 20 61 70 70 72 6f  Return the appro
2adb0 70 72 69 61 74 65 20 63 6f 6c 6c 61 74 69 6e 67  priate collating
2adb1 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68   sequence for th
2adb2 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e  e iCol-th column
2adb3 20 6f 66 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c   of.** the resul
2adb4 74 20 73 65 74 20 66 6f 72 20 74 68 65 20 63 6f  t set for the co
2adb5 6d 70 6f 75 6e 64 2d 73 65 6c 65 63 74 20 73 74  mpound-select st
2adb6 61 74 65 6d 65 6e 74 20 22 70 22 2e 20 20 52 65  atement "p".  Re
2adb7 74 75 72 6e 20 4e 55 4c 4c 20 69 66 0a 2a 2a 20  turn NULL if.** 
2adb8 74 68 65 20 63 6f 6c 75 6d 6e 20 68 61 73 20 6e  the column has n
2adb9 6f 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74  o default collat
2adba 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a  ing sequence..**
2adbb 0a 2a 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e  .** The collatin
2adbc 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74  g sequence for t
2adbd 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65  he compound sele
2adbe 63 74 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d  ct is taken from
2adbf 20 74 68 65 0a 2a 2a 20 6c 65 66 74 2d 6d 6f 73   the.** left-mos
2adc0 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 73 65  t term of the se
2adc1 6c 65 63 74 20 74 68 61 74 20 68 61 73 20 61 20  lect that has a 
2adc2 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
2adc3 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 43 6f  ce..*/.static Co
2adc4 6c 6c 53 65 71 20 2a 6d 75 6c 74 69 53 65 6c 65  llSeq *multiSele
2adc5 63 74 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20  ctCollSeq(Parse 
2adc6 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20  *pParse, Select 
2adc7 2a 70 2c 20 69 6e 74 20 69 43 6f 6c 29 7b 0a 20  *p, int iCol){. 
2adc8 20 43 6f 6c 6c 53 65 71 20 2a 70 52 65 74 3b 0a   CollSeq *pRet;.
2adc9 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 20    if( p->pPrior 
2adca 29 7b 0a 20 20 20 20 70 52 65 74 20 3d 20 6d 75  ){.    pRet = mu
2adcb 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71  ltiSelectCollSeq
2adcc 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 50 72 69  (pParse, p->pPri
2adcd 6f 72 2c 20 69 43 6f 6c 29 3b 0a 20 20 7d 65 6c  or, iCol);.  }el
2adce 73 65 7b 0a 20 20 20 20 70 52 65 74 20 3d 20 30  se{.    pRet = 0
2adcf 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
2add0 69 43 6f 6c 3e 3d 30 20 29 3b 0a 20 20 69 66 28  iCol>=0 );.  if(
2add1 20 70 52 65 74 3d 3d 30 20 26 26 20 69 43 6f 6c   pRet==0 && iCol
2add2 3c 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70  <p->pEList->nExp
2add3 72 20 29 7b 0a 20 20 20 20 70 52 65 74 20 3d 20  r ){.    pRet = 
2add4 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53  sqlite3ExprCollS
2add5 65 71 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 45  eq(pParse, p->pE
2add6 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 70 45  List->a[iCol].pE
2add7 78 70 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  xpr);.  }.  retu
2add8 72 6e 20 70 52 65 74 3b 0a 7d 0a 23 65 6e 64 69  rn pRet;.}.#endi
2add9 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2adda 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
2addb 20 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20   */../* Forward 
2addc 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73 74 61  reference */.sta
2addd 74 69 63 20 69 6e 74 20 6d 75 6c 74 69 53 65 6c  tic int multiSel
2adde 65 63 74 4f 72 64 65 72 42 79 28 0a 20 20 50 61  ectOrderBy(.  Pa
2addf 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
2ade0 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63      /* Parsing c
2ade1 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65  ontext */.  Sele
2ade2 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  ct *p,          
2ade3 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d 6d    /* The right-m
2ade4 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 73 20 74  ost of SELECTs t
2ade5 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20 20  o be coded */.  
2ade6 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65 73  SelectDest *pDes
2ade7 74 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74 6f  t     /* What to
2ade8 20 64 6f 20 77 69 74 68 20 71 75 65 72 79 20 72   do with query r
2ade9 65 73 75 6c 74 73 20 2a 2f 0a 29 3b 0a 0a 0a 23  esults */.);...#
2adea 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2adeb 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  IT_COMPOUND_SELE
2adec 43 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  CT./*.** This ro
2aded 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
2adee 74 6f 20 70 72 6f 63 65 73 73 20 61 20 63 6f 6d  to process a com
2adef 70 6f 75 6e 64 20 71 75 65 72 79 20 66 6f 72 6d  pound query form
2adf0 20 66 72 6f 6d 0a 2a 2a 20 74 77 6f 20 6f 72 20   from.** two or 
2adf1 6d 6f 72 65 20 73 65 70 61 72 61 74 65 20 71 75  more separate qu
2adf2 65 72 69 65 73 20 75 73 69 6e 67 20 55 4e 49 4f  eries using UNIO
2adf3 4e 2c 20 55 4e 49 4f 4e 20 41 4c 4c 2c 20 45 58  N, UNION ALL, EX
2adf4 43 45 50 54 2c 20 6f 72 0a 2a 2a 20 49 4e 54 45  CEPT, or.** INTE
2adf5 52 53 45 43 54 0a 2a 2a 0a 2a 2a 20 22 70 22 20  RSECT.**.** "p" 
2adf6 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 72 69  points to the ri
2adf7 67 68 74 2d 6d 6f 73 74 20 6f 66 20 74 68 65 20  ght-most of the 
2adf8 74 77 6f 20 71 75 65 72 69 65 73 2e 20 20 74 68  two queries.  th
2adf9 65 20 71 75 65 72 79 20 6f 6e 20 74 68 65 0a 2a  e query on the.*
2adfa 2a 20 6c 65 66 74 20 69 73 20 70 2d 3e 70 50 72  * left is p->pPr
2adfb 69 6f 72 2e 20 20 54 68 65 20 6c 65 66 74 20 71  ior.  The left q
2adfc 75 65 72 79 20 63 6f 75 6c 64 20 61 6c 73 6f 20  uery could also 
2adfd 62 65 20 61 20 63 6f 6d 70 6f 75 6e 64 20 71 75  be a compound qu
2adfe 65 72 79 0a 2a 2a 20 69 6e 20 77 68 69 63 68 20  ery.** in which 
2adff 63 61 73 65 20 74 68 69 73 20 72 6f 75 74 69 6e  case this routin
2ae00 65 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64  e will be called
2ae01 20 72 65 63 75 72 73 69 76 65 6c 79 2e 20 0a 2a   recursively. .*
2ae02 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 73  *.** The results
2ae03 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 20 71 75   of the total qu
2ae04 65 72 79 20 61 72 65 20 74 6f 20 62 65 20 77 72  ery are to be wr
2ae05 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 64 65 73  itten into a des
2ae06 74 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 74  tination.** of t
2ae07 79 70 65 20 65 44 65 73 74 20 77 69 74 68 20 70  ype eDest with p
2ae08 61 72 61 6d 65 74 65 72 20 69 50 61 72 6d 2e 0a  arameter iParm..
2ae09 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 20 31 3a  **.** Example 1:
2ae0a 20 20 43 6f 6e 73 69 64 65 72 20 61 20 74 68 72    Consider a thr
2ae0b 65 65 2d 77 61 79 20 63 6f 6d 70 6f 75 6e 64 20  ee-way compound 
2ae0c 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  SQL statement..*
2ae0d 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  *.**     SELECT 
2ae0e 61 20 46 52 4f 4d 20 74 31 20 55 4e 49 4f 4e 20  a FROM t1 UNION 
2ae0f 53 45 4c 45 43 54 20 62 20 46 52 4f 4d 20 74 32  SELECT b FROM t2
2ae10 20 55 4e 49 4f 4e 20 53 45 4c 45 43 54 20 63 20   UNION SELECT c 
2ae11 46 52 4f 4d 20 74 33 0a 2a 2a 0a 2a 2a 20 54 68  FROM t3.**.** Th
2ae12 69 73 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  is statement is 
2ae13 70 61 72 73 65 64 20 75 70 20 61 73 20 66 6f 6c  parsed up as fol
2ae14 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  lows:.**.**     
2ae15 53 45 4c 45 43 54 20 63 20 46 52 4f 4d 20 74 33  SELECT c FROM t3
2ae16 0a 2a 2a 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20  .**      |.**   
2ae17 20 20 20 60 2d 2d 2d 2d 2d 3e 20 20 53 45 4c 45     `----->  SELE
2ae18 43 54 20 62 20 46 52 4f 4d 20 74 32 0a 2a 2a 20  CT b FROM t2.** 
2ae19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7c                 |
2ae1a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2ae1b 20 20 20 60 2d 2d 2d 2d 2d 2d 3e 20 20 53 45 4c     `------>  SEL
2ae1c 45 43 54 20 61 20 46 52 4f 4d 20 74 31 0a 2a 2a  ECT a FROM t1.**
2ae1d 0a 2a 2a 20 54 68 65 20 61 72 72 6f 77 73 20 69  .** The arrows i
2ae1e 6e 20 74 68 65 20 64 69 61 67 72 61 6d 20 61 62  n the diagram ab
2ae1f 6f 76 65 20 72 65 70 72 65 73 65 6e 74 20 74 68  ove represent th
2ae20 65 20 53 65 6c 65 63 74 2e 70 50 72 69 6f 72 20  e Select.pPrior 
2ae21 70 6f 69 6e 74 65 72 2e 0a 2a 2a 20 53 6f 20 69  pointer..** So i
2ae22 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  f this routine i
2ae23 73 20 63 61 6c 6c 65 64 20 77 69 74 68 20 70 20  s called with p 
2ae24 65 71 75 61 6c 20 74 6f 20 74 68 65 20 74 33 20  equal to the t3 
2ae25 71 75 65 72 79 2c 20 74 68 65 6e 0a 2a 2a 20 70  query, then.** p
2ae26 50 72 69 6f 72 20 77 69 6c 6c 20 62 65 20 74 68  Prior will be th
2ae27 65 20 74 32 20 71 75 65 72 79 2e 20 20 70 2d 3e  e t2 query.  p->
2ae28 6f 70 20 77 69 6c 6c 20 62 65 20 54 4b 5f 55 4e  op will be TK_UN
2ae29 49 4f 4e 20 69 6e 20 74 68 69 73 20 63 61 73 65  ION in this case
2ae2a 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 69 63 65 20 74  ..**.** Notice t
2ae2b 68 61 74 20 62 65 63 61 75 73 65 20 6f 66 20 74  hat because of t
2ae2c 68 65 20 77 61 79 20 53 51 4c 69 74 65 20 70 61  he way SQLite pa
2ae2d 72 73 65 73 20 63 6f 6d 70 6f 75 6e 64 20 53 45  rses compound SE
2ae2e 4c 45 43 54 73 2c 20 74 68 65 0a 2a 2a 20 69 6e  LECTs, the.** in
2ae2f 64 69 76 69 64 75 61 6c 20 73 65 6c 65 63 74 73  dividual selects
2ae30 20 61 6c 77 61 79 73 20 67 72 6f 75 70 20 66 72   always group fr
2ae31 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 68 74  om left to right
2ae32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2ae33 6d 75 6c 74 69 53 65 6c 65 63 74 28 0a 20 20 50  multiSelect(.  P
2ae34 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2ae35 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2ae36 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c  context */.  Sel
2ae37 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20  ect *p,         
2ae38 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74 2d     /* The right-
2ae39 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 73 20  most of SELECTs 
2ae3a 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20  to be coded */. 
2ae3b 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44 65   SelectDest *pDe
2ae3c 73 74 20 20 20 20 20 2f 2a 20 57 68 61 74 20 74  st     /* What t
2ae3d 6f 20 64 6f 20 77 69 74 68 20 71 75 65 72 79 20  o do with query 
2ae3e 72 65 73 75 6c 74 73 20 2a 2f 0a 29 7b 0a 20 20  results */.){.  
2ae3f 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
2ae40 4f 4b 3b 20 20 20 2f 2a 20 53 75 63 63 65 73 73  OK;   /* Success
2ae41 20 63 6f 64 65 20 66 72 6f 6d 20 61 20 73 75 62   code from a sub
2ae42 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 53 65 6c  routine */.  Sel
2ae43 65 63 74 20 2a 70 50 72 69 6f 72 3b 20 20 20 20  ect *pPrior;    
2ae44 20 20 20 2f 2a 20 41 6e 6f 74 68 65 72 20 53 45     /* Another SE
2ae45 4c 45 43 54 20 69 6d 6d 65 64 69 61 74 65 6c 79  LECT immediately
2ae46 20 74 6f 20 6f 75 72 20 6c 65 66 74 20 2a 2f 0a   to our left */.
2ae47 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20    Vdbe *v;      
2ae48 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e 65 72          /* Gener
2ae49 61 74 65 20 63 6f 64 65 20 74 6f 20 74 68 69 73  ate code to this
2ae4a 20 56 44 42 45 20 2a 2f 0a 20 20 53 65 6c 65 63   VDBE */.  Selec
2ae4b 74 44 65 73 74 20 64 65 73 74 3b 20 20 20 20 20  tDest dest;     
2ae4c 20 2f 2a 20 41 6c 74 65 72 6e 61 74 69 76 65 20   /* Alternative 
2ae4d 64 61 74 61 20 64 65 73 74 69 6e 61 74 69 6f 6e  data destination
2ae4e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 44   */.  Select *pD
2ae4f 65 6c 65 74 65 20 3d 20 30 3b 20 20 2f 2a 20 43  elete = 0;  /* C
2ae50 68 61 69 6e 20 6f 66 20 73 69 6d 70 6c 65 20 73  hain of simple s
2ae51 65 6c 65 63 74 73 20 74 6f 20 64 65 6c 65 74 65  elects to delete
2ae52 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
2ae53 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  b;          /* D
2ae54 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2ae55 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 4d 61 6b 65  on */..  /* Make
2ae56 20 73 75 72 65 20 74 68 65 72 65 20 69 73 20 6e   sure there is n
2ae57 6f 20 4f 52 44 45 52 20 42 59 20 6f 72 20 4c 49  o ORDER BY or LI
2ae58 4d 49 54 20 63 6c 61 75 73 65 20 6f 6e 20 70 72  MIT clause on pr
2ae59 69 6f 72 20 53 45 4c 45 43 54 73 2e 20 20 4f 6e  ior SELECTs.  On
2ae5a 6c 79 0a 20 20 2a 2a 20 74 68 65 20 6c 61 73 74  ly.  ** the last
2ae5b 20 28 72 69 67 68 74 2d 6d 6f 73 74 29 20 53 45   (right-most) SE
2ae5c 4c 45 43 54 20 69 6e 20 74 68 65 20 73 65 72 69  LECT in the seri
2ae5d 65 73 20 6d 61 79 20 68 61 76 65 20 61 6e 20 4f  es may have an O
2ae5e 52 44 45 52 20 42 59 20 6f 72 20 4c 49 4d 49 54  RDER BY or LIMIT
2ae5f 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
2ae60 20 70 20 26 26 20 70 2d 3e 70 50 72 69 6f 72 20   p && p->pPrior 
2ae61 29 3b 20 20 2f 2a 20 43 61 6c 6c 69 6e 67 20 66  );  /* Calling f
2ae62 75 6e 63 74 69 6f 6e 20 67 75 61 72 61 6e 74 65  unction guarante
2ae63 65 73 20 74 68 69 73 20 6d 75 63 68 20 2a 2f 0a  es this much */.
2ae64 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
2ae65 62 3b 0a 20 20 70 50 72 69 6f 72 20 3d 20 70 2d  b;.  pPrior = p-
2ae66 3e 70 50 72 69 6f 72 3b 0a 20 20 61 73 73 65 72  >pPrior;.  asser
2ae67 74 28 20 70 50 72 69 6f 72 2d 3e 70 52 69 67 68  t( pPrior->pRigh
2ae68 74 6d 6f 73 74 21 3d 70 50 72 69 6f 72 20 29 3b  tmost!=pPrior );
2ae69 0a 20 20 61 73 73 65 72 74 28 20 70 50 72 69 6f  .  assert( pPrio
2ae6a 72 2d 3e 70 52 69 67 68 74 6d 6f 73 74 3d 3d 70  r->pRightmost==p
2ae6b 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 29 3b 0a  ->pRightmost );.
2ae6c 20 20 64 65 73 74 20 3d 20 2a 70 44 65 73 74 3b    dest = *pDest;
2ae6d 0a 20 20 69 66 28 20 70 50 72 69 6f 72 2d 3e 70  .  if( pPrior->p
2ae6e 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 73  OrderBy ){.    s
2ae6f 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2ae70 50 61 72 73 65 2c 22 4f 52 44 45 52 20 42 59 20  Parse,"ORDER BY 
2ae71 63 6c 61 75 73 65 20 73 68 6f 75 6c 64 20 63 6f  clause should co
2ae72 6d 65 20 61 66 74 65 72 20 25 73 20 6e 6f 74 20  me after %s not 
2ae73 62 65 66 6f 72 65 22 2c 0a 20 20 20 20 20 20 73  before",.      s
2ae74 65 6c 65 63 74 4f 70 4e 61 6d 65 28 70 2d 3e 6f  electOpName(p->o
2ae75 70 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 31 3b  p));.    rc = 1;
2ae76 0a 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74 69 5f  .    goto multi_
2ae77 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a  select_end;.  }.
2ae78 20 20 69 66 28 20 70 50 72 69 6f 72 2d 3e 70 4c    if( pPrior->pL
2ae79 69 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  imit ){.    sqli
2ae7a 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2ae7b 73 65 2c 22 4c 49 4d 49 54 20 63 6c 61 75 73 65  se,"LIMIT clause
2ae7c 20 73 68 6f 75 6c 64 20 63 6f 6d 65 20 61 66 74   should come aft
2ae7d 65 72 20 25 73 20 6e 6f 74 20 62 65 66 6f 72 65  er %s not before
2ae7e 22 2c 0a 20 20 20 20 20 20 73 65 6c 65 63 74 4f  ",.      selectO
2ae7f 70 4e 61 6d 65 28 70 2d 3e 6f 70 29 29 3b 0a 20  pName(p->op));. 
2ae80 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 67     rc = 1;.    g
2ae81 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74  oto multi_select
2ae82 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 20 20 76 20 3d  _end;.  }..  v =
2ae83 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
2ae84 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73 65 72  pParse);.  asser
2ae85 74 28 20 76 21 3d 30 20 29 3b 20 20 2f 2a 20 54  t( v!=0 );  /* T
2ae86 68 65 20 56 44 42 45 20 61 6c 72 65 61 64 79 20  he VDBE already 
2ae87 63 72 65 61 74 65 64 20 62 79 20 63 61 6c 6c 69  created by calli
2ae88 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a  ng function */..
2ae89 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20    /* Create the 
2ae8a 64 65 73 74 69 6e 61 74 69 6f 6e 20 74 65 6d 70  destination temp
2ae8b 6f 72 61 72 79 20 74 61 62 6c 65 20 69 66 20 6e  orary table if n
2ae8c 65 63 65 73 73 61 72 79 0a 20 20 2a 2f 0a 20 20  ecessary.  */.  
2ae8d 69 66 28 20 64 65 73 74 2e 65 44 65 73 74 3d 3d  if( dest.eDest==
2ae8e 53 52 54 5f 45 70 68 65 6d 54 61 62 20 29 7b 0a  SRT_EphemTab ){.
2ae8f 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
2ae90 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 73 71 6c  EList );.    sql
2ae91 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2ae92 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  , OP_OpenEphemer
2ae93 61 6c 2c 20 64 65 73 74 2e 69 50 61 72 6d 2c 20  al, dest.iParm, 
2ae94 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  p->pEList->nExpr
2ae95 29 3b 0a 20 20 20 20 64 65 73 74 2e 65 44 65 73  );.    dest.eDes
2ae96 74 20 3d 20 53 52 54 5f 54 61 62 6c 65 3b 0a 20  t = SRT_Table;. 
2ae97 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75   }..  /* Make su
2ae98 72 65 20 61 6c 6c 20 53 45 4c 45 43 54 73 20 69  re all SELECTs i
2ae99 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  n the statement 
2ae9a 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 6e 75  have the same nu
2ae9b 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73  mber of elements
2ae9c 0a 20 20 2a 2a 20 69 6e 20 74 68 65 69 72 20 72  .  ** in their r
2ae9d 65 73 75 6c 74 20 73 65 74 73 2e 0a 20 20 2a 2f  esult sets..  */
2ae9e 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 45  .  assert( p->pE
2ae9f 4c 69 73 74 20 26 26 20 70 50 72 69 6f 72 2d 3e  List && pPrior->
2aea0 70 45 4c 69 73 74 20 29 3b 0a 20 20 69 66 28 20  pEList );.  if( 
2aea1 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  p->pEList->nExpr
2aea2 21 3d 70 50 72 69 6f 72 2d 3e 70 45 4c 69 73 74  !=pPrior->pEList
2aea3 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 73  ->nExpr ){.    s
2aea4 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2aea5 50 61 72 73 65 2c 20 22 53 45 4c 45 43 54 73 20  Parse, "SELECTs 
2aea6 74 6f 20 74 68 65 20 6c 65 66 74 20 61 6e 64 20  to the left and 
2aea7 72 69 67 68 74 20 6f 66 20 25 73 22 0a 20 20 20  right of %s".   
2aea8 20 20 20 22 20 64 6f 20 6e 6f 74 20 68 61 76 65     " do not have
2aea9 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72   the same number
2aeaa 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d   of result colum
2aeab 6e 73 22 2c 20 73 65 6c 65 63 74 4f 70 4e 61 6d  ns", selectOpNam
2aeac 65 28 70 2d 3e 6f 70 29 29 3b 0a 20 20 20 20 72  e(p->op));.    r
2aead 63 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20  c = 1;.    goto 
2aeae 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64  multi_select_end
2aeaf 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70  ;.  }..  /* Comp
2aeb0 6f 75 6e 64 20 53 45 4c 45 43 54 73 20 74 68 61  ound SELECTs tha
2aeb1 74 20 68 61 76 65 20 61 6e 20 4f 52 44 45 52 20  t have an ORDER 
2aeb2 42 59 20 63 6c 61 75 73 65 20 61 72 65 20 68 61  BY clause are ha
2aeb3 6e 64 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79  ndled separately
2aeb4 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ..  */.  if( p->
2aeb5 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
2aeb6 72 65 74 75 72 6e 20 6d 75 6c 74 69 53 65 6c 65  return multiSele
2aeb7 63 74 4f 72 64 65 72 42 79 28 70 50 61 72 73 65  ctOrderBy(pParse
2aeb8 2c 20 70 2c 20 70 44 65 73 74 29 3b 0a 20 20 7d  , p, pDest);.  }
2aeb9 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
2aeba 63 6f 64 65 20 66 6f 72 20 74 68 65 20 6c 65 66  code for the lef
2aebb 74 20 61 6e 64 20 72 69 67 68 74 20 53 45 4c 45  t and right SELE
2aebc 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20  CT statements.. 
2aebd 20 2a 2f 0a 20 20 73 77 69 74 63 68 28 20 70 2d   */.  switch( p-
2aebe 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20  >op ){.    case 
2aebf 54 4b 5f 41 4c 4c 3a 20 7b 0a 20 20 20 20 20 20  TK_ALL: {.      
2aec0 69 6e 74 20 61 64 64 72 20 3d 20 30 3b 0a 20 20  int addr = 0;.  
2aec1 20 20 20 20 61 73 73 65 72 74 28 20 21 70 50 72      assert( !pPr
2aec2 69 6f 72 2d 3e 70 4c 69 6d 69 74 20 29 3b 0a 20  ior->pLimit );. 
2aec3 20 20 20 20 20 70 50 72 69 6f 72 2d 3e 70 4c 69       pPrior->pLi
2aec4 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69 74 3b  mit = p->pLimit;
2aec5 0a 20 20 20 20 20 20 70 50 72 69 6f 72 2d 3e 70  .      pPrior->p
2aec6 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f 66 66  Offset = p->pOff
2aec7 73 65 74 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  set;.      rc = 
2aec8 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50  sqlite3Select(pP
2aec9 61 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 26 64  arse, pPrior, &d
2aeca 65 73 74 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70  est);.      p->p
2aecb 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20  Limit = 0;.     
2aecc 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20 30 3b   p->pOffset = 0;
2aecd 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
2aece 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75  .        goto mu
2aecf 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  lti_select_end;.
2aed0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d        }.      p-
2aed1 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20  >pPrior = 0;.   
2aed2 20 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d 20 70     p->iLimit = p
2aed3 50 72 69 6f 72 2d 3e 69 4c 69 6d 69 74 3b 0a 20  Prior->iLimit;. 
2aed4 20 20 20 20 20 70 2d 3e 69 4f 66 66 73 65 74 20       p->iOffset 
2aed5 3d 20 70 50 72 69 6f 72 2d 3e 69 4f 66 66 73 65  = pPrior->iOffse
2aed6 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e  t;.      if( p->
2aed7 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20  iLimit ){.      
2aed8 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
2aed9 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2aeda 5f 49 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d  _IfZero, p->iLim
2aedb 69 74 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62  it);.        Vdb
2aedc 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4a 75  eComment((v, "Ju
2aedd 6d 70 20 61 68 65 61 64 20 69 66 20 4c 49 4d 49  mp ahead if LIMI
2aede 54 20 72 65 61 63 68 65 64 22 29 29 3b 0a 20 20  T reached"));.  
2aedf 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d      }.      rc =
2aee0 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
2aee1 50 61 72 73 65 2c 20 70 2c 20 26 64 65 73 74 29  Parse, p, &dest)
2aee2 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
2aee3 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2aee4 29 3b 0a 20 20 20 20 20 20 70 44 65 6c 65 74 65  );.      pDelete
2aee5 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20   = p->pPrior;.  
2aee6 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20      p->pPrior = 
2aee7 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 69 66  pPrior;.      if
2aee8 28 20 61 64 64 72 20 29 7b 0a 20 20 20 20 20 20  ( addr ){.      
2aee9 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2aeea 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a  pHere(v, addr);.
2aeeb 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
2aeec 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
2aeed 61 73 65 20 54 4b 5f 45 58 43 45 50 54 3a 0a 20  ase TK_EXCEPT:. 
2aeee 20 20 20 63 61 73 65 20 54 4b 5f 55 4e 49 4f 4e     case TK_UNION
2aeef 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 75 6e  : {.      int un
2aef0 69 6f 6e 54 61 62 3b 20 20 20 20 2f 2a 20 43 75  ionTab;    /* Cu
2aef1 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74  rsor number of t
2aef2 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  he temporary tab
2aef3 6c 65 20 68 6f 6c 64 69 6e 67 20 72 65 73 75 6c  le holding resul
2aef4 74 20 2a 2f 0a 20 20 20 20 20 20 75 38 20 6f 70  t */.      u8 op
2aef5 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4f   = 0;       /* O
2aef6 6e 65 20 6f 66 20 74 68 65 20 53 52 54 5f 20 6f  ne of the SRT_ o
2aef7 70 65 72 61 74 69 6f 6e 73 20 74 6f 20 61 70 70  perations to app
2aef8 6c 79 20 74 6f 20 73 65 6c 66 20 2a 2f 0a 20 20  ly to self */.  
2aef9 20 20 20 20 69 6e 74 20 70 72 69 6f 72 4f 70 3b      int priorOp;
2aefa 20 20 20 20 20 2f 2a 20 54 68 65 20 53 52 54 5f       /* The SRT_
2aefb 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20 61 70   operation to ap
2aefc 70 6c 79 20 74 6f 20 70 72 69 6f 72 20 73 65 6c  ply to prior sel
2aefd 65 63 74 73 20 2a 2f 0a 20 20 20 20 20 20 45 78  ects */.      Ex
2aefe 70 72 20 2a 70 4c 69 6d 69 74 2c 20 2a 70 4f 66  pr *pLimit, *pOf
2aeff 66 73 65 74 3b 20 2f 2a 20 53 61 76 65 64 20 76  fset; /* Saved v
2af00 61 6c 75 65 73 20 6f 66 20 70 2d 3e 6e 4c 69 6d  alues of p->nLim
2af01 69 74 20 61 6e 64 20 70 2d 3e 6e 4f 66 66 73 65  it and p->nOffse
2af02 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61  t */.      int a
2af03 64 64 72 3b 0a 20 20 20 20 20 20 53 65 6c 65 63  ddr;.      Selec
2af04 74 44 65 73 74 20 75 6e 69 6f 6e 64 65 73 74 3b  tDest uniondest;
2af05 0a 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ..      testcase
2af06 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 43 45  ( p->op==TK_EXCE
2af07 50 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  PT );.      test
2af08 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f  case( p->op==TK_
2af09 55 4e 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 70  UNION );.      p
2af0a 72 69 6f 72 4f 70 20 3d 20 53 52 54 5f 55 6e 69  riorOp = SRT_Uni
2af0b 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 64 65  on;.      if( de
2af0c 73 74 2e 65 44 65 73 74 3d 3d 70 72 69 6f 72 4f  st.eDest==priorO
2af0d 70 20 26 26 20 41 4c 57 41 59 53 28 21 70 2d 3e  p && ALWAYS(!p->
2af0e 70 4c 69 6d 69 74 20 26 26 21 70 2d 3e 70 4f 66  pLimit &&!p->pOf
2af0f 66 73 65 74 29 20 29 7b 0a 20 20 20 20 20 20 20  fset) ){.       
2af10 20 2f 2a 20 57 65 20 63 61 6e 20 72 65 75 73 65   /* We can reuse
2af11 20 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62   a temporary tab
2af12 6c 65 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  le generated by 
2af13 61 20 53 45 4c 45 43 54 20 74 6f 20 6f 75 72 0a  a SELECT to our.
2af14 20 20 20 20 20 20 20 20 2a 2a 20 72 69 67 68 74          ** right
2af15 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
2af16 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
2af17 70 52 69 67 68 74 6d 6f 73 74 21 3d 70 20 29 3b  pRightmost!=p );
2af18 20 20 2f 2a 20 43 61 6e 20 6f 6e 6c 79 20 68 61    /* Can only ha
2af19 70 70 65 6e 20 66 6f 72 20 6c 65 66 74 77 61 72  ppen for leftwar
2af1a 64 20 65 6c 65 6d 65 6e 74 73 0a 20 20 20 20 20  d elements.     
2af1b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2af1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2af1d 2a 2a 20 6f 66 20 61 20 33 2d 77 61 79 20 6f 72  ** of a 3-way or
2af1e 20 6d 6f 72 65 20 63 6f 6d 70 6f 75 6e 64 20 2a   more compound *
2af1f 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  /.        assert
2af20 28 20 70 2d 3e 70 4c 69 6d 69 74 3d 3d 30 20 29  ( p->pLimit==0 )
2af21 3b 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 61 6c  ;      /* Not al
2af22 6c 6f 77 65 64 20 6f 6e 20 6c 65 66 74 77 61 72  lowed on leftwar
2af23 64 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20  d elements */.  
2af24 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
2af25 3e 70 4f 66 66 73 65 74 3d 3d 30 20 29 3b 20 20  >pOffset==0 );  
2af26 20 20 20 2f 2a 20 4e 6f 74 20 61 6c 6c 6f 77 65     /* Not allowe
2af27 64 20 6f 6e 20 6c 65 66 74 77 61 72 64 20 65 6c  d on leftward el
2af28 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20  ements */.      
2af29 20 20 75 6e 69 6f 6e 54 61 62 20 3d 20 64 65 73    unionTab = des
2af2a 74 2e 69 50 61 72 6d 3b 0a 20 20 20 20 20 20 7d  t.iParm;.      }
2af2b 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
2af2c 20 57 65 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f   We will need to
2af2d 20 63 72 65 61 74 65 20 6f 75 72 20 6f 77 6e 20   create our own 
2af2e 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20  temporary table 
2af2f 74 6f 20 68 6f 6c 64 20 74 68 65 0a 20 20 20 20  to hold the.    
2af30 20 20 20 20 2a 2a 20 69 6e 74 65 72 6d 65 64 69      ** intermedi
2af31 61 74 65 20 72 65 73 75 6c 74 73 2e 0a 20 20 20  ate results..   
2af32 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
2af33 75 6e 69 6f 6e 54 61 62 20 3d 20 70 50 61 72 73  unionTab = pPars
2af34 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20  e->nTab++;.     
2af35 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 4f     assert( p->pO
2af36 72 64 65 72 42 79 3d 3d 30 20 29 3b 0a 20 20 20  rderBy==0 );.   
2af37 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
2af38 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2af39 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
2af3a 6c 2c 20 75 6e 69 6f 6e 54 61 62 2c 20 30 29 3b  l, unionTab, 0);
2af3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2af3c 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d   p->addrOpenEphm
2af3d 5b 30 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20 20  [0] == -1 );.   
2af3e 20 20 20 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e       p->addrOpen
2af3f 45 70 68 6d 5b 30 5d 20 3d 20 61 64 64 72 3b 0a  Ephm[0] = addr;.
2af40 20 20 20 20 20 20 20 20 70 2d 3e 70 52 69 67 68          p->pRigh
2af41 74 6d 6f 73 74 2d 3e 73 65 6c 46 6c 61 67 73 20  tmost->selFlags 
2af42 7c 3d 20 53 46 5f 55 73 65 73 45 70 68 65 6d 65  |= SF_UsesEpheme
2af43 72 61 6c 3b 0a 20 20 20 20 20 20 20 20 61 73 73  ral;.        ass
2af44 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 29  ert( p->pEList )
2af45 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20  ;.      }..     
2af46 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 53 45 4c   /* Code the SEL
2af47 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 74  ECT statements t
2af48 6f 20 6f 75 72 20 6c 65 66 74 0a 20 20 20 20 20  o our left.     
2af49 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
2af4a 28 20 21 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65  ( !pPrior->pOrde
2af4b 72 42 79 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  rBy );.      sql
2af4c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e  ite3SelectDestIn
2af4d 69 74 28 26 75 6e 69 6f 6e 64 65 73 74 2c 20 70  it(&uniondest, p
2af4e 72 69 6f 72 4f 70 2c 20 75 6e 69 6f 6e 54 61 62  riorOp, unionTab
2af4f 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  );.      rc = sq
2af50 6c 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72  lite3Select(pPar
2af51 73 65 2c 20 70 50 72 69 6f 72 2c 20 26 75 6e 69  se, pPrior, &uni
2af52 6f 6e 64 65 73 74 29 3b 0a 20 20 20 20 20 20 69  ondest);.      i
2af53 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20  f( rc ){.       
2af54 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65   goto multi_sele
2af55 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a  ct_end;.      }.
2af56 0a 20 20 20 20 20 20 2f 2a 20 43 6f 64 65 20 74  .      /* Code t
2af57 68 65 20 63 75 72 72 65 6e 74 20 53 45 4c 45 43  he current SELEC
2af58 54 20 73 74 61 74 65 6d 65 6e 74 0a 20 20 20 20  T statement.    
2af59 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70    */.      if( p
2af5a 2d 3e 6f 70 3d 3d 54 4b 5f 45 58 43 45 50 54 20  ->op==TK_EXCEPT 
2af5b 29 7b 0a 20 20 20 20 20 20 20 20 6f 70 20 3d 20  ){.        op = 
2af5c 53 52 54 5f 45 78 63 65 70 74 3b 0a 20 20 20 20  SRT_Except;.    
2af5d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2af5e 20 61 73 73 65 72 74 28 20 70 2d 3e 6f 70 3d 3d   assert( p->op==
2af5f 54 4b 5f 55 4e 49 4f 4e 20 29 3b 0a 20 20 20 20  TK_UNION );.    
2af60 20 20 20 20 6f 70 20 3d 20 53 52 54 5f 55 6e 69      op = SRT_Uni
2af61 6f 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  on;.      }.    
2af62 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b    p->pPrior = 0;
2af63 0a 20 20 20 20 20 20 70 4c 69 6d 69 74 20 3d 20  .      pLimit = 
2af64 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20  p->pLimit;.     
2af65 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a   p->pLimit = 0;.
2af66 20 20 20 20 20 20 70 4f 66 66 73 65 74 20 3d 20        pOffset = 
2af67 70 2d 3e 70 4f 66 66 73 65 74 3b 0a 20 20 20 20  p->pOffset;.    
2af68 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20 30    p->pOffset = 0
2af69 3b 0a 20 20 20 20 20 20 75 6e 69 6f 6e 64 65 73  ;.      uniondes
2af6a 74 2e 65 44 65 73 74 20 3d 20 6f 70 3b 0a 20 20  t.eDest = op;.  
2af6b 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2af6c 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
2af6d 2c 20 26 75 6e 69 6f 6e 64 65 73 74 29 3b 0a 20  , &uniondest);. 
2af6e 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
2af6f 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  c!=SQLITE_OK );.
2af70 20 20 20 20 20 20 2f 2a 20 51 75 65 72 79 20 66        /* Query f
2af71 6c 61 74 74 65 6e 69 6e 67 20 69 6e 20 73 71 6c  lattening in sql
2af72 69 74 65 33 53 65 6c 65 63 74 28 29 20 6d 69 67  ite3Select() mig
2af73 68 74 20 72 65 66 69 6c 6c 20 70 2d 3e 70 4f 72  ht refill p->pOr
2af74 64 65 72 42 79 2e 0a 20 20 20 20 20 20 2a 2a 20  derBy..      ** 
2af75 42 65 20 73 75 72 65 20 74 6f 20 64 65 6c 65 74  Be sure to delet
2af76 65 20 70 2d 3e 70 4f 72 64 65 72 42 79 2c 20 74  e p->pOrderBy, t
2af77 68 65 72 65 66 6f 72 65 2c 20 74 6f 20 61 76 6f  herefore, to avo
2af78 69 64 20 61 20 6d 65 6d 6f 72 79 20 6c 65 61 6b  id a memory leak
2af79 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  . */.      sqlit
2af7a 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
2af7b 28 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79  (db, p->pOrderBy
2af7c 29 3b 0a 20 20 20 20 20 20 70 44 65 6c 65 74 65  );.      pDelete
2af7d 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20   = p->pPrior;.  
2af7e 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20      p->pPrior = 
2af7f 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 70 2d  pPrior;.      p-
2af80 3e 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20  >pOrderBy = 0;. 
2af81 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2af82 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 4c  Delete(db, p->pL
2af83 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 70 2d 3e  imit);.      p->
2af84 70 4c 69 6d 69 74 20 3d 20 70 4c 69 6d 69 74 3b  pLimit = pLimit;
2af85 0a 20 20 20 20 20 20 70 2d 3e 70 4f 66 66 73 65  .      p->pOffse
2af86 74 20 3d 20 70 4f 66 66 73 65 74 3b 0a 20 20 20  t = pOffset;.   
2af87 20 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d 20 30     p->iLimit = 0
2af88 3b 0a 20 20 20 20 20 20 70 2d 3e 69 4f 66 66 73  ;.      p->iOffs
2af89 65 74 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 2f  et = 0;..      /
2af8a 2a 20 43 6f 6e 76 65 72 74 20 74 68 65 20 64 61  * Convert the da
2af8b 74 61 20 69 6e 20 74 68 65 20 74 65 6d 70 6f 72  ta in the tempor
2af8c 61 72 79 20 74 61 62 6c 65 20 69 6e 74 6f 20 77  ary table into w
2af8d 68 61 74 65 76 65 72 20 66 6f 72 6d 0a 20 20 20  hatever form.   
2af8e 20 20 20 2a 2a 20 69 74 20 69 73 20 74 68 61 74     ** it is that
2af8f 20 77 65 20 63 75 72 72 65 6e 74 6c 79 20 6e 65   we currently ne
2af90 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ed..      */.   
2af91 20 20 20 61 73 73 65 72 74 28 20 75 6e 69 6f 6e     assert( union
2af92 54 61 62 3d 3d 64 65 73 74 2e 69 50 61 72 6d 20  Tab==dest.iParm 
2af93 7c 7c 20 64 65 73 74 2e 65 44 65 73 74 21 3d 70  || dest.eDest!=p
2af94 72 69 6f 72 4f 70 20 29 3b 0a 20 20 20 20 20 20  riorOp );.      
2af95 69 66 28 20 64 65 73 74 2e 65 44 65 73 74 21 3d  if( dest.eDest!=
2af96 70 72 69 6f 72 4f 70 20 29 7b 0a 20 20 20 20 20  priorOp ){.     
2af97 20 20 20 69 6e 74 20 69 43 6f 6e 74 2c 20 69 42     int iCont, iB
2af98 72 65 61 6b 2c 20 69 53 74 61 72 74 3b 0a 20 20  reak, iStart;.  
2af99 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
2af9a 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 20  >pEList );.     
2af9b 20 20 20 69 66 28 20 64 65 73 74 2e 65 44 65 73     if( dest.eDes
2af9c 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 29 7b  t==SRT_Output ){
2af9d 0a 20 20 20 20 20 20 20 20 20 20 53 65 6c 65 63  .          Selec
2af9e 74 20 2a 70 46 69 72 73 74 20 3d 20 70 3b 0a 20  t *pFirst = p;. 
2af9f 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20           while( 
2afa0 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 20 29  pFirst->pPrior )
2afa1 20 70 46 69 72 73 74 20 3d 20 70 46 69 72 73 74   pFirst = pFirst
2afa2 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20  ->pPrior;.      
2afa3 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c 75      generateColu
2afa4 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20  mnNames(pParse, 
2afa5 30 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c 69 73  0, pFirst->pELis
2afa6 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  t);.        }.  
2afa7 20 20 20 20 20 20 69 42 72 65 61 6b 20 3d 20 73        iBreak = s
2afa8 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
2afa9 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20  bel(v);.        
2afaa 69 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56  iCont = sqlite3V
2afab 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
2afac 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65  .        compute
2afad 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73 28 70  LimitRegisters(p
2afae 50 61 72 73 65 2c 20 70 2c 20 69 42 72 65 61 6b  Parse, p, iBreak
2afaf 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2afb0 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2afb1 4f 50 5f 52 65 77 69 6e 64 2c 20 75 6e 69 6f 6e  OP_Rewind, union
2afb2 54 61 62 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20  Tab, iBreak);.  
2afb3 20 20 20 20 20 20 69 53 74 61 72 74 20 3d 20 73        iStart = s
2afb4 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
2afb5 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20  tAddr(v);.      
2afb6 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f    selectInnerLoo
2afb7 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e  p(pParse, p, p->
2afb8 70 45 4c 69 73 74 2c 20 75 6e 69 6f 6e 54 61 62  pEList, unionTab
2afb9 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78  , p->pEList->nEx
2afba 70 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  pr,.            
2afbb 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 2d              0, -
2afbc 31 2c 20 26 64 65 73 74 2c 20 69 43 6f 6e 74 2c  1, &dest, iCont,
2afbd 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20   iBreak);.      
2afbe 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
2afbf 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 43 6f  olveLabel(v, iCo
2afc0 6e 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  nt);.        sql
2afc1 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2afc2 2c 20 4f 50 5f 4e 65 78 74 2c 20 75 6e 69 6f 6e  , OP_Next, union
2afc3 54 61 62 2c 20 69 53 74 61 72 74 29 3b 0a 20 20  Tab, iStart);.  
2afc4 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2afc5 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2afc6 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20   iBreak);.      
2afc7 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2afc8 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op2(v, OP_Close,
2afc9 20 75 6e 69 6f 6e 54 61 62 2c 20 30 29 3b 0a 20   unionTab, 0);. 
2afca 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
2afcb 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65  ak;.    }.    de
2afcc 66 61 75 6c 74 3a 20 61 73 73 65 72 74 28 20 70  fault: assert( p
2afcd 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45  ->op==TK_INTERSE
2afce 43 54 20 29 3b 20 7b 0a 20 20 20 20 20 20 69 6e  CT ); {.      in
2afcf 74 20 74 61 62 31 2c 20 74 61 62 32 3b 0a 20 20  t tab1, tab2;.  
2afd0 20 20 20 20 69 6e 74 20 69 43 6f 6e 74 2c 20 69      int iCont, i
2afd1 42 72 65 61 6b 2c 20 69 53 74 61 72 74 3b 0a 20  Break, iStart;. 
2afd2 20 20 20 20 20 45 78 70 72 20 2a 70 4c 69 6d 69       Expr *pLimi
2afd3 74 2c 20 2a 70 4f 66 66 73 65 74 3b 0a 20 20 20  t, *pOffset;.   
2afd4 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20     int addr;.   
2afd5 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 69 6e     SelectDest in
2afd6 74 65 72 73 65 63 74 64 65 73 74 3b 0a 20 20 20  tersectdest;.   
2afd7 20 20 20 69 6e 74 20 72 31 3b 0a 0a 20 20 20 20     int r1;..    
2afd8 20 20 2f 2a 20 49 4e 54 45 52 53 45 43 54 20 69    /* INTERSECT i
2afd9 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  s different from
2afda 20 74 68 65 20 6f 74 68 65 72 73 20 73 69 6e 63   the others sinc
2afdb 65 20 69 74 20 72 65 71 75 69 72 65 73 0a 20 20  e it requires.  
2afdc 20 20 20 20 2a 2a 20 74 77 6f 20 74 65 6d 70 6f      ** two tempo
2afdd 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 48 65  rary tables.  He
2afde 6e 63 65 20 69 74 20 68 61 73 20 69 74 73 20 6f  nce it has its o
2afdf 77 6e 20 63 61 73 65 2e 20 20 42 65 67 69 6e 0a  wn case.  Begin.
2afe0 20 20 20 20 20 20 2a 2a 20 62 79 20 61 6c 6c 6f        ** by allo
2afe1 63 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c 65  cating the table
2afe2 73 20 77 65 20 77 69 6c 6c 20 6e 65 65 64 2e 0a  s we will need..
2afe3 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 74        */.      t
2afe4 61 62 31 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  ab1 = pParse->nT
2afe5 61 62 2b 2b 3b 0a 20 20 20 20 20 20 74 61 62 32  ab++;.      tab2
2afe6 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
2afe7 2b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  +;.      assert(
2afe8 20 70 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30 20   p->pOrderBy==0 
2afe9 29 3b 0a 0a 20 20 20 20 20 20 61 64 64 72 20 3d  );..      addr =
2afea 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2afeb 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68  p2(v, OP_OpenEph
2afec 65 6d 65 72 61 6c 2c 20 74 61 62 31 2c 20 30 29  emeral, tab1, 0)
2afed 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
2afee 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  p->addrOpenEphm[
2afef 30 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20 20 20  0] == -1 );.    
2aff0 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68    p->addrOpenEph
2aff1 6d 5b 30 5d 20 3d 20 61 64 64 72 3b 0a 20 20 20  m[0] = addr;.   
2aff2 20 20 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74     p->pRightmost
2aff3 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46  ->selFlags |= SF
2aff4 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c 3b 0a  _UsesEphemeral;.
2aff5 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
2aff6 3e 70 45 4c 69 73 74 20 29 3b 0a 0a 20 20 20 20  >pEList );..    
2aff7 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 53 45    /* Code the SE
2aff8 4c 45 43 54 73 20 74 6f 20 6f 75 72 20 6c 65 66  LECTs to our lef
2aff9 74 20 69 6e 74 6f 20 74 65 6d 70 6f 72 61 72 79  t into temporary
2affa 20 74 61 62 6c 65 20 22 74 61 62 31 22 2e 0a 20   table "tab1".. 
2affb 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
2affc 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49  lite3SelectDestI
2affd 6e 69 74 28 26 69 6e 74 65 72 73 65 63 74 64 65  nit(&intersectde
2affe 73 74 2c 20 53 52 54 5f 55 6e 69 6f 6e 2c 20 74  st, SRT_Union, t
2afff 61 62 31 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  ab1);.      rc =
2b000 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
2b001 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 26  Parse, pPrior, &
2b002 69 6e 74 65 72 73 65 63 74 64 65 73 74 29 3b 0a  intersectdest);.
2b003 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
2b004 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c          goto mul
2b005 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20  ti_select_end;. 
2b006 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
2b007 20 43 6f 64 65 20 74 68 65 20 63 75 72 72 65 6e   Code the curren
2b008 74 20 53 45 4c 45 43 54 20 69 6e 74 6f 20 74 65  t SELECT into te
2b009 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 22 74  mporary table "t
2b00a 61 62 32 22 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ab2".      */.  
2b00b 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
2b00c 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b00d 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
2b00e 2c 20 74 61 62 32 2c 20 30 29 3b 0a 20 20 20 20  , tab2, 0);.    
2b00f 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 64 64    assert( p->add
2b010 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 3d 20  rOpenEphm[1] == 
2b011 2d 31 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61  -1 );.      p->a
2b012 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d  ddrOpenEphm[1] =
2b013 20 61 64 64 72 3b 0a 20 20 20 20 20 20 70 2d 3e   addr;.      p->
2b014 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20  pPrior = 0;.    
2b015 20 20 70 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4c    pLimit = p->pL
2b016 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70  imit;.      p->p
2b017 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20  Limit = 0;.     
2b018 20 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f   pOffset = p->pO
2b019 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70 2d 3e  ffset;.      p->
2b01a 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20  pOffset = 0;.   
2b01b 20 20 20 69 6e 74 65 72 73 65 63 74 64 65 73 74     intersectdest
2b01c 2e 69 50 61 72 6d 20 3d 20 74 61 62 32 3b 0a 20  .iParm = tab2;. 
2b01d 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
2b01e 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
2b01f 70 2c 20 26 69 6e 74 65 72 73 65 63 74 64 65 73  p, &intersectdes
2b020 74 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  t);.      testca
2b021 73 65 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  se( rc!=SQLITE_O
2b022 4b 20 29 3b 0a 20 20 20 20 20 20 70 44 65 6c 65  K );.      pDele
2b023 74 65 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a  te = p->pPrior;.
2b024 20 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20        p->pPrior 
2b025 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20  = pPrior;.      
2b026 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
2b027 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29  e(db, p->pLimit)
2b028 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69  ;.      p->pLimi
2b029 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 20 20  t = pLimit;.    
2b02a 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20 70    p->pOffset = p
2b02b 4f 66 66 73 65 74 3b 0a 0a 20 20 20 20 20 20 2f  Offset;..      /
2b02c 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2b02d 74 6f 20 74 61 6b 65 20 74 68 65 20 69 6e 74 65  to take the inte
2b02e 72 73 65 63 74 69 6f 6e 20 6f 66 20 74 68 65 20  rsection of the 
2b02f 74 77 6f 20 74 65 6d 70 6f 72 61 72 79 0a 20 20  two temporary.  
2b030 20 20 20 20 2a 2a 20 74 61 62 6c 65 73 2e 0a 20      ** tables.. 
2b031 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 73       */.      as
2b032 73 65 72 74 28 20 70 2d 3e 70 45 4c 69 73 74 20  sert( p->pEList 
2b033 29 3b 0a 20 20 20 20 20 20 69 66 28 20 64 65 73  );.      if( des
2b034 74 2e 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74  t.eDest==SRT_Out
2b035 70 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20 53  put ){.        S
2b036 65 6c 65 63 74 20 2a 70 46 69 72 73 74 20 3d 20  elect *pFirst = 
2b037 70 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  p;.        while
2b038 28 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72  ( pFirst->pPrior
2b039 20 29 20 70 46 69 72 73 74 20 3d 20 70 46 69 72   ) pFirst = pFir
2b03a 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20  st->pPrior;.    
2b03b 20 20 20 20 67 65 6e 65 72 61 74 65 43 6f 6c 75      generateColu
2b03c 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20  mnNames(pParse, 
2b03d 30 2c 20 70 46 69 72 73 74 2d 3e 70 45 4c 69 73  0, pFirst->pELis
2b03e 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  t);.      }.    
2b03f 20 20 69 42 72 65 61 6b 20 3d 20 73 71 6c 69 74    iBreak = sqlit
2b040 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
2b041 76 29 3b 0a 20 20 20 20 20 20 69 43 6f 6e 74 20  v);.      iCont 
2b042 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
2b043 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
2b044 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65 67   computeLimitReg
2b045 69 73 74 65 72 73 28 70 50 61 72 73 65 2c 20 70  isters(pParse, p
2b046 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20  , iBreak);.     
2b047 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b048 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c  p2(v, OP_Rewind,
2b049 20 74 61 62 31 2c 20 69 42 72 65 61 6b 29 3b 0a   tab1, iBreak);.
2b04a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74        r1 = sqlit
2b04b 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2b04c 72 73 65 29 3b 0a 20 20 20 20 20 20 69 53 74 61  rse);.      iSta
2b04d 72 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  rt = sqlite3Vdbe
2b04e 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77  AddOp2(v, OP_Row
2b04f 4b 65 79 2c 20 74 61 62 31 2c 20 72 31 29 3b 0a  Key, tab1, r1);.
2b050 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2b051 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f  eAddOp3(v, OP_No
2b052 74 46 6f 75 6e 64 2c 20 74 61 62 32 2c 20 69 43  tFound, tab2, iC
2b053 6f 6e 74 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  ont, r1);.      
2b054 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2b055 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
2b056 29 3b 0a 20 20 20 20 20 20 73 65 6c 65 63 74 49  );.      selectI
2b057 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c  nnerLoop(pParse,
2b058 20 70 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 74   p, p->pEList, t
2b059 61 62 31 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e  ab1, p->pEList->
2b05a 6e 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20 20  nExpr,.         
2b05b 20 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20               0, 
2b05c 2d 31 2c 20 26 64 65 73 74 2c 20 69 43 6f 6e 74  -1, &dest, iCont
2b05d 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 20 20 20  , iBreak);.     
2b05e 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
2b05f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 43 6f 6e  lveLabel(v, iCon
2b060 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
2b061 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2b062 50 5f 4e 65 78 74 2c 20 74 61 62 31 2c 20 69 53  P_Next, tab1, iS
2b063 74 61 72 74 29 3b 0a 20 20 20 20 20 20 73 71 6c  tart);.      sql
2b064 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
2b065 61 62 65 6c 28 76 2c 20 69 42 72 65 61 6b 29 3b  abel(v, iBreak);
2b066 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2b067 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
2b068 6c 6f 73 65 2c 20 74 61 62 32 2c 20 30 29 3b 0a  lose, tab2, 0);.
2b069 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2b06a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c  eAddOp2(v, OP_Cl
2b06b 6f 73 65 2c 20 74 61 62 31 2c 20 30 29 3b 0a 20  ose, tab1, 0);. 
2b06c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2b06d 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6d 70  }.  }..  /* Comp
2b06e 75 74 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  ute collating se
2b06f 71 75 65 6e 63 65 73 20 75 73 65 64 20 62 79 20  quences used by 
2b070 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20  .  ** temporary 
2b071 74 61 62 6c 65 73 20 6e 65 65 64 65 64 20 74 6f  tables needed to
2b072 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 63   implement the c
2b073 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2e 0a  ompound select..
2b074 20 20 2a 2a 20 41 74 74 61 63 68 20 74 68 65 20    ** Attach the 
2b075 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72  KeyInfo structur
2b076 65 20 74 6f 20 61 6c 6c 20 74 65 6d 70 6f 72 61  e to all tempora
2b077 72 79 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2a 0a  ry tables..  **.
2b078 20 20 2a 2a 20 54 68 69 73 20 73 65 63 74 69 6f    ** This sectio
2b079 6e 20 69 73 20 72 75 6e 20 62 79 20 74 68 65 20  n is run by the 
2b07a 72 69 67 68 74 2d 6d 6f 73 74 20 53 45 4c 45 43  right-most SELEC
2b07b 54 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e 6c 79  T statement only
2b07c 2e 0a 20 20 2a 2a 20 53 45 4c 45 43 54 20 73 74  ..  ** SELECT st
2b07d 61 74 65 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  atements to the 
2b07e 6c 65 66 74 20 61 6c 77 61 79 73 20 73 6b 69 70  left always skip
2b07f 20 74 68 69 73 20 70 61 72 74 2e 20 20 54 68 65   this part.  The
2b080 20 72 69 67 68 74 2d 6d 6f 73 74 0a 20 20 2a 2a   right-most.  **
2b081 20 53 45 4c 45 43 54 20 6d 69 67 68 74 20 61 6c   SELECT might al
2b082 73 6f 20 73 6b 69 70 20 74 68 69 73 20 70 61 72  so skip this par
2b083 74 20 69 66 20 69 74 20 68 61 73 20 6e 6f 20 4f  t if it has no O
2b084 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 61  RDER BY clause a
2b085 6e 64 0a 20 20 2a 2a 20 6e 6f 20 74 65 6d 70 20  nd.  ** no temp 
2b086 74 61 62 6c 65 73 20 61 72 65 20 72 65 71 75 69  tables are requi
2b087 72 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  red..  */.  if( 
2b088 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  p->selFlags & SF
2b089 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c 20 29  _UsesEphemeral )
2b08a 7b 0a 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20  {.    int i;    
2b08b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b08c 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2b08d 74 65 72 20 2a 2f 0a 20 20 20 20 4b 65 79 49 6e  ter */.    KeyIn
2b08e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20 20  fo *pKeyInfo;   
2b08f 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c           /* Coll
2b090 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66  ating sequence f
2b091 6f 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  or the result se
2b092 74 20 2a 2f 0a 20 20 20 20 53 65 6c 65 63 74 20  t */.    Select 
2b093 2a 70 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20 20  *pLoop;         
2b094 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f         /* For lo
2b095 6f 70 69 6e 67 20 74 68 72 6f 75 67 68 20 53 45  oping through SE
2b096 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20  LECT statements 
2b097 2a 2f 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  */.    CollSeq *
2b098 2a 61 70 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20  *apColl;        
2b099 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70       /* For loop
2b09a 69 6e 67 20 74 68 72 6f 75 67 68 20 70 4b 65 79  ing through pKey
2b09b 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 5d 20 2a 2f  Info->aColl[] */
2b09c 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20  .    int nCol;  
2b09d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b09e 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2b09f 63 6f 6c 75 6d 6e 73 20 69 6e 20 72 65 73 75 6c  columns in resul
2b0a0 74 20 73 65 74 20 2a 2f 0a 0a 20 20 20 20 61 73  t set */..    as
2b0a1 73 65 72 74 28 20 70 2d 3e 70 52 69 67 68 74 6d  sert( p->pRightm
2b0a2 6f 73 74 3d 3d 70 20 29 3b 0a 20 20 20 20 6e 43  ost==p );.    nC
2b0a3 6f 6c 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e  ol = p->pEList->
2b0a4 6e 45 78 70 72 3b 0a 20 20 20 20 70 4b 65 79 49  nExpr;.    pKeyI
2b0a5 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  nfo = sqlite3DbM
2b0a6 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 0a 20 20  allocZero(db,.  
2b0a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b0a8 20 20 20 20 20 73 69 7a 65 6f 66 28 2a 70 4b 65       sizeof(*pKe
2b0a9 79 49 6e 66 6f 29 2b 6e 43 6f 6c 2a 28 73 69 7a  yInfo)+nCol*(siz
2b0aa 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 20 2b 20  eof(CollSeq*) + 
2b0ab 31 29 29 3b 0a 20 20 20 20 69 66 28 20 21 70 4b  1));.    if( !pK
2b0ac 65 79 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20  eyInfo ){.      
2b0ad 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
2b0ae 4d 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6d 75  M;.      goto mu
2b0af 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  lti_select_end;.
2b0b0 20 20 20 20 7d 0a 0a 20 20 20 20 70 4b 65 79 49      }..    pKeyI
2b0b1 6e 66 6f 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64  nfo->enc = ENC(d
2b0b2 62 29 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f  b);.    pKeyInfo
2b0b3 2d 3e 6e 46 69 65 6c 64 20 3d 20 28 75 31 36 29  ->nField = (u16)
2b0b4 6e 43 6f 6c 3b 0a 0a 20 20 20 20 66 6f 72 28 69  nCol;..    for(i
2b0b5 3d 30 2c 20 61 70 43 6f 6c 6c 3d 70 4b 65 79 49  =0, apColl=pKeyI
2b0b6 6e 66 6f 2d 3e 61 43 6f 6c 6c 3b 20 69 3c 6e 43  nfo->aColl; i<nC
2b0b7 6f 6c 3b 20 69 2b 2b 2c 20 61 70 43 6f 6c 6c 2b  ol; i++, apColl+
2b0b8 2b 29 7b 0a 20 20 20 20 20 20 2a 61 70 43 6f 6c  +){.      *apCol
2b0b9 6c 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63 74 43  l = multiSelectC
2b0ba 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
2b0bb 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , i);.      if( 
2b0bc 30 3d 3d 2a 61 70 43 6f 6c 6c 20 29 7b 0a 20 20  0==*apColl ){.  
2b0bd 20 20 20 20 20 20 2a 61 70 43 6f 6c 6c 20 3d 20        *apColl = 
2b0be 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20  db->pDfltColl;. 
2b0bf 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
2b0c0 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 3b 20 70    for(pLoop=p; p
2b0c1 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f 6f  Loop; pLoop=pLoo
2b0c2 70 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20 20 20  p->pPrior){.    
2b0c3 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 3b 20    for(i=0; i<2; 
2b0c4 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  i++){.        in
2b0c5 74 20 61 64 64 72 20 3d 20 70 4c 6f 6f 70 2d 3e  t addr = pLoop->
2b0c6 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 69 5d 3b  addrOpenEphm[i];
2b0c7 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 64 64  .        if( add
2b0c8 72 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  r<0 ){.         
2b0c9 20 2f 2a 20 49 66 20 5b 30 5d 20 69 73 20 75 6e   /* If [0] is un
2b0ca 75 73 65 64 20 74 68 65 6e 20 5b 31 5d 20 69 73  used then [1] is
2b0cb 20 61 6c 73 6f 20 75 6e 75 73 65 64 2e 20 20 53   also unused.  S
2b0cc 6f 20 77 65 20 63 61 6e 0a 20 20 20 20 20 20 20  o we can.       
2b0cd 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 73 61 66     ** always saf
2b0ce 65 6c 79 20 61 62 6f 72 74 20 61 73 20 73 6f 6f  ely abort as soo
2b0cf 6e 20 61 73 20 74 68 65 20 66 69 72 73 74 20 75  n as the first u
2b0d0 6e 75 73 65 64 20 73 6c 6f 74 20 69 73 20 66 6f  nused slot is fo
2b0d1 75 6e 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  und */.         
2b0d2 20 61 73 73 65 72 74 28 20 70 4c 6f 6f 70 2d 3e   assert( pLoop->
2b0d3 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 3c  addrOpenEphm[1]<
2b0d4 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62  0 );.          b
2b0d5 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
2b0d6 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2b0d7 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 61  dbeChangeP2(v, a
2b0d8 64 64 72 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20 20  ddr, nCol);.    
2b0d9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2b0da 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72 2c  hangeP4(v, addr,
2b0db 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f   (char*)pKeyInfo
2b0dc 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20  , P4_KEYINFO);. 
2b0dd 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 61 64         pLoop->ad
2b0de 64 72 4f 70 65 6e 45 70 68 6d 5b 69 5d 20 3d 20  drOpenEphm[i] = 
2b0df 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  -1;.      }.    
2b0e0 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  }.    sqlite3DbF
2b0e1 72 65 65 28 64 62 2c 20 70 4b 65 79 49 6e 66 6f  ree(db, pKeyInfo
2b0e2 29 3b 0a 20 20 7d 0a 0a 6d 75 6c 74 69 5f 73 65  );.  }..multi_se
2b0e3 6c 65 63 74 5f 65 6e 64 3a 0a 20 20 70 44 65 73  lect_end:.  pDes
2b0e4 74 2d 3e 69 4d 65 6d 20 3d 20 64 65 73 74 2e 69  t->iMem = dest.i
2b0e5 4d 65 6d 3b 0a 20 20 70 44 65 73 74 2d 3e 6e 4d  Mem;.  pDest->nM
2b0e6 65 6d 20 3d 20 64 65 73 74 2e 6e 4d 65 6d 3b 0a  em = dest.nMem;.
2b0e7 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
2b0e8 65 6c 65 74 65 28 64 62 2c 20 70 44 65 6c 65 74  elete(db, pDelet
2b0e9 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  e);.  return rc;
2b0ea 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
2b0eb 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e  ITE_OMIT_COMPOUN
2b0ec 44 5f 53 45 4c 45 43 54 20 2a 2f 0a 0a 2f 2a 0a  D_SELECT */../*.
2b0ed 2a 2a 20 43 6f 64 65 20 61 6e 20 6f 75 74 70 75  ** Code an outpu
2b0ee 74 20 73 75 62 72 6f 75 74 69 6e 65 20 66 6f 72  t subroutine for
2b0ef 20 61 20 63 6f 72 6f 75 74 69 6e 65 20 69 6d 70   a coroutine imp
2b0f0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61  lementation of a
2b0f1 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74 6d  .** SELECT statm
2b0f2 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  ent..**.** The d
2b0f3 61 74 61 20 74 6f 20 62 65 20 6f 75 74 70 75 74  ata to be output
2b0f4 20 69 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e   is contained in
2b0f5 20 70 49 6e 2d 3e 69 4d 65 6d 2e 20 20 54 68 65   pIn->iMem.  The
2b0f6 72 65 20 61 72 65 0a 2a 2a 20 70 49 6e 2d 3e 6e  re are.** pIn->n
2b0f7 4d 65 6d 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62  Mem columns to b
2b0f8 65 20 6f 75 74 70 75 74 2e 20 20 70 44 65 73 74  e output.  pDest
2b0f9 20 69 73 20 77 68 65 72 65 20 74 68 65 20 6f 75   is where the ou
2b0fa 74 70 75 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62  tput should.** b
2b0fb 65 20 73 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 72 65  e sent..**.** re
2b0fc 67 52 65 74 75 72 6e 20 69 73 20 74 68 65 20 6e  gReturn is the n
2b0fd 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72 65 67  umber of the reg
2b0fe 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 74 68  ister holding th
2b0ff 65 20 73 75 62 72 6f 75 74 69 6e 65 0a 2a 2a 20  e subroutine.** 
2b100 72 65 74 75 72 6e 20 61 64 64 72 65 73 73 2e 0a  return address..
2b101 2a 2a 0a 2a 2a 20 49 66 20 72 65 67 50 72 65 76  **.** If regPrev
2b102 3e 30 20 74 68 65 6e 20 69 74 20 69 73 20 61 20  >0 then it is a 
2b103 74 68 65 20 66 69 72 73 74 20 72 65 67 69 73 74  the first regist
2b104 65 72 20 69 6e 20 61 20 76 65 63 74 6f 72 20 74  er in a vector t
2b105 68 61 74 0a 2a 2a 20 72 65 63 6f 72 64 73 20 74  hat.** records t
2b106 68 65 20 70 72 65 76 69 6f 75 73 20 6f 75 74 70  he previous outp
2b107 75 74 2e 20 20 6d 65 6d 5b 72 65 67 50 72 65 76  ut.  mem[regPrev
2b108 5d 20 69 73 20 61 20 66 6c 61 67 20 74 68 61 74  ] is a flag that
2b109 20 69 73 20 66 61 6c 73 65 0a 2a 2a 20 69 66 20   is false.** if 
2b10a 74 68 65 72 65 20 68 61 73 20 62 65 65 6e 20 6e  there has been n
2b10b 6f 20 70 72 65 76 69 6f 75 73 20 6f 75 74 70 75  o previous outpu
2b10c 74 2e 20 20 49 66 20 72 65 67 50 72 65 76 3e 30  t.  If regPrev>0
2b10d 20 74 68 65 6e 20 63 6f 64 65 20 69 73 0a 2a 2a   then code is.**
2b10e 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 73 75   generated to su
2b10f 70 70 72 65 73 73 20 64 75 70 6c 69 63 61 74 65  ppress duplicate
2b110 73 2e 20 20 70 4b 65 79 49 6e 66 6f 20 69 73 20  s.  pKeyInfo is 
2b111 75 73 65 64 20 66 6f 72 20 63 6f 6d 70 61 72 69  used for compari
2b112 6e 67 0a 2a 2a 20 6b 65 79 73 2e 0a 2a 2a 0a 2a  ng.** keys..**.*
2b113 2a 20 49 66 20 74 68 65 20 4c 49 4d 49 54 20 66  * If the LIMIT f
2b114 6f 75 6e 64 20 69 6e 20 70 2d 3e 69 4c 69 6d 69  ound in p->iLimi
2b115 74 20 69 73 20 72 65 61 63 68 65 64 2c 20 6a 75  t is reached, ju
2b116 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  mp immediately t
2b117 6f 0a 2a 2a 20 69 42 72 65 61 6b 2e 0a 2a 2f 0a  o.** iBreak..*/.
2b118 73 74 61 74 69 63 20 69 6e 74 20 67 65 6e 65 72  static int gener
2b119 61 74 65 4f 75 74 70 75 74 53 75 62 72 6f 75 74  ateOutputSubrout
2b11a 69 6e 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ine(.  Parse *pP
2b11b 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
2b11c 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
2b11d 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  t */.  Select *p
2b11e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
2b11f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
2b120 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 6c 65  tement */.  Sele
2b121 63 74 44 65 73 74 20 2a 70 49 6e 2c 20 20 20 20  ctDest *pIn,    
2b122 20 20 20 20 2f 2a 20 43 6f 72 6f 75 74 69 6e 65      /* Coroutine
2b123 20 73 75 70 70 6c 79 69 6e 67 20 64 61 74 61 20   supplying data 
2b124 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20  */.  SelectDest 
2b125 2a 70 44 65 73 74 2c 20 20 20 20 20 20 2f 2a 20  *pDest,      /* 
2b126 57 68 65 72 65 20 74 6f 20 73 65 6e 64 20 74 68  Where to send th
2b127 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74 20  e data */.  int 
2b128 72 65 67 52 65 74 75 72 6e 2c 20 20 20 20 20 20  regReturn,      
2b129 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75 72      /* The retur
2b12a 6e 20 61 64 64 72 65 73 73 20 72 65 67 69 73 74  n address regist
2b12b 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 50  er */.  int regP
2b12c 72 65 76 2c 20 20 20 20 20 20 20 20 20 20 20 20  rev,            
2b12d 2f 2a 20 50 72 65 76 69 6f 75 73 20 72 65 73 75  /* Previous resu
2b12e 6c 74 20 72 65 67 69 73 74 65 72 2e 20 20 4e 6f  lt register.  No
2b12f 20 75 6e 69 71 75 65 6e 65 73 73 20 69 66 20 30   uniqueness if 0
2b130 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70   */.  KeyInfo *p
2b131 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 2f 2a  KeyInfo,      /*
2b132 20 46 6f 72 20 63 6f 6d 70 61 72 69 6e 67 20 77   For comparing w
2b133 69 74 68 20 70 72 65 76 69 6f 75 73 20 65 6e 74  ith previous ent
2b134 72 79 20 2a 2f 0a 20 20 69 6e 74 20 70 34 74 79  ry */.  int p4ty
2b135 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  pe,             
2b136 2f 2a 20 54 68 65 20 70 34 20 74 79 70 65 20 66  /* The p4 type f
2b137 6f 72 20 70 4b 65 79 49 6e 66 6f 20 2a 2f 0a 20  or pKeyInfo */. 
2b138 20 69 6e 74 20 69 42 72 65 61 6b 20 20 20 20 20   int iBreak     
2b139 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70           /* Jump
2b13a 20 68 65 72 65 20 69 66 20 77 65 20 68 69 74 20   here if we hit 
2b13b 74 68 65 20 4c 49 4d 49 54 20 2a 2f 0a 29 7b 0a  the LIMIT */.){.
2b13c 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
2b13d 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74  se->pVdbe;.  int
2b13e 20 69 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 69 6e   iContinue;.  in
2b13f 74 20 61 64 64 72 3b 0a 0a 20 20 61 64 64 72 20  t addr;..  addr 
2b140 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72  = sqlite3VdbeCur
2b141 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 69  rentAddr(v);.  i
2b142 43 6f 6e 74 69 6e 75 65 20 3d 20 73 71 6c 69 74  Continue = sqlit
2b143 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
2b144 76 29 3b 0a 0a 20 20 2f 2a 20 53 75 70 70 72 65  v);..  /* Suppre
2b145 73 73 20 64 75 70 6c 69 63 61 74 65 73 20 66 6f  ss duplicates fo
2b146 72 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54 2c  r UNION, EXCEPT,
2b147 20 61 6e 64 20 49 4e 54 45 52 53 45 43 54 20 0a   and INTERSECT .
2b148 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 67 50 72    */.  if( regPr
2b149 65 76 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 31  ev ){.    int j1
2b14a 2c 20 6a 32 3b 0a 20 20 20 20 6a 31 20 3d 20 73  , j2;.    j1 = s
2b14b 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2b14c 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 72 65  (v, OP_IfNot, re
2b14d 67 50 72 65 76 29 3b 0a 20 20 20 20 6a 32 20 3d  gPrev);.    j2 =
2b14e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2b14f 70 34 28 76 2c 20 4f 50 5f 43 6f 6d 70 61 72 65  p4(v, OP_Compare
2b150 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 72 65 67  , pIn->iMem, reg
2b151 50 72 65 76 2b 31 2c 20 70 49 6e 2d 3e 6e 4d 65  Prev+1, pIn->nMe
2b152 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  m,.             
2b153 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b154 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f   (char*)pKeyInfo
2b155 2c 20 70 34 74 79 70 65 29 3b 0a 20 20 20 20 73  , p4type);.    s
2b156 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2b157 28 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 6a 32 2b  (v, OP_Jump, j2+
2b158 32 2c 20 69 43 6f 6e 74 69 6e 75 65 2c 20 6a 32  2, iContinue, j2
2b159 2b 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  +2);.    sqlite3
2b15a 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
2b15b 6a 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  j1);.    sqlite3
2b15c 45 78 70 72 43 6f 64 65 43 6f 70 79 28 70 50 61  ExprCodeCopy(pPa
2b15d 72 73 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20  rse, pIn->iMem, 
2b15e 72 65 67 50 72 65 76 2b 31 2c 20 70 49 6e 2d 3e  regPrev+1, pIn->
2b15f 6e 4d 65 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74  nMem);.    sqlit
2b160 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b161 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 72  OP_Integer, 1, r
2b162 65 67 50 72 65 76 29 3b 0a 20 20 7d 0a 20 20 69  egPrev);.  }.  i
2b163 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d  f( pParse->db->m
2b164 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65  allocFailed ) re
2b165 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 53 75  turn 0;..  /* Su
2b166 70 70 72 65 73 73 20 74 68 65 20 74 68 65 20 66  ppress the the f
2b167 69 72 73 74 20 4f 46 46 53 45 54 20 65 6e 74 72  irst OFFSET entr
2b168 69 65 73 20 69 66 20 74 68 65 72 65 20 69 73 20  ies if there is 
2b169 61 6e 20 4f 46 46 53 45 54 20 63 6c 61 75 73 65  an OFFSET clause
2b16a 0a 20 20 2a 2f 0a 20 20 63 6f 64 65 4f 66 66 73  .  */.  codeOffs
2b16b 65 74 28 76 2c 20 70 2c 20 69 43 6f 6e 74 69 6e  et(v, p, iContin
2b16c 75 65 29 3b 0a 0a 20 20 73 77 69 74 63 68 28 20  ue);..  switch( 
2b16d 70 44 65 73 74 2d 3e 65 44 65 73 74 20 29 7b 0a  pDest->eDest ){.
2b16e 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68 65      /* Store the
2b16f 20 72 65 73 75 6c 74 20 61 73 20 64 61 74 61 20   result as data 
2b170 75 73 69 6e 67 20 61 20 75 6e 69 71 75 65 20 6b  using a unique k
2b171 65 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  ey..    */.    c
2b172 61 73 65 20 53 52 54 5f 54 61 62 6c 65 3a 0a 20  ase SRT_Table:. 
2b173 20 20 20 63 61 73 65 20 53 52 54 5f 45 70 68 65     case SRT_Ephe
2b174 6d 54 61 62 3a 20 7b 0a 20 20 20 20 20 20 69 6e  mTab: {.      in
2b175 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65  t r1 = sqlite3Ge
2b176 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
2b177 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 32 20 3d  ;.      int r2 =
2b178 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2b179 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
2b17a 20 20 74 65 73 74 63 61 73 65 28 20 70 44 65 73    testcase( pDes
2b17b 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 54 61  t->eDest==SRT_Ta
2b17c 62 6c 65 20 29 3b 0a 20 20 20 20 20 20 74 65 73  ble );.      tes
2b17d 74 63 61 73 65 28 20 70 44 65 73 74 2d 3e 65 44  tcase( pDest->eD
2b17e 65 73 74 3d 3d 53 52 54 5f 45 70 68 65 6d 54 61  est==SRT_EphemTa
2b17f 62 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  b );.      sqlit
2b180 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2b181 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 70  OP_MakeRecord, p
2b182 49 6e 2d 3e 69 4d 65 6d 2c 20 70 49 6e 2d 3e 6e  In->iMem, pIn->n
2b183 4d 65 6d 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  Mem, r1);.      
2b184 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b185 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64  2(v, OP_NewRowid
2b186 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2c 20  , pDest->iParm, 
2b187 72 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  r2);.      sqlit
2b188 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2b189 4f 50 5f 49 6e 73 65 72 74 2c 20 70 44 65 73 74  OP_Insert, pDest
2b18a 2d 3e 69 50 61 72 6d 2c 20 72 31 2c 20 72 32 29  ->iParm, r1, r2)
2b18b 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2b18c 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f  dbeChangeP5(v, O
2b18d 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20  PFLAG_APPEND);. 
2b18e 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
2b18f 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
2b190 65 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 73 71  e, r2);.      sq
2b191 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2b192 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
2b193 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2b194 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c    }..#ifndef SQL
2b195 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
2b196 59 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61  Y.    /* If we a
2b197 72 65 20 63 72 65 61 74 69 6e 67 20 61 20 73 65  re creating a se
2b198 74 20 66 6f 72 20 61 6e 20 22 65 78 70 72 20 49  t for an "expr I
2b199 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e 29 22 20  N (SELECT ...)" 
2b19a 63 6f 6e 73 74 72 75 63 74 2c 0a 20 20 20 20 2a  construct,.    *
2b19b 2a 20 74 68 65 6e 20 74 68 65 72 65 20 73 68 6f  * then there sho
2b19c 75 6c 64 20 62 65 20 61 20 73 69 6e 67 6c 65 20  uld be a single 
2b19d 69 74 65 6d 20 6f 6e 20 74 68 65 20 73 74 61 63  item on the stac
2b19e 6b 2e 20 20 57 72 69 74 65 20 74 68 69 73 0a 20  k.  Write this. 
2b19f 20 20 20 2a 2a 20 69 74 65 6d 20 69 6e 74 6f 20     ** item into 
2b1a0 74 68 65 20 73 65 74 20 74 61 62 6c 65 20 77 69  the set table wi
2b1a1 74 68 20 62 6f 67 75 73 20 64 61 74 61 2e 0a 20  th bogus data.. 
2b1a2 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53     */.    case S
2b1a3 52 54 5f 53 65 74 3a 20 7b 0a 20 20 20 20 20 20  RT_Set: {.      
2b1a4 69 6e 74 20 72 31 3b 0a 20 20 20 20 20 20 61 73  int r1;.      as
2b1a5 73 65 72 74 28 20 70 49 6e 2d 3e 6e 4d 65 6d 3d  sert( pIn->nMem=
2b1a6 3d 31 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61  =1 );.      p->a
2b1a7 66 66 69 6e 69 74 79 20 3d 20 0a 20 20 20 20 20  ffinity = .     
2b1a8 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6d 70 61      sqlite3Compa
2b1a9 72 65 41 66 66 69 6e 69 74 79 28 70 2d 3e 70 45  reAffinity(p->pE
2b1aa 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
2b1ab 2c 20 70 44 65 73 74 2d 3e 61 66 66 69 6e 69 74  , pDest->affinit
2b1ac 79 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73  y);.      r1 = s
2b1ad 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
2b1ae 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
2b1af 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b1b0 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  4(v, OP_MakeReco
2b1b1 72 64 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 31  rd, pIn->iMem, 1
2b1b2 2c 20 72 31 2c 20 26 70 2d 3e 61 66 66 69 6e 69  , r1, &p->affini
2b1b3 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71  ty, 1);.      sq
2b1b4 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41 66  lite3ExprCacheAf
2b1b5 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61  finityChange(pPa
2b1b6 72 73 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20  rse, pIn->iMem, 
2b1b7 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
2b1b8 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2b1b9 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70 44 65  P_IdxInsert, pDe
2b1ba 73 74 2d 3e 69 50 61 72 6d 2c 20 72 31 29 3b 0a  st->iParm, r1);.
2b1bb 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
2b1bc 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
2b1bd 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 62  se, r1);.      b
2b1be 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66  reak;.    }..#if
2b1bf 20 30 20 20 2f 2a 20 4e 65 76 65 72 20 6f 63 63   0  /* Never occ
2b1c0 75 72 73 20 6f 6e 20 61 6e 20 4f 52 44 45 52 20  urs on an ORDER 
2b1c1 42 59 20 71 75 65 72 79 20 2a 2f 0a 20 20 20 20  BY query */.    
2b1c2 2f 2a 20 49 66 20 61 6e 79 20 72 6f 77 20 65 78  /* If any row ex
2b1c3 69 73 74 20 69 6e 20 74 68 65 20 72 65 73 75 6c  ist in the resul
2b1c4 74 20 73 65 74 2c 20 72 65 63 6f 72 64 20 74 68  t set, record th
2b1c5 61 74 20 66 61 63 74 20 61 6e 64 20 61 62 6f 72  at fact and abor
2b1c6 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  t..    */.    ca
2b1c7 73 65 20 53 52 54 5f 45 78 69 73 74 73 3a 20 7b  se SRT_Exists: {
2b1c8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2b1c9 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2b1ca 6e 74 65 67 65 72 2c 20 31 2c 20 70 44 65 73 74  nteger, 1, pDest
2b1cb 2d 3e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20  ->iParm);.      
2b1cc 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 63 6c 61  /* The LIMIT cla
2b1cd 75 73 65 20 77 69 6c 6c 20 74 65 72 6d 69 6e 61  use will termina
2b1ce 74 65 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20  te the loop for 
2b1cf 75 73 20 2a 2f 0a 20 20 20 20 20 20 62 72 65 61  us */.      brea
2b1d0 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  k;.    }.#endif.
2b1d1 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20  .    /* If this 
2b1d2 69 73 20 61 20 73 63 61 6c 61 72 20 73 65 6c 65  is a scalar sele
2b1d3 63 74 20 74 68 61 74 20 69 73 20 70 61 72 74 20  ct that is part 
2b1d4 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  of an expression
2b1d5 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 73 74  , then.    ** st
2b1d6 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 73 20  ore the results 
2b1d7 69 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  in the appropria
2b1d8 74 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61  te memory cell a
2b1d9 6e 64 20 62 72 65 61 6b 20 6f 75 74 0a 20 20 20  nd break out.   
2b1da 20 2a 2a 20 6f 66 20 74 68 65 20 73 63 61 6e 20   ** of the scan 
2b1db 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  loop..    */.   
2b1dc 20 63 61 73 65 20 53 52 54 5f 4d 65 6d 3a 20 7b   case SRT_Mem: {
2b1dd 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2b1de 49 6e 2d 3e 6e 4d 65 6d 3d 3d 31 20 29 3b 0a 20  In->nMem==1 );. 
2b1df 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2b1e0 43 6f 64 65 4d 6f 76 65 28 70 50 61 72 73 65 2c  CodeMove(pParse,
2b1e1 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 70 44 65 73   pIn->iMem, pDes
2b1e2 74 2d 3e 69 50 61 72 6d 2c 20 31 29 3b 0a 20 20  t->iParm, 1);.  
2b1e3 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49 54      /* The LIMIT
2b1e4 20 63 6c 61 75 73 65 20 77 69 6c 6c 20 6a 75 6d   clause will jum
2b1e5 70 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f 6f  p out of the loo
2b1e6 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20 20 20  p for us */.    
2b1e7 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23    break;.    }.#
2b1e8 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66  endif /* #ifndef
2b1e9 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
2b1ea 51 55 45 52 59 20 2a 2f 0a 0a 20 20 20 20 2f 2a  QUERY */..    /*
2b1eb 20 54 68 65 20 72 65 73 75 6c 74 73 20 61 72 65   The results are
2b1ec 20 73 74 6f 72 65 64 20 69 6e 20 61 20 73 65 71   stored in a seq
2b1ed 75 65 6e 63 65 20 6f 66 20 72 65 67 69 73 74 65  uence of registe
2b1ee 72 73 0a 20 20 20 20 2a 2a 20 73 74 61 72 74 69  rs.    ** starti
2b1ef 6e 67 20 61 74 20 70 44 65 73 74 2d 3e 69 4d 65  ng at pDest->iMe
2b1f0 6d 2e 20 20 54 68 65 6e 20 74 68 65 20 63 6f 2d  m.  Then the co-
2b1f1 72 6f 75 74 69 6e 65 20 79 69 65 6c 64 73 2e 0a  routine yields..
2b1f2 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
2b1f3 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 3a 20 7b  SRT_Coroutine: {
2b1f4 0a 20 20 20 20 20 20 69 66 28 20 70 44 65 73 74  .      if( pDest
2b1f5 2d 3e 69 4d 65 6d 3d 3d 30 20 29 7b 0a 20 20 20  ->iMem==0 ){.   
2b1f6 20 20 20 20 20 70 44 65 73 74 2d 3e 69 4d 65 6d       pDest->iMem
2b1f7 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2b1f8 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70  pRange(pParse, p
2b1f9 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 20  In->nMem);.     
2b1fa 20 20 20 70 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d     pDest->nMem =
2b1fb 20 70 49 6e 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20   pIn->nMem;.    
2b1fc 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
2b1fd 33 45 78 70 72 43 6f 64 65 4d 6f 76 65 28 70 50  3ExprCodeMove(pP
2b1fe 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c  arse, pIn->iMem,
2b1ff 20 70 44 65 73 74 2d 3e 69 4d 65 6d 2c 20 70 44   pDest->iMem, pD
2b200 65 73 74 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20  est->nMem);.    
2b201 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b202 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c  Op1(v, OP_Yield,
2b203 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 29 3b 0a   pDest->iParm);.
2b204 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2b205 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 6e 6f   }..    /* If no
2b206 6e 65 20 6f 66 20 74 68 65 20 61 62 6f 76 65 2c  ne of the above,
2b207 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74   then the result
2b208 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 6d 75 73   destination mus
2b209 74 20 62 65 0a 20 20 20 20 2a 2a 20 53 52 54 5f  t be.    ** SRT_
2b20a 4f 75 74 70 75 74 2e 20 20 54 68 69 73 20 72 6f  Output.  This ro
2b20b 75 74 69 6e 65 20 69 73 20 6e 65 76 65 72 20 63  utine is never c
2b20c 61 6c 6c 65 64 20 77 69 74 68 20 61 6e 79 20 6f  alled with any o
2b20d 74 68 65 72 0a 20 20 20 20 2a 2a 20 64 65 73 74  ther.    ** dest
2b20e 69 6e 61 74 69 6f 6e 20 6f 74 68 65 72 20 74 68  ination other th
2b20f 61 6e 20 74 68 65 20 6f 6e 65 73 20 68 61 6e 64  an the ones hand
2b210 6c 65 64 20 61 62 6f 76 65 20 6f 72 20 53 52 54  led above or SRT
2b211 5f 4f 75 74 70 75 74 2e 0a 20 20 20 20 2a 2a 0a  _Output..    **.
2b212 20 20 20 20 2a 2a 20 46 6f 72 20 53 52 54 5f 4f      ** For SRT_O
2b213 75 74 70 75 74 2c 20 72 65 73 75 6c 74 73 20 61  utput, results a
2b214 72 65 20 73 74 6f 72 65 64 20 69 6e 20 61 20 73  re stored in a s
2b215 65 71 75 65 6e 63 65 20 6f 66 20 72 65 67 69 73  equence of regis
2b216 74 65 72 73 2e 20 20 0a 20 20 20 20 2a 2a 20 54  ters.  .    ** T
2b217 68 65 6e 20 74 68 65 20 4f 50 5f 52 65 73 75 6c  hen the OP_Resul
2b218 74 52 6f 77 20 6f 70 63 6f 64 65 20 69 73 20 75  tRow opcode is u
2b219 73 65 64 20 74 6f 20 63 61 75 73 65 20 73 71 6c  sed to cause sql
2b21a 69 74 65 33 5f 73 74 65 70 28 29 20 74 6f 0a 20  ite3_step() to. 
2b21b 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 74 68 65     ** return the
2b21c 20 6e 65 78 74 20 72 6f 77 20 6f 66 20 72 65 73   next row of res
2b21d 75 6c 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ult..    */.    
2b21e 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
2b21f 20 61 73 73 65 72 74 28 20 70 44 65 73 74 2d 3e   assert( pDest->
2b220 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70 75  eDest==SRT_Outpu
2b221 74 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  t );.      sqlit
2b222 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b223 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 70 49  OP_ResultRow, pI
2b224 6e 2d 3e 69 4d 65 6d 2c 20 70 49 6e 2d 3e 6e 4d  n->iMem, pIn->nM
2b225 65 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  em);.      sqlit
2b226 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e  e3ExprCacheAffin
2b227 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65  ityChange(pParse
2b228 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 70 49 6e  , pIn->iMem, pIn
2b229 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 20 20 62  ->nMem);.      b
2b22a 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
2b22b 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68  .  /* Jump to th
2b22c 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f  e end of the loo
2b22d 70 20 69 66 20 74 68 65 20 4c 49 4d 49 54 20 69  p if the LIMIT i
2b22e 73 20 72 65 61 63 68 65 64 2e 0a 20 20 2a 2f 0a  s reached..  */.
2b22f 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20    if( p->iLimit 
2b230 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
2b231 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
2b232 64 64 49 6d 6d 2c 20 70 2d 3e 69 4c 69 6d 69 74  ddImm, p->iLimit
2b233 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  , -1);.    sqlit
2b234 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b235 4f 50 5f 49 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c  OP_IfZero, p->iL
2b236 69 6d 69 74 2c 20 69 42 72 65 61 6b 29 3b 0a 20  imit, iBreak);. 
2b237 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74   }..  /* Generat
2b238 65 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65  e the subroutine
2b239 20 72 65 74 75 72 6e 0a 20 20 2a 2f 0a 20 20 73   return.  */.  s
2b23a 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
2b23b 65 4c 61 62 65 6c 28 76 2c 20 69 43 6f 6e 74 69  eLabel(v, iConti
2b23c 6e 75 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  nue);.  sqlite3V
2b23d 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
2b23e 52 65 74 75 72 6e 2c 20 72 65 67 52 65 74 75 72  Return, regRetur
2b23f 6e 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 61 64  n);..  return ad
2b240 64 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 74  dr;.}../*.** Alt
2b241 65 72 6e 61 74 69 76 65 20 63 6f 6d 70 6f 75 6e  ernative compoun
2b242 64 20 73 65 6c 65 63 74 20 63 6f 64 65 20 67 65  d select code ge
2b243 6e 65 72 61 74 6f 72 20 66 6f 72 20 63 61 73 65  nerator for case
2b244 73 20 77 68 65 6e 20 74 68 65 72 65 0a 2a 2a 20  s when there.** 
2b245 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63  is an ORDER BY c
2b246 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 65 20  lause..**.** We 
2b247 61 73 73 75 6d 65 20 61 20 71 75 65 72 79 20 6f  assume a query o
2b248 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
2b249 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  form:.**.**     
2b24a 20 3c 73 65 6c 65 63 74 41 3e 20 20 3c 6f 70 65   <selectA>  <ope
2b24b 72 61 74 6f 72 3e 20 20 3c 73 65 6c 65 63 74 42  rator>  <selectB
2b24c 3e 20 20 4f 52 44 45 52 20 42 59 20 3c 6f 72 64  >  ORDER BY <ord
2b24d 65 72 62 79 6c 69 73 74 3e 0a 2a 2a 0a 2a 2a 20  erbylist>.**.** 
2b24e 3c 6f 70 65 72 61 74 6f 72 3e 20 69 73 20 6f 6e  <operator> is on
2b24f 65 20 6f 66 20 55 4e 49 4f 4e 20 41 4c 4c 2c 20  e of UNION ALL, 
2b250 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54 2c 20 6f  UNION, EXCEPT, o
2b251 72 20 49 4e 54 45 52 53 45 43 54 2e 20 20 54 68  r INTERSECT.  Th
2b252 65 20 69 64 65 61 0a 2a 2a 20 69 73 20 74 6f 20  e idea.** is to 
2b253 63 6f 64 65 20 62 6f 74 68 20 3c 73 65 6c 65 63  code both <selec
2b254 74 41 3e 20 61 6e 64 20 3c 73 65 6c 65 63 74 42  tA> and <selectB
2b255 3e 20 77 69 74 68 20 74 68 65 20 4f 52 44 45 52  > with the ORDER
2b256 20 42 59 20 63 6c 61 75 73 65 20 61 73 0a 2a 2a   BY clause as.**
2b257 20 63 6f 2d 72 6f 75 74 69 6e 65 73 2e 20 20 54   co-routines.  T
2b258 68 65 6e 20 72 75 6e 20 74 68 65 20 63 6f 2d 72  hen run the co-r
2b259 6f 75 74 69 6e 65 73 20 69 6e 20 70 61 72 61 6c  outines in paral
2b25a 6c 65 6c 20 61 6e 64 20 6d 65 72 67 65 20 74 68  lel and merge th
2b25b 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20 69 6e 74  e results.** int
2b25c 6f 20 74 68 65 20 6f 75 74 70 75 74 2e 20 20 49  o the output.  I
2b25d 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f 20 74 68  n addition to th
2b25e 65 20 74 77 6f 20 63 6f 72 6f 75 74 69 6e 65 73  e two coroutines
2b25f 20 28 63 61 6c 6c 65 64 20 73 65 6c 65 63 74 41   (called selectA
2b260 20 61 6e 64 0a 2a 2a 20 73 65 6c 65 63 74 42 29   and.** selectB)
2b261 20 74 68 65 72 65 20 61 72 65 20 37 20 73 75 62   there are 7 sub
2b262 72 6f 75 74 69 6e 65 73 3a 0a 2a 2a 0a 2a 2a 20  routines:.**.** 
2b263 20 20 20 6f 75 74 41 3a 20 20 20 20 4d 6f 76 65     outA:    Move
2b264 20 74 68 65 20 6f 75 74 70 75 74 20 6f 66 20 74   the output of t
2b265 68 65 20 73 65 6c 65 63 74 41 20 63 6f 72 6f 75  he selectA corou
2b266 74 69 6e 65 20 69 6e 74 6f 20 74 68 65 20 6f 75  tine into the ou
2b267 74 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  tput.**         
2b268 20 20 20 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f      of the compo
2b269 75 6e 64 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a  und query..**.**
2b26a 20 20 20 20 6f 75 74 42 3a 20 20 20 20 4d 6f 76      outB:    Mov
2b26b 65 20 74 68 65 20 6f 75 74 70 75 74 20 6f 66 20  e the output of 
2b26c 74 68 65 20 73 65 6c 65 63 74 42 20 63 6f 72 6f  the selectB coro
2b26d 75 74 69 6e 65 20 69 6e 74 6f 20 74 68 65 20 6f  utine into the o
2b26e 75 74 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20  utput.**        
2b26f 20 20 20 20 20 6f 66 20 74 68 65 20 63 6f 6d 70       of the comp
2b270 6f 75 6e 64 20 71 75 65 72 79 2e 20 20 28 4f 6e  ound query.  (On
2b271 6c 79 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72  ly generated for
2b272 20 55 4e 49 4f 4e 20 61 6e 64 0a 2a 2a 20 20 20   UNION and.**   
2b273 20 20 20 20 20 20 20 20 20 20 55 4e 49 4f 4e 20            UNION 
2b274 41 4c 4c 2e 20 20 45 58 43 45 50 54 20 61 6e 64  ALL.  EXCEPT and
2b275 20 49 4e 53 45 52 54 53 45 43 54 20 6e 65 76 65   INSERTSECT neve
2b276 72 20 6f 75 74 70 75 74 20 61 20 72 6f 77 20 74  r output a row t
2b277 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  hat.**          
2b278 20 20 20 61 70 70 65 61 72 73 20 6f 6e 6c 79 20     appears only 
2b279 69 6e 20 42 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 20  in B.).**.**    
2b27a 41 6c 74 42 3a 20 20 20 20 43 61 6c 6c 65 64 20  AltB:    Called 
2b27b 77 68 65 6e 20 74 68 65 72 65 20 69 73 20 64 61  when there is da
2b27c 74 61 20 66 72 6f 6d 20 62 6f 74 68 20 63 6f 72  ta from both cor
2b27d 6f 75 74 69 6e 65 73 20 61 6e 64 20 41 3c 42 2e  outines and A<B.
2b27e 0a 2a 2a 0a 2a 2a 20 20 20 20 41 65 71 42 3a 20  .**.**    AeqB: 
2b27f 20 20 20 43 61 6c 6c 65 64 20 77 68 65 6e 20 74     Called when t
2b280 68 65 72 65 20 69 73 20 64 61 74 61 20 66 72 6f  here is data fro
2b281 6d 20 62 6f 74 68 20 63 6f 72 6f 75 74 69 6e 65  m both coroutine
2b282 73 20 61 6e 64 20 41 3d 3d 42 2e 0a 2a 2a 0a 2a  s and A==B..**.*
2b283 2a 20 20 20 20 41 67 74 42 3a 20 20 20 20 43 61  *    AgtB:    Ca
2b284 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 72 65 20  lled when there 
2b285 69 73 20 64 61 74 61 20 66 72 6f 6d 20 62 6f 74  is data from bot
2b286 68 20 63 6f 72 6f 75 74 69 6e 65 73 20 61 6e 64  h coroutines and
2b287 20 41 3e 42 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 45   A>B..**.**    E
2b288 6f 66 41 3a 20 20 20 20 43 61 6c 6c 65 64 20 77  ofA:    Called w
2b289 68 65 6e 20 64 61 74 61 20 69 73 20 65 78 68 61  hen data is exha
2b28a 75 73 74 65 64 20 66 72 6f 6d 20 73 65 6c 65 63  usted from selec
2b28b 74 41 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 45 6f 66  tA..**.**    Eof
2b28c 42 3a 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65  B:    Called whe
2b28d 6e 20 64 61 74 61 20 69 73 20 65 78 68 61 75 73  n data is exhaus
2b28e 74 65 64 20 66 72 6f 6d 20 73 65 6c 65 63 74 42  ted from selectB
2b28f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c  ..**.** The impl
2b290 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
2b291 65 20 6c 61 74 74 65 72 20 66 69 76 65 20 73 75  e latter five su
2b292 62 72 6f 75 74 69 6e 65 73 20 64 65 70 65 6e 64  broutines depend
2b293 20 6f 6e 20 77 68 69 63 68 20 0a 2a 2a 20 3c 6f   on which .** <o
2b294 70 65 72 61 74 6f 72 3e 20 69 73 20 75 73 65 64  perator> is used
2b295 3a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  :.**.**.**      
2b296 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c         UNION ALL
2b297 20 20 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 20           UNION  
2b298 20 20 20 20 20 20 20 20 20 20 45 58 43 45 50 54            EXCEPT
2b299 20 20 20 20 20 20 20 20 20 20 49 4e 54 45 52 53            INTERS
2b29a 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ECT.**          
2b29b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d  -------------  -
2b29c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2b29d 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
2b29e 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    --------------
2b29f 2d 2d 2d 0a 2a 2a 20 20 20 41 6c 74 42 3a 20 20  ---.**   AltB:  
2b2a0 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20   outA, nextA    
2b2a1 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20    outA, nextA   
2b2a2 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20      outA, nextA 
2b2a3 20 20 20 20 20 20 20 20 6e 65 78 74 41 0a 2a 2a          nextA.**
2b2a4 0a 2a 2a 20 20 20 41 65 71 42 3a 20 20 20 6f 75  .**   AeqB:   ou
2b2a5 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20 20  tA, nextA       
2b2a6 20 20 6e 65 78 74 41 20 20 20 20 20 20 20 20 20    nextA         
2b2a7 20 20 20 20 6e 65 78 74 41 20 20 20 20 20 20 20      nextA       
2b2a8 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 0a 2a 2a    outA, nextA.**
2b2a9 0a 2a 2a 20 20 20 41 67 74 42 3a 20 20 20 6f 75  .**   AgtB:   ou
2b2aa 74 42 2c 20 6e 65 78 74 42 20 20 20 20 20 20 6f  tB, nextB      o
2b2ab 75 74 42 2c 20 6e 65 78 74 42 20 20 20 20 20 20  utB, nextB      
2b2ac 20 20 20 20 6e 65 78 74 42 20 20 20 20 20 20 20      nextB       
2b2ad 20 20 20 20 20 6e 65 78 74 42 0a 2a 2a 0a 2a 2a       nextB.**.**
2b2ae 20 20 20 45 6f 66 41 3a 20 20 20 6f 75 74 42 2c     EofA:   outB,
2b2af 20 6e 65 78 74 42 20 20 20 20 20 20 6f 75 74 42   nextB      outB
2b2b0 2c 20 6e 65 78 74 42 20 20 20 20 20 20 20 20 20  , nextB         
2b2b1 20 68 61 6c 74 20 20 20 20 20 20 20 20 20 20 20   halt           
2b2b2 20 20 68 61 6c 74 0a 2a 2a 0a 2a 2a 20 20 20 45    halt.**.**   E
2b2b3 6f 66 42 3a 20 20 20 6f 75 74 41 2c 20 6e 65 78  ofB:   outA, nex
2b2b4 74 41 20 20 20 20 20 20 6f 75 74 41 2c 20 6e 65  tA      outA, ne
2b2b5 78 74 41 20 20 20 20 20 20 20 6f 75 74 41 2c 20  xtA       outA, 
2b2b6 6e 65 78 74 41 20 20 20 20 20 20 20 20 20 68 61  nextA         ha
2b2b7 6c 74 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20  lt.**.** In the 
2b2b8 41 6c 74 42 2c 20 41 65 71 42 2c 20 61 6e 64 20  AltB, AeqB, and 
2b2b9 41 67 74 42 20 73 75 62 72 6f 75 74 69 6e 65 73  AgtB subroutines
2b2ba 2c 20 61 6e 20 45 4f 46 20 6f 6e 20 41 20 66 6f  , an EOF on A fo
2b2bb 6c 6c 6f 77 69 6e 67 20 6e 65 78 74 41 0a 2a 2a  llowing nextA.**
2b2bc 20 63 61 75 73 65 73 20 61 6e 20 69 6d 6d 65 64   causes an immed
2b2bd 69 61 74 65 20 6a 75 6d 70 20 74 6f 20 45 6f 66  iate jump to Eof
2b2be 41 20 61 6e 64 20 61 6e 20 45 4f 46 20 6f 6e 20  A and an EOF on 
2b2bf 42 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 78 74  B following next
2b2c0 42 20 63 61 75 73 65 73 0a 2a 2a 20 61 6e 20 69  B causes.** an i
2b2c1 6d 6d 65 64 69 61 74 65 20 6a 75 6d 70 20 74 6f  mmediate jump to
2b2c2 20 45 6f 66 42 2e 20 20 57 69 74 68 69 6e 20 45   EofB.  Within E
2b2c3 6f 66 41 20 61 6e 64 20 45 6f 66 42 2c 20 61 6e  ofA and EofB, an
2b2c4 64 20 45 4f 46 20 6f 6e 20 65 6e 74 72 79 20 6f  d EOF on entry o
2b2c5 72 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e  r.** following n
2b2c6 65 78 74 58 20 63 61 75 73 65 73 20 61 20 6a 75  extX causes a ju
2b2c7 6d 70 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  mp to the end of
2b2c8 20 74 68 65 20 73 65 6c 65 63 74 20 70 72 6f 63   the select proc
2b2c9 65 73 73 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 44 75  essing..**.** Du
2b2ca 70 6c 69 63 61 74 65 20 72 65 6d 6f 76 61 6c 20  plicate removal 
2b2cb 69 6e 20 74 68 65 20 55 4e 49 4f 4e 2c 20 45 58  in the UNION, EX
2b2cc 43 45 50 54 2c 20 61 6e 64 20 49 4e 54 45 52 53  CEPT, and INTERS
2b2cd 45 43 54 20 63 61 73 65 73 20 69 73 20 68 61 6e  ECT cases is han
2b2ce 64 6c 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 74  dled.** within t
2b2cf 68 65 20 6f 75 74 70 75 74 20 73 75 62 72 6f 75  he output subrou
2b2d0 74 69 6e 65 2e 20 20 54 68 65 20 72 65 67 50 72  tine.  The regPr
2b2d1 65 76 20 72 65 67 69 73 74 65 72 20 73 65 74 20  ev register set 
2b2d2 68 6f 6c 64 73 20 74 68 65 20 70 72 65 76 69 6f  holds the previo
2b2d3 75 73 6c 79 0a 2a 2a 20 6f 75 74 70 75 74 20 76  usly.** output v
2b2d4 61 6c 75 65 2e 20 20 41 20 63 6f 6d 70 61 72 69  alue.  A compari
2b2d5 73 6f 6e 20 69 73 20 6d 61 64 65 20 61 67 61 69  son is made agai
2b2d6 6e 73 74 20 74 68 69 73 20 76 61 6c 75 65 20 61  nst this value a
2b2d7 6e 64 20 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a  nd the output.**
2b2d8 20 69 73 20 73 6b 69 70 70 65 64 20 69 66 20 74   is skipped if t
2b2d9 68 65 20 6e 65 78 74 20 72 65 73 75 6c 74 73 20  he next results 
2b2da 77 6f 75 6c 64 20 62 65 20 74 68 65 20 73 61 6d  would be the sam
2b2db 65 20 61 73 20 74 68 65 20 70 72 65 76 69 6f 75  e as the previou
2b2dc 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70  s..**.** The imp
2b2dd 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 70 6c 61 6e  lementation plan
2b2de 20 69 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74   is to implement
2b2df 20 74 68 65 20 74 77 6f 20 63 6f 72 6f 75 74 69   the two corouti
2b2e0 6e 65 73 20 61 6e 64 20 73 65 76 65 6e 0a 2a 2a  nes and seven.**
2b2e1 20 73 75 62 72 6f 75 74 69 6e 65 73 20 66 69 72   subroutines fir
2b2e2 73 74 2c 20 74 68 65 6e 20 70 75 74 20 74 68 65  st, then put the
2b2e3 20 63 6f 6e 74 72 6f 6c 20 6c 6f 67 69 63 20 61   control logic a
2b2e4 74 20 74 68 65 20 62 6f 74 74 6f 6d 2e 20 20 4c  t the bottom.  L
2b2e5 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
2b2e6 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 49 6e           goto In
2b2e7 69 74 0a 2a 2a 20 20 20 20 20 63 6f 41 3a 20 63  it.**     coA: c
2b2e8 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20 6c 65 66  oroutine for lef
2b2e9 74 20 71 75 65 72 79 20 28 41 29 0a 2a 2a 20 20  t query (A).**  
2b2ea 20 20 20 63 6f 42 3a 20 63 6f 72 6f 75 74 69 6e     coB: coroutin
2b2eb 65 20 66 6f 72 20 72 69 67 68 74 20 71 75 65 72  e for right quer
2b2ec 79 20 28 42 29 0a 2a 2a 20 20 20 20 6f 75 74 41  y (B).**    outA
2b2ed 3a 20 6f 75 74 70 75 74 20 6f 6e 65 20 72 6f 77  : output one row
2b2ee 20 6f 66 20 41 0a 2a 2a 20 20 20 20 6f 75 74 42   of A.**    outB
2b2ef 3a 20 6f 75 74 70 75 74 20 6f 6e 65 20 72 6f 77  : output one row
2b2f0 20 6f 66 20 42 20 28 55 4e 49 4f 4e 20 61 6e 64   of B (UNION and
2b2f1 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 6e 6c 79 29   UNION ALL only)
2b2f2 0a 2a 2a 20 20 20 20 45 6f 66 41 3a 20 2e 2e 2e  .**    EofA: ...
2b2f3 0a 2a 2a 20 20 20 20 45 6f 66 42 3a 20 2e 2e 2e  .**    EofB: ...
2b2f4 0a 2a 2a 20 20 20 20 41 6c 74 42 3a 20 2e 2e 2e  .**    AltB: ...
2b2f5 0a 2a 2a 20 20 20 20 41 65 71 42 3a 20 2e 2e 2e  .**    AeqB: ...
2b2f6 0a 2a 2a 20 20 20 20 41 67 74 42 3a 20 2e 2e 2e  .**    AgtB: ...
2b2f7 0a 2a 2a 20 20 20 20 49 6e 69 74 3a 20 69 6e 69  .**    Init: ini
2b2f8 74 69 61 6c 69 7a 65 20 63 6f 72 6f 75 74 69 6e  tialize coroutin
2b2f9 65 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 20  e registers.**  
2b2fa 20 20 20 20 20 20 20 20 79 69 65 6c 64 20 63 6f          yield co
2b2fb 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 66  A.**          if
2b2fc 20 65 6f 66 28 41 29 20 67 6f 74 6f 20 45 6f 66   eof(A) goto Eof
2b2fd 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 79 69  A.**          yi
2b2fe 65 6c 64 20 63 6f 42 0a 2a 2a 20 20 20 20 20 20  eld coB.**      
2b2ff 20 20 20 20 69 66 20 65 6f 66 28 42 29 20 67 6f      if eof(B) go
2b300 74 6f 20 45 6f 66 42 0a 2a 2a 20 20 20 20 43 6d  to EofB.**    Cm
2b301 70 72 3a 20 43 6f 6d 70 61 72 65 20 41 2c 20 42  pr: Compare A, B
2b302 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 4a 75 6d  .**          Jum
2b303 70 20 41 6c 74 42 2c 20 41 65 71 42 2c 20 41 67  p AltB, AeqB, Ag
2b304 74 42 0a 2a 2a 20 20 20 20 20 45 6e 64 3a 20 2e  tB.**     End: .
2b305 2e 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 63 61 6c 6c  ...**.** We call
2b306 20 41 6c 74 42 2c 20 41 65 71 42 2c 20 41 67 74   AltB, AeqB, Agt
2b307 42 2c 20 45 6f 66 41 2c 20 61 6e 64 20 45 6f 66  B, EofA, and Eof
2b308 42 20 22 73 75 62 72 6f 75 74 69 6e 65 73 22 20  B "subroutines" 
2b309 62 75 74 20 74 68 65 79 20 61 72 65 20 6e 6f 74  but they are not
2b30a 0a 2a 2a 20 61 63 74 75 61 6c 6c 79 20 63 61 6c  .** actually cal
2b30b 6c 65 64 20 75 73 69 6e 67 20 47 6f 73 75 62 20  led using Gosub 
2b30c 61 6e 64 20 74 68 65 79 20 64 6f 20 6e 6f 74 20  and they do not 
2b30d 52 65 74 75 72 6e 2e 20 20 45 6f 66 41 20 61 6e  Return.  EofA an
2b30e 64 20 45 6f 66 42 20 6c 6f 6f 70 0a 2a 2a 20 75  d EofB loop.** u
2b30f 6e 74 69 6c 20 61 6c 6c 20 64 61 74 61 20 69 73  ntil all data is
2b310 20 65 78 68 61 75 73 74 65 64 20 74 68 65 6e 20   exhausted then 
2b311 6a 75 6d 70 20 74 6f 20 74 68 65 20 22 65 6e 64  jump to the "end
2b312 22 20 6c 61 62 65 2e 20 20 41 6c 74 42 2c 20 41  " labe.  AltB, A
2b313 65 71 42 2c 0a 2a 2a 20 61 6e 64 20 41 67 74 42  eqB,.** and AgtB
2b314 20 6a 75 6d 70 20 74 6f 20 65 69 74 68 65 72 20   jump to either 
2b315 4c 32 20 6f 72 20 74 6f 20 6f 6e 65 20 6f 66 20  L2 or to one of 
2b316 45 6f 66 41 20 6f 72 20 45 6f 66 42 2e 0a 2a 2f  EofA or EofB..*/
2b317 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2b318 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  OMIT_COMPOUND_SE
2b319 4c 45 43 54 0a 73 74 61 74 69 63 20 69 6e 74 20  LECT.static int 
2b31a 6d 75 6c 74 69 53 65 6c 65 63 74 4f 72 64 65 72  multiSelectOrder
2b31b 42 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  By(.  Parse *pPa
2b31c 72 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  rse,        /* P
2b31d 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2b31e 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20  /.  Select *p,  
2b31f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2b320 20 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20 53   right-most of S
2b321 45 4c 45 43 54 73 20 74 6f 20 62 65 20 63 6f 64  ELECTs to be cod
2b322 65 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65  ed */.  SelectDe
2b323 73 74 20 2a 70 44 65 73 74 20 20 20 20 20 2f 2a  st *pDest     /*
2b324 20 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68   What to do with
2b325 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 20 2a   query results *
2b326 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  /.){.  int i, j;
2b327 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2b328 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f  Loop counters */
2b329 0a 20 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f  .  Select *pPrio
2b32a 72 3b 20 20 20 20 20 20 20 2f 2a 20 41 6e 6f 74  r;       /* Anot
2b32b 68 65 72 20 53 45 4c 45 43 54 20 69 6d 6d 65 64  her SELECT immed
2b32c 69 61 74 65 6c 79 20 74 6f 20 6f 75 72 20 6c 65  iately to our le
2b32d 66 74 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b  ft */.  Vdbe *v;
2b32e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2b32f 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
2b330 6f 20 74 68 69 73 20 56 44 42 45 20 2a 2f 0a 20  o this VDBE */. 
2b331 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74   SelectDest dest
2b332 41 3b 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e  A;     /* Destin
2b333 61 74 69 6f 6e 20 66 6f 72 20 63 6f 72 6f 75 74  ation for corout
2b334 69 6e 65 20 41 20 2a 2f 0a 20 20 53 65 6c 65 63  ine A */.  Selec
2b335 74 44 65 73 74 20 64 65 73 74 42 3b 20 20 20 20  tDest destB;    
2b336 20 2f 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20   /* Destination 
2b337 66 6f 72 20 63 6f 72 6f 75 74 69 6e 65 20 42 20  for coroutine B 
2b338 2a 2f 0a 20 20 69 6e 74 20 72 65 67 41 64 64 72  */.  int regAddr
2b339 41 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64  A;         /* Ad
2b33a 64 72 65 73 73 20 72 65 67 69 73 74 65 72 20 66  dress register f
2b33b 6f 72 20 73 65 6c 65 63 74 2d 41 20 63 6f 72 6f  or select-A coro
2b33c 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72  utine */.  int r
2b33d 65 67 45 6f 66 41 3b 20 20 20 20 20 20 20 20 20  egEofA;         
2b33e 20 2f 2a 20 46 6c 61 67 20 74 6f 20 69 6e 64 69   /* Flag to indi
2b33f 63 61 74 65 20 77 68 65 6e 20 73 65 6c 65 63 74  cate when select
2b340 2d 41 20 69 73 20 63 6f 6d 70 6c 65 74 65 20 2a  -A is complete *
2b341 2f 0a 20 20 69 6e 74 20 72 65 67 41 64 64 72 42  /.  int regAddrB
2b342 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64  ;         /* Add
2b343 72 65 73 73 20 72 65 67 69 73 74 65 72 20 66 6f  ress register fo
2b344 72 20 73 65 6c 65 63 74 2d 42 20 63 6f 72 6f 75  r select-B corou
2b345 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65  tine */.  int re
2b346 67 45 6f 66 42 3b 20 20 20 20 20 20 20 20 20 20  gEofB;          
2b347 2f 2a 20 46 6c 61 67 20 74 6f 20 69 6e 64 69 63  /* Flag to indic
2b348 61 74 65 20 77 68 65 6e 20 73 65 6c 65 63 74 2d  ate when select-
2b349 42 20 69 73 20 63 6f 6d 70 6c 65 74 65 20 2a 2f  B is complete */
2b34a 0a 20 20 69 6e 74 20 61 64 64 72 53 65 6c 65 63  .  int addrSelec
2b34b 74 41 3b 20 20 20 20 20 20 2f 2a 20 41 64 64 72  tA;      /* Addr
2b34c 65 73 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63  ess of the selec
2b34d 74 2d 41 20 63 6f 72 6f 75 74 69 6e 65 20 2a 2f  t-A coroutine */
2b34e 0a 20 20 69 6e 74 20 61 64 64 72 53 65 6c 65 63  .  int addrSelec
2b34f 74 42 3b 20 20 20 20 20 20 2f 2a 20 41 64 64 72  tB;      /* Addr
2b350 65 73 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63  ess of the selec
2b351 74 2d 42 20 63 6f 72 6f 75 74 69 6e 65 20 2a 2f  t-B coroutine */
2b352 0a 20 20 69 6e 74 20 72 65 67 4f 75 74 41 3b 20  .  int regOutA; 
2b353 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
2b354 65 73 73 20 72 65 67 69 73 74 65 72 20 66 6f 72  ess register for
2b355 20 74 68 65 20 6f 75 74 70 75 74 2d 41 20 73 75   the output-A su
2b356 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e  broutine */.  in
2b357 74 20 72 65 67 4f 75 74 42 3b 20 20 20 20 20 20  t regOutB;      
2b358 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 72      /* Address r
2b359 65 67 69 73 74 65 72 20 66 6f 72 20 74 68 65 20  egister for the 
2b35a 6f 75 74 70 75 74 2d 42 20 73 75 62 72 6f 75 74  output-B subrout
2b35b 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ine */.  int add
2b35c 72 4f 75 74 41 3b 20 20 20 20 20 20 20 20 20 2f  rOutA;         /
2b35d 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65  * Address of the
2b35e 20 6f 75 74 70 75 74 2d 41 20 73 75 62 72 6f 75   output-A subrou
2b35f 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64  tine */.  int ad
2b360 64 72 4f 75 74 42 20 3d 20 30 3b 20 20 20 20 20  drOutB = 0;     
2b361 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68  /* Address of th
2b362 65 20 6f 75 74 70 75 74 2d 42 20 73 75 62 72 6f  e output-B subro
2b363 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61  utine */.  int a
2b364 64 64 72 45 6f 66 41 3b 20 20 20 20 20 20 20 20  ddrEofA;        
2b365 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74   /* Address of t
2b366 68 65 20 73 65 6c 65 63 74 2d 41 2d 65 78 68 61  he select-A-exha
2b367 75 73 74 65 64 20 73 75 62 72 6f 75 74 69 6e 65  usted subroutine
2b368 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 45 6f   */.  int addrEo
2b369 66 42 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41  fB;         /* A
2b36a 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 73 65  ddress of the se
2b36b 6c 65 63 74 2d 42 2d 65 78 68 61 75 73 74 65 64  lect-B-exhausted
2b36c 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20   subroutine */. 
2b36d 20 69 6e 74 20 61 64 64 72 41 6c 74 42 3b 20 20   int addrAltB;  
2b36e 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
2b36f 73 20 6f 66 20 74 68 65 20 41 3c 42 20 73 75 62  s of the A<B sub
2b370 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74  routine */.  int
2b371 20 61 64 64 72 41 65 71 42 3b 20 20 20 20 20 20   addrAeqB;      
2b372 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66     /* Address of
2b373 20 74 68 65 20 41 3d 3d 42 20 73 75 62 72 6f 75   the A==B subrou
2b374 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64  tine */.  int ad
2b375 64 72 41 67 74 42 3b 20 20 20 20 20 20 20 20 20  drAgtB;         
2b376 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68  /* Address of th
2b377 65 20 41 3e 42 20 73 75 62 72 6f 75 74 69 6e 65  e A>B subroutine
2b378 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4c 69 6d   */.  int regLim
2b379 69 74 41 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c  itA;        /* L
2b37a 69 6d 69 74 20 72 65 67 69 73 74 65 72 20 66 6f  imit register fo
2b37b 72 20 73 65 6c 65 63 74 2d 41 20 2a 2f 0a 20 20  r select-A */.  
2b37c 69 6e 74 20 72 65 67 4c 69 6d 69 74 42 3b 20 20  int regLimitB;  
2b37d 20 20 20 20 20 20 2f 2a 20 4c 69 6d 69 74 20 72        /* Limit r
2b37e 65 67 69 73 74 65 72 20 66 6f 72 20 73 65 6c 65  egister for sele
2b37f 63 74 2d 41 20 2a 2f 0a 20 20 69 6e 74 20 72 65  ct-A */.  int re
2b380 67 50 72 65 76 3b 20 20 20 20 20 20 20 20 20 20  gPrev;          
2b381 2f 2a 20 41 20 72 61 6e 67 65 20 6f 66 20 72 65  /* A range of re
2b382 67 69 73 74 65 72 73 20 74 6f 20 68 6f 6c 64 20  gisters to hold 
2b383 70 72 65 76 69 6f 75 73 20 6f 75 74 70 75 74 20  previous output 
2b384 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 64 4c 69  */.  int savedLi
2b385 6d 69 74 3b 20 20 20 20 20 20 20 2f 2a 20 53 61  mit;       /* Sa
2b386 76 65 64 20 76 61 6c 75 65 20 6f 66 20 70 2d 3e  ved value of p->
2b387 69 4c 69 6d 69 74 20 2a 2f 0a 20 20 69 6e 74 20  iLimit */.  int 
2b388 73 61 76 65 64 4f 66 66 73 65 74 3b 20 20 20 20  savedOffset;    
2b389 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65    /* Saved value
2b38a 20 6f 66 20 70 2d 3e 69 4f 66 66 73 65 74 20 2a   of p->iOffset *
2b38b 2f 0a 20 20 69 6e 74 20 6c 61 62 65 6c 43 6d 70  /.  int labelCmp
2b38c 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62  r;        /* Lab
2b38d 65 6c 20 66 6f 72 20 74 68 65 20 73 74 61 72 74  el for the start
2b38e 20 6f 66 20 74 68 65 20 6d 65 72 67 65 20 61 6c   of the merge al
2b38f 67 6f 72 69 74 68 6d 20 2a 2f 0a 20 20 69 6e 74  gorithm */.  int
2b390 20 6c 61 62 65 6c 45 6e 64 3b 20 20 20 20 20 20   labelEnd;      
2b391 20 20 20 2f 2a 20 4c 61 62 65 6c 20 66 6f 72 20     /* Label for 
2b392 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6f  the end of the o
2b393 76 65 72 61 6c 6c 20 53 45 4c 45 43 54 20 73 74  verall SELECT st
2b394 6d 74 20 2a 2f 0a 20 20 69 6e 74 20 6a 31 3b 20  mt */.  int j1; 
2b395 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2b396 20 4a 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f   Jump instructio
2b397 6e 73 20 74 68 61 74 20 67 65 74 20 72 65 74 61  ns that get reta
2b398 72 67 65 74 74 65 64 20 2a 2f 0a 20 20 69 6e 74  rgetted */.  int
2b399 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20   op;            
2b39a 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f     /* One of TK_
2b39b 41 4c 4c 2c 20 54 4b 5f 55 4e 49 4f 4e 2c 20 54  ALL, TK_UNION, T
2b39c 4b 5f 45 58 43 45 50 54 2c 20 54 4b 5f 49 4e 54  K_EXCEPT, TK_INT
2b39d 45 52 53 45 43 54 20 2a 2f 0a 20 20 4b 65 79 49  ERSECT */.  KeyI
2b39e 6e 66 6f 20 2a 70 4b 65 79 44 75 70 20 3d 20 30  nfo *pKeyDup = 0
2b39f 3b 20 2f 2a 20 43 6f 6d 70 61 72 69 73 6f 6e 20  ; /* Comparison 
2b3a0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20  information for 
2b3a1 64 75 70 6c 69 63 61 74 65 20 72 65 6d 6f 76 61  duplicate remova
2b3a2 6c 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a  l */.  KeyInfo *
2b3a3 70 4b 65 79 4d 65 72 67 65 3b 20 20 20 2f 2a 20  pKeyMerge;   /* 
2b3a4 43 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 66 6f 72  Comparison infor
2b3a5 6d 61 74 69 6f 6e 20 66 6f 72 20 6d 65 72 67 69  mation for mergi
2b3a6 6e 67 20 72 6f 77 73 20 2a 2f 0a 20 20 73 71 6c  ng rows */.  sql
2b3a7 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
2b3a8 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63     /* Database c
2b3a9 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 45  onnection */.  E
2b3aa 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
2b3ab 79 3b 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45  y;   /* The ORDE
2b3ac 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20  R BY clause */. 
2b3ad 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b 20 20   int nOrderBy;  
2b3ae 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
2b3af 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 74 68 65   of terms in the
2b3b0 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2b3b1 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 50 65 72 6d   */.  int *aPerm
2b3b2 75 74 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d  ute;        /* M
2b3b3 61 70 70 69 6e 67 20 66 72 6f 6d 20 4f 52 44 45  apping from ORDE
2b3b4 52 20 42 59 20 74 65 72 6d 73 20 74 6f 20 72 65  R BY terms to re
2b3b5 73 75 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73  sult set columns
2b3b6 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70   */..  assert( p
2b3b7 2d 3e 70 4f 72 64 65 72 42 79 21 3d 30 20 29 3b  ->pOrderBy!=0 );
2b3b8 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79 44  .  assert( pKeyD
2b3b9 75 70 3d 3d 30 20 29 3b 20 2f 2a 20 22 4d 61 6e  up==0 ); /* "Man
2b3ba 61 67 65 64 22 20 63 6f 64 65 20 6e 65 65 64 73  aged" code needs
2b3bb 20 74 68 69 73 2e 20 20 54 69 63 6b 65 74 20 23   this.  Ticket #
2b3bc 33 33 38 32 2e 20 2a 2f 0a 20 20 64 62 20 3d 20  3382. */.  db = 
2b3bd 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 76 20  pParse->db;.  v 
2b3be 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
2b3bf 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d 30 20  .  assert( v!=0 
2b3c0 29 3b 20 20 20 20 20 20 20 2f 2a 20 41 6c 72 65  );       /* Alre
2b3c1 61 64 79 20 74 68 72 6f 77 6e 20 74 68 65 20 65  ady thrown the e
2b3c2 72 72 6f 72 20 69 66 20 56 44 42 45 20 61 6c 6c  rror if VDBE all
2b3c3 6f 63 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20 6c  oc failed */.  l
2b3c4 61 62 65 6c 45 6e 64 20 3d 20 73 71 6c 69 74 65  abelEnd = sqlite
2b3c5 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76  3VdbeMakeLabel(v
2b3c6 29 3b 0a 20 20 6c 61 62 65 6c 43 6d 70 72 20 3d  );.  labelCmpr =
2b3c7 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
2b3c8 4c 61 62 65 6c 28 76 29 3b 0a 0a 0a 20 20 2f 2a  Label(v);...  /*
2b3c9 20 50 61 74 63 68 20 75 70 20 74 68 65 20 4f 52   Patch up the OR
2b3ca 44 45 52 20 42 59 20 63 6c 61 75 73 65 0a 20 20  DER BY clause.  
2b3cb 2a 2f 0a 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b  */.  op = p->op;
2b3cc 20 20 0a 20 20 70 50 72 69 6f 72 20 3d 20 70 2d    .  pPrior = p-
2b3cd 3e 70 50 72 69 6f 72 3b 0a 20 20 61 73 73 65 72  >pPrior;.  asser
2b3ce 74 28 20 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65  t( pPrior->pOrde
2b3cf 72 42 79 3d 3d 30 20 29 3b 0a 20 20 70 4f 72 64  rBy==0 );.  pOrd
2b3d0 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72  erBy = p->pOrder
2b3d1 42 79 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f  By;.  assert( pO
2b3d2 72 64 65 72 42 79 20 29 3b 0a 20 20 6e 4f 72 64  rderBy );.  nOrd
2b3d3 65 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 2d  erBy = pOrderBy-
2b3d4 3e 6e 45 78 70 72 3b 0a 0a 20 20 2f 2a 20 46 6f  >nExpr;..  /* Fo
2b3d5 72 20 6f 70 65 72 61 74 6f 72 73 20 6f 74 68 65  r operators othe
2b3d6 72 20 74 68 61 6e 20 55 4e 49 4f 4e 20 41 4c 4c  r than UNION ALL
2b3d7 20 77 65 20 68 61 76 65 20 74 6f 20 6d 61 6b 65   we have to make
2b3d8 20 73 75 72 65 20 74 68 61 74 0a 20 20 2a 2a 20   sure that.  ** 
2b3d9 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
2b3da 75 73 65 20 63 6f 76 65 72 73 20 65 76 65 72 79  use covers every
2b3db 20 74 65 72 6d 20 6f 66 20 74 68 65 20 72 65 73   term of the res
2b3dc 75 6c 74 20 73 65 74 2e 20 20 41 64 64 0a 20 20  ult set.  Add.  
2b3dd 2a 2a 20 74 65 72 6d 73 20 74 6f 20 74 68 65 20  ** terms to the 
2b3de 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2b3df 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 20 20  as necessary..  
2b3e0 2a 2f 0a 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f  */.  if( op!=TK_
2b3e1 41 4c 4c 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  ALL ){.    for(i
2b3e2 3d 31 3b 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  =1; db->mallocFa
2b3e3 69 6c 65 64 3d 3d 30 20 26 26 20 69 3c 3d 70 2d  iled==0 && i<=p-
2b3e4 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  >pEList->nExpr; 
2b3e5 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74 72 75  i++){.      stru
2b3e6 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
2b3e7 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 66   *pItem;.      f
2b3e8 6f 72 28 6a 3d 30 2c 20 70 49 74 65 6d 3d 70 4f  or(j=0, pItem=pO
2b3e9 72 64 65 72 42 79 2d 3e 61 3b 20 6a 3c 6e 4f 72  rderBy->a; j<nOr
2b3ea 64 65 72 42 79 3b 20 6a 2b 2b 2c 20 70 49 74 65  derBy; j++, pIte
2b3eb 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61 73  m++){.        as
2b3ec 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 69 43 6f  sert( pItem->iCo
2b3ed 6c 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 69  l>0 );.        i
2b3ee 66 28 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 3d 3d  f( pItem->iCol==
2b3ef 69 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  i ) break;.     
2b3f0 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6a 3d 3d   }.      if( j==
2b3f1 6e 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  nOrderBy ){.    
2b3f2 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 20 3d      Expr *pNew =
2b3f3 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c   sqlite3Expr(db,
2b3f4 20 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30 29 3b   TK_INTEGER, 0);
2b3f5 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4e 65  .        if( pNe
2b3f6 77 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51  w==0 ) return SQ
2b3f7 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
2b3f8 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20      pNew->flags 
2b3f9 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a  |= EP_IntValue;.
2b3fa 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 75 2e          pNew->u.
2b3fb 69 56 61 6c 75 65 20 3d 20 69 3b 0a 20 20 20 20  iValue = i;.    
2b3fc 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d 20 73      pOrderBy = s
2b3fd 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
2b3fe 70 65 6e 64 28 70 50 61 72 73 65 2c 20 70 4f 72  pend(pParse, pOr
2b3ff 64 65 72 42 79 2c 20 70 4e 65 77 29 3b 0a 20 20  derBy, pNew);.  
2b400 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e        pOrderBy->
2b401 61 5b 6e 4f 72 64 65 72 42 79 2b 2b 5d 2e 69 43  a[nOrderBy++].iC
2b402 6f 6c 20 3d 20 28 75 31 36 29 69 3b 0a 20 20 20  ol = (u16)i;.   
2b403 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
2b404 20 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65    /* Compute the
2b405 20 63 6f 6d 70 61 72 69 73 6f 6e 20 70 65 72 6d   comparison perm
2b406 75 74 61 74 69 6f 6e 20 61 6e 64 20 6b 65 79 69  utation and keyi
2b407 6e 66 6f 20 74 68 61 74 20 69 73 20 75 73 65 64  nfo that is used
2b408 20 77 69 74 68 0a 20 20 2a 2a 20 74 68 65 20 70   with.  ** the p
2b409 65 72 6d 75 74 61 74 69 6f 6e 20 75 73 65 64 20  ermutation used 
2b40a 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20  to determine if 
2b40b 74 68 65 20 6e 65 78 74 0a 20 20 2a 2a 20 72 6f  the next.  ** ro
2b40c 77 20 6f 66 20 72 65 73 75 6c 74 73 20 63 6f 6d  w of results com
2b40d 65 73 20 66 72 6f 6d 20 73 65 6c 65 63 74 41 20  es from selectA 
2b40e 6f 72 20 73 65 6c 65 63 74 42 2e 20 20 41 6c 73  or selectB.  Als
2b40f 6f 20 61 64 64 20 65 78 70 6c 69 63 69 74 0a 20  o add explicit. 
2b410 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 73 20 74   ** collations t
2b411 6f 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  o the ORDER BY c
2b412 6c 61 75 73 65 20 74 65 72 6d 73 20 73 6f 20 74  lause terms so t
2b413 68 61 74 20 77 68 65 6e 20 74 68 65 20 73 75 62  hat when the sub
2b414 71 75 65 72 69 65 73 0a 20 20 2a 2a 20 74 6f 20  queries.  ** to 
2b415 74 68 65 20 72 69 67 68 74 20 61 6e 64 20 74 68  the right and th
2b416 65 20 6c 65 66 74 20 61 72 65 20 65 76 61 6c 75  e left are evalu
2b417 61 74 65 64 2c 20 74 68 65 79 20 75 73 65 20 74  ated, they use t
2b418 68 65 20 63 6f 72 72 65 63 74 0a 20 20 2a 2a 20  he correct.  ** 
2b419 63 6f 6c 6c 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a  collation..  */.
2b41a 20 20 61 50 65 72 6d 75 74 65 20 3d 20 73 71 6c    aPermute = sql
2b41b 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
2b41c 64 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a  db, sizeof(int)*
2b41d 6e 4f 72 64 65 72 42 79 29 3b 0a 20 20 69 66 28  nOrderBy);.  if(
2b41e 20 61 50 65 72 6d 75 74 65 20 29 7b 0a 20 20 20   aPermute ){.   
2b41f 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
2b420 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20  _item *pItem;.  
2b421 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d    for(i=0, pItem
2b422 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c  =pOrderBy->a; i<
2b423 6e 4f 72 64 65 72 42 79 3b 20 69 2b 2b 2c 20 70  nOrderBy; i++, p
2b424 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 61  Item++){.      a
2b425 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 69 43  ssert( pItem->iC
2b426 6f 6c 3e 30 20 20 26 26 20 70 49 74 65 6d 2d 3e  ol>0  && pItem->
2b427 69 43 6f 6c 3c 3d 70 2d 3e 70 45 4c 69 73 74 2d  iCol<=p->pEList-
2b428 3e 6e 45 78 70 72 20 29 3b 0a 20 20 20 20 20 20  >nExpr );.      
2b429 61 50 65 72 6d 75 74 65 5b 69 5d 20 3d 20 70 49  aPermute[i] = pI
2b42a 74 65 6d 2d 3e 69 43 6f 6c 20 2d 20 31 3b 0a 20  tem->iCol - 1;. 
2b42b 20 20 20 7d 0a 20 20 20 20 70 4b 65 79 4d 65 72     }.    pKeyMer
2b42c 67 65 20 3d 0a 20 20 20 20 20 20 73 71 6c 69 74  ge =.      sqlit
2b42d 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
2b42e 2c 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79 4d 65  , sizeof(*pKeyMe
2b42f 72 67 65 29 2b 6e 4f 72 64 65 72 42 79 2a 28 73  rge)+nOrderBy*(s
2b430 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 2b  izeof(CollSeq*)+
2b431 31 29 29 3b 0a 20 20 20 20 69 66 28 20 70 4b 65  1));.    if( pKe
2b432 79 4d 65 72 67 65 20 29 7b 0a 20 20 20 20 20 20  yMerge ){.      
2b433 70 4b 65 79 4d 65 72 67 65 2d 3e 61 53 6f 72 74  pKeyMerge->aSort
2b434 4f 72 64 65 72 20 3d 20 28 75 38 2a 29 26 70 4b  Order = (u8*)&pK
2b435 65 79 4d 65 72 67 65 2d 3e 61 43 6f 6c 6c 5b 6e  eyMerge->aColl[n
2b436 4f 72 64 65 72 42 79 5d 3b 0a 20 20 20 20 20 20  OrderBy];.      
2b437 70 4b 65 79 4d 65 72 67 65 2d 3e 6e 46 69 65 6c  pKeyMerge->nFiel
2b438 64 20 3d 20 28 75 31 36 29 6e 4f 72 64 65 72 42  d = (u16)nOrderB
2b439 79 3b 0a 20 20 20 20 20 20 70 4b 65 79 4d 65 72  y;.      pKeyMer
2b43a 67 65 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62  ge->enc = ENC(db
2b43b 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  );.      for(i=0
2b43c 3b 20 69 3c 6e 4f 72 64 65 72 42 79 3b 20 69 2b  ; i<nOrderBy; i+
2b43d 2b 29 7b 0a 20 20 20 20 20 20 20 20 43 6f 6c 6c  +){.        Coll
2b43e 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20  Seq *pColl;.    
2b43f 20 20 20 20 45 78 70 72 20 2a 70 54 65 72 6d 20      Expr *pTerm 
2b440 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69 5d  = pOrderBy->a[i]
2b441 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20  .pExpr;.        
2b442 69 66 28 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73  if( pTerm->flags
2b443 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65   & EP_ExpCollate
2b444 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43   ){.          pC
2b445 6f 6c 6c 20 3d 20 70 54 65 72 6d 2d 3e 70 43 6f  oll = pTerm->pCo
2b446 6c 6c 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ll;.        }els
2b447 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43 6f  e{.          pCo
2b448 6c 6c 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63 74  ll = multiSelect
2b449 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
2b44a 70 2c 20 61 50 65 72 6d 75 74 65 5b 69 5d 29 3b  p, aPermute[i]);
2b44b 0a 20 20 20 20 20 20 20 20 20 20 70 54 65 72 6d  .          pTerm
2b44c 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78  ->flags |= EP_Ex
2b44d 70 43 6f 6c 6c 61 74 65 3b 0a 20 20 20 20 20 20  pCollate;.      
2b44e 20 20 20 20 70 54 65 72 6d 2d 3e 70 43 6f 6c 6c      pTerm->pColl
2b44f 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20   = pColl;.      
2b450 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4b 65 79    }.        pKey
2b451 4d 65 72 67 65 2d 3e 61 43 6f 6c 6c 5b 69 5d 20  Merge->aColl[i] 
2b452 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20  = pColl;.       
2b453 20 70 4b 65 79 4d 65 72 67 65 2d 3e 61 53 6f 72   pKeyMerge->aSor
2b454 74 4f 72 64 65 72 5b 69 5d 20 3d 20 70 4f 72 64  tOrder[i] = pOrd
2b455 65 72 42 79 2d 3e 61 5b 69 5d 2e 73 6f 72 74 4f  erBy->a[i].sortO
2b456 72 64 65 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rder;.      }.  
2b457 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
2b458 20 70 4b 65 79 4d 65 72 67 65 20 3d 20 30 3b 0a   pKeyMerge = 0;.
2b459 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 74 74 61    }..  /* Reatta
2b45a 63 68 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  ch the ORDER BY 
2b45b 63 6c 61 75 73 65 20 74 6f 20 74 68 65 20 71 75  clause to the qu
2b45c 65 72 79 2e 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70  ery..  */.  p->p
2b45d 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72  OrderBy = pOrder
2b45e 42 79 3b 0a 20 20 70 50 72 69 6f 72 2d 3e 70 4f  By;.  pPrior->pO
2b45f 72 64 65 72 42 79 20 3d 20 73 71 6c 69 74 65 33  rderBy = sqlite3
2b460 45 78 70 72 4c 69 73 74 44 75 70 28 70 50 61 72  ExprListDup(pPar
2b461 73 65 2d 3e 64 62 2c 20 70 4f 72 64 65 72 42 79  se->db, pOrderBy
2b462 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  , 0);..  /* Allo
2b463 63 61 74 65 20 61 20 72 61 6e 67 65 20 6f 66 20  cate a range of 
2b464 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74  temporary regist
2b465 65 72 73 20 61 6e 64 20 74 68 65 20 4b 65 79 49  ers and the KeyI
2b466 6e 66 6f 20 6e 65 65 64 65 64 0a 20 20 2a 2a 20  nfo needed.  ** 
2b467 66 6f 72 20 74 68 65 20 6c 6f 67 69 63 20 74 68  for the logic th
2b468 61 74 20 72 65 6d 6f 76 65 73 20 64 75 70 6c 69  at removes dupli
2b469 63 61 74 65 20 72 65 73 75 6c 74 20 72 6f 77 73  cate result rows
2b46a 20 77 68 65 6e 20 74 68 65 0a 20 20 2a 2a 20 6f   when the.  ** o
2b46b 70 65 72 61 74 6f 72 20 69 73 20 55 4e 49 4f 4e  perator is UNION
2b46c 2c 20 45 58 43 45 50 54 2c 20 6f 72 20 49 4e 54  , EXCEPT, or INT
2b46d 45 52 53 45 43 54 20 28 62 75 74 20 6e 6f 74 20  ERSECT (but not 
2b46e 55 4e 49 4f 4e 20 41 4c 4c 29 2e 0a 20 20 2a 2f  UNION ALL)..  */
2b46f 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 41 4c  .  if( op==TK_AL
2b470 4c 20 29 7b 0a 20 20 20 20 72 65 67 50 72 65 76  L ){.    regPrev
2b471 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
2b472 20 20 20 69 6e 74 20 6e 45 78 70 72 20 3d 20 70     int nExpr = p
2b473 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b  ->pEList->nExpr;
2b474 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 4f 72  .    assert( nOr
2b475 64 65 72 42 79 3e 3d 6e 45 78 70 72 20 7c 7c 20  derBy>=nExpr || 
2b476 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2b477 20 29 3b 0a 20 20 20 20 72 65 67 50 72 65 76 20   );.    regPrev 
2b478 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
2b479 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 45  Range(pParse, nE
2b47a 78 70 72 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69  xpr+1);.    sqli
2b47b 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b47c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20   OP_Integer, 0, 
2b47d 72 65 67 50 72 65 76 29 3b 0a 20 20 20 20 70 4b  regPrev);.    pK
2b47e 65 79 44 75 70 20 3d 20 73 71 6c 69 74 65 33 44  eyDup = sqlite3D
2b47f 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 0a  bMallocZero(db,.
2b480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b481 20 20 73 69 7a 65 6f 66 28 2a 70 4b 65 79 44 75    sizeof(*pKeyDu
2b482 70 29 20 2b 20 6e 45 78 70 72 2a 28 73 69 7a 65  p) + nExpr*(size
2b483 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 2b 31 29 20  of(CollSeq*)+1) 
2b484 29 3b 0a 20 20 20 20 69 66 28 20 70 4b 65 79 44  );.    if( pKeyD
2b485 75 70 20 29 7b 0a 20 20 20 20 20 20 70 4b 65 79  up ){.      pKey
2b486 44 75 70 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20  Dup->aSortOrder 
2b487 3d 20 28 75 38 2a 29 26 70 4b 65 79 44 75 70 2d  = (u8*)&pKeyDup-
2b488 3e 61 43 6f 6c 6c 5b 6e 45 78 70 72 5d 3b 0a 20  >aColl[nExpr];. 
2b489 20 20 20 20 20 70 4b 65 79 44 75 70 2d 3e 6e 46       pKeyDup->nF
2b48a 69 65 6c 64 20 3d 20 28 75 31 36 29 6e 45 78 70  ield = (u16)nExp
2b48b 72 3b 0a 20 20 20 20 20 20 70 4b 65 79 44 75 70  r;.      pKeyDup
2b48c 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b  ->enc = ENC(db);
2b48d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
2b48e 69 3c 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20  i<nExpr; i++){. 
2b48f 20 20 20 20 20 20 20 70 4b 65 79 44 75 70 2d 3e         pKeyDup->
2b490 61 43 6f 6c 6c 5b 69 5d 20 3d 20 6d 75 6c 74 69  aColl[i] = multi
2b491 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70 50  SelectCollSeq(pP
2b492 61 72 73 65 2c 20 70 2c 20 69 29 3b 0a 20 20 20  arse, p, i);.   
2b493 20 20 20 20 20 70 4b 65 79 44 75 70 2d 3e 61 53       pKeyDup->aS
2b494 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 30 3b  ortOrder[i] = 0;
2b495 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2b496 20 7d 0a 20 0a 20 20 2f 2a 20 53 65 70 61 72 61   }. .  /* Separa
2b497 74 65 20 74 68 65 20 6c 65 66 74 20 61 6e 64 20  te the left and 
2b498 74 68 65 20 72 69 67 68 74 20 71 75 65 72 79 20  the right query 
2b499 66 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74 68 65 72  from one another
2b49a 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 50 72 69 6f  .  */.  p->pPrio
2b49b 72 20 3d 20 30 3b 0a 20 20 70 50 72 69 6f 72 2d  r = 0;.  pPrior-
2b49c 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 30 3b  >pRightmost = 0;
2b49d 0a 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  .  sqlite3Resolv
2b49e 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 70 50  eOrderGroupBy(pP
2b49f 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 4f 72 64  arse, p, p->pOrd
2b4a0 65 72 42 79 2c 20 22 4f 52 44 45 52 22 29 3b 0a  erBy, "ORDER");.
2b4a1 20 20 69 66 28 20 70 50 72 69 6f 72 2d 3e 70 50    if( pPrior->pP
2b4a2 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20 73  rior==0 ){.    s
2b4a3 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64  qlite3ResolveOrd
2b4a4 65 72 47 72 6f 75 70 42 79 28 70 50 61 72 73 65  erGroupBy(pParse
2b4a5 2c 20 70 50 72 69 6f 72 2c 20 70 50 72 69 6f 72  , pPrior, pPrior
2b4a6 2d 3e 70 4f 72 64 65 72 42 79 2c 20 22 4f 52 44  ->pOrderBy, "ORD
2b4a7 45 52 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ER");.  }..  /* 
2b4a8 43 6f 6d 70 75 74 65 20 74 68 65 20 6c 69 6d 69  Compute the limi
2b4a9 74 20 72 65 67 69 73 74 65 72 73 20 2a 2f 0a 20  t registers */. 
2b4aa 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65 67   computeLimitReg
2b4ab 69 73 74 65 72 73 28 70 50 61 72 73 65 2c 20 70  isters(pParse, p
2b4ac 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 69  , labelEnd);.  i
2b4ad 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20 26 26 20  f( p->iLimit && 
2b4ae 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20  op==TK_ALL ){.  
2b4af 20 20 72 65 67 4c 69 6d 69 74 41 20 3d 20 2b 2b    regLimitA = ++
2b4b0 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2b4b1 20 20 72 65 67 4c 69 6d 69 74 42 20 3d 20 2b 2b    regLimitB = ++
2b4b2 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2b4b3 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b4b4 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20  Op2(v, OP_Copy, 
2b4b5 70 2d 3e 69 4f 66 66 73 65 74 20 3f 20 70 2d 3e  p->iOffset ? p->
2b4b6 69 4f 66 66 73 65 74 2b 31 20 3a 20 70 2d 3e 69  iOffset+1 : p->i
2b4b7 4c 69 6d 69 74 2c 0a 20 20 20 20 20 20 20 20 20  Limit,.         
2b4b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b4b9 20 20 20 20 20 20 20 20 20 72 65 67 4c 69 6d 69           regLimi
2b4ba 74 41 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  tA);.    sqlite3
2b4bb 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b4bc 5f 43 6f 70 79 2c 20 72 65 67 4c 69 6d 69 74 41  _Copy, regLimitA
2b4bd 2c 20 72 65 67 4c 69 6d 69 74 42 29 3b 0a 20 20  , regLimitB);.  
2b4be 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 67 4c 69  }else{.    regLi
2b4bf 6d 69 74 41 20 3d 20 72 65 67 4c 69 6d 69 74 42  mitA = regLimitB
2b4c0 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69   = 0;.  }.  sqli
2b4c1 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
2b4c2 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20  , p->pLimit);.  
2b4c3 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20  p->pLimit = 0;. 
2b4c4 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2b4c5 74 65 28 64 62 2c 20 70 2d 3e 70 4f 66 66 73 65  te(db, p->pOffse
2b4c6 74 29 3b 0a 20 20 70 2d 3e 70 4f 66 66 73 65 74  t);.  p->pOffset
2b4c7 20 3d 20 30 3b 0a 0a 20 20 72 65 67 41 64 64 72   = 0;..  regAddr
2b4c8 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  A = ++pParse->nM
2b4c9 65 6d 3b 0a 20 20 72 65 67 45 6f 66 41 20 3d 20  em;.  regEofA = 
2b4ca 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
2b4cb 20 20 72 65 67 41 64 64 72 42 20 3d 20 2b 2b 70    regAddrB = ++p
2b4cc 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72  Parse->nMem;.  r
2b4cd 65 67 45 6f 66 42 20 3d 20 2b 2b 70 50 61 72 73  egEofB = ++pPars
2b4ce 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 4f 75  e->nMem;.  regOu
2b4cf 74 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  tA = ++pParse->n
2b4d0 4d 65 6d 3b 0a 20 20 72 65 67 4f 75 74 42 20 3d  Mem;.  regOutB =
2b4d1 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2b4d2 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
2b4d3 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 41 2c  DestInit(&destA,
2b4d4 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 2c 20   SRT_Coroutine, 
2b4d5 72 65 67 41 64 64 72 41 29 3b 0a 20 20 73 71 6c  regAddrA);.  sql
2b4d6 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e  ite3SelectDestIn
2b4d7 69 74 28 26 64 65 73 74 42 2c 20 53 52 54 5f 43  it(&destB, SRT_C
2b4d8 6f 72 6f 75 74 69 6e 65 2c 20 72 65 67 41 64 64  oroutine, regAdd
2b4d9 72 42 29 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20  rB);..  /* Jump 
2b4da 70 61 73 74 20 74 68 65 20 76 61 72 69 6f 75 73  past the various
2b4db 20 73 75 62 72 6f 75 74 69 6e 65 73 20 61 6e 64   subroutines and
2b4dc 20 63 6f 72 6f 75 74 69 6e 65 73 20 74 6f 20 74   coroutines to t
2b4dd 68 65 20 6d 61 69 6e 0a 20 20 2a 2a 20 6d 65 72  he main.  ** mer
2b4de 67 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a 20 20 6a  ge loop.  */.  j
2b4df 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
2b4e0 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp0(v, OP_Goto
2b4e1 29 3b 0a 20 20 61 64 64 72 53 65 6c 65 63 74 41  );.  addrSelectA
2b4e2 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
2b4e3 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 0a 0a  rrentAddr(v);...
2b4e4 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20    /* Generate a 
2b4e5 63 6f 72 6f 75 74 69 6e 65 20 74 6f 20 65 76 61  coroutine to eva
2b4e6 6c 75 61 74 65 20 74 68 65 20 53 45 4c 45 43 54  luate the SELECT
2b4e7 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 74 68   statement to th
2b4e8 65 0a 20 20 2a 2a 20 6c 65 66 74 20 6f 66 20 74  e.  ** left of t
2b4e9 68 65 20 63 6f 6d 70 6f 75 6e 64 20 6f 70 65 72  he compound oper
2b4ea 61 74 6f 72 20 2d 20 74 68 65 20 22 41 22 20 73  ator - the "A" s
2b4eb 65 6c 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 56 64  elect..  */.  Vd
2b4ec 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76  beNoopComment((v
2b4ed 2c 20 22 42 65 67 69 6e 20 63 6f 72 6f 75 74 69  , "Begin corouti
2b4ee 6e 65 20 66 6f 72 20 6c 65 66 74 20 53 45 4c 45  ne for left SELE
2b4ef 43 54 22 29 29 3b 0a 20 20 70 50 72 69 6f 72 2d  CT"));.  pPrior-
2b4f0 3e 69 4c 69 6d 69 74 20 3d 20 72 65 67 4c 69 6d  >iLimit = regLim
2b4f1 69 74 41 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  itA;.  sqlite3Se
2b4f2 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 50 72  lect(pParse, pPr
2b4f3 69 6f 72 2c 20 26 64 65 73 74 41 29 3b 0a 20 20  ior, &destA);.  
2b4f4 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b4f5 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2b4f6 20 31 2c 20 72 65 67 45 6f 66 41 29 3b 0a 20 20   1, regEofA);.  
2b4f7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b4f8 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72  1(v, OP_Yield, r
2b4f9 65 67 41 64 64 72 41 29 3b 0a 20 20 56 64 62 65  egAddrA);.  Vdbe
2b4fa 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  NoopComment((v, 
2b4fb 22 45 6e 64 20 63 6f 72 6f 75 74 69 6e 65 20 66  "End coroutine f
2b4fc 6f 72 20 6c 65 66 74 20 53 45 4c 45 43 54 22 29  or left SELECT")
2b4fd 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74  );..  /* Generat
2b4fe 65 20 61 20 63 6f 72 6f 75 74 69 6e 65 20 74 6f  e a coroutine to
2b4ff 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 53 45   evaluate the SE
2b500 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 6f  LECT statement o
2b501 6e 20 0a 20 20 2a 2a 20 74 68 65 20 72 69 67 68  n .  ** the righ
2b502 74 20 2d 20 74 68 65 20 22 42 22 20 73 65 6c 65  t - the "B" sele
2b503 63 74 0a 20 20 2a 2f 0a 20 20 61 64 64 72 53 65  ct.  */.  addrSe
2b504 6c 65 63 74 42 20 3d 20 73 71 6c 69 74 65 33 56  lectB = sqlite3V
2b505 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
2b506 29 3b 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d  );.  VdbeNoopCom
2b507 6d 65 6e 74 28 28 76 2c 20 22 42 65 67 69 6e 20  ment((v, "Begin 
2b508 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20 72 69  coroutine for ri
2b509 67 68 74 20 53 45 4c 45 43 54 22 29 29 3b 0a 20  ght SELECT"));. 
2b50a 20 73 61 76 65 64 4c 69 6d 69 74 20 3d 20 70 2d   savedLimit = p-
2b50b 3e 69 4c 69 6d 69 74 3b 0a 20 20 73 61 76 65 64  >iLimit;.  saved
2b50c 4f 66 66 73 65 74 20 3d 20 70 2d 3e 69 4f 66 66  Offset = p->iOff
2b50d 73 65 74 3b 0a 20 20 70 2d 3e 69 4c 69 6d 69 74  set;.  p->iLimit
2b50e 20 3d 20 72 65 67 4c 69 6d 69 74 42 3b 0a 20 20   = regLimitB;.  
2b50f 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 20  p->iOffset = 0; 
2b510 20 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63   .  sqlite3Selec
2b511 74 28 70 50 61 72 73 65 2c 20 70 2c 20 26 64 65  t(pParse, p, &de
2b512 73 74 42 29 3b 0a 20 20 70 2d 3e 69 4c 69 6d 69  stB);.  p->iLimi
2b513 74 20 3d 20 73 61 76 65 64 4c 69 6d 69 74 3b 0a  t = savedLimit;.
2b514 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20 73    p->iOffset = s
2b515 61 76 65 64 4f 66 66 73 65 74 3b 0a 20 20 73 71  avedOffset;.  sq
2b516 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b517 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31  v, OP_Integer, 1
2b518 2c 20 72 65 67 45 6f 66 42 29 3b 0a 20 20 73 71  , regEofB);.  sq
2b519 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
2b51a 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65 67  v, OP_Yield, reg
2b51b 41 64 64 72 42 29 3b 0a 20 20 56 64 62 65 4e 6f  AddrB);.  VdbeNo
2b51c 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 45  opComment((v, "E
2b51d 6e 64 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72  nd coroutine for
2b51e 20 72 69 67 68 74 20 53 45 4c 45 43 54 22 29 29   right SELECT"))
2b51f 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  ;..  /* Generate
2b520 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68   a subroutine th
2b521 61 74 20 6f 75 74 70 75 74 73 20 74 68 65 20 63  at outputs the c
2b522 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68  urrent row of th
2b523 65 20 41 0a 20 20 2a 2a 20 73 65 6c 65 63 74 20  e A.  ** select 
2b524 61 73 20 74 68 65 20 6e 65 78 74 20 6f 75 74 70  as the next outp
2b525 75 74 20 72 6f 77 20 6f 66 20 74 68 65 20 63 6f  ut row of the co
2b526 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2e 0a 20  mpound select.. 
2b527 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f   */.  VdbeNoopCo
2b528 6d 6d 65 6e 74 28 28 76 2c 20 22 4f 75 74 70 75  mment((v, "Outpu
2b529 74 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 41 22  t routine for A"
2b52a 29 29 3b 0a 20 20 61 64 64 72 4f 75 74 41 20 3d  ));.  addrOutA =
2b52b 20 67 65 6e 65 72 61 74 65 4f 75 74 70 75 74 53   generateOutputS
2b52c 75 62 72 6f 75 74 69 6e 65 28 70 50 61 72 73 65  ubroutine(pParse
2b52d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2b52e 20 20 20 70 2c 20 26 64 65 73 74 41 2c 20 70 44     p, &destA, pD
2b52f 65 73 74 2c 20 72 65 67 4f 75 74 41 2c 0a 20 20  est, regOutA,.  
2b530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
2b531 65 67 50 72 65 76 2c 20 70 4b 65 79 44 75 70 2c  egPrev, pKeyDup,
2b532 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
2b533 4f 46 46 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a  OFF, labelEnd);.
2b534 20 20 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65    .  /* Generate
2b535 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68   a subroutine th
2b536 61 74 20 6f 75 74 70 75 74 73 20 74 68 65 20 63  at outputs the c
2b537 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68  urrent row of th
2b538 65 20 42 0a 20 20 2a 2a 20 73 65 6c 65 63 74 20  e B.  ** select 
2b539 61 73 20 74 68 65 20 6e 65 78 74 20 6f 75 74 70  as the next outp
2b53a 75 74 20 72 6f 77 20 6f 66 20 74 68 65 20 63 6f  ut row of the co
2b53b 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2e 0a 20  mpound select.. 
2b53c 20 2a 2f 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b   */.  if( op==TK
2b53d 5f 41 4c 4c 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 55  _ALL || op==TK_U
2b53e 4e 49 4f 4e 20 29 7b 0a 20 20 20 20 56 64 62 65  NION ){.    Vdbe
2b53f 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  NoopComment((v, 
2b540 22 4f 75 74 70 75 74 20 72 6f 75 74 69 6e 65 20  "Output routine 
2b541 66 6f 72 20 42 22 29 29 3b 0a 20 20 20 20 61 64  for B"));.    ad
2b542 64 72 4f 75 74 42 20 3d 20 67 65 6e 65 72 61 74  drOutB = generat
2b543 65 4f 75 74 70 75 74 53 75 62 72 6f 75 74 69 6e  eOutputSubroutin
2b544 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20  e(pParse,.      
2b545 20 20 20 20 20 20 20 20 20 20 20 70 2c 20 26 64             p, &d
2b546 65 73 74 42 2c 20 70 44 65 73 74 2c 20 72 65 67  estB, pDest, reg
2b547 4f 75 74 42 2c 0a 20 20 20 20 20 20 20 20 20 20  OutB,.          
2b548 20 20 20 20 20 20 20 72 65 67 50 72 65 76 2c 20         regPrev, 
2b549 70 4b 65 79 44 75 70 2c 20 50 34 5f 4b 45 59 49  pKeyDup, P4_KEYI
2b54a 4e 46 4f 5f 53 54 41 54 49 43 2c 20 6c 61 62 65  NFO_STATIC, labe
2b54b 6c 45 6e 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  lEnd);.  }..  /*
2b54c 20 47 65 6e 65 72 61 74 65 20 61 20 73 75 62 72   Generate a subr
2b54d 6f 75 74 69 6e 65 20 74 6f 20 72 75 6e 20 77 68  outine to run wh
2b54e 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20 66  en the results f
2b54f 72 6f 6d 20 73 65 6c 65 63 74 20 41 0a 20 20 2a  rom select A.  *
2b550 2a 20 61 72 65 20 65 78 68 61 75 73 74 65 64 20  * are exhausted 
2b551 61 6e 64 20 6f 6e 6c 79 20 64 61 74 61 20 69 6e  and only data in
2b552 20 73 65 6c 65 63 74 20 42 20 72 65 6d 61 69 6e   select B remain
2b553 73 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f  s..  */.  VdbeNo
2b554 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 65  opComment((v, "e
2b555 6f 66 2d 41 20 73 75 62 72 6f 75 74 69 6e 65 22  of-A subroutine"
2b556 29 29 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b  ));.  if( op==TK
2b557 5f 45 58 43 45 50 54 20 7c 7c 20 6f 70 3d 3d 54  _EXCEPT || op==T
2b558 4b 5f 49 4e 54 45 52 53 45 43 54 20 29 7b 0a 20  K_INTERSECT ){. 
2b559 20 20 20 61 64 64 72 45 6f 66 41 20 3d 20 73 71     addrEofA = sq
2b55a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b55b 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 6c  v, OP_Goto, 0, l
2b55c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 7d 65 6c 73  abelEnd);.  }els
2b55d 65 7b 20 20 0a 20 20 20 20 61 64 64 72 45 6f 66  e{  .    addrEof
2b55e 41 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  A = sqlite3VdbeA
2b55f 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20  ddOp2(v, OP_If, 
2b560 72 65 67 45 6f 66 42 2c 20 6c 61 62 65 6c 45 6e  regEofB, labelEn
2b561 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  d);.    sqlite3V
2b562 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2b563 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 42 2c 20  Gosub, regOutB, 
2b564 61 64 64 72 4f 75 74 42 29 3b 0a 20 20 20 20 73  addrOutB);.    s
2b565 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
2b566 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65  (v, OP_Yield, re
2b567 67 41 64 64 72 42 29 3b 0a 20 20 20 20 73 71 6c  gAddrB);.    sql
2b568 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2b569 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64  , OP_Goto, 0, ad
2b56a 64 72 45 6f 66 41 29 3b 0a 20 20 7d 0a 0a 20 20  drEofA);.  }..  
2b56b 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73 75  /* Generate a su
2b56c 62 72 6f 75 74 69 6e 65 20 74 6f 20 72 75 6e 20  broutine to run 
2b56d 77 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73  when the results
2b56e 20 66 72 6f 6d 20 73 65 6c 65 63 74 20 42 0a 20   from select B. 
2b56f 20 2a 2a 20 61 72 65 20 65 78 68 61 75 73 74 65   ** are exhauste
2b570 64 20 61 6e 64 20 6f 6e 6c 79 20 64 61 74 61 20  d and only data 
2b571 69 6e 20 73 65 6c 65 63 74 20 41 20 72 65 6d 61  in select A rema
2b572 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ins..  */.  if( 
2b573 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 43 54  op==TK_INTERSECT
2b574 20 29 7b 0a 20 20 20 20 61 64 64 72 45 6f 66 42   ){.    addrEofB
2b575 20 3d 20 61 64 64 72 45 6f 66 41 3b 0a 20 20 7d   = addrEofA;.  }
2b576 65 6c 73 65 7b 20 20 0a 20 20 20 20 56 64 62 65  else{  .    Vdbe
2b577 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  NoopComment((v, 
2b578 22 65 6f 66 2d 42 20 73 75 62 72 6f 75 74 69 6e  "eof-B subroutin
2b579 65 22 29 29 3b 0a 20 20 20 20 61 64 64 72 45 6f  e"));.    addrEo
2b57a 66 42 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  fB = sqlite3Vdbe
2b57b 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c  AddOp2(v, OP_If,
2b57c 20 72 65 67 45 6f 66 41 2c 20 6c 61 62 65 6c 45   regEofA, labelE
2b57d 6e 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  nd);.    sqlite3
2b57e 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b57f 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 41 2c  _Gosub, regOutA,
2b580 20 61 64 64 72 4f 75 74 41 29 3b 0a 20 20 20 20   addrOutA);.    
2b581 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b582 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72  1(v, OP_Yield, r
2b583 65 67 41 64 64 72 41 29 3b 0a 20 20 20 20 73 71  egAddrA);.    sq
2b584 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b585 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61  v, OP_Goto, 0, a
2b586 64 64 72 45 6f 66 42 29 3b 0a 20 20 7d 0a 0a 20  ddrEofB);.  }.. 
2b587 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
2b588 65 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 20  e to handle the 
2b589 63 61 73 65 20 6f 66 20 41 3c 42 0a 20 20 2a 2f  case of A<B.  */
2b58a 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65  .  VdbeNoopComme
2b58b 6e 74 28 28 76 2c 20 22 41 2d 6c 74 2d 42 20 73  nt((v, "A-lt-B s
2b58c 75 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20  ubroutine"));.  
2b58d 61 64 64 72 41 6c 74 42 20 3d 20 73 71 6c 69 74  addrAltB = sqlit
2b58e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2b58f 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74  OP_Gosub, regOut
2b590 41 2c 20 61 64 64 72 4f 75 74 41 29 3b 0a 20 20  A, addrOutA);.  
2b591 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b592 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72  1(v, OP_Yield, r
2b593 65 67 41 64 64 72 41 29 3b 0a 20 20 73 71 6c 69  egAddrA);.  sqli
2b594 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2b595 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 41 2c   OP_If, regEofA,
2b596 20 61 64 64 72 45 6f 66 41 29 3b 0a 20 20 73 71   addrEofA);.  sq
2b597 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b598 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 6c  v, OP_Goto, 0, l
2b599 61 62 65 6c 43 6d 70 72 29 3b 0a 0a 20 20 2f 2a  abelCmpr);..  /*
2b59a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
2b59b 6f 20 68 61 6e 64 6c 65 20 74 68 65 20 63 61 73  o handle the cas
2b59c 65 20 6f 66 20 41 3d 3d 42 0a 20 20 2a 2f 0a 20  e of A==B.  */. 
2b59d 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20   if( op==TK_ALL 
2b59e 29 7b 0a 20 20 20 20 61 64 64 72 41 65 71 42 20  ){.    addrAeqB 
2b59f 3d 20 61 64 64 72 41 6c 74 42 3b 0a 20 20 7d 65  = addrAltB;.  }e
2b5a0 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49  lse if( op==TK_I
2b5a1 4e 54 45 52 53 45 43 54 20 29 7b 0a 20 20 20 20  NTERSECT ){.    
2b5a2 61 64 64 72 41 65 71 42 20 3d 20 61 64 64 72 41  addrAeqB = addrA
2b5a3 6c 74 42 3b 0a 20 20 20 20 61 64 64 72 41 6c 74  ltB;.    addrAlt
2b5a4 42 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  B++;.  }else{.  
2b5a5 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e    VdbeNoopCommen
2b5a6 74 28 28 76 2c 20 22 41 2d 65 71 2d 42 20 73 75  t((v, "A-eq-B su
2b5a7 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 20  broutine"));.   
2b5a8 20 61 64 64 72 41 65 71 42 20 3d 0a 20 20 20 20   addrAeqB =.    
2b5a9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b5aa 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72  1(v, OP_Yield, r
2b5ab 65 67 41 64 64 72 41 29 3b 0a 20 20 20 20 73 71  egAddrA);.    sq
2b5ac 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2b5ad 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66  v, OP_If, regEof
2b5ae 41 2c 20 61 64 64 72 45 6f 66 41 29 3b 0a 20 20  A, addrEofA);.  
2b5af 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b5b0 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
2b5b1 30 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0a 20  0, labelCmpr);. 
2b5b2 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74   }..  /* Generat
2b5b3 65 20 63 6f 64 65 20 74 6f 20 68 61 6e 64 6c 65  e code to handle
2b5b4 20 74 68 65 20 63 61 73 65 20 6f 66 20 41 3e 42   the case of A>B
2b5b5 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70  .  */.  VdbeNoop
2b5b6 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 41 2d 67  Comment((v, "A-g
2b5b7 74 2d 42 20 73 75 62 72 6f 75 74 69 6e 65 22 29  t-B subroutine")
2b5b8 29 3b 0a 20 20 61 64 64 72 41 67 74 42 20 3d 20  );.  addrAgtB = 
2b5b9 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
2b5ba 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 69 66 28  ntAddr(v);.  if(
2b5bb 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 7c 7c 20 6f   op==TK_ALL || o
2b5bc 70 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 29 7b 0a 20  p==TK_UNION ){. 
2b5bd 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2b5be 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62  dOp2(v, OP_Gosub
2b5bf 2c 20 72 65 67 4f 75 74 42 2c 20 61 64 64 72 4f  , regOutB, addrO
2b5c0 75 74 42 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  utB);.  }.  sqli
2b5c1 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2b5c2 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65 67 41 64   OP_Yield, regAd
2b5c3 64 72 42 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  drB);.  sqlite3V
2b5c4 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2b5c5 49 66 2c 20 72 65 67 45 6f 66 42 2c 20 61 64 64  If, regEofB, add
2b5c6 72 45 6f 66 42 29 3b 0a 20 20 73 71 6c 69 74 65  rEofB);.  sqlite
2b5c7 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2b5c8 50 5f 47 6f 74 6f 2c 20 30 2c 20 6c 61 62 65 6c  P_Goto, 0, label
2b5c9 43 6d 70 72 29 3b 0a 0a 20 20 2f 2a 20 54 68 69  Cmpr);..  /* Thi
2b5ca 73 20 63 6f 64 65 20 72 75 6e 73 20 6f 6e 63 65  s code runs once
2b5cb 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 65   to initialize e
2b5cc 76 65 72 79 74 68 69 6e 67 2e 0a 20 20 2a 2f 0a  verything..  */.
2b5cd 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2b5ce 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20  pHere(v, j1);.  
2b5cf 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b5d0 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2b5d1 20 30 2c 20 72 65 67 45 6f 66 41 29 3b 0a 20 20   0, regEofA);.  
2b5d2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b5d3 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2b5d4 20 30 2c 20 72 65 67 45 6f 66 42 29 3b 0a 20 20   0, regEofB);.  
2b5d5 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2b5d6 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 72  2(v, OP_Gosub, r
2b5d7 65 67 41 64 64 72 41 2c 20 61 64 64 72 53 65 6c  egAddrA, addrSel
2b5d8 65 63 74 41 29 3b 0a 20 20 73 71 6c 69 74 65 33  ectA);.  sqlite3
2b5d9 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2b5da 5f 47 6f 73 75 62 2c 20 72 65 67 41 64 64 72 42  _Gosub, regAddrB
2b5db 2c 20 61 64 64 72 53 65 6c 65 63 74 42 29 3b 0a  , addrSelectB);.
2b5dc 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2b5dd 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65  Op2(v, OP_If, re
2b5de 67 45 6f 66 41 2c 20 61 64 64 72 45 6f 66 41 29  gEofA, addrEofA)
2b5df 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2b5e0 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20  ddOp2(v, OP_If, 
2b5e1 72 65 67 45 6f 66 42 2c 20 61 64 64 72 45 6f 66  regEofB, addrEof
2b5e2 42 29 3b 0a 0a 20 20 2f 2a 20 49 6d 70 6c 65 6d  B);..  /* Implem
2b5e3 65 6e 74 20 74 68 65 20 6d 61 69 6e 20 6d 65 72  ent the main mer
2b5e4 67 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a 20 20 73  ge loop.  */.  s
2b5e5 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
2b5e6 65 4c 61 62 65 6c 28 76 2c 20 6c 61 62 65 6c 43  eLabel(v, labelC
2b5e7 6d 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  mpr);.  sqlite3V
2b5e8 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2b5e9 50 65 72 6d 75 74 61 74 69 6f 6e 2c 20 30 2c 20  Permutation, 0, 
2b5ea 30 2c 20 30 2c 20 28 63 68 61 72 2a 29 61 50 65  0, 0, (char*)aPe
2b5eb 72 6d 75 74 65 2c 20 50 34 5f 49 4e 54 41 52 52  rmute, P4_INTARR
2b5ec 41 59 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  AY);.  sqlite3Vd
2b5ed 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43  beAddOp4(v, OP_C
2b5ee 6f 6d 70 61 72 65 2c 20 64 65 73 74 41 2e 69 4d  ompare, destA.iM
2b5ef 65 6d 2c 20 64 65 73 74 42 2e 69 4d 65 6d 2c 20  em, destB.iMem, 
2b5f0 6e 4f 72 64 65 72 42 79 2c 0a 20 20 20 20 20 20  nOrderBy,.      
2b5f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b5f2 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 4d 65     (char*)pKeyMe
2b5f3 72 67 65 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  rge, P4_KEYINFO_
2b5f4 48 41 4e 44 4f 46 46 29 3b 0a 20 20 73 71 6c 69  HANDOFF);.  sqli
2b5f5 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2b5f6 20 4f 50 5f 4a 75 6d 70 2c 20 61 64 64 72 41 6c   OP_Jump, addrAl
2b5f7 74 42 2c 20 61 64 64 72 41 65 71 42 2c 20 61 64  tB, addrAeqB, ad
2b5f8 64 72 41 67 74 42 29 3b 0a 0a 20 20 2f 2a 20 52  drAgtB);..  /* R
2b5f9 65 6c 65 61 73 65 20 74 65 6d 70 6f 72 61 72 79  elease temporary
2b5fa 20 72 65 67 69 73 74 65 72 73 0a 20 20 2a 2f 0a   registers.  */.
2b5fb 20 20 69 66 28 20 72 65 67 50 72 65 76 20 29 7b    if( regPrev ){
2b5fc 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65  .    sqlite3Rele
2b5fd 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61  aseTempRange(pPa
2b5fe 72 73 65 2c 20 72 65 67 50 72 65 76 2c 20 6e 4f  rse, regPrev, nO
2b5ff 72 64 65 72 42 79 2b 31 29 3b 0a 20 20 7d 0a 0a  rderBy+1);.  }..
2b600 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65    /* Jump to the
2b601 20 74 68 69 73 20 70 6f 69 6e 74 20 69 6e 20 6f   this point in o
2b602 72 64 65 72 20 74 6f 20 74 65 72 6d 69 6e 61 74  rder to terminat
2b603 65 20 74 68 65 20 71 75 65 72 79 2e 0a 20 20 2a  e the query..  *
2b604 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52  /.  sqlite3VdbeR
2b605 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 6c  esolveLabel(v, l
2b606 61 62 65 6c 45 6e 64 29 3b 0a 0a 20 20 2f 2a 20  abelEnd);..  /* 
2b607 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  Set the number o
2b608 66 20 6f 75 74 70 75 74 20 63 6f 6c 75 6d 6e 73  f output columns
2b609 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 65 73  .  */.  if( pDes
2b60a 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75  t->eDest==SRT_Ou
2b60b 74 70 75 74 20 29 7b 0a 20 20 20 20 53 65 6c 65  tput ){.    Sele
2b60c 63 74 20 2a 70 46 69 72 73 74 20 3d 20 70 50 72  ct *pFirst = pPr
2b60d 69 6f 72 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  ior;.    while( 
2b60e 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 20 29  pFirst->pPrior )
2b60f 20 70 46 69 72 73 74 20 3d 20 70 46 69 72 73 74   pFirst = pFirst
2b610 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 67 65  ->pPrior;.    ge
2b611 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65  nerateColumnName
2b612 73 28 70 50 61 72 73 65 2c 20 30 2c 20 70 46 69  s(pParse, 0, pFi
2b613 72 73 74 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20  rst->pEList);.  
2b614 7d 0a 0a 20 20 2f 2a 20 52 65 61 73 73 65 6d 62  }..  /* Reassemb
2b615 6c 79 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20  ly the compound 
2b616 71 75 65 72 79 20 73 6f 20 74 68 61 74 20 69 74  query so that it
2b617 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 63   will be freed c
2b618 6f 72 72 65 63 74 6c 79 0a 20 20 2a 2a 20 62 79  orrectly.  ** by
2b619 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e   the calling fun
2b61a 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28 20 70  ction */.  if( p
2b61b 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20 20  ->pPrior ){.    
2b61c 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
2b61d 65 74 65 28 64 62 2c 20 70 2d 3e 70 50 72 69 6f  ete(db, p->pPrio
2b61e 72 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 50 72  r);.  }.  p->pPr
2b61f 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0a 0a 20  ior = pPrior;.. 
2b620 20 2f 2a 2a 2a 20 54 42 44 3a 20 20 49 6e 73 65   /*** TBD:  Inse
2b621 72 74 20 73 75 62 72 6f 75 74 69 6e 65 20 63 61  rt subroutine ca
2b622 6c 6c 73 20 74 6f 20 63 6c 6f 73 65 20 63 75 72  lls to close cur
2b623 73 6f 72 73 20 6f 6e 20 69 6e 63 6f 6d 70 6c 65  sors on incomple
2b624 74 65 0a 20 20 2a 2a 2a 2a 20 73 75 62 71 75 65  te.  **** subque
2b625 72 69 65 73 20 2a 2a 2a 2a 2f 0a 20 20 72 65 74  ries ****/.  ret
2b626 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
2b627 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65  .#endif..#if !de
2b628 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2b629 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c 20 21  T_SUBQUERY) || !
2b62a 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2b62b 4d 49 54 5f 56 49 45 57 29 0a 2f 2a 20 46 6f 72  MIT_VIEW)./* For
2b62c 77 61 72 64 20 44 65 63 6c 61 72 61 74 69 6f 6e  ward Declaration
2b62d 73 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  s */.static void
2b62e 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 73   substExprList(s
2b62f 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 4c 69 73  qlite3*, ExprLis
2b630 74 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73  t*, int, ExprLis
2b631 74 2a 29 3b 0a 73 74 61 74 69 63 20 76 6f 69 64  t*);.static void
2b632 20 73 75 62 73 74 53 65 6c 65 63 74 28 73 71 6c   substSelect(sql
2b633 69 74 65 33 2a 2c 20 53 65 6c 65 63 74 20 2a 2c  ite3*, Select *,
2b634 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 20 2a   int, ExprList *
2b635 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 63 61 6e 20 74  );../*.** Scan t
2b636 68 72 6f 75 67 68 20 74 68 65 20 65 78 70 72 65  hrough the expre
2b637 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 20 52 65  ssion pExpr.  Re
2b638 70 6c 61 63 65 20 65 76 65 72 79 20 72 65 66 65  place every refe
2b639 72 65 6e 63 65 20 74 6f 0a 2a 2a 20 61 20 63 6f  rence to.** a co
2b63a 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65 20 6e 75  lumn in table nu
2b63b 6d 62 65 72 20 69 54 61 62 6c 65 20 77 69 74 68  mber iTable with
2b63c 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 69   a copy of the i
2b63d 43 6f 6c 75 6d 6e 2d 74 68 0a 2a 2a 20 65 6e 74  Column-th.** ent
2b63e 72 79 20 69 6e 20 70 45 4c 69 73 74 2e 20 20 28  ry in pEList.  (
2b63f 42 75 74 20 6c 65 61 76 65 20 72 65 66 65 72 65  But leave refere
2b640 6e 63 65 73 20 74 6f 20 74 68 65 20 52 4f 57 49  nces to the ROWI
2b641 44 20 63 6f 6c 75 6d 6e 20 0a 2a 2a 20 75 6e 63  D column .** unc
2b642 68 61 6e 67 65 64 2e 29 0a 2a 2a 0a 2a 2a 20 54  hanged.).**.** T
2b643 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 70  his routine is p
2b644 61 72 74 20 6f 66 20 74 68 65 20 66 6c 61 74 74  art of the flatt
2b645 65 6e 69 6e 67 20 70 72 6f 63 65 64 75 72 65 2e  ening procedure.
2b646 20 20 41 20 73 75 62 71 75 65 72 79 0a 2a 2a 20    A subquery.** 
2b647 77 68 6f 73 65 20 72 65 73 75 6c 74 20 73 65 74  whose result set
2b648 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 70   is defined by p
2b649 45 4c 69 73 74 20 61 70 70 65 61 72 73 20 61 73  EList appears as
2b64a 20 65 6e 74 72 79 20 69 6e 20 74 68 65 0a 2a 2a   entry in the.**
2b64b 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
2b64c 61 20 53 45 4c 45 43 54 20 73 75 63 68 20 74 68  a SELECT such th
2b64d 61 74 20 74 68 65 20 56 44 42 45 20 63 75 72 73  at the VDBE curs
2b64e 6f 72 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74  or assigned to t
2b64f 68 61 74 0a 2a 2a 20 46 4f 52 4d 20 63 6c 61 75  hat.** FORM clau
2b650 73 65 20 65 6e 74 72 79 20 69 73 20 69 54 61 62  se entry is iTab
2b651 6c 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  le.  This routin
2b652 65 20 6d 61 6b 65 20 74 68 65 20 6e 65 63 65 73  e make the neces
2b653 73 61 72 79 20 0a 2a 2a 20 63 68 61 6e 67 65 73  sary .** changes
2b654 20 74 6f 20 70 45 78 70 72 20 73 6f 20 74 68 61   to pExpr so tha
2b655 74 20 69 74 20 72 65 66 65 72 73 20 64 69 72 65  t it refers dire
2b656 63 74 6c 79 20 74 6f 20 74 68 65 20 73 6f 75 72  ctly to the sour
2b657 63 65 20 74 61 62 6c 65 0a 2a 2a 20 6f 66 20 74  ce table.** of t
2b658 68 65 20 73 75 62 71 75 65 72 79 20 72 61 74 68  he subquery rath
2b659 65 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  er the result se
2b65a 74 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 72  t of the subquer
2b65b 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 45 78 70  y..*/.static Exp
2b65c 72 20 2a 73 75 62 73 74 45 78 70 72 28 0a 20 20  r *substExpr(.  
2b65d 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
2b65e 20 20 20 20 2f 2a 20 52 65 70 6f 72 74 20 6d 61      /* Report ma
2b65f 6c 6c 6f 63 20 65 72 72 6f 72 73 20 74 6f 20 74  lloc errors to t
2b660 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  his connection *
2b661 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c  /.  Expr *pExpr,
2b662 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 20          /* Expr 
2b663 69 6e 20 77 68 69 63 68 20 73 75 62 73 74 69 74  in which substit
2b664 75 74 69 6f 6e 20 6f 63 63 75 72 73 20 2a 2f 0a  ution occurs */.
2b665 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20    int iTable,   
2b666 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74        /* Table t
2b667 6f 20 62 65 20 73 75 62 73 74 69 74 75 74 65 64  o be substituted
2b668 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
2b669 70 45 4c 69 73 74 20 20 20 20 2f 2a 20 53 75 62  pEList    /* Sub
2b66a 73 74 69 74 75 74 65 20 65 78 70 72 65 73 73 69  stitute expressi
2b66b 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20  ons */.){.  if( 
2b66c 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72  pExpr==0 ) retur
2b66d 6e 20 30 3b 0a 20 20 69 66 28 20 70 45 78 70 72  n 0;.  if( pExpr
2b66e 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20  ->op==TK_COLUMN 
2b66f 26 26 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  && pExpr->iTable
2b670 3d 3d 69 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  ==iTable ){.    
2b671 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  if( pExpr->iColu
2b672 6d 6e 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 45  mn<0 ){.      pE
2b673 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c  xpr->op = TK_NUL
2b674 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  L;.    }else{.  
2b675 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a      Expr *pNew;.
2b676 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45        assert( pE
2b677 4c 69 73 74 21 3d 30 20 26 26 20 70 45 78 70 72  List!=0 && pExpr
2b678 2d 3e 69 43 6f 6c 75 6d 6e 3c 70 45 4c 69 73 74  ->iColumn<pEList
2b679 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 20 20 20  ->nExpr );.     
2b67a 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
2b67b 70 4c 65 66 74 3d 3d 30 20 26 26 20 70 45 78 70  pLeft==0 && pExp
2b67c 72 2d 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a  r->pRight==0 );.
2b67d 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c        pNew = sql
2b67e 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
2b67f 70 45 4c 69 73 74 2d 3e 61 5b 70 45 78 70 72 2d  pEList->a[pExpr-
2b680 3e 69 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72 2c  >iColumn].pExpr,
2b681 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70   0);.      if( p
2b682 4e 65 77 20 26 26 20 70 45 78 70 72 2d 3e 70 43  New && pExpr->pC
2b683 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70  oll ){.        p
2b684 4e 65 77 2d 3e 70 43 6f 6c 6c 20 3d 20 70 45 78  New->pColl = pEx
2b685 70 72 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20  pr->pColl;.     
2b686 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
2b687 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
2b688 45 78 70 72 29 3b 0a 20 20 20 20 20 20 70 45 78  Expr);.      pEx
2b689 70 72 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 7d  pr = pNew;.    }
2b68a 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 45  .  }else{.    pE
2b68b 78 70 72 2d 3e 70 4c 65 66 74 20 3d 20 73 75 62  xpr->pLeft = sub
2b68c 73 74 45 78 70 72 28 64 62 2c 20 70 45 78 70 72  stExpr(db, pExpr
2b68d 2d 3e 70 4c 65 66 74 2c 20 69 54 61 62 6c 65 2c  ->pLeft, iTable,
2b68e 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 70 45   pEList);.    pE
2b68f 78 70 72 2d 3e 70 52 69 67 68 74 20 3d 20 73 75  xpr->pRight = su
2b690 62 73 74 45 78 70 72 28 64 62 2c 20 70 45 78 70  bstExpr(db, pExp
2b691 72 2d 3e 70 52 69 67 68 74 2c 20 69 54 61 62 6c  r->pRight, iTabl
2b692 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  e, pEList);.    
2b693 69 66 28 20 45 78 70 72 48 61 73 50 72 6f 70 65  if( ExprHasPrope
2b694 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 78 49  rty(pExpr, EP_xI
2b695 73 53 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20  sSelect) ){.    
2b696 20 20 73 75 62 73 74 53 65 6c 65 63 74 28 64 62    substSelect(db
2b697 2c 20 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65  , pExpr->x.pSele
2b698 63 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69  ct, iTable, pELi
2b699 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  st);.    }else{.
2b69a 20 20 20 20 20 20 73 75 62 73 74 45 78 70 72 4c        substExprL
2b69b 69 73 74 28 64 62 2c 20 70 45 78 70 72 2d 3e 78  ist(db, pExpr->x
2b69c 2e 70 4c 69 73 74 2c 20 69 54 61 62 6c 65 2c 20  .pList, iTable, 
2b69d 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20  pEList);.    }. 
2b69e 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 45 78 70   }.  return pExp
2b69f 72 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  r;.}.static void
2b6a0 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 0a   substExprList(.
2b6a1 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
2b6a2 20 20 20 20 20 20 20 2f 2a 20 52 65 70 6f 72 74         /* Report
2b6a3 20 6d 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 68   malloc errors h
2b6a4 65 72 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ere */.  ExprLis
2b6a5 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 2f 2a  t *pList,     /*
2b6a6 20 4c 69 73 74 20 74 6f 20 73 63 61 6e 20 61 6e   List to scan an
2b6a7 64 20 69 6e 20 77 68 69 63 68 20 74 6f 20 6d 61  d in which to ma
2b6a8 6b 65 20 73 75 62 73 74 69 74 75 74 65 73 20 2a  ke substitutes *
2b6a9 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20  /.  int iTable, 
2b6aa 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
2b6ab 65 20 74 6f 20 62 65 20 73 75 62 73 74 69 74 75  e to be substitu
2b6ac 74 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ted */.  ExprLis
2b6ad 74 20 2a 70 45 4c 69 73 74 20 20 20 20 20 2f 2a  t *pEList     /*
2b6ae 20 53 75 62 73 74 69 74 75 74 65 20 76 61 6c 75   Substitute valu
2b6af 65 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  es */.){.  int i
2b6b0 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30  ;.  if( pList==0
2b6b1 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72   ) return;.  for
2b6b2 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  (i=0; i<pList->n
2b6b3 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
2b6b4 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70  pList->a[i].pExp
2b6b5 72 20 3d 20 73 75 62 73 74 45 78 70 72 28 64 62  r = substExpr(db
2b6b6 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45  , pList->a[i].pE
2b6b7 78 70 72 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c  xpr, iTable, pEL
2b6b8 69 73 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74  ist);.  }.}.stat
2b6b9 69 63 20 76 6f 69 64 20 73 75 62 73 74 53 65 6c  ic void substSel
2b6ba 65 63 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ect(.  sqlite3 *
2b6bb 64 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52  db,         /* R
2b6bc 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65 72 72  eport malloc err
2b6bd 6f 72 73 20 68 65 72 65 20 2a 2f 0a 20 20 53 65  ors here */.  Se
2b6be 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20  lect *p,        
2b6bf 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 73 74 61     /* SELECT sta
2b6c0 74 65 6d 65 6e 74 20 69 6e 20 77 68 69 63 68 20  tement in which 
2b6c1 74 6f 20 6d 61 6b 65 20 73 75 62 73 74 69 74 75  to make substitu
2b6c2 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 69  tions */.  int i
2b6c3 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20  Table,          
2b6c4 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62 65 20 72  /* Table to be r
2b6c5 65 70 6c 61 63 65 64 20 2a 2f 0a 20 20 45 78 70  eplaced */.  Exp
2b6c6 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 20 20  rList *pEList   
2b6c7 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20    /* Substitute 
2b6c8 76 61 6c 75 65 73 20 2a 2f 0a 29 7b 0a 20 20 53  values */.){.  S
2b6c9 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 20  rcList *pSrc;.  
2b6ca 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
2b6cb 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 6e  tem *pItem;.  in
2b6cc 74 20 69 3b 0a 20 20 69 66 28 20 21 70 20 29 20  t i;.  if( !p ) 
2b6cd 72 65 74 75 72 6e 3b 0a 20 20 73 75 62 73 74 45  return;.  substE
2b6ce 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d 3e 70  xprList(db, p->p
2b6cf 45 4c 69 73 74 2c 20 69 54 61 62 6c 65 2c 20 70  EList, iTable, p
2b6d0 45 4c 69 73 74 29 3b 0a 20 20 73 75 62 73 74 45  EList);.  substE
2b6d1 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d 3e 70  xprList(db, p->p
2b6d2 47 72 6f 75 70 42 79 2c 20 69 54 61 62 6c 65 2c  GroupBy, iTable,
2b6d3 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75 62 73   pEList);.  subs
2b6d4 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d  tExprList(db, p-
2b6d5 3e 70 4f 72 64 65 72 42 79 2c 20 69 54 61 62 6c  >pOrderBy, iTabl
2b6d6 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 70 2d  e, pEList);.  p-
2b6d7 3e 70 48 61 76 69 6e 67 20 3d 20 73 75 62 73 74  >pHaving = subst
2b6d8 45 78 70 72 28 64 62 2c 20 70 2d 3e 70 48 61 76  Expr(db, p->pHav
2b6d9 69 6e 67 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c  ing, iTable, pEL
2b6da 69 73 74 29 3b 0a 20 20 70 2d 3e 70 57 68 65 72  ist);.  p->pWher
2b6db 65 20 3d 20 73 75 62 73 74 45 78 70 72 28 64 62  e = substExpr(db
2b6dc 2c 20 70 2d 3e 70 57 68 65 72 65 2c 20 69 54 61  , p->pWhere, iTa
2b6dd 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  ble, pEList);.  
2b6de 73 75 62 73 74 53 65 6c 65 63 74 28 64 62 2c 20  substSelect(db, 
2b6df 70 2d 3e 70 50 72 69 6f 72 2c 20 69 54 61 62 6c  p->pPrior, iTabl
2b6e0 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 70 53  e, pEList);.  pS
2b6e1 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20  rc = p->pSrc;.  
2b6e2 61 73 73 65 72 74 28 20 70 53 72 63 20 29 3b 20  assert( pSrc ); 
2b6e3 20 2f 2a 20 45 76 65 6e 20 66 6f 72 20 28 53 45   /* Even for (SE
2b6e4 4c 45 43 54 20 31 29 20 77 65 20 68 61 76 65 3a  LECT 1) we have:
2b6e5 20 70 53 72 63 21 3d 30 20 62 75 74 20 70 53 72   pSrc!=0 but pSr
2b6e6 63 2d 3e 6e 53 72 63 3d 3d 30 20 2a 2f 0a 20 20  c->nSrc==0 */.  
2b6e7 69 66 28 20 41 4c 57 41 59 53 28 70 53 72 63 29  if( ALWAYS(pSrc)
2b6e8 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 70 53   ){.    for(i=pS
2b6e9 72 63 2d 3e 6e 53 72 63 2c 20 70 49 74 65 6d 3d  rc->nSrc, pItem=
2b6ea 70 53 72 63 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d  pSrc->a; i>0; i-
2b6eb 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  -, pItem++){.   
2b6ec 20 20 20 73 75 62 73 74 53 65 6c 65 63 74 28 64     substSelect(d
2b6ed 62 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63  b, pItem->pSelec
2b6ee 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73  t, iTable, pELis
2b6ef 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  t);.    }.  }.}.
2b6f0 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
2b6f1 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
2b6f2 55 42 51 55 45 52 59 29 20 7c 7c 20 21 64 65 66  UBQUERY) || !def
2b6f3 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
2b6f4 5f 56 49 45 57 29 20 2a 2f 0a 0a 23 69 66 20 21  _VIEW) */..#if !
2b6f5 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2b6f6 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c  MIT_SUBQUERY) ||
2b6f7 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
2b6f8 5f 4f 4d 49 54 5f 56 49 45 57 29 0a 2f 2a 0a 2a  _OMIT_VIEW)./*.*
2b6f9 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
2b6fa 74 74 65 6d 70 74 73 20 74 6f 20 66 6c 61 74 74  ttempts to flatt
2b6fb 65 6e 20 73 75 62 71 75 65 72 69 65 73 20 69 6e  en subqueries in
2b6fc 20 6f 72 64 65 72 20 74 6f 20 73 70 65 65 64 0a   order to speed.
2b6fd 2a 2a 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 49  ** execution.  I
2b6fe 74 20 72 65 74 75 72 6e 73 20 31 20 69 66 20 69  t returns 1 if i
2b6ff 74 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 20  t makes changes 
2b700 61 6e 64 20 30 20 69 66 20 6e 6f 20 66 6c 61 74  and 0 if no flat
2b701 74 65 6e 69 6e 67 0a 2a 2a 20 6f 63 63 75 72 73  tening.** occurs
2b702 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 75 6e 64 65 72  ..**.** To under
2b703 73 74 61 6e 64 20 74 68 65 20 63 6f 6e 63 65 70  stand the concep
2b704 74 20 6f 66 20 66 6c 61 74 74 65 6e 69 6e 67 2c  t of flattening,
2b705 20 63 6f 6e 73 69 64 65 72 20 74 68 65 20 66 6f   consider the fo
2b706 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 71 75 65 72 79  llowing.** query
2b707 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45  :.**.**     SELE
2b708 43 54 20 61 20 46 52 4f 4d 20 28 53 45 4c 45 43  CT a FROM (SELEC
2b709 54 20 78 2b 79 20 41 53 20 61 20 46 52 4f 4d 20  T x+y AS a FROM 
2b70a 74 31 20 57 48 45 52 45 20 7a 3c 31 30 30 29 20  t1 WHERE z<100) 
2b70b 57 48 45 52 45 20 61 3e 35 0a 2a 2a 0a 2a 2a 20  WHERE a>5.**.** 
2b70c 54 68 65 20 64 65 66 61 75 6c 74 20 77 61 79 20  The default way 
2b70d 6f 66 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20  of implementing 
2b70e 74 68 69 73 20 71 75 65 72 79 20 69 73 20 74 6f  this query is to
2b70f 20 65 78 65 63 75 74 65 20 74 68 65 0a 2a 2a 20   execute the.** 
2b710 73 75 62 71 75 65 72 79 20 66 69 72 73 74 20 61  subquery first a
2b711 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  nd store the res
2b712 75 6c 74 73 20 69 6e 20 61 20 74 65 6d 70 6f 72  ults in a tempor
2b713 61 72 79 20 74 61 62 6c 65 2c 20 74 68 65 6e 0a  ary table, then.
2b714 2a 2a 20 72 75 6e 20 74 68 65 20 6f 75 74 65 72  ** run the outer
2b715 20 71 75 65 72 79 20 6f 6e 20 74 68 61 74 20 74   query on that t
2b716 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2e 20  emporary table. 
2b717 20 54 68 69 73 20 72 65 71 75 69 72 65 73 20 74   This requires t
2b718 77 6f 0a 2a 2a 20 70 61 73 73 65 73 20 6f 76 65  wo.** passes ove
2b719 72 20 74 68 65 20 64 61 74 61 2e 20 20 46 75 72  r the data.  Fur
2b71a 74 68 65 72 6d 6f 72 65 2c 20 62 65 63 61 75 73  thermore, becaus
2b71b 65 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  e the temporary 
2b71c 74 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f 20  table.** has no 
2b71d 69 6e 64 69 63 65 73 2c 20 74 68 65 20 57 48 45  indices, the WHE
2b71e 52 45 20 63 6c 61 75 73 65 20 6f 6e 20 74 68 65  RE clause on the
2b71f 20 6f 75 74 65 72 20 71 75 65 72 79 20 63 61 6e   outer query can
2b720 6e 6f 74 20 62 65 0a 2a 2a 20 6f 70 74 69 6d 69  not be.** optimi
2b721 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  zed..**.** This 
2b722 72 6f 75 74 69 6e 65 20 61 74 74 65 6d 70 74 73  routine attempts
2b723 20 74 6f 20 72 65 77 72 69 74 65 20 71 75 65 72   to rewrite quer
2b724 69 65 73 20 73 75 63 68 20 61 73 20 74 68 65 20  ies such as the 
2b725 61 62 6f 76 65 20 69 6e 74 6f 0a 2a 2a 20 61 20  above into.** a 
2b726 73 69 6e 67 6c 65 20 66 6c 61 74 20 73 65 6c 65  single flat sele
2b727 63 74 2c 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a  ct, like this:.*
2b728 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  *.**     SELECT 
2b729 78 2b 79 20 41 53 20 61 20 46 52 4f 4d 20 74 31  x+y AS a FROM t1
2b72a 20 57 48 45 52 45 20 7a 3c 31 30 30 20 41 4e 44   WHERE z<100 AND
2b72b 20 61 3e 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63   a>5.**.** The c
2b72c 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 66 6f  ode generated fo
2b72d 72 20 74 68 69 73 20 73 69 6d 70 69 66 69 63 61  r this simpifica
2b72e 74 69 6f 6e 20 67 69 76 65 73 20 74 68 65 20 73  tion gives the s
2b72f 61 6d 65 20 72 65 73 75 6c 74 0a 2a 2a 20 62 75  ame result.** bu
2b730 74 20 6f 6e 6c 79 20 68 61 73 20 74 6f 20 73 63  t only has to sc
2b731 61 6e 20 74 68 65 20 64 61 74 61 20 6f 6e 63 65  an the data once
2b732 2e 20 20 41 6e 64 20 62 65 63 61 75 73 65 20 69  .  And because i
2b733 6e 64 69 63 65 73 20 6d 69 67 68 74 20 0a 2a 2a  ndices might .**
2b734 20 65 78 69 73 74 20 6f 6e 20 74 68 65 20 74 61   exist on the ta
2b735 62 6c 65 20 74 31 2c 20 61 20 63 6f 6d 70 6c 65  ble t1, a comple
2b736 74 65 20 73 63 61 6e 20 6f 66 20 74 68 65 20 64  te scan of the d
2b737 61 74 61 20 6d 69 67 68 74 20 62 65 0a 2a 2a 20  ata might be.** 
2b738 61 76 6f 69 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 46  avoided..**.** F
2b739 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 6f 6e 6c  lattening is onl
2b73a 79 20 61 74 74 65 6d 70 74 65 64 20 69 66 20 61  y attempted if a
2b73b 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ll of the follow
2b73c 69 6e 67 20 61 72 65 20 74 72 75 65 3a 0a 2a 2a  ing are true:.**
2b73d 0a 2a 2a 20 20 20 28 31 29 20 20 54 68 65 20 73  .**   (1)  The s
2b73e 75 62 71 75 65 72 79 20 61 6e 64 20 74 68 65 20  ubquery and the 
2b73f 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f 20 6e  outer query do n
2b740 6f 74 20 62 6f 74 68 20 75 73 65 20 61 67 67 72  ot both use aggr
2b741 65 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20  egates..**.**   
2b742 28 32 29 20 20 54 68 65 20 73 75 62 71 75 65 72  (2)  The subquer
2b743 79 20 69 73 20 6e 6f 74 20 61 6e 20 61 67 67 72  y is not an aggr
2b744 65 67 61 74 65 20 6f 72 20 74 68 65 20 6f 75 74  egate or the out
2b745 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f 74 20  er query is not 
2b746 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20  a join..**.**   
2b747 28 33 29 20 20 54 68 65 20 73 75 62 71 75 65 72  (3)  The subquer
2b748 79 20 69 73 20 6e 6f 74 20 74 68 65 20 72 69 67  y is not the rig
2b749 68 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20  ht operand of a 
2b74a 6c 65 66 74 20 6f 75 74 65 72 20 6a 6f 69 6e 0a  left outer join.
2b74b 2a 2a 20 20 20 20 20 20 20 20 28 4f 72 69 67 69  **        (Origi
2b74c 6e 61 6c 6c 79 20 74 69 63 6b 65 74 20 23 33 30  nally ticket #30
2b74d 36 2e 20 20 53 74 72 65 6e 67 68 74 65 6e 65 64  6.  Strenghtened
2b74e 20 62 79 20 74 69 63 6b 65 74 20 23 33 33 30 30   by ticket #3300
2b74f 29 0a 2a 2a 0a 2a 2a 20 20 20 28 34 29 20 20 54  ).**.**   (4)  T
2b750 68 65 20 73 75 62 71 75 65 72 79 20 69 73 20 6e  he subquery is n
2b751 6f 74 20 44 49 53 54 49 4e 43 54 20 6f 72 20 74  ot DISTINCT or t
2b752 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 69  he outer query i
2b753 73 20 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a 2a 2a  s not a join..**
2b754 0a 2a 2a 20 20 20 28 35 29 20 20 54 68 65 20 73  .**   (5)  The s
2b755 75 62 71 75 65 72 79 20 69 73 20 6e 6f 74 20 44  ubquery is not D
2b756 49 53 54 49 4e 43 54 20 6f 72 20 74 68 65 20 6f  ISTINCT or the o
2b757 75 74 65 72 20 71 75 65 72 79 20 64 6f 65 73 20  uter query does 
2b758 6e 6f 74 20 75 73 65 0a 2a 2a 20 20 20 20 20 20  not use.**      
2b759 20 20 61 67 67 72 65 67 61 74 65 73 2e 0a 2a 2a    aggregates..**
2b75a 0a 2a 2a 20 20 20 28 36 29 20 20 54 68 65 20 73  .**   (6)  The s
2b75b 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74  ubquery does not
2b75c 20 75 73 65 20 61 67 67 72 65 67 61 74 65 73 20   use aggregates 
2b75d 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  or the outer que
2b75e 72 79 20 69 73 20 6e 6f 74 0a 2a 2a 20 20 20 20  ry is not.**    
2b75f 20 20 20 20 44 49 53 54 49 4e 43 54 2e 0a 2a 2a      DISTINCT..**
2b760 0a 2a 2a 20 20 20 28 37 29 20 20 54 68 65 20 73  .**   (7)  The s
2b761 75 62 71 75 65 72 79 20 68 61 73 20 61 20 46 52  ubquery has a FR
2b762 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a  OM clause..**.**
2b763 20 20 20 28 38 29 20 20 54 68 65 20 73 75 62 71     (8)  The subq
2b764 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73  uery does not us
2b765 65 20 4c 49 4d 49 54 20 6f 72 20 74 68 65 20 6f  e LIMIT or the o
2b766 75 74 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f  uter query is no
2b767 74 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20  t a join..**.** 
2b768 20 20 28 39 29 20 20 54 68 65 20 73 75 62 71 75    (9)  The subqu
2b769 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65  ery does not use
2b76a 20 4c 49 4d 49 54 20 6f 72 20 74 68 65 20 6f 75   LIMIT or the ou
2b76b 74 65 72 20 71 75 65 72 79 20 64 6f 65 73 20 6e  ter query does n
2b76c 6f 74 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 20  ot use.**       
2b76d 20 61 67 67 72 65 67 61 74 65 73 2e 0a 2a 2a 0a   aggregates..**.
2b76e 2a 2a 20 20 28 31 30 29 20 20 54 68 65 20 73 75  **  (10)  The su
2b76f 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20  bquery does not 
2b770 75 73 65 20 61 67 67 72 65 67 61 74 65 73 20 6f  use aggregates o
2b771 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  r the outer quer
2b772 79 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 20 20  y does not.**   
2b773 20 20 20 20 20 75 73 65 20 4c 49 4d 49 54 2e 0a       use LIMIT..
2b774 2a 2a 0a 2a 2a 20 20 28 31 31 29 20 20 54 68 65  **.**  (11)  The
2b775 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 74 68   subquery and th
2b776 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f  e outer query do
2b777 20 6e 6f 74 20 62 6f 74 68 20 68 61 76 65 20 4f   not both have O
2b778 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 73 2e  RDER BY clauses.
2b779 0a 2a 2a 0a 2a 2a 20 20 28 31 32 29 20 20 4e 6f  .**.**  (12)  No
2b77a 74 20 69 6d 70 6c 65 6d 65 6e 74 65 64 2e 20 20  t implemented.  
2b77b 53 75 62 73 75 6d 65 64 20 69 6e 74 6f 20 72 65  Subsumed into re
2b77c 73 74 72 69 63 74 69 6f 6e 20 28 33 29 2e 20 20  striction (3).  
2b77d 57 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a  Was previously.*
2b77e 2a 20 20 20 20 20 20 20 20 61 20 73 65 70 61 72  *        a separ
2b77f 61 74 65 20 72 65 73 74 72 69 63 74 69 6f 6e 20  ate restriction 
2b780 64 65 72 69 76 69 6e 67 20 66 72 6f 6d 20 74 69  deriving from ti
2b781 63 6b 65 74 20 23 33 35 30 2e 0a 2a 2a 0a 2a 2a  cket #350..**.**
2b782 20 20 28 31 33 29 20 20 54 68 65 20 73 75 62 71    (13)  The subq
2b783 75 65 72 79 20 61 6e 64 20 6f 75 74 65 72 20 71  uery and outer q
2b784 75 65 72 79 20 64 6f 20 6e 6f 74 20 62 6f 74 68  uery do not both
2b785 20 75 73 65 20 4c 49 4d 49 54 0a 2a 2a 0a 2a 2a   use LIMIT.**.**
2b786 20 20 28 31 34 29 20 20 54 68 65 20 73 75 62 71    (14)  The subq
2b787 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73  uery does not us
2b788 65 20 4f 46 46 53 45 54 0a 2a 2a 0a 2a 2a 20 20  e OFFSET.**.**  
2b789 28 31 35 29 20 20 54 68 65 20 6f 75 74 65 72 20  (15)  The outer 
2b78a 71 75 65 72 79 20 69 73 20 6e 6f 74 20 70 61 72  query is not par
2b78b 74 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20  t of a compound 
2b78c 73 65 6c 65 63 74 20 6f 72 20 74 68 65 0a 2a 2a  select or the.**
2b78d 20 20 20 20 20 20 20 20 73 75 62 71 75 65 72 79          subquery
2b78e 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 62   does not have b
2b78f 6f 74 68 20 61 6e 20 4f 52 44 45 52 20 42 59 20  oth an ORDER BY 
2b790 61 6e 64 20 61 20 4c 49 4d 49 54 20 63 6c 61 75  and a LIMIT clau
2b791 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20 28 53  se..**        (S
2b792 65 65 20 74 69 63 6b 65 74 20 23 32 33 33 39 29  ee ticket #2339)
2b793 0a 2a 2a 0a 2a 2a 20 20 28 31 36 29 20 20 54 68  .**.**  (16)  Th
2b794 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 73  e outer query is
2b795 20 6e 6f 74 20 61 6e 20 61 67 67 72 65 67 61 74   not an aggregat
2b796 65 20 6f 72 20 74 68 65 20 73 75 62 71 75 65 72  e or the subquer
2b797 79 20 64 6f 65 73 0a 2a 2a 20 20 20 20 20 20 20  y does.**       
2b798 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 4f 52 44   not contain ORD
2b799 45 52 20 42 59 2e 20 20 28 54 69 63 6b 65 74 20  ER BY.  (Ticket 
2b79a 23 32 39 34 32 29 20 20 54 68 69 73 20 75 73 65  #2942)  This use
2b79b 64 20 74 6f 20 6e 6f 74 20 6d 61 74 74 65 72 0a  d to not matter.
2b79c 2a 2a 20 20 20 20 20 20 20 20 75 6e 74 69 6c 20  **        until 
2b79d 77 65 20 69 6e 74 72 6f 64 75 63 65 64 20 74 68  we introduced th
2b79e 65 20 67 72 6f 75 70 5f 63 6f 6e 63 61 74 28 29  e group_concat()
2b79f 20 66 75 6e 63 74 69 6f 6e 2e 20 20 0a 2a 2a 0a   function.  .**.
2b7a0 2a 2a 20 20 28 31 37 29 20 20 54 68 65 20 73 75  **  (17)  The su
2b7a1 62 2d 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61  b-query is not a
2b7a2 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74   compound select
2b7a3 2c 20 6f 72 20 69 74 20 69 73 20 61 20 55 4e 49  , or it is a UNI
2b7a4 4f 4e 20 41 4c 4c 20 0a 2a 2a 20 20 20 20 20 20  ON ALL .**      
2b7a5 20 20 63 6f 6d 70 6f 75 6e 64 20 63 6c 61 75 73    compound claus
2b7a6 65 20 6d 61 64 65 20 75 70 20 65 6e 74 69 72 65  e made up entire
2b7a7 6c 79 20 6f 66 20 6e 6f 6e 2d 61 67 67 72 65 67  ly of non-aggreg
2b7a8 61 74 65 20 71 75 65 72 69 65 73 2c 20 61 6e 64  ate queries, and
2b7a9 20 0a 2a 2a 20 20 20 20 20 20 20 20 74 68 65 20   .**        the 
2b7aa 70 61 72 65 6e 74 20 71 75 65 72 79 3a 0a 2a 2a  parent query:.**
2b7ab 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2a 20 69  .**          * i
2b7ac 73 20 6e 6f 74 20 69 74 73 65 6c 66 20 70 61 72  s not itself par
2b7ad 74 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20  t of a compound 
2b7ae 73 65 6c 65 63 74 2c 0a 2a 2a 20 20 20 20 20 20  select,.**      
2b7af 20 20 20 20 2a 20 69 73 20 6e 6f 74 20 61 6e 20      * is not an 
2b7b0 61 67 67 72 65 67 61 74 65 20 6f 72 20 44 49 53  aggregate or DIS
2b7b1 54 49 4e 43 54 20 71 75 65 72 79 2c 20 61 6e 64  TINCT query, and
2b7b2 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2a 20 68  .**          * h
2b7b3 61 73 20 6e 6f 20 6f 74 68 65 72 20 74 61 62 6c  as no other tabl
2b7b4 65 73 20 6f 72 20 73 75 62 2d 73 65 6c 65 63 74  es or sub-select
2b7b5 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  s in the FROM cl
2b7b6 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ause..**.**     
2b7b7 20 20 20 54 68 65 20 70 61 72 65 6e 74 20 61 6e     The parent an
2b7b8 64 20 73 75 62 2d 71 75 65 72 79 20 6d 61 79 20  d sub-query may 
2b7b9 63 6f 6e 74 61 69 6e 20 57 48 45 52 45 20 63 6c  contain WHERE cl
2b7ba 61 75 73 65 73 2e 20 53 75 62 6a 65 63 74 20 74  auses. Subject t
2b7bb 6f 0a 2a 2a 20 20 20 20 20 20 20 20 72 75 6c 65  o.**        rule
2b7bc 73 20 28 31 31 29 2c 20 28 31 33 29 20 61 6e 64  s (11), (13) and
2b7bd 20 28 31 34 29 2c 20 74 68 65 79 20 6d 61 79 20   (14), they may 
2b7be 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 20 4f 52 44  also contain ORD
2b7bf 45 52 20 42 59 2c 0a 2a 2a 20 20 20 20 20 20 20  ER BY,.**       
2b7c0 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45   LIMIT and OFFSE
2b7c1 54 20 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a  T clauses..**.**
2b7c2 20 20 28 31 38 29 20 20 49 66 20 74 68 65 20 73    (18)  If the s
2b7c3 75 62 2d 71 75 65 72 79 20 69 73 20 61 20 63 6f  ub-query is a co
2b7c4 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2c 20 74  mpound select, t
2b7c5 68 65 6e 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66  hen all terms of
2b7c6 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 4f   the.**        O
2b7c7 52 44 45 52 20 62 79 20 63 6c 61 75 73 65 20 6f  RDER by clause o
2b7c8 66 20 74 68 65 20 70 61 72 65 6e 74 20 6d 75 73  f the parent mus
2b7c9 74 20 62 65 20 73 69 6d 70 6c 65 20 72 65 66 65  t be simple refe
2b7ca 72 65 6e 63 65 73 20 74 6f 20 0a 2a 2a 20 20 20  rences to .**   
2b7cb 20 20 20 20 20 63 6f 6c 75 6d 6e 73 20 6f 66 20       columns of 
2b7cc 74 68 65 20 73 75 62 2d 71 75 65 72 79 2e 0a 2a  the sub-query..*
2b7cd 2a 0a 2a 2a 20 20 28 31 39 29 20 20 54 68 65 20  *.**  (19)  The 
2b7ce 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f  subquery does no
2b7cf 74 20 75 73 65 20 4c 49 4d 49 54 20 6f 72 20 74  t use LIMIT or t
2b7d0 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 64  he outer query d
2b7d1 6f 65 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20  oes not.**      
2b7d2 20 20 68 61 76 65 20 61 20 57 48 45 52 45 20 63    have a WHERE c
2b7d3 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 28 32  lause..**.**  (2
2b7d4 30 29 20 20 49 66 20 74 68 65 20 73 75 62 2d 71  0)  If the sub-q
2b7d5 75 65 72 79 20 69 73 20 61 20 63 6f 6d 70 6f 75  uery is a compou
2b7d6 6e 64 20 73 65 6c 65 63 74 2c 20 74 68 65 6e 20  nd select, then 
2b7d7 69 74 20 6d 75 73 74 20 6e 6f 74 20 75 73 65 0a  it must not use.
2b7d8 2a 2a 20 20 20 20 20 20 20 20 61 6e 20 4f 52 44  **        an ORD
2b7d9 45 52 20 42 59 20 63 6c 61 75 73 65 2e 20 20 54  ER BY clause.  T
2b7da 69 63 6b 65 74 20 23 33 37 37 33 2e 20 20 57 65  icket #3773.  We
2b7db 20 63 6f 75 6c 64 20 72 65 6c 61 78 20 74 68 69   could relax thi
2b7dc 73 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20  s constraint.** 
2b7dd 20 20 20 20 20 20 20 73 6f 6d 65 77 68 61 74 20         somewhat 
2b7de 62 79 20 73 61 79 69 6e 67 20 74 68 61 74 20 74  by saying that t
2b7df 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20  he terms of the 
2b7e0 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2b7e1 6d 75 73 74 0a 2a 2a 20 20 20 20 20 20 20 20 61  must.**        a
2b7e2 70 70 65 61 72 20 61 73 20 75 6e 6d 6f 64 69 66  ppear as unmodif
2b7e3 69 65 64 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d  ied result colum
2b7e4 6e 73 20 69 6e 20 74 68 65 20 6f 75 74 65 72 20  ns in the outer 
2b7e5 71 75 65 72 79 2e 20 20 42 75 74 0a 2a 2a 20 20  query.  But.**  
2b7e6 20 20 20 20 20 20 68 61 76 65 20 6f 74 68 65 72        have other
2b7e7 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 69   optimizations i
2b7e8 6e 20 6d 69 6e 64 20 74 6f 20 64 65 61 6c 20 77  n mind to deal w
2b7e9 69 74 68 20 74 68 61 74 20 63 61 73 65 2e 0a 2a  ith that case..*
2b7ea 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 72 6f 75  *.** In this rou
2b7eb 74 69 6e 65 2c 20 74 68 65 20 22 70 22 20 70 61  tine, the "p" pa
2b7ec 72 61 6d 65 74 65 72 20 69 73 20 61 20 70 6f 69  rameter is a poi
2b7ed 6e 74 65 72 20 74 6f 20 74 68 65 20 6f 75 74 65  nter to the oute
2b7ee 72 20 71 75 65 72 79 2e 0a 2a 2a 20 54 68 65 20  r query..** The 
2b7ef 73 75 62 71 75 65 72 79 20 69 73 20 70 2d 3e 70  subquery is p->p
2b7f0 53 72 63 2d 3e 61 5b 69 46 72 6f 6d 5d 2e 20 20  Src->a[iFrom].  
2b7f1 69 73 41 67 67 20 69 73 20 74 72 75 65 20 69 66  isAgg is true if
2b7f2 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79   the outer query
2b7f3 0a 2a 2a 20 75 73 65 73 20 61 67 67 72 65 67 61  .** uses aggrega
2b7f4 74 65 73 20 61 6e 64 20 73 75 62 71 75 65 72 79  tes and subquery
2b7f5 49 73 41 67 67 20 69 73 20 74 72 75 65 20 69 66  IsAgg is true if
2b7f6 20 74 68 65 20 73 75 62 71 75 65 72 79 20 75 73   the subquery us
2b7f7 65 73 20 61 67 67 72 65 67 61 74 65 73 2e 0a 2a  es aggregates..*
2b7f8 2a 0a 2a 2a 20 49 66 20 66 6c 61 74 74 65 6e 69  *.** If flatteni
2b7f9 6e 67 20 69 73 20 6e 6f 74 20 61 74 74 65 6d 70  ng is not attemp
2b7fa 74 65 64 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  ted, this routin
2b7fb 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 61 6e 64  e is a no-op and
2b7fc 20 72 65 74 75 72 6e 73 20 30 2e 0a 2a 2a 20 49   returns 0..** I
2b7fd 66 20 66 6c 61 74 74 65 6e 69 6e 67 20 69 73 20  f flattening is 
2b7fe 61 74 74 65 6d 70 74 65 64 20 74 68 69 73 20 72  attempted this r
2b7ff 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 31  outine returns 1
2b800 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74  ..**.** All of t
2b801 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e  he expression an
2b802 61 6c 79 73 69 73 20 6d 75 73 74 20 6f 63 63 75  alysis must occu
2b803 72 20 6f 6e 20 62 6f 74 68 20 74 68 65 20 6f 75  r on both the ou
2b804 74 65 72 20 71 75 65 72 79 20 61 6e 64 0a 2a 2a  ter query and.**
2b805 20 74 68 65 20 73 75 62 71 75 65 72 79 20 62 65   the subquery be
2b806 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74 69 6e  fore this routin
2b807 65 20 72 75 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69  e runs..*/.stati
2b808 63 20 69 6e 74 20 66 6c 61 74 74 65 6e 53 75 62  c int flattenSub
2b809 71 75 65 72 79 28 0a 20 20 50 61 72 73 65 20 2a  query(.  Parse *
2b80a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a  pParse,       /*
2b80b 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
2b80c 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c   */.  Select *p,
2b80d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2b80e 65 20 70 61 72 65 6e 74 20 6f 72 20 6f 75 74 65  e parent or oute
2b80f 72 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  r SELECT stateme
2b810 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 69 46 72 6f  nt */.  int iFro
2b811 6d 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  m,           /* 
2b812 49 6e 64 65 78 20 69 6e 20 70 2d 3e 70 53 72 63  Index in p->pSrc
2b813 2d 3e 61 5b 5d 20 6f 66 20 74 68 65 20 69 6e 6e  ->a[] of the inn
2b814 65 72 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 20  er subquery */. 
2b815 20 69 6e 74 20 69 73 41 67 67 2c 20 20 20 20 20   int isAgg,     
2b816 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
2b817 20 6f 75 74 65 72 20 53 45 4c 45 43 54 20 75 73   outer SELECT us
2b818 65 73 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  es aggregate fun
2b819 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20  ctions */.  int 
2b81a 73 75 62 71 75 65 72 79 49 73 41 67 67 20 20 20  subqueryIsAgg   
2b81b 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
2b81c 73 75 62 71 75 65 72 79 20 75 73 65 73 20 61 67  subquery uses ag
2b81d 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
2b81e 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20  s */.){.  const 
2b81f 63 68 61 72 20 2a 7a 53 61 76 65 64 41 75 74 68  char *zSavedAuth
2b820 43 6f 6e 74 65 78 74 20 3d 20 70 50 61 72 73 65  Context = pParse
2b821 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a  ->zAuthContext;.
2b822 20 20 53 65 6c 65 63 74 20 2a 70 50 61 72 65 6e    Select *pParen
2b823 74 3b 0a 20 20 53 65 6c 65 63 74 20 2a 70 53 75  t;.  Select *pSu
2b824 62 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  b;       /* The 
2b825 69 6e 6e 65 72 20 71 75 65 72 79 20 6f 72 20 22  inner query or "
2b826 73 75 62 71 75 65 72 79 22 20 2a 2f 0a 20 20 53  subquery" */.  S
2b827 65 6c 65 63 74 20 2a 70 53 75 62 31 3b 20 20 20  elect *pSub1;   
2b828 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f     /* Pointer to
2b829 20 74 68 65 20 72 69 67 68 74 6d 6f 73 74 20 73   the rightmost s
2b82a 65 6c 65 63 74 20 69 6e 20 73 75 62 2d 71 75 65  elect in sub-que
2b82b 72 79 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  ry */.  SrcList 
2b82c 2a 70 53 72 63 3b 20 20 20 20 20 20 2f 2a 20 54  *pSrc;      /* T
2b82d 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  he FROM clause o
2b82e 66 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  f the outer quer
2b82f 79 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  y */.  SrcList *
2b830 70 53 75 62 53 72 63 3b 20 20 20 2f 2a 20 54 68  pSubSrc;   /* Th
2b831 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  e FROM clause of
2b832 20 74 68 65 20 73 75 62 71 75 65 72 79 20 2a 2f   the subquery */
2b833 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  .  ExprList *pLi
2b834 73 74 3b 20 20 20 20 2f 2a 20 54 68 65 20 72 65  st;    /* The re
2b835 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20  sult set of the 
2b836 6f 75 74 65 72 20 71 75 65 72 79 20 2a 2f 0a 20  outer query */. 
2b837 20 69 6e 74 20 69 50 61 72 65 6e 74 3b 20 20 20   int iParent;   
2b838 20 20 20 20 20 2f 2a 20 56 44 42 45 20 63 75 72       /* VDBE cur
2b839 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68  sor number of th
2b83a 65 20 70 53 75 62 20 72 65 73 75 6c 74 20 73 65  e pSub result se
2b83b 74 20 74 65 6d 70 20 74 61 62 6c 65 20 2a 2f 0a  t temp table */.
2b83c 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
2b83d 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
2b83e 75 6e 74 65 72 20 2a 2f 0a 20 20 45 78 70 72 20  unter */.  Expr 
2b83f 2a 70 57 68 65 72 65 3b 20 20 20 20 20 20 20 20  *pWhere;        
2b840 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2b841 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2b842 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  */.  struct SrcL
2b843 69 73 74 5f 69 74 65 6d 20 2a 70 53 75 62 69 74  ist_item *pSubit
2b844 65 6d 3b 20 20 20 2f 2a 20 54 68 65 20 73 75 62  em;   /* The sub
2b845 71 75 65 72 79 20 2a 2f 0a 20 20 73 71 6c 69 74  query */.  sqlit
2b846 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
2b847 3e 64 62 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  >db;..  /* Check
2b848 20 74 6f 20 73 65 65 20 69 66 20 66 6c 61 74 74   to see if flatt
2b849 65 6e 69 6e 67 20 69 73 20 70 65 72 6d 69 74 74  ening is permitt
2b84a 65 64 2e 20 20 52 65 74 75 72 6e 20 30 20 69 66  ed.  Return 0 if
2b84b 20 6e 6f 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73   not..  */.  ass
2b84c 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61  ert( p!=0 );.  a
2b84d 73 73 65 72 74 28 20 70 2d 3e 70 50 72 69 6f 72  ssert( p->pPrior
2b84e 3d 3d 30 20 29 3b 20 20 2f 2a 20 55 6e 61 62 6c  ==0 );  /* Unabl
2b84f 65 20 74 6f 20 66 6c 61 74 74 65 6e 20 63 6f 6d  e to flatten com
2b850 70 6f 75 6e 64 20 71 75 65 72 69 65 73 20 2a 2f  pound queries */
2b851 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72  .  pSrc = p->pSr
2b852 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 72  c;.  assert( pSr
2b853 63 20 26 26 20 69 46 72 6f 6d 3e 3d 30 20 26 26  c && iFrom>=0 &&
2b854 20 69 46 72 6f 6d 3c 70 53 72 63 2d 3e 6e 53 72   iFrom<pSrc->nSr
2b855 63 20 29 3b 0a 20 20 70 53 75 62 69 74 65 6d 20  c );.  pSubitem 
2b856 3d 20 26 70 53 72 63 2d 3e 61 5b 69 46 72 6f 6d  = &pSrc->a[iFrom
2b857 5d 3b 0a 20 20 69 50 61 72 65 6e 74 20 3d 20 70  ];.  iParent = p
2b858 53 75 62 69 74 65 6d 2d 3e 69 43 75 72 73 6f 72  Subitem->iCursor
2b859 3b 0a 20 20 70 53 75 62 20 3d 20 70 53 75 62 69  ;.  pSub = pSubi
2b85a 74 65 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20  tem->pSelect;.  
2b85b 61 73 73 65 72 74 28 20 70 53 75 62 21 3d 30 20  assert( pSub!=0 
2b85c 29 3b 0a 20 20 69 66 28 20 69 73 41 67 67 20 26  );.  if( isAgg &
2b85d 26 20 73 75 62 71 75 65 72 79 49 73 41 67 67 20  & subqueryIsAgg 
2b85e 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20  ) return 0;     
2b85f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
2b860 65 73 74 72 69 63 74 69 6f 6e 20 28 31 29 20 20  estriction (1)  
2b861 2a 2f 0a 20 20 69 66 28 20 73 75 62 71 75 65 72  */.  if( subquer
2b862 79 49 73 41 67 67 20 26 26 20 70 53 72 63 2d 3e  yIsAgg && pSrc->
2b863 6e 53 72 63 3e 31 20 29 20 72 65 74 75 72 6e 20  nSrc>1 ) return 
2b864 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52  0;          /* R
2b865 65 73 74 72 69 63 74 69 6f 6e 20 28 32 29 20 20  estriction (2)  
2b866 2a 2f 0a 20 20 70 53 75 62 53 72 63 20 3d 20 70  */.  pSubSrc = p
2b867 53 75 62 2d 3e 70 53 72 63 3b 0a 20 20 61 73 73  Sub->pSrc;.  ass
2b868 65 72 74 28 20 70 53 75 62 53 72 63 20 29 3b 0a  ert( pSubSrc );.
2b869 20 20 2f 2a 20 50 72 69 6f 72 20 74 6f 20 76 65    /* Prior to ve
2b86a 72 73 69 6f 6e 20 33 2e 31 2e 32 2c 20 77 68 65  rsion 3.1.2, whe
2b86b 6e 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53  n LIMIT and OFFS
2b86c 45 54 20 68 61 64 20 74 6f 20 62 65 20 73 69 6d  ET had to be sim
2b86d 70 6c 65 20 63 6f 6e 73 74 61 6e 74 73 2c 0a 20  ple constants,. 
2b86e 20 2a 2a 20 6e 6f 74 20 61 72 62 69 74 72 61 72   ** not arbitrar
2b86f 79 20 65 78 70 72 65 73 73 73 69 6f 6e 73 2c 20  y expresssions, 
2b870 77 65 20 61 6c 6c 6f 77 65 64 20 73 6f 6d 65 20  we allowed some 
2b871 63 6f 6d 62 69 6e 69 6e 67 20 6f 66 20 4c 49 4d  combining of LIM
2b872 49 54 20 61 6e 64 20 4f 46 46 53 45 54 0a 20 20  IT and OFFSET.  
2b873 2a 2a 20 62 65 63 61 75 73 65 20 74 68 65 79 20  ** because they 
2b874 63 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65  could be compute
2b875 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  d at compile-tim
2b876 65 2e 20 20 42 75 74 20 77 68 65 6e 20 4c 49 4d  e.  But when LIM
2b877 49 54 20 61 6e 64 20 4f 46 46 53 45 54 0a 20 20  IT and OFFSET.  
2b878 2a 2a 20 62 65 63 61 6d 65 20 61 72 62 69 74 72  ** became arbitr
2b879 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e 73 2c  ary expressions,
2b87a 20 77 65 20 77 65 72 65 20 66 6f 72 63 65 64 20   we were forced 
2b87b 74 6f 20 61 64 64 20 72 65 73 74 72 69 63 74 69  to add restricti
2b87c 6f 6e 73 20 28 31 33 29 0a 20 20 2a 2a 20 61 6e  ons (13).  ** an
2b87d 64 20 28 31 34 29 2e 20 2a 2f 0a 20 20 69 66 28  d (14). */.  if(
2b87e 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20 26 26   pSub->pLimit &&
2b87f 20 70 2d 3e 70 4c 69 6d 69 74 20 29 20 72 65 74   p->pLimit ) ret
2b880 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20  urn 0;          
2b881 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69      /* Restricti
2b882 6f 6e 20 28 31 33 29 20 2a 2f 0a 20 20 69 66 28  on (13) */.  if(
2b883 20 70 53 75 62 2d 3e 70 4f 66 66 73 65 74 20 29   pSub->pOffset )
2b884 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20   return 0;      
2b885 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b886 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69      /* Restricti
2b887 6f 6e 20 28 31 34 29 20 2a 2f 0a 20 20 69 66 28  on (14) */.  if(
2b888 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 26   p->pRightmost &
2b889 26 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20 26  & pSub->pLimit &
2b88a 26 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79  & pSub->pOrderBy
2b88b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
2b88c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2b88d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b88e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2b88f 52 65 73 74 72 69 63 74 69 6f 6e 20 28 31 35 29  Restriction (15)
2b890 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 53   */.  }.  if( pS
2b891 75 62 53 72 63 2d 3e 6e 53 72 63 3d 3d 30 20 29  ubSrc->nSrc==0 )
2b892 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20   return 0;      
2b893 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b894 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20   /* Restriction 
2b895 28 37 29 20 20 2a 2f 0a 20 20 69 66 28 20 28 28  (7)  */.  if( ((
2b896 70 53 75 62 2d 3e 73 65 6c 46 6c 61 67 73 20 26  pSub->selFlags &
2b897 20 53 46 5f 44 69 73 74 69 6e 63 74 29 21 3d 30   SF_Distinct)!=0
2b898 20 7c 7c 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74   || pSub->pLimit
2b899 29 20 0a 20 20 20 20 20 20 20 20 20 26 26 20 28  ) .         && (
2b89a 70 53 72 63 2d 3e 6e 53 72 63 3e 31 20 7c 7c 20  pSrc->nSrc>1 || 
2b89b 69 73 41 67 67 29 20 29 7b 20 20 20 20 20 20 20  isAgg) ){       
2b89c 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f     /* Restrictio
2b89d 6e 73 20 28 34 29 28 35 29 28 38 29 28 39 29 20  ns (4)(5)(8)(9) 
2b89e 2a 2f 0a 20 20 20 20 20 72 65 74 75 72 6e 20 30  */.     return 0
2b89f 3b 20 20 20 20 20 20 20 0a 20 20 7d 0a 20 20 69  ;       .  }.  i
2b8a0 66 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20  f( (p->selFlags 
2b8a1 26 20 53 46 5f 44 69 73 74 69 6e 63 74 29 21 3d  & SF_Distinct)!=
2b8a2 30 20 26 26 20 73 75 62 71 75 65 72 79 49 73 41  0 && subqueryIsA
2b8a3 67 67 20 29 7b 0a 20 20 20 20 20 72 65 74 75 72  gg ){.     retur
2b8a4 6e 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  n 0;         /* 
2b8a5 52 65 73 74 72 69 63 74 69 6f 6e 20 28 36 29 20  Restriction (6) 
2b8a6 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d   */.  }.  if( p-
2b8a7 3e 70 4f 72 64 65 72 42 79 20 26 26 20 70 53 75  >pOrderBy && pSu
2b8a8 62 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  b->pOrderBy ){. 
2b8a9 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
2b8aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b8ab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b8ac 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72          /* Restr
2b8ad 69 63 74 69 6f 6e 20 28 31 31 29 20 2a 2f 0a 20  iction (11) */. 
2b8ae 20 7d 0a 20 20 69 66 28 20 69 73 41 67 67 20 26   }.  if( isAgg &
2b8af 26 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79  & pSub->pOrderBy
2b8b0 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
2b8b1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
2b8b2 65 73 74 72 69 63 74 69 6f 6e 20 28 31 36 29 20  estriction (16) 
2b8b3 2a 2f 0a 20 20 69 66 28 20 70 53 75 62 2d 3e 70  */.  if( pSub->p
2b8b4 4c 69 6d 69 74 20 26 26 20 70 2d 3e 70 57 68 65  Limit && p->pWhe
2b8b5 72 65 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20  re ) return 0;  
2b8b6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
2b8b7 65 73 74 72 69 63 74 69 6f 6e 20 28 31 39 29 20  estriction (19) 
2b8b8 2a 2f 0a 0a 20 20 2f 2a 20 4f 42 53 4f 4c 45 54  */..  /* OBSOLET
2b8b9 45 20 43 4f 4d 4d 45 4e 54 20 31 3a 0a 20 20 2a  E COMMENT 1:.  *
2b8ba 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 33 3a  * Restriction 3:
2b8bb 20 20 49 66 20 74 68 65 20 73 75 62 71 75 65 72    If the subquer
2b8bc 79 20 69 73 20 61 20 6a 6f 69 6e 2c 20 6d 61 6b  y is a join, mak
2b8bd 65 20 73 75 72 65 20 74 68 65 20 73 75 62 71 75  e sure the subqu
2b8be 65 72 79 20 69 73 20 0a 20 20 2a 2a 20 6e 6f 74  ery is .  ** not
2b8bf 20 75 73 65 64 20 61 73 20 74 68 65 20 72 69 67   used as the rig
2b8c0 68 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 6e  ht operand of an
2b8c1 20 6f 75 74 65 72 20 6a 6f 69 6e 2e 20 20 45 78   outer join.  Ex
2b8c2 61 6d 70 6c 65 73 20 6f 66 20 77 68 79 20 74 68  amples of why th
2b8c3 69 73 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 61  is.  ** is not a
2b8c4 6c 6c 6f 77 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a  llowed:.  **.  *
2b8c5 2a 20 20 20 20 20 20 20 20 20 74 31 20 4c 45 46  *         t1 LEF
2b8c6 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 28 74 32  T OUTER JOIN (t2
2b8c7 20 4a 4f 49 4e 20 74 33 29 0a 20 20 2a 2a 0a 20   JOIN t3).  **. 
2b8c8 20 2a 2a 20 49 66 20 77 65 20 66 6c 61 74 74 65   ** If we flatte
2b8c9 6e 20 74 68 65 20 61 62 6f 76 65 2c 20 77 65 20  n the above, we 
2b8ca 77 6f 75 6c 64 20 67 65 74 0a 20 20 2a 2a 0a 20  would get.  **. 
2b8cb 20 2a 2a 20 20 20 20 20 20 20 20 20 28 74 31 20   **         (t1 
2b8cc 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20  LEFT OUTER JOIN 
2b8cd 74 32 29 20 4a 4f 49 4e 20 74 33 0a 20 20 2a 2a  t2) JOIN t3.  **
2b8ce 0a 20 20 2a 2a 20 77 68 69 63 68 20 69 73 20 6e  .  ** which is n
2b8cf 6f 74 20 61 74 20 61 6c 6c 20 74 68 65 20 73 61  ot at all the sa
2b8d0 6d 65 20 74 68 69 6e 67 2e 0a 20 20 2a 2a 0a 20  me thing..  **. 
2b8d1 20 2a 2a 20 4f 42 53 4f 4c 45 54 45 20 43 4f 4d   ** OBSOLETE COM
2b8d2 4d 45 4e 54 20 32 3a 0a 20 20 2a 2a 20 52 65 73  MENT 2:.  ** Res
2b8d3 74 72 69 63 74 69 6f 6e 20 31 32 3a 20 20 49 66  triction 12:  If
2b8d4 20 74 68 65 20 73 75 62 71 75 65 72 79 20 69 73   the subquery is
2b8d5 20 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 61   the right opera
2b8d6 6e 64 20 6f 66 20 61 20 6c 65 66 74 20 6f 75 74  nd of a left out
2b8d7 65 72 0a 20 20 2a 2a 20 6a 6f 69 6e 2c 20 6d 61  er.  ** join, ma
2b8d8 6b 65 20 73 75 72 65 20 74 68 65 20 73 75 62 71  ke sure the subq
2b8d9 75 65 72 79 20 68 61 73 20 6e 6f 20 57 48 45 52  uery has no WHER
2b8da 45 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 20 41  E clause..  ** A
2b8db 6e 20 65 78 61 6d 70 6c 65 73 20 6f 66 20 77 68  n examples of wh
2b8dc 79 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 6c  y this is not al
2b8dd 6c 6f 77 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  lowed:.  **.  **
2b8de 20 20 20 20 20 20 20 20 20 74 31 20 4c 45 46 54           t1 LEFT
2b8df 20 4f 55 54 45 52 20 4a 4f 49 4e 20 28 53 45 4c   OUTER JOIN (SEL
2b8e0 45 43 54 20 2a 20 46 52 4f 4d 20 74 32 20 57 48  ECT * FROM t2 WH
2b8e1 45 52 45 20 74 32 2e 78 3e 30 29 0a 20 20 2a 2a  ERE t2.x>0).  **
2b8e2 0a 20 20 2a 2a 20 49 66 20 77 65 20 66 6c 61 74  .  ** If we flat
2b8e3 74 65 6e 20 74 68 65 20 61 62 6f 76 65 2c 20 77  ten the above, w
2b8e4 65 20 77 6f 75 6c 64 20 67 65 74 0a 20 20 2a 2a  e would get.  **
2b8e5 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 28 74  .  **         (t
2b8e6 31 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49  1 LEFT OUTER JOI
2b8e7 4e 20 74 32 29 20 57 48 45 52 45 20 74 32 2e 78  N t2) WHERE t2.x
2b8e8 3e 30 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42 75 74  >0.  **.  ** But
2b8e9 20 74 68 65 20 74 32 2e 78 3e 30 20 74 65 73 74   the t2.x>0 test
2b8ea 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 66 61 69   will always fai
2b8eb 6c 20 6f 6e 20 61 20 4e 55 4c 4c 20 72 6f 77 20  l on a NULL row 
2b8ec 6f 66 20 74 32 2c 20 77 68 69 63 68 0a 20 20 2a  of t2, which.  *
2b8ed 2a 20 65 66 66 65 63 74 69 76 65 6c 79 20 63 6f  * effectively co
2b8ee 6e 76 65 72 74 73 20 74 68 65 20 4f 55 54 45 52  nverts the OUTER
2b8ef 20 4a 4f 49 4e 20 69 6e 74 6f 20 61 6e 20 49 4e   JOIN into an IN
2b8f0 4e 45 52 20 4a 4f 49 4e 2e 0a 20 20 2a 2a 0a 20  NER JOIN..  **. 
2b8f1 20 2a 2a 20 54 48 49 53 20 4f 56 45 52 52 49 44   ** THIS OVERRID
2b8f2 45 53 20 4f 42 53 4f 4c 45 54 45 20 43 4f 4d 4d  ES OBSOLETE COMM
2b8f3 45 4e 54 53 20 31 20 41 4e 44 20 32 20 41 42 4f  ENTS 1 AND 2 ABO
2b8f4 56 45 3a 0a 20 20 2a 2a 20 54 69 63 6b 65 74 20  VE:.  ** Ticket 
2b8f5 23 33 33 30 30 20 73 68 6f 77 73 20 74 68 61 74  #3300 shows that
2b8f6 20 66 6c 61 74 74 65 6e 69 6e 67 20 74 68 65 20   flattening the 
2b8f7 72 69 67 68 74 20 74 65 72 6d 20 6f 66 20 61 20  right term of a 
2b8f8 4c 45 46 54 20 4a 4f 49 4e 0a 20 20 2a 2a 20 69  LEFT JOIN.  ** i
2b8f9 73 20 66 72 61 75 67 68 74 20 77 69 74 68 20 64  s fraught with d
2b8fa 61 6e 67 65 72 2e 20 20 42 65 73 74 20 74 6f 20  anger.  Best to 
2b8fb 61 76 6f 69 64 20 74 68 65 20 77 68 6f 6c 65 20  avoid the whole 
2b8fc 74 68 69 6e 67 2e 20 20 49 66 20 74 68 65 0a 20  thing.  If the. 
2b8fd 20 2a 2a 20 73 75 62 71 75 65 72 79 20 69 73 20   ** subquery is 
2b8fe 74 68 65 20 72 69 67 68 74 20 74 65 72 6d 20 6f  the right term o
2b8ff 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 2c 20 74  f a LEFT JOIN, t
2b900 68 65 6e 20 64 6f 20 6e 6f 74 20 66 6c 61 74 74  hen do not flatt
2b901 65 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  en..  */.  if( (
2b902 70 53 75 62 69 74 65 6d 2d 3e 6a 6f 69 6e 74 79  pSubitem->jointy
2b903 70 65 20 26 20 4a 54 5f 4f 55 54 45 52 29 21 3d  pe & JT_OUTER)!=
2b904 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
2b905 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 73  0;.  }..  /* Res
2b906 74 72 69 63 74 69 6f 6e 20 31 37 3a 20 49 66 20  triction 17: If 
2b907 74 68 65 20 73 75 62 2d 71 75 65 72 79 20 69 73  the sub-query is
2b908 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45   a compound SELE
2b909 43 54 2c 20 74 68 65 6e 20 69 74 20 6d 75 73 74  CT, then it must
2b90a 0a 20 20 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74  .  ** use only t
2b90b 68 65 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 70 65  he UNION ALL ope
2b90c 72 61 74 6f 72 2e 20 41 6e 64 20 6e 6f 6e 65 20  rator. And none 
2b90d 6f 66 20 74 68 65 20 73 69 6d 70 6c 65 20 73 65  of the simple se
2b90e 6c 65 63 74 20 71 75 65 72 69 65 73 0a 20 20 2a  lect queries.  *
2b90f 2a 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 74  * that make up t
2b910 68 65 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45  he compound SELE
2b911 43 54 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74  CT are allowed t
2b912 6f 20 62 65 20 61 67 67 72 65 67 61 74 65 20 6f  o be aggregate o
2b913 72 20 64 69 73 74 69 6e 63 74 0a 20 20 2a 2a 20  r distinct.  ** 
2b914 71 75 65 72 69 65 73 2e 0a 20 20 2a 2f 0a 20 20  queries..  */.  
2b915 69 66 28 20 70 53 75 62 2d 3e 70 50 72 69 6f 72  if( pSub->pPrior
2b916 20 29 7b 0a 20 20 20 20 69 66 28 20 70 53 75 62   ){.    if( pSub
2b917 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ->pOrderBy ){.  
2b918 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 2f      return 0;  /
2b919 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 32 30  * Restriction 20
2b91a 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   */.    }.    if
2b91b 28 20 69 73 41 67 67 20 7c 7c 20 28 70 2d 3e 73  ( isAgg || (p->s
2b91c 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69 73  elFlags & SF_Dis
2b91d 74 69 6e 63 74 29 21 3d 30 20 7c 7c 20 70 53 72  tinct)!=0 || pSr
2b91e 63 2d 3e 6e 53 72 63 21 3d 31 20 29 7b 0a 20 20  c->nSrc!=1 ){.  
2b91f 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
2b920 20 20 7d 0a 20 20 20 20 66 6f 72 28 70 53 75 62    }.    for(pSub
2b921 31 3d 70 53 75 62 3b 20 70 53 75 62 31 3b 20 70  1=pSub; pSub1; p
2b922 53 75 62 31 3d 70 53 75 62 31 2d 3e 70 50 72 69  Sub1=pSub1->pPri
2b923 6f 72 29 7b 0a 20 20 20 20 20 20 74 65 73 74 63  or){.      testc
2b924 61 73 65 28 20 28 70 53 75 62 31 2d 3e 73 65 6c  ase( (pSub1->sel
2b925 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 74  Flags & (SF_Dist
2b926 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74  inct|SF_Aggregat
2b927 65 29 29 3d 3d 53 46 5f 44 69 73 74 69 6e 63 74  e))==SF_Distinct
2b928 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
2b929 73 65 28 20 28 70 53 75 62 31 2d 3e 73 65 6c 46  se( (pSub1->selF
2b92a 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 69  lags & (SF_Disti
2b92b 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65  nct|SF_Aggregate
2b92c 29 29 3d 3d 53 46 5f 41 67 67 72 65 67 61 74 65  ))==SF_Aggregate
2b92d 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28 70   );.      if( (p
2b92e 53 75 62 31 2d 3e 73 65 6c 46 6c 61 67 73 20 26  Sub1->selFlags &
2b92f 20 28 53 46 5f 44 69 73 74 69 6e 63 74 7c 53 46   (SF_Distinct|SF
2b930 5f 41 67 67 72 65 67 61 74 65 29 29 21 3d 30 0a  _Aggregate))!=0.
2b931 20 20 20 20 20 20 20 7c 7c 20 28 70 53 75 62 31         || (pSub1
2b932 2d 3e 70 50 72 69 6f 72 20 26 26 20 70 53 75 62  ->pPrior && pSub
2b933 31 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 29 20 0a  1->op!=TK_ALL) .
2b934 20 20 20 20 20 20 20 7c 7c 20 4e 45 56 45 52 28         || NEVER(
2b935 70 53 75 62 31 2d 3e 70 53 72 63 3d 3d 30 29 20  pSub1->pSrc==0) 
2b936 7c 7c 20 70 53 75 62 31 2d 3e 70 53 72 63 2d 3e  || pSub1->pSrc->
2b937 6e 53 72 63 21 3d 31 0a 20 20 20 20 20 20 29 7b  nSrc!=1.      ){
2b938 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
2b939 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
2b93a 0a 0a 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63  ..    /* Restric
2b93b 74 69 6f 6e 20 31 38 2e 20 2a 2f 0a 20 20 20 20  tion 18. */.    
2b93c 69 66 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 20  if( p->pOrderBy 
2b93d 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 69 3b  ){.      int ii;
2b93e 0a 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b  .      for(ii=0;
2b93f 20 69 69 3c 70 2d 3e 70 4f 72 64 65 72 42 79 2d   ii<p->pOrderBy-
2b940 3e 6e 45 78 70 72 3b 20 69 69 2b 2b 29 7b 0a 20  >nExpr; ii++){. 
2b941 20 20 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4f         if( p->pO
2b942 72 64 65 72 42 79 2d 3e 61 5b 69 69 5d 2e 69 43  rderBy->a[ii].iC
2b943 6f 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  ol==0 ) return 0
2b944 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2b945 20 20 7d 0a 0a 20 20 2f 2a 2a 2a 2a 2a 20 49 66    }..  /***** If
2b946 20 77 65 20 72 65 61 63 68 20 74 68 69 73 20 70   we reach this p
2b947 6f 69 6e 74 2c 20 66 6c 61 74 74 65 6e 69 6e 67  oint, flattening
2b948 20 69 73 20 70 65 72 6d 69 74 74 65 64 2e 20 2a   is permitted. *
2b949 2a 2a 2a 2a 2f 0a 0a 20 20 2f 2a 20 41 75 74 68  ****/..  /* Auth
2b94a 6f 72 69 7a 65 20 74 68 65 20 73 75 62 71 75 65  orize the subque
2b94b 72 79 20 2a 2f 0a 20 20 70 50 61 72 73 65 2d 3e  ry */.  pParse->
2b94c 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70  zAuthContext = p
2b94d 53 75 62 69 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a  Subitem->zName;.
2b94e 20 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65    sqlite3AuthChe
2b94f 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ck(pParse, SQLIT
2b950 45 5f 53 45 4c 45 43 54 2c 20 30 2c 20 30 2c 20  E_SELECT, 0, 0, 
2b951 30 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 7a 41  0);.  pParse->zA
2b952 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a 53 61  uthContext = zSa
2b953 76 65 64 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a  vedAuthContext;.
2b954 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 75 62  .  /* If the sub
2b955 2d 71 75 65 72 79 20 69 73 20 61 20 63 6f 6d 70  -query is a comp
2b956 6f 75 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74  ound SELECT stat
2b957 65 6d 65 6e 74 2c 20 74 68 65 6e 20 28 62 79 20  ement, then (by 
2b958 72 65 73 74 72 69 63 74 69 6f 6e 73 0a 20 20 2a  restrictions.  *
2b959 2a 20 31 37 20 61 6e 64 20 31 38 20 61 62 6f 76  * 17 and 18 abov
2b95a 65 29 20 69 74 20 6d 75 73 74 20 62 65 20 61 20  e) it must be a 
2b95b 55 4e 49 4f 4e 20 41 4c 4c 20 61 6e 64 20 74 68  UNION ALL and th
2b95c 65 20 70 61 72 65 6e 74 20 71 75 65 72 79 20 6d  e parent query m
2b95d 75 73 74 20 0a 20 20 2a 2a 20 62 65 20 6f 66 20  ust .  ** be of 
2b95e 74 68 65 20 66 6f 72 6d 3a 0a 20 20 2a 2a 0a 20  the form:.  **. 
2b95f 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 3c   **     SELECT <
2b960 65 78 70 72 2d 6c 69 73 74 3e 20 46 52 4f 4d 20  expr-list> FROM 
2b961 28 3c 73 75 62 2d 71 75 65 72 79 3e 29 20 3c 77  (<sub-query>) <w
2b962 68 65 72 65 2d 63 6c 61 75 73 65 3e 20 0a 20 20  here-clause> .  
2b963 2a 2a 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 65 64  **.  ** followed
2b964 20 62 79 20 61 6e 79 20 4f 52 44 45 52 20 42 59   by any ORDER BY
2b965 2c 20 4c 49 4d 49 54 20 61 6e 64 2f 6f 72 20 4f  , LIMIT and/or O
2b966 46 46 53 45 54 20 63 6c 61 75 73 65 73 2e 20 54  FFSET clauses. T
2b967 68 69 73 20 62 6c 6f 63 6b 0a 20 20 2a 2a 20 63  his block.  ** c
2b968 72 65 61 74 65 73 20 4e 2d 31 20 63 6f 70 69 65  reates N-1 copie
2b969 73 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20  s of the parent 
2b96a 71 75 65 72 79 20 77 69 74 68 6f 75 74 20 61 6e  query without an
2b96b 79 20 4f 52 44 45 52 20 42 59 2c 20 4c 49 4d 49  y ORDER BY, LIMI
2b96c 54 20 6f 72 20 0a 20 20 2a 2a 20 4f 46 46 53 45  T or .  ** OFFSE
2b96d 54 20 63 6c 61 75 73 65 73 20 61 6e 64 20 6a 6f  T clauses and jo
2b96e 69 6e 73 20 74 68 65 6d 20 74 6f 20 74 68 65 20  ins them to the 
2b96f 6c 65 66 74 2d 68 61 6e 64 2d 73 69 64 65 20 6f  left-hand-side o
2b970 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 20  f the original. 
2b971 20 2a 2a 20 75 73 69 6e 67 20 55 4e 49 4f 4e 20   ** using UNION 
2b972 41 4c 4c 20 6f 70 65 72 61 74 6f 72 73 2e 20 49  ALL operators. I
2b973 6e 20 74 68 69 73 20 63 61 73 65 20 4e 20 69 73  n this case N is
2b974 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73   the number of s
2b975 69 6d 70 6c 65 0a 20 20 2a 2a 20 73 65 6c 65 63  imple.  ** selec
2b976 74 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20  t statements in 
2b977 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 75 62  the compound sub
2b978 2d 71 75 65 72 79 2e 0a 20 20 2a 2a 0a 20 20 2a  -query..  **.  *
2b979 2a 20 45 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a 0a  * Example:.  **.
2b97a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20    **     SELECT 
2b97b 61 2b 31 20 46 52 4f 4d 20 28 0a 20 20 2a 2a 20  a+1 FROM (.  ** 
2b97c 20 20 20 20 20 20 20 53 45 4c 45 43 54 20 78 20         SELECT x 
2b97d 46 52 4f 4d 20 74 61 62 0a 20 20 2a 2a 20 20 20  FROM tab.  **   
2b97e 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20       UNION ALL. 
2b97f 20 2a 2a 20 20 20 20 20 20 20 20 53 45 4c 45 43   **        SELEC
2b980 54 20 79 20 46 52 4f 4d 20 74 61 62 0a 20 20 2a  T y FROM tab.  *
2b981 2a 20 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 41  *        UNION A
2b982 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 53  LL.  **        S
2b983 45 4c 45 43 54 20 61 62 73 28 7a 2a 32 29 20 46  ELECT abs(z*2) F
2b984 52 4f 4d 20 74 61 62 32 0a 20 20 2a 2a 20 20 20  ROM tab2.  **   
2b985 20 20 29 20 57 48 45 52 45 20 61 21 3d 35 20 4f    ) WHERE a!=5 O
2b986 52 44 45 52 20 42 59 20 31 0a 20 20 2a 2a 0a 20  RDER BY 1.  **. 
2b987 20 2a 2a 20 54 72 61 6e 73 66 6f 72 6d 65 64 20   ** Transformed 
2b988 69 6e 74 6f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  into:.  **.  ** 
2b989 20 20 20 20 53 45 4c 45 43 54 20 78 2b 31 20 46      SELECT x+1 F
2b98a 52 4f 4d 20 74 61 62 20 57 48 45 52 45 20 78 2b  ROM tab WHERE x+
2b98b 31 21 3d 35 0a 20 20 2a 2a 20 20 20 20 20 55 4e  1!=5.  **     UN
2b98c 49 4f 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20  ION ALL.  **    
2b98d 20 53 45 4c 45 43 54 20 79 2b 31 20 46 52 4f 4d   SELECT y+1 FROM
2b98e 20 74 61 62 20 57 48 45 52 45 20 79 2b 31 21 3d   tab WHERE y+1!=
2b98f 35 0a 20 20 2a 2a 20 20 20 20 20 55 4e 49 4f 4e  5.  **     UNION
2b990 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 53 45   ALL.  **     SE
2b991 4c 45 43 54 20 61 62 73 28 7a 2a 32 29 2b 31 20  LECT abs(z*2)+1 
2b992 46 52 4f 4d 20 74 61 62 32 20 57 48 45 52 45 20  FROM tab2 WHERE 
2b993 61 62 73 28 7a 2a 32 29 2b 31 21 3d 35 0a 20 20  abs(z*2)+1!=5.  
2b994 2a 2a 20 20 20 20 20 4f 52 44 45 52 20 42 59 20  **     ORDER BY 
2b995 31 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 63  1.  **.  ** We c
2b996 61 6c 6c 20 74 68 69 73 20 74 68 65 20 22 63 6f  all this the "co
2b997 6d 70 6f 75 6e 64 2d 73 75 62 71 75 65 72 79 20  mpound-subquery 
2b998 66 6c 61 74 74 65 6e 69 6e 67 22 2e 0a 20 20 2a  flattening"..  *
2b999 2f 0a 20 20 66 6f 72 28 70 53 75 62 3d 70 53 75  /.  for(pSub=pSu
2b99a 62 2d 3e 70 50 72 69 6f 72 3b 20 70 53 75 62 3b  b->pPrior; pSub;
2b99b 20 70 53 75 62 3d 70 53 75 62 2d 3e 70 50 72 69   pSub=pSub->pPri
2b99c 6f 72 29 7b 0a 20 20 20 20 53 65 6c 65 63 74 20  or){.    Select 
2b99d 2a 70 4e 65 77 3b 0a 20 20 20 20 45 78 70 72 4c  *pNew;.    ExprL
2b99e 69 73 74 20 2a 70 4f 72 64 65 72 42 79 20 3d 20  ist *pOrderBy = 
2b99f 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 20  p->pOrderBy;.   
2b9a0 20 45 78 70 72 20 2a 70 4c 69 6d 69 74 20 3d 20   Expr *pLimit = 
2b9a1 70 2d 3e 70 4c 69 6d 69 74 3b 0a 20 20 20 20 53  p->pLimit;.    S
2b9a2 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 20 3d 20  elect *pPrior = 
2b9a3 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 70  p->pPrior;.    p
2b9a4 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a  ->pOrderBy = 0;.
2b9a5 20 20 20 20 70 2d 3e 70 53 72 63 20 3d 20 30 3b      p->pSrc = 0;
2b9a6 0a 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d  .    p->pPrior =
2b9a7 20 30 3b 0a 20 20 20 20 70 2d 3e 70 4c 69 6d 69   0;.    p->pLimi
2b9a8 74 20 3d 20 30 3b 0a 20 20 20 20 70 4e 65 77 20  t = 0;.    pNew 
2b9a9 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  = sqlite3SelectD
2b9aa 75 70 28 64 62 2c 20 70 2c 20 30 29 3b 0a 20 20  up(db, p, 0);.  
2b9ab 20 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 70 4c    p->pLimit = pL
2b9ac 69 6d 69 74 3b 0a 20 20 20 20 70 2d 3e 70 4f 72  imit;.    p->pOr
2b9ad 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79  derBy = pOrderBy
2b9ae 3b 0a 20 20 20 20 70 2d 3e 70 53 72 63 20 3d 20  ;.    p->pSrc = 
2b9af 70 53 72 63 3b 0a 20 20 20 20 70 2d 3e 6f 70 20  pSrc;.    p->op 
2b9b0 3d 20 54 4b 5f 41 4c 4c 3b 0a 20 20 20 20 70 2d  = TK_ALL;.    p-
2b9b1 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 30 3b  >pRightmost = 0;
2b9b2 0a 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30  .    if( pNew==0
2b9b3 20 29 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d   ){.      pNew =
2b9b4 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 7d 65 6c   pPrior;.    }el
2b9b5 73 65 7b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e  se{.      pNew->
2b9b6 70 50 72 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b  pPrior = pPrior;
2b9b7 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 52 69  .      pNew->pRi
2b9b8 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0a 20 20 20  ghtmost = 0;.   
2b9b9 20 7d 0a 20 20 20 20 70 2d 3e 70 50 72 69 6f 72   }.    p->pPrior
2b9ba 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 69 66 28   = pNew;.    if(
2b9bb 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2b9bc 64 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  d ) return 1;.  
2b9bd 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 66 6c  }..  /* Begin fl
2b9be 61 74 74 65 6e 69 6e 67 20 74 68 65 20 69 46 72  attening the iFr
2b9bf 6f 6d 2d 74 68 20 65 6e 74 72 79 20 6f 66 20 74  om-th entry of t
2b9c0 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 0a  he FROM clause .
2b9c1 20 20 2a 2a 20 69 6e 20 74 68 65 20 6f 75 74 65    ** in the oute
2b9c2 72 20 71 75 65 72 79 2e 0a 20 20 2a 2f 0a 20 20  r query..  */.  
2b9c3 70 53 75 62 20 3d 20 70 53 75 62 31 20 3d 20 70  pSub = pSub1 = p
2b9c4 53 75 62 69 74 65 6d 2d 3e 70 53 65 6c 65 63 74  Subitem->pSelect
2b9c5 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 74  ;..  /* Delete t
2b9c6 68 65 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62  he transient tab
2b9c7 6c 65 20 73 74 72 75 63 74 75 72 65 20 61 73 73  le structure ass
2b9c8 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
2b9c9 0a 20 20 2a 2a 20 73 75 62 71 75 65 72 79 0a 20  .  ** subquery. 
2b9ca 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 44 62 46   */.  sqlite3DbF
2b9cb 72 65 65 28 64 62 2c 20 70 53 75 62 69 74 65 6d  ree(db, pSubitem
2b9cc 2d 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20  ->zDatabase);.  
2b9cd 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2b9ce 2c 20 70 53 75 62 69 74 65 6d 2d 3e 7a 4e 61 6d  , pSubitem->zNam
2b9cf 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  e);.  sqlite3DbF
2b9d0 72 65 65 28 64 62 2c 20 70 53 75 62 69 74 65 6d  ree(db, pSubitem
2b9d1 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20 70 53 75  ->zAlias);.  pSu
2b9d2 62 69 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65  bitem->zDatabase
2b9d3 20 3d 20 30 3b 0a 20 20 70 53 75 62 69 74 65 6d   = 0;.  pSubitem
2b9d4 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20 70  ->zName = 0;.  p
2b9d5 53 75 62 69 74 65 6d 2d 3e 7a 41 6c 69 61 73 20  Subitem->zAlias 
2b9d6 3d 20 30 3b 0a 20 20 70 53 75 62 69 74 65 6d 2d  = 0;.  pSubitem-
2b9d7 3e 70 53 65 6c 65 63 74 20 3d 20 30 3b 0a 0a 20  >pSelect = 0;.. 
2b9d8 20 2f 2a 20 44 65 66 65 72 20 64 65 6c 65 74 69   /* Defer deleti
2b9d9 6e 67 20 74 68 65 20 54 61 62 6c 65 20 6f 62 6a  ng the Table obj
2b9da 65 63 74 20 61 73 73 6f 63 69 61 74 65 64 20 77  ect associated w
2b9db 69 74 68 20 74 68 65 0a 20 20 2a 2a 20 73 75 62  ith the.  ** sub
2b9dc 71 75 65 72 79 20 75 6e 74 69 6c 20 63 6f 64 65  query until code
2b9dd 20 67 65 6e 65 72 61 74 69 6f 6e 20 69 73 0a 20   generation is. 
2b9de 20 2a 2a 20 63 6f 6d 70 6c 65 74 65 2c 20 73 69   ** complete, si
2b9df 6e 63 65 20 74 68 65 72 65 20 6d 61 79 20 73 74  nce there may st
2b9e0 69 6c 6c 20 65 78 69 73 74 20 45 78 70 72 2e 70  ill exist Expr.p
2b9e1 54 61 62 20 65 6e 74 72 69 65 73 20 74 68 61 74  Tab entries that
2b9e2 0a 20 20 2a 2a 20 72 65 66 65 72 20 74 6f 20 74  .  ** refer to t
2b9e3 68 65 20 73 75 62 71 75 65 72 79 20 65 76 65 6e  he subquery even
2b9e4 20 61 66 74 65 72 20 66 6c 61 74 74 65 6e 69 6e   after flattenin
2b9e5 67 2e 20 20 54 69 63 6b 65 74 20 23 33 33 34 36  g.  Ticket #3346
2b9e6 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 70 53 75 62  ..  **.  ** pSub
2b9e7 69 74 65 6d 2d 3e 70 54 61 62 20 69 73 20 61 6c  item->pTab is al
2b9e8 77 61 79 73 20 6e 6f 6e 2d 4e 55 4c 4c 20 62 79  ways non-NULL by
2b9e9 20 74 65 73 74 20 72 65 73 74 72 69 63 74 69 6f   test restrictio
2b9ea 6e 73 20 61 6e 64 20 74 65 73 74 73 20 61 62 6f  ns and tests abo
2b9eb 76 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 41  ve..  */.  if( A
2b9ec 4c 57 41 59 53 28 70 53 75 62 69 74 65 6d 2d 3e  LWAYS(pSubitem->
2b9ed 70 54 61 62 21 3d 30 29 20 29 7b 0a 20 20 20 20  pTab!=0) ){.    
2b9ee 54 61 62 6c 65 20 2a 70 54 61 62 54 6f 44 65 6c  Table *pTabToDel
2b9ef 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 70 54 61   = pSubitem->pTa
2b9f0 62 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 54  b;.    if( pTabT
2b9f1 6f 44 65 6c 2d 3e 6e 52 65 66 3d 3d 31 20 29 7b  oDel->nRef==1 ){
2b9f2 0a 20 20 20 20 20 20 50 61 72 73 65 20 2a 70 54  .      Parse *pT
2b9f3 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69 74 65  oplevel = sqlite
2b9f4 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70  3ParseToplevel(p
2b9f5 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 70 54  Parse);.      pT
2b9f6 61 62 54 6f 44 65 6c 2d 3e 70 4e 65 78 74 5a 6f  abToDel->pNextZo
2b9f7 6d 62 69 65 20 3d 20 70 54 6f 70 6c 65 76 65 6c  mbie = pToplevel
2b9f8 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 3b 0a 20 20  ->pZombieTab;.  
2b9f9 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 70      pToplevel->p
2b9fa 5a 6f 6d 62 69 65 54 61 62 20 3d 20 70 54 61 62  ZombieTab = pTab
2b9fb 54 6f 44 65 6c 3b 0a 20 20 20 20 7d 65 6c 73 65  ToDel;.    }else
2b9fc 7b 0a 20 20 20 20 20 20 70 54 61 62 54 6f 44 65  {.      pTabToDe
2b9fd 6c 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 7d  l->nRef--;.    }
2b9fe 0a 20 20 20 20 70 53 75 62 69 74 65 6d 2d 3e 70  .    pSubitem->p
2b9ff 54 61 62 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  Tab = 0;.  }..  
2ba00 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  /* The following
2ba01 20 6c 6f 6f 70 20 72 75 6e 73 20 6f 6e 63 65 20   loop runs once 
2ba02 66 6f 72 20 65 61 63 68 20 74 65 72 6d 20 69 6e  for each term in
2ba03 20 61 20 63 6f 6d 70 6f 75 6e 64 2d 73 75 62 71   a compound-subq
2ba04 75 65 72 79 0a 20 20 2a 2a 20 66 6c 61 74 74 65  uery.  ** flatte
2ba05 6e 69 6e 67 20 28 61 73 20 64 65 73 63 72 69 62  ning (as describ
2ba06 65 64 20 61 62 6f 76 65 29 2e 20 20 49 66 20 77  ed above).  If w
2ba07 65 20 61 72 65 20 64 6f 69 6e 67 20 61 20 64 69  e are doing a di
2ba08 66 66 65 72 65 6e 74 20 6b 69 6e 64 0a 20 20 2a  fferent kind.  *
2ba09 2a 20 6f 66 20 66 6c 61 74 74 65 6e 69 6e 67 20  * of flattening 
2ba0a 2d 20 61 20 66 6c 61 74 74 65 6e 69 6e 67 20 6f  - a flattening o
2ba0b 74 68 65 72 20 74 68 61 6e 20 61 20 63 6f 6d 70  ther than a comp
2ba0c 6f 75 6e 64 2d 73 75 62 71 75 65 72 79 20 66 6c  ound-subquery fl
2ba0d 61 74 74 65 6e 69 6e 67 20 2d 0a 20 20 2a 2a 20  attening -.  ** 
2ba0e 74 68 65 6e 20 74 68 69 73 20 6c 6f 6f 70 20 6f  then this loop o
2ba0f 6e 6c 79 20 72 75 6e 73 20 6f 6e 63 65 2e 0a 20  nly runs once.. 
2ba10 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20 6c 6f   **.  ** This lo
2ba11 6f 70 20 6d 6f 76 65 73 20 61 6c 6c 20 6f 66 20  op moves all of 
2ba12 74 68 65 20 46 52 4f 4d 20 65 6c 65 6d 65 6e 74  the FROM element
2ba13 73 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 72  s of the subquer
2ba14 79 20 69 6e 74 6f 20 74 68 65 0a 20 20 2a 2a 20  y into the.  ** 
2ba15 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
2ba16 6f 66 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  of the outer que
2ba17 72 79 2e 20 20 42 65 66 6f 72 65 20 64 6f 69 6e  ry.  Before doin
2ba18 67 20 74 68 69 73 2c 20 72 65 6d 65 6d 62 65 72  g this, remember
2ba19 0a 20 20 2a 2a 20 74 68 65 20 63 75 72 73 6f 72  .  ** the cursor
2ba1a 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
2ba1b 6f 72 69 67 69 6e 61 6c 20 6f 75 74 65 72 20 71  original outer q
2ba1c 75 65 72 79 20 46 52 4f 4d 20 65 6c 65 6d 65 6e  uery FROM elemen
2ba1d 74 20 69 6e 0a 20 20 2a 2a 20 69 50 61 72 65 6e  t in.  ** iParen
2ba1e 74 2e 20 20 54 68 65 20 69 50 61 72 65 6e 74 20  t.  The iParent 
2ba1f 63 75 72 73 6f 72 20 77 69 6c 6c 20 6e 65 76 65  cursor will neve
2ba20 72 20 62 65 20 75 73 65 64 2e 20 20 53 75 62 73  r be used.  Subs
2ba21 65 71 75 65 6e 74 20 63 6f 64 65 0a 20 20 2a 2a  equent code.  **
2ba22 20 77 69 6c 6c 20 73 63 61 6e 20 65 78 70 72 65   will scan expre
2ba23 73 73 69 6f 6e 73 20 6c 6f 6f 6b 69 6e 67 20 66  ssions looking f
2ba24 6f 72 20 69 50 61 72 65 6e 74 20 72 65 66 65 72  or iParent refer
2ba25 65 6e 63 65 73 20 61 6e 64 20 72 65 70 6c 61 63  ences and replac
2ba26 65 0a 20 20 2a 2a 20 74 68 6f 73 65 20 72 65 66  e.  ** those ref
2ba27 65 72 65 6e 63 65 73 20 77 69 74 68 20 65 78 70  erences with exp
2ba28 72 65 73 73 69 6f 6e 73 20 74 68 61 74 20 72 65  ressions that re
2ba29 73 6f 6c 76 65 20 74 6f 20 74 68 65 20 73 75 62  solve to the sub
2ba2a 71 75 65 72 79 20 46 52 4f 4d 0a 20 20 2a 2a 20  query FROM.  ** 
2ba2b 65 6c 65 6d 65 6e 74 73 20 77 65 20 61 72 65 20  elements we are 
2ba2c 6e 6f 77 20 63 6f 70 79 69 6e 67 20 69 6e 2e 0a  now copying in..
2ba2d 20 20 2a 2f 0a 20 20 66 6f 72 28 70 50 61 72 65    */.  for(pPare
2ba2e 6e 74 3d 70 3b 20 70 50 61 72 65 6e 74 3b 20 70  nt=p; pParent; p
2ba2f 50 61 72 65 6e 74 3d 70 50 61 72 65 6e 74 2d 3e  Parent=pParent->
2ba30 70 50 72 69 6f 72 2c 20 70 53 75 62 3d 70 53 75  pPrior, pSub=pSu
2ba31 62 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20 20 20  b->pPrior){.    
2ba32 69 6e 74 20 6e 53 75 62 53 72 63 3b 0a 20 20 20  int nSubSrc;.   
2ba33 20 75 38 20 6a 6f 69 6e 74 79 70 65 20 3d 20 30   u8 jointype = 0
2ba34 3b 0a 20 20 20 20 70 53 75 62 53 72 63 20 3d 20  ;.    pSubSrc = 
2ba35 70 53 75 62 2d 3e 70 53 72 63 3b 20 20 20 20 20  pSub->pSrc;     
2ba36 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  /* FROM clause o
2ba37 66 20 73 75 62 71 75 65 72 79 20 2a 2f 0a 20 20  f subquery */.  
2ba38 20 20 6e 53 75 62 53 72 63 20 3d 20 70 53 75 62    nSubSrc = pSub
2ba39 53 72 63 2d 3e 6e 53 72 63 3b 20 20 2f 2a 20 4e  Src->nSrc;  /* N
2ba3a 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 69  umber of terms i
2ba3b 6e 20 73 75 62 71 75 65 72 79 20 46 52 4f 4d 20  n subquery FROM 
2ba3c 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 70 53  clause */.    pS
2ba3d 72 63 20 3d 20 70 50 61 72 65 6e 74 2d 3e 70 53  rc = pParent->pS
2ba3e 72 63 3b 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20  rc;     /* FROM 
2ba3f 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 6f 75  clause of the ou
2ba40 74 65 72 20 71 75 65 72 79 20 2a 2f 0a 0a 20 20  ter query */..  
2ba41 20 20 69 66 28 20 70 53 72 63 20 29 7b 0a 20 20    if( pSrc ){.  
2ba42 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72      assert( pPar
2ba43 65 6e 74 3d 3d 70 20 29 3b 20 20 2f 2a 20 46 69  ent==p );  /* Fi
2ba44 72 73 74 20 74 69 6d 65 20 74 68 72 6f 75 67 68  rst time through
2ba45 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20   the loop */.   
2ba46 20 20 20 6a 6f 69 6e 74 79 70 65 20 3d 20 70 53     jointype = pS
2ba47 75 62 69 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70 65  ubitem->jointype
2ba48 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2ba49 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65     assert( pPare
2ba4a 6e 74 21 3d 70 20 29 3b 20 20 2f 2a 20 32 6e 64  nt!=p );  /* 2nd
2ba4b 20 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20   and subsequent 
2ba4c 74 69 6d 65 73 20 74 68 72 6f 75 67 68 20 74 68  times through th
2ba4d 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 20 20 20 20  e loop */.      
2ba4e 70 53 72 63 20 3d 20 70 50 61 72 65 6e 74 2d 3e  pSrc = pParent->
2ba4f 70 53 72 63 20 3d 20 73 71 6c 69 74 65 33 53 72  pSrc = sqlite3Sr
2ba50 63 4c 69 73 74 41 70 70 65 6e 64 28 64 62 2c 20  cListAppend(db, 
2ba51 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  0, 0, 0);.      
2ba52 69 66 28 20 70 53 72 63 3d 3d 30 20 29 7b 0a 20  if( pSrc==0 ){. 
2ba53 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64         assert( d
2ba54 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2ba55 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
2ba56 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2ba57 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 75 62 71  .    /* The subq
2ba58 75 65 72 79 20 75 73 65 73 20 61 20 73 69 6e 67  uery uses a sing
2ba59 6c 65 20 73 6c 6f 74 20 6f 66 20 74 68 65 20 46  le slot of the F
2ba5a 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68  ROM clause of th
2ba5b 65 20 6f 75 74 65 72 0a 20 20 20 20 2a 2a 20 71  e outer.    ** q
2ba5c 75 65 72 79 2e 20 20 49 66 20 74 68 65 20 73 75  uery.  If the su
2ba5d 62 71 75 65 72 79 20 68 61 73 20 6d 6f 72 65 20  bquery has more 
2ba5e 74 68 61 6e 20 6f 6e 65 20 65 6c 65 6d 65 6e 74  than one element
2ba5f 20 69 6e 20 69 74 73 20 46 52 4f 4d 20 63 6c 61   in its FROM cla
2ba60 75 73 65 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e  use,.    ** then
2ba61 20 65 78 70 61 6e 64 20 74 68 65 20 6f 75 74 65   expand the oute
2ba62 72 20 71 75 65 72 79 20 74 6f 20 6d 61 6b 65 20  r query to make 
2ba63 73 70 61 63 65 20 66 6f 72 20 69 74 20 74 6f 20  space for it to 
2ba64 68 6f 6c 64 20 61 6c 6c 20 65 6c 65 6d 65 6e 74  hold all element
2ba65 73 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20  s.    ** of the 
2ba66 73 75 62 71 75 65 72 79 2e 0a 20 20 20 20 2a 2a  subquery..    **
2ba67 0a 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a  .    ** Example:
2ba68 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
2ba69 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20    SELECT * FROM 
2ba6a 74 61 62 41 2c 20 28 53 45 4c 45 43 54 20 2a 20  tabA, (SELECT * 
2ba6b 46 52 4f 4d 20 73 75 62 31 2c 20 73 75 62 32 29  FROM sub1, sub2)
2ba6c 2c 20 74 61 62 42 3b 0a 20 20 20 20 2a 2a 0a 20  , tabB;.    **. 
2ba6d 20 20 20 2a 2a 20 54 68 65 20 6f 75 74 65 72 20     ** The outer 
2ba6e 71 75 65 72 79 20 68 61 73 20 33 20 73 6c 6f 74  query has 3 slot
2ba6f 73 20 69 6e 20 69 74 73 20 46 52 4f 4d 20 63 6c  s in its FROM cl
2ba70 61 75 73 65 2e 20 20 4f 6e 65 20 73 6c 6f 74 20  ause.  One slot 
2ba71 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f 75  of the.    ** ou
2ba72 74 65 72 20 71 75 65 72 79 20 28 74 68 65 20 6d  ter query (the m
2ba73 69 64 64 6c 65 20 73 6c 6f 74 29 20 69 73 20 75  iddle slot) is u
2ba74 73 65 64 20 62 79 20 74 68 65 20 73 75 62 71 75  sed by the subqu
2ba75 65 72 79 2e 20 20 54 68 65 20 6e 65 78 74 0a 20  ery.  The next. 
2ba76 20 20 20 2a 2a 20 62 6c 6f 63 6b 20 6f 66 20 63     ** block of c
2ba77 6f 64 65 20 77 69 6c 6c 20 65 78 70 61 6e 64 20  ode will expand 
2ba78 74 68 65 20 6f 75 74 20 71 75 65 72 79 20 74 6f  the out query to
2ba79 20 34 20 73 6c 6f 74 73 2e 20 20 54 68 65 20 6d   4 slots.  The m
2ba7a 69 64 64 6c 65 0a 20 20 20 20 2a 2a 20 73 6c 6f  iddle.    ** slo
2ba7b 74 20 69 73 20 65 78 70 61 6e 64 65 64 20 74 6f  t is expanded to
2ba7c 20 74 77 6f 20 73 6c 6f 74 73 20 69 6e 20 6f 72   two slots in or
2ba7d 64 65 72 20 74 6f 20 6d 61 6b 65 20 73 70 61 63  der to make spac
2ba7e 65 20 66 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a  e for the.    **
2ba7f 20 74 77 6f 20 65 6c 65 6d 65 6e 74 73 20 69 6e   two elements in
2ba80 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2ba81 20 6f 66 20 74 68 65 20 73 75 62 71 75 65 72 79   of the subquery
2ba82 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
2ba83 20 6e 53 75 62 53 72 63 3e 31 20 29 7b 0a 20 20   nSubSrc>1 ){.  
2ba84 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 53 72      pParent->pSr
2ba85 63 20 3d 20 70 53 72 63 20 3d 20 73 71 6c 69 74  c = pSrc = sqlit
2ba86 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65  e3SrcListEnlarge
2ba87 28 64 62 2c 20 70 53 72 63 2c 20 6e 53 75 62 53  (db, pSrc, nSubS
2ba88 72 63 2d 31 2c 69 46 72 6f 6d 2b 31 29 3b 0a 20  rc-1,iFrom+1);. 
2ba89 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c       if( db->mal
2ba8a 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
2ba8b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2ba8c 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
2ba8d 2a 20 54 72 61 6e 73 66 65 72 20 74 68 65 20 46  * Transfer the F
2ba8e 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 73  ROM clause terms
2ba8f 20 66 72 6f 6d 20 74 68 65 20 73 75 62 71 75 65   from the subque
2ba90 72 79 20 69 6e 74 6f 20 74 68 65 0a 20 20 20 20  ry into the.    
2ba91 2a 2a 20 6f 75 74 65 72 20 71 75 65 72 79 2e 0a  ** outer query..
2ba92 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69      */.    for(i
2ba93 3d 30 3b 20 69 3c 6e 53 75 62 53 72 63 3b 20 69  =0; i<nSubSrc; i
2ba94 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
2ba95 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64  e3IdListDelete(d
2ba96 62 2c 20 70 53 72 63 2d 3e 61 5b 69 2b 69 46 72  b, pSrc->a[i+iFr
2ba97 6f 6d 5d 2e 70 55 73 69 6e 67 29 3b 0a 20 20 20  om].pUsing);.   
2ba98 20 20 20 70 53 72 63 2d 3e 61 5b 69 2b 69 46 72     pSrc->a[i+iFr
2ba99 6f 6d 5d 20 3d 20 70 53 75 62 53 72 63 2d 3e 61  om] = pSubSrc->a
2ba9a 5b 69 5d 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65  [i];.      memse
2ba9b 74 28 26 70 53 75 62 53 72 63 2d 3e 61 5b 69 5d  t(&pSubSrc->a[i]
2ba9c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 53 75 62  , 0, sizeof(pSub
2ba9d 53 72 63 2d 3e 61 5b 69 5d 29 29 3b 0a 20 20 20  Src->a[i]));.   
2ba9e 20 7d 0a 20 20 20 20 70 53 72 63 2d 3e 61 5b 69   }.    pSrc->a[i
2ba9f 46 72 6f 6d 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d  From].jointype =
2baa0 20 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 0a 20 20   jointype;.  .  
2baa1 20 20 2f 2a 20 4e 6f 77 20 62 65 67 69 6e 20 73    /* Now begin s
2baa2 75 62 73 74 69 74 75 74 69 6e 67 20 73 75 62 71  ubstituting subq
2baa3 75 65 72 79 20 72 65 73 75 6c 74 20 73 65 74 20  uery result set 
2baa4 65 78 70 72 65 73 73 69 6f 6e 73 20 66 6f 72 20  expressions for 
2baa5 0a 20 20 20 20 2a 2a 20 72 65 66 65 72 65 6e 63  .    ** referenc
2baa6 65 73 20 74 6f 20 74 68 65 20 69 50 61 72 65 6e  es to the iParen
2baa7 74 20 69 6e 20 74 68 65 20 6f 75 74 65 72 20 71  t in the outer q
2baa8 75 65 72 79 2e 0a 20 20 20 20 2a 2a 20 0a 20 20  uery..    ** .  
2baa9 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 20 20    ** Example:.  
2baaa 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 53 45    **.    **   SE
2baab 4c 45 43 54 20 61 2b 35 2c 20 62 2a 31 30 20 46  LECT a+5, b*10 F
2baac 52 4f 4d 20 28 53 45 4c 45 43 54 20 78 2a 33 20  ROM (SELECT x*3 
2baad 41 53 20 61 2c 20 79 2b 31 30 20 41 53 20 62 20  AS a, y+10 AS b 
2baae 46 52 4f 4d 20 74 31 29 20 57 48 45 52 45 20 61  FROM t1) WHERE a
2baaf 3e 62 3b 0a 20 20 20 20 2a 2a 20 20 20 5c 20 20  >b;.    **   \  
2bab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bab1 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f     \____________
2bab2 5f 20 73 75 62 71 75 65 72 79 20 5f 5f 5f 5f 5f  _ subquery _____
2bab3 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 20 20 20  _____/          
2bab4 2f 0a 20 20 20 20 2a 2a 20 20 20 20 5c 5f 5f 5f  /.    **    \___
2bab5 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
2bab6 5f 5f 20 6f 75 74 65 72 20 71 75 65 72 79 20 5f  __ outer query _
2bab7 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
2bab8 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 20  _____________/. 
2bab9 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 65 20     **.    ** We 
2baba 6c 6f 6f 6b 20 61 74 20 65 76 65 72 79 20 65 78  look at every ex
2babb 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20  pression in the 
2babc 6f 75 74 65 72 20 71 75 65 72 79 20 61 6e 64 20  outer query and 
2babd 65 76 65 72 79 20 70 6c 61 63 65 20 77 65 20 73  every place we s
2babe 65 65 0a 20 20 20 20 2a 2a 20 22 61 22 20 77 65  ee.    ** "a" we
2babf 20 73 75 62 73 74 69 74 75 74 65 20 22 78 2a 33   substitute "x*3
2bac0 22 20 61 6e 64 20 65 76 65 72 79 20 70 6c 61 63  " and every plac
2bac1 65 20 77 65 20 73 65 65 20 22 62 22 20 77 65 20  e we see "b" we 
2bac2 73 75 62 73 74 69 74 75 74 65 20 22 79 2b 31 30  substitute "y+10
2bac3 22 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4c  "..    */.    pL
2bac4 69 73 74 20 3d 20 70 50 61 72 65 6e 74 2d 3e 70  ist = pParent->p
2bac5 45 4c 69 73 74 3b 0a 20 20 20 20 66 6f 72 28 69  EList;.    for(i
2bac6 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78  =0; i<pList->nEx
2bac7 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; i++){.      
2bac8 69 66 28 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  if( pList->a[i].
2bac9 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20  zName==0 ){.    
2baca 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2bacb 7a 53 70 61 6e 20 3d 20 70 4c 69 73 74 2d 3e 61  zSpan = pList->a
2bacc 5b 69 5d 2e 7a 53 70 61 6e 3b 0a 20 20 20 20 20  [i].zSpan;.     
2bacd 20 20 20 69 66 28 20 41 4c 57 41 59 53 28 7a 53     if( ALWAYS(zS
2bace 70 61 6e 29 20 29 7b 0a 20 20 20 20 20 20 20 20  pan) ){.        
2bacf 20 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e    pList->a[i].zN
2bad0 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53  ame = sqlite3DbS
2bad1 74 72 44 75 70 28 64 62 2c 20 7a 53 70 61 6e 29  trDup(db, zSpan)
2bad2 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2bad3 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 75    }.    }.    su
2bad4 62 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20  bstExprList(db, 
2bad5 70 50 61 72 65 6e 74 2d 3e 70 45 4c 69 73 74 2c  pParent->pEList,
2bad6 20 69 50 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e   iParent, pSub->
2bad7 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 69 66 28  pEList);.    if(
2bad8 20 69 73 41 67 67 20 29 7b 0a 20 20 20 20 20 20   isAgg ){.      
2bad9 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 64 62  substExprList(db
2bada 2c 20 70 50 61 72 65 6e 74 2d 3e 70 47 72 6f 75  , pParent->pGrou
2badb 70 42 79 2c 20 69 50 61 72 65 6e 74 2c 20 70 53  pBy, iParent, pS
2badc 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20  ub->pEList);.   
2badd 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76     pParent->pHav
2bade 69 6e 67 20 3d 20 73 75 62 73 74 45 78 70 72 28  ing = substExpr(
2badf 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 48 61  db, pParent->pHa
2bae0 76 69 6e 67 2c 20 69 50 61 72 65 6e 74 2c 20 70  ving, iParent, p
2bae1 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20  Sub->pEList);.  
2bae2 20 20 7d 0a 20 20 20 20 69 66 28 20 70 53 75 62    }.    if( pSub
2bae3 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ->pOrderBy ){.  
2bae4 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72      assert( pPar
2bae5 65 6e 74 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30  ent->pOrderBy==0
2bae6 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e   );.      pParen
2bae7 74 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 70 53  t->pOrderBy = pS
2bae8 75 62 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20  ub->pOrderBy;.  
2bae9 20 20 20 20 70 53 75 62 2d 3e 70 4f 72 64 65 72      pSub->pOrder
2baea 42 79 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  By = 0;.    }els
2baeb 65 20 69 66 28 20 70 50 61 72 65 6e 74 2d 3e 70  e if( pParent->p
2baec 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20  OrderBy ){.     
2baed 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 64   substExprList(d
2baee 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 4f 72 64  b, pParent->pOrd
2baef 65 72 42 79 2c 20 69 50 61 72 65 6e 74 2c 20 70  erBy, iParent, p
2baf0 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20  Sub->pEList);.  
2baf1 20 20 7d 0a 20 20 20 20 69 66 28 20 70 53 75 62    }.    if( pSub
2baf2 2d 3e 70 57 68 65 72 65 20 29 7b 0a 20 20 20 20  ->pWhere ){.    
2baf3 20 20 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74    pWhere = sqlit
2baf4 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 53  e3ExprDup(db, pS
2baf5 75 62 2d 3e 70 57 68 65 72 65 2c 20 30 29 3b 0a  ub->pWhere, 0);.
2baf6 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2baf7 20 70 57 68 65 72 65 20 3d 20 30 3b 0a 20 20 20   pWhere = 0;.   
2baf8 20 7d 0a 20 20 20 20 69 66 28 20 73 75 62 71 75   }.    if( subqu
2baf9 65 72 79 49 73 41 67 67 20 29 7b 0a 20 20 20 20  eryIsAgg ){.    
2bafa 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e    assert( pParen
2bafb 74 2d 3e 70 48 61 76 69 6e 67 3d 3d 30 20 29 3b  t->pHaving==0 );
2bafc 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e  .      pParent->
2bafd 70 48 61 76 69 6e 67 20 3d 20 70 50 61 72 65 6e  pHaving = pParen
2bafe 74 2d 3e 70 57 68 65 72 65 3b 0a 20 20 20 20 20  t->pWhere;.     
2baff 20 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65   pParent->pWhere
2bb00 20 3d 20 70 57 68 65 72 65 3b 0a 20 20 20 20 20   = pWhere;.     
2bb01 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e   pParent->pHavin
2bb02 67 20 3d 20 73 75 62 73 74 45 78 70 72 28 64 62  g = substExpr(db
2bb03 2c 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76 69  , pParent->pHavi
2bb04 6e 67 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 75  ng, iParent, pSu
2bb05 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  b->pEList);.    
2bb06 20 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76 69    pParent->pHavi
2bb07 6e 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ng = sqlite3Expr
2bb08 41 6e 64 28 64 62 2c 20 70 50 61 72 65 6e 74 2d  And(db, pParent-
2bb09 3e 70 48 61 76 69 6e 67 2c 20 0a 20 20 20 20 20  >pHaving, .     
2bb0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bb0b 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2bb0c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20  ite3ExprDup(db, 
2bb0d 70 53 75 62 2d 3e 70 48 61 76 69 6e 67 2c 20 30  pSub->pHaving, 0
2bb0e 29 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ));.      assert
2bb0f 28 20 70 50 61 72 65 6e 74 2d 3e 70 47 72 6f 75  ( pParent->pGrou
2bb10 70 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  pBy==0 );.      
2bb11 70 50 61 72 65 6e 74 2d 3e 70 47 72 6f 75 70 42  pParent->pGroupB
2bb12 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  y = sqlite3ExprL
2bb13 69 73 74 44 75 70 28 64 62 2c 20 70 53 75 62 2d  istDup(db, pSub-
2bb14 3e 70 47 72 6f 75 70 42 79 2c 20 30 29 3b 0a 20  >pGroupBy, 0);. 
2bb15 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2bb16 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 20  pParent->pWhere 
2bb17 3d 20 73 75 62 73 74 45 78 70 72 28 64 62 2c 20  = substExpr(db, 
2bb18 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 2c  pParent->pWhere,
2bb19 20 69 50 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e   iParent, pSub->
2bb1a 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 70  pEList);.      p
2bb1b 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 20 3d  Parent->pWhere =
2bb1c 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28   sqlite3ExprAnd(
2bb1d 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 57 68  db, pParent->pWh
2bb1e 65 72 65 2c 20 70 57 68 65 72 65 29 3b 0a 20 20  ere, pWhere);.  
2bb1f 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 54 68    }.  .    /* Th
2bb20 65 20 66 6c 61 74 74 65 6e 65 64 20 71 75 65 72  e flattened quer
2bb21 79 20 69 73 20 64 69 73 74 69 6e 63 74 20 69 66  y is distinct if
2bb22 20 65 69 74 68 65 72 20 74 68 65 20 69 6e 6e 65   either the inne
2bb23 72 20 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20  r or the.    ** 
2bb24 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20 64  outer query is d
2bb25 69 73 74 69 6e 63 74 2e 20 0a 20 20 20 20 2a 2f  istinct. .    */
2bb26 0a 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 73 65  .    pParent->se
2bb27 6c 46 6c 61 67 73 20 7c 3d 20 70 53 75 62 2d 3e  lFlags |= pSub->
2bb28 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69  selFlags & SF_Di
2bb29 73 74 69 6e 63 74 3b 0a 20 20 0a 20 20 20 20 2f  stinct;.  .    /
2bb2a 2a 0a 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 20  *.    ** SELECT 
2bb2b 2e 2e 2e 20 46 52 4f 4d 20 28 53 45 4c 45 43 54  ... FROM (SELECT
2bb2c 20 2e 2e 2e 20 4c 49 4d 49 54 20 61 20 4f 46 46   ... LIMIT a OFF
2bb2d 53 45 54 20 62 29 20 4c 49 4d 49 54 20 78 20 4f  SET b) LIMIT x O
2bb2e 46 46 53 45 54 20 79 3b 0a 20 20 20 20 2a 2a 0a  FFSET y;.    **.
2bb2f 20 20 20 20 2a 2a 20 4f 6e 65 20 69 73 20 74 65      ** One is te
2bb30 6d 70 74 65 64 20 74 6f 20 74 72 79 20 74 6f 20  mpted to try to 
2bb31 61 64 64 20 61 20 61 6e 64 20 62 20 74 6f 20 63  add a and b to c
2bb32 6f 6d 62 69 6e 65 20 74 68 65 20 6c 69 6d 69 74  ombine the limit
2bb33 73 2e 20 20 42 75 74 20 74 68 69 73 0a 20 20 20  s.  But this.   
2bb34 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72   ** does not wor
2bb35 6b 20 69 66 20 65 69 74 68 65 72 20 6c 69 6d 69  k if either limi
2bb36 74 20 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 20  t is negative.. 
2bb37 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53     */.    if( pS
2bb38 75 62 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20  ub->pLimit ){.  
2bb39 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 4c 69      pParent->pLi
2bb3a 6d 69 74 20 3d 20 70 53 75 62 2d 3e 70 4c 69 6d  mit = pSub->pLim
2bb3b 69 74 3b 0a 20 20 20 20 20 20 70 53 75 62 2d 3e  it;.      pSub->
2bb3c 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20  pLimit = 0;.    
2bb3d 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 69  }.  }..  /* Fini
2bb3e 61 6c 6c 79 2c 20 64 65 6c 65 74 65 20 77 68 61  ally, delete wha
2bb3f 74 20 69 73 20 6c 65 66 74 20 6f 66 20 74 68 65  t is left of the
2bb40 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 72 65   subquery and re
2bb41 74 75 72 6e 0a 20 20 2a 2a 20 73 75 63 63 65 73  turn.  ** succes
2bb42 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  s..  */.  sqlite
2bb43 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
2bb44 2c 20 70 53 75 62 31 29 3b 0a 0a 20 20 72 65 74  , pSub1);..  ret
2bb45 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 20  urn 1;.}.#endif 
2bb46 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
2bb47 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
2bb48 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  ) || !defined(SQ
2bb49 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
2bb4a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a  */../*.** Analyz
2bb4b 65 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  e the SELECT sta
2bb4c 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20 61 73  tement passed as
2bb4d 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20   an argument to 
2bb4e 73 65 65 20 69 66 20 69 74 0a 2a 2a 20 69 73 20  see if it.** is 
2bb4f 61 20 6d 69 6e 28 29 20 6f 72 20 6d 61 78 28 29  a min() or max()
2bb50 20 71 75 65 72 79 2e 20 52 65 74 75 72 6e 20 57   query. Return W
2bb51 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e  HERE_ORDERBY_MIN
2bb52 20 6f 72 20 57 48 45 52 45 5f 4f 52 44 45 52 42   or WHERE_ORDERB
2bb53 59 5f 4d 41 58 20 69 66 20 0a 2a 2a 20 69 74 20  Y_MAX if .** it 
2bb54 69 73 2c 20 6f 72 20 30 20 6f 74 68 65 72 77 69  is, or 0 otherwi
2bb55 73 65 2e 20 41 74 20 70 72 65 73 65 6e 74 2c 20  se. At present, 
2bb56 61 20 71 75 65 72 79 20 69 73 20 63 6f 6e 73 69  a query is consi
2bb57 64 65 72 65 64 20 74 6f 20 62 65 0a 2a 2a 20 61  dered to be.** a
2bb58 20 6d 69 6e 28 29 2f 6d 61 78 28 29 20 71 75 65   min()/max() que
2bb59 72 79 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 31  ry if:.**.**   1
2bb5a 2e 20 54 68 65 72 65 20 69 73 20 61 20 73 69 6e  . There is a sin
2bb5b 67 6c 65 20 6f 62 6a 65 63 74 20 69 6e 20 74 68  gle object in th
2bb5c 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a  e FROM clause..*
2bb5d 2a 0a 2a 2a 20 20 20 32 2e 20 54 68 65 72 65 20  *.**   2. There 
2bb5e 69 73 20 61 20 73 69 6e 67 6c 65 20 65 78 70 72  is a single expr
2bb5f 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 72 65  ession in the re
2bb60 73 75 6c 74 20 73 65 74 2c 20 61 6e 64 20 69 74  sult set, and it
2bb61 20 69 73 0a 2a 2a 20 20 20 20 20 20 65 69 74 68   is.**      eith
2bb62 65 72 20 6d 69 6e 28 78 29 20 6f 72 20 6d 61 78  er min(x) or max
2bb63 28 78 29 2c 20 77 68 65 72 65 20 78 20 69 73 20  (x), where x is 
2bb64 61 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e  a column referen
2bb65 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38  ce..*/.static u8
2bb66 20 6d 69 6e 4d 61 78 51 75 65 72 79 28 53 65 6c   minMaxQuery(Sel
2bb67 65 63 74 20 2a 70 29 7b 0a 20 20 45 78 70 72 20  ect *p){.  Expr 
2bb68 2a 70 45 78 70 72 3b 0a 20 20 45 78 70 72 4c 69  *pExpr;.  ExprLi
2bb69 73 74 20 2a 70 45 4c 69 73 74 20 3d 20 70 2d 3e  st *pEList = p->
2bb6a 70 45 4c 69 73 74 3b 0a 0a 20 20 69 66 28 20 70  pEList;..  if( p
2bb6b 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31 20  EList->nExpr!=1 
2bb6c 29 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f 4f  ) return WHERE_O
2bb6d 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b 0a 20  RDERBY_NORMAL;. 
2bb6e 20 70 45 78 70 72 20 3d 20 70 45 4c 69 73 74 2d   pExpr = pEList-
2bb6f 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 69  >a[0].pExpr;.  i
2bb70 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b  f( pExpr->op!=TK
2bb71 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 29 20  _AGG_FUNCTION ) 
2bb72 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
2bb73 4e 45 56 45 52 28 45 78 70 72 48 61 73 50 72 6f  NEVER(ExprHasPro
2bb74 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
2bb75 78 49 73 53 65 6c 65 63 74 29 29 20 29 20 72 65  xIsSelect)) ) re
2bb76 74 75 72 6e 20 30 3b 0a 20 20 70 45 4c 69 73 74  turn 0;.  pEList
2bb77 20 3d 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73   = pExpr->x.pLis
2bb78 74 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 3d  t;.  if( pEList=
2bb79 3d 30 20 7c 7c 20 70 45 4c 69 73 74 2d 3e 6e 45  =0 || pEList->nE
2bb7a 78 70 72 21 3d 31 20 29 20 72 65 74 75 72 6e 20  xpr!=1 ) return 
2bb7b 30 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74 2d  0;.  if( pEList-
2bb7c 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 21  >a[0].pExpr->op!
2bb7d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 29  =TK_AGG_COLUMN )
2bb7e 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f 4f 52   return WHERE_OR
2bb7f 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b 0a 20 20  DERBY_NORMAL;.  
2bb80 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
2bb81 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20  Property(pExpr, 
2bb82 45 50 5f 49 6e 74 56 61 6c 75 65 29 20 29 3b 0a  EP_IntValue) );.
2bb83 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
2bb84 49 43 6d 70 28 70 45 78 70 72 2d 3e 75 2e 7a 54  ICmp(pExpr->u.zT
2bb85 6f 6b 65 6e 2c 22 6d 69 6e 22 29 3d 3d 30 20 29  oken,"min")==0 )
2bb86 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 48 45  {.    return WHE
2bb87 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 3b 0a  RE_ORDERBY_MIN;.
2bb88 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69    }else if( sqli
2bb89 74 65 33 53 74 72 49 43 6d 70 28 70 45 78 70 72  te3StrICmp(pExpr
2bb8a 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c 22 6d 61 78 22  ->u.zToken,"max"
2bb8b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  )==0 ){.    retu
2bb8c 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59  rn WHERE_ORDERBY
2bb8d 5f 4d 41 58 3b 0a 20 20 7d 0a 20 20 72 65 74 75  _MAX;.  }.  retu
2bb8e 72 6e 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59  rn WHERE_ORDERBY
2bb8f 5f 4e 4f 52 4d 41 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a  _NORMAL;.}../*.*
2bb90 2a 20 54 68 65 20 73 65 6c 65 63 74 20 73 74 61  * The select sta
2bb91 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20 61 73  tement passed as
2bb92 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   the first argum
2bb93 65 6e 74 20 69 73 20 61 6e 20 61 67 67 72 65 67  ent is an aggreg
2bb94 61 74 65 20 71 75 65 72 79 2e 0a 2a 2a 20 54 68  ate query..** Th
2bb95 65 20 73 65 63 6f 6e 64 20 61 72 67 6d 65 6e 74  e second argment
2bb96 20 69 73 20 74 68 65 20 61 73 73 6f 63 69 61 74   is the associat
2bb97 65 64 20 61 67 67 72 65 67 61 74 65 2d 69 6e 66  ed aggregate-inf
2bb98 6f 20 6f 62 6a 65 63 74 2e 20 54 68 69 73 20 0a  o object. This .
2bb99 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 74 65 73 74  ** function test
2bb9a 73 20 69 66 20 74 68 65 20 53 45 4c 45 43 54 20  s if the SELECT 
2bb9b 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a  is of the form:.
2bb9c 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 63  **.**   SELECT c
2bb9d 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 3c 74 62  ount(*) FROM <tb
2bb9e 6c 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 74  l>.**.** where t
2bb9f 61 62 6c 65 20 69 73 20 61 20 64 61 74 61 62 61  able is a databa
2bba0 73 65 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20  se table, not a 
2bba1 73 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 76 69  sub-select or vi
2bba2 65 77 2e 20 49 66 20 74 68 65 20 71 75 65 72 79  ew. If the query
2bba3 0a 2a 2a 20 64 6f 65 73 20 6d 61 74 63 68 20 74  .** does match t
2bba4 68 69 73 20 70 61 74 74 65 72 6e 2c 20 74 68 65  his pattern, the
2bba5 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
2bba6 68 65 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 20  he Table object 
2bba7 72 65 70 72 65 73 65 6e 74 69 6e 67 0a 2a 2a 20  representing.** 
2bba8 3c 74 62 6c 3e 20 69 73 20 72 65 74 75 72 6e 65  <tbl> is returne
2bba9 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 30 20  d. Otherwise, 0 
2bbaa 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  is returned..*/.
2bbab 73 74 61 74 69 63 20 54 61 62 6c 65 20 2a 69 73  static Table *is
2bbac 53 69 6d 70 6c 65 43 6f 75 6e 74 28 53 65 6c 65  SimpleCount(Sele
2bbad 63 74 20 2a 70 2c 20 41 67 67 49 6e 66 6f 20 2a  ct *p, AggInfo *
2bbae 70 41 67 67 49 6e 66 6f 29 7b 0a 20 20 54 61 62  pAggInfo){.  Tab
2bbaf 6c 65 20 2a 70 54 61 62 3b 0a 20 20 45 78 70 72  le *pTab;.  Expr
2bbb0 20 2a 70 45 78 70 72 3b 0a 0a 20 20 61 73 73 65   *pExpr;..  asse
2bbb1 72 74 28 20 21 70 2d 3e 70 47 72 6f 75 70 42 79  rt( !p->pGroupBy
2bbb2 20 29 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 70 57   );..  if( p->pW
2bbb3 68 65 72 65 20 7c 7c 20 70 2d 3e 70 45 4c 69 73  here || p->pELis
2bbb4 74 2d 3e 6e 45 78 70 72 21 3d 31 20 0a 20 20 20  t->nExpr!=1 .   
2bbb5 7c 7c 20 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63  || p->pSrc->nSrc
2bbb6 21 3d 31 20 7c 7c 20 70 2d 3e 70 53 72 63 2d 3e  !=1 || p->pSrc->
2bbb7 61 5b 30 5d 2e 70 53 65 6c 65 63 74 0a 20 20 29  a[0].pSelect.  )
2bbb8 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
2bbb9 20 20 7d 0a 20 20 70 54 61 62 20 3d 20 70 2d 3e    }.  pTab = p->
2bbba 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62 3b  pSrc->a[0].pTab;
2bbbb 0a 20 20 70 45 78 70 72 20 3d 20 70 2d 3e 70 45  .  pExpr = p->pE
2bbbc 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
2bbbd 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  ;.  assert( pTab
2bbbe 20 26 26 20 21 70 54 61 62 2d 3e 70 53 65 6c 65   && !pTab->pSele
2bbbf 63 74 20 26 26 20 70 45 78 70 72 20 29 3b 0a 0a  ct && pExpr );..
2bbc0 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
2bbc1 70 54 61 62 29 20 29 20 72 65 74 75 72 6e 20 30  pTab) ) return 0
2bbc2 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f  ;.  if( pExpr->o
2bbc3 70 21 3d 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  p!=TK_AGG_FUNCTI
2bbc4 4f 4e 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  ON ) return 0;. 
2bbc5 20 69 66 28 20 28 70 41 67 67 49 6e 66 6f 2d 3e   if( (pAggInfo->
2bbc6 61 46 75 6e 63 5b 30 5d 2e 70 46 75 6e 63 2d 3e  aFunc[0].pFunc->
2bbc7 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 46 55 4e  flags&SQLITE_FUN
2bbc8 43 5f 43 4f 55 4e 54 29 3d 3d 30 20 29 20 72 65  C_COUNT)==0 ) re
2bbc9 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 45  turn 0;.  if( pE
2bbca 78 70 72 2d 3e 66 6c 61 67 73 26 45 50 5f 44 69  xpr->flags&EP_Di
2bbcb 73 74 69 6e 63 74 20 29 20 72 65 74 75 72 6e 20  stinct ) return 
2bbcc 30 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70 54 61  0;..  return pTa
2bbcd 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  b;.}../*.** If t
2bbce 68 65 20 73 6f 75 72 63 65 2d 6c 69 73 74 20 69  he source-list i
2bbcf 74 65 6d 20 70 61 73 73 65 64 20 61 73 20 61 6e  tem passed as an
2bbd0 20 61 72 67 75 6d 65 6e 74 20 77 61 73 20 61 75   argument was au
2bbd1 67 6d 65 6e 74 65 64 20 77 69 74 68 20 61 6e 0a  gmented with an.
2bbd2 2a 2a 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c  ** INDEXED BY cl
2bbd3 61 75 73 65 2c 20 74 68 65 6e 20 74 72 79 20 74  ause, then try t
2bbd4 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 73 70 65  o locate the spe
2bbd5 63 69 66 69 65 64 20 69 6e 64 65 78 2e 20 49 66  cified index. If
2bbd6 20 74 68 65 72 65 0a 2a 2a 20 77 61 73 20 73 75   there.** was su
2bbd7 63 68 20 61 20 63 6c 61 75 73 65 20 61 6e 64 20  ch a clause and 
2bbd8 74 68 65 20 6e 61 6d 65 64 20 69 6e 64 65 78 20  the named index 
2bbd9 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c  cannot be found,
2bbda 20 72 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49   return .** SQLI
2bbdb 54 45 5f 45 52 52 4f 52 20 61 6e 64 20 6c 65 61  TE_ERROR and lea
2bbdc 76 65 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 70  ve an error in p
2bbdd 50 61 72 73 65 2e 20 4f 74 68 65 72 77 69 73 65  Parse. Otherwise
2bbde 2c 20 70 6f 70 75 6c 61 74 65 20 0a 2a 2a 20 70  , populate .** p
2bbdf 46 72 6f 6d 2d 3e 70 49 6e 64 65 78 20 61 6e 64  From->pIndex and
2bbe0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
2bbe1 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  K..*/.SQLITE_PRI
2bbe2 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2bbe3 49 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70 28  IndexedByLookup(
2bbe4 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 73  Parse *pParse, s
2bbe5 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
2bbe6 65 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 66  em *pFrom){.  if
2bbe7 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 20 26 26  ( pFrom->pTab &&
2bbe8 20 70 46 72 6f 6d 2d 3e 7a 49 6e 64 65 78 20 29   pFrom->zIndex )
2bbe9 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61  {.    Table *pTa
2bbea 62 20 3d 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3b  b = pFrom->pTab;
2bbeb 0a 20 20 20 20 63 68 61 72 20 2a 7a 49 6e 64 65  .    char *zInde
2bbec 78 20 3d 20 70 46 72 6f 6d 2d 3e 7a 49 6e 64 65  x = pFrom->zInde
2bbed 78 3b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  x;.    Index *pI
2bbee 64 78 3b 0a 20 20 20 20 66 6f 72 28 70 49 64 78  dx;.    for(pIdx
2bbef 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 0a  =pTab->pIndex; .
2bbf0 20 20 20 20 20 20 20 20 70 49 64 78 20 26 26 20          pIdx && 
2bbf1 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70  sqlite3StrICmp(p
2bbf2 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 7a 49 6e 64  Idx->zName, zInd
2bbf3 65 78 29 3b 20 0a 20 20 20 20 20 20 20 20 70 49  ex); .        pI
2bbf4 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 0a 20  dx=pIdx->pNext. 
2bbf5 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 21 70     );.    if( !p
2bbf6 49 64 78 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Idx ){.      sql
2bbf7 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2bbf8 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 69 6e  rse, "no such in
2bbf9 64 65 78 3a 20 25 73 22 2c 20 7a 49 6e 64 65 78  dex: %s", zIndex
2bbfa 2c 20 30 29 3b 0a 20 20 20 20 20 20 72 65 74 75  , 0);.      retu
2bbfb 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
2bbfc 0a 20 20 20 20 7d 0a 20 20 20 20 70 46 72 6f 6d  .    }.    pFrom
2bbfd 2d 3e 70 49 6e 64 65 78 20 3d 20 70 49 64 78 3b  ->pIndex = pIdx;
2bbfe 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
2bbff 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
2bc00 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
2bc01 73 20 61 20 57 61 6c 6b 65 72 20 63 61 6c 6c 62  s a Walker callb
2bc02 61 63 6b 20 66 6f 72 20 22 65 78 70 61 6e 64 69  ack for "expandi
2bc03 6e 67 22 20 61 20 53 45 4c 45 43 54 20 73 74 61  ng" a SELECT sta
2bc04 74 65 6d 65 6e 74 2e 0a 2a 2a 20 22 45 78 70 61  tement..** "Expa
2bc05 6e 64 69 6e 67 22 20 6d 65 61 6e 73 20 74 6f 20  nding" means to 
2bc06 64 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  do the following
2bc07 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20  :.**.**    (1)  
2bc08 4d 61 6b 65 20 73 75 72 65 20 56 44 42 45 20 63  Make sure VDBE c
2bc09 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 68 61  ursor numbers ha
2bc0a 76 65 20 62 65 65 6e 20 61 73 73 69 67 6e 65 64  ve been assigned
2bc0b 20 74 6f 20 65 76 65 72 79 0a 2a 2a 20 20 20 20   to every.**    
2bc0c 20 20 20 20 20 65 6c 65 6d 65 6e 74 20 6f 66 20       element of 
2bc0d 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e  the FROM clause.
2bc0e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20 46  .**.**    (2)  F
2bc0f 69 6c 6c 20 69 6e 20 74 68 65 20 70 54 61 62 4c  ill in the pTabL
2bc10 69 73 74 2d 3e 61 5b 5d 2e 70 54 61 62 20 66 69  ist->a[].pTab fi
2bc11 65 6c 64 73 20 69 6e 20 74 68 65 20 53 72 63 4c  elds in the SrcL
2bc12 69 73 74 20 74 68 61 74 20 0a 2a 2a 20 20 20 20  ist that .**    
2bc13 20 20 20 20 20 64 65 66 69 6e 65 73 20 46 52 4f       defines FRO
2bc14 4d 20 63 6c 61 75 73 65 2e 20 20 57 68 65 6e 20  M clause.  When 
2bc15 76 69 65 77 73 20 61 70 70 65 61 72 20 69 6e 20  views appear in 
2bc16 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2c  the FROM clause,
2bc17 0a 2a 2a 20 20 20 20 20 20 20 20 20 66 69 6c 6c  .**         fill
2bc18 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 2e 70   pTabList->a[].p
2bc19 53 65 6c 65 63 74 20 77 69 74 68 20 61 20 63 6f  Select with a co
2bc1a 70 79 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  py of the SELECT
2bc1b 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20   statement.**   
2bc1c 20 20 20 20 20 20 74 68 61 74 20 69 6d 70 6c 65        that imple
2bc1d 6d 65 6e 74 73 20 74 68 65 20 76 69 65 77 2e 20  ments the view. 
2bc1e 20 41 20 63 6f 70 79 20 69 73 20 6d 61 64 65 20   A copy is made 
2bc1f 6f 66 20 74 68 65 20 76 69 65 77 27 73 20 53 45  of the view's SE
2bc20 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20  LECT.**         
2bc21 73 74 61 74 65 6d 65 6e 74 20 73 6f 20 74 68 61  statement so tha
2bc22 74 20 77 65 20 63 61 6e 20 66 72 65 65 6c 79 20  t we can freely 
2bc23 6d 6f 64 69 66 79 20 6f 72 20 64 65 6c 65 74 65  modify or delete
2bc24 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 0a   that statement.
2bc25 2a 2a 20 20 20 20 20 20 20 20 20 77 69 74 68 6f  **         witho
2bc26 75 74 20 77 6f 72 72 79 69 6e 67 20 61 62 6f 75  ut worrying abou
2bc27 74 20 6d 65 73 73 69 6e 67 20 75 70 20 74 68 65  t messing up the
2bc28 20 70 72 65 73 69 73 74 65 6e 74 20 72 65 70 72   presistent repr
2bc29 65 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 20 20  esentation.**   
2bc2a 20 20 20 20 20 20 6f 66 20 74 68 65 20 76 69 65        of the vie
2bc2b 77 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 33 29 20  w..**.**    (3) 
2bc2c 20 41 64 64 20 74 65 72 6d 73 20 74 6f 20 74 68   Add terms to th
2bc2d 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74  e WHERE clause t
2bc2e 6f 20 61 63 63 6f 6d 6f 64 61 74 65 20 74 68 65  o accomodate the
2bc2f 20 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f 72 64   NATURAL keyword
2bc30 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f 6e 20 6a  .**         on j
2bc31 6f 69 6e 73 20 61 6e 64 20 74 68 65 20 4f 4e 20  oins and the ON 
2bc32 61 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65  and USING clause
2bc33 20 6f 66 20 6a 6f 69 6e 73 2e 0a 2a 2a 0a 2a 2a   of joins..**.**
2bc34 20 20 20 20 28 34 29 20 20 53 63 61 6e 20 74 68      (4)  Scan th
2bc35 65 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e  e list of column
2bc36 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  s in the result 
2bc37 73 65 74 20 28 70 45 4c 69 73 74 29 20 6c 6f 6f  set (pEList) loo
2bc38 6b 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20  king.**         
2bc39 66 6f 72 20 69 6e 73 74 61 6e 63 65 73 20 6f 66  for instances of
2bc3a 20 74 68 65 20 22 2a 22 20 6f 70 65 72 61 74 6f   the "*" operato
2bc3b 72 20 6f 72 20 74 68 65 20 54 41 42 4c 45 2e 2a  r or the TABLE.*
2bc3c 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 20 20 20   operator..**   
2bc3d 20 20 20 20 20 20 49 66 20 66 6f 75 6e 64 2c 20        If found, 
2bc3e 65 78 70 61 6e 64 20 65 61 63 68 20 22 2a 22 20  expand each "*" 
2bc3f 74 6f 20 62 65 20 65 76 65 72 79 20 63 6f 6c 75  to be every colu
2bc40 6d 6e 20 69 6e 20 65 76 65 72 79 20 74 61 62 6c  mn in every tabl
2bc41 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 61 6e 64  e.**         and
2bc42 20 54 41 42 4c 45 2e 2a 20 74 6f 20 62 65 20 65   TABLE.* to be e
2bc43 76 65 72 79 20 63 6f 6c 75 6d 6e 20 69 6e 20 54  very column in T
2bc44 41 42 4c 45 2e 0a 2a 2a 0a 2a 2f 0a 73 74 61 74  ABLE..**.*/.stat
2bc45 69 63 20 69 6e 74 20 73 65 6c 65 63 74 45 78 70  ic int selectExp
2bc46 61 6e 64 65 72 28 57 61 6c 6b 65 72 20 2a 70 57  ander(Walker *pW
2bc47 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70  alker, Select *p
2bc48 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  ){.  Parse *pPar
2bc49 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50  se = pWalker->pP
2bc4a 61 72 73 65 3b 0a 20 20 69 6e 74 20 69 2c 20 6a  arse;.  int i, j
2bc4b 2c 20 6b 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a  , k;.  SrcList *
2bc4c 70 54 61 62 4c 69 73 74 3b 0a 20 20 45 78 70 72  pTabList;.  Expr
2bc4d 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20  List *pEList;.  
2bc4e 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
2bc4f 74 65 6d 20 2a 70 46 72 6f 6d 3b 0a 20 20 73 71  tem *pFrom;.  sq
2bc50 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
2bc51 73 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 64  se->db;..  if( d
2bc52 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2bc53 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57   ){.    return W
2bc54 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 20 20  RC_Abort;.  }.  
2bc55 69 66 28 20 4e 45 56 45 52 28 70 2d 3e 70 53 72  if( NEVER(p->pSr
2bc56 63 3d 3d 30 29 20 7c 7c 20 28 70 2d 3e 73 65 6c  c==0) || (p->sel
2bc57 46 6c 61 67 73 20 26 20 53 46 5f 45 78 70 61 6e  Flags & SF_Expan
2bc58 64 65 64 29 21 3d 30 20 29 7b 0a 20 20 20 20 72  ded)!=0 ){.    r
2bc59 65 74 75 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b  eturn WRC_Prune;
2bc5a 0a 20 20 7d 0a 20 20 70 2d 3e 73 65 6c 46 6c 61  .  }.  p->selFla
2bc5b 67 73 20 7c 3d 20 53 46 5f 45 78 70 61 6e 64 65  gs |= SF_Expande
2bc5c 64 3b 0a 20 20 70 54 61 62 4c 69 73 74 20 3d 20  d;.  pTabList = 
2bc5d 70 2d 3e 70 53 72 63 3b 0a 20 20 70 45 4c 69 73  p->pSrc;.  pELis
2bc5e 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74 3b 0a 0a  t = p->pEList;..
2bc5f 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 63    /* Make sure c
2bc60 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 68 61  ursor numbers ha
2bc61 76 65 20 62 65 65 6e 20 61 73 73 69 67 6e 65 64  ve been assigned
2bc62 20 74 6f 20 61 6c 6c 20 65 6e 74 72 69 65 73 20   to all entries 
2bc63 69 6e 0a 20 20 2a 2a 20 74 68 65 20 46 52 4f 4d  in.  ** the FROM
2bc64 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 53   clause of the S
2bc65 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
2bc66 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53  .  */.  sqlite3S
2bc67 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75 72 73  rcListAssignCurs
2bc68 6f 72 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  ors(pParse, pTab
2bc69 4c 69 73 74 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f  List);..  /* Loo
2bc6a 6b 20 75 70 20 65 76 65 72 79 20 74 61 62 6c 65  k up every table
2bc6b 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20 46 52   named in the FR
2bc6c 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  OM clause of the
2bc6d 20 73 65 6c 65 63 74 2e 20 20 49 66 0a 20 20 2a   select.  If.  *
2bc6e 2a 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68  * an entry of th
2bc6f 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73  e FROM clause is
2bc70 20 61 20 73 75 62 71 75 65 72 79 20 69 6e 73 74   a subquery inst
2bc71 65 61 64 20 6f 66 20 61 20 74 61 62 6c 65 20 6f  ead of a table o
2bc72 72 20 76 69 65 77 2c 0a 20 20 2a 2a 20 74 68 65  r view,.  ** the
2bc73 6e 20 63 72 65 61 74 65 20 61 20 74 72 61 6e 73  n create a trans
2bc74 69 65 6e 74 20 74 61 62 6c 65 20 73 74 72 75 63  ient table struc
2bc75 74 75 72 65 20 74 6f 20 64 65 73 63 72 69 62 65  ture to describe
2bc76 20 74 68 65 20 73 75 62 71 75 65 72 79 2e 0a 20   the subquery.. 
2bc77 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70   */.  for(i=0, p
2bc78 46 72 6f 6d 3d 70 54 61 62 4c 69 73 74 2d 3e 61  From=pTabList->a
2bc79 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  ; i<pTabList->nS
2bc7a 72 63 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b 2b  rc; i++, pFrom++
2bc7b 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  ){.    Table *pT
2bc7c 61 62 3b 0a 20 20 20 20 69 66 28 20 70 46 72 6f  ab;.    if( pFro
2bc7d 6d 2d 3e 70 54 61 62 21 3d 30 20 29 7b 0a 20 20  m->pTab!=0 ){.  
2bc7e 20 20 20 20 2f 2a 20 54 68 69 73 20 73 74 61 74      /* This stat
2bc7f 65 6d 65 6e 74 20 68 61 73 20 61 6c 72 65 61 64  ement has alread
2bc80 79 20 62 65 65 6e 20 70 72 65 70 61 72 65 64 2e  y been prepared.
2bc81 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65    There is no ne
2bc82 65 64 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 67  ed.      ** to g
2bc83 6f 20 66 75 72 74 68 65 72 2e 20 2a 2f 0a 20 20  o further. */.  
2bc84 20 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30      assert( i==0
2bc85 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   );.      return
2bc86 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20   WRC_Prune;.    
2bc87 7d 0a 20 20 20 20 69 66 28 20 70 46 72 6f 6d 2d  }.    if( pFrom-
2bc88 3e 7a 4e 61 6d 65 3d 3d 30 20 29 7b 0a 23 69 66  >zName==0 ){.#if
2bc89 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2bc8a 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20 20 20  _SUBQUERY.      
2bc8b 53 65 6c 65 63 74 20 2a 70 53 65 6c 20 3d 20 70  Select *pSel = p
2bc8c 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a 20  From->pSelect;. 
2bc8d 20 20 20 20 20 2f 2a 20 41 20 73 75 62 2d 71 75       /* A sub-qu
2bc8e 65 72 79 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ery in the FROM 
2bc8f 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45  clause of a SELE
2bc90 43 54 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65  CT */.      asse
2bc91 72 74 28 20 70 53 65 6c 21 3d 30 20 29 3b 0a 20  rt( pSel!=0 );. 
2bc92 20 20 20 20 20 61 73 73 65 72 74 28 20 70 46 72       assert( pFr
2bc93 6f 6d 2d 3e 70 54 61 62 3d 3d 30 20 29 3b 0a 20  om->pTab==0 );. 
2bc94 20 20 20 20 20 73 71 6c 69 74 65 33 57 61 6c 6b       sqlite3Walk
2bc95 53 65 6c 65 63 74 28 70 57 61 6c 6b 65 72 2c 20  Select(pWalker, 
2bc96 70 53 65 6c 29 3b 0a 20 20 20 20 20 20 70 46 72  pSel);.      pFr
2bc97 6f 6d 2d 3e 70 54 61 62 20 3d 20 70 54 61 62 20  om->pTab = pTab 
2bc98 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2bc99 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
2bc9a 28 54 61 62 6c 65 29 29 3b 0a 20 20 20 20 20 20  (Table));.      
2bc9b 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 72 65  if( pTab==0 ) re
2bc9c 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
2bc9d 20 20 20 20 20 20 70 54 61 62 2d 3e 64 62 4d 65        pTab->dbMe
2bc9e 6d 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  m = db->lookasid
2bc9f 65 2e 62 45 6e 61 62 6c 65 64 20 3f 20 64 62 20  e.bEnabled ? db 
2bca0 3a 20 30 3b 0a 20 20 20 20 20 20 70 54 61 62 2d  : 0;.      pTab-
2bca1 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 20  >nRef = 1;.     
2bca2 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20 3d 20 73   pTab->zName = s
2bca3 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
2bca4 2c 20 22 73 71 6c 69 74 65 5f 73 75 62 71 75 65  , "sqlite_subque
2bca5 72 79 5f 25 70 5f 22 2c 20 28 76 6f 69 64 2a 29  ry_%p_", (void*)
2bca6 70 54 61 62 29 3b 0a 20 20 20 20 20 20 77 68 69  pTab);.      whi
2bca7 6c 65 28 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72  le( pSel->pPrior
2bca8 20 29 7b 20 70 53 65 6c 20 3d 20 70 53 65 6c 2d   ){ pSel = pSel-
2bca9 3e 70 50 72 69 6f 72 3b 20 7d 0a 20 20 20 20 20  >pPrior; }.     
2bcaa 20 73 65 6c 65 63 74 43 6f 6c 75 6d 6e 73 46 72   selectColumnsFr
2bcab 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72 73  omExprList(pPars
2bcac 65 2c 20 70 53 65 6c 2d 3e 70 45 4c 69 73 74 2c  e, pSel->pEList,
2bcad 20 26 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 26 70   &pTab->nCol, &p
2bcae 54 61 62 2d 3e 61 43 6f 6c 29 3b 0a 20 20 20 20  Tab->aCol);.    
2bcaf 20 20 70 54 61 62 2d 3e 69 50 4b 65 79 20 3d 20    pTab->iPKey = 
2bcb0 2d 31 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e  -1;.      pTab->
2bcb1 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f 45  tabFlags |= TF_E
2bcb2 70 68 65 6d 65 72 61 6c 3b 0a 23 65 6e 64 69 66  phemeral;.#endif
2bcb3 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2bcb4 20 20 2f 2a 20 41 6e 20 6f 72 64 69 6e 61 72 79    /* An ordinary
2bcb5 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 6e   table or view n
2bcb6 61 6d 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  ame in the FROM 
2bcb7 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20  clause */.      
2bcb8 61 73 73 65 72 74 28 20 70 46 72 6f 6d 2d 3e 70  assert( pFrom->p
2bcb9 54 61 62 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  Tab==0 );.      
2bcba 70 46 72 6f 6d 2d 3e 70 54 61 62 20 3d 20 70 54  pFrom->pTab = pT
2bcbb 61 62 20 3d 20 0a 20 20 20 20 20 20 20 20 73 71  ab = .        sq
2bcbc 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
2bcbd 28 70 50 61 72 73 65 2c 30 2c 70 46 72 6f 6d 2d  (pParse,0,pFrom-
2bcbe 3e 7a 4e 61 6d 65 2c 70 46 72 6f 6d 2d 3e 7a 44  >zName,pFrom->zD
2bcbf 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 20 20  atabase);.      
2bcc0 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 72 65  if( pTab==0 ) re
2bcc1 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
2bcc2 20 20 20 20 20 20 70 54 61 62 2d 3e 6e 52 65 66        pTab->nRef
2bcc3 2b 2b 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64  ++;.#if !defined
2bcc4 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45  (SQLITE_OMIT_VIE
2bcc5 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 20 28  W) || !defined (
2bcc6 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
2bcc7 55 41 4c 54 41 42 4c 45 29 0a 20 20 20 20 20 20  UALTABLE).      
2bcc8 69 66 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63  if( pTab->pSelec
2bcc9 74 20 7c 7c 20 49 73 56 69 72 74 75 61 6c 28 70  t || IsVirtual(p
2bcca 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20  Tab) ){.        
2bccb 2f 2a 20 57 65 20 72 65 61 63 68 20 68 65 72 65  /* We reach here
2bccc 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20 74 61   if the named ta
2bccd 62 6c 65 20 69 73 20 61 20 72 65 61 6c 6c 79 20  ble is a really 
2bcce 61 20 76 69 65 77 20 2a 2f 0a 20 20 20 20 20 20  a view */.      
2bccf 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 69 65    if( sqlite3Vie
2bcd0 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28  wGetColumnNames(
2bcd1 70 50 61 72 73 65 2c 20 70 54 61 62 29 20 29 20  pParse, pTab) ) 
2bcd2 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
2bcd3 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
2bcd4 28 20 70 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74  ( pFrom->pSelect
2bcd5 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 70  ==0 );.        p
2bcd6 46 72 6f 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20  From->pSelect = 
2bcd7 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
2bcd8 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 65 6c 65  (db, pTab->pSele
2bcd9 63 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  ct, 0);.        
2bcda 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
2bcdb 74 28 70 57 61 6c 6b 65 72 2c 20 70 46 72 6f 6d  t(pWalker, pFrom
2bcdc 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20 20  ->pSelect);.    
2bcdd 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d    }.#endif.    }
2bcde 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63 61 74 65 20  ..    /* Locate 
2bcdf 74 68 65 20 69 6e 64 65 78 20 6e 61 6d 65 64 20  the index named 
2bce0 62 79 20 74 68 65 20 49 4e 44 45 58 45 44 20 42  by the INDEXED B
2bce1 59 20 63 6c 61 75 73 65 2c 20 69 66 20 61 6e 79  Y clause, if any
2bce2 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c  . */.    if( sql
2bce3 69 74 65 33 49 6e 64 65 78 65 64 42 79 4c 6f 6f  ite3IndexedByLoo
2bce4 6b 75 70 28 70 50 61 72 73 65 2c 20 70 46 72 6f  kup(pParse, pFro
2bce5 6d 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  m) ){.      retu
2bce6 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
2bce7 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50 72    }.  }..  /* Pr
2bce8 6f 63 65 73 73 20 4e 41 54 55 52 41 4c 20 6b 65  ocess NATURAL ke
2bce9 79 77 6f 72 64 73 2c 20 61 6e 64 20 4f 4e 20 61  ywords, and ON a
2bcea 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73  nd USING clauses
2bceb 20 6f 66 20 6a 6f 69 6e 73 2e 0a 20 20 2a 2f 0a   of joins..  */.
2bcec 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
2bced 46 61 69 6c 65 64 20 7c 7c 20 73 71 6c 69 74 65  Failed || sqlite
2bcee 50 72 6f 63 65 73 73 4a 6f 69 6e 28 70 50 61 72  ProcessJoin(pPar
2bcef 73 65 2c 20 70 29 20 29 7b 0a 20 20 20 20 72 65  se, p) ){.    re
2bcf0 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
2bcf1 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20 65 76    }..  /* For ev
2bcf2 65 72 79 20 22 2a 22 20 74 68 61 74 20 6f 63 63  ery "*" that occ
2bcf3 75 72 73 20 69 6e 20 74 68 65 20 63 6f 6c 75 6d  urs in the colum
2bcf4 6e 20 6c 69 73 74 2c 20 69 6e 73 65 72 74 20 74  n list, insert t
2bcf5 68 65 20 6e 61 6d 65 73 20 6f 66 0a 20 20 2a 2a  he names of.  **
2bcf6 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 69 6e 20   all columns in 
2bcf7 61 6c 6c 20 74 61 62 6c 65 73 2e 20 20 41 6e 64  all tables.  And
2bcf8 20 66 6f 72 20 65 76 65 72 79 20 54 41 42 4c 45   for every TABLE
2bcf9 2e 2a 20 69 6e 73 65 72 74 20 74 68 65 20 6e 61  .* insert the na
2bcfa 6d 65 73 0a 20 20 2a 2a 20 6f 66 20 61 6c 6c 20  mes.  ** of all 
2bcfb 63 6f 6c 75 6d 6e 73 20 69 6e 20 54 41 42 4c 45  columns in TABLE
2bcfc 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 69 6e  .  The parser in
2bcfd 73 65 72 74 65 64 20 61 20 73 70 65 63 69 61 6c  serted a special
2bcfe 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a   expression.  **
2bcff 20 77 69 74 68 20 74 68 65 20 54 4b 5f 41 4c 4c   with the TK_ALL
2bd00 20 6f 70 65 72 61 74 6f 72 20 66 6f 72 20 65 61   operator for ea
2bd01 63 68 20 22 2a 22 20 74 68 61 74 20 69 74 20 66  ch "*" that it f
2bd02 6f 75 6e 64 20 69 6e 20 74 68 65 20 63 6f 6c 75  ound in the colu
2bd03 6d 6e 20 6c 69 73 74 2e 0a 20 20 2a 2a 20 54 68  mn list..  ** Th
2bd04 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64 65  e following code
2bd05 20 6a 75 73 74 20 68 61 73 20 74 6f 20 6c 6f 63   just has to loc
2bd06 61 74 65 20 74 68 65 20 54 4b 5f 41 4c 4c 20 65  ate the TK_ALL e
2bd07 78 70 72 65 73 73 69 6f 6e 73 20 61 6e 64 20 65  xpressions and e
2bd08 78 70 61 6e 64 0a 20 20 2a 2a 20 65 61 63 68 20  xpand.  ** each 
2bd09 6f 6e 65 20 74 6f 20 74 68 65 20 6c 69 73 74 20  one to the list 
2bd0a 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 69  of all columns i
2bd0b 6e 20 61 6c 6c 20 74 61 62 6c 65 73 2e 0a 20 20  n all tables..  
2bd0c 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69 72 73  **.  ** The firs
2bd0d 74 20 6c 6f 6f 70 20 6a 75 73 74 20 63 68 65 63  t loop just chec
2bd0e 6b 73 20 74 6f 20 73 65 65 20 69 66 20 74 68 65  ks to see if the
2bd0f 72 65 20 61 72 65 20 61 6e 79 20 22 2a 22 20 6f  re are any "*" o
2bd10 70 65 72 61 74 6f 72 73 0a 20 20 2a 2a 20 74 68  perators.  ** th
2bd11 61 74 20 6e 65 65 64 20 65 78 70 61 6e 64 69 6e  at need expandin
2bd12 67 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 6b 3d  g..  */.  for(k=
2bd13 30 3b 20 6b 3c 70 45 4c 69 73 74 2d 3e 6e 45 78  0; k<pEList->nEx
2bd14 70 72 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 45 78  pr; k++){.    Ex
2bd15 70 72 20 2a 70 45 20 3d 20 70 45 4c 69 73 74 2d  pr *pE = pEList-
2bd16 3e 61 5b 6b 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[k].pExpr;.   
2bd17 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f   if( pE->op==TK_
2bd18 41 4c 4c 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ALL ) break;.   
2bd19 20 61 73 73 65 72 74 28 20 70 45 2d 3e 6f 70 21   assert( pE->op!
2bd1a 3d 54 4b 5f 44 4f 54 20 7c 7c 20 70 45 2d 3e 70  =TK_DOT || pE->p
2bd1b 52 69 67 68 74 21 3d 30 20 29 3b 0a 20 20 20 20  Right!=0 );.    
2bd1c 61 73 73 65 72 74 28 20 70 45 2d 3e 6f 70 21 3d  assert( pE->op!=
2bd1d 54 4b 5f 44 4f 54 20 7c 7c 20 28 70 45 2d 3e 70  TK_DOT || (pE->p
2bd1e 4c 65 66 74 21 3d 30 20 26 26 20 70 45 2d 3e 70  Left!=0 && pE->p
2bd1f 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 29  Left->op==TK_ID)
2bd20 20 29 3b 0a 20 20 20 20 69 66 28 20 70 45 2d 3e   );.    if( pE->
2bd21 6f 70 3d 3d 54 4b 5f 44 4f 54 20 26 26 20 70 45  op==TK_DOT && pE
2bd22 2d 3e 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b  ->pRight->op==TK
2bd23 5f 41 4c 4c 20 29 20 62 72 65 61 6b 3b 0a 20 20  _ALL ) break;.  
2bd24 7d 0a 20 20 69 66 28 20 6b 3c 70 45 4c 69 73 74  }.  if( k<pEList
2bd25 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 2f  ->nExpr ){.    /
2bd26 2a 0a 20 20 20 20 2a 2a 20 49 66 20 77 65 20 67  *.    ** If we g
2bd27 65 74 20 68 65 72 65 20 69 74 20 6d 65 61 6e 73  et here it means
2bd28 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
2bd29 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20  contains one or 
2bd2a 6d 6f 72 65 20 22 2a 22 0a 20 20 20 20 2a 2a 20  more "*".    ** 
2bd2b 6f 70 65 72 61 74 6f 72 73 20 74 68 61 74 20 6e  operators that n
2bd2c 65 65 64 20 74 6f 20 62 65 20 65 78 70 61 6e 64  eed to be expand
2bd2d 65 64 2e 20 20 4c 6f 6f 70 20 74 68 72 6f 75 67  ed.  Loop throug
2bd2e 68 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f  h each expressio
2bd2f 6e 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20  n.    ** in the 
2bd30 72 65 73 75 6c 74 20 73 65 74 20 61 6e 64 20 65  result set and e
2bd31 78 70 61 6e 64 20 74 68 65 6d 20 6f 6e 65 20 62  xpand them one b
2bd32 79 20 6f 6e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  y one..    */.  
2bd33 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
2bd34 74 5f 69 74 65 6d 20 2a 61 20 3d 20 70 45 4c 69  t_item *a = pELi
2bd35 73 74 2d 3e 61 3b 0a 20 20 20 20 45 78 70 72 4c  st->a;.    ExprL
2bd36 69 73 74 20 2a 70 4e 65 77 20 3d 20 30 3b 0a 20  ist *pNew = 0;. 
2bd37 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 70     int flags = p
2bd38 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73  Parse->db->flags
2bd39 3b 0a 20 20 20 20 69 6e 74 20 6c 6f 6e 67 4e 61  ;.    int longNa
2bd3a 6d 65 73 20 3d 20 28 66 6c 61 67 73 20 26 20 53  mes = (flags & S
2bd3b 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e 61 6d  QLITE_FullColNam
2bd3c 65 73 29 21 3d 30 0a 20 20 20 20 20 20 20 20 20  es)!=0.         
2bd3d 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
2bd3e 28 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  (flags & SQLITE_
2bd3f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 29 3d 3d  ShortColNames)==
2bd40 30 3b 0a 0a 20 20 20 20 66 6f 72 28 6b 3d 30 3b  0;..    for(k=0;
2bd41 20 6b 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72   k<pEList->nExpr
2bd42 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78  ; k++){.      Ex
2bd43 70 72 20 2a 70 45 20 3d 20 61 5b 6b 5d 2e 70 45  pr *pE = a[k].pE
2bd44 78 70 72 3b 0a 20 20 20 20 20 20 61 73 73 65 72  xpr;.      asser
2bd45 74 28 20 70 45 2d 3e 6f 70 21 3d 54 4b 5f 44 4f  t( pE->op!=TK_DO
2bd46 54 20 7c 7c 20 70 45 2d 3e 70 52 69 67 68 74 21  T || pE->pRight!
2bd47 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  =0 );.      if( 
2bd48 70 45 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 20 26  pE->op!=TK_ALL &
2bd49 26 20 28 70 45 2d 3e 6f 70 21 3d 54 4b 5f 44 4f  & (pE->op!=TK_DO
2bd4a 54 20 7c 7c 20 70 45 2d 3e 70 52 69 67 68 74 2d  T || pE->pRight-
2bd4b 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 29 20 29 7b 0a  >op!=TK_ALL) ){.
2bd4c 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
2bd4d 70 61 72 74 69 63 75 6c 61 72 20 65 78 70 72 65  particular expre
2bd4e 73 73 69 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 6e  ssion does not n
2bd4f 65 65 64 20 74 6f 20 62 65 20 65 78 70 61 6e 64  eed to be expand
2bd50 65 64 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ed..        */. 
2bd51 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71         pNew = sq
2bd52 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
2bd53 65 6e 64 28 70 50 61 72 73 65 2c 20 70 4e 65 77  end(pParse, pNew
2bd54 2c 20 61 5b 6b 5d 2e 70 45 78 70 72 29 3b 0a 20  , a[k].pExpr);. 
2bd55 20 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20         if( pNew 
2bd56 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 65  ){.          pNe
2bd57 77 2d 3e 61 5b 70 4e 65 77 2d 3e 6e 45 78 70 72  w->a[pNew->nExpr
2bd58 2d 31 5d 2e 7a 4e 61 6d 65 20 3d 20 61 5b 6b 5d  -1].zName = a[k]
2bd59 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20  .zName;.        
2bd5a 20 20 70 4e 65 77 2d 3e 61 5b 70 4e 65 77 2d 3e    pNew->a[pNew->
2bd5b 6e 45 78 70 72 2d 31 5d 2e 7a 53 70 61 6e 20 3d  nExpr-1].zSpan =
2bd5c 20 61 5b 6b 5d 2e 7a 53 70 61 6e 3b 0a 20 20 20   a[k].zSpan;.   
2bd5d 20 20 20 20 20 20 20 61 5b 6b 5d 2e 7a 4e 61 6d         a[k].zNam
2bd5e 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  e = 0;.         
2bd5f 20 61 5b 6b 5d 2e 7a 53 70 61 6e 20 3d 20 30 3b   a[k].zSpan = 0;
2bd60 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2bd61 20 20 20 61 5b 6b 5d 2e 70 45 78 70 72 20 3d 20     a[k].pExpr = 
2bd62 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  0;.      }else{.
2bd63 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
2bd64 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20  expression is a 
2bd65 22 2a 22 20 6f 72 20 61 20 22 54 41 42 4c 45 2e  "*" or a "TABLE.
2bd66 2a 22 20 61 6e 64 20 6e 65 65 64 73 20 74 6f 20  *" and needs to 
2bd67 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 78  be.        ** ex
2bd68 70 61 6e 64 65 64 2e 20 2a 2f 0a 20 20 20 20 20  panded. */.     
2bd69 20 20 20 69 6e 74 20 74 61 62 6c 65 53 65 65 6e     int tableSeen
2bd6a 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 53 65   = 0;      /* Se
2bd6b 74 20 74 6f 20 31 20 77 68 65 6e 20 54 41 42 4c  t to 1 when TABL
2bd6c 45 20 6d 61 74 63 68 65 73 20 2a 2f 0a 20 20 20  E matches */.   
2bd6d 20 20 20 20 20 63 68 61 72 20 2a 7a 54 4e 61 6d       char *zTNam
2bd6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e;            /*
2bd6f 20 74 65 78 74 20 6f 66 20 6e 61 6d 65 20 6f 66   text of name of
2bd70 20 54 41 42 4c 45 20 2a 2f 0a 20 20 20 20 20 20   TABLE */.      
2bd71 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b    if( pE->op==TK
2bd72 5f 44 4f 54 20 29 7b 0a 20 20 20 20 20 20 20 20  _DOT ){.        
2bd73 20 20 61 73 73 65 72 74 28 20 70 45 2d 3e 70 4c    assert( pE->pL
2bd74 65 66 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20  eft!=0 );.      
2bd75 20 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70      assert( !Exp
2bd76 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 2d  rHasProperty(pE-
2bd77 3e 70 4c 65 66 74 2c 20 45 50 5f 49 6e 74 56 61  >pLeft, EP_IntVa
2bd78 6c 75 65 29 20 29 3b 0a 20 20 20 20 20 20 20 20  lue) );.        
2bd79 20 20 7a 54 4e 61 6d 65 20 3d 20 70 45 2d 3e 70    zTName = pE->p
2bd7a 4c 65 66 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a  Left->u.zToken;.
2bd7b 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
2bd7c 20 20 20 20 20 20 20 20 20 7a 54 4e 61 6d 65 20           zTName 
2bd7d 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 0;.        }. 
2bd7e 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20         for(i=0, 
2bd7f 70 46 72 6f 6d 3d 70 54 61 62 4c 69 73 74 2d 3e  pFrom=pTabList->
2bd80 61 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  a; i<pTabList->n
2bd81 53 72 63 3b 20 69 2b 2b 2c 20 70 46 72 6f 6d 2b  Src; i++, pFrom+
2bd82 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 54 61  +){.          Ta
2bd83 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 46 72 6f  ble *pTab = pFro
2bd84 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20 20  m->pTab;.       
2bd85 20 20 20 63 68 61 72 20 2a 7a 54 61 62 4e 61 6d     char *zTabNam
2bd86 65 20 3d 20 70 46 72 6f 6d 2d 3e 7a 41 6c 69 61  e = pFrom->zAlia
2bd87 73 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  s;.          if(
2bd88 20 7a 54 61 62 4e 61 6d 65 3d 3d 30 20 29 7b 0a   zTabName==0 ){.
2bd89 20 20 20 20 20 20 20 20 20 20 20 20 7a 54 61 62              zTab
2bd8a 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61  Name = pTab->zNa
2bd8b 6d 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  me;.          }.
2bd8c 20 20 20 20 20 20 20 20 20 20 69 66 28 20 64 62            if( db
2bd8d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2bd8e 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
2bd8f 20 20 69 66 28 20 7a 54 4e 61 6d 65 20 26 26 20    if( zTName && 
2bd90 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
2bd91 54 4e 61 6d 65 2c 20 7a 54 61 62 4e 61 6d 65 29  TName, zTabName)
2bd92 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  !=0 ){.         
2bd93 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
2bd94 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2bd95 20 20 20 74 61 62 6c 65 53 65 65 6e 20 3d 20 31     tableSeen = 1
2bd96 3b 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28  ;.          for(
2bd97 6a 3d 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f  j=0; j<pTab->nCo
2bd98 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  l; j++){.       
2bd99 20 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72       Expr *pExpr
2bd9a 2c 20 2a 70 52 69 67 68 74 3b 0a 20 20 20 20 20  , *pRight;.     
2bd9b 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61         char *zNa
2bd9c 6d 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  me = pTab->aCol[
2bd9d 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20  j].zName;.      
2bd9e 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c        char *zCol
2bd9f 6e 61 6d 65 3b 20 20 2f 2a 20 54 68 65 20 63 6f  name;  /* The co
2bda0 6d 70 75 74 65 64 20 63 6f 6c 75 6d 6e 20 6e 61  mputed column na
2bda1 6d 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  me */.          
2bda2 20 20 63 68 61 72 20 2a 7a 54 6f 46 72 65 65 3b    char *zToFree;
2bda3 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63 65 64 20 73     /* Malloced s
2bda4 74 72 69 6e 67 20 74 68 61 74 20 6e 65 65 64 73  tring that needs
2bda5 20 74 6f 20 62 65 20 66 72 65 65 64 20 2a 2f 0a   to be freed */.
2bda6 20 20 20 20 20 20 20 20 20 20 20 20 54 6f 6b 65              Toke
2bda7 6e 20 73 43 6f 6c 6e 61 6d 65 3b 20 20 2f 2a 20  n sColname;  /* 
2bda8 43 6f 6d 70 75 74 65 64 20 63 6f 6c 75 6d 6e 20  Computed column 
2bda9 6e 61 6d 65 20 61 73 20 61 20 74 6f 6b 65 6e 20  name as a token 
2bdaa 2a 2f 0a 0a 20 20 20 20 20 20 20 20 20 20 20 20  */..            
2bdab 2f 2a 20 49 66 20 61 20 63 6f 6c 75 6d 6e 20 69  /* If a column i
2bdac 73 20 6d 61 72 6b 65 64 20 61 73 20 27 68 69 64  s marked as 'hid
2bdad 64 65 6e 27 20 28 63 75 72 72 65 6e 74 6c 79 20  den' (currently 
2bdae 6f 6e 6c 79 20 70 6f 73 73 69 62 6c 65 0a 20 20  only possible.  
2bdaf 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 6f 72            ** for
2bdb0 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 29   virtual tables)
2bdb1 2c 20 64 6f 20 6e 6f 74 20 69 6e 63 6c 75 64 65  , do not include
2bdb2 20 69 74 20 69 6e 20 74 68 65 20 65 78 70 61 6e   it in the expan
2bdb3 64 65 64 0a 20 20 20 20 20 20 20 20 20 20 20 20  ded.            
2bdb4 2a 2a 20 72 65 73 75 6c 74 2d 73 65 74 20 6c 69  ** result-set li
2bdb5 73 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20  st..            
2bdb6 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  */.            i
2bdb7 66 28 20 49 73 48 69 64 64 65 6e 43 6f 6c 75 6d  f( IsHiddenColum
2bdb8 6e 28 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d  n(&pTab->aCol[j]
2bdb9 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
2bdba 20 20 20 61 73 73 65 72 74 28 49 73 56 69 72 74     assert(IsVirt
2bdbb 75 61 6c 28 70 54 61 62 29 29 3b 0a 20 20 20 20  ual(pTab));.    
2bdbc 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e            contin
2bdbd 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ue;.            
2bdbe 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  }..            i
2bdbf 66 28 20 69 3e 30 20 26 26 20 7a 54 4e 61 6d 65  f( i>0 && zTName
2bdc0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
2bdc1 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c       struct SrcL
2bdc2 69 73 74 5f 69 74 65 6d 20 2a 70 4c 65 66 74 20  ist_item *pLeft 
2bdc3 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69  = &pTabList->a[i
2bdc4 2d 31 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 20  -1];.           
2bdc5 20 20 20 69 66 28 20 28 70 4c 65 66 74 5b 31 5d     if( (pLeft[1]
2bdc6 2e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4e  .jointype & JT_N
2bdc7 41 54 55 52 41 4c 29 21 3d 30 20 26 26 0a 20 20  ATURAL)!=0 &&.  
2bdc8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bdc9 20 20 20 20 20 20 63 6f 6c 75 6d 6e 49 6e 64 65        columnInde
2bdca 78 28 70 4c 65 66 74 2d 3e 70 54 61 62 2c 20 7a  x(pLeft->pTab, z
2bdcb 4e 61 6d 65 29 3e 3d 30 20 29 7b 0a 20 20 20 20  Name)>=0 ){.    
2bdcc 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
2bdcd 6e 20 61 20 4e 41 54 55 52 41 4c 20 6a 6f 69 6e  n a NATURAL join
2bdce 2c 20 6f 6d 69 74 20 74 68 65 20 6a 6f 69 6e 20  , omit the join 
2bdcf 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 74 68 65  columns from the
2bdd0 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
2bdd1 20 20 2a 2a 20 74 61 62 6c 65 20 6f 6e 20 74 68    ** table on th
2bdd2 65 20 72 69 67 68 74 20 2a 2f 0a 20 20 20 20 20  e right */.     
2bdd3 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69             conti
2bdd4 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20  nue;.           
2bdd5 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
2bdd6 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 64     if( sqlite3Id
2bdd7 4c 69 73 74 49 6e 64 65 78 28 70 4c 65 66 74 5b  ListIndex(pLeft[
2bdd8 31 5d 2e 70 55 73 69 6e 67 2c 20 7a 4e 61 6d 65  1].pUsing, zName
2bdd9 29 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )>=0 ){.        
2bdda 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 61 20          /* In a 
2bddb 6a 6f 69 6e 20 77 69 74 68 20 61 20 55 53 49 4e  join with a USIN
2bddc 47 20 63 6c 61 75 73 65 2c 20 6f 6d 69 74 20 63  G clause, omit c
2bddd 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 0a 20 20  olumns in the.  
2bdde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
2bddf 20 75 73 69 6e 67 20 63 6c 61 75 73 65 20 66 72   using clause fr
2bde0 6f 6d 20 74 68 65 20 74 61 62 6c 65 20 6f 6e 20  om the table on 
2bde1 74 68 65 20 72 69 67 68 74 2e 20 2a 2f 0a 20 20  the right. */.  
2bde2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
2bde3 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
2bde4 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2bde5 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
2bde6 20 20 70 52 69 67 68 74 20 3d 20 73 71 6c 69 74    pRight = sqlit
2bde7 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 44  e3Expr(db, TK_ID
2bde8 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  , zName);.      
2bde9 20 20 20 20 20 20 7a 43 6f 6c 6e 61 6d 65 20 3d        zColname =
2bdea 20 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20   zName;.        
2bdeb 20 20 20 20 7a 54 6f 46 72 65 65 20 3d 20 30 3b      zToFree = 0;
2bdec 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
2bded 20 6c 6f 6e 67 4e 61 6d 65 73 20 7c 7c 20 70 54   longNames || pT
2bdee 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3e 31 20 29  abList->nSrc>1 )
2bdef 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
2bdf0 45 78 70 72 20 2a 70 4c 65 66 74 3b 0a 20 20 20  Expr *pLeft;.   
2bdf1 20 20 20 20 20 20 20 20 20 20 20 70 4c 65 66 74             pLeft
2bdf2 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64   = sqlite3Expr(d
2bdf3 62 2c 20 54 4b 5f 49 44 2c 20 7a 54 61 62 4e 61  b, TK_ID, zTabNa
2bdf4 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  me);.           
2bdf5 20 20 20 70 45 78 70 72 20 3d 20 73 71 6c 69 74     pExpr = sqlit
2bdf6 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2bdf7 54 4b 5f 44 4f 54 2c 20 70 4c 65 66 74 2c 20 70  TK_DOT, pLeft, p
2bdf8 52 69 67 68 74 2c 20 30 29 3b 0a 20 20 20 20 20  Right, 0);.     
2bdf9 20 20 20 20 20 20 20 20 20 69 66 28 20 6c 6f 6e           if( lon
2bdfa 67 4e 61 6d 65 73 20 29 7b 0a 20 20 20 20 20 20  gNames ){.      
2bdfb 20 20 20 20 20 20 20 20 20 20 7a 43 6f 6c 6e 61            zColna
2bdfc 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  me = sqlite3MPri
2bdfd 6e 74 66 28 64 62 2c 20 22 25 73 2e 25 73 22 2c  ntf(db, "%s.%s",
2bdfe 20 7a 54 61 62 4e 61 6d 65 2c 20 7a 4e 61 6d 65   zTabName, zName
2bdff 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
2be00 20 20 20 7a 54 6f 46 72 65 65 20 3d 20 7a 43 6f     zToFree = zCo
2be01 6c 6e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 20  lname;.         
2be02 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
2be03 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2be04 20 20 20 20 20 20 20 20 70 45 78 70 72 20 3d 20          pExpr = 
2be05 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20  pRight;.        
2be06 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
2be07 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
2be08 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
2be09 50 61 72 73 65 2c 20 70 4e 65 77 2c 20 70 45 78  Parse, pNew, pEx
2be0a 70 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  pr);.           
2be0b 20 73 43 6f 6c 6e 61 6d 65 2e 7a 20 3d 20 7a 43   sColname.z = zC
2be0c 6f 6c 6e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20  olname;.        
2be0d 20 20 20 20 73 43 6f 6c 6e 61 6d 65 2e 6e 20 3d      sColname.n =
2be0e 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2be0f 28 7a 43 6f 6c 6e 61 6d 65 29 3b 0a 20 20 20 20  (zColname);.    
2be10 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2be11 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28 70  xprListSetName(p
2be12 50 61 72 73 65 2c 20 70 4e 65 77 2c 20 26 73 43  Parse, pNew, &sC
2be13 6f 6c 6e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  olname, 0);.    
2be14 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
2be15 62 46 72 65 65 28 64 62 2c 20 7a 54 6f 46 72 65  bFree(db, zToFre
2be16 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  e);.          }.
2be17 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2be18 20 20 69 66 28 20 21 74 61 62 6c 65 53 65 65 6e    if( !tableSeen
2be19 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
2be1a 28 20 7a 54 4e 61 6d 65 20 29 7b 0a 20 20 20 20  ( zTName ){.    
2be1b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2be1c 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
2be1d 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65 3a 20  "no such table: 
2be1e 25 73 22 2c 20 7a 54 4e 61 6d 65 29 3b 0a 20 20  %s", zTName);.  
2be1f 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
2be20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2be21 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2be22 65 2c 20 22 6e 6f 20 74 61 62 6c 65 73 20 73 70  e, "no tables sp
2be23 65 63 69 66 69 65 64 22 29 3b 0a 20 20 20 20 20  ecified");.     
2be24 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
2be25 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
2be26 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
2be27 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 45 4c  stDelete(db, pEL
2be28 69 73 74 29 3b 0a 20 20 20 20 70 2d 3e 70 45 4c  ist);.    p->pEL
2be29 69 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a  ist = pNew;.  }.
2be2a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43  #if SQLITE_MAX_C
2be2b 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70 2d 3e 70  OLUMN.  if( p->p
2be2c 45 4c 69 73 74 20 26 26 20 70 2d 3e 70 45 4c 69  EList && p->pELi
2be2d 73 74 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c  st->nExpr>db->aL
2be2e 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
2be2f 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20  T_COLUMN] ){.   
2be30 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
2be31 28 70 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61  (pParse, "too ma
2be32 6e 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 72 65  ny columns in re
2be33 73 75 6c 74 20 73 65 74 22 29 3b 0a 20 20 7d 0a  sult set");.  }.
2be34 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
2be35 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a  WRC_Continue;.}.
2be36 0a 2f 2a 0a 2a 2a 20 4e 6f 2d 6f 70 20 72 6f 75  ./*.** No-op rou
2be37 74 69 6e 65 20 66 6f 72 20 74 68 65 20 70 61 72  tine for the par
2be38 73 65 2d 74 72 65 65 20 77 61 6c 6b 65 72 2e 0a  se-tree walker..
2be39 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20  **.** When this 
2be3a 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 57  routine is the W
2be3b 61 6c 6b 65 72 2e 78 45 78 70 72 43 61 6c 6c 62  alker.xExprCallb
2be3c 61 63 6b 20 74 68 65 6e 20 65 78 70 72 65 73 73  ack then express
2be3d 69 6f 6e 20 74 72 65 65 73 0a 2a 2a 20 61 72 65  ion trees.** are
2be3e 20 77 61 6c 6b 65 64 20 77 69 74 68 6f 75 74 20   walked without 
2be3f 61 6e 79 20 61 63 74 69 6f 6e 73 20 62 65 69 6e  any actions bein
2be40 67 20 74 61 6b 65 6e 20 61 74 20 65 61 63 68 20  g taken at each 
2be41 6e 6f 64 65 2e 20 20 50 72 65 73 75 6d 61 62 6c  node.  Presumabl
2be42 79 2c 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20  y,.** when this 
2be43 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
2be44 66 6f 72 20 57 61 6c 6b 65 72 2e 78 45 78 70 72  for Walker.xExpr
2be45 43 61 6c 6c 62 61 63 6b 20 74 68 65 6e 20 0a 2a  Callback then .*
2be46 2a 20 57 61 6c 6b 65 72 2e 78 53 65 6c 65 63 74  * Walker.xSelect
2be47 43 61 6c 6c 62 61 63 6b 20 69 73 20 73 65 74 20  Callback is set 
2be48 74 6f 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20  to do something 
2be49 75 73 65 66 75 6c 20 66 6f 72 20 65 76 65 72 79  useful for every
2be4a 20 0a 2a 2a 20 73 75 62 71 75 65 72 79 20 69 6e   .** subquery in
2be4b 20 74 68 65 20 70 61 72 73 65 72 20 74 72 65 65   the parser tree
2be4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2be4d 65 78 70 72 57 61 6c 6b 4e 6f 6f 70 28 57 61 6c  exprWalkNoop(Wal
2be4e 6b 65 72 20 2a 4e 6f 74 55 73 65 64 2c 20 45 78  ker *NotUsed, Ex
2be4f 70 72 20 2a 4e 6f 74 55 73 65 64 32 29 7b 0a 20  pr *NotUsed2){. 
2be50 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
2be51 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55  R2(NotUsed, NotU
2be52 73 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20  sed2);.  return 
2be53 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a  WRC_Continue;.}.
2be54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
2be55 69 6e 65 20 22 65 78 70 61 6e 64 73 22 20 61 20  ine "expands" a 
2be56 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2be57 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 20   and all of its 
2be58 73 75 62 71 75 65 72 69 65 73 2e 0a 2a 2a 20 46  subqueries..** F
2be59 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
2be5a 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 77 68 61  formation on wha
2be5b 74 20 69 74 20 6d 65 61 6e 73 20 74 6f 20 22 65  t it means to "e
2be5c 78 70 61 6e 64 22 20 61 20 53 45 4c 45 43 54 0a  xpand" a SELECT.
2be5d 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2c 20 73 65  ** statement, se
2be5e 65 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 6f 6e  e the comment on
2be5f 20 74 68 65 20 73 65 6c 65 63 74 45 78 70 61 6e   the selectExpan
2be60 64 20 77 6f 72 6b 65 72 20 63 61 6c 6c 62 61 63  d worker callbac
2be61 6b 20 61 62 6f 76 65 2e 0a 2a 2a 0a 2a 2a 20 45  k above..**.** E
2be62 78 70 61 6e 64 69 6e 67 20 61 20 53 45 4c 45 43  xpanding a SELEC
2be63 54 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 74  T statement is t
2be64 68 65 20 66 69 72 73 74 20 73 74 65 70 20 69 6e  he first step in
2be65 20 70 72 6f 63 65 73 73 69 6e 67 20 61 0a 2a 2a   processing a.**
2be66 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2be67 74 2e 20 20 54 68 65 20 53 45 4c 45 43 54 20 73  t.  The SELECT s
2be68 74 61 74 65 6d 65 6e 74 20 6d 75 73 74 20 62 65  tatement must be
2be69 20 65 78 70 61 6e 64 65 64 20 62 65 66 6f 72 65   expanded before
2be6a 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74  .** name resolut
2be6b 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d 65 64  ion is performed
2be6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 74 68  ..**.** If anyth
2be6d 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20  ing goes wrong, 
2be6e 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
2be6f 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f   is written into
2be70 20 70 50 61 72 73 65 2e 0a 2a 2a 20 54 68 65 20   pParse..** The 
2be71 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
2be72 20 63 61 6e 20 64 65 74 65 63 74 20 74 68 65 20   can detect the 
2be73 70 72 6f 62 6c 65 6d 20 62 79 20 6c 6f 6f 6b 69  problem by looki
2be74 6e 67 20 61 74 20 70 50 61 72 73 65 2d 3e 6e 45  ng at pParse->nE
2be75 72 72 0a 2a 2a 20 61 6e 64 2f 6f 72 20 70 50 61  rr.** and/or pPa
2be76 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rse->db->mallocF
2be77 61 69 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ailed..*/.static
2be78 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c   void sqlite3Sel
2be79 65 63 74 45 78 70 61 6e 64 28 50 61 72 73 65 20  ectExpand(Parse 
2be7a 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20  *pParse, Select 
2be7b 2a 70 53 65 6c 65 63 74 29 7b 0a 20 20 57 61 6c  *pSelect){.  Wal
2be7c 6b 65 72 20 77 3b 0a 20 20 77 2e 78 53 65 6c 65  ker w;.  w.xSele
2be7d 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 73 65 6c  ctCallback = sel
2be7e 65 63 74 45 78 70 61 6e 64 65 72 3b 0a 20 20 77  ectExpander;.  w
2be7f 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d  .xExprCallback =
2be80 20 65 78 70 72 57 61 6c 6b 4e 6f 6f 70 3b 0a 20   exprWalkNoop;. 
2be81 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72   w.pParse = pPar
2be82 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c  se;.  sqlite3Wal
2be83 6b 53 65 6c 65 63 74 28 26 77 2c 20 70 53 65 6c  kSelect(&w, pSel
2be84 65 63 74 29 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65  ect);.}...#ifnde
2be85 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  f SQLITE_OMIT_SU
2be86 42 51 55 45 52 59 0a 2f 2a 0a 2a 2a 20 54 68 69  BQUERY./*.** Thi
2be87 73 20 69 73 20 61 20 57 61 6c 6b 65 72 2e 78 53  s is a Walker.xS
2be88 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20 63 61  electCallback ca
2be89 6c 6c 62 61 63 6b 20 66 6f 72 20 74 68 65 20 73  llback for the s
2be8a 71 6c 69 74 65 33 53 65 6c 65 63 74 54 79 70 65  qlite3SelectType
2be8b 49 6e 66 6f 28 29 0a 2a 2a 20 69 6e 74 65 72 66  Info().** interf
2be8c 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65  ace..**.** For e
2be8d 61 63 68 20 46 52 4f 4d 2d 63 6c 61 75 73 65 20  ach FROM-clause 
2be8e 73 75 62 71 75 65 72 79 2c 20 61 64 64 20 43 6f  subquery, add Co
2be8f 6c 75 6d 6e 2e 7a 54 79 70 65 20 61 6e 64 20 43  lumn.zType and C
2be90 6f 6c 75 6d 6e 2e 7a 43 6f 6c 6c 0a 2a 2a 20 69  olumn.zColl.** i
2be91 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 74 68  nformation to th
2be92 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
2be93 65 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74  e that represent
2be94 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  s the result set
2be95 0a 2a 2a 20 6f 66 20 74 68 61 74 20 73 75 62 71  .** of that subq
2be96 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  uery..**.** The 
2be97 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  Table structure 
2be98 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20  that represents 
2be99 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 77  the result set w
2be9a 61 73 20 63 6f 6e 73 74 72 75 63 74 65 64 0a 2a  as constructed.*
2be9b 2a 20 62 79 20 73 65 6c 65 63 74 45 78 70 61 6e  * by selectExpan
2be9c 64 65 72 28 29 20 62 75 74 20 74 68 65 20 74 79  der() but the ty
2be9d 70 65 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e  pe and collation
2be9e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 61 73   information was
2be9f 20 6f 6d 69 74 74 65 64 0a 2a 2a 20 61 74 20 74   omitted.** at t
2bea0 68 61 74 20 70 6f 69 6e 74 20 62 65 63 61 75 73  hat point becaus
2bea1 65 20 69 64 65 6e 74 69 66 69 65 72 73 20 68 61  e identifiers ha
2bea2 64 20 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 72  d not yet been r
2bea3 65 73 6f 6c 76 65 64 2e 20 20 54 68 69 73 0a 2a  esolved.  This.*
2bea4 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  * routine is cal
2bea5 6c 65 64 20 61 66 74 65 72 20 69 64 65 6e 74 69  led after identi
2bea6 66 69 65 72 20 72 65 73 6f 6c 75 74 69 6f 6e 2e  fier resolution.
2bea7 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
2bea8 65 6c 65 63 74 41 64 64 53 75 62 71 75 65 72 79  electAddSubquery
2bea9 54 79 70 65 49 6e 66 6f 28 57 61 6c 6b 65 72 20  TypeInfo(Walker 
2beaa 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74  *pWalker, Select
2beab 20 2a 70 29 7b 0a 20 20 50 61 72 73 65 20 2a 70   *p){.  Parse *p
2beac 50 61 72 73 65 3b 0a 20 20 69 6e 74 20 69 3b 0a  Parse;.  int i;.
2bead 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c    SrcList *pTabL
2beae 69 73 74 3b 0a 20 20 73 74 72 75 63 74 20 53 72  ist;.  struct Sr
2beaf 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 46 72 6f  cList_item *pFro
2beb0 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d  m;..  assert( p-
2beb1 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 52  >selFlags & SF_R
2beb2 65 73 6f 6c 76 65 64 20 29 3b 0a 20 20 61 73 73  esolved );.  ass
2beb3 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67  ert( (p->selFlag
2beb4 73 20 26 20 53 46 5f 48 61 73 54 79 70 65 49 6e  s & SF_HasTypeIn
2beb5 66 6f 29 3d 3d 30 20 29 3b 0a 20 20 70 2d 3e 73  fo)==0 );.  p->s
2beb6 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46 5f 48 61  elFlags |= SF_Ha
2beb7 73 54 79 70 65 49 6e 66 6f 3b 0a 20 20 70 50 61  sTypeInfo;.  pPa
2beb8 72 73 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70  rse = pWalker->p
2beb9 50 61 72 73 65 3b 0a 20 20 70 54 61 62 4c 69 73  Parse;.  pTabLis
2beba 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 66  t = p->pSrc;.  f
2bebb 6f 72 28 69 3d 30 2c 20 70 46 72 6f 6d 3d 70 54  or(i=0, pFrom=pT
2bebc 61 62 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 54 61  abList->a; i<pTa
2bebd 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  bList->nSrc; i++
2bebe 2c 20 70 46 72 6f 6d 2b 2b 29 7b 0a 20 20 20 20  , pFrom++){.    
2bebf 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 46  Table *pTab = pF
2bec0 72 6f 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 69  rom->pTab;.    i
2bec1 66 28 20 41 4c 57 41 59 53 28 70 54 61 62 21 3d  f( ALWAYS(pTab!=
2bec2 30 29 20 26 26 20 28 70 54 61 62 2d 3e 74 61 62  0) && (pTab->tab
2bec3 46 6c 61 67 73 20 26 20 54 46 5f 45 70 68 65 6d  Flags & TF_Ephem
2bec4 65 72 61 6c 29 21 3d 30 20 29 7b 0a 20 20 20 20  eral)!=0 ){.    
2bec5 20 20 2f 2a 20 41 20 73 75 62 2d 71 75 65 72 79    /* A sub-query
2bec6 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
2bec7 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54 20  use of a SELECT 
2bec8 2a 2f 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20  */.      Select 
2bec9 2a 70 53 65 6c 20 3d 20 70 46 72 6f 6d 2d 3e 70  *pSel = pFrom->p
2beca 53 65 6c 65 63 74 3b 0a 20 20 20 20 20 20 61 73  Select;.      as
2becb 73 65 72 74 28 20 70 53 65 6c 20 29 3b 0a 20 20  sert( pSel );.  
2becc 20 20 20 20 77 68 69 6c 65 28 20 70 53 65 6c 2d      while( pSel-
2becd 3e 70 50 72 69 6f 72 20 29 20 70 53 65 6c 20 3d  >pPrior ) pSel =
2bece 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72 3b 0a 20   pSel->pPrior;. 
2becf 20 20 20 20 20 73 65 6c 65 63 74 41 64 64 43 6f       selectAddCo
2bed0 6c 75 6d 6e 54 79 70 65 41 6e 64 43 6f 6c 6c 61  lumnTypeAndColla
2bed1 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 54 61  tion(pParse, pTa
2bed2 62 2d 3e 6e 43 6f 6c 2c 20 70 54 61 62 2d 3e 61  b->nCol, pTab->a
2bed3 43 6f 6c 2c 20 70 53 65 6c 29 3b 0a 20 20 20 20  Col, pSel);.    
2bed4 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 57  }.  }.  return W
2bed5 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 23  RC_Continue;.}.#
2bed6 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  endif.../*.** Th
2bed7 69 73 20 72 6f 75 74 69 6e 65 20 61 64 64 73 20  is routine adds 
2bed8 64 61 74 61 74 79 70 65 20 61 6e 64 20 63 6f 6c  datatype and col
2bed9 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
2beda 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 0a 2a  information to.*
2bedb 2a 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75  * the Table stru
2bedc 63 74 75 72 65 73 20 6f 66 20 61 6c 6c 20 46 52  ctures of all FR
2bedd 4f 4d 2d 63 6c 61 75 73 65 20 73 75 62 71 75 65  OM-clause subque
2bede 72 69 65 73 20 69 6e 20 61 0a 2a 2a 20 53 45 4c  ries in a.** SEL
2bedf 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a  ECT statement..*
2bee0 2a 0a 2a 2a 20 55 73 65 20 74 68 69 73 20 72 6f  *.** Use this ro
2bee1 75 74 69 6e 65 20 61 66 74 65 72 20 6e 61 6d 65  utine after name
2bee2 20 72 65 73 6f 6c 75 74 69 6f 6e 2e 0a 2a 2f 0a   resolution..*/.
2bee3 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
2bee4 74 65 33 53 65 6c 65 63 74 41 64 64 54 79 70 65  te3SelectAddType
2bee5 49 6e 66 6f 28 50 61 72 73 65 20 2a 70 50 61 72  Info(Parse *pPar
2bee6 73 65 2c 20 53 65 6c 65 63 74 20 2a 70 53 65 6c  se, Select *pSel
2bee7 65 63 74 29 7b 0a 23 69 66 6e 64 65 66 20 53 51  ect){.#ifndef SQ
2bee8 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
2bee9 52 59 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20  RY.  Walker w;. 
2beea 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61   w.xSelectCallba
2beeb 63 6b 20 3d 20 73 65 6c 65 63 74 41 64 64 53 75  ck = selectAddSu
2beec 62 71 75 65 72 79 54 79 70 65 49 6e 66 6f 3b 0a  bqueryTypeInfo;.
2beed 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62 61 63    w.xExprCallbac
2beee 6b 20 3d 20 65 78 70 72 57 61 6c 6b 4e 6f 6f 70  k = exprWalkNoop
2beef 3b 0a 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70  ;.  w.pParse = p
2bef0 50 61 72 73 65 3b 0a 20 20 73 71 6c 69 74 65 33  Parse;.  sqlite3
2bef1 57 61 6c 6b 53 65 6c 65 63 74 28 26 77 2c 20 70  WalkSelect(&w, p
2bef2 53 65 6c 65 63 74 29 3b 0a 23 65 6e 64 69 66 0a  Select);.#endif.
2bef3 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  }.../*.** This r
2bef4 6f 75 74 69 6e 65 20 73 65 74 73 20 6f 66 20 61  outine sets of a
2bef5 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2bef6 74 20 66 6f 72 20 70 72 6f 63 65 73 73 69 6e 67  t for processing
2bef7 2e 20 20 54 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77  .  The.** follow
2bef8 69 6e 67 20 69 73 20 61 63 63 6f 6d 70 6c 69 73  ing is accomplis
2bef9 68 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a  hed:.**.**     *
2befa 20 20 56 44 42 45 20 43 75 72 73 6f 72 20 6e 75    VDBE Cursor nu
2befb 6d 62 65 72 73 20 61 72 65 20 61 73 73 69 67 6e  mbers are assign
2befc 65 64 20 74 6f 20 61 6c 6c 20 46 52 4f 4d 2d 63  ed to all FROM-c
2befd 6c 61 75 73 65 20 74 65 72 6d 73 2e 0a 2a 2a 20  lause terms..** 
2befe 20 20 20 20 2a 20 20 45 70 68 65 6d 65 72 61 6c      *  Ephemeral
2beff 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 73 20 61   Table objects a
2bf00 72 65 20 63 72 65 61 74 65 64 20 66 6f 72 20 61  re created for a
2bf01 6c 6c 20 46 52 4f 4d 2d 63 6c 61 75 73 65 20 73  ll FROM-clause s
2bf02 75 62 71 75 65 72 69 65 73 2e 0a 2a 2a 20 20 20  ubqueries..**   
2bf03 20 20 2a 20 20 4f 4e 20 61 6e 64 20 55 53 49 4e    *  ON and USIN
2bf04 47 20 63 6c 61 75 73 65 73 20 61 72 65 20 73 68  G clauses are sh
2bf05 69 66 74 65 64 20 69 6e 74 6f 20 57 48 45 52 45  ifted into WHERE
2bf06 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 20   statements.**  
2bf07 20 20 20 2a 20 20 57 69 6c 64 63 61 72 64 73 20     *  Wildcards 
2bf08 22 2a 22 20 61 6e 64 20 22 54 41 42 4c 45 2e 2a  "*" and "TABLE.*
2bf09 22 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 73  " in result sets
2bf0a 20 61 72 65 20 65 78 70 61 6e 64 65 64 2e 0a 2a   are expanded..*
2bf0b 2a 20 20 20 20 20 2a 20 20 49 64 65 6e 74 69 66  *     *  Identif
2bf0c 69 65 72 73 20 69 6e 20 65 78 70 72 65 73 73 69  iers in expressi
2bf0d 6f 6e 20 61 72 65 20 6d 61 74 63 68 65 64 20 74  on are matched t
2bf0e 6f 20 74 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  o tables..**.** 
2bf0f 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 63 74  This routine act
2bf10 73 20 72 65 63 75 72 73 69 76 65 6c 79 20 6f 6e  s recursively on
2bf11 20 61 6c 6c 20 73 75 62 71 75 65 72 69 65 73 20   all subqueries 
2bf12 77 69 74 68 69 6e 20 74 68 65 20 53 45 4c 45 43  within the SELEC
2bf13 54 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  T..*/.SQLITE_PRI
2bf14 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2bf15 33 53 65 6c 65 63 74 50 72 65 70 28 0a 20 20 50  3SelectPrep(.  P
2bf16 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2bf17 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
2bf18 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
2bf19 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20   Select *p,     
2bf1a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53          /* The S
2bf1b 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
2bf1c 62 65 69 6e 67 20 63 6f 64 65 64 2e 20 2a 2f 0a  being coded. */.
2bf1d 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70    NameContext *p
2bf1e 4f 75 74 65 72 4e 43 20 20 2f 2a 20 4e 61 6d 65  OuterNC  /* Name
2bf1f 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 63 6f 6e   context for con
2bf20 74 61 69 6e 65 72 20 2a 2f 0a 29 7b 0a 20 20 73  tainer */.){.  s
2bf21 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 69 66  qlite3 *db;.  if
2bf22 28 20 4e 45 56 45 52 28 70 3d 3d 30 29 20 29 20  ( NEVER(p==0) ) 
2bf23 72 65 74 75 72 6e 3b 0a 20 20 64 62 20 3d 20 70  return;.  db = p
2bf24 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
2bf25 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53   p->selFlags & S
2bf26 46 5f 48 61 73 54 79 70 65 49 6e 66 6f 20 29 20  F_HasTypeInfo ) 
2bf27 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65  return;.  sqlite
2bf28 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 70 50  3SelectExpand(pP
2bf29 61 72 73 65 2c 20 70 29 3b 0a 20 20 69 66 28 20  arse, p);.  if( 
2bf2a 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
2bf2b 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2bf2c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c   ) return;.  sql
2bf2d 69 74 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 63  ite3ResolveSelec
2bf2e 74 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70  tNames(pParse, p
2bf2f 2c 20 70 4f 75 74 65 72 4e 43 29 3b 0a 20 20 69  , pOuterNC);.  i
2bf30 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  f( pParse->nErr 
2bf31 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
2bf32 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  led ) return;.  
2bf33 73 71 6c 69 74 65 33 53 65 6c 65 63 74 41 64 64  sqlite3SelectAdd
2bf34 54 79 70 65 49 6e 66 6f 28 70 50 61 72 73 65 2c  TypeInfo(pParse,
2bf35 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   p);.}../*.** Re
2bf36 73 65 74 20 74 68 65 20 61 67 67 72 65 67 61 74  set the aggregat
2bf37 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 2e 0a 2a  e accumulator..*
2bf38 2a 0a 2a 2a 20 54 68 65 20 61 67 67 72 65 67 61  *.** The aggrega
2bf39 74 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 69  te accumulator i
2bf3a 73 20 61 20 73 65 74 20 6f 66 20 6d 65 6d 6f 72  s a set of memor
2bf3b 79 20 63 65 6c 6c 73 20 74 68 61 74 20 68 6f 6c  y cells that hol
2bf3c 64 0a 2a 2a 20 69 6e 74 65 72 6d 65 64 69 61 74  d.** intermediat
2bf3d 65 20 72 65 73 75 6c 74 73 20 77 68 69 6c 65 20  e results while 
2bf3e 63 61 6c 63 75 6c 61 74 69 6e 67 20 61 6e 20 61  calculating an a
2bf3f 67 67 72 65 67 61 74 65 2e 20 20 54 68 69 73 0a  ggregate.  This.
2bf40 2a 2a 20 72 6f 75 74 69 6e 65 20 73 69 6d 70 6c  ** routine simpl
2bf41 79 20 73 74 6f 72 65 73 20 4e 55 4c 4c 73 20 69  y stores NULLs i
2bf42 6e 20 61 6c 6c 20 6f 66 20 74 68 6f 73 65 20 6d  n all of those m
2bf43 65 6d 6f 72 79 20 63 65 6c 6c 73 2e 0a 2a 2f 0a  emory cells..*/.
2bf44 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 73 65  static void rese
2bf45 74 41 63 63 75 6d 75 6c 61 74 6f 72 28 50 61 72  tAccumulator(Par
2bf46 73 65 20 2a 70 50 61 72 73 65 2c 20 41 67 67 49  se *pParse, AggI
2bf47 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 29 7b 0a  nfo *pAggInfo){.
2bf48 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
2bf49 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74  se->pVdbe;.  int
2bf4a 20 69 3b 0a 20 20 73 74 72 75 63 74 20 41 67 67   i;.  struct Agg
2bf4b 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 46 75 6e 63  Info_func *pFunc
2bf4c 3b 0a 20 20 69 66 28 20 70 41 67 67 49 6e 66 6f  ;.  if( pAggInfo
2bf4d 2d 3e 6e 46 75 6e 63 2b 70 41 67 67 49 6e 66 6f  ->nFunc+pAggInfo
2bf4e 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a  ->nColumn==0 ){.
2bf4f 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
2bf50 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 41 67    for(i=0; i<pAg
2bf51 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  gInfo->nColumn; 
2bf52 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  i++){.    sqlite
2bf53 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2bf54 50 5f 4e 75 6c 6c 2c 20 30 2c 20 70 41 67 67 49  P_Null, 0, pAggI
2bf55 6e 66 6f 2d 3e 61 43 6f 6c 5b 69 5d 2e 69 4d 65  nfo->aCol[i].iMe
2bf56 6d 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 70 46  m);.  }.  for(pF
2bf57 75 6e 63 3d 70 41 67 67 49 6e 66 6f 2d 3e 61 46  unc=pAggInfo->aF
2bf58 75 6e 63 2c 20 69 3d 30 3b 20 69 3c 70 41 67 67  unc, i=0; i<pAgg
2bf59 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b  Info->nFunc; i++
2bf5a 2c 20 70 46 75 6e 63 2b 2b 29 7b 0a 20 20 20 20  , pFunc++){.    
2bf5b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2bf5c 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c  2(v, OP_Null, 0,
2bf5d 20 70 46 75 6e 63 2d 3e 69 4d 65 6d 29 3b 0a 20   pFunc->iMem);. 
2bf5e 20 20 20 69 66 28 20 70 46 75 6e 63 2d 3e 69 44     if( pFunc->iD
2bf5f 69 73 74 69 6e 63 74 3e 3d 30 20 29 7b 0a 20 20  istinct>=0 ){.  
2bf60 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70      Expr *pE = p
2bf61 46 75 6e 63 2d 3e 70 45 78 70 72 3b 0a 20 20 20  Func->pExpr;.   
2bf62 20 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72     assert( !Expr
2bf63 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 2c 20  HasProperty(pE, 
2bf64 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b  EP_xIsSelect) );
2bf65 0a 20 20 20 20 20 20 69 66 28 20 70 45 2d 3e 78  .      if( pE->x
2bf66 2e 70 4c 69 73 74 3d 3d 30 20 7c 7c 20 70 45 2d  .pList==0 || pE-
2bf67 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21  >x.pList->nExpr!
2bf68 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  =1 ){.        sq
2bf69 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2bf6a 61 72 73 65 2c 20 22 44 49 53 54 49 4e 43 54 20  arse, "DISTINCT 
2bf6b 61 67 67 72 65 67 61 74 65 73 20 6d 75 73 74 20  aggregates must 
2bf6c 68 61 76 65 20 65 78 61 63 74 6c 79 20 6f 6e 65  have exactly one
2bf6d 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 22 61   ".           "a
2bf6e 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 20 20 20  rgument");.     
2bf6f 20 20 20 70 46 75 6e 63 2d 3e 69 44 69 73 74 69     pFunc->iDisti
2bf70 6e 63 74 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20  nct = -1;.      
2bf71 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 4b  }else{.        K
2bf72 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f  eyInfo *pKeyInfo
2bf73 20 3d 20 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78   = keyInfoFromEx
2bf74 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70  prList(pParse, p
2bf75 45 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a 20 20 20  E->x.pList);.   
2bf76 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2bf77 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65  AddOp4(v, OP_Ope
2bf78 6e 45 70 68 65 6d 65 72 61 6c 2c 20 70 46 75 6e  nEphemeral, pFun
2bf79 63 2d 3e 69 44 69 73 74 69 6e 63 74 2c 20 30 2c  c->iDistinct, 0,
2bf7a 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
2bf7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63                (c
2bf7c 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50  har*)pKeyInfo, P
2bf7d 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  4_KEYINFO_HANDOF
2bf7e 46 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  F);.      }.    
2bf7f 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  }.  }.}../*.** I
2bf80 6e 76 6f 6b 65 20 74 68 65 20 4f 50 5f 41 67 67  nvoke the OP_Agg
2bf81 46 69 6e 61 6c 69 7a 65 20 6f 70 63 6f 64 65 20  Finalize opcode 
2bf82 66 6f 72 20 65 76 65 72 79 20 61 67 67 72 65 67  for every aggreg
2bf83 61 74 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  ate function.** 
2bf84 69 6e 20 74 68 65 20 41 67 67 49 6e 66 6f 20 73  in the AggInfo s
2bf85 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 61  tructure..*/.sta
2bf86 74 69 63 20 76 6f 69 64 20 66 69 6e 61 6c 69 7a  tic void finaliz
2bf87 65 41 67 67 46 75 6e 63 74 69 6f 6e 73 28 50 61  eAggFunctions(Pa
2bf88 72 73 65 20 2a 70 50 61 72 73 65 2c 20 41 67 67  rse *pParse, Agg
2bf89 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 29 7b  Info *pAggInfo){
2bf8a 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61  .  Vdbe *v = pPa
2bf8b 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e  rse->pVdbe;.  in
2bf8c 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 41 67  t i;.  struct Ag
2bf8d 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 46 3b 0a  gInfo_func *pF;.
2bf8e 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 3d 70 41    for(i=0, pF=pA
2bf8f 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b 20 69  ggInfo->aFunc; i
2bf90 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63  <pAggInfo->nFunc
2bf91 3b 20 69 2b 2b 2c 20 70 46 2b 2b 29 7b 0a 20 20  ; i++, pF++){.  
2bf92 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
2bf93 74 20 3d 20 70 46 2d 3e 70 45 78 70 72 2d 3e 78  t = pF->pExpr->x
2bf94 2e 70 4c 69 73 74 3b 0a 20 20 20 20 61 73 73 65  .pList;.    asse
2bf95 72 74 28 20 21 45 78 70 72 48 61 73 50 72 6f 70  rt( !ExprHasProp
2bf96 65 72 74 79 28 70 46 2d 3e 70 45 78 70 72 2c 20  erty(pF->pExpr, 
2bf97 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b  EP_xIsSelect) );
2bf98 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2bf99 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 41 67 67  AddOp4(v, OP_Agg
2bf9a 46 69 6e 61 6c 2c 20 70 46 2d 3e 69 4d 65 6d 2c  Final, pF->iMem,
2bf9b 20 70 4c 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e   pList ? pList->
2bf9c 6e 45 78 70 72 20 3a 20 30 2c 20 30 2c 0a 20 20  nExpr : 0, 0,.  
2bf9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bf9e 20 20 20 20 28 76 6f 69 64 2a 29 70 46 2d 3e 70      (void*)pF->p
2bf9f 46 75 6e 63 2c 20 50 34 5f 46 55 4e 43 44 45 46  Func, P4_FUNCDEF
2bfa0 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
2bfa1 55 70 64 61 74 65 20 74 68 65 20 61 63 63 75 6d  Update the accum
2bfa2 75 6c 61 74 6f 72 20 6d 65 6d 6f 72 79 20 63 65  ulator memory ce
2bfa3 6c 6c 73 20 66 6f 72 20 61 6e 20 61 67 67 72 65  lls for an aggre
2bfa4 67 61 74 65 20 62 61 73 65 64 20 6f 6e 0a 2a 2a  gate based on.**
2bfa5 20 74 68 65 20 63 75 72 72 65 6e 74 20 63 75 72   the current cur
2bfa6 73 6f 72 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f  sor position..*/
2bfa7 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 70 64  .static void upd
2bfa8 61 74 65 41 63 63 75 6d 75 6c 61 74 6f 72 28 50  ateAccumulator(P
2bfa9 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 41 67  arse *pParse, Ag
2bfaa 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 29  gInfo *pAggInfo)
2bfab 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  {.  Vdbe *v = pP
2bfac 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69  arse->pVdbe;.  i
2bfad 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20 41  nt i;.  struct A
2bfae 67 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 46 3b  ggInfo_func *pF;
2bfaf 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66  .  struct AggInf
2bfb0 6f 5f 63 6f 6c 20 2a 70 43 3b 0a 0a 20 20 70 41  o_col *pC;..  pA
2bfb1 67 67 49 6e 66 6f 2d 3e 64 69 72 65 63 74 4d 6f  ggInfo->directMo
2bfb2 64 65 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65  de = 1;.  sqlite
2bfb3 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28  3ExprCacheClear(
2bfb4 70 50 61 72 73 65 29 3b 0a 20 20 66 6f 72 28 69  pParse);.  for(i
2bfb5 3d 30 2c 20 70 46 3d 70 41 67 67 49 6e 66 6f 2d  =0, pF=pAggInfo-
2bfb6 3e 61 46 75 6e 63 3b 20 69 3c 70 41 67 67 49 6e  >aFunc; i<pAggIn
2bfb7 66 6f 2d 3e 6e 46 75 6e 63 3b 20 69 2b 2b 2c 20  fo->nFunc; i++, 
2bfb8 70 46 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6e  pF++){.    int n
2bfb9 41 72 67 3b 0a 20 20 20 20 69 6e 74 20 61 64 64  Arg;.    int add
2bfba 72 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 69  rNext = 0;.    i
2bfbb 6e 74 20 72 65 67 41 67 67 3b 0a 20 20 20 20 45  nt regAgg;.    E
2bfbc 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d  xprList *pList =
2bfbd 20 70 46 2d 3e 70 45 78 70 72 2d 3e 78 2e 70 4c   pF->pExpr->x.pL
2bfbe 69 73 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ist;.    assert(
2bfbf 20 21 45 78 70 72 48 61 73 50 72 6f 70 65 72 74   !ExprHasPropert
2bfc0 79 28 70 46 2d 3e 70 45 78 70 72 2c 20 45 50 5f  y(pF->pExpr, EP_
2bfc1 78 49 73 53 65 6c 65 63 74 29 20 29 3b 0a 20 20  xIsSelect) );.  
2bfc2 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20    if( pList ){. 
2bfc3 20 20 20 20 20 6e 41 72 67 20 3d 20 70 4c 69 73       nArg = pLis
2bfc4 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20  t->nExpr;.      
2bfc5 72 65 67 41 67 67 20 3d 20 73 71 6c 69 74 65 33  regAgg = sqlite3
2bfc6 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61  GetTempRange(pPa
2bfc7 72 73 65 2c 20 6e 41 72 67 29 3b 0a 20 20 20 20  rse, nArg);.    
2bfc8 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
2bfc9 65 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65  eExprList(pParse
2bfca 2c 20 70 4c 69 73 74 2c 20 72 65 67 41 67 67 2c  , pList, regAgg,
2bfcb 20 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a   0);.    }else{.
2bfcc 20 20 20 20 20 20 6e 41 72 67 20 3d 20 30 3b 0a        nArg = 0;.
2bfcd 20 20 20 20 20 20 72 65 67 41 67 67 20 3d 20 30        regAgg = 0
2bfce 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
2bfcf 70 46 2d 3e 69 44 69 73 74 69 6e 63 74 3e 3d 30  pF->iDistinct>=0
2bfd0 20 29 7b 0a 20 20 20 20 20 20 61 64 64 72 4e 65   ){.      addrNe
2bfd1 78 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  xt = sqlite3Vdbe
2bfd2 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20  MakeLabel(v);.  
2bfd3 20 20 20 20 61 73 73 65 72 74 28 20 6e 41 72 67      assert( nArg
2bfd4 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 63 6f 64  ==1 );.      cod
2bfd5 65 44 69 73 74 69 6e 63 74 28 70 50 61 72 73 65  eDistinct(pParse
2bfd6 2c 20 70 46 2d 3e 69 44 69 73 74 69 6e 63 74 2c  , pF->iDistinct,
2bfd7 20 61 64 64 72 4e 65 78 74 2c 20 31 2c 20 72 65   addrNext, 1, re
2bfd8 67 41 67 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20  gAgg);.    }.   
2bfd9 20 69 66 28 20 70 46 2d 3e 70 46 75 6e 63 2d 3e   if( pF->pFunc->
2bfda 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46  flags & SQLITE_F
2bfdb 55 4e 43 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a  UNC_NEEDCOLL ){.
2bfdc 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70        CollSeq *p
2bfdd 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  Coll = 0;.      
2bfde 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
2bfdf 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20  item *pItem;.   
2bfe0 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
2bfe1 61 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30  assert( pList!=0
2bfe2 20 29 3b 20 20 2f 2a 20 70 4c 69 73 74 21 3d 30   );  /* pList!=0
2bfe3 20 69 66 20 70 46 2d 3e 70 46 75 6e 63 20 68 61   if pF->pFunc ha
2bfe4 73 20 4e 45 45 44 43 4f 4c 4c 20 2a 2f 0a 20 20  s NEEDCOLL */.  
2bfe5 20 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 74      for(j=0, pIt
2bfe6 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 21 70 43  em=pList->a; !pC
2bfe7 6f 6c 6c 20 26 26 20 6a 3c 6e 41 72 67 3b 20 6a  oll && j<nArg; j
2bfe8 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  ++, pItem++){.  
2bfe9 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71        pColl = sq
2bfea 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
2bfeb 28 70 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e  (pParse, pItem->
2bfec 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 0a  pExpr);.      }.
2bfed 20 20 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c        if( !pColl
2bfee 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c   ){.        pCol
2bfef 6c 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  l = pParse->db->
2bff0 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20 20 20  pDfltColl;.     
2bff1 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
2bff2 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2bff3 5f 43 6f 6c 6c 53 65 71 2c 20 30 2c 20 30 2c 20  _CollSeq, 0, 0, 
2bff4 30 2c 20 28 63 68 61 72 20 2a 29 70 43 6f 6c 6c  0, (char *)pColl
2bff5 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20  , P4_COLLSEQ);. 
2bff6 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
2bff7 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
2bff8 5f 41 67 67 53 74 65 70 2c 20 30 2c 20 72 65 67  _AggStep, 0, reg
2bff9 41 67 67 2c 20 70 46 2d 3e 69 4d 65 6d 2c 0a 20  Agg, pF->iMem,. 
2bffa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2bffb 20 20 20 20 20 28 76 6f 69 64 2a 29 70 46 2d 3e       (void*)pF->
2bffc 70 46 75 6e 63 2c 20 50 34 5f 46 55 4e 43 44 45  pFunc, P4_FUNCDE
2bffd 46 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  F);.    sqlite3V
2bffe 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 28  dbeChangeP5(v, (
2bfff 75 38 29 6e 41 72 67 29 3b 0a 20 20 20 20 73 71  u8)nArg);.    sq
2c000 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2c001 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65  Range(pParse, re
2c002 67 41 67 67 2c 20 6e 41 72 67 29 3b 0a 20 20 20  gAgg, nArg);.   
2c003 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
2c004 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  eAffinityChange(
2c005 70 50 61 72 73 65 2c 20 72 65 67 41 67 67 2c 20  pParse, regAgg, 
2c006 6e 41 72 67 29 3b 0a 20 20 20 20 69 66 28 20 61  nArg);.    if( a
2c007 64 64 72 4e 65 78 74 20 29 7b 0a 20 20 20 20 20  ddrNext ){.     
2c008 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
2c009 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64 72  lveLabel(v, addr
2c00a 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 73 71 6c  Next);.      sql
2c00b 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65  ite3ExprCacheCle
2c00c 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  ar(pParse);.    
2c00d 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 2c  }.  }.  for(i=0,
2c00e 20 70 43 3d 70 41 67 67 49 6e 66 6f 2d 3e 61 43   pC=pAggInfo->aC
2c00f 6f 6c 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e  ol; i<pAggInfo->
2c010 6e 41 63 63 75 6d 75 6c 61 74 6f 72 3b 20 69 2b  nAccumulator; i+
2c011 2b 2c 20 70 43 2b 2b 29 7b 0a 20 20 20 20 73 71  +, pC++){.    sq
2c012 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
2c013 61 72 73 65 2c 20 70 43 2d 3e 70 45 78 70 72 2c  arse, pC->pExpr,
2c014 20 70 43 2d 3e 69 4d 65 6d 29 3b 0a 20 20 7d 0a   pC->iMem);.  }.
2c015 20 20 70 41 67 67 49 6e 66 6f 2d 3e 64 69 72 65    pAggInfo->dire
2c016 63 74 4d 6f 64 65 20 3d 20 30 3b 0a 20 20 73 71  ctMode = 0;.  sq
2c017 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c  lite3ExprCacheCl
2c018 65 61 72 28 70 50 61 72 73 65 29 3b 0a 7d 0a 0a  ear(pParse);.}..
2c019 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
2c01a 6f 64 65 20 66 6f 72 20 74 68 65 20 53 45 4c 45  ode for the SELE
2c01b 43 54 20 73 74 61 74 65 6d 65 6e 74 20 67 69 76  CT statement giv
2c01c 65 6e 20 69 6e 20 74 68 65 20 70 20 61 72 67 75  en in the p argu
2c01d 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68  ment.  .**.** Th
2c01e 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 64 69  e results are di
2c01f 73 74 72 69 62 75 74 65 64 20 69 6e 20 76 61 72  stributed in var
2c020 69 6f 75 73 20 77 61 79 73 20 64 65 70 65 6e 64  ious ways depend
2c021 69 6e 67 20 6f 6e 20 74 68 65 0a 2a 2a 20 63 6f  ing on the.** co
2c022 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 53 65  ntents of the Se
2c023 6c 65 63 74 44 65 73 74 20 73 74 72 75 63 74 75  lectDest structu
2c024 72 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  re pointed to by
2c025 20 61 72 67 75 6d 65 6e 74 20 70 44 65 73 74 0a   argument pDest.
2c026 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  ** as follows:.*
2c027 2a 0a 2a 2a 20 20 20 20 20 70 44 65 73 74 2d 3e  *.**     pDest->
2c028 65 44 65 73 74 20 20 20 20 52 65 73 75 6c 74 0a  eDest    Result.
2c029 2a 2a 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  **     ---------
2c02a 2d 2d 2d 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  ---    ---------
2c02b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2c02c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2c02d 2d 2d 0a 2a 2a 20 20 20 20 20 53 52 54 5f 4f 75  --.**     SRT_Ou
2c02e 74 70 75 74 20 20 20 20 20 20 47 65 6e 65 72 61  tput      Genera
2c02f 74 65 20 61 20 72 6f 77 20 6f 66 20 6f 75 74 70  te a row of outp
2c030 75 74 20 28 75 73 69 6e 67 20 74 68 65 20 4f 50  ut (using the OP
2c031 5f 52 65 73 75 6c 74 52 6f 77 0a 2a 2a 20 20 20  _ResultRow.**   
2c032 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c033 20 20 6f 70 63 6f 64 65 29 20 66 6f 72 20 65 61    opcode) for ea
2c034 63 68 20 72 6f 77 20 69 6e 20 74 68 65 20 72 65  ch row in the re
2c035 73 75 6c 74 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20  sult set..**.** 
2c036 20 20 20 20 53 52 54 5f 4d 65 6d 20 20 20 20 20      SRT_Mem     
2c037 20 20 20 20 4f 6e 6c 79 20 76 61 6c 69 64 20 69      Only valid i
2c038 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  f the result is 
2c039 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 2e  a single column.
2c03a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2c03b 20 20 20 20 20 20 20 20 53 74 6f 72 65 20 74 68          Store th
2c03c 65 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20 6f  e first column o
2c03d 66 20 74 68 65 20 66 69 72 73 74 20 72 65 73 75  f the first resu
2c03e 6c 74 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20  lt row.**       
2c03f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
2c040 20 72 65 67 69 73 74 65 72 20 70 44 65 73 74 2d   register pDest-
2c041 3e 69 50 61 72 6d 20 74 68 65 6e 20 61 62 61 6e  >iParm then aban
2c042 64 6f 6e 20 74 68 65 20 72 65 73 74 0a 2a 2a 20  don the rest.** 
2c043 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c044 20 20 20 20 6f 66 20 74 68 65 20 71 75 65 72 79      of the query
2c045 2e 20 20 54 68 69 73 20 64 65 73 74 69 6e 61 74  .  This destinat
2c046 69 6f 6e 20 69 6d 70 6c 69 65 73 20 22 4c 49 4d  ion implies "LIM
2c047 49 54 20 31 22 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  IT 1"..**.**    
2c048 20 53 52 54 5f 53 65 74 20 20 20 20 20 20 20 20   SRT_Set        
2c049 20 54 68 65 20 72 65 73 75 6c 74 20 6d 75 73 74   The result must
2c04a 20 62 65 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c   be a single col
2c04b 75 6d 6e 2e 20 20 53 74 6f 72 65 20 65 61 63 68  umn.  Store each
2c04c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
2c04d 20 20 20 20 20 20 20 20 72 6f 77 20 6f 66 20 72          row of r
2c04e 65 73 75 6c 74 20 61 73 20 74 68 65 20 6b 65 79  esult as the key
2c04f 20 69 6e 20 74 61 62 6c 65 20 70 44 65 73 74 2d   in table pDest-
2c050 3e 69 50 61 72 6d 2e 20 0a 2a 2a 20 20 20 20 20  >iParm. .**     
2c051 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c052 41 70 70 6c 79 20 74 68 65 20 61 66 66 69 6e 69  Apply the affini
2c053 74 79 20 70 44 65 73 74 2d 3e 61 66 66 69 6e 69  ty pDest->affini
2c054 74 79 20 62 65 66 6f 72 65 20 73 74 6f 72 69 6e  ty before storin
2c055 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  g.**            
2c056 20 20 20 20 20 20 20 20 20 72 65 73 75 6c 74 73           results
2c057 2e 20 20 55 73 65 64 20 74 6f 20 69 6d 70 6c 65  .  Used to imple
2c058 6d 65 6e 74 20 22 49 4e 20 28 53 45 4c 45 43 54  ment "IN (SELECT
2c059 20 2e 2e 2e 29 22 2e 0a 2a 2a 0a 2a 2a 20 20 20   ...)"..**.**   
2c05a 20 20 53 52 54 5f 55 6e 69 6f 6e 20 20 20 20 20    SRT_Union     
2c05b 20 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20    Store results 
2c05c 61 73 20 61 20 6b 65 79 20 69 6e 20 61 20 74 65  as a key in a te
2c05d 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 70 44  mporary table pD
2c05e 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a 0a 2a  est->iParm..**.*
2c05f 2a 20 20 20 20 20 53 52 54 5f 45 78 63 65 70 74  *     SRT_Except
2c060 20 20 20 20 20 20 52 65 6d 6f 76 65 20 72 65 73        Remove res
2c061 75 6c 74 73 20 66 72 6f 6d 20 74 68 65 20 74 65  ults from the te
2c062 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 70 44  mporary table pD
2c063 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a 0a 2a  est->iParm..**.*
2c064 2a 20 20 20 20 20 53 52 54 5f 54 61 62 6c 65 20  *     SRT_Table 
2c065 20 20 20 20 20 20 53 74 6f 72 65 20 72 65 73 75        Store resu
2c066 6c 74 73 20 69 6e 20 74 65 6d 70 6f 72 61 72 79  lts in temporary
2c067 20 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69 50   table pDest->iP
2c068 61 72 6d 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20  arm..**         
2c069 20 20 20 20 20 20 20 20 20 20 20 20 54 68 69 73              This
2c06a 20 69 73 20 6c 69 6b 65 20 53 52 54 5f 45 70 68   is like SRT_Eph
2c06b 65 6d 54 61 62 20 65 78 63 65 70 74 20 74 68 61  emTab except tha
2c06c 74 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20 20  t the table.**  
2c06d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c06e 20 20 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f     is assumed to
2c06f 20 61 6c 72 65 61 64 79 20 62 65 20 6f 70 65 6e   already be open
2c070 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f  ..**.**     SRT_
2c071 45 70 68 65 6d 54 61 62 20 20 20 20 43 72 65 61  EphemTab    Crea
2c072 74 65 20 61 6e 20 74 65 6d 70 6f 72 61 72 79 20  te an temporary 
2c073 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69 50 61  table pDest->iPa
2c074 72 6d 20 61 6e 64 20 73 74 6f 72 65 0a 2a 2a 20  rm and store.** 
2c075 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c076 20 20 20 20 74 68 65 20 72 65 73 75 6c 74 20 74      the result t
2c077 68 65 72 65 2e 20 54 68 65 20 63 75 72 73 6f 72  here. The cursor
2c078 20 69 73 20 6c 65 66 74 20 6f 70 65 6e 20 61 66   is left open af
2c079 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ter.**          
2c07a 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72             retur
2c07b 6e 69 6e 67 2e 20 20 54 68 69 73 20 69 73 20 6c  ning.  This is l
2c07c 69 6b 65 20 53 52 54 5f 54 61 62 6c 65 20 65 78  ike SRT_Table ex
2c07d 63 65 70 74 20 74 68 61 74 0a 2a 2a 20 20 20 20  cept that.**    
2c07e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c07f 20 74 68 69 73 20 64 65 73 74 69 6e 61 74 69 6f   this destinatio
2c080 6e 20 75 73 65 73 20 4f 50 5f 4f 70 65 6e 45 70  n uses OP_OpenEp
2c081 68 65 6d 65 72 61 6c 20 74 6f 20 63 72 65 61 74  hemeral to creat
2c082 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
2c083 20 20 20 20 20 20 20 20 20 74 68 65 20 74 61 62           the tab
2c084 6c 65 20 66 69 72 73 74 2e 0a 2a 2a 0a 2a 2a 20  le first..**.** 
2c085 20 20 20 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e      SRT_Coroutin
2c086 65 20 20 20 47 65 6e 65 72 61 74 65 20 61 20 63  e   Generate a c
2c087 6f 2d 72 6f 75 74 69 6e 65 20 74 68 61 74 20 72  o-routine that r
2c088 65 74 75 72 6e 73 20 61 20 6e 65 77 20 72 6f 77  eturns a new row
2c089 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   of.**          
2c08a 20 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c             resul
2c08b 74 73 20 65 61 63 68 20 74 69 6d 65 20 69 74 20  ts each time it 
2c08c 69 73 20 69 6e 76 6f 6b 65 64 2e 20 20 54 68 65  is invoked.  The
2c08d 20 65 6e 74 72 79 20 70 6f 69 6e 74 0a 2a 2a 20   entry point.** 
2c08e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c08f 20 20 20 20 6f 66 20 74 68 65 20 63 6f 2d 72 6f      of the co-ro
2c090 75 74 69 6e 65 20 69 73 20 73 74 6f 72 65 64 20  utine is stored 
2c091 69 6e 20 72 65 67 69 73 74 65 72 20 70 44 65 73  in register pDes
2c092 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a 20  t->iParm..**.** 
2c093 20 20 20 20 53 52 54 5f 45 78 69 73 74 73 20 20      SRT_Exists  
2c094 20 20 20 20 53 74 6f 72 65 20 61 20 31 20 69 6e      Store a 1 in
2c095 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 70 44 65   memory cell pDe
2c096 73 74 2d 3e 69 50 61 72 6d 20 69 66 20 74 68 65  st->iParm if the
2c097 20 72 65 73 75 6c 74 0a 2a 2a 20 20 20 20 20 20   result.**      
2c098 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
2c099 65 74 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 2e  et is not empty.
2c09a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f 44  .**.**     SRT_D
2c09b 69 73 63 61 72 64 20 20 20 20 20 54 68 72 6f 77  iscard     Throw
2c09c 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 77 61   the results awa
2c09d 79 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64  y.  This is used
2c09e 20 62 79 20 53 45 4c 45 43 54 0a 2a 2a 20 20 20   by SELECT.**   
2c09f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c0a0 20 20 73 74 61 74 65 6d 65 6e 74 73 20 77 69 74    statements wit
2c0a1 68 69 6e 20 74 72 69 67 67 65 72 73 20 77 68 6f  hin triggers who
2c0a2 73 65 20 6f 6e 6c 79 20 70 75 72 70 6f 73 65 20  se only purpose 
2c0a3 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  is.**           
2c0a4 20 20 20 20 20 20 20 20 20 20 74 68 65 20 73 69            the si
2c0a5 64 65 2d 65 66 66 65 63 74 73 20 6f 66 20 66 75  de-effects of fu
2c0a6 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54  nctions..**.** T
2c0a7 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
2c0a8 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
2c0a9 66 20 65 72 72 6f 72 73 2e 20 20 49 66 20 61 6e  f errors.  If an
2c0aa 79 20 65 72 72 6f 72 73 20 61 72 65 0a 2a 2a 20  y errors are.** 
2c0ab 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74 68 65  encountered, the
2c0ac 6e 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65  n an appropriate
2c0ad 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
2c0ae 73 20 6c 65 66 74 20 69 6e 0a 2a 2a 20 70 50 61  s left in.** pPa
2c0af 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a  rse->zErrMsg..**
2c0b0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2c0b1 20 64 6f 65 73 20 4e 4f 54 20 66 72 65 65 20 74   does NOT free t
2c0b2 68 65 20 53 65 6c 65 63 74 20 73 74 72 75 63 74  he Select struct
2c0b3 75 72 65 20 70 61 73 73 65 64 20 69 6e 2e 20 20  ure passed in.  
2c0b4 54 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 66  The.** calling f
2c0b5 75 6e 63 74 69 6f 6e 20 6e 65 65 64 73 20 74 6f  unction needs to
2c0b6 20 64 6f 20 74 68 61 74 2e 0a 2a 2f 0a 53 51 4c   do that..*/.SQL
2c0b7 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
2c0b8 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 0a 20  sqlite3Select(. 
2c0b9 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2c0ba 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2c0bb 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
2c0bc 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20  .  Select *p,   
2c0bd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2c0be 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2c0bf 74 20 62 65 69 6e 67 20 63 6f 64 65 64 2e 20 2a  t being coded. *
2c0c0 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a  /.  SelectDest *
2c0c1 70 44 65 73 74 20 20 20 20 20 20 2f 2a 20 57 68  pDest      /* Wh
2c0c2 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20 74 68  at to do with th
2c0c3 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 20  e query results 
2c0c4 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a  */.){.  int i, j
2c0c5 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
2c0c6 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20  * Loop counters 
2c0c7 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20 2a  */.  WhereInfo *
2c0c8 70 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20 52  pWInfo;     /* R
2c0c9 65 74 75 72 6e 20 66 72 6f 6d 20 73 71 6c 69 74  eturn from sqlit
2c0ca 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 20 2a  e3WhereBegin() *
2c0cb 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20  /.  Vdbe *v;    
2c0cc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2c0cd 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  e virtual machin
2c0ce 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  e under construc
2c0cf 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 73  tion */.  int is
2c0d0 41 67 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  Agg;            
2c0d1 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 73 65 6c   /* True for sel
2c0d2 65 63 74 20 6c 69 73 74 73 20 6c 69 6b 65 20 22  ect lists like "
2c0d3 63 6f 75 6e 74 28 2a 29 22 20 2a 2f 0a 20 20 45  count(*)" */.  E
2c0d4 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
2c0d5 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66        /* List of
2c0d6 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 65 78 74 72   columns to extr
2c0d7 61 63 74 2e 20 2a 2f 0a 20 20 53 72 63 4c 69 73  act. */.  SrcLis
2c0d8 74 20 2a 70 54 61 62 4c 69 73 74 3b 20 20 20 20  t *pTabList;    
2c0d9 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c   /* List of tabl
2c0da 65 73 20 74 6f 20 73 65 6c 65 63 74 20 66 72 6f  es to select fro
2c0db 6d 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  m */.  Expr *pWh
2c0dc 65 72 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ere;          /*
2c0dd 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
2c0de 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20  e.  May be NULL 
2c0df 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2c0e0 4f 72 64 65 72 42 79 3b 20 20 20 20 2f 2a 20 54  OrderBy;    /* T
2c0e1 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2c0e2 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c  se.  May be NULL
2c0e3 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
2c0e4 70 47 72 6f 75 70 42 79 3b 20 20 20 20 2f 2a 20  pGroupBy;    /* 
2c0e5 54 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c 61  The GROUP BY cla
2c0e6 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c  use.  May be NUL
2c0e7 4c 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 48 61  L */.  Expr *pHa
2c0e8 76 69 6e 67 3b 20 20 20 20 20 20 20 20 20 2f 2a  ving;         /*
2c0e9 20 54 68 65 20 48 41 56 49 4e 47 20 63 6c 61 75   The HAVING clau
2c0ea 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c  se.  May be NULL
2c0eb 20 2a 2f 0a 20 20 69 6e 74 20 69 73 44 69 73 74   */.  int isDist
2c0ec 69 6e 63 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  inct;        /* 
2c0ed 54 72 75 65 20 69 66 20 74 68 65 20 44 49 53 54  True if the DIST
2c0ee 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69 73 20  INCT keyword is 
2c0ef 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  present */.  int
2c0f0 20 64 69 73 74 69 6e 63 74 3b 20 20 20 20 20 20   distinct;      
2c0f1 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20      /* Table to 
2c0f2 75 73 65 20 66 6f 72 20 74 68 65 20 64 69 73 74  use for the dist
2c0f3 69 6e 63 74 20 73 65 74 20 2a 2f 0a 20 20 69 6e  inct set */.  in
2c0f4 74 20 72 63 20 3d 20 31 3b 20 20 20 20 20 20 20  t rc = 1;       
2c0f5 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74 6f       /* Value to
2c0f6 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 68 69   return from thi
2c0f7 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  s function */.  
2c0f8 69 6e 74 20 61 64 64 72 53 6f 72 74 49 6e 64 65  int addrSortInde
2c0f9 78 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73  x;     /* Addres
2c0fa 73 20 6f 66 20 61 6e 20 4f 50 5f 4f 70 65 6e 45  s of an OP_OpenE
2c0fb 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72 75 63  phemeral instruc
2c0fc 74 69 6f 6e 20 2a 2f 0a 20 20 41 67 67 49 6e 66  tion */.  AggInf
2c0fd 6f 20 73 41 67 67 49 6e 66 6f 3b 20 20 20 20 20  o sAggInfo;     
2c0fe 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
2c0ff 75 73 65 64 20 62 79 20 61 67 67 72 65 67 61 74  used by aggregat
2c100 65 20 71 75 65 72 69 65 73 20 2a 2f 0a 20 20 69  e queries */.  i
2c101 6e 74 20 69 45 6e 64 3b 20 20 20 20 20 20 20 20  nt iEnd;        
2c102 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
2c103 20 6f 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74   of the end of t
2c104 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 73 71  he query */.  sq
2c105 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
2c106 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61       /* The data
2c107 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
2c108 2a 2f 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73  */..  db = pPars
2c109 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 3d 3d  e->db;.  if( p==
2c10a 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46  0 || db->mallocF
2c10b 61 69 6c 65 64 20 7c 7c 20 70 50 61 72 73 65 2d  ailed || pParse-
2c10c 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20 72 65 74  >nErr ){.    ret
2c10d 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28  urn 1;.  }.  if(
2c10e 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
2c10f 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
2c110 5f 53 45 4c 45 43 54 2c 20 30 2c 20 30 2c 20 30  _SELECT, 0, 0, 0
2c111 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20  ) ) return 1;.  
2c112 6d 65 6d 73 65 74 28 26 73 41 67 67 49 6e 66 6f  memset(&sAggInfo
2c113 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 41 67 67  , 0, sizeof(sAgg
2c114 49 6e 66 6f 29 29 3b 0a 0a 20 20 69 66 28 20 49  Info));..  if( I
2c115 67 6e 6f 72 61 62 6c 65 4f 72 64 65 72 62 79 28  gnorableOrderby(
2c116 70 44 65 73 74 29 20 29 7b 0a 20 20 20 20 61 73  pDest) ){.    as
2c117 73 65 72 74 28 70 44 65 73 74 2d 3e 65 44 65 73  sert(pDest->eDes
2c118 74 3d 3d 53 52 54 5f 45 78 69 73 74 73 20 7c 7c  t==SRT_Exists ||
2c119 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d 53   pDest->eDest==S
2c11a 52 54 5f 55 6e 69 6f 6e 20 7c 7c 20 0a 20 20 20  RT_Union || .   
2c11b 20 20 20 20 20 20 20 20 70 44 65 73 74 2d 3e 65          pDest->e
2c11c 44 65 73 74 3d 3d 53 52 54 5f 45 78 63 65 70 74  Dest==SRT_Except
2c11d 20 7c 7c 20 70 44 65 73 74 2d 3e 65 44 65 73 74   || pDest->eDest
2c11e 3d 3d 53 52 54 5f 44 69 73 63 61 72 64 29 3b 0a  ==SRT_Discard);.
2c11f 20 20 20 20 2f 2a 20 49 66 20 4f 52 44 45 52 20      /* If ORDER 
2c120 42 59 20 6d 61 6b 65 73 20 6e 6f 20 64 69 66 66  BY makes no diff
2c121 65 72 65 6e 63 65 20 69 6e 20 74 68 65 20 6f 75  erence in the ou
2c122 74 70 75 74 20 74 68 65 6e 20 6e 65 69 74 68 65  tput then neithe
2c123 72 20 64 6f 65 73 0a 20 20 20 20 2a 2a 20 44 49  r does.    ** DI
2c124 53 54 49 4e 43 54 20 73 6f 20 69 74 20 63 61 6e  STINCT so it can
2c125 20 62 65 20 72 65 6d 6f 76 65 64 20 74 6f 6f 2e   be removed too.
2c126 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 45   */.    sqlite3E
2c127 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
2c128 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a  , p->pOrderBy);.
2c129 20 20 20 20 70 2d 3e 70 4f 72 64 65 72 42 79 20      p->pOrderBy 
2c12a 3d 20 30 3b 0a 20 20 20 20 70 2d 3e 73 65 6c 46  = 0;.    p->selF
2c12b 6c 61 67 73 20 26 3d 20 7e 53 46 5f 44 69 73 74  lags &= ~SF_Dist
2c12c 69 6e 63 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  inct;.  }.  sqli
2c12d 74 65 33 53 65 6c 65 63 74 50 72 65 70 28 70 50  te3SelectPrep(pP
2c12e 61 72 73 65 2c 20 70 2c 20 30 29 3b 0a 20 20 70  arse, p, 0);.  p
2c12f 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72  OrderBy = p->pOr
2c130 64 65 72 42 79 3b 0a 20 20 70 54 61 62 4c 69 73  derBy;.  pTabLis
2c131 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 70  t = p->pSrc;.  p
2c132 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73  EList = p->pELis
2c133 74 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  t;.  if( pParse-
2c134 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c  >nErr || db->mal
2c135 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
2c136 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64   goto select_end
2c137 3b 0a 20 20 7d 0a 20 20 69 73 41 67 67 20 3d 20  ;.  }.  isAgg = 
2c138 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53  (p->selFlags & S
2c139 46 5f 41 67 67 72 65 67 61 74 65 29 21 3d 30 3b  F_Aggregate)!=0;
2c13a 0a 20 20 61 73 73 65 72 74 28 20 70 45 4c 69 73  .  assert( pELis
2c13b 74 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 42 65  t!=0 );..  /* Be
2c13c 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67 20 63  gin generating c
2c13d 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d 20  ode..  */.  v = 
2c13e 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2c13f 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d  Parse);.  if( v=
2c140 3d 30 20 29 20 67 6f 74 6f 20 73 65 6c 65 63 74  =0 ) goto select
2c141 5f 65 6e 64 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65  _end;..  /* Gene
2c142 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61 6c  rate code for al
2c143 6c 20 73 75 62 2d 71 75 65 72 69 65 73 20 69 6e  l sub-queries in
2c144 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2c145 0a 20 20 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e  .  */.#if !defin
2c146 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
2c147 55 42 51 55 45 52 59 29 20 7c 7c 20 21 64 65 66  UBQUERY) || !def
2c148 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
2c149 5f 56 49 45 57 29 0a 20 20 66 6f 72 28 69 3d 30  _VIEW).  for(i=0
2c14a 3b 20 21 70 2d 3e 70 50 72 69 6f 72 20 26 26 20  ; !p->pPrior && 
2c14b 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  i<pTabList->nSrc
2c14c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74 72 75  ; i++){.    stru
2c14d 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
2c14e 2a 70 49 74 65 6d 20 3d 20 26 70 54 61 62 4c 69  *pItem = &pTabLi
2c14f 73 74 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 53 65  st->a[i];.    Se
2c150 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b 0a 20  lectDest dest;. 
2c151 20 20 20 53 65 6c 65 63 74 20 2a 70 53 75 62 20     Select *pSub 
2c152 3d 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74  = pItem->pSelect
2c153 3b 0a 20 20 20 20 69 6e 74 20 69 73 41 67 67 53  ;.    int isAggS
2c154 75 62 3b 0a 0a 20 20 20 20 69 66 28 20 70 53 75  ub;..    if( pSu
2c155 62 3d 3d 30 20 7c 7c 20 70 49 74 65 6d 2d 3e 69  b==0 || pItem->i
2c156 73 50 6f 70 75 6c 61 74 65 64 20 29 20 63 6f 6e  sPopulated ) con
2c157 74 69 6e 75 65 3b 0a 0a 20 20 20 20 2f 2a 20 49  tinue;..    /* I
2c158 6e 63 72 65 6d 65 6e 74 20 50 61 72 73 65 2e 6e  ncrement Parse.n
2c159 48 65 69 67 68 74 20 62 79 20 74 68 65 20 68 65  Height by the he
2c15a 69 67 68 74 20 6f 66 20 74 68 65 20 6c 61 72 67  ight of the larg
2c15b 65 73 74 20 65 78 70 72 65 73 73 69 6f 6e 0a 20  est expression. 
2c15c 20 20 20 2a 2a 20 74 72 65 65 20 72 65 66 65 72     ** tree refer
2c15d 65 64 20 74 6f 20 62 79 20 74 68 69 73 2c 20 74  ed to by this, t
2c15e 68 65 20 70 61 72 65 6e 74 20 73 65 6c 65 63 74  he parent select
2c15f 2e 20 54 68 65 20 63 68 69 6c 64 20 73 65 6c 65  . The child sele
2c160 63 74 0a 20 20 20 20 2a 2a 20 6d 61 79 20 63 6f  ct.    ** may co
2c161 6e 74 61 69 6e 20 65 78 70 72 65 73 73 69 6f 6e  ntain expression
2c162 20 74 72 65 65 73 20 6f 66 20 61 74 20 6d 6f 73   trees of at mos
2c163 74 0a 20 20 20 20 2a 2a 20 28 53 51 4c 49 54 45  t.    ** (SQLITE
2c164 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 2d  _MAX_EXPR_DEPTH-
2c165 50 61 72 73 65 2e 6e 48 65 69 67 68 74 29 20 68  Parse.nHeight) h
2c166 65 69 67 68 74 2e 20 54 68 69 73 20 69 73 20 61  eight. This is a
2c167 20 62 69 74 0a 20 20 20 20 2a 2a 20 6d 6f 72 65   bit.    ** more
2c168 20 63 6f 6e 73 65 72 76 61 74 69 76 65 20 74 68   conservative th
2c169 61 6e 20 6e 65 63 65 73 73 61 72 79 2c 20 62 75  an necessary, bu
2c16a 74 20 6d 75 63 68 20 65 61 73 69 65 72 20 74 68  t much easier th
2c16b 61 6e 20 65 6e 66 6f 72 63 69 6e 67 0a 20 20 20  an enforcing.   
2c16c 20 2a 2a 20 61 6e 20 65 78 61 63 74 20 6c 69 6d   ** an exact lim
2c16d 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  it..    */.    p
2c16e 50 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 20 2b  Parse->nHeight +
2c16f 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45  = sqlite3SelectE
2c170 78 70 72 48 65 69 67 68 74 28 70 29 3b 0a 0a 20  xprHeight(p);.. 
2c171 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73     /* Check to s
2c172 65 65 20 69 66 20 74 68 65 20 73 75 62 71 75 65  ee if the subque
2c173 72 79 20 63 61 6e 20 62 65 20 61 62 73 6f 72 62  ry can be absorb
2c174 65 64 20 69 6e 74 6f 20 74 68 65 20 70 61 72 65  ed into the pare
2c175 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 73 41 67 67  nt. */.    isAgg
2c176 53 75 62 20 3d 20 28 70 53 75 62 2d 3e 73 65 6c  Sub = (pSub->sel
2c177 46 6c 61 67 73 20 26 20 53 46 5f 41 67 67 72 65  Flags & SF_Aggre
2c178 67 61 74 65 29 21 3d 30 3b 0a 20 20 20 20 69 66  gate)!=0;.    if
2c179 28 20 66 6c 61 74 74 65 6e 53 75 62 71 75 65 72  ( flattenSubquer
2c17a 79 28 70 50 61 72 73 65 2c 20 70 2c 20 69 2c 20  y(pParse, p, i, 
2c17b 69 73 41 67 67 2c 20 69 73 41 67 67 53 75 62 29  isAgg, isAggSub)
2c17c 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73   ){.      if( is
2c17d 41 67 67 53 75 62 20 29 7b 0a 20 20 20 20 20 20  AggSub ){.      
2c17e 20 20 69 73 41 67 67 20 3d 20 31 3b 0a 20 20 20    isAgg = 1;.   
2c17f 20 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73       p->selFlags
2c180 20 7c 3d 20 53 46 5f 41 67 67 72 65 67 61 74 65   |= SF_Aggregate
2c181 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2c182 69 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 65 6c 73  i = -1;.    }els
2c183 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
2c184 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26  SelectDestInit(&
2c185 64 65 73 74 2c 20 53 52 54 5f 45 70 68 65 6d 54  dest, SRT_EphemT
2c186 61 62 2c 20 70 49 74 65 6d 2d 3e 69 43 75 72 73  ab, pItem->iCurs
2c187 6f 72 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  or);.      asser
2c188 74 28 20 70 49 74 65 6d 2d 3e 69 73 50 6f 70 75  t( pItem->isPopu
2c189 6c 61 74 65 64 3d 3d 30 20 29 3b 0a 20 20 20 20  lated==0 );.    
2c18a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28    sqlite3Select(
2c18b 70 50 61 72 73 65 2c 20 70 53 75 62 2c 20 26 64  pParse, pSub, &d
2c18c 65 73 74 29 3b 0a 20 20 20 20 20 20 70 49 74 65  est);.      pIte
2c18d 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 20 3d  m->isPopulated =
2c18e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   1;.    }.    if
2c18f 28 20 2f 2a 70 50 61 72 73 65 2d 3e 6e 45 72 72  ( /*pParse->nErr
2c190 20 7c 7c 2a 2f 20 64 62 2d 3e 6d 61 6c 6c 6f 63   ||*/ db->malloc
2c191 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
2c192 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b  goto select_end;
2c193 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73  .    }.    pPars
2c194 65 2d 3e 6e 48 65 69 67 68 74 20 2d 3d 20 73 71  e->nHeight -= sq
2c195 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48  lite3SelectExprH
2c196 65 69 67 68 74 28 70 29 3b 0a 20 20 20 20 70 54  eight(p);.    pT
2c197 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63  abList = p->pSrc
2c198 3b 0a 20 20 20 20 69 66 28 20 21 49 67 6e 6f 72  ;.    if( !Ignor
2c199 61 62 6c 65 4f 72 64 65 72 62 79 28 70 44 65 73  ableOrderby(pDes
2c19a 74 29 20 29 7b 0a 20 20 20 20 20 20 70 4f 72 64  t) ){.      pOrd
2c19b 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72  erBy = p->pOrder
2c19c 42 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  By;.    }.  }.  
2c19d 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69  pEList = p->pELi
2c19e 73 74 3b 0a 23 65 6e 64 69 66 0a 20 20 70 57 68  st;.#endif.  pWh
2c19f 65 72 65 20 3d 20 70 2d 3e 70 57 68 65 72 65 3b  ere = p->pWhere;
2c1a0 0a 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d  .  pGroupBy = p-
2c1a1 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 70 48 61  >pGroupBy;.  pHa
2c1a2 76 69 6e 67 20 3d 20 70 2d 3e 70 48 61 76 69 6e  ving = p->pHavin
2c1a3 67 3b 0a 20 20 69 73 44 69 73 74 69 6e 63 74 20  g;.  isDistinct 
2c1a4 3d 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  = (p->selFlags &
2c1a5 20 53 46 5f 44 69 73 74 69 6e 63 74 29 21 3d 30   SF_Distinct)!=0
2c1a6 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
2c1a7 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f  E_OMIT_COMPOUND_
2c1a8 53 45 4c 45 43 54 0a 20 20 2f 2a 20 49 66 20 74  SELECT.  /* If t
2c1a9 68 65 72 65 20 69 73 20 61 72 65 20 61 20 73 65  here is are a se
2c1aa 71 75 65 6e 63 65 20 6f 66 20 71 75 65 72 69 65  quence of querie
2c1ab 73 2c 20 64 6f 20 74 68 65 20 65 61 72 6c 69 65  s, do the earlie
2c1ac 72 20 6f 6e 65 73 20 66 69 72 73 74 2e 0a 20 20  r ones first..  
2c1ad 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 69  */.  if( p->pPri
2c1ae 6f 72 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  or ){.    if( p-
2c1af 3e 70 52 69 67 68 74 6d 6f 73 74 3d 3d 30 20 29  >pRightmost==0 )
2c1b0 7b 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a  {.      Select *
2c1b1 70 4c 6f 6f 70 2c 20 2a 70 52 69 67 68 74 20 3d  pLoop, *pRight =
2c1b2 20 30 3b 0a 20 20 20 20 20 20 69 6e 74 20 63 6e   0;.      int cn
2c1b3 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 74  t = 0;.      int
2c1b4 20 6d 78 53 65 6c 65 63 74 3b 0a 20 20 20 20 20   mxSelect;.     
2c1b5 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 3b 20 70 4c   for(pLoop=p; pL
2c1b6 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f 6f 70  oop; pLoop=pLoop
2c1b7 2d 3e 70 50 72 69 6f 72 2c 20 63 6e 74 2b 2b 29  ->pPrior, cnt++)
2c1b8 7b 0a 20 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d  {.        pLoop-
2c1b9 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 70 3b  >pRightmost = p;
2c1ba 0a 20 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e  .        pLoop->
2c1bb 70 4e 65 78 74 20 3d 20 70 52 69 67 68 74 3b 0a  pNext = pRight;.
2c1bc 20 20 20 20 20 20 20 20 70 52 69 67 68 74 20 3d          pRight =
2c1bd 20 70 4c 6f 6f 70 3b 0a 20 20 20 20 20 20 7d 0a   pLoop;.      }.
2c1be 20 20 20 20 20 20 6d 78 53 65 6c 65 63 74 20 3d        mxSelect =
2c1bf 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49   db->aLimit[SQLI
2c1c0 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55 4e  TE_LIMIT_COMPOUN
2c1c1 44 5f 53 45 4c 45 43 54 5d 3b 0a 20 20 20 20 20  D_SELECT];.     
2c1c2 20 69 66 28 20 6d 78 53 65 6c 65 63 74 20 26 26   if( mxSelect &&
2c1c3 20 63 6e 74 3e 6d 78 53 65 6c 65 63 74 20 29 7b   cnt>mxSelect ){
2c1c4 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2c1c5 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2c1c6 20 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73   "too many terms
2c1c7 20 69 6e 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c   in compound SEL
2c1c8 45 43 54 22 29 3b 0a 20 20 20 20 20 20 20 20 72  ECT");.        r
2c1c9 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
2c1ca 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
2c1cb 6e 20 6d 75 6c 74 69 53 65 6c 65 63 74 28 70 50  n multiSelect(pP
2c1cc 61 72 73 65 2c 20 70 2c 20 70 44 65 73 74 29 3b  arse, p, pDest);
2c1cd 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
2c1ce 2a 20 49 66 20 77 72 69 74 69 6e 67 20 74 6f 20  * If writing to 
2c1cf 6d 65 6d 6f 72 79 20 6f 72 20 67 65 6e 65 72 61  memory or genera
2c1d0 74 69 6e 67 20 61 20 73 65 74 0a 20 20 2a 2a 20  ting a set.  ** 
2c1d1 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 63 6f  only a single co
2c1d2 6c 75 6d 6e 20 6d 61 79 20 62 65 20 6f 75 74 70  lumn may be outp
2c1d3 75 74 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66  ut..  */.#ifndef
2c1d4 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
2c1d5 51 55 45 52 59 0a 20 20 69 66 28 20 63 68 65 63  QUERY.  if( chec
2c1d6 6b 46 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53  kForMultiColumnS
2c1d7 65 6c 65 63 74 45 72 72 6f 72 28 70 50 61 72 73  electError(pPars
2c1d8 65 2c 20 70 44 65 73 74 2c 20 70 45 4c 69 73 74  e, pDest, pEList
2c1d9 2d 3e 6e 45 78 70 72 29 20 29 7b 0a 20 20 20 20  ->nExpr) ){.    
2c1da 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b  goto select_end;
2c1db 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
2c1dc 2a 20 49 66 20 70 6f 73 73 69 62 6c 65 2c 20 72  * If possible, r
2c1dd 65 77 72 69 74 65 20 74 68 65 20 71 75 65 72 79  ewrite the query
2c1de 20 74 6f 20 75 73 65 20 47 52 4f 55 50 20 42 59   to use GROUP BY
2c1df 20 69 6e 73 74 65 61 64 20 6f 66 20 44 49 53 54   instead of DIST
2c1e0 49 4e 43 54 2e 0a 20 20 2a 2a 20 47 52 4f 55 50  INCT..  ** GROUP
2c1e1 20 42 59 20 6d 69 67 68 74 20 75 73 65 20 61 6e   BY might use an
2c1e2 20 69 6e 64 65 78 2c 20 44 49 53 54 49 4e 43 54   index, DISTINCT
2c1e3 20 6e 65 76 65 72 20 64 6f 65 73 2e 0a 20 20 2a   never does..  *
2c1e4 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70  /.  assert( p->p
2c1e5 47 72 6f 75 70 42 79 3d 3d 30 20 7c 7c 20 28 70  GroupBy==0 || (p
2c1e6 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f  ->selFlags & SF_
2c1e7 41 67 67 72 65 67 61 74 65 29 21 3d 30 20 29 3b  Aggregate)!=0 );
2c1e8 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c 46 6c  .  if( (p->selFl
2c1e9 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 69 6e  ags & (SF_Distin
2c1ea 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65 29  ct|SF_Aggregate)
2c1eb 29 3d 3d 53 46 5f 44 69 73 74 69 6e 63 74 20 29  )==SF_Distinct )
2c1ec 7b 0a 20 20 20 20 70 2d 3e 70 47 72 6f 75 70 42  {.    p->pGroupB
2c1ed 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  y = sqlite3ExprL
2c1ee 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 45  istDup(db, p->pE
2c1ef 4c 69 73 74 2c 20 30 29 3b 0a 20 20 20 20 70 47  List, 0);.    pG
2c1f0 72 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f  roupBy = p->pGro
2c1f1 75 70 42 79 3b 0a 20 20 20 20 70 2d 3e 73 65 6c  upBy;.    p->sel
2c1f2 46 6c 61 67 73 20 26 3d 20 7e 53 46 5f 44 69 73  Flags &= ~SF_Dis
2c1f3 74 69 6e 63 74 3b 0a 20 20 20 20 69 73 44 69 73  tinct;.    isDis
2c1f4 74 69 6e 63 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a  tinct = 0;.  }..
2c1f5 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
2c1f6 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61   an ORDER BY cla
2c1f7 75 73 65 2c 20 74 68 65 6e 20 74 68 69 73 20 73  use, then this s
2c1f8 6f 72 74 69 6e 67 0a 20 20 2a 2a 20 69 6e 64 65  orting.  ** inde
2c1f9 78 20 6d 69 67 68 74 20 65 6e 64 20 75 70 20 62  x might end up b
2c1fa 65 69 6e 67 20 75 6e 75 73 65 64 20 69 66 20 74  eing unused if t
2c1fb 68 65 20 64 61 74 61 20 63 61 6e 20 62 65 20 0a  he data can be .
2c1fc 20 20 2a 2a 20 65 78 74 72 61 63 74 65 64 20 69    ** extracted i
2c1fd 6e 20 70 72 65 2d 73 6f 72 74 65 64 20 6f 72 64  n pre-sorted ord
2c1fe 65 72 2e 20 20 49 66 20 74 68 61 74 20 69 73 20  er.  If that is 
2c1ff 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e 20 74  the case, then t
2c200 68 65 0a 20 20 2a 2a 20 4f 50 5f 4f 70 65 6e 45  he.  ** OP_OpenE
2c201 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72 75 63  phemeral instruc
2c202 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 68 61  tion will be cha
2c203 6e 67 65 64 20 74 6f 20 61 6e 20 4f 50 5f 4e 6f  nged to an OP_No
2c204 6f 70 20 6f 6e 63 65 0a 20 20 2a 2a 20 77 65 20  op once.  ** we 
2c205 66 69 67 75 72 65 20 6f 75 74 20 74 68 61 74 20  figure out that 
2c206 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65  the sorting inde
2c207 78 20 69 73 20 6e 6f 74 20 6e 65 65 64 65 64 2e  x is not needed.
2c208 20 20 54 68 65 20 61 64 64 72 53 6f 72 74 49 6e    The addrSortIn
2c209 64 65 78 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c  dex.  ** variabl
2c20a 65 20 69 73 20 75 73 65 64 20 74 6f 20 66 61 63  e is used to fac
2c20b 69 6c 69 74 61 74 65 20 74 68 61 74 20 63 68 61  ilitate that cha
2c20c 6e 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  nge..  */.  if( 
2c20d 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
2c20e 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66  KeyInfo *pKeyInf
2c20f 6f 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20  o;.    pKeyInfo 
2c210 3d 20 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70  = keyInfoFromExp
2c211 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 4f  rList(pParse, pO
2c212 72 64 65 72 42 79 29 3b 0a 20 20 20 20 70 4f 72  rderBy);.    pOr
2c213 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 20  derBy->iECursor 
2c214 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
2c215 3b 0a 20 20 20 20 70 2d 3e 61 64 64 72 4f 70 65  ;.    p->addrOpe
2c216 6e 45 70 68 6d 5b 32 5d 20 3d 20 61 64 64 72 53  nEphm[2] = addrS
2c217 6f 72 74 49 6e 64 65 78 20 3d 0a 20 20 20 20 20  ortIndex =.     
2c218 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c219 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68  p4(v, OP_OpenEph
2c21a 65 6d 65 72 61 6c 2c 0a 20 20 20 20 20 20 20 20  emeral,.        
2c21b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c21c 20 20 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43     pOrderBy->iEC
2c21d 75 72 73 6f 72 2c 20 70 4f 72 64 65 72 42 79 2d  ursor, pOrderBy-
2c21e 3e 6e 45 78 70 72 2b 32 2c 20 30 2c 0a 20 20 20  >nExpr+2, 0,.   
2c21f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c220 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
2c221 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49  KeyInfo, P4_KEYI
2c222 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20  NFO_HANDOFF);.  
2c223 7d 65 6c 73 65 7b 0a 20 20 20 20 61 64 64 72 53  }else{.    addrS
2c224 6f 72 74 49 6e 64 65 78 20 3d 20 2d 31 3b 0a 20  ortIndex = -1;. 
2c225 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
2c226 6f 75 74 70 75 74 20 69 73 20 64 65 73 74 69 6e  output is destin
2c227 65 64 20 66 6f 72 20 61 20 74 65 6d 70 6f 72 61  ed for a tempora
2c228 72 79 20 74 61 62 6c 65 2c 20 6f 70 65 6e 20 74  ry table, open t
2c229 68 61 74 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a  hat table..  */.
2c22a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 65 44 65    if( pDest->eDe
2c22b 73 74 3d 3d 53 52 54 5f 45 70 68 65 6d 54 61 62  st==SRT_EphemTab
2c22c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
2c22d 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c22e 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 70  OpenEphemeral, p
2c22f 44 65 73 74 2d 3e 69 50 61 72 6d 2c 20 70 45 4c  Dest->iParm, pEL
2c230 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 7d  ist->nExpr);.  }
2c231 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6c  ..  /* Set the l
2c232 69 6d 69 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 69  imiter..  */.  i
2c233 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62  End = sqlite3Vdb
2c234 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
2c235 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65 67   computeLimitReg
2c236 69 73 74 65 72 73 28 70 50 61 72 73 65 2c 20 70  isters(pParse, p
2c237 2c 20 69 45 6e 64 29 3b 0a 0a 20 20 2f 2a 20 4f  , iEnd);..  /* O
2c238 70 65 6e 20 61 20 76 69 72 74 75 61 6c 20 69 6e  pen a virtual in
2c239 64 65 78 20 74 6f 20 75 73 65 20 66 6f 72 20 74  dex to use for t
2c23a 68 65 20 64 69 73 74 69 6e 63 74 20 73 65 74 2e  he distinct set.
2c23b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73 44 69  .  */.  if( isDi
2c23c 73 74 69 6e 63 74 20 29 7b 0a 20 20 20 20 4b 65  stinct ){.    Ke
2c23d 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b  yInfo *pKeyInfo;
2c23e 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73 41  .    assert( isA
2c23f 67 67 20 7c 7c 20 70 47 72 6f 75 70 42 79 20 29  gg || pGroupBy )
2c240 3b 0a 20 20 20 20 64 69 73 74 69 6e 63 74 20 3d  ;.    distinct =
2c241 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b   pParse->nTab++;
2c242 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20  .    pKeyInfo = 
2c243 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c  keyInfoFromExprL
2c244 69 73 74 28 70 50 61 72 73 65 2c 20 70 2d 3e 70  ist(pParse, p->p
2c245 45 4c 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69  EList);.    sqli
2c246 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2c247 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
2c248 6c 2c 20 64 69 73 74 69 6e 63 74 2c 20 30 2c 20  l, distinct, 0, 
2c249 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
2c24a 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72             (char
2c24b 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b  *)pKeyInfo, P4_K
2c24c 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b  EYINFO_HANDOFF);
2c24d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 69  .  }else{.    di
2c24e 73 74 69 6e 63 74 20 3d 20 2d 31 3b 0a 20 20 7d  stinct = -1;.  }
2c24f 0a 0a 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65  ..  /* Aggregate
2c250 20 61 6e 64 20 6e 6f 6e 2d 61 67 67 72 65 67 61   and non-aggrega
2c251 74 65 20 71 75 65 72 69 65 73 20 61 72 65 20 68  te queries are h
2c252 61 6e 64 6c 65 64 20 64 69 66 66 65 72 65 6e 74  andled different
2c253 6c 79 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 41  ly */.  if( !isA
2c254 67 67 20 26 26 20 70 47 72 6f 75 70 42 79 3d 3d  gg && pGroupBy==
2c255 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73  0 ){.    /* This
2c256 20 63 61 73 65 20 69 73 20 66 6f 72 20 6e 6f 6e   case is for non
2c257 2d 61 67 67 72 65 67 61 74 65 20 71 75 65 72 69  -aggregate queri
2c258 65 73 0a 20 20 20 20 2a 2a 20 42 65 67 69 6e 20  es.    ** Begin 
2c259 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 61  the database sca
2c25a 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 57 49  n.    */.    pWI
2c25b 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65  nfo = sqlite3Whe
2c25c 72 65 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20  reBegin(pParse, 
2c25d 70 54 61 62 4c 69 73 74 2c 20 70 57 68 65 72 65  pTabList, pWhere
2c25e 2c 20 26 70 4f 72 64 65 72 42 79 2c 20 30 29 3b  , &pOrderBy, 0);
2c25f 0a 20 20 20 20 69 66 28 20 70 57 49 6e 66 6f 3d  .    if( pWInfo=
2c260 3d 30 20 29 20 67 6f 74 6f 20 73 65 6c 65 63 74  =0 ) goto select
2c261 5f 65 6e 64 3b 0a 0a 20 20 20 20 2f 2a 20 49 66  _end;..    /* If
2c262 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 74   sorting index t
2c263 68 61 74 20 77 61 73 20 63 72 65 61 74 65 64 20  hat was created 
2c264 62 79 20 61 20 70 72 69 6f 72 20 4f 50 5f 4f 70  by a prior OP_Op
2c265 65 6e 45 70 68 65 6d 65 72 61 6c 20 0a 20 20 20  enEphemeral .   
2c266 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20   ** instruction 
2c267 65 6e 64 65 64 20 75 70 20 6e 6f 74 20 62 65 69  ended up not bei
2c268 6e 67 20 6e 65 65 64 65 64 2c 20 74 68 65 6e 20  ng needed, then 
2c269 63 68 61 6e 67 65 20 74 68 65 20 4f 50 5f 4f 70  change the OP_Op
2c26a 65 6e 45 70 68 65 6d 65 72 61 6c 0a 20 20 20 20  enEphemeral.    
2c26b 2a 2a 20 69 6e 74 6f 20 61 6e 20 4f 50 5f 4e 6f  ** into an OP_No
2c26c 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  op..    */.    i
2c26d 66 28 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78  f( addrSortIndex
2c26e 3e 3d 30 20 26 26 20 70 4f 72 64 65 72 42 79 3d  >=0 && pOrderBy=
2c26f 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
2c270 74 65 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e  te3VdbeChangeToN
2c271 6f 6f 70 28 76 2c 20 61 64 64 72 53 6f 72 74 49  oop(v, addrSortI
2c272 6e 64 65 78 2c 20 31 29 3b 0a 20 20 20 20 20 20  ndex, 1);.      
2c273 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  p->addrOpenEphm[
2c274 32 5d 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 0a  2] = -1;.    }..
2c275 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 73      /* Use the s
2c276 74 61 6e 64 61 72 64 20 69 6e 6e 65 72 20 6c 6f  tandard inner lo
2c277 6f 70 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  op.    */.    as
2c278 73 65 72 74 28 21 69 73 44 69 73 74 69 6e 63 74  sert(!isDistinct
2c279 29 3b 0a 20 20 20 20 73 65 6c 65 63 74 49 6e 6e  );.    selectInn
2c27a 65 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 70  erLoop(pParse, p
2c27b 2c 20 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c 20  , pEList, 0, 0, 
2c27c 70 4f 72 64 65 72 42 79 2c 20 2d 31 2c 20 70 44  pOrderBy, -1, pD
2c27d 65 73 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  est,.           
2c27e 20 20 20 20 20 20 20 20 20 70 57 49 6e 66 6f 2d           pWInfo-
2c27f 3e 69 43 6f 6e 74 69 6e 75 65 2c 20 70 57 49 6e  >iContinue, pWIn
2c280 66 6f 2d 3e 69 42 72 65 61 6b 29 3b 0a 0a 20 20  fo->iBreak);..  
2c281 20 20 2f 2a 20 45 6e 64 20 74 68 65 20 64 61 74    /* End the dat
2c282 61 62 61 73 65 20 73 63 61 6e 20 6c 6f 6f 70 2e  abase scan loop.
2c283 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
2c284 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e  te3WhereEnd(pWIn
2c285 66 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  fo);.  }else{.  
2c286 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65    /* This is the
2c287 20 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20   processing for 
2c288 61 67 67 72 65 67 61 74 65 20 71 75 65 72 69 65  aggregate querie
2c289 73 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f 6e  s */.    NameCon
2c28a 74 65 78 74 20 73 4e 43 3b 20 20 20 20 2f 2a 20  text sNC;    /* 
2c28b 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72  Name context for
2c28c 20 70 72 6f 63 65 73 73 69 6e 67 20 61 67 67 72   processing aggr
2c28d 65 67 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f  egate informatio
2c28e 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 41 4d  n */.    int iAM
2c28f 65 6d 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  em;          /* 
2c290 46 69 72 73 74 20 4d 65 6d 20 61 64 64 72 65 73  First Mem addres
2c291 73 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 63 75  s for storing cu
2c292 72 72 65 6e 74 20 47 52 4f 55 50 20 42 59 20 2a  rrent GROUP BY *
2c293 2f 0a 20 20 20 20 69 6e 74 20 69 42 4d 65 6d 3b  /.    int iBMem;
2c294 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72            /* Fir
2c295 73 74 20 4d 65 6d 20 61 64 64 72 65 73 73 20 66  st Mem address f
2c296 6f 72 20 70 72 65 76 69 6f 75 73 20 47 52 4f 55  or previous GROU
2c297 50 20 42 59 20 2a 2f 0a 20 20 20 20 69 6e 74 20  P BY */.    int 
2c298 69 55 73 65 46 6c 61 67 3b 20 20 20 20 20 20 20  iUseFlag;       
2c299 2f 2a 20 4d 65 6d 20 61 64 64 72 65 73 73 20 68  /* Mem address h
2c29a 6f 6c 64 69 6e 67 20 66 6c 61 67 20 69 6e 64 69  olding flag indi
2c29b 63 61 74 69 6e 67 20 74 68 61 74 20 61 74 20 6c  cating that at l
2c29c 65 61 73 74 0a 20 20 20 20 20 20 20 20 20 20 20  east.           
2c29d 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
2c29e 6f 6e 65 20 72 6f 77 20 6f 66 20 74 68 65 20 69  one row of the i
2c29f 6e 70 75 74 20 74 6f 20 74 68 65 20 61 67 67 72  nput to the aggr
2c2a0 65 67 61 74 6f 72 20 68 61 73 20 62 65 65 6e 0a  egator has been.
2c2a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c2a2 20 20 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65          ** proce
2c2a3 73 73 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ssed */.    int 
2c2a4 69 41 62 6f 72 74 46 6c 61 67 3b 20 20 20 20 20  iAbortFlag;     
2c2a5 2f 2a 20 4d 65 6d 20 61 64 64 72 65 73 73 20 77  /* Mem address w
2c2a6 68 69 63 68 20 63 61 75 73 65 73 20 71 75 65 72  hich causes quer
2c2a7 79 20 61 62 6f 72 74 20 69 66 20 70 6f 73 69 74  y abort if posit
2c2a8 69 76 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 67  ive */.    int g
2c2a9 72 6f 75 70 42 79 53 6f 72 74 3b 20 20 20 20 2f  roupBySort;    /
2c2aa 2a 20 52 6f 77 73 20 63 6f 6d 65 20 66 72 6f 6d  * Rows come from
2c2ab 20 73 6f 75 72 63 65 20 69 6e 20 47 52 4f 55 50   source in GROUP
2c2ac 20 42 59 20 6f 72 64 65 72 20 2a 2f 0a 20 20 20   BY order */.   
2c2ad 20 69 6e 74 20 61 64 64 72 45 6e 64 3b 20 20 20   int addrEnd;   
2c2ae 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 70       /* End of p
2c2af 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 74 68  rocessing for th
2c2b0 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 0a 20 20  is SELECT */..  
2c2b1 20 20 2f 2a 20 52 65 6d 6f 76 65 20 61 6e 79 20    /* Remove any 
2c2b2 61 6e 64 20 61 6c 6c 20 61 6c 69 61 73 65 73 20  and all aliases 
2c2b3 62 65 74 77 65 65 6e 20 74 68 65 20 72 65 73 75  between the resu
2c2b4 6c 74 20 73 65 74 20 61 6e 64 20 74 68 65 0a 20  lt set and the. 
2c2b5 20 20 20 2a 2a 20 47 52 4f 55 50 20 42 59 20 63     ** GROUP BY c
2c2b6 6c 61 75 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  lause..    */.  
2c2b7 20 20 69 66 28 20 70 47 72 6f 75 70 42 79 20 29    if( pGroupBy )
2c2b8 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b 3b 20 20  {.      int k;  
2c2b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c2ba 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
2c2bb 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 73  unter */.      s
2c2bc 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
2c2bd 74 65 6d 20 2a 70 49 74 65 6d 3b 20 20 2f 2a 20  tem *pItem;  /* 
2c2be 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72  For looping over
2c2bf 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 61   expression in a
2c2c0 20 6c 69 73 74 20 2a 2f 0a 0a 20 20 20 20 20 20   list */..      
2c2c1 66 6f 72 28 6b 3d 70 2d 3e 70 45 4c 69 73 74 2d  for(k=p->pEList-
2c2c2 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d 3d 70 2d  >nExpr, pItem=p-
2c2c3 3e 70 45 4c 69 73 74 2d 3e 61 3b 20 6b 3e 30 3b  >pEList->a; k>0;
2c2c4 20 6b 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a   k--, pItem++){.
2c2c5 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69          pItem->i
2c2c6 41 6c 69 61 73 20 3d 20 30 3b 0a 20 20 20 20 20  Alias = 0;.     
2c2c7 20 7d 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d 70   }.      for(k=p
2c2c8 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 2c 20  GroupBy->nExpr, 
2c2c9 70 49 74 65 6d 3d 70 47 72 6f 75 70 42 79 2d 3e  pItem=pGroupBy->
2c2ca 61 3b 20 6b 3e 30 3b 20 6b 2d 2d 2c 20 70 49 74  a; k>0; k--, pIt
2c2cb 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70  em++){.        p
2c2cc 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20 3d 20 30  Item->iAlias = 0
2c2cd 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2c2ce 0a 20 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65  . .    /* Create
2c2cf 20 61 20 6c 61 62 65 6c 20 74 6f 20 6a 75 6d 70   a label to jump
2c2d0 20 74 6f 20 77 68 65 6e 20 77 65 20 77 61 6e 74   to when we want
2c2d1 20 74 6f 20 61 62 6f 72 74 20 74 68 65 20 71 75   to abort the qu
2c2d2 65 72 79 20 2a 2f 0a 20 20 20 20 61 64 64 72 45  ery */.    addrE
2c2d3 6e 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  nd = sqlite3Vdbe
2c2d4 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20  MakeLabel(v);.. 
2c2d5 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 54 4b     /* Convert TK
2c2d6 5f 43 4f 4c 55 4d 4e 20 6e 6f 64 65 73 20 69 6e  _COLUMN nodes in
2c2d7 74 6f 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  to TK_AGG_COLUMN
2c2d8 20 61 6e 64 20 6d 61 6b 65 20 65 6e 74 72 69 65   and make entrie
2c2d9 73 20 69 6e 0a 20 20 20 20 2a 2a 20 73 41 67 67  s in.    ** sAgg
2c2da 49 6e 66 6f 20 66 6f 72 20 61 6c 6c 20 54 4b 5f  Info for all TK_
2c2db 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 6e 6f 64  AGG_FUNCTION nod
2c2dc 65 73 20 69 6e 20 65 78 70 72 65 73 73 69 6f 6e  es in expression
2c2dd 73 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20  s of the.    ** 
2c2de 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
2c2df 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6d 65 6d  ..    */.    mem
2c2e0 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a  set(&sNC, 0, siz
2c2e1 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 20 20 73  eof(sNC));.    s
2c2e2 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72  NC.pParse = pPar
2c2e3 73 65 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 63  se;.    sNC.pSrc
2c2e4 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b  List = pTabList;
2c2e5 0a 20 20 20 20 73 4e 43 2e 70 41 67 67 49 6e 66  .    sNC.pAggInf
2c2e6 6f 20 3d 20 26 73 41 67 67 49 6e 66 6f 3b 0a 20  o = &sAggInfo;. 
2c2e7 20 20 20 73 41 67 67 49 6e 66 6f 2e 6e 53 6f 72     sAggInfo.nSor
2c2e8 74 69 6e 67 43 6f 6c 75 6d 6e 20 3d 20 70 47 72  tingColumn = pGr
2c2e9 6f 75 70 42 79 20 3f 20 70 47 72 6f 75 70 42 79  oupBy ? pGroupBy
2c2ea 2d 3e 6e 45 78 70 72 2b 31 20 3a 20 30 3b 0a 20  ->nExpr+1 : 0;. 
2c2eb 20 20 20 73 41 67 67 49 6e 66 6f 2e 70 47 72 6f     sAggInfo.pGro
2c2ec 75 70 42 79 20 3d 20 70 47 72 6f 75 70 42 79 3b  upBy = pGroupBy;
2c2ed 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2c2ee 41 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 26  AnalyzeAggList(&
2c2ef 73 4e 43 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20  sNC, pEList);.  
2c2f0 20 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61    sqlite3ExprAna
2c2f1 6c 79 7a 65 41 67 67 4c 69 73 74 28 26 73 4e 43  lyzeAggList(&sNC
2c2f2 2c 20 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 20  , pOrderBy);.   
2c2f3 20 69 66 28 20 70 48 61 76 69 6e 67 20 29 7b 0a   if( pHaving ){.
2c2f4 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2c2f5 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74  rAnalyzeAggregat
2c2f6 65 73 28 26 73 4e 43 2c 20 70 48 61 76 69 6e 67  es(&sNC, pHaving
2c2f7 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 41 67  );.    }.    sAg
2c2f8 67 49 6e 66 6f 2e 6e 41 63 63 75 6d 75 6c 61 74  gInfo.nAccumulat
2c2f9 6f 72 20 3d 20 73 41 67 67 49 6e 66 6f 2e 6e 43  or = sAggInfo.nC
2c2fa 6f 6c 75 6d 6e 3b 0a 20 20 20 20 66 6f 72 28 69  olumn;.    for(i
2c2fb 3d 30 3b 20 69 3c 73 41 67 67 49 6e 66 6f 2e 6e  =0; i<sAggInfo.n
2c2fc 46 75 6e 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Func; i++){.    
2c2fd 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
2c2fe 61 73 50 72 6f 70 65 72 74 79 28 73 41 67 67 49  asProperty(sAggI
2c2ff 6e 66 6f 2e 61 46 75 6e 63 5b 69 5d 2e 70 45 78  nfo.aFunc[i].pEx
2c300 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74  pr, EP_xIsSelect
2c301 29 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) );.      sqlit
2c302 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
2c303 4c 69 73 74 28 26 73 4e 43 2c 20 73 41 67 67 49  List(&sNC, sAggI
2c304 6e 66 6f 2e 61 46 75 6e 63 5b 69 5d 2e 70 45 78  nfo.aFunc[i].pEx
2c305 70 72 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a 20 20  pr->x.pList);.  
2c306 20 20 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e    }.    if( db->
2c307 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67  mallocFailed ) g
2c308 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a  oto select_end;.
2c309 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65 73 73 69  .    /* Processi
2c30a 6e 67 20 66 6f 72 20 61 67 67 72 65 67 61 74 65  ng for aggregate
2c30b 73 20 77 69 74 68 20 47 52 4f 55 50 20 42 59 20  s with GROUP BY 
2c30c 69 73 20 76 65 72 79 20 64 69 66 66 65 72 65 6e  is very differen
2c30d 74 20 61 6e 64 0a 20 20 20 20 2a 2a 20 6d 75 63  t and.    ** muc
2c30e 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 65 78 20 74  h more complex t
2c30f 68 61 6e 20 61 67 67 72 65 67 61 74 65 73 20 77  han aggregates w
2c310 69 74 68 6f 75 74 20 61 20 47 52 4f 55 50 20 42  ithout a GROUP B
2c311 59 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  Y..    */.    if
2c312 28 20 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20  ( pGroupBy ){.  
2c313 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65      KeyInfo *pKe
2c314 79 49 6e 66 6f 3b 20 20 2f 2a 20 4b 65 79 69 6e  yInfo;  /* Keyin
2c315 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  g information fo
2c316 72 20 74 68 65 20 67 72 6f 75 70 20 62 79 20 63  r the group by c
2c317 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 69  lause */.      i
2c318 6e 74 20 6a 31 3b 20 20 20 20 20 20 20 20 20 20  nt j1;          
2c319 20 20 20 2f 2a 20 41 2d 76 73 2d 42 20 63 6f 6d     /* A-vs-B com
2c31a 70 61 72 69 73 69 6f 6e 20 6a 75 6d 70 20 2a 2f  parision jump */
2c31b 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 4f  .      int addrO
2c31c 75 74 70 75 74 52 6f 77 3b 20 20 2f 2a 20 53 74  utputRow;  /* St
2c31d 61 72 74 20 6f 66 20 73 75 62 72 6f 75 74 69 6e  art of subroutin
2c31e 65 20 74 68 61 74 20 6f 75 74 70 75 74 73 20 61  e that outputs a
2c31f 20 72 65 73 75 6c 74 20 72 6f 77 20 2a 2f 0a 20   result row */. 
2c320 20 20 20 20 20 69 6e 74 20 72 65 67 4f 75 74 70       int regOutp
2c321 75 74 52 6f 77 3b 20 20 20 2f 2a 20 52 65 74 75  utRow;   /* Retu
2c322 72 6e 20 61 64 64 72 65 73 73 20 72 65 67 69 73  rn address regis
2c323 74 65 72 20 66 6f 72 20 6f 75 74 70 75 74 20 73  ter for output s
2c324 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 20  ubroutine */.   
2c325 20 20 20 69 6e 74 20 61 64 64 72 53 65 74 41 62     int addrSetAb
2c326 6f 72 74 3b 20 20 20 2f 2a 20 53 65 74 20 74 68  ort;   /* Set th
2c327 65 20 61 62 6f 72 74 20 66 6c 61 67 20 61 6e 64  e abort flag and
2c328 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 20 20 20   return */.     
2c329 20 69 6e 74 20 61 64 64 72 54 6f 70 4f 66 4c 6f   int addrTopOfLo
2c32a 6f 70 3b 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74  op;  /* Top of t
2c32b 68 65 20 69 6e 70 75 74 20 6c 6f 6f 70 20 2a 2f  he input loop */
2c32c 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 53  .      int addrS
2c32d 6f 72 74 69 6e 67 49 64 78 3b 20 2f 2a 20 54 68  ortingIdx; /* Th
2c32e 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72  e OP_OpenEphemer
2c32f 61 6c 20 66 6f 72 20 74 68 65 20 73 6f 72 74 69  al for the sorti
2c330 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20  ng index */.    
2c331 20 20 69 6e 74 20 61 64 64 72 52 65 73 65 74 3b    int addrReset;
2c332 20 20 20 20 20 20 2f 2a 20 53 75 62 72 6f 75 74        /* Subrout
2c333 69 6e 65 20 66 6f 72 20 72 65 73 65 74 74 69 6e  ine for resettin
2c334 67 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f  g the accumulato
2c335 72 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72  r */.      int r
2c336 65 67 52 65 73 65 74 3b 20 20 20 20 20 20 20 2f  egReset;       /
2c337 2a 20 52 65 74 75 72 6e 20 61 64 64 72 65 73 73  * Return address
2c338 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 72 65   register for re
2c339 73 65 74 20 73 75 62 72 6f 75 74 69 6e 65 20 2a  set subroutine *
2c33a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  /..      /* If t
2c33b 68 65 72 65 20 69 73 20 61 20 47 52 4f 55 50 20  here is a GROUP 
2c33c 42 59 20 63 6c 61 75 73 65 20 77 65 20 6d 69 67  BY clause we mig
2c33d 68 74 20 6e 65 65 64 20 61 20 73 6f 72 74 69 6e  ht need a sortin
2c33e 67 20 69 6e 64 65 78 20 74 6f 0a 20 20 20 20 20  g index to.     
2c33f 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 69 74   ** implement it
2c340 2e 20 20 41 6c 6c 6f 63 61 74 65 20 74 68 61 74  .  Allocate that
2c341 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 6e   sorting index n
2c342 6f 77 2e 20 20 49 66 20 69 74 20 74 75 72 6e 73  ow.  If it turns
2c343 20 6f 75 74 0a 20 20 20 20 20 20 2a 2a 20 74 68   out.      ** th
2c344 61 74 20 77 65 20 64 6f 20 6e 6f 74 20 6e 65 65  at we do not nee
2c345 64 20 69 74 20 61 66 74 65 72 20 61 6c 6c 2c 20  d it after all, 
2c346 74 68 65 20 4f 70 65 6e 45 70 68 65 6d 65 72 61  the OpenEphemera
2c347 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e 0a 20 20  l instruction.  
2c348 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63      ** will be c
2c349 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20 61 20  onverted into a 
2c34a 4e 6f 6f 70 2e 20 20 0a 20 20 20 20 20 20 2a 2f  Noop.  .      */
2c34b 0a 20 20 20 20 20 20 73 41 67 67 49 6e 66 6f 2e  .      sAggInfo.
2c34c 73 6f 72 74 69 6e 67 49 64 78 20 3d 20 70 50 61  sortingIdx = pPa
2c34d 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20  rse->nTab++;.   
2c34e 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 6b 65     pKeyInfo = ke
2c34f 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73  yInfoFromExprLis
2c350 74 28 70 50 61 72 73 65 2c 20 70 47 72 6f 75 70  t(pParse, pGroup
2c351 42 79 29 3b 0a 20 20 20 20 20 20 61 64 64 72 53  By);.      addrS
2c352 6f 72 74 69 6e 67 49 64 78 20 3d 20 73 71 6c 69  ortingIdx = sqli
2c353 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2c354 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
2c355 6c 2c 20 0a 20 20 20 20 20 20 20 20 20 20 73 41  l, .          sA
2c356 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64  ggInfo.sortingId
2c357 78 2c 20 73 41 67 67 49 6e 66 6f 2e 6e 53 6f 72  x, sAggInfo.nSor
2c358 74 69 6e 67 43 6f 6c 75 6d 6e 2c 20 0a 20 20 20  tingColumn, .   
2c359 20 20 20 20 20 20 20 30 2c 20 28 63 68 61 72 2a         0, (char*
2c35a 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45  )pKeyInfo, P4_KE
2c35b 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a  YINFO_HANDOFF);.
2c35c 0a 20 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61  .      /* Initia
2c35d 6c 69 7a 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61  lize memory loca
2c35e 74 69 6f 6e 73 20 75 73 65 64 20 62 79 20 47 52  tions used by GR
2c35f 4f 55 50 20 42 59 20 61 67 67 72 65 67 61 74 65  OUP BY aggregate
2c360 20 70 72 6f 63 65 73 73 69 6e 67 0a 20 20 20 20   processing.    
2c361 20 20 2a 2f 0a 20 20 20 20 20 20 69 55 73 65 46    */.      iUseF
2c362 6c 61 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  lag = ++pParse->
2c363 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 69 41 62 6f  nMem;.      iAbo
2c364 72 74 46 6c 61 67 20 3d 20 2b 2b 70 50 61 72 73  rtFlag = ++pPars
2c365 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 72  e->nMem;.      r
2c366 65 67 4f 75 74 70 75 74 52 6f 77 20 3d 20 2b 2b  egOutputRow = ++
2c367 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2c368 20 20 20 20 61 64 64 72 4f 75 74 70 75 74 52 6f      addrOutputRo
2c369 77 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  w = sqlite3VdbeM
2c36a 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
2c36b 20 20 20 72 65 67 52 65 73 65 74 20 3d 20 2b 2b     regReset = ++
2c36c 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2c36d 20 20 20 20 61 64 64 72 52 65 73 65 74 20 3d 20      addrReset = 
2c36e 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
2c36f 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 69  abel(v);.      i
2c370 41 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e  AMem = pParse->n
2c371 4d 65 6d 20 2b 20 31 3b 0a 20 20 20 20 20 20 70  Mem + 1;.      p
2c372 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70  Parse->nMem += p
2c373 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72 3b 0a  GroupBy->nExpr;.
2c374 20 20 20 20 20 20 69 42 4d 65 6d 20 3d 20 70 50        iBMem = pP
2c375 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a  arse->nMem + 1;.
2c376 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d        pParse->nM
2c377 65 6d 20 2b 3d 20 70 47 72 6f 75 70 42 79 2d 3e  em += pGroupBy->
2c378 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 73 71 6c  nExpr;.      sql
2c379 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2c37a 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c  , OP_Integer, 0,
2c37b 20 69 41 62 6f 72 74 46 6c 61 67 29 3b 0a 20 20   iAbortFlag);.  
2c37c 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
2c37d 28 76 2c 20 22 63 6c 65 61 72 20 61 62 6f 72 74  (v, "clear abort
2c37e 20 66 6c 61 67 22 29 29 3b 0a 20 20 20 20 20 20   flag"));.      
2c37f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c380 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2c381 20 30 2c 20 69 55 73 65 46 6c 61 67 29 3b 0a 20   0, iUseFlag);. 
2c382 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
2c383 28 28 76 2c 20 22 69 6e 64 69 63 61 74 65 20 61  ((v, "indicate a
2c384 63 63 75 6d 75 6c 61 74 6f 72 20 65 6d 70 74 79  ccumulator empty
2c385 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 42  "));..      /* B
2c386 65 67 69 6e 20 61 20 6c 6f 6f 70 20 74 68 61 74  egin a loop that
2c387 20 77 69 6c 6c 20 65 78 74 72 61 63 74 20 61 6c   will extract al
2c388 6c 20 73 6f 75 72 63 65 20 72 6f 77 73 20 69 6e  l source rows in
2c389 20 47 52 4f 55 50 20 42 59 20 6f 72 64 65 72 2e   GROUP BY order.
2c38a 0a 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20 6d  .      ** This m
2c38b 69 67 68 74 20 69 6e 76 6f 6c 76 65 20 74 77 6f  ight involve two
2c38c 20 73 65 70 61 72 61 74 65 20 6c 6f 6f 70 73 20   separate loops 
2c38d 77 69 74 68 20 61 6e 20 4f 50 5f 53 6f 72 74 20  with an OP_Sort 
2c38e 69 6e 20 62 65 74 77 65 65 6e 2c 20 6f 72 0a 20  in between, or. 
2c38f 20 20 20 20 20 2a 2a 20 69 74 20 6d 69 67 68 74       ** it might
2c390 20 62 65 20 61 20 73 69 6e 67 6c 65 20 6c 6f 6f   be a single loo
2c391 70 20 74 68 61 74 20 75 73 65 73 20 61 6e 20 69  p that uses an i
2c392 6e 64 65 78 20 74 6f 20 65 78 74 72 61 63 74 20  ndex to extract 
2c393 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 20 20 20 20  information.    
2c394 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 69 67 68    ** in the righ
2c395 74 20 6f 72 64 65 72 20 74 6f 20 62 65 67 69 6e  t order to begin
2c396 20 77 69 74 68 2e 0a 20 20 20 20 20 20 2a 2f 0a   with..      */.
2c397 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c398 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
2c399 73 75 62 2c 20 72 65 67 52 65 73 65 74 2c 20 61  sub, regReset, a
2c39a 64 64 72 52 65 73 65 74 29 3b 0a 20 20 20 20 20  ddrReset);.     
2c39b 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65   pWInfo = sqlite
2c39c 33 57 68 65 72 65 42 65 67 69 6e 28 70 50 61 72  3WhereBegin(pPar
2c39d 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70 57  se, pTabList, pW
2c39e 68 65 72 65 2c 20 26 70 47 72 6f 75 70 42 79 2c  here, &pGroupBy,
2c39f 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70   0);.      if( p
2c3a0 57 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20  WInfo==0 ) goto 
2c3a1 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20 20 20  select_end;.    
2c3a2 20 20 69 66 28 20 70 47 72 6f 75 70 42 79 3d 3d    if( pGroupBy==
2c3a3 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  0 ){.        /* 
2c3a4 54 68 65 20 6f 70 74 69 6d 69 7a 65 72 20 69 73  The optimizer is
2c3a5 20 61 62 6c 65 20 74 6f 20 64 65 6c 69 76 65 72   able to deliver
2c3a6 20 72 6f 77 73 20 69 6e 20 67 72 6f 75 70 20 62   rows in group b
2c3a7 79 20 6f 72 64 65 72 20 73 6f 0a 20 20 20 20 20  y order so.     
2c3a8 20 20 20 2a 2a 20 77 65 20 64 6f 20 6e 6f 74 20     ** we do not 
2c3a9 68 61 76 65 20 74 6f 20 73 6f 72 74 2e 20 20 54  have to sort.  T
2c3aa 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  he OP_OpenEpheme
2c3ab 72 61 6c 20 74 61 62 6c 65 20 77 69 6c 6c 20 62  ral table will b
2c3ac 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 61 6e  e.        ** can
2c3ad 63 65 6c 6c 65 64 20 6c 61 74 65 72 20 62 65 63  celled later bec
2c3ae 61 75 73 65 20 77 65 20 73 74 69 6c 6c 20 6e 65  ause we still ne
2c3af 65 64 20 74 6f 20 75 73 65 20 74 68 65 20 70 4b  ed to use the pK
2c3b0 65 79 49 6e 66 6f 0a 20 20 20 20 20 20 20 20 2a  eyInfo.        *
2c3b1 2f 0a 20 20 20 20 20 20 20 20 70 47 72 6f 75 70  /.        pGroup
2c3b2 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70 42 79  By = p->pGroupBy
2c3b3 3b 0a 20 20 20 20 20 20 20 20 67 72 6f 75 70 42  ;.        groupB
2c3b4 79 53 6f 72 74 20 3d 20 30 3b 0a 20 20 20 20 20  ySort = 0;.     
2c3b5 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2c3b6 2f 2a 20 52 6f 77 73 20 61 72 65 20 63 6f 6d 69  /* Rows are comi
2c3b7 6e 67 20 6f 75 74 20 69 6e 20 75 6e 64 65 74 65  ng out in undete
2c3b8 72 6d 69 6e 65 64 20 6f 72 64 65 72 2e 20 20 57  rmined order.  W
2c3b9 65 20 68 61 76 65 20 74 6f 20 70 75 73 68 0a 20  e have to push. 
2c3ba 20 20 20 20 20 20 20 2a 2a 20 65 61 63 68 20 72         ** each r
2c3bb 6f 77 20 69 6e 74 6f 20 61 20 73 6f 72 74 69 6e  ow into a sortin
2c3bc 67 20 69 6e 64 65 78 2c 20 74 65 72 6d 69 6e 61  g index, termina
2c3bd 74 65 20 74 68 65 20 66 69 72 73 74 20 6c 6f 6f  te the first loo
2c3be 70 2c 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  p,.        ** th
2c3bf 65 6e 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65  en loop over the
2c3c0 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 69   sorting index i
2c3c1 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 74 20 74  n order to get t
2c3c2 68 65 20 6f 75 74 70 75 74 0a 20 20 20 20 20 20  he output.      
2c3c3 20 20 2a 2a 20 69 6e 20 73 6f 72 74 65 64 20 6f    ** in sorted o
2c3c4 72 64 65 72 0a 20 20 20 20 20 20 20 20 2a 2f 0a  rder.        */.
2c3c5 20 20 20 20 20 20 20 20 69 6e 74 20 72 65 67 42          int regB
2c3c6 61 73 65 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  ase;.        int
2c3c7 20 72 65 67 52 65 63 6f 72 64 3b 0a 20 20 20 20   regRecord;.    
2c3c8 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20      int nCol;.  
2c3c9 20 20 20 20 20 20 69 6e 74 20 6e 47 72 6f 75 70        int nGroup
2c3ca 42 79 3b 0a 0a 20 20 20 20 20 20 20 20 67 72 6f  By;..        gro
2c3cb 75 70 42 79 53 6f 72 74 20 3d 20 31 3b 0a 20 20  upBySort = 1;.  
2c3cc 20 20 20 20 20 20 6e 47 72 6f 75 70 42 79 20 3d        nGroupBy =
2c3cd 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70 72   pGroupBy->nExpr
2c3ce 3b 0a 20 20 20 20 20 20 20 20 6e 43 6f 6c 20 3d  ;.        nCol =
2c3cf 20 6e 47 72 6f 75 70 42 79 20 2b 20 31 3b 0a 20   nGroupBy + 1;. 
2c3d0 20 20 20 20 20 20 20 6a 20 3d 20 6e 47 72 6f 75         j = nGrou
2c3d1 70 42 79 2b 31 3b 0a 20 20 20 20 20 20 20 20 66  pBy+1;.        f
2c3d2 6f 72 28 69 3d 30 3b 20 69 3c 73 41 67 67 49 6e  or(i=0; i<sAggIn
2c3d3 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29  fo.nColumn; i++)
2c3d4 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
2c3d5 73 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b 69 5d  sAggInfo.aCol[i]
2c3d6 2e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3e 3d  .iSorterColumn>=
2c3d7 6a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  j ){.           
2c3d8 20 6e 43 6f 6c 2b 2b 3b 0a 20 20 20 20 20 20 20   nCol++;.       
2c3d9 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20       j++;.      
2c3da 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
2c3db 20 20 20 20 20 20 20 20 72 65 67 42 61 73 65 20          regBase 
2c3dc 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
2c3dd 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 43  Range(pParse, nC
2c3de 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  ol);.        sql
2c3df 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c 65  ite3ExprCacheCle
2c3e0 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  ar(pParse);.    
2c3e1 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2c3e2 6f 64 65 45 78 70 72 4c 69 73 74 28 70 50 61 72  odeExprList(pPar
2c3e3 73 65 2c 20 70 47 72 6f 75 70 42 79 2c 20 72 65  se, pGroupBy, re
2c3e4 67 42 61 73 65 2c 20 30 29 3b 0a 20 20 20 20 20  gBase, 0);.     
2c3e5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c3e6 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 71 75 65  dOp2(v, OP_Seque
2c3e7 6e 63 65 2c 20 73 41 67 67 49 6e 66 6f 2e 73 6f  nce, sAggInfo.so
2c3e8 72 74 69 6e 67 49 64 78 2c 72 65 67 42 61 73 65  rtingIdx,regBase
2c3e9 2b 6e 47 72 6f 75 70 42 79 29 3b 0a 20 20 20 20  +nGroupBy);.    
2c3ea 20 20 20 20 6a 20 3d 20 6e 47 72 6f 75 70 42 79      j = nGroupBy
2c3eb 2b 31 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  +1;.        for(
2c3ec 69 3d 30 3b 20 69 3c 73 41 67 67 49 6e 66 6f 2e  i=0; i<sAggInfo.
2c3ed 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
2c3ee 20 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20           struct 
2c3ef 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f  AggInfo_col *pCo
2c3f0 6c 20 3d 20 26 73 41 67 67 49 6e 66 6f 2e 61 43  l = &sAggInfo.aC
2c3f1 6f 6c 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20  ol[i];.         
2c3f2 20 69 66 28 20 70 43 6f 6c 2d 3e 69 53 6f 72 74   if( pCol->iSort
2c3f3 65 72 43 6f 6c 75 6d 6e 3e 3d 6a 20 29 7b 0a 20  erColumn>=j ){. 
2c3f4 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 72             int r
2c3f5 31 20 3d 20 6a 20 2b 20 72 65 67 42 61 73 65 3b  1 = j + regBase;
2c3f6 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74  .            int
2c3f7 20 72 32 3b 0a 0a 20 20 20 20 20 20 20 20 20 20   r2;..          
2c3f8 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78    r2 = sqlite3Ex
2c3f9 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28  prCodeGetColumn(
2c3fa 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
2c3fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c3fc 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 70 54          pCol->pT
2c3fd 61 62 2c 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d  ab, pCol->iColum
2c3fe 6e 2c 20 70 43 6f 6c 2d 3e 69 54 61 62 6c 65 2c  n, pCol->iTable,
2c3ff 20 72 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20   r1, 0);.       
2c400 20 20 20 20 20 69 66 28 20 72 31 21 3d 72 32 20       if( r1!=r2 
2c401 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
2c402 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c403 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20  p2(v, OP_SCopy, 
2c404 72 32 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20  r2, r1);.       
2c405 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
2c406 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 20 20     j++;.        
2c407 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
2c408 20 20 20 20 20 20 72 65 67 52 65 63 6f 72 64 20        regRecord 
2c409 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
2c40a 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
2c40b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2c40c 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp3(v, OP_Mak
2c40d 65 52 65 63 6f 72 64 2c 20 72 65 67 42 61 73 65  eRecord, regBase
2c40e 2c 20 6e 43 6f 6c 2c 20 72 65 67 52 65 63 6f 72  , nCol, regRecor
2c40f 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  d);.        sqli
2c410 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2c411 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 73   OP_IdxInsert, s
2c412 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49  AggInfo.sortingI
2c413 64 78 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a  dx, regRecord);.
2c414 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52          sqlite3R
2c415 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
2c416 61 72 73 65 2c 20 72 65 67 52 65 63 6f 72 64 29  arse, regRecord)
2c417 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2c418 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67  3ReleaseTempRang
2c419 65 28 70 50 61 72 73 65 2c 20 72 65 67 42 61 73  e(pParse, regBas
2c41a 65 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20  e, nCol);.      
2c41b 20 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e    sqlite3WhereEn
2c41c 64 28 70 57 49 6e 66 6f 29 3b 0a 20 20 20 20 20  d(pWInfo);.     
2c41d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c41e 64 4f 70 32 28 76 2c 20 4f 50 5f 53 6f 72 74 2c  dOp2(v, OP_Sort,
2c41f 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e   sAggInfo.sortin
2c420 67 49 64 78 2c 20 61 64 64 72 45 6e 64 29 3b 0a  gIdx, addrEnd);.
2c421 20 20 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d          VdbeComm
2c422 65 6e 74 28 28 76 2c 20 22 47 52 4f 55 50 20 42  ent((v, "GROUP B
2c423 59 20 73 6f 72 74 22 29 29 3b 0a 20 20 20 20 20  Y sort"));.     
2c424 20 20 20 73 41 67 67 49 6e 66 6f 2e 75 73 65 53     sAggInfo.useS
2c425 6f 72 74 69 6e 67 49 64 78 20 3d 20 31 3b 0a 20  ortingIdx = 1;. 
2c426 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2c427 70 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61  prCacheClear(pPa
2c428 72 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  rse);.      }.. 
2c429 20 20 20 20 20 2f 2a 20 45 76 61 6c 75 61 74 65       /* Evaluate
2c42a 20 74 68 65 20 63 75 72 72 65 6e 74 20 47 52 4f   the current GRO
2c42b 55 50 20 42 59 20 74 65 72 6d 73 20 61 6e 64 20  UP BY terms and 
2c42c 73 74 6f 72 65 20 69 6e 20 62 30 2c 20 62 31 2c  store in b0, b1,
2c42d 20 62 32 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a 20   b2....      ** 
2c42e 28 62 30 20 69 73 20 6d 65 6d 6f 72 79 20 6c 6f  (b0 is memory lo
2c42f 63 61 74 69 6f 6e 20 69 42 4d 65 6d 2b 30 2c 20  cation iBMem+0, 
2c430 62 31 20 69 73 20 69 42 4d 65 6d 2b 31 2c 20 61  b1 is iBMem+1, a
2c431 6e 64 20 73 6f 20 66 6f 72 74 68 29 0a 20 20 20  nd so forth).   
2c432 20 20 20 2a 2a 20 54 68 65 6e 20 63 6f 6d 70 61     ** Then compa
2c433 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 47  re the current G
2c434 52 4f 55 50 20 42 59 20 74 65 72 6d 73 20 61 67  ROUP BY terms ag
2c435 61 69 6e 73 74 20 74 68 65 20 47 52 4f 55 50 20  ainst the GROUP 
2c436 42 59 20 74 65 72 6d 73 0a 20 20 20 20 20 20 2a  BY terms.      *
2c437 2a 20 66 72 6f 6d 20 74 68 65 20 70 72 65 76 69  * from the previ
2c438 6f 75 73 20 72 6f 77 20 63 75 72 72 65 6e 74 6c  ous row currentl
2c439 79 20 73 74 6f 72 65 64 20 69 6e 20 61 30 2c 20  y stored in a0, 
2c43a 61 31 2c 20 61 32 2e 2e 2e 0a 20 20 20 20 20 20  a1, a2....      
2c43b 2a 2f 0a 20 20 20 20 20 20 61 64 64 72 54 6f 70  */.      addrTop
2c43c 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33  OfLoop = sqlite3
2c43d 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
2c43e 76 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  v);.      sqlite
2c43f 33 45 78 70 72 43 61 63 68 65 43 6c 65 61 72 28  3ExprCacheClear(
2c440 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 66  pParse);.      f
2c441 6f 72 28 6a 3d 30 3b 20 6a 3c 70 47 72 6f 75 70  or(j=0; j<pGroup
2c442 42 79 2d 3e 6e 45 78 70 72 3b 20 6a 2b 2b 29 7b  By->nExpr; j++){
2c443 0a 20 20 20 20 20 20 20 20 69 66 28 20 67 72 6f  .        if( gro
2c444 75 70 42 79 53 6f 72 74 20 29 7b 0a 20 20 20 20  upBySort ){.    
2c445 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c446 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
2c447 6c 75 6d 6e 2c 20 73 41 67 67 49 6e 66 6f 2e 73  lumn, sAggInfo.s
2c448 6f 72 74 69 6e 67 49 64 78 2c 20 6a 2c 20 69 42  ortingIdx, j, iB
2c449 4d 65 6d 2b 6a 29 3b 0a 20 20 20 20 20 20 20 20  Mem+j);.        
2c44a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
2c44b 20 73 41 67 67 49 6e 66 6f 2e 64 69 72 65 63 74   sAggInfo.direct
2c44c 4d 6f 64 65 20 3d 20 31 3b 0a 20 20 20 20 20 20  Mode = 1;.      
2c44d 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
2c44e 6f 64 65 28 70 50 61 72 73 65 2c 20 70 47 72 6f  ode(pParse, pGro
2c44f 75 70 42 79 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72  upBy->a[j].pExpr
2c450 2c 20 69 42 4d 65 6d 2b 6a 29 3b 0a 20 20 20 20  , iBMem+j);.    
2c451 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
2c452 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2c453 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6d 70  ddOp4(v, OP_Comp
2c454 61 72 65 2c 20 69 41 4d 65 6d 2c 20 69 42 4d 65  are, iAMem, iBMe
2c455 6d 2c 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78  m, pGroupBy->nEx
2c456 70 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  pr,.            
2c457 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63                (c
2c458 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50  har*)pKeyInfo, P
2c459 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 20 20  4_KEYINFO);.    
2c45a 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64    j1 = sqlite3Vd
2c45b 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
2c45c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2c45d 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2c45e 4a 75 6d 70 2c 20 6a 31 2b 31 2c 20 30 2c 20 6a  Jump, j1+1, 0, j
2c45f 31 2b 31 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  1+1);..      /* 
2c460 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
2c461 61 74 20 72 75 6e 73 20 77 68 65 6e 65 76 65 72  at runs whenever
2c462 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 68   the GROUP BY ch
2c463 61 6e 67 65 73 2e 0a 20 20 20 20 20 20 2a 2a 20  anges..      ** 
2c464 43 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 47  Changes in the G
2c465 52 4f 55 50 20 42 59 20 61 72 65 20 64 65 74 65  ROUP BY are dete
2c466 63 74 65 64 20 62 79 20 74 68 65 20 70 72 65 76  cted by the prev
2c467 69 6f 75 73 20 63 6f 64 65 0a 20 20 20 20 20 20  ious code.      
2c468 2a 2a 20 62 6c 6f 63 6b 2e 20 20 49 66 20 74 68  ** block.  If th
2c469 65 72 65 20 77 65 72 65 20 6e 6f 20 63 68 61 6e  ere were no chan
2c46a 67 65 73 2c 20 74 68 69 73 20 62 6c 6f 63 6b 20  ges, this block 
2c46b 69 73 20 73 6b 69 70 70 65 64 2e 0a 20 20 20 20  is skipped..    
2c46c 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68    **.      ** Th
2c46d 69 73 20 63 6f 64 65 20 63 6f 70 69 65 73 20 63  is code copies c
2c46e 75 72 72 65 6e 74 20 67 72 6f 75 70 20 62 79 20  urrent group by 
2c46f 74 65 72 6d 73 20 69 6e 20 62 30 2c 62 31 2c 62  terms in b0,b1,b
2c470 32 2c 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a 20 6f  2,....      ** o
2c471 76 65 72 20 74 6f 20 61 30 2c 61 31 2c 61 32 2e  ver to a0,a1,a2.
2c472 20 20 49 74 20 74 68 65 6e 20 63 61 6c 6c 73 20    It then calls 
2c473 74 68 65 20 6f 75 74 70 75 74 20 73 75 62 72 6f  the output subro
2c474 75 74 69 6e 65 0a 20 20 20 20 20 20 2a 2a 20 61  utine.      ** a
2c475 6e 64 20 72 65 73 65 74 73 20 74 68 65 20 61 67  nd resets the ag
2c476 67 72 65 67 61 74 65 20 61 63 63 75 6d 75 6c 61  gregate accumula
2c477 74 6f 72 20 72 65 67 69 73 74 65 72 73 20 69 6e  tor registers in
2c478 20 70 72 65 70 61 72 61 74 69 6f 6e 0a 20 20 20   preparation.   
2c479 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 6e 65     ** for the ne
2c47a 78 74 20 47 52 4f 55 50 20 42 59 20 62 61 74 63  xt GROUP BY batc
2c47b 68 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  h..      */.    
2c47c 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
2c47d 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 69 42  eMove(pParse, iB
2c47e 4d 65 6d 2c 20 69 41 4d 65 6d 2c 20 70 47 72 6f  Mem, iAMem, pGro
2c47f 75 70 42 79 2d 3e 6e 45 78 70 72 29 3b 0a 20 20  upBy->nExpr);.  
2c480 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2c481 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75  ddOp2(v, OP_Gosu
2c482 62 2c 20 72 65 67 4f 75 74 70 75 74 52 6f 77 2c  b, regOutputRow,
2c483 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 29 3b   addrOutputRow);
2c484 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
2c485 6e 74 28 28 76 2c 20 22 6f 75 74 70 75 74 20 6f  nt((v, "output o
2c486 6e 65 20 72 6f 77 22 29 29 3b 0a 20 20 20 20 20  ne row"));.     
2c487 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c488 70 32 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20  p2(v, OP_IfPos, 
2c489 69 41 62 6f 72 74 46 6c 61 67 2c 20 61 64 64 72  iAbortFlag, addr
2c48a 45 6e 64 29 3b 0a 20 20 20 20 20 20 56 64 62 65  End);.      Vdbe
2c48b 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 63 68 65  Comment((v, "che
2c48c 63 6b 20 61 62 6f 72 74 20 66 6c 61 67 22 29 29  ck abort flag"))
2c48d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2c48e 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c48f 47 6f 73 75 62 2c 20 72 65 67 52 65 73 65 74 2c  Gosub, regReset,
2c490 20 61 64 64 72 52 65 73 65 74 29 3b 0a 20 20 20   addrReset);.   
2c491 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
2c492 76 2c 20 22 72 65 73 65 74 20 61 63 63 75 6d 75  v, "reset accumu
2c493 6c 61 74 6f 72 22 29 29 3b 0a 0a 20 20 20 20 20  lator"));..     
2c494 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 61   /* Update the a
2c495 67 67 72 65 67 61 74 65 20 61 63 63 75 6d 75 6c  ggregate accumul
2c496 61 74 6f 72 73 20 62 61 73 65 64 20 6f 6e 20 74  ators based on t
2c497 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 20 20  he content of.  
2c498 20 20 20 20 2a 2a 20 74 68 65 20 63 75 72 72 65      ** the curre
2c499 6e 74 20 72 6f 77 0a 20 20 20 20 20 20 2a 2f 0a  nt row.      */.
2c49a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c49b 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29  eJumpHere(v, j1)
2c49c 3b 0a 20 20 20 20 20 20 75 70 64 61 74 65 41 63  ;.      updateAc
2c49d 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72 73 65  cumulator(pParse
2c49e 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20  , &sAggInfo);.  
2c49f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2c4a0 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
2c4a1 67 65 72 2c 20 31 2c 20 69 55 73 65 46 6c 61 67  ger, 1, iUseFlag
2c4a2 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
2c4a3 6d 65 6e 74 28 28 76 2c 20 22 69 6e 64 69 63 61  ment((v, "indica
2c4a4 74 65 20 64 61 74 61 20 69 6e 20 61 63 63 75 6d  te data in accum
2c4a5 75 6c 61 74 6f 72 22 29 29 3b 0a 0a 20 20 20 20  ulator"));..    
2c4a6 20 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20    /* End of the 
2c4a7 6c 6f 6f 70 0a 20 20 20 20 20 20 2a 2f 0a 20 20  loop.      */.  
2c4a8 20 20 20 20 69 66 28 20 67 72 6f 75 70 42 79 53      if( groupByS
2c4a9 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ort ){.        s
2c4aa 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2c4ab 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 73 41 67  (v, OP_Next, sAg
2c4ac 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78  gInfo.sortingIdx
2c4ad 2c 20 61 64 64 72 54 6f 70 4f 66 4c 6f 6f 70 29  , addrTopOfLoop)
2c4ae 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2c4af 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 68         sqlite3Wh
2c4b0 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a  ereEnd(pWInfo);.
2c4b1 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2c4b2 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28  dbeChangeToNoop(
2c4b3 76 2c 20 61 64 64 72 53 6f 72 74 69 6e 67 49 64  v, addrSortingId
2c4b4 78 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 0a  x, 1);.      }..
2c4b5 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20        /* Output 
2c4b6 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 20 6f 66  the final row of
2c4b7 20 72 65 73 75 6c 74 0a 20 20 20 20 20 20 2a 2f   result.      */
2c4b8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2c4b9 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
2c4ba 6f 73 75 62 2c 20 72 65 67 4f 75 74 70 75 74 52  osub, regOutputR
2c4bb 6f 77 2c 20 61 64 64 72 4f 75 74 70 75 74 52 6f  ow, addrOutputRo
2c4bc 77 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f  w);.      VdbeCo
2c4bd 6d 6d 65 6e 74 28 28 76 2c 20 22 6f 75 74 70 75  mment((v, "outpu
2c4be 74 20 66 69 6e 61 6c 20 72 6f 77 22 29 29 3b 0a  t final row"));.
2c4bf 0a 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 6f  .      /* Jump o
2c4c0 76 65 72 20 74 68 65 20 73 75 62 72 6f 75 74 69  ver the subrouti
2c4c1 6e 65 73 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  nes.      */.   
2c4c2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2c4c3 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
2c4c4 20 30 2c 20 61 64 64 72 45 6e 64 29 3b 0a 0a 20   0, addrEnd);.. 
2c4c5 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
2c4c6 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68   a subroutine th
2c4c7 61 74 20 6f 75 74 70 75 74 73 20 61 20 73 69 6e  at outputs a sin
2c4c8 67 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 20 72  gle row of the r
2c4c9 65 73 75 6c 74 0a 20 20 20 20 20 20 2a 2a 20 73  esult.      ** s
2c4ca 65 74 2e 20 20 54 68 69 73 20 73 75 62 72 6f 75  et.  This subrou
2c4cb 74 69 6e 65 20 66 69 72 73 74 20 6c 6f 6f 6b 73  tine first looks
2c4cc 20 61 74 20 74 68 65 20 69 55 73 65 46 6c 61 67   at the iUseFlag
2c4cd 2e 20 20 49 66 20 69 55 73 65 46 6c 61 67 0a 20  .  If iUseFlag. 
2c4ce 20 20 20 20 20 2a 2a 20 69 73 20 6c 65 73 73 20       ** is less 
2c4cf 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
2c4d0 20 7a 65 72 6f 2c 20 74 68 65 20 73 75 62 72 6f   zero, the subro
2c4d1 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
2c4d2 2e 20 20 49 66 0a 20 20 20 20 20 20 2a 2a 20 74  .  If.      ** t
2c4d3 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20 63 61  he processing ca
2c4d4 6c 6c 73 20 66 6f 72 20 74 68 65 20 71 75 65 72  lls for the quer
2c4d5 79 20 74 6f 20 61 62 6f 72 74 2c 20 74 68 69 73  y to abort, this
2c4d6 20 73 75 62 72 6f 75 74 69 6e 65 0a 20 20 20 20   subroutine.    
2c4d7 20 20 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 73 20    ** increments 
2c4d8 74 68 65 20 69 41 62 6f 72 74 46 6c 61 67 20 6d  the iAbortFlag m
2c4d9 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 62  emory location b
2c4da 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20  efore returning 
2c4db 69 6e 0a 20 20 20 20 20 20 2a 2a 20 6f 72 64 65  in.      ** orde
2c4dc 72 20 74 6f 20 73 69 67 6e 61 6c 20 74 68 65 20  r to signal the 
2c4dd 63 61 6c 6c 65 72 20 74 6f 20 61 62 6f 72 74 2e  caller to abort.
2c4de 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2c4df 61 64 64 72 53 65 74 41 62 6f 72 74 20 3d 20 73  addrSetAbort = s
2c4e0 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
2c4e1 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20  tAddr(v);.      
2c4e2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2c4e3 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
2c4e4 20 31 2c 20 69 41 62 6f 72 74 46 6c 61 67 29 3b   1, iAbortFlag);
2c4e5 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
2c4e6 6e 74 28 28 76 2c 20 22 73 65 74 20 61 62 6f 72  nt((v, "set abor
2c4e7 74 20 66 6c 61 67 22 29 29 3b 0a 20 20 20 20 20  t flag"));.     
2c4e8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c4e9 70 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c  p1(v, OP_Return,
2c4ea 20 72 65 67 4f 75 74 70 75 74 52 6f 77 29 3b 0a   regOutputRow);.
2c4eb 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2c4ec 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2c4ed 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 29 3b   addrOutputRow);
2c4ee 0a 20 20 20 20 20 20 61 64 64 72 4f 75 74 70 75  .      addrOutpu
2c4ef 74 52 6f 77 20 3d 20 73 71 6c 69 74 65 33 56 64  tRow = sqlite3Vd
2c4f0 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
2c4f1 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2c4f2 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2c4f3 49 66 50 6f 73 2c 20 69 55 73 65 46 6c 61 67 2c  IfPos, iUseFlag,
2c4f4 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 2b 32   addrOutputRow+2
2c4f5 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
2c4f6 6d 65 6e 74 28 28 76 2c 20 22 47 72 6f 75 70 62  ment((v, "Groupb
2c4f7 79 20 72 65 73 75 6c 74 20 67 65 6e 65 72 61 74  y result generat
2c4f8 6f 72 20 65 6e 74 72 79 20 70 6f 69 6e 74 22 29  or entry point")
2c4f9 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2c4fa 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2c4fb 5f 52 65 74 75 72 6e 2c 20 72 65 67 4f 75 74 70  _Return, regOutp
2c4fc 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 66 69  utRow);.      fi
2c4fd 6e 61 6c 69 7a 65 41 67 67 46 75 6e 63 74 69 6f  nalizeAggFunctio
2c4fe 6e 73 28 70 50 61 72 73 65 2c 20 26 73 41 67 67  ns(pParse, &sAgg
2c4ff 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 73 71 6c  Info);.      sql
2c500 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
2c501 70 50 61 72 73 65 2c 20 70 48 61 76 69 6e 67 2c  pParse, pHaving,
2c502 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 2b 31   addrOutputRow+1
2c503 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  , SQLITE_JUMPIFN
2c504 55 4c 4c 29 3b 0a 20 20 20 20 20 20 73 65 6c 65  ULL);.      sele
2c505 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72  ctInnerLoop(pPar
2c506 73 65 2c 20 70 2c 20 70 2d 3e 70 45 4c 69 73 74  se, p, p->pEList
2c507 2c 20 30 2c 20 30 2c 20 70 4f 72 64 65 72 42 79  , 0, 0, pOrderBy
2c508 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2c509 20 20 20 20 20 20 20 20 64 69 73 74 69 6e 63 74          distinct
2c50a 2c 20 70 44 65 73 74 2c 0a 20 20 20 20 20 20 20  , pDest,.       
2c50b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
2c50c 64 64 72 4f 75 74 70 75 74 52 6f 77 2b 31 2c 20  ddrOutputRow+1, 
2c50d 61 64 64 72 53 65 74 41 62 6f 72 74 29 3b 0a 20  addrSetAbort);. 
2c50e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2c50f 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74  AddOp1(v, OP_Ret
2c510 75 72 6e 2c 20 72 65 67 4f 75 74 70 75 74 52 6f  urn, regOutputRo
2c511 77 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f  w);.      VdbeCo
2c512 6d 6d 65 6e 74 28 28 76 2c 20 22 65 6e 64 20 67  mment((v, "end g
2c513 72 6f 75 70 62 79 20 72 65 73 75 6c 74 20 67 65  roupby result ge
2c514 6e 65 72 61 74 6f 72 22 29 29 3b 0a 0a 20 20 20  nerator"));..   
2c515 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61     /* Generate a
2c516 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74   subroutine that
2c517 20 77 69 6c 6c 20 72 65 73 65 74 20 74 68 65 20   will reset the 
2c518 67 72 6f 75 70 2d 62 79 20 61 63 63 75 6d 75 6c  group-by accumul
2c519 61 74 6f 72 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ator.      */.  
2c51a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52      sqlite3VdbeR
2c51b 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61  esolveLabel(v, a
2c51c 64 64 72 52 65 73 65 74 29 3b 0a 20 20 20 20 20  ddrReset);.     
2c51d 20 72 65 73 65 74 41 63 63 75 6d 75 6c 61 74 6f   resetAccumulato
2c51e 72 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49  r(pParse, &sAggI
2c51f 6e 66 6f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  nfo);.      sqli
2c520 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
2c521 20 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65 67 52   OP_Return, regR
2c522 65 73 65 74 29 3b 0a 20 20 20 20 20 0a 20 20 20  eset);.     .   
2c523 20 7d 20 2f 2a 20 65 6e 64 69 66 20 70 47 72 6f   } /* endif pGro
2c524 75 70 42 79 2e 20 20 42 65 67 69 6e 20 61 67 67  upBy.  Begin agg
2c525 72 65 67 61 74 65 20 71 75 65 72 69 65 73 20 77  regate queries w
2c526 69 74 68 6f 75 74 20 47 52 4f 55 50 20 42 59 3a  ithout GROUP BY:
2c527 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20 7b 0a 20   */.    else {. 
2c528 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70       ExprList *p
2c529 44 65 6c 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66  Del = 0;.#ifndef
2c52a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 54 52   SQLITE_OMIT_BTR
2c52b 45 45 43 4f 55 4e 54 0a 20 20 20 20 20 20 54 61  EECOUNT.      Ta
2c52c 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 20  ble *pTab;.     
2c52d 20 69 66 28 20 28 70 54 61 62 20 3d 20 69 73 53   if( (pTab = isS
2c52e 69 6d 70 6c 65 43 6f 75 6e 74 28 70 2c 20 26 73  impleCount(p, &s
2c52f 41 67 67 49 6e 66 6f 29 29 21 3d 30 20 29 7b 0a  AggInfo))!=0 ){.
2c530 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 69 73          /* If is
2c531 53 69 6d 70 6c 65 43 6f 75 6e 74 28 29 20 72 65  SimpleCount() re
2c532 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20  turns a pointer 
2c533 74 6f 20 61 20 54 61 62 6c 65 20 73 74 72 75 63  to a Table struc
2c534 74 75 72 65 2c 20 74 68 65 6e 0a 20 20 20 20 20  ture, then.     
2c535 20 20 20 2a 2a 20 74 68 65 20 53 51 4c 20 73 74     ** the SQL st
2c536 61 74 65 6d 65 6e 74 20 69 73 20 6f 66 20 74 68  atement is of th
2c537 65 20 66 6f 72 6d 3a 0a 20 20 20 20 20 20 20 20  e form:.        
2c538 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  **.        **   
2c539 53 45 4c 45 43 54 20 63 6f 75 6e 74 28 2a 29 20  SELECT count(*) 
2c53a 46 52 4f 4d 20 3c 74 62 6c 3e 0a 20 20 20 20 20  FROM <tbl>.     
2c53b 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
2c53c 20 77 68 65 72 65 20 74 68 65 20 54 61 62 6c 65   where the Table
2c53d 20 73 74 72 75 63 74 75 72 65 20 72 65 74 75 72   structure retur
2c53e 6e 65 64 20 72 65 70 72 65 73 65 6e 74 73 20 74  ned represents t
2c53f 61 62 6c 65 20 3c 74 62 6c 3e 2e 0a 20 20 20 20  able <tbl>..    
2c540 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
2c541 2a 20 54 68 69 73 20 73 74 61 74 65 6d 65 6e 74  * This statement
2c542 20 69 73 20 73 6f 20 63 6f 6d 6d 6f 6e 20 74 68   is so common th
2c543 61 74 20 69 74 20 69 73 20 6f 70 74 69 6d 69 7a  at it is optimiz
2c544 65 64 20 73 70 65 63 69 61 6c 6c 79 2e 20 54 68  ed specially. Th
2c545 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 50 5f  e.        ** OP_
2c546 43 6f 75 6e 74 20 69 6e 73 74 72 75 63 74 69 6f  Count instructio
2c547 6e 20 69 73 20 65 78 65 63 75 74 65 64 20 65 69  n is executed ei
2c548 74 68 65 72 20 6f 6e 20 74 68 65 20 69 6e 74 6b  ther on the intk
2c549 65 79 20 74 61 62 6c 65 20 74 68 61 74 0a 20 20  ey table that.  
2c54a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e        ** contain
2c54b 73 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74  s the data for t
2c54c 61 62 6c 65 20 3c 74 62 6c 3e 20 6f 72 20 6f 6e  able <tbl> or on
2c54d 20 6f 6e 65 20 6f 66 20 69 74 73 20 69 6e 64 65   one of its inde
2c54e 78 65 73 2e 20 49 74 0a 20 20 20 20 20 20 20 20  xes. It.        
2c54f 2a 2a 20 69 73 20 62 65 74 74 65 72 20 74 6f 20  ** is better to 
2c550 65 78 65 63 75 74 65 20 74 68 65 20 6f 70 20 6f  execute the op o
2c551 6e 20 61 6e 20 69 6e 64 65 78 2c 20 61 73 20 69  n an index, as i
2c552 6e 64 65 78 65 73 20 61 72 65 20 61 6c 6d 6f 73  ndexes are almos
2c553 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c 77  t.        ** alw
2c554 61 79 73 20 73 70 72 65 61 64 20 61 63 72 6f 73  ays spread acros
2c555 73 20 6c 65 73 73 20 70 61 67 65 73 20 74 68 61  s less pages tha
2c556 6e 20 74 68 65 69 72 20 63 6f 72 72 65 73 70 6f  n their correspo
2c557 6e 64 69 6e 67 20 74 61 62 6c 65 73 2e 0a 20 20  nding tables..  
2c558 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
2c559 20 63 6f 6e 73 74 20 69 6e 74 20 69 44 62 20 3d   const int iDb =
2c55a 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
2c55b 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
2c55c 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29  , pTab->pSchema)
2c55d 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20  ;.        const 
2c55e 69 6e 74 20 69 43 73 72 20 3d 20 70 50 61 72 73  int iCsr = pPars
2c55f 65 2d 3e 6e 54 61 62 2b 2b 3b 20 20 20 20 20 2f  e->nTab++;     /
2c560 2a 20 43 75 72 73 6f 72 20 74 6f 20 73 63 61 6e  * Cursor to scan
2c561 20 62 2d 74 72 65 65 20 2a 2f 0a 20 20 20 20 20   b-tree */.     
2c562 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20     Index *pIdx; 
2c563 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c564 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65 72 61          /* Itera
2c565 74 6f 72 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a  tor variable */.
2c566 20 20 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20          KeyInfo 
2c567 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 30 3b 20 20  *pKeyInfo = 0;  
2c568 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c569 4b 65 79 69 6e 66 6f 20 66 6f 72 20 73 63 61 6e  Keyinfo for scan
2c56a 6e 65 64 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20  ned index */.   
2c56b 20 20 20 20 20 49 6e 64 65 78 20 2a 70 42 65 73       Index *pBes
2c56c 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
2c56d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65 73            /* Bes
2c56e 74 20 69 6e 64 65 78 20 66 6f 75 6e 64 20 73 6f  t index found so
2c56f 20 66 61 72 20 2a 2f 0a 20 20 20 20 20 20 20 20   far */.        
2c570 69 6e 74 20 69 52 6f 6f 74 20 3d 20 70 54 61 62  int iRoot = pTab
2c571 2d 3e 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 20  ->tnum;         
2c572 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67       /* Root pag
2c573 65 20 6f 66 20 73 63 61 6e 6e 65 64 20 62 2d 74  e of scanned b-t
2c574 72 65 65 20 2a 2f 0a 0a 20 20 20 20 20 20 20 20  ree */..        
2c575 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66  sqlite3CodeVerif
2c576 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20  ySchema(pParse, 
2c577 69 44 62 29 3b 0a 20 20 20 20 20 20 20 20 73 71  iDb);.        sq
2c578 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70  lite3TableLock(p
2c579 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62  Parse, iDb, pTab
2c57a 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 61 62 2d  ->tnum, 0, pTab-
2c57b 3e 7a 4e 61 6d 65 29 3b 0a 0a 20 20 20 20 20 20  >zName);..      
2c57c 20 20 2f 2a 20 53 65 61 72 63 68 20 66 6f 72 20    /* Search for 
2c57d 74 68 65 20 69 6e 64 65 78 20 74 68 61 74 20 68  the index that h
2c57e 61 73 20 74 68 65 20 6c 65 61 73 74 20 61 6d 6f  as the least amo
2c57f 75 6e 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 2e 20  unt of columns. 
2c580 49 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  If.        ** th
2c581 65 72 65 20 69 73 20 73 75 63 68 20 61 6e 20 69  ere is such an i
2c582 6e 64 65 78 2c 20 61 6e 64 20 69 74 20 68 61 73  ndex, and it has
2c583 20 6c 65 73 73 20 63 6f 6c 75 6d 6e 73 20 74 68   less columns th
2c584 61 6e 20 74 68 65 20 74 61 62 6c 65 0a 20 20 20  an the table.   
2c585 20 20 20 20 20 2a 2a 20 64 6f 65 73 2c 20 74 68       ** does, th
2c586 65 6e 20 77 65 20 63 61 6e 20 61 73 73 75 6d 65  en we can assume
2c587 20 74 68 61 74 20 69 74 20 63 6f 6e 73 75 6d 65   that it consume
2c588 73 20 6c 65 73 73 20 73 70 61 63 65 20 6f 6e 20  s less space on 
2c589 64 69 73 6b 20 61 6e 64 0a 20 20 20 20 20 20 20  disk and.       
2c58a 20 2a 2a 20 77 69 6c 6c 20 74 68 65 72 65 66 6f   ** will therefo
2c58b 72 65 20 62 65 20 63 68 65 61 70 65 72 20 74 6f  re be cheaper to
2c58c 20 73 63 61 6e 20 74 6f 20 64 65 74 65 72 6d 69   scan to determi
2c58d 6e 65 20 74 68 65 20 71 75 65 72 79 20 72 65 73  ne the query res
2c58e 75 6c 74 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20  ult..        ** 
2c58f 49 6e 20 74 68 69 73 20 63 61 73 65 20 73 65 74  In this case set
2c590 20 69 52 6f 6f 74 20 74 6f 20 74 68 65 20 72 6f   iRoot to the ro
2c591 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 6f  ot page number o
2c592 66 20 74 68 65 20 69 6e 64 65 78 20 62 2d 74 72  f the index b-tr
2c593 65 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e  ee.        ** an
2c594 64 20 70 4b 65 79 49 6e 66 6f 20 74 6f 20 74 68  d pKeyInfo to th
2c595 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74  e KeyInfo struct
2c596 75 72 65 20 72 65 71 75 69 72 65 64 20 74 6f 20  ure required to 
2c597 6e 61 76 69 67 61 74 65 20 74 68 65 0a 20 20 20  navigate the.   
2c598 20 20 20 20 20 2a 2a 20 69 6e 64 65 78 2e 0a 20       ** index.. 
2c599 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
2c59a 20 20 2a 2a 20 49 6e 20 70 72 61 63 74 69 63 65    ** In practice
2c59b 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74 72   the KeyInfo str
2c59c 75 63 74 75 72 65 20 77 69 6c 6c 20 6e 6f 74 20  ucture will not 
2c59d 62 65 20 75 73 65 64 2e 20 49 74 20 69 73 20 6f  be used. It is o
2c59e 6e 6c 79 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  nly .        ** 
2c59f 70 61 73 73 65 64 20 74 6f 20 6b 65 65 70 20 4f  passed to keep O
2c5a0 50 5f 4f 70 65 6e 52 65 61 64 20 68 61 70 70 79  P_OpenRead happy
2c5a1 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
2c5a2 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54       for(pIdx=pT
2c5a3 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78  ab->pIndex; pIdx
2c5a4 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65  ; pIdx=pIdx->pNe
2c5a5 78 74 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  xt){.          i
2c5a6 66 28 20 21 70 42 65 73 74 20 7c 7c 20 70 49 64  f( !pBest || pId
2c5a7 78 2d 3e 6e 43 6f 6c 75 6d 6e 3c 70 42 65 73 74  x->nColumn<pBest
2c5a8 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->nColumn ){.   
2c5a9 20 20 20 20 20 20 20 20 20 70 42 65 73 74 20 3d           pBest =
2c5aa 20 70 49 64 78 3b 0a 20 20 20 20 20 20 20 20 20   pIdx;.         
2c5ab 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
2c5ac 20 20 20 20 20 69 66 28 20 70 42 65 73 74 20 26       if( pBest &
2c5ad 26 20 70 42 65 73 74 2d 3e 6e 43 6f 6c 75 6d 6e  & pBest->nColumn
2c5ae 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20  <pTab->nCol ){. 
2c5af 20 20 20 20 20 20 20 20 20 69 52 6f 6f 74 20 3d           iRoot =
2c5b0 20 70 42 65 73 74 2d 3e 74 6e 75 6d 3b 0a 20 20   pBest->tnum;.  
2c5b1 20 20 20 20 20 20 20 20 70 4b 65 79 49 6e 66 6f          pKeyInfo
2c5b2 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b   = sqlite3IndexK
2c5b3 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70  eyinfo(pParse, p
2c5b4 42 65 73 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  Best);.        }
2c5b5 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 65  ..        /* Ope
2c5b6 6e 20 61 20 72 65 61 64 2d 6f 6e 6c 79 20 63 75  n a read-only cu
2c5b7 72 73 6f 72 2c 20 65 78 65 63 75 74 65 20 74 68  rsor, execute th
2c5b8 65 20 4f 50 5f 43 6f 75 6e 74 2c 20 63 6c 6f 73  e OP_Count, clos
2c5b9 65 20 74 68 65 20 63 75 72 73 6f 72 2e 20 2a 2f  e the cursor. */
2c5ba 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2c5bb 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2c5bc 5f 4f 70 65 6e 52 65 61 64 2c 20 69 43 73 72 2c  _OpenRead, iCsr,
2c5bd 20 69 52 6f 6f 74 2c 20 69 44 62 29 3b 0a 20 20   iRoot, iDb);.  
2c5be 20 20 20 20 20 20 69 66 28 20 70 4b 65 79 49 6e        if( pKeyIn
2c5bf 66 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  fo ){.          
2c5c0 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
2c5c1 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 68 61 72  eP4(v, -1, (char
2c5c2 20 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34 5f   *)pKeyInfo, P4_
2c5c3 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29  KEYINFO_HANDOFF)
2c5c4 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2c5c5 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2c5c6 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 75 6e  ddOp2(v, OP_Coun
2c5c7 74 2c 20 69 43 73 72 2c 20 73 41 67 67 49 6e 66  t, iCsr, sAggInf
2c5c8 6f 2e 61 46 75 6e 63 5b 30 5d 2e 69 4d 65 6d 29  o.aFunc[0].iMem)
2c5c9 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2c5ca 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2c5cb 50 5f 43 6c 6f 73 65 2c 20 69 43 73 72 29 3b 0a  P_Close, iCsr);.
2c5cc 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64        }else.#end
2c5cd 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
2c5ce 54 5f 42 54 52 45 45 43 4f 55 4e 54 20 2a 2f 0a  T_BTREECOUNT */.
2c5cf 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20        {.        
2c5d0 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 20  /* Check if the 
2c5d1 71 75 65 72 79 20 69 73 20 6f 66 20 6f 6e 65 20  query is of one 
2c5d2 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
2c5d3 20 66 6f 72 6d 73 3a 0a 20 20 20 20 20 20 20 20   forms:.        
2c5d4 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20  **.        **   
2c5d5 53 45 4c 45 43 54 20 6d 69 6e 28 78 29 20 46 52  SELECT min(x) FR
2c5d6 4f 4d 20 2e 2e 2e 0a 20 20 20 20 20 20 20 20 2a  OM ....        *
2c5d7 2a 20 20 20 53 45 4c 45 43 54 20 6d 61 78 28 78  *   SELECT max(x
2c5d8 29 20 46 52 4f 4d 20 2e 2e 2e 0a 20 20 20 20 20  ) FROM ....     
2c5d9 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a     **.        **
2c5da 20 49 66 20 69 74 20 69 73 2c 20 74 68 65 6e 20   If it is, then 
2c5db 61 73 6b 20 74 68 65 20 63 6f 64 65 20 69 6e 20  ask the code in 
2c5dc 77 68 65 72 65 2e 63 20 74 6f 20 61 74 74 65 6d  where.c to attem
2c5dd 70 74 20 74 6f 20 73 6f 72 74 20 72 65 73 75 6c  pt to sort resul
2c5de 74 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 73  ts.        ** as
2c5df 20 69 66 20 74 68 65 72 65 20 77 61 73 20 61 6e   if there was an
2c5e0 20 22 4f 52 44 45 52 20 4f 4e 20 78 22 20 6f 72   "ORDER ON x" or
2c5e1 20 22 4f 52 44 45 52 20 4f 4e 20 78 20 44 45 53   "ORDER ON x DES
2c5e2 43 22 20 63 6c 61 75 73 65 2e 20 0a 20 20 20 20  C" clause. .    
2c5e3 20 20 20 20 2a 2a 20 49 66 20 77 68 65 72 65 2e      ** If where.
2c5e4 63 20 69 73 20 61 62 6c 65 20 74 6f 20 70 72 6f  c is able to pro
2c5e5 64 75 63 65 20 72 65 73 75 6c 74 73 20 73 6f 72  duce results sor
2c5e6 74 65 64 20 69 6e 20 74 68 69 73 20 6f 72 64 65  ted in this orde
2c5e7 72 2c 20 74 68 65 6e 0a 20 20 20 20 20 20 20 20  r, then.        
2c5e8 2a 2a 20 61 64 64 20 76 64 62 65 20 63 6f 64 65  ** add vdbe code
2c5e9 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66   to break out of
2c5ea 20 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20   the processing 
2c5eb 6c 6f 6f 70 20 61 66 74 65 72 20 74 68 65 20 0a  loop after the .
2c5ec 20 20 20 20 20 20 20 20 2a 2a 20 66 69 72 73 74          ** first
2c5ed 20 69 74 65 72 61 74 69 6f 6e 20 28 73 69 6e 63   iteration (sinc
2c5ee 65 20 74 68 65 20 66 69 72 73 74 20 69 74 65 72  e the first iter
2c5ef 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 6f  ation of the loo
2c5f0 70 20 69 73 20 0a 20 20 20 20 20 20 20 20 2a 2a  p is .        **
2c5f1 20 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 6f   guaranteed to o
2c5f2 70 65 72 61 74 65 20 6f 6e 20 74 68 65 20 72 6f  perate on the ro
2c5f3 77 20 77 69 74 68 20 74 68 65 20 6d 69 6e 69 6d  w with the minim
2c5f4 75 6d 20 6f 72 20 6d 61 78 69 6d 75 6d 20 0a 20  um or maximum . 
2c5f5 20 20 20 20 20 20 20 2a 2a 20 76 61 6c 75 65 20         ** value 
2c5f6 6f 66 20 78 2c 20 74 68 65 20 6f 6e 6c 79 20 72  of x, the only r
2c5f7 6f 77 20 72 65 71 75 69 72 65 64 29 2e 0a 20 20  ow required)..  
2c5f8 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20        **.       
2c5f9 20 2a 2a 20 41 20 73 70 65 63 69 61 6c 20 66 6c   ** A special fl
2c5fa 61 67 20 6d 75 73 74 20 62 65 20 70 61 73 73 65  ag must be passe
2c5fb 64 20 74 6f 20 73 71 6c 69 74 65 33 57 68 65 72  d to sqlite3Wher
2c5fc 65 42 65 67 69 6e 28 29 20 74 6f 20 73 6c 69 67  eBegin() to slig
2c5fd 68 74 6c 79 0a 20 20 20 20 20 20 20 20 2a 2a 20  htly.        ** 
2c5fe 6d 6f 64 69 66 79 20 62 65 68 61 76 69 6f 75 72  modify behaviour
2c5ff 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20 20 20   as follows:.   
2c600 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
2c601 2a 2a 20 20 20 2b 20 49 66 20 74 68 65 20 71 75  **   + If the qu
2c602 65 72 79 20 69 73 20 61 20 22 53 45 4c 45 43 54  ery is a "SELECT
2c603 20 6d 69 6e 28 78 29 22 2c 20 74 68 65 6e 20 74   min(x)", then t
2c604 68 65 20 6c 6f 6f 70 20 63 6f 64 65 64 20 62 79  he loop coded by
2c605 0a 20 20 20 20 20 20 20 20 2a 2a 20 20 20 20 20  .        **     
2c606 77 68 65 72 65 2e 63 20 73 68 6f 75 6c 64 20 6e  where.c should n
2c607 6f 74 20 69 74 65 72 61 74 65 20 6f 76 65 72 20  ot iterate over 
2c608 61 6e 79 20 76 61 6c 75 65 73 20 77 69 74 68 20  any values with 
2c609 61 20 4e 55 4c 4c 20 76 61 6c 75 65 0a 20 20 20  a NULL value.   
2c60a 20 20 20 20 20 2a 2a 20 20 20 20 20 66 6f 72 20       **     for 
2c60b 78 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  x..        **.  
2c60c 20 20 20 20 20 20 2a 2a 20 20 20 2b 20 54 68 65        **   + The
2c60d 20 6f 70 74 69 6d 69 7a 65 72 20 63 6f 64 65 20   optimizer code 
2c60e 69 6e 20 77 68 65 72 65 2e 63 20 28 74 68 65 20  in where.c (the 
2c60f 74 68 69 6e 67 20 74 68 61 74 20 64 65 63 69 64  thing that decid
2c610 65 73 20 77 68 69 63 68 0a 20 20 20 20 20 20 20  es which.       
2c611 20 2a 2a 20 20 20 20 20 69 6e 64 65 78 20 6f 72   **     index or
2c612 20 69 6e 64 69 63 65 73 20 74 6f 20 75 73 65 29   indices to use)
2c613 20 73 68 6f 75 6c 64 20 70 6c 61 63 65 20 61 20   should place a 
2c614 64 69 66 66 65 72 65 6e 74 20 70 72 69 6f 72 69  different priori
2c615 74 79 20 6f 6e 20 0a 20 20 20 20 20 20 20 20 2a  ty on .        *
2c616 2a 20 20 20 20 20 73 61 74 69 73 66 79 69 6e 67  *     satisfying
2c617 20 74 68 65 20 27 4f 52 44 45 52 20 42 59 27 20   the 'ORDER BY' 
2c618 63 6c 61 75 73 65 20 74 68 61 6e 20 69 74 20 64  clause than it d
2c619 6f 65 73 20 69 6e 20 6f 74 68 65 72 20 63 61 73  oes in other cas
2c61a 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 20  es..        **  
2c61b 20 20 20 52 65 66 65 72 20 74 6f 20 63 6f 64 65     Refer to code
2c61c 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 20 69 6e   and comments in
2c61d 20 77 68 65 72 65 2e 63 20 66 6f 72 20 64 65 74   where.c for det
2c61e 61 69 6c 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f  ails..        */
2c61f 0a 20 20 20 20 20 20 20 20 45 78 70 72 4c 69 73  .        ExprLis
2c620 74 20 2a 70 4d 69 6e 4d 61 78 20 3d 20 30 3b 0a  t *pMinMax = 0;.
2c621 20 20 20 20 20 20 20 20 75 38 20 66 6c 61 67 20          u8 flag 
2c622 3d 20 6d 69 6e 4d 61 78 51 75 65 72 79 28 70 29  = minMaxQuery(p)
2c623 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 66 6c  ;.        if( fl
2c624 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ag ){.          
2c625 61 73 73 65 72 74 28 20 21 45 78 70 72 48 61 73  assert( !ExprHas
2c626 50 72 6f 70 65 72 74 79 28 70 2d 3e 70 45 4c 69  Property(p->pELi
2c627 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2c 20  st->a[0].pExpr, 
2c628 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29 3b  EP_xIsSelect) );
2c629 0a 20 20 20 20 20 20 20 20 20 20 70 4d 69 6e 4d  .          pMinM
2c62a 61 78 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ax = sqlite3Expr
2c62b 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70  ListDup(db, p->p
2c62c 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70  EList->a[0].pExp
2c62d 72 2d 3e 78 2e 70 4c 69 73 74 2c 30 29 3b 0a 20  r->x.pList,0);. 
2c62e 20 20 20 20 20 20 20 20 20 70 44 65 6c 20 3d 20           pDel = 
2c62f 70 4d 69 6e 4d 61 78 3b 0a 20 20 20 20 20 20 20  pMinMax;.       
2c630 20 20 20 69 66 28 20 70 4d 69 6e 4d 61 78 20 26     if( pMinMax &
2c631 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  & !db->mallocFai
2c632 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20  led ){.         
2c633 20 20 20 70 4d 69 6e 4d 61 78 2d 3e 61 5b 30 5d     pMinMax->a[0]
2c634 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 66 6c 61  .sortOrder = fla
2c635 67 21 3d 57 48 45 52 45 5f 4f 52 44 45 52 42 59  g!=WHERE_ORDERBY
2c636 5f 4d 49 4e 20 3f 31 3a 30 3b 0a 20 20 20 20 20  _MIN ?1:0;.     
2c637 20 20 20 20 20 20 20 70 4d 69 6e 4d 61 78 2d 3e         pMinMax->
2c638 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f 70 20 3d  a[0].pExpr->op =
2c639 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20   TK_COLUMN;.    
2c63a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2c63b 7d 0a 20 20 0a 20 20 20 20 20 20 20 20 2f 2a 20  }.  .        /* 
2c63c 54 68 69 73 20 63 61 73 65 20 72 75 6e 73 20 69  This case runs i
2c63d 66 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20  f the aggregate 
2c63e 68 61 73 20 6e 6f 20 47 52 4f 55 50 20 42 59 20  has no GROUP BY 
2c63f 63 6c 61 75 73 65 2e 20 20 54 68 65 0a 20 20 20  clause.  The.   
2c640 20 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 69       ** processi
2c641 6e 67 20 69 73 20 6d 75 63 68 20 73 69 6d 70 6c  ng is much simpl
2c642 65 72 20 73 69 6e 63 65 20 74 68 65 72 65 20 69  er since there i
2c643 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20  s only a single 
2c644 72 6f 77 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f  row.        ** o
2c645 66 20 6f 75 74 70 75 74 2e 0a 20 20 20 20 20 20  f output..      
2c646 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 73    */.        res
2c647 65 74 41 63 63 75 6d 75 6c 61 74 6f 72 28 70 50  etAccumulator(pP
2c648 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f 29  arse, &sAggInfo)
2c649 3b 0a 20 20 20 20 20 20 20 20 70 57 49 6e 66 6f  ;.        pWInfo
2c64a 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42   = sqlite3WhereB
2c64b 65 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61  egin(pParse, pTa
2c64c 62 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 20 26  bList, pWhere, &
2c64d 70 4d 69 6e 4d 61 78 2c 20 66 6c 61 67 29 3b 0a  pMinMax, flag);.
2c64e 20 20 20 20 20 20 20 20 69 66 28 20 70 57 49 6e          if( pWIn
2c64f 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  fo==0 ){.       
2c650 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
2c651 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 44 65  stDelete(db, pDe
2c652 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  l);.          go
2c653 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20  to select_end;. 
2c654 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2c655 20 75 70 64 61 74 65 41 63 63 75 6d 75 6c 61 74   updateAccumulat
2c656 6f 72 28 70 50 61 72 73 65 2c 20 26 73 41 67 67  or(pParse, &sAgg
2c657 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 69  Info);.        i
2c658 66 28 20 21 70 4d 69 6e 4d 61 78 20 26 26 20 66  f( !pMinMax && f
2c659 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 20  lag ){.         
2c65a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2c65b 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
2c65c 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b  , pWInfo->iBreak
2c65d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 56 64 62  );.          Vdb
2c65e 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25 73  eComment((v, "%s
2c65f 28 29 20 62 79 20 69 6e 64 65 78 22 2c 0a 20 20  () by index",.  
2c660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 66                (f
2c661 6c 61 67 3d 3d 57 48 45 52 45 5f 4f 52 44 45 52  lag==WHERE_ORDER
2c662 42 59 5f 4d 49 4e 3f 22 6d 69 6e 22 3a 22 6d 61  BY_MIN?"min":"ma
2c663 78 22 29 29 29 3b 0a 20 20 20 20 20 20 20 20 7d  x")));.        }
2c664 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2c665 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29  WhereEnd(pWInfo)
2c666 3b 0a 20 20 20 20 20 20 20 20 66 69 6e 61 6c 69  ;.        finali
2c667 7a 65 41 67 67 46 75 6e 63 74 69 6f 6e 73 28 70  zeAggFunctions(p
2c668 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66 6f  Parse, &sAggInfo
2c669 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  );.      }..    
2c66a 20 20 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a    pOrderBy = 0;.
2c66b 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2c66c 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c  rIfFalse(pParse,
2c66d 20 70 48 61 76 69 6e 67 2c 20 61 64 64 72 45 6e   pHaving, addrEn
2c66e 64 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  d, SQLITE_JUMPIF
2c66f 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 73 65 6c  NULL);.      sel
2c670 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70 50 61  ectInnerLoop(pPa
2c671 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45 4c 69 73  rse, p, p->pELis
2c672 74 2c 20 30 2c 20 30 2c 20 30 2c 20 2d 31 2c 20  t, 0, 0, 0, -1, 
2c673 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2c674 20 20 20 20 20 20 20 70 44 65 73 74 2c 20 61 64         pDest, ad
2c675 64 72 45 6e 64 2c 20 61 64 64 72 45 6e 64 29 3b  drEnd, addrEnd);
2c676 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2c677 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
2c678 20 70 44 65 6c 29 3b 0a 20 20 20 20 7d 0a 20 20   pDel);.    }.  
2c679 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
2c67a 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64  olveLabel(v, add
2c67b 72 45 6e 64 29 3b 0a 20 20 20 20 0a 20 20 7d 20  rEnd);.    .  } 
2c67c 2f 2a 20 65 6e 64 69 66 20 61 67 67 72 65 67 61  /* endif aggrega
2c67d 74 65 20 71 75 65 72 79 20 2a 2f 0a 0a 20 20 2f  te query */..  /
2c67e 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6e  * If there is an
2c67f 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2c680 2c 20 74 68 65 6e 20 77 65 20 6e 65 65 64 20 74  , then we need t
2c681 6f 20 73 6f 72 74 20 74 68 65 20 72 65 73 75 6c  o sort the resul
2c682 74 73 0a 20 20 2a 2a 20 61 6e 64 20 73 65 6e 64  ts.  ** and send
2c683 20 74 68 65 6d 20 74 6f 20 74 68 65 20 63 61 6c   them to the cal
2c684 6c 62 61 63 6b 20 6f 6e 65 20 62 79 20 6f 6e 65  lback one by one
2c685 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f 72  ..  */.  if( pOr
2c686 64 65 72 42 79 20 29 7b 0a 20 20 20 20 67 65 6e  derBy ){.    gen
2c687 65 72 61 74 65 53 6f 72 74 54 61 69 6c 28 70 50  erateSortTail(pP
2c688 61 72 73 65 2c 20 70 2c 20 76 2c 20 70 45 4c 69  arse, p, v, pELi
2c689 73 74 2d 3e 6e 45 78 70 72 2c 20 70 44 65 73 74  st->nExpr, pDest
2c68a 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d  );.  }..  /* Jum
2c68b 70 20 68 65 72 65 20 74 6f 20 73 6b 69 70 20 74  p here to skip t
2c68c 68 69 73 20 71 75 65 72 79 0a 20 20 2a 2f 0a 20  his query.  */. 
2c68d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
2c68e 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 45 6e 64  lveLabel(v, iEnd
2c68f 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 53 45 4c  );..  /* The SEL
2c690 45 43 54 20 77 61 73 20 73 75 63 63 65 73 73 66  ECT was successf
2c691 75 6c 6c 79 20 63 6f 64 65 64 2e 20 20 20 53 65  ully coded.   Se
2c692 74 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64  t the return cod
2c693 65 20 74 6f 20 30 0a 20 20 2a 2a 20 74 6f 20 69  e to 0.  ** to i
2c694 6e 64 69 63 61 74 65 20 6e 6f 20 65 72 72 6f 72  ndicate no error
2c695 73 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 30  s..  */.  rc = 0
2c696 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 74 72 6f 6c 20  ;..  /* Control 
2c697 6a 75 6d 70 73 20 74 6f 20 68 65 72 65 20 69 66  jumps to here if
2c698 20 61 6e 20 65 72 72 6f 72 20 69 73 20 65 6e 63   an error is enc
2c699 6f 75 6e 74 65 72 65 64 20 61 62 6f 76 65 2c 20  ountered above, 
2c69a 6f 72 20 75 70 6f 6e 0a 20 20 2a 2a 20 73 75 63  or upon.  ** suc
2c69b 63 65 73 73 66 75 6c 20 63 6f 64 69 6e 67 20 6f  cessful coding o
2c69c 66 20 74 68 65 20 53 45 4c 45 43 54 2e 0a 20 20  f the SELECT..  
2c69d 2a 2f 0a 73 65 6c 65 63 74 5f 65 6e 64 3a 0a 0a  */.select_end:..
2c69e 20 20 2f 2a 20 49 64 65 6e 74 69 66 79 20 63 6f    /* Identify co
2c69f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 66 20 72 65  lumn names if re
2c6a0 73 75 6c 74 73 20 6f 66 20 74 68 65 20 53 45 4c  sults of the SEL
2c6a1 45 43 54 20 61 72 65 20 74 6f 20 62 65 20 6f 75  ECT are to be ou
2c6a2 74 70 75 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  tput..  */.  if(
2c6a3 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
2c6a4 26 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d 3d  & pDest->eDest==
2c6a5 53 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20 20  SRT_Output ){.  
2c6a6 20 20 67 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e    generateColumn
2c6a7 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54  Names(pParse, pT
2c6a8 61 62 4c 69 73 74 2c 20 70 45 4c 69 73 74 29 3b  abList, pEList);
2c6a9 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 44  .  }..  sqlite3D
2c6aa 62 46 72 65 65 28 64 62 2c 20 73 41 67 67 49 6e  bFree(db, sAggIn
2c6ab 66 6f 2e 61 43 6f 6c 29 3b 0a 20 20 73 71 6c 69  fo.aCol);.  sqli
2c6ac 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 73 41  te3DbFree(db, sA
2c6ad 67 67 49 6e 66 6f 2e 61 46 75 6e 63 29 3b 0a 20  ggInfo.aFunc);. 
2c6ae 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23   return rc;.}..#
2c6af 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  if defined(SQLIT
2c6b0 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 2a 2a  E_DEBUG)./*.****
2c6b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c6b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c6b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c6b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c6b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
2c6b6 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64  he following cod
2c6b7 65 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65  e is used for te
2c6b8 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67  sting and debugg
2c6b9 69 6e 67 20 6f 6e 6c 79 2e 20 20 54 68 65 20 63  ing only.  The c
2c6ba 6f 64 65 0a 2a 2a 20 74 68 61 74 20 66 6f 6c 6c  ode.** that foll
2c6bb 6f 77 73 20 64 6f 65 73 20 6e 6f 74 20 61 70 70  ows does not app
2c6bc 65 61 72 20 69 6e 20 6e 6f 72 6d 61 6c 20 62 75  ear in normal bu
2c6bd 69 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  ilds..**.** Thes
2c6be 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75  e routines are u
2c6bf 73 65 64 20 74 6f 20 70 72 69 6e 74 20 6f 75 74  sed to print out
2c6c0 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
2c6c1 61 6c 6c 20 6f 72 20 70 61 72 74 20 6f 66 20 61  all or part of a
2c6c2 20 0a 2a 2a 20 70 61 72 73 65 20 73 74 72 75 63   .** parse struc
2c6c3 74 75 72 65 73 20 73 75 63 68 20 61 73 20 53 65  tures such as Se
2c6c4 6c 65 63 74 20 6f 72 20 45 78 70 72 2e 20 20 53  lect or Expr.  S
2c6c5 75 63 68 20 70 72 69 6e 74 6f 75 74 73 20 61 72  uch printouts ar
2c6c6 65 20 75 73 65 66 75 6c 0a 2a 2a 20 66 6f 72 20  e useful.** for 
2c6c7 68 65 6c 70 69 6e 67 20 74 6f 20 75 6e 64 65 72  helping to under
2c6c8 73 74 61 6e 64 20 77 68 61 74 20 69 73 20 68 61  stand what is ha
2c6c9 70 70 65 6e 69 6e 67 20 69 6e 73 69 64 65 20 74  ppening inside t
2c6ca 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74 6f  he code generato
2c6cb 72 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68 65 20  r.** during the 
2c6cc 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 63 6f 6d  execution of com
2c6cd 70 6c 65 78 20 53 45 4c 45 43 54 20 73 74 61 74  plex SELECT stat
2c6ce 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ements..**.** Th
2c6cf 65 73 65 20 72 6f 75 74 69 6e 65 20 61 72 65 20  ese routine are 
2c6d0 6e 6f 74 20 63 61 6c 6c 65 64 20 61 6e 79 77 68  not called anywh
2c6d1 65 72 65 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  ere from within 
2c6d2 74 68 65 20 6e 6f 72 6d 61 6c 0a 2a 2a 20 63 6f  the normal.** co
2c6d3 64 65 20 62 61 73 65 2e 20 20 54 68 65 6e 20 61  de base.  Then a
2c6d4 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62  re intended to b
2c6d5 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69  e called from wi
2c6d6 74 68 69 6e 20 74 68 65 20 64 65 62 75 67 67 65  thin the debugge
2c6d7 72 0a 2a 2a 20 6f 72 20 66 72 6f 6d 20 74 65 6d  r.** or from tem
2c6d8 70 6f 72 61 72 79 20 22 70 72 69 6e 74 66 22 20  porary "printf" 
2c6d9 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 73 65 72  statements inser
2c6da 74 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e  ted for debuggin
2c6db 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  g..*/.SQLITE_PRI
2c6dc 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2c6dd 33 50 72 69 6e 74 45 78 70 72 28 45 78 70 72 20  3PrintExpr(Expr 
2c6de 2a 70 29 7b 0a 20 20 69 66 28 20 21 45 78 70 72  *p){.  if( !Expr
2c6df 48 61 73 50 72 6f 70 65 72 74 79 28 70 2c 20 45  HasProperty(p, E
2c6e0 50 5f 49 6e 74 56 61 6c 75 65 29 20 26 26 20 70  P_IntValue) && p
2c6e1 2d 3e 75 2e 7a 54 6f 6b 65 6e 20 29 7b 0a 20 20  ->u.zToken ){.  
2c6e2 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
2c6e3 69 6e 74 66 28 22 28 25 73 22 2c 20 70 2d 3e 75  intf("(%s", p->u
2c6e4 2e 7a 54 6f 6b 65 6e 29 3b 0a 20 20 7d 65 6c 73  .zToken);.  }els
2c6e5 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  e{.    sqlite3De
2c6e6 62 75 67 50 72 69 6e 74 66 28 22 28 25 64 22 2c  bugPrintf("(%d",
2c6e7 20 70 2d 3e 6f 70 29 3b 0a 20 20 7d 0a 20 20 69   p->op);.  }.  i
2c6e8 66 28 20 70 2d 3e 70 4c 65 66 74 20 29 7b 0a 20  f( p->pLeft ){. 
2c6e9 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
2c6ea 72 69 6e 74 66 28 22 20 22 29 3b 0a 20 20 20 20  rintf(" ");.    
2c6eb 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72  sqlite3PrintExpr
2c6ec 28 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 7d 0a  (p->pLeft);.  }.
2c6ed 20 20 69 66 28 20 70 2d 3e 70 52 69 67 68 74 20    if( p->pRight 
2c6ee 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  ){.    sqlite3De
2c6ef 62 75 67 50 72 69 6e 74 66 28 22 20 22 29 3b 0a  bugPrintf(" ");.
2c6f0 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74      sqlite3Print
2c6f1 45 78 70 72 28 70 2d 3e 70 52 69 67 68 74 29 3b  Expr(p->pRight);
2c6f2 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 65  .  }.  sqlite3De
2c6f3 62 75 67 50 72 69 6e 74 66 28 22 29 22 29 3b 0a  bugPrintf(")");.
2c6f4 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
2c6f5 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 69   void sqlite3Pri
2c6f6 6e 74 45 78 70 72 4c 69 73 74 28 45 78 70 72 4c  ntExprList(ExprL
2c6f7 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69  ist *pList){.  i
2c6f8 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
2c6f9 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   i<pList->nExpr;
2c6fa 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74   i++){.    sqlit
2c6fb 65 33 50 72 69 6e 74 45 78 70 72 28 70 4c 69 73  e3PrintExpr(pLis
2c6fc 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 3b 0a  t->a[i].pExpr);.
2c6fd 20 20 20 20 69 66 28 20 69 3c 70 4c 69 73 74 2d      if( i<pList-
2c6fe 3e 6e 45 78 70 72 2d 31 20 29 7b 0a 20 20 20 20  >nExpr-1 ){.    
2c6ff 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
2c700 69 6e 74 66 28 22 2c 20 22 29 3b 0a 20 20 20 20  intf(", ");.    
2c701 7d 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50  }.  }.}.SQLITE_P
2c702 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2c703 74 65 33 50 72 69 6e 74 53 65 6c 65 63 74 28 53  te3PrintSelect(S
2c704 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 69 6e  elect *p, int in
2c705 64 65 6e 74 29 7b 0a 20 20 73 71 6c 69 74 65 33  dent){.  sqlite3
2c706 44 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73  DebugPrintf("%*s
2c707 53 45 4c 45 43 54 28 25 70 29 20 22 2c 20 69 6e  SELECT(%p) ", in
2c708 64 65 6e 74 2c 20 22 22 2c 20 70 29 3b 0a 20 20  dent, "", p);.  
2c709 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72  sqlite3PrintExpr
2c70a 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73 74 29 3b  List(p->pEList);
2c70b 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50  .  sqlite3DebugP
2c70c 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 69  rintf("\n");.  i
2c70d 66 28 20 70 2d 3e 70 53 72 63 20 29 7b 0a 20 20  f( p->pSrc ){.  
2c70e 20 20 63 68 61 72 20 2a 7a 50 72 65 66 69 78 3b    char *zPrefix;
2c70f 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
2c710 7a 50 72 65 66 69 78 20 3d 20 22 46 52 4f 4d 22  zPrefix = "FROM"
2c711 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
2c712 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 3b 20  <p->pSrc->nSrc; 
2c713 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74 72 75  i++){.      stru
2c714 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
2c715 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e 70 53 72  *pItem = &p->pSr
2c716 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20 73  c->a[i];.      s
2c717 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2c718 66 28 22 25 2a 73 20 22 2c 20 69 6e 64 65 6e 74  f("%*s ", indent
2c719 2b 36 2c 20 7a 50 72 65 66 69 78 29 3b 0a 20 20  +6, zPrefix);.  
2c71a 20 20 20 20 7a 50 72 65 66 69 78 20 3d 20 22 22      zPrefix = ""
2c71b 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65  ;.      if( pIte
2c71c 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  m->pSelect ){.  
2c71d 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62        sqlite3Deb
2c71e 75 67 50 72 69 6e 74 66 28 22 28 5c 6e 22 29 3b  ugPrintf("(\n");
2c71f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2c720 50 72 69 6e 74 53 65 6c 65 63 74 28 70 49 74 65  PrintSelect(pIte
2c721 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 69 6e 64 65  m->pSelect, inde
2c722 6e 74 2b 31 30 29 3b 0a 20 20 20 20 20 20 20 20  nt+10);.        
2c723 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
2c724 74 66 28 22 25 2a 73 29 22 2c 20 69 6e 64 65 6e  tf("%*s)", inden
2c725 74 2b 38 2c 20 22 22 29 3b 0a 20 20 20 20 20 20  t+8, "");.      
2c726 7d 65 6c 73 65 20 69 66 28 20 70 49 74 65 6d 2d  }else if( pItem-
2c727 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20  >zName ){.      
2c728 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
2c729 69 6e 74 66 28 22 25 73 22 2c 20 70 49 74 65 6d  intf("%s", pItem
2c72a 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ->zName);.      
2c72b 7d 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65  }.      if( pIte
2c72c 6d 2d 3e 70 54 61 62 20 29 7b 0a 20 20 20 20 20  m->pTab ){.     
2c72d 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
2c72e 72 69 6e 74 66 28 22 28 74 61 62 6c 65 3a 20 25  rintf("(table: %
2c72f 73 29 22 2c 20 70 49 74 65 6d 2d 3e 70 54 61 62  s)", pItem->pTab
2c730 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ->zName);.      
2c731 7d 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65  }.      if( pIte
2c732 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20 20 20  m->zAlias ){.   
2c733 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75       sqlite3Debu
2c734 67 50 72 69 6e 74 66 28 22 20 41 53 20 25 73 22  gPrintf(" AS %s"
2c735 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29  , pItem->zAlias)
2c736 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2c737 69 66 28 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e  if( i<p->pSrc->n
2c738 53 72 63 2d 31 20 29 7b 0a 20 20 20 20 20 20 20  Src-1 ){.       
2c739 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2c73a 6e 74 66 28 22 2c 22 29 3b 0a 20 20 20 20 20 20  ntf(",");.      
2c73b 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  }.      sqlite3D
2c73c 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e 22 29  ebugPrintf("\n")
2c73d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
2c73e 28 20 70 2d 3e 70 57 68 65 72 65 20 29 7b 0a 20  ( p->pWhere ){. 
2c73f 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
2c740 72 69 6e 74 66 28 22 25 2a 73 20 57 48 45 52 45  rintf("%*s WHERE
2c741 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22 29 3b   ", indent, "");
2c742 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e  .    sqlite3Prin
2c743 74 45 78 70 72 28 70 2d 3e 70 57 68 65 72 65 29  tExpr(p->pWhere)
2c744 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62  ;.    sqlite3Deb
2c745 75 67 50 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a  ugPrintf("\n");.
2c746 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 47 72    }.  if( p->pGr
2c747 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 73 71 6c  oupBy ){.    sql
2c748 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
2c749 22 25 2a 73 20 47 52 4f 55 50 20 42 59 20 22 2c  "%*s GROUP BY ",
2c74a 20 69 6e 64 65 6e 74 2c 20 22 22 29 3b 0a 20 20   indent, "");.  
2c74b 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78    sqlite3PrintEx
2c74c 70 72 4c 69 73 74 28 70 2d 3e 70 47 72 6f 75 70  prList(p->pGroup
2c74d 42 79 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  By);.    sqlite3
2c74e 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e 22  DebugPrintf("\n"
2c74f 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  );.  }.  if( p->
2c750 70 48 61 76 69 6e 67 20 29 7b 0a 20 20 20 20 73  pHaving ){.    s
2c751 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
2c752 66 28 22 25 2a 73 20 48 41 56 49 4e 47 20 22 2c  f("%*s HAVING ",
2c753 20 69 6e 64 65 6e 74 2c 20 22 22 29 3b 0a 20 20   indent, "");.  
2c754 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78    sqlite3PrintEx
2c755 70 72 28 70 2d 3e 70 48 61 76 69 6e 67 29 3b 0a  pr(p->pHaving);.
2c756 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
2c757 50 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20  Printf("\n");.  
2c758 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4f 72 64 65  }.  if( p->pOrde
2c759 72 42 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  rBy ){.    sqlit
2c75a 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25  e3DebugPrintf("%
2c75b 2a 73 20 4f 52 44 45 52 20 42 59 20 22 2c 20 69  *s ORDER BY ", i
2c75c 6e 64 65 6e 74 2c 20 22 22 29 3b 0a 20 20 20 20  ndent, "");.    
2c75d 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72  sqlite3PrintExpr
2c75e 4c 69 73 74 28 70 2d 3e 70 4f 72 64 65 72 42 79  List(p->pOrderBy
2c75f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  );.    sqlite3De
2c760 62 75 67 50 72 69 6e 74 66 28 22 5c 6e 22 29 3b  bugPrintf("\n");
2c761 0a 20 20 7d 0a 7d 0a 2f 2a 20 45 6e 64 20 6f 66  .  }.}./* End of
2c762 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 64   the structure d
2c763 65 62 75 67 20 70 72 69 6e 74 69 6e 67 20 63 6f  ebug printing co
2c764 64 65 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  de.*************
2c765 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c766 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c767 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c768 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c769 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69  /.#endif /* defi
2c76a 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29  ned(SQLITE_TEST)
2c76b 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
2c76c 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a 0a 2f 2a  TE_DEBUG) */../*
2c76d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
2c76e 64 20 6f 66 20 73 65 6c 65 63 74 2e 63 20 2a 2a  d of select.c **
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 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2c772 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
2c773 67 69 6e 20 66 69 6c 65 20 74 61 62 6c 65 2e 63  gin file table.c
2c774 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2c775 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c776 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2c777 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
2c778 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
2c779 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
2c77a 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
2c77b 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
2c77c 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
2c77d 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
2c77e 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
2c77f 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
2c780 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
2c781 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
2c782 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
2c783 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
2c784 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
2c785 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
2c786 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
2c787 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
2c788 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
2c789 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
2c78a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c78b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c78c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c78d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c78e 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
2c78f 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73  e contains the s
2c790 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65  qlite3_get_table
2c791 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 66  () and sqlite3_f
2c792 72 65 65 5f 74 61 62 6c 65 28 29 0a 2a 2a 20 69  ree_table().** i
2c793 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65  nterface routine
2c794 73 2e 20 20 54 68 65 73 65 20 61 72 65 20 6a 75  s.  These are ju
2c795 73 74 20 77 72 61 70 70 65 72 73 20 61 72 6f 75  st wrappers arou
2c796 6e 64 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 69  nd the main.** i
2c797 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65  nterface routine
2c798 20 6f 66 20 73 71 6c 69 74 65 33 5f 65 78 65 63   of sqlite3_exec
2c799 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ()..**.** These 
2c79a 72 6f 75 74 69 6e 65 73 20 61 72 65 20 69 6e 20  routines are in 
2c79b 61 20 73 65 70 61 72 61 74 65 20 66 69 6c 65 73  a separate files
2c79c 20 73 6f 20 74 68 61 74 20 74 68 65 79 20 77 69   so that they wi
2c79d 6c 6c 20 6e 6f 74 20 62 65 20 6c 69 6e 6b 65 64  ll not be linked
2c79e 0a 2a 2a 20 69 66 20 74 68 65 79 20 61 72 65 20  .** if they are 
2c79f 6e 6f 74 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20  not used..**.** 
2c7a0 24 49 64 3a 20 74 61 62 6c 65 2e 63 2c 76 20 31  $Id: table.c,v 1
2c7a1 2e 34 30 20 32 30 30 39 2f 30 34 2f 31 30 20 31  .40 2009/04/10 1
2c7a2 34 3a 32 38 3a 30 30 20 64 72 68 20 45 78 70 20  4:28:00 drh Exp 
2c7a3 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  $.*/..#ifndef SQ
2c7a4 4c 49 54 45 5f 4f 4d 49 54 5f 47 45 54 5f 54 41  LITE_OMIT_GET_TA
2c7a5 42 4c 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  BLE../*.** This 
2c7a6 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65  structure is use
2c7a7 64 20 74 6f 20 70 61 73 73 20 64 61 74 61 20 66  d to pass data f
2c7a8 72 6f 6d 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  rom sqlite3_get_
2c7a9 74 61 62 6c 65 28 29 20 74 68 72 6f 75 67 68 0a  table() through.
2c7aa 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61  ** to the callba
2c7ab 63 6b 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  ck function is u
2c7ac 73 65 73 20 74 6f 20 62 75 69 6c 64 20 74 68 65  ses to build the
2c7ad 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a 74 79 70 65   result..*/.type
2c7ae 64 65 66 20 73 74 72 75 63 74 20 54 61 62 52 65  def struct TabRe
2c7af 73 75 6c 74 20 7b 0a 20 20 63 68 61 72 20 2a 2a  sult {.  char **
2c7b0 61 7a 52 65 73 75 6c 74 3b 20 20 20 2f 2a 20 41  azResult;   /* A
2c7b1 63 63 75 6d 75 6c 61 74 65 64 20 6f 75 74 70 75  ccumulated outpu
2c7b2 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 45 72  t */.  char *zEr
2c7b3 72 4d 73 67 3b 20 20 20 20 20 2f 2a 20 45 72 72  rMsg;     /* Err
2c7b4 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78 74 2c  or message text,
2c7b5 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   if an error occ
2c7b6 75 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c  urs */.  int nAl
2c7b7 6c 6f 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 53  loc;        /* S
2c7b8 6c 6f 74 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  lots allocated f
2c7b9 6f 72 20 61 7a 52 65 73 75 6c 74 5b 5d 20 2a 2f  or azResult[] */
2c7ba 0a 20 20 69 6e 74 20 6e 52 6f 77 3b 20 20 20 20  .  int nRow;    
2c7bb 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2c7bc 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 72  of rows in the r
2c7bd 65 73 75 6c 74 20 2a 2f 0a 20 20 69 6e 74 20 6e  esult */.  int n
2c7be 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 2f 2a  Column;       /*
2c7bf 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   Number of colum
2c7c0 6e 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ns in the result
2c7c1 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b   */.  int nData;
2c7c2 20 20 20 20 20 20 20 20 20 2f 2a 20 53 6c 6f 74           /* Slot
2c7c3 73 20 75 73 65 64 20 69 6e 20 61 7a 52 65 73 75  s used in azResu
2c7c4 6c 74 5b 5d 2e 20 20 28 6e 52 6f 77 2b 31 29 2a  lt[].  (nRow+1)*
2c7c5 6e 43 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74  nColumn */.  int
2c7c6 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20   rc;            
2c7c7 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66  /* Return code f
2c7c8 72 6f 6d 20 73 71 6c 69 74 65 33 5f 65 78 65 63  rom sqlite3_exec
2c7c9 28 29 20 2a 2f 0a 7d 20 54 61 62 52 65 73 75 6c  () */.} TabResul
2c7ca 74 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  t;../*.** This r
2c7cb 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
2c7cc 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 72   once for each r
2c7cd 6f 77 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74  ow in the result
2c7ce 20 74 61 62 6c 65 2e 20 20 49 74 73 20 6a 6f 62   table.  Its job
2c7cf 0a 2a 2a 20 69 73 20 74 6f 20 66 69 6c 6c 20 69  .** is to fill i
2c7d0 6e 20 74 68 65 20 54 61 62 52 65 73 75 6c 74 20  n the TabResult 
2c7d1 73 74 72 75 63 74 75 72 65 20 61 70 70 72 6f 70  structure approp
2c7d2 72 69 61 74 65 6c 79 2c 20 61 6c 6c 6f 63 61 74  riately, allocat
2c7d3 69 6e 67 20 6e 65 77 0a 2a 2a 20 6d 65 6d 6f 72  ing new.** memor
2c7d4 79 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a  y as necessary..
2c7d5 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  */.static int sq
2c7d6 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 5f  lite3_get_table_
2c7d7 63 62 28 76 6f 69 64 20 2a 70 41 72 67 2c 20 69  cb(void *pArg, i
2c7d8 6e 74 20 6e 43 6f 6c 2c 20 63 68 61 72 20 2a 2a  nt nCol, char **
2c7d9 61 72 67 76 2c 20 63 68 61 72 20 2a 2a 63 6f 6c  argv, char **col
2c7da 76 29 7b 0a 20 20 54 61 62 52 65 73 75 6c 74 20  v){.  TabResult 
2c7db 2a 70 20 3d 20 28 54 61 62 52 65 73 75 6c 74 2a  *p = (TabResult*
2c7dc 29 70 41 72 67 3b 20 20 2f 2a 20 52 65 73 75 6c  )pArg;  /* Resul
2c7dd 74 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 2a 2f  t accumulator */
2c7de 0a 20 20 69 6e 74 20 6e 65 65 64 3b 20 20 20 20  .  int need;    
2c7df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c7e0 20 20 20 20 20 2f 2a 20 53 6c 6f 74 73 20 6e 65       /* Slots ne
2c7e1 65 64 65 64 20 69 6e 20 70 2d 3e 61 7a 52 65 73  eded in p->azRes
2c7e2 75 6c 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69  ult[] */.  int i
2c7e3 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2c7e4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c7e5 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
2c7e6 20 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20    char *z;      
2c7e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c7e8 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20      /* A single 
2c7e9 63 6f 6c 75 6d 6e 20 6f 66 20 72 65 73 75 6c 74  column of result
2c7ea 20 2a 2f 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73   */..  /* Make s
2c7eb 75 72 65 20 74 68 65 72 65 20 69 73 20 65 6e 6f  ure there is eno
2c7ec 75 67 68 20 73 70 61 63 65 20 69 6e 20 70 2d 3e  ugh space in p->
2c7ed 61 7a 52 65 73 75 6c 74 20 74 6f 20 68 6f 6c 64  azResult to hold
2c7ee 20 65 76 65 72 79 74 68 69 6e 67 0a 20 20 2a 2a   everything.  **
2c7ef 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65 6d 65   we need to reme
2c7f0 6d 62 65 72 20 66 72 6f 6d 20 74 68 69 73 20 69  mber from this i
2c7f1 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65  nvocation of the
2c7f2 20 63 61 6c 6c 62 61 63 6b 2e 0a 20 20 2a 2f 0a   callback..  */.
2c7f3 20 20 69 66 28 20 70 2d 3e 6e 52 6f 77 3d 3d 30    if( p->nRow==0
2c7f4 20 26 26 20 61 72 67 76 21 3d 30 20 29 7b 0a 20   && argv!=0 ){. 
2c7f5 20 20 20 6e 65 65 64 20 3d 20 6e 43 6f 6c 2a 32     need = nCol*2
2c7f6 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e  ;.  }else{.    n
2c7f7 65 65 64 20 3d 20 6e 43 6f 6c 3b 0a 20 20 7d 0a  eed = nCol;.  }.
2c7f8 20 20 69 66 28 20 70 2d 3e 6e 44 61 74 61 20 2b    if( p->nData +
2c7f9 20 6e 65 65 64 20 3e 20 70 2d 3e 6e 41 6c 6c 6f   need > p->nAllo
2c7fa 63 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a  c ){.    char **
2c7fb 61 7a 4e 65 77 3b 0a 20 20 20 20 70 2d 3e 6e 41  azNew;.    p->nA
2c7fc 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 41 6c 6c 6f 63  lloc = p->nAlloc
2c7fd 2a 32 20 2b 20 6e 65 65 64 3b 0a 20 20 20 20 61  *2 + need;.    a
2c7fe 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72  zNew = sqlite3_r
2c7ff 65 61 6c 6c 6f 63 28 20 70 2d 3e 61 7a 52 65 73  ealloc( p->azRes
2c800 75 6c 74 2c 20 73 69 7a 65 6f 66 28 63 68 61 72  ult, sizeof(char
2c801 2a 29 2a 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a  *)*p->nAlloc );.
2c802 20 20 20 20 69 66 28 20 61 7a 4e 65 77 3d 3d 30      if( azNew==0
2c803 20 29 20 67 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66   ) goto malloc_f
2c804 61 69 6c 65 64 3b 0a 20 20 20 20 70 2d 3e 61 7a  ailed;.    p->az
2c805 52 65 73 75 6c 74 20 3d 20 61 7a 4e 65 77 3b 0a  Result = azNew;.
2c806 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69    }..  /* If thi
2c807 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20 72  s is the first r
2c808 6f 77 2c 20 74 68 65 6e 20 67 65 6e 65 72 61 74  ow, then generat
2c809 65 20 61 6e 20 65 78 74 72 61 20 72 6f 77 20 63  e an extra row c
2c80a 6f 6e 74 61 69 6e 69 6e 67 0a 20 20 2a 2a 20 74  ontaining.  ** t
2c80b 68 65 20 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20  he names of all 
2c80c 63 6f 6c 75 6d 6e 73 2e 0a 20 20 2a 2f 0a 20 20  columns..  */.  
2c80d 69 66 28 20 70 2d 3e 6e 52 6f 77 3d 3d 30 20 29  if( p->nRow==0 )
2c80e 7b 0a 20 20 20 20 70 2d 3e 6e 43 6f 6c 75 6d 6e  {.    p->nColumn
2c80f 20 3d 20 6e 43 6f 6c 3b 0a 20 20 20 20 66 6f 72   = nCol;.    for
2c810 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
2c811 2b 29 7b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71  +){.      z = sq
2c812 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25  lite3_mprintf("%
2c813 73 22 2c 20 63 6f 6c 76 5b 69 5d 29 3b 0a 20 20  s", colv[i]);.  
2c814 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 67      if( z==0 ) g
2c815 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65  oto malloc_faile
2c816 64 3b 0a 20 20 20 20 20 20 70 2d 3e 61 7a 52 65  d;.      p->azRe
2c817 73 75 6c 74 5b 70 2d 3e 6e 44 61 74 61 2b 2b 5d  sult[p->nData++]
2c818 20 3d 20 7a 3b 0a 20 20 20 20 7d 0a 20 20 7d 65   = z;.    }.  }e
2c819 6c 73 65 20 69 66 28 20 70 2d 3e 6e 43 6f 6c 75  lse if( p->nColu
2c81a 6d 6e 21 3d 6e 43 6f 6c 20 29 7b 0a 20 20 20 20  mn!=nCol ){.    
2c81b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e  sqlite3_free(p->
2c81c 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d  zErrMsg);.    p-
2c81d 3e 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74  >zErrMsg = sqlit
2c81e 65 33 5f 6d 70 72 69 6e 74 66 28 0a 20 20 20 20  e3_mprintf(.    
2c81f 20 20 20 22 73 71 6c 69 74 65 33 5f 67 65 74 5f     "sqlite3_get_
2c820 74 61 62 6c 65 28 29 20 63 61 6c 6c 65 64 20 77  table() called w
2c821 69 74 68 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20  ith two or more 
2c822 69 6e 63 6f 6d 70 61 74 69 62 6c 65 20 71 75 65  incompatible que
2c823 72 69 65 73 22 0a 20 20 20 20 29 3b 0a 20 20 20  ries".    );.   
2c824 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f   p->rc = SQLITE_
2c825 45 52 52 4f 52 3b 0a 20 20 20 20 72 65 74 75 72  ERROR;.    retur
2c826 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43  n 1;.  }..  /* C
2c827 6f 70 79 20 6f 76 65 72 20 74 68 65 20 72 6f 77  opy over the row
2c828 20 64 61 74 61 0a 20 20 2a 2f 0a 20 20 69 66 28   data.  */.  if(
2c829 20 61 72 67 76 21 3d 30 20 29 7b 0a 20 20 20 20   argv!=0 ){.    
2c82a 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b  for(i=0; i<nCol;
2c82b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   i++){.      if(
2c82c 20 61 72 67 76 5b 69 5d 3d 3d 30 20 29 7b 0a 20   argv[i]==0 ){. 
2c82d 20 20 20 20 20 20 20 7a 20 3d 20 30 3b 0a 20 20         z = 0;.  
2c82e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2c82f 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69 74     int n = sqlit
2c830 65 33 53 74 72 6c 65 6e 33 30 28 61 72 67 76 5b  e3Strlen30(argv[
2c831 69 5d 29 2b 31 3b 0a 20 20 20 20 20 20 20 20 7a  i])+1;.        z
2c832 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
2c833 63 28 20 6e 20 29 3b 0a 20 20 20 20 20 20 20 20  c( n );.        
2c834 69 66 28 20 7a 3d 3d 30 20 29 20 67 6f 74 6f 20  if( z==0 ) goto 
2c835 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b 0a 20  malloc_failed;. 
2c836 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c         memcpy(z,
2c837 20 61 72 67 76 5b 69 5d 2c 20 6e 29 3b 0a 20 20   argv[i], n);.  
2c838 20 20 20 20 7d 0a 20 20 20 20 20 20 70 2d 3e 61      }.      p->a
2c839 7a 52 65 73 75 6c 74 5b 70 2d 3e 6e 44 61 74 61  zResult[p->nData
2c83a 2b 2b 5d 20 3d 20 7a 3b 0a 20 20 20 20 7d 0a 20  ++] = z;.    }. 
2c83b 20 20 20 70 2d 3e 6e 52 6f 77 2b 2b 3b 0a 20 20     p->nRow++;.  
2c83c 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 0a 6d  }.  return 0;..m
2c83d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3a 0a 20 20  alloc_failed:.  
2c83e 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e  p->rc = SQLITE_N
2c83f 4f 4d 45 4d 3b 0a 20 20 72 65 74 75 72 6e 20 31  OMEM;.  return 1
2c840 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79  ;.}../*.** Query
2c841 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20   the database.  
2c842 42 75 74 20 69 6e 73 74 65 61 64 20 6f 66 20 69  But instead of i
2c843 6e 76 6f 6b 69 6e 67 20 61 20 63 61 6c 6c 62 61  nvoking a callba
2c844 63 6b 20 66 6f 72 20 65 61 63 68 20 72 6f 77 2c  ck for each row,
2c845 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 66 6f 72  .** malloc() for
2c846 20 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   space to hold t
2c847 68 65 20 72 65 73 75 6c 74 20 61 6e 64 20 72 65  he result and re
2c848 74 75 72 6e 20 74 68 65 20 65 6e 74 69 72 65 20  turn the entire 
2c849 72 65 73 75 6c 74 73 0a 2a 2a 20 61 74 20 74 68  results.** at th
2c84a 65 20 63 6f 6e 63 6c 75 73 69 6f 6e 20 6f 66 20  e conclusion of 
2c84b 74 68 65 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20  the call..**.** 
2c84c 54 68 65 20 72 65 73 75 6c 74 20 74 68 61 74 20  The result that 
2c84d 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 2a  is written to **
2c84e 2a 70 61 7a 52 65 73 75 6c 74 20 69 73 20 68 65  *pazResult is he
2c84f 6c 64 20 69 6e 20 6d 65 6d 6f 72 79 20 6f 62 74  ld in memory obt
2c850 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20 6d 61  ained.** from ma
2c851 6c 6c 6f 63 28 29 2e 20 20 42 75 74 20 74 68 65  lloc().  But the
2c852 20 63 61 6c 6c 65 72 20 63 61 6e 6e 6f 74 20 66   caller cannot f
2c853 72 65 65 20 74 68 69 73 20 6d 65 6d 6f 72 79 20  ree this memory 
2c854 64 69 72 65 63 74 6c 79 2e 20 20 0a 2a 2a 20 49  directly.  .** I
2c855 6e 73 74 65 61 64 2c 20 74 68 65 20 65 6e 74 69  nstead, the enti
2c856 72 65 20 74 61 62 6c 65 20 73 68 6f 75 6c 64 20  re table should 
2c857 62 65 20 70 61 73 73 65 64 20 74 6f 20 73 71 6c  be passed to sql
2c858 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28  ite3_free_table(
2c859 29 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 63 61  ) when.** the ca
2c85a 6c 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20  lling procedure 
2c85b 69 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e  is finished usin
2c85c 67 20 69 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  g it..*/.SQLITE_
2c85d 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2c85e 67 65 74 5f 74 61 62 6c 65 28 0a 20 20 73 71 6c  get_table(.  sql
2c85f 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
2c860 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2c861 64 61 74 61 62 61 73 65 20 6f 6e 20 77 68 69 63  database on whic
2c862 68 20 74 68 65 20 53 51 4c 20 65 78 65 63 75 74  h the SQL execut
2c863 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  es */.  const ch
2c864 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20  ar *zSql,       
2c865 20 20 20 20 2f 2a 20 54 68 65 20 53 51 4c 20 74      /* The SQL t
2c866 6f 20 62 65 20 65 78 65 63 75 74 65 64 20 2a 2f  o be executed */
2c867 0a 20 20 63 68 61 72 20 2a 2a 2a 70 61 7a 52 65  .  char ***pazRe
2c868 73 75 6c 74 2c 20 20 20 20 20 20 20 20 20 20 2f  sult,          /
2c869 2a 20 57 72 69 74 65 20 74 68 65 20 72 65 73 75  * Write the resu
2c86a 6c 74 20 74 61 62 6c 65 20 68 65 72 65 20 2a 2f  lt table here */
2c86b 0a 20 20 69 6e 74 20 2a 70 6e 52 6f 77 2c 20 20  .  int *pnRow,  
2c86c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2c86d 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62  * Write the numb
2c86e 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20 74 68  er of rows in th
2c86f 65 20 72 65 73 75 6c 74 20 68 65 72 65 20 2a 2f  e result here */
2c870 0a 20 20 69 6e 74 20 2a 70 6e 43 6f 6c 75 6d 6e  .  int *pnColumn
2c871 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
2c872 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d 62  * Write the numb
2c873 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66  er of columns of
2c874 20 72 65 73 75 6c 74 20 68 65 72 65 20 2a 2f 0a   result here */.
2c875 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d 73    char **pzErrMs
2c876 67 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  g             /*
2c877 20 57 72 69 74 65 20 65 72 72 6f 72 20 6d 65 73   Write error mes
2c878 73 61 67 65 73 20 68 65 72 65 20 2a 2f 0a 29 7b  sages here */.){
2c879 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 54 61 62  .  int rc;.  Tab
2c87a 52 65 73 75 6c 74 20 72 65 73 3b 0a 0a 20 20 2a  Result res;..  *
2c87b 70 61 7a 52 65 73 75 6c 74 20 3d 20 30 3b 0a 20  pazResult = 0;. 
2c87c 20 69 66 28 20 70 6e 43 6f 6c 75 6d 6e 20 29 20   if( pnColumn ) 
2c87d 2a 70 6e 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20  *pnColumn = 0;. 
2c87e 20 69 66 28 20 70 6e 52 6f 77 20 29 20 2a 70 6e   if( pnRow ) *pn
2c87f 52 6f 77 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  Row = 0;.  if( p
2c880 7a 45 72 72 4d 73 67 20 29 20 2a 70 7a 45 72 72  zErrMsg ) *pzErr
2c881 4d 73 67 20 3d 20 30 3b 0a 20 20 72 65 73 2e 7a  Msg = 0;.  res.z
2c882 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 72 65  ErrMsg = 0;.  re
2c883 73 2e 6e 52 6f 77 20 3d 20 30 3b 0a 20 20 72 65  s.nRow = 0;.  re
2c884 73 2e 6e 43 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 20  s.nColumn = 0;. 
2c885 20 72 65 73 2e 6e 44 61 74 61 20 3d 20 31 3b 0a   res.nData = 1;.
2c886 20 20 72 65 73 2e 6e 41 6c 6c 6f 63 20 3d 20 32    res.nAlloc = 2
2c887 30 3b 0a 20 20 72 65 73 2e 72 63 20 3d 20 53 51  0;.  res.rc = SQ
2c888 4c 49 54 45 5f 4f 4b 3b 0a 20 20 72 65 73 2e 61  LITE_OK;.  res.a
2c889 7a 52 65 73 75 6c 74 20 3d 20 73 71 6c 69 74 65  zResult = sqlite
2c88a 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
2c88b 63 68 61 72 2a 29 2a 72 65 73 2e 6e 41 6c 6c 6f  char*)*res.nAllo
2c88c 63 20 29 3b 0a 20 20 69 66 28 20 72 65 73 2e 61  c );.  if( res.a
2c88d 7a 52 65 73 75 6c 74 3d 3d 30 20 29 7b 0a 20 20  zResult==0 ){.  
2c88e 20 20 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d     db->errCode =
2c88f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
2c890 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2c891 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72  E_NOMEM;.  }.  r
2c892 65 73 2e 61 7a 52 65 73 75 6c 74 5b 30 5d 20 3d  es.azResult[0] =
2c893 20 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   0;.  rc = sqlit
2c894 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 71 6c  e3_exec(db, zSql
2c895 2c 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61  , sqlite3_get_ta
2c896 62 6c 65 5f 63 62 2c 20 26 72 65 73 2c 20 70 7a  ble_cb, &res, pz
2c897 45 72 72 4d 73 67 29 3b 0a 20 20 61 73 73 65 72  ErrMsg);.  asser
2c898 74 28 20 73 69 7a 65 6f 66 28 72 65 73 2e 61 7a  t( sizeof(res.az
2c899 52 65 73 75 6c 74 5b 30 5d 29 3e 3d 20 73 69 7a  Result[0])>= siz
2c89a 65 6f 66 28 72 65 73 2e 6e 44 61 74 61 29 20 29  eof(res.nData) )
2c89b 3b 0a 20 20 72 65 73 2e 61 7a 52 65 73 75 6c 74  ;.  res.azResult
2c89c 5b 30 5d 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  [0] = SQLITE_INT
2c89d 5f 54 4f 5f 50 54 52 28 72 65 73 2e 6e 44 61 74  _TO_PTR(res.nDat
2c89e 61 29 3b 0a 20 20 69 66 28 20 28 72 63 26 30 78  a);.  if( (rc&0x
2c89f 66 66 29 3d 3d 53 51 4c 49 54 45 5f 41 42 4f 52  ff)==SQLITE_ABOR
2c8a0 54 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  T ){.    sqlite3
2c8a1 5f 66 72 65 65 5f 74 61 62 6c 65 28 26 72 65 73  _free_table(&res
2c8a2 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 29 3b 0a 20  .azResult[1]);. 
2c8a3 20 20 20 69 66 28 20 72 65 73 2e 7a 45 72 72 4d     if( res.zErrM
2c8a4 73 67 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  sg ){.      if( 
2c8a5 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20  pzErrMsg ){.    
2c8a6 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
2c8a7 28 2a 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  (*pzErrMsg);.   
2c8a8 20 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d       *pzErrMsg =
2c8a9 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
2c8aa 28 22 25 73 22 2c 72 65 73 2e 7a 45 72 72 4d 73  ("%s",res.zErrMs
2c8ab 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  g);.      }.    
2c8ac 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 72    sqlite3_free(r
2c8ad 65 73 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  es.zErrMsg);.   
2c8ae 20 7d 0a 20 20 20 20 64 62 2d 3e 65 72 72 43 6f   }.    db->errCo
2c8af 64 65 20 3d 20 72 65 73 2e 72 63 3b 20 20 2f 2a  de = res.rc;  /*
2c8b0 20 41 73 73 75 6d 65 20 33 32 2d 62 69 74 20 61   Assume 32-bit a
2c8b1 73 73 69 67 6e 6d 65 6e 74 20 69 73 20 61 74 6f  ssignment is ato
2c8b2 6d 69 63 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  mic */.    retur
2c8b3 6e 20 72 65 73 2e 72 63 3b 0a 20 20 7d 0a 20 20  n res.rc;.  }.  
2c8b4 73 71 6c 69 74 65 33 5f 66 72 65 65 28 72 65 73  sqlite3_free(res
2c8b5 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 69 66 28  .zErrMsg);.  if(
2c8b6 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2c8b7 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
2c8b8 65 65 5f 74 61 62 6c 65 28 26 72 65 73 2e 61 7a  ee_table(&res.az
2c8b9 52 65 73 75 6c 74 5b 31 5d 29 3b 0a 20 20 20 20  Result[1]);.    
2c8ba 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
2c8bb 20 69 66 28 20 72 65 73 2e 6e 41 6c 6c 6f 63 3e   if( res.nAlloc>
2c8bc 72 65 73 2e 6e 44 61 74 61 20 29 7b 0a 20 20 20  res.nData ){.   
2c8bd 20 63 68 61 72 20 2a 2a 61 7a 4e 65 77 3b 0a 20   char **azNew;. 
2c8be 20 20 20 61 7a 4e 65 77 20 3d 20 73 71 6c 69 74     azNew = sqlit
2c8bf 65 33 5f 72 65 61 6c 6c 6f 63 28 20 72 65 73 2e  e3_realloc( res.
2c8c0 61 7a 52 65 73 75 6c 74 2c 20 73 69 7a 65 6f 66  azResult, sizeof
2c8c1 28 63 68 61 72 2a 29 2a 72 65 73 2e 6e 44 61 74  (char*)*res.nDat
2c8c2 61 20 29 3b 0a 20 20 20 20 69 66 28 20 61 7a 4e  a );.    if( azN
2c8c3 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  ew==0 ){.      s
2c8c4 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c  qlite3_free_tabl
2c8c5 65 28 26 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b  e(&res.azResult[
2c8c6 31 5d 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 65  1]);.      db->e
2c8c7 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f  rrCode = SQLITE_
2c8c8 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 72 65 74  NOMEM;.      ret
2c8c9 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
2c8ca 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 73 2e  ;.    }.    res.
2c8cb 61 7a 52 65 73 75 6c 74 20 3d 20 61 7a 4e 65 77  azResult = azNew
2c8cc 3b 0a 20 20 7d 0a 20 20 2a 70 61 7a 52 65 73 75  ;.  }.  *pazResu
2c8cd 6c 74 20 3d 20 26 72 65 73 2e 61 7a 52 65 73 75  lt = &res.azResu
2c8ce 6c 74 5b 31 5d 3b 0a 20 20 69 66 28 20 70 6e 43  lt[1];.  if( pnC
2c8cf 6f 6c 75 6d 6e 20 29 20 2a 70 6e 43 6f 6c 75 6d  olumn ) *pnColum
2c8d0 6e 20 3d 20 72 65 73 2e 6e 43 6f 6c 75 6d 6e 3b  n = res.nColumn;
2c8d1 0a 20 20 69 66 28 20 70 6e 52 6f 77 20 29 20 2a  .  if( pnRow ) *
2c8d2 70 6e 52 6f 77 20 3d 20 72 65 73 2e 6e 52 6f 77  pnRow = res.nRow
2c8d3 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
2c8d4 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
2c8d5 74 69 6e 65 20 66 72 65 65 73 20 74 68 65 20 73  tine frees the s
2c8d6 70 61 63 65 20 74 68 65 20 73 71 6c 69 74 65 33  pace the sqlite3
2c8d7 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 6d 61 6c  _get_table() mal
2c8d8 6c 6f 63 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  loced..*/.SQLITE
2c8d9 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
2c8da 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 0a 20 20  3_free_table(.  
2c8db 63 68 61 72 20 2a 2a 61 7a 52 65 73 75 6c 74 20  char **azResult 
2c8dc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
2c8dd 73 75 6c 74 20 72 65 74 75 72 6e 65 64 20 66 72  sult returned fr
2c8de 6f 6d 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  om from sqlite3_
2c8df 67 65 74 5f 74 61 62 6c 65 28 29 20 2a 2f 0a 29  get_table() */.)
2c8e0 7b 0a 20 20 69 66 28 20 61 7a 52 65 73 75 6c 74  {.  if( azResult
2c8e1 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6e   ){.    int i, n
2c8e2 3b 0a 20 20 20 20 61 7a 52 65 73 75 6c 74 2d 2d  ;.    azResult--
2c8e3 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 61 7a  ;.    assert( az
2c8e4 52 65 73 75 6c 74 21 3d 30 20 29 3b 0a 20 20 20  Result!=0 );.   
2c8e5 20 6e 20 3d 20 53 51 4c 49 54 45 5f 50 54 52 5f   n = SQLITE_PTR_
2c8e6 54 4f 5f 49 4e 54 28 61 7a 52 65 73 75 6c 74 5b  TO_INT(azResult[
2c8e7 30 5d 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31  0]);.    for(i=1
2c8e8 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 20 69 66 28  ; i<n; i++){ if(
2c8e9 20 61 7a 52 65 73 75 6c 74 5b 69 5d 20 29 20 73   azResult[i] ) s
2c8ea 71 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 52 65  qlite3_free(azRe
2c8eb 73 75 6c 74 5b 69 5d 29 3b 20 7d 0a 20 20 20 20  sult[i]); }.    
2c8ec 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 7a 52  sqlite3_free(azR
2c8ed 65 73 75 6c 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 23  esult);.  }.}..#
2c8ee 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2c8ef 4f 4d 49 54 5f 47 45 54 5f 54 41 42 4c 45 20 2a  OMIT_GET_TABLE *
2c8f0 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
2c8f1 2a 2a 20 45 6e 64 20 6f 66 20 74 61 62 6c 65 2e  ** End of table.
2c8f2 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2c8f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c8f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c8f5 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2c8f6 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 74 72  ** Begin file tr
2c8f7 69 67 67 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  igger.c ********
2c8f8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c8f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c8fa 2a 2f 0a 2f 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20  */./*.**.** The 
2c8fb 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
2c8fc 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
2c8fd 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
2c8fe 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
2c8ff 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
2c900 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
2c901 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
2c902 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
2c903 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
2c904 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
2c905 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
2c906 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
2c907 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
2c908 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
2c909 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
2c90a 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
2c90b 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
2c90c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c90d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c90e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c90f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c910 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 24 49  ****.**.**.** $I
2c911 64 3a 20 74 72 69 67 67 65 72 2e 63 2c 76 20 31  d: trigger.c,v 1
2c912 2e 31 34 33 20 32 30 30 39 2f 30 38 2f 31 30 20  .143 2009/08/10 
2c913 30 33 3a 35 37 3a 35 38 20 73 68 61 6e 65 20 45  03:57:58 shane E
2c914 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66  xp $.*/..#ifndef
2c915 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
2c916 47 47 45 52 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  GGER./*.** Delet
2c917 65 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  e a linked list 
2c918 6f 66 20 54 72 69 67 67 65 72 53 74 65 70 20 73  of TriggerStep s
2c919 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 53 51  tructures..*/.SQ
2c91a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2c91b 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54  d sqlite3DeleteT
2c91c 72 69 67 67 65 72 53 74 65 70 28 73 71 6c 69 74  riggerStep(sqlit
2c91d 65 33 20 2a 64 62 2c 20 54 72 69 67 67 65 72 53  e3 *db, TriggerS
2c91e 74 65 70 20 2a 70 54 72 69 67 67 65 72 53 74 65  tep *pTriggerSte
2c91f 70 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 54 72  p){.  while( pTr
2c920 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20 20 20  iggerStep ){.   
2c921 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 20 70   TriggerStep * p
2c922 54 6d 70 20 3d 20 70 54 72 69 67 67 65 72 53 74  Tmp = pTriggerSt
2c923 65 70 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72  ep;.    pTrigger
2c924 53 74 65 70 20 3d 20 70 54 72 69 67 67 65 72 53  Step = pTriggerS
2c925 74 65 70 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 20  tep->pNext;..   
2c926 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2c927 74 65 28 64 62 2c 20 70 54 6d 70 2d 3e 70 57 68  te(db, pTmp->pWh
2c928 65 72 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ere);.    sqlite
2c929 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
2c92a 64 62 2c 20 70 54 6d 70 2d 3e 70 45 78 70 72 4c  db, pTmp->pExprL
2c92b 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ist);.    sqlite
2c92c 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
2c92d 2c 20 70 54 6d 70 2d 3e 70 53 65 6c 65 63 74 29  , pTmp->pSelect)
2c92e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64 4c  ;.    sqlite3IdL
2c92f 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54  istDelete(db, pT
2c930 6d 70 2d 3e 70 49 64 4c 69 73 74 29 3b 0a 0a 20  mp->pIdList);.. 
2c931 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2c932 28 64 62 2c 20 70 54 6d 70 29 3b 0a 20 20 7d 0a  (db, pTmp);.  }.
2c933 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74  }../*.** Given t
2c934 61 62 6c 65 20 70 54 61 62 2c 20 72 65 74 75 72  able pTab, retur
2c935 6e 20 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  n a list of all 
2c936 74 68 65 20 74 72 69 67 67 65 72 73 20 61 74 74  the triggers att
2c937 61 63 68 65 64 20 74 6f 20 0a 2a 2a 20 74 68 65  ached to .** the
2c938 20 74 61 62 6c 65 2e 20 54 68 65 20 6c 69 73 74   table. The list
2c939 20 69 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79   is connected by
2c93a 20 54 72 69 67 67 65 72 2e 70 4e 65 78 74 20 70   Trigger.pNext p
2c93b 6f 69 6e 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 41  ointers..**.** A
2c93c 6c 6c 20 6f 66 20 74 68 65 20 74 72 69 67 67 65  ll of the trigge
2c93d 72 73 20 6f 6e 20 70 54 61 62 20 74 68 61 74 20  rs on pTab that 
2c93e 61 72 65 20 69 6e 20 74 68 65 20 73 61 6d 65 20  are in the same 
2c93f 64 61 74 61 62 61 73 65 20 61 73 20 70 54 61 62  database as pTab
2c940 0a 2a 2a 20 61 72 65 20 61 6c 72 65 61 64 79 20  .** are already 
2c941 61 74 74 61 63 68 65 64 20 74 6f 20 70 54 61 62  attached to pTab
2c942 2d 3e 70 54 72 69 67 67 65 72 2e 20 20 42 75 74  ->pTrigger.  But
2c943 20 74 68 65 72 65 20 6d 69 67 68 74 20 62 65 20   there might be 
2c944 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 74 72  additional.** tr
2c945 69 67 67 65 72 73 20 6f 6e 20 70 54 61 62 20 69  iggers on pTab i
2c946 6e 20 74 68 65 20 54 45 4d 50 20 73 63 68 65 6d  n the TEMP schem
2c947 61 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  a.  This routine
2c948 20 70 72 65 70 65 6e 64 73 20 61 6c 6c 0a 2a 2a   prepends all.**
2c949 20 54 45 4d 50 20 74 72 69 67 67 65 72 73 20 6f   TEMP triggers o
2c94a 6e 20 70 54 61 62 20 74 6f 20 74 68 65 20 62 65  n pTab to the be
2c94b 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70  ginning of the p
2c94c 54 61 62 2d 3e 70 54 72 69 67 67 65 72 20 6c 69  Tab->pTrigger li
2c94d 73 74 0a 2a 2a 20 61 6e 64 20 72 65 74 75 72 6e  st.** and return
2c94e 73 20 74 68 65 20 63 6f 6d 62 69 6e 65 64 20 6c  s the combined l
2c94f 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 73 74  ist..**.** To st
2c950 61 74 65 20 69 74 20 61 6e 6f 74 68 65 72 20 77  ate it another w
2c951 61 79 3a 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ay:  This routin
2c952 65 20 72 65 74 75 72 6e 73 20 61 20 6c 69 73 74  e returns a list
2c953 20 6f 66 20 61 6c 6c 20 74 72 69 67 67 65 72 73   of all triggers
2c954 0a 2a 2a 20 74 68 61 74 20 66 69 72 65 20 6f 66  .** that fire of
2c955 66 20 6f 66 20 70 54 61 62 2e 20 20 54 68 65 20  f of pTab.  The 
2c956 6c 69 73 74 20 77 69 6c 6c 20 69 6e 63 6c 75 64  list will includ
2c957 65 20 61 6e 79 20 54 45 4d 50 20 74 72 69 67 67  e any TEMP trigg
2c958 65 72 73 20 6f 6e 0a 2a 2a 20 70 54 61 62 20 61  ers on.** pTab a
2c959 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 74 72  s well as the tr
2c95a 69 67 67 65 72 73 20 6c 69 73 65 64 20 69 6e 20  iggers lised in 
2c95b 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72 2e 0a  pTab->pTrigger..
2c95c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2c95d 45 20 54 72 69 67 67 65 72 20 2a 73 71 6c 69 74  E Trigger *sqlit
2c95e 65 33 54 72 69 67 67 65 72 4c 69 73 74 28 50 61  e3TriggerList(Pa
2c95f 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62  rse *pParse, Tab
2c960 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 53 63 68  le *pTab){.  Sch
2c961 65 6d 61 20 2a 20 63 6f 6e 73 74 20 70 54 6d 70  ema * const pTmp
2c962 53 63 68 65 6d 61 20 3d 20 70 50 61 72 73 65 2d  Schema = pParse-
2c963 3e 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68  >db->aDb[1].pSch
2c964 65 6d 61 3b 0a 20 20 54 72 69 67 67 65 72 20 2a  ema;.  Trigger *
2c965 70 4c 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20  pList = 0;      
2c966 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2c967 69 73 74 20 6f 66 20 74 72 69 67 67 65 72 73 20  ist of triggers 
2c968 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 0a 20 20  to return */..  
2c969 69 66 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61  if( pParse->disa
2c96a 62 6c 65 54 72 69 67 67 65 72 73 20 29 7b 0a 20  bleTriggers ){. 
2c96b 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
2c96c 0a 0a 20 20 69 66 28 20 70 54 6d 70 53 63 68 65  ..  if( pTmpSche
2c96d 6d 61 21 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d  ma!=pTab->pSchem
2c96e 61 20 29 7b 0a 20 20 20 20 48 61 73 68 45 6c 65  a ){.    HashEle
2c96f 6d 20 2a 70 3b 0a 20 20 20 20 66 6f 72 28 70 3d  m *p;.    for(p=
2c970 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28  sqliteHashFirst(
2c971 26 70 54 6d 70 53 63 68 65 6d 61 2d 3e 74 72 69  &pTmpSchema->tri
2c972 67 48 61 73 68 29 3b 20 70 3b 20 70 3d 73 71 6c  gHash); p; p=sql
2c973 69 74 65 48 61 73 68 4e 65 78 74 28 70 29 29 7b  iteHashNext(p)){
2c974 0a 20 20 20 20 20 20 54 72 69 67 67 65 72 20 2a  .      Trigger *
2c975 70 54 72 69 67 20 3d 20 28 54 72 69 67 67 65 72  pTrig = (Trigger
2c976 20 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 74   *)sqliteHashDat
2c977 61 28 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20  a(p);.      if( 
2c978 70 54 72 69 67 2d 3e 70 54 61 62 53 63 68 65 6d  pTrig->pTabSchem
2c979 61 3d 3d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  a==pTab->pSchema
2c97a 0a 20 20 20 20 20 20 20 26 26 20 30 3d 3d 73 71  .       && 0==sq
2c97b 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 54 72  lite3StrICmp(pTr
2c97c 69 67 2d 3e 74 61 62 6c 65 2c 20 70 54 61 62 2d  ig->table, pTab-
2c97d 3e 7a 4e 61 6d 65 29 20 0a 20 20 20 20 20 20 29  >zName) .      )
2c97e 7b 0a 20 20 20 20 20 20 20 20 70 54 72 69 67 2d  {.        pTrig-
2c97f 3e 70 4e 65 78 74 20 3d 20 28 70 4c 69 73 74 20  >pNext = (pList 
2c980 3f 20 70 4c 69 73 74 20 3a 20 70 54 61 62 2d 3e  ? pList : pTab->
2c981 70 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20 20  pTrigger);.     
2c982 20 20 20 70 4c 69 73 74 20 3d 20 70 54 72 69 67     pList = pTrig
2c983 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2c984 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 28 70    }..  return (p
2c985 4c 69 73 74 20 3f 20 70 4c 69 73 74 20 3a 20 70  List ? pList : p
2c986 54 61 62 2d 3e 70 54 72 69 67 67 65 72 29 3b 0a  Tab->pTrigger);.
2c987 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73  }../*.** This is
2c988 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70   called by the p
2c989 61 72 73 65 72 20 77 68 65 6e 20 69 74 20 73 65  arser when it se
2c98a 65 73 20 61 20 43 52 45 41 54 45 20 54 52 49 47  es a CREATE TRIG
2c98b 47 45 52 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a  GER statement.**
2c98c 20 75 70 20 74 6f 20 74 68 65 20 70 6f 69 6e 74   up to the point
2c98d 20 6f 66 20 74 68 65 20 42 45 47 49 4e 20 62 65   of the BEGIN be
2c98e 66 6f 72 65 20 74 68 65 20 74 72 69 67 67 65 72  fore the trigger
2c98f 20 61 63 74 69 6f 6e 73 2e 20 20 41 20 54 72 69   actions.  A Tri
2c990 67 67 65 72 0a 2a 2a 20 73 74 72 75 63 74 75 72  gger.** structur
2c991 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 62  e is generated b
2c992 61 73 65 64 20 6f 6e 20 74 68 65 20 69 6e 66 6f  ased on the info
2c993 72 6d 61 74 69 6f 6e 20 61 76 61 69 6c 61 62 6c  rmation availabl
2c994 65 20 61 6e 64 20 73 74 6f 72 65 64 0a 2a 2a 20  e and stored.** 
2c995 69 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  in pParse->pNewT
2c996 72 69 67 67 65 72 2e 20 20 41 66 74 65 72 20 74  rigger.  After t
2c997 68 65 20 74 72 69 67 67 65 72 20 61 63 74 69 6f  he trigger actio
2c998 6e 73 20 68 61 76 65 20 62 65 65 6e 20 70 61 72  ns have been par
2c999 73 65 64 2c 20 74 68 65 0a 2a 2a 20 73 71 6c 69  sed, the.** sqli
2c99a 74 65 33 46 69 6e 69 73 68 54 72 69 67 67 65 72  te3FinishTrigger
2c99b 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  () function is c
2c99c 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74  alled to complet
2c99d 65 20 74 68 65 20 74 72 69 67 67 65 72 0a 2a 2a  e the trigger.**
2c99e 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 70 72   construction pr
2c99f 6f 63 65 73 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ocess..*/.SQLITE
2c9a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2c9a1 6c 69 74 65 33 42 65 67 69 6e 54 72 69 67 67 65  lite3BeginTrigge
2c9a2 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  r(.  Parse *pPar
2c9a3 73 65 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  se,      /* The 
2c9a4 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20 6f 66  parse context of
2c9a5 20 74 68 65 20 43 52 45 41 54 45 20 54 52 49 47   the CREATE TRIG
2c9a6 47 45 52 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  GER statement */
2c9a7 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31  .  Token *pName1
2c9a8 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 61  ,      /* The na
2c9a9 6d 65 20 6f 66 20 74 68 65 20 74 72 69 67 67 65  me of the trigge
2c9aa 72 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e  r */.  Token *pN
2c9ab 61 6d 65 32 2c 20 20 20 20 20 20 2f 2a 20 54 68  ame2,      /* Th
2c9ac 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72  e name of the tr
2c9ad 69 67 67 65 72 20 2a 2f 0a 20 20 69 6e 74 20 74  igger */.  int t
2c9ae 72 5f 74 6d 2c 20 20 20 20 20 20 20 20 20 20 2f  r_tm,          /
2c9af 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 42 45 46 4f  * One of TK_BEFO
2c9b0 52 45 2c 20 54 4b 5f 41 46 54 45 52 2c 20 54 4b  RE, TK_AFTER, TK
2c9b1 5f 49 4e 53 54 45 41 44 20 2a 2f 0a 20 20 69 6e  _INSTEAD */.  in
2c9b2 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20  t op,           
2c9b3 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 49    /* One of TK_I
2c9b4 4e 53 45 52 54 2c 20 54 4b 5f 55 50 44 41 54 45  NSERT, TK_UPDATE
2c9b5 2c 20 54 4b 5f 44 45 4c 45 54 45 20 2a 2f 0a 20  , TK_DELETE */. 
2c9b6 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c 75 6d 6e   IdList *pColumn
2c9b7 73 2c 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e 20 6c  s,   /* column l
2c9b8 69 73 74 20 69 66 20 74 68 69 73 20 69 73 20 61  ist if this is a
2c9b9 6e 20 55 50 44 41 54 45 20 4f 46 20 74 72 69 67  n UPDATE OF trig
2c9ba 67 65 72 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ger */.  SrcList
2c9bb 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 2f 2a 20   *pTableName,/* 
2c9bc 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  The name of the 
2c9bd 74 61 62 6c 65 2f 76 69 65 77 20 74 68 65 20 74  table/view the t
2c9be 72 69 67 67 65 72 20 61 70 70 6c 69 65 73 20 74  rigger applies t
2c9bf 6f 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  o */.  Expr *pWh
2c9c0 65 6e 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 48  en,        /* WH
2c9c1 45 4e 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69  EN clause */.  i
2c9c2 6e 74 20 69 73 54 65 6d 70 2c 20 20 20 20 20 20  nt isTemp,      
2c9c3 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
2c9c4 65 20 54 45 4d 50 4f 52 41 52 59 20 6b 65 79 77  e TEMPORARY keyw
2c9c5 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a  ord is present *
2c9c6 2f 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 20  /.  int noErr   
2c9c7 20 20 20 20 20 20 20 20 2f 2a 20 53 75 70 70 72          /* Suppr
2c9c8 65 73 73 20 65 72 72 6f 72 73 20 69 66 20 74 68  ess errors if th
2c9c9 65 20 74 72 69 67 67 65 72 20 61 6c 72 65 61 64  e trigger alread
2c9ca 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20  y exists */.){. 
2c9cb 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67   Trigger *pTrigg
2c9cc 65 72 20 3d 20 30 3b 20 20 2f 2a 20 54 68 65 20  er = 0;  /* The 
2c9cd 6e 65 77 20 74 72 69 67 67 65 72 20 2a 2f 0a 20  new trigger */. 
2c9ce 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20   Table *pTab;   
2c9cf 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c           /* Tabl
2c9d0 65 20 74 68 61 74 20 74 68 65 20 74 72 69 67 67  e that the trigg
2c9d1 65 72 20 66 69 72 65 73 20 6f 66 66 20 6f 66 20  er fires off of 
2c9d2 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65  */.  char *zName
2c9d3 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
2c9d4 4e 61 6d 65 20 6f 66 20 74 68 65 20 74 72 69 67  Name of the trig
2c9d5 67 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ger */.  sqlite3
2c9d6 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2c9d7 62 3b 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62  b;  /* The datab
2c9d8 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
2c9d9 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
2c9da 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2c9db 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 73  he database to s
2c9dc 74 6f 72 65 20 74 68 65 20 74 72 69 67 67 65 72  tore the trigger
2c9dd 20 69 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a   in */.  Token *
2c9de 70 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  pName;          
2c9df 20 2f 2a 20 54 68 65 20 75 6e 71 75 61 6c 69 66   /* The unqualif
2c9e0 69 65 64 20 64 62 20 6e 61 6d 65 20 2a 2f 0a 20  ied db name */. 
2c9e1 20 44 62 46 69 78 65 72 20 73 46 69 78 3b 20 20   DbFixer sFix;  
2c9e2 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74           /* Stat
2c9e3 65 20 76 65 63 74 6f 72 20 66 6f 72 20 74 68 65  e vector for the
2c9e4 20 44 42 20 66 69 78 65 72 20 2a 2f 0a 20 20 69   DB fixer */.  i
2c9e5 6e 74 20 69 54 61 62 44 62 3b 20 20 20 20 20 20  nt iTabDb;      
2c9e6 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
2c9e7 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
2c9e8 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a  holding pTab */.
2c9e9 0a 20 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65  .  assert( pName
2c9ea 31 21 3d 30 20 29 3b 20 20 20 2f 2a 20 70 4e 61  1!=0 );   /* pNa
2c9eb 6d 65 31 2d 3e 7a 20 6d 69 67 68 74 20 62 65 20  me1->z might be 
2c9ec 4e 55 4c 4c 2c 20 62 75 74 20 6e 6f 74 20 70 4e  NULL, but not pN
2c9ed 61 6d 65 31 20 69 74 73 65 6c 66 20 2a 2f 0a 20  ame1 itself */. 
2c9ee 20 61 73 73 65 72 74 28 20 70 4e 61 6d 65 32 21   assert( pName2!
2c9ef 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
2c9f0 6f 70 3d 3d 54 4b 5f 49 4e 53 45 52 54 20 7c 7c  op==TK_INSERT ||
2c9f1 20 6f 70 3d 3d 54 4b 5f 55 50 44 41 54 45 20 7c   op==TK_UPDATE |
2c9f2 7c 20 6f 70 3d 3d 54 4b 5f 44 45 4c 45 54 45 20  | op==TK_DELETE 
2c9f3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e  );.  assert( op>
2c9f4 30 20 26 26 20 6f 70 3c 30 78 66 66 20 29 3b 0a  0 && op<0xff );.
2c9f5 20 20 69 66 28 20 69 73 54 65 6d 70 20 29 7b 0a    if( isTemp ){.
2c9f6 20 20 20 20 2f 2a 20 49 66 20 54 45 4d 50 20 77      /* If TEMP w
2c9f7 61 73 20 73 70 65 63 69 66 69 65 64 2c 20 74 68  as specified, th
2c9f8 65 6e 20 74 68 65 20 74 72 69 67 67 65 72 20 6e  en the trigger n
2c9f9 61 6d 65 20 6d 61 79 20 6e 6f 74 20 62 65 20 71  ame may not be q
2c9fa 75 61 6c 69 66 69 65 64 2e 20 2a 2f 0a 20 20 20  ualified. */.   
2c9fb 20 69 66 28 20 70 4e 61 6d 65 32 2d 3e 6e 3e 30   if( pName2->n>0
2c9fc 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2c9fd 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
2c9fe 2c 20 22 74 65 6d 70 6f 72 61 72 79 20 74 72 69  , "temporary tri
2c9ff 67 67 65 72 20 6d 61 79 20 6e 6f 74 20 68 61 76  gger may not hav
2ca00 65 20 71 75 61 6c 69 66 69 65 64 20 6e 61 6d 65  e qualified name
2ca01 22 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74  ");.      goto t
2ca02 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a  rigger_cleanup;.
2ca03 20 20 20 20 7d 0a 20 20 20 20 69 44 62 20 3d 20      }.    iDb = 
2ca04 31 3b 0a 20 20 20 20 70 4e 61 6d 65 20 3d 20 70  1;.    pName = p
2ca05 4e 61 6d 65 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Name1;.  }else{.
2ca06 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75      /* Figure ou
2ca07 74 20 74 68 65 20 64 62 20 74 68 61 74 20 74 68  t the db that th
2ca08 65 20 74 68 65 20 74 72 69 67 67 65 72 20 77 69  e the trigger wi
2ca09 6c 6c 20 62 65 20 63 72 65 61 74 65 64 20 69 6e  ll be created in
2ca0a 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20 73 71   */.    iDb = sq
2ca0b 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d 65  lite3TwoPartName
2ca0c 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c  (pParse, pName1,
2ca0d 20 70 4e 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29   pName2, &pName)
2ca0e 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3c 30 20  ;.    if( iDb<0 
2ca0f 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 72  ){.      goto tr
2ca10 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
2ca11 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
2ca12 66 20 74 68 65 20 74 72 69 67 67 65 72 20 6e 61  f the trigger na
2ca13 6d 65 20 77 61 73 20 75 6e 71 75 61 6c 69 66 69  me was unqualifi
2ca14 65 64 2c 20 61 6e 64 20 74 68 65 20 74 61 62 6c  ed, and the tabl
2ca15 65 20 69 73 20 61 20 74 65 6d 70 20 74 61 62 6c  e is a temp tabl
2ca16 65 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 73 65 74  e,.  ** then set
2ca17 20 69 44 62 20 74 6f 20 31 20 74 6f 20 63 72 65   iDb to 1 to cre
2ca18 61 74 65 20 74 68 65 20 74 72 69 67 67 65 72 20  ate the trigger 
2ca19 69 6e 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  in the temporary
2ca1a 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20   database..  ** 
2ca1b 49 66 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  If sqlite3SrcLis
2ca1c 74 4c 6f 6f 6b 75 70 28 29 20 72 65 74 75 72 6e  tLookup() return
2ca1d 73 20 30 2c 20 69 6e 64 69 63 61 74 69 6e 67 20  s 0, indicating 
2ca1e 74 68 65 20 74 61 62 6c 65 20 64 6f 65 73 20 6e  the table does n
2ca1f 6f 74 0a 20 20 2a 2a 20 65 78 69 73 74 2c 20 74  ot.  ** exist, t
2ca20 68 65 20 65 72 72 6f 72 20 69 73 20 63 61 75 67  he error is caug
2ca21 68 74 20 62 79 20 74 68 65 20 62 6c 6f 63 6b 20  ht by the block 
2ca22 62 65 6c 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66  below..  */.  if
2ca23 28 20 21 70 54 61 62 6c 65 4e 61 6d 65 20 7c 7c  ( !pTableName ||
2ca24 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
2ca25 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72  d ){.    goto tr
2ca26 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
2ca27 20 7d 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69   }.  pTab = sqli
2ca28 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70  te3SrcListLookup
2ca29 28 70 50 61 72 73 65 2c 20 70 54 61 62 6c 65 4e  (pParse, pTableN
2ca2a 61 6d 65 29 3b 0a 20 20 69 66 28 20 70 4e 61 6d  ame);.  if( pNam
2ca2b 65 32 2d 3e 6e 3d 3d 30 20 26 26 20 70 54 61 62  e2->n==0 && pTab
2ca2c 20 26 26 20 70 54 61 62 2d 3e 70 53 63 68 65 6d   && pTab->pSchem
2ca2d 61 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53  a==db->aDb[1].pS
2ca2e 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 69 44 62  chema ){.    iDb
2ca2f 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   = 1;.  }..  /* 
2ca30 45 6e 73 75 72 65 20 74 68 65 20 74 61 62 6c 65  Ensure the table
2ca31 20 6e 61 6d 65 20 6d 61 74 63 68 65 73 20 64 61   name matches da
2ca32 74 61 62 61 73 65 20 6e 61 6d 65 20 61 6e 64 20  tabase name and 
2ca33 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 65  that the table e
2ca34 78 69 73 74 73 20 2a 2f 0a 20 20 69 66 28 20 64  xists */.  if( d
2ca35 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2ca36 29 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63  ) goto trigger_c
2ca37 6c 65 61 6e 75 70 3b 0a 20 20 61 73 73 65 72 74  leanup;.  assert
2ca38 28 20 70 54 61 62 6c 65 4e 61 6d 65 2d 3e 6e 53  ( pTableName->nS
2ca39 72 63 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 73  rc==1 );.  if( s
2ca3a 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 26 73  qlite3FixInit(&s
2ca3b 46 69 78 2c 20 70 50 61 72 73 65 2c 20 69 44 62  Fix, pParse, iDb
2ca3c 2c 20 22 74 72 69 67 67 65 72 22 2c 20 70 4e 61  , "trigger", pNa
2ca3d 6d 65 29 20 26 26 20 0a 20 20 20 20 20 20 73 71  me) && .      sq
2ca3e 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74 28  lite3FixSrcList(
2ca3f 26 73 46 69 78 2c 20 70 54 61 62 6c 65 4e 61 6d  &sFix, pTableNam
2ca40 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74  e) ){.    goto t
2ca41 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a  rigger_cleanup;.
2ca42 20 20 7d 0a 20 20 70 54 61 62 20 3d 20 73 71 6c    }.  pTab = sql
2ca43 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75  ite3SrcListLooku
2ca44 70 28 70 50 61 72 73 65 2c 20 70 54 61 62 6c 65  p(pParse, pTable
2ca45 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 70 54  Name);.  if( !pT
2ca46 61 62 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  ab ){.    /* The
2ca47 20 74 61 62 6c 65 20 64 6f 65 73 20 6e 6f 74 20   table does not 
2ca48 65 78 69 73 74 2e 20 2a 2f 0a 20 20 20 20 69 66  exist. */.    if
2ca49 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62 3d 3d  ( db->init.iDb==
2ca4a 31 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 69  1 ){.      /* Ti
2ca4b 63 6b 65 74 20 23 33 38 31 30 2e 0a 20 20 20 20  cket #3810..    
2ca4c 20 20 2a 2a 20 4e 6f 72 6d 61 6c 6c 79 2c 20 77    ** Normally, w
2ca4d 68 65 6e 65 76 65 72 20 61 20 74 61 62 6c 65 20  henever a table 
2ca4e 69 73 20 64 72 6f 70 70 65 64 2c 20 61 6c 6c 20  is dropped, all 
2ca4f 61 73 73 6f 63 69 61 74 65 64 20 74 72 69 67 67  associated trigg
2ca50 65 72 73 20 61 72 65 0a 20 20 20 20 20 20 2a 2a  ers are.      **
2ca51 20 64 72 6f 70 70 65 64 20 74 6f 6f 2e 20 20 42   dropped too.  B
2ca52 75 74 20 69 66 20 61 20 54 45 4d 50 20 74 72 69  ut if a TEMP tri
2ca53 67 67 65 72 20 69 73 20 63 72 65 61 74 65 64 20  gger is created 
2ca54 6f 6e 20 61 20 6e 6f 6e 2d 54 45 4d 50 20 74 61  on a non-TEMP ta
2ca55 62 6c 65 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64  ble.      ** and
2ca56 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 64 72   the table is dr
2ca57 6f 70 70 65 64 20 62 79 20 61 20 64 69 66 66 65  opped by a diffe
2ca58 72 65 6e 74 20 64 61 74 61 62 61 73 65 20 63 6f  rent database co
2ca59 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65 0a 20 20  nnection, the.  
2ca5a 20 20 20 20 2a 2a 20 74 72 69 67 67 65 72 20 69      ** trigger i
2ca5b 73 20 6e 6f 74 20 76 69 73 69 62 6c 65 20 74 6f  s not visible to
2ca5c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   the database co
2ca5d 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74 20 64 6f  nnection that do
2ca5e 65 73 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  es the.      ** 
2ca5f 64 72 6f 70 20 73 6f 20 74 68 65 20 74 72 69 67  drop so the trig
2ca60 67 65 72 20 63 61 6e 6e 6f 74 20 62 65 20 64 72  ger cannot be dr
2ca61 6f 70 70 65 64 2e 20 20 54 68 69 73 20 72 65 73  opped.  This res
2ca62 75 6c 74 73 20 69 6e 20 61 6e 0a 20 20 20 20 20  ults in an.     
2ca63 20 2a 2a 20 22 6f 72 70 68 61 6e 65 64 20 74 72   ** "orphaned tr
2ca64 69 67 67 65 72 22 20 2d 20 61 20 74 72 69 67 67  igger" - a trigg
2ca65 65 72 20 77 68 6f 73 65 20 61 73 73 6f 63 69 61  er whose associa
2ca66 74 65 64 20 74 61 62 6c 65 20 69 73 20 6d 69 73  ted table is mis
2ca67 73 69 6e 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  sing..      */. 
2ca68 20 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 6f 72       db->init.or
2ca69 70 68 61 6e 54 72 69 67 67 65 72 20 3d 20 31 3b  phanTrigger = 1;
2ca6a 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20  .    }.    goto 
2ca6b 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
2ca6c 0a 20 20 7d 0a 20 20 69 66 28 20 49 73 56 69 72  .  }.  if( IsVir
2ca6d 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
2ca6e 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2ca6f 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f  g(pParse, "canno
2ca70 74 20 63 72 65 61 74 65 20 74 72 69 67 67 65 72  t create trigger
2ca71 73 20 6f 6e 20 76 69 72 74 75 61 6c 20 74 61 62  s on virtual tab
2ca72 6c 65 73 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20  les");.    goto 
2ca73 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
2ca74 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  .  }..  /* Check
2ca75 20 74 68 61 74 20 74 68 65 20 74 72 69 67 67 65   that the trigge
2ca76 72 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 72 65  r name is not re
2ca77 73 65 72 76 65 64 20 61 6e 64 20 74 68 61 74 20  served and that 
2ca78 6e 6f 20 74 72 69 67 67 65 72 20 6f 66 20 74 68  no trigger of th
2ca79 65 0a 20 20 2a 2a 20 73 70 65 63 69 66 69 65 64  e.  ** specified
2ca7a 20 6e 61 6d 65 20 65 78 69 73 74 73 20 2a 2f 0a   name exists */.
2ca7b 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
2ca7c 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
2ca7d 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28  b, pName);.  if(
2ca7e 20 21 7a 4e 61 6d 65 20 7c 7c 20 53 51 4c 49 54   !zName || SQLIT
2ca7f 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68 65  E_OK!=sqlite3Che
2ca80 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50 61  ckObjectName(pPa
2ca81 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20  rse, zName) ){. 
2ca82 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f     goto trigger_
2ca83 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69  cleanup;.  }.  i
2ca84 66 28 20 73 71 6c 69 74 65 33 48 61 73 68 46 69  f( sqlite3HashFi
2ca85 6e 64 28 26 28 64 62 2d 3e 61 44 62 5b 69 44 62  nd(&(db->aDb[iDb
2ca86 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48  ].pSchema->trigH
2ca87 61 73 68 29 2c 0a 20 20 20 20 20 20 20 20 20 20  ash),.          
2ca88 20 20 20 20 20 20 20 20 20 20 20 20 7a 4e 61 6d              zNam
2ca89 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  e, sqlite3Strlen
2ca8a 33 30 28 7a 4e 61 6d 65 29 29 20 29 7b 0a 20 20  30(zName)) ){.  
2ca8b 20 20 69 66 28 20 21 6e 6f 45 72 72 20 29 7b 0a    if( !noErr ){.
2ca8c 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
2ca8d 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74  orMsg(pParse, "t
2ca8e 72 69 67 67 65 72 20 25 54 20 61 6c 72 65 61 64  rigger %T alread
2ca8f 79 20 65 78 69 73 74 73 22 2c 20 70 4e 61 6d 65  y exists", pName
2ca90 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74  );.    }.    got
2ca91 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
2ca92 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 6f 20  p;.  }..  /* Do 
2ca93 6e 6f 74 20 63 72 65 61 74 65 20 61 20 74 72 69  not create a tri
2ca94 67 67 65 72 20 6f 6e 20 61 20 73 79 73 74 65 6d  gger on a system
2ca95 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20   table */.  if( 
2ca96 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
2ca97 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71  pTab->zName, "sq
2ca98 6c 69 74 65 5f 22 2c 20 37 29 3d 3d 30 20 29 7b  lite_", 7)==0 ){
2ca99 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
2ca9a 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 61  rMsg(pParse, "ca
2ca9b 6e 6e 6f 74 20 63 72 65 61 74 65 20 74 72 69 67  nnot create trig
2ca9c 67 65 72 20 6f 6e 20 73 79 73 74 65 6d 20 74 61  ger on system ta
2ca9d 62 6c 65 22 29 3b 0a 20 20 20 20 70 50 61 72 73  ble");.    pPars
2ca9e 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 67  e->nErr++;.    g
2ca9f 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61  oto trigger_clea
2caa0 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  nup;.  }..  /* I
2caa1 4e 53 54 45 41 44 20 6f 66 20 74 72 69 67 67 65  NSTEAD of trigge
2caa2 72 73 20 61 72 65 20 6f 6e 6c 79 20 66 6f 72 20  rs are only for 
2caa3 76 69 65 77 73 20 61 6e 64 20 76 69 65 77 73 20  views and views 
2caa4 6f 6e 6c 79 20 73 75 70 70 6f 72 74 20 49 4e 53  only support INS
2caa5 54 45 41 44 0a 20 20 2a 2a 20 6f 66 20 74 72 69  TEAD.  ** of tri
2caa6 67 67 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  ggers..  */.  if
2caa7 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  ( pTab->pSelect 
2caa8 26 26 20 74 72 5f 74 6d 21 3d 54 4b 5f 49 4e 53  && tr_tm!=TK_INS
2caa9 54 45 41 44 20 29 7b 0a 20 20 20 20 73 71 6c 69  TEAD ){.    sqli
2caaa 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2caab 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65 61  se, "cannot crea
2caac 74 65 20 25 73 20 74 72 69 67 67 65 72 20 6f 6e  te %s trigger on
2caad 20 76 69 65 77 3a 20 25 53 22 2c 20 0a 20 20 20   view: %S", .   
2caae 20 20 20 20 20 28 74 72 5f 74 6d 20 3d 3d 20 54       (tr_tm == T
2caaf 4b 5f 42 45 46 4f 52 45 29 3f 22 42 45 46 4f 52  K_BEFORE)?"BEFOR
2cab0 45 22 3a 22 41 46 54 45 52 22 2c 20 70 54 61 62  E":"AFTER", pTab
2cab1 6c 65 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  leName, 0);.    
2cab2 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65  goto trigger_cle
2cab3 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20  anup;.  }.  if( 
2cab4 21 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 26  !pTab->pSelect &
2cab5 26 20 74 72 5f 74 6d 3d 3d 54 4b 5f 49 4e 53 54  & tr_tm==TK_INST
2cab6 45 41 44 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  EAD ){.    sqlit
2cab7 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2cab8 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65 61 74  e, "cannot creat
2cab9 65 20 49 4e 53 54 45 41 44 20 4f 46 22 0a 20 20  e INSTEAD OF".  
2caba 20 20 20 20 20 20 22 20 74 72 69 67 67 65 72 20        " trigger 
2cabb 6f 6e 20 74 61 62 6c 65 3a 20 25 53 22 2c 20 70  on table: %S", p
2cabc 54 61 62 6c 65 4e 61 6d 65 2c 20 30 29 3b 0a 20  TableName, 0);. 
2cabd 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f     goto trigger_
2cabe 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69  cleanup;.  }.  i
2cabf 54 61 62 44 62 20 3d 20 73 71 6c 69 74 65 33 53  TabDb = sqlite3S
2cac0 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
2cac1 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
2cac2 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2cac3 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
2cac4 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20  ION.  {.    int 
2cac5 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52  code = SQLITE_CR
2cac6 45 41 54 45 5f 54 52 49 47 47 45 52 3b 0a 20 20  EATE_TRIGGER;.  
2cac7 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
2cac8 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 54 61 62  b = db->aDb[iTab
2cac9 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63  Db].zName;.    c
2caca 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 54 72  onst char *zDbTr
2cacb 69 67 20 3d 20 69 73 54 65 6d 70 20 3f 20 64 62  ig = isTemp ? db
2cacc 2d 3e 61 44 62 5b 31 5d 2e 7a 4e 61 6d 65 20 3a  ->aDb[1].zName :
2cacd 20 7a 44 62 3b 0a 20 20 20 20 69 66 28 20 69 54   zDb;.    if( iT
2cace 61 62 44 62 3d 3d 31 20 7c 7c 20 69 73 54 65 6d  abDb==1 || isTem
2cacf 70 20 29 20 63 6f 64 65 20 3d 20 53 51 4c 49 54  p ) code = SQLIT
2cad0 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52  E_CREATE_TEMP_TR
2cad1 49 47 47 45 52 3b 0a 20 20 20 20 69 66 28 20 73  IGGER;.    if( s
2cad2 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
2cad3 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 7a 4e  pParse, code, zN
2cad4 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ame, pTab->zName
2cad5 2c 20 7a 44 62 54 72 69 67 29 20 29 7b 0a 20 20  , zDbTrig) ){.  
2cad6 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72      goto trigger
2cad7 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a  _cleanup;.    }.
2cad8 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41      if( sqlite3A
2cad9 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
2cada 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20   SQLITE_INSERT, 
2cadb 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 54 61  SCHEMA_TABLE(iTa
2cadc 62 44 62 29 2c 30 2c 7a 44 62 29 29 7b 0a 20 20  bDb),0,zDb)){.  
2cadd 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72      goto trigger
2cade 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a  _cleanup;.    }.
2cadf 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
2cae0 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67   INSTEAD OF trig
2cae1 67 65 72 73 20 63 61 6e 20 6f 6e 6c 79 20 61 70  gers can only ap
2cae2 70 65 61 72 20 6f 6e 20 76 69 65 77 73 20 61 6e  pear on views an
2cae3 64 20 42 45 46 4f 52 45 20 74 72 69 67 67 65 72  d BEFORE trigger
2cae4 73 0a 20 20 2a 2a 20 63 61 6e 6e 6f 74 20 61 70  s.  ** cannot ap
2cae5 70 65 61 72 20 6f 6e 20 76 69 65 77 73 2e 20 20  pear on views.  
2cae6 53 6f 20 77 65 20 6d 69 67 68 74 20 61 73 20 77  So we might as w
2cae7 65 6c 6c 20 74 72 61 6e 73 6c 61 74 65 20 65 76  ell translate ev
2cae8 65 72 79 0a 20 20 2a 2a 20 49 4e 53 54 45 41 44  ery.  ** INSTEAD
2cae9 20 4f 46 20 74 72 69 67 67 65 72 20 69 6e 74 6f   OF trigger into
2caea 20 61 20 42 45 46 4f 52 45 20 74 72 69 67 67 65   a BEFORE trigge
2caeb 72 2e 20 20 49 74 20 73 69 6d 70 6c 69 66 69 65  r.  It simplifie
2caec 73 20 63 6f 64 65 0a 20 20 2a 2a 20 65 6c 73 65  s code.  ** else
2caed 77 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  where..  */.  if
2caee 20 28 74 72 5f 74 6d 20 3d 3d 20 54 4b 5f 49 4e   (tr_tm == TK_IN
2caef 53 54 45 41 44 29 7b 0a 20 20 20 20 74 72 5f 74  STEAD){.    tr_t
2caf0 6d 20 3d 20 54 4b 5f 42 45 46 4f 52 45 3b 0a 20  m = TK_BEFORE;. 
2caf1 20 7d 0a 0a 20 20 2f 2a 20 42 75 69 6c 64 20 74   }..  /* Build t
2caf2 68 65 20 54 72 69 67 67 65 72 20 6f 62 6a 65 63  he Trigger objec
2caf3 74 20 2a 2f 0a 20 20 70 54 72 69 67 67 65 72 20  t */.  pTrigger 
2caf4 3d 20 28 54 72 69 67 67 65 72 2a 29 73 71 6c 69  = (Trigger*)sqli
2caf5 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
2caf6 64 62 2c 20 73 69 7a 65 6f 66 28 54 72 69 67 67  db, sizeof(Trigg
2caf7 65 72 29 29 3b 0a 20 20 69 66 28 20 70 54 72 69  er));.  if( pTri
2caf8 67 67 65 72 3d 3d 30 20 29 20 67 6f 74 6f 20 74  gger==0 ) goto t
2caf9 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a  rigger_cleanup;.
2cafa 20 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d    pTrigger->zNam
2cafb 65 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 7a 4e 61  e = zName;.  zNa
2cafc 6d 65 20 3d 20 30 3b 0a 20 20 70 54 72 69 67 67  me = 0;.  pTrigg
2cafd 65 72 2d 3e 74 61 62 6c 65 20 3d 20 73 71 6c 69  er->table = sqli
2cafe 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20  te3DbStrDup(db, 
2caff 70 54 61 62 6c 65 4e 61 6d 65 2d 3e 61 5b 30 5d  pTableName->a[0]
2cb00 2e 7a 4e 61 6d 65 29 3b 0a 20 20 70 54 72 69 67  .zName);.  pTrig
2cb01 67 65 72 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64  ger->pSchema = d
2cb02 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68  b->aDb[iDb].pSch
2cb03 65 6d 61 3b 0a 20 20 70 54 72 69 67 67 65 72 2d  ema;.  pTrigger-
2cb04 3e 70 54 61 62 53 63 68 65 6d 61 20 3d 20 70 54  >pTabSchema = pT
2cb05 61 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 70  ab->pSchema;.  p
2cb06 54 72 69 67 67 65 72 2d 3e 6f 70 20 3d 20 28 75  Trigger->op = (u
2cb07 38 29 6f 70 3b 0a 20 20 70 54 72 69 67 67 65 72  8)op;.  pTrigger
2cb08 2d 3e 74 72 5f 74 6d 20 3d 20 74 72 5f 74 6d 3d  ->tr_tm = tr_tm=
2cb09 3d 54 4b 5f 42 45 46 4f 52 45 20 3f 20 54 52 49  =TK_BEFORE ? TRI
2cb0a 47 47 45 52 5f 42 45 46 4f 52 45 20 3a 20 54 52  GGER_BEFORE : TR
2cb0b 49 47 47 45 52 5f 41 46 54 45 52 3b 0a 20 20 70  IGGER_AFTER;.  p
2cb0c 54 72 69 67 67 65 72 2d 3e 70 57 68 65 6e 20 3d  Trigger->pWhen =
2cb0d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
2cb0e 64 62 2c 20 70 57 68 65 6e 2c 20 45 58 50 52 44  db, pWhen, EXPRD
2cb0f 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 70 54  UP_REDUCE);.  pT
2cb10 72 69 67 67 65 72 2d 3e 70 43 6f 6c 75 6d 6e 73  rigger->pColumns
2cb11 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74   = sqlite3IdList
2cb12 44 75 70 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 73  Dup(db, pColumns
2cb13 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  );.  assert( pPa
2cb14 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72  rse->pNewTrigger
2cb15 3d 3d 30 20 29 3b 0a 20 20 70 50 61 72 73 65 2d  ==0 );.  pParse-
2cb16 3e 70 4e 65 77 54 72 69 67 67 65 72 20 3d 20 70  >pNewTrigger = p
2cb17 54 72 69 67 67 65 72 3b 0a 0a 74 72 69 67 67 65  Trigger;..trigge
2cb18 72 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c  r_cleanup:.  sql
2cb19 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2cb1a 4e 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Name);.  sqlite3
2cb1b 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62  SrcListDelete(db
2cb1c 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20  , pTableName);. 
2cb1d 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65   sqlite3IdListDe
2cb1e 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c 75 6d 6e  lete(db, pColumn
2cb1f 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  s);.  sqlite3Exp
2cb20 72 44 65 6c 65 74 65 28 64 62 2c 20 70 57 68 65  rDelete(db, pWhe
2cb21 6e 29 3b 0a 20 20 69 66 28 20 21 70 50 61 72 73  n);.  if( !pPars
2cb22 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 20 29  e->pNewTrigger )
2cb23 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c  {.    sqlite3Del
2cb24 65 74 65 54 72 69 67 67 65 72 28 64 62 2c 20 70  eteTrigger(db, p
2cb25 54 72 69 67 67 65 72 29 3b 0a 20 20 7d 65 6c 73  Trigger);.  }els
2cb26 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
2cb27 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67  Parse->pNewTrigg
2cb28 65 72 3d 3d 70 54 72 69 67 67 65 72 20 29 3b 0a  er==pTrigger );.
2cb29 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
2cb2a 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
2cb2b 6c 65 64 20 61 66 74 65 72 20 61 6c 6c 20 6f 66  led after all of
2cb2c 20 74 68 65 20 74 72 69 67 67 65 72 20 61 63 74   the trigger act
2cb2d 69 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20 70  ions have been p
2cb2e 61 72 73 65 64 0a 2a 2a 20 69 6e 20 6f 72 64 65  arsed.** in orde
2cb2f 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68  r to complete th
2cb30 65 20 70 72 6f 63 65 73 73 20 6f 66 20 62 75 69  e process of bui
2cb31 6c 64 69 6e 67 20 74 68 65 20 74 72 69 67 67 65  lding the trigge
2cb32 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
2cb33 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2cb34 33 46 69 6e 69 73 68 54 72 69 67 67 65 72 28 0a  3FinishTrigger(.
2cb35 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2cb36 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
2cb37 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
2cb38 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53   TriggerStep *pS
2cb39 74 65 70 4c 69 73 74 2c 20 2f 2a 20 54 68 65 20  tepList, /* The 
2cb3a 74 72 69 67 67 65 72 65 64 20 70 72 6f 67 72 61  triggered progra
2cb3b 6d 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 41  m */.  Token *pA
2cb3c 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ll             /
2cb3d 2a 20 54 6f 6b 65 6e 20 74 68 61 74 20 64 65 73  * Token that des
2cb3e 63 72 69 62 65 73 20 74 68 65 20 63 6f 6d 70 6c  cribes the compl
2cb3f 65 74 65 20 43 52 45 41 54 45 20 54 52 49 47 47  ete CREATE TRIGG
2cb40 45 52 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67  ER */.){.  Trigg
2cb41 65 72 20 2a 70 54 72 69 67 20 3d 20 70 50 61 72  er *pTrig = pPar
2cb42 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 3b  se->pNewTrigger;
2cb43 20 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 62      /* Trigger b
2cb44 65 69 6e 67 20 66 69 6e 69 73 68 65 64 20 2a 2f  eing finished */
2cb45 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  .  char *zName; 
2cb46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cb47 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2cb48 61 6d 65 20 6f 66 20 74 72 69 67 67 65 72 20 2a  ame of trigger *
2cb49 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
2cb4a 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20  = pParse->db;   
2cb4b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2cb4c 54 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  The database */.
2cb4d 20 20 44 62 46 69 78 65 72 20 73 46 69 78 3b 0a    DbFixer sFix;.
2cb4e 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
2cb4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cb50 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
2cb51 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
2cb52 67 20 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f  g the trigger */
2cb53 0a 20 20 54 6f 6b 65 6e 20 6e 61 6d 65 54 6f 6b  .  Token nameTok
2cb54 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  en;           /*
2cb55 20 54 72 69 67 67 65 72 20 6e 61 6d 65 20 66 6f   Trigger name fo
2cb56 72 20 65 72 72 6f 72 20 72 65 70 6f 72 74 69 6e  r error reportin
2cb57 67 20 2a 2f 0a 0a 20 20 70 54 72 69 67 20 3d 20  g */..  pTrig = 
2cb58 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67  pParse->pNewTrig
2cb59 67 65 72 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70  ger;.  pParse->p
2cb5a 4e 65 77 54 72 69 67 67 65 72 20 3d 20 30 3b 0a  NewTrigger = 0;.
2cb5b 20 20 69 66 28 20 4e 45 56 45 52 28 70 50 61 72    if( NEVER(pPar
2cb5c 73 65 2d 3e 6e 45 72 72 29 20 7c 7c 20 21 70 54  se->nErr) || !pT
2cb5d 72 69 67 20 29 20 67 6f 74 6f 20 74 72 69 67 67  rig ) goto trigg
2cb5e 65 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70  erfinish_cleanup
2cb5f 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 70 54 72 69  ;.  zName = pTri
2cb60 67 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 69 44 62 20  g->zName;.  iDb 
2cb61 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
2cb62 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
2cb63 62 2c 20 70 54 72 69 67 2d 3e 70 53 63 68 65 6d  b, pTrig->pSchem
2cb64 61 29 3b 0a 20 20 70 54 72 69 67 2d 3e 73 74 65  a);.  pTrig->ste
2cb65 70 5f 6c 69 73 74 20 3d 20 70 53 74 65 70 4c 69  p_list = pStepLi
2cb66 73 74 3b 0a 20 20 77 68 69 6c 65 28 20 70 53 74  st;.  while( pSt
2cb67 65 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 70 53  epList ){.    pS
2cb68 74 65 70 4c 69 73 74 2d 3e 70 54 72 69 67 20 3d  tepList->pTrig =
2cb69 20 70 54 72 69 67 3b 0a 20 20 20 20 70 53 74 65   pTrig;.    pSte
2cb6a 70 4c 69 73 74 20 3d 20 70 53 74 65 70 4c 69 73  pList = pStepLis
2cb6b 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20  t->pNext;.  }.  
2cb6c 6e 61 6d 65 54 6f 6b 65 6e 2e 7a 20 3d 20 70 54  nameToken.z = pT
2cb6d 72 69 67 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 6e 61  rig->zName;.  na
2cb6e 6d 65 54 6f 6b 65 6e 2e 6e 20 3d 20 73 71 6c 69  meToken.n = sqli
2cb6f 74 65 33 53 74 72 6c 65 6e 33 30 28 6e 61 6d 65  te3Strlen30(name
2cb70 54 6f 6b 65 6e 2e 7a 29 3b 0a 20 20 69 66 28 20  Token.z);.  if( 
2cb71 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28 26  sqlite3FixInit(&
2cb72 73 46 69 78 2c 20 70 50 61 72 73 65 2c 20 69 44  sFix, pParse, iD
2cb73 62 2c 20 22 74 72 69 67 67 65 72 22 2c 20 26 6e  b, "trigger", &n
2cb74 61 6d 65 54 6f 6b 65 6e 29 20 0a 20 20 20 20 20  ameToken) .     
2cb75 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 46       && sqlite3F
2cb76 69 78 54 72 69 67 67 65 72 53 74 65 70 28 26 73  ixTriggerStep(&s
2cb77 46 69 78 2c 20 70 54 72 69 67 2d 3e 73 74 65 70  Fix, pTrig->step
2cb78 5f 6c 69 73 74 29 20 29 7b 0a 20 20 20 20 67 6f  _list) ){.    go
2cb79 74 6f 20 74 72 69 67 67 65 72 66 69 6e 69 73 68  to triggerfinish
2cb7a 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
2cb7b 20 2f 2a 20 69 66 20 77 65 20 61 72 65 20 6e 6f   /* if we are no
2cb7c 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 2c 20  t initializing, 
2cb7d 61 6e 64 20 74 68 69 73 20 74 72 69 67 67 65 72  and this trigger
2cb7e 20 69 73 20 6e 6f 74 20 6f 6e 20 61 20 54 45 4d   is not on a TEM
2cb7f 50 20 74 61 62 6c 65 2c 20 0a 20 20 2a 2a 20 62  P table, .  ** b
2cb80 75 69 6c 64 20 74 68 65 20 73 71 6c 69 74 65 5f  uild the sqlite_
2cb81 6d 61 73 74 65 72 20 65 6e 74 72 79 0a 20 20 2a  master entry.  *
2cb82 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69  /.  if( !db->ini
2cb83 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 56 64  t.busy ){.    Vd
2cb84 62 65 20 2a 76 3b 0a 20 20 20 20 63 68 61 72 20  be *v;.    char 
2cb85 2a 7a 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65  *z;..    /* Make
2cb86 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74 68 65   an entry in the
2cb87 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74   sqlite_master t
2cb88 61 62 6c 65 20 2a 2f 0a 20 20 20 20 76 20 3d 20  able */.    v = 
2cb89 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2cb8a 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66 28 20  Parse);.    if( 
2cb8b 76 3d 3d 30 20 29 20 67 6f 74 6f 20 74 72 69 67  v==0 ) goto trig
2cb8c 67 65 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75  gerfinish_cleanu
2cb8d 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  p;.    sqlite3Be
2cb8e 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f  ginWriteOperatio
2cb8f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44 62  n(pParse, 0, iDb
2cb90 29 3b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  );.    z = sqlit
2cb91 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20  e3DbStrNDup(db, 
2cb92 28 63 68 61 72 2a 29 70 41 6c 6c 2d 3e 7a 2c 20  (char*)pAll->z, 
2cb93 70 41 6c 6c 2d 3e 6e 29 3b 0a 20 20 20 20 73 71  pAll->n);.    sq
2cb94 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
2cb95 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20  (pParse,.       
2cb96 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 25 51 2e  "INSERT INTO %Q.
2cb97 25 73 20 56 41 4c 55 45 53 28 27 74 72 69 67 67  %s VALUES('trigg
2cb98 65 72 27 2c 25 51 2c 25 51 2c 30 2c 27 43 52 45  er',%Q,%Q,0,'CRE
2cb99 41 54 45 20 54 52 49 47 47 45 52 20 25 71 27 29  ATE TRIGGER %q')
2cb9a 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e 61 44  ",.       db->aD
2cb9b 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53 43  b[iDb].zName, SC
2cb9c 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c  HEMA_TABLE(iDb),
2cb9d 20 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 70   zName,.       p
2cb9e 54 72 69 67 2d 3e 74 61 62 6c 65 2c 20 7a 29 3b  Trig->table, z);
2cb9f 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2cba0 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 73  ee(db, z);.    s
2cba1 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b  qlite3ChangeCook
2cba2 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b  ie(pParse, iDb);
2cba3 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2cba4 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72  AddOp4(v, OP_Par
2cba5 73 65 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 30  seSchema, iDb, 0
2cba6 2c 20 30 2c 20 73 71 6c 69 74 65 33 4d 50 72 69  , 0, sqlite3MPri
2cba7 6e 74 66 28 0a 20 20 20 20 20 20 20 20 64 62 2c  ntf(.        db,
2cba8 20 22 74 79 70 65 3d 27 74 72 69 67 67 65 72 27   "type='trigger'
2cba9 20 41 4e 44 20 6e 61 6d 65 3d 27 25 71 27 22 2c   AND name='%q'",
2cbaa 20 7a 4e 61 6d 65 29 2c 20 50 34 5f 44 59 4e 41   zName), P4_DYNA
2cbab 4d 49 43 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a  MIC.    );.  }..
2cbac 20 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62    if( db->init.b
2cbad 75 73 79 20 29 7b 0a 20 20 20 20 54 72 69 67 67  usy ){.    Trigg
2cbae 65 72 20 2a 70 4c 69 6e 6b 20 3d 20 70 54 72 69  er *pLink = pTri
2cbaf 67 3b 0a 20 20 20 20 48 61 73 68 20 2a 70 48 61  g;.    Hash *pHa
2cbb0 73 68 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44  sh = &db->aDb[iD
2cbb1 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69 67  b].pSchema->trig
2cbb2 48 61 73 68 3b 0a 20 20 20 20 70 54 72 69 67 20  Hash;.    pTrig 
2cbb3 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73  = sqlite3HashIns
2cbb4 65 72 74 28 70 48 61 73 68 2c 20 7a 4e 61 6d 65  ert(pHash, zName
2cbb5 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  , sqlite3Strlen3
2cbb6 30 28 7a 4e 61 6d 65 29 2c 20 70 54 72 69 67 29  0(zName), pTrig)
2cbb7 3b 0a 20 20 20 20 69 66 28 20 70 54 72 69 67 20  ;.    if( pTrig 
2cbb8 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c  ){.      db->mal
2cbb9 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
2cbba 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 69     }else if( pLi
2cbbb 6e 6b 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 4c 69  nk->pSchema==pLi
2cbbc 6e 6b 2d 3e 70 54 61 62 53 63 68 65 6d 61 20 29  nk->pTabSchema )
2cbbd 7b 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70  {.      Table *p
2cbbe 54 61 62 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  Tab;.      int n
2cbbf 20 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e   = sqlite3Strlen
2cbc0 33 30 28 70 4c 69 6e 6b 2d 3e 74 61 62 6c 65 29  30(pLink->table)
2cbc1 3b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 73  ;.      pTab = s
2cbc2 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26  qlite3HashFind(&
2cbc3 70 4c 69 6e 6b 2d 3e 70 54 61 62 53 63 68 65 6d  pLink->pTabSchem
2cbc4 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 4c 69 6e  a->tblHash, pLin
2cbc5 6b 2d 3e 74 61 62 6c 65 2c 20 6e 29 3b 0a 20 20  k->table, n);.  
2cbc6 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62      assert( pTab
2cbc7 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4c 69  !=0 );.      pLi
2cbc8 6e 6b 2d 3e 70 4e 65 78 74 20 3d 20 70 54 61 62  nk->pNext = pTab
2cbc9 2d 3e 70 54 72 69 67 67 65 72 3b 0a 20 20 20 20  ->pTrigger;.    
2cbca 20 20 70 54 61 62 2d 3e 70 54 72 69 67 67 65 72    pTab->pTrigger
2cbcb 20 3d 20 70 4c 69 6e 6b 3b 0a 20 20 20 20 7d 0a   = pLink;.    }.
2cbcc 20 20 7d 0a 0a 74 72 69 67 67 65 72 66 69 6e 69    }..triggerfini
2cbcd 73 68 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71  sh_cleanup:.  sq
2cbce 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67  lite3DeleteTrigg
2cbcf 65 72 28 64 62 2c 20 70 54 72 69 67 29 3b 0a 20  er(db, pTrig);. 
2cbd0 20 61 73 73 65 72 74 28 20 21 70 50 61 72 73 65   assert( !pParse
2cbd1 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 20 29 3b  ->pNewTrigger );
2cbd2 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  .  sqlite3Delete
2cbd3 54 72 69 67 67 65 72 53 74 65 70 28 64 62 2c 20  TriggerStep(db, 
2cbd4 70 53 74 65 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f  pStepList);.}../
2cbd5 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 53 45 4c 45  *.** Turn a SELE
2cbd6 43 54 20 73 74 61 74 65 6d 65 6e 74 20 28 74 68  CT statement (th
2cbd7 61 74 20 74 68 65 20 70 53 65 6c 65 63 74 20 70  at the pSelect p
2cbd8 61 72 61 6d 65 74 65 72 20 70 6f 69 6e 74 73 20  arameter points 
2cbd9 74 6f 29 20 69 6e 74 6f 0a 2a 2a 20 61 20 74 72  to) into.** a tr
2cbda 69 67 67 65 72 20 73 74 65 70 2e 20 20 52 65 74  igger step.  Ret
2cbdb 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
2cbdc 20 61 20 54 72 69 67 67 65 72 53 74 65 70 20 73   a TriggerStep s
2cbdd 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
2cbde 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73  The parser calls
2cbdf 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68   this routine wh
2cbe0 65 6e 20 69 74 20 66 69 6e 64 73 20 61 20 53 45  en it finds a SE
2cbe1 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 69  LECT statement i
2cbe2 6e 0a 2a 2a 20 62 6f 64 79 20 6f 66 20 61 20 54  n.** body of a T
2cbe3 52 49 47 47 45 52 2e 20 20 0a 2a 2f 0a 53 51 4c  RIGGER.  .*/.SQL
2cbe4 49 54 45 5f 50 52 49 56 41 54 45 20 54 72 69 67  ITE_PRIVATE Trig
2cbe5 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33  gerStep *sqlite3
2cbe6 54 72 69 67 67 65 72 53 65 6c 65 63 74 53 74 65  TriggerSelectSte
2cbe7 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 53  p(sqlite3 *db, S
2cbe8 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 29 7b  elect *pSelect){
2cbe9 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  .  TriggerStep *
2cbea 70 54 72 69 67 67 65 72 53 74 65 70 20 3d 20 73  pTriggerStep = s
2cbeb 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65  qlite3DbMallocZe
2cbec 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 72  ro(db, sizeof(Tr
2cbed 69 67 67 65 72 53 74 65 70 29 29 3b 0a 20 20 69  iggerStep));.  i
2cbee 66 28 20 70 54 72 69 67 67 65 72 53 74 65 70 3d  f( pTriggerStep=
2cbef 3d 30 20 29 20 7b 0a 20 20 20 20 73 71 6c 69 74  =0 ) {.    sqlit
2cbf0 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
2cbf1 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20  b, pSelect);.   
2cbf2 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
2cbf3 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f   pTriggerStep->o
2cbf4 70 20 3d 20 54 4b 5f 53 45 4c 45 43 54 3b 0a 20  p = TK_SELECT;. 
2cbf5 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70   pTriggerStep->p
2cbf6 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74  Select = pSelect
2cbf7 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70  ;.  pTriggerStep
2cbf8 2d 3e 6f 72 63 6f 6e 66 20 3d 20 4f 45 5f 44 65  ->orconf = OE_De
2cbf9 66 61 75 6c 74 3b 0a 20 20 72 65 74 75 72 6e 20  fault;.  return 
2cbfa 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a 7d 0a  pTriggerStep;.}.
2cbfb 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
2cbfc 73 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 61 20  space to hold a 
2cbfd 6e 65 77 20 74 72 69 67 67 65 72 20 73 74 65 70  new trigger step
2cbfe 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61 74 65 64  .  The allocated
2cbff 20 73 70 61 63 65 0a 2a 2a 20 68 6f 6c 64 73 20   space.** holds 
2cc00 62 6f 74 68 20 74 68 65 20 54 72 69 67 67 65 72  both the Trigger
2cc01 53 74 65 70 20 6f 62 6a 65 63 74 20 61 6e 64 20  Step object and 
2cc02 74 68 65 20 54 72 69 67 67 65 72 53 74 65 70 2e  the TriggerStep.
2cc03 74 61 72 67 65 74 2e 7a 20 73 74 72 69 6e 67 2e  target.z string.
2cc04 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 4f 4f 4d  .**.** If an OOM
2cc05 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 4e   error occurs, N
2cc06 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20  ULL is returned 
2cc07 61 6e 64 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  and db->mallocFa
2cc08 69 6c 65 64 20 69 73 20 73 65 74 2e 0a 2a 2f 0a  iled is set..*/.
2cc09 73 74 61 74 69 63 20 54 72 69 67 67 65 72 53 74  static TriggerSt
2cc0a 65 70 20 2a 74 72 69 67 67 65 72 53 74 65 70 41  ep *triggerStepA
2cc0b 6c 6c 6f 63 61 74 65 28 0a 20 20 73 71 6c 69 74  llocate(.  sqlit
2cc0c 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
2cc0d 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
2cc0e 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
2cc0f 0a 20 20 75 38 20 6f 70 2c 20 20 20 20 20 20 20  .  u8 op,       
2cc10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2cc11 2a 20 54 72 69 67 67 65 72 20 6f 70 63 6f 64 65  * Trigger opcode
2cc12 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61   */.  Token *pNa
2cc13 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  me              
2cc14 20 20 2f 2a 20 54 68 65 20 74 61 72 67 65 74 20    /* The target 
2cc15 6e 61 6d 65 20 2a 2f 0a 29 7b 0a 20 20 54 72 69  name */.){.  Tri
2cc16 67 67 65 72 53 74 65 70 20 2a 70 54 72 69 67 67  ggerStep *pTrigg
2cc17 65 72 53 74 65 70 3b 0a 0a 20 20 70 54 72 69 67  erStep;..  pTrig
2cc18 67 65 72 53 74 65 70 20 3d 20 73 71 6c 69 74 65  gerStep = sqlite
2cc19 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
2cc1a 2c 20 73 69 7a 65 6f 66 28 54 72 69 67 67 65 72  , sizeof(Trigger
2cc1b 53 74 65 70 29 20 2b 20 70 4e 61 6d 65 2d 3e 6e  Step) + pName->n
2cc1c 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65  );.  if( pTrigge
2cc1d 72 53 74 65 70 20 29 7b 0a 20 20 20 20 63 68 61  rStep ){.    cha
2cc1e 72 20 2a 7a 20 3d 20 28 63 68 61 72 2a 29 26 70  r *z = (char*)&p
2cc1f 54 72 69 67 67 65 72 53 74 65 70 5b 31 5d 3b 0a  TriggerStep[1];.
2cc20 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20 70 4e      memcpy(z, pN
2cc21 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e  ame->z, pName->n
2cc22 29 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72 53  );.    pTriggerS
2cc23 74 65 70 2d 3e 74 61 72 67 65 74 2e 7a 20 3d 20  tep->target.z = 
2cc24 7a 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72 53  z;.    pTriggerS
2cc25 74 65 70 2d 3e 74 61 72 67 65 74 2e 6e 20 3d 20  tep->target.n = 
2cc26 70 4e 61 6d 65 2d 3e 6e 3b 0a 20 20 20 20 70 54  pName->n;.    pT
2cc27 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 70 20 3d  riggerStep->op =
2cc28 20 6f 70 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   op;.  }.  retur
2cc29 6e 20 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a  n pTriggerStep;.
2cc2a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 75 69 6c 64 20 61  }../*.** Build a
2cc2b 20 74 72 69 67 67 65 72 20 73 74 65 70 20 6f 75   trigger step ou
2cc2c 74 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 20 73  t of an INSERT s
2cc2d 74 61 74 65 6d 65 6e 74 2e 20 20 52 65 74 75 72  tatement.  Retur
2cc2e 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74  n a pointer.** t
2cc2f 6f 20 74 68 65 20 6e 65 77 20 74 72 69 67 67 65  o the new trigge
2cc30 72 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a 20 54 68  r step..**.** Th
2cc31 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74  e parser calls t
2cc32 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e  his routine when
2cc33 20 69 74 20 73 65 65 73 20 61 6e 20 49 4e 53 45   it sees an INSE
2cc34 52 54 20 69 6e 73 69 64 65 20 74 68 65 0a 2a 2a  RT inside the.**
2cc35 20 62 6f 64 79 20 6f 66 20 61 20 74 72 69 67 67   body of a trigg
2cc36 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
2cc37 49 56 41 54 45 20 54 72 69 67 67 65 72 53 74 65  IVATE TriggerSte
2cc38 70 20 2a 73 71 6c 69 74 65 33 54 72 69 67 67 65  p *sqlite3Trigge
2cc39 72 49 6e 73 65 72 74 53 74 65 70 28 0a 20 20 73  rInsertStep(.  s
2cc3a 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
2cc3b 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61     /* The databa
2cc3c 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
2cc3d 0a 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65  .  Token *pTable
2cc3e 4e 61 6d 65 2c 20 20 2f 2a 20 4e 61 6d 65 20 6f  Name,  /* Name o
2cc3f 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e 74 6f  f the table into
2cc40 20 77 68 69 63 68 20 77 65 20 69 6e 73 65 72 74   which we insert
2cc41 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43   */.  IdList *pC
2cc42 6f 6c 75 6d 6e 2c 20 20 20 20 2f 2a 20 4c 69 73  olumn,    /* Lis
2cc43 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  t of columns in 
2cc44 70 54 61 62 6c 65 4e 61 6d 65 20 74 6f 20 69 6e  pTableName to in
2cc45 73 65 72 74 20 69 6e 74 6f 20 2a 2f 0a 20 20 45  sert into */.  E
2cc46 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c  xprList *pEList,
2cc47 20 20 20 2f 2a 20 54 68 65 20 56 41 4c 55 45 20     /* The VALUE 
2cc48 63 6c 61 75 73 65 3a 20 61 20 6c 69 73 74 20 6f  clause: a list o
2cc49 66 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 69  f values to be i
2cc4a 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 53 65 6c  nserted */.  Sel
2cc4b 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20  ect *pSelect,   
2cc4c 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61   /* A SELECT sta
2cc4d 74 65 6d 65 6e 74 20 74 68 61 74 20 73 75 70 70  tement that supp
2cc4e 6c 69 65 73 20 76 61 6c 75 65 73 20 2a 2f 0a 20  lies values */. 
2cc4f 20 75 38 20 6f 72 63 6f 6e 66 20 20 20 20 20 20   u8 orconf      
2cc50 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e 66       /* The conf
2cc51 6c 69 63 74 20 61 6c 67 6f 72 69 74 68 6d 20 28  lict algorithm (
2cc52 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 52 65 70  OE_Abort, OE_Rep
2cc53 6c 61 63 65 2c 20 65 74 63 2e 29 20 2a 2f 0a 29  lace, etc.) */.)
2cc54 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20  {.  TriggerStep 
2cc55 2a 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a 0a  *pTriggerStep;..
2cc56 20 20 61 73 73 65 72 74 28 70 45 4c 69 73 74 20    assert(pEList 
2cc57 3d 3d 20 30 20 7c 7c 20 70 53 65 6c 65 63 74 20  == 0 || pSelect 
2cc58 3d 3d 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28  == 0);.  assert(
2cc59 70 45 4c 69 73 74 20 21 3d 20 30 20 7c 7c 20 70  pEList != 0 || p
2cc5a 53 65 6c 65 63 74 20 21 3d 20 30 20 7c 7c 20 64  Select != 0 || d
2cc5b 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29  b->mallocFailed)
2cc5c 3b 0a 0a 20 20 70 54 72 69 67 67 65 72 53 74 65  ;..  pTriggerSte
2cc5d 70 20 3d 20 74 72 69 67 67 65 72 53 74 65 70 41  p = triggerStepA
2cc5e 6c 6c 6f 63 61 74 65 28 64 62 2c 20 54 4b 5f 49  llocate(db, TK_I
2cc5f 4e 53 45 52 54 2c 20 70 54 61 62 6c 65 4e 61 6d  NSERT, pTableNam
2cc60 65 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67  e);.  if( pTrigg
2cc61 65 72 53 74 65 70 20 29 7b 0a 20 20 20 20 70 54  erStep ){.    pT
2cc62 72 69 67 67 65 72 53 74 65 70 2d 3e 70 53 65 6c  riggerStep->pSel
2cc63 65 63 74 20 3d 20 73 71 6c 69 74 65 33 53 65 6c  ect = sqlite3Sel
2cc64 65 63 74 44 75 70 28 64 62 2c 20 70 53 65 6c 65  ectDup(db, pSele
2cc65 63 74 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55  ct, EXPRDUP_REDU
2cc66 43 45 29 3b 0a 20 20 20 20 70 54 72 69 67 67 65  CE);.    pTrigge
2cc67 72 53 74 65 70 2d 3e 70 49 64 4c 69 73 74 20 3d  rStep->pIdList =
2cc68 20 70 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 70 54   pColumn;.    pT
2cc69 72 69 67 67 65 72 53 74 65 70 2d 3e 70 45 78 70  riggerStep->pExp
2cc6a 72 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45  rList = sqlite3E
2cc6b 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70  xprListDup(db, p
2cc6c 45 4c 69 73 74 2c 20 45 58 50 52 44 55 50 5f 52  EList, EXPRDUP_R
2cc6d 45 44 55 43 45 29 3b 0a 20 20 20 20 70 54 72 69  EDUCE);.    pTri
2cc6e 67 67 65 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66  ggerStep->orconf
2cc6f 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20 20 7d 65 6c   = orconf;.  }el
2cc70 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 49  se{.    sqlite3I
2cc71 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  dListDelete(db, 
2cc72 70 43 6f 6c 75 6d 6e 29 3b 0a 20 20 7d 0a 20 20  pColumn);.  }.  
2cc73 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2cc74 65 6c 65 74 65 28 64 62 2c 20 70 45 4c 69 73 74  elete(db, pEList
2cc75 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  );.  sqlite3Sele
2cc76 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65  ctDelete(db, pSe
2cc77 6c 65 63 74 29 3b 0a 0a 20 20 72 65 74 75 72 6e  lect);..  return
2cc78 20 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a 7d   pTriggerStep;.}
2cc79 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63  ../*.** Construc
2cc7a 74 20 61 20 74 72 69 67 67 65 72 20 73 74 65 70  t a trigger step
2cc7b 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
2cc7c 20 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65   an UPDATE state
2cc7d 6d 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 0a  ment and return.
2cc7e 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
2cc7f 74 68 61 74 20 74 72 69 67 67 65 72 20 73 74 65  that trigger ste
2cc80 70 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 63  p.  The parser c
2cc81 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e  alls this routin
2cc82 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 73 65 65  e when it.** see
2cc83 73 20 61 6e 20 55 50 44 41 54 45 20 73 74 61 74  s an UPDATE stat
2cc84 65 6d 65 6e 74 20 69 6e 73 69 64 65 20 74 68 65  ement inside the
2cc85 20 62 6f 64 79 20 6f 66 20 61 20 43 52 45 41 54   body of a CREAT
2cc86 45 20 54 52 49 47 47 45 52 2e 0a 2a 2f 0a 53 51  E TRIGGER..*/.SQ
2cc87 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 72 69  LITE_PRIVATE Tri
2cc88 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65  ggerStep *sqlite
2cc89 33 54 72 69 67 67 65 72 55 70 64 61 74 65 53 74  3TriggerUpdateSt
2cc8a 65 70 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ep(.  sqlite3 *d
2cc8b 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  b,         /* Th
2cc8c 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
2cc8d 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ction */.  Token
2cc8e 20 2a 70 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20   *pTableName,   
2cc8f 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74  /* Name of the t
2cc90 61 62 6c 65 20 74 6f 20 62 65 20 75 70 64 61 74  able to be updat
2cc91 65 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ed */.  ExprList
2cc92 20 2a 70 45 4c 69 73 74 2c 20 20 20 20 2f 2a 20   *pEList,    /* 
2cc93 54 68 65 20 53 45 54 20 63 6c 61 75 73 65 3a 20  The SET clause: 
2cc94 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 20 61  list of column a
2cc95 6e 64 20 6e 65 77 20 76 61 6c 75 65 73 20 2a 2f  nd new values */
2cc96 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c  .  Expr *pWhere,
2cc97 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57          /* The W
2cc98 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
2cc99 20 75 38 20 6f 72 63 6f 6e 66 20 20 20 20 20 20   u8 orconf      
2cc9a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6e        /* The con
2cc9b 66 6c 69 63 74 20 61 6c 67 6f 72 69 74 68 6d 2e  flict algorithm.
2cc9c 20 28 4f 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49   (OE_Abort, OE_I
2cc9d 67 6e 6f 72 65 2c 20 65 74 63 29 20 2a 2f 0a 29  gnore, etc) */.)
2cc9e 7b 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20  {.  TriggerStep 
2cc9f 2a 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a 0a  *pTriggerStep;..
2cca0 20 20 70 54 72 69 67 67 65 72 53 74 65 70 20 3d    pTriggerStep =
2cca1 20 74 72 69 67 67 65 72 53 74 65 70 41 6c 6c 6f   triggerStepAllo
2cca2 63 61 74 65 28 64 62 2c 20 54 4b 5f 55 50 44 41  cate(db, TK_UPDA
2cca3 54 45 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b  TE, pTableName);
2cca4 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72 53  .  if( pTriggerS
2cca5 74 65 70 20 29 7b 0a 20 20 20 20 70 54 72 69 67  tep ){.    pTrig
2cca6 67 65 72 53 74 65 70 2d 3e 70 45 78 70 72 4c 69  gerStep->pExprLi
2cca7 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  st = sqlite3Expr
2cca8 4c 69 73 74 44 75 70 28 64 62 2c 20 70 45 4c 69  ListDup(db, pELi
2cca9 73 74 2c 20 45 58 50 52 44 55 50 5f 52 45 44 55  st, EXPRDUP_REDU
2ccaa 43 45 29 3b 0a 20 20 20 20 70 54 72 69 67 67 65  CE);.    pTrigge
2ccab 72 53 74 65 70 2d 3e 70 57 68 65 72 65 20 3d 20  rStep->pWhere = 
2ccac 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
2ccad 62 2c 20 70 57 68 65 72 65 2c 20 45 58 50 52 44  b, pWhere, EXPRD
2ccae 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20 20  UP_REDUCE);.    
2ccaf 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f 72  pTriggerStep->or
2ccb0 63 6f 6e 66 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20  conf = orconf;. 
2ccb1 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72   }.  sqlite3Expr
2ccb2 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
2ccb3 45 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65  EList);.  sqlite
2ccb4 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
2ccb5 70 57 68 65 72 65 29 3b 0a 20 20 72 65 74 75 72  pWhere);.  retur
2ccb6 6e 20 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a  n pTriggerStep;.
2ccb7 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75  }../*.** Constru
2ccb8 63 74 20 61 20 74 72 69 67 67 65 72 20 73 74 65  ct a trigger ste
2ccb9 70 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  p that implement
2ccba 73 20 61 20 44 45 4c 45 54 45 20 73 74 61 74 65  s a DELETE state
2ccbb 6d 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 0a  ment and return.
2ccbc 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  ** a pointer to 
2ccbd 74 68 61 74 20 74 72 69 67 67 65 72 20 73 74 65  that trigger ste
2ccbe 70 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 63  p.  The parser c
2ccbf 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e  alls this routin
2ccc0 65 20 77 68 65 6e 20 69 74 0a 2a 2a 20 73 65 65  e when it.** see
2ccc1 73 20 61 20 44 45 4c 45 54 45 20 73 74 61 74 65  s a DELETE state
2ccc2 6d 65 6e 74 20 69 6e 73 69 64 65 20 74 68 65 20  ment inside the 
2ccc3 62 6f 64 79 20 6f 66 20 61 20 43 52 45 41 54 45  body of a CREATE
2ccc4 20 54 52 49 47 47 45 52 2e 0a 2a 2f 0a 53 51 4c   TRIGGER..*/.SQL
2ccc5 49 54 45 5f 50 52 49 56 41 54 45 20 54 72 69 67  ITE_PRIVATE Trig
2ccc6 67 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33  gerStep *sqlite3
2ccc7 54 72 69 67 67 65 72 44 65 6c 65 74 65 53 74 65  TriggerDeleteSte
2ccc8 70 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  p(.  sqlite3 *db
2ccc9 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
2ccca 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  Database connect
2cccb 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ion */.  Token *
2cccc 70 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20 20  pTableName,     
2cccd 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 66 72   /* The table fr
2ccce 6f 6d 20 77 68 69 63 68 20 72 6f 77 73 20 61 72  om which rows ar
2cccf 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 45  e deleted */.  E
2ccd0 78 70 72 20 2a 70 57 68 65 72 65 20 20 20 20 20  xpr *pWhere     
2ccd1 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48         /* The WH
2ccd2 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 29 7b  ERE clause */.){
2ccd3 0a 20 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  .  TriggerStep *
2ccd4 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a 0a 20  pTriggerStep;.. 
2ccd5 20 70 54 72 69 67 67 65 72 53 74 65 70 20 3d 20   pTriggerStep = 
2ccd6 74 72 69 67 67 65 72 53 74 65 70 41 6c 6c 6f 63  triggerStepAlloc
2ccd7 61 74 65 28 64 62 2c 20 54 4b 5f 44 45 4c 45 54  ate(db, TK_DELET
2ccd8 45 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a  E, pTableName);.
2ccd9 20 20 69 66 28 20 70 54 72 69 67 67 65 72 53 74    if( pTriggerSt
2ccda 65 70 20 29 7b 0a 20 20 20 20 70 54 72 69 67 67  ep ){.    pTrigg
2ccdb 65 72 53 74 65 70 2d 3e 70 57 68 65 72 65 20 3d  erStep->pWhere =
2ccdc 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
2ccdd 64 62 2c 20 70 57 68 65 72 65 2c 20 45 58 50 52  db, pWhere, EXPR
2ccde 44 55 50 5f 52 45 44 55 43 45 29 3b 0a 20 20 20  DUP_REDUCE);.   
2ccdf 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f   pTriggerStep->o
2cce0 72 63 6f 6e 66 20 3d 20 4f 45 5f 44 65 66 61 75  rconf = OE_Defau
2cce1 6c 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  lt;.  }.  sqlite
2cce2 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
2cce3 70 57 68 65 72 65 29 3b 0a 20 20 72 65 74 75 72  pWhere);.  retur
2cce4 6e 20 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a  n pTriggerStep;.
2cce5 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 63 75 72 73  }../* .** Recurs
2cce6 69 76 65 6c 79 20 64 65 6c 65 74 65 20 61 20 54  ively delete a T
2cce7 72 69 67 67 65 72 20 73 74 72 75 63 74 75 72 65  rigger structure
2cce8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2cce9 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44  TE void sqlite3D
2ccea 65 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c  eleteTrigger(sql
2cceb 69 74 65 33 20 2a 64 62 2c 20 54 72 69 67 67 65  ite3 *db, Trigge
2ccec 72 20 2a 70 54 72 69 67 67 65 72 29 7b 0a 20 20  r *pTrigger){.  
2cced 69 66 28 20 70 54 72 69 67 67 65 72 3d 3d 30 20  if( pTrigger==0 
2ccee 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69  ) return;.  sqli
2ccef 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72  te3DeleteTrigger
2ccf0 53 74 65 70 28 64 62 2c 20 70 54 72 69 67 67 65  Step(db, pTrigge
2ccf1 72 2d 3e 73 74 65 70 5f 6c 69 73 74 29 3b 0a 20  r->step_list);. 
2ccf2 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2ccf3 62 2c 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61  b, pTrigger->zNa
2ccf4 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  me);.  sqlite3Db
2ccf5 46 72 65 65 28 64 62 2c 20 70 54 72 69 67 67 65  Free(db, pTrigge
2ccf6 72 2d 3e 74 61 62 6c 65 29 3b 0a 20 20 73 71 6c  r->table);.  sql
2ccf7 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2ccf8 62 2c 20 70 54 72 69 67 67 65 72 2d 3e 70 57 68  b, pTrigger->pWh
2ccf9 65 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 49 64  en);.  sqlite3Id
2ccfa 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
2ccfb 54 72 69 67 67 65 72 2d 3e 70 43 6f 6c 75 6d 6e  Trigger->pColumn
2ccfc 73 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  s);.  sqlite3DbF
2ccfd 72 65 65 28 64 62 2c 20 70 54 72 69 67 67 65 72  ree(db, pTrigger
2ccfe 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
2ccff 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
2cd00 6c 65 64 20 74 6f 20 64 72 6f 70 20 61 20 74 72  led to drop a tr
2cd01 69 67 67 65 72 20 66 72 6f 6d 20 74 68 65 20 64  igger from the d
2cd02 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20  atabase schema. 
2cd03 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 79 20  .**.** This may 
2cd04 62 65 20 63 61 6c 6c 65 64 20 64 69 72 65 63 74  be called direct
2cd05 6c 79 20 66 72 6f 6d 20 74 68 65 20 70 61 72 73  ly from the pars
2cd06 65 72 20 61 6e 64 20 74 68 65 72 65 66 6f 72 65  er and therefore
2cd07 20 69 64 65 6e 74 69 66 69 65 73 0a 2a 2a 20 74   identifies.** t
2cd08 68 65 20 74 72 69 67 67 65 72 20 62 79 20 6e 61  he trigger by na
2cd09 6d 65 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  me.  The sqlite3
2cd0a 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 29  DropTriggerPtr()
2cd0b 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 74 68   routine does th
2cd0c 65 0a 2a 2a 20 73 61 6d 65 20 6a 6f 62 20 61 73  e.** same job as
2cd0d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 65 78   this routine ex
2cd0e 63 65 70 74 20 69 74 20 74 61 6b 65 73 20 61 20  cept it takes a 
2cd0f 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 74  pointer to the t
2cd10 72 69 67 67 65 72 0a 2a 2a 20 69 6e 73 74 65 61  rigger.** instea
2cd11 64 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72  d of the trigger
2cd12 20 6e 61 6d 65 2e 0a 2a 2a 2f 0a 53 51 4c 49 54   name..**/.SQLIT
2cd13 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2cd14 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65  qlite3DropTrigge
2cd15 72 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  r(Parse *pParse,
2cd16 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d 65 2c   SrcList *pName,
2cd17 20 69 6e 74 20 6e 6f 45 72 72 29 7b 0a 20 20 54   int noErr){.  T
2cd18 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72  rigger *pTrigger
2cd19 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20   = 0;.  int i;. 
2cd1a 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
2cd1b 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
2cd1c 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e 4e 61  zName;.  int nNa
2cd1d 6d 65 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  me;.  sqlite3 *d
2cd1e 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2cd1f 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
2cd20 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 64  cFailed ) goto d
2cd21 72 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65 61  rop_trigger_clea
2cd22 6e 75 70 3b 0a 20 20 69 66 28 20 53 51 4c 49 54  nup;.  if( SQLIT
2cd23 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 52 65 61  E_OK!=sqlite3Rea
2cd24 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
2cd25 29 7b 0a 20 20 20 20 67 6f 74 6f 20 64 72 6f 70  ){.    goto drop
2cd26 5f 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70  _trigger_cleanup
2cd27 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
2cd28 20 70 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20   pName->nSrc==1 
2cd29 29 3b 0a 20 20 7a 44 62 20 3d 20 70 4e 61 6d 65  );.  zDb = pName
2cd2a 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65  ->a[0].zDatabase
2cd2b 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20 70 4e 61 6d  ;.  zName = pNam
2cd2c 65 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 3b 0a 20  e->a[0].zName;. 
2cd2d 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   nName = sqlite3
2cd2e 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b  Strlen30(zName);
2cd2f 0a 20 20 66 6f 72 28 69 3d 4f 4d 49 54 5f 54 45  .  for(i=OMIT_TE
2cd30 4d 50 44 42 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b  MPDB; i<db->nDb;
2cd31 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6a   i++){.    int j
2cd32 20 3d 20 28 69 3c 32 29 20 3f 20 69 5e 31 20 3a   = (i<2) ? i^1 :
2cd33 20 69 3b 20 20 2f 2a 20 53 65 61 72 63 68 20 54   i;  /* Search T
2cd34 45 4d 50 20 62 65 66 6f 72 65 20 4d 41 49 4e 20  EMP before MAIN 
2cd35 2a 2f 0a 20 20 20 20 69 66 28 20 7a 44 62 20 26  */.    if( zDb &
2cd36 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  & sqlite3StrICmp
2cd37 28 64 62 2d 3e 61 44 62 5b 6a 5d 2e 7a 4e 61 6d  (db->aDb[j].zNam
2cd38 65 2c 20 7a 44 62 29 20 29 20 63 6f 6e 74 69 6e  e, zDb) ) contin
2cd39 75 65 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72  ue;.    pTrigger
2cd3a 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 69   = sqlite3HashFi
2cd3b 6e 64 28 26 28 64 62 2d 3e 61 44 62 5b 6a 5d 2e  nd(&(db->aDb[j].
2cd3c 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73  pSchema->trigHas
2cd3d 68 29 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  h), zName, nName
2cd3e 29 3b 0a 20 20 20 20 69 66 28 20 70 54 72 69 67  );.    if( pTrig
2cd3f 67 65 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d  ger ) break;.  }
2cd40 0a 20 20 69 66 28 20 21 70 54 72 69 67 67 65 72  .  if( !pTrigger
2cd41 20 29 7b 0a 20 20 20 20 69 66 28 20 21 6e 6f 45   ){.    if( !noE
2cd42 72 72 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  rr ){.      sqli
2cd43 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
2cd44 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 74 72 69  se, "no such tri
2cd45 67 67 65 72 3a 20 25 53 22 2c 20 70 4e 61 6d 65  gger: %S", pName
2cd46 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , 0);.    }.    
2cd47 67 6f 74 6f 20 64 72 6f 70 5f 74 72 69 67 67 65  goto drop_trigge
2cd48 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  r_cleanup;.  }. 
2cd49 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67   sqlite3DropTrig
2cd4a 67 65 72 50 74 72 28 70 50 61 72 73 65 2c 20 70  gerPtr(pParse, p
2cd4b 54 72 69 67 67 65 72 29 3b 0a 0a 64 72 6f 70 5f  Trigger);..drop_
2cd4c 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3a  trigger_cleanup:
2cd4d 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
2cd4e 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4e 61 6d  tDelete(db, pNam
2cd4f 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  e);.}../*.** Ret
2cd50 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
2cd51 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63   the Table struc
2cd52 74 75 72 65 20 66 6f 72 20 74 68 65 20 74 61 62  ture for the tab
2cd53 6c 65 20 74 68 61 74 20 61 20 74 72 69 67 67 65  le that a trigge
2cd54 72 0a 2a 2a 20 69 73 20 73 65 74 20 6f 6e 2e 0a  r.** is set on..
2cd55 2a 2f 0a 73 74 61 74 69 63 20 54 61 62 6c 65 20  */.static Table 
2cd56 2a 74 61 62 6c 65 4f 66 54 72 69 67 67 65 72 28  *tableOfTrigger(
2cd57 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
2cd58 72 29 7b 0a 20 20 69 6e 74 20 6e 20 3d 20 73 71  r){.  int n = sq
2cd59 6c 69 74 65 33 53 74 72 6c 65 6e 33 30 28 70 54  lite3Strlen30(pT
2cd5a 72 69 67 67 65 72 2d 3e 74 61 62 6c 65 29 3b 0a  rigger->table);.
2cd5b 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
2cd5c 48 61 73 68 46 69 6e 64 28 26 70 54 72 69 67 67  HashFind(&pTrigg
2cd5d 65 72 2d 3e 70 54 61 62 53 63 68 65 6d 61 2d 3e  er->pTabSchema->
2cd5e 74 62 6c 48 61 73 68 2c 20 70 54 72 69 67 67 65  tblHash, pTrigge
2cd5f 72 2d 3e 74 61 62 6c 65 2c 20 6e 29 3b 0a 7d 0a  r->table, n);.}.
2cd60 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 61 20 74  ../*.** Drop a t
2cd61 72 69 67 67 65 72 20 67 69 76 65 6e 20 61 20 70  rigger given a p
2cd62 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74 20 74  ointer to that t
2cd63 72 69 67 67 65 72 2e 20 0a 2a 2f 0a 53 51 4c 49  rigger. .*/.SQLI
2cd64 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
2cd65 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67  sqlite3DropTrigg
2cd66 65 72 50 74 72 28 50 61 72 73 65 20 2a 70 50 61  erPtr(Parse *pPa
2cd67 72 73 65 2c 20 54 72 69 67 67 65 72 20 2a 70 54  rse, Trigger *pT
2cd68 72 69 67 67 65 72 29 7b 0a 20 20 54 61 62 6c 65  rigger){.  Table
2cd69 20 20 20 2a 70 54 61 62 6c 65 3b 0a 20 20 56 64     *pTable;.  Vd
2cd6a 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33  be *v;.  sqlite3
2cd6b 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2cd6c 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20  b;.  int iDb;.. 
2cd6d 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
2cd6e 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
2cd6f 73 65 2d 3e 64 62 2c 20 70 54 72 69 67 67 65 72  se->db, pTrigger
2cd70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73  ->pSchema);.  as
2cd71 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20  sert( iDb>=0 && 
2cd72 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20  iDb<db->nDb );. 
2cd73 20 70 54 61 62 6c 65 20 3d 20 74 61 62 6c 65 4f   pTable = tableO
2cd74 66 54 72 69 67 67 65 72 28 70 54 72 69 67 67 65  fTrigger(pTrigge
2cd75 72 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54  r);.  assert( pT
2cd76 61 62 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74  able );.  assert
2cd77 28 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d  ( pTable->pSchem
2cd78 61 3d 3d 70 54 72 69 67 67 65 72 2d 3e 70 53 63  a==pTrigger->pSc
2cd79 68 65 6d 61 20 7c 7c 20 69 44 62 3d 3d 31 20 29  hema || iDb==1 )
2cd7a 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
2cd7b 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
2cd7c 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20  ION.  {.    int 
2cd7d 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52  code = SQLITE_DR
2cd7e 4f 50 5f 54 52 49 47 47 45 52 3b 0a 20 20 20 20  OP_TRIGGER;.    
2cd7f 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20  const char *zDb 
2cd80 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  = db->aDb[iDb].z
2cd81 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20  Name;.    const 
2cd82 63 68 61 72 20 2a 7a 54 61 62 20 3d 20 53 43 48  char *zTab = SCH
2cd83 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a  EMA_TABLE(iDb);.
2cd84 20 20 20 20 69 66 28 20 69 44 62 3d 3d 31 20 29      if( iDb==1 )
2cd85 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44   code = SQLITE_D
2cd86 52 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52  ROP_TEMP_TRIGGER
2cd87 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
2cd88 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
2cd89 65 2c 20 63 6f 64 65 2c 20 70 54 72 69 67 67 65  e, code, pTrigge
2cd8a 72 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 6c 65  r->zName, pTable
2cd8b 2d 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 7c 7c  ->zName, zDb) ||
2cd8c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 75  .      sqlite3Au
2cd8d 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
2cd8e 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a  SQLITE_DELETE, z
2cd8f 54 61 62 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a  Tab, 0, zDb) ){.
2cd90 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
2cd91 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a    }.  }.#endif..
2cd92 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f    /* Generate co
2cd93 64 65 20 74 6f 20 64 65 73 74 72 6f 79 20 74 68  de to destroy th
2cd94 65 20 64 61 74 61 62 61 73 65 20 72 65 63 6f 72  e database recor
2cd95 64 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72  d of the trigger
2cd96 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
2cd97 20 70 54 61 62 6c 65 21 3d 30 20 29 3b 0a 20 20   pTable!=0 );.  
2cd98 69 66 28 20 28 76 20 3d 20 73 71 6c 69 74 65 33  if( (v = sqlite3
2cd99 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 29  GetVdbe(pParse))
2cd9a 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 62  !=0 ){.    int b
2cd9b 61 73 65 3b 0a 20 20 20 20 73 74 61 74 69 63 20  ase;.    static 
2cd9c 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74  const VdbeOpList
2cd9d 20 64 72 6f 70 54 72 69 67 67 65 72 5b 5d 20 3d   dropTrigger[] =
2cd9e 20 7b 0a 20 20 20 20 20 20 7b 20 4f 50 5f 52 65   {.      { OP_Re
2cd9f 77 69 6e 64 2c 20 20 20 20 20 30 2c 20 41 44 44  wind,     0, ADD
2cda0 52 28 39 29 2c 20 20 30 7d 2c 0a 20 20 20 20 20  R(9),  0},.     
2cda1 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20   { OP_String8,  
2cda2 20 20 30 2c 20 31 2c 20 20 20 20 20 20 20 20 30    0, 1,        0
2cda3 7d 2c 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20  }, /* 1 */.     
2cda4 20 7b 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20 20   { OP_Column,   
2cda5 20 20 30 2c 20 31 2c 20 20 20 20 20 20 20 20 32    0, 1,        2
2cda6 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65  },.      { OP_Ne
2cda7 2c 20 20 20 20 20 20 20 20 20 32 2c 20 41 44 44  ,         2, ADD
2cda8 52 28 38 29 2c 20 20 31 7d 2c 0a 20 20 20 20 20  R(8),  1},.     
2cda9 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20   { OP_String8,  
2cdaa 20 20 30 2c 20 31 2c 20 20 20 20 20 20 20 20 30    0, 1,        0
2cdab 7d 2c 20 2f 2a 20 34 3a 20 22 74 72 69 67 67 65  }, /* 4: "trigge
2cdac 72 22 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50  r" */.      { OP
2cdad 5f 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 30 2c 20  _Column,     0, 
2cdae 30 2c 20 20 20 20 20 20 20 20 32 7d 2c 0a 20 20  0,        2},.  
2cdaf 20 20 20 20 7b 20 4f 50 5f 4e 65 2c 20 20 20 20      { OP_Ne,    
2cdb0 20 20 20 20 20 32 2c 20 41 44 44 52 28 38 29 2c       2, ADDR(8),
2cdb1 20 20 31 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50    1},.      { OP
2cdb2 5f 44 65 6c 65 74 65 2c 20 20 20 20 20 30 2c 20  _Delete,     0, 
2cdb3 30 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20  0,        0},.  
2cdb4 20 20 20 20 7b 20 4f 50 5f 4e 65 78 74 2c 20 20      { OP_Next,  
2cdb5 20 20 20 20 20 30 2c 20 41 44 44 52 28 31 29 2c       0, ADDR(1),
2cdb6 20 20 30 7d 2c 20 2f 2a 20 38 20 2a 2f 0a 20 20    0}, /* 8 */.  
2cdb7 20 20 7d 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65    };..    sqlite
2cdb8 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
2cdb9 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20  tion(pParse, 0, 
2cdba 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  iDb);.    sqlite
2cdbb 33 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 6c 65  3OpenMasterTable
2cdbc 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20  (pParse, iDb);. 
2cdbd 20 20 20 62 61 73 65 20 3d 20 73 71 6c 69 74 65     base = sqlite
2cdbe 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76  3VdbeAddOpList(v
2cdbf 2c 20 20 41 72 72 61 79 53 69 7a 65 28 64 72 6f  ,  ArraySize(dro
2cdc0 70 54 72 69 67 67 65 72 29 2c 20 64 72 6f 70 54  pTrigger), dropT
2cdc1 72 69 67 67 65 72 29 3b 0a 20 20 20 20 73 71 6c  rigger);.    sql
2cdc2 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
2cdc3 28 76 2c 20 62 61 73 65 2b 31 2c 20 70 54 72 69  (v, base+1, pTri
2cdc4 67 67 65 72 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b  gger->zName, 0);
2cdc5 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2cdc6 43 68 61 6e 67 65 50 34 28 76 2c 20 62 61 73 65  ChangeP4(v, base
2cdc7 2b 34 2c 20 22 74 72 69 67 67 65 72 22 2c 20 50  +4, "trigger", P
2cdc8 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73  4_STATIC);.    s
2cdc9 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b  qlite3ChangeCook
2cdca 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b  ie(pParse, iDb);
2cdcb 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2cdcc 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp2(v, OP_Clo
2cdcd 73 65 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  se, 0, 0);.    s
2cdce 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2cdcf 28 76 2c 20 4f 50 5f 44 72 6f 70 54 72 69 67 67  (v, OP_DropTrigg
2cdd0 65 72 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 70  er, iDb, 0, 0, p
2cdd1 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 2c 20  Trigger->zName, 
2cdd2 30 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 72  0);.    if( pPar
2cdd3 73 65 2d 3e 6e 4d 65 6d 3c 33 20 29 7b 0a 20 20  se->nMem<3 ){.  
2cdd4 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d      pParse->nMem
2cdd5 20 3d 20 33 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 3;.    }.  }.
2cdd6 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20  }../*.** Remove 
2cdd7 61 20 74 72 69 67 67 65 72 20 66 72 6f 6d 20 74  a trigger from t
2cdd8 68 65 20 68 61 73 68 20 74 61 62 6c 65 73 20 6f  he hash tables o
2cdd9 66 20 74 68 65 20 73 71 6c 69 74 65 2a 20 70 6f  f the sqlite* po
2cdda 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  inter..*/.SQLITE
2cddb 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2cddc 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65  lite3UnlinkAndDe
2cddd 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c 69  leteTrigger(sqli
2cdde 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69 44 62  te3 *db, int iDb
2cddf 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
2cde0 61 6d 65 29 7b 0a 20 20 48 61 73 68 20 2a 70 48  ame){.  Hash *pH
2cde1 61 73 68 20 3d 20 26 28 64 62 2d 3e 61 44 62 5b  ash = &(db->aDb[
2cde2 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72  iDb].pSchema->tr
2cde3 69 67 48 61 73 68 29 3b 0a 20 20 54 72 69 67 67  igHash);.  Trigg
2cde4 65 72 20 2a 70 54 72 69 67 67 65 72 3b 0a 20 20  er *pTrigger;.  
2cde5 70 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74  pTrigger = sqlit
2cde6 65 33 48 61 73 68 49 6e 73 65 72 74 28 70 48 61  e3HashInsert(pHa
2cde7 73 68 2c 20 7a 4e 61 6d 65 2c 20 73 71 6c 69 74  sh, zName, sqlit
2cde8 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65  e3Strlen30(zName
2cde9 29 2c 20 30 29 3b 0a 20 20 69 66 28 20 41 4c 57  ), 0);.  if( ALW
2cdea 41 59 53 28 70 54 72 69 67 67 65 72 29 20 29 7b  AYS(pTrigger) ){
2cdeb 0a 20 20 20 20 69 66 28 20 70 54 72 69 67 67 65  .    if( pTrigge
2cdec 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 72 69  r->pSchema==pTri
2cded 67 67 65 72 2d 3e 70 54 61 62 53 63 68 65 6d 61  gger->pTabSchema
2cdee 20 29 7b 0a 20 20 20 20 20 20 54 61 62 6c 65 20   ){.      Table 
2cdef 2a 70 54 61 62 20 3d 20 74 61 62 6c 65 4f 66 54  *pTab = tableOfT
2cdf0 72 69 67 67 65 72 28 70 54 72 69 67 67 65 72 29  rigger(pTrigger)
2cdf1 3b 0a 20 20 20 20 20 20 54 72 69 67 67 65 72 20  ;.      Trigger 
2cdf2 2a 2a 70 70 3b 0a 20 20 20 20 20 20 66 6f 72 28  **pp;.      for(
2cdf3 70 70 3d 26 70 54 61 62 2d 3e 70 54 72 69 67 67  pp=&pTab->pTrigg
2cdf4 65 72 3b 20 2a 70 70 21 3d 70 54 72 69 67 67 65  er; *pp!=pTrigge
2cdf5 72 3b 20 70 70 3d 26 28 28 2a 70 70 29 2d 3e 70  r; pp=&((*pp)->p
2cdf6 4e 65 78 74 29 29 3b 0a 20 20 20 20 20 20 2a 70  Next));.      *p
2cdf7 70 20 3d 20 28 2a 70 70 29 2d 3e 70 4e 65 78 74  p = (*pp)->pNext
2cdf8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
2cdf9 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72  te3DeleteTrigger
2cdfa 28 64 62 2c 20 70 54 72 69 67 67 65 72 29 3b 0a  (db, pTrigger);.
2cdfb 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d      db->flags |=
2cdfc 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68   SQLITE_InternCh
2cdfd 61 6e 67 65 73 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  anges;.  }.}../*
2cdfe 0a 2a 2a 20 70 45 4c 69 73 74 20 69 73 20 74 68  .** pEList is th
2cdff 65 20 53 45 54 20 63 6c 61 75 73 65 20 6f 66 20  e SET clause of 
2ce00 61 6e 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  an UPDATE statem
2ce01 65 6e 74 2e 20 20 45 61 63 68 20 65 6e 74 72 79  ent.  Each entry
2ce02 0a 2a 2a 20 69 6e 20 70 45 4c 69 73 74 20 69 73  .** in pEList is
2ce03 20 6f 66 20 74 68 65 20 66 6f 72 6d 61 74 20 3c   of the format <
2ce04 69 64 3e 3d 3c 65 78 70 72 3e 2e 20 20 49 66 20  id>=<expr>.  If 
2ce05 61 6e 79 20 6f 66 20 74 68 65 20 65 6e 74 72 69  any of the entri
2ce06 65 73 0a 2a 2a 20 69 6e 20 70 45 4c 69 73 74 20  es.** in pEList 
2ce07 68 61 76 65 20 61 6e 20 3c 69 64 3e 20 77 68 69  have an <id> whi
2ce08 63 68 20 6d 61 74 63 68 65 73 20 61 6e 20 69 64  ch matches an id
2ce09 65 6e 74 69 66 69 65 72 20 69 6e 20 70 49 64 4c  entifier in pIdL
2ce0a 69 73 74 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 74  ist,.** then ret
2ce0b 75 72 6e 20 54 52 55 45 2e 20 20 49 66 20 70 49  urn TRUE.  If pI
2ce0c 64 4c 69 73 74 3d 3d 4e 55 4c 4c 2c 20 74 68 65  dList==NULL, the
2ce0d 6e 20 69 74 20 69 73 20 63 6f 6e 73 69 64 65 72  n it is consider
2ce0e 65 64 20 61 0a 2a 2a 20 77 69 6c 64 63 61 72 64  ed a.** wildcard
2ce0f 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 61 6e   that matches an
2ce10 79 74 68 69 6e 67 2e 20 20 4c 69 6b 65 77 69 73  ything.  Likewis
2ce11 65 20 69 66 20 70 45 4c 69 73 74 3d 3d 4e 55 4c  e if pEList==NUL
2ce12 4c 20 74 68 65 6e 0a 2a 2a 20 69 74 20 6d 61 74  L then.** it mat
2ce13 63 68 65 73 20 61 6e 79 74 68 69 6e 67 20 73 6f  ches anything so
2ce14 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20 74   always return t
2ce15 72 75 65 2e 20 20 52 65 74 75 72 6e 20 66 61 6c  rue.  Return fal
2ce16 73 65 20 6f 6e 6c 79 0a 2a 2a 20 69 66 20 74 68  se only.** if th
2ce17 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2e  ere is no match.
2ce18 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
2ce19 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76 65 72 6c 61  heckColumnOverla
2ce1a 70 28 49 64 4c 69 73 74 20 2a 70 49 64 4c 69 73  p(IdList *pIdLis
2ce1b 74 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  t, ExprList *pEL
2ce1c 69 73 74 29 7b 0a 20 20 69 6e 74 20 65 3b 0a 20  ist){.  int e;. 
2ce1d 20 69 66 28 20 70 49 64 4c 69 73 74 3d 3d 30 20   if( pIdList==0 
2ce1e 7c 7c 20 4e 45 56 45 52 28 70 45 4c 69 73 74 3d  || NEVER(pEList=
2ce1f 3d 30 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  =0) ) return 1;.
2ce20 20 20 66 6f 72 28 65 3d 30 3b 20 65 3c 70 45 4c    for(e=0; e<pEL
2ce21 69 73 74 2d 3e 6e 45 78 70 72 3b 20 65 2b 2b 29  ist->nExpr; e++)
2ce22 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
2ce23 33 49 64 4c 69 73 74 49 6e 64 65 78 28 70 49 64  3IdListIndex(pId
2ce24 4c 69 73 74 2c 20 70 45 4c 69 73 74 2d 3e 61 5b  List, pEList->a[
2ce25 65 5d 2e 7a 4e 61 6d 65 29 3e 3d 30 20 29 20 72  e].zName)>=0 ) r
2ce26 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72  eturn 1;.  }.  r
2ce27 65 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a 2f 2a 0a  eturn 0; .}../*.
2ce28 2a 2a 20 52 65 74 75 72 6e 20 61 20 6c 69 73 74  ** Return a list
2ce29 20 6f 66 20 61 6c 6c 20 74 72 69 67 67 65 72 73   of all triggers
2ce2a 20 6f 6e 20 74 61 62 6c 65 20 70 54 61 62 20 69   on table pTab i
2ce2b 66 20 74 68 65 72 65 20 65 78 69 73 74 73 20 61  f there exists a
2ce2c 74 20 6c 65 61 73 74 0a 2a 2a 20 6f 6e 65 20 74  t least.** one t
2ce2d 72 69 67 67 65 72 20 74 68 61 74 20 6d 75 73 74  rigger that must
2ce2e 20 62 65 20 66 69 72 65 64 20 77 68 65 6e 20 61   be fired when a
2ce2f 6e 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74  n operation of t
2ce30 79 70 65 20 27 6f 70 27 20 69 73 20 0a 2a 2a 20  ype 'op' is .** 
2ce31 70 65 72 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65  performed on the
2ce32 20 74 61 62 6c 65 2c 20 61 6e 64 2c 20 69 66 20   table, and, if 
2ce33 74 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 69  that operation i
2ce34 73 20 61 6e 20 55 50 44 41 54 45 2c 20 69 66 20  s an UPDATE, if 
2ce35 61 74 0a 2a 2a 20 6c 65 61 73 74 20 6f 6e 65 20  at.** least one 
2ce36 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 69  of the columns i
2ce37 6e 20 70 43 68 61 6e 67 65 73 20 69 73 20 62 65  n pChanges is be
2ce38 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e 0a 2a 2f  ing modified..*/
2ce39 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2ce3a 54 72 69 67 67 65 72 20 2a 73 71 6c 69 74 65 33  Trigger *sqlite3
2ce3b 54 72 69 67 67 65 72 73 45 78 69 73 74 28 0a 20  TriggersExist(. 
2ce3c 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2ce3d 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
2ce3e 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  e context */.  T
2ce3f 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
2ce40 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
2ce41 62 6c 65 20 74 68 65 20 63 6f 6e 74 61 69 6e 73  ble the contains
2ce42 20 74 68 65 20 74 72 69 67 67 65 72 73 20 2a 2f   the triggers */
2ce43 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20  .  int op,      
2ce44 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f 6e             /* on
2ce45 65 20 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20  e of TK_DELETE, 
2ce46 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 55 50  TK_INSERT, TK_UP
2ce47 44 41 54 45 20 2a 2f 0a 20 20 45 78 70 72 4c 69  DATE */.  ExprLi
2ce48 73 74 20 2a 70 43 68 61 6e 67 65 73 2c 20 20 20  st *pChanges,   
2ce49 20 20 2f 2a 20 43 6f 6c 75 6d 6e 73 20 74 68 61    /* Columns tha
2ce4a 74 20 63 68 61 6e 67 65 20 69 6e 20 61 6e 20 55  t change in an U
2ce4b 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20  PDATE statement 
2ce4c 2a 2f 0a 20 20 69 6e 74 20 2a 70 4d 61 73 6b 20  */.  int *pMask 
2ce4d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2ce4e 4f 55 54 3a 20 4d 61 73 6b 20 6f 66 20 54 52 49  OUT: Mask of TRI
2ce4f 47 47 45 52 5f 42 45 46 4f 52 45 7c 54 52 49 47  GGER_BEFORE|TRIG
2ce50 47 45 52 5f 41 46 54 45 52 20 2a 2f 0a 29 7b 0a  GER_AFTER */.){.
2ce51 20 20 69 6e 74 20 6d 61 73 6b 20 3d 20 30 3b 0a    int mask = 0;.
2ce52 20 20 54 72 69 67 67 65 72 20 2a 70 4c 69 73 74    Trigger *pList
2ce53 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65   = sqlite3Trigge
2ce54 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 54  rList(pParse, pT
2ce55 61 62 29 3b 0a 20 20 54 72 69 67 67 65 72 20 2a  ab);.  Trigger *
2ce56 70 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 69  p;.  assert( pLi
2ce57 73 74 3d 3d 30 20 7c 7c 20 49 73 56 69 72 74 75  st==0 || IsVirtu
2ce58 61 6c 28 70 54 61 62 29 3d 3d 30 20 29 3b 0a 20  al(pTab)==0 );. 
2ce59 20 66 6f 72 28 70 3d 70 4c 69 73 74 3b 20 70 3b   for(p=pList; p;
2ce5a 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20   p=p->pNext){.  
2ce5b 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 6f 70 20    if( p->op==op 
2ce5c 26 26 20 63 68 65 63 6b 43 6f 6c 75 6d 6e 4f 76  && checkColumnOv
2ce5d 65 72 6c 61 70 28 70 2d 3e 70 43 6f 6c 75 6d 6e  erlap(p->pColumn
2ce5e 73 2c 20 70 43 68 61 6e 67 65 73 29 20 29 7b 0a  s, pChanges) ){.
2ce5f 20 20 20 20 20 20 6d 61 73 6b 20 7c 3d 20 70 2d        mask |= p-
2ce60 3e 74 72 5f 74 6d 3b 0a 20 20 20 20 7d 0a 20 20  >tr_tm;.    }.  
2ce61 7d 0a 20 20 69 66 28 20 70 4d 61 73 6b 20 29 7b  }.  if( pMask ){
2ce62 0a 20 20 20 20 2a 70 4d 61 73 6b 20 3d 20 6d 61  .    *pMask = ma
2ce63 73 6b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  sk;.  }.  return
2ce64 20 28 6d 61 73 6b 20 3f 20 70 4c 69 73 74 20 3a   (mask ? pList :
2ce65 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f   0);.}../*.** Co
2ce66 6e 76 65 72 74 20 74 68 65 20 70 53 74 65 70 2d  nvert the pStep-
2ce67 3e 74 61 72 67 65 74 20 74 6f 6b 65 6e 20 69 6e  >target token in
2ce68 74 6f 20 61 20 53 72 63 4c 69 73 74 20 61 6e 64  to a SrcList and
2ce69 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   return a pointe
2ce6a 72 0a 2a 2a 20 74 6f 20 74 68 61 74 20 53 72 63  r.** to that Src
2ce6b 4c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  List..**.** This
2ce6c 20 72 6f 75 74 69 6e 65 20 61 64 64 73 20 61 20   routine adds a 
2ce6d 73 70 65 63 69 66 69 63 20 64 61 74 61 62 61 73  specific databas
2ce6e 65 20 6e 61 6d 65 2c 20 69 66 20 6e 65 65 64 65  e name, if neede
2ce6f 64 2c 20 74 6f 20 74 68 65 20 74 61 72 67 65 74  d, to the target
2ce70 20 77 68 65 6e 0a 2a 2a 20 66 6f 72 6d 69 6e 67   when.** forming
2ce71 20 74 68 65 20 53 72 63 4c 69 73 74 2e 20 20 54   the SrcList.  T
2ce72 68 69 73 20 70 72 65 76 65 6e 74 73 20 61 20 74  his prevents a t
2ce73 72 69 67 67 65 72 20 69 6e 20 6f 6e 65 20 64 61  rigger in one da
2ce74 74 61 62 61 73 65 20 66 72 6f 6d 0a 2a 2a 20 72  tabase from.** r
2ce75 65 66 65 72 72 69 6e 67 20 74 6f 20 61 20 74 61  eferring to a ta
2ce76 72 67 65 74 20 69 6e 20 61 6e 6f 74 68 65 72 20  rget in another 
2ce77 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20 65 78  database.  An ex
2ce78 63 65 70 74 69 6f 6e 20 69 73 20 77 68 65 6e 20  ception is when 
2ce79 74 68 65 0a 2a 2a 20 74 72 69 67 67 65 72 20 69  the.** trigger i
2ce7a 73 20 69 6e 20 54 45 4d 50 20 69 6e 20 77 68 69  s in TEMP in whi
2ce7b 63 68 20 63 61 73 65 20 69 74 20 63 61 6e 20 72  ch case it can r
2ce7c 65 66 65 72 20 74 6f 20 61 6e 79 20 6f 74 68 65  efer to any othe
2ce7d 72 20 64 61 74 61 62 61 73 65 20 69 74 0a 2a 2a  r database it.**
2ce7e 20 77 61 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69   wants..*/.stati
2ce7f 63 20 53 72 63 4c 69 73 74 20 2a 74 61 72 67 65  c SrcList *targe
2ce80 74 53 72 63 4c 69 73 74 28 0a 20 20 50 61 72 73  tSrcList(.  Pars
2ce81 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2ce82 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20   /* The parsing 
2ce83 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69  context */.  Tri
2ce84 67 67 65 72 53 74 65 70 20 2a 70 53 74 65 70 20  ggerStep *pStep 
2ce85 20 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72    /* The trigger
2ce86 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
2ce87 74 61 72 67 65 74 20 74 6f 6b 65 6e 20 2a 2f 0a  target token */.
2ce88 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  ){.  int iDb;   
2ce89 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
2ce8a 65 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ex of the databa
2ce8b 73 65 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 53  se to use */.  S
2ce8c 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20 20 20  rcList *pSrc;   
2ce8d 20 20 20 20 2f 2a 20 53 72 63 4c 69 73 74 20 74      /* SrcList t
2ce8e 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a 2f  o be returned */
2ce8f 0a 0a 20 20 70 53 72 63 20 3d 20 73 71 6c 69 74  ..  pSrc = sqlit
2ce90 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28  e3SrcListAppend(
2ce91 70 50 61 72 73 65 2d 3e 64 62 2c 20 30 2c 20 26  pParse->db, 0, &
2ce92 70 53 74 65 70 2d 3e 74 61 72 67 65 74 2c 20 30  pStep->target, 0
2ce93 29 3b 0a 20 20 69 66 28 20 70 53 72 63 20 29 7b  );.  if( pSrc ){
2ce94 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72  .    assert( pSr
2ce95 63 2d 3e 6e 53 72 63 3e 30 20 29 3b 0a 20 20 20  c->nSrc>0 );.   
2ce96 20 61 73 73 65 72 74 28 20 70 53 72 63 2d 3e 61   assert( pSrc->a
2ce97 21 3d 30 20 29 3b 0a 20 20 20 20 69 44 62 20 3d  !=0 );.    iDb =
2ce98 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f   sqlite3SchemaTo
2ce99 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62  Index(pParse->db
2ce9a 2c 20 70 53 74 65 70 2d 3e 70 54 72 69 67 2d 3e  , pStep->pTrig->
2ce9b 70 53 63 68 65 6d 61 29 3b 0a 20 20 20 20 69 66  pSchema);.    if
2ce9c 28 20 69 44 62 3d 3d 30 20 7c 7c 20 69 44 62 3e  ( iDb==0 || iDb>
2ce9d 3d 32 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =2 ){.      sqli
2ce9e 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2ce9f 2d 3e 64 62 3b 0a 20 20 20 20 20 20 61 73 73 65  ->db;.      asse
2cea0 72 74 28 20 69 44 62 3c 70 50 61 72 73 65 2d 3e  rt( iDb<pParse->
2cea1 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 20 20 20  db->nDb );.     
2cea2 20 70 53 72 63 2d 3e 61 5b 70 53 72 63 2d 3e 6e   pSrc->a[pSrc->n
2cea3 53 72 63 2d 31 5d 2e 7a 44 61 74 61 62 61 73 65  Src-1].zDatabase
2cea4 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
2cea5 75 70 28 64 62 2c 20 64 62 2d 3e 61 44 62 5b 69  up(db, db->aDb[i
2cea6 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  Db].zName);.    
2cea7 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  }.  }.  return p
2cea8 53 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  Src;.}../*.** Ge
2cea9 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f 64 65  nerate VDBE code
2ceaa 20 66 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65   for the stateme
2ceab 6e 74 73 20 69 6e 73 69 64 65 20 74 68 65 20 62  nts inside the b
2ceac 6f 64 79 20 6f 66 20 61 20 73 69 6e 67 6c 65 20  ody of a single 
2cead 0a 2a 2a 20 74 72 69 67 67 65 72 2e 0a 2a 2f 0a  .** trigger..*/.
2ceae 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 54  static int codeT
2ceaf 72 69 67 67 65 72 50 72 6f 67 72 61 6d 28 0a 20  riggerProgram(. 
2ceb0 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2ceb1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2ceb2 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
2ceb3 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 65   */.  TriggerSte
2ceb4 70 20 2a 70 53 74 65 70 4c 69 73 74 2c 20 20 20  p *pStepList,   
2ceb5 2f 2a 20 4c 69 73 74 20 6f 66 20 73 74 61 74 65  /* List of state
2ceb6 6d 65 6e 74 73 20 69 6e 73 69 64 65 20 74 68 65  ments inside the
2ceb7 20 74 72 69 67 67 65 72 20 62 6f 64 79 20 2a 2f   trigger body */
2ceb8 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 20 20 20  .  int orconf   
2ceb9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2ceba 43 6f 6e 66 6c 69 63 74 20 61 6c 67 6f 72 69 74  Conflict algorit
2cebb 68 6d 2e 20 28 4f 45 5f 41 62 6f 72 74 2c 20 65  hm. (OE_Abort, e
2cebc 74 63 29 20 2a 2f 20 20 0a 29 7b 0a 20 20 54 72  tc) */  .){.  Tr
2cebd 69 67 67 65 72 53 74 65 70 20 2a 70 53 74 65 70  iggerStep *pStep
2cebe 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50  ;.  Vdbe *v = pP
2cebf 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 73  arse->pVdbe;.  s
2cec0 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2cec1 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65  rse->db;..  asse
2cec2 72 74 28 20 70 50 61 72 73 65 2d 3e 70 54 72 69  rt( pParse->pTri
2cec3 67 67 65 72 54 61 62 20 26 26 20 70 50 61 72 73  ggerTab && pPars
2cec4 65 2d 3e 70 54 6f 70 6c 65 76 65 6c 20 29 3b 0a  e->pToplevel );.
2cec5 20 20 61 73 73 65 72 74 28 20 70 53 74 65 70 4c    assert( pStepL
2cec6 69 73 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ist );.  assert(
2cec7 20 76 21 3d 30 20 29 3b 0a 20 20 66 6f 72 28 70   v!=0 );.  for(p
2cec8 53 74 65 70 3d 70 53 74 65 70 4c 69 73 74 3b 20  Step=pStepList; 
2cec9 70 53 74 65 70 3b 20 70 53 74 65 70 3d 70 53 74  pStep; pStep=pSt
2ceca 65 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  ep->pNext){.    
2cecb 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74 68  /* Figure out th
2cecc 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f  e ON CONFLICT po
2cecd 6c 69 63 79 20 74 68 61 74 20 77 69 6c 6c 20 62  licy that will b
2cece 65 20 75 73 65 64 20 66 6f 72 20 74 68 69 73 20  e used for this 
2cecf 73 74 65 70 0a 20 20 20 20 2a 2a 20 6f 66 20 74  step.    ** of t
2ced0 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  he trigger progr
2ced1 61 6d 2e 20 49 66 20 74 68 65 20 73 74 61 74 65  am. If the state
2ced2 6d 65 6e 74 20 74 68 61 74 20 63 61 75 73 65 64  ment that caused
2ced3 20 74 68 69 73 20 74 72 69 67 67 65 72 0a 20 20   this trigger.  
2ced4 20 20 2a 2a 20 74 6f 20 66 69 72 65 20 68 61 64    ** to fire had
2ced5 20 61 6e 20 65 78 70 6c 69 63 69 74 20 4f 4e 20   an explicit ON 
2ced6 43 4f 4e 46 4c 49 43 54 2c 20 74 68 65 6e 20 75  CONFLICT, then u
2ced7 73 65 20 69 74 2e 20 4f 74 68 65 72 77 69 73 65  se it. Otherwise
2ced8 2c 20 75 73 65 0a 20 20 20 20 2a 2a 20 74 68 65  , use.    ** the
2ced9 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c   ON CONFLICT pol
2ceda 69 63 79 20 74 68 61 74 20 77 61 73 20 73 70 65  icy that was spe
2cedb 63 69 66 69 65 64 20 61 73 20 70 61 72 74 20 6f  cified as part o
2cedc 66 20 74 68 65 20 74 72 69 67 67 65 72 0a 20 20  f the trigger.  
2cedd 20 20 2a 2a 20 73 74 65 70 20 73 74 61 74 65 6d    ** step statem
2cede 65 6e 74 2e 20 45 78 61 6d 70 6c 65 3a 0a 20 20  ent. Example:.  
2cedf 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 43 52    **.    **   CR
2cee0 45 41 54 45 20 54 52 49 47 47 45 52 20 41 46 54  EATE TRIGGER AFT
2cee1 45 52 20 49 4e 53 45 52 54 20 4f 4e 20 74 31 20  ER INSERT ON t1 
2cee2 42 45 47 49 4e 3b 0a 20 20 20 20 2a 2a 20 20 20  BEGIN;.    **   
2cee3 20 20 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c    INSERT OR REPL
2cee4 41 43 45 20 49 4e 54 4f 20 74 32 20 56 41 4c 55  ACE INTO t2 VALU
2cee5 45 53 28 6e 65 77 2e 61 2c 20 6e 65 77 2e 62 29  ES(new.a, new.b)
2cee6 3b 0a 20 20 20 20 2a 2a 20 20 20 45 4e 44 3b 0a  ;.    **   END;.
2cee7 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
2cee8 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 31 20 2e  INSERT INTO t1 .
2cee9 2e 2e 20 3b 20 20 20 20 20 20 20 20 20 20 20 20  .. ;            
2ceea 2d 2d 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 74  -- insert into t
2ceeb 32 20 75 73 65 73 20 52 45 50 4c 41 43 45 20 70  2 uses REPLACE p
2ceec 6f 6c 69 63 79 0a 20 20 20 20 2a 2a 20 20 20 49  olicy.    **   I
2ceed 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20  NSERT OR IGNORE 
2ceee 49 4e 54 4f 20 74 31 20 2e 2e 2e 20 3b 20 20 2d  INTO t1 ... ;  -
2ceef 2d 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 74 32  - insert into t2
2cef0 20 75 73 65 73 20 49 47 4e 4f 52 45 20 70 6f 6c   uses IGNORE pol
2cef1 69 63 79 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  icy.    */.    p
2cef2 50 61 72 73 65 2d 3e 65 4f 72 63 6f 6e 66 20 3d  Parse->eOrconf =
2cef3 20 28 6f 72 63 6f 6e 66 3d 3d 4f 45 5f 44 65 66   (orconf==OE_Def
2cef4 61 75 6c 74 29 3f 70 53 74 65 70 2d 3e 6f 72 63  ault)?pStep->orc
2cef5 6f 6e 66 3a 28 75 38 29 6f 72 63 6f 6e 66 3b 0a  onf:(u8)orconf;.
2cef6 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 53 74  .    switch( pSt
2cef7 65 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 20 20  ep->op ){.      
2cef8 63 61 73 65 20 54 4b 5f 55 50 44 41 54 45 3a 20  case TK_UPDATE: 
2cef9 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2cefa 33 55 70 64 61 74 65 28 70 50 61 72 73 65 2c 20  3Update(pParse, 
2cefb 0a 20 20 20 20 20 20 20 20 20 20 74 61 72 67 65  .          targe
2cefc 74 53 72 63 4c 69 73 74 28 70 50 61 72 73 65 2c  tSrcList(pParse,
2cefd 20 70 53 74 65 70 29 2c 0a 20 20 20 20 20 20 20   pStep),.       
2cefe 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69     sqlite3ExprLi
2ceff 73 74 44 75 70 28 64 62 2c 20 70 53 74 65 70 2d  stDup(db, pStep-
2cf00 3e 70 45 78 70 72 4c 69 73 74 2c 20 30 29 2c 20  >pExprList, 0), 
2cf01 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2cf02 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 53  e3ExprDup(db, pS
2cf03 74 65 70 2d 3e 70 57 68 65 72 65 2c 20 30 29 2c  tep->pWhere, 0),
2cf04 20 0a 20 20 20 20 20 20 20 20 20 20 70 50 61 72   .          pPar
2cf05 73 65 2d 3e 65 4f 72 63 6f 6e 66 0a 20 20 20 20  se->eOrconf.    
2cf06 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 62      );.        b
2cf07 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2cf08 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 53 45      case TK_INSE
2cf09 52 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71  RT: {.        sq
2cf0a 6c 69 74 65 33 49 6e 73 65 72 74 28 70 50 61 72  lite3Insert(pPar
2cf0b 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 74  se, .          t
2cf0c 61 72 67 65 74 53 72 63 4c 69 73 74 28 70 50 61  argetSrcList(pPa
2cf0d 72 73 65 2c 20 70 53 74 65 70 29 2c 0a 20 20 20  rse, pStep),.   
2cf0e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2cf0f 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 53  prListDup(db, pS
2cf10 74 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 2c 20  tep->pExprList, 
2cf11 30 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20 73  0), .          s
2cf12 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28  qlite3SelectDup(
2cf13 64 62 2c 20 70 53 74 65 70 2d 3e 70 53 65 6c 65  db, pStep->pSele
2cf14 63 74 2c 20 30 29 2c 20 0a 20 20 20 20 20 20 20  ct, 0), .       
2cf15 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74     sqlite3IdList
2cf16 44 75 70 28 64 62 2c 20 70 53 74 65 70 2d 3e 70  Dup(db, pStep->p
2cf17 49 64 4c 69 73 74 29 2c 20 0a 20 20 20 20 20 20  IdList), .      
2cf18 20 20 20 20 70 50 61 72 73 65 2d 3e 65 4f 72 63      pParse->eOrc
2cf19 6f 6e 66 0a 20 20 20 20 20 20 20 20 29 3b 0a 20  onf.        );. 
2cf1a 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2cf1b 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
2cf1c 20 54 4b 5f 44 45 4c 45 54 45 3a 20 7b 0a 20 20   TK_DELETE: {.  
2cf1d 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c        sqlite3Del
2cf1e 65 74 65 46 72 6f 6d 28 70 50 61 72 73 65 2c 20  eteFrom(pParse, 
2cf1f 0a 20 20 20 20 20 20 20 20 20 20 74 61 72 67 65  .          targe
2cf20 74 53 72 63 4c 69 73 74 28 70 50 61 72 73 65 2c  tSrcList(pParse,
2cf21 20 70 53 74 65 70 29 2c 0a 20 20 20 20 20 20 20   pStep),.       
2cf22 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 75     sqlite3ExprDu
2cf23 70 28 64 62 2c 20 70 53 74 65 70 2d 3e 70 57 68  p(db, pStep->pWh
2cf24 65 72 65 2c 20 30 29 0a 20 20 20 20 20 20 20 20  ere, 0).        
2cf25 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
2cf26 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2cf27 64 65 66 61 75 6c 74 3a 20 61 73 73 65 72 74 28  default: assert(
2cf28 20 70 53 74 65 70 2d 3e 6f 70 3d 3d 54 4b 5f 53   pStep->op==TK_S
2cf29 45 4c 45 43 54 20 29 3b 20 7b 0a 20 20 20 20 20  ELECT ); {.     
2cf2a 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 73 44     SelectDest sD
2cf2b 65 73 74 3b 0a 20 20 20 20 20 20 20 20 53 65 6c  est;.        Sel
2cf2c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 3d 20 73  ect *pSelect = s
2cf2d 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28  qlite3SelectDup(
2cf2e 64 62 2c 20 70 53 74 65 70 2d 3e 70 53 65 6c 65  db, pStep->pSele
2cf2f 63 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  ct, 0);.        
2cf30 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73  sqlite3SelectDes
2cf31 74 49 6e 69 74 28 26 73 44 65 73 74 2c 20 53 52  tInit(&sDest, SR
2cf32 54 5f 44 69 73 63 61 72 64 2c 20 30 29 3b 0a 20  T_Discard, 0);. 
2cf33 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65         sqlite3Se
2cf34 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 65  lect(pParse, pSe
2cf35 6c 65 63 74 2c 20 26 73 44 65 73 74 29 3b 0a 20  lect, &sDest);. 
2cf36 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65         sqlite3Se
2cf37 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70  lectDelete(db, p
2cf38 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20  Select);.       
2cf39 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
2cf3a 20 20 20 20 7d 20 0a 20 20 20 20 69 66 28 20 70      } .    if( p
2cf3b 53 74 65 70 2d 3e 6f 70 21 3d 54 4b 5f 53 45 4c  Step->op!=TK_SEL
2cf3c 45 43 54 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ECT ){.      sql
2cf3d 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76  ite3VdbeAddOp0(v
2cf3e 2c 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 29  , OP_ResetCount)
2cf3f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  ;.    }.  }..  r
2cf40 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 23 69 66 64  eturn 0;.}..#ifd
2cf41 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
2cf42 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
2cf43 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 61  ion is used to a
2cf44 64 64 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 29  dd VdbeComment()
2cf45 20 61 6e 6e 6f 74 61 74 69 6f 6e 73 20 74 6f 20   annotations to 
2cf46 61 20 56 44 42 45 0a 2a 2a 20 70 72 6f 67 72 61  a VDBE.** progra
2cf47 6d 2e 20 49 74 20 69 73 20 6e 6f 74 20 75 73 65  m. It is not use
2cf48 64 20 69 6e 20 70 72 6f 64 75 63 74 69 6f 6e 20  d in production 
2cf49 63 6f 64 65 2c 20 6f 6e 6c 79 20 66 6f 72 20 64  code, only for d
2cf4a 65 62 75 67 67 69 6e 67 2e 0a 2a 2f 0a 73 74 61  ebugging..*/.sta
2cf4b 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
2cf4c 6f 6e 45 72 72 6f 72 54 65 78 74 28 69 6e 74 20  onErrorText(int 
2cf4d 6f 6e 45 72 72 6f 72 29 7b 0a 20 20 73 77 69 74  onError){.  swit
2cf4e 63 68 28 20 6f 6e 45 72 72 6f 72 20 29 7b 0a 20  ch( onError ){. 
2cf4f 20 20 20 63 61 73 65 20 4f 45 5f 41 62 6f 72 74     case OE_Abort
2cf50 3a 20 20 20 20 72 65 74 75 72 6e 20 22 61 62 6f  :    return "abo
2cf51 72 74 22 3b 0a 20 20 20 20 63 61 73 65 20 4f 45  rt";.    case OE
2cf52 5f 52 6f 6c 6c 62 61 63 6b 3a 20 72 65 74 75 72  _Rollback: retur
2cf53 6e 20 22 72 6f 6c 6c 62 61 63 6b 22 3b 0a 20 20  n "rollback";.  
2cf54 20 20 63 61 73 65 20 4f 45 5f 46 61 69 6c 3a 20    case OE_Fail: 
2cf55 20 20 20 20 72 65 74 75 72 6e 20 22 66 61 69 6c      return "fail
2cf56 22 3b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 52  ";.    case OE_R
2cf57 65 70 6c 61 63 65 3a 20 20 72 65 74 75 72 6e 20  eplace:  return 
2cf58 22 72 65 70 6c 61 63 65 22 3b 0a 20 20 20 20 63  "replace";.    c
2cf59 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65 3a 20 20  ase OE_Ignore:  
2cf5a 20 72 65 74 75 72 6e 20 22 69 67 6e 6f 72 65 22   return "ignore"
2cf5b 3b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 44 65  ;.    case OE_De
2cf5c 66 61 75 6c 74 3a 20 20 72 65 74 75 72 6e 20 22  fault:  return "
2cf5d 64 65 66 61 75 6c 74 22 3b 0a 20 20 7d 0a 20 20  default";.  }.  
2cf5e 72 65 74 75 72 6e 20 22 6e 2f 61 22 3b 0a 7d 0a  return "n/a";.}.
2cf5f 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 50 61  #endif../*.** Pa
2cf60 72 73 65 20 63 6f 6e 74 65 78 74 20 73 74 72 75  rse context stru
2cf61 63 74 75 72 65 20 70 46 72 6f 6d 20 68 61 73 20  cture pFrom has 
2cf62 6a 75 73 74 20 62 65 65 6e 20 75 73 65 64 20 74  just been used t
2cf63 6f 20 63 72 65 61 74 65 20 61 20 73 75 62 2d 76  o create a sub-v
2cf64 64 62 65 0a 2a 2a 20 28 74 72 69 67 67 65 72 20  dbe.** (trigger 
2cf65 70 72 6f 67 72 61 6d 29 2e 20 49 66 20 61 6e 20  program). If an 
2cf66 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72  error has occurr
2cf67 65 64 2c 20 74 72 61 6e 73 66 65 72 20 65 72 72  ed, transfer err
2cf68 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a  or information.*
2cf69 2a 20 66 72 6f 6d 20 70 46 72 6f 6d 20 74 6f 20  * from pFrom to 
2cf6a 70 54 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  pTo..*/.static v
2cf6b 6f 69 64 20 74 72 61 6e 73 66 65 72 50 61 72 73  oid transferPars
2cf6c 65 45 72 72 6f 72 28 50 61 72 73 65 20 2a 70 54  eError(Parse *pT
2cf6d 6f 2c 20 50 61 72 73 65 20 2a 70 46 72 6f 6d 29  o, Parse *pFrom)
2cf6e 7b 0a 20 20 61 73 73 65 72 74 28 20 70 46 72 6f  {.  assert( pFro
2cf6f 6d 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30 20 7c 7c  m->zErrMsg==0 ||
2cf70 20 70 46 72 6f 6d 2d 3e 6e 45 72 72 20 29 3b 0a   pFrom->nErr );.
2cf71 20 20 61 73 73 65 72 74 28 20 70 54 6f 2d 3e 7a    assert( pTo->z
2cf72 45 72 72 4d 73 67 3d 3d 30 20 7c 7c 20 70 54 6f  ErrMsg==0 || pTo
2cf73 2d 3e 6e 45 72 72 20 29 3b 0a 20 20 69 66 28 20  ->nErr );.  if( 
2cf74 70 54 6f 2d 3e 6e 45 72 72 3d 3d 30 20 29 7b 0a  pTo->nErr==0 ){.
2cf75 20 20 20 20 70 54 6f 2d 3e 7a 45 72 72 4d 73 67      pTo->zErrMsg
2cf76 20 3d 20 70 46 72 6f 6d 2d 3e 7a 45 72 72 4d 73   = pFrom->zErrMs
2cf77 67 3b 0a 20 20 20 20 70 54 6f 2d 3e 6e 45 72 72  g;.    pTo->nErr
2cf78 20 3d 20 70 46 72 6f 6d 2d 3e 6e 45 72 72 3b 0a   = pFrom->nErr;.
2cf79 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
2cf7a 69 74 65 33 44 62 46 72 65 65 28 70 46 72 6f 6d  ite3DbFree(pFrom
2cf7b 2d 3e 64 62 2c 20 70 46 72 6f 6d 2d 3e 7a 45 72  ->db, pFrom->zEr
2cf7c 72 4d 73 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  rMsg);.  }.}../*
2cf7d 0a 2a 2a 20 43 72 65 61 74 65 20 61 6e 64 20 70  .** Create and p
2cf7e 6f 70 75 6c 61 74 65 20 61 20 6e 65 77 20 54 72  opulate a new Tr
2cf7f 69 67 67 65 72 50 72 67 20 6f 62 6a 65 63 74 20  iggerPrg object 
2cf80 77 69 74 68 20 61 20 73 75 62 2d 70 72 6f 67 72  with a sub-progr
2cf81 61 6d 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  am .** implement
2cf82 69 6e 67 20 74 72 69 67 67 65 72 20 70 54 72 69  ing trigger pTri
2cf83 67 67 65 72 20 77 69 74 68 20 4f 4e 20 43 4f 4e  gger with ON CON
2cf84 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 6f 72 63  FLICT policy orc
2cf85 6f 6e 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54  onf..*/.static T
2cf86 72 69 67 67 65 72 50 72 67 20 2a 63 6f 64 65 52  riggerPrg *codeR
2cf87 6f 77 54 72 69 67 67 65 72 28 0a 20 20 50 61 72  owTrigger(.  Par
2cf88 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
2cf89 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 70 61 72    /* Current par
2cf8a 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  se context */.  
2cf8b 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
2cf8c 72 2c 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20  r,   /* Trigger 
2cf8d 74 6f 20 63 6f 64 65 20 2a 2f 0a 20 20 54 61 62  to code */.  Tab
2cf8e 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20  le *pTab,       
2cf8f 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 70    /* The table p
2cf90 54 72 69 67 67 65 72 20 69 73 20 61 74 74 61 63  Trigger is attac
2cf91 68 65 64 20 74 6f 20 2a 2f 0a 20 20 69 6e 74 20  hed to */.  int 
2cf92 6f 72 63 6f 6e 66 20 20 20 20 20 20 20 20 20 20  orconf          
2cf93 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20   /* ON CONFLICT 
2cf94 70 6f 6c 69 63 79 20 74 6f 20 63 6f 64 65 20 74  policy to code t
2cf95 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 77  rigger program w
2cf96 69 74 68 20 2a 2f 0a 29 7b 0a 20 20 50 61 72 73  ith */.){.  Pars
2cf97 65 20 2a 70 54 6f 70 20 3d 20 73 71 6c 69 74 65  e *pTop = sqlite
2cf98 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c 28 70  3ParseToplevel(p
2cf99 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65  Parse);.  sqlite
2cf9a 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
2cf9b 64 62 3b 20 20 20 2f 2a 20 44 61 74 61 62 61 73  db;   /* Databas
2cf9c 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 54 72  e handle */.  Tr
2cf9d 69 67 67 65 72 50 72 67 20 2a 70 50 72 67 3b 20  iggerPrg *pPrg; 
2cf9e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c            /* Val
2cf9f 75 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a  ue to return */.
2cfa0 20 20 45 78 70 72 20 2a 70 57 68 65 6e 20 3d 20    Expr *pWhen = 
2cfa1 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  0;            /*
2cfa2 20 44 75 70 6c 69 63 61 74 65 20 6f 66 20 74 72   Duplicate of tr
2cfa3 69 67 67 65 72 20 57 48 45 4e 20 65 78 70 72 65  igger WHEN expre
2cfa4 73 73 69 6f 6e 20 2a 2f 0a 20 20 56 64 62 65 20  ssion */.  Vdbe 
2cfa5 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  *v;             
2cfa6 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 6f 72         /* Tempor
2cfa7 61 72 79 20 56 4d 20 2a 2f 0a 20 20 4e 61 6d 65  ary VM */.  Name
2cfa8 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20  Context sNC;    
2cfa9 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
2cfaa 63 6f 6e 74 65 78 74 20 66 6f 72 20 73 75 62 2d  context for sub-
2cfab 76 64 62 65 20 2a 2f 0a 20 20 53 75 62 50 72 6f  vdbe */.  SubPro
2cfac 67 72 61 6d 20 2a 70 50 72 6f 67 72 61 6d 20 3d  gram *pProgram =
2cfad 20 30 3b 20 20 20 2f 2a 20 53 75 62 2d 76 64 62   0;   /* Sub-vdb
2cfae 65 20 66 6f 72 20 74 72 69 67 67 65 72 20 70 72  e for trigger pr
2cfaf 6f 67 72 61 6d 20 2a 2f 0a 20 20 50 61 72 73 65  ogram */.  Parse
2cfb0 20 2a 70 53 75 62 50 61 72 73 65 3b 20 20 20 20   *pSubParse;    
2cfb1 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 20         /* Parse 
2cfb2 63 6f 6e 74 65 78 74 20 66 6f 72 20 73 75 62 2d  context for sub-
2cfb3 76 64 62 65 20 2a 2f 0a 20 20 69 6e 74 20 69 45  vdbe */.  int iE
2cfb4 6e 64 54 72 69 67 67 65 72 20 3d 20 30 3b 20 20  ndTrigger = 0;  
2cfb5 20 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c 20 74        /* Label t
2cfb6 6f 20 6a 75 6d 70 20 74 6f 20 69 66 20 57 48 45  o jump to if WHE
2cfb7 4e 20 69 73 20 66 61 6c 73 65 20 2a 2f 0a 0a 20  N is false */.. 
2cfb8 20 61 73 73 65 72 74 28 20 70 54 72 69 67 67 65   assert( pTrigge
2cfb9 72 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 7c 7c 20 70  r->zName==0 || p
2cfba 54 61 62 3d 3d 74 61 62 6c 65 4f 66 54 72 69 67  Tab==tableOfTrig
2cfbb 67 65 72 28 70 54 72 69 67 67 65 72 29 20 29 3b  ger(pTrigger) );
2cfbc 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
2cfbd 74 68 65 20 54 72 69 67 67 65 72 50 72 67 20 61  the TriggerPrg a
2cfbe 6e 64 20 53 75 62 50 72 6f 67 72 61 6d 20 6f 62  nd SubProgram ob
2cfbf 6a 65 63 74 73 2e 20 54 6f 20 65 6e 73 75 72 65  jects. To ensure
2cfc0 20 74 68 61 74 20 74 68 65 79 0a 20 20 2a 2a 20   that they.  ** 
2cfc1 61 72 65 20 66 72 65 65 64 20 69 66 20 61 6e 20  are freed if an 
2cfc2 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 69  error occurs, li
2cfc3 6e 6b 20 74 68 65 6d 20 69 6e 74 6f 20 74 68 65  nk them into the
2cfc4 20 50 61 72 73 65 2e 70 54 72 69 67 67 65 72 50   Parse.pTriggerP
2cfc5 72 67 20 0a 20 20 2a 2a 20 6c 69 73 74 20 6f 66  rg .  ** list of
2cfc6 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c 20 50   the top-level P
2cfc7 61 72 73 65 20 6f 62 6a 65 63 74 20 73 6f 6f 6e  arse object soon
2cfc8 65 72 20 72 61 74 68 65 72 20 74 68 61 6e 20 6c  er rather than l
2cfc9 61 74 65 72 2e 20 20 2a 2f 0a 20 20 70 50 72 67  ater.  */.  pPrg
2cfca 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
2cfcb 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f  ocZero(db, sizeo
2cfcc 66 28 54 72 69 67 67 65 72 50 72 67 29 29 3b 0a  f(TriggerPrg));.
2cfcd 20 20 69 66 28 20 21 70 50 72 67 20 29 20 72 65    if( !pPrg ) re
2cfce 74 75 72 6e 20 30 3b 0a 20 20 70 50 72 67 2d 3e  turn 0;.  pPrg->
2cfcf 70 4e 65 78 74 20 3d 20 70 54 6f 70 2d 3e 70 54  pNext = pTop->pT
2cfd0 72 69 67 67 65 72 50 72 67 3b 0a 20 20 70 54 6f  riggerPrg;.  pTo
2cfd1 70 2d 3e 70 54 72 69 67 67 65 72 50 72 67 20 3d  p->pTriggerPrg =
2cfd2 20 70 50 72 67 3b 0a 20 20 70 50 72 67 2d 3e 70   pPrg;.  pPrg->p
2cfd3 50 72 6f 67 72 61 6d 20 3d 20 70 50 72 6f 67 72  Program = pProgr
2cfd4 61 6d 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61  am = sqlite3DbMa
2cfd5 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a  llocZero(db, siz
2cfd6 65 6f 66 28 53 75 62 50 72 6f 67 72 61 6d 29 29  eof(SubProgram))
2cfd7 3b 0a 20 20 69 66 28 20 21 70 50 72 6f 67 72 61  ;.  if( !pProgra
2cfd8 6d 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  m ) return 0;.  
2cfd9 70 50 72 6f 67 72 61 6d 2d 3e 6e 52 65 66 20 3d  pProgram->nRef =
2cfda 20 31 3b 0a 20 20 70 50 72 67 2d 3e 70 54 72 69   1;.  pPrg->pTri
2cfdb 67 67 65 72 20 3d 20 70 54 72 69 67 67 65 72 3b  gger = pTrigger;
2cfdc 0a 20 20 70 50 72 67 2d 3e 6f 72 63 6f 6e 66 20  .  pPrg->orconf 
2cfdd 3d 20 6f 72 63 6f 6e 66 3b 0a 20 20 70 50 72 67  = orconf;.  pPrg
2cfde 2d 3e 6f 6c 64 6d 61 73 6b 20 3d 20 30 78 66 66  ->oldmask = 0xff
2cfdf 66 66 66 66 66 66 3b 0a 0a 20 20 2f 2a 20 41 6c  ffffff;..  /* Al
2cfe0 6c 6f 63 61 74 65 20 61 6e 64 20 70 6f 70 75 6c  locate and popul
2cfe1 61 74 65 20 61 20 6e 65 77 20 50 61 72 73 65 20  ate a new Parse 
2cfe2 63 6f 6e 74 65 78 74 20 74 6f 20 75 73 65 20 66  context to use f
2cfe3 6f 72 20 63 6f 64 69 6e 67 20 74 68 65 20 0a 20  or coding the . 
2cfe4 20 2a 2a 20 74 72 69 67 67 65 72 20 73 75 62 2d   ** trigger sub-
2cfe5 70 72 6f 67 72 61 6d 2e 20 20 2a 2f 0a 20 20 70  program.  */.  p
2cfe6 53 75 62 50 61 72 73 65 20 3d 20 73 71 6c 69 74  SubParse = sqlit
2cfe7 65 33 53 74 61 63 6b 41 6c 6c 6f 63 5a 65 72 6f  e3StackAllocZero
2cfe8 28 64 62 2c 20 73 69 7a 65 6f 66 28 50 61 72 73  (db, sizeof(Pars
2cfe9 65 29 29 3b 0a 20 20 69 66 28 20 21 70 53 75 62  e));.  if( !pSub
2cfea 50 61 72 73 65 20 29 20 72 65 74 75 72 6e 20 30  Parse ) return 0
2cfeb 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c  ;.  memset(&sNC,
2cfec 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29   0, sizeof(sNC))
2cfed 3b 0a 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d  ;.  sNC.pParse =
2cfee 20 70 53 75 62 50 61 72 73 65 3b 0a 20 20 70 53   pSubParse;.  pS
2cfef 75 62 50 61 72 73 65 2d 3e 64 62 20 3d 20 64 62  ubParse->db = db
2cff0 3b 0a 20 20 70 53 75 62 50 61 72 73 65 2d 3e 70  ;.  pSubParse->p
2cff1 54 72 69 67 67 65 72 54 61 62 20 3d 20 70 54 61  TriggerTab = pTa
2cff2 62 3b 0a 20 20 70 53 75 62 50 61 72 73 65 2d 3e  b;.  pSubParse->
2cff3 70 54 6f 70 6c 65 76 65 6c 20 3d 20 70 54 6f 70  pToplevel = pTop
2cff4 3b 0a 20 20 70 53 75 62 50 61 72 73 65 2d 3e 7a  ;.  pSubParse->z
2cff5 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 54  AuthContext = pT
2cff6 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65 3b 0a 20  rigger->zName;. 
2cff7 20 70 53 75 62 50 61 72 73 65 2d 3e 65 54 72 69   pSubParse->eTri
2cff8 67 67 65 72 4f 70 20 3d 20 70 54 72 69 67 67 65  ggerOp = pTrigge
2cff9 72 2d 3e 6f 70 3b 0a 0a 20 20 76 20 3d 20 73 71  r->op;..  v = sq
2cffa 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 53 75  lite3GetVdbe(pSu
2cffb 62 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  bParse);.  if( v
2cffc 20 29 7b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d   ){.    VdbeComm
2cffd 65 6e 74 28 28 76 2c 20 22 53 74 61 72 74 3a 20  ent((v, "Start: 
2cffe 25 73 2e 25 73 20 28 25 73 20 25 73 25 73 25 73  %s.%s (%s %s%s%s
2cfff 20 4f 4e 20 25 73 29 22 2c 20 0a 20 20 20 20 20   ON %s)", .     
2d000 20 70 54 72 69 67 67 65 72 2d 3e 7a 4e 61 6d 65   pTrigger->zName
2d001 2c 20 6f 6e 45 72 72 6f 72 54 65 78 74 28 6f 72  , onErrorText(or
2d002 63 6f 6e 66 29 2c 0a 20 20 20 20 20 20 28 70 54  conf),.      (pT
2d003 72 69 67 67 65 72 2d 3e 74 72 5f 74 6d 3d 3d 54  rigger->tr_tm==T
2d004 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 3f 20  RIGGER_BEFORE ? 
2d005 22 42 45 46 4f 52 45 22 20 3a 20 22 41 46 54 45  "BEFORE" : "AFTE
2d006 52 22 29 2c 0a 20 20 20 20 20 20 20 20 28 70 54  R"),.        (pT
2d007 72 69 67 67 65 72 2d 3e 6f 70 3d 3d 54 4b 5f 55  rigger->op==TK_U
2d008 50 44 41 54 45 20 3f 20 22 55 50 44 41 54 45 22  PDATE ? "UPDATE"
2d009 20 3a 20 22 22 29 2c 0a 20 20 20 20 20 20 20 20   : ""),.        
2d00a 28 70 54 72 69 67 67 65 72 2d 3e 6f 70 3d 3d 54  (pTrigger->op==T
2d00b 4b 5f 49 4e 53 45 52 54 20 3f 20 22 49 4e 53 45  K_INSERT ? "INSE
2d00c 52 54 22 20 3a 20 22 22 29 2c 0a 20 20 20 20 20  RT" : ""),.     
2d00d 20 20 20 28 70 54 72 69 67 67 65 72 2d 3e 6f 70     (pTrigger->op
2d00e 3d 3d 54 4b 5f 44 45 4c 45 54 45 20 3f 20 22 44  ==TK_DELETE ? "D
2d00f 45 4c 45 54 45 22 20 3a 20 22 22 29 2c 0a 20 20  ELETE" : ""),.  
2d010 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 0a      pTab->zName.
2d011 20 20 20 20 29 29 3b 0a 23 69 66 6e 64 65 66 20      ));.#ifndef 
2d012 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
2d013 45 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  E.    sqlite3Vdb
2d014 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
2d015 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4d   .      sqlite3M
2d016 50 72 69 6e 74 66 28 64 62 2c 20 22 2d 2d 20 54  Printf(db, "-- T
2d017 52 49 47 47 45 52 20 25 73 22 2c 20 70 54 72 69  RIGGER %s", pTri
2d018 67 67 65 72 2d 3e 7a 4e 61 6d 65 29 2c 20 50 34  gger->zName), P4
2d019 5f 44 59 4e 41 4d 49 43 0a 20 20 20 20 29 3b 0a  _DYNAMIC.    );.
2d01a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 49  #endif..    /* I
2d01b 66 20 6f 6e 65 20 77 61 73 20 73 70 65 63 69 66  f one was specif
2d01c 69 65 64 2c 20 63 6f 64 65 20 74 68 65 20 57 48  ied, code the WH
2d01d 45 4e 20 63 6c 61 75 73 65 2e 20 49 66 20 69 74  EN clause. If it
2d01e 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20 66 61   evaluates to fa
2d01f 6c 73 65 0a 20 20 20 20 2a 2a 20 28 6f 72 20 4e  lse.    ** (or N
2d020 55 4c 4c 29 20 74 68 65 20 73 75 62 2d 76 64 62  ULL) the sub-vdb
2d021 65 20 69 73 20 69 6d 6d 65 64 69 61 74 65 6c 79  e is immediately
2d022 20 68 61 6c 74 65 64 20 62 79 20 6a 75 6d 70 69   halted by jumpi
2d023 6e 67 20 74 6f 20 74 68 65 20 0a 20 20 20 20 2a  ng to the .    *
2d024 2a 20 4f 50 5f 48 61 6c 74 20 69 6e 73 65 72 74  * OP_Halt insert
2d025 65 64 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  ed at the end of
2d026 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 20 20 2a   the program.  *
2d027 2f 0a 20 20 20 20 69 66 28 20 70 54 72 69 67 67  /.    if( pTrigg
2d028 65 72 2d 3e 70 57 68 65 6e 20 29 7b 0a 20 20 20  er->pWhen ){.   
2d029 20 20 20 70 57 68 65 6e 20 3d 20 73 71 6c 69 74     pWhen = sqlit
2d02a 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 54  e3ExprDup(db, pT
2d02b 72 69 67 67 65 72 2d 3e 70 57 68 65 6e 2c 20 30  rigger->pWhen, 0
2d02c 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51 4c  );.      if( SQL
2d02d 49 54 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 52  ITE_OK==sqlite3R
2d02e 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28  esolveExprNames(
2d02f 26 73 4e 43 2c 20 70 57 68 65 6e 29 20 0a 20 20  &sNC, pWhen) .  
2d030 20 20 20 20 20 26 26 20 64 62 2d 3e 6d 61 6c 6c       && db->mall
2d031 6f 63 46 61 69 6c 65 64 3d 3d 30 20 0a 20 20 20  ocFailed==0 .   
2d032 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 69 45     ){.        iE
2d033 6e 64 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69  ndTrigger = sqli
2d034 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
2d035 28 76 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  (v);.        sql
2d036 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
2d037 70 53 75 62 50 61 72 73 65 2c 20 70 57 68 65 6e  pSubParse, pWhen
2d038 2c 20 69 45 6e 64 54 72 69 67 67 65 72 2c 20 53  , iEndTrigger, S
2d039 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
2d03a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2d03b 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2d03c 74 65 28 64 62 2c 20 70 57 68 65 6e 29 3b 0a 20  te(db, pWhen);. 
2d03d 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 64     }..    /* Cod
2d03e 65 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72  e the trigger pr
2d03f 6f 67 72 61 6d 20 69 6e 74 6f 20 74 68 65 20 73  ogram into the s
2d040 75 62 2d 76 64 62 65 2e 20 2a 2f 0a 20 20 20 20  ub-vdbe. */.    
2d041 63 6f 64 65 54 72 69 67 67 65 72 50 72 6f 67 72  codeTriggerProgr
2d042 61 6d 28 70 53 75 62 50 61 72 73 65 2c 20 70 54  am(pSubParse, pT
2d043 72 69 67 67 65 72 2d 3e 73 74 65 70 5f 6c 69 73  rigger->step_lis
2d044 74 2c 20 6f 72 63 6f 6e 66 29 3b 0a 0a 20 20 20  t, orconf);..   
2d045 20 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20 4f 50   /* Insert an OP
2d046 5f 48 61 6c 74 20 61 74 20 74 68 65 20 65 6e 64  _Halt at the end
2d047 20 6f 66 20 74 68 65 20 73 75 62 2d 70 72 6f 67   of the sub-prog
2d048 72 61 6d 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ram. */.    if( 
2d049 69 45 6e 64 54 72 69 67 67 65 72 20 29 7b 0a 20  iEndTrigger ){. 
2d04a 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2d04b 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
2d04c 69 45 6e 64 54 72 69 67 67 65 72 29 3b 0a 20 20  iEndTrigger);.  
2d04d 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
2d04e 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f  dbeAddOp0(v, OP_
2d04f 48 61 6c 74 29 3b 0a 20 20 20 20 56 64 62 65 43  Halt);.    VdbeC
2d050 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 45 6e 64 3a  omment((v, "End:
2d051 20 25 73 2e 25 73 22 2c 20 70 54 72 69 67 67 65   %s.%s", pTrigge
2d052 72 2d 3e 7a 4e 61 6d 65 2c 20 6f 6e 45 72 72 6f  r->zName, onErro
2d053 72 54 65 78 74 28 6f 72 63 6f 6e 66 29 29 29 3b  rText(orconf)));
2d054 0a 0a 20 20 20 20 74 72 61 6e 73 66 65 72 50 61  ..    transferPa
2d055 72 73 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c  rseError(pParse,
2d056 20 70 53 75 62 50 61 72 73 65 29 3b 0a 20 20 20   pSubParse);.   
2d057 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
2d058 61 69 6c 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20  ailed==0 ){.    
2d059 20 20 70 50 72 6f 67 72 61 6d 2d 3e 61 4f 70 20    pProgram->aOp 
2d05a 3d 20 73 71 6c 69 74 65 33 56 64 62 65 54 61 6b  = sqlite3VdbeTak
2d05b 65 4f 70 41 72 72 61 79 28 76 2c 20 26 70 50 72  eOpArray(v, &pPr
2d05c 6f 67 72 61 6d 2d 3e 6e 4f 70 2c 20 26 70 54 6f  ogram->nOp, &pTo
2d05d 70 2d 3e 6e 4d 61 78 41 72 67 29 3b 0a 20 20 20  p->nMaxArg);.   
2d05e 20 7d 0a 20 20 20 20 70 50 72 6f 67 72 61 6d 2d   }.    pProgram-
2d05f 3e 6e 4d 65 6d 20 3d 20 70 53 75 62 50 61 72 73  >nMem = pSubPars
2d060 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 70 50 72  e->nMem;.    pPr
2d061 6f 67 72 61 6d 2d 3e 6e 43 73 72 20 3d 20 70 53  ogram->nCsr = pS
2d062 75 62 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20  ubParse->nTab;. 
2d063 20 20 20 70 50 72 6f 67 72 61 6d 2d 3e 74 6f 6b     pProgram->tok
2d064 65 6e 20 3d 20 28 76 6f 69 64 20 2a 29 70 54 72  en = (void *)pTr
2d065 69 67 67 65 72 3b 0a 20 20 20 20 70 50 72 67 2d  igger;.    pPrg-
2d066 3e 6f 6c 64 6d 61 73 6b 20 3d 20 70 53 75 62 50  >oldmask = pSubP
2d067 61 72 73 65 2d 3e 6f 6c 64 6d 61 73 6b 3b 0a 20  arse->oldmask;. 
2d068 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65     sqlite3VdbeDe
2d069 6c 65 74 65 28 76 29 3b 0a 20 20 7d 0a 0a 20 20  lete(v);.  }..  
2d06a 61 73 73 65 72 74 28 20 21 70 53 75 62 50 61 72  assert( !pSubPar
2d06b 73 65 2d 3e 70 41 69 6e 63 20 20 20 20 20 20 20  se->pAinc       
2d06c 26 26 20 21 70 53 75 62 50 61 72 73 65 2d 3e 70  && !pSubParse->p
2d06d 5a 6f 6d 62 69 65 54 61 62 20 29 3b 0a 20 20 61  ZombieTab );.  a
2d06e 73 73 65 72 74 28 20 21 70 53 75 62 50 61 72 73  ssert( !pSubPars
2d06f 65 2d 3e 70 54 72 69 67 67 65 72 50 72 67 20 26  e->pTriggerPrg &
2d070 26 20 21 70 53 75 62 50 61 72 73 65 2d 3e 6e 4d  & !pSubParse->nM
2d071 61 78 41 72 67 20 29 3b 0a 20 20 73 71 6c 69 74  axArg );.  sqlit
2d072 65 33 53 74 61 63 6b 46 72 65 65 28 64 62 2c 20  e3StackFree(db, 
2d073 70 53 75 62 50 61 72 73 65 29 3b 0a 0a 20 20 72  pSubParse);..  r
2d074 65 74 75 72 6e 20 70 50 72 67 3b 0a 7d 0a 20 20  eturn pPrg;.}.  
2d075 20 20 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20    ./*.** Return 
2d076 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 54  a pointer to a T
2d077 72 69 67 67 65 72 50 72 67 20 6f 62 6a 65 63 74  riggerPrg object
2d078 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
2d079 73 75 62 2d 70 72 6f 67 72 61 6d 20 66 6f 72 0a  sub-program for.
2d07a 2a 2a 20 74 72 69 67 67 65 72 20 70 54 72 69 67  ** trigger pTrig
2d07b 67 65 72 20 77 69 74 68 20 64 65 66 61 75 6c 74  ger with default
2d07c 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67   ON CONFLICT alg
2d07d 6f 72 69 74 68 6d 20 6f 72 63 6f 6e 66 2e 20 49  orithm orconf. I
2d07e 66 20 6e 6f 20 73 75 63 68 0a 2a 2a 20 54 72 69  f no such.** Tri
2d07f 67 67 65 72 50 72 67 20 6f 62 6a 65 63 74 20 65  ggerPrg object e
2d080 78 69 73 74 73 2c 20 61 20 6e 65 77 20 6f 62 6a  xists, a new obj
2d081 65 63 74 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  ect is allocated
2d082 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 64 20 62   and populated b
2d083 65 66 6f 72 65 0a 2a 2a 20 62 65 69 6e 67 20 72  efore.** being r
2d084 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74  eturned..*/.stat
2d085 69 63 20 54 72 69 67 67 65 72 50 72 67 20 2a 67  ic TriggerPrg *g
2d086 65 74 52 6f 77 54 72 69 67 67 65 72 28 0a 20 20  etRowTrigger(.  
2d087 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2d088 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
2d089 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f  parse context */
2d08a 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72 69  .  Trigger *pTri
2d08b 67 67 65 72 2c 20 20 20 2f 2a 20 54 72 69 67 67  gger,   /* Trigg
2d08c 65 72 20 74 6f 20 63 6f 64 65 20 2a 2f 0a 20 20  er to code */.  
2d08d 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20  Table *pTab,    
2d08e 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c       /* The tabl
2d08f 65 20 74 72 69 67 67 65 72 20 70 54 72 69 67 67  e trigger pTrigg
2d090 65 72 20 69 73 20 61 74 74 61 63 68 65 64 20 74  er is attached t
2d091 6f 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e  o */.  int orcon
2d092 66 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  f           /* O
2d093 4e 20 43 4f 4e 46 4c 49 43 54 20 61 6c 67 6f 72  N CONFLICT algor
2d094 69 74 68 6d 2e 20 2a 2f 0a 29 7b 0a 20 20 50 61  ithm. */.){.  Pa
2d095 72 73 65 20 2a 70 52 6f 6f 74 20 3d 20 73 71 6c  rse *pRoot = sql
2d096 69 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65  ite3ParseTopleve
2d097 6c 28 70 50 61 72 73 65 29 3b 0a 20 20 54 72 69  l(pParse);.  Tri
2d098 67 67 65 72 50 72 67 20 2a 70 50 72 67 3b 0a 0a  ggerPrg *pPrg;..
2d099 20 20 61 73 73 65 72 74 28 20 70 54 72 69 67 67    assert( pTrigg
2d09a 65 72 2d 3e 7a 4e 61 6d 65 3d 3d 30 20 7c 7c 20  er->zName==0 || 
2d09b 70 54 61 62 3d 3d 74 61 62 6c 65 4f 66 54 72 69  pTab==tableOfTri
2d09c 67 67 65 72 28 70 54 72 69 67 67 65 72 29 20 29  gger(pTrigger) )
2d09d 3b 0a 0a 20 20 2f 2a 20 49 74 20 6d 61 79 20 62  ;..  /* It may b
2d09e 65 20 74 68 61 74 20 74 68 69 73 20 74 72 69 67  e that this trig
2d09f 67 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20  ger has already 
2d0a0 62 65 65 6e 20 63 6f 64 65 64 20 28 6f 72 20 69  been coded (or i
2d0a1 73 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 70 72  s in the.  ** pr
2d0a2 6f 63 65 73 73 20 6f 66 20 62 65 69 6e 67 20 63  ocess of being c
2d0a3 6f 64 65 64 29 2e 20 49 66 20 74 68 69 73 20 69  oded). If this i
2d0a4 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e  s the case, then
2d0a5 20 61 6e 20 65 6e 74 72 79 20 77 69 74 68 0a 20   an entry with. 
2d0a6 20 2a 2a 20 61 20 6d 61 74 63 68 69 6e 67 20 54   ** a matching T
2d0a7 72 69 67 67 65 72 50 72 67 2e 70 54 72 69 67 67  riggerPrg.pTrigg
2d0a8 65 72 20 66 69 65 6c 64 20 77 69 6c 6c 20 62 65  er field will be
2d0a9 20 70 72 65 73 65 6e 74 20 73 6f 6d 65 77 68 65   present somewhe
2d0aa 72 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 50  re.  ** in the P
2d0ab 61 72 73 65 2e 70 54 72 69 67 67 65 72 50 72 67  arse.pTriggerPrg
2d0ac 20 6c 69 73 74 2e 20 53 65 61 72 63 68 20 66 6f   list. Search fo
2d0ad 72 20 73 75 63 68 20 61 6e 20 65 6e 74 72 79 2e  r such an entry.
2d0ae 20 20 2a 2f 0a 20 20 66 6f 72 28 70 50 72 67 3d    */.  for(pPrg=
2d0af 70 52 6f 6f 74 2d 3e 70 54 72 69 67 67 65 72 50  pRoot->pTriggerP
2d0b0 72 67 3b 20 0a 20 20 20 20 20 20 70 50 72 67 20  rg; .      pPrg 
2d0b1 26 26 20 28 70 50 72 67 2d 3e 70 54 72 69 67 67  && (pPrg->pTrigg
2d0b2 65 72 21 3d 70 54 72 69 67 67 65 72 20 7c 7c 20  er!=pTrigger || 
2d0b3 70 50 72 67 2d 3e 6f 72 63 6f 6e 66 21 3d 6f 72  pPrg->orconf!=or
2d0b4 63 6f 6e 66 29 3b 20 0a 20 20 20 20 20 20 70 50  conf); .      pP
2d0b5 72 67 3d 70 50 72 67 2d 3e 70 4e 65 78 74 0a 20  rg=pPrg->pNext. 
2d0b6 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20   );..  /* If an 
2d0b7 65 78 69 73 74 69 6e 67 20 54 72 69 67 67 65 72  existing Trigger
2d0b8 50 72 67 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65  Prg could not be
2d0b9 20 6c 6f 63 61 74 65 64 2c 20 63 72 65 61 74 65   located, create
2d0ba 20 61 20 6e 65 77 20 6f 6e 65 2e 20 2a 2f 0a 20   a new one. */. 
2d0bb 20 69 66 28 20 21 70 50 72 67 20 29 7b 0a 20 20   if( !pPrg ){.  
2d0bc 20 20 70 50 72 67 20 3d 20 63 6f 64 65 52 6f 77    pPrg = codeRow
2d0bd 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20  Trigger(pParse, 
2d0be 70 54 72 69 67 67 65 72 2c 20 70 54 61 62 2c 20  pTrigger, pTab, 
2d0bf 6f 72 63 6f 6e 66 29 3b 0a 20 20 7d 0a 0a 20 20  orconf);.  }..  
2d0c0 72 65 74 75 72 6e 20 70 50 72 67 3b 0a 7d 0a 0a  return pPrg;.}..
2d0c1 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
2d0c2 6f 64 65 20 66 6f 72 20 74 68 65 20 74 72 69 67  ode for the trig
2d0c3 67 65 72 20 70 72 6f 67 72 61 6d 20 61 73 73 6f  ger program asso
2d0c4 63 69 61 74 65 64 20 77 69 74 68 20 74 72 69 67  ciated with trig
2d0c5 67 65 72 20 70 20 6f 6e 20 0a 2a 2a 20 74 61 62  ger p on .** tab
2d0c6 6c 65 20 70 54 61 62 2e 20 54 68 65 20 72 65 67  le pTab. The reg
2d0c7 2c 20 6f 72 63 6f 6e 66 20 61 6e 64 20 69 67 6e  , orconf and ign
2d0c8 6f 72 65 4a 75 6d 70 20 70 61 72 61 6d 65 74 65  oreJump paramete
2d0c9 72 73 20 70 61 73 73 65 64 20 74 6f 20 74 68 69  rs passed to thi
2d0ca 73 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 72  s.** function ar
2d0cb 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68  e the same as th
2d0cc 6f 73 65 20 64 65 73 63 72 69 62 65 64 20 69 6e  ose described in
2d0cd 20 74 68 65 20 68 65 61 64 65 72 20 66 75 6e 63   the header func
2d0ce 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 73 71 6c 69  tion for.** sqli
2d0cf 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65  te3CodeRowTrigge
2d0d0 72 28 29 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  r().*/.SQLITE_PR
2d0d1 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2d0d2 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72  e3CodeRowTrigger
2d0d3 44 69 72 65 63 74 28 0a 20 20 50 61 72 73 65 20  Direct(.  Parse 
2d0d4 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f  *pParse,       /
2d0d5 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20  * Parse context 
2d0d6 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 2c  */.  Trigger *p,
2d0d7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 69            /* Tri
2d0d8 67 67 65 72 20 74 6f 20 63 6f 64 65 20 2a 2f 0a  gger to code */.
2d0d9 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
2d0da 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61         /* The ta
2d0db 62 6c 65 20 74 6f 20 63 6f 64 65 20 74 72 69 67  ble to code trig
2d0dc 67 65 72 73 20 66 72 6f 6d 20 2a 2f 0a 20 20 69  gers from */.  i
2d0dd 6e 74 20 72 65 67 2c 20 20 20 20 20 20 20 20 20  nt reg,         
2d0de 20 20 20 20 2f 2a 20 52 65 67 20 61 72 72 61 79      /* Reg array
2d0df 20 63 6f 6e 74 61 69 6e 69 6e 67 20 4f 4c 44 2e   containing OLD.
2d0e0 2a 20 61 6e 64 20 4e 45 57 2e 2a 20 76 61 6c 75  * and NEW.* valu
2d0e1 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f  es */.  int orco
2d0e2 6e 66 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  nf,          /* 
2d0e3 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f 6c 69  ON CONFLICT poli
2d0e4 63 79 20 2a 2f 0a 20 20 69 6e 74 20 69 67 6e 6f  cy */.  int igno
2d0e5 72 65 4a 75 6d 70 20 20 20 20 20 20 20 2f 2a 20  reJump       /* 
2d0e6 49 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 6a  Instruction to j
2d0e7 75 6d 70 20 74 6f 20 66 6f 72 20 52 41 49 53 45  ump to for RAISE
2d0e8 28 49 47 4e 4f 52 45 29 20 2a 2f 0a 29 7b 0a 20  (IGNORE) */.){. 
2d0e9 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74   Vdbe *v = sqlit
2d0ea 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
2d0eb 29 3b 20 2f 2a 20 4d 61 69 6e 20 56 4d 20 2a 2f  ); /* Main VM */
2d0ec 0a 20 20 54 72 69 67 67 65 72 50 72 67 20 2a 70  .  TriggerPrg *p
2d0ed 50 72 67 3b 0a 20 20 70 50 72 67 20 3d 20 67 65  Prg;.  pPrg = ge
2d0ee 74 52 6f 77 54 72 69 67 67 65 72 28 70 50 61 72  tRowTrigger(pPar
2d0ef 73 65 2c 20 70 2c 20 70 54 61 62 2c 20 6f 72 63  se, p, pTab, orc
2d0f0 6f 6e 66 29 3b 0a 20 20 61 73 73 65 72 74 28 20  onf);.  assert( 
2d0f1 70 50 72 67 20 7c 7c 20 70 50 61 72 73 65 2d 3e  pPrg || pParse->
2d0f2 6e 45 72 72 20 7c 7c 20 70 50 61 72 73 65 2d 3e  nErr || pParse->
2d0f3 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2d0f4 20 29 3b 0a 0a 20 20 2f 2a 20 43 6f 64 65 20 74   );..  /* Code t
2d0f5 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 6f 70  he OP_Program op
2d0f6 63 6f 64 65 20 69 6e 20 74 68 65 20 70 61 72 65  code in the pare
2d0f7 6e 74 20 56 44 42 45 2e 20 50 34 20 6f 66 20 74  nt VDBE. P4 of t
2d0f8 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 0a 20  he OP_Program . 
2d0f9 20 2a 2a 20 69 73 20 61 20 70 6f 69 6e 74 65 72   ** is a pointer
2d0fa 20 74 6f 20 74 68 65 20 73 75 62 2d 76 64 62 65   to the sub-vdbe
2d0fb 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
2d0fc 74 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 2e  trigger program.
2d0fd 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 72 67 20    */.  if( pPrg 
2d0fe 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
2d0ff 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 50  beAddOp3(v, OP_P
2d100 72 6f 67 72 61 6d 2c 20 72 65 67 2c 20 69 67 6e  rogram, reg, ign
2d101 6f 72 65 4a 75 6d 70 2c 20 2b 2b 70 50 61 72 73  oreJump, ++pPars
2d102 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 70 50  e->nMem);.    pP
2d103 72 67 2d 3e 70 50 72 6f 67 72 61 6d 2d 3e 6e 52  rg->pProgram->nR
2d104 65 66 2b 2b 3b 0a 20 20 20 20 73 71 6c 69 74 65  ef++;.    sqlite
2d105 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
2d106 20 2d 31 2c 20 28 63 6f 6e 73 74 20 63 68 61 72   -1, (const char
2d107 20 2a 29 70 50 72 67 2d 3e 70 50 72 6f 67 72 61   *)pPrg->pProgra
2d108 6d 2c 20 50 34 5f 53 55 42 50 52 4f 47 52 41 4d  m, P4_SUBPROGRAM
2d109 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  );.    VdbeComme
2d10a 6e 74 28 0a 20 20 20 20 20 20 20 20 28 76 2c 20  nt(.        (v, 
2d10b 22 43 61 6c 6c 3a 20 25 73 2e 25 73 22 2c 20 28  "Call: %s.%s", (
2d10c 70 2d 3e 7a 4e 61 6d 65 3f 70 2d 3e 7a 4e 61 6d  p->zName?p->zNam
2d10d 65 3a 22 66 6b 65 79 22 29 2c 20 6f 6e 45 72 72  e:"fkey"), onErr
2d10e 6f 72 54 65 78 74 28 6f 72 63 6f 6e 66 29 29 29  orText(orconf)))
2d10f 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68  ;..    /* Set th
2d110 65 20 50 35 20 6f 70 65 72 61 6e 64 20 6f 66 20  e P5 operand of 
2d111 74 68 65 20 4f 50 5f 50 72 6f 67 72 61 6d 20 69  the OP_Program i
2d112 6e 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 6e 6f  nstruction to no
2d113 6e 2d 7a 65 72 6f 20 69 66 0a 20 20 20 20 2a 2a  n-zero if.    **
2d114 20 72 65 63 75 72 73 69 76 65 20 69 6e 76 6f 63   recursive invoc
2d115 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 74 72  ation of this tr
2d116 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 69 73  igger program is
2d117 20 64 69 73 61 6c 6c 6f 77 65 64 2e 20 52 65 63   disallowed. Rec
2d118 75 72 73 69 76 65 0a 20 20 20 20 2a 2a 20 69 6e  ursive.    ** in
2d119 76 6f 63 61 74 69 6f 6e 20 69 73 20 64 69 73 61  vocation is disa
2d11a 6c 6c 6f 77 65 64 20 69 66 20 28 61 29 20 74 68  llowed if (a) th
2d11b 65 20 73 75 62 2d 70 72 6f 67 72 61 6d 20 69 73  e sub-program is
2d11c 20 72 65 61 6c 6c 79 20 61 20 74 72 69 67 67 65   really a trigge
2d11d 72 2c 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 61 20  r,.    ** not a 
2d11e 66 6f 72 65 69 67 6e 20 6b 65 79 20 61 63 74 69  foreign key acti
2d11f 6f 6e 2c 20 61 6e 64 20 28 62 29 20 74 68 65 20  on, and (b) the 
2d120 66 6c 61 67 20 74 6f 20 65 6e 61 62 6c 65 20 72  flag to enable r
2d121 65 63 75 72 73 69 76 65 20 74 72 69 67 67 65 72  ecursive trigger
2d122 73 0a 20 20 20 20 2a 2a 20 69 73 20 63 6c 65 61  s.    ** is clea
2d123 72 2e 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  r.  */.    sqlit
2d124 65 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76  e3VdbeChangeP5(v
2d125 2c 20 28 75 38 29 28 70 2d 3e 7a 4e 61 6d 65 20  , (u8)(p->zName 
2d126 26 26 20 21 28 70 50 61 72 73 65 2d 3e 64 62 2d  && !(pParse->db-
2d127 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65  >flags&SQLITE_Re
2d128 63 54 72 69 67 67 65 72 73 29 29 29 3b 0a 20 20  cTriggers)));.  
2d129 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
2d12a 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 64  is called to cod
2d12b 65 20 74 68 65 20 72 65 71 75 69 72 65 64 20 46  e the required F
2d12c 4f 52 20 45 41 43 48 20 52 4f 57 20 74 72 69 67  OR EACH ROW trig
2d12d 67 65 72 73 20 66 6f 72 20 61 6e 20 6f 70 65 72  gers for an oper
2d12e 61 74 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 61 62 6c  ation.** on tabl
2d12f 65 20 70 54 61 62 2e 20 54 68 65 20 6f 70 65 72  e pTab. The oper
2d130 61 74 69 6f 6e 20 74 6f 20 63 6f 64 65 20 74 72  ation to code tr
2d131 69 67 67 65 72 73 20 66 6f 72 20 28 49 4e 53 45  iggers for (INSE
2d132 52 54 2c 20 55 50 44 41 54 45 20 6f 72 20 44 45  RT, UPDATE or DE
2d133 4c 45 54 45 29 0a 2a 2a 20 69 73 20 67 69 76 65  LETE).** is give
2d134 6e 20 62 79 20 74 68 65 20 6f 70 20 70 61 72 61  n by the op para
2d135 6d 61 74 65 72 2e 20 54 68 65 20 74 72 5f 74 6d  mater. The tr_tm
2d136 20 70 61 72 61 6d 65 74 65 72 20 64 65 74 65 72   parameter deter
2d137 6d 69 6e 65 73 20 77 68 65 74 68 65 72 20 74 68  mines whether th
2d138 65 0a 2a 2a 20 42 45 46 4f 52 45 20 6f 72 20 41  e.** BEFORE or A
2d139 46 54 45 52 20 74 72 69 67 67 65 72 73 20 61 72  FTER triggers ar
2d13a 65 20 63 6f 64 65 64 2e 20 49 66 20 74 68 65 20  e coded. If the 
2d13b 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 61 6e 20  operation is an 
2d13c 55 50 44 41 54 45 2c 20 74 68 65 6e 0a 2a 2a 20  UPDATE, then.** 
2d13d 70 61 72 61 6d 65 74 65 72 20 70 43 68 61 6e 67  parameter pChang
2d13e 65 73 20 69 73 20 70 61 73 73 65 64 20 74 68 65  es is passed the
2d13f 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73   list of columns
2d140 20 62 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2e   being modified.
2d141 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  .**.** If there 
2d142 61 72 65 20 6e 6f 20 74 72 69 67 67 65 72 73 20  are no triggers 
2d143 74 68 61 74 20 66 69 72 65 20 61 74 20 74 68 65  that fire at the
2d144 20 73 70 65 63 69 66 69 65 64 20 74 69 6d 65 20   specified time 
2d145 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65  for the specifie
2d146 64 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 6f  d.** operation o
2d147 6e 20 70 54 61 62 2c 20 74 68 69 73 20 66 75 6e  n pTab, this fun
2d148 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70  ction is a no-op
2d149 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 67 20  ..**.** The reg 
2d14a 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
2d14b 61 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 66  address of the f
2d14c 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79  irst in an array
2d14d 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 0a 2a   of registers .*
2d14e 2a 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 20 74  * that contain t
2d14f 68 65 20 76 61 6c 75 65 73 20 73 75 62 73 74 69  he values substi
2d150 74 75 74 65 64 20 66 6f 72 20 74 68 65 20 6e 65  tuted for the ne
2d151 77 2e 2a 20 61 6e 64 20 6f 6c 64 2e 2a 20 72 65  w.* and old.* re
2d152 66 65 72 65 6e 63 65 73 0a 2a 2a 20 69 6e 20 74  ferences.** in t
2d153 68 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  he trigger progr
2d154 61 6d 2e 20 49 66 20 4e 20 69 73 20 74 68 65 20  am. If N is the 
2d155 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
2d156 73 20 69 6e 20 74 61 62 6c 65 20 70 54 61 62 0a  s in table pTab.
2d157 2a 2a 20 28 61 20 63 6f 70 79 20 6f 66 20 70 54  ** (a copy of pT
2d158 61 62 2d 3e 6e 43 6f 6c 29 2c 20 74 68 65 6e 20  ab->nCol), then 
2d159 72 65 67 69 73 74 65 72 73 20 61 72 65 20 70 6f  registers are po
2d15a 70 75 6c 61 74 65 64 20 61 73 20 66 6f 6c 6c 6f  pulated as follo
2d15b 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 52 65 67 69  ws:.**.**   Regi
2d15c 73 74 65 72 20 20 20 20 20 20 20 43 6f 6e 74 61  ster       Conta
2d15d 69 6e 73 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  ins.**   -------
2d15e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d15f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2d160 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
2d161 2a 2a 20 20 20 72 65 67 2b 30 20 20 20 20 20 20  **   reg+0      
2d162 20 20 20 20 4f 4c 44 2e 72 6f 77 69 64 0a 2a 2a      OLD.rowid.**
2d163 20 20 20 72 65 67 2b 31 20 20 20 20 20 20 20 20     reg+1        
2d164 20 20 4f 4c 44 2e 2a 20 76 61 6c 75 65 20 6f 66    OLD.* value of
2d165 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d   left-most colum
2d166 6e 20 6f 66 20 70 54 61 62 0a 2a 2a 20 20 20 2e  n of pTab.**   .
2d167 2e 2e 20 20 20 20 20 20 20 20 20 20 20 20 2e 2e  ..            ..
2d168 2e 0a 2a 2a 20 20 20 72 65 67 2b 4e 20 20 20 20  ..**   reg+N    
2d169 20 20 20 20 20 20 4f 4c 44 2e 2a 20 76 61 6c 75        OLD.* valu
2d16a 65 20 6f 66 20 72 69 67 68 74 2d 6d 6f 73 74 20  e of right-most 
2d16b 63 6f 6c 75 6d 6e 20 6f 66 20 70 54 61 62 0a 2a  column of pTab.*
2d16c 2a 20 20 20 72 65 67 2b 4e 2b 31 20 20 20 20 20  *   reg+N+1     
2d16d 20 20 20 4e 45 57 2e 72 6f 77 69 64 0a 2a 2a 20     NEW.rowid.** 
2d16e 20 20 72 65 67 2b 4e 2b 32 20 20 20 20 20 20 20    reg+N+2       
2d16f 20 4f 4c 44 2e 2a 20 76 61 6c 75 65 20 6f 66 20   OLD.* value of 
2d170 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e  left-most column
2d171 20 6f 66 20 70 54 61 62 0a 2a 2a 20 20 20 2e 2e   of pTab.**   ..
2d172 2e 20 20 20 20 20 20 20 20 20 20 20 20 2e 2e 2e  .            ...
2d173 0a 2a 2a 20 20 20 72 65 67 2b 4e 2b 4e 2b 31 20  .**   reg+N+N+1 
2d174 20 20 20 20 20 4e 45 57 2e 2a 20 76 61 6c 75 65       NEW.* value
2d175 20 6f 66 20 72 69 67 68 74 2d 6d 6f 73 74 20 63   of right-most c
2d176 6f 6c 75 6d 6e 20 6f 66 20 70 54 61 62 0a 2a 2a  olumn of pTab.**
2d177 0a 2a 2a 20 46 6f 72 20 4f 4e 20 44 45 4c 45 54  .** For ON DELET
2d178 45 20 74 72 69 67 67 65 72 73 2c 20 74 68 65 20  E triggers, the 
2d179 72 65 67 69 73 74 65 72 73 20 63 6f 6e 74 61 69  registers contai
2d17a 6e 69 6e 67 20 74 68 65 20 4e 45 57 2e 2a 20 76  ning the NEW.* v
2d17b 61 6c 75 65 73 20 77 69 6c 6c 0a 2a 2a 20 6e 65  alues will.** ne
2d17c 76 65 72 20 62 65 20 61 63 63 65 73 73 65 64 20  ver be accessed 
2d17d 62 79 20 74 68 65 20 74 72 69 67 67 65 72 20 70  by the trigger p
2d17e 72 6f 67 72 61 6d 2c 20 73 6f 20 74 68 65 79 20  rogram, so they 
2d17f 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65  are not allocate
2d180 64 20 6f 72 20 0a 2a 2a 20 70 6f 70 75 6c 61 74  d or .** populat
2d181 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72  ed by the caller
2d182 20 28 74 68 65 72 65 20 69 73 20 6e 6f 20 64 61   (there is no da
2d183 74 61 20 74 6f 20 70 6f 70 75 6c 61 74 65 20 74  ta to populate t
2d184 68 65 6d 20 77 69 74 68 20 61 6e 79 77 61 79 29  hem with anyway)
2d185 2e 20 0a 2a 2a 20 53 69 6d 69 6c 61 72 6c 79 2c  . .** Similarly,
2d186 20 66 6f 72 20 4f 4e 20 49 4e 53 45 52 54 20 74   for ON INSERT t
2d187 72 69 67 67 65 72 73 20 74 68 65 20 76 61 6c 75  riggers the valu
2d188 65 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65  es stored in the
2d189 20 4f 4c 44 2e 2a 20 72 65 67 69 73 74 65 72 73   OLD.* registers
2d18a 0a 2a 2a 20 61 72 65 20 6e 65 76 65 72 20 61 63  .** are never ac
2d18b 63 65 73 73 65 64 2c 20 61 6e 64 20 73 6f 20 61  cessed, and so a
2d18c 72 65 20 6e 6f 74 20 61 6c 6c 6f 63 61 74 65 64  re not allocated
2d18d 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20   by the caller. 
2d18e 53 6f 2c 20 66 6f 72 20 61 6e 0a 2a 2a 20 4f 4e  So, for an.** ON
2d18f 20 49 4e 53 45 52 54 20 74 72 69 67 67 65 72 2c   INSERT trigger,
2d190 20 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65   the value passe
2d191 64 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69  d to this functi
2d192 6f 6e 20 61 73 20 70 61 72 61 6d 65 74 65 72 20  on as parameter 
2d193 72 65 67 0a 2a 2a 20 69 73 20 6e 6f 74 20 61 20  reg.** is not a 
2d194 72 65 61 64 61 62 6c 65 20 72 65 67 69 73 74 65  readable registe
2d195 72 2c 20 61 6c 74 68 6f 75 67 68 20 72 65 67 69  r, although regi
2d196 73 74 65 72 73 20 28 72 65 67 2b 4e 29 20 74 68  sters (reg+N) th
2d197 72 6f 75 67 68 20 0a 2a 2a 20 28 72 65 67 2b 4e  rough .** (reg+N
2d198 2b 4e 2b 31 29 20 61 72 65 2e 0a 2a 2a 0a 2a 2a  +N+1) are..**.**
2d199 20 50 61 72 61 6d 65 74 65 72 20 6f 72 63 6f 6e   Parameter orcon
2d19a 66 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74  f is the default
2d19b 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75   conflict resolu
2d19c 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 20 66  tion algorithm f
2d19d 6f 72 20 74 68 65 0a 2a 2a 20 74 72 69 67 67 65  or the.** trigge
2d19e 72 20 70 72 6f 67 72 61 6d 20 74 6f 20 75 73 65  r program to use
2d19f 20 28 52 45 50 4c 41 43 45 2c 20 49 47 4e 4f 52   (REPLACE, IGNOR
2d1a0 45 20 65 74 63 2e 29 2e 20 50 61 72 61 6d 65 74  E etc.). Paramet
2d1a1 65 72 20 69 67 6e 6f 72 65 4a 75 6d 70 0a 2a 2a  er ignoreJump.**
2d1a2 20 69 73 20 74 68 65 20 69 6e 73 74 72 75 63 74   is the instruct
2d1a3 69 6f 6e 20 74 68 61 74 20 63 6f 6e 74 72 6f 6c  ion that control
2d1a4 20 73 68 6f 75 6c 64 20 6a 75 6d 70 20 74 6f 20   should jump to 
2d1a5 69 66 20 61 20 74 72 69 67 67 65 72 20 70 72 6f  if a trigger pro
2d1a6 67 72 61 6d 0a 2a 2a 20 72 61 69 73 65 73 20 61  gram.** raises a
2d1a7 6e 20 49 47 4e 4f 52 45 20 65 78 63 65 70 74 69  n IGNORE excepti
2d1a8 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
2d1a9 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2d1aa 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72  e3CodeRowTrigger
2d1ab 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2d1ac 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73  e,       /* Pars
2d1ad 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  e context */.  T
2d1ae 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72  rigger *pTrigger
2d1af 2c 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74  ,   /* List of t
2d1b0 72 69 67 67 65 72 73 20 6f 6e 20 74 61 62 6c 65  riggers on table
2d1b1 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74 20 6f   pTab */.  int o
2d1b2 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
2d1b3 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 55 50 44  /* One of TK_UPD
2d1b4 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20  ATE, TK_INSERT, 
2d1b5 54 4b 5f 44 45 4c 45 54 45 20 2a 2f 0a 20 20 45  TK_DELETE */.  E
2d1b6 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65  xprList *pChange
2d1b7 73 2c 20 20 2f 2a 20 43 68 61 6e 67 65 73 20 6c  s,  /* Changes l
2d1b8 69 73 74 20 66 6f 72 20 61 6e 79 20 55 50 44 41  ist for any UPDA
2d1b9 54 45 20 4f 46 20 74 72 69 67 67 65 72 73 20 2a  TE OF triggers *
2d1ba 2f 0a 20 20 69 6e 74 20 74 72 5f 74 6d 2c 20 20  /.  int tr_tm,  
2d1bb 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
2d1bc 6f 66 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52  of TRIGGER_BEFOR
2d1bd 45 2c 20 54 52 49 47 47 45 52 5f 41 46 54 45 52  E, TRIGGER_AFTER
2d1be 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
2d1bf 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  b,         /* Th
2d1c0 65 20 74 61 62 6c 65 20 74 6f 20 63 6f 64 65 20  e table to code 
2d1c1 74 72 69 67 67 65 72 73 20 66 72 6f 6d 20 2a 2f  triggers from */
2d1c2 0a 20 20 69 6e 74 20 72 65 67 2c 20 20 20 20 20  .  int reg,     
2d1c3 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
2d1c4 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79  irst in an array
2d1c5 20 6f 66 20 72 65 67 69 73 74 65 72 73 20 28 73   of registers (s
2d1c6 65 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 69  ee above) */.  i
2d1c7 6e 74 20 6f 72 63 6f 6e 66 2c 20 20 20 20 20 20  nt orconf,      
2d1c8 20 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49      /* ON CONFLI
2d1c9 43 54 20 70 6f 6c 69 63 79 20 2a 2f 0a 20 20 69  CT policy */.  i
2d1ca 6e 74 20 69 67 6e 6f 72 65 4a 75 6d 70 20 20 20  nt ignoreJump   
2d1cb 20 20 20 20 2f 2a 20 49 6e 73 74 72 75 63 74 69      /* Instructi
2d1cc 6f 6e 20 74 6f 20 6a 75 6d 70 20 74 6f 20 66 6f  on to jump to fo
2d1cd 72 20 52 41 49 53 45 28 49 47 4e 4f 52 45 29 20  r RAISE(IGNORE) 
2d1ce 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 20  */.){.  Trigger 
2d1cf 2a 70 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  *p;          /* 
2d1d0 55 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 20  Used to iterate 
2d1d1 74 68 72 6f 75 67 68 20 70 54 72 69 67 67 65 72  through pTrigger
2d1d2 20 6c 69 73 74 20 2a 2f 0a 0a 20 20 61 73 73 65   list */..  asse
2d1d3 72 74 28 20 6f 70 3d 3d 54 4b 5f 55 50 44 41 54  rt( op==TK_UPDAT
2d1d4 45 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 4e 53 45  E || op==TK_INSE
2d1d5 52 54 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 44 45 4c  RT || op==TK_DEL
2d1d6 45 54 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ETE );.  assert(
2d1d7 20 74 72 5f 74 6d 3d 3d 54 52 49 47 47 45 52 5f   tr_tm==TRIGGER_
2d1d8 42 45 46 4f 52 45 20 7c 7c 20 74 72 5f 74 6d 3d  BEFORE || tr_tm=
2d1d9 3d 54 52 49 47 47 45 52 5f 41 46 54 45 52 20 29  =TRIGGER_AFTER )
2d1da 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6f 70 3d  ;.  assert( (op=
2d1db 3d 54 4b 5f 55 50 44 41 54 45 29 3d 3d 28 70 43  =TK_UPDATE)==(pC
2d1dc 68 61 6e 67 65 73 21 3d 30 29 20 29 3b 0a 0a 20  hanges!=0) );.. 
2d1dd 20 66 6f 72 28 70 3d 70 54 72 69 67 67 65 72 3b   for(p=pTrigger;
2d1de 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b   p; p=p->pNext){
2d1df 0a 0a 20 20 20 20 2f 2a 20 53 61 6e 69 74 79 20  ..    /* Sanity 
2d1e0 63 68 65 63 6b 69 6e 67 3a 20 20 54 68 65 20 73  checking:  The s
2d1e1 63 68 65 6d 61 20 66 6f 72 20 74 68 65 20 74 72  chema for the tr
2d1e2 69 67 67 65 72 20 61 6e 64 20 66 6f 72 20 74 68  igger and for th
2d1e3 65 20 74 61 62 6c 65 20 61 72 65 0a 20 20 20 20  e table are.    
2d1e4 2a 2a 20 61 6c 77 61 79 73 20 64 65 66 69 6e 65  ** always define
2d1e5 64 2e 20 20 54 68 65 20 74 72 69 67 67 65 72 20  d.  The trigger 
2d1e6 6d 75 73 74 20 62 65 20 69 6e 20 74 68 65 20 73  must be in the s
2d1e7 61 6d 65 20 73 63 68 65 6d 61 20 61 73 20 74 68  ame schema as th
2d1e8 65 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 6f  e table.    ** o
2d1e9 72 20 65 6c 73 65 20 69 74 20 6d 75 73 74 20 62  r else it must b
2d1ea 65 20 61 20 54 45 4d 50 20 74 72 69 67 67 65 72  e a TEMP trigger
2d1eb 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  . */.    assert(
2d1ec 20 70 2d 3e 70 53 63 68 65 6d 61 21 3d 30 20 29   p->pSchema!=0 )
2d1ed 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  ;.    assert( p-
2d1ee 3e 70 54 61 62 53 63 68 65 6d 61 21 3d 30 20 29  >pTabSchema!=0 )
2d1ef 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  ;.    assert( p-
2d1f0 3e 70 53 63 68 65 6d 61 3d 3d 70 2d 3e 70 54 61  >pSchema==p->pTa
2d1f1 62 53 63 68 65 6d 61 20 0a 20 20 20 20 20 20 20  bSchema .       
2d1f2 20 20 7c 7c 20 70 2d 3e 70 53 63 68 65 6d 61 3d    || p->pSchema=
2d1f3 3d 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62  =pParse->db->aDb
2d1f4 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29 3b 0a 0a  [1].pSchema );..
2d1f5 20 20 20 20 2f 2a 20 44 65 74 65 72 6d 69 6e 65      /* Determine
2d1f6 20 77 68 65 74 68 65 72 20 77 65 20 73 68 6f 75   whether we shou
2d1f7 6c 64 20 63 6f 64 65 20 74 68 69 73 20 74 72 69  ld code this tri
2d1f8 67 67 65 72 20 2a 2f 0a 20 20 20 20 69 66 28 20  gger */.    if( 
2d1f9 70 2d 3e 6f 70 3d 3d 6f 70 20 0a 20 20 20 20 20  p->op==op .     
2d1fa 26 26 20 70 2d 3e 74 72 5f 74 6d 3d 3d 74 72 5f  && p->tr_tm==tr_
2d1fb 74 6d 20 0a 20 20 20 20 20 26 26 20 63 68 65 63  tm .     && chec
2d1fc 6b 43 6f 6c 75 6d 6e 4f 76 65 72 6c 61 70 28 70  kColumnOverlap(p
2d1fd 2d 3e 70 43 6f 6c 75 6d 6e 73 2c 20 70 43 68 61  ->pColumns, pCha
2d1fe 6e 67 65 73 29 0a 20 20 20 20 29 7b 0a 20 20 20  nges).    ){.   
2d1ff 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f     sqlite3CodeRo
2d200 77 54 72 69 67 67 65 72 44 69 72 65 63 74 28 70  wTriggerDirect(p
2d201 50 61 72 73 65 2c 20 70 2c 20 70 54 61 62 2c 20  Parse, p, pTab, 
2d202 72 65 67 2c 20 6f 72 63 6f 6e 66 2c 20 69 67 6e  reg, orconf, ign
2d203 6f 72 65 4a 75 6d 70 29 3b 0a 20 20 20 20 7d 0a  oreJump);.    }.
2d204 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 69    }.}../*.** Tri
2d205 67 67 65 72 73 20 66 69 72 65 64 20 62 79 20 55  ggers fired by U
2d206 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20  PDATE or DELETE 
2d207 73 74 61 74 65 6d 65 6e 74 73 20 6d 61 79 20 61  statements may a
2d208 63 63 65 73 73 20 76 61 6c 75 65 73 20 73 74 6f  ccess values sto
2d209 72 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 6f 6c  red.** in the ol
2d20a 64 2e 2a 20 70 73 65 75 64 6f 2d 74 61 62 6c 65  d.* pseudo-table
2d20b 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
2d20c 72 65 74 75 72 6e 73 20 61 20 33 32 2d 62 69 74  returns a 32-bit
2d20d 20 62 69 74 6d 61 73 6b 0a 2a 2a 20 69 6e 64 69   bitmask.** indi
2d20e 63 61 74 69 6e 67 20 77 68 69 63 68 20 63 6f 6c  cating which col
2d20f 75 6d 6e 73 20 6f 66 20 74 68 65 20 6f 6c 64 2e  umns of the old.
2d210 2a 20 74 61 62 6c 65 20 61 63 74 75 61 6c 6c 79  * table actually
2d211 20 61 72 65 20 75 73 65 64 20 62 79 0a 2a 2a 20   are used by.** 
2d212 74 72 69 67 67 65 72 73 2e 20 54 68 69 73 20 69  triggers. This i
2d213 6e 66 6f 72 6d 61 74 69 6f 6e 20 6d 61 79 20 62  nformation may b
2d214 65 20 75 73 65 64 20 62 79 20 74 68 65 20 63 61  e used by the ca
2d215 6c 6c 65 72 20 74 6f 20 61 76 6f 69 64 20 68 61  ller to avoid ha
2d216 76 69 6e 67 0a 2a 2a 20 74 6f 20 6c 6f 61 64 20  ving.** to load 
2d217 74 68 65 20 65 6e 74 69 72 65 20 6f 6c 64 2e 2a  the entire old.*
2d218 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 6d 65 6d   record into mem
2d219 6f 72 79 20 77 68 65 6e 20 65 78 65 63 75 74 69  ory when executi
2d21a 6e 67 20 61 6e 20 55 50 44 41 54 45 0a 2a 2a 20  ng an UPDATE.** 
2d21b 6f 72 20 44 45 4c 45 54 45 20 63 6f 6d 6d 61 6e  or DELETE comman
2d21c 64 2e 0a 2a 2a 0a 2a 2a 20 42 69 74 20 30 20 6f  d..**.** Bit 0 o
2d21d 66 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 6d  f the returned m
2d21e 61 73 6b 20 69 73 20 73 65 74 20 69 66 20 74 68  ask is set if th
2d21f 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75  e left-most colu
2d220 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 74 61 62  mn of the.** tab
2d221 6c 65 20 6d 61 79 20 62 65 20 61 63 63 65 73 73  le may be access
2d222 65 64 20 75 73 69 6e 67 20 61 6e 20 6f 6c 64 2e  ed using an old.
2d223 3c 63 6f 6c 3e 20 72 65 66 65 72 65 6e 63 65 2e  <col> reference.
2d224 20 42 69 74 20 31 20 69 73 20 73 65 74 20 69 66   Bit 1 is set if
2d225 0a 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 6c  .** the second l
2d226 65 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 76  eftmost column v
2d227 61 6c 75 65 20 69 73 20 72 65 71 75 69 72 65 64  alue is required
2d228 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 20 49 66 20  , and so on. If 
2d229 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6d 6f 72  there.** are mor
2d22a 65 20 74 68 61 6e 20 33 32 20 63 6f 6c 75 6d 6e  e than 32 column
2d22b 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2c 20  s in the table, 
2d22c 61 6e 64 20 61 74 20 6c 65 61 73 74 20 6f 6e 65  and at least one
2d22d 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73 0a   of the columns.
2d22e 2a 2a 20 77 69 74 68 20 61 6e 20 69 6e 64 65 78  ** with an index
2d22f 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 33 32   greater than 32
2d230 20 6d 61 79 20 62 65 20 61 63 63 65 73 73 65 64   may be accessed
2d231 2c 20 30 78 66 66 66 66 66 66 66 66 20 69 73 20  , 0xffffffff is 
2d232 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
2d233 49 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62  It is not possib
2d234 6c 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  le to determine 
2d235 69 66 20 74 68 65 20 6f 6c 64 2e 72 6f 77 69 64  if the old.rowid
2d236 20 63 6f 6c 75 6d 6e 20 69 73 20 61 63 63 65 73   column is acces
2d237 73 65 64 0a 2a 2a 20 62 79 20 74 72 69 67 67 65  sed.** by trigge
2d238 72 73 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 6d  rs. The caller m
2d239 75 73 74 20 61 6c 77 61 79 73 20 61 73 73 75 6d  ust always assum
2d23a 65 20 74 68 61 74 20 69 74 20 69 73 2e 0a 2a 2a  e that it is..**
2d23b 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20  .** There is no 
2d23c 65 71 75 69 76 61 6c 65 6e 74 20 66 75 6e 63 74  equivalent funct
2d23d 69 6f 6e 20 66 6f 72 20 6e 65 77 2e 2a 20 72 65  ion for new.* re
2d23e 66 65 72 65 6e 63 65 73 2e 0a 2a 2f 0a 53 51 4c  ferences..*/.SQL
2d23f 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20  ITE_PRIVATE u32 
2d240 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 4f 6c  sqlite3TriggerOl
2d241 64 6d 61 73 6b 28 0a 20 20 50 61 72 73 65 20 2a  dmask(.  Parse *
2d242 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a  pParse,       /*
2d243 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a   Parse context *
2d244 2f 0a 20 20 54 72 69 67 67 65 72 20 2a 70 54 72  /.  Trigger *pTr
2d245 69 67 67 65 72 2c 20 20 20 2f 2a 20 4c 69 73 74  igger,   /* List
2d246 20 6f 66 20 74 72 69 67 67 65 72 73 20 6f 6e 20   of triggers on 
2d247 74 61 62 6c 65 20 70 54 61 62 20 2a 2f 0a 20 20  table pTab */.  
2d248 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67  ExprList *pChang
2d249 65 73 2c 20 20 2f 2a 20 43 68 61 6e 67 65 73 20  es,  /* Changes 
2d24a 6c 69 73 74 20 66 6f 72 20 61 6e 79 20 55 50 44  list for any UPD
2d24b 41 54 45 20 4f 46 20 74 72 69 67 67 65 72 73 20  ATE OF triggers 
2d24c 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
2d24d 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
2d24e 20 74 61 62 6c 65 20 74 6f 20 63 6f 64 65 20 74   table to code t
2d24f 72 69 67 67 65 72 73 20 66 72 6f 6d 20 2a 2f 0a  riggers from */.
2d250 20 20 69 6e 74 20 6f 72 63 6f 6e 66 20 20 20 20    int orconf    
2d251 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c         /* Defaul
2d252 74 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 70 6f  t ON CONFLICT po
2d253 6c 69 63 79 20 66 6f 72 20 74 72 69 67 67 65 72  licy for trigger
2d254 20 73 74 65 70 73 20 2a 2f 0a 29 7b 0a 20 20 63   steps */.){.  c
2d255 6f 6e 73 74 20 69 6e 74 20 6f 70 20 3d 20 70 43  onst int op = pC
2d256 68 61 6e 67 65 73 20 3f 20 54 4b 5f 55 50 44 41  hanges ? TK_UPDA
2d257 54 45 20 3a 20 54 4b 5f 44 45 4c 45 54 45 3b 0a  TE : TK_DELETE;.
2d258 20 20 75 33 32 20 6d 61 73 6b 20 3d 20 30 3b 0a    u32 mask = 0;.
2d259 20 20 54 72 69 67 67 65 72 20 2a 70 3b 0a 0a 20    Trigger *p;.. 
2d25a 20 66 6f 72 28 70 3d 70 54 72 69 67 67 65 72 3b   for(p=pTrigger;
2d25b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b   p; p=p->pNext){
2d25c 0a 20 20 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d  .    if( p->op==
2d25d 6f 70 20 26 26 20 63 68 65 63 6b 43 6f 6c 75 6d  op && checkColum
2d25e 6e 4f 76 65 72 6c 61 70 28 70 2d 3e 70 43 6f 6c  nOverlap(p->pCol
2d25f 75 6d 6e 73 2c 70 43 68 61 6e 67 65 73 29 20 29  umns,pChanges) )
2d260 7b 0a 20 20 20 20 20 20 54 72 69 67 67 65 72 50  {.      TriggerP
2d261 72 67 20 2a 70 50 72 67 3b 0a 20 20 20 20 20 20  rg *pPrg;.      
2d262 70 50 72 67 20 3d 20 67 65 74 52 6f 77 54 72 69  pPrg = getRowTri
2d263 67 67 65 72 28 70 50 61 72 73 65 2c 20 70 2c 20  gger(pParse, p, 
2d264 70 54 61 62 2c 20 6f 72 63 6f 6e 66 29 3b 0a 20  pTab, orconf);. 
2d265 20 20 20 20 20 69 66 28 20 70 50 72 67 20 29 7b       if( pPrg ){
2d266 0a 20 20 20 20 20 20 20 20 6d 61 73 6b 20 7c 3d  .        mask |=
2d267 20 70 50 72 67 2d 3e 6f 6c 64 6d 61 73 6b 3b 0a   pPrg->oldmask;.
2d268 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2d269 7d 0a 0a 20 20 72 65 74 75 72 6e 20 6d 61 73 6b  }..  return mask
2d26a 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21  ;.}..#endif /* !
2d26b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
2d26c 4d 49 54 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a  MIT_TRIGGER) */.
2d26d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2d26e 20 45 6e 64 20 6f 66 20 74 72 69 67 67 65 72 2e   End of trigger.
2d26f 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2d270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d271 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2d272 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2d273 20 42 65 67 69 6e 20 66 69 6c 65 20 75 70 64 61   Begin file upda
2d274 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  te.c ***********
2d275 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d276 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2d277 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
2d278 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
2d279 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
2d27a 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
2d27b 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
2d27c 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
2d27d 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
2d27e 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
2d27f 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
2d280 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
2d281 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
2d282 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
2d283 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
2d284 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
2d285 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
2d286 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
2d287 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
2d288 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
2d289 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
2d28a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d28b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d28c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d28d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d28e 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
2d28f 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20  file contains C 
2d290 63 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68  code routines th
2d291 61 74 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79  at are called by
2d292 20 74 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 74   the parser.** t
2d293 6f 20 68 61 6e 64 6c 65 20 55 50 44 41 54 45 20  o handle UPDATE 
2d294 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a  statements..**.*
2d295 2a 20 24 49 64 3a 20 75 70 64 61 74 65 2e 63 2c  * $Id: update.c,
2d296 76 20 31 2e 32 30 37 20 32 30 30 39 2f 30 38 2f  v 1.207 2009/08/
2d297 30 38 20 31 38 3a 30 31 3a 30 38 20 64 72 68 20  08 18:01:08 drh 
2d298 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65  Exp $.*/..#ifnde
2d299 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
2d29a 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 46 6f  RTUALTABLE./* Fo
2d29b 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f  rward declaratio
2d29c 6e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  n */.static void
2d29d 20 75 70 64 61 74 65 56 69 72 74 75 61 6c 54 61   updateVirtualTa
2d29e 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ble(.  Parse *pP
2d29f 61 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 54  arse,       /* T
2d2a0 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65  he parsing conte
2d2a1 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20  xt */.  SrcList 
2d2a2 2a 70 53 72 63 2c 20 20 20 20 20 20 20 2f 2a 20  *pSrc,       /* 
2d2a3 54 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  The virtual tabl
2d2a4 65 20 74 6f 20 62 65 20 6d 6f 64 69 66 69 65 64  e to be modified
2d2a5 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
2d2a6 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  b,         /* Th
2d2a7 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
2d2a8 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2d2a9 43 68 61 6e 67 65 73 2c 20 20 2f 2a 20 54 68 65  Changes,  /* The
2d2aa 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 63 68 61 6e   columns to chan
2d2ab 67 65 20 69 6e 20 74 68 65 20 55 50 44 41 54 45  ge in the UPDATE
2d2ac 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
2d2ad 45 78 70 72 20 2a 70 52 6f 77 69 64 45 78 70 72  Expr *pRowidExpr
2d2ae 2c 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69  ,    /* Expressi
2d2af 6f 6e 20 75 73 65 64 20 74 6f 20 72 65 63 6f 6d  on used to recom
2d2b0 70 75 74 65 20 74 68 65 20 72 6f 77 69 64 20 2a  pute the rowid *
2d2b1 2f 0a 20 20 69 6e 74 20 2a 61 58 52 65 66 2c 20  /.  int *aXRef, 
2d2b2 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 70 70           /* Mapp
2d2b3 69 6e 67 20 66 72 6f 6d 20 63 6f 6c 75 6d 6e 73  ing from columns
2d2b4 20 6f 66 20 70 54 61 62 20 74 6f 20 65 6e 74 72   of pTab to entr
2d2b5 69 65 73 20 69 6e 20 70 43 68 61 6e 67 65 73 20  ies in pChanges 
2d2b6 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72  */.  Expr *pWher
2d2b7 65 20 20 20 20 20 20 20 20 20 2f 2a 20 57 48 45  e         /* WHE
2d2b8 52 45 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  RE clause of the
2d2b9 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
2d2ba 74 20 2a 2f 0a 29 3b 0a 23 65 6e 64 69 66 20 2f  t */.);.#endif /
2d2bb 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  * SQLITE_OMIT_VI
2d2bc 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f  RTUALTABLE */../
2d2bd 2a 0a 2a 2a 20 54 68 65 20 6d 6f 73 74 20 72 65  *.** The most re
2d2be 63 65 6e 74 6c 79 20 63 6f 64 65 64 20 69 6e 73  cently coded ins
2d2bf 74 72 75 63 74 69 6f 6e 20 77 61 73 20 61 6e 20  truction was an 
2d2c0 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 72 65 74  OP_Column to ret
2d2c1 72 69 65 76 65 20 74 68 65 0a 2a 2a 20 69 2d 74  rieve the.** i-t
2d2c2 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c  h column of tabl
2d2c3 65 20 70 54 61 62 2e 20 54 68 69 73 20 72 6f 75  e pTab. This rou
2d2c4 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 50 34  tine sets the P4
2d2c5 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68   parameter of th
2d2c6 65 20 0a 2a 2a 20 4f 50 5f 43 6f 6c 75 6d 6e 20  e .** OP_Column 
2d2c7 74 6f 20 74 68 65 20 64 65 66 61 75 6c 74 20 76  to the default v
2d2c8 61 6c 75 65 2c 20 69 66 20 61 6e 79 2e 0a 2a 2a  alue, if any..**
2d2c9 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20  .** The default 
2d2ca 76 61 6c 75 65 20 6f 66 20 61 20 63 6f 6c 75 6d  value of a colum
2d2cb 6e 20 69 73 20 73 70 65 63 69 66 69 65 64 20 62  n is specified b
2d2cc 79 20 61 20 44 45 46 41 55 4c 54 20 63 6c 61 75  y a DEFAULT clau
2d2cd 73 65 20 69 6e 20 74 68 65 20 0a 2a 2a 20 63 6f  se in the .** co
2d2ce 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2e  lumn definition.
2d2cf 20 54 68 69 73 20 77 61 73 20 65 69 74 68 65 72   This was either
2d2d0 20 73 75 70 70 6c 69 65 64 20 62 79 20 74 68 65   supplied by the
2d2d1 20 75 73 65 72 20 77 68 65 6e 20 74 68 65 20 74   user when the t
2d2d2 61 62 6c 65 0a 2a 2a 20 77 61 73 20 63 72 65 61  able.** was crea
2d2d3 74 65 64 2c 20 6f 72 20 61 64 64 65 64 20 6c 61  ted, or added la
2d2d4 74 65 72 20 74 6f 20 74 68 65 20 74 61 62 6c 65  ter to the table
2d2d5 20 64 65 66 69 6e 69 74 69 6f 6e 20 62 79 20 61   definition by a
2d2d6 6e 20 41 4c 54 45 52 20 54 41 42 4c 45 0a 2a 2a  n ALTER TABLE.**
2d2d7 20 63 6f 6d 6d 61 6e 64 2e 20 49 66 20 74 68 65   command. If the
2d2d8 20 6c 61 74 74 65 72 2c 20 74 68 65 6e 20 74 68   latter, then th
2d2d9 65 20 72 6f 77 2d 72 65 63 6f 72 64 73 20 69 6e  e row-records in
2d2da 20 74 68 65 20 74 61 62 6c 65 20 62 74 72 65 65   the table btree
2d2db 20 6f 6e 20 64 69 73 6b 0a 2a 2a 20 6d 61 79 20   on disk.** may 
2d2dc 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 20 76 61  not contain a va
2d2dd 6c 75 65 20 66 6f 72 20 74 68 65 20 63 6f 6c 75  lue for the colu
2d2de 6d 6e 20 61 6e 64 20 74 68 65 20 64 65 66 61 75  mn and the defau
2d2df 6c 74 20 76 61 6c 75 65 2c 20 74 61 6b 65 6e 0a  lt value, taken.
2d2e0 2a 2a 20 66 72 6f 6d 20 74 68 65 20 50 34 20 70  ** from the P4 p
2d2e1 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68 65 20  arameter of the 
2d2e2 4f 50 5f 43 6f 6c 75 6d 6e 20 69 6e 73 74 72 75  OP_Column instru
2d2e3 63 74 69 6f 6e 2c 20 69 73 20 72 65 74 75 72 6e  ction, is return
2d2e4 65 64 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 20 49  ed instead..** I
2d2e5 66 20 74 68 65 20 66 6f 72 6d 65 72 2c 20 74 68  f the former, th
2d2e6 65 6e 20 61 6c 6c 20 72 6f 77 2d 72 65 63 6f 72  en all row-recor
2d2e7 64 73 20 61 72 65 20 67 75 61 72 61 6e 74 65 65  ds are guarantee
2d2e8 64 20 74 6f 20 69 6e 63 6c 75 64 65 20 61 20 76  d to include a v
2d2e9 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20  alue.** for the 
2d2ea 63 6f 6c 75 6d 6e 20 61 6e 64 20 74 68 65 20 50  column and the P
2d2eb 34 20 76 61 6c 75 65 20 69 73 20 6e 6f 74 20 72  4 value is not r
2d2ec 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 43  equired..**.** C
2d2ed 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e  olumn definition
2d2ee 73 20 63 72 65 61 74 65 64 20 62 79 20 61 6e 20  s created by an 
2d2ef 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d  ALTER TABLE comm
2d2f0 61 6e 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76  and may only hav
2d2f1 65 20 0a 2a 2a 20 6c 69 74 65 72 61 6c 20 64 65  e .** literal de
2d2f2 66 61 75 6c 74 20 76 61 6c 75 65 73 20 73 70 65  fault values spe
2d2f3 63 69 66 69 65 64 3a 20 61 20 6e 75 6d 62 65 72  cified: a number
2d2f4 2c 20 6e 75 6c 6c 20 6f 72 20 61 20 73 74 72 69  , null or a stri
2d2f5 6e 67 2e 20 28 49 66 20 61 20 6d 6f 72 65 0a 2a  ng. (If a more.*
2d2f6 2a 20 63 6f 6d 70 6c 69 63 61 74 65 64 20 64 65  * complicated de
2d2f7 66 61 75 6c 74 20 65 78 70 72 65 73 73 69 6f 6e  fault expression
2d2f8 20 76 61 6c 75 65 20 77 61 73 20 70 72 6f 76 69   value was provi
2d2f9 64 65 64 2c 20 69 74 20 69 73 20 65 76 61 6c 75  ded, it is evalu
2d2fa 61 74 65 64 20 0a 2a 2a 20 77 68 65 6e 20 74 68  ated .** when th
2d2fb 65 20 41 4c 54 45 52 20 54 41 42 4c 45 20 69 73  e ALTER TABLE is
2d2fc 20 65 78 65 63 75 74 65 64 20 61 6e 64 20 6f 6e   executed and on
2d2fd 65 20 6f 66 20 74 68 65 20 6c 69 74 65 72 61 6c  e of the literal
2d2fe 20 76 61 6c 75 65 73 20 77 72 69 74 74 65 6e 0a   values written.
2d2ff 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 71 6c 69  ** into the sqli
2d300 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e  te_master table.
2d301 29 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 66 6f 72  ).**.** Therefor
2d302 65 2c 20 74 68 65 20 50 34 20 70 61 72 61 6d 65  e, the P4 parame
2d303 74 65 72 20 69 73 20 6f 6e 6c 79 20 72 65 71 75  ter is only requ
2d304 69 72 65 64 20 69 66 20 74 68 65 20 64 65 66 61  ired if the defa
2d305 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 0a 2a 2a  ult value for.**
2d306 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 61   the column is a
2d307 20 6c 69 74 65 72 61 6c 20 6e 75 6d 62 65 72 2c   literal number,
2d308 20 73 74 72 69 6e 67 20 6f 72 20 6e 75 6c 6c 2e   string or null.
2d309 20 54 68 65 20 73 71 6c 69 74 65 33 56 61 6c 75   The sqlite3Valu
2d30a 65 46 72 6f 6d 45 78 70 72 28 29 0a 2a 2a 20 66  eFromExpr().** f
2d30b 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 70 61 62  unction is capab
2d30c 6c 65 20 6f 66 20 74 72 61 6e 73 66 6f 72 6d 69  le of transformi
2d30d 6e 67 20 74 68 65 73 65 20 74 79 70 65 73 20 6f  ng these types o
2d30e 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e  f expressions in
2d30f 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61  to.** sqlite3_va
2d310 6c 75 65 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a  lue objects..**.
2d311 2a 2a 20 49 66 20 70 61 72 61 6d 65 74 65 72 20  ** If parameter 
2d312 69 52 65 67 20 69 73 20 6e 6f 74 20 6e 65 67 61  iReg is not nega
2d313 74 69 76 65 2c 20 63 6f 64 65 20 61 6e 20 4f 50  tive, code an OP
2d314 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 20 69 6e  _RealAffinity in
2d315 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 6f 6e 20  struction.** on 
2d316 72 65 67 69 73 74 65 72 20 69 52 65 67 2e 20 54  register iReg. T
2d317 68 69 73 20 69 73 20 75 73 65 64 20 77 68 65 6e  his is used when
2d318 20 61 6e 20 65 71 75 69 76 61 6c 65 6e 74 20 69   an equivalent i
2d319 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 73 20  nteger value is 
2d31a 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20 70 6c  .** stored in pl
2d31b 61 63 65 20 6f 66 20 61 6e 20 38 2d 62 79 74 65  ace of an 8-byte
2d31c 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20   floating point 
2d31d 76 61 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 74  value in order t
2d31e 6f 20 73 61 76 65 20 0a 2a 2a 20 73 70 61 63 65  o save .** space
2d31f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2d320 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
2d321 43 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 56 64  ColumnDefault(Vd
2d322 62 65 20 2a 76 2c 20 54 61 62 6c 65 20 2a 70 54  be *v, Table *pT
2d323 61 62 2c 20 69 6e 74 20 69 2c 20 69 6e 74 20 69  ab, int i, int i
2d324 52 65 67 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Reg){.  assert( 
2d325 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 69 66 28  pTab!=0 );.  if(
2d326 20 21 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20   !pTab->pSelect 
2d327 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  ){.    sqlite3_v
2d328 61 6c 75 65 20 2a 70 56 61 6c 75 65 3b 0a 20 20  alue *pValue;.  
2d329 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 73    u8 enc = ENC(s
2d32a 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 29  qlite3VdbeDb(v))
2d32b 3b 0a 20 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43  ;.    Column *pC
2d32c 6f 6c 20 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c  ol = &pTab->aCol
2d32d 5b 69 5d 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d  [i];.    VdbeCom
2d32e 6d 65 6e 74 28 28 76 2c 20 22 25 73 2e 25 73 22  ment((v, "%s.%s"
2d32f 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 70  , pTab->zName, p
2d330 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20  Col->zName));.  
2d331 20 20 61 73 73 65 72 74 28 20 69 3c 70 54 61 62    assert( i<pTab
2d332 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20 20 20 73 71  ->nCol );.    sq
2d333 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78  lite3ValueFromEx
2d334 70 72 28 73 71 6c 69 74 65 33 56 64 62 65 44 62  pr(sqlite3VdbeDb
2d335 28 76 29 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74  (v), pCol->pDflt
2d336 2c 20 65 6e 63 2c 20 0a 20 20 20 20 20 20 20 20  , enc, .        
2d337 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d338 20 70 43 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 2c   pCol->affinity,
2d339 20 26 70 56 61 6c 75 65 29 3b 0a 20 20 20 20 69   &pValue);.    i
2d33a 66 28 20 70 56 61 6c 75 65 20 29 7b 0a 20 20 20  f( pValue ){.   
2d33b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
2d33c 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63  angeP4(v, -1, (c
2d33d 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 56 61 6c  onst char *)pVal
2d33e 75 65 2c 20 50 34 5f 4d 45 4d 29 3b 0a 20 20 20  ue, P4_MEM);.   
2d33f 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
2d340 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
2d341 50 4f 49 4e 54 0a 20 20 20 20 69 66 28 20 69 52  POINT.    if( iR
2d342 65 67 3e 3d 30 20 26 26 20 70 54 61 62 2d 3e 61  eg>=0 && pTab->a
2d343 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 3d  Col[i].affinity=
2d344 3d 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c  =SQLITE_AFF_REAL
2d345 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2d346 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2d347 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 2c 20  P_RealAffinity, 
2d348 69 52 65 67 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  iReg);.    }.#en
2d349 64 69 66 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  dif.  }.}../*.**
2d34a 20 50 72 6f 63 65 73 73 20 61 6e 20 55 50 44 41   Process an UPDA
2d34b 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  TE statement..**
2d34c 0a 2a 2a 20 20 20 55 50 44 41 54 45 20 4f 52 20  .**   UPDATE OR 
2d34d 49 47 4e 4f 52 45 20 74 61 62 6c 65 5f 77 78 79  IGNORE table_wxy
2d34e 7a 20 53 45 54 20 61 3d 62 2c 20 63 3d 64 20 57  z SET a=b, c=d W
2d34f 48 45 52 45 20 65 3c 35 20 41 4e 44 20 66 20 4e  HERE e<5 AND f N
2d350 4f 54 20 4e 55 4c 4c 3b 0a 2a 2a 20 20 20 20 20  OT NULL;.**     
2d351 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f 2f 20 5c       \_______/ \
2d352 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f  ________/     \_
2d353 5f 5f 5f 5f 5f 2f 20 20 20 20 20 20 20 5c 5f 5f  _____/       \__
2d354 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a  ______________/.
2d355 2a 20 20 20 20 20 20 20 20 20 20 20 20 6f 6e 45  *            onE
2d356 72 72 6f 72 20 20 20 70 54 61 62 4c 69 73 74 20  rror   pTabList 
2d357 20 20 20 20 20 70 43 68 61 6e 67 65 73 20 20 20       pChanges   
2d358 20 20 20 20 20 20 20 20 20 20 70 57 68 65 72 65            pWhere
2d359 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2d35a 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55  TE void sqlite3U
2d35b 70 64 61 74 65 28 0a 20 20 50 61 72 73 65 20 2a  pdate(.  Parse *
2d35c 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20  pParse,         
2d35d 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f  /* The parser co
2d35e 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69  ntext */.  SrcLi
2d35f 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20  st *pTabList,   
2d360 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 69    /* The table i
2d361 6e 20 77 68 69 63 68 20 77 65 20 73 68 6f 75 6c  n which we shoul
2d362 64 20 63 68 61 6e 67 65 20 74 68 69 6e 67 73 20  d change things 
2d363 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
2d364 43 68 61 6e 67 65 73 2c 20 20 20 20 2f 2a 20 54  Changes,    /* T
2d365 68 69 6e 67 73 20 74 6f 20 62 65 20 63 68 61 6e  hings to be chan
2d366 67 65 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ged */.  Expr *p
2d367 57 68 65 72 65 2c 20 20 20 20 20 20 20 20 20 20  Where,          
2d368 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61  /* The WHERE cla
2d369 75 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c  use.  May be nul
2d36a 6c 20 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72  l */.  int onErr
2d36b 6f 72 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  or            /*
2d36c 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20 63   How to handle c
2d36d 6f 6e 73 74 72 61 69 6e 74 20 65 72 72 6f 72 73  onstraint errors
2d36e 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20   */.){.  int i, 
2d36f 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  j;              
2d370 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73  /* Loop counters
2d371 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
2d372 62 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  b;           /* 
2d373 54 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20  The table to be 
2d374 75 70 64 61 74 65 64 20 2a 2f 0a 20 20 69 6e 74  updated */.  int
2d375 20 61 64 64 72 20 3d 20 30 3b 20 20 20 20 20 20   addr = 0;      
2d376 20 20 20 20 2f 2a 20 56 44 42 45 20 69 6e 73 74      /* VDBE inst
2d377 72 75 63 74 69 6f 6e 20 61 64 64 72 65 73 73 20  ruction address 
2d378 6f 66 20 74 68 65 20 73 74 61 72 74 20 6f 66 20  of the start of 
2d379 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 57 68  the loop */.  Wh
2d37a 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b  ereInfo *pWInfo;
2d37b 20 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74       /* Informat
2d37c 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 57 48  ion about the WH
2d37d 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
2d37e 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20  Vdbe *v;        
2d37f 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69         /* The vi
2d380 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65  rtual database e
2d381 6e 67 69 6e 65 20 2a 2f 0a 20 20 49 6e 64 65 78  ngine */.  Index
2d382 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20 20   *pIdx;         
2d383 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67    /* For looping
2d384 20 6f 76 65 72 20 69 6e 64 69 63 65 73 20 2a 2f   over indices */
2d385 0a 20 20 69 6e 74 20 6e 49 64 78 3b 20 20 20 20  .  int nIdx;    
2d386 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
2d387 62 65 72 20 6f 66 20 69 6e 64 69 63 65 73 20 74  ber of indices t
2d388 68 61 74 20 6e 65 65 64 20 75 70 64 61 74 69 6e  hat need updatin
2d389 67 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b  g */.  int iCur;
2d38a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2d38b 20 56 44 42 45 20 43 75 72 73 6f 72 20 6e 75 6d   VDBE Cursor num
2d38c 62 65 72 20 6f 66 20 70 54 61 62 20 2a 2f 0a 20  ber of pTab */. 
2d38d 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
2d38e 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
2d38f 61 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72  atabase structur
2d390 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67  e */.  int *aReg
2d391 49 64 78 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a  Idx = 0;      /*
2d392 20 4f 6e 65 20 72 65 67 69 73 74 65 72 20 61 73   One register as
2d393 73 69 67 6e 65 64 20 74 6f 20 65 61 63 68 20 69  signed to each i
2d394 6e 64 65 78 20 74 6f 20 62 65 20 75 70 64 61 74  ndex to be updat
2d395 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 58 52  ed */.  int *aXR
2d396 65 66 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  ef = 0;        /
2d397 2a 20 61 58 52 65 66 5b 69 5d 20 69 73 20 74 68  * aXRef[i] is th
2d398 65 20 69 6e 64 65 78 20 69 6e 20 70 43 68 61 6e  e index in pChan
2d399 67 65 73 2d 3e 61 5b 5d 20 6f 66 20 74 68 65 0a  ges->a[] of the.
2d39a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d39b 20 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 20 65           ** an e
2d39c 78 70 72 65 73 73 69 6f 6e 20 66 6f 72 20 74 68  xpression for th
2d39d 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  e i-th column of
2d39e 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20 20 20   the table..    
2d39f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d3a0 20 20 20 20 20 2a 2a 20 61 58 52 65 66 5b 69 5d       ** aXRef[i]
2d3a1 3d 3d 2d 31 20 69 66 20 74 68 65 20 69 2d 74 68  ==-1 if the i-th
2d3a2 20 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 63   column is not c
2d3a3 68 61 6e 67 65 64 2e 20 2a 2f 0a 20 20 69 6e 74  hanged. */.  int
2d3a4 20 63 68 6e 67 52 6f 77 69 64 3b 20 20 20 20 20   chngRowid;     
2d3a5 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
2d3a6 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  he record number
2d3a7 20 69 73 20 62 65 69 6e 67 20 63 68 61 6e 67 65   is being change
2d3a8 64 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 6f  d */.  Expr *pRo
2d3a9 77 69 64 45 78 70 72 20 3d 20 30 3b 20 20 2f 2a  widExpr = 0;  /*
2d3aa 20 45 78 70 72 65 73 73 69 6f 6e 20 64 65 66 69   Expression defi
2d3ab 6e 69 6e 67 20 74 68 65 20 6e 65 77 20 72 65 63  ning the new rec
2d3ac 6f 72 64 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  ord number */.  
2d3ad 69 6e 74 20 6f 70 65 6e 41 6c 6c 20 3d 20 30 3b  int openAll = 0;
2d3ae 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
2d3af 66 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6e 65  f all indices ne
2d3b0 65 64 20 74 6f 20 62 65 20 6f 70 65 6e 65 64 20  ed to be opened 
2d3b1 2a 2f 0a 20 20 41 75 74 68 43 6f 6e 74 65 78 74  */.  AuthContext
2d3b2 20 73 43 6f 6e 74 65 78 74 3b 20 20 2f 2a 20 54   sContext;  /* T
2d3b3 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  he authorization
2d3b4 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 4e 61   context */.  Na
2d3b5 6d 65 43 6f 6e 74 65 78 74 20 73 4e 43 3b 20 20  meContext sNC;  
2d3b6 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65       /* The name
2d3b7 2d 63 6f 6e 74 65 78 74 20 74 6f 20 72 65 73 6f  -context to reso
2d3b8 6c 76 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20  lve expressions 
2d3b9 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b  in */.  int iDb;
2d3ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2d3bb 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 74 61  * Database conta
2d3bc 69 6e 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20  ining the table 
2d3bd 62 65 69 6e 67 20 75 70 64 61 74 65 64 20 2a 2f  being updated */
2d3be 0a 20 20 69 6e 74 20 6a 31 3b 20 20 20 20 20 20  .  int j1;      
2d3bf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64            /* Add
2d3c0 72 65 73 73 65 73 20 6f 66 20 6a 75 6d 70 20 69  resses of jump i
2d3c1 6e 73 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a 20  nstructions */. 
2d3c2 20 69 6e 74 20 6f 6b 4f 6e 65 50 61 73 73 3b 20   int okOnePass; 
2d3c3 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
2d3c4 66 6f 72 20 6f 6e 65 2d 70 61 73 73 20 61 6c 67  for one-pass alg
2d3c5 6f 72 69 74 68 6d 20 77 69 74 68 6f 75 74 20 74  orithm without t
2d3c6 68 65 20 46 49 46 4f 20 2a 2f 0a 20 20 69 6e 74  he FIFO */.  int
2d3c7 20 68 61 73 46 4b 3b 20 20 20 20 20 20 20 20 20   hasFK;         
2d3c8 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 66      /* True if f
2d3c9 6f 72 65 69 67 6e 20 6b 65 79 20 70 72 6f 63 65  oreign key proce
2d3ca 73 73 69 6e 67 20 69 73 20 72 65 71 75 69 72 65  ssing is require
2d3cb 64 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  d */..#ifndef SQ
2d3cc 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
2d3cd 52 0a 20 20 69 6e 74 20 69 73 56 69 65 77 3b 20  R.  int isView; 
2d3ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d3cf 20 2f 2a 20 54 72 79 69 6e 67 20 74 6f 20 75 70   /* Trying to up
2d3d0 64 61 74 65 20 61 20 76 69 65 77 20 2a 2f 0a 20  date a view */. 
2d3d1 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67   Trigger *pTrigg
2d3d2 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  er;           /*
2d3d3 20 4c 69 73 74 20 6f 66 20 74 72 69 67 67 65 72   List of trigger
2d3d4 73 20 6f 6e 20 70 54 61 62 2c 20 69 66 20 72 65  s on pTab, if re
2d3d5 71 75 69 72 65 64 20 2a 2f 0a 23 65 6e 64 69 66  quired */.#endif
2d3d6 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20  ..  /* Register 
2d3d7 41 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f 0a 20  Allocations */. 
2d3d8 20 69 6e 74 20 72 65 67 52 6f 77 43 6f 75 6e 74   int regRowCount
2d3d9 20 3d 20 30 3b 20 20 20 2f 2a 20 41 20 63 6f 75   = 0;   /* A cou
2d3da 6e 74 20 6f 66 20 72 6f 77 73 20 63 68 61 6e 67  nt of rows chang
2d3db 65 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4f  ed */.  int regO
2d3dc 6c 64 52 6f 77 69 64 3b 20 20 20 20 20 20 20 2f  ldRowid;       /
2d3dd 2a 20 54 68 65 20 6f 6c 64 20 72 6f 77 69 64 20  * The old rowid 
2d3de 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4e 65 77 52  */.  int regNewR
2d3df 6f 77 69 64 3b 20 20 20 20 20 20 20 2f 2a 20 54  owid;       /* T
2d3e0 68 65 20 6e 65 77 20 72 6f 77 69 64 20 2a 2f 0a  he new rowid */.
2d3e1 20 20 69 6e 74 20 72 65 67 4e 65 77 3b 0a 20 20    int regNew;.  
2d3e2 69 6e 74 20 72 65 67 4f 6c 64 20 3d 20 30 3b 0a  int regOld = 0;.
2d3e3 20 20 69 6e 74 20 72 65 67 52 6f 77 53 65 74 20    int regRowSet 
2d3e4 3d 20 30 3b 20 20 20 20 20 2f 2a 20 52 6f 77 73  = 0;     /* Rows
2d3e5 65 74 20 6f 66 20 72 6f 77 73 20 74 6f 20 62 65  et of rows to be
2d3e6 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20 69 6e   updated */.  in
2d3e7 74 20 72 65 67 52 65 63 3b 20 20 20 20 20 20 20  t regRec;       
2d3e8 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
2d3e9 20 75 73 65 64 20 66 6f 72 20 6e 65 77 20 74 61   used for new ta
2d3ea 62 6c 65 20 72 65 63 6f 72 64 20 74 6f 20 69 6e  ble record to in
2d3eb 73 65 72 74 20 2a 2f 0a 0a 20 20 6d 65 6d 73 65  sert */..  memse
2d3ec 74 28 26 73 43 6f 6e 74 65 78 74 2c 20 30 2c 20  t(&sContext, 0, 
2d3ed 73 69 7a 65 6f 66 28 73 43 6f 6e 74 65 78 74 29  sizeof(sContext)
2d3ee 29 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  );.  db = pParse
2d3ef 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 50 61 72  ->db;.  if( pPar
2d3f0 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e  se->nErr || db->
2d3f1 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
2d3f2 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f      goto update_
2d3f3 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 61  cleanup;.  }.  a
2d3f4 73 73 65 72 74 28 20 70 54 61 62 4c 69 73 74 2d  ssert( pTabList-
2d3f5 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a 0a 20 20 2f  >nSrc==1 );..  /
2d3f6 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61 62  * Locate the tab
2d3f7 6c 65 20 77 68 69 63 68 20 77 65 20 77 61 6e 74  le which we want
2d3f8 20 74 6f 20 75 70 64 61 74 65 2e 20 0a 20 20 2a   to update. .  *
2d3f9 2f 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74  /.  pTab = sqlit
2d3fa 65 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28  e3SrcListLookup(
2d3fb 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74  pParse, pTabList
2d3fc 29 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30  );.  if( pTab==0
2d3fd 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63   ) goto update_c
2d3fe 6c 65 61 6e 75 70 3b 0a 20 20 69 44 62 20 3d 20  leanup;.  iDb = 
2d3ff 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
2d400 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
2d401 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
2d402 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75  ..  /* Figure ou
2d403 74 20 69 66 20 77 65 20 68 61 76 65 20 61 6e 79  t if we have any
2d404 20 74 72 69 67 67 65 72 73 20 61 6e 64 20 69 66   triggers and if
2d405 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67   the table being
2d406 0a 20 20 2a 2a 20 75 70 64 61 74 65 64 20 69 73  .  ** updated is
2d407 20 61 20 76 69 65 77 2e 0a 20 20 2a 2f 0a 23 69   a view..  */.#i
2d408 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2d409 54 5f 54 52 49 47 47 45 52 0a 20 20 70 54 72 69  T_TRIGGER.  pTri
2d40a 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 54 72  gger = sqlite3Tr
2d40b 69 67 67 65 72 73 45 78 69 73 74 28 70 50 61 72  iggersExist(pPar
2d40c 73 65 2c 20 70 54 61 62 2c 20 54 4b 5f 55 50 44  se, pTab, TK_UPD
2d40d 41 54 45 2c 20 70 43 68 61 6e 67 65 73 2c 20 30  ATE, pChanges, 0
2d40e 29 3b 0a 20 20 69 73 56 69 65 77 20 3d 20 70 54  );.  isView = pT
2d40f 61 62 2d 3e 70 53 65 6c 65 63 74 21 3d 30 3b 0a  ab->pSelect!=0;.
2d410 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 70  #else.# define p
2d411 54 72 69 67 67 65 72 20 30 0a 23 20 64 65 66 69  Trigger 0.# defi
2d412 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64  ne isView 0.#end
2d413 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
2d414 5f 4f 4d 49 54 5f 56 49 45 57 0a 23 20 75 6e 64  _OMIT_VIEW.# und
2d415 65 66 20 69 73 56 69 65 77 0a 23 20 64 65 66 69  ef isView.# defi
2d416 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65 6e 64  ne isView 0.#end
2d417 69 66 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65  if..  if( sqlite
2d418 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61  3ViewGetColumnNa
2d419 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  mes(pParse, pTab
2d41a 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 75 70  ) ){.    goto up
2d41b 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  date_cleanup;.  
2d41c 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 49  }.  if( sqlite3I
2d41d 73 52 65 61 64 4f 6e 6c 79 28 70 50 61 72 73 65  sReadOnly(pParse
2d41e 2c 20 70 54 61 62 2c 20 28 70 54 72 69 67 67 65  , pTab, (pTrigge
2d41f 72 3f 31 3a 30 29 29 20 29 7b 0a 20 20 20 20 67  r?1:0)) ){.    g
2d420 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e  oto update_clean
2d421 75 70 3b 0a 20 20 7d 0a 20 20 61 58 52 65 66 20  up;.  }.  aXRef 
2d422 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2d423 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28  cRaw(db, sizeof(
2d424 69 6e 74 29 20 2a 20 70 54 61 62 2d 3e 6e 43 6f  int) * pTab->nCo
2d425 6c 20 29 3b 0a 20 20 69 66 28 20 61 58 52 65 66  l );.  if( aXRef
2d426 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74  ==0 ) goto updat
2d427 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 66 6f 72  e_cleanup;.  for
2d428 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43  (i=0; i<pTab->nC
2d429 6f 6c 3b 20 69 2b 2b 29 20 61 58 52 65 66 5b 69  ol; i++) aXRef[i
2d42a 5d 20 3d 20 2d 31 3b 0a 0a 20 20 2f 2a 20 41 6c  ] = -1;..  /* Al
2d42b 6c 6f 63 61 74 65 20 61 20 63 75 72 73 6f 72 73  locate a cursors
2d42c 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64 61   for the main da
2d42d 74 61 62 61 73 65 20 74 61 62 6c 65 20 61 6e 64  tabase table and
2d42e 20 66 6f 72 20 61 6c 6c 20 69 6e 64 69 63 65 73   for all indices
2d42f 2e 0a 20 20 2a 2a 20 54 68 65 20 69 6e 64 65 78  ..  ** The index
2d430 20 63 75 72 73 6f 72 73 20 6d 69 67 68 74 20 6e   cursors might n
2d431 6f 74 20 62 65 20 75 73 65 64 2c 20 62 75 74 20  ot be used, but 
2d432 69 66 20 74 68 65 79 20 61 72 65 20 75 73 65 64  if they are used
2d433 20 74 68 65 79 0a 20 20 2a 2a 20 6e 65 65 64 20   they.  ** need 
2d434 74 6f 20 6f 63 63 75 72 20 72 69 67 68 74 20 61  to occur right a
2d435 66 74 65 72 20 74 68 65 20 64 61 74 61 62 61 73  fter the databas
2d436 65 20 63 75 72 73 6f 72 2e 20 20 53 6f 20 67 6f  e cursor.  So go
2d437 20 61 68 65 61 64 20 61 6e 64 0a 20 20 2a 2a 20   ahead and.  ** 
2d438 61 6c 6c 6f 63 61 74 65 20 65 6e 6f 75 67 68 20  allocate enough 
2d439 73 70 61 63 65 2c 20 6a 75 73 74 20 69 6e 20 63  space, just in c
2d43a 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 70 54 61 62  ase..  */.  pTab
2d43b 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43 75 72 73  List->a[0].iCurs
2d43c 6f 72 20 3d 20 69 43 75 72 20 3d 20 70 50 61 72  or = iCur = pPar
2d43d 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 66 6f  se->nTab++;.  fo
2d43e 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  r(pIdx=pTab->pIn
2d43f 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
2d440 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  pIdx->pNext){.  
2d441 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b    pParse->nTab++
2d442 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 6e 69 74  ;.  }..  /* Init
2d443 69 61 6c 69 7a 65 20 74 68 65 20 6e 61 6d 65 2d  ialize the name-
2d444 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 6d 65 6d  context */.  mem
2d445 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a  set(&sNC, 0, siz
2d446 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43  eof(sNC));.  sNC
2d447 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
2d448 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74  ;.  sNC.pSrcList
2d449 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a 0a 20 20   = pTabList;..  
2d44a 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65 20 63  /* Resolve the c
2d44b 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 61  olumn names in a
2d44c 6c 6c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  ll the expressio
2d44d 6e 73 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6f  ns of the.  ** o
2d44e 66 20 74 68 65 20 55 50 44 41 54 45 20 73 74 61  f the UPDATE sta
2d44f 74 65 6d 65 6e 74 2e 20 20 41 6c 73 6f 20 66 69  tement.  Also fi
2d450 6e 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e  nd the column in
2d451 64 65 78 0a 20 20 2a 2a 20 66 6f 72 20 65 61 63  dex.  ** for eac
2d452 68 20 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 75  h column to be u
2d453 70 64 61 74 65 64 20 69 6e 20 74 68 65 20 70 43  pdated in the pC
2d454 68 61 6e 67 65 73 20 61 72 72 61 79 2e 20 20 46  hanges array.  F
2d455 6f 72 20 65 61 63 68 0a 20 20 2a 2a 20 63 6f 6c  or each.  ** col
2d456 75 6d 6e 20 74 6f 20 62 65 20 75 70 64 61 74 65  umn to be update
2d457 64 2c 20 6d 61 6b 65 20 73 75 72 65 20 77 65 20  d, make sure we 
2d458 68 61 76 65 20 61 75 74 68 6f 72 69 7a 61 74 69  have authorizati
2d459 6f 6e 20 74 6f 20 63 68 61 6e 67 65 0a 20 20 2a  on to change.  *
2d45a 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2e 0a 20  * that column.. 
2d45b 20 2a 2f 0a 20 20 63 68 6e 67 52 6f 77 69 64 20   */.  chngRowid 
2d45c 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  = 0;.  for(i=0; 
2d45d 69 3c 70 43 68 61 6e 67 65 73 2d 3e 6e 45 78 70  i<pChanges->nExp
2d45e 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  r; i++){.    if(
2d45f 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
2d460 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70  xprNames(&sNC, p
2d461 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70 45  Changes->a[i].pE
2d462 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  xpr) ){.      go
2d463 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75  to update_cleanu
2d464 70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  p;.    }.    for
2d465 28 6a 3d 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43  (j=0; j<pTab->nC
2d466 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; j++){.      
2d467 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
2d468 6d 70 28 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d  mp(pTab->aCol[j]
2d469 2e 7a 4e 61 6d 65 2c 20 70 43 68 61 6e 67 65 73  .zName, pChanges
2d46a 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3d 3d 30  ->a[i].zName)==0
2d46b 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
2d46c 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20 29  j==pTab->iPKey )
2d46d 7b 0a 20 20 20 20 20 20 20 20 20 20 63 68 6e 67  {.          chng
2d46e 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 20  Rowid = 1;.     
2d46f 20 20 20 20 20 70 52 6f 77 69 64 45 78 70 72 20       pRowidExpr 
2d470 3d 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d  = pChanges->a[i]
2d471 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20  .pExpr;.        
2d472 7d 0a 20 20 20 20 20 20 20 20 61 58 52 65 66 5b  }.        aXRef[
2d473 6a 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 20 20  j] = i;.        
2d474 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
2d475 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6a 3e 3d     }.    if( j>=
2d476 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20  pTab->nCol ){.  
2d477 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 49      if( sqlite3I
2d478 73 52 6f 77 69 64 28 70 43 68 61 6e 67 65 73 2d  sRowid(pChanges-
2d479 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 20 29 7b 0a  >a[i].zName) ){.
2d47a 20 20 20 20 20 20 20 20 63 68 6e 67 52 6f 77 69          chngRowi
2d47b 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70  d = 1;.        p
2d47c 52 6f 77 69 64 45 78 70 72 20 3d 20 70 43 68 61  RowidExpr = pCha
2d47d 6e 67 65 73 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  nges->a[i].pExpr
2d47e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2d47f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72         sqlite3Er
2d480 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2d481 6e 6f 20 73 75 63 68 20 63 6f 6c 75 6d 6e 3a 20  no such column: 
2d482 25 73 22 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61  %s", pChanges->a
2d483 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  [i].zName);.    
2d484 20 20 20 20 67 6f 74 6f 20 75 70 64 61 74 65 5f      goto update_
2d485 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 7d  cleanup;.      }
2d486 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53  .    }.#ifndef S
2d487 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
2d488 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 7b 0a 20  RIZATION.    {. 
2d489 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20       int rc;.   
2d48a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41     rc = sqlite3A
2d48b 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
2d48c 20 53 51 4c 49 54 45 5f 55 50 44 41 54 45 2c 20   SQLITE_UPDATE, 
2d48d 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20  pTab->zName,.   
2d48e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d48f 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61 43          pTab->aC
2d490 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c 20 64 62 2d  ol[j].zName, db-
2d491 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29  >aDb[iDb].zName)
2d492 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
2d493 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20  SQLITE_DENY ){. 
2d494 20 20 20 20 20 20 20 67 6f 74 6f 20 75 70 64 61         goto upda
2d495 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  te_cleanup;.    
2d496 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d    }else if( rc==
2d497 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 29 7b  SQLITE_IGNORE ){
2d498 0a 20 20 20 20 20 20 20 20 61 58 52 65 66 5b 6a  .        aXRef[j
2d499 5d 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a  ] = -1;.      }.
2d49a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d      }.#endif.  }
2d49b 0a 0a 20 20 68 61 73 46 4b 20 3d 20 73 71 6c 69  ..  hasFK = sqli
2d49c 74 65 33 46 6b 52 65 71 75 69 72 65 64 28 70 50  te3FkRequired(pP
2d49d 61 72 73 65 2c 20 70 54 61 62 2c 20 61 58 52 65  arse, pTab, aXRe
2d49e 66 2c 20 63 68 6e 67 52 6f 77 69 64 29 3b 0a 0a  f, chngRowid);..
2d49f 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65    /* Allocate me
2d4a0 6d 6f 72 79 20 66 6f 72 20 74 68 65 20 61 72 72  mory for the arr
2d4a1 61 79 20 61 52 65 67 49 64 78 5b 5d 2e 20 20 54  ay aRegIdx[].  T
2d4a2 68 65 72 65 20 69 73 20 6f 6e 65 20 65 6e 74 72  here is one entr
2d4a3 79 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 61 72  y in the.  ** ar
2d4a4 72 61 79 20 66 6f 72 20 65 61 63 68 20 69 6e 64  ray for each ind
2d4a5 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ex associated wi
2d4a6 74 68 20 74 61 62 6c 65 20 62 65 69 6e 67 20 75  th table being u
2d4a7 70 64 61 74 65 64 2e 20 20 46 69 6c 6c 20 69 6e  pdated.  Fill in
2d4a8 0a 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20  .  ** the value 
2d4a9 77 69 74 68 20 61 20 72 65 67 69 73 74 65 72 20  with a register 
2d4aa 6e 75 6d 62 65 72 20 66 6f 72 20 69 6e 64 69 63  number for indic
2d4ab 65 73 20 74 68 61 74 20 61 72 65 20 74 6f 20 62  es that are to b
2d4ac 65 20 75 73 65 64 0a 20 20 2a 2a 20 61 6e 64 20  e used.  ** and 
2d4ad 77 69 74 68 20 7a 65 72 6f 20 66 6f 72 20 75 6e  with zero for un
2d4ae 75 73 65 64 20 69 6e 64 69 63 65 73 2e 0a 20 20  used indices..  
2d4af 2a 2f 0a 20 20 66 6f 72 28 6e 49 64 78 3d 30 2c  */.  for(nIdx=0,
2d4b0 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64   pIdx=pTab->pInd
2d4b1 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70  ex; pIdx; pIdx=p
2d4b2 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6e 49 64 78  Idx->pNext, nIdx
2d4b3 2b 2b 29 7b 7d 0a 20 20 69 66 28 20 6e 49 64 78  ++){}.  if( nIdx
2d4b4 3e 30 20 29 7b 0a 20 20 20 20 61 52 65 67 49 64  >0 ){.    aRegId
2d4b5 78 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  x = sqlite3DbMal
2d4b6 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
2d4b7 66 28 49 6e 64 65 78 2a 29 20 2a 20 6e 49 64 78  f(Index*) * nIdx
2d4b8 20 29 3b 0a 20 20 20 20 69 66 28 20 61 52 65 67   );.    if( aReg
2d4b9 49 64 78 3d 3d 30 20 29 20 67 6f 74 6f 20 75 70  Idx==0 ) goto up
2d4ba 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  date_cleanup;.  
2d4bb 7d 0a 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 64  }.  for(j=0, pId
2d4bc 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20  x=pTab->pIndex; 
2d4bd 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d  pIdx; pIdx=pIdx-
2d4be 3e 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20 20  >pNext, j++){.  
2d4bf 20 20 69 6e 74 20 72 65 67 3b 0a 20 20 20 20 69    int reg;.    i
2d4c0 66 28 20 63 68 6e 67 52 6f 77 69 64 20 29 7b 0a  f( chngRowid ){.
2d4c1 20 20 20 20 20 20 72 65 67 20 3d 20 2b 2b 70 50        reg = ++pP
2d4c2 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
2d4c3 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 67  }else{.      reg
2d4c4 20 3d 20 30 3b 0a 20 20 20 20 20 20 66 6f 72 28   = 0;.      for(
2d4c5 69 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f  i=0; i<pIdx->nCo
2d4c6 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  lumn; i++){.    
2d4c7 20 20 20 20 69 66 28 20 61 58 52 65 66 5b 70 49      if( aXRef[pI
2d4c8 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 5d  dx->aiColumn[i]]
2d4c9 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  >=0 ){.         
2d4ca 20 72 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d   reg = ++pParse-
2d4cb 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20  >nMem;.         
2d4cc 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
2d4cd 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
2d4ce 20 20 20 20 61 52 65 67 49 64 78 5b 6a 5d 20 3d      aRegIdx[j] =
2d4cf 20 72 65 67 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   reg;.  }..  /* 
2d4d0 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69 6e 67  Begin generating
2d4d1 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 76 20 3d 20   code. */.  v = 
2d4d2 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
2d4d3 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d  Parse);.  if( v=
2d4d4 3d 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65  =0 ) goto update
2d4d5 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 69 66 28 20  _cleanup;.  if( 
2d4d6 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
2d4d7 30 20 29 20 73 71 6c 69 74 65 33 56 64 62 65 43  0 ) sqlite3VdbeC
2d4d8 6f 75 6e 74 43 68 61 6e 67 65 73 28 76 29 3b 0a  ountChanges(v);.
2d4d9 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72    sqlite3BeginWr
2d4da 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61  iteOperation(pPa
2d4db 72 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 0a 23  rse, 1, iDb);..#
2d4dc 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2d4dd 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
2d4de 20 20 2f 2a 20 56 69 72 74 75 61 6c 20 74 61 62    /* Virtual tab
2d4df 6c 65 73 20 6d 75 73 74 20 62 65 20 68 61 6e 64  les must be hand
2d4e0 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79 20 2a  led separately *
2d4e1 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61  /.  if( IsVirtua
2d4e2 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 75  l(pTab) ){.    u
2d4e3 70 64 61 74 65 56 69 72 74 75 61 6c 54 61 62 6c  pdateVirtualTabl
2d4e4 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 4c 69  e(pParse, pTabLi
2d4e5 73 74 2c 20 70 54 61 62 2c 20 70 43 68 61 6e 67  st, pTab, pChang
2d4e6 65 73 2c 20 70 52 6f 77 69 64 45 78 70 72 2c 20  es, pRowidExpr, 
2d4e7 61 58 52 65 66 2c 0a 20 20 20 20 20 20 20 20 20  aXRef,.         
2d4e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 57                pW
2d4e9 68 65 72 65 29 3b 0a 20 20 20 20 70 57 68 65 72  here);.    pWher
2d4ea 65 20 3d 20 30 3b 0a 20 20 20 20 70 54 61 62 4c  e = 0;.    pTabL
2d4eb 69 73 74 20 3d 20 30 3b 0a 20 20 20 20 67 6f 74  ist = 0;.    got
2d4ec 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70  o update_cleanup
2d4ed 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
2d4ee 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 72 65 71 75  /* Allocate requ
2d4ef 69 72 65 64 20 72 65 67 69 73 74 65 72 73 2e 20  ired registers. 
2d4f0 2a 2f 0a 20 20 72 65 67 4f 6c 64 52 6f 77 69 64  */.  regOldRowid
2d4f1 20 3d 20 72 65 67 4e 65 77 52 6f 77 69 64 20 3d   = regNewRowid =
2d4f2 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
2d4f3 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72 20  .  if( pTrigger 
2d4f4 7c 7c 20 68 61 73 46 4b 20 29 7b 0a 20 20 20 20  || hasFK ){.    
2d4f5 72 65 67 4f 6c 64 20 3d 20 70 50 61 72 73 65 2d  regOld = pParse-
2d4f6 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 20 20 70  >nMem + 1;.    p
2d4f7 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70  Parse->nMem += p
2d4f8 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 7d 0a 20  Tab->nCol;.  }. 
2d4f9 20 69 66 28 20 63 68 6e 67 52 6f 77 69 64 20 7c   if( chngRowid |
2d4fa 7c 20 70 54 72 69 67 67 65 72 20 7c 7c 20 68 61  | pTrigger || ha
2d4fb 73 46 4b 20 29 7b 0a 20 20 20 20 72 65 67 4e 65  sFK ){.    regNe
2d4fc 77 52 6f 77 69 64 20 3d 20 2b 2b 70 50 61 72 73  wRowid = ++pPars
2d4fd 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72  e->nMem;.  }.  r
2d4fe 65 67 4e 65 77 20 3d 20 70 50 61 72 73 65 2d 3e  egNew = pParse->
2d4ff 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 70 50 61 72  nMem + 1;.  pPar
2d500 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 70 54 61 62  se->nMem += pTab
2d501 2d 3e 6e 43 6f 6c 3b 0a 20 20 72 65 67 52 65 63  ->nCol;.  regRec
2d502 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
2d503 6d 3b 0a 0a 20 20 2f 2a 20 53 74 61 72 74 20 74  m;..  /* Start t
2d504 68 65 20 76 69 65 77 20 63 6f 6e 74 65 78 74 2e  he view context.
2d505 20 2a 2f 0a 20 20 69 66 28 20 69 73 56 69 65 77   */.  if( isView
2d506 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41   ){.    sqlite3A
2d507 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28 70  uthContextPush(p
2d508 50 61 72 73 65 2c 20 26 73 43 6f 6e 74 65 78 74  Parse, &sContext
2d509 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
2d50a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20    }..  /* If we 
2d50b 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20 75 70  are trying to up
2d50c 64 61 74 65 20 61 20 76 69 65 77 2c 20 72 65 61  date a view, rea
2d50d 6c 69 7a 65 20 74 68 61 74 20 76 69 65 77 20 69  lize that view i
2d50e 6e 74 6f 0a 20 20 2a 2a 20 61 20 65 70 68 65 6d  nto.  ** a ephem
2d50f 65 72 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f  eral table..  */
2d510 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
2d511 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
2d512 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
2d513 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29  TE_OMIT_TRIGGER)
2d514 0a 20 20 69 66 28 20 69 73 56 69 65 77 20 29 7b  .  if( isView ){
2d515 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61 74 65  .    sqlite3Mate
2d516 72 69 61 6c 69 7a 65 56 69 65 77 28 70 50 61 72  rializeView(pPar
2d517 73 65 2c 20 70 54 61 62 2c 20 70 57 68 65 72 65  se, pTab, pWhere
2d518 2c 20 69 43 75 72 29 3b 0a 20 20 7d 0a 23 65 6e  , iCur);.  }.#en
2d519 64 69 66 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76  dif..  /* Resolv
2d51a 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d  e the column nam
2d51b 65 73 20 69 6e 20 61 6c 6c 20 74 68 65 20 65 78  es in all the ex
2d51c 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68 65  pressions in the
2d51d 0a 20 20 2a 2a 20 57 48 45 52 45 20 63 6c 61 75  .  ** WHERE clau
2d51e 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73  se..  */.  if( s
2d51f 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70  qlite3ResolveExp
2d520 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 57 68  rNames(&sNC, pWh
2d521 65 72 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  ere) ){.    goto
2d522 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b   update_cleanup;
2d523 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e  .  }..  /* Begin
2d524 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
2d525 61 6e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  an.  */.  sqlite
2d526 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2d527 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 4f 6c  P_Null, 0, regOl
2d528 64 52 6f 77 69 64 29 3b 0a 20 20 70 57 49 6e 66  dRowid);.  pWInf
2d529 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65  o = sqlite3Where
2d52a 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54  Begin(pParse, pT
2d52b 61 62 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 30  abList, pWhere,0
2d52c 2c 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 53 5f  , WHERE_ONEPASS_
2d52d 44 45 53 49 52 45 44 29 3b 0a 20 20 69 66 28 20  DESIRED);.  if( 
2d52e 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f  pWInfo==0 ) goto
2d52f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b   update_cleanup;
2d530 0a 20 20 6f 6b 4f 6e 65 50 61 73 73 20 3d 20 70  .  okOnePass = p
2d531 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73  WInfo->okOnePass
2d532 3b 0a 0a 20 20 2f 2a 20 52 65 6d 65 6d 62 65 72  ;..  /* Remember
2d533 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 65 76   the rowid of ev
2d534 65 72 79 20 69 74 65 6d 20 74 6f 20 62 65 20 75  ery item to be u
2d535 70 64 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 73  pdated..  */.  s
2d536 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2d537 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43  (v, OP_Rowid, iC
2d538 75 72 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29  ur, regOldRowid)
2d539 3b 0a 20 20 69 66 28 20 21 6f 6b 4f 6e 65 50 61  ;.  if( !okOnePa
2d53a 73 73 20 29 7b 0a 20 20 20 20 72 65 67 52 6f 77  ss ){.    regRow
2d53b 53 65 74 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  Set = ++pParse->
2d53c 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65  nMem;.    sqlite
2d53d 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2d53e 50 5f 52 6f 77 53 65 74 41 64 64 2c 20 72 65 67  P_RowSetAdd, reg
2d53f 52 6f 77 53 65 74 2c 20 72 65 67 4f 6c 64 52 6f  RowSet, regOldRo
2d540 77 69 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  wid);.  }..  /* 
2d541 45 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65  End the database
2d542 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20 2a 2f   scan loop..  */
2d543 0a 20 20 73 71 6c 69 74 65 33 57 68 65 72 65 45  .  sqlite3WhereE
2d544 6e 64 28 70 57 49 6e 66 6f 29 3b 0a 0a 20 20 2f  nd(pWInfo);..  /
2d545 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
2d546 20 63 6f 75 6e 74 20 6f 66 20 75 70 64 61 74 65   count of update
2d547 64 20 72 6f 77 73 0a 20 20 2a 2f 0a 20 20 69 66  d rows.  */.  if
2d548 28 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  ( (db->flags & S
2d549 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 29  QLITE_CountRows)
2d54a 20 26 26 20 21 70 50 61 72 73 65 2d 3e 70 54 72   && !pParse->pTr
2d54b 69 67 67 65 72 54 61 62 20 29 7b 0a 20 20 20 20  iggerTab ){.    
2d54c 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20 2b 2b  regRowCount = ++
2d54d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2d54e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2d54f 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
2d550 72 2c 20 30 2c 20 72 65 67 52 6f 77 43 6f 75 6e  r, 0, regRowCoun
2d551 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21  t);.  }..  if( !
2d552 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 2f 2a  isView ){.    /*
2d553 20 0a 20 20 20 20 2a 2a 20 4f 70 65 6e 20 65 76   .    ** Open ev
2d554 65 72 79 20 69 6e 64 65 78 20 74 68 61 74 20 6e  ery index that n
2d555 65 65 64 73 20 75 70 64 61 74 69 6e 67 2e 20 20  eeds updating.  
2d556 4e 6f 74 65 20 74 68 61 74 20 69 66 20 61 6e 79  Note that if any
2d557 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78 20 63 6f  .    ** index co
2d558 75 6c 64 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20  uld potentially 
2d559 69 6e 76 6f 6b 65 20 61 20 52 45 50 4c 41 43 45  invoke a REPLACE
2d55a 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75   conflict resolu
2d55b 74 69 6f 6e 20 0a 20 20 20 20 2a 2a 20 61 63 74  tion .    ** act
2d55c 69 6f 6e 2c 20 74 68 65 6e 20 77 65 20 6e 65 65  ion, then we nee
2d55d 64 20 74 6f 20 6f 70 65 6e 20 61 6c 6c 20 69 6e  d to open all in
2d55e 64 69 63 65 73 20 62 65 63 61 75 73 65 20 77 65  dices because we
2d55f 20 6d 69 67 68 74 20 6e 65 65 64 0a 20 20 20 20   might need.    
2d560 2a 2a 20 74 6f 20 62 65 20 64 65 6c 65 74 69 6e  ** to be deletin
2d561 67 20 73 6f 6d 65 20 72 65 63 6f 72 64 73 2e 0a  g some records..
2d562 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
2d563 6f 6b 4f 6e 65 50 61 73 73 20 29 20 73 71 6c 69  okOnePass ) sqli
2d564 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61  te3OpenTable(pPa
2d565 72 73 65 2c 20 69 43 75 72 2c 20 69 44 62 2c 20  rse, iCur, iDb, 
2d566 70 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  pTab, OP_OpenWri
2d567 74 65 29 3b 20 0a 20 20 20 20 69 66 28 20 6f 6e  te); .    if( on
2d568 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63  Error==OE_Replac
2d569 65 20 29 7b 0a 20 20 20 20 20 20 6f 70 65 6e 41  e ){.      openA
2d56a 6c 6c 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73  ll = 1;.    }els
2d56b 65 7b 0a 20 20 20 20 20 20 6f 70 65 6e 41 6c 6c  e{.      openAll
2d56c 20 3d 20 30 3b 0a 20 20 20 20 20 20 66 6f 72 28   = 0;.      for(
2d56d 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
2d56e 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49  x; pIdx; pIdx=pI
2d56f 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  dx->pNext){.    
2d570 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 6f 6e      if( pIdx->on
2d571 45 72 72 6f 72 3d 3d 4f 45 5f 52 65 70 6c 61 63  Error==OE_Replac
2d572 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6f  e ){.          o
2d573 70 65 6e 41 6c 6c 20 3d 20 31 3b 0a 20 20 20 20  penAll = 1;.    
2d574 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2d575 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
2d576 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30     }.    for(i=0
2d577 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e  , pIdx=pTab->pIn
2d578 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78 3d  dex; pIdx; pIdx=
2d579 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b  pIdx->pNext, i++
2d57a 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6f 70 65  ){.      if( ope
2d57b 6e 41 6c 6c 20 7c 7c 20 61 52 65 67 49 64 78 5b  nAll || aRegIdx[
2d57c 69 5d 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  i]>0 ){.        
2d57d 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d 20  KeyInfo *pKey = 
2d57e 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69  sqlite3IndexKeyi
2d57f 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 64 78  nfo(pParse, pIdx
2d580 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2d581 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2d582 4f 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 43  OP_OpenWrite, iC
2d583 75 72 2b 69 2b 31 2c 20 70 49 64 78 2d 3e 74 6e  ur+i+1, pIdx->tn
2d584 75 6d 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20  um, iDb,.       
2d585 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d586 28 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f  (char*)pKey, P4_
2d587 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29  KEYINFO_HANDOFF)
2d588 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
2d589 28 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3e 69  ( pParse->nTab>i
2d58a 43 75 72 2b 69 2b 31 20 29 3b 0a 20 20 20 20 20  Cur+i+1 );.     
2d58b 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20   }.    }.  }..  
2d58c 2f 2a 20 54 6f 70 20 6f 66 20 74 68 65 20 75 70  /* Top of the up
2d58d 64 61 74 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69  date loop */.  i
2d58e 66 28 20 6f 6b 4f 6e 65 50 61 73 73 20 29 7b 0a  f( okOnePass ){.
2d58f 20 20 20 20 69 6e 74 20 61 31 20 3d 20 73 71 6c      int a1 = sql
2d590 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2d591 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65  , OP_NotNull, re
2d592 67 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 20 20  gOldRowid);.    
2d593 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
2d594 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47  beAddOp0(v, OP_G
2d595 6f 74 6f 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  oto);.    sqlite
2d596 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2d597 20 61 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   a1);.  }else{. 
2d598 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
2d599 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2d59a 50 5f 52 6f 77 53 65 74 52 65 61 64 2c 20 72 65  P_RowSetRead, re
2d59b 67 52 6f 77 53 65 74 2c 20 30 2c 20 72 65 67 4f  gRowSet, 0, regO
2d59c 6c 64 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 0a 20  ldRowid);.  }.. 
2d59d 20 2f 2a 20 4d 61 6b 65 20 63 75 72 73 6f 72 20   /* Make cursor 
2d59e 69 43 75 72 20 70 6f 69 6e 74 20 74 6f 20 74 68  iCur point to th
2d59f 65 20 72 65 63 6f 72 64 20 74 68 61 74 20 69 73  e record that is
2d5a0 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 2e 20   being updated. 
2d5a1 49 66 0a 20 20 2a 2a 20 74 68 69 73 20 72 65 63  If.  ** this rec
2d5a2 6f 72 64 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  ord does not exi
2d5a3 73 74 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73  st for some reas
2d5a4 6f 6e 20 28 64 65 6c 65 74 65 64 20 62 79 20 61  on (deleted by a
2d5a5 20 74 72 69 67 67 65 72 2c 0a 20 20 2a 2a 20 66   trigger,.  ** f
2d5a6 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 6e  or example, then
2d5a7 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 6e 65 78   jump to the nex
2d5a8 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74  t iteration of t
2d5a9 68 65 20 52 6f 77 53 65 74 20 6c 6f 6f 70 2e 20  he RowSet loop. 
2d5aa 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62   */.  sqlite3Vdb
2d5ab 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f  eAddOp3(v, OP_No
2d5ac 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20 61  tExists, iCur, a
2d5ad 64 64 72 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64  ddr, regOldRowid
2d5ae 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
2d5af 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 77 69  record number wi
2d5b0 6c 6c 20 63 68 61 6e 67 65 2c 20 73 65 74 20 72  ll change, set r
2d5b1 65 67 69 73 74 65 72 20 72 65 67 4e 65 77 52 6f  egister regNewRo
2d5b2 77 69 64 20 74 6f 0a 20 20 2a 2a 20 63 6f 6e 74  wid to.  ** cont
2d5b3 61 69 6e 20 74 68 65 20 6e 65 77 20 76 61 6c 75  ain the new valu
2d5b4 65 2e 20 49 66 20 74 68 65 20 72 65 63 6f 72 64  e. If the record
2d5b5 20 6e 75 6d 62 65 72 20 69 73 20 6e 6f 74 20 62   number is not b
2d5b6 65 69 6e 67 20 6d 6f 64 69 66 69 65 64 2c 0a 20  eing modified,. 
2d5b7 20 2a 2a 20 74 68 65 6e 20 72 65 67 4e 65 77 52   ** then regNewR
2d5b8 6f 77 69 64 20 69 73 20 74 68 65 20 73 61 6d 65  owid is the same
2d5b9 20 72 65 67 69 73 74 65 72 20 61 73 20 72 65 67   register as reg
2d5ba 4f 6c 64 52 6f 77 69 64 2c 20 77 68 69 63 68 20  OldRowid, which 
2d5bb 69 73 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20  is.  ** already 
2d5bc 70 6f 70 75 6c 61 74 65 64 2e 20 20 2a 2f 0a 20  populated.  */. 
2d5bd 20 61 73 73 65 72 74 28 20 63 68 6e 67 52 6f 77   assert( chngRow
2d5be 69 64 20 7c 7c 20 70 54 72 69 67 67 65 72 20 7c  id || pTrigger |
2d5bf 7c 20 68 61 73 46 4b 20 7c 7c 20 72 65 67 4f 6c  | hasFK || regOl
2d5c0 64 52 6f 77 69 64 3d 3d 72 65 67 4e 65 77 52 6f  dRowid==regNewRo
2d5c1 77 69 64 20 29 3b 0a 20 20 69 66 28 20 63 68 6e  wid );.  if( chn
2d5c2 67 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 73 71  gRowid ){.    sq
2d5c3 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
2d5c4 61 72 73 65 2c 20 70 52 6f 77 69 64 45 78 70 72  arse, pRowidExpr
2d5c5 2c 20 72 65 67 4e 65 77 52 6f 77 69 64 29 3b 0a  , regNewRowid);.
2d5c6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2d5c7 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74  ddOp1(v, OP_Must
2d5c8 42 65 49 6e 74 2c 20 72 65 67 4e 65 77 52 6f 77  BeInt, regNewRow
2d5c9 69 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  id);.  }..  /* I
2d5ca 66 20 74 68 65 72 65 20 61 72 65 20 74 72 69 67  f there are trig
2d5cb 67 65 72 73 20 6f 6e 20 74 68 69 73 20 74 61 62  gers on this tab
2d5cc 6c 65 2c 20 70 6f 70 75 6c 61 74 65 20 61 6e 20  le, populate an 
2d5cd 61 72 72 61 79 20 6f 66 20 72 65 67 69 73 74 65  array of registe
2d5ce 72 73 20 0a 20 20 2a 2a 20 77 69 74 68 20 74 68  rs .  ** with th
2d5cf 65 20 72 65 71 75 69 72 65 64 20 6f 6c 64 2e 2a  e required old.*
2d5d0 20 63 6f 6c 75 6d 6e 20 64 61 74 61 2e 20 20 2a   column data.  *
2d5d1 2f 0a 20 20 69 66 28 20 68 61 73 46 4b 20 7c 7c  /.  if( hasFK ||
2d5d2 20 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20   pTrigger ){.   
2d5d3 20 75 33 32 20 6f 6c 64 6d 61 73 6b 20 3d 20 28   u32 oldmask = (
2d5d4 68 61 73 46 4b 20 3f 20 73 71 6c 69 74 65 33 46  hasFK ? sqlite3F
2d5d5 6b 4f 6c 64 6d 61 73 6b 28 70 50 61 72 73 65 2c  kOldmask(pParse,
2d5d6 20 70 54 61 62 29 20 3a 20 30 29 3b 0a 20 20 20   pTab) : 0);.   
2d5d7 20 6f 6c 64 6d 61 73 6b 20 7c 3d 20 73 71 6c 69   oldmask |= sqli
2d5d8 74 65 33 54 72 69 67 67 65 72 4f 6c 64 6d 61 73  te3TriggerOldmas
2d5d9 6b 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67  k(pParse, pTrigg
2d5da 65 72 2c 20 70 43 68 61 6e 67 65 73 2c 20 70 54  er, pChanges, pT
2d5db 61 62 2c 20 6f 6e 45 72 72 6f 72 29 3b 0a 20 20  ab, onError);.  
2d5dc 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61    for(i=0; i<pTa
2d5dd 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20  b->nCol; i++){. 
2d5de 20 20 20 20 20 69 66 28 20 61 58 52 65 66 5b 69       if( aXRef[i
2d5df 5d 3c 30 20 7c 7c 20 6f 6c 64 6d 61 73 6b 3d 3d  ]<0 || oldmask==
2d5e0 30 78 66 66 66 66 66 66 66 66 20 7c 7c 20 28 6f  0xffffffff || (o
2d5e1 6c 64 6d 61 73 6b 20 26 20 28 31 3c 3c 69 29 29  ldmask & (1<<i))
2d5e2 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
2d5e3 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
2d5e4 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72   OP_Column, iCur
2d5e5 2c 20 69 2c 20 72 65 67 4f 6c 64 2b 69 29 3b 0a  , i, regOld+i);.
2d5e6 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43          sqlite3C
2d5e7 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20  olumnDefault(v, 
2d5e8 70 54 61 62 2c 20 69 2c 20 72 65 67 4f 6c 64 2b  pTab, i, regOld+
2d5e9 69 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  i);.      }else{
2d5ea 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2d5eb 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2d5ec 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 4f 6c 64  _Null, 0, regOld
2d5ed 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  +i);.      }.   
2d5ee 20 7d 0a 20 20 20 20 69 66 28 20 63 68 6e 67 52   }.    if( chngR
2d5ef 6f 77 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20  owid==0 ){.     
2d5f0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2d5f1 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72  p2(v, OP_Copy, r
2d5f2 65 67 4f 6c 64 52 6f 77 69 64 2c 20 72 65 67 4e  egOldRowid, regN
2d5f3 65 77 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a  ewRowid);.    }.
2d5f4 20 20 7d 0a 0a 20 20 2f 2a 20 50 6f 70 75 6c 61    }..  /* Popula
2d5f5 74 65 20 74 68 65 20 61 72 72 61 79 20 6f 66 20  te the array of 
2d5f6 72 65 67 69 73 74 65 72 73 20 62 65 67 69 6e 6e  registers beginn
2d5f7 69 6e 67 20 61 74 20 72 65 67 4e 65 77 20 77 69  ing at regNew wi
2d5f8 74 68 20 74 68 65 20 6e 65 77 0a 20 20 2a 2a 20  th the new.  ** 
2d5f9 72 6f 77 20 64 61 74 61 2e 20 54 68 69 73 20 61  row data. This a
2d5fa 72 72 61 79 20 69 73 20 75 73 65 64 20 74 6f 20  rray is used to 
2d5fb 63 68 65 63 6b 20 63 6f 6e 73 74 61 69 6e 74 73  check constaints
2d5fc 2c 20 63 72 65 61 74 65 20 74 68 65 20 6e 65 77  , create the new
2d5fd 0a 20 20 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20  .  ** table and 
2d5fe 69 6e 64 65 78 20 72 65 63 6f 72 64 73 2c 20 61  index records, a
2d5ff 6e 64 20 61 73 20 74 68 65 20 76 61 6c 75 65 73  nd as the values
2d600 20 66 6f 72 20 61 6e 79 20 6e 65 77 2e 2a 20 72   for any new.* r
2d601 65 66 65 72 65 6e 63 65 73 0a 20 20 2a 2a 20 6d  eferences.  ** m
2d602 61 64 65 20 62 79 20 74 72 69 67 67 65 72 73 2e  ade by triggers.
2d603 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20    */.  for(i=0; 
2d604 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b  i<pTab->nCol; i+
2d605 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 3d 3d 70  +){.    if( i==p
2d606 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20  Tab->iPKey ){.  
2d607 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2d608 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
2d609 2c 20 30 2c 20 72 65 67 4e 65 77 2b 69 29 3b 0a  , 0, regNew+i);.
2d60a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2d60b 20 6a 20 3d 20 61 58 52 65 66 5b 69 5d 3b 0a 20   j = aXRef[i];. 
2d60c 20 20 20 20 20 69 66 28 20 6a 3c 30 20 29 7b 0a       if( j<0 ){.
2d60d 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2d60e 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2d60f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69 2c  Column, iCur, i,
2d610 20 72 65 67 4e 65 77 2b 69 29 3b 0a 20 20 20 20   regNew+i);.    
2d611 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d      sqlite3Colum
2d612 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61 62  nDefault(v, pTab
2d613 2c 20 69 2c 20 72 65 67 4e 65 77 2b 69 29 3b 0a  , i, regNew+i);.
2d614 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2d615 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2d616 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 43 68  Code(pParse, pCh
2d617 61 6e 67 65 73 2d 3e 61 5b 6a 5d 2e 70 45 78 70  anges->a[j].pExp
2d618 72 2c 20 72 65 67 4e 65 77 2b 69 29 3b 0a 20 20  r, regNew+i);.  
2d619 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
2d61a 0a 20 20 2f 2a 20 46 69 72 65 20 61 6e 79 20 42  .  /* Fire any B
2d61b 45 46 4f 52 45 20 55 50 44 41 54 45 20 74 72 69  EFORE UPDATE tri
2d61c 67 67 65 72 73 2e 20 54 68 69 73 20 68 61 70 70  ggers. This happ
2d61d 65 6e 73 20 62 65 66 6f 72 65 20 63 6f 6e 73 74  ens before const
2d61e 72 61 69 6e 74 73 20 61 72 65 0a 20 20 2a 2a 20  raints are.  ** 
2d61f 76 65 72 69 66 69 65 64 2e 20 4f 6e 65 20 63 6f  verified. One co
2d620 75 6c 64 20 61 72 67 75 65 20 74 68 61 74 20 74  uld argue that t
2d621 68 69 73 20 69 73 20 77 72 6f 6e 67 2e 20 20 2a  his is wrong.  *
2d622 2f 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72  /.  if( pTrigger
2d623 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
2d624 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2d625 41 66 66 69 6e 69 74 79 2c 20 72 65 67 4e 65 77  Affinity, regNew
2d626 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20  , pTab->nCol);. 
2d627 20 20 20 73 71 6c 69 74 65 33 54 61 62 6c 65 41     sqlite3TableA
2d628 66 66 69 6e 69 74 79 53 74 72 28 76 2c 20 70 54  ffinityStr(v, pT
2d629 61 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ab);.    sqlite3
2d62a 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70  CodeRowTrigger(p
2d62b 50 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 2c  Parse, pTrigger,
2d62c 20 54 4b 5f 55 50 44 41 54 45 2c 20 70 43 68 61   TK_UPDATE, pCha
2d62d 6e 67 65 73 2c 20 0a 20 20 20 20 20 20 20 20 54  nges, .        T
2d62e 52 49 47 47 45 52 5f 42 45 46 4f 52 45 2c 20 70  RIGGER_BEFORE, p
2d62f 54 61 62 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64  Tab, regOldRowid
2d630 2c 20 6f 6e 45 72 72 6f 72 2c 20 61 64 64 72 29  , onError, addr)
2d631 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 6f  ;..    /* The ro
2d632 77 2d 74 72 69 67 67 65 72 20 6d 61 79 20 68 61  w-trigger may ha
2d633 76 65 20 64 65 6c 65 74 65 64 20 74 68 65 20 72  ve deleted the r
2d634 6f 77 20 62 65 69 6e 67 20 75 70 64 61 74 65 64  ow being updated
2d635 2e 20 49 6e 20 74 68 69 73 0a 20 20 20 20 2a 2a  . In this.    **
2d636 20 63 61 73 65 2c 20 6a 75 6d 70 20 74 6f 20 74   case, jump to t
2d637 68 65 20 6e 65 78 74 20 72 6f 77 2e 20 4e 6f 20  he next row. No 
2d638 75 70 64 61 74 65 73 20 6f 72 20 41 46 54 45 52  updates or AFTER
2d639 20 74 72 69 67 67 65 72 73 20 61 72 65 20 0a 20   triggers are . 
2d63a 20 20 20 2a 2a 20 72 65 71 75 69 72 65 64 2e 20     ** required. 
2d63b 54 68 69 73 20 62 65 68 61 76 69 6f 75 72 20 2d  This behaviour -
2d63c 20 77 68 61 74 20 68 61 70 70 65 6e 73 20 77 68   what happens wh
2d63d 65 6e 20 74 68 65 20 72 6f 77 20 62 65 69 6e 67  en the row being
2d63e 20 75 70 64 61 74 65 64 0a 20 20 20 20 2a 2a 20   updated.    ** 
2d63f 69 73 20 64 65 6c 65 74 65 64 20 6f 72 20 72 65  is deleted or re
2d640 6e 61 6d 65 64 20 62 79 20 61 20 42 45 46 4f 52  named by a BEFOR
2d641 45 20 74 72 69 67 67 65 72 20 2d 20 69 73 20 6c  E trigger - is l
2d642 65 66 74 20 75 6e 64 65 66 69 6e 65 64 20 69 6e  eft undefined in
2d643 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 6f 63 75   the.    ** docu
2d644 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 2a 2f 0a 20  mentation.  */. 
2d645 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2d646 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78  dOp3(v, OP_NotEx
2d647 69 73 74 73 2c 20 69 43 75 72 2c 20 61 64 64 72  ists, iCur, addr
2d648 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64 29 3b 0a  , regOldRowid);.
2d649 20 20 7d 0a 0a 20 20 69 66 28 20 21 69 73 56 69    }..  if( !isVi
2d64a 65 77 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 44 6f  ew ){..    /* Do
2d64b 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63   constraint chec
2d64c 6b 73 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  ks. */.    sqlit
2d64d 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72  e3GenerateConstr
2d64e 61 69 6e 74 43 68 65 63 6b 73 28 70 50 61 72 73  aintChecks(pPars
2d64f 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 72  e, pTab, iCur, r
2d650 65 67 4e 65 77 52 6f 77 69 64 2c 0a 20 20 20 20  egNewRowid,.    
2d651 20 20 20 20 61 52 65 67 49 64 78 2c 20 28 63 68      aRegIdx, (ch
2d652 6e 67 52 6f 77 69 64 3f 72 65 67 4f 6c 64 52 6f  ngRowid?regOldRo
2d653 77 69 64 3a 30 29 2c 20 31 2c 20 6f 6e 45 72 72  wid:0), 1, onErr
2d654 6f 72 2c 20 61 64 64 72 2c 20 30 29 3b 0a 0a 20  or, addr, 0);.. 
2d655 20 20 20 2f 2a 20 44 6f 20 46 4b 20 63 6f 6e 73     /* Do FK cons
2d656 74 72 61 69 6e 74 20 63 68 65 63 6b 73 2e 20 2a  traint checks. *
2d657 2f 0a 20 20 20 20 69 66 28 20 68 61 73 46 4b 20  /.    if( hasFK 
2d658 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2d659 46 6b 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  FkCheck(pParse, 
2d65a 70 54 61 62 2c 20 72 65 67 4f 6c 64 52 6f 77 69  pTab, regOldRowi
2d65b 64 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  d, 0);.    }..  
2d65c 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20    /* Delete the 
2d65d 69 6e 64 65 78 20 65 6e 74 72 69 65 73 20 61 73  index entries as
2d65e 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
2d65f 65 20 63 75 72 72 65 6e 74 20 72 65 63 6f 72 64  e current record
2d660 2e 20 20 2a 2f 0a 20 20 20 20 6a 31 20 3d 20 73  .  */.    j1 = s
2d661 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
2d662 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73  (v, OP_NotExists
2d663 2c 20 69 43 75 72 2c 20 30 2c 20 72 65 67 4f 6c  , iCur, 0, regOl
2d664 64 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c  dRowid);.    sql
2d665 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f 77 49  ite3GenerateRowI
2d666 6e 64 65 78 44 65 6c 65 74 65 28 70 50 61 72 73  ndexDelete(pPars
2d667 65 2c 20 70 54 61 62 2c 20 69 43 75 72 2c 20 61  e, pTab, iCur, a
2d668 52 65 67 49 64 78 29 3b 0a 20 20 0a 20 20 20 20  RegIdx);.  .    
2d669 2f 2a 20 49 66 20 63 68 61 6e 67 69 6e 67 20 74  /* If changing t
2d66a 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  he record number
2d66b 2c 20 64 65 6c 65 74 65 20 74 68 65 20 6f 6c 64  , delete the old
2d66c 20 72 65 63 6f 72 64 2e 20 20 2a 2f 0a 20 20 20   record.  */.   
2d66d 20 69 66 28 20 68 61 73 46 4b 20 7c 7c 20 63 68   if( hasFK || ch
2d66e 6e 67 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20  ngRowid ){.     
2d66f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2d670 70 32 28 76 2c 20 4f 50 5f 44 65 6c 65 74 65 2c  p2(v, OP_Delete,
2d671 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 20 20 7d   iCur, 0);.    }
2d672 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2d673 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b  JumpHere(v, j1);
2d674 0a 0a 20 20 20 20 69 66 28 20 68 61 73 46 4b 20  ..    if( hasFK 
2d675 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2d676 46 6b 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  FkCheck(pParse, 
2d677 70 54 61 62 2c 20 30 2c 20 72 65 67 4e 65 77 52  pTab, 0, regNewR
2d678 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20 0a  owid);.    }.  .
2d679 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 74 68      /* Insert th
2d67a 65 20 6e 65 77 20 69 6e 64 65 78 20 65 6e 74 72  e new index entr
2d67b 69 65 73 20 61 6e 64 20 74 68 65 20 6e 65 77 20  ies and the new 
2d67c 72 65 63 6f 72 64 2e 20 2a 2f 0a 20 20 20 20 73  record. */.    s
2d67d 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e  qlite3CompleteIn
2d67e 73 65 72 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  sertion(pParse, 
2d67f 70 54 61 62 2c 20 69 43 75 72 2c 20 72 65 67 4e  pTab, iCur, regN
2d680 65 77 52 6f 77 69 64 2c 20 61 52 65 67 49 64 78  ewRowid, aRegIdx
2d681 2c 20 31 2c 20 30 2c 20 30 29 3b 0a 0a 20 20 20  , 1, 0, 0);..   
2d682 20 2f 2a 20 44 6f 20 61 6e 79 20 4f 4e 20 43 41   /* Do any ON CA
2d683 53 43 41 44 45 2c 20 53 45 54 20 4e 55 4c 4c 20  SCADE, SET NULL 
2d684 6f 72 20 53 45 54 20 44 45 46 41 55 4c 54 20 6f  or SET DEFAULT o
2d685 70 65 72 61 74 69 6f 6e 73 20 72 65 71 75 69 72  perations requir
2d686 65 64 20 74 6f 0a 20 20 20 20 2a 2a 20 68 61 6e  ed to.    ** han
2d687 64 6c 65 20 72 6f 77 73 20 28 70 6f 73 73 69 62  dle rows (possib
2d688 6c 79 20 69 6e 20 6f 74 68 65 72 20 74 61 62 6c  ly in other tabl
2d689 65 73 29 20 74 68 61 74 20 72 65 66 65 72 20 76  es) that refer v
2d68a 69 61 20 61 20 66 6f 72 65 69 67 6e 20 6b 65 79  ia a foreign key
2d68b 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 72  .    ** to the r
2d68c 6f 77 20 6a 75 73 74 20 75 70 64 61 74 65 64 2e  ow just updated.
2d68d 20 2a 2f 20 0a 20 20 20 20 69 66 28 20 68 61 73   */ .    if( has
2d68e 46 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  FK ){.      sqli
2d68f 74 65 33 46 6b 41 63 74 69 6f 6e 73 28 70 50 61  te3FkActions(pPa
2d690 72 73 65 2c 20 70 54 61 62 2c 20 70 43 68 61 6e  rse, pTab, pChan
2d691 67 65 73 2c 20 72 65 67 4f 6c 64 52 6f 77 69 64  ges, regOldRowid
2d692 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
2d693 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65  /* Increment the
2d694 20 72 6f 77 20 63 6f 75 6e 74 65 72 20 0a 20 20   row counter .  
2d695 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c  */.  if( (db->fl
2d696 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75  ags & SQLITE_Cou
2d697 6e 74 52 6f 77 73 29 20 26 26 20 21 70 50 61 72  ntRows) && !pPar
2d698 73 65 2d 3e 70 54 72 69 67 67 65 72 54 61 62 29  se->pTriggerTab)
2d699 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
2d69a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64  eAddOp2(v, OP_Ad
2d69b 64 49 6d 6d 2c 20 72 65 67 52 6f 77 43 6f 75 6e  dImm, regRowCoun
2d69c 74 2c 20 31 29 3b 0a 20 20 7d 0a 0a 20 20 73 71  t, 1);.  }..  sq
2d69d 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67  lite3CodeRowTrig
2d69e 67 65 72 28 70 50 61 72 73 65 2c 20 70 54 72 69  ger(pParse, pTri
2d69f 67 67 65 72 2c 20 54 4b 5f 55 50 44 41 54 45 2c  gger, TK_UPDATE,
2d6a0 20 70 43 68 61 6e 67 65 73 2c 20 0a 20 20 20 20   pChanges, .    
2d6a1 20 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 2c    TRIGGER_AFTER,
2d6a2 20 70 54 61 62 2c 20 72 65 67 4f 6c 64 52 6f 77   pTab, regOldRow
2d6a3 69 64 2c 20 6f 6e 45 72 72 6f 72 2c 20 61 64 64  id, onError, add
2d6a4 72 29 3b 0a 0a 20 20 2f 2a 20 52 65 70 65 61 74  r);..  /* Repeat
2d6a5 20 74 68 65 20 61 62 6f 76 65 20 77 69 74 68 20   the above with 
2d6a6 74 68 65 20 6e 65 78 74 20 72 65 63 6f 72 64 20  the next record 
2d6a7 74 6f 20 62 65 20 75 70 64 61 74 65 64 2c 20 75  to be updated, u
2d6a8 6e 74 69 6c 0a 20 20 2a 2a 20 61 6c 6c 20 72 65  ntil.  ** all re
2d6a9 63 6f 72 64 20 73 65 6c 65 63 74 65 64 20 62 79  cord selected by
2d6aa 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
2d6ab 65 20 68 61 76 65 20 62 65 65 6e 20 75 70 64 61  e have been upda
2d6ac 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ted..  */.  sqli
2d6ad 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2d6ae 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64   OP_Goto, 0, add
2d6af 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  r);.  sqlite3Vdb
2d6b0 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
2d6b1 72 29 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65 20  r);..  /* Close 
2d6b2 61 6c 6c 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20  all tables */.  
2d6b3 66 6f 72 28 69 3d 30 2c 20 70 49 64 78 3d 70 54  for(i=0, pIdx=pT
2d6b4 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78  ab->pIndex; pIdx
2d6b5 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65  ; pIdx=pIdx->pNe
2d6b6 78 74 2c 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  xt, i++){.    if
2d6b7 28 20 6f 70 65 6e 41 6c 6c 20 7c 7c 20 61 52 65  ( openAll || aRe
2d6b8 67 49 64 78 5b 69 5d 3e 30 20 29 7b 0a 20 20 20  gIdx[i]>0 ){.   
2d6b9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2d6ba 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp2(v, OP_Close
2d6bb 2c 20 69 43 75 72 2b 69 2b 31 2c 20 30 29 3b 0a  , iCur+i+1, 0);.
2d6bc 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69      }.  }.  sqli
2d6bd 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2d6be 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72 2c   OP_Close, iCur,
2d6bf 20 30 29 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74   0);..  /* Updat
2d6c0 65 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71  e the sqlite_seq
2d6c1 75 65 6e 63 65 20 74 61 62 6c 65 20 62 79 20 73  uence table by s
2d6c2 74 6f 72 69 6e 67 20 74 68 65 20 63 6f 6e 74 65  toring the conte
2d6c3 6e 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 6d  nt of the.  ** m
2d6c4 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 63 6f 75  aximum rowid cou
2d6c5 6e 74 65 72 20 76 61 6c 75 65 73 20 72 65 63 6f  nter values reco
2d6c6 72 64 65 64 20 77 68 69 6c 65 20 69 6e 73 65 72  rded while inser
2d6c7 74 69 6e 67 20 69 6e 74 6f 0a 20 20 2a 2a 20 61  ting into.  ** a
2d6c8 75 74 6f 69 6e 63 72 65 6d 65 6e 74 20 74 61 62  utoincrement tab
2d6c9 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  les..  */.  if( 
2d6ca 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d  pParse->nested==
2d6cb 30 20 26 26 20 70 50 61 72 73 65 2d 3e 70 54 72  0 && pParse->pTr
2d6cc 69 67 67 65 72 54 61 62 3d 3d 30 20 29 7b 0a 20  iggerTab==0 ){. 
2d6cd 20 20 20 73 71 6c 69 74 65 33 41 75 74 6f 69 6e     sqlite3Autoin
2d6ce 63 72 65 6d 65 6e 74 45 6e 64 28 70 50 61 72 73  crementEnd(pPars
2d6cf 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20  e);.  }..  /*.  
2d6d0 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
2d6d1 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61  mber of rows tha
2d6d2 74 20 77 65 72 65 20 63 68 61 6e 67 65 64 2e 20  t were changed. 
2d6d3 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  If this routine 
2d6d4 69 73 20 0a 20 20 2a 2a 20 67 65 6e 65 72 61 74  is .  ** generat
2d6d5 69 6e 67 20 63 6f 64 65 20 62 65 63 61 75 73 65  ing code because
2d6d6 20 6f 66 20 61 20 63 61 6c 6c 20 74 6f 20 73 71   of a call to sq
2d6d7 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
2d6d8 28 29 2c 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20  (), do not.  ** 
2d6d9 69 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62  invoke the callb
2d6da 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20  ack function..  
2d6db 2a 2f 0a 20 20 69 66 28 20 28 64 62 2d 3e 66 6c  */.  if( (db->fl
2d6dc 61 67 73 26 53 51 4c 49 54 45 5f 43 6f 75 6e 74  ags&SQLITE_Count
2d6dd 52 6f 77 73 29 20 26 26 20 21 70 50 61 72 73 65  Rows) && !pParse
2d6de 2d 3e 70 54 72 69 67 67 65 72 54 61 62 20 26 26  ->pTriggerTab &&
2d6df 20 21 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64   !pParse->nested
2d6e0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
2d6e1 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2d6e2 52 65 73 75 6c 74 52 6f 77 2c 20 72 65 67 52 6f  ResultRow, regRo
2d6e3 77 43 6f 75 6e 74 2c 20 31 29 3b 0a 20 20 20 20  wCount, 1);.    
2d6e4 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
2d6e5 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20  mCols(v, 1);.   
2d6e6 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
2d6e7 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
2d6e8 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 72 6f 77 73  NAME_NAME, "rows
2d6e9 20 75 70 64 61 74 65 64 22 2c 20 53 51 4c 49 54   updated", SQLIT
2d6ea 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 0a  E_STATIC);.  }..
2d6eb 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3a 0a  update_cleanup:.
2d6ec 20 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e    sqlite3AuthCon
2d6ed 74 65 78 74 50 6f 70 28 26 73 43 6f 6e 74 65 78  textPop(&sContex
2d6ee 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  t);.  sqlite3DbF
2d6ef 72 65 65 28 64 62 2c 20 61 52 65 67 49 64 78 29  ree(db, aRegIdx)
2d6f0 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
2d6f1 65 28 64 62 2c 20 61 58 52 65 66 29 3b 0a 20 20  e(db, aXRef);.  
2d6f2 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
2d6f3 6c 65 74 65 28 64 62 2c 20 70 54 61 62 4c 69 73  lete(db, pTabLis
2d6f4 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  t);.  sqlite3Exp
2d6f5 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  rListDelete(db, 
2d6f6 70 43 68 61 6e 67 65 73 29 3b 0a 20 20 73 71 6c  pChanges);.  sql
2d6f7 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2d6f8 62 2c 20 70 57 68 65 72 65 29 3b 0a 20 20 72 65  b, pWhere);.  re
2d6f9 74 75 72 6e 3b 0a 7d 0a 2f 2a 20 4d 61 6b 65 20  turn;.}./* Make 
2d6fa 73 75 72 65 20 22 69 73 56 69 65 77 22 20 61 6e  sure "isView" an
2d6fb 64 20 6f 74 68 65 72 20 6d 61 63 72 6f 73 20 64  d other macros d
2d6fc 65 66 69 6e 65 64 20 61 62 6f 76 65 20 61 72 65  efined above are
2d6fd 20 75 6e 64 65 66 69 6e 65 64 2e 20 4f 74 68 65   undefined. Othe
2d6fe 72 77 69 73 65 0a 2a 2a 20 74 68 65 6c 79 20 6d  rwise.** thely m
2d6ff 61 79 20 69 6e 74 65 72 66 65 72 65 20 77 69 74  ay interfere wit
2d700 68 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 66  h compilation of
2d701 20 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e 73   other functions
2d702 20 69 6e 20 74 68 69 73 20 66 69 6c 65 0a 2a 2a   in this file.**
2d703 20 28 6f 72 20 69 6e 20 61 6e 6f 74 68 65 72 20   (or in another 
2d704 66 69 6c 65 2c 20 69 66 20 74 68 69 73 20 66 69  file, if this fi
2d705 6c 65 20 62 65 63 6f 6d 65 73 20 70 61 72 74 20  le becomes part 
2d706 6f 66 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74  of the amalgamat
2d707 69 6f 6e 29 2e 20 20 2a 2f 0a 23 69 66 64 65 66  ion).  */.#ifdef
2d708 20 69 73 56 69 65 77 0a 20 23 75 6e 64 65 66 20   isView. #undef 
2d709 69 73 56 69 65 77 0a 23 65 6e 64 69 66 0a 23 69  isView.#endif.#i
2d70a 66 64 65 66 20 70 54 72 69 67 67 65 72 0a 20 23  fdef pTrigger. #
2d70b 75 6e 64 65 66 20 70 54 72 69 67 67 65 72 0a 23  undef pTrigger.#
2d70c 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
2d70d 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
2d70e 41 4c 54 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 47 65  ALTABLE./*.** Ge
2d70f 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
2d710 61 6e 20 55 50 44 41 54 45 20 6f 66 20 61 20 76  an UPDATE of a v
2d711 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a  irtual table..**
2d712 0a 2a 2a 20 54 68 65 20 73 74 72 61 74 65 67 79  .** The strategy
2d713 20 69 73 20 74 68 61 74 20 77 65 20 63 72 65 61   is that we crea
2d714 74 65 20 61 6e 20 65 70 68 65 6d 65 72 69 61 6c  te an ephemerial
2d715 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f 6e 74   table that cont
2d716 61 69 6e 73 0a 2a 2a 20 66 6f 72 20 65 61 63 68  ains.** for each
2d717 20 72 6f 77 20 74 6f 20 62 65 20 63 68 61 6e 67   row to be chang
2d718 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 41 29 20  ed:.**.**   (A) 
2d719 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 72 6f   The original ro
2d71a 77 69 64 20 6f 66 20 74 68 61 74 20 72 6f 77 2e  wid of that row.
2d71b 0a 2a 2a 20 20 20 28 42 29 20 20 54 68 65 20 72  .**   (B)  The r
2d71c 65 76 69 73 65 64 20 72 6f 77 69 64 20 66 6f 72  evised rowid for
2d71d 20 74 68 65 20 72 6f 77 2e 20 28 6e 6f 74 65 31   the row. (note1
2d71e 29 0a 2a 2a 20 20 20 28 43 29 20 20 54 68 65 20  ).**   (C)  The 
2d71f 63 6f 6e 74 65 6e 74 20 6f 66 20 65 76 65 72 79  content of every
2d720 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72   column in the r
2d721 6f 77 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 77  ow..**.** Then w
2d722 65 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 69 73  e loop over this
2d723 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65   ephemeral table
2d724 20 61 6e 64 20 66 6f 72 20 65 61 63 68 20 72 6f   and for each ro
2d725 77 20 69 6e 0a 2a 2a 20 74 68 65 20 65 70 68 65  w in.** the ephe
2d726 72 6d 65 72 61 6c 20 74 61 62 6c 65 20 63 61 6c  rmeral table cal
2d727 6c 20 56 55 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a  l VUpdate..**.**
2d728 20 57 68 65 6e 20 66 69 6e 69 73 68 65 64 2c 20   When finished, 
2d729 64 72 6f 70 20 74 68 65 20 65 70 68 65 6d 65 72  drop the ephemer
2d72a 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  al table..**.** 
2d72b 28 6e 6f 74 65 31 29 20 41 63 74 75 61 6c 6c 79  (note1) Actually
2d72c 2c 20 69 66 20 77 65 20 6b 6e 6f 77 20 69 6e 20  , if we know in 
2d72d 61 64 76 61 6e 63 65 20 74 68 61 74 20 28 41 29  advance that (A)
2d72e 20 69 73 20 61 6c 77 61 79 73 20 74 68 65 20 73   is always the s
2d72f 61 6d 65 0a 2a 2a 20 61 73 20 28 42 29 20 77 65  ame.** as (B) we
2d730 20 6f 6e 6c 79 20 73 74 6f 72 65 20 28 41 29 2c   only store (A),
2d731 20 74 68 65 6e 20 64 75 70 6c 69 63 61 74 65 20   then duplicate 
2d732 28 41 29 20 77 68 65 6e 20 70 75 6c 6c 69 6e 67  (A) when pulling
2d733 0a 2a 2a 20 69 74 20 6f 75 74 20 6f 66 20 74 68  .** it out of th
2d734 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c  e ephemeral tabl
2d735 65 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67  e before calling
2d736 20 56 55 70 64 61 74 65 2e 0a 2a 2f 0a 73 74 61   VUpdate..*/.sta
2d737 74 69 63 20 76 6f 69 64 20 75 70 64 61 74 65 56  tic void updateV
2d738 69 72 74 75 61 6c 54 61 62 6c 65 28 0a 20 20 50  irtualTable(.  P
2d739 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2d73a 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69      /* The parsi
2d73b 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
2d73c 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20  SrcList *pSrc,  
2d73d 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74       /* The virt
2d73e 75 61 6c 20 74 61 62 6c 65 20 74 6f 20 62 65 20  ual table to be 
2d73f 6d 6f 64 69 66 69 65 64 20 2a 2f 0a 20 20 54 61  modified */.  Ta
2d740 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20  ble *pTab,      
2d741 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
2d742 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70  l table */.  Exp
2d743 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73 2c  rList *pChanges,
2d744 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73    /* The columns
2d745 20 74 6f 20 63 68 61 6e 67 65 20 69 6e 20 74 68   to change in th
2d746 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65  e UPDATE stateme
2d747 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52  nt */.  Expr *pR
2d748 6f 77 69 64 2c 20 20 20 20 20 20 20 20 2f 2a 20  owid,        /* 
2d749 45 78 70 72 65 73 73 69 6f 6e 20 75 73 65 64 20  Expression used 
2d74a 74 6f 20 72 65 63 6f 6d 70 75 74 65 20 74 68 65  to recompute the
2d74b 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20   rowid */.  int 
2d74c 2a 61 58 52 65 66 2c 20 20 20 20 20 20 20 20 20  *aXRef,         
2d74d 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d   /* Mapping from
2d74e 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 70 54 61 62   columns of pTab
2d74f 20 74 6f 20 65 6e 74 72 69 65 73 20 69 6e 20 70   to entries in p
2d750 43 68 61 6e 67 65 73 20 2a 2f 0a 20 20 45 78 70  Changes */.  Exp
2d751 72 20 2a 70 57 68 65 72 65 20 20 20 20 20 20 20  r *pWhere       
2d752 20 20 2f 2a 20 57 48 45 52 45 20 63 6c 61 75 73    /* WHERE claus
2d753 65 20 6f 66 20 74 68 65 20 55 50 44 41 54 45 20  e of the UPDATE 
2d754 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a  statement */.){.
2d755 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
2d756 73 65 2d 3e 70 56 64 62 65 3b 20 20 2f 2a 20 56  se->pVdbe;  /* V
2d757 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 75  irtual machine u
2d758 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
2d759 6e 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  n */.  ExprList 
2d75a 2a 70 45 4c 69 73 74 20 3d 20 30 3b 20 20 20 20  *pEList = 0;    
2d75b 20 2f 2a 20 54 68 65 20 72 65 73 75 6c 74 20 73   /* The result s
2d75c 65 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  et of the SELECT
2d75d 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
2d75e 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20  Select *pSelect 
2d75f 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 54 68 65  = 0;      /* The
2d760 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
2d761 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78  t */.  Expr *pEx
2d762 70 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  pr;             
2d763 20 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 65 78   /* Temporary ex
2d764 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e  pression */.  in
2d765 74 20 65 70 68 65 6d 54 61 62 3b 20 20 20 20 20  t ephemTab;     
2d766 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65          /* Table
2d767 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 72 65 73   holding the res
2d768 75 6c 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43  ult of the SELEC
2d769 54 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  T */.  int i;   
2d76a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d76b 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
2d76c 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 3b 20   */.  int addr; 
2d76d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d76e 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 6f  /* Address of to
2d76f 70 20 6f 66 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69  p of loop */.  i
2d770 6e 74 20 69 52 65 67 3b 20 20 20 20 20 20 20 20  nt iReg;        
2d771 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
2d772 74 20 72 65 67 69 73 74 65 72 20 69 6e 20 73 65  t register in se
2d773 74 20 70 61 73 73 65 64 20 74 6f 20 4f 50 5f 56  t passed to OP_V
2d774 55 70 64 61 74 65 20 2a 2f 0a 20 20 73 71 6c 69  Update */.  sqli
2d775 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2d776 2d 3e 64 62 3b 20 2f 2a 20 44 61 74 61 62 61 73  ->db; /* Databas
2d777 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
2d778 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 56    const char *pV
2d779 54 61 62 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  Tab = (const cha
2d77a 72 2a 29 73 71 6c 69 74 65 33 47 65 74 56 54 61  r*)sqlite3GetVTa
2d77b 62 6c 65 28 64 62 2c 20 70 54 61 62 29 3b 0a 20  ble(db, pTab);. 
2d77c 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74   SelectDest dest
2d77d 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63  ;..  /* Construc
2d77e 74 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  t the SELECT sta
2d77f 74 65 6d 65 6e 74 20 74 68 61 74 20 77 69 6c 6c  tement that will
2d780 20 66 69 6e 64 20 74 68 65 20 6e 65 77 20 76 61   find the new va
2d781 6c 75 65 73 20 66 6f 72 0a 20 20 2a 2a 20 61 6c  lues for.  ** al
2d782 6c 20 75 70 64 61 74 65 64 20 72 6f 77 73 2e 20  l updated rows. 
2d783 0a 20 20 2a 2f 0a 20 20 70 45 4c 69 73 74 20 3d  .  */.  pEList =
2d784 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2d785 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30  Append(pParse, 0
2d786 2c 20 73 71 6c 69 74 65 33 45 78 70 72 28 64 62  , sqlite3Expr(db
2d787 2c 20 54 4b 5f 49 44 2c 20 22 5f 72 6f 77 69 64  , TK_ID, "_rowid
2d788 5f 22 29 29 3b 0a 20 20 69 66 28 20 70 52 6f 77  _"));.  if( pRow
2d789 69 64 20 29 7b 0a 20 20 20 20 70 45 4c 69 73 74  id ){.    pEList
2d78a 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
2d78b 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
2d78c 20 70 45 4c 69 73 74 2c 0a 20 20 20 20 20 20 20   pEList,.       
2d78d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d78e 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2d78f 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
2d790 52 6f 77 69 64 2c 20 30 29 29 3b 0a 20 20 7d 0a  Rowid, 0));.  }.
2d791 20 20 61 73 73 65 72 74 28 20 70 54 61 62 2d 3e    assert( pTab->
2d792 69 50 4b 65 79 3c 30 20 29 3b 0a 20 20 66 6f 72  iPKey<0 );.  for
2d793 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43  (i=0; i<pTab->nC
2d794 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  ol; i++){.    if
2d795 28 20 61 58 52 65 66 5b 69 5d 3e 3d 30 20 29 7b  ( aXRef[i]>=0 ){
2d796 0a 20 20 20 20 20 20 70 45 78 70 72 20 3d 20 73  .      pExpr = s
2d797 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
2d798 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 61 58  , pChanges->a[aX
2d799 52 65 66 5b 69 5d 5d 2e 70 45 78 70 72 2c 20 30  Ref[i]].pExpr, 0
2d79a 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
2d79b 20 20 20 20 70 45 78 70 72 20 3d 20 73 71 6c 69      pExpr = sqli
2d79c 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 49  te3Expr(db, TK_I
2d79d 44 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d  D, pTab->aCol[i]
2d79e 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20  .zName);.    }. 
2d79f 20 20 20 70 45 4c 69 73 74 20 3d 20 73 71 6c 69     pEList = sqli
2d7a0 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
2d7a1 64 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74  d(pParse, pEList
2d7a2 2c 20 70 45 78 70 72 29 3b 0a 20 20 7d 0a 20 20  , pExpr);.  }.  
2d7a3 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65  pSelect = sqlite
2d7a4 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73  3SelectNew(pPars
2d7a5 65 2c 20 70 45 4c 69 73 74 2c 20 70 53 72 63 2c  e, pEList, pSrc,
2d7a6 20 70 57 68 65 72 65 2c 20 30 2c 20 30 2c 20 30   pWhere, 0, 0, 0
2d7a7 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 0a 20  , 0, 0, 0);.  . 
2d7a8 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 65   /* Create the e
2d7a9 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 69  phemeral table i
2d7aa 6e 74 6f 20 77 68 69 63 68 20 74 68 65 20 75 70  nto which the up
2d7ab 64 61 74 65 20 72 65 73 75 6c 74 73 20 77 69 6c  date results wil
2d7ac 6c 0a 20 20 2a 2a 20 62 65 20 73 74 6f 72 65 64  l.  ** be stored
2d7ad 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
2d7ae 20 76 20 29 3b 0a 20 20 65 70 68 65 6d 54 61 62   v );.  ephemTab
2d7af 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
2d7b0 2b 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  +;.  sqlite3Vdbe
2d7b1 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65  AddOp2(v, OP_Ope
2d7b2 6e 45 70 68 65 6d 65 72 61 6c 2c 20 65 70 68 65  nEphemeral, ephe
2d7b3 6d 54 61 62 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c  mTab, pTab->nCol
2d7b4 2b 31 2b 28 70 52 6f 77 69 64 21 3d 30 29 29 3b  +1+(pRowid!=0));
2d7b5 0a 0a 20 20 2f 2a 20 66 69 6c 6c 20 74 68 65 20  ..  /* fill the 
2d7b6 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20  ephemeral table 
2d7b7 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53  .  */.  sqlite3S
2d7b8 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64  electDestInit(&d
2d7b9 65 73 74 2c 20 53 52 54 5f 54 61 62 6c 65 2c 20  est, SRT_Table, 
2d7ba 65 70 68 65 6d 54 61 62 29 3b 0a 20 20 73 71 6c  ephemTab);.  sql
2d7bb 69 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73  ite3Select(pPars
2d7bc 65 2c 20 70 53 65 6c 65 63 74 2c 20 26 64 65 73  e, pSelect, &des
2d7bd 74 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61  t);..  /* Genera
2d7be 74 65 20 63 6f 64 65 20 74 6f 20 73 63 61 6e 20  te code to scan 
2d7bf 74 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61  the ephemeral ta
2d7c0 62 6c 65 20 61 6e 64 20 63 61 6c 6c 20 56 55 70  ble and call VUp
2d7c1 64 61 74 65 2e 20 2a 2f 0a 20 20 69 52 65 67 20  date. */.  iReg 
2d7c2 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2d7c3 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  ;.  pParse->nMem
2d7c4 20 2b 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31   += pTab->nCol+1
2d7c5 3b 0a 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74  ;.  addr = sqlit
2d7c6 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2d7c7 4f 50 5f 52 65 77 69 6e 64 2c 20 65 70 68 65 6d  OP_Rewind, ephem
2d7c8 54 61 62 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74  Tab, 0);.  sqlit
2d7c9 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
2d7ca 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20 65 70 68 65  OP_Column,  ephe
2d7cb 6d 54 61 62 2c 20 30 2c 20 69 52 65 67 29 3b 0a  mTab, 0, iReg);.
2d7cc 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2d7cd 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
2d7ce 2c 20 65 70 68 65 6d 54 61 62 2c 20 28 70 52 6f  , ephemTab, (pRo
2d7cf 77 69 64 3f 31 3a 30 29 2c 20 69 52 65 67 2b 31  wid?1:0), iReg+1
2d7d0 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
2d7d1 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29  pTab->nCol; i++)
2d7d2 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
2d7d3 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
2d7d4 6c 75 6d 6e 2c 20 65 70 68 65 6d 54 61 62 2c 20  lumn, ephemTab, 
2d7d5 69 2b 31 2b 28 70 52 6f 77 69 64 21 3d 30 29 2c  i+1+(pRowid!=0),
2d7d6 20 69 52 65 67 2b 32 2b 69 29 3b 0a 20 20 7d 0a   iReg+2+i);.  }.
2d7d7 20 20 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b    sqlite3VtabMak
2d7d8 65 57 72 69 74 61 62 6c 65 28 70 50 61 72 73 65  eWritable(pParse
2d7d9 2c 20 70 54 61 62 29 3b 0a 20 20 73 71 6c 69 74  , pTab);.  sqlit
2d7da 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
2d7db 4f 50 5f 56 55 70 64 61 74 65 2c 20 30 2c 20 70  OP_VUpdate, 0, p
2d7dc 54 61 62 2d 3e 6e 43 6f 6c 2b 32 2c 20 69 52 65  Tab->nCol+2, iRe
2d7dd 67 2c 20 70 56 54 61 62 2c 20 50 34 5f 56 54 41  g, pVTab, P4_VTA
2d7de 42 29 3b 0a 20 20 73 71 6c 69 74 65 33 4d 61 79  B);.  sqlite3May
2d7df 41 62 6f 72 74 28 70 50 61 72 73 65 29 3b 0a 20  Abort(pParse);. 
2d7e0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2d7e1 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 65  p2(v, OP_Next, e
2d7e2 70 68 65 6d 54 61 62 2c 20 61 64 64 72 2b 31 29  phemTab, addr+1)
2d7e3 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a  ;.  sqlite3VdbeJ
2d7e4 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29  umpHere(v, addr)
2d7e5 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
2d7e6 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp2(v, OP_Clos
2d7e7 65 2c 20 65 70 68 65 6d 54 61 62 2c 20 30 29 3b  e, ephemTab, 0);
2d7e8 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 75 70 20 2a  ..  /* Cleanup *
2d7e9 2f 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  /.  sqlite3Selec
2d7ea 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c  tDelete(db, pSel
2d7eb 65 63 74 29 3b 20 20 0a 7d 0a 23 65 6e 64 69 66  ect);  .}.#endif
2d7ec 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2d7ed 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a  VIRTUALTABLE */.
2d7ee 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2d7ef 20 45 6e 64 20 6f 66 20 75 70 64 61 74 65 2e 63   End of update.c
2d7f0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2d7f1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d7f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2d7f3 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2d7f4 20 42 65 67 69 6e 20 66 69 6c 65 20 76 61 63 75   Begin file vacu
2d7f5 75 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  um.c ***********
2d7f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d7f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2d7f8 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 41 70 72 69  ./*.** 2003 Apri
2d7f9 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  l 6.**.** The au
2d7fa 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
2d7fb 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
2d7fc 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
2d7fd 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
2d7fe 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
2d7ff 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
2d800 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
2d801 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
2d802 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
2d803 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
2d804 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
2d805 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
2d806 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
2d807 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
2d808 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
2d809 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
2d80a 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
2d80b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d80c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d80d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d80e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d80f 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
2d810 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73  contains code us
2d811 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  ed to implement 
2d812 74 68 65 20 56 41 43 55 55 4d 20 63 6f 6d 6d 61  the VACUUM comma
2d813 6e 64 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6f  nd..**.** Most o
2d814 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68  f the code in th
2d815 69 73 20 66 69 6c 65 20 6d 61 79 20 62 65 20 6f  is file may be o
2d816 6d 69 74 74 65 64 20 62 79 20 64 65 66 69 6e 69  mitted by defini
2d817 6e 67 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45  ng the.** SQLITE
2d818 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 20 6d 61 63  _OMIT_VACUUM mac
2d819 72 6f 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76  ro..**.** $Id: v
2d81a 61 63 75 75 6d 2e 63 2c 76 20 31 2e 39 31 20 32  acuum.c,v 1.91 2
2d81b 30 30 39 2f 30 37 2f 30 32 20 30 37 3a 34 37 3a  009/07/02 07:47:
2d81c 33 33 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  33 danielk1977 E
2d81d 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 20 21 64 65  xp $.*/..#if !de
2d81e 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2d81f 54 5f 56 41 43 55 55 4d 29 20 26 26 20 21 64 65  T_VACUUM) && !de
2d820 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2d821 54 5f 41 54 54 41 43 48 29 0a 2f 2a 0a 2a 2a 20  T_ATTACH)./*.** 
2d822 45 78 65 63 75 74 65 20 7a 53 71 6c 20 6f 6e 20  Execute zSql on 
2d823 64 61 74 61 62 61 73 65 20 64 62 2e 20 52 65 74  database db. Ret
2d824 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  urn an error cod
2d825 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
2d826 20 65 78 65 63 53 71 6c 28 73 71 6c 69 74 65 33   execSql(sqlite3
2d827 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   *db, const char
2d828 20 2a 7a 53 71 6c 29 7b 0a 20 20 73 71 6c 69 74   *zSql){.  sqlit
2d829 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a  e3_stmt *pStmt;.
2d82a 20 20 56 56 41 5f 4f 4e 4c 59 28 20 69 6e 74 20    VVA_ONLY( int 
2d82b 72 63 3b 20 29 0a 20 20 69 66 28 20 21 7a 53 71  rc; ).  if( !zSq
2d82c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  l ){.    return 
2d82d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
2d82e 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  }.  if( SQLITE_O
2d82f 4b 21 3d 73 71 6c 69 74 65 33 5f 70 72 65 70 61  K!=sqlite3_prepa
2d830 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c  re(db, zSql, -1,
2d831 20 26 70 53 74 6d 74 2c 20 30 29 20 29 7b 0a 20   &pStmt, 0) ){. 
2d832 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65     return sqlite
2d833 33 5f 65 72 72 63 6f 64 65 28 64 62 29 3b 0a 20  3_errcode(db);. 
2d834 20 7d 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 20 72   }.  VVA_ONLY( r
2d835 63 20 3d 20 29 20 73 71 6c 69 74 65 33 5f 73 74  c = ) sqlite3_st
2d836 65 70 28 70 53 74 6d 74 29 3b 0a 20 20 61 73 73  ep(pStmt);.  ass
2d837 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
2d838 52 4f 57 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ROW );.  return 
2d839 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
2d83a 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  (pStmt);.}../*.*
2d83b 2a 20 45 78 65 63 75 74 65 20 7a 53 71 6c 20 6f  * Execute zSql o
2d83c 6e 20 64 61 74 61 62 61 73 65 20 64 62 2e 20 54  n database db. T
2d83d 68 65 20 73 74 61 74 65 6d 65 6e 74 20 72 65 74  he statement ret
2d83e 75 72 6e 73 20 65 78 61 63 74 6c 79 0a 2a 2a 20  urns exactly.** 
2d83f 6f 6e 65 20 63 6f 6c 75 6d 6e 2e 20 45 78 65 63  one column. Exec
2d840 75 74 65 20 74 68 69 73 20 61 73 20 53 51 4c 20  ute this as SQL 
2d841 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  on the same data
2d842 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  base..*/.static 
2d843 69 6e 74 20 65 78 65 63 45 78 65 63 53 71 6c 28  int execExecSql(
2d844 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e  sqlite3 *db, con
2d845 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 29 7b 0a  st char *zSql){.
2d846 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
2d847 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 72 63 3b  pStmt;.  int rc;
2d848 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
2d849 5f 70 72 65 70 61 72 65 28 64 62 2c 20 7a 53 71  _prepare(db, zSq
2d84a 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30  l, -1, &pStmt, 0
2d84b 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
2d84c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
2d84d 72 63 3b 0a 0a 20 20 77 68 69 6c 65 28 20 53 51  rc;..  while( SQ
2d84e 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65  LITE_ROW==sqlite
2d84f 33 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29 7b  3_step(pStmt) ){
2d850 0a 20 20 20 20 72 63 20 3d 20 65 78 65 63 53 71  .    rc = execSq
2d851 6c 28 64 62 2c 20 28 63 68 61 72 2a 29 73 71 6c  l(db, (char*)sql
2d852 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
2d853 28 70 53 74 6d 74 2c 20 30 29 29 3b 0a 20 20 20  (pStmt, 0));.   
2d854 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2d855 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  OK ){.      sqli
2d856 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74  te3_finalize(pSt
2d857 6d 74 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  mt);.      retur
2d858 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  n rc;.    }.  }.
2d859 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
2d85a 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74  3_finalize(pStmt
2d85b 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  );.}../*.** The 
2d85c 6e 6f 6e 2d 73 74 61 6e 64 61 72 64 20 56 41 43  non-standard VAC
2d85d 55 55 4d 20 63 6f 6d 6d 61 6e 64 20 69 73 20 75  UUM command is u
2d85e 73 65 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 20  sed to clean up 
2d85f 74 68 65 20 64 61 74 61 62 61 73 65 2c 0a 2a 2a  the database,.**
2d860 20 63 6f 6c 6c 61 70 73 65 20 66 72 65 65 20 73   collapse free s
2d861 70 61 63 65 2c 20 65 74 63 2e 20 20 49 74 20 69  pace, etc.  It i
2d862 73 20 6d 6f 64 65 6c 6c 65 64 20 61 66 74 65 72  s modelled after
2d863 20 74 68 65 20 56 41 43 55 55 4d 20 63 6f 6d 6d   the VACUUM comm
2d864 61 6e 64 0a 2a 2a 20 69 6e 20 50 6f 73 74 67 72  and.** in Postgr
2d865 65 53 51 4c 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 76  eSQL..**.** In v
2d866 65 72 73 69 6f 6e 20 31 2e 30 2e 78 20 6f 66 20  ersion 1.0.x of 
2d867 53 51 4c 69 74 65 2c 20 74 68 65 20 56 41 43 55  SQLite, the VACU
2d868 55 4d 20 63 6f 6d 6d 61 6e 64 20 77 6f 75 6c 64  UM command would
2d869 20 63 61 6c 6c 0a 2a 2a 20 67 64 62 6d 5f 72 65   call.** gdbm_re
2d86a 6f 72 67 61 6e 69 7a 65 28 29 20 6f 6e 20 61 6c  organize() on al
2d86b 6c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  l the database t
2d86c 61 62 6c 65 73 2e 20 20 42 75 74 20 62 65 67 69  ables.  But begi
2d86d 6e 6e 69 6e 67 0a 2a 2a 20 77 69 74 68 20 32 2e  nning.** with 2.
2d86e 30 2e 30 2c 20 53 51 4c 69 74 65 20 6e 6f 20 6c  0.0, SQLite no l
2d86f 6f 6e 67 65 72 20 75 73 65 73 20 47 44 42 4d 20  onger uses GDBM 
2d870 73 6f 20 74 68 69 73 20 63 6f 6d 6d 61 6e 64 20  so this command 
2d871 68 61 73 0a 2a 2a 20 62 65 63 6f 6d 65 20 61 20  has.** become a 
2d872 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45  no-op..*/.SQLITE
2d873 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2d874 6c 69 74 65 33 56 61 63 75 75 6d 28 50 61 72 73  lite3Vacuum(Pars
2d875 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 56 64  e *pParse){.  Vd
2d876 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47  be *v = sqlite3G
2d877 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
2d878 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73    if( v ){.    s
2d879 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2d87a 28 76 2c 20 4f 50 5f 56 61 63 75 75 6d 2c 20 30  (v, OP_Vacuum, 0
2d87b 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , 0);.  }.  retu
2d87c 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rn;.}../*.** Thi
2d87d 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d  s routine implem
2d87e 65 6e 74 73 20 74 68 65 20 4f 50 5f 56 61 63 75  ents the OP_Vacu
2d87f 75 6d 20 6f 70 63 6f 64 65 20 6f 66 20 74 68 65  um opcode of the
2d880 20 56 44 42 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45   VDBE..*/.SQLITE
2d881 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
2d882 69 74 65 33 52 75 6e 56 61 63 75 75 6d 28 63 68  ite3RunVacuum(ch
2d883 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c 20 73  ar **pzErrMsg, s
2d884 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
2d885 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
2d886 4b 3b 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e  K;     /* Return
2d887 20 63 6f 64 65 20 66 72 6f 6d 20 73 65 72 76 69   code from servi
2d888 63 65 20 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20  ce routines */. 
2d889 20 42 74 72 65 65 20 2a 70 4d 61 69 6e 3b 20 20   Btree *pMain;  
2d88a 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2d88b 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 20 76  database being v
2d88c 61 63 75 75 6d 65 64 20 2a 2f 0a 20 20 42 74 72  acuumed */.  Btr
2d88d 65 65 20 2a 70 54 65 6d 70 3b 20 20 20 20 20 20  ee *pTemp;      
2d88e 20 20 20 20 20 2f 2a 20 54 68 65 20 74 65 6d 70       /* The temp
2d88f 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 20 77  orary database w
2d890 65 20 76 61 63 75 75 6d 20 69 6e 74 6f 20 2a 2f  e vacuum into */
2d891 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20  .  char *zSql = 
2d892 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51  0;         /* SQ
2d893 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a  L statements */.
2d894 20 20 69 6e 74 20 73 61 76 65 64 5f 66 6c 61 67    int saved_flag
2d895 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 61 76  s;        /* Sav
2d896 65 64 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  ed value of the 
2d897 64 62 2d 3e 66 6c 61 67 73 20 2a 2f 0a 20 20 69  db->flags */.  i
2d898 6e 74 20 73 61 76 65 64 5f 6e 43 68 61 6e 67 65  nt saved_nChange
2d899 3b 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64 20  ;      /* Saved 
2d89a 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e 6e 43 68  value of db->nCh
2d89b 61 6e 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 61  ange */.  int sa
2d89c 76 65 64 5f 6e 54 6f 74 61 6c 43 68 61 6e 67 65  ved_nTotalChange
2d89d 3b 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65  ; /* Saved value
2d89e 20 6f 66 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68   of db->nTotalCh
2d89f 61 6e 67 65 20 2a 2f 0a 20 20 44 62 20 2a 70 44  ange */.  Db *pD
2d8a0 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  b = 0;          
2d8a1 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f    /* Database to
2d8a2 20 64 65 74 61 63 68 20 61 74 20 65 6e 64 20 6f   detach at end o
2d8a3 66 20 76 61 63 75 75 6d 20 2a 2f 0a 20 20 69 6e  f vacuum */.  in
2d8a4 74 20 69 73 4d 65 6d 44 62 3b 20 20 20 20 20 20  t isMemDb;      
2d8a5 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
2d8a6 20 76 61 63 75 75 6d 69 6e 67 20 61 20 3a 6d 65   vacuuming a :me
2d8a7 6d 6f 72 79 3a 20 64 61 74 61 62 61 73 65 20 2a  mory: database *
2d8a8 2f 0a 20 20 69 6e 74 20 6e 52 65 73 3b 0a 0a 20  /.  int nRes;.. 
2d8a9 20 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f   if( !db->autoCo
2d8aa 6d 6d 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  mmit ){.    sqli
2d8ab 74 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a 45  te3SetString(pzE
2d8ac 72 72 4d 73 67 2c 20 64 62 2c 20 22 63 61 6e 6e  rrMsg, db, "cann
2d8ad 6f 74 20 56 41 43 55 55 4d 20 66 72 6f 6d 20 77  ot VACUUM from w
2d8ae 69 74 68 69 6e 20 61 20 74 72 61 6e 73 61 63 74  ithin a transact
2d8af 69 6f 6e 22 29 3b 0a 20 20 20 20 72 65 74 75 72  ion");.    retur
2d8b0 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
2d8b1 20 20 7d 0a 0a 20 20 2f 2a 20 53 61 76 65 20 74    }..  /* Save t
2d8b2 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65  he current value
2d8b3 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
2d8b4 20 66 6c 61 67 73 20 73 6f 20 74 68 61 74 20 69   flags so that i
2d8b5 74 20 63 61 6e 20 62 65 20 0a 20 20 2a 2a 20 72  t can be .  ** r
2d8b6 65 73 74 6f 72 65 64 20 62 65 66 6f 72 65 20 72  estored before r
2d8b7 65 74 75 72 6e 69 6e 67 2e 20 54 68 65 6e 20 73  eturning. Then s
2d8b8 65 74 20 74 68 65 20 77 72 69 74 61 62 6c 65 2d  et the writable-
2d8b9 73 63 68 65 6d 61 20 66 6c 61 67 2c 20 61 6e 64  schema flag, and
2d8ba 0a 20 20 2a 2a 20 64 69 73 61 62 6c 65 20 43 48  .  ** disable CH
2d8bb 45 43 4b 20 61 6e 64 20 66 6f 72 65 69 67 6e 20  ECK and foreign 
2d8bc 6b 65 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e  key constraints.
2d8bd 20 20 2a 2f 0a 20 20 73 61 76 65 64 5f 66 6c 61    */.  saved_fla
2d8be 67 73 20 3d 20 64 62 2d 3e 66 6c 61 67 73 3b 0a  gs = db->flags;.
2d8bf 20 20 73 61 76 65 64 5f 6e 43 68 61 6e 67 65 20    saved_nChange 
2d8c0 3d 20 64 62 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20  = db->nChange;. 
2d8c1 20 73 61 76 65 64 5f 6e 54 6f 74 61 6c 43 68 61   saved_nTotalCha
2d8c2 6e 67 65 20 3d 20 64 62 2d 3e 6e 54 6f 74 61 6c  nge = db->nTotal
2d8c3 43 68 61 6e 67 65 3b 0a 20 20 64 62 2d 3e 66 6c  Change;.  db->fl
2d8c4 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 57 72  ags |= SQLITE_Wr
2d8c5 69 74 65 53 63 68 65 6d 61 20 7c 20 53 51 4c 49  iteSchema | SQLI
2d8c6 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73 3b  TE_IgnoreChecks;
2d8c7 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20  .  db->flags &= 
2d8c8 7e 53 51 4c 49 54 45 5f 46 6f 72 65 69 67 6e 4b  ~SQLITE_ForeignK
2d8c9 65 79 73 3b 0a 0a 20 20 70 4d 61 69 6e 20 3d 20  eys;..  pMain = 
2d8ca 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3b 0a  db->aDb[0].pBt;.
2d8cb 20 20 69 73 4d 65 6d 44 62 20 3d 20 73 71 6c 69    isMemDb = sqli
2d8cc 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28  te3PagerIsMemdb(
2d8cd 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
2d8ce 72 28 70 4d 61 69 6e 29 29 3b 0a 0a 20 20 2f 2a  r(pMain));..  /*
2d8cf 20 41 74 74 61 63 68 20 74 68 65 20 74 65 6d 70   Attach the temp
2d8d0 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 20 61  orary database a
2d8d1 73 20 27 76 61 63 75 75 6d 5f 64 62 27 2e 20 54  s 'vacuum_db'. T
2d8d2 68 65 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 70  he synchronous p
2d8d3 72 61 67 6d 61 0a 20 20 2a 2a 20 63 61 6e 20 62  ragma.  ** can b
2d8d4 65 20 73 65 74 20 74 6f 20 27 6f 66 66 27 20 66  e set to 'off' f
2d8d5 6f 72 20 74 68 69 73 20 66 69 6c 65 2c 20 61 73  or this file, as
2d8d6 20 69 74 20 69 73 20 6e 6f 74 20 72 65 63 6f 76   it is not recov
2d8d7 65 72 65 64 20 69 66 20 61 20 63 72 61 73 68 0a  ered if a crash.
2d8d8 20 20 2a 2a 20 6f 63 63 75 72 73 20 61 6e 79 77    ** occurs anyw
2d8d9 61 79 2e 20 54 68 65 20 69 6e 74 65 67 72 69 74  ay. The integrit
2d8da 79 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  y of the databas
2d8db 65 20 69 73 20 6d 61 69 6e 74 61 69 6e 65 64 20  e is maintained 
2d8dc 62 79 20 61 0a 20 20 2a 2a 20 28 70 6f 73 73 69  by a.  ** (possi
2d8dd 62 6c 79 20 73 79 6e 63 68 72 6f 6e 6f 75 73 29  bly synchronous)
2d8de 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65   transaction ope
2d8df 6e 65 64 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20  ned on the main 
2d8e0 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a  database before.
2d8e1 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65    ** sqlite3Btre
2d8e2 65 43 6f 70 79 46 69 6c 65 28 29 20 69 73 20 63  eCopyFile() is c
2d8e3 61 6c 6c 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  alled..  **.  **
2d8e4 20 41 6e 20 6f 70 74 69 6d 69 73 61 74 69 6f 6e   An optimisation
2d8e5 20 77 6f 75 6c 64 20 62 65 20 74 6f 20 75 73 65   would be to use
2d8e6 20 61 20 6e 6f 6e 2d 6a 6f 75 72 6e 61 6c 65 64   a non-journaled
2d8e7 20 70 61 67 65 72 2e 0a 20 20 2a 2a 20 28 4c 61   pager..  ** (La
2d8e8 74 65 72 3a 29 20 49 20 74 72 69 65 64 20 73 65  ter:) I tried se
2d8e9 74 74 69 6e 67 20 22 50 52 41 47 4d 41 20 76 61  tting "PRAGMA va
2d8ea 63 75 75 6d 5f 64 62 2e 6a 6f 75 72 6e 61 6c 5f  cuum_db.journal_
2d8eb 6d 6f 64 65 3d 4f 46 46 22 20 62 75 74 0a 20 20  mode=OFF" but.  
2d8ec 2a 2a 20 74 68 61 74 20 61 63 74 75 61 6c 6c 79  ** that actually
2d8ed 20 6d 61 64 65 20 74 68 65 20 56 41 43 55 55 4d   made the VACUUM
2d8ee 20 72 75 6e 20 73 6c 6f 77 65 72 2e 20 20 56 65   run slower.  Ve
2d8ef 72 79 20 6c 69 74 74 6c 65 20 6a 6f 75 72 6e 61  ry little journa
2d8f0 6c 6c 69 6e 67 0a 20 20 2a 2a 20 61 63 74 75 61  lling.  ** actua
2d8f1 6c 6c 79 20 6f 63 63 75 72 73 20 77 68 65 6e 20  lly occurs when 
2d8f2 64 6f 69 6e 67 20 61 20 76 61 63 75 75 6d 20 73  doing a vacuum s
2d8f3 69 6e 63 65 20 74 68 65 20 76 61 63 75 75 6d 5f  ince the vacuum_
2d8f4 64 62 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 0a  db is initially.
2d8f5 20 20 2a 2a 20 65 6d 70 74 79 2e 20 20 4f 6e 6c    ** empty.  Onl
2d8f6 79 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65  y the journal he
2d8f7 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 2e  ader is written.
2d8f8 20 20 41 70 70 61 72 65 6e 74 6c 79 20 69 74 20    Apparently it 
2d8f9 74 61 6b 65 73 20 6d 6f 72 65 0a 20 20 2a 2a 20  takes more.  ** 
2d8fa 74 69 6d 65 20 74 6f 20 70 61 72 73 65 20 61 6e  time to parse an
2d8fb 64 20 72 75 6e 20 74 68 65 20 50 52 41 47 4d 41  d run the PRAGMA
2d8fc 20 74 6f 20 74 75 72 6e 20 6a 6f 75 72 6e 61 6c   to turn journal
2d8fd 6c 69 6e 67 20 6f 66 66 20 74 68 61 6e 20 69 74  ling off than it
2d8fe 20 64 6f 65 73 0a 20 20 2a 2a 20 74 6f 20 77 72   does.  ** to wr
2d8ff 69 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ite the journal 
2d900 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 20 20 2a  header file..  *
2d901 2f 0a 20 20 7a 53 71 6c 20 3d 20 22 41 54 54 41  /.  zSql = "ATTA
2d902 43 48 20 27 27 20 41 53 20 76 61 63 75 75 6d 5f  CH '' AS vacuum_
2d903 64 62 3b 22 3b 0a 20 20 72 63 20 3d 20 65 78 65  db;";.  rc = exe
2d904 63 53 71 6c 28 64 62 2c 20 7a 53 71 6c 29 3b 0a  cSql(db, zSql);.
2d905 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2d906 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f  _OK ) goto end_o
2d907 66 5f 76 61 63 75 75 6d 3b 0a 20 20 70 44 62 20  f_vacuum;.  pDb 
2d908 3d 20 26 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e  = &db->aDb[db->n
2d909 44 62 2d 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  Db-1];.  assert(
2d90a 20 73 74 72 63 6d 70 28 64 62 2d 3e 61 44 62 5b   strcmp(db->aDb[
2d90b 64 62 2d 3e 6e 44 62 2d 31 5d 2e 7a 4e 61 6d 65  db->nDb-1].zName
2d90c 2c 22 76 61 63 75 75 6d 5f 64 62 22 29 3d 3d 30  ,"vacuum_db")==0
2d90d 20 29 3b 0a 20 20 70 54 65 6d 70 20 3d 20 64 62   );.  pTemp = db
2d90e 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 2d 31 5d  ->aDb[db->nDb-1]
2d90f 2e 70 42 74 3b 0a 0a 20 20 2f 2a 20 54 68 65 20  .pBt;..  /* The 
2d910 63 61 6c 6c 20 74 6f 20 65 78 65 63 53 71 6c 28  call to execSql(
2d911 29 20 74 6f 20 61 74 74 61 63 68 20 74 68 65 20  ) to attach the 
2d912 74 65 6d 70 20 64 61 74 61 62 61 73 65 20 68 61  temp database ha
2d913 73 20 6c 65 66 74 20 74 68 65 20 66 69 6c 65 0a  s left the file.
2d914 20 20 2a 2a 20 6c 6f 63 6b 65 64 20 28 61 73 20    ** locked (as 
2d915 74 68 65 72 65 20 77 61 73 20 6d 6f 72 65 20 74  there was more t
2d916 68 61 6e 20 6f 6e 65 20 61 63 74 69 76 65 20 73  han one active s
2d917 74 61 74 65 6d 65 6e 74 20 77 68 65 6e 20 74 68  tatement when th
2d918 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20  e transaction.  
2d919 2a 2a 20 74 6f 20 72 65 61 64 20 74 68 65 20 73  ** to read the s
2d91a 63 68 65 6d 61 20 77 61 73 20 63 6f 6e 63 6c 75  chema was conclu
2d91b 64 65 64 2e 20 55 6e 6c 6f 63 6b 20 69 74 20 68  ded. Unlock it h
2d91c 65 72 65 20 73 6f 20 74 68 61 74 20 74 68 69 73  ere so that this
2d91d 20 64 6f 65 73 6e 27 74 0a 20 20 2a 2a 20 63 61   doesn't.  ** ca
2d91e 75 73 65 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72  use problems for
2d91f 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 42 74 72   the call to Btr
2d920 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 29 20  eeSetPageSize() 
2d921 62 65 6c 6f 77 2e 20 20 2a 2f 0a 20 20 73 71 6c  below.  */.  sql
2d922 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28  ite3BtreeCommit(
2d923 70 54 65 6d 70 29 3b 0a 0a 20 20 6e 52 65 73 20  pTemp);..  nRes 
2d924 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
2d925 74 52 65 73 65 72 76 65 28 70 4d 61 69 6e 29 3b  tReserve(pMain);
2d926 0a 0a 20 20 2f 2a 20 41 20 56 41 43 55 55 4d 20  ..  /* A VACUUM 
2d927 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65 20 74 68  cannot change th
2d928 65 20 70 61 67 65 73 69 7a 65 20 6f 66 20 61 6e  e pagesize of an
2d929 20 65 6e 63 72 79 70 74 65 64 20 64 61 74 61 62   encrypted datab
2d92a 61 73 65 2e 20 2a 2f 0a 23 69 66 64 65 66 20 53  ase. */.#ifdef S
2d92b 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a  QLITE_HAS_CODEC.
2d92c 20 20 69 66 28 20 64 62 2d 3e 6e 65 78 74 50 61    if( db->nextPa
2d92d 67 65 73 69 7a 65 20 29 7b 0a 20 20 20 20 65 78  gesize ){.    ex
2d92e 74 65 72 6e 20 76 6f 69 64 20 73 71 6c 69 74 65  tern void sqlite
2d92f 33 43 6f 64 65 63 47 65 74 4b 65 79 28 73 71 6c  3CodecGetKey(sql
2d930 69 74 65 33 2a 2c 20 69 6e 74 2c 20 76 6f 69 64  ite3*, int, void
2d931 2a 2a 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20 69  **, int*);.    i
2d932 6e 74 20 6e 4b 65 79 3b 0a 20 20 20 20 63 68 61  nt nKey;.    cha
2d933 72 20 2a 7a 4b 65 79 3b 0a 20 20 20 20 73 71 6c  r *zKey;.    sql
2d934 69 74 65 33 43 6f 64 65 63 47 65 74 4b 65 79 28  ite3CodecGetKey(
2d935 64 62 2c 20 30 2c 20 28 76 6f 69 64 2a 2a 29 26  db, 0, (void**)&
2d936 7a 4b 65 79 2c 20 26 6e 4b 65 79 29 3b 0a 20 20  zKey, &nKey);.  
2d937 20 20 69 66 28 20 6e 4b 65 79 20 29 20 64 62 2d    if( nKey ) db-
2d938 3e 6e 65 78 74 50 61 67 65 73 69 7a 65 20 3d 20  >nextPagesize = 
2d939 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  0;.  }.#endif.. 
2d93a 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65   if( sqlite3Btre
2d93b 65 53 65 74 50 61 67 65 53 69 7a 65 28 70 54 65  eSetPageSize(pTe
2d93c 6d 70 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65  mp, sqlite3Btree
2d93d 47 65 74 50 61 67 65 53 69 7a 65 28 70 4d 61 69  GetPageSize(pMai
2d93e 6e 29 2c 20 6e 52 65 73 2c 20 30 29 0a 20 20 20  n), nRes, 0).   
2d93f 7c 7c 20 28 21 69 73 4d 65 6d 44 62 20 26 26 20  || (!isMemDb && 
2d940 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 50  sqlite3BtreeSetP
2d941 61 67 65 53 69 7a 65 28 70 54 65 6d 70 2c 20 64  ageSize(pTemp, d
2d942 62 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a 65 2c  b->nextPagesize,
2d943 20 6e 52 65 73 2c 20 30 29 29 0a 20 20 20 7c 7c   nRes, 0)).   ||
2d944 20 4e 45 56 45 52 28 64 62 2d 3e 6d 61 6c 6c 6f   NEVER(db->mallo
2d945 63 46 61 69 6c 65 64 29 0a 20 20 29 7b 0a 20 20  cFailed).  ){.  
2d946 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
2d947 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e  MEM;.    goto en
2d948 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 7d  d_of_vacuum;.  }
2d949 0a 20 20 72 63 20 3d 20 65 78 65 63 53 71 6c 28  .  rc = execSql(
2d94a 64 62 2c 20 22 50 52 41 47 4d 41 20 76 61 63 75  db, "PRAGMA vacu
2d94b 75 6d 5f 64 62 2e 73 79 6e 63 68 72 6f 6e 6f 75  um_db.synchronou
2d94c 73 3d 4f 46 46 22 29 3b 0a 20 20 69 66 28 20 72  s=OFF");.  if( r
2d94d 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
2d94e 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f      goto end_of_
2d94f 76 61 63 75 75 6d 3b 0a 20 20 7d 0a 0a 23 69 66  vacuum;.  }..#if
2d950 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2d951 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 73 71  _AUTOVACUUM.  sq
2d952 6c 69 74 65 33 42 74 72 65 65 53 65 74 41 75 74  lite3BtreeSetAut
2d953 6f 56 61 63 75 75 6d 28 70 54 65 6d 70 2c 20 64  oVacuum(pTemp, d
2d954 62 2d 3e 6e 65 78 74 41 75 74 6f 76 61 63 3e 3d  b->nextAutovac>=
2d955 30 20 3f 20 64 62 2d 3e 6e 65 78 74 41 75 74 6f  0 ? db->nextAuto
2d956 76 61 63 20 3a 0a 20 20 20 20 20 20 20 20 20 20  vac :.          
2d957 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d958 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d959 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
2d95a 41 75 74 6f 56 61 63 75 75 6d 28 70 4d 61 69 6e  AutoVacuum(pMain
2d95b 29 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a  ));.#endif..  /*
2d95c 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63   Begin a transac
2d95d 74 69 6f 6e 20 2a 2f 0a 20 20 72 63 20 3d 20 65  tion */.  rc = e
2d95e 78 65 63 53 71 6c 28 64 62 2c 20 22 42 45 47 49  xecSql(db, "BEGI
2d95f 4e 20 45 58 43 4c 55 53 49 56 45 3b 22 29 3b 0a  N EXCLUSIVE;");.
2d960 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2d961 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f  _OK ) goto end_o
2d962 66 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20  f_vacuum;..  /* 
2d963 51 75 65 72 79 20 74 68 65 20 73 63 68 65 6d 61  Query the schema
2d964 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74   of the main dat
2d965 61 62 61 73 65 2e 20 43 72 65 61 74 65 20 61 20  abase. Create a 
2d966 6d 69 72 72 6f 72 20 73 63 68 65 6d 61 0a 20 20  mirror schema.  
2d967 2a 2a 20 69 6e 20 74 68 65 20 74 65 6d 70 6f 72  ** in the tempor
2d968 61 72 79 20 64 61 74 61 62 61 73 65 2e 0a 20 20  ary database..  
2d969 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78  */.  rc = execEx
2d96a 65 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20  ecSql(db, .     
2d96b 20 22 53 45 4c 45 43 54 20 27 43 52 45 41 54 45   "SELECT 'CREATE
2d96c 20 54 41 42 4c 45 20 76 61 63 75 75 6d 5f 64 62   TABLE vacuum_db
2d96d 2e 27 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c  .' || substr(sql
2d96e 2c 31 34 29 20 22 0a 20 20 20 20 20 20 22 20 20  ,14) ".      "  
2d96f 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74  FROM sqlite_mast
2d970 65 72 20 57 48 45 52 45 20 74 79 70 65 3d 27 74  er WHERE type='t
2d971 61 62 6c 65 27 20 41 4e 44 20 6e 61 6d 65 21 3d  able' AND name!=
2d972 27 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65  'sqlite_sequence
2d973 27 22 0a 20 20 20 20 20 20 22 20 20 20 41 4e 44  '".      "   AND
2d974 20 72 6f 6f 74 70 61 67 65 3e 30 22 0a 20 20 29   rootpage>0".  )
2d975 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2d976 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64  TE_OK ) goto end
2d977 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 72 63  _of_vacuum;.  rc
2d978 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28 64   = execExecSql(d
2d979 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43  b, .      "SELEC
2d97a 54 20 27 43 52 45 41 54 45 20 49 4e 44 45 58 20  T 'CREATE INDEX 
2d97b 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 73  vacuum_db.' || s
2d97c 75 62 73 74 72 28 73 71 6c 2c 31 34 29 22 0a 20  ubstr(sql,14)". 
2d97d 20 20 20 20 20 22 20 20 46 52 4f 4d 20 73 71 6c       "  FROM sql
2d97e 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45  ite_master WHERE
2d97f 20 73 71 6c 20 4c 49 4b 45 20 27 43 52 45 41 54   sql LIKE 'CREAT
2d980 45 20 49 4e 44 45 58 20 25 27 20 22 29 3b 0a 20  E INDEX %' ");. 
2d981 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2d982 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66  OK ) goto end_of
2d983 5f 76 61 63 75 75 6d 3b 0a 20 20 72 63 20 3d 20  _vacuum;.  rc = 
2d984 65 78 65 63 45 78 65 63 53 71 6c 28 64 62 2c 20  execExecSql(db, 
2d985 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 27  .      "SELECT '
2d986 43 52 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e  CREATE UNIQUE IN
2d987 44 45 58 20 76 61 63 75 75 6d 5f 64 62 2e 27 20  DEX vacuum_db.' 
2d988 7c 7c 20 73 75 62 73 74 72 28 73 71 6c 2c 32 31  || substr(sql,21
2d989 29 20 22 0a 20 20 20 20 20 20 22 20 20 46 52 4f  ) ".      "  FRO
2d98a 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  M sqlite_master 
2d98b 57 48 45 52 45 20 73 71 6c 20 4c 49 4b 45 20 27  WHERE sql LIKE '
2d98c 43 52 45 41 54 45 20 55 4e 49 51 55 45 20 49 4e  CREATE UNIQUE IN
2d98d 44 45 58 20 25 27 22 29 3b 0a 20 20 69 66 28 20  DEX %'");.  if( 
2d98e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2d98f 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75  goto end_of_vacu
2d990 75 6d 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70 20 74  um;..  /* Loop t
2d991 68 72 6f 75 67 68 20 74 68 65 20 74 61 62 6c 65  hrough the table
2d992 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64 61  s in the main da
2d993 74 61 62 61 73 65 2e 20 46 6f 72 20 65 61 63 68  tabase. For each
2d994 2c 20 64 6f 0a 20 20 2a 2a 20 61 6e 20 22 49 4e  , do.  ** an "IN
2d995 53 45 52 54 20 49 4e 54 4f 20 76 61 63 75 75 6d  SERT INTO vacuum
2d996 5f 64 62 2e 78 78 78 20 53 45 4c 45 43 54 20 2a  _db.xxx SELECT *
2d997 20 46 52 4f 4d 20 6d 61 69 6e 2e 78 78 78 3b 22   FROM main.xxx;"
2d998 20 74 6f 20 63 6f 70 79 0a 20 20 2a 2a 20 74 68   to copy.  ** th
2d999 65 20 63 6f 6e 74 65 6e 74 73 20 74 6f 20 74 68  e contents to th
2d99a 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61  e temporary data
2d99b 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20  base..  */.  rc 
2d99c 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28 64 62  = execExecSql(db
2d99d 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54  , .      "SELECT
2d99e 20 27 49 4e 53 45 52 54 20 49 4e 54 4f 20 76 61   'INSERT INTO va
2d99f 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 71 75 6f  cuum_db.' || quo
2d9a0 74 65 28 6e 61 6d 65 29 20 22 0a 20 20 20 20 20  te(name) ".     
2d9a1 20 22 7c 7c 20 27 20 53 45 4c 45 43 54 20 2a 20   "|| ' SELECT * 
2d9a2 46 52 4f 4d 20 6d 61 69 6e 2e 27 20 7c 7c 20 71  FROM main.' || q
2d9a3 75 6f 74 65 28 6e 61 6d 65 29 20 7c 7c 20 27 3b  uote(name) || ';
2d9a4 27 22 0a 20 20 20 20 20 20 22 46 52 4f 4d 20 6d  '".      "FROM m
2d9a5 61 69 6e 2e 73 71 6c 69 74 65 5f 6d 61 73 74 65  ain.sqlite_maste
2d9a6 72 20 22 0a 20 20 20 20 20 20 22 57 48 45 52 45  r ".      "WHERE
2d9a7 20 74 79 70 65 20 3d 20 27 74 61 62 6c 65 27 20   type = 'table' 
2d9a8 41 4e 44 20 6e 61 6d 65 21 3d 27 73 71 6c 69 74  AND name!='sqlit
2d9a9 65 5f 73 65 71 75 65 6e 63 65 27 20 22 0a 20 20  e_sequence' ".  
2d9aa 20 20 20 20 22 20 20 41 4e 44 20 72 6f 6f 74 70      "  AND rootp
2d9ab 61 67 65 3e 30 22 0a 0a 20 20 29 3b 0a 20 20 69  age>0"..  );.  i
2d9ac 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2d9ad 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76   ) goto end_of_v
2d9ae 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20 43 6f 70  acuum;..  /* Cop
2d9af 79 20 6f 76 65 72 20 74 68 65 20 73 65 71 75 65  y over the seque
2d9b0 6e 63 65 20 74 61 62 6c 65 0a 20 20 2a 2f 0a 20  nce table.  */. 
2d9b1 20 72 63 20 3d 20 65 78 65 63 45 78 65 63 53 71   rc = execExecSq
2d9b2 6c 28 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45  l(db, .      "SE
2d9b3 4c 45 43 54 20 27 44 45 4c 45 54 45 20 46 52 4f  LECT 'DELETE FRO
2d9b4 4d 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c  M vacuum_db.' ||
2d9b5 20 71 75 6f 74 65 28 6e 61 6d 65 29 20 7c 7c 20   quote(name) || 
2d9b6 27 3b 27 20 22 0a 20 20 20 20 20 20 22 46 52 4f  ';' ".      "FRO
2d9b7 4d 20 76 61 63 75 75 6d 5f 64 62 2e 73 71 6c 69  M vacuum_db.sqli
2d9b8 74 65 5f 6d 61 73 74 65 72 20 57 48 45 52 45 20  te_master WHERE 
2d9b9 6e 61 6d 65 3d 27 73 71 6c 69 74 65 5f 73 65 71  name='sqlite_seq
2d9ba 75 65 6e 63 65 27 20 22 0a 20 20 29 3b 0a 20 20  uence' ".  );.  
2d9bb 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2d9bc 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f  K ) goto end_of_
2d9bd 76 61 63 75 75 6d 3b 0a 20 20 72 63 20 3d 20 65  vacuum;.  rc = e
2d9be 78 65 63 45 78 65 63 53 71 6c 28 64 62 2c 20 0a  xecExecSql(db, .
2d9bf 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 27 49        "SELECT 'I
2d9c0 4e 53 45 52 54 20 49 4e 54 4f 20 76 61 63 75 75  NSERT INTO vacuu
2d9c1 6d 5f 64 62 2e 27 20 7c 7c 20 71 75 6f 74 65 28  m_db.' || quote(
2d9c2 6e 61 6d 65 29 20 22 0a 20 20 20 20 20 20 22 7c  name) ".      "|
2d9c3 7c 20 27 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  | ' SELECT * FRO
2d9c4 4d 20 6d 61 69 6e 2e 27 20 7c 7c 20 71 75 6f 74  M main.' || quot
2d9c5 65 28 6e 61 6d 65 29 20 7c 7c 20 27 3b 27 20 22  e(name) || ';' "
2d9c6 0a 20 20 20 20 20 20 22 46 52 4f 4d 20 76 61 63  .      "FROM vac
2d9c7 75 75 6d 5f 64 62 2e 73 71 6c 69 74 65 5f 6d 61  uum_db.sqlite_ma
2d9c8 73 74 65 72 20 57 48 45 52 45 20 6e 61 6d 65 3d  ster WHERE name=
2d9c9 3d 27 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  ='sqlite_sequenc
2d9ca 65 27 3b 22 0a 20 20 29 3b 0a 20 20 69 66 28 20  e';".  );.  if( 
2d9cb 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2d9cc 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75  goto end_of_vacu
2d9cd 75 6d 3b 0a 0a 0a 20 20 2f 2a 20 43 6f 70 79 20  um;...  /* Copy 
2d9ce 74 68 65 20 74 72 69 67 67 65 72 73 2c 20 76 69  the triggers, vi
2d9cf 65 77 73 2c 20 61 6e 64 20 76 69 72 74 75 61 6c  ews, and virtual
2d9d0 20 74 61 62 6c 65 73 20 66 72 6f 6d 20 74 68 65   tables from the
2d9d1 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 0a 20   main database. 
2d9d2 20 2a 2a 20 6f 76 65 72 20 74 6f 20 74 68 65 20   ** over to the 
2d9d3 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61  temporary databa
2d9d4 73 65 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 65  se.  None of the
2d9d5 73 65 20 6f 62 6a 65 63 74 73 20 68 61 73 20 61  se objects has a
2d9d6 6e 79 0a 20 20 2a 2a 20 61 73 73 6f 63 69 61 74  ny.  ** associat
2d9d7 65 64 20 73 74 6f 72 61 67 65 2c 20 73 6f 20 61  ed storage, so a
2d9d8 6c 6c 20 77 65 20 68 61 76 65 20 74 6f 20 64 6f  ll we have to do
2d9d9 20 69 73 20 63 6f 70 79 20 74 68 65 69 72 20 65   is copy their e
2d9da 6e 74 72 69 65 73 0a 20 20 2a 2a 20 66 72 6f 6d  ntries.  ** from
2d9db 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54   the SQLITE_MAST
2d9dc 45 52 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20  ER table..  */. 
2d9dd 20 72 63 20 3d 20 65 78 65 63 53 71 6c 28 64 62   rc = execSql(db
2d9de 2c 0a 20 20 20 20 20 20 22 49 4e 53 45 52 54 20  ,.      "INSERT 
2d9df 49 4e 54 4f 20 76 61 63 75 75 6d 5f 64 62 2e 73  INTO vacuum_db.s
2d9e0 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 22 0a 20  qlite_master ". 
2d9e1 20 20 20 20 20 22 20 20 53 45 4c 45 43 54 20 74       "  SELECT t
2d9e2 79 70 65 2c 20 6e 61 6d 65 2c 20 74 62 6c 5f 6e  ype, name, tbl_n
2d9e3 61 6d 65 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73  ame, rootpage, s
2d9e4 71 6c 22 0a 20 20 20 20 20 20 22 20 20 20 20 46  ql".      "    F
2d9e5 52 4f 4d 20 6d 61 69 6e 2e 73 71 6c 69 74 65 5f  ROM main.sqlite_
2d9e6 6d 61 73 74 65 72 22 0a 20 20 20 20 20 20 22 20  master".      " 
2d9e7 20 20 57 48 45 52 45 20 74 79 70 65 3d 27 76 69    WHERE type='vi
2d9e8 65 77 27 20 4f 52 20 74 79 70 65 3d 27 74 72 69  ew' OR type='tri
2d9e9 67 67 65 72 27 22 0a 20 20 20 20 20 20 22 20 20  gger'".      "  
2d9ea 20 20 20 20 4f 52 20 28 74 79 70 65 3d 27 74 61      OR (type='ta
2d9eb 62 6c 65 27 20 41 4e 44 20 72 6f 6f 74 70 61 67  ble' AND rootpag
2d9ec 65 3d 30 29 22 0a 20 20 29 3b 0a 20 20 69 66 28  e=0)".  );.  if(
2d9ed 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f   rc ) goto end_o
2d9ee 66 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20  f_vacuum;..  /* 
2d9ef 41 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 75  At this point, u
2d9f0 6e 6c 65 73 73 20 74 68 65 20 6d 61 69 6e 20 64  nless the main d
2d9f1 62 20 77 61 73 20 63 6f 6d 70 6c 65 74 65 6c 79  b was completely
2d9f2 20 65 6d 70 74 79 2c 20 74 68 65 72 65 20 69 73   empty, there is
2d9f3 20 6e 6f 77 20 61 0a 20 20 2a 2a 20 74 72 61 6e   now a.  ** tran
2d9f4 73 61 63 74 69 6f 6e 20 6f 70 65 6e 20 6f 6e 20  saction open on 
2d9f5 74 68 65 20 76 61 63 75 75 6d 20 64 61 74 61 62  the vacuum datab
2d9f6 61 73 65 2c 20 62 75 74 20 6e 6f 74 20 6f 6e 20  ase, but not on 
2d9f7 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
2d9f8 65 2e 0a 20 20 2a 2a 20 4f 70 65 6e 20 61 20 62  e..  ** Open a b
2d9f9 74 72 65 65 20 6c 65 76 65 6c 20 74 72 61 6e 73  tree level trans
2d9fa 61 63 74 69 6f 6e 20 6f 6e 20 74 68 65 20 6d 61  action on the ma
2d9fb 69 6e 20 64 61 74 61 62 61 73 65 2e 20 54 68 69  in database. Thi
2d9fc 73 20 61 6c 6c 6f 77 73 20 61 0a 20 20 2a 2a 20  s allows a.  ** 
2d9fd 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 42  call to sqlite3B
2d9fe 74 72 65 65 43 6f 70 79 46 69 6c 65 28 29 2e 20  treeCopyFile(). 
2d9ff 54 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  The main databas
2da00 65 20 62 74 72 65 65 20 6c 65 76 65 6c 0a 20 20  e btree level.  
2da01 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  ** transaction i
2da02 73 20 74 68 65 6e 20 63 6f 6d 6d 69 74 74 65 64  s then committed
2da03 2c 20 73 6f 20 74 68 65 20 53 51 4c 20 6c 65 76  , so the SQL lev
2da04 65 6c 20 6e 65 76 65 72 20 6b 6e 6f 77 73 20 69  el never knows i
2da05 74 20 77 61 73 0a 20 20 2a 2a 20 6f 70 65 6e 65  t was.  ** opene
2da06 64 20 66 6f 72 20 77 72 69 74 69 6e 67 2e 20 54  d for writing. T
2da07 68 69 73 20 77 61 79 2c 20 74 68 65 20 53 51 4c  his way, the SQL
2da08 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 75 73 65   transaction use
2da09 64 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 0a  d to create the.
2da0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 64    ** temporary d
2da0b 61 74 61 62 61 73 65 20 6e 65 76 65 72 20 6e 65  atabase never ne
2da0c 65 64 73 20 74 6f 20 62 65 20 63 6f 6d 6d 69 74  eds to be commit
2da0d 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 7b 0a 20 20  ted..  */.  {.  
2da0e 20 20 75 33 32 20 6d 65 74 61 3b 0a 20 20 20 20    u32 meta;.    
2da0f 69 6e 74 20 69 3b 0a 0a 20 20 20 20 2f 2a 20 54  int i;..    /* T
2da10 68 69 73 20 61 72 72 61 79 20 64 65 74 65 72 6d  his array determ
2da11 69 6e 65 73 20 77 68 69 63 68 20 6d 65 74 61 20  ines which meta 
2da12 6d 65 74 61 20 76 61 6c 75 65 73 20 61 72 65 20  meta values are 
2da13 70 72 65 73 65 72 76 65 64 20 69 6e 20 74 68 65  preserved in the
2da14 0a 20 20 20 20 2a 2a 20 76 61 63 75 75 6d 2e 20  .    ** vacuum. 
2da15 20 45 76 65 6e 20 65 6e 74 72 69 65 73 20 61 72   Even entries ar
2da16 65 20 74 68 65 20 6d 65 74 61 20 76 61 6c 75 65  e the meta value
2da17 20 6e 75 6d 62 65 72 20 61 6e 64 20 6f 64 64 20   number and odd 
2da18 65 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a 20 61  entries.    ** a
2da19 72 65 20 61 6e 20 69 6e 63 72 65 6d 65 6e 74 20  re an increment 
2da1a 74 6f 20 61 70 70 6c 79 20 74 6f 20 74 68 65 20  to apply to the 
2da1b 6d 65 74 61 20 76 61 6c 75 65 20 61 66 74 65 72  meta value after
2da1c 20 74 68 65 20 76 61 63 75 75 6d 2e 0a 20 20 20   the vacuum..   
2da1d 20 2a 2a 20 54 68 65 20 69 6e 63 72 65 6d 65 6e   ** The incremen
2da1e 74 20 69 73 20 75 73 65 64 20 74 6f 20 69 6e 63  t is used to inc
2da1f 72 65 61 73 65 20 74 68 65 20 73 63 68 65 6d 61  rease the schema
2da20 20 63 6f 6f 6b 69 65 20 73 6f 20 74 68 61 74 20   cookie so that 
2da21 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 63 6f 6e  other.    ** con
2da22 6e 65 63 74 69 6f 6e 73 20 74 6f 20 74 68 65 20  nections to the 
2da23 73 61 6d 65 20 64 61 74 61 62 61 73 65 20 77 69  same database wi
2da24 6c 6c 20 6b 6e 6f 77 20 74 6f 20 72 65 72 65 61  ll know to rerea
2da25 64 20 74 68 65 20 73 63 68 65 6d 61 2e 0a 20 20  d the schema..  
2da26 20 20 2a 2f 0a 20 20 20 20 73 74 61 74 69 63 20    */.    static 
2da27 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
2da28 68 61 72 20 61 43 6f 70 79 5b 5d 20 3d 20 7b 0a  har aCopy[] = {.
2da29 20 20 20 20 20 20 20 42 54 52 45 45 5f 53 43 48         BTREE_SCH
2da2a 45 4d 41 5f 56 45 52 53 49 4f 4e 2c 20 20 20 20  EMA_VERSION,    
2da2b 20 31 2c 20 20 2f 2a 20 41 64 64 20 6f 6e 65 20   1,  /* Add one 
2da2c 74 6f 20 74 68 65 20 6f 6c 64 20 73 63 68 65 6d  to the old schem
2da2d 61 20 63 6f 6f 6b 69 65 20 2a 2f 0a 20 20 20 20  a cookie */.    
2da2e 20 20 20 42 54 52 45 45 5f 44 45 46 41 55 4c 54     BTREE_DEFAULT
2da2f 5f 43 41 43 48 45 5f 53 49 5a 45 2c 20 30 2c 20  _CACHE_SIZE, 0, 
2da30 20 2f 2a 20 50 72 65 73 65 72 76 65 20 74 68 65   /* Preserve the
2da31 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 63 61   default page ca
2da32 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20  che size */.    
2da33 20 20 20 42 54 52 45 45 5f 54 45 58 54 5f 45 4e     BTREE_TEXT_EN
2da34 43 4f 44 49 4e 47 2c 20 20 20 20 20 20 30 2c 20  CODING,      0, 
2da35 20 2f 2a 20 50 72 65 73 65 72 76 65 20 74 68 65   /* Preserve the
2da36 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a   text encoding *
2da37 2f 0a 20 20 20 20 20 20 20 42 54 52 45 45 5f 55  /.       BTREE_U
2da38 53 45 52 5f 56 45 52 53 49 4f 4e 2c 20 20 20 20  SER_VERSION,    
2da39 20 20 20 30 2c 20 20 2f 2a 20 50 72 65 73 65 72     0,  /* Preser
2da3a 76 65 20 74 68 65 20 75 73 65 72 20 76 65 72 73  ve the user vers
2da3b 69 6f 6e 20 2a 2f 0a 20 20 20 20 7d 3b 0a 0a 20  ion */.    };.. 
2da3c 20 20 20 61 73 73 65 72 74 28 20 31 3d 3d 73 71     assert( 1==sq
2da3d 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72  lite3BtreeIsInTr
2da3e 61 6e 73 28 70 54 65 6d 70 29 20 29 3b 0a 20 20  ans(pTemp) );.  
2da3f 20 20 61 73 73 65 72 74 28 20 31 3d 3d 73 71 6c    assert( 1==sql
2da40 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61  ite3BtreeIsInTra
2da41 6e 73 28 70 4d 61 69 6e 29 20 29 3b 0a 0a 20 20  ns(pMain) );..  
2da42 20 20 2f 2a 20 43 6f 70 79 20 42 74 72 65 65 20    /* Copy Btree 
2da43 6d 65 74 61 20 76 61 6c 75 65 73 20 2a 2f 0a 20  meta values */. 
2da44 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72     for(i=0; i<Ar
2da45 72 61 79 53 69 7a 65 28 61 43 6f 70 79 29 3b 20  raySize(aCopy); 
2da46 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 2f 2a 20  i+=2){.      /* 
2da47 47 65 74 4d 65 74 61 28 29 20 61 6e 64 20 55 70  GetMeta() and Up
2da48 64 61 74 65 4d 65 74 61 28 29 20 63 61 6e 6e 6f  dateMeta() canno
2da49 74 20 66 61 69 6c 20 69 6e 20 74 68 69 73 20 63  t fail in this c
2da4a 6f 6e 74 65 78 74 20 62 65 63 61 75 73 65 0a 20  ontext because. 
2da4b 20 20 20 20 20 2a 2a 20 77 65 20 61 6c 72 65 61       ** we alrea
2da4c 64 79 20 68 61 76 65 20 70 61 67 65 20 31 20 6c  dy have page 1 l
2da4d 6f 61 64 65 64 20 69 6e 74 6f 20 63 61 63 68 65  oaded into cache
2da4e 20 61 6e 64 20 6d 61 72 6b 65 64 20 64 69 72 74   and marked dirt
2da4f 79 2e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  y. */.      sqli
2da50 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61 28  te3BtreeGetMeta(
2da51 70 4d 61 69 6e 2c 20 61 43 6f 70 79 5b 69 5d 2c  pMain, aCopy[i],
2da52 20 26 6d 65 74 61 29 3b 0a 20 20 20 20 20 20 72   &meta);.      r
2da53 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
2da54 55 70 64 61 74 65 4d 65 74 61 28 70 54 65 6d 70  UpdateMeta(pTemp
2da55 2c 20 61 43 6f 70 79 5b 69 5d 2c 20 6d 65 74 61  , aCopy[i], meta
2da56 2b 61 43 6f 70 79 5b 69 2b 31 5d 29 3b 0a 20 20  +aCopy[i+1]);.  
2da57 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 72 63      if( NEVER(rc
2da58 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29 20 67  !=SQLITE_OK) ) g
2da59 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75  oto end_of_vacuu
2da5a 6d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 63  m;.    }..    rc
2da5b 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43   = sqlite3BtreeC
2da5c 6f 70 79 46 69 6c 65 28 70 4d 61 69 6e 2c 20 70  opyFile(pMain, p
2da5d 54 65 6d 70 29 3b 0a 20 20 20 20 69 66 28 20 72  Temp);.    if( r
2da5e 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
2da5f 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75  oto end_of_vacuu
2da60 6d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  m;.    rc = sqli
2da61 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 28 70  te3BtreeCommit(p
2da62 54 65 6d 70 29 3b 0a 20 20 20 20 69 66 28 20 72  Temp);.    if( r
2da63 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67  c!=SQLITE_OK ) g
2da64 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75  oto end_of_vacuu
2da65 6d 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  m;.#ifndef SQLIT
2da66 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
2da67 4d 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  M.    sqlite3Btr
2da68 65 65 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28  eeSetAutoVacuum(
2da69 70 4d 61 69 6e 2c 20 73 71 6c 69 74 65 33 42 74  pMain, sqlite3Bt
2da6a 72 65 65 47 65 74 41 75 74 6f 56 61 63 75 75 6d  reeGetAutoVacuum
2da6b 28 70 54 65 6d 70 29 29 3b 0a 23 65 6e 64 69 66  (pTemp));.#endif
2da6c 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
2da6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc==SQLITE_OK );
2da6e 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
2da6f 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28  treeSetPageSize(
2da70 70 4d 61 69 6e 2c 20 73 71 6c 69 74 65 33 42 74  pMain, sqlite3Bt
2da71 72 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70  reeGetPageSize(p
2da72 54 65 6d 70 29 2c 20 6e 52 65 73 2c 31 29 3b 0a  Temp), nRes,1);.
2da73 0a 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3a 0a  .end_of_vacuum:.
2da74 20 20 2f 2a 20 52 65 73 74 6f 72 65 20 74 68 65    /* Restore the
2da75 20 6f 72 69 67 69 6e 61 6c 20 76 61 6c 75 65 20   original value 
2da76 6f 66 20 64 62 2d 3e 66 6c 61 67 73 20 2a 2f 0a  of db->flags */.
2da77 20 20 64 62 2d 3e 66 6c 61 67 73 20 3d 20 73 61    db->flags = sa
2da78 76 65 64 5f 66 6c 61 67 73 3b 0a 20 20 64 62 2d  ved_flags;.  db-
2da79 3e 6e 43 68 61 6e 67 65 20 3d 20 73 61 76 65 64  >nChange = saved
2da7a 5f 6e 43 68 61 6e 67 65 3b 0a 20 20 64 62 2d 3e  _nChange;.  db->
2da7b 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 3d 20 73  nTotalChange = s
2da7c 61 76 65 64 5f 6e 54 6f 74 61 6c 43 68 61 6e 67  aved_nTotalChang
2da7d 65 3b 0a 0a 20 20 2f 2a 20 43 75 72 72 65 6e 74  e;..  /* Current
2da7e 6c 79 20 74 68 65 72 65 20 69 73 20 61 6e 20 53  ly there is an S
2da7f 51 4c 20 6c 65 76 65 6c 20 74 72 61 6e 73 61 63  QL level transac
2da80 74 69 6f 6e 20 6f 70 65 6e 20 6f 6e 20 74 68 65  tion open on the
2da81 20 76 61 63 75 75 6d 0a 20 20 2a 2a 20 64 61 74   vacuum.  ** dat
2da82 61 62 61 73 65 2e 20 4e 6f 20 6c 6f 63 6b 73 20  abase. No locks 
2da83 61 72 65 20 68 65 6c 64 20 6f 6e 20 61 6e 79 20  are held on any 
2da84 6f 74 68 65 72 20 66 69 6c 65 73 20 28 73 69 6e  other files (sin
2da85 63 65 20 74 68 65 20 6d 61 69 6e 20 66 69 6c 65  ce the main file
2da86 0a 20 20 2a 2a 20 77 61 73 20 63 6f 6d 6d 69 74  .  ** was commit
2da87 74 65 64 20 61 74 20 74 68 65 20 62 74 72 65 65  ted at the btree
2da88 20 6c 65 76 65 6c 29 2e 20 53 6f 20 69 74 20 73   level). So it s
2da89 61 66 65 20 74 6f 20 65 6e 64 20 74 68 65 20 74  afe to end the t
2da8a 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a 20  ransaction.  ** 
2da8b 62 79 20 6d 61 6e 75 61 6c 6c 79 20 73 65 74 74  by manually sett
2da8c 69 6e 67 20 74 68 65 20 61 75 74 6f 43 6f 6d 6d  ing the autoComm
2da8d 69 74 20 66 6c 61 67 20 74 6f 20 74 72 75 65 20  it flag to true 
2da8e 61 6e 64 20 64 65 74 61 63 68 69 6e 67 20 74 68  and detaching th
2da8f 65 0a 20 20 2a 2a 20 76 61 63 75 75 6d 20 64 61  e.  ** vacuum da
2da90 74 61 62 61 73 65 2e 20 54 68 65 20 76 61 63 75  tabase. The vacu
2da91 75 6d 5f 64 62 20 6a 6f 75 72 6e 61 6c 20 66 69  um_db journal fi
2da92 6c 65 20 69 73 20 64 65 6c 65 74 65 64 20 77 68  le is deleted wh
2da93 65 6e 20 74 68 65 20 70 61 67 65 72 0a 20 20 2a  en the pager.  *
2da94 2a 20 69 73 20 63 6c 6f 73 65 64 20 62 79 20 74  * is closed by t
2da95 68 65 20 44 45 54 41 43 48 2e 0a 20 20 2a 2f 0a  he DETACH..  */.
2da96 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74    db->autoCommit
2da97 20 3d 20 31 3b 0a 0a 20 20 69 66 28 20 70 44 62   = 1;..  if( pDb
2da98 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   ){.    sqlite3B
2da99 74 72 65 65 43 6c 6f 73 65 28 70 44 62 2d 3e 70  treeClose(pDb->p
2da9a 42 74 29 3b 0a 20 20 20 20 70 44 62 2d 3e 70 42  Bt);.    pDb->pB
2da9b 74 20 3d 20 30 3b 0a 20 20 20 20 70 44 62 2d 3e  t = 0;.    pDb->
2da9c 70 53 63 68 65 6d 61 20 3d 20 30 3b 0a 20 20 7d  pSchema = 0;.  }
2da9d 0a 0a 20 20 73 71 6c 69 74 65 33 52 65 73 65 74  ..  sqlite3Reset
2da9e 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
2da9f 62 2c 20 30 29 3b 0a 0a 20 20 72 65 74 75 72 6e  b, 0);..  return
2daa0 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 20 2f   rc;.}.#endif  /
2daa1 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41  * SQLITE_OMIT_VA
2daa2 43 55 55 4d 20 26 26 20 53 51 4c 49 54 45 5f 4f  CUUM && SQLITE_O
2daa3 4d 49 54 5f 41 54 54 41 43 48 20 2a 2f 0a 0a 2f  MIT_ATTACH */../
2daa4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
2daa5 6e 64 20 6f 66 20 76 61 63 75 75 6d 2e 63 20 2a  nd of vacuum.c *
2daa6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2daa7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2daa8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2daa9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
2daaa 65 67 69 6e 20 66 69 6c 65 20 76 74 61 62 2e 63  egin file vtab.c
2daab 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2daac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2daad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2daae 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6e 65 20 31  *.** 2006 June 1
2daaf 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  0.**.** The auth
2dab0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
2dab1 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
2dab2 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
2dab3 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
2dab4 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
2dab5 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
2dab6 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
2dab7 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
2dab8 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
2dab9 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
2daba 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
2dabb 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
2dabc 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
2dabd 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
2dabe 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
2dabf 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
2dac0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
2dac1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dac2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dac3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dac4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dac5 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
2dac6 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64  ntains code used
2dac7 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65   to help impleme
2dac8 6e 74 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  nt virtual table
2dac9 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 74  s..**.** $Id: vt
2daca 61 62 2e 63 2c 76 20 31 2e 39 34 20 32 30 30 39  ab.c,v 1.94 2009
2dacb 2f 30 38 2f 30 38 20 31 38 3a 30 31 3a 30 38 20  /08/08 18:01:08 
2dacc 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66  drh Exp $.*/.#if
2dacd 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2dace 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 0a 2f  _VIRTUALTABLE../
2dacf 2a 0a 2a 2a 20 54 68 65 20 61 63 74 75 61 6c 20  *.** The actual 
2dad0 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 64 6f  function that do
2dad1 65 73 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 63  es the work of c
2dad2 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20 6d 6f  reating a new mo
2dad3 64 75 6c 65 2e 0a 2a 2a 20 54 68 69 73 20 66 75  dule..** This fu
2dad4 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  nction implement
2dad5 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 72  s the sqlite3_cr
2dad6 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20 61 6e  eate_module() an
2dad7 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63 72 65  d.** sqlite3_cre
2dad8 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 29 20  ate_module_v2() 
2dad9 69 6e 74 65 72 66 61 63 65 73 2e 0a 2a 2f 0a 73  interfaces..*/.s
2dada 74 61 74 69 63 20 69 6e 74 20 63 72 65 61 74 65  tatic int create
2dadb 4d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65  Module(.  sqlite
2dadc 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
2dadd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
2dade 61 62 61 73 65 20 69 6e 20 77 68 69 63 68 20 6d  abase in which m
2dadf 6f 64 75 6c 65 20 69 73 20 72 65 67 69 73 74 65  odule is registe
2dae0 72 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  red */.  const c
2dae1 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20  har *zName,     
2dae2 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
2dae3 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68 69   assigned to thi
2dae4 73 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f  s module */.  co
2dae5 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  nst sqlite3_modu
2dae6 6c 65 20 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a  le *pModule,  /*
2dae7 20 54 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 20   The definition 
2dae8 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f  of the module */
2dae9 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 20 20  .  void *pAux,  
2daea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2daeb 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 70 6f     /* Context po
2daec 69 6e 74 65 72 20 66 6f 72 20 78 43 72 65 61 74  inter for xCreat
2daed 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20 20  e/xConnect */.  
2daee 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29  void (*xDestroy)
2daef 28 76 6f 69 64 20 2a 29 20 20 20 20 20 20 20 20  (void *)        
2daf0 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74 72 75  /* Module destru
2daf1 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  ctor function */
2daf2 0a 29 7b 0a 20 20 69 6e 74 20 72 63 2c 20 6e 4e  .){.  int rc, nN
2daf3 61 6d 65 3b 0a 20 20 4d 6f 64 75 6c 65 20 2a 70  ame;.  Module *p
2daf4 4d 6f 64 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  Mod;..  sqlite3_
2daf5 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
2daf6 6d 75 74 65 78 29 3b 0a 20 20 6e 4e 61 6d 65 20  mutex);.  nName 
2daf7 3d 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  = sqlite3Strlen3
2daf8 30 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 4d 6f 64  0(zName);.  pMod
2daf9 20 3d 20 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c   = (Module *)sql
2dafa 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
2dafb 64 62 2c 20 73 69 7a 65 6f 66 28 4d 6f 64 75 6c  db, sizeof(Modul
2dafc 65 29 20 2b 20 6e 4e 61 6d 65 20 2b 20 31 29 3b  e) + nName + 1);
2dafd 0a 20 20 69 66 28 20 70 4d 6f 64 20 29 7b 0a 20  .  if( pMod ){. 
2dafe 20 20 20 4d 6f 64 75 6c 65 20 2a 70 44 65 6c 3b     Module *pDel;
2daff 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 70 79  .    char *zCopy
2db00 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70 4d 6f   = (char *)(&pMo
2db01 64 5b 31 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70  d[1]);.    memcp
2db02 79 28 7a 43 6f 70 79 2c 20 7a 4e 61 6d 65 2c 20  y(zCopy, zName, 
2db03 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 70 4d  nName+1);.    pM
2db04 6f 64 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 43 6f 70  od->zName = zCop
2db05 79 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e 70 4d 6f  y;.    pMod->pMo
2db06 64 75 6c 65 20 3d 20 70 4d 6f 64 75 6c 65 3b 0a  dule = pModule;.
2db07 20 20 20 20 70 4d 6f 64 2d 3e 70 41 75 78 20 3d      pMod->pAux =
2db08 20 70 41 75 78 3b 0a 20 20 20 20 70 4d 6f 64 2d   pAux;.    pMod-
2db09 3e 78 44 65 73 74 72 6f 79 20 3d 20 78 44 65 73  >xDestroy = xDes
2db0a 74 72 6f 79 3b 0a 20 20 20 20 70 44 65 6c 20 3d  troy;.    pDel =
2db0b 20 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74   (Module *)sqlit
2db0c 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 64 62  e3HashInsert(&db
2db0d 2d 3e 61 4d 6f 64 75 6c 65 2c 20 7a 43 6f 70 79  ->aModule, zCopy
2db0e 2c 20 6e 4e 61 6d 65 2c 20 28 76 6f 69 64 2a 29  , nName, (void*)
2db0f 70 4d 6f 64 29 3b 0a 20 20 20 20 69 66 28 20 70  pMod);.    if( p
2db10 44 65 6c 20 26 26 20 70 44 65 6c 2d 3e 78 44 65  Del && pDel->xDe
2db11 73 74 72 6f 79 20 29 7b 0a 20 20 20 20 20 20 70  stroy ){.      p
2db12 44 65 6c 2d 3e 78 44 65 73 74 72 6f 79 28 70 44  Del->xDestroy(pD
2db13 65 6c 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20 7d  el->pAux);.    }
2db14 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
2db15 65 65 28 64 62 2c 20 70 44 65 6c 29 3b 0a 20 20  ee(db, pDel);.  
2db16 20 20 69 66 28 20 70 44 65 6c 3d 3d 70 4d 6f 64    if( pDel==pMod
2db17 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61   ){.      db->ma
2db18 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
2db19 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2db1a 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63  3ResetInternalSc
2db1b 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d  hema(db, 0);.  }
2db1c 65 6c 73 65 20 69 66 28 20 78 44 65 73 74 72 6f  else if( xDestro
2db1d 79 20 29 7b 0a 20 20 20 20 78 44 65 73 74 72 6f  y ){.    xDestro
2db1e 79 28 70 41 75 78 29 3b 0a 20 20 7d 0a 20 20 72  y(pAux);.  }.  r
2db1f 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
2db20 69 74 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b  it(db, SQLITE_OK
2db21 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
2db22 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
2db23 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
2db24 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 65  ;.}.../*.** Exte
2db25 72 6e 61 6c 20 41 50 49 20 66 75 6e 63 74 69 6f  rnal API functio
2db26 6e 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65  n used to create
2db27 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 2d 74   a new virtual-t
2db28 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a  able module..*/.
2db29 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2db2a 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f  qlite3_create_mo
2db2b 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 20  dule(.  sqlite3 
2db2c 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
2db2d 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
2db2e 61 73 65 20 69 6e 20 77 68 69 63 68 20 6d 6f 64  ase in which mod
2db2f 75 6c 65 20 69 73 20 72 65 67 69 73 74 65 72 65  ule is registere
2db30 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  d */.  const cha
2db31 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20  r *zName,       
2db32 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 61         /* Name a
2db33 73 73 69 67 6e 65 64 20 74 6f 20 74 68 69 73 20  ssigned to this 
2db34 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  module */.  cons
2db35 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
2db36 20 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20 54   *pModule,  /* T
2db37 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66  he definition of
2db38 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20   the module */. 
2db39 20 76 6f 69 64 20 2a 70 41 75 78 20 20 20 20 20   void *pAux     
2db3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2db3b 20 2f 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e   /* Context poin
2db3c 74 65 72 20 66 6f 72 20 78 43 72 65 61 74 65 2f  ter for xCreate/
2db3d 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29 7b 0a 20  xConnect */.){. 
2db3e 20 72 65 74 75 72 6e 20 63 72 65 61 74 65 4d 6f   return createMo
2db3f 64 75 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  dule(db, zName, 
2db40 70 4d 6f 64 75 6c 65 2c 20 70 41 75 78 2c 20 30  pModule, pAux, 0
2db41 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 65  );.}../*.** Exte
2db42 72 6e 61 6c 20 41 50 49 20 66 75 6e 63 74 69 6f  rnal API functio
2db43 6e 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65  n used to create
2db44 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 2d 74   a new virtual-t
2db45 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a  able module..*/.
2db46 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2db47 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f  qlite3_create_mo
2db48 64 75 6c 65 5f 76 32 28 0a 20 20 73 71 6c 69 74  dule_v2(.  sqlit
2db49 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
2db4a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
2db4b 74 61 62 61 73 65 20 69 6e 20 77 68 69 63 68 20  tabase in which 
2db4c 6d 6f 64 75 6c 65 20 69 73 20 72 65 67 69 73 74  module is regist
2db4d 65 72 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ered */.  const 
2db4e 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20  char *zName,    
2db4f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
2db50 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68  e assigned to th
2db51 69 73 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63  is module */.  c
2db52 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
2db53 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f  ule *pModule,  /
2db54 2a 20 54 68 65 20 64 65 66 69 6e 69 74 69 6f 6e  * The definition
2db55 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a   of the module *
2db56 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c 20  /.  void *pAux, 
2db57 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2db58 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 70      /* Context p
2db59 6f 69 6e 74 65 72 20 66 6f 72 20 78 43 72 65 61  ointer for xCrea
2db5a 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 20  te/xConnect */. 
2db5b 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79   void (*xDestroy
2db5c 29 28 76 6f 69 64 20 2a 29 20 20 20 20 20 20 20  )(void *)       
2db5d 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74 72   /* Module destr
2db5e 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a  uctor function *
2db5f 2f 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 72  /.){.  return cr
2db60 65 61 74 65 4d 6f 64 75 6c 65 28 64 62 2c 20 7a  eateModule(db, z
2db61 4e 61 6d 65 2c 20 70 4d 6f 64 75 6c 65 2c 20 70  Name, pModule, p
2db62 41 75 78 2c 20 78 44 65 73 74 72 6f 79 29 3b 0a  Aux, xDestroy);.
2db63 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68  }../*.** Lock th
2db64 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
2db65 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 6e 6f  so that it canno
2db66 74 20 62 65 20 64 69 73 63 6f 6e 6e 65 63 74 65  t be disconnecte
2db67 64 2e 0a 2a 2a 20 4c 6f 63 6b 73 20 6e 65 73 74  d..** Locks nest
2db68 2e 20 20 45 76 65 72 79 20 6c 6f 63 6b 20 73 68  .  Every lock sh
2db69 6f 75 6c 64 20 68 61 76 65 20 61 20 63 6f 72 72  ould have a corr
2db6a 65 73 70 6f 6e 64 69 6e 67 20 75 6e 6c 6f 63 6b  esponding unlock
2db6b 2e 0a 2a 2a 20 49 66 20 61 6e 20 75 6e 6c 6f 63  ..** If an unloc
2db6c 6b 20 69 73 20 6f 6d 69 74 74 65 64 2c 20 72 65  k is omitted, re
2db6d 73 6f 75 72 63 65 73 20 6c 65 61 6b 73 20 77 69  sources leaks wi
2db6e 6c 6c 20 6f 63 63 75 72 2e 20 20 0a 2a 2a 0a 2a  ll occur.  .**.*
2db6f 2a 20 49 66 20 61 20 64 69 73 63 6f 6e 6e 65 63  * If a disconnec
2db70 74 20 69 73 20 61 74 74 65 6d 70 74 65 64 20 77  t is attempted w
2db71 68 69 6c 65 20 61 20 76 69 72 74 75 61 6c 20 74  hile a virtual t
2db72 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 2c 0a  able is locked,.
2db73 2a 2a 20 74 68 65 20 64 69 73 63 6f 6e 6e 65 63  ** the disconnec
2db74 74 20 69 73 20 64 65 66 65 72 72 65 64 20 75 6e  t is deferred un
2db75 74 69 6c 20 61 6c 6c 20 6c 6f 63 6b 73 20 68 61  til all locks ha
2db76 76 65 20 62 65 65 6e 20 72 65 6d 6f 76 65 64 2e  ve been removed.
2db77 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
2db78 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
2db79 74 61 62 4c 6f 63 6b 28 56 54 61 62 6c 65 20 2a  tabLock(VTable *
2db7a 70 56 54 61 62 29 7b 0a 20 20 70 56 54 61 62 2d  pVTab){.  pVTab-
2db7b 3e 6e 52 65 66 2b 2b 3b 0a 7d 0a 0a 0a 2f 2a 0a  >nRef++;.}.../*.
2db7c 2a 2a 20 70 54 61 62 20 69 73 20 61 20 70 6f 69  ** pTab is a poi
2db7d 6e 74 65 72 20 74 6f 20 61 20 54 61 62 6c 65 20  nter to a Table 
2db7e 73 74 72 75 63 74 75 72 65 20 72 65 70 72 65 73  structure repres
2db7f 65 6e 74 69 6e 67 20 61 20 76 69 72 74 75 61 6c  enting a virtual
2db80 2d 74 61 62 6c 65 2e 0a 2a 2a 20 52 65 74 75 72  -table..** Retur
2db81 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
2db82 68 65 20 56 54 61 62 6c 65 20 6f 62 6a 65 63 74  he VTable object
2db83 20 75 73 65 64 20 62 79 20 63 6f 6e 6e 65 63 74   used by connect
2db84 69 6f 6e 20 64 62 20 74 6f 20 61 63 63 65 73 73  ion db to access
2db85 20 0a 2a 2a 20 74 68 69 73 20 76 69 72 74 75 61   .** this virtua
2db86 6c 2d 74 61 62 6c 65 2c 20 69 66 20 6f 6e 65 20  l-table, if one 
2db87 68 61 73 20 62 65 65 6e 20 63 72 65 61 74 65 64  has been created
2db88 2c 20 6f 72 20 4e 55 4c 4c 20 6f 74 68 65 72 77  , or NULL otherw
2db89 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ise..*/.SQLITE_P
2db8a 52 49 56 41 54 45 20 56 54 61 62 6c 65 20 2a 73  RIVATE VTable *s
2db8b 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28  qlite3GetVTable(
2db8c 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 61 62  sqlite3 *db, Tab
2db8d 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 56 54 61  le *pTab){.  VTa
2db8e 62 6c 65 20 2a 70 56 74 61 62 3b 0a 20 20 61 73  ble *pVtab;.  as
2db8f 73 65 72 74 28 20 49 73 56 69 72 74 75 61 6c 28  sert( IsVirtual(
2db90 70 54 61 62 29 20 29 3b 0a 20 20 66 6f 72 28 70  pTab) );.  for(p
2db91 56 74 61 62 3d 70 54 61 62 2d 3e 70 56 54 61 62  Vtab=pTab->pVTab
2db92 6c 65 3b 20 70 56 74 61 62 20 26 26 20 70 56 74  le; pVtab && pVt
2db93 61 62 2d 3e 64 62 21 3d 64 62 3b 20 70 56 74 61  ab->db!=db; pVta
2db94 62 3d 70 56 74 61 62 2d 3e 70 4e 65 78 74 29 3b  b=pVtab->pNext);
2db95 0a 20 20 72 65 74 75 72 6e 20 70 56 74 61 62 3b  .  return pVtab;
2db96 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d  .}../*.** Decrem
2db97 65 6e 74 20 74 68 65 20 72 65 66 2d 63 6f 75 6e  ent the ref-coun
2db98 74 20 6f 6e 20 61 20 76 69 72 74 75 61 6c 20 74  t on a virtual t
2db99 61 62 6c 65 20 6f 62 6a 65 63 74 2e 20 57 68 65  able object. Whe
2db9a 6e 20 74 68 65 20 72 65 66 2d 63 6f 75 6e 74 0a  n the ref-count.
2db9b 2a 2a 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c  ** reaches zero,
2db9c 20 63 61 6c 6c 20 74 68 65 20 78 44 69 73 63 6f   call the xDisco
2db9d 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 20 74  nnect() method t
2db9e 6f 20 64 65 6c 65 74 65 20 74 68 65 20 6f 62 6a  o delete the obj
2db9f 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ect..*/.SQLITE_P
2dba0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2dba1 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28 56 54  te3VtabUnlock(VT
2dba2 61 62 6c 65 20 2a 70 56 54 61 62 29 7b 0a 20 20  able *pVTab){.  
2dba3 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 56  sqlite3 *db = pV
2dba4 54 61 62 2d 3e 64 62 3b 0a 0a 20 20 61 73 73 65  Tab->db;..  asse
2dba5 72 74 28 20 64 62 20 29 3b 0a 20 20 61 73 73 65  rt( db );.  asse
2dba6 72 74 28 20 70 56 54 61 62 2d 3e 6e 52 65 66 3e  rt( pVTab->nRef>
2dba7 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  0 );.  assert( s
2dba8 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63  qlite3SafetyChec
2dba9 6b 4f 6b 28 64 62 29 20 29 3b 0a 0a 20 20 70 56  kOk(db) );..  pV
2dbaa 54 61 62 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69  Tab->nRef--;.  i
2dbab 66 28 20 70 56 54 61 62 2d 3e 6e 52 65 66 3d 3d  f( pVTab->nRef==
2dbac 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
2dbad 5f 76 74 61 62 20 2a 70 20 3d 20 70 56 54 61 62  _vtab *p = pVTab
2dbae 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20 69 66 28  ->pVtab;.    if(
2dbaf 20 70 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c   p ){.#ifdef SQL
2dbb0 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20  ITE_DEBUG.      
2dbb1 69 66 28 20 70 56 54 61 62 2d 3e 64 62 2d 3e 6d  if( pVTab->db->m
2dbb2 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47  agic==SQLITE_MAG
2dbb3 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 20  IC_BUSY ){.     
2dbb4 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33     (void)sqlite3
2dbb5 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20  SafetyOff(db);. 
2dbb6 20 20 20 20 20 20 20 70 2d 3e 70 4d 6f 64 75 6c         p->pModul
2dbb7 65 2d 3e 78 44 69 73 63 6f 6e 6e 65 63 74 28 70  e->xDisconnect(p
2dbb8 29 3b 0a 20 20 20 20 20 20 20 20 28 76 6f 69 64  );.        (void
2dbb9 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e  )sqlite3SafetyOn
2dbba 28 64 62 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c  (db);.      } el
2dbbb 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  se.#endif.      
2dbbc 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 4d 6f  {.        p->pMo
2dbbd 64 75 6c 65 2d 3e 78 44 69 73 63 6f 6e 6e 65 63  dule->xDisconnec
2dbbe 74 28 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  t(p);.      }.  
2dbbf 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44    }.    sqlite3D
2dbc0 62 46 72 65 65 28 64 62 2c 20 70 56 54 61 62 29  bFree(db, pVTab)
2dbc1 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
2dbc2 61 62 6c 65 20 70 20 69 73 20 61 20 76 69 72 74  able p is a virt
2dbc3 75 61 6c 20 74 61 62 6c 65 2e 20 54 68 69 73 20  ual table. This 
2dbc4 66 75 6e 63 74 69 6f 6e 20 6d 6f 76 65 73 20 61  function moves a
2dbc5 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74  ll elements in t
2dbc6 68 65 0a 2a 2a 20 70 2d 3e 70 56 54 61 62 6c 65  he.** p->pVTable
2dbc7 20 6c 69 73 74 20 74 6f 20 74 68 65 20 73 71 6c   list to the sql
2dbc8 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74  ite3.pDisconnect
2dbc9 20 6c 69 73 74 73 20 6f 66 20 74 68 65 69 72 20   lists of their 
2dbca 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 64 61  associated.** da
2dbcb 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
2dbcc 6e 73 20 74 6f 20 62 65 20 64 69 73 63 6f 6e 6e  ns to be disconn
2dbcd 65 63 74 65 64 20 61 74 20 74 68 65 20 6e 65 78  ected at the nex
2dbce 74 20 6f 70 70 6f 72 74 75 6e 69 74 79 2e 20 0a  t opportunity. .
2dbcf 2a 2a 20 45 78 63 65 70 74 2c 20 69 66 20 61 72  ** Except, if ar
2dbd0 67 75 6d 65 6e 74 20 64 62 20 69 73 20 6e 6f 74  gument db is not
2dbd1 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 20   NULL, then the 
2dbd2 65 6e 74 72 79 20 61 73 73 6f 63 69 61 74 65 64  entry associated
2dbd3 20 77 69 74 68 0a 2a 2a 20 63 6f 6e 6e 65 63 74   with.** connect
2dbd4 69 6f 6e 20 64 62 20 69 73 20 6c 65 66 74 20 69  ion db is left i
2dbd5 6e 20 74 68 65 20 70 2d 3e 70 56 54 61 62 6c 65  n the p->pVTable
2dbd6 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63   list..*/.static
2dbd7 20 56 54 61 62 6c 65 20 2a 76 74 61 62 44 69 73   VTable *vtabDis
2dbd8 63 6f 6e 6e 65 63 74 41 6c 6c 28 73 71 6c 69 74  connectAll(sqlit
2dbd9 65 33 20 2a 64 62 2c 20 54 61 62 6c 65 20 2a 70  e3 *db, Table *p
2dbda 29 7b 0a 20 20 56 54 61 62 6c 65 20 2a 70 52 65  ){.  VTable *pRe
2dbdb 74 20 3d 20 30 3b 0a 20 20 56 54 61 62 6c 65 20  t = 0;.  VTable 
2dbdc 2a 70 56 54 61 62 6c 65 20 3d 20 70 2d 3e 70 56  *pVTable = p->pV
2dbdd 54 61 62 6c 65 3b 0a 20 20 70 2d 3e 70 56 54 61  Table;.  p->pVTa
2dbde 62 6c 65 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 41  ble = 0;..  /* A
2dbdf 73 73 65 72 74 20 74 68 61 74 20 74 68 65 20 6d  ssert that the m
2dbe0 75 74 65 78 20 28 69 66 20 61 6e 79 29 20 61 73  utex (if any) as
2dbe1 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
2dbe2 65 20 42 74 53 68 61 72 65 64 20 64 61 74 61 62  e BtShared datab
2dbe3 61 73 65 20 0a 20 20 2a 2a 20 74 68 61 74 20 63  ase .  ** that c
2dbe4 6f 6e 74 61 69 6e 73 20 74 61 62 6c 65 20 70 20  ontains table p 
2dbe5 69 73 20 68 65 6c 64 20 62 79 20 74 68 65 20 63  is held by the c
2dbe6 61 6c 6c 65 72 2e 20 53 65 65 20 68 65 61 64 65  aller. See heade
2dbe7 72 20 63 6f 6d 6d 65 6e 74 73 20 0a 20 20 2a 2a  r comments .  **
2dbe8 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f 6e 20   above function 
2dbe9 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63  sqlite3VtabUnloc
2dbea 6b 4c 69 73 74 28 29 20 66 6f 72 20 61 6e 20 65  kList() for an e
2dbeb 78 70 6c 61 6e 61 74 69 6f 6e 20 6f 66 20 77 68  xplanation of wh
2dbec 79 0a 20 20 2a 2a 20 74 68 69 73 20 6d 61 6b 65  y.  ** this make
2dbed 73 20 69 74 20 73 61 66 65 20 74 6f 20 61 63 63  s it safe to acc
2dbee 65 73 73 20 74 68 65 20 73 71 6c 69 74 65 33 2e  ess the sqlite3.
2dbef 70 44 69 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74  pDisconnect list
2dbf0 20 6f 66 20 61 6e 79 0a 20 20 2a 2a 20 64 61 74   of any.  ** dat
2dbf1 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2dbf2 20 74 68 61 74 20 6d 61 79 20 68 61 76 65 20 61   that may have a
2dbf3 6e 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 70  n entry in the p
2dbf4 2d 3e 70 56 54 61 62 6c 65 20 6c 69 73 74 2e 20  ->pVTable list. 
2dbf5 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 64 62   */.  assert( db
2dbf6 3d 3d 30 20 7c 7c 0a 20 20 20 20 73 71 6c 69 74  ==0 ||.    sqlit
2dbf7 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
2dbf8 78 28 64 62 2d 3e 61 44 62 5b 73 71 6c 69 74 65  x(db->aDb[sqlite
2dbf9 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
2dbfa 62 2c 20 70 2d 3e 70 53 63 68 65 6d 61 29 5d 2e  b, p->pSchema)].
2dbfb 70 42 74 29 20 0a 20 20 29 3b 0a 0a 20 20 77 68  pBt) .  );..  wh
2dbfc 69 6c 65 28 20 70 56 54 61 62 6c 65 20 29 7b 0a  ile( pVTable ){.
2dbfd 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 32      sqlite3 *db2
2dbfe 20 3d 20 70 56 54 61 62 6c 65 2d 3e 64 62 3b 0a   = pVTable->db;.
2dbff 20 20 20 20 56 54 61 62 6c 65 20 2a 70 4e 65 78      VTable *pNex
2dc00 74 20 3d 20 70 56 54 61 62 6c 65 2d 3e 70 4e 65  t = pVTable->pNe
2dc01 78 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  xt;.    assert( 
2dc02 64 62 32 20 29 3b 0a 20 20 20 20 69 66 28 20 64  db2 );.    if( d
2dc03 62 32 3d 3d 64 62 20 29 7b 0a 20 20 20 20 20 20  b2==db ){.      
2dc04 70 52 65 74 20 3d 20 70 56 54 61 62 6c 65 3b 0a  pRet = pVTable;.
2dc05 20 20 20 20 20 20 70 2d 3e 70 56 54 61 62 6c 65        p->pVTable
2dc06 20 3d 20 70 52 65 74 3b 0a 20 20 20 20 20 20 70   = pRet;.      p
2dc07 52 65 74 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a  Ret->pNext = 0;.
2dc08 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2dc09 20 70 56 54 61 62 6c 65 2d 3e 70 4e 65 78 74 20   pVTable->pNext 
2dc0a 3d 20 64 62 32 2d 3e 70 44 69 73 63 6f 6e 6e 65  = db2->pDisconne
2dc0b 63 74 3b 0a 20 20 20 20 20 20 64 62 32 2d 3e 70  ct;.      db2->p
2dc0c 44 69 73 63 6f 6e 6e 65 63 74 20 3d 20 70 56 54  Disconnect = pVT
2dc0d 61 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20  able;.    }.    
2dc0e 70 56 54 61 62 6c 65 20 3d 20 70 4e 65 78 74 3b  pVTable = pNext;
2dc0f 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
2dc10 21 64 62 20 7c 7c 20 70 52 65 74 20 29 3b 0a 20  !db || pRet );. 
2dc11 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a   return pRet;.}.
2dc12 0a 0a 2f 2a 0a 2a 2a 20 44 69 73 63 6f 6e 6e 65  ../*.** Disconne
2dc13 63 74 20 61 6c 6c 20 74 68 65 20 76 69 72 74 75  ct all the virtu
2dc14 61 6c 20 74 61 62 6c 65 20 6f 62 6a 65 63 74 73  al table objects
2dc15 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e   in the sqlite3.
2dc16 70 44 69 73 63 6f 6e 6e 65 63 74 20 6c 69 73 74  pDisconnect list
2dc17 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
2dc18 63 74 69 6f 6e 20 6d 61 79 20 6f 6e 6c 79 20 62  ction may only b
2dc19 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68  e called when th
2dc1a 65 20 6d 75 74 65 78 65 73 20 61 73 73 6f 63 69  e mutexes associ
2dc1b 61 74 65 64 20 77 69 74 68 20 61 6c 6c 0a 2a 2a  ated with all.**
2dc1c 20 73 68 61 72 65 64 20 62 2d 74 72 65 65 20 64   shared b-tree d
2dc1d 61 74 61 62 61 73 65 73 20 6f 70 65 6e 65 64 20  atabases opened 
2dc1e 75 73 69 6e 67 20 63 6f 6e 6e 65 63 74 69 6f 6e  using connection
2dc1f 20 64 62 20 61 72 65 20 68 65 6c 64 20 62 79 20   db are held by 
2dc20 74 68 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 2e 20  the .** caller. 
2dc21 54 68 69 73 20 69 73 20 64 6f 6e 65 20 74 6f 20  This is done to 
2dc22 70 72 6f 74 65 63 74 20 74 68 65 20 73 71 6c 69  protect the sqli
2dc23 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20  te3.pDisconnect 
2dc24 6c 69 73 74 2e 20 54 68 65 0a 2a 2a 20 73 71 6c  list. The.** sql
2dc25 69 74 65 33 2e 70 44 69 73 63 6f 6e 6e 65 63 74  ite3.pDisconnect
2dc26 20 6c 69 73 74 20 69 73 20 61 63 63 65 73 73 65   list is accesse
2dc27 64 20 6f 6e 6c 79 20 61 73 20 66 6f 6c 6c 6f 77  d only as follow
2dc28 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 42 79  s:.**.**   1) By
2dc29 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 20   this function. 
2dc2a 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 61 6c  In this case, al
2dc2b 6c 20 42 74 53 68 61 72 65 64 20 6d 75 74 65 78  l BtShared mutex
2dc2c 65 73 20 61 6e 64 20 74 68 65 20 6d 75 74 65 78  es and the mutex
2dc2d 0a 2a 2a 20 20 20 20 20 20 61 73 73 6f 63 69 61  .**      associa
2dc2e 74 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74  ted with the dat
2dc2f 61 62 61 73 65 20 68 61 6e 64 6c 65 20 69 74 73  abase handle its
2dc30 65 6c 66 20 6d 75 73 74 20 62 65 20 68 65 6c 64  elf must be held
2dc31 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20 42 79 20  ..**.**   2) By 
2dc32 66 75 6e 63 74 69 6f 6e 20 76 74 61 62 44 69 73  function vtabDis
2dc33 63 6f 6e 6e 65 63 74 41 6c 6c 28 29 2c 20 77 68  connectAll(), wh
2dc34 65 6e 20 69 74 20 61 64 64 73 20 61 20 56 54 61  en it adds a VTa
2dc35 62 6c 65 20 65 6e 74 72 79 20 74 6f 0a 2a 2a 20  ble entry to.** 
2dc36 20 20 20 20 20 74 68 65 20 73 71 6c 69 74 65 33       the sqlite3
2dc37 2e 70 44 69 73 63 6f 6e 6e 65 63 74 20 6c 69 73  .pDisconnect lis
2dc38 74 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20  t. In this case 
2dc39 65 69 74 68 65 72 20 74 68 65 20 42 74 53 68 61  either the BtSha
2dc3a 72 65 64 20 6d 75 74 65 78 0a 2a 2a 20 20 20 20  red mutex.**    
2dc3b 20 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74    associated wit
2dc3c 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  h the database t
2dc3d 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
2dc3e 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 69 73   is stored in is
2dc3f 20 68 65 6c 64 0a 2a 2a 20 20 20 20 20 20 6f 72   held.**      or
2dc40 2c 20 69 66 20 74 68 65 20 76 69 72 74 75 61 6c  , if the virtual
2dc41 20 74 61 62 6c 65 20 69 73 20 73 74 6f 72 65 64   table is stored
2dc42 20 69 6e 20 61 20 6e 6f 6e 2d 73 68 61 72 61 62   in a non-sharab
2dc43 6c 65 20 64 61 74 61 62 61 73 65 2c 20 74 68 65  le database, the
2dc44 6e 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 64 61  n.**      the da
2dc45 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 6d 75  tabase handle mu
2dc46 74 65 78 20 69 73 20 68 65 6c 64 2e 0a 2a 2a 0a  tex is held..**.
2dc47 2a 2a 20 41 73 20 61 20 72 65 73 75 6c 74 2c 20  ** As a result, 
2dc48 61 20 73 71 6c 69 74 65 33 2e 70 44 69 73 63 6f  a sqlite3.pDisco
2dc49 6e 6e 65 63 74 20 63 61 6e 6e 6f 74 20 62 65 20  nnect cannot be 
2dc4a 61 63 63 65 73 73 65 64 20 73 69 6d 75 6c 74 61  accessed simulta
2dc4b 6e 65 6f 75 73 6c 79 20 0a 2a 2a 20 62 79 20 6d  neously .** by m
2dc4c 75 6c 74 69 70 6c 65 20 74 68 72 65 61 64 73 2e  ultiple threads.
2dc4d 20 49 74 20 69 73 20 74 68 72 65 61 64 2d 73 61   It is thread-sa
2dc4e 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  fe..*/.SQLITE_PR
2dc4f 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
2dc50 65 33 56 74 61 62 55 6e 6c 6f 63 6b 4c 69 73 74  e3VtabUnlockList
2dc51 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
2dc52 20 56 54 61 62 6c 65 20 2a 70 20 3d 20 64 62 2d   VTable *p = db-
2dc53 3e 70 44 69 73 63 6f 6e 6e 65 63 74 3b 0a 20 20  >pDisconnect;.  
2dc54 64 62 2d 3e 70 44 69 73 63 6f 6e 6e 65 63 74 20  db->pDisconnect 
2dc55 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  = 0;..  assert( 
2dc56 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
2dc57 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20  sAllMutexes(db) 
2dc58 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
2dc59 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
2dc5a 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  db->mutex) );.. 
2dc5b 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71   if( p ){.    sq
2dc5c 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61  lite3ExpirePrepa
2dc5d 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62  redStatements(db
2dc5e 29 3b 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20  );.    do {.    
2dc5f 20 20 56 54 61 62 6c 65 20 2a 70 4e 65 78 74 20    VTable *pNext 
2dc60 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  = p->pNext;.    
2dc61 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e 6c    sqlite3VtabUnl
2dc62 6f 63 6b 28 70 29 3b 0a 20 20 20 20 20 20 70 20  ock(p);.      p 
2dc63 3d 20 70 4e 65 78 74 3b 0a 20 20 20 20 7d 77 68  = pNext;.    }wh
2dc64 69 6c 65 28 20 70 20 29 3b 0a 20 20 7d 0a 7d 0a  ile( p );.  }.}.
2dc65 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 79  ./*.** Clear any
2dc66 20 61 6e 64 20 61 6c 6c 20 76 69 72 74 75 61 6c   and all virtual
2dc67 2d 74 61 62 6c 65 20 69 6e 66 6f 72 6d 61 74 69  -table informati
2dc68 6f 6e 20 66 72 6f 6d 20 74 68 65 20 54 61 62 6c  on from the Tabl
2dc69 65 20 72 65 63 6f 72 64 2e 0a 2a 2a 20 54 68 69  e record..** Thi
2dc6a 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
2dc6b 6c 65 64 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65  led, for example
2dc6c 2c 20 6a 75 73 74 20 62 65 66 6f 72 65 20 64 65  , just before de
2dc6d 6c 65 74 69 6e 67 20 74 68 65 20 54 61 62 6c 65  leting the Table
2dc6e 0a 2a 2a 20 72 65 63 6f 72 64 2e 0a 2a 2a 0a 2a  .** record..**.*
2dc6f 2a 20 53 69 6e 63 65 20 69 74 20 69 73 20 61 20  * Since it is a 
2dc70 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 2c 20 74  virtual-table, t
2dc71 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
2dc72 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f  re contains a po
2dc73 69 6e 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20  inter.** to the 
2dc74 68 65 61 64 20 6f 66 20 61 20 6c 69 6e 6b 65 64  head of a linked
2dc75 20 6c 69 73 74 20 6f 66 20 56 54 61 62 6c 65 20   list of VTable 
2dc76 73 74 72 75 63 74 75 72 65 73 2e 20 45 61 63 68  structures. Each
2dc77 20 56 54 61 62 6c 65 20 0a 2a 2a 20 73 74 72 75   VTable .** stru
2dc78 63 74 75 72 65 20 69 73 20 61 73 73 6f 63 69 61  cture is associa
2dc79 74 65 64 20 77 69 74 68 20 61 20 73 69 6e 67 6c  ted with a singl
2dc7a 65 20 73 71 6c 69 74 65 33 2a 20 75 73 65 72 20  e sqlite3* user 
2dc7b 6f 66 20 74 68 65 20 73 63 68 65 6d 61 2e 0a 2a  of the schema..*
2dc7c 2a 20 54 68 65 20 72 65 66 65 72 65 6e 63 65 20  * The reference 
2dc7d 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 56 54 61  count of the VTa
2dc7e 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 61 73  ble structure as
2dc7f 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 64 61  sociated with da
2dc80 74 61 62 61 73 65 20 0a 2a 2a 20 63 6f 6e 6e 65  tabase .** conne
2dc81 63 74 69 6f 6e 20 64 62 20 69 73 20 64 65 63 72  ction db is decr
2dc82 65 6d 65 6e 74 65 64 20 69 6d 6d 65 64 69 61 74  emented immediat
2dc83 65 6c 79 20 28 77 68 69 63 68 20 6d 61 79 20 6c  ely (which may l
2dc84 65 61 64 20 74 6f 20 74 68 65 20 0a 2a 2a 20 73  ead to the .** s
2dc85 74 72 75 63 74 75 72 65 20 62 65 69 6e 67 20 78  tructure being x
2dc86 44 69 73 63 6f 6e 6e 65 63 74 65 64 20 61 6e 64  Disconnected and
2dc87 20 66 72 65 65 29 2e 20 41 6e 79 20 6f 74 68 65   free). Any othe
2dc88 72 20 56 54 61 62 6c 65 20 73 74 72 75 63 74 75  r VTable structu
2dc89 72 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20 6c 69  res.** in the li
2dc8a 73 74 20 61 72 65 20 6d 6f 76 65 64 20 74 6f 20  st are moved to 
2dc8b 74 68 65 20 73 71 6c 69 74 65 33 2e 70 44 69 73  the sqlite3.pDis
2dc8c 63 6f 6e 6e 65 63 74 20 6c 69 73 74 20 6f 66 20  connect list of 
2dc8d 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20 0a  the associated .
2dc8e 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ** database conn
2dc8f 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ection..*/.SQLIT
2dc90 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2dc91 71 6c 69 74 65 33 56 74 61 62 43 6c 65 61 72 28  qlite3VtabClear(
2dc92 54 61 62 6c 65 20 2a 70 29 7b 0a 20 20 76 74 61  Table *p){.  vta
2dc93 62 44 69 73 63 6f 6e 6e 65 63 74 41 6c 6c 28 30  bDisconnectAll(0
2dc94 2c 20 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61  , p);.  if( p->a
2dc95 7a 4d 6f 64 75 6c 65 41 72 67 20 29 7b 0a 20 20  zModuleArg ){.  
2dc96 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72    int i;.    for
2dc97 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4d 6f 64 75  (i=0; i<p->nModu
2dc98 6c 65 41 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20  leArg; i++){.   
2dc99 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2dc9a 28 70 2d 3e 64 62 4d 65 6d 2c 20 70 2d 3e 61 7a  (p->dbMem, p->az
2dc9b 4d 6f 64 75 6c 65 41 72 67 5b 69 5d 29 3b 0a 20  ModuleArg[i]);. 
2dc9c 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
2dc9d 44 62 46 72 65 65 28 70 2d 3e 64 62 4d 65 6d 2c  DbFree(p->dbMem,
2dc9e 20 70 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 29   p->azModuleArg)
2dc9f 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ;.  }.}../*.** A
2dca0 64 64 20 61 20 6e 65 77 20 6d 6f 64 75 6c 65 20  dd a new module 
2dca1 61 72 67 75 6d 65 6e 74 20 74 6f 20 70 54 61 62  argument to pTab
2dca2 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b  le->azModuleArg[
2dca3 5d 2e 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67  ]..** The string
2dca4 20 69 73 20 6e 6f 74 20 63 6f 70 69 65 64 20 2d   is not copied -
2dca5 20 74 68 65 20 70 6f 69 6e 74 65 72 20 69 73 20   the pointer is 
2dca6 73 74 6f 72 65 64 2e 20 20 54 68 65 0a 2a 2a 20  stored.  The.** 
2dca7 73 74 72 69 6e 67 20 77 69 6c 6c 20 62 65 20 66  string will be f
2dca8 72 65 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c  reed automatical
2dca9 6c 79 20 77 68 65 6e 20 74 68 65 20 74 61 62 6c  ly when the tabl
2dcaa 65 20 69 73 0a 2a 2a 20 64 65 6c 65 74 65 64 2e  e is.** deleted.
2dcab 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2dcac 61 64 64 4d 6f 64 75 6c 65 41 72 67 75 6d 65 6e  addModuleArgumen
2dcad 74 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54  t(sqlite3 *db, T
2dcae 61 62 6c 65 20 2a 70 54 61 62 6c 65 2c 20 63 68  able *pTable, ch
2dcaf 61 72 20 2a 7a 41 72 67 29 7b 0a 20 20 69 6e 74  ar *zArg){.  int
2dcb0 20 69 20 3d 20 70 54 61 62 6c 65 2d 3e 6e 4d 6f   i = pTable->nMo
2dcb1 64 75 6c 65 41 72 67 2b 2b 3b 0a 20 20 69 6e 74  duleArg++;.  int
2dcb2 20 6e 42 79 74 65 73 20 3d 20 73 69 7a 65 6f 66   nBytes = sizeof
2dcb3 28 63 68 61 72 20 2a 29 2a 28 31 2b 70 54 61 62  (char *)*(1+pTab
2dcb4 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 29 3b  le->nModuleArg);
2dcb5 0a 20 20 63 68 61 72 20 2a 2a 61 7a 4d 6f 64 75  .  char **azModu
2dcb6 6c 65 41 72 67 3b 0a 20 20 61 7a 4d 6f 64 75 6c  leArg;.  azModul
2dcb7 65 41 72 67 20 3d 20 73 71 6c 69 74 65 33 44 62  eArg = sqlite3Db
2dcb8 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70 54 61 62  Realloc(db, pTab
2dcb9 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 2c  le->azModuleArg,
2dcba 20 6e 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20   nBytes);.  if( 
2dcbb 61 7a 4d 6f 64 75 6c 65 41 72 67 3d 3d 30 20 29  azModuleArg==0 )
2dcbc 7b 0a 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20  {.    int j;.   
2dcbd 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 69 3b 20 6a   for(j=0; j<i; j
2dcbe 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
2dcbf 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54 61  e3DbFree(db, pTa
2dcc0 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67  ble->azModuleArg
2dcc1 5b 6a 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  [j]);.    }.    
2dcc2 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2dcc3 2c 20 7a 41 72 67 29 3b 0a 20 20 20 20 73 71 6c  , zArg);.    sql
2dcc4 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2dcc5 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41  Table->azModuleA
2dcc6 72 67 29 3b 0a 20 20 20 20 70 54 61 62 6c 65 2d  rg);.    pTable-
2dcc7 3e 6e 4d 6f 64 75 6c 65 41 72 67 20 3d 20 30 3b  >nModuleArg = 0;
2dcc8 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 7a  .  }else{.    az
2dcc9 4d 6f 64 75 6c 65 41 72 67 5b 69 5d 20 3d 20 7a  ModuleArg[i] = z
2dcca 41 72 67 3b 0a 20 20 20 20 61 7a 4d 6f 64 75 6c  Arg;.    azModul
2dccb 65 41 72 67 5b 69 2b 31 5d 20 3d 20 30 3b 0a 20  eArg[i+1] = 0;. 
2dccc 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e 61 7a 4d   }.  pTable->azM
2dccd 6f 64 75 6c 65 41 72 67 20 3d 20 61 7a 4d 6f 64  oduleArg = azMod
2dcce 75 6c 65 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  uleArg;.}../*.**
2dccf 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c   The parser call
2dcd0 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  s this routine w
2dcd1 68 65 6e 20 69 74 20 66 69 72 73 74 20 73 65 65  hen it first see
2dcd2 73 20 61 20 43 52 45 41 54 45 20 56 49 52 54 55  s a CREATE VIRTU
2dcd3 41 4c 20 54 41 42 4c 45 0a 2a 2a 20 73 74 61 74  AL TABLE.** stat
2dcd4 65 6d 65 6e 74 2e 20 20 54 68 65 20 6d 6f 64 75  ement.  The modu
2dcd5 6c 65 20 6e 61 6d 65 20 68 61 73 20 62 65 65 6e  le name has been
2dcd6 20 70 61 72 73 65 64 2c 20 62 75 74 20 74 68 65   parsed, but the
2dcd7 20 6f 70 74 69 6f 6e 61 6c 20 6c 69 73 74 0a 2a   optional list.*
2dcd8 2a 20 6f 66 20 70 61 72 61 6d 65 74 65 72 73 20  * of parameters 
2dcd9 74 68 61 74 20 66 6f 6c 6c 6f 77 20 74 68 65 20  that follow the 
2dcda 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 61 72 65 20  module name are 
2dcdb 73 74 69 6c 6c 20 70 65 6e 64 69 6e 67 2e 0a 2a  still pending..*
2dcdc 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2dcdd 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61   void sqlite3Vta
2dcde 62 42 65 67 69 6e 50 61 72 73 65 28 0a 20 20 50  bBeginParse(.  P
2dcdf 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2dce0 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2dce1 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b  context */.  Tok
2dce2 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20 20 20  en *pName1,     
2dce3 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 6e 65     /* Name of ne
2dce4 77 20 74 61 62 6c 65 2c 20 6f 72 20 64 61 74 61  w table, or data
2dce5 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 54  base name */.  T
2dce6 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20  oken *pName2,   
2dce7 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
2dce8 6e 65 77 20 74 61 62 6c 65 20 6f 72 20 4e 55 4c  new table or NUL
2dce9 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4d  L */.  Token *pM
2dcea 6f 64 75 6c 65 4e 61 6d 65 20 20 20 20 2f 2a 20  oduleName    /* 
2dceb 4e 61 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 75  Name of the modu
2dcec 6c 65 20 66 6f 72 20 74 68 65 20 76 69 72 74 75  le for the virtu
2dced 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20  al table */.){. 
2dcee 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
2dcef 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61         /* The da
2dcf0 74 61 62 61 73 65 20 74 68 65 20 74 61 62 6c 65  tabase the table
2dcf1 20 69 73 20 62 65 69 6e 67 20 63 72 65 61 74 65   is being create
2dcf2 64 20 69 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20  d in */.  Table 
2dcf3 2a 70 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20  *pTable;        
2dcf4 2f 2a 20 54 68 65 20 6e 65 77 20 76 69 72 74 75  /* The new virtu
2dcf5 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 71  al table */.  sq
2dcf6 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
2dcf7 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
2dcf8 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a 20  connection */.. 
2dcf9 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62   sqlite3StartTab
2dcfa 6c 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65  le(pParse, pName
2dcfb 31 2c 20 70 4e 61 6d 65 32 2c 20 30 2c 20 30 2c  1, pName2, 0, 0,
2dcfc 20 31 2c 20 30 29 3b 0a 20 20 70 54 61 62 6c 65   1, 0);.  pTable
2dcfd 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
2dcfe 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 54 61 62  able;.  if( pTab
2dcff 6c 65 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  le==0 ) return;.
2dd00 20 20 61 73 73 65 72 74 28 20 30 3d 3d 70 54 61    assert( 0==pTa
2dd01 62 6c 65 2d 3e 70 49 6e 64 65 78 20 29 3b 0a 0a  ble->pIndex );..
2dd02 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
2dd03 62 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  b;.  iDb = sqlit
2dd04 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
2dd05 64 62 2c 20 70 54 61 62 6c 65 2d 3e 70 53 63 68  db, pTable->pSch
2dd06 65 6d 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ema);.  assert( 
2dd07 69 44 62 3e 3d 30 20 29 3b 0a 0a 20 20 70 54 61  iDb>=0 );..  pTa
2dd08 62 6c 65 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d  ble->tabFlags |=
2dd09 20 54 46 5f 56 69 72 74 75 61 6c 3b 0a 20 20 70   TF_Virtual;.  p
2dd0a 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41 72  Table->nModuleAr
2dd0b 67 20 3d 20 30 3b 0a 20 20 61 64 64 4d 6f 64 75  g = 0;.  addModu
2dd0c 6c 65 41 72 67 75 6d 65 6e 74 28 64 62 2c 20 70  leArgument(db, p
2dd0d 54 61 62 6c 65 2c 20 73 71 6c 69 74 65 33 4e 61  Table, sqlite3Na
2dd0e 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
2dd0f 70 4d 6f 64 75 6c 65 4e 61 6d 65 29 29 3b 0a 20  pModuleName));. 
2dd10 20 61 64 64 4d 6f 64 75 6c 65 41 72 67 75 6d 65   addModuleArgume
2dd11 6e 74 28 64 62 2c 20 70 54 61 62 6c 65 2c 20 73  nt(db, pTable, s
2dd12 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64  qlite3DbStrDup(d
2dd13 62 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  b, db->aDb[iDb].
2dd14 7a 4e 61 6d 65 29 29 3b 0a 20 20 61 64 64 4d 6f  zName));.  addMo
2dd15 64 75 6c 65 41 72 67 75 6d 65 6e 74 28 64 62 2c  duleArgument(db,
2dd16 20 70 54 61 62 6c 65 2c 20 73 71 6c 69 74 65 33   pTable, sqlite3
2dd17 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 54 61  DbStrDup(db, pTa
2dd18 62 6c 65 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20  ble->zName));.  
2dd19 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b  pParse->sNameTok
2dd1a 65 6e 2e 6e 20 3d 20 28 69 6e 74 29 28 26 70 4d  en.n = (int)(&pM
2dd1b 6f 64 75 6c 65 4e 61 6d 65 2d 3e 7a 5b 70 4d 6f  oduleName->z[pMo
2dd1c 64 75 6c 65 4e 61 6d 65 2d 3e 6e 5d 20 2d 20 70  duleName->n] - p
2dd1d 4e 61 6d 65 31 2d 3e 7a 29 3b 0a 0a 23 69 66 6e  Name1->z);..#ifn
2dd1e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2dd1f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20  AUTHORIZATION.  
2dd20 2f 2a 20 43 72 65 61 74 69 6e 67 20 61 20 76 69  /* Creating a vi
2dd21 72 74 75 61 6c 20 74 61 62 6c 65 20 69 6e 76 6f  rtual table invo
2dd22 6b 65 73 20 74 68 65 20 61 75 74 68 6f 72 69 7a  kes the authoriz
2dd23 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20 74  ation callback t
2dd24 77 69 63 65 2e 0a 20 20 2a 2a 20 54 68 65 20 66  wice..  ** The f
2dd25 69 72 73 74 20 69 6e 76 6f 63 61 74 69 6f 6e 2c  irst invocation,
2dd26 20 74 6f 20 6f 62 74 61 69 6e 20 70 65 72 6d 69   to obtain permi
2dd27 73 73 69 6f 6e 20 74 6f 20 49 4e 53 45 52 54 20  ssion to INSERT 
2dd28 61 20 72 6f 77 20 69 6e 74 6f 20 74 68 65 0a 20  a row into the. 
2dd29 20 2a 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65   ** sqlite_maste
2dd2a 72 20 74 61 62 6c 65 2c 20 68 61 73 20 61 6c 72  r table, has alr
2dd2b 65 61 64 79 20 62 65 65 6e 20 6d 61 64 65 20 62  eady been made b
2dd2c 79 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 61  y sqlite3StartTa
2dd2d 62 6c 65 28 29 2e 0a 20 20 2a 2a 20 54 68 65 20  ble()..  ** The 
2dd2e 73 65 63 6f 6e 64 20 63 61 6c 6c 2c 20 74 6f 20  second call, to 
2dd2f 6f 62 74 61 69 6e 20 70 65 72 6d 69 73 73 69 6f  obtain permissio
2dd30 6e 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20  n to create the 
2dd31 74 61 62 6c 65 2c 20 69 73 20 6d 61 64 65 20 6e  table, is made n
2dd32 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ow..  */.  if( p
2dd33 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65 41  Table->azModuleA
2dd34 72 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  rg ){.    sqlite
2dd35 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73  3AuthCheck(pPars
2dd36 65 2c 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45  e, SQLITE_CREATE
2dd37 5f 56 54 41 42 4c 45 2c 20 70 54 61 62 6c 65 2d  _VTABLE, pTable-
2dd38 3e 7a 4e 61 6d 65 2c 20 0a 20 20 20 20 20 20 20  >zName, .       
2dd39 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 61 7a 4d       pTable->azM
2dd3a 6f 64 75 6c 65 41 72 67 5b 30 5d 2c 20 70 50 61  oduleArg[0], pPa
2dd3b 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62  rse->db->aDb[iDb
2dd3c 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 23 65  ].zName);.  }.#e
2dd3d 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  ndif.}../*.** Th
2dd3e 69 73 20 72 6f 75 74 69 6e 65 20 74 61 6b 65 73  is routine takes
2dd3f 20 74 68 65 20 6d 6f 64 75 6c 65 20 61 72 67 75   the module argu
2dd40 6d 65 6e 74 20 74 68 61 74 20 68 61 73 20 62 65  ment that has be
2dd41 65 6e 20 61 63 63 75 6d 75 6c 61 74 69 6e 67 0a  en accumulating.
2dd42 2a 2a 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 41  ** in pParse->zA
2dd43 72 67 5b 5d 20 61 6e 64 20 61 70 70 65 6e 64 73  rg[] and appends
2dd44 20 69 74 20 74 6f 20 74 68 65 20 6c 69 73 74 20   it to the list 
2dd45 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 6f 6e 20  of arguments on 
2dd46 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 74  the.** virtual t
2dd47 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 75  able currently u
2dd48 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
2dd49 6e 20 69 6e 20 70 50 61 72 73 65 2d 3e 70 54 61  n in pParse->pTa
2dd4a 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ble..*/.static v
2dd4b 6f 69 64 20 61 64 64 41 72 67 75 6d 65 6e 74 54  oid addArgumentT
2dd4c 6f 56 74 61 62 28 50 61 72 73 65 20 2a 70 50 61  oVtab(Parse *pPa
2dd4d 72 73 65 29 7b 0a 20 20 69 66 28 20 70 50 61 72  rse){.  if( pPar
2dd4e 73 65 2d 3e 73 41 72 67 2e 7a 20 26 26 20 41 4c  se->sArg.z && AL
2dd4f 57 41 59 53 28 70 50 61 72 73 65 2d 3e 70 4e 65  WAYS(pParse->pNe
2dd50 77 54 61 62 6c 65 29 20 29 7b 0a 20 20 20 20 63  wTable) ){.    c
2dd51 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28  onst char *z = (
2dd52 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 50 61 72  const char*)pPar
2dd53 73 65 2d 3e 73 41 72 67 2e 7a 3b 0a 20 20 20 20  se->sArg.z;.    
2dd54 69 6e 74 20 6e 20 3d 20 70 50 61 72 73 65 2d 3e  int n = pParse->
2dd55 73 41 72 67 2e 6e 3b 0a 20 20 20 20 73 71 6c 69  sArg.n;.    sqli
2dd56 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2dd57 2d 3e 64 62 3b 0a 20 20 20 20 61 64 64 4d 6f 64  ->db;.    addMod
2dd58 75 6c 65 41 72 67 75 6d 65 6e 74 28 64 62 2c 20  uleArgument(db, 
2dd59 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
2dd5a 65 2c 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e  e, sqlite3DbStrN
2dd5b 44 75 70 28 64 62 2c 20 7a 2c 20 6e 29 29 3b 0a  Dup(db, z, n));.
2dd5c 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65    }.}../*.** The
2dd5d 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68   parser calls th
2dd5e 69 73 20 72 6f 75 74 69 6e 65 20 61 66 74 65 72  is routine after
2dd5f 20 74 68 65 20 43 52 45 41 54 45 20 56 49 52 54   the CREATE VIRT
2dd60 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65 6d  UAL TABLE statem
2dd61 65 6e 74 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20  ent.** has been 
2dd62 63 6f 6d 70 6c 65 74 65 6c 79 20 70 61 72 73 65  completely parse
2dd63 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
2dd64 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2dd65 33 56 74 61 62 46 69 6e 69 73 68 50 61 72 73 65  3VtabFinishParse
2dd66 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
2dd67 54 6f 6b 65 6e 20 2a 70 45 6e 64 29 7b 0a 20 20  Token *pEnd){.  
2dd68 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50  Table *pTab = pP
2dd69 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b  arse->pNewTable;
2dd6a 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 62    /* The table b
2dd6b 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64  eing constructed
2dd6c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
2dd6d 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20  b = pParse->db; 
2dd6e 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
2dd6f 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2dd70 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20 70 54 61  on */..  if( pTa
2dd71 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  b==0 ) return;. 
2dd72 20 61 64 64 41 72 67 75 6d 65 6e 74 54 6f 56 74   addArgumentToVt
2dd73 61 62 28 70 50 61 72 73 65 29 3b 0a 20 20 70 50  ab(pParse);.  pP
2dd74 61 72 73 65 2d 3e 73 41 72 67 2e 7a 20 3d 20 30  arse->sArg.z = 0
2dd75 3b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 6e 4d  ;.  if( pTab->nM
2dd76 6f 64 75 6c 65 41 72 67 3c 31 20 29 20 72 65 74  oduleArg<1 ) ret
2dd77 75 72 6e 3b 0a 20 20 0a 20 20 2f 2a 20 49 66 20  urn;.  .  /* If 
2dd78 74 68 65 20 43 52 45 41 54 45 20 56 49 52 54 55  the CREATE VIRTU
2dd79 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  AL TABLE stateme
2dd7a 6e 74 20 69 73 20 62 65 69 6e 67 20 65 6e 74 65  nt is being ente
2dd7b 72 65 64 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a  red for the.  **
2dd7c 20 66 69 72 73 74 20 74 69 6d 65 20 28 69 6e 20   first time (in 
2dd7d 6f 74 68 65 72 20 77 6f 72 64 73 20 69 66 20 74  other words if t
2dd7e 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
2dd7f 20 69 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69   is actually bei
2dd80 6e 67 0a 20 20 2a 2a 20 63 72 65 61 74 65 64 20  ng.  ** created 
2dd81 6e 6f 77 20 69 6e 73 74 65 61 64 20 6f 66 20 6a  now instead of j
2dd82 75 73 74 20 62 65 69 6e 67 20 72 65 61 64 20 6f  ust being read o
2dd83 75 74 20 6f 66 20 73 71 6c 69 74 65 5f 6d 61 73  ut of sqlite_mas
2dd84 74 65 72 29 20 74 68 65 6e 0a 20 20 2a 2a 20 64  ter) then.  ** d
2dd85 6f 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 69  o additional ini
2dd86 74 69 61 6c 69 7a 61 74 69 6f 6e 20 77 6f 72 6b  tialization work
2dd87 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 73   and store the s
2dd88 74 61 74 65 6d 65 6e 74 20 74 65 78 74 0a 20 20  tatement text.  
2dd89 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  ** in the sqlite
2dd8a 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 0a 20  _master table.. 
2dd8b 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e 69   */.  if( !db->i
2dd8c 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20  nit.busy ){.    
2dd8d 63 68 61 72 20 2a 7a 53 74 6d 74 3b 0a 20 20 20  char *zStmt;.   
2dd8e 20 63 68 61 72 20 2a 7a 57 68 65 72 65 3b 0a 20   char *zWhere;. 
2dd8f 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20     int iDb;.    
2dd90 56 64 62 65 20 2a 76 3b 0a 0a 20 20 20 20 2f 2a  Vdbe *v;..    /*
2dd91 20 43 6f 6d 70 75 74 65 20 74 68 65 20 63 6f 6d   Compute the com
2dd92 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 74 68  plete text of th
2dd93 65 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c  e CREATE VIRTUAL
2dd94 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
2dd95 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 45 6e 64   */.    if( pEnd
2dd96 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65   ){.      pParse
2dd97 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 6e 20 3d  ->sNameToken.n =
2dd98 20 28 69 6e 74 29 28 70 45 6e 64 2d 3e 7a 20 2d   (int)(pEnd->z -
2dd99 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f   pParse->sNameTo
2dd9a 6b 65 6e 2e 7a 29 20 2b 20 70 45 6e 64 2d 3e 6e  ken.z) + pEnd->n
2dd9b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 53 74 6d  ;.    }.    zStm
2dd9c 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  t = sqlite3MPrin
2dd9d 74 66 28 64 62 2c 20 22 43 52 45 41 54 45 20 56  tf(db, "CREATE V
2dd9e 49 52 54 55 41 4c 20 54 41 42 4c 45 20 25 54 22  IRTUAL TABLE %T"
2dd9f 2c 20 26 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65  , &pParse->sName
2dda0 54 6f 6b 65 6e 29 3b 0a 0a 20 20 20 20 2f 2a 20  Token);..    /* 
2dda1 41 20 73 6c 6f 74 20 66 6f 72 20 74 68 65 20 72  A slot for the r
2dda2 65 63 6f 72 64 20 68 61 73 20 61 6c 72 65 61 64  ecord has alread
2dda3 79 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64  y been allocated
2dda4 20 69 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20   in the .    ** 
2dda5 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61  SQLITE_MASTER ta
2dda6 62 6c 65 2e 20 20 57 65 20 6a 75 73 74 20 6e 65  ble.  We just ne
2dda7 65 64 20 74 6f 20 75 70 64 61 74 65 20 74 68 61  ed to update tha
2dda8 74 20 73 6c 6f 74 20 77 69 74 68 20 61 6c 6c 0a  t slot with all.
2dda9 20 20 20 20 2a 2a 20 74 68 65 20 69 6e 66 6f 72      ** the infor
2ddaa 6d 61 74 69 6f 6e 20 77 65 27 76 65 20 63 6f 6c  mation we've col
2ddab 6c 65 63 74 65 64 2e 20 20 0a 20 20 20 20 2a 2a  lected.  .    **
2ddac 0a 20 20 20 20 2a 2a 20 54 68 65 20 56 4d 20 72  .    ** The VM r
2ddad 65 67 69 73 74 65 72 20 6e 75 6d 62 65 72 20 70  egister number p
2ddae 50 61 72 73 65 2d 3e 72 65 67 52 6f 77 69 64 20  Parse->regRowid 
2ddaf 68 6f 6c 64 73 20 74 68 65 20 72 6f 77 69 64 20  holds the rowid 
2ddb0 6f 66 20 61 6e 0a 20 20 20 20 2a 2a 20 65 6e 74  of an.    ** ent
2ddb1 72 79 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  ry in the sqlite
2ddb2 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 74 68  _master table th
2ddb3 74 20 77 61 73 20 63 72 65 61 74 65 64 20 66 6f  t was created fo
2ddb4 72 20 74 68 69 73 20 76 74 61 62 0a 20 20 20 20  r this vtab.    
2ddb5 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 53 74 61  ** by sqlite3Sta
2ddb6 72 74 54 61 62 6c 65 28 29 2e 0a 20 20 20 20 2a  rtTable()..    *
2ddb7 2f 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69  /.    iDb = sqli
2ddb8 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
2ddb9 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  (db, pTab->pSche
2ddba 6d 61 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ma);.    sqlite3
2ddbb 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72  NestedParse(pPar
2ddbc 73 65 2c 0a 20 20 20 20 20 20 22 55 50 44 41 54  se,.      "UPDAT
2ddbd 45 20 25 51 2e 25 73 20 22 0a 20 20 20 20 20 20  E %Q.%s ".      
2ddbe 20 20 20 22 53 45 54 20 74 79 70 65 3d 27 74 61     "SET type='ta
2ddbf 62 6c 65 27 2c 20 6e 61 6d 65 3d 25 51 2c 20 74  ble', name=%Q, t
2ddc0 62 6c 5f 6e 61 6d 65 3d 25 51 2c 20 72 6f 6f 74  bl_name=%Q, root
2ddc1 70 61 67 65 3d 30 2c 20 73 71 6c 3d 25 51 20 22  page=0, sql=%Q "
2ddc2 0a 20 20 20 20 20 20 20 22 57 48 45 52 45 20 72  .       "WHERE r
2ddc3 6f 77 69 64 3d 23 25 64 22 2c 0a 20 20 20 20 20  owid=#%d",.     
2ddc4 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
2ddc5 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c  ame, SCHEMA_TABL
2ddc6 45 28 69 44 62 29 2c 0a 20 20 20 20 20 20 70 54  E(iDb),.      pT
2ddc7 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20  ab->zName,.     
2ddc8 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 0a 20 20   pTab->zName,.  
2ddc9 20 20 20 20 7a 53 74 6d 74 2c 0a 20 20 20 20 20      zStmt,.     
2ddca 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 77 69   pParse->regRowi
2ddcb 64 0a 20 20 20 20 29 3b 0a 20 20 20 20 73 71 6c  d.    );.    sql
2ddcc 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
2ddcd 53 74 6d 74 29 3b 0a 20 20 20 20 76 20 3d 20 73  Stmt);.    v = s
2ddce 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
2ddcf 61 72 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  arse);.    sqlit
2ddd0 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70  e3ChangeCookie(p
2ddd1 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 0a 20 20  Parse, iDb);..  
2ddd2 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2ddd3 4f 70 32 28 76 2c 20 4f 50 5f 45 78 70 69 72 65  Op2(v, OP_Expire
2ddd4 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 7a 57 68  , 0, 0);.    zWh
2ddd5 65 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  ere = sqlite3MPr
2ddd6 69 6e 74 66 28 64 62 2c 20 22 6e 61 6d 65 3d 27  intf(db, "name='
2ddd7 25 71 27 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  %q'", pTab->zNam
2ddd8 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  e);.    sqlite3V
2ddd9 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2ddda 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 62  ParseSchema, iDb
2dddb 2c 20 31 2c 20 30 2c 20 7a 57 68 65 72 65 2c 20  , 1, 0, zWhere, 
2dddc 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20  P4_DYNAMIC);.   
2dddd 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2ddde 70 34 28 76 2c 20 4f 50 5f 56 43 72 65 61 74 65  p4(v, OP_VCreate
2dddf 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 0a 20 20  , iDb, 0, 0, .  
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 70 54 61 62 2d 3e 7a 4e 61         pTab->zNa
2dde2 6d 65 2c 20 73 71 6c 69 74 65 33 53 74 72 6c 65  me, sqlite3Strle
2dde3 6e 33 30 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 29  n30(pTab->zName)
2dde4 20 2b 20 31 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a   + 1);.  }..  /*
2dde5 20 49 66 20 77 65 20 61 72 65 20 72 65 72 65 61   If we are rerea
2dde6 64 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 5f  ding the sqlite_
2dde7 6d 61 73 74 65 72 20 74 61 62 6c 65 20 63 72 65  master table cre
2dde8 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72  ate the in-memor
2dde9 79 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20 6f 66  y.  ** record of
2ddea 20 74 68 65 20 74 61 62 6c 65 2e 20 54 68 65 20   the table. The 
2ddeb 78 43 6f 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f  xConnect() metho
2ddec 64 20 69 73 20 6e 6f 74 20 63 61 6c 6c 65 64 20  d is not called 
2dded 75 6e 74 69 6c 0a 20 20 2a 2a 20 74 68 65 20 66  until.  ** the f
2ddee 69 72 73 74 20 74 69 6d 65 20 74 68 65 20 76 69  irst time the vi
2ddef 72 74 75 61 6c 20 74 61 62 6c 65 20 69 73 20 75  rtual table is u
2ddf0 73 65 64 20 69 6e 20 61 6e 20 53 51 4c 20 73 74  sed in an SQL st
2ddf1 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 0a 20 20  atement. This.  
2ddf2 2a 2a 20 61 6c 6c 6f 77 73 20 61 20 73 63 68 65  ** allows a sche
2ddf3 6d 61 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ma that contains
2ddf4 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   virtual tables 
2ddf5 74 6f 20 62 65 20 6c 6f 61 64 65 64 20 62 65 66  to be loaded bef
2ddf6 6f 72 65 0a 20 20 2a 2a 20 74 68 65 20 72 65 71  ore.  ** the req
2ddf7 75 69 72 65 64 20 76 69 72 74 75 61 6c 20 74 61  uired virtual ta
2ddf8 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ble implementati
2ddf9 6f 6e 73 20 61 72 65 20 72 65 67 69 73 74 65 72  ons are register
2ddfa 65 64 2e 20 20 2a 2f 0a 20 20 65 6c 73 65 20 7b  ed.  */.  else {
2ddfb 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 4f 6c 64  .    Table *pOld
2ddfc 3b 0a 20 20 20 20 53 63 68 65 6d 61 20 2a 70 53  ;.    Schema *pS
2ddfd 63 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53  chema = pTab->pS
2ddfe 63 68 65 6d 61 3b 0a 20 20 20 20 63 6f 6e 73 74  chema;.    const
2ddff 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70   char *zName = p
2de00 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20  Tab->zName;.    
2de01 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69  int nName = sqli
2de02 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61 6d  te3Strlen30(zNam
2de03 65 29 3b 0a 20 20 20 20 70 4f 6c 64 20 3d 20 73  e);.    pOld = s
2de04 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74  qlite3HashInsert
2de05 28 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61  (&pSchema->tblHa
2de06 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  sh, zName, nName
2de07 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 69 66 28  , pTab);.    if(
2de08 20 70 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20 64   pOld ){.      d
2de09 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2de0a 3d 20 31 3b 0a 20 20 20 20 20 20 61 73 73 65 72  = 1;.      asser
2de0b 74 28 20 70 54 61 62 3d 3d 70 4f 6c 64 20 29 3b  t( pTab==pOld );
2de0c 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75 73 74    /* Malloc must
2de0d 20 68 61 76 65 20 66 61 69 6c 65 64 20 69 6e 73   have failed ins
2de0e 69 64 65 20 48 61 73 68 49 6e 73 65 72 74 28 29  ide HashInsert()
2de0f 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e   */.      return
2de10 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53 63 68  ;.    }.    pSch
2de11 65 6d 61 2d 3e 64 62 20 3d 20 70 50 61 72 73 65  ema->db = pParse
2de12 2d 3e 64 62 3b 0a 20 20 20 20 70 50 61 72 73 65  ->db;.    pParse
2de13 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20 30 3b  ->pNewTable = 0;
2de14 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
2de15 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74  e parser calls t
2de16 68 69 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e  his routine when
2de17 20 69 74 20 73 65 65 73 20 74 68 65 20 66 69 72   it sees the fir
2de18 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20 6f 66 20 61  st token.** of a
2de19 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  n argument to th
2de1a 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 69 6e  e module name in
2de1b 20 61 20 43 52 45 41 54 45 20 56 49 52 54 55 41   a CREATE VIRTUA
2de1c 4c 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e  L TABLE statemen
2de1d 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
2de1e 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
2de1f 33 56 74 61 62 41 72 67 49 6e 69 74 28 50 61 72  3VtabArgInit(Par
2de20 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 61  se *pParse){.  a
2de21 64 64 41 72 67 75 6d 65 6e 74 54 6f 56 74 61 62  ddArgumentToVtab
2de22 28 70 50 61 72 73 65 29 3b 0a 20 20 70 50 61 72  (pParse);.  pPar
2de23 73 65 2d 3e 73 41 72 67 2e 7a 20 3d 20 30 3b 0a  se->sArg.z = 0;.
2de24 20 20 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 6e    pParse->sArg.n
2de25 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54   = 0;.}../*.** T
2de26 68 65 20 70 61 72 73 65 72 20 63 61 6c 6c 73 20  he parser calls 
2de27 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 6f 72  this routine for
2de28 20 65 61 63 68 20 74 6f 6b 65 6e 20 61 66 74 65   each token afte
2de29 72 20 74 68 65 20 66 69 72 73 74 20 74 6f 6b 65  r the first toke
2de2a 6e 0a 2a 2a 20 69 6e 20 61 6e 20 61 72 67 75 6d  n.** in an argum
2de2b 65 6e 74 20 74 6f 20 74 68 65 20 6d 6f 64 75 6c  ent to the modul
2de2c 65 20 6e 61 6d 65 20 69 6e 20 61 20 43 52 45 41  e name in a CREA
2de2d 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45  TE VIRTUAL TABLE
2de2e 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53   statement..*/.S
2de2f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
2de30 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 41 72  id sqlite3VtabAr
2de31 67 45 78 74 65 6e 64 28 50 61 72 73 65 20 2a 70  gExtend(Parse *p
2de32 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 29  Parse, Token *p)
2de33 7b 0a 20 20 54 6f 6b 65 6e 20 2a 70 41 72 67 20  {.  Token *pArg 
2de34 3d 20 26 70 50 61 72 73 65 2d 3e 73 41 72 67 3b  = &pParse->sArg;
2de35 0a 20 20 69 66 28 20 70 41 72 67 2d 3e 7a 3d 3d  .  if( pArg->z==
2de36 30 20 29 7b 0a 20 20 20 20 70 41 72 67 2d 3e 7a  0 ){.    pArg->z
2de37 20 3d 20 70 2d 3e 7a 3b 0a 20 20 20 20 70 41 72   = p->z;.    pAr
2de38 67 2d 3e 6e 20 3d 20 70 2d 3e 6e 3b 0a 20 20 7d  g->n = p->n;.  }
2de39 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74  else{.    assert
2de3a 28 70 41 72 67 2d 3e 7a 20 3c 20 70 2d 3e 7a 29  (pArg->z < p->z)
2de3b 3b 0a 20 20 20 20 70 41 72 67 2d 3e 6e 20 3d 20  ;.    pArg->n = 
2de3c 28 69 6e 74 29 28 26 70 2d 3e 7a 5b 70 2d 3e 6e  (int)(&p->z[p->n
2de3d 5d 20 2d 20 70 41 72 67 2d 3e 7a 29 3b 0a 20 20  ] - pArg->z);.  
2de3e 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b  }.}../*.** Invok
2de3f 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  e a virtual tabl
2de40 65 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 28 65  e constructor (e
2de41 69 74 68 65 72 20 78 43 72 65 61 74 65 20 6f 72  ither xCreate or
2de42 20 78 43 6f 6e 6e 65 63 74 29 2e 20 54 68 65 0a   xConnect). The.
2de43 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68  ** pointer to th
2de44 65 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e  e function to in
2de45 76 6f 6b 65 20 69 73 20 70 61 73 73 65 64 20 61  voke is passed a
2de46 73 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72  s the fourth par
2de47 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 69  ameter.** to thi
2de48 73 20 70 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a  s procedure..*/.
2de49 73 74 61 74 69 63 20 69 6e 74 20 76 74 61 62 43  static int vtabC
2de4a 61 6c 6c 43 6f 6e 73 74 72 75 63 74 6f 72 28 0a  allConstructor(.
2de4b 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a    sqlite3 *db, .
2de4c 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 0a 20    Table *pTab,. 
2de4d 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 2c 0a 20   Module *pMod,. 
2de4e 20 69 6e 74 20 28 2a 78 43 6f 6e 73 74 72 75 63   int (*xConstruc
2de4f 74 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f 69 64  t)(sqlite3*,void
2de50 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72  *,int,const char
2de51 2a 63 6f 6e 73 74 2a 2c 73 71 6c 69 74 65 33 5f  *const*,sqlite3_
2de52 76 74 61 62 2a 2a 2c 63 68 61 72 2a 2a 29 2c 0a  vtab**,char**),.
2de53 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 0a 29    char **pzErr.)
2de54 7b 0a 20 20 56 54 61 62 6c 65 20 2a 70 56 54 61  {.  VTable *pVTa
2de55 62 6c 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ble;.  int rc;. 
2de56 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
2de57 73 74 2a 61 7a 41 72 67 20 3d 20 28 63 6f 6e 73  st*azArg = (cons
2de58 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 29 70  t char *const*)p
2de59 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67  Tab->azModuleArg
2de5a 3b 0a 20 20 69 6e 74 20 6e 41 72 67 20 3d 20 70  ;.  int nArg = p
2de5b 54 61 62 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 3b  Tab->nModuleArg;
2de5c 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20  .  char *zErr = 
2de5d 30 3b 0a 20 20 63 68 61 72 20 2a 7a 4d 6f 64 75  0;.  char *zModu
2de5e 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  leName = sqlite3
2de5f 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 22  MPrintf(db, "%s"
2de60 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
2de61 0a 20 20 69 66 28 20 21 7a 4d 6f 64 75 6c 65 4e  .  if( !zModuleN
2de62 61 6d 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ame ){.    retur
2de63 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
2de64 20 20 7d 0a 0a 20 20 70 56 54 61 62 6c 65 20 3d    }..  pVTable =
2de65 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
2de66 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
2de67 56 54 61 62 6c 65 29 29 3b 0a 20 20 69 66 28 20  VTable));.  if( 
2de68 21 70 56 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  !pVTable ){.    
2de69 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2de6a 2c 20 7a 4d 6f 64 75 6c 65 4e 61 6d 65 29 3b 0a  , zModuleName);.
2de6b 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2de6c 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70  E_NOMEM;.  }.  p
2de6d 56 54 61 62 6c 65 2d 3e 64 62 20 3d 20 64 62 3b  VTable->db = db;
2de6e 0a 20 20 70 56 54 61 62 6c 65 2d 3e 70 4d 6f 64  .  pVTable->pMod
2de6f 20 3d 20 70 4d 6f 64 3b 0a 0a 20 20 61 73 73 65   = pMod;..  asse
2de70 72 74 28 20 21 64 62 2d 3e 70 56 54 61 62 20 29  rt( !db->pVTab )
2de71 3b 0a 20 20 61 73 73 65 72 74 28 20 78 43 6f 6e  ;.  assert( xCon
2de72 73 74 72 75 63 74 20 29 3b 0a 20 20 64 62 2d 3e  struct );.  db->
2de73 70 56 54 61 62 20 3d 20 70 54 61 62 3b 0a 0a 20  pVTab = pTab;.. 
2de74 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 76   /* Invoke the v
2de75 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 6f 6e  irtual table con
2de76 73 74 72 75 63 74 6f 72 20 2a 2f 0a 20 20 28 76  structor */.  (v
2de77 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
2de78 79 4f 66 66 28 64 62 29 3b 0a 20 20 72 63 20 3d  yOff(db);.  rc =
2de79 20 78 43 6f 6e 73 74 72 75 63 74 28 64 62 2c 20   xConstruct(db, 
2de7a 70 4d 6f 64 2d 3e 70 41 75 78 2c 20 6e 41 72 67  pMod->pAux, nArg
2de7b 2c 20 61 7a 41 72 67 2c 20 26 70 56 54 61 62 6c  , azArg, &pVTabl
2de7c 65 2d 3e 70 56 74 61 62 2c 20 26 7a 45 72 72 29  e->pVtab, &zErr)
2de7d 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65  ;.  (void)sqlite
2de7e 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20  3SafetyOn(db);. 
2de7f 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
2de80 4e 4f 4d 45 4d 20 29 20 64 62 2d 3e 6d 61 6c 6c  NOMEM ) db->mall
2de81 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 0a 20  ocFailed = 1;.. 
2de82 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
2de83 72 63 20 29 7b 0a 20 20 20 20 69 66 28 20 7a 45  rc ){.    if( zE
2de84 72 72 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a  rr==0 ){.      *
2de85 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d  pzErr = sqlite3M
2de86 50 72 69 6e 74 66 28 64 62 2c 20 22 76 74 61 62  Printf(db, "vtab
2de87 6c 65 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 66  le constructor f
2de88 61 69 6c 65 64 3a 20 25 73 22 2c 20 7a 4d 6f 64  ailed: %s", zMod
2de89 75 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65  uleName);.    }e
2de8a 6c 73 65 20 7b 0a 20 20 20 20 20 20 2a 70 7a 45  lse {.      *pzE
2de8b 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  rr = sqlite3MPri
2de8c 6e 74 66 28 64 62 2c 20 22 25 73 22 2c 20 7a 45  ntf(db, "%s", zE
2de8d 72 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  rr);.      sqlit
2de8e 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72  e3DbFree(db, zEr
2de8f 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  r);.    }.    sq
2de90 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2de91 70 56 54 61 62 6c 65 29 3b 0a 20 20 7d 65 6c 73  pVTable);.  }els
2de92 65 20 69 66 28 20 41 4c 57 41 59 53 28 70 56 54  e if( ALWAYS(pVT
2de93 61 62 6c 65 2d 3e 70 56 74 61 62 29 20 29 7b 0a  able->pVtab) ){.
2de94 20 20 20 20 2f 2a 20 4a 75 73 74 69 66 69 63 61      /* Justifica
2de95 74 69 6f 6e 20 6f 66 20 41 4c 57 41 59 53 28 29  tion of ALWAYS()
2de96 3a 20 20 41 20 63 6f 72 72 65 63 74 20 76 74 61  :  A correct vta
2de97 62 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 6d 75  b constructor mu
2de98 73 74 20 61 6c 6c 6f 63 61 74 65 0a 20 20 20 20  st allocate.    
2de99 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  ** the sqlite3_v
2de9a 74 61 62 20 6f 62 6a 65 63 74 20 69 66 20 73 75  tab object if su
2de9b 63 63 65 73 73 66 75 6c 2e 20 20 2a 2f 0a 20 20  ccessful.  */.  
2de9c 20 20 70 56 54 61 62 6c 65 2d 3e 70 56 74 61 62    pVTable->pVtab
2de9d 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64  ->pModule = pMod
2de9e 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 70  ->pModule;.    p
2de9f 56 54 61 62 6c 65 2d 3e 6e 52 65 66 20 3d 20 31  VTable->nRef = 1
2dea0 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 70 56  ;.    if( db->pV
2dea1 54 61 62 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e  Tab ){.      con
2dea2 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
2dea3 20 3d 20 22 76 74 61 62 6c 65 20 63 6f 6e 73 74   = "vtable const
2dea4 72 75 63 74 6f 72 20 64 69 64 20 6e 6f 74 20 64  ructor did not d
2dea5 65 63 6c 61 72 65 20 73 63 68 65 6d 61 3a 20 25  eclare schema: %
2dea6 73 22 3b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72  s";.      *pzErr
2dea7 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
2dea8 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 70  f(db, zFormat, p
2dea9 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Tab->zName);.   
2deaa 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e     sqlite3VtabUn
2deab 6c 6f 63 6b 28 70 56 54 61 62 6c 65 29 3b 0a 20  lock(pVTable);. 
2deac 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
2dead 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 65 6c 73  _ERROR;.    }els
2deae 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f  e{.      int iCo
2deaf 6c 3b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 65  l;.      /* If e
2deb0 76 65 72 79 74 68 69 6e 67 20 77 65 6e 74 20 61  verything went a
2deb1 63 63 6f 72 64 69 6e 67 20 74 6f 20 70 6c 61 6e  ccording to plan
2deb2 2c 20 6c 69 6e 6b 20 74 68 65 20 6e 65 77 20 56  , link the new V
2deb3 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 0a  Table structure.
2deb4 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68        ** into th
2deb5 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 68 65  e linked list he
2deb6 61 64 65 64 20 62 79 20 70 54 61 62 2d 3e 70 56  aded by pTab->pV
2deb7 54 61 62 6c 65 2e 20 54 68 65 6e 20 6c 6f 6f 70  Table. Then loop
2deb8 20 74 68 72 6f 75 67 68 20 74 68 65 20 0a 20 20   through the .  
2deb9 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 73 20 6f      ** columns o
2deba 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 73  f the table to s
2debb 65 65 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65  ee if any of the
2debc 6d 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 74 6f  m contain the to
2debd 6b 65 6e 20 22 68 69 64 64 65 6e 22 2e 0a 20 20  ken "hidden"..  
2debe 20 20 20 20 2a 2a 20 49 66 20 73 6f 2c 20 73 65      ** If so, se
2debf 74 20 74 68 65 20 43 6f 6c 75 6d 6e 2e 69 73 48  t the Column.isH
2dec0 69 64 64 65 6e 20 66 6c 61 67 20 61 6e 64 20 72  idden flag and r
2dec1 65 6d 6f 76 65 20 74 68 65 20 74 6f 6b 65 6e 20  emove the token 
2dec2 66 72 6f 6d 0a 20 20 20 20 20 20 2a 2a 20 74 68  from.      ** th
2dec3 65 20 74 79 70 65 20 73 74 72 69 6e 67 2e 20 20  e type string.  
2dec4 2a 2f 0a 20 20 20 20 20 20 70 56 54 61 62 6c 65  */.      pVTable
2dec5 2d 3e 70 4e 65 78 74 20 3d 20 70 54 61 62 2d 3e  ->pNext = pTab->
2dec6 70 56 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 70  pVTable;.      p
2dec7 54 61 62 2d 3e 70 56 54 61 62 6c 65 20 3d 20 70  Tab->pVTable = p
2dec8 56 54 61 62 6c 65 3b 0a 0a 20 20 20 20 20 20 66  VTable;..      f
2dec9 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c  or(iCol=0; iCol<
2deca 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c  pTab->nCol; iCol
2decb 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61  ++){.        cha
2decc 72 20 2a 7a 54 79 70 65 20 3d 20 70 54 61 62 2d  r *zType = pTab-
2decd 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54 79 70  >aCol[iCol].zTyp
2dece 65 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  e;.        int n
2decf 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 69 6e  Type;.        in
2ded0 74 20 69 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  t i = 0;.       
2ded1 20 69 66 28 20 21 7a 54 79 70 65 20 29 20 63 6f   if( !zType ) co
2ded2 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
2ded3 6e 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 53  nType = sqlite3S
2ded4 74 72 6c 65 6e 33 30 28 7a 54 79 70 65 29 3b 0a  trlen30(zType);.
2ded5 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
2ded6 74 65 33 53 74 72 4e 49 43 6d 70 28 22 68 69 64  te3StrNICmp("hid
2ded7 64 65 6e 22 2c 20 7a 54 79 70 65 2c 20 36 29 7c  den", zType, 6)|
2ded8 7c 28 7a 54 79 70 65 5b 36 5d 20 26 26 20 7a 54  |(zType[6] && zT
2ded9 79 70 65 5b 36 5d 21 3d 27 20 27 29 20 29 7b 0a  ype[6]!=' ') ){.
2deda 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d            for(i=
2dedb 30 3b 20 69 3c 6e 54 79 70 65 3b 20 69 2b 2b 29  0; i<nType; i++)
2dedc 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  {.            if
2dedd 28 20 28 30 3d 3d 73 71 6c 69 74 65 33 53 74 72  ( (0==sqlite3Str
2dede 4e 49 43 6d 70 28 22 20 68 69 64 64 65 6e 22 2c  NICmp(" hidden",
2dedf 20 26 7a 54 79 70 65 5b 69 5d 2c 20 37 29 29 0a   &zType[i], 7)).
2dee0 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
2dee1 28 7a 54 79 70 65 5b 69 2b 37 5d 3d 3d 27 5c 30  (zType[i+7]=='\0
2dee2 27 20 7c 7c 20 7a 54 79 70 65 5b 69 2b 37 5d 3d  ' || zType[i+7]=
2dee3 3d 27 20 27 29 0a 20 20 20 20 20 20 20 20 20 20  =' ').          
2dee4 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20    ){.           
2dee5 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20     i++;.        
2dee6 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2dee7 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2dee8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
2dee9 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 3c 6e  .        if( i<n
2deea 54 79 70 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Type ){.        
2deeb 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 20    int j;.       
2deec 20 20 20 69 6e 74 20 6e 44 65 6c 20 3d 20 36 20     int nDel = 6 
2deed 2b 20 28 7a 54 79 70 65 5b 69 2b 36 5d 20 3f 20  + (zType[i+6] ? 
2deee 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 20 20  1 : 0);.        
2deef 20 20 66 6f 72 28 6a 3d 69 3b 20 28 6a 2b 6e 44    for(j=i; (j+nD
2def0 65 6c 29 3c 3d 6e 54 79 70 65 3b 20 6a 2b 2b 29  el)<=nType; j++)
2def1 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7a 54  {.            zT
2def2 79 70 65 5b 6a 5d 20 3d 20 7a 54 79 70 65 5b 6a  ype[j] = zType[j
2def3 2b 6e 44 65 6c 5d 3b 0a 20 20 20 20 20 20 20 20  +nDel];.        
2def4 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66    }.          if
2def5 28 20 7a 54 79 70 65 5b 69 5d 3d 3d 27 5c 30 27  ( zType[i]=='\0'
2def6 20 26 26 20 69 3e 30 20 29 7b 0a 20 20 20 20 20   && i>0 ){.     
2def7 20 20 20 20 20 20 20 61 73 73 65 72 74 28 7a 54         assert(zT
2def8 79 70 65 5b 69 2d 31 5d 3d 3d 27 20 27 29 3b 0a  ype[i-1]==' ');.
2def9 20 20 20 20 20 20 20 20 20 20 20 20 7a 54 79 70              zTyp
2defa 65 5b 69 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20  e[i-1] = '\0';. 
2defb 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2defc 20 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b       pTab->aCol[
2defd 69 43 6f 6c 5d 2e 69 73 48 69 64 64 65 6e 20 3d  iCol].isHidden =
2defe 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
2deff 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
2df00 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
2df01 28 64 62 2c 20 7a 4d 6f 64 75 6c 65 4e 61 6d 65  (db, zModuleName
2df02 29 3b 0a 20 20 64 62 2d 3e 70 56 54 61 62 20 3d  );.  db->pVTab =
2df03 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   0;.  return rc;
2df04 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
2df05 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b  unction is invok
2df06 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65 72  ed by the parser
2df07 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 78 43 6f   to call the xCo
2df08 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 0a 2a  nnect() method.*
2df09 2a 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  * of the virtual
2df0a 20 74 61 62 6c 65 20 70 54 61 62 2e 20 49 66 20   table pTab. If 
2df0b 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
2df0c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69   an error code i
2df0d 73 20 72 65 74 75 72 6e 65 64 20 0a 2a 2a 20 61  s returned .** a
2df0e 6e 64 20 61 6e 20 65 72 72 6f 72 20 6c 65 66 74  nd an error left
2df0f 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a 2a 0a 2a   in pParse..**.*
2df10 2a 20 54 68 69 73 20 63 61 6c 6c 20 69 73 20 61  * This call is a
2df11 20 6e 6f 2d 6f 70 20 69 66 20 74 61 62 6c 65 20   no-op if table 
2df12 70 54 61 62 20 69 73 20 6e 6f 74 20 61 20 76 69  pTab is not a vi
2df13 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a  rtual table..*/.
2df14 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2df15 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61  nt sqlite3VtabCa
2df16 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72 73 65 20  llConnect(Parse 
2df17 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a  *pParse, Table *
2df18 70 54 61 62 29 7b 0a 20 20 73 71 6c 69 74 65 33  pTab){.  sqlite3
2df19 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
2df1a 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  b;.  const char 
2df1b 2a 7a 4d 6f 64 3b 0a 20 20 4d 6f 64 75 6c 65 20  *zMod;.  Module 
2df1c 2a 70 4d 6f 64 3b 0a 20 20 69 6e 74 20 72 63 3b  *pMod;.  int rc;
2df1d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  ..  assert( pTab
2df1e 20 29 3b 0a 20 20 69 66 28 20 28 70 54 61 62 2d   );.  if( (pTab-
2df1f 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56  >tabFlags & TF_V
2df20 69 72 74 75 61 6c 29 3d 3d 30 20 7c 7c 20 73 71  irtual)==0 || sq
2df21 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 64  lite3GetVTable(d
2df22 62 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20  b, pTab) ){.    
2df23 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
2df24 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61  ;.  }..  /* Loca
2df25 74 65 20 74 68 65 20 72 65 71 75 69 72 65 64 20  te the required 
2df26 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
2df27 64 75 6c 65 20 2a 2f 0a 20 20 7a 4d 6f 64 20 3d  dule */.  zMod =
2df28 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41   pTab->azModuleA
2df29 72 67 5b 30 5d 3b 0a 20 20 70 4d 6f 64 20 3d 20  rg[0];.  pMod = 
2df2a 28 4d 6f 64 75 6c 65 2a 29 73 71 6c 69 74 65 33  (Module*)sqlite3
2df2b 48 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 4d  HashFind(&db->aM
2df2c 6f 64 75 6c 65 2c 20 7a 4d 6f 64 2c 20 73 71 6c  odule, zMod, sql
2df2d 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4d 6f  ite3Strlen30(zMo
2df2e 64 29 29 3b 0a 0a 20 20 69 66 28 20 21 70 4d 6f  d));..  if( !pMo
2df2f 64 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  d ){.    const c
2df30 68 61 72 20 2a 7a 4d 6f 64 75 6c 65 20 3d 20 70  har *zModule = p
2df31 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67  Tab->azModuleArg
2df32 5b 30 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  [0];.    sqlite3
2df33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2df34 20 22 6e 6f 20 73 75 63 68 20 6d 6f 64 75 6c 65   "no such module
2df35 3a 20 25 73 22 2c 20 7a 4d 6f 64 75 6c 65 29 3b  : %s", zModule);
2df36 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
2df37 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b  _ERROR;.  }else{
2df38 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72 20  .    char *zErr 
2df39 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 76 74  = 0;.    rc = vt
2df3a 61 62 43 61 6c 6c 43 6f 6e 73 74 72 75 63 74 6f  abCallConstructo
2df3b 72 28 64 62 2c 20 70 54 61 62 2c 20 70 4d 6f 64  r(db, pTab, pMod
2df3c 2c 20 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d  , pMod->pModule-
2df3d 3e 78 43 6f 6e 6e 65 63 74 2c 20 26 7a 45 72 72  >xConnect, &zErr
2df3e 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
2df3f 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
2df40 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2df41 67 28 70 50 61 72 73 65 2c 20 22 25 73 22 2c 20  g(pParse, "%s", 
2df42 7a 45 72 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20  zErr);.    }.   
2df43 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2df44 62 2c 20 7a 45 72 72 29 3b 0a 20 20 7d 0a 0a 20  b, zErr);.  }.. 
2df45 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
2df46 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 76 69 72  *.** Add the vir
2df47 74 75 61 6c 20 74 61 62 6c 65 20 70 56 54 61 62  tual table pVTab
2df48 20 74 6f 20 74 68 65 20 61 72 72 61 79 20 73 71   to the array sq
2df49 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 5b 5d 2e  lite3.aVTrans[].
2df4a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .*/.static int a
2df4b 64 64 54 6f 56 54 72 61 6e 73 28 73 71 6c 69 74  ddToVTrans(sqlit
2df4c 65 33 20 2a 64 62 2c 20 56 54 61 62 6c 65 20 2a  e3 *db, VTable *
2df4d 70 56 54 61 62 29 7b 0a 20 20 63 6f 6e 73 74 20  pVTab){.  const 
2df4e 69 6e 74 20 41 52 52 41 59 5f 49 4e 43 52 20 3d  int ARRAY_INCR =
2df4f 20 35 3b 0a 0a 20 20 2f 2a 20 47 72 6f 77 20 74   5;..  /* Grow t
2df50 68 65 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61  he sqlite3.aVTra
2df51 6e 73 20 61 72 72 61 79 20 69 66 20 72 65 71 75  ns array if requ
2df52 69 72 65 64 20 2a 2f 0a 20 20 69 66 28 20 28 64  ired */.  if( (d
2df53 62 2d 3e 6e 56 54 72 61 6e 73 25 41 52 52 41 59  b->nVTrans%ARRAY
2df54 5f 49 4e 43 52 29 3d 3d 30 20 29 7b 0a 20 20 20  _INCR)==0 ){.   
2df55 20 56 54 61 62 6c 65 20 2a 2a 61 56 54 72 61 6e   VTable **aVTran
2df56 73 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65  s;.    int nByte
2df57 73 20 3d 20 73 69 7a 65 6f 66 28 73 71 6c 69 74  s = sizeof(sqlit
2df58 65 33 5f 76 74 61 62 20 2a 29 20 2a 20 28 64 62  e3_vtab *) * (db
2df59 2d 3e 6e 56 54 72 61 6e 73 20 2b 20 41 52 52 41  ->nVTrans + ARRA
2df5a 59 5f 49 4e 43 52 29 3b 0a 20 20 20 20 61 56 54  Y_INCR);.    aVT
2df5b 72 61 6e 73 20 3d 20 73 71 6c 69 74 65 33 44 62  rans = sqlite3Db
2df5c 52 65 61 6c 6c 6f 63 28 64 62 2c 20 28 76 6f 69  Realloc(db, (voi
2df5d 64 20 2a 29 64 62 2d 3e 61 56 54 72 61 6e 73 2c  d *)db->aVTrans,
2df5e 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 69 66   nBytes);.    if
2df5f 28 20 21 61 56 54 72 61 6e 73 20 29 7b 0a 20 20  ( !aVTrans ){.  
2df60 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2df61 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20  E_NOMEM;.    }. 
2df62 20 20 20 6d 65 6d 73 65 74 28 26 61 56 54 72 61     memset(&aVTra
2df63 6e 73 5b 64 62 2d 3e 6e 56 54 72 61 6e 73 5d 2c  ns[db->nVTrans],
2df64 20 30 2c 20 73 69 7a 65 6f 66 28 73 71 6c 69 74   0, sizeof(sqlit
2df65 65 33 5f 76 74 61 62 20 2a 29 2a 41 52 52 41 59  e3_vtab *)*ARRAY
2df66 5f 49 4e 43 52 29 3b 0a 20 20 20 20 64 62 2d 3e  _INCR);.    db->
2df67 61 56 54 72 61 6e 73 20 3d 20 61 56 54 72 61 6e  aVTrans = aVTran
2df68 73 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64  s;.  }..  /* Add
2df69 20 70 56 74 61 62 20 74 6f 20 74 68 65 20 65 6e   pVtab to the en
2df6a 64 20 6f 66 20 73 71 6c 69 74 65 33 2e 61 56 54  d of sqlite3.aVT
2df6b 72 61 6e 73 20 2a 2f 0a 20 20 64 62 2d 3e 61 56  rans */.  db->aV
2df6c 54 72 61 6e 73 5b 64 62 2d 3e 6e 56 54 72 61 6e  Trans[db->nVTran
2df6d 73 2b 2b 5d 20 3d 20 70 56 54 61 62 3b 0a 20 20  s++] = pVTab;.  
2df6e 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28  sqlite3VtabLock(
2df6f 70 56 54 61 62 29 3b 0a 20 20 72 65 74 75 72 6e  pVTab);.  return
2df70 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
2df71 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
2df72 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 79  on is invoked by
2df73 20 74 68 65 20 76 64 62 65 20 74 6f 20 63 61 6c   the vdbe to cal
2df74 6c 20 74 68 65 20 78 43 72 65 61 74 65 20 6d 65  l the xCreate me
2df75 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20 76  thod.** of the v
2df76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6e 61 6d  irtual table nam
2df77 65 64 20 7a 54 61 62 20 69 6e 20 64 61 74 61 62  ed zTab in datab
2df78 61 73 65 20 69 44 62 2e 20 0a 2a 2a 0a 2a 2a 20  ase iDb. .**.** 
2df79 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
2df7a 72 73 2c 20 2a 70 7a 45 72 72 20 69 73 20 73 65  rs, *pzErr is se
2df7b 74 20 74 6f 20 70 6f 69 6e 74 20 61 6e 20 61 6e  t to point an an
2df7c 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67   English languag
2df7d 65 0a 2a 2a 20 64 65 73 63 72 69 70 74 69 6f 6e  e.** description
2df7e 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 61 6e   of the error an
2df7f 64 20 61 6e 20 53 51 4c 49 54 45 5f 58 58 58 20  d an SQLITE_XXX 
2df80 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65  error code is re
2df81 74 75 72 6e 65 64 2e 0a 2a 2a 20 49 6e 20 74 68  turned..** In th
2df82 69 73 20 63 61 73 65 20 74 68 65 20 63 61 6c 6c  is case the call
2df83 65 72 20 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c  er must call sql
2df84 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 29  ite3DbFree(db, )
2df85 20 6f 6e 20 2a 70 7a 45 72 72 2e 0a 2a 2f 0a 53   on *pzErr..*/.S
2df86 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2df87 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61 6c  t sqlite3VtabCal
2df88 6c 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 20  lCreate(sqlite3 
2df89 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f  *db, int iDb, co
2df8a 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 2c 20  nst char *zTab, 
2df8b 63 68 61 72 20 2a 2a 70 7a 45 72 72 29 7b 0a 20  char **pzErr){. 
2df8c 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
2df8d 5f 4f 4b 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54  _OK;.  Table *pT
2df8e 61 62 3b 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d  ab;.  Module *pM
2df8f 6f 64 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  od;.  const char
2df90 20 2a 7a 4d 6f 64 3b 0a 0a 20 20 70 54 61 62 20   *zMod;..  pTab 
2df91 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  = sqlite3FindTab
2df92 6c 65 28 64 62 2c 20 7a 54 61 62 2c 20 64 62 2d  le(db, zTab, db-
2df93 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29  >aDb[iDb].zName)
2df94 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62  ;.  assert( pTab
2df95 20 26 26 20 28 70 54 61 62 2d 3e 74 61 62 46 6c   && (pTab->tabFl
2df96 61 67 73 20 26 20 54 46 5f 56 69 72 74 75 61 6c  ags & TF_Virtual
2df97 29 21 3d 30 20 26 26 20 21 70 54 61 62 2d 3e 70  )!=0 && !pTab->p
2df98 56 54 61 62 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20  VTable );..  /* 
2df99 4c 6f 63 61 74 65 20 74 68 65 20 72 65 71 75 69  Locate the requi
2df9a 72 65 64 20 76 69 72 74 75 61 6c 20 74 61 62 6c  red virtual tabl
2df9b 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 7a 4d  e module */.  zM
2df9c 6f 64 20 3d 20 70 54 61 62 2d 3e 61 7a 4d 6f 64  od = pTab->azMod
2df9d 75 6c 65 41 72 67 5b 30 5d 3b 0a 20 20 70 4d 6f  uleArg[0];.  pMo
2df9e 64 20 3d 20 28 4d 6f 64 75 6c 65 2a 29 73 71 6c  d = (Module*)sql
2df9f 69 74 65 33 48 61 73 68 46 69 6e 64 28 26 64 62  ite3HashFind(&db
2dfa0 2d 3e 61 4d 6f 64 75 6c 65 2c 20 7a 4d 6f 64 2c  ->aModule, zMod,
2dfa1 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2dfa2 28 7a 4d 6f 64 29 29 3b 0a 0a 20 20 2f 2a 20 49  (zMod));..  /* I
2dfa3 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 68 61 73  f the module has
2dfa4 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65 64   been registered
2dfa5 20 61 6e 64 20 69 6e 63 6c 75 64 65 73 20 61 20   and includes a 
2dfa6 43 72 65 61 74 65 20 6d 65 74 68 6f 64 2c 20 0a  Create method, .
2dfa7 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 69 74 20 6e    ** invoke it n
2dfa8 6f 77 2e 20 49 66 20 74 68 65 20 6d 6f 64 75 6c  ow. If the modul
2dfa9 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 72  e has not been r
2dfaa 65 67 69 73 74 65 72 65 64 2c 20 72 65 74 75 72  egistered, retur
2dfab 6e 20 61 6e 20 0a 20 20 2a 2a 20 65 72 72 6f 72  n an .  ** error
2dfac 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 64 6f 20  . Otherwise, do 
2dfad 6e 6f 74 68 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20  nothing..  */.  
2dfae 69 66 28 20 21 70 4d 6f 64 20 29 7b 0a 20 20 20  if( !pMod ){.   
2dfaf 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65   *pzErr = sqlite
2dfb0 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f  3MPrintf(db, "no
2dfb1 20 73 75 63 68 20 6d 6f 64 75 6c 65 3a 20 25 73   such module: %s
2dfb2 22 2c 20 7a 4d 6f 64 29 3b 0a 20 20 20 20 72 63  ", zMod);.    rc
2dfb3 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
2dfb4 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
2dfb5 20 3d 20 76 74 61 62 43 61 6c 6c 43 6f 6e 73 74   = vtabCallConst
2dfb6 72 75 63 74 6f 72 28 64 62 2c 20 70 54 61 62 2c  ructor(db, pTab,
2dfb7 20 70 4d 6f 64 2c 20 70 4d 6f 64 2d 3e 70 4d 6f   pMod, pMod->pMo
2dfb8 64 75 6c 65 2d 3e 78 43 72 65 61 74 65 2c 20 70  dule->xCreate, p
2dfb9 7a 45 72 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  zErr);.  }..  /*
2dfba 20 4a 75 73 74 69 66 69 63 61 74 69 6f 6e 20 6f   Justification o
2dfbb 66 20 41 4c 57 41 59 53 28 29 3a 20 20 54 68 65  f ALWAYS():  The
2dfbc 20 78 43 6f 6e 73 74 72 75 63 74 6f 72 20 6d 65   xConstructor me
2dfbd 74 68 6f 64 20 69 73 20 72 65 71 75 69 72 65 64  thod is required
2dfbe 20 74 6f 0a 20 20 2a 2a 20 63 72 65 61 74 65 20   to.  ** create 
2dfbf 61 20 76 61 6c 69 64 20 73 71 6c 69 74 65 33 5f  a valid sqlite3_
2dfc0 76 74 61 62 20 69 66 20 69 74 20 72 65 74 75 72  vtab if it retur
2dfc1 6e 73 20 53 51 4c 49 54 45 5f 4f 4b 2e 20 2a 2f  ns SQLITE_OK. */
2dfc2 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
2dfc3 45 5f 4f 4b 20 26 26 20 41 4c 57 41 59 53 28 73  E_OK && ALWAYS(s
2dfc4 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28  qlite3GetVTable(
2dfc5 64 62 2c 20 70 54 61 62 29 29 20 29 7b 0a 20 20  db, pTab)) ){.  
2dfc6 20 20 20 20 72 63 20 3d 20 61 64 64 54 6f 56 54      rc = addToVT
2dfc7 72 61 6e 73 28 64 62 2c 20 73 71 6c 69 74 65 33  rans(db, sqlite3
2dfc8 47 65 74 56 54 61 62 6c 65 28 64 62 2c 20 70 54  GetVTable(db, pT
2dfc9 61 62 29 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  ab));.  }..  ret
2dfca 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
2dfcb 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
2dfcc 73 20 75 73 65 64 20 74 6f 20 73 65 74 20 74 68  s used to set th
2dfcd 65 20 73 63 68 65 6d 61 20 6f 66 20 61 20 76 69  e schema of a vi
2dfce 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 49 74  rtual table.  It
2dfcf 20 69 73 20 6f 6e 6c 79 0a 2a 2a 20 76 61 6c 69   is only.** vali
2dfd0 64 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 66  d to call this f
2dfd1 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 20 77 69 74  unction from wit
2dfd2 68 69 6e 20 74 68 65 20 78 43 72 65 61 74 65 28  hin the xCreate(
2dfd3 29 20 6f 72 20 78 43 6f 6e 6e 65 63 74 28 29 20  ) or xConnect() 
2dfd4 6f 66 20 61 0a 2a 2a 20 76 69 72 74 75 61 6c 20  of a.** virtual 
2dfd5 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f  table module..*/
2dfd6 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2dfd7 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f  sqlite3_declare_
2dfd8 76 74 61 62 28 73 71 6c 69 74 65 33 20 2a 64 62  vtab(sqlite3 *db
2dfd9 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43  , const char *zC
2dfda 72 65 61 74 65 54 61 62 6c 65 29 7b 0a 20 20 50  reateTable){.  P
2dfdb 61 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 0a 20  arse *pParse;.. 
2dfdc 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
2dfdd 5f 4f 4b 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54  _OK;.  Table *pT
2dfde 61 62 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72  ab;.  char *zErr
2dfdf 20 3d 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33   = 0;..  sqlite3
2dfe0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
2dfe1 3e 6d 75 74 65 78 29 3b 0a 20 20 70 54 61 62 20  >mutex);.  pTab 
2dfe2 3d 20 64 62 2d 3e 70 56 54 61 62 3b 0a 20 20 69  = db->pVTab;.  i
2dfe3 66 28 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20  f( !pTab ){.    
2dfe4 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
2dfe5 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20   SQLITE_MISUSE, 
2dfe6 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
2dfe7 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
2dfe8 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 65 74 75  mutex);.    retu
2dfe9 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  rn SQLITE_MISUSE
2dfea 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
2dfeb 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20  (pTab->tabFlags 
2dfec 26 20 54 46 5f 56 69 72 74 75 61 6c 29 21 3d 30  & TF_Virtual)!=0
2dfed 20 29 3b 0a 0a 20 20 70 50 61 72 73 65 20 3d 20   );..  pParse = 
2dfee 73 71 6c 69 74 65 33 53 74 61 63 6b 41 6c 6c 6f  sqlite3StackAllo
2dfef 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
2dff0 28 2a 70 50 61 72 73 65 29 29 3b 0a 20 20 69 66  (*pParse));.  if
2dff1 28 20 70 50 61 72 73 65 3d 3d 30 20 29 7b 0a 20  ( pParse==0 ){. 
2dff2 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
2dff3 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  OMEM;.  }else{. 
2dff4 20 20 20 70 50 61 72 73 65 2d 3e 64 65 63 6c 61     pParse->decla
2dff5 72 65 56 74 61 62 20 3d 20 31 3b 0a 20 20 20 20  reVtab = 1;.    
2dff6 70 50 61 72 73 65 2d 3e 64 62 20 3d 20 64 62 3b  pParse->db = db;
2dff7 0a 20 20 0a 20 20 20 20 69 66 28 20 0a 20 20 20  .  .    if( .   
2dff8 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 20 3d       SQLITE_OK =
2dff9 3d 20 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73  = sqlite3RunPars
2dffa 65 72 28 70 50 61 72 73 65 2c 20 7a 43 72 65 61  er(pParse, zCrea
2dffb 74 65 54 61 62 6c 65 2c 20 26 7a 45 72 72 29 20  teTable, &zErr) 
2dffc 26 26 20 0a 20 20 20 20 20 20 20 20 70 50 61 72  && .        pPar
2dffd 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 26 26  se->pNewTable &&
2dffe 20 0a 20 20 20 20 20 20 20 20 21 70 50 61 72 73   .        !pPars
2dfff 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2d 3e 70 53  e->pNewTable->pS
2e000 65 6c 65 63 74 20 26 26 20 0a 20 20 20 20 20 20  elect && .      
2e001 20 20 28 70 50 61 72 73 65 2d 3e 70 4e 65 77 54    (pParse->pNewT
2e002 61 62 6c 65 2d 3e 74 61 62 46 6c 61 67 73 20 26  able->tabFlags &
2e003 20 54 46 5f 56 69 72 74 75 61 6c 29 3d 3d 30 0a   TF_Virtual)==0.
2e004 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 66 28      ){.      if(
2e005 20 21 70 54 61 62 2d 3e 61 43 6f 6c 20 29 7b 0a   !pTab->aCol ){.
2e006 20 20 20 20 20 20 20 20 70 54 61 62 2d 3e 61 43          pTab->aC
2e007 6f 6c 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65  ol = pParse->pNe
2e008 77 54 61 62 6c 65 2d 3e 61 43 6f 6c 3b 0a 20 20  wTable->aCol;.  
2e009 20 20 20 20 20 20 70 54 61 62 2d 3e 6e 43 6f 6c        pTab->nCol
2e00a 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
2e00b 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 0a 20 20 20 20  able->nCol;.    
2e00c 20 20 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77      pParse->pNew
2e00d 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b  Table->nCol = 0;
2e00e 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
2e00f 3e 70 4e 65 77 54 61 62 6c 65 2d 3e 61 43 6f 6c  >pNewTable->aCol
2e010 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
2e011 20 20 20 20 64 62 2d 3e 70 56 54 61 62 20 3d 20      db->pVTab = 
2e012 30 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a  0;.    } else {.
2e013 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
2e014 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 45 52  or(db, SQLITE_ER
2e015 52 4f 52 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20  ROR, zErr);.    
2e016 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2e017 64 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 20  db, zErr);.     
2e018 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
2e019 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  OR;.    }.    pP
2e01a 61 72 73 65 2d 3e 64 65 63 6c 61 72 65 56 74 61  arse->declareVta
2e01b 62 20 3d 20 30 3b 0a 20 20 0a 20 20 20 20 69 66  b = 0;.  .    if
2e01c 28 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20  ( pParse->pVdbe 
2e01d 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
2e01e 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 70 50 61  VdbeFinalize(pPa
2e01f 72 73 65 2d 3e 70 56 64 62 65 29 3b 0a 20 20 20  rse->pVdbe);.   
2e020 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65   }.    sqlite3De
2e021 6c 65 74 65 54 61 62 6c 65 28 70 50 61 72 73 65  leteTable(pParse
2e022 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3b 0a 20 20  ->pNewTable);.  
2e023 20 20 73 71 6c 69 74 65 33 53 74 61 63 6b 46 72    sqlite3StackFr
2e024 65 65 28 64 62 2c 20 70 50 61 72 73 65 29 3b 0a  ee(db, pParse);.
2e025 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 28    }..  assert( (
2e026 72 63 26 30 78 66 66 29 3d 3d 72 63 20 29 3b 0a  rc&0xff)==rc );.
2e027 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70    rc = sqlite3Ap
2e028 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20  iExit(db, rc);. 
2e029 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
2e02a 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
2e02b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2e02c 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
2e02d 74 69 6f 6e 20 69 73 20 69 6e 76 6f 6b 65 64 20  tion is invoked 
2e02e 62 79 20 74 68 65 20 76 64 62 65 20 74 6f 20 63  by the vdbe to c
2e02f 61 6c 6c 20 74 68 65 20 78 44 65 73 74 72 6f 79  all the xDestroy
2e030 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68   method.** of th
2e031 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
2e032 6e 61 6d 65 64 20 7a 54 61 62 20 69 6e 20 64 61  named zTab in da
2e033 74 61 62 61 73 65 20 69 44 62 2e 20 54 68 69 73  tabase iDb. This
2e034 20 6f 63 63 75 72 73 0a 2a 2a 20 77 68 65 6e 20   occurs.** when 
2e035 61 20 44 52 4f 50 20 54 41 42 4c 45 20 69 73 20  a DROP TABLE is 
2e036 6d 65 6e 74 69 6f 6e 65 64 2e 0a 2a 2a 0a 2a 2a  mentioned..**.**
2e037 20 54 68 69 73 20 63 61 6c 6c 20 69 73 20 61 20   This call is a 
2e038 6e 6f 2d 6f 70 20 69 66 20 7a 54 61 62 20 69 73  no-op if zTab is
2e039 20 6e 6f 74 20 61 20 76 69 72 74 75 61 6c 20 74   not a virtual t
2e03a 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  able..*/.SQLITE_
2e03b 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2e03c 74 65 33 56 74 61 62 43 61 6c 6c 44 65 73 74 72  te3VtabCallDestr
2e03d 6f 79 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  oy(sqlite3 *db, 
2e03e 69 6e 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63  int iDb, const c
2e03f 68 61 72 20 2a 7a 54 61 62 29 7b 0a 20 20 69 6e  har *zTab){.  in
2e040 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
2e041 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  ;.  Table *pTab;
2e042 0a 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74  ..  pTab = sqlit
2e043 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
2e044 7a 54 61 62 2c 20 64 62 2d 3e 61 44 62 5b 69 44  zTab, db->aDb[iD
2e045 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28  b].zName);.  if(
2e046 20 41 4c 57 41 59 53 28 70 54 61 62 21 3d 30 20   ALWAYS(pTab!=0 
2e047 26 26 20 70 54 61 62 2d 3e 70 56 54 61 62 6c 65  && pTab->pVTable
2e048 21 3d 30 29 20 29 7b 0a 20 20 20 20 56 54 61 62  !=0) ){.    VTab
2e049 6c 65 20 2a 70 20 3d 20 76 74 61 62 44 69 73 63  le *p = vtabDisc
2e04a 6f 6e 6e 65 63 74 41 6c 6c 28 64 62 2c 20 70 54  onnectAll(db, pT
2e04b 61 62 29 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73  ab);..    rc = s
2e04c 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
2e04d 64 62 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  db);.    assert(
2e04e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
2e04f 3b 0a 20 20 20 20 72 63 20 3d 20 70 2d 3e 70 4d  ;.    rc = p->pM
2e050 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 65  od->pModule->xDe
2e051 73 74 72 6f 79 28 70 2d 3e 70 56 74 61 62 29 3b  stroy(p->pVtab);
2e052 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74  .    (void)sqlit
2e053 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a  e3SafetyOn(db);.
2e054 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74  .    /* Remove t
2e055 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 2a  he sqlite3_vtab*
2e056 20 66 72 6f 6d 20 74 68 65 20 61 56 54 72 61 6e   from the aVTran
2e057 73 5b 5d 20 61 72 72 61 79 2c 20 69 66 20 61 70  s[] array, if ap
2e058 70 6c 69 63 61 62 6c 65 20 2a 2f 0a 20 20 20 20  plicable */.    
2e059 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
2e05a 4b 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  K ){.      asser
2e05b 74 28 20 70 54 61 62 2d 3e 70 56 54 61 62 6c 65  t( pTab->pVTable
2e05c 3d 3d 70 20 26 26 20 70 2d 3e 70 4e 65 78 74 3d  ==p && p->pNext=
2e05d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70  =0 );.      p->p
2e05e 56 74 61 62 20 3d 20 30 3b 0a 20 20 20 20 20 20  Vtab = 0;.      
2e05f 70 54 61 62 2d 3e 70 56 54 61 62 6c 65 20 3d 20  pTab->pVTable = 
2e060 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  0;.      sqlite3
2e061 56 74 61 62 55 6e 6c 6f 63 6b 28 70 29 3b 0a 20  VtabUnlock(p);. 
2e062 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
2e063 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
2e064 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e  This function in
2e065 76 6f 6b 65 73 20 65 69 74 68 65 72 20 74 68 65  vokes either the
2e066 20 78 52 6f 6c 6c 62 61 63 6b 20 6f 72 20 78 43   xRollback or xC
2e067 6f 6d 6d 69 74 20 6d 65 74 68 6f 64 0a 2a 2a 20  ommit method.** 
2e068 6f 66 20 65 61 63 68 20 6f 66 20 74 68 65 20 76  of each of the v
2e069 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e  irtual tables in
2e06a 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 56 54   the sqlite3.aVT
2e06b 72 61 6e 73 20 61 72 72 61 79 2e 20 54 68 65 20  rans array. The 
2e06c 6d 65 74 68 6f 64 0a 2a 2a 20 63 61 6c 6c 65 64  method.** called
2e06d 20 69 73 20 69 64 65 6e 74 69 66 69 65 64 20 62   is identified b
2e06e 79 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  y the second arg
2e06f 75 6d 65 6e 74 2c 20 22 6f 66 66 73 65 74 22 2c  ument, "offset",
2e070 20 77 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65   which is.** the
2e071 20 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20 6d   offset of the m
2e072 65 74 68 6f 64 20 74 6f 20 63 61 6c 6c 20 69 6e  ethod to call in
2e073 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64   the sqlite3_mod
2e074 75 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ule structure..*
2e075 2a 0a 2a 2a 20 54 68 65 20 61 72 72 61 79 20 69  *.** The array i
2e076 73 20 63 6c 65 61 72 65 64 20 61 66 74 65 72 20  s cleared after 
2e077 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 63 61 6c  invoking the cal
2e078 6c 62 61 63 6b 73 2e 20 0a 2a 2f 0a 73 74 61 74  lbacks. .*/.stat
2e079 69 63 20 76 6f 69 64 20 63 61 6c 6c 46 69 6e 61  ic void callFina
2e07a 6c 69 73 65 72 28 73 71 6c 69 74 65 33 20 2a 64  liser(sqlite3 *d
2e07b 62 2c 20 69 6e 74 20 6f 66 66 73 65 74 29 7b 0a  b, int offset){.
2e07c 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 64    int i;.  if( d
2e07d 62 2d 3e 61 56 54 72 61 6e 73 20 29 7b 0a 20 20  b->aVTrans ){.  
2e07e 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
2e07f 3e 6e 56 54 72 61 6e 73 3b 20 69 2b 2b 29 7b 0a  >nVTrans; i++){.
2e080 20 20 20 20 20 20 56 54 61 62 6c 65 20 2a 70 56        VTable *pV
2e081 54 61 62 20 3d 20 64 62 2d 3e 61 56 54 72 61 6e  Tab = db->aVTran
2e082 73 5b 69 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69  s[i];.      sqli
2e083 74 65 33 5f 76 74 61 62 20 2a 70 20 3d 20 70 56  te3_vtab *p = pV
2e084 54 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 20 20  Tab->pVtab;.    
2e085 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20    if( p ){.     
2e086 20 20 20 69 6e 74 20 28 2a 78 29 28 73 71 6c 69     int (*x)(sqli
2e087 74 65 33 5f 76 74 61 62 20 2a 29 3b 0a 20 20 20  te3_vtab *);.   
2e088 20 20 20 20 20 78 20 3d 20 2a 28 69 6e 74 20 28       x = *(int (
2e089 2a 2a 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  **)(sqlite3_vtab
2e08a 20 2a 29 29 28 28 63 68 61 72 20 2a 29 70 2d 3e   *))((char *)p->
2e08b 70 4d 6f 64 75 6c 65 20 2b 20 6f 66 66 73 65 74  pModule + offset
2e08c 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 78  );.        if( x
2e08d 20 29 20 78 28 70 29 3b 0a 20 20 20 20 20 20 7d   ) x(p);.      }
2e08e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 74  .      sqlite3Vt
2e08f 61 62 55 6e 6c 6f 63 6b 28 70 56 54 61 62 29 3b  abUnlock(pVTab);
2e090 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
2e091 65 33 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d  e3DbFree(db, db-
2e092 3e 61 56 54 72 61 6e 73 29 3b 0a 20 20 20 20 64  >aVTrans);.    d
2e093 62 2d 3e 6e 56 54 72 61 6e 73 20 3d 20 30 3b 0a  b->nVTrans = 0;.
2e094 20 20 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20      db->aVTrans 
2e095 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  = 0;.  }.}../*.*
2e096 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 53 79  * Invoke the xSy
2e097 6e 63 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c  nc method of all
2e098 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   virtual tables 
2e099 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61  in the sqlite3.a
2e09a 56 54 72 61 6e 73 0a 2a 2a 20 61 72 72 61 79 2e  VTrans.** array.
2e09b 20 52 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f   Return the erro
2e09c 72 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 66  r code for the f
2e09d 69 72 73 74 20 65 72 72 6f 72 20 74 68 61 74 20  irst error that 
2e09e 6f 63 63 75 72 73 2c 20 6f 72 0a 2a 2a 20 53 51  occurs, or.** SQ
2e09f 4c 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 20 78  LITE_OK if all x
2e0a0 53 79 6e 63 20 6f 70 65 72 61 74 69 6f 6e 73 20  Sync operations 
2e0a1 61 72 65 20 73 75 63 63 65 73 73 66 75 6c 2e 0a  are successful..
2e0a2 2a 2a 0a 2a 2a 20 53 65 74 20 2a 70 7a 45 72 72  **.** Set *pzErr
2e0a3 6d 73 67 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  msg to point to 
2e0a4 61 20 62 75 66 66 65 72 20 74 68 61 74 20 73 68  a buffer that sh
2e0a5 6f 75 6c 64 20 62 65 20 72 65 6c 65 61 73 65 64  ould be released
2e0a6 20 75 73 69 6e 67 20 0a 2a 2a 20 73 71 6c 69 74   using .** sqlit
2e0a7 65 33 44 62 46 72 65 65 28 29 20 63 6f 6e 74 61  e3DbFree() conta
2e0a8 69 6e 69 6e 67 20 61 6e 20 65 72 72 6f 72 20 6d  ining an error m
2e0a9 65 73 73 61 67 65 2c 20 69 66 20 6f 6e 65 20 69  essage, if one i
2e0aa 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a 2a 2f 0a  s available..*/.
2e0ab 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2e0ac 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 53 79  nt sqlite3VtabSy
2e0ad 6e 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  nc(sqlite3 *db, 
2e0ae 63 68 61 72 20 2a 2a 70 7a 45 72 72 6d 73 67 29  char **pzErrmsg)
2e0af 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  {.  int i;.  int
2e0b0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
2e0b1 0a 20 20 69 6e 74 20 72 63 73 61 66 65 74 79 3b  .  int rcsafety;
2e0b2 0a 20 20 56 54 61 62 6c 65 20 2a 2a 61 56 54 72  .  VTable **aVTr
2e0b3 61 6e 73 20 3d 20 64 62 2d 3e 61 56 54 72 61 6e  ans = db->aVTran
2e0b4 73 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  s;..  rc = sqlit
2e0b5 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
2e0b6 0a 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d  .  db->aVTrans =
2e0b7 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72   0;.  for(i=0; r
2e0b8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
2e0b9 69 3c 64 62 2d 3e 6e 56 54 72 61 6e 73 3b 20 69  i<db->nVTrans; i
2e0ba 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 28 2a 78  ++){.    int (*x
2e0bb 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  )(sqlite3_vtab *
2e0bc 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76  );.    sqlite3_v
2e0bd 74 61 62 20 2a 70 56 74 61 62 20 3d 20 61 56 54  tab *pVtab = aVT
2e0be 72 61 6e 73 5b 69 5d 2d 3e 70 56 74 61 62 3b 0a  rans[i]->pVtab;.
2e0bf 20 20 20 20 69 66 28 20 70 56 74 61 62 20 26 26      if( pVtab &&
2e0c0 20 28 78 20 3d 20 70 56 74 61 62 2d 3e 70 4d 6f   (x = pVtab->pMo
2e0c1 64 75 6c 65 2d 3e 78 53 79 6e 63 29 21 3d 30 20  dule->xSync)!=0 
2e0c2 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 78 28  ){.      rc = x(
2e0c3 70 56 74 61 62 29 3b 0a 20 20 20 20 20 20 73 71  pVtab);.      sq
2e0c4 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2e0c5 2a 70 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20  *pzErrmsg);.    
2e0c6 20 20 2a 70 7a 45 72 72 6d 73 67 20 3d 20 70 56    *pzErrmsg = pV
2e0c7 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20  tab->zErrMsg;.  
2e0c8 20 20 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d      pVtab->zErrM
2e0c9 73 67 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  sg = 0;.    }.  
2e0ca 7d 0a 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 20  }.  db->aVTrans 
2e0cb 3d 20 61 56 54 72 61 6e 73 3b 0a 20 20 72 63 73  = aVTrans;.  rcs
2e0cc 61 66 65 74 79 20 3d 20 73 71 6c 69 74 65 33 53  afety = sqlite3S
2e0cd 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 0a 20 20  afetyOn(db);..  
2e0ce 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
2e0cf 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 72 63  K ){.    rc = rc
2e0d0 73 61 66 65 74 79 3b 0a 20 20 7d 0a 20 20 72 65  safety;.  }.  re
2e0d1 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
2e0d2 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 52 6f  * Invoke the xRo
2e0d3 6c 6c 62 61 63 6b 20 6d 65 74 68 6f 64 20 6f 66  llback method of
2e0d4 20 61 6c 6c 20 76 69 72 74 75 61 6c 20 74 61 62   all virtual tab
2e0d5 6c 65 73 20 69 6e 20 74 68 65 20 0a 2a 2a 20 73  les in the .** s
2e0d6 71 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20 61  qlite3.aVTrans a
2e0d7 72 72 61 79 2e 20 54 68 65 6e 20 63 6c 65 61 72  rray. Then clear
2e0d8 20 74 68 65 20 61 72 72 61 79 20 69 74 73 65 6c   the array itsel
2e0d9 66 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  f..*/.SQLITE_PRI
2e0da 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2e0db 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 73 71 6c  VtabRollback(sql
2e0dc 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 63 61 6c  ite3 *db){.  cal
2e0dd 6c 46 69 6e 61 6c 69 73 65 72 28 64 62 2c 20 6f  lFinaliser(db, o
2e0de 66 66 73 65 74 6f 66 28 73 71 6c 69 74 65 33 5f  ffsetof(sqlite3_
2e0df 6d 6f 64 75 6c 65 2c 78 52 6f 6c 6c 62 61 63 6b  module,xRollback
2e0e0 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  ));.  return SQL
2e0e1 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
2e0e2 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 43 6f 6d   Invoke the xCom
2e0e3 6d 69 74 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c  mit method of al
2e0e4 6c 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  l virtual tables
2e0e5 20 69 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69   in the .** sqli
2e0e6 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61  te3.aVTrans arra
2e0e7 79 2e 20 54 68 65 6e 20 63 6c 65 61 72 20 74 68  y. Then clear th
2e0e8 65 20 61 72 72 61 79 20 69 74 73 65 6c 66 2e 0a  e array itself..
2e0e9 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2e0ea 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61  E int sqlite3Vta
2e0eb 62 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 20  bCommit(sqlite3 
2e0ec 2a 64 62 29 7b 0a 20 20 63 61 6c 6c 46 69 6e 61  *db){.  callFina
2e0ed 6c 69 73 65 72 28 64 62 2c 20 6f 66 66 73 65 74  liser(db, offset
2e0ee 6f 66 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  of(sqlite3_modul
2e0ef 65 2c 78 43 6f 6d 6d 69 74 29 29 3b 0a 20 20 72  e,xCommit));.  r
2e0f0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2e0f1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
2e0f2 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 70   virtual table p
2e0f3 56 74 61 62 20 73 75 70 70 6f 72 74 73 20 74 68  Vtab supports th
2e0f4 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e  e transaction in
2e0f5 74 65 72 66 61 63 65 0a 2a 2a 20 28 78 42 65 67  terface.** (xBeg
2e0f6 69 6e 2f 78 52 6f 6c 6c 62 61 63 6b 2f 78 43 6f  in/xRollback/xCo
2e0f7 6d 6d 69 74 20 61 6e 64 20 6f 70 74 69 6f 6e 61  mmit and optiona
2e0f8 6c 6c 79 20 78 53 79 6e 63 29 20 61 6e 64 20 61  lly xSync) and a
2e0f9 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a   transaction is.
2e0fa 2a 2a 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  ** not currently
2e0fb 20 6f 70 65 6e 2c 20 69 6e 76 6f 6b 65 20 74 68   open, invoke th
2e0fc 65 20 78 42 65 67 69 6e 20 6d 65 74 68 6f 64 20  e xBegin method 
2e0fd 6e 6f 77 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  now..**.** If th
2e0fe 65 20 78 42 65 67 69 6e 20 63 61 6c 6c 20 69 73  e xBegin call is
2e0ff 20 73 75 63 63 65 73 73 66 75 6c 2c 20 70 6c 61   successful, pla
2e100 63 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76  ce the sqlite3_v
2e101 74 61 62 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 69  tab pointer.** i
2e102 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 56  n the sqlite3.aV
2e103 54 72 61 6e 73 20 61 72 72 61 79 2e 0a 2a 2f 0a  Trans array..*/.
2e104 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2e105 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 42 65  nt sqlite3VtabBe
2e106 67 69 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  gin(sqlite3 *db,
2e107 20 56 54 61 62 6c 65 20 2a 70 56 54 61 62 29 7b   VTable *pVTab){
2e108 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
2e109 54 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73 74 20 73  TE_OK;.  const s
2e10a 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70  qlite3_module *p
2e10b 4d 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a 20 53 70  Module;..  /* Sp
2e10c 65 63 69 61 6c 20 63 61 73 65 3a 20 49 66 20 64  ecial case: If d
2e10d 62 2d 3e 61 56 54 72 61 6e 73 20 69 73 20 4e 55  b->aVTrans is NU
2e10e 4c 4c 20 61 6e 64 20 64 62 2d 3e 6e 56 54 72 61  LL and db->nVTra
2e10f 6e 73 20 69 73 20 67 72 65 61 74 65 72 0a 20 20  ns is greater.  
2e110 2a 2a 20 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68  ** than zero, th
2e111 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
2e112 20 69 73 20 62 65 69 6e 67 20 63 61 6c 6c 65 64   is being called
2e113 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 0a 20   from within a. 
2e114 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64 75   ** virtual modu
2e115 6c 65 20 78 53 79 6e 63 28 29 20 63 61 6c 6c 62  le xSync() callb
2e116 61 63 6b 2e 20 49 74 20 69 73 20 69 6c 6c 65 67  ack. It is illeg
2e117 61 6c 20 74 6f 20 77 72 69 74 65 20 74 6f 20 0a  al to write to .
2e118 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d 6f 64    ** virtual mod
2e119 75 6c 65 20 74 61 62 6c 65 73 20 69 6e 20 74 68  ule tables in th
2e11a 69 73 20 63 61 73 65 2c 20 73 6f 20 72 65 74 75  is case, so retu
2e11b 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44  rn SQLITE_LOCKED
2e11c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
2e11d 69 74 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64  ite3VtabInSync(d
2e11e 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  b) ){.    return
2e11f 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 0a   SQLITE_LOCKED;.
2e120 20 20 7d 0a 20 20 69 66 28 20 21 70 56 54 61 62    }.  if( !pVTab
2e121 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
2e122 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 20 0a 20  QLITE_OK;.  } . 
2e123 20 70 4d 6f 64 75 6c 65 20 3d 20 70 56 54 61 62   pModule = pVTab
2e124 2d 3e 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  ->pVtab->pModule
2e125 3b 0a 0a 20 20 69 66 28 20 70 4d 6f 64 75 6c 65  ;..  if( pModule
2e126 2d 3e 78 42 65 67 69 6e 20 29 7b 0a 20 20 20 20  ->xBegin ){.    
2e127 69 6e 74 20 69 3b 0a 0a 0a 20 20 20 20 2f 2a 20  int i;...    /* 
2e128 49 66 20 70 56 74 61 62 20 69 73 20 61 6c 72 65  If pVtab is alre
2e129 61 64 79 20 69 6e 20 74 68 65 20 61 56 54 72 61  ady in the aVTra
2e12a 6e 73 20 61 72 72 61 79 2c 20 72 65 74 75 72 6e  ns array, return
2e12b 20 65 61 72 6c 79 20 2a 2f 0a 20 20 20 20 66 6f   early */.    fo
2e12c 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 56 54  r(i=0; i<db->nVT
2e12d 72 61 6e 73 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  rans; i++){.    
2e12e 20 20 69 66 28 20 64 62 2d 3e 61 56 54 72 61 6e    if( db->aVTran
2e12f 73 5b 69 5d 3d 3d 70 56 54 61 62 20 29 7b 0a 20  s[i]==pVTab ){. 
2e130 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
2e131 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d  LITE_OK;.      }
2e132 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49  .    }..    /* I
2e133 6e 76 6f 6b 65 20 74 68 65 20 78 42 65 67 69 6e  nvoke the xBegin
2e134 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 20 20 72   method */.    r
2e135 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 42 65  c = pModule->xBe
2e136 67 69 6e 28 70 56 54 61 62 2d 3e 70 56 74 61 62  gin(pVTab->pVtab
2e137 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  );.    if( rc==S
2e138 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
2e139 20 20 72 63 20 3d 20 61 64 64 54 6f 56 54 72 61    rc = addToVTra
2e13a 6e 73 28 64 62 2c 20 70 56 54 61 62 29 3b 0a 20  ns(db, pVTab);. 
2e13b 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
2e13c 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
2e13d 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
2e13e 65 72 20 28 70 44 65 66 29 20 69 73 20 61 20 66  er (pDef) is a f
2e13f 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e  unction implemen
2e140 74 61 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20  tation.  The.** 
2e141 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
2e142 20 28 70 45 78 70 72 29 20 69 73 20 74 68 65 20   (pExpr) is the 
2e143 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  first argument t
2e144 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  o this function.
2e145 0a 2a 2a 20 49 66 20 70 45 78 70 72 20 69 73 20  .** If pExpr is 
2e146 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76 69  a column in a vi
2e147 72 74 75 61 6c 20 74 61 62 6c 65 2c 20 74 68 65  rtual table, the
2e148 6e 20 6c 65 74 20 74 68 65 20 76 69 72 74 75 61  n let the virtua
2e149 6c 0a 2a 2a 20 74 61 62 6c 65 20 69 6d 70 6c 65  l.** table imple
2e14a 6d 65 6e 74 61 74 69 6f 6e 20 68 61 76 65 20 61  mentation have a
2e14b 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f  n opportunity to
2e14c 20 6f 76 65 72 6c 6f 61 64 20 74 68 65 20 66 75   overload the fu
2e14d 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  nction..**.** Th
2e14e 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
2e14f 65 64 20 74 6f 20 61 6c 6c 6f 77 20 76 69 72 74  ed to allow virt
2e150 75 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d  ual table implem
2e151 65 6e 74 61 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20  entations to.** 
2e152 6f 76 65 72 6c 6f 61 64 20 4d 41 54 43 48 2c 20  overload MATCH, 
2e153 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20 61 6e 64 20  LIKE, GLOB, and 
2e154 52 45 47 45 58 50 20 6f 70 65 72 61 74 6f 72 73  REGEXP operators
2e155 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 65  ..**.** Return e
2e156 69 74 68 65 72 20 74 68 65 20 70 44 65 66 20 61  ither the pDef a
2e157 72 67 75 6d 65 6e 74 20 28 69 6e 64 69 63 61 74  rgument (indicat
2e158 69 6e 67 20 6e 6f 20 63 68 61 6e 67 65 29 20 6f  ing no change) o
2e159 72 20 61 20 0a 2a 2a 20 6e 65 77 20 46 75 6e 63  r a .** new Func
2e15a 44 65 66 20 73 74 72 75 63 74 75 72 65 20 74 68  Def structure th
2e15b 61 74 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20  at is marked as 
2e15c 65 70 68 65 6d 65 72 61 6c 20 75 73 69 6e 67 20  ephemeral using 
2e15d 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 46 55  the.** SQLITE_FU
2e15e 4e 43 5f 45 50 48 45 4d 20 66 6c 61 67 2e 0a 2a  NC_EPHEM flag..*
2e15f 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
2e160 20 46 75 6e 63 44 65 66 20 2a 73 71 6c 69 74 65   FuncDef *sqlite
2e161 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e  3VtabOverloadFun
2e162 63 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33  ction(.  sqlite3
2e163 20 2a 64 62 2c 20 20 20 20 2f 2a 20 44 61 74 61   *db,    /* Data
2e164 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
2e165 66 6f 72 20 72 65 70 6f 72 74 69 6e 67 20 6d 61  for reporting ma
2e166 6c 6c 6f 63 20 70 72 6f 62 6c 65 6d 73 20 2a 2f  lloc problems */
2e167 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66  .  FuncDef *pDef
2e168 2c 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74  ,  /* Function t
2e169 6f 20 70 6f 73 73 69 62 6c 79 20 6f 76 65 72 6c  o possibly overl
2e16a 6f 61 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 72  oad */.  int nAr
2e16b 67 2c 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  g,       /* Numb
2e16c 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  er of arguments 
2e16d 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
2e16e 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72  */.  Expr *pExpr
2e16f 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 61 72       /* First ar
2e170 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 75  gument to the fu
2e171 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 54  nction */.){.  T
2e172 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 73 71  able *pTab;.  sq
2e173 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
2e174 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 6f 64  b;.  sqlite3_mod
2e175 75 6c 65 20 2a 70 4d 6f 64 3b 0a 20 20 76 6f 69  ule *pMod;.  voi
2e176 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69 74  d (*xFunc)(sqlit
2e177 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
2e178 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
2e179 20 3d 20 30 3b 0a 20 20 76 6f 69 64 20 2a 70 41   = 0;.  void *pA
2e17a 72 67 20 3d 20 30 3b 0a 20 20 46 75 6e 63 44 65  rg = 0;.  FuncDe
2e17b 66 20 2a 70 4e 65 77 3b 0a 20 20 69 6e 74 20 72  f *pNew;.  int r
2e17c 63 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a  c = 0;.  char *z
2e17d 4c 6f 77 65 72 4e 61 6d 65 3b 0a 20 20 75 6e 73  LowerName;.  uns
2e17e 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 0a  igned char *z;..
2e17f 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73  .  /* Check to s
2e180 65 65 20 74 68 65 20 6c 65 66 74 20 6f 70 65 72  ee the left oper
2e181 61 6e 64 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20  and is a column 
2e182 69 6e 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  in a virtual tab
2e183 6c 65 20 2a 2f 0a 20 20 69 66 28 20 4e 45 56 45  le */.  if( NEVE
2e184 52 28 70 45 78 70 72 3d 3d 30 29 20 29 20 72 65  R(pExpr==0) ) re
2e185 74 75 72 6e 20 70 44 65 66 3b 0a 20 20 69 66 28  turn pDef;.  if(
2e186 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43   pExpr->op!=TK_C
2e187 4f 4c 55 4d 4e 20 29 20 72 65 74 75 72 6e 20 70  OLUMN ) return p
2e188 44 65 66 3b 0a 20 20 70 54 61 62 20 3d 20 70 45  Def;.  pTab = pE
2e189 78 70 72 2d 3e 70 54 61 62 3b 0a 20 20 69 66 28  xpr->pTab;.  if(
2e18a 20 4e 45 56 45 52 28 70 54 61 62 3d 3d 30 29 20   NEVER(pTab==0) 
2e18b 29 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20  ) return pDef;. 
2e18c 20 69 66 28 20 28 70 54 61 62 2d 3e 74 61 62 46   if( (pTab->tabF
2e18d 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74 75 61  lags & TF_Virtua
2e18e 6c 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 70  l)==0 ) return p
2e18f 44 65 66 3b 0a 20 20 70 56 74 61 62 20 3d 20 73  Def;.  pVtab = s
2e190 71 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28  qlite3GetVTable(
2e191 64 62 2c 20 70 54 61 62 29 2d 3e 70 56 74 61 62  db, pTab)->pVtab
2e192 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56 74 61  ;.  assert( pVta
2e193 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  b!=0 );.  assert
2e194 28 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  ( pVtab->pModule
2e195 21 3d 30 20 29 3b 0a 20 20 70 4d 6f 64 20 3d 20  !=0 );.  pMod = 
2e196 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20  (sqlite3_module 
2e197 2a 29 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  *)pVtab->pModule
2e198 3b 0a 20 20 69 66 28 20 70 4d 6f 64 2d 3e 78 46  ;.  if( pMod->xF
2e199 69 6e 64 46 75 6e 63 74 69 6f 6e 3d 3d 30 20 29  indFunction==0 )
2e19a 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 0a   return pDef;. .
2e19b 20 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20 78 46    /* Call the xF
2e19c 69 6e 64 46 75 6e 63 74 69 6f 6e 20 6d 65 74 68  indFunction meth
2e19d 6f 64 20 6f 6e 20 74 68 65 20 76 69 72 74 75 61  od on the virtua
2e19e 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e  l table implemen
2e19f 74 61 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20 73  tation.  ** to s
2e1a0 65 65 20 69 66 20 74 68 65 20 69 6d 70 6c 65 6d  ee if the implem
2e1a1 65 6e 74 61 74 69 6f 6e 20 77 61 6e 74 73 20 74  entation wants t
2e1a2 6f 20 6f 76 65 72 6c 6f 61 64 20 74 68 69 73 20  o overload this 
2e1a3 66 75 6e 63 74 69 6f 6e 20 0a 20 20 2a 2f 0a 20  function .  */. 
2e1a4 20 7a 4c 6f 77 65 72 4e 61 6d 65 20 3d 20 73 71   zLowerName = sq
2e1a5 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
2e1a6 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pDef->zName);.
2e1a7 20 20 69 66 28 20 7a 4c 6f 77 65 72 4e 61 6d 65    if( zLowerName
2e1a8 20 29 7b 0a 20 20 20 20 66 6f 72 28 7a 3d 28 75   ){.    for(z=(u
2e1a9 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 7a 4c  nsigned char*)zL
2e1aa 6f 77 65 72 4e 61 6d 65 3b 20 2a 7a 3b 20 7a 2b  owerName; *z; z+
2e1ab 2b 29 7b 0a 20 20 20 20 20 20 2a 7a 20 3d 20 73  +){.      *z = s
2e1ac 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77  qlite3UpperToLow
2e1ad 65 72 5b 2a 7a 5d 3b 0a 20 20 20 20 7d 0a 20 20  er[*z];.    }.  
2e1ae 20 20 72 63 20 3d 20 70 4d 6f 64 2d 3e 78 46 69    rc = pMod->xFi
2e1af 6e 64 46 75 6e 63 74 69 6f 6e 28 70 56 74 61 62  ndFunction(pVtab
2e1b0 2c 20 6e 41 72 67 2c 20 7a 4c 6f 77 65 72 4e 61  , nArg, zLowerNa
2e1b1 6d 65 2c 20 26 78 46 75 6e 63 2c 20 26 70 41 72  me, &xFunc, &pAr
2e1b2 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  g);.    sqlite3D
2e1b3 62 46 72 65 65 28 64 62 2c 20 7a 4c 6f 77 65 72  bFree(db, zLower
2e1b4 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28  Name);.  }.  if(
2e1b5 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65   rc==0 ){.    re
2e1b6 74 75 72 6e 20 70 44 65 66 3b 0a 20 20 7d 0a 0a  turn pDef;.  }..
2e1b7 20 20 2f 2a 20 43 72 65 61 74 65 20 61 20 6e 65    /* Create a ne
2e1b8 77 20 65 70 68 65 6d 65 72 61 6c 20 66 75 6e 63  w ephemeral func
2e1b9 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20  tion definition 
2e1ba 66 6f 72 20 74 68 65 20 6f 76 65 72 6c 6f 61 64  for the overload
2e1bb 65 64 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e  ed.  ** function
2e1bc 20 2a 2f 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c   */.  pNew = sql
2e1bd 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
2e1be 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65  (db, sizeof(*pNe
2e1bf 77 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  w).             
2e1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e1c1 2b 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33  + sqlite3Strlen3
2e1c2 30 28 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 20 2b  0(pDef->zName) +
2e1c3 20 31 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d   1);.  if( pNew=
2e1c4 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
2e1c5 20 70 44 65 66 3b 0a 20 20 7d 0a 20 20 2a 70 4e   pDef;.  }.  *pN
2e1c6 65 77 20 3d 20 2a 70 44 65 66 3b 0a 20 20 70 4e  ew = *pDef;.  pN
2e1c7 65 77 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61  ew->zName = (cha
2e1c8 72 20 2a 29 26 70 4e 65 77 5b 31 5d 3b 0a 20 20  r *)&pNew[1];.  
2e1c9 6d 65 6d 63 70 79 28 70 4e 65 77 2d 3e 7a 4e 61  memcpy(pNew->zNa
2e1ca 6d 65 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c  me, pDef->zName,
2e1cb 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 33 30   sqlite3Strlen30
2e1cc 28 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 2b 31 29  (pDef->zName)+1)
2e1cd 3b 0a 20 20 70 4e 65 77 2d 3e 78 46 75 6e 63 20  ;.  pNew->xFunc 
2e1ce 3d 20 78 46 75 6e 63 3b 0a 20 20 70 4e 65 77 2d  = xFunc;.  pNew-
2e1cf 3e 70 55 73 65 72 44 61 74 61 20 3d 20 70 41 72  >pUserData = pAr
2e1d0 67 3b 0a 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73  g;.  pNew->flags
2e1d1 20 7c 3d 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f   |= SQLITE_FUNC_
2e1d2 45 50 48 45 4d 3b 0a 20 20 72 65 74 75 72 6e 20  EPHEM;.  return 
2e1d3 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  pNew;.}../*.** M
2e1d4 61 6b 65 20 73 75 72 65 20 76 69 72 74 75 61 6c  ake sure virtual
2e1d5 20 74 61 62 6c 65 20 70 54 61 62 20 69 73 20 63   table pTab is c
2e1d6 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20  ontained in the 
2e1d7 70 50 61 72 73 65 2d 3e 61 70 56 69 72 74 75 61  pParse->apVirtua
2e1d8 6c 4c 6f 63 6b 5b 5d 0a 2a 2a 20 61 72 72 61 79  lLock[].** array
2e1d9 20 73 6f 20 74 68 61 74 20 61 6e 20 4f 50 5f 56   so that an OP_V
2e1da 42 65 67 69 6e 20 77 69 6c 6c 20 67 65 74 20 67  Begin will get g
2e1db 65 6e 65 72 61 74 65 64 20 66 6f 72 20 69 74 2e  enerated for it.
2e1dc 20 20 41 64 64 20 70 54 61 62 20 74 6f 20 74 68    Add pTab to th
2e1dd 65 0a 2a 2a 20 61 72 72 61 79 20 69 66 20 69 74  e.** array if it
2e1de 20 69 73 20 6d 69 73 73 69 6e 67 2e 20 20 49 66   is missing.  If
2e1df 20 70 54 61 62 20 69 73 20 61 6c 72 65 61 64 79   pTab is already
2e1e0 20 69 6e 20 74 68 65 20 61 72 72 61 79 2c 20 74   in the array, t
2e1e1 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69  his routine.** i
2e1e2 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51  s a no-op..*/.SQ
2e1e3 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2e1e4 64 20 73 71 6c 69 74 65 33 56 74 61 62 4d 61 6b  d sqlite3VtabMak
2e1e5 65 57 72 69 74 61 62 6c 65 28 50 61 72 73 65 20  eWritable(Parse 
2e1e6 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a  *pParse, Table *
2e1e7 70 54 61 62 29 7b 0a 20 20 50 61 72 73 65 20 2a  pTab){.  Parse *
2e1e8 70 54 6f 70 6c 65 76 65 6c 20 3d 20 73 71 6c 69  pToplevel = sqli
2e1e9 74 65 33 50 61 72 73 65 54 6f 70 6c 65 76 65 6c  te3ParseToplevel
2e1ea 28 70 50 61 72 73 65 29 3b 0a 20 20 69 6e 74 20  (pParse);.  int 
2e1eb 69 2c 20 6e 3b 0a 20 20 54 61 62 6c 65 20 2a 2a  i, n;.  Table **
2e1ec 61 70 56 74 61 62 4c 6f 63 6b 3b 0a 0a 20 20 61  apVtabLock;..  a
2e1ed 73 73 65 72 74 28 20 49 73 56 69 72 74 75 61 6c  ssert( IsVirtual
2e1ee 28 70 54 61 62 29 20 29 3b 0a 20 20 66 6f 72 28  (pTab) );.  for(
2e1ef 69 3d 30 3b 20 69 3c 70 54 6f 70 6c 65 76 65 6c  i=0; i<pToplevel
2e1f0 2d 3e 6e 56 74 61 62 4c 6f 63 6b 3b 20 69 2b 2b  ->nVtabLock; i++
2e1f1 29 7b 0a 20 20 20 20 69 66 28 20 70 54 61 62 3d  ){.    if( pTab=
2e1f2 3d 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 70 56 74  =pToplevel->apVt
2e1f3 61 62 4c 6f 63 6b 5b 69 5d 20 29 20 72 65 74 75  abLock[i] ) retu
2e1f4 72 6e 3b 0a 20 20 7d 0a 20 20 6e 20 3d 20 28 70  rn;.  }.  n = (p
2e1f5 54 6f 70 6c 65 76 65 6c 2d 3e 6e 56 74 61 62 4c  Toplevel->nVtabL
2e1f6 6f 63 6b 2b 31 29 2a 73 69 7a 65 6f 66 28 70 54  ock+1)*sizeof(pT
2e1f7 6f 70 6c 65 76 65 6c 2d 3e 61 70 56 74 61 62 4c  oplevel->apVtabL
2e1f8 6f 63 6b 5b 30 5d 29 3b 0a 20 20 61 70 56 74 61  ock[0]);.  apVta
2e1f9 62 4c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f  bLock = sqlite3_
2e1fa 72 65 61 6c 6c 6f 63 28 70 54 6f 70 6c 65 76 65  realloc(pTopleve
2e1fb 6c 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 2c 20 6e  l->apVtabLock, n
2e1fc 29 3b 0a 20 20 69 66 28 20 61 70 56 74 61 62 4c  );.  if( apVtabL
2e1fd 6f 63 6b 20 29 7b 0a 20 20 20 20 70 54 6f 70 6c  ock ){.    pTopl
2e1fe 65 76 65 6c 2d 3e 61 70 56 74 61 62 4c 6f 63 6b  evel->apVtabLock
2e1ff 20 3d 20 61 70 56 74 61 62 4c 6f 63 6b 3b 0a 20   = apVtabLock;. 
2e200 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d 3e 61 70     pToplevel->ap
2e201 56 74 61 62 4c 6f 63 6b 5b 70 54 6f 70 6c 65 76  VtabLock[pToplev
2e202 65 6c 2d 3e 6e 56 74 61 62 4c 6f 63 6b 2b 2b 5d  el->nVtabLock++]
2e203 20 3d 20 70 54 61 62 3b 0a 20 20 7d 65 6c 73 65   = pTab;.  }else
2e204 7b 0a 20 20 20 20 70 54 6f 70 6c 65 76 65 6c 2d  {.    pToplevel-
2e205 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
2e206 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 23 65  d = 1;.  }.}..#e
2e207 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2e208 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2e209 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
2e20a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 74 61 62  **** End of vtab
2e20b 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2e20c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e20d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e20e 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
2e20f 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
2e210 77 68 65 72 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  where.c ********
2e211 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e212 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e213 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
2e214 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
2e215 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
2e216 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
2e217 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
2e218 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
2e219 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
2e21a 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
2e21b 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
2e21c 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
2e21d 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
2e21e 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2e21f 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
2e220 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
2e221 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
2e222 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2e223 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
2e224 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
2e225 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
2e226 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
2e227 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e228 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e229 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2e22a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
2e22b 68 69 73 20 6d 6f 64 75 6c 65 20 63 6f 6e 74 61  his module conta
2e22c 69 6e 73 20 43 20 63 6f 64 65 20 74 68 61 74 20  ins C code that 
2e22d 67 65 6e 65 72 61 74 65 73 20 56 44 42 45 20 63  generates VDBE c
2e22e 6f 64 65 20 75 73 65 64 20 74 6f 20 70 72 6f 63  ode used to proc
2e22f 65 73 73 0a 2a 2a 20 74 68 65 20 57 48 45 52 45  ess.** the WHERE
2e230 20 63 6c 61 75 73 65 20 6f 66 20 53 51 4c 20 73   clause of SQL s
2e231 74 61 74 65 6d 65 6e 74 73 2e 20 20 54 68 69 73  tatements.  This
2e232 20 6d 6f 64 75 6c 65 20 69 73 20 72 65 73 70 6f   module is respo
2e233 6e 73 69 62 6c 65 20 66 6f 72 0a 2a 2a 20 67 65  nsible for.** ge
2e234 6e 65 72 61 74 69 6e 67 20 74 68 65 20 63 6f 64  nerating the cod
2e235 65 20 74 68 61 74 20 6c 6f 6f 70 73 20 74 68 72  e that loops thr
2e236 6f 75 67 68 20 61 20 74 61 62 6c 65 20 6c 6f 6f  ough a table loo
2e237 6b 69 6e 67 20 66 6f 72 20 61 70 70 6c 69 63 61  king for applica
2e238 62 6c 65 0a 2a 2a 20 72 6f 77 73 2e 20 20 49 6e  ble.** rows.  In
2e239 64 69 63 65 73 20 61 72 65 20 73 65 6c 65 63 74  dices are select
2e23a 65 64 20 61 6e 64 20 75 73 65 64 20 74 6f 20 73  ed and used to s
2e23b 70 65 65 64 20 74 68 65 20 73 65 61 72 63 68 20  peed the search 
2e23c 77 68 65 6e 20 64 6f 69 6e 67 0a 2a 2a 20 73 6f  when doing.** so
2e23d 20 69 73 20 61 70 70 6c 69 63 61 62 6c 65 2e 20   is applicable. 
2e23e 20 42 65 63 61 75 73 65 20 74 68 69 73 20 6d 6f   Because this mo
2e23f 64 75 6c 65 20 69 73 20 72 65 73 70 6f 6e 73 69  dule is responsi
2e240 62 6c 65 20 66 6f 72 20 73 65 6c 65 63 74 69 6e  ble for selectin
2e241 67 0a 2a 2a 20 69 6e 64 69 63 65 73 2c 20 79 6f  g.** indices, yo
2e242 75 20 6d 69 67 68 74 20 61 6c 73 6f 20 74 68 69  u might also thi
2e243 6e 6b 20 6f 66 20 74 68 69 73 20 6d 6f 64 75 6c  nk of this modul
2e244 65 20 61 73 20 74 68 65 20 22 71 75 65 72 79 20  e as the "query 
2e245 6f 70 74 69 6d 69 7a 65 72 22 2e 0a 2a 2a 0a 2a  optimizer"..**.*
2e246 2a 20 24 49 64 3a 20 77 68 65 72 65 2e 63 2c 76  * $Id: where.c,v
2e247 20 31 2e 34 31 31 20 32 30 30 39 2f 30 37 2f 33   1.411 2009/07/3
2e248 31 20 30 36 3a 31 34 3a 35 32 20 64 61 6e 69 65  1 06:14:52 danie
2e249 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
2e24a 0a 2f 2a 0a 2a 2a 20 54 72 61 63 65 20 6f 75 74  ./*.** Trace out
2e24b 70 75 74 20 6d 61 63 72 6f 73 0a 2a 2f 0a 23 69  put macros.*/.#i
2e24c 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  f defined(SQLITE
2e24d 5f 54 45 53 54 29 20 7c 7c 20 64 65 66 69 6e 65  _TEST) || define
2e24e 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a  d(SQLITE_DEBUG).
2e24f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2e250 6e 74 20 73 71 6c 69 74 65 33 57 68 65 72 65 54  nt sqlite3WhereT
2e251 72 61 63 65 20 3d 20 30 3b 0a 23 65 6e 64 69 66  race = 0;.#endif
2e252 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
2e253 49 54 45 5f 54 45 53 54 29 20 26 26 20 64 65 66  ITE_TEST) && def
2e254 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55  ined(SQLITE_DEBU
2e255 47 29 0a 23 20 64 65 66 69 6e 65 20 57 48 45 52  G).# define WHER
2e256 45 54 52 41 43 45 28 58 29 20 20 69 66 28 73 71  ETRACE(X)  if(sq
2e257 6c 69 74 65 33 57 68 65 72 65 54 72 61 63 65 29  lite3WhereTrace)
2e258 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
2e259 6e 74 66 20 58 0a 23 65 6c 73 65 0a 23 20 64 65  ntf X.#else.# de
2e25a 66 69 6e 65 20 57 48 45 52 45 54 52 41 43 45 28  fine WHERETRACE(
2e25b 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 46 6f  X).#endif../* Fo
2e25c 72 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 0a  rward reference.
2e25d 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
2e25e 74 20 57 68 65 72 65 43 6c 61 75 73 65 20 57 68  t WhereClause Wh
2e25f 65 72 65 43 6c 61 75 73 65 3b 0a 74 79 70 65 64  ereClause;.typed
2e260 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 4d  ef struct WhereM
2e261 61 73 6b 53 65 74 20 57 68 65 72 65 4d 61 73 6b  askSet WhereMask
2e262 53 65 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72  Set;.typedef str
2e263 75 63 74 20 57 68 65 72 65 4f 72 49 6e 66 6f 20  uct WhereOrInfo 
2e264 57 68 65 72 65 4f 72 49 6e 66 6f 3b 0a 74 79 70  WhereOrInfo;.typ
2e265 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65 72  edef struct Wher
2e266 65 41 6e 64 49 6e 66 6f 20 57 68 65 72 65 41 6e  eAndInfo WhereAn
2e267 64 49 6e 66 6f 3b 0a 74 79 70 65 64 65 66 20 73  dInfo;.typedef s
2e268 74 72 75 63 74 20 57 68 65 72 65 43 6f 73 74 20  truct WhereCost 
2e269 57 68 65 72 65 43 6f 73 74 3b 0a 0a 2f 2a 0a 2a  WhereCost;../*.*
2e26a 2a 20 54 68 65 20 71 75 65 72 79 20 67 65 6e 65  * The query gene
2e26b 72 61 74 6f 72 20 75 73 65 73 20 61 6e 20 61 72  rator uses an ar
2e26c 72 61 79 20 6f 66 20 69 6e 73 74 61 6e 63 65 73  ray of instances
2e26d 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
2e26e 72 65 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 69 74  re to.** help it
2e26f 20 61 6e 61 6c 79 7a 65 20 74 68 65 20 73 75 62   analyze the sub
2e270 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74  expressions of t
2e271 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  he WHERE clause.
2e272 20 20 45 61 63 68 20 57 48 45 52 45 0a 2a 2a 20    Each WHERE.** 
2e273 63 6c 61 75 73 65 20 73 75 62 65 78 70 72 65 73  clause subexpres
2e274 73 69 6f 6e 20 69 73 20 73 65 70 61 72 61 74 65  sion is separate
2e275 64 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65 72  d from the other
2e276 73 20 62 79 20 41 4e 44 20 6f 70 65 72 61 74 6f  s by AND operato
2e277 72 73 2c 0a 2a 2a 20 75 73 75 61 6c 6c 79 2c 20  rs,.** usually, 
2e278 6f 72 20 73 6f 6d 65 74 69 6d 65 73 20 73 75 62  or sometimes sub
2e279 65 78 70 72 65 73 73 69 6f 6e 73 20 73 65 70 61  expressions sepa
2e27a 72 61 74 65 64 20 62 79 20 4f 52 2e 0a 2a 2a 0a  rated by OR..**.
2e27b 2a 2a 20 41 6c 6c 20 57 68 65 72 65 54 65 72 6d  ** All WhereTerm
2e27c 73 20 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 20  s are collected 
2e27d 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 57 68  into a single Wh
2e27e 65 72 65 43 6c 61 75 73 65 20 73 74 72 75 63 74  ereClause struct
2e27f 75 72 65 2e 20 20 0a 2a 2a 20 54 68 65 20 66 6f  ure.  .** The fo
2e280 6c 6c 6f 77 69 6e 67 20 69 64 65 6e 74 69 74 79  llowing identity
2e281 20 68 6f 6c 64 73 3a 0a 2a 2a 0a 2a 2a 20 20 20   holds:.**.**   
2e282 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 2e 70       WhereTerm.p
2e283 57 43 2d 3e 61 5b 57 68 65 72 65 54 65 72 6d 2e  WC->a[WhereTerm.
2e284 69 64 78 5d 20 3d 3d 20 57 68 65 72 65 54 65 72  idx] == WhereTer
2e285 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 74  m.**.** When a t
2e286 65 72 6d 20 69 73 20 6f 66 20 74 68 65 20 66 6f  erm is of the fo
2e287 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  rm:.**.**       
2e288 20 20 20 20 20 20 20 58 20 3c 6f 70 3e 20 3c 65         X <op> <e
2e289 78 70 72 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65  xpr>.**.** where
2e28a 20 58 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 6e   X is a column n
2e28b 61 6d 65 20 61 6e 64 20 3c 6f 70 3e 20 69 73 20  ame and <op> is 
2e28c 6f 6e 65 20 6f 66 20 63 65 72 74 61 69 6e 20 6f  one of certain o
2e28d 70 65 72 61 74 6f 72 73 2c 0a 2a 2a 20 74 68 65  perators,.** the
2e28e 6e 20 57 68 65 72 65 54 65 72 6d 2e 6c 65 66 74  n WhereTerm.left
2e28f 43 75 72 73 6f 72 20 61 6e 64 20 57 68 65 72 65  Cursor and Where
2e290 54 65 72 6d 2e 75 2e 6c 65 66 74 43 6f 6c 75 6d  Term.u.leftColum
2e291 6e 20 72 65 63 6f 72 64 20 74 68 65 0a 2a 2a 20  n record the.** 
2e292 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 61 6e  cursor number an
2e293 64 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20  d column number 
2e294 66 6f 72 20 58 2e 20 20 57 68 65 72 65 54 65 72  for X.  WhereTer
2e295 6d 2e 65 4f 70 65 72 61 74 6f 72 20 72 65 63 6f  m.eOperator reco
2e296 72 64 73 0a 2a 2a 20 74 68 65 20 3c 6f 70 3e 20  rds.** the <op> 
2e297 75 73 69 6e 67 20 61 20 62 69 74 6d 61 73 6b 20  using a bitmask 
2e298 65 6e 63 6f 64 69 6e 67 20 64 65 66 69 6e 65 64  encoding defined
2e299 20 62 79 20 57 4f 5f 78 78 78 20 62 65 6c 6f 77   by WO_xxx below
2e29a 2e 20 20 54 68 65 0a 2a 2a 20 75 73 65 20 6f 66  .  The.** use of
2e29b 20 61 20 62 69 74 6d 61 73 6b 20 65 6e 63 6f 64   a bitmask encod
2e29c 69 6e 67 20 66 6f 72 20 74 68 65 20 6f 70 65 72  ing for the oper
2e29d 61 74 6f 72 20 61 6c 6c 6f 77 73 20 75 73 20 74  ator allows us t
2e29e 6f 20 73 65 61 72 63 68 0a 2a 2a 20 71 75 69 63  o search.** quic
2e29f 6b 6c 79 20 66 6f 72 20 74 65 72 6d 73 20 74 68  kly for terms th
2e2a0 61 74 20 6d 61 74 63 68 20 61 6e 79 20 6f 66 20  at match any of 
2e2a1 73 65 76 65 72 61 6c 20 64 69 66 66 65 72 65 6e  several differen
2e2a2 74 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a 2a 0a  t operators..**.
2e2a3 2a 2a 20 41 20 57 68 65 72 65 54 65 72 6d 20 6d  ** A WhereTerm m
2e2a4 69 67 68 74 20 61 6c 73 6f 20 62 65 20 74 77 6f  ight also be two
2e2a5 20 6f 72 20 6d 6f 72 65 20 73 75 62 74 65 72 6d   or more subterm
2e2a6 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20 4f  s connected by O
2e2a7 52 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  R:.**.**        
2e2a8 20 28 74 31 2e 58 20 3c 6f 70 3e 20 3c 65 78 70   (t1.X <op> <exp
2e2a9 72 3e 29 20 4f 52 20 28 74 31 2e 59 20 3c 6f 70  r>) OR (t1.Y <op
2e2aa 3e 20 3c 65 78 70 72 3e 29 20 4f 52 20 2e 2e 2e  > <expr>) OR ...
2e2ab 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20  ..**.** In this 
2e2ac 73 65 63 6f 6e 64 20 63 61 73 65 2c 20 77 74 46  second case, wtF
2e2ad 6c 61 67 20 61 73 20 74 68 65 20 54 45 52 4d 5f  lag as the TERM_
2e2ae 4f 52 49 4e 46 4f 20 73 65 74 20 61 6e 64 20 65  ORINFO set and e
2e2af 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 4f 52 0a  Operator==WO_OR.
2e2b0 2a 2a 20 61 6e 64 20 74 68 65 20 57 68 65 72 65  ** and the Where
2e2b1 54 65 72 6d 2e 75 2e 70 4f 72 49 6e 66 6f 20 66  Term.u.pOrInfo f
2e2b2 69 65 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20 61  ield points to a
2e2b3 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61  uxiliary informa
2e2b4 74 69 6f 6e 20 74 68 61 74 0a 2a 2a 20 69 73 20  tion that.** is 
2e2b5 63 6f 6c 6c 65 63 74 65 64 20 61 62 6f 75 74 20  collected about 
2e2b6 74 68 65 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 74  the.**.** If a t
2e2b7 65 72 6d 20 69 6e 20 74 68 65 20 57 48 45 52 45  erm in the WHERE
2e2b8 20 63 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f 74   clause does not
2e2b9 20 6d 61 74 63 68 20 65 69 74 68 65 72 20 6f 66   match either of
2e2ba 20 74 68 65 20 74 77 6f 20 70 72 65 76 69 6f 75   the two previou
2e2bb 73 0a 2a 2a 20 63 61 74 65 67 6f 72 69 65 73 2c  s.** categories,
2e2bc 20 74 68 65 6e 20 65 4f 70 65 72 61 74 6f 72 3d   then eOperator=
2e2bd 3d 30 2e 20 20 54 68 65 20 57 68 65 72 65 54 65  =0.  The WhereTe
2e2be 72 6d 2e 70 45 78 70 72 20 66 69 65 6c 64 20 69  rm.pExpr field i
2e2bf 73 20 73 74 69 6c 6c 20 73 65 74 0a 2a 2a 20 74  s still set.** t
2e2c0 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 73  o the original s
2e2c1 75 62 65 78 70 72 65 73 73 69 6f 6e 20 63 6f 6e  ubexpression con
2e2c2 74 65 6e 74 20 61 6e 64 20 77 74 46 6c 61 67 73  tent and wtFlags
2e2c3 20 69 73 20 73 65 74 20 75 70 20 61 70 70 72 6f   is set up appro
2e2c4 70 72 69 61 74 65 6c 79 0a 2a 2a 20 62 75 74 20  priately.** but 
2e2c5 6e 6f 20 6f 74 68 65 72 20 66 69 65 6c 64 73 20  no other fields 
2e2c6 69 6e 20 74 68 65 20 57 68 65 72 65 54 65 72 6d  in the WhereTerm
2e2c7 20 6f 62 6a 65 63 74 20 61 72 65 20 6d 65 61 6e   object are mean
2e2c8 69 6e 67 66 75 6c 2e 0a 2a 2a 0a 2a 2a 20 57 68  ingful..**.** Wh
2e2c9 65 6e 20 65 4f 70 65 72 61 74 6f 72 21 3d 30 2c  en eOperator!=0,
2e2ca 20 70 72 65 72 65 71 52 69 67 68 74 20 61 6e 64   prereqRight and
2e2cb 20 70 72 65 72 65 71 41 6c 6c 20 72 65 63 6f 72   prereqAll recor
2e2cc 64 20 73 65 74 73 20 6f 66 20 63 75 72 73 6f 72  d sets of cursor
2e2cd 20 6e 75 6d 62 65 72 73 2c 0a 2a 2a 20 62 75 74   numbers,.** but
2e2ce 20 74 68 65 79 20 64 6f 20 73 6f 20 69 6e 64 69   they do so indi
2e2cf 72 65 63 74 6c 79 2e 20 20 41 20 73 69 6e 67 6c  rectly.  A singl
2e2d0 65 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 73  e WhereMaskSet s
2e2d1 74 72 75 63 74 75 72 65 20 74 72 61 6e 73 6c 61  tructure transla
2e2d2 74 65 73 0a 2a 2a 20 63 75 72 73 6f 72 20 6e 75  tes.** cursor nu
2e2d3 6d 62 65 72 20 69 6e 74 6f 20 62 69 74 73 20 61  mber into bits a
2e2d4 6e 64 20 74 68 65 20 74 72 61 6e 73 6c 61 74 65  nd the translate
2e2d5 64 20 62 69 74 20 69 73 20 73 74 6f 72 65 64 20  d bit is stored 
2e2d6 69 6e 20 74 68 65 20 70 72 65 72 65 71 0a 2a 2a  in the prereq.**
2e2d7 20 66 69 65 6c 64 73 2e 20 20 54 68 65 20 74 72   fields.  The tr
2e2d8 61 6e 73 6c 61 74 69 6f 6e 20 69 73 20 75 73 65  anslation is use
2e2d9 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61  d in order to ma
2e2da 78 69 6d 69 7a 65 20 74 68 65 20 6e 75 6d 62 65  ximize the numbe
2e2db 72 20 6f 66 0a 2a 2a 20 62 69 74 73 20 74 68 61  r of.** bits tha
2e2dc 74 20 77 69 6c 6c 20 66 69 74 20 69 6e 20 61 20  t will fit in a 
2e2dd 42 69 74 6d 61 73 6b 2e 20 20 54 68 65 20 56 44  Bitmask.  The VD
2e2de 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  BE cursor number
2e2df 73 20 6d 69 67 68 74 20 62 65 0a 2a 2a 20 73 70  s might be.** sp
2e2e0 72 65 61 64 20 6f 75 74 20 6f 76 65 72 20 74 68  read out over th
2e2e1 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20 69  e non-negative i
2e2e2 6e 74 65 67 65 72 73 2e 20 20 46 6f 72 20 65 78  ntegers.  For ex
2e2e3 61 6d 70 6c 65 2c 20 74 68 65 20 63 75 72 73 6f  ample, the curso
2e2e4 72 0a 2a 2a 20 6e 75 6d 62 65 72 73 20 6d 69 67  r.** numbers mig
2e2e5 68 74 20 62 65 20 33 2c 20 38 2c 20 39 2c 20 31  ht be 3, 8, 9, 1
2e2e6 30 2c 20 32 30 2c 20 32 33 2c 20 34 31 2c 20 61  0, 20, 23, 41, a
2e2e7 6e 64 20 34 35 2e 20 20 54 68 65 20 57 68 65 72  nd 45.  The Wher
2e2e8 65 4d 61 73 6b 53 65 74 0a 2a 2a 20 74 72 61 6e  eMaskSet.** tran
2e2e9 73 6c 61 74 65 73 20 74 68 65 73 65 20 73 70 61  slates these spa
2e2ea 72 73 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  rse cursor numbe
2e2eb 72 73 20 69 6e 74 6f 20 63 6f 6e 73 65 63 75 74  rs into consecut
2e2ec 69 76 65 20 69 6e 74 65 67 65 72 73 0a 2a 2a 20  ive integers.** 
2e2ed 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 30  beginning with 0
2e2ee 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61 6b   in order to mak
2e2ef 65 20 74 68 65 20 62 65 73 74 20 70 6f 73 73 69  e the best possi
2e2f0 62 6c 65 20 75 73 65 20 6f 66 20 74 68 65 20 61  ble use of the a
2e2f1 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 62 69 74 73  vailable.** bits
2e2f2 20 69 6e 20 74 68 65 20 42 69 74 6d 61 73 6b 2e   in the Bitmask.
2e2f3 20 20 53 6f 2c 20 69 6e 20 74 68 65 20 65 78 61    So, in the exa
2e2f4 6d 70 6c 65 20 61 62 6f 76 65 2c 20 74 68 65 20  mple above, the 
2e2f5 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 0a 2a  cursor numbers.*
2e2f6 2a 20 77 6f 75 6c 64 20 62 65 20 6d 61 70 70 65  * would be mappe
2e2f7 64 20 69 6e 74 6f 20 69 6e 74 65 67 65 72 73 20  d into integers 
2e2f8 30 20 74 68 72 6f 75 67 68 20 37 2e 0a 2a 2a 0a  0 through 7..**.
2e2f9 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** The number of
2e2fa 20 74 65 72 6d 73 20 69 6e 20 61 20 6a 6f 69 6e   terms in a join
2e2fb 20 69 73 20 6c 69 6d 69 74 65 64 20 62 79 20 74   is limited by t
2e2fc 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74  he number of bit
2e2fd 73 0a 2a 2a 20 69 6e 20 70 72 65 72 65 71 52 69  s.** in prereqRi
2e2fe 67 68 74 20 61 6e 64 20 70 72 65 72 65 71 41 6c  ght and prereqAl
2e2ff 6c 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 20  l.  The default 
2e300 69 73 20 36 34 20 62 69 74 73 2c 20 68 65 6e 63  is 64 bits, henc
2e301 65 20 53 51 4c 69 74 65 0a 2a 2a 20 69 73 20 6f  e SQLite.** is o
2e302 6e 6c 79 20 61 62 6c 65 20 74 6f 20 70 72 6f 63  nly able to proc
2e303 65 73 73 20 6a 6f 69 6e 73 20 77 69 74 68 20 36  ess joins with 6
2e304 34 20 6f 72 20 66 65 77 65 72 20 74 61 62 6c 65  4 or fewer table
2e305 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  s..*/.typedef st
2e306 72 75 63 74 20 57 68 65 72 65 54 65 72 6d 20 57  ruct WhereTerm W
2e307 68 65 72 65 54 65 72 6d 3b 0a 73 74 72 75 63 74  hereTerm;.struct
2e308 20 57 68 65 72 65 54 65 72 6d 20 7b 0a 20 20 45   WhereTerm {.  E
2e309 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20 20  xpr *pExpr;     
2e30a 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
2e30b 72 20 74 6f 20 74 68 65 20 73 75 62 65 78 70 72  r to the subexpr
2e30c 65 73 73 69 6f 6e 20 74 68 61 74 20 69 73 20 74  ession that is t
2e30d 68 69 73 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e  his term */.  in
2e30e 74 20 69 50 61 72 65 6e 74 3b 20 20 20 20 20 20  t iParent;      
2e30f 20 20 20 20 20 20 2f 2a 20 44 69 73 61 62 6c 65        /* Disable
2e310 20 70 57 43 2d 3e 61 5b 69 50 61 72 65 6e 74 5d   pWC->a[iParent]
2e311 20 77 68 65 6e 20 74 68 69 73 20 74 65 72 6d 20   when this term 
2e312 64 69 73 61 62 6c 65 64 20 2a 2f 0a 20 20 69 6e  disabled */.  in
2e313 74 20 6c 65 66 74 43 75 72 73 6f 72 3b 20 20 20  t leftCursor;   
2e314 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
2e315 6e 75 6d 62 65 72 20 6f 66 20 58 20 69 6e 20 22  number of X in "
2e316 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 20 2a  X <op> <expr>" *
2e317 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20 20 20  /.  union {.    
2e318 69 6e 74 20 6c 65 66 74 43 6f 6c 75 6d 6e 3b 20  int leftColumn; 
2e319 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d          /* Colum
2e31a 6e 20 6e 75 6d 62 65 72 20 6f 66 20 58 20 69 6e  n number of X in
2e31b 20 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e 22   "X <op> <expr>"
2e31c 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 4f 72 49   */.    WhereOrI
2e31d 6e 66 6f 20 2a 70 4f 72 49 6e 66 6f 3b 20 20 20  nfo *pOrInfo;   
2e31e 2f 2a 20 45 78 74 72 61 20 69 6e 66 6f 72 6d 61  /* Extra informa
2e31f 74 69 6f 6e 20 69 66 20 65 4f 70 65 72 61 74 6f  tion if eOperato
2e320 72 3d 3d 57 4f 5f 4f 52 20 2a 2f 0a 20 20 20 20  r==WO_OR */.    
2e321 57 68 65 72 65 41 6e 64 49 6e 66 6f 20 2a 70 41  WhereAndInfo *pA
2e322 6e 64 49 6e 66 6f 3b 20 2f 2a 20 45 78 74 72 61  ndInfo; /* Extra
2e323 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 66 20   information if 
2e324 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 41 4e  eOperator==WO_AN
2e325 44 20 2a 2f 0a 20 20 7d 20 75 3b 0a 20 20 75 31  D */.  } u;.  u1
2e326 36 20 65 4f 70 65 72 61 74 6f 72 3b 20 20 20 20  6 eOperator;    
2e327 20 20 20 20 20 20 2f 2a 20 41 20 57 4f 5f 78 78        /* A WO_xx
2e328 20 76 61 6c 75 65 20 64 65 73 63 72 69 62 69 6e   value describin
2e329 67 20 3c 6f 70 3e 20 2a 2f 0a 20 20 75 38 20 77  g <op> */.  u8 w
2e32a 74 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20  tFlags;         
2e32b 20 20 20 20 2f 2a 20 54 45 52 4d 5f 78 78 78 20      /* TERM_xxx 
2e32c 62 69 74 20 66 6c 61 67 73 2e 20 20 53 65 65 20  bit flags.  See 
2e32d 62 65 6c 6f 77 20 2a 2f 0a 20 20 75 38 20 6e 43  below */.  u8 nC
2e32e 68 69 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20  hild;           
2e32f 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2e330 63 68 69 6c 64 72 65 6e 20 74 68 61 74 20 6d 75  children that mu
2e331 73 74 20 64 69 73 61 62 6c 65 20 75 73 20 2a 2f  st disable us */
2e332 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a  .  WhereClause *
2e333 70 57 43 3b 20 20 20 20 20 20 20 2f 2a 20 54 68  pWC;       /* Th
2e334 65 20 63 6c 61 75 73 65 20 74 68 69 73 20 74 65  e clause this te
2e335 72 6d 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f  rm is part of */
2e336 0a 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72 65  .  Bitmask prere
2e337 71 52 69 67 68 74 3b 20 20 20 20 2f 2a 20 42 69  qRight;    /* Bi
2e338 74 6d 61 73 6b 20 6f 66 20 74 61 62 6c 65 73 20  tmask of tables 
2e339 75 73 65 64 20 62 79 20 70 45 78 70 72 2d 3e 70  used by pExpr->p
2e33a 52 69 67 68 74 20 2a 2f 0a 20 20 42 69 74 6d 61  Right */.  Bitma
2e33b 73 6b 20 70 72 65 72 65 71 41 6c 6c 3b 20 20 20  sk prereqAll;   
2e33c 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66     /* Bitmask of
2e33d 20 74 61 62 6c 65 73 20 72 65 66 65 72 65 6e 63   tables referenc
2e33e 65 64 20 62 79 20 70 45 78 70 72 20 2a 2f 0a 7d  ed by pExpr */.}
2e33f 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64  ;../*.** Allowed
2e340 20 76 61 6c 75 65 73 20 6f 66 20 57 68 65 72 65   values of Where
2e341 54 65 72 6d 2e 77 74 46 6c 61 67 73 0a 2a 2f 0a  Term.wtFlags.*/.
2e342 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 44 59 4e  #define TERM_DYN
2e343 41 4d 49 43 20 20 20 20 30 78 30 31 20 20 20 2f  AMIC    0x01   /
2e344 2a 20 4e 65 65 64 20 74 6f 20 63 61 6c 6c 20 73  * Need to call s
2e345 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
2e346 28 64 62 2c 20 70 45 78 70 72 29 20 2a 2f 0a 23  (db, pExpr) */.#
2e347 64 65 66 69 6e 65 20 54 45 52 4d 5f 56 49 52 54  define TERM_VIRT
2e348 55 41 4c 20 20 20 20 30 78 30 32 20 20 20 2f 2a  UAL    0x02   /*
2e349 20 41 64 64 65 64 20 62 79 20 74 68 65 20 6f 70   Added by the op
2e34a 74 69 6d 69 7a 65 72 2e 20 20 44 6f 20 6e 6f 74  timizer.  Do not
2e34b 20 63 6f 64 65 20 2a 2f 0a 23 64 65 66 69 6e 65   code */.#define
2e34c 20 54 45 52 4d 5f 43 4f 44 45 44 20 20 20 20 20   TERM_CODED     
2e34d 20 30 78 30 34 20 20 20 2f 2a 20 54 68 69 73 20   0x04   /* This 
2e34e 74 65 72 6d 20 69 73 20 61 6c 72 65 61 64 79 20  term is already 
2e34f 63 6f 64 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  coded */.#define
2e350 20 54 45 52 4d 5f 43 4f 50 49 45 44 20 20 20 20   TERM_COPIED    
2e351 20 30 78 30 38 20 20 20 2f 2a 20 48 61 73 20 61   0x08   /* Has a
2e352 20 63 68 69 6c 64 20 2a 2f 0a 23 64 65 66 69 6e   child */.#defin
2e353 65 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 20 20 20  e TERM_ORINFO   
2e354 20 20 30 78 31 30 20 20 20 2f 2a 20 4e 65 65 64    0x10   /* Need
2e355 20 74 6f 20 66 72 65 65 20 74 68 65 20 57 68 65   to free the Whe
2e356 72 65 54 65 72 6d 2e 75 2e 70 4f 72 49 6e 66 6f  reTerm.u.pOrInfo
2e357 20 6f 62 6a 65 63 74 20 2a 2f 0a 23 64 65 66 69   object */.#defi
2e358 6e 65 20 54 45 52 4d 5f 41 4e 44 49 4e 46 4f 20  ne TERM_ANDINFO 
2e359 20 20 20 30 78 32 30 20 20 20 2f 2a 20 4e 65 65     0x20   /* Nee
2e35a 64 20 74 6f 20 66 72 65 65 20 74 68 65 20 57 68  d to free the Wh
2e35b 65 72 65 54 65 72 6d 2e 75 2e 70 41 6e 64 49 6e  ereTerm.u.pAndIn
2e35c 66 6f 20 6f 62 6a 20 2a 2f 0a 23 64 65 66 69 6e  fo obj */.#defin
2e35d 65 20 54 45 52 4d 5f 4f 52 5f 4f 4b 20 20 20 20  e TERM_OR_OK    
2e35e 20 20 30 78 34 30 20 20 20 2f 2a 20 55 73 65 64    0x40   /* Used
2e35f 20 64 75 72 69 6e 67 20 4f 52 2d 63 6c 61 75 73   during OR-claus
2e360 65 20 70 72 6f 63 65 73 73 69 6e 67 20 2a 2f 0a  e processing */.
2e361 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
2e362 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
2e363 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 68 6f  ing structure ho
2e364 6c 64 73 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74  lds all informat
2e365 69 6f 6e 20 61 62 6f 75 74 20 61 0a 2a 2a 20 57  ion about a.** W
2e366 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 4d 6f  HERE clause.  Mo
2e367 73 74 6c 79 20 74 68 69 73 20 69 73 20 61 20 63  stly this is a c
2e368 6f 6e 74 61 69 6e 65 72 20 66 6f 72 20 6f 6e 65  ontainer for one
2e369 20 6f 72 20 6d 6f 72 65 20 57 68 65 72 65 54 65   or more WhereTe
2e36a 72 6d 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57  rms..*/.struct W
2e36b 68 65 72 65 43 6c 61 75 73 65 20 7b 0a 20 20 50  hereClause {.  P
2e36c 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20  arse *pParse;   
2e36d 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2e36e 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
2e36f 0a 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20  .  WhereMaskSet 
2e370 2a 70 4d 61 73 6b 53 65 74 3b 20 20 2f 2a 20 4d  *pMaskSet;  /* M
2e371 61 70 70 69 6e 67 20 6f 66 20 74 61 62 6c 65 20  apping of table 
2e372 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 74  cursor numbers t
2e373 6f 20 62 69 74 6d 61 73 6b 73 20 2a 2f 0a 20 20  o bitmasks */.  
2e374 42 69 74 6d 61 73 6b 20 76 6d 61 73 6b 3b 20 20  Bitmask vmask;  
2e375 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69 74 6d           /* Bitm
2e376 61 73 6b 20 69 64 65 6e 74 69 66 79 69 6e 67 20  ask identifying 
2e377 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63 75  virtual table cu
2e378 72 73 6f 72 73 20 2a 2f 0a 20 20 75 38 20 6f 70  rsors */.  u8 op
2e379 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2e37a 20 20 20 20 2f 2a 20 53 70 6c 69 74 20 6f 70 65      /* Split ope
2e37b 72 61 74 6f 72 2e 20 20 54 4b 5f 41 4e 44 20 6f  rator.  TK_AND o
2e37c 72 20 54 4b 5f 4f 52 20 2a 2f 0a 20 20 69 6e 74  r TK_OR */.  int
2e37d 20 6e 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20   nTerm;         
2e37e 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2e37f 6f 66 20 74 65 72 6d 73 20 2a 2f 0a 20 20 69 6e  of terms */.  in
2e380 74 20 6e 53 6c 6f 74 3b 20 20 20 20 20 20 20 20  t nSlot;        
2e381 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
2e382 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
2e383 5b 5d 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72  [] */.  WhereTer
2e384 6d 20 2a 61 3b 20 20 20 20 20 20 20 20 20 20 20  m *a;           
2e385 20 2f 2a 20 45 61 63 68 20 61 5b 5d 20 64 65 73   /* Each a[] des
2e386 63 72 69 62 65 73 20 61 20 74 65 72 6d 20 6f 66  cribes a term of
2e387 20 74 68 65 20 57 48 45 52 45 20 63 6c 75 61 73   the WHERE cluas
2e388 65 20 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64  e */.#if defined
2e389 28 53 51 4c 49 54 45 5f 53 4d 41 4c 4c 5f 53 54  (SQLITE_SMALL_ST
2e38a 41 43 4b 29 0a 20 20 57 68 65 72 65 54 65 72 6d  ACK).  WhereTerm
2e38b 20 61 53 74 61 74 69 63 5b 31 5d 3b 20 20 20 20   aStatic[1];    
2e38c 2f 2a 20 49 6e 69 74 69 61 6c 20 73 74 61 74 69  /* Initial stati
2e38d 63 20 73 70 61 63 65 20 66 6f 72 20 61 5b 5d 20  c space for a[] 
2e38e 2a 2f 0a 23 65 6c 73 65 0a 20 20 57 68 65 72 65  */.#else.  Where
2e38f 54 65 72 6d 20 61 53 74 61 74 69 63 5b 38 5d 3b  Term aStatic[8];
2e390 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20 73      /* Initial s
2e391 74 61 74 69 63 20 73 70 61 63 65 20 66 6f 72 20  tatic space for 
2e392 61 5b 5d 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b  a[] */.#endif.};
2e393 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65 54  ../*.** A WhereT
2e394 65 72 6d 20 77 69 74 68 20 65 4f 70 65 72 61 74  erm with eOperat
2e395 6f 72 3d 3d 57 4f 5f 4f 52 20 68 61 73 20 69 74  or==WO_OR has it
2e396 73 20 75 2e 70 4f 72 49 6e 66 6f 20 70 6f 69 6e  s u.pOrInfo poin
2e397 74 65 72 20 73 65 74 20 74 6f 0a 2a 2a 20 61 20  ter set to.** a 
2e398 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f  dynamically allo
2e399 63 61 74 65 64 20 69 6e 73 74 61 6e 63 65 20 6f  cated instance o
2e39a 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
2e39b 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74  structure..*/.st
2e39c 72 75 63 74 20 57 68 65 72 65 4f 72 49 6e 66 6f  ruct WhereOrInfo
2e39d 20 7b 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65   {.  WhereClause
2e39e 20 77 63 3b 20 20 20 20 20 20 20 20 20 20 2f 2a   wc;          /*
2e39f 20 44 65 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 69   Decomposition i
2e3a0 6e 74 6f 20 73 75 62 74 65 72 6d 73 20 2a 2f 0a  nto subterms */.
2e3a1 20 20 42 69 74 6d 61 73 6b 20 69 6e 64 65 78 61    Bitmask indexa
2e3a2 62 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20 42 69  ble;       /* Bi
2e3a3 74 6d 61 73 6b 20 6f 66 20 61 6c 6c 20 69 6e 64  tmask of all ind
2e3a4 65 78 61 62 6c 65 20 74 61 62 6c 65 73 20 69 6e  exable tables in
2e3a5 20 74 68 65 20 63 6c 61 75 73 65 20 2a 2f 0a 7d   the clause */.}
2e3a6 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 57 68 65 72 65  ;../*.** A Where
2e3a7 54 65 72 6d 20 77 69 74 68 20 65 4f 70 65 72 61  Term with eOpera
2e3a8 74 6f 72 3d 3d 57 4f 5f 41 4e 44 20 68 61 73 20  tor==WO_AND has 
2e3a9 69 74 73 20 75 2e 70 41 6e 64 49 6e 66 6f 20 70  its u.pAndInfo p
2e3aa 6f 69 6e 74 65 72 20 73 65 74 20 74 6f 0a 2a 2a  ointer set to.**
2e3ab 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61   a dynamically a
2e3ac 6c 6c 6f 63 61 74 65 64 20 69 6e 73 74 61 6e 63  llocated instanc
2e3ad 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
2e3ae 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  ng structure..*/
2e3af 0a 73 74 72 75 63 74 20 57 68 65 72 65 41 6e 64  .struct WhereAnd
2e3b0 49 6e 66 6f 20 7b 0a 20 20 57 68 65 72 65 43 6c  Info {.  WhereCl
2e3b1 61 75 73 65 20 77 63 3b 20 20 20 20 20 20 20 20  ause wc;        
2e3b2 20 20 2f 2a 20 54 68 65 20 73 75 62 65 78 70 72    /* The subexpr
2e3b3 65 73 73 69 6f 6e 20 62 72 6f 6b 65 6e 20 6f 75  ession broken ou
2e3b4 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  t */.};../*.** A
2e3b5 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
2e3b6 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
2e3b7 63 74 75 72 65 20 6b 65 65 70 73 20 74 72 61 63  cture keeps trac
2e3b8 6b 20 6f 66 20 61 20 6d 61 70 70 69 6e 67 0a 2a  k of a mapping.*
2e3b9 2a 20 62 65 74 77 65 65 6e 20 56 44 42 45 20 63  * between VDBE c
2e3ba 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 61 6e  ursor numbers an
2e3bb 64 20 62 69 74 73 20 6f 66 20 74 68 65 20 62 69  d bits of the bi
2e3bc 74 6d 61 73 6b 73 20 69 6e 20 57 68 65 72 65 54  tmasks in WhereT
2e3bd 65 72 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56  erm..**.** The V
2e3be 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  DBE cursor numbe
2e3bf 72 73 20 61 72 65 20 73 6d 61 6c 6c 20 69 6e 74  rs are small int
2e3c0 65 67 65 72 73 20 63 6f 6e 74 61 69 6e 65 64 20  egers contained 
2e3c1 69 6e 20 0a 2a 2a 20 53 72 63 4c 69 73 74 5f 69  in .** SrcList_i
2e3c2 74 65 6d 2e 69 43 75 72 73 6f 72 20 61 6e 64 20  tem.iCursor and 
2e3c3 45 78 70 72 2e 69 54 61 62 6c 65 20 66 69 65 6c  Expr.iTable fiel
2e3c4 64 73 2e 20 20 46 6f 72 20 61 6e 79 20 67 69 76  ds.  For any giv
2e3c5 65 6e 20 57 48 45 52 45 20 0a 2a 2a 20 63 6c 61  en WHERE .** cla
2e3c6 75 73 65 2c 20 74 68 65 20 63 75 72 73 6f 72 20  use, the cursor 
2e3c7 6e 75 6d 62 65 72 73 20 6d 69 67 68 74 20 6e 6f  numbers might no
2e3c8 74 20 62 65 67 69 6e 20 77 69 74 68 20 30 20 61  t begin with 0 a
2e3c9 6e 64 20 74 68 65 79 20 6d 69 67 68 74 0a 2a 2a  nd they might.**
2e3ca 20 63 6f 6e 74 61 69 6e 20 67 61 70 73 20 69 6e   contain gaps in
2e3cb 20 74 68 65 20 6e 75 6d 62 65 72 69 6e 67 20 73   the numbering s
2e3cc 65 71 75 65 6e 63 65 2e 20 20 42 75 74 20 77 65  equence.  But we
2e3cd 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20 6d 61   want to make ma
2e3ce 78 69 6d 75 6d 0a 2a 2a 20 75 73 65 20 6f 66 20  ximum.** use of 
2e3cf 74 68 65 20 62 69 74 73 20 69 6e 20 6f 75 72 20  the bits in our 
2e3d0 62 69 74 6d 61 73 6b 73 2e 20 20 54 68 69 73 20  bitmasks.  This 
2e3d1 73 74 72 75 63 74 75 72 65 20 70 72 6f 76 69 64  structure provid
2e3d2 65 73 20 61 20 6d 61 70 70 69 6e 67 0a 2a 2a 20  es a mapping.** 
2e3d3 66 72 6f 6d 20 74 68 65 20 73 70 61 72 73 65 20  from the sparse 
2e3d4 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69  cursor numbers i
2e3d5 6e 74 6f 20 63 6f 6e 73 65 63 75 74 69 76 65 20  nto consecutive 
2e3d6 69 6e 74 65 67 65 72 73 20 62 65 67 69 6e 6e 69  integers beginni
2e3d7 6e 67 0a 2a 2a 20 77 69 74 68 20 30 2e 0a 2a 2a  ng.** with 0..**
2e3d8 0a 2a 2a 20 49 66 20 57 68 65 72 65 4d 61 73 6b  .** If WhereMask
2e3d9 53 65 74 2e 69 78 5b 41 5d 3d 3d 42 20 69 74 20  Set.ix[A]==B it 
2e3da 6d 65 61 6e 73 20 74 68 61 74 20 54 68 65 20 41  means that The A
2e3db 2d 74 68 20 62 69 74 20 6f 66 20 61 20 42 69 74  -th bit of a Bit
2e3dc 6d 61 73 6b 0a 2a 2a 20 63 6f 72 72 65 73 70 6f  mask.** correspo
2e3dd 6e 64 73 20 56 44 42 45 20 63 75 72 73 6f 72 20  nds VDBE cursor 
2e3de 6e 75 6d 62 65 72 20 42 2e 20 20 54 68 65 20 41  number B.  The A
2e3df 2d 74 68 20 62 69 74 20 6f 66 20 61 20 62 69 74  -th bit of a bit
2e3e0 6d 61 73 6b 20 69 73 20 31 3c 3c 41 2e 0a 2a 2a  mask is 1<<A..**
2e3e1 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  .** For example,
2e3e2 20 69 66 20 74 68 65 20 57 48 45 52 45 20 63 6c   if the WHERE cl
2e3e3 61 75 73 65 20 65 78 70 72 65 73 73 69 6f 6e 20  ause expression 
2e3e4 75 73 65 64 20 74 68 65 73 65 20 56 44 42 45 0a  used these VDBE.
2e3e5 2a 2a 20 63 75 72 73 6f 72 73 3a 20 20 34 2c 20  ** cursors:  4, 
2e3e6 35 2c 20 38 2c 20 32 39 2c 20 35 37 2c 20 37 33  5, 8, 29, 57, 73
2e3e7 2e 20 20 54 68 65 6e 20 74 68 65 20 20 57 68 65  .  Then the  Whe
2e3e8 72 65 4d 61 73 6b 53 65 74 20 73 74 72 75 63 74  reMaskSet struct
2e3e9 75 72 65 0a 2a 2a 20 77 6f 75 6c 64 20 6d 61 70  ure.** would map
2e3ea 20 74 68 6f 73 65 20 63 75 72 73 6f 72 20 6e 75   those cursor nu
2e3eb 6d 62 65 72 73 20 69 6e 74 6f 20 62 69 74 73 20  mbers into bits 
2e3ec 30 20 74 68 72 6f 75 67 68 20 35 2e 0a 2a 2a 0a  0 through 5..**.
2e3ed 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  ** Note that the
2e3ee 20 6d 61 70 70 69 6e 67 20 69 73 20 6e 6f 74 20   mapping is not 
2e3ef 6e 65 63 65 73 73 61 72 69 6c 79 20 6f 72 64 65  necessarily orde
2e3f0 72 65 64 2e 20 20 49 6e 20 74 68 65 20 65 78 61  red.  In the exa
2e3f1 6d 70 6c 65 0a 2a 2a 20 61 62 6f 76 65 2c 20 74  mple.** above, t
2e3f2 68 65 20 6d 61 70 70 69 6e 67 20 6d 69 67 68 74  he mapping might
2e3f3 20 67 6f 20 6c 69 6b 65 20 74 68 69 73 3a 20 20   go like this:  
2e3f4 34 2d 3e 33 2c 20 35 2d 3e 31 2c 20 38 2d 3e 32  4->3, 5->1, 8->2
2e3f5 2c 20 32 39 2d 3e 30 2c 0a 2a 2a 20 35 37 2d 3e  , 29->0,.** 57->
2e3f6 35 2c 20 37 33 2d 3e 34 2e 20 20 4f 72 20 6f 6e  5, 73->4.  Or on
2e3f7 65 20 6f 66 20 37 31 39 20 6f 74 68 65 72 20 63  e of 719 other c
2e3f8 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6d 69 67 68  ombinations migh
2e3f9 74 20 62 65 20 75 73 65 64 2e 20 49 74 0a 2a 2a  t be used. It.**
2e3fa 20 64 6f 65 73 20 6e 6f 74 20 72 65 61 6c 6c 79   does not really
2e3fb 20 6d 61 74 74 65 72 2e 20 20 57 68 61 74 20 69   matter.  What i
2e3fc 73 20 69 6d 70 6f 72 74 61 6e 74 20 69 73 20 74  s important is t
2e3fd 68 61 74 20 73 70 61 72 73 65 20 63 75 72 73 6f  hat sparse curso
2e3fe 72 0a 2a 2a 20 6e 75 6d 62 65 72 73 20 61 6c 6c  r.** numbers all
2e3ff 20 67 65 74 20 6d 61 70 70 65 64 20 69 6e 74 6f   get mapped into
2e400 20 62 69 74 20 6e 75 6d 62 65 72 73 20 74 68 61   bit numbers tha
2e401 74 20 62 65 67 69 6e 20 77 69 74 68 20 30 20 61  t begin with 0 a
2e402 6e 64 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 6e 6f  nd contain.** no
2e403 20 67 61 70 73 2e 0a 2a 2f 0a 73 74 72 75 63 74   gaps..*/.struct
2e404 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20 7b 0a   WhereMaskSet {.
2e405 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20    int n;        
2e406 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e407 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 73 73  /* Number of ass
2e408 69 67 6e 65 64 20 63 75 72 73 6f 72 20 76 61 6c  igned cursor val
2e409 75 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 78 5b  ues */.  int ix[
2e40a 42 4d 53 5d 3b 20 20 20 20 20 20 20 20 20 20 20  BMS];           
2e40b 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
2e40c 20 61 73 73 69 67 6e 65 64 20 74 6f 20 65 61 63   assigned to eac
2e40d 68 20 62 69 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  h bit */.};../*.
2e40e 2a 2a 20 41 20 57 68 65 72 65 43 6f 73 74 20 6f  ** A WhereCost o
2e40f 62 6a 65 63 74 20 72 65 63 6f 72 64 73 20 61 20  bject records a 
2e410 6c 6f 6f 6b 75 70 20 73 74 72 61 74 65 67 79 20  lookup strategy 
2e411 61 6e 64 20 74 68 65 20 65 73 74 69 6d 61 74 65  and the estimate
2e412 64 0a 2a 2a 20 63 6f 73 74 20 6f 66 20 70 75 72  d.** cost of pur
2e413 73 75 69 6e 67 20 74 68 61 74 20 73 74 72 61 74  suing that strat
2e414 65 67 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57  egy..*/.struct W
2e415 68 65 72 65 43 6f 73 74 20 7b 0a 20 20 57 68 65  hereCost {.  Whe
2e416 72 65 50 6c 61 6e 20 70 6c 61 6e 3b 20 20 20 20  rePlan plan;    
2e417 2f 2a 20 54 68 65 20 6c 6f 6f 6b 75 70 20 73 74  /* The lookup st
2e418 72 61 74 65 67 79 20 2a 2f 0a 20 20 64 6f 75 62  rategy */.  doub
2e419 6c 65 20 72 43 6f 73 74 3b 20 20 20 20 20 20 2f  le rCost;      /
2e41a 2a 20 4f 76 65 72 61 6c 6c 20 63 6f 73 74 20 6f  * Overall cost o
2e41b 66 20 70 75 72 73 75 69 6e 67 20 74 68 69 73 20  f pursuing this 
2e41c 73 65 61 72 63 68 20 73 74 72 61 74 65 67 79 20  search strategy 
2e41d 2a 2f 0a 20 20 64 6f 75 62 6c 65 20 6e 52 6f 77  */.  double nRow
2e41e 3b 20 20 20 20 20 20 20 2f 2a 20 45 73 74 69 6d  ;       /* Estim
2e41f 61 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 6f  ated number of o
2e420 75 74 70 75 74 20 72 6f 77 73 20 2a 2f 0a 20 20  utput rows */.  
2e421 42 69 74 6d 61 73 6b 20 75 73 65 64 3b 20 20 20  Bitmask used;   
2e422 20 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66     /* Bitmask of
2e423 20 63 75 72 73 6f 72 73 20 75 73 65 64 20 62 79   cursors used by
2e424 20 74 68 69 73 20 70 6c 61 6e 20 2a 2f 0a 7d 3b   this plan */.};
2e425 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 6d 61 73 6b 73  ../*.** Bitmasks
2e426 20 66 6f 72 20 74 68 65 20 6f 70 65 72 61 74 6f   for the operato
2e427 72 73 20 74 68 61 74 20 69 6e 64 69 63 65 73 20  rs that indices 
2e428 61 72 65 20 61 62 6c 65 20 74 6f 20 65 78 70 6c  are able to expl
2e429 6f 69 74 2e 20 20 41 6e 0a 2a 2a 20 4f 52 2d 65  oit.  An.** OR-e
2e42a 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66  d combination of
2e42b 20 74 68 65 73 65 20 76 61 6c 75 65 73 20 63 61   these values ca
2e42c 6e 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 73  n be used when s
2e42d 65 61 72 63 68 69 6e 67 20 66 6f 72 0a 2a 2a 20  earching for.** 
2e42e 74 65 72 6d 73 20 69 6e 20 74 68 65 20 77 68 65  terms in the whe
2e42f 72 65 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 23 64  re clause..*/.#d
2e430 65 66 69 6e 65 20 57 4f 5f 49 4e 20 20 20 20 20  efine WO_IN     
2e431 30 78 30 30 31 0a 23 64 65 66 69 6e 65 20 57 4f  0x001.#define WO
2e432 5f 45 51 20 20 20 20 20 30 78 30 30 32 0a 23 64  _EQ     0x002.#d
2e433 65 66 69 6e 65 20 57 4f 5f 4c 54 20 20 20 20 20  efine WO_LT     
2e434 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 4c 54 2d 54  (WO_EQ<<(TK_LT-T
2e435 4b 5f 45 51 29 29 0a 23 64 65 66 69 6e 65 20 57  K_EQ)).#define W
2e436 4f 5f 4c 45 20 20 20 20 20 28 57 4f 5f 45 51 3c  O_LE     (WO_EQ<
2e437 3c 28 54 4b 5f 4c 45 2d 54 4b 5f 45 51 29 29 0a  <(TK_LE-TK_EQ)).
2e438 23 64 65 66 69 6e 65 20 57 4f 5f 47 54 20 20 20  #define WO_GT   
2e439 20 20 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f 47 54    (WO_EQ<<(TK_GT
2e43a 2d 54 4b 5f 45 51 29 29 0a 23 64 65 66 69 6e 65  -TK_EQ)).#define
2e43b 20 57 4f 5f 47 45 20 20 20 20 20 28 57 4f 5f 45   WO_GE     (WO_E
2e43c 51 3c 3c 28 54 4b 5f 47 45 2d 54 4b 5f 45 51 29  Q<<(TK_GE-TK_EQ)
2e43d 29 0a 23 64 65 66 69 6e 65 20 57 4f 5f 4d 41 54  ).#define WO_MAT
2e43e 43 48 20 20 30 78 30 34 30 0a 23 64 65 66 69 6e  CH  0x040.#defin
2e43f 65 20 57 4f 5f 49 53 4e 55 4c 4c 20 30 78 30 38  e WO_ISNULL 0x08
2e440 30 0a 23 64 65 66 69 6e 65 20 57 4f 5f 4f 52 20  0.#define WO_OR 
2e441 20 20 20 20 30 78 31 30 30 20 20 20 20 20 20 20      0x100       
2e442 2f 2a 20 54 77 6f 20 6f 72 20 6d 6f 72 65 20 4f  /* Two or more O
2e443 52 2d 63 6f 6e 6e 65 63 74 65 64 20 74 65 72 6d  R-connected term
2e444 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 4f 5f  s */.#define WO_
2e445 41 4e 44 20 20 20 20 30 78 32 30 30 20 20 20 20  AND    0x200    
2e446 20 20 20 2f 2a 20 54 77 6f 20 6f 72 20 6d 6f 72     /* Two or mor
2e447 65 20 41 4e 44 2d 63 6f 6e 6e 65 63 74 65 64 20  e AND-connected 
2e448 74 65 72 6d 73 20 2a 2f 0a 0a 23 64 65 66 69 6e  terms */..#defin
2e449 65 20 57 4f 5f 41 4c 4c 20 20 20 20 30 78 66 66  e WO_ALL    0xff
2e44a 66 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20  f       /* Mask 
2e44b 6f 66 20 61 6c 6c 20 70 6f 73 73 69 62 6c 65 20  of all possible 
2e44c 57 4f 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 23  WO_* values */.#
2e44d 64 65 66 69 6e 65 20 57 4f 5f 53 49 4e 47 4c 45  define WO_SINGLE
2e44e 20 30 78 30 66 66 20 20 20 20 20 20 20 2f 2a 20   0x0ff       /* 
2e44f 4d 61 73 6b 20 6f 66 20 61 6c 6c 20 6e 6f 6e 2d  Mask of all non-
2e450 63 6f 6d 70 6f 75 6e 64 20 57 4f 5f 2a 20 76 61  compound WO_* va
2e451 6c 75 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56  lues */../*.** V
2e452 61 6c 75 65 20 66 6f 72 20 77 73 46 6c 61 67 73  alue for wsFlags
2e453 20 72 65 74 75 72 6e 65 64 20 62 79 20 62 65 73   returned by bes
2e454 74 49 6e 64 65 78 28 29 20 61 6e 64 20 73 74 6f  tIndex() and sto
2e455 72 65 64 20 69 6e 0a 2a 2a 20 57 68 65 72 65 4c  red in.** WhereL
2e456 65 76 65 6c 2e 77 73 46 6c 61 67 73 2e 20 20 54  evel.wsFlags.  T
2e457 68 65 73 65 20 66 6c 61 67 73 20 64 65 74 65 72  hese flags deter
2e458 6d 69 6e 65 20 77 68 69 63 68 20 73 65 61 72 63  mine which searc
2e459 68 0a 2a 2a 20 73 74 72 61 74 65 67 69 65 73 20  h.** strategies 
2e45a 61 72 65 20 61 70 70 72 6f 70 72 69 61 74 65 2e  are appropriate.
2e45b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65 61 73 74  .**.** The least
2e45c 20 73 69 67 6e 69 66 69 63 61 6e 74 20 31 32 20   significant 12 
2e45d 62 69 74 73 20 69 73 20 72 65 73 65 72 76 65 64  bits is reserved
2e45e 20 61 73 20 61 20 6d 61 73 6b 20 66 6f 72 20 57   as a mask for W
2e45f 4f 5f 20 76 61 6c 75 65 73 20 61 62 6f 76 65 2e  O_ values above.
2e460 0a 2a 2a 20 54 68 65 20 57 68 65 72 65 4c 65 76  .** The WhereLev
2e461 65 6c 2e 77 73 46 6c 61 67 73 20 66 69 65 6c 64  el.wsFlags field
2e462 20 69 73 20 75 73 75 61 6c 6c 79 20 73 65 74 20   is usually set 
2e463 74 6f 20 57 4f 5f 49 4e 7c 57 4f 5f 45 51 7c 57  to WO_IN|WO_EQ|W
2e464 4f 5f 49 53 4e 55 4c 4c 2e 0a 2a 2a 20 42 75 74  O_ISNULL..** But
2e465 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73   if the table is
2e466 20 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65   the right table
2e467 20 6f 66 20 61 20 6c 65 66 74 20 6a 6f 69 6e 2c   of a left join,
2e468 20 57 68 65 72 65 4c 65 76 65 6c 2e 77 73 46 6c   WhereLevel.wsFl
2e469 61 67 73 0a 2a 2a 20 69 73 20 73 65 74 20 74 6f  ags.** is set to
2e46a 20 57 4f 5f 49 4e 7c 57 4f 5f 45 51 2e 20 20 54   WO_IN|WO_EQ.  T
2e46b 68 65 20 57 68 65 72 65 4c 65 76 65 6c 2e 77 73  he WhereLevel.ws
2e46c 46 6c 61 67 73 20 66 69 65 6c 64 20 63 61 6e 20  Flags field can 
2e46d 74 68 65 6e 20 62 65 20 75 73 65 64 20 61 73 0a  then be used as.
2e46e 2a 2a 20 74 68 65 20 22 6f 70 22 20 70 61 72 61  ** the "op" para
2e46f 6d 65 74 65 72 20 74 6f 20 66 69 6e 64 54 65 72  meter to findTer
2e470 6d 20 77 68 65 6e 20 77 65 20 61 72 65 20 72 65  m when we are re
2e471 73 6f 6c 76 69 6e 67 20 65 71 75 61 6c 69 74 79  solving equality
2e472 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 2a 2a   constraints..**
2e473 20 49 53 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69   ISNULL constrai
2e474 6e 74 73 20 77 69 6c 6c 20 74 68 65 6e 20 6e 6f  nts will then no
2e475 74 20 62 65 20 75 73 65 64 20 6f 6e 20 74 68 65  t be used on the
2e476 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20   right table of 
2e477 61 20 6c 65 66 74 0a 2a 2a 20 6a 6f 69 6e 2e 20  a left.** join. 
2e478 20 54 69 63 6b 65 74 73 20 23 32 31 37 37 20 61   Tickets #2177 a
2e479 6e 64 20 23 32 31 38 39 2e 0a 2a 2f 0a 23 64 65  nd #2189..*/.#de
2e47a 66 69 6e 65 20 57 48 45 52 45 5f 52 4f 57 49 44  fine WHERE_ROWID
2e47b 5f 45 51 20 20 20 20 20 30 78 30 30 30 30 31 30  _EQ     0x000010
2e47c 30 30 20 20 2f 2a 20 72 6f 77 69 64 3d 45 58 50  00  /* rowid=EXP
2e47d 52 20 6f 72 20 72 6f 77 69 64 20 49 4e 20 28 2e  R or rowid IN (.
2e47e 2e 2e 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  ..) */.#define W
2e47f 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45  HERE_ROWID_RANGE
2e480 20 20 30 78 30 30 30 30 32 30 30 30 20 20 2f 2a    0x00002000  /*
2e481 20 72 6f 77 69 64 3c 45 58 50 52 20 61 6e 64 2f   rowid<EXPR and/
2e482 6f 72 20 72 6f 77 69 64 3e 45 58 50 52 20 2a 2f  or rowid>EXPR */
2e483 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 43  .#define WHERE_C
2e484 4f 4c 55 4d 4e 5f 45 51 20 20 20 20 30 78 30 30  OLUMN_EQ    0x00
2e485 30 31 30 30 30 30 20 20 2f 2a 20 78 3d 45 58 50  010000  /* x=EXP
2e486 52 20 6f 72 20 78 20 49 4e 20 28 2e 2e 2e 29 20  R or x IN (...) 
2e487 6f 72 20 78 20 49 53 20 4e 55 4c 4c 20 2a 2f 0a  or x IS NULL */.
2e488 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 43 4f  #define WHERE_CO
2e489 4c 55 4d 4e 5f 52 41 4e 47 45 20 30 78 30 30 30  LUMN_RANGE 0x000
2e48a 32 30 30 30 30 20 20 2f 2a 20 78 3c 45 58 50 52  20000  /* x<EXPR
2e48b 20 61 6e 64 2f 6f 72 20 78 3e 45 58 50 52 20 2a   and/or x>EXPR *
2e48c 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
2e48d 43 4f 4c 55 4d 4e 5f 49 4e 20 20 20 20 30 78 30  COLUMN_IN    0x0
2e48e 30 30 34 30 30 30 30 20 20 2f 2a 20 78 20 49 4e  0040000  /* x IN
2e48f 20 28 2e 2e 2e 29 20 2a 2f 0a 23 64 65 66 69 6e   (...) */.#defin
2e490 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e  e WHERE_COLUMN_N
2e491 55 4c 4c 20 20 30 78 30 30 30 38 30 30 30 30 20  ULL  0x00080000 
2e492 20 2f 2a 20 78 20 49 53 20 4e 55 4c 4c 20 2a 2f   /* x IS NULL */
2e493 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 49  .#define WHERE_I
2e494 4e 44 45 58 45 44 20 20 20 20 20 20 30 78 30 30  NDEXED      0x00
2e495 30 66 30 30 30 30 20 20 2f 2a 20 41 6e 79 74 68  0f0000  /* Anyth
2e496 69 6e 67 20 74 68 61 74 20 75 73 65 73 20 61 6e  ing that uses an
2e497 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e   index */.#defin
2e498 65 20 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20  e WHERE_IN_ABLE 
2e499 20 20 20 20 20 30 78 30 30 30 66 31 30 30 30 20       0x000f1000 
2e49a 20 2f 2a 20 41 62 6c 65 20 74 6f 20 73 75 70 70   /* Able to supp
2e49b 6f 72 74 20 61 6e 20 49 4e 20 6f 70 65 72 61 74  ort an IN operat
2e49c 6f 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  or */.#define WH
2e49d 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 20 20 20  ERE_TOP_LIMIT   
2e49e 20 30 78 30 30 31 30 30 30 30 30 20 20 2f 2a 20   0x00100000  /* 
2e49f 78 3c 45 58 50 52 20 6f 72 20 78 3c 3d 45 58 50  x<EXPR or x<=EXP
2e4a0 52 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a  R constraint */.
2e4a1 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 42 54  #define WHERE_BT
2e4a2 4d 5f 4c 49 4d 49 54 20 20 20 20 30 78 30 30 32  M_LIMIT    0x002
2e4a3 30 30 30 30 30 20 20 2f 2a 20 78 3e 45 58 50 52  00000  /* x>EXPR
2e4a4 20 6f 72 20 78 3e 3d 45 58 50 52 20 63 6f 6e 73   or x>=EXPR cons
2e4a5 74 72 61 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e  traint */.#defin
2e4a6 65 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59  e WHERE_IDX_ONLY
2e4a7 20 20 20 20 20 30 78 30 30 38 30 30 30 30 30 20       0x00800000 
2e4a8 20 2f 2a 20 55 73 65 20 69 6e 64 65 78 20 6f 6e   /* Use index on
2e4a9 6c 79 20 2d 20 6f 6d 69 74 20 74 61 62 6c 65 20  ly - omit table 
2e4aa 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
2e4ab 5f 4f 52 44 45 52 42 59 20 20 20 20 20 20 30 78  _ORDERBY      0x
2e4ac 30 31 30 30 30 30 30 30 20 20 2f 2a 20 4f 75 74  01000000  /* Out
2e4ad 70 75 74 20 77 69 6c 6c 20 61 70 70 65 61 72 20  put will appear 
2e4ae 69 6e 20 63 6f 72 72 65 63 74 20 6f 72 64 65 72  in correct order
2e4af 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
2e4b0 45 5f 52 45 56 45 52 53 45 20 20 20 20 20 20 30  E_REVERSE      0
2e4b1 78 30 32 30 30 30 30 30 30 20 20 2f 2a 20 53 63  x02000000  /* Sc
2e4b2 61 6e 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72  an in reverse or
2e4b3 64 65 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57  der */.#define W
2e4b4 48 45 52 45 5f 55 4e 49 51 55 45 20 20 20 20 20  HERE_UNIQUE     
2e4b5 20 20 30 78 30 34 30 30 30 30 30 30 20 20 2f 2a    0x04000000  /*
2e4b6 20 53 65 6c 65 63 74 73 20 6e 6f 20 6d 6f 72 65   Selects no more
2e4b7 20 74 68 61 6e 20 6f 6e 65 20 72 6f 77 20 2a 2f   than one row */
2e4b8 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 56  .#define WHERE_V
2e4b9 49 52 54 55 41 4c 54 41 42 4c 45 20 30 78 30 38  IRTUALTABLE 0x08
2e4ba 30 30 30 30 30 30 20 20 2f 2a 20 55 73 65 20 76  000000  /* Use v
2e4bb 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 70 72 6f  irtual-table pro
2e4bc 63 65 73 73 69 6e 67 20 2a 2f 0a 23 64 65 66 69  cessing */.#defi
2e4bd 6e 65 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f  ne WHERE_MULTI_O
2e4be 52 20 20 20 20 20 30 78 31 30 30 30 30 30 30 30  R     0x10000000
2e4bf 20 20 2f 2a 20 4f 52 20 75 73 69 6e 67 20 6d 75    /* OR using mu
2e4c0 6c 74 69 70 6c 65 20 69 6e 64 69 63 65 73 20 2a  ltiple indices *
2e4c1 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c  /../*.** Initial
2e4c2 69 7a 65 20 61 20 70 72 65 61 6c 6c 6f 63 61 74  ize a preallocat
2e4c3 65 64 20 57 68 65 72 65 43 6c 61 75 73 65 20 73  ed WhereClause s
2e4c4 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 61  tructure..*/.sta
2e4c5 74 69 63 20 76 6f 69 64 20 77 68 65 72 65 43 6c  tic void whereCl
2e4c6 61 75 73 65 49 6e 69 74 28 0a 20 20 57 68 65 72  auseInit(.  Wher
2e4c7 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20  eClause *pWC,   
2e4c8 20 20 20 20 20 2f 2a 20 54 68 65 20 57 68 65 72       /* The Wher
2e4c9 65 43 6c 61 75 73 65 20 74 6f 20 62 65 20 69 6e  eClause to be in
2e4ca 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 50  itialized */.  P
2e4cb 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2e4cc 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
2e4cd 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2e4ce 2f 0a 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74  /.  WhereMaskSet
2e4cf 20 2a 70 4d 61 73 6b 53 65 74 20 20 20 2f 2a 20   *pMaskSet   /* 
2e4d0 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 74 61 62  Mapping from tab
2e4d1 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  le cursor number
2e4d2 73 20 74 6f 20 62 69 74 6d 61 73 6b 73 20 2a 2f  s to bitmasks */
2e4d3 0a 29 7b 0a 20 20 70 57 43 2d 3e 70 50 61 72 73  .){.  pWC->pPars
2e4d4 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 70 57  e = pParse;.  pW
2e4d5 43 2d 3e 70 4d 61 73 6b 53 65 74 20 3d 20 70 4d  C->pMaskSet = pM
2e4d6 61 73 6b 53 65 74 3b 0a 20 20 70 57 43 2d 3e 6e  askSet;.  pWC->n
2e4d7 54 65 72 6d 20 3d 20 30 3b 0a 20 20 70 57 43 2d  Term = 0;.  pWC-
2e4d8 3e 6e 53 6c 6f 74 20 3d 20 41 72 72 61 79 53 69  >nSlot = ArraySi
2e4d9 7a 65 28 70 57 43 2d 3e 61 53 74 61 74 69 63 29  ze(pWC->aStatic)
2e4da 3b 0a 20 20 70 57 43 2d 3e 61 20 3d 20 70 57 43  ;.  pWC->a = pWC
2e4db 2d 3e 61 53 74 61 74 69 63 3b 0a 20 20 70 57 43  ->aStatic;.  pWC
2e4dc 2d 3e 76 6d 61 73 6b 20 3d 20 30 3b 0a 7d 0a 0a  ->vmask = 0;.}..
2e4dd 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72  /* Forward refer
2e4de 65 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63 20 76  ence */.static v
2e4df 6f 69 64 20 77 68 65 72 65 43 6c 61 75 73 65 43  oid whereClauseC
2e4e0 6c 65 61 72 28 57 68 65 72 65 43 6c 61 75 73 65  lear(WhereClause
2e4e1 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c  *);../*.** Deall
2e4e2 6f 63 61 74 65 20 61 6c 6c 20 6d 65 6d 6f 72 79  ocate all memory
2e4e3 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
2e4e4 20 61 20 57 68 65 72 65 4f 72 49 6e 66 6f 20 6f   a WhereOrInfo o
2e4e5 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  bject..*/.static
2e4e6 20 76 6f 69 64 20 77 68 65 72 65 4f 72 49 6e 66   void whereOrInf
2e4e7 6f 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20  oDelete(sqlite3 
2e4e8 2a 64 62 2c 20 57 68 65 72 65 4f 72 49 6e 66 6f  *db, WhereOrInfo
2e4e9 20 2a 70 29 7b 0a 20 20 77 68 65 72 65 43 6c 61   *p){.  whereCla
2e4ea 75 73 65 43 6c 65 61 72 28 26 70 2d 3e 77 63 29  useClear(&p->wc)
2e4eb 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
2e4ec 65 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a  e(db, p);.}../*.
2e4ed 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 6c  ** Deallocate al
2e4ee 6c 20 6d 65 6d 6f 72 79 20 61 73 73 6f 63 69 61  l memory associa
2e4ef 74 65 64 20 77 69 74 68 20 61 20 57 68 65 72 65  ted with a Where
2e4f0 41 6e 64 49 6e 66 6f 20 6f 62 6a 65 63 74 2e 0a  AndInfo object..
2e4f1 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
2e4f2 68 65 72 65 41 6e 64 49 6e 66 6f 44 65 6c 65 74  hereAndInfoDelet
2e4f3 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 57  e(sqlite3 *db, W
2e4f4 68 65 72 65 41 6e 64 49 6e 66 6f 20 2a 70 29 7b  hereAndInfo *p){
2e4f5 0a 20 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c  .  whereClauseCl
2e4f6 65 61 72 28 26 70 2d 3e 77 63 29 3b 0a 20 20 73  ear(&p->wc);.  s
2e4f7 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2e4f8 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65   p);.}../*.** De
2e4f9 61 6c 6c 6f 63 61 74 65 20 61 20 57 68 65 72 65  allocate a Where
2e4fa 43 6c 61 75 73 65 20 73 74 72 75 63 74 75 72 65  Clause structure
2e4fb 2e 20 20 54 68 65 20 57 68 65 72 65 43 6c 61 75  .  The WhereClau
2e4fc 73 65 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  se structure.** 
2e4fd 69 74 73 65 6c 66 20 69 73 20 6e 6f 74 20 66 72  itself is not fr
2e4fe 65 65 64 2e 20 20 54 68 69 73 20 72 6f 75 74 69  eed.  This routi
2e4ff 6e 65 20 69 73 20 74 68 65 20 69 6e 76 65 72 73  ne is the invers
2e500 65 20 6f 66 20 77 68 65 72 65 43 6c 61 75 73 65  e of whereClause
2e501 49 6e 69 74 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  Init()..*/.stati
2e502 63 20 76 6f 69 64 20 77 68 65 72 65 43 6c 61 75  c void whereClau
2e503 73 65 43 6c 65 61 72 28 57 68 65 72 65 43 6c 61  seClear(WhereCla
2e504 75 73 65 20 2a 70 57 43 29 7b 0a 20 20 69 6e 74  use *pWC){.  int
2e505 20 69 3b 0a 20 20 57 68 65 72 65 54 65 72 6d 20   i;.  WhereTerm 
2e506 2a 61 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  *a;.  sqlite3 *d
2e507 62 20 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 2d  b = pWC->pParse-
2e508 3e 64 62 3b 0a 20 20 66 6f 72 28 69 3d 70 57 43  >db;.  for(i=pWC
2e509 2d 3e 6e 54 65 72 6d 2d 31 2c 20 61 3d 70 57 43  ->nTerm-1, a=pWC
2e50a 2d 3e 61 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20  ->a; i>=0; i--, 
2e50b 61 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 2d  a++){.    if( a-
2e50c 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f  >wtFlags & TERM_
2e50d 44 59 4e 41 4d 49 43 20 29 7b 0a 20 20 20 20 20  DYNAMIC ){.     
2e50e 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2e50f 74 65 28 64 62 2c 20 61 2d 3e 70 45 78 70 72 29  te(db, a->pExpr)
2e510 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
2e511 61 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52  a->wtFlags & TER
2e512 4d 5f 4f 52 49 4e 46 4f 20 29 7b 0a 20 20 20 20  M_ORINFO ){.    
2e513 20 20 77 68 65 72 65 4f 72 49 6e 66 6f 44 65 6c    whereOrInfoDel
2e514 65 74 65 28 64 62 2c 20 61 2d 3e 75 2e 70 4f 72  ete(db, a->u.pOr
2e515 49 6e 66 6f 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Info);.    }else
2e516 20 69 66 28 20 61 2d 3e 77 74 46 6c 61 67 73 20   if( a->wtFlags 
2e517 26 20 54 45 52 4d 5f 41 4e 44 49 4e 46 4f 20 29  & TERM_ANDINFO )
2e518 7b 0a 20 20 20 20 20 20 77 68 65 72 65 41 6e 64  {.      whereAnd
2e519 49 6e 66 6f 44 65 6c 65 74 65 28 64 62 2c 20 61  InfoDelete(db, a
2e51a 2d 3e 75 2e 70 41 6e 64 49 6e 66 6f 29 3b 0a 20  ->u.pAndInfo);. 
2e51b 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70     }.  }.  if( p
2e51c 57 43 2d 3e 61 21 3d 70 57 43 2d 3e 61 53 74 61  WC->a!=pWC->aSta
2e51d 74 69 63 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  tic ){.    sqlit
2e51e 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 57 43  e3DbFree(db, pWC
2e51f 2d 3e 61 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ->a);.  }.}../*.
2e520 2a 2a 20 41 64 64 20 61 20 73 69 6e 67 6c 65 20  ** Add a single 
2e521 6e 65 77 20 57 68 65 72 65 54 65 72 6d 20 65 6e  new WhereTerm en
2e522 74 72 79 20 74 6f 20 74 68 65 20 57 68 65 72 65  try to the Where
2e523 43 6c 61 75 73 65 20 6f 62 6a 65 63 74 20 70 57  Clause object pW
2e524 43 2e 0a 2a 2a 20 54 68 65 20 6e 65 77 20 57 68  C..** The new Wh
2e525 65 72 65 54 65 72 6d 20 6f 62 6a 65 63 74 20 69  ereTerm object i
2e526 73 20 63 6f 6e 73 74 72 75 63 74 65 64 20 66 72  s constructed fr
2e527 6f 6d 20 45 78 70 72 20 70 20 61 6e 64 20 77 69  om Expr p and wi
2e528 74 68 20 77 74 46 6c 61 67 73 2e 0a 2a 2a 20 54  th wtFlags..** T
2e529 68 65 20 69 6e 64 65 78 20 69 6e 20 70 57 43 2d  he index in pWC-
2e52a 3e 61 5b 5d 20 6f 66 20 74 68 65 20 6e 65 77 20  >a[] of the new 
2e52b 57 68 65 72 65 54 65 72 6d 20 69 73 20 72 65 74  WhereTerm is ret
2e52c 75 72 6e 65 64 20 6f 6e 20 73 75 63 63 65 73 73  urned on success
2e52d 2e 0a 2a 2a 20 30 20 69 73 20 72 65 74 75 72 6e  ..** 0 is return
2e52e 65 64 20 69 66 20 74 68 65 20 6e 65 77 20 57 68  ed if the new Wh
2e52f 65 72 65 54 65 72 6d 20 63 6f 75 6c 64 20 6e 6f  ereTerm could no
2e530 74 20 62 65 20 61 64 64 65 64 20 64 75 65 20 74  t be added due t
2e531 6f 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c  o a memory.** al
2e532 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 2e 20  location error. 
2e533 20 54 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   The memory allo
2e534 63 61 74 69 6f 6e 20 66 61 69 6c 75 72 65 20 77  cation failure w
2e535 69 6c 6c 20 62 65 20 72 65 63 6f 72 64 65 64 20  ill be recorded 
2e536 69 6e 0a 2a 2a 20 74 68 65 20 64 62 2d 3e 6d 61  in.** the db->ma
2e537 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20  llocFailed flag 
2e538 73 6f 20 74 68 61 74 20 68 69 67 68 65 72 2d 6c  so that higher-l
2e539 65 76 65 6c 20 66 75 6e 63 74 69 6f 6e 73 20 63  evel functions c
2e53a 61 6e 20 64 65 74 65 63 74 20 69 74 2e 0a 2a 2a  an detect it..**
2e53b 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2e53c 20 77 69 6c 6c 20 69 6e 63 72 65 61 73 65 20 74   will increase t
2e53d 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70  he size of the p
2e53e 57 43 2d 3e 61 5b 5d 20 61 72 72 61 79 20 61 73  WC->a[] array as
2e53f 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a 2a 0a 2a   necessary..**.*
2e540 2a 20 49 66 20 74 68 65 20 77 74 46 6c 61 67 73  * If the wtFlags
2e541 20 61 72 67 75 6d 65 6e 74 20 69 6e 63 6c 75 64   argument includ
2e542 65 73 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43 2c  es TERM_DYNAMIC,
2e543 20 74 68 65 6e 20 72 65 73 70 6f 6e 73 69 62 69   then responsibi
2e544 6c 69 74 79 0a 2a 2a 20 66 6f 72 20 66 72 65 65  lity.** for free
2e545 69 6e 67 20 74 68 65 20 65 78 70 72 65 73 73 69  ing the expressi
2e546 6f 6e 20 70 20 69 73 20 61 73 73 75 6d 65 64 20  on p is assumed 
2e547 62 79 20 74 68 65 20 57 68 65 72 65 43 6c 61 75  by the WhereClau
2e548 73 65 20 6f 62 6a 65 63 74 20 70 57 43 2e 0a 2a  se object pWC..*
2e549 2a 20 54 68 69 73 20 69 73 20 74 72 75 65 20 65  * This is true e
2e54a 76 65 6e 20 69 66 20 74 68 69 73 20 72 6f 75 74  ven if this rout
2e54b 69 6e 65 20 66 61 69 6c 73 20 74 6f 20 61 6c 6c  ine fails to all
2e54c 6f 63 61 74 65 20 61 20 6e 65 77 20 57 68 65 72  ocate a new Wher
2e54d 65 54 65 72 6d 2e 0a 2a 2a 0a 2a 2a 20 57 41 52  eTerm..**.** WAR
2e54e 4e 49 4e 47 3a 20 20 54 68 69 73 20 72 6f 75 74  NING:  This rout
2e54f 69 6e 65 20 6d 69 67 68 74 20 72 65 61 6c 6c 6f  ine might reallo
2e550 63 61 74 65 20 74 68 65 20 73 70 61 63 65 20 75  cate the space u
2e551 73 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20  sed to store.** 
2e552 57 68 65 72 65 54 65 72 6d 73 2e 20 20 41 6c 6c  WhereTerms.  All
2e553 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 57 68 65   pointers to Whe
2e554 72 65 54 65 72 6d 73 20 73 68 6f 75 6c 64 20 62  reTerms should b
2e555 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20 61 66  e invalidated af
2e556 74 65 72 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 74  ter.** calling t
2e557 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 53 75  his routine.  Su
2e558 63 68 20 70 6f 69 6e 74 65 72 73 20 6d 61 79 20  ch pointers may 
2e559 62 65 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 64  be reinitialized
2e55a 20 62 79 20 72 65 66 65 72 65 6e 63 69 6e 67 0a   by referencing.
2e55b 2a 2a 20 74 68 65 20 70 57 43 2d 3e 61 5b 5d 20  ** the pWC->a[] 
2e55c 61 72 72 61 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  array..*/.static
2e55d 20 69 6e 74 20 77 68 65 72 65 43 6c 61 75 73 65   int whereClause
2e55e 49 6e 73 65 72 74 28 57 68 65 72 65 43 6c 61 75  Insert(WhereClau
2e55f 73 65 20 2a 70 57 43 2c 20 45 78 70 72 20 2a 70  se *pWC, Expr *p
2e560 2c 20 75 38 20 77 74 46 6c 61 67 73 29 7b 0a 20  , u8 wtFlags){. 
2e561 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72   WhereTerm *pTer
2e562 6d 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 20  m;.  int idx;.  
2e563 69 66 28 20 70 57 43 2d 3e 6e 54 65 72 6d 3e 3d  if( pWC->nTerm>=
2e564 70 57 43 2d 3e 6e 53 6c 6f 74 20 29 7b 0a 20 20  pWC->nSlot ){.  
2e565 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 4f 6c    WhereTerm *pOl
2e566 64 20 3d 20 70 57 43 2d 3e 61 3b 0a 20 20 20 20  d = pWC->a;.    
2e567 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 57  sqlite3 *db = pW
2e568 43 2d 3e 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20  C->pParse->db;. 
2e569 20 20 20 70 57 43 2d 3e 61 20 3d 20 73 71 6c 69     pWC->a = sqli
2e56a 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
2e56b 62 2c 20 73 69 7a 65 6f 66 28 70 57 43 2d 3e 61  b, sizeof(pWC->a
2e56c 5b 30 5d 29 2a 70 57 43 2d 3e 6e 53 6c 6f 74 2a  [0])*pWC->nSlot*
2e56d 32 20 29 3b 0a 20 20 20 20 69 66 28 20 70 57 43  2 );.    if( pWC
2e56e 2d 3e 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ->a==0 ){.      
2e56f 69 66 28 20 77 74 46 6c 61 67 73 20 26 20 54 45  if( wtFlags & TE
2e570 52 4d 5f 44 59 4e 41 4d 49 43 20 29 7b 0a 20 20  RM_DYNAMIC ){.  
2e571 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2e572 72 44 65 6c 65 74 65 28 64 62 2c 20 70 29 3b 0a  rDelete(db, p);.
2e573 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 57        }.      pW
2e574 43 2d 3e 61 20 3d 20 70 4f 6c 64 3b 0a 20 20 20  C->a = pOld;.   
2e575 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
2e576 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 57   }.    memcpy(pW
2e577 43 2d 3e 61 2c 20 70 4f 6c 64 2c 20 73 69 7a 65  C->a, pOld, size
2e578 6f 66 28 70 57 43 2d 3e 61 5b 30 5d 29 2a 70 57  of(pWC->a[0])*pW
2e579 43 2d 3e 6e 54 65 72 6d 29 3b 0a 20 20 20 20 69  C->nTerm);.    i
2e57a 66 28 20 70 4f 6c 64 21 3d 70 57 43 2d 3e 61 53  f( pOld!=pWC->aS
2e57b 74 61 74 69 63 20 29 7b 0a 20 20 20 20 20 20 73  tatic ){.      s
2e57c 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2e57d 20 70 4f 6c 64 29 3b 0a 20 20 20 20 7d 0a 20 20   pOld);.    }.  
2e57e 20 20 70 57 43 2d 3e 6e 53 6c 6f 74 20 3d 20 73    pWC->nSlot = s
2e57f 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69  qlite3DbMallocSi
2e580 7a 65 28 64 62 2c 20 70 57 43 2d 3e 61 29 2f 73  ze(db, pWC->a)/s
2e581 69 7a 65 6f 66 28 70 57 43 2d 3e 61 5b 30 5d 29  izeof(pWC->a[0])
2e582 3b 0a 20 20 7d 0a 20 20 70 54 65 72 6d 20 3d 20  ;.  }.  pTerm = 
2e583 26 70 57 43 2d 3e 61 5b 69 64 78 20 3d 20 70 57  &pWC->a[idx = pW
2e584 43 2d 3e 6e 54 65 72 6d 2b 2b 5d 3b 0a 20 20 70  C->nTerm++];.  p
2e585 54 65 72 6d 2d 3e 70 45 78 70 72 20 3d 20 70 3b  Term->pExpr = p;
2e586 0a 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67  .  pTerm->wtFlag
2e587 73 20 3d 20 77 74 46 6c 61 67 73 3b 0a 20 20 70  s = wtFlags;.  p
2e588 54 65 72 6d 2d 3e 70 57 43 20 3d 20 70 57 43 3b  Term->pWC = pWC;
2e589 0a 20 20 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e  .  pTerm->iParen
2e58a 74 20 3d 20 2d 31 3b 0a 20 20 72 65 74 75 72 6e  t = -1;.  return
2e58b 20 69 64 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54   idx;.}../*.** T
2e58c 68 69 73 20 72 6f 75 74 69 6e 65 20 69 64 65 6e  his routine iden
2e58d 74 69 66 69 65 73 20 73 75 62 65 78 70 72 65 73  tifies subexpres
2e58e 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 57 48 45  sions in the WHE
2e58f 52 45 20 63 6c 61 75 73 65 20 77 68 65 72 65 0a  RE clause where.
2e590 2a 2a 20 65 61 63 68 20 73 75 62 65 78 70 72 65  ** each subexpre
2e591 73 73 69 6f 6e 20 69 73 20 73 65 70 61 72 61 74  ssion is separat
2e592 65 64 20 62 79 20 74 68 65 20 41 4e 44 20 6f 70  ed by the AND op
2e593 65 72 61 74 6f 72 20 6f 72 20 73 6f 6d 65 20 6f  erator or some o
2e594 74 68 65 72 0a 2a 2a 20 6f 70 65 72 61 74 6f 72  ther.** operator
2e595 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68   specified in th
2e596 65 20 6f 70 20 70 61 72 61 6d 65 74 65 72 2e 20  e op parameter. 
2e597 20 54 68 65 20 57 68 65 72 65 43 6c 61 75 73 65   The WhereClause
2e598 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73   structure.** is
2e599 20 66 69 6c 6c 65 64 20 77 69 74 68 20 70 6f 69   filled with poi
2e59a 6e 74 65 72 73 20 74 6f 20 73 75 62 65 78 70 72  nters to subexpr
2e59b 65 73 73 69 6f 6e 73 2e 20 20 46 6f 72 20 65 78  essions.  For ex
2e59c 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ample:.**.**    
2e59d 57 48 45 52 45 20 20 61 3d 3d 27 68 65 6c 6c 6f  WHERE  a=='hello
2e59e 27 20 41 4e 44 20 63 6f 61 6c 65 73 63 65 28 62  ' AND coalesce(b
2e59f 2c 31 31 29 3c 31 30 20 41 4e 44 20 28 63 2b 31  ,11)<10 AND (c+1
2e5a0 32 21 3d 64 20 4f 52 20 63 3d 3d 32 32 29 0a 2a  2!=d OR c==22).*
2e5a1 2a 20 20 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f  *           \___
2e5a2 5f 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f  _____/     \____
2e5a3 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20  ___________/    
2e5a4 20 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f   \______________
2e5a5 5f 5f 2f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  __/.**          
2e5a6 20 20 73 6c 6f 74 5b 30 5d 20 20 20 20 20 20 20    slot[0]       
2e5a7 20 20 20 20 20 73 6c 6f 74 5b 31 5d 20 20 20 20       slot[1]    
2e5a8 20 20 20 20 20 20 20 20 20 20 20 73 6c 6f 74 5b             slot[
2e5a9 32 5d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72 69  2].**.** The ori
2e5aa 67 69 6e 61 6c 20 57 48 45 52 45 20 63 6c 61 75  ginal WHERE clau
2e5ab 73 65 20 69 6e 20 70 45 78 70 72 20 69 73 20 75  se in pExpr is u
2e5ac 6e 61 6c 74 65 72 65 64 2e 20 20 41 6c 6c 20 74  naltered.  All t
2e5ad 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 64  his routine.** d
2e5ae 6f 65 73 20 69 73 20 6d 61 6b 65 20 73 6c 6f 74  oes is make slot
2e5af 5b 5d 20 65 6e 74 72 69 65 73 20 70 6f 69 6e 74  [] entries point
2e5b0 20 74 6f 20 73 75 62 73 74 72 75 63 74 75 72 65   to substructure
2e5b1 20 77 69 74 68 69 6e 20 70 45 78 70 72 2e 0a 2a   within pExpr..*
2e5b2 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 70 72 65 76  *.** In the prev
2e5b3 69 6f 75 73 20 73 65 6e 74 65 6e 63 65 20 61 6e  ious sentence an
2e5b4 64 20 69 6e 20 74 68 65 20 64 69 61 67 72 61 6d  d in the diagram
2e5b5 2c 20 22 73 6c 6f 74 5b 5d 22 20 72 65 66 65 72  , "slot[]" refer
2e5b6 73 20 74 6f 0a 2a 2a 20 74 68 65 20 57 68 65 72  s to.** the Wher
2e5b7 65 43 6c 61 75 73 65 2e 61 5b 5d 20 61 72 72 61  eClause.a[] arra
2e5b8 79 2e 20 20 54 68 65 20 73 6c 6f 74 5b 5d 20 61  y.  The slot[] a
2e5b9 72 72 61 79 20 67 72 6f 77 73 20 61 73 20 6e 65  rray grows as ne
2e5ba 65 64 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 0a  eded to contain.
2e5bb 2a 2a 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66 20  ** all terms of 
2e5bc 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2e5bd 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2e5be 20 77 68 65 72 65 53 70 6c 69 74 28 57 68 65 72   whereSplit(Wher
2e5bf 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 45 78  eClause *pWC, Ex
2e5c0 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 6f  pr *pExpr, int o
2e5c1 70 29 7b 0a 20 20 70 57 43 2d 3e 6f 70 20 3d 20  p){.  pWC->op = 
2e5c2 28 75 38 29 6f 70 3b 0a 20 20 69 66 28 20 70 45  (u8)op;.  if( pE
2e5c3 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  xpr==0 ) return;
2e5c4 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70  .  if( pExpr->op
2e5c5 21 3d 6f 70 20 29 7b 0a 20 20 20 20 77 68 65 72  !=op ){.    wher
2e5c6 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57  eClauseInsert(pW
2e5c7 43 2c 20 70 45 78 70 72 2c 20 30 29 3b 0a 20 20  C, pExpr, 0);.  
2e5c8 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68 65 72 65  }else{.    where
2e5c9 53 70 6c 69 74 28 70 57 43 2c 20 70 45 78 70 72  Split(pWC, pExpr
2e5ca 2d 3e 70 4c 65 66 74 2c 20 6f 70 29 3b 0a 20 20  ->pLeft, op);.  
2e5cb 20 20 77 68 65 72 65 53 70 6c 69 74 28 70 57 43    whereSplit(pWC
2e5cc 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  , pExpr->pRight,
2e5cd 20 6f 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   op);.  }.}../*.
2e5ce 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e  ** Initialize an
2e5cf 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 73 6b   expression mask
2e5d0 20 73 65 74 20 28 61 20 57 68 65 72 65 4d 61 73   set (a WhereMas
2e5d1 6b 53 65 74 20 6f 62 6a 65 63 74 29 0a 2a 2f 0a  kSet object).*/.
2e5d2 23 64 65 66 69 6e 65 20 69 6e 69 74 4d 61 73 6b  #define initMask
2e5d3 53 65 74 28 50 29 20 20 6d 65 6d 73 65 74 28 50  Set(P)  memset(P
2e5d4 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 50 29 29  , 0, sizeof(*P))
2e5d5 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
2e5d6 68 65 20 62 69 74 6d 61 73 6b 20 66 6f 72 20 74  he bitmask for t
2e5d7 68 65 20 67 69 76 65 6e 20 63 75 72 73 6f 72 20  he given cursor 
2e5d8 6e 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 20  number.  Return 
2e5d9 30 20 69 66 0a 2a 2a 20 69 43 75 72 73 6f 72 20  0 if.** iCursor 
2e5da 69 73 20 6e 6f 74 20 69 6e 20 74 68 65 20 73 65  is not in the se
2e5db 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 42 69 74  t..*/.static Bit
2e5dc 6d 61 73 6b 20 67 65 74 4d 61 73 6b 28 57 68 65  mask getMask(Whe
2e5dd 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b  reMaskSet *pMask
2e5de 53 65 74 2c 20 69 6e 74 20 69 43 75 72 73 6f 72  Set, int iCursor
2e5df 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73  ){.  int i;.  as
2e5e0 73 65 72 74 28 20 70 4d 61 73 6b 53 65 74 2d 3e  sert( pMaskSet->
2e5e1 6e 3c 3d 73 69 7a 65 6f 66 28 42 69 74 6d 61 73  n<=sizeof(Bitmas
2e5e2 6b 29 2a 38 20 29 3b 0a 20 20 66 6f 72 28 69 3d  k)*8 );.  for(i=
2e5e3 30 3b 20 69 3c 70 4d 61 73 6b 53 65 74 2d 3e 6e  0; i<pMaskSet->n
2e5e4 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
2e5e5 70 4d 61 73 6b 53 65 74 2d 3e 69 78 5b 69 5d 3d  pMaskSet->ix[i]=
2e5e6 3d 69 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20  =iCursor ){.    
2e5e7 20 20 72 65 74 75 72 6e 20 28 28 42 69 74 6d 61    return ((Bitma
2e5e8 73 6b 29 31 29 3c 3c 69 3b 0a 20 20 20 20 7d 0a  sk)1)<<i;.    }.
2e5e9 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
2e5ea 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  }../*.** Create 
2e5eb 61 20 6e 65 77 20 6d 61 73 6b 20 66 6f 72 20 63  a new mask for c
2e5ec 75 72 73 6f 72 20 69 43 75 72 73 6f 72 2e 0a 2a  ursor iCursor..*
2e5ed 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20 6f 6e  *.** There is on
2e5ee 65 20 63 75 72 73 6f 72 20 70 65 72 20 74 61 62  e cursor per tab
2e5ef 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  le in the FROM c
2e5f0 6c 61 75 73 65 2e 20 20 54 68 65 20 6e 75 6d 62  lause.  The numb
2e5f1 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73 20  er of.** tables 
2e5f2 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  in the FROM clau
2e5f3 73 65 20 69 73 20 6c 69 6d 69 74 65 64 20 62 79  se is limited by
2e5f4 20 61 20 74 65 73 74 20 65 61 72 6c 79 20 69 6e   a test early in
2e5f5 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 57   the.** sqlite3W
2e5f6 68 65 72 65 42 65 67 69 6e 28 29 20 72 6f 75 74  hereBegin() rout
2e5f7 69 6e 65 2e 20 20 53 6f 20 77 65 20 6b 6e 6f 77  ine.  So we know
2e5f8 20 74 68 61 74 20 74 68 65 20 70 4d 61 73 6b 53   that the pMaskS
2e5f9 65 74 2d 3e 69 78 5b 5d 0a 2a 2a 20 61 72 72 61  et->ix[].** arra
2e5fa 79 20 77 69 6c 6c 20 6e 65 76 65 72 20 6f 76 65  y will never ove
2e5fb 72 66 6c 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 63  rflow..*/.static
2e5fc 20 76 6f 69 64 20 63 72 65 61 74 65 4d 61 73 6b   void createMask
2e5fd 28 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70  (WhereMaskSet *p
2e5fe 4d 61 73 6b 53 65 74 2c 20 69 6e 74 20 69 43 75  MaskSet, int iCu
2e5ff 72 73 6f 72 29 7b 0a 20 20 61 73 73 65 72 74 28  rsor){.  assert(
2e600 20 70 4d 61 73 6b 53 65 74 2d 3e 6e 20 3c 20 41   pMaskSet->n < A
2e601 72 72 61 79 53 69 7a 65 28 70 4d 61 73 6b 53 65  rraySize(pMaskSe
2e602 74 2d 3e 69 78 29 20 29 3b 0a 20 20 70 4d 61 73  t->ix) );.  pMas
2e603 6b 53 65 74 2d 3e 69 78 5b 70 4d 61 73 6b 53 65  kSet->ix[pMaskSe
2e604 74 2d 3e 6e 2b 2b 5d 20 3d 20 69 43 75 72 73 6f  t->n++] = iCurso
2e605 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  r;.}../*.** This
2e606 20 72 6f 75 74 69 6e 65 20 77 61 6c 6b 73 20 28   routine walks (
2e607 72 65 63 75 72 73 69 76 65 6c 79 29 20 61 6e 20  recursively) an 
2e608 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20  expression tree 
2e609 61 6e 64 20 67 65 6e 65 72 61 74 65 73 0a 2a 2a  and generates.**
2e60a 20 61 20 62 69 74 6d 61 73 6b 20 69 6e 64 69 63   a bitmask indic
2e60b 61 74 69 6e 67 20 77 68 69 63 68 20 74 61 62 6c  ating which tabl
2e60c 65 73 20 61 72 65 20 75 73 65 64 20 69 6e 20 74  es are used in t
2e60d 68 61 74 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a  hat expression.*
2e60e 2a 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e  * tree..**.** In
2e60f 20 6f 72 64 65 72 20 66 6f 72 20 74 68 69 73 20   order for this 
2e610 72 6f 75 74 69 6e 65 20 74 6f 20 77 6f 72 6b 2c  routine to work,
2e611 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e   the calling fun
2e612 63 74 69 6f 6e 20 6d 75 73 74 20 68 61 76 65 0a  ction must have.
2e613 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e  ** previously in
2e614 76 6f 6b 65 64 20 73 71 6c 69 74 65 33 52 65 73  voked sqlite3Res
2e615 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20  olveExprNames() 
2e616 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  on the expressio
2e617 6e 2e 20 20 53 65 65 0a 2a 2a 20 74 68 65 20 68  n.  See.** the h
2e618 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e  eader comment on
2e619 20 74 68 61 74 20 72 6f 75 74 69 6e 65 20 66 6f   that routine fo
2e61a 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66  r additional inf
2e61b 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65  ormation..** The
2e61c 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
2e61d 78 70 72 4e 61 6d 65 73 28 29 20 72 6f 75 74 69  xprNames() routi
2e61e 6e 65 73 20 6c 6f 6f 6b 73 20 66 6f 72 20 63 6f  nes looks for co
2e61f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 6e 64 0a 2a  lumn names and.*
2e620 2a 20 73 65 74 73 20 74 68 65 69 72 20 6f 70 63  * sets their opc
2e621 6f 64 65 73 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d  odes to TK_COLUM
2e622 4e 20 61 6e 64 20 74 68 65 69 72 20 45 78 70 72  N and their Expr
2e623 2e 69 54 61 62 6c 65 20 66 69 65 6c 64 73 20 74  .iTable fields t
2e624 6f 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 63 75  o.** the VDBE cu
2e625 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 74  rsor number of t
2e626 68 65 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20  he table.  This 
2e627 72 6f 75 74 69 6e 65 20 6a 75 73 74 20 68 61 73  routine just has
2e628 20 74 6f 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 65   to.** translate
2e629 20 74 68 65 20 63 75 72 73 6f 72 20 6e 75 6d 62   the cursor numb
2e62a 65 72 73 20 69 6e 74 6f 20 62 69 74 6d 61 73 6b  ers into bitmask
2e62b 20 76 61 6c 75 65 73 20 61 6e 64 20 4f 52 20 61   values and OR a
2e62c 6c 6c 0a 2a 2a 20 74 68 65 20 62 69 74 6d 61 73  ll.** the bitmas
2e62d 6b 73 20 74 6f 67 65 74 68 65 72 2e 0a 2a 2f 0a  ks together..*/.
2e62e 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20 65  static Bitmask e
2e62f 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67  xprListTableUsag
2e630 65 28 57 68 65 72 65 4d 61 73 6b 53 65 74 2a 2c  e(WhereMaskSet*,
2e631 20 45 78 70 72 4c 69 73 74 2a 29 3b 0a 73 74 61   ExprList*);.sta
2e632 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72  tic Bitmask expr
2e633 53 65 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65  SelectTableUsage
2e634 28 57 68 65 72 65 4d 61 73 6b 53 65 74 2a 2c 20  (WhereMaskSet*, 
2e635 53 65 6c 65 63 74 2a 29 3b 0a 73 74 61 74 69 63  Select*);.static
2e636 20 42 69 74 6d 61 73 6b 20 65 78 70 72 54 61 62   Bitmask exprTab
2e637 6c 65 55 73 61 67 65 28 57 68 65 72 65 4d 61 73  leUsage(WhereMas
2e638 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20  kSet *pMaskSet, 
2e639 45 78 70 72 20 2a 70 29 7b 0a 20 20 42 69 74 6d  Expr *p){.  Bitm
2e63a 61 73 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a 20 20  ask mask = 0;.  
2e63b 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
2e63c 6e 20 30 3b 0a 20 20 69 66 28 20 70 2d 3e 6f 70  n 0;.  if( p->op
2e63d 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20  ==TK_COLUMN ){. 
2e63e 20 20 20 6d 61 73 6b 20 3d 20 67 65 74 4d 61 73     mask = getMas
2e63f 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 69  k(pMaskSet, p->i
2e640 54 61 62 6c 65 29 3b 0a 20 20 20 20 72 65 74 75  Table);.    retu
2e641 72 6e 20 6d 61 73 6b 3b 0a 20 20 7d 0a 20 20 6d  rn mask;.  }.  m
2e642 61 73 6b 20 3d 20 65 78 70 72 54 61 62 6c 65 55  ask = exprTableU
2e643 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70  sage(pMaskSet, p
2e644 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 6d 61 73  ->pRight);.  mas
2e645 6b 20 7c 3d 20 65 78 70 72 54 61 62 6c 65 55 73  k |= exprTableUs
2e646 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d  age(pMaskSet, p-
2e647 3e 70 4c 65 66 74 29 3b 0a 20 20 69 66 28 20 45  >pLeft);.  if( E
2e648 78 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70  xprHasProperty(p
2e649 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20  , EP_xIsSelect) 
2e64a 29 7b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20 65  ){.    mask |= e
2e64b 78 70 72 53 65 6c 65 63 74 54 61 62 6c 65 55 73  xprSelectTableUs
2e64c 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d  age(pMaskSet, p-
2e64d 3e 78 2e 70 53 65 6c 65 63 74 29 3b 0a 20 20 7d  >x.pSelect);.  }
2e64e 65 6c 73 65 7b 0a 20 20 20 20 6d 61 73 6b 20 7c  else{.    mask |
2e64f 3d 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55  = exprListTableU
2e650 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70  sage(pMaskSet, p
2e651 2d 3e 78 2e 70 4c 69 73 74 29 3b 0a 20 20 7d 0a  ->x.pList);.  }.
2e652 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a 7d    return mask;.}
2e653 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20  .static Bitmask 
2e654 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61  exprListTableUsa
2e655 67 65 28 57 68 65 72 65 4d 61 73 6b 53 65 74 20  ge(WhereMaskSet 
2e656 2a 70 4d 61 73 6b 53 65 74 2c 20 45 78 70 72 4c  *pMaskSet, ExprL
2e657 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69  ist *pList){.  i
2e658 6e 74 20 69 3b 0a 20 20 42 69 74 6d 61 73 6b 20  nt i;.  Bitmask 
2e659 6d 61 73 6b 20 3d 20 30 3b 0a 20 20 69 66 28 20  mask = 0;.  if( 
2e65a 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 66 6f 72  pList ){.    for
2e65b 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  (i=0; i<pList->n
2e65c 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  Expr; i++){.    
2e65d 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54 61    mask |= exprTa
2e65e 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65  bleUsage(pMaskSe
2e65f 74 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70  t, pList->a[i].p
2e660 45 78 70 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Expr);.    }.  }
2e661 0a 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b 0a  .  return mask;.
2e662 7d 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b  }.static Bitmask
2e663 20 65 78 70 72 53 65 6c 65 63 74 54 61 62 6c 65   exprSelectTable
2e664 55 73 61 67 65 28 57 68 65 72 65 4d 61 73 6b 53  Usage(WhereMaskS
2e665 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 53 65  et *pMaskSet, Se
2e666 6c 65 63 74 20 2a 70 53 29 7b 0a 20 20 42 69 74  lect *pS){.  Bit
2e667 6d 61 73 6b 20 6d 61 73 6b 20 3d 20 30 3b 0a 20  mask mask = 0;. 
2e668 20 77 68 69 6c 65 28 20 70 53 20 29 7b 0a 20 20   while( pS ){.  
2e669 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69    mask |= exprLi
2e66a 73 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61  stTableUsage(pMa
2e66b 73 6b 53 65 74 2c 20 70 53 2d 3e 70 45 4c 69 73  skSet, pS->pELis
2e66c 74 29 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20  t);.    mask |= 
2e66d 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61  exprListTableUsa
2e66e 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d  ge(pMaskSet, pS-
2e66f 3e 70 47 72 6f 75 70 42 79 29 3b 0a 20 20 20 20  >pGroupBy);.    
2e670 6d 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69 73 74  mask |= exprList
2e671 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b  TableUsage(pMask
2e672 53 65 74 2c 20 70 53 2d 3e 70 4f 72 64 65 72 42  Set, pS->pOrderB
2e673 79 29 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d 20  y);.    mask |= 
2e674 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70  exprTableUsage(p
2e675 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70 57 68  MaskSet, pS->pWh
2e676 65 72 65 29 3b 0a 20 20 20 20 6d 61 73 6b 20 7c  ere);.    mask |
2e677 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65  = exprTableUsage
2e678 28 70 4d 61 73 6b 53 65 74 2c 20 70 53 2d 3e 70  (pMaskSet, pS->p
2e679 48 61 76 69 6e 67 29 3b 0a 20 20 20 20 70 53 20  Having);.    pS 
2e67a 3d 20 70 53 2d 3e 70 50 72 69 6f 72 3b 0a 20 20  = pS->pPrior;.  
2e67b 7d 0a 20 20 72 65 74 75 72 6e 20 6d 61 73 6b 3b  }.  return mask;
2e67c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
2e67d 20 54 52 55 45 20 69 66 20 74 68 65 20 67 69 76   TRUE if the giv
2e67e 65 6e 20 6f 70 65 72 61 74 6f 72 20 69 73 20 6f  en operator is o
2e67f 6e 65 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74  ne of the operat
2e680 6f 72 73 20 74 68 61 74 20 69 73 0a 2a 2a 20 61  ors that is.** a
2e681 6c 6c 6f 77 65 64 20 66 6f 72 20 61 6e 20 69 6e  llowed for an in
2e682 64 65 78 61 62 6c 65 20 57 48 45 52 45 20 63 6c  dexable WHERE cl
2e683 61 75 73 65 20 74 65 72 6d 2e 20 20 54 68 65 20  ause term.  The 
2e684 61 6c 6c 6f 77 65 64 20 6f 70 65 72 61 74 6f 72  allowed operator
2e685 73 20 61 72 65 0a 2a 2a 20 22 3d 22 2c 20 22 3c  s are.** "=", "<
2e686 22 2c 20 22 3e 22 2c 20 22 3c 3d 22 2c 20 22 3e  ", ">", "<=", ">
2e687 3d 22 2c 20 61 6e 64 20 22 49 4e 22 2e 0a 2a 2f  =", and "IN"..*/
2e688 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f  .static int allo
2e689 77 65 64 4f 70 28 69 6e 74 20 6f 70 29 7b 0a 20  wedOp(int op){. 
2e68a 20 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3e 54   assert( TK_GT>T
2e68b 4b 5f 45 51 20 26 26 20 54 4b 5f 47 54 3c 54 4b  K_EQ && TK_GT<TK
2e68c 5f 47 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _GE );.  assert(
2e68d 20 54 4b 5f 4c 54 3e 54 4b 5f 45 51 20 26 26 20   TK_LT>TK_EQ && 
2e68e 54 4b 5f 4c 54 3c 54 4b 5f 47 45 20 29 3b 0a 20  TK_LT<TK_GE );. 
2e68f 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 3e 54   assert( TK_LE>T
2e690 4b 5f 45 51 20 26 26 20 54 4b 5f 4c 45 3c 54 4b  K_EQ && TK_LE<TK
2e691 5f 47 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _GE );.  assert(
2e692 20 54 4b 5f 47 45 3d 3d 54 4b 5f 45 51 2b 34 20   TK_GE==TK_EQ+4 
2e693 29 3b 0a 20 20 72 65 74 75 72 6e 20 6f 70 3d 3d  );.  return op==
2e694 54 4b 5f 49 4e 20 7c 7c 20 28 6f 70 3e 3d 54 4b  TK_IN || (op>=TK
2e695 5f 45 51 20 26 26 20 6f 70 3c 3d 54 4b 5f 47 45  _EQ && op<=TK_GE
2e696 29 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55  ) || op==TK_ISNU
2e697 4c 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 77 61  LL;.}../*.** Swa
2e698 70 20 74 77 6f 20 6f 62 6a 65 63 74 73 20 6f 66  p two objects of
2e699 20 74 79 70 65 20 54 59 50 45 2e 0a 2a 2f 0a 23   type TYPE..*/.#
2e69a 64 65 66 69 6e 65 20 53 57 41 50 28 54 59 50 45  define SWAP(TYPE
2e69b 2c 41 2c 42 29 20 7b 54 59 50 45 20 74 3d 41 3b  ,A,B) {TYPE t=A;
2e69c 20 41 3d 42 3b 20 42 3d 74 3b 7d 0a 0a 2f 2a 0a   A=B; B=t;}../*.
2e69d 2a 2a 20 43 6f 6d 6d 75 74 65 20 61 20 63 6f 6d  ** Commute a com
2e69e 70 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72  parison operator
2e69f 2e 20 20 45 78 70 72 65 73 73 69 6f 6e 73 20 6f  .  Expressions o
2e6a0 66 20 74 68 65 20 66 6f 72 6d 20 22 58 20 6f 70  f the form "X op
2e6a1 20 59 22 0a 2a 2a 20 61 72 65 20 63 6f 6e 76 65   Y".** are conve
2e6a2 72 74 65 64 20 69 6e 74 6f 20 22 59 20 6f 70 20  rted into "Y op 
2e6a3 58 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 63  X"..**.** If a c
2e6a4 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2e6a5 65 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20  e is associated 
2e6a6 77 69 74 68 20 65 69 74 68 65 72 20 74 68 65 20  with either the 
2e6a7 6c 65 66 74 20 6f 72 20 72 69 67 68 74 0a 2a 2a  left or right.**
2e6a8 20 73 69 64 65 20 6f 66 20 74 68 65 20 63 6f 6d   side of the com
2e6a9 70 61 72 69 73 6f 6e 2c 20 69 74 20 72 65 6d 61  parison, it rema
2e6aa 69 6e 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ins associated w
2e6ab 69 74 68 20 74 68 65 20 73 61 6d 65 20 73 69 64  ith the same sid
2e6ac 65 20 61 66 74 65 72 0a 2a 2a 20 74 68 65 20 63  e after.** the c
2e6ad 6f 6d 6d 75 74 61 74 69 6f 6e 2e 20 53 6f 20 22  ommutation. So "
2e6ae 59 20 63 6f 6c 6c 61 74 65 20 4e 4f 43 41 53 45  Y collate NOCASE
2e6af 20 6f 70 20 58 22 20 62 65 63 6f 6d 65 73 20 0a   op X" becomes .
2e6b0 2a 2a 20 22 58 20 63 6f 6c 6c 61 74 65 20 4e 4f  ** "X collate NO
2e6b1 43 41 53 45 20 6f 70 20 59 22 2e 20 54 68 69 73  CASE op Y". This
2e6b2 20 69 73 20 62 65 63 61 75 73 65 20 61 6e 79 20   is because any 
2e6b3 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
2e6b4 63 65 20 6f 6e 0a 2a 2a 20 74 68 65 20 6c 65 66  ce on.** the lef
2e6b5 74 20 68 61 6e 64 20 73 69 64 65 20 6f 66 20 61  t hand side of a
2e6b6 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 76 65 72   comparison over
2e6b7 72 69 64 65 73 20 61 6e 79 20 63 6f 6c 6c 61 74  rides any collat
2e6b8 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 0a 2a 2a  ion sequence .**
2e6b9 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 65   attached to the
2e6ba 20 72 69 67 68 74 2e 20 46 6f 72 20 74 68 65 20   right. For the 
2e6bb 73 61 6d 65 20 72 65 61 73 6f 6e 20 74 68 65 20  same reason the 
2e6bc 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 66 6c  EP_ExpCollate fl
2e6bd 61 67 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 6d  ag.** is not com
2e6be 6d 75 74 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  muted..*/.static
2e6bf 20 76 6f 69 64 20 65 78 70 72 43 6f 6d 6d 75 74   void exprCommut
2e6c0 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  e(Parse *pParse,
2e6c1 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20   Expr *pExpr){. 
2e6c2 20 75 31 36 20 65 78 70 52 69 67 68 74 20 3d 20   u16 expRight = 
2e6c3 28 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e  (pExpr->pRight->
2e6c4 66 6c 61 67 73 20 26 20 45 50 5f 45 78 70 43 6f  flags & EP_ExpCo
2e6c5 6c 6c 61 74 65 29 3b 0a 20 20 75 31 36 20 65 78  llate);.  u16 ex
2e6c6 70 4c 65 66 74 20 3d 20 28 70 45 78 70 72 2d 3e  pLeft = (pExpr->
2e6c7 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20 45  pLeft->flags & E
2e6c8 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29 3b 0a 20  P_ExpCollate);. 
2e6c9 20 61 73 73 65 72 74 28 20 61 6c 6c 6f 77 65 64   assert( allowed
2e6ca 4f 70 28 70 45 78 70 72 2d 3e 6f 70 29 20 26 26  Op(pExpr->op) &&
2e6cb 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 49   pExpr->op!=TK_I
2e6cc 4e 20 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 52  N );.  pExpr->pR
2e6cd 69 67 68 74 2d 3e 70 43 6f 6c 6c 20 3d 20 73 71  ight->pColl = sq
2e6ce 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
2e6cf 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
2e6d0 70 52 69 67 68 74 29 3b 0a 20 20 70 45 78 70 72  pRight);.  pExpr
2e6d1 2d 3e 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20 3d  ->pLeft->pColl =
2e6d2 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c   sqlite3ExprColl
2e6d3 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 78 70  Seq(pParse, pExp
2e6d4 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 53 57 41  r->pLeft);.  SWA
2e6d5 50 28 43 6f 6c 6c 53 65 71 2a 2c 70 45 78 70 72  P(CollSeq*,pExpr
2e6d6 2d 3e 70 52 69 67 68 74 2d 3e 70 43 6f 6c 6c 2c  ->pRight->pColl,
2e6d7 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 70 43  pExpr->pLeft->pC
2e6d8 6f 6c 6c 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70  oll);.  pExpr->p
2e6d9 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 3d 20 28  Right->flags = (
2e6da 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 66  pExpr->pRight->f
2e6db 6c 61 67 73 20 26 20 7e 45 50 5f 45 78 70 43 6f  lags & ~EP_ExpCo
2e6dc 6c 6c 61 74 65 29 20 7c 20 65 78 70 4c 65 66 74  llate) | expLeft
2e6dd 3b 0a 20 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  ;.  pExpr->pLeft
2e6de 2d 3e 66 6c 61 67 73 20 3d 20 28 70 45 78 70 72  ->flags = (pExpr
2e6df 2d 3e 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26  ->pLeft->flags &
2e6e0 20 7e 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 29   ~EP_ExpCollate)
2e6e1 20 7c 20 65 78 70 52 69 67 68 74 3b 0a 20 20 53   | expRight;.  S
2e6e2 57 41 50 28 45 78 70 72 2a 2c 70 45 78 70 72 2d  WAP(Expr*,pExpr-
2e6e3 3e 70 52 69 67 68 74 2c 70 45 78 70 72 2d 3e 70  >pRight,pExpr->p
2e6e4 4c 65 66 74 29 3b 0a 20 20 69 66 28 20 70 45 78  Left);.  if( pEx
2e6e5 70 72 2d 3e 6f 70 3e 3d 54 4b 5f 47 54 20 29 7b  pr->op>=TK_GT ){
2e6e6 0a 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f  .    assert( TK_
2e6e7 4c 54 3d 3d 54 4b 5f 47 54 2b 32 20 29 3b 0a 20  LT==TK_GT+2 );. 
2e6e8 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47 45     assert( TK_GE
2e6e9 3d 3d 54 4b 5f 4c 45 2b 32 20 29 3b 0a 20 20 20  ==TK_LE+2 );.   
2e6ea 20 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3e 54   assert( TK_GT>T
2e6eb 4b 5f 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65  K_EQ );.    asse
2e6ec 72 74 28 20 54 4b 5f 47 54 3c 54 4b 5f 4c 45 20  rt( TK_GT<TK_LE 
2e6ed 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
2e6ee 45 78 70 72 2d 3e 6f 70 3e 3d 54 4b 5f 47 54 20  Expr->op>=TK_GT 
2e6ef 26 26 20 70 45 78 70 72 2d 3e 6f 70 3c 3d 54 4b  && pExpr->op<=TK
2e6f0 5f 47 45 20 29 3b 0a 20 20 20 20 70 45 78 70 72  _GE );.    pExpr
2e6f1 2d 3e 6f 70 20 3d 20 28 28 70 45 78 70 72 2d 3e  ->op = ((pExpr->
2e6f2 6f 70 2d 54 4b 5f 47 54 29 5e 32 29 2b 54 4b 5f  op-TK_GT)^2)+TK_
2e6f3 47 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  GT;.  }.}../*.**
2e6f4 20 54 72 61 6e 73 6c 61 74 65 20 66 72 6f 6d 20   Translate from 
2e6f5 54 4b 5f 78 78 20 6f 70 65 72 61 74 6f 72 20 74  TK_xx operator t
2e6f6 6f 20 57 4f 5f 78 78 20 62 69 74 6d 61 73 6b 2e  o WO_xx bitmask.
2e6f7 0a 2a 2f 0a 73 74 61 74 69 63 20 75 31 36 20 6f  .*/.static u16 o
2e6f8 70 65 72 61 74 6f 72 4d 61 73 6b 28 69 6e 74 20  peratorMask(int 
2e6f9 6f 70 29 7b 0a 20 20 75 31 36 20 63 3b 0a 20 20  op){.  u16 c;.  
2e6fa 61 73 73 65 72 74 28 20 61 6c 6c 6f 77 65 64 4f  assert( allowedO
2e6fb 70 28 6f 70 29 20 29 3b 0a 20 20 69 66 28 20 6f  p(op) );.  if( o
2e6fc 70 3d 3d 54 4b 5f 49 4e 20 29 7b 0a 20 20 20 20  p==TK_IN ){.    
2e6fd 63 20 3d 20 57 4f 5f 49 4e 3b 0a 20 20 7d 65 6c  c = WO_IN;.  }el
2e6fe 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 53  se if( op==TK_IS
2e6ff 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 63 20 3d 20  NULL ){.    c = 
2e700 57 4f 5f 49 53 4e 55 4c 4c 3b 0a 20 20 7d 65 6c  WO_ISNULL;.  }el
2e701 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
2e702 28 57 4f 5f 45 51 3c 3c 28 6f 70 2d 54 4b 5f 45  (WO_EQ<<(op-TK_E
2e703 51 29 29 20 3c 20 30 78 37 66 66 66 20 29 3b 0a  Q)) < 0x7fff );.
2e704 20 20 20 20 63 20 3d 20 28 75 31 36 29 28 57 4f      c = (u16)(WO
2e705 5f 45 51 3c 3c 28 6f 70 2d 54 4b 5f 45 51 29 29  _EQ<<(op-TK_EQ))
2e706 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
2e707 6f 70 21 3d 54 4b 5f 49 53 4e 55 4c 4c 20 7c 7c  op!=TK_ISNULL ||
2e708 20 63 3d 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b   c==WO_ISNULL );
2e709 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54  .  assert( op!=T
2e70a 4b 5f 49 4e 20 7c 7c 20 63 3d 3d 57 4f 5f 49 4e  K_IN || c==WO_IN
2e70b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70   );.  assert( op
2e70c 21 3d 54 4b 5f 45 51 20 7c 7c 20 63 3d 3d 57 4f  !=TK_EQ || c==WO
2e70d 5f 45 51 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _EQ );.  assert(
2e70e 20 6f 70 21 3d 54 4b 5f 4c 54 20 7c 7c 20 63 3d   op!=TK_LT || c=
2e70f 3d 57 4f 5f 4c 54 20 29 3b 0a 20 20 61 73 73 65  =WO_LT );.  asse
2e710 72 74 28 20 6f 70 21 3d 54 4b 5f 4c 45 20 7c 7c  rt( op!=TK_LE ||
2e711 20 63 3d 3d 57 4f 5f 4c 45 20 29 3b 0a 20 20 61   c==WO_LE );.  a
2e712 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 47 54  ssert( op!=TK_GT
2e713 20 7c 7c 20 63 3d 3d 57 4f 5f 47 54 20 29 3b 0a   || c==WO_GT );.
2e714 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b    assert( op!=TK
2e715 5f 47 45 20 7c 7c 20 63 3d 3d 57 4f 5f 47 45 20  _GE || c==WO_GE 
2e716 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d  );.  return c;.}
2e717 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 20 66  ../*.** Search f
2e718 6f 72 20 61 20 74 65 72 6d 20 69 6e 20 74 68 65  or a term in the
2e719 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 68   WHERE clause th
2e71a 61 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72  at is of the for
2e71b 6d 20 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e  m "X <op> <expr>
2e71c 22 0a 2a 2a 20 77 68 65 72 65 20 58 20 69 73 20  ".** where X is 
2e71d 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74  a reference to t
2e71e 68 65 20 69 43 6f 6c 75 6d 6e 20 6f 66 20 74 61  he iColumn of ta
2e71f 62 6c 65 20 69 43 75 72 20 61 6e 64 20 3c 6f 70  ble iCur and <op
2e720 3e 20 69 73 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74  > is one of.** t
2e721 68 65 20 57 4f 5f 78 78 20 6f 70 65 72 61 74 6f  he WO_xx operato
2e722 72 20 63 6f 64 65 73 20 73 70 65 63 69 66 69 65  r codes specifie
2e723 64 20 62 79 20 74 68 65 20 6f 70 20 70 61 72 61  d by the op para
2e724 6d 65 74 65 72 2e 0a 2a 2a 20 52 65 74 75 72 6e  meter..** Return
2e725 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
2e726 65 20 74 65 72 6d 2e 20 20 52 65 74 75 72 6e 20  e term.  Return 
2e727 30 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a  0 if not found..
2e728 2a 2f 0a 73 74 61 74 69 63 20 57 68 65 72 65 54  */.static WhereT
2e729 65 72 6d 20 2a 66 69 6e 64 54 65 72 6d 28 0a 20  erm *findTerm(. 
2e72a 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57   WhereClause *pW
2e72b 43 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48  C,     /* The WH
2e72c 45 52 45 20 63 6c 61 75 73 65 20 74 6f 20 62 65  ERE clause to be
2e72d 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 20 69   searched */.  i
2e72e 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 20  nt iCur,        
2e72f 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e       /* Cursor n
2e730 75 6d 62 65 72 20 6f 66 20 4c 48 53 20 2a 2f 0a  umber of LHS */.
2e731 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20    int iColumn,  
2e732 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d          /* Colum
2e733 6e 20 6e 75 6d 62 65 72 20 6f 66 20 4c 48 53 20  n number of LHS 
2e734 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74  */.  Bitmask not
2e735 52 65 61 64 79 2c 20 20 20 20 20 2f 2a 20 52 48  Ready,     /* RH
2e736 53 20 6d 75 73 74 20 6e 6f 74 20 6f 76 65 72 6c  S must not overl
2e737 61 70 20 77 69 74 68 20 74 68 69 73 20 6d 61 73  ap with this mas
2e738 6b 20 2a 2f 0a 20 20 75 33 32 20 6f 70 2c 20 20  k */.  u32 op,  
2e739 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2e73a 4d 61 73 6b 20 6f 66 20 57 4f 5f 78 78 20 76 61  Mask of WO_xx va
2e73b 6c 75 65 73 20 64 65 73 63 72 69 62 69 6e 67 20  lues describing 
2e73c 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 49 6e  operator */.  In
2e73d 64 65 78 20 2a 70 49 64 78 20 20 20 20 20 20 20  dex *pIdx       
2e73e 20 20 20 20 2f 2a 20 4d 75 73 74 20 62 65 20 63      /* Must be c
2e73f 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68 20 74  ompatible with t
2e740 68 69 73 20 69 6e 64 65 78 2c 20 69 66 20 6e 6f  his index, if no
2e741 74 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a 20 20 57  t NULL */.){.  W
2e742 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b  hereTerm *pTerm;
2e743 0a 20 20 69 6e 74 20 6b 3b 0a 20 20 61 73 73 65  .  int k;.  asse
2e744 72 74 28 20 69 43 75 72 3e 3d 30 20 29 3b 0a 20  rt( iCur>=0 );. 
2e745 20 6f 70 20 26 3d 20 57 4f 5f 41 4c 4c 3b 0a 20   op &= WO_ALL;. 
2e746 20 66 6f 72 28 70 54 65 72 6d 3d 70 57 43 2d 3e   for(pTerm=pWC->
2e747 61 2c 20 6b 3d 70 57 43 2d 3e 6e 54 65 72 6d 3b  a, k=pWC->nTerm;
2e748 20 6b 3b 20 6b 2d 2d 2c 20 70 54 65 72 6d 2b 2b   k; k--, pTerm++
2e749 29 7b 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d  ){.    if( pTerm
2e74a 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43  ->leftCursor==iC
2e74b 75 72 0a 20 20 20 20 20 20 20 26 26 20 28 70 54  ur.       && (pT
2e74c 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74  erm->prereqRight
2e74d 20 26 20 6e 6f 74 52 65 61 64 79 29 3d 3d 30 0a   & notReady)==0.
2e74e 20 20 20 20 20 20 20 26 26 20 70 54 65 72 6d 2d         && pTerm-
2e74f 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 3d 3d 69  >u.leftColumn==i
2e750 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 20 26 26  Column.       &&
2e751 20 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74   (pTerm->eOperat
2e752 6f 72 20 26 20 6f 70 29 21 3d 30 0a 20 20 20 20  or & op)!=0.    
2e753 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49 64  ){.      if( pId
2e754 78 20 26 26 20 70 54 65 72 6d 2d 3e 65 4f 70 65  x && pTerm->eOpe
2e755 72 61 74 6f 72 21 3d 57 4f 5f 49 53 4e 55 4c 4c  rator!=WO_ISNULL
2e756 20 29 7b 0a 20 20 20 20 20 20 20 20 45 78 70 72   ){.        Expr
2e757 20 2a 70 58 20 3d 20 70 54 65 72 6d 2d 3e 70 45   *pX = pTerm->pE
2e758 78 70 72 3b 0a 20 20 20 20 20 20 20 20 43 6f 6c  xpr;.        Col
2e759 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20  lSeq *pColl;.   
2e75a 20 20 20 20 20 63 68 61 72 20 69 64 78 61 66 66       char idxaff
2e75b 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a 3b  ;.        int j;
2e75c 0a 20 20 20 20 20 20 20 20 50 61 72 73 65 20 2a  .        Parse *
2e75d 70 50 61 72 73 65 20 3d 20 70 57 43 2d 3e 70 50  pParse = pWC->pP
2e75e 61 72 73 65 3b 0a 0a 20 20 20 20 20 20 20 20 69  arse;..        i
2e75f 64 78 61 66 66 20 3d 20 70 49 64 78 2d 3e 70 54  dxaff = pIdx->pT
2e760 61 62 6c 65 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 75  able->aCol[iColu
2e761 6d 6e 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20 20  mn].affinity;.  
2e762 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74        if( !sqlit
2e763 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f  e3IndexAffinityO
2e764 6b 28 70 58 2c 20 69 64 78 61 66 66 29 20 29 20  k(pX, idxaff) ) 
2e765 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20 20  continue;..     
2e766 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74     /* Figure out
2e767 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   the collation s
2e768 65 71 75 65 6e 63 65 20 72 65 71 75 69 72 65 64  equence required
2e769 20 66 72 6f 6d 20 61 6e 20 69 6e 64 65 78 20 66   from an index f
2e76a 6f 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 74  or.        ** it
2e76b 20 74 6f 20 62 65 20 75 73 65 66 75 6c 20 66 6f   to be useful fo
2e76c 72 20 6f 70 74 69 6d 69 73 69 6e 67 20 65 78 70  r optimising exp
2e76d 72 65 73 73 69 6f 6e 20 70 58 2e 20 53 74 6f 72  ression pX. Stor
2e76e 65 20 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a  e this.        *
2e76f 2a 20 76 61 6c 75 65 20 69 6e 20 76 61 72 69 61  * value in varia
2e770 62 6c 65 20 70 43 6f 6c 6c 2e 0a 20 20 20 20 20  ble pColl..     
2e771 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73     */.        as
2e772 73 65 72 74 28 70 58 2d 3e 70 4c 65 66 74 29 3b  sert(pX->pLeft);
2e773 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d  .        pColl =
2e774 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f   sqlite3BinaryCo
2e775 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61  mpareCollSeq(pPa
2e776 72 73 65 2c 20 70 58 2d 3e 70 4c 65 66 74 2c 20  rse, pX->pLeft, 
2e777 70 58 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20  pX->pRight);.   
2e778 20 20 20 20 20 61 73 73 65 72 74 28 70 43 6f 6c       assert(pCol
2e779 6c 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72  l || pParse->nEr
2e77a 72 29 3b 0a 0a 20 20 20 20 20 20 20 20 66 6f 72  r);..        for
2e77b 28 6a 3d 30 3b 20 70 49 64 78 2d 3e 61 69 43 6f  (j=0; pIdx->aiCo
2e77c 6c 75 6d 6e 5b 6a 5d 21 3d 69 43 6f 6c 75 6d 6e  lumn[j]!=iColumn
2e77d 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
2e77e 20 20 69 66 28 20 4e 45 56 45 52 28 6a 3e 3d 70    if( NEVER(j>=p
2e77f 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 20 29 20  Idx->nColumn) ) 
2e780 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20  return 0;.      
2e781 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
2e782 70 43 6f 6c 6c 20 26 26 20 73 71 6c 69 74 65 33  pColl && sqlite3
2e783 53 74 72 49 43 6d 70 28 70 43 6f 6c 6c 2d 3e 7a  StrICmp(pColl->z
2e784 4e 61 6d 65 2c 20 70 49 64 78 2d 3e 61 7a 43 6f  Name, pIdx->azCo
2e785 6c 6c 5b 6a 5d 29 20 29 20 63 6f 6e 74 69 6e 75  ll[j]) ) continu
2e786 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  e;.      }.     
2e787 20 72 65 74 75 72 6e 20 70 54 65 72 6d 3b 0a 20   return pTerm;. 
2e788 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
2e789 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61  n 0;.}../* Forwa
2e78a 72 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a  rd reference */.
2e78b 73 74 61 74 69 63 20 76 6f 69 64 20 65 78 70 72  static void expr
2e78c 41 6e 61 6c 79 7a 65 28 53 72 63 4c 69 73 74 2a  Analyze(SrcList*
2e78d 2c 20 57 68 65 72 65 43 6c 61 75 73 65 2a 2c 20  , WhereClause*, 
2e78e 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c  int);../*.** Cal
2e78f 6c 20 65 78 70 72 41 6e 61 6c 79 7a 65 20 6f 6e  l exprAnalyze on
2e790 20 61 6c 6c 20 74 65 72 6d 73 20 69 6e 20 61 20   all terms in a 
2e791 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 0a  WHERE clause.  .
2e792 2a 2a 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69 63 20  **.**.*/.static 
2e793 76 6f 69 64 20 65 78 70 72 41 6e 61 6c 79 7a 65  void exprAnalyze
2e794 41 6c 6c 28 0a 20 20 53 72 63 4c 69 73 74 20 2a  All(.  SrcList *
2e795 70 54 61 62 4c 69 73 74 2c 20 20 20 20 20 20 20  pTabList,       
2e796 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  /* the FROM clau
2e797 73 65 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61  se */.  WhereCla
2e798 75 73 65 20 2a 70 57 43 20 20 20 20 20 20 20 20  use *pWC        
2e799 20 2f 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c   /* the WHERE cl
2e79a 61 75 73 65 20 74 6f 20 62 65 20 61 6e 61 6c 79  ause to be analy
2e79b 7a 65 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  zed */.){.  int 
2e79c 69 3b 0a 20 20 66 6f 72 28 69 3d 70 57 43 2d 3e  i;.  for(i=pWC->
2e79d 6e 54 65 72 6d 2d 31 3b 20 69 3e 3d 30 3b 20 69  nTerm-1; i>=0; i
2e79e 2d 2d 29 7b 0a 20 20 20 20 65 78 70 72 41 6e 61  --){.    exprAna
2e79f 6c 79 7a 65 28 70 54 61 62 4c 69 73 74 2c 20 70  lyze(pTabList, p
2e7a0 57 43 2c 20 69 29 3b 0a 20 20 7d 0a 7d 0a 0a 23  WC, i);.  }.}..#
2e7a1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2e7a2 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49 5a 41  IT_LIKE_OPTIMIZA
2e7a3 54 49 4f 4e 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b  TION./*.** Check
2e7a4 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 67   to see if the g
2e7a5 69 76 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  iven expression 
2e7a6 69 73 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c 4f  is a LIKE or GLO
2e7a7 42 20 6f 70 65 72 61 74 6f 72 20 74 68 61 74 0a  B operator that.
2e7a8 2a 2a 20 63 61 6e 20 62 65 20 6f 70 74 69 6d 69  ** can be optimi
2e7a9 7a 65 64 20 75 73 69 6e 67 20 69 6e 65 71 75 61  zed using inequa
2e7aa 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73  lity constraints
2e7ab 2e 20 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  .  Return TRUE i
2e7ac 66 20 69 74 20 69 73 0a 2a 2a 20 73 6f 20 61 6e  f it is.** so an
2e7ad 64 20 66 61 6c 73 65 20 69 66 20 6e 6f 74 2e 0a  d false if not..
2e7ae 2a 2a 0a 2a 2a 20 49 6e 20 6f 72 64 65 72 20 66  **.** In order f
2e7af 6f 72 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20  or the operator 
2e7b0 74 6f 20 62 65 20 6f 70 74 69 6d 69 7a 69 62 6c  to be optimizibl
2e7b1 65 2c 20 74 68 65 20 52 48 53 20 6d 75 73 74 20  e, the RHS must 
2e7b2 62 65 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20 6c  be a string.** l
2e7b3 69 74 65 72 61 6c 20 74 68 61 74 20 64 6f 65 73  iteral that does
2e7b4 20 6e 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20   not begin with 
2e7b5 61 20 77 69 6c 64 63 61 72 64 2e 20 20 0a 2a 2f  a wildcard.  .*/
2e7b6 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 4c 69  .static int isLi
2e7b7 6b 65 4f 72 47 6c 6f 62 28 0a 20 20 50 61 72 73  keOrGlob(.  Pars
2e7b8 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a  e *pParse,    /*
2e7b9 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64   Parsing and cod
2e7ba 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e  e generating con
2e7bb 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a  text */.  Expr *
2e7bc 70 45 78 70 72 2c 20 20 20 20 20 20 2f 2a 20 54  pExpr,      /* T
2e7bd 65 73 74 20 74 68 69 73 20 65 78 70 72 65 73 73  est this express
2e7be 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 2a  ion */.  Expr **
2e7bf 70 70 50 72 65 66 69 78 2c 20 20 2f 2a 20 50 6f  ppPrefix,  /* Po
2e7c0 69 6e 74 65 72 20 74 6f 20 54 4b 5f 53 54 52 49  inter to TK_STRI
2e7c1 4e 47 20 65 78 70 72 65 73 73 69 6f 6e 20 77 69  NG expression wi
2e7c2 74 68 20 70 61 74 74 65 72 6e 20 70 72 65 66 69  th pattern prefi
2e7c3 78 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 73 43  x */.  int *pisC
2e7c4 6f 6d 70 6c 65 74 65 2c 20 2f 2a 20 54 72 75 65  omplete, /* True
2e7c5 20 69 66 20 74 68 65 20 6f 6e 6c 79 20 77 69 6c   if the only wil
2e7c6 64 63 61 72 64 20 69 73 20 25 20 69 6e 20 74 68  dcard is % in th
2e7c7 65 20 6c 61 73 74 20 63 68 61 72 61 63 74 65 72  e last character
2e7c8 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 6f 43 61   */.  int *pnoCa
2e7c9 73 65 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  se      /* True 
2e7ca 69 66 20 75 70 70 65 72 63 61 73 65 20 69 73 20  if uppercase is 
2e7cb 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 6c 6f  equivalent to lo
2e7cc 77 65 72 63 61 73 65 20 2a 2f 0a 29 7b 0a 20 20  wercase */.){.  
2e7cd 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20  const char *z = 
2e7ce 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74  0;         /* St
2e7cf 72 69 6e 67 20 6f 6e 20 52 48 53 20 6f 66 20 4c  ring on RHS of L
2e7d0 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a  IKE operator */.
2e7d1 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20    Expr *pRight, 
2e7d2 2a 70 4c 65 66 74 3b 20 20 20 20 20 20 2f 2a 20  *pLeft;      /* 
2e7d3 52 69 67 68 74 20 61 6e 64 20 6c 65 66 74 20 73  Right and left s
2e7d4 69 7a 65 20 6f 66 20 4c 49 4b 45 20 6f 70 65 72  ize of LIKE oper
2e7d5 61 74 6f 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69  ator */.  ExprLi
2e7d6 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 20 20 20  st *pList;      
2e7d7 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
2e7d8 6f 70 65 72 61 6e 64 73 20 74 6f 20 74 68 65 20  operands to the 
2e7d9 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 2a 2f  LIKE operator */
2e7da 0a 20 20 69 6e 74 20 63 3b 20 20 20 20 20 20 20  .  int c;       
2e7db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e7dc 20 4f 6e 65 20 63 68 61 72 61 63 74 65 72 20 69   One character i
2e7dd 6e 20 7a 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 63  n z[] */.  int c
2e7de 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  nt;             
2e7df 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
2e7e0 6f 66 20 6e 6f 6e 2d 77 69 6c 64 63 61 72 64 20  of non-wildcard 
2e7e1 70 72 65 66 69 78 20 63 68 61 72 61 63 74 65 72  prefix character
2e7e2 73 20 2a 2f 0a 20 20 63 68 61 72 20 77 63 5b 33  s */.  char wc[3
2e7e3 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ];              
2e7e4 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 20 63 68    /* Wildcard ch
2e7e5 61 72 61 63 74 65 72 73 20 2a 2f 0a 20 20 43 6f  aracters */.  Co
2e7e6 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20  llSeq *pColl;   
2e7e7 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c           /* Coll
2e7e8 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66  ating sequence f
2e7e9 6f 72 20 4c 48 53 20 2a 2f 0a 20 20 73 71 6c 69  or LHS */.  sqli
2e7ea 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
2e7eb 2d 3e 64 62 3b 20 20 2f 2a 20 44 61 74 61 62 61  ->db;  /* Databa
2e7ec 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
2e7ed 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  .  sqlite3_value
2e7ee 20 2a 70 56 61 6c 20 3d 20 30 3b 0a 20 20 69 6e   *pVal = 0;.  in
2e7ef 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20  t op;           
2e7f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70 63 6f           /* Opco
2e7f1 64 65 20 6f 66 20 70 52 69 67 68 74 20 2a 2f 0a  de of pRight */.
2e7f2 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49  .  if( !sqlite3I
2e7f3 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28 64 62  sLikeFunction(db
2e7f4 2c 20 70 45 78 70 72 2c 20 70 6e 6f 43 61 73 65  , pExpr, pnoCase
2e7f5 2c 20 77 63 29 20 29 7b 0a 20 20 20 20 72 65 74  , wc) ){.    ret
2e7f6 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 69 66 64 65  urn 0;.  }.#ifde
2e7f7 66 20 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a  f SQLITE_EBCDIC.
2e7f8 20 20 69 66 28 20 2a 70 6e 6f 43 61 73 65 20 29    if( *pnoCase )
2e7f9 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69   return 0;.#endi
2e7fa 66 0a 20 20 70 4c 69 73 74 20 3d 20 70 45 78 70  f.  pList = pExp
2e7fb 72 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 70 4c  r->x.pList;.  pL
2e7fc 65 66 74 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 31  eft = pList->a[1
2e7fd 5d 2e 70 45 78 70 72 3b 0a 20 20 69 66 28 20 70  ].pExpr;.  if( p
2e7fe 4c 65 66 74 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c  Left->op!=TK_COL
2e7ff 55 4d 4e 20 7c 7c 20 73 71 6c 69 74 65 33 45 78  UMN || sqlite3Ex
2e800 70 72 41 66 66 69 6e 69 74 79 28 70 4c 65 66 74  prAffinity(pLeft
2e801 29 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45  )!=SQLITE_AFF_TE
2e802 58 54 20 29 7b 0a 20 20 20 20 2f 2a 20 49 4d 50  XT ){.    /* IMP
2e803 3a 20 52 2d 30 32 30 36 35 2d 34 39 34 36 35 20  : R-02065-49465 
2e804 54 68 65 20 6c 65 66 74 2d 68 61 6e 64 20 73 69  The left-hand si
2e805 64 65 20 6f 66 20 74 68 65 20 4c 49 4b 45 20 6f  de of the LIKE o
2e806 72 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 20  r GLOB operator 
2e807 6d 75 73 74 0a 20 20 20 20 2a 2a 20 62 65 20 74  must.    ** be t
2e808 68 65 20 6e 61 6d 65 20 6f 66 20 61 6e 20 69 6e  he name of an in
2e809 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20 77 69 74  dexed column wit
2e80a 68 20 54 45 58 54 20 61 66 66 69 6e 69 74 79 2e  h TEXT affinity.
2e80b 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 30   */.    return 0
2e80c 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
2e80d 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 21 3d  pLeft->iColumn!=
2e80e 28 2d 31 29 20 29 3b 20 2f 2a 20 42 65 63 61 75  (-1) ); /* Becau
2e80f 73 65 20 49 50 4b 20 6e 65 76 65 72 20 68 61 73  se IPK never has
2e810 20 41 46 46 5f 54 45 58 54 20 2a 2f 0a 20 20 70   AFF_TEXT */.  p
2e811 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78  Coll = sqlite3Ex
2e812 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  prCollSeq(pParse
2e813 2c 20 70 4c 65 66 74 29 3b 0a 20 20 61 73 73 65  , pLeft);.  asse
2e814 72 74 28 20 70 43 6f 6c 6c 21 3d 30 20 29 3b 20  rt( pColl!=0 ); 
2e815 20 2f 2a 20 45 76 65 72 79 20 6e 6f 6e 2d 49 50   /* Every non-IP
2e816 4b 20 63 6f 6c 75 6d 6e 20 68 61 73 20 61 20 63  K column has a c
2e817 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
2e818 65 20 2a 2f 0a 20 20 69 66 28 20 28 70 43 6f 6c  e */.  if( (pCol
2e819 6c 2d 3e 74 79 70 65 21 3d 53 51 4c 49 54 45 5f  l->type!=SQLITE_
2e81a 43 4f 4c 4c 5f 42 49 4e 41 52 59 20 7c 7c 20 2a  COLL_BINARY || *
2e81b 70 6e 6f 43 61 73 65 29 20 26 26 0a 20 20 20 20  pnoCase) &&.    
2e81c 20 20 28 70 43 6f 6c 6c 2d 3e 74 79 70 65 21 3d    (pColl->type!=
2e81d 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41  SQLITE_COLL_NOCA
2e81e 53 45 20 7c 7c 20 21 2a 70 6e 6f 43 61 73 65 29  SE || !*pnoCase)
2e81f 20 29 7b 0a 20 20 20 20 2f 2a 20 49 4d 50 3a 20   ){.    /* IMP: 
2e820 52 2d 30 39 30 30 33 2d 33 32 30 34 36 20 46 6f  R-09003-32046 Fo
2e821 72 20 74 68 65 20 47 4c 4f 42 20 6f 70 65 72 61  r the GLOB opera
2e822 74 6f 72 2c 20 74 68 65 20 63 6f 6c 75 6d 6e 20  tor, the column 
2e823 6d 75 73 74 20 75 73 65 20 74 68 65 0a 20 20 20  must use the.   
2e824 20 2a 2a 20 64 65 66 61 75 6c 74 20 42 49 4e 41   ** default BINA
2e825 52 59 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  RY collating seq
2e826 75 65 6e 63 65 2e 0a 20 20 20 20 2a 2a 20 49 4d  uence..    ** IM
2e827 50 3a 20 52 2d 34 31 34 30 38 2d 32 38 33 30 36  P: R-41408-28306
2e828 20 46 6f 72 20 74 68 65 20 4c 49 4b 45 20 6f 70   For the LIKE op
2e829 65 72 61 74 6f 72 2c 20 69 66 20 63 61 73 65 5f  erator, if case_
2e82a 73 65 6e 73 69 74 69 76 65 5f 6c 69 6b 65 20 6d  sensitive_like m
2e82b 6f 64 65 0a 20 20 20 20 2a 2a 20 69 73 20 65 6e  ode.    ** is en
2e82c 61 62 6c 65 64 20 74 68 65 6e 20 74 68 65 20 63  abled then the c
2e82d 6f 6c 75 6d 6e 20 6d 75 73 74 20 75 73 65 20 74  olumn must use t
2e82e 68 65 20 64 65 66 61 75 6c 74 20 42 49 4e 41 52  he default BINAR
2e82f 59 20 63 6f 6c 6c 61 74 69 6e 67 0a 20 20 20 20  Y collating.    
2e830 2a 2a 20 73 65 71 75 65 6e 63 65 2c 20 6f 72 20  ** sequence, or 
2e831 69 66 20 63 61 73 65 5f 73 65 6e 73 69 74 69 76  if case_sensitiv
2e832 65 5f 6c 69 6b 65 20 6d 6f 64 65 20 69 73 20 64  e_like mode is d
2e833 69 73 61 62 6c 65 64 20 74 68 65 6e 20 74 68 65  isabled then the
2e834 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a 20 6d   column.    ** m
2e835 75 73 74 20 75 73 65 20 74 68 65 20 62 75 69 6c  ust use the buil
2e836 74 2d 69 6e 20 4e 4f 43 41 53 45 20 63 6f 6c 6c  t-in NOCASE coll
2e837 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 0a  ating sequence..
2e838 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 74 75 72      */.    retur
2e839 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 52 69 67  n 0;.  }..  pRig
2e83a 68 74 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 30 5d  ht = pList->a[0]
2e83b 2e 70 45 78 70 72 3b 0a 20 20 6f 70 20 3d 20 70  .pExpr;.  op = p
2e83c 52 69 67 68 74 2d 3e 6f 70 3b 0a 20 20 69 66 28  Right->op;.  if(
2e83d 20 6f 70 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52   op==TK_REGISTER
2e83e 20 29 7b 0a 20 20 20 20 6f 70 20 3d 20 70 52 69   ){.    op = pRi
2e83f 67 68 74 2d 3e 6f 70 32 3b 0a 20 20 7d 0a 20 20  ght->op2;.  }.  
2e840 69 66 28 20 6f 70 3d 3d 54 4b 5f 56 41 52 49 41  if( op==TK_VARIA
2e841 42 4c 45 20 29 7b 0a 20 20 20 20 56 64 62 65 20  BLE ){.    Vdbe 
2e842 2a 70 52 65 70 72 65 70 61 72 65 20 3d 20 70 50  *pReprepare = pP
2e843 61 72 73 65 2d 3e 70 52 65 70 72 65 70 61 72 65  arse->pReprepare
2e844 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 73 71 6c  ;.    pVal = sql
2e845 69 74 65 33 56 64 62 65 47 65 74 56 61 6c 75 65  ite3VdbeGetValue
2e846 28 70 52 65 70 72 65 70 61 72 65 2c 20 70 52 69  (pReprepare, pRi
2e847 67 68 74 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 53 51  ght->iColumn, SQ
2e848 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 29 3b 0a  LITE_AFF_NONE);.
2e849 20 20 20 20 69 66 28 20 70 56 61 6c 20 26 26 20      if( pVal && 
2e84a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
2e84b 70 65 28 70 56 61 6c 29 3d 3d 53 51 4c 49 54 45  pe(pVal)==SQLITE
2e84c 5f 54 45 58 54 20 29 7b 0a 20 20 20 20 20 20 7a  _TEXT ){.      z
2e84d 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74   = (char *)sqlit
2e84e 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 70 56  e3_value_text(pV
2e84f 61 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  al);.    }.    s
2e850 71 6c 69 74 65 33 56 64 62 65 53 65 74 56 61 72  qlite3VdbeSetVar
2e851 6d 61 73 6b 28 70 50 61 72 73 65 2d 3e 70 56 64  mask(pParse->pVd
2e852 62 65 2c 20 70 52 69 67 68 74 2d 3e 69 43 6f 6c  be, pRight->iCol
2e853 75 6d 6e 29 3b 0a 20 20 20 20 61 73 73 65 72 74  umn);.    assert
2e854 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54 4b  ( pRight->op==TK
2e855 5f 56 41 52 49 41 42 4c 45 20 7c 7c 20 70 52 69  _VARIABLE || pRi
2e856 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49  ght->op==TK_REGI
2e857 53 54 45 52 20 29 3b 0a 20 20 7d 65 6c 73 65 20  STER );.  }else 
2e858 69 66 28 20 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e  if( op==TK_STRIN
2e859 47 20 29 7b 0a 20 20 20 20 7a 20 3d 20 70 52 69  G ){.    z = pRi
2e85a 67 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 3b 0a 20  ght->u.zToken;. 
2e85b 20 7d 0a 20 20 69 66 28 20 7a 20 29 7b 0a 20 20   }.  if( z ){.  
2e85c 20 20 63 6e 74 20 3d 20 30 3b 0a 20 20 20 20 77    cnt = 0;.    w
2e85d 68 69 6c 65 28 20 28 63 3d 7a 5b 63 6e 74 5d 29  hile( (c=z[cnt])
2e85e 21 3d 30 20 26 26 20 63 21 3d 77 63 5b 30 5d 20  !=0 && c!=wc[0] 
2e85f 26 26 20 63 21 3d 77 63 5b 31 5d 20 26 26 20 63  && c!=wc[1] && c
2e860 21 3d 77 63 5b 32 5d 20 29 7b 0a 20 20 20 20 20  !=wc[2] ){.     
2e861 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20   cnt++;.    }.  
2e862 20 20 69 66 28 20 63 6e 74 21 3d 30 20 26 26 20    if( cnt!=0 && 
2e863 63 21 3d 30 20 26 26 20 32 35 35 21 3d 28 75 38  c!=0 && 255!=(u8
2e864 29 7a 5b 63 6e 74 2d 31 5d 20 29 7b 0a 20 20 20  )z[cnt-1] ){.   
2e865 20 20 20 45 78 70 72 20 2a 70 50 72 65 66 69 78     Expr *pPrefix
2e866 3b 0a 20 20 20 20 20 20 2a 70 69 73 43 6f 6d 70  ;.      *pisComp
2e867 6c 65 74 65 20 3d 20 7a 5b 63 6e 74 5d 3d 3d 77  lete = z[cnt]==w
2e868 63 5b 30 5d 20 26 26 20 7a 5b 63 6e 74 2b 31 5d  c[0] && z[cnt+1]
2e869 3d 3d 30 3b 0a 20 20 20 20 20 20 70 50 72 65 66  ==0;.      pPref
2e86a 69 78 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ix = sqlite3Expr
2e86b 28 64 62 2c 20 54 4b 5f 53 54 52 49 4e 47 2c 20  (db, TK_STRING, 
2e86c 7a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50  z);.      if( pP
2e86d 72 65 66 69 78 20 29 20 70 50 72 65 66 69 78 2d  refix ) pPrefix-
2e86e 3e 75 2e 7a 54 6f 6b 65 6e 5b 63 6e 74 5d 20 3d  >u.zToken[cnt] =
2e86f 20 30 3b 0a 20 20 20 20 20 20 2a 70 70 50 72 65   0;.      *ppPre
2e870 66 69 78 20 3d 20 70 50 72 65 66 69 78 3b 0a 20  fix = pPrefix;. 
2e871 20 20 20 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f       if( op==TK_
2e872 56 41 52 49 41 42 4c 45 20 29 7b 0a 20 20 20 20  VARIABLE ){.    
2e873 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50      Vdbe *v = pP
2e874 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 20  arse->pVdbe;.   
2e875 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2e876 53 65 74 56 61 72 6d 61 73 6b 28 76 2c 20 70 52  SetVarmask(v, pR
2e877 69 67 68 74 2d 3e 69 43 6f 6c 75 6d 6e 29 3b 0a  ight->iColumn);.
2e878 20 20 20 20 20 20 20 20 69 66 28 20 2a 70 69 73          if( *pis
2e879 43 6f 6d 70 6c 65 74 65 20 26 26 20 70 52 69 67  Complete && pRig
2e87a 68 74 2d 3e 75 2e 7a 54 6f 6b 65 6e 5b 31 5d 20  ht->u.zToken[1] 
2e87b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
2e87c 49 66 20 74 68 65 20 72 68 73 20 6f 66 20 74 68  If the rhs of th
2e87d 65 20 4c 49 4b 45 20 65 78 70 72 65 73 73 69 6f  e LIKE expressio
2e87e 6e 20 69 73 20 61 20 76 61 72 69 61 62 6c 65 2c  n is a variable,
2e87f 20 61 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74   and the current
2e880 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 76 61  .          ** va
2e881 6c 75 65 20 6f 66 20 74 68 65 20 76 61 72 69 61  lue of the varia
2e882 62 6c 65 20 6d 65 61 6e 73 20 74 68 65 72 65 20  ble means there 
2e883 69 73 20 6e 6f 20 6e 65 65 64 20 74 6f 20 69 6e  is no need to in
2e884 76 6f 6b 65 20 74 68 65 20 4c 49 4b 45 0a 20 20  voke the LIKE.  
2e885 20 20 20 20 20 20 20 20 2a 2a 20 66 75 6e 63 74          ** funct
2e886 69 6f 6e 2c 20 74 68 65 6e 20 6e 6f 20 4f 50 5f  ion, then no OP_
2e887 56 61 72 69 61 62 6c 65 20 77 69 6c 6c 20 62 65  Variable will be
2e888 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 70 72   added to the pr
2e889 6f 67 72 61 6d 2e 0a 20 20 20 20 20 20 20 20 20  ogram..         
2e88a 20 2a 2a 20 54 68 69 73 20 63 61 75 73 65 73 20   ** This causes 
2e88b 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 74 68 65  problems for the
2e88c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61   sqlite3_bind_pa
2e88d 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 0a 20  rameter_name(). 
2e88e 20 20 20 20 20 20 20 20 20 2a 2a 20 41 50 49 2e           ** API.
2e88f 20 54 6f 20 77 6f 72 6b 61 72 6f 75 6e 64 20 74   To workaround t
2e890 68 65 6d 2c 20 61 64 64 20 61 20 64 75 6d 6d 79  hem, add a dummy
2e891 20 4f 50 5f 56 61 72 69 61 62 6c 65 20 68 65 72   OP_Variable her
2e892 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 20  e..          */ 
2e893 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 72  .          int r
2e894 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  1 = sqlite3GetTe
2e895 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
2e896 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
2e897 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70  ExprCodeTarget(p
2e898 50 61 72 73 65 2c 20 70 52 69 67 68 74 2c 20 72  Parse, pRight, r
2e899 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  1);.          sq
2e89a 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
2e89b 33 28 76 2c 20 73 71 6c 69 74 65 33 56 64 62 65  3(v, sqlite3Vdbe
2e89c 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 2d 31  CurrentAddr(v)-1
2e89d 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20  , 0);.          
2e89e 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
2e89f 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
2e8a0 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
2e8a1 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
2e8a2 20 20 20 20 20 20 7a 20 3d 20 30 3b 0a 20 20 20        z = 0;.   
2e8a3 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65   }.  }..  sqlite
2e8a4 33 56 61 6c 75 65 46 72 65 65 28 70 56 61 6c 29  3ValueFree(pVal)
2e8a5 3b 0a 20 20 72 65 74 75 72 6e 20 28 7a 21 3d 30  ;.  return (z!=0
2e8a6 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
2e8a7 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f  QLITE_OMIT_LIKE_
2e8a8 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a  OPTIMIZATION */.
2e8a9 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2e8aa 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2e8ab 4c 45 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74  LE./*.** Check t
2e8ac 6f 20 73 65 65 20 69 66 20 74 68 65 20 67 69 76  o see if the giv
2e8ad 65 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  en expression is
2e8ae 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a   of the form.**.
2e8af 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d  **         colum
2e8b0 6e 20 4d 41 54 43 48 20 65 78 70 72 0a 2a 2a 0a  n MATCH expr.**.
2e8b1 2a 2a 20 49 66 20 69 74 20 69 73 20 74 68 65 6e  ** If it is then
2e8b2 20 72 65 74 75 72 6e 20 54 52 55 45 2e 20 20 49   return TRUE.  I
2e8b3 66 20 6e 6f 74 2c 20 72 65 74 75 72 6e 20 46 41  f not, return FA
2e8b4 4c 53 45 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  LSE..*/.static i
2e8b5 6e 74 20 69 73 4d 61 74 63 68 4f 66 43 6f 6c 75  nt isMatchOfColu
2e8b6 6d 6e 28 0a 20 20 45 78 70 72 20 2a 70 45 78 70  mn(.  Expr *pExp
2e8b7 72 20 20 20 20 20 20 2f 2a 20 54 65 73 74 20 74  r      /* Test t
2e8b8 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  his expression *
2e8b9 2f 0a 29 7b 0a 20 20 45 78 70 72 4c 69 73 74 20  /.){.  ExprList 
2e8ba 2a 70 4c 69 73 74 3b 0a 0a 20 20 69 66 28 20 70  *pList;..  if( p
2e8bb 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46 55 4e  Expr->op!=TK_FUN
2e8bc 43 54 49 4f 4e 20 29 7b 0a 20 20 20 20 72 65 74  CTION ){.    ret
2e8bd 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  urn 0;.  }.  if(
2e8be 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
2e8bf 70 45 78 70 72 2d 3e 75 2e 7a 54 6f 6b 65 6e 2c  pExpr->u.zToken,
2e8c0 22 6d 61 74 63 68 22 29 21 3d 30 20 29 7b 0a 20  "match")!=0 ){. 
2e8c1 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
2e8c2 0a 20 20 70 4c 69 73 74 20 3d 20 70 45 78 70 72  .  pList = pExpr
2e8c3 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 69 66 28  ->x.pList;.  if(
2e8c4 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 32   pList->nExpr!=2
2e8c5 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
2e8c6 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c 69 73  ;.  }.  if( pLis
2e8c7 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 2d 3e 6f  t->a[1].pExpr->o
2e8c8 70 20 21 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 20 29  p != TK_COLUMN )
2e8c9 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
2e8ca 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a    }.  return 1;.
2e8cb 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
2e8cc 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
2e8cd 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49  ABLE */../*.** I
2e8ce 66 20 74 68 65 20 70 42 61 73 65 20 65 78 70 72  f the pBase expr
2e8cf 65 73 73 69 6f 6e 20 6f 72 69 67 69 6e 61 74 65  ession originate
2e8d0 64 20 69 6e 20 74 68 65 20 4f 4e 20 6f 72 20 55  d in the ON or U
2e8d1 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 0a 2a  SING clause of.*
2e8d2 2a 20 61 20 6a 6f 69 6e 2c 20 74 68 65 6e 20 74  * a join, then t
2e8d3 72 61 6e 73 66 65 72 20 74 68 65 20 61 70 70 72  ransfer the appr
2e8d4 6f 70 72 69 61 74 65 20 6d 61 72 6b 69 6e 67 73  opriate markings
2e8d5 20 6f 76 65 72 20 74 6f 20 64 65 72 69 76 65 64   over to derived
2e8d6 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2e8d7 20 74 72 61 6e 73 66 65 72 4a 6f 69 6e 4d 61 72   transferJoinMar
2e8d8 6b 69 6e 67 73 28 45 78 70 72 20 2a 70 44 65 72  kings(Expr *pDer
2e8d9 69 76 65 64 2c 20 45 78 70 72 20 2a 70 42 61 73  ived, Expr *pBas
2e8da 65 29 7b 0a 20 20 70 44 65 72 69 76 65 64 2d 3e  e){.  pDerived->
2e8db 66 6c 61 67 73 20 7c 3d 20 70 42 61 73 65 2d 3e  flags |= pBase->
2e8dc 66 6c 61 67 73 20 26 20 45 50 5f 46 72 6f 6d 4a  flags & EP_FromJ
2e8dd 6f 69 6e 3b 0a 20 20 70 44 65 72 69 76 65 64 2d  oin;.  pDerived-
2e8de 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65  >iRightJoinTable
2e8df 20 3d 20 70 42 61 73 65 2d 3e 69 52 69 67 68 74   = pBase->iRight
2e8e0 4a 6f 69 6e 54 61 62 6c 65 3b 0a 7d 0a 0a 23 69  JoinTable;.}..#i
2e8e1 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
2e8e2 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49  E_OMIT_OR_OPTIMI
2e8e3 5a 41 54 49 4f 4e 29 20 26 26 20 21 64 65 66 69  ZATION) && !defi
2e8e4 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
2e8e5 53 55 42 51 55 45 52 59 29 0a 2f 2a 0a 2a 2a 20  SUBQUERY)./*.** 
2e8e6 41 6e 61 6c 79 7a 65 20 61 20 74 65 72 6d 20 74  Analyze a term t
2e8e7 68 61 74 20 63 6f 6e 73 69 73 74 73 20 6f 66 20  hat consists of 
2e8e8 74 77 6f 20 6f 72 20 6d 6f 72 65 20 4f 52 2d 63  two or more OR-c
2e8e9 6f 6e 6e 65 63 74 65 64 0a 2a 2a 20 73 75 62 74  onnected.** subt
2e8ea 65 72 6d 73 2e 20 20 53 6f 20 69 6e 3a 0a 2a 2a  erms.  So in:.**
2e8eb 0a 2a 2a 20 20 20 20 20 2e 2e 2e 20 57 48 45 52  .**     ... WHER
2e8ec 45 20 20 28 61 3d 35 29 20 41 4e 44 20 28 62 3d  E  (a=5) AND (b=
2e8ed 37 20 4f 52 20 63 3d 39 20 4f 52 20 64 3d 31 33  7 OR c=9 OR d=13
2e8ee 29 20 41 4e 44 20 28 64 3d 31 33 29 0a 2a 2a 20  ) AND (d=13).** 
2e8ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e8f0 20 20 20 20 20 20 20 20 20 5e 5e 5e 5e 5e 5e 5e           ^^^^^^^
2e8f1 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 5e 0a 2a 2a  ^^^^^^^^^^^^^.**
2e8f2 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2e8f3 20 61 6e 61 6c 79 7a 65 73 20 74 65 72 6d 73 20   analyzes terms 
2e8f4 73 75 63 68 20 61 73 20 74 68 65 20 6d 69 64 64  such as the midd
2e8f5 6c 65 20 74 65 72 6d 20 69 6e 20 74 68 65 20 61  le term in the a
2e8f6 62 6f 76 65 20 65 78 61 6d 70 6c 65 2e 0a 2a 2a  bove example..**
2e8f7 20 41 20 57 68 65 72 65 4f 72 54 65 72 6d 20 6f   A WhereOrTerm o
2e8f8 62 6a 65 63 74 20 69 73 20 63 6f 6d 70 75 74 65  bject is compute
2e8f9 64 20 61 6e 64 20 61 74 74 61 63 68 65 64 20 74  d and attached t
2e8fa 6f 20 74 68 65 20 74 65 72 6d 20 75 6e 64 65 72  o the term under
2e8fb 0a 2a 2a 20 61 6e 61 6c 79 73 69 73 2c 20 72 65  .** analysis, re
2e8fc 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20  gardless of the 
2e8fd 6f 75 74 63 6f 6d 65 20 6f 66 20 74 68 65 20 61  outcome of the a
2e8fe 6e 61 6c 79 73 69 73 2e 20 20 48 65 6e 63 65 3a  nalysis.  Hence:
2e8ff 0a 2a 2a 0a 2a 2a 20 20 20 20 20 57 68 65 72 65  .**.**     Where
2e900 54 65 72 6d 2e 77 74 46 6c 61 67 73 20 20 20 7c  Term.wtFlags   |
2e901 3d 20 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 0a 2a  =  TERM_ORINFO.*
2e902 2a 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 2e  *     WhereTerm.
2e903 75 2e 70 4f 72 49 6e 66 6f 20 20 3d 20 20 61 20  u.pOrInfo  =  a 
2e904 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f  dynamically allo
2e905 63 61 74 65 64 20 57 68 65 72 65 4f 72 54 65 72  cated WhereOrTer
2e906 6d 20 6f 62 6a 65 63 74 0a 2a 2a 0a 2a 2a 20 54  m object.**.** T
2e907 68 65 20 74 65 72 6d 20 62 65 69 6e 67 20 61 6e  he term being an
2e908 61 6c 79 7a 65 64 20 6d 75 73 74 20 68 61 76 65  alyzed must have
2e909 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 6f 66 20   two or more of 
2e90a 4f 52 2d 63 6f 6e 6e 65 63 74 65 64 20 73 75 62  OR-connected sub
2e90b 74 65 72 6d 73 2e 0a 2a 2a 20 41 20 73 69 6e 67  terms..** A sing
2e90c 6c 65 20 73 75 62 74 65 72 6d 20 6d 69 67 68 74  le subterm might
2e90d 20 62 65 20 61 20 73 65 74 20 6f 66 20 41 4e 44   be a set of AND
2e90e 2d 63 6f 6e 6e 65 63 74 65 64 20 73 75 62 2d 73  -connected sub-s
2e90f 75 62 74 65 72 6d 73 2e 0a 2a 2a 20 45 78 61 6d  ubterms..** Exam
2e910 70 6c 65 73 20 6f 66 20 74 65 72 6d 73 20 75 6e  ples of terms un
2e911 64 65 72 20 61 6e 61 6c 79 73 69 73 3a 0a 2a 2a  der analysis:.**
2e912 0a 2a 2a 20 20 20 20 20 28 41 29 20 20 20 20 20  .**     (A)     
2e913 74 31 2e 78 3d 74 32 2e 79 20 4f 52 20 74 31 2e  t1.x=t2.y OR t1.
2e914 78 3d 74 32 2e 7a 20 4f 52 20 74 31 2e 79 3d 31  x=t2.z OR t1.y=1
2e915 35 20 4f 52 20 74 31 2e 7a 3d 74 33 2e 61 2b 35  5 OR t1.z=t3.a+5
2e916 0a 2a 2a 20 20 20 20 20 28 42 29 20 20 20 20 20  .**     (B)     
2e917 78 3d 65 78 70 72 31 20 4f 52 20 65 78 70 72 32  x=expr1 OR expr2
2e918 3d 78 20 4f 52 20 78 3d 65 78 70 72 33 0a 2a 2a  =x OR x=expr3.**
2e919 20 20 20 20 20 28 43 29 20 20 20 20 20 74 31 2e       (C)     t1.
2e91a 78 3d 74 32 2e 79 20 4f 52 20 28 74 31 2e 78 3d  x=t2.y OR (t1.x=
2e91b 74 32 2e 7a 20 41 4e 44 20 74 31 2e 79 3d 31 35  t2.z AND t1.y=15
2e91c 29 0a 2a 2a 20 20 20 20 20 28 44 29 20 20 20 20  ).**     (D)    
2e91d 20 78 3d 65 78 70 72 31 20 4f 52 20 28 79 3e 31   x=expr1 OR (y>1
2e91e 31 20 41 4e 44 20 79 3c 32 32 20 41 4e 44 20 7a  1 AND y<22 AND z
2e91f 20 4c 49 4b 45 20 27 2a 68 65 6c 6c 6f 2a 27 29   LIKE '*hello*')
2e920 0a 2a 2a 20 20 20 20 20 28 45 29 20 20 20 20 20  .**     (E)     
2e921 28 70 2e 61 3d 31 20 41 4e 44 20 71 2e 62 3d 32  (p.a=1 AND q.b=2
2e922 20 41 4e 44 20 72 2e 63 3d 33 29 20 4f 52 20 28   AND r.c=3) OR (
2e923 70 2e 78 3d 34 20 41 4e 44 20 71 2e 79 3d 35 20  p.x=4 AND q.y=5 
2e924 41 4e 44 20 72 2e 7a 3d 36 29 0a 2a 2a 0a 2a 2a  AND r.z=6).**.**
2e925 20 43 41 53 45 20 31 3a 0a 2a 2a 0a 2a 2a 20 49   CASE 1:.**.** I
2e926 66 20 61 6c 6c 20 73 75 62 74 65 72 6d 73 20 61  f all subterms a
2e927 72 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 54  re of the form T
2e928 2e 43 3d 65 78 70 72 20 66 6f 72 20 73 6f 6d 65  .C=expr for some
2e929 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e 20 6f   single column o
2e92a 66 20 43 0a 2a 2a 20 61 20 73 69 6e 67 6c 65 20  f C.** a single 
2e92b 74 61 62 6c 65 20 54 20 28 61 73 20 73 68 6f 77  table T (as show
2e92c 6e 20 69 6e 20 65 78 61 6d 70 6c 65 20 42 20 61  n in example B a
2e92d 62 6f 76 65 29 20 74 68 65 6e 20 63 72 65 61 74  bove) then creat
2e92e 65 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 0a  e a new virtual.
2e92f 2a 2a 20 74 65 72 6d 20 74 68 61 74 20 69 73 20  ** term that is 
2e930 61 6e 20 65 71 75 69 76 61 6c 65 6e 74 20 49 4e  an equivalent IN
2e931 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 49 6e   expression.  In
2e932 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66   other words, if
2e933 20 74 68 65 20 74 65 72 6d 0a 2a 2a 20 62 65 69   the term.** bei
2e934 6e 67 20 61 6e 61 6c 79 7a 65 64 20 69 73 3a 0a  ng analyzed is:.
2e935 2a 2a 0a 2a 2a 20 20 20 20 20 20 78 20 3d 20 65  **.**      x = e
2e936 78 70 72 31 20 20 4f 52 20 20 65 78 70 72 32 20  xpr1  OR  expr2 
2e937 3d 20 78 20 20 4f 52 20 20 78 20 3d 20 65 78 70  = x  OR  x = exp
2e938 72 33 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 63 72  r3.**.** then cr
2e939 65 61 74 65 20 61 20 6e 65 77 20 76 69 72 74 75  eate a new virtu
2e93a 61 6c 20 74 65 72 6d 20 6c 69 6b 65 20 74 68 69  al term like thi
2e93b 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 78 20  s:.**.**      x 
2e93c 49 4e 20 28 65 78 70 72 31 2c 65 78 70 72 32 2c  IN (expr1,expr2,
2e93d 65 78 70 72 33 29 0a 2a 2a 0a 2a 2a 20 43 41 53  expr3).**.** CAS
2e93e 45 20 32 3a 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6c  E 2:.**.** If al
2e93f 6c 20 73 75 62 74 65 72 6d 73 20 61 72 65 20 69  l subterms are i
2e940 6e 64 65 78 61 62 6c 65 20 62 79 20 61 20 73 69  ndexable by a si
2e941 6e 67 6c 65 20 74 61 62 6c 65 20 54 2c 20 74 68  ngle table T, th
2e942 65 6e 20 73 65 74 0a 2a 2a 0a 2a 2a 20 20 20 20  en set.**.**    
2e943 20 57 68 65 72 65 54 65 72 6d 2e 65 4f 70 65 72   WhereTerm.eOper
2e944 61 74 6f 72 20 20 20 20 20 20 20 20 20 20 20 20  ator            
2e945 20 20 3d 20 20 57 4f 5f 4f 52 0a 2a 2a 20 20 20    =  WO_OR.**   
2e946 20 20 57 68 65 72 65 54 65 72 6d 2e 75 2e 70 4f    WhereTerm.u.pO
2e947 72 49 6e 66 6f 2d 3e 69 6e 64 65 78 61 62 6c 65  rInfo->indexable
2e948 20 20 7c 3d 20 20 74 68 65 20 63 75 72 73 6f 72    |=  the cursor
2e949 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 61 62 6c   number for tabl
2e94a 65 20 54 0a 2a 2a 0a 2a 2a 20 41 20 73 75 62 74  e T.**.** A subt
2e94b 65 72 6d 20 69 73 20 22 69 6e 64 65 78 61 62 6c  erm is "indexabl
2e94c 65 22 20 69 66 20 69 74 20 69 73 20 6f 66 20 74  e" if it is of t
2e94d 68 65 20 66 6f 72 6d 0a 2a 2a 20 22 54 2e 43 20  he form.** "T.C 
2e94e 3c 6f 70 3e 20 3c 65 78 70 72 3e 22 20 77 68 65  <op> <expr>" whe
2e94f 72 65 20 43 20 69 73 20 61 6e 79 20 63 6f 6c 75  re C is any colu
2e950 6d 6e 20 6f 66 20 74 61 62 6c 65 20 54 20 61 6e  mn of table T an
2e951 64 20 0a 2a 2a 20 3c 6f 70 3e 20 69 73 20 6f 6e  d .** <op> is on
2e952 65 20 6f 66 20 22 3d 22 2c 20 22 3c 22 2c 20 22  e of "=", "<", "
2e953 3c 3d 22 2c 20 22 3e 22 2c 20 22 3e 3d 22 2c 20  <=", ">", ">=", 
2e954 22 49 53 20 4e 55 4c 4c 22 2c 20 6f 72 20 22 49  "IS NULL", or "I
2e955 4e 22 2e 0a 2a 2a 20 41 20 73 75 62 74 65 72 6d  N"..** A subterm
2e956 20 69 73 20 61 6c 73 6f 20 69 6e 64 65 78 61 62   is also indexab
2e957 6c 65 20 69 66 20 69 74 20 69 73 20 61 6e 20 41  le if it is an A
2e958 4e 44 20 6f 66 20 74 77 6f 20 6f 72 20 6d 6f 72  ND of two or mor
2e959 65 0a 2a 2a 20 73 75 62 73 75 62 74 65 72 6d 73  e.** subsubterms
2e95a 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 66   at least one of
2e95b 20 77 68 69 63 68 20 69 73 20 69 6e 64 65 78 61   which is indexa
2e95c 62 6c 65 2e 20 20 49 6e 64 65 78 61 62 6c 65 20  ble.  Indexable 
2e95d 41 4e 44 20 0a 2a 2a 20 73 75 62 74 65 72 6d 73  AND .** subterms
2e95e 20 68 61 76 65 20 74 68 65 69 72 20 65 4f 70 65   have their eOpe
2e95f 72 61 74 6f 72 20 73 65 74 20 74 6f 20 57 4f 5f  rator set to WO_
2e960 41 4e 44 20 61 6e 64 20 74 68 65 79 20 68 61 76  AND and they hav
2e961 65 0a 2a 2a 20 75 2e 70 41 6e 64 49 6e 66 6f 20  e.** u.pAndInfo 
2e962 73 65 74 20 74 6f 20 61 20 64 79 6e 61 6d 69 63  set to a dynamic
2e963 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 57  ally allocated W
2e964 68 65 72 65 41 6e 64 54 65 72 6d 20 6f 62 6a 65  hereAndTerm obje
2e965 63 74 2e 0a 2a 2a 0a 2a 2a 20 46 72 6f 6d 20 61  ct..**.** From a
2e966 6e 6f 74 68 65 72 20 70 6f 69 6e 74 20 6f 66 20  nother point of 
2e967 76 69 65 77 2c 20 22 69 6e 64 65 78 61 62 6c 65  view, "indexable
2e968 22 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  " means that the
2e969 20 73 75 62 74 65 72 6d 20 63 6f 75 6c 64 0a 2a   subterm could.*
2e96a 2a 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 65  * potentially be
2e96b 20 75 73 65 64 20 77 69 74 68 20 61 6e 20 69 6e   used with an in
2e96c 64 65 78 20 69 66 20 61 6e 20 61 70 70 72 6f 70  dex if an approp
2e96d 72 69 61 74 65 20 69 6e 64 65 78 20 65 78 69 73  riate index exis
2e96e 74 73 2e 0a 2a 2a 20 54 68 69 73 20 61 6e 61 6c  ts..** This anal
2e96f 79 73 69 73 20 64 6f 65 73 20 6e 6f 74 20 63 6f  ysis does not co
2e970 6e 73 69 64 65 72 20 77 68 65 74 68 65 72 20 6f  nsider whether o
2e971 72 20 6e 6f 74 20 74 68 65 20 69 6e 64 65 78 20  r not the index 
2e972 65 78 69 73 74 73 3b 20 74 68 61 74 0a 2a 2a 20  exists; that.** 
2e973 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 74 68 65  is something the
2e974 20 62 65 73 74 49 6e 64 65 78 28 29 20 72 6f 75   bestIndex() rou
2e975 74 69 6e 65 20 77 69 6c 6c 20 64 65 74 65 72 6d  tine will determ
2e976 69 6e 65 2e 20 20 54 68 69 73 20 61 6e 61 6c 79  ine.  This analy
2e977 73 69 73 0a 2a 2a 20 6f 6e 6c 79 20 6c 6f 6f 6b  sis.** only look
2e978 73 20 61 74 20 77 68 65 74 68 65 72 20 73 75 62  s at whether sub
2e979 74 65 72 6d 73 20 61 70 70 72 6f 70 72 69 61 74  terms appropriat
2e97a 65 20 66 6f 72 20 69 6e 64 65 78 69 6e 67 20 65  e for indexing e
2e97b 78 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20  xist..**.** All 
2e97c 65 78 61 6d 70 6c 65 73 20 41 20 74 68 72 6f 75  examples A throu
2e97d 67 68 20 45 20 61 62 6f 76 65 20 61 6c 6c 20 73  gh E above all s
2e97e 61 74 69 73 66 79 20 63 61 73 65 20 32 2e 20 20  atisfy case 2.  
2e97f 42 75 74 20 69 66 20 61 20 74 65 72 6d 0a 2a 2a  But if a term.**
2e980 20 61 6c 73 6f 20 73 74 61 74 69 73 66 69 65 73   also statisfies
2e981 20 63 61 73 65 20 31 20 28 73 75 63 68 20 61 73   case 1 (such as
2e982 20 42 29 20 77 65 20 6b 6e 6f 77 20 74 68 61 74   B) we know that
2e983 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 72 20 77   the optimizer w
2e984 69 6c 6c 0a 2a 2a 20 61 6c 77 61 79 73 20 70 72  ill.** always pr
2e985 65 66 65 72 20 63 61 73 65 20 31 2c 20 73 6f 20  efer case 1, so 
2e986 69 6e 20 74 68 61 74 20 63 61 73 65 20 77 65 20  in that case we 
2e987 70 72 65 74 65 6e 64 20 74 68 61 74 20 63 61 73  pretend that cas
2e988 65 20 32 20 69 73 20 6e 6f 74 0a 2a 2a 20 73 61  e 2 is not.** sa
2e989 74 69 73 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  tisfied..**.** I
2e98a 74 20 6d 69 67 68 74 20 62 65 20 74 68 65 20 63  t might be the c
2e98b 61 73 65 20 74 68 61 74 20 6d 75 6c 74 69 70 6c  ase that multipl
2e98c 65 20 74 61 62 6c 65 73 20 61 72 65 20 69 6e 64  e tables are ind
2e98d 65 78 61 62 6c 65 2e 20 20 46 6f 72 20 65 78 61  exable.  For exa
2e98e 6d 70 6c 65 2c 0a 2a 2a 20 28 45 29 20 61 62 6f  mple,.** (E) abo
2e98f 76 65 20 69 73 20 69 6e 64 65 78 61 62 6c 65 20  ve is indexable 
2e990 6f 6e 20 74 61 62 6c 65 73 20 50 2c 20 51 2c 20  on tables P, Q, 
2e991 61 6e 64 20 52 2e 0a 2a 2a 0a 2a 2a 20 54 65 72  and R..**.** Ter
2e992 6d 73 20 74 68 61 74 20 73 61 74 69 73 66 79 20  ms that satisfy 
2e993 63 61 73 65 20 32 20 61 72 65 20 63 61 6e 64 69  case 2 are candi
2e994 64 61 74 65 73 20 66 6f 72 20 6c 6f 6f 6b 75 70  dates for lookup
2e995 20 62 79 20 75 73 69 6e 67 0a 2a 2a 20 73 65 70   by using.** sep
2e996 61 72 61 74 65 20 69 6e 64 69 63 65 73 20 74 6f  arate indices to
2e997 20 66 69 6e 64 20 72 6f 77 69 64 73 20 66 6f 72   find rowids for
2e998 20 65 61 63 68 20 73 75 62 74 65 72 6d 20 61 6e   each subterm an
2e999 64 20 63 6f 6d 70 6f 73 69 6e 67 0a 2a 2a 20 74  d composing.** t
2e99a 68 65 20 75 6e 69 6f 6e 20 6f 66 20 61 6c 6c 20  he union of all 
2e99b 72 6f 77 69 64 73 20 75 73 69 6e 67 20 61 20 52  rowids using a R
2e99c 6f 77 53 65 74 20 6f 62 6a 65 63 74 2e 20 20 54  owSet object.  T
2e99d 68 69 73 20 69 73 20 73 69 6d 69 6c 61 72 0a 2a  his is similar.*
2e99e 2a 20 74 6f 20 22 62 69 74 6d 61 70 20 69 6e 64  * to "bitmap ind
2e99f 69 63 65 73 22 20 69 6e 20 6f 74 68 65 72 20 64  ices" in other d
2e9a0 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 73 2e  atabase engines.
2e9a1 0a 2a 2a 0a 2a 2a 20 4f 54 48 45 52 57 49 53 45  .**.** OTHERWISE
2e9a2 3a 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 65 69 74 68  :.**.** If neith
2e9a3 65 72 20 63 61 73 65 20 31 20 6e 6f 72 20 63 61  er case 1 nor ca
2e9a4 73 65 20 32 20 61 70 70 6c 79 2c 20 74 68 65 6e  se 2 apply, then
2e9a5 20 6c 65 61 76 65 20 74 68 65 20 65 4f 70 65 72   leave the eOper
2e9a6 61 74 6f 72 20 73 65 74 20 74 6f 0a 2a 2a 20 7a  ator set to.** z
2e9a7 65 72 6f 2e 20 20 54 68 69 73 20 74 65 72 6d 20  ero.  This term 
2e9a8 69 73 20 6e 6f 74 20 75 73 65 66 75 6c 20 66 6f  is not useful fo
2e9a9 72 20 73 65 61 72 63 68 2e 0a 2a 2f 0a 73 74 61  r search..*/.sta
2e9aa 74 69 63 20 76 6f 69 64 20 65 78 70 72 41 6e 61  tic void exprAna
2e9ab 6c 79 7a 65 4f 72 54 65 72 6d 28 0a 20 20 53 72  lyzeOrTerm(.  Sr
2e9ac 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20 20  cList *pSrc,    
2e9ad 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 46          /* the F
2e9ae 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ROM clause */.  
2e9af 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43  WhereClause *pWC
2e9b0 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65  ,         /* the
2e9b1 20 63 6f 6d 70 6c 65 74 65 20 57 48 45 52 45 20   complete WHERE 
2e9b2 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20  clause */.  int 
2e9b3 69 64 78 54 65 72 6d 20 20 20 20 20 20 20 20 20  idxTerm         
2e9b4 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
2e9b5 66 20 74 68 65 20 4f 52 2d 74 65 72 6d 20 74 6f  f the OR-term to
2e9b6 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a   be analyzed */.
2e9b7 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  ){.  Parse *pPar
2e9b8 73 65 20 3d 20 70 57 43 2d 3e 70 50 61 72 73 65  se = pWC->pParse
2e9b9 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
2e9ba 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  Parser context *
2e9bb 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
2e9bc 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20  = pParse->db;   
2e9bd 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
2e9be 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
2e9bf 6f 6e 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72  on */.  WhereTer
2e9c0 6d 20 2a 70 54 65 72 6d 20 3d 20 26 70 57 43 2d  m *pTerm = &pWC-
2e9c1 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 20 20 20 20  >a[idxTerm];    
2e9c2 2f 2a 20 54 68 65 20 74 65 72 6d 20 74 6f 20 62  /* The term to b
2e9c3 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20  e analyzed */.  
2e9c4 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 54  Expr *pExpr = pT
2e9c5 65 72 6d 2d 3e 70 45 78 70 72 3b 20 20 20 20 20  erm->pExpr;     
2e9c6 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65          /* The e
2e9c7 78 70 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65  xpression of the
2e9c8 20 74 65 72 6d 20 2a 2f 0a 20 20 57 68 65 72 65   term */.  Where
2e9c9 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65  MaskSet *pMaskSe
2e9ca 74 20 3d 20 70 57 43 2d 3e 70 4d 61 73 6b 53 65  t = pWC->pMaskSe
2e9cb 74 3b 20 2f 2a 20 54 61 62 6c 65 20 75 73 65 20  t; /* Table use 
2e9cc 6d 61 73 6b 73 20 2a 2f 0a 20 20 69 6e 74 20 69  masks */.  int i
2e9cd 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2e9ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e9cf 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
2e9d0 65 72 73 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c  ers */.  WhereCl
2e9d1 61 75 73 65 20 2a 70 4f 72 57 63 3b 20 20 20 20  ause *pOrWc;    
2e9d2 20 20 20 2f 2a 20 42 72 65 61 6b 75 70 20 6f 66     /* Breakup of
2e9d3 20 70 54 65 72 6d 20 69 6e 74 6f 20 73 75 62 74   pTerm into subt
2e9d4 65 72 6d 73 20 2a 2f 0a 20 20 57 68 65 72 65 54  erms */.  WhereT
2e9d5 65 72 6d 20 2a 70 4f 72 54 65 72 6d 3b 20 20 20  erm *pOrTerm;   
2e9d6 20 20 20 20 2f 2a 20 41 20 53 75 62 2d 74 65 72      /* A Sub-ter
2e9d7 6d 20 77 69 74 68 69 6e 20 74 68 65 20 70 4f 72  m within the pOr
2e9d8 57 63 20 2a 2f 0a 20 20 57 68 65 72 65 4f 72 49  Wc */.  WhereOrI
2e9d9 6e 66 6f 20 2a 70 4f 72 49 6e 66 6f 3b 20 20 20  nfo *pOrInfo;   
2e9da 20 20 2f 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20    /* Additional 
2e9db 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 73 6f  information asso
2e9dc 63 69 61 74 65 64 20 77 69 74 68 20 70 54 65 72  ciated with pTer
2e9dd 6d 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 63  m */.  Bitmask c
2e9de 68 6e 67 54 6f 49 4e 3b 20 20 20 20 20 20 20 20  hngToIN;        
2e9df 20 2f 2a 20 54 61 62 6c 65 73 20 74 68 61 74 20   /* Tables that 
2e9e0 6d 69 67 68 74 20 73 61 74 69 73 66 79 20 63 61  might satisfy ca
2e9e1 73 65 20 31 20 2a 2f 0a 20 20 42 69 74 6d 61 73  se 1 */.  Bitmas
2e9e2 6b 20 69 6e 64 65 78 61 62 6c 65 3b 20 20 20 20  k indexable;    
2e9e3 20 20 20 20 2f 2a 20 54 61 62 6c 65 73 20 74 68      /* Tables th
2e9e4 61 74 20 61 72 65 20 69 6e 64 65 78 61 62 6c 65  at are indexable
2e9e5 2c 20 73 61 74 69 73 66 79 69 6e 67 20 63 61 73  , satisfying cas
2e9e6 65 20 32 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a  e 2 */..  /*.  *
2e9e7 2a 20 42 72 65 61 6b 20 74 68 65 20 4f 52 20 63  * Break the OR c
2e9e8 6c 61 75 73 65 20 69 6e 74 6f 20 69 74 73 20 73  lause into its s
2e9e9 65 70 61 72 61 74 65 20 73 75 62 74 65 72 6d 73  eparate subterms
2e9ea 2e 20 20 54 68 65 20 73 75 62 74 65 72 6d 73 20  .  The subterms 
2e9eb 61 72 65 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20  are.  ** stored 
2e9ec 69 6e 20 61 20 57 68 65 72 65 43 6c 61 75 73 65  in a WhereClause
2e9ed 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61   structure conta
2e9ee 69 6e 69 6e 67 20 77 69 74 68 69 6e 20 74 68 65  ining within the
2e9ef 20 57 68 65 72 65 4f 72 49 6e 66 6f 0a 20 20 2a   WhereOrInfo.  *
2e9f0 2a 20 6f 62 6a 65 63 74 20 74 68 61 74 20 69 73  * object that is
2e9f1 20 61 74 74 61 63 68 65 64 20 74 6f 20 74 68 65   attached to the
2e9f2 20 6f 72 69 67 69 6e 61 6c 20 4f 52 20 63 6c 61   original OR cla
2e9f3 75 73 65 20 74 65 72 6d 2e 0a 20 20 2a 2f 0a 20  use term..  */. 
2e9f4 20 61 73 73 65 72 74 28 20 28 70 54 65 72 6d 2d   assert( (pTerm-
2e9f5 3e 77 74 46 6c 61 67 73 20 26 20 28 54 45 52 4d  >wtFlags & (TERM
2e9f6 5f 44 59 4e 41 4d 49 43 7c 54 45 52 4d 5f 4f 52  _DYNAMIC|TERM_OR
2e9f7 49 4e 46 4f 7c 54 45 52 4d 5f 41 4e 44 49 4e 46  INFO|TERM_ANDINF
2e9f8 4f 29 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  O))==0 );.  asse
2e9f9 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  rt( pExpr->op==T
2e9fa 4b 5f 4f 52 20 29 3b 0a 20 20 70 54 65 72 6d 2d  K_OR );.  pTerm-
2e9fb 3e 75 2e 70 4f 72 49 6e 66 6f 20 3d 20 70 4f 72  >u.pOrInfo = pOr
2e9fc 49 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62  Info = sqlite3Db
2e9fd 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
2e9fe 69 7a 65 6f 66 28 2a 70 4f 72 49 6e 66 6f 29 29  izeof(*pOrInfo))
2e9ff 3b 0a 20 20 69 66 28 20 70 4f 72 49 6e 66 6f 3d  ;.  if( pOrInfo=
2ea00 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70  =0 ) return;.  p
2ea01 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d  Term->wtFlags |=
2ea02 20 54 45 52 4d 5f 4f 52 49 4e 46 4f 3b 0a 20 20   TERM_ORINFO;.  
2ea03 70 4f 72 57 63 20 3d 20 26 70 4f 72 49 6e 66 6f  pOrWc = &pOrInfo
2ea04 2d 3e 77 63 3b 0a 20 20 77 68 65 72 65 43 6c 61  ->wc;.  whereCla
2ea05 75 73 65 49 6e 69 74 28 70 4f 72 57 63 2c 20 70  useInit(pOrWc, p
2ea06 57 43 2d 3e 70 50 61 72 73 65 2c 20 70 4d 61 73  WC->pParse, pMas
2ea07 6b 53 65 74 29 3b 0a 20 20 77 68 65 72 65 53 70  kSet);.  whereSp
2ea08 6c 69 74 28 70 4f 72 57 63 2c 20 70 45 78 70 72  lit(pOrWc, pExpr
2ea09 2c 20 54 4b 5f 4f 52 29 3b 0a 20 20 65 78 70 72  , TK_OR);.  expr
2ea0a 41 6e 61 6c 79 7a 65 41 6c 6c 28 70 53 72 63 2c  AnalyzeAll(pSrc,
2ea0b 20 70 4f 72 57 63 29 3b 0a 20 20 69 66 28 20 64   pOrWc);.  if( d
2ea0c 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
2ea0d 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65  ) return;.  asse
2ea0e 72 74 28 20 70 4f 72 57 63 2d 3e 6e 54 65 72 6d  rt( pOrWc->nTerm
2ea0f 3e 3d 32 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  >=2 );..  /*.  *
2ea10 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 73 65  * Compute the se
2ea11 74 20 6f 66 20 74 61 62 6c 65 73 20 74 68 61 74  t of tables that
2ea12 20 6d 69 67 68 74 20 73 61 74 69 73 66 79 20 63   might satisfy c
2ea13 61 73 65 73 20 31 20 6f 72 20 32 2e 0a 20 20 2a  ases 1 or 2..  *
2ea14 2f 0a 20 20 69 6e 64 65 78 61 62 6c 65 20 3d 20  /.  indexable = 
2ea15 7e 28 42 69 74 6d 61 73 6b 29 30 3b 0a 20 20 63  ~(Bitmask)0;.  c
2ea16 68 6e 67 54 6f 49 4e 20 3d 20 7e 28 70 57 43 2d  hngToIN = ~(pWC-
2ea17 3e 76 6d 61 73 6b 29 3b 0a 20 20 66 6f 72 28 69  >vmask);.  for(i
2ea18 3d 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d 31 2c  =pOrWc->nTerm-1,
2ea19 20 70 4f 72 54 65 72 6d 3d 70 4f 72 57 63 2d 3e   pOrTerm=pOrWc->
2ea1a 61 3b 20 69 3e 3d 30 20 26 26 20 69 6e 64 65 78  a; i>=0 && index
2ea1b 61 62 6c 65 3b 20 69 2d 2d 2c 20 70 4f 72 54 65  able; i--, pOrTe
2ea1c 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 28  rm++){.    if( (
2ea1d 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  pOrTerm->eOperat
2ea1e 6f 72 20 26 20 57 4f 5f 53 49 4e 47 4c 45 29 3d  or & WO_SINGLE)=
2ea1f 3d 30 20 29 7b 0a 20 20 20 20 20 20 57 68 65 72  =0 ){.      Wher
2ea20 65 41 6e 64 49 6e 66 6f 20 2a 70 41 6e 64 49 6e  eAndInfo *pAndIn
2ea21 66 6f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  fo;.      assert
2ea22 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72  ( pOrTerm->eOper
2ea23 61 74 6f 72 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ator==0 );.     
2ea24 20 61 73 73 65 72 74 28 20 28 70 4f 72 54 65 72   assert( (pOrTer
2ea25 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 28 54 45  m->wtFlags & (TE
2ea26 52 4d 5f 41 4e 44 49 4e 46 4f 7c 54 45 52 4d 5f  RM_ANDINFO|TERM_
2ea27 4f 52 49 4e 46 4f 29 29 3d 3d 30 20 29 3b 0a 20  ORINFO))==0 );. 
2ea28 20 20 20 20 20 63 68 6e 67 54 6f 49 4e 20 3d 20       chngToIN = 
2ea29 30 3b 0a 20 20 20 20 20 20 70 41 6e 64 49 6e 66  0;.      pAndInf
2ea2a 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  o = sqlite3DbMal
2ea2b 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f  locRaw(db, sizeo
2ea2c 66 28 2a 70 41 6e 64 49 6e 66 6f 29 29 3b 0a 20  f(*pAndInfo));. 
2ea2d 20 20 20 20 20 69 66 28 20 70 41 6e 64 49 6e 66       if( pAndInf
2ea2e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 57 68 65  o ){.        Whe
2ea2f 72 65 43 6c 61 75 73 65 20 2a 70 41 6e 64 57 43  reClause *pAndWC
2ea30 3b 0a 20 20 20 20 20 20 20 20 57 68 65 72 65 54  ;.        WhereT
2ea31 65 72 6d 20 2a 70 41 6e 64 54 65 72 6d 3b 0a 20  erm *pAndTerm;. 
2ea32 20 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20         int j;.  
2ea33 20 20 20 20 20 20 42 69 74 6d 61 73 6b 20 62 20        Bitmask b 
2ea34 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70 4f 72  = 0;.        pOr
2ea35 54 65 72 6d 2d 3e 75 2e 70 41 6e 64 49 6e 66 6f  Term->u.pAndInfo
2ea36 20 3d 20 70 41 6e 64 49 6e 66 6f 3b 0a 20 20 20   = pAndInfo;.   
2ea37 20 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e 77 74       pOrTerm->wt
2ea38 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 41 4e  Flags |= TERM_AN
2ea39 44 49 4e 46 4f 3b 0a 20 20 20 20 20 20 20 20 70  DINFO;.        p
2ea3a 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f  OrTerm->eOperato
2ea3b 72 20 3d 20 57 4f 5f 41 4e 44 3b 0a 20 20 20 20  r = WO_AND;.    
2ea3c 20 20 20 20 70 41 6e 64 57 43 20 3d 20 26 70 41      pAndWC = &pA
2ea3d 6e 64 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20 20  ndInfo->wc;.    
2ea3e 20 20 20 20 77 68 65 72 65 43 6c 61 75 73 65 49      whereClauseI
2ea3f 6e 69 74 28 70 41 6e 64 57 43 2c 20 70 57 43 2d  nit(pAndWC, pWC-
2ea40 3e 70 50 61 72 73 65 2c 20 70 4d 61 73 6b 53 65  >pParse, pMaskSe
2ea41 74 29 3b 0a 20 20 20 20 20 20 20 20 77 68 65 72  t);.        wher
2ea42 65 53 70 6c 69 74 28 70 41 6e 64 57 43 2c 20 70  eSplit(pAndWC, p
2ea43 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2c 20 54  OrTerm->pExpr, T
2ea44 4b 5f 41 4e 44 29 3b 0a 20 20 20 20 20 20 20 20  K_AND);.        
2ea45 65 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 70  exprAnalyzeAll(p
2ea46 53 72 63 2c 20 70 41 6e 64 57 43 29 3b 0a 20 20  Src, pAndWC);.  
2ea47 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2ea48 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2ea49 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
2ea4a 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
2ea4b 64 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66  d ){.          f
2ea4c 6f 72 28 6a 3d 30 2c 20 70 41 6e 64 54 65 72 6d  or(j=0, pAndTerm
2ea4d 3d 70 41 6e 64 57 43 2d 3e 61 3b 20 6a 3c 70 41  =pAndWC->a; j<pA
2ea4e 6e 64 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a 2b 2b  ndWC->nTerm; j++
2ea4f 2c 20 70 41 6e 64 54 65 72 6d 2b 2b 29 7b 0a 20  , pAndTerm++){. 
2ea50 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72             asser
2ea51 74 28 20 70 41 6e 64 54 65 72 6d 2d 3e 70 45 78  t( pAndTerm->pEx
2ea52 70 72 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  pr );.          
2ea53 20 20 69 66 28 20 61 6c 6c 6f 77 65 64 4f 70 28    if( allowedOp(
2ea54 70 41 6e 64 54 65 72 6d 2d 3e 70 45 78 70 72 2d  pAndTerm->pExpr-
2ea55 3e 6f 70 29 20 29 7b 0a 20 20 20 20 20 20 20 20  >op) ){.        
2ea56 20 20 20 20 20 20 62 20 7c 3d 20 67 65 74 4d 61        b |= getMa
2ea57 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70 41 6e  sk(pMaskSet, pAn
2ea58 64 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  dTerm->leftCurso
2ea59 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  r);.            
2ea5a 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  }.          }.  
2ea5b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2ea5c 69 6e 64 65 78 61 62 6c 65 20 26 3d 20 62 3b 0a  indexable &= b;.
2ea5d 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
2ea5e 65 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e 77  e if( pOrTerm->w
2ea5f 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f  tFlags & TERM_CO
2ea60 50 49 45 44 20 29 7b 0a 20 20 20 20 20 20 2f 2a  PIED ){.      /*
2ea61 20 53 6b 69 70 20 74 68 69 73 20 74 65 72 6d 20   Skip this term 
2ea62 66 6f 72 20 6e 6f 77 2e 20 20 57 65 20 72 65 76  for now.  We rev
2ea63 69 73 69 74 20 69 74 20 77 68 65 6e 20 77 65 20  isit it when we 
2ea64 70 72 6f 63 65 73 73 20 74 68 65 0a 20 20 20 20  process the.    
2ea65 20 20 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69    ** correspondi
2ea66 6e 67 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20  ng TERM_VIRTUAL 
2ea67 74 65 72 6d 20 2a 2f 0a 20 20 20 20 7d 65 6c 73  term */.    }els
2ea68 65 7b 0a 20 20 20 20 20 20 42 69 74 6d 61 73 6b  e{.      Bitmask
2ea69 20 62 3b 0a 20 20 20 20 20 20 62 20 3d 20 67 65   b;.      b = ge
2ea6a 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20  tMask(pMaskSet, 
2ea6b 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  pOrTerm->leftCur
2ea6c 73 6f 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  sor);.      if( 
2ea6d 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73  pOrTerm->wtFlags
2ea6e 20 26 20 54 45 52 4d 5f 56 49 52 54 55 41 4c 20   & TERM_VIRTUAL 
2ea6f 29 7b 0a 20 20 20 20 20 20 20 20 57 68 65 72 65  ){.        Where
2ea70 54 65 72 6d 20 2a 70 4f 74 68 65 72 20 3d 20 26  Term *pOther = &
2ea71 70 4f 72 57 63 2d 3e 61 5b 70 4f 72 54 65 72 6d  pOrWc->a[pOrTerm
2ea72 2d 3e 69 50 61 72 65 6e 74 5d 3b 0a 20 20 20 20  ->iParent];.    
2ea73 20 20 20 20 62 20 7c 3d 20 67 65 74 4d 61 73 6b      b |= getMask
2ea74 28 70 4d 61 73 6b 53 65 74 2c 20 70 4f 74 68 65  (pMaskSet, pOthe
2ea75 72 2d 3e 6c 65 66 74 43 75 72 73 6f 72 29 3b 0a  r->leftCursor);.
2ea76 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 6e        }.      in
2ea77 64 65 78 61 62 6c 65 20 26 3d 20 62 3b 0a 20 20  dexable &= b;.  
2ea78 20 20 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d      if( pOrTerm-
2ea79 3e 65 4f 70 65 72 61 74 6f 72 21 3d 57 4f 5f 45  >eOperator!=WO_E
2ea7a 51 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 6e  Q ){.        chn
2ea7b 67 54 6f 49 4e 20 3d 20 30 3b 0a 20 20 20 20 20  gToIN = 0;.     
2ea7c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2ea7d 63 68 6e 67 54 6f 49 4e 20 26 3d 20 62 3b 0a 20  chngToIN &= b;. 
2ea7e 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
2ea7f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52 65 63 6f  ..  /*.  ** Reco
2ea80 72 64 20 74 68 65 20 73 65 74 20 6f 66 20 74 61  rd the set of ta
2ea81 62 6c 65 73 20 74 68 61 74 20 73 61 74 69 73 66  bles that satisf
2ea82 79 20 63 61 73 65 20 32 2e 20 20 54 68 65 20 73  y case 2.  The s
2ea83 65 74 20 6d 69 67 68 74 20 62 65 0a 20 20 2a 2a  et might be.  **
2ea84 20 65 6d 70 74 79 2e 0a 20 20 2a 2f 0a 20 20 70   empty..  */.  p
2ea85 4f 72 49 6e 66 6f 2d 3e 69 6e 64 65 78 61 62 6c  OrInfo->indexabl
2ea86 65 20 3d 20 69 6e 64 65 78 61 62 6c 65 3b 0a 20  e = indexable;. 
2ea87 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
2ea88 72 20 3d 20 69 6e 64 65 78 61 62 6c 65 3d 3d 30  r = indexable==0
2ea89 20 3f 20 30 20 3a 20 57 4f 5f 4f 52 3b 0a 0a 20   ? 0 : WO_OR;.. 
2ea8a 20 2f 2a 0a 20 20 2a 2a 20 63 68 6e 67 54 6f 49   /*.  ** chngToI
2ea8b 4e 20 68 6f 6c 64 73 20 61 20 73 65 74 20 6f 66  N holds a set of
2ea8c 20 74 61 62 6c 65 73 20 74 68 61 74 20 2a 6d 69   tables that *mi
2ea8d 67 68 74 2a 20 73 61 74 69 73 66 79 20 63 61 73  ght* satisfy cas
2ea8e 65 20 31 2e 20 20 42 75 74 0a 20 20 2a 2a 20 77  e 1.  But.  ** w
2ea8f 65 20 68 61 76 65 20 74 6f 20 64 6f 20 73 6f 6d  e have to do som
2ea90 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 68 65  e additional che
2ea91 63 6b 69 6e 67 20 74 6f 20 73 65 65 20 69 66 20  cking to see if 
2ea92 63 61 73 65 20 31 20 72 65 61 6c 6c 79 0a 20 20  case 1 really.  
2ea93 2a 2a 20 69 73 20 73 61 74 69 73 66 69 65 64 2e  ** is satisfied.
2ea94 0a 20 20 2a 2a 0a 20 20 2a 2a 20 63 68 6e 67 54  .  **.  ** chngT
2ea95 6f 49 4e 20 77 69 6c 6c 20 68 6f 6c 64 20 65 69  oIN will hold ei
2ea96 74 68 65 72 20 30 2c 20 31 2c 20 6f 72 20 32 20  ther 0, 1, or 2 
2ea97 62 69 74 73 2e 20 20 54 68 65 20 30 2d 62 69 74  bits.  The 0-bit
2ea98 20 63 61 73 65 20 6d 65 61 6e 73 0a 20 20 2a 2a   case means.  **
2ea99 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20 6e   that there is n
2ea9a 6f 20 70 6f 73 73 69 62 69 6c 69 74 79 20 6f 66  o possibility of
2ea9b 20 74 72 61 6e 73 66 6f 72 6d 69 6e 67 20 74 68   transforming th
2ea9c 65 20 4f 52 20 63 6c 61 75 73 65 20 69 6e 74 6f  e OR clause into
2ea9d 20 61 6e 0a 20 20 2a 2a 20 49 4e 20 6f 70 65 72   an.  ** IN oper
2ea9e 61 74 6f 72 20 62 65 63 61 75 73 65 20 6f 6e 65  ator because one
2ea9f 20 6f 72 20 6d 6f 72 65 20 74 65 72 6d 73 20 69   or more terms i
2eaa0 6e 20 74 68 65 20 4f 52 20 63 6c 61 75 73 65 20  n the OR clause 
2eaa1 63 6f 6e 74 61 69 6e 0a 20 20 2a 2a 20 73 6f 6d  contain.  ** som
2eaa2 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61  ething other tha
2eaa3 6e 20 3d 3d 20 6f 6e 20 61 20 63 6f 6c 75 6d 6e  n == on a column
2eaa4 20 69 6e 20 74 68 65 20 73 69 6e 67 6c 65 20 74   in the single t
2eaa5 61 62 6c 65 2e 20 20 54 68 65 20 31 2d 62 69 74  able.  The 1-bit
2eaa6 0a 20 20 2a 2a 20 63 61 73 65 20 6d 65 61 6e 73  .  ** case means
2eaa7 20 74 68 61 74 20 65 76 65 72 79 20 74 65 72 6d   that every term
2eaa8 20 6f 66 20 74 68 65 20 4f 52 20 63 6c 61 75 73   of the OR claus
2eaa9 65 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  e is of the form
2eaaa 0a 20 20 2a 2a 20 22 74 61 62 6c 65 2e 63 6f 6c  .  ** "table.col
2eaab 75 6d 6e 3d 65 78 70 72 22 20 66 6f 72 20 73 6f  umn=expr" for so
2eaac 6d 65 20 73 69 6e 67 6c 65 20 74 61 62 6c 65 2e  me single table.
2eaad 20 20 54 68 65 20 6f 6e 65 20 62 69 74 20 74 68    The one bit th
2eaae 61 74 20 69 73 20 73 65 74 0a 20 20 2a 2a 20 77  at is set.  ** w
2eaaf 69 6c 6c 20 63 6f 72 72 65 73 70 6f 6e 64 20 74  ill correspond t
2eab0 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 74 61 62  o the common tab
2eab1 6c 65 2e 20 20 57 65 20 73 74 69 6c 6c 20 6e 65  le.  We still ne
2eab2 65 64 20 74 6f 20 63 68 65 63 6b 20 74 6f 20 6d  ed to check to m
2eab3 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65 20 74 68  ake.  ** sure th
2eab4 65 20 73 61 6d 65 20 63 6f 6c 75 6d 6e 20 69 73  e same column is
2eab5 20 75 73 65 64 20 6f 6e 20 61 6c 6c 20 74 65 72   used on all ter
2eab6 6d 73 2e 20 20 54 68 65 20 32 2d 62 69 74 20 63  ms.  The 2-bit c
2eab7 61 73 65 20 69 73 20 77 68 65 6e 0a 20 20 2a 2a  ase is when.  **
2eab8 20 74 68 65 20 61 6c 6c 20 74 65 72 6d 73 20 61   the all terms a
2eab9 72 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  re of the form "
2eaba 74 61 62 6c 65 31 2e 63 6f 6c 75 6d 6e 3d 74 61  table1.column=ta
2eabb 62 6c 65 32 2e 63 6f 6c 75 6d 6e 22 2e 20 20 49  ble2.column".  I
2eabc 74 0a 20 20 2a 2a 20 6d 69 67 68 74 20 62 65 20  t.  ** might be 
2eabd 70 6f 73 73 69 62 6c 65 20 74 6f 20 66 6f 72 6d  possible to form
2eabe 20 61 6e 20 49 4e 20 6f 70 65 72 61 74 6f 72 20   an IN operator 
2eabf 77 69 74 68 20 65 69 74 68 65 72 20 74 61 62 6c  with either tabl
2eac0 65 31 2e 63 6f 6c 75 6d 6e 0a 20 20 2a 2a 20 6f  e1.column.  ** o
2eac1 72 20 74 61 62 6c 65 32 2e 63 6f 6c 75 6d 6e 20  r table2.column 
2eac2 61 73 20 74 68 65 20 4c 48 53 20 69 66 20 65 69  as the LHS if ei
2eac3 74 68 65 72 20 69 73 20 63 6f 6d 6d 6f 6e 20 74  ther is common t
2eac4 6f 20 65 76 65 72 79 20 74 65 72 6d 20 6f 66 0a  o every term of.
2eac5 20 20 2a 2a 20 74 68 65 20 4f 52 20 63 6c 61 75    ** the OR clau
2eac6 73 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f  se..  **.  ** No
2eac7 74 65 20 74 68 61 74 20 74 65 72 6d 73 20 6f 66  te that terms of
2eac8 20 74 68 65 20 66 6f 72 6d 20 22 74 61 62 6c 65   the form "table
2eac9 2e 63 6f 6c 75 6d 6e 31 3d 74 61 62 6c 65 2e 63  .column1=table.c
2eaca 6f 6c 75 6d 6e 32 22 20 28 74 68 65 0a 20 20 2a  olumn2" (the.  *
2eacb 2a 20 73 61 6d 65 20 74 61 62 6c 65 20 6f 6e 20  * same table on 
2eacc 62 6f 74 68 20 73 69 7a 65 73 20 6f 66 20 74 68  both sizes of th
2eacd 65 20 3d 3d 29 20 63 61 6e 6e 6f 74 20 62 65 20  e ==) cannot be 
2eace 6f 70 74 69 6d 69 7a 65 64 2e 0a 20 20 2a 2f 0a  optimized..  */.
2eacf 20 20 69 66 28 20 63 68 6e 67 54 6f 49 4e 20 29    if( chngToIN )
2ead0 7b 0a 20 20 20 20 69 6e 74 20 6f 6b 54 6f 43 68  {.    int okToCh
2ead1 6e 67 54 6f 49 4e 20 3d 20 30 3b 20 20 20 20 20  ngToIN = 0;     
2ead2 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 63  /* True if the c
2ead3 6f 6e 76 65 72 73 69 6f 6e 20 74 6f 20 49 4e 20  onversion to IN 
2ead4 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 20 20  is valid */.    
2ead5 69 6e 74 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31  int iColumn = -1
2ead6 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c  ;         /* Col
2ead7 75 6d 6e 20 69 6e 64 65 78 20 6f 6e 20 6c 68 73  umn index on lhs
2ead8 20 6f 66 20 49 4e 20 6f 70 65 72 61 74 6f 72 20   of IN operator 
2ead9 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 75 72 73  */.    int iCurs
2eada 6f 72 20 3d 20 2d 31 3b 20 20 20 20 20 20 20 20  or = -1;        
2eadb 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73 6f 72   /* Table cursor
2eadc 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 74   common to all t
2eadd 65 72 6d 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20  erms */.    int 
2eade 6a 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  j = 0;          
2eadf 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
2eae0 75 6e 74 65 72 20 2a 2f 0a 0a 20 20 20 20 2f 2a  unter */..    /*
2eae1 20 53 65 61 72 63 68 20 66 6f 72 20 61 20 74 61   Search for a ta
2eae2 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 74  ble and column t
2eae3 68 61 74 20 61 70 70 65 61 72 73 20 6f 6e 20 6f  hat appears on o
2eae4 6e 65 20 73 69 64 65 20 6f 72 20 74 68 65 0a 20  ne side or the. 
2eae5 20 20 20 2a 2a 20 6f 74 68 65 72 20 6f 66 20 74     ** other of t
2eae6 68 65 20 3d 3d 20 6f 70 65 72 61 74 6f 72 20 69  he == operator i
2eae7 6e 20 65 76 65 72 79 20 73 75 62 74 65 72 6d 2e  n every subterm.
2eae8 20 20 54 68 61 74 20 74 61 62 6c 65 20 61 6e 64    That table and
2eae9 20 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a 20 77   column.    ** w
2eaea 69 6c 6c 20 62 65 20 72 65 63 6f 72 64 65 64 20  ill be recorded 
2eaeb 69 6e 20 69 43 75 72 73 6f 72 20 61 6e 64 20 69  in iCursor and i
2eaec 43 6f 6c 75 6d 6e 2e 20 20 54 68 65 72 65 20 6d  Column.  There m
2eaed 69 67 68 74 20 6e 6f 74 20 62 65 20 61 6e 79 0a  ight not be any.
2eaee 20 20 20 20 2a 2a 20 73 75 63 68 20 74 61 62 6c      ** such tabl
2eaef 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 20 20 53  e and column.  S
2eaf0 65 74 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20  et okToChngToIN 
2eaf1 69 66 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74  if an appropriat
2eaf2 65 20 74 61 62 6c 65 0a 20 20 20 20 2a 2a 20 61  e table.    ** a
2eaf3 6e 64 20 63 6f 6c 75 6d 6e 20 69 73 20 66 6f 75  nd column is fou
2eaf4 6e 64 20 62 75 74 20 6c 65 61 76 65 20 6f 6b 54  nd but leave okT
2eaf5 6f 43 68 6e 67 54 6f 49 4e 20 66 61 6c 73 65 20  oChngToIN false 
2eaf6 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 20 20  if not found..  
2eaf7 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 6a 3d 30    */.    for(j=0
2eaf8 3b 20 6a 3c 32 20 26 26 20 21 6f 6b 54 6f 43 68  ; j<2 && !okToCh
2eaf9 6e 67 54 6f 49 4e 3b 20 6a 2b 2b 29 7b 0a 20 20  ngToIN; j++){.  
2eafa 20 20 20 20 70 4f 72 54 65 72 6d 20 3d 20 70 4f      pOrTerm = pO
2eafb 72 57 63 2d 3e 61 3b 0a 20 20 20 20 20 20 66 6f  rWc->a;.      fo
2eafc 72 28 69 3d 70 4f 72 57 63 2d 3e 6e 54 65 72 6d  r(i=pOrWc->nTerm
2eafd 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70  -1; i>=0; i--, p
2eafe 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20  OrTerm++){.     
2eaff 20 20 20 61 73 73 65 72 74 28 20 70 4f 72 54 65     assert( pOrTe
2eb00 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57  rm->eOperator==W
2eb01 4f 5f 45 51 20 29 3b 0a 20 20 20 20 20 20 20 20  O_EQ );.        
2eb02 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73  pOrTerm->wtFlags
2eb03 20 26 3d 20 7e 54 45 52 4d 5f 4f 52 5f 4f 4b 3b   &= ~TERM_OR_OK;
2eb04 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 4f 72  .        if( pOr
2eb05 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72  Term->leftCursor
2eb06 3d 3d 69 43 75 72 73 6f 72 20 29 7b 0a 20 20 20  ==iCursor ){.   
2eb07 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69         /* This i
2eb08 73 20 74 68 65 20 32 2d 62 69 74 20 63 61 73 65  s the 2-bit case
2eb09 20 61 6e 64 20 77 65 20 61 72 65 20 6f 6e 20 74   and we are on t
2eb0a 68 65 20 73 65 63 6f 6e 64 20 69 74 65 72 61 74  he second iterat
2eb0b 69 6f 6e 20 61 6e 64 0a 20 20 20 20 20 20 20 20  ion and.        
2eb0c 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 74 65 72    ** current ter
2eb0d 6d 20 69 73 20 66 72 6f 6d 20 74 68 65 20 66 69  m is from the fi
2eb0e 72 73 74 20 69 74 65 72 61 74 69 6f 6e 2e 20 20  rst iteration.  
2eb0f 53 6f 20 73 6b 69 70 20 74 68 69 73 20 74 65 72  So skip this ter
2eb10 6d 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  m. */.          
2eb11 61 73 73 65 72 74 28 20 6a 3d 3d 31 20 29 3b 0a  assert( j==1 );.
2eb12 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e            contin
2eb13 75 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ue;.        }.  
2eb14 20 20 20 20 20 20 69 66 28 20 28 63 68 6e 67 54        if( (chngT
2eb15 6f 49 4e 20 26 20 67 65 74 4d 61 73 6b 28 70 4d  oIN & getMask(pM
2eb16 61 73 6b 53 65 74 2c 20 70 4f 72 54 65 72 6d 2d  askSet, pOrTerm-
2eb17 3e 6c 65 66 74 43 75 72 73 6f 72 29 29 3d 3d 30  >leftCursor))==0
2eb18 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a   ){.          /*
2eb19 20 54 68 69 73 20 74 65 72 6d 20 6d 75 73 74 20   This term must 
2eb1a 62 65 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 74  be of the form t
2eb1b 31 2e 61 3d 3d 74 32 2e 62 20 77 68 65 72 65 20  1.a==t2.b where 
2eb1c 74 32 20 69 73 20 69 6e 20 74 68 65 0a 20 20 20  t2 is in the.   
2eb1d 20 20 20 20 20 20 20 2a 2a 20 63 68 6e 67 54 6f         ** chngTo
2eb1e 49 4e 20 73 65 74 20 62 75 74 20 74 31 20 69 73  IN set but t1 is
2eb1f 20 6e 6f 74 2e 20 20 54 68 69 73 20 74 65 72 6d   not.  This term
2eb20 20 77 69 6c 6c 20 62 65 20 65 69 74 68 65 72 20   will be either 
2eb21 70 72 65 63 65 65 64 65 64 0a 20 20 20 20 20 20  preceeded.      
2eb22 20 20 20 20 2a 2a 20 6f 72 20 66 6f 6c 6c 77 65      ** or follwe
2eb23 64 20 62 79 20 61 6e 20 69 6e 76 65 72 74 65 64  d by an inverted
2eb24 20 63 6f 70 79 20 28 74 32 2e 62 3d 3d 74 31 2e   copy (t2.b==t1.
2eb25 61 29 2e 20 20 53 6b 69 70 20 74 68 69 73 20 74  a).  Skip this t
2eb26 65 72 6d 20 0a 20 20 20 20 20 20 20 20 20 20 2a  erm .          *
2eb27 2a 20 61 6e 64 20 75 73 65 20 69 74 73 20 69 6e  * and use its in
2eb28 76 65 72 73 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20  version. */.    
2eb29 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
2eb2a 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73  pOrTerm->wtFlags
2eb2b 20 26 20 54 45 52 4d 5f 43 4f 50 49 45 44 20 29   & TERM_COPIED )
2eb2c 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65 73 74  ;.          test
2eb2d 63 61 73 65 28 20 70 4f 72 54 65 72 6d 2d 3e 77  case( pOrTerm->w
2eb2e 74 46 6c 61 67 73 20 26 20 54 45 52 4d 5f 56 49  tFlags & TERM_VI
2eb2f 52 54 55 41 4c 20 29 3b 0a 20 20 20 20 20 20 20  RTUAL );.       
2eb30 20 20 20 61 73 73 65 72 74 28 20 70 4f 72 54 65     assert( pOrTe
2eb31 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 28 54  rm->wtFlags & (T
2eb32 45 52 4d 5f 43 4f 50 49 45 44 7c 54 45 52 4d 5f  ERM_COPIED|TERM_
2eb33 56 49 52 54 55 41 4c 29 20 29 3b 0a 20 20 20 20  VIRTUAL) );.    
2eb34 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
2eb35 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2eb36 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 4f 72 54    iColumn = pOrT
2eb37 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d  erm->u.leftColum
2eb38 6e 3b 0a 20 20 20 20 20 20 20 20 69 43 75 72 73  n;.        iCurs
2eb39 6f 72 20 3d 20 70 4f 72 54 65 72 6d 2d 3e 6c 65  or = pOrTerm->le
2eb3a 66 74 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20  ftCursor;.      
2eb3b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
2eb3c 0a 20 20 20 20 20 20 69 66 28 20 69 3c 30 20 29  .      if( i<0 )
2eb3d 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 20  {.        /* No 
2eb3e 63 61 6e 64 69 64 61 74 65 20 74 61 62 6c 65 2b  candidate table+
2eb3f 63 6f 6c 75 6d 6e 20 77 61 73 20 66 6f 75 6e 64  column was found
2eb40 2e 20 20 54 68 69 73 20 63 61 6e 20 6f 6e 6c 79  .  This can only
2eb41 20 6f 63 63 75 72 0a 20 20 20 20 20 20 20 20 2a   occur.        *
2eb42 2a 20 6f 6e 20 74 68 65 20 73 65 63 6f 6e 64 20  * on the second 
2eb43 69 74 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20 20  iteration */.   
2eb44 20 20 20 20 20 61 73 73 65 72 74 28 20 6a 3d 3d       assert( j==
2eb45 31 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  1 );.        ass
2eb46 65 72 74 28 20 28 63 68 6e 67 54 6f 49 4e 26 28  ert( (chngToIN&(
2eb47 63 68 6e 67 54 6f 49 4e 2d 31 29 29 3d 3d 30 20  chngToIN-1))==0 
2eb48 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
2eb49 74 28 20 63 68 6e 67 54 6f 49 4e 3d 3d 67 65 74  t( chngToIN==get
2eb4a 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 69  Mask(pMaskSet, i
2eb4b 43 75 72 73 6f 72 29 20 29 3b 0a 20 20 20 20 20  Cursor) );.     
2eb4c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2eb4d 7d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  }.      testcase
2eb4e 28 20 6a 3d 3d 31 20 29 3b 0a 0a 20 20 20 20 20  ( j==1 );..     
2eb4f 20 2f 2a 20 57 65 20 68 61 76 65 20 66 6f 75 6e   /* We have foun
2eb50 64 20 61 20 63 61 6e 64 69 64 61 74 65 20 74 61  d a candidate ta
2eb51 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 20  ble and column. 
2eb52 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66   Check to see if
2eb53 20 74 68 61 74 0a 20 20 20 20 20 20 2a 2a 20 74   that.      ** t
2eb54 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e 20  able and column 
2eb55 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 65 76 65  is common to eve
2eb56 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65 20 4f  ry term in the O
2eb57 52 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20  R clause */.    
2eb58 20 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e 20 3d    okToChngToIN =
2eb59 20 31 3b 0a 20 20 20 20 20 20 66 6f 72 28 3b 20   1;.      for(; 
2eb5a 69 3e 3d 30 20 26 26 20 6f 6b 54 6f 43 68 6e 67  i>=0 && okToChng
2eb5b 54 6f 49 4e 3b 20 69 2d 2d 2c 20 70 4f 72 54 65  ToIN; i--, pOrTe
2eb5c 72 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 61  rm++){.        a
2eb5d 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e  ssert( pOrTerm->
2eb5e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 45 51  eOperator==WO_EQ
2eb5f 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
2eb60 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72  pOrTerm->leftCur
2eb61 73 6f 72 21 3d 69 43 75 72 73 6f 72 20 29 7b 0a  sor!=iCursor ){.
2eb62 20 20 20 20 20 20 20 20 20 20 70 4f 72 54 65 72            pOrTer
2eb63 6d 2d 3e 77 74 46 6c 61 67 73 20 26 3d 20 7e 54  m->wtFlags &= ~T
2eb64 45 52 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20  ERM_OR_OK;.     
2eb65 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 72     }else if( pOr
2eb66 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75  Term->u.leftColu
2eb67 6d 6e 21 3d 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20  mn!=iColumn ){. 
2eb68 20 20 20 20 20 20 20 20 20 6f 6b 54 6f 43 68 6e           okToChn
2eb69 67 54 6f 49 4e 20 3d 20 30 3b 0a 20 20 20 20 20  gToIN = 0;.     
2eb6a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2eb6b 20 20 20 20 69 6e 74 20 61 66 66 4c 65 66 74 2c      int affLeft,
2eb6c 20 61 66 66 52 69 67 68 74 3b 0a 20 20 20 20 20   affRight;.     
2eb6d 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 72       /* If the r
2eb6e 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69  ight-hand side i
2eb6f 73 20 61 6c 73 6f 20 61 20 63 6f 6c 75 6d 6e 2c  s also a column,
2eb70 20 74 68 65 6e 20 74 68 65 20 61 66 66 69 6e 69   then the affini
2eb71 74 69 65 73 0a 20 20 20 20 20 20 20 20 20 20 2a  ties.          *
2eb72 2a 20 6f 66 20 62 6f 74 68 20 72 69 67 68 74 20  * of both right 
2eb73 61 6e 64 20 6c 65 66 74 20 73 69 64 65 73 20 6d  and left sides m
2eb74 75 73 74 20 62 65 20 73 75 63 68 20 74 68 61 74  ust be such that
2eb75 20 6e 6f 20 74 79 70 65 0a 20 20 20 20 20 20 20   no type.       
2eb76 20 20 20 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e     ** conversion
2eb77 73 20 61 72 65 20 72 65 71 75 69 72 65 64 20 6f  s are required o
2eb78 6e 20 74 68 65 20 72 69 67 68 74 2e 20 20 28 54  n the right.  (T
2eb79 69 63 6b 65 74 20 23 32 32 34 39 29 0a 20 20 20  icket #2249).   
2eb7a 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
2eb7b 20 20 20 20 61 66 66 52 69 67 68 74 20 3d 20 73      affRight = s
2eb7c 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e 69  qlite3ExprAffini
2eb7d 74 79 28 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70  ty(pOrTerm->pExp
2eb7e 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20 20 20  r->pRight);.    
2eb7f 20 20 20 20 20 20 61 66 66 4c 65 66 74 20 3d 20        affLeft = 
2eb80 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69 6e  sqlite3ExprAffin
2eb81 69 74 79 28 70 4f 72 54 65 72 6d 2d 3e 70 45 78  ity(pOrTerm->pEx
2eb82 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20  pr->pLeft);.    
2eb83 20 20 20 20 20 20 69 66 28 20 61 66 66 52 69 67        if( affRig
2eb84 68 74 21 3d 30 20 26 26 20 61 66 66 52 69 67 68  ht!=0 && affRigh
2eb85 74 21 3d 61 66 66 4c 65 66 74 20 29 7b 0a 20 20  t!=affLeft ){.  
2eb86 20 20 20 20 20 20 20 20 20 20 6f 6b 54 6f 43 68            okToCh
2eb87 6e 67 54 6f 49 4e 20 3d 20 30 3b 0a 20 20 20 20  ngToIN = 0;.    
2eb88 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2eb89 20 20 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d           pOrTerm
2eb8a 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54 45 52  ->wtFlags |= TER
2eb8b 4d 5f 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 20  M_OR_OK;.       
2eb8c 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
2eb8d 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
2eb8e 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69    /* At this poi
2eb8f 6e 74 2c 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e  nt, okToChngToIN
2eb90 20 69 73 20 74 72 75 65 20 69 66 20 6f 72 69 67   is true if orig
2eb91 69 6e 61 6c 20 70 54 65 72 6d 20 73 61 74 69 73  inal pTerm satis
2eb92 66 69 65 73 0a 20 20 20 20 2a 2a 20 63 61 73 65  fies.    ** case
2eb93 20 31 2e 20 20 49 6e 20 74 68 61 74 20 63 61 73   1.  In that cas
2eb94 65 2c 20 63 6f 6e 73 74 72 75 63 74 20 61 20 6e  e, construct a n
2eb95 65 77 20 76 69 72 74 75 61 6c 20 74 65 72 6d 20  ew virtual term 
2eb96 74 68 61 74 20 69 73 20 0a 20 20 20 20 2a 2a 20  that is .    ** 
2eb97 70 54 65 72 6d 20 63 6f 6e 76 65 72 74 65 64 20  pTerm converted 
2eb98 69 6e 74 6f 20 61 6e 20 49 4e 20 6f 70 65 72 61  into an IN opera
2eb99 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  tor..    */.    
2eb9a 69 66 28 20 6f 6b 54 6f 43 68 6e 67 54 6f 49 4e  if( okToChngToIN
2eb9b 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a   ){.      Expr *
2eb9c 70 44 75 70 3b 20 20 20 20 20 20 20 20 20 20 20  pDup;           
2eb9d 20 2f 2a 20 41 20 74 72 61 6e 73 69 65 6e 74 20   /* A transient 
2eb9e 64 75 70 6c 69 63 61 74 65 20 65 78 70 72 65 73  duplicate expres
2eb9f 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78  sion */.      Ex
2eba0 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20  prList *pList = 
2eba1 30 3b 20 20 20 2f 2a 20 54 68 65 20 52 48 53 20  0;   /* The RHS 
2eba2 6f 66 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74  of the IN operat
2eba3 6f 72 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72  or */.      Expr
2eba4 20 2a 70 4c 65 66 74 20 3d 20 30 3b 20 20 20 20   *pLeft = 0;    
2eba5 20 20 20 2f 2a 20 54 68 65 20 4c 48 53 20 6f 66     /* The LHS of
2eba6 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f 72   the IN operator
2eba7 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a   */.      Expr *
2eba8 70 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 20  pNew;           
2eba9 20 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65   /* The complete
2ebaa 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a   IN operator */.
2ebab 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 70 4f 72  .      for(i=pOr
2ebac 57 63 2d 3e 6e 54 65 72 6d 2d 31 2c 20 70 4f 72  Wc->nTerm-1, pOr
2ebad 54 65 72 6d 3d 70 4f 72 57 63 2d 3e 61 3b 20 69  Term=pOrWc->a; i
2ebae 3e 3d 30 3b 20 69 2d 2d 2c 20 70 4f 72 54 65 72  >=0; i--, pOrTer
2ebaf 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66  m++){.        if
2ebb0 28 20 28 70 4f 72 54 65 72 6d 2d 3e 77 74 46 6c  ( (pOrTerm->wtFl
2ebb1 61 67 73 20 26 20 54 45 52 4d 5f 4f 52 5f 4f 4b  ags & TERM_OR_OK
2ebb2 29 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  )==0 ) continue;
2ebb3 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
2ebb4 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61   pOrTerm->eOpera
2ebb5 74 6f 72 3d 3d 57 4f 5f 45 51 20 29 3b 0a 20 20  tor==WO_EQ );.  
2ebb6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
2ebb7 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  rTerm->leftCurso
2ebb8 72 3d 3d 69 43 75 72 73 6f 72 20 29 3b 0a 20 20  r==iCursor );.  
2ebb9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
2ebba 72 54 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c  rTerm->u.leftCol
2ebbb 75 6d 6e 3d 3d 69 43 6f 6c 75 6d 6e 20 29 3b 0a  umn==iColumn );.
2ebbc 20 20 20 20 20 20 20 20 70 44 75 70 20 3d 20 73          pDup = s
2ebbd 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
2ebbe 2c 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72  , pOrTerm->pExpr
2ebbf 2d 3e 70 52 69 67 68 74 2c 20 30 29 3b 0a 20 20  ->pRight, 0);.  
2ebc0 20 20 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71        pList = sq
2ebc1 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
2ebc2 65 6e 64 28 70 57 43 2d 3e 70 50 61 72 73 65 2c  end(pWC->pParse,
2ebc3 20 70 4c 69 73 74 2c 20 70 44 75 70 29 3b 0a 20   pList, pDup);. 
2ebc4 20 20 20 20 20 20 20 70 4c 65 66 74 20 3d 20 70         pLeft = p
2ebc5 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70  OrTerm->pExpr->p
2ebc6 4c 65 66 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Left;.      }.  
2ebc7 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 65 66      assert( pLef
2ebc8 74 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 44  t!=0 );.      pD
2ebc9 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  up = sqlite3Expr
2ebca 44 75 70 28 64 62 2c 20 70 4c 65 66 74 2c 20 30  Dup(db, pLeft, 0
2ebcb 29 3b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20  );.      pNew = 
2ebcc 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2ebcd 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 70 44 75 70  rse, TK_IN, pDup
2ebce 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 69  , 0, 0);.      i
2ebcf 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20  f( pNew ){.     
2ebd0 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b 0a 20     int idxNew;. 
2ebd1 20 20 20 20 20 20 20 74 72 61 6e 73 66 65 72 4a         transferJ
2ebd2 6f 69 6e 4d 61 72 6b 69 6e 67 73 28 70 4e 65 77  oinMarkings(pNew
2ebd3 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20  , pExpr);.      
2ebd4 20 20 61 73 73 65 72 74 28 20 21 45 78 70 72 48    assert( !ExprH
2ebd5 61 73 50 72 6f 70 65 72 74 79 28 70 4e 65 77 2c  asProperty(pNew,
2ebd6 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29 20 29   EP_xIsSelect) )
2ebd7 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e  ;.        pNew->
2ebd8 78 2e 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b  x.pList = pList;
2ebd9 0a 20 20 20 20 20 20 20 20 69 64 78 4e 65 77 20  .        idxNew 
2ebda 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73  = whereClauseIns
2ebdb 65 72 74 28 70 57 43 2c 20 70 4e 65 77 2c 20 54  ert(pWC, pNew, T
2ebdc 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d  ERM_VIRTUAL|TERM
2ebdd 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20  _DYNAMIC);.     
2ebde 20 20 20 74 65 73 74 63 61 73 65 28 20 69 64 78     testcase( idx
2ebdf 4e 65 77 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  New==0 );.      
2ebe0 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 70 53    exprAnalyze(pS
2ebe1 72 63 2c 20 70 57 43 2c 20 69 64 78 4e 65 77 29  rc, pWC, idxNew)
2ebe2 3b 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d 20  ;.        pTerm 
2ebe3 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72  = &pWC->a[idxTer
2ebe4 6d 5d 3b 0a 20 20 20 20 20 20 20 20 70 57 43 2d  m];.        pWC-
2ebe5 3e 61 5b 69 64 78 4e 65 77 5d 2e 69 50 61 72 65  >a[idxNew].iPare
2ebe6 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20  nt = idxTerm;.  
2ebe7 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68        pTerm->nCh
2ebe8 69 6c 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  ild = 1;.      }
2ebe9 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71  else{.        sq
2ebea 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
2ebeb 65 74 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a  ete(db, pList);.
2ebec 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 54        }.      pT
2ebed 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d  erm->eOperator =
2ebee 20 30 3b 20 20 2f 2a 20 63 61 73 65 20 31 20 74   0;  /* case 1 t
2ebef 72 75 6d 70 73 20 63 61 73 65 20 32 20 2a 2f 0a  rumps case 2 */.
2ebf0 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64      }.  }.}.#end
2ebf1 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d  if /* !SQLITE_OM
2ebf2 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49  IT_OR_OPTIMIZATI
2ebf3 4f 4e 20 26 26 20 21 53 51 4c 49 54 45 5f 4f 4d  ON && !SQLITE_OM
2ebf4 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f 0a 0a  IT_SUBQUERY */..
2ebf5 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74  ./*.** The input
2ebf6 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   to this routine
2ebf7 20 69 73 20 61 6e 20 57 68 65 72 65 54 65 72 6d   is an WhereTerm
2ebf8 20 73 74 72 75 63 74 75 72 65 20 77 69 74 68 20   structure with 
2ebf9 6f 6e 6c 79 20 74 68 65 0a 2a 2a 20 22 70 45 78  only the.** "pEx
2ebfa 70 72 22 20 66 69 65 6c 64 20 66 69 6c 6c 65 64  pr" field filled
2ebfb 20 69 6e 2e 20 20 54 68 65 20 6a 6f 62 20 6f 66   in.  The job of
2ebfc 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
2ebfd 20 74 6f 20 61 6e 61 6c 79 7a 65 20 74 68 65 0a   to analyze the.
2ebfe 2a 2a 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e  ** subexpression
2ebff 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 61 6c   and populate al
2ec00 6c 20 74 68 65 20 6f 74 68 65 72 20 66 69 65 6c  l the other fiel
2ec01 64 73 20 6f 66 20 74 68 65 20 57 68 65 72 65 54  ds of the WhereT
2ec02 65 72 6d 0a 2a 2a 20 73 74 72 75 63 74 75 72 65  erm.** structure
2ec03 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 65  ..**.** If the e
2ec04 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f 66 20  xpression is of 
2ec05 74 68 65 20 66 6f 72 6d 20 22 3c 65 78 70 72 3e  the form "<expr>
2ec06 20 3c 6f 70 3e 20 58 22 20 69 74 20 67 65 74 73   <op> X" it gets
2ec07 20 63 6f 6d 6d 75 74 65 64 0a 2a 2a 20 74 6f 20   commuted.** to 
2ec08 74 68 65 20 73 74 61 6e 64 61 72 64 20 66 6f 72  the standard for
2ec09 6d 20 6f 66 20 22 58 20 3c 6f 70 3e 20 3c 65 78  m of "X <op> <ex
2ec0a 70 72 3e 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  pr>"..**.** If t
2ec0b 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
2ec0c 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 58 20   of the form "X 
2ec0d 3c 6f 70 3e 20 59 22 20 77 68 65 72 65 20 62 6f  <op> Y" where bo
2ec0e 74 68 20 58 20 61 6e 64 20 59 20 61 72 65 0a 2a  th X and Y are.*
2ec0f 2a 20 63 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20  * columns, then 
2ec10 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 65 78 70  the original exp
2ec11 72 65 73 73 69 6f 6e 20 69 73 20 75 6e 63 68 61  ression is uncha
2ec12 6e 67 65 64 20 61 6e 64 20 61 20 6e 65 77 20 76  nged and a new v
2ec13 69 72 74 75 61 6c 0a 2a 2a 20 74 65 72 6d 20 6f  irtual.** term o
2ec14 66 20 74 68 65 20 66 6f 72 6d 20 22 59 20 3c 6f  f the form "Y <o
2ec15 70 3e 20 58 22 20 69 73 20 61 64 64 65 64 20 74  p> X" is added t
2ec16 6f 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  o the WHERE clau
2ec17 73 65 20 61 6e 64 0a 2a 2a 20 61 6e 61 6c 79 7a  se and.** analyz
2ec18 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e 20 20  ed separately.  
2ec19 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 65 72  The original ter
2ec1a 6d 20 69 73 20 6d 61 72 6b 65 64 20 77 69 74 68  m is marked with
2ec1b 20 54 45 52 4d 5f 43 4f 50 49 45 44 0a 2a 2a 20   TERM_COPIED.** 
2ec1c 61 6e 64 20 74 68 65 20 6e 65 77 20 74 65 72 6d  and the new term
2ec1d 20 69 73 20 6d 61 72 6b 65 64 20 77 69 74 68 20   is marked with 
2ec1e 54 45 52 4d 5f 44 59 4e 41 4d 49 43 20 28 62 65  TERM_DYNAMIC (be
2ec1f 63 61 75 73 65 20 69 74 27 73 20 70 45 78 70 72  cause it's pExpr
2ec20 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20  .** needs to be 
2ec21 66 72 65 65 64 20 77 69 74 68 20 74 68 65 20 57  freed with the W
2ec22 68 65 72 65 43 6c 61 75 73 65 29 20 61 6e 64 20  hereClause) and 
2ec23 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 28 62 65  TERM_VIRTUAL (be
2ec24 63 61 75 73 65 20 69 74 0a 2a 2a 20 69 73 20 61  cause it.** is a
2ec25 20 63 6f 6d 6d 75 74 65 64 20 63 6f 70 79 20 6f   commuted copy o
2ec26 66 20 61 20 70 72 69 6f 72 20 74 65 72 6d 2e 29  f a prior term.)
2ec27 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 74    The original t
2ec28 65 72 6d 20 68 61 73 20 6e 43 68 69 6c 64 3d 31  erm has nChild=1
2ec29 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63 6f 70 79  .** and the copy
2ec2a 20 68 61 73 20 69 64 78 50 61 72 65 6e 74 20 73   has idxParent s
2ec2b 65 74 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20  et to the index 
2ec2c 6f 66 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20  of the original 
2ec2d 74 65 72 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  term..*/.static 
2ec2e 76 6f 69 64 20 65 78 70 72 41 6e 61 6c 79 7a 65  void exprAnalyze
2ec2f 28 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  (.  SrcList *pSr
2ec30 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  c,            /*
2ec31 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
2ec32 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73   */.  WhereClaus
2ec33 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20 20  e *pWC,         
2ec34 2f 2a 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  /* the WHERE cla
2ec35 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78  use */.  int idx
2ec36 54 65 72 6d 20 20 20 20 20 20 20 20 20 20 20 20  Term            
2ec37 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74     /* Index of t
2ec38 68 65 20 74 65 72 6d 20 74 6f 20 62 65 20 61 6e  he term to be an
2ec39 61 6c 79 7a 65 64 20 2a 2f 0a 29 7b 0a 20 20 57  alyzed */.){.  W
2ec3a 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b  hereTerm *pTerm;
2ec3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec3c 2f 2a 20 54 68 65 20 74 65 72 6d 20 74 6f 20 62  /* The term to b
2ec3d 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20  e analyzed */.  
2ec3e 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d  WhereMaskSet *pM
2ec3f 61 73 6b 53 65 74 3b 20 20 20 20 20 20 20 20 20  askSet;         
2ec40 20 2f 2a 20 53 65 74 20 6f 66 20 74 61 62 6c 65   /* Set of table
2ec41 20 69 6e 64 65 78 20 6d 61 73 6b 73 20 2a 2f 0a   index masks */.
2ec42 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20    Expr *pExpr;  
2ec43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec44 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
2ec45 73 69 6f 6e 20 74 6f 20 62 65 20 61 6e 61 6c 79  sion to be analy
2ec46 7a 65 64 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  zed */.  Bitmask
2ec47 20 70 72 65 72 65 71 4c 65 66 74 3b 20 20 20 20   prereqLeft;    
2ec48 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 65            /* Pre
2ec49 72 65 71 75 65 73 69 74 65 73 20 6f 66 20 74 68  requesites of th
2ec4a 65 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 2a  e pExpr->pLeft *
2ec4b 2f 0a 20 20 42 69 74 6d 61 73 6b 20 70 72 65 72  /.  Bitmask prer
2ec4c 65 71 41 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  eqAll;          
2ec4d 20 20 20 20 20 2f 2a 20 50 72 65 72 65 71 75 65       /* Prereque
2ec4e 73 69 74 65 73 20 6f 66 20 70 45 78 70 72 20 2a  sites of pExpr *
2ec4f 2f 0a 20 20 42 69 74 6d 61 73 6b 20 65 78 74 72  /.  Bitmask extr
2ec50 61 52 69 67 68 74 20 3d 20 30 3b 20 20 20 20 20  aRight = 0;     
2ec51 20 20 20 20 20 2f 2a 20 2a 2f 0a 20 20 45 78 70       /* */.  Exp
2ec52 72 20 2a 70 53 74 72 31 20 3d 20 30 3b 20 20 20  r *pStr1 = 0;   
2ec53 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2ec54 20 52 48 53 20 6f 66 20 4c 49 4b 45 2f 47 4c 4f   RHS of LIKE/GLO
2ec55 42 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20  B operator */.  
2ec56 69 6e 74 20 69 73 43 6f 6d 70 6c 65 74 65 20 3d  int isComplete =
2ec57 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
2ec58 20 2f 2a 20 52 48 53 20 6f 66 20 4c 49 4b 45 2f   /* RHS of LIKE/
2ec59 47 4c 4f 42 20 65 6e 64 73 20 77 69 74 68 20 77  GLOB ends with w
2ec5a 69 6c 64 63 61 72 64 20 2a 2f 0a 20 20 69 6e 74  ildcard */.  int
2ec5b 20 6e 6f 43 61 73 65 20 3d 20 30 3b 20 20 20 20   noCase = 0;    
2ec5c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2ec5d 20 4c 49 4b 45 2f 47 4c 4f 42 20 64 69 73 74 69   LIKE/GLOB disti
2ec5e 6e 67 75 69 73 68 65 73 20 63 61 73 65 20 2a 2f  nguishes case */
2ec5f 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20 20  .  int op;      
2ec60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec61 20 20 20 20 2f 2a 20 54 6f 70 2d 6c 65 76 65 6c      /* Top-level
2ec62 20 6f 70 65 72 61 74 6f 72 2e 20 20 70 45 78 70   operator.  pExp
2ec63 72 2d 3e 6f 70 20 2a 2f 0a 20 20 50 61 72 73 65  r->op */.  Parse
2ec64 20 2a 70 50 61 72 73 65 20 3d 20 70 57 43 2d 3e   *pParse = pWC->
2ec65 70 50 61 72 73 65 3b 20 20 20 20 20 2f 2a 20 50  pParse;     /* P
2ec66 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2ec67 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
2ec68 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20  = pParse->db;   
2ec69 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
2ec6a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a   connection */..
2ec6b 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
2ec6c 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65  Failed ){.    re
2ec6d 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 54 65 72  turn;.  }.  pTer
2ec6e 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54  m = &pWC->a[idxT
2ec6f 65 72 6d 5d 3b 0a 20 20 70 4d 61 73 6b 53 65 74  erm];.  pMaskSet
2ec70 20 3d 20 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74   = pWC->pMaskSet
2ec71 3b 0a 20 20 70 45 78 70 72 20 3d 20 70 54 65 72  ;.  pExpr = pTer
2ec72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 70 72 65 72  m->pExpr;.  prer
2ec73 65 71 4c 65 66 74 20 3d 20 65 78 70 72 54 61 62  eqLeft = exprTab
2ec74 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
2ec75 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b  , pExpr->pLeft);
2ec76 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f  .  op = pExpr->o
2ec77 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  p;.  if( op==TK_
2ec78 49 4e 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  IN ){.    assert
2ec79 28 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3d  ( pExpr->pRight=
2ec7a 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 45 78  =0 );.    if( Ex
2ec7b 70 72 48 61 73 50 72 6f 70 65 72 74 79 28 70 45  prHasProperty(pE
2ec7c 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
2ec7d 74 29 20 29 7b 0a 20 20 20 20 20 20 70 54 65 72  t) ){.      pTer
2ec7e 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d  m->prereqRight =
2ec7f 20 65 78 70 72 53 65 6c 65 63 74 54 61 62 6c 65   exprSelectTable
2ec80 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
2ec81 70 45 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74  pExpr->x.pSelect
2ec82 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
2ec83 20 20 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65      pTerm->prere
2ec84 71 52 69 67 68 74 20 3d 20 65 78 70 72 4c 69 73  qRight = exprLis
2ec85 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73  tTableUsage(pMas
2ec86 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e 78 2e 70  kSet, pExpr->x.p
2ec87 4c 69 73 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  List);.    }.  }
2ec88 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  else if( op==TK_
2ec89 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 70 54  ISNULL ){.    pT
2ec8a 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74  erm->prereqRight
2ec8b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
2ec8c 20 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71     pTerm->prereq
2ec8d 52 69 67 68 74 20 3d 20 65 78 70 72 54 61 62 6c  Right = exprTabl
2ec8e 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c  eUsage(pMaskSet,
2ec8f 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 29 3b   pExpr->pRight);
2ec90 0a 20 20 7d 0a 20 20 70 72 65 72 65 71 41 6c 6c  .  }.  prereqAll
2ec91 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61 67   = exprTableUsag
2ec92 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70  e(pMaskSet, pExp
2ec93 72 29 3b 0a 20 20 69 66 28 20 45 78 70 72 48 61  r);.  if( ExprHa
2ec94 73 50 72 6f 70 65 72 74 79 28 70 45 78 70 72 2c  sProperty(pExpr,
2ec95 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b   EP_FromJoin) ){
2ec96 0a 20 20 20 20 42 69 74 6d 61 73 6b 20 78 20 3d  .    Bitmask x =
2ec97 20 67 65 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65   getMask(pMaskSe
2ec98 74 2c 20 70 45 78 70 72 2d 3e 69 52 69 67 68 74  t, pExpr->iRight
2ec99 4a 6f 69 6e 54 61 62 6c 65 29 3b 0a 20 20 20 20  JoinTable);.    
2ec9a 70 72 65 72 65 71 41 6c 6c 20 7c 3d 20 78 3b 0a  prereqAll |= x;.
2ec9b 20 20 20 20 65 78 74 72 61 52 69 67 68 74 20 3d      extraRight =
2ec9c 20 78 2d 31 3b 20 20 2f 2a 20 4f 4e 20 63 6c 61   x-1;  /* ON cla
2ec9d 75 73 65 20 74 65 72 6d 73 20 6d 61 79 20 6e 6f  use terms may no
2ec9e 74 20 62 65 20 75 73 65 64 20 77 69 74 68 20 61  t be used with a
2ec9f 6e 20 69 6e 64 65 78 0a 20 20 20 20 20 20 20 20  n index.        
2eca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
2eca1 2a 20 6f 6e 20 6c 65 66 74 20 74 61 62 6c 65 20  * on left table 
2eca2 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 2e 20  of a LEFT JOIN. 
2eca3 20 54 69 63 6b 65 74 20 23 33 30 31 35 20 2a 2f   Ticket #3015 */
2eca4 0a 20 20 7d 0a 20 20 70 54 65 72 6d 2d 3e 70 72  .  }.  pTerm->pr
2eca5 65 72 65 71 41 6c 6c 20 3d 20 70 72 65 72 65 71  ereqAll = prereq
2eca6 41 6c 6c 3b 0a 20 20 70 54 65 72 6d 2d 3e 6c 65  All;.  pTerm->le
2eca7 66 74 43 75 72 73 6f 72 20 3d 20 2d 31 3b 0a 20  ftCursor = -1;. 
2eca8 20 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 20   pTerm->iParent 
2eca9 3d 20 2d 31 3b 0a 20 20 70 54 65 72 6d 2d 3e 65  = -1;.  pTerm->e
2ecaa 4f 70 65 72 61 74 6f 72 20 3d 20 30 3b 0a 20 20  Operator = 0;.  
2ecab 69 66 28 20 61 6c 6c 6f 77 65 64 4f 70 28 6f 70  if( allowedOp(op
2ecac 29 20 26 26 20 28 70 54 65 72 6d 2d 3e 70 72 65  ) && (pTerm->pre
2ecad 72 65 71 52 69 67 68 74 20 26 20 70 72 65 72 65  reqRight & prere
2ecae 71 4c 65 66 74 29 3d 3d 30 20 29 7b 0a 20 20 20  qLeft)==0 ){.   
2ecaf 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20 70   Expr *pLeft = p
2ecb0 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20  Expr->pLeft;.   
2ecb1 20 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20   Expr *pRight = 
2ecb2 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20  pExpr->pRight;. 
2ecb3 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70     if( pLeft->op
2ecb4 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20  ==TK_COLUMN ){. 
2ecb5 20 20 20 20 20 70 54 65 72 6d 2d 3e 6c 65 66 74       pTerm->left
2ecb6 43 75 72 73 6f 72 20 3d 20 70 4c 65 66 74 2d 3e  Cursor = pLeft->
2ecb7 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 70 54  iTable;.      pT
2ecb8 65 72 6d 2d 3e 75 2e 6c 65 66 74 43 6f 6c 75 6d  erm->u.leftColum
2ecb9 6e 20 3d 20 70 4c 65 66 74 2d 3e 69 43 6f 6c 75  n = pLeft->iColu
2ecba 6d 6e 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 2d  mn;.      pTerm-
2ecbb 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 6f 70 65  >eOperator = ope
2ecbc 72 61 74 6f 72 4d 61 73 6b 28 6f 70 29 3b 0a 20  ratorMask(op);. 
2ecbd 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 52 69     }.    if( pRi
2ecbe 67 68 74 20 26 26 20 70 52 69 67 68 74 2d 3e 6f  ght && pRight->o
2ecbf 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 7b 0a  p==TK_COLUMN ){.
2ecc0 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20        WhereTerm 
2ecc1 2a 70 4e 65 77 3b 0a 20 20 20 20 20 20 45 78 70  *pNew;.      Exp
2ecc2 72 20 2a 70 44 75 70 3b 0a 20 20 20 20 20 20 69  r *pDup;.      i
2ecc3 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75  f( pTerm->leftCu
2ecc4 72 73 6f 72 3e 3d 30 20 29 7b 0a 20 20 20 20 20  rsor>=0 ){.     
2ecc5 20 20 20 69 6e 74 20 69 64 78 4e 65 77 3b 0a 20     int idxNew;. 
2ecc6 20 20 20 20 20 20 20 70 44 75 70 20 3d 20 73 71         pDup = sq
2ecc7 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62 2c  lite3ExprDup(db,
2ecc8 20 70 45 78 70 72 2c 20 30 29 3b 0a 20 20 20 20   pExpr, 0);.    
2ecc9 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c      if( db->mall
2ecca 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
2eccb 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
2eccc 72 44 65 6c 65 74 65 28 64 62 2c 20 70 44 75 70  rDelete(db, pDup
2eccd 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  );.          ret
2ecce 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  urn;.        }. 
2eccf 20 20 20 20 20 20 20 69 64 78 4e 65 77 20 3d 20         idxNew = 
2ecd0 77 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72  whereClauseInser
2ecd1 74 28 70 57 43 2c 20 70 44 75 70 2c 20 54 45 52  t(pWC, pDup, TER
2ecd2 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44  M_VIRTUAL|TERM_D
2ecd3 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20 20 20  YNAMIC);.       
2ecd4 20 69 66 28 20 69 64 78 4e 65 77 3d 3d 30 20 29   if( idxNew==0 )
2ecd5 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20   return;.       
2ecd6 20 70 4e 65 77 20 3d 20 26 70 57 43 2d 3e 61 5b   pNew = &pWC->a[
2ecd7 69 64 78 4e 65 77 5d 3b 0a 20 20 20 20 20 20 20  idxNew];.       
2ecd8 20 70 4e 65 77 2d 3e 69 50 61 72 65 6e 74 20 3d   pNew->iParent =
2ecd9 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 20 20   idxTerm;.      
2ecda 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e    pTerm = &pWC->
2ecdb 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20  a[idxTerm];.    
2ecdc 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c      pTerm->nChil
2ecdd 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70  d = 1;.        p
2ecde 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d  Term->wtFlags |=
2ecdf 20 54 45 52 4d 5f 43 4f 50 49 45 44 3b 0a 20 20   TERM_COPIED;.  
2ece0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2ece1 20 20 20 70 44 75 70 20 3d 20 70 45 78 70 72 3b     pDup = pExpr;
2ece2 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d 20  .        pNew = 
2ece3 70 54 65 72 6d 3b 0a 20 20 20 20 20 20 7d 0a 20  pTerm;.      }. 
2ece4 20 20 20 20 20 65 78 70 72 43 6f 6d 6d 75 74 65       exprCommute
2ece5 28 70 50 61 72 73 65 2c 20 70 44 75 70 29 3b 0a  (pParse, pDup);.
2ece6 20 20 20 20 20 20 70 4c 65 66 74 20 3d 20 70 44        pLeft = pD
2ece7 75 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20 20  up->pLeft;.     
2ece8 20 70 4e 65 77 2d 3e 6c 65 66 74 43 75 72 73 6f   pNew->leftCurso
2ece9 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c  r = pLeft->iTabl
2ecea 65 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 75  e;.      pNew->u
2eceb 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c  .leftColumn = pL
2ecec 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20  eft->iColumn;.  
2eced 20 20 20 20 70 4e 65 77 2d 3e 70 72 65 72 65 71      pNew->prereq
2ecee 52 69 67 68 74 20 3d 20 70 72 65 72 65 71 4c 65  Right = prereqLe
2ecef 66 74 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e  ft;.      pNew->
2ecf0 70 72 65 72 65 71 41 6c 6c 20 3d 20 70 72 65 72  prereqAll = prer
2ecf1 65 71 41 6c 6c 3b 0a 20 20 20 20 20 20 70 4e 65  eqAll;.      pNe
2ecf2 77 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 6f  w->eOperator = o
2ecf3 70 65 72 61 74 6f 72 4d 61 73 6b 28 70 44 75 70  peratorMask(pDup
2ecf4 2d 3e 6f 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ->op);.    }.  }
2ecf5 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2ecf6 5f 4f 4d 49 54 5f 42 45 54 57 45 45 4e 5f 4f 50  _OMIT_BETWEEN_OP
2ecf7 54 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20  TIMIZATION.  /* 
2ecf8 49 66 20 61 20 74 65 72 6d 20 69 73 20 74 68 65  If a term is the
2ecf9 20 42 45 54 57 45 45 4e 20 6f 70 65 72 61 74 6f   BETWEEN operato
2ecfa 72 2c 20 63 72 65 61 74 65 20 74 77 6f 20 6e 65  r, create two ne
2ecfb 77 20 76 69 72 74 75 61 6c 20 74 65 72 6d 73 0a  w virtual terms.
2ecfc 20 20 2a 2a 20 74 68 61 74 20 64 65 66 69 6e 65    ** that define
2ecfd 20 74 68 65 20 72 61 6e 67 65 20 74 68 61 74 20   the range that 
2ecfe 74 68 65 20 42 45 54 57 45 45 4e 20 69 6d 70 6c  the BETWEEN impl
2ecff 65 6d 65 6e 74 73 2e 20 20 46 6f 72 20 65 78 61  ements.  For exa
2ed00 6d 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  mple:.  **.  ** 
2ed01 20 20 20 20 20 61 20 42 45 54 57 45 45 4e 20 62       a BETWEEN b
2ed02 20 41 4e 44 20 63 0a 20 20 2a 2a 0a 20 20 2a 2a   AND c.  **.  **
2ed03 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e   is converted in
2ed04 74 6f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  to:.  **.  **   
2ed05 20 20 20 28 61 20 42 45 54 57 45 45 4e 20 62 20     (a BETWEEN b 
2ed06 41 4e 44 20 63 29 20 41 4e 44 20 28 61 3e 3d 62  AND c) AND (a>=b
2ed07 29 20 41 4e 44 20 28 61 3c 3d 63 29 0a 20 20 2a  ) AND (a<=c).  *
2ed08 2a 0a 20 20 2a 2a 20 54 68 65 20 74 77 6f 20 6e  *.  ** The two n
2ed09 65 77 20 74 65 72 6d 73 20 61 72 65 20 61 64 64  ew terms are add
2ed0a 65 64 20 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20  ed onto the end 
2ed0b 6f 66 20 74 68 65 20 57 68 65 72 65 43 6c 61 75  of the WhereClau
2ed0c 73 65 20 6f 62 6a 65 63 74 2e 0a 20 20 2a 2a 20  se object..  ** 
2ed0d 54 68 65 20 6e 65 77 20 74 65 72 6d 73 20 61 72  The new terms ar
2ed0e 65 20 22 64 79 6e 61 6d 69 63 22 20 61 6e 64 20  e "dynamic" and 
2ed0f 61 72 65 20 63 68 69 6c 64 72 65 6e 20 6f 66 20  are children of 
2ed10 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 42 45 54  the original BET
2ed11 57 45 45 4e 0a 20 20 2a 2a 20 74 65 72 6d 2e 20  WEEN.  ** term. 
2ed12 20 54 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74   That means that
2ed13 20 69 66 20 74 68 65 20 42 45 54 57 45 45 4e 20   if the BETWEEN 
2ed14 74 65 72 6d 20 69 73 20 63 6f 64 65 64 2c 20 74  term is coded, t
2ed15 68 65 20 63 68 69 6c 64 72 65 6e 20 61 72 65 0a  he children are.
2ed16 20 20 2a 2a 20 73 6b 69 70 70 65 64 2e 20 20 4f    ** skipped.  O
2ed17 72 2c 20 69 66 20 74 68 65 20 63 68 69 6c 64 72  r, if the childr
2ed18 65 6e 20 61 72 65 20 73 61 74 69 73 66 69 65 64  en are satisfied
2ed19 20 62 79 20 61 6e 20 69 6e 64 65 78 2c 20 74 68   by an index, th
2ed1a 65 20 6f 72 69 67 69 6e 61 6c 0a 20 20 2a 2a 20  e original.  ** 
2ed1b 42 45 54 57 45 45 4e 20 74 65 72 6d 20 69 73 20  BETWEEN term is 
2ed1c 73 6b 69 70 70 65 64 2e 0a 20 20 2a 2f 0a 20 20  skipped..  */.  
2ed1d 65 6c 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e  else if( pExpr->
2ed1e 6f 70 3d 3d 54 4b 5f 42 45 54 57 45 45 4e 20 26  op==TK_BETWEEN &
2ed1f 26 20 70 57 43 2d 3e 6f 70 3d 3d 54 4b 5f 41 4e  & pWC->op==TK_AN
2ed20 44 20 29 7b 0a 20 20 20 20 45 78 70 72 4c 69 73  D ){.    ExprLis
2ed21 74 20 2a 70 4c 69 73 74 20 3d 20 70 45 78 70 72  t *pList = pExpr
2ed22 2d 3e 78 2e 70 4c 69 73 74 3b 0a 20 20 20 20 69  ->x.pList;.    i
2ed23 6e 74 20 69 3b 0a 20 20 20 20 73 74 61 74 69 63  nt i;.    static
2ed24 20 63 6f 6e 73 74 20 75 38 20 6f 70 73 5b 5d 20   const u8 ops[] 
2ed25 3d 20 7b 54 4b 5f 47 45 2c 20 54 4b 5f 4c 45 7d  = {TK_GE, TK_LE}
2ed26 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4c  ;.    assert( pL
2ed27 69 73 74 21 3d 30 20 29 3b 0a 20 20 20 20 61 73  ist!=0 );.    as
2ed28 73 65 72 74 28 20 70 4c 69 73 74 2d 3e 6e 45 78  sert( pList->nEx
2ed29 70 72 3d 3d 32 20 29 3b 0a 20 20 20 20 66 6f 72  pr==2 );.    for
2ed2a 28 69 3d 30 3b 20 69 3c 32 3b 20 69 2b 2b 29 7b  (i=0; i<2; i++){
2ed2b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e 65  .      Expr *pNe
2ed2c 77 45 78 70 72 3b 0a 20 20 20 20 20 20 69 6e 74  wExpr;.      int
2ed2d 20 69 64 78 4e 65 77 3b 0a 20 20 20 20 20 20 70   idxNew;.      p
2ed2e 4e 65 77 45 78 70 72 20 3d 20 73 71 6c 69 74 65  NewExpr = sqlite
2ed2f 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 6f  3PExpr(pParse, o
2ed30 70 73 5b 69 5d 2c 20 0a 20 20 20 20 20 20 20 20  ps[i], .        
2ed31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ed32 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2ed33 44 75 70 28 64 62 2c 20 70 45 78 70 72 2d 3e 70  Dup(db, pExpr->p
2ed34 4c 65 66 74 2c 20 30 29 2c 0a 20 20 20 20 20 20  Left, 0),.      
2ed35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ed36 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2ed37 70 72 44 75 70 28 64 62 2c 20 70 4c 69 73 74 2d  prDup(db, pList-
2ed38 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20 30 29 2c  >a[i].pExpr, 0),
2ed39 20 30 29 3b 0a 20 20 20 20 20 20 69 64 78 4e 65   0);.      idxNe
2ed3a 77 20 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49  w = whereClauseI
2ed3b 6e 73 65 72 74 28 70 57 43 2c 20 70 4e 65 77 45  nsert(pWC, pNewE
2ed3c 78 70 72 2c 20 54 45 52 4d 5f 56 49 52 54 55 41  xpr, TERM_VIRTUA
2ed3d 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b  L|TERM_DYNAMIC);
2ed3e 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
2ed3f 20 69 64 78 4e 65 77 3d 3d 30 20 29 3b 0a 20 20   idxNew==0 );.  
2ed40 20 20 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 28      exprAnalyze(
2ed41 70 53 72 63 2c 20 70 57 43 2c 20 69 64 78 4e 65  pSrc, pWC, idxNe
2ed42 77 29 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 20  w);.      pTerm 
2ed43 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72  = &pWC->a[idxTer
2ed44 6d 5d 3b 0a 20 20 20 20 20 20 70 57 43 2d 3e 61  m];.      pWC->a
2ed45 5b 69 64 78 4e 65 77 5d 2e 69 50 61 72 65 6e 74  [idxNew].iParent
2ed46 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20 20   = idxTerm;.    
2ed47 7d 0a 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68  }.    pTerm->nCh
2ed48 69 6c 64 20 3d 20 32 3b 0a 20 20 7d 0a 23 65 6e  ild = 2;.  }.#en
2ed49 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2ed4a 49 54 5f 42 45 54 57 45 45 4e 5f 4f 50 54 49 4d  IT_BETWEEN_OPTIM
2ed4b 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 23 69 66 20  IZATION */..#if 
2ed4c 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2ed4d 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41  OMIT_OR_OPTIMIZA
2ed4e 54 49 4f 4e 29 20 26 26 20 21 64 65 66 69 6e 65  TION) && !define
2ed4f 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  d(SQLITE_OMIT_SU
2ed50 42 51 55 45 52 59 29 0a 20 20 2f 2a 20 41 6e 61  BQUERY).  /* Ana
2ed51 6c 79 7a 65 20 61 20 74 65 72 6d 20 74 68 61 74  lyze a term that
2ed52 20 69 73 20 63 6f 6d 70 6f 73 65 64 20 6f 66 20   is composed of 
2ed53 74 77 6f 20 6f 72 20 6d 6f 72 65 20 73 75 62 74  two or more subt
2ed54 65 72 6d 73 20 63 6f 6e 6e 65 63 74 65 64 20 62  erms connected b
2ed55 79 0a 20 20 2a 2a 20 61 6e 20 4f 52 20 6f 70 65  y.  ** an OR ope
2ed56 72 61 74 6f 72 2e 0a 20 20 2a 2f 0a 20 20 65 6c  rator..  */.  el
2ed57 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70  se if( pExpr->op
2ed58 3d 3d 54 4b 5f 4f 52 20 29 7b 0a 20 20 20 20 61  ==TK_OR ){.    a
2ed59 73 73 65 72 74 28 20 70 57 43 2d 3e 6f 70 3d 3d  ssert( pWC->op==
2ed5a 54 4b 5f 41 4e 44 20 29 3b 0a 20 20 20 20 65 78  TK_AND );.    ex
2ed5b 70 72 41 6e 61 6c 79 7a 65 4f 72 54 65 72 6d 28  prAnalyzeOrTerm(
2ed5c 70 53 72 63 2c 20 70 57 43 2c 20 69 64 78 54 65  pSrc, pWC, idxTe
2ed5d 72 6d 29 3b 0a 20 20 20 20 70 54 65 72 6d 20 3d  rm);.    pTerm =
2ed5e 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65 72 6d   &pWC->a[idxTerm
2ed5f 5d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  ];.  }.#endif /*
2ed60 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f   SQLITE_OMIT_OR_
2ed61 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a  OPTIMIZATION */.
2ed62 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2ed63 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49  OMIT_LIKE_OPTIMI
2ed64 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 41 64 64 20  ZATION.  /* Add 
2ed65 63 6f 6e 73 74 72 61 69 6e 74 73 20 74 6f 20 72  constraints to r
2ed66 65 64 75 63 65 20 74 68 65 20 73 65 61 72 63 68  educe the search
2ed67 20 73 70 61 63 65 20 6f 6e 20 61 20 4c 49 4b 45   space on a LIKE
2ed68 20 6f 72 20 47 4c 4f 42 0a 20 20 2a 2a 20 6f 70   or GLOB.  ** op
2ed69 65 72 61 74 6f 72 2e 0a 20 20 2a 2a 0a 20 20 2a  erator..  **.  *
2ed6a 2a 20 41 20 6c 69 6b 65 20 70 61 74 74 65 72 6e  * A like pattern
2ed6b 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 78 20   of the form "x 
2ed6c 4c 49 4b 45 20 27 61 62 63 25 27 22 20 69 73 20  LIKE 'abc%'" is 
2ed6d 63 68 61 6e 67 65 64 20 69 6e 74 6f 20 63 6f 6e  changed into con
2ed6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a 0a 20 20  straints.  **.  
2ed6f 2a 2a 20 20 20 20 20 20 20 20 20 20 78 3e 3d 27  **          x>='
2ed70 61 62 63 27 20 41 4e 44 20 78 3c 27 61 62 64 27  abc' AND x<'abd'
2ed71 20 41 4e 44 20 78 20 4c 49 4b 45 20 27 61 62 63   AND x LIKE 'abc
2ed72 25 27 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65  %'.  **.  ** The
2ed73 20 6c 61 73 74 20 63 68 61 72 61 63 74 65 72 20   last character 
2ed74 6f 66 20 74 68 65 20 70 72 65 66 69 78 20 22 61  of the prefix "a
2ed75 62 63 22 20 69 73 20 69 6e 63 72 65 6d 65 6e 74  bc" is increment
2ed76 65 64 20 74 6f 20 66 6f 72 6d 20 74 68 65 0a 20  ed to form the. 
2ed77 20 2a 2a 20 74 65 72 6d 69 6e 61 74 69 6f 6e 20   ** termination 
2ed78 63 6f 6e 64 69 74 69 6f 6e 20 22 61 62 64 22 2e  condition "abd".
2ed79 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 57 43 2d  .  */.  if( pWC-
2ed7a 3e 6f 70 3d 3d 54 4b 5f 41 4e 44 20 0a 20 20 20  >op==TK_AND .   
2ed7b 26 26 20 69 73 4c 69 6b 65 4f 72 47 6c 6f 62 28  && isLikeOrGlob(
2ed7c 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26  pParse, pExpr, &
2ed7d 70 53 74 72 31 2c 20 26 69 73 43 6f 6d 70 6c 65  pStr1, &isComple
2ed7e 74 65 2c 20 26 6e 6f 43 61 73 65 29 0a 20 20 29  te, &noCase).  )
2ed7f 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66  {.    Expr *pLef
2ed80 74 3b 20 20 20 20 20 20 20 2f 2a 20 4c 48 53 20  t;       /* LHS 
2ed81 6f 66 20 4c 49 4b 45 2f 47 4c 4f 42 20 6f 70 65  of LIKE/GLOB ope
2ed82 72 61 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70  rator */.    Exp
2ed83 72 20 2a 70 53 74 72 32 3b 20 20 20 20 20 20 20  r *pStr2;       
2ed84 2f 2a 20 43 6f 70 79 20 6f 66 20 70 53 74 72 31  /* Copy of pStr1
2ed85 20 2d 20 52 48 53 20 6f 66 20 4c 49 4b 45 2f 47   - RHS of LIKE/G
2ed86 4c 4f 42 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a  LOB operator */.
2ed87 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77 45 78      Expr *pNewEx
2ed88 70 72 31 3b 0a 20 20 20 20 45 78 70 72 20 2a 70  pr1;.    Expr *p
2ed89 4e 65 77 45 78 70 72 32 3b 0a 20 20 20 20 69 6e  NewExpr2;.    in
2ed8a 74 20 69 64 78 4e 65 77 31 3b 0a 20 20 20 20 69  t idxNew1;.    i
2ed8b 6e 74 20 69 64 78 4e 65 77 32 3b 0a 0a 20 20 20  nt idxNew2;..   
2ed8c 20 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e   pLeft = pExpr->
2ed8d 78 2e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45  x.pList->a[1].pE
2ed8e 78 70 72 3b 0a 20 20 20 20 70 53 74 72 32 20 3d  xpr;.    pStr2 =
2ed8f 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
2ed90 64 62 2c 20 70 53 74 72 31 2c 20 30 29 3b 0a 20  db, pStr1, 0);. 
2ed91 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c     if( !db->mall
2ed92 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
2ed93 20 20 75 38 20 63 2c 20 2a 70 43 3b 20 20 20 20    u8 c, *pC;    
2ed94 20 20 20 2f 2a 20 4c 61 73 74 20 63 68 61 72 61     /* Last chara
2ed95 63 74 65 72 20 62 65 66 6f 72 65 20 74 68 65 20  cter before the 
2ed96 66 69 72 73 74 20 77 69 6c 64 63 61 72 64 20 2a  first wildcard *
2ed97 2f 0a 20 20 20 20 20 20 70 43 20 3d 20 28 75 38  /.      pC = (u8
2ed98 2a 29 26 70 53 74 72 32 2d 3e 75 2e 7a 54 6f 6b  *)&pStr2->u.zTok
2ed99 65 6e 5b 73 71 6c 69 74 65 33 53 74 72 6c 65 6e  en[sqlite3Strlen
2ed9a 33 30 28 70 53 74 72 32 2d 3e 75 2e 7a 54 6f 6b  30(pStr2->u.zTok
2ed9b 65 6e 29 2d 31 5d 3b 0a 20 20 20 20 20 20 63 20  en)-1];.      c 
2ed9c 3d 20 2a 70 43 3b 0a 20 20 20 20 20 20 69 66 28  = *pC;.      if(
2ed9d 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20 20 20 20   noCase ){.     
2ed9e 20 20 20 2f 2a 20 54 68 65 20 70 6f 69 6e 74 20     /* The point 
2ed9f 69 73 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 20  is to increment 
2eda0 74 68 65 20 6c 61 73 74 20 63 68 61 72 61 63 74  the last charact
2eda1 65 72 20 62 65 66 6f 72 65 20 74 68 65 20 66 69  er before the fi
2eda2 72 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 77  rst.        ** w
2eda3 69 6c 64 63 61 72 64 2e 20 20 42 75 74 20 69 66  ildcard.  But if
2eda4 20 77 65 20 69 6e 63 72 65 6d 65 6e 74 20 27 40   we increment '@
2eda5 27 2c 20 74 68 61 74 20 77 69 6c 6c 20 70 75 73  ', that will pus
2eda6 68 20 69 74 20 69 6e 74 6f 20 74 68 65 0a 20 20  h it into the.  
2eda7 20 20 20 20 20 20 2a 2a 20 61 6c 70 68 61 62 65        ** alphabe
2eda8 74 69 63 20 72 61 6e 67 65 20 77 68 65 72 65 20  tic range where 
2eda9 63 61 73 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73  case conversions
2edaa 20 77 69 6c 6c 20 6d 65 73 73 20 75 70 20 74 68   will mess up th
2edab 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e  e .        ** in
2edac 65 71 75 61 6c 69 74 79 2e 20 20 54 6f 20 61 76  equality.  To av
2edad 6f 69 64 20 74 68 69 73 2c 20 6d 61 6b 65 20 73  oid this, make s
2edae 75 72 65 20 74 6f 20 61 6c 73 6f 20 72 75 6e 20  ure to also run 
2edaf 74 68 65 20 66 75 6c 6c 0a 20 20 20 20 20 20 20  the full.       
2edb0 20 2a 2a 20 4c 49 4b 45 20 6f 6e 20 61 6c 6c 20   ** LIKE on all 
2edb1 63 61 6e 64 69 64 61 74 65 20 65 78 70 72 65 73  candidate expres
2edb2 73 69 6f 6e 73 20 62 79 20 63 6c 65 61 72 69 6e  sions by clearin
2edb3 67 20 74 68 65 20 69 73 43 6f 6d 70 6c 65 74 65  g the isComplete
2edb4 20 66 6c 61 67 0a 20 20 20 20 20 20 20 20 2a 2f   flag.        */
2edb5 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d  .        if( c==
2edb6 27 41 27 2d 31 20 29 20 69 73 43 6f 6d 70 6c 65  'A'-1 ) isComple
2edb7 74 65 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 20  te = 0;..       
2edb8 20 63 20 3d 20 73 71 6c 69 74 65 33 55 70 70 65   c = sqlite3Uppe
2edb9 72 54 6f 4c 6f 77 65 72 5b 63 5d 3b 0a 20 20 20  rToLower[c];.   
2edba 20 20 20 7d 0a 20 20 20 20 20 20 2a 70 43 20 3d     }.      *pC =
2edbb 20 63 20 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20   c + 1;.    }.  
2edbc 20 20 70 4e 65 77 45 78 70 72 31 20 3d 20 73 71    pNewExpr1 = sq
2edbd 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2edbe 65 2c 20 54 4b 5f 47 45 2c 20 73 71 6c 69 74 65  e, TK_GE, sqlite
2edbf 33 45 78 70 72 44 75 70 28 64 62 2c 70 4c 65 66  3ExprDup(db,pLef
2edc0 74 2c 30 29 2c 70 53 74 72 31 2c 30 29 3b 0a 20  t,0),pStr1,0);. 
2edc1 20 20 20 69 64 78 4e 65 77 31 20 3d 20 77 68 65     idxNew1 = whe
2edc2 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70  reClauseInsert(p
2edc3 57 43 2c 20 70 4e 65 77 45 78 70 72 31 2c 20 54  WC, pNewExpr1, T
2edc4 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d  ERM_VIRTUAL|TERM
2edc5 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 74  _DYNAMIC);.    t
2edc6 65 73 74 63 61 73 65 28 20 69 64 78 4e 65 77 31  estcase( idxNew1
2edc7 3d 3d 30 20 29 3b 0a 20 20 20 20 65 78 70 72 41  ==0 );.    exprA
2edc8 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 43  nalyze(pSrc, pWC
2edc9 2c 20 69 64 78 4e 65 77 31 29 3b 0a 20 20 20 20  , idxNew1);.    
2edca 70 4e 65 77 45 78 70 72 32 20 3d 20 73 71 6c 69  pNewExpr2 = sqli
2edcb 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2edcc 20 54 4b 5f 4c 54 2c 20 73 71 6c 69 74 65 33 45   TK_LT, sqlite3E
2edcd 78 70 72 44 75 70 28 64 62 2c 70 4c 65 66 74 2c  xprDup(db,pLeft,
2edce 30 29 2c 70 53 74 72 32 2c 30 29 3b 0a 20 20 20  0),pStr2,0);.   
2edcf 20 69 64 78 4e 65 77 32 20 3d 20 77 68 65 72 65   idxNew2 = where
2edd0 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57 43  ClauseInsert(pWC
2edd1 2c 20 70 4e 65 77 45 78 70 72 32 2c 20 54 45 52  , pNewExpr2, TER
2edd2 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44  M_VIRTUAL|TERM_D
2edd3 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 74 65 73  YNAMIC);.    tes
2edd4 74 63 61 73 65 28 20 69 64 78 4e 65 77 32 3d 3d  tcase( idxNew2==
2edd5 30 20 29 3b 0a 20 20 20 20 65 78 70 72 41 6e 61  0 );.    exprAna
2edd6 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 43 2c 20  lyze(pSrc, pWC, 
2edd7 69 64 78 4e 65 77 32 29 3b 0a 20 20 20 20 70 54  idxNew2);.    pT
2edd8 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64  erm = &pWC->a[id
2edd9 78 54 65 72 6d 5d 3b 0a 20 20 20 20 69 66 28 20  xTerm];.    if( 
2edda 69 73 43 6f 6d 70 6c 65 74 65 20 29 7b 0a 20 20  isComplete ){.  
2eddb 20 20 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e 65      pWC->a[idxNe
2eddc 77 31 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69 64  w1].iParent = id
2eddd 78 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 57 43  xTerm;.      pWC
2edde 2d 3e 61 5b 69 64 78 4e 65 77 32 5d 2e 69 50 61  ->a[idxNew2].iPa
2eddf 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a  rent = idxTerm;.
2ede0 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68        pTerm->nCh
2ede1 69 6c 64 20 3d 20 32 3b 0a 20 20 20 20 7d 0a 20  ild = 2;.    }. 
2ede2 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c   }.#endif /* SQL
2ede3 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50  ITE_OMIT_LIKE_OP
2ede4 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 23  TIMIZATION */..#
2ede5 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2ede6 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
2ede7 20 20 2f 2a 20 41 64 64 20 61 20 57 4f 5f 4d 41    /* Add a WO_MA
2ede8 54 43 48 20 61 75 78 69 6c 69 61 72 79 20 74 65  TCH auxiliary te
2ede9 72 6d 20 74 6f 20 74 68 65 20 63 6f 6e 73 74 72  rm to the constr
2edea 61 69 6e 74 20 73 65 74 20 69 66 20 74 68 65 0a  aint set if the.
2edeb 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 65 78 70    ** current exp
2edec 72 65 73 73 69 6f 6e 20 69 73 20 6f 66 20 74 68  ression is of th
2eded 65 20 66 6f 72 6d 3a 20 20 63 6f 6c 75 6d 6e 20  e form:  column 
2edee 4d 41 54 43 48 20 65 78 70 72 2e 0a 20 20 2a 2a  MATCH expr..  **
2edef 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f   This informatio
2edf0 6e 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65  n is used by the
2edf1 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65 74 68   xBestIndex meth
2edf2 6f 64 73 20 6f 66 0a 20 20 2a 2a 20 76 69 72 74  ods of.  ** virt
2edf3 75 61 6c 20 74 61 62 6c 65 73 2e 20 20 54 68 65  ual tables.  The
2edf4 20 6e 61 74 69 76 65 20 71 75 65 72 79 20 6f 70   native query op
2edf5 74 69 6d 69 7a 65 72 20 64 6f 65 73 20 6e 6f 74  timizer does not
2edf6 20 61 74 74 65 6d 70 74 0a 20 20 2a 2a 20 74 6f   attempt.  ** to
2edf7 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 77 69 74   do anything wit
2edf8 68 20 4d 41 54 43 48 20 66 75 6e 63 74 69 6f 6e  h MATCH function
2edf9 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 73  s..  */.  if( is
2edfa 4d 61 74 63 68 4f 66 43 6f 6c 75 6d 6e 28 70 45  MatchOfColumn(pE
2edfb 78 70 72 29 20 29 7b 0a 20 20 20 20 69 6e 74 20  xpr) ){.    int 
2edfc 69 64 78 4e 65 77 3b 0a 20 20 20 20 45 78 70 72  idxNew;.    Expr
2edfd 20 2a 70 52 69 67 68 74 2c 20 2a 70 4c 65 66 74   *pRight, *pLeft
2edfe 3b 0a 20 20 20 20 57 68 65 72 65 54 65 72 6d 20  ;.    WhereTerm 
2edff 2a 70 4e 65 77 54 65 72 6d 3b 0a 20 20 20 20 42  *pNewTerm;.    B
2ee00 69 74 6d 61 73 6b 20 70 72 65 72 65 71 43 6f 6c  itmask prereqCol
2ee01 75 6d 6e 2c 20 70 72 65 72 65 71 45 78 70 72 3b  umn, prereqExpr;
2ee02 0a 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20 70  ..    pRight = p
2ee03 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74 2d 3e 61  Expr->x.pList->a
2ee04 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 70  [0].pExpr;.    p
2ee05 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 78 2e  Left = pExpr->x.
2ee06 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70  pList->a[1].pExp
2ee07 72 3b 0a 20 20 20 20 70 72 65 72 65 71 45 78 70  r;.    prereqExp
2ee08 72 20 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61  r = exprTableUsa
2ee09 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 52 69  ge(pMaskSet, pRi
2ee0a 67 68 74 29 3b 0a 20 20 20 20 70 72 65 72 65 71  ght);.    prereq
2ee0b 43 6f 6c 75 6d 6e 20 3d 20 65 78 70 72 54 61 62  Column = exprTab
2ee0c 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
2ee0d 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 69 66  , pLeft);.    if
2ee0e 28 20 28 70 72 65 72 65 71 45 78 70 72 20 26 20  ( (prereqExpr & 
2ee0f 70 72 65 72 65 71 43 6f 6c 75 6d 6e 29 3d 3d 30  prereqColumn)==0
2ee10 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a   ){.      Expr *
2ee11 70 4e 65 77 45 78 70 72 3b 0a 20 20 20 20 20 20  pNewExpr;.      
2ee12 70 4e 65 77 45 78 70 72 20 3d 20 73 71 6c 69 74  pNewExpr = sqlit
2ee13 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2ee14 54 4b 5f 4d 41 54 43 48 2c 20 0a 20 20 20 20 20  TK_MATCH, .     
2ee15 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ee16 20 20 20 20 20 20 20 20 20 30 2c 20 73 71 6c 69           0, sqli
2ee17 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
2ee18 52 69 67 68 74 2c 20 30 29 2c 20 30 29 3b 0a 20  Right, 0), 0);. 
2ee19 20 20 20 20 20 69 64 78 4e 65 77 20 3d 20 77 68       idxNew = wh
2ee1a 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28  ereClauseInsert(
2ee1b 70 57 43 2c 20 70 4e 65 77 45 78 70 72 2c 20 54  pWC, pNewExpr, T
2ee1c 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52 4d  ERM_VIRTUAL|TERM
2ee1d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20 20  _DYNAMIC);.     
2ee1e 20 74 65 73 74 63 61 73 65 28 20 69 64 78 4e 65   testcase( idxNe
2ee1f 77 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 4e  w==0 );.      pN
2ee20 65 77 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61  ewTerm = &pWC->a
2ee21 5b 69 64 78 4e 65 77 5d 3b 0a 20 20 20 20 20 20  [idxNew];.      
2ee22 70 4e 65 77 54 65 72 6d 2d 3e 70 72 65 72 65 71  pNewTerm->prereq
2ee23 52 69 67 68 74 20 3d 20 70 72 65 72 65 71 45 78  Right = prereqEx
2ee24 70 72 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65  pr;.      pNewTe
2ee25 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 3d  rm->leftCursor =
2ee26 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65 3b 0a   pLeft->iTable;.
2ee27 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e        pNewTerm->
2ee28 75 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 70  u.leftColumn = p
2ee29 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20  Left->iColumn;. 
2ee2a 20 20 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 65       pNewTerm->e
2ee2b 4f 70 65 72 61 74 6f 72 20 3d 20 57 4f 5f 4d 41  Operator = WO_MA
2ee2c 54 43 48 3b 0a 20 20 20 20 20 20 70 4e 65 77 54  TCH;.      pNewT
2ee2d 65 72 6d 2d 3e 69 50 61 72 65 6e 74 20 3d 20 69  erm->iParent = i
2ee2e 64 78 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 54  dxTerm;.      pT
2ee2f 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64  erm = &pWC->a[id
2ee30 78 54 65 72 6d 5d 3b 0a 20 20 20 20 20 20 70 54  xTerm];.      pT
2ee31 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d 20 31 3b  erm->nChild = 1;
2ee32 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 77 74  .      pTerm->wt
2ee33 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f  Flags |= TERM_CO
2ee34 50 49 45 44 3b 0a 20 20 20 20 20 20 70 4e 65 77  PIED;.      pNew
2ee35 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 20  Term->prereqAll 
2ee36 3d 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 41  = pTerm->prereqA
2ee37 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  ll;.    }.  }.#e
2ee38 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2ee39 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2ee3a 20 2a 2f 0a 0a 20 20 2f 2a 20 50 72 65 76 65 6e   */..  /* Preven
2ee3b 74 20 4f 4e 20 63 6c 61 75 73 65 20 74 65 72 6d  t ON clause term
2ee3c 73 20 6f 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e  s of a LEFT JOIN
2ee3d 20 66 72 6f 6d 20 62 65 69 6e 67 20 75 73 65 64   from being used
2ee3e 20 74 6f 20 64 72 69 76 65 0a 20 20 2a 2a 20 61   to drive.  ** a
2ee3f 6e 20 69 6e 64 65 78 20 66 6f 72 20 74 61 62 6c  n index for tabl
2ee40 65 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f  es to the left o
2ee41 66 20 74 68 65 20 6a 6f 69 6e 2e 0a 20 20 2a 2f  f the join..  */
2ee42 0a 20 20 70 54 65 72 6d 2d 3e 70 72 65 72 65 71  .  pTerm->prereq
2ee43 52 69 67 68 74 20 7c 3d 20 65 78 74 72 61 52 69  Right |= extraRi
2ee44 67 68 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ght;.}../*.** Re
2ee45 74 75 72 6e 20 54 52 55 45 20 69 66 20 61 6e 79  turn TRUE if any
2ee46 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
2ee47 6f 6e 73 20 69 6e 20 70 4c 69 73 74 2d 3e 61 5b  ons in pList->a[
2ee48 69 46 69 72 73 74 2e 2e 2e 5d 20 63 6f 6e 74 61  iFirst...] conta
2ee49 69 6e 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e 63  in.** a referenc
2ee4a 65 20 74 6f 20 61 6e 79 20 74 61 62 6c 65 20 6f  e to any table o
2ee4b 74 68 65 72 20 74 68 61 6e 20 74 68 65 20 69 42  ther than the iB
2ee4c 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74  ase table..*/.st
2ee4d 61 74 69 63 20 69 6e 74 20 72 65 66 65 72 65 6e  atic int referen
2ee4e 63 65 73 4f 74 68 65 72 54 61 62 6c 65 73 28 0a  cesOtherTables(.
2ee4f 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73    ExprList *pLis
2ee50 74 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53  t,          /* S
2ee51 65 61 72 63 68 20 65 78 70 72 65 73 73 69 6f 6e  earch expression
2ee52 73 20 69 6e 20 74 68 73 20 6c 69 73 74 20 2a 2f  s in ths list */
2ee53 0a 20 20 57 68 65 72 65 4d 61 73 6b 53 65 74 20  .  WhereMaskSet 
2ee54 2a 70 4d 61 73 6b 53 65 74 2c 20 20 20 2f 2a 20  *pMaskSet,   /* 
2ee55 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 74 61 62  Mapping from tab
2ee56 6c 65 73 20 74 6f 20 62 69 74 6d 61 70 73 20 2a  les to bitmaps *
2ee57 2f 0a 20 20 69 6e 74 20 69 46 69 72 73 74 2c 20  /.  int iFirst, 
2ee58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2ee59 20 42 65 20 73 65 61 72 63 68 69 6e 67 20 77 69   Be searching wi
2ee5a 74 68 20 74 68 65 20 69 46 69 72 73 74 2d 74 68  th the iFirst-th
2ee5b 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
2ee5c 20 69 6e 74 20 69 42 61 73 65 20 20 20 20 20 20   int iBase      
2ee5d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 67             /* Ig
2ee5e 6e 6f 72 65 20 72 65 66 65 72 65 6e 63 65 73 20  nore references 
2ee5f 74 6f 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f  to this table */
2ee60 0a 29 7b 0a 20 20 42 69 74 6d 61 73 6b 20 61 6c  .){.  Bitmask al
2ee61 6c 6f 77 65 64 20 3d 20 7e 67 65 74 4d 61 73 6b  lowed = ~getMask
2ee62 28 70 4d 61 73 6b 53 65 74 2c 20 69 42 61 73 65  (pMaskSet, iBase
2ee63 29 3b 0a 20 20 77 68 69 6c 65 28 20 69 46 69 72  );.  while( iFir
2ee64 73 74 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20  st<pList->nExpr 
2ee65 29 7b 0a 20 20 20 20 69 66 28 20 28 65 78 70 72  ){.    if( (expr
2ee66 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b  TableUsage(pMask
2ee67 53 65 74 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 46  Set, pList->a[iF
2ee68 69 72 73 74 2b 2b 5d 2e 70 45 78 70 72 29 26 61  irst++].pExpr)&a
2ee69 6c 6c 6f 77 65 64 29 21 3d 30 20 29 7b 0a 20 20  llowed)!=0 ){.  
2ee6a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2ee6b 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
2ee6c 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68   0;.}.../*.** Th
2ee6d 69 73 20 72 6f 75 74 69 6e 65 20 64 65 63 69 64  is routine decid
2ee6e 65 73 20 69 66 20 70 49 64 78 20 63 61 6e 20 62  es if pIdx can b
2ee6f 65 20 75 73 65 64 20 74 6f 20 73 61 74 69 73 66  e used to satisf
2ee70 79 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a 2a  y the ORDER BY.*
2ee71 2a 20 63 6c 61 75 73 65 2e 20 20 49 66 20 69 74  * clause.  If it
2ee72 20 63 61 6e 2c 20 69 74 20 72 65 74 75 72 6e 73   can, it returns
2ee73 20 31 2e 20 20 49 66 20 70 49 64 78 20 63 61 6e   1.  If pIdx can
2ee74 6e 6f 74 20 73 61 74 69 73 66 79 20 74 68 65 0a  not satisfy the.
2ee75 2a 2a 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  ** ORDER BY clau
2ee76 73 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  se, this routine
2ee77 20 72 65 74 75 72 6e 73 20 30 2e 0a 2a 2a 0a 2a   returns 0..**.*
2ee78 2a 20 70 4f 72 64 65 72 42 79 20 69 73 20 61 6e  * pOrderBy is an
2ee79 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2ee7a 20 66 72 6f 6d 20 61 20 53 45 4c 45 43 54 20 73   from a SELECT s
2ee7b 74 61 74 65 6d 65 6e 74 2e 20 20 70 54 61 62 20  tatement.  pTab 
2ee7c 69 73 20 74 68 65 0a 2a 2a 20 6c 65 66 74 2d 6d  is the.** left-m
2ee7d 6f 73 74 20 74 61 62 6c 65 20 69 6e 20 74 68 65  ost table in the
2ee7e 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
2ee7f 74 68 61 74 20 73 61 6d 65 20 53 45 4c 45 43 54  that same SELECT
2ee80 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 0a 2a   statement and.*
2ee81 2a 20 74 68 65 20 74 61 62 6c 65 20 68 61 73 20  * the table has 
2ee82 61 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  a cursor number 
2ee83 6f 66 20 22 62 61 73 65 22 2e 20 20 70 49 64 78  of "base".  pIdx
2ee84 20 69 73 20 61 6e 20 69 6e 64 65 78 20 6f 6e 20   is an index on 
2ee85 70 54 61 62 2e 0a 2a 2a 0a 2a 2a 20 6e 45 71 43  pTab..**.** nEqC
2ee86 6f 6c 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ol is the number
2ee87 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 70   of columns of p
2ee88 49 64 78 20 74 68 61 74 20 61 72 65 20 75 73 65  Idx that are use
2ee89 64 20 61 73 20 65 71 75 61 6c 69 74 79 0a 2a 2a  d as equality.**
2ee8a 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 41   constraints.  A
2ee8b 6e 79 20 6f 66 20 74 68 65 73 65 20 63 6f 6c 75  ny of these colu
2ee8c 6d 6e 73 20 6d 61 79 20 62 65 20 6d 69 73 73 69  mns may be missi
2ee8d 6e 67 20 66 72 6f 6d 20 74 68 65 20 4f 52 44 45  ng from the ORDE
2ee8e 52 20 42 59 0a 2a 2a 20 63 6c 61 75 73 65 20 61  R BY.** clause a
2ee8f 6e 64 20 74 68 65 20 6d 61 74 63 68 20 63 61 6e  nd the match can
2ee90 20 73 74 69 6c 6c 20 62 65 20 61 20 73 75 63 63   still be a succ
2ee91 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 74  ess..**.** All t
2ee92 65 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44 45  erms of the ORDE
2ee93 52 20 42 59 20 74 68 61 74 20 6d 61 74 63 68 20  R BY that match 
2ee94 61 67 61 69 6e 73 74 20 74 68 65 20 69 6e 64 65  against the inde
2ee95 78 20 6d 75 73 74 20 62 65 20 65 69 74 68 65 72  x must be either
2ee96 0a 2a 2a 20 41 53 43 20 6f 72 20 44 45 53 43 2e  .** ASC or DESC.
2ee97 20 20 28 54 65 72 6d 73 20 6f 66 20 74 68 65 20    (Terms of the 
2ee98 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2ee99 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20  past the end of 
2ee9a 61 20 55 4e 49 51 55 45 0a 2a 2a 20 69 6e 64 65  a UNIQUE.** inde
2ee9b 78 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f  x do not need to
2ee9c 20 73 61 74 69 73 66 79 20 74 68 69 73 20 63 6f   satisfy this co
2ee9d 6e 73 74 72 61 69 6e 74 2e 29 20 20 54 68 65 20  nstraint.)  The 
2ee9e 2a 70 62 52 65 76 20 76 61 6c 75 65 20 69 73 0a  *pbRev value is.
2ee9f 2a 2a 20 73 65 74 20 74 6f 20 31 20 69 66 20 74  ** set to 1 if t
2eea0 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2eea1 73 65 20 69 73 20 61 6c 6c 20 44 45 53 43 20 61  se is all DESC a
2eea2 6e 64 20 69 74 20 69 73 20 73 65 74 20 74 6f 20  nd it is set to 
2eea3 30 20 69 66 0a 2a 2a 20 74 68 65 20 4f 52 44 45  0 if.** the ORDE
2eea4 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 61  R BY clause is a
2eea5 6c 6c 20 41 53 43 2e 0a 2a 2f 0a 73 74 61 74 69  ll ASC..*/.stati
2eea6 63 20 69 6e 74 20 69 73 53 6f 72 74 69 6e 67 49  c int isSortingI
2eea7 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70  ndex(.  Parse *p
2eea8 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
2eea9 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
2eeaa 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 4d 61 73  xt */.  WhereMas
2eeab 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20  kSet *pMaskSet, 
2eeac 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20  /* Mapping from 
2eead 74 61 62 6c 65 20 63 75 72 73 6f 72 20 6e 75 6d  table cursor num
2eeae 62 65 72 73 20 74 6f 20 62 69 74 6d 61 70 73 20  bers to bitmaps 
2eeaf 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  */.  Index *pIdx
2eeb0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
2eeb1 54 68 65 20 69 6e 64 65 78 20 77 65 20 61 72 65  The index we are
2eeb2 20 74 65 73 74 69 6e 67 20 2a 2f 0a 20 20 69 6e   testing */.  in
2eeb3 74 20 62 61 73 65 2c 20 20 20 20 20 20 20 20 20  t base,         
2eeb4 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20        /* Cursor 
2eeb5 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74  number for the t
2eeb6 61 62 6c 65 20 74 6f 20 62 65 20 73 6f 72 74 65  able to be sorte
2eeb7 64 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  d */.  ExprList 
2eeb8 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 2f  *pOrderBy,     /
2eeb9 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63  * The ORDER BY c
2eeba 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e  lause */.  int n
2eebb 45 71 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20  EqCol,          
2eebc 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2eebd 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 73 20 77 69  index columns wi
2eebe 74 68 20 3d 3d 20 63 6f 6e 73 74 72 61 69 6e 74  th == constraint
2eebf 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 62 52 65  s */.  int *pbRe
2eec0 76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  v              /
2eec1 2a 20 53 65 74 20 74 6f 20 31 20 69 66 20 4f 52  * Set to 1 if OR
2eec2 44 45 52 20 42 59 20 69 73 20 44 45 53 43 20 2a  DER BY is DESC *
2eec3 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  /.){.  int i, j;
2eec4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eec5 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
2eec6 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74  ounters */.  int
2eec7 20 73 6f 72 74 4f 72 64 65 72 20 3d 20 30 3b 20   sortOrder = 0; 
2eec8 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2eec9 58 4f 52 20 6f 66 20 69 6e 64 65 78 20 61 6e 64  XOR of index and
2eeca 20 4f 52 44 45 52 20 42 59 20 73 6f 72 74 20 64   ORDER BY sort d
2eecb 69 72 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  irection */.  in
2eecc 74 20 6e 54 65 72 6d 3b 20 20 20 20 20 20 20 20  t nTerm;        
2eecd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2eece 20 4e 75 6d 62 65 72 20 6f 66 20 4f 52 44 45 52   Number of ORDER
2eecf 20 42 59 20 74 65 72 6d 73 20 2a 2f 0a 20 20 73   BY terms */.  s
2eed0 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
2eed1 74 65 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 2f  tem *pTerm;    /
2eed2 2a 20 41 20 74 65 72 6d 20 6f 66 20 74 68 65 20  * A term of the 
2eed3 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2eed4 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
2eed5 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a   = pParse->db;..
2eed6 20 20 61 73 73 65 72 74 28 20 70 4f 72 64 65 72    assert( pOrder
2eed7 42 79 21 3d 30 20 29 3b 0a 20 20 6e 54 65 72 6d  By!=0 );.  nTerm
2eed8 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78   = pOrderBy->nEx
2eed9 70 72 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 54  pr;.  assert( nT
2eeda 65 72 6d 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 41  erm>0 );..  /* A
2eedb 72 67 75 6d 65 6e 74 20 70 49 64 78 20 6d 75 73  rgument pIdx mus
2eedc 74 20 65 69 74 68 65 72 20 70 6f 69 6e 74 20 74  t either point t
2eedd 6f 20 61 20 27 72 65 61 6c 27 20 6e 61 6d 65 64  o a 'real' named
2eede 20 69 6e 64 65 78 20 73 74 72 75 63 74 75 72 65   index structure
2eedf 2c 20 0a 20 20 2a 2a 20 6f 72 20 61 6e 20 69 6e  , .  ** or an in
2eee0 64 65 78 20 73 74 72 75 63 74 75 72 65 20 61 6c  dex structure al
2eee1 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 20 73  located on the s
2eee2 74 61 63 6b 20 62 79 20 62 65 73 74 42 74 72 65  tack by bestBtre
2eee3 65 49 6e 64 65 78 28 29 20 74 6f 0a 20 20 2a 2a  eIndex() to.  **
2eee4 20 72 65 70 72 65 73 65 6e 74 20 74 68 65 20 72   represent the r
2eee5 6f 77 69 64 20 69 6e 64 65 78 20 74 68 61 74 20  owid index that 
2eee6 69 73 20 70 61 72 74 20 6f 66 20 65 76 65 72 79  is part of every
2eee7 20 74 61 62 6c 65 2e 20 20 2a 2f 0a 20 20 61 73   table.  */.  as
2eee8 73 65 72 74 28 20 70 49 64 78 2d 3e 7a 4e 61 6d  sert( pIdx->zNam
2eee9 65 20 7c 7c 20 28 70 49 64 78 2d 3e 6e 43 6f 6c  e || (pIdx->nCol
2eeea 75 6d 6e 3d 3d 31 20 26 26 20 70 49 64 78 2d 3e  umn==1 && pIdx->
2eeeb 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 2d 31 29  aiColumn[0]==-1)
2eeec 20 29 3b 0a 0a 20 20 2f 2a 20 4d 61 74 63 68 20   );..  /* Match 
2eeed 74 65 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44  terms of the ORD
2eeee 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 67 61  ER BY clause aga
2eeef 69 6e 73 74 20 63 6f 6c 75 6d 6e 73 20 6f 66 0a  inst columns of.
2eef0 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78 2e 0a    ** the index..
2eef1 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 65 20 74    **.  ** Note t
2eef2 68 61 74 20 69 6e 64 69 63 65 73 20 68 61 76 65  hat indices have
2eef3 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 72   pIdx->nColumn r
2eef4 65 67 75 6c 61 72 20 63 6f 6c 75 6d 6e 73 20 70  egular columns p
2eef5 6c 75 73 0a 20 20 2a 2a 20 6f 6e 65 20 61 64 64  lus.  ** one add
2eef6 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d 6e 20 63  itional column c
2eef7 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f  ontaining the ro
2eef8 77 69 64 2e 20 20 54 68 65 20 72 6f 77 69 64 20  wid.  The rowid 
2eef9 63 6f 6c 75 6d 6e 0a 20 20 2a 2a 20 6f 66 20 74  column.  ** of t
2eefa 68 65 20 69 6e 64 65 78 20 69 73 20 61 6c 73 6f  he index is also
2eefb 20 61 6c 6c 6f 77 65 64 20 74 6f 20 6d 61 74 63   allowed to matc
2eefc 68 20 61 67 61 69 6e 73 74 20 74 68 65 20 4f 52  h against the OR
2eefd 44 45 52 20 42 59 0a 20 20 2a 2a 20 63 6c 61 75  DER BY.  ** clau
2eefe 73 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  se..  */.  for(i
2eeff 3d 6a 3d 30 2c 20 70 54 65 72 6d 3d 70 4f 72 64  =j=0, pTerm=pOrd
2ef00 65 72 42 79 2d 3e 61 3b 20 6a 3c 6e 54 65 72 6d  erBy->a; j<nTerm
2ef01 20 26 26 20 69 3c 3d 70 49 64 78 2d 3e 6e 43 6f   && i<=pIdx->nCo
2ef02 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  lumn; i++){.    
2ef03 45 78 70 72 20 2a 70 45 78 70 72 3b 20 20 20 20  Expr *pExpr;    
2ef04 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
2ef05 73 69 6f 6e 20 6f 66 20 74 68 65 20 4f 52 44 45  sion of the ORDE
2ef06 52 20 42 59 20 70 54 65 72 6d 20 2a 2f 0a 20 20  R BY pTerm */.  
2ef07 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
2ef08 3b 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c  ;    /* The coll
2ef09 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f  ating sequence o
2ef0a 66 20 70 45 78 70 72 20 2a 2f 0a 20 20 20 20 69  f pExpr */.    i
2ef0b 6e 74 20 74 65 72 6d 53 6f 72 74 4f 72 64 65 72  nt termSortOrder
2ef0c 3b 20 2f 2a 20 53 6f 72 74 20 6f 72 64 65 72 20  ; /* Sort order 
2ef0d 66 6f 72 20 74 68 69 73 20 74 65 72 6d 20 2a 2f  for this term */
2ef0e 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  .    int iColumn
2ef0f 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 69  ;       /* The i
2ef10 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68  -th column of th
2ef11 65 20 69 6e 64 65 78 2e 20 20 2d 31 20 66 6f 72  e index.  -1 for
2ef12 20 72 6f 77 69 64 20 2a 2f 0a 20 20 20 20 69 6e   rowid */.    in
2ef13 74 20 69 53 6f 72 74 4f 72 64 65 72 3b 20 20 20  t iSortOrder;   
2ef14 20 2f 2a 20 31 20 66 6f 72 20 44 45 53 43 2c 20   /* 1 for DESC, 
2ef15 30 20 66 6f 72 20 41 53 43 20 6f 6e 20 74 68 65  0 for ASC on the
2ef16 20 69 2d 74 68 20 69 6e 64 65 78 20 74 65 72 6d   i-th index term
2ef17 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68   */.    const ch
2ef18 61 72 20 2a 7a 43 6f 6c 6c 3b 20 2f 2a 20 4e 61  ar *zColl; /* Na
2ef19 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74  me of the collat
2ef1a 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72  ing sequence for
2ef1b 20 69 2d 74 68 20 69 6e 64 65 78 20 74 65 72 6d   i-th index term
2ef1c 20 2a 2f 0a 0a 20 20 20 20 70 45 78 70 72 20 3d   */..    pExpr =
2ef1d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20   pTerm->pExpr;. 
2ef1e 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70     if( pExpr->op
2ef1f 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70  !=TK_COLUMN || p
2ef20 45 78 70 72 2d 3e 69 54 61 62 6c 65 21 3d 62 61  Expr->iTable!=ba
2ef21 73 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43  se ){.      /* C
2ef22 61 6e 20 6e 6f 74 20 75 73 65 20 61 6e 20 69 6e  an not use an in
2ef23 64 65 78 20 73 6f 72 74 20 6f 6e 20 61 6e 79 74  dex sort on anyt
2ef24 68 69 6e 67 20 74 68 61 74 20 69 73 20 6e 6f 74  hing that is not
2ef25 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65   a column in the
2ef26 0a 20 20 20 20 20 20 2a 2a 20 6c 65 66 74 2d 6d  .      ** left-m
2ef27 6f 73 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65  ost table of the
2ef28 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a   FROM clause */.
2ef29 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2ef2a 20 7d 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73   }.    pColl = s
2ef2b 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65  qlite3ExprCollSe
2ef2c 71 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 29  q(pParse, pExpr)
2ef2d 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c  ;.    if( !pColl
2ef2e 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 20   ){.      pColl 
2ef2f 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b  = db->pDfltColl;
2ef30 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
2ef31 49 64 78 2d 3e 7a 4e 61 6d 65 20 26 26 20 69 3c  Idx->zName && i<
2ef32 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b  pIdx->nColumn ){
2ef33 0a 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d  .      iColumn =
2ef34 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b   pIdx->aiColumn[
2ef35 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 69 43  i];.      if( iC
2ef36 6f 6c 75 6d 6e 3d 3d 70 49 64 78 2d 3e 70 54 61  olumn==pIdx->pTa
2ef37 62 6c 65 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20  ble->iPKey ){.  
2ef38 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20        iColumn = 
2ef39 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  -1;.      }.    
2ef3a 20 20 69 53 6f 72 74 4f 72 64 65 72 20 3d 20 70    iSortOrder = p
2ef3b 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b  Idx->aSortOrder[
2ef3c 69 5d 3b 0a 20 20 20 20 20 20 7a 43 6f 6c 6c 20  i];.      zColl 
2ef3d 3d 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 69  = pIdx->azColl[i
2ef3e 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  ];.    }else{.  
2ef3f 20 20 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 2d 31      iColumn = -1
2ef40 3b 0a 20 20 20 20 20 20 69 53 6f 72 74 4f 72 64  ;.      iSortOrd
2ef41 65 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 7a 43  er = 0;.      zC
2ef42 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61  oll = pColl->zNa
2ef43 6d 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  me;.    }.    if
2ef44 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  ( pExpr->iColumn
2ef45 21 3d 69 43 6f 6c 75 6d 6e 20 7c 7c 20 73 71 6c  !=iColumn || sql
2ef46 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c  ite3StrICmp(pCol
2ef47 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 6c 29  l->zName, zColl)
2ef48 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54 65 72   ){.      /* Ter
2ef49 6d 20 6a 20 6f 66 20 74 68 65 20 4f 52 44 45 52  m j of the ORDER
2ef4a 20 42 59 20 63 6c 61 75 73 65 20 64 6f 65 73 20   BY clause does 
2ef4b 6e 6f 74 20 6d 61 74 63 68 20 63 6f 6c 75 6d 6e  not match column
2ef4c 20 69 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20   i of the index 
2ef4d 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 69 3c 6e  */.      if( i<n
2ef4e 45 71 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20  EqCol ){.       
2ef4f 20 2f 2a 20 49 66 20 61 6e 20 69 6e 64 65 78 20   /* If an index 
2ef50 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 63  column that is c
2ef51 6f 6e 73 74 72 61 69 6e 65 64 20 62 79 20 3d 3d  onstrained by ==
2ef52 20 66 61 69 6c 73 20 74 6f 20 6d 61 74 63 68 20   fails to match 
2ef53 61 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 52  an.        ** OR
2ef54 44 45 52 20 42 59 20 74 65 72 6d 2c 20 74 68 61  DER BY term, tha
2ef55 74 20 69 73 20 4f 4b 2e 20 20 4a 75 73 74 20 69  t is OK.  Just i
2ef56 67 6e 6f 72 65 20 74 68 61 74 20 63 6f 6c 75 6d  gnore that colum
2ef57 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78 0a 20  n of the index. 
2ef58 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
2ef59 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
2ef5a 20 20 7d 65 6c 73 65 20 69 66 28 20 69 3d 3d 70    }else if( i==p
2ef5b 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a  Idx->nColumn ){.
2ef5c 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
2ef5d 20 63 6f 6c 75 6d 6e 20 69 20 69 73 20 74 68 65   column i is the
2ef5e 20 72 6f 77 69 64 2e 20 20 41 6c 6c 20 6f 74 68   rowid.  All oth
2ef5f 65 72 20 74 65 72 6d 73 20 6d 61 74 63 68 2e 20  er terms match. 
2ef60 2a 2f 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  */.        break
2ef61 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
2ef62 20 20 20 20 20 20 20 2f 2a 20 49 66 20 61 6e 20         /* If an 
2ef63 69 6e 64 65 78 20 63 6f 6c 75 6d 6e 20 66 61 69  index column fai
2ef64 6c 73 20 74 6f 20 6d 61 74 63 68 20 61 6e 64 20  ls to match and 
2ef65 69 73 20 6e 6f 74 20 63 6f 6e 73 74 72 61 69 6e  is not constrain
2ef66 65 64 20 62 79 20 3d 3d 0a 20 20 20 20 20 20 20  ed by ==.       
2ef67 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 69 6e 64   ** then the ind
2ef68 65 78 20 63 61 6e 6e 6f 74 20 73 61 74 69 73 66  ex cannot satisf
2ef69 79 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  y the ORDER BY c
2ef6a 6f 6e 73 74 72 61 69 6e 74 2e 0a 20 20 20 20 20  onstraint..     
2ef6b 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65     */.        re
2ef6c 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a  turn 0;.      }.
2ef6d 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
2ef6e 28 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64  ( pIdx->aSortOrd
2ef6f 65 72 21 3d 30 20 7c 7c 20 69 43 6f 6c 75 6d 6e  er!=0 || iColumn
2ef70 3d 3d 2d 31 20 29 3b 0a 20 20 20 20 61 73 73 65  ==-1 );.    asse
2ef71 72 74 28 20 70 54 65 72 6d 2d 3e 73 6f 72 74 4f  rt( pTerm->sortO
2ef72 72 64 65 72 3d 3d 30 20 7c 7c 20 70 54 65 72 6d  rder==0 || pTerm
2ef73 2d 3e 73 6f 72 74 4f 72 64 65 72 3d 3d 31 20 29  ->sortOrder==1 )
2ef74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 53  ;.    assert( iS
2ef75 6f 72 74 4f 72 64 65 72 3d 3d 30 20 7c 7c 20 69  ortOrder==0 || i
2ef76 53 6f 72 74 4f 72 64 65 72 3d 3d 31 20 29 3b 0a  SortOrder==1 );.
2ef77 20 20 20 20 74 65 72 6d 53 6f 72 74 4f 72 64 65      termSortOrde
2ef78 72 20 3d 20 69 53 6f 72 74 4f 72 64 65 72 20 5e  r = iSortOrder ^
2ef79 20 70 54 65 72 6d 2d 3e 73 6f 72 74 4f 72 64 65   pTerm->sortOrde
2ef7a 72 3b 0a 20 20 20 20 69 66 28 20 69 3e 6e 45 71  r;.    if( i>nEq
2ef7b 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28  Col ){.      if(
2ef7c 20 74 65 72 6d 53 6f 72 74 4f 72 64 65 72 21 3d   termSortOrder!=
2ef7d 73 6f 72 74 4f 72 64 65 72 20 29 7b 0a 20 20 20  sortOrder ){.   
2ef7e 20 20 20 20 20 2f 2a 20 49 6e 64 69 63 65 73 20       /* Indices 
2ef7f 63 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 64  can only be used
2ef80 20 69 66 20 61 6c 6c 20 4f 52 44 45 52 20 42 59   if all ORDER BY
2ef81 20 74 65 72 6d 73 20 70 61 73 74 20 74 68 65 0a   terms past the.
2ef82 20 20 20 20 20 20 20 20 2a 2a 20 65 71 75 61 6c          ** equal
2ef83 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  ity constraints 
2ef84 61 72 65 20 61 6c 6c 20 65 69 74 68 65 72 20 44  are all either D
2ef85 45 53 43 20 6f 72 20 41 53 43 2e 20 2a 2f 0a 20  ESC or ASC. */. 
2ef86 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b         return 0;
2ef87 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
2ef88 73 65 7b 0a 20 20 20 20 20 20 73 6f 72 74 4f 72  se{.      sortOr
2ef89 64 65 72 20 3d 20 74 65 72 6d 53 6f 72 74 4f 72  der = termSortOr
2ef8a 64 65 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6a  der;.    }.    j
2ef8b 2b 2b 3b 0a 20 20 20 20 70 54 65 72 6d 2b 2b 3b  ++;.    pTerm++;
2ef8c 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e  .    if( iColumn
2ef8d 3c 30 20 26 26 20 21 72 65 66 65 72 65 6e 63 65  <0 && !reference
2ef8e 73 4f 74 68 65 72 54 61 62 6c 65 73 28 70 4f 72  sOtherTables(pOr
2ef8f 64 65 72 42 79 2c 20 70 4d 61 73 6b 53 65 74 2c  derBy, pMaskSet,
2ef90 20 6a 2c 20 62 61 73 65 29 20 29 7b 0a 20 20 20   j, base) ){.   
2ef91 20 20 20 2f 2a 20 49 66 20 74 68 65 20 69 6e 64     /* If the ind
2ef92 65 78 65 64 20 63 6f 6c 75 6d 6e 20 69 73 20 74  exed column is t
2ef93 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 61  he primary key a
2ef94 6e 64 20 65 76 65 72 79 74 68 69 6e 67 20 6d 61  nd everything ma
2ef95 74 63 68 65 73 0a 20 20 20 20 20 20 2a 2a 20 73  tches.      ** s
2ef96 6f 20 66 61 72 20 61 6e 64 20 6e 6f 6e 65 20 6f  o far and none o
2ef97 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 74  f the ORDER BY t
2ef98 65 72 6d 73 20 74 6f 20 74 68 65 20 72 69 67 68  erms to the righ
2ef99 74 20 72 65 66 65 72 65 6e 63 65 20 6f 74 68 65  t reference othe
2ef9a 72 0a 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65  r.      ** table
2ef9b 73 20 69 6e 20 74 68 65 20 6a 6f 69 6e 2c 20 74  s in the join, t
2ef9c 68 65 6e 20 77 65 20 61 72 65 20 61 73 73 75 72  hen we are assur
2ef9d 65 64 20 74 68 61 74 20 74 68 65 20 69 6e 64 65  ed that the inde
2ef9e 78 20 63 61 6e 20 62 65 20 75 73 65 64 20 0a 20  x can be used . 
2ef9f 20 20 20 20 20 2a 2a 20 74 6f 20 73 6f 72 74 20       ** to sort 
2efa0 62 65 63 61 75 73 65 20 74 68 65 20 70 72 69 6d  because the prim
2efa1 61 72 79 20 6b 65 79 20 69 73 20 75 6e 69 71 75  ary key is uniqu
2efa2 65 20 61 6e 64 20 73 6f 20 6e 6f 6e 65 20 6f 66  e and so none of
2efa3 20 74 68 65 20 6f 74 68 65 72 0a 20 20 20 20 20   the other.     
2efa4 20 2a 2a 20 63 6f 6c 75 6d 6e 73 20 77 69 6c 6c   ** columns will
2efa5 20 6d 61 6b 65 20 61 6e 79 20 64 69 66 66 65 72   make any differ
2efa6 65 6e 63 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ence.      */.  
2efa7 20 20 20 20 6a 20 3d 20 6e 54 65 72 6d 3b 0a 20      j = nTerm;. 
2efa8 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 62 52     }.  }..  *pbR
2efa9 65 76 20 3d 20 73 6f 72 74 4f 72 64 65 72 21 3d  ev = sortOrder!=
2efaa 30 3b 0a 20 20 69 66 28 20 6a 3e 3d 6e 54 65 72  0;.  if( j>=nTer
2efab 6d 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6c 6c 20  m ){.    /* All 
2efac 74 65 72 6d 73 20 6f 66 20 74 68 65 20 4f 52 44  terms of the ORD
2efad 45 52 20 42 59 20 63 6c 61 75 73 65 20 61 72 65  ER BY clause are
2efae 20 63 6f 76 65 72 65 64 20 62 79 20 74 68 69 73   covered by this
2efaf 20 69 6e 64 65 78 20 73 6f 0a 20 20 20 20 2a 2a   index so.    **
2efb0 20 74 68 69 73 20 69 6e 64 65 78 20 63 61 6e 20   this index can 
2efb1 62 65 20 75 73 65 64 20 66 6f 72 20 73 6f 72 74  be used for sort
2efb2 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75  ing. */.    retu
2efb3 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 1;.  }.  if( 
2efb4 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f  pIdx->onError!=O
2efb5 45 5f 4e 6f 6e 65 20 26 26 20 69 3d 3d 70 49 64  E_None && i==pId
2efb6 78 2d 3e 6e 43 6f 6c 75 6d 6e 0a 20 20 20 20 20  x->nColumn.     
2efb7 20 26 26 20 21 72 65 66 65 72 65 6e 63 65 73 4f   && !referencesO
2efb8 74 68 65 72 54 61 62 6c 65 73 28 70 4f 72 64 65  therTables(pOrde
2efb9 72 42 79 2c 20 70 4d 61 73 6b 53 65 74 2c 20 6a  rBy, pMaskSet, j
2efba 2c 20 62 61 73 65 29 20 29 7b 0a 20 20 20 20 2f  , base) ){.    /
2efbb 2a 20 41 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74  * All terms of t
2efbc 68 69 73 20 69 6e 64 65 78 20 6d 61 74 63 68 20  his index match 
2efbd 73 6f 6d 65 20 70 72 65 66 69 78 20 6f 66 20 74  some prefix of t
2efbe 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
2efbf 73 65 0a 20 20 20 20 2a 2a 20 61 6e 64 20 74 68  se.    ** and th
2efc0 65 20 69 6e 64 65 78 20 69 73 20 55 4e 49 51 55  e index is UNIQU
2efc1 45 20 61 6e 64 20 6e 6f 20 74 65 72 6d 73 20 6f  E and no terms o
2efc2 6e 20 74 68 65 20 74 61 69 6c 20 6f 66 20 74 68  n the tail of th
2efc3 65 20 4f 52 44 45 52 20 42 59 0a 20 20 20 20 2a  e ORDER BY.    *
2efc4 2a 20 63 6c 61 75 73 65 20 72 65 66 65 72 65 6e  * clause referen
2efc5 63 65 20 6f 74 68 65 72 20 74 61 62 6c 65 73 20  ce other tables 
2efc6 69 6e 20 61 20 6a 6f 69 6e 2e 20 20 49 66 20 74  in a join.  If t
2efc7 68 69 73 20 69 73 20 61 6c 6c 20 74 72 75 65 20  his is all true 
2efc8 74 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20  then.    ** the 
2efc9 6f 72 64 65 72 20 62 79 20 63 6c 61 75 73 65 20  order by clause 
2efca 69 73 20 73 75 70 65 72 66 6c 75 6f 75 73 2e 20  is superfluous. 
2efcb 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  */.    return 1;
2efcc 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
2efcd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61 72  .}../*.** Prepar
2efce 65 20 61 20 63 72 75 64 65 20 65 73 74 69 6d 61  e a crude estima
2efcf 74 65 20 6f 66 20 74 68 65 20 6c 6f 67 61 72 69  te of the logari
2efd0 74 68 6d 20 6f 66 20 74 68 65 20 69 6e 70 75 74  thm of the input
2efd1 20 76 61 6c 75 65 2e 0a 2a 2a 20 54 68 65 20 72   value..** The r
2efd2 65 73 75 6c 74 73 20 6e 65 65 64 20 6e 6f 74 20  esults need not 
2efd3 62 65 20 65 78 61 63 74 2e 20 20 54 68 69 73 20  be exact.  This 
2efd4 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72  is only used for
2efd5 20 65 73 74 69 6d 61 74 69 6e 67 0a 2a 2a 20 74   estimating.** t
2efd6 68 65 20 74 6f 74 61 6c 20 63 6f 73 74 20 6f 66  he total cost of
2efd7 20 70 65 72 66 6f 72 6d 69 6e 67 20 6f 70 65 72   performing oper
2efd8 61 74 69 6f 6e 73 20 77 69 74 68 20 4f 28 6c 6f  ations with O(lo
2efd9 67 4e 29 20 6f 72 20 4f 28 4e 6c 6f 67 4e 29 0a  gN) or O(NlogN).
2efda 2a 2a 20 63 6f 6d 70 6c 65 78 69 74 79 2e 20 20  ** complexity.  
2efdb 42 65 63 61 75 73 65 20 4e 20 69 73 20 6a 75 73  Because N is jus
2efdc 74 20 61 20 67 75 65 73 73 2c 20 69 74 20 69 73  t a guess, it is
2efdd 20 6e 6f 20 67 72 65 61 74 20 74 72 61 67 65 64   no great traged
2efde 79 20 69 66 0a 2a 2a 20 6c 6f 67 4e 20 69 73 20  y if.** logN is 
2efdf 61 20 6c 69 74 74 6c 65 20 6f 66 66 2e 0a 2a 2f  a little off..*/
2efe0 0a 73 74 61 74 69 63 20 64 6f 75 62 6c 65 20 65  .static double e
2efe1 73 74 4c 6f 67 28 64 6f 75 62 6c 65 20 4e 29 7b  stLog(double N){
2efe2 0a 20 20 64 6f 75 62 6c 65 20 6c 6f 67 4e 20 3d  .  double logN =
2efe3 20 31 3b 0a 20 20 64 6f 75 62 6c 65 20 78 20 3d   1;.  double x =
2efe4 20 31 30 3b 0a 20 20 77 68 69 6c 65 28 20 4e 3e   10;.  while( N>
2efe5 78 20 29 7b 0a 20 20 20 20 6c 6f 67 4e 20 2b 3d  x ){.    logN +=
2efe6 20 31 3b 0a 20 20 20 20 78 20 2a 3d 20 31 30 3b   1;.    x *= 10;
2efe7 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6c 6f  .  }.  return lo
2efe8 67 4e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 77 6f  gN;.}../*.** Two
2efe9 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 70 72   routines for pr
2efea 69 6e 74 69 6e 67 20 74 68 65 20 63 6f 6e 74 65  inting the conte
2efeb 6e 74 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33  nt of an sqlite3
2efec 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a 2a 20 73  _index_info.** s
2efed 74 72 75 63 74 75 72 65 2e 20 20 55 73 65 64 20  tructure.  Used 
2efee 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20  for testing and 
2efef 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 20  debugging only. 
2eff0 20 49 66 20 6e 65 69 74 68 65 72 0a 2a 2a 20 53   If neither.** S
2eff1 51 4c 49 54 45 5f 54 45 53 54 20 6f 72 20 53 51  QLITE_TEST or SQ
2eff2 4c 49 54 45 5f 44 45 42 55 47 20 61 72 65 20 64  LITE_DEBUG are d
2eff3 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65  efined, then the
2eff4 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61  se routines.** a
2eff5 72 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 69  re no-ops..*/.#i
2eff6 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
2eff7 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
2eff8 42 4c 45 29 20 26 26 20 64 65 66 69 6e 65 64 28  BLE) && defined(
2eff9 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 73 74  SQLITE_DEBUG).st
2effa 61 74 69 63 20 76 6f 69 64 20 54 52 41 43 45 5f  atic void TRACE_
2effb 49 44 58 5f 49 4e 50 55 54 53 28 73 71 6c 69 74  IDX_INPUTS(sqlit
2effc 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70  e3_index_info *p
2effd 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66  ){.  int i;.  if
2effe 28 20 21 73 71 6c 69 74 65 33 57 68 65 72 65 54  ( !sqlite3WhereT
2efff 72 61 63 65 20 29 20 72 65 74 75 72 6e 3b 0a 20  race ) return;. 
2f000 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e   for(i=0; i<p->n
2f001 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b 29  Constraint; i++)
2f002 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62  {.    sqlite3Deb
2f003 75 67 50 72 69 6e 74 66 28 22 20 20 63 6f 6e 73  ugPrintf("  cons
2f004 74 72 61 69 6e 74 5b 25 64 5d 3a 20 63 6f 6c 3d  traint[%d]: col=
2f005 25 64 20 74 65 72 6d 69 64 3d 25 64 20 6f 70 3d  %d termid=%d op=
2f006 25 64 20 75 73 61 62 6c 65 64 3d 25 64 5c 6e 22  %d usabled=%d\n"
2f007 2c 0a 20 20 20 20 20 20 20 69 2c 0a 20 20 20 20  ,.       i,.    
2f008 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e     p->aConstrain
2f009 74 5b 69 5d 2e 69 43 6f 6c 75 6d 6e 2c 0a 20 20  t[i].iColumn,.  
2f00a 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61       p->aConstra
2f00b 69 6e 74 5b 69 5d 2e 69 54 65 72 6d 4f 66 66 73  int[i].iTermOffs
2f00c 65 74 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43  et,.       p->aC
2f00d 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 6f 70 2c  onstraint[i].op,
2f00e 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73  .       p->aCons
2f00f 74 72 61 69 6e 74 5b 69 5d 2e 75 73 61 62 6c 65  traint[i].usable
2f010 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  );.  }.  for(i=0
2f011 3b 20 69 3c 70 2d 3e 6e 4f 72 64 65 72 42 79 3b  ; i<p->nOrderBy;
2f012 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74   i++){.    sqlit
2f013 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 20  e3DebugPrintf(" 
2f014 20 6f 72 64 65 72 62 79 5b 25 64 5d 3a 20 63 6f   orderby[%d]: co
2f015 6c 3d 25 64 20 64 65 73 63 3d 25 64 5c 6e 22 2c  l=%d desc=%d\n",
2f016 0a 20 20 20 20 20 20 20 69 2c 0a 20 20 20 20 20  .       i,.     
2f017 20 20 70 2d 3e 61 4f 72 64 65 72 42 79 5b 69 5d    p->aOrderBy[i]
2f018 2e 69 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20 20  .iColumn,.      
2f019 20 70 2d 3e 61 4f 72 64 65 72 42 79 5b 69 5d 2e   p->aOrderBy[i].
2f01a 64 65 73 63 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61  desc);.  }.}.sta
2f01b 74 69 63 20 76 6f 69 64 20 54 52 41 43 45 5f 49  tic void TRACE_I
2f01c 44 58 5f 4f 55 54 50 55 54 53 28 73 71 6c 69 74  DX_OUTPUTS(sqlit
2f01d 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70  e3_index_info *p
2f01e 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66  ){.  int i;.  if
2f01f 28 20 21 73 71 6c 69 74 65 33 57 68 65 72 65 54  ( !sqlite3WhereT
2f020 72 61 63 65 20 29 20 72 65 74 75 72 6e 3b 0a 20  race ) return;. 
2f021 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e   for(i=0; i<p->n
2f022 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b 29  Constraint; i++)
2f023 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62  {.    sqlite3Deb
2f024 75 67 50 72 69 6e 74 66 28 22 20 20 75 73 61 67  ugPrintf("  usag
2f025 65 5b 25 64 5d 3a 20 61 72 67 76 49 64 78 3d 25  e[%d]: argvIdx=%
2f026 64 20 6f 6d 69 74 3d 25 64 5c 6e 22 2c 0a 20 20  d omit=%d\n",.  
2f027 20 20 20 20 20 69 2c 0a 20 20 20 20 20 20 20 70       i,.       p
2f028 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61  ->aConstraintUsa
2f029 67 65 5b 69 5d 2e 61 72 67 76 49 6e 64 65 78 2c  ge[i].argvIndex,
2f02a 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73  .       p->aCons
2f02b 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e 6f  traintUsage[i].o
2f02c 6d 69 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  mit);.  }.  sqli
2f02d 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
2f02e 20 20 69 64 78 4e 75 6d 3d 25 64 5c 6e 22 2c 20    idxNum=%d\n", 
2f02f 70 2d 3e 69 64 78 4e 75 6d 29 3b 0a 20 20 73 71  p->idxNum);.  sq
2f030 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
2f031 28 22 20 20 69 64 78 53 74 72 3d 25 73 5c 6e 22  ("  idxStr=%s\n"
2f032 2c 20 70 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20  , p->idxStr);.  
2f033 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
2f034 74 66 28 22 20 20 6f 72 64 65 72 42 79 43 6f 6e  tf("  orderByCon
2f035 73 75 6d 65 64 3d 25 64 5c 6e 22 2c 20 70 2d 3e  sumed=%d\n", p->
2f036 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 29  orderByConsumed)
2f037 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75 67  ;.  sqlite3Debug
2f038 50 72 69 6e 74 66 28 22 20 20 65 73 74 69 6d 61  Printf("  estima
2f039 74 65 64 43 6f 73 74 3d 25 67 5c 6e 22 2c 20 70  tedCost=%g\n", p
2f03a 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74 29  ->estimatedCost)
2f03b 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  ;.}.#else.#defin
2f03c 65 20 54 52 41 43 45 5f 49 44 58 5f 49 4e 50 55  e TRACE_IDX_INPU
2f03d 54 53 28 41 29 0a 23 64 65 66 69 6e 65 20 54 52  TS(A).#define TR
2f03e 41 43 45 5f 49 44 58 5f 4f 55 54 50 55 54 53 28  ACE_IDX_OUTPUTS(
2f03f 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 0a 2a  A).#endif../* .*
2f040 2a 20 52 65 71 75 69 72 65 64 20 62 65 63 61 75  * Required becau
2f041 73 65 20 62 65 73 74 49 6e 64 65 78 28 29 20 69  se bestIndex() i
2f042 73 20 63 61 6c 6c 65 64 20 62 79 20 62 65 73 74  s called by best
2f043 4f 72 43 6c 61 75 73 65 49 6e 64 65 78 28 29 20  OrClauseIndex() 
2f044 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2f045 62 65 73 74 49 6e 64 65 78 28 0a 20 20 20 20 50  bestIndex(.    P
2f046 61 72 73 65 2a 2c 20 57 68 65 72 65 43 6c 61 75  arse*, WhereClau
2f047 73 65 2a 2c 20 73 74 72 75 63 74 20 53 72 63 4c  se*, struct SrcL
2f048 69 73 74 5f 69 74 65 6d 2a 2c 20 42 69 74 6d 61  ist_item*, Bitma
2f049 73 6b 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 57  sk, ExprList*, W
2f04a 68 65 72 65 43 6f 73 74 2a 29 3b 0a 0a 2f 2a 0a  hereCost*);../*.
2f04b 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2f04c 61 74 74 65 6d 70 74 73 20 74 6f 20 66 69 6e 64  attempts to find
2f04d 20 61 6e 20 73 63 61 6e 6e 69 6e 67 20 73 74 72   an scanning str
2f04e 61 74 65 67 79 20 74 68 61 74 20 63 61 6e 20 62  ategy that can b
2f04f 65 20 75 73 65 64 20 0a 2a 2a 20 74 6f 20 6f 70  e used .** to op
2f050 74 69 6d 69 7a 65 20 61 6e 20 27 4f 52 27 20 65  timize an 'OR' e
2f051 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69  xpression that i
2f052 73 20 70 61 72 74 20 6f 66 20 61 20 57 48 45 52  s part of a WHER
2f053 45 20 63 6c 61 75 73 65 2e 20 0a 2a 2a 0a 2a 2a  E clause. .**.**
2f054 20 54 68 65 20 74 61 62 6c 65 20 61 73 73 6f 63   The table assoc
2f055 69 61 74 65 64 20 77 69 74 68 20 46 52 4f 4d 20  iated with FROM 
2f056 63 6c 61 75 73 65 20 74 65 72 6d 20 70 53 72 63  clause term pSrc
2f057 20 6d 61 79 20 62 65 20 65 69 74 68 65 72 20 61   may be either a
2f058 0a 2a 2a 20 72 65 67 75 6c 61 72 20 42 2d 54 72  .** regular B-Tr
2f059 65 65 20 74 61 62 6c 65 20 6f 72 20 61 20 76 69  ee table or a vi
2f05a 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a  rtual table..*/.
2f05b 73 74 61 74 69 63 20 76 6f 69 64 20 62 65 73 74  static void best
2f05c 4f 72 43 6c 61 75 73 65 49 6e 64 65 78 28 0a 20  OrClauseIndex(. 
2f05d 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
2f05e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2f05f 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74  The parsing cont
2f060 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c  ext */.  WhereCl
2f061 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20  ause *pWC,      
2f062 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52       /* The WHER
2f063 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74  E clause */.  st
2f064 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
2f065 6d 20 2a 70 53 72 63 2c 20 20 2f 2a 20 54 68 65  m *pSrc,  /* The
2f066 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72   FROM clause ter
2f067 6d 20 74 6f 20 73 65 61 72 63 68 20 2a 2f 0a 20  m to search */. 
2f068 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64   Bitmask notRead
2f069 79 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  y,           /* 
2f06a 4d 61 73 6b 20 6f 66 20 63 75 72 73 6f 72 73 20  Mask of cursors 
2f06b 74 68 61 74 20 61 72 65 20 6e 6f 74 20 61 76 61  that are not ava
2f06c 69 6c 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72  ilable */.  Expr
2f06d 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20  List *pOrderBy, 
2f06e 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f          /* The O
2f06f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a  RDER BY clause *
2f070 2f 0a 20 20 57 68 65 72 65 43 6f 73 74 20 2a 70  /.  WhereCost *p
2f071 43 6f 73 74 20 20 20 20 20 20 20 20 20 20 20 20  Cost            
2f072 2f 2a 20 4c 6f 77 65 73 74 20 63 6f 73 74 20 71  /* Lowest cost q
2f073 75 65 72 79 20 70 6c 61 6e 20 2a 2f 0a 29 7b 0a  uery plan */.){.
2f074 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2f075 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41 54  MIT_OR_OPTIMIZAT
2f076 49 4f 4e 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20  ION.  const int 
2f077 69 43 75 72 20 3d 20 70 53 72 63 2d 3e 69 43 75  iCur = pSrc->iCu
2f078 72 73 6f 72 3b 20 20 20 2f 2a 20 54 68 65 20 63  rsor;   /* The c
2f079 75 72 73 6f 72 20 6f 66 20 74 68 65 20 74 61 62  ursor of the tab
2f07a 6c 65 20 74 6f 20 62 65 20 61 63 63 65 73 73 65  le to be accesse
2f07b 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 42 69 74  d */.  const Bit
2f07c 6d 61 73 6b 20 6d 61 73 6b 53 72 63 20 3d 20 67  mask maskSrc = g
2f07d 65 74 4d 61 73 6b 28 70 57 43 2d 3e 70 4d 61 73  etMask(pWC->pMas
2f07e 6b 53 65 74 2c 20 69 43 75 72 29 3b 20 20 2f 2a  kSet, iCur);  /*
2f07f 20 42 69 74 6d 61 73 6b 20 66 6f 72 20 70 53 72   Bitmask for pSr
2f080 63 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d  c */.  WhereTerm
2f081 20 2a 20 63 6f 6e 73 74 20 70 57 43 45 6e 64 20   * const pWCEnd 
2f082 3d 20 26 70 57 43 2d 3e 61 5b 70 57 43 2d 3e 6e  = &pWC->a[pWC->n
2f083 54 65 72 6d 5d 3b 20 20 20 20 20 20 20 20 2f 2a  Term];        /*
2f084 20 45 6e 64 20 6f 66 20 70 57 43 2d 3e 61 5b 5d   End of pWC->a[]
2f085 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20   */.  WhereTerm 
2f086 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20  *pTerm;         
2f087 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e          /* A sin
2f088 67 6c 65 20 74 65 72 6d 20 6f 66 20 74 68 65 20  gle term of the 
2f089 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a  WHERE clause */.
2f08a 0a 20 20 2f 2a 20 53 65 61 72 63 68 20 74 68 65  .  /* Search the
2f08b 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65   WHERE clause te
2f08c 72 6d 73 20 66 6f 72 20 61 20 75 73 61 62 6c 65  rms for a usable
2f08d 20 57 4f 5f 4f 52 20 74 65 72 6d 2e 20 2a 2f 0a   WO_OR term. */.
2f08e 20 20 66 6f 72 28 70 54 65 72 6d 3d 70 57 43 2d    for(pTerm=pWC-
2f08f 3e 61 3b 20 70 54 65 72 6d 3c 70 57 43 45 6e 64  >a; pTerm<pWCEnd
2f090 3b 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20  ; pTerm++){.    
2f091 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  if( pTerm->eOper
2f092 61 74 6f 72 3d 3d 57 4f 5f 4f 52 20 0a 20 20 20  ator==WO_OR .   
2f093 20 20 26 26 20 28 28 70 54 65 72 6d 2d 3e 70 72    && ((pTerm->pr
2f094 65 72 65 71 41 6c 6c 20 26 20 7e 6d 61 73 6b 53  ereqAll & ~maskS
2f095 72 63 29 20 26 20 6e 6f 74 52 65 61 64 79 29 3d  rc) & notReady)=
2f096 3d 30 0a 20 20 20 20 20 26 26 20 28 70 54 65 72  =0.     && (pTer
2f097 6d 2d 3e 75 2e 70 4f 72 49 6e 66 6f 2d 3e 69 6e  m->u.pOrInfo->in
2f098 64 65 78 61 62 6c 65 20 26 20 6d 61 73 6b 53 72  dexable & maskSr
2f099 63 29 21 3d 30 20 0a 20 20 20 20 29 7b 0a 20 20  c)!=0 .    ){.  
2f09a 20 20 20 20 57 68 65 72 65 43 6c 61 75 73 65 20      WhereClause 
2f09b 2a 20 63 6f 6e 73 74 20 70 4f 72 57 43 20 3d 20  * const pOrWC = 
2f09c 26 70 54 65 72 6d 2d 3e 75 2e 70 4f 72 49 6e 66  &pTerm->u.pOrInf
2f09d 6f 2d 3e 77 63 3b 0a 20 20 20 20 20 20 57 68 65  o->wc;.      Whe
2f09e 72 65 54 65 72 6d 20 2a 20 63 6f 6e 73 74 20 70  reTerm * const p
2f09f 4f 72 57 43 45 6e 64 20 3d 20 26 70 4f 72 57 43  OrWCEnd = &pOrWC
2f0a0 2d 3e 61 5b 70 4f 72 57 43 2d 3e 6e 54 65 72 6d  ->a[pOrWC->nTerm
2f0a1 5d 3b 0a 20 20 20 20 20 20 57 68 65 72 65 54 65  ];.      WhereTe
2f0a2 72 6d 20 2a 70 4f 72 54 65 72 6d 3b 0a 20 20 20  rm *pOrTerm;.   
2f0a3 20 20 20 69 6e 74 20 66 6c 61 67 73 20 3d 20 57     int flags = W
2f0a4 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52 3b 0a 20  HERE_MULTI_OR;. 
2f0a5 20 20 20 20 20 64 6f 75 62 6c 65 20 72 54 6f 74       double rTot
2f0a6 61 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f  al = 0;.      do
2f0a7 75 62 6c 65 20 6e 52 6f 77 20 3d 20 30 3b 0a 20  uble nRow = 0;. 
2f0a8 20 20 20 20 20 42 69 74 6d 61 73 6b 20 75 73 65       Bitmask use
2f0a9 64 20 3d 20 30 3b 0a 0a 20 20 20 20 20 20 66 6f  d = 0;..      fo
2f0aa 72 28 70 4f 72 54 65 72 6d 3d 70 4f 72 57 43 2d  r(pOrTerm=pOrWC-
2f0ab 3e 61 3b 20 70 4f 72 54 65 72 6d 3c 70 4f 72 57  >a; pOrTerm<pOrW
2f0ac 43 45 6e 64 3b 20 70 4f 72 54 65 72 6d 2b 2b 29  CEnd; pOrTerm++)
2f0ad 7b 0a 20 20 20 20 20 20 20 20 57 68 65 72 65 43  {.        WhereC
2f0ae 6f 73 74 20 73 54 65 72 6d 43 6f 73 74 3b 0a 20  ost sTermCost;. 
2f0af 20 20 20 20 20 20 20 57 48 45 52 45 54 52 41 43         WHERETRAC
2f0b0 45 28 28 22 2e 2e 2e 20 4d 75 6c 74 69 2d 69 6e  E(("... Multi-in
2f0b1 64 65 78 20 4f 52 20 74 65 73 74 69 6e 67 20 66  dex OR testing f
2f0b2 6f 72 20 74 65 72 6d 20 25 64 20 6f 66 20 25 64  or term %d of %d
2f0b3 2e 2e 2e 2e 5c 6e 22 2c 20 0a 20 20 20 20 20 20  ....\n", .      
2f0b4 20 20 20 20 28 70 4f 72 54 65 72 6d 20 2d 20 70      (pOrTerm - p
2f0b5 4f 72 57 43 2d 3e 61 29 2c 20 28 70 54 65 72 6d  OrWC->a), (pTerm
2f0b6 20 2d 20 70 57 43 2d 3e 61 29 0a 20 20 20 20 20   - pWC->a).     
2f0b7 20 20 20 29 29 3b 0a 20 20 20 20 20 20 20 20 69     ));.        i
2f0b8 66 28 20 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65  f( pOrTerm->eOpe
2f0b9 72 61 74 6f 72 3d 3d 57 4f 5f 41 4e 44 20 29 7b  rator==WO_AND ){
2f0ba 0a 20 20 20 20 20 20 20 20 20 20 57 68 65 72 65  .          Where
2f0bb 43 6c 61 75 73 65 20 2a 70 41 6e 64 57 43 20 3d  Clause *pAndWC =
2f0bc 20 26 70 4f 72 54 65 72 6d 2d 3e 75 2e 70 41 6e   &pOrTerm->u.pAn
2f0bd 64 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20 20 20  dInfo->wc;.     
2f0be 20 20 20 20 20 62 65 73 74 49 6e 64 65 78 28 70       bestIndex(p
2f0bf 50 61 72 73 65 2c 20 70 41 6e 64 57 43 2c 20 70  Parse, pAndWC, p
2f0c0 53 72 63 2c 20 6e 6f 74 52 65 61 64 79 2c 20 30  Src, notReady, 0
2f0c1 2c 20 26 73 54 65 72 6d 43 6f 73 74 29 3b 0a 20  , &sTermCost);. 
2f0c2 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
2f0c3 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43 75   pOrTerm->leftCu
2f0c4 72 73 6f 72 3d 3d 69 43 75 72 20 29 7b 0a 20 20  rsor==iCur ){.  
2f0c5 20 20 20 20 20 20 20 20 57 68 65 72 65 43 6c 61          WhereCla
2f0c6 75 73 65 20 74 65 6d 70 57 43 3b 0a 20 20 20 20  use tempWC;.    
2f0c7 20 20 20 20 20 20 74 65 6d 70 57 43 2e 70 50 61        tempWC.pPa
2f0c8 72 73 65 20 3d 20 70 57 43 2d 3e 70 50 61 72 73  rse = pWC->pPars
2f0c9 65 3b 0a 20 20 20 20 20 20 20 20 20 20 74 65 6d  e;.          tem
2f0ca 70 57 43 2e 70 4d 61 73 6b 53 65 74 20 3d 20 70  pWC.pMaskSet = p
2f0cb 57 43 2d 3e 70 4d 61 73 6b 53 65 74 3b 0a 20 20  WC->pMaskSet;.  
2f0cc 20 20 20 20 20 20 20 20 74 65 6d 70 57 43 2e 6f          tempWC.o
2f0cd 70 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20 20 20  p = TK_AND;.    
2f0ce 20 20 20 20 20 20 74 65 6d 70 57 43 2e 61 20 3d        tempWC.a =
2f0cf 20 70 4f 72 54 65 72 6d 3b 0a 20 20 20 20 20 20   pOrTerm;.      
2f0d0 20 20 20 20 74 65 6d 70 57 43 2e 6e 54 65 72 6d      tempWC.nTerm
2f0d1 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20   = 1;.          
2f0d2 62 65 73 74 49 6e 64 65 78 28 70 50 61 72 73 65  bestIndex(pParse
2f0d3 2c 20 26 74 65 6d 70 57 43 2c 20 70 53 72 63 2c  , &tempWC, pSrc,
2f0d4 20 6e 6f 74 52 65 61 64 79 2c 20 30 2c 20 26 73   notReady, 0, &s
2f0d5 54 65 72 6d 43 6f 73 74 29 3b 0a 20 20 20 20 20  TermCost);.     
2f0d6 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2f0d7 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
2f0d8 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2f0d9 72 54 6f 74 61 6c 20 2b 3d 20 73 54 65 72 6d 43  rTotal += sTermC
2f0da 6f 73 74 2e 72 43 6f 73 74 3b 0a 20 20 20 20 20  ost.rCost;.     
2f0db 20 20 20 6e 52 6f 77 20 2b 3d 20 73 54 65 72 6d     nRow += sTerm
2f0dc 43 6f 73 74 2e 6e 52 6f 77 3b 0a 20 20 20 20 20  Cost.nRow;.     
2f0dd 20 20 20 75 73 65 64 20 7c 3d 20 73 54 65 72 6d     used |= sTerm
2f0de 43 6f 73 74 2e 75 73 65 64 3b 0a 20 20 20 20 20  Cost.used;.     
2f0df 20 20 20 69 66 28 20 72 54 6f 74 61 6c 3e 3d 70     if( rTotal>=p
2f0e0 43 6f 73 74 2d 3e 72 43 6f 73 74 20 29 20 62 72  Cost->rCost ) br
2f0e1 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  eak;.      }..  
2f0e2 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20      /* If there 
2f0e3 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63  is an ORDER BY c
2f0e4 6c 61 75 73 65 2c 20 69 6e 63 72 65 61 73 65 20  lause, increase 
2f0e5 74 68 65 20 73 63 61 6e 20 63 6f 73 74 20 74 6f  the scan cost to
2f0e6 20 61 63 63 6f 75 6e 74 20 0a 20 20 20 20 20 20   account .      
2f0e7 2a 2a 20 66 6f 72 20 74 68 65 20 63 6f 73 74 20  ** for the cost 
2f0e8 6f 66 20 74 68 65 20 73 6f 72 74 2e 20 2a 2f 0a  of the sort. */.
2f0e9 20 20 20 20 20 20 69 66 28 20 70 4f 72 64 65 72        if( pOrder
2f0ea 42 79 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  By!=0 ){.       
2f0eb 20 72 54 6f 74 61 6c 20 2b 3d 20 6e 52 6f 77 2a   rTotal += nRow*
2f0ec 65 73 74 4c 6f 67 28 6e 52 6f 77 29 3b 0a 20 20  estLog(nRow);.  
2f0ed 20 20 20 20 20 20 57 48 45 52 45 54 52 41 43 45        WHERETRACE
2f0ee 28 28 22 2e 2e 2e 20 73 6f 72 74 69 6e 67 20 69  (("... sorting i
2f0ef 6e 63 72 65 61 73 65 73 20 4f 52 20 63 6f 73 74  ncreases OR cost
2f0f0 20 74 6f 20 25 2e 39 67 5c 6e 22 2c 20 72 54 6f   to %.9g\n", rTo
2f0f1 74 61 6c 29 29 3b 0a 20 20 20 20 20 20 7d 0a 0a  tal));.      }..
2f0f2 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20        /* If the 
2f0f3 63 6f 73 74 20 6f 66 20 73 63 61 6e 6e 69 6e 67  cost of scanning
2f0f4 20 75 73 69 6e 67 20 74 68 69 73 20 4f 52 20 74   using this OR t
2f0f5 65 72 6d 20 66 6f 72 20 6f 70 74 69 6d 69 7a 61  erm for optimiza
2f0f6 74 69 6f 6e 20 69 73 0a 20 20 20 20 20 20 2a 2a  tion is.      **
2f0f7 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 63   less than the c
2f0f8 75 72 72 65 6e 74 20 63 6f 73 74 20 73 74 6f 72  urrent cost stor
2f0f9 65 64 20 69 6e 20 70 43 6f 73 74 2c 20 72 65 70  ed in pCost, rep
2f0fa 6c 61 63 65 20 74 68 65 20 63 6f 6e 74 65 6e 74  lace the content
2f0fb 73 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 70 43  s.      ** of pC
2f0fc 6f 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 57 48  ost. */.      WH
2f0fd 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20 6d  ERETRACE(("... m
2f0fe 75 6c 74 69 2d 69 6e 64 65 78 20 4f 52 20 63 6f  ulti-index OR co
2f0ff 73 74 3d 25 2e 39 67 20 6e 72 6f 77 3d 25 2e 39  st=%.9g nrow=%.9
2f100 67 5c 6e 22 2c 20 72 54 6f 74 61 6c 2c 20 6e 52  g\n", rTotal, nR
2f101 6f 77 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ow));.      if( 
2f102 72 54 6f 74 61 6c 3c 70 43 6f 73 74 2d 3e 72 43  rTotal<pCost->rC
2f103 6f 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ost ){.        p
2f104 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20 72 54  Cost->rCost = rT
2f105 6f 74 61 6c 3b 0a 20 20 20 20 20 20 20 20 70 43  otal;.        pC
2f106 6f 73 74 2d 3e 6e 52 6f 77 20 3d 20 6e 52 6f 77  ost->nRow = nRow
2f107 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 73 74 2d  ;.        pCost-
2f108 3e 75 73 65 64 20 3d 20 75 73 65 64 3b 0a 20 20  >used = used;.  
2f109 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61        pCost->pla
2f10a 6e 2e 77 73 46 6c 61 67 73 20 3d 20 66 6c 61 67  n.wsFlags = flag
2f10b 73 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 73 74  s;.        pCost
2f10c 2d 3e 70 6c 61 6e 2e 75 2e 70 54 65 72 6d 20 3d  ->plan.u.pTerm =
2f10d 20 70 54 65 72 6d 3b 0a 20 20 20 20 20 20 7d 0a   pTerm;.      }.
2f10e 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
2f10f 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2f110 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20  OR_OPTIMIZATION 
2f111 2a 2f 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  */.}..#ifndef SQ
2f112 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2f113 4c 54 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 41 6c 6c  LTABLE./*.** All
2f114 6f 63 61 74 65 20 61 6e 64 20 70 6f 70 75 6c 61  ocate and popula
2f115 74 65 20 61 6e 20 73 71 6c 69 74 65 33 5f 69 6e  te an sqlite3_in
2f116 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75  dex_info structu
2f117 72 65 2e 20 49 74 20 69 73 20 74 68 65 20 0a 2a  re. It is the .*
2f118 2a 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79  * responsibility
2f119 20 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74   of the caller t
2f11a 6f 20 65 76 65 6e 74 75 61 6c 6c 79 20 72 65 6c  o eventually rel
2f11b 65 61 73 65 20 74 68 65 20 73 74 72 75 63 74 75  ease the structu
2f11c 72 65 0a 2a 2a 20 62 79 20 70 61 73 73 69 6e 67  re.** by passing
2f11d 20 74 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74   the pointer ret
2f11e 75 72 6e 65 64 20 62 79 20 74 68 69 73 20 66 75  urned by this fu
2f11f 6e 63 74 69 6f 6e 20 74 6f 20 73 71 6c 69 74 65  nction to sqlite
2f120 33 5f 66 72 65 65 28 29 2e 0a 2a 2f 0a 73 74 61  3_free()..*/.sta
2f121 74 69 63 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  tic sqlite3_inde
2f122 78 5f 69 6e 66 6f 20 2a 61 6c 6c 6f 63 61 74 65  x_info *allocate
2f123 49 6e 64 65 78 49 6e 66 6f 28 0a 20 20 50 61 72  IndexInfo(.  Par
2f124 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20 20 57  se *pParse, .  W
2f125 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c  hereClause *pWC,
2f126 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
2f127 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c 0a 20 20  t_item *pSrc,.  
2f128 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
2f129 42 79 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a  By.){.  int i, j
2f12a 3b 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 0a 20  ;.  int nTerm;. 
2f12b 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
2f12c 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74  index_constraint
2f12d 20 2a 70 49 64 78 43 6f 6e 73 3b 0a 20 20 73 74   *pIdxCons;.  st
2f12e 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64  ruct sqlite3_ind
2f12f 65 78 5f 6f 72 64 65 72 62 79 20 2a 70 49 64 78  ex_orderby *pIdx
2f130 4f 72 64 65 72 42 79 3b 0a 20 20 73 74 72 75 63  OrderBy;.  struc
2f131 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
2f132 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65  constraint_usage
2f133 20 2a 70 55 73 61 67 65 3b 0a 20 20 57 68 65 72   *pUsage;.  Wher
2f134 65 54 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20  eTerm *pTerm;.  
2f135 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b 0a 20 20  int nOrderBy;.  
2f136 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e  sqlite3_index_in
2f137 66 6f 20 2a 70 49 64 78 49 6e 66 6f 3b 0a 0a 20  fo *pIdxInfo;.. 
2f138 20 57 48 45 52 45 54 52 41 43 45 28 28 22 52 65   WHERETRACE(("Re
2f139 63 6f 6d 70 75 74 69 6e 67 20 69 6e 64 65 78 20  computing index 
2f13a 69 6e 66 6f 20 66 6f 72 20 25 73 2e 2e 2e 5c 6e  info for %s...\n
2f13b 22 2c 20 70 53 72 63 2d 3e 70 54 61 62 2d 3e 7a  ", pSrc->pTab->z
2f13c 4e 61 6d 65 29 29 3b 0a 0a 20 20 2f 2a 20 43 6f  Name));..  /* Co
2f13d 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  unt the number o
2f13e 66 20 70 6f 73 73 69 62 6c 65 20 57 48 45 52 45  f possible WHERE
2f13f 20 63 6c 61 75 73 65 20 63 6f 6e 73 74 72 61 69   clause constrai
2f140 6e 74 73 20 72 65 66 65 72 72 69 6e 67 0a 20 20  nts referring.  
2f141 2a 2a 20 74 6f 20 74 68 69 73 20 76 69 72 74 75  ** to this virtu
2f142 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 66 6f  al table */.  fo
2f143 72 28 69 3d 6e 54 65 72 6d 3d 30 2c 20 70 54 65  r(i=nTerm=0, pTe
2f144 72 6d 3d 70 57 43 2d 3e 61 3b 20 69 3c 70 57 43  rm=pWC->a; i<pWC
2f145 2d 3e 6e 54 65 72 6d 3b 20 69 2b 2b 2c 20 70 54  ->nTerm; i++, pT
2f146 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  erm++){.    if( 
2f147 70 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f  pTerm->leftCurso
2f148 72 20 21 3d 20 70 53 72 63 2d 3e 69 43 75 72 73  r != pSrc->iCurs
2f149 6f 72 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  or ) continue;. 
2f14a 20 20 20 61 73 73 65 72 74 28 20 28 70 54 65 72     assert( (pTer
2f14b 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 26 28 70 54  m->eOperator&(pT
2f14c 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 2d 31  erm->eOperator-1
2f14d 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 74 65 73  ))==0 );.    tes
2f14e 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f  tcase( pTerm->eO
2f14f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 4e 20 29  perator==WO_IN )
2f150 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
2f151 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2f152 3d 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20  ==WO_ISNULL );. 
2f153 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f     if( pTerm->eO
2f154 70 65 72 61 74 6f 72 20 26 20 28 57 4f 5f 49 4e  perator & (WO_IN
2f155 7c 57 4f 5f 49 53 4e 55 4c 4c 29 20 29 20 63 6f  |WO_ISNULL) ) co
2f156 6e 74 69 6e 75 65 3b 0a 20 20 20 20 6e 54 65 72  ntinue;.    nTer
2f157 6d 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  m++;.  }..  /* I
2f158 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63  f the ORDER BY c
2f159 6c 61 75 73 65 20 63 6f 6e 74 61 69 6e 73 20 6f  lause contains o
2f15a 6e 6c 79 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74  nly columns in t
2f15b 68 65 20 63 75 72 72 65 6e 74 20 0a 20 20 2a 2a  he current .  **
2f15c 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74   virtual table t
2f15d 68 65 6e 20 61 6c 6c 6f 63 61 74 65 20 73 70 61  hen allocate spa
2f15e 63 65 20 66 6f 72 20 74 68 65 20 61 4f 72 64 65  ce for the aOrde
2f15f 72 42 79 20 70 61 72 74 20 6f 66 0a 20 20 2a 2a  rBy part of.  **
2f160 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   the sqlite3_ind
2f161 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
2f162 65 2e 0a 20 20 2a 2f 0a 20 20 6e 4f 72 64 65 72  e..  */.  nOrder
2f163 42 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 4f  By = 0;.  if( pO
2f164 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 66 6f  rderBy ){.    fo
2f165 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42  r(i=0; i<pOrderB
2f166 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  y->nExpr; i++){.
2f167 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 78 70        Expr *pExp
2f168 72 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b  r = pOrderBy->a[
2f169 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  i].pExpr;.      
2f16a 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54  if( pExpr->op!=T
2f16b 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 70 45 78 70  K_COLUMN || pExp
2f16c 72 2d 3e 69 54 61 62 6c 65 21 3d 70 53 72 63 2d  r->iTable!=pSrc-
2f16d 3e 69 43 75 72 73 6f 72 20 29 20 62 72 65 61 6b  >iCursor ) break
2f16e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
2f16f 69 3d 3d 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78  i==pOrderBy->nEx
2f170 70 72 20 29 7b 0a 20 20 20 20 20 20 6e 4f 72 64  pr ){.      nOrd
2f171 65 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 2d  erBy = pOrderBy-
2f172 3e 6e 45 78 70 72 3b 0a 20 20 20 20 7d 0a 20 20  >nExpr;.    }.  
2f173 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  }..  /* Allocate
2f174 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   the sqlite3_ind
2f175 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
2f176 65 0a 20 20 2a 2f 0a 20 20 70 49 64 78 49 6e 66  e.  */.  pIdxInf
2f177 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  o = sqlite3DbMal
2f178 6c 6f 63 5a 65 72 6f 28 70 50 61 72 73 65 2d 3e  locZero(pParse->
2f179 64 62 2c 20 73 69 7a 65 6f 66 28 2a 70 49 64 78  db, sizeof(*pIdx
2f17a 49 6e 66 6f 29 0a 20 20 20 20 20 20 20 20 20 20  Info).          
2f17b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f17c 20 2b 20 28 73 69 7a 65 6f 66 28 2a 70 49 64 78   + (sizeof(*pIdx
2f17d 43 6f 6e 73 29 20 2b 20 73 69 7a 65 6f 66 28 2a  Cons) + sizeof(*
2f17e 70 55 73 61 67 65 29 29 2a 6e 54 65 72 6d 0a 20  pUsage))*nTerm. 
2f17f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f180 20 20 20 20 20 20 20 20 20 20 2b 20 73 69 7a 65            + size
2f181 6f 66 28 2a 70 49 64 78 4f 72 64 65 72 42 79 29  of(*pIdxOrderBy)
2f182 2a 6e 4f 72 64 65 72 42 79 20 29 3b 0a 20 20 69  *nOrderBy );.  i
2f183 66 28 20 70 49 64 78 49 6e 66 6f 3d 3d 30 20 29  f( pIdxInfo==0 )
2f184 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
2f185 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6f  orMsg(pParse, "o
2f186 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a  ut of memory");.
2f187 20 20 20 20 2f 2a 20 28 64 6f 75 62 6c 65 29 30      /* (double)0
2f188 20 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49   In case of SQLI
2f189 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
2f18a 5f 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 20  _POINT... */.   
2f18b 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a   return 0;.  }..
2f18c 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
2f18d 74 68 65 20 73 74 72 75 63 74 75 72 65 2e 20 20  the structure.  
2f18e 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  The sqlite3_inde
2f18f 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  x_info structure
2f190 20 63 6f 6e 74 61 69 6e 73 0a 20 20 2a 2a 20 6d   contains.  ** m
2f191 61 6e 79 20 66 69 65 6c 64 73 20 74 68 61 74 20  any fields that 
2f192 61 72 65 20 64 65 63 6c 61 72 65 64 20 22 63 6f  are declared "co
2f193 6e 73 74 22 20 74 6f 20 70 72 65 76 65 6e 74 20  nst" to prevent 
2f194 78 42 65 73 74 49 6e 64 65 78 20 66 72 6f 6d 0a  xBestIndex from.
2f195 20 20 2a 2a 20 63 68 61 6e 67 69 6e 67 20 74 68    ** changing th
2f196 65 6d 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20  em.  We have to 
2f197 64 6f 20 73 6f 6d 65 20 66 75 6e 6b 79 20 63 61  do some funky ca
2f198 73 74 69 6e 67 20 69 6e 20 6f 72 64 65 72 20 74  sting in order t
2f199 6f 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a  o.  ** initializ
2f19a 65 20 74 68 6f 73 65 20 66 69 65 6c 64 73 2e 0a  e those fields..
2f19b 20 20 2a 2f 0a 20 20 70 49 64 78 43 6f 6e 73 20    */.  pIdxCons 
2f19c 3d 20 28 73 74 72 75 63 74 20 73 71 6c 69 74 65  = (struct sqlite
2f19d 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
2f19e 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f 5b 31 5d  nt*)&pIdxInfo[1]
2f19f 3b 0a 20 20 70 49 64 78 4f 72 64 65 72 42 79 20  ;.  pIdxOrderBy 
2f1a0 3d 20 28 73 74 72 75 63 74 20 73 71 6c 69 74 65  = (struct sqlite
2f1a1 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72 62 79 2a  3_index_orderby*
2f1a2 29 26 70 49 64 78 43 6f 6e 73 5b 6e 54 65 72 6d  )&pIdxCons[nTerm
2f1a3 5d 3b 0a 20 20 70 55 73 61 67 65 20 3d 20 28 73  ];.  pUsage = (s
2f1a4 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
2f1a5 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75  dex_constraint_u
2f1a6 73 61 67 65 2a 29 26 70 49 64 78 4f 72 64 65 72  sage*)&pIdxOrder
2f1a7 42 79 5b 6e 4f 72 64 65 72 42 79 5d 3b 0a 20 20  By[nOrderBy];.  
2f1a8 2a 28 69 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f  *(int*)&pIdxInfo
2f1a9 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20  ->nConstraint = 
2f1aa 6e 54 65 72 6d 3b 0a 20 20 2a 28 69 6e 74 2a 29  nTerm;.  *(int*)
2f1ab 26 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65  &pIdxInfo->nOrde
2f1ac 72 42 79 20 3d 20 6e 4f 72 64 65 72 42 79 3b 0a  rBy = nOrderBy;.
2f1ad 20 20 2a 28 73 74 72 75 63 74 20 73 71 6c 69 74    *(struct sqlit
2f1ae 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61  e3_index_constra
2f1af 69 6e 74 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d  int**)&pIdxInfo-
2f1b0 3e 61 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 70  >aConstraint = p
2f1b1 49 64 78 43 6f 6e 73 3b 0a 20 20 2a 28 73 74 72  IdxCons;.  *(str
2f1b2 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  uct sqlite3_inde
2f1b3 78 5f 6f 72 64 65 72 62 79 2a 2a 29 26 70 49 64  x_orderby**)&pId
2f1b4 78 49 6e 66 6f 2d 3e 61 4f 72 64 65 72 42 79 20  xInfo->aOrderBy 
2f1b5 3d 20 70 49 64 78 4f 72 64 65 72 42 79 3b 0a 20  = pIdxOrderBy;. 
2f1b6 20 2a 28 73 74 72 75 63 74 20 73 71 6c 69 74 65   *(struct sqlite
2f1b7 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
2f1b8 6e 74 5f 75 73 61 67 65 2a 2a 29 26 70 49 64 78  nt_usage**)&pIdx
2f1b9 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
2f1ba 74 55 73 61 67 65 20 3d 0a 20 20 20 20 20 20 20  tUsage =.       
2f1bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f1bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f1bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f1be 20 20 20 20 20 20 20 20 20 20 20 20 70 55 73 61              pUsa
2f1bf 67 65 3b 0a 0a 20 20 66 6f 72 28 69 3d 6a 3d 30  ge;..  for(i=j=0
2f1c0 2c 20 70 54 65 72 6d 3d 70 57 43 2d 3e 61 3b 20  , pTerm=pWC->a; 
2f1c1 69 3c 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 69 2b  i<pWC->nTerm; i+
2f1c2 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20 20  +, pTerm++){.   
2f1c3 20 69 66 28 20 70 54 65 72 6d 2d 3e 6c 65 66 74   if( pTerm->left
2f1c4 43 75 72 73 6f 72 20 21 3d 20 70 53 72 63 2d 3e  Cursor != pSrc->
2f1c5 69 43 75 72 73 6f 72 20 29 20 63 6f 6e 74 69 6e  iCursor ) contin
2f1c6 75 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ue;.    assert( 
2f1c7 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f  (pTerm->eOperato
2f1c8 72 26 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  r&(pTerm->eOpera
2f1c9 74 6f 72 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20  tor-1))==0 );.  
2f1ca 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72    testcase( pTer
2f1cb 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f  m->eOperator==WO
2f1cc 5f 49 4e 20 29 3b 0a 20 20 20 20 74 65 73 74 63  _IN );.    testc
2f1cd 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65  ase( pTerm->eOpe
2f1ce 72 61 74 6f 72 3d 3d 57 4f 5f 49 53 4e 55 4c 4c  rator==WO_ISNULL
2f1cf 20 29 3b 0a 20 20 20 20 69 66 28 20 70 54 65 72   );.    if( pTer
2f1d0 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28  m->eOperator & (
2f1d1 57 4f 5f 49 4e 7c 57 4f 5f 49 53 4e 55 4c 4c 29  WO_IN|WO_ISNULL)
2f1d2 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
2f1d3 20 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e 69 43 6f   pIdxCons[j].iCo
2f1d4 6c 75 6d 6e 20 3d 20 70 54 65 72 6d 2d 3e 75 2e  lumn = pTerm->u.
2f1d5 6c 65 66 74 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  leftColumn;.    
2f1d6 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e 69 54 65 72  pIdxCons[j].iTer
2f1d7 6d 4f 66 66 73 65 74 20 3d 20 69 3b 0a 20 20 20  mOffset = i;.   
2f1d8 20 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e 6f 70 20   pIdxCons[j].op 
2f1d9 3d 20 28 75 38 29 70 54 65 72 6d 2d 3e 65 4f 70  = (u8)pTerm->eOp
2f1da 65 72 61 74 6f 72 3b 0a 20 20 20 20 2f 2a 20 54  erator;.    /* T
2f1db 68 65 20 64 69 72 65 63 74 20 61 73 73 69 67 6e  he direct assign
2f1dc 6d 65 6e 74 20 69 6e 20 74 68 65 20 70 72 65 76  ment in the prev
2f1dd 69 6f 75 73 20 6c 69 6e 65 20 69 73 20 70 6f 73  ious line is pos
2f1de 73 69 62 6c 65 20 6f 6e 6c 79 20 62 65 63 61 75  sible only becau
2f1df 73 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 57 4f  se.    ** the WO
2f1e0 5f 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4e 44  _ and SQLITE_IND
2f1e1 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 20 63  EX_CONSTRAINT_ c
2f1e2 6f 64 65 73 20 61 72 65 20 69 64 65 6e 74 69 63  odes are identic
2f1e3 61 6c 2e 20 20 54 68 65 0a 20 20 20 20 2a 2a 20  al.  The.    ** 
2f1e4 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74  following assert
2f1e5 73 20 76 65 72 69 66 79 20 74 68 69 73 20 66 61  s verify this fa
2f1e6 63 74 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  ct. */.    asser
2f1e7 74 28 20 57 4f 5f 45 51 3d 3d 53 51 4c 49 54 45  t( WO_EQ==SQLITE
2f1e8 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
2f1e9 54 5f 45 51 20 29 3b 0a 20 20 20 20 61 73 73 65  T_EQ );.    asse
2f1ea 72 74 28 20 57 4f 5f 4c 54 3d 3d 53 51 4c 49 54  rt( WO_LT==SQLIT
2f1eb 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
2f1ec 4e 54 5f 4c 54 20 29 3b 0a 20 20 20 20 61 73 73  NT_LT );.    ass
2f1ed 65 72 74 28 20 57 4f 5f 4c 45 3d 3d 53 51 4c 49  ert( WO_LE==SQLI
2f1ee 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41  TE_INDEX_CONSTRA
2f1ef 49 4e 54 5f 4c 45 20 29 3b 0a 20 20 20 20 61 73  INT_LE );.    as
2f1f0 73 65 72 74 28 20 57 4f 5f 47 54 3d 3d 53 51 4c  sert( WO_GT==SQL
2f1f1 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52  ITE_INDEX_CONSTR
2f1f2 41 49 4e 54 5f 47 54 20 29 3b 0a 20 20 20 20 61  AINT_GT );.    a
2f1f3 73 73 65 72 74 28 20 57 4f 5f 47 45 3d 3d 53 51  ssert( WO_GE==SQ
2f1f4 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
2f1f5 52 41 49 4e 54 5f 47 45 20 29 3b 0a 20 20 20 20  RAINT_GE );.    
2f1f6 61 73 73 65 72 74 28 20 57 4f 5f 4d 41 54 43 48  assert( WO_MATCH
2f1f7 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  ==SQLITE_INDEX_C
2f1f8 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43 48 20  ONSTRAINT_MATCH 
2f1f9 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
2f1fa 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
2f1fb 26 20 28 57 4f 5f 45 51 7c 57 4f 5f 4c 54 7c 57  & (WO_EQ|WO_LT|W
2f1fc 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45  O_LE|WO_GT|WO_GE
2f1fd 7c 57 4f 5f 4d 41 54 43 48 29 20 29 3b 0a 20 20  |WO_MATCH) );.  
2f1fe 20 20 6a 2b 2b 3b 0a 20 20 7d 0a 20 20 66 6f 72    j++;.  }.  for
2f1ff 28 69 3d 30 3b 20 69 3c 6e 4f 72 64 65 72 42 79  (i=0; i<nOrderBy
2f200 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72  ; i++){.    Expr
2f201 20 2a 70 45 78 70 72 20 3d 20 70 4f 72 64 65 72   *pExpr = pOrder
2f202 42 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a  By->a[i].pExpr;.
2f203 20 20 20 20 70 49 64 78 4f 72 64 65 72 42 79 5b      pIdxOrderBy[
2f204 69 5d 2e 69 43 6f 6c 75 6d 6e 20 3d 20 70 45 78  i].iColumn = pEx
2f205 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20  pr->iColumn;.   
2f206 20 70 49 64 78 4f 72 64 65 72 42 79 5b 69 5d 2e   pIdxOrderBy[i].
2f207 64 65 73 63 20 3d 20 70 4f 72 64 65 72 42 79 2d  desc = pOrderBy-
2f208 3e 61 5b 69 5d 2e 73 6f 72 74 4f 72 64 65 72 3b  >a[i].sortOrder;
2f209 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70  .  }..  return p
2f20a 49 64 78 49 6e 66 6f 3b 0a 7d 0a 0a 2f 2a 0a 2a  IdxInfo;.}../*.*
2f20b 2a 20 54 68 65 20 74 61 62 6c 65 20 6f 62 6a 65  * The table obje
2f20c 63 74 20 72 65 66 65 72 65 6e 63 65 20 70 61 73  ct reference pas
2f20d 73 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  sed as the secon
2f20e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  d argument to th
2f20f 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 6d  is function.** m
2f210 75 73 74 20 72 65 70 72 65 73 65 6e 74 20 61 20  ust represent a 
2f211 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 54  virtual table. T
2f212 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 6e 76  his function inv
2f213 6f 6b 65 73 20 74 68 65 20 78 42 65 73 74 49 6e  okes the xBestIn
2f214 64 65 78 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20  dex().** method 
2f215 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 74  of the virtual t
2f216 61 62 6c 65 20 77 69 74 68 20 74 68 65 20 73 71  able with the sq
2f217 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f  lite3_index_info
2f218 20 70 6f 69 6e 74 65 72 20 70 61 73 73 65 64 0a   pointer passed.
2f219 2a 2a 20 61 73 20 74 68 65 20 61 72 67 75 6d 65  ** as the argume
2f21a 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  nt..**.** If an 
2f21b 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 70 50  error occurs, pP
2f21c 61 72 73 65 20 69 73 20 70 6f 70 75 6c 61 74 65  arse is populate
2f21d 64 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20  d with an error 
2f21e 6d 65 73 73 61 67 65 20 61 6e 64 20 61 0a 2a 2a  message and a.**
2f21f 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65 20   non-zero value 
2f220 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68  is returned. Oth
2f221 65 72 77 69 73 65 2c 20 30 20 69 73 20 72 65 74  erwise, 0 is ret
2f222 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 6f 75  urned and the ou
2f223 74 70 75 74 0a 2a 2a 20 70 61 72 74 20 6f 66 20  tput.** part of 
2f224 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  the sqlite3_inde
2f225 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  x_info structure
2f226 20 69 73 20 6c 65 66 74 20 70 6f 70 75 6c 61 74   is left populat
2f227 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65  ed..**.** Whethe
2f228 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 72 72 6f  r or not an erro
2f229 72 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 69  r is returned, i
2f22a 74 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73  t is the respons
2f22b 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 0a 2a  ibility of the.*
2f22c 2a 20 63 61 6c 6c 65 72 20 74 6f 20 65 76 65 6e  * caller to even
2f22d 74 75 61 6c 6c 79 20 66 72 65 65 20 70 2d 3e 69  tually free p->i
2f22e 64 78 53 74 72 20 69 66 20 70 2d 3e 6e 65 65 64  dxStr if p->need
2f22f 54 6f 46 72 65 65 49 64 78 53 74 72 20 69 6e 64  ToFreeIdxStr ind
2f230 69 63 61 74 65 73 0a 2a 2a 20 74 68 61 74 20 74  icates.** that t
2f231 68 69 73 20 69 73 20 72 65 71 75 69 72 65 64 2e  his is required.
2f232 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76  .*/.static int v
2f233 74 61 62 42 65 73 74 49 6e 64 65 78 28 50 61 72  tabBestIndex(Par
2f234 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
2f235 65 20 2a 70 54 61 62 2c 20 73 71 6c 69 74 65 33  e *pTab, sqlite3
2f236 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 29 7b  _index_info *p){
2f237 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
2f238 2a 70 56 74 61 62 20 3d 20 73 71 6c 69 74 65 33  *pVtab = sqlite3
2f239 47 65 74 56 54 61 62 6c 65 28 70 50 61 72 73 65  GetVTable(pParse
2f23a 2d 3e 64 62 2c 20 70 54 61 62 29 2d 3e 70 56 74  ->db, pTab)->pVt
2f23b 61 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  ab;.  int i;.  i
2f23c 6e 74 20 72 63 3b 0a 0a 20 20 28 76 6f 69 64 29  nt rc;..  (void)
2f23d 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
2f23e 28 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a 20 20  (pParse->db);.  
2f23f 57 48 45 52 45 54 52 41 43 45 28 28 22 78 42 65  WHERETRACE(("xBe
2f240 73 74 49 6e 64 65 78 20 66 6f 72 20 25 73 5c 6e  stIndex for %s\n
2f241 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 29  ", pTab->zName))
2f242 3b 0a 20 20 54 52 41 43 45 5f 49 44 58 5f 49 4e  ;.  TRACE_IDX_IN
2f243 50 55 54 53 28 70 29 3b 0a 20 20 72 63 20 3d 20  PUTS(p);.  rc = 
2f244 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e  pVtab->pModule->
2f245 78 42 65 73 74 49 6e 64 65 78 28 70 56 74 61 62  xBestIndex(pVtab
2f246 2c 20 70 29 3b 0a 20 20 54 52 41 43 45 5f 49 44  , p);.  TRACE_ID
2f247 58 5f 4f 55 54 50 55 54 53 28 70 29 3b 0a 20 20  X_OUTPUTS(p);.  
2f248 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
2f249 65 74 79 4f 6e 28 70 50 61 72 73 65 2d 3e 64 62  etyOn(pParse->db
2f24a 29 3b 0a 0a 20 20 69 66 28 20 72 63 21 3d 53 51  );..  if( rc!=SQ
2f24b 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
2f24c 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
2f24d 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 70 50 61  MEM ){.      pPa
2f24e 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rse->db->mallocF
2f24f 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d  ailed = 1;.    }
2f250 65 6c 73 65 20 69 66 28 20 21 70 56 74 61 62 2d  else if( !pVtab-
2f251 3e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20  >zErrMsg ){.    
2f252 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2f253 67 28 70 50 61 72 73 65 2c 20 22 25 73 22 2c 20  g(pParse, "%s", 
2f254 73 71 6c 69 74 65 33 45 72 72 53 74 72 28 72 63  sqlite3ErrStr(rc
2f255 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ));.    }else{. 
2f256 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
2f257 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73  rMsg(pParse, "%s
2f258 22 2c 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73  ", pVtab->zErrMs
2f259 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  g);.    }.  }.  
2f25a 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 50  sqlite3DbFree(pP
2f25b 61 72 73 65 2d 3e 64 62 2c 20 70 56 74 61 62 2d  arse->db, pVtab-
2f25c 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 56 74  >zErrMsg);.  pVt
2f25d 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b  ab->zErrMsg = 0;
2f25e 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ..  for(i=0; i<p
2f25f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69  ->nConstraint; i
2f260 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 21 70 2d  ++){.    if( !p-
2f261 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e  >aConstraint[i].
2f262 75 73 61 62 6c 65 20 26 26 20 70 2d 3e 61 43 6f  usable && p->aCo
2f263 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d  nstraintUsage[i]
2f264 2e 61 72 67 76 49 6e 64 65 78 3e 30 20 29 7b 0a  .argvIndex>0 ){.
2f265 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
2f266 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20  orMsg(pParse, . 
2f267 20 20 20 20 20 20 20 20 20 22 74 61 62 6c 65 20           "table 
2f268 25 73 3a 20 78 42 65 73 74 49 6e 64 65 78 20 72  %s: xBestIndex r
2f269 65 74 75 72 6e 65 64 20 61 6e 20 69 6e 76 61 6c  eturned an inval
2f26a 69 64 20 70 6c 61 6e 22 2c 20 70 54 61 62 2d 3e  id plan", pTab->
2f26b 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20  zName);.    }.  
2f26c 7d 0a 0a 20 20 72 65 74 75 72 6e 20 70 50 61 72  }..  return pPar
2f26d 73 65 2d 3e 6e 45 72 72 3b 0a 7d 0a 0a 0a 2f 2a  se->nErr;.}.../*
2f26e 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20  .** Compute the 
2f26f 62 65 73 74 20 69 6e 64 65 78 20 66 6f 72 20 61  best index for a
2f270 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a   virtual table..
2f271 2a 2a 0a 2a 2a 20 54 68 65 20 62 65 73 74 20 69  **.** The best i
2f272 6e 64 65 78 20 69 73 20 63 6f 6d 70 75 74 65 64  ndex is computed
2f273 20 62 79 20 74 68 65 20 78 42 65 73 74 49 6e 64   by the xBestInd
2f274 65 78 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  ex method of the
2f275 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c   virtual.** tabl
2f276 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68 69 73 20  e module.  This 
2f277 72 6f 75 74 69 6e 65 20 69 73 20 72 65 61 6c 6c  routine is reall
2f278 79 20 6a 75 73 74 20 61 20 77 72 61 70 70 65 72  y just a wrapper
2f279 20 74 68 61 74 20 73 65 74 73 20 75 70 0a 2a 2a   that sets up.**
2f27a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   the sqlite3_ind
2f27b 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
2f27c 65 20 74 68 61 74 20 69 73 20 75 73 65 64 20 74  e that is used t
2f27d 6f 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20 77 69  o communicate wi
2f27e 74 68 0a 2a 2a 20 78 42 65 73 74 49 6e 64 65 78  th.** xBestIndex
2f27f 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 20 6a 6f 69  ..**.** In a joi
2f280 6e 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  n, this routine 
2f281 6d 69 67 68 74 20 62 65 20 63 61 6c 6c 65 64 20  might be called 
2f282 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 66  multiple times f
2f283 6f 72 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 76  or the.** same v
2f284 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54  irtual table.  T
2f285 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  he sqlite3_index
2f286 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20  _info structure 
2f287 69 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e  is created.** an
2f288 64 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 6f 6e  d initialized on
2f289 20 74 68 65 20 66 69 72 73 74 20 69 6e 76 6f 63   the first invoc
2f28a 61 74 69 6f 6e 20 61 6e 64 20 72 65 75 73 65 64  ation and reused
2f28b 20 6f 6e 20 61 6c 6c 20 73 75 62 73 65 71 75 65   on all subseque
2f28c 6e 74 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e  nt.** invocation
2f28d 73 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f  s.  The sqlite3_
2f28e 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63  index_info struc
2f28f 74 75 72 65 20 69 73 20 61 6c 73 6f 20 75 73 65  ture is also use
2f290 64 20 77 68 65 6e 0a 2a 2a 20 63 6f 64 65 20 69  d when.** code i
2f291 73 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 61  s generated to a
2f292 63 63 65 73 73 20 74 68 65 20 76 69 72 74 75 61  ccess the virtua
2f293 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20 77 68  l table.  The wh
2f294 65 72 65 49 6e 66 6f 44 65 6c 65 74 65 28 29 20  ereInfoDelete() 
2f295 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 61 6b 65  .** routine take
2f296 73 20 63 61 72 65 20 6f 66 20 66 72 65 65 69 6e  s care of freein
2f297 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  g the sqlite3_in
2f298 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75  dex_info structu
2f299 72 65 20 61 66 74 65 72 0a 2a 2a 20 65 76 65 72  re after.** ever
2f29a 79 62 6f 64 79 20 68 61 73 20 66 69 6e 69 73 68  ybody has finish
2f29b 65 64 20 77 69 74 68 20 69 74 2e 0a 2a 2f 0a 73  ed with it..*/.s
2f29c 74 61 74 69 63 20 76 6f 69 64 20 62 65 73 74 56  tatic void bestV
2f29d 69 72 74 75 61 6c 49 6e 64 65 78 28 0a 20 20 50  irtualIndex(.  P
2f29e 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2f29f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2f2a0 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f  * The parsing co
2f2a1 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65  ntext */.  Where
2f2a2 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20  Clause *pWC,    
2f2a3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2f2a4 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
2f2a5 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  /.  struct SrcLi
2f2a6 73 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c 20 20  st_item *pSrc,  
2f2a7 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20      /* The FROM 
2f2a8 63 6c 61 75 73 65 20 74 65 72 6d 20 74 6f 20 73  clause term to s
2f2a9 65 61 72 63 68 20 2a 2f 0a 20 20 42 69 74 6d 61  earch */.  Bitma
2f2aa 73 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20  sk notReady,    
2f2ab 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
2f2ac 73 6b 20 6f 66 20 63 75 72 73 6f 72 73 20 74 68  sk of cursors th
2f2ad 61 74 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c  at are not avail
2f2ae 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69  able */.  ExprLi
2f2af 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20  st *pOrderBy,   
2f2b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
2f2b1 20 6f 72 64 65 72 20 62 79 20 63 6c 61 75 73 65   order by clause
2f2b2 20 2a 2f 0a 20 20 57 68 65 72 65 43 6f 73 74 20   */.  WhereCost 
2f2b3 2a 70 43 6f 73 74 2c 20 20 20 20 20 20 20 20 20  *pCost,         
2f2b4 20 20 20 20 20 20 2f 2a 20 4c 6f 77 65 73 74 20        /* Lowest 
2f2b5 63 6f 73 74 20 71 75 65 72 79 20 70 6c 61 6e 20  cost query plan 
2f2b6 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 64  */.  sqlite3_ind
2f2b7 65 78 5f 69 6e 66 6f 20 2a 2a 70 70 49 64 78 49  ex_info **ppIdxI
2f2b8 6e 66 6f 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e  nfo  /* Index in
2f2b9 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73 65 64  formation passed
2f2ba 20 74 6f 20 78 42 65 73 74 49 6e 64 65 78 20 2a   to xBestIndex *
2f2bb 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 54  /.){.  Table *pT
2f2bc 61 62 20 3d 20 70 53 72 63 2d 3e 70 54 61 62 3b  ab = pSrc->pTab;
2f2bd 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  .  sqlite3_index
2f2be 5f 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66 6f 3b  _info *pIdxInfo;
2f2bf 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  .  struct sqlite
2f2c0 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
2f2c1 6e 74 20 2a 70 49 64 78 43 6f 6e 73 3b 0a 20 20  nt *pIdxCons;.  
2f2c2 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
2f2c3 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f  ndex_constraint_
2f2c4 75 73 61 67 65 20 2a 70 55 73 61 67 65 3b 0a 20  usage *pUsage;. 
2f2c5 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72   WhereTerm *pTer
2f2c6 6d 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20  m;.  int i, j;. 
2f2c7 20 69 6e 74 20 6e 4f 72 64 65 72 42 79 3b 0a 0a   int nOrderBy;..
2f2c8 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 77    /* Make sure w
2f2c9 73 46 6c 61 67 73 20 69 73 20 69 6e 69 74 69 61  sFlags is initia
2f2ca 6c 69 7a 65 64 20 74 6f 20 73 6f 6d 65 20 73 61  lized to some sa
2f2cb 6e 65 20 76 61 6c 75 65 2e 20 4f 74 68 65 72 77  ne value. Otherw
2f2cc 69 73 65 2c 20 69 66 20 74 68 65 20 0a 20 20 2a  ise, if the .  *
2f2cd 2a 20 6d 61 6c 6c 6f 63 20 69 6e 20 61 6c 6c 6f  * malloc in allo
2f2ce 63 61 74 65 49 6e 64 65 78 49 6e 66 6f 28 29 20  cateIndexInfo() 
2f2cf 66 61 69 6c 73 20 61 6e 64 20 74 68 69 73 20 66  fails and this f
2f2d0 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
2f2d1 6c 65 61 76 69 6e 67 0a 20 20 2a 2a 20 77 73 46  leaving.  ** wsF
2f2d2 6c 61 67 73 20 69 6e 20 61 6e 20 75 6e 69 6e 69  lags in an unini
2f2d3 74 69 61 6c 69 7a 65 64 20 73 74 61 74 65 2c 20  tialized state, 
2f2d4 74 68 65 20 63 61 6c 6c 65 72 20 6d 61 79 20 62  the caller may b
2f2d5 65 68 61 76 65 20 75 6e 70 72 65 64 69 63 74 61  ehave unpredicta
2f2d6 62 6c 79 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73  bly..  */.  mems
2f2d7 65 74 28 70 43 6f 73 74 2c 20 30 2c 20 73 69 7a  et(pCost, 0, siz
2f2d8 65 6f 66 28 2a 70 43 6f 73 74 29 29 3b 0a 20 20  eof(*pCost));.  
2f2d9 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c  pCost->plan.wsFl
2f2da 61 67 73 20 3d 20 57 48 45 52 45 5f 56 49 52 54  ags = WHERE_VIRT
2f2db 55 41 4c 54 41 42 4c 45 3b 0a 0a 20 20 2f 2a 20  UALTABLE;..  /* 
2f2dc 49 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69  If the sqlite3_i
2f2dd 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74  ndex_info struct
2f2de 75 72 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e  ure has not been
2f2df 20 70 72 65 76 69 6f 75 73 6c 79 0a 20 20 2a 2a   previously.  **
2f2e0 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 69   allocated and i
2f2e1 6e 69 74 69 61 6c 69 7a 65 64 2c 20 74 68 65 6e  nitialized, then
2f2e2 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 69 6e   allocate and in
2f2e3 69 74 69 61 6c 69 7a 65 20 69 74 20 6e 6f 77 2e  itialize it now.
2f2e4 0a 20 20 2a 2f 0a 20 20 70 49 64 78 49 6e 66 6f  .  */.  pIdxInfo
2f2e5 20 3d 20 2a 70 70 49 64 78 49 6e 66 6f 3b 0a 20   = *ppIdxInfo;. 
2f2e6 20 69 66 28 20 70 49 64 78 49 6e 66 6f 3d 3d 30   if( pIdxInfo==0
2f2e7 20 29 7b 0a 20 20 20 20 2a 70 70 49 64 78 49 6e   ){.    *ppIdxIn
2f2e8 66 6f 20 3d 20 70 49 64 78 49 6e 66 6f 20 3d 20  fo = pIdxInfo = 
2f2e9 61 6c 6c 6f 63 61 74 65 49 6e 64 65 78 49 6e 66  allocateIndexInf
2f2ea 6f 28 70 50 61 72 73 65 2c 20 70 57 43 2c 20 70  o(pParse, pWC, p
2f2eb 53 72 63 2c 20 70 4f 72 64 65 72 42 79 29 3b 0a  Src, pOrderBy);.
2f2ec 20 20 7d 0a 20 20 69 66 28 20 70 49 64 78 49 6e    }.  if( pIdxIn
2f2ed 66 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  fo==0 ){.    ret
2f2ee 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  urn;.  }..  /* A
2f2ef 74 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68  t this point, th
2f2f0 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  e sqlite3_index_
2f2f1 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74  info structure t
2f2f2 68 61 74 20 70 49 64 78 49 6e 66 6f 20 70 6f 69  hat pIdxInfo poi
2f2f3 6e 74 73 0a 20 20 2a 2a 20 74 6f 20 77 69 6c 6c  nts.  ** to will
2f2f4 20 68 61 76 65 20 62 65 65 6e 20 69 6e 69 74 69   have been initi
2f2f5 61 6c 69 7a 65 64 2c 20 65 69 74 68 65 72 20 64  alized, either d
2f2f6 75 72 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e  uring the curren
2f2f7 74 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 72 0a  t invocation or.
2f2f8 20 20 2a 2a 20 64 75 72 69 6e 67 20 73 6f 6d 65    ** during some
2f2f9 20 70 72 69 6f 72 20 69 6e 76 6f 63 61 74 69 6f   prior invocatio
2f2fa 6e 2e 20 20 4e 6f 77 20 77 65 20 6a 75 73 74 20  n.  Now we just 
2f2fb 68 61 76 65 20 74 6f 20 63 75 73 74 6f 6d 69 7a  have to customiz
2f2fc 65 20 74 68 65 0a 20 20 2a 2a 20 64 65 74 61 69  e the.  ** detai
2f2fd 6c 73 20 6f 66 20 70 49 64 78 49 6e 66 6f 20 66  ls of pIdxInfo f
2f2fe 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 69  or the current i
2f2ff 6e 76 6f 63 61 74 69 6f 6e 20 61 6e 64 20 70 61  nvocation and pa
2f300 73 73 20 69 74 20 74 6f 0a 20 20 2a 2a 20 78 42  ss it to.  ** xB
2f301 65 73 74 49 6e 64 65 78 2e 0a 20 20 2a 2f 0a 0a  estIndex..  */..
2f302 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20    /* The module 
2f303 6e 61 6d 65 20 6d 75 73 74 20 62 65 20 64 65 66  name must be def
2f304 69 6e 65 64 2e 20 41 6c 73 6f 2c 20 62 79 20 74  ined. Also, by t
2f305 68 69 73 20 70 6f 69 6e 74 20 74 68 65 72 65 20  his point there 
2f306 6d 75 73 74 0a 20 20 2a 2a 20 62 65 20 61 20 70  must.  ** be a p
2f307 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c  ointer to an sql
2f308 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74  ite3_vtab struct
2f309 75 72 65 2e 20 4f 74 68 65 72 77 69 73 65 0a 20  ure. Otherwise. 
2f30a 20 2a 2a 20 73 71 6c 69 74 65 33 56 69 65 77 47   ** sqlite3ViewG
2f30b 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20  etColumnNames() 
2f30c 77 6f 75 6c 64 20 68 61 76 65 20 70 69 63 6b 65  would have picke
2f30d 64 20 75 70 20 74 68 65 20 65 72 72 6f 72 2e 20  d up the error. 
2f30e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
2f30f 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72  pTab->azModuleAr
2f310 67 20 26 26 20 70 54 61 62 2d 3e 61 7a 4d 6f 64  g && pTab->azMod
2f311 75 6c 65 41 72 67 5b 30 5d 20 29 3b 0a 20 20 61  uleArg[0] );.  a
2f312 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 47 65  ssert( sqlite3Ge
2f313 74 56 54 61 62 6c 65 28 70 50 61 72 73 65 2d 3e  tVTable(pParse->
2f314 64 62 2c 20 70 54 61 62 29 20 29 3b 0a 0a 20 20  db, pTab) );..  
2f315 2f 2a 20 53 65 74 20 74 68 65 20 61 43 6f 6e 73  /* Set the aCons
2f316 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 20  traint[].usable 
2f317 66 69 65 6c 64 73 20 61 6e 64 20 69 6e 69 74 69  fields and initi
2f318 61 6c 69 7a 65 20 61 6c 6c 20 0a 20 20 2a 2a 20  alize all .  ** 
2f319 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c 65 73  output variables
2f31a 20 74 6f 20 7a 65 72 6f 2e 0a 20 20 2a 2a 0a 20   to zero..  **. 
2f31b 20 2a 2a 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b   ** aConstraint[
2f31c 5d 2e 75 73 61 62 6c 65 20 69 73 20 74 72 75 65  ].usable is true
2f31d 20 66 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 73   for constraints
2f31e 20 77 68 65 72 65 20 74 68 65 20 72 69 67 68 74   where the right
2f31f 2d 68 61 6e 64 0a 20 20 2a 2a 20 73 69 64 65 20  -hand.  ** side 
2f320 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c 79 20 72 65  contains only re
2f321 66 65 72 65 6e 63 65 73 20 74 6f 20 74 61 62 6c  ferences to tabl
2f322 65 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f  es to the left o
2f323 66 20 74 68 65 20 63 75 72 72 65 6e 74 0a 20 20  f the current.  
2f324 2a 2a 20 74 61 62 6c 65 2e 20 20 49 6e 20 6f 74  ** table.  In ot
2f325 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20 74 68  her words, if th
2f326 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20  e constraint is 
2f327 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 20 20 2a  of the form:.  *
2f328 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20  *.  **          
2f329 20 63 6f 6c 75 6d 6e 20 3d 20 65 78 70 72 0a 20   column = expr. 
2f32a 20 2a 2a 0a 20 20 2a 2a 20 61 6e 64 20 77 65 20   **.  ** and we 
2f32b 61 72 65 20 65 76 61 6c 75 61 74 69 6e 67 20 61  are evaluating a
2f32c 20 6a 6f 69 6e 2c 20 74 68 65 6e 20 74 68 65 20   join, then the 
2f32d 63 6f 6e 73 74 72 61 69 6e 74 20 6f 6e 20 63 6f  constraint on co
2f32e 6c 75 6d 6e 20 69 73 20 0a 20 20 2a 2a 20 6f 6e  lumn is .  ** on
2f32f 6c 79 20 76 61 6c 69 64 20 69 66 20 61 6c 6c 20  ly valid if all 
2f330 74 61 62 6c 65 73 20 72 65 66 65 72 65 6e 63 65  tables reference
2f331 64 20 69 6e 20 65 78 70 72 20 6f 63 63 75 72 20  d in expr occur 
2f332 74 6f 20 74 68 65 20 6c 65 66 74 0a 20 20 2a 2a  to the left.  **
2f333 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f   of the table co
2f334 6e 74 61 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2e  ntaining column.
2f335 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 61  .  **.  ** The a
2f336 43 6f 6e 73 74 72 61 69 6e 74 73 5b 5d 20 61 72  Constraints[] ar
2f337 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 65 6e 74  ray contains ent
2f338 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 63 6f 6e  ries for all con
2f339 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a 20 6f 6e  straints.  ** on
2f33a 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61 62   the current tab
2f33b 6c 65 2e 20 20 54 68 61 74 20 77 61 79 20 77 65  le.  That way we
2f33c 20 6f 6e 6c 79 20 68 61 76 65 20 74 6f 20 63 6f   only have to co
2f33d 6d 70 75 74 65 20 69 74 20 6f 6e 63 65 0a 20 20  mpute it once.  
2f33e 2a 2a 20 65 76 65 6e 20 74 68 6f 75 67 68 20 77  ** even though w
2f33f 65 20 6d 69 67 68 74 20 74 72 79 20 74 6f 20 70  e might try to p
2f340 69 63 6b 20 74 68 65 20 62 65 73 74 20 69 6e 64  ick the best ind
2f341 65 78 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  ex multiple time
2f342 73 2e 0a 20 20 2a 2a 20 46 6f 72 20 65 61 63 68  s..  ** For each
2f343 20 61 74 74 65 6d 70 74 20 61 74 20 70 69 63 6b   attempt at pick
2f344 69 6e 67 20 61 6e 20 69 6e 64 65 78 2c 20 74 68  ing an index, th
2f345 65 20 6f 72 64 65 72 20 6f 66 20 74 61 62 6c 65  e order of table
2f346 73 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 6a 6f  s in the.  ** jo
2f347 69 6e 20 6d 69 67 68 74 20 62 65 20 64 69 66 66  in might be diff
2f348 65 72 65 6e 74 20 73 6f 20 77 65 20 68 61 76 65  erent so we have
2f349 20 74 6f 20 72 65 63 6f 6d 70 75 74 65 20 74 68   to recompute th
2f34a 65 20 75 73 61 62 6c 65 20 66 6c 61 67 0a 20 20  e usable flag.  
2f34b 2a 2a 20 65 61 63 68 20 74 69 6d 65 2e 0a 20 20  ** each time..  
2f34c 2a 2f 0a 20 20 70 49 64 78 43 6f 6e 73 20 3d 20  */.  pIdxCons = 
2f34d 2a 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  *(struct sqlite3
2f34e 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e  _index_constrain
2f34f 74 2a 2a 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61  t**)&pIdxInfo->a
2f350 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 70 55  Constraint;.  pU
2f351 73 61 67 65 20 3d 20 70 49 64 78 49 6e 66 6f 2d  sage = pIdxInfo-
2f352 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67  >aConstraintUsag
2f353 65 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  e;.  for(i=0; i<
2f354 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74  pIdxInfo->nConst
2f355 72 61 69 6e 74 3b 20 69 2b 2b 2c 20 70 49 64 78  raint; i++, pIdx
2f356 43 6f 6e 73 2b 2b 29 7b 0a 20 20 20 20 6a 20 3d  Cons++){.    j =
2f357 20 70 49 64 78 43 6f 6e 73 2d 3e 69 54 65 72 6d   pIdxCons->iTerm
2f358 4f 66 66 73 65 74 3b 0a 20 20 20 20 70 54 65 72  Offset;.    pTer
2f359 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 6a 5d 3b 0a  m = &pWC->a[j];.
2f35a 20 20 20 20 70 49 64 78 43 6f 6e 73 2d 3e 75 73      pIdxCons->us
2f35b 61 62 6c 65 20 3d 20 28 70 54 65 72 6d 2d 3e 70  able = (pTerm->p
2f35c 72 65 72 65 71 52 69 67 68 74 26 6e 6f 74 52 65  rereqRight&notRe
2f35d 61 64 79 29 20 3f 20 30 20 3a 20 31 3b 0a 20 20  ady) ? 0 : 1;.  
2f35e 7d 0a 20 20 6d 65 6d 73 65 74 28 70 55 73 61 67  }.  memset(pUsag
2f35f 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 70 55 73  e, 0, sizeof(pUs
2f360 61 67 65 5b 30 5d 29 2a 70 49 64 78 49 6e 66 6f  age[0])*pIdxInfo
2f361 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 29 3b 0a  ->nConstraint);.
2f362 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f 2d 3e    if( pIdxInfo->
2f363 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72  needToFreeIdxStr
2f364 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
2f365 66 72 65 65 28 70 49 64 78 49 6e 66 6f 2d 3e 69  free(pIdxInfo->i
2f366 64 78 53 74 72 29 3b 0a 20 20 7d 0a 20 20 70 49  dxStr);.  }.  pI
2f367 64 78 49 6e 66 6f 2d 3e 69 64 78 53 74 72 20 3d  dxInfo->idxStr =
2f368 20 30 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e   0;.  pIdxInfo->
2f369 69 64 78 4e 75 6d 20 3d 20 30 3b 0a 20 20 70 49  idxNum = 0;.  pI
2f36a 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72  dxInfo->needToFr
2f36b 65 65 49 64 78 53 74 72 20 3d 20 30 3b 0a 20 20  eeIdxStr = 0;.  
2f36c 70 49 64 78 49 6e 66 6f 2d 3e 6f 72 64 65 72 42  pIdxInfo->orderB
2f36d 79 43 6f 6e 73 75 6d 65 64 20 3d 20 30 3b 0a 20  yConsumed = 0;. 
2f36e 20 2f 2a 20 28 28 64 6f 75 62 6c 65 29 32 29 20   /* ((double)2) 
2f36f 49 6e 20 63 61 73 65 20 6f 66 20 53 51 4c 49 54  In case of SQLIT
2f370 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f  E_OMIT_FLOATING_
2f371 50 4f 49 4e 54 2e 2e 2e 20 2a 2f 0a 20 20 70 49  POINT... */.  pI
2f372 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65  dxInfo->estimate
2f373 64 43 6f 73 74 20 3d 20 53 51 4c 49 54 45 5f 42  dCost = SQLITE_B
2f374 49 47 5f 44 42 4c 20 2f 20 28 28 64 6f 75 62 6c  IG_DBL / ((doubl
2f375 65 29 32 29 3b 0a 20 20 6e 4f 72 64 65 72 42 79  e)2);.  nOrderBy
2f376 20 3d 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72   = pIdxInfo->nOr
2f377 64 65 72 42 79 3b 0a 20 20 69 66 28 20 21 70 4f  derBy;.  if( !pO
2f378 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 70 49  rderBy ){.    pI
2f379 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79  dxInfo->nOrderBy
2f37a 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 69 66 28   = 0;.  }..  if(
2f37b 20 76 74 61 62 42 65 73 74 49 6e 64 65 78 28 70   vtabBestIndex(p
2f37c 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70 49 64  Parse, pTab, pId
2f37d 78 49 6e 66 6f 29 20 29 7b 0a 20 20 20 20 72 65  xInfo) ){.    re
2f37e 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70 49 64  turn;.  }..  pId
2f37f 78 43 6f 6e 73 20 3d 20 2a 28 73 74 72 75 63 74  xCons = *(struct
2f380 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63   sqlite3_index_c
2f381 6f 6e 73 74 72 61 69 6e 74 2a 2a 29 26 70 49 64  onstraint**)&pId
2f382 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69  xInfo->aConstrai
2f383 6e 74 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  nt;.  for(i=0; i
2f384 3c 70 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73  <pIdxInfo->nCons
2f385 74 72 61 69 6e 74 3b 20 69 2b 2b 29 7b 0a 20 20  traint; i++){.  
2f386 20 20 69 66 28 20 70 55 73 61 67 65 5b 69 5d 2e    if( pUsage[i].
2f387 61 72 67 76 49 6e 64 65 78 3e 30 20 29 7b 0a 20  argvIndex>0 ){. 
2f388 20 20 20 20 20 70 43 6f 73 74 2d 3e 75 73 65 64       pCost->used
2f389 20 7c 3d 20 70 57 43 2d 3e 61 5b 70 49 64 78 43   |= pWC->a[pIdxC
2f38a 6f 6e 73 5b 69 5d 2e 69 54 65 72 6d 4f 66 66 73  ons[i].iTermOffs
2f38b 65 74 5d 2e 70 72 65 72 65 71 52 69 67 68 74 3b  et].prereqRight;
2f38c 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
2f38d 20 54 68 65 20 63 6f 73 74 20 69 73 20 6e 6f 74   The cost is not
2f38e 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65 20 6c   allowed to be l
2f38f 61 72 67 65 72 20 74 68 61 6e 20 53 51 4c 49 54  arger than SQLIT
2f390 45 5f 42 49 47 5f 44 42 4c 20 28 74 68 65 0a 20  E_BIG_DBL (the. 
2f391 20 2a 2a 20 69 6e 69 74 61 6c 20 76 61 6c 75 65   ** inital value
2f392 20 6f 66 20 6c 6f 77 65 73 74 43 6f 73 74 20 69   of lowestCost i
2f393 6e 20 74 68 69 73 20 6c 6f 6f 70 2e 20 49 66 20  n this loop. If 
2f394 69 74 20 69 73 2c 20 74 68 65 6e 20 74 68 65 0a  it is, then the.
2f395 20 20 2a 2a 20 28 63 6f 73 74 3c 6c 6f 77 65 73    ** (cost<lowes
2f396 74 43 6f 73 74 29 20 74 65 73 74 20 62 65 6c 6f  tCost) test belo
2f397 77 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20  w will never be 
2f398 74 72 75 65 2e 0a 20 20 2a 2a 20 0a 20 20 2a 2a  true..  ** .  **
2f399 20 55 73 65 20 22 28 64 6f 75 62 6c 65 29 32 22   Use "(double)2"
2f39a 20 69 6e 73 74 65 61 64 20 6f 66 20 22 32 2e 30   instead of "2.0
2f39b 22 20 69 6e 20 63 61 73 65 20 4f 4d 49 54 5f 46  " in case OMIT_F
2f39c 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20 0a 20  LOATING_POINT . 
2f39d 20 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 2e 0a   ** is defined..
2f39e 20 20 2a 2f 0a 20 20 69 66 28 20 28 53 51 4c 49    */.  if( (SQLI
2f39f 54 45 5f 42 49 47 5f 44 42 4c 2f 28 28 64 6f 75  TE_BIG_DBL/((dou
2f3a0 62 6c 65 29 32 29 29 3c 70 49 64 78 49 6e 66 6f  ble)2))<pIdxInfo
2f3a1 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74 20  ->estimatedCost 
2f3a2 29 7b 0a 20 20 20 20 70 43 6f 73 74 2d 3e 72 43  ){.    pCost->rC
2f3a3 6f 73 74 20 3d 20 28 53 51 4c 49 54 45 5f 42 49  ost = (SQLITE_BI
2f3a4 47 5f 44 42 4c 2f 28 28 64 6f 75 62 6c 65 29 32  G_DBL/((double)2
2f3a5 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ));.  }else{.   
2f3a6 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20   pCost->rCost = 
2f3a7 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74 69 6d 61  pIdxInfo->estima
2f3a8 74 65 64 43 6f 73 74 3b 0a 20 20 7d 0a 20 20 70  tedCost;.  }.  p
2f3a9 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70 56 74  Cost->plan.u.pVt
2f3aa 61 62 49 64 78 20 3d 20 70 49 64 78 49 6e 66 6f  abIdx = pIdxInfo
2f3ab 3b 0a 20 20 69 66 28 20 70 49 64 78 49 6e 66 6f  ;.  if( pIdxInfo
2f3ac 2d 3e 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65  ->orderByConsume
2f3ad 64 20 29 7b 0a 20 20 20 20 70 43 6f 73 74 2d 3e  d ){.    pCost->
2f3ae 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 7c 3d 20  plan.wsFlags |= 
2f3af 57 48 45 52 45 5f 4f 52 44 45 52 42 59 3b 0a 20  WHERE_ORDERBY;. 
2f3b0 20 7d 0a 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e   }.  pCost->plan
2f3b1 2e 6e 45 71 20 3d 20 30 3b 0a 20 20 70 49 64 78  .nEq = 0;.  pIdx
2f3b2 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 20 3d  Info->nOrderBy =
2f3b3 20 6e 4f 72 64 65 72 42 79 3b 0a 0a 20 20 2f 2a   nOrderBy;..  /*
2f3b4 20 54 72 79 20 74 6f 20 66 69 6e 64 20 61 20 6d   Try to find a m
2f3b5 6f 72 65 20 65 66 66 69 63 69 65 6e 74 20 61 63  ore efficient ac
2f3b6 63 65 73 73 20 70 61 74 74 65 72 6e 20 62 79 20  cess pattern by 
2f3b7 75 73 69 6e 67 20 6d 75 6c 74 69 70 6c 65 20 69  using multiple i
2f3b8 6e 64 65 78 65 73 0a 20 20 2a 2a 20 74 6f 20 6f  ndexes.  ** to o
2f3b9 70 74 69 6d 69 7a 65 20 61 6e 20 4f 52 20 65 78  ptimize an OR ex
2f3ba 70 72 65 73 73 69 6f 6e 20 77 69 74 68 69 6e 20  pression within 
2f3bb 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2f3bc 2e 20 0a 20 20 2a 2f 0a 20 20 62 65 73 74 4f 72  . .  */.  bestOr
2f3bd 43 6c 61 75 73 65 49 6e 64 65 78 28 70 50 61 72  ClauseIndex(pPar
2f3be 73 65 2c 20 70 57 43 2c 20 70 53 72 63 2c 20 6e  se, pWC, pSrc, n
2f3bf 6f 74 52 65 61 64 79 2c 20 70 4f 72 64 65 72 42  otReady, pOrderB
2f3c0 79 2c 20 70 43 6f 73 74 29 3b 0a 7d 0a 23 65 6e  y, pCost);.}.#en
2f3c1 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2f3c2 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20  IT_VIRTUALTABLE 
2f3c3 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65  */../*.** Argume
2f3c4 6e 74 20 70 49 64 78 20 69 73 20 61 20 70 6f 69  nt pIdx is a poi
2f3c5 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 64 65 78  nter to an index
2f3c6 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20   structure that 
2f3c7 68 61 73 20 61 6e 20 61 72 72 61 79 20 6f 66 0a  has an array of.
2f3c8 2a 2a 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f  ** SQLITE_INDEX_
2f3c9 53 41 4d 50 4c 45 53 20 65 76 65 6e 6c 79 20 73  SAMPLES evenly s
2f3ca 70 61 63 65 64 20 73 61 6d 70 6c 65 73 20 6f 66  paced samples of
2f3cb 20 74 68 65 20 66 69 72 73 74 20 69 6e 64 65 78   the first index
2f3cc 65 64 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 73 74 6f  ed column.** sto
2f3cd 72 65 64 20 69 6e 20 49 6e 64 65 78 2e 61 53 61  red in Index.aSa
2f3ce 6d 70 6c 65 2e 20 54 68 65 20 64 6f 6d 61 69 6e  mple. The domain
2f3cf 20 6f 66 20 76 61 6c 75 65 73 20 73 74 6f 72 65   of values store
2f3d0 64 20 69 6e 20 73 61 69 64 20 63 6f 6c 75 6d 6e  d in said column
2f3d1 0a 2a 2a 20 6d 61 79 20 62 65 20 74 68 6f 75 67  .** may be thoug
2f3d2 68 74 20 6f 66 20 61 73 20 64 69 76 69 64 65 64  ht of as divided
2f3d3 20 69 6e 74 6f 20 28 53 51 4c 49 54 45 5f 49 4e   into (SQLITE_IN
2f3d4 44 45 58 5f 53 41 4d 50 4c 45 53 2b 31 29 20 72  DEX_SAMPLES+1) r
2f3d5 65 67 69 6f 6e 73 2e 0a 2a 2a 20 52 65 67 69 6f  egions..** Regio
2f3d6 6e 20 30 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c  n 0 contains all
2f3d7 20 76 61 6c 75 65 73 20 73 6d 61 6c 6c 65 72 20   values smaller 
2f3d8 74 68 61 6e 20 74 68 65 20 66 69 72 73 74 20 73  than the first s
2f3d9 61 6d 70 6c 65 20 76 61 6c 75 65 2e 20 52 65 67  ample value. Reg
2f3da 69 6f 6e 0a 2a 2a 20 31 20 63 6f 6e 74 61 69 6e  ion.** 1 contain
2f3db 73 20 76 61 6c 75 65 73 20 6c 61 72 67 65 72 20  s values larger 
2f3dc 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
2f3dd 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
2f3de 65 20 66 69 72 73 74 20 73 61 6d 70 6c 65 2c 0a  e first sample,.
2f3df 2a 2a 20 62 75 74 20 73 6d 61 6c 6c 65 72 20 74  ** but smaller t
2f3e0 68 61 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66  han the value of
2f3e1 20 74 68 65 20 73 65 63 6f 6e 64 2e 20 41 6e 64   the second. And
2f3e2 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66   so on..**.** If
2f3e3 20 73 75 63 63 65 73 73 66 75 6c 2c 20 74 68 69   successful, thi
2f3e4 73 20 66 75 6e 63 74 69 6f 6e 20 64 65 74 65 72  s function deter
2f3e5 6d 69 6e 65 73 20 77 68 69 63 68 20 6f 66 20 74  mines which of t
2f3e6 68 65 20 72 65 67 69 6f 6e 73 20 76 61 6c 75 65  he regions value
2f3e7 20 0a 2a 2a 20 70 56 61 6c 20 6c 69 65 73 20 69   .** pVal lies i
2f3e8 6e 2c 20 73 65 74 73 20 2a 70 69 52 65 67 69 6f  n, sets *piRegio
2f3e9 6e 20 74 6f 20 74 68 65 20 72 65 67 69 6f 6e 20  n to the region 
2f3ea 69 6e 64 65 78 20 28 61 20 76 61 6c 75 65 20 62  index (a value b
2f3eb 65 74 77 65 65 6e 20 30 0a 2a 2a 20 61 6e 64 20  etween 0.** and 
2f3ec 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d  SQLITE_INDEX_SAM
2f3ed 50 4c 45 53 2b 31 2c 20 69 6e 63 6c 75 73 69 76  PLES+1, inclusiv
2f3ee 65 29 20 61 6e 64 20 72 65 74 75 72 6e 73 20 53  e) and returns S
2f3ef 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 20 4f 72 2c  QLITE_OK..** Or,
2f3f0 20 69 66 20 61 6e 20 4f 4f 4d 20 6f 63 63 75 72   if an OOM occur
2f3f1 73 20 77 68 69 6c 65 20 63 6f 6e 76 65 72 74 69  s while converti
2f3f2 6e 67 20 74 65 78 74 20 76 61 6c 75 65 73 20 62  ng text values b
2f3f3 65 74 77 65 65 6e 20 65 6e 63 6f 64 69 6e 67 73  etween encodings
2f3f4 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  ,.** SQLITE_NOME
2f3f5 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e  M is returned an
2f3f6 64 20 2a 70 69 52 65 67 69 6f 6e 20 69 73 20 75  d *piRegion is u
2f3f7 6e 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66  ndefined..*/.#if
2f3f8 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
2f3f9 45 5f 53 54 41 54 32 0a 73 74 61 74 69 63 20 69  E_STAT2.static i
2f3fa 6e 74 20 77 68 65 72 65 52 61 6e 67 65 52 65 67  nt whereRangeReg
2f3fb 69 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ion(.  Parse *pP
2f3fc 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  arse,           
2f3fd 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63     /* Database c
2f3fe 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 49  onnection */.  I
2f3ff 6e 64 65 78 20 2a 70 49 64 78 2c 20 20 20 20 20  ndex *pIdx,     
2f400 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
2f401 64 65 78 20 74 6f 20 63 6f 6e 73 69 64 65 72 20  dex to consider 
2f402 64 6f 6d 61 69 6e 20 6f 66 20 2a 2f 0a 20 20 73  domain of */.  s
2f403 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
2f404 61 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 56 61  al,        /* Va
2f405 6c 75 65 20 74 6f 20 63 6f 6e 73 69 64 65 72 20  lue to consider 
2f406 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 52 65 67 69  */.  int *piRegi
2f407 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  on              
2f408 20 2f 2a 20 4f 55 54 3a 20 52 65 67 69 6f 6e 20   /* OUT: Region 
2f409 6f 66 20 64 6f 6d 61 69 6e 20 69 6e 20 77 68 69  of domain in whi
2f40a 63 68 20 76 61 6c 75 65 20 6c 69 65 73 20 2a 2f  ch value lies */
2f40b 0a 29 7b 0a 20 20 69 66 28 20 41 4c 57 41 59 53  .){.  if( ALWAYS
2f40c 28 70 56 61 6c 29 20 29 7b 0a 20 20 20 20 49 6e  (pVal) ){.    In
2f40d 64 65 78 53 61 6d 70 6c 65 20 2a 61 53 61 6d 70  dexSample *aSamp
2f40e 6c 65 20 3d 20 70 49 64 78 2d 3e 61 53 61 6d 70  le = pIdx->aSamp
2f40f 6c 65 3b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20  le;.    int i = 
2f410 30 3b 0a 20 20 20 20 69 6e 74 20 65 54 79 70 65  0;.    int eType
2f411 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
2f412 5f 74 79 70 65 28 70 56 61 6c 29 3b 0a 0a 20 20  _type(pVal);..  
2f413 20 20 69 66 28 20 65 54 79 70 65 3d 3d 53 51 4c    if( eType==SQL
2f414 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 7c 20 65  ITE_INTEGER || e
2f415 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 46 4c 4f  Type==SQLITE_FLO
2f416 41 54 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62  AT ){.      doub
2f417 6c 65 20 72 20 3d 20 73 71 6c 69 74 65 33 5f 76  le r = sqlite3_v
2f418 61 6c 75 65 5f 64 6f 75 62 6c 65 28 70 56 61 6c  alue_double(pVal
2f419 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  );.      for(i=0
2f41a 3b 20 69 3c 53 51 4c 49 54 45 5f 49 4e 44 45 58  ; i<SQLITE_INDEX
2f41b 5f 53 41 4d 50 4c 45 53 3b 20 69 2b 2b 29 7b 0a  _SAMPLES; i++){.
2f41c 20 20 20 20 20 20 20 20 69 66 28 20 61 53 61 6d          if( aSam
2f41d 70 6c 65 5b 69 5d 2e 65 54 79 70 65 3d 3d 53 51  ple[i].eType==SQ
2f41e 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 63 6f 6e 74  LITE_NULL ) cont
2f41f 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 69 66  inue;.        if
2f420 28 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 65 54 79  ( aSample[i].eTy
2f421 70 65 3e 3d 53 51 4c 49 54 45 5f 54 45 58 54 20  pe>=SQLITE_TEXT 
2f422 7c 7c 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 75 2e  || aSample[i].u.
2f423 72 3e 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  r>r ) break;.   
2f424 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 20     }.    }else{ 
2f425 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a  .      sqlite3 *
2f426 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2f427 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  .      CollSeq *
2f428 70 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 63 6f 6e  pColl;.      con
2f429 73 74 20 75 38 20 2a 7a 3b 0a 20 20 20 20 20 20  st u8 *z;.      
2f42a 69 6e 74 20 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a  int n;..      /*
2f42b 20 70 56 61 6c 20 63 6f 6d 65 73 20 66 72 6f 6d   pVal comes from
2f42c 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 6f   sqlite3ValueFro
2f42d 6d 45 78 70 72 28 29 20 73 6f 20 74 68 65 20 74  mExpr() so the t
2f42e 79 70 65 20 63 61 6e 6e 6f 74 20 62 65 20 4e 55  ype cannot be NU
2f42f 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65  LL */.      asse
2f430 72 74 28 20 65 54 79 70 65 3d 3d 53 51 4c 49 54  rt( eType==SQLIT
2f431 45 5f 54 45 58 54 20 7c 7c 20 65 54 79 70 65 3d  E_TEXT || eType=
2f432 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 3b 0a  =SQLITE_BLOB );.
2f433 0a 20 20 20 20 20 20 69 66 28 20 65 54 79 70 65  .      if( eType
2f434 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b  ==SQLITE_BLOB ){
2f435 0a 20 20 20 20 20 20 20 20 7a 20 3d 20 28 63 6f  .        z = (co
2f436 6e 73 74 20 75 38 20 2a 29 73 71 6c 69 74 65 33  nst u8 *)sqlite3
2f437 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 70 56 61 6c  _value_blob(pVal
2f438 29 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  );.        pColl
2f439 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c   = db->pDfltColl
2f43a 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
2f43b 28 20 70 43 6f 6c 6c 2d 3e 65 6e 63 3d 3d 53 51  ( pColl->enc==SQ
2f43c 4c 49 54 45 5f 55 54 46 38 20 29 3b 0a 20 20 20  LITE_UTF8 );.   
2f43d 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2f43e 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
2f43f 33 47 65 74 43 6f 6c 6c 53 65 71 28 64 62 2c 20  3GetCollSeq(db, 
2f440 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 30 2c 20  SQLITE_UTF8, 0, 
2f441 2a 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 29 3b 0a  *pIdx->azColl);.
2f442 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c          if( pCol
2f443 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  l==0 ){.        
2f444 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
2f445 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75  g(pParse, "no su
2f446 63 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ch collation seq
2f447 75 65 6e 63 65 3a 20 25 73 22 2c 0a 20 20 20 20  uence: %s",.    
2f448 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f449 20 20 20 20 20 20 2a 70 49 64 78 2d 3e 61 7a 43        *pIdx->azC
2f44a 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20  oll);.          
2f44b 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
2f44c 52 4f 52 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ROR;.        }. 
2f44d 20 20 20 20 20 20 20 7a 20 3d 20 28 63 6f 6e 73         z = (cons
2f44e 74 20 75 38 20 2a 29 73 71 6c 69 74 65 33 56 61  t u8 *)sqlite3Va
2f44f 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 70 43  lueText(pVal, pC
2f450 6f 6c 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20  oll->enc);.     
2f451 20 20 20 69 66 28 20 21 7a 20 29 7b 0a 20 20 20     if( !z ){.   
2f452 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
2f453 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
2f454 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 61 73      }.        as
2f455 73 65 72 74 28 20 7a 20 26 26 20 70 43 6f 6c 6c  sert( z && pColl
2f456 20 26 26 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20   && pColl->xCmp 
2f457 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2f458 20 6e 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75   n = sqlite3Valu
2f459 65 42 79 74 65 73 28 70 56 61 6c 2c 20 70 43 6f  eBytes(pVal, pCo
2f45a 6c 6c 2d 3e 65 6e 63 29 3b 0a 0a 20 20 20 20 20  ll->enc);..     
2f45b 20 66 6f 72 28 69 3d 30 3b 20 69 3c 53 51 4c 49   for(i=0; i<SQLI
2f45c 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53  TE_INDEX_SAMPLES
2f45d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
2f45e 69 6e 74 20 72 3b 0a 20 20 20 20 20 20 20 20 69  int r;.        i
2f45f 6e 74 20 65 53 61 6d 70 6c 65 74 79 70 65 20 3d  nt eSampletype =
2f460 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 65 54 79 70   aSample[i].eTyp
2f461 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 65  e;.        if( e
2f462 53 61 6d 70 6c 65 74 79 70 65 3d 3d 53 51 4c 49  Sampletype==SQLI
2f463 54 45 5f 4e 55 4c 4c 20 7c 7c 20 65 53 61 6d 70  TE_NULL || eSamp
2f464 6c 65 74 79 70 65 3c 65 54 79 70 65 20 29 20 63  letype<eType ) c
2f465 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
2f466 20 69 66 28 20 28 65 53 61 6d 70 6c 65 74 79 70   if( (eSampletyp
2f467 65 21 3d 65 54 79 70 65 29 20 29 20 62 72 65 61  e!=eType) ) brea
2f468 6b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  k;.#ifndef SQLIT
2f469 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20 20  E_OMIT_UTF16.   
2f46a 20 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 2d 3e       if( pColl->
2f46b 65 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38  enc!=SQLITE_UTF8
2f46c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e   ){.          in
2f46d 74 20 6e 53 61 6d 70 6c 65 3b 0a 20 20 20 20 20  t nSample;.     
2f46e 20 20 20 20 20 63 68 61 72 20 2a 7a 53 61 6d 70       char *zSamp
2f46f 6c 65 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38  le = sqlite3Utf8
2f470 74 6f 31 36 28 0a 20 20 20 20 20 20 20 20 20 20  to16(.          
2f471 20 20 20 20 64 62 2c 20 70 43 6f 6c 6c 2d 3e 65      db, pColl->e
2f472 6e 63 2c 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 75  nc, aSample[i].u
2f473 2e 7a 2c 20 61 53 61 6d 70 6c 65 5b 69 5d 2e 6e  .z, aSample[i].n
2f474 42 79 74 65 2c 20 26 6e 53 61 6d 70 6c 65 0a 20  Byte, &nSample. 
2f475 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20           );.    
2f476 20 20 20 20 20 20 69 66 28 20 21 7a 53 61 6d 70        if( !zSamp
2f477 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  le ){.          
2f478 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d 61    assert( db->ma
2f479 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
2f47a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
2f47b 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
2f47c 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
2f47d 20 20 20 20 20 72 20 3d 20 70 43 6f 6c 6c 2d 3e       r = pColl->
2f47e 78 43 6d 70 28 70 43 6f 6c 6c 2d 3e 70 55 73 65  xCmp(pColl->pUse
2f47f 72 2c 20 6e 53 61 6d 70 6c 65 2c 20 7a 53 61 6d  r, nSample, zSam
2f480 70 6c 65 2c 20 6e 2c 20 7a 29 3b 0a 20 20 20 20  ple, n, z);.    
2f481 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
2f482 72 65 65 28 64 62 2c 20 7a 53 61 6d 70 6c 65 29  ree(db, zSample)
2f483 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 0a  ;.        }else.
2f484 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 7b  #endif.        {
2f485 0a 20 20 20 20 20 20 20 20 20 20 72 20 3d 20 70  .          r = p
2f486 43 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c 6c  Coll->xCmp(pColl
2f487 2d 3e 70 55 73 65 72 2c 20 61 53 61 6d 70 6c 65  ->pUser, aSample
2f488 5b 69 5d 2e 6e 42 79 74 65 2c 20 61 53 61 6d 70  [i].nByte, aSamp
2f489 6c 65 5b 69 5d 2e 75 2e 7a 2c 20 6e 2c 20 7a 29  le[i].u.z, n, z)
2f48a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2f48b 20 20 20 20 69 66 28 20 72 3e 30 20 29 20 62 72      if( r>0 ) br
2f48c 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2f48d 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20   }..    assert( 
2f48e 69 3e 3d 30 20 26 26 20 69 3c 3d 53 51 4c 49 54  i>=0 && i<=SQLIT
2f48f 45 5f 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 20  E_INDEX_SAMPLES 
2f490 29 3b 0a 20 20 20 20 2a 70 69 52 65 67 69 6f 6e  );.    *piRegion
2f491 20 3d 20 69 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = i;.  }.  retu
2f492 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
2f493 23 65 6e 64 69 66 20 20 20 2f 2a 20 23 69 66 64  #endif   /* #ifd
2f494 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
2f495 5f 53 54 41 54 32 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  _STAT2 */../*.**
2f496 20 49 66 20 65 78 70 72 65 73 73 69 6f 6e 20 70   If expression p
2f497 45 78 70 72 20 72 65 70 72 65 73 65 6e 74 73 20  Expr represents 
2f498 61 20 6c 69 74 65 72 61 6c 20 76 61 6c 75 65 2c  a literal value,
2f499 20 73 65 74 20 2a 70 70 20 74 6f 20 70 6f 69 6e   set *pp to poin
2f49a 74 20 74 6f 0a 2a 2a 20 61 6e 20 73 71 6c 69 74  t to.** an sqlit
2f49b 65 33 5f 76 61 6c 75 65 20 73 74 72 75 63 74 75  e3_value structu
2f49c 72 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  re containing th
2f49d 65 20 73 61 6d 65 20 76 61 6c 75 65 2c 20 77 69  e same value, wi
2f49e 74 68 20 61 66 66 69 6e 69 74 79 0a 2a 2a 20 61  th affinity.** a
2f49f 66 66 20 61 70 70 6c 69 65 64 20 74 6f 20 69 74  ff applied to it
2f4a0 2c 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  , before returni
2f4a1 6e 67 2e 20 49 74 20 69 73 20 74 68 65 20 72 65  ng. It is the re
2f4a2 73 70 6f 6e 73 69 62 69 6c 69 74 79 20 6f 66 20  sponsibility of 
2f4a3 74 68 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 20 74  the .** caller t
2f4a4 6f 20 65 76 65 6e 74 75 61 6c 6c 79 20 72 65 6c  o eventually rel
2f4a5 65 61 73 65 20 74 68 69 73 20 73 74 72 75 63 74  ease this struct
2f4a6 75 72 65 20 62 79 20 70 61 73 73 69 6e 67 20 69  ure by passing i
2f4a7 74 20 74 6f 20 0a 2a 2a 20 73 71 6c 69 74 65 33  t to .** sqlite3
2f4a8 56 61 6c 75 65 46 72 65 65 28 29 2e 0a 2a 2a 0a  ValueFree()..**.
2f4a9 2a 2a 20 49 66 20 74 68 65 20 63 75 72 72 65 6e  ** If the curren
2f4aa 74 20 70 61 72 73 65 20 69 73 20 61 20 72 65 63  t parse is a rec
2f4ab 6f 6d 70 69 6c 65 20 28 73 71 6c 69 74 65 33 52  ompile (sqlite3R
2f4ac 65 70 72 65 70 61 72 65 28 29 29 20 61 6e 64 20  eprepare()) and 
2f4ad 70 45 78 70 72 0a 2a 2a 20 69 73 20 61 6e 20 53  pExpr.** is an S
2f4ae 51 4c 20 76 61 72 69 61 62 6c 65 20 74 68 61 74  QL variable that
2f4af 20 63 75 72 72 65 6e 74 6c 79 20 68 61 73 20 61   currently has a
2f4b0 20 6e 6f 6e 2d 4e 55 4c 4c 20 76 61 6c 75 65 20   non-NULL value 
2f4b1 62 6f 75 6e 64 20 74 6f 20 69 74 2c 0a 2a 2a 20  bound to it,.** 
2f4b2 63 72 65 61 74 65 20 61 6e 20 73 71 6c 69 74 65  create an sqlite
2f4b3 33 5f 76 61 6c 75 65 20 73 74 72 75 63 74 75 72  3_value structur
2f4b4 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69  e containing thi
2f4b5 73 20 76 61 6c 75 65 2c 20 61 67 61 69 6e 20 77  s value, again w
2f4b6 69 74 68 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20  ith.** affinity 
2f4b7 61 66 66 20 61 70 70 6c 69 65 64 20 74 6f 20 69  aff applied to i
2f4b8 74 2c 20 69 6e 73 74 65 61 64 2e 0a 2a 2a 0a 2a  t, instead..**.*
2f4b9 2a 20 49 66 20 6e 65 69 74 68 65 72 20 6f 66 20  * If neither of 
2f4ba 74 68 65 20 61 62 6f 76 65 20 61 70 70 6c 79 2c  the above apply,
2f4bb 20 73 65 74 20 2a 70 70 20 74 6f 20 4e 55 4c 4c   set *pp to NULL
2f4bc 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72  ..**.** If an er
2f4bd 72 6f 72 20 6f 63 63 75 72 73 2c 20 72 65 74 75  ror occurs, retu
2f4be 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  rn an error code
2f4bf 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 53 51 4c  . Otherwise, SQL
2f4c0 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 23 69 66 64 65  ITE_OK..*/.#ifde
2f4c1 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
2f4c2 53 54 41 54 32 0a 73 74 61 74 69 63 20 69 6e 74  STAT2.static int
2f4c3 20 76 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 0a   valueFromExpr(.
2f4c4 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
2f4c5 20 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c   .  Expr *pExpr,
2f4c6 20 0a 20 20 75 38 20 61 66 66 2c 20 0a 20 20 73   .  u8 aff, .  s
2f4c7 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70  qlite3_value **p
2f4c8 70 0a 29 7b 0a 20 20 2f 2a 20 54 68 65 20 65 76  p.){.  /* The ev
2f4c9 61 6c 43 6f 6e 73 74 45 78 70 72 28 29 20 66 75  alConstExpr() fu
2f4ca 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 68 61 76 65  nction will have
2f4cb 20 61 6c 72 65 61 64 79 20 63 6f 6e 76 65 72 74   already convert
2f4cc 65 64 20 61 6e 79 20 54 4b 5f 56 41 52 49 41 42  ed any TK_VARIAB
2f4cd 4c 45 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 69  LE.  ** expressi
2f4ce 6f 6e 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 61  on involved in a
2f4cf 6e 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 74  n comparison int
2f4d0 6f 20 61 20 54 4b 5f 52 45 47 49 53 54 45 52 2e  o a TK_REGISTER.
2f4d1 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 45   */.  assert( pE
2f4d2 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 56 41 52 49  xpr->op!=TK_VARI
2f4d3 41 42 4c 45 20 29 3b 0a 20 20 69 66 28 20 70 45  ABLE );.  if( pE
2f4d4 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49  xpr->op==TK_REGI
2f4d5 53 54 45 52 20 26 26 20 70 45 78 70 72 2d 3e 6f  STER && pExpr->o
2f4d6 70 32 3d 3d 54 4b 5f 56 41 52 49 41 42 4c 45 20  p2==TK_VARIABLE 
2f4d7 29 7b 0a 20 20 20 20 69 6e 74 20 69 56 61 72 20  ){.    int iVar 
2f4d8 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  = pExpr->iColumn
2f4d9 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2f4da 65 53 65 74 56 61 72 6d 61 73 6b 28 70 50 61 72  eSetVarmask(pPar
2f4db 73 65 2d 3e 70 56 64 62 65 2c 20 69 56 61 72 29  se->pVdbe, iVar)
2f4dc 3b 0a 20 20 20 20 2a 70 70 20 3d 20 73 71 6c 69  ;.    *pp = sqli
2f4dd 74 65 33 56 64 62 65 47 65 74 56 61 6c 75 65 28  te3VdbeGetValue(
2f4de 70 50 61 72 73 65 2d 3e 70 52 65 70 72 65 70 61  pParse->pReprepa
2f4df 72 65 2c 20 69 56 61 72 2c 20 61 66 66 29 3b 0a  re, iVar, aff);.
2f4e0 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2f4e1 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75  E_OK;.  }.  retu
2f4e2 72 6e 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46  rn sqlite3ValueF
2f4e3 72 6f 6d 45 78 70 72 28 70 50 61 72 73 65 2d 3e  romExpr(pParse->
2f4e4 64 62 2c 20 70 45 78 70 72 2c 20 53 51 4c 49 54  db, pExpr, SQLIT
2f4e5 45 5f 55 54 46 38 2c 20 61 66 66 2c 20 70 70 29  E_UTF8, aff, pp)
2f4e6 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
2f4e7 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
2f4e8 69 73 20 75 73 65 64 20 74 6f 20 65 73 74 69 6d  is used to estim
2f4e9 61 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  ate the number o
2f4ea 66 20 72 6f 77 73 20 74 68 61 74 20 77 69 6c 6c  f rows that will
2f4eb 20 62 65 20 76 69 73 69 74 65 64 0a 2a 2a 20 62   be visited.** b
2f4ec 79 20 73 63 61 6e 6e 69 6e 67 20 61 6e 20 69 6e  y scanning an in
2f4ed 64 65 78 20 66 6f 72 20 61 20 72 61 6e 67 65 20  dex for a range 
2f4ee 6f 66 20 76 61 6c 75 65 73 2e 20 54 68 65 20 72  of values. The r
2f4ef 61 6e 67 65 20 6d 61 79 20 68 61 76 65 20 61 6e  ange may have an
2f4f0 20 75 70 70 65 72 0a 2a 2a 20 62 6f 75 6e 64 2c   upper.** bound,
2f4f1 20 61 20 6c 6f 77 65 72 20 62 6f 75 6e 64 2c 20   a lower bound, 
2f4f2 6f 72 20 62 6f 74 68 2e 20 54 68 65 20 57 48 45  or both. The WHE
2f4f3 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20  RE clause terms 
2f4f4 74 68 61 74 20 73 65 74 20 74 68 65 20 75 70 70  that set the upp
2f4f5 65 72 0a 2a 2a 20 61 6e 64 20 6c 6f 77 65 72 20  er.** and lower 
2f4f6 62 6f 75 6e 64 73 20 61 72 65 20 72 65 70 72 65  bounds are repre
2f4f7 73 65 6e 74 65 64 20 62 79 20 70 4c 6f 77 65 72  sented by pLower
2f4f8 20 61 6e 64 20 70 55 70 70 65 72 20 72 65 73 70   and pUpper resp
2f4f9 65 63 74 69 76 65 6c 79 2e 20 46 6f 72 0a 2a 2a  ectively. For.**
2f4fa 20 65 78 61 6d 70 6c 65 2c 20 61 73 73 75 6d 69   example, assumi
2f4fb 6e 67 20 74 68 61 74 20 69 6e 64 65 78 20 70 20  ng that index p 
2f4fc 69 73 20 6f 6e 20 74 31 28 61 29 3a 0a 2a 2a 0a  is on t1(a):.**.
2f4fd 2a 2a 20 20 20 2e 2e 2e 20 46 52 4f 4d 20 74 31  **   ... FROM t1
2f4fe 20 57 48 45 52 45 20 61 20 3e 20 3f 20 41 4e 44   WHERE a > ? AND
2f4ff 20 61 20 3c 20 3f 20 2e 2e 2e 0a 2a 2a 20 20 20   a < ? ....**   
2f500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f501 20 7c 5f 5f 5f 5f 5f 7c 20 20 20 7c 5f 5f 5f 5f   |_____|   |____
2f502 5f 7c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  _|.**           
2f503 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 20 20              |   
2f504 20 20 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20 20        |.**      
2f505 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
2f506 4c 6f 77 65 72 20 20 20 20 70 55 70 70 65 72 0a  Lower    pUpper.
2f507 2a 2a 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20  **.** If either 
2f508 6f 66 20 74 68 65 20 75 70 70 65 72 20 6f 72 20  of the upper or 
2f509 6c 6f 77 65 72 20 62 6f 75 6e 64 20 69 73 20 6e  lower bound is n
2f50a 6f 74 20 70 72 65 73 65 6e 74 2c 20 74 68 65 6e  ot present, then
2f50b 20 4e 55 4c 4c 20 69 73 20 70 61 73 73 65 64 20   NULL is passed 
2f50c 69 6e 0a 2a 2a 20 70 6c 61 63 65 20 6f 66 20 74  in.** place of t
2f50d 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  he corresponding
2f50e 20 57 68 65 72 65 54 65 72 6d 2e 0a 2a 2a 0a 2a   WhereTerm..**.*
2f50f 2a 20 54 68 65 20 6e 45 71 20 70 61 72 61 6d 65  * The nEq parame
2f510 74 65 72 20 69 73 20 70 61 73 73 65 64 20 74 68  ter is passed th
2f511 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 69  e index of the i
2f512 6e 64 65 78 20 63 6f 6c 75 6d 6e 20 73 75 62 6a  ndex column subj
2f513 65 63 74 20 74 6f 20 74 68 65 0a 2a 2a 20 72 61  ect to the.** ra
2f514 6e 67 65 20 63 6f 6e 73 74 72 61 69 6e 74 2e 20  nge constraint. 
2f515 4f 72 2c 20 65 71 75 69 76 61 6c 65 6e 74 6c 79  Or, equivalently
2f516 2c 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  , the number of 
2f517 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
2f518 69 6e 74 73 0a 2a 2a 20 6f 70 74 69 6d 69 7a 65  ints.** optimize
2f519 64 20 62 79 20 74 68 65 20 70 72 6f 70 6f 73 65  d by the propose
2f51a 64 20 69 6e 64 65 78 20 73 63 61 6e 2e 20 46 6f  d index scan. Fo
2f51b 72 20 65 78 61 6d 70 6c 65 2c 20 61 73 73 75 6d  r example, assum
2f51c 69 6e 67 20 69 6e 64 65 78 20 70 20 69 73 0a 2a  ing index p is.*
2f51d 2a 20 6f 6e 20 74 31 28 61 2c 20 62 29 2c 20 61  * on t1(a, b), a
2f51e 6e 64 20 74 68 65 20 53 51 4c 20 71 75 65 72 79  nd the SQL query
2f51f 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2e 2e 2e   is:.**.**   ...
2f520 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61   FROM t1 WHERE a
2f521 20 3d 20 3f 20 41 4e 44 20 62 20 3e 20 3f 20 41   = ? AND b > ? A
2f522 4e 44 20 62 20 3c 20 3f 20 2e 2e 2e 0a 2a 2a 0a  ND b < ? ....**.
2f523 2a 2a 20 74 68 65 6e 20 6e 45 71 20 73 68 6f 75  ** then nEq shou
2f524 6c 64 20 62 65 20 70 61 73 73 65 64 20 74 68 65  ld be passed the
2f525 20 76 61 6c 75 65 20 31 20 28 61 73 20 74 68 65   value 1 (as the
2f526 20 72 61 6e 67 65 20 72 65 73 74 72 69 63 74 65   range restricte
2f527 64 20 63 6f 6c 75 6d 6e 2c 0a 2a 2a 20 62 2c 20  d column,.** b, 
2f528 69 73 20 74 68 65 20 73 65 63 6f 6e 64 20 6c 65  is the second le
2f529 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f  ft-most column o
2f52a 66 20 74 68 65 20 69 6e 64 65 78 29 2e 20 4f 72  f the index). Or
2f52b 2c 20 69 66 20 74 68 65 20 71 75 65 72 79 20 69  , if the query i
2f52c 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 2e 2e 2e 20 46  s:.**.**   ... F
2f52d 52 4f 4d 20 74 31 20 57 48 45 52 45 20 61 20 3e  ROM t1 WHERE a >
2f52e 20 3f 20 41 4e 44 20 61 20 3c 20 3f 20 2e 2e 2e   ? AND a < ? ...
2f52f 0a 2a 2a 0a 2a 2a 20 74 68 65 6e 20 6e 45 71 20  .**.** then nEq 
2f530 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64  should be passed
2f531 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65   0..**.** The re
2f532 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20  turned value is 
2f533 61 6e 20 69 6e 74 65 67 65 72 20 62 65 74 77 65  an integer betwe
2f534 65 6e 20 31 20 61 6e 64 20 31 30 30 2c 20 69 6e  en 1 and 100, in
2f535 63 6c 75 73 69 76 65 2e 20 41 20 72 65 74 75 72  clusive. A retur
2f536 6e 0a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 31 20  n.** value of 1 
2f537 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20 74  indicates that t
2f538 68 65 20 70 72 6f 70 6f 73 65 64 20 72 61 6e 67  he proposed rang
2f539 65 20 73 63 61 6e 20 69 73 20 65 78 70 65 63 74  e scan is expect
2f53a 65 64 20 74 6f 20 76 69 73 69 74 0a 2a 2a 20 61  ed to visit.** a
2f53b 70 70 72 6f 78 69 6d 61 74 65 6c 79 20 31 2f 31  pproximately 1/1
2f53c 30 30 74 68 20 28 31 25 29 20 6f 66 20 74 68 65  00th (1%) of the
2f53d 20 72 6f 77 73 20 73 65 6c 65 63 74 65 64 20 62   rows selected b
2f53e 79 20 74 68 65 20 6e 45 71 20 65 71 75 61 6c 69  y the nEq equali
2f53f 74 79 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74  ty.** constraint
2f540 73 20 28 69 66 20 61 6e 79 29 2e 20 41 20 72 65  s (if any). A re
2f541 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 31 30  turn value of 10
2f542 30 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74  0 indicates that
2f543 20 69 74 20 69 73 20 65 78 70 65 63 74 65 64 0a   it is expected.
2f544 2a 2a 20 74 68 61 74 20 74 68 65 20 72 61 6e 67  ** that the rang
2f545 65 20 73 63 61 6e 20 77 69 6c 6c 20 76 69 73 69  e scan will visi
2f546 74 20 65 76 65 72 79 20 72 6f 77 20 28 31 30 30  t every row (100
2f547 25 29 20 73 65 6c 65 63 74 65 64 20 62 79 20 74  %) selected by t
2f548 68 65 20 65 71 75 61 6c 69 74 79 0a 2a 2a 20 63  he equality.** c
2f549 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 2a 2a 0a 2a  onstraints..**.*
2f54a 2a 20 49 6e 20 74 68 65 20 61 62 73 65 6e 63 65  * In the absence
2f54b 20 6f 66 20 73 71 6c 69 74 65 5f 73 74 61 74 32   of sqlite_stat2
2f54c 20 41 4e 41 4c 59 5a 45 20 64 61 74 61 2c 20 65   ANALYZE data, e
2f54d 61 63 68 20 72 61 6e 67 65 20 69 6e 65 71 75 61  ach range inequa
2f54e 6c 69 74 79 0a 2a 2a 20 72 65 64 75 63 65 73 20  lity.** reduces 
2f54f 74 68 65 20 73 65 61 72 63 68 20 73 70 61 63 65  the search space
2f550 20 62 79 20 32 2f 33 72 64 73 2e 20 20 48 65 6e   by 2/3rds.  Hen
2f551 63 65 20 61 20 73 69 6e 67 6c 65 20 63 6f 6e 73  ce a single cons
2f552 74 72 61 69 6e 74 20 28 78 3e 3f 29 0a 2a 2a 20  traint (x>?).** 
2f553 72 65 73 75 6c 74 73 20 69 6e 20 61 20 72 65 74  results in a ret
2f554 75 72 6e 20 6f 66 20 33 33 20 61 6e 64 20 61 20  urn of 33 and a 
2f555 72 61 6e 67 65 20 63 6f 6e 73 74 72 61 69 6e 74  range constraint
2f556 20 28 78 3e 3f 20 41 4e 44 20 78 3c 3f 29 20 72   (x>? AND x<?) r
2f557 65 73 75 6c 74 73 0a 2a 2a 20 69 6e 20 61 20 72  esults.** in a r
2f558 65 74 75 72 6e 20 6f 66 20 31 31 2e 0a 2a 2f 0a  eturn of 11..*/.
2f559 73 74 61 74 69 63 20 69 6e 74 20 77 68 65 72 65  static int where
2f55a 52 61 6e 67 65 53 63 61 6e 45 73 74 28 0a 20 20  RangeScanEst(.  
2f55b 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
2f55c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
2f55d 26 20 63 6f 64 65 20 67 65 6e 65 72 61 74 69 6e  & code generatin
2f55e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 49  g context */.  I
2f55f 6e 64 65 78 20 2a 70 2c 20 20 20 20 20 20 20 20  ndex *p,        
2f560 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78      /* The index
2f561 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
2f562 72 61 6e 67 65 2d 63 6f 6d 70 61 72 65 64 20 63  range-compared c
2f563 6f 6c 75 6d 6e 3b 20 22 78 22 20 2a 2f 0a 20 20  olumn; "x" */.  
2f564 69 6e 74 20 6e 45 71 2c 20 20 20 20 20 20 20 20  int nEq,        
2f565 20 20 20 20 20 2f 2a 20 69 6e 64 65 78 20 69 6e       /* index in
2f566 74 6f 20 70 2d 3e 61 43 6f 6c 5b 5d 20 6f 66 20  to p->aCol[] of 
2f567 74 68 65 20 72 61 6e 67 65 2d 63 6f 6d 70 61 72  the range-compar
2f568 65 64 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 57  ed column */.  W
2f569 68 65 72 65 54 65 72 6d 20 2a 70 4c 6f 77 65 72  hereTerm *pLower
2f56a 2c 20 20 20 2f 2a 20 4c 6f 77 65 72 20 62 6f 75  ,   /* Lower bou
2f56b 6e 64 20 6f 6e 20 74 68 65 20 72 61 6e 67 65 2e  nd on the range.
2f56c 20 65 78 3a 20 22 78 3e 31 32 33 22 20 4d 69 67   ex: "x>123" Mig
2f56d 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20  ht be NULL */.  
2f56e 57 68 65 72 65 54 65 72 6d 20 2a 70 55 70 70 65  WhereTerm *pUppe
2f56f 72 2c 20 20 20 2f 2a 20 55 70 70 65 72 20 62 6f  r,   /* Upper bo
2f570 75 6e 64 20 6f 6e 20 74 68 65 20 72 61 6e 67 65  und on the range
2f571 2e 20 65 78 3a 20 22 78 3c 34 35 35 22 20 4d 69  . ex: "x<455" Mi
2f572 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20  ght be NULL */. 
2f573 20 69 6e 74 20 2a 70 69 45 73 74 20 20 20 20 20   int *piEst     
2f574 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65        /* OUT: Re
2f575 74 75 72 6e 20 76 61 6c 75 65 20 2a 2f 0a 29 7b  turn value */.){
2f576 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
2f577 54 45 5f 4f 4b 3b 0a 0a 23 69 66 64 65 66 20 53  TE_OK;..#ifdef S
2f578 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 53 54 41  QLITE_ENABLE_STA
2f579 54 32 0a 0a 20 20 69 66 28 20 6e 45 71 3d 3d 30  T2..  if( nEq==0
2f57a 20 26 26 20 70 2d 3e 61 53 61 6d 70 6c 65 20 29   && p->aSample )
2f57b 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61  {.    sqlite3_va
2f57c 6c 75 65 20 2a 70 4c 6f 77 65 72 56 61 6c 20 3d  lue *pLowerVal =
2f57d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   0;.    sqlite3_
2f57e 76 61 6c 75 65 20 2a 70 55 70 70 65 72 56 61 6c  value *pUpperVal
2f57f 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 69 45   = 0;.    int iE
2f580 73 74 3b 0a 20 20 20 20 69 6e 74 20 69 4c 6f 77  st;.    int iLow
2f581 65 72 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  er = 0;.    int 
2f582 69 55 70 70 65 72 20 3d 20 53 51 4c 49 54 45 5f  iUpper = SQLITE_
2f583 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 3b 0a 20  INDEX_SAMPLES;. 
2f584 20 20 20 75 38 20 61 66 66 20 3d 20 70 2d 3e 70     u8 aff = p->p
2f585 54 61 62 6c 65 2d 3e 61 43 6f 6c 5b 70 2d 3e 61  Table->aCol[p->a
2f586 69 43 6f 6c 75 6d 6e 5b 30 5d 5d 2e 61 66 66 69  iColumn[0]].affi
2f587 6e 69 74 79 3b 0a 0a 20 20 20 20 69 66 28 20 70  nity;..    if( p
2f588 4c 6f 77 65 72 20 29 7b 0a 20 20 20 20 20 20 45  Lower ){.      E
2f589 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 4c 6f  xpr *pExpr = pLo
2f58a 77 65 72 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67  wer->pExpr->pRig
2f58b 68 74 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 76  ht;.      rc = v
2f58c 61 6c 75 65 46 72 6f 6d 45 78 70 72 28 70 50 61  alueFromExpr(pPa
2f58d 72 73 65 2c 20 70 45 78 70 72 2c 20 61 66 66 2c  rse, pExpr, aff,
2f58e 20 26 70 4c 6f 77 65 72 56 61 6c 29 3b 0a 20 20   &pLowerVal);.  
2f58f 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d    }.    if( rc==
2f590 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 55 70  SQLITE_OK && pUp
2f591 70 65 72 20 29 7b 0a 20 20 20 20 20 20 45 78 70  per ){.      Exp
2f592 72 20 2a 70 45 78 70 72 20 3d 20 70 55 70 70 65  r *pExpr = pUppe
2f593 72 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74  r->pExpr->pRight
2f594 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 76 61 6c  ;.      rc = val
2f595 75 65 46 72 6f 6d 45 78 70 72 28 70 50 61 72 73  ueFromExpr(pPars
2f596 65 2c 20 70 45 78 70 72 2c 20 61 66 66 2c 20 26  e, pExpr, aff, &
2f597 70 55 70 70 65 72 56 61 6c 29 3b 0a 20 20 20 20  pUpperVal);.    
2f598 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  }..    if( rc!=S
2f599 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 28 70 4c 6f  QLITE_OK || (pLo
2f59a 77 65 72 56 61 6c 3d 3d 30 20 26 26 20 70 55 70  werVal==0 && pUp
2f59b 70 65 72 56 61 6c 3d 3d 30 29 20 29 7b 0a 20 20  perVal==0) ){.  
2f59c 20 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65      sqlite3Value
2f59d 46 72 65 65 28 70 4c 6f 77 65 72 56 61 6c 29 3b  Free(pLowerVal);
2f59e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61  .      sqlite3Va
2f59f 6c 75 65 46 72 65 65 28 70 55 70 70 65 72 56 61  lueFree(pUpperVa
2f5a0 6c 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 72  l);.      goto r
2f5a1 61 6e 67 65 5f 65 73 74 5f 66 61 6c 6c 62 61 63  ange_est_fallbac
2f5a2 6b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  k;.    }else if(
2f5a3 20 70 4c 6f 77 65 72 56 61 6c 3d 3d 30 20 29 7b   pLowerVal==0 ){
2f5a4 0a 20 20 20 20 20 20 72 63 20 3d 20 77 68 65 72  .      rc = wher
2f5a5 65 52 61 6e 67 65 52 65 67 69 6f 6e 28 70 50 61  eRangeRegion(pPa
2f5a6 72 73 65 2c 20 70 2c 20 70 55 70 70 65 72 56 61  rse, p, pUpperVa
2f5a7 6c 2c 20 26 69 55 70 70 65 72 29 3b 0a 20 20 20  l, &iUpper);.   
2f5a8 20 20 20 69 66 28 20 70 4c 6f 77 65 72 20 29 20     if( pLower ) 
2f5a9 69 4c 6f 77 65 72 20 3d 20 69 55 70 70 65 72 2f  iLower = iUpper/
2f5aa 32 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  2;.    }else if(
2f5ab 20 70 55 70 70 65 72 56 61 6c 3d 3d 30 20 29 7b   pUpperVal==0 ){
2f5ac 0a 20 20 20 20 20 20 72 63 20 3d 20 77 68 65 72  .      rc = wher
2f5ad 65 52 61 6e 67 65 52 65 67 69 6f 6e 28 70 50 61  eRangeRegion(pPa
2f5ae 72 73 65 2c 20 70 2c 20 70 4c 6f 77 65 72 56 61  rse, p, pLowerVa
2f5af 6c 2c 20 26 69 4c 6f 77 65 72 29 3b 0a 20 20 20  l, &iLower);.   
2f5b0 20 20 20 69 66 28 20 70 55 70 70 65 72 20 29 20     if( pUpper ) 
2f5b1 69 55 70 70 65 72 20 3d 20 28 69 4c 6f 77 65 72  iUpper = (iLower
2f5b2 20 2b 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f   + SQLITE_INDEX_
2f5b3 53 41 4d 50 4c 45 53 20 2b 20 31 29 2f 32 3b 0a  SAMPLES + 1)/2;.
2f5b4 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2f5b5 20 72 63 20 3d 20 77 68 65 72 65 52 61 6e 67 65   rc = whereRange
2f5b6 52 65 67 69 6f 6e 28 70 50 61 72 73 65 2c 20 70  Region(pParse, p
2f5b7 2c 20 70 55 70 70 65 72 56 61 6c 2c 20 26 69 55  , pUpperVal, &iU
2f5b8 70 70 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28  pper);.      if(
2f5b9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
2f5ba 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 77  {.        rc = w
2f5bb 68 65 72 65 52 61 6e 67 65 52 65 67 69 6f 6e 28  hereRangeRegion(
2f5bc 70 50 61 72 73 65 2c 20 70 2c 20 70 4c 6f 77 65  pParse, p, pLowe
2f5bd 72 56 61 6c 2c 20 26 69 4c 6f 77 65 72 29 3b 0a  rVal, &iLower);.
2f5be 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
2f5bf 20 20 20 69 45 73 74 20 3d 20 69 55 70 70 65 72     iEst = iUpper
2f5c0 20 2d 20 69 4c 6f 77 65 72 3b 0a 20 20 20 20 74   - iLower;.    t
2f5c1 65 73 74 63 61 73 65 28 20 69 45 73 74 3d 3d 53  estcase( iEst==S
2f5c2 51 4c 49 54 45 5f 49 4e 44 45 58 5f 53 41 4d 50  QLITE_INDEX_SAMP
2f5c3 4c 45 53 20 29 3b 0a 20 20 20 20 61 73 73 65 72  LES );.    asser
2f5c4 74 28 20 69 45 73 74 3c 3d 53 51 4c 49 54 45 5f  t( iEst<=SQLITE_
2f5c5 49 4e 44 45 58 5f 53 41 4d 50 4c 45 53 20 29 3b  INDEX_SAMPLES );
2f5c6 0a 20 20 20 20 69 66 28 20 69 45 73 74 3c 31 20  .    if( iEst<1 
2f5c7 29 7b 0a 20 20 20 20 20 20 69 45 73 74 20 3d 20  ){.      iEst = 
2f5c8 31 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73 71  1;.    }..    sq
2f5c9 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70  lite3ValueFree(p
2f5ca 4c 6f 77 65 72 56 61 6c 29 3b 0a 20 20 20 20 73  LowerVal);.    s
2f5cb 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
2f5cc 70 55 70 70 65 72 56 61 6c 29 3b 0a 20 20 20 20  pUpperVal);.    
2f5cd 2a 70 69 45 73 74 20 3d 20 28 69 45 73 74 20 2a  *piEst = (iEst *
2f5ce 20 31 30 30 29 2f 53 51 4c 49 54 45 5f 49 4e 44   100)/SQLITE_IND
2f5cf 45 58 5f 53 41 4d 50 4c 45 53 3b 0a 20 20 20 20  EX_SAMPLES;.    
2f5d0 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 72  return rc;.  }.r
2f5d1 61 6e 67 65 5f 65 73 74 5f 66 61 6c 6c 62 61 63  ange_est_fallbac
2f5d2 6b 3a 0a 23 65 6c 73 65 0a 20 20 55 4e 55 53 45  k:.#else.  UNUSE
2f5d3 44 5f 50 41 52 41 4d 45 54 45 52 28 70 50 61 72  D_PARAMETER(pPar
2f5d4 73 65 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  se);.  UNUSED_PA
2f5d5 52 41 4d 45 54 45 52 28 70 29 3b 0a 20 20 55 4e  RAMETER(p);.  UN
2f5d6 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 6e  USED_PARAMETER(n
2f5d7 45 71 29 3b 0a 23 65 6e 64 69 66 0a 20 20 61 73  Eq);.#endif.  as
2f5d8 73 65 72 74 28 20 70 4c 6f 77 65 72 20 7c 7c 20  sert( pLower || 
2f5d9 70 55 70 70 65 72 20 29 3b 0a 20 20 69 66 28 20  pUpper );.  if( 
2f5da 70 4c 6f 77 65 72 20 26 26 20 70 55 70 70 65 72  pLower && pUpper
2f5db 20 29 7b 0a 20 20 20 20 2a 70 69 45 73 74 20 3d   ){.    *piEst =
2f5dc 20 31 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20   11;.  }else{.  
2f5dd 20 20 2a 70 69 45 73 74 20 3d 20 33 33 3b 0a 20    *piEst = 33;. 
2f5de 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
2f5df 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74  }.../*.** Find t
2f5e0 68 65 20 71 75 65 72 79 20 70 6c 61 6e 20 66 6f  he query plan fo
2f5e1 72 20 61 63 63 65 73 73 69 6e 67 20 61 20 70 61  r accessing a pa
2f5e2 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 2e 20  rticular table. 
2f5e3 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 62 65   Write the.** be
2f5e4 73 74 20 71 75 65 72 79 20 70 6c 61 6e 20 61 6e  st query plan an
2f5e5 64 20 69 74 73 20 63 6f 73 74 20 69 6e 74 6f 20  d its cost into 
2f5e6 74 68 65 20 57 68 65 72 65 43 6f 73 74 20 6f 62  the WhereCost ob
2f5e7 6a 65 63 74 20 73 75 70 70 6c 69 65 64 20 61 73  ject supplied as
2f5e8 20 74 68 65 0a 2a 2a 20 6c 61 73 74 20 70 61 72   the.** last par
2f5e9 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  ameter..**.** Th
2f5ea 65 20 6c 6f 77 65 73 74 20 63 6f 73 74 20 70 6c  e lowest cost pl
2f5eb 61 6e 20 77 69 6e 73 2e 20 20 54 68 65 20 63 6f  an wins.  The co
2f5ec 73 74 20 69 73 20 61 6e 20 65 73 74 69 6d 61 74  st is an estimat
2f5ed 65 20 6f 66 20 74 68 65 20 61 6d 6f 75 6e 74 20  e of the amount 
2f5ee 6f 66 0a 2a 2a 20 43 50 55 20 61 6e 64 20 64 69  of.** CPU and di
2f5ef 73 6b 20 49 2f 4f 20 6e 65 65 64 20 74 6f 20 70  sk I/O need to p
2f5f0 72 6f 63 65 73 73 20 74 68 65 20 72 65 71 75 65  rocess the reque
2f5f1 73 74 20 75 73 69 6e 67 20 74 68 65 20 73 65 6c  st using the sel
2f5f2 65 63 74 65 64 20 70 6c 61 6e 2e 0a 2a 2a 20 46  ected plan..** F
2f5f3 61 63 74 6f 72 73 20 74 68 61 74 20 69 6e 66 6c  actors that infl
2f5f4 75 65 6e 63 65 20 63 6f 73 74 20 69 6e 63 6c 75  uence cost inclu
2f5f5 64 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20  de:.**.**    *  
2f5f6 54 68 65 20 65 73 74 69 6d 61 74 65 64 20 6e 75  The estimated nu
2f5f7 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 74 68 61  mber of rows tha
2f5f8 74 20 77 69 6c 6c 20 62 65 20 72 65 74 72 69 65  t will be retrie
2f5f9 76 65 64 2e 20 20 28 54 68 65 0a 2a 2a 20 20 20  ved.  (The.**   
2f5fa 20 20 20 20 66 65 77 65 72 20 74 68 65 20 62 65      fewer the be
2f5fb 74 74 65 72 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 20  tter.).**.**    
2f5fc 2a 20 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f  *  Whether or no
2f5fd 74 20 73 6f 72 74 69 6e 67 20 6d 75 73 74 20 6f  t sorting must o
2f5fe 63 63 75 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a  ccur..**.**    *
2f5ff 20 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74    Whether or not
2f600 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 73   there must be s
2f601 65 70 61 72 61 74 65 20 6c 6f 6f 6b 75 70 73 20  eparate lookups 
2f602 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  in the.**       
2f603 69 6e 64 65 78 20 61 6e 64 20 69 6e 20 74 68 65  index and in the
2f604 20 6d 61 69 6e 20 74 61 62 6c 65 2e 0a 2a 2a 0a   main table..**.
2f605 2a 2a 20 49 66 20 74 68 65 72 65 20 77 61 73 20  ** If there was 
2f606 61 6e 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c  an INDEXED BY cl
2f607 61 75 73 65 20 28 70 53 72 63 2d 3e 70 49 6e 64  ause (pSrc->pInd
2f608 65 78 29 20 61 74 74 61 63 68 65 64 20 74 6f 20  ex) attached to 
2f609 74 68 65 20 74 61 62 6c 65 20 69 6e 0a 2a 2a 20  the table in.** 
2f60a 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
2f60b 74 2c 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e  t, then this fun
2f60c 63 74 69 6f 6e 20 6f 6e 6c 79 20 63 6f 6e 73 69  ction only consi
2f60d 64 65 72 73 20 70 6c 61 6e 73 20 75 73 69 6e 67  ders plans using
2f60e 20 74 68 65 20 0a 2a 2a 20 6e 61 6d 65 64 20 69   the .** named i
2f60f 6e 64 65 78 2e 20 49 66 20 6e 6f 20 73 75 63 68  ndex. If no such
2f610 20 70 6c 61 6e 20 69 73 20 66 6f 75 6e 64 2c 20   plan is found, 
2f611 74 68 65 6e 20 74 68 65 20 72 65 74 75 72 6e 65  then the returne
2f612 64 20 63 6f 73 74 20 69 73 0a 2a 2a 20 53 51 4c  d cost is.** SQL
2f613 49 54 45 5f 42 49 47 5f 44 42 4c 2e 20 49 66 20  ITE_BIG_DBL. If 
2f614 61 20 70 6c 61 6e 20 69 73 20 66 6f 75 6e 64 20  a plan is found 
2f615 74 68 61 74 20 75 73 65 73 20 74 68 65 20 6e 61  that uses the na
2f616 6d 65 64 20 69 6e 64 65 78 2c 20 0a 2a 2a 20 74  med index, .** t
2f617 68 65 6e 20 74 68 65 20 63 6f 73 74 20 69 73 20  hen the cost is 
2f618 63 61 6c 63 75 6c 61 74 65 64 20 69 6e 20 74 68  calculated in th
2f619 65 20 75 73 75 61 6c 20 77 61 79 2e 0a 2a 2a 0a  e usual way..**.
2f61a 2a 2a 20 49 66 20 61 20 4e 4f 54 20 49 4e 44 45  ** If a NOT INDE
2f61b 58 45 44 20 63 6c 61 75 73 65 20 28 70 53 72 63  XED clause (pSrc
2f61c 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 21 3d 30 29  ->notIndexed!=0)
2f61d 20 77 61 73 20 61 74 74 61 63 68 65 64 20 74 6f   was attached to
2f61e 20 74 68 65 20 74 61 62 6c 65 20 0a 2a 2a 20 69   the table .** i
2f61f 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  n the SELECT sta
2f620 74 65 6d 65 6e 74 2c 20 74 68 65 6e 20 6e 6f 20  tement, then no 
2f621 69 6e 64 65 78 65 73 20 61 72 65 20 63 6f 6e 73  indexes are cons
2f622 69 64 65 72 65 64 2e 20 48 6f 77 65 76 65 72 2c  idered. However,
2f623 20 74 68 65 20 0a 2a 2a 20 73 65 6c 65 63 74 65   the .** selecte
2f624 64 20 70 6c 61 6e 20 6d 61 79 20 73 74 69 6c 6c  d plan may still
2f625 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67 65 20   take advantage 
2f626 6f 66 20 74 68 65 20 74 61 62 6c 65 73 20 62 75  of the tables bu
2f627 69 6c 74 2d 69 6e 20 72 6f 77 69 64 0a 2a 2a 20  ilt-in rowid.** 
2f628 69 6e 64 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63  index..*/.static
2f629 20 76 6f 69 64 20 62 65 73 74 42 74 72 65 65 49   void bestBtreeI
2f62a 6e 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70  ndex(.  Parse *p
2f62b 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
2f62c 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69      /* The parsi
2f62d 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
2f62e 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43  WhereClause *pWC
2f62f 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ,           /* T
2f630 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
2f631 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  */.  struct SrcL
2f632 69 73 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c 20  ist_item *pSrc, 
2f633 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61   /* The FROM cla
2f634 75 73 65 20 74 65 72 6d 20 74 6f 20 73 65 61 72  use term to sear
2f635 63 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20  ch */.  Bitmask 
2f636 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20 20 20  notReady,       
2f637 20 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 63      /* Mask of c
2f638 75 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20  ursors that are 
2f639 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f  not available */
2f63a 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72  .  ExprList *pOr
2f63b 64 65 72 42 79 2c 20 20 20 20 20 20 20 20 20 2f  derBy,         /
2f63c 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63  * The ORDER BY c
2f63d 6c 61 75 73 65 20 2a 2f 0a 20 20 57 68 65 72 65  lause */.  Where
2f63e 43 6f 73 74 20 2a 70 43 6f 73 74 20 20 20 20 20  Cost *pCost     
2f63f 20 20 20 20 20 20 20 2f 2a 20 4c 6f 77 65 73 74         /* Lowest
2f640 20 63 6f 73 74 20 71 75 65 72 79 20 70 6c 61 6e   cost query plan
2f641 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 43 75   */.){.  int iCu
2f642 72 20 3d 20 70 53 72 63 2d 3e 69 43 75 72 73 6f  r = pSrc->iCurso
2f643 72 3b 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73  r;   /* The curs
2f644 6f 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  or of the table 
2f645 74 6f 20 62 65 20 61 63 63 65 73 73 65 64 20 2a  to be accessed *
2f646 2f 0a 20 20 49 6e 64 65 78 20 2a 70 50 72 6f 62  /.  Index *pProb
2f647 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
2f648 2f 2a 20 41 6e 20 69 6e 64 65 78 20 77 65 20 61  /* An index we a
2f649 72 65 20 65 76 61 6c 75 61 74 69 6e 67 20 2a 2f  re evaluating */
2f64a 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20  .  Index *pIdx; 
2f64b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2f64c 2a 20 43 6f 70 79 20 6f 66 20 70 50 72 6f 62 65  * Copy of pProbe
2f64d 2c 20 6f 72 20 7a 65 72 6f 20 66 6f 72 20 49 50  , or zero for IP
2f64e 4b 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74  K index */.  int
2f64f 20 65 71 54 65 72 6d 4d 61 73 6b 3b 20 20 20 20   eqTermMask;    
2f650 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72           /* Curr
2f651 65 6e 74 20 6d 61 73 6b 20 6f 66 20 76 61 6c 69  ent mask of vali
2f652 64 20 65 71 75 61 6c 69 74 79 20 6f 70 65 72 61  d equality opera
2f653 74 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 64  tors */.  int id
2f654 78 45 71 54 65 72 6d 4d 61 73 6b 3b 20 20 20 20  xEqTermMask;    
2f655 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6d        /* Index m
2f656 61 73 6b 20 6f 66 20 76 61 6c 69 64 20 65 71 75  ask of valid equ
2f657 61 6c 69 74 79 20 6f 70 65 72 61 74 6f 72 73 20  ality operators 
2f658 2a 2f 0a 20 20 49 6e 64 65 78 20 73 50 6b 3b 20  */.  Index sPk; 
2f659 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f65a 20 2f 2a 20 41 20 66 61 6b 65 20 69 6e 64 65 78   /* A fake index
2f65b 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 68 65 20   object for the 
2f65c 70 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 20  primary key */. 
2f65d 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 61 69   unsigned int ai
2f65e 52 6f 77 45 73 74 50 6b 5b 32 5d 3b 20 2f 2a 20  RowEstPk[2]; /* 
2f65f 54 68 65 20 61 69 52 6f 77 45 73 74 5b 5d 20 76  The aiRowEst[] v
2f660 61 6c 75 65 20 66 6f 72 20 74 68 65 20 73 50 6b  alue for the sPk
2f661 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20   index */.  int 
2f662 61 69 43 6f 6c 75 6d 6e 50 6b 20 3d 20 2d 31 3b  aiColumnPk = -1;
2f663 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 61          /* The a
2f664 43 6f 6c 75 6d 6e 5b 5d 20 76 61 6c 75 65 20 66  Column[] value f
2f665 6f 72 20 74 68 65 20 73 50 6b 20 69 6e 64 65 78  or the sPk index
2f666 20 2a 2f 0a 20 20 69 6e 74 20 77 73 46 6c 61 67   */.  int wsFlag
2f667 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20  Mask;           
2f668 20 20 2f 2a 20 41 6c 6c 6f 77 65 64 20 66 6c 61    /* Allowed fla
2f669 67 73 20 69 6e 20 70 43 6f 73 74 2d 3e 70 6c 61  gs in pCost->pla
2f66a 6e 2e 77 73 46 6c 61 67 20 2a 2f 0a 0a 20 20 2f  n.wsFlag */..  /
2f66b 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65  * Initialize the
2f66c 20 63 6f 73 74 20 74 6f 20 61 20 77 6f 72 73 74   cost to a worst
2f66d 2d 63 61 73 65 20 76 61 6c 75 65 20 2a 2f 0a 20  -case value */. 
2f66e 20 6d 65 6d 73 65 74 28 70 43 6f 73 74 2c 20 30   memset(pCost, 0
2f66f 2c 20 73 69 7a 65 6f 66 28 2a 70 43 6f 73 74 29  , sizeof(*pCost)
2f670 29 3b 0a 20 20 70 43 6f 73 74 2d 3e 72 43 6f 73  );.  pCost->rCos
2f671 74 20 3d 20 53 51 4c 49 54 45 5f 42 49 47 5f 44  t = SQLITE_BIG_D
2f672 42 4c 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  BL;..  /* If the
2f673 20 70 53 72 63 20 74 61 62 6c 65 20 69 73 20 74   pSrc table is t
2f674 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f  he right table o
2f675 66 20 61 20 4c 45 46 54 20 4a 4f 49 4e 20 74 68  f a LEFT JOIN th
2f676 65 6e 20 77 65 20 6d 61 79 20 6e 6f 74 0a 20 20  en we may not.  
2f677 2a 2a 20 75 73 65 20 61 6e 20 69 6e 64 65 78 20  ** use an index 
2f678 74 6f 20 73 61 74 69 73 66 79 20 49 53 20 4e 55  to satisfy IS NU
2f679 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 6f  LL constraints o
2f67a 6e 20 74 68 61 74 20 74 61 62 6c 65 2e 20 20 54  n that table.  T
2f67b 68 69 73 20 69 73 0a 20 20 2a 2a 20 62 65 63 61  his is.  ** beca
2f67c 75 73 65 20 63 6f 6c 75 6d 6e 73 20 6d 69 67 68  use columns migh
2f67d 74 20 65 6e 64 20 75 70 20 62 65 69 6e 67 20 4e  t end up being N
2f67e 55 4c 4c 20 69 66 20 74 68 65 20 74 61 62 6c 65  ULL if the table
2f67f 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20   does not match 
2f680 2d 0a 20 20 2a 2a 20 61 20 63 69 72 63 75 6d 73  -.  ** a circums
2f681 74 61 6e 63 65 20 77 68 69 63 68 20 74 68 65 20  tance which the 
2f682 69 6e 64 65 78 20 63 61 6e 6e 6f 74 20 68 65 6c  index cannot hel
2f683 70 20 75 73 20 64 69 73 63 6f 76 65 72 2e 20 20  p us discover.  
2f684 54 69 63 6b 65 74 20 23 32 31 37 37 2e 0a 20 20  Ticket #2177..  
2f685 2a 2f 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 6a  */.  if( pSrc->j
2f686 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4c 45 46  ointype & JT_LEF
2f687 54 20 29 7b 0a 20 20 20 20 69 64 78 45 71 54 65  T ){.    idxEqTe
2f688 72 6d 4d 61 73 6b 20 3d 20 57 4f 5f 45 51 7c 57  rmMask = WO_EQ|W
2f689 4f 5f 49 4e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  O_IN;.  }else{. 
2f68a 20 20 20 69 64 78 45 71 54 65 72 6d 4d 61 73 6b     idxEqTermMask
2f68b 20 3d 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 7c 57   = WO_EQ|WO_IN|W
2f68c 4f 5f 49 53 4e 55 4c 4c 3b 0a 20 20 7d 0a 0a 20  O_ISNULL;.  }.. 
2f68d 20 69 66 28 20 70 53 72 63 2d 3e 70 49 6e 64 65   if( pSrc->pInde
2f68e 78 20 29 7b 0a 20 20 20 20 2f 2a 20 41 6e 20 49  x ){.    /* An I
2f68f 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75 73 65  NDEXED BY clause
2f690 20 73 70 65 63 69 66 69 65 73 20 61 20 70 61 72   specifies a par
2f691 74 69 63 75 6c 61 72 20 69 6e 64 65 78 20 74 6f  ticular index to
2f692 20 75 73 65 20 2a 2f 0a 20 20 20 20 70 49 64 78   use */.    pIdx
2f693 20 3d 20 70 50 72 6f 62 65 20 3d 20 70 53 72 63   = pProbe = pSrc
2f694 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20 20 77 73  ->pIndex;.    ws
2f695 46 6c 61 67 4d 61 73 6b 20 3d 20 7e 28 57 48 45  FlagMask = ~(WHE
2f696 52 45 5f 52 4f 57 49 44 5f 45 51 7c 57 48 45 52  RE_ROWID_EQ|WHER
2f697 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29 3b 0a  E_ROWID_RANGE);.
2f698 20 20 20 20 65 71 54 65 72 6d 4d 61 73 6b 20 3d      eqTermMask =
2f699 20 69 64 78 45 71 54 65 72 6d 4d 61 73 6b 3b 0a   idxEqTermMask;.
2f69a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
2f69b 54 68 65 72 65 20 69 73 20 6e 6f 20 49 4e 44 45  There is no INDE
2f69c 58 45 44 20 42 59 20 63 6c 61 75 73 65 2e 20 20  XED BY clause.  
2f69d 43 72 65 61 74 65 20 61 20 66 61 6b 65 20 49 6e  Create a fake In
2f69e 64 65 78 20 6f 62 6a 65 63 74 20 74 6f 0a 20 20  dex object to.  
2f69f 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 20 74    ** represent t
2f6a0 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 2a  he primary key *
2f6a1 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 46 69  /.    Index *pFi
2f6a2 72 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  rst;            
2f6a3 20 20 20 20 2f 2a 20 41 6e 79 20 6f 74 68 65 72      /* Any other
2f6a4 20 69 6e 64 65 78 20 6f 6e 20 74 68 65 20 74 61   index on the ta
2f6a5 62 6c 65 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65  ble */.    memse
2f6a6 74 28 26 73 50 6b 2c 20 30 2c 20 73 69 7a 65 6f  t(&sPk, 0, sizeo
2f6a7 66 28 49 6e 64 65 78 29 29 3b 0a 20 20 20 20 73  f(Index));.    s
2f6a8 50 6b 2e 6e 43 6f 6c 75 6d 6e 20 3d 20 31 3b 0a  Pk.nColumn = 1;.
2f6a9 20 20 20 20 73 50 6b 2e 61 69 43 6f 6c 75 6d 6e      sPk.aiColumn
2f6aa 20 3d 20 26 61 69 43 6f 6c 75 6d 6e 50 6b 3b 0a   = &aiColumnPk;.
2f6ab 20 20 20 20 73 50 6b 2e 61 69 52 6f 77 45 73 74      sPk.aiRowEst
2f6ac 20 3d 20 61 69 52 6f 77 45 73 74 50 6b 3b 0a 20   = aiRowEstPk;. 
2f6ad 20 20 20 61 69 52 6f 77 45 73 74 50 6b 5b 31 5d     aiRowEstPk[1]
2f6ae 20 3d 20 31 3b 0a 20 20 20 20 73 50 6b 2e 6f 6e   = 1;.    sPk.on
2f6af 45 72 72 6f 72 20 3d 20 4f 45 5f 52 65 70 6c 61  Error = OE_Repla
2f6b0 63 65 3b 0a 20 20 20 20 73 50 6b 2e 70 54 61 62  ce;.    sPk.pTab
2f6b1 6c 65 20 3d 20 70 53 72 63 2d 3e 70 54 61 62 3b  le = pSrc->pTab;
2f6b2 0a 20 20 20 20 70 46 69 72 73 74 20 3d 20 70 53  .    pFirst = pS
2f6b3 72 63 2d 3e 70 54 61 62 2d 3e 70 49 6e 64 65 78  rc->pTab->pIndex
2f6b4 3b 0a 20 20 20 20 69 66 28 20 70 53 72 63 2d 3e  ;.    if( pSrc->
2f6b5 6e 6f 74 49 6e 64 65 78 65 64 3d 3d 30 20 29 7b  notIndexed==0 ){
2f6b6 0a 20 20 20 20 20 20 73 50 6b 2e 70 4e 65 78 74  .      sPk.pNext
2f6b7 20 3d 20 70 46 69 72 73 74 3b 0a 20 20 20 20 7d   = pFirst;.    }
2f6b8 0a 20 20 20 20 2f 2a 20 54 68 65 20 61 69 52 6f  .    /* The aiRo
2f6b9 77 45 73 74 50 6b 5b 30 5d 20 69 73 20 61 6e 20  wEstPk[0] is an 
2f6ba 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20  estimate of the 
2f6bb 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
2f6bc 72 6f 77 73 20 69 6e 20 74 68 65 0a 20 20 20 20  rows in the.    
2f6bd 2a 2a 20 74 61 62 6c 65 2e 20 20 47 65 74 20 74  ** table.  Get t
2f6be 68 69 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  his information 
2f6bf 66 72 6f 6d 20 74 68 65 20 41 4e 41 4c 59 5a 45  from the ANALYZE
2f6c0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 66 20   information if 
2f6c1 69 74 20 69 73 0a 20 20 20 20 2a 2a 20 61 76 61  it is.    ** ava
2f6c2 69 6c 61 62 6c 65 2e 20 20 49 66 20 6e 6f 74 20  ilable.  If not 
2f6c3 61 76 61 69 6c 61 62 6c 65 2c 20 61 73 73 75 6d  available, assum
2f6c4 65 20 74 68 65 20 74 61 62 6c 65 20 31 20 6d 69  e the table 1 mi
2f6c5 6c 6c 69 6f 6e 20 72 6f 77 73 20 69 6e 20 73 69  llion rows in si
2f6c6 7a 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ze..    */.    i
2f6c7 66 28 20 70 46 69 72 73 74 20 29 7b 0a 20 20 20  f( pFirst ){.   
2f6c8 20 20 20 61 73 73 65 72 74 28 20 70 46 69 72 73     assert( pFirs
2f6c9 74 2d 3e 61 69 52 6f 77 45 73 74 21 3d 30 20 29  t->aiRowEst!=0 )
2f6ca 3b 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 64 20 74  ; /* Allocated t
2f6cb 6f 67 65 74 68 65 72 20 77 69 74 68 20 70 46 69  ogether with pFi
2f6cc 72 73 74 20 2a 2f 0a 20 20 20 20 20 20 61 69 52  rst */.      aiR
2f6cd 6f 77 45 73 74 50 6b 5b 30 5d 20 3d 20 70 46 69  owEstPk[0] = pFi
2f6ce 72 73 74 2d 3e 61 69 52 6f 77 45 73 74 5b 30 5d  rst->aiRowEst[0]
2f6cf 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2f6d0 20 20 20 61 69 52 6f 77 45 73 74 50 6b 5b 30 5d     aiRowEstPk[0]
2f6d1 20 3d 20 31 30 30 30 30 30 30 3b 0a 20 20 20 20   = 1000000;.    
2f6d2 7d 0a 20 20 20 20 70 50 72 6f 62 65 20 3d 20 26  }.    pProbe = &
2f6d3 73 50 6b 3b 0a 20 20 20 20 77 73 46 6c 61 67 4d  sPk;.    wsFlagM
2f6d4 61 73 6b 20 3d 20 7e 28 0a 20 20 20 20 20 20 20  ask = ~(.       
2f6d5 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e   WHERE_COLUMN_IN
2f6d6 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51  |WHERE_COLUMN_EQ
2f6d7 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55  |WHERE_COLUMN_NU
2f6d8 4c 4c 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  LL|WHERE_COLUMN_
2f6d9 52 41 4e 47 45 0a 20 20 20 20 29 3b 0a 20 20 20  RANGE.    );.   
2f6da 20 65 71 54 65 72 6d 4d 61 73 6b 20 3d 20 57 4f   eqTermMask = WO
2f6db 5f 45 51 7c 57 4f 5f 49 4e 3b 0a 20 20 20 20 70  _EQ|WO_IN;.    p
2f6dc 49 64 78 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  Idx = 0;.  }..  
2f6dd 2f 2a 20 4c 6f 6f 70 20 6f 76 65 72 20 61 6c 6c  /* Loop over all
2f6de 20 69 6e 64 69 63 65 73 20 6c 6f 6f 6b 69 6e 67   indices looking
2f6df 20 66 6f 72 20 74 68 65 20 62 65 73 74 20 6f 6e   for the best on
2f6e0 65 20 74 6f 20 75 73 65 0a 20 20 2a 2f 0a 20 20  e to use.  */.  
2f6e1 66 6f 72 28 3b 20 70 50 72 6f 62 65 3b 20 70 49  for(; pProbe; pI
2f6e2 64 78 3d 70 50 72 6f 62 65 3d 70 50 72 6f 62 65  dx=pProbe=pProbe
2f6e3 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 63 6f  ->pNext){.    co
2f6e4 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  nst unsigned int
2f6e5 20 2a 20 63 6f 6e 73 74 20 61 69 52 6f 77 45 73   * const aiRowEs
2f6e6 74 20 3d 20 70 50 72 6f 62 65 2d 3e 61 69 52 6f  t = pProbe->aiRo
2f6e7 77 45 73 74 3b 0a 20 20 20 20 64 6f 75 62 6c 65  wEst;.    double
2f6e8 20 63 6f 73 74 3b 20 20 20 20 20 20 20 20 20 20   cost;          
2f6e9 20 20 20 20 20 20 2f 2a 20 43 6f 73 74 20 6f 66        /* Cost of
2f6ea 20 75 73 69 6e 67 20 70 50 72 6f 62 65 20 2a 2f   using pProbe */
2f6eb 0a 20 20 20 20 64 6f 75 62 6c 65 20 6e 52 6f 77  .    double nRow
2f6ec 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2f6ed 20 2f 2a 20 45 73 74 69 6d 61 74 65 64 20 6e 75   /* Estimated nu
2f6ee 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 69 6e 20  mber of rows in 
2f6ef 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20  result set */.  
2f6f0 20 20 69 6e 74 20 72 65 76 3b 20 20 20 20 20 20    int rev;      
2f6f1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2f6f2 20 54 72 75 65 20 74 6f 20 73 63 61 6e 20 69 6e   True to scan in
2f6f3 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 20 2a   reverse order *
2f6f4 2f 0a 20 20 20 20 69 6e 74 20 77 73 46 6c 61 67  /.    int wsFlag
2f6f5 73 20 3d 20 30 3b 0a 20 20 20 20 42 69 74 6d 61  s = 0;.    Bitma
2f6f6 73 6b 20 75 73 65 64 20 3d 20 30 3b 0a 0a 20 20  sk used = 0;..  
2f6f7 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
2f6f8 6e 67 20 76 61 72 69 61 62 6c 65 73 20 61 72 65  ng variables are
2f6f9 20 70 6f 70 75 6c 61 74 65 64 20 62 61 73 65 64   populated based
2f6fa 20 6f 6e 20 74 68 65 20 70 72 6f 70 65 72 74 69   on the properti
2f6fb 65 73 20 6f 66 0a 20 20 20 20 2a 2a 20 73 63 61  es of.    ** sca
2f6fc 6e 20 62 65 69 6e 67 20 65 76 61 6c 75 61 74 65  n being evaluate
2f6fd 64 2e 20 54 68 65 79 20 61 72 65 20 74 68 65 6e  d. They are then
2f6fe 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d 69   used to determi
2f6ff 6e 65 20 74 68 65 20 65 78 70 65 63 74 65 64 0a  ne the expected.
2f700 20 20 20 20 2a 2a 20 63 6f 73 74 20 61 6e 64 20      ** cost and 
2f701 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 72  number of rows r
2f702 65 74 75 72 6e 65 64 2e 0a 20 20 20 20 2a 2a 0a  eturned..    **.
2f703 20 20 20 20 2a 2a 20 20 6e 45 71 3a 20 0a 20 20      **  nEq: .  
2f704 20 20 2a 2a 20 20 20 20 4e 75 6d 62 65 72 20 6f    **    Number o
2f705 66 20 65 71 75 61 6c 69 74 79 20 74 65 72 6d 73  f equality terms
2f706 20 74 68 61 74 20 63 61 6e 20 62 65 20 69 6d 70   that can be imp
2f707 6c 65 6d 65 6e 74 65 64 20 75 73 69 6e 67 20 74  lemented using t
2f708 68 65 20 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2a  he index..    **
2f709 0a 20 20 20 20 2a 2a 20 20 6e 49 6e 4d 75 6c 3a  .    **  nInMul:
2f70a 20 20 0a 20 20 20 20 2a 2a 20 20 20 20 54 68 65    .    **    The
2f70b 20 22 69 6e 2d 6d 75 6c 74 69 70 6c 69 65 72 22   "in-multiplier"
2f70c 2e 20 54 68 69 73 20 69 73 20 61 6e 20 65 73 74  . This is an est
2f70d 69 6d 61 74 65 20 6f 66 20 68 6f 77 20 6d 61 6e  imate of how man
2f70e 79 20 73 65 65 6b 20 6f 70 65 72 61 74 69 6f 6e  y seek operation
2f70f 73 20 0a 20 20 20 20 2a 2a 20 20 20 20 53 51 4c  s .    **    SQL
2f710 69 74 65 20 6d 75 73 74 20 70 65 72 66 6f 72 6d  ite must perform
2f711 20 6f 6e 20 74 68 65 20 69 6e 64 65 78 20 69 6e   on the index in
2f712 20 71 75 65 73 74 69 6f 6e 2e 20 46 6f 72 20 65   question. For e
2f713 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 0a  xample, if the .
2f714 20 20 20 20 2a 2a 20 20 20 20 57 48 45 52 45 20      **    WHERE 
2f715 63 6c 61 75 73 65 20 69 73 3a 0a 20 20 20 20 2a  clause is:.    *
2f716 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 57 48  *.    **      WH
2f717 45 52 45 20 61 20 49 4e 20 28 31 2c 20 32 2c 20  ERE a IN (1, 2, 
2f718 33 29 20 41 4e 44 20 62 20 49 4e 20 28 34 2c 20  3) AND b IN (4, 
2f719 35 2c 20 36 29 0a 20 20 20 20 2a 2a 0a 20 20 20  5, 6).    **.   
2f71a 20 2a 2a 20 20 20 20 53 51 4c 69 74 65 20 6d 75   **    SQLite mu
2f71b 73 74 20 70 65 72 66 6f 72 6d 20 39 20 6c 6f 6f  st perform 9 loo
2f71c 6b 75 70 73 20 6f 6e 20 61 6e 20 69 6e 64 65 78  kups on an index
2f71d 20 6f 6e 20 28 61 2c 20 62 29 2c 20 73 6f 20 6e   on (a, b), so n
2f71e 49 6e 4d 75 6c 20 69 73 20 0a 20 20 20 20 2a 2a  InMul is .    **
2f71f 20 20 20 20 73 65 74 20 74 6f 20 39 2e 20 47 69      set to 9. Gi
2f720 76 65 6e 20 74 68 65 20 73 61 6d 65 20 73 63 68  ven the same sch
2f721 65 6d 61 20 61 6e 64 20 65 69 74 68 65 72 20 6f  ema and either o
2f722 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
2f723 57 48 45 52 45 20 0a 20 20 20 20 2a 2a 20 20 20  WHERE .    **   
2f724 20 63 6c 61 75 73 65 73 3a 0a 20 20 20 20 2a 2a   clauses:.    **
2f725 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 57 48 45  .    **      WHE
2f726 52 45 20 61 20 3d 20 20 31 0a 20 20 20 20 2a 2a  RE a =  1.    **
2f727 20 20 20 20 20 20 57 48 45 52 45 20 61 20 3e 3d        WHERE a >=
2f728 20 32 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a   2.    **.    **
2f729 20 20 20 20 6e 49 6e 4d 75 6c 20 69 73 20 73 65      nInMul is se
2f72a 74 20 74 6f 20 31 2e 0a 20 20 20 20 2a 2a 0a 20  t to 1..    **. 
2f72b 20 20 20 2a 2a 20 20 20 20 49 66 20 74 68 65 72     **    If ther
2f72c 65 20 65 78 69 73 74 73 20 61 20 57 48 45 52 45  e exists a WHERE
2f72d 20 74 65 72 6d 20 6f 66 20 74 68 65 20 66 6f 72   term of the for
2f72e 6d 20 22 78 20 49 4e 20 28 53 45 4c 45 43 54 20  m "x IN (SELECT 
2f72f 2e 2e 2e 29 22 2c 20 74 68 65 6e 20 0a 20 20 20  ...)", then .   
2f730 20 2a 2a 20 20 20 20 74 68 65 20 73 75 62 2d 73   **    the sub-s
2f731 65 6c 65 63 74 20 69 73 20 61 73 73 75 6d 65 64  elect is assumed
2f732 20 74 6f 20 72 65 74 75 72 6e 20 32 35 20 72 6f   to return 25 ro
2f733 77 73 20 66 6f 72 20 74 68 65 20 70 75 72 70 6f  ws for the purpo
2f734 73 65 73 20 6f 66 20 0a 20 20 20 20 2a 2a 20 20  ses of .    **  
2f735 20 20 64 65 74 65 72 6d 69 6e 69 6e 67 20 6e 49    determining nI
2f736 6e 4d 75 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  nMul..    **.   
2f737 20 2a 2a 20 20 62 49 6e 45 73 74 3a 20 20 0a 20   **  bInEst:  . 
2f738 20 20 20 2a 2a 20 20 20 20 53 65 74 20 74 6f 20     **    Set to 
2f739 74 72 75 65 20 69 66 20 74 68 65 72 65 20 77 61  true if there wa
2f73a 73 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 22  s at least one "
2f73b 78 20 49 4e 20 28 53 45 4c 45 43 54 20 2e 2e 2e  x IN (SELECT ...
2f73c 29 22 20 74 65 72 6d 20 75 73 65 64 20 0a 20 20  )" term used .  
2f73d 20 20 2a 2a 20 20 20 20 69 6e 20 64 65 74 65 72    **    in deter
2f73e 6d 69 6e 69 6e 67 20 74 68 65 20 76 61 6c 75 65  mining the value
2f73f 20 6f 66 20 6e 49 6e 4d 75 6c 2e 0a 20 20 20 20   of nInMul..    
2f740 2a 2a 0a 20 20 20 20 2a 2a 20 20 6e 42 6f 75 6e  **.    **  nBoun
2f741 64 3a 0a 20 20 20 20 2a 2a 20 20 20 20 41 6e 20  d:.    **    An 
2f742 65 73 74 69 6d 61 74 65 20 6f 6e 20 74 68 65 20  estimate on the 
2f743 61 6d 6f 75 6e 74 20 6f 66 20 74 68 65 20 74 61  amount of the ta
2f744 62 6c 65 20 74 68 61 74 20 6d 75 73 74 20 62 65  ble that must be
2f745 20 73 65 61 72 63 68 65 64 2e 20 20 41 0a 20 20   searched.  A.  
2f746 20 20 2a 2a 20 20 20 20 76 61 6c 75 65 20 6f 66    **    value of
2f747 20 31 30 30 20 6d 65 61 6e 73 20 74 68 65 20 65   100 means the e
2f748 6e 74 69 72 65 20 74 61 62 6c 65 20 69 73 20 73  ntire table is s
2f749 65 61 72 63 68 65 64 2e 20 20 52 61 6e 67 65 20  earched.  Range 
2f74a 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 20 20  constraints.    
2f74b 2a 2a 20 20 20 20 6d 69 67 68 74 20 72 65 64 75  **    might redu
2f74c 63 65 20 74 68 69 73 20 74 6f 20 61 20 76 61 6c  ce this to a val
2f74d 75 65 20 6c 65 73 73 20 74 68 61 6e 20 31 30 30  ue less than 100
2f74e 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61   to indicate tha
2f74f 74 20 6f 6e 6c 79 0a 20 20 20 20 2a 2a 20 20 20  t only.    **   
2f750 20 61 20 66 72 61 63 74 69 6f 6e 20 6f 66 20 74   a fraction of t
2f751 68 65 20 74 61 62 6c 65 20 6e 65 65 64 73 20 73  he table needs s
2f752 65 61 72 63 68 69 6e 67 2e 20 20 49 6e 20 74 68  earching.  In th
2f753 65 20 61 62 73 65 6e 63 65 20 6f 66 0a 20 20 20  e absence of.   
2f754 20 2a 2a 20 20 20 20 73 71 6c 69 74 65 5f 73 74   **    sqlite_st
2f755 61 74 32 20 41 4e 41 4c 59 5a 45 20 64 61 74 61  at2 ANALYZE data
2f756 2c 20 61 20 73 69 6e 67 6c 65 20 69 6e 65 71 75  , a single inequ
2f757 61 6c 69 74 79 20 72 65 64 75 63 65 73 20 74 68  ality reduces th
2f758 65 20 73 65 61 72 63 68 0a 20 20 20 20 2a 2a 20  e search.    ** 
2f759 20 20 20 73 70 61 63 65 20 74 6f 20 31 2f 33 72     space to 1/3r
2f75a 64 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73  d its original s
2f75b 69 7a 65 2e 20 20 53 6f 20 61 6e 20 78 3e 3f 20  ize.  So an x>? 
2f75c 63 6f 6e 73 74 72 61 69 6e 74 20 72 65 64 75 63  constraint reduc
2f75d 65 73 0a 20 20 20 20 2a 2a 20 20 20 20 6e 42 6f  es.    **    nBo
2f75e 75 6e 64 20 74 6f 20 33 33 2e 20 20 54 77 6f 20  und to 33.  Two 
2f75f 63 6f 6e 73 74 72 61 69 6e 74 73 20 28 78 3e 3f  constraints (x>?
2f760 20 41 4e 44 20 78 3c 3f 29 20 72 65 64 75 63 65   AND x<?) reduce
2f761 20 6e 42 6f 75 6e 64 20 74 6f 20 31 31 2e 0a 20   nBound to 11.. 
2f762 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 62 53     **.    **  bS
2f763 6f 72 74 3a 20 20 20 0a 20 20 20 20 2a 2a 20 20  ort:   .    **  
2f764 20 20 42 6f 6f 6c 65 61 6e 2e 20 54 72 75 65 20    Boolean. True 
2f765 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20 4f  if there is an O
2f766 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 74  RDER BY clause t
2f767 68 61 74 20 77 69 6c 6c 20 72 65 71 75 69 72 65  hat will require
2f768 20 61 6e 20 0a 20 20 20 20 2a 2a 20 20 20 20 65   an .    **    e
2f769 78 74 65 72 6e 61 6c 20 73 6f 72 74 20 28 69 2e  xternal sort (i.
2f76a 65 2e 20 73 63 61 6e 6e 69 6e 67 20 74 68 65 20  e. scanning the 
2f76b 69 6e 64 65 78 20 62 65 69 6e 67 20 65 76 61 6c  index being eval
2f76c 75 61 74 65 64 20 77 69 6c 6c 20 6e 6f 74 20 0a  uated will not .
2f76d 20 20 20 20 2a 2a 20 20 20 20 63 6f 72 72 65 63      **    correc
2f76e 74 6c 79 20 6f 72 64 65 72 20 72 65 63 6f 72 64  tly order record
2f76f 73 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  s)..    **.    *
2f770 2a 20 20 62 4c 6f 6f 6b 75 70 3a 20 0a 20 20 20  *  bLookup: .   
2f771 20 2a 2a 20 20 20 20 42 6f 6f 6c 65 61 6e 2e 20   **    Boolean. 
2f772 54 72 75 65 20 69 66 20 66 6f 72 20 65 61 63 68  True if for each
2f773 20 69 6e 64 65 78 20 65 6e 74 72 79 20 76 69 73   index entry vis
2f774 69 74 65 64 20 61 20 6c 6f 6f 6b 75 70 20 6f 6e  ited a lookup on
2f775 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 20 20 20   the .    **    
2f776 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 61  corresponding ta
2f777 62 6c 65 20 62 2d 74 72 65 65 20 69 73 20 72 65  ble b-tree is re
2f778 71 75 69 72 65 64 2e 20 54 68 69 73 20 69 73 20  quired. This is 
2f779 61 6c 77 61 79 73 20 66 61 6c 73 65 20 0a 20 20  always false .  
2f77a 20 20 2a 2a 20 20 20 20 66 6f 72 20 74 68 65 20    **    for the 
2f77b 72 6f 77 69 64 20 69 6e 64 65 78 2e 20 46 6f 72  rowid index. For
2f77c 20 6f 74 68 65 72 20 69 6e 64 65 78 65 73 2c 20   other indexes, 
2f77d 69 74 20 69 73 20 74 72 75 65 20 75 6e 6c 65 73  it is true unles
2f77e 73 20 61 6c 6c 20 74 68 65 20 0a 20 20 20 20 2a  s all the .    *
2f77f 2a 20 20 20 20 63 6f 6c 75 6d 6e 73 20 6f 66 20  *    columns of 
2f780 74 68 65 20 74 61 62 6c 65 20 75 73 65 64 20 62  the table used b
2f781 79 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  y the SELECT sta
2f782 74 65 6d 65 6e 74 20 61 72 65 20 70 72 65 73 65  tement are prese
2f783 6e 74 20 69 6e 20 0a 20 20 20 20 2a 2a 20 20 20  nt in .    **   
2f784 20 74 68 65 20 69 6e 64 65 78 20 28 73 75 63 68   the index (such
2f785 20 61 6e 20 69 6e 64 65 78 20 69 73 20 73 6f 6d   an index is som
2f786 65 74 69 6d 65 73 20 64 65 73 63 72 69 62 65 64  etimes described
2f787 20 61 73 20 61 20 63 6f 76 65 72 69 6e 67 20 69   as a covering i
2f788 6e 64 65 78 29 2e 0a 20 20 20 20 2a 2a 20 20 20  ndex)..    **   
2f789 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 67 69   For example, gi
2f78a 76 65 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 6e  ven the index on
2f78b 20 28 61 2c 20 62 29 2c 20 74 68 65 20 73 65 63   (a, b), the sec
2f78c 6f 6e 64 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  ond of the follo
2f78d 77 69 6e 67 20 0a 20 20 20 20 2a 2a 20 20 20 20  wing .    **    
2f78e 74 77 6f 20 71 75 65 72 69 65 73 20 72 65 71 75  two queries requ
2f78f 69 72 65 73 20 74 61 62 6c 65 20 62 2d 74 72 65  ires table b-tre
2f790 65 20 6c 6f 6f 6b 75 70 73 2c 20 62 75 74 20 74  e lookups, but t
2f791 68 65 20 66 69 72 73 74 20 64 6f 65 73 20 6e 6f  he first does no
2f792 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  t..    **.    **
2f793 20 20 20 20 20 20 20 20 20 20 20 20 20 53 45 4c               SEL
2f794 45 43 54 20 61 2c 20 62 20 20 20 20 46 52 4f 4d  ECT a, b    FROM
2f795 20 74 62 6c 20 57 48 45 52 45 20 61 20 3d 20 31   tbl WHERE a = 1
2f796 3b 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  ;.    **        
2f797 20 20 20 20 20 53 45 4c 45 43 54 20 61 2c 20 62       SELECT a, b
2f798 2c 20 63 20 46 52 4f 4d 20 74 62 6c 20 57 48 45  , c FROM tbl WHE
2f799 52 45 20 61 20 3d 20 31 3b 0a 20 20 20 20 2a 2f  RE a = 1;.    */
2f79a 0a 20 20 20 20 69 6e 74 20 6e 45 71 3b 0a 20 20  .    int nEq;.  
2f79b 20 20 69 6e 74 20 62 49 6e 45 73 74 20 3d 20 30    int bInEst = 0
2f79c 3b 0a 20 20 20 20 69 6e 74 20 6e 49 6e 4d 75 6c  ;.    int nInMul
2f79d 20 3d 20 31 3b 0a 20 20 20 20 69 6e 74 20 6e 42   = 1;.    int nB
2f79e 6f 75 6e 64 20 3d 20 31 30 30 3b 0a 20 20 20 20  ound = 100;.    
2f79f 69 6e 74 20 62 53 6f 72 74 20 3d 20 30 3b 0a 20  int bSort = 0;. 
2f7a0 20 20 20 69 6e 74 20 62 4c 6f 6f 6b 75 70 20 3d     int bLookup =
2f7a1 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 74 65   0;..    /* Dete
2f7a2 72 6d 69 6e 65 20 74 68 65 20 76 61 6c 75 65 73  rmine the values
2f7a3 20 6f 66 20 6e 45 71 20 61 6e 64 20 6e 49 6e 4d   of nEq and nInM
2f7a4 75 6c 20 2a 2f 0a 20 20 20 20 66 6f 72 28 6e 45  ul */.    for(nE
2f7a5 71 3d 30 3b 20 6e 45 71 3c 70 50 72 6f 62 65 2d  q=0; nEq<pProbe-
2f7a6 3e 6e 43 6f 6c 75 6d 6e 3b 20 6e 45 71 2b 2b 29  >nColumn; nEq++)
2f7a7 7b 0a 20 20 20 20 20 20 57 68 65 72 65 54 65 72  {.      WhereTer
2f7a8 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20  m *pTerm;       
2f7a9 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20      /* A single 
2f7aa 74 65 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52  term of the WHER
2f7ab 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20  E clause */.    
2f7ac 20 20 69 6e 74 20 6a 20 3d 20 70 50 72 6f 62 65    int j = pProbe
2f7ad 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45 71 5d 3b  ->aiColumn[nEq];
2f7ae 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20 66  .      pTerm = f
2f7af 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75  indTerm(pWC, iCu
2f7b0 72 2c 20 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20  r, j, notReady, 
2f7b1 65 71 54 65 72 6d 4d 61 73 6b 2c 20 70 49 64 78  eqTermMask, pIdx
2f7b2 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 65  );.      if( pTe
2f7b3 72 6d 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20  rm==0 ) break;. 
2f7b4 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20       wsFlags |= 
2f7b5 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51  (WHERE_COLUMN_EQ
2f7b6 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 29  |WHERE_ROWID_EQ)
2f7b7 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 65 72  ;.      if( pTer
2f7b8 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57  m->eOperator & W
2f7b9 4f 5f 49 4e 20 29 7b 0a 20 20 20 20 20 20 20 20  O_IN ){.        
2f7ba 45 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 54  Expr *pExpr = pT
2f7bb 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20  erm->pExpr;.    
2f7bc 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d 20 57      wsFlags |= W
2f7bd 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 3b 0a  HERE_COLUMN_IN;.
2f7be 20 20 20 20 20 20 20 20 69 66 28 20 45 78 70 72          if( Expr
2f7bf 48 61 73 50 72 6f 70 65 72 74 79 28 70 45 78 70  HasProperty(pExp
2f7c0 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63 74 29  r, EP_xIsSelect)
2f7c1 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 49   ){.          nI
2f7c2 6e 4d 75 6c 20 2a 3d 20 32 35 3b 0a 20 20 20 20  nMul *= 25;.    
2f7c3 20 20 20 20 20 20 62 49 6e 45 73 74 20 3d 20 31        bInEst = 1
2f7c4 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  ;.        }else 
2f7c5 69 66 28 20 70 45 78 70 72 2d 3e 78 2e 70 4c 69  if( pExpr->x.pLi
2f7c6 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  st ){.          
2f7c7 6e 49 6e 4d 75 6c 20 2a 3d 20 70 45 78 70 72 2d  nInMul *= pExpr-
2f7c8 3e 78 2e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20  >x.pList->nExpr 
2f7c9 2b 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  + 1;.        }. 
2f7ca 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
2f7cb 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
2f7cc 26 20 57 4f 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20  & WO_ISNULL ){. 
2f7cd 20 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c         wsFlags |
2f7ce 3d 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e  = WHERE_COLUMN_N
2f7cf 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ULL;.      }.   
2f7d0 20 20 20 75 73 65 64 20 7c 3d 20 70 54 65 72 6d     used |= pTerm
2f7d1 2d 3e 70 72 65 72 65 71 52 69 67 68 74 3b 0a 20  ->prereqRight;. 
2f7d2 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 74     }..    /* Det
2f7d3 65 72 6d 69 6e 65 20 74 68 65 20 76 61 6c 75 65  ermine the value
2f7d4 20 6f 66 20 6e 42 6f 75 6e 64 2e 20 2a 2f 0a 20   of nBound. */. 
2f7d5 20 20 20 69 66 28 20 6e 45 71 3c 70 50 72 6f 62     if( nEq<pProb
2f7d6 65 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20  e->nColumn ){.  
2f7d7 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 50 72 6f      int j = pPro
2f7d8 62 65 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45 71  be->aiColumn[nEq
2f7d9 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 66 69 6e  ];.      if( fin
2f7da 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72 2c  dTerm(pWC, iCur,
2f7db 20 6a 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f   j, notReady, WO
2f7dc 5f 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c  _LT|WO_LE|WO_GT|
2f7dd 57 4f 5f 47 45 2c 20 70 49 64 78 29 20 29 7b 0a  WO_GE, pIdx) ){.
2f7de 20 20 20 20 20 20 20 20 57 68 65 72 65 54 65 72          WhereTer
2f7df 6d 20 2a 70 54 6f 70 20 3d 20 66 69 6e 64 54 65  m *pTop = findTe
2f7e0 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c  rm(pWC, iCur, j,
2f7e1 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 4c 54   notReady, WO_LT
2f7e2 7c 57 4f 5f 4c 45 2c 20 70 49 64 78 29 3b 0a 20  |WO_LE, pIdx);. 
2f7e3 20 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d         WhereTerm
2f7e4 20 2a 70 42 74 6d 20 3d 20 66 69 6e 64 54 65 72   *pBtm = findTer
2f7e5 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20  m(pWC, iCur, j, 
2f7e6 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 47 54 7c  notReady, WO_GT|
2f7e7 57 4f 5f 47 45 2c 20 70 49 64 78 29 3b 0a 20 20  WO_GE, pIdx);.  
2f7e8 20 20 20 20 20 20 77 68 65 72 65 52 61 6e 67 65        whereRange
2f7e9 53 63 61 6e 45 73 74 28 70 50 61 72 73 65 2c 20  ScanEst(pParse, 
2f7ea 70 50 72 6f 62 65 2c 20 6e 45 71 2c 20 70 42 74  pProbe, nEq, pBt
2f7eb 6d 2c 20 70 54 6f 70 2c 20 26 6e 42 6f 75 6e 64  m, pTop, &nBound
2f7ec 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  );.        if( p
2f7ed 54 6f 70 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Top ){.         
2f7ee 20 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52   wsFlags |= WHER
2f7ef 45 5f 54 4f 50 5f 4c 49 4d 49 54 3b 0a 20 20 20  E_TOP_LIMIT;.   
2f7f0 20 20 20 20 20 20 20 75 73 65 64 20 7c 3d 20 70         used |= p
2f7f1 54 6f 70 2d 3e 70 72 65 72 65 71 52 69 67 68 74  Top->prereqRight
2f7f2 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2f7f3 20 20 20 20 69 66 28 20 70 42 74 6d 20 29 7b 0a      if( pBtm ){.
2f7f4 20 20 20 20 20 20 20 20 20 20 77 73 46 6c 61 67            wsFlag
2f7f5 73 20 7c 3d 20 57 48 45 52 45 5f 42 54 4d 5f 4c  s |= WHERE_BTM_L
2f7f6 49 4d 49 54 3b 0a 20 20 20 20 20 20 20 20 20 20  IMIT;.          
2f7f7 75 73 65 64 20 7c 3d 20 70 42 74 6d 2d 3e 70 72  used |= pBtm->pr
2f7f8 65 72 65 71 52 69 67 68 74 3b 0a 20 20 20 20 20  ereqRight;.     
2f7f9 20 20 20 7d 0a 20 20 20 20 20 20 20 20 77 73 46     }.        wsF
2f7fa 6c 61 67 73 20 7c 3d 20 28 57 48 45 52 45 5f 43  lags |= (WHERE_C
2f7fb 4f 4c 55 4d 4e 5f 52 41 4e 47 45 7c 57 48 45 52  OLUMN_RANGE|WHER
2f7fc 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29 3b 0a  E_ROWID_RANGE);.
2f7fd 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
2f7fe 65 20 69 66 28 20 70 50 72 6f 62 65 2d 3e 6f 6e  e if( pProbe->on
2f7ff 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29  Error!=OE_None )
2f800 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
2f801 28 20 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  ( wsFlags & WHER
2f802 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 20 29 3b 0a 20  E_COLUMN_IN );. 
2f803 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 77       testcase( w
2f804 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 43  sFlags & WHERE_C
2f805 4f 4c 55 4d 4e 5f 4e 55 4c 4c 20 29 3b 0a 20 20  OLUMN_NULL );.  
2f806 20 20 20 20 69 66 28 20 28 77 73 46 6c 61 67 73      if( (wsFlags
2f807 20 26 20 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e   & (WHERE_COLUMN
2f808 5f 49 4e 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  _IN|WHERE_COLUMN
2f809 5f 4e 55 4c 4c 29 29 3d 3d 30 20 29 7b 0a 20 20  _NULL))==0 ){.  
2f80a 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d        wsFlags |=
2f80b 20 57 48 45 52 45 5f 55 4e 49 51 55 45 3b 0a 20   WHERE_UNIQUE;. 
2f80c 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
2f80d 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
2f80e 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61   an ORDER BY cla
2f80f 75 73 65 20 61 6e 64 20 74 68 65 20 69 6e 64 65  use and the inde
2f810 78 20 62 65 69 6e 67 20 63 6f 6e 73 69 64 65 72  x being consider
2f811 65 64 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 6e  ed will.    ** n
2f812 61 74 75 72 61 6c 6c 79 20 73 63 61 6e 20 72 6f  aturally scan ro
2f813 77 73 20 69 6e 20 74 68 65 20 72 65 71 75 69 72  ws in the requir
2f814 65 64 20 6f 72 64 65 72 2c 20 73 65 74 20 74 68  ed order, set th
2f815 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6c  e appropriate fl
2f816 61 67 73 0a 20 20 20 20 2a 2a 20 69 6e 20 77 73  ags.    ** in ws
2f817 46 6c 61 67 73 2e 20 4f 74 68 65 72 77 69 73 65  Flags. Otherwise
2f818 2c 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e  , if there is an
2f819 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
2f81a 20 62 75 74 20 74 68 65 20 69 6e 64 65 78 0a 20   but the index. 
2f81b 20 20 20 2a 2a 20 77 69 6c 6c 20 73 63 61 6e 20     ** will scan 
2f81c 72 6f 77 73 20 69 6e 20 61 20 64 69 66 66 65 72  rows in a differ
2f81d 65 6e 74 20 6f 72 64 65 72 2c 20 73 65 74 20 74  ent order, set t
2f81e 68 65 20 62 53 6f 72 74 20 76 61 72 69 61 62 6c  he bSort variabl
2f81f 65 2e 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  e.  */.    if( p
2f820 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20  OrderBy ){.     
2f821 20 69 66 28 20 28 77 73 46 6c 61 67 73 20 26 20   if( (wsFlags & 
2f822 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49 4e  (WHERE_COLUMN_IN
2f823 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 4e 55  |WHERE_COLUMN_NU
2f824 4c 4c 29 29 3d 3d 30 0a 20 20 20 20 20 20 20 20  LL))==0.        
2f825 26 26 20 69 73 53 6f 72 74 69 6e 67 49 6e 64 65  && isSortingInde
2f826 78 28 70 50 61 72 73 65 2c 70 57 43 2d 3e 70 4d  x(pParse,pWC->pM
2f827 61 73 6b 53 65 74 2c 70 50 72 6f 62 65 2c 69 43  askSet,pProbe,iC
2f828 75 72 2c 70 4f 72 64 65 72 42 79 2c 6e 45 71 2c  ur,pOrderBy,nEq,
2f829 26 72 65 76 29 0a 20 20 20 20 20 20 29 7b 0a 20  &rev).      ){. 
2f82a 20 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c         wsFlags |
2f82b 3d 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41  = WHERE_ROWID_RA
2f82c 4e 47 45 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e  NGE|WHERE_COLUMN
2f82d 5f 52 41 4e 47 45 7c 57 48 45 52 45 5f 4f 52 44  _RANGE|WHERE_ORD
2f82e 45 52 42 59 3b 0a 20 20 20 20 20 20 20 20 77 73  ERBY;.        ws
2f82f 46 6c 61 67 73 20 7c 3d 20 28 72 65 76 20 3f 20  Flags |= (rev ? 
2f830 57 48 45 52 45 5f 52 45 56 45 52 53 45 20 3a 20  WHERE_REVERSE : 
2f831 30 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  0);.      }else{
2f832 0a 20 20 20 20 20 20 20 20 62 53 6f 72 74 20 3d  .        bSort =
2f833 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
2f834 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 63 75 72  }..    /* If cur
2f835 72 65 6e 74 6c 79 20 63 61 6c 63 75 6c 61 74 69  rently calculati
2f836 6e 67 20 74 68 65 20 63 6f 73 74 20 6f 66 20 75  ng the cost of u
2f837 73 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 28 6e  sing an index (n
2f838 6f 74 20 74 68 65 20 49 50 4b 0a 20 20 20 20 2a  ot the IPK.    *
2f839 2a 20 69 6e 64 65 78 29 2c 20 64 65 74 65 72 6d  * index), determ
2f83a 69 6e 65 20 69 66 20 61 6c 6c 20 72 65 71 75 69  ine if all requi
2f83b 72 65 64 20 63 6f 6c 75 6d 6e 20 64 61 74 61 20  red column data 
2f83c 6d 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64 20  may be obtained 
2f83d 77 69 74 68 6f 75 74 20 0a 20 20 20 20 2a 2a 20  without .    ** 
2f83e 73 65 65 6b 69 6e 67 20 74 6f 20 65 6e 74 72 69  seeking to entri
2f83f 65 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 74  es in the main t
2f840 61 62 6c 65 20 28 69 2e 65 2e 20 69 66 20 74 68  able (i.e. if th
2f841 65 20 69 6e 64 65 78 20 69 73 20 61 20 63 6f 76  e index is a cov
2f842 65 72 69 6e 67 0a 20 20 20 20 2a 2a 20 69 6e 64  ering.    ** ind
2f843 65 78 20 66 6f 72 20 74 68 69 73 20 71 75 65 72  ex for this quer
2f844 79 29 2e 20 49 66 20 69 74 20 69 73 2c 20 73 65  y). If it is, se
2f845 74 20 74 68 65 20 57 48 45 52 45 5f 49 44 58 5f  t the WHERE_IDX_
2f846 4f 4e 4c 59 20 66 6c 61 67 20 69 6e 0a 20 20 20  ONLY flag in.   
2f847 20 2a 2a 20 77 73 46 6c 61 67 73 2e 20 4f 74 68   ** wsFlags. Oth
2f848 65 72 77 69 73 65 2c 20 73 65 74 20 74 68 65 20  erwise, set the 
2f849 62 4c 6f 6f 6b 75 70 20 76 61 72 69 61 62 6c 65  bLookup variable
2f84a 20 74 6f 20 74 72 75 65 2e 20 20 2a 2f 0a 20 20   to true.  */.  
2f84b 20 20 69 66 28 20 70 49 64 78 20 26 26 20 77 73    if( pIdx && ws
2f84c 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 20 20 42  Flags ){.      B
2f84d 69 74 6d 61 73 6b 20 6d 20 3d 20 70 53 72 63 2d  itmask m = pSrc-
2f84e 3e 63 6f 6c 55 73 65 64 3b 0a 20 20 20 20 20 20  >colUsed;.      
2f84f 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72  int j;.      for
2f850 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43  (j=0; j<pIdx->nC
2f851 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20  olumn; j++){.   
2f852 20 20 20 20 20 69 6e 74 20 78 20 3d 20 70 49 64       int x = pId
2f853 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a  x->aiColumn[j];.
2f854 20 20 20 20 20 20 20 20 69 66 28 20 78 3c 42 4d          if( x<BM
2f855 53 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20  S-1 ){.         
2f856 20 6d 20 26 3d 20 7e 28 28 28 42 69 74 6d 61 73   m &= ~(((Bitmas
2f857 6b 29 31 29 3c 3c 78 29 3b 0a 20 20 20 20 20 20  k)1)<<x);.      
2f858 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
2f859 20 20 69 66 28 20 6d 3d 3d 30 20 29 7b 0a 20 20    if( m==0 ){.  
2f85a 20 20 20 20 20 20 77 73 46 6c 61 67 73 20 7c 3d        wsFlags |=
2f85b 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 3b   WHERE_IDX_ONLY;
2f85c 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2f85d 20 20 20 20 20 20 62 4c 6f 6f 6b 75 70 20 3d 20        bLookup = 
2f85e 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
2f85f 0a 0a 20 20 20 20 2f 2a 2a 2a 2a 20 42 65 67 69  ..    /**** Begi
2f860 6e 20 61 64 64 69 6e 67 20 75 70 20 74 68 65 20  n adding up the 
2f861 63 6f 73 74 20 6f 66 20 75 73 69 6e 67 20 74 68  cost of using th
2f862 69 73 20 69 6e 64 65 78 20 28 4e 65 65 64 73 20  is index (Needs 
2f863 69 6d 70 72 6f 76 65 6d 65 6e 74 73 29 0a 20 20  improvements).  
2f864 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 73 74 69    **.    ** Esti
2f865 6d 61 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20  mate the number 
2f866 6f 66 20 72 6f 77 73 20 6f 66 20 6f 75 74 70 75  of rows of outpu
2f867 74 2e 20 20 46 6f 72 20 61 6e 20 49 4e 20 6f 70  t.  For an IN op
2f868 65 72 61 74 6f 72 2c 0a 20 20 20 20 2a 2a 20 64  erator,.    ** d
2f869 6f 20 6e 6f 74 20 6c 65 74 20 74 68 65 20 65 73  o not let the es
2f86a 74 69 6d 61 74 65 20 65 78 63 65 65 64 20 68 61  timate exceed ha
2f86b 6c 66 20 74 68 65 20 72 6f 77 73 20 69 6e 20 74  lf the rows in t
2f86c 68 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2f  he table..    */
2f86d 0a 20 20 20 20 6e 52 6f 77 20 3d 20 28 64 6f 75  .    nRow = (dou
2f86e 62 6c 65 29 28 61 69 52 6f 77 45 73 74 5b 6e 45  ble)(aiRowEst[nE
2f86f 71 5d 20 2a 20 6e 49 6e 4d 75 6c 29 3b 0a 20 20  q] * nInMul);.  
2f870 20 20 69 66 28 20 62 49 6e 45 73 74 20 26 26 20    if( bInEst && 
2f871 6e 52 6f 77 2a 32 3e 61 69 52 6f 77 45 73 74 5b  nRow*2>aiRowEst[
2f872 30 5d 20 29 7b 0a 20 20 20 20 20 20 6e 52 6f 77  0] ){.      nRow
2f873 20 3d 20 61 69 52 6f 77 45 73 74 5b 30 5d 2f 32   = aiRowEst[0]/2
2f874 3b 0a 20 20 20 20 20 20 6e 49 6e 4d 75 6c 20 3d  ;.      nInMul =
2f875 20 28 69 6e 74 29 28 6e 52 6f 77 20 2f 20 61 69   (int)(nRow / ai
2f876 52 6f 77 45 73 74 5b 6e 45 71 5d 29 3b 0a 20 20  RowEst[nEq]);.  
2f877 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 73 73 75    }..    /* Assu
2f878 6d 65 20 63 6f 6e 73 74 61 6e 74 20 63 6f 73 74  me constant cost
2f879 20 74 6f 20 61 63 63 65 73 73 20 61 20 72 6f 77   to access a row
2f87a 20 61 6e 64 20 6c 6f 67 61 72 69 74 68 6d 69 63   and logarithmic
2f87b 20 63 6f 73 74 20 74 6f 0a 20 20 20 20 2a 2a 20   cost to.    ** 
2f87c 64 6f 20 61 20 62 69 6e 61 72 79 20 73 65 61 72  do a binary sear
2f87d 63 68 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20  ch.  Hence, the 
2f87e 69 6e 69 74 69 61 6c 20 63 6f 73 74 20 69 73 20  initial cost is 
2f87f 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6f 75  the number of ou
2f880 74 70 75 74 0a 20 20 20 20 2a 2a 20 72 6f 77 73  tput.    ** rows
2f881 20 70 6c 75 73 20 6c 6f 67 32 28 74 61 62 6c 65   plus log2(table
2f882 2d 73 69 7a 65 29 20 74 69 6d 65 73 20 74 68 65  -size) times the
2f883 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 6e 61 72   number of binar
2f884 79 20 73 65 61 72 63 68 65 73 2e 0a 20 20 20 20  y searches..    
2f885 2a 2f 0a 20 20 20 20 63 6f 73 74 20 3d 20 6e 52  */.    cost = nR
2f886 6f 77 20 2b 20 6e 49 6e 4d 75 6c 2a 65 73 74 4c  ow + nInMul*estL
2f887 6f 67 28 61 69 52 6f 77 45 73 74 5b 30 5d 29 3b  og(aiRowEst[0]);
2f888 0a 0a 20 20 20 20 2f 2a 20 41 64 6a 75 73 74 20  ..    /* Adjust 
2f889 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  the number of ro
2f88a 77 73 20 61 6e 64 20 74 68 65 20 63 6f 73 74 20  ws and the cost 
2f88b 64 6f 77 6e 77 61 72 64 20 74 6f 20 72 65 66 6c  downward to refl
2f88c 65 63 74 20 72 6f 77 73 0a 20 20 20 20 2a 2a 20  ect rows.    ** 
2f88d 74 68 61 74 20 61 72 65 20 65 78 63 6c 75 64 65  that are exclude
2f88e 64 20 62 79 20 72 61 6e 67 65 20 63 6f 6e 73 74  d by range const
2f88f 72 61 69 6e 74 73 2e 0a 20 20 20 20 2a 2f 0a 20  raints..    */. 
2f890 20 20 20 6e 52 6f 77 20 3d 20 28 6e 52 6f 77 20     nRow = (nRow 
2f891 2a 20 28 64 6f 75 62 6c 65 29 6e 42 6f 75 6e 64  * (double)nBound
2f892 29 20 2f 20 28 64 6f 75 62 6c 65 29 31 30 30 3b  ) / (double)100;
2f893 0a 20 20 20 20 63 6f 73 74 20 3d 20 28 63 6f 73  .    cost = (cos
2f894 74 20 2a 20 28 64 6f 75 62 6c 65 29 6e 42 6f 75  t * (double)nBou
2f895 6e 64 29 20 2f 20 28 64 6f 75 62 6c 65 29 31 30  nd) / (double)10
2f896 30 3b 0a 0a 20 20 20 20 2f 2a 20 41 64 64 20 69  0;..    /* Add i
2f897 6e 20 74 68 65 20 65 73 74 69 6d 61 74 65 64 20  n the estimated 
2f898 63 6f 73 74 20 6f 66 20 73 6f 72 74 69 6e 67 20  cost of sorting 
2f899 74 68 65 20 72 65 73 75 6c 74 0a 20 20 20 20 2a  the result.    *
2f89a 2f 0a 20 20 20 20 69 66 28 20 62 53 6f 72 74 20  /.    if( bSort 
2f89b 29 7b 0a 20 20 20 20 20 20 63 6f 73 74 20 2b 3d  ){.      cost +=
2f89c 20 63 6f 73 74 2a 65 73 74 4c 6f 67 28 63 6f 73   cost*estLog(cos
2f89d 74 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  t);.    }..    /
2f89e 2a 20 49 66 20 61 6c 6c 20 69 6e 66 6f 72 6d 61  * If all informa
2f89f 74 69 6f 6e 20 63 61 6e 20 62 65 20 74 61 6b 65  tion can be take
2f8a0 6e 20 64 69 72 65 63 74 6c 79 20 66 72 6f 6d 20  n directly from 
2f8a1 74 68 65 20 69 6e 64 65 78 2c 20 77 65 20 61 76  the index, we av
2f8a2 6f 69 64 0a 20 20 20 20 2a 2a 20 64 6f 69 6e 67  oid.    ** doing
2f8a3 20 74 61 62 6c 65 20 6c 6f 6f 6b 75 70 73 2e 20   table lookups. 
2f8a4 20 54 68 69 73 20 72 65 64 75 63 65 73 20 74 68   This reduces th
2f8a5 65 20 63 6f 73 74 20 62 79 20 68 61 6c 66 2e 20  e cost by half. 
2f8a6 20 28 4e 6f 74 20 72 65 61 6c 6c 79 20 2d 0a 20   (Not really -. 
2f8a7 20 20 20 2a 2a 20 74 68 69 73 20 6e 65 65 64 73     ** this needs
2f8a8 20 74 6f 20 62 65 20 66 69 78 65 64 2e 29 0a 20   to be fixed.). 
2f8a9 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 49     */.    if( pI
2f8aa 64 78 20 26 26 20 62 4c 6f 6f 6b 75 70 3d 3d 30  dx && bLookup==0
2f8ab 20 29 7b 0a 20 20 20 20 20 20 63 6f 73 74 20 2f   ){.      cost /
2f8ac 3d 20 28 64 6f 75 62 6c 65 29 32 3b 0a 20 20 20  = (double)2;.   
2f8ad 20 7d 0a 20 20 20 20 2f 2a 2a 2a 2a 20 43 6f 73   }.    /**** Cos
2f8ae 74 20 6f 66 20 75 73 69 6e 67 20 74 68 69 73 20  t of using this 
2f8af 69 6e 64 65 78 20 68 61 73 20 6e 6f 77 20 62 65  index has now be
2f8b0 65 6e 20 63 6f 6d 70 75 74 65 64 20 2a 2a 2a 2a  en computed ****
2f8b1 2f 0a 0a 20 20 20 20 57 48 45 52 45 54 52 41 43  /..    WHERETRAC
2f8b2 45 28 28 0a 20 20 20 20 20 20 22 74 62 6c 3d 25  E((.      "tbl=%
2f8b3 73 20 69 64 78 3d 25 73 20 6e 45 71 3d 25 64 20  s idx=%s nEq=%d 
2f8b4 6e 49 6e 4d 75 6c 3d 25 64 20 6e 42 6f 75 6e 64  nInMul=%d nBound
2f8b5 3d 25 64 20 62 53 6f 72 74 3d 25 64 20 62 4c 6f  =%d bSort=%d bLo
2f8b6 6f 6b 75 70 3d 25 64 22 0a 20 20 20 20 20 20 22  okup=%d".      "
2f8b7 20 77 73 46 6c 61 67 73 3d 25 64 20 20 20 28 6e   wsFlags=%d   (n
2f8b8 52 6f 77 3d 25 2e 32 66 20 63 6f 73 74 3d 25 2e  Row=%.2f cost=%.
2f8b9 32 66 29 5c 6e 22 2c 0a 20 20 20 20 20 20 70 53  2f)\n",.      pS
2f8ba 72 63 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  rc->pTab->zName,
2f8bb 20 28 70 49 64 78 20 3f 20 70 49 64 78 2d 3e 7a   (pIdx ? pIdx->z
2f8bc 4e 61 6d 65 20 3a 20 22 69 70 6b 22 29 2c 20 0a  Name : "ipk"), .
2f8bd 20 20 20 20 20 20 6e 45 71 2c 20 6e 49 6e 4d 75        nEq, nInMu
2f8be 6c 2c 20 6e 42 6f 75 6e 64 2c 20 62 53 6f 72 74  l, nBound, bSort
2f8bf 2c 20 62 4c 6f 6f 6b 75 70 2c 20 77 73 46 6c 61  , bLookup, wsFla
2f8c0 67 73 2c 20 6e 52 6f 77 2c 20 63 6f 73 74 0a 20  gs, nRow, cost. 
2f8c1 20 20 20 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 49     ));..    /* I
2f8c2 66 20 74 68 69 73 20 69 6e 64 65 78 20 69 73 20  f this index is 
2f8c3 74 68 65 20 62 65 73 74 20 77 65 20 68 61 76 65  the best we have
2f8c4 20 73 65 65 6e 20 73 6f 20 66 61 72 2c 20 74 68   seen so far, th
2f8c5 65 6e 20 72 65 63 6f 72 64 20 74 68 69 73 0a 20  en record this. 
2f8c6 20 20 20 2a 2a 20 69 6e 64 65 78 20 61 6e 64 20     ** index and 
2f8c7 69 74 73 20 63 6f 73 74 20 69 6e 20 74 68 65 20  its cost in the 
2f8c8 70 43 6f 73 74 20 73 74 72 75 63 74 75 72 65 2e  pCost structure.
2f8c9 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
2f8ca 28 21 70 49 64 78 20 7c 7c 20 77 73 46 6c 61 67  (!pIdx || wsFlag
2f8cb 73 29 20 26 26 20 63 6f 73 74 3c 70 43 6f 73 74  s) && cost<pCost
2f8cc 2d 3e 72 43 6f 73 74 20 29 7b 0a 20 20 20 20 20  ->rCost ){.     
2f8cd 20 70 43 6f 73 74 2d 3e 72 43 6f 73 74 20 3d 20   pCost->rCost = 
2f8ce 63 6f 73 74 3b 0a 20 20 20 20 20 20 70 43 6f 73  cost;.      pCos
2f8cf 74 2d 3e 6e 52 6f 77 20 3d 20 6e 52 6f 77 3b 0a  t->nRow = nRow;.
2f8d0 20 20 20 20 20 20 70 43 6f 73 74 2d 3e 75 73 65        pCost->use
2f8d1 64 20 3d 20 75 73 65 64 3b 0a 20 20 20 20 20 20  d = used;.      
2f8d2 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c  pCost->plan.wsFl
2f8d3 61 67 73 20 3d 20 28 77 73 46 6c 61 67 73 26 77  ags = (wsFlags&w
2f8d4 73 46 6c 61 67 4d 61 73 6b 29 3b 0a 20 20 20 20  sFlagMask);.    
2f8d5 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 6e 45    pCost->plan.nE
2f8d6 71 20 3d 20 6e 45 71 3b 0a 20 20 20 20 20 20 70  q = nEq;.      p
2f8d7 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64  Cost->plan.u.pId
2f8d8 78 20 3d 20 70 49 64 78 3b 0a 20 20 20 20 7d 0a  x = pIdx;.    }.
2f8d9 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65  .    /* If there
2f8da 20 77 61 73 20 61 6e 20 49 4e 44 45 58 45 44 20   was an INDEXED 
2f8db 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65 6e 20  BY clause, then 
2f8dc 6f 6e 6c 79 20 74 68 61 74 20 6f 6e 65 20 69 6e  only that one in
2f8dd 64 65 78 20 69 73 0a 20 20 20 20 2a 2a 20 63 6f  dex is.    ** co
2f8de 6e 73 69 64 65 72 65 64 2e 20 2a 2f 0a 20 20 20  nsidered. */.   
2f8df 20 69 66 28 20 70 53 72 63 2d 3e 70 49 6e 64 65   if( pSrc->pInde
2f8e0 78 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20 20 20  x ) break;..    
2f8e1 2f 2a 20 52 65 73 65 74 20 6d 61 73 6b 73 20 66  /* Reset masks f
2f8e2 6f 72 20 74 68 65 20 6e 65 78 74 20 69 6e 64 65  or the next inde
2f8e3 78 20 69 6e 20 74 68 65 20 6c 6f 6f 70 20 2a 2f  x in the loop */
2f8e4 0a 20 20 20 20 77 73 46 6c 61 67 4d 61 73 6b 20  .    wsFlagMask 
2f8e5 3d 20 7e 28 57 48 45 52 45 5f 52 4f 57 49 44 5f  = ~(WHERE_ROWID_
2f8e6 45 51 7c 57 48 45 52 45 5f 52 4f 57 49 44 5f 52  EQ|WHERE_ROWID_R
2f8e7 41 4e 47 45 29 3b 0a 20 20 20 20 65 71 54 65 72  ANGE);.    eqTer
2f8e8 6d 4d 61 73 6b 20 3d 20 69 64 78 45 71 54 65 72  mMask = idxEqTer
2f8e9 6d 4d 61 73 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  mMask;.  }..  /*
2f8ea 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
2f8eb 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2f8ec 61 6e 64 20 74 68 65 20 53 51 4c 49 54 45 5f 52  and the SQLITE_R
2f8ed 65 76 65 72 73 65 4f 72 64 65 72 20 66 6c 61 67  everseOrder flag
2f8ee 0a 20 20 2a 2a 20 69 73 20 73 65 74 2c 20 74 68  .  ** is set, th
2f8ef 65 6e 20 72 65 76 65 72 73 65 20 74 68 65 20 6f  en reverse the o
2f8f0 72 64 65 72 20 74 68 61 74 20 74 68 65 20 69 6e  rder that the in
2f8f1 64 65 78 20 77 69 6c 6c 20 62 65 20 73 63 61 6e  dex will be scan
2f8f2 6e 65 64 0a 20 20 2a 2a 20 69 6e 2e 20 54 68 69  ned.  ** in. Thi
2f8f3 73 20 69 73 20 75 73 65 64 20 66 6f 72 20 61 70  s is used for ap
2f8f4 70 6c 69 63 61 74 69 6f 6e 20 74 65 73 74 69 6e  plication testin
2f8f5 67 2c 20 74 6f 20 68 65 6c 70 20 66 69 6e 64 20  g, to help find 
2f8f6 63 61 73 65 73 0a 20 20 2a 2a 20 77 68 65 72 65  cases.  ** where
2f8f7 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 62 65 68   application beh
2f8f8 61 76 69 6f 75 72 20 64 65 70 65 6e 64 73 20 6f  aviour depends o
2f8f9 6e 20 74 68 65 20 28 75 6e 64 65 66 69 6e 65 64  n the (undefined
2f8fa 29 20 6f 72 64 65 72 20 74 68 61 74 0a 20 20 2a  ) order that.  *
2f8fb 2a 20 53 51 4c 69 74 65 20 6f 75 74 70 75 74 73  * SQLite outputs
2f8fc 20 72 6f 77 73 20 69 6e 20 69 6e 20 74 68 65 20   rows in in the 
2f8fd 61 62 73 65 6e 63 65 20 6f 66 20 61 6e 20 4f 52  absence of an OR
2f8fe 44 45 52 20 42 59 20 63 6c 61 75 73 65 2e 20 20  DER BY clause.  
2f8ff 2a 2f 0a 20 20 69 66 28 20 21 70 4f 72 64 65 72  */.  if( !pOrder
2f900 42 79 20 26 26 20 70 50 61 72 73 65 2d 3e 64 62  By && pParse->db
2f901 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
2f902 5f 52 65 76 65 72 73 65 4f 72 64 65 72 20 29 7b  _ReverseOrder ){
2f903 0a 20 20 20 20 70 43 6f 73 74 2d 3e 70 6c 61 6e  .    pCost->plan
2f904 2e 77 73 46 6c 61 67 73 20 7c 3d 20 57 48 45 52  .wsFlags |= WHER
2f905 45 5f 52 45 56 45 52 53 45 3b 0a 20 20 7d 0a 0a  E_REVERSE;.  }..
2f906 20 20 61 73 73 65 72 74 28 20 70 4f 72 64 65 72    assert( pOrder
2f907 42 79 20 7c 7c 20 28 70 43 6f 73 74 2d 3e 70 6c  By || (pCost->pl
2f908 61 6e 2e 77 73 46 6c 61 67 73 26 57 48 45 52 45  an.wsFlags&WHERE
2f909 5f 4f 52 44 45 52 42 59 29 3d 3d 30 20 29 3b 0a  _ORDERBY)==0 );.
2f90a 20 20 61 73 73 65 72 74 28 20 70 43 6f 73 74 2d    assert( pCost-
2f90b 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3d 3d 30 20  >plan.u.pIdx==0 
2f90c 7c 7c 20 28 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e  || (pCost->plan.
2f90d 77 73 46 6c 61 67 73 26 57 48 45 52 45 5f 52 4f  wsFlags&WHERE_RO
2f90e 57 49 44 5f 45 51 29 3d 3d 30 20 29 3b 0a 20 20  WID_EQ)==0 );.  
2f90f 61 73 73 65 72 74 28 20 70 53 72 63 2d 3e 70 49  assert( pSrc->pI
2f910 6e 64 65 78 3d 3d 30 20 0a 20 20 20 20 20 20 20  ndex==0 .       
2f911 7c 7c 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e 75  || pCost->plan.u
2f912 2e 70 49 64 78 3d 3d 30 20 0a 20 20 20 20 20 20  .pIdx==0 .      
2f913 20 7c 7c 20 70 43 6f 73 74 2d 3e 70 6c 61 6e 2e   || pCost->plan.
2f914 75 2e 70 49 64 78 3d 3d 70 53 72 63 2d 3e 70 49  u.pIdx==pSrc->pI
2f915 6e 64 65 78 20 0a 20 20 29 3b 0a 0a 20 20 57 48  ndex .  );..  WH
2f916 45 52 45 54 52 41 43 45 28 28 22 62 65 73 74 20  ERETRACE(("best 
2f917 69 6e 64 65 78 20 69 73 3a 20 25 73 5c 6e 22 2c  index is: %s\n",
2f918 20 0a 20 20 20 20 28 70 43 6f 73 74 2d 3e 70 6c   .    (pCost->pl
2f919 61 6e 2e 75 2e 70 49 64 78 20 3f 20 70 43 6f 73  an.u.pIdx ? pCos
2f91a 74 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e  t->plan.u.pIdx->
2f91b 7a 4e 61 6d 65 20 3a 20 22 69 70 6b 22 29 0a 20  zName : "ipk"). 
2f91c 20 29 29 3b 0a 20 20 0a 20 20 62 65 73 74 4f 72   ));.  .  bestOr
2f91d 43 6c 61 75 73 65 49 6e 64 65 78 28 70 50 61 72  ClauseIndex(pPar
2f91e 73 65 2c 20 70 57 43 2c 20 70 53 72 63 2c 20 6e  se, pWC, pSrc, n
2f91f 6f 74 52 65 61 64 79 2c 20 70 4f 72 64 65 72 42  otReady, pOrderB
2f920 79 2c 20 70 43 6f 73 74 29 3b 0a 20 20 70 43 6f  y, pCost);.  pCo
2f921 73 74 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  st->plan.wsFlags
2f922 20 7c 3d 20 65 71 54 65 72 6d 4d 61 73 6b 3b 0a   |= eqTermMask;.
2f923 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68  }../*.** Find th
2f924 65 20 71 75 65 72 79 20 70 6c 61 6e 20 66 6f 72  e query plan for
2f925 20 61 63 63 65 73 73 69 6e 67 20 74 61 62 6c 65   accessing table
2f926 20 70 53 72 63 2d 3e 70 54 61 62 2e 20 57 72 69   pSrc->pTab. Wri
2f927 74 65 20 74 68 65 0a 2a 2a 20 62 65 73 74 20 71  te the.** best q
2f928 75 65 72 79 20 70 6c 61 6e 20 61 6e 64 20 69 74  uery plan and it
2f929 73 20 63 6f 73 74 20 69 6e 74 6f 20 74 68 65 20  s cost into the 
2f92a 57 68 65 72 65 43 6f 73 74 20 6f 62 6a 65 63 74  WhereCost object
2f92b 20 73 75 70 70 6c 69 65 64 20 0a 2a 2a 20 61 73   supplied .** as
2f92c 20 74 68 65 20 6c 61 73 74 20 70 61 72 61 6d 65   the last parame
2f92d 74 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69  ter. This functi
2f92e 6f 6e 20 6d 61 79 20 63 61 6c 63 75 6c 61 74 65  on may calculate
2f92f 20 74 68 65 20 63 6f 73 74 20 6f 66 0a 2a 2a 20   the cost of.** 
2f930 62 6f 74 68 20 72 65 61 6c 20 61 6e 64 20 76 69  both real and vi
2f931 72 74 75 61 6c 20 74 61 62 6c 65 20 73 63 61 6e  rtual table scan
2f932 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
2f933 64 20 62 65 73 74 49 6e 64 65 78 28 0a 20 20 50  d bestIndex(.  P
2f934 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2f935 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2f936 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  e parsing contex
2f937 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75  t */.  WhereClau
2f938 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20 20 20  se *pWC,        
2f939 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
2f93a 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72 75  clause */.  stru
2f93b 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
2f93c 2a 70 53 72 63 2c 20 20 2f 2a 20 54 68 65 20 46  *pSrc,  /* The F
2f93d 52 4f 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 20  ROM clause term 
2f93e 74 6f 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 42  to search */.  B
2f93f 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79 2c  itmask notReady,
2f940 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
2f941 73 6b 20 6f 66 20 63 75 72 73 6f 72 73 20 74 68  sk of cursors th
2f942 61 74 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c  at are not avail
2f943 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69  able */.  ExprLi
2f944 73 74 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20  st *pOrderBy,   
2f945 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f 52 44        /* The ORD
2f946 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a  ER BY clause */.
2f947 20 20 57 68 65 72 65 43 6f 73 74 20 2a 70 43 6f    WhereCost *pCo
2f948 73 74 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  st            /*
2f949 20 4c 6f 77 65 73 74 20 63 6f 73 74 20 71 75 65   Lowest cost que
2f94a 72 79 20 70 6c 61 6e 20 2a 2f 0a 29 7b 0a 23 69  ry plan */.){.#i
2f94b 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2f94c 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
2f94d 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
2f94e 53 72 63 2d 3e 70 54 61 62 29 20 29 7b 0a 20 20  Src->pTab) ){.  
2f94f 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f    sqlite3_index_
2f950 69 6e 66 6f 20 2a 70 20 3d 20 30 3b 0a 20 20 20  info *p = 0;.   
2f951 20 62 65 73 74 56 69 72 74 75 61 6c 49 6e 64 65   bestVirtualInde
2f952 78 28 70 50 61 72 73 65 2c 20 70 57 43 2c 20 70  x(pParse, pWC, p
2f953 53 72 63 2c 20 6e 6f 74 52 65 61 64 79 2c 20 70  Src, notReady, p
2f954 4f 72 64 65 72 42 79 2c 20 70 43 6f 73 74 2c 20  OrderBy, pCost, 
2f955 26 70 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  &p);.    if( p->
2f956 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72  needToFreeIdxStr
2f957 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2f958 33 5f 66 72 65 65 28 70 2d 3e 69 64 78 53 74 72  3_free(p->idxStr
2f959 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
2f95a 69 74 65 33 44 62 46 72 65 65 28 70 50 61 72 73  ite3DbFree(pPars
2f95b 65 2d 3e 64 62 2c 20 70 29 3b 0a 20 20 7d 65 6c  e->db, p);.  }el
2f95c 73 65 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20  se.#endif.  {.  
2f95d 20 20 62 65 73 74 42 74 72 65 65 49 6e 64 65 78    bestBtreeIndex
2f95e 28 70 50 61 72 73 65 2c 20 70 57 43 2c 20 70 53  (pParse, pWC, pS
2f95f 72 63 2c 20 6e 6f 74 52 65 61 64 79 2c 20 70 4f  rc, notReady, pO
2f960 72 64 65 72 42 79 2c 20 70 43 6f 73 74 29 3b 0a  rderBy, pCost);.
2f961 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 69 73    }.}../*.** Dis
2f962 61 62 6c 65 20 61 20 74 65 72 6d 20 69 6e 20 74  able a term in t
2f963 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  he WHERE clause.
2f964 20 20 45 78 63 65 70 74 2c 20 64 6f 20 6e 6f 74    Except, do not
2f965 20 64 69 73 61 62 6c 65 20 74 68 65 20 74 65 72   disable the ter
2f966 6d 0a 2a 2a 20 69 66 20 69 74 20 63 6f 6e 74 72  m.** if it contr
2f967 6f 6c 73 20 61 20 4c 45 46 54 20 4f 55 54 45 52  ols a LEFT OUTER
2f968 20 4a 4f 49 4e 20 61 6e 64 20 69 74 20 64 69 64   JOIN and it did
2f969 20 6e 6f 74 20 6f 72 69 67 69 6e 61 74 65 20 69   not originate i
2f96a 6e 20 74 68 65 20 4f 4e 0a 2a 2a 20 6f 72 20 55  n the ON.** or U
2f96b 53 49 4e 47 20 63 6c 61 75 73 65 20 6f 66 20 74  SING clause of t
2f96c 68 61 74 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20  hat join..**.** 
2f96d 43 6f 6e 73 69 64 65 72 20 74 68 65 20 74 65 72  Consider the ter
2f96e 6d 20 74 32 2e 7a 3d 27 6f 6b 27 20 69 6e 20 74  m t2.z='ok' in t
2f96f 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 71 75 65  he following que
2f970 72 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 31  ries:.**.**   (1
2f971 29 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d  )  SELECT * FROM
2f972 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74 32   t1 LEFT JOIN t2
2f973 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 78 20 57 48   ON t1.a=t2.x WH
2f974 45 52 45 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a  ERE t2.z='ok'.**
2f975 20 20 20 28 32 29 20 20 53 45 4c 45 43 54 20 2a     (2)  SELECT *
2f976 20 46 52 4f 4d 20 74 31 20 4c 45 46 54 20 4a 4f   FROM t1 LEFT JO
2f977 49 4e 20 74 32 20 4f 4e 20 74 31 2e 61 3d 74 32  IN t2 ON t1.a=t2
2f978 2e 78 20 41 4e 44 20 74 32 2e 7a 3d 27 6f 6b 27  .x AND t2.z='ok'
2f979 0a 2a 2a 20 20 20 28 33 29 20 20 53 45 4c 45 43  .**   (3)  SELEC
2f97a 54 20 2a 20 46 52 4f 4d 20 74 31 2c 20 74 32 20  T * FROM t1, t2 
2f97b 57 48 45 52 45 20 74 31 2e 61 3d 74 32 2e 78 20  WHERE t1.a=t2.x 
2f97c 41 4e 44 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a  AND t2.z='ok'.**
2f97d 0a 2a 2a 20 54 68 65 20 74 32 2e 7a 3d 27 6f 6b  .** The t2.z='ok
2f97e 27 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e  ' is disabled in
2f97f 20 74 68 65 20 69 6e 20 28 32 29 20 62 65 63 61   the in (2) beca
2f980 75 73 65 20 69 74 20 6f 72 69 67 69 6e 61 74 65  use it originate
2f981 73 0a 2a 2a 20 69 6e 20 74 68 65 20 4f 4e 20 63  s.** in the ON c
2f982 6c 61 75 73 65 2e 20 20 54 68 65 20 74 65 72 6d  lause.  The term
2f983 20 69 73 20 64 69 73 61 62 6c 65 64 20 69 6e 20   is disabled in 
2f984 28 33 29 20 62 65 63 61 75 73 65 20 69 74 20 69  (3) because it i
2f985 73 20 6e 6f 74 20 70 61 72 74 0a 2a 2a 20 6f 66  s not part.** of
2f986 20 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f   a LEFT OUTER JO
2f987 49 4e 2e 20 20 49 6e 20 28 31 29 2c 20 74 68 65  IN.  In (1), the
2f988 20 74 65 72 6d 20 69 73 20 6e 6f 74 20 64 69 73   term is not dis
2f989 61 62 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 44 69 73  abled..**.** Dis
2f98a 61 62 6c 69 6e 67 20 61 20 74 65 72 6d 20 63 61  abling a term ca
2f98b 75 73 65 73 20 74 68 61 74 20 74 65 72 6d 20 74  uses that term t
2f98c 6f 20 6e 6f 74 20 62 65 20 74 65 73 74 65 64 20  o not be tested 
2f98d 69 6e 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f  in the inner loo
2f98e 70 0a 2a 2a 20 6f 66 20 74 68 65 20 6a 6f 69 6e  p.** of the join
2f98f 2e 20 20 44 69 73 61 62 6c 69 6e 67 20 69 73 20  .  Disabling is 
2f990 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e  an optimization.
2f991 20 20 57 68 65 6e 20 74 65 72 6d 73 20 61 72 65    When terms are
2f992 20 73 61 74 69 73 66 69 65 64 0a 2a 2a 20 62 79   satisfied.** by
2f993 20 69 6e 64 69 63 65 73 2c 20 77 65 20 64 69 73   indices, we dis
2f994 61 62 6c 65 20 74 68 65 6d 20 74 6f 20 70 72 65  able them to pre
2f995 76 65 6e 74 20 72 65 64 75 6e 64 61 6e 74 20 74  vent redundant t
2f996 65 73 74 73 20 69 6e 20 74 68 65 20 69 6e 6e 65  ests in the inne
2f997 72 0a 2a 2a 20 6c 6f 6f 70 2e 20 20 57 65 20 77  r.** loop.  We w
2f998 6f 75 6c 64 20 67 65 74 20 74 68 65 20 63 6f 72  ould get the cor
2f999 72 65 63 74 20 72 65 73 75 6c 74 73 20 69 66 20  rect results if 
2f99a 6e 6f 74 68 69 6e 67 20 77 65 72 65 20 65 76 65  nothing were eve
2f99b 72 20 64 69 73 61 62 6c 65 64 2c 0a 2a 2a 20 62  r disabled,.** b
2f99c 75 74 20 6a 6f 69 6e 73 20 6d 69 67 68 74 20 72  ut joins might r
2f99d 75 6e 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77  un a little slow
2f99e 65 72 2e 20 20 54 68 65 20 74 72 69 63 6b 20 69  er.  The trick i
2f99f 73 20 74 6f 20 64 69 73 61 62 6c 65 20 61 73 20  s to disable as 
2f9a0 6d 75 63 68 0a 2a 2a 20 61 73 20 77 65 20 63 61  much.** as we ca
2f9a1 6e 20 77 69 74 68 6f 75 74 20 64 69 73 61 62 6c  n without disabl
2f9a2 69 6e 67 20 74 6f 6f 20 6d 75 63 68 2e 20 20 49  ing too much.  I
2f9a3 66 20 77 65 20 64 69 73 61 62 6c 65 64 20 69 6e  f we disabled in
2f9a4 20 28 31 29 2c 20 77 65 27 64 20 67 65 74 0a 2a   (1), we'd get.*
2f9a5 2a 20 74 68 65 20 77 72 6f 6e 67 20 61 6e 73 77  * the wrong answ
2f9a6 65 72 2e 20 20 53 65 65 20 74 69 63 6b 65 74 20  er.  See ticket 
2f9a7 23 38 31 33 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  #813..*/.static 
2f9a8 76 6f 69 64 20 64 69 73 61 62 6c 65 54 65 72 6d  void disableTerm
2f9a9 28 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65  (WhereLevel *pLe
2f9aa 76 65 6c 2c 20 57 68 65 72 65 54 65 72 6d 20 2a  vel, WhereTerm *
2f9ab 70 54 65 72 6d 29 7b 0a 20 20 69 66 28 20 70 54  pTerm){.  if( pT
2f9ac 65 72 6d 0a 20 20 20 20 20 20 26 26 20 41 4c 57  erm.      && ALW
2f9ad 41 59 53 28 28 70 54 65 72 6d 2d 3e 77 74 46 6c  AYS((pTerm->wtFl
2f9ae 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 44 45 44  ags & TERM_CODED
2f9af 29 3d 3d 30 29 0a 20 20 20 20 20 20 26 26 20 28  )==0).      && (
2f9b0 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69  pLevel->iLeftJoi
2f9b1 6e 3d 3d 30 20 7c 7c 20 45 78 70 72 48 61 73 50  n==0 || ExprHasP
2f9b2 72 6f 70 65 72 74 79 28 70 54 65 72 6d 2d 3e 70  roperty(pTerm->p
2f9b3 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f 69  Expr, EP_FromJoi
2f9b4 6e 29 29 0a 20 20 29 7b 0a 20 20 20 20 70 54 65  n)).  ){.    pTe
2f9b5 72 6d 2d 3e 77 74 46 6c 61 67 73 20 7c 3d 20 54  rm->wtFlags |= T
2f9b6 45 52 4d 5f 43 4f 44 45 44 3b 0a 20 20 20 20 69  ERM_CODED;.    i
2f9b7 66 28 20 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e  f( pTerm->iParen
2f9b8 74 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 57 68  t>=0 ){.      Wh
2f9b9 65 72 65 54 65 72 6d 20 2a 70 4f 74 68 65 72 20  ereTerm *pOther 
2f9ba 3d 20 26 70 54 65 72 6d 2d 3e 70 57 43 2d 3e 61  = &pTerm->pWC->a
2f9bb 5b 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 5d  [pTerm->iParent]
2f9bc 3b 0a 20 20 20 20 20 20 69 66 28 20 28 2d 2d 70  ;.      if( (--p
2f9bd 4f 74 68 65 72 2d 3e 6e 43 68 69 6c 64 29 3d 3d  Other->nChild)==
2f9be 30 20 29 7b 0a 20 20 20 20 20 20 20 20 64 69 73  0 ){.        dis
2f9bf 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c  ableTerm(pLevel,
2f9c0 20 70 4f 74 68 65 72 29 3b 0a 20 20 20 20 20 20   pOther);.      
2f9c1 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  }.    }.  }.}../
2f9c2 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f  *.** Code an OP_
2f9c3 41 66 66 69 6e 69 74 79 20 6f 70 63 6f 64 65 20  Affinity opcode 
2f9c4 74 6f 20 61 70 70 6c 79 20 74 68 65 20 63 6f 6c  to apply the col
2f9c5 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72  umn affinity str
2f9c6 69 6e 67 20 7a 41 66 66 0a 2a 2a 20 74 6f 20 74  ing zAff.** to t
2f9c7 68 65 20 6e 20 72 65 67 69 73 74 65 72 73 20 73  he n registers s
2f9c8 74 61 72 74 69 6e 67 20 61 74 20 62 61 73 65 2e  tarting at base.
2f9c9 20 0a 2a 2a 0a 2a 2a 20 42 75 66 66 65 72 20 7a   .**.** Buffer z
2f9ca 41 66 66 20 77 61 73 20 61 6c 6c 6f 63 61 74 65  Aff was allocate
2f9cb 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 44  d using sqlite3D
2f9cc 62 4d 61 6c 6c 6f 63 28 29 2e 20 49 74 20 69 73  bMalloc(). It is
2f9cd 20 74 68 65 20 0a 2a 2a 20 72 65 73 70 6f 6e 73   the .** respons
2f9ce 69 62 69 6c 69 74 79 20 6f 66 20 74 68 69 73 20  ibility of this 
2f9cf 66 75 6e 63 74 69 6f 6e 20 74 6f 20 61 72 72 61  function to arra
2f9d0 6e 67 65 20 66 6f 72 20 69 74 20 74 6f 20 62 65  nge for it to be
2f9d1 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a 20 66   eventually.** f
2f9d2 72 65 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74  reed using sqlit
2f9d3 65 33 44 62 46 72 65 65 28 29 2e 0a 2a 2f 0a 73  e3DbFree()..*/.s
2f9d4 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 41  tatic void codeA
2f9d5 70 70 6c 79 41 66 66 69 6e 69 74 79 28 50 61 72  pplyAffinity(Par
2f9d6 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
2f9d7 62 61 73 65 2c 20 69 6e 74 20 6e 2c 20 63 68 61  base, int n, cha
2f9d8 72 20 2a 7a 41 66 66 29 7b 0a 20 20 56 64 62 65  r *zAff){.  Vdbe
2f9d9 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
2f9da 64 62 65 3b 0a 20 20 61 73 73 65 72 74 28 20 76  dbe;.  assert( v
2f9db 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  !=0 );.  sqlite3
2f9dc 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2f9dd 5f 41 66 66 69 6e 69 74 79 2c 20 62 61 73 65 2c  _Affinity, base,
2f9de 20 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64   n);.  sqlite3Vd
2f9df 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31  beChangeP4(v, -1
2f9e0 2c 20 7a 41 66 66 2c 20 50 34 5f 44 59 4e 41 4d  , zAff, P4_DYNAM
2f9e1 49 43 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  IC);.  sqlite3Ex
2f9e2 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43  prCacheAffinityC
2f9e3 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 62 61  hange(pParse, ba
2f9e4 73 65 2c 20 6e 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  se, n);.}.../*.*
2f9e5 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
2f9e6 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 65 71 75  for a single equ
2f9e7 61 6c 69 74 79 20 74 65 72 6d 20 6f 66 20 74 68  ality term of th
2f9e8 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20  e WHERE clause. 
2f9e9 20 41 6e 20 65 71 75 61 6c 69 74 79 0a 2a 2a 20   An equality.** 
2f9ea 74 65 72 6d 20 63 61 6e 20 62 65 20 65 69 74 68  term can be eith
2f9eb 65 72 20 58 3d 65 78 70 72 20 6f 72 20 58 20 49  er X=expr or X I
2f9ec 4e 20 28 2e 2e 2e 29 2e 20 20 20 70 54 65 72 6d  N (...).   pTerm
2f9ed 20 69 73 20 74 68 65 20 74 65 72 6d 20 74 6f 20   is the term to 
2f9ee 62 65 20 0a 2a 2a 20 63 6f 64 65 64 2e 0a 2a 2a  be .** coded..**
2f9ef 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20  .** The current 
2f9f0 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 63 6f  value for the co
2f9f1 6e 73 74 72 61 69 6e 74 20 69 73 20 6c 65 66 74  nstraint is left
2f9f2 20 69 6e 20 72 65 67 69 73 74 65 72 20 69 52 65   in register iRe
2f9f3 67 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 63  g..**.** For a c
2f9f4 6f 6e 73 74 72 61 69 6e 74 20 6f 66 20 74 68 65  onstraint of the
2f9f5 20 66 6f 72 6d 20 58 3d 65 78 70 72 2c 20 74 68   form X=expr, th
2f9f6 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
2f9f7 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 69 74  evaluated and it
2f9f8 73 0a 2a 2a 20 72 65 73 75 6c 74 20 69 73 20 6c  s.** result is l
2f9f9 65 66 74 20 6f 6e 20 74 68 65 20 73 74 61 63 6b  eft on the stack
2f9fa 2e 20 20 46 6f 72 20 63 6f 6e 73 74 72 61 69 6e  .  For constrain
2f9fb 74 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 58  ts of the form X
2f9fc 20 49 4e 20 28 2e 2e 2e 29 0a 2a 2a 20 74 68 69   IN (...).** thi
2f9fd 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 75  s routine sets u
2f9fe 70 20 61 20 6c 6f 6f 70 20 74 68 61 74 20 77 69  p a loop that wi
2f9ff 6c 6c 20 69 74 65 72 61 74 65 20 6f 76 65 72 20  ll iterate over 
2fa00 61 6c 6c 20 76 61 6c 75 65 73 20 6f 66 20 58 2e  all values of X.
2fa01 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
2fa02 6f 64 65 45 71 75 61 6c 69 74 79 54 65 72 6d 28  odeEqualityTerm(
2fa03 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2fa04 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61  ,      /* The pa
2fa05 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
2fa06 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54  .  WhereTerm *pT
2fa07 65 72 6d 2c 20 20 20 2f 2a 20 54 68 65 20 74 65  erm,   /* The te
2fa08 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  rm of the WHERE 
2fa09 63 6c 61 75 73 65 20 74 6f 20 62 65 20 63 6f 64  clause to be cod
2fa0a 65 64 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76  ed */.  WhereLev
2fa0b 65 6c 20 2a 70 4c 65 76 65 6c 2c 20 2f 2a 20 57  el *pLevel, /* W
2fa0c 68 65 6e 20 6c 65 76 65 6c 20 6f 66 20 74 68 65  hen level of the
2fa0d 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 77 65 20   FROM clause we 
2fa0e 61 72 65 20 77 6f 72 6b 69 6e 67 20 6f 6e 20 2a  are working on *
2fa0f 2f 0a 20 20 69 6e 74 20 69 54 61 72 67 65 74 20  /.  int iTarget 
2fa10 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74 65 6d          /* Attem
2fa11 70 74 20 74 6f 20 6c 65 61 76 65 20 72 65 73 75  pt to leave resu
2fa12 6c 74 73 20 69 6e 20 74 68 69 73 20 72 65 67 69  lts in this regi
2fa13 73 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 45 78 70  ster */.){.  Exp
2fa14 72 20 2a 70 58 20 3d 20 70 54 65 72 6d 2d 3e 70  r *pX = pTerm->p
2fa15 45 78 70 72 3b 0a 20 20 56 64 62 65 20 2a 76 20  Expr;.  Vdbe *v 
2fa16 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
2fa17 0a 20 20 69 6e 74 20 69 52 65 67 3b 20 20 20 20  .  int iReg;    
2fa18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2fa19 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   Register holdin
2fa1a 67 20 72 65 73 75 6c 74 73 20 2a 2f 0a 0a 20 20  g results */..  
2fa1b 61 73 73 65 72 74 28 20 69 54 61 72 67 65 74 3e  assert( iTarget>
2fa1c 30 20 29 3b 0a 20 20 69 66 28 20 70 58 2d 3e 6f  0 );.  if( pX->o
2fa1d 70 3d 3d 54 4b 5f 45 51 20 29 7b 0a 20 20 20 20  p==TK_EQ ){.    
2fa1e 69 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78  iReg = sqlite3Ex
2fa1f 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61  prCodeTarget(pPa
2fa20 72 73 65 2c 20 70 58 2d 3e 70 52 69 67 68 74 2c  rse, pX->pRight,
2fa21 20 69 54 61 72 67 65 74 29 3b 0a 20 20 7d 65 6c   iTarget);.  }el
2fa22 73 65 20 69 66 28 20 70 58 2d 3e 6f 70 3d 3d 54  se if( pX->op==T
2fa23 4b 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  K_ISNULL ){.    
2fa24 69 52 65 67 20 3d 20 69 54 61 72 67 65 74 3b 0a  iReg = iTarget;.
2fa25 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2fa26 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
2fa27 2c 20 30 2c 20 69 52 65 67 29 3b 0a 23 69 66 6e  , 0, iReg);.#ifn
2fa28 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2fa29 53 55 42 51 55 45 52 59 0a 20 20 7d 65 6c 73 65  SUBQUERY.  }else
2fa2a 7b 0a 20 20 20 20 69 6e 74 20 65 54 79 70 65 3b  {.    int eType;
2fa2b 0a 20 20 20 20 69 6e 74 20 69 54 61 62 3b 0a 20  .    int iTab;. 
2fa2c 20 20 20 73 74 72 75 63 74 20 49 6e 4c 6f 6f 70     struct InLoop
2fa2d 20 2a 70 49 6e 3b 0a 0a 20 20 20 20 61 73 73 65   *pIn;..    asse
2fa2e 72 74 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 49  rt( pX->op==TK_I
2fa2f 4e 20 29 3b 0a 20 20 20 20 69 52 65 67 20 3d 20  N );.    iReg = 
2fa30 69 54 61 72 67 65 74 3b 0a 20 20 20 20 65 54 79  iTarget;.    eTy
2fa31 70 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  pe = sqlite3Find
2fa32 49 6e 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  InIndex(pParse, 
2fa33 70 58 2c 20 30 29 3b 0a 20 20 20 20 69 54 61 62  pX, 0);.    iTab
2fa34 20 3d 20 70 58 2d 3e 69 54 61 62 6c 65 3b 0a 20   = pX->iTable;. 
2fa35 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2fa36 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
2fa37 64 2c 20 69 54 61 62 2c 20 30 29 3b 0a 20 20 20  d, iTab, 0);.   
2fa38 20 61 73 73 65 72 74 28 20 70 4c 65 76 65 6c 2d   assert( pLevel-
2fa39 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
2fa3a 57 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 29 3b  WHERE_IN_ABLE );
2fa3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d  .    if( pLevel-
2fa3c 3e 75 2e 69 6e 2e 6e 49 6e 3d 3d 30 20 29 7b 0a  >u.in.nIn==0 ){.
2fa3d 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 61 64        pLevel->ad
2fa3e 64 72 4e 78 74 20 3d 20 73 71 6c 69 74 65 33 56  drNxt = sqlite3V
2fa3f 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
2fa40 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c 65 76 65  .    }.    pLeve
2fa41 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 2b 2b 3b 0a 20  l->u.in.nIn++;. 
2fa42 20 20 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e     pLevel->u.in.
2fa43 61 49 6e 4c 6f 6f 70 20 3d 0a 20 20 20 20 20 20  aInLoop =.      
2fa44 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f   sqlite3DbReallo
2fa45 63 4f 72 46 72 65 65 28 70 50 61 72 73 65 2d 3e  cOrFree(pParse->
2fa46 64 62 2c 20 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e  db, pLevel->u.in
2fa47 2e 61 49 6e 4c 6f 6f 70 2c 0a 20 20 20 20 20 20  .aInLoop,.      
2fa48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa49 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70          sizeof(p
2fa4a 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 61 49 6e 4c  Level->u.in.aInL
2fa4b 6f 6f 70 5b 30 5d 29 2a 70 4c 65 76 65 6c 2d 3e  oop[0])*pLevel->
2fa4c 75 2e 69 6e 2e 6e 49 6e 29 3b 0a 20 20 20 20 70  u.in.nIn);.    p
2fa4d 49 6e 20 3d 20 70 4c 65 76 65 6c 2d 3e 75 2e 69  In = pLevel->u.i
2fa4e 6e 2e 61 49 6e 4c 6f 6f 70 3b 0a 20 20 20 20 69  n.aInLoop;.    i
2fa4f 66 28 20 70 49 6e 20 29 7b 0a 20 20 20 20 20 20  f( pIn ){.      
2fa50 70 49 6e 20 2b 3d 20 70 4c 65 76 65 6c 2d 3e 75  pIn += pLevel->u
2fa51 2e 69 6e 2e 6e 49 6e 20 2d 20 31 3b 0a 20 20 20  .in.nIn - 1;.   
2fa52 20 20 20 70 49 6e 2d 3e 69 43 75 72 20 3d 20 69     pIn->iCur = i
2fa53 54 61 62 3b 0a 20 20 20 20 20 20 69 66 28 20 65  Tab;.      if( e
2fa54 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52  Type==IN_INDEX_R
2fa55 4f 57 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20  OWID ){.        
2fa56 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 20 3d  pIn->addrInTop =
2fa57 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2fa58 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20  p2(v, OP_Rowid, 
2fa59 69 54 61 62 2c 20 69 52 65 67 29 3b 0a 20 20 20  iTab, iReg);.   
2fa5a 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2fa5b 20 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70    pIn->addrInTop
2fa5c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2fa5d 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d  dOp3(v, OP_Colum
2fa5e 6e 2c 20 69 54 61 62 2c 20 30 2c 20 69 52 65 67  n, iTab, 0, iReg
2fa5f 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2fa60 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2fa61 70 31 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c  p1(v, OP_IsNull,
2fa62 20 69 52 65 67 29 3b 0a 20 20 20 20 7d 65 6c 73   iReg);.    }els
2fa63 65 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d  e{.      pLevel-
2fa64 3e 75 2e 69 6e 2e 6e 49 6e 20 3d 20 30 3b 0a 20  >u.in.nIn = 0;. 
2fa65 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
2fa66 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c    disableTerm(pL
2fa67 65 76 65 6c 2c 20 70 54 65 72 6d 29 3b 0a 20 20  evel, pTerm);.  
2fa68 72 65 74 75 72 6e 20 69 52 65 67 3b 0a 7d 0a 0a  return iReg;.}..
2fa69 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63  /*.** Generate c
2fa6a 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20 65 76  ode that will ev
2fa6b 61 6c 75 61 74 65 20 61 6c 6c 20 3d 3d 20 61 6e  aluate all == an
2fa6c 64 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73  d IN constraints
2fa6d 20 66 6f 72 20 61 6e 0a 2a 2a 20 69 6e 64 65 78   for an.** index
2fa6e 2e 20 20 54 68 65 20 76 61 6c 75 65 73 20 66 6f  .  The values fo
2fa6f 72 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74  r all constraint
2fa70 73 20 61 72 65 20 6c 65 66 74 20 6f 6e 20 74 68  s are left on th
2fa71 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46  e stack..**.** F
2fa72 6f 72 20 65 78 61 6d 70 6c 65 2c 20 63 6f 6e 73  or example, cons
2fa73 69 64 65 72 20 74 61 62 6c 65 20 74 31 28 61 2c  ider table t1(a,
2fa74 62 2c 63 2c 64 2c 65 2c 66 29 20 77 69 74 68 20  b,c,d,e,f) with 
2fa75 69 6e 64 65 78 20 69 31 28 61 2c 62 2c 63 29 2e  index i1(a,b,c).
2fa76 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74 68 65 20  .** Suppose the 
2fa77 57 48 45 52 45 20 63 6c 61 75 73 65 20 69 73 20  WHERE clause is 
2fa78 74 68 69 73 3a 20 20 61 3d 3d 35 20 41 4e 44 20  this:  a==5 AND 
2fa79 62 20 49 4e 20 28 31 2c 32 2c 33 29 20 41 4e 44  b IN (1,2,3) AND
2fa7a 20 63 3e 35 20 41 4e 44 20 63 3c 31 30 0a 2a 2a   c>5 AND c<10.**
2fa7b 20 54 68 65 20 69 6e 64 65 78 20 68 61 73 20 61   The index has a
2fa7c 73 20 6d 61 6e 79 20 61 73 20 74 68 72 65 65 20  s many as three 
2fa7d 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
2fa7e 69 6e 74 73 2c 20 62 75 74 20 69 6e 20 74 68 69  ints, but in thi
2fa7f 73 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 74 68  s.** example, th
2fa80 65 20 74 68 69 72 64 20 22 63 22 20 76 61 6c 75  e third "c" valu
2fa81 65 20 69 73 20 61 6e 20 69 6e 65 71 75 61 6c 69  e is an inequali
2fa82 74 79 2e 20 20 53 6f 20 6f 6e 6c 79 20 74 77 6f  ty.  So only two
2fa83 20 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 73   .** constraints
2fa84 20 61 72 65 20 63 6f 64 65 64 2e 20 20 54 68 69   are coded.  Thi
2fa85 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 67  s routine will g
2fa86 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
2fa87 65 76 61 6c 75 61 74 65 0a 2a 2a 20 61 3d 3d 35  evaluate.** a==5
2fa88 20 61 6e 64 20 62 20 49 4e 20 28 31 2c 32 2c 33   and b IN (1,2,3
2fa89 29 2e 20 20 54 68 65 20 63 75 72 72 65 6e 74 20  ).  The current 
2fa8a 76 61 6c 75 65 73 20 66 6f 72 20 61 20 61 6e 64  values for a and
2fa8b 20 62 20 77 69 6c 6c 20 62 65 20 73 74 6f 72 65   b will be store
2fa8c 64 0a 2a 2a 20 69 6e 20 63 6f 6e 73 65 63 75 74  d.** in consecut
2fa8d 69 76 65 20 72 65 67 69 73 74 65 72 73 20 61 6e  ive registers an
2fa8e 64 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74  d the index of t
2fa8f 68 65 20 66 69 72 73 74 20 72 65 67 69 73 74 65  he first registe
2fa90 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  r is returned..*
2fa91 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 65 78 61 6d  *.** In the exam
2fa92 70 6c 65 20 61 62 6f 76 65 20 6e 45 71 3d 3d 32  ple above nEq==2
2fa93 2e 20 20 42 75 74 20 74 68 69 73 20 73 75 62 72  .  But this subr
2fa94 6f 75 74 69 6e 65 20 77 6f 72 6b 73 20 66 6f 72  outine works for
2fa95 20 61 6e 79 20 76 61 6c 75 65 0a 2a 2a 20 6f 66   any value.** of
2fa96 20 6e 45 71 20 69 6e 63 6c 75 64 69 6e 67 20 30   nEq including 0
2fa97 2e 20 20 49 66 20 6e 45 71 3d 3d 30 2c 20 74 68  .  If nEq==0, th
2fa98 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 65  is routine is ne
2fa99 61 72 6c 79 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a  arly a no-op..**
2fa9a 20 54 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20   The only thing 
2fa9b 69 74 20 64 6f 65 73 20 69 73 20 61 6c 6c 6f 63  it does is alloc
2fa9c 61 74 65 20 74 68 65 20 70 4c 65 76 65 6c 2d 3e  ate the pLevel->
2fa9d 69 4d 65 6d 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  iMem memory cell
2fa9e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
2fa9f 74 69 6e 65 20 61 6c 77 61 79 73 20 61 6c 6c 6f  tine always allo
2faa0 63 61 74 65 73 20 61 74 20 6c 65 61 73 74 20 6f  cates at least o
2faa1 6e 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 61  ne memory cell a
2faa2 6e 64 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68  nd returns.** th
2faa3 65 20 69 6e 64 65 78 20 6f 66 20 74 68 61 74 20  e index of that 
2faa4 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 54 68 65  memory cell. The
2faa5 20 63 6f 64 65 20 74 68 61 74 0a 2a 2a 20 63 61   code that.** ca
2faa6 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  lls this routine
2faa7 20 77 69 6c 6c 20 75 73 65 20 74 68 61 74 20 6d   will use that m
2faa8 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 73 74  emory cell to st
2faa9 6f 72 65 20 74 68 65 20 74 65 72 6d 69 6e 61 74  ore the terminat
2faaa 69 6f 6e 0a 2a 2a 20 6b 65 79 20 76 61 6c 75 65  ion.** key value
2faab 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 20 20 49   of the loop.  I
2faac 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 49 4e  f one or more IN
2faad 20 6f 70 65 72 61 74 6f 72 73 20 61 70 70 65 61   operators appea
2faae 72 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20  r, then.** this 
2faaf 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65  routine allocate
2fab0 73 20 61 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20  s an additional 
2fab1 6e 45 71 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73  nEq memory cells
2fab2 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a   for internal.**
2fab3 20 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 42 65 66 6f   use..**.** Befo
2fab4 72 65 20 72 65 74 75 72 6e 69 6e 67 2c 20 2a 70  re returning, *p
2fab5 7a 41 66 66 20 69 73 20 73 65 74 20 74 6f 20 70  zAff is set to p
2fab6 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72  oint to a buffer
2fab7 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 0a 2a 2a   containing a.**
2fab8 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63 6f 6c   copy of the col
2fab9 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72  umn affinity str
2faba 69 6e 67 20 6f 66 20 74 68 65 20 69 6e 64 65 78  ing of the index
2fabb 20 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67   allocated using
2fabc 0a 2a 2a 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  .** sqlite3DbMal
2fabd 6c 6f 63 28 29 2e 20 45 78 63 65 70 74 2c 20 65  loc(). Except, e
2fabe 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 63 6f  ntries in the co
2fabf 70 79 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67  py of the string
2fac0 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77   associated.** w
2fac1 69 74 68 20 65 71 75 61 6c 69 74 79 20 63 6f 6e  ith equality con
2fac2 73 74 72 61 69 6e 74 73 20 74 68 61 74 20 75 73  straints that us
2fac3 65 20 4e 4f 4e 45 20 61 66 66 69 6e 69 74 79 20  e NONE affinity 
2fac4 61 72 65 20 73 65 74 20 74 6f 0a 2a 2a 20 53 51  are set to.** SQ
2fac5 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 2e 20 54  LITE_AFF_NONE. T
2fac6 68 69 73 20 69 73 20 74 6f 20 64 65 61 6c 20 77  his is to deal w
2fac7 69 74 68 20 53 51 4c 20 73 75 63 68 20 61 73 20  ith SQL such as 
2fac8 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a  the following:.*
2fac9 2a 0a 2a 2a 20 20 20 43 52 45 41 54 45 20 54 41  *.**   CREATE TA
2faca 42 4c 45 20 74 31 28 61 20 54 45 58 54 20 50 52  BLE t1(a TEXT PR
2facb 49 4d 41 52 59 20 4b 45 59 2c 20 62 29 3b 0a 2a  IMARY KEY, b);.*
2facc 2a 20 20 20 53 45 4c 45 43 54 20 2e 2e 2e 20 46  *   SELECT ... F
2facd 52 4f 4d 20 74 31 20 41 53 20 74 32 2c 20 74 31  ROM t1 AS t2, t1
2face 20 57 48 45 52 45 20 74 31 2e 61 20 3d 20 74 32   WHERE t1.a = t2
2facf 2e 62 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65  .b;.**.** In the
2fad0 20 65 78 61 6d 70 6c 65 20 61 62 6f 76 65 2c 20   example above, 
2fad1 74 68 65 20 69 6e 64 65 78 20 6f 6e 20 74 31 28  the index on t1(
2fad2 61 29 20 68 61 73 20 54 45 58 54 20 61 66 66 69  a) has TEXT affi
2fad3 6e 69 74 79 2e 20 42 75 74 20 73 69 6e 63 65 0a  nity. But since.
2fad4 2a 2a 20 74 68 65 20 72 69 67 68 74 20 68 61 6e  ** the right han
2fad5 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 65 71  d side of the eq
2fad6 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e  uality constrain
2fad7 74 20 28 74 32 2e 62 29 20 68 61 73 20 4e 4f 4e  t (t2.b) has NON
2fad8 45 20 61 66 66 69 6e 69 74 79 2c 0a 2a 2a 20 6e  E affinity,.** n
2fad9 6f 20 63 6f 6e 76 65 72 73 69 6f 6e 20 73 68 6f  o conversion sho
2fada 75 6c 64 20 62 65 20 61 74 74 65 6d 70 74 65 64  uld be attempted
2fadb 20 62 65 66 6f 72 65 20 75 73 69 6e 67 20 61 20   before using a 
2fadc 74 32 2e 62 20 76 61 6c 75 65 20 61 73 20 70 61  t2.b value as pa
2fadd 72 74 20 6f 66 0a 2a 2a 20 61 20 6b 65 79 20 74  rt of.** a key t
2fade 6f 20 73 65 61 72 63 68 20 74 68 65 20 69 6e 64  o search the ind
2fadf 65 78 2e 20 48 65 6e 63 65 20 74 68 65 20 66 69  ex. Hence the fi
2fae0 72 73 74 20 62 79 74 65 20 69 6e 20 74 68 65 20  rst byte in the 
2fae1 72 65 74 75 72 6e 65 64 20 61 66 66 69 6e 69 74  returned affinit
2fae2 79 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 20 74  y.** string in t
2fae3 68 69 73 20 65 78 61 6d 70 6c 65 20 77 6f 75 6c  his example woul
2fae4 64 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c 49  d be set to SQLI
2fae5 54 45 5f 41 46 46 5f 4e 4f 4e 45 2e 0a 2a 2f 0a  TE_AFF_NONE..*/.
2fae6 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64 65 41  static int codeA
2fae7 6c 6c 45 71 75 61 6c 69 74 79 54 65 72 6d 73 28  llEqualityTerms(
2fae8 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
2fae9 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73  ,        /* Pars
2faea 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
2faeb 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65   WhereLevel *pLe
2faec 76 65 6c 2c 20 20 20 2f 2a 20 57 68 69 63 68 20  vel,   /* Which 
2faed 6e 65 73 74 65 64 20 6c 6f 6f 70 20 6f 66 20 74  nested loop of t
2faee 68 65 20 46 52 4f 4d 20 77 65 20 61 72 65 20 63  he FROM we are c
2faef 6f 64 69 6e 67 20 2a 2f 0a 20 20 57 68 65 72 65  oding */.  Where
2faf0 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20  Clause *pWC,    
2faf1 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c   /* The WHERE cl
2faf2 61 75 73 65 20 2a 2f 0a 20 20 42 69 74 6d 61 73  ause */.  Bitmas
2faf3 6b 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20  k notReady,     
2faf4 2f 2a 20 57 68 69 63 68 20 70 61 72 74 73 20 6f  /* Which parts o
2faf5 66 20 46 52 4f 4d 20 68 61 76 65 20 6e 6f 74 20  f FROM have not 
2faf6 79 65 74 20 62 65 65 6e 20 63 6f 64 65 64 20 2a  yet been coded *
2faf7 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 52 65  /.  int nExtraRe
2faf8 67 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  g,        /* Num
2faf9 62 65 72 20 6f 66 20 65 78 74 72 61 20 72 65 67  ber of extra reg
2fafa 69 73 74 65 72 73 20 74 6f 20 61 6c 6c 6f 63 61  isters to alloca
2fafb 74 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70  te */.  char **p
2fafc 7a 41 66 66 20 20 20 20 20 20 20 20 20 20 2f 2a  zAff          /*
2fafd 20 4f 55 54 3a 20 53 65 74 20 74 6f 20 70 6f 69   OUT: Set to poi
2fafe 6e 74 20 74 6f 20 61 66 66 69 6e 69 74 79 20 73  nt to affinity s
2faff 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e  tring */.){.  in
2fb00 74 20 6e 45 71 20 3d 20 70 4c 65 76 65 6c 2d 3e  t nEq = pLevel->
2fb01 70 6c 61 6e 2e 6e 45 71 3b 20 20 20 2f 2a 20 54  plan.nEq;   /* T
2fb02 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 3d 3d 20  he number of == 
2fb03 6f 72 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74  or IN constraint
2fb04 73 20 74 6f 20 63 6f 64 65 20 2a 2f 0a 20 20 56  s to code */.  V
2fb05 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
2fb06 3e 70 56 64 62 65 3b 20 20 20 20 20 20 2f 2a 20  >pVdbe;      /* 
2fb07 54 68 65 20 76 6d 20 75 6e 64 65 72 20 63 6f 6e  The vm under con
2fb08 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 20 20 49  struction */.  I
2fb09 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20  ndex *pIdx;     
2fb0a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2fb0b 54 68 65 20 69 6e 64 65 78 20 62 65 69 6e 67 20  The index being 
2fb0c 75 73 65 64 20 66 6f 72 20 74 68 69 73 20 6c 6f  used for this lo
2fb0d 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72  op */.  int iCur
2fb0e 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 54 61 62 43   = pLevel->iTabC
2fb0f 75 72 3b 20 20 20 2f 2a 20 54 68 65 20 63 75 72  ur;   /* The cur
2fb10 73 6f 72 20 6f 66 20 74 68 65 20 74 61 62 6c 65  sor of the table
2fb11 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d 20   */.  WhereTerm 
2fb12 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20  *pTerm;         
2fb13 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20      /* A single 
2fb14 63 6f 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 20  constraint term 
2fb15 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20  */.  int j;     
2fb16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fb17 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
2fb18 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 42  er */.  int regB
2fb19 61 73 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ase;            
2fb1a 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72 65        /* Base re
2fb1b 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  gister */.  int 
2fb1c 6e 52 65 67 3b 20 20 20 20 20 20 20 20 20 20 20  nReg;           
2fb1d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
2fb1e 62 65 72 20 6f 66 20 72 65 67 69 73 74 65 72 73  ber of registers
2fb1f 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a   to allocate */.
2fb20 20 20 63 68 61 72 20 2a 7a 41 66 66 3b 20 20 20    char *zAff;   
2fb21 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fb22 2f 2a 20 41 66 66 69 6e 69 74 79 20 73 74 72 69  /* Affinity stri
2fb23 6e 67 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a  ng to return */.
2fb24 0a 20 20 2f 2a 20 54 68 69 73 20 6d 6f 64 75 6c  .  /* This modul
2fb25 65 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64  e is only called
2fb26 20 6f 6e 20 71 75 65 72 79 20 70 6c 61 6e 73 20   on query plans 
2fb27 74 68 61 74 20 75 73 65 20 61 6e 20 69 6e 64 65  that use an inde
2fb28 78 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  x. */.  assert( 
2fb29 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
2fb2a 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 4e 44  lags & WHERE_IND
2fb2b 45 58 45 44 20 29 3b 0a 20 20 70 49 64 78 20 3d  EXED );.  pIdx =
2fb2c 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75 2e   pLevel->plan.u.
2fb2d 70 49 64 78 3b 0a 0a 20 20 2f 2a 20 46 69 67 75  pIdx;..  /* Figu
2fb2e 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20  re out how many 
2fb2f 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 77 65 20  memory cells we 
2fb30 77 69 6c 6c 20 6e 65 65 64 20 74 68 65 6e 20 61  will need then a
2fb31 6c 6c 6f 63 61 74 65 20 74 68 65 6d 2e 0a 20 20  llocate them..  
2fb32 2a 2f 0a 20 20 72 65 67 42 61 73 65 20 3d 20 70  */.  regBase = p
2fb33 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20 31 3b  Parse->nMem + 1;
2fb34 0a 20 20 6e 52 65 67 20 3d 20 70 4c 65 76 65 6c  .  nReg = pLevel
2fb35 2d 3e 70 6c 61 6e 2e 6e 45 71 20 2b 20 6e 45 78  ->plan.nEq + nEx
2fb36 74 72 61 52 65 67 3b 0a 20 20 70 50 61 72 73 65  traReg;.  pParse
2fb37 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 52 65 67 3b 0a  ->nMem += nReg;.
2fb38 0a 20 20 7a 41 66 66 20 3d 20 73 71 6c 69 74 65  .  zAff = sqlite
2fb39 33 44 62 53 74 72 44 75 70 28 70 50 61 72 73 65  3DbStrDup(pParse
2fb3a 2d 3e 64 62 2c 20 73 71 6c 69 74 65 33 49 6e 64  ->db, sqlite3Ind
2fb3b 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 76 2c  exAffinityStr(v,
2fb3c 20 70 49 64 78 29 29 3b 0a 20 20 69 66 28 20 21   pIdx));.  if( !
2fb3d 7a 41 66 66 20 29 7b 0a 20 20 20 20 70 50 61 72  zAff ){.    pPar
2fb3e 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  se->db->mallocFa
2fb3f 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20  iled = 1;.  }.. 
2fb40 20 2f 2a 20 45 76 61 6c 75 61 74 65 20 74 68 65   /* Evaluate the
2fb41 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72   equality constr
2fb42 61 69 6e 74 73 0a 20 20 2a 2f 0a 20 20 61 73 73  aints.  */.  ass
2fb43 65 72 74 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75  ert( pIdx->nColu
2fb44 6d 6e 3e 3d 6e 45 71 20 29 3b 0a 20 20 66 6f 72  mn>=nEq );.  for
2fb45 28 6a 3d 30 3b 20 6a 3c 6e 45 71 3b 20 6a 2b 2b  (j=0; j<nEq; j++
2fb46 29 7b 0a 20 20 20 20 69 6e 74 20 72 31 3b 0a 20  ){.    int r1;. 
2fb47 20 20 20 69 6e 74 20 6b 20 3d 20 70 49 64 78 2d     int k = pIdx-
2fb48 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20 20  >aiColumn[j];.  
2fb49 20 20 70 54 65 72 6d 20 3d 20 66 69 6e 64 54 65    pTerm = findTe
2fb4a 72 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6b 2c  rm(pWC, iCur, k,
2fb4b 20 6e 6f 74 52 65 61 64 79 2c 20 70 4c 65 76 65   notReady, pLeve
2fb4c 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 2c  l->plan.wsFlags,
2fb4d 20 70 49 64 78 29 3b 0a 20 20 20 20 69 66 28 20   pIdx);.    if( 
2fb4e 4e 45 56 45 52 28 70 54 65 72 6d 3d 3d 30 29 20  NEVER(pTerm==0) 
2fb4f 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 61 73 73  ) break;.    ass
2fb50 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 77 74 46  ert( (pTerm->wtF
2fb51 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 44 45  lags & TERM_CODE
2fb52 44 29 3d 3d 30 20 29 3b 0a 20 20 20 20 72 31 20  D)==0 );.    r1 
2fb53 3d 20 63 6f 64 65 45 71 75 61 6c 69 74 79 54 65  = codeEqualityTe
2fb54 72 6d 28 70 50 61 72 73 65 2c 20 70 54 65 72 6d  rm(pParse, pTerm
2fb55 2c 20 70 4c 65 76 65 6c 2c 20 72 65 67 42 61 73  , pLevel, regBas
2fb56 65 2b 6a 29 3b 0a 20 20 20 20 69 66 28 20 72 31  e+j);.    if( r1
2fb57 21 3d 72 65 67 42 61 73 65 2b 6a 20 29 7b 0a 20  !=regBase+j ){. 
2fb58 20 20 20 20 20 69 66 28 20 6e 52 65 67 3d 3d 31       if( nReg==1
2fb59 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
2fb5a 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65  te3ReleaseTempRe
2fb5b 67 28 70 50 61 72 73 65 2c 20 72 65 67 42 61 73  g(pParse, regBas
2fb5c 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 67 42  e);.        regB
2fb5d 61 73 65 20 3d 20 72 31 3b 0a 20 20 20 20 20 20  ase = r1;.      
2fb5e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
2fb5f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2fb60 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 31  (v, OP_SCopy, r1
2fb61 2c 20 72 65 67 42 61 73 65 2b 6a 29 3b 0a 20 20  , regBase+j);.  
2fb62 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
2fb63 74 65 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d  testcase( pTerm-
2fb64 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f  >eOperator & WO_
2fb65 49 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 74 65  ISNULL );.    te
2fb66 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65  stcase( pTerm->e
2fb67 4f 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49 4e  Operator & WO_IN
2fb68 20 29 3b 0a 20 20 20 20 69 66 28 20 28 70 54 65   );.    if( (pTe
2fb69 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  rm->eOperator & 
2fb6a 28 57 4f 5f 49 53 4e 55 4c 4c 7c 57 4f 5f 49 4e  (WO_ISNULL|WO_IN
2fb6b 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  ))==0 ){.      s
2fb6c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2fb6d 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72  (v, OP_IsNull, r
2fb6e 65 67 42 61 73 65 2b 6a 2c 20 70 4c 65 76 65 6c  egBase+j, pLevel
2fb6f 2d 3e 61 64 64 72 42 72 6b 29 3b 0a 20 20 20 20  ->addrBrk);.    
2fb70 20 20 69 66 28 20 7a 41 66 66 20 0a 20 20 20 20    if( zAff .    
2fb71 20 20 20 26 26 20 73 71 6c 69 74 65 33 43 6f 6d     && sqlite3Com
2fb72 70 61 72 65 41 66 66 69 6e 69 74 79 28 70 54 65  pareAffinity(pTe
2fb73 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68  rm->pExpr->pRigh
2fb74 74 2c 20 7a 41 66 66 5b 6a 5d 29 3d 3d 53 51 4c  t, zAff[j])==SQL
2fb75 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 0a 20 20 20  ITE_AFF_NONE.   
2fb76 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 41     ){.        zA
2fb77 66 66 5b 6a 5d 20 3d 20 53 51 4c 49 54 45 5f 41  ff[j] = SQLITE_A
2fb78 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 7d  FF_NONE;.      }
2fb79 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 7a  .    }.  }.  *pz
2fb7a 41 66 66 20 3d 20 7a 41 66 66 3b 0a 20 20 72 65  Aff = zAff;.  re
2fb7b 74 75 72 6e 20 72 65 67 42 61 73 65 3b 0a 7d 0a  turn regBase;.}.
2fb7c 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
2fb7d 63 6f 64 65 20 66 6f 72 20 74 68 65 20 73 74 61  code for the sta
2fb7e 72 74 20 6f 66 20 74 68 65 20 69 4c 65 76 65 6c  rt of the iLevel
2fb7f 2d 74 68 20 6c 6f 6f 70 20 69 6e 20 74 68 65 20  -th loop in the 
2fb80 57 48 45 52 45 20 63 6c 61 75 73 65 0a 2a 2a 20  WHERE clause.** 
2fb81 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 64  implementation d
2fb82 65 73 63 72 69 62 65 64 20 62 79 20 70 57 49 6e  escribed by pWIn
2fb83 66 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 42 69  fo..*/.static Bi
2fb84 74 6d 61 73 6b 20 63 6f 64 65 4f 6e 65 4c 6f 6f  tmask codeOneLoo
2fb85 70 53 74 61 72 74 28 0a 20 20 57 68 65 72 65 49  pStart(.  WhereI
2fb86 6e 66 6f 20 2a 70 57 49 6e 66 6f 2c 20 20 20 2f  nfo *pWInfo,   /
2fb87 2a 20 43 6f 6d 70 6c 65 74 65 20 69 6e 66 6f 72  * Complete infor
2fb88 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
2fb89 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
2fb8a 0a 20 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20 20  .  int iLevel,  
2fb8b 20 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68          /* Which
2fb8c 20 6c 65 76 65 6c 20 6f 66 20 70 57 49 6e 66 6f   level of pWInfo
2fb8d 2d 3e 61 5b 5d 20 73 68 6f 75 6c 64 20 62 65 20  ->a[] should be 
2fb8e 63 6f 64 65 64 20 2a 2f 0a 20 20 75 31 36 20 77  coded */.  u16 w
2fb8f 63 74 72 6c 46 6c 61 67 73 2c 20 20 20 20 20 20  ctrlFlags,      
2fb90 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 57 48  /* One of the WH
2fb91 45 52 45 5f 2a 20 66 6c 61 67 73 20 64 65 66 69  ERE_* flags defi
2fb92 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74  ned in sqliteInt
2fb93 2e 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20  .h */.  Bitmask 
2fb94 6e 6f 74 52 65 61 64 79 20 20 20 20 20 2f 2a 20  notReady     /* 
2fb95 57 68 69 63 68 20 74 61 62 6c 65 73 20 61 72 65  Which tables are
2fb96 20 63 75 72 72 65 6e 74 6c 79 20 61 76 61 69 6c   currently avail
2fb97 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  able */.){.  int
2fb98 20 6a 2c 20 6b 3b 20 20 20 20 20 20 20 20 20 20   j, k;          
2fb99 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
2fb9a 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72  rs */.  int iCur
2fb9b 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
2fb9c 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  The VDBE cursor 
2fb9d 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 2a 2f  for the table */
2fb9e 0a 20 20 69 6e 74 20 61 64 64 72 4e 78 74 3b 20  .  int addrNxt; 
2fb9f 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65          /* Where
2fba0 20 74 6f 20 6a 75 6d 70 20 74 6f 20 63 6f 6e 74   to jump to cont
2fba1 69 6e 75 65 20 77 69 74 68 20 74 68 65 20 6e 65  inue with the ne
2fba2 78 74 20 49 4e 20 63 61 73 65 20 2a 2f 0a 20 20  xt IN case */.  
2fba3 69 6e 74 20 6f 6d 69 74 54 61 62 6c 65 3b 20 20  int omitTable;  
2fba4 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
2fba5 77 65 20 75 73 65 20 74 68 65 20 69 6e 64 65 78  we use the index
2fba6 20 6f 6e 6c 79 20 2a 2f 0a 20 20 69 6e 74 20 62   only */.  int b
2fba7 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 20 20  Rev;            
2fba8 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 6e 65  /* True if we ne
2fba9 65 64 20 74 6f 20 73 63 61 6e 20 69 6e 20 72 65  ed to scan in re
2fbaa 76 65 72 73 65 20 6f 72 64 65 72 20 2a 2f 0a 20  verse order */. 
2fbab 20 57 68 65 72 65 4c 65 76 65 6c 20 2a 70 4c 65   WhereLevel *pLe
2fbac 76 65 6c 3b 20 20 2f 2a 20 54 68 65 20 77 68 65  vel;  /* The whe
2fbad 72 65 20 6c 65 76 65 6c 20 74 6f 20 62 65 20 63  re level to be c
2fbae 6f 64 65 64 20 2a 2f 0a 20 20 57 68 65 72 65 43  oded */.  WhereC
2fbaf 6c 61 75 73 65 20 2a 70 57 43 3b 20 20 20 20 2f  lause *pWC;    /
2fbb0 2a 20 44 65 63 6f 6d 70 6f 73 69 74 69 6f 6e 20  * Decomposition 
2fbb1 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20 57 48  of the entire WH
2fbb2 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20  ERE clause */.  
2fbb3 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72 6d  WhereTerm *pTerm
2fbb4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2fbb5 2f 2a 20 41 20 57 48 45 52 45 20 63 6c 61 75 73  /* A WHERE claus
2fbb6 65 20 74 65 72 6d 20 2a 2f 0a 20 20 50 61 72 73  e term */.  Pars
2fbb7 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20  e *pParse;      
2fbb8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
2fbb9 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  arsing context *
2fbba 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20  /.  Vdbe *v;    
2fbbb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fbbc 20 20 20 20 2f 2a 20 54 68 65 20 70 72 65 70 61      /* The prepa
2fbbd 72 65 64 20 73 74 6d 74 20 75 6e 64 65 72 20 63  red stmt under c
2fbbe 6f 6e 73 74 72 75 63 74 69 6f 6e 73 20 2a 2f 0a  onstructions */.
2fbbf 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
2fbc0 5f 69 74 65 6d 20 2a 70 54 61 62 49 74 65 6d 3b  _item *pTabItem;
2fbc1 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65    /* FROM clause
2fbc2 20 74 65 72 6d 20 62 65 69 6e 67 20 63 6f 64 65   term being code
2fbc3 64 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 42  d */.  int addrB
2fbc4 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rk;             
2fbc5 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68         /* Jump h
2fbc6 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75 74  ere to break out
2fbc7 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a   of the loop */.
2fbc8 20 20 69 6e 74 20 61 64 64 72 43 6f 6e 74 3b 20    int addrCont; 
2fbc9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fbca 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
2fbcb 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20  o continue with 
2fbcc 6e 65 78 74 20 63 79 63 6c 65 20 2a 2f 0a 20 20  next cycle */.  
2fbcd 69 6e 74 20 69 52 6f 77 69 64 52 65 67 20 3d 20  int iRowidReg = 
2fbce 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 77  0;        /* Row
2fbcf 69 64 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  id is stored in 
2fbd0 74 68 69 73 20 72 65 67 69 73 74 65 72 2c 20 69  this register, i
2fbd1 66 20 6e 6f 74 20 7a 65 72 6f 20 2a 2f 0a 20 20  f not zero */.  
2fbd2 69 6e 74 20 69 52 65 6c 65 61 73 65 52 65 67 20  int iReleaseReg 
2fbd3 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 54 65 6d  = 0;      /* Tem
2fbd4 70 20 72 65 67 69 73 74 65 72 20 74 6f 20 66 72  p register to fr
2fbd5 65 65 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e  ee before return
2fbd6 69 6e 67 20 2a 2f 0a 0a 20 20 70 50 61 72 73 65  ing */..  pParse
2fbd7 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 50 61 72 73   = pWInfo->pPars
2fbd8 65 3b 0a 20 20 76 20 3d 20 70 50 61 72 73 65 2d  e;.  v = pParse-
2fbd9 3e 70 56 64 62 65 3b 0a 20 20 70 57 43 20 3d 20  >pVdbe;.  pWC = 
2fbda 70 57 49 6e 66 6f 2d 3e 70 57 43 3b 0a 20 20 70  pWInfo->pWC;.  p
2fbdb 4c 65 76 65 6c 20 3d 20 26 70 57 49 6e 66 6f 2d  Level = &pWInfo-
2fbdc 3e 61 5b 69 4c 65 76 65 6c 5d 3b 0a 20 20 70 54  >a[iLevel];.  pT
2fbdd 61 62 49 74 65 6d 20 3d 20 26 70 57 49 6e 66 6f  abItem = &pWInfo
2fbde 2d 3e 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c  ->pTabList->a[pL
2fbdf 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20  evel->iFrom];.  
2fbe0 69 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d 2d  iCur = pTabItem-
2fbe1 3e 69 43 75 72 73 6f 72 3b 0a 20 20 62 52 65 76  >iCursor;.  bRev
2fbe2 20 3d 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e   = (pLevel->plan
2fbe3 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
2fbe4 5f 52 45 56 45 52 53 45 29 21 3d 30 3b 0a 20 20  _REVERSE)!=0;.  
2fbe5 6f 6d 69 74 54 61 62 6c 65 20 3d 20 28 70 4c 65  omitTable = (pLe
2fbe6 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67  vel->plan.wsFlag
2fbe7 73 20 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e  s & WHERE_IDX_ON
2fbe8 4c 59 29 21 3d 30 20 0a 20 20 20 20 20 20 20 20  LY)!=0 .        
2fbe9 20 20 20 26 26 20 28 77 63 74 72 6c 46 6c 61 67     && (wctrlFlag
2fbea 73 20 26 20 57 48 45 52 45 5f 46 4f 52 43 45 5f  s & WHERE_FORCE_
2fbeb 54 41 42 4c 45 29 3d 3d 30 3b 0a 0a 20 20 2f 2a  TABLE)==0;..  /*
2fbec 20 43 72 65 61 74 65 20 6c 61 62 65 6c 73 20 66   Create labels f
2fbed 6f 72 20 74 68 65 20 22 62 72 65 61 6b 22 20 61  or the "break" a
2fbee 6e 64 20 22 63 6f 6e 74 69 6e 75 65 22 20 69 6e  nd "continue" in
2fbef 73 74 72 75 63 74 69 6f 6e 73 0a 20 20 2a 2a 20  structions.  ** 
2fbf0 66 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20  for the current 
2fbf1 6c 6f 6f 70 2e 20 20 4a 75 6d 70 20 74 6f 20 61  loop.  Jump to a
2fbf2 64 64 72 42 72 6b 20 74 6f 20 62 72 65 61 6b 20  ddrBrk to break 
2fbf3 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2e 0a 20  out of a loop.. 
2fbf4 20 2a 2a 20 4a 75 6d 70 20 74 6f 20 63 6f 6e 74   ** Jump to cont
2fbf5 20 74 6f 20 67 6f 20 69 6d 6d 65 64 69 61 74 65   to go immediate
2fbf6 6c 79 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69  ly to the next i
2fbf7 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a  teration of the.
2fbf8 20 20 2a 2a 20 6c 6f 6f 70 2e 0a 20 20 2a 2a 0a    ** loop..  **.
2fbf9 20 20 2a 2a 20 57 68 65 6e 20 74 68 65 72 65 20    ** When there 
2fbfa 69 73 20 61 6e 20 49 4e 20 6f 70 65 72 61 74 6f  is an IN operato
2fbfb 72 2c 20 77 65 20 61 6c 73 6f 20 68 61 76 65 20  r, we also have 
2fbfc 61 20 22 61 64 64 72 4e 78 74 22 20 6c 61 62 65  a "addrNxt" labe
2fbfd 6c 20 74 68 61 74 0a 20 20 2a 2a 20 6d 65 61 6e  l that.  ** mean
2fbfe 73 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69  s to continue wi
2fbff 74 68 20 74 68 65 20 6e 65 78 74 20 49 4e 20 76  th the next IN v
2fc00 61 6c 75 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e  alue combination
2fc01 2e 20 20 57 68 65 6e 0a 20 20 2a 2a 20 74 68 65  .  When.  ** the
2fc02 72 65 20 61 72 65 20 6e 6f 20 49 4e 20 6f 70 65  re are no IN ope
2fc03 72 61 74 6f 72 73 20 69 6e 20 74 68 65 20 63 6f  rators in the co
2fc04 6e 73 74 72 61 69 6e 74 73 2c 20 74 68 65 20 22  nstraints, the "
2fc05 61 64 64 72 4e 78 74 22 20 6c 61 62 65 6c 0a 20  addrNxt" label. 
2fc06 20 2a 2a 20 69 73 20 74 68 65 20 73 61 6d 65 20   ** is the same 
2fc07 61 73 20 22 61 64 64 72 42 72 6b 22 2e 0a 20 20  as "addrBrk"..  
2fc08 2a 2f 0a 20 20 61 64 64 72 42 72 6b 20 3d 20 70  */.  addrBrk = p
2fc09 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72 6b 20 3d  Level->addrBrk =
2fc0a 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74   pLevel->addrNxt
2fc0b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
2fc0c 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 61 64  keLabel(v);.  ad
2fc0d 64 72 43 6f 6e 74 20 3d 20 70 4c 65 76 65 6c 2d  drCont = pLevel-
2fc0e 3e 61 64 64 72 43 6f 6e 74 20 3d 20 73 71 6c 69  >addrCont = sqli
2fc0f 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
2fc10 28 76 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  (v);..  /* If th
2fc11 69 73 20 69 73 20 74 68 65 20 72 69 67 68 74 20  is is the right 
2fc12 74 61 62 6c 65 20 6f 66 20 61 20 4c 45 46 54 20  table of a LEFT 
2fc13 4f 55 54 45 52 20 4a 4f 49 4e 2c 20 61 6c 6c 6f  OUTER JOIN, allo
2fc14 63 61 74 65 20 61 6e 64 0a 20 20 2a 2a 20 69 6e  cate and.  ** in
2fc15 69 74 69 61 6c 69 7a 65 20 61 20 6d 65 6d 6f 72  itialize a memor
2fc16 79 20 63 65 6c 6c 20 74 68 61 74 20 72 65 63 6f  y cell that reco
2fc17 72 64 73 20 69 66 20 74 68 69 73 20 74 61 62 6c  rds if this tabl
2fc18 65 20 6d 61 74 63 68 65 73 20 61 6e 79 0a 20 20  e matches any.  
2fc19 2a 2a 20 72 6f 77 20 6f 66 20 74 68 65 20 6c 65  ** row of the le
2fc1a 66 74 20 74 61 62 6c 65 20 6f 66 20 74 68 65 20  ft table of the 
2fc1b 6a 6f 69 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  join..  */.  if(
2fc1c 20 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 3e 30   pLevel->iFrom>0
2fc1d 20 26 26 20 28 70 54 61 62 49 74 65 6d 5b 30 5d   && (pTabItem[0]
2fc1e 2e 6a 6f 69 6e 74 79 70 65 20 26 20 4a 54 5f 4c  .jointype & JT_L
2fc1f 45 46 54 29 21 3d 30 20 29 7b 0a 20 20 20 20 70  EFT)!=0 ){.    p
2fc20 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e  Level->iLeftJoin
2fc21 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
2fc22 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  m;.    sqlite3Vd
2fc23 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
2fc24 6e 74 65 67 65 72 2c 20 30 2c 20 70 4c 65 76 65  nteger, 0, pLeve
2fc25 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a 20  l->iLeftJoin);. 
2fc26 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
2fc27 76 2c 20 22 69 6e 69 74 20 4c 45 46 54 20 4a 4f  v, "init LEFT JO
2fc28 49 4e 20 6e 6f 2d 6d 61 74 63 68 20 66 6c 61 67  IN no-match flag
2fc29 22 29 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65  "));.  }..#ifnde
2fc2a 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
2fc2b 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28  RTUALTABLE.  if(
2fc2c 20 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e    (pLevel->plan.
2fc2d 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  wsFlags & WHERE_
2fc2e 56 49 52 54 55 41 4c 54 41 42 4c 45 29 21 3d 30  VIRTUALTABLE)!=0
2fc2f 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65 20   ){.    /* Case 
2fc30 30 3a 20 20 54 68 65 20 74 61 62 6c 65 20 69 73  0:  The table is
2fc31 20 61 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65   a virtual-table
2fc32 2e 20 20 55 73 65 20 74 68 65 20 56 46 69 6c 74  .  Use the VFilt
2fc33 65 72 20 61 6e 64 20 56 4e 65 78 74 0a 20 20 20  er and VNext.   
2fc34 20 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20   **          to 
2fc35 61 63 63 65 73 73 20 74 68 65 20 64 61 74 61 2e  access the data.
2fc36 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20  .    */.    int 
2fc37 69 52 65 67 3b 20 20 20 2f 2a 20 50 33 20 56 61  iReg;   /* P3 Va
2fc38 6c 75 65 20 66 6f 72 20 4f 50 5f 56 46 69 6c 74  lue for OP_VFilt
2fc39 65 72 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  er */.    sqlite
2fc3a 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 56  3_index_info *pV
2fc3b 74 61 62 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d  tabIdx = pLevel-
2fc3c 3e 70 6c 61 6e 2e 75 2e 70 56 74 61 62 49 64 78  >plan.u.pVtabIdx
2fc3d 3b 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6e 73 74  ;.    int nConst
2fc3e 72 61 69 6e 74 20 3d 20 70 56 74 61 62 49 64 78  raint = pVtabIdx
2fc3f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 20  ->nConstraint;. 
2fc40 20 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65     struct sqlite
2fc41 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
2fc42 6e 74 5f 75 73 61 67 65 20 2a 61 55 73 61 67 65  nt_usage *aUsage
2fc43 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   =.             
2fc44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fc45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fc46 20 20 20 70 56 74 61 62 49 64 78 2d 3e 61 43 6f     pVtabIdx->aCo
2fc47 6e 73 74 72 61 69 6e 74 55 73 61 67 65 3b 0a 20  nstraintUsage;. 
2fc48 20 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20     const struct 
2fc49 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f  sqlite3_index_co
2fc4a 6e 73 74 72 61 69 6e 74 20 2a 61 43 6f 6e 73 74  nstraint *aConst
2fc4b 72 61 69 6e 74 20 3d 0a 20 20 20 20 20 20 20 20  raint =.        
2fc4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fc4d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fc4e 20 20 20 20 20 20 20 20 70 56 74 61 62 49 64 78          pVtabIdx
2fc4f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 0a  ->aConstraint;..
2fc50 20 20 20 20 69 52 65 67 20 3d 20 73 71 6c 69 74      iReg = sqlit
2fc51 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70  e3GetTempRange(p
2fc52 50 61 72 73 65 2c 20 6e 43 6f 6e 73 74 72 61 69  Parse, nConstrai
2fc53 6e 74 2b 32 29 3b 0a 20 20 20 20 66 6f 72 28 6a  nt+2);.    for(j
2fc54 3d 31 3b 20 6a 3c 3d 6e 43 6f 6e 73 74 72 61 69  =1; j<=nConstrai
2fc55 6e 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  nt; j++){.      
2fc56 66 6f 72 28 6b 3d 30 3b 20 6b 3c 6e 43 6f 6e 73  for(k=0; k<nCons
2fc57 74 72 61 69 6e 74 3b 20 6b 2b 2b 29 7b 0a 20 20  traint; k++){.  
2fc58 20 20 20 20 20 20 69 66 28 20 61 55 73 61 67 65        if( aUsage
2fc59 5b 6b 5d 2e 61 72 67 76 49 6e 64 65 78 3d 3d 6a  [k].argvIndex==j
2fc5a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e   ){.          in
2fc5b 74 20 69 54 65 72 6d 20 3d 20 61 43 6f 6e 73 74  t iTerm = aConst
2fc5c 72 61 69 6e 74 5b 6b 5d 2e 69 54 65 72 6d 4f 66  raint[k].iTermOf
2fc5d 66 73 65 74 3b 0a 20 20 20 20 20 20 20 20 20 20  fset;.          
2fc5e 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
2fc5f 70 50 61 72 73 65 2c 20 70 57 43 2d 3e 61 5b 69  pParse, pWC->a[i
2fc60 54 65 72 6d 5d 2e 70 45 78 70 72 2d 3e 70 52 69  Term].pExpr->pRi
2fc61 67 68 74 2c 20 69 52 65 67 2b 6a 2b 31 29 3b 0a  ght, iReg+j+1);.
2fc62 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
2fc63 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2fc64 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d   }.      if( k==
2fc65 6e 43 6f 6e 73 74 72 61 69 6e 74 20 29 20 62 72  nConstraint ) br
2fc66 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  eak;.    }.    s
2fc67 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2fc68 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2fc69 70 56 74 61 62 49 64 78 2d 3e 69 64 78 4e 75 6d  pVtabIdx->idxNum
2fc6a 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 73 71 6c  , iReg);.    sql
2fc6b 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2fc6c 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 6a 2d  , OP_Integer, j-
2fc6d 31 2c 20 69 52 65 67 2b 31 29 3b 0a 20 20 20 20  1, iReg+1);.    
2fc6e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2fc6f 34 28 76 2c 20 4f 50 5f 56 46 69 6c 74 65 72 2c  4(v, OP_VFilter,
2fc70 20 69 43 75 72 2c 20 61 64 64 72 42 72 6b 2c 20   iCur, addrBrk, 
2fc71 69 52 65 67 2c 20 70 56 74 61 62 49 64 78 2d 3e  iReg, pVtabIdx->
2fc72 69 64 78 53 74 72 2c 0a 20 20 20 20 20 20 20 20  idxStr,.        
2fc73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 56                pV
2fc74 74 61 62 49 64 78 2d 3e 6e 65 65 64 54 6f 46 72  tabIdx->needToFr
2fc75 65 65 49 64 78 53 74 72 20 3f 20 50 34 5f 4d 50  eeIdxStr ? P4_MP
2fc76 52 49 4e 54 46 20 3a 20 50 34 5f 53 54 41 54 49  RINTF : P4_STATI
2fc77 43 29 3b 0a 20 20 20 20 70 56 74 61 62 49 64 78  C);.    pVtabIdx
2fc78 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53  ->needToFreeIdxS
2fc79 74 72 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28  tr = 0;.    for(
2fc7a 6a 3d 30 3b 20 6a 3c 6e 43 6f 6e 73 74 72 61 69  j=0; j<nConstrai
2fc7b 6e 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  nt; j++){.      
2fc7c 69 66 28 20 61 55 73 61 67 65 5b 6a 5d 2e 6f 6d  if( aUsage[j].om
2fc7d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  it ){.        in
2fc7e 74 20 69 54 65 72 6d 20 3d 20 61 43 6f 6e 73 74  t iTerm = aConst
2fc7f 72 61 69 6e 74 5b 6a 5d 2e 69 54 65 72 6d 4f 66  raint[j].iTermOf
2fc80 66 73 65 74 3b 0a 20 20 20 20 20 20 20 20 64 69  fset;.        di
2fc81 73 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c  sableTerm(pLevel
2fc82 2c 20 26 70 57 43 2d 3e 61 5b 69 54 65 72 6d 5d  , &pWC->a[iTerm]
2fc83 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
2fc84 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20  .    pLevel->op 
2fc85 3d 20 4f 50 5f 56 4e 65 78 74 3b 0a 20 20 20 20  = OP_VNext;.    
2fc86 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69 43 75  pLevel->p1 = iCu
2fc87 72 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70  r;.    pLevel->p
2fc88 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  2 = sqlite3VdbeC
2fc89 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20  urrentAddr(v);. 
2fc8a 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
2fc8b 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73  eTempRange(pPars
2fc8c 65 2c 20 69 52 65 67 2c 20 6e 43 6f 6e 73 74 72  e, iReg, nConstr
2fc8d 61 69 6e 74 2b 32 29 3b 0a 20 20 7d 65 6c 73 65  aint+2);.  }else
2fc8e 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
2fc8f 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
2fc90 42 4c 45 20 2a 2f 0a 0a 20 20 69 66 28 20 70 4c  BLE */..  if( pL
2fc91 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
2fc92 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57 49 44  gs & WHERE_ROWID
2fc93 5f 45 51 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61  _EQ ){.    /* Ca
2fc94 73 65 20 31 3a 20 20 57 65 20 63 61 6e 20 64 69  se 1:  We can di
2fc95 72 65 63 74 6c 79 20 72 65 66 65 72 65 6e 63 65  rectly reference
2fc96 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 75 73   a single row us
2fc97 69 6e 67 20 61 6e 0a 20 20 20 20 2a 2a 20 20 20  ing an.    **   
2fc98 20 20 20 20 20 20 20 65 71 75 61 6c 69 74 79 20         equality 
2fc99 63 6f 6d 70 61 72 69 73 6f 6e 20 61 67 61 69 6e  comparison again
2fc9a 73 74 20 74 68 65 20 52 4f 57 49 44 20 66 69 65  st the ROWID fie
2fc9b 6c 64 2e 20 20 4f 72 0a 20 20 20 20 2a 2a 20 20  ld.  Or.    **  
2fc9c 20 20 20 20 20 20 20 20 77 65 20 72 65 66 65 72          we refer
2fc9d 65 6e 63 65 20 6d 75 6c 74 69 70 6c 65 20 72 6f  ence multiple ro
2fc9e 77 73 20 75 73 69 6e 67 20 61 20 22 72 6f 77 69  ws using a "rowi
2fc9f 64 20 49 4e 20 28 2e 2e 2e 29 22 0a 20 20 20 20  d IN (...)".    
2fca0 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73  **          cons
2fca1 74 72 75 63 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  truct..    */.  
2fca2 20 20 69 52 65 6c 65 61 73 65 52 65 67 20 3d 20    iReleaseReg = 
2fca3 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
2fca4 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 70  g(pParse);.    p
2fca5 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28  Term = findTerm(
2fca6 70 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e  pWC, iCur, -1, n
2fca7 6f 74 52 65 61 64 79 2c 20 57 4f 5f 45 51 7c 57  otReady, WO_EQ|W
2fca8 4f 5f 49 4e 2c 20 30 29 3b 0a 20 20 20 20 61 73  O_IN, 0);.    as
2fca9 73 65 72 74 28 20 70 54 65 72 6d 21 3d 30 20 29  sert( pTerm!=0 )
2fcaa 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  ;.    assert( pT
2fcab 65 72 6d 2d 3e 70 45 78 70 72 21 3d 30 20 29 3b  erm->pExpr!=0 );
2fcac 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65  .    assert( pTe
2fcad 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d  rm->leftCursor==
2fcae 69 43 75 72 20 29 3b 0a 20 20 20 20 61 73 73 65  iCur );.    asse
2fcaf 72 74 28 20 6f 6d 69 74 54 61 62 6c 65 3d 3d 30  rt( omitTable==0
2fcb0 20 29 3b 0a 20 20 20 20 69 52 6f 77 69 64 52 65   );.    iRowidRe
2fcb1 67 20 3d 20 63 6f 64 65 45 71 75 61 6c 69 74 79  g = codeEquality
2fcb2 54 65 72 6d 28 70 50 61 72 73 65 2c 20 70 54 65  Term(pParse, pTe
2fcb3 72 6d 2c 20 70 4c 65 76 65 6c 2c 20 69 52 65 6c  rm, pLevel, iRel
2fcb4 65 61 73 65 52 65 67 29 3b 0a 20 20 20 20 61 64  easeReg);.    ad
2fcb5 64 72 4e 78 74 20 3d 20 70 4c 65 76 65 6c 2d 3e  drNxt = pLevel->
2fcb6 61 64 64 72 4e 78 74 3b 0a 20 20 20 20 73 71 6c  addrNxt;.    sql
2fcb7 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2fcb8 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20  , OP_MustBeInt, 
2fcb9 69 52 6f 77 69 64 52 65 67 2c 20 61 64 64 72 4e  iRowidReg, addrN
2fcba 78 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  xt);.    sqlite3
2fcbb 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
2fcbc 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69 43 75 72  _NotExists, iCur
2fcbd 2c 20 61 64 64 72 4e 78 74 2c 20 69 52 6f 77 69  , addrNxt, iRowi
2fcbe 64 52 65 67 29 3b 0a 20 20 20 20 73 71 6c 69 74  dReg);.    sqlit
2fcbf 65 33 45 78 70 72 43 61 63 68 65 53 74 6f 72 65  e3ExprCacheStore
2fcc0 28 70 50 61 72 73 65 2c 20 69 43 75 72 2c 20 2d  (pParse, iCur, -
2fcc1 31 2c 20 69 52 6f 77 69 64 52 65 67 29 3b 0a 20  1, iRowidReg);. 
2fcc2 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
2fcc3 76 2c 20 22 70 6b 22 29 29 3b 0a 20 20 20 20 70  v, "pk"));.    p
2fcc4 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 4e  Level->op = OP_N
2fcc5 6f 6f 70 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  oop;.  }else if(
2fcc6 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
2fcc7 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 52 4f  Flags & WHERE_RO
2fcc8 57 49 44 5f 52 41 4e 47 45 20 29 7b 0a 20 20 20  WID_RANGE ){.   
2fcc9 20 2f 2a 20 43 61 73 65 20 32 3a 20 20 57 65 20   /* Case 2:  We 
2fcca 68 61 76 65 20 61 6e 20 69 6e 65 71 75 61 6c 69  have an inequali
2fccb 74 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61 67  ty comparison ag
2fccc 61 69 6e 73 74 20 74 68 65 20 52 4f 57 49 44 20  ainst the ROWID 
2fccd 66 69 65 6c 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  field..    */.  
2fcce 20 20 69 6e 74 20 74 65 73 74 4f 70 20 3d 20 4f    int testOp = O
2fccf 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 69 6e 74 20  P_Noop;.    int 
2fcd0 73 74 61 72 74 3b 0a 20 20 20 20 69 6e 74 20 6d  start;.    int m
2fcd1 65 6d 45 6e 64 56 61 6c 75 65 20 3d 20 30 3b 0a  emEndValue = 0;.
2fcd2 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70      WhereTerm *p
2fcd3 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b 0a 0a 20  Start, *pEnd;.. 
2fcd4 20 20 20 61 73 73 65 72 74 28 20 6f 6d 69 74 54     assert( omitT
2fcd5 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70  able==0 );.    p
2fcd6 53 74 61 72 74 20 3d 20 66 69 6e 64 54 65 72 6d  Start = findTerm
2fcd7 28 70 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20  (pWC, iCur, -1, 
2fcd8 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 47 54 7c  notReady, WO_GT|
2fcd9 57 4f 5f 47 45 2c 20 30 29 3b 0a 20 20 20 20 70  WO_GE, 0);.    p
2fcda 45 6e 64 20 3d 20 66 69 6e 64 54 65 72 6d 28 70  End = findTerm(p
2fcdb 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f  WC, iCur, -1, no
2fcdc 74 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f  tReady, WO_LT|WO
2fcdd 5f 4c 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  _LE, 0);.    if(
2fcde 20 62 52 65 76 20 29 7b 0a 20 20 20 20 20 20 70   bRev ){.      p
2fcdf 54 65 72 6d 20 3d 20 70 53 74 61 72 74 3b 0a 20  Term = pStart;. 
2fce0 20 20 20 20 20 70 53 74 61 72 74 20 3d 20 70 45       pStart = pE
2fce1 6e 64 3b 0a 20 20 20 20 20 20 70 45 6e 64 20 3d  nd;.      pEnd =
2fce2 20 70 54 65 72 6d 3b 0a 20 20 20 20 7d 0a 20 20   pTerm;.    }.  
2fce3 20 20 69 66 28 20 70 53 74 61 72 74 20 29 7b 0a    if( pStart ){.
2fce4 20 20 20 20 20 20 45 78 70 72 20 2a 70 58 3b 20        Expr *pX; 
2fce5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2fce6 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  he expression th
2fce7 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 73  at defines the s
2fce8 74 61 72 74 20 62 6f 75 6e 64 20 2a 2f 0a 20 20  tart bound */.  
2fce9 20 20 20 20 69 6e 74 20 72 31 2c 20 72 54 65 6d      int r1, rTem
2fcea 70 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 67  p;        /* Reg
2fceb 69 73 74 65 72 73 20 66 6f 72 20 68 6f 6c 64 69  isters for holdi
2fcec 6e 67 20 74 68 65 20 73 74 61 72 74 20 62 6f 75  ng the start bou
2fced 6e 64 61 72 79 20 2a 2f 0a 0a 20 20 20 20 20 20  ndary */..      
2fcee 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  /* The following
2fcef 20 63 6f 6e 73 74 61 6e 74 20 6d 61 70 73 20 54   constant maps T
2fcf0 4b 5f 78 78 20 63 6f 64 65 73 20 69 6e 74 6f 20  K_xx codes into 
2fcf1 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 0a 20  corresponding . 
2fcf2 20 20 20 20 20 2a 2a 20 73 65 65 6b 20 6f 70 63       ** seek opc
2fcf3 6f 64 65 73 2e 20 20 49 74 20 64 65 70 65 6e 64  odes.  It depend
2fcf4 73 20 6f 6e 20 61 20 70 61 72 74 69 63 75 6c 61  s on a particula
2fcf5 72 20 6f 72 64 65 72 69 6e 67 20 6f 66 20 54 4b  r ordering of TK
2fcf6 5f 78 78 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  _xx.      */.   
2fcf7 20 20 20 63 6f 6e 73 74 20 75 38 20 61 4d 6f 76     const u8 aMov
2fcf8 65 4f 70 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20  eOp[] = {.      
2fcf9 20 20 20 20 20 2f 2a 20 54 4b 5f 47 54 20 2a 2f       /* TK_GT */
2fcfa 20 20 4f 50 5f 53 65 65 6b 47 74 2c 0a 20 20 20    OP_SeekGt,.   
2fcfb 20 20 20 20 20 20 20 20 2f 2a 20 54 4b 5f 4c 45          /* TK_LE
2fcfc 20 2a 2f 20 20 4f 50 5f 53 65 65 6b 4c 65 2c 0a   */  OP_SeekLe,.
2fcfd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 4b             /* TK
2fcfe 5f 4c 54 20 2a 2f 20 20 4f 50 5f 53 65 65 6b 4c  _LT */  OP_SeekL
2fcff 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t,.           /*
2fd00 20 54 4b 5f 47 45 20 2a 2f 20 20 4f 50 5f 53 65   TK_GE */  OP_Se
2fd01 65 6b 47 65 0a 20 20 20 20 20 20 7d 3b 0a 20 20  ekGe.      };.  
2fd02 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c      assert( TK_L
2fd03 45 3d 3d 54 4b 5f 47 54 2b 31 20 29 3b 20 20 20  E==TK_GT+1 );   
2fd04 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20     /* Make sure 
2fd05 74 68 65 20 6f 72 64 65 72 69 6e 67 2e 2e 20 2a  the ordering.. *
2fd06 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  /.      assert( 
2fd07 54 4b 5f 4c 54 3d 3d 54 4b 5f 47 54 2b 32 20 29  TK_LT==TK_GT+2 )
2fd08 3b 20 20 20 20 20 20 2f 2a 20 20 2e 2e 2e 20 6f  ;      /*  ... o
2fd09 66 20 74 68 65 20 54 4b 5f 78 78 20 76 61 6c 75  f the TK_xx valu
2fd0a 65 73 2e 2e 2e 20 2a 2f 0a 20 20 20 20 20 20 61  es... */.      a
2fd0b 73 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 54 4b  ssert( TK_GE==TK
2fd0c 5f 47 54 2b 33 20 29 3b 20 20 20 20 20 20 2f 2a  _GT+3 );      /*
2fd0d 20 20 2e 2e 2e 20 69 73 20 63 6f 72 72 65 63 63    ... is correcc
2fd0e 74 2e 20 2a 2f 0a 0a 20 20 20 20 20 20 70 58 20  t. */..      pX 
2fd0f 3d 20 70 53 74 61 72 74 2d 3e 70 45 78 70 72 3b  = pStart->pExpr;
2fd10 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2fd11 58 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73  X!=0 );.      as
2fd12 73 65 72 74 28 20 70 53 74 61 72 74 2d 3e 6c 65  sert( pStart->le
2fd13 66 74 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 29  ftCursor==iCur )
2fd14 3b 0a 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c  ;.      r1 = sql
2fd15 69 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70  ite3ExprCodeTemp
2fd16 28 70 50 61 72 73 65 2c 20 70 58 2d 3e 70 52 69  (pParse, pX->pRi
2fd17 67 68 74 2c 20 26 72 54 65 6d 70 29 3b 0a 20 20  ght, &rTemp);.  
2fd18 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2fd19 64 64 4f 70 33 28 76 2c 20 61 4d 6f 76 65 4f 70  ddOp3(v, aMoveOp
2fd1a 5b 70 58 2d 3e 6f 70 2d 54 4b 5f 47 54 5d 2c 20  [pX->op-TK_GT], 
2fd1b 69 43 75 72 2c 20 61 64 64 72 42 72 6b 2c 20 72  iCur, addrBrk, r
2fd1c 31 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f  1);.      VdbeCo
2fd1d 6d 6d 65 6e 74 28 28 76 2c 20 22 70 6b 22 29 29  mment((v, "pk"))
2fd1e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
2fd1f 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79  xprCacheAffinity
2fd20 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  Change(pParse, r
2fd21 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  1, 1);.      sql
2fd22 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
2fd23 65 67 28 70 50 61 72 73 65 2c 20 72 54 65 6d 70  eg(pParse, rTemp
2fd24 29 3b 0a 20 20 20 20 20 20 64 69 73 61 62 6c 65  );.      disable
2fd25 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 53 74  Term(pLevel, pSt
2fd26 61 72 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  art);.    }else{
2fd27 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2fd28 62 65 41 64 64 4f 70 32 28 76 2c 20 62 52 65 76  beAddOp2(v, bRev
2fd29 20 3f 20 4f 50 5f 4c 61 73 74 20 3a 20 4f 50 5f   ? OP_Last : OP_
2fd2a 52 65 77 69 6e 64 2c 20 69 43 75 72 2c 20 61 64  Rewind, iCur, ad
2fd2b 64 72 42 72 6b 29 3b 0a 20 20 20 20 7d 0a 20 20  drBrk);.    }.  
2fd2c 20 20 69 66 28 20 70 45 6e 64 20 29 7b 0a 20 20    if( pEnd ){.  
2fd2d 20 20 20 20 45 78 70 72 20 2a 70 58 3b 0a 20 20      Expr *pX;.  
2fd2e 20 20 20 20 70 58 20 3d 20 70 45 6e 64 2d 3e 70      pX = pEnd->p
2fd2f 45 78 70 72 3b 0a 20 20 20 20 20 20 61 73 73 65  Expr;.      asse
2fd30 72 74 28 20 70 58 21 3d 30 20 29 3b 0a 20 20 20  rt( pX!=0 );.   
2fd31 20 20 20 61 73 73 65 72 74 28 20 70 45 6e 64 2d     assert( pEnd-
2fd32 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75  >leftCursor==iCu
2fd33 72 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d 45 6e  r );.      memEn
2fd34 64 56 61 6c 75 65 20 3d 20 2b 2b 70 50 61 72 73  dValue = ++pPars
2fd35 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 73  e->nMem;.      s
2fd36 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
2fd37 50 61 72 73 65 2c 20 70 58 2d 3e 70 52 69 67 68  Parse, pX->pRigh
2fd38 74 2c 20 6d 65 6d 45 6e 64 56 61 6c 75 65 29 3b  t, memEndValue);
2fd39 0a 20 20 20 20 20 20 69 66 28 20 70 58 2d 3e 6f  .      if( pX->o
2fd3a 70 3d 3d 54 4b 5f 4c 54 20 7c 7c 20 70 58 2d 3e  p==TK_LT || pX->
2fd3b 6f 70 3d 3d 54 4b 5f 47 54 20 29 7b 0a 20 20 20  op==TK_GT ){.   
2fd3c 20 20 20 20 20 74 65 73 74 4f 70 20 3d 20 62 52       testOp = bR
2fd3d 65 76 20 3f 20 4f 50 5f 4c 65 20 3a 20 4f 50 5f  ev ? OP_Le : OP_
2fd3e 47 65 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  Ge;.      }else{
2fd3f 0a 20 20 20 20 20 20 20 20 74 65 73 74 4f 70 20  .        testOp 
2fd40 3d 20 62 52 65 76 20 3f 20 4f 50 5f 4c 74 20 3a  = bRev ? OP_Lt :
2fd41 20 4f 50 5f 47 74 3b 0a 20 20 20 20 20 20 7d 0a   OP_Gt;.      }.
2fd42 20 20 20 20 20 20 64 69 73 61 62 6c 65 54 65 72        disableTer
2fd43 6d 28 70 4c 65 76 65 6c 2c 20 70 45 6e 64 29 3b  m(pLevel, pEnd);
2fd44 0a 20 20 20 20 7d 0a 20 20 20 20 73 74 61 72 74  .    }.    start
2fd45 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
2fd46 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20  rrentAddr(v);.  
2fd47 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 62    pLevel->op = b
2fd48 52 65 76 20 3f 20 4f 50 5f 50 72 65 76 20 3a 20  Rev ? OP_Prev : 
2fd49 4f 50 5f 4e 65 78 74 3b 0a 20 20 20 20 70 4c 65  OP_Next;.    pLe
2fd4a 76 65 6c 2d 3e 70 31 20 3d 20 69 43 75 72 3b 0a  vel->p1 = iCur;.
2fd4b 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d      pLevel->p2 =
2fd4c 20 73 74 61 72 74 3b 0a 20 20 20 20 70 4c 65 76   start;.    pLev
2fd4d 65 6c 2d 3e 70 35 20 3d 20 28 70 53 74 61 72 74  el->p5 = (pStart
2fd4e 3d 3d 30 20 26 26 20 70 45 6e 64 3d 3d 30 29 20  ==0 && pEnd==0) 
2fd4f 3f 31 3a 30 3b 0a 20 20 20 20 69 66 28 20 74 65  ?1:0;.    if( te
2fd50 73 74 4f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b  stOp!=OP_Noop ){
2fd51 0a 20 20 20 20 20 20 69 52 6f 77 69 64 52 65 67  .      iRowidReg
2fd52 20 3d 20 69 52 65 6c 65 61 73 65 52 65 67 20 3d   = iReleaseReg =
2fd53 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
2fd54 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
2fd55 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2fd56 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c  Op2(v, OP_Rowid,
2fd57 20 69 43 75 72 2c 20 69 52 6f 77 69 64 52 65 67   iCur, iRowidReg
2fd58 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2fd59 45 78 70 72 43 61 63 68 65 53 74 6f 72 65 28 70  ExprCacheStore(p
2fd5a 50 61 72 73 65 2c 20 69 43 75 72 2c 20 2d 31 2c  Parse, iCur, -1,
2fd5b 20 69 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20   iRowidReg);.   
2fd5c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2fd5d 64 4f 70 33 28 76 2c 20 74 65 73 74 4f 70 2c 20  dOp3(v, testOp, 
2fd5e 6d 65 6d 45 6e 64 56 61 6c 75 65 2c 20 61 64 64  memEndValue, add
2fd5f 72 42 72 6b 2c 20 69 52 6f 77 69 64 52 65 67 29  rBrk, iRowidReg)
2fd60 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2fd61 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 53  dbeChangeP5(v, S
2fd62 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49  QLITE_AFF_NUMERI
2fd63 43 20 7c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  C | SQLITE_JUMPI
2fd64 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 7d 0a 20 20  FNULL);.    }.  
2fd65 7d 65 6c 73 65 20 69 66 28 20 70 4c 65 76 65 6c  }else if( pLevel
2fd66 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
2fd67 20 28 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52   (WHERE_COLUMN_R
2fd68 41 4e 47 45 7c 57 48 45 52 45 5f 43 4f 4c 55 4d  ANGE|WHERE_COLUM
2fd69 4e 5f 45 51 29 20 29 7b 0a 20 20 20 20 2f 2a 20  N_EQ) ){.    /* 
2fd6a 43 61 73 65 20 33 3a 20 41 20 73 63 61 6e 20 75  Case 3: A scan u
2fd6b 73 69 6e 67 20 61 6e 20 69 6e 64 65 78 2e 0a 20  sing an index.. 
2fd6c 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
2fd6d 20 20 20 20 20 54 68 65 20 57 48 45 52 45 20 63       The WHERE c
2fd6e 6c 61 75 73 65 20 6d 61 79 20 63 6f 6e 74 61 69  lause may contai
2fd6f 6e 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 65  n zero or more e
2fd70 71 75 61 6c 69 74 79 20 0a 20 20 20 20 2a 2a 20  quality .    ** 
2fd71 20 20 20 20 20 20 20 20 74 65 72 6d 73 20 28 22          terms ("
2fd72 3d 3d 22 20 6f 72 20 22 49 4e 22 20 6f 70 65 72  ==" or "IN" oper
2fd73 61 74 6f 72 73 29 20 74 68 61 74 20 72 65 66 65  ators) that refe
2fd74 72 20 74 6f 20 74 68 65 20 4e 0a 20 20 20 20 2a  r to the N.    *
2fd75 2a 20 20 20 20 20 20 20 20 20 6c 65 66 74 2d 6d  *         left-m
2fd76 6f 73 74 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74  ost columns of t
2fd77 68 65 20 69 6e 64 65 78 2e 20 49 74 20 6d 61 79  he index. It may
2fd78 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 0a 20 20   also contain.  
2fd79 20 20 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 65    **         ine
2fd7a 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
2fd7b 6e 74 73 20 28 3e 2c 20 3c 2c 20 3e 3d 20 6f 72  nts (>, <, >= or
2fd7c 20 3c 3d 29 20 6f 6e 20 74 68 65 20 69 6e 64 65   <=) on the inde
2fd7d 78 65 64 0a 20 20 20 20 2a 2a 20 20 20 20 20 20  xed.    **      
2fd7e 20 20 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69     column that i
2fd7f 6d 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f  mmediately follo
2fd80 77 73 20 74 68 65 20 4e 20 65 71 75 61 6c 69 74  ws the N equalit
2fd81 69 65 73 2e 20 4f 6e 6c 79 20 0a 20 20 20 20 2a  ies. Only .    *
2fd82 2a 20 20 20 20 20 20 20 20 20 74 68 65 20 72 69  *         the ri
2fd83 67 68 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20  ght-most column 
2fd84 63 61 6e 20 62 65 20 61 6e 20 69 6e 65 71 75 61  can be an inequa
2fd85 6c 69 74 79 20 2d 20 74 68 65 20 72 65 73 74 20  lity - the rest 
2fd86 6d 75 73 74 0a 20 20 20 20 2a 2a 20 20 20 20 20  must.    **     
2fd87 20 20 20 20 75 73 65 20 74 68 65 20 22 3d 3d 22      use the "=="
2fd88 20 61 6e 64 20 22 49 4e 22 20 6f 70 65 72 61 74   and "IN" operat
2fd89 6f 72 73 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  ors. For example
2fd8a 2c 20 69 66 20 74 68 65 20 0a 20 20 20 20 2a 2a  , if the .    **
2fd8b 20 20 20 20 20 20 20 20 20 69 6e 64 65 78 20 69           index i
2fd8c 73 20 6f 6e 20 28 78 2c 79 2c 7a 29 2c 20 74 68  s on (x,y,z), th
2fd8d 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  en the following
2fd8e 20 63 6c 61 75 73 65 73 20 61 72 65 20 61 6c 6c   clauses are all
2fd8f 20 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20   .    **        
2fd90 20 6f 70 74 69 6d 69 7a 65 64 3a 0a 20 20 20 20   optimized:.    
2fd91 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  **.    **       
2fd92 20 20 20 20 20 78 3d 35 0a 20 20 20 20 2a 2a 20       x=5.    ** 
2fd93 20 20 20 20 20 20 20 20 20 20 20 78 3d 35 20 41             x=5 A
2fd94 4e 44 20 79 3d 31 30 0a 20 20 20 20 2a 2a 20 20  ND y=10.    **  
2fd95 20 20 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e            x=5 AN
2fd96 44 20 79 3c 31 30 0a 20 20 20 20 2a 2a 20 20 20  D y<10.    **   
2fd97 20 20 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44           x=5 AND
2fd98 20 79 3e 35 20 41 4e 44 20 79 3c 31 30 0a 20 20   y>5 AND y<10.  
2fd99 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20    **            
2fd9a 78 3d 35 20 41 4e 44 20 79 3d 35 20 41 4e 44 20  x=5 AND y=5 AND 
2fd9b 7a 3c 3d 31 30 0a 20 20 20 20 2a 2a 0a 20 20 20  z<=10.    **.   
2fd9c 20 2a 2a 20 20 20 20 20 20 20 20 20 54 68 65 20   **         The 
2fd9d 7a 3c 31 30 20 74 65 72 6d 20 6f 66 20 74 68 65  z<10 term of the
2fd9e 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6e 6e 6f   following canno
2fd9f 74 20 62 65 20 75 73 65 64 2c 20 6f 6e 6c 79 0a  t be used, only.
2fda0 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 74      **         t
2fda1 68 65 20 78 3d 35 20 74 65 72 6d 3a 0a 20 20 20  he x=5 term:.   
2fda2 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   **.    **      
2fda3 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20 7a 3c        x=5 AND z<
2fda4 31 30 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  10.    **.    **
2fda5 20 20 20 20 20 20 20 20 20 4e 20 6d 61 79 20 62           N may b
2fda6 65 20 7a 65 72 6f 20 69 66 20 74 68 65 72 65 20  e zero if there 
2fda7 61 72 65 20 69 6e 65 71 75 61 6c 69 74 79 20 63  are inequality c
2fda8 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 20 20 20 20  onstraints..    
2fda9 2a 2a 20 20 20 20 20 20 20 20 20 49 66 20 74 68  **         If th
2fdaa 65 72 65 20 61 72 65 20 6e 6f 20 69 6e 65 71 75  ere are no inequ
2fdab 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
2fdac 73 2c 20 74 68 65 6e 20 4e 20 69 73 20 61 74 0a  s, then N is at.
2fdad 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 6c      **         l
2fdae 65 61 73 74 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a  east one..    **
2fdaf 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
2fdb0 54 68 69 73 20 63 61 73 65 20 69 73 20 61 6c 73  This case is als
2fdb1 6f 20 75 73 65 64 20 77 68 65 6e 20 74 68 65 72  o used when ther
2fdb2 65 20 61 72 65 20 6e 6f 20 57 48 45 52 45 20 63  e are no WHERE c
2fdb3 6c 61 75 73 65 0a 20 20 20 20 2a 2a 20 20 20 20  lause.    **    
2fdb4 20 20 20 20 20 63 6f 6e 73 74 72 61 69 6e 74 73       constraints
2fdb5 20 62 75 74 20 61 6e 20 69 6e 64 65 78 20 69 73   but an index is
2fdb6 20 73 65 6c 65 63 74 65 64 20 61 6e 79 77 61 79   selected anyway
2fdb7 2c 20 69 6e 20 6f 72 64 65 72 0a 20 20 20 20 2a  , in order.    *
2fdb8 2a 20 20 20 20 20 20 20 20 20 74 6f 20 66 6f 72  *         to for
2fdb9 63 65 20 74 68 65 20 6f 75 74 70 75 74 20 6f 72  ce the output or
2fdba 64 65 72 20 74 6f 20 63 6f 6e 66 6f 72 6d 20 74  der to conform t
2fdbb 6f 20 61 6e 20 4f 52 44 45 52 20 42 59 2e 0a 20  o an ORDER BY.. 
2fdbc 20 20 20 2a 2f 20 20 0a 20 20 20 20 69 6e 74 20     */  .    int 
2fdbd 61 53 74 61 72 74 4f 70 5b 5d 20 3d 20 7b 0a 20  aStartOp[] = {. 
2fdbe 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20 30 2c       0,.      0,
2fdbf 0a 20 20 20 20 20 20 4f 50 5f 52 65 77 69 6e 64  .      OP_Rewind
2fdc0 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32  ,           /* 2
2fdc1 3a 20 28 21 73 74 61 72 74 5f 63 6f 6e 73 74 72  : (!start_constr
2fdc2 61 69 6e 74 73 20 26 26 20 73 74 61 72 74 45 71  aints && startEq
2fdc3 20 26 26 20 20 21 62 52 65 76 29 20 2a 2f 0a 20   &&  !bRev) */. 
2fdc4 20 20 20 20 20 4f 50 5f 4c 61 73 74 2c 20 20 20       OP_Last,   
2fdc5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 33 3a 20            /* 3: 
2fdc6 28 21 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69  (!start_constrai
2fdc7 6e 74 73 20 26 26 20 73 74 61 72 74 45 71 20 26  nts && startEq &
2fdc8 26 20 20 20 62 52 65 76 29 20 2a 2f 0a 20 20 20  &   bRev) */.   
2fdc9 20 20 20 4f 50 5f 53 65 65 6b 47 74 2c 20 20 20     OP_SeekGt,   
2fdca 20 20 20 20 20 20 20 20 2f 2a 20 34 3a 20 28 73          /* 4: (s
2fdcb 74 61 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73  tart_constraints
2fdcc 20 20 26 26 20 21 73 74 61 72 74 45 71 20 26 26    && !startEq &&
2fdcd 20 21 62 52 65 76 29 20 2a 2f 0a 20 20 20 20 20   !bRev) */.     
2fdce 20 4f 50 5f 53 65 65 6b 4c 74 2c 20 20 20 20 20   OP_SeekLt,     
2fdcf 20 20 20 20 20 20 2f 2a 20 35 3a 20 28 73 74 61        /* 5: (sta
2fdd0 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 20  rt_constraints  
2fdd1 26 26 20 21 73 74 61 72 74 45 71 20 26 26 20 20  && !startEq &&  
2fdd2 62 52 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f  bRev) */.      O
2fdd3 50 5f 53 65 65 6b 47 65 2c 20 20 20 20 20 20 20  P_SeekGe,       
2fdd4 20 20 20 20 2f 2a 20 36 3a 20 28 73 74 61 72 74      /* 6: (start
2fdd5 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26  _constraints  &&
2fdd6 20 20 73 74 61 72 74 45 71 20 26 26 20 21 62 52    startEq && !bR
2fdd7 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f  ev) */.      OP_
2fdd8 53 65 65 6b 4c 65 20 20 20 20 20 20 20 20 20 20  SeekLe          
2fdd9 20 20 2f 2a 20 37 3a 20 28 73 74 61 72 74 5f 63    /* 7: (start_c
2fdda 6f 6e 73 74 72 61 69 6e 74 73 20 20 26 26 20 20  onstraints  &&  
2fddb 73 74 61 72 74 45 71 20 26 26 20 20 62 52 65 76  startEq &&  bRev
2fddc 29 20 2a 2f 0a 20 20 20 20 7d 3b 0a 20 20 20 20  ) */.    };.    
2fddd 69 6e 74 20 61 45 6e 64 4f 70 5b 5d 20 3d 20 7b  int aEndOp[] = {
2fdde 0a 20 20 20 20 20 20 4f 50 5f 4e 6f 6f 70 2c 20  .      OP_Noop, 
2fddf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30              /* 0
2fde0 3a 20 28 21 65 6e 64 5f 63 6f 6e 73 74 72 61 69  : (!end_constrai
2fde1 6e 74 73 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50  nts) */.      OP
2fde2 5f 49 64 78 47 45 2c 20 20 20 20 20 20 20 20 20  _IdxGE,         
2fde3 20 20 20 2f 2a 20 31 3a 20 28 65 6e 64 5f 63 6f     /* 1: (end_co
2fde4 6e 73 74 72 61 69 6e 74 73 20 26 26 20 21 62 52  nstraints && !bR
2fde5 65 76 29 20 2a 2f 0a 20 20 20 20 20 20 4f 50 5f  ev) */.      OP_
2fde6 49 64 78 4c 54 20 20 20 20 20 20 20 20 20 20 20  IdxLT           
2fde7 20 20 2f 2a 20 32 3a 20 28 65 6e 64 5f 63 6f 6e    /* 2: (end_con
2fde8 73 74 72 61 69 6e 74 73 20 26 26 20 62 52 65 76  straints && bRev
2fde9 29 20 2a 2f 0a 20 20 20 20 7d 3b 0a 20 20 20 20  ) */.    };.    
2fdea 69 6e 74 20 6e 45 71 20 3d 20 70 4c 65 76 65 6c  int nEq = pLevel
2fdeb 2d 3e 70 6c 61 6e 2e 6e 45 71 3b 0a 20 20 20 20  ->plan.nEq;.    
2fdec 69 6e 74 20 69 73 4d 69 6e 51 75 65 72 79 20 3d  int isMinQuery =
2fded 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20   0;          /* 
2fdee 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 6f 70  If this is an op
2fdef 74 69 6d 69 7a 65 64 20 53 45 4c 45 43 54 20 6d  timized SELECT m
2fdf0 69 6e 28 78 29 2e 2e 20 2a 2f 0a 20 20 20 20 69  in(x).. */.    i
2fdf1 6e 74 20 72 65 67 42 61 73 65 3b 20 20 20 20 20  nt regBase;     
2fdf2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
2fdf3 61 73 65 20 72 65 67 69 73 74 65 72 20 68 6f 6c  ase register hol
2fdf4 64 69 6e 67 20 63 6f 6e 73 74 72 61 69 6e 74 20  ding constraint 
2fdf5 76 61 6c 75 65 73 20 2a 2f 0a 20 20 20 20 69 6e  values */.    in
2fdf6 74 20 72 31 3b 20 20 20 20 20 20 20 20 20 20 20  t r1;           
2fdf7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65             /* Te
2fdf8 6d 70 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 20  mp register */. 
2fdf9 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 52     WhereTerm *pR
2fdfa 61 6e 67 65 53 74 61 72 74 20 3d 20 30 3b 20 20  angeStart = 0;  
2fdfb 2f 2a 20 49 6e 65 71 75 61 6c 69 74 79 20 63 6f  /* Inequality co
2fdfc 6e 73 74 72 61 69 6e 74 20 61 74 20 72 61 6e 67  nstraint at rang
2fdfd 65 20 73 74 61 72 74 20 2a 2f 0a 20 20 20 20 57  e start */.    W
2fdfe 68 65 72 65 54 65 72 6d 20 2a 70 52 61 6e 67 65  hereTerm *pRange
2fdff 45 6e 64 20 3d 20 30 3b 20 20 20 20 2f 2a 20 49  End = 0;    /* I
2fe00 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72  nequality constr
2fe01 61 69 6e 74 20 61 74 20 72 61 6e 67 65 20 65 6e  aint at range en
2fe02 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 73 74 61  d */.    int sta
2fe03 72 74 45 71 3b 20 20 20 20 20 20 20 20 20 20 20  rtEq;           
2fe04 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
2fe05 20 72 61 6e 67 65 20 73 74 61 72 74 20 75 73 65   range start use
2fe06 73 20 3d 3d 2c 20 3e 3d 20 6f 72 20 3c 3d 20 2a  s ==, >= or <= *
2fe07 2f 0a 20 20 20 20 69 6e 74 20 65 6e 64 45 71 3b  /.    int endEq;
2fe08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe09 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 72 61     /* True if ra
2fe0a 6e 67 65 20 65 6e 64 20 75 73 65 73 20 3d 3d 2c  nge end uses ==,
2fe0b 20 3e 3d 20 6f 72 20 3c 3d 20 2a 2f 0a 20 20 20   >= or <= */.   
2fe0c 20 69 6e 74 20 73 74 61 72 74 5f 63 6f 6e 73 74   int start_const
2fe0d 72 61 69 6e 74 73 3b 20 20 20 20 20 20 20 2f 2a  raints;       /*
2fe0e 20 53 74 61 72 74 20 6f 66 20 72 61 6e 67 65 20   Start of range 
2fe0f 69 73 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 2a  is constrained *
2fe10 2f 0a 20 20 20 20 69 6e 74 20 6e 43 6f 6e 73 74  /.    int nConst
2fe11 72 61 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20  raint;          
2fe12 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2fe13 63 6f 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 73  constraint terms
2fe14 20 2a 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70   */.    Index *p
2fe15 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Idx;         /* 
2fe16 54 68 65 20 69 6e 64 65 78 20 77 65 20 77 69 6c  The index we wil
2fe17 6c 20 62 65 20 75 73 69 6e 67 20 2a 2f 0a 20 20  l be using */.  
2fe18 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b 20 20    int iIdxCur;  
2fe19 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44         /* The VD
2fe1a 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68  BE cursor for th
2fe1b 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69  e index */.    i
2fe1c 6e 74 20 6e 45 78 74 72 61 52 65 67 20 3d 20 30  nt nExtraReg = 0
2fe1d 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66  ;   /* Number of
2fe1e 20 65 78 74 72 61 20 72 65 67 69 73 74 65 72 73   extra registers
2fe1f 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 20 20 69   needed */.    i
2fe20 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20  nt op;          
2fe21 20 20 20 20 2f 2a 20 49 6e 73 74 72 75 63 74 69      /* Instructi
2fe22 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 20  on opcode */.   
2fe23 20 63 68 61 72 20 2a 7a 41 66 66 3b 0a 0a 20 20   char *zAff;..  
2fe24 20 20 70 49 64 78 20 3d 20 70 4c 65 76 65 6c 2d    pIdx = pLevel-
2fe25 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3b 0a 20 20  >plan.u.pIdx;.  
2fe26 20 20 69 49 64 78 43 75 72 20 3d 20 70 4c 65 76    iIdxCur = pLev
2fe27 65 6c 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20 20  el->iIdxCur;.   
2fe28 20 6b 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c   k = pIdx->aiCol
2fe29 75 6d 6e 5b 6e 45 71 5d 3b 20 20 20 20 20 2f 2a  umn[nEq];     /*
2fe2a 20 43 6f 6c 75 6d 6e 20 66 6f 72 20 69 6e 65 71   Column for ineq
2fe2b 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e  uality constrain
2fe2c 74 73 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 49 66  ts */..    /* If
2fe2d 20 74 68 69 73 20 6c 6f 6f 70 20 73 61 74 69 73   this loop satis
2fe2e 66 69 65 73 20 61 20 73 6f 72 74 20 6f 72 64 65  fies a sort orde
2fe2f 72 20 28 70 4f 72 64 65 72 42 79 29 20 72 65 71  r (pOrderBy) req
2fe30 75 65 73 74 20 74 68 61 74 20 0a 20 20 20 20 2a  uest that .    *
2fe31 2a 20 77 61 73 20 70 61 73 73 65 64 20 74 6f 20  * was passed to 
2fe32 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74 6f  this function to
2fe33 20 69 6d 70 6c 65 6d 65 6e 74 20 61 20 22 53 45   implement a "SE
2fe34 4c 45 43 54 20 6d 69 6e 28 78 29 20 2e 2e 2e 22  LECT min(x) ..."
2fe35 20 0a 20 20 20 20 2a 2a 20 71 75 65 72 79 2c 20   .    ** query, 
2fe36 74 68 65 6e 20 74 68 65 20 63 61 6c 6c 65 72 20  then the caller 
2fe37 77 69 6c 6c 20 6f 6e 6c 79 20 61 6c 6c 6f 77 20  will only allow 
2fe38 74 68 65 20 6c 6f 6f 70 20 74 6f 20 72 75 6e 20  the loop to run 
2fe39 66 6f 72 0a 20 20 20 20 2a 2a 20 61 20 73 69 6e  for.    ** a sin
2fe3a 67 6c 65 20 69 74 65 72 61 74 69 6f 6e 2e 20 54  gle iteration. T
2fe3b 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74  his means that t
2fe3c 68 65 20 66 69 72 73 74 20 72 6f 77 20 72 65 74  he first row ret
2fe3d 75 72 6e 65 64 0a 20 20 20 20 2a 2a 20 73 68 6f  urned.    ** sho
2fe3e 75 6c 64 20 6e 6f 74 20 68 61 76 65 20 61 20 4e  uld not have a N
2fe3f 55 4c 4c 20 76 61 6c 75 65 20 73 74 6f 72 65 64  ULL value stored
2fe40 20 69 6e 20 27 78 27 2e 20 49 66 20 63 6f 6c 75   in 'x'. If colu
2fe41 6d 6e 20 27 78 27 20 69 73 0a 20 20 20 20 2a 2a  mn 'x' is.    **
2fe42 20 74 68 65 20 66 69 72 73 74 20 6f 6e 65 20 61   the first one a
2fe43 66 74 65 72 20 74 68 65 20 6e 45 71 20 65 71 75  fter the nEq equ
2fe44 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
2fe45 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2c 0a  s in the index,.
2fe46 20 20 20 20 2a 2a 20 74 68 69 73 20 72 65 71 75      ** this requ
2fe47 69 72 65 73 20 73 6f 6d 65 20 73 70 65 63 69 61  ires some specia
2fe48 6c 20 68 61 6e 64 6c 69 6e 67 2e 0a 20 20 20 20  l handling..    
2fe49 2a 2f 0a 20 20 20 20 69 66 28 20 28 77 63 74 72  */.    if( (wctr
2fe4a 6c 46 6c 61 67 73 26 57 48 45 52 45 5f 4f 52 44  lFlags&WHERE_ORD
2fe4b 45 52 42 59 5f 4d 49 4e 29 21 3d 30 0a 20 20 20  ERBY_MIN)!=0.   
2fe4c 20 20 26 26 20 28 70 4c 65 76 65 6c 2d 3e 70 6c    && (pLevel->pl
2fe4d 61 6e 2e 77 73 46 6c 61 67 73 26 57 48 45 52 45  an.wsFlags&WHERE
2fe4e 5f 4f 52 44 45 52 42 59 29 0a 20 20 20 20 20 26  _ORDERBY).     &
2fe4f 26 20 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  & (pIdx->nColumn
2fe50 3e 6e 45 71 29 0a 20 20 20 20 29 7b 0a 20 20 20  >nEq).    ){.   
2fe51 20 20 20 2f 2a 20 61 73 73 65 72 74 28 20 70 4f     /* assert( pO
2fe52 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3d 3d 31  rderBy->nExpr==1
2fe53 20 29 3b 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20   ); */.      /* 
2fe54 61 73 73 65 72 74 28 20 70 4f 72 64 65 72 42 79  assert( pOrderBy
2fe55 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 69 43  ->a[0].pExpr->iC
2fe56 6f 6c 75 6d 6e 3d 3d 70 49 64 78 2d 3e 61 69 43  olumn==pIdx->aiC
2fe57 6f 6c 75 6d 6e 5b 6e 45 71 5d 20 29 3b 20 2a 2f  olumn[nEq] ); */
2fe58 0a 20 20 20 20 20 20 69 73 4d 69 6e 51 75 65 72  .      isMinQuer
2fe59 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 6e 45 78  y = 1;.      nEx
2fe5a 74 72 61 52 65 67 20 3d 20 31 3b 0a 20 20 20 20  traReg = 1;.    
2fe5b 7d 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64 20 61  }..    /* Find a
2fe5c 6e 79 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f  ny inequality co
2fe5d 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 73 20 66  nstraint terms f
2fe5e 6f 72 20 74 68 65 20 73 74 61 72 74 20 61 6e 64  or the start and
2fe5f 20 65 6e 64 20 0a 20 20 20 20 2a 2a 20 6f 66 20   end .    ** of 
2fe60 74 68 65 20 72 61 6e 67 65 2e 20 0a 20 20 20 20  the range. .    
2fe61 2a 2f 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65  */.    if( pLeve
2fe62 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
2fe63 26 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49  & WHERE_TOP_LIMI
2fe64 54 20 29 7b 0a 20 20 20 20 20 20 70 52 61 6e 67  T ){.      pRang
2fe65 65 45 6e 64 20 3d 20 66 69 6e 64 54 65 72 6d 28  eEnd = findTerm(
2fe66 70 57 43 2c 20 69 43 75 72 2c 20 6b 2c 20 6e 6f  pWC, iCur, k, no
2fe67 74 52 65 61 64 79 2c 20 28 57 4f 5f 4c 54 7c 57  tReady, (WO_LT|W
2fe68 4f 5f 4c 45 29 2c 20 70 49 64 78 29 3b 0a 20 20  O_LE), pIdx);.  
2fe69 20 20 20 20 6e 45 78 74 72 61 52 65 67 20 3d 20      nExtraReg = 
2fe6a 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  1;.    }.    if(
2fe6b 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73   pLevel->plan.ws
2fe6c 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 42 54  Flags & WHERE_BT
2fe6d 4d 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20 20  M_LIMIT ){.     
2fe6e 20 70 52 61 6e 67 65 53 74 61 72 74 20 3d 20 66   pRangeStart = f
2fe6f 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75  indTerm(pWC, iCu
2fe70 72 2c 20 6b 2c 20 6e 6f 74 52 65 61 64 79 2c 20  r, k, notReady, 
2fe71 28 57 4f 5f 47 54 7c 57 4f 5f 47 45 29 2c 20 70  (WO_GT|WO_GE), p
2fe72 49 64 78 29 3b 0a 20 20 20 20 20 20 6e 45 78 74  Idx);.      nExt
2fe73 72 61 52 65 67 20 3d 20 31 3b 0a 20 20 20 20 7d  raReg = 1;.    }
2fe74 0a 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74  ..    /* Generat
2fe75 65 20 63 6f 64 65 20 74 6f 20 65 76 61 6c 75 61  e code to evalua
2fe76 74 65 20 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e  te all constrain
2fe77 74 20 74 65 72 6d 73 20 75 73 69 6e 67 20 3d 3d  t terms using ==
2fe78 20 6f 72 20 49 4e 0a 20 20 20 20 2a 2a 20 61 6e   or IN.    ** an
2fe79 64 20 73 74 6f 72 65 20 74 68 65 20 76 61 6c 75  d store the valu
2fe7a 65 73 20 6f 66 20 74 68 6f 73 65 20 74 65 72 6d  es of those term
2fe7b 73 20 69 6e 20 61 6e 20 61 72 72 61 79 20 6f 66  s in an array of
2fe7c 20 72 65 67 69 73 74 65 72 73 0a 20 20 20 20 2a   registers.    *
2fe7d 2a 20 73 74 61 72 74 69 6e 67 20 61 74 20 72 65  * starting at re
2fe7e 67 42 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  gBase..    */.  
2fe7f 20 20 72 65 67 42 61 73 65 20 3d 20 63 6f 64 65    regBase = code
2fe80 41 6c 6c 45 71 75 61 6c 69 74 79 54 65 72 6d 73  AllEqualityTerms
2fe81 28 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65  (.        pParse
2fe82 2c 20 70 4c 65 76 65 6c 2c 20 70 57 43 2c 20 6e  , pLevel, pWC, n
2fe83 6f 74 52 65 61 64 79 2c 20 6e 45 78 74 72 61 52  otReady, nExtraR
2fe84 65 67 2c 20 26 7a 41 66 66 0a 20 20 20 20 29 3b  eg, &zAff.    );
2fe85 0a 20 20 20 20 61 64 64 72 4e 78 74 20 3d 20 70  .    addrNxt = p
2fe86 4c 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74 3b 0a  Level->addrNxt;.
2fe87 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 61 72  .    /* If we ar
2fe88 65 20 64 6f 69 6e 67 20 61 20 72 65 76 65 72 73  e doing a revers
2fe89 65 20 6f 72 64 65 72 20 73 63 61 6e 20 6f 6e 20  e order scan on 
2fe8a 61 6e 20 61 73 63 65 6e 64 69 6e 67 20 69 6e 64  an ascending ind
2fe8b 65 78 2c 20 6f 72 0a 20 20 20 20 2a 2a 20 61 20  ex, or.    ** a 
2fe8c 66 6f 72 77 61 72 64 20 6f 72 64 65 72 20 73 63  forward order sc
2fe8d 61 6e 20 6f 6e 20 61 20 64 65 73 63 65 6e 64 69  an on a descendi
2fe8e 6e 67 20 69 6e 64 65 78 2c 20 69 6e 74 65 72 63  ng index, interc
2fe8f 68 61 6e 67 65 20 74 68 65 20 0a 20 20 20 20 2a  hange the .    *
2fe90 2a 20 73 74 61 72 74 20 61 6e 64 20 65 6e 64 20  * start and end 
2fe91 74 65 72 6d 73 20 28 70 52 61 6e 67 65 53 74 61  terms (pRangeSta
2fe92 72 74 20 61 6e 64 20 70 52 61 6e 67 65 45 6e 64  rt and pRangeEnd
2fe93 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  )..    */.    if
2fe94 28 20 62 52 65 76 3d 3d 28 70 49 64 78 2d 3e 61  ( bRev==(pIdx->a
2fe95 53 6f 72 74 4f 72 64 65 72 5b 6e 45 71 5d 3d 3d  SortOrder[nEq]==
2fe96 53 51 4c 49 54 45 5f 53 4f 5f 41 53 43 29 20 29  SQLITE_SO_ASC) )
2fe97 7b 0a 20 20 20 20 20 20 53 57 41 50 28 57 68 65  {.      SWAP(Whe
2fe98 72 65 54 65 72 6d 20 2a 2c 20 70 52 61 6e 67 65  reTerm *, pRange
2fe99 45 6e 64 2c 20 70 52 61 6e 67 65 53 74 61 72 74  End, pRangeStart
2fe9a 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 74 65  );.    }..    te
2fe9b 73 74 63 61 73 65 28 20 70 52 61 6e 67 65 53 74  stcase( pRangeSt
2fe9c 61 72 74 20 26 26 20 70 52 61 6e 67 65 53 74 61  art && pRangeSta
2fe9d 72 74 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  rt->eOperator & 
2fe9e 57 4f 5f 4c 45 20 29 3b 0a 20 20 20 20 74 65 73  WO_LE );.    tes
2fe9f 74 63 61 73 65 28 20 70 52 61 6e 67 65 53 74 61  tcase( pRangeSta
2fea0 72 74 20 26 26 20 70 52 61 6e 67 65 53 74 61 72  rt && pRangeStar
2fea1 74 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 57  t->eOperator & W
2fea2 4f 5f 47 45 20 29 3b 0a 20 20 20 20 74 65 73 74  O_GE );.    test
2fea3 63 61 73 65 28 20 70 52 61 6e 67 65 45 6e 64 20  case( pRangeEnd 
2fea4 26 26 20 70 52 61 6e 67 65 45 6e 64 2d 3e 65 4f  && pRangeEnd->eO
2fea5 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 4c 45 20  perator & WO_LE 
2fea6 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
2fea7 20 70 52 61 6e 67 65 45 6e 64 20 26 26 20 70 52   pRangeEnd && pR
2fea8 61 6e 67 65 45 6e 64 2d 3e 65 4f 70 65 72 61 74  angeEnd->eOperat
2fea9 6f 72 20 26 20 57 4f 5f 47 45 20 29 3b 0a 20 20  or & WO_GE );.  
2feaa 20 20 73 74 61 72 74 45 71 20 3d 20 21 70 52 61    startEq = !pRa
2feab 6e 67 65 53 74 61 72 74 20 7c 7c 20 70 52 61 6e  ngeStart || pRan
2feac 67 65 53 74 61 72 74 2d 3e 65 4f 70 65 72 61 74  geStart->eOperat
2fead 6f 72 20 26 20 28 57 4f 5f 4c 45 7c 57 4f 5f 47  or & (WO_LE|WO_G
2feae 45 29 3b 0a 20 20 20 20 65 6e 64 45 71 20 3d 20  E);.    endEq = 
2feaf 20 20 21 70 52 61 6e 67 65 45 6e 64 20 7c 7c 20    !pRangeEnd || 
2feb0 70 52 61 6e 67 65 45 6e 64 2d 3e 65 4f 70 65 72  pRangeEnd->eOper
2feb1 61 74 6f 72 20 26 20 28 57 4f 5f 4c 45 7c 57 4f  ator & (WO_LE|WO
2feb2 5f 47 45 29 3b 0a 20 20 20 20 73 74 61 72 74 5f  _GE);.    start_
2feb3 63 6f 6e 73 74 72 61 69 6e 74 73 20 3d 20 70 52  constraints = pR
2feb4 61 6e 67 65 53 74 61 72 74 20 7c 7c 20 6e 45 71  angeStart || nEq
2feb5 3e 30 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 65 6b  >0;..    /* Seek
2feb6 20 74 68 65 20 69 6e 64 65 78 20 63 75 72 73 6f   the index curso
2feb7 72 20 74 6f 20 74 68 65 20 73 74 61 72 74 20 6f  r to the start o
2feb8 66 20 74 68 65 20 72 61 6e 67 65 2e 20 2a 2f 0a  f the range. */.
2feb9 20 20 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74 20      nConstraint 
2feba 3d 20 6e 45 71 3b 0a 20 20 20 20 69 66 28 20 70  = nEq;.    if( p
2febb 52 61 6e 67 65 53 74 61 72 74 20 29 7b 0a 20 20  RangeStart ){.  
2febc 20 20 20 20 45 78 70 72 20 2a 70 52 69 67 68 74      Expr *pRight
2febd 20 3d 20 70 52 61 6e 67 65 53 74 61 72 74 2d 3e   = pRangeStart->
2febe 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20  pExpr->pRight;. 
2febf 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
2fec0 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 52 69  Code(pParse, pRi
2fec1 67 68 74 2c 20 72 65 67 42 61 73 65 2b 6e 45 71  ght, regBase+nEq
2fec2 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2fec3 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2fec4 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 42 61 73 65  _IsNull, regBase
2fec5 2b 6e 45 71 2c 20 61 64 64 72 4e 78 74 29 3b 0a  +nEq, addrNxt);.
2fec6 20 20 20 20 20 20 69 66 28 20 7a 41 66 66 20 0a        if( zAff .
2fec7 20 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65         && sqlite
2fec8 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79  3CompareAffinity
2fec9 28 70 52 69 67 68 74 2c 20 7a 41 66 66 5b 6e 43  (pRight, zAff[nC
2feca 6f 6e 73 74 72 61 69 6e 74 5d 29 3d 3d 53 51 4c  onstraint])==SQL
2fecb 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 0a 20 20 20  ITE_AFF_NONE.   
2fecc 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a     ){.        /*
2fecd 20 53 69 6e 63 65 20 74 68 65 20 63 6f 6d 70 61   Since the compa
2fece 72 69 73 6f 6e 20 69 73 20 74 6f 20 62 65 20 70  rison is to be p
2fecf 65 72 66 6f 72 6d 65 64 20 77 69 74 68 20 6e 6f  erformed with no
2fed0 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 70 70   conversions app
2fed1 6c 69 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20  lied.        ** 
2fed2 74 6f 20 74 68 65 20 6f 70 65 72 61 6e 64 73 2c  to the operands,
2fed3 20 73 65 74 20 74 68 65 20 61 66 66 69 6e 69 74   set the affinit
2fed4 79 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 70 52  y to apply to pR
2fed5 69 67 68 74 20 74 6f 20 0a 20 20 20 20 20 20 20  ight to .       
2fed6 20 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e   ** SQLITE_AFF_N
2fed7 4f 4e 45 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20  ONE.  */.       
2fed8 20 7a 41 66 66 5b 6e 43 6f 6e 73 74 72 61 69 6e   zAff[nConstrain
2fed9 74 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f  t] = SQLITE_AFF_
2feda 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 7d 0a 20 20  NONE;.      }.  
2fedb 20 20 20 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b      nConstraint+
2fedc 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  +;.    }else if(
2fedd 20 69 73 4d 69 6e 51 75 65 72 79 20 29 7b 0a 20   isMinQuery ){. 
2fede 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2fedf 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c  AddOp2(v, OP_Nul
2fee0 6c 2c 20 30 2c 20 72 65 67 42 61 73 65 2b 6e 45  l, 0, regBase+nE
2fee1 71 29 3b 0a 20 20 20 20 20 20 6e 43 6f 6e 73 74  q);.      nConst
2fee2 72 61 69 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 73  raint++;.      s
2fee3 74 61 72 74 45 71 20 3d 20 30 3b 0a 20 20 20 20  tartEq = 0;.    
2fee4 20 20 73 74 61 72 74 5f 63 6f 6e 73 74 72 61 69    start_constrai
2fee5 6e 74 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20  nts = 1;.    }. 
2fee6 20 20 20 63 6f 64 65 41 70 70 6c 79 41 66 66 69     codeApplyAffi
2fee7 6e 69 74 79 28 70 50 61 72 73 65 2c 20 72 65 67  nity(pParse, reg
2fee8 42 61 73 65 2c 20 6e 43 6f 6e 73 74 72 61 69 6e  Base, nConstrain
2fee9 74 2c 20 7a 41 66 66 29 3b 0a 20 20 20 20 6f 70  t, zAff);.    op
2feea 20 3d 20 61 53 74 61 72 74 4f 70 5b 28 73 74 61   = aStartOp[(sta
2feeb 72 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 3c 3c  rt_constraints<<
2feec 32 29 20 2b 20 28 73 74 61 72 74 45 71 3c 3c 31  2) + (startEq<<1
2feed 29 20 2b 20 62 52 65 76 5d 3b 0a 20 20 20 20 61  ) + bRev];.    a
2feee 73 73 65 72 74 28 20 6f 70 21 3d 30 20 29 3b 0a  ssert( op!=0 );.
2feef 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
2fef0 3d 3d 4f 50 5f 52 65 77 69 6e 64 20 29 3b 0a 20  ==OP_Rewind );. 
2fef1 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
2fef2 3d 4f 50 5f 4c 61 73 74 20 29 3b 0a 20 20 20 20  =OP_Last );.    
2fef3 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50  testcase( op==OP
2fef4 5f 53 65 65 6b 47 74 20 29 3b 0a 20 20 20 20 74  _SeekGt );.    t
2fef5 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f  estcase( op==OP_
2fef6 53 65 65 6b 47 65 20 29 3b 0a 20 20 20 20 74 65  SeekGe );.    te
2fef7 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 53  stcase( op==OP_S
2fef8 65 65 6b 4c 65 20 29 3b 0a 20 20 20 20 74 65 73  eekLe );.    tes
2fef9 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 53 65  tcase( op==OP_Se
2fefa 65 6b 4c 74 20 29 3b 0a 20 20 20 20 73 71 6c 69  ekLt );.    sqli
2fefb 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
2fefc 20 6f 70 2c 20 69 49 64 78 43 75 72 2c 20 61 64   op, iIdxCur, ad
2fefd 64 72 4e 78 74 2c 20 72 65 67 42 61 73 65 2c 20  drNxt, regBase, 
2fefe 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2feff 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4e         SQLITE_IN
2ff00 54 5f 54 4f 5f 50 54 52 28 6e 43 6f 6e 73 74 72  T_TO_PTR(nConstr
2ff01 61 69 6e 74 29 2c 20 50 34 5f 49 4e 54 33 32 29  aint), P4_INT32)
2ff02 3b 0a 0a 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74  ;..    /* Load t
2ff03 68 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  he value for the
2ff04 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73   inequality cons
2ff05 74 72 61 69 6e 74 20 61 74 20 74 68 65 20 65 6e  traint at the en
2ff06 64 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20  d of the.    ** 
2ff07 72 61 6e 67 65 20 28 69 66 20 61 6e 79 29 2e 0a  range (if any)..
2ff08 20 20 20 20 2a 2f 0a 20 20 20 20 6e 43 6f 6e 73      */.    nCons
2ff09 74 72 61 69 6e 74 20 3d 20 6e 45 71 3b 0a 20 20  traint = nEq;.  
2ff0a 20 20 69 66 28 20 70 52 61 6e 67 65 45 6e 64 20    if( pRangeEnd 
2ff0b 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70  ){.      Expr *p
2ff0c 52 69 67 68 74 20 3d 20 70 52 61 6e 67 65 45 6e  Right = pRangeEn
2ff0d 64 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67 68 74  d->pExpr->pRight
2ff0e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
2ff0f 78 70 72 43 61 63 68 65 52 65 6d 6f 76 65 28 70  xprCacheRemove(p
2ff10 50 61 72 73 65 2c 20 72 65 67 42 61 73 65 2b 6e  Parse, regBase+n
2ff11 45 71 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Eq);.      sqlit
2ff12 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
2ff13 65 2c 20 70 52 69 67 68 74 2c 20 72 65 67 42 61  e, pRight, regBa
2ff14 73 65 2b 6e 45 71 29 3b 0a 20 20 20 20 20 20 73  se+nEq);.      s
2ff15 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2ff16 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72  (v, OP_IsNull, r
2ff17 65 67 42 61 73 65 2b 6e 45 71 2c 20 61 64 64 72  egBase+nEq, addr
2ff18 4e 78 74 29 3b 0a 20 20 20 20 20 20 7a 41 66 66  Nxt);.      zAff
2ff19 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 44   = sqlite3DbStrD
2ff1a 75 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a  up(pParse->db, z
2ff1b 41 66 66 29 3b 0a 20 20 20 20 20 20 69 66 28 20  Aff);.      if( 
2ff1c 7a 41 66 66 20 0a 20 20 20 20 20 20 20 26 26 20  zAff .       && 
2ff1d 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66  sqlite3CompareAf
2ff1e 66 69 6e 69 74 79 28 70 52 69 67 68 74 2c 20 7a  finity(pRight, z
2ff1f 41 66 66 5b 6e 43 6f 6e 73 74 72 61 69 6e 74 5d  Aff[nConstraint]
2ff20 29 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f  )==SQLITE_AFF_NO
2ff21 4e 45 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20  NE.      ){.    
2ff22 20 20 20 20 2f 2a 20 53 69 6e 63 65 20 74 68 65      /* Since the
2ff23 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74   comparison is t
2ff24 6f 20 62 65 20 70 65 72 66 6f 72 6d 65 64 20 77  o be performed w
2ff25 69 74 68 20 6e 6f 20 63 6f 6e 76 65 72 73 69 6f  ith no conversio
2ff26 6e 73 20 61 70 70 6c 69 65 64 0a 20 20 20 20 20  ns applied.     
2ff27 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 6f 70 65     ** to the ope
2ff28 72 61 6e 64 73 2c 20 73 65 74 20 74 68 65 20 61  rands, set the a
2ff29 66 66 69 6e 69 74 79 20 74 6f 20 61 70 70 6c 79  ffinity to apply
2ff2a 20 74 6f 20 70 52 69 67 68 74 20 74 6f 20 0a 20   to pRight to . 
2ff2b 20 20 20 20 20 20 20 2a 2a 20 53 51 4c 49 54 45         ** SQLITE
2ff2c 5f 41 46 46 5f 4e 4f 4e 45 2e 20 20 2a 2f 0a 20  _AFF_NONE.  */. 
2ff2d 20 20 20 20 20 20 20 7a 41 66 66 5b 6e 43 6f 6e         zAff[nCon
2ff2e 73 74 72 61 69 6e 74 5d 20 3d 20 53 51 4c 49 54  straint] = SQLIT
2ff2f 45 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20  E_AFF_NONE;.    
2ff30 20 20 7d 0a 20 20 20 20 20 20 63 6f 64 65 41 70    }.      codeAp
2ff31 70 6c 79 41 66 66 69 6e 69 74 79 28 70 50 61 72  plyAffinity(pPar
2ff32 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 45 71  se, regBase, nEq
2ff33 2b 31 2c 20 7a 41 66 66 29 3b 0a 20 20 20 20 20  +1, zAff);.     
2ff34 20 6e 43 6f 6e 73 74 72 61 69 6e 74 2b 2b 3b 0a   nConstraint++;.
2ff35 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 6f      }..    /* To
2ff36 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 62 6f  p of the loop bo
2ff37 64 79 20 2a 2f 0a 20 20 20 20 70 4c 65 76 65 6c  dy */.    pLevel
2ff38 2d 3e 70 32 20 3d 20 73 71 6c 69 74 65 33 56 64  ->p2 = sqlite3Vd
2ff39 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
2ff3a 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20  ;..    /* Check 
2ff3b 69 66 20 74 68 65 20 69 6e 64 65 78 20 63 75 72  if the index cur
2ff3c 73 6f 72 20 69 73 20 70 61 73 74 20 74 68 65 20  sor is past the 
2ff3d 65 6e 64 20 6f 66 20 74 68 65 20 72 61 6e 67 65  end of the range
2ff3e 2e 20 2a 2f 0a 20 20 20 20 6f 70 20 3d 20 61 45  . */.    op = aE
2ff3f 6e 64 4f 70 5b 28 70 52 61 6e 67 65 45 6e 64 20  ndOp[(pRangeEnd 
2ff40 7c 7c 20 6e 45 71 29 20 2a 20 28 31 20 2b 20 62  || nEq) * (1 + b
2ff41 52 65 76 29 5d 3b 0a 20 20 20 20 74 65 73 74 63  Rev)];.    testc
2ff42 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 4e 6f 6f 70  ase( op==OP_Noop
2ff43 20 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65   );.    testcase
2ff44 28 20 6f 70 3d 3d 4f 50 5f 49 64 78 47 45 20 29  ( op==OP_IdxGE )
2ff45 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20  ;.    testcase( 
2ff46 6f 70 3d 3d 4f 50 5f 49 64 78 4c 54 20 29 3b 0a  op==OP_IdxLT );.
2ff47 20 20 20 20 69 66 28 20 6f 70 21 3d 4f 50 5f 4e      if( op!=OP_N
2ff48 6f 6f 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  oop ){.      sql
2ff49 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2ff4a 2c 20 6f 70 2c 20 69 49 64 78 43 75 72 2c 20 61  , op, iIdxCur, a
2ff4b 64 64 72 4e 78 74 2c 20 72 65 67 42 61 73 65 2c  ddrNxt, regBase,
2ff4c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2ff4d 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
2ff4e 49 4e 54 5f 54 4f 5f 50 54 52 28 6e 43 6f 6e 73  INT_TO_PTR(nCons
2ff4f 74 72 61 69 6e 74 29 2c 20 50 34 5f 49 4e 54 33  traint), P4_INT3
2ff50 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  2);.      sqlite
2ff51 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
2ff52 20 65 6e 64 45 71 21 3d 62 52 65 76 20 3f 31 3a   endEq!=bRev ?1:
2ff53 30 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f  0);.    }..    /
2ff54 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 69  * If there are i
2ff55 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72  nequality constr
2ff56 61 69 6e 74 73 2c 20 63 68 65 63 6b 20 74 68 61  aints, check tha
2ff57 74 20 74 68 65 20 76 61 6c 75 65 0a 20 20 20 20  t the value.    
2ff58 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  ** of the table 
2ff59 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 65 20  column that the 
2ff5a 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 74 72  inequality contr
2ff5b 61 69 6e 73 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  ains is not NULL
2ff5c 2e 0a 20 20 20 20 2a 2a 20 49 66 20 69 74 20 69  ..    ** If it i
2ff5d 73 2c 20 6a 75 6d 70 20 74 6f 20 74 68 65 20 6e  s, jump to the n
2ff5e 65 78 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66  ext iteration of
2ff5f 20 74 68 65 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a   the loop..    *
2ff60 2f 0a 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74  /.    r1 = sqlit
2ff61 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
2ff62 72 73 65 29 3b 0a 20 20 20 20 74 65 73 74 63 61  rse);.    testca
2ff63 73 65 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  se( pLevel->plan
2ff64 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
2ff65 5f 42 54 4d 5f 4c 49 4d 49 54 20 29 3b 0a 20 20  _BTM_LIMIT );.  
2ff66 20 20 74 65 73 74 63 61 73 65 28 20 70 4c 65 76    testcase( pLev
2ff67 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
2ff68 20 26 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d   & WHERE_TOP_LIM
2ff69 49 54 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c  IT );.    if( pL
2ff6a 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61  evel->plan.wsFla
2ff6b 67 73 20 26 20 28 57 48 45 52 45 5f 42 54 4d 5f  gs & (WHERE_BTM_
2ff6c 4c 49 4d 49 54 7c 57 48 45 52 45 5f 54 4f 50 5f  LIMIT|WHERE_TOP_
2ff6d 4c 49 4d 49 54 29 20 29 7b 0a 20 20 20 20 20 20  LIMIT) ){.      
2ff6e 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2ff6f 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
2ff70 69 49 64 78 43 75 72 2c 20 6e 45 71 2c 20 72 31  iIdxCur, nEq, r1
2ff71 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2ff72 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2ff73 5f 49 73 4e 75 6c 6c 2c 20 72 31 2c 20 61 64 64  _IsNull, r1, add
2ff74 72 43 6f 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20  rCont);.    }.  
2ff75 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
2ff76 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
2ff77 72 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 65  r1);..    /* See
2ff78 6b 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 73  k the table curs
2ff79 6f 72 2c 20 69 66 20 72 65 71 75 69 72 65 64 20  or, if required 
2ff7a 2a 2f 0a 20 20 20 20 64 69 73 61 62 6c 65 54 65  */.    disableTe
2ff7b 72 6d 28 70 4c 65 76 65 6c 2c 20 70 52 61 6e 67  rm(pLevel, pRang
2ff7c 65 53 74 61 72 74 29 3b 0a 20 20 20 20 64 69 73  eStart);.    dis
2ff7d 61 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c  ableTerm(pLevel,
2ff7e 20 70 52 61 6e 67 65 45 6e 64 29 3b 0a 20 20 20   pRangeEnd);.   
2ff7f 20 69 66 28 20 21 6f 6d 69 74 54 61 62 6c 65 20   if( !omitTable 
2ff80 29 7b 0a 20 20 20 20 20 20 69 52 6f 77 69 64 52  ){.      iRowidR
2ff81 65 67 20 3d 20 69 52 65 6c 65 61 73 65 52 65 67  eg = iReleaseReg
2ff82 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
2ff83 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
2ff84 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2ff85 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 52  ddOp2(v, OP_IdxR
2ff86 6f 77 69 64 2c 20 69 49 64 78 43 75 72 2c 20 69  owid, iIdxCur, i
2ff87 52 6f 77 69 64 52 65 67 29 3b 0a 20 20 20 20 20  RowidReg);.     
2ff88 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
2ff89 65 53 74 6f 72 65 28 70 50 61 72 73 65 2c 20 69  eStore(pParse, i
2ff8a 43 75 72 2c 20 2d 31 2c 20 69 52 6f 77 69 64 52  Cur, -1, iRowidR
2ff8b 65 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  eg);.      sqlit
2ff8c 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2ff8d 4f 50 5f 53 65 65 6b 2c 20 69 43 75 72 2c 20 69  OP_Seek, iCur, i
2ff8e 52 6f 77 69 64 52 65 67 29 3b 20 20 2f 2a 20 44  RowidReg);  /* D
2ff8f 65 66 65 72 72 65 64 20 73 65 65 6b 20 2a 2f 0a  eferred seek */.
2ff90 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65      }..    /* Re
2ff91 63 6f 72 64 20 74 68 65 20 69 6e 73 74 72 75 63  cord the instruc
2ff92 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 74 65 72  tion used to ter
2ff93 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 2e  minate the loop.
2ff94 20 44 69 73 61 62 6c 65 20 0a 20 20 20 20 2a 2a   Disable .    **
2ff95 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65   WHERE clause te
2ff96 72 6d 73 20 6d 61 64 65 20 72 65 64 75 6e 64 61  rms made redunda
2ff97 6e 74 20 62 79 20 74 68 65 20 69 6e 64 65 78 20  nt by the index 
2ff98 72 61 6e 67 65 20 73 63 61 6e 2e 0a 20 20 20 20  range scan..    
2ff99 2a 2f 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f  */.    pLevel->o
2ff9a 70 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f 50 72  p = bRev ? OP_Pr
2ff9b 65 76 20 3a 20 4f 50 5f 4e 65 78 74 3b 0a 20 20  ev : OP_Next;.  
2ff9c 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69    pLevel->p1 = i
2ff9d 49 64 78 43 75 72 3b 0a 20 20 7d 65 6c 73 65 0a  IdxCur;.  }else.
2ff9e 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2ff9f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41  OMIT_OR_OPTIMIZA
2ffa0 54 49 4f 4e 0a 20 20 69 66 28 20 70 4c 65 76 65  TION.  if( pLeve
2ffa1 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20  l->plan.wsFlags 
2ffa2 26 20 57 48 45 52 45 5f 4d 55 4c 54 49 5f 4f 52  & WHERE_MULTI_OR
2ffa3 20 29 7b 0a 20 20 20 20 2f 2a 20 43 61 73 65 20   ){.    /* Case 
2ffa4 34 3a 20 20 54 77 6f 20 6f 72 20 6d 6f 72 65 20  4:  Two or more 
2ffa5 73 65 70 61 72 61 74 65 6c 79 20 69 6e 64 65 78  separately index
2ffa6 65 64 20 74 65 72 6d 73 20 63 6f 6e 6e 65 63 74  ed terms connect
2ffa7 65 64 20 62 79 20 4f 52 0a 20 20 20 20 2a 2a 0a  ed by OR.    **.
2ffa8 20 20 20 20 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a      ** Example:.
2ffa9 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
2ffaa 43 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28  CREATE TABLE t1(
2ffab 61 2c 62 2c 63 2c 64 29 3b 0a 20 20 20 20 2a 2a  a,b,c,d);.    **
2ffac 20 20 20 43 52 45 41 54 45 20 49 4e 44 45 58 20     CREATE INDEX 
2ffad 69 31 20 4f 4e 20 74 31 28 61 29 3b 0a 20 20 20  i1 ON t1(a);.   
2ffae 20 2a 2a 20 20 20 43 52 45 41 54 45 20 49 4e 44   **   CREATE IND
2ffaf 45 58 20 69 32 20 4f 4e 20 74 31 28 62 29 3b 0a  EX i2 ON t1(b);.
2ffb0 20 20 20 20 2a 2a 20 20 20 43 52 45 41 54 45 20      **   CREATE 
2ffb1 49 4e 44 45 58 20 69 33 20 4f 4e 20 74 31 28 63  INDEX i3 ON t1(c
2ffb2 29 3b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  );.    **.    **
2ffb3 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d     SELECT * FROM
2ffb4 20 74 31 20 57 48 45 52 45 20 61 3d 35 20 4f 52   t1 WHERE a=5 OR
2ffb5 20 62 3d 37 20 4f 52 20 28 63 3d 31 31 20 41 4e   b=7 OR (c=11 AN
2ffb6 44 20 64 3d 31 33 29 0a 20 20 20 20 2a 2a 0a 20  D d=13).    **. 
2ffb7 20 20 20 2a 2a 20 49 6e 20 74 68 65 20 65 78 61     ** In the exa
2ffb8 6d 70 6c 65 2c 20 74 68 65 72 65 20 61 72 65 20  mple, there are 
2ffb9 74 68 72 65 65 20 69 6e 64 65 78 65 64 20 74 65  three indexed te
2ffba 72 6d 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79  rms connected by
2ffbb 20 4f 52 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20   OR..    ** The 
2ffbc 74 6f 70 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  top of the loop 
2ffbd 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a  looks like this:
2ffbe 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20  .    **.    **  
2ffbf 20 20 20 20 20 20 20 20 4e 75 6c 6c 20 20 20 20          Null    
2ffc0 20 20 20 31 20 20 20 20 20 20 20 20 20 20 20 20     1            
2ffc1 20 20 20 20 23 20 5a 65 72 6f 20 74 68 65 20 72      # Zero the r
2ffc2 6f 77 73 65 74 20 69 6e 20 72 65 67 20 31 0a 20  owset in reg 1. 
2ffc3 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
2ffc4 6e 2c 20 66 6f 72 20 65 61 63 68 20 69 6e 64 65  n, for each inde
2ffc5 78 65 64 20 74 65 72 6d 2c 20 74 68 65 20 66 6f  xed term, the fo
2ffc6 6c 6c 6f 77 69 6e 67 2e 20 54 68 65 20 61 72 67  llowing. The arg
2ffc7 75 6d 65 6e 74 73 20 74 6f 0a 20 20 20 20 2a 2a  uments to.    **
2ffc8 20 52 6f 77 53 65 74 54 65 73 74 20 61 72 65 20   RowSetTest are 
2ffc9 73 75 63 68 20 74 68 61 74 20 74 68 65 20 72 6f  such that the ro
2ffca 77 69 64 20 6f 66 20 74 68 65 20 63 75 72 72 65  wid of the curre
2ffcb 6e 74 20 72 6f 77 20 69 73 20 69 6e 73 65 72 74  nt row is insert
2ffcc 65 64 0a 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74  ed.    ** into t
2ffcd 68 65 20 52 6f 77 53 65 74 2e 20 49 66 20 69 74  he RowSet. If it
2ffce 20 69 73 20 61 6c 72 65 61 64 79 20 70 72 65 73   is already pres
2ffcf 65 6e 74 2c 20 63 6f 6e 74 72 6f 6c 20 73 6b 69  ent, control ski
2ffd0 70 73 20 74 68 65 0a 20 20 20 20 2a 2a 20 47 6f  ps the.    ** Go
2ffd1 73 75 62 20 6f 70 63 6f 64 65 20 61 6e 64 20 6a  sub opcode and j
2ffd2 75 6d 70 73 20 73 74 72 61 69 67 68 74 20 74 6f  umps straight to
2ffd3 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61   the code genera
2ffd4 74 65 64 20 62 79 20 57 68 65 72 65 45 6e 64 28  ted by WhereEnd(
2ffd5 29 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  )..    **.    **
2ffd6 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57          sqlite3W
2ffd7 68 65 72 65 42 65 67 69 6e 28 3c 74 65 72 6d 3e  hereBegin(<term>
2ffd8 29 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20  ).    **        
2ffd9 20 20 52 6f 77 53 65 74 54 65 73 74 20 20 20 20    RowSetTest    
2ffda 20 20 20 20 20 20 20 20 20 20 20 20 20 20 23 20                # 
2ffdb 49 6e 73 65 72 74 20 72 6f 77 69 64 20 69 6e 74  Insert rowid int
2ffdc 6f 20 72 6f 77 73 65 74 0a 20 20 20 20 2a 2a 20  o rowset.    ** 
2ffdd 20 20 20 20 20 20 20 20 20 47 6f 73 75 62 20 20           Gosub  
2ffde 20 20 20 20 32 20 41 0a 20 20 20 20 2a 2a 20 20      2 A.    **  
2ffdf 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 68 65        sqlite3Whe
2ffe0 72 65 45 6e 64 28 29 0a 20 20 20 20 2a 2a 0a 20  reEnd().    **. 
2ffe1 20 20 20 2a 2a 20 46 6f 6c 6c 6f 77 69 6e 67 20     ** Following 
2ffe2 74 68 65 20 61 62 6f 76 65 2c 20 63 6f 64 65 20  the above, code 
2ffe3 74 6f 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65  to terminate the
2ffe4 20 6c 6f 6f 70 2e 20 4c 61 62 65 6c 20 41 2c 20   loop. Label A, 
2ffe5 74 68 65 20 74 61 72 67 65 74 0a 20 20 20 20 2a  the target.    *
2ffe6 2a 20 6f 66 20 74 68 65 20 47 6f 73 75 62 20 61  * of the Gosub a
2ffe7 62 6f 76 65 2c 20 6a 75 6d 70 73 20 74 6f 20 74  bove, jumps to t
2ffe8 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 72  he instruction r
2ffe9 69 67 68 74 20 61 66 74 65 72 20 74 68 65 20 47  ight after the G
2ffea 6f 74 6f 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  oto..    **.    
2ffeb 2a 2a 20 20 20 20 20 20 20 20 20 20 4e 75 6c 6c  **          Null
2ffec 20 20 20 20 20 20 20 31 20 20 20 20 20 20 20 20         1        
2ffed 20 20 20 20 20 20 20 20 23 20 5a 65 72 6f 20 74          # Zero t
2ffee 68 65 20 72 6f 77 73 65 74 20 69 6e 20 72 65 67  he rowset in reg
2ffef 20 31 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20   1.    **       
2fff0 20 20 20 47 6f 74 6f 20 20 20 20 20 20 20 42 20     Goto       B 
2fff1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 23                 #
2fff2 20 54 68 65 20 6c 6f 6f 70 20 69 73 20 66 69 6e   The loop is fin
2fff3 69 73 68 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20  ished..    **.  
2fff4 20 20 2a 2a 20 20 20 20 20 20 20 41 3a 20 3c 6c    **       A: <l
2fff5 6f 6f 70 20 62 6f 64 79 3e 20 20 20 20 20 20 20  oop body>       
2fff6 20 20 20 20 20 20 20 20 20 20 23 20 52 65 74 75            # Retu
2fff7 72 6e 20 64 61 74 61 2c 20 77 68 61 74 65 76 65  rn data, whateve
2fff8 72 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  r..    **.    **
2fff9 20 20 20 20 20 20 20 20 20 20 52 65 74 75 72 6e            Return
2fffa 20 20 20 20 20 32 20 20 20 20 20 20 20 20 20 20       2          
2fffb 20 20 20 20 20 20 23 20 4a 75 6d 70 20 62 61 63        # Jump bac
2fffc 6b 20 74 6f 20 74 68 65 20 47 6f 73 75 62 0a 20  k to the Gosub. 
2fffd 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20     **.    **    
2fffe 20 20 20 42 3a 20 3c 61 66 74 65 72 20 74 68 65     B: <after the
2ffff 20 6c 6f 6f 70 3e 0a 20 20 20 20 2a 2a 0a 20 20   loop>.    **.  
30000 20 20 2a 2f 0a 20 20 20 20 57 68 65 72 65 43 6c    */.    WhereCl
30001 61 75 73 65 20 2a 70 4f 72 57 63 3b 20 20 20 20  ause *pOrWc;    
30002 2f 2a 20 54 68 65 20 4f 52 2d 63 6c 61 75 73 65  /* The OR-clause
30003 20 62 72 6f 6b 65 6e 20 6f 75 74 20 69 6e 74 6f   broken out into
30004 20 73 75 62 74 65 72 6d 73 20 2a 2f 0a 20 20 20   subterms */.   
30005 20 57 68 65 72 65 54 65 72 6d 20 2a 70 46 69 6e   WhereTerm *pFin
30006 61 6c 3b 20 20 20 20 20 2f 2a 20 46 69 6e 61 6c  al;     /* Final
30007 20 73 75 62 74 65 72 6d 20 77 69 74 68 69 6e 20   subterm within 
30008 74 68 65 20 4f 52 2d 63 6c 61 75 73 65 2e 20 2a  the OR-clause. *
30009 2f 0a 20 20 20 20 53 72 63 4c 69 73 74 20 6f 6e  /.    SrcList on
3000a 65 54 61 62 3b 20 20 20 20 20 20 20 20 2f 2a 20  eTab;        /* 
3000b 53 68 6f 72 74 65 6e 65 64 20 74 61 62 6c 65 20  Shortened table 
3000c 6c 69 73 74 20 2a 2f 0a 0a 20 20 20 20 69 6e 74  list */..    int
3000d 20 72 65 67 52 65 74 75 72 6e 20 3d 20 2b 2b 70   regReturn = ++p
3000e 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 20 20 20 20  Parse->nMem;    
3000f 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
30010 65 72 20 75 73 65 64 20 77 69 74 68 20 4f 50 5f  er used with OP_
30011 47 6f 73 75 62 20 2a 2f 0a 20 20 20 20 69 6e 74  Gosub */.    int
30012 20 72 65 67 52 6f 77 73 65 74 20 3d 20 30 3b 20   regRowset = 0; 
30013 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30014 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
30015 65 72 20 66 6f 72 20 52 6f 77 53 65 74 20 6f 62  er for RowSet ob
30016 6a 65 63 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ject */.    int 
30017 72 65 67 52 6f 77 69 64 20 3d 20 30 3b 20 20 20  regRowid = 0;   
30018 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30019 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65        /* Registe
3001a 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64 20  r holding rowid 
3001b 2a 2f 0a 20 20 20 20 69 6e 74 20 69 4c 6f 6f 70  */.    int iLoop
3001c 42 6f 64 79 20 3d 20 73 71 6c 69 74 65 33 56 64  Body = sqlite3Vd
3001d 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 20  beMakeLabel(v); 
3001e 20 2f 2a 20 53 74 61 72 74 20 6f 66 20 6c 6f 6f   /* Start of loo
3001f 70 20 62 6f 64 79 20 2a 2f 0a 20 20 20 20 69 6e  p body */.    in
30020 74 20 69 52 65 74 49 6e 69 74 3b 20 20 20 20 20  t iRetInit;     
30021 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30022 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
30023 73 73 20 6f 66 20 72 65 67 52 65 74 75 72 6e 20  ss of regReturn 
30024 69 6e 69 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20  init */.    int 
30025 69 69 3b 0a 20 20 20 0a 20 20 20 20 70 54 65 72  ii;.   .    pTer
30026 6d 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  m = pLevel->plan
30027 2e 75 2e 70 54 65 72 6d 3b 0a 20 20 20 20 61 73  .u.pTerm;.    as
30028 73 65 72 74 28 20 70 54 65 72 6d 21 3d 30 20 29  sert( pTerm!=0 )
30029 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54  ;.    assert( pT
3002a 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 3d 3d  erm->eOperator==
3002b 57 4f 5f 4f 52 20 29 3b 0a 20 20 20 20 61 73 73  WO_OR );.    ass
3002c 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 77 74 46  ert( (pTerm->wtF
3002d 6c 61 67 73 20 26 20 54 45 52 4d 5f 4f 52 49 4e  lags & TERM_ORIN
3002e 46 4f 29 21 3d 30 20 29 3b 0a 20 20 20 20 70 4f  FO)!=0 );.    pO
3002f 72 57 63 20 3d 20 26 70 54 65 72 6d 2d 3e 75 2e  rWc = &pTerm->u.
30030 70 4f 72 49 6e 66 6f 2d 3e 77 63 3b 0a 20 20 20  pOrInfo->wc;.   
30031 20 70 46 69 6e 61 6c 20 3d 20 26 70 4f 72 57 63   pFinal = &pOrWc
30032 2d 3e 61 5b 70 4f 72 57 63 2d 3e 6e 54 65 72 6d  ->a[pOrWc->nTerm
30033 2d 31 5d 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 74  -1];..    /* Set
30034 20 75 70 20 61 20 53 72 63 4c 69 73 74 20 63 6f   up a SrcList co
30035 6e 74 61 69 6e 69 6e 67 20 6a 75 73 74 20 74 68  ntaining just th
30036 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 73 63  e table being sc
30037 61 6e 6e 65 64 20 62 79 20 74 68 69 73 20 6c 6f  anned by this lo
30038 6f 70 2e 20 2a 2f 0a 20 20 20 20 6f 6e 65 54 61  op. */.    oneTa
30039 62 2e 6e 53 72 63 20 3d 20 31 3b 0a 20 20 20 20  b.nSrc = 1;.    
3003a 6f 6e 65 54 61 62 2e 6e 41 6c 6c 6f 63 20 3d 20  oneTab.nAlloc = 
3003b 31 3b 0a 20 20 20 20 6f 6e 65 54 61 62 2e 61 5b  1;.    oneTab.a[
3003c 30 5d 20 3d 20 2a 70 54 61 62 49 74 65 6d 3b 0a  0] = *pTabItem;.
3003d 0a 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69  .    /* Initiali
3003e 7a 65 20 74 68 65 20 72 6f 77 73 65 74 20 72 65  ze the rowset re
3003f 67 69 73 74 65 72 20 74 6f 20 63 6f 6e 74 61 69  gister to contai
30040 6e 20 4e 55 4c 4c 2e 20 41 6e 20 53 51 4c 20 4e  n NULL. An SQL N
30041 55 4c 4c 20 69 73 20 0a 20 20 20 20 2a 2a 20 65  ULL is .    ** e
30042 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 61 6e 20  quivalent to an 
30043 65 6d 70 74 79 20 72 6f 77 73 65 74 2e 0a 20 20  empty rowset..  
30044 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 41 6c 73 6f    **.    ** Also
30045 20 69 6e 69 74 69 61 6c 69 7a 65 20 72 65 67 52   initialize regR
30046 65 74 75 72 6e 20 74 6f 20 63 6f 6e 74 61 69 6e  eturn to contain
30047 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20   the address of 
30048 74 68 65 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  the instruction 
30049 0a 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74  .    ** immediat
3004a 65 6c 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 68  ely following th
3004b 65 20 4f 50 5f 52 65 74 75 72 6e 20 61 74 20 74  e OP_Return at t
3004c 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74 68 65  he bottom of the
3004d 20 6c 6f 6f 70 2e 20 54 68 69 73 0a 20 20 20 20   loop. This.    
3004e 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 20 69  ** is required i
3004f 6e 20 61 20 66 65 77 20 6f 62 73 63 75 72 65 20  n a few obscure 
30050 4c 45 46 54 20 4a 4f 49 4e 20 63 61 73 65 73 20  LEFT JOIN cases 
30051 77 68 65 72 65 20 63 6f 6e 74 72 6f 6c 20 6a 75  where control ju
30052 6d 70 73 0a 20 20 20 20 2a 2a 20 6f 76 65 72 20  mps.    ** over 
30053 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 6c  the top of the l
30054 6f 6f 70 20 69 6e 74 6f 20 74 68 65 20 62 6f 64  oop into the bod
30055 79 20 6f 66 20 69 74 2e 20 49 6e 20 74 68 69 73  y of it. In this
30056 20 63 61 73 65 20 74 68 65 20 0a 20 20 20 20 2a   case the .    *
30057 2a 20 63 6f 72 72 65 63 74 20 72 65 73 70 6f 6e  * correct respon
30058 73 65 20 66 6f 72 20 74 68 65 20 65 6e 64 2d 6f  se for the end-o
30059 66 2d 6c 6f 6f 70 20 63 6f 64 65 20 28 74 68 65  f-loop code (the
3005a 20 4f 50 5f 52 65 74 75 72 6e 29 20 69 73 20 74   OP_Return) is t
3005b 6f 20 0a 20 20 20 20 2a 2a 20 66 61 6c 6c 20 74  o .    ** fall t
3005c 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65  hrough to the ne
3005d 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2c 20  xt instruction, 
3005e 6a 75 73 74 20 61 73 20 61 6e 20 4f 50 5f 4e 65  just as an OP_Ne
3005f 78 74 20 64 6f 65 73 20 69 66 0a 20 20 20 20 2a  xt does if.    *
30060 2a 20 63 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 75  * called on an u
30061 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 63 75 72  ninitialized cur
30062 73 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  sor..    */.    
30063 69 66 28 20 28 77 63 74 72 6c 46 6c 61 67 73 20  if( (wctrlFlags 
30064 26 20 57 48 45 52 45 5f 44 55 50 4c 49 43 41 54  & WHERE_DUPLICAT
30065 45 53 5f 4f 4b 29 3d 3d 30 20 29 7b 0a 20 20 20  ES_OK)==0 ){.   
30066 20 20 20 72 65 67 52 6f 77 73 65 74 20 3d 20 2b     regRowset = +
30067 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
30068 20 20 20 20 20 72 65 67 52 6f 77 69 64 20 3d 20       regRowid = 
30069 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
3006a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
3006b 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
3006c 6c 6c 2c 20 30 2c 20 72 65 67 52 6f 77 73 65 74  ll, 0, regRowset
3006d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 52 65  );.    }.    iRe
3006e 74 49 6e 69 74 20 3d 20 73 71 6c 69 74 65 33 56  tInit = sqlite3V
3006f 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
30070 49 6e 74 65 67 65 72 2c 20 30 2c 20 72 65 67 52  Integer, 0, regR
30071 65 74 75 72 6e 29 3b 0a 0a 20 20 20 20 66 6f 72  eturn);..    for
30072 28 69 69 3d 30 3b 20 69 69 3c 70 4f 72 57 63 2d  (ii=0; ii<pOrWc-
30073 3e 6e 54 65 72 6d 3b 20 69 69 2b 2b 29 7b 0a 20  >nTerm; ii++){. 
30074 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a       WhereTerm *
30075 70 4f 72 54 65 72 6d 20 3d 20 26 70 4f 72 57 63  pOrTerm = &pOrWc
30076 2d 3e 61 5b 69 69 5d 3b 0a 20 20 20 20 20 20 69  ->a[ii];.      i
30077 66 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74  f( pOrTerm->left
30078 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 7c 7c 20  Cursor==iCur || 
30079 70 4f 72 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74  pOrTerm->eOperat
3007a 6f 72 3d 3d 57 4f 5f 41 4e 44 20 29 7b 0a 20 20  or==WO_AND ){.  
3007b 20 20 20 20 20 20 57 68 65 72 65 49 6e 66 6f 20        WhereInfo 
3007c 2a 70 53 75 62 57 49 6e 66 6f 3b 20 20 20 20 20  *pSubWInfo;     
3007d 20 20 20 20 20 2f 2a 20 49 6e 66 6f 20 66 6f 72       /* Info for
3007e 20 73 69 6e 67 6c 65 20 4f 52 2d 74 65 72 6d 20   single OR-term 
3007f 73 63 61 6e 20 2a 2f 0a 20 20 20 20 20 20 20 20  scan */.        
30080 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20  /* Loop through 
30081 74 61 62 6c 65 20 65 6e 74 72 69 65 73 20 74 68  table entries th
30082 61 74 20 6d 61 74 63 68 20 74 65 72 6d 20 70 4f  at match term pO
30083 72 54 65 72 6d 2e 20 2a 2f 0a 20 20 20 20 20 20  rTerm. */.      
30084 20 20 70 53 75 62 57 49 6e 66 6f 20 3d 20 73 71    pSubWInfo = sq
30085 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28  lite3WhereBegin(
30086 70 50 61 72 73 65 2c 20 26 6f 6e 65 54 61 62 2c  pParse, &oneTab,
30087 20 70 4f 72 54 65 72 6d 2d 3e 70 45 78 70 72 2c   pOrTerm->pExpr,
30088 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
30089 20 20 20 20 20 20 20 20 20 20 20 20 57 48 45 52              WHER
3008a 45 5f 4f 4d 49 54 5f 4f 50 45 4e 20 7c 20 57 48  E_OMIT_OPEN | WH
3008b 45 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 20 7c  ERE_OMIT_CLOSE |
3008c 20 57 48 45 52 45 5f 46 4f 52 43 45 5f 54 41 42   WHERE_FORCE_TAB
3008d 4c 45 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  LE);.        if(
3008e 20 70 53 75 62 57 49 6e 66 6f 20 29 7b 0a 20 20   pSubWInfo ){.  
3008f 20 20 20 20 20 20 20 20 69 66 28 20 28 77 63 74          if( (wct
30090 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  rlFlags & WHERE_
30091 44 55 50 4c 49 43 41 54 45 53 5f 4f 4b 29 3d 3d  DUPLICATES_OK)==
30092 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
30093 20 69 6e 74 20 69 53 65 74 20 3d 20 28 28 69 69   int iSet = ((ii
30094 3d 3d 70 4f 72 57 63 2d 3e 6e 54 65 72 6d 2d 31  ==pOrWc->nTerm-1
30095 29 3f 2d 31 3a 69 69 29 3b 0a 20 20 20 20 20 20  )?-1:ii);.      
30096 20 20 20 20 20 20 69 6e 74 20 72 3b 0a 20 20 20        int r;.   
30097 20 20 20 20 20 20 20 20 20 72 20 3d 20 73 71 6c           r = sql
30098 69 74 65 33 45 78 70 72 43 6f 64 65 47 65 74 43  ite3ExprCodeGetC
30099 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 70 54  olumn(pParse, pT
3009a 61 62 49 74 65 6d 2d 3e 70 54 61 62 2c 20 2d 31  abItem->pTab, -1
3009b 2c 20 69 43 75 72 2c 20 0a 20 20 20 20 20 20 20  , iCur, .       
3009c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3009d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3009e 20 20 72 65 67 52 6f 77 69 64 2c 20 30 29 3b 0a    regRowid, 0);.
3009f 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
300a0 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
300a1 20 4f 50 5f 52 6f 77 53 65 74 54 65 73 74 2c 20   OP_RowSetTest, 
300a2 72 65 67 52 6f 77 73 65 74 2c 0a 20 20 20 20 20  regRowset,.     
300a3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
300a4 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
300a5 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
300a6 76 29 2b 32 2c 0a 20 20 20 20 20 20 20 20 20 20  v)+2,.          
300a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
300a8 20 20 20 20 72 2c 20 53 51 4c 49 54 45 5f 49 4e      r, SQLITE_IN
300a9 54 5f 54 4f 5f 50 54 52 28 69 53 65 74 29 2c 20  T_TO_PTR(iSet), 
300aa 50 34 5f 49 4e 54 33 32 29 3b 0a 20 20 20 20 20  P4_INT32);.     
300ab 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
300ac 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
300ad 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20  p2(v, OP_Gosub, 
300ae 72 65 67 52 65 74 75 72 6e 2c 20 69 4c 6f 6f 70  regReturn, iLoop
300af 42 6f 64 79 29 3b 0a 0a 20 20 20 20 20 20 20 20  Body);..        
300b0 20 20 2f 2a 20 46 69 6e 69 73 68 20 74 68 65 20    /* Finish the 
300b1 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 61 62  loop through tab
300b2 6c 65 20 65 6e 74 72 69 65 73 20 74 68 61 74 20  le entries that 
300b3 6d 61 74 63 68 20 74 65 72 6d 20 70 4f 72 54 65  match term pOrTe
300b4 72 6d 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  rm. */.         
300b5 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
300b6 28 70 53 75 62 57 49 6e 66 6f 29 3b 0a 20 20 20  (pSubWInfo);.   
300b7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
300b8 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
300b9 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
300ba 69 52 65 74 49 6e 69 74 2c 20 73 71 6c 69 74 65  iRetInit, sqlite
300bb 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
300bc 28 76 29 29 3b 0a 20 20 20 20 2f 2a 20 73 71 6c  (v));.    /* sql
300bd 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
300be 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65  , OP_Null, 0, re
300bf 67 52 6f 77 73 65 74 29 3b 20 2a 2f 0a 20 20 20  gRowset); */.   
300c0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
300c1 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
300c2 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72  , pLevel->addrBr
300c3 6b 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  k);.    sqlite3V
300c4 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
300c5 76 2c 20 69 4c 6f 6f 70 42 6f 64 79 29 3b 0a 0a  v, iLoopBody);..
300c6 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d      pLevel->op =
300c7 20 4f 50 5f 52 65 74 75 72 6e 3b 0a 20 20 20 20   OP_Return;.    
300c8 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 72 65 67  pLevel->p1 = reg
300c9 52 65 74 75 72 6e 3b 0a 20 20 20 20 64 69 73 61  Return;.    disa
300ca 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20  bleTerm(pLevel, 
300cb 70 54 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 0a  pTerm);.  }else.
300cc 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
300cd 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a  _OMIT_OR_OPTIMIZ
300ce 41 54 49 4f 4e 20 2a 2f 0a 0a 20 20 7b 0a 20 20  ATION */..  {.  
300cf 20 20 2f 2a 20 43 61 73 65 20 35 3a 20 20 54 68    /* Case 5:  Th
300d0 65 72 65 20 69 73 20 6e 6f 20 75 73 61 62 6c 65  ere is no usable
300d1 20 69 6e 64 65 78 2e 20 20 57 65 20 6d 75 73 74   index.  We must
300d2 20 64 6f 20 61 20 63 6f 6d 70 6c 65 74 65 0a 20   do a complete. 
300d3 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 73     **          s
300d4 63 61 6e 20 6f 66 20 74 68 65 20 65 6e 74 69 72  can of the entir
300d5 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a  e table..    */.
300d6 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74      static const
300d7 20 75 38 20 61 53 74 65 70 5b 5d 20 3d 20 7b 20   u8 aStep[] = { 
300d8 4f 50 5f 4e 65 78 74 2c 20 4f 50 5f 50 72 65 76  OP_Next, OP_Prev
300d9 20 7d 3b 0a 20 20 20 20 73 74 61 74 69 63 20 63   };.    static c
300da 6f 6e 73 74 20 75 38 20 61 53 74 61 72 74 5b 5d  onst u8 aStart[]
300db 20 3d 20 7b 20 4f 50 5f 52 65 77 69 6e 64 2c 20   = { OP_Rewind, 
300dc 4f 50 5f 4c 61 73 74 20 7d 3b 0a 20 20 20 20 61  OP_Last };.    a
300dd 73 73 65 72 74 28 20 62 52 65 76 3d 3d 30 20 7c  ssert( bRev==0 |
300de 7c 20 62 52 65 76 3d 3d 31 20 29 3b 0a 20 20 20  | bRev==1 );.   
300df 20 61 73 73 65 72 74 28 20 6f 6d 69 74 54 61 62   assert( omitTab
300e0 6c 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70 4c 65  le==0 );.    pLe
300e1 76 65 6c 2d 3e 6f 70 20 3d 20 61 53 74 65 70 5b  vel->op = aStep[
300e2 62 52 65 76 5d 3b 0a 20 20 20 20 70 4c 65 76 65  bRev];.    pLeve
300e3 6c 2d 3e 70 31 20 3d 20 69 43 75 72 3b 0a 20 20  l->p1 = iCur;.  
300e4 20 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d 20 31    pLevel->p2 = 1
300e5 20 2b 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   + sqlite3VdbeAd
300e6 64 4f 70 32 28 76 2c 20 61 53 74 61 72 74 5b 62  dOp2(v, aStart[b
300e7 52 65 76 5d 2c 20 69 43 75 72 2c 20 61 64 64 72  Rev], iCur, addr
300e8 42 72 6b 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c  Brk);.    pLevel
300e9 2d 3e 70 35 20 3d 20 53 51 4c 49 54 45 5f 53 54  ->p5 = SQLITE_ST
300ea 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 43 41  MTSTATUS_FULLSCA
300eb 4e 5f 53 54 45 50 3b 0a 20 20 7d 0a 20 20 6e 6f  N_STEP;.  }.  no
300ec 74 52 65 61 64 79 20 26 3d 20 7e 67 65 74 4d 61  tReady &= ~getMa
300ed 73 6b 28 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74  sk(pWC->pMaskSet
300ee 2c 20 69 43 75 72 29 3b 0a 0a 20 20 2f 2a 20 49  , iCur);..  /* I
300ef 6e 73 65 72 74 20 63 6f 64 65 20 74 6f 20 74 65  nsert code to te
300f0 73 74 20 65 76 65 72 79 20 73 75 62 65 78 70 72  st every subexpr
300f1 65 73 73 69 6f 6e 20 74 68 61 74 20 63 61 6e 20  ession that can 
300f2 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 20 20  be completely.  
300f3 2a 2a 20 63 6f 6d 70 75 74 65 64 20 75 73 69 6e  ** computed usin
300f4 67 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 65  g the current se
300f5 74 20 6f 66 20 74 61 62 6c 65 73 2e 0a 20 20 2a  t of tables..  *
300f6 2f 0a 20 20 6b 20 3d 20 30 3b 0a 20 20 66 6f 72  /.  k = 0;.  for
300f7 28 70 54 65 72 6d 3d 70 57 43 2d 3e 61 2c 20 6a  (pTerm=pWC->a, j
300f8 3d 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a 3e 30  =pWC->nTerm; j>0
300f9 3b 20 6a 2d 2d 2c 20 70 54 65 72 6d 2b 2b 29 7b  ; j--, pTerm++){
300fa 0a 20 20 20 20 45 78 70 72 20 2a 70 45 3b 0a 20  .    Expr *pE;. 
300fb 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65     testcase( pTe
300fc 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45  rm->wtFlags & TE
300fd 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b 0a 20 20  RM_VIRTUAL );.  
300fe 20 20 74 65 73 74 63 61 73 65 28 20 70 54 65 72    testcase( pTer
300ff 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54 45 52  m->wtFlags & TER
30100 4d 5f 43 4f 44 45 44 20 29 3b 0a 20 20 20 20 69  M_CODED );.    i
30101 66 28 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67  f( pTerm->wtFlag
30102 73 20 26 20 28 54 45 52 4d 5f 56 49 52 54 55 41  s & (TERM_VIRTUA
30103 4c 7c 54 45 52 4d 5f 43 4f 44 45 44 29 20 29 20  L|TERM_CODED) ) 
30104 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66  continue;.    if
30105 28 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71  ( (pTerm->prereq
30106 41 6c 6c 20 26 20 6e 6f 74 52 65 61 64 79 29 21  All & notReady)!
30107 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =0 ) continue;. 
30108 20 20 20 70 45 20 3d 20 70 54 65 72 6d 2d 3e 70     pE = pTerm->p
30109 45 78 70 72 3b 0a 20 20 20 20 61 73 73 65 72 74  Expr;.    assert
3010a 28 20 70 45 21 3d 30 20 29 3b 0a 20 20 20 20 69  ( pE!=0 );.    i
3010b 66 28 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74  f( pLevel->iLeft
3010c 4a 6f 69 6e 20 26 26 20 21 45 78 70 72 48 61 73  Join && !ExprHas
3010d 50 72 6f 70 65 72 74 79 28 70 45 2c 20 45 50 5f  Property(pE, EP_
3010e 46 72 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20  FromJoin) ){.   
3010f 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
30110 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78   }.    sqlite3Ex
30111 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65  prIfFalse(pParse
30112 2c 20 70 45 2c 20 61 64 64 72 43 6f 6e 74 2c 20  , pE, addrCont, 
30113 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
30114 4c 29 3b 0a 20 20 20 20 6b 20 3d 20 31 3b 0a 20  L);.    k = 1;. 
30115 20 20 20 70 54 65 72 6d 2d 3e 77 74 46 6c 61 67     pTerm->wtFlag
30116 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 44 45 44 3b  s |= TERM_CODED;
30117 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f 72 20 61  .  }..  /* For a
30118 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e   LEFT OUTER JOIN
30119 2c 20 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20  , generate code 
3011a 74 68 61 74 20 77 69 6c 6c 20 72 65 63 6f 72 64  that will record
3011b 20 74 68 65 20 66 61 63 74 20 74 68 61 74 0a 20   the fact that. 
3011c 20 2a 2a 20 61 74 20 6c 65 61 73 74 20 6f 6e 65   ** at least one
3011d 20 72 6f 77 20 6f 66 20 74 68 65 20 72 69 67 68   row of the righ
3011e 74 20 74 61 62 6c 65 20 68 61 73 20 6d 61 74 63  t table has matc
3011f 68 65 64 20 74 68 65 20 6c 65 66 74 20 74 61 62  hed the left tab
30120 6c 65 2e 20 20 0a 20 20 2a 2f 0a 20 20 69 66 28  le.  .  */.  if(
30121 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f   pLevel->iLeftJo
30122 69 6e 20 29 7b 0a 20 20 20 20 70 4c 65 76 65 6c  in ){.    pLevel
30123 2d 3e 61 64 64 72 46 69 72 73 74 20 3d 20 73 71  ->addrFirst = sq
30124 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
30125 41 64 64 72 28 76 29 3b 0a 20 20 20 20 73 71 6c  Addr(v);.    sql
30126 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
30127 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c  , OP_Integer, 1,
30128 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f   pLevel->iLeftJo
30129 69 6e 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d  in);.    VdbeCom
3012a 6d 65 6e 74 28 28 76 2c 20 22 72 65 63 6f 72 64  ment((v, "record
3012b 20 4c 45 46 54 20 4a 4f 49 4e 20 68 69 74 22 29   LEFT JOIN hit")
3012c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  );.    sqlite3Ex
3012d 70 72 43 61 63 68 65 43 6c 65 61 72 28 70 50 61  prCacheClear(pPa
3012e 72 73 65 29 3b 0a 20 20 20 20 66 6f 72 28 70 54  rse);.    for(pT
3012f 65 72 6d 3d 70 57 43 2d 3e 61 2c 20 6a 3d 30 3b  erm=pWC->a, j=0;
30130 20 6a 3c 70 57 43 2d 3e 6e 54 65 72 6d 3b 20 6a   j<pWC->nTerm; j
30131 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a 20 20  ++, pTerm++){.  
30132 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 54      testcase( pT
30133 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20 54  erm->wtFlags & T
30134 45 52 4d 5f 56 49 52 54 55 41 4c 20 29 3b 0a 20  ERM_VIRTUAL );. 
30135 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
30136 54 65 72 6d 2d 3e 77 74 46 6c 61 67 73 20 26 20  Term->wtFlags & 
30137 54 45 52 4d 5f 43 4f 44 45 44 20 29 3b 0a 20 20  TERM_CODED );.  
30138 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e 77      if( pTerm->w
30139 74 46 6c 61 67 73 20 26 20 28 54 45 52 4d 5f 56  tFlags & (TERM_V
3013a 49 52 54 55 41 4c 7c 54 45 52 4d 5f 43 4f 44 45  IRTUAL|TERM_CODE
3013b 44 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  D) ) continue;. 
3013c 20 20 20 20 20 69 66 28 20 28 70 54 65 72 6d 2d       if( (pTerm-
3013d 3e 70 72 65 72 65 71 41 6c 6c 20 26 20 6e 6f 74  >prereqAll & not
3013e 52 65 61 64 79 29 21 3d 30 20 29 20 63 6f 6e 74  Ready)!=0 ) cont
3013f 69 6e 75 65 3b 0a 20 20 20 20 20 20 61 73 73 65  inue;.      asse
30140 72 74 28 20 70 54 65 72 6d 2d 3e 70 45 78 70 72  rt( pTerm->pExpr
30141 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
30142 33 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61  3ExprIfFalse(pPa
30143 72 73 65 2c 20 70 54 65 72 6d 2d 3e 70 45 78 70  rse, pTerm->pExp
30144 72 2c 20 61 64 64 72 43 6f 6e 74 2c 20 53 51 4c  r, addrCont, SQL
30145 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b  ITE_JUMPIFNULL);
30146 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 77 74  .      pTerm->wt
30147 46 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f  Flags |= TERM_CO
30148 44 45 44 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  DED;.    }.  }. 
30149 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
3014a 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 69  empReg(pParse, i
3014b 52 65 6c 65 61 73 65 52 65 67 29 3b 0a 0a 20 20  ReleaseReg);..  
3014c 72 65 74 75 72 6e 20 6e 6f 74 52 65 61 64 79 3b  return notReady;
3014d 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .}..#if defined(
3014e 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 2f 2a 0a  SQLITE_TEST)./*.
3014f 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
30150 20 76 61 72 69 61 62 6c 65 20 68 6f 6c 64 73 20   variable holds 
30151 61 20 74 65 78 74 20 64 65 73 63 72 69 70 74 69  a text descripti
30152 6f 6e 20 6f 66 20 71 75 65 72 79 20 70 6c 61 6e  on of query plan
30153 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 62 79   generated.** by
30154 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
30155 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
30156 57 68 65 72 65 42 65 67 69 6e 28 29 2e 20 20 45  WhereBegin().  E
30157 61 63 68 20 63 61 6c 6c 20 74 6f 20 57 68 65 72  ach call to Wher
30158 65 42 65 67 69 6e 0a 2a 2a 20 6f 76 65 72 77 72  eBegin.** overwr
30159 69 74 65 73 20 74 68 65 20 70 72 65 76 69 6f 75  ites the previou
3015a 73 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61  s.  This informa
3015b 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72  tion is used for
3015c 20 74 65 73 74 69 6e 67 20 61 6e 64 0a 2a 2a 20   testing and.** 
3015d 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 0a 2a  analysis only..*
3015e 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61  /.SQLITE_API cha
3015f 72 20 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f  r sqlite3_query_
30160 70 6c 61 6e 5b 42 4d 53 2a 32 2a 34 30 5d 3b 20  plan[BMS*2*40]; 
30161 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20   /* Text of the 
30162 6a 6f 69 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69  join */.static i
30163 6e 74 20 6e 51 50 6c 61 6e 20 3d 20 30 3b 20 20  nt nQPlan = 0;  
30164 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
30165 65 78 74 20 66 72 65 65 20 73 6c 6f 77 20 69 6e  ext free slow in
30166 20 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 5d 20 2a   _query_plan[] *
30167 2f 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  /..#endif /* SQL
30168 49 54 45 5f 54 45 53 54 20 2a 2f 0a 0a 0a 2f 2a  ITE_TEST */.../*
30169 0a 2a 2a 20 46 72 65 65 20 61 20 57 68 65 72 65  .** Free a Where
3016a 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a 2a  Info structure.*
3016b 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 68  /.static void wh
3016c 65 72 65 49 6e 66 6f 46 72 65 65 28 73 71 6c 69  ereInfoFree(sqli
3016d 74 65 33 20 2a 64 62 2c 20 57 68 65 72 65 49 6e  te3 *db, WhereIn
3016e 66 6f 20 2a 70 57 49 6e 66 6f 29 7b 0a 20 20 69  fo *pWInfo){.  i
3016f 66 28 20 70 57 49 6e 66 6f 20 29 7b 0a 20 20 20  f( pWInfo ){.   
30170 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28   int i;.    for(
30171 69 3d 30 3b 20 69 3c 70 57 49 6e 66 6f 2d 3e 6e  i=0; i<pWInfo->n
30172 4c 65 76 65 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  Level; i++){.   
30173 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78     sqlite3_index
30174 5f 69 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 70  _info *pInfo = p
30175 57 49 6e 66 6f 2d 3e 61 5b 69 5d 2e 70 49 64 78  WInfo->a[i].pIdx
30176 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 20  Info;.      if( 
30177 70 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 20  pInfo ){.       
30178 20 2f 2a 20 61 73 73 65 72 74 28 20 70 49 6e 66   /* assert( pInf
30179 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64 78  o->needToFreeIdx
3017a 53 74 72 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61  Str==0 || db->ma
3017b 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 20 2a 2f  llocFailed ); */
3017c 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 6e  .        if( pIn
3017d 66 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49 64  fo->needToFreeId
3017e 78 53 74 72 20 29 7b 0a 20 20 20 20 20 20 20 20  xStr ){.        
3017f 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
30180 49 6e 66 6f 2d 3e 69 64 78 53 74 72 29 3b 0a 20  Info->idxStr);. 
30181 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
30182 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
30183 62 2c 20 70 49 6e 66 6f 29 3b 0a 20 20 20 20 20  b, pInfo);.     
30184 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 65   }.    }.    whe
30185 72 65 43 6c 61 75 73 65 43 6c 65 61 72 28 70 57  reClauseClear(pW
30186 49 6e 66 6f 2d 3e 70 57 43 29 3b 0a 20 20 20 20  Info->pWC);.    
30187 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
30188 2c 20 70 57 49 6e 66 6f 29 3b 0a 20 20 7d 0a 7d  , pWInfo);.  }.}
30189 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  .../*.** Generat
3018a 65 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20  e the beginning 
3018b 6f 66 20 74 68 65 20 6c 6f 6f 70 20 75 73 65 64  of the loop used
3018c 20 66 6f 72 20 57 48 45 52 45 20 63 6c 61 75 73   for WHERE claus
3018d 65 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a  e processing..**
3018e 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
3018f 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  e is a pointer t
30190 6f 20 61 6e 20 6f 70 61 71 75 65 20 73 74 72 75  o an opaque stru
30191 63 74 75 72 65 20 74 68 61 74 20 63 6f 6e 74 61  cture that conta
30192 69 6e 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  ins.** informati
30193 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 74 65 72  on needed to ter
30194 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f 6f 70 2e  minate the loop.
30195 20 20 4c 61 74 65 72 2c 20 74 68 65 20 63 61 6c    Later, the cal
30196 6c 69 6e 67 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  ling routine.** 
30197 73 68 6f 75 6c 64 20 69 6e 76 6f 6b 65 20 73 71  should invoke sq
30198 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 29 20  lite3WhereEnd() 
30199 77 69 74 68 20 74 68 65 20 72 65 74 75 72 6e 20  with the return 
3019a 76 61 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75  value of this fu
3019b 6e 63 74 69 6f 6e 0a 2a 2a 20 69 6e 20 6f 72 64  nction.** in ord
3019c 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74  er to complete t
3019d 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
3019e 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a 2a 0a 2a  processing..**.*
3019f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
301a0 63 75 72 73 2c 20 74 68 69 73 20 72 6f 75 74 69  curs, this routi
301a1 6e 65 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e  ne returns NULL.
301a2 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73 69 63  .**.** The basic
301a3 20 69 64 65 61 20 69 73 20 74 6f 20 64 6f 20 61   idea is to do a
301a4 20 6e 65 73 74 65 64 20 6c 6f 6f 70 2c 20 6f 6e   nested loop, on
301a5 65 20 6c 6f 6f 70 20 66 6f 72 20 65 61 63 68 20  e loop for each 
301a6 74 61 62 6c 65 20 69 6e 0a 2a 2a 20 74 68 65 20  table in.** the 
301a7 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 61  FROM clause of a
301a8 20 73 65 6c 65 63 74 2e 20 20 28 49 4e 53 45 52   select.  (INSER
301a9 54 20 61 6e 64 20 55 50 44 41 54 45 20 73 74 61  T and UPDATE sta
301aa 74 65 6d 65 6e 74 73 20 61 72 65 20 74 68 65 0a  tements are the.
301ab 2a 2a 20 73 61 6d 65 20 61 73 20 61 20 53 45 4c  ** same as a SEL
301ac 45 43 54 20 77 69 74 68 20 6f 6e 6c 79 20 61 20  ECT with only a 
301ad 73 69 6e 67 6c 65 20 74 61 62 6c 65 20 69 6e 20  single table in 
301ae 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e  the FROM clause.
301af 29 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c  )  For.** exampl
301b0 65 2c 20 69 66 20 74 68 65 20 53 51 4c 20 69 73  e, if the SQL is
301b1 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   this:.**.**    
301b2 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d     SELECT * FROM
301b3 20 74 31 2c 20 74 32 2c 20 74 33 20 57 48 45 52   t1, t2, t3 WHER
301b4 45 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20 54 68 65  E ...;.**.** The
301b5 6e 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72  n the code gener
301b6 61 74 65 64 20 69 73 20 63 6f 6e 63 65 70 74 75  ated is conceptu
301b7 61 6c 6c 79 20 6c 69 6b 65 20 74 68 65 20 66 6f  ally like the fo
301b8 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  llowing:.**.**  
301b9 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77 31      foreach row1
301ba 20 69 6e 20 74 31 20 64 6f 20 20 20 20 20 20 20   in t1 do       
301bb 5c 20 20 20 20 43 6f 64 65 20 67 65 6e 65 72 61  \    Code genera
301bc 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 66 6f  ted.**        fo
301bd 72 65 61 63 68 20 72 6f 77 32 20 69 6e 20 74 32  reach row2 in t2
301be 20 64 6f 20 20 20 20 20 20 7c 2d 2d 20 62 79 20   do      |-- by 
301bf 73 71 6c 69 74 65 33 57 68 65 72 65 42 65 67 69  sqlite3WhereBegi
301c0 6e 28 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  n().**          
301c1 66 6f 72 65 61 63 68 20 72 6f 77 33 20 69 6e 20  foreach row3 in 
301c2 74 33 20 64 6f 20 20 20 2f 0a 2a 2a 20 20 20 20  t3 do   /.**    
301c3 20 20 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20          ....**  
301c4 20 20 20 20 20 20 20 20 65 6e 64 20 20 20 20 20          end     
301c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
301c6 5c 20 20 20 20 43 6f 64 65 20 67 65 6e 65 72 61  \    Code genera
301c7 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 65 6e  ted.**        en
301c8 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
301c9 20 20 20 20 20 20 20 20 20 7c 2d 2d 20 62 79 20           |-- by 
301ca 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28  sqlite3WhereEnd(
301cb 29 0a 2a 2a 20 20 20 20 20 20 65 6e 64 20 20 20  ).**      end   
301cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
301cd 20 20 20 20 20 20 2f 0a 2a 2a 0a 2a 2a 20 4e 6f        /.**.** No
301ce 74 65 20 74 68 61 74 20 74 68 65 20 6c 6f 6f 70  te that the loop
301cf 73 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 6e  s might not be n
301d0 65 73 74 65 64 20 69 6e 20 74 68 65 20 6f 72 64  ested in the ord
301d1 65 72 20 69 6e 20 77 68 69 63 68 20 74 68 65 79  er in which they
301d2 0a 2a 2a 20 61 70 70 65 61 72 20 69 6e 20 74 68  .** appear in th
301d3 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 66  e FROM clause if
301d4 20 61 20 64 69 66 66 65 72 65 6e 74 20 6f 72 64   a different ord
301d5 65 72 20 69 73 20 62 65 74 74 65 72 20 61 62 6c  er is better abl
301d6 65 20 74 6f 20 6d 61 6b 65 0a 2a 2a 20 75 73 65  e to make.** use
301d7 20 6f 66 20 69 6e 64 69 63 65 73 2e 20 20 4e 6f   of indices.  No
301d8 74 65 20 61 6c 73 6f 20 74 68 61 74 20 77 68 65  te also that whe
301d9 6e 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74 6f  n the IN operato
301da 72 20 61 70 70 65 61 72 73 20 69 6e 0a 2a 2a 20  r appears in.** 
301db 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
301dc 2c 20 69 74 20 6d 69 67 68 74 20 72 65 73 75 6c  , it might resul
301dd 74 20 69 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20  t in additional 
301de 6e 65 73 74 65 64 20 6c 6f 6f 70 73 20 66 6f 72  nested loops for
301df 0a 2a 2a 20 73 63 61 6e 6e 69 6e 67 20 74 68 72  .** scanning thr
301e0 6f 75 67 68 20 61 6c 6c 20 76 61 6c 75 65 73 20  ough all values 
301e1 6f 6e 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e  on the right-han
301e2 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 49 4e  d side of the IN
301e3 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72  ..**.** There ar
301e4 65 20 42 74 72 65 65 20 63 75 72 73 6f 72 73 20  e Btree cursors 
301e5 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
301e6 65 61 63 68 20 74 61 62 6c 65 2e 20 20 74 31 20  each table.  t1 
301e7 75 73 65 73 20 63 75 72 73 6f 72 0a 2a 2a 20 6e  uses cursor.** n
301e8 75 6d 62 65 72 20 70 54 61 62 4c 69 73 74 2d 3e  umber pTabList->
301e9 61 5b 30 5d 2e 69 43 75 72 73 6f 72 2e 20 20 74  a[0].iCursor.  t
301ea 32 20 75 73 65 73 20 74 68 65 20 63 75 72 73 6f  2 uses the curso
301eb 72 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 31 5d  r pTabList->a[1]
301ec 2e 69 43 75 72 73 6f 72 2e 0a 2a 2a 20 41 6e 64  .iCursor..** And
301ed 20 73 6f 20 66 6f 72 74 68 2e 20 20 54 68 69 73   so forth.  This
301ee 20 72 6f 75 74 69 6e 65 20 67 65 6e 65 72 61 74   routine generat
301ef 65 73 20 63 6f 64 65 20 74 6f 20 6f 70 65 6e 20  es code to open 
301f0 74 68 6f 73 65 20 56 44 42 45 20 63 75 72 73 6f  those VDBE curso
301f1 72 73 0a 2a 2a 20 61 6e 64 20 73 71 6c 69 74 65  rs.** and sqlite
301f2 33 57 68 65 72 65 45 6e 64 28 29 20 67 65 6e 65  3WhereEnd() gene
301f3 72 61 74 65 73 20 74 68 65 20 63 6f 64 65 20 74  rates the code t
301f4 6f 20 63 6c 6f 73 65 20 74 68 65 6d 2e 0a 2a 2a  o close them..**
301f5 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 68 61  .** The code tha
301f6 74 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  t sqlite3WhereBe
301f7 67 69 6e 28 29 20 67 65 6e 65 72 61 74 65 73 20  gin() generates 
301f8 6c 65 61 76 65 73 20 74 68 65 20 63 75 72 73 6f  leaves the curso
301f9 72 73 20 6e 61 6d 65 64 0a 2a 2a 20 69 6e 20 70  rs named.** in p
301fa 54 61 62 4c 69 73 74 20 70 6f 69 6e 74 69 6e 67  TabList pointing
301fb 20 61 74 20 74 68 65 69 72 20 61 70 70 72 6f 70   at their approp
301fc 72 69 61 74 65 20 65 6e 74 72 69 65 73 2e 20 20  riate entries.  
301fd 54 68 65 20 5b 2e 2e 2e 5d 20 63 6f 64 65 0a 2a  The [...] code.*
301fe 2a 20 63 61 6e 20 75 73 65 20 4f 50 5f 43 6f 6c  * can use OP_Col
301ff 75 6d 6e 20 61 6e 64 20 4f 50 5f 52 6f 77 69 64  umn and OP_Rowid
30200 20 6f 70 63 6f 64 65 73 20 6f 6e 20 74 68 65 73   opcodes on thes
30201 65 20 63 75 72 73 6f 72 73 20 74 6f 20 65 78 74  e cursors to ext
30202 72 61 63 74 0a 2a 2a 20 64 61 74 61 20 66 72 6f  ract.** data fro
30203 6d 20 74 68 65 20 76 61 72 69 6f 75 73 20 74 61  m the various ta
30204 62 6c 65 73 20 6f 66 20 74 68 65 20 6c 6f 6f 70  bles of the loop
30205 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 57  ..**.** If the W
30206 48 45 52 45 20 63 6c 61 75 73 65 20 69 73 20 65  HERE clause is e
30207 6d 70 74 79 2c 20 74 68 65 20 66 6f 72 65 61 63  mpty, the foreac
30208 68 20 6c 6f 6f 70 73 20 6d 75 73 74 20 65 61 63  h loops must eac
30209 68 20 73 63 61 6e 20 74 68 65 69 72 0a 2a 2a 20  h scan their.** 
3020a 65 6e 74 69 72 65 20 74 61 62 6c 65 73 2e 20 20  entire tables.  
3020b 54 68 75 73 20 61 20 74 68 72 65 65 2d 77 61 79  Thus a three-way
3020c 20 6a 6f 69 6e 20 69 73 20 61 6e 20 4f 28 4e 5e   join is an O(N^
3020d 33 29 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42  3) operation.  B
3020e 75 74 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 62  ut if.** the tab
3020f 6c 65 73 20 68 61 76 65 20 69 6e 64 69 63 65 73  les have indices
30210 20 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 74   and there are t
30211 65 72 6d 73 20 69 6e 20 74 68 65 20 57 48 45 52  erms in the WHER
30212 45 20 63 6c 61 75 73 65 20 74 68 61 74 0a 2a 2a  E clause that.**
30213 20 72 65 66 65 72 20 74 6f 20 74 68 6f 73 65 20   refer to those 
30214 69 6e 64 69 63 65 73 2c 20 61 20 63 6f 6d 70 6c  indices, a compl
30215 65 74 65 20 74 61 62 6c 65 20 73 63 61 6e 20 63  ete table scan c
30216 61 6e 20 62 65 20 61 76 6f 69 64 65 64 20 61 6e  an be avoided an
30217 64 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20 77 69  d the.** code wi
30218 6c 6c 20 72 75 6e 20 6d 75 63 68 20 66 61 73 74  ll run much fast
30219 65 72 2e 20 20 4d 6f 73 74 20 6f 66 20 74 68 65  er.  Most of the
3021a 20 77 6f 72 6b 20 6f 66 20 74 68 69 73 20 72 6f   work of this ro
3021b 75 74 69 6e 65 20 69 73 20 63 68 65 63 6b 69 6e  utine is checkin
3021c 67 0a 2a 2a 20 74 6f 20 73 65 65 20 69 66 20 74  g.** to see if t
3021d 68 65 72 65 20 61 72 65 20 69 6e 64 69 63 65 73  here are indices
3021e 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65   that can be use
3021f 64 20 74 6f 20 73 70 65 65 64 20 75 70 20 74 68  d to speed up th
30220 65 20 6c 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 54 65  e loop..**.** Te
30221 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52 45  rms of the WHERE
30222 20 63 6c 61 75 73 65 20 61 72 65 20 61 6c 73 6f   clause are also
30223 20 75 73 65 64 20 74 6f 20 6c 69 6d 69 74 20 77   used to limit w
30224 68 69 63 68 20 72 6f 77 73 20 61 63 74 75 61 6c  hich rows actual
30225 6c 79 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 74 6f  ly.** make it to
30226 20 74 68 65 20 22 2e 2e 2e 22 20 69 6e 20 74 68   the "..." in th
30227 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 68 65 20  e middle of the 
30228 6c 6f 6f 70 2e 20 20 41 66 74 65 72 20 65 61 63  loop.  After eac
30229 68 20 22 66 6f 72 65 61 63 68 22 2c 0a 2a 2a 20  h "foreach",.** 
3022a 74 65 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45  terms of the WHE
3022b 52 45 20 63 6c 61 75 73 65 20 74 68 61 74 20 75  RE clause that u
3022c 73 65 20 6f 6e 6c 79 20 74 65 72 6d 73 20 69 6e  se only terms in
3022d 20 74 68 61 74 20 6c 6f 6f 70 20 61 6e 64 20 6f   that loop and o
3022e 75 74 65 72 0a 2a 2a 20 6c 6f 6f 70 73 20 61 72  uter.** loops ar
3022f 65 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20  e evaluated and 
30230 69 66 20 66 61 6c 73 65 20 61 20 6a 75 6d 70 20  if false a jump 
30231 69 73 20 6d 61 64 65 20 61 72 6f 75 6e 64 20 61  is made around a
30232 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a  ll subsequent.**
30233 20 69 6e 6e 65 72 20 6c 6f 6f 70 73 20 28 6f 72   inner loops (or
30234 20 61 72 6f 75 6e 64 20 74 68 65 20 22 2e 2e 2e   around the "...
30235 22 20 69 66 20 74 68 65 20 74 65 73 74 20 6f 63  " if the test oc
30236 63 75 72 73 20 77 69 74 68 69 6e 20 74 68 65 20  curs within the 
30237 69 6e 6e 65 72 2d 0a 2a 2a 20 6d 6f 73 74 20 6c  inner-.** most l
30238 6f 6f 70 29 0a 2a 2a 0a 2a 2a 20 4f 55 54 45 52  oop).**.** OUTER
30239 20 4a 4f 49 4e 53 0a 2a 2a 0a 2a 2a 20 41 6e 20   JOINS.**.** An 
3023a 6f 75 74 65 72 20 6a 6f 69 6e 20 6f 66 20 74 61  outer join of ta
3023b 62 6c 65 73 20 74 31 20 61 6e 64 20 74 32 20 69  bles t1 and t2 i
3023c 73 20 63 6f 6e 63 65 70 74 61 6c 6c 79 20 63 6f  s conceptally co
3023d 64 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  ded as follows:.
3023e 2a 2a 0a 2a 2a 20 20 20 20 66 6f 72 65 61 63 68  **.**    foreach
3023f 20 72 6f 77 31 20 69 6e 20 74 31 20 64 6f 0a 2a   row1 in t1 do.*
30240 2a 20 20 20 20 20 20 66 6c 61 67 20 3d 20 30 0a  *      flag = 0.
30241 2a 2a 20 20 20 20 20 20 66 6f 72 65 61 63 68 20  **      foreach 
30242 72 6f 77 32 20 69 6e 20 74 32 20 64 6f 0a 2a 2a  row2 in t2 do.**
30243 20 20 20 20 20 20 20 20 73 74 61 72 74 3a 0a 2a          start:.*
30244 2a 20 20 20 20 20 20 20 20 20 20 2e 2e 2e 0a 2a  *          ....*
30245 2a 20 20 20 20 20 20 20 20 20 20 66 6c 61 67 20  *          flag 
30246 3d 20 31 0a 2a 2a 20 20 20 20 20 20 65 6e 64 0a  = 1.**      end.
30247 2a 2a 20 20 20 20 20 20 69 66 20 66 6c 61 67 3d  **      if flag=
30248 3d 30 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20  =0 then.**      
30249 20 20 6d 6f 76 65 20 74 68 65 20 72 6f 77 32 20    move the row2 
3024a 63 75 72 73 6f 72 20 74 6f 20 61 20 6e 75 6c 6c  cursor to a null
3024b 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20 67   row.**        g
3024c 6f 74 6f 20 73 74 61 72 74 0a 2a 2a 20 20 20 20  oto start.**    
3024d 20 20 66 69 0a 2a 2a 20 20 20 20 65 6e 64 0a 2a    fi.**    end.*
3024e 2a 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 43 4c  *.** ORDER BY CL
3024f 41 55 53 45 20 50 52 4f 43 45 53 53 49 4e 47 0a  AUSE PROCESSING.
30250 2a 2a 0a 2a 2a 20 2a 70 70 4f 72 64 65 72 42 79  **.** *ppOrderBy
30251 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
30252 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
30253 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43 54  ause of a SELECT
30254 20 73 74 61 74 65 6d 65 6e 74 2c 0a 2a 2a 20 69   statement,.** i
30255 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 2e 20  f there is one. 
30256 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
30257 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
30258 6f 72 20 69 66 20 74 68 69 73 20 72 6f 75 74 69  or if this routi
30259 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20  ne.** is called 
3025a 66 72 6f 6d 20 61 6e 20 55 50 44 41 54 45 20 6f  from an UPDATE o
3025b 72 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65  r DELETE stateme
3025c 6e 74 2c 20 74 68 65 6e 20 70 70 4f 72 64 65 72  nt, then ppOrder
3025d 42 79 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  By is NULL..**.*
3025e 2a 20 49 66 20 61 6e 20 69 6e 64 65 78 20 63 61  * If an index ca
3025f 6e 20 62 65 20 75 73 65 64 20 73 6f 20 74 68 61  n be used so tha
30260 74 20 74 68 65 20 6e 61 74 75 72 61 6c 20 6f 75  t the natural ou
30261 74 70 75 74 20 6f 72 64 65 72 20 6f 66 20 74 68  tput order of th
30262 65 20 74 61 62 6c 65 0a 2a 2a 20 73 63 61 6e 20  e table.** scan 
30263 69 73 20 63 6f 72 72 65 63 74 20 66 6f 72 20 74  is correct for t
30264 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
30265 73 65 2c 20 74 68 65 6e 20 74 68 61 74 20 69 6e  se, then that in
30266 64 65 78 20 69 73 20 75 73 65 64 20 61 6e 64 0a  dex is used and.
30267 2a 2a 20 2a 70 70 4f 72 64 65 72 42 79 20 69 73  ** *ppOrderBy is
30268 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 20 54   set to NULL.  T
30269 68 69 73 20 69 73 20 61 6e 20 6f 70 74 69 6d 69  his is an optimi
3026a 7a 61 74 69 6f 6e 20 74 68 61 74 20 70 72 65 76  zation that prev
3026b 65 6e 74 73 20 61 6e 0a 2a 2a 20 75 6e 6e 65 63  ents an.** unnec
3026c 65 73 73 61 72 79 20 73 6f 72 74 20 6f 66 20 74  essary sort of t
3026d 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 69 66  he result set if
3026e 20 61 6e 20 69 6e 64 65 78 20 61 70 70 72 6f 70   an index approp
3026f 72 69 61 74 65 20 66 6f 72 20 74 68 65 0a 2a 2a  riate for the.**
30270 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
30271 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 2e   already exists.
30272 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 77 68  .**.** If the wh
30273 65 72 65 20 63 6c 61 75 73 65 20 6c 6f 6f 70 73  ere clause loops
30274 20 63 61 6e 6e 6f 74 20 62 65 20 61 72 72 61 6e   cannot be arran
30275 67 65 64 20 74 6f 20 70 72 6f 76 69 64 65 20 74  ged to provide t
30276 68 65 20 63 6f 72 72 65 63 74 0a 2a 2a 20 6f 75  he correct.** ou
30277 74 70 75 74 20 6f 72 64 65 72 2c 20 74 68 65 6e  tput order, then
30278 20 74 68 65 20 2a 70 70 4f 72 64 65 72 42 79 20   the *ppOrderBy 
30279 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f  is unchanged..*/
3027a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
3027b 57 68 65 72 65 49 6e 66 6f 20 2a 73 71 6c 69 74  WhereInfo *sqlit
3027c 65 33 57 68 65 72 65 42 65 67 69 6e 28 0a 20 20  e3WhereBegin(.  
3027d 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
3027e 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
3027f 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ser context */. 
30280 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69   SrcList *pTabLi
30281 73 74 2c 20 20 20 20 2f 2a 20 41 20 6c 69 73 74  st,    /* A list
30282 20 6f 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 74   of all tables t
30283 6f 20 62 65 20 73 63 61 6e 6e 65 64 20 2a 2f 0a  o be scanned */.
30284 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20    Expr *pWhere, 
30285 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57          /* The W
30286 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
30287 20 45 78 70 72 4c 69 73 74 20 2a 2a 70 70 4f 72   ExprList **ppOr
30288 64 65 72 42 79 2c 20 2f 2a 20 41 6e 20 4f 52 44  derBy, /* An ORD
30289 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20 6f 72  ER BY clause, or
3028a 20 4e 55 4c 4c 20 2a 2f 0a 20 20 75 31 36 20 77   NULL */.  u16 w
3028b 63 74 72 6c 46 6c 61 67 73 20 20 20 20 20 20 20  ctrlFlags       
3028c 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 57   /* One of the W
3028d 48 45 52 45 5f 2a 20 66 6c 61 67 73 20 64 65 66  HERE_* flags def
3028e 69 6e 65 64 20 69 6e 20 73 71 6c 69 74 65 49 6e  ined in sqliteIn
3028f 74 2e 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  t.h */.){.  int 
30290 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
30291 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
30292 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20  ounter */.  int 
30293 6e 42 79 74 65 57 49 6e 66 6f 3b 20 20 20 20 20  nByteWInfo;     
30294 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 2e 20 62         /* Num. b
30295 79 74 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66  ytes allocated f
30296 6f 72 20 57 68 65 72 65 49 6e 66 6f 20 73 74 72  or WhereInfo str
30297 75 63 74 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e  uct */.  WhereIn
30298 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20  fo *pWInfo;     
30299 20 20 20 20 2f 2a 20 57 69 6c 6c 20 62 65 63 6f      /* Will beco
3029a 6d 65 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  me the return va
3029b 6c 75 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63  lue of this func
3029c 74 69 6f 6e 20 2a 2f 0a 20 20 56 64 62 65 20 2a  tion */.  Vdbe *
3029d 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
3029e 65 3b 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74  e;   /* The virt
3029f 75 61 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67  ual database eng
302a0 69 6e 65 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  ine */.  Bitmask
302a1 20 6e 6f 74 52 65 61 64 79 3b 20 20 20 20 20 20   notReady;      
302a2 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 73 20 74      /* Cursors t
302a3 68 61 74 20 61 72 65 20 6e 6f 74 20 79 65 74 20  hat are not yet 
302a4 70 6f 73 69 74 69 6f 6e 65 64 20 2a 2f 0a 20 20  positioned */.  
302a5 57 68 65 72 65 4d 61 73 6b 53 65 74 20 2a 70 4d  WhereMaskSet *pM
302a6 61 73 6b 53 65 74 3b 20 20 20 20 2f 2a 20 54 68  askSet;    /* Th
302a7 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 61 73  e expression mas
302a8 6b 20 73 65 74 20 2a 2f 0a 20 20 57 68 65 72 65  k set */.  Where
302a9 43 6c 61 75 73 65 20 2a 70 57 43 3b 20 20 20 20  Clause *pWC;    
302aa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65             /* De
302ab 63 6f 6d 70 6f 73 69 74 69 6f 6e 20 6f 66 20 74  composition of t
302ac 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
302ad 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  */.  struct SrcL
302ae 69 73 74 5f 69 74 65 6d 20 2a 70 54 61 62 49 74  ist_item *pTabIt
302af 65 6d 3b 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65  em;  /* A single
302b0 20 65 6e 74 72 79 20 66 72 6f 6d 20 70 54 61 62   entry from pTab
302b1 4c 69 73 74 20 2a 2f 0a 20 20 57 68 65 72 65 4c  List */.  WhereL
302b2 65 76 65 6c 20 2a 70 4c 65 76 65 6c 3b 20 20 20  evel *pLevel;   
302b3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73            /* A s
302b4 69 6e 67 6c 65 20 6c 65 76 65 6c 20 69 6e 20 74  ingle level in t
302b5 68 65 20 70 57 49 6e 66 6f 20 6c 69 73 74 20 2a  he pWInfo list *
302b6 2f 0a 20 20 69 6e 74 20 69 46 72 6f 6d 3b 20 20  /.  int iFrom;  
302b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
302b8 20 20 20 20 2f 2a 20 46 69 72 73 74 20 75 6e 75      /* First unu
302b9 73 65 64 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  sed FROM clause 
302ba 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  element */.  int
302bb 20 61 6e 64 46 6c 61 67 73 3b 20 20 20 20 20 20   andFlags;      
302bc 20 20 20 20 20 20 20 20 2f 2a 20 41 4e 44 2d 65          /* AND-e
302bd 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f 66  d combination of
302be 20 61 6c 6c 20 70 57 43 2d 3e 61 5b 5d 2e 77 74   all pWC->a[].wt
302bf 46 6c 61 67 73 20 2a 2f 0a 20 20 73 71 6c 69 74  Flags */.  sqlit
302c0 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20  e3 *db;         
302c1 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73        /* Databas
302c2 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
302c3 0a 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72  .  /* The number
302c4 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68   of tables in th
302c5 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 73  e FROM clause is
302c6 20 6c 69 6d 69 74 65 64 20 62 79 20 74 68 65 20   limited by the 
302c7 6e 75 6d 62 65 72 20 6f 66 0a 20 20 2a 2a 20 62  number of.  ** b
302c8 69 74 73 20 69 6e 20 61 20 42 69 74 6d 61 73 6b  its in a Bitmask
302c9 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61   .  */.  if( pTa
302ca 62 4c 69 73 74 2d 3e 6e 53 72 63 3e 42 4d 53 20  bList->nSrc>BMS 
302cb 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
302cc 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
302cd 61 74 20 6d 6f 73 74 20 25 64 20 74 61 62 6c 65  at most %d table
302ce 73 20 69 6e 20 61 20 6a 6f 69 6e 22 2c 20 42 4d  s in a join", BM
302cf 53 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  S);.    return 0
302d0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  ;.  }..  /* Allo
302d1 63 61 74 65 20 61 6e 64 20 69 6e 69 74 69 61 6c  cate and initial
302d2 69 7a 65 20 74 68 65 20 57 68 65 72 65 49 6e 66  ize the WhereInf
302d3 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  o structure that
302d4 20 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65   will become the
302d5 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c  .  ** return val
302d6 75 65 2e 20 41 20 73 69 6e 67 6c 65 20 61 6c 6c  ue. A single all
302d7 6f 63 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20  ocation is used 
302d8 74 6f 20 73 74 6f 72 65 20 74 68 65 20 57 68 65  to store the Whe
302d9 72 65 49 6e 66 6f 0a 20 20 2a 2a 20 73 74 72 75  reInfo.  ** stru
302da 63 74 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  ct, the contents
302db 20 6f 66 20 57 68 65 72 65 49 6e 66 6f 2e 61 5b   of WhereInfo.a[
302dc 5d 2c 20 74 68 65 20 57 68 65 72 65 43 6c 61 75  ], the WhereClau
302dd 73 65 20 73 74 72 75 63 74 75 72 65 0a 20 20 2a  se structure.  *
302de 2a 20 61 6e 64 20 74 68 65 20 57 68 65 72 65 4d  * and the WhereM
302df 61 73 6b 53 65 74 20 73 74 72 75 63 74 75 72 65  askSet structure
302e0 2e 20 53 69 6e 63 65 20 57 68 65 72 65 43 6c 61  . Since WhereCla
302e1 75 73 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20  use contains an 
302e2 38 2d 62 79 74 65 0a 20 20 2a 2a 20 66 69 65 6c  8-byte.  ** fiel
302e3 64 20 28 74 79 70 65 20 42 69 74 6d 61 73 6b 29  d (type Bitmask)
302e4 20 69 74 20 6d 75 73 74 20 62 65 20 61 6c 69 67   it must be alig
302e5 6e 65 64 20 6f 6e 20 61 6e 20 38 2d 62 79 74 65  ned on an 8-byte
302e6 20 62 6f 75 6e 64 61 72 79 20 6f 6e 0a 20 20 2a   boundary on.  *
302e7 2a 20 73 6f 6d 65 20 61 72 63 68 69 74 65 63 74  * some architect
302e8 75 72 65 73 2e 20 48 65 6e 63 65 20 74 68 65 20  ures. Hence the 
302e9 52 4f 55 4e 44 38 28 29 20 62 65 6c 6f 77 2e 0a  ROUND8() below..
302ea 20 20 2a 2f 0a 20 20 64 62 20 3d 20 70 50 61 72    */.  db = pPar
302eb 73 65 2d 3e 64 62 3b 0a 20 20 6e 42 79 74 65 57  se->db;.  nByteW
302ec 49 6e 66 6f 20 3d 20 52 4f 55 4e 44 38 28 73 69  Info = ROUND8(si
302ed 7a 65 6f 66 28 57 68 65 72 65 49 6e 66 6f 29 2b  zeof(WhereInfo)+
302ee 28 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 2d  (pTabList->nSrc-
302ef 31 29 2a 73 69 7a 65 6f 66 28 57 68 65 72 65 4c  1)*sizeof(WhereL
302f0 65 76 65 6c 29 29 3b 0a 20 20 70 57 49 6e 66 6f  evel));.  pWInfo
302f1 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
302f2 6f 63 5a 65 72 6f 28 64 62 2c 20 0a 20 20 20 20  ocZero(db, .    
302f3 20 20 6e 42 79 74 65 57 49 6e 66 6f 20 2b 20 0a    nByteWInfo + .
302f4 20 20 20 20 20 20 73 69 7a 65 6f 66 28 57 68 65        sizeof(Whe
302f5 72 65 43 6c 61 75 73 65 29 20 2b 0a 20 20 20 20  reClause) +.    
302f6 20 20 73 69 7a 65 6f 66 28 57 68 65 72 65 4d 61    sizeof(WhereMa
302f7 73 6b 53 65 74 29 0a 20 20 29 3b 0a 20 20 69 66  skSet).  );.  if
302f8 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
302f9 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 77  ed ){.    goto w
302fa 68 65 72 65 42 65 67 69 6e 45 72 72 6f 72 3b 0a  hereBeginError;.
302fb 20 20 7d 0a 20 20 70 57 49 6e 66 6f 2d 3e 6e 4c    }.  pWInfo->nL
302fc 65 76 65 6c 20 3d 20 70 54 61 62 4c 69 73 74 2d  evel = pTabList-
302fd 3e 6e 53 72 63 3b 0a 20 20 70 57 49 6e 66 6f 2d  >nSrc;.  pWInfo-
302fe 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  >pParse = pParse
302ff 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 70 54 61 62  ;.  pWInfo->pTab
30300 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b  List = pTabList;
30301 0a 20 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61  .  pWInfo->iBrea
30302 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  k = sqlite3VdbeM
30303 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 70  akeLabel(v);.  p
30304 57 49 6e 66 6f 2d 3e 70 57 43 20 3d 20 70 57 43  WInfo->pWC = pWC
30305 20 3d 20 28 57 68 65 72 65 43 6c 61 75 73 65 20   = (WhereClause 
30306 2a 29 26 28 28 75 38 20 2a 29 70 57 49 6e 66 6f  *)&((u8 *)pWInfo
30307 29 5b 6e 42 79 74 65 57 49 6e 66 6f 5d 3b 0a 20  )[nByteWInfo];. 
30308 20 70 57 49 6e 66 6f 2d 3e 77 63 74 72 6c 46 6c   pWInfo->wctrlFl
30309 61 67 73 20 3d 20 77 63 74 72 6c 46 6c 61 67 73  ags = wctrlFlags
3030a 3b 0a 20 20 70 4d 61 73 6b 53 65 74 20 3d 20 28  ;.  pMaskSet = (
3030b 57 68 65 72 65 4d 61 73 6b 53 65 74 2a 29 26 70  WhereMaskSet*)&p
3030c 57 43 5b 31 5d 3b 0a 0a 20 20 2f 2a 20 53 70 6c  WC[1];..  /* Spl
3030d 69 74 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  it the WHERE cla
3030e 75 73 65 20 69 6e 74 6f 20 73 65 70 61 72 61 74  use into separat
3030f 65 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73  e subexpressions
30310 20 77 68 65 72 65 20 65 61 63 68 0a 20 20 2a 2a   where each.  **
30311 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 20 69   subexpression i
30312 73 20 73 65 70 61 72 61 74 65 64 20 62 79 20 61  s separated by a
30313 6e 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 0a  n AND operator..
30314 20 20 2a 2f 0a 20 20 69 6e 69 74 4d 61 73 6b 53    */.  initMaskS
30315 65 74 28 70 4d 61 73 6b 53 65 74 29 3b 0a 20 20  et(pMaskSet);.  
30316 77 68 65 72 65 43 6c 61 75 73 65 49 6e 69 74 28  whereClauseInit(
30317 70 57 43 2c 20 70 50 61 72 73 65 2c 20 70 4d 61  pWC, pParse, pMa
30318 73 6b 53 65 74 29 3b 0a 20 20 73 71 6c 69 74 65  skSet);.  sqlite
30319 33 45 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e  3ExprCodeConstan
3031a 74 73 28 70 50 61 72 73 65 2c 20 70 57 68 65 72  ts(pParse, pWher
3031b 65 29 3b 0a 20 20 77 68 65 72 65 53 70 6c 69 74  e);.  whereSplit
3031c 28 70 57 43 2c 20 70 57 68 65 72 65 2c 20 54 4b  (pWC, pWhere, TK
3031d 5f 41 4e 44 29 3b 0a 20 20 20 20 0a 20 20 2f 2a  _AND);.    .  /*
3031e 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 61   Special case: a
3031f 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 68   WHERE clause th
30320 61 74 20 69 73 20 63 6f 6e 73 74 61 6e 74 2e 20  at is constant. 
30321 20 45 76 61 6c 75 61 74 65 20 74 68 65 0a 20 20   Evaluate the.  
30322 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e  ** expression an
30323 64 20 65 69 74 68 65 72 20 6a 75 6d 70 20 6f 76  d either jump ov
30324 65 72 20 61 6c 6c 20 6f 66 20 74 68 65 20 63 6f  er all of the co
30325 64 65 20 6f 72 20 66 61 6c 6c 20 74 68 72 75 2e  de or fall thru.
30326 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 57 68 65  .  */.  if( pWhe
30327 72 65 20 26 26 20 28 70 54 61 62 4c 69 73 74 2d  re && (pTabList-
30328 3e 6e 53 72 63 3d 3d 30 20 7c 7c 20 73 71 6c 69  >nSrc==0 || sqli
30329 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74 61 6e  te3ExprIsConstan
3032a 74 4e 6f 74 4a 6f 69 6e 28 70 57 68 65 72 65 29  tNotJoin(pWhere)
3032b 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
3032c 45 78 70 72 49 66 46 61 6c 73 65 28 70 50 61 72  ExprIfFalse(pPar
3032d 73 65 2c 20 70 57 68 65 72 65 2c 20 70 57 49 6e  se, pWhere, pWIn
3032e 66 6f 2d 3e 69 42 72 65 61 6b 2c 20 53 51 4c 49  fo->iBreak, SQLI
3032f 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a  TE_JUMPIFNULL);.
30330 20 20 20 20 70 57 68 65 72 65 20 3d 20 30 3b 0a      pWhere = 0;.
30331 20 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 69 67 6e    }..  /* Assign
30332 20 61 20 62 69 74 20 66 72 6f 6d 20 74 68 65 20   a bit from the 
30333 62 69 74 6d 61 73 6b 20 74 6f 20 65 76 65 72 79  bitmask to every
30334 20 74 65 72 6d 20 69 6e 20 74 68 65 20 46 52 4f   term in the FRO
30335 4d 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 0a 20  M clause..  **. 
30336 20 2a 2a 20 57 68 65 6e 20 61 73 73 69 67 6e 69   ** When assigni
30337 6e 67 20 62 69 74 6d 61 73 6b 20 76 61 6c 75 65  ng bitmask value
30338 73 20 74 6f 20 46 52 4f 4d 20 63 6c 61 75 73 65  s to FROM clause
30339 20 63 75 72 73 6f 72 73 2c 20 69 74 20 6d 75 73   cursors, it mus
3033a 74 20 62 65 0a 20 20 2a 2a 20 74 68 65 20 63 61  t be.  ** the ca
3033b 73 65 20 74 68 61 74 20 69 66 20 58 20 69 73 20  se that if X is 
3033c 74 68 65 20 62 69 74 6d 61 73 6b 20 66 6f 72 20  the bitmask for 
3033d 74 68 65 20 4e 2d 74 68 20 46 52 4f 4d 20 63 6c  the N-th FROM cl
3033e 61 75 73 65 20 74 65 72 6d 20 74 68 65 6e 0a 20  ause term then. 
3033f 20 2a 2a 20 74 68 65 20 62 69 74 6d 61 73 6b 20   ** the bitmask 
30340 66 6f 72 20 61 6c 6c 20 46 52 4f 4d 20 63 6c 61  for all FROM cla
30341 75 73 65 20 74 65 72 6d 73 20 74 6f 20 74 68 65  use terms to the
30342 20 6c 65 66 74 20 6f 66 20 74 68 65 20 4e 2d 74   left of the N-t
30343 68 20 74 65 72 6d 0a 20 20 2a 2a 20 69 73 20 28  h term.  ** is (
30344 58 2d 31 29 2e 20 20 20 41 6e 20 65 78 70 72 65  X-1).   An expre
30345 73 73 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 4f  ssion from the O
30346 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20 4c 45  N clause of a LE
30347 46 54 20 4a 4f 49 4e 20 63 61 6e 20 75 73 65 0a  FT JOIN can use.
30348 20 20 2a 2a 20 69 74 73 20 45 78 70 72 2e 69 52    ** its Expr.iR
30349 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 76 61  ightJoinTable va
3034a 6c 75 65 20 74 6f 20 66 69 6e 64 20 74 68 65 20  lue to find the 
3034b 62 69 74 6d 61 73 6b 20 6f 66 20 74 68 65 20 72  bitmask of the r
3034c 69 67 68 74 20 74 61 62 6c 65 0a 20 20 2a 2a 20  ight table.  ** 
3034d 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20 53 75  of the join.  Su
3034e 62 74 72 61 63 74 69 6e 67 20 6f 6e 65 20 66 72  btracting one fr
3034f 6f 6d 20 74 68 65 20 72 69 67 68 74 20 74 61 62  om the right tab
30350 6c 65 20 62 69 74 6d 61 73 6b 20 67 69 76 65 73  le bitmask gives
30351 20 61 0a 20 20 2a 2a 20 62 69 74 6d 61 73 6b 20   a.  ** bitmask 
30352 66 6f 72 20 61 6c 6c 20 74 61 62 6c 65 73 20 74  for all tables t
30353 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20 74 68  o the left of th
30354 65 20 6a 6f 69 6e 2e 20 20 4b 6e 6f 77 69 6e 67  e join.  Knowing
30355 20 74 68 65 20 62 69 74 6d 61 73 6b 0a 20 20 2a   the bitmask.  *
30356 2a 20 66 6f 72 20 61 6c 6c 20 74 61 62 6c 65 73  * for all tables
30357 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20   to the left of 
30358 61 20 6c 65 66 74 20 6a 6f 69 6e 20 69 73 20 69  a left join is i
30359 6d 70 6f 72 74 61 6e 74 2e 20 20 54 69 63 6b 65  mportant.  Ticke
3035a 74 20 23 33 30 31 35 2e 0a 20 20 2a 2a 0a 20 20  t #3015..  **.  
3035b 2a 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65  ** Configure the
3035c 20 57 68 65 72 65 43 6c 61 75 73 65 2e 76 6d 61   WhereClause.vma
3035d 73 6b 20 76 61 72 69 61 62 6c 65 20 73 6f 20 74  sk variable so t
3035e 68 61 74 20 62 69 74 73 20 74 68 61 74 20 63 6f  hat bits that co
3035f 72 72 65 73 70 6f 6e 64 0a 20 20 2a 2a 20 74 6f  rrespond.  ** to
30360 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 63   virtual table c
30361 75 72 73 6f 72 73 20 61 72 65 20 73 65 74 2e 20  ursors are set. 
30362 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20  This is used to 
30363 73 65 6c 65 63 74 69 76 65 6c 79 20 64 69 73 61  selectively disa
30364 62 6c 65 20 0a 20 20 2a 2a 20 74 68 65 20 4f 52  ble .  ** the OR
30365 2d 74 6f 2d 49 4e 20 74 72 61 6e 73 66 6f 72 6d  -to-IN transform
30366 61 74 69 6f 6e 20 69 6e 20 65 78 70 72 41 6e 61  ation in exprAna
30367 6c 79 7a 65 4f 72 54 65 72 6d 28 29 2e 20 49 74  lyzeOrTerm(). It
30368 20 69 73 20 6e 6f 74 20 68 65 6c 70 66 75 6c 20   is not helpful 
30369 0a 20 20 2a 2a 20 77 69 74 68 20 76 69 72 74 75  .  ** with virtu
3036a 61 6c 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a  al tables..  */.
3036b 20 20 61 73 73 65 72 74 28 20 70 57 43 2d 3e 76    assert( pWC->v
3036c 6d 61 73 6b 3d 3d 30 20 26 26 20 70 4d 61 73 6b  mask==0 && pMask
3036d 53 65 74 2d 3e 6e 3d 3d 30 20 29 3b 0a 20 20 66  Set->n==0 );.  f
3036e 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c 69  or(i=0; i<pTabLi
3036f 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a  st->nSrc; i++){.
30370 20 20 20 20 63 72 65 61 74 65 4d 61 73 6b 28 70      createMask(p
30371 4d 61 73 6b 53 65 74 2c 20 70 54 61 62 4c 69 73  MaskSet, pTabLis
30372 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 29  t->a[i].iCursor)
30373 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
30374 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
30375 4c 45 0a 20 20 20 20 69 66 28 20 41 4c 57 41 59  LE.    if( ALWAY
30376 53 28 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d  S(pTabList->a[i]
30377 2e 70 54 61 62 29 20 26 26 20 49 73 56 69 72 74  .pTab) && IsVirt
30378 75 61 6c 28 70 54 61 62 4c 69 73 74 2d 3e 61 5b  ual(pTabList->a[
30379 69 5d 2e 70 54 61 62 29 20 29 7b 0a 20 20 20 20  i].pTab) ){.    
3037a 20 20 70 57 43 2d 3e 76 6d 61 73 6b 20 7c 3d 20    pWC->vmask |= 
3037b 28 28 42 69 74 6d 61 73 6b 29 31 20 3c 3c 20 69  ((Bitmask)1 << i
3037c 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  );.    }.#endif.
3037d 20 20 7d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42    }.#ifndef NDEB
3037e 55 47 0a 20 20 7b 0a 20 20 20 20 42 69 74 6d 61  UG.  {.    Bitma
3037f 73 6b 20 74 6f 54 68 65 4c 65 66 74 20 3d 20 30  sk toTheLeft = 0
30380 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
30381 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b  <pTabList->nSrc;
30382 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42 69 74   i++){.      Bit
30383 6d 61 73 6b 20 6d 20 3d 20 67 65 74 4d 61 73 6b  mask m = getMask
30384 28 70 4d 61 73 6b 53 65 74 2c 20 70 54 61 62 4c  (pMaskSet, pTabL
30385 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f  ist->a[i].iCurso
30386 72 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  r);.      assert
30387 28 20 28 6d 2d 31 29 3d 3d 74 6f 54 68 65 4c 65  ( (m-1)==toTheLe
30388 66 74 20 29 3b 0a 20 20 20 20 20 20 74 6f 54 68  ft );.      toTh
30389 65 4c 65 66 74 20 7c 3d 20 6d 3b 0a 20 20 20 20  eLeft |= m;.    
3038a 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  }.  }.#endif..  
3038b 2f 2a 20 41 6e 61 6c 79 7a 65 20 61 6c 6c 20 6f  /* Analyze all o
3038c 66 20 74 68 65 20 73 75 62 65 78 70 72 65 73 73  f the subexpress
3038d 69 6f 6e 73 2e 20 20 4e 6f 74 65 20 74 68 61 74  ions.  Note that
3038e 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 29 20 6d   exprAnalyze() m
3038f 69 67 68 74 0a 20 20 2a 2a 20 61 64 64 20 6e 65  ight.  ** add ne
30390 77 20 76 69 72 74 75 61 6c 20 74 65 72 6d 73 20  w virtual terms 
30391 6f 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20  onto the end of 
30392 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
30393 2e 20 20 57 65 20 64 6f 20 6e 6f 74 0a 20 20 2a  .  We do not.  *
30394 2a 20 77 61 6e 74 20 74 6f 20 61 6e 61 6c 79 7a  * want to analyz
30395 65 20 74 68 65 73 65 20 76 69 72 74 75 61 6c 20  e these virtual 
30396 74 65 72 6d 73 2c 20 73 6f 20 73 74 61 72 74 20  terms, so start 
30397 61 6e 61 6c 79 7a 69 6e 67 20 61 74 20 74 68 65  analyzing at the
30398 20 65 6e 64 0a 20 20 2a 2a 20 61 6e 64 20 77 6f   end.  ** and wo
30399 72 6b 20 66 6f 72 77 61 72 64 20 73 6f 20 74 68  rk forward so th
3039a 61 74 20 74 68 65 20 61 64 64 65 64 20 76 69 72  at the added vir
3039b 74 75 61 6c 20 74 65 72 6d 73 20 61 72 65 20 6e  tual terms are n
3039c 65 76 65 72 20 70 72 6f 63 65 73 73 65 64 2e 0a  ever processed..
3039d 20 20 2a 2f 0a 20 20 65 78 70 72 41 6e 61 6c 79    */.  exprAnaly
3039e 7a 65 41 6c 6c 28 70 54 61 62 4c 69 73 74 2c 20  zeAll(pTabList, 
3039f 70 57 43 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e  pWC);.  if( db->
303a0 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
303a1 20 20 20 20 67 6f 74 6f 20 77 68 65 72 65 42 65      goto whereBe
303a2 67 69 6e 45 72 72 6f 72 3b 0a 20 20 7d 0a 0a 20  ginError;.  }.. 
303a3 20 2f 2a 20 43 68 6f 73 65 20 74 68 65 20 62 65   /* Chose the be
303a4 73 74 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20  st index to use 
303a5 66 6f 72 20 65 61 63 68 20 74 61 62 6c 65 20 69  for each table i
303a6 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
303a7 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69  e..  **.  ** Thi
303a8 73 20 6c 6f 6f 70 20 66 69 6c 6c 73 20 69 6e 20  s loop fills in 
303a9 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 69  the following fi
303aa 65 6c 64 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  elds:.  **.  ** 
303ab 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 70 49    pWInfo->a[].pI
303ac 64 78 20 20 20 20 20 20 54 68 65 20 69 6e 64 65  dx      The inde
303ad 78 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 69  x to use for thi
303ae 73 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 6c  s level of the l
303af 6f 6f 70 2e 0a 20 20 2a 2a 20 20 20 70 57 49 6e  oop..  **   pWIn
303b0 66 6f 2d 3e 61 5b 5d 2e 77 73 46 6c 61 67 73 20  fo->a[].wsFlags 
303b1 20 20 57 48 45 52 45 5f 78 78 78 20 66 6c 61 67    WHERE_xxx flag
303b2 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
303b3 68 20 70 49 64 78 0a 20 20 2a 2a 20 20 20 70 57  h pIdx.  **   pW
303b4 49 6e 66 6f 2d 3e 61 5b 5d 2e 6e 45 71 20 20 20  Info->a[].nEq   
303b5 20 20 20 20 54 68 65 20 6e 75 6d 62 65 72 20 6f      The number o
303b6 66 20 3d 3d 20 61 6e 64 20 49 4e 20 63 6f 6e 73  f == and IN cons
303b7 74 72 61 69 6e 74 73 0a 20 20 2a 2a 20 20 20 70  traints.  **   p
303b8 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 69 46 72 6f 6d  WInfo->a[].iFrom
303b9 20 20 20 20 20 57 68 69 63 68 20 74 65 72 6d 20       Which term 
303ba 6f 66 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75  of the FROM clau
303bb 73 65 20 69 73 20 62 65 69 6e 67 20 63 6f 64 65  se is being code
303bc 64 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d  d.  **   pWInfo-
303bd 3e 61 5b 5d 2e 69 54 61 62 43 75 72 20 20 20 54  >a[].iTabCur   T
303be 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 66  he VDBE cursor f
303bf 6f 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20  or the database 
303c0 74 61 62 6c 65 0a 20 20 2a 2a 20 20 20 70 57 49  table.  **   pWI
303c1 6e 66 6f 2d 3e 61 5b 5d 2e 69 49 64 78 43 75 72  nfo->a[].iIdxCur
303c2 20 20 20 54 68 65 20 56 44 42 45 20 63 75 72 73     The VDBE curs
303c3 6f 72 20 66 6f 72 20 74 68 65 20 69 6e 64 65 78  or for the index
303c4 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e  .  **   pWInfo->
303c5 61 5b 5d 2e 70 54 65 72 6d 20 20 20 20 20 57 68  a[].pTerm     Wh
303c6 65 6e 20 77 73 46 6c 61 67 73 3d 3d 57 4f 5f 4f  en wsFlags==WO_O
303c7 52 2c 20 74 68 65 20 4f 52 2d 63 6c 61 75 73 65  R, the OR-clause
303c8 20 74 65 72 6d 0a 20 20 2a 2a 0a 20 20 2a 2a 20   term.  **.  ** 
303c9 54 68 69 73 20 6c 6f 6f 70 20 61 6c 73 6f 20 66  This loop also f
303ca 69 67 75 72 65 73 20 6f 75 74 20 74 68 65 20 6e  igures out the n
303cb 65 73 74 69 6e 67 20 6f 72 64 65 72 20 6f 66 20  esting order of 
303cc 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 46 52  tables in the FR
303cd 4f 4d 0a 20 20 2a 2a 20 63 6c 61 75 73 65 2e 0a  OM.  ** clause..
303ce 20 20 2a 2f 0a 20 20 6e 6f 74 52 65 61 64 79 20    */.  notReady 
303cf 3d 20 7e 28 42 69 74 6d 61 73 6b 29 30 3b 0a 20  = ~(Bitmask)0;. 
303d0 20 70 54 61 62 49 74 65 6d 20 3d 20 70 54 61 62   pTabItem = pTab
303d1 4c 69 73 74 2d 3e 61 3b 0a 20 20 70 4c 65 76 65  List->a;.  pLeve
303d2 6c 20 3d 20 70 57 49 6e 66 6f 2d 3e 61 3b 0a 20  l = pWInfo->a;. 
303d3 20 61 6e 64 46 6c 61 67 73 20 3d 20 7e 30 3b 0a   andFlags = ~0;.
303d4 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22 2a    WHERETRACE(("*
303d5 2a 2a 20 4f 70 74 69 6d 69 7a 65 72 20 53 74 61  ** Optimizer Sta
303d6 72 74 20 2a 2a 2a 5c 6e 22 29 29 3b 0a 20 20 66  rt ***\n"));.  f
303d7 6f 72 28 69 3d 69 46 72 6f 6d 3d 30 2c 20 70 4c  or(i=iFrom=0, pL
303d8 65 76 65 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20  evel=pWInfo->a; 
303d9 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  i<pTabList->nSrc
303da 3b 20 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29  ; i++, pLevel++)
303db 7b 0a 20 20 20 20 57 68 65 72 65 43 6f 73 74 20  {.    WhereCost 
303dc 62 65 73 74 50 6c 61 6e 3b 20 20 20 20 20 20 20  bestPlan;       
303dd 20 20 2f 2a 20 4d 6f 73 74 20 65 66 66 69 63 69    /* Most effici
303de 65 6e 74 20 70 6c 61 6e 20 73 65 65 6e 20 73 6f  ent plan seen so
303df 20 66 61 72 20 2a 2f 0a 20 20 20 20 49 6e 64 65   far */.    Inde
303e0 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20 20  x *pIdx;        
303e1 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
303e2 20 66 6f 72 20 46 52 4f 4d 20 74 61 62 6c 65 20   for FROM table 
303e3 61 74 20 70 54 61 62 49 74 65 6d 20 2a 2f 0a 20  at pTabItem */. 
303e4 20 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20     int j;       
303e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
303e6 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76  * For looping ov
303e7 65 72 20 46 52 4f 4d 20 74 61 62 6c 65 73 20 2a  er FROM tables *
303e8 2f 0a 20 20 20 20 69 6e 74 20 62 65 73 74 4a 20  /.    int bestJ 
303e9 3d 20 2d 31 3b 20 20 20 20 20 20 20 20 20 20 20  = -1;           
303ea 20 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 6f    /* The value o
303eb 66 20 6a 20 2a 2f 0a 20 20 20 20 42 69 74 6d 61  f j */.    Bitma
303ec 73 6b 20 6d 3b 20 20 20 20 20 20 20 20 20 20 20  sk m;           
303ed 20 20 20 20 20 20 20 2f 2a 20 42 69 74 6d 61 73         /* Bitmas
303ee 6b 20 76 61 6c 75 65 20 66 6f 72 20 6a 20 6f 72  k value for j or
303ef 20 62 65 73 74 4a 20 2a 2f 0a 20 20 20 20 69 6e   bestJ */.    in
303f0 74 20 69 73 4f 70 74 69 6d 61 6c 3b 20 20 20 20  t isOptimal;    
303f1 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 74 65            /* Ite
303f2 72 61 74 6f 72 20 66 6f 72 20 6f 70 74 69 6d 61  rator for optima
303f3 6c 2f 6e 6f 6e 2d 6f 70 74 69 6d 61 6c 20 73 65  l/non-optimal se
303f4 61 72 63 68 20 2a 2f 0a 0a 20 20 20 20 6d 65 6d  arch */..    mem
303f5 73 65 74 28 26 62 65 73 74 50 6c 61 6e 2c 20 30  set(&bestPlan, 0
303f6 2c 20 73 69 7a 65 6f 66 28 62 65 73 74 50 6c 61  , sizeof(bestPla
303f7 6e 29 29 3b 0a 20 20 20 20 62 65 73 74 50 6c 61  n));.    bestPla
303f8 6e 2e 72 43 6f 73 74 20 3d 20 53 51 4c 49 54 45  n.rCost = SQLITE
303f9 5f 42 49 47 5f 44 42 4c 3b 0a 0a 20 20 20 20 2f  _BIG_DBL;..    /
303fa 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74  * Loop through t
303fb 68 65 20 72 65 6d 61 69 6e 69 6e 67 20 65 6e 74  he remaining ent
303fc 72 69 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d  ries in the FROM
303fd 20 63 6c 61 75 73 65 20 74 6f 20 66 69 6e 64 20   clause to find 
303fe 74 68 65 0a 20 20 20 20 2a 2a 20 6e 65 78 74 20  the.    ** next 
303ff 6e 65 73 74 65 64 20 6c 6f 6f 70 2e 20 54 68 65  nested loop. The
30400 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 65 6e 74   FROM clause ent
30401 72 69 65 73 20 6d 61 79 20 62 65 20 69 74 65 72  ries may be iter
30402 61 74 65 64 20 74 68 72 6f 75 67 68 0a 20 20 20  ated through.   
30403 20 2a 2a 20 65 69 74 68 65 72 20 6f 6e 63 65 20   ** either once 
30404 6f 72 20 74 77 69 63 65 2e 20 0a 20 20 20 20 2a  or twice. .    *
30405 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 66 69 72  *.    ** The fir
30406 73 74 20 69 74 65 72 61 74 69 6f 6e 2c 20 77 68  st iteration, wh
30407 69 63 68 20 69 73 20 61 6c 77 61 79 73 20 70 65  ich is always pe
30408 72 66 6f 72 6d 65 64 2c 20 73 65 61 72 63 68 65  rformed, searche
30409 73 20 66 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a  s for the.    **
3040a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 65 6e 74   FROM clause ent
3040b 72 79 20 74 68 61 74 20 70 65 72 6d 69 74 73 20  ry that permits 
3040c 74 68 65 20 6c 6f 77 65 73 74 2d 63 6f 73 74 2c  the lowest-cost,
3040d 20 22 6f 70 74 69 6d 61 6c 22 20 73 63 61 6e 2e   "optimal" scan.
3040e 20 49 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73 20   In.    ** this 
3040f 63 6f 6e 74 65 78 74 20 61 6e 20 6f 70 74 69 6d  context an optim
30410 61 6c 20 73 63 61 6e 20 69 73 20 6f 6e 65 20 74  al scan is one t
30411 68 61 74 20 75 73 65 73 20 74 68 65 20 73 61 6d  hat uses the sam
30412 65 20 73 74 72 61 74 65 67 79 0a 20 20 20 20 2a  e strategy.    *
30413 2a 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20  * for the given 
30414 46 52 4f 4d 20 63 6c 61 75 73 65 20 65 6e 74 72  FROM clause entr
30415 79 20 61 73 20 77 6f 75 6c 64 20 62 65 20 73 65  y as would be se
30416 6c 65 63 74 65 64 20 69 66 20 74 68 65 20 65 6e  lected if the en
30417 74 72 79 0a 20 20 20 20 2a 2a 20 77 65 72 65 20  try.    ** were 
30418 75 73 65 64 20 61 73 20 74 68 65 20 69 6e 6e 65  used as the inne
30419 72 6d 6f 73 74 20 6e 65 73 74 65 64 20 6c 6f 6f  rmost nested loo
3041a 70 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72  p.  In other wor
3041b 64 73 2c 20 61 20 74 61 62 6c 65 0a 20 20 20 20  ds, a table.    
3041c 2a 2a 20 69 73 20 63 68 6f 73 65 6e 20 73 75 63  ** is chosen suc
3041d 68 20 74 68 61 74 20 74 68 65 20 63 6f 73 74 20  h that the cost 
3041e 6f 66 20 72 75 6e 6e 69 6e 67 20 74 68 61 74 20  of running that 
3041f 74 61 62 6c 65 20 63 61 6e 6e 6f 74 20 62 65 20  table cannot be 
30420 72 65 64 75 63 65 64 0a 20 20 20 20 2a 2a 20 62  reduced.    ** b
30421 79 20 77 61 69 74 69 6e 67 20 66 6f 72 20 6f 74  y waiting for ot
30422 68 65 72 20 74 61 62 6c 65 73 20 74 6f 20 72 75  her tables to ru
30423 6e 20 66 69 72 73 74 2e 0a 20 20 20 20 2a 2a 0a  n first..    **.
30424 20 20 20 20 2a 2a 20 54 68 65 20 73 65 63 6f 6e      ** The secon
30425 64 20 69 74 65 72 61 74 69 6f 6e 20 69 73 20 6f  d iteration is o
30426 6e 6c 79 20 70 65 72 66 6f 72 6d 65 64 20 69 66  nly performed if
30427 20 6e 6f 20 6f 70 74 69 6d 61 6c 20 73 63 61 6e   no optimal scan
30428 20 73 74 72 61 74 65 67 69 65 73 0a 20 20 20 20   strategies.    
30429 2a 2a 20 77 65 72 65 20 66 6f 75 6e 64 20 62 79  ** were found by
3042a 20 74 68 65 20 66 69 72 73 74 2e 20 54 68 69 73   the first. This
3042b 20 69 74 65 72 61 74 69 6f 6e 20 69 73 20 75 73   iteration is us
3042c 65 64 20 74 6f 20 73 65 61 72 63 68 20 66 6f 72  ed to search for
3042d 20 74 68 65 0a 20 20 20 20 2a 2a 20 6c 6f 77 65   the.    ** lowe
3042e 73 74 20 63 6f 73 74 20 73 63 61 6e 20 6f 76 65  st cost scan ove
3042f 72 61 6c 6c 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  rall..    **.   
30430 20 2a 2a 20 50 72 65 76 69 6f 75 73 20 76 65 72   ** Previous ver
30431 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20  sions of SQLite 
30432 70 65 72 66 6f 72 6d 65 64 20 6f 6e 6c 79 20 74  performed only t
30433 68 65 20 73 65 63 6f 6e 64 20 69 74 65 72 61 74  he second iterat
30434 69 6f 6e 20 2d 0a 20 20 20 20 2a 2a 20 74 68 65  ion -.    ** the
30435 20 6e 65 78 74 20 6f 75 74 65 72 6d 6f 73 74 20   next outermost 
30436 6c 6f 6f 70 20 77 61 73 20 61 6c 77 61 79 73 20  loop was always 
30437 74 68 61 74 20 77 69 74 68 20 74 68 65 20 6c 6f  that with the lo
30438 77 65 73 74 20 6f 76 65 72 61 6c 6c 0a 20 20 20  west overall.   
30439 20 2a 2a 20 63 6f 73 74 2e 20 48 6f 77 65 76 65   ** cost. Howeve
3043a 72 2c 20 74 68 69 73 20 6d 65 61 6e 74 20 74 68  r, this meant th
3043b 61 74 20 53 51 4c 69 74 65 20 63 6f 75 6c 64 20  at SQLite could 
3043c 73 65 6c 65 63 74 20 74 68 65 20 77 72 6f 6e 67  select the wrong
3043d 20 70 6c 61 6e 0a 20 20 20 20 2a 2a 20 66 6f 72   plan.    ** for
3043e 20 73 63 72 69 70 74 73 20 73 75 63 68 20 61 73   scripts such as
3043f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
30440 20 20 20 20 2a 2a 20 20 20 0a 20 20 20 20 2a 2a      **   .    **
30441 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45 20     CREATE TABLE 
30442 74 31 28 61 2c 20 62 29 3b 20 0a 20 20 20 20 2a  t1(a, b); .    *
30443 2a 20 20 20 43 52 45 41 54 45 20 54 41 42 4c 45  *   CREATE TABLE
30444 20 74 32 28 63 2c 20 64 29 3b 0a 20 20 20 20 2a   t2(c, d);.    *
30445 2a 20 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  *   SELECT * FRO
30446 4d 20 74 32 2c 20 74 31 20 57 48 45 52 45 20 74  M t2, t1 WHERE t
30447 32 2e 72 6f 77 69 64 20 3d 20 74 31 2e 61 3b 0a  2.rowid = t1.a;.
30448 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
30449 65 20 62 65 73 74 20 73 74 72 61 74 65 67 79 20  e best strategy 
3044a 69 73 20 74 6f 20 69 74 65 72 61 74 65 20 74 68  is to iterate th
3044b 72 6f 75 67 68 20 74 61 62 6c 65 20 74 31 20 66  rough table t1 f
3044c 69 72 73 74 2e 20 48 6f 77 65 76 65 72 20 69 74  irst. However it
3044d 0a 20 20 20 20 2a 2a 20 69 73 20 6e 6f 74 20 70  .    ** is not p
3044e 6f 73 73 69 62 6c 65 20 74 6f 20 64 65 74 65 72  ossible to deter
3044f 6d 69 6e 65 20 74 68 69 73 20 77 69 74 68 20 61  mine this with a
30450 20 73 69 6d 70 6c 65 20 67 72 65 65 64 79 20 61   simple greedy a
30451 6c 67 6f 72 69 74 68 6d 2e 0a 20 20 20 20 2a 2a  lgorithm..    **
30452 20 48 6f 77 65 76 65 72 2c 20 73 69 6e 63 65 20   However, since 
30453 74 68 65 20 63 6f 73 74 20 6f 66 20 61 20 6c 69  the cost of a li
30454 6e 65 61 72 20 73 63 61 6e 20 74 68 72 6f 75 67  near scan throug
30455 68 20 74 61 62 6c 65 20 74 32 20 69 73 20 74 68  h table t2 is th
30456 65 20 73 61 6d 65 20 0a 20 20 20 20 2a 2a 20 61  e same .    ** a
30457 73 20 74 68 65 20 63 6f 73 74 20 6f 66 20 61 20  s the cost of a 
30458 6c 69 6e 65 61 72 20 73 63 61 6e 20 74 68 72 6f  linear scan thro
30459 75 67 68 20 74 61 62 6c 65 20 74 31 2c 20 61 20  ugh table t1, a 
3045a 73 69 6d 70 6c 65 20 67 72 65 65 64 79 20 0a 20  simple greedy . 
3045b 20 20 20 2a 2a 20 61 6c 67 6f 72 69 74 68 6d 20     ** algorithm 
3045c 6d 61 79 20 63 68 6f 6f 73 65 20 74 6f 20 75 73  may choose to us
3045d 65 20 74 32 20 66 6f 72 20 74 68 65 20 6f 75 74  e t2 for the out
3045e 65 72 20 6c 6f 6f 70 2c 20 77 68 69 63 68 20 69  er loop, which i
3045f 73 20 61 20 6d 75 63 68 0a 20 20 20 20 2a 2a 20  s a much.    ** 
30460 63 6f 73 74 6c 69 65 72 20 61 70 70 72 6f 61 63  costlier approac
30461 68 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f  h..    */.    fo
30462 72 28 69 73 4f 70 74 69 6d 61 6c 3d 31 3b 20 69  r(isOptimal=1; i
30463 73 4f 70 74 69 6d 61 6c 3e 3d 30 20 26 26 20 62  sOptimal>=0 && b
30464 65 73 74 4a 3c 30 3b 20 69 73 4f 70 74 69 6d 61  estJ<0; isOptima
30465 6c 2d 2d 29 7b 0a 20 20 20 20 20 20 42 69 74 6d  l--){.      Bitm
30466 61 73 6b 20 6d 61 73 6b 20 3d 20 28 69 73 4f 70  ask mask = (isOp
30467 74 69 6d 61 6c 20 3f 20 30 20 3a 20 6e 6f 74 52  timal ? 0 : notR
30468 65 61 64 79 29 3b 0a 20 20 20 20 20 20 61 73 73  eady);.      ass
30469 65 72 74 28 20 28 70 54 61 62 4c 69 73 74 2d 3e  ert( (pTabList->
3046a 6e 53 72 63 2d 69 46 72 6f 6d 29 3e 31 20 7c 7c  nSrc-iFrom)>1 ||
3046b 20 69 73 4f 70 74 69 6d 61 6c 20 29 3b 0a 20 20   isOptimal );.  
3046c 20 20 20 20 66 6f 72 28 6a 3d 69 46 72 6f 6d 2c      for(j=iFrom,
3046d 20 70 54 61 62 49 74 65 6d 3d 26 70 54 61 62 4c   pTabItem=&pTabL
3046e 69 73 74 2d 3e 61 5b 6a 5d 3b 20 6a 3c 70 54 61  ist->a[j]; j<pTa
3046f 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 6a 2b 2b  bList->nSrc; j++
30470 2c 20 70 54 61 62 49 74 65 6d 2b 2b 29 7b 0a 20  , pTabItem++){. 
30471 20 20 20 20 20 20 20 69 6e 74 20 64 6f 4e 6f 74         int doNot
30472 52 65 6f 72 64 65 72 3b 20 20 20 20 2f 2a 20 54  Reorder;    /* T
30473 72 75 65 20 69 66 20 74 68 69 73 20 74 61 62 6c  rue if this tabl
30474 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20  e should not be 
30475 72 65 6f 72 64 65 72 65 64 20 2a 2f 0a 20 20 20  reordered */.   
30476 20 20 20 20 20 57 68 65 72 65 43 6f 73 74 20 73       WhereCost s
30477 43 6f 73 74 3b 20 20 20 20 20 2f 2a 20 43 6f 73  Cost;     /* Cos
30478 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72  t information fr
30479 6f 6d 20 62 65 73 74 5b 56 69 72 74 75 61 6c 5d  om best[Virtual]
3047a 49 6e 64 65 78 28 29 20 2a 2f 0a 20 20 20 20 20  Index() */.     
3047b 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72     ExprList *pOr
3047c 64 65 72 42 79 3b 20 20 2f 2a 20 4f 52 44 45 52  derBy;  /* ORDER
3047d 20 42 59 20 63 6c 61 75 73 65 20 66 6f 72 20 69   BY clause for i
3047e 6e 64 65 78 20 74 6f 20 6f 70 74 69 6d 69 7a 65  ndex to optimize
3047f 20 2a 2f 0a 20 20 0a 20 20 20 20 20 20 20 20 64   */.  .        d
30480 6f 4e 6f 74 52 65 6f 72 64 65 72 20 3d 20 20 28  oNotReorder =  (
30481 70 54 61 62 49 74 65 6d 2d 3e 6a 6f 69 6e 74 79  pTabItem->jointy
30482 70 65 20 26 20 28 4a 54 5f 4c 45 46 54 7c 4a 54  pe & (JT_LEFT|JT
30483 5f 43 52 4f 53 53 29 29 21 3d 30 3b 0a 20 20 20  _CROSS))!=0;.   
30484 20 20 20 20 20 69 66 28 20 6a 21 3d 69 46 72 6f       if( j!=iFro
30485 6d 20 26 26 20 64 6f 4e 6f 74 52 65 6f 72 64 65  m && doNotReorde
30486 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  r ) break;.     
30487 20 20 20 6d 20 3d 20 67 65 74 4d 61 73 6b 28 70     m = getMask(p
30488 4d 61 73 6b 53 65 74 2c 20 70 54 61 62 49 74 65  MaskSet, pTabIte
30489 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20  m->iCursor);.   
3048a 20 20 20 20 20 69 66 28 20 28 6d 20 26 20 6e 6f       if( (m & no
3048b 74 52 65 61 64 79 29 3d 3d 30 20 29 7b 0a 20 20  tReady)==0 ){.  
3048c 20 20 20 20 20 20 20 20 69 66 28 20 6a 3d 3d 69          if( j==i
3048d 46 72 6f 6d 20 29 20 69 46 72 6f 6d 2b 2b 3b 0a  From ) iFrom++;.
3048e 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69 6e            contin
3048f 75 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ue;.        }.  
30490 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d        pOrderBy =
30491 20 28 28 69 3d 3d 30 20 26 26 20 70 70 4f 72 64   ((i==0 && ppOrd
30492 65 72 42 79 20 29 3f 2a 70 70 4f 72 64 65 72 42  erBy )?*ppOrderB
30493 79 3a 30 29 3b 0a 20 20 0a 20 20 20 20 20 20 20  y:0);.  .       
30494 20 61 73 73 65 72 74 28 20 70 54 61 62 49 74 65   assert( pTabIte
30495 6d 2d 3e 70 54 61 62 20 29 3b 0a 23 69 66 6e 64  m->pTab );.#ifnd
30496 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
30497 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20  IRTUALTABLE.    
30498 20 20 20 20 69 66 28 20 49 73 56 69 72 74 75 61      if( IsVirtua
30499 6c 28 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62  l(pTabItem->pTab
3049a 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  ) ){.          s
3049b 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
3049c 6f 20 2a 2a 70 70 20 3d 20 26 70 57 49 6e 66 6f  o **pp = &pWInfo
3049d 2d 3e 61 5b 6a 5d 2e 70 49 64 78 49 6e 66 6f 3b  ->a[j].pIdxInfo;
3049e 0a 20 20 20 20 20 20 20 20 20 20 62 65 73 74 56  .          bestV
3049f 69 72 74 75 61 6c 49 6e 64 65 78 28 70 50 61 72  irtualIndex(pPar
304a0 73 65 2c 20 70 57 43 2c 20 70 54 61 62 49 74 65  se, pWC, pTabIte
304a1 6d 2c 20 6d 61 73 6b 2c 20 70 4f 72 64 65 72 42  m, mask, pOrderB
304a2 79 2c 20 26 73 43 6f 73 74 2c 20 70 70 29 3b 0a  y, &sCost, pp);.
304a3 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 0a 23          }else .#
304a4 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 7b 0a  endif.        {.
304a5 20 20 20 20 20 20 20 20 20 20 62 65 73 74 42 74            bestBt
304a6 72 65 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c  reeIndex(pParse,
304a7 20 70 57 43 2c 20 70 54 61 62 49 74 65 6d 2c 20   pWC, pTabItem, 
304a8 6d 61 73 6b 2c 20 70 4f 72 64 65 72 42 79 2c 20  mask, pOrderBy, 
304a9 26 73 43 6f 73 74 29 3b 0a 20 20 20 20 20 20 20  &sCost);.       
304aa 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72   }.        asser
304ab 74 28 20 69 73 4f 70 74 69 6d 61 6c 20 7c 7c 20  t( isOptimal || 
304ac 28 73 43 6f 73 74 2e 75 73 65 64 26 6e 6f 74 52  (sCost.used&notR
304ad 65 61 64 79 29 3d 3d 30 20 29 3b 0a 0a 20 20 20  eady)==0 );..   
304ae 20 20 20 20 20 69 66 28 20 28 73 43 6f 73 74 2e       if( (sCost.
304af 75 73 65 64 26 6e 6f 74 52 65 61 64 79 29 3d 3d  used&notReady)==
304b0 30 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 6a  0.         && (j
304b1 3d 3d 69 46 72 6f 6d 20 7c 7c 20 73 43 6f 73 74  ==iFrom || sCost
304b2 2e 72 43 6f 73 74 3c 62 65 73 74 50 6c 61 6e 2e  .rCost<bestPlan.
304b3 72 43 6f 73 74 29 20 0a 20 20 20 20 20 20 20 20  rCost) .        
304b4 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62 65 73  ){.          bes
304b5 74 50 6c 61 6e 20 3d 20 73 43 6f 73 74 3b 0a 20  tPlan = sCost;. 
304b6 20 20 20 20 20 20 20 20 20 62 65 73 74 4a 20 3d           bestJ =
304b7 20 6a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   j;.        }.  
304b8 20 20 20 20 20 20 69 66 28 20 64 6f 4e 6f 74 52        if( doNotR
304b9 65 6f 72 64 65 72 20 29 20 62 72 65 61 6b 3b 0a  eorder ) break;.
304ba 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
304bb 20 20 61 73 73 65 72 74 28 20 62 65 73 74 4a 3e    assert( bestJ>
304bc 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
304bd 28 20 6e 6f 74 52 65 61 64 79 20 26 20 67 65 74  ( notReady & get
304be 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20 70  Mask(pMaskSet, p
304bf 54 61 62 4c 69 73 74 2d 3e 61 5b 62 65 73 74 4a  TabList->a[bestJ
304c0 5d 2e 69 43 75 72 73 6f 72 29 20 29 3b 0a 20 20  ].iCursor) );.  
304c1 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22 2a    WHERETRACE(("*
304c2 2a 2a 20 4f 70 74 69 6d 69 7a 65 72 20 73 65 6c  ** Optimizer sel
304c3 65 63 74 73 20 74 61 62 6c 65 20 25 64 20 66 6f  ects table %d fo
304c4 72 20 6c 6f 6f 70 20 25 64 5c 6e 22 2c 20 62 65  r loop %d\n", be
304c5 73 74 4a 2c 0a 20 20 20 20 20 20 20 20 20 20 20  stJ,.           
304c6 70 4c 65 76 65 6c 2d 70 57 49 6e 66 6f 2d 3e 61  pLevel-pWInfo->a
304c7 29 29 3b 0a 20 20 20 20 69 66 28 20 28 62 65 73  ));.    if( (bes
304c8 74 50 6c 61 6e 2e 70 6c 61 6e 2e 77 73 46 6c 61  tPlan.plan.wsFla
304c9 67 73 20 26 20 57 48 45 52 45 5f 4f 52 44 45 52  gs & WHERE_ORDER
304ca 42 59 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  BY)!=0 ){.      
304cb 2a 70 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a  *ppOrderBy = 0;.
304cc 20 20 20 20 7d 0a 20 20 20 20 61 6e 64 46 6c 61      }.    andFla
304cd 67 73 20 26 3d 20 62 65 73 74 50 6c 61 6e 2e 70  gs &= bestPlan.p
304ce 6c 61 6e 2e 77 73 46 6c 61 67 73 3b 0a 20 20 20  lan.wsFlags;.   
304cf 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 20 3d 20   pLevel->plan = 
304d0 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 3b 0a 20  bestPlan.plan;. 
304d1 20 20 20 69 66 28 20 62 65 73 74 50 6c 61 6e 2e     if( bestPlan.
304d2 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
304d3 48 45 52 45 5f 49 4e 44 45 58 45 44 20 29 7b 0a  HERE_INDEXED ){.
304d4 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 69 49        pLevel->iI
304d5 64 78 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e  dxCur = pParse->
304d6 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73  nTab++;.    }els
304d7 65 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d  e{.      pLevel-
304d8 3e 69 49 64 78 43 75 72 20 3d 20 2d 31 3b 0a 20  >iIdxCur = -1;. 
304d9 20 20 20 7d 0a 20 20 20 20 6e 6f 74 52 65 61 64     }.    notRead
304da 79 20 26 3d 20 7e 67 65 74 4d 61 73 6b 28 70 4d  y &= ~getMask(pM
304db 61 73 6b 53 65 74 2c 20 70 54 61 62 4c 69 73 74  askSet, pTabList
304dc 2d 3e 61 5b 62 65 73 74 4a 5d 2e 69 43 75 72 73  ->a[bestJ].iCurs
304dd 6f 72 29 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  or);.    pLevel-
304de 3e 69 46 72 6f 6d 20 3d 20 28 75 38 29 62 65 73  >iFrom = (u8)bes
304df 74 4a 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63  tJ;..    /* Chec
304e0 6b 20 74 68 61 74 20 69 66 20 74 68 65 20 74 61  k that if the ta
304e1 62 6c 65 20 73 63 61 6e 6e 65 64 20 62 79 20 74  ble scanned by t
304e2 68 69 73 20 6c 6f 6f 70 20 69 74 65 72 61 74 69  his loop iterati
304e3 6f 6e 20 68 61 64 20 61 6e 0a 20 20 20 20 2a 2a  on had an.    **
304e4 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75   INDEXED BY clau
304e5 73 65 20 61 74 74 61 63 68 65 64 20 74 6f 20 69  se attached to i
304e6 74 2c 20 74 68 61 74 20 74 68 65 20 6e 61 6d 65  t, that the name
304e7 64 20 69 6e 64 65 78 20 69 73 20 62 65 69 6e 67  d index is being
304e8 0a 20 20 20 20 2a 2a 20 75 73 65 64 20 66 6f 72  .    ** used for
304e9 20 74 68 65 20 73 63 61 6e 2e 20 49 66 20 6e 6f   the scan. If no
304ea 74 2c 20 74 68 65 6e 20 71 75 65 72 79 20 63 6f  t, then query co
304eb 6d 70 69 6c 61 74 69 6f 6e 20 68 61 73 20 66 61  mpilation has fa
304ec 69 6c 65 64 2e 0a 20 20 20 20 2a 2a 20 52 65 74  iled..    ** Ret
304ed 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20  urn an error..  
304ee 20 20 2a 2f 0a 20 20 20 20 70 49 64 78 20 3d 20    */.    pIdx = 
304ef 70 54 61 62 4c 69 73 74 2d 3e 61 5b 62 65 73 74  pTabList->a[best
304f0 4a 5d 2e 70 49 6e 64 65 78 3b 0a 20 20 20 20 69  J].pIndex;.    i
304f1 66 28 20 70 49 64 78 20 29 7b 0a 20 20 20 20 20  f( pIdx ){.     
304f2 20 69 66 28 20 28 62 65 73 74 50 6c 61 6e 2e 70   if( (bestPlan.p
304f3 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
304f4 45 52 45 5f 49 4e 44 45 58 45 44 29 3d 3d 30 20  ERE_INDEXED)==0 
304f5 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
304f6 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
304f7 65 2c 20 22 63 61 6e 6e 6f 74 20 75 73 65 20 69  e, "cannot use i
304f8 6e 64 65 78 3a 20 25 73 22 2c 20 70 49 64 78 2d  ndex: %s", pIdx-
304f9 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20  >zName);.       
304fa 20 67 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e   goto whereBegin
304fb 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 7d 65 6c  Error;.      }el
304fc 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49  se{.        /* I
304fd 66 20 61 6e 20 49 4e 44 45 58 45 44 20 42 59 20  f an INDEXED BY 
304fe 63 6c 61 75 73 65 20 69 73 20 75 73 65 64 2c 20  clause is used, 
304ff 74 68 65 20 62 65 73 74 49 6e 64 65 78 28 29 20  the bestIndex() 
30500 66 75 6e 63 74 69 6f 6e 20 69 73 0a 20 20 20 20  function is.    
30501 20 20 20 20 2a 2a 20 67 75 61 72 61 6e 74 65 65      ** guarantee
30502 64 20 74 6f 20 66 69 6e 64 20 74 68 65 20 69 6e  d to find the in
30503 64 65 78 20 73 70 65 63 69 66 69 65 64 20 69 6e  dex specified in
30504 20 74 68 65 20 49 4e 44 45 58 45 44 20 42 59 20   the INDEXED BY 
30505 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20 2a  clause.        *
30506 2a 20 69 66 20 69 74 20 66 69 6e 64 20 61 6e 20  * if it find an 
30507 69 6e 64 65 78 20 61 74 20 61 6c 6c 2e 20 2a 2f  index at all. */
30508 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
30509 20 62 65 73 74 50 6c 61 6e 2e 70 6c 61 6e 2e 75   bestPlan.plan.u
3050a 2e 70 49 64 78 3d 3d 70 49 64 78 20 29 3b 0a 20  .pIdx==pIdx );. 
3050b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
3050c 0a 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22  .  WHERETRACE(("
3050d 2a 2a 2a 20 4f 70 74 69 6d 69 7a 65 72 20 46 69  *** Optimizer Fi
3050e 6e 69 73 68 65 64 20 2a 2a 2a 5c 6e 22 29 29 3b  nished ***\n"));
3050f 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
30510 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Err || db->mallo
30511 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67  cFailed ){.    g
30512 6f 74 6f 20 77 68 65 72 65 42 65 67 69 6e 45 72  oto whereBeginEr
30513 72 6f 72 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  ror;.  }..  /* I
30514 66 20 74 68 65 20 74 6f 74 61 6c 20 71 75 65 72  f the total quer
30515 79 20 6f 6e 6c 79 20 73 65 6c 65 63 74 73 20 61  y only selects a
30516 20 73 69 6e 67 6c 65 20 72 6f 77 2c 20 74 68 65   single row, the
30517 6e 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a 20  n the ORDER BY. 
30518 20 2a 2a 20 63 6c 61 75 73 65 20 69 73 20 69 72   ** clause is ir
30519 72 65 6c 65 76 61 6e 74 2e 0a 20 20 2a 2f 0a 20  relevant..  */. 
3051a 20 69 66 28 20 28 61 6e 64 46 6c 61 67 73 20 26   if( (andFlags &
3051b 20 57 48 45 52 45 5f 55 4e 49 51 55 45 29 21 3d   WHERE_UNIQUE)!=
3051c 30 20 26 26 20 70 70 4f 72 64 65 72 42 79 20 29  0 && ppOrderBy )
3051d 7b 0a 20 20 20 20 2a 70 70 4f 72 64 65 72 42 79  {.    *ppOrderBy
3051e 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   = 0;.  }..  /* 
3051f 49 66 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73  If the caller is
30520 20 61 6e 20 55 50 44 41 54 45 20 6f 72 20 44 45   an UPDATE or DE
30521 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 74  LETE statement t
30522 68 61 74 20 69 73 20 72 65 71 75 65 73 74 69 6e  hat is requestin
30523 67 0a 20 20 2a 2a 20 74 6f 20 75 73 65 20 61 20  g.  ** to use a 
30524 6f 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72 69 74  one-pass algorit
30525 68 6d 2c 20 64 65 74 65 72 6d 69 6e 65 20 69 66  hm, determine if
30526 20 74 68 69 73 20 69 73 20 61 70 70 72 6f 70 72   this is appropr
30527 69 61 74 65 2e 0a 20 20 2a 2a 20 54 68 65 20 6f  iate..  ** The o
30528 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68  ne-pass algorith
30529 6d 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 69 66 20  m only works if 
3052a 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
3052b 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a   constraints.  *
3052c 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  * the statement 
3052d 74 6f 20 75 70 64 61 74 65 20 61 20 73 69 6e 67  to update a sing
3052e 6c 65 20 72 6f 77 2e 0a 20 20 2a 2f 0a 20 20 61  le row..  */.  a
3052f 73 73 65 72 74 28 20 28 77 63 74 72 6c 46 6c 61  ssert( (wctrlFla
30530 67 73 20 26 20 57 48 45 52 45 5f 4f 4e 45 50 41  gs & WHERE_ONEPA
30531 53 53 5f 44 45 53 49 52 45 44 29 3d 3d 30 20 7c  SS_DESIRED)==0 |
30532 7c 20 70 57 49 6e 66 6f 2d 3e 6e 4c 65 76 65 6c  | pWInfo->nLevel
30533 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 28 77 63  ==1 );.  if( (wc
30534 74 72 6c 46 6c 61 67 73 20 26 20 57 48 45 52 45  trlFlags & WHERE
30535 5f 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44  _ONEPASS_DESIRED
30536 29 21 3d 30 20 26 26 20 28 61 6e 64 46 6c 61 67  )!=0 && (andFlag
30537 73 20 26 20 57 48 45 52 45 5f 55 4e 49 51 55 45  s & WHERE_UNIQUE
30538 29 21 3d 30 20 29 7b 0a 20 20 20 20 70 57 49 6e  )!=0 ){.    pWIn
30539 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 20 3d 20  fo->okOnePass = 
3053a 31 3b 0a 20 20 20 20 70 57 49 6e 66 6f 2d 3e 61  1;.    pWInfo->a
3053b 5b 30 5d 2e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  [0].plan.wsFlags
3053c 20 26 3d 20 7e 57 48 45 52 45 5f 49 44 58 5f 4f   &= ~WHERE_IDX_O
3053d 4e 4c 59 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f  NLY;.  }..  /* O
3053e 70 65 6e 20 61 6c 6c 20 74 61 62 6c 65 73 20 69  pen all tables i
3053f 6e 20 74 68 65 20 70 54 61 62 4c 69 73 74 20 61  n the pTabList a
30540 6e 64 20 61 6e 79 20 69 6e 64 69 63 65 73 20 73  nd any indices s
30541 65 6c 65 63 74 65 64 20 66 6f 72 0a 20 20 2a 2a  elected for.  **
30542 20 73 65 61 72 63 68 69 6e 67 20 74 68 6f 73 65   searching those
30543 20 74 61 62 6c 65 73 2e 0a 20 20 2a 2f 0a 20 20   tables..  */.  
30544 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66  sqlite3CodeVerif
30545 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20  ySchema(pParse, 
30546 2d 31 29 3b 20 2f 2a 20 49 6e 73 65 72 74 20 74  -1); /* Insert t
30547 68 65 20 63 6f 6f 6b 69 65 20 76 65 72 69 66 69  he cookie verifi
30548 65 72 20 47 6f 74 6f 20 2a 2f 0a 20 20 66 6f 72  er Goto */.  for
30549 28 69 3d 30 2c 20 70 4c 65 76 65 6c 3d 70 57 49  (i=0, pLevel=pWI
3054a 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69  nfo->a; i<pTabLi
3054b 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70  st->nSrc; i++, p
3054c 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20 20 20 54 61  Level++){.    Ta
3054d 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 2f  ble *pTab;     /
3054e 2a 20 54 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20  * Table to open 
3054f 2a 2f 0a 20 20 20 20 69 6e 74 20 69 44 62 3b 20  */.    int iDb; 
30550 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
30551 20 6f 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e   of database con
30552 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 2f 69 6e  taining table/in
30553 64 65 78 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  dex */..#ifndef 
30554 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c  SQLITE_OMIT_EXPL
30555 41 49 4e 0a 20 20 20 20 69 66 28 20 70 50 61 72  AIN.    if( pPar
30556 73 65 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 29  se->explain==2 )
30557 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4d  {.      char *zM
30558 73 67 3b 0a 20 20 20 20 20 20 73 74 72 75 63 74  sg;.      struct
30559 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
3055a 49 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74  Item = &pTabList
3055b 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f  ->a[pLevel->iFro
3055c 6d 5d 3b 0a 20 20 20 20 20 20 7a 4d 73 67 20 3d  m];.      zMsg =
3055d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
3055e 64 62 2c 20 22 54 41 42 4c 45 20 25 73 22 2c 20  db, "TABLE %s", 
3055f 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  pItem->zName);. 
30560 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e       if( pItem->
30561 7a 41 6c 69 61 73 20 29 7b 0a 20 20 20 20 20 20  zAlias ){.      
30562 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33    zMsg = sqlite3
30563 4d 41 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73  MAppendf(db, zMs
30564 67 2c 20 22 25 73 20 41 53 20 25 73 22 2c 20 7a  g, "%s AS %s", z
30565 4d 73 67 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69  Msg, pItem->zAli
30566 61 73 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  as);.      }.   
30567 20 20 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e     if( (pLevel->
30568 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
30569 48 45 52 45 5f 49 4e 44 45 58 45 44 29 21 3d 30  HERE_INDEXED)!=0
3056a 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67   ){.        zMsg
3056b 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e   = sqlite3MAppen
3056c 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73  df(db, zMsg, "%s
3056d 20 57 49 54 48 20 49 4e 44 45 58 20 25 73 22 2c   WITH INDEX %s",
3056e 0a 20 20 20 20 20 20 20 20 20 20 20 7a 4d 73 67  .           zMsg
3056f 2c 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75  , pLevel->plan.u
30570 2e 70 49 64 78 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  .pIdx->zName);. 
30571 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
30572 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
30573 61 67 73 20 26 20 57 48 45 52 45 5f 4d 55 4c 54  ags & WHERE_MULT
30574 49 5f 4f 52 20 29 7b 0a 20 20 20 20 20 20 20 20  I_OR ){.        
30575 7a 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 41  zMsg = sqlite3MA
30576 70 70 65 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c  ppendf(db, zMsg,
30577 20 22 25 73 20 56 49 41 20 4d 55 4c 54 49 2d 49   "%s VIA MULTI-I
30578 4e 44 45 58 20 55 4e 49 4f 4e 22 2c 20 7a 4d 73  NDEX UNION", zMs
30579 67 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  g);.      }else 
3057a 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  if( pLevel->plan
3057b 2e 77 73 46 6c 61 67 73 20 26 20 28 57 48 45 52  .wsFlags & (WHER
3057c 45 5f 52 4f 57 49 44 5f 45 51 7c 57 48 45 52 45  E_ROWID_EQ|WHERE
3057d 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29 20 29 7b  _ROWID_RANGE) ){
3057e 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20  .        zMsg = 
3057f 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28  sqlite3MAppendf(
30580 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 55 53  db, zMsg, "%s US
30581 49 4e 47 20 50 52 49 4d 41 52 59 20 4b 45 59 22  ING PRIMARY KEY"
30582 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20 7d  , zMsg);.      }
30583 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
30584 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
30585 45 0a 20 20 20 20 20 20 65 6c 73 65 20 69 66 28  E.      else if(
30586 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77   (pLevel->plan.w
30587 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 56  sFlags & WHERE_V
30588 49 52 54 55 41 4c 54 41 42 4c 45 29 21 3d 30 20  IRTUALTABLE)!=0 
30589 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
3058a 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70  e3_index_info *p
3058b 56 74 61 62 49 64 78 20 3d 20 70 4c 65 76 65 6c  VtabIdx = pLevel
3058c 2d 3e 70 6c 61 6e 2e 75 2e 70 56 74 61 62 49 64  ->plan.u.pVtabId
3058d 78 3b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20  x;.        zMsg 
3058e 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64  = sqlite3MAppend
3058f 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20  f(db, zMsg, "%s 
30590 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 49 4e  VIRTUAL TABLE IN
30591 44 45 58 20 25 64 3a 25 73 22 2c 20 7a 4d 73 67  DEX %d:%s", zMsg
30592 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
30593 20 20 20 20 20 20 70 56 74 61 62 49 64 78 2d 3e        pVtabIdx->
30594 69 64 78 4e 75 6d 2c 20 70 56 74 61 62 49 64 78  idxNum, pVtabIdx
30595 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20 20 20 20  ->idxStr);.     
30596 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20   }.#endif.      
30597 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e  if( pLevel->plan
30598 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52 45  .wsFlags & WHERE
30599 5f 4f 52 44 45 52 42 59 20 29 7b 0a 20 20 20 20  _ORDERBY ){.    
3059a 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69 74      zMsg = sqlit
3059b 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20 7a  e3MAppendf(db, z
3059c 4d 73 67 2c 20 22 25 73 20 4f 52 44 45 52 20 42  Msg, "%s ORDER B
3059d 59 22 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20  Y", zMsg);.     
3059e 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
3059f 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
305a0 5f 45 78 70 6c 61 69 6e 2c 20 69 2c 20 70 4c 65  _Explain, i, pLe
305a1 76 65 6c 2d 3e 69 46 72 6f 6d 2c 20 30 2c 20 7a  vel->iFrom, 0, z
305a2 4d 73 67 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29  Msg, P4_DYNAMIC)
305a3 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f  ;.    }.#endif /
305a4 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58  * SQLITE_OMIT_EX
305a5 50 4c 41 49 4e 20 2a 2f 0a 20 20 20 20 70 54 61  PLAIN */.    pTa
305a6 62 49 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73  bItem = &pTabLis
305a7 74 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72  t->a[pLevel->iFr
305a8 6f 6d 5d 3b 0a 20 20 20 20 70 54 61 62 20 3d 20  om];.    pTab = 
305a9 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 3b 0a  pTabItem->pTab;.
305aa 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65      iDb = sqlite
305ab 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
305ac 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
305ad 29 3b 0a 20 20 20 20 69 66 28 20 28 70 54 61 62  );.    if( (pTab
305ae 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f  ->tabFlags & TF_
305af 45 70 68 65 6d 65 72 61 6c 29 21 3d 30 20 7c 7c  Ephemeral)!=0 ||
305b0 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29   pTab->pSelect )
305b1 20 63 6f 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64   continue;.#ifnd
305b2 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
305b3 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20  IRTUALTABLE.    
305b4 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61  if( (pLevel->pla
305b5 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
305b6 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 21  E_VIRTUALTABLE)!
305b7 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73  =0 ){.      cons
305b8 74 20 63 68 61 72 20 2a 70 56 54 61 62 20 3d 20  t char *pVTab = 
305b9 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
305ba 6c 69 74 65 33 47 65 74 56 54 61 62 6c 65 28 64  lite3GetVTable(d
305bb 62 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20  b, pTab);.      
305bc 69 6e 74 20 69 43 75 72 20 3d 20 70 54 61 62 49  int iCur = pTabI
305bd 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a 20 20  tem->iCursor;.  
305be 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
305bf 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 4f 70 65  ddOp4(v, OP_VOpe
305c0 6e 2c 20 69 43 75 72 2c 20 30 2c 20 30 2c 20 70  n, iCur, 0, 0, p
305c1 56 54 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a  VTab, P4_VTAB);.
305c2 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66      }else.#endif
305c3 0a 20 20 20 20 69 66 28 20 28 70 4c 65 76 65 6c  .    if( (pLevel
305c4 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26  ->plan.wsFlags &
305c5 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29   WHERE_IDX_ONLY)
305c6 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 26 26 20  ==0.         && 
305c7 28 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48  (wctrlFlags & WH
305c8 45 52 45 5f 4f 4d 49 54 5f 4f 50 45 4e 29 3d 3d  ERE_OMIT_OPEN)==
305c9 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6f  0 ){.      int o
305ca 70 20 3d 20 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e  p = pWInfo->okOn
305cb 65 50 61 73 73 20 3f 20 4f 50 5f 4f 70 65 6e 57  ePass ? OP_OpenW
305cc 72 69 74 65 20 3a 20 4f 50 5f 4f 70 65 6e 52 65  rite : OP_OpenRe
305cd 61 64 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ad;.      sqlite
305ce 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73  3OpenTable(pPars
305cf 65 2c 20 70 54 61 62 49 74 65 6d 2d 3e 69 43 75  e, pTabItem->iCu
305d0 72 73 6f 72 2c 20 69 44 62 2c 20 70 54 61 62 2c  rsor, iDb, pTab,
305d1 20 6f 70 29 3b 0a 20 20 20 20 20 20 69 66 28 20   op);.      if( 
305d2 21 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61  !pWInfo->okOnePa
305d3 73 73 20 26 26 20 70 54 61 62 2d 3e 6e 43 6f 6c  ss && pTab->nCol
305d4 3c 42 4d 53 20 29 7b 0a 20 20 20 20 20 20 20 20  <BMS ){.        
305d5 42 69 74 6d 61 73 6b 20 62 20 3d 20 70 54 61 62  Bitmask b = pTab
305d6 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20  Item->colUsed;. 
305d7 20 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 30         int n = 0
305d8 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20  ;.        for(; 
305d9 62 3b 20 62 3d 62 3e 3e 31 2c 20 6e 2b 2b 29 7b  b; b=b>>1, n++){
305da 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
305db 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
305dc 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
305dd 65 6e 74 41 64 64 72 28 76 29 2d 31 2c 20 53 51  entAddr(v)-1, SQ
305de 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28  LITE_INT_TO_PTR(
305df 6e 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20  n), P4_INT32);. 
305e0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e         assert( n
305e1 3c 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a  <=pTab->nCol );.
305e2 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
305e3 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e{.      sqlite3
305e4 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65  TableLock(pParse
305e5 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75  , iDb, pTab->tnu
305e6 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  m, 0, pTab->zNam
305e7 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4c  e);.    }.    pL
305e8 65 76 65 6c 2d 3e 69 54 61 62 43 75 72 20 3d 20  evel->iTabCur = 
305e9 70 54 61 62 49 74 65 6d 2d 3e 69 43 75 72 73 6f  pTabItem->iCurso
305ea 72 3b 0a 20 20 20 20 69 66 28 20 28 70 4c 65 76  r;.    if( (pLev
305eb 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73  el->plan.wsFlags
305ec 20 26 20 57 48 45 52 45 5f 49 4e 44 45 58 45 44   & WHERE_INDEXED
305ed 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 49 6e  )!=0 ){.      In
305ee 64 65 78 20 2a 70 49 78 20 3d 20 70 4c 65 76 65  dex *pIx = pLeve
305ef 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 3b 0a  l->plan.u.pIdx;.
305f0 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70        KeyInfo *p
305f1 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64  Key = sqlite3Ind
305f2 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65  exKeyinfo(pParse
305f3 2c 20 70 49 78 29 3b 0a 20 20 20 20 20 20 69 6e  , pIx);.      in
305f4 74 20 69 49 64 78 43 75 72 20 3d 20 70 4c 65 76  t iIdxCur = pLev
305f5 65 6c 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20 20  el->iIdxCur;.   
305f6 20 20 20 61 73 73 65 72 74 28 20 70 49 78 2d 3e     assert( pIx->
305f7 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70  pSchema==pTab->p
305f8 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20 20  Schema );.      
305f9 61 73 73 65 72 74 28 20 69 49 64 78 43 75 72 3e  assert( iIdxCur>
305fa 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =0 );.      sqli
305fb 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
305fc 20 4f 50 5f 4f 70 65 6e 52 65 61 64 2c 20 69 49   OP_OpenRead, iI
305fd 64 78 43 75 72 2c 20 70 49 78 2d 3e 74 6e 75 6d  dxCur, pIx->tnum
305fe 2c 20 69 44 62 2c 0a 20 20 20 20 20 20 20 20 20  , iDb,.         
305ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
30600 63 68 61 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b  char*)pKey, P4_K
30601 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b  EYINFO_HANDOFF);
30602 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
30603 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70 49 78  nt((v, "%s", pIx
30604 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 7d  ->zName));.    }
30605 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64 65  .    sqlite3Code
30606 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50 61  VerifySchema(pPa
30607 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20  rse, iDb);.  }. 
30608 20 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 20 3d 20   pWInfo->iTop = 
30609 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
3060a 6e 74 41 64 64 72 28 76 29 3b 0a 0a 20 20 2f 2a  ntAddr(v);..  /*
3060b 20 47 65 6e 65 72 61 74 65 20 74 68 65 20 63 6f   Generate the co
3060c 64 65 20 74 6f 20 64 6f 20 74 68 65 20 73 65 61  de to do the sea
3060d 72 63 68 2e 20 20 45 61 63 68 20 69 74 65 72 61  rch.  Each itera
3060e 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 0a  tion of the for.
3060f 20 20 2a 2a 20 6c 6f 6f 70 20 62 65 6c 6f 77 20    ** loop below 
30610 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 66  generates code f
30611 6f 72 20 61 20 73 69 6e 67 6c 65 20 6e 65 73 74  or a single nest
30612 65 64 20 6c 6f 6f 70 20 6f 66 20 74 68 65 20 56  ed loop of the V
30613 4d 0a 20 20 2a 2a 20 70 72 6f 67 72 61 6d 2e 0a  M.  ** program..
30614 20 20 2a 2f 0a 20 20 6e 6f 74 52 65 61 64 79 20    */.  notReady 
30615 3d 20 7e 28 42 69 74 6d 61 73 6b 29 30 3b 0a 20  = ~(Bitmask)0;. 
30616 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62   for(i=0; i<pTab
30617 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29  List->nSrc; i++)
30618 7b 0a 20 20 20 20 6e 6f 74 52 65 61 64 79 20 3d  {.    notReady =
30619 20 63 6f 64 65 4f 6e 65 4c 6f 6f 70 53 74 61 72   codeOneLoopStar
3061a 74 28 70 57 49 6e 66 6f 2c 20 69 2c 20 77 63 74  t(pWInfo, i, wct
3061b 72 6c 46 6c 61 67 73 2c 20 6e 6f 74 52 65 61 64  rlFlags, notRead
3061c 79 29 3b 0a 20 20 20 20 70 57 49 6e 66 6f 2d 3e  y);.    pWInfo->
3061d 69 43 6f 6e 74 69 6e 75 65 20 3d 20 70 57 49 6e  iContinue = pWIn
3061e 66 6f 2d 3e 61 5b 69 5d 2e 61 64 64 72 43 6f 6e  fo->a[i].addrCon
3061f 74 3b 0a 20 20 7d 0a 0a 23 69 66 64 65 66 20 53  t;.  }..#ifdef S
30620 51 4c 49 54 45 5f 54 45 53 54 20 20 2f 2a 20 46  QLITE_TEST  /* F
30621 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 64  or testing and d
30622 65 62 75 67 67 69 6e 67 20 75 73 65 20 6f 6e 6c  ebugging use onl
30623 79 20 2a 2f 0a 20 20 2f 2a 20 52 65 63 6f 72 64  y */.  /* Record
30624 20 69 6e 20 74 68 65 20 71 75 65 72 79 20 70 6c   in the query pl
30625 61 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  an information a
30626 62 6f 75 74 20 74 68 65 20 63 75 72 72 65 6e 74  bout the current
30627 20 74 61 62 6c 65 0a 20 20 2a 2a 20 61 6e 64 20   table.  ** and 
30628 74 68 65 20 69 6e 64 65 78 20 75 73 65 64 20 74  the index used t
30629 6f 20 61 63 63 65 73 73 20 69 74 20 28 69 66 20  o access it (if 
3062a 61 6e 79 29 2e 20 20 49 66 20 74 68 65 20 74 61  any).  If the ta
3062b 62 6c 65 20 69 74 73 65 6c 66 0a 20 20 2a 2a 20  ble itself.  ** 
3062c 69 73 20 6e 6f 74 20 75 73 65 64 2c 20 69 74 73  is not used, its
3062d 20 6e 61 6d 65 20 69 73 20 6a 75 73 74 20 27 7b   name is just '{
3062e 7d 27 2e 20 20 49 66 20 6e 6f 20 69 6e 64 65 78  }'.  If no index
3062f 20 69 73 20 75 73 65 64 0a 20 20 2a 2a 20 74 68   is used.  ** th
30630 65 20 69 6e 64 65 78 20 69 73 20 6c 69 73 74 65  e index is liste
30631 64 20 61 73 20 22 7b 7d 22 2e 20 20 49 66 20 74  d as "{}".  If t
30632 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69  he primary key i
30633 73 20 75 73 65 64 20 74 68 65 0a 20 20 2a 2a 20  s used the.  ** 
30634 69 6e 64 65 78 20 6e 61 6d 65 20 69 73 20 27 2a  index name is '*
30635 27 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  '..  */.  for(i=
30636 30 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e  0; i<pTabList->n
30637 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63  Src; i++){.    c
30638 68 61 72 20 2a 7a 3b 0a 20 20 20 20 69 6e 74 20  har *z;.    int 
30639 6e 3b 0a 20 20 20 20 70 4c 65 76 65 6c 20 3d 20  n;.    pLevel = 
3063a 26 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d 3b 0a 20  &pWInfo->a[i];. 
3063b 20 20 20 70 54 61 62 49 74 65 6d 20 3d 20 26 70     pTabItem = &p
3063c 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65  TabList->a[pLeve
3063d 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20 20 7a  l->iFrom];.    z
3063e 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 7a 41 6c   = pTabItem->zAl
3063f 69 61 73 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d  ias;.    if( z==
30640 30 20 29 20 7a 20 3d 20 70 54 61 62 49 74 65 6d  0 ) z = pTabItem
30641 2d 3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20  ->pTab->zName;. 
30642 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 53 74     n = sqlite3St
30643 72 6c 65 6e 33 30 28 7a 29 3b 0a 20 20 20 20 69  rlen30(z);.    i
30644 66 28 20 6e 2b 6e 51 50 6c 61 6e 20 3c 20 73 69  f( n+nQPlan < si
30645 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 71 75 65  zeof(sqlite3_que
30646 72 79 5f 70 6c 61 6e 29 2d 31 30 20 29 7b 0a 20  ry_plan)-10 ){. 
30647 20 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d       if( pLevel-
30648 3e 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20  >plan.wsFlags & 
30649 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 20 29  WHERE_IDX_ONLY )
3064a 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79  {.        memcpy
3064b 28 26 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f  (&sqlite3_query_
3064c 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 22 7b  plan[nQPlan], "{
3064d 7d 22 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20  }", 2);.        
3064e 6e 51 50 6c 61 6e 20 2b 3d 20 32 3b 0a 20 20 20  nQPlan += 2;.   
3064f 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
30650 20 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74 65    memcpy(&sqlite
30651 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50  3_query_plan[nQP
30652 6c 61 6e 5d 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20  lan], z, n);.   
30653 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 6e       nQPlan += n
30654 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
30655 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c  sqlite3_query_pl
30656 61 6e 5b 6e 51 50 6c 61 6e 2b 2b 5d 20 3d 20 27  an[nQPlan++] = '
30657 20 27 3b 0a 20 20 20 20 7d 0a 20 20 20 20 74 65   ';.    }.    te
30658 73 74 63 61 73 65 28 20 70 4c 65 76 65 6c 2d 3e  stcase( pLevel->
30659 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
3065a 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 20 29 3b  HERE_ROWID_EQ );
3065b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70  .    testcase( p
3065c 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46 6c  Level->plan.wsFl
3065d 61 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57 49  ags & WHERE_ROWI
3065e 44 5f 52 41 4e 47 45 20 29 3b 0a 20 20 20 20 69  D_RANGE );.    i
3065f 66 28 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e  f( pLevel->plan.
30660 77 73 46 6c 61 67 73 20 26 20 28 57 48 45 52 45  wsFlags & (WHERE
30661 5f 52 4f 57 49 44 5f 45 51 7c 57 48 45 52 45 5f  _ROWID_EQ|WHERE_
30662 52 4f 57 49 44 5f 52 41 4e 47 45 29 20 29 7b 0a  ROWID_RANGE) ){.
30663 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 73 71        memcpy(&sq
30664 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e  lite3_query_plan
30665 5b 6e 51 50 6c 61 6e 5d 2c 20 22 2a 20 22 2c 20  [nQPlan], "* ", 
30666 32 29 3b 0a 20 20 20 20 20 20 6e 51 50 6c 61 6e  2);.      nQPlan
30667 20 2b 3d 20 32 3b 0a 20 20 20 20 7d 65 6c 73 65   += 2;.    }else
30668 20 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c   if( (pLevel->pl
30669 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45  an.wsFlags & WHE
3066a 52 45 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 29  RE_INDEXED)!=0 )
3066b 7b 0a 20 20 20 20 20 20 6e 20 3d 20 73 71 6c 69  {.      n = sqli
3066c 74 65 33 53 74 72 6c 65 6e 33 30 28 70 4c 65 76  te3Strlen30(pLev
3066d 65 6c 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 2d  el->plan.u.pIdx-
3066e 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 69  >zName);.      i
3066f 66 28 20 6e 2b 6e 51 50 6c 61 6e 20 3c 20 73 69  f( n+nQPlan < si
30670 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 71 75 65  zeof(sqlite3_que
30671 72 79 5f 70 6c 61 6e 29 2d 32 20 29 7b 0a 20 20  ry_plan)-2 ){.  
30672 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 73 71        memcpy(&sq
30673 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e  lite3_query_plan
30674 5b 6e 51 50 6c 61 6e 5d 2c 20 70 4c 65 76 65 6c  [nQPlan], pLevel
30675 2d 3e 70 6c 61 6e 2e 75 2e 70 49 64 78 2d 3e 7a  ->plan.u.pIdx->z
30676 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20 20 20 20 20  Name, n);.      
30677 20 20 6e 51 50 6c 61 6e 20 2b 3d 20 6e 3b 0a 20    nQPlan += n;. 
30678 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 71         sqlite3_q
30679 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e  uery_plan[nQPlan
3067a 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 20  ++] = ' ';.     
3067b 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
3067c 20 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c 69      memcpy(&sqli
3067d 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e  te3_query_plan[n
3067e 51 50 6c 61 6e 5d 2c 20 22 7b 7d 20 22 2c 20 33  QPlan], "{} ", 3
3067f 29 3b 0a 20 20 20 20 20 20 6e 51 50 6c 61 6e 20  );.      nQPlan 
30680 2b 3d 20 33 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  += 3;.    }.  }.
30681 20 20 77 68 69 6c 65 28 20 6e 51 50 6c 61 6e 3e    while( nQPlan>
30682 30 20 26 26 20 73 71 6c 69 74 65 33 5f 71 75 65  0 && sqlite3_que
30683 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 2d 31  ry_plan[nQPlan-1
30684 5d 3d 3d 27 20 27 20 29 7b 0a 20 20 20 20 73 71  ]==' ' ){.    sq
30685 6c 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e  lite3_query_plan
30686 5b 2d 2d 6e 51 50 6c 61 6e 5d 20 3d 20 30 3b 0a  [--nQPlan] = 0;.
30687 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 71 75    }.  sqlite3_qu
30688 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d  ery_plan[nQPlan]
30689 20 3d 20 30 3b 0a 20 20 6e 51 50 6c 61 6e 20 3d   = 0;.  nQPlan =
3068a 20 30 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51   0;.#endif /* SQ
3068b 4c 49 54 45 5f 54 45 53 54 20 2f 2f 20 54 65 73  LITE_TEST // Tes
3068c 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69  ting and debuggi
3068d 6e 67 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 0a  ng use only */..
3068e 20 20 2f 2a 20 52 65 63 6f 72 64 20 74 68 65 20    /* Record the 
3068f 63 6f 6e 74 69 6e 75 61 74 69 6f 6e 20 61 64 64  continuation add
30690 72 65 73 73 20 69 6e 20 74 68 65 20 57 68 65 72  ress in the Wher
30691 65 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e  eInfo structure.
30692 20 20 54 68 65 6e 0a 20 20 2a 2a 20 63 6c 65 61    Then.  ** clea
30693 6e 20 75 70 20 61 6e 64 20 72 65 74 75 72 6e 2e  n up and return.
30694 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 70  .  */.  return p
30695 57 49 6e 66 6f 3b 0a 0a 20 20 2f 2a 20 4a 75 6d  WInfo;..  /* Jum
30696 70 20 68 65 72 65 20 69 66 20 6d 61 6c 6c 6f 63  p here if malloc
30697 20 66 61 69 6c 73 20 2a 2f 0a 77 68 65 72 65 42   fails */.whereB
30698 65 67 69 6e 45 72 72 6f 72 3a 0a 20 20 77 68 65  eginError:.  whe
30699 72 65 49 6e 66 6f 46 72 65 65 28 64 62 2c 20 70  reInfoFree(db, p
3069a 57 49 6e 66 6f 29 3b 0a 20 20 72 65 74 75 72 6e  WInfo);.  return
3069b 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e   0;.}../*.** Gen
3069c 65 72 61 74 65 20 74 68 65 20 65 6e 64 20 6f 66  erate the end of
3069d 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 2e   the WHERE loop.
3069e 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 6f    See comments o
3069f 6e 20 0a 2a 2a 20 73 71 6c 69 74 65 33 57 68 65  n .** sqlite3Whe
306a0 72 65 42 65 67 69 6e 28 29 20 66 6f 72 20 61 64  reBegin() for ad
306a1 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
306a2 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
306a3 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
306a4 69 74 65 33 57 68 65 72 65 45 6e 64 28 57 68 65  ite3WhereEnd(Whe
306a5 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 29 7b  reInfo *pWInfo){
306a6 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
306a7 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 50 61 72 73   = pWInfo->pPars
306a8 65 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  e;.  Vdbe *v = p
306a9 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
306aa 69 6e 74 20 69 3b 0a 20 20 57 68 65 72 65 4c 65  int i;.  WhereLe
306ab 76 65 6c 20 2a 70 4c 65 76 65 6c 3b 0a 20 20 53  vel *pLevel;.  S
306ac 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74  rcList *pTabList
306ad 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 54 61 62 4c   = pWInfo->pTabL
306ae 69 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  ist;.  sqlite3 *
306af 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
306b0 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
306b1 6c 6f 6f 70 20 74 65 72 6d 69 6e 61 74 69 6f 6e  loop termination
306b2 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 73 71   code..  */.  sq
306b3 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 43 6c  lite3ExprCacheCl
306b4 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20 20 66  ear(pParse);.  f
306b5 6f 72 28 69 3d 70 54 61 62 4c 69 73 74 2d 3e 6e  or(i=pTabList->n
306b6 53 72 63 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d  Src-1; i>=0; i--
306b7 29 7b 0a 20 20 20 20 70 4c 65 76 65 6c 20 3d 20  ){.    pLevel = 
306b8 26 70 57 49 6e 66 6f 2d 3e 61 5b 69 5d 3b 0a 20  &pWInfo->a[i];. 
306b9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
306ba 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 70 4c  solveLabel(v, pL
306bb 65 76 65 6c 2d 3e 61 64 64 72 43 6f 6e 74 29 3b  evel->addrCont);
306bc 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d  .    if( pLevel-
306bd 3e 6f 70 21 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a  >op!=OP_Noop ){.
306be 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
306bf 65 41 64 64 4f 70 32 28 76 2c 20 70 4c 65 76 65  eAddOp2(v, pLeve
306c0 6c 2d 3e 6f 70 2c 20 70 4c 65 76 65 6c 2d 3e 70  l->op, pLevel->p
306c1 31 2c 20 70 4c 65 76 65 6c 2d 3e 70 32 29 3b 0a  1, pLevel->p2);.
306c2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
306c3 65 43 68 61 6e 67 65 50 35 28 76 2c 20 70 4c 65  eChangeP5(v, pLe
306c4 76 65 6c 2d 3e 70 35 29 3b 0a 20 20 20 20 7d 0a  vel->p5);.    }.
306c5 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e      if( pLevel->
306c6 70 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57  plan.wsFlags & W
306c7 48 45 52 45 5f 49 4e 5f 41 42 4c 45 20 26 26 20  HERE_IN_ABLE && 
306c8 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e  pLevel->u.in.nIn
306c9 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 74 72 75  >0 ){.      stru
306ca 63 74 20 49 6e 4c 6f 6f 70 20 2a 70 49 6e 3b 0a  ct InLoop *pIn;.
306cb 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20        int j;.   
306cc 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
306cd 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 70 4c  solveLabel(v, pL
306ce 65 76 65 6c 2d 3e 61 64 64 72 4e 78 74 29 3b 0a  evel->addrNxt);.
306cf 20 20 20 20 20 20 66 6f 72 28 6a 3d 70 4c 65 76        for(j=pLev
306d0 65 6c 2d 3e 75 2e 69 6e 2e 6e 49 6e 2c 20 70 49  el->u.in.nIn, pI
306d1 6e 3d 26 70 4c 65 76 65 6c 2d 3e 75 2e 69 6e 2e  n=&pLevel->u.in.
306d2 61 49 6e 4c 6f 6f 70 5b 6a 2d 31 5d 3b 20 6a 3e  aInLoop[j-1]; j>
306d3 30 3b 20 6a 2d 2d 2c 20 70 49 6e 2d 2d 29 7b 0a  0; j--, pIn--){.
306d4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
306d5 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 70  dbeJumpHere(v, p
306d6 49 6e 2d 3e 61 64 64 72 49 6e 54 6f 70 2b 31 29  In->addrInTop+1)
306d7 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
306d8 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
306d9 50 5f 4e 65 78 74 2c 20 70 49 6e 2d 3e 69 43 75  P_Next, pIn->iCu
306da 72 2c 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f  r, pIn->addrInTo
306db 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  p);.        sqli
306dc 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
306dd 76 2c 20 70 49 6e 2d 3e 61 64 64 72 49 6e 54 6f  v, pIn->addrInTo
306de 70 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  p-1);.      }.  
306df 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
306e0 65 28 64 62 2c 20 70 4c 65 76 65 6c 2d 3e 75 2e  e(db, pLevel->u.
306e1 69 6e 2e 61 49 6e 4c 6f 6f 70 29 3b 0a 20 20 20  in.aInLoop);.   
306e2 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
306e3 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
306e4 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72 42 72  , pLevel->addrBr
306e5 6b 29 3b 0a 20 20 20 20 69 66 28 20 70 4c 65 76  k);.    if( pLev
306e6 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 29 7b  el->iLeftJoin ){
306e7 0a 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b  .      int addr;
306e8 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71  .      addr = sq
306e9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
306ea 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 70 4c 65  v, OP_IfPos, pLe
306eb 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f 69 6e 29 3b  vel->iLeftJoin);
306ec 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
306ed 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e  beAddOp1(v, OP_N
306ee 75 6c 6c 52 6f 77 2c 20 70 54 61 62 4c 69 73 74  ullRow, pTabList
306ef 2d 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 29 3b  ->a[i].iCursor);
306f0 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65 76 65  .      if( pLeve
306f1 6c 2d 3e 69 49 64 78 43 75 72 3e 3d 30 20 29 7b  l->iIdxCur>=0 ){
306f2 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
306f3 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
306f4 5f 4e 75 6c 6c 52 6f 77 2c 20 70 4c 65 76 65 6c  _NullRow, pLevel
306f5 2d 3e 69 49 64 78 43 75 72 29 3b 0a 20 20 20 20  ->iIdxCur);.    
306f6 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4c    }.      if( pL
306f7 65 76 65 6c 2d 3e 6f 70 3d 3d 4f 50 5f 52 65 74  evel->op==OP_Ret
306f8 75 72 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 73  urn ){.        s
306f9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
306fa 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20 70 4c  (v, OP_Gosub, pL
306fb 65 76 65 6c 2d 3e 70 31 2c 20 70 4c 65 76 65 6c  evel->p1, pLevel
306fc 2d 3e 61 64 64 72 46 69 72 73 74 29 3b 0a 20 20  ->addrFirst);.  
306fd 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
306fe 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
306ff 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
30700 20 30 2c 20 70 4c 65 76 65 6c 2d 3e 61 64 64 72   0, pLevel->addr
30701 46 69 72 73 74 29 3b 0a 20 20 20 20 20 20 7d 0a  First);.      }.
30702 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
30703 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
30704 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  r);.    }.  }.. 
30705 20 2f 2a 20 54 68 65 20 22 62 72 65 61 6b 22 20   /* The "break" 
30706 70 6f 69 6e 74 20 69 73 20 68 65 72 65 2c 20 6a  point is here, j
30707 75 73 74 20 70 61 73 74 20 74 68 65 20 65 6e 64  ust past the end
30708 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20 6c 6f   of the outer lo
30709 6f 70 2e 0a 20 20 2a 2a 20 53 65 74 20 69 74 2e  op..  ** Set it.
3070a 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  .  */.  sqlite3V
3070b 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
3070c 76 2c 20 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61  v, pWInfo->iBrea
3070d 6b 29 3b 0a 0a 20 20 2f 2a 20 43 6c 6f 73 65 20  k);..  /* Close 
3070e 61 6c 6c 20 6f 66 20 74 68 65 20 63 75 72 73 6f  all of the curso
3070f 72 73 20 74 68 61 74 20 77 65 72 65 20 6f 70 65  rs that were ope
30710 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33 57 68  ned by sqlite3Wh
30711 65 72 65 42 65 67 69 6e 2e 0a 20 20 2a 2f 0a 20  ereBegin..  */. 
30712 20 66 6f 72 28 69 3d 30 2c 20 70 4c 65 76 65 6c   for(i=0, pLevel
30713 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20 69 3c 70 54  =pWInfo->a; i<pT
30714 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  abList->nSrc; i+
30715 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29 7b 0a 20 20  +, pLevel++){.  
30716 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74    struct SrcList
30717 5f 69 74 65 6d 20 2a 70 54 61 62 49 74 65 6d 20  _item *pTabItem 
30718 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70  = &pTabList->a[p
30719 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20  Level->iFrom];. 
3071a 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
3071b 20 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 3b   pTabItem->pTab;
3071c 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61  .    assert( pTa
3071d 62 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  b!=0 );.    if( 
3071e 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20  (pTab->tabFlags 
3071f 26 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 29 21  & TF_Ephemeral)!
30720 3d 30 20 7c 7c 20 70 54 61 62 2d 3e 70 53 65 6c  =0 || pTab->pSel
30721 65 63 74 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ect ) continue;.
30722 20 20 20 20 69 66 28 20 28 70 57 49 6e 66 6f 2d      if( (pWInfo-
30723 3e 77 63 74 72 6c 46 6c 61 67 73 20 26 20 57 48  >wctrlFlags & WH
30724 45 52 45 5f 4f 4d 49 54 5f 43 4c 4f 53 45 29 3d  ERE_OMIT_CLOSE)=
30725 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  =0 ){.      if( 
30726 21 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61  !pWInfo->okOnePa
30727 73 73 20 26 26 20 28 70 4c 65 76 65 6c 2d 3e 70  ss && (pLevel->p
30728 6c 61 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48  lan.wsFlags & WH
30729 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29 3d 3d 30  ERE_IDX_ONLY)==0
3072a 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
3072b 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
3072c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70 54 61 62 49   OP_Close, pTabI
3072d 74 65 6d 2d 3e 69 43 75 72 73 6f 72 29 3b 0a 20  tem->iCursor);. 
3072e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
3072f 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77   (pLevel->plan.w
30730 73 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49  sFlags & WHERE_I
30731 4e 44 45 58 45 44 29 21 3d 30 20 29 7b 0a 20 20  NDEXED)!=0 ){.  
30732 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
30733 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
30734 6f 73 65 2c 20 70 4c 65 76 65 6c 2d 3e 69 49 64  ose, pLevel->iId
30735 78 43 75 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  xCur);.      }. 
30736 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20     }..    /* If 
30737 74 68 69 73 20 73 63 61 6e 20 75 73 65 73 20 61  this scan uses a
30738 6e 20 69 6e 64 65 78 2c 20 6d 61 6b 65 20 63 6f  n index, make co
30739 64 65 20 73 75 62 73 74 69 74 75 74 69 6f 6e 73  de substitutions
3073a 20 74 6f 20 72 65 61 64 20 64 61 74 61 0a 20 20   to read data.  
3073b 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 69 6e    ** from the in
3073c 64 65 78 20 69 6e 20 70 72 65 66 65 72 65 6e 63  dex in preferenc
3073d 65 20 74 6f 20 74 68 65 20 74 61 62 6c 65 2e 20  e to the table. 
3073e 53 6f 6d 65 74 69 6d 65 73 2c 20 74 68 69 73 20  Sometimes, this 
3073f 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 65  means.    ** the
30740 20 74 61 62 6c 65 20 6e 65 65 64 20 6e 65 76 65   table need neve
30741 72 20 62 65 20 72 65 61 64 20 66 72 6f 6d 2e 20  r be read from. 
30742 54 68 69 73 20 69 73 20 61 20 70 65 72 66 6f 72  This is a perfor
30743 6d 61 6e 63 65 20 62 6f 6f 73 74 2c 0a 20 20 20  mance boost,.   
30744 20 2a 2a 20 61 73 20 74 68 65 20 76 64 62 65 20   ** as the vdbe 
30745 6c 65 76 65 6c 20 77 61 69 74 73 20 75 6e 74 69  level waits unti
30746 6c 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 72  l the table is r
30747 65 61 64 20 62 65 66 6f 72 65 20 61 63 74 75 61  ead before actua
30748 6c 6c 79 0a 20 20 20 20 2a 2a 20 73 65 65 6b 69  lly.    ** seeki
30749 6e 67 20 74 68 65 20 74 61 62 6c 65 20 63 75 72  ng the table cur
3074a 73 6f 72 20 74 6f 20 74 68 65 20 72 65 63 6f 72  sor to the recor
3074b 64 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  d corresponding 
3074c 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 0a 20  to the current. 
3074d 20 20 20 2a 2a 20 70 6f 73 69 74 69 6f 6e 20 69     ** position i
3074e 6e 20 74 68 65 20 69 6e 64 65 78 2e 0a 20 20 20  n the index..   
3074f 20 2a 2a 20 0a 20 20 20 20 2a 2a 20 43 61 6c 6c   ** .    ** Call
30750 73 20 74 6f 20 74 68 65 20 63 6f 64 65 20 67 65  s to the code ge
30751 6e 65 72 61 74 6f 72 20 69 6e 20 62 65 74 77 65  nerator in betwe
30752 65 6e 20 73 71 6c 69 74 65 33 57 68 65 72 65 42  en sqlite3WhereB
30753 65 67 69 6e 20 61 6e 64 0a 20 20 20 20 2a 2a 20  egin and.    ** 
30754 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64 20  sqlite3WhereEnd 
30755 77 69 6c 6c 20 68 61 76 65 20 63 72 65 61 74 65  will have create
30756 64 20 63 6f 64 65 20 74 68 61 74 20 72 65 66 65  d code that refe
30757 72 65 6e 63 65 73 20 74 68 65 20 74 61 62 6c 65  rences the table
30758 0a 20 20 20 20 2a 2a 20 64 69 72 65 63 74 6c 79  .    ** directly
30759 2e 20 20 54 68 69 73 20 6c 6f 6f 70 20 73 63 61  .  This loop sca
3075a 6e 73 20 61 6c 6c 20 74 68 61 74 20 63 6f 64 65  ns all that code
3075b 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 6f 70 63   looking for opc
3075c 6f 64 65 73 0a 20 20 20 20 2a 2a 20 74 68 61 74  odes.    ** that
3075d 20 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 74   reference the t
3075e 61 62 6c 65 20 61 6e 64 20 63 6f 6e 76 65 72 74  able and convert
3075f 73 20 74 68 65 6d 20 69 6e 74 6f 20 6f 70 63 6f  s them into opco
30760 64 65 73 20 74 68 61 74 0a 20 20 20 20 2a 2a 20  des that.    ** 
30761 72 65 66 65 72 65 6e 63 65 20 74 68 65 20 69 6e  reference the in
30762 64 65 78 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  dex..    */.    
30763 69 66 28 20 28 70 4c 65 76 65 6c 2d 3e 70 6c 61  if( (pLevel->pla
30764 6e 2e 77 73 46 6c 61 67 73 20 26 20 57 48 45 52  n.wsFlags & WHER
30765 45 5f 49 4e 44 45 58 45 44 29 21 3d 30 20 26 26  E_INDEXED)!=0 &&
30766 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
30767 65 64 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6b  ed){.      int k
30768 2c 20 6a 2c 20 6c 61 73 74 3b 0a 20 20 20 20 20  , j, last;.     
30769 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20   VdbeOp *pOp;.  
3076a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20      Index *pIdx 
3076b 3d 20 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 75  = pLevel->plan.u
3076c 2e 70 49 64 78 3b 0a 20 20 20 20 20 20 69 6e 74  .pIdx;.      int
3076d 20 75 73 65 49 6e 64 65 78 4f 6e 6c 79 20 3d 20   useIndexOnly = 
3076e 70 4c 65 76 65 6c 2d 3e 70 6c 61 6e 2e 77 73 46  pLevel->plan.wsF
3076f 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49 44 58  lags & WHERE_IDX
30770 5f 4f 4e 4c 59 3b 0a 0a 20 20 20 20 20 20 61 73  _ONLY;..      as
30771 73 65 72 74 28 20 70 49 64 78 21 3d 30 20 29 3b  sert( pIdx!=0 );
30772 0a 20 20 20 20 20 20 70 4f 70 20 3d 20 73 71 6c  .      pOp = sql
30773 69 74 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c  ite3VdbeGetOp(v,
30774 20 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 29 3b 0a   pWInfo->iTop);.
30775 20 20 20 20 20 20 6c 61 73 74 20 3d 20 73 71 6c        last = sql
30776 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
30777 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 66 6f  ddr(v);.      fo
30778 72 28 6b 3d 70 57 49 6e 66 6f 2d 3e 69 54 6f 70  r(k=pWInfo->iTop
30779 3b 20 6b 3c 6c 61 73 74 3b 20 6b 2b 2b 2c 20 70  ; k<last; k++, p
3077a 4f 70 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69  Op++){.        i
3077b 66 28 20 70 4f 70 2d 3e 70 31 21 3d 70 4c 65 76  f( pOp->p1!=pLev
3077c 65 6c 2d 3e 69 54 61 62 43 75 72 20 29 20 63 6f  el->iTabCur ) co
3077d 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20  ntinue;.        
3077e 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
3077f 3d 4f 50 5f 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20  =OP_Column ){.  
30780 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b          for(j=0;
30781 20 6a 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   j<pIdx->nColumn
30782 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
30783 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d      if( pOp->p2=
30784 3d 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  =pIdx->aiColumn[
30785 6a 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j] ){.          
30786 20 20 20 20 70 4f 70 2d 3e 70 32 20 3d 20 6a 3b      pOp->p2 = j;
30787 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
30788 4f 70 2d 3e 70 31 20 3d 20 70 4c 65 76 65 6c 2d  Op->p1 = pLevel-
30789 3e 69 49 64 78 43 75 72 3b 0a 20 20 20 20 20 20  >iIdxCur;.      
3078a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3078b 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
3078c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
3078d 20 20 20 61 73 73 65 72 74 28 21 75 73 65 49 6e     assert(!useIn
3078e 64 65 78 4f 6e 6c 79 20 7c 7c 20 6a 3c 70 49 64  dexOnly || j<pId
3078f 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  x->nColumn);.   
30790 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70       }else if( p
30791 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52  Op->opcode==OP_R
30792 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20  owid ){.        
30793 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70 4c 65 76    pOp->p1 = pLev
30794 65 6c 2d 3e 69 49 64 78 43 75 72 3b 0a 20 20 20  el->iIdxCur;.   
30795 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f         pOp->opco
30796 64 65 20 3d 20 4f 50 5f 49 64 78 52 6f 77 69 64  de = OP_IdxRowid
30797 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  ;.        }else 
30798 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
30799 3d 4f 50 5f 4e 75 6c 6c 52 6f 77 20 26 26 20 75  =OP_NullRow && u
3079a 73 65 49 6e 64 65 78 4f 6e 6c 79 20 29 7b 0a 20  seIndexOnly ){. 
3079b 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f 70           pOp->op
3079c 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a  code = OP_Noop;.
3079d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
3079e 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  }.    }.  }..  /
3079f 2a 20 46 69 6e 61 6c 20 63 6c 65 61 6e 75 70 0a  * Final cleanup.
307a0 20 20 2a 2f 0a 20 20 77 68 65 72 65 49 6e 66 6f    */.  whereInfo
307a1 46 72 65 65 28 64 62 2c 20 70 57 49 6e 66 6f 29  Free(db, pWInfo)
307a2 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f  ;.  return;.}../
307a3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
307a4 6e 64 20 6f 66 20 77 68 65 72 65 2e 63 20 2a 2a  nd of where.c **
307a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
307a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
307a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
307a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
307a9 65 67 69 6e 20 66 69 6c 65 20 70 61 72 73 65 2e  egin file parse.
307aa 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
307ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
307ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
307ad 2a 20 44 72 69 76 65 72 20 74 65 6d 70 6c 61 74  * Driver templat
307ae 65 20 66 6f 72 20 74 68 65 20 4c 45 4d 4f 4e 20  e for the LEMON 
307af 70 61 72 73 65 72 20 67 65 6e 65 72 61 74 6f 72  parser generator
307b0 2e 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  ..** The author 
307b1 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
307b2 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
307b3 63 65 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54  ce code..**.** T
307b4 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 22  his version of "
307b5 6c 65 6d 70 61 72 2e 63 22 20 69 73 20 6d 6f 64  lempar.c" is mod
307b6 69 66 69 65 64 2c 20 73 6c 69 67 68 74 6c 79 2c  ified, slightly,
307b7 20 66 6f 72 20 75 73 65 20 62 79 20 53 51 4c 69   for use by SQLi
307b8 74 65 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20  te..** The only 
307b9 6d 6f 64 69 66 69 63 61 74 69 6f 6e 73 20 61 72  modifications ar
307ba 65 20 74 68 65 20 61 64 64 69 74 69 6f 6e 20 6f  e the addition o
307bb 66 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 4e 45  f a couple of NE
307bc 56 45 52 28 29 0a 2a 2a 20 6d 61 63 72 6f 73 20  VER().** macros 
307bd 74 6f 20 64 69 73 61 62 6c 65 20 74 65 73 74 73  to disable tests
307be 20 74 68 61 74 20 61 72 65 20 6e 65 65 64 65 64   that are needed
307bf 20 69 6e 20 74 68 65 20 63 61 73 65 20 6f 66 20   in the case of 
307c0 61 20 67 65 6e 65 72 61 6c 0a 2a 2a 20 4c 41 4c  a general.** LAL
307c1 52 28 31 29 20 67 72 61 6d 6d 61 72 20 62 75 74  R(1) grammar but
307c2 20 77 68 69 63 68 20 61 72 65 20 61 6c 77 61 79   which are alway
307c3 73 20 66 61 6c 73 65 20 69 6e 20 74 68 65 0a 2a  s false in the.*
307c4 2a 20 73 70 65 63 69 66 69 63 20 67 72 61 6d 6d  * specific gramm
307c5 61 72 20 75 73 65 64 20 62 79 20 53 51 4c 69 74  ar used by SQLit
307c6 65 2e 0a 2a 2f 0a 2f 2a 20 46 69 72 73 74 20 6f  e..*/./* First o
307c7 66 66 2c 20 63 6f 64 65 20 69 73 20 69 6e 63 6c  ff, code is incl
307c8 75 64 65 64 20 74 68 61 74 20 66 6f 6c 6c 6f 77  uded that follow
307c9 73 20 74 68 65 20 22 69 6e 63 6c 75 64 65 22 20  s the "include" 
307ca 64 65 63 6c 61 72 61 74 69 6f 6e 0a 2a 2a 20 69  declaration.** i
307cb 6e 20 74 68 65 20 69 6e 70 75 74 20 67 72 61 6d  n the input gram
307cc 6d 61 72 20 66 69 6c 65 2e 20 2a 2f 0a 0a 0a 2f  mar file. */.../
307cd 2a 0a 2a 2a 20 44 69 73 61 62 6c 65 20 61 6c 6c  *.** Disable all
307ce 20 65 72 72 6f 72 20 72 65 63 6f 76 65 72 79 20   error recovery 
307cf 70 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 74 68  processing in th
307d0 65 20 70 61 72 73 65 72 20 70 75 73 68 2d 64 6f  e parser push-do
307d1 77 6e 0a 2a 2a 20 61 75 74 6f 6d 61 74 6f 6e 2e  wn.** automaton.
307d2 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 59 59 4e 4f  .*/.#define YYNO
307d3 45 52 52 4f 52 52 45 43 4f 56 45 52 59 20 31 0a  ERRORRECOVERY 1.
307d4 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 79 79 74 65  ./*.** Make yyte
307d5 73 74 63 61 73 65 28 29 20 74 68 65 20 73 61 6d  stcase() the sam
307d6 65 20 61 73 20 74 65 73 74 63 61 73 65 28 29 0a  e as testcase().
307d7 2a 2f 0a 23 64 65 66 69 6e 65 20 79 79 74 65 73  */.#define yytes
307d8 74 63 61 73 65 28 58 29 20 74 65 73 74 63 61 73  tcase(X) testcas
307d9 65 28 58 29 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  e(X)../*.** An i
307da 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
307db 73 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20  structure holds 
307dc 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  information abou
307dd 74 20 74 68 65 0a 2a 2a 20 4c 49 4d 49 54 20 63  t the.** LIMIT c
307de 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45 43  lause of a SELEC
307df 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a  T statement..*/.
307e0 73 74 72 75 63 74 20 4c 69 6d 69 74 56 61 6c 20  struct LimitVal 
307e1 7b 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d 69 74  {.  Expr *pLimit
307e2 3b 20 20 20 20 2f 2a 20 54 68 65 20 4c 49 4d 49  ;    /* The LIMI
307e3 54 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 4e  T expression.  N
307e4 55 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73 20  ULL if there is 
307e5 6e 6f 20 6c 69 6d 69 74 20 2a 2f 0a 20 20 45 78  no limit */.  Ex
307e6 70 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20 2f  pr *pOffset;   /
307e7 2a 20 54 68 65 20 4f 46 46 53 45 54 20 65 78 70  * The OFFSET exp
307e8 72 65 73 73 69 6f 6e 2e 20 20 4e 55 4c 4c 20 69  ression.  NULL i
307e9 66 20 74 68 65 72 65 20 69 73 20 6e 6f 6e 65 20  f there is none 
307ea 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  */.};../*.** An 
307eb 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73  instance of this
307ec 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73   structure is us
307ed 65 64 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  ed to store the 
307ee 4c 49 4b 45 2c 0a 2a 2a 20 47 4c 4f 42 2c 20 4e  LIKE,.** GLOB, N
307ef 4f 54 20 4c 49 4b 45 2c 20 61 6e 64 20 4e 4f 54  OT LIKE, and NOT
307f0 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 73 2e   GLOB operators.
307f1 0a 2a 2f 0a 73 74 72 75 63 74 20 4c 69 6b 65 4f  .*/.struct LikeO
307f2 70 20 7b 0a 20 20 54 6f 6b 65 6e 20 65 4f 70 65  p {.  Token eOpe
307f3 72 61 74 6f 72 3b 20 20 2f 2a 20 22 6c 69 6b 65  rator;  /* "like
307f4 22 20 6f 72 20 22 67 6c 6f 62 22 20 6f 72 20 22  " or "glob" or "
307f5 72 65 67 65 78 70 22 20 2a 2f 0a 20 20 69 6e 74  regexp" */.  int
307f6 20 6e 6f 74 3b 20 20 20 20 20 20 20 20 20 2f 2a   not;         /*
307f7 20 54 72 75 65 20 69 66 20 74 68 65 20 4e 4f 54   True if the NOT
307f8 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73   keyword is pres
307f9 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ent */.};../*.**
307fa 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
307fb 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
307fc 72 75 63 74 75 72 65 20 64 65 73 63 72 69 62 65  ructure describe
307fd 73 20 74 68 65 20 65 76 65 6e 74 20 6f 66 20 61  s the event of a
307fe 0a 2a 2a 20 54 52 49 47 47 45 52 2e 20 20 22 61  .** TRIGGER.  "a
307ff 22 20 69 73 20 74 68 65 20 65 76 65 6e 74 20 74  " is the event t
30800 79 70 65 2c 20 6f 6e 65 20 6f 66 20 54 4b 5f 55  ype, one of TK_U
30801 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45 52 54  PDATE, TK_INSERT
30802 2c 0a 2a 2a 20 54 4b 5f 44 45 4c 45 54 45 2c 20  ,.** TK_DELETE, 
30803 6f 72 20 54 4b 5f 49 4e 53 54 45 41 44 2e 20 20  or TK_INSTEAD.  
30804 49 66 20 74 68 65 20 65 76 65 6e 74 20 69 73 20  If the event is 
30805 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a  of the form.**.*
30806 2a 20 20 20 20 20 20 55 50 44 41 54 45 20 4f 4e  *      UPDATE ON
30807 20 28 61 2c 62 2c 63 29 0a 2a 2a 0a 2a 2a 20 54   (a,b,c).**.** T
30808 68 65 6e 20 74 68 65 20 22 62 22 20 49 64 4c 69  hen the "b" IdLi
30809 73 74 20 72 65 63 6f 72 64 73 20 74 68 65 20 6c  st records the l
3080a 69 73 74 20 22 61 2c 62 2c 63 22 2e 0a 2a 2f 0a  ist "a,b,c"..*/.
3080b 73 74 72 75 63 74 20 54 72 69 67 45 76 65 6e 74  struct TrigEvent
3080c 20 7b 20 69 6e 74 20 61 3b 20 49 64 4c 69 73 74   { int a; IdList
3080d 20 2a 20 62 3b 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20   * b; };../*.** 
3080e 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
3080f 68 69 73 20 73 74 72 75 63 74 75 72 65 20 68 6f  his structure ho
30810 6c 64 73 20 74 68 65 20 41 54 54 41 43 48 20 6b  lds the ATTACH k
30811 65 79 20 61 6e 64 20 74 68 65 20 6b 65 79 20 74  ey and the key t
30812 79 70 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 41  ype..*/.struct A
30813 74 74 61 63 68 4b 65 79 20 7b 20 69 6e 74 20 74  ttachKey { int t
30814 79 70 65 3b 20 20 54 6f 6b 65 6e 20 6b 65 79 3b  ype;  Token key;
30815 20 7d 3b 0a 0a 0a 20 20 2f 2a 20 54 68 69 73 20   };...  /* This 
30816 69 73 20 61 20 75 74 69 6c 69 74 79 20 72 6f 75  is a utility rou
30817 74 69 6e 65 20 75 73 65 64 20 74 6f 20 73 65 74  tine used to set
30818 20 74 68 65 20 45 78 70 72 53 70 61 6e 2e 7a 53   the ExprSpan.zS
30819 74 61 72 74 20 61 6e 64 0a 20 20 2a 2a 20 45 78  tart and.  ** Ex
3081a 70 72 53 70 61 6e 2e 7a 45 6e 64 20 76 61 6c 75  prSpan.zEnd valu
3081b 65 73 20 6f 66 20 70 4f 75 74 20 73 6f 20 74 68  es of pOut so th
3081c 61 74 20 74 68 65 20 73 70 61 6e 20 63 6f 76 65  at the span cove
3081d 72 73 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 0a  rs the complete.
3081e 20 20 2a 2a 20 72 61 6e 67 65 20 6f 66 20 74 65    ** range of te
3081f 78 74 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74  xt beginning wit
30820 68 20 70 53 74 61 72 74 20 61 6e 64 20 67 6f 69  h pStart and goi
30821 6e 67 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  ng to the end of
30822 20 70 45 6e 64 2e 0a 20 20 2a 2f 0a 20 20 73 74   pEnd..  */.  st
30823 61 74 69 63 20 76 6f 69 64 20 73 70 61 6e 53 65  atic void spanSe
30824 74 28 45 78 70 72 53 70 61 6e 20 2a 70 4f 75 74  t(ExprSpan *pOut
30825 2c 20 54 6f 6b 65 6e 20 2a 70 53 74 61 72 74 2c  , Token *pStart,
30826 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 29 7b 0a 20   Token *pEnd){. 
30827 20 20 20 70 4f 75 74 2d 3e 7a 53 74 61 72 74 20     pOut->zStart 
30828 3d 20 70 53 74 61 72 74 2d 3e 7a 3b 0a 20 20 20  = pStart->z;.   
30829 20 70 4f 75 74 2d 3e 7a 45 6e 64 20 3d 20 26 70   pOut->zEnd = &p
3082a 45 6e 64 2d 3e 7a 5b 70 45 6e 64 2d 3e 6e 5d 3b  End->z[pEnd->n];
3082b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74  .  }..  /* Const
3082c 72 75 63 74 20 61 20 6e 65 77 20 45 78 70 72 20  ruct a new Expr 
3082d 6f 62 6a 65 63 74 20 66 72 6f 6d 20 61 20 73 69  object from a si
3082e 6e 67 6c 65 20 69 64 65 6e 74 69 66 69 65 72 2e  ngle identifier.
3082f 20 20 55 73 65 20 74 68 65 0a 20 20 2a 2a 20 6e    Use the.  ** n
30830 65 77 20 45 78 70 72 20 74 6f 20 70 6f 70 75 6c  ew Expr to popul
30831 61 74 65 20 70 4f 75 74 2e 20 20 53 65 74 20 74  ate pOut.  Set t
30832 68 65 20 73 70 61 6e 20 6f 66 20 70 4f 75 74 20  he span of pOut 
30833 74 6f 20 62 65 20 74 68 65 20 69 64 65 6e 74 69  to be the identi
30834 66 69 65 72 0a 20 20 2a 2a 20 74 68 61 74 20 63  fier.  ** that c
30835 72 65 61 74 65 64 20 74 68 65 20 65 78 70 72 65  reated the expre
30836 73 73 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 74  ssion..  */.  st
30837 61 74 69 63 20 76 6f 69 64 20 73 70 61 6e 45 78  atic void spanEx
30838 70 72 28 45 78 70 72 53 70 61 6e 20 2a 70 4f 75  pr(ExprSpan *pOu
30839 74 2c 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  t, Parse *pParse
3083a 2c 20 69 6e 74 20 6f 70 2c 20 54 6f 6b 65 6e 20  , int op, Token 
3083b 2a 70 56 61 6c 75 65 29 7b 0a 20 20 20 20 70 4f  *pValue){.    pO
3083c 75 74 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c 69  ut->pExpr = sqli
3083d 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
3083e 20 6f 70 2c 20 30 2c 20 30 2c 20 70 56 61 6c 75   op, 0, 0, pValu
3083f 65 29 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 53  e);.    pOut->zS
30840 74 61 72 74 20 3d 20 70 56 61 6c 75 65 2d 3e 7a  tart = pValue->z
30841 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e 64  ;.    pOut->zEnd
30842 20 3d 20 26 70 56 61 6c 75 65 2d 3e 7a 5b 70 56   = &pValue->z[pV
30843 61 6c 75 65 2d 3e 6e 5d 3b 0a 20 20 7d 0a 0a 20  alue->n];.  }.. 
30844 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65   /* This routine
30845 20 63 6f 6e 73 74 72 75 63 74 73 20 61 20 62 69   constructs a bi
30846 6e 61 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20  nary expression 
30847 6e 6f 64 65 20 6f 75 74 20 6f 66 20 74 77 6f 20  node out of two 
30848 45 78 70 72 53 70 61 6e 0a 20 20 2a 2a 20 6f 62  ExprSpan.  ** ob
30849 6a 65 63 74 73 20 61 6e 64 20 75 73 65 73 20 74  jects and uses t
3084a 68 65 20 72 65 73 75 6c 74 20 74 6f 20 70 6f 70  he result to pop
3084b 75 6c 61 74 65 20 61 20 6e 65 77 20 45 78 70 72  ulate a new Expr
3084c 53 70 61 6e 20 6f 62 6a 65 63 74 2e 0a 20 20 2a  Span object..  *
3084d 2f 0a 20 20 73 74 61 74 69 63 20 76 6f 69 64 20  /.  static void 
3084e 73 70 61 6e 42 69 6e 61 72 79 45 78 70 72 28 0a  spanBinaryExpr(.
3084f 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70 4f      ExprSpan *pO
30850 75 74 2c 20 20 20 20 20 2f 2a 20 57 72 69 74 65  ut,     /* Write
30851 20 74 68 65 20 72 65 73 75 6c 74 20 68 65 72 65   the result here
30852 20 2a 2f 0a 20 20 20 20 50 61 72 73 65 20 2a 70   */.    Parse *p
30853 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 54  Parse,      /* T
30854 68 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65  he parsing conte
30855 78 74 2e 20 20 45 72 72 6f 72 73 20 61 63 63 75  xt.  Errors accu
30856 6d 75 6c 61 74 65 20 68 65 72 65 20 2a 2f 0a 20  mulate here */. 
30857 20 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20     int op,      
30858 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 69         /* The bi
30859 6e 61 72 79 20 6f 70 65 72 61 74 69 6f 6e 20 2a  nary operation *
3085a 2f 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a  /.    ExprSpan *
3085b 70 4c 65 66 74 2c 20 20 20 20 2f 2a 20 54 68 65  pLeft,    /* The
3085c 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f   left operand */
3085d 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70  .    ExprSpan *p
3085e 52 69 67 68 74 20 20 20 20 2f 2a 20 54 68 65 20  Right    /* The 
3085f 72 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f  right operand */
30860 0a 20 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e  .  ){.    pOut->
30861 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50  pExpr = sqlite3P
30862 45 78 70 72 28 70 50 61 72 73 65 2c 20 6f 70 2c  Expr(pParse, op,
30863 20 70 4c 65 66 74 2d 3e 70 45 78 70 72 2c 20 70   pLeft->pExpr, p
30864 52 69 67 68 74 2d 3e 70 45 78 70 72 2c 20 30 29  Right->pExpr, 0)
30865 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 53 74 61  ;.    pOut->zSta
30866 72 74 20 3d 20 70 4c 65 66 74 2d 3e 7a 53 74 61  rt = pLeft->zSta
30867 72 74 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45  rt;.    pOut->zE
30868 6e 64 20 3d 20 70 52 69 67 68 74 2d 3e 7a 45 6e  nd = pRight->zEn
30869 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e  d;.  }..  /* Con
3086a 73 74 72 75 63 74 20 61 6e 20 65 78 70 72 65 73  struct an expres
3086b 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20 61 20  sion node for a 
3086c 75 6e 61 72 79 20 70 6f 73 74 66 69 78 20 6f 70  unary postfix op
3086d 65 72 61 74 6f 72 0a 20 20 2a 2f 0a 20 20 73 74  erator.  */.  st
3086e 61 74 69 63 20 76 6f 69 64 20 73 70 61 6e 55 6e  atic void spanUn
3086f 61 72 79 50 6f 73 74 66 69 78 28 0a 20 20 20 20  aryPostfix(.    
30870 45 78 70 72 53 70 61 6e 20 2a 70 4f 75 74 2c 20  ExprSpan *pOut, 
30871 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
30872 74 68 65 20 6e 65 77 20 65 78 70 72 65 73 73 69  the new expressi
30873 6f 6e 20 6e 6f 64 65 20 68 65 72 65 20 2a 2f 0a  on node here */.
30874 20 20 20 20 50 61 72 73 65 20 2a 70 50 61 72 73      Parse *pPars
30875 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61  e,         /* Pa
30876 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 74 6f  rsing context to
30877 20 72 65 63 6f 72 64 20 65 72 72 6f 72 73 20 2a   record errors *
30878 2f 0a 20 20 20 20 69 6e 74 20 6f 70 2c 20 20 20  /.    int op,   
30879 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3087a 54 68 65 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a  The operator */.
3087b 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a 70 4f      ExprSpan *pO
3087c 70 65 72 61 6e 64 2c 20 20 20 20 2f 2a 20 54 68  perand,    /* Th
3087d 65 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20  e operand */.   
3087e 20 54 6f 6b 65 6e 20 2a 70 50 6f 73 74 4f 70 20   Token *pPostOp 
3087f 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f          /* The o
30880 70 65 72 61 6e 64 20 74 6f 6b 65 6e 20 66 6f 72  perand token for
30881 20 73 65 74 74 69 6e 67 20 74 68 65 20 73 70 61   setting the spa
30882 6e 20 2a 2f 0a 20 20 29 7b 0a 20 20 20 20 70 4f  n */.  ){.    pO
30883 75 74 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c 69  ut->pExpr = sqli
30884 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
30885 20 6f 70 2c 20 70 4f 70 65 72 61 6e 64 2d 3e 70   op, pOperand->p
30886 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20  Expr, 0, 0);.   
30887 20 70 4f 75 74 2d 3e 7a 53 74 61 72 74 20 3d 20   pOut->zStart = 
30888 70 4f 70 65 72 61 6e 64 2d 3e 7a 53 74 61 72 74  pOperand->zStart
30889 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 7a 45 6e 64  ;.    pOut->zEnd
3088a 20 3d 20 26 70 50 6f 73 74 4f 70 2d 3e 7a 5b 70   = &pPostOp->z[p
3088b 50 6f 73 74 4f 70 2d 3e 6e 5d 3b 0a 20 20 7d 20  PostOp->n];.  } 
3088c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3088d 20 20 20 20 20 20 20 20 20 20 0a 0a 20 20 2f 2a            ..  /*
3088e 20 43 6f 6e 73 74 72 75 63 74 20 61 6e 20 65 78   Construct an ex
3088f 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f  pression node fo
30890 72 20 61 20 75 6e 61 72 79 20 70 72 65 66 69 78  r a unary prefix
30891 20 6f 70 65 72 61 74 6f 72 0a 20 20 2a 2f 0a 20   operator.  */. 
30892 20 73 74 61 74 69 63 20 76 6f 69 64 20 73 70 61   static void spa
30893 6e 55 6e 61 72 79 50 72 65 66 69 78 28 0a 20 20  nUnaryPrefix(.  
30894 20 20 45 78 70 72 53 70 61 6e 20 2a 70 4f 75 74    ExprSpan *pOut
30895 2c 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74  ,        /* Writ
30896 65 20 74 68 65 20 6e 65 77 20 65 78 70 72 65 73  e the new expres
30897 73 69 6f 6e 20 6e 6f 64 65 20 68 65 72 65 20 2a  sion node here *
30898 2f 0a 20 20 20 20 50 61 72 73 65 20 2a 70 50 61  /.    Parse *pPa
30899 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  rse,         /* 
3089a 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
3089b 74 6f 20 72 65 63 6f 72 64 20 65 72 72 6f 72 73  to record errors
3089c 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6f 70 2c 20   */.    int op, 
3089d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3089e 2a 20 54 68 65 20 6f 70 65 72 61 74 6f 72 20 2a  * The operator *
3089f 2f 0a 20 20 20 20 45 78 70 72 53 70 61 6e 20 2a  /.    ExprSpan *
308a0 70 4f 70 65 72 61 6e 64 2c 20 20 20 20 2f 2a 20  pOperand,    /* 
308a1 54 68 65 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  The operand */. 
308a2 20 20 20 54 6f 6b 65 6e 20 2a 70 50 72 65 4f 70     Token *pPreOp
308a3 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
308a4 6f 70 65 72 61 6e 64 20 74 6f 6b 65 6e 20 66 6f  operand token fo
308a5 72 20 73 65 74 74 69 6e 67 20 74 68 65 20 73 70  r setting the sp
308a6 61 6e 20 2a 2f 0a 20 20 29 7b 0a 20 20 20 20 70  an */.  ){.    p
308a7 4f 75 74 2d 3e 70 45 78 70 72 20 3d 20 73 71 6c  Out->pExpr = sql
308a8 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
308a9 2c 20 6f 70 2c 20 70 4f 70 65 72 61 6e 64 2d 3e  , op, pOperand->
308aa 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20  pExpr, 0, 0);.  
308ab 20 20 70 4f 75 74 2d 3e 7a 53 74 61 72 74 20 3d    pOut->zStart =
308ac 20 70 50 72 65 4f 70 2d 3e 7a 3b 0a 20 20 20 20   pPreOp->z;.    
308ad 70 4f 75 74 2d 3e 7a 45 6e 64 20 3d 20 70 4f 70  pOut->zEnd = pOp
308ae 65 72 61 6e 64 2d 3e 7a 45 6e 64 3b 0a 20 20 7d  erand->zEnd;.  }
308af 0a 2f 2a 20 4e 65 78 74 20 69 73 20 61 6c 6c 20  ./* Next is all 
308b0 74 6f 6b 65 6e 20 76 61 6c 75 65 73 2c 20 69 6e  token values, in
308b1 20 61 20 66 6f 72 6d 20 73 75 69 74 61 62 6c 65   a form suitable
308b2 20 66 6f 72 20 75 73 65 20 62 79 20 6d 61 6b 65   for use by make
308b3 68 65 61 64 65 72 73 2e 0a 2a 2a 20 54 68 69 73  headers..** This
308b4 20 73 65 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65   section will be
308b5 20 6e 75 6c 6c 20 75 6e 6c 65 73 73 20 6c 65 6d   null unless lem
308b6 6f 6e 20 69 73 20 72 75 6e 20 77 69 74 68 20 74  on is run with t
308b7 68 65 20 2d 6d 20 73 77 69 74 63 68 2e 0a 2a 2f  he -m switch..*/
308b8 0a 2f 2a 20 0a 2a 2a 20 54 68 65 73 65 20 63 6f  ./* .** These co
308b9 6e 73 74 61 6e 74 73 20 28 61 6c 6c 20 67 65 6e  nstants (all gen
308ba 65 72 61 74 65 64 20 61 75 74 6f 6d 61 74 69 63  erated automatic
308bb 61 6c 6c 79 20 62 79 20 74 68 65 20 70 61 72 73  ally by the pars
308bc 65 72 20 67 65 6e 65 72 61 74 6f 72 29 0a 2a 2a  er generator).**
308bd 20 73 70 65 63 69 66 79 20 74 68 65 20 76 61 72   specify the var
308be 69 6f 75 73 20 6b 69 6e 64 73 20 6f 66 20 74 6f  ious kinds of to
308bf 6b 65 6e 73 20 28 74 65 72 6d 69 6e 61 6c 73 29  kens (terminals)
308c0 20 74 68 61 74 20 74 68 65 20 70 61 72 73 65 72   that the parser
308c1 0a 2a 2a 20 75 6e 64 65 72 73 74 61 6e 64 73 2e  .** understands.
308c2 20 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 73 79 6d   .**.** Each sym
308c3 62 6f 6c 20 68 65 72 65 20 69 73 20 61 20 74 65  bol here is a te
308c4 72 6d 69 6e 61 6c 20 73 79 6d 62 6f 6c 20 69 6e  rminal symbol in
308c5 20 74 68 65 20 67 72 61 6d 6d 61 72 2e 0a 2a 2f   the grammar..*/
308c6 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68  ./* Make sure th
308c7 65 20 49 4e 54 45 52 46 41 43 45 20 6d 61 63 72  e INTERFACE macr
308c8 6f 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f  o is defined..*/
308c9 0a 23 69 66 6e 64 65 66 20 49 4e 54 45 52 46 41  .#ifndef INTERFA
308ca 43 45 0a 23 20 64 65 66 69 6e 65 20 49 4e 54 45  CE.# define INTE
308cb 52 46 41 43 45 20 31 0a 23 65 6e 64 69 66 0a 2f  RFACE 1.#endif./
308cc 2a 20 54 68 65 20 6e 65 78 74 20 74 68 69 6e 67  * The next thing
308cd 20 69 6e 63 6c 75 64 65 64 20 69 73 20 73 65 72   included is ser
308ce 69 65 73 20 6f 66 20 64 65 66 69 6e 65 73 20 77  ies of defines w
308cf 68 69 63 68 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20  hich control.** 
308d0 76 61 72 69 6f 75 73 20 61 73 70 65 63 74 73 20  various aspects 
308d1 6f 66 20 74 68 65 20 67 65 6e 65 72 61 74 65 64  of the generated
308d2 20 70 61 72 73 65 72 2e 0a 2a 2a 20 20 20 20 59   parser..**    Y
308d3 59 43 4f 44 45 54 59 50 45 20 20 20 20 20 20 20  YCODETYPE       
308d4 20 20 69 73 20 74 68 65 20 64 61 74 61 20 74 79    is the data ty
308d5 70 65 20 75 73 65 64 20 66 6f 72 20 73 74 6f 72  pe used for stor
308d6 69 6e 67 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20  ing terminal.** 
308d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
308d8 20 20 20 20 20 20 61 6e 64 20 6e 6f 6e 74 65 72        and nonter
308d9 6d 69 6e 61 6c 20 6e 75 6d 62 65 72 73 2e 20 20  minal numbers.  
308da 22 75 6e 73 69 67 6e 65 64 20 63 68 61 72 22 20  "unsigned char" 
308db 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  is.**           
308dc 20 20 20 20 20 20 20 20 20 20 20 20 75 73 65 64              used
308dd 20 69 66 20 74 68 65 72 65 20 61 72 65 20 66 65   if there are fe
308de 77 65 72 20 74 68 61 6e 20 32 35 30 20 74 65 72  wer than 250 ter
308df 6d 69 6e 61 6c 73 0a 2a 2a 20 20 20 20 20 20 20  minals.**       
308e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
308e1 61 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 73  and nonterminals
308e2 2e 20 20 22 69 6e 74 22 20 69 73 20 75 73 65 64  .  "int" is used
308e3 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 20 20   otherwise..**  
308e4 20 20 59 59 4e 4f 43 4f 44 45 20 20 20 20 20 20    YYNOCODE      
308e5 20 20 20 20 20 69 73 20 61 20 6e 75 6d 62 65 72       is a number
308e6 20 6f 66 20 74 79 70 65 20 59 59 43 4f 44 45 54   of type YYCODET
308e7 59 50 45 20 77 68 69 63 68 20 63 6f 72 72 65 73  YPE which corres
308e8 70 6f 6e 64 73 0a 2a 2a 20 20 20 20 20 20 20 20  ponds.**        
308e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
308ea 6f 20 6e 6f 20 6c 65 67 61 6c 20 74 65 72 6d 69  o no legal termi
308eb 6e 61 6c 20 6f 72 20 6e 6f 6e 74 65 72 6d 69 6e  nal or nontermin
308ec 61 6c 20 6e 75 6d 62 65 72 2e 20 20 54 68 69 73  al number.  This
308ed 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
308ee 20 20 20 20 20 20 20 20 20 20 6e 75 6d 62 65 72            number
308ef 20 69 73 20 75 73 65 64 20 74 6f 20 66 69 6c 6c   is used to fill
308f0 20 69 6e 20 65 6d 70 74 79 20 73 6c 6f 74 73 20   in empty slots 
308f1 6f 66 20 74 68 65 20 68 61 73 68 20 0a 2a 2a 20  of the hash .** 
308f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
308f3 20 20 20 20 20 20 74 61 62 6c 65 2e 0a 2a 2a 20        table..** 
308f4 20 20 20 59 59 46 41 4c 4c 42 41 43 4b 20 20 20     YYFALLBACK   
308f5 20 20 20 20 20 20 49 66 20 64 65 66 69 6e 65 64        If defined
308f6 2c 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73  , this indicates
308f7 20 74 68 61 74 20 6f 6e 65 20 6f 72 20 6d 6f 72   that one or mor
308f8 65 20 74 6f 6b 65 6e 73 0a 2a 2a 20 20 20 20 20  e tokens.**     
308f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
308fa 20 20 68 61 76 65 20 66 61 6c 6c 2d 62 61 63 6b    have fall-back
308fb 20 76 61 6c 75 65 73 20 77 68 69 63 68 20 73 68   values which sh
308fc 6f 75 6c 64 20 62 65 20 75 73 65 64 20 69 66 20  ould be used if 
308fd 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
308fe 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 72 69               ori
308ff 67 69 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 74  ginal value of t
30900 68 65 20 74 6f 6b 65 6e 20 77 69 6c 6c 20 6e 6f  he token will no
30901 74 20 70 61 72 73 65 2e 0a 2a 2a 20 20 20 20 59  t parse..**    Y
30902 59 41 43 54 49 4f 4e 54 59 50 45 20 20 20 20 20  YACTIONTYPE     
30903 20 20 69 73 20 74 68 65 20 64 61 74 61 20 74 79    is the data ty
30904 70 65 20 75 73 65 64 20 66 6f 72 20 73 74 6f 72  pe used for stor
30905 69 6e 67 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20  ing terminal.** 
30906 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30907 20 20 20 20 20 20 61 6e 64 20 6e 6f 6e 74 65 72        and nonter
30908 6d 69 6e 61 6c 20 6e 75 6d 62 65 72 73 2e 20 20  minal numbers.  
30909 22 75 6e 73 69 67 6e 65 64 20 63 68 61 72 22 20  "unsigned char" 
3090a 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  is.**           
3090b 20 20 20 20 20 20 20 20 20 20 20 20 75 73 65 64              used
3090c 20 69 66 20 74 68 65 72 65 20 61 72 65 20 66 65   if there are fe
3090d 77 65 72 20 74 68 61 6e 20 32 35 30 20 72 75 6c  wer than 250 rul
3090e 65 73 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20  es and.**       
3090f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30910 73 74 61 74 65 73 20 63 6f 6d 62 69 6e 65 64 2e  states combined.
30911 20 20 22 69 6e 74 22 20 69 73 20 75 73 65 64 20    "int" is used 
30912 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 20 20 20  otherwise..**   
30913 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f   sqlite3ParserTO
30914 4b 45 4e 54 59 50 45 20 20 20 20 20 69 73 20 74  KENTYPE     is t
30915 68 65 20 64 61 74 61 20 74 79 70 65 20 75 73 65  he data type use
30916 64 20 66 6f 72 20 6d 69 6e 6f 72 20 74 6f 6b 65  d for minor toke
30917 6e 73 20 67 69 76 65 6e 20 0a 2a 2a 20 20 20 20  ns given .**    
30918 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30919 20 20 20 64 69 72 65 63 74 6c 79 20 74 6f 20 74     directly to t
3091a 68 65 20 70 61 72 73 65 72 20 66 72 6f 6d 20 74  he parser from t
3091b 68 65 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2a  he tokenizer..**
3091c 20 20 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20      YYMINORTYPE 
3091d 20 20 20 20 20 20 20 69 73 20 74 68 65 20 64 61         is the da
3091e 74 61 20 74 79 70 65 20 75 73 65 64 20 66 6f 72  ta type used for
3091f 20 61 6c 6c 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e   all minor token
30920 73 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  s..**           
30921 20 20 20 20 20 20 20 20 20 20 20 20 54 68 69 73              This
30922 20 69 73 20 74 79 70 69 63 61 6c 6c 79 20 61 20   is typically a 
30923 75 6e 69 6f 6e 20 6f 66 20 6d 61 6e 79 20 74 79  union of many ty
30924 70 65 73 2c 20 6f 6e 65 20 6f 66 0a 2a 2a 20 20  pes, one of.**  
30925 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30926 20 20 20 20 20 77 68 69 63 68 20 69 73 20 73 71       which is sq
30927 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45 4e  lite3ParserTOKEN
30928 54 59 50 45 2e 20 20 54 68 65 20 65 6e 74 72 79  TYPE.  The entry
30929 20 69 6e 20 74 68 65 20 75 6e 69 6f 6e 0a 2a 2a   in the union.**
3092a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3092b 20 20 20 20 20 20 20 66 6f 72 20 62 61 73 65 20         for base 
3092c 74 6f 6b 65 6e 73 20 69 73 20 63 61 6c 6c 65 64  tokens is called
3092d 20 22 79 79 30 22 2e 0a 2a 2a 20 20 20 20 59 59   "yy0"..**    YY
3092e 53 54 41 43 4b 44 45 50 54 48 20 20 20 20 20 20  STACKDEPTH      
3092f 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20   is the maximum 
30930 64 65 70 74 68 20 6f 66 20 74 68 65 20 70 61 72  depth of the par
30931 73 65 72 27 73 20 73 74 61 63 6b 2e 20 20 49 66  ser's stack.  If
30932 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
30933 20 20 20 20 20 20 20 20 20 20 7a 65 72 6f 20 74            zero t
30934 68 65 20 73 74 61 63 6b 20 69 73 20 64 79 6e 61  he stack is dyna
30935 6d 69 63 61 6c 6c 79 20 73 69 7a 65 64 20 75 73  mically sized us
30936 69 6e 67 20 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a  ing realloc().**
30937 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65      sqlite3Parse
30938 72 41 52 47 5f 53 44 45 43 4c 20 20 20 20 20 41  rARG_SDECL     A
30939 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65   static variable
3093a 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 66 6f 72   declaration for
3093b 20 74 68 65 20 25 65 78 74 72 61 5f 61 72 67 75   the %extra_argu
3093c 6d 65 6e 74 0a 2a 2a 20 20 20 20 73 71 6c 69 74  ment.**    sqlit
3093d 65 33 50 61 72 73 65 72 41 52 47 5f 50 44 45 43  e3ParserARG_PDEC
3093e 4c 20 20 20 20 20 41 20 70 61 72 61 6d 65 74 65  L     A paramete
3093f 72 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 66 6f  r declaration fo
30940 72 20 74 68 65 20 25 65 78 74 72 61 5f 61 72 67  r the %extra_arg
30941 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 73 71 6c 69  ument.**    sqli
30942 74 65 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f  te3ParserARG_STO
30943 52 45 20 20 20 20 20 43 6f 64 65 20 74 6f 20 73  RE     Code to s
30944 74 6f 72 65 20 25 65 78 74 72 61 5f 61 72 67 75  tore %extra_argu
30945 6d 65 6e 74 20 69 6e 74 6f 20 79 79 70 50 61 72  ment into yypPar
30946 73 65 72 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65  ser.**    sqlite
30947 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48  3ParserARG_FETCH
30948 20 20 20 20 20 43 6f 64 65 20 74 6f 20 65 78 74       Code to ext
30949 72 61 63 74 20 25 65 78 74 72 61 5f 61 72 67 75  ract %extra_argu
3094a 6d 65 6e 74 20 66 72 6f 6d 20 79 79 70 50 61 72  ment from yypPar
3094b 73 65 72 0a 2a 2a 20 20 20 20 59 59 4e 53 54 41  ser.**    YYNSTA
3094c 54 45 20 20 20 20 20 20 20 20 20 20 20 74 68 65  TE           the
3094d 20 63 6f 6d 62 69 6e 65 64 20 6e 75 6d 62 65 72   combined number
3094e 20 6f 66 20 73 74 61 74 65 73 2e 0a 2a 2a 20 20   of states..**  
3094f 20 20 59 59 4e 52 55 4c 45 20 20 20 20 20 20 20    YYNRULE       
30950 20 20 20 20 20 74 68 65 20 6e 75 6d 62 65 72 20       the number 
30951 6f 66 20 72 75 6c 65 73 20 69 6e 20 74 68 65 20  of rules in the 
30952 67 72 61 6d 6d 61 72 0a 2a 2a 20 20 20 20 59 59  grammar.**    YY
30953 45 52 52 4f 52 53 59 4d 42 4f 4c 20 20 20 20 20  ERRORSYMBOL     
30954 20 69 73 20 74 68 65 20 63 6f 64 65 20 6e 75 6d   is the code num
30955 62 65 72 20 6f 66 20 74 68 65 20 65 72 72 6f 72  ber of the error
30956 20 73 79 6d 62 6f 6c 2e 20 20 49 66 20 6e 6f 74   symbol.  If not
30957 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
30958 20 20 20 20 20 20 20 20 20 20 64 65 66 69 6e 65            define
30959 64 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 20 65 72  d, then do no er
3095a 72 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a  ror processing..
3095b 2a 2f 0a 23 64 65 66 69 6e 65 20 59 59 43 4f 44  */.#define YYCOD
3095c 45 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 63  ETYPE unsigned c
3095d 68 61 72 0a 23 64 65 66 69 6e 65 20 59 59 4e 4f  har.#define YYNO
3095e 43 4f 44 45 20 32 35 34 0a 23 64 65 66 69 6e 65  CODE 254.#define
3095f 20 59 59 41 43 54 49 4f 4e 54 59 50 45 20 75 6e   YYACTIONTYPE un
30960 73 69 67 6e 65 64 20 73 68 6f 72 74 20 69 6e 74  signed short int
30961 0a 23 64 65 66 69 6e 65 20 59 59 57 49 4c 44 43  .#define YYWILDC
30962 41 52 44 20 36 37 0a 23 64 65 66 69 6e 65 20 73  ARD 67.#define s
30963 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45  qlite3ParserTOKE
30964 4e 54 59 50 45 20 54 6f 6b 65 6e 0a 74 79 70 65  NTYPE Token.type
30965 64 65 66 20 75 6e 69 6f 6e 20 7b 0a 20 20 69 6e  def union {.  in
30966 74 20 79 79 69 6e 69 74 3b 0a 20 20 73 71 6c 69  t yyinit;.  sqli
30967 74 65 33 50 61 72 73 65 72 54 4f 4b 45 4e 54 59  te3ParserTOKENTY
30968 50 45 20 79 79 30 3b 0a 20 20 53 65 6c 65 63 74  PE yy0;.  Select
30969 2a 20 79 79 33 3b 0a 20 20 45 78 70 72 4c 69 73  * yy3;.  ExprLis
3096a 74 2a 20 79 79 31 34 3b 0a 20 20 53 72 63 4c 69  t* yy14;.  SrcLi
3096b 73 74 2a 20 79 79 36 35 3b 0a 20 20 73 74 72 75  st* yy65;.  stru
3096c 63 74 20 4c 69 6b 65 4f 70 20 79 79 39 36 3b 0a  ct LikeOp yy96;.
3096d 20 20 45 78 70 72 2a 20 79 79 31 33 32 3b 0a 20    Expr* yy132;. 
3096e 20 75 38 20 79 79 31 38 36 3b 0a 20 20 69 6e 74   u8 yy186;.  int
3096f 20 79 79 33 32 38 3b 0a 20 20 45 78 70 72 53 70   yy328;.  ExprSp
30970 61 6e 20 79 79 33 34 36 3b 0a 20 20 73 74 72 75  an yy346;.  stru
30971 63 74 20 54 72 69 67 45 76 65 6e 74 20 79 79 33  ct TrigEvent yy3
30972 37 38 3b 0a 20 20 49 64 4c 69 73 74 2a 20 79 79  78;.  IdList* yy
30973 34 30 38 3b 0a 20 20 73 74 72 75 63 74 20 7b 69  408;.  struct {i
30974 6e 74 20 76 61 6c 75 65 3b 20 69 6e 74 20 6d 61  nt value; int ma
30975 73 6b 3b 7d 20 79 79 34 32 39 3b 0a 20 20 54 72  sk;} yy429;.  Tr
30976 69 67 67 65 72 53 74 65 70 2a 20 79 79 34 37 33  iggerStep* yy473
30977 3b 0a 20 20 73 74 72 75 63 74 20 4c 69 6d 69 74  ;.  struct Limit
30978 56 61 6c 20 79 79 34 37 36 3b 0a 7d 20 59 59 4d  Val yy476;.} YYM
30979 49 4e 4f 52 54 59 50 45 3b 0a 23 69 66 6e 64 65  INORTYPE;.#ifnde
3097a 66 20 59 59 53 54 41 43 4b 44 45 50 54 48 0a 23  f YYSTACKDEPTH.#
3097b 64 65 66 69 6e 65 20 59 59 53 54 41 43 4b 44 45  define YYSTACKDE
3097c 50 54 48 20 31 30 30 0a 23 65 6e 64 69 66 0a 23  PTH 100.#endif.#
3097d 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61  define sqlite3Pa
3097e 72 73 65 72 41 52 47 5f 53 44 45 43 4c 20 50 61  rserARG_SDECL Pa
3097f 72 73 65 20 2a 70 50 61 72 73 65 3b 0a 23 64 65  rse *pParse;.#de
30980 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73  fine sqlite3Pars
30981 65 72 41 52 47 5f 50 44 45 43 4c 20 2c 50 61 72  erARG_PDECL ,Par
30982 73 65 20 2a 70 50 61 72 73 65 0a 23 64 65 66 69  se *pParse.#defi
30983 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  ne sqlite3Parser
30984 41 52 47 5f 46 45 54 43 48 20 50 61 72 73 65 20  ARG_FETCH Parse 
30985 2a 70 50 61 72 73 65 20 3d 20 79 79 70 50 61 72  *pParse = yypPar
30986 73 65 72 2d 3e 70 50 61 72 73 65 0a 23 64 65 66  ser->pParse.#def
30987 69 6e 65 20 73 71 6c 69 74 65 33 50 61 72 73 65  ine sqlite3Parse
30988 72 41 52 47 5f 53 54 4f 52 45 20 79 79 70 50 61  rARG_STORE yypPa
30989 72 73 65 72 2d 3e 70 50 61 72 73 65 20 3d 20 70  rser->pParse = p
3098a 50 61 72 73 65 0a 23 64 65 66 69 6e 65 20 59 59  Parse.#define YY
3098b 4e 53 54 41 54 45 20 36 32 39 0a 23 64 65 66 69  NSTATE 629.#defi
3098c 6e 65 20 59 59 4e 52 55 4c 45 20 33 32 39 0a 23  ne YYNRULE 329.#
3098d 64 65 66 69 6e 65 20 59 59 46 41 4c 4c 42 41 43  define YYFALLBAC
3098e 4b 20 31 0a 23 64 65 66 69 6e 65 20 59 59 5f 4e  K 1.#define YY_N
3098f 4f 5f 41 43 54 49 4f 4e 20 20 20 20 20 20 28 59  O_ACTION      (Y
30990 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b  YNSTATE+YYNRULE+
30991 32 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 41 43  2).#define YY_AC
30992 43 45 50 54 5f 41 43 54 49 4f 4e 20 20 28 59 59  CEPT_ACTION  (YY
30993 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b 31  NSTATE+YYNRULE+1
30994 29 0a 23 64 65 66 69 6e 65 20 59 59 5f 45 52 52  ).#define YY_ERR
30995 4f 52 5f 41 43 54 49 4f 4e 20 20 20 28 59 59 4e  OR_ACTION   (YYN
30996 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 29 0a 0a  STATE+YYNRULE)..
30997 2f 2a 20 54 68 65 20 79 79 7a 65 72 6f 6d 69 6e  /* The yyzeromin
30998 6f 72 20 63 6f 6e 73 74 61 6e 74 20 69 73 20 75  or constant is u
30999 73 65 64 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a  sed to initializ
3099a 65 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 0a 2a  e instances of.*
3099b 2a 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 6f 62  * YYMINORTYPE ob
3099c 6a 65 63 74 73 20 74 6f 20 7a 65 72 6f 2e 20 2a  jects to zero. *
3099d 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 59  /.static const Y
3099e 59 4d 49 4e 4f 52 54 59 50 45 20 79 79 7a 65 72  YMINORTYPE yyzer
3099f 6f 6d 69 6e 6f 72 20 3d 20 7b 20 30 20 7d 3b 0a  ominor = { 0 };.
309a0 0a 2f 2a 20 44 65 66 69 6e 65 20 74 68 65 20 79  ./* Define the y
309a1 79 74 65 73 74 63 61 73 65 28 29 20 6d 61 63 72  ytestcase() macr
309a2 6f 20 74 6f 20 62 65 20 61 20 6e 6f 2d 6f 70 20  o to be a no-op 
309a3 69 66 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64  if is not alread
309a4 79 20 64 65 66 69 6e 65 64 0a 2a 2a 20 6f 74 68  y defined.** oth
309a5 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 41 70  erwise..**.** Ap
309a6 70 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20 63  plications can c
309a7 68 6f 6f 73 65 20 74 6f 20 64 65 66 69 6e 65 20  hoose to define 
309a8 79 79 74 65 73 74 63 61 73 65 28 29 20 69 6e 20  yytestcase() in 
309a9 74 68 65 20 25 69 6e 63 6c 75 64 65 20 73 65 63  the %include sec
309aa 74 69 6f 6e 0a 2a 2a 20 74 6f 20 61 20 6d 61 63  tion.** to a mac
309ab 72 6f 20 74 68 61 74 20 63 61 6e 20 61 73 73 69  ro that can assi
309ac 73 74 20 69 6e 20 76 65 72 69 66 79 69 6e 67 20  st in verifying 
309ad 63 6f 64 65 20 63 6f 76 65 72 61 67 65 2e 20 20  code coverage.  
309ae 46 6f 72 20 70 72 6f 64 75 63 74 69 6f 6e 0a 2a  For production.*
309af 2a 20 63 6f 64 65 20 74 68 65 20 79 79 74 65 73  * code the yytes
309b0 74 63 61 73 65 28 29 20 6d 61 63 72 6f 20 73 68  tcase() macro sh
309b1 6f 75 6c 64 20 62 65 20 74 75 72 6e 65 64 20 6f  ould be turned o
309b2 66 66 2e 20 20 42 75 74 20 69 74 20 69 73 20 75  ff.  But it is u
309b3 73 65 66 75 6c 0a 2a 2a 20 66 6f 72 20 74 65 73  seful.** for tes
309b4 74 69 6e 67 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66  ting..*/.#ifndef
309b5 20 79 79 74 65 73 74 63 61 73 65 0a 23 20 64 65   yytestcase.# de
309b6 66 69 6e 65 20 79 79 74 65 73 74 63 61 73 65 28  fine yytestcase(
309b7 58 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 20 4e  X).#endif.../* N
309b8 65 78 74 20 61 72 65 20 74 68 65 20 74 61 62 6c  ext are the tabl
309b9 65 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72  es used to deter
309ba 6d 69 6e 65 20 77 68 61 74 20 61 63 74 69 6f 6e  mine what action
309bb 20 74 6f 20 74 61 6b 65 20 62 61 73 65 64 20 6f   to take based o
309bc 6e 20 74 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74  n the.** current
309bd 20 73 74 61 74 65 20 61 6e 64 20 6c 6f 6f 6b 61   state and looka
309be 68 65 61 64 20 74 6f 6b 65 6e 2e 20 20 54 68 65  head token.  The
309bf 73 65 20 74 61 62 6c 65 73 20 61 72 65 20 75 73  se tables are us
309c0 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 0a  ed to implement.
309c1 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61  ** functions tha
309c2 74 20 74 61 6b 65 20 61 20 73 74 61 74 65 20 6e  t take a state n
309c3 75 6d 62 65 72 20 61 6e 64 20 6c 6f 6f 6b 61 68  umber and lookah
309c4 65 61 64 20 76 61 6c 75 65 20 61 6e 64 20 72 65  ead value and re
309c5 74 75 72 6e 20 61 6e 0a 2a 2a 20 61 63 74 69 6f  turn an.** actio
309c6 6e 20 69 6e 74 65 67 65 72 2e 20 20 0a 2a 2a 0a  n integer.  .**.
309c7 2a 2a 20 53 75 70 70 6f 73 65 20 74 68 65 20 61  ** Suppose the a
309c8 63 74 69 6f 6e 20 69 6e 74 65 67 65 72 20 69 73  ction integer is
309c9 20 4e 2e 20 20 54 68 65 6e 20 74 68 65 20 61 63   N.  Then the ac
309ca 74 69 6f 6e 20 69 73 20 64 65 74 65 72 6d 69 6e  tion is determin
309cb 65 64 20 61 73 0a 2a 2a 20 66 6f 6c 6c 6f 77 73  ed as.** follows
309cc 0a 2a 2a 0a 2a 2a 20 20 20 30 20 3c 3d 20 4e 20  .**.**   0 <= N 
309cd 3c 20 59 59 4e 53 54 41 54 45 20 20 20 20 20 20  < YYNSTATE      
309ce 20 20 20 20 20 20 20 20 20 20 20 20 53 68 69 66              Shif
309cf 74 20 4e 2e 20 20 54 68 61 74 20 69 73 2c 20 70  t N.  That is, p
309d0 75 73 68 20 74 68 65 20 6c 6f 6f 6b 61 68 65 61  ush the lookahea
309d1 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  d.**            
309d2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
309d3 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
309d4 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20 61  onto the stack a
309d5 6e 64 20 67 6f 74 6f 20 73 74 61 74 65 20 4e 2e  nd goto state N.
309d6 0a 2a 2a 0a 2a 2a 20 20 20 59 59 4e 53 54 41 54  .**.**   YYNSTAT
309d7 45 20 3c 3d 20 4e 20 3c 20 59 59 4e 53 54 41 54  E <= N < YYNSTAT
309d8 45 2b 59 59 4e 52 55 4c 45 20 20 20 52 65 64 75  E+YYNRULE   Redu
309d9 63 65 20 62 79 20 72 75 6c 65 20 4e 2d 59 59 4e  ce by rule N-YYN
309da 53 54 41 54 45 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e  STATE..**.**   N
309db 20 3d 3d 20 59 59 4e 53 54 41 54 45 2b 59 59 4e   == YYNSTATE+YYN
309dc 52 55 4c 45 20 20 20 20 20 20 20 20 20 20 20 20  RULE            
309dd 20 20 41 20 73 79 6e 74 61 78 20 65 72 72 6f 72    A syntax error
309de 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 0a 2a   has occurred..*
309df 2a 0a 2a 2a 20 20 20 4e 20 3d 3d 20 59 59 4e 53  *.**   N == YYNS
309e0 54 41 54 45 2b 59 59 4e 52 55 4c 45 2b 31 20 20  TATE+YYNRULE+1  
309e1 20 20 20 20 20 20 20 20 20 20 54 68 65 20 70 61            The pa
309e2 72 73 65 72 20 61 63 63 65 70 74 73 20 69 74 73  rser accepts its
309e3 20 69 6e 70 75 74 2e 0a 2a 2a 0a 2a 2a 20 20 20   input..**.**   
309e4 4e 20 3d 3d 20 59 59 4e 53 54 41 54 45 2b 59 59  N == YYNSTATE+YY
309e5 4e 52 55 4c 45 2b 32 20 20 20 20 20 20 20 20 20  NRULE+2         
309e6 20 20 20 4e 6f 20 73 75 63 68 20 61 63 74 69 6f     No such actio
309e7 6e 2e 20 20 44 65 6e 6f 74 65 73 20 75 6e 75 73  n.  Denotes unus
309e8 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ed.**           
309e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
309ea 20 20 20 20 20 20 20 20 20 20 20 73 6c 6f 74 73             slots
309eb 20 69 6e 20 74 68 65 20 79 79 5f 61 63 74 69 6f   in the yy_actio
309ec 6e 5b 5d 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  n[] table..**.**
309ed 20 54 68 65 20 61 63 74 69 6f 6e 20 74 61 62 6c   The action tabl
309ee 65 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65 64  e is constructed
309ef 20 61 73 20 61 20 73 69 6e 67 6c 65 20 6c 61 72   as a single lar
309f0 67 65 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 79  ge table named y
309f1 79 5f 61 63 74 69 6f 6e 5b 5d 2e 0a 2a 2a 20 47  y_action[]..** G
309f2 69 76 65 6e 20 73 74 61 74 65 20 53 20 61 6e 64  iven state S and
309f3 20 6c 6f 6f 6b 61 68 65 61 64 20 58 2c 20 74 68   lookahead X, th
309f4 65 20 61 63 74 69 6f 6e 20 69 73 20 63 6f 6d 70  e action is comp
309f5 75 74 65 64 20 61 73 0a 2a 2a 0a 2a 2a 20 20 20  uted as.**.**   
309f6 20 20 20 79 79 5f 61 63 74 69 6f 6e 5b 20 79 79     yy_action[ yy
309f7 5f 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d 20 2b  _shift_ofst[S] +
309f8 20 58 20 5d 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68   X ].**.** If th
309f9 65 20 69 6e 64 65 78 20 76 61 6c 75 65 20 79 79  e index value yy
309fa 5f 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d 2b 58  _shift_ofst[S]+X
309fb 20 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65   is out of range
309fc 20 6f 72 20 69 66 20 74 68 65 20 76 61 6c 75 65   or if the value
309fd 0a 2a 2a 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64  .** yy_lookahead
309fe 5b 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 53  [yy_shift_ofst[S
309ff 5d 2b 58 5d 20 69 73 20 6e 6f 74 20 65 71 75 61  ]+X] is not equa
30a00 6c 20 74 6f 20 58 20 6f 72 20 69 66 20 79 79 5f  l to X or if yy_
30a01 73 68 69 66 74 5f 6f 66 73 74 5b 53 5d 0a 2a 2a  shift_ofst[S].**
30a02 20 69 73 20 65 71 75 61 6c 20 74 6f 20 59 59 5f   is equal to YY_
30a03 53 48 49 46 54 5f 55 53 45 5f 44 46 4c 54 2c 20  SHIFT_USE_DFLT, 
30a04 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  it means that th
30a05 65 20 61 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20  e action is not 
30a06 69 6e 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20  in the table.** 
30a07 61 6e 64 20 74 68 61 74 20 79 79 5f 64 65 66 61  and that yy_defa
30a08 75 6c 74 5b 53 5d 20 73 68 6f 75 6c 64 20 62 65  ult[S] should be
30a09 20 75 73 65 64 20 69 6e 73 74 65 61 64 2e 20 20   used instead.  
30a0a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 72 6d 75  .**.** The formu
30a0b 6c 61 20 61 62 6f 76 65 20 69 73 20 66 6f 72 20  la above is for 
30a0c 63 6f 6d 70 75 74 69 6e 67 20 74 68 65 20 61 63  computing the ac
30a0d 74 69 6f 6e 20 77 68 65 6e 20 74 68 65 20 6c 6f  tion when the lo
30a0e 6f 6b 61 68 65 61 64 20 69 73 0a 2a 2a 20 61 20  okahead is.** a 
30a0f 74 65 72 6d 69 6e 61 6c 20 73 79 6d 62 6f 6c 2e  terminal symbol.
30a10 20 20 49 66 20 74 68 65 20 6c 6f 6f 6b 61 68 65    If the lookahe
30a11 61 64 20 69 73 20 61 20 6e 6f 6e 2d 74 65 72 6d  ad is a non-term
30a12 69 6e 61 6c 20 28 61 73 20 6f 63 63 75 72 73 20  inal (as occurs 
30a13 61 66 74 65 72 0a 2a 2a 20 61 20 72 65 64 75 63  after.** a reduc
30a14 65 20 61 63 74 69 6f 6e 29 20 74 68 65 6e 20 74  e action) then t
30a15 68 65 20 79 79 5f 72 65 64 75 63 65 5f 6f 66 73  he yy_reduce_ofs
30a16 74 5b 5d 20 61 72 72 61 79 20 69 73 20 75 73 65  t[] array is use
30a17 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a  d in place of.**
30a18 20 74 68 65 20 79 79 5f 73 68 69 66 74 5f 6f 66   the yy_shift_of
30a19 73 74 5b 5d 20 61 72 72 61 79 20 61 6e 64 20 59  st[] array and Y
30a1a 59 5f 52 45 44 55 43 45 5f 55 53 45 5f 44 46 4c  Y_REDUCE_USE_DFL
30a1b 54 20 69 73 20 75 73 65 64 20 69 6e 20 70 6c 61  T is used in pla
30a1c 63 65 20 6f 66 0a 2a 2a 20 59 59 5f 53 48 49 46  ce of.** YY_SHIF
30a1d 54 5f 55 53 45 5f 44 46 4c 54 2e 0a 2a 2a 0a 2a  T_USE_DFLT..**.*
30a1e 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
30a1f 61 72 65 20 74 68 65 20 74 61 62 6c 65 73 20 67  are the tables g
30a20 65 6e 65 72 61 74 65 64 20 69 6e 20 74 68 69 73  enerated in this
30a21 20 73 65 63 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20   section:.**.** 
30a22 20 79 79 5f 61 63 74 69 6f 6e 5b 5d 20 20 20 20   yy_action[]    
30a23 20 20 20 20 41 20 73 69 6e 67 6c 65 20 74 61 62      A single tab
30a24 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6c  le containing al
30a25 6c 20 61 63 74 69 6f 6e 73 2e 0a 2a 2a 20 20 79  l actions..**  y
30a26 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 5d 20 20 20  y_lookahead[]   
30a27 20 20 41 20 74 61 62 6c 65 20 63 6f 6e 74 61 69    A table contai
30a28 6e 69 6e 67 20 74 68 65 20 6c 6f 6f 6b 61 68 65  ning the lookahe
30a29 61 64 20 66 6f 72 20 65 61 63 68 20 65 6e 74 72  ad for each entr
30a2a 79 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  y in.**         
30a2b 20 20 20 20 20 20 20 20 20 20 20 20 79 79 5f 61              yy_a
30a2c 63 74 69 6f 6e 2e 20 20 55 73 65 64 20 74 6f 20  ction.  Used to 
30a2d 64 65 74 65 63 74 20 68 61 73 68 20 63 6f 6c 6c  detect hash coll
30a2e 69 73 69 6f 6e 73 2e 0a 2a 2a 20 20 79 79 5f 73  isions..**  yy_s
30a2f 68 69 66 74 5f 6f 66 73 74 5b 5d 20 20 20 20 46  hift_ofst[]    F
30a30 6f 72 20 65 61 63 68 20 73 74 61 74 65 2c 20 74  or each state, t
30a31 68 65 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 79  he offset into y
30a32 79 5f 61 63 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20  y_action for.** 
30a33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30a34 20 20 20 20 73 68 69 66 74 69 6e 67 20 74 65 72      shifting ter
30a35 6d 69 6e 61 6c 73 2e 0a 2a 2a 20 20 79 79 5f 72  minals..**  yy_r
30a36 65 64 75 63 65 5f 6f 66 73 74 5b 5d 20 20 20 46  educe_ofst[]   F
30a37 6f 72 20 65 61 63 68 20 73 74 61 74 65 2c 20 74  or each state, t
30a38 68 65 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 79  he offset into y
30a39 79 5f 61 63 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20  y_action for.** 
30a3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30a3b 20 20 20 20 73 68 69 66 74 69 6e 67 20 6e 6f 6e      shifting non
30a3c 2d 74 65 72 6d 69 6e 61 6c 73 20 61 66 74 65 72  -terminals after
30a3d 20 61 20 72 65 64 75 63 65 2e 0a 2a 2a 20 20 79   a reduce..**  y
30a3e 79 5f 64 65 66 61 75 6c 74 5b 5d 20 20 20 20 20  y_default[]     
30a3f 20 20 44 65 66 61 75 6c 74 20 61 63 74 69 6f 6e    Default action
30a40 20 66 6f 72 20 65 61 63 68 20 73 74 61 74 65 2e   for each state.
30a41 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
30a42 20 59 59 41 43 54 49 4f 4e 54 59 50 45 20 79 79   YYACTIONTYPE yy
30a43 5f 61 63 74 69 6f 6e 5b 5d 20 3d 20 7b 0a 20 2f  _action[] = {. /
30a44 2a 20 20 20 20 20 30 20 2a 2f 20 20 20 33 31 32  *     0 */   312
30a45 2c 20 20 20 35 33 2c 20 20 34 39 34 2c 20 20 20  ,   53,  494,   
30a46 35 36 2c 20 20 31 34 33 2c 20 20 31 37 37 2c 20  56,  143,  177, 
30a47 20 31 38 31 2c 20 20 35 36 31 2c 20 20 20 34 34   181,  561,   44
30a48 2c 20 20 20 34 34 2c 0a 20 2f 2a 20 20 20 20 31  ,   44,. /*    1
30a49 30 20 2a 2f 20 20 20 20 34 34 2c 20 20 20 34 34  0 */    44,   44
30a4a 2c 20 20 32 32 34 2c 20 20 20 34 36 2c 20 20 20  ,  224,   46,   
30a4b 34 36 2c 20 20 20 34 36 2c 20 20 20 34 36 2c 20  46,   46,   46, 
30a4c 20 20 34 37 2c 20 20 20 34 37 2c 20 20 20 34 38    47,   47,   48
30a4d 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20  ,. /*    20 */  
30a4e 20 20 34 38 2c 20 20 20 34 38 2c 20 20 20 34 39    48,   48,   49
30a4f 2c 20 20 32 32 36 2c 20 20 35 31 31 2c 20 20 33  ,  226,  511,  3
30a50 34 35 2c 20 20 32 30 39 2c 20 20 35 31 33 2c 20  45,  209,  513, 
30a51 20 35 30 31 2c 20 20 32 32 36 2c 0a 20 2f 2a 20   501,  226,. /* 
30a52 20 20 20 33 30 20 2a 2f 20 20 20 35 35 30 2c 20     30 */   550, 
30a53 20 35 34 31 2c 20 20 20 35 34 2c 20 20 20 34 36   541,   54,   46
30a54 2c 20 20 20 34 36 2c 20 20 20 34 36 2c 20 20 20  ,   46,   46,   
30a55 34 36 2c 20 20 20 34 37 2c 20 20 20 34 37 2c 20  46,   47,   47, 
30a56 20 20 34 38 2c 0a 20 2f 2a 20 20 20 20 34 30 20    48,. /*    40 
30a57 2a 2f 20 20 20 20 34 38 2c 20 20 20 34 38 2c 20  */    48,   48, 
30a58 20 20 34 39 2c 20 20 32 32 36 2c 20 20 20 34 38    49,  226,   48
30a59 2c 20 20 20 34 38 2c 20 20 20 34 38 2c 20 20 20  ,   48,   48,   
30a5a 34 39 2c 20 20 32 32 36 2c 20 20 20 34 33 2c 0a  49,  226,   43,.
30a5b 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20 20 20   /*    50 */    
30a5c 34 31 2c 20 20 20 35 35 2c 20 20 35 33 39 2c 20  41,   55,  539, 
30a5d 20 35 33 37 2c 20 20 35 34 30 2c 20 20 35 34 30   537,  540,  540
30a5e 2c 20 20 20 34 35 2c 20 20 20 34 35 2c 20 20 20  ,   45,   45,   
30a5f 34 34 2c 20 20 20 34 34 2c 0a 20 2f 2a 20 20 20  44,   44,. /*   
30a60 20 36 30 20 2a 2f 20 20 20 20 34 34 2c 20 20 20   60 */    44,   
30a61 34 34 2c 20 20 32 35 32 2c 20 20 20 34 36 2c 20  44,  252,   46, 
30a62 20 20 34 36 2c 20 20 20 34 36 2c 20 20 20 34 36    46,   46,   46
30a63 2c 20 20 20 34 37 2c 20 20 20 34 37 2c 20 20 20  ,   47,   47,   
30a64 34 38 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f  48,. /*    70 */
30a65 20 20 20 20 34 38 2c 20 20 20 34 38 2c 20 20 20      48,   48,   
30a66 34 39 2c 20 20 32 32 36 2c 20 20 33 31 32 2c 20  49,  226,  312, 
30a67 20 35 36 31 2c 20 20 33 32 31 2c 20 20 34 39 33   561,  321,  493
30a68 2c 20 20 34 32 39 2c 20 20 32 34 36 2c 0a 20 2f  ,  429,  246,. /
30a69 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 20 36 30  *    80 */    60
30a6a 2c 20 20 20 34 36 2c 20 20 20 34 36 2c 20 20 20  ,   46,   46,   
30a6b 34 36 2c 20 20 20 34 36 2c 20 20 20 34 37 2c 20  46,   46,   47, 
30a6c 20 20 34 37 2c 20 20 20 34 38 2c 20 20 20 34 38    47,   48,   48
30a6d 2c 20 20 20 34 38 2c 0a 20 2f 2a 20 20 20 20 39  ,   48,. /*    9
30a6e 30 20 2a 2f 20 20 20 20 34 39 2c 20 20 32 32 36  0 */    49,  226
30a6f 2c 20 20 36 31 33 2c 20 20 33 32 31 2c 20 20 34  ,  613,  321,  4
30a70 39 33 2c 20 20 34 38 30 2c 20 20 34 37 37 2c 20  93,  480,  477, 
30a71 20 31 35 35 2c 20 20 34 32 36 2c 20 20 33 32 36   155,  426,  326
30a72 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20  ,. /*   100 */  
30a73 20 33 36 39 2c 20 20 33 37 32 2c 20 20 33 37 33   369,  372,  373
30a74 2c 20 20 35 38 31 2c 20 20 35 35 30 2c 20 20 35  ,  581,  550,  5
30a75 34 31 2c 20 20 33 31 31 2c 20 20 35 35 35 2c 20  41,  311,  555, 
30a76 20 31 35 35 2c 20 20 20 34 30 2c 0a 20 2f 2a 20   155,   40,. /* 
30a77 20 20 31 31 30 20 2a 2f 20 20 20 33 37 34 2c 20    110 */   374, 
30a78 20 33 36 39 2c 20 20 33 37 32 2c 20 20 33 37 33   369,  372,  373
30a79 2c 20 20 20 34 37 2c 20 20 20 34 37 2c 20 20 20  ,   47,   47,   
30a7a 34 38 2c 20 20 20 34 38 2c 20 20 20 34 38 2c 20  48,   48,   48, 
30a7b 20 20 34 39 2c 0a 20 2f 2a 20 20 20 31 32 30 20    49,. /*   120 
30a7c 2a 2f 20 20 20 32 32 36 2c 20 20 33 37 34 2c 20  */   226,  374, 
30a7d 20 35 33 36 2c 20 20 20 34 33 2c 20 20 20 34 31   536,   43,   41
30a7e 2c 20 20 20 35 35 2c 20 20 35 33 39 2c 20 20 35  ,   55,  539,  5
30a7f 33 37 2c 20 20 35 34 30 2c 20 20 35 34 30 2c 0a  37,  540,  540,.
30a80 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 20   /*   130 */    
30a81 34 35 2c 20 20 20 34 35 2c 20 20 20 34 34 2c 20  45,   45,   44, 
30a82 20 20 34 34 2c 20 20 20 34 34 2c 20 20 20 34 34    44,   44,   44
30a83 2c 20 20 33 30 31 2c 20 20 20 34 36 2c 20 20 20  ,  301,   46,   
30a84 34 36 2c 20 20 20 34 36 2c 0a 20 2f 2a 20 20 20  46,   46,. /*   
30a85 31 34 30 20 2a 2f 20 20 20 20 34 36 2c 20 20 20  140 */    46,   
30a86 34 37 2c 20 20 20 34 37 2c 20 20 20 34 38 2c 20  47,   47,   48, 
30a87 20 20 34 38 2c 20 20 20 34 38 2c 20 20 20 34 39    48,   48,   49
30a88 2c 20 20 32 32 36 2c 20 20 33 31 32 2c 20 20 32  ,  226,  312,  2
30a89 32 32 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f  22,. /*   150 */
30a8a 20 20 20 32 31 30 2c 20 20 34 34 35 2c 20 20 34     210,  445,  4
30a8b 33 31 2c 20 20 31 35 36 2c 20 20 31 33 39 2c 20  31,  156,  139, 
30a8c 20 32 35 30 2c 20 20 33 36 36 2c 20 20 32 36 37   250,  366,  267
30a8d 2c 20 20 33 36 37 2c 20 20 31 35 34 2c 0a 20 2f  ,  367,  154,. /
30a8e 2a 20 20 20 31 36 30 20 2a 2f 20 20 20 35 30 39  *   160 */   509
30a8f 2c 20 20 33 35 30 2c 20 20 35 31 33 2c 20 20 35  ,  350,  513,  5
30a90 30 31 2c 20 20 35 36 31 2c 20 20 32 34 38 2c 20  01,  561,  248, 
30a91 20 32 32 32 2c 20 20 34 38 34 2c 20 20 35 31 33   222,  484,  513
30a92 2c 20 20 35 30 31 2c 0a 20 2f 2a 20 20 20 31 37  ,  501,. /*   17
30a93 30 20 2a 2f 20 20 20 33 34 30 2c 20 20 31 33 39  0 */   340,  139
30a94 2c 20 20 32 35 30 2c 20 20 33 36 36 2c 20 20 32  ,  250,  366,  2
30a95 36 37 2c 20 20 33 36 37 2c 20 20 31 35 34 2c 20  67,  367,  154, 
30a96 20 33 31 36 2c 20 20 35 35 30 2c 20 20 35 34 31   316,  550,  541
30a97 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20  ,. /*   180 */  
30a98 20 36 30 39 2c 20 20 32 38 31 2c 20 20 32 34 38   609,  281,  248
30a99 2c 20 20 31 39 37 2c 20 20 35 36 35 2c 20 20 33  ,  197,  565,  3
30a9a 33 37 2c 20 20 33 30 39 2c 20 20 34 37 31 2c 20  37,  309,  471, 
30a9b 20 34 32 38 2c 20 20 35 31 31 2c 0a 20 2f 2a 20   428,  511,. /* 
30a9c 20 20 31 39 30 20 2a 2f 20 20 20 35 38 32 2c 20    190 */   582, 
30a9d 20 32 30 39 2c 20 20 35 35 36 2c 20 20 35 39 39   209,  556,  599
30a9e 2c 20 20 35 36 36 2c 20 20 20 38 31 2c 20 20 34  ,  566,   81,  4
30a9f 39 33 2c 20 20 20 34 33 2c 20 20 20 34 31 2c 20  93,   43,   41, 
30aa0 20 20 35 35 2c 0a 20 2f 2a 20 20 20 32 30 30 20    55,. /*   200 
30aa1 2a 2f 20 20 20 35 33 39 2c 20 20 35 33 37 2c 20  */   539,  537, 
30aa2 20 35 34 30 2c 20 20 35 34 30 2c 20 20 20 34 35   540,  540,   45
30aa3 2c 20 20 20 34 35 2c 20 20 20 34 34 2c 20 20 20  ,   45,   44,   
30aa4 34 34 2c 20 20 20 34 34 2c 20 20 20 34 34 2c 0a  44,   44,   44,.
30aa5 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20 20 34   /*   210 */   4
30aa6 37 31 2c 20 20 20 34 36 2c 20 20 20 34 36 2c 20  71,   46,   46, 
30aa7 20 20 34 36 2c 20 20 20 34 36 2c 20 20 20 34 37    46,   46,   47
30aa8 2c 20 20 20 34 37 2c 20 20 20 34 38 2c 20 20 20  ,   47,   48,   
30aa9 34 38 2c 20 20 20 34 38 2c 0a 20 2f 2a 20 20 20  48,   48,. /*   
30aaa 32 32 30 20 2a 2f 20 20 20 20 34 39 2c 20 20 32  220 */    49,  2
30aab 32 36 2c 20 20 33 31 32 2c 20 20 35 35 33 2c 20  26,  312,  553, 
30aac 20 35 35 33 2c 20 20 35 35 33 2c 20 20 32 38 37   553,  553,  287
30aad 2c 20 20 31 33 33 2c 20 20 35 31 37 2c 20 20 32  ,  133,  517,  2
30aae 33 31 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f  31,. /*   230 */
30aaf 20 20 20 34 38 30 2c 20 20 34 37 37 2c 20 20 35     480,  477,  5
30ab0 36 31 2c 20 20 35 38 30 2c 20 20 33 35 35 2c 20  61,  580,  355, 
30ab1 20 32 33 35 2c 20 20 34 38 30 2c 20 20 34 37 37   235,  480,  477
30ab2 2c 20 20 34 38 33 2c 20 20 35 37 33 2c 0a 20 2f  ,  483,  573,. /
30ab3 2a 20 20 20 32 34 30 20 2a 2f 20 20 20 33 34 30  *   240 */   340
30ab4 2c 20 20 35 31 33 2c 20 20 35 30 31 2c 20 20 31  ,  513,  501,  1
30ab5 38 33 2c 20 20 35 39 31 2c 20 20 20 36 36 2c 20  83,  591,   66, 
30ab6 20 35 37 31 2c 20 20 35 37 32 2c 20 20 34 39 33   571,  572,  493
30ab7 2c 20 20 32 33 38 2c 0a 20 2f 2a 20 20 20 32 35  ,  238,. /*   25
30ab8 30 20 2a 2f 20 20 20 32 30 35 2c 20 20 33 38 37  0 */   205,  387
30ab9 2c 20 20 35 35 30 2c 20 20 35 34 31 2c 20 20 31  ,  550,  541,  1
30aba 35 35 2c 20 20 33 33 37 2c 20 20 33 39 30 2c 20  55,  337,  390, 
30abb 20 33 36 39 2c 20 20 33 37 32 2c 20 20 33 37 33   369,  372,  373
30abc 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20  ,. /*   260 */  
30abd 20 39 35 39 2c 20 20 31 38 36 2c 20 20 34 39 30   959,  186,  490
30abe 2c 20 20 20 20 32 2c 20 20 35 36 36 2c 20 20 20  ,    2,  566,   
30abf 39 34 2c 20 20 34 36 36 2c 20 20 33 37 34 2c 20  94,  466,  374, 
30ac0 20 31 39 35 2c 20 20 36 31 34 2c 0a 20 2f 2a 20   195,  614,. /* 
30ac1 20 20 32 37 30 20 2a 2f 20 20 20 31 39 38 2c 20    270 */   198, 
30ac2 20 20 34 33 2c 20 20 20 34 31 2c 20 20 20 35 35    43,   41,   55
30ac3 2c 20 20 35 33 39 2c 20 20 35 33 37 2c 20 20 35  ,  539,  537,  5
30ac4 34 30 2c 20 20 35 34 30 2c 20 20 20 34 35 2c 20  40,  540,   45, 
30ac5 20 20 34 35 2c 0a 20 2f 2a 20 20 20 32 38 30 20    45,. /*   280 
30ac6 2a 2f 20 20 20 20 34 34 2c 20 20 20 34 34 2c 20  */    44,   44, 
30ac7 20 20 34 34 2c 20 20 20 34 34 2c 20 20 35 36 35    44,   44,  565
30ac8 2c 20 20 20 34 36 2c 20 20 20 34 36 2c 20 20 20  ,   46,   46,   
30ac9 34 36 2c 20 20 20 34 36 2c 20 20 20 34 37 2c 0a  46,   46,   47,.
30aca 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20 20 20   /*   290 */    
30acb 34 37 2c 20 20 20 34 38 2c 20 20 20 34 38 2c 20  47,   48,   48, 
30acc 20 20 34 38 2c 20 20 20 34 39 2c 20 20 32 32 36    48,   49,  226
30acd 2c 20 20 33 31 32 2c 20 20 33 34 30 2c 20 20 35  ,  312,  340,  5
30ace 37 30 2c 20 20 20 35 33 2c 0a 20 2f 2a 20 20 20  70,   53,. /*   
30acf 33 30 30 20 2a 2f 20 20 20 36 31 38 2c 20 20 20  300 */   618,   
30ad0 35 36 2c 20 20 31 34 33 2c 20 20 34 34 39 2c 20  56,  143,  449, 
30ad1 20 35 36 35 2c 20 20 33 34 30 2c 20 20 33 35 33   565,  340,  353
30ad2 2c 20 20 32 30 31 2c 20 20 31 34 31 2c 20 20 34  ,  201,  141,  4
30ad3 38 30 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f  80,. /*   310 */
30ad4 20 20 20 34 37 37 2c 20 20 33 35 32 2c 20 20 33     477,  352,  3
30ad5 33 37 2c 20 20 34 39 33 2c 20 20 34 30 30 2c 20  37,  493,  400, 
30ad6 20 32 37 33 2c 20 20 32 30 30 2c 20 20 31 34 36   273,  200,  146
30ad7 2c 20 20 35 36 39 2c 20 20 35 36 38 2c 0a 20 2f  ,  569,  568,. /
30ad8 2a 20 20 20 33 32 30 20 2a 2f 20 20 20 33 33 37  *   320 */   337
30ad9 2c 20 20 35 36 36 2c 20 20 20 37 31 2c 20 20 35  ,  566,   71,  5
30ada 37 30 2c 20 20 32 32 34 2c 20 20 32 39 32 2c 20  70,  224,  292, 
30adb 20 35 35 30 2c 20 20 35 34 31 2c 20 20 33 36 31   550,  541,  361
30adc 2c 20 20 35 36 36 2c 0a 20 2f 2a 20 20 20 33 33  ,  566,. /*   33
30add 30 20 2a 2f 20 20 20 20 39 34 2c 20 20 33 34 32  0 */    94,  342
30ade 2c 20 20 34 38 33 2c 20 20 35 33 34 2c 20 20 35  ,  483,  534,  5
30adf 33 34 2c 20 20 20 36 38 2c 20 20 33 36 30 2c 20  34,   68,  360, 
30ae0 20 32 38 30 2c 20 20 34 39 33 2c 20 20 35 31 33   280,  493,  513
30ae1 2c 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20  ,. /*   340 */  
30ae2 20 35 30 31 2c 20 20 35 36 35 2c 20 20 33 38 31   501,  565,  381
30ae3 2c 20 20 35 36 39 2c 20 20 34 31 30 2c 20 20 20  ,  569,  410,   
30ae4 34 33 2c 20 20 20 34 31 2c 20 20 20 35 35 2c 20  43,   41,   55, 
30ae5 20 35 33 39 2c 20 20 35 33 37 2c 0a 20 2f 2a 20   539,  537,. /* 
30ae6 20 20 33 35 30 20 2a 2f 20 20 20 35 34 30 2c 20    350 */   540, 
30ae7 20 35 34 30 2c 20 20 20 34 35 2c 20 20 20 34 35   540,   45,   45
30ae8 2c 20 20 20 34 34 2c 20 20 20 34 34 2c 20 20 20  ,   44,   44,   
30ae9 34 34 2c 20 20 20 34 34 2c 20 20 34 36 32 2c 20  44,   44,  462, 
30aea 20 20 34 36 2c 0a 20 2f 2a 20 20 20 33 36 30 20    46,. /*   360 
30aeb 2a 2f 20 20 20 20 34 36 2c 20 20 20 34 36 2c 20  */    46,   46, 
30aec 20 20 34 36 2c 20 20 20 34 37 2c 20 20 20 34 37    46,   47,   47
30aed 2c 20 20 20 34 38 2c 20 20 20 34 38 2c 20 20 20  ,   48,   48,   
30aee 34 38 2c 20 20 20 34 39 2c 20 20 32 32 36 2c 0a  48,   49,  226,.
30aef 20 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20 20 33   /*   370 */   3
30af0 31 32 2c 20 20 33 35 33 2c 20 20 34 36 38 2c 20  12,  353,  468, 
30af1 20 34 39 33 2c 20 20 32 34 36 2c 20 20 32 30 38   493,  246,  208
30af2 2c 20 20 33 36 33 2c 20 20 20 20 31 2c 20 20 33  ,  363,    1,  3
30af3 34 30 2c 20 20 35 36 37 2c 0a 20 2f 2a 20 20 20  40,  567,. /*   
30af4 33 38 30 20 2a 2f 20 20 20 32 33 30 2c 20 20 35  380 */   230,  5
30af5 31 33 2c 20 20 35 30 31 2c 20 20 31 34 39 2c 20  13,  501,  149, 
30af6 20 33 33 33 2c 20 20 35 34 36 2c 20 20 20 20 34   333,  546,    4
30af7 2c 20 20 36 31 32 2c 20 20 36 31 37 2c 20 20 34  ,  612,  617,  4
30af8 39 33 2c 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f  93,. /*   390 */
30af9 20 20 20 32 34 30 2c 20 20 33 34 30 2c 20 20 31     240,  340,  1
30afa 31 32 2c 20 20 33 33 37 2c 20 20 36 31 31 2c 20  12,  337,  611, 
30afb 20 35 31 33 2c 20 20 35 30 31 2c 20 20 32 32 34   513,  501,  224
30afc 2c 20 20 32 31 32 2c 20 20 35 38 30 2c 0a 20 2f  ,  212,  580,. /
30afd 2a 20 20 20 34 30 30 20 2a 2f 20 20 20 35 35 30  *   400 */   550
30afe 2c 20 20 35 34 31 2c 20 20 35 36 36 2c 20 20 20  ,  541,  566,   
30aff 39 35 2c 20 20 34 30 39 2c 20 20 32 33 30 2c 20  95,  409,  230, 
30b00 20 33 33 37 2c 20 20 34 38 30 2c 20 20 34 37 37   337,  480,  477
30b01 2c 20 20 33 39 39 2c 0a 20 2f 2a 20 20 20 34 31  ,  399,. /*   41
30b02 30 20 2a 2f 20 20 20 34 38 32 2c 20 20 20 36 36  0 */   482,   66
30b03 2c 20 20 34 37 31 2c 20 20 35 31 36 2c 20 20 34  ,  471,  516,  4
30b04 39 33 2c 20 20 35 36 36 2c 20 20 20 39 34 2c 20  93,  566,   94, 
30b05 20 34 31 32 2c 20 20 34 36 36 2c 20 20 20 34 33   412,  466,   43
30b06 2c 0a 20 2f 2a 20 20 20 34 32 30 20 2a 2f 20 20  ,. /*   420 */  
30b07 20 20 34 31 2c 20 20 20 35 35 2c 20 20 35 33 39    41,   55,  539
30b08 2c 20 20 35 33 37 2c 20 20 35 34 30 2c 20 20 35  ,  537,  540,  5
30b09 34 30 2c 20 20 20 34 35 2c 20 20 20 34 35 2c 20  40,   45,   45, 
30b0a 20 20 34 34 2c 20 20 20 34 34 2c 0a 20 2f 2a 20    44,   44,. /* 
30b0b 20 20 34 33 30 20 2a 2f 20 20 20 20 34 34 2c 20    430 */    44, 
30b0c 20 20 34 34 2c 20 20 35 36 35 2c 20 20 20 34 36    44,  565,   46
30b0d 2c 20 20 20 34 36 2c 20 20 20 34 36 2c 20 20 20  ,   46,   46,   
30b0e 34 36 2c 20 20 20 34 37 2c 20 20 20 34 37 2c 20  46,   47,   47, 
30b0f 20 20 34 38 2c 0a 20 2f 2a 20 20 20 34 34 30 20    48,. /*   440 
30b10 2a 2f 20 20 20 20 34 38 2c 20 20 20 34 38 2c 20  */    48,   48, 
30b11 20 20 34 39 2c 20 20 32 32 36 2c 20 20 33 31 32    49,  226,  312
30b12 2c 20 20 35 30 32 2c 20 20 35 38 35 2c 20 20 35  ,  502,  585,  5
30b13 38 30 2c 20 20 33 32 39 2c 20 20 34 38 30 2c 0a  80,  329,  480,.
30b14 20 2f 2a 20 20 20 34 35 30 20 2a 2f 20 20 20 34   /*   450 */   4
30b15 37 37 2c 20 20 34 35 31 2c 20 20 35 31 33 2c 20  77,  451,  513, 
30b16 20 35 30 31 2c 20 20 35 39 32 2c 20 20 32 34 37   501,  592,  247
30b17 2c 20 20 33 33 32 2c 20 20 33 35 37 2c 20 20 36  ,  332,  357,  6
30b18 31 35 2c 20 20 20 36 36 2c 0a 20 2f 2a 20 20 20  15,   66,. /*   
30b19 34 36 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 35  460 */   150,  5
30b1a 30 30 2c 20 20 34 39 33 2c 20 20 34 38 30 2c 20  00,  493,  480, 
30b1b 20 34 37 37 2c 20 20 31 35 31 2c 20 20 34 30 30   477,  151,  400
30b1c 2c 20 20 32 37 33 2c 20 20 20 35 33 2c 20 20 34  ,  273,   53,  4
30b1d 31 34 2c 0a 20 2f 2a 20 20 20 34 37 30 20 2a 2f  14,. /*   470 */
30b1e 20 20 20 20 35 36 2c 20 20 31 34 33 2c 20 20 32      56,  143,  2
30b1f 31 35 2c 20 20 31 34 36 2c 20 20 35 35 30 2c 20  15,  146,  550, 
30b20 20 35 34 31 2c 20 20 32 32 34 2c 20 20 34 39 39   541,  224,  499
30b21 2c 20 20 36 31 37 2c 20 20 34 32 31 2c 0a 20 2f  ,  617,  421,. /
30b22 2a 20 20 20 34 38 30 20 2a 2f 20 20 20 33 31 37  *   480 */   317
30b23 2c 20 20 35 32 38 2c 20 20 35 32 34 2c 20 20 20  ,  528,  524,   
30b24 34 32 2c 20 20 32 38 32 2c 20 20 34 31 35 2c 20  42,  282,  415, 
30b25 20 35 39 34 2c 20 20 20 33 34 2c 20 20 34 37 36   594,   34,  476
30b26 2c 20 20 32 38 30 2c 0a 20 2f 2a 20 20 20 34 39  ,  280,. /*   49
30b27 30 20 2a 2f 20 20 20 34 32 30 2c 20 20 33 39 37  0 */   420,  397
30b28 2c 20 20 32 30 33 2c 20 20 20 34 33 2c 20 20 20  ,  203,   43,   
30b29 34 31 2c 20 20 20 35 35 2c 20 20 35 33 39 2c 20  41,   55,  539, 
30b2a 20 35 33 37 2c 20 20 35 34 30 2c 20 20 35 34 30   537,  540,  540
30b2b 2c 0a 20 2f 2a 20 20 20 35 30 30 20 2a 2f 20 20  ,. /*   500 */  
30b2c 20 20 34 35 2c 20 20 20 34 35 2c 20 20 20 34 34    45,   45,   44
30b2d 2c 20 20 20 34 34 2c 20 20 20 34 34 2c 20 20 20  ,   44,   44,   
30b2e 34 34 2c 20 20 34 39 33 2c 20 20 20 34 36 2c 20  44,  493,   46, 
30b2f 20 20 34 36 2c 20 20 20 34 36 2c 0a 20 2f 2a 20    46,   46,. /* 
30b30 20 20 35 31 30 20 2a 2f 20 20 20 20 34 36 2c 20    510 */    46, 
30b31 20 20 34 37 2c 20 20 20 34 37 2c 20 20 20 34 38    47,   47,   48
30b32 2c 20 20 20 34 38 2c 20 20 20 34 38 2c 20 20 20  ,   48,   48,   
30b33 34 39 2c 20 20 32 32 36 2c 20 20 33 31 32 2c 20  49,  226,  312, 
30b34 20 33 32 34 2c 0a 20 2f 2a 20 20 20 35 32 30 20   324,. /*   520 
30b35 2a 2f 20 20 20 34 38 30 2c 20 20 34 37 37 2c 20  */   480,  477, 
30b36 20 34 33 35 2c 20 20 34 34 37 2c 20 20 34 33 39   435,  447,  439
30b37 2c 20 20 33 38 34 2c 20 20 35 36 35 2c 20 20 33  ,  384,  565,  3
30b38 34 30 2c 20 20 32 38 34 2c 20 20 33 34 30 2c 0a  40,  284,  340,.
30b39 20 2f 2a 20 20 20 35 33 30 20 2a 2f 20 20 20 32   /*   530 */   2
30b3a 34 31 2c 20 20 33 34 34 2c 20 20 35 32 38 2c 20  41,  344,  528, 
30b3b 20 35 32 34 2c 20 20 20 35 33 2c 20 20 33 34 30   524,   53,  340
30b3c 2c 20 20 20 35 36 2c 20 20 31 34 33 2c 20 20 36  ,   56,  143,  6
30b3d 32 30 2c 20 20 31 38 35 2c 0a 20 2f 2a 20 20 20  20,  185,. /*   
30b3e 35 34 30 20 2a 2f 20 20 20 34 36 31 2c 20 20 34  540 */   461,  4
30b3f 38 33 2c 20 20 33 33 37 2c 20 20 32 33 30 2c 20  83,  337,  230, 
30b40 20 33 33 37 2c 20 20 34 37 38 2c 20 20 32 31 37   337,  478,  217
30b41 2c 20 20 34 33 39 2c 20 20 35 35 30 2c 20 20 35  ,  439,  550,  5
30b42 34 31 2c 0a 20 2f 2a 20 20 20 35 35 30 20 2a 2f  41,. /*   550 */
30b43 20 20 20 33 33 37 2c 20 20 35 36 36 2c 20 20 20     337,  566,   
30b44 39 35 2c 20 20 35 36 36 2c 20 20 20 38 39 2c 20  95,  566,   89, 
30b45 20 34 39 33 2c 20 20 35 39 36 2c 20 20 34 32 35   493,  596,  425
30b46 2c 20 20 32 32 37 2c 20 20 35 36 36 2c 0a 20 2f  ,  227,  566,. /
30b47 2a 20 20 20 35 36 30 20 2a 2f 20 20 20 20 38 31  *   560 */    81
30b48 2c 20 20 20 35 32 2c 20 20 34 35 39 2c 20 20 34  ,   52,  459,  4
30b49 34 38 2c 20 20 34 34 30 2c 20 20 34 30 32 2c 20  48,  440,  402, 
30b4a 20 35 38 34 2c 20 20 20 34 33 2c 20 20 20 34 31   584,   43,   41
30b4b 2c 20 20 20 35 35 2c 0a 20 2f 2a 20 20 20 35 37  ,   55,. /*   57
30b4c 30 20 2a 2f 20 20 20 35 33 39 2c 20 20 35 33 37  0 */   539,  537
30b4d 2c 20 20 35 34 30 2c 20 20 35 34 30 2c 20 20 20  ,  540,  540,   
30b4e 34 35 2c 20 20 20 34 35 2c 20 20 20 34 34 2c 20  45,   45,   44, 
30b4f 20 20 34 34 2c 20 20 20 34 34 2c 20 20 20 34 34    44,   44,   44
30b50 2c 0a 20 2f 2a 20 20 20 35 38 30 20 2a 2f 20 20  ,. /*   580 */  
30b51 20 35 36 35 2c 20 20 20 34 36 2c 20 20 20 34 36   565,   46,   46
30b52 2c 20 20 20 34 36 2c 20 20 20 34 36 2c 20 20 20  ,   46,   46,   
30b53 34 37 2c 20 20 20 34 37 2c 20 20 20 34 38 2c 20  47,   47,   48, 
30b54 20 20 34 38 2c 20 20 20 34 38 2c 0a 20 2f 2a 20    48,   48,. /* 
30b55 20 20 35 39 30 20 2a 2f 20 20 20 20 34 39 2c 20    590 */    49, 
30b56 20 32 32 36 2c 20 20 33 31 32 2c 20 20 33 36 32   226,  312,  362
30b57 2c 20 20 32 33 30 2c 20 20 33 34 30 2c 20 20 32  ,  230,  340,  2
30b58 35 39 2c 20 20 33 32 33 2c 20 20 34 39 31 2c 20  59,  323,  491, 
30b59 20 33 37 37 2c 0a 20 2f 2a 20 20 20 36 30 30 20   377,. /*   600 
30b5a 2a 2f 20 20 20 32 33 34 2c 20 20 32 35 37 2c 20  */   234,  257, 
30b5b 20 34 35 38 2c 20 20 33 38 35 2c 20 20 33 39 38   458,  385,  398
30b5c 2c 20 20 32 37 38 2c 20 20 32 38 33 2c 20 20 34  ,  278,  283,  4
30b5d 38 38 2c 20 20 33 33 37 2c 20 20 33 34 30 2c 0a  88,  337,  340,.
30b5e 20 2f 2a 20 20 20 36 31 30 20 2a 2f 20 20 20 33   /*   610 */   3
30b5f 33 37 2c 20 20 31 34 31 2c 20 20 33 34 30 2c 20  37,  141,  340, 
30b60 20 34 36 31 2c 20 20 33 34 30 2c 20 20 35 39 37   461,  340,  597
30b61 2c 20 20 34 39 33 2c 20 20 35 36 36 2c 20 20 20  ,  493,  566,   
30b62 20 39 2c 20 20 35 36 36 2c 0a 20 2f 2a 20 20 20   9,  566,. /*   
30b63 36 32 30 20 2a 2f 20 20 20 20 39 36 2c 20 20 20  620 */    96,   
30b64 36 32 2c 20 20 35 35 30 2c 20 20 35 34 31 2c 20  62,  550,  541, 
30b65 20 33 33 37 2c 20 20 35 39 33 2c 20 20 34 30 31   337,  593,  401
30b66 2c 20 20 33 33 37 2c 20 20 34 39 33 2c 20 20 33  ,  337,  493,  3
30b67 33 37 2c 0a 20 2f 2a 20 20 20 36 33 30 20 2a 2f  37,. /*   630 */
30b68 20 20 20 31 35 31 2c 20 20 33 32 38 2c 20 20 31     151,  328,  1
30b69 35 37 2c 20 20 35 36 36 2c 20 20 20 38 35 2c 20  57,  566,   85, 
30b6a 20 34 38 33 2c 20 20 35 36 36 2c 20 20 20 37 37   483,  566,   77
30b6b 2c 20 20 35 36 36 2c 20 20 20 38 36 2c 0a 20 2f  ,  566,   86,. /
30b6c 2a 20 20 20 36 34 30 20 2a 2f 20 20 20 38 37 34  *   640 */   874
30b6d 2c 20 20 20 34 33 2c 20 20 20 34 31 2c 20 20 20  ,   43,   41,   
30b6e 35 35 2c 20 20 35 33 39 2c 20 20 35 33 37 2c 20  55,  539,  537, 
30b6f 20 35 34 30 2c 20 20 35 34 30 2c 20 20 20 34 35   540,  540,   45
30b70 2c 20 20 20 34 35 2c 0a 20 2f 2a 20 20 20 36 35  ,   45,. /*   65
30b71 30 20 2a 2f 20 20 20 20 34 34 2c 20 20 20 34 34  0 */    44,   44
30b72 2c 20 20 20 34 34 2c 20 20 20 34 34 2c 20 20 33  ,   44,   44,  3
30b73 34 30 2c 20 20 20 34 36 2c 20 20 20 34 36 2c 20  40,   46,   46, 
30b74 20 20 34 36 2c 20 20 20 34 36 2c 20 20 20 34 37    46,   46,   47
30b75 2c 0a 20 2f 2a 20 20 20 36 36 30 20 2a 2f 20 20  ,. /*   660 */  
30b76 20 20 34 37 2c 20 20 20 34 38 2c 20 20 20 34 38    47,   48,   48
30b77 2c 20 20 20 34 38 2c 20 20 20 34 39 2c 20 20 32  ,   48,   49,  2
30b78 32 36 2c 20 20 33 31 32 2c 20 20 33 34 30 2c 20  26,  312,  340, 
30b79 20 34 34 34 2c 20 20 33 33 37 2c 0a 20 2f 2a 20   444,  337,. /* 
30b7a 20 20 36 37 30 20 2a 2f 20 20 20 33 36 34 2c 20    670 */   364, 
30b7b 20 20 33 30 2c 20 20 34 34 33 2c 20 20 34 35 37    30,  443,  457
30b7c 2c 20 20 35 30 32 2c 20 20 31 35 30 2c 20 20 33  ,  502,  150,  3
30b7d 32 37 2c 20 20 20 36 35 2c 20 20 35 36 36 2c 20  27,   65,  566, 
30b7e 20 20 39 39 2c 0a 20 2f 2a 20 20 20 36 38 30 20    99,. /*   680 
30b7f 2a 2f 20 20 20 33 34 30 2c 20 20 35 36 31 2c 20  */   340,  561, 
30b80 20 33 33 37 2c 20 20 34 30 38 2c 20 20 33 32 34   337,  408,  324
30b81 2c 20 20 33 34 30 2c 20 20 33 38 36 2c 20 20 33  ,  340,  386,  3
30b82 34 30 2c 20 20 32 34 38 2c 20 20 35 38 39 2c 0a  40,  248,  589,.
30b83 20 2f 2a 20 20 20 36 39 30 20 2a 2f 20 20 20 35   /*   690 */   5
30b84 30 30 2c 20 20 35 36 36 2c 20 20 31 33 37 2c 20  00,  566,  137, 
30b85 20 32 35 36 2c 20 20 35 37 34 2c 20 20 33 33 37   256,  574,  337
30b86 2c 20 20 35 35 30 2c 20 20 35 34 31 2c 20 20 35  ,  550,  541,  5
30b87 37 39 2c 20 20 32 34 36 2c 0a 20 2f 2a 20 20 20  79,  246,. /*   
30b88 37 30 30 20 2a 2f 20 20 20 33 33 37 2c 20 20 32  700 */   337,  2
30b89 32 34 2c 20 20 33 33 37 2c 20 20 32 35 38 2c 20  24,  337,  258, 
30b8a 20 35 36 36 2c 20 20 31 33 36 2c 20 20 34 39 39   566,  136,  499
30b8b 2c 20 20 33 37 31 2c 20 20 33 34 39 2c 20 20 35  ,  371,  349,  5
30b8c 36 36 2c 0a 20 2f 2a 20 20 20 37 31 30 20 2a 2f  66,. /*   710 */
30b8d 20 20 20 31 33 38 2c 20 20 35 36 36 2c 20 20 31     138,  566,  1
30b8e 30 31 2c 20 20 34 30 37 2c 20 20 34 39 33 2c 20  01,  407,  493, 
30b8f 20 20 34 33 2c 20 20 20 34 31 2c 20 20 20 35 35    43,   41,   55
30b90 2c 20 20 35 33 39 2c 20 20 35 33 37 2c 0a 20 2f  ,  539,  537,. /
30b91 2a 20 20 20 37 32 30 20 2a 2f 20 20 20 35 34 30  *   720 */   540
30b92 2c 20 20 35 34 30 2c 20 20 20 34 35 2c 20 20 20  ,  540,   45,   
30b93 34 35 2c 20 20 20 34 34 2c 20 20 20 34 34 2c 20  45,   44,   44, 
30b94 20 20 34 34 2c 20 20 20 34 34 2c 20 20 33 34 30    44,   44,  340
30b95 2c 20 20 20 34 36 2c 0a 20 2f 2a 20 20 20 37 33  ,   46,. /*   73
30b96 30 20 2a 2f 20 20 20 20 34 36 2c 20 20 20 34 36  0 */    46,   46
30b97 2c 20 20 20 34 36 2c 20 20 20 34 37 2c 20 20 20  ,   46,   47,   
30b98 34 37 2c 20 20 20 34 38 2c 20 20 20 34 38 2c 20  47,   48,   48, 
30b99 20 20 34 38 2c 20 20 20 34 39 2c 20 20 32 32 36    48,   49,  226
30b9a 2c 0a 20 2f 2a 20 20 20 37 34 30 20 2a 2f 20 20  ,. /*   740 */  
30b9b 20 33 31 32 2c 20 20 33 34 30 2c 20 20 32 32 39   312,  340,  229
30b9c 2c 20 20 33 33 37 2c 20 20 33 34 30 2c 20 20 33  ,  337,  340,  3
30b9d 34 32 2c 20 20 32 35 32 2c 20 20 35 33 34 2c 20  42,  252,  534, 
30b9e 20 35 33 34 2c 20 20 35 36 31 2c 0a 20 2f 2a 20   534,  561,. /* 
30b9f 20 20 37 35 30 20 2a 2f 20 20 20 35 32 32 2c 20    750 */   522, 
30ba0 20 35 32 32 2c 20 20 35 36 36 2c 20 20 20 39 31   522,  566,   91
30ba1 2c 20 20 35 37 34 2c 20 20 31 37 37 2c 20 20 33  ,  574,  177,  3
30ba2 33 37 2c 20 20 35 36 31 2c 20 20 35 37 39 2c 20  37,  561,  579, 
30ba3 20 33 33 37 2c 0a 20 2f 2a 20 20 20 37 36 30 20   337,. /*   760 
30ba4 2a 2f 20 20 20 33 34 30 2c 20 20 34 39 33 2c 20  */   340,  493, 
30ba5 20 20 35 38 2c 20 20 33 34 30 2c 20 20 20 33 38    58,  340,   38
30ba6 2c 20 20 35 36 36 2c 20 20 20 38 38 2c 20 20 34  ,  566,   88,  4
30ba7 34 31 2c 20 20 35 36 36 2c 20 20 20 31 37 2c 0a  41,  566,   17,.
30ba8 20 2f 2a 20 20 20 37 37 30 20 2a 2f 20 20 20 35   /*   770 */   5
30ba9 35 30 2c 20 20 35 34 31 2c 20 20 35 30 38 2c 20  50,  541,  508, 
30baa 20 20 32 39 2c 20 20 33 30 36 2c 20 20 33 33 37    29,  306,  337
30bab 2c 20 20 34 35 32 2c 20 20 39 34 38 2c 20 20 33  ,  452,  948,  3
30bac 33 37 2c 20 20 39 34 38 2c 0a 20 2f 2a 20 20 20  37,  948,. /*   
30bad 37 38 30 20 2a 2f 20 20 20 36 32 39 2c 20 20 35  780 */   629,  5
30bae 37 35 2c 20 20 34 31 36 2c 20 20 33 31 38 2c 20  75,  416,  318, 
30baf 20 35 36 36 2c 20 20 20 39 32 2c 20 20 31 39 32   566,   92,  192
30bb0 2c 20 20 35 36 36 2c 20 20 20 37 33 2c 20 20 20  ,  566,   73,   
30bb1 34 33 2c 0a 20 2f 2a 20 20 20 37 39 30 20 2a 2f  43,. /*   790 */
30bb2 20 20 20 20 34 31 2c 20 20 20 35 35 2c 20 20 35      41,   55,  5
30bb3 33 39 2c 20 20 35 33 37 2c 20 20 35 34 30 2c 20  39,  537,  540, 
30bb4 20 35 34 30 2c 20 20 20 34 35 2c 20 20 20 34 35   540,   45,   45
30bb5 2c 20 20 20 34 34 2c 20 20 20 34 34 2c 0a 20 2f  ,   44,   44,. /
30bb6 2a 20 20 20 38 30 30 20 2a 2f 20 20 20 20 34 34  *   800 */    44
30bb7 2c 20 20 20 34 34 2c 20 20 33 34 30 2c 20 20 20  ,   44,  340,   
30bb8 34 36 2c 20 20 20 34 36 2c 20 20 20 34 36 2c 20  46,   46,   46, 
30bb9 20 20 34 36 2c 20 20 20 34 37 2c 20 20 20 34 37    46,   47,   47
30bba 2c 20 20 20 34 38 2c 0a 20 2f 2a 20 20 20 38 31  ,   48,. /*   81
30bbb 30 20 2a 2f 20 20 20 20 34 38 2c 20 20 20 34 38  0 */    48,   48
30bbc 2c 20 20 20 34 39 2c 20 20 32 32 36 2c 20 20 33  ,   49,  226,  3
30bbd 31 32 2c 20 20 33 34 30 2c 20 20 32 34 36 2c 20  12,  340,  246, 
30bbe 20 33 33 37 2c 20 20 33 34 30 2c 20 20 32 34 36   337,  340,  246
30bbf 2c 0a 20 2f 2a 20 20 20 38 32 30 20 2a 2f 20 20  ,. /*   820 */  
30bc0 20 32 37 30 2c 20 20 35 32 36 2c 20 20 33 34 30   270,  526,  340
30bc1 2c 20 20 34 36 39 2c 20 20 32 39 39 2c 20 20 35  ,  469,  299,  5
30bc2 36 31 2c 20 20 35 36 36 2c 20 20 20 37 30 2c 20  61,  566,   70, 
30bc3 20 32 30 32 2c 20 20 32 30 34 2c 0a 20 2f 2a 20   202,  204,. /* 
30bc4 20 20 38 33 30 20 2a 2f 20 20 20 33 33 37 2c 20    830 */   337, 
30bc5 20 34 39 33 2c 20 20 35 30 37 2c 20 20 33 33 37   493,  507,  337
30bc6 2c 20 20 34 39 33 2c 20 20 34 39 33 2c 20 20 33  ,  493,  493,  3
30bc7 34 30 2c 20 20 33 33 37 2c 20 20 34 39 33 2c 20  40,  337,  493, 
30bc8 20 35 36 36 2c 0a 20 2f 2a 20 20 20 38 34 30 20   566,. /*   840 
30bc9 2a 2f 20 20 20 20 39 30 2c 20 20 35 33 32 2c 20  */    90,  532, 
30bca 20 35 36 36 2c 20 20 20 38 32 2c 20 20 35 35 30   566,   82,  550
30bcb 2c 20 20 35 34 31 2c 20 20 35 36 36 2c 20 20 20  ,  541,  566,   
30bcc 37 32 2c 20 20 35 35 37 2c 20 20 34 35 32 2c 0a  72,  557,  452,.
30bcd 20 2f 2a 20 20 20 38 35 30 20 2a 2f 20 20 20 39   /*   850 */   9
30bce 34 39 2c 20 20 33 33 37 2c 20 20 39 34 39 2c 20  49,  337,  949, 
30bcf 20 32 36 36 2c 20 20 35 39 38 2c 20 20 33 34 32   266,  598,  342
30bd0 2c 20 20 31 35 30 2c 20 20 35 33 34 2c 20 20 35  ,  150,  534,  5
30bd1 33 34 2c 20 20 32 32 38 2c 0a 20 2f 2a 20 20 20  34,  228,. /*   
30bd2 38 36 30 20 2a 2f 20 20 20 35 36 36 2c 20 20 20  860 */   566,   
30bd3 37 39 2c 20 20 35 33 33 2c 20 20 20 34 33 2c 20  79,  533,   43, 
30bd4 20 20 34 31 2c 20 20 20 35 35 2c 20 20 35 33 39    41,   55,  539
30bd5 2c 20 20 35 33 37 2c 20 20 35 34 30 2c 20 20 35  ,  537,  540,  5
30bd6 34 30 2c 0a 20 2f 2a 20 20 20 38 37 30 20 2a 2f  40,. /*   870 */
30bd7 20 20 20 20 34 35 2c 20 20 20 34 35 2c 20 20 20      45,   45,   
30bd8 34 34 2c 20 20 20 34 34 2c 20 20 20 34 34 2c 20  44,   44,   44, 
30bd9 20 20 34 34 2c 20 20 33 34 30 2c 20 20 20 34 36    44,  340,   46
30bda 2c 20 20 20 34 36 2c 20 20 20 34 36 2c 0a 20 2f  ,   46,   46,. /
30bdb 2a 20 20 20 38 38 30 20 2a 2f 20 20 20 20 34 36  *   880 */    46
30bdc 2c 20 20 20 34 37 2c 20 20 20 34 37 2c 20 20 20  ,   47,   47,   
30bdd 34 38 2c 20 20 20 34 38 2c 20 20 20 34 38 2c 20  48,   48,   48, 
30bde 20 20 34 39 2c 20 20 32 32 36 2c 20 20 33 31 32    49,  226,  312
30bdf 2c 20 20 33 34 30 2c 0a 20 2f 2a 20 20 20 38 39  ,  340,. /*   89
30be0 30 20 2a 2f 20 20 20 32 34 36 2c 20 20 33 33 37  0 */   246,  337
30be1 2c 20 20 33 34 30 2c 20 20 32 35 32 2c 20 20 35  ,  340,  252,  5
30be2 32 36 2c 20 20 20 34 39 2c 20 20 32 32 36 2c 20  26,   49,  226, 
30be3 20 32 35 34 2c 20 20 35 34 39 2c 20 20 32 36 30   254,  549,  260
30be4 2c 0a 20 2f 2a 20 20 20 39 30 30 20 2a 2f 20 20  ,. /*   900 */  
30be5 20 35 36 36 2c 20 20 20 38 33 2c 20 20 35 38 38   566,   83,  588
30be6 2c 20 20 35 38 36 2c 20 20 33 33 37 2c 20 20 34  ,  586,  337,  4
30be7 39 33 2c 20 20 36 30 36 2c 20 20 33 33 37 2c 20  93,  606,  337, 
30be8 20 34 39 33 2c 20 20 34 38 35 2c 0a 20 2f 2a 20   493,  485,. /* 
30be9 20 20 39 31 30 20 2a 2f 20 20 20 33 34 30 2c 20    910 */   340, 
30bea 20 35 39 32 2c 20 20 33 39 32 2c 20 20 35 36 36   592,  392,  566
30beb 2c 20 20 20 36 39 2c 20 20 34 30 36 2c 20 20 35  ,   69,  406,  5
30bec 36 36 2c 20 20 20 38 30 2c 20 20 35 35 30 2c 20  66,   80,  550, 
30bed 20 35 34 31 2c 0a 20 2f 2a 20 20 20 39 32 30 20   541,. /*   920 
30bee 2a 2f 20 20 20 33 34 32 2c 20 20 33 36 38 2c 20  */   342,  368, 
30bef 20 35 33 34 2c 20 20 35 33 34 2c 20 20 32 35 33   534,  534,  253
30bf0 2c 20 20 33 33 37 2c 20 20 31 35 30 2c 20 20 34  ,  337,  150,  4
30bf1 39 33 2c 20 20 36 30 34 2c 20 20 33 34 32 2c 0a  93,  604,  342,.
30bf2 20 2f 2a 20 20 20 39 33 30 20 2a 2f 20 20 20 32   /*   930 */   2
30bf3 32 33 2c 20 20 35 33 34 2c 20 20 35 33 34 2c 20  23,  534,  534, 
30bf4 20 35 32 35 2c 20 20 35 36 36 2c 20 20 31 30 30   525,  566,  100
30bf5 2c 20 20 32 38 35 2c 20 20 20 34 33 2c 20 20 20  ,  285,   43,   
30bf6 35 37 2c 20 20 20 35 35 2c 0a 20 2f 2a 20 20 20  57,   55,. /*   
30bf7 39 34 30 20 2a 2f 20 20 20 35 33 39 2c 20 20 35  940 */   539,  5
30bf8 33 37 2c 20 20 35 34 30 2c 20 20 35 34 30 2c 20  37,  540,  540, 
30bf9 20 20 34 35 2c 20 20 20 34 35 2c 20 20 20 34 34    45,   45,   44
30bfa 2c 20 20 20 34 34 2c 20 20 20 34 34 2c 20 20 20  ,   44,   44,   
30bfb 34 34 2c 0a 20 2f 2a 20 20 20 39 35 30 20 2a 2f  44,. /*   950 */
30bfc 20 20 20 33 34 30 2c 20 20 20 34 36 2c 20 20 20     340,   46,   
30bfd 34 36 2c 20 20 20 34 36 2c 20 20 20 34 36 2c 20  46,   46,   46, 
30bfe 20 20 34 37 2c 20 20 20 34 37 2c 20 20 20 34 38    47,   47,   48
30bff 2c 20 20 20 34 38 2c 20 20 20 34 38 2c 0a 20 2f  ,   48,   48,. /
30c00 2a 20 20 20 39 36 30 20 2a 2f 20 20 20 20 34 39  *   960 */    49
30c01 2c 20 20 32 32 36 2c 20 20 33 31 32 2c 20 20 33  ,  226,  312,  3
30c02 39 32 2c 20 20 32 34 36 2c 20 20 33 33 37 2c 20  92,  246,  337, 
30c03 20 33 34 30 2c 20 20 32 35 32 2c 20 20 35 37 35   340,  252,  575
30c04 2c 20 20 34 31 36 2c 0a 20 2f 2a 20 20 20 39 37  ,  416,. /*   97
30c05 30 20 2a 2f 20 20 20 33 35 31 2c 20 20 34 37 32  0 */   351,  472
30c06 2c 20 20 32 37 36 2c 20 20 33 34 37 2c 20 20 35  ,  276,  347,  5
30c07 36 36 2c 20 20 20 38 37 2c 20 20 32 37 39 2c 20  66,   87,  279, 
30c08 20 33 33 30 2c 20 20 34 39 33 2c 20 20 34 39 33   330,  493,  493
30c09 2c 0a 20 2f 2a 20 20 20 39 38 30 20 2a 2f 20 20  ,. /*   980 */  
30c0a 20 34 39 36 2c 20 20 33 33 37 2c 20 20 34 39 33   496,  337,  493
30c0b 2c 20 20 34 36 31 2c 20 20 33 34 30 2c 20 20 34  ,  461,  340,  4
30c0c 39 33 2c 20 20 34 39 33 2c 20 20 34 39 33 2c 20  93,  493,  493, 
30c0d 20 35 34 38 2c 20 20 35 34 37 2c 0a 20 2f 2a 20   548,  547,. /* 
30c0e 20 20 39 39 30 20 2a 2f 20 20 20 35 36 36 2c 20    990 */   566, 
30c0f 20 20 39 38 2c 20 20 35 35 30 2c 20 20 35 34 31    98,  550,  541
30c10 2c 20 20 34 39 38 2c 20 20 34 39 33 2c 20 20 31  ,  498,  493,  1
30c11 37 32 2c 20 20 35 32 33 2c 20 20 34 39 33 2c 20  72,  523,  493, 
30c12 20 33 33 37 2c 0a 20 2f 2a 20 20 31 30 30 30 20   337,. /*  1000 
30c13 2a 2f 20 20 20 33 31 31 2c 20 20 35 37 37 2c 20  */   311,  577, 
30c14 20 31 39 31 2c 20 20 32 34 33 2c 20 20 33 31 39   191,  243,  319
30c15 2c 20 20 34 32 33 2c 20 20 34 32 32 2c 20 20 35  ,  423,  422,  5
30c16 33 30 2c 20 20 35 36 36 2c 20 20 31 30 35 2c 0a  30,  566,  105,.
30c17 20 2f 2a 20 20 31 30 31 30 20 2a 2f 20 20 20 35   /*  1010 */   5
30c18 34 34 2c 20 20 35 34 33 2c 20 20 20 34 31 2c 20  44,  543,   41, 
30c19 20 20 35 35 2c 20 20 35 33 39 2c 20 20 35 33 37    55,  539,  537
30c1a 2c 20 20 35 34 30 2c 20 20 35 34 30 2c 20 20 20  ,  540,  540,   
30c1b 34 35 2c 20 20 20 34 35 2c 0a 20 2f 2a 20 20 31  45,   45,. /*  1
30c1c 30 32 30 20 2a 2f 20 20 20 20 34 34 2c 20 20 20  020 */    44,   
30c1d 34 34 2c 20 20 20 34 34 2c 20 20 20 34 34 2c 20  44,   44,   44, 
30c1e 20 33 34 30 2c 20 20 20 34 36 2c 20 20 20 34 36   340,   46,   46
30c1f 2c 20 20 20 34 36 2c 20 20 20 34 36 2c 20 20 20  ,   46,   46,   
30c20 34 37 2c 0a 20 2f 2a 20 20 31 30 33 30 20 2a 2f  47,. /*  1030 */
30c21 20 20 20 20 34 37 2c 20 20 20 34 38 2c 20 20 20      47,   48,   
30c22 34 38 2c 20 20 20 34 38 2c 20 20 20 34 39 2c 20  48,   48,   49, 
30c23 20 32 32 36 2c 20 20 33 31 32 2c 20 20 35 34 35   226,  312,  545
30c24 2c 20 20 33 34 30 2c 20 20 33 33 37 2c 0a 20 2f  ,  340,  337,. /
30c25 2a 20 20 31 30 34 30 20 2a 2f 20 20 20 33 34 30  *  1040 */   340
30c26 2c 20 20 32 34 36 2c 20 20 33 39 32 2c 20 20 32  ,  246,  392,  2
30c27 30 37 2c 20 20 32 35 32 2c 20 20 34 34 35 2c 20  07,  252,  445, 
30c28 20 33 32 30 2c 20 20 34 38 39 2c 20 20 35 36 36   320,  489,  566
30c29 2c 20 20 31 30 33 2c 0a 20 2f 2a 20 20 31 30 35  ,  103,. /*  105
30c2a 30 20 2a 2f 20 20 20 36 32 37 2c 20 20 36 32 32  0 */   627,  622
30c2b 2c 20 20 35 31 39 2c 20 20 33 33 37 2c 20 20 32  ,  519,  337,  2
30c2c 33 32 2c 20 20 33 33 37 2c 20 20 34 39 33 2c 20  32,  337,  493, 
30c2d 20 34 39 33 2c 20 20 33 34 30 2c 20 20 34 39 33   493,  340,  493
30c2e 2c 0a 20 2f 2a 20 20 31 30 36 30 20 2a 2f 20 20  ,. /*  1060 */  
30c2f 20 34 37 39 2c 20 20 33 34 30 2c 20 20 35 36 36   479,  340,  566
30c30 2c 20 20 31 30 38 2c 20 20 35 36 36 2c 20 20 31  ,  108,  566,  1
30c31 30 39 2c 20 20 35 35 30 2c 20 20 35 34 31 2c 20  09,  550,  541, 
30c32 20 20 20 36 2c 20 20 36 30 37 2c 0a 20 2f 2a 20     6,  607,. /* 
30c33 20 31 30 37 30 20 2a 2f 20 20 20 36 30 38 2c 20   1070 */   608, 
30c34 20 34 35 34 2c 20 20 20 20 32 2c 20 20 33 33 37   454,    2,  337
30c35 2c 20 20 34 39 32 2c 20 20 34 39 33 2c 20 20 33  ,  492,  493,  3
30c36 33 37 2c 20 20 35 36 35 2c 20 20 35 31 37 2c 20  37,  565,  517, 
30c37 20 32 33 31 2c 0a 20 2f 2a 20 20 31 30 38 30 20   231,. /*  1080 
30c38 2a 2f 20 20 20 34 37 31 2c 20 20 33 32 32 2c 20  */   471,  322, 
30c39 20 35 36 36 2c 20 20 31 33 34 2c 20 20 32 38 39   566,  134,  289
30c3a 2c 20 20 35 36 36 2c 20 20 31 33 35 2c 20 20 20  ,  566,  135,   
30c3b 35 35 2c 20 20 35 33 39 2c 20 20 35 33 37 2c 0a  55,  539,  537,.
30c3c 20 2f 2a 20 20 31 30 39 30 20 2a 2f 20 20 20 35   /*  1090 */   5
30c3d 34 30 2c 20 20 35 34 30 2c 20 20 20 34 35 2c 20  40,  540,   45, 
30c3e 20 20 34 35 2c 20 20 20 34 34 2c 20 20 20 34 34    45,   44,   44
30c3f 2c 20 20 20 34 34 2c 20 20 20 34 34 2c 20 20 34  ,   44,   44,  4
30c40 38 37 2c 20 20 20 34 36 2c 0a 20 2f 2a 20 20 31  87,   46,. /*  1
30c41 31 30 30 20 2a 2f 20 20 20 20 34 36 2c 20 20 20  100 */    46,   
30c42 34 36 2c 20 20 20 34 36 2c 20 20 20 34 37 2c 20  46,   46,   47, 
30c43 20 20 34 37 2c 20 20 20 34 38 2c 20 20 20 34 38    47,   48,   48
30c44 2c 20 20 20 34 38 2c 20 20 20 34 39 2c 20 20 32  ,   48,   49,  2
30c45 32 36 2c 0a 20 2f 2a 20 20 31 31 31 30 20 2a 2f  26,. /*  1110 */
30c46 20 20 20 20 32 33 2c 20 20 33 34 36 2c 20 20 33      23,  346,  3
30c47 34 30 2c 20 20 20 20 33 2c 20 20 33 39 37 2c 20  40,    3,  397, 
30c48 20 34 36 34 2c 20 20 34 32 32 2c 20 20 33 33 36   464,  422,  336
30c49 2c 20 20 35 30 31 2c 20 20 32 39 30 2c 0a 20 2f  ,  501,  290,. /
30c4a 2a 20 20 31 31 32 30 20 2a 2f 20 20 20 31 34 37  *  1120 */   147
30c4b 2c 20 20 32 31 34 2c 20 20 20 20 37 2c 20 20 20  ,  214,    7,   
30c4c 32 33 2c 20 20 33 34 36 2c 20 20 33 34 30 2c 20  23,  346,  340, 
30c4d 20 20 20 33 2c 20 20 33 33 37 2c 20 20 33 34 33     3,  337,  343
30c4e 2c 20 20 34 39 33 2c 0a 20 2f 2a 20 20 31 31 33  ,  493,. /*  113
30c4f 30 20 2a 2f 20 20 20 33 33 36 2c 20 20 35 30 31  0 */   336,  501
30c50 2c 20 20 34 36 33 2c 20 20 32 33 37 2c 20 20 31  ,  463,  237,  1
30c51 37 32 2c 20 20 34 39 33 2c 20 20 35 36 36 2c 20  72,  493,  566, 
30c52 20 20 38 34 2c 20 20 34 37 35 2c 20 20 33 34 30    84,  475,  340
30c53 2c 0a 20 2f 2a 20 20 31 31 34 30 20 2a 2f 20 20  ,. /*  1140 */  
30c54 20 33 33 37 2c 20 20 33 34 33 2c 20 20 32 31 31   337,  343,  211
30c55 2c 20 20 33 34 30 2c 20 20 32 34 36 2c 20 20 32  ,  340,  246,  2
30c56 32 35 2c 20 20 32 34 34 2c 20 20 33 34 38 2c 20  25,  244,  348, 
30c57 20 33 39 34 2c 20 20 35 36 36 2c 0a 20 2f 2a 20   394,  566,. /* 
30c58 20 31 31 35 30 20 2a 2f 20 20 20 20 37 35 2c 20   1150 */    75, 
30c59 20 33 34 31 2c 20 20 20 31 38 2c 20 20 34 39 33   341,   18,  493
30c5a 2c 20 20 33 33 37 2c 20 20 35 36 35 2c 20 20 33  ,  337,  565,  3
30c5b 39 33 2c 20 20 35 30 39 2c 20 20 33 33 37 2c 20  93,  509,  337, 
30c5c 20 34 39 33 2c 0a 20 2f 2a 20 20 31 31 36 30 20   493,. /*  1160 
30c5d 2a 2f 20 20 20 33 34 38 2c 20 20 34 39 33 2c 20  */   348,  493, 
30c5e 20 33 39 35 2c 20 20 35 36 36 2c 20 20 20 39 37   395,  566,   97
30c5f 2c 20 20 32 34 35 2c 20 20 34 39 33 2c 20 20 35  ,  245,  493,  5
30c60 36 36 2c 20 20 20 36 31 2c 20 20 32 37 34 2c 0a  66,   61,  274,.
30c61 20 2f 2a 20 20 31 31 37 30 20 2a 2f 20 20 20 35   /*  1170 */   5
30c62 30 39 2c 20 20 32 31 37 2c 20 20 20 31 36 2c 20  09,  217,   16, 
30c63 20 33 35 38 2c 20 20 31 38 39 2c 20 20 32 36 34   358,  189,  264
30c64 2c 20 20 35 36 35 2c 20 20 20 32 36 2c 20 20 20  ,  565,   26,   
30c65 32 35 2c 20 20 33 34 30 2c 0a 20 2f 2a 20 20 31  25,  340,. /*  1
30c66 31 38 30 20 2a 2f 20 20 20 34 39 33 2c 20 20 32  180 */   493,  2
30c67 33 36 2c 20 20 35 31 38 2c 20 20 33 31 30 2c 20  36,  518,  310, 
30c68 20 20 32 34 2c 20 20 33 33 38 2c 20 20 33 33 39    24,  338,  339
30c69 2c 20 20 33 30 34 2c 20 20 34 39 33 2c 20 20 35  ,  304,  493,  5
30c6a 35 36 2c 0a 20 2f 2a 20 20 31 31 39 30 20 2a 2f  56,. /*  1190 */
30c6b 20 20 20 20 32 36 2c 20 20 20 32 35 2c 20 20 34      26,   25,  4
30c6c 35 30 2c 20 20 36 30 32 2c 20 20 33 33 37 2c 20  50,  602,  337, 
30c6d 20 34 30 34 2c 20 20 31 32 30 2c 20 20 20 32 34   404,  120,   24
30c6e 2c 20 20 33 33 38 2c 20 20 33 33 39 2c 0a 20 2f  ,  338,  339,. /
30c6f 2a 20 20 31 32 30 30 20 2a 2f 20 20 20 34 31 39  *  1200 */   419
30c70 2c 20 20 31 36 39 2c 20 20 35 35 36 2c 20 20 35  ,  169,  556,  5
30c71 36 36 2c 20 20 31 30 36 2c 20 20 35 36 35 2c 20  66,  106,  565, 
30c72 20 32 32 34 2c 20 20 31 31 37 2c 20 20 34 39 35   224,  117,  495
30c73 2c 20 20 20 32 37 2c 0a 20 2f 2a 20 20 31 32 31  ,   27,. /*  121
30c74 30 20 2a 2f 20 20 20 33 34 36 2c 20 20 35 36 32  0 */   346,  562
30c75 2c 20 20 20 20 33 2c 20 20 33 39 36 2c 20 20 31  ,    3,  396,  1
30c76 38 37 2c 20 20 33 34 30 2c 20 20 33 33 36 2c 20  87,  340,  336, 
30c77 20 35 30 31 2c 20 20 31 37 39 2c 20 20 31 38 34   501,  179,  184
30c78 2c 0a 20 2f 2a 20 20 31 32 32 30 20 2a 2f 20 20  ,. /*  1220 */  
30c79 20 35 35 33 2c 20 20 35 35 33 2c 20 20 35 35 33   553,  553,  553
30c7a 2c 20 20 35 35 32 2c 20 20 35 35 31 2c 20 20 20  ,  552,  551,   
30c7b 31 31 2c 20 20 34 39 33 2c 20 20 33 34 33 2c 20  11,  493,  343, 
30c7c 20 34 39 33 2c 20 20 33 39 38 2c 0a 20 2f 2a 20   493,  398,. /* 
30c7d 20 31 32 33 30 20 2a 2f 20 20 20 33 33 37 2c 20   1230 */   337, 
30c7e 20 34 33 30 2c 20 20 20 20 38 2c 20 20 35 35 33   430,    8,  553
30c7f 2c 20 20 35 35 33 2c 20 20 35 35 33 2c 20 20 35  ,  553,  553,  5
30c80 35 32 2c 20 20 35 35 31 2c 20 20 20 31 31 2c 20  52,  551,   11, 
30c81 20 35 36 36 2c 0a 20 2f 2a 20 20 31 32 34 30 20   566,. /*  1240 
30c82 2a 2f 20 20 20 31 30 34 2c 20 20 33 33 35 2c 20  */   104,  335, 
30c83 20 33 34 30 2c 20 20 32 35 35 2c 20 20 33 34 30   340,  255,  340
30c84 2c 20 20 36 31 36 2c 20 20 33 34 38 2c 20 20 33  ,  616,  348,  3
30c85 34 30 2c 20 20 32 35 31 2c 20 20 33 33 31 2c 0a  40,  251,  331,.
30c86 20 2f 2a 20 20 31 32 35 30 20 2a 2f 20 20 20 33   /*  1250 */   3
30c87 34 30 2c 20 20 33 39 31 2c 20 20 35 35 38 2c 20  40,  391,  558, 
30c88 20 35 38 37 2c 20 20 20 35 30 2c 20 20 33 34 30   587,   50,  340
30c89 2c 20 20 35 30 39 2c 20 20 33 33 37 2c 20 20 34  ,  509,  337,  4
30c8a 39 33 2c 20 20 33 33 37 2c 0a 20 2f 2a 20 20 31  93,  337,. /*  1
30c8b 32 36 30 20 2a 2f 20 20 20 34 39 33 2c 20 20 33  260 */   493,  3
30c8c 36 35 2c 20 20 33 33 37 2c 20 20 34 39 33 2c 20  65,  337,  493, 
30c8d 20 35 39 30 2c 20 20 33 33 37 2c 20 20 35 36 36   590,  337,  566
30c8e 2c 20 20 20 37 38 2c 20 20 35 36 36 2c 20 20 31  ,   78,  566,  1
30c8f 30 32 2c 0a 20 2f 2a 20 20 31 32 37 30 20 2a 2f  02,. /*  1270 */
30c90 20 20 20 33 33 37 2c 20 20 35 36 36 2c 20 20 20     337,  566,   
30c91 37 36 2c 20 20 32 31 33 2c 20 20 35 36 36 2c 20  76,  213,  566, 
30c92 20 20 37 34 2c 20 20 20 32 36 2c 20 20 20 32 35    74,   26,   25
30c93 2c 20 20 36 31 30 2c 20 20 35 36 36 2c 0a 20 2f  ,  610,  566,. /
30c94 2a 20 20 31 32 38 30 20 2a 2f 20 20 20 20 39 33  *  1280 */    93
30c95 2c 20 20 33 37 38 2c 20 20 34 32 34 2c 20 20 20  ,  378,  424,   
30c96 32 34 2c 20 20 33 33 38 2c 20 20 33 33 39 2c 20  24,  338,  339, 
30c97 20 32 36 33 2c 20 20 35 38 33 2c 20 20 35 35 36   263,  583,  556
30c98 2c 20 20 32 32 30 2c 0a 20 2f 2a 20 20 31 32 39  ,  220,. /*  129
30c99 30 20 2a 2f 20 20 20 31 34 30 2c 20 20 36 32 34  0 */   140,  624
30c9a 2c 20 20 32 39 34 2c 20 20 34 39 33 2c 20 20 33  ,  294,  493,  3
30c9b 30 30 2c 20 20 33 38 39 2c 20 20 20 32 33 2c 20  00,  389,   23, 
30c9c 20 33 34 36 2c 20 20 33 37 36 2c 20 20 20 20 33   346,  376,    3
30c9d 2c 0a 20 2f 2a 20 20 31 33 30 30 20 2a 2f 20 20  ,. /*  1300 */  
30c9e 20 31 35 30 2c 20 20 34 39 33 2c 20 20 34 39 33   150,  493,  493
30c9f 2c 20 20 33 33 36 2c 20 20 35 30 31 2c 20 20 34  ,  336,  501,  4
30ca0 39 33 2c 20 20 34 39 33 2c 20 20 34 39 33 2c 20  93,  493,  493, 
30ca1 20 32 32 31 2c 20 20 34 39 33 2c 0a 20 2f 2a 20   221,  493,. /* 
30ca2 20 31 33 31 30 20 2a 2f 20 20 20 34 39 33 2c 20   1310 */   493, 
30ca3 20 35 35 34 2c 20 20 32 36 38 2c 20 20 32 31 39   554,  268,  219
30ca4 2c 20 20 33 34 33 2c 20 20 32 36 31 2c 20 20 35  ,  343,  261,  5
30ca5 31 35 2c 20 20 34 36 30 2c 20 20 34 31 38 2c 20  15,  460,  418, 
30ca6 20 35 35 33 2c 0a 20 2f 2a 20 20 31 33 32 30 20   553,. /*  1320 
30ca7 2a 2f 20 20 20 35 35 33 2c 20 20 35 35 33 2c 20  */   553,  553, 
30ca8 20 35 35 32 2c 20 20 35 35 31 2c 20 20 20 31 31   552,  551,   11
30ca9 2c 20 20 35 39 35 2c 20 20 34 39 33 2c 20 20 34  ,  595,  493,  4
30caa 39 33 2c 20 20 31 35 32 2c 20 20 32 30 36 2c 0a  93,  152,  206,.
30cab 20 2f 2a 20 20 31 33 33 30 20 2a 2f 20 20 20 34   /*  1330 */   4
30cac 39 33 2c 20 20 34 39 33 2c 20 20 34 39 33 2c 20  93,  493,  493, 
30cad 20 33 34 38 2c 20 20 32 37 32 2c 20 20 32 37 31   348,  272,  271
30cae 2c 20 20 32 36 39 2c 20 20 31 35 38 2c 20 20 33  ,  269,  158,  3
30caf 30 32 2c 20 20 36 32 35 2c 0a 20 2f 2a 20 20 31  02,  625,. /*  1
30cb0 33 34 30 20 2a 2f 20 20 20 35 33 31 2c 20 20 35  340 */   531,  5
30cb1 32 31 2c 20 20 20 36 37 2c 20 20 35 30 39 2c 20  21,   67,  509, 
30cb2 20 35 30 36 2c 20 20 35 32 30 2c 20 20 20 35 30   506,  520,   50
30cb3 2c 20 20 36 32 38 2c 20 20 33 38 30 2c 20 20 36  ,  628,  380,  6
30cb4 30 30 2c 0a 20 2f 2a 20 20 31 33 35 30 20 2a 2f  00,. /*  1350 */
30cb5 20 20 20 31 37 33 2c 20 20 36 30 35 2c 20 20 31     173,  605,  1
30cb6 32 33 2c 20 20 36 32 33 2c 20 20 34 39 33 2c 20  23,  623,  493, 
30cb7 20 31 37 38 2c 20 20 20 33 37 2c 20 20 33 30 35   178,   37,  305
30cb8 2c 20 20 33 30 38 2c 20 20 32 36 35 2c 0a 20 2f  ,  308,  265,. /
30cb9 2a 20 20 31 33 36 30 20 2a 2f 20 20 20 34 39 33  *  1360 */   493
30cba 2c 20 20 36 32 30 2c 20 20 36 32 31 2c 20 20 20  ,  620,  621,   
30cbb 32 36 2c 20 20 20 32 35 2c 20 20 31 36 35 2c 20  26,   25,  165, 
30cbc 20 32 36 32 2c 20 20 33 30 37 2c 20 20 36 31 39   262,  307,  619
30cbd 2c 20 20 31 36 36 2c 0a 20 2f 2a 20 20 31 33 37  ,  166,. /*  137
30cbe 30 20 2a 2f 20 20 20 20 32 34 2c 20 20 33 33 38  0 */    24,  338
30cbf 2c 20 20 33 33 39 2c 20 20 33 33 34 2c 20 20 34  ,  339,  334,  4
30cc0 33 36 2c 20 20 35 35 36 2c 20 20 34 32 37 2c 20  36,  556,  427, 
30cc1 20 34 33 37 2c 20 20 20 33 33 2c 20 20 31 36 30   437,   33,  160
30cc2 2c 0a 20 2f 2a 20 20 31 33 38 30 20 2a 2f 20 20  ,. /*  1380 */  
30cc3 20 31 34 35 2c 20 20 34 36 36 2c 20 20 35 31 33   145,  466,  513
30cc4 2c 20 20 35 30 31 2c 20 20 20 32 30 2c 20 20 31  ,  501,   20,  1
30cc5 34 38 2c 20 20 32 37 35 2c 20 20 31 32 32 2c 20  48,  275,  122, 
30cc6 20 31 37 34 2c 20 20 33 35 39 2c 0a 20 2f 2a 20   174,  359,. /* 
30cc7 20 31 33 39 30 20 2a 2f 20 20 20 34 34 32 2c 20   1390 */   442, 
30cc8 20 34 36 37 2c 20 20 34 38 36 2c 20 20 20 36 33   467,  486,   63
30cc9 2c 20 20 20 33 36 2c 20 20 32 33 33 2c 20 20 20  ,   36,  233,   
30cca 34 32 2c 20 20 34 36 35 2c 20 20 35 31 32 2c 20  42,  465,  512, 
30ccb 20 34 30 33 2c 0a 20 2f 2a 20 20 31 34 30 30 20   403,. /*  1400 
30ccc 2a 2f 20 20 20 35 34 32 2c 20 20 31 38 32 2c 20  */   542,  182, 
30ccd 20 31 31 39 2c 20 20 31 31 33 2c 20 20 31 32 38   119,  113,  128
30cce 2c 20 20 31 33 32 2c 20 20 35 35 33 2c 20 20 35  ,  132,  553,  5
30ccf 35 33 2c 20 20 35 35 33 2c 20 20 35 35 32 2c 0a  53,  553,  552,.
30cd0 20 2f 2a 20 20 31 34 31 30 20 2a 2f 20 20 20 35   /*  1410 */   5
30cd1 35 31 2c 20 20 20 31 31 2c 20 20 20 33 39 2c 20  51,   11,   39, 
30cd2 20 32 38 38 2c 20 20 35 33 38 2c 20 20 34 35 35   288,  538,  455
30cd3 2c 20 20 32 39 31 2c 20 20 35 36 33 2c 20 20 33  ,  291,  563,  3
30cd4 38 38 2c 20 20 33 35 34 2c 0a 20 2f 2a 20 20 31  88,  354,. /*  1
30cd5 34 32 30 20 2a 2f 20 20 20 32 39 33 2c 20 20 32  420 */   293,  2
30cd6 39 35 2c 20 20 35 30 39 2c 20 20 20 33 31 2c 20  95,  509,   31, 
30cd7 20 34 35 33 2c 20 20 33 35 36 2c 20 20 32 39 36   453,  356,  296
30cd8 2c 20 20 33 32 35 2c 20 20 31 35 39 2c 20 20 34  ,  325,  159,  4
30cd9 30 35 2c 0a 20 2f 2a 20 20 31 34 33 30 20 2a 2f  05,. /*  1430 */
30cda 20 20 20 32 38 36 2c 20 20 32 39 37 2c 20 20 35     286,  297,  5
30cdb 36 34 2c 20 20 31 35 33 2c 20 20 33 31 33 2c 20  64,  153,  313, 
30cdc 20 33 38 32 2c 20 20 20 33 35 2c 20 20 31 32 39   382,   35,  129
30cdd 2c 20 20 33 30 33 2c 20 20 32 32 34 2c 0a 20 2f  ,  303,  224,. /
30cde 2a 20 20 31 34 34 30 20 2a 2f 20 20 20 32 31 38  *  1440 */   218
30cdf 2c 20 20 31 32 31 2c 20 20 31 39 34 2c 20 20 31  ,  121,  194,  1
30ce0 39 36 2c 20 20 34 33 33 2c 20 20 31 30 37 2c 20  96,  433,  107, 
30ce1 20 35 35 39 2c 20 20 32 34 32 2c 20 20 36 30 31   559,  242,  601
30ce2 2c 20 20 32 31 36 2c 0a 20 2f 2a 20 20 31 34 35  ,  216,. /*  145
30ce3 30 20 2a 2f 20 20 20 34 32 38 2c 20 20 36 37 30  0 */   428,  670
30ce4 2c 20 20 32 38 30 2c 20 20 36 37 31 2c 20 20 35  ,  280,  671,  5
30ce5 35 36 2c 20 20 36 37 32 2c 20 20 31 36 32 2c 20  56,  672,  162, 
30ce6 20 31 36 33 2c 20 20 20 33 32 2c 20 20 35 33 35   163,   32,  535
30ce7 2c 0a 20 2f 2a 20 20 31 34 36 30 20 2a 2f 20 20  ,. /*  1460 */  
30ce8 20 20 35 39 2c 20 20 35 30 35 2c 20 20 35 32 39    59,  505,  529
30ce9 2c 20 20 34 31 31 2c 20 20 35 31 30 2c 20 20 31  ,  411,  510,  1
30cea 39 30 2c 20 20 31 39 39 2c 20 20 31 37 36 2c 20  90,  199,  176, 
30ceb 20 31 36 37 2c 20 20 35 30 34 2c 0a 20 2f 2a 20   167,  504,. /* 
30cec 20 31 34 37 30 20 2a 2f 20 20 20 35 30 33 2c 20   1470 */   503, 
30ced 20 34 39 37 2c 20 20 20 20 35 2c 20 20 33 31 35   497,    5,  315
30cee 2c 20 20 33 31 34 2c 20 20 20 31 33 2c 20 20 20  ,  314,   13,   
30cef 31 39 2c 20 20 20 31 32 2c 20 20 34 37 34 2c 20  19,   12,  474, 
30cf0 20 31 33 31 2c 0a 20 2f 2a 20 20 31 34 38 30 20   131,. /*  1480 
30cf1 2a 2f 20 20 20 34 35 36 2c 20 20 31 34 34 2c 20  */   456,  144, 
30cf2 20 34 33 34 2c 20 20 34 33 32 2c 20 20 31 36 38   434,  432,  168
30cf3 2c 20 20 35 35 33 2c 20 20 35 35 33 2c 20 20 35  ,  553,  553,  5
30cf4 35 33 2c 20 20 32 33 39 2c 20 20 20 35 31 2c 0a  53,  239,   51,.
30cf5 20 2f 2a 20 20 31 34 39 30 20 2a 2f 20 20 20 36   /*  1490 */   6
30cf6 30 33 2c 20 20 31 31 38 2c 20 20 31 31 31 2c 20  03,  118,  111, 
30cf7 20 31 34 32 2c 20 20 32 34 39 2c 20 20 20 32 31   142,  249,   21
30cf8 2c 20 20 31 32 34 2c 20 20 31 36 34 2c 20 20 33  ,  124,  164,  3
30cf9 37 39 2c 20 20 32 35 38 2c 0a 20 2f 2a 20 20 31  79,  258,. /*  1
30cfa 35 30 30 20 2a 2f 20 20 20 36 32 36 2c 20 20 31  500 */   626,  1
30cfb 38 38 2c 20 20 31 32 36 2c 20 20 31 37 32 2c 20  88,  126,  172, 
30cfc 20 32 39 38 2c 20 20 33 38 33 2c 20 20 31 36 31   298,  383,  161
30cfd 2c 20 20 33 37 30 2c 20 20 33 37 35 2c 20 20 31  ,  370,  375,  1
30cfe 31 34 2c 0a 20 2f 2a 20 20 31 35 31 30 20 2a 2f  14,. /*  1510 */
30cff 20 20 20 20 31 35 2c 20 20 34 37 30 2c 20 20 34      15,  470,  4
30d00 38 31 2c 20 20 31 38 30 2c 20 20 31 32 35 2c 20  81,  180,  125, 
30d01 20 31 31 35 2c 20 20 31 32 37 2c 20 20 34 33 38   115,  127,  438
30d02 2c 20 20 34 34 36 2c 20 20 20 31 30 2c 0a 20 2f  ,  446,   10,. /
30d03 2a 20 20 31 35 32 30 20 2a 2f 20 20 20 35 31 34  *  1520 */   514
30d04 2c 20 20 31 31 30 2c 20 20 31 37 31 2c 20 20 35  ,  110,  171,  5
30d05 32 37 2c 20 20 31 37 30 2c 20 20 31 31 36 2c 20  27,  170,  116, 
30d06 20 31 33 30 2c 20 20 35 36 30 2c 20 20 20 36 34   130,  560,   64
30d07 2c 20 20 20 31 34 2c 0a 20 2f 2a 20 20 31 35 33  ,   14,. /*  153
30d08 30 20 2a 2f 20 20 20 31 37 35 2c 20 20 35 37 36  0 */   175,  576
30d09 2c 20 20 35 37 38 2c 20 20 34 31 33 2c 20 20 32  ,  578,  413,  2
30d0a 37 37 2c 20 20 31 39 33 2c 20 20 34 31 37 2c 20  77,  193,  417, 
30d0b 20 39 36 30 2c 20 20 34 37 33 2c 20 20 39 36 30   960,  473,  960
30d0c 2c 0a 20 2f 2a 20 20 31 35 34 30 20 2a 2f 20 20  ,. /*  1540 */  
30d0d 20 20 32 38 2c 20 20 39 36 30 2c 20 20 20 32 32    28,  960,   22
30d0e 2c 0a 7d 3b 0a 73 74 61 74 69 63 20 63 6f 6e 73  ,.};.static cons
30d0f 74 20 59 59 43 4f 44 45 54 59 50 45 20 79 79 5f  t YYCODETYPE yy_
30d10 6c 6f 6f 6b 61 68 65 61 64 5b 5d 20 3d 20 7b 0a  lookahead[] = {.
30d11 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20 20 20   /*     0 */    
30d12 31 39 2c 20 20 32 32 32 2c 20 20 32 32 33 2c 20  19,  222,  223, 
30d13 20 32 32 34 2c 20 20 32 32 35 2c 20 20 20 32 34   224,  225,   24
30d14 2c 20 20 20 33 35 2c 20 20 20 32 36 2c 20 20 20  ,   35,   26,   
30d15 37 37 2c 20 20 20 37 38 2c 0a 20 2f 2a 20 20 20  77,   78,. /*   
30d16 20 31 30 20 2a 2f 20 20 20 20 37 39 2c 20 20 20   10 */    79,   
30d17 38 30 2c 20 20 31 31 35 2c 20 20 20 38 32 2c 20  80,  115,   82, 
30d18 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35    83,   84,   85
30d19 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20  ,   86,   87,   
30d1a 38 38 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f  88,. /*    20 */
30d1b 20 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20      89,   90,   
30d1c 39 31 2c 20 20 20 39 32 2c 20 20 31 36 36 2c 20  91,   92,  166, 
30d1d 20 31 36 37 2c 20 20 31 36 38 2c 20 20 20 32 36   167,  168,   26
30d1e 2c 20 20 20 32 37 2c 20 20 20 39 32 2c 0a 20 2f  ,   27,   92,. /
30d1f 2a 20 20 20 20 33 30 20 2a 2f 20 20 20 20 34 39  *    30 */    49
30d20 2c 20 20 20 35 30 2c 20 20 20 38 31 2c 20 20 20  ,   50,   81,   
30d21 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20  82,   83,   84, 
30d22 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37    85,   86,   87
30d23 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20 20 34  ,   88,. /*    4
30d24 30 20 2a 2f 20 20 20 20 38 39 2c 20 20 20 39 30  0 */    89,   90
30d25 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20  ,   91,   92,   
30d26 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20  88,   89,   90, 
30d27 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 36 38    91,   92,   68
30d28 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20  ,. /*    50 */  
30d29 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31    69,   70,   71
30d2a 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20  ,   72,   73,   
30d2b 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20  74,   75,   76, 
30d2c 20 20 37 37 2c 20 20 20 37 38 2c 0a 20 2f 2a 20    77,   78,. /* 
30d2d 20 20 20 36 30 20 2a 2f 20 20 20 20 37 39 2c 20     60 */    79, 
30d2e 20 20 38 30 2c 20 20 31 35 30 2c 20 20 20 38 32    80,  150,   82
30d2f 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20  ,   83,   84,   
30d30 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20  85,   86,   87, 
30d31 20 20 38 38 2c 0a 20 2f 2a 20 20 20 20 37 30 20    88,. /*    70 
30d32 2a 2f 20 20 20 20 38 39 2c 20 20 20 39 30 2c 20  */    89,   90, 
30d33 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31 39    91,   92,   19
30d34 2c 20 20 20 39 34 2c 20 20 20 31 39 2c 20 20 31  ,   94,   19,  1
30d35 36 35 2c 20 20 31 37 34 2c 20 20 31 35 30 2c 0a  65,  174,  150,.
30d36 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 20   /*    80 */    
30d37 32 35 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20  25,   82,   83, 
30d38 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36    84,   85,   86
30d39 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20  ,   87,   88,   
30d3a 38 39 2c 20 20 20 39 30 2c 0a 20 2f 2a 20 20 20  89,   90,. /*   
30d3b 20 39 30 20 2a 2f 20 20 20 20 39 31 2c 20 20 20   90 */    91,   
30d3c 39 32 2c 20 20 31 38 36 2c 20 20 20 31 39 2c 20  92,  186,   19, 
30d3d 20 31 36 35 2c 20 20 20 39 34 2c 20 20 20 39 35   165,   94,   95
30d3e 2c 20 20 20 39 36 2c 20 20 31 37 34 2c 20 20 31  ,   96,  174,  1
30d3f 38 37 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f  87,. /*   100 */
30d40 20 20 20 20 39 39 2c 20 20 31 30 30 2c 20 20 31      99,  100,  1
30d41 30 31 2c 20 20 31 38 36 2c 20 20 20 34 39 2c 20  01,  186,   49, 
30d42 20 20 35 30 2c 20 20 20 32 32 2c 20 20 20 32 33    50,   22,   23
30d43 2c 20 20 20 39 36 2c 20 20 20 35 34 2c 0a 20 2f  ,   96,   54,. /
30d44 2a 20 20 20 31 31 30 20 2a 2f 20 20 20 31 30 39  *   110 */   109
30d45 2c 20 20 20 39 39 2c 20 20 31 30 30 2c 20 20 31  ,   99,  100,  1
30d46 30 31 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20  01,   86,   87, 
30d47 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30    88,   89,   90
30d48 2c 20 20 20 39 31 2c 0a 20 2f 2a 20 20 20 31 32  ,   91,. /*   12
30d49 30 20 2a 2f 20 20 20 20 39 32 2c 20 20 31 30 39  0 */    92,  109
30d4a 2c 20 20 31 39 33 2c 20 20 20 36 38 2c 20 20 20  ,  193,   68,   
30d4b 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20  69,   70,   71, 
30d4c 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34    72,   73,   74
30d4d 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20  ,. /*   130 */  
30d4e 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37    75,   76,   77
30d4f 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20  ,   78,   79,   
30d50 38 30 2c 20 20 31 39 35 2c 20 20 20 38 32 2c 20  80,  195,   82, 
30d51 20 20 38 33 2c 20 20 20 38 34 2c 0a 20 2f 2a 20    83,   84,. /* 
30d52 20 20 31 34 30 20 2a 2f 20 20 20 20 38 35 2c 20    140 */    85, 
30d53 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38    86,   87,   88
30d54 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20  ,   89,   90,   
30d55 39 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c 20  91,   92,   19, 
30d56 20 20 39 32 2c 0a 20 2f 2a 20 20 20 31 35 30 20    92,. /*   150 
30d57 2a 2f 20 20 20 31 36 30 2c 20 20 20 36 37 2c 20  */   160,   67, 
30d58 20 20 32 33 2c 20 20 20 39 36 2c 20 20 20 39 37    23,   96,   97
30d59 2c 20 20 20 39 38 2c 20 20 20 39 39 2c 20 20 31  ,   98,   99,  1
30d5a 30 30 2c 20 20 31 30 31 2c 20 20 31 30 32 2c 0a  00,  101,  102,.
30d5b 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20 20 20   /*   160 */    
30d5c 36 36 2c 20 20 31 39 31 2c 20 20 20 32 36 2c 20  66,  191,   26, 
30d5d 20 20 32 37 2c 20 20 20 32 36 2c 20 20 31 30 38    27,   26,  108
30d5e 2c 20 20 20 39 32 2c 20 20 32 30 38 2c 20 20 20  ,   92,  208,   
30d5f 32 36 2c 20 20 20 32 37 2c 0a 20 2f 2a 20 20 20  26,   27,. /*   
30d60 31 37 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 20  170 */   150,   
30d61 39 37 2c 20 20 20 39 38 2c 20 20 20 39 39 2c 20  97,   98,   99, 
30d62 20 31 30 30 2c 20 20 31 30 31 2c 20 20 31 30 32   100,  101,  102
30d63 2c 20 20 31 35 35 2c 20 20 20 34 39 2c 20 20 20  ,  155,   49,   
30d64 35 30 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f  50,. /*   180 */
30d65 20 20 20 31 38 33 2c 20 20 31 35 30 2c 20 20 31     183,  150,  1
30d66 30 38 2c 20 20 20 32 35 2c 20 20 31 39 34 2c 20  08,   25,  194, 
30d67 20 31 36 35 2c 20 20 31 36 33 2c 20 20 31 36 36   165,  163,  166
30d68 2c 20 20 20 39 34 2c 20 20 31 36 36 2c 0a 20 2f  ,   94,  166,. /
30d69 2a 20 20 20 31 39 30 20 2a 2f 20 20 20 31 36 37  *   190 */   167
30d6a 2c 20 20 31 36 38 2c 20 20 20 39 38 2c 20 20 31  ,  168,   98,  1
30d6b 38 33 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  83,  174,  175, 
30d6c 20 31 36 35 2c 20 20 20 36 38 2c 20 20 20 36 39   165,   68,   69
30d6d 2c 20 20 20 37 30 2c 0a 20 2f 2a 20 20 20 32 30  ,   70,. /*   20
30d6e 30 20 2a 2f 20 20 20 20 37 31 2c 20 20 20 37 32  0 */    71,   72
30d6f 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20  ,   73,   74,   
30d70 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20  75,   76,   77, 
30d71 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30    78,   79,   80
30d72 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20  ,. /*   210 */  
30d73 20 31 36 36 2c 20 20 20 38 32 2c 20 20 20 38 33   166,   82,   83
30d74 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20  ,   84,   85,   
30d75 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20  86,   87,   88, 
30d76 20 20 38 39 2c 20 20 20 39 30 2c 0a 20 2f 2a 20    89,   90,. /* 
30d77 20 20 32 32 30 20 2a 2f 20 20 20 20 39 31 2c 20    220 */    91, 
30d78 20 20 39 32 2c 20 20 20 31 39 2c 20 20 31 32 39    92,   19,  129
30d79 2c 20 20 31 33 30 2c 20 20 31 33 31 2c 20 20 32  ,  130,  131,  2
30d7a 30 35 2c 20 20 20 32 34 2c 20 20 20 38 36 2c 20  05,   24,   86, 
30d7b 20 20 38 37 2c 0a 20 2f 2a 20 20 20 32 33 30 20    87,. /*   230 
30d7c 2a 2f 20 20 20 20 39 34 2c 20 20 20 39 35 2c 20  */    94,   95, 
30d7d 20 20 39 34 2c 20 20 31 35 30 2c 20 20 32 31 34    94,  150,  214
30d7e 2c 20 20 32 31 35 2c 20 20 20 39 34 2c 20 20 20  ,  215,   94,   
30d7f 39 35 2c 20 20 20 32 35 2c 20 20 20 39 37 2c 0a  95,   25,   97,.
30d80 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20 20 31   /*   240 */   1
30d81 35 30 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20  50,   26,   27, 
30d82 20 20 32 33 2c 20 20 31 36 31 2c 20 20 31 36 32    23,  161,  162
30d83 2c 20 20 31 30 34 2c 20 20 31 30 35 2c 20 20 31  ,  104,  105,  1
30d84 36 35 2c 20 20 32 30 35 2c 0a 20 2f 2a 20 20 20  65,  205,. /*   
30d85 32 35 30 20 2a 2f 20 20 20 31 36 30 2c 20 20 32  250 */   160,  2
30d86 32 39 2c 20 20 20 34 39 2c 20 20 20 35 30 2c 20  29,   49,   50, 
30d87 20 20 39 36 2c 20 20 31 36 35 2c 20 20 32 33 34    96,  165,  234
30d88 2c 20 20 20 39 39 2c 20 20 31 30 30 2c 20 20 31  ,   99,  100,  1
30d89 30 31 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f  01,. /*   260 */
30d8a 20 20 20 31 34 32 2c 20 20 31 34 33 2c 20 20 31     142,  143,  1
30d8b 34 34 2c 20 20 31 34 35 2c 20 20 31 37 34 2c 20  44,  145,  174, 
30d8c 20 31 37 35 2c 20 20 20 35 37 2c 20 20 31 30 39   175,   57,  109
30d8d 2c 20 20 31 38 35 2c 20 20 31 38 36 2c 0a 20 2f  ,  185,  186,. /
30d8e 2a 20 20 20 32 37 30 20 2a 2f 20 20 20 31 36 30  *   270 */   160
30d8f 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20  ,   68,   69,   
30d90 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20  70,   71,   72, 
30d91 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35    73,   74,   75
30d92 2c 20 20 20 37 36 2c 0a 20 2f 2a 20 20 20 32 38  ,   76,. /*   28
30d93 30 20 2a 2f 20 20 20 20 37 37 2c 20 20 20 37 38  0 */    77,   78
30d94 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 31  ,   79,   80,  1
30d95 39 34 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20  94,   82,   83, 
30d96 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36    84,   85,   86
30d97 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20  ,. /*   290 */  
30d98 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39    87,   88,   89
30d99 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20  ,   90,   91,   
30d9a 39 32 2c 20 20 20 31 39 2c 20 20 31 35 30 2c 20  92,   19,  150, 
30d9b 20 31 35 30 2c 20 20 32 32 32 2c 0a 20 2f 2a 20   150,  222,. /* 
30d9c 20 20 33 30 30 20 2a 2f 20 20 20 20 32 33 2c 20    300 */    23, 
30d9d 20 32 32 34 2c 20 20 32 32 35 2c 20 20 20 38 38   224,  225,   88
30d9e 2c 20 20 31 39 34 2c 20 20 31 35 30 2c 20 20 32  ,  194,  150,  2
30d9f 31 36 2c 20 20 31 36 30 2c 20 20 20 39 35 2c 20  16,  160,   95, 
30da0 20 20 39 34 2c 0a 20 2f 2a 20 20 20 33 31 30 20    94,. /*   310 
30da1 2a 2f 20 20 20 20 39 35 2c 20 20 32 32 31 2c 20  */    95,  221, 
30da2 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 30 35   165,  165,  105
30da3 2c 20 20 31 30 36 2c 20 20 32 30 36 2c 20 20 32  ,  106,  206,  2
30da4 30 37 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 0a  07,  170,  171,.
30da5 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20 20 20 31   /*   320 */   1
30da6 36 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  65,  174,  175, 
30da7 20 31 35 30 2c 20 20 31 31 35 2c 20 20 31 34 38   150,  115,  148
30da8 2c 20 20 20 34 39 2c 20 20 20 35 30 2c 20 20 20  ,   49,   50,   
30da9 31 39 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20 20  19,  174,. /*   
30daa 33 33 30 20 2a 2f 20 20 20 31 37 35 2c 20 20 31  330 */   175,  1
30dab 31 31 2c 20 20 31 31 39 2c 20 20 31 31 33 2c 20  11,  119,  113, 
30dac 20 31 31 34 2c 20 20 20 32 32 2c 20 20 20 32 37   114,   22,   27
30dad 2c 20 20 31 32 38 2c 20 20 31 36 35 2c 20 20 20  ,  128,  165,   
30dae 32 36 2c 0a 20 2f 2a 20 20 20 33 34 30 20 2a 2f  26,. /*   340 */
30daf 20 20 20 20 32 37 2c 20 20 31 39 34 2c 20 20 32      27,  194,  2
30db0 33 37 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20  37,  170,  171, 
30db1 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30    68,   69,   70
30db2 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 0a 20 2f  ,   71,   72,. /
30db3 2a 20 20 20 33 35 30 20 2a 2f 20 20 20 20 37 33  *   350 */    73
30db4 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20  ,   74,   75,   
30db5 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20  76,   77,   78, 
30db6 20 20 37 39 2c 20 20 20 38 30 2c 20 20 31 35 30    79,   80,  150
30db7 2c 20 20 20 38 32 2c 0a 20 2f 2a 20 20 20 33 36  ,   82,. /*   36
30db8 30 20 2a 2f 20 20 20 20 38 33 2c 20 20 20 38 34  0 */    83,   84
30db9 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20  ,   85,   86,   
30dba 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20  87,   88,   89, 
30dbb 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32    90,   91,   92
30dbc 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a 2f 20 20  ,. /*   370 */  
30dbd 20 20 31 39 2c 20 20 32 31 36 2c 20 20 20 31 31    19,  216,   11
30dbe 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 32  ,  165,  150,  2
30dbf 33 36 2c 20 20 32 32 31 2c 20 20 20 32 32 2c 20  36,  221,   22, 
30dc0 20 31 35 30 2c 20 20 32 33 31 2c 0a 20 2f 2a 20   150,  231,. /* 
30dc1 20 20 33 38 30 20 2a 2f 20 20 20 32 33 32 2c 20    380 */   232, 
30dc2 20 20 32 36 2c 20 20 20 32 37 2c 20 20 20 32 35    26,   27,   25
30dc3 2c 20 20 31 34 36 2c 20 20 31 34 37 2c 20 20 31  ,  146,  147,  1
30dc4 39 36 2c 20 20 31 38 31 2c 20 20 31 38 32 2c 20  96,  181,  182, 
30dc5 20 31 36 35 2c 0a 20 2f 2a 20 20 20 33 39 30 20   165,. /*   390 
30dc6 2a 2f 20 20 20 31 35 32 2c 20 20 31 35 30 2c 20  */   152,  150, 
30dc7 20 31 35 34 2c 20 20 31 36 35 2c 20 20 31 38 38   154,  165,  188
30dc8 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20 20 31  ,   26,   27,  1
30dc9 31 35 2c 20 20 31 36 30 2c 20 20 31 35 30 2c 0a  15,  160,  150,.
30dca 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20 20 20 20   /*   400 */    
30dcb 34 39 2c 20 20 20 35 30 2c 20 20 31 37 34 2c 20  49,   50,  174, 
30dcc 20 31 37 35 2c 20 20 32 33 31 2c 20 20 32 33 32   175,  231,  232
30dcd 2c 20 20 31 36 35 2c 20 20 20 39 34 2c 20 20 20  ,  165,   94,   
30dce 39 35 2c 20 20 31 32 37 2c 0a 20 2f 2a 20 20 20  95,  127,. /*   
30dcf 34 31 30 20 2a 2f 20 20 20 31 36 31 2c 20 20 31  410 */   161,  1
30dd0 36 32 2c 20 20 31 36 36 2c 20 20 31 36 36 2c 20  62,  166,  166, 
30dd1 20 31 36 35 2c 20 20 31 37 34 2c 20 20 31 37 35   165,  174,  175
30dd2 2c 20 20 31 39 33 2c 20 20 20 35 37 2c 20 20 20  ,  193,   57,   
30dd3 36 38 2c 0a 20 2f 2a 20 20 20 34 32 30 20 2a 2f  68,. /*   420 */
30dd4 20 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20      69,   70,   
30dd5 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20  71,   72,   73, 
30dd6 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36    74,   75,   76
30dd7 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 0a 20 2f  ,   77,   78,. /
30dd8 2a 20 20 20 34 33 30 20 2a 2f 20 20 20 20 37 39  *   430 */    79
30dd9 2c 20 20 20 38 30 2c 20 20 31 39 34 2c 20 20 20  ,   80,  194,   
30dda 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20  82,   83,   84, 
30ddb 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37    85,   86,   87
30ddc 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20 34 34  ,   88,. /*   44
30ddd 30 20 2a 2f 20 20 20 20 38 39 2c 20 20 20 39 30  0 */    89,   90
30dde 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20  ,   91,   92,   
30ddf 31 39 2c 20 20 20 31 32 2c 20 20 31 37 33 2c 20  19,   12,  173, 
30de0 20 31 35 30 2c 20 20 32 32 30 2c 20 20 20 39 34   150,  220,   94
30de1 2c 0a 20 2f 2a 20 20 20 34 35 30 20 2a 2f 20 20  ,. /*   450 */  
30de2 20 20 39 35 2c 20 20 32 30 35 2c 20 20 20 32 36    95,  205,   26
30de3 2c 20 20 20 32 37 2c 20 20 31 38 31 2c 20 20 31  ,   27,  181,  1
30de4 35 38 2c 20 20 32 31 38 2c 20 20 32 31 36 2c 20  58,  218,  216, 
30de5 20 31 36 31 2c 20 20 31 36 32 2c 0a 20 2f 2a 20   161,  162,. /* 
30de6 20 20 34 36 30 20 2a 2f 20 20 20 20 32 35 2c 20    460 */    25, 
30de7 20 20 32 38 2c 20 20 31 36 35 2c 20 20 20 39 34    28,  165,   94
30de8 2c 20 20 20 39 35 2c 20 20 20 35 30 2c 20 20 31  ,   95,   50,  1
30de9 30 35 2c 20 20 31 30 36 2c 20 20 32 32 32 2c 20  05,  106,  222, 
30dea 20 32 34 35 2c 0a 20 2f 2a 20 20 20 34 37 30 20   245,. /*   470 
30deb 2a 2f 20 20 20 32 32 34 2c 20 20 32 32 35 2c 20  */   224,  225, 
30dec 20 32 30 36 2c 20 20 32 30 37 2c 20 20 20 34 39   206,  207,   49
30ded 2c 20 20 20 35 30 2c 20 20 31 31 35 2c 20 20 20  ,   50,  115,   
30dee 34 34 2c 20 20 31 38 32 2c 20 20 20 34 36 2c 0a  44,  182,   46,.
30def 20 2f 2a 20 20 20 34 38 30 20 2a 2f 20 20 20 31   /*   480 */   1
30df0 36 39 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20  69,  170,  171, 
30df1 20 31 32 35 2c 20 20 20 31 36 2c 20 20 32 34 37   125,   16,  247
30df2 2c 20 20 31 36 36 2c 20 20 31 33 36 2c 20 20 31  ,  166,  136,  1
30df3 31 39 2c 20 20 31 32 38 2c 0a 20 2f 2a 20 20 20  19,  128,. /*   
30df4 34 39 30 20 2a 2f 20 20 20 20 35 37 2c 20 20 31  490 */    57,  1
30df5 35 30 2c 20 20 31 36 30 2c 20 20 20 36 38 2c 20  50,  160,   68, 
30df6 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31    69,   70,   71
30df7 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20  ,   72,   73,   
30df8 37 34 2c 0a 20 2f 2a 20 20 20 35 30 30 20 2a 2f  74,. /*   500 */
30df9 20 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20      75,   76,   
30dfa 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20  77,   78,   79, 
30dfb 20 20 38 30 2c 20 20 31 36 35 2c 20 20 20 38 32    80,  165,   82
30dfc 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 0a 20 2f  ,   83,   84,. /
30dfd 2a 20 20 20 35 31 30 20 2a 2f 20 20 20 20 38 35  *   510 */    85
30dfe 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20  ,   86,   87,   
30dff 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 20  88,   89,   90, 
30e00 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20 31 39    91,   92,   19
30e01 2c 20 20 31 30 34 2c 0a 20 2f 2a 20 20 20 35 32  ,  104,. /*   52
30e02 30 20 2a 2f 20 20 20 20 39 34 2c 20 20 20 39 35  0 */    94,   95
30e03 2c 20 20 20 32 33 2c 20 20 20 32 31 2c 20 20 31  ,   23,   21,  1
30e04 30 35 2c 20 20 31 30 36 2c 20 20 31 39 34 2c 20  05,  106,  194, 
30e05 20 31 35 30 2c 20 20 20 36 30 2c 20 20 31 35 30   150,   60,  150
30e06 2c 0a 20 2f 2a 20 20 20 35 33 30 20 2a 2f 20 20  ,. /*   530 */  
30e07 20 20 36 32 2c 20 20 31 36 39 2c 20 20 31 37 30    62,  169,  170
30e08 2c 20 20 31 37 31 2c 20 20 32 32 32 2c 20 20 31  ,  171,  222,  1
30e09 35 30 2c 20 20 32 32 34 2c 20 20 32 32 35 2c 20  50,  224,  225, 
30e0a 20 31 30 33 2c 20 20 20 32 33 2c 0a 20 2f 2a 20   103,   23,. /* 
30e0b 20 20 35 34 30 20 2a 2f 20 20 20 31 35 30 2c 20    540 */   150, 
30e0c 20 20 32 35 2c 20 20 31 36 35 2c 20 20 32 33 32    25,  165,  232
30e0d 2c 20 20 31 36 35 2c 20 20 31 31 39 2c 20 20 31  ,  165,  119,  1
30e0e 36 30 2c 20 20 31 32 38 2c 20 20 20 34 39 2c 20  60,  128,   49, 
30e0f 20 20 35 30 2c 0a 20 2f 2a 20 20 20 35 35 30 20    50,. /*   550 
30e10 2a 2f 20 20 20 31 36 35 2c 20 20 31 37 34 2c 20  */   165,  174, 
30e11 20 31 37 35 2c 20 20 31 37 34 2c 20 20 31 37 35   175,  174,  175
30e12 2c 20 20 31 36 35 2c 20 20 32 34 34 2c 20 20 31  ,  165,  244,  1
30e13 36 35 2c 20 20 32 31 37 2c 20 20 31 37 34 2c 0a  65,  217,  174,.
30e14 20 2f 2a 20 20 20 35 36 30 20 2a 2f 20 20 20 31   /*   560 */   1
30e15 37 35 2c 20 20 31 33 36 2c 20 20 20 20 37 2c 20  75,  136,    7, 
30e16 20 20 20 38 2c 20 20 20 20 39 2c 20 20 20 36 33     8,    9,   63
30e17 2c 20 20 31 37 34 2c 20 20 20 36 38 2c 20 20 20  ,  174,   68,   
30e18 36 39 2c 20 20 20 37 30 2c 0a 20 2f 2a 20 20 20  69,   70,. /*   
30e19 35 37 30 20 2a 2f 20 20 20 20 37 31 2c 20 20 20  570 */    71,   
30e1a 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20  72,   73,   74, 
30e1b 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37    75,   76,   77
30e1c 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20  ,   78,   79,   
30e1d 38 30 2c 0a 20 2f 2a 20 20 20 35 38 30 20 2a 2f  80,. /*   580 */
30e1e 20 20 20 31 39 34 2c 20 20 20 38 32 2c 20 20 20     194,   82,   
30e1f 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20  83,   84,   85, 
30e20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20 38 38    86,   87,   88
30e21 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 0a 20 2f  ,   89,   90,. /
30e22 2a 20 20 20 35 39 30 20 2a 2f 20 20 20 20 39 31  *   590 */    91
30e23 2c 20 20 20 39 32 2c 20 20 20 31 39 2c 20 20 31  ,   92,   19,  1
30e24 35 30 2c 20 20 32 33 32 2c 20 20 31 35 30 2c 20  50,  232,  150, 
30e25 20 20 32 33 2c 20 20 32 32 30 2c 20 20 20 33 32    23,  220,   32
30e26 2c 20 20 20 31 39 2c 0a 20 2f 2a 20 20 20 36 30  ,   19,. /*   60
30e27 30 20 2a 2f 20 20 20 32 31 35 2c 20 20 31 35 30  0 */   215,  150
30e28 2c 20 20 31 30 30 2c 20 20 32 31 33 2c 20 20 32  ,  100,  213,  2
30e29 31 38 2c 20 20 32 32 36 2c 20 20 31 33 38 2c 20  18,  226,  138, 
30e2a 20 20 34 31 2c 20 20 31 36 35 2c 20 20 31 35 30    41,  165,  150
30e2b 2c 0a 20 2f 2a 20 20 20 36 31 30 20 2a 2f 20 20  ,. /*   610 */  
30e2c 20 31 36 35 2c 20 20 20 39 35 2c 20 20 31 35 30   165,   95,  150
30e2d 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31  ,  150,  150,  1
30e2e 37 37 2c 20 20 31 36 35 2c 20 20 31 37 34 2c 20  77,  165,  174, 
30e2f 20 31 37 35 2c 20 20 31 37 34 2c 0a 20 2f 2a 20   175,  174,. /* 
30e30 20 20 36 32 30 20 2a 2f 20 20 20 31 37 35 2c 20    620 */   175, 
30e31 20 32 33 35 2c 20 20 20 34 39 2c 20 20 20 35 30   235,   49,   50
30e32 2c 20 20 31 36 35 2c 20 20 31 37 37 2c 20 20 32  ,  165,  177,  2
30e33 34 30 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20  40,  165,  165, 
30e34 20 31 36 35 2c 0a 20 2f 2a 20 20 20 36 33 30 20   165,. /*   630 
30e35 2a 2f 20 20 20 20 35 30 2c 20 20 32 34 38 2c 20  */    50,  248, 
30e36 20 32 34 39 2c 20 20 31 37 34 2c 20 20 31 37 35   249,  174,  175
30e37 2c 20 20 31 31 39 2c 20 20 31 37 34 2c 20 20 31  ,  119,  174,  1
30e38 37 35 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 0a  75,  174,  175,.
30e39 20 2f 2a 20 20 20 36 34 30 20 2a 2f 20 20 20 31   /*   640 */   1
30e3a 33 38 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20  38,   68,   69, 
30e3b 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32    70,   71,   72
30e3c 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20  ,   73,   74,   
30e3d 37 35 2c 20 20 20 37 36 2c 0a 20 2f 2a 20 20 20  75,   76,. /*   
30e3e 36 35 30 20 2a 2f 20 20 20 20 37 37 2c 20 20 20  650 */    77,   
30e3f 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20  78,   79,   80, 
30e40 20 31 35 30 2c 20 20 20 38 32 2c 20 20 20 38 33   150,   82,   83
30e41 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20  ,   84,   85,   
30e42 38 36 2c 0a 20 2f 2a 20 20 20 36 36 30 20 2a 2f  86,. /*   660 */
30e43 20 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20      87,   88,   
30e44 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20  89,   90,   91, 
30e45 20 20 39 32 2c 20 20 20 31 39 2c 20 20 31 35 30    92,   19,  150
30e46 2c 20 20 20 33 30 2c 20 20 31 36 35 2c 0a 20 2f  ,   30,  165,. /
30e47 2a 20 20 20 36 37 30 20 2a 2f 20 20 20 20 38 38  *   670 */    88
30e48 2c 20 20 20 32 34 2c 20 20 20 33 34 2c 20 20 20  ,   24,   34,   
30e49 32 33 2c 20 20 20 31 32 2c 20 20 20 32 35 2c 20  23,   12,   25, 
30e4a 20 32 31 33 2c 20 20 20 32 32 2c 20 20 31 37 34   213,   22,  174
30e4b 2c 20 20 31 37 35 2c 0a 20 2f 2a 20 20 20 36 38  ,  175,. /*   68
30e4c 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 20 32 36  0 */   150,   26
30e4d 2c 20 20 31 36 35 2c 20 20 20 39 37 2c 20 20 31  ,  165,   97,  1
30e4e 30 34 2c 20 20 31 35 30 2c 20 20 20 34 38 2c 20  04,  150,   48, 
30e4f 20 31 35 30 2c 20 20 31 30 38 2c 20 20 31 36 36   150,  108,  166
30e50 2c 0a 20 2f 2a 20 20 20 36 39 30 20 2a 2f 20 20  ,. /*   690 */  
30e51 20 20 32 38 2c 20 20 31 37 34 2c 20 20 31 37 35    28,  174,  175
30e52 2c 20 20 20 39 38 2c 20 20 31 31 32 2c 20 20 31  ,   98,  112,  1
30e53 36 35 2c 20 20 20 34 39 2c 20 20 20 35 30 2c 20  65,   49,   50, 
30e54 20 31 31 36 2c 20 20 31 35 30 2c 0a 20 2f 2a 20   116,  150,. /* 
30e55 20 20 37 30 30 20 2a 2f 20 20 20 31 36 35 2c 20    700 */   165, 
30e56 20 31 31 35 2c 20 20 31 36 35 2c 20 20 31 30 38   115,  165,  108
30e57 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20  ,  174,  175,   
30e58 34 34 2c 20 20 31 37 38 2c 20 20 20 34 36 2c 20  44,  178,   46, 
30e59 20 31 37 34 2c 0a 20 2f 2a 20 20 20 37 31 30 20   174,. /*   710 
30e5a 2a 2f 20 20 20 31 37 35 2c 20 20 31 37 34 2c 20  */   175,  174, 
30e5b 20 31 37 35 2c 20 20 31 32 37 2c 20 20 31 36 35   175,  127,  165
30e5c 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20  ,   68,   69,   
30e5d 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 0a  70,   71,   72,.
30e5e 20 2f 2a 20 20 20 37 32 30 20 2a 2f 20 20 20 20   /*   720 */    
30e5f 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20  73,   74,   75, 
30e60 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38    76,   77,   78
30e61 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 31  ,   79,   80,  1
30e62 35 30 2c 20 20 20 38 32 2c 0a 20 2f 2a 20 20 20  50,   82,. /*   
30e63 37 33 30 20 2a 2f 20 20 20 20 38 33 2c 20 20 20  730 */    83,   
30e64 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20  84,   85,   86, 
30e65 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20 38 39    87,   88,   89
30e66 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20  ,   90,   91,   
30e67 39 32 2c 0a 20 2f 2a 20 20 20 37 34 30 20 2a 2f  92,. /*   740 */
30e68 20 20 20 20 31 39 2c 20 20 31 35 30 2c 20 20 31      19,  150,  1
30e69 39 33 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20  93,  165,  150, 
30e6a 20 31 31 31 2c 20 20 31 35 30 2c 20 20 31 31 33   111,  150,  113
30e6b 2c 20 20 31 31 34 2c 20 20 20 39 34 2c 0a 20 2f  ,  114,   94,. /
30e6c 2a 20 20 20 37 35 30 20 2a 2f 20 20 20 31 32 39  *   750 */   129
30e6d 2c 20 20 31 33 30 2c 20 20 31 37 34 2c 20 20 31  ,  130,  174,  1
30e6e 37 35 2c 20 20 31 31 32 2c 20 20 20 32 34 2c 20  75,  112,   24, 
30e6f 20 31 36 35 2c 20 20 20 32 36 2c 20 20 31 31 36   165,   26,  116
30e70 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 20 37 36  ,  165,. /*   76
30e71 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31 36 35  0 */   150,  165
30e72 2c 20 20 31 33 35 2c 20 20 31 35 30 2c 20 20 31  ,  135,  150,  1
30e73 33 37 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  37,  174,  175, 
30e74 20 20 32 33 2c 20 20 31 37 34 2c 20 20 31 37 35    23,  174,  175
30e75 2c 0a 20 2f 2a 20 20 20 37 37 30 20 2a 2f 20 20  ,. /*   770 */  
30e76 20 20 34 39 2c 20 20 20 35 30 2c 20 20 20 32 33    49,   50,   23
30e77 2c 20 20 20 35 32 2c 20 20 20 32 35 2c 20 20 31  ,   52,   25,  1
30e78 36 35 2c 20 20 20 32 32 2c 20 20 20 32 33 2c 20  65,   22,   23, 
30e79 20 31 36 35 2c 20 20 20 32 35 2c 0a 20 2f 2a 20   165,   25,. /* 
30e7a 20 20 37 38 30 20 2a 2f 20 20 20 20 20 30 2c 20    780 */     0, 
30e7b 20 20 20 31 2c 20 20 20 20 32 2c 20 20 31 38 37     1,    2,  187
30e7c 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31  ,  174,  175,  1
30e7d 39 36 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  96,  174,  175, 
30e7e 20 20 36 38 2c 0a 20 2f 2a 20 20 20 37 39 30 20    68,. /*   790 
30e7f 2a 2f 20 20 20 20 36 39 2c 20 20 20 37 30 2c 20  */    69,   70, 
30e80 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33    71,   72,   73
30e81 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 20 20 20  ,   74,   75,   
30e82 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 0a  76,   77,   78,.
30e83 20 2f 2a 20 20 20 38 30 30 20 2a 2f 20 20 20 20   /*   800 */    
30e84 37 39 2c 20 20 20 38 30 2c 20 20 31 35 30 2c 20  79,   80,  150, 
30e85 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34    82,   83,   84
30e86 2c 20 20 20 38 35 2c 20 20 20 38 36 2c 20 20 20  ,   85,   86,   
30e87 38 37 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20  87,   88,. /*   
30e88 38 31 30 20 2a 2f 20 20 20 20 38 39 2c 20 20 20  810 */    89,   
30e89 39 30 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20  90,   91,   92, 
30e8a 20 20 31 39 2c 20 20 31 35 30 2c 20 20 31 35 30    19,  150,  150
30e8b 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31  ,  165,  150,  1
30e8c 35 30 2c 0a 20 2f 2a 20 20 20 38 32 30 20 2a 2f  50,. /*   820 */
30e8d 20 20 20 31 35 30 2c 20 20 20 36 37 2c 20 20 31     150,   67,  1
30e8e 35 30 2c 20 20 31 35 30 2c 20 20 31 39 38 2c 20  50,  150,  198, 
30e8f 20 20 39 34 2c 20 20 31 37 34 2c 20 20 31 37 35    94,  174,  175
30e90 2c 20 20 31 30 35 2c 20 20 31 30 36 2c 0a 20 2f  ,  105,  106,. /
30e91 2a 20 20 20 38 33 30 20 2a 2f 20 20 20 31 36 35  *   830 */   165
30e92 2c 20 20 31 36 35 2c 20 20 20 32 33 2c 20 20 31  ,  165,   23,  1
30e93 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35 2c 20  65,  165,  165, 
30e94 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31 36 35   150,  165,  165
30e95 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20 20 38 34  ,  174,. /*   84
30e96 30 20 2a 2f 20 20 20 31 37 35 2c 20 20 20 32 33  0 */   175,   23
30e97 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20  ,  174,  175,   
30e98 34 39 2c 20 20 20 35 30 2c 20 20 31 37 34 2c 20  49,   50,  174, 
30e99 20 31 37 35 2c 20 20 31 36 36 2c 20 20 20 32 32   175,  166,   22
30e9a 2c 0a 20 2f 2a 20 20 20 38 35 30 20 2a 2f 20 20  ,. /*   850 */  
30e9b 20 20 32 33 2c 20 20 31 36 35 2c 20 20 20 32 35    23,  165,   25
30e9c 2c 20 20 20 31 36 2c 20 20 20 32 33 2c 20 20 31  ,   16,   23,  1
30e9d 31 31 2c 20 20 20 32 35 2c 20 20 31 31 33 2c 20  11,   25,  113, 
30e9e 20 31 31 34 2c 20 20 31 39 33 2c 0a 20 2f 2a 20   114,  193,. /* 
30e9f 20 20 38 36 30 20 2a 2f 20 20 20 31 37 34 2c 20    860 */   174, 
30ea0 20 31 37 35 2c 20 20 31 39 33 2c 20 20 20 36 38   175,  193,   68
30ea1 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20  ,   69,   70,   
30ea2 37 31 2c 20 20 20 37 32 2c 20 20 20 37 33 2c 20  71,   72,   73, 
30ea3 20 20 37 34 2c 0a 20 2f 2a 20 20 20 38 37 30 20    74,. /*   870 
30ea4 2a 2f 20 20 20 20 37 35 2c 20 20 20 37 36 2c 20  */    75,   76, 
30ea5 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39    77,   78,   79
30ea6 2c 20 20 20 38 30 2c 20 20 31 35 30 2c 20 20 20  ,   80,  150,   
30ea7 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 0a  82,   83,   84,.
30ea8 20 2f 2a 20 20 20 38 38 30 20 2a 2f 20 20 20 20   /*   880 */    
30ea9 38 35 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20  85,   86,   87, 
30eaa 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30    88,   89,   90
30eab 2c 20 20 20 39 31 2c 20 20 20 39 32 2c 20 20 20  ,   91,   92,   
30eac 31 39 2c 20 20 31 35 30 2c 0a 20 2f 2a 20 20 20  19,  150,. /*   
30ead 38 39 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31  890 */   150,  1
30eae 36 35 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20  65,  150,  150, 
30eaf 20 20 36 37 2c 20 20 20 39 31 2c 20 20 20 39 32    67,   91,   92
30eb0 2c 20 20 20 36 30 2c 20 20 31 39 39 2c 20 20 20  ,   60,  199,   
30eb1 36 32 2c 0a 20 2f 2a 20 20 20 39 30 30 20 2a 2f  62,. /*   900 */
30eb2 20 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31     174,  175,  1
30eb3 37 32 2c 20 20 31 37 33 2c 20 20 31 36 35 2c 20  72,  173,  165, 
30eb4 20 31 36 35 2c 20 20 20 33 36 2c 20 20 31 36 35   165,   36,  165
30eb5 2c 20 20 31 36 35 2c 20 20 20 32 35 2c 0a 20 2f  ,  165,   25,. /
30eb6 2a 20 20 20 39 31 30 20 2a 2f 20 20 20 31 35 30  *   910 */   150
30eb7 2c 20 20 31 38 31 2c 20 20 31 35 30 2c 20 20 31  ,  181,  150,  1
30eb8 37 34 2c 20 20 31 37 35 2c 20 20 32 30 39 2c 20  74,  175,  209, 
30eb9 20 31 37 34 2c 20 20 31 37 35 2c 20 20 20 34 39   174,  175,   49
30eba 2c 20 20 20 35 30 2c 0a 20 2f 2a 20 20 20 39 32  ,   50,. /*   92
30ebb 30 20 2a 2f 20 20 20 31 31 31 2c 20 20 20 35 31  0 */   111,   51
30ebc 2c 20 20 31 31 33 2c 20 20 31 31 34 2c 20 20 20  ,  113,  114,   
30ebd 32 33 2c 20 20 31 36 35 2c 20 20 20 32 35 2c 20  23,  165,   25, 
30ebe 20 31 36 35 2c 20 20 20 35 38 2c 20 20 31 31 31   165,   58,  111
30ebf 2c 0a 20 2f 2a 20 20 20 39 33 30 20 2a 2f 20 20  ,. /*   930 */  
30ec0 20 31 38 37 2c 20 20 31 31 33 2c 20 20 31 31 34   187,  113,  114
30ec1 2c 20 20 31 39 33 2c 20 20 31 37 34 2c 20 20 31  ,  193,  174,  1
30ec2 37 35 2c 20 20 32 30 39 2c 20 20 20 36 38 2c 20  75,  209,   68, 
30ec3 20 20 36 39 2c 20 20 20 37 30 2c 0a 20 2f 2a 20    69,   70,. /* 
30ec4 20 20 39 34 30 20 2a 2f 20 20 20 20 37 31 2c 20    940 */    71, 
30ec5 20 20 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34    72,   73,   74
30ec6 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20  ,   75,   76,   
30ec7 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20  77,   78,   79, 
30ec8 20 20 38 30 2c 0a 20 2f 2a 20 20 20 39 35 30 20    80,. /*   950 
30ec9 2a 2f 20 20 20 31 35 30 2c 20 20 20 38 32 2c 20  */   150,   82, 
30eca 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35    83,   84,   85
30ecb 2c 20 20 20 38 36 2c 20 20 20 38 37 2c 20 20 20  ,   86,   87,   
30ecc 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30 2c 0a  88,   89,   90,.
30ecd 20 2f 2a 20 20 20 39 36 30 20 2a 2f 20 20 20 20   /*   960 */    
30ece 39 31 2c 20 20 20 39 32 2c 20 20 20 31 39 2c 20  91,   92,   19, 
30ecf 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 36 35   150,  150,  165
30ed0 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 20  ,  150,  150,   
30ed1 20 31 2c 20 20 20 20 32 2c 0a 20 2f 2a 20 20 20   1,    2,. /*   
30ed2 39 37 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31  970 */   150,  1
30ed3 35 30 2c 20 20 31 35 30 2c 20 20 32 32 38 2c 20  50,  150,  228, 
30ed4 20 31 37 34 2c 20 20 31 37 35 2c 20 20 32 34 32   174,  175,  242
30ed5 2c 20 20 31 30 37 2c 20 20 31 36 35 2c 20 20 31  ,  107,  165,  1
30ed6 36 35 2c 0a 20 2f 2a 20 20 20 39 38 30 20 2a 2f  65,. /*   980 */
30ed7 20 20 20 31 35 30 2c 20 20 31 36 35 2c 20 20 31     150,  165,  1
30ed8 36 35 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20  65,  150,  150, 
30ed9 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35   165,  165,  165
30eda 2c 20 20 20 34 39 2c 20 20 20 35 30 2c 0a 20 2f  ,   49,   50,. /
30edb 2a 20 20 20 39 39 30 20 2a 2f 20 20 20 31 37 34  *   990 */   174
30edc 2c 20 20 31 37 35 2c 20 20 20 34 39 2c 20 20 20  ,  175,   49,   
30edd 35 30 2c 20 20 20 32 33 2c 20 20 31 36 35 2c 20  50,   23,  165, 
30ede 20 20 32 35 2c 20 20 32 33 33 2c 20 20 31 36 35    25,  233,  165
30edf 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 31 30 30  ,  165,. /*  100
30ee0 30 20 2a 2f 20 20 20 20 32 32 2c 20 20 20 32 33  0 */    22,   23
30ee1 2c 20 20 31 31 38 2c 20 20 32 34 31 2c 20 20 31  ,  118,  241,  1
30ee2 38 37 2c 20 20 31 39 30 2c 20 20 31 39 31 2c 20  87,  190,  191, 
30ee3 20 31 39 33 2c 20 20 31 37 34 2c 20 20 31 37 35   193,  174,  175
30ee4 2c 0a 20 2f 2a 20 20 31 30 31 30 20 2a 2f 20 20  ,. /*  1010 */  
30ee5 20 20 37 31 2c 20 20 20 37 32 2c 20 20 20 36 39    71,   72,   69
30ee6 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20  ,   70,   71,   
30ee7 37 32 2c 20 20 20 37 33 2c 20 20 20 37 34 2c 20  72,   73,   74, 
30ee8 20 20 37 35 2c 20 20 20 37 36 2c 0a 20 2f 2a 20    75,   76,. /* 
30ee9 20 31 30 32 30 20 2a 2f 20 20 20 20 37 37 2c 20   1020 */    77, 
30eea 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30    78,   79,   80
30eeb 2c 20 20 31 35 30 2c 20 20 20 38 32 2c 20 20 20  ,  150,   82,   
30eec 38 33 2c 20 20 20 38 34 2c 20 20 20 38 35 2c 20  83,   84,   85, 
30eed 20 20 38 36 2c 0a 20 2f 2a 20 20 31 30 33 30 20    86,. /*  1030 
30eee 2a 2f 20 20 20 20 38 37 2c 20 20 20 38 38 2c 20  */    87,   88, 
30eef 20 20 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31    89,   90,   91
30ef0 2c 20 20 20 39 32 2c 20 20 20 31 39 2c 20 20 20  ,   92,   19,   
30ef1 39 38 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 0a  98,  150,  165,.
30ef2 20 2f 2a 20 20 31 30 34 30 20 2a 2f 20 20 20 31   /*  1040 */   1
30ef3 35 30 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20  50,  150,  150, 
30ef4 20 31 36 30 2c 20 20 31 35 30 2c 20 20 20 36 37   160,  150,   67
30ef5 2c 20 20 32 31 33 2c 20 20 31 39 39 2c 20 20 31  ,  213,  199,  1
30ef6 37 34 2c 20 20 31 37 35 2c 0a 20 2f 2a 20 20 31  74,  175,. /*  1
30ef7 30 35 30 20 2a 2f 20 20 20 20 20 37 2c 20 20 20  050 */     7,   
30ef8 20 38 2c 20 20 32 33 33 2c 20 20 31 36 35 2c 20   8,  233,  165, 
30ef9 20 32 34 31 2c 20 20 31 36 35 2c 20 20 31 36 35   241,  165,  165
30efa 2c 20 20 31 36 35 2c 20 20 31 35 30 2c 20 20 31  ,  165,  150,  1
30efb 36 35 2c 0a 20 2f 2a 20 20 31 30 36 30 20 2a 2f  65,. /*  1060 */
30efc 20 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31     150,  150,  1
30efd 37 34 2c 20 20 31 37 35 2c 20 20 31 37 34 2c 20  74,  175,  174, 
30efe 20 31 37 35 2c 20 20 20 34 39 2c 20 20 20 35 30   175,   49,   50
30eff 2c 20 20 20 32 32 2c 20 20 20 39 37 2c 0a 20 2f  ,   22,   97,. /
30f00 2a 20 20 31 30 37 30 20 2a 2f 20 20 20 20 39 38  *  1070 */    98
30f01 2c 20 20 31 34 34 2c 20 20 31 34 35 2c 20 20 31  ,  144,  145,  1
30f02 36 35 2c 20 20 31 37 37 2c 20 20 31 36 35 2c 20  65,  177,  165, 
30f03 20 31 36 35 2c 20 20 31 39 34 2c 20 20 20 38 36   165,  194,   86
30f04 2c 20 20 20 38 37 2c 0a 20 2f 2a 20 20 31 30 38  ,   87,. /*  108
30f05 30 20 2a 2f 20 20 20 31 36 36 2c 20 20 31 38 37  0 */   166,  187
30f06 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31  ,  174,  175,  1
30f07 39 33 2c 20 20 31 37 34 2c 20 20 31 37 35 2c 20  93,  174,  175, 
30f08 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32    70,   71,   72
30f09 2c 0a 20 2f 2a 20 20 31 30 39 30 20 2a 2f 20 20  ,. /*  1090 */  
30f0a 20 20 37 33 2c 20 20 20 37 34 2c 20 20 20 37 35    73,   74,   75
30f0b 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20  ,   76,   77,   
30f0c 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20  78,   79,   80, 
30f0d 20 31 37 37 2c 20 20 20 38 32 2c 0a 20 2f 2a 20   177,   82,. /* 
30f0e 20 31 31 30 30 20 2a 2f 20 20 20 20 38 33 2c 20   1100 */    83, 
30f0f 20 20 38 34 2c 20 20 20 38 35 2c 20 20 20 38 36    84,   85,   86
30f10 2c 20 20 20 38 37 2c 20 20 20 38 38 2c 20 20 20  ,   87,   88,   
30f11 38 39 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20  89,   90,   91, 
30f12 20 20 39 32 2c 0a 20 2f 2a 20 20 31 31 31 30 20    92,. /*  1110 
30f13 2a 2f 20 20 20 20 31 39 2c 20 20 20 32 30 2c 20  */    19,   20, 
30f14 20 31 35 30 2c 20 20 20 32 32 2c 20 20 31 35 30   150,   22,  150
30f15 2c 20 20 31 39 30 2c 20 20 31 39 31 2c 20 20 20  ,  190,  191,   
30f16 32 36 2c 20 20 20 32 37 2c 20 20 32 30 35 2c 0a  26,   27,  205,.
30f17 20 2f 2a 20 20 31 31 32 30 20 2a 2f 20 20 20 31   /*  1120 */   1
30f18 35 30 2c 20 20 31 36 30 2c 20 20 20 37 36 2c 20  50,  160,   76, 
30f19 20 20 31 39 2c 20 20 20 32 30 2c 20 20 31 35 30    19,   20,  150
30f1a 2c 20 20 20 32 32 2c 20 20 31 36 35 2c 20 20 20  ,   22,  165,   
30f1b 33 37 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 31  37,  165,. /*  1
30f1c 31 33 30 20 2a 2f 20 20 20 20 32 36 2c 20 20 20  130 */    26,   
30f1d 32 37 2c 20 20 20 32 33 2c 20 20 32 34 31 2c 20  27,   23,  241, 
30f1e 20 20 32 35 2c 20 20 31 36 35 2c 20 20 31 37 34    25,  165,  174
30f1f 2c 20 20 31 37 35 2c 20 20 31 35 30 2c 20 20 31  ,  175,  150,  1
30f20 35 30 2c 0a 20 2f 2a 20 20 31 31 34 30 20 2a 2f  50,. /*  1140 */
30f21 20 20 20 31 36 35 2c 20 20 20 33 37 2c 20 20 31     165,   37,  1
30f22 36 30 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20  60,  150,  150, 
30f23 20 31 39 37 2c 20 20 31 35 30 2c 20 20 20 35 36   197,  150,   56
30f24 2c 20 20 20 31 39 2c 20 20 31 37 34 2c 0a 20 2f  ,   19,  174,. /
30f25 2a 20 20 31 31 35 30 20 2a 2f 20 20 20 31 37 35  *  1150 */   175
30f26 2c 20 20 31 35 30 2c 20 20 32 30 34 2c 20 20 31  ,  150,  204,  1
30f27 36 35 2c 20 20 31 36 35 2c 20 20 31 39 34 2c 20  65,  165,  194, 
30f28 20 20 32 37 2c 20 20 20 36 36 2c 20 20 31 36 35    27,   66,  165
30f29 2c 20 20 31 36 35 2c 0a 20 2f 2a 20 20 31 31 36  ,  165,. /*  116
30f2a 30 20 2a 2f 20 20 20 20 35 36 2c 20 20 31 36 35  0 */    56,  165
30f2b 2c 20 20 32 34 32 2c 20 20 31 37 34 2c 20 20 31  ,  242,  174,  1
30f2c 37 35 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20  75,  150,  165, 
30f2d 20 31 37 34 2c 20 20 31 37 35 2c 20 20 32 30 39   174,  175,  209
30f2e 2c 0a 20 2f 2a 20 20 31 31 37 30 20 2a 2f 20 20  ,. /*  1170 */  
30f2f 20 20 36 36 2c 20 20 31 36 30 2c 20 20 20 32 32    66,  160,   22
30f30 2c 20 20 31 35 30 2c 20 20 20 32 34 2c 20 20 31  ,  150,   24,  1
30f31 37 37 2c 20 20 31 39 34 2c 20 20 20 38 36 2c 20  77,  194,   86, 
30f32 20 20 38 37 2c 20 20 31 35 30 2c 0a 20 2f 2a 20    87,  150,. /* 
30f33 20 31 31 38 30 20 2a 2f 20 20 20 31 36 35 2c 20   1180 */   165, 
30f34 20 32 31 37 2c 20 20 32 35 30 2c 20 20 32 35 31   217,  250,  251
30f35 2c 20 20 20 39 33 2c 20 20 20 39 34 2c 20 20 20  ,   93,   94,   
30f36 39 35 2c 20 20 31 39 33 2c 20 20 31 36 35 2c 20  95,  193,  165, 
30f37 20 20 39 38 2c 0a 20 2f 2a 20 20 31 31 39 30 20    98,. /*  1190 
30f38 2a 2f 20 20 20 20 38 36 2c 20 20 20 38 37 2c 20  */    86,   87, 
30f39 20 20 38 38 2c 20 20 31 35 33 2c 20 20 31 36 35    88,  153,  165
30f3a 2c 20 20 20 31 38 2c 20 20 31 39 32 2c 20 20 20  ,   18,  192,   
30f3b 39 33 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 0a  93,   94,   95,.
30f3c 20 2f 2a 20 20 31 32 30 30 20 2a 2f 20 20 20 20   /*  1200 */    
30f3d 31 33 2c 20 20 31 35 31 2c 20 20 20 39 38 2c 20  13,  151,   98, 
30f3e 20 31 37 34 2c 20 20 31 37 35 2c 20 20 31 39 34   174,  175,  194
30f3f 2c 20 20 31 31 35 2c 20 20 20 32 32 2c 20 20 31  ,  115,   22,  1
30f40 35 37 2c 20 20 20 31 39 2c 0a 20 2f 2a 20 20 31  57,   19,. /*  1
30f41 32 31 30 20 2a 2f 20 20 20 20 32 30 2c 20 20 31  210 */    20,  1
30f42 35 30 2c 20 20 20 32 32 2c 20 20 31 35 30 2c 20  50,   22,  150, 
30f43 20 32 31 39 2c 20 20 31 35 30 2c 20 20 20 32 36   219,  150,   26
30f44 2c 20 20 20 32 37 2c 20 20 31 31 37 2c 20 20 31  ,   27,  117,  1
30f45 35 37 2c 0a 20 2f 2a 20 20 31 32 32 30 20 2a 2f  57,. /*  1220 */
30f46 20 20 20 31 32 39 2c 20 20 31 33 30 2c 20 20 31     129,  130,  1
30f47 33 31 2c 20 20 31 33 32 2c 20 20 31 33 33 2c 20  31,  132,  133, 
30f48 20 31 33 34 2c 20 20 31 36 35 2c 20 20 20 33 37   134,  165,   37
30f49 2c 20 20 31 36 35 2c 20 20 32 31 38 2c 0a 20 2f  ,  165,  218,. /
30f4a 2a 20 20 31 32 33 30 20 2a 2f 20 20 20 31 36 35  *  1230 */   165
30f4b 2c 20 20 31 35 37 2c 20 20 20 32 35 2c 20 20 31  ,  157,   25,  1
30f4c 32 39 2c 20 20 31 33 30 2c 20 20 31 33 31 2c 20  29,  130,  131, 
30f4d 20 31 33 32 2c 20 20 31 33 33 2c 20 20 31 33 34   132,  133,  134
30f4e 2c 20 20 31 37 34 2c 0a 20 2f 2a 20 20 31 32 34  ,  174,. /*  124
30f4f 30 20 2a 2f 20 20 20 31 37 35 2c 20 20 31 35 39  0 */   175,  159
30f50 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31  ,  150,  150,  1
30f51 35 30 2c 20 20 31 35 30 2c 20 20 20 35 36 2c 20  50,  150,   56, 
30f52 20 31 35 30 2c 20 20 31 35 30 2c 20 20 20 34 37   150,  150,   47
30f53 2c 0a 20 2f 2a 20 20 31 32 35 30 20 2a 2f 20 20  ,. /*  1250 */  
30f54 20 31 35 30 2c 20 20 32 34 30 2c 20 20 20 32 33   150,  240,   23
30f55 2c 20 20 31 38 34 2c 20 20 20 32 35 2c 20 20 31  ,  184,   25,  1
30f56 35 30 2c 20 20 20 36 36 2c 20 20 31 36 35 2c 20  50,   66,  165, 
30f57 20 31 36 35 2c 20 20 31 36 35 2c 0a 20 2f 2a 20   165,  165,. /* 
30f58 20 31 32 36 30 20 2a 2f 20 20 20 31 36 35 2c 20   1260 */   165, 
30f59 20 31 30 34 2c 20 20 31 36 35 2c 20 20 31 36 35   104,  165,  165
30f5a 2c 20 20 31 37 36 2c 20 20 31 36 35 2c 20 20 31  ,  176,  165,  1
30f5b 37 34 2c 20 20 31 37 35 2c 20 20 31 37 34 2c 20  74,  175,  174, 
30f5c 20 31 37 35 2c 0a 20 2f 2a 20 20 31 32 37 30 20   175,. /*  1270 
30f5d 2a 2f 20 20 20 31 36 35 2c 20 20 31 37 34 2c 20  */   165,  174, 
30f5e 20 31 37 35 2c 20 20 31 35 37 2c 20 20 31 37 34   175,  157,  174
30f5f 2c 20 20 31 37 35 2c 20 20 20 38 36 2c 20 20 20  ,  175,   86,   
30f60 38 37 2c 20 20 31 35 30 2c 20 20 31 37 34 2c 0a  87,  150,  174,.
30f61 20 2f 2a 20 20 31 32 38 30 20 2a 2f 20 20 20 31   /*  1280 */   1
30f62 37 35 2c 20 20 20 31 38 2c 20 20 31 37 36 2c 20  75,   18,  176, 
30f63 20 20 39 33 2c 20 20 20 39 34 2c 20 20 20 39 35    93,   94,   95
30f64 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 20  ,  150,  150,   
30f65 39 38 2c 20 20 20 39 32 2c 0a 20 2f 2a 20 20 31  98,   92,. /*  1
30f66 32 39 30 20 2a 2f 20 20 20 31 35 30 2c 20 20 31  290 */   150,  1
30f67 35 30 2c 20 20 31 35 30 2c 20 20 31 36 35 2c 20  50,  150,  165, 
30f68 20 31 35 30 2c 20 20 31 35 30 2c 20 20 20 31 39   150,  150,   19
30f69 2c 20 20 20 32 30 2c 20 20 20 32 33 2c 20 20 20  ,   20,   23,   
30f6a 32 32 2c 0a 20 2f 2a 20 20 31 33 30 30 20 2a 2f  22,. /*  1300 */
30f6b 20 20 20 20 32 35 2c 20 20 31 36 35 2c 20 20 31      25,  165,  1
30f6c 36 35 2c 20 20 20 32 36 2c 20 20 20 32 37 2c 20  65,   26,   27, 
30f6d 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35   165,  165,  165
30f6e 2c 20 20 32 33 30 2c 20 20 31 36 35 2c 0a 20 2f  ,  230,  165,. /
30f6f 2a 20 20 31 33 31 30 20 2a 2f 20 20 20 31 36 35  *  1310 */   165
30f70 2c 20 20 31 35 30 2c 20 20 31 35 30 2c 20 20 32  ,  150,  150,  2
30f71 33 30 2c 20 20 20 33 37 2c 20 20 31 35 30 2c 20  30,   37,  150, 
30f72 20 31 35 30 2c 20 20 31 35 30 2c 20 20 31 38 34   150,  150,  184
30f73 2c 20 20 31 32 39 2c 0a 20 2f 2a 20 20 31 33 32  ,  129,. /*  132
30f74 30 20 2a 2f 20 20 20 31 33 30 2c 20 20 31 33 31  0 */   130,  131
30f75 2c 20 20 31 33 32 2c 20 20 31 33 33 2c 20 20 31  ,  132,  133,  1
30f76 33 34 2c 20 20 31 37 36 2c 20 20 31 36 35 2c 20  34,  176,  165, 
30f77 20 31 36 35 2c 20 20 31 35 36 2c 20 20 20 20 35   165,  156,    5
30f78 2c 0a 20 2f 2a 20 20 31 33 33 30 20 2a 2f 20 20  ,. /*  1330 */  
30f79 20 31 36 35 2c 20 20 31 36 35 2c 20 20 31 36 35   165,  165,  165
30f7a 2c 20 20 20 35 36 2c 20 20 20 31 30 2c 20 20 20  ,   56,   10,   
30f7b 31 31 2c 20 20 20 31 32 2c 20 20 20 31 33 2c 20  11,   12,   13, 
30f7c 20 20 31 34 2c 20 20 31 35 30 2c 0a 20 2f 2a 20    14,  150,. /* 
30f7d 20 31 33 34 30 20 2a 2f 20 20 20 20 32 33 2c 20   1340 */    23, 
30f7e 20 20 31 37 2c 20 20 20 32 35 2c 20 20 20 36 36    17,   25,   66
30f7f 2c 20 20 20 32 33 2c 20 20 31 35 30 2c 20 20 20  ,   23,  150,   
30f80 32 35 2c 20 20 31 37 36 2c 20 20 31 35 37 2c 20  25,  176,  157, 
30f81 20 31 37 36 2c 0a 20 2f 2a 20 20 31 33 35 30 20   176,. /*  1350 
30f82 2a 2f 20 20 20 31 35 36 2c 20 20 20 34 30 2c 20  */   156,   40, 
30f83 20 20 32 32 2c 20 20 31 35 37 2c 20 20 31 36 35    22,  157,  165
30f84 2c 20 20 20 33 31 2c 20 20 32 34 33 2c 20 20 20  ,   31,  243,   
30f85 33 33 2c 20 20 31 37 39 2c 20 20 31 37 36 2c 0a  33,  179,  176,.
30f86 20 2f 2a 20 20 31 33 36 30 20 2a 2f 20 20 20 31   /*  1360 */   1
30f87 36 35 2c 20 20 31 30 33 2c 20 20 31 37 38 2c 20  65,  103,  178, 
30f88 20 20 38 36 2c 20 20 20 38 37 2c 20 20 31 35 36    86,   87,  156
30f89 2c 20 20 20 34 32 2c 20 20 31 37 39 2c 20 20 31  ,   42,  179,  1
30f8a 37 36 2c 20 20 20 20 36 2c 0a 20 2f 2a 20 20 31  76,    6,. /*  1
30f8b 33 37 30 20 2a 2f 20 20 20 20 39 33 2c 20 20 20  370 */    93,   
30f8c 39 34 2c 20 20 20 39 35 2c 20 20 31 34 39 2c 20  94,   95,  149, 
30f8d 20 31 35 37 2c 20 20 20 39 38 2c 20 20 31 34 39   157,   98,  149
30f8e 2c 20 20 31 34 39 2c 20 20 20 32 32 2c 20 20 20  ,  149,   22,   
30f8f 35 35 2c 0a 20 2f 2a 20 20 31 33 38 30 20 2a 2f  55,. /*  1380 */
30f90 20 20 20 20 36 38 2c 20 20 20 35 37 2c 20 20 20      68,   57,   
30f91 32 36 2c 20 20 20 32 37 2c 20 20 31 30 34 2c 20  26,   27,  104, 
30f92 20 20 36 31 2c 20 20 32 31 30 2c 20 20 31 38 39    61,  210,  189
30f93 2c 20 20 20 36 34 2c 20 20 31 32 30 2c 0a 20 2f  ,   64,  120,. /
30f94 2a 20 20 31 33 39 30 20 2a 2f 20 20 20 31 35 37  *  1390 */   157
30f95 2c 20 20 32 31 31 2c 20 20 20 33 38 2c 20 20 32  ,  211,   38,  2
30f96 34 36 2c 20 20 31 33 35 2c 20 20 32 32 37 2c 20  46,  135,  227, 
30f97 20 31 32 35 2c 20 20 31 38 39 2c 20 20 31 39 39   125,  189,  199
30f98 2c 20 20 31 35 37 2c 0a 20 2f 2a 20 20 31 34 30  ,  157,. /*  140
30f99 30 20 2a 2f 20 20 20 31 39 39 2c 20 20 32 31 39  0 */   199,  219
30f9a 2c 20 20 31 39 32 2c 20 20 31 39 32 2c 20 20 31  ,  192,  192,  1
30f9b 39 32 2c 20 20 31 38 39 2c 20 20 31 32 39 2c 20  92,  189,  129, 
30f9c 20 31 33 30 2c 20 20 31 33 31 2c 20 20 31 33 32   130,  131,  132
30f9d 2c 0a 20 2f 2a 20 20 31 34 31 30 20 2a 2f 20 20  ,. /*  1410 */  
30f9e 20 31 33 33 2c 20 20 31 33 34 2c 20 20 31 32 34   133,  134,  124
30f9f 2c 20 20 32 31 30 2c 20 20 31 34 39 2c 20 20 32  ,  210,  149,  2
30fa0 31 31 2c 20 20 32 31 30 2c 20 20 32 30 33 2c 20  11,  210,  203, 
30fa1 20 31 35 37 2c 20 20 31 32 31 2c 0a 20 2f 2a 20   157,  121,. /* 
30fa2 20 31 34 32 30 20 2a 2f 20 20 20 32 30 32 2c 20   1420 */   202, 
30fa3 20 32 30 31 2c 20 20 20 36 36 2c 20 20 31 32 33   201,   66,  123
30fa4 2c 20 20 32 31 31 2c 20 20 31 32 32 2c 20 20 32  ,  211,  122,  2
30fa5 30 30 2c 20 20 31 35 37 2c 20 20 31 35 36 2c 20  00,  157,  156, 
30fa6 20 31 30 35 2c 0a 20 2f 2a 20 20 31 34 33 30 20   105,. /*  1430 
30fa7 2a 2f 20 20 20 31 30 36 2c 20 20 31 39 39 2c 20  */   106,  199, 
30fa8 20 31 39 34 2c 20 20 31 35 31 2c 20 20 31 31 30   194,  151,  110
30fa9 2c 20 20 20 34 35 2c 20 20 31 33 35 2c 20 20 31  ,   45,  135,  1
30faa 38 30 2c 20 20 32 33 38 2c 20 20 31 31 35 2c 0a  80,  238,  115,.
30fab 20 2f 2a 20 20 31 34 34 30 20 2a 2f 20 20 20 31   /*  1440 */   1
30fac 38 30 2c 20 20 31 32 36 2c 20 20 20 38 36 2c 20  80,  126,   86, 
30fad 20 20 38 37 2c 20 20 32 33 39 2c 20 20 31 36 34    87,  239,  164
30fae 2c 20 20 20 20 31 2c 20 20 20 31 35 2c 20 20 20  ,    1,   15,   
30faf 32 33 2c 20 20 20 32 32 2c 0a 20 2f 2a 20 20 31  23,   22,. /*  1
30fb0 34 35 30 20 2a 2f 20 20 20 20 39 34 2c 20 20 31  450 */    94,  1
30fb1 31 37 2c 20 20 31 32 38 2c 20 20 31 31 37 2c 20  17,  128,  117, 
30fb2 20 20 39 38 2c 20 20 31 31 37 2c 20 20 31 31 37    98,  117,  117
30fb3 2c 20 20 31 31 37 2c 20 20 32 34 33 2c 20 20 31  ,  117,  243,  1
30fb4 31 32 2c 0a 20 2f 2a 20 20 31 34 36 30 20 2a 2f  12,. /*  1460 */
30fb5 20 20 20 20 32 32 2c 20 20 20 31 31 2c 20 20 20      22,   11,   
30fb6 32 33 2c 20 20 31 33 39 2c 20 20 20 32 33 2c 20  23,  139,   23, 
30fb7 20 20 32 32 2c 20 20 20 32 32 2c 20 20 20 32 35    22,   22,   25
30fb8 2c 20 20 32 34 39 2c 20 20 20 32 33 2c 0a 20 2f  ,  249,   23,. /
30fb9 2a 20 20 31 34 37 30 20 2a 2f 20 20 20 20 32 33  *  1470 */    23
30fba 2c 20 20 20 32 33 2c 20 20 20 33 35 2c 20 20 32  ,   23,   35,  2
30fbb 35 32 2c 20 20 32 35 32 2c 20 20 20 33 35 2c 20  52,  252,   35, 
30fbc 20 20 32 35 2c 20 20 20 32 35 2c 20 20 31 31 39    25,   25,  119
30fbd 2c 20 20 20 32 32 2c 0a 20 2f 2a 20 20 31 34 38  ,   22,. /*  148
30fbe 30 20 2a 2f 20 20 20 20 32 37 2c 20 20 31 31 37  0 */    27,  117
30fbf 2c 20 20 20 32 33 2c 20 20 20 32 33 2c 20 20 20  ,   23,   23,   
30fc0 33 35 2c 20 20 31 32 39 2c 20 20 31 33 30 2c 20  35,  129,  130, 
30fc1 20 31 33 31 2c 20 20 20 35 32 2c 20 20 20 32 32   131,   52,   22
30fc2 2c 0a 20 2f 2a 20 20 31 34 39 30 20 2a 2f 20 20  ,. /*  1490 */  
30fc3 20 20 32 39 2c 20 20 20 32 32 2c 20 20 20 32 32    29,   22,   22
30fc4 2c 20 20 20 33 39 2c 20 20 20 32 33 2c 20 20 20  ,   39,   23,   
30fc5 32 32 2c 20 20 20 32 32 2c 20 20 31 30 32 2c 20  22,   22,  102, 
30fc6 20 20 31 39 2c 20 20 31 30 38 2c 0a 20 2f 2a 20    19,  108,. /* 
30fc7 20 31 35 30 30 20 2a 2f 20 20 20 20 32 30 2c 20   1500 */    20, 
30fc8 20 20 32 34 2c 20 20 31 30 34 2c 20 20 20 32 35    24,  104,   25
30fc9 2c 20 20 31 33 38 2c 20 20 20 34 33 2c 20 20 31  ,  138,   43,  1
30fca 30 34 2c 20 20 20 35 32 2c 20 20 20 35 32 2c 20  04,   52,   52, 
30fcb 20 20 32 32 2c 0a 20 2f 2a 20 20 31 35 31 30 20    22,. /*  1510 
30fcc 2a 2f 20 20 20 20 20 35 2c 20 20 20 20 31 2c 20  */     5,    1, 
30fcd 20 20 32 37 2c 20 20 31 31 37 2c 20 20 31 30 37    27,  117,  107
30fce 2c 20 20 31 32 36 2c 20 20 20 35 33 2c 20 20 20  ,  126,   53,   
30fcf 35 39 2c 20 20 20 35 33 2c 20 20 20 32 32 2c 0a  59,   53,   22,.
30fd0 20 2f 2a 20 20 31 35 32 30 20 2a 2f 20 20 20 20   /*  1520 */    
30fd1 20 31 2c 20 20 31 31 38 2c 20 20 20 31 36 2c 20   1,  118,   16, 
30fd2 20 20 32 30 2c 20 20 31 32 30 2c 20 20 31 30 37    20,  120,  107
30fd3 2c 20 20 31 31 38 2c 20 20 31 32 37 2c 20 20 20  ,  118,  127,   
30fd4 31 36 2c 20 20 20 32 32 2c 0a 20 2f 2a 20 20 31  16,   22,. /*  1
30fd5 35 33 30 20 2a 2f 20 20 20 20 31 35 2c 20 20 20  530 */    15,   
30fd6 32 33 2c 20 20 20 32 33 2c 20 20 20 36 35 2c 20  23,   23,   65, 
30fd7 20 31 34 30 2c 20 20 20 32 32 2c 20 20 20 20 33   140,   22,    3
30fd8 2c 20 20 32 35 33 2c 20 20 20 20 34 2c 20 20 32  ,  253,    4,  2
30fd9 35 33 2c 0a 20 2f 2a 20 20 31 35 34 30 20 2a 2f  53,. /*  1540 */
30fda 20 20 20 20 37 36 2c 20 20 32 35 33 2c 20 20 20      76,  253,   
30fdb 37 36 2c 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 59  76,.};.#define Y
30fdc 59 5f 53 48 49 46 54 5f 55 53 45 5f 44 46 4c 54  Y_SHIFT_USE_DFLT
30fdd 20 28 2d 31 30 34 29 0a 23 64 65 66 69 6e 65 20   (-104).#define 
30fde 59 59 5f 53 48 49 46 54 5f 4d 41 58 20 34 31 37  YY_SHIFT_MAX 417
30fdf 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 68  .static const sh
30fe0 6f 72 74 20 79 79 5f 73 68 69 66 74 5f 6f 66 73  ort yy_shift_ofs
30fe1 74 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20  t[] = {. /*     
30fe2 30 20 2a 2f 20 20 20 39 36 37 2c 20 31 30 39 31  0 */   967, 1091
30fe3 2c 20 31 33 32 34 2c 20 31 30 39 31 2c 20 31 32  , 1324, 1091, 12
30fe4 37 37 2c 20 31 32 37 37 2c 20 20 31 34 32 2c 20  77, 1277,  142, 
30fe5 20 31 34 32 2c 20 20 20 20 31 2c 20 20 2d 31 39   142,    1,  -19
30fe6 2c 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20  ,. /*    10 */  
30fe7 31 32 37 37 2c 20 31 32 37 37 2c 20 31 32 37 37  1277, 1277, 1277
30fe8 2c 20 31 32 37 37 2c 20 31 32 37 37 2c 20 20 32  , 1277, 1277,  2
30fe9 30 39 2c 20 20 31 33 36 2c 20 20 37 32 31 2c 20  09,  136,  721, 
30fea 31 31 30 34 2c 20 31 32 37 37 2c 0a 20 2f 2a 20  1104, 1277,. /* 
30feb 20 20 20 32 30 20 2a 2f 20 20 31 32 37 37 2c 20     20 */  1277, 
30fec 31 32 37 37 2c 20 31 32 37 37 2c 20 31 32 37 37  1277, 1277, 1277
30fed 2c 20 31 32 37 37 2c 20 31 32 37 37 2c 20 31 32  , 1277, 1277, 12
30fee 37 37 2c 20 31 32 37 37 2c 20 31 32 37 37 2c 20  77, 1277, 1277, 
30fef 31 32 37 37 2c 0a 20 2f 2a 20 20 20 20 33 30 20  1277,. /*    30 
30ff0 2a 2f 20 20 31 32 37 37 2c 20 31 32 37 37 2c 20  */  1277, 1277, 
30ff1 31 32 37 37 2c 20 31 32 37 37 2c 20 31 32 37 37  1277, 1277, 1277
30ff2 2c 20 31 32 37 37 2c 20 31 32 37 37 2c 20 31 32  , 1277, 1277, 12
30ff3 37 37 2c 20 31 32 37 37 2c 20 31 32 37 37 2c 0a  77, 1277, 1277,.
30ff4 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20 31 32   /*    40 */  12
30ff5 37 37 2c 20 31 32 37 37 2c 20 31 32 37 37 2c 20  77, 1277, 1277, 
30ff6 31 32 37 37 2c 20 31 32 37 37 2c 20 31 32 37 37  1277, 1277, 1277
30ff7 2c 20 31 32 37 37 2c 20 31 32 37 37 2c 20 31 32  , 1277, 1277, 12
30ff8 37 37 2c 20 31 32 37 37 2c 0a 20 2f 2a 20 20 20  77, 1277,. /*   
30ff9 20 35 30 20 2a 2f 20 20 31 32 37 37 2c 20 31 32   50 */  1277, 12
30ffa 37 37 2c 20 31 32 37 37 2c 20 31 32 37 37 2c 20  77, 1277, 1277, 
30ffb 31 32 37 37 2c 20 31 31 39 30 2c 20 31 32 37 37  1277, 1190, 1277
30ffc 2c 20 31 32 37 37 2c 20 31 32 37 37 2c 20 31 32  , 1277, 1277, 12
30ffd 37 37 2c 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f  77,. /*    60 */
30ffe 20 20 31 32 37 37 2c 20 20 2d 34 39 2c 20 20 33    1277,  -49,  3
30fff 36 31 2c 20 20 31 33 36 2c 20 20 31 33 36 2c 20  61,  136,  136, 
31000 20 39 39 32 2c 20 20 31 33 38 2c 20 20 39 39 32   992,  138,  992
31001 2c 20 2d 31 30 33 2c 20 20 20 35 35 2c 0a 20 2f  , -103,   55,. /
31002 2a 20 20 20 20 37 30 20 2a 2f 20 20 20 31 32 39  *    70 */   129
31003 2c 20 20 34 39 39 2c 20 20 33 35 31 2c 20 20 36  ,  499,  351,  6
31004 34 37 2c 20 20 32 30 33 2c 20 20 32 37 37 2c 20  47,  203,  277, 
31005 20 34 32 35 2c 20 20 35 37 33 2c 20 20 38 36 39   425,  573,  869
31006 2c 20 20 37 39 35 2c 0a 20 2f 2a 20 20 20 20 38  ,  795,. /*    8
31007 30 20 2a 2f 20 20 20 37 39 35 2c 20 20 37 39 35  0 */   795,  795
31008 2c 20 20 37 39 35 2c 20 20 37 39 35 2c 20 20 37  ,  795,  795,  7
31009 39 35 2c 20 20 37 39 35 2c 20 20 37 39 35 2c 20  95,  795,  795, 
3100a 20 37 39 35 2c 20 20 37 39 35 2c 20 20 37 39 35   795,  795,  795
3100b 2c 0a 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20  ,. /*    90 */  
3100c 20 37 39 35 2c 20 20 37 39 35 2c 20 20 37 39 35   795,  795,  795
3100d 2c 20 20 37 39 35 2c 20 20 37 39 35 2c 20 20 37  ,  795,  795,  7
3100e 39 35 2c 20 20 37 39 35 2c 20 20 37 39 35 2c 20  95,  795,  795, 
3100f 20 39 34 33 2c 20 31 30 31 37 2c 0a 20 2f 2a 20   943, 1017,. /* 
31010 20 20 31 30 30 20 2a 2f 20 20 31 30 31 37 2c 20    100 */  1017, 
31011 20 2d 36 39 2c 20 20 2d 36 39 2c 20 20 2d 36 39   -69,  -69,  -69
31012 2c 20 20 2d 36 39 2c 20 20 20 2d 31 2c 20 20 20  ,  -69,   -1,   
31013 2d 31 2c 20 20 20 35 37 2c 20 20 20 32 38 2c 20  -1,   57,   28, 
31014 20 2d 34 34 2c 0a 20 2f 2a 20 20 20 31 31 30 20   -44,. /*   110 
31015 2a 2f 20 20 20 31 33 36 2c 20 20 31 33 36 2c 20  */   136,  136, 
31016 20 35 30 32 2c 20 20 31 33 36 2c 20 20 31 33 36   502,  136,  136
31017 2c 20 20 31 33 36 2c 20 20 31 33 36 2c 20 20 31  ,  136,  136,  1
31018 33 36 2c 20 20 31 33 36 2c 20 20 31 33 36 2c 0a  36,  136,  136,.
31019 20 2f 2a 20 20 20 31 32 30 20 2a 2f 20 20 20 31   /*   120 */   1
3101a 33 36 2c 20 20 31 33 36 2c 20 20 31 33 36 2c 20  36,  136,  136, 
3101b 20 31 33 36 2c 20 20 31 33 36 2c 20 20 31 33 36   136,  136,  136
3101c 2c 20 20 31 33 36 2c 20 20 31 33 36 2c 20 20 31  ,  136,  136,  1
3101d 33 36 2c 20 20 35 38 30 2c 0a 20 2f 2a 20 20 20  36,  580,. /*   
3101e 31 33 30 20 2a 2f 20 20 20 31 33 36 2c 20 20 31  130 */   136,  1
3101f 33 36 2c 20 20 31 33 36 2c 20 20 31 33 38 2c 20  36,  136,  138, 
31020 20 38 30 34 2c 20 20 2d 36 33 2c 20 2d 31 30 34   804,  -63, -104
31021 2c 20 2d 31 30 34 2c 20 2d 31 30 34 2c 20 31 33  , -104, -104, 13
31022 35 36 2c 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f  56,. /*   140 */
31023 20 20 20 20 37 34 2c 20 20 34 32 36 2c 20 20 34      74,  426,  4
31024 33 33 2c 20 20 33 35 35 2c 20 20 32 31 35 2c 20  33,  355,  215, 
31025 20 34 33 33 2c 20 20 33 31 33 2c 20 20 33 36 39   433,  313,  369
31026 2c 20 20 31 33 36 2c 20 20 31 33 36 2c 0a 20 2f  ,  136,  136,. /
31027 2a 20 20 20 31 35 30 20 2a 2f 20 20 20 31 33 36  *   150 */   136
31028 2c 20 20 31 33 36 2c 20 20 31 33 36 2c 20 20 31  ,  136,  136,  1
31029 33 36 2c 20 20 31 33 36 2c 20 20 31 33 36 2c 20  36,  136,  136, 
3102a 20 31 33 36 2c 20 20 37 35 34 2c 20 20 31 33 36   136,  754,  136
3102b 2c 20 20 31 33 36 2c 0a 20 2f 2a 20 20 20 31 36  ,  136,. /*   16
3102c 30 20 2a 2f 20 20 20 31 33 36 2c 20 20 31 33 36  0 */   136,  136
3102d 2c 20 20 31 33 36 2c 20 20 31 33 36 2c 20 20 31  ,  136,  136,  1
3102e 33 36 2c 20 20 31 33 36 2c 20 20 31 33 36 2c 20  36,  136,  136, 
3102f 20 38 32 37 2c 20 20 31 33 36 2c 20 20 31 33 36   827,  136,  136
31030 2c 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20  ,. /*   170 */  
31031 20 31 33 36 2c 20 20 31 33 36 2c 20 20 31 33 36   136,  136,  136
31032 2c 20 20 31 33 36 2c 20 20 31 33 36 2c 20 20 31  ,  136,  136,  1
31033 33 36 2c 20 20 31 33 36 2c 20 20 31 33 36 2c 20  36,  136,  136, 
31034 20 31 33 36 2c 20 20 31 33 36 2c 0a 20 2f 2a 20   136,  136,. /* 
31035 20 20 31 38 30 20 2a 2f 20 20 20 31 33 36 2c 20    180 */   136, 
31036 20 31 33 36 2c 20 20 35 38 36 2c 20 20 37 33 31   136,  586,  731
31037 2c 20 20 37 33 31 2c 20 20 37 33 31 2c 20 20 37  ,  731,  731,  7
31038 38 30 2c 20 20 32 38 32 2c 20 2d 31 30 33 2c 20  80,  282, -103, 
31039 2d 31 30 33 2c 0a 20 2f 2a 20 20 20 31 39 30 20  -103,. /*   190 
3103a 2a 2f 20 20 2d 31 30 33 2c 20 2d 31 30 34 2c 20  */  -103, -104, 
3103b 2d 31 30 34 2c 20 2d 31 30 34 2c 20 20 20 39 34  -104, -104,   94
3103c 2c 20 20 31 35 38 2c 20 20 20 39 34 2c 20 20 20  ,  158,   94,   
3103d 31 32 2c 20 20 32 32 30 2c 20 20 36 36 32 2c 0a  12,  220,  662,.
3103e 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20 20 35   /*   200 */   5
3103f 31 36 2c 20 20 37 34 34 2c 20 20 38 37 30 2c 20  16,  744,  870, 
31040 20 38 30 39 2c 20 20 38 37 30 2c 20 20 38 31 38   809,  870,  818
31041 2c 20 20 35 35 35 2c 20 20 36 33 34 2c 20 20 34  ,  555,  634,  4
31042 31 39 2c 20 20 36 35 35 2c 0a 20 2f 2a 20 20 20  19,  655,. /*   
31043 32 31 30 20 2a 2f 20 20 20 36 33 34 2c 20 20 36  210 */   634,  6
31044 33 34 2c 20 20 36 33 34 2c 20 20 36 33 38 2c 20  34,  634,  638, 
31045 20 36 33 34 2c 20 20 32 31 33 2c 20 20 35 38 32   634,  213,  582
31046 2c 20 20 36 33 34 2c 20 20 34 31 35 2c 20 20 35  ,  634,  415,  5
31047 36 36 2c 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f  66,. /*   220 */
31048 20 20 20 31 33 38 2c 20 20 35 36 36 2c 20 20 31     138,  566,  1
31049 33 38 2c 20 20 34 33 35 2c 20 20 36 34 32 2c 20  38,  435,  642, 
3104a 20 38 38 34 2c 20 20 31 33 38 2c 20 20 33 35 38   884,  138,  358
3104b 2c 20 20 33 30 39 2c 20 20 33 30 39 2c 0a 20 2f  ,  309,  309,. /
3104c 2a 20 20 20 32 33 30 20 2a 2f 20 20 20 36 32 31  *   230 */   621
3104d 2c 20 20 36 32 31 2c 20 31 31 32 39 2c 20 20 36  ,  621, 1129,  6
3104e 32 37 2c 20 20 35 36 36 2c 20 20 35 36 36 2c 20  27,  566,  566, 
3104f 20 33 35 38 2c 20 31 31 32 39 2c 20 20 33 30 39   358, 1129,  309
31050 2c 20 20 35 36 36 2c 0a 20 2f 2a 20 20 20 32 34  ,  566,. /*   24
31051 30 20 2a 2f 20 20 31 31 35 30 2c 20 31 31 37 37  0 */  1150, 1177
31052 2c 20 31 31 38 37 2c 20 31 31 38 35 2c 20 31 31  , 1187, 1185, 11
31053 30 31 2c 20 31 31 30 31 2c 20 31 31 30 31 2c 20  01, 1101, 1101, 
31054 31 32 30 37 2c 20 31 32 30 32 2c 20 31 31 35 37  1207, 1202, 1157
31055 2c 0a 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20  ,. /*   250 */  
31056 31 31 35 37 2c 20 31 31 30 31 2c 20 31 31 39 37  1157, 1101, 1197
31057 2c 20 31 31 35 37 2c 20 31 32 36 33 2c 20 31 31  , 1157, 1263, 11
31058 39 37 2c 20 31 31 35 37 2c 20 31 31 30 31 2c 20  97, 1157, 1101, 
31059 31 32 30 32 2c 20 31 31 35 37 2c 0a 20 2f 2a 20  1202, 1157,. /* 
3105a 20 20 32 36 30 20 2a 2f 20 20 31 32 36 33 2c 20    260 */  1263, 
3105b 31 31 30 31 2c 20 31 33 31 31 2c 20 31 33 33 30  1101, 1311, 1330
3105c 2c 20 31 31 35 37 2c 20 31 32 35 38 2c 20 31 32  , 1157, 1258, 12
3105d 36 33 2c 20 31 31 35 37 2c 20 31 33 33 30 2c 20  63, 1157, 1330, 
3105e 31 33 36 33 2c 0a 20 2f 2a 20 20 20 32 37 30 20  1363,. /*   270 
3105f 2a 2f 20 20 31 31 30 31 2c 20 31 33 36 33 2c 20  */  1101, 1363, 
31060 31 33 36 33 2c 20 31 33 31 32 2c 20 31 32 38 30  1363, 1312, 1280
31061 2c 20 31 32 36 39 2c 20 31 31 30 31 2c 20 31 33  , 1269, 1101, 13
31062 35 34 2c 20 31 32 35 39 2c 20 31 32 37 31 2c 0a  54, 1259, 1271,.
31063 20 2f 2a 20 20 20 32 38 30 20 2a 2f 20 20 31 33   /*   280 */  13
31064 31 32 2c 20 31 31 30 31 2c 20 31 31 37 37 2c 20  12, 1101, 1177, 
31065 31 31 37 37 2c 20 31 31 37 37 2c 20 31 32 37 31  1177, 1177, 1271
31066 2c 20 31 33 31 32 2c 20 31 32 38 30 2c 20 31 32  , 1312, 1280, 12
31067 36 39 2c 20 31 31 38 35 2c 0a 20 2f 2a 20 20 20  69, 1185,. /*   
31068 32 39 30 20 2a 2f 20 20 31 32 38 30 2c 20 31 32  290 */  1280, 12
31069 36 39 2c 20 31 33 36 33 2c 20 31 32 38 38 2c 20  69, 1363, 1288, 
3106a 31 31 30 31 2c 20 31 32 39 38 2c 20 31 33 30 30  1101, 1298, 1300
3106b 2c 20 31 33 30 33 2c 20 31 32 36 33 2c 20 31 32  , 1303, 1263, 12
3106c 37 31 2c 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f  71,. /*   300 */
3106d 20 20 31 31 30 31 2c 20 2d 31 30 33 2c 20 31 31    1101, -103, 11
3106e 38 37 2c 20 31 33 30 31 2c 20 31 33 39 30 2c 20  87, 1301, 1390, 
3106f 31 33 31 31 2c 20 2d 31 30 34 2c 20 2d 31 30 34  1311, -104, -104
31070 2c 20 2d 31 30 34 2c 20 2d 31 30 34 2c 0a 20 2f  , -104, -104,. /
31071 2a 20 20 20 33 31 30 20 2a 2f 20 20 2d 31 30 34  *   310 */  -104
31072 2c 20 2d 31 30 34 2c 20 20 39 33 39 2c 20 20 34  , -104,  939,  4
31073 36 38 2c 20 20 39 37 38 2c 20 20 20 38 34 2c 20  68,  978,   84, 
31074 20 38 33 37 2c 20 31 33 31 37 2c 20 20 36 35 30   837, 1317,  650
31075 2c 20 20 38 33 31 2c 0a 20 2f 2a 20 20 20 33 32  ,  831,. /*   32
31076 30 20 2a 2f 20 20 20 39 37 31 2c 20 20 35 39 35  0 */   971,  595
31077 2c 20 31 32 37 35 2c 20 31 33 32 31 2c 20 20 37  , 1275, 1321,  7
31078 32 33 2c 20 31 30 34 36 2c 20 20 39 30 31 2c 20  23, 1046,  901, 
31079 31 31 30 39 2c 20 20 37 34 39 2c 20 31 32 32 39  1109,  749, 1229
3107a 2c 0a 20 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20  ,. /*   330 */  
3107b 20 39 37 32 2c 20 31 30 34 33 2c 20 31 33 31 35   972, 1043, 1315
3107c 2c 20 31 34 34 35 2c 20 31 34 33 32 2c 20 31 34  , 1445, 1432, 14
3107d 32 35 2c 20 31 34 32 37 2c 20 31 33 33 34 2c 20  25, 1427, 1334, 
3107e 31 33 33 36 2c 20 31 33 33 38 2c 0a 20 2f 2a 20  1336, 1338,. /* 
3107f 20 20 33 34 30 20 2a 2f 20 20 31 33 33 39 2c 20    340 */  1339, 
31080 31 33 34 30 2c 20 31 33 34 37 2c 20 31 34 33 38  1340, 1347, 1438
31081 2c 20 31 34 33 39 2c 20 31 34 34 31 2c 20 31 34  , 1439, 1441, 14
31082 34 33 2c 20 31 34 35 30 2c 20 31 34 34 34 2c 20  43, 1450, 1444, 
31083 31 34 34 36 2c 0a 20 2f 2a 20 20 20 33 35 30 20  1446,. /*   350 
31084 2a 2f 20 20 31 34 34 32 2c 20 31 34 34 37 2c 20  */  1442, 1447, 
31085 31 34 34 38 2c 20 31 34 35 31 2c 20 31 34 33 37  1448, 1451, 1437
31086 2c 20 31 34 35 32 2c 20 31 34 34 30 2c 20 31 34  , 1452, 1440, 14
31087 35 31 2c 20 31 33 35 39 2c 20 31 34 35 37 2c 0a  51, 1359, 1457,.
31088 20 2f 2a 20 20 20 33 36 30 20 2a 2f 20 20 31 34   /*   360 */  14
31089 34 39 2c 20 31 34 35 33 2c 20 31 33 36 34 2c 20  49, 1453, 1364, 
3108a 31 34 35 39 2c 20 31 34 36 30 2c 20 31 34 35 34  1459, 1460, 1454
3108b 2c 20 31 34 33 36 2c 20 31 34 36 37 2c 20 31 34  , 1436, 1467, 14
3108c 36 31 2c 20 31 34 35 35 2c 0a 20 2f 2a 20 20 20  61, 1455,. /*   
3108d 33 37 30 20 2a 2f 20 20 31 34 36 39 2c 20 31 34  370 */  1469, 14
3108e 37 31 2c 20 31 34 37 30 2c 20 31 34 37 33 2c 20  71, 1470, 1473, 
3108f 31 34 35 36 2c 20 31 34 37 34 2c 20 31 33 39 35  1456, 1474, 1395
31090 2c 20 31 33 39 31 2c 20 31 34 37 39 2c 20 31 34  , 1391, 1479, 14
31091 38 30 2c 0a 20 2f 2a 20 20 20 33 38 30 20 2a 2f  80,. /*   380 */
31092 20 20 31 34 37 37 2c 20 31 33 39 38 2c 20 31 34    1477, 1398, 14
31093 36 32 2c 20 31 34 35 38 2c 20 31 34 36 33 2c 20  62, 1458, 1463, 
31094 31 34 37 38 2c 20 31 34 36 35 2c 20 31 33 36 36  1478, 1465, 1366
31095 2c 20 31 34 30 32 2c 20 31 34 38 37 2c 0a 20 2f  , 1402, 1487,. /
31096 2a 20 20 20 33 39 30 20 2a 2f 20 20 31 35 30 35  *   390 */  1505
31097 2c 20 31 35 31 30 2c 20 31 33 39 36 2c 20 20 2d  , 1510, 1396,  -
31098 32 39 2c 20 31 34 38 35 2c 20 31 34 30 37 2c 20  29, 1485, 1407, 
31099 31 34 36 34 2c 20 31 34 36 36 2c 20 31 33 38 39  1464, 1466, 1389
3109a 2c 20 31 34 39 37 2c 0a 20 2f 2a 20 20 20 34 30  , 1497,. /*   40
3109b 30 20 2a 2f 20 20 31 34 30 33 2c 20 31 35 31 39  0 */  1403, 1519
3109c 2c 20 31 35 30 36 2c 20 31 34 30 34 2c 20 31 35  , 1506, 1404, 15
3109d 30 33 2c 20 31 34 30 38 2c 20 31 34 31 38 2c 20  03, 1408, 1418, 
3109e 31 35 30 37 2c 20 31 34 30 30 2c 20 31 35 30 38  1507, 1400, 1508
3109f 2c 0a 20 2f 2a 20 20 20 34 31 30 20 2a 2f 20 20  ,. /*   410 */  
310a0 31 35 30 39 2c 20 31 35 31 32 2c 20 31 34 36 38  1509, 1512, 1468
310a1 2c 20 31 35 31 35 2c 20 31 33 39 34 2c 20 31 35  , 1515, 1394, 15
310a2 31 33 2c 20 31 35 33 33 2c 20 31 35 33 34 2c 0a  13, 1533, 1534,.
310a3 7d 3b 0a 23 64 65 66 69 6e 65 20 59 59 5f 52 45  };.#define YY_RE
310a4 44 55 43 45 5f 55 53 45 5f 44 46 4c 54 20 28 2d  DUCE_USE_DFLT (-
310a5 32 32 32 29 0a 23 64 65 66 69 6e 65 20 59 59 5f  222).#define YY_
310a6 52 45 44 55 43 45 5f 4d 41 58 20 33 31 31 0a 73  REDUCE_MAX 311.s
310a7 74 61 74 69 63 20 63 6f 6e 73 74 20 73 68 6f 72  tatic const shor
310a8 74 20 79 79 5f 72 65 64 75 63 65 5f 6f 66 73 74  t yy_reduce_ofst
310a9 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30  [] = {. /*     0
310aa 20 2a 2f 20 20 20 31 31 38 2c 20 20 20 39 30 2c   */   118,   90,
310ab 20 20 32 33 38 2c 20 20 31 34 37 2c 20 20 31 35    238,  147,  15
310ac 35 2c 20 20 20 32 30 2c 20 20 31 37 33 2c 20 20  5,   20,  173,  
310ad 31 34 38 2c 20 20 20 38 33 2c 20 20 32 34 36 2c  148,   83,  246,
310ae 0a 20 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 20  . /*    10 */   
310af 33 37 37 2c 20 20 33 37 39 2c 20 20 33 38 35 2c  377,  379,  385,
310b0 20 20 32 34 31 2c 20 20 32 32 38 2c 20 20 33 38    241,  228,  38
310b1 36 2c 20 20 32 39 37 2c 20 20 33 31 32 2c 20 20  6,  297,  312,  
310b2 34 34 33 2c 20 20 34 34 35 2c 0a 20 2f 2a 20 20  443,  445,. /*  
310b3 20 20 32 30 20 2a 2f 20 20 20 34 35 39 2c 20 20    20 */   459,  
310b4 34 36 32 2c 20 20 34 36 34 2c 20 20 35 30 34 2c  462,  464,  504,
310b5 20 20 35 31 37 2c 20 20 35 33 30 2c 20 20 35 33    517,  530,  53
310b6 35 2c 20 20 35 33 37 2c 20 20 35 37 38 2c 20 20  5,  537,  578,  
310b7 35 39 31 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a  591,. /*    30 *
310b8 2f 20 20 20 35 39 34 2c 20 20 36 31 30 2c 20 20  /   594,  610,  
310b9 36 31 33 2c 20 20 36 35 32 2c 20 20 36 36 35 2c  613,  652,  665,
310ba 20 20 36 36 38 2c 20 20 36 37 32 2c 20 20 36 38    668,  672,  68
310bb 36 2c 20 20 37 32 36 2c 20 20 37 33 39 2c 0a 20  6,  726,  739,. 
310bc 2f 2a 20 20 20 20 34 30 20 2a 2f 20 20 20 37 34  /*    40 */   74
310bd 32 2c 20 20 37 36 30 2c 20 20 38 30 30 2c 20 20  2,  760,  800,  
310be 38 31 36 2c 20 20 38 33 34 2c 20 20 38 37 34 2c  816,  834,  874,
310bf 20 20 38 38 38 2c 20 20 38 39 30 2c 20 20 39 30    888,  890,  90
310c0 38 2c 20 20 39 31 31 2c 0a 20 2f 2a 20 20 20 20  8,  911,. /*    
310c1 35 30 20 2a 2f 20 20 20 39 36 32 2c 20 20 39 37  50 */   962,  97
310c2 35 2c 20 20 39 38 39 2c 20 20 39 39 33 2c 20 31  5,  989,  993, 1
310c3 30 32 39 2c 20 31 30 36 35 2c 20 31 30 39 32 2c  029, 1065, 1092,
310c4 20 31 30 39 34 2c 20 31 30 39 37 2c 20 31 31 30   1094, 1097, 110
310c5 30 2c 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20  0,. /*    60 */ 
310c6 20 31 31 30 35 2c 20 2d 32 32 31 2c 20 31 30 31   1105, -221, 101
310c7 31 2c 20 20 32 34 39 2c 20 20 32 32 34 2c 20 20  1,  249,  224,  
310c8 33 31 31 2c 20 20 20 32 33 2c 20 20 33 36 32 2c  311,   23,  362,
310c9 20 20 31 31 30 2c 20 20 20 37 37 2c 0a 20 2f 2a    110,   77,. /*
310ca 20 20 20 20 37 30 20 2a 2f 20 20 20 20 37 37 2c      70 */    77,
310cb 20 20 20 37 37 2c 20 20 20 37 37 2c 20 20 20 37     77,   77,   7
310cc 37 2c 20 20 20 37 37 2c 20 20 20 37 37 2c 20 20  7,   77,   77,  
310cd 20 37 37 2c 20 20 20 37 37 2c 20 20 20 37 37 2c   77,   77,   77,
310ce 20 20 20 37 37 2c 0a 20 2f 2a 20 20 20 20 38 30     77,. /*    80
310cf 20 2a 2f 20 20 20 20 37 37 2c 20 20 20 37 37 2c   */    77,   77,
310d0 20 20 20 37 37 2c 20 20 20 37 37 2c 20 20 20 37     77,   77,   7
310d1 37 2c 20 20 20 37 37 2c 20 20 20 37 37 2c 20 20  7,   77,   77,  
310d2 20 37 37 2c 20 20 20 37 37 2c 20 20 20 37 37 2c   77,   77,   77,
310d3 0a 20 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20 20  . /*    90 */   
310d4 20 37 37 2c 20 20 20 37 37 2c 20 20 20 37 37 2c   77,   77,   77,
310d5 20 20 20 37 37 2c 20 20 20 37 37 2c 20 20 20 37     77,   77,   7
310d6 37 2c 20 20 20 37 37 2c 20 20 20 37 37 2c 20 20  7,   77,   77,  
310d7 20 37 37 2c 20 20 20 37 37 2c 0a 20 2f 2a 20 20   77,   77,. /*  
310d8 20 31 30 30 20 2a 2f 20 20 20 20 37 37 2c 20 20   100 */    77,  
310d9 20 37 37 2c 20 20 20 37 37 2c 20 20 20 37 37 2c   77,   77,   77,
310da 20 20 20 37 37 2c 20 20 20 37 37 2c 20 20 20 37     77,   77,   7
310db 37 2c 20 20 37 33 30 2c 20 20 20 37 37 2c 20 20  7,  730,   77,  
310dc 20 37 37 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a   77,. /*   110 *
310dd 2f 20 20 20 38 31 33 2c 20 20 2d 38 38 2c 20 20  /   813,  -88,  
310de 20 32 32 2c 20 20 36 36 39 2c 20 20 35 39 36 2c   22,  669,  596,
310df 20 20 37 36 32 2c 20 20 33 34 31 2c 20 20 38 33    762,  341,  83
310e0 33 2c 20 20 37 34 33 2c 20 20 37 34 30 2c 0a 20  3,  743,  740,. 
310e1 2f 2a 20 20 20 31 32 30 20 2a 2f 20 20 20 38 31  /*   120 */   81
310e2 34 2c 20 20 38 39 31 2c 20 20 38 39 32 2c 20 20  4,  891,  892,  
310e3 38 31 37 2c 20 20 38 39 34 2c 20 20 39 36 34 2c  817,  894,  964,
310e4 20 20 39 39 34 2c 20 20 33 39 30 2c 20 20 2d 37    994,  390,  -7
310e5 31 2c 20 20 32 30 36 2c 0a 20 2f 2a 20 20 20 31  1,  206,. /*   1
310e6 33 30 20 2a 2f 20 20 20 35 34 39 2c 20 20 34 36  30 */   549,  46
310e7 33 2c 20 20 36 36 36 2c 20 2d 31 34 32 2c 20 20  3,  666, -142,  
310e8 20 37 37 2c 20 20 20 37 37 2c 20 20 20 37 37 2c   77,   77,   77,
310e9 20 20 20 37 37 2c 20 20 20 37 37 2c 20 20 33 39     77,   77,  39
310ea 32 2c 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20  2,. /*   140 */ 
310eb 20 20 32 37 33 2c 20 20 39 37 30 2c 20 20 38 31    273,  970,  81
310ec 35 2c 20 20 39 39 36 2c 20 31 30 30 31 2c 20 20  5,  996, 1001,  
310ed 39 32 35 2c 20 31 30 31 35 2c 20 31 30 32 33 2c  925, 1015, 1023,
310ee 20 31 30 36 31 2c 20 31 30 36 33 2c 0a 20 2f 2a   1061, 1063,. /*
310ef 20 20 20 31 35 30 20 2a 2f 20 20 31 30 39 33 2c     150 */  1093,
310f0 20 31 30 39 35 2c 20 31 30 39 38 2c 20 31 31 32   1095, 1098, 112
310f1 38 2c 20 31 31 33 36 2c 20 31 31 33 37 2c 20 31  8, 1136, 1137, 1
310f2 31 34 30 2c 20 20 39 33 32 2c 20 31 31 34 31 2c  140,  932, 1141,
310f3 20 31 31 34 32 2c 0a 20 2f 2a 20 20 20 31 36 30   1142,. /*   160
310f4 20 2a 2f 20 20 31 31 34 34 2c 20 31 31 34 35 2c   */  1144, 1145,
310f5 20 31 30 30 31 2c 20 31 31 36 31 2c 20 31 31 36   1001, 1161, 116
310f6 32 2c 20 31 31 36 35 2c 20 31 31 36 36 2c 20 20  2, 1165, 1166,  
310f7 39 33 32 2c 20 31 31 36 37 2c 20 31 31 38 39 2c  932, 1167, 1189,
310f8 0a 20 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 31  . /*   170 */  1
310f9 31 39 35 2c 20 20 20 33 31 2c 20 20 32 30 38 2c  195,   31,  208,
310fa 20 20 34 35 31 2c 20 20 36 37 30 2c 20 20 36 37    451,  670,  67
310fb 33 2c 20 20 38 32 30 2c 20 20 38 32 31 2c 20 20  3,  820,  821,  
310fc 38 32 32 2c 20 20 38 33 30 2c 0a 20 2f 2a 20 20  822,  830,. /*  
310fd 20 31 38 30 20 2a 2f 20 20 20 39 38 38 2c 20 20   180 */   988,  
310fe 39 31 30 2c 20 20 2d 31 30 2c 20 20 20 32 31 2c  910,  -10,   21,
310ff 20 20 20 34 34 2c 20 20 39 31 34 2c 20 20 39 32     44,  914,  92
31100 37 2c 20 20 38 38 33 2c 20 20 39 36 31 2c 20 20  7,  883,  961,  
31101 39 38 32 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a  982,. /*   190 *
31102 2f 20 20 20 33 33 32 2c 20 20 32 36 36 2c 20 20  /   332,  266,  
31103 39 34 38 2c 20 20 33 38 33 2c 20 20 2d 39 36 2c  948,  383,  -96,
31104 20 20 2d 39 34 2c 20 20 2d 37 36 2c 20 20 2d 38    -94,  -76,  -8
31105 33 2c 20 20 2d 35 39 2c 20 20 2d 33 30 2c 0a 20  3,  -59,  -30,. 
31106 2f 2a 20 20 20 32 30 30 20 2a 2f 20 20 20 2d 34  /*   200 */   -4
31107 31 2c 20 20 2d 35 39 2c 20 20 20 2d 33 2c 20 20  1,  -59,   -3,  
31108 2d 35 39 2c 20 20 20 31 30 2c 20 20 2d 35 39 2c  -59,   10,  -59,
31109 20 20 31 37 37 2c 20 20 2d 35 39 2c 20 20 31 30    177,  -59,  10
3110a 35 2c 20 20 32 34 37 2c 0a 20 2f 2a 20 20 20 32  5,  247,. /*   2
3110b 31 30 20 2a 2f 20 20 20 2d 35 39 2c 20 20 2d 35  10 */   -59,  -5
3110c 39 2c 20 20 2d 35 39 2c 20 20 31 33 39 2c 20 20  9,  -59,  139,  
3110d 2d 35 39 2c 20 20 2d 34 31 2c 20 20 31 39 30 2c  -59,  -41,  190,
3110e 20 20 2d 35 39 2c 20 20 32 39 36 2c 20 20 34 33    -59,  296,  43
3110f 38 2c 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20  8,. /*   220 */ 
31110 20 20 33 32 30 2c 20 20 34 34 38 2c 20 20 35 32    320,  448,  52
31111 33 2c 20 20 35 32 39 2c 20 20 35 39 30 2c 20 20  3,  529,  590,  
31112 36 32 36 2c 20 20 36 38 32 2c 20 20 36 39 39 2c  626,  682,  699,
31113 20 20 37 30 36 2c 20 20 37 32 37 2c 0a 20 2f 2a    706,  727,. /*
31114 20 20 20 32 33 30 20 2a 2f 20 20 20 37 36 34 2c     230 */   764,
31115 20 20 38 31 39 2c 20 20 37 33 34 2c 20 20 37 34    819,  734,  74
31116 35 2c 20 20 38 39 37 2c 20 20 39 32 31 2c 20 20  5,  897,  921,  
31117 38 34 38 2c 20 20 39 32 30 2c 20 20 39 36 30 2c  848,  920,  960,
31118 20 20 39 39 38 2c 0a 20 2f 2a 20 20 20 32 34 30    998,. /*   240
31119 20 2a 2f 20 20 31 30 34 30 2c 20 31 30 30 34 2c   */  1040, 1004,
3111a 20 31 30 35 30 2c 20 20 39 39 35 2c 20 31 30 35   1050,  995, 105
3111b 31 2c 20 31 30 36 32 2c 20 31 30 37 34 2c 20 31  1, 1062, 1074, 1
3111c 30 38 32 2c 20 31 30 36 39 2c 20 31 30 38 38 2c  082, 1069, 1088,
3111d 0a 20 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20 31  . /*   250 */  1
3111e 31 30 36 2c 20 31 31 31 36 2c 20 31 30 37 38 2c  106, 1116, 1078,
3111f 20 31 31 34 39 2c 20 31 31 37 32 2c 20 31 30 38   1149, 1172, 108
31120 33 2c 20 31 31 37 31 2c 20 31 31 39 31 2c 20 31  3, 1171, 1191, 1
31121 31 33 34 2c 20 31 31 37 33 2c 0a 20 2f 2a 20 20  134, 1173,. /*  
31122 20 32 36 30 20 2a 2f 20 20 31 31 39 34 2c 20 31   260 */  1194, 1
31123 31 39 36 2c 20 31 31 31 33 2c 20 31 31 37 39 2c  196, 1113, 1179,
31124 20 31 31 38 33 2c 20 31 31 38 34 2c 20 31 32 30   1183, 1184, 120
31125 39 2c 20 31 31 39 32 2c 20 31 31 38 38 2c 20 31  9, 1192, 1188, 1
31126 32 32 34 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a  224,. /*   270 *
31127 2f 20 20 31 32 31 37 2c 20 31 32 32 37 2c 20 31  /  1217, 1227, 1
31128 32 32 38 2c 20 31 31 39 38 2c 20 31 31 37 36 2c  228, 1198, 1176,
31129 20 31 31 38 30 2c 20 31 32 33 33 2c 20 31 31 34   1180, 1233, 114
3112a 37 2c 20 31 31 36 38 2c 20 31 31 39 39 2c 0a 20  7, 1168, 1199,. 
3112b 2f 2a 20 20 20 32 38 30 20 2a 2f 20 20 31 32 30  /*   280 */  120
3112c 38 2c 20 31 32 34 32 2c 20 31 32 31 30 2c 20 31  8, 1242, 1210, 1
3112d 32 31 31 2c 20 31 32 31 32 2c 20 31 32 30 31 2c  211, 1212, 1201,
3112e 20 31 32 31 36 2c 20 31 32 30 33 2c 20 31 32 30   1216, 1203, 120
3112f 34 2c 20 31 31 38 32 2c 0a 20 2f 2a 20 20 20 32  4, 1182,. /*   2
31130 39 30 20 2a 2f 20 20 31 32 30 36 2c 20 31 32 31  90 */  1206, 121
31131 33 2c 20 31 32 36 35 2c 20 31 32 31 34 2c 20 31  3, 1265, 1214, 1
31132 32 36 31 2c 20 31 32 31 38 2c 20 31 32 32 30 2c  261, 1218, 1220,
31133 20 31 32 32 36 2c 20 31 32 37 32 2c 20 31 32 33   1226, 1272, 123
31134 32 2c 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20  2,. /*   300 */ 
31135 20 31 32 37 30 2c 20 31 32 33 38 2c 20 31 32 38   1270, 1238, 128
31136 32 2c 20 31 32 30 35 2c 20 31 32 30 30 2c 20 31  2, 1205, 1200, 1
31137 32 31 35 2c 20 31 32 31 39 2c 20 31 32 35 37 2c  215, 1219, 1257,
31138 20 31 32 36 30 2c 20 31 32 38 31 2c 0a 20 2f 2a   1260, 1281,. /*
31139 20 20 20 33 31 30 20 2a 2f 20 20 31 32 32 31 2c     310 */  1221,
3113a 20 31 32 32 32 2c 0a 7d 3b 0a 73 74 61 74 69 63   1222,.};.static
3113b 20 63 6f 6e 73 74 20 59 59 41 43 54 49 4f 4e 54   const YYACTIONT
3113c 59 50 45 20 79 79 5f 64 65 66 61 75 6c 74 5b 5d  YPE yy_default[]
3113d 20 3d 20 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a   = {. /*     0 *
3113e 2f 20 20 20 36 33 34 2c 20 20 38 36 39 2c 20 20  /   634,  869,  
3113f 39 35 38 2c 20 20 39 35 38 2c 20 20 38 36 39 2c  958,  958,  869,
31140 20 20 39 35 38 2c 20 20 38 39 38 2c 20 20 38 39    958,  898,  89
31141 38 2c 20 20 39 35 38 2c 20 20 37 35 37 2c 0a 20  8,  958,  757,. 
31142 2f 2a 20 20 20 20 31 30 20 2a 2f 20 20 20 39 35  /*    10 */   95
31143 38 2c 20 20 38 36 37 2c 20 20 39 35 38 2c 20 20  8,  867,  958,  
31144 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31145 20 20 39 35 38 2c 20 20 39 33 32 2c 20 20 39 35    958,  932,  95
31146 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 20  8,  958,. /*    
31147 32 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35  20 */   958,  95
31148 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31149 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
3114a 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
3114b 38 2c 0a 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20  8,. /*    30 */ 
3114c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
3114d 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
3114e 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
3114f 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a    958,  958,. /*
31150 20 20 20 20 34 30 20 2a 2f 20 20 20 39 35 38 2c      40 */   958,
31151 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31152 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31153 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31154 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 20 35 30    958,. /*    50
31155 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c   */   958,  958,
31156 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31157 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31158 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31159 0a 20 2f 2a 20 20 20 20 36 30 20 2a 2f 20 20 20  . /*    60 */   
3115a 39 35 38 2c 20 20 38 34 31 2c 20 20 39 35 38 2c  958,  841,  958,
3115b 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 38 39    958,  958,  89
3115c 38 2c 20 20 36 37 33 2c 20 20 38 39 38 2c 20 20  8,  673,  898,  
3115d 37 36 31 2c 20 20 37 39 32 2c 0a 20 2f 2a 20 20  761,  792,. /*  
3115e 20 20 37 30 20 2a 2f 20 20 20 39 35 38 2c 20 20    70 */   958,  
3115f 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31160 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31161 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31162 39 33 31 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a  931,. /*    80 *
31163 2f 20 20 20 37 39 33 2c 20 20 37 38 33 2c 20 20  /   793,  783,  
31164 39 31 31 2c 20 20 38 36 34 2c 20 20 38 30 36 2c  911,  864,  806,
31165 20 20 37 37 32 2c 20 20 38 30 30 2c 20 20 37 39    772,  800,  79
31166 37 2c 20 20 39 33 33 2c 20 20 38 36 36 2c 0a 20  7,  933,  866,. 
31167 2f 2a 20 20 20 20 39 30 20 2a 2f 20 20 20 38 36  /*    90 */   86
31168 33 2c 20 20 37 39 39 2c 20 20 37 39 30 2c 20 20  3,  799,  790,  
31169 37 39 34 2c 20 20 38 37 31 2c 20 20 38 30 37 2c  794,  871,  807,
3116a 20 20 38 37 30 2c 20 20 38 36 32 2c 20 20 38 32    870,  862,  82
3116b 39 2c 20 20 38 34 37 2c 0a 20 2f 2a 20 20 20 31  9,  847,. /*   1
3116c 30 30 20 2a 2f 20 20 20 38 32 38 2c 20 20 38 34  00 */   828,  84
3116d 36 2c 20 20 38 35 33 2c 20 20 38 33 31 2c 20 20  6,  853,  831,  
3116e 38 34 35 2c 20 20 38 33 30 2c 20 20 38 34 30 2c  845,  830,  840,
3116f 20 20 36 36 35 2c 20 20 38 33 32 2c 20 20 38 33    665,  832,  83
31170 33 2c 0a 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20  3,. /*   110 */ 
31171 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 36 36    958,  958,  66
31172 30 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  0,  958,  958,  
31173 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31174 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a    958,  958,. /*
31175 20 20 20 31 32 30 20 2a 2f 20 20 20 39 35 38 2c     120 */   958,
31176 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31177 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31178 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31179 20 20 37 32 36 2c 0a 20 2f 2a 20 20 20 31 33 30    726,. /*   130
3117a 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c   */   958,  958,
3117b 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 38 33    958,  958,  83
3117c 34 2c 20 20 38 33 35 2c 20 20 38 34 39 2c 20 20  4,  835,  849,  
3117d 38 34 38 2c 20 20 38 35 30 2c 20 20 39 35 38 2c  848,  850,  958,
3117e 0a 20 2f 2a 20 20 20 31 34 30 20 2a 2f 20 20 20  . /*   140 */   
3117f 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31180 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31181 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31182 38 38 32 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20  882,  958,. /*  
31183 20 31 35 30 20 2a 2f 20 20 20 39 35 38 2c 20 20   150 */   958,  
31184 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
31185 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31186 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
31187 39 35 38 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a  958,. /*   160 *
31188 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  /   958,  958,  
31189 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
3118a 20 20 39 35 38 2c 20 20 36 34 30 2c 20 20 39 35    958,  640,  95
3118b 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20  8,  958,  958,. 
3118c 2f 2a 20 20 20 31 37 30 20 2a 2f 20 20 20 39 35  /*   170 */   95
3118d 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
3118e 39 35 38 2c 20 20 39 33 36 2c 20 20 39 35 38 2c  958,  936,  958,
3118f 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 33    958,  958,  93
31190 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 31  8,  958,. /*   1
31191 38 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35  80 */   958,  95
31192 38 2c 20 20 39 35 38 2c 20 20 37 35 37 2c 20 20  8,  958,  757,  
31193 37 35 37 2c 20 20 37 35 37 2c 20 20 36 33 34 2c  757,  757,  634,
31194 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
31195 38 2c 0a 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20  8,. /*   190 */ 
31196 20 20 39 35 38 2c 20 20 37 36 31 2c 20 20 37 35    958,  761,  75
31197 31 2c 20 20 39 35 30 2c 20 20 39 35 38 2c 20 20  1,  950,  958,  
31198 37 31 37 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  717,  958,  958,
31199 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a    958,  958,. /*
3119a 20 20 20 32 30 30 20 2a 2f 20 20 20 39 35 38 2c     200 */   958,
3119b 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
3119c 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
3119d 36 34 32 2c 20 20 39 32 31 2c 20 20 39 35 38 2c  642,  921,  958,
3119e 20 20 36 37 35 2c 0a 20 2f 2a 20 20 20 32 31 30    675,. /*   210
3119f 20 2a 2f 20 20 20 38 30 32 2c 20 20 36 36 32 2c   */   802,  662,
311a0 20 20 37 34 30 2c 20 20 39 30 34 2c 20 20 37 33    740,  904,  73
311a1 38 2c 20 20 37 35 39 2c 20 20 37 34 39 2c 20 20  8,  759,  749,  
311a2 39 32 33 2c 20 20 36 39 36 2c 20 20 37 38 36 2c  923,  696,  786,
311a3 0a 20 2f 2a 20 20 20 32 32 30 20 2a 2f 20 20 20  . /*   220 */   
311a4 39 35 38 2c 20 20 37 38 36 2c 20 20 39 35 38 2c  958,  786,  958,
311a5 20 20 36 39 39 2c 20 20 37 34 39 2c 20 20 37 35    699,  749,  75
311a6 38 2c 20 20 39 35 38 2c 20 20 37 39 36 2c 20 20  8,  958,  796,  
311a7 37 37 34 2c 20 20 37 37 34 2c 0a 20 2f 2a 20 20  774,  774,. /*  
311a8 20 32 33 30 20 2a 2f 20 20 20 39 35 38 2c 20 20   230 */   958,  
311a9 39 35 38 2c 20 20 39 31 36 2c 20 20 38 36 35 2c  958,  916,  865,
311aa 20 20 37 38 36 2c 20 20 37 38 36 2c 20 20 37 39    786,  786,  79
311ab 36 2c 20 20 39 31 36 2c 20 20 37 37 34 2c 20 20  6,  916,  774,  
311ac 37 38 36 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a  786,. /*   240 *
311ad 2f 20 20 20 39 35 38 2c 20 20 37 33 37 2c 20 20  /   958,  737,  
311ae 36 35 30 2c 20 20 38 30 38 2c 20 20 37 36 35 2c  650,  808,  765,
311af 20 20 37 36 35 2c 20 20 37 36 35 2c 20 20 37 31    765,  765,  71
311b0 36 2c 20 20 37 31 33 2c 20 20 37 32 38 2c 0a 20  6,  713,  728,. 
311b1 2f 2a 20 20 20 32 35 30 20 2a 2f 20 20 20 37 32  /*   250 */   72
311b2 38 2c 20 20 37 36 35 2c 20 20 38 37 39 2c 20 20  8,  765,  879,  
311b3 37 32 38 2c 20 20 36 35 37 2c 20 20 38 37 39 2c  728,  657,  879,
311b4 20 20 37 32 38 2c 20 20 37 36 35 2c 20 20 37 31    728,  765,  71
311b5 33 2c 20 20 37 32 38 2c 0a 20 2f 2a 20 20 20 32  3,  728,. /*   2
311b6 36 30 20 2a 2f 20 20 20 36 35 37 2c 20 20 37 36  60 */   657,  76
311b7 35 2c 20 20 39 33 35 2c 20 20 38 37 35 2c 20 20  5,  935,  875,  
311b8 37 32 38 2c 20 20 36 39 39 2c 20 20 36 35 37 2c  728,  699,  657,
311b9 20 20 37 32 38 2c 20 20 38 37 35 2c 20 20 36 33    728,  875,  63
311ba 39 2c 0a 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20  9,. /*   270 */ 
311bb 20 20 37 36 35 2c 20 20 36 33 39 2c 20 20 36 33    765,  639,  63
311bc 39 2c 20 20 37 33 30 2c 20 20 37 37 33 2c 20 20  9,  730,  773,  
311bd 37 37 38 2c 20 20 37 36 35 2c 20 20 39 34 33 2c  778,  765,  943,
311be 20 20 39 35 38 2c 20 20 37 39 36 2c 0a 20 2f 2a    958,  796,. /*
311bf 20 20 20 32 38 30 20 2a 2f 20 20 20 37 33 30 2c     280 */   730,
311c0 20 20 37 36 35 2c 20 20 37 33 37 2c 20 20 37 33    765,  737,  73
311c1 37 2c 20 20 37 33 37 2c 20 20 37 39 36 2c 20 20  7,  737,  796,  
311c2 37 33 30 2c 20 20 37 37 33 2c 20 20 37 37 38 2c  730,  773,  778,
311c3 20 20 38 30 38 2c 0a 20 2f 2a 20 20 20 32 39 30    808,. /*   290
311c4 20 2a 2f 20 20 20 37 37 33 2c 20 20 37 37 38 2c   */   773,  778,
311c5 20 20 36 33 39 2c 20 20 37 39 31 2c 20 20 37 36    639,  791,  76
311c6 35 2c 20 20 37 37 39 2c 20 20 37 38 39 2c 20 20  5,  779,  789,  
311c7 37 38 37 2c 20 20 36 35 37 2c 20 20 37 39 36 2c  787,  657,  796,
311c8 0a 20 2f 2a 20 20 20 33 30 30 20 2a 2f 20 20 20  . /*   300 */   
311c9 37 36 35 2c 20 20 39 35 38 2c 20 20 36 35 30 2c  765,  958,  650,
311ca 20 20 39 31 30 2c 20 20 39 30 38 2c 20 20 39 33    910,  908,  93
311cb 35 2c 20 20 39 35 30 2c 20 20 37 30 31 2c 20 20  5,  950,  701,  
311cc 37 30 31 2c 20 20 36 38 33 2c 0a 20 2f 2a 20 20  701,  683,. /*  
311cd 20 33 31 30 20 2a 2f 20 20 20 39 35 35 2c 20 20   310 */   955,  
311ce 39 35 35 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  955,  958,  958,
311cf 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
311d0 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
311d1 39 35 38 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a  958,. /*   320 *
311d2 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  /   958,  958,  
311d3 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
311d4 20 20 38 38 34 2c 20 20 39 35 38 2c 20 20 39 35    884,  958,  95
311d5 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20  8,  958,  958,. 
311d6 2f 2a 20 20 20 33 33 30 20 2a 2f 20 20 20 39 35  /*   330 */   95
311d7 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
311d8 39 35 38 2c 20 20 36 34 38 2c 20 20 39 35 38 2c  958,  648,  958,
311d9 20 20 36 36 37 2c 20 20 38 31 35 2c 20 20 38 32    667,  815,  82
311da 30 2c 20 20 38 31 36 2c 0a 20 2f 2a 20 20 20 33  0,  816,. /*   3
311db 34 30 20 2a 2f 20 20 20 39 35 38 2c 20 20 38 31  40 */   958,  81
311dc 37 2c 20 20 37 34 33 2c 20 20 39 35 38 2c 20 20  7,  743,  958,  
311dd 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
311de 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
311df 38 2c 0a 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20  8,. /*   350 */ 
311e0 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
311e1 38 2c 20 20 38 36 38 2c 20 20 39 35 38 2c 20 20  8,  868,  958,  
311e2 37 38 30 2c 20 20 39 35 38 2c 20 20 37 38 38 2c  780,  958,  788,
311e3 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a    958,  958,. /*
311e4 20 20 20 33 36 30 20 2a 2f 20 20 20 39 35 38 2c     360 */   958,
311e5 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
311e6 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
311e7 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
311e8 20 20 39 35 38 2c 0a 20 2f 2a 20 20 20 33 37 30    958,. /*   370
311e9 20 2a 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c   */   958,  958,
311ea 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
311eb 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
311ec 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
311ed 0a 20 2f 2a 20 20 20 33 38 30 20 2a 2f 20 20 20  . /*   380 */   
311ee 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
311ef 20 20 39 35 38 2c 20 20 39 30 36 2c 20 20 39 30    958,  906,  90
311f0 37 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  7,  958,  958,  
311f1 39 35 38 2c 20 20 39 35 38 2c 0a 20 2f 2a 20 20  958,  958,. /*  
311f2 20 33 39 30 20 2a 2f 20 20 20 39 35 38 2c 20 20   390 */   958,  
311f3 39 35 38 2c 20 20 39 31 34 2c 20 20 39 35 38 2c  958,  914,  958,
311f4 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
311f5 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  8,  958,  958,  
311f6 39 35 38 2c 0a 20 2f 2a 20 20 20 34 30 30 20 2a  958,. /*   400 *
311f7 2f 20 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20  /   958,  958,  
311f8 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c  958,  958,  958,
311f9 20 20 39 35 38 2c 20 20 39 35 38 2c 20 20 39 35    958,  958,  95
311fa 38 2c 20 20 39 35 38 2c 20 20 39 35 38 2c 0a 20  8,  958,  958,. 
311fb 2f 2a 20 20 20 34 31 30 20 2a 2f 20 20 20 39 35  /*   410 */   95
311fc 38 2c 20 20 39 35 38 2c 20 20 39 34 32 2c 20 20  8,  958,  942,  
311fd 39 35 38 2c 20 20 39 35 38 2c 20 20 39 34 35 2c  958,  958,  945,
311fe 20 20 36 33 35 2c 20 20 39 35 38 2c 20 20 37 31    635,  958,  71
311ff 31 2c 20 20 36 34 39 2c 0a 20 2f 2a 20 20 20 34  1,  649,. /*   4
31200 32 30 20 2a 2f 20 20 20 37 33 34 2c 20 20 37 33  20 */   734,  73
31201 33 2c 20 20 37 33 32 2c 20 20 37 32 39 2c 20 20  3,  732,  729,  
31202 36 39 31 2c 20 20 36 39 30 2c 20 20 36 38 39 2c  691,  690,  689,
31203 20 20 36 34 37 2c 20 20 38 32 30 2c 20 20 36 38    647,  820,  68
31204 38 2c 0a 20 2f 2a 20 20 20 34 33 30 20 2a 2f 20  8,. /*   430 */ 
31205 20 20 37 36 37 2c 20 20 36 38 37 2c 20 20 38 32    767,  687,  82
31206 36 2c 20 20 39 30 30 2c 20 20 38 32 35 2c 20 20  6,  900,  825,  
31207 38 31 33 2c 20 20 39 33 37 2c 20 20 36 34 36 2c  813,  937,  646,
31208 20 20 39 30 39 2c 20 20 39 30 35 2c 0a 20 2f 2a    909,  905,. /*
31209 20 20 20 34 34 30 20 2a 2f 20 20 20 36 34 35 2c     440 */   645,
3120a 20 20 38 35 37 2c 20 20 39 33 39 2c 20 20 39 30    857,  939,  90
3120b 31 2c 20 20 39 30 32 2c 20 20 39 35 37 2c 20 20  1,  902,  957,  
3120c 39 30 33 2c 20 20 36 35 39 2c 20 20 36 34 34 2c  903,  659,  644,
3120d 20 20 37 35 34 2c 0a 20 2f 2a 20 20 20 34 35 30    754,. /*   450
3120e 20 2a 2f 20 20 20 37 35 33 2c 20 20 37 35 32 2c   */   753,  752,
3120f 20 20 39 35 34 2c 20 20 37 36 34 2c 20 20 36 33    954,  764,  63
31210 30 2c 20 20 37 36 33 2c 20 20 37 37 36 2c 20 20  0,  763,  776,  
31211 38 37 32 2c 20 20 38 37 33 2c 20 20 36 34 33 2c  872,  873,  643,
31212 0a 20 2f 2a 20 20 20 34 36 30 20 2a 2f 20 20 20  . /*   460 */   
31213 37 37 35 2c 20 20 38 31 31 2c 20 20 38 31 30 2c  775,  811,  810,
31214 20 20 37 37 37 2c 20 20 37 33 31 2c 20 20 38 30    777,  731,  80
31215 34 2c 20 20 38 30 35 2c 20 20 37 36 32 2c 20 20  4,  805,  762,  
31216 38 39 39 2c 20 20 39 34 30 2c 0a 20 2f 2a 20 20  899,  940,. /*  
31217 20 34 37 30 20 2a 2f 20 20 20 39 31 32 2c 20 20   470 */   912,  
31218 37 35 36 2c 20 20 37 35 35 2c 20 20 36 33 36 2c  756,  755,  636,
31219 20 20 37 37 31 2c 20 20 39 31 35 2c 20 20 37 37    771,  915,  77
3121a 30 2c 20 20 36 37 32 2c 20 20 37 36 39 2c 20 20  0,  672,  769,  
3121b 39 31 37 2c 0a 20 2f 2a 20 20 20 34 38 30 20 2a  917,. /*   480 *
3121c 2f 20 20 20 36 37 31 2c 20 20 39 31 38 2c 20 20  /   671,  918,  
3121d 39 34 31 2c 20 20 37 36 38 2c 20 20 37 36 30 2c  941,  768,  760,
3121e 20 20 37 35 30 2c 20 20 39 34 34 2c 20 20 37 38    750,  944,  78
3121f 32 2c 20 20 37 38 35 2c 20 20 39 31 39 2c 0a 20  2,  785,  919,. 
31220 2f 2a 20 20 20 34 39 30 20 2a 2f 20 20 20 36 33  /*   490 */   63
31221 31 2c 20 20 37 38 34 2c 20 20 37 38 31 2c 20 20  1,  784,  781,  
31222 36 37 30 2c 20 20 38 34 32 2c 20 20 38 35 39 2c  670,  842,  859,
31223 20 20 37 36 36 2c 20 20 38 35 36 2c 20 20 38 30    766,  856,  80
31224 39 2c 20 20 39 32 38 2c 0a 20 2f 2a 20 20 20 35  9,  928,. /*   5
31225 30 30 20 2a 2f 20 20 20 39 32 37 2c 20 20 36 36  00 */   927,  66
31226 38 2c 20 20 39 32 36 2c 20 20 39 32 35 2c 20 20  8,  926,  925,  
31227 39 32 34 2c 20 20 38 36 31 2c 20 20 39 32 30 2c  924,  861,  920,
31228 20 20 38 36 30 2c 20 20 39 34 36 2c 20 20 38 32    860,  946,  82
31229 37 2c 0a 20 2f 2a 20 20 20 35 31 30 20 2a 2f 20  7,. /*   510 */ 
3122a 20 20 38 32 34 2c 20 20 36 37 38 2c 20 20 39 32    824,  678,  92
3122b 32 2c 20 20 36 36 37 2c 20 20 39 31 33 2c 20 20  2,  667,  913,  
3122c 36 34 31 2c 20 20 36 37 39 2c 20 20 38 39 37 2c  641,  679,  897,
3122d 20 20 39 35 31 2c 20 20 38 39 35 2c 0a 20 2f 2a    951,  895,. /*
3122e 20 20 20 35 32 30 20 2a 2f 20 20 20 39 34 37 2c     520 */   947,
3122f 20 20 36 35 36 2c 20 20 38 39 36 2c 20 20 38 39    656,  896,  89
31230 34 2c 20 20 36 38 31 2c 20 20 37 33 35 2c 20 20  4,  681,  735,  
31231 39 35 32 2c 20 20 37 33 36 2c 20 20 36 38 30 2c  952,  736,  680,
31232 20 20 36 37 37 2c 0a 20 2f 2a 20 20 20 35 33 30    677,. /*   530
31233 20 2a 2f 20 20 20 37 33 39 2c 20 20 36 37 36 2c   */   739,  676,
31234 20 20 38 35 38 2c 20 20 38 38 31 2c 20 20 37 34    858,  881,  74
31235 35 2c 20 20 37 34 34 2c 20 20 39 32 39 2c 20 20  5,  744,  929,  
31236 38 35 34 2c 20 20 36 33 38 2c 20 20 38 35 31 2c  854,  638,  851,
31237 0a 20 2f 2a 20 20 20 35 34 30 20 2a 2f 20 20 20  . /*   540 */   
31238 38 34 33 2c 20 20 38 33 38 2c 20 20 37 39 35 2c  843,  838,  795,
31239 20 20 38 35 35 2c 20 20 38 35 32 2c 20 20 38 34    855,  852,  84
3123a 34 2c 20 20 36 33 37 2c 20 20 38 33 39 2c 20 20  4,  637,  839,  
3123b 38 33 37 2c 20 20 37 39 38 2c 0a 20 2f 2a 20 20  837,  798,. /*  
3123c 20 35 35 30 20 2a 2f 20 20 20 38 33 36 2c 20 20   550 */   836,  
3123d 38 32 32 2c 20 20 38 32 31 2c 20 20 38 31 39 2c  822,  821,  819,
3123e 20 20 38 31 38 2c 20 20 39 35 33 2c 20 20 38 31    818,  953,  81
3123f 34 2c 20 20 38 32 33 2c 20 20 38 30 31 2c 20 20  4,  823,  801,  
31240 36 33 33 2c 0a 20 2f 2a 20 20 20 35 36 30 20 2a  633,. /*   560 *
31241 2f 20 20 20 38 30 33 2c 20 20 36 36 39 2c 20 20  /   803,  669,  
31242 38 38 33 2c 20 20 37 34 36 2c 20 20 37 34 32 2c  883,  746,  742,
31243 20 20 37 34 31 2c 20 20 38 31 32 2c 20 20 38 38    741,  812,  88
31244 35 2c 20 20 38 38 37 2c 20 20 38 38 39 2c 0a 20  5,  887,  889,. 
31245 2f 2a 20 20 20 35 37 30 20 2a 2f 20 20 20 38 39  /*   570 */   89
31246 30 2c 20 20 38 39 31 2c 20 20 38 39 32 2c 20 20  0,  891,  892,  
31247 38 39 33 2c 20 20 37 34 38 2c 20 20 36 33 32 2c  893,  748,  632,
31248 20 20 38 38 36 2c 20 20 39 35 36 2c 20 20 38 38    886,  956,  88
31249 38 2c 20 20 37 34 37 2c 0a 20 2f 2a 20 20 20 35  8,  747,. /*   5
3124a 38 30 20 2a 2f 20 20 20 36 36 36 2c 20 20 37 31  80 */   666,  71
3124b 38 2c 20 20 36 37 34 2c 20 20 37 32 31 2c 20 20  8,  674,  721,  
3124c 36 38 36 2c 20 20 36 38 34 2c 20 20 36 38 35 2c  686,  684,  685,
3124d 20 20 37 31 32 2c 20 20 36 38 32 2c 20 20 36 39    712,  682,  69
3124e 38 2c 0a 20 2f 2a 20 20 20 35 39 30 20 2a 2f 20  8,. /*   590 */ 
3124f 20 20 37 32 32 2c 20 20 36 36 33 2c 20 20 36 39    722,  663,  69
31250 37 2c 20 20 38 37 38 2c 20 20 38 38 30 2c 20 20  7,  878,  880,  
31251 37 32 33 2c 20 20 39 33 30 2c 20 20 38 37 37 2c  723,  930,  877,
31252 20 20 38 37 36 2c 20 20 37 30 35 2c 0a 20 2f 2a    876,  705,. /*
31253 20 20 20 36 30 30 20 2a 2f 20 20 20 37 32 34 2c     600 */   724,
31254 20 20 36 36 31 2c 20 20 36 35 34 2c 20 20 37 31    661,  654,  71
31255 30 2c 20 20 37 30 39 2c 20 20 39 33 34 2c 20 20  0,  709,  934,  
31256 37 30 38 2c 20 20 37 30 37 2c 20 20 37 30 36 2c  708,  707,  706,
31257 20 20 37 30 34 2c 0a 20 2f 2a 20 20 20 36 31 30    704,. /*   610
31258 20 2a 2f 20 20 20 36 35 32 2c 20 20 37 32 35 2c   */   652,  725,
31259 20 20 37 32 37 2c 20 20 37 31 39 2c 20 20 37 32    727,  719,  72
3125a 30 2c 20 20 36 36 34 2c 20 20 37 30 33 2c 20 20  0,  664,  703,  
3125b 37 30 32 2c 20 20 36 39 35 2c 20 20 36 39 34 2c  702,  695,  694,
3125c 0a 20 2f 2a 20 20 20 36 32 30 20 2a 2f 20 20 20  . /*   620 */   
3125d 37 30 30 2c 20 20 36 39 33 2c 20 20 37 31 35 2c  700,  693,  715,
3125e 20 20 36 35 35 2c 20 20 36 35 31 2c 20 20 36 35    655,  651,  65
3125f 33 2c 20 20 36 35 38 2c 20 20 37 31 34 2c 20 20  3,  658,  714,  
31260 36 39 32 2c 0a 7d 3b 0a 23 64 65 66 69 6e 65 20  692,.};.#define 
31261 59 59 5f 53 5a 5f 41 43 54 54 41 42 20 28 69 6e  YY_SZ_ACTTAB (in
31262 74 29 28 73 69 7a 65 6f 66 28 79 79 5f 61 63 74  t)(sizeof(yy_act
31263 69 6f 6e 29 2f 73 69 7a 65 6f 66 28 79 79 5f 61  ion)/sizeof(yy_a
31264 63 74 69 6f 6e 5b 30 5d 29 29 0a 0a 2f 2a 20 54  ction[0]))../* T
31265 68 65 20 6e 65 78 74 20 74 61 62 6c 65 20 6d 61  he next table ma
31266 70 73 20 74 6f 6b 65 6e 73 20 69 6e 74 6f 20 66  ps tokens into f
31267 61 6c 6c 62 61 63 6b 20 74 6f 6b 65 6e 73 2e 20  allback tokens. 
31268 20 49 66 20 61 20 63 6f 6e 73 74 72 75 63 74 0a   If a construct.
31269 2a 2a 20 6c 69 6b 65 20 74 68 65 20 66 6f 6c 6c  ** like the foll
3126a 6f 77 69 6e 67 3a 0a 2a 2a 20 0a 2a 2a 20 20 20  owing:.** .**   
3126b 20 20 20 25 66 61 6c 6c 62 61 63 6b 20 49 44 20     %fallback ID 
3126c 58 20 59 20 5a 2e 0a 2a 2a 0a 2a 2a 20 61 70 70  X Y Z..**.** app
3126d 65 61 72 73 20 69 6e 20 74 68 65 20 67 72 61 6d  ears in the gram
3126e 6d 61 72 2c 20 74 68 65 6e 20 49 44 20 62 65 63  mar, then ID bec
3126f 6f 6d 65 73 20 61 20 66 61 6c 6c 62 61 63 6b 20  omes a fallback 
31270 74 6f 6b 65 6e 20 66 6f 72 20 58 2c 20 59 2c 0a  token for X, Y,.
31271 2a 2a 20 61 6e 64 20 5a 2e 20 20 57 68 65 6e 65  ** and Z.  Whene
31272 76 65 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 74  ver one of the t
31273 6f 6b 65 6e 73 20 58 2c 20 59 2c 20 6f 72 20 5a  okens X, Y, or Z
31274 20 69 73 20 69 6e 70 75 74 20 74 6f 20 74 68 65   is input to the
31275 20 70 61 72 73 65 72 0a 2a 2a 20 62 75 74 20 69   parser.** but i
31276 74 20 64 6f 65 73 20 6e 6f 74 20 70 61 72 73 65  t does not parse
31277 2c 20 74 68 65 20 74 79 70 65 20 6f 66 20 74 68  , the type of th
31278 65 20 74 6f 6b 65 6e 20 69 73 20 63 68 61 6e 67  e token is chang
31279 65 64 20 74 6f 20 49 44 20 61 6e 64 0a 2a 2a 20  ed to ID and.** 
3127a 74 68 65 20 70 61 72 73 65 20 69 73 20 72 65 74  the parse is ret
3127b 72 69 65 64 20 62 65 66 6f 72 65 20 61 6e 20 65  ried before an e
3127c 72 72 6f 72 20 69 73 20 74 68 72 6f 77 6e 2e 0a  rror is thrown..
3127d 2a 2f 0a 23 69 66 64 65 66 20 59 59 46 41 4c 4c  */.#ifdef YYFALL
3127e 42 41 43 4b 0a 73 74 61 74 69 63 20 63 6f 6e 73  BACK.static cons
3127f 74 20 59 59 43 4f 44 45 54 59 50 45 20 79 79 46  t YYCODETYPE yyF
31280 61 6c 6c 62 61 63 6b 5b 5d 20 3d 20 7b 0a 20 20  allback[] = {.  
31281 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20    0,  /*        
31282 20 20 24 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a    $ => nothing *
31283 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
31284 20 20 20 53 45 4d 49 20 3d 3e 20 6e 6f 74 68 69     SEMI => nothi
31285 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ng */.   26,  /*
31286 20 20 20 20 45 58 50 4c 41 49 4e 20 3d 3e 20 49      EXPLAIN => I
31287 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
31288 20 20 20 20 20 51 55 45 52 59 20 3d 3e 20 49 44       QUERY => ID
31289 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
3128a 20 20 20 20 20 50 4c 41 4e 20 3d 3e 20 49 44 20       PLAN => ID 
3128b 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
3128c 20 20 20 42 45 47 49 4e 20 3d 3e 20 49 44 20 2a     BEGIN => ID *
3128d 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 54 52 41  /.    0,  /* TRA
3128e 4e 53 41 43 54 49 4f 4e 20 3d 3e 20 6e 6f 74 68  NSACTION => noth
3128f 69 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f  ing */.   26,  /
31290 2a 20 20 20 44 45 46 45 52 52 45 44 20 3d 3e 20  *   DEFERRED => 
31291 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
31292 20 20 49 4d 4d 45 44 49 41 54 45 20 3d 3e 20 49    IMMEDIATE => I
31293 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
31294 20 45 58 43 4c 55 53 49 56 45 20 3d 3e 20 49 44   EXCLUSIVE => ID
31295 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
31296 20 20 20 43 4f 4d 4d 49 54 20 3d 3e 20 6e 6f 74     COMMIT => not
31297 68 69 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20  hing */.   26,  
31298 2f 2a 20 20 20 20 20 20 20 20 45 4e 44 20 3d 3e  /*        END =>
31299 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
3129a 2a 20 20 20 52 4f 4c 4c 42 41 43 4b 20 3d 3e 20  *   ROLLBACK => 
3129b 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
3129c 20 20 53 41 56 45 50 4f 49 4e 54 20 3d 3e 20 49    SAVEPOINT => I
3129d 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
3129e 20 20 20 52 45 4c 45 41 53 45 20 3d 3e 20 49 44     RELEASE => ID
3129f 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
312a0 20 20 20 20 20 20 20 54 4f 20 3d 3e 20 6e 6f 74         TO => not
312a1 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
312a2 2f 2a 20 20 20 20 20 20 54 41 42 4c 45 20 3d 3e  /*      TABLE =>
312a3 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20   nothing */.    
312a4 30 2c 20 20 2f 2a 20 20 20 20 20 43 52 45 41 54  0,  /*     CREAT
312a5 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  E => nothing */.
312a6 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20     26,  /*      
312a7 20 20 20 49 46 20 3d 3e 20 49 44 20 2a 2f 0a 20     IF => ID */. 
312a8 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
312a9 20 4e 4f 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20   NOT => nothing 
312aa 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
312ab 20 20 45 58 49 53 54 53 20 3d 3e 20 6e 6f 74 68    EXISTS => noth
312ac 69 6e 67 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f  ing */.   26,  /
312ad 2a 20 20 20 20 20 20 20 54 45 4d 50 20 3d 3e 20  *       TEMP => 
312ae 49 44 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ID */.    0,  /*
312af 20 20 20 20 20 20 20 20 20 4c 50 20 3d 3e 20 6e           LP => n
312b0 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
312b1 20 20 2f 2a 20 20 20 20 20 20 20 20 20 52 50 20    /*         RP 
312b2 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
312b3 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20 20    0,  /*        
312b4 20 41 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a   AS => nothing *
312b5 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
312b6 20 20 43 4f 4d 4d 41 20 3d 3e 20 6e 6f 74 68 69    COMMA => nothi
312b7 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
312b8 20 20 20 20 20 20 20 20 20 49 44 20 3d 3e 20 6e           ID => n
312b9 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
312ba 20 20 2f 2a 20 20 20 20 49 4e 44 45 58 45 44 20    /*    INDEXED 
312bb 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
312bc 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 41 42   26,  /*      AB
312bd 4f 52 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ORT => ID */.   
312be 32 36 2c 20 20 2f 2a 20 20 20 20 20 41 43 54 49  26,  /*     ACTI
312bf 4f 4e 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  ON => ID */.   2
312c0 36 2c 20 20 2f 2a 20 20 20 20 20 20 41 46 54 45  6,  /*      AFTE
312c1 52 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  R => ID */.   26
312c2 2c 20 20 2f 2a 20 20 20 20 41 4e 41 4c 59 5a 45  ,  /*    ANALYZE
312c3 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
312c4 20 20 2f 2a 20 20 20 20 20 20 20 20 41 53 43 20    /*        ASC 
312c5 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
312c6 20 2f 2a 20 20 20 20 20 41 54 54 41 43 48 20 3d   /*     ATTACH =
312c7 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
312c8 2f 2a 20 20 20 20 20 42 45 46 4f 52 45 20 3d 3e  /*     BEFORE =>
312c9 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
312ca 2a 20 20 20 20 20 20 20 20 20 42 59 20 3d 3e 20  *         BY => 
312cb 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
312cc 20 20 20 20 43 41 53 43 41 44 45 20 3d 3e 20 49      CASCADE => I
312cd 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
312ce 20 20 20 20 20 20 43 41 53 54 20 3d 3e 20 49 44        CAST => ID
312cf 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
312d0 20 43 4f 4c 55 4d 4e 4b 57 20 3d 3e 20 49 44 20   COLUMNKW => ID 
312d1 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
312d2 43 4f 4e 46 4c 49 43 54 20 3d 3e 20 49 44 20 2a  CONFLICT => ID *
312d3 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 44  /.   26,  /*   D
312d4 41 54 41 42 41 53 45 20 3d 3e 20 49 44 20 2a 2f  ATABASE => ID */
312d5 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20  .   26,  /*     
312d6 20 20 44 45 53 43 20 3d 3e 20 49 44 20 2a 2f 0a    DESC => ID */.
312d7 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 44     26,  /*     D
312d8 45 54 41 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20  ETACH => ID */. 
312d9 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20    26,  /*       
312da 45 41 43 48 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  EACH => ID */.  
312db 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 46   26,  /*       F
312dc 41 49 4c 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  AIL => ID */.   
312dd 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 46  26,  /*        F
312de 4f 52 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  OR => ID */.   2
312df 36 2c 20 20 2f 2a 20 20 20 20 20 49 47 4e 4f 52  6,  /*     IGNOR
312e0 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  E => ID */.   26
312e1 2c 20 20 2f 2a 20 20 49 4e 49 54 49 41 4c 4c 59  ,  /*  INITIALLY
312e2 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
312e3 20 20 2f 2a 20 20 20 20 49 4e 53 54 45 41 44 20    /*    INSTEAD 
312e4 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
312e5 20 2f 2a 20 20 20 20 4c 49 4b 45 5f 4b 57 20 3d   /*    LIKE_KW =
312e6 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
312e7 2f 2a 20 20 20 20 20 20 4d 41 54 43 48 20 3d 3e  /*      MATCH =>
312e8 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f   ID */.   26,  /
312e9 2a 20 20 20 20 20 20 20 20 20 4e 4f 20 3d 3e 20  *         NO => 
312ea 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a  ID */.   26,  /*
312eb 20 20 20 20 20 20 20 20 4b 45 59 20 3d 3e 20 49          KEY => I
312ec 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20  D */.   26,  /* 
312ed 20 20 20 20 20 20 20 20 4f 46 20 3d 3e 20 49 44          OF => ID
312ee 20 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20   */.   26,  /*  
312ef 20 20 20 4f 46 46 53 45 54 20 3d 3e 20 49 44 20     OFFSET => ID 
312f0 2a 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20  */.   26,  /*   
312f1 20 20 50 52 41 47 4d 41 20 3d 3e 20 49 44 20 2a    PRAGMA => ID *
312f2 2f 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20  /.   26,  /*    
312f3 20 20 52 41 49 53 45 20 3d 3e 20 49 44 20 2a 2f    RAISE => ID */
312f4 0a 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 52  .   26,  /*    R
312f5 45 50 4c 41 43 45 20 3d 3e 20 49 44 20 2a 2f 0a  EPLACE => ID */.
312f6 20 20 20 32 36 2c 20 20 2f 2a 20 20 20 52 45 53     26,  /*   RES
312f7 54 52 49 43 54 20 3d 3e 20 49 44 20 2a 2f 0a 20  TRICT => ID */. 
312f8 20 20 32 36 2c 20 20 2f 2a 20 20 20 20 20 20 20    26,  /*       
312f9 20 52 4f 57 20 3d 3e 20 49 44 20 2a 2f 0a 20 20   ROW => ID */.  
312fa 20 32 36 2c 20 20 2f 2a 20 20 20 20 54 52 49 47   26,  /*    TRIG
312fb 47 45 52 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  GER => ID */.   
312fc 32 36 2c 20 20 2f 2a 20 20 20 20 20 56 41 43 55  26,  /*     VACU
312fd 55 4d 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  UM => ID */.   2
312fe 36 2c 20 20 2f 2a 20 20 20 20 20 20 20 56 49 45  6,  /*       VIE
312ff 57 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36  W => ID */.   26
31300 2c 20 20 2f 2a 20 20 20 20 56 49 52 54 55 41 4c  ,  /*    VIRTUAL
31301 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c   => ID */.   26,
31302 20 20 2f 2a 20 20 20 20 52 45 49 4e 44 45 58 20    /*    REINDEX 
31303 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20  => ID */.   26, 
31304 20 2f 2a 20 20 20 20 20 52 45 4e 41 4d 45 20 3d   /*     RENAME =
31305 3e 20 49 44 20 2a 2f 0a 20 20 20 32 36 2c 20 20  > ID */.   26,  
31306 2f 2a 20 20 20 43 54 49 4d 45 5f 4b 57 20 3d 3e  /*   CTIME_KW =>
31307 20 49 44 20 2a 2f 0a 7d 3b 0a 23 65 6e 64 69 66   ID */.};.#endif
31308 20 2f 2a 20 59 59 46 41 4c 4c 42 41 43 4b 20 2a   /* YYFALLBACK *
31309 2f 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /../* The follow
3130a 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 72 65  ing structure re
3130b 70 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c  presents a singl
3130c 65 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  e element of the
3130d 0a 2a 2a 20 70 61 72 73 65 72 27 73 20 73 74 61  .** parser's sta
3130e 63 6b 2e 20 20 49 6e 66 6f 72 6d 61 74 69 6f 6e  ck.  Information
3130f 20 73 74 6f 72 65 64 20 69 6e 63 6c 75 64 65 73   stored includes
31310 3a 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 20 54 68 65  :.**.**   +  The
31311 20 73 74 61 74 65 20 6e 75 6d 62 65 72 20 66 6f   state number fo
31312 72 20 74 68 65 20 70 61 72 73 65 72 20 61 74 20  r the parser at 
31313 74 68 69 73 20 6c 65 76 65 6c 20 6f 66 20 74 68  this level of th
31314 65 20 73 74 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20  e stack..**.**  
31315 20 2b 20 20 54 68 65 20 76 61 6c 75 65 20 6f 66   +  The value of
31316 20 74 68 65 20 74 6f 6b 65 6e 20 73 74 6f 72 65   the token store
31317 64 20 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20  d at this level 
31318 6f 66 20 74 68 65 20 73 74 61 63 6b 2e 0a 2a 2a  of the stack..**
31319 20 20 20 20 20 20 28 49 6e 20 6f 74 68 65 72 20        (In other 
3131a 77 6f 72 64 73 2c 20 74 68 65 20 22 6d 61 6a 6f  words, the "majo
3131b 72 22 20 74 6f 6b 65 6e 2e 29 0a 2a 2a 0a 2a 2a  r" token.).**.**
3131c 20 20 20 2b 20 20 54 68 65 20 73 65 6d 61 6e 74     +  The semant
3131d 69 63 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20  ic value stored 
3131e 61 74 20 74 68 69 73 20 6c 65 76 65 6c 20 6f 66  at this level of
3131f 20 74 68 65 20 73 74 61 63 6b 2e 20 20 54 68 69   the stack.  Thi
31320 73 20 69 73 0a 2a 2a 20 20 20 20 20 20 74 68 65  s is.**      the
31321 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65   information use
31322 64 20 62 79 20 74 68 65 20 61 63 74 69 6f 6e 20  d by the action 
31323 72 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 65 20  routines in the 
31324 67 72 61 6d 6d 61 72 2e 0a 2a 2a 20 20 20 20 20  grammar..**     
31325 20 49 74 20 69 73 20 73 6f 6d 65 74 69 6d 65 73   It is sometimes
31326 20 63 61 6c 6c 65 64 20 74 68 65 20 22 6d 69 6e   called the "min
31327 6f 72 22 20 74 6f 6b 65 6e 2e 0a 2a 2f 0a 73 74  or" token..*/.st
31328 72 75 63 74 20 79 79 53 74 61 63 6b 45 6e 74 72  ruct yyStackEntr
31329 79 20 7b 0a 20 20 59 59 41 43 54 49 4f 4e 54 59  y {.  YYACTIONTY
3132a 50 45 20 73 74 61 74 65 6e 6f 3b 20 20 2f 2a 20  PE stateno;  /* 
3132b 54 68 65 20 73 74 61 74 65 2d 6e 75 6d 62 65 72  The state-number
3132c 20 2a 2f 0a 20 20 59 59 43 4f 44 45 54 59 50 45   */.  YYCODETYPE
3132d 20 6d 61 6a 6f 72 3b 20 20 20 20 20 20 2f 2a 20   major;      /* 
3132e 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20  The major token 
3132f 76 61 6c 75 65 2e 20 20 54 68 69 73 20 69 73 20  value.  This is 
31330 74 68 65 20 63 6f 64 65 0a 20 20 20 20 20 20 20  the code.       
31331 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31332 20 20 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20    ** number for 
31333 74 68 65 20 74 6f 6b 65 6e 20 61 74 20 74 68 69  the token at thi
31334 73 20 73 74 61 63 6b 20 6c 65 76 65 6c 20 2a 2f  s stack level */
31335 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 6d  .  YYMINORTYPE m
31336 69 6e 6f 72 3b 20 20 20 20 20 2f 2a 20 54 68 65  inor;     /* The
31337 20 75 73 65 72 2d 73 75 70 70 6c 69 65 64 20 6d   user-supplied m
31338 69 6e 6f 72 20 74 6f 6b 65 6e 20 76 61 6c 75 65  inor token value
31339 2e 20 20 54 68 69 73 0a 20 20 20 20 20 20 20 20  .  This.        
3133a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3133b 20 2a 2a 20 69 73 20 74 68 65 20 76 61 6c 75 65   ** is the value
3133c 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 20 2a   of the token  *
3133d 2f 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74 72  /.};.typedef str
3133e 75 63 74 20 79 79 53 74 61 63 6b 45 6e 74 72 79  uct yyStackEntry
3133f 20 79 79 53 74 61 63 6b 45 6e 74 72 79 3b 0a 0a   yyStackEntry;..
31340 2f 2a 20 54 68 65 20 73 74 61 74 65 20 6f 66 20  /* The state of 
31341 74 68 65 20 70 61 72 73 65 72 20 69 73 20 63 6f  the parser is co
31342 6d 70 6c 65 74 65 6c 79 20 63 6f 6e 74 61 69 6e  mpletely contain
31343 65 64 20 69 6e 20 61 6e 20 69 6e 73 74 61 6e 63  ed in an instanc
31344 65 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c  e of.** the foll
31345 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
31346 2a 2f 0a 73 74 72 75 63 74 20 79 79 50 61 72 73  */.struct yyPars
31347 65 72 20 7b 0a 20 20 69 6e 74 20 79 79 69 64 78  er {.  int yyidx
31348 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
31349 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
3134a 20 74 6f 70 20 65 6c 65 6d 65 6e 74 20 69 6e 20   top element in 
3134b 73 74 61 63 6b 20 2a 2f 0a 23 69 66 64 65 66 20  stack */.#ifdef 
3134c 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44  YYTRACKMAXSTACKD
3134d 45 50 54 48 0a 20 20 69 6e 74 20 79 79 69 64 78  EPTH.  int yyidx
3134e 4d 61 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Max;            
3134f 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
31350 76 61 6c 75 65 20 6f 66 20 79 79 69 64 78 20 2a  value of yyidx *
31351 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 79  /.#endif.  int y
31352 79 65 72 72 63 6e 74 3b 20 20 20 20 20 20 20 20  yerrcnt;        
31353 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 69 66           /* Shif
31354 74 73 20 6c 65 66 74 20 62 65 66 6f 72 65 20 6f  ts left before o
31355 75 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20  ut of the error 
31356 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73  */.  sqlite3Pars
31357 65 72 41 52 47 5f 53 44 45 43 4c 20 20 20 20 20  erARG_SDECL     
31358 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20             /* A 
31359 70 6c 61 63 65 20 74 6f 20 68 6f 6c 64 20 25 65  place to hold %e
3135a 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 2a 2f  xtra_argument */
3135b 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45 50 54  .#if YYSTACKDEPT
3135c 48 3c 3d 30 0a 20 20 69 6e 74 20 79 79 73 74 6b  H<=0.  int yystk
3135d 73 7a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  sz;             
3135e 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
3135f 73 69 64 65 20 6f 66 20 74 68 65 20 73 74 61 63  side of the stac
31360 6b 20 2a 2f 0a 20 20 79 79 53 74 61 63 6b 45 6e  k */.  yyStackEn
31361 74 72 79 20 2a 79 79 73 74 61 63 6b 3b 20 20 20  try *yystack;   
31362 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
31363 65 72 27 73 20 73 74 61 63 6b 20 2a 2f 0a 23 65  er's stack */.#e
31364 6c 73 65 0a 20 20 79 79 53 74 61 63 6b 45 6e 74  lse.  yyStackEnt
31365 72 79 20 79 79 73 74 61 63 6b 5b 59 59 53 54 41  ry yystack[YYSTA
31366 43 4b 44 45 50 54 48 5d 3b 20 20 2f 2a 20 54 68  CKDEPTH];  /* Th
31367 65 20 70 61 72 73 65 72 27 73 20 73 74 61 63 6b  e parser's stack
31368 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a 74 79   */.#endif.};.ty
31369 70 65 64 65 66 20 73 74 72 75 63 74 20 79 79 50  pedef struct yyP
3136a 61 72 73 65 72 20 79 79 50 61 72 73 65 72 3b 0a  arser yyParser;.
3136b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
3136c 73 74 61 74 69 63 20 46 49 4c 45 20 2a 79 79 54  static FILE *yyT
3136d 72 61 63 65 46 49 4c 45 20 3d 20 30 3b 0a 73 74  raceFILE = 0;.st
3136e 61 74 69 63 20 63 68 61 72 20 2a 79 79 54 72 61  atic char *yyTra
3136f 63 65 50 72 6f 6d 70 74 20 3d 20 30 3b 0a 23 65  cePrompt = 0;.#e
31370 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a  ndif /* NDEBUG *
31371 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  /..#ifndef NDEBU
31372 47 0a 2f 2a 20 0a 2a 2a 20 54 75 72 6e 20 70 61  G./* .** Turn pa
31373 72 73 65 72 20 74 72 61 63 69 6e 67 20 6f 6e 20  rser tracing on 
31374 62 79 20 67 69 76 69 6e 67 20 61 20 73 74 72 65  by giving a stre
31375 61 6d 20 74 6f 20 77 68 69 63 68 20 74 6f 20 77  am to which to w
31376 72 69 74 65 20 74 68 65 20 74 72 61 63 65 0a 2a  rite the trace.*
31377 2a 20 61 6e 64 20 61 20 70 72 6f 6d 70 74 20 74  * and a prompt t
31378 6f 20 70 72 65 66 61 63 65 20 65 61 63 68 20 74  o preface each t
31379 72 61 63 65 20 6d 65 73 73 61 67 65 2e 20 20 54  race message.  T
3137a 72 61 63 69 6e 67 20 69 73 20 74 75 72 6e 65 64  racing is turned
3137b 20 6f 66 66 0a 2a 2a 20 62 79 20 6d 61 6b 69 6e   off.** by makin
3137c 67 20 65 69 74 68 65 72 20 61 72 67 75 6d 65 6e  g either argumen
3137d 74 20 4e 55 4c 4c 20 0a 2a 2a 0a 2a 2a 20 49 6e  t NULL .**.** In
3137e 70 75 74 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a  puts:.** <ul>.**
3137f 20 3c 6c 69 3e 20 41 20 46 49 4c 45 2a 20 74 6f   <li> A FILE* to
31380 20 77 68 69 63 68 20 74 72 61 63 65 20 6f 75 74   which trace out
31381 70 75 74 20 73 68 6f 75 6c 64 20 62 65 20 77 72  put should be wr
31382 69 74 74 65 6e 2e 0a 2a 2a 20 20 20 20 20 20 49  itten..**      I
31383 66 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 72 61  f NULL, then tra
31384 63 69 6e 67 20 69 73 20 74 75 72 6e 65 64 20 6f  cing is turned o
31385 66 66 2e 0a 2a 2a 20 3c 6c 69 3e 20 41 20 70 72  ff..** <li> A pr
31386 65 66 69 78 20 73 74 72 69 6e 67 20 77 72 69 74  efix string writ
31387 74 65 6e 20 61 74 20 74 68 65 20 62 65 67 69 6e  ten at the begin
31388 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a 2a  ning of every.**
31389 20 20 20 20 20 20 6c 69 6e 65 20 6f 66 20 74 72        line of tr
3138a 61 63 65 20 6f 75 74 70 75 74 2e 20 20 49 66 20  ace output.  If 
3138b 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 72 61 63 69  NULL, then traci
3138c 6e 67 20 69 73 0a 2a 2a 20 20 20 20 20 20 74 75  ng is.**      tu
3138d 72 6e 65 64 20 6f 66 66 2e 0a 2a 2a 20 3c 2f 75  rned off..** </u
3138e 6c 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74 73  l>.**.** Outputs
3138f 3a 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53 51  :.** None..*/.SQ
31390 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
31391 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 54  d sqlite3ParserT
31392 72 61 63 65 28 46 49 4c 45 20 2a 54 72 61 63 65  race(FILE *Trace
31393 46 49 4c 45 2c 20 63 68 61 72 20 2a 7a 54 72 61  FILE, char *zTra
31394 63 65 50 72 6f 6d 70 74 29 7b 0a 20 20 79 79 54  cePrompt){.  yyT
31395 72 61 63 65 46 49 4c 45 20 3d 20 54 72 61 63 65  raceFILE = Trace
31396 46 49 4c 45 3b 0a 20 20 79 79 54 72 61 63 65 50  FILE;.  yyTraceP
31397 72 6f 6d 70 74 20 3d 20 7a 54 72 61 63 65 50 72  rompt = zTracePr
31398 6f 6d 70 74 3b 0a 20 20 69 66 28 20 79 79 54 72  ompt;.  if( yyTr
31399 61 63 65 46 49 4c 45 3d 3d 30 20 29 20 79 79 54  aceFILE==0 ) yyT
3139a 72 61 63 65 50 72 6f 6d 70 74 20 3d 20 30 3b 0a  racePrompt = 0;.
3139b 20 20 65 6c 73 65 20 69 66 28 20 79 79 54 72 61    else if( yyTra
3139c 63 65 50 72 6f 6d 70 74 3d 3d 30 20 29 20 79 79  cePrompt==0 ) yy
3139d 54 72 61 63 65 46 49 4c 45 20 3d 20 30 3b 0a 7d  TraceFILE = 0;.}
3139e 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42 55  .#endif /* NDEBU
3139f 47 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e 44  G */..#ifndef ND
313a0 45 42 55 47 0a 2f 2a 20 46 6f 72 20 74 72 61 63  EBUG./* For trac
313a1 69 6e 67 20 73 68 69 66 74 73 2c 20 74 68 65 20  ing shifts, the 
313a2 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20 74 65 72  names of all ter
313a3 6d 69 6e 61 6c 73 20 61 6e 64 20 6e 6f 6e 74 65  minals and nonte
313a4 72 6d 69 6e 61 6c 73 0a 2a 2a 20 61 72 65 20 72  rminals.** are r
313a5 65 71 75 69 72 65 64 2e 20 20 54 68 65 20 66 6f  equired.  The fo
313a6 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 73 75  llowing table su
313a7 70 70 6c 69 65 73 20 74 68 65 73 65 20 6e 61 6d  pplies these nam
313a8 65 73 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  es */.static con
313a9 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20 79  st char *const y
313aa 79 54 6f 6b 65 6e 4e 61 6d 65 5b 5d 20 3d 20 7b  yTokenName[] = {
313ab 20 0a 20 20 22 24 22 2c 20 20 20 20 20 20 20 20   .  "$",        
313ac 20 20 20 20 20 22 53 45 4d 49 22 2c 20 20 20 20       "SEMI",    
313ad 20 20 20 20 20 20 22 45 58 50 4c 41 49 4e 22 2c        "EXPLAIN",
313ae 20 20 20 20 20 20 20 22 51 55 45 52 59 22 2c 20         "QUERY", 
313af 20 20 20 20 20 20 0a 20 20 22 50 4c 41 4e 22 2c        .  "PLAN",
313b0 20 20 20 20 20 20 20 20 20 20 22 42 45 47 49 4e            "BEGIN
313b1 22 2c 20 20 20 20 20 20 20 20 20 22 54 52 41 4e  ",         "TRAN
313b2 53 41 43 54 49 4f 4e 22 2c 20 20 20 22 44 45 46  SACTION",   "DEF
313b3 45 52 52 45 44 22 2c 20 20 20 20 0a 20 20 22 49  ERRED",    .  "I
313b4 4d 4d 45 44 49 41 54 45 22 2c 20 20 20 20 20 22  MMEDIATE",     "
313b5 45 58 43 4c 55 53 49 56 45 22 2c 20 20 20 20 20  EXCLUSIVE",     
313b6 22 43 4f 4d 4d 49 54 22 2c 20 20 20 20 20 20 20  "COMMIT",       
313b7 20 22 45 4e 44 22 2c 20 20 20 20 20 20 20 20 20   "END",         
313b8 0a 20 20 22 52 4f 4c 4c 42 41 43 4b 22 2c 20 20  .  "ROLLBACK",  
313b9 20 20 20 20 22 53 41 56 45 50 4f 49 4e 54 22 2c      "SAVEPOINT",
313ba 20 20 20 20 20 22 52 45 4c 45 41 53 45 22 2c 20       "RELEASE", 
313bb 20 20 20 20 20 20 22 54 4f 22 2c 20 20 20 20 20        "TO",     
313bc 20 20 20 20 20 0a 20 20 22 54 41 42 4c 45 22 2c       .  "TABLE",
313bd 20 20 20 20 20 20 20 20 20 22 43 52 45 41 54 45           "CREATE
313be 22 2c 20 20 20 20 20 20 20 20 22 49 46 22 2c 20  ",        "IF", 
313bf 20 20 20 20 20 20 20 20 20 20 20 22 4e 4f 54 22             "NOT"
313c0 2c 20 20 20 20 20 20 20 20 20 0a 20 20 22 45 58  ,         .  "EX
313c1 49 53 54 53 22 2c 20 20 20 20 20 20 20 20 22 54  ISTS",        "T
313c2 45 4d 50 22 2c 20 20 20 20 20 20 20 20 20 20 22  EMP",          "
313c3 4c 50 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  LP",            
313c4 22 52 50 22 2c 20 20 20 20 20 20 20 20 20 20 0a  "RP",          .
313c5 20 20 22 41 53 22 2c 20 20 20 20 20 20 20 20 20    "AS",         
313c6 20 20 20 22 43 4f 4d 4d 41 22 2c 20 20 20 20 20     "COMMA",     
313c7 20 20 20 20 22 49 44 22 2c 20 20 20 20 20 20 20      "ID",       
313c8 20 20 20 20 20 22 49 4e 44 45 58 45 44 22 2c 20       "INDEXED", 
313c9 20 20 20 20 0a 20 20 22 41 42 4f 52 54 22 2c 20      .  "ABORT", 
313ca 20 20 20 20 20 20 20 20 22 41 43 54 49 4f 4e 22          "ACTION"
313cb 2c 20 20 20 20 20 20 20 20 22 41 46 54 45 52 22  ,        "AFTER"
313cc 2c 20 20 20 20 20 20 20 20 20 22 41 4e 41 4c 59  ,         "ANALY
313cd 5a 45 22 2c 20 20 20 20 20 0a 20 20 22 41 53 43  ZE",     .  "ASC
313ce 22 2c 20 20 20 20 20 20 20 20 20 20 20 22 41 54  ",           "AT
313cf 54 41 43 48 22 2c 20 20 20 20 20 20 20 20 22 42  TACH",        "B
313d0 45 46 4f 52 45 22 2c 20 20 20 20 20 20 20 20 22  EFORE",        "
313d1 42 59 22 2c 20 20 20 20 20 20 20 20 20 20 0a 20  BY",          . 
313d2 20 22 43 41 53 43 41 44 45 22 2c 20 20 20 20 20   "CASCADE",     
313d3 20 20 22 43 41 53 54 22 2c 20 20 20 20 20 20 20    "CAST",       
313d4 20 20 20 22 43 4f 4c 55 4d 4e 4b 57 22 2c 20 20     "COLUMNKW",  
313d5 20 20 20 20 22 43 4f 4e 46 4c 49 43 54 22 2c 20      "CONFLICT", 
313d6 20 20 20 0a 20 20 22 44 41 54 41 42 41 53 45 22     .  "DATABASE"
313d7 2c 20 20 20 20 20 20 22 44 45 53 43 22 2c 20 20  ,      "DESC",  
313d8 20 20 20 20 20 20 20 20 22 44 45 54 41 43 48 22          "DETACH"
313d9 2c 20 20 20 20 20 20 20 20 22 45 41 43 48 22 2c  ,        "EACH",
313da 20 20 20 20 20 20 20 20 0a 20 20 22 46 41 49 4c          .  "FAIL
313db 22 2c 20 20 20 20 20 20 20 20 20 20 22 46 4f 52  ",          "FOR
313dc 22 2c 20 20 20 20 20 20 20 20 20 20 20 22 49 47  ",           "IG
313dd 4e 4f 52 45 22 2c 20 20 20 20 20 20 20 20 22 49  NORE",        "I
313de 4e 49 54 49 41 4c 4c 59 22 2c 20 20 20 0a 20 20  NITIALLY",   .  
313df 22 49 4e 53 54 45 41 44 22 2c 20 20 20 20 20 20  "INSTEAD",      
313e0 20 22 4c 49 4b 45 5f 4b 57 22 2c 20 20 20 20 20   "LIKE_KW",     
313e1 20 20 22 4d 41 54 43 48 22 2c 20 20 20 20 20 20    "MATCH",      
313e2 20 20 20 22 4e 4f 22 2c 20 20 20 20 20 20 20 20     "NO",        
313e3 20 20 0a 20 20 22 4b 45 59 22 2c 20 20 20 20 20    .  "KEY",     
313e4 20 20 20 20 20 20 22 4f 46 22 2c 20 20 20 20 20        "OF",     
313e5 20 20 20 20 20 20 20 22 4f 46 46 53 45 54 22 2c         "OFFSET",
313e6 20 20 20 20 20 20 20 20 22 50 52 41 47 4d 41 22          "PRAGMA"
313e7 2c 20 20 20 20 20 20 0a 20 20 22 52 41 49 53 45  ,      .  "RAISE
313e8 22 2c 20 20 20 20 20 20 20 20 20 22 52 45 50 4c  ",         "REPL
313e9 41 43 45 22 2c 20 20 20 20 20 20 20 22 52 45 53  ACE",       "RES
313ea 54 52 49 43 54 22 2c 20 20 20 20 20 20 22 52 4f  TRICT",      "RO
313eb 57 22 2c 20 20 20 20 20 20 20 20 20 0a 20 20 22  W",         .  "
313ec 54 52 49 47 47 45 52 22 2c 20 20 20 20 20 20 20  TRIGGER",       
313ed 22 56 41 43 55 55 4d 22 2c 20 20 20 20 20 20 20  "VACUUM",       
313ee 20 22 56 49 45 57 22 2c 20 20 20 20 20 20 20 20   "VIEW",        
313ef 20 20 22 56 49 52 54 55 41 4c 22 2c 20 20 20 20    "VIRTUAL",    
313f0 20 0a 20 20 22 52 45 49 4e 44 45 58 22 2c 20 20   .  "REINDEX",  
313f1 20 20 20 20 20 22 52 45 4e 41 4d 45 22 2c 20 20       "RENAME",  
313f2 20 20 20 20 20 20 22 43 54 49 4d 45 5f 4b 57 22        "CTIME_KW"
313f3 2c 20 20 20 20 20 20 22 41 4e 59 22 2c 20 20 20  ,      "ANY",   
313f4 20 20 20 20 20 20 0a 20 20 22 4f 52 22 2c 20 20        .  "OR",  
313f5 20 20 20 20 20 20 20 20 20 20 22 41 4e 44 22 2c            "AND",
313f6 20 20 20 20 20 20 20 20 20 20 20 22 49 53 22 2c             "IS",
313f7 20 20 20 20 20 20 20 20 20 20 20 20 22 42 45 54              "BET
313f8 57 45 45 4e 22 2c 20 20 20 20 20 0a 20 20 22 49  WEEN",     .  "I
313f9 4e 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  N",            "
313fa 49 53 4e 55 4c 4c 22 2c 20 20 20 20 20 20 20 20  ISNULL",        
313fb 22 4e 4f 54 4e 55 4c 4c 22 2c 20 20 20 20 20 20  "NOTNULL",      
313fc 20 22 4e 45 22 2c 20 20 20 20 20 20 20 20 20 20   "NE",          
313fd 0a 20 20 22 45 51 22 2c 20 20 20 20 20 20 20 20  .  "EQ",        
313fe 20 20 20 20 22 47 54 22 2c 20 20 20 20 20 20 20      "GT",       
313ff 20 20 20 20 20 22 4c 45 22 2c 20 20 20 20 20 20       "LE",      
31400 20 20 20 20 20 20 22 4c 54 22 2c 20 20 20 20 20        "LT",     
31401 20 20 20 20 20 0a 20 20 22 47 45 22 2c 20 20 20       .  "GE",   
31402 20 20 20 20 20 20 20 20 20 22 45 53 43 41 50 45           "ESCAPE
31403 22 2c 20 20 20 20 20 20 20 20 22 42 49 54 41 4e  ",        "BITAN
31404 44 22 2c 20 20 20 20 20 20 20 20 22 42 49 54 4f  D",        "BITO
31405 52 22 2c 20 20 20 20 20 20 20 0a 20 20 22 4c 53  R",       .  "LS
31406 48 49 46 54 22 2c 20 20 20 20 20 20 20 20 22 52  HIFT",        "R
31407 53 48 49 46 54 22 2c 20 20 20 20 20 20 20 20 22  SHIFT",        "
31408 50 4c 55 53 22 2c 20 20 20 20 20 20 20 20 20 20  PLUS",          
31409 22 4d 49 4e 55 53 22 2c 20 20 20 20 20 20 20 0a  "MINUS",       .
3140a 20 20 22 53 54 41 52 22 2c 20 20 20 20 20 20 20    "STAR",       
3140b 20 20 20 22 53 4c 41 53 48 22 2c 20 20 20 20 20     "SLASH",     
3140c 20 20 20 20 22 52 45 4d 22 2c 20 20 20 20 20 20      "REM",      
3140d 20 20 20 20 20 22 43 4f 4e 43 41 54 22 2c 20 20       "CONCAT",  
3140e 20 20 20 20 0a 20 20 22 43 4f 4c 4c 41 54 45 22      .  "COLLATE"
3140f 2c 20 20 20 20 20 20 20 22 42 49 54 4e 4f 54 22  ,       "BITNOT"
31410 2c 20 20 20 20 20 20 20 20 22 53 54 52 49 4e 47  ,        "STRING
31411 22 2c 20 20 20 20 20 20 20 20 22 4a 4f 49 4e 5f  ",        "JOIN_
31412 4b 57 22 2c 20 20 20 20 20 0a 20 20 22 43 4f 4e  KW",     .  "CON
31413 53 54 52 41 49 4e 54 22 2c 20 20 20 20 22 44 45  STRAINT",    "DE
31414 46 41 55 4c 54 22 2c 20 20 20 20 20 20 20 22 4e  FAULT",       "N
31415 55 4c 4c 22 2c 20 20 20 20 20 20 20 20 20 20 22  ULL",          "
31416 50 52 49 4d 41 52 59 22 2c 20 20 20 20 20 0a 20  PRIMARY",     . 
31417 20 22 55 4e 49 51 55 45 22 2c 20 20 20 20 20 20   "UNIQUE",      
31418 20 20 22 43 48 45 43 4b 22 2c 20 20 20 20 20 20    "CHECK",      
31419 20 20 20 22 52 45 46 45 52 45 4e 43 45 53 22 2c     "REFERENCES",
3141a 20 20 20 20 22 41 55 54 4f 49 4e 43 52 22 2c 20      "AUTOINCR", 
3141b 20 20 20 0a 20 20 22 4f 4e 22 2c 20 20 20 20 20     .  "ON",     
3141c 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 22 2c         "DELETE",
3141d 20 20 20 20 20 20 20 20 22 55 50 44 41 54 45 22          "UPDATE"
3141e 2c 20 20 20 20 20 20 20 20 22 53 45 54 22 2c 20  ,        "SET", 
3141f 20 20 20 20 20 20 20 20 0a 20 20 22 44 45 46 45          .  "DEFE
31420 52 52 41 42 4c 45 22 2c 20 20 20 20 22 46 4f 52  RRABLE",    "FOR
31421 45 49 47 4e 22 2c 20 20 20 20 20 20 20 22 44 52  EIGN",       "DR
31422 4f 50 22 2c 20 20 20 20 20 20 20 20 20 20 22 55  OP",          "U
31423 4e 49 4f 4e 22 2c 20 20 20 20 20 20 20 0a 20 20  NION",       .  
31424 22 41 4c 4c 22 2c 20 20 20 20 20 20 20 20 20 20  "ALL",          
31425 20 22 45 58 43 45 50 54 22 2c 20 20 20 20 20 20   "EXCEPT",      
31426 20 20 22 49 4e 54 45 52 53 45 43 54 22 2c 20 20    "INTERSECT",  
31427 20 20 20 22 53 45 4c 45 43 54 22 2c 20 20 20 20     "SELECT",    
31428 20 20 0a 20 20 22 44 49 53 54 49 4e 43 54 22 2c    .  "DISTINCT",
31429 20 20 20 20 20 20 22 44 4f 54 22 2c 20 20 20 20        "DOT",    
3142a 20 20 20 20 20 20 20 22 46 52 4f 4d 22 2c 20 20         "FROM",  
3142b 20 20 20 20 20 20 20 20 22 4a 4f 49 4e 22 2c 20          "JOIN", 
3142c 20 20 20 20 20 20 20 0a 20 20 22 55 53 49 4e 47         .  "USING
3142d 22 2c 20 20 20 20 20 20 20 20 20 22 4f 52 44 45  ",         "ORDE
3142e 52 22 2c 20 20 20 20 20 20 20 20 20 22 47 52 4f  R",         "GRO
3142f 55 50 22 2c 20 20 20 20 20 20 20 20 20 22 48 41  UP",         "HA
31430 56 49 4e 47 22 2c 20 20 20 20 20 20 0a 20 20 22  VING",      .  "
31431 4c 49 4d 49 54 22 2c 20 20 20 20 20 20 20 20 20  LIMIT",         
31432 22 57 48 45 52 45 22 2c 20 20 20 20 20 20 20 20  "WHERE",        
31433 20 22 49 4e 54 4f 22 2c 20 20 20 20 20 20 20 20   "INTO",        
31434 20 20 22 56 41 4c 55 45 53 22 2c 20 20 20 20 20    "VALUES",     
31435 20 0a 20 20 22 49 4e 53 45 52 54 22 2c 20 20 20   .  "INSERT",   
31436 20 20 20 20 20 22 49 4e 54 45 47 45 52 22 2c 20       "INTEGER", 
31437 20 20 20 20 20 20 22 46 4c 4f 41 54 22 2c 20 20        "FLOAT",  
31438 20 20 20 20 20 20 20 22 42 4c 4f 42 22 2c 20 20         "BLOB",  
31439 20 20 20 20 20 20 0a 20 20 22 52 45 47 49 53 54        .  "REGIST
3143a 45 52 22 2c 20 20 20 20 20 20 22 56 41 52 49 41  ER",      "VARIA
3143b 42 4c 45 22 2c 20 20 20 20 20 20 22 43 41 53 45  BLE",      "CASE
3143c 22 2c 20 20 20 20 20 20 20 20 20 20 22 57 48 45  ",          "WHE
3143d 4e 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 54  N",        .  "T
3143e 48 45 4e 22 2c 20 20 20 20 20 20 20 20 20 20 22  HEN",          "
3143f 45 4c 53 45 22 2c 20 20 20 20 20 20 20 20 20 20  ELSE",          
31440 22 49 4e 44 45 58 22 2c 20 20 20 20 20 20 20 20  "INDEX",        
31441 20 22 41 4c 54 45 52 22 2c 20 20 20 20 20 20 20   "ALTER",       
31442 0a 20 20 22 41 44 44 22 2c 20 20 20 20 20 20 20  .  "ADD",       
31443 20 20 20 20 22 65 72 72 6f 72 22 2c 20 20 20 20      "error",    
31444 20 20 20 20 20 22 69 6e 70 75 74 22 2c 20 20 20       "input",   
31445 20 20 20 20 20 20 22 63 6d 64 6c 69 73 74 22 2c        "cmdlist",
31446 20 20 20 20 20 0a 20 20 22 65 63 6d 64 22 2c 20       .  "ecmd", 
31447 20 20 20 20 20 20 20 20 20 22 65 78 70 6c 61 69           "explai
31448 6e 22 2c 20 20 20 20 20 20 20 22 63 6d 64 78 22  n",       "cmdx"
31449 2c 20 20 20 20 20 20 20 20 20 20 22 63 6d 64 22  ,          "cmd"
3144a 2c 20 20 20 20 20 20 20 20 20 0a 20 20 22 74 72  ,         .  "tr
3144b 61 6e 73 74 79 70 65 22 2c 20 20 20 20 20 22 74  anstype",     "t
3144c 72 61 6e 73 5f 6f 70 74 22 2c 20 20 20 20 20 22  rans_opt",     "
3144d 6e 6d 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  nm",            
3144e 22 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 22 2c  "savepoint_opt",
3144f 0a 20 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65  .  "create_table
31450 22 2c 20 20 22 63 72 65 61 74 65 5f 74 61 62 6c  ",  "create_tabl
31451 65 5f 61 72 67 73 22 2c 20 20 22 63 72 65 61 74  e_args",  "creat
31452 65 6b 77 22 2c 20 20 20 20 20 20 22 74 65 6d 70  ekw",      "temp
31453 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 69 66  ",        .  "if
31454 6e 6f 74 65 78 69 73 74 73 22 2c 20 20 20 22 64  notexists",   "d
31455 62 6e 6d 22 2c 20 20 20 20 20 20 20 20 20 20 22  bnm",          "
31456 63 6f 6c 75 6d 6e 6c 69 73 74 22 2c 20 20 20 20  columnlist",    
31457 22 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 22 2c 0a  "conslist_opt",.
31458 20 20 22 73 65 6c 65 63 74 22 2c 20 20 20 20 20    "select",     
31459 20 20 20 22 63 6f 6c 75 6d 6e 22 2c 20 20 20 20     "column",    
3145a 20 20 20 20 22 63 6f 6c 75 6d 6e 69 64 22 2c 20      "columnid", 
3145b 20 20 20 20 20 22 74 79 70 65 22 2c 20 20 20 20       "type",    
3145c 20 20 20 20 0a 20 20 22 63 61 72 67 6c 69 73 74      .  "carglist
3145d 22 2c 20 20 20 20 20 20 22 69 64 22 2c 20 20 20  ",      "id",   
3145e 20 20 20 20 20 20 20 20 20 22 69 64 73 22 2c 20           "ids", 
3145f 20 20 20 20 20 20 20 20 20 20 22 74 79 70 65 74            "typet
31460 6f 6b 65 6e 22 2c 20 20 20 0a 20 20 22 74 79 70  oken",   .  "typ
31461 65 6e 61 6d 65 22 2c 20 20 20 20 20 20 22 73 69  ename",      "si
31462 67 6e 65 64 22 2c 20 20 20 20 20 20 20 20 22 70  gned",        "p
31463 6c 75 73 5f 6e 75 6d 22 2c 20 20 20 20 20 20 22  lus_num",      "
31464 6d 69 6e 75 73 5f 6e 75 6d 22 2c 20 20 20 0a 20  minus_num",   . 
31465 20 22 63 61 72 67 22 2c 20 20 20 20 20 20 20 20   "carg",        
31466 20 20 22 63 63 6f 6e 73 22 2c 20 20 20 20 20 20    "ccons",      
31467 20 20 20 22 74 65 72 6d 22 2c 20 20 20 20 20 20     "term",      
31468 20 20 20 20 22 65 78 70 72 22 2c 20 20 20 20 20      "expr",     
31469 20 20 20 0a 20 20 22 6f 6e 63 6f 6e 66 22 2c 20     .  "onconf", 
3146a 20 20 20 20 20 20 20 22 73 6f 72 74 6f 72 64 65         "sortorde
3146b 72 22 2c 20 20 20 20 20 22 61 75 74 6f 69 6e 63  r",     "autoinc
3146c 22 2c 20 20 20 20 20 20 20 22 69 64 78 6c 69 73  ",       "idxlis
3146d 74 5f 6f 70 74 22 2c 20 0a 20 20 22 72 65 66 61  t_opt", .  "refa
3146e 72 67 73 22 2c 20 20 20 20 20 20 20 22 64 65 66  rgs",       "def
3146f 65 72 5f 73 75 62 63 6c 61 75 73 65 22 2c 20 20  er_subclause",  
31470 22 72 65 66 61 72 67 22 2c 20 20 20 20 20 20 20  "refarg",       
31471 20 22 72 65 66 61 63 74 22 2c 20 20 20 20 20 20   "refact",      
31472 0a 20 20 22 69 6e 69 74 5f 64 65 66 65 72 72 65  .  "init_deferre
31473 64 5f 70 72 65 64 5f 6f 70 74 22 2c 20 20 22 63  d_pred_opt",  "c
31474 6f 6e 73 6c 69 73 74 22 2c 20 20 20 20 20 20 22  onslist",      "
31475 74 63 6f 6e 73 22 2c 20 20 20 20 20 20 20 20 20  tcons",         
31476 22 69 64 78 6c 69 73 74 22 2c 20 20 20 20 20 0a  "idxlist",     .
31477 20 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75    "defer_subclau
31478 73 65 5f 6f 70 74 22 2c 20 20 22 6f 72 63 6f 6e  se_opt",  "orcon
31479 66 22 2c 20 20 20 20 20 20 20 20 22 72 65 73 6f  f",        "reso
3147a 6c 76 65 74 79 70 65 22 2c 20 20 20 22 72 61 69  lvetype",   "rai
3147b 73 65 74 79 70 65 22 2c 20 20 20 0a 20 20 22 69  setype",   .  "i
3147c 66 65 78 69 73 74 73 22 2c 20 20 20 20 20 20 22  fexists",      "
3147d 66 75 6c 6c 6e 61 6d 65 22 2c 20 20 20 20 20 20  fullname",      
3147e 22 6f 6e 65 73 65 6c 65 63 74 22 2c 20 20 20 20  "oneselect",    
3147f 20 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70   "multiselect_op
31480 22 2c 0a 20 20 22 64 69 73 74 69 6e 63 74 22 2c  ",.  "distinct",
31481 20 20 20 20 20 20 22 73 65 6c 63 6f 6c 6c 69 73        "selcollis
31482 74 22 2c 20 20 20 20 22 66 72 6f 6d 22 2c 20 20  t",    "from",  
31483 20 20 20 20 20 20 20 20 22 77 68 65 72 65 5f 6f          "where_o
31484 70 74 22 2c 20 20 20 0a 20 20 22 67 72 6f 75 70  pt",   .  "group
31485 62 79 5f 6f 70 74 22 2c 20 20 20 22 68 61 76 69  by_opt",   "havi
31486 6e 67 5f 6f 70 74 22 2c 20 20 20 20 22 6f 72 64  ng_opt",    "ord
31487 65 72 62 79 5f 6f 70 74 22 2c 20 20 20 22 6c 69  erby_opt",   "li
31488 6d 69 74 5f 6f 70 74 22 2c 20 20 20 0a 20 20 22  mit_opt",   .  "
31489 73 63 6c 70 22 2c 20 20 20 20 20 20 20 20 20 20  sclp",          
3148a 22 61 73 22 2c 20 20 20 20 20 20 20 20 20 20 20  "as",           
3148b 20 22 73 65 6c 74 61 62 6c 69 73 74 22 2c 20 20   "seltablist",  
3148c 20 20 22 73 74 6c 5f 70 72 65 66 69 78 22 2c 20    "stl_prefix", 
3148d 20 0a 20 20 22 6a 6f 69 6e 6f 70 22 2c 20 20 20   .  "joinop",   
3148e 20 20 20 20 20 22 69 6e 64 65 78 65 64 5f 6f 70       "indexed_op
3148f 74 22 2c 20 20 20 22 6f 6e 5f 6f 70 74 22 2c 20  t",   "on_opt", 
31490 20 20 20 20 20 20 20 22 75 73 69 6e 67 5f 6f 70         "using_op
31491 74 22 2c 20 20 20 0a 20 20 22 6a 6f 69 6e 6f 70  t",   .  "joinop
31492 32 22 2c 20 20 20 20 20 20 20 22 69 6e 73 63 6f  2",       "insco
31493 6c 6c 69 73 74 22 2c 20 20 20 20 22 73 6f 72 74  llist",    "sort
31494 6c 69 73 74 22 2c 20 20 20 20 20 20 22 73 6f 72  list",      "sor
31495 74 69 74 65 6d 22 2c 20 20 20 20 0a 20 20 22 6e  titem",    .  "n
31496 65 78 70 72 6c 69 73 74 22 2c 20 20 20 20 20 22  exprlist",     "
31497 73 65 74 6c 69 73 74 22 2c 20 20 20 20 20 20 20  setlist",       
31498 22 69 6e 73 65 72 74 5f 63 6d 64 22 2c 20 20 20  "insert_cmd",   
31499 20 22 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74   "inscollist_opt
3149a 22 2c 0a 20 20 22 69 74 65 6d 6c 69 73 74 22 2c  ",.  "itemlist",
3149b 20 20 20 20 20 20 22 65 78 70 72 6c 69 73 74 22        "exprlist"
3149c 2c 20 20 20 20 20 20 22 6c 69 6b 65 6f 70 22 2c  ,      "likeop",
3149d 20 20 20 20 20 20 20 20 22 65 73 63 61 70 65 22          "escape"
3149e 2c 20 20 20 20 20 20 0a 20 20 22 62 65 74 77 65  ,      .  "betwe
3149f 65 6e 5f 6f 70 22 2c 20 20 20 20 22 69 6e 5f 6f  en_op",    "in_o
314a0 70 22 2c 20 20 20 20 20 20 20 20 20 22 63 61 73  p",         "cas
314a1 65 5f 6f 70 65 72 61 6e 64 22 2c 20 20 22 63 61  e_operand",  "ca
314a2 73 65 5f 65 78 70 72 6c 69 73 74 22 2c 0a 20 20  se_exprlist",.  
314a3 22 63 61 73 65 5f 65 6c 73 65 22 2c 20 20 20 20  "case_else",    
314a4 20 22 75 6e 69 71 75 65 66 6c 61 67 22 2c 20 20   "uniqueflag",  
314a5 20 20 22 63 6f 6c 6c 61 74 65 22 2c 20 20 20 20    "collate",    
314a6 20 20 20 22 6e 6d 6e 75 6d 22 2c 20 20 20 20 20     "nmnum",     
314a7 20 20 0a 20 20 22 70 6c 75 73 5f 6f 70 74 22 2c    .  "plus_opt",
314a8 20 20 20 20 20 20 22 6e 75 6d 62 65 72 22 2c 20        "number", 
314a9 20 20 20 20 20 20 20 22 74 72 69 67 67 65 72 5f         "trigger_
314aa 64 65 63 6c 22 2c 20 20 22 74 72 69 67 67 65 72  decl",  "trigger
314ab 5f 63 6d 64 5f 6c 69 73 74 22 2c 0a 20 20 22 74  _cmd_list",.  "t
314ac 72 69 67 67 65 72 5f 74 69 6d 65 22 2c 20 20 22  rigger_time",  "
314ad 74 72 69 67 67 65 72 5f 65 76 65 6e 74 22 2c 20  trigger_event", 
314ae 20 22 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65   "foreach_clause
314af 22 2c 20 20 22 77 68 65 6e 5f 63 6c 61 75 73 65  ",  "when_clause
314b0 22 2c 20 0a 20 20 22 74 72 69 67 67 65 72 5f 63  ", .  "trigger_c
314b1 6d 64 22 2c 20 20 20 22 74 72 6e 6d 22 2c 20 20  md",   "trnm",  
314b2 20 20 20 20 20 20 20 20 22 74 72 69 64 78 62 79          "tridxby
314b3 22 2c 20 20 20 20 20 20 20 22 64 61 74 61 62 61  ",       "databa
314b4 73 65 5f 6b 77 5f 6f 70 74 22 2c 0a 20 20 22 6b  se_kw_opt",.  "k
314b5 65 79 5f 6f 70 74 22 2c 20 20 20 20 20 20 20 22  ey_opt",       "
314b6 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e  add_column_fulln
314b7 61 6d 65 22 2c 20 20 22 6b 77 63 6f 6c 75 6d 6e  ame",  "kwcolumn
314b8 5f 6f 70 74 22 2c 20 20 22 63 72 65 61 74 65 5f  _opt",  "create_
314b9 76 74 61 62 22 2c 20 0a 20 20 22 76 74 61 62 61  vtab", .  "vtaba
314ba 72 67 6c 69 73 74 22 2c 20 20 20 22 76 74 61 62  rglist",   "vtab
314bb 61 72 67 22 2c 20 20 20 20 20 20 20 22 76 74 61  arg",       "vta
314bc 62 61 72 67 74 6f 6b 65 6e 22 2c 20 20 22 6c 70  bargtoken",  "lp
314bd 22 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20 22  ",          .  "
314be 61 6e 79 6c 69 73 74 22 2c 20 20 20 20 20 0a 7d  anylist",     .}
314bf 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44 45 42  ;.#endif /* NDEB
314c0 55 47 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 4e  UG */..#ifndef N
314c1 44 45 42 55 47 0a 2f 2a 20 46 6f 72 20 74 72 61  DEBUG./* For tra
314c2 63 69 6e 67 20 72 65 64 75 63 65 20 61 63 74 69  cing reduce acti
314c3 6f 6e 73 2c 20 74 68 65 20 6e 61 6d 65 73 20 6f  ons, the names o
314c4 66 20 61 6c 6c 20 72 75 6c 65 73 20 61 72 65 20  f all rules are 
314c5 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74 61  required..*/.sta
314c6 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
314c7 63 6f 6e 73 74 20 79 79 52 75 6c 65 4e 61 6d 65  const yyRuleName
314c8 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20 20 30 20 2a  [] = {. /*   0 *
314c9 2f 20 22 69 6e 70 75 74 20 3a 3a 3d 20 63 6d 64  / "input ::= cmd
314ca 6c 69 73 74 22 2c 0a 20 2f 2a 20 20 20 31 20 2a  list",. /*   1 *
314cb 2f 20 22 63 6d 64 6c 69 73 74 20 3a 3a 3d 20 63  / "cmdlist ::= c
314cc 6d 64 6c 69 73 74 20 65 63 6d 64 22 2c 0a 20 2f  mdlist ecmd",. /
314cd 2a 20 20 20 32 20 2a 2f 20 22 63 6d 64 6c 69 73  *   2 */ "cmdlis
314ce 74 20 3a 3a 3d 20 65 63 6d 64 22 2c 0a 20 2f 2a  t ::= ecmd",. /*
314cf 20 20 20 33 20 2a 2f 20 22 65 63 6d 64 20 3a 3a     3 */ "ecmd ::
314d0 3d 20 53 45 4d 49 22 2c 0a 20 2f 2a 20 20 20 34  = SEMI",. /*   4
314d1 20 2a 2f 20 22 65 63 6d 64 20 3a 3a 3d 20 65 78   */ "ecmd ::= ex
314d2 70 6c 61 69 6e 20 63 6d 64 78 20 53 45 4d 49 22  plain cmdx SEMI"
314d3 2c 0a 20 2f 2a 20 20 20 35 20 2a 2f 20 22 65 78  ,. /*   5 */ "ex
314d4 70 6c 61 69 6e 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  plain ::=",. /* 
314d5 20 20 36 20 2a 2f 20 22 65 78 70 6c 61 69 6e 20    6 */ "explain 
314d6 3a 3a 3d 20 45 58 50 4c 41 49 4e 22 2c 0a 20 2f  ::= EXPLAIN",. /
314d7 2a 20 20 20 37 20 2a 2f 20 22 65 78 70 6c 61 69  *   7 */ "explai
314d8 6e 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 20 51 55  n ::= EXPLAIN QU
314d9 45 52 59 20 50 4c 41 4e 22 2c 0a 20 2f 2a 20 20  ERY PLAN",. /*  
314da 20 38 20 2a 2f 20 22 63 6d 64 78 20 3a 3a 3d 20   8 */ "cmdx ::= 
314db 63 6d 64 22 2c 0a 20 2f 2a 20 20 20 39 20 2a 2f  cmd",. /*   9 */
314dc 20 22 63 6d 64 20 3a 3a 3d 20 42 45 47 49 4e 20   "cmd ::= BEGIN 
314dd 74 72 61 6e 73 74 79 70 65 20 74 72 61 6e 73 5f  transtype trans_
314de 6f 70 74 22 2c 0a 20 2f 2a 20 20 31 30 20 2a 2f  opt",. /*  10 */
314df 20 22 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 22   "trans_opt ::="
314e0 2c 0a 20 2f 2a 20 20 31 31 20 2a 2f 20 22 74 72  ,. /*  11 */ "tr
314e1 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54 52 41 4e  ans_opt ::= TRAN
314e2 53 41 43 54 49 4f 4e 22 2c 0a 20 2f 2a 20 20 31  SACTION",. /*  1
314e3 32 20 2a 2f 20 22 74 72 61 6e 73 5f 6f 70 74 20  2 */ "trans_opt 
314e4 3a 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f 4e 20  ::= TRANSACTION 
314e5 6e 6d 22 2c 0a 20 2f 2a 20 20 31 33 20 2a 2f 20  nm",. /*  13 */ 
314e6 22 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 22 2c  "transtype ::=",
314e7 0a 20 2f 2a 20 20 31 34 20 2a 2f 20 22 74 72 61  . /*  14 */ "tra
314e8 6e 73 74 79 70 65 20 3a 3a 3d 20 44 45 46 45 52  nstype ::= DEFER
314e9 52 45 44 22 2c 0a 20 2f 2a 20 20 31 35 20 2a 2f  RED",. /*  15 */
314ea 20 22 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20   "transtype ::= 
314eb 49 4d 4d 45 44 49 41 54 45 22 2c 0a 20 2f 2a 20  IMMEDIATE",. /* 
314ec 20 31 36 20 2a 2f 20 22 74 72 61 6e 73 74 79 70   16 */ "transtyp
314ed 65 20 3a 3a 3d 20 45 58 43 4c 55 53 49 56 45 22  e ::= EXCLUSIVE"
314ee 2c 0a 20 2f 2a 20 20 31 37 20 2a 2f 20 22 63 6d  ,. /*  17 */ "cm
314ef 64 20 3a 3a 3d 20 43 4f 4d 4d 49 54 20 74 72 61  d ::= COMMIT tra
314f0 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 31 38  ns_opt",. /*  18
314f1 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 45 4e 44   */ "cmd ::= END
314f2 20 74 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f 2a   trans_opt",. /*
314f3 20 20 31 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d    19 */ "cmd ::=
314f4 20 52 4f 4c 4c 42 41 43 4b 20 74 72 61 6e 73 5f   ROLLBACK trans_
314f5 6f 70 74 22 2c 0a 20 2f 2a 20 20 32 30 20 2a 2f  opt",. /*  20 */
314f6 20 22 73 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20   "savepoint_opt 
314f7 3a 3a 3d 20 53 41 56 45 50 4f 49 4e 54 22 2c 0a  ::= SAVEPOINT",.
314f8 20 2f 2a 20 20 32 31 20 2a 2f 20 22 73 61 76 65   /*  21 */ "save
314f9 70 6f 69 6e 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a  point_opt ::=",.
314fa 20 2f 2a 20 20 32 32 20 2a 2f 20 22 63 6d 64 20   /*  22 */ "cmd 
314fb 3a 3a 3d 20 53 41 56 45 50 4f 49 4e 54 20 6e 6d  ::= SAVEPOINT nm
314fc 22 2c 0a 20 2f 2a 20 20 32 33 20 2a 2f 20 22 63  ",. /*  23 */ "c
314fd 6d 64 20 3a 3a 3d 20 52 45 4c 45 41 53 45 20 73  md ::= RELEASE s
314fe 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 6e 6d 22  avepoint_opt nm"
314ff 2c 0a 20 2f 2a 20 20 32 34 20 2a 2f 20 22 63 6d  ,. /*  24 */ "cm
31500 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 20 74  d ::= ROLLBACK t
31501 72 61 6e 73 5f 6f 70 74 20 54 4f 20 73 61 76 65  rans_opt TO save
31502 70 6f 69 6e 74 5f 6f 70 74 20 6e 6d 22 2c 0a 20  point_opt nm",. 
31503 2f 2a 20 20 32 35 20 2a 2f 20 22 63 6d 64 20 3a  /*  25 */ "cmd :
31504 3a 3d 20 63 72 65 61 74 65 5f 74 61 62 6c 65 20  := create_table 
31505 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67  create_table_arg
31506 73 22 2c 0a 20 2f 2a 20 20 32 36 20 2a 2f 20 22  s",. /*  26 */ "
31507 63 72 65 61 74 65 5f 74 61 62 6c 65 20 3a 3a 3d  create_table ::=
31508 20 63 72 65 61 74 65 6b 77 20 74 65 6d 70 20 54   createkw temp T
31509 41 42 4c 45 20 69 66 6e 6f 74 65 78 69 73 74 73  ABLE ifnotexists
3150a 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 20   nm dbnm",. /*  
3150b 32 37 20 2a 2f 20 22 63 72 65 61 74 65 6b 77 20  27 */ "createkw 
3150c 3a 3a 3d 20 43 52 45 41 54 45 22 2c 0a 20 2f 2a  ::= CREATE",. /*
3150d 20 20 32 38 20 2a 2f 20 22 69 66 6e 6f 74 65 78    28 */ "ifnotex
3150e 69 73 74 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20  ists ::=",. /*  
3150f 32 39 20 2a 2f 20 22 69 66 6e 6f 74 65 78 69 73  29 */ "ifnotexis
31510 74 73 20 3a 3a 3d 20 49 46 20 4e 4f 54 20 45 58  ts ::= IF NOT EX
31511 49 53 54 53 22 2c 0a 20 2f 2a 20 20 33 30 20 2a  ISTS",. /*  30 *
31512 2f 20 22 74 65 6d 70 20 3a 3a 3d 20 54 45 4d 50  / "temp ::= TEMP
31513 22 2c 0a 20 2f 2a 20 20 33 31 20 2a 2f 20 22 74  ",. /*  31 */ "t
31514 65 6d 70 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 33  emp ::=",. /*  3
31515 32 20 2a 2f 20 22 63 72 65 61 74 65 5f 74 61 62  2 */ "create_tab
31516 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 4c 50 20 63  le_args ::= LP c
31517 6f 6c 75 6d 6e 6c 69 73 74 20 63 6f 6e 73 6c 69  olumnlist consli
31518 73 74 5f 6f 70 74 20 52 50 22 2c 0a 20 2f 2a 20  st_opt RP",. /* 
31519 20 33 33 20 2a 2f 20 22 63 72 65 61 74 65 5f 74   33 */ "create_t
3151a 61 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 41 53  able_args ::= AS
3151b 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 20 33   select",. /*  3
3151c 34 20 2a 2f 20 22 63 6f 6c 75 6d 6e 6c 69 73 74  4 */ "columnlist
3151d 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 6c 69 73 74 20   ::= columnlist 
3151e 43 4f 4d 4d 41 20 63 6f 6c 75 6d 6e 22 2c 0a 20  COMMA column",. 
3151f 2f 2a 20 20 33 35 20 2a 2f 20 22 63 6f 6c 75 6d  /*  35 */ "colum
31520 6e 6c 69 73 74 20 3a 3a 3d 20 63 6f 6c 75 6d 6e  nlist ::= column
31521 22 2c 0a 20 2f 2a 20 20 33 36 20 2a 2f 20 22 63  ",. /*  36 */ "c
31522 6f 6c 75 6d 6e 20 3a 3a 3d 20 63 6f 6c 75 6d 6e  olumn ::= column
31523 69 64 20 74 79 70 65 20 63 61 72 67 6c 69 73 74  id type carglist
31524 22 2c 0a 20 2f 2a 20 20 33 37 20 2a 2f 20 22 63  ",. /*  37 */ "c
31525 6f 6c 75 6d 6e 69 64 20 3a 3a 3d 20 6e 6d 22 2c  olumnid ::= nm",
31526 0a 20 2f 2a 20 20 33 38 20 2a 2f 20 22 69 64 20  . /*  38 */ "id 
31527 3a 3a 3d 20 49 44 22 2c 0a 20 2f 2a 20 20 33 39  ::= ID",. /*  39
31528 20 2a 2f 20 22 69 64 20 3a 3a 3d 20 49 4e 44 45   */ "id ::= INDE
31529 58 45 44 22 2c 0a 20 2f 2a 20 20 34 30 20 2a 2f  XED",. /*  40 */
3152a 20 22 69 64 73 20 3a 3a 3d 20 49 44 7c 53 54 52   "ids ::= ID|STR
3152b 49 4e 47 22 2c 0a 20 2f 2a 20 20 34 31 20 2a 2f  ING",. /*  41 */
3152c 20 22 6e 6d 20 3a 3a 3d 20 69 64 22 2c 0a 20 2f   "nm ::= id",. /
3152d 2a 20 20 34 32 20 2a 2f 20 22 6e 6d 20 3a 3a 3d  *  42 */ "nm ::=
3152e 20 53 54 52 49 4e 47 22 2c 0a 20 2f 2a 20 20 34   STRING",. /*  4
3152f 33 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 4a 4f 49  3 */ "nm ::= JOI
31530 4e 5f 4b 57 22 2c 0a 20 2f 2a 20 20 34 34 20 2a  N_KW",. /*  44 *
31531 2f 20 22 74 79 70 65 20 3a 3a 3d 22 2c 0a 20 2f  / "type ::=",. /
31532 2a 20 20 34 35 20 2a 2f 20 22 74 79 70 65 20 3a  *  45 */ "type :
31533 3a 3d 20 74 79 70 65 74 6f 6b 65 6e 22 2c 0a 20  := typetoken",. 
31534 2f 2a 20 20 34 36 20 2a 2f 20 22 74 79 70 65 74  /*  46 */ "typet
31535 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d  oken ::= typenam
31536 65 22 2c 0a 20 2f 2a 20 20 34 37 20 2a 2f 20 22  e",. /*  47 */ "
31537 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79  typetoken ::= ty
31538 70 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65 64  pename LP signed
31539 20 52 50 22 2c 0a 20 2f 2a 20 20 34 38 20 2a 2f   RP",. /*  48 */
3153a 20 22 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20   "typetoken ::= 
3153b 74 79 70 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e  typename LP sign
3153c 65 64 20 43 4f 4d 4d 41 20 73 69 67 6e 65 64 20  ed COMMA signed 
3153d 52 50 22 2c 0a 20 2f 2a 20 20 34 39 20 2a 2f 20  RP",. /*  49 */ 
3153e 22 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 69 64  "typename ::= id
3153f 73 22 2c 0a 20 2f 2a 20 20 35 30 20 2a 2f 20 22  s",. /*  50 */ "
31540 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 74 79 70  typename ::= typ
31541 65 6e 61 6d 65 20 69 64 73 22 2c 0a 20 2f 2a 20  ename ids",. /* 
31542 20 35 31 20 2a 2f 20 22 73 69 67 6e 65 64 20 3a   51 */ "signed :
31543 3a 3d 20 70 6c 75 73 5f 6e 75 6d 22 2c 0a 20 2f  := plus_num",. /
31544 2a 20 20 35 32 20 2a 2f 20 22 73 69 67 6e 65 64  *  52 */ "signed
31545 20 3a 3a 3d 20 6d 69 6e 75 73 5f 6e 75 6d 22 2c   ::= minus_num",
31546 0a 20 2f 2a 20 20 35 33 20 2a 2f 20 22 63 61 72  . /*  53 */ "car
31547 67 6c 69 73 74 20 3a 3a 3d 20 63 61 72 67 6c 69  glist ::= cargli
31548 73 74 20 63 61 72 67 22 2c 0a 20 2f 2a 20 20 35  st carg",. /*  5
31549 34 20 2a 2f 20 22 63 61 72 67 6c 69 73 74 20 3a  4 */ "carglist :
3154a 3a 3d 22 2c 0a 20 2f 2a 20 20 35 35 20 2a 2f 20  :=",. /*  55 */ 
3154b 22 63 61 72 67 20 3a 3a 3d 20 43 4f 4e 53 54 52  "carg ::= CONSTR
3154c 41 49 4e 54 20 6e 6d 20 63 63 6f 6e 73 22 2c 0a  AINT nm ccons",.
3154d 20 2f 2a 20 20 35 36 20 2a 2f 20 22 63 61 72 67   /*  56 */ "carg
3154e 20 3a 3a 3d 20 63 63 6f 6e 73 22 2c 0a 20 2f 2a   ::= ccons",. /*
3154f 20 20 35 37 20 2a 2f 20 22 63 63 6f 6e 73 20 3a    57 */ "ccons :
31550 3a 3d 20 44 45 46 41 55 4c 54 20 74 65 72 6d 22  := DEFAULT term"
31551 2c 0a 20 2f 2a 20 20 35 38 20 2a 2f 20 22 63 63  ,. /*  58 */ "cc
31552 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20  ons ::= DEFAULT 
31553 4c 50 20 65 78 70 72 20 52 50 22 2c 0a 20 2f 2a  LP expr RP",. /*
31554 20 20 35 39 20 2a 2f 20 22 63 63 6f 6e 73 20 3a    59 */ "ccons :
31555 3a 3d 20 44 45 46 41 55 4c 54 20 50 4c 55 53 20  := DEFAULT PLUS 
31556 74 65 72 6d 22 2c 0a 20 2f 2a 20 20 36 30 20 2a  term",. /*  60 *
31557 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46  / "ccons ::= DEF
31558 41 55 4c 54 20 4d 49 4e 55 53 20 74 65 72 6d 22  AULT MINUS term"
31559 2c 0a 20 2f 2a 20 20 36 31 20 2a 2f 20 22 63 63  ,. /*  61 */ "cc
3155a 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20  ons ::= DEFAULT 
3155b 69 64 22 2c 0a 20 2f 2a 20 20 36 32 20 2a 2f 20  id",. /*  62 */ 
3155c 22 63 63 6f 6e 73 20 3a 3a 3d 20 4e 55 4c 4c 20  "ccons ::= NULL 
3155d 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 36 33  onconf",. /*  63
3155e 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 4e   */ "ccons ::= N
3155f 4f 54 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 22 2c  OT NULL onconf",
31560 0a 20 2f 2a 20 20 36 34 20 2a 2f 20 22 63 63 6f  . /*  64 */ "cco
31561 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b  ns ::= PRIMARY K
31562 45 59 20 73 6f 72 74 6f 72 64 65 72 20 6f 6e 63  EY sortorder onc
31563 6f 6e 66 20 61 75 74 6f 69 6e 63 22 2c 0a 20 2f  onf autoinc",. /
31564 2a 20 20 36 35 20 2a 2f 20 22 63 63 6f 6e 73 20  *  65 */ "ccons 
31565 3a 3a 3d 20 55 4e 49 51 55 45 20 6f 6e 63 6f 6e  ::= UNIQUE oncon
31566 66 22 2c 0a 20 2f 2a 20 20 36 36 20 2a 2f 20 22  f",. /*  66 */ "
31567 63 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43 4b 20  ccons ::= CHECK 
31568 4c 50 20 65 78 70 72 20 52 50 22 2c 0a 20 2f 2a  LP expr RP",. /*
31569 20 20 36 37 20 2a 2f 20 22 63 63 6f 6e 73 20 3a    67 */ "ccons :
3156a 3a 3d 20 52 45 46 45 52 45 4e 43 45 53 20 6e 6d  := REFERENCES nm
3156b 20 69 64 78 6c 69 73 74 5f 6f 70 74 20 72 65 66   idxlist_opt ref
3156c 61 72 67 73 22 2c 0a 20 2f 2a 20 20 36 38 20 2a  args",. /*  68 *
3156d 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 64 65 66  / "ccons ::= def
3156e 65 72 5f 73 75 62 63 6c 61 75 73 65 22 2c 0a 20  er_subclause",. 
3156f 2f 2a 20 20 36 39 20 2a 2f 20 22 63 63 6f 6e 73  /*  69 */ "ccons
31570 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45 20 69 64 73   ::= COLLATE ids
31571 22 2c 0a 20 2f 2a 20 20 37 30 20 2a 2f 20 22 61  ",. /*  70 */ "a
31572 75 74 6f 69 6e 63 20 3a 3a 3d 22 2c 0a 20 2f 2a  utoinc ::=",. /*
31573 20 20 37 31 20 2a 2f 20 22 61 75 74 6f 69 6e 63    71 */ "autoinc
31574 20 3a 3a 3d 20 41 55 54 4f 49 4e 43 52 22 2c 0a   ::= AUTOINCR",.
31575 20 2f 2a 20 20 37 32 20 2a 2f 20 22 72 65 66 61   /*  72 */ "refa
31576 72 67 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 37  rgs ::=",. /*  7
31577 33 20 2a 2f 20 22 72 65 66 61 72 67 73 20 3a 3a  3 */ "refargs ::
31578 3d 20 72 65 66 61 72 67 73 20 72 65 66 61 72 67  = refargs refarg
31579 22 2c 0a 20 2f 2a 20 20 37 34 20 2a 2f 20 22 72  ",. /*  74 */ "r
3157a 65 66 61 72 67 20 3a 3a 3d 20 4d 41 54 43 48 20  efarg ::= MATCH 
3157b 6e 6d 22 2c 0a 20 2f 2a 20 20 37 35 20 2a 2f 20  nm",. /*  75 */ 
3157c 22 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 44  "refarg ::= ON D
3157d 45 4c 45 54 45 20 72 65 66 61 63 74 22 2c 0a 20  ELETE refact",. 
3157e 2f 2a 20 20 37 36 20 2a 2f 20 22 72 65 66 61 72  /*  76 */ "refar
3157f 67 20 3a 3a 3d 20 4f 4e 20 55 50 44 41 54 45 20  g ::= ON UPDATE 
31580 72 65 66 61 63 74 22 2c 0a 20 2f 2a 20 20 37 37  refact",. /*  77
31581 20 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d 20   */ "refact ::= 
31582 53 45 54 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 20  SET NULL",. /*  
31583 37 38 20 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a  78 */ "refact ::
31584 3d 20 53 45 54 20 44 45 46 41 55 4c 54 22 2c 0a  = SET DEFAULT",.
31585 20 2f 2a 20 20 37 39 20 2a 2f 20 22 72 65 66 61   /*  79 */ "refa
31586 63 74 20 3a 3a 3d 20 43 41 53 43 41 44 45 22 2c  ct ::= CASCADE",
31587 0a 20 2f 2a 20 20 38 30 20 2a 2f 20 22 72 65 66  . /*  80 */ "ref
31588 61 63 74 20 3a 3a 3d 20 52 45 53 54 52 49 43 54  act ::= RESTRICT
31589 22 2c 0a 20 2f 2a 20 20 38 31 20 2a 2f 20 22 72  ",. /*  81 */ "r
3158a 65 66 61 63 74 20 3a 3a 3d 20 4e 4f 20 41 43 54  efact ::= NO ACT
3158b 49 4f 4e 22 2c 0a 20 2f 2a 20 20 38 32 20 2a 2f  ION",. /*  82 */
3158c 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73   "defer_subclaus
3158d 65 20 3a 3a 3d 20 4e 4f 54 20 44 45 46 45 52 52  e ::= NOT DEFERR
3158e 41 42 4c 45 20 69 6e 69 74 5f 64 65 66 65 72 72  ABLE init_deferr
3158f 65 64 5f 70 72 65 64 5f 6f 70 74 22 2c 0a 20 2f  ed_pred_opt",. /
31590 2a 20 20 38 33 20 2a 2f 20 22 64 65 66 65 72 5f  *  83 */ "defer_
31591 73 75 62 63 6c 61 75 73 65 20 3a 3a 3d 20 44 45  subclause ::= DE
31592 46 45 52 52 41 42 4c 45 20 69 6e 69 74 5f 64 65  FERRABLE init_de
31593 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 22  ferred_pred_opt"
31594 2c 0a 20 2f 2a 20 20 38 34 20 2a 2f 20 22 69 6e  ,. /*  84 */ "in
31595 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64  it_deferred_pred
31596 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20  _opt ::=",. /*  
31597 38 35 20 2a 2f 20 22 69 6e 69 74 5f 64 65 66 65  85 */ "init_defe
31598 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a  rred_pred_opt ::
31599 3d 20 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45  = INITIALLY DEFE
3159a 52 52 45 44 22 2c 0a 20 2f 2a 20 20 38 36 20 2a  RRED",. /*  86 *
3159b 2f 20 22 69 6e 69 74 5f 64 65 66 65 72 72 65 64  / "init_deferred
3159c 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e  _pred_opt ::= IN
3159d 49 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54  ITIALLY IMMEDIAT
3159e 45 22 2c 0a 20 2f 2a 20 20 38 37 20 2a 2f 20 22  E",. /*  87 */ "
3159f 63 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d  conslist_opt ::=
315a0 22 2c 0a 20 2f 2a 20 20 38 38 20 2a 2f 20 22 63  ",. /*  88 */ "c
315a1 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20  onslist_opt ::= 
315a2 43 4f 4d 4d 41 20 63 6f 6e 73 6c 69 73 74 22 2c  COMMA conslist",
315a3 0a 20 2f 2a 20 20 38 39 20 2a 2f 20 22 63 6f 6e  . /*  89 */ "con
315a4 73 6c 69 73 74 20 3a 3a 3d 20 63 6f 6e 73 6c 69  slist ::= consli
315a5 73 74 20 43 4f 4d 4d 41 20 74 63 6f 6e 73 22 2c  st COMMA tcons",
315a6 0a 20 2f 2a 20 20 39 30 20 2a 2f 20 22 63 6f 6e  . /*  90 */ "con
315a7 73 6c 69 73 74 20 3a 3a 3d 20 63 6f 6e 73 6c 69  slist ::= consli
315a8 73 74 20 74 63 6f 6e 73 22 2c 0a 20 2f 2a 20 20  st tcons",. /*  
315a9 39 31 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 20  91 */ "conslist 
315aa 3a 3a 3d 20 74 63 6f 6e 73 22 2c 0a 20 2f 2a 20  ::= tcons",. /* 
315ab 20 39 32 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a   92 */ "tcons ::
315ac 3d 20 43 4f 4e 53 54 52 41 49 4e 54 20 6e 6d 22  = CONSTRAINT nm"
315ad 2c 0a 20 2f 2a 20 20 39 33 20 2a 2f 20 22 74 63  ,. /*  93 */ "tc
315ae 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59 20  ons ::= PRIMARY 
315af 4b 45 59 20 4c 50 20 69 64 78 6c 69 73 74 20 61  KEY LP idxlist a
315b0 75 74 6f 69 6e 63 20 52 50 20 6f 6e 63 6f 6e 66  utoinc RP onconf
315b1 22 2c 0a 20 2f 2a 20 20 39 34 20 2a 2f 20 22 74  ",. /*  94 */ "t
315b2 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20  cons ::= UNIQUE 
315b3 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20 6f 6e  LP idxlist RP on
315b4 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 39 35 20 2a  conf",. /*  95 *
315b5 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 43 48 45  / "tcons ::= CHE
315b6 43 4b 20 4c 50 20 65 78 70 72 20 52 50 20 6f 6e  CK LP expr RP on
315b7 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 39 36 20 2a  conf",. /*  96 *
315b8 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 46 4f 52  / "tcons ::= FOR
315b9 45 49 47 4e 20 4b 45 59 20 4c 50 20 69 64 78 6c  EIGN KEY LP idxl
315ba 69 73 74 20 52 50 20 52 45 46 45 52 45 4e 43 45  ist RP REFERENCE
315bb 53 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70 74  S nm idxlist_opt
315bc 20 72 65 66 61 72 67 73 20 64 65 66 65 72 5f 73   refargs defer_s
315bd 75 62 63 6c 61 75 73 65 5f 6f 70 74 22 2c 0a 20  ubclause_opt",. 
315be 2f 2a 20 20 39 37 20 2a 2f 20 22 64 65 66 65 72  /*  97 */ "defer
315bf 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a  _subclause_opt :
315c0 3a 3d 22 2c 0a 20 2f 2a 20 20 39 38 20 2a 2f 20  :=",. /*  98 */ 
315c1 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65  "defer_subclause
315c2 5f 6f 70 74 20 3a 3a 3d 20 64 65 66 65 72 5f 73  _opt ::= defer_s
315c3 75 62 63 6c 61 75 73 65 22 2c 0a 20 2f 2a 20 20  ubclause",. /*  
315c4 39 39 20 2a 2f 20 22 6f 6e 63 6f 6e 66 20 3a 3a  99 */ "onconf ::
315c5 3d 22 2c 0a 20 2f 2a 20 31 30 30 20 2a 2f 20 22  =",. /* 100 */ "
315c6 6f 6e 63 6f 6e 66 20 3a 3a 3d 20 4f 4e 20 43 4f  onconf ::= ON CO
315c7 4e 46 4c 49 43 54 20 72 65 73 6f 6c 76 65 74 79  NFLICT resolvety
315c8 70 65 22 2c 0a 20 2f 2a 20 31 30 31 20 2a 2f 20  pe",. /* 101 */ 
315c9 22 6f 72 63 6f 6e 66 20 3a 3a 3d 22 2c 0a 20 2f  "orconf ::=",. /
315ca 2a 20 31 30 32 20 2a 2f 20 22 6f 72 63 6f 6e 66  * 102 */ "orconf
315cb 20 3a 3a 3d 20 4f 52 20 72 65 73 6f 6c 76 65 74   ::= OR resolvet
315cc 79 70 65 22 2c 0a 20 2f 2a 20 31 30 33 20 2a 2f  ype",. /* 103 */
315cd 20 22 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a   "resolvetype ::
315ce 3d 20 72 61 69 73 65 74 79 70 65 22 2c 0a 20 2f  = raisetype",. /
315cf 2a 20 31 30 34 20 2a 2f 20 22 72 65 73 6f 6c 76  * 104 */ "resolv
315d0 65 74 79 70 65 20 3a 3a 3d 20 49 47 4e 4f 52 45  etype ::= IGNORE
315d1 22 2c 0a 20 2f 2a 20 31 30 35 20 2a 2f 20 22 72  ",. /* 105 */ "r
315d2 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20 52  esolvetype ::= R
315d3 45 50 4c 41 43 45 22 2c 0a 20 2f 2a 20 31 30 36  EPLACE",. /* 106
315d4 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 52 4f   */ "cmd ::= DRO
315d5 50 20 54 41 42 4c 45 20 69 66 65 78 69 73 74 73  P TABLE ifexists
315d6 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20   fullname",. /* 
315d7 31 30 37 20 2a 2f 20 22 69 66 65 78 69 73 74 73  107 */ "ifexists
315d8 20 3a 3a 3d 20 49 46 20 45 58 49 53 54 53 22 2c   ::= IF EXISTS",
315d9 0a 20 2f 2a 20 31 30 38 20 2a 2f 20 22 69 66 65  . /* 108 */ "ife
315da 78 69 73 74 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  xists ::=",. /* 
315db 31 30 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  109 */ "cmd ::= 
315dc 63 72 65 61 74 65 6b 77 20 74 65 6d 70 20 56 49  createkw temp VI
315dd 45 57 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e  EW ifnotexists n
315de 6d 20 64 62 6e 6d 20 41 53 20 73 65 6c 65 63 74  m dbnm AS select
315df 22 2c 0a 20 2f 2a 20 31 31 30 20 2a 2f 20 22 63  ",. /* 110 */ "c
315e0 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 56 49 45 57  md ::= DROP VIEW
315e1 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61   ifexists fullna
315e2 6d 65 22 2c 0a 20 2f 2a 20 31 31 31 20 2a 2f 20  me",. /* 111 */ 
315e3 22 63 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74 22  "cmd ::= select"
315e4 2c 0a 20 2f 2a 20 31 31 32 20 2a 2f 20 22 73 65  ,. /* 112 */ "se
315e5 6c 65 63 74 20 3a 3a 3d 20 6f 6e 65 73 65 6c 65  lect ::= onesele
315e6 63 74 22 2c 0a 20 2f 2a 20 31 31 33 20 2a 2f 20  ct",. /* 113 */ 
315e7 22 73 65 6c 65 63 74 20 3a 3a 3d 20 73 65 6c 65  "select ::= sele
315e8 63 74 20 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f  ct multiselect_o
315e9 70 20 6f 6e 65 73 65 6c 65 63 74 22 2c 0a 20 2f  p oneselect",. /
315ea 2a 20 31 31 34 20 2a 2f 20 22 6d 75 6c 74 69 73  * 114 */ "multis
315eb 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49  elect_op ::= UNI
315ec 4f 4e 22 2c 0a 20 2f 2a 20 31 31 35 20 2a 2f 20  ON",. /* 115 */ 
315ed 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20  "multiselect_op 
315ee 3a 3a 3d 20 55 4e 49 4f 4e 20 41 4c 4c 22 2c 0a  ::= UNION ALL",.
315ef 20 2f 2a 20 31 31 36 20 2a 2f 20 22 6d 75 6c 74   /* 116 */ "mult
315f0 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20 45  iselect_op ::= E
315f1 58 43 45 50 54 7c 49 4e 54 45 52 53 45 43 54 22  XCEPT|INTERSECT"
315f2 2c 0a 20 2f 2a 20 31 31 37 20 2a 2f 20 22 6f 6e  ,. /* 117 */ "on
315f3 65 73 65 6c 65 63 74 20 3a 3a 3d 20 53 45 4c 45  eselect ::= SELE
315f4 43 54 20 64 69 73 74 69 6e 63 74 20 73 65 6c 63  CT distinct selc
315f5 6f 6c 6c 69 73 74 20 66 72 6f 6d 20 77 68 65 72  ollist from wher
315f6 65 5f 6f 70 74 20 67 72 6f 75 70 62 79 5f 6f 70  e_opt groupby_op
315f7 74 20 68 61 76 69 6e 67 5f 6f 70 74 20 6f 72 64  t having_opt ord
315f8 65 72 62 79 5f 6f 70 74 20 6c 69 6d 69 74 5f 6f  erby_opt limit_o
315f9 70 74 22 2c 0a 20 2f 2a 20 31 31 38 20 2a 2f 20  pt",. /* 118 */ 
315fa 22 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20 44 49  "distinct ::= DI
315fb 53 54 49 4e 43 54 22 2c 0a 20 2f 2a 20 31 31 39  STINCT",. /* 119
315fc 20 2a 2f 20 22 64 69 73 74 69 6e 63 74 20 3a 3a   */ "distinct ::
315fd 3d 20 41 4c 4c 22 2c 0a 20 2f 2a 20 31 32 30 20  = ALL",. /* 120 
315fe 2a 2f 20 22 64 69 73 74 69 6e 63 74 20 3a 3a 3d  */ "distinct ::=
315ff 22 2c 0a 20 2f 2a 20 31 32 31 20 2a 2f 20 22 73  ",. /* 121 */ "s
31600 63 6c 70 20 3a 3a 3d 20 73 65 6c 63 6f 6c 6c 69  clp ::= selcolli
31601 73 74 20 43 4f 4d 4d 41 22 2c 0a 20 2f 2a 20 31  st COMMA",. /* 1
31602 32 32 20 2a 2f 20 22 73 63 6c 70 20 3a 3a 3d 22  22 */ "sclp ::="
31603 2c 0a 20 2f 2a 20 31 32 33 20 2a 2f 20 22 73 65  ,. /* 123 */ "se
31604 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c  lcollist ::= scl
31605 70 20 65 78 70 72 20 61 73 22 2c 0a 20 2f 2a 20  p expr as",. /* 
31606 31 32 34 20 2a 2f 20 22 73 65 6c 63 6f 6c 6c 69  124 */ "selcolli
31607 73 74 20 3a 3a 3d 20 73 63 6c 70 20 53 54 41 52  st ::= sclp STAR
31608 22 2c 0a 20 2f 2a 20 31 32 35 20 2a 2f 20 22 73  ",. /* 125 */ "s
31609 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73 63  elcollist ::= sc
3160a 6c 70 20 6e 6d 20 44 4f 54 20 53 54 41 52 22 2c  lp nm DOT STAR",
3160b 0a 20 2f 2a 20 31 32 36 20 2a 2f 20 22 61 73 20  . /* 126 */ "as 
3160c 3a 3a 3d 20 41 53 20 6e 6d 22 2c 0a 20 2f 2a 20  ::= AS nm",. /* 
3160d 31 32 37 20 2a 2f 20 22 61 73 20 3a 3a 3d 20 69  127 */ "as ::= i
3160e 64 73 22 2c 0a 20 2f 2a 20 31 32 38 20 2a 2f 20  ds",. /* 128 */ 
3160f 22 61 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32  "as ::=",. /* 12
31610 39 20 2a 2f 20 22 66 72 6f 6d 20 3a 3a 3d 22 2c  9 */ "from ::=",
31611 0a 20 2f 2a 20 31 33 30 20 2a 2f 20 22 66 72 6f  . /* 130 */ "fro
31612 6d 20 3a 3a 3d 20 46 52 4f 4d 20 73 65 6c 74 61  m ::= FROM selta
31613 62 6c 69 73 74 22 2c 0a 20 2f 2a 20 31 33 31 20  blist",. /* 131 
31614 2a 2f 20 22 73 74 6c 5f 70 72 65 66 69 78 20 3a  */ "stl_prefix :
31615 3a 3d 20 73 65 6c 74 61 62 6c 69 73 74 20 6a 6f  := seltablist jo
31616 69 6e 6f 70 22 2c 0a 20 2f 2a 20 31 33 32 20 2a  inop",. /* 132 *
31617 2f 20 22 73 74 6c 5f 70 72 65 66 69 78 20 3a 3a  / "stl_prefix ::
31618 3d 22 2c 0a 20 2f 2a 20 31 33 33 20 2a 2f 20 22  =",. /* 133 */ "
31619 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73  seltablist ::= s
3161a 74 6c 5f 70 72 65 66 69 78 20 6e 6d 20 64 62 6e  tl_prefix nm dbn
3161b 6d 20 61 73 20 69 6e 64 65 78 65 64 5f 6f 70 74  m as indexed_opt
3161c 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70   on_opt using_op
3161d 74 22 2c 0a 20 2f 2a 20 31 33 34 20 2a 2f 20 22  t",. /* 134 */ "
3161e 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73  seltablist ::= s
3161f 74 6c 5f 70 72 65 66 69 78 20 4c 50 20 73 65 6c  tl_prefix LP sel
31620 65 63 74 20 52 50 20 61 73 20 6f 6e 5f 6f 70 74  ect RP as on_opt
31621 20 75 73 69 6e 67 5f 6f 70 74 22 2c 0a 20 2f 2a   using_opt",. /*
31622 20 31 33 35 20 2a 2f 20 22 73 65 6c 74 61 62 6c   135 */ "seltabl
31623 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65 66  ist ::= stl_pref
31624 69 78 20 4c 50 20 73 65 6c 74 61 62 6c 69 73 74  ix LP seltablist
31625 20 52 50 20 61 73 20 6f 6e 5f 6f 70 74 20 75 73   RP as on_opt us
31626 69 6e 67 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 33  ing_opt",. /* 13
31627 36 20 2a 2f 20 22 64 62 6e 6d 20 3a 3a 3d 22 2c  6 */ "dbnm ::=",
31628 0a 20 2f 2a 20 31 33 37 20 2a 2f 20 22 64 62 6e  . /* 137 */ "dbn
31629 6d 20 3a 3a 3d 20 44 4f 54 20 6e 6d 22 2c 0a 20  m ::= DOT nm",. 
3162a 2f 2a 20 31 33 38 20 2a 2f 20 22 66 75 6c 6c 6e  /* 138 */ "fulln
3162b 61 6d 65 20 3a 3a 3d 20 6e 6d 20 64 62 6e 6d 22  ame ::= nm dbnm"
3162c 2c 0a 20 2f 2a 20 31 33 39 20 2a 2f 20 22 6a 6f  ,. /* 139 */ "jo
3162d 69 6e 6f 70 20 3a 3a 3d 20 43 4f 4d 4d 41 7c 4a  inop ::= COMMA|J
3162e 4f 49 4e 22 2c 0a 20 2f 2a 20 31 34 30 20 2a 2f  OIN",. /* 140 */
3162f 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49   "joinop ::= JOI
31630 4e 5f 4b 57 20 4a 4f 49 4e 22 2c 0a 20 2f 2a 20  N_KW JOIN",. /* 
31631 31 34 31 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a  141 */ "joinop :
31632 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 4a 4f  := JOIN_KW nm JO
31633 49 4e 22 2c 0a 20 2f 2a 20 31 34 32 20 2a 2f 20  IN",. /* 142 */ 
31634 22 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e  "joinop ::= JOIN
31635 5f 4b 57 20 6e 6d 20 6e 6d 20 4a 4f 49 4e 22 2c  _KW nm nm JOIN",
31636 0a 20 2f 2a 20 31 34 33 20 2a 2f 20 22 6f 6e 5f  . /* 143 */ "on_
31637 6f 70 74 20 3a 3a 3d 20 4f 4e 20 65 78 70 72 22  opt ::= ON expr"
31638 2c 0a 20 2f 2a 20 31 34 34 20 2a 2f 20 22 6f 6e  ,. /* 144 */ "on
31639 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31  _opt ::=",. /* 1
3163a 34 35 20 2a 2f 20 22 69 6e 64 65 78 65 64 5f 6f  45 */ "indexed_o
3163b 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 34 36  pt ::=",. /* 146
3163c 20 2a 2f 20 22 69 6e 64 65 78 65 64 5f 6f 70 74   */ "indexed_opt
3163d 20 3a 3a 3d 20 49 4e 44 45 58 45 44 20 42 59 20   ::= INDEXED BY 
3163e 6e 6d 22 2c 0a 20 2f 2a 20 31 34 37 20 2a 2f 20  nm",. /* 147 */ 
3163f 22 69 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d  "indexed_opt ::=
31640 20 4e 4f 54 20 49 4e 44 45 58 45 44 22 2c 0a 20   NOT INDEXED",. 
31641 2f 2a 20 31 34 38 20 2a 2f 20 22 75 73 69 6e 67  /* 148 */ "using
31642 5f 6f 70 74 20 3a 3a 3d 20 55 53 49 4e 47 20 4c  _opt ::= USING L
31643 50 20 69 6e 73 63 6f 6c 6c 69 73 74 20 52 50 22  P inscollist RP"
31644 2c 0a 20 2f 2a 20 31 34 39 20 2a 2f 20 22 75 73  ,. /* 149 */ "us
31645 69 6e 67 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f  ing_opt ::=",. /
31646 2a 20 31 35 30 20 2a 2f 20 22 6f 72 64 65 72 62  * 150 */ "orderb
31647 79 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  y_opt ::=",. /* 
31648 31 35 31 20 2a 2f 20 22 6f 72 64 65 72 62 79 5f  151 */ "orderby_
31649 6f 70 74 20 3a 3a 3d 20 4f 52 44 45 52 20 42 59  opt ::= ORDER BY
3164a 20 73 6f 72 74 6c 69 73 74 22 2c 0a 20 2f 2a 20   sortlist",. /* 
3164b 31 35 32 20 2a 2f 20 22 73 6f 72 74 6c 69 73 74  152 */ "sortlist
3164c 20 3a 3a 3d 20 73 6f 72 74 6c 69 73 74 20 43 4f   ::= sortlist CO
3164d 4d 4d 41 20 73 6f 72 74 69 74 65 6d 20 73 6f 72  MMA sortitem sor
3164e 74 6f 72 64 65 72 22 2c 0a 20 2f 2a 20 31 35 33  torder",. /* 153
3164f 20 2a 2f 20 22 73 6f 72 74 6c 69 73 74 20 3a 3a   */ "sortlist ::
31650 3d 20 73 6f 72 74 69 74 65 6d 20 73 6f 72 74 6f  = sortitem sorto
31651 72 64 65 72 22 2c 0a 20 2f 2a 20 31 35 34 20 2a  rder",. /* 154 *
31652 2f 20 22 73 6f 72 74 69 74 65 6d 20 3a 3a 3d 20  / "sortitem ::= 
31653 65 78 70 72 22 2c 0a 20 2f 2a 20 31 35 35 20 2a  expr",. /* 155 *
31654 2f 20 22 73 6f 72 74 6f 72 64 65 72 20 3a 3a 3d  / "sortorder ::=
31655 20 41 53 43 22 2c 0a 20 2f 2a 20 31 35 36 20 2a   ASC",. /* 156 *
31656 2f 20 22 73 6f 72 74 6f 72 64 65 72 20 3a 3a 3d  / "sortorder ::=
31657 20 44 45 53 43 22 2c 0a 20 2f 2a 20 31 35 37 20   DESC",. /* 157 
31658 2a 2f 20 22 73 6f 72 74 6f 72 64 65 72 20 3a 3a  */ "sortorder ::
31659 3d 22 2c 0a 20 2f 2a 20 31 35 38 20 2a 2f 20 22  =",. /* 158 */ "
3165a 67 72 6f 75 70 62 79 5f 6f 70 74 20 3a 3a 3d 22  groupby_opt ::="
3165b 2c 0a 20 2f 2a 20 31 35 39 20 2a 2f 20 22 67 72  ,. /* 159 */ "gr
3165c 6f 75 70 62 79 5f 6f 70 74 20 3a 3a 3d 20 47 52  oupby_opt ::= GR
3165d 4f 55 50 20 42 59 20 6e 65 78 70 72 6c 69 73 74  OUP BY nexprlist
3165e 22 2c 0a 20 2f 2a 20 31 36 30 20 2a 2f 20 22 68  ",. /* 160 */ "h
3165f 61 76 69 6e 67 5f 6f 70 74 20 3a 3a 3d 22 2c 0a  aving_opt ::=",.
31660 20 2f 2a 20 31 36 31 20 2a 2f 20 22 68 61 76 69   /* 161 */ "havi
31661 6e 67 5f 6f 70 74 20 3a 3a 3d 20 48 41 56 49 4e  ng_opt ::= HAVIN
31662 47 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 36 32  G expr",. /* 162
31663 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70 74 20 3a   */ "limit_opt :
31664 3a 3d 22 2c 0a 20 2f 2a 20 31 36 33 20 2a 2f 20  :=",. /* 163 */ 
31665 22 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c  "limit_opt ::= L
31666 49 4d 49 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20  IMIT expr",. /* 
31667 31 36 34 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f 70  164 */ "limit_op
31668 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70 72  t ::= LIMIT expr
31669 20 4f 46 46 53 45 54 20 65 78 70 72 22 2c 0a 20   OFFSET expr",. 
3166a 2f 2a 20 31 36 35 20 2a 2f 20 22 6c 69 6d 69 74  /* 165 */ "limit
3166b 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65  _opt ::= LIMIT e
3166c 78 70 72 20 43 4f 4d 4d 41 20 65 78 70 72 22 2c  xpr COMMA expr",
3166d 0a 20 2f 2a 20 31 36 36 20 2a 2f 20 22 63 6d 64  . /* 166 */ "cmd
3166e 20 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52 4f 4d   ::= DELETE FROM
3166f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 64 65 78 65   fullname indexe
31670 64 5f 6f 70 74 20 77 68 65 72 65 5f 6f 70 74 22  d_opt where_opt"
31671 2c 0a 20 2f 2a 20 31 36 37 20 2a 2f 20 22 77 68  ,. /* 167 */ "wh
31672 65 72 65 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f  ere_opt ::=",. /
31673 2a 20 31 36 38 20 2a 2f 20 22 77 68 65 72 65 5f  * 168 */ "where_
31674 6f 70 74 20 3a 3a 3d 20 57 48 45 52 45 20 65 78  opt ::= WHERE ex
31675 70 72 22 2c 0a 20 2f 2a 20 31 36 39 20 2a 2f 20  pr",. /* 169 */ 
31676 22 63 6d 64 20 3a 3a 3d 20 55 50 44 41 54 45 20  "cmd ::= UPDATE 
31677 6f 72 63 6f 6e 66 20 66 75 6c 6c 6e 61 6d 65 20  orconf fullname 
31678 69 6e 64 65 78 65 64 5f 6f 70 74 20 53 45 54 20  indexed_opt SET 
31679 73 65 74 6c 69 73 74 20 77 68 65 72 65 5f 6f 70  setlist where_op
3167a 74 22 2c 0a 20 2f 2a 20 31 37 30 20 2a 2f 20 22  t",. /* 170 */ "
3167b 73 65 74 6c 69 73 74 20 3a 3a 3d 20 73 65 74 6c  setlist ::= setl
3167c 69 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 45 51 20  ist COMMA nm EQ 
3167d 65 78 70 72 22 2c 0a 20 2f 2a 20 31 37 31 20 2a  expr",. /* 171 *
3167e 2f 20 22 73 65 74 6c 69 73 74 20 3a 3a 3d 20 6e  / "setlist ::= n
3167f 6d 20 45 51 20 65 78 70 72 22 2c 0a 20 2f 2a 20  m EQ expr",. /* 
31680 31 37 32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  172 */ "cmd ::= 
31681 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20  insert_cmd INTO 
31682 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c  fullname inscoll
31683 69 73 74 5f 6f 70 74 20 56 41 4c 55 45 53 20 4c  ist_opt VALUES L
31684 50 20 69 74 65 6d 6c 69 73 74 20 52 50 22 2c 0a  P itemlist RP",.
31685 20 2f 2a 20 31 37 33 20 2a 2f 20 22 63 6d 64 20   /* 173 */ "cmd 
31686 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49  ::= insert_cmd I
31687 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73  NTO fullname ins
31688 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73 65 6c 65  collist_opt sele
31689 63 74 22 2c 0a 20 2f 2a 20 31 37 34 20 2a 2f 20  ct",. /* 174 */ 
3168a 22 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f  "cmd ::= insert_
3168b 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d  cmd INTO fullnam
3168c 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74  e inscollist_opt
3168d 20 44 45 46 41 55 4c 54 20 56 41 4c 55 45 53 22   DEFAULT VALUES"
3168e 2c 0a 20 2f 2a 20 31 37 35 20 2a 2f 20 22 69 6e  ,. /* 175 */ "in
3168f 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20 49 4e 53  sert_cmd ::= INS
31690 45 52 54 20 6f 72 63 6f 6e 66 22 2c 0a 20 2f 2a  ERT orconf",. /*
31691 20 31 37 36 20 2a 2f 20 22 69 6e 73 65 72 74 5f   176 */ "insert_
31692 63 6d 64 20 3a 3a 3d 20 52 45 50 4c 41 43 45 22  cmd ::= REPLACE"
31693 2c 0a 20 2f 2a 20 31 37 37 20 2a 2f 20 22 69 74  ,. /* 177 */ "it
31694 65 6d 6c 69 73 74 20 3a 3a 3d 20 69 74 65 6d 6c  emlist ::= iteml
31695 69 73 74 20 43 4f 4d 4d 41 20 65 78 70 72 22 2c  ist COMMA expr",
31696 0a 20 2f 2a 20 31 37 38 20 2a 2f 20 22 69 74 65  . /* 178 */ "ite
31697 6d 6c 69 73 74 20 3a 3a 3d 20 65 78 70 72 22 2c  mlist ::= expr",
31698 0a 20 2f 2a 20 31 37 39 20 2a 2f 20 22 69 6e 73  . /* 179 */ "ins
31699 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 22  collist_opt ::="
3169a 2c 0a 20 2f 2a 20 31 38 30 20 2a 2f 20 22 69 6e  ,. /* 180 */ "in
3169b 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d  scollist_opt ::=
3169c 20 4c 50 20 69 6e 73 63 6f 6c 6c 69 73 74 20 52   LP inscollist R
3169d 50 22 2c 0a 20 2f 2a 20 31 38 31 20 2a 2f 20 22  P",. /* 181 */ "
3169e 69 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 69  inscollist ::= i
3169f 6e 73 63 6f 6c 6c 69 73 74 20 43 4f 4d 4d 41 20  nscollist COMMA 
316a0 6e 6d 22 2c 0a 20 2f 2a 20 31 38 32 20 2a 2f 20  nm",. /* 182 */ 
316a1 22 69 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20  "inscollist ::= 
316a2 6e 6d 22 2c 0a 20 2f 2a 20 31 38 33 20 2a 2f 20  nm",. /* 183 */ 
316a3 22 65 78 70 72 20 3a 3a 3d 20 74 65 72 6d 22 2c  "expr ::= term",
316a4 0a 20 2f 2a 20 31 38 34 20 2a 2f 20 22 65 78 70  . /* 184 */ "exp
316a5 72 20 3a 3a 3d 20 4c 50 20 65 78 70 72 20 52 50  r ::= LP expr RP
316a6 22 2c 0a 20 2f 2a 20 31 38 35 20 2a 2f 20 22 74  ",. /* 185 */ "t
316a7 65 72 6d 20 3a 3a 3d 20 4e 55 4c 4c 22 2c 0a 20  erm ::= NULL",. 
316a8 2f 2a 20 31 38 36 20 2a 2f 20 22 65 78 70 72 20  /* 186 */ "expr 
316a9 3a 3a 3d 20 69 64 22 2c 0a 20 2f 2a 20 31 38 37  ::= id",. /* 187
316aa 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4a 4f   */ "expr ::= JO
316ab 49 4e 5f 4b 57 22 2c 0a 20 2f 2a 20 31 38 38 20  IN_KW",. /* 188 
316ac 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 6e 6d 20  */ "expr ::= nm 
316ad 44 4f 54 20 6e 6d 22 2c 0a 20 2f 2a 20 31 38 39  DOT nm",. /* 189
316ae 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 6e 6d   */ "expr ::= nm
316af 20 44 4f 54 20 6e 6d 20 44 4f 54 20 6e 6d 22 2c   DOT nm DOT nm",
316b0 0a 20 2f 2a 20 31 39 30 20 2a 2f 20 22 74 65 72  . /* 190 */ "ter
316b1 6d 20 3a 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c  m ::= INTEGER|FL
316b2 4f 41 54 7c 42 4c 4f 42 22 2c 0a 20 2f 2a 20 31  OAT|BLOB",. /* 1
316b3 39 31 20 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20  91 */ "term ::= 
316b4 53 54 52 49 4e 47 22 2c 0a 20 2f 2a 20 31 39 32  STRING",. /* 192
316b5 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 52 45   */ "expr ::= RE
316b6 47 49 53 54 45 52 22 2c 0a 20 2f 2a 20 31 39 33  GISTER",. /* 193
316b7 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 56 41   */ "expr ::= VA
316b8 52 49 41 42 4c 45 22 2c 0a 20 2f 2a 20 31 39 34  RIABLE",. /* 194
316b9 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
316ba 70 72 20 43 4f 4c 4c 41 54 45 20 69 64 73 22 2c  pr COLLATE ids",
316bb 0a 20 2f 2a 20 31 39 35 20 2a 2f 20 22 65 78 70  . /* 195 */ "exp
316bc 72 20 3a 3a 3d 20 43 41 53 54 20 4c 50 20 65 78  r ::= CAST LP ex
316bd 70 72 20 41 53 20 74 79 70 65 74 6f 6b 65 6e 20  pr AS typetoken 
316be 52 50 22 2c 0a 20 2f 2a 20 31 39 36 20 2a 2f 20  RP",. /* 196 */ 
316bf 22 65 78 70 72 20 3a 3a 3d 20 49 44 20 4c 50 20  "expr ::= ID LP 
316c0 64 69 73 74 69 6e 63 74 20 65 78 70 72 6c 69 73  distinct exprlis
316c1 74 20 52 50 22 2c 0a 20 2f 2a 20 31 39 37 20 2a  t RP",. /* 197 *
316c2 2f 20 22 65 78 70 72 20 3a 3a 3d 20 49 44 20 4c  / "expr ::= ID L
316c3 50 20 53 54 41 52 20 52 50 22 2c 0a 20 2f 2a 20  P STAR RP",. /* 
316c4 31 39 38 20 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d  198 */ "term ::=
316c5 20 43 54 49 4d 45 5f 4b 57 22 2c 0a 20 2f 2a 20   CTIME_KW",. /* 
316c6 31 39 39 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  199 */ "expr ::=
316c7 20 65 78 70 72 20 41 4e 44 20 65 78 70 72 22 2c   expr AND expr",
316c8 0a 20 2f 2a 20 32 30 30 20 2a 2f 20 22 65 78 70  . /* 200 */ "exp
316c9 72 20 3a 3a 3d 20 65 78 70 72 20 4f 52 20 65 78  r ::= expr OR ex
316ca 70 72 22 2c 0a 20 2f 2a 20 32 30 31 20 2a 2f 20  pr",. /* 201 */ 
316cb 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 4c  "expr ::= expr L
316cc 54 7c 47 54 7c 47 45 7c 4c 45 20 65 78 70 72 22  T|GT|GE|LE expr"
316cd 2c 0a 20 2f 2a 20 32 30 32 20 2a 2f 20 22 65 78  ,. /* 202 */ "ex
316ce 70 72 20 3a 3a 3d 20 65 78 70 72 20 45 51 7c 4e  pr ::= expr EQ|N
316cf 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 33  E expr",. /* 203
316d0 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
316d1 70 72 20 42 49 54 41 4e 44 7c 42 49 54 4f 52 7c  pr BITAND|BITOR|
316d2 4c 53 48 49 46 54 7c 52 53 48 49 46 54 20 65 78  LSHIFT|RSHIFT ex
316d3 70 72 22 2c 0a 20 2f 2a 20 32 30 34 20 2a 2f 20  pr",. /* 204 */ 
316d4 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 50  "expr ::= expr P
316d5 4c 55 53 7c 4d 49 4e 55 53 20 65 78 70 72 22 2c  LUS|MINUS expr",
316d6 0a 20 2f 2a 20 32 30 35 20 2a 2f 20 22 65 78 70  . /* 205 */ "exp
316d7 72 20 3a 3a 3d 20 65 78 70 72 20 53 54 41 52 7c  r ::= expr STAR|
316d8 53 4c 41 53 48 7c 52 45 4d 20 65 78 70 72 22 2c  SLASH|REM expr",
316d9 0a 20 2f 2a 20 32 30 36 20 2a 2f 20 22 65 78 70  . /* 206 */ "exp
316da 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4e 43 41  r ::= expr CONCA
316db 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 37  T expr",. /* 207
316dc 20 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20   */ "likeop ::= 
316dd 4c 49 4b 45 5f 4b 57 22 2c 0a 20 2f 2a 20 32 30  LIKE_KW",. /* 20
316de 38 20 2a 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d  8 */ "likeop ::=
316df 20 4e 4f 54 20 4c 49 4b 45 5f 4b 57 22 2c 0a 20   NOT LIKE_KW",. 
316e0 2f 2a 20 32 30 39 20 2a 2f 20 22 6c 69 6b 65 6f  /* 209 */ "likeo
316e1 70 20 3a 3a 3d 20 4d 41 54 43 48 22 2c 0a 20 2f  p ::= MATCH",. /
316e2 2a 20 32 31 30 20 2a 2f 20 22 6c 69 6b 65 6f 70  * 210 */ "likeop
316e3 20 3a 3a 3d 20 4e 4f 54 20 4d 41 54 43 48 22 2c   ::= NOT MATCH",
316e4 0a 20 2f 2a 20 32 31 31 20 2a 2f 20 22 65 73 63  . /* 211 */ "esc
316e5 61 70 65 20 3a 3a 3d 20 45 53 43 41 50 45 20 65  ape ::= ESCAPE e
316e6 78 70 72 22 2c 0a 20 2f 2a 20 32 31 32 20 2a 2f  xpr",. /* 212 */
316e7 20 22 65 73 63 61 70 65 20 3a 3a 3d 22 2c 0a 20   "escape ::=",. 
316e8 2f 2a 20 32 31 33 20 2a 2f 20 22 65 78 70 72 20  /* 213 */ "expr 
316e9 3a 3a 3d 20 65 78 70 72 20 6c 69 6b 65 6f 70 20  ::= expr likeop 
316ea 65 78 70 72 20 65 73 63 61 70 65 22 2c 0a 20 2f  expr escape",. /
316eb 2a 20 32 31 34 20 2a 2f 20 22 65 78 70 72 20 3a  * 214 */ "expr :
316ec 3a 3d 20 65 78 70 72 20 49 53 4e 55 4c 4c 7c 4e  := expr ISNULL|N
316ed 4f 54 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 32 31 35  OTNULL",. /* 215
316ee 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
316ef 70 72 20 4e 4f 54 20 4e 55 4c 4c 22 2c 0a 20 2f  pr NOT NULL",. /
316f0 2a 20 32 31 36 20 2a 2f 20 22 65 78 70 72 20 3a  * 216 */ "expr :
316f1 3a 3d 20 65 78 70 72 20 49 53 20 65 78 70 72 22  := expr IS expr"
316f2 2c 0a 20 2f 2a 20 32 31 37 20 2a 2f 20 22 65 78  ,. /* 217 */ "ex
316f3 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20 4e  pr ::= expr IS N
316f4 4f 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 31  OT expr",. /* 21
316f5 38 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4e  8 */ "expr ::= N
316f6 4f 54 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 31  OT expr",. /* 21
316f7 39 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 42  9 */ "expr ::= B
316f8 49 54 4e 4f 54 20 65 78 70 72 22 2c 0a 20 2f 2a  ITNOT expr",. /*
316f9 20 32 32 30 20 2a 2f 20 22 65 78 70 72 20 3a 3a   220 */ "expr ::
316fa 3d 20 4d 49 4e 55 53 20 65 78 70 72 22 2c 0a 20  = MINUS expr",. 
316fb 2f 2a 20 32 32 31 20 2a 2f 20 22 65 78 70 72 20  /* 221 */ "expr 
316fc 3a 3a 3d 20 50 4c 55 53 20 65 78 70 72 22 2c 0a  ::= PLUS expr",.
316fd 20 2f 2a 20 32 32 32 20 2a 2f 20 22 62 65 74 77   /* 222 */ "betw
316fe 65 65 6e 5f 6f 70 20 3a 3a 3d 20 42 45 54 57 45  een_op ::= BETWE
316ff 45 4e 22 2c 0a 20 2f 2a 20 32 32 33 20 2a 2f 20  EN",. /* 223 */ 
31700 22 62 65 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20  "between_op ::= 
31701 4e 4f 54 20 42 45 54 57 45 45 4e 22 2c 0a 20 2f  NOT BETWEEN",. /
31702 2a 20 32 32 34 20 2a 2f 20 22 65 78 70 72 20 3a  * 224 */ "expr :
31703 3a 3d 20 65 78 70 72 20 62 65 74 77 65 65 6e 5f  := expr between_
31704 6f 70 20 65 78 70 72 20 41 4e 44 20 65 78 70 72  op expr AND expr
31705 22 2c 0a 20 2f 2a 20 32 32 35 20 2a 2f 20 22 69  ",. /* 225 */ "i
31706 6e 5f 6f 70 20 3a 3a 3d 20 49 4e 22 2c 0a 20 2f  n_op ::= IN",. /
31707 2a 20 32 32 36 20 2a 2f 20 22 69 6e 5f 6f 70 20  * 226 */ "in_op 
31708 3a 3a 3d 20 4e 4f 54 20 49 4e 22 2c 0a 20 2f 2a  ::= NOT IN",. /*
31709 20 32 32 37 20 2a 2f 20 22 65 78 70 72 20 3a 3a   227 */ "expr ::
3170a 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20  = expr in_op LP 
3170b 65 78 70 72 6c 69 73 74 20 52 50 22 2c 0a 20 2f  exprlist RP",. /
3170c 2a 20 32 32 38 20 2a 2f 20 22 65 78 70 72 20 3a  * 228 */ "expr :
3170d 3a 3d 20 4c 50 20 73 65 6c 65 63 74 20 52 50 22  := LP select RP"
3170e 2c 0a 20 2f 2a 20 32 32 39 20 2a 2f 20 22 65 78  ,. /* 229 */ "ex
3170f 70 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f  pr ::= expr in_o
31710 70 20 4c 50 20 73 65 6c 65 63 74 20 52 50 22 2c  p LP select RP",
31711 0a 20 2f 2a 20 32 33 30 20 2a 2f 20 22 65 78 70  . /* 230 */ "exp
31712 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f 6f 70  r ::= expr in_op
31713 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 32   nm dbnm",. /* 2
31714 33 31 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20  31 */ "expr ::= 
31715 45 58 49 53 54 53 20 4c 50 20 73 65 6c 65 63 74  EXISTS LP select
31716 20 52 50 22 2c 0a 20 2f 2a 20 32 33 32 20 2a 2f   RP",. /* 232 */
31717 20 22 65 78 70 72 20 3a 3a 3d 20 43 41 53 45 20   "expr ::= CASE 
31718 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 63 61 73  case_operand cas
31719 65 5f 65 78 70 72 6c 69 73 74 20 63 61 73 65 5f  e_exprlist case_
3171a 65 6c 73 65 20 45 4e 44 22 2c 0a 20 2f 2a 20 32  else END",. /* 2
3171b 33 33 20 2a 2f 20 22 63 61 73 65 5f 65 78 70 72  33 */ "case_expr
3171c 6c 69 73 74 20 3a 3a 3d 20 63 61 73 65 5f 65 78  list ::= case_ex
3171d 70 72 6c 69 73 74 20 57 48 45 4e 20 65 78 70 72  prlist WHEN expr
3171e 20 54 48 45 4e 20 65 78 70 72 22 2c 0a 20 2f 2a   THEN expr",. /*
3171f 20 32 33 34 20 2a 2f 20 22 63 61 73 65 5f 65 78   234 */ "case_ex
31720 70 72 6c 69 73 74 20 3a 3a 3d 20 57 48 45 4e 20  prlist ::= WHEN 
31721 65 78 70 72 20 54 48 45 4e 20 65 78 70 72 22 2c  expr THEN expr",
31722 0a 20 2f 2a 20 32 33 35 20 2a 2f 20 22 63 61 73  . /* 235 */ "cas
31723 65 5f 65 6c 73 65 20 3a 3a 3d 20 45 4c 53 45 20  e_else ::= ELSE 
31724 65 78 70 72 22 2c 0a 20 2f 2a 20 32 33 36 20 2a  expr",. /* 236 *
31725 2f 20 22 63 61 73 65 5f 65 6c 73 65 20 3a 3a 3d  / "case_else ::=
31726 22 2c 0a 20 2f 2a 20 32 33 37 20 2a 2f 20 22 63  ",. /* 237 */ "c
31727 61 73 65 5f 6f 70 65 72 61 6e 64 20 3a 3a 3d 20  ase_operand ::= 
31728 65 78 70 72 22 2c 0a 20 2f 2a 20 32 33 38 20 2a  expr",. /* 238 *
31729 2f 20 22 63 61 73 65 5f 6f 70 65 72 61 6e 64 20  / "case_operand 
3172a 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 33 39 20 2a 2f  ::=",. /* 239 */
3172b 20 22 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 6e   "exprlist ::= n
3172c 65 78 70 72 6c 69 73 74 22 2c 0a 20 2f 2a 20 32  exprlist",. /* 2
3172d 34 30 20 2a 2f 20 22 65 78 70 72 6c 69 73 74 20  40 */ "exprlist 
3172e 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 34 31 20 2a 2f  ::=",. /* 241 */
3172f 20 22 6e 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20   "nexprlist ::= 
31730 6e 65 78 70 72 6c 69 73 74 20 43 4f 4d 4d 41 20  nexprlist COMMA 
31731 65 78 70 72 22 2c 0a 20 2f 2a 20 32 34 32 20 2a  expr",. /* 242 *
31732 2f 20 22 6e 65 78 70 72 6c 69 73 74 20 3a 3a 3d  / "nexprlist ::=
31733 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 34 33 20   expr",. /* 243 
31734 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63 72 65 61  */ "cmd ::= crea
31735 74 65 6b 77 20 75 6e 69 71 75 65 66 6c 61 67 20  tekw uniqueflag 
31736 49 4e 44 45 58 20 69 66 6e 6f 74 65 78 69 73 74  INDEX ifnotexist
31737 73 20 6e 6d 20 64 62 6e 6d 20 4f 4e 20 6e 6d 20  s nm dbnm ON nm 
31738 4c 50 20 69 64 78 6c 69 73 74 20 52 50 22 2c 0a  LP idxlist RP",.
31739 20 2f 2a 20 32 34 34 20 2a 2f 20 22 75 6e 69 71   /* 244 */ "uniq
3173a 75 65 66 6c 61 67 20 3a 3a 3d 20 55 4e 49 51 55  ueflag ::= UNIQU
3173b 45 22 2c 0a 20 2f 2a 20 32 34 35 20 2a 2f 20 22  E",. /* 245 */ "
3173c 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d 22 2c  uniqueflag ::=",
3173d 0a 20 2f 2a 20 32 34 36 20 2a 2f 20 22 69 64 78  . /* 246 */ "idx
3173e 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  list_opt ::=",. 
3173f 2f 2a 20 32 34 37 20 2a 2f 20 22 69 64 78 6c 69  /* 247 */ "idxli
31740 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20 69 64  st_opt ::= LP id
31741 78 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 32  xlist RP",. /* 2
31742 34 38 20 2a 2f 20 22 69 64 78 6c 69 73 74 20 3a  48 */ "idxlist :
31743 3a 3d 20 69 64 78 6c 69 73 74 20 43 4f 4d 4d 41  := idxlist COMMA
31744 20 6e 6d 20 63 6f 6c 6c 61 74 65 20 73 6f 72 74   nm collate sort
31745 6f 72 64 65 72 22 2c 0a 20 2f 2a 20 32 34 39 20  order",. /* 249 
31746 2a 2f 20 22 69 64 78 6c 69 73 74 20 3a 3a 3d 20  */ "idxlist ::= 
31747 6e 6d 20 63 6f 6c 6c 61 74 65 20 73 6f 72 74 6f  nm collate sorto
31748 72 64 65 72 22 2c 0a 20 2f 2a 20 32 35 30 20 2a  rder",. /* 250 *
31749 2f 20 22 63 6f 6c 6c 61 74 65 20 3a 3a 3d 22 2c  / "collate ::=",
3174a 0a 20 2f 2a 20 32 35 31 20 2a 2f 20 22 63 6f 6c  . /* 251 */ "col
3174b 6c 61 74 65 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45  late ::= COLLATE
3174c 20 69 64 73 22 2c 0a 20 2f 2a 20 32 35 32 20 2a   ids",. /* 252 *
3174d 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20  / "cmd ::= DROP 
3174e 49 4e 44 45 58 20 69 66 65 78 69 73 74 73 20 66  INDEX ifexists f
3174f 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 32 35  ullname",. /* 25
31750 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 56 41  3 */ "cmd ::= VA
31751 43 55 55 4d 22 2c 0a 20 2f 2a 20 32 35 34 20 2a  CUUM",. /* 254 *
31752 2f 20 22 63 6d 64 20 3a 3a 3d 20 56 41 43 55 55  / "cmd ::= VACUU
31753 4d 20 6e 6d 22 2c 0a 20 2f 2a 20 32 35 35 20 2a  M nm",. /* 255 *
31754 2f 20 22 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d  / "cmd ::= PRAGM
31755 41 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20  A nm dbnm",. /* 
31756 32 35 36 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20  256 */ "cmd ::= 
31757 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45  PRAGMA nm dbnm E
31758 51 20 6e 6d 6e 75 6d 22 2c 0a 20 2f 2a 20 32 35  Q nmnum",. /* 25
31759 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50 52  7 */ "cmd ::= PR
3175a 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 4c 50 20  AGMA nm dbnm LP 
3175b 6e 6d 6e 75 6d 20 52 50 22 2c 0a 20 2f 2a 20 32  nmnum RP",. /* 2
3175c 35 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50  58 */ "cmd ::= P
3175d 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51  RAGMA nm dbnm EQ
3175e 20 6d 69 6e 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a   minus_num",. /*
3175f 20 32 35 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   259 */ "cmd ::=
31760 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20   PRAGMA nm dbnm 
31761 4c 50 20 6d 69 6e 75 73 5f 6e 75 6d 20 52 50 22  LP minus_num RP"
31762 2c 0a 20 2f 2a 20 32 36 30 20 2a 2f 20 22 6e 6d  ,. /* 260 */ "nm
31763 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d  num ::= plus_num
31764 22 2c 0a 20 2f 2a 20 32 36 31 20 2a 2f 20 22 6e  ",. /* 261 */ "n
31765 6d 6e 75 6d 20 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f  mnum ::= nm",. /
31766 2a 20 32 36 32 20 2a 2f 20 22 6e 6d 6e 75 6d 20  * 262 */ "nmnum 
31767 3a 3a 3d 20 4f 4e 22 2c 0a 20 2f 2a 20 32 36 33  ::= ON",. /* 263
31768 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 44   */ "nmnum ::= D
31769 45 4c 45 54 45 22 2c 0a 20 2f 2a 20 32 36 34 20  ELETE",. /* 264 
3176a 2a 2f 20 22 6e 6d 6e 75 6d 20 3a 3a 3d 20 44 45  */ "nmnum ::= DE
3176b 46 41 55 4c 54 22 2c 0a 20 2f 2a 20 32 36 35 20  FAULT",. /* 265 
3176c 2a 2f 20 22 70 6c 75 73 5f 6e 75 6d 20 3a 3a 3d  */ "plus_num ::=
3176d 20 70 6c 75 73 5f 6f 70 74 20 6e 75 6d 62 65 72   plus_opt number
3176e 22 2c 0a 20 2f 2a 20 32 36 36 20 2a 2f 20 22 6d  ",. /* 266 */ "m
3176f 69 6e 75 73 5f 6e 75 6d 20 3a 3a 3d 20 4d 49 4e  inus_num ::= MIN
31770 55 53 20 6e 75 6d 62 65 72 22 2c 0a 20 2f 2a 20  US number",. /* 
31771 32 36 37 20 2a 2f 20 22 6e 75 6d 62 65 72 20 3a  267 */ "number :
31772 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f 41 54  := INTEGER|FLOAT
31773 22 2c 0a 20 2f 2a 20 32 36 38 20 2a 2f 20 22 70  ",. /* 268 */ "p
31774 6c 75 73 5f 6f 70 74 20 3a 3a 3d 20 50 4c 55 53  lus_opt ::= PLUS
31775 22 2c 0a 20 2f 2a 20 32 36 39 20 2a 2f 20 22 70  ",. /* 269 */ "p
31776 6c 75 73 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f  lus_opt ::=",. /
31777 2a 20 32 37 30 20 2a 2f 20 22 63 6d 64 20 3a 3a  * 270 */ "cmd ::
31778 3d 20 63 72 65 61 74 65 6b 77 20 74 72 69 67 67  = createkw trigg
31779 65 72 5f 64 65 63 6c 20 42 45 47 49 4e 20 74 72  er_decl BEGIN tr
3177a 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 45  igger_cmd_list E
3177b 4e 44 22 2c 0a 20 2f 2a 20 32 37 31 20 2a 2f 20  ND",. /* 271 */ 
3177c 22 74 72 69 67 67 65 72 5f 64 65 63 6c 20 3a 3a  "trigger_decl ::
3177d 3d 20 74 65 6d 70 20 54 52 49 47 47 45 52 20 69  = temp TRIGGER i
3177e 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64 62  fnotexists nm db
3177f 6e 6d 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20  nm trigger_time 
31780 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20 4f 4e  trigger_event ON
31781 20 66 75 6c 6c 6e 61 6d 65 20 66 6f 72 65 61 63   fullname foreac
31782 68 5f 63 6c 61 75 73 65 20 77 68 65 6e 5f 63 6c  h_clause when_cl
31783 61 75 73 65 22 2c 0a 20 2f 2a 20 32 37 32 20 2a  ause",. /* 272 *
31784 2f 20 22 74 72 69 67 67 65 72 5f 74 69 6d 65 20  / "trigger_time 
31785 3a 3a 3d 20 42 45 46 4f 52 45 22 2c 0a 20 2f 2a  ::= BEFORE",. /*
31786 20 32 37 33 20 2a 2f 20 22 74 72 69 67 67 65 72   273 */ "trigger
31787 5f 74 69 6d 65 20 3a 3a 3d 20 41 46 54 45 52 22  _time ::= AFTER"
31788 2c 0a 20 2f 2a 20 32 37 34 20 2a 2f 20 22 74 72  ,. /* 274 */ "tr
31789 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 49  igger_time ::= I
3178a 4e 53 54 45 41 44 20 4f 46 22 2c 0a 20 2f 2a 20  NSTEAD OF",. /* 
3178b 32 37 35 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  275 */ "trigger_
3178c 74 69 6d 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  time ::=",. /* 2
3178d 37 36 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 65  76 */ "trigger_e
3178e 76 65 6e 74 20 3a 3a 3d 20 44 45 4c 45 54 45 7c  vent ::= DELETE|
3178f 49 4e 53 45 52 54 22 2c 0a 20 2f 2a 20 32 37 37  INSERT",. /* 277
31790 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 65 76 65   */ "trigger_eve
31791 6e 74 20 3a 3a 3d 20 55 50 44 41 54 45 22 2c 0a  nt ::= UPDATE",.
31792 20 2f 2a 20 32 37 38 20 2a 2f 20 22 74 72 69 67   /* 278 */ "trig
31793 67 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 55 50  ger_event ::= UP
31794 44 41 54 45 20 4f 46 20 69 6e 73 63 6f 6c 6c 69  DATE OF inscolli
31795 73 74 22 2c 0a 20 2f 2a 20 32 37 39 20 2a 2f 20  st",. /* 279 */ 
31796 22 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 20  "foreach_clause 
31797 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 38 30 20 2a 2f  ::=",. /* 280 */
31798 20 22 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65   "foreach_clause
31799 20 3a 3a 3d 20 46 4f 52 20 45 41 43 48 20 52 4f   ::= FOR EACH RO
3179a 57 22 2c 0a 20 2f 2a 20 32 38 31 20 2a 2f 20 22  W",. /* 281 */ "
3179b 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d 22  when_clause ::="
3179c 2c 0a 20 2f 2a 20 32 38 32 20 2a 2f 20 22 77 68  ,. /* 282 */ "wh
3179d 65 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 57 48  en_clause ::= WH
3179e 45 4e 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 38  EN expr",. /* 28
3179f 33 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d  3 */ "trigger_cm
317a0 64 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69 67 67  d_list ::= trigg
317a1 65 72 5f 63 6d 64 5f 6c 69 73 74 20 74 72 69 67  er_cmd_list trig
317a2 67 65 72 5f 63 6d 64 20 53 45 4d 49 22 2c 0a 20  ger_cmd SEMI",. 
317a3 2f 2a 20 32 38 34 20 2a 2f 20 22 74 72 69 67 67  /* 284 */ "trigg
317a4 65 72 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20  er_cmd_list ::= 
317a5 74 72 69 67 67 65 72 5f 63 6d 64 20 53 45 4d 49  trigger_cmd SEMI
317a6 22 2c 0a 20 2f 2a 20 32 38 35 20 2a 2f 20 22 74  ",. /* 285 */ "t
317a7 72 6e 6d 20 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a  rnm ::= nm",. /*
317a8 20 32 38 36 20 2a 2f 20 22 74 72 6e 6d 20 3a 3a   286 */ "trnm ::
317a9 3d 20 6e 6d 20 44 4f 54 20 6e 6d 22 2c 0a 20 2f  = nm DOT nm",. /
317aa 2a 20 32 38 37 20 2a 2f 20 22 74 72 69 64 78 62  * 287 */ "tridxb
317ab 79 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 38 38 20  y ::=",. /* 288 
317ac 2a 2f 20 22 74 72 69 64 78 62 79 20 3a 3a 3d 20  */ "tridxby ::= 
317ad 49 4e 44 45 58 45 44 20 42 59 20 6e 6d 22 2c 0a  INDEXED BY nm",.
317ae 20 2f 2a 20 32 38 39 20 2a 2f 20 22 74 72 69 64   /* 289 */ "trid
317af 78 62 79 20 3a 3a 3d 20 4e 4f 54 20 49 4e 44 45  xby ::= NOT INDE
317b0 58 45 44 22 2c 0a 20 2f 2a 20 32 39 30 20 2a 2f  XED",. /* 290 */
317b1 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a   "trigger_cmd ::
317b2 3d 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66 20  = UPDATE orconf 
317b3 74 72 6e 6d 20 74 72 69 64 78 62 79 20 53 45 54  trnm tridxby SET
317b4 20 73 65 74 6c 69 73 74 20 77 68 65 72 65 5f 6f   setlist where_o
317b5 70 74 22 2c 0a 20 2f 2a 20 32 39 31 20 2a 2f 20  pt",. /* 291 */ 
317b6 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d  "trigger_cmd ::=
317b7 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f   insert_cmd INTO
317b8 20 74 72 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73 74   trnm inscollist
317b9 5f 6f 70 74 20 56 41 4c 55 45 53 20 4c 50 20 69  _opt VALUES LP i
317ba 74 65 6d 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a  temlist RP",. /*
317bb 20 32 39 32 20 2a 2f 20 22 74 72 69 67 67 65 72   292 */ "trigger
317bc 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f  _cmd ::= insert_
317bd 63 6d 64 20 49 4e 54 4f 20 74 72 6e 6d 20 69 6e  cmd INTO trnm in
317be 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73 65 6c  scollist_opt sel
317bf 65 63 74 22 2c 0a 20 2f 2a 20 32 39 33 20 2a 2f  ect",. /* 293 */
317c0 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a   "trigger_cmd ::
317c1 3d 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 72  = DELETE FROM tr
317c2 6e 6d 20 74 72 69 64 78 62 79 20 77 68 65 72 65  nm tridxby where
317c3 5f 6f 70 74 22 2c 0a 20 2f 2a 20 32 39 34 20 2a  _opt",. /* 294 *
317c4 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a  / "trigger_cmd :
317c5 3a 3d 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a 20  := select",. /* 
317c6 32 39 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d  295 */ "expr ::=
317c7 20 52 41 49 53 45 20 4c 50 20 49 47 4e 4f 52 45   RAISE LP IGNORE
317c8 20 52 50 22 2c 0a 20 2f 2a 20 32 39 36 20 2a 2f   RP",. /* 296 */
317c9 20 22 65 78 70 72 20 3a 3a 3d 20 52 41 49 53 45   "expr ::= RAISE
317ca 20 4c 50 20 72 61 69 73 65 74 79 70 65 20 43 4f   LP raisetype CO
317cb 4d 4d 41 20 6e 6d 20 52 50 22 2c 0a 20 2f 2a 20  MMA nm RP",. /* 
317cc 32 39 37 20 2a 2f 20 22 72 61 69 73 65 74 79 70  297 */ "raisetyp
317cd 65 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b 22 2c  e ::= ROLLBACK",
317ce 0a 20 2f 2a 20 32 39 38 20 2a 2f 20 22 72 61 69  . /* 298 */ "rai
317cf 73 65 74 79 70 65 20 3a 3a 3d 20 41 42 4f 52 54  setype ::= ABORT
317d0 22 2c 0a 20 2f 2a 20 32 39 39 20 2a 2f 20 22 72  ",. /* 299 */ "r
317d1 61 69 73 65 74 79 70 65 20 3a 3a 3d 20 46 41 49  aisetype ::= FAI
317d2 4c 22 2c 0a 20 2f 2a 20 33 30 30 20 2a 2f 20 22  L",. /* 300 */ "
317d3 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 52 49  cmd ::= DROP TRI
317d4 47 47 45 52 20 69 66 65 78 69 73 74 73 20 66 75  GGER ifexists fu
317d5 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 33 30 31  llname",. /* 301
317d6 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 54 54   */ "cmd ::= ATT
317d7 41 43 48 20 64 61 74 61 62 61 73 65 5f 6b 77 5f  ACH database_kw_
317d8 6f 70 74 20 65 78 70 72 20 41 53 20 65 78 70 72  opt expr AS expr
317d9 20 6b 65 79 5f 6f 70 74 22 2c 0a 20 2f 2a 20 33   key_opt",. /* 3
317da 30 32 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44  02 */ "cmd ::= D
317db 45 54 41 43 48 20 64 61 74 61 62 61 73 65 5f 6b  ETACH database_k
317dc 77 5f 6f 70 74 20 65 78 70 72 22 2c 0a 20 2f 2a  w_opt expr",. /*
317dd 20 33 30 33 20 2a 2f 20 22 6b 65 79 5f 6f 70 74   303 */ "key_opt
317de 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 30 34 20 2a   ::=",. /* 304 *
317df 2f 20 22 6b 65 79 5f 6f 70 74 20 3a 3a 3d 20 4b  / "key_opt ::= K
317e0 45 59 20 65 78 70 72 22 2c 0a 20 2f 2a 20 33 30  EY expr",. /* 30
317e1 35 20 2a 2f 20 22 64 61 74 61 62 61 73 65 5f 6b  5 */ "database_k
317e2 77 5f 6f 70 74 20 3a 3a 3d 20 44 41 54 41 42 41  w_opt ::= DATABA
317e3 53 45 22 2c 0a 20 2f 2a 20 33 30 36 20 2a 2f 20  SE",. /* 306 */ 
317e4 22 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74  "database_kw_opt
317e5 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 30 37 20 2a   ::=",. /* 307 *
317e6 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44  / "cmd ::= REIND
317e7 45 58 22 2c 0a 20 2f 2a 20 33 30 38 20 2a 2f 20  EX",. /* 308 */ 
317e8 22 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44 45 58  "cmd ::= REINDEX
317e9 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 33   nm dbnm",. /* 3
317ea 30 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41  09 */ "cmd ::= A
317eb 4e 41 4c 59 5a 45 22 2c 0a 20 2f 2a 20 33 31 30  NALYZE",. /* 310
317ec 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 4e 41   */ "cmd ::= ANA
317ed 4c 59 5a 45 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20  LYZE nm dbnm",. 
317ee 2f 2a 20 33 31 31 20 2a 2f 20 22 63 6d 64 20 3a  /* 311 */ "cmd :
317ef 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c 45 20 66  := ALTER TABLE f
317f0 75 6c 6c 6e 61 6d 65 20 52 45 4e 41 4d 45 20 54  ullname RENAME T
317f1 4f 20 6e 6d 22 2c 0a 20 2f 2a 20 33 31 32 20 2a  O nm",. /* 312 *
317f2 2f 20 22 63 6d 64 20 3a 3a 3d 20 41 4c 54 45 52  / "cmd ::= ALTER
317f3 20 54 41 42 4c 45 20 61 64 64 5f 63 6f 6c 75 6d   TABLE add_colum
317f4 6e 5f 66 75 6c 6c 6e 61 6d 65 20 41 44 44 20 6b  n_fullname ADD k
317f5 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 63 6f 6c 75  wcolumn_opt colu
317f6 6d 6e 22 2c 0a 20 2f 2a 20 33 31 33 20 2a 2f 20  mn",. /* 313 */ 
317f7 22 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c 6c  "add_column_full
317f8 6e 61 6d 65 20 3a 3a 3d 20 66 75 6c 6c 6e 61 6d  name ::= fullnam
317f9 65 22 2c 0a 20 2f 2a 20 33 31 34 20 2a 2f 20 22  e",. /* 314 */ "
317fa 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 3a 3a 3d  kwcolumn_opt ::=
317fb 22 2c 0a 20 2f 2a 20 33 31 35 20 2a 2f 20 22 6b  ",. /* 315 */ "k
317fc 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20 3a 3a 3d 20  wcolumn_opt ::= 
317fd 43 4f 4c 55 4d 4e 4b 57 22 2c 0a 20 2f 2a 20 33  COLUMNKW",. /* 3
317fe 31 36 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 63  16 */ "cmd ::= c
317ff 72 65 61 74 65 5f 76 74 61 62 22 2c 0a 20 2f 2a  reate_vtab",. /*
31800 20 33 31 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   317 */ "cmd ::=
31801 20 63 72 65 61 74 65 5f 76 74 61 62 20 4c 50 20   create_vtab LP 
31802 76 74 61 62 61 72 67 6c 69 73 74 20 52 50 22 2c  vtabarglist RP",
31803 0a 20 2f 2a 20 33 31 38 20 2a 2f 20 22 63 72 65  . /* 318 */ "cre
31804 61 74 65 5f 76 74 61 62 20 3a 3a 3d 20 63 72 65  ate_vtab ::= cre
31805 61 74 65 6b 77 20 56 49 52 54 55 41 4c 20 54 41  atekw VIRTUAL TA
31806 42 4c 45 20 6e 6d 20 64 62 6e 6d 20 55 53 49 4e  BLE nm dbnm USIN
31807 47 20 6e 6d 22 2c 0a 20 2f 2a 20 33 31 39 20 2a  G nm",. /* 319 *
31808 2f 20 22 76 74 61 62 61 72 67 6c 69 73 74 20 3a  / "vtabarglist :
31809 3a 3d 20 76 74 61 62 61 72 67 22 2c 0a 20 2f 2a  := vtabarg",. /*
3180a 20 33 32 30 20 2a 2f 20 22 76 74 61 62 61 72 67   320 */ "vtabarg
3180b 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62 61 72 67  list ::= vtabarg
3180c 6c 69 73 74 20 43 4f 4d 4d 41 20 76 74 61 62 61  list COMMA vtaba
3180d 72 67 22 2c 0a 20 2f 2a 20 33 32 31 20 2a 2f 20  rg",. /* 321 */ 
3180e 22 76 74 61 62 61 72 67 20 3a 3a 3d 22 2c 0a 20  "vtabarg ::=",. 
3180f 2f 2a 20 33 32 32 20 2a 2f 20 22 76 74 61 62 61  /* 322 */ "vtaba
31810 72 67 20 3a 3a 3d 20 76 74 61 62 61 72 67 20 76  rg ::= vtabarg v
31811 74 61 62 61 72 67 74 6f 6b 65 6e 22 2c 0a 20 2f  tabargtoken",. /
31812 2a 20 33 32 33 20 2a 2f 20 22 76 74 61 62 61 72  * 323 */ "vtabar
31813 67 74 6f 6b 65 6e 20 3a 3a 3d 20 41 4e 59 22 2c  gtoken ::= ANY",
31814 0a 20 2f 2a 20 33 32 34 20 2a 2f 20 22 76 74 61  . /* 324 */ "vta
31815 62 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20 6c 70  bargtoken ::= lp
31816 20 61 6e 79 6c 69 73 74 20 52 50 22 2c 0a 20 2f   anylist RP",. /
31817 2a 20 33 32 35 20 2a 2f 20 22 6c 70 20 3a 3a 3d  * 325 */ "lp ::=
31818 20 4c 50 22 2c 0a 20 2f 2a 20 33 32 36 20 2a 2f   LP",. /* 326 */
31819 20 22 61 6e 79 6c 69 73 74 20 3a 3a 3d 22 2c 0a   "anylist ::=",.
3181a 20 2f 2a 20 33 32 37 20 2a 2f 20 22 61 6e 79 6c   /* 327 */ "anyl
3181b 69 73 74 20 3a 3a 3d 20 61 6e 79 6c 69 73 74 20  ist ::= anylist 
3181c 4c 50 20 61 6e 79 6c 69 73 74 20 52 50 22 2c 0a  LP anylist RP",.
3181d 20 2f 2a 20 33 32 38 20 2a 2f 20 22 61 6e 79 6c   /* 328 */ "anyl
3181e 69 73 74 20 3a 3a 3d 20 61 6e 79 6c 69 73 74 20  ist ::= anylist 
3181f 41 4e 59 22 2c 0a 7d 3b 0a 23 65 6e 64 69 66 20  ANY",.};.#endif 
31820 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 0a 23  /* NDEBUG */...#
31821 69 66 20 59 59 53 54 41 43 4b 44 45 50 54 48 3c  if YYSTACKDEPTH<
31822 3d 30 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20  =0./*.** Try to 
31823 69 6e 63 72 65 61 73 65 20 74 68 65 20 73 69 7a  increase the siz
31824 65 20 6f 66 20 74 68 65 20 70 61 72 73 65 72 20  e of the parser 
31825 73 74 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63  stack..*/.static
31826 20 76 6f 69 64 20 79 79 47 72 6f 77 53 74 61 63   void yyGrowStac
31827 6b 28 79 79 50 61 72 73 65 72 20 2a 70 29 7b 0a  k(yyParser *p){.
31828 20 20 69 6e 74 20 6e 65 77 53 69 7a 65 3b 0a 20    int newSize;. 
31829 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 70   yyStackEntry *p
3182a 4e 65 77 3b 0a 0a 20 20 6e 65 77 53 69 7a 65 20  New;..  newSize 
3182b 3d 20 70 2d 3e 79 79 73 74 6b 73 7a 2a 32 20 2b  = p->yystksz*2 +
3182c 20 31 30 30 3b 0a 20 20 70 4e 65 77 20 3d 20 72   100;.  pNew = r
3182d 65 61 6c 6c 6f 63 28 70 2d 3e 79 79 73 74 61 63  ealloc(p->yystac
3182e 6b 2c 20 6e 65 77 53 69 7a 65 2a 73 69 7a 65 6f  k, newSize*sizeo
3182f 66 28 70 4e 65 77 5b 30 5d 29 29 3b 0a 20 20 69  f(pNew[0]));.  i
31830 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 70  f( pNew ){.    p
31831 2d 3e 79 79 73 74 61 63 6b 20 3d 20 70 4e 65 77  ->yystack = pNew
31832 3b 0a 20 20 20 20 70 2d 3e 79 79 73 74 6b 73 7a  ;.    p->yystksz
31833 20 3d 20 6e 65 77 53 69 7a 65 3b 0a 23 69 66 6e   = newSize;.#ifn
31834 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 69  def NDEBUG.    i
31835 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29  f( yyTraceFILE )
31836 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  {.      fprintf(
31837 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73 53  yyTraceFILE,"%sS
31838 74 61 63 6b 20 67 72 6f 77 73 20 74 6f 20 25 64  tack grows to %d
31839 20 65 6e 74 72 69 65 73 21 5c 6e 22 2c 0a 20 20   entries!\n",.  
3183a 20 20 20 20 20 20 20 20 20 20 20 20 79 79 54 72              yyTr
3183b 61 63 65 50 72 6f 6d 70 74 2c 20 70 2d 3e 79 79  acePrompt, p->yy
3183c 73 74 6b 73 7a 29 3b 0a 20 20 20 20 7d 0a 23 65  stksz);.    }.#e
3183d 6e 64 69 66 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69  ndif.  }.}.#endi
3183e 66 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 66  f../* .** This f
3183f 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61 74 65  unction allocate
31840 73 20 61 20 6e 65 77 20 70 61 72 73 65 72 2e 0a  s a new parser..
31841 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 61 72 67 75  ** The only argu
31842 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65  ment is a pointe
31843 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20  r to a function 
31844 77 68 69 63 68 20 77 6f 72 6b 73 20 6c 69 6b 65  which works like
31845 0a 2a 2a 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 0a 2a  .** malloc..**.*
31846 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a 20 41 20 70  * Inputs:.** A p
31847 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66 75  ointer to the fu
31848 6e 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 61  nction used to a
31849 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 2e 0a  llocate memory..
3184a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74 73 3a 0a 2a  **.** Outputs:.*
3184b 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  * A pointer to a
3184c 20 70 61 72 73 65 72 2e 20 20 54 68 69 73 20 70   parser.  This p
3184d 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64 20 69  ointer is used i
3184e 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  n subsequent cal
3184f 6c 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33  ls.** to sqlite3
31850 50 61 72 73 65 72 20 61 6e 64 20 73 71 6c 69 74  Parser and sqlit
31851 65 33 50 61 72 73 65 72 46 72 65 65 2e 0a 2a 2f  e3ParserFree..*/
31852 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
31853 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 72  void *sqlite3Par
31854 73 65 72 41 6c 6c 6f 63 28 76 6f 69 64 20 2a 28  serAlloc(void *(
31855 2a 6d 61 6c 6c 6f 63 50 72 6f 63 29 28 73 69 7a  *mallocProc)(siz
31856 65 5f 74 29 29 7b 0a 20 20 79 79 50 61 72 73 65  e_t)){.  yyParse
31857 72 20 2a 70 50 61 72 73 65 72 3b 0a 20 20 70 50  r *pParser;.  pP
31858 61 72 73 65 72 20 3d 20 28 79 79 50 61 72 73 65  arser = (yyParse
31859 72 2a 29 28 2a 6d 61 6c 6c 6f 63 50 72 6f 63 29  r*)(*mallocProc)
3185a 28 20 28 73 69 7a 65 5f 74 29 73 69 7a 65 6f 66  ( (size_t)sizeof
3185b 28 79 79 50 61 72 73 65 72 29 20 29 3b 0a 20 20  (yyParser) );.  
3185c 69 66 28 20 70 50 61 72 73 65 72 20 29 7b 0a 20  if( pParser ){. 
3185d 20 20 20 70 50 61 72 73 65 72 2d 3e 79 79 69 64     pParser->yyid
3185e 78 20 3d 20 2d 31 3b 0a 23 69 66 64 65 66 20 59  x = -1;.#ifdef Y
3185f 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45  YTRACKMAXSTACKDE
31860 50 54 48 0a 20 20 20 20 70 50 61 72 73 65 72 2d  PTH.    pParser-
31861 3e 79 79 69 64 78 4d 61 78 20 3d 20 30 3b 0a 23  >yyidxMax = 0;.#
31862 65 6e 64 69 66 0a 23 69 66 20 59 59 53 54 41 43  endif.#if YYSTAC
31863 4b 44 45 50 54 48 3c 3d 30 0a 20 20 20 20 70 50  KDEPTH<=0.    pP
31864 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 20 3d  arser->yystack =
31865 20 4e 55 4c 4c 3b 0a 20 20 20 20 70 50 61 72 73   NULL;.    pPars
31866 65 72 2d 3e 79 79 73 74 6b 73 7a 20 3d 20 30 3b  er->yystksz = 0;
31867 0a 20 20 20 20 79 79 47 72 6f 77 53 74 61 63 6b  .    yyGrowStack
31868 28 70 50 61 72 73 65 72 29 3b 0a 23 65 6e 64 69  (pParser);.#endi
31869 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  f.  }.  return p
3186a 50 61 72 73 65 72 3b 0a 7d 0a 0a 2f 2a 20 54 68  Parser;.}../* Th
3186b 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63  e following func
3186c 74 69 6f 6e 20 64 65 6c 65 74 65 73 20 74 68 65  tion deletes the
3186d 20 76 61 6c 75 65 20 61 73 73 6f 63 69 61 74 65   value associate
3186e 64 20 77 69 74 68 20 61 0a 2a 2a 20 73 79 6d 62  d with a.** symb
3186f 6f 6c 2e 20 20 54 68 65 20 73 79 6d 62 6f 6c 20  ol.  The symbol 
31870 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 20  can be either a 
31871 74 65 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f 6e 74  terminal or nont
31872 65 72 6d 69 6e 61 6c 2e 0a 2a 2a 20 22 79 79 6d  erminal..** "yym
31873 61 6a 6f 72 22 20 69 73 20 74 68 65 20 73 79 6d  ajor" is the sym
31874 62 6f 6c 20 63 6f 64 65 2c 20 61 6e 64 20 22 79  bol code, and "y
31875 79 70 6d 69 6e 6f 72 22 20 69 73 20 61 20 70 6f  ypminor" is a po
31876 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 68 65 20  inter to.** the 
31877 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  value..*/.static
31878 20 76 6f 69 64 20 79 79 5f 64 65 73 74 72 75 63   void yy_destruc
31879 74 6f 72 28 0a 20 20 79 79 50 61 72 73 65 72 20  tor(.  yyParser 
3187a 2a 79 79 70 50 61 72 73 65 72 2c 20 20 20 20 2f  *yypParser,    /
3187b 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a  * The parser */.
3187c 20 20 59 59 43 4f 44 45 54 59 50 45 20 79 79 6d    YYCODETYPE yym
3187d 61 6a 6f 72 2c 20 20 20 20 20 2f 2a 20 54 79 70  ajor,     /* Typ
3187e 65 20 63 6f 64 65 20 66 6f 72 20 6f 62 6a 65 63  e code for objec
3187f 74 20 74 6f 20 64 65 73 74 72 6f 79 20 2a 2f 0a  t to destroy */.
31880 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 2a 79    YYMINORTYPE *y
31881 79 70 6d 69 6e 6f 72 20 20 20 2f 2a 20 54 68 65  ypminor   /* The
31882 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20 64 65   object to be de
31883 73 74 72 6f 79 65 64 20 2a 2f 0a 29 7b 0a 20 20  stroyed */.){.  
31884 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
31885 5f 46 45 54 43 48 3b 0a 20 20 73 77 69 74 63 68  _FETCH;.  switch
31886 28 20 79 79 6d 61 6a 6f 72 20 29 7b 0a 20 20 20  ( yymajor ){.   
31887 20 2f 2a 20 48 65 72 65 20 69 73 20 69 6e 73 65   /* Here is inse
31888 72 74 65 64 20 74 68 65 20 61 63 74 69 6f 6e 73  rted the actions
31889 20 77 68 69 63 68 20 74 61 6b 65 20 70 6c 61 63   which take plac
3188a 65 20 77 68 65 6e 20 61 0a 20 20 20 20 2a 2a 20  e when a.    ** 
3188b 74 65 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f 6e 2d  terminal or non-
3188c 74 65 72 6d 69 6e 61 6c 20 69 73 20 64 65 73 74  terminal is dest
3188d 72 6f 79 65 64 2e 20 20 54 68 69 73 20 63 61 6e  royed.  This can
3188e 20 68 61 70 70 65 6e 0a 20 20 20 20 2a 2a 20 77   happen.    ** w
3188f 68 65 6e 20 74 68 65 20 73 79 6d 62 6f 6c 20 69  hen the symbol i
31890 73 20 70 6f 70 70 65 64 20 66 72 6f 6d 20 74 68  s popped from th
31891 65 20 73 74 61 63 6b 20 64 75 72 69 6e 67 20 61  e stack during a
31892 0a 20 20 20 20 2a 2a 20 72 65 64 75 63 65 20 6f  .    ** reduce o
31893 72 20 64 75 72 69 6e 67 20 65 72 72 6f 72 20 70  r during error p
31894 72 6f 63 65 73 73 69 6e 67 20 6f 72 20 77 68 65  rocessing or whe
31895 6e 20 61 20 70 61 72 73 65 72 20 69 73 20 0a 20  n a parser is . 
31896 20 20 20 2a 2a 20 62 65 69 6e 67 20 64 65 73 74     ** being dest
31897 72 6f 79 65 64 20 62 65 66 6f 72 65 20 69 74 20  royed before it 
31898 69 73 20 66 69 6e 69 73 68 65 64 20 70 61 72 73  is finished pars
31899 69 6e 67 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ing..    **.    
3189a 2a 2a 20 4e 6f 74 65 3a 20 64 75 72 69 6e 67 20  ** Note: during 
3189b 61 20 72 65 64 75 63 65 2c 20 74 68 65 20 6f 6e  a reduce, the on
3189c 6c 79 20 73 79 6d 62 6f 6c 73 20 64 65 73 74 72  ly symbols destr
3189d 6f 79 65 64 20 61 72 65 20 74 68 6f 73 65 0a 20  oyed are those. 
3189e 20 20 20 2a 2a 20 77 68 69 63 68 20 61 70 70 65     ** which appe
3189f 61 72 20 6f 6e 20 74 68 65 20 52 48 53 20 6f 66  ar on the RHS of
318a0 20 74 68 65 20 72 75 6c 65 2c 20 62 75 74 20 77   the rule, but w
318a1 68 69 63 68 20 61 72 65 20 6e 6f 74 20 75 73 65  hich are not use
318a2 64 0a 20 20 20 20 2a 2a 20 69 6e 73 69 64 65 20  d.    ** inside 
318a3 74 68 65 20 43 20 63 6f 64 65 2e 0a 20 20 20 20  the C code..    
318a4 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 36 30 3a  */.    case 160:
318a5 20 2f 2a 20 73 65 6c 65 63 74 20 2a 2f 0a 20 20   /* select */.  
318a6 20 20 63 61 73 65 20 31 39 34 3a 20 2f 2a 20 6f    case 194: /* o
318a7 6e 65 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a 73 71  neselect */.{.sq
318a8 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74  lite3SelectDelet
318a9 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79  e(pParse->db, (y
318aa 79 70 6d 69 6e 6f 72 2d 3e 79 79 33 29 29 3b 0a  ypminor->yy3));.
318ab 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
318ac 20 20 20 63 61 73 65 20 31 37 34 3a 20 2f 2a 20     case 174: /* 
318ad 74 65 72 6d 20 2a 2f 0a 20 20 20 20 63 61 73 65  term */.    case
318ae 20 31 37 35 3a 20 2f 2a 20 65 78 70 72 20 2a 2f   175: /* expr */
318af 0a 20 20 20 20 63 61 73 65 20 32 32 33 3a 20 2f  .    case 223: /
318b0 2a 20 65 73 63 61 70 65 20 2a 2f 0a 7b 0a 73 71  * escape */.{.sq
318b1 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
318b2 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79 70  pParse->db, (yyp
318b3 6d 69 6e 6f 72 2d 3e 79 79 33 34 36 29 2e 70 45  minor->yy346).pE
318b4 78 70 72 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72  xpr);.}.      br
318b5 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 31 37  eak;.    case 17
318b6 39 3a 20 2f 2a 20 69 64 78 6c 69 73 74 5f 6f 70  9: /* idxlist_op
318b7 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 38  t */.    case 18
318b8 37 3a 20 2f 2a 20 69 64 78 6c 69 73 74 20 2a 2f  7: /* idxlist */
318b9 0a 20 20 20 20 63 61 73 65 20 31 39 37 3a 20 2f  .    case 197: /
318ba 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20 2a 2f 0a  * selcollist */.
318bb 20 20 20 20 63 61 73 65 20 32 30 30 3a 20 2f 2a      case 200: /*
318bc 20 67 72 6f 75 70 62 79 5f 6f 70 74 20 2a 2f 0a   groupby_opt */.
318bd 20 20 20 20 63 61 73 65 20 32 30 32 3a 20 2f 2a      case 202: /*
318be 20 6f 72 64 65 72 62 79 5f 6f 70 74 20 2a 2f 0a   orderby_opt */.
318bf 20 20 20 20 63 61 73 65 20 32 30 34 3a 20 2f 2a      case 204: /*
318c0 20 73 63 6c 70 20 2a 2f 0a 20 20 20 20 63 61 73   sclp */.    cas
318c1 65 20 32 31 34 3a 20 2f 2a 20 73 6f 72 74 6c 69  e 214: /* sortli
318c2 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  st */.    case 2
318c3 31 36 3a 20 2f 2a 20 6e 65 78 70 72 6c 69 73 74  16: /* nexprlist
318c4 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 31 37   */.    case 217
318c5 3a 20 2f 2a 20 73 65 74 6c 69 73 74 20 2a 2f 0a  : /* setlist */.
318c6 20 20 20 20 63 61 73 65 20 32 32 30 3a 20 2f 2a      case 220: /*
318c7 20 69 74 65 6d 6c 69 73 74 20 2a 2f 0a 20 20 20   itemlist */.   
318c8 20 63 61 73 65 20 32 32 31 3a 20 2f 2a 20 65 78   case 221: /* ex
318c9 70 72 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61  prlist */.    ca
318ca 73 65 20 32 32 37 3a 20 2f 2a 20 63 61 73 65 5f  se 227: /* case_
318cb 65 78 70 72 6c 69 73 74 20 2a 2f 0a 7b 0a 73 71  exprlist */.{.sq
318cc 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
318cd 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
318ce 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 31 34 29  (yypminor->yy14)
318cf 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b  );.}.      break
318d0 3b 0a 20 20 20 20 63 61 73 65 20 31 39 33 3a 20  ;.    case 193: 
318d1 2f 2a 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 20  /* fullname */. 
318d2 20 20 20 63 61 73 65 20 31 39 38 3a 20 2f 2a 20     case 198: /* 
318d3 66 72 6f 6d 20 2a 2f 0a 20 20 20 20 63 61 73 65  from */.    case
318d4 20 32 30 36 3a 20 2f 2a 20 73 65 6c 74 61 62 6c   206: /* seltabl
318d5 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  ist */.    case 
318d6 32 30 37 3a 20 2f 2a 20 73 74 6c 5f 70 72 65 66  207: /* stl_pref
318d7 69 78 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 53  ix */.{.sqlite3S
318d8 72 63 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61  rcListDelete(pPa
318d9 72 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e  rse->db, (yypmin
318da 6f 72 2d 3e 79 79 36 35 29 29 3b 0a 7d 0a 20 20  or->yy65));.}.  
318db 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
318dc 61 73 65 20 31 39 39 3a 20 2f 2a 20 77 68 65 72  ase 199: /* wher
318dd 65 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73  e_opt */.    cas
318de 65 20 32 30 31 3a 20 2f 2a 20 68 61 76 69 6e 67  e 201: /* having
318df 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  _opt */.    case
318e0 20 32 31 30 3a 20 2f 2a 20 6f 6e 5f 6f 70 74 20   210: /* on_opt 
318e1 2a 2f 0a 20 20 20 20 63 61 73 65 20 32 31 35 3a  */.    case 215:
318e2 20 2f 2a 20 73 6f 72 74 69 74 65 6d 20 2a 2f 0a   /* sortitem */.
318e3 20 20 20 20 63 61 73 65 20 32 32 36 3a 20 2f 2a      case 226: /*
318e4 20 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 2a 2f   case_operand */
318e5 0a 20 20 20 20 63 61 73 65 20 32 32 38 3a 20 2f  .    case 228: /
318e6 2a 20 63 61 73 65 5f 65 6c 73 65 20 2a 2f 0a 20  * case_else */. 
318e7 20 20 20 63 61 73 65 20 32 33 39 3a 20 2f 2a 20     case 239: /* 
318e8 77 68 65 6e 5f 63 6c 61 75 73 65 20 2a 2f 0a 20  when_clause */. 
318e9 20 20 20 63 61 73 65 20 32 34 34 3a 20 2f 2a 20     case 244: /* 
318ea 6b 65 79 5f 6f 70 74 20 2a 2f 0a 7b 0a 73 71 6c  key_opt */.{.sql
318eb 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 70  ite3ExprDelete(p
318ec 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79 70 6d  Parse->db, (yypm
318ed 69 6e 6f 72 2d 3e 79 79 31 33 32 29 29 3b 0a 7d  inor->yy132));.}
318ee 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
318ef 20 20 63 61 73 65 20 32 31 31 3a 20 2f 2a 20 75    case 211: /* u
318f0 73 69 6e 67 5f 6f 70 74 20 2a 2f 0a 20 20 20 20  sing_opt */.    
318f1 63 61 73 65 20 32 31 33 3a 20 2f 2a 20 69 6e 73  case 213: /* ins
318f2 63 6f 6c 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63  collist */.    c
318f3 61 73 65 20 32 31 39 3a 20 2f 2a 20 69 6e 73 63  ase 219: /* insc
318f4 6f 6c 6c 69 73 74 5f 6f 70 74 20 2a 2f 0a 7b 0a  ollist_opt */.{.
318f5 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c  sqlite3IdListDel
318f6 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
318f7 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 34 30 38  (yypminor->yy408
318f8 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61  ));.}.      brea
318f9 6b 3b 0a 20 20 20 20 63 61 73 65 20 32 33 35 3a  k;.    case 235:
318fa 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 5f   /* trigger_cmd_
318fb 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  list */.    case
318fc 20 32 34 30 3a 20 2f 2a 20 74 72 69 67 67 65 72   240: /* trigger
318fd 5f 63 6d 64 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65  _cmd */.{.sqlite
318fe 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 53 74  3DeleteTriggerSt
318ff 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 28  ep(pParse->db, (
31900 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 34 37 33 29  yypminor->yy473)
31901 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b  );.}.      break
31902 3b 0a 20 20 20 20 63 61 73 65 20 32 33 37 3a 20  ;.    case 237: 
31903 2f 2a 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74  /* trigger_event
31904 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 49 64 4c   */.{.sqlite3IdL
31905 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  istDelete(pParse
31906 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d  ->db, (yypminor-
31907 3e 79 79 33 37 38 29 2e 62 29 3b 0a 7d 0a 20 20  >yy378).b);.}.  
31908 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64      break;.    d
31909 65 66 61 75 6c 74 3a 20 20 62 72 65 61 6b 3b 20  efault:  break; 
3190a 20 20 2f 2a 20 49 66 20 6e 6f 20 64 65 73 74 72    /* If no destr
3190b 75 63 74 6f 72 20 61 63 74 69 6f 6e 20 73 70 65  uctor action spe
3190c 63 69 66 69 65 64 3a 20 64 6f 20 6e 6f 74 68 69  cified: do nothi
3190d 6e 67 20 2a 2f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ng */.  }.}../*.
3190e 2a 2a 20 50 6f 70 20 74 68 65 20 70 61 72 73 65  ** Pop the parse
3190f 72 27 73 20 73 74 61 63 6b 20 6f 6e 63 65 2e 0a  r's stack once..
31910 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69  **.** If there i
31911 73 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 72  s a destructor r
31912 6f 75 74 69 6e 65 20 61 73 73 6f 63 69 61 74 65  outine associate
31913 64 20 77 69 74 68 20 74 68 65 20 74 6f 6b 65 6e  d with the token
31914 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 70 6f 70   which.** is pop
31915 70 65 64 20 66 72 6f 6d 20 74 68 65 20 73 74 61  ped from the sta
31916 63 6b 2c 20 74 68 65 6e 20 63 61 6c 6c 20 69 74  ck, then call it
31917 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ..**.** Return t
31918 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 6e  he major token n
31919 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 73 79  umber for the sy
3191a 6d 62 6f 6c 20 70 6f 70 70 65 64 2e 0a 2a 2f 0a  mbol popped..*/.
3191b 73 74 61 74 69 63 20 69 6e 74 20 79 79 5f 70 6f  static int yy_po
3191c 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28 79  p_parser_stack(y
3191d 79 50 61 72 73 65 72 20 2a 70 50 61 72 73 65 72  yParser *pParser
3191e 29 7b 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20  ){.  YYCODETYPE 
3191f 79 79 6d 61 6a 6f 72 3b 0a 20 20 79 79 53 74 61  yymajor;.  yySta
31920 63 6b 45 6e 74 72 79 20 2a 79 79 74 6f 73 20 3d  ckEntry *yytos =
31921 20 26 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61   &pParser->yysta
31922 63 6b 5b 70 50 61 72 73 65 72 2d 3e 79 79 69 64  ck[pParser->yyid
31923 78 5d 3b 0a 0a 20 20 2f 2a 20 54 68 65 72 65 20  x];..  /* There 
31924 69 73 20 6e 6f 20 6d 65 63 68 61 6e 69 73 6d 20  is no mechanism 
31925 62 79 20 77 68 69 63 68 20 74 68 65 20 70 61 72  by which the par
31926 73 65 72 20 73 74 61 63 6b 20 63 61 6e 20 62 65  ser stack can be
31927 20 70 6f 70 70 65 64 20 62 65 6c 6f 77 0a 20 20   popped below.  
31928 2a 2a 20 65 6d 70 74 79 20 69 6e 20 53 51 4c 69  ** empty in SQLi
31929 74 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20 4e 45  te.  */.  if( NE
3192a 56 45 52 28 70 50 61 72 73 65 72 2d 3e 79 79 69  VER(pParser->yyi
3192b 64 78 3c 30 29 20 29 20 72 65 74 75 72 6e 20 30  dx<0) ) return 0
3192c 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ;.#ifndef NDEBUG
3192d 0a 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49  .  if( yyTraceFI
3192e 4c 45 20 26 26 20 70 50 61 72 73 65 72 2d 3e 79  LE && pParser->y
3192f 79 69 64 78 3e 3d 30 20 29 7b 0a 20 20 20 20 66  yidx>=0 ){.    f
31930 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49  printf(yyTraceFI
31931 4c 45 2c 22 25 73 50 6f 70 70 69 6e 67 20 25 73  LE,"%sPopping %s
31932 5c 6e 22 2c 0a 20 20 20 20 20 20 79 79 54 72 61  \n",.      yyTra
31933 63 65 50 72 6f 6d 70 74 2c 0a 20 20 20 20 20 20  cePrompt,.      
31934 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 74 6f  yyTokenName[yyto
31935 73 2d 3e 6d 61 6a 6f 72 5d 29 3b 0a 20 20 7d 0a  s->major]);.  }.
31936 23 65 6e 64 69 66 0a 20 20 79 79 6d 61 6a 6f 72  #endif.  yymajor
31937 20 3d 20 79 79 74 6f 73 2d 3e 6d 61 6a 6f 72 3b   = yytos->major;
31938 0a 20 20 79 79 5f 64 65 73 74 72 75 63 74 6f 72  .  yy_destructor
31939 28 70 50 61 72 73 65 72 2c 20 79 79 6d 61 6a 6f  (pParser, yymajo
3193a 72 2c 20 26 79 79 74 6f 73 2d 3e 6d 69 6e 6f 72  r, &yytos->minor
3193b 29 3b 0a 20 20 70 50 61 72 73 65 72 2d 3e 79 79  );.  pParser->yy
3193c 69 64 78 2d 2d 3b 0a 20 20 72 65 74 75 72 6e 20  idx--;.  return 
3193d 79 79 6d 61 6a 6f 72 3b 0a 7d 0a 0a 2f 2a 20 0a  yymajor;.}../* .
3193e 2a 2a 20 44 65 61 6c 6c 6f 63 61 74 65 20 61 6e  ** Deallocate an
3193f 64 20 64 65 73 74 72 6f 79 20 61 20 70 61 72 73  d destroy a pars
31940 65 72 2e 20 20 44 65 73 74 72 75 63 74 6f 72 73  er.  Destructors
31941 20 61 72 65 20 61 6c 6c 20 63 61 6c 6c 65 64 20   are all called 
31942 66 6f 72 0a 2a 2a 20 61 6c 6c 20 73 74 61 63 6b  for.** all stack
31943 20 65 6c 65 6d 65 6e 74 73 20 62 65 66 6f 72 65   elements before
31944 20 73 68 75 74 74 69 6e 67 20 74 68 65 20 70 61   shutting the pa
31945 72 73 65 72 20 64 6f 77 6e 2e 0a 2a 2a 0a 2a 2a  rser down..**.**
31946 20 49 6e 70 75 74 73 3a 0a 2a 2a 20 3c 75 6c 3e   Inputs:.** <ul>
31947 0a 2a 2a 20 3c 6c 69 3e 20 20 41 20 70 6f 69 6e  .** <li>  A poin
31948 74 65 72 20 74 6f 20 74 68 65 20 70 61 72 73 65  ter to the parse
31949 72 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20  r.  This should 
3194a 62 65 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20  be a pointer.** 
3194b 20 20 20 20 20 20 6f 62 74 61 69 6e 65 64 20 66        obtained f
3194c 72 6f 6d 20 73 71 6c 69 74 65 33 50 61 72 73 65  rom sqlite3Parse
3194d 72 41 6c 6c 6f 63 2e 0a 2a 2a 20 3c 6c 69 3e 20  rAlloc..** <li> 
3194e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   A pointer to a 
3194f 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 74 6f  function used to
31950 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 20   reclaim memory 
31951 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 20 20 20 20  obtained.**     
31952 20 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a    from malloc..*
31953 2a 20 3c 2f 75 6c 3e 0a 2a 2f 0a 53 51 4c 49 54  * </ul>.*/.SQLIT
31954 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
31955 71 6c 69 74 65 33 50 61 72 73 65 72 46 72 65 65  qlite3ParserFree
31956 28 0a 20 20 76 6f 69 64 20 2a 70 2c 20 20 20 20  (.  void *p,    
31957 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31958 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 74 6f  /* The parser to
31959 20 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20   be deleted */. 
3195a 20 76 6f 69 64 20 28 2a 66 72 65 65 50 72 6f 63   void (*freeProc
3195b 29 28 76 6f 69 64 2a 29 20 20 20 20 20 2f 2a 20  )(void*)     /* 
3195c 46 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 74 6f  Function used to
3195d 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79 20   reclaim memory 
3195e 2a 2f 0a 29 7b 0a 20 20 79 79 50 61 72 73 65 72  */.){.  yyParser
3195f 20 2a 70 50 61 72 73 65 72 20 3d 20 28 79 79 50   *pParser = (yyP
31960 61 72 73 65 72 2a 29 70 3b 0a 20 20 2f 2a 20 49  arser*)p;.  /* I
31961 6e 20 53 51 4c 69 74 65 2c 20 77 65 20 6e 65 76  n SQLite, we nev
31962 65 72 20 74 72 79 20 74 6f 20 64 65 73 74 72 6f  er try to destro
31963 79 20 61 20 70 61 72 73 65 72 20 74 68 61 74 20  y a parser that 
31964 77 61 73 20 6e 6f 74 20 73 75 63 63 65 73 73 66  was not successf
31965 75 6c 6c 79 0a 20 20 2a 2a 20 63 72 65 61 74 65  ully.  ** create
31966 64 20 69 6e 20 74 68 65 20 66 69 72 73 74 20 70  d in the first p
31967 6c 61 63 65 2e 20 2a 2f 0a 20 20 69 66 28 20 4e  lace. */.  if( N
31968 45 56 45 52 28 70 50 61 72 73 65 72 3d 3d 30 29  EVER(pParser==0)
31969 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 77 68 69   ) return;.  whi
3196a 6c 65 28 20 70 50 61 72 73 65 72 2d 3e 79 79 69  le( pParser->yyi
3196b 64 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70  dx>=0 ) yy_pop_p
3196c 61 72 73 65 72 5f 73 74 61 63 6b 28 70 50 61 72  arser_stack(pPar
3196d 73 65 72 29 3b 0a 23 69 66 20 59 59 53 54 41 43  ser);.#if YYSTAC
3196e 4b 44 45 50 54 48 3c 3d 30 0a 20 20 66 72 65 65  KDEPTH<=0.  free
3196f 28 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63  (pParser->yystac
31970 6b 29 3b 0a 23 65 6e 64 69 66 0a 20 20 28 2a 66  k);.#endif.  (*f
31971 72 65 65 50 72 6f 63 29 28 28 76 6f 69 64 2a 29  reeProc)((void*)
31972 70 50 61 72 73 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a  pParser);.}../*.
31973 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 65  ** Return the pe
31974 61 6b 20 64 65 70 74 68 20 6f 66 20 74 68 65 20  ak depth of the 
31975 73 74 61 63 6b 20 66 6f 72 20 61 20 70 61 72 73  stack for a pars
31976 65 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 59 59  er..*/.#ifdef YY
31977 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45 50  TRACKMAXSTACKDEP
31978 54 48 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  TH.SQLITE_PRIVAT
31979 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 72  E int sqlite3Par
3197a 73 65 72 53 74 61 63 6b 50 65 61 6b 28 76 6f 69  serStackPeak(voi
3197b 64 20 2a 70 29 7b 0a 20 20 79 79 50 61 72 73 65  d *p){.  yyParse
3197c 72 20 2a 70 50 61 72 73 65 72 20 3d 20 28 79 79  r *pParser = (yy
3197d 50 61 72 73 65 72 2a 29 70 3b 0a 20 20 72 65 74  Parser*)p;.  ret
3197e 75 72 6e 20 70 50 61 72 73 65 72 2d 3e 79 79 69  urn pParser->yyi
3197f 64 78 4d 61 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a  dxMax;.}.#endif.
31980 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20  ./*.** Find the 
31981 61 70 70 72 6f 70 72 69 61 74 65 20 61 63 74 69  appropriate acti
31982 6f 6e 20 66 6f 72 20 61 20 70 61 72 73 65 72 20  on for a parser 
31983 67 69 76 65 6e 20 74 68 65 20 74 65 72 6d 69 6e  given the termin
31984 61 6c 0a 2a 2a 20 6c 6f 6f 6b 2d 61 68 65 61 64  al.** look-ahead
31985 20 74 6f 6b 65 6e 20 69 4c 6f 6f 6b 41 68 65 61   token iLookAhea
31986 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
31987 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e  look-ahead token
31988 20 69 73 20 59 59 4e 4f 43 4f 44 45 2c 20 74 68   is YYNOCODE, th
31989 65 6e 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20  en check to see 
3198a 69 66 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73  if the action is
3198b 0a 2a 2a 20 69 6e 64 65 70 65 6e 64 65 6e 74 20  .** independent 
3198c 6f 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61  of the look-ahea
3198d 64 2e 20 20 49 66 20 69 74 20 69 73 2c 20 72 65  d.  If it is, re
3198e 74 75 72 6e 20 74 68 65 20 61 63 74 69 6f 6e 2c  turn the action,
3198f 20 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20 72 65   otherwise.** re
31990 74 75 72 6e 20 59 59 5f 4e 4f 5f 41 43 54 49 4f  turn YY_NO_ACTIO
31991 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  N..*/.static int
31992 20 79 79 5f 66 69 6e 64 5f 73 68 69 66 74 5f 61   yy_find_shift_a
31993 63 74 69 6f 6e 28 0a 20 20 79 79 50 61 72 73 65  ction(.  yyParse
31994 72 20 2a 70 50 61 72 73 65 72 2c 20 20 20 20 20  r *pParser,     
31995 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
31996 20 2a 2f 0a 20 20 59 59 43 4f 44 45 54 59 50 45   */.  YYCODETYPE
31997 20 69 4c 6f 6f 6b 41 68 65 61 64 20 20 20 20 20   iLookAhead     
31998 2f 2a 20 54 68 65 20 6c 6f 6f 6b 2d 61 68 65 61  /* The look-ahea
31999 64 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20  d token */.){.  
3199a 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 73 74 61  int i;.  int sta
3199b 74 65 6e 6f 20 3d 20 70 50 61 72 73 65 72 2d 3e  teno = pParser->
3199c 79 79 73 74 61 63 6b 5b 70 50 61 72 73 65 72 2d  yystack[pParser-
3199d 3e 79 79 69 64 78 5d 2e 73 74 61 74 65 6e 6f 3b  >yyidx].stateno;
3199e 0a 20 0a 20 20 69 66 28 20 73 74 61 74 65 6e 6f  . .  if( stateno
3199f 3e 59 59 5f 53 48 49 46 54 5f 4d 41 58 20 7c 7c  >YY_SHIFT_MAX ||
319a0 20 28 69 20 3d 20 79 79 5f 73 68 69 66 74 5f 6f   (i = yy_shift_o
319a1 66 73 74 5b 73 74 61 74 65 6e 6f 5d 29 3d 3d 59  fst[stateno])==Y
319a2 59 5f 53 48 49 46 54 5f 55 53 45 5f 44 46 4c 54  Y_SHIFT_USE_DFLT
319a3 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 79   ){.    return y
319a4 79 5f 64 65 66 61 75 6c 74 5b 73 74 61 74 65 6e  y_default[staten
319a5 6f 5d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  o];.  }.  assert
319a6 28 20 69 4c 6f 6f 6b 41 68 65 61 64 21 3d 59 59  ( iLookAhead!=YY
319a7 4e 4f 43 4f 44 45 20 29 3b 0a 20 20 69 20 2b 3d  NOCODE );.  i +=
319a8 20 69 4c 6f 6f 6b 41 68 65 61 64 3b 0a 20 20 69   iLookAhead;.  i
319a9 66 28 20 69 3c 30 20 7c 7c 20 69 3e 3d 59 59 5f  f( i<0 || i>=YY_
319aa 53 5a 5f 41 43 54 54 41 42 20 7c 7c 20 79 79 5f  SZ_ACTTAB || yy_
319ab 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d 21 3d 69 4c  lookahead[i]!=iL
319ac 6f 6f 6b 41 68 65 61 64 20 29 7b 0a 20 20 20 20  ookAhead ){.    
319ad 2f 2a 20 54 68 65 20 75 73 65 72 20 6f 66 20 22  /* The user of "
319ae 3b 22 20 69 6e 73 74 65 61 64 20 6f 66 20 22 5c  ;" instead of "\
319af 30 30 30 22 20 61 73 20 61 20 73 74 61 74 65 6d  000" as a statem
319b0 65 6e 74 20 74 65 72 6d 69 6e 61 74 6f 72 20 69  ent terminator i
319b1 6e 20 53 51 4c 69 74 65 0a 20 20 20 20 2a 2a 20  n SQLite.    ** 
319b2 6d 65 61 6e 73 20 74 68 61 74 20 77 65 20 61 6c  means that we al
319b3 77 61 79 73 20 68 61 76 65 20 61 20 6c 6f 6f 6b  ways have a look
319b4 2d 61 68 65 61 64 20 74 6f 6b 65 6e 2e 20 2a 2f  -ahead token. */
319b5 0a 20 20 20 20 69 66 28 20 69 4c 6f 6f 6b 41 68  .    if( iLookAh
319b6 65 61 64 3e 30 20 29 7b 0a 23 69 66 64 65 66 20  ead>0 ){.#ifdef 
319b7 59 59 46 41 4c 4c 42 41 43 4b 0a 20 20 20 20 20  YYFALLBACK.     
319b8 20 59 59 43 4f 44 45 54 59 50 45 20 69 46 61 6c   YYCODETYPE iFal
319b9 6c 62 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20  lback;          
319ba 20 20 2f 2a 20 46 61 6c 6c 62 61 63 6b 20 74 6f    /* Fallback to
319bb 6b 65 6e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  ken */.      if(
319bc 20 69 4c 6f 6f 6b 41 68 65 61 64 3c 73 69 7a 65   iLookAhead<size
319bd 6f 66 28 79 79 46 61 6c 6c 62 61 63 6b 29 2f 73  of(yyFallback)/s
319be 69 7a 65 6f 66 28 79 79 46 61 6c 6c 62 61 63 6b  izeof(yyFallback
319bf 5b 30 5d 29 0a 20 20 20 20 20 20 20 20 20 20 20  [0]).           
319c0 20 20 26 26 20 28 69 46 61 6c 6c 62 61 63 6b 20    && (iFallback 
319c1 3d 20 79 79 46 61 6c 6c 62 61 63 6b 5b 69 4c 6f  = yyFallback[iLo
319c2 6f 6b 41 68 65 61 64 5d 29 21 3d 30 20 29 7b 0a  okAhead])!=0 ){.
319c3 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
319c4 20 20 20 20 20 20 20 69 66 28 20 79 79 54 72 61         if( yyTra
319c5 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 20  ceFILE ){.      
319c6 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72      fprintf(yyTr
319c7 61 63 65 46 49 4c 45 2c 20 22 25 73 46 41 4c 4c  aceFILE, "%sFALL
319c8 42 41 43 4b 20 25 73 20 3d 3e 20 25 73 5c 6e 22  BACK %s => %s\n"
319c9 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 79  ,.             y
319ca 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 20 79 79  yTracePrompt, yy
319cb 54 6f 6b 65 6e 4e 61 6d 65 5b 69 4c 6f 6f 6b 41  TokenName[iLookA
319cc 68 65 61 64 5d 2c 20 79 79 54 6f 6b 65 6e 4e 61  head], yyTokenNa
319cd 6d 65 5b 69 46 61 6c 6c 62 61 63 6b 5d 29 3b 0a  me[iFallback]);.
319ce 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66          }.#endif
319cf 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
319d0 79 79 5f 66 69 6e 64 5f 73 68 69 66 74 5f 61 63  yy_find_shift_ac
319d1 74 69 6f 6e 28 70 50 61 72 73 65 72 2c 20 69 46  tion(pParser, iF
319d2 61 6c 6c 62 61 63 6b 29 3b 0a 20 20 20 20 20 20  allback);.      
319d3 7d 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  }.#endif.#ifdef 
319d4 59 59 57 49 4c 44 43 41 52 44 0a 20 20 20 20 20  YYWILDCARD.     
319d5 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a   {.        int j
319d6 20 3d 20 69 20 2d 20 69 4c 6f 6f 6b 41 68 65 61   = i - iLookAhea
319d7 64 20 2b 20 59 59 57 49 4c 44 43 41 52 44 3b 0a  d + YYWILDCARD;.
319d8 20 20 20 20 20 20 20 20 69 66 28 20 6a 3e 3d 30          if( j>=0
319d9 20 26 26 20 6a 3c 59 59 5f 53 5a 5f 41 43 54 54   && j<YY_SZ_ACTT
319da 41 42 20 26 26 20 79 79 5f 6c 6f 6f 6b 61 68 65  AB && yy_lookahe
319db 61 64 5b 6a 5d 3d 3d 59 59 57 49 4c 44 43 41 52  ad[j]==YYWILDCAR
319dc 44 20 29 7b 0a 23 69 66 6e 64 65 66 20 4e 44 45  D ){.#ifndef NDE
319dd 42 55 47 0a 20 20 20 20 20 20 20 20 20 20 69 66  BUG.          if
319de 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b  ( yyTraceFILE ){
319df 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 70 72  .            fpr
319e0 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45  intf(yyTraceFILE
319e1 2c 20 22 25 73 57 49 4c 44 43 41 52 44 20 25 73  , "%sWILDCARD %s
319e2 20 3d 3e 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20   => %s\n",.     
319e3 20 20 20 20 20 20 20 20 20 20 79 79 54 72 61 63            yyTrac
319e4 65 50 72 6f 6d 70 74 2c 20 79 79 54 6f 6b 65 6e  ePrompt, yyToken
319e5 4e 61 6d 65 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d  Name[iLookAhead]
319e6 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 59 59  , yyTokenName[YY
319e7 57 49 4c 44 43 41 52 44 5d 29 3b 0a 20 20 20 20  WILDCARD]);.    
319e8 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f        }.#endif /
319e9 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 20 20 20 20  * NDEBUG */.    
319ea 20 20 20 20 20 20 72 65 74 75 72 6e 20 79 79 5f        return yy_
319eb 61 63 74 69 6f 6e 5b 6a 5d 3b 0a 20 20 20 20 20  action[j];.     
319ec 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e     }.      }.#en
319ed 64 69 66 20 2f 2a 20 59 59 57 49 4c 44 43 41 52  dif /* YYWILDCAR
319ee 44 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 72  D */.    }.    r
319ef 65 74 75 72 6e 20 79 79 5f 64 65 66 61 75 6c 74  eturn yy_default
319f0 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d 65 6c  [stateno];.  }el
319f1 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 79  se{.    return y
319f2 79 5f 61 63 74 69 6f 6e 5b 69 5d 3b 0a 20 20 7d  y_action[i];.  }
319f3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74  .}../*.** Find t
319f4 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 61  he appropriate a
319f5 63 74 69 6f 6e 20 66 6f 72 20 61 20 70 61 72 73  ction for a pars
319f6 65 72 20 67 69 76 65 6e 20 74 68 65 20 6e 6f 6e  er given the non
319f7 2d 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20 6c 6f 6f  -terminal.** loo
319f8 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 69 4c  k-ahead token iL
319f9 6f 6f 6b 41 68 65 61 64 2e 0a 2a 2a 0a 2a 2a 20  ookAhead..**.** 
319fa 49 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61  If the look-ahea
319fb 64 20 74 6f 6b 65 6e 20 69 73 20 59 59 4e 4f 43  d token is YYNOC
319fc 4f 44 45 2c 20 74 68 65 6e 20 63 68 65 63 6b 20  ODE, then check 
319fd 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 61 63  to see if the ac
319fe 74 69 6f 6e 20 69 73 0a 2a 2a 20 69 6e 64 65 70  tion is.** indep
319ff 65 6e 64 65 6e 74 20 6f 66 20 74 68 65 20 6c 6f  endent of the lo
31a00 6f 6b 2d 61 68 65 61 64 2e 20 20 49 66 20 69 74  ok-ahead.  If it
31a01 20 69 73 2c 20 72 65 74 75 72 6e 20 74 68 65 20   is, return the 
31a02 61 63 74 69 6f 6e 2c 20 6f 74 68 65 72 77 69 73  action, otherwis
31a03 65 0a 2a 2a 20 72 65 74 75 72 6e 20 59 59 5f 4e  e.** return YY_N
31a04 4f 5f 41 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 61  O_ACTION..*/.sta
31a05 74 69 63 20 69 6e 74 20 79 79 5f 66 69 6e 64 5f  tic int yy_find_
31a06 72 65 64 75 63 65 5f 61 63 74 69 6f 6e 28 0a 20  reduce_action(. 
31a07 20 69 6e 74 20 73 74 61 74 65 6e 6f 2c 20 20 20   int stateno,   
31a08 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75             /* Cu
31a09 72 72 65 6e 74 20 73 74 61 74 65 20 6e 75 6d 62  rrent state numb
31a0a 65 72 20 2a 2f 0a 20 20 59 59 43 4f 44 45 54 59  er */.  YYCODETY
31a0b 50 45 20 69 4c 6f 6f 6b 41 68 65 61 64 20 20 20  PE iLookAhead   
31a0c 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 6b 2d 61 68    /* The look-ah
31a0d 65 61 64 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a  ead token */.){.
31a0e 20 20 69 6e 74 20 69 3b 0a 23 69 66 64 65 66 20    int i;.#ifdef 
31a0f 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20  YYERRORSYMBOL.  
31a10 69 66 28 20 73 74 61 74 65 6e 6f 3e 59 59 5f 52  if( stateno>YY_R
31a11 45 44 55 43 45 5f 4d 41 58 20 29 7b 0a 20 20 20  EDUCE_MAX ){.   
31a12 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66 61 75   return yy_defau
31a13 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d  lt[stateno];.  }
31a14 0a 23 65 6c 73 65 0a 20 20 61 73 73 65 72 74 28  .#else.  assert(
31a15 20 73 74 61 74 65 6e 6f 3c 3d 59 59 5f 52 45 44   stateno<=YY_RED
31a16 55 43 45 5f 4d 41 58 20 29 3b 0a 23 65 6e 64 69  UCE_MAX );.#endi
31a17 66 0a 20 20 69 20 3d 20 79 79 5f 72 65 64 75 63  f.  i = yy_reduc
31a18 65 5f 6f 66 73 74 5b 73 74 61 74 65 6e 6f 5d 3b  e_ofst[stateno];
31a19 0a 20 20 61 73 73 65 72 74 28 20 69 21 3d 59 59  .  assert( i!=YY
31a1a 5f 52 45 44 55 43 45 5f 55 53 45 5f 44 46 4c 54  _REDUCE_USE_DFLT
31a1b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4c   );.  assert( iL
31a1c 6f 6f 6b 41 68 65 61 64 21 3d 59 59 4e 4f 43 4f  ookAhead!=YYNOCO
31a1d 44 45 20 29 3b 0a 20 20 69 20 2b 3d 20 69 4c 6f  DE );.  i += iLo
31a1e 6f 6b 41 68 65 61 64 3b 0a 23 69 66 64 65 66 20  okAhead;.#ifdef 
31a1f 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20  YYERRORSYMBOL.  
31a20 69 66 28 20 69 3c 30 20 7c 7c 20 69 3e 3d 59 59  if( i<0 || i>=YY
31a21 5f 53 5a 5f 41 43 54 54 41 42 20 7c 7c 20 79 79  _SZ_ACTTAB || yy
31a22 5f 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d 21 3d 69  _lookahead[i]!=i
31a23 4c 6f 6f 6b 41 68 65 61 64 20 29 7b 0a 20 20 20  LookAhead ){.   
31a24 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66 61 75   return yy_defau
31a25 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20 20 7d  lt[stateno];.  }
31a26 0a 23 65 6c 73 65 0a 20 20 61 73 73 65 72 74 28  .#else.  assert(
31a27 20 69 3e 3d 30 20 26 26 20 69 3c 59 59 5f 53 5a   i>=0 && i<YY_SZ
31a28 5f 41 43 54 54 41 42 20 29 3b 0a 20 20 61 73 73  _ACTTAB );.  ass
31a29 65 72 74 28 20 79 79 5f 6c 6f 6f 6b 61 68 65 61  ert( yy_lookahea
31a2a 64 5b 69 5d 3d 3d 69 4c 6f 6f 6b 41 68 65 61 64  d[i]==iLookAhead
31a2b 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74   );.#endif.  ret
31a2c 75 72 6e 20 79 79 5f 61 63 74 69 6f 6e 5b 69 5d  urn yy_action[i]
31a2d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  ;.}../*.** The f
31a2e 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65  ollowing routine
31a2f 20 69 73 20 63 61 6c 6c 65 64 20 69 66 20 74 68   is called if th
31a30 65 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77  e stack overflow
31a31 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  s..*/.static voi
31a32 64 20 79 79 53 74 61 63 6b 4f 76 65 72 66 6c 6f  d yyStackOverflo
31a33 77 28 79 79 50 61 72 73 65 72 20 2a 79 79 70 50  w(yyParser *yypP
31a34 61 72 73 65 72 2c 20 59 59 4d 49 4e 4f 52 54 59  arser, YYMINORTY
31a35 50 45 20 2a 79 79 70 4d 69 6e 6f 72 29 7b 0a 20  PE *yypMinor){. 
31a36 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
31a37 52 47 5f 46 45 54 43 48 3b 0a 20 20 20 79 79 70  RG_FETCH;.   yyp
31a38 50 61 72 73 65 72 2d 3e 79 79 69 64 78 2d 2d 3b  Parser->yyidx--;
31a39 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
31a3a 20 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49     if( yyTraceFI
31a3b 4c 45 20 29 7b 0a 20 20 20 20 20 66 70 72 69 6e  LE ){.     fprin
31a3c 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22  tf(yyTraceFILE,"
31a3d 25 73 53 74 61 63 6b 20 4f 76 65 72 66 6c 6f 77  %sStack Overflow
31a3e 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d  !\n",yyTraceProm
31a3f 70 74 29 3b 0a 20 20 20 7d 0a 23 65 6e 64 69 66  pt);.   }.#endif
31a40 0a 20 20 20 77 68 69 6c 65 28 20 79 79 70 50 61  .   while( yypPa
31a41 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29  rser->yyidx>=0 )
31a42 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73   yy_pop_parser_s
31a43 74 61 63 6b 28 79 79 70 50 61 72 73 65 72 29 3b  tack(yypParser);
31a44 0a 20 20 20 2f 2a 20 48 65 72 65 20 63 6f 64 65  .   /* Here code
31a45 20 69 73 20 69 6e 73 65 72 74 65 64 20 77 68 69   is inserted whi
31a46 63 68 20 77 69 6c 6c 20 65 78 65 63 75 74 65 20  ch will execute 
31a47 69 66 20 74 68 65 20 70 61 72 73 65 72 0a 20 20  if the parser.  
31a48 20 2a 2a 20 73 74 61 63 6b 20 65 76 65 72 79 20   ** stack every 
31a49 6f 76 65 72 66 6c 6f 77 73 20 2a 2f 0a 0a 20 20  overflows */..  
31a4a 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
31a4b 28 79 79 70 4d 69 6e 6f 72 29 3b 20 2f 2a 20 53  (yypMinor); /* S
31a4c 69 6c 65 6e 63 65 20 73 6f 6d 65 20 63 6f 6d 70  ilence some comp
31a4d 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 20 2a 2f  iler warnings */
31a4e 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d  .  sqlite3ErrorM
31a4f 73 67 28 70 50 61 72 73 65 2c 20 22 70 61 72 73  sg(pParse, "pars
31a50 65 72 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f  er stack overflo
31a51 77 22 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70  w");.  pParse->p
31a52 61 72 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20  arseError = 1;. 
31a53 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
31a54 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70  RG_STORE; /* Sup
31a55 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62  press warning ab
31a56 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78 74 72  out unused %extr
31a57 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 20 2a  a_argument var *
31a58 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f  /.}../*.** Perfo
31a59 72 6d 20 61 20 73 68 69 66 74 20 61 63 74 69 6f  rm a shift actio
31a5a 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
31a5b 64 20 79 79 5f 73 68 69 66 74 28 0a 20 20 79 79  d yy_shift(.  yy
31a5c 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73 65  Parser *yypParse
31a5d 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  r,          /* T
31a5e 68 65 20 70 61 72 73 65 72 20 74 6f 20 62 65 20  he parser to be 
31a5f 73 68 69 66 74 65 64 20 2a 2f 0a 20 20 69 6e 74  shifted */.  int
31a60 20 79 79 4e 65 77 53 74 61 74 65 2c 20 20 20 20   yyNewState,    
31a61 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
31a62 65 20 6e 65 77 20 73 74 61 74 65 20 74 6f 20 73  e new state to s
31a63 68 69 66 74 20 69 6e 20 2a 2f 0a 20 20 69 6e 74  hift in */.  int
31a64 20 79 79 4d 61 6a 6f 72 2c 20 20 20 20 20 20 20   yyMajor,       
31a65 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
31a66 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 74 6f  e major token to
31a67 20 73 68 69 66 74 20 69 6e 20 2a 2f 0a 20 20 59   shift in */.  Y
31a68 59 4d 49 4e 4f 52 54 59 50 45 20 2a 79 79 70 4d  YMINORTYPE *yypM
31a69 69 6e 6f 72 20 20 20 20 20 20 20 20 20 2f 2a 20  inor         /* 
31a6a 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d  Pointer to the m
31a6b 69 6e 6f 72 20 74 6f 6b 65 6e 20 74 6f 20 73 68  inor token to sh
31a6c 69 66 74 20 69 6e 20 2a 2f 0a 29 7b 0a 20 20 79  ift in */.){.  y
31a6d 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 79 79 74  yStackEntry *yyt
31a6e 6f 73 3b 0a 20 20 79 79 70 50 61 72 73 65 72 2d  os;.  yypParser-
31a6f 3e 79 79 69 64 78 2b 2b 3b 0a 23 69 66 64 65 66  >yyidx++;.#ifdef
31a70 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b   YYTRACKMAXSTACK
31a71 44 45 50 54 48 0a 20 20 69 66 28 20 79 79 70 50  DEPTH.  if( yypP
31a72 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 79 79 70  arser->yyidx>yyp
31a73 50 61 72 73 65 72 2d 3e 79 79 69 64 78 4d 61 78  Parser->yyidxMax
31a74 20 29 7b 0a 20 20 20 20 79 79 70 50 61 72 73 65   ){.    yypParse
31a75 72 2d 3e 79 79 69 64 78 4d 61 78 20 3d 20 79 79  r->yyidxMax = yy
31a76 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3b 0a  pParser->yyidx;.
31a77 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 20 59    }.#endif.#if Y
31a78 59 53 54 41 43 4b 44 45 50 54 48 3e 30 20 0a 20  YSTACKDEPTH>0 . 
31a79 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e   if( yypParser->
31a7a 79 79 69 64 78 3e 3d 59 59 53 54 41 43 4b 44 45  yyidx>=YYSTACKDE
31a7b 50 54 48 20 29 7b 0a 20 20 20 20 79 79 53 74 61  PTH ){.    yySta
31a7c 63 6b 4f 76 65 72 66 6c 6f 77 28 79 79 70 50 61  ckOverflow(yypPa
31a7d 72 73 65 72 2c 20 79 79 70 4d 69 6e 6f 72 29 3b  rser, yypMinor);
31a7e 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
31a7f 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 79 79 70  .#else.  if( yyp
31a80 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d 79  Parser->yyidx>=y
31a81 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 6b 73  ypParser->yystks
31a82 7a 20 29 7b 0a 20 20 20 20 79 79 47 72 6f 77 53  z ){.    yyGrowS
31a83 74 61 63 6b 28 79 79 70 50 61 72 73 65 72 29 3b  tack(yypParser);
31a84 0a 20 20 20 20 69 66 28 20 79 79 70 50 61 72 73  .    if( yypPars
31a85 65 72 2d 3e 79 79 69 64 78 3e 3d 79 79 70 50 61  er->yyidx>=yypPa
31a86 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a 20 29 7b  rser->yystksz ){
31a87 0a 20 20 20 20 20 20 79 79 53 74 61 63 6b 4f 76  .      yyStackOv
31a88 65 72 66 6c 6f 77 28 79 79 70 50 61 72 73 65 72  erflow(yypParser
31a89 2c 20 79 79 70 4d 69 6e 6f 72 29 3b 0a 20 20 20  , yypMinor);.   
31a8a 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
31a8b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79 79  .  }.#endif.  yy
31a8c 74 6f 73 20 3d 20 26 79 79 70 50 61 72 73 65 72  tos = &yypParser
31a8d 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70 50 61 72  ->yystack[yypPar
31a8e 73 65 72 2d 3e 79 79 69 64 78 5d 3b 0a 20 20 79  ser->yyidx];.  y
31a8f 79 74 6f 73 2d 3e 73 74 61 74 65 6e 6f 20 3d 20  ytos->stateno = 
31a90 28 59 59 41 43 54 49 4f 4e 54 59 50 45 29 79 79  (YYACTIONTYPE)yy
31a91 4e 65 77 53 74 61 74 65 3b 0a 20 20 79 79 74 6f  NewState;.  yyto
31a92 73 2d 3e 6d 61 6a 6f 72 20 3d 20 28 59 59 43 4f  s->major = (YYCO
31a93 44 45 54 59 50 45 29 79 79 4d 61 6a 6f 72 3b 0a  DETYPE)yyMajor;.
31a94 20 20 79 79 74 6f 73 2d 3e 6d 69 6e 6f 72 20 3d    yytos->minor =
31a95 20 2a 79 79 70 4d 69 6e 6f 72 3b 0a 23 69 66 6e   *yypMinor;.#ifn
31a96 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28  def NDEBUG.  if(
31a97 20 79 79 54 72 61 63 65 46 49 4c 45 20 26 26 20   yyTraceFILE && 
31a98 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
31a99 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  >0 ){.    int i;
31a9a 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54  .    fprintf(yyT
31a9b 72 61 63 65 46 49 4c 45 2c 22 25 73 53 68 69 66  raceFILE,"%sShif
31a9c 74 20 25 64 5c 6e 22 2c 79 79 54 72 61 63 65 50  t %d\n",yyTraceP
31a9d 72 6f 6d 70 74 2c 79 79 4e 65 77 53 74 61 74 65  rompt,yyNewState
31a9e 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79  );.    fprintf(y
31a9f 79 54 72 61 63 65 46 49 4c 45 2c 22 25 73 53 74  yTraceFILE,"%sSt
31aa0 61 63 6b 3a 22 2c 79 79 54 72 61 63 65 50 72 6f  ack:",yyTracePro
31aa1 6d 70 74 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  mpt);.    for(i=
31aa2 31 3b 20 69 3c 3d 79 79 70 50 61 72 73 65 72 2d  1; i<=yypParser-
31aa3 3e 79 79 69 64 78 3b 20 69 2b 2b 29 0a 20 20 20  >yyidx; i++).   
31aa4 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
31aa5 63 65 46 49 4c 45 2c 22 20 25 73 22 2c 79 79 54  ceFILE," %s",yyT
31aa6 6f 6b 65 6e 4e 61 6d 65 5b 79 79 70 50 61 72 73  okenName[yypPars
31aa7 65 72 2d 3e 79 79 73 74 61 63 6b 5b 69 5d 2e 6d  er->yystack[i].m
31aa8 61 6a 6f 72 5d 29 3b 0a 20 20 20 20 66 70 72 69  ajor]);.    fpri
31aa9 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
31aaa 22 5c 6e 22 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  "\n");.  }.#endi
31aab 66 0a 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c  f.}../* The foll
31aac 6f 77 69 6e 67 20 74 61 62 6c 65 20 63 6f 6e 74  owing table cont
31aad 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ains information
31aae 20 61 62 6f 75 74 20 65 76 65 72 79 20 72 75 6c   about every rul
31aaf 65 20 74 68 61 74 0a 2a 2a 20 69 73 20 75 73 65  e that.** is use
31ab0 64 20 64 75 72 69 6e 67 20 74 68 65 20 72 65 64  d during the red
31ab1 75 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  uce..*/.static c
31ab2 6f 6e 73 74 20 73 74 72 75 63 74 20 7b 0a 20 20  onst struct {.  
31ab3 59 59 43 4f 44 45 54 59 50 45 20 6c 68 73 3b 20  YYCODETYPE lhs; 
31ab4 20 20 20 20 20 20 20 20 2f 2a 20 53 79 6d 62 6f          /* Symbo
31ab5 6c 20 6f 6e 20 74 68 65 20 6c 65 66 74 2d 68 61  l on the left-ha
31ab6 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 72  nd side of the r
31ab7 75 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ule */.  unsigne
31ab8 64 20 63 68 61 72 20 6e 72 68 73 3b 20 20 20 20  d char nrhs;    
31ab9 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72 69   /* Number of ri
31aba 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 73 79  ght-hand side sy
31abb 6d 62 6f 6c 73 20 69 6e 20 74 68 65 20 72 75 6c  mbols in the rul
31abc 65 20 2a 2f 0a 7d 20 79 79 52 75 6c 65 49 6e 66  e */.} yyRuleInf
31abd 6f 5b 5d 20 3d 20 7b 0a 20 20 7b 20 31 34 32 2c  o[] = {.  { 142,
31abe 20 31 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 32   1 },.  { 143, 2
31abf 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 31 20 7d   },.  { 143, 1 }
31ac0 2c 0a 20 20 7b 20 31 34 34 2c 20 31 20 7d 2c 0a  ,.  { 144, 1 },.
31ac1 20 20 7b 20 31 34 34 2c 20 33 20 7d 2c 0a 20 20    { 144, 3 },.  
31ac2 7b 20 31 34 35 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 145, 0 },.  { 
31ac3 31 34 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  145, 1 },.  { 14
31ac4 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 36 2c  5, 3 },.  { 146,
31ac5 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33   1 },.  { 147, 3
31ac6 20 7d 2c 0a 20 20 7b 20 31 34 39 2c 20 30 20 7d   },.  { 149, 0 }
31ac7 2c 0a 20 20 7b 20 31 34 39 2c 20 31 20 7d 2c 0a  ,.  { 149, 1 },.
31ac8 20 20 7b 20 31 34 39 2c 20 32 20 7d 2c 0a 20 20    { 149, 2 },.  
31ac9 7b 20 31 34 38 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 148, 0 },.  { 
31aca 31 34 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  148, 1 },.  { 14
31acb 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 38 2c  8, 1 },.  { 148,
31acc 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32   1 },.  { 147, 2
31acd 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d   },.  { 147, 2 }
31ace 2c 0a 20 20 7b 20 31 34 37 2c 20 32 20 7d 2c 0a  ,.  { 147, 2 },.
31acf 20 20 7b 20 31 35 31 2c 20 31 20 7d 2c 0a 20 20    { 151, 1 },.  
31ad0 7b 20 31 35 31 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 151, 0 },.  { 
31ad1 31 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34  147, 2 },.  { 14
31ad2 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 3 },.  { 147,
31ad3 20 35 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 32   5 },.  { 147, 2
31ad4 20 7d 2c 0a 20 20 7b 20 31 35 32 2c 20 36 20 7d   },.  { 152, 6 }
31ad5 2c 0a 20 20 7b 20 31 35 34 2c 20 31 20 7d 2c 0a  ,.  { 154, 1 },.
31ad6 20 20 7b 20 31 35 36 2c 20 30 20 7d 2c 0a 20 20    { 156, 0 },.  
31ad7 7b 20 31 35 36 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 156, 3 },.  { 
31ad8 31 35 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 35  155, 1 },.  { 15
31ad9 35 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35 33 2c  5, 0 },.  { 153,
31ada 20 34 20 7d 2c 0a 20 20 7b 20 31 35 33 2c 20 32   4 },.  { 153, 2
31adb 20 7d 2c 0a 20 20 7b 20 31 35 38 2c 20 33 20 7d   },.  { 158, 3 }
31adc 2c 0a 20 20 7b 20 31 35 38 2c 20 31 20 7d 2c 0a  ,.  { 158, 1 },.
31add 20 20 7b 20 31 36 31 2c 20 33 20 7d 2c 0a 20 20    { 161, 3 },.  
31ade 7b 20 31 36 32 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 162, 1 },.  { 
31adf 31 36 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36  165, 1 },.  { 16
31ae0 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 36 2c  5, 1 },.  { 166,
31ae1 20 31 20 7d 2c 0a 20 20 7b 20 31 35 30 2c 20 31   1 },.  { 150, 1
31ae2 20 7d 2c 0a 20 20 7b 20 31 35 30 2c 20 31 20 7d   },.  { 150, 1 }
31ae3 2c 0a 20 20 7b 20 31 35 30 2c 20 31 20 7d 2c 0a  ,.  { 150, 1 },.
31ae4 20 20 7b 20 31 36 33 2c 20 30 20 7d 2c 0a 20 20    { 163, 0 },.  
31ae5 7b 20 31 36 33 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 163, 1 },.  { 
31ae6 31 36 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36  167, 1 },.  { 16
31ae7 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 36 37 2c  7, 4 },.  { 167,
31ae8 20 36 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 31   6 },.  { 168, 1
31ae9 20 7d 2c 0a 20 20 7b 20 31 36 38 2c 20 32 20 7d   },.  { 168, 2 }
31aea 2c 0a 20 20 7b 20 31 36 39 2c 20 31 20 7d 2c 0a  ,.  { 169, 1 },.
31aeb 20 20 7b 20 31 36 39 2c 20 31 20 7d 2c 0a 20 20    { 169, 1 },.  
31aec 7b 20 31 36 34 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 164, 2 },.  { 
31aed 31 36 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37  164, 0 },.  { 17
31aee 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 32 2c  2, 3 },.  { 172,
31aef 20 31 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 32   1 },.  { 173, 2
31af0 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 34 20 7d   },.  { 173, 4 }
31af1 2c 0a 20 20 7b 20 31 37 33 2c 20 33 20 7d 2c 0a  ,.  { 173, 3 },.
31af2 20 20 7b 20 31 37 33 2c 20 33 20 7d 2c 0a 20 20    { 173, 3 },.  
31af3 7b 20 31 37 33 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 173, 2 },.  { 
31af4 31 37 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37  173, 2 },.  { 17
31af5 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 33 2c  3, 3 },.  { 173,
31af6 20 35 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 32   5 },.  { 173, 2
31af7 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 34 20 7d   },.  { 173, 4 }
31af8 2c 0a 20 20 7b 20 31 37 33 2c 20 34 20 7d 2c 0a  ,.  { 173, 4 },.
31af9 20 20 7b 20 31 37 33 2c 20 31 20 7d 2c 0a 20 20    { 173, 1 },.  
31afa 7b 20 31 37 33 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 173, 2 },.  { 
31afb 31 37 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 37  178, 0 },.  { 17
31afc 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38 30 2c  8, 1 },.  { 180,
31afd 20 30 20 7d 2c 0a 20 20 7b 20 31 38 30 2c 20 32   0 },.  { 180, 2
31afe 20 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 32 20 7d   },.  { 182, 2 }
31aff 2c 0a 20 20 7b 20 31 38 32 2c 20 33 20 7d 2c 0a  ,.  { 182, 3 },.
31b00 20 20 7b 20 31 38 32 2c 20 33 20 7d 2c 0a 20 20    { 182, 3 },.  
31b01 7b 20 31 38 33 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 183, 2 },.  { 
31b02 31 38 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38  183, 2 },.  { 18
31b03 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38 33 2c  3, 1 },.  { 183,
31b04 20 31 20 7d 2c 0a 20 20 7b 20 31 38 33 2c 20 32   1 },.  { 183, 2
31b05 20 7d 2c 0a 20 20 7b 20 31 38 31 2c 20 33 20 7d   },.  { 181, 3 }
31b06 2c 0a 20 20 7b 20 31 38 31 2c 20 32 20 7d 2c 0a  ,.  { 181, 2 },.
31b07 20 20 7b 20 31 38 34 2c 20 30 20 7d 2c 0a 20 20    { 184, 0 },.  
31b08 7b 20 31 38 34 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 184, 2 },.  { 
31b09 31 38 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 35  184, 2 },.  { 15
31b0a 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35 39 2c  9, 0 },.  { 159,
31b0b 20 32 20 7d 2c 0a 20 20 7b 20 31 38 35 2c 20 33   2 },.  { 185, 3
31b0c 20 7d 2c 0a 20 20 7b 20 31 38 35 2c 20 32 20 7d   },.  { 185, 2 }
31b0d 2c 0a 20 20 7b 20 31 38 35 2c 20 31 20 7d 2c 0a  ,.  { 185, 1 },.
31b0e 20 20 7b 20 31 38 36 2c 20 32 20 7d 2c 0a 20 20    { 186, 2 },.  
31b0f 7b 20 31 38 36 2c 20 37 20 7d 2c 0a 20 20 7b 20  { 186, 7 },.  { 
31b10 31 38 36 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 38  186, 5 },.  { 18
31b11 36 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 38 36 2c  6, 5 },.  { 186,
31b12 20 31 30 20 7d 2c 0a 20 20 7b 20 31 38 38 2c 20   10 },.  { 188, 
31b13 30 20 7d 2c 0a 20 20 7b 20 31 38 38 2c 20 31 20  0 },.  { 188, 1 
31b14 7d 2c 0a 20 20 7b 20 31 37 36 2c 20 30 20 7d 2c  },.  { 176, 0 },
31b15 0a 20 20 7b 20 31 37 36 2c 20 33 20 7d 2c 0a 20  .  { 176, 3 },. 
31b16 20 7b 20 31 38 39 2c 20 30 20 7d 2c 0a 20 20 7b   { 189, 0 },.  {
31b17 20 31 38 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   189, 2 },.  { 1
31b18 39 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 30  90, 1 },.  { 190
31b19 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 30 2c 20  , 1 },.  { 190, 
31b1a 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 34 20  1 },.  { 147, 4 
31b1b 7d 2c 0a 20 20 7b 20 31 39 32 2c 20 32 20 7d 2c  },.  { 192, 2 },
31b1c 0a 20 20 7b 20 31 39 32 2c 20 30 20 7d 2c 0a 20  .  { 192, 0 },. 
31b1d 20 7b 20 31 34 37 2c 20 38 20 7d 2c 0a 20 20 7b   { 147, 8 },.  {
31b1e 20 31 34 37 2c 20 34 20 7d 2c 0a 20 20 7b 20 31   147, 4 },.  { 1
31b1f 34 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 30  47, 1 },.  { 160
31b20 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 30 2c 20  , 1 },.  { 160, 
31b21 33 20 7d 2c 0a 20 20 7b 20 31 39 35 2c 20 31 20  3 },.  { 195, 1 
31b22 7d 2c 0a 20 20 7b 20 31 39 35 2c 20 32 20 7d 2c  },.  { 195, 2 },
31b23 0a 20 20 7b 20 31 39 35 2c 20 31 20 7d 2c 0a 20  .  { 195, 1 },. 
31b24 20 7b 20 31 39 34 2c 20 39 20 7d 2c 0a 20 20 7b   { 194, 9 },.  {
31b25 20 31 39 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   196, 1 },.  { 1
31b26 39 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 36  96, 1 },.  { 196
31b27 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 34 2c 20  , 0 },.  { 204, 
31b28 32 20 7d 2c 0a 20 20 7b 20 32 30 34 2c 20 30 20  2 },.  { 204, 0 
31b29 7d 2c 0a 20 20 7b 20 31 39 37 2c 20 33 20 7d 2c  },.  { 197, 3 },
31b2a 0a 20 20 7b 20 31 39 37 2c 20 32 20 7d 2c 0a 20  .  { 197, 2 },. 
31b2b 20 7b 20 31 39 37 2c 20 34 20 7d 2c 0a 20 20 7b   { 197, 4 },.  {
31b2c 20 32 30 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   205, 2 },.  { 2
31b2d 30 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 30 35  05, 1 },.  { 205
31b2e 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 38 2c 20  , 0 },.  { 198, 
31b2f 30 20 7d 2c 0a 20 20 7b 20 31 39 38 2c 20 32 20  0 },.  { 198, 2 
31b30 7d 2c 0a 20 20 7b 20 32 30 37 2c 20 32 20 7d 2c  },.  { 207, 2 },
31b31 0a 20 20 7b 20 32 30 37 2c 20 30 20 7d 2c 0a 20  .  { 207, 0 },. 
31b32 20 7b 20 32 30 36 2c 20 37 20 7d 2c 0a 20 20 7b   { 206, 7 },.  {
31b33 20 32 30 36 2c 20 37 20 7d 2c 0a 20 20 7b 20 32   206, 7 },.  { 2
31b34 30 36 2c 20 37 20 7d 2c 0a 20 20 7b 20 31 35 37  06, 7 },.  { 157
31b35 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35 37 2c 20  , 0 },.  { 157, 
31b36 32 20 7d 2c 0a 20 20 7b 20 31 39 33 2c 20 32 20  2 },.  { 193, 2 
31b37 7d 2c 0a 20 20 7b 20 32 30 38 2c 20 31 20 7d 2c  },.  { 208, 1 },
31b38 0a 20 20 7b 20 32 30 38 2c 20 32 20 7d 2c 0a 20  .  { 208, 2 },. 
31b39 20 7b 20 32 30 38 2c 20 33 20 7d 2c 0a 20 20 7b   { 208, 3 },.  {
31b3a 20 32 30 38 2c 20 34 20 7d 2c 0a 20 20 7b 20 32   208, 4 },.  { 2
31b3b 31 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31 30  10, 2 },.  { 210
31b3c 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 39 2c 20  , 0 },.  { 209, 
31b3d 30 20 7d 2c 0a 20 20 7b 20 32 30 39 2c 20 33 20  0 },.  { 209, 3 
31b3e 7d 2c 0a 20 20 7b 20 32 30 39 2c 20 32 20 7d 2c  },.  { 209, 2 },
31b3f 0a 20 20 7b 20 32 31 31 2c 20 34 20 7d 2c 0a 20  .  { 211, 4 },. 
31b40 20 7b 20 32 31 31 2c 20 30 20 7d 2c 0a 20 20 7b   { 211, 0 },.  {
31b41 20 32 30 32 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   202, 0 },.  { 2
31b42 30 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 31 34  02, 3 },.  { 214
31b43 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 31 34 2c 20  , 4 },.  { 214, 
31b44 32 20 7d 2c 0a 20 20 7b 20 32 31 35 2c 20 31 20  2 },.  { 215, 1 
31b45 7d 2c 0a 20 20 7b 20 31 37 37 2c 20 31 20 7d 2c  },.  { 177, 1 },
31b46 0a 20 20 7b 20 31 37 37 2c 20 31 20 7d 2c 0a 20  .  { 177, 1 },. 
31b47 20 7b 20 31 37 37 2c 20 30 20 7d 2c 0a 20 20 7b   { 177, 0 },.  {
31b48 20 32 30 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   200, 0 },.  { 2
31b49 30 30 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 30 31  00, 3 },.  { 201
31b4a 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 31 2c 20  , 0 },.  { 201, 
31b4b 32 20 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 30 20  2 },.  { 203, 0 
31b4c 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 32 20 7d 2c  },.  { 203, 2 },
31b4d 0a 20 20 7b 20 32 30 33 2c 20 34 20 7d 2c 0a 20  .  { 203, 4 },. 
31b4e 20 7b 20 32 30 33 2c 20 34 20 7d 2c 0a 20 20 7b   { 203, 4 },.  {
31b4f 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31   147, 5 },.  { 1
31b50 39 39 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 39  99, 0 },.  { 199
31b51 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 2 },.  { 147, 
31b52 37 20 7d 2c 0a 20 20 7b 20 32 31 37 2c 20 35 20  7 },.  { 217, 5 
31b53 7d 2c 0a 20 20 7b 20 32 31 37 2c 20 33 20 7d 2c  },.  { 217, 3 },
31b54 0a 20 20 7b 20 31 34 37 2c 20 38 20 7d 2c 0a 20  .  { 147, 8 },. 
31b55 20 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b   { 147, 5 },.  {
31b56 20 31 34 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 32   147, 6 },.  { 2
31b57 31 38 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31 38  18, 2 },.  { 218
31b58 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 30 2c 20  , 1 },.  { 220, 
31b59 33 20 7d 2c 0a 20 20 7b 20 32 32 30 2c 20 31 20  3 },.  { 220, 1 
31b5a 7d 2c 0a 20 20 7b 20 32 31 39 2c 20 30 20 7d 2c  },.  { 219, 0 },
31b5b 0a 20 20 7b 20 32 31 39 2c 20 33 20 7d 2c 0a 20  .  { 219, 3 },. 
31b5c 20 7b 20 32 31 33 2c 20 33 20 7d 2c 0a 20 20 7b   { 213, 3 },.  {
31b5d 20 32 31 33 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   213, 1 },.  { 1
31b5e 37 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 1 },.  { 175
31b5f 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 34 2c 20  , 3 },.  { 174, 
31b60 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 31 20  1 },.  { 175, 1 
31b61 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 31 20 7d 2c  },.  { 175, 1 },
31b62 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20  .  { 175, 3 },. 
31b63 20 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20 20 7b   { 175, 5 },.  {
31b64 20 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   174, 1 },.  { 1
31b65 37 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35  74, 1 },.  { 175
31b66 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 1 },.  { 175, 
31b67 31 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20  1 },.  { 175, 3 
31b68 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 36 20 7d 2c  },.  { 175, 6 },
31b69 0a 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20  .  { 175, 5 },. 
31b6a 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b   { 175, 4 },.  {
31b6b 20 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   174, 1 },.  { 1
31b6c 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 3 },.  { 175
31b6d 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 3 },.  { 175, 
31b6e 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20  3 },.  { 175, 3 
31b6f 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c  },.  { 175, 3 },
31b70 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20  .  { 175, 3 },. 
31b71 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b   { 175, 3 },.  {
31b72 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 32   175, 3 },.  { 2
31b73 32 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 32  22, 1 },.  { 222
31b74 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 32 32 2c 20  , 2 },.  { 222, 
31b75 31 20 7d 2c 0a 20 20 7b 20 32 32 32 2c 20 32 20  1 },.  { 222, 2 
31b76 7d 2c 0a 20 20 7b 20 32 32 33 2c 20 32 20 7d 2c  },.  { 223, 2 },
31b77 0a 20 20 7b 20 32 32 33 2c 20 30 20 7d 2c 0a 20  .  { 223, 0 },. 
31b78 20 7b 20 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b   { 175, 4 },.  {
31b79 20 31 37 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   175, 2 },.  { 1
31b7a 37 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 3 },.  { 175
31b7b 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20  , 3 },.  { 175, 
31b7c 34 20 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 32 20  4 },.  { 175, 2 
31b7d 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 32 20 7d 2c  },.  { 175, 2 },
31b7e 0a 20 20 7b 20 31 37 35 2c 20 32 20 7d 2c 0a 20  .  { 175, 2 },. 
31b7f 20 7b 20 31 37 35 2c 20 32 20 7d 2c 0a 20 20 7b   { 175, 2 },.  {
31b80 20 32 32 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   224, 1 },.  { 2
31b81 32 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 35  24, 2 },.  { 175
31b82 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 32 35 2c 20  , 5 },.  { 225, 
31b83 31 20 7d 2c 0a 20 20 7b 20 32 32 35 2c 20 32 20  1 },.  { 225, 2 
31b84 7d 2c 0a 20 20 7b 20 31 37 35 2c 20 35 20 7d 2c  },.  { 175, 5 },
31b85 0a 20 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20  .  { 175, 3 },. 
31b86 20 7b 20 31 37 35 2c 20 35 20 7d 2c 0a 20 20 7b   { 175, 5 },.  {
31b87 20 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b 20 31   175, 4 },.  { 1
31b88 37 35 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 35  75, 4 },.  { 175
31b89 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 32 37 2c 20  , 5 },.  { 227, 
31b8a 35 20 7d 2c 0a 20 20 7b 20 32 32 37 2c 20 34 20  5 },.  { 227, 4 
31b8b 7d 2c 0a 20 20 7b 20 32 32 38 2c 20 32 20 7d 2c  },.  { 228, 2 },
31b8c 0a 20 20 7b 20 32 32 38 2c 20 30 20 7d 2c 0a 20  .  { 228, 0 },. 
31b8d 20 7b 20 32 32 36 2c 20 31 20 7d 2c 0a 20 20 7b   { 226, 1 },.  {
31b8e 20 32 32 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   226, 0 },.  { 2
31b8f 32 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 31  21, 1 },.  { 221
31b90 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 31 36 2c 20  , 0 },.  { 216, 
31b91 33 20 7d 2c 0a 20 20 7b 20 32 31 36 2c 20 31 20  3 },.  { 216, 1 
31b92 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31 31 20 7d  },.  { 147, 11 }
31b93 2c 0a 20 20 7b 20 32 32 39 2c 20 31 20 7d 2c 0a  ,.  { 229, 1 },.
31b94 20 20 7b 20 32 32 39 2c 20 30 20 7d 2c 0a 20 20    { 229, 0 },.  
31b95 7b 20 31 37 39 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 179, 0 },.  { 
31b96 31 37 39 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38  179, 3 },.  { 18
31b97 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 38 37 2c  7, 5 },.  { 187,
31b98 20 33 20 7d 2c 0a 20 20 7b 20 32 33 30 2c 20 30   3 },.  { 230, 0
31b99 20 7d 2c 0a 20 20 7b 20 32 33 30 2c 20 32 20 7d   },.  { 230, 2 }
31b9a 2c 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d 2c 0a  ,.  { 147, 4 },.
31b9b 20 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0a 20 20    { 147, 1 },.  
31b9c 7b 20 31 34 37 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 147, 2 },.  { 
31b9d 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 34  147, 3 },.  { 14
31b9e 37 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34 37 2c  7, 5 },.  { 147,
31b9f 20 36 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 35   6 },.  { 147, 5
31ba0 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d   },.  { 147, 6 }
31ba1 2c 0a 20 20 7b 20 32 33 31 2c 20 31 20 7d 2c 0a  ,.  { 231, 1 },.
31ba2 20 20 7b 20 32 33 31 2c 20 31 20 7d 2c 0a 20 20    { 231, 1 },.  
31ba3 7b 20 32 33 31 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 231, 1 },.  { 
31ba4 32 33 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33  231, 1 },.  { 23
31ba5 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 30 2c  1, 1 },.  { 170,
31ba6 20 32 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 32   2 },.  { 171, 2
31ba7 20 7d 2c 0a 20 20 7b 20 32 33 33 2c 20 31 20 7d   },.  { 233, 1 }
31ba8 2c 0a 20 20 7b 20 32 33 32 2c 20 31 20 7d 2c 0a  ,.  { 232, 1 },.
31ba9 20 20 7b 20 32 33 32 2c 20 30 20 7d 2c 0a 20 20    { 232, 0 },.  
31baa 7b 20 31 34 37 2c 20 35 20 7d 2c 0a 20 20 7b 20  { 147, 5 },.  { 
31bab 32 33 34 2c 20 31 31 20 7d 2c 0a 20 20 7b 20 32  234, 11 },.  { 2
31bac 33 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 36  36, 1 },.  { 236
31bad 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 36 2c 20  , 1 },.  { 236, 
31bae 32 20 7d 2c 0a 20 20 7b 20 32 33 36 2c 20 30 20  2 },.  { 236, 0 
31baf 7d 2c 0a 20 20 7b 20 32 33 37 2c 20 31 20 7d 2c  },.  { 237, 1 },
31bb0 0a 20 20 7b 20 32 33 37 2c 20 31 20 7d 2c 0a 20  .  { 237, 1 },. 
31bb1 20 7b 20 32 33 37 2c 20 33 20 7d 2c 0a 20 20 7b   { 237, 3 },.  {
31bb2 20 32 33 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   238, 0 },.  { 2
31bb3 33 38 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 33 39  38, 3 },.  { 239
31bb4 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33 39 2c 20  , 0 },.  { 239, 
31bb5 32 20 7d 2c 0a 20 20 7b 20 32 33 35 2c 20 33 20  2 },.  { 235, 3 
31bb6 7d 2c 0a 20 20 7b 20 32 33 35 2c 20 32 20 7d 2c  },.  { 235, 2 },
31bb7 0a 20 20 7b 20 32 34 31 2c 20 31 20 7d 2c 0a 20  .  { 241, 1 },. 
31bb8 20 7b 20 32 34 31 2c 20 33 20 7d 2c 0a 20 20 7b   { 241, 3 },.  {
31bb9 20 32 34 32 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   242, 0 },.  { 2
31bba 34 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 34 32  42, 3 },.  { 242
31bbb 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 34 30 2c 20  , 2 },.  { 240, 
31bbc 37 20 7d 2c 0a 20 20 7b 20 32 34 30 2c 20 38 20  7 },.  { 240, 8 
31bbd 7d 2c 0a 20 20 7b 20 32 34 30 2c 20 35 20 7d 2c  },.  { 240, 5 },
31bbe 0a 20 20 7b 20 32 34 30 2c 20 35 20 7d 2c 0a 20  .  { 240, 5 },. 
31bbf 20 7b 20 32 34 30 2c 20 31 20 7d 2c 0a 20 20 7b   { 240, 1 },.  {
31bc0 20 31 37 35 2c 20 34 20 7d 2c 0a 20 20 7b 20 31   175, 4 },.  { 1
31bc1 37 35 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 39 31  75, 6 },.  { 191
31bc2 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 31 2c 20  , 1 },.  { 191, 
31bc3 31 20 7d 2c 0a 20 20 7b 20 31 39 31 2c 20 31 20  1 },.  { 191, 1 
31bc4 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d 2c  },.  { 147, 4 },
31bc5 0a 20 20 7b 20 31 34 37 2c 20 36 20 7d 2c 0a 20  .  { 147, 6 },. 
31bc6 20 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b   { 147, 3 },.  {
31bc7 20 32 34 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 32   244, 0 },.  { 2
31bc8 34 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 34 33  44, 2 },.  { 243
31bc9 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 34 33 2c 20  , 1 },.  { 243, 
31bca 30 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31 20  0 },.  { 147, 1 
31bcb 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 33 20 7d 2c  },.  { 147, 3 },
31bcc 0a 20 20 7b 20 31 34 37 2c 20 31 20 7d 2c 0a 20  .  { 147, 1 },. 
31bcd 20 7b 20 31 34 37 2c 20 33 20 7d 2c 0a 20 20 7b   { 147, 3 },.  {
31bce 20 31 34 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 31   147, 6 },.  { 1
31bcf 34 37 2c 20 36 20 7d 2c 0a 20 20 7b 20 32 34 35  47, 6 },.  { 245
31bd0 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 34 36 2c 20  , 1 },.  { 246, 
31bd1 30 20 7d 2c 0a 20 20 7b 20 32 34 36 2c 20 31 20  0 },.  { 246, 1 
31bd2 7d 2c 0a 20 20 7b 20 31 34 37 2c 20 31 20 7d 2c  },.  { 147, 1 },
31bd3 0a 20 20 7b 20 31 34 37 2c 20 34 20 7d 2c 0a 20  .  { 147, 4 },. 
31bd4 20 7b 20 32 34 37 2c 20 37 20 7d 2c 0a 20 20 7b   { 247, 7 },.  {
31bd5 20 32 34 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 32   248, 1 },.  { 2
31bd6 34 38 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 34 39  48, 3 },.  { 249
31bd7 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 34 39 2c 20  , 0 },.  { 249, 
31bd8 32 20 7d 2c 0a 20 20 7b 20 32 35 30 2c 20 31 20  2 },.  { 250, 1 
31bd9 7d 2c 0a 20 20 7b 20 32 35 30 2c 20 33 20 7d 2c  },.  { 250, 3 },
31bda 0a 20 20 7b 20 32 35 31 2c 20 31 20 7d 2c 0a 20  .  { 251, 1 },. 
31bdb 20 7b 20 32 35 32 2c 20 30 20 7d 2c 0a 20 20 7b   { 252, 0 },.  {
31bdc 20 32 35 32 2c 20 34 20 7d 2c 0a 20 20 7b 20 32   252, 4 },.  { 2
31bdd 35 32 2c 20 32 20 7d 2c 0a 7d 3b 0a 0a 73 74 61  52, 2 },.};..sta
31bde 74 69 63 20 76 6f 69 64 20 79 79 5f 61 63 63 65  tic void yy_acce
31bdf 70 74 28 79 79 50 61 72 73 65 72 2a 29 3b 20 20  pt(yyParser*);  
31be0 2f 2a 20 46 6f 72 77 61 72 64 20 44 65 63 6c 61  /* Forward Decla
31be1 72 61 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  ration */../*.**
31be2 20 50 65 72 66 6f 72 6d 20 61 20 72 65 64 75 63   Perform a reduc
31be3 65 20 61 63 74 69 6f 6e 20 61 6e 64 20 74 68 65  e action and the
31be4 20 73 68 69 66 74 20 74 68 61 74 20 6d 75 73 74   shift that must
31be5 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a 20   immediately.** 
31be6 66 6f 6c 6c 6f 77 20 74 68 65 20 72 65 64 75 63  follow the reduc
31be7 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
31be8 64 20 79 79 5f 72 65 64 75 63 65 28 0a 20 20 79  d yy_reduce(.  y
31be9 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73  yParser *yypPars
31bea 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  er,         /* T
31beb 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 69  he parser */.  i
31bec 6e 74 20 79 79 72 75 6c 65 6e 6f 20 20 20 20 20  nt yyruleno     
31bed 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
31bee 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72 75 6c  umber of the rul
31bef 65 20 62 79 20 77 68 69 63 68 20 74 6f 20 72 65  e by which to re
31bf0 64 75 63 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  duce */.){.  int
31bf1 20 79 79 67 6f 74 6f 3b 20 20 20 20 20 20 20 20   yygoto;        
31bf2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31bf3 54 68 65 20 6e 65 78 74 20 73 74 61 74 65 20 2a  The next state *
31bf4 2f 0a 20 20 69 6e 74 20 79 79 61 63 74 3b 20 20  /.  int yyact;  
31bf5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31bf6 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 78 74 20      /* The next 
31bf7 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 59 59 4d 49  action */.  YYMI
31bf8 4e 4f 52 54 59 50 45 20 79 79 67 6f 74 6f 6d 69  NORTYPE yygotomi
31bf9 6e 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  nor;        /* T
31bfa 68 65 20 4c 48 53 20 6f 66 20 74 68 65 20 72 75  he LHS of the ru
31bfb 6c 65 20 72 65 64 75 63 65 64 20 2a 2f 0a 20 20  le reduced */.  
31bfc 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 79 79  yyStackEntry *yy
31bfd 6d 73 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  msp;            
31bfe 2f 2a 20 54 68 65 20 74 6f 70 20 6f 66 20 74 68  /* The top of th
31bff 65 20 70 61 72 73 65 72 27 73 20 73 74 61 63 6b  e parser's stack
31c00 20 2a 2f 0a 20 20 69 6e 74 20 79 79 73 69 7a 65   */.  int yysize
31c01 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
31c02 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75 6e 74 20        /* Amount 
31c03 74 6f 20 70 6f 70 20 74 68 65 20 73 74 61 63 6b  to pop the stack
31c04 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72   */.  sqlite3Par
31c05 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 20 20  serARG_FETCH;.  
31c06 79 79 6d 73 70 20 3d 20 26 79 79 70 50 61 72 73  yymsp = &yypPars
31c07 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70 50  er->yystack[yypP
31c08 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 3b 0a 23  arser->yyidx];.#
31c09 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20  ifndef NDEBUG.  
31c0a 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20  if( yyTraceFILE 
31c0b 26 26 20 79 79 72 75 6c 65 6e 6f 3e 3d 30 20 0a  && yyruleno>=0 .
31c0c 20 20 20 20 20 20 20 20 26 26 20 79 79 72 75 6c          && yyrul
31c0d 65 6e 6f 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66  eno<(int)(sizeof
31c0e 28 79 79 52 75 6c 65 4e 61 6d 65 29 2f 73 69 7a  (yyRuleName)/siz
31c0f 65 6f 66 28 79 79 52 75 6c 65 4e 61 6d 65 5b 30  eof(yyRuleName[0
31c10 5d 29 29 20 29 7b 0a 20 20 20 20 66 70 72 69 6e  ])) ){.    fprin
31c11 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 20  tf(yyTraceFILE, 
31c12 22 25 73 52 65 64 75 63 65 20 5b 25 73 5d 2e 5c  "%sReduce [%s].\
31c13 6e 22 2c 20 79 79 54 72 61 63 65 50 72 6f 6d 70  n", yyTracePromp
31c14 74 2c 0a 20 20 20 20 20 20 79 79 52 75 6c 65 4e  t,.      yyRuleN
31c15 61 6d 65 5b 79 79 72 75 6c 65 6e 6f 5d 29 3b 0a  ame[yyruleno]);.
31c16 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 4e 44    }.#endif /* ND
31c17 45 42 55 47 20 2a 2f 0a 0a 20 20 2f 2a 20 53 69  EBUG */..  /* Si
31c18 6c 65 6e 63 65 20 63 6f 6d 70 6c 61 69 6e 74 73  lence complaints
31c19 20 66 72 6f 6d 20 70 75 72 69 66 79 20 61 62 6f   from purify abo
31c1a 75 74 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 20 62  ut yygotominor b
31c1b 65 69 6e 67 20 75 6e 69 6e 69 74 69 61 6c 69 7a  eing uninitializ
31c1c 65 64 0a 20 20 2a 2a 20 69 6e 20 73 6f 6d 65 20  ed.  ** in some 
31c1d 63 61 73 65 73 20 77 68 65 6e 20 69 74 20 69 73  cases when it is
31c1e 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65   copied into the
31c1f 20 73 74 61 63 6b 20 61 66 74 65 72 20 74 68 65   stack after the
31c20 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 2a 2a 20   following.  ** 
31c21 73 77 69 74 63 68 2e 20 20 79 79 67 6f 74 6f 6d  switch.  yygotom
31c22 69 6e 6f 72 20 69 73 20 75 6e 69 6e 69 74 69 61  inor is uninitia
31c23 6c 69 7a 65 64 20 77 68 65 6e 20 61 20 72 75 6c  lized when a rul
31c24 65 20 72 65 64 75 63 65 73 20 74 68 61 74 20 64  e reduces that d
31c25 6f 65 73 0a 20 20 2a 2a 20 6e 6f 74 20 73 65 74  oes.  ** not set
31c26 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 69 74   the value of it
31c27 73 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65  s left-hand side
31c28 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 2e 20 20 4c   nonterminal.  L
31c29 65 61 76 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20  eaving the.  ** 
31c2a 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6e 6f 6e  value of the non
31c2b 74 65 72 6d 69 6e 61 6c 20 75 6e 69 6e 69 74 69  terminal uniniti
31c2c 61 6c 69 7a 65 64 20 69 73 20 75 74 74 65 72 6c  alized is utterl
31c2d 79 20 68 61 72 6d 6c 65 73 73 20 61 73 20 6c 6f  y harmless as lo
31c2e 6e 67 0a 20 20 2a 2a 20 61 73 20 74 68 65 20 76  ng.  ** as the v
31c2f 61 6c 75 65 20 69 73 20 6e 65 76 65 72 20 75 73  alue is never us
31c30 65 64 2e 20 20 53 6f 20 72 65 61 6c 6c 79 20 74  ed.  So really t
31c31 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67 20 74 68  he only thing th
31c32 69 73 20 63 6f 64 65 0a 20 20 2a 2a 20 61 63 63  is code.  ** acc
31c33 6f 6d 70 6c 69 73 68 65 73 20 69 73 20 74 6f 20  omplishes is to 
31c34 71 75 69 65 74 65 6e 20 70 75 72 69 66 79 2e 20  quieten purify. 
31c35 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 32 30 30 37   .  **.  ** 2007
31c36 2d 30 31 2d 31 36 3a 20 20 54 68 65 20 77 69 72  -01-16:  The wir
31c37 65 73 68 61 72 6b 20 70 72 6f 6a 65 63 74 20 28  eshark project (
31c38 77 77 77 2e 77 69 72 65 73 68 61 72 6b 2e 6f 72  www.wireshark.or
31c39 67 29 20 72 65 70 6f 72 74 73 20 74 68 61 74 0a  g) reports that.
31c3a 20 20 2a 2a 20 77 69 74 68 6f 75 74 20 74 68 69    ** without thi
31c3b 73 20 63 6f 64 65 2c 20 74 68 65 69 72 20 70 61  s code, their pa
31c3c 72 73 65 72 20 73 65 67 66 61 75 6c 74 73 2e 20  rser segfaults. 
31c3d 20 49 27 6d 20 6e 6f 74 20 73 75 72 65 20 77 68   I'm not sure wh
31c3e 61 74 20 74 68 65 72 65 0a 20 20 2a 2a 20 70 61  at there.  ** pa
31c3f 72 73 65 72 20 69 73 20 64 6f 69 6e 67 20 74 6f  rser is doing to
31c40 20 6d 61 6b 65 20 74 68 69 73 20 68 61 70 70 65   make this happe
31c41 6e 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20  n.  This is the 
31c42 73 65 63 6f 6e 64 20 62 75 67 20 72 65 70 6f 72  second bug repor
31c43 74 0a 20 20 2a 2a 20 66 72 6f 6d 20 77 69 72 65  t.  ** from wire
31c44 73 68 61 72 6b 20 74 68 69 73 20 77 65 65 6b 2e  shark this week.
31c45 20 20 43 6c 65 61 72 6c 79 20 74 68 65 79 20 61    Clearly they a
31c46 72 65 20 73 74 72 65 73 73 69 6e 67 20 4c 65 6d  re stressing Lem
31c47 6f 6e 20 69 6e 20 77 61 79 73 0a 20 20 2a 2a 20  on in ways.  ** 
31c48 74 68 61 74 20 69 74 20 68 61 73 20 6e 6f 74 20  that it has not 
31c49 62 65 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 20  been previously 
31c4a 73 74 72 65 73 73 65 64 2e 2e 2e 20 20 28 53 51  stressed...  (SQ
31c4b 4c 69 74 65 20 74 69 63 6b 65 74 20 23 32 31 37  Lite ticket #217
31c4c 32 29 0a 20 20 2a 2f 0a 20 20 2f 2a 6d 65 6d 73  2).  */.  /*mems
31c4d 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2c  et(&yygotominor,
31c4e 20 30 2c 20 73 69 7a 65 6f 66 28 79 79 67 6f 74   0, sizeof(yygot
31c4f 6f 6d 69 6e 6f 72 29 29 3b 2a 2f 0a 20 20 79 79  ominor));*/.  yy
31c50 67 6f 74 6f 6d 69 6e 6f 72 20 3d 20 79 79 7a 65  gotominor = yyze
31c51 72 6f 6d 69 6e 6f 72 3b 0a 0a 0a 20 20 73 77 69  rominor;...  swi
31c52 74 63 68 28 20 79 79 72 75 6c 65 6e 6f 20 29 7b  tch( yyruleno ){
31c53 0a 20 20 2f 2a 20 42 65 67 69 6e 6e 69 6e 67 20  .  /* Beginning 
31c54 68 65 72 65 20 61 72 65 20 74 68 65 20 72 65 64  here are the red
31c55 75 63 74 69 6f 6e 20 63 61 73 65 73 2e 20 20 41  uction cases.  A
31c56 20 74 79 70 69 63 61 6c 20 65 78 61 6d 70 6c 65   typical example
31c57 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 73 3a 0a 20  .  ** follows:. 
31c58 20 2a 2a 20 20 20 63 61 73 65 20 30 3a 0a 20 20   **   case 0:.  
31c59 2a 2a 20 20 23 6c 69 6e 65 20 3c 6c 69 6e 65 6e  **  #line <linen
31c5a 6f 3e 20 3c 67 72 61 6d 6d 61 72 66 69 6c 65 3e  o> <grammarfile>
31c5b 0a 20 20 2a 2a 20 20 20 20 20 7b 20 2e 2e 2e 20  .  **     { ... 
31c5c 7d 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20 55  }           // U
31c5d 73 65 72 20 73 75 70 70 6c 69 65 64 20 63 6f 64  ser supplied cod
31c5e 65 0a 20 20 2a 2a 20 20 23 6c 69 6e 65 20 3c 6c  e.  **  #line <l
31c5f 69 6e 65 6e 6f 3e 20 3c 74 68 69 73 66 69 6c 65  ineno> <thisfile
31c60 3e 0a 20 20 2a 2a 20 20 20 20 20 62 72 65 61 6b  >.  **     break
31c61 3b 0a 20 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ;.  */.      cas
31c62 65 20 35 3a 20 2f 2a 20 65 78 70 6c 61 69 6e 20  e 5: /* explain 
31c63 3a 3a 3d 20 2a 2f 0a 7b 20 73 71 6c 69 74 65 33  ::= */.{ sqlite3
31c64 42 65 67 69 6e 50 61 72 73 65 28 70 50 61 72 73  BeginParse(pPars
31c65 65 2c 20 30 29 3b 20 7d 0a 20 20 20 20 20 20 20  e, 0); }.       
31c66 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31c67 73 65 20 36 3a 20 2f 2a 20 65 78 70 6c 61 69 6e  se 6: /* explain
31c68 20 3a 3a 3d 20 45 58 50 4c 41 49 4e 20 2a 2f 0a   ::= EXPLAIN */.
31c69 7b 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61  { sqlite3BeginPa
31c6a 72 73 65 28 70 50 61 72 73 65 2c 20 31 29 3b 20  rse(pParse, 1); 
31c6b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31c6c 0a 20 20 20 20 20 20 63 61 73 65 20 37 3a 20 2f  .      case 7: /
31c6d 2a 20 65 78 70 6c 61 69 6e 20 3a 3a 3d 20 45 58  * explain ::= EX
31c6e 50 4c 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e  PLAIN QUERY PLAN
31c6f 20 2a 2f 0a 7b 20 73 71 6c 69 74 65 33 42 65 67   */.{ sqlite3Beg
31c70 69 6e 50 61 72 73 65 28 70 50 61 72 73 65 2c 20  inParse(pParse, 
31c71 32 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  2); }.        br
31c72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
31c73 38 3a 20 2f 2a 20 63 6d 64 78 20 3a 3a 3d 20 63  8: /* cmdx ::= c
31c74 6d 64 20 2a 2f 0a 7b 20 73 71 6c 69 74 65 33 46  md */.{ sqlite3F
31c75 69 6e 69 73 68 43 6f 64 69 6e 67 28 70 50 61 72  inishCoding(pPar
31c76 73 65 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  se); }.        b
31c77 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31c78 20 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 42   9: /* cmd ::= B
31c79 45 47 49 4e 20 74 72 61 6e 73 74 79 70 65 20 74  EGIN transtype t
31c7a 72 61 6e 73 5f 6f 70 74 20 2a 2f 0a 7b 73 71 6c  rans_opt */.{sql
31c7b 69 74 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63  ite3BeginTransac
31c7c 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 79 79 6d  tion(pParse, yym
31c7d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
31c7e 32 38 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  28);}.        br
31c7f 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
31c80 31 33 3a 20 2f 2a 20 74 72 61 6e 73 74 79 70 65  13: /* transtype
31c81 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d   ::= */.{yygotom
31c82 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 54 4b 5f  inor.yy328 = TK_
31c83 44 45 46 45 52 52 45 44 3b 7d 0a 20 20 20 20 20  DEFERRED;}.     
31c84 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31c85 63 61 73 65 20 31 34 3a 20 2f 2a 20 74 72 61 6e  case 14: /* tran
31c86 73 74 79 70 65 20 3a 3a 3d 20 44 45 46 45 52 52  stype ::= DEFERR
31c87 45 44 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  ED */.      case
31c88 20 31 35 3a 20 2f 2a 20 74 72 61 6e 73 74 79 70   15: /* transtyp
31c89 65 20 3a 3a 3d 20 49 4d 4d 45 44 49 41 54 45 20  e ::= IMMEDIATE 
31c8a 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31c8b 72 75 6c 65 6e 6f 3d 3d 31 35 29 3b 0a 20 20 20  ruleno==15);.   
31c8c 20 20 20 63 61 73 65 20 31 36 3a 20 2f 2a 20 74     case 16: /* t
31c8d 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 45 58 43  ranstype ::= EXC
31c8e 4c 55 53 49 56 45 20 2a 2f 20 79 79 74 65 73 74  LUSIVE */ yytest
31c8f 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31  case(yyruleno==1
31c90 36 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  6);.      case 1
31c91 31 34 3a 20 2f 2a 20 6d 75 6c 74 69 73 65 6c 65  14: /* multisele
31c92 63 74 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20  ct_op ::= UNION 
31c93 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31c94 72 75 6c 65 6e 6f 3d 3d 31 31 34 29 3b 0a 20 20  ruleno==114);.  
31c95 20 20 20 20 63 61 73 65 20 31 31 36 3a 20 2f 2a      case 116: /*
31c96 20 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20   multiselect_op 
31c97 3a 3a 3d 20 45 58 43 45 50 54 7c 49 4e 54 45 52  ::= EXCEPT|INTER
31c98 53 45 43 54 20 2a 2f 20 79 79 74 65 73 74 63 61  SECT */ yytestca
31c99 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 31 36  se(yyruleno==116
31c9a 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  );.{yygotominor.
31c9b 79 79 33 32 38 20 3d 20 79 79 6d 73 70 5b 30 5d  yy328 = yymsp[0]
31c9c 2e 6d 61 6a 6f 72 3b 7d 0a 20 20 20 20 20 20 20  .major;}.       
31c9d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31c9e 73 65 20 31 37 3a 20 2f 2a 20 63 6d 64 20 3a 3a  se 17: /* cmd ::
31c9f 3d 20 43 4f 4d 4d 49 54 20 74 72 61 6e 73 5f 6f  = COMMIT trans_o
31ca0 70 74 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  pt */.      case
31ca1 20 31 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20   18: /* cmd ::= 
31ca2 45 4e 44 20 74 72 61 6e 73 5f 6f 70 74 20 2a 2f  END trans_opt */
31ca3 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
31ca4 6c 65 6e 6f 3d 3d 31 38 29 3b 0a 7b 73 71 6c 69  leno==18);.{sqli
31ca5 74 65 33 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63  te3CommitTransac
31ca6 74 69 6f 6e 28 70 50 61 72 73 65 29 3b 7d 0a 20  tion(pParse);}. 
31ca7 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31ca8 20 20 20 20 63 61 73 65 20 31 39 3a 20 2f 2a 20      case 19: /* 
31ca9 63 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43 4b  cmd ::= ROLLBACK
31caa 20 74 72 61 6e 73 5f 6f 70 74 20 2a 2f 0a 7b 73   trans_opt */.{s
31cab 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72  qlite3RollbackTr
31cac 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 72 73 65  ansaction(pParse
31cad 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
31cae 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32  k;.      case 22
31caf 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 53 41 56  : /* cmd ::= SAV
31cb0 45 50 4f 49 4e 54 20 6e 6d 20 2a 2f 0a 7b 0a 20  EPOINT nm */.{. 
31cb1 20 73 71 6c 69 74 65 33 53 61 76 65 70 6f 69 6e   sqlite3Savepoin
31cb2 74 28 70 50 61 72 73 65 2c 20 53 41 56 45 50 4f  t(pParse, SAVEPO
31cb3 49 4e 54 5f 42 45 47 49 4e 2c 20 26 79 79 6d 73  INT_BEGIN, &yyms
31cb4 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
31cb5 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
31cb6 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 3a  ;.      case 23:
31cb7 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 45 4c 45   /* cmd ::= RELE
31cb8 41 53 45 20 73 61 76 65 70 6f 69 6e 74 5f 6f 70  ASE savepoint_op
31cb9 74 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  t nm */.{.  sqli
31cba 74 65 33 53 61 76 65 70 6f 69 6e 74 28 70 50 61  te3Savepoint(pPa
31cbb 72 73 65 2c 20 53 41 56 45 50 4f 49 4e 54 5f 52  rse, SAVEPOINT_R
31cbc 45 4c 45 41 53 45 2c 20 26 79 79 6d 73 70 5b 30  ELEASE, &yymsp[0
31cbd 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a  ].minor.yy0);.}.
31cbe 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31cbf 20 20 20 20 20 63 61 73 65 20 32 34 3a 20 2f 2a       case 24: /*
31cc0 20 63 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43   cmd ::= ROLLBAC
31cc1 4b 20 74 72 61 6e 73 5f 6f 70 74 20 54 4f 20 73  K trans_opt TO s
31cc2 61 76 65 70 6f 69 6e 74 5f 6f 70 74 20 6e 6d 20  avepoint_opt nm 
31cc3 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 53 61  */.{.  sqlite3Sa
31cc4 76 65 70 6f 69 6e 74 28 70 50 61 72 73 65 2c 20  vepoint(pParse, 
31cc5 53 41 56 45 50 4f 49 4e 54 5f 52 4f 4c 4c 42 41  SAVEPOINT_ROLLBA
31cc6 43 4b 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  CK, &yymsp[0].mi
31cc7 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20  nor.yy0);.}.    
31cc8 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
31cc9 20 63 61 73 65 20 32 36 3a 20 2f 2a 20 63 72 65   case 26: /* cre
31cca 61 74 65 5f 74 61 62 6c 65 20 3a 3a 3d 20 63 72  ate_table ::= cr
31ccb 65 61 74 65 6b 77 20 74 65 6d 70 20 54 41 42 4c  eatekw temp TABL
31ccc 45 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d  E ifnotexists nm
31ccd 20 64 62 6e 6d 20 2a 2f 0a 7b 0a 20 20 20 73 71   dbnm */.{.   sq
31cce 6c 69 74 65 33 53 74 61 72 74 54 61 62 6c 65 28  lite3StartTable(
31ccf 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31  pParse,&yymsp[-1
31cd0 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
31cd1 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  sp[0].minor.yy0,
31cd2 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
31cd3 79 79 33 32 38 2c 30 2c 30 2c 79 79 6d 73 70 5b  yy328,0,0,yymsp[
31cd4 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29  -2].minor.yy328)
31cd5 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
31cd6 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 37  k;.      case 27
31cd7 3a 20 2f 2a 20 63 72 65 61 74 65 6b 77 20 3a 3a  : /* createkw ::
31cd8 3d 20 43 52 45 41 54 45 20 2a 2f 0a 7b 0a 20 20  = CREATE */.{.  
31cd9 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6c 6f 6f 6b  pParse->db->look
31cda 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d  aside.bEnabled =
31cdb 20 30 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f   0;.  yygotomino
31cdc 72 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b 30 5d  r.yy0 = yymsp[0]
31cdd 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0a 7d 0a 20 20  .minor.yy0;.}.  
31cde 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31cdf 20 20 20 63 61 73 65 20 32 38 3a 20 2f 2a 20 69     case 28: /* i
31ce0 66 6e 6f 74 65 78 69 73 74 73 20 3a 3a 3d 20 2a  fnotexists ::= *
31ce1 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33 31 3a  /.      case 31:
31ce2 20 2f 2a 20 74 65 6d 70 20 3a 3a 3d 20 2a 2f 20   /* temp ::= */ 
31ce3 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
31ce4 65 6e 6f 3d 3d 33 31 29 3b 0a 20 20 20 20 20 20  eno==31);.      
31ce5 63 61 73 65 20 37 30 3a 20 2f 2a 20 61 75 74 6f  case 70: /* auto
31ce6 69 6e 63 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73  inc ::= */ yytes
31ce7 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
31ce8 37 30 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  70);.      case 
31ce9 38 32 3a 20 2f 2a 20 64 65 66 65 72 5f 73 75 62  82: /* defer_sub
31cea 63 6c 61 75 73 65 20 3a 3a 3d 20 4e 4f 54 20 44  clause ::= NOT D
31ceb 45 46 45 52 52 41 42 4c 45 20 69 6e 69 74 5f 64  EFERRABLE init_d
31cec 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74  eferred_pred_opt
31ced 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
31cee 79 72 75 6c 65 6e 6f 3d 3d 38 32 29 3b 0a 20 20  yruleno==82);.  
31cef 20 20 20 20 63 61 73 65 20 38 34 3a 20 2f 2a 20      case 84: /* 
31cf0 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72  init_deferred_pr
31cf1 65 64 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79  ed_opt ::= */ yy
31cf2 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
31cf3 6f 3d 3d 38 34 29 3b 0a 20 20 20 20 20 20 63 61  o==84);.      ca
31cf4 73 65 20 38 36 3a 20 2f 2a 20 69 6e 69 74 5f 64  se 86: /* init_d
31cf5 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74  eferred_pred_opt
31cf6 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59 20 49   ::= INITIALLY I
31cf7 4d 4d 45 44 49 41 54 45 20 2a 2f 20 79 79 74 65  MMEDIATE */ yyte
31cf8 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
31cf9 3d 38 36 29 3b 0a 20 20 20 20 20 20 63 61 73 65  =86);.      case
31cfa 20 39 37 3a 20 2f 2a 20 64 65 66 65 72 5f 73 75   97: /* defer_su
31cfb 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a 3a 3d 20  bclause_opt ::= 
31cfc 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31cfd 72 75 6c 65 6e 6f 3d 3d 39 37 29 3b 0a 20 20 20  ruleno==97);.   
31cfe 20 20 20 63 61 73 65 20 31 30 38 3a 20 2f 2a 20     case 108: /* 
31cff 69 66 65 78 69 73 74 73 20 3a 3a 3d 20 2a 2f 20  ifexists ::= */ 
31d00 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
31d01 65 6e 6f 3d 3d 31 30 38 29 3b 0a 20 20 20 20 20  eno==108);.     
31d02 20 63 61 73 65 20 31 31 39 3a 20 2f 2a 20 64 69   case 119: /* di
31d03 73 74 69 6e 63 74 20 3a 3a 3d 20 41 4c 4c 20 2a  stinct ::= ALL *
31d04 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
31d05 75 6c 65 6e 6f 3d 3d 31 31 39 29 3b 0a 20 20 20  uleno==119);.   
31d06 20 20 20 63 61 73 65 20 31 32 30 3a 20 2f 2a 20     case 120: /* 
31d07 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20 2a 2f 20  distinct ::= */ 
31d08 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
31d09 65 6e 6f 3d 3d 31 32 30 29 3b 0a 20 20 20 20 20  eno==120);.     
31d0a 20 63 61 73 65 20 32 32 32 3a 20 2f 2a 20 62 65   case 222: /* be
31d0b 74 77 65 65 6e 5f 6f 70 20 3a 3a 3d 20 42 45 54  tween_op ::= BET
31d0c 57 45 45 4e 20 2a 2f 20 79 79 74 65 73 74 63 61  WEEN */ yytestca
31d0d 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 32 32  se(yyruleno==222
31d0e 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32  );.      case 22
31d0f 35 3a 20 2f 2a 20 69 6e 5f 6f 70 20 3a 3a 3d 20  5: /* in_op ::= 
31d10 49 4e 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  IN */ yytestcase
31d11 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 32 35 29 3b  (yyruleno==225);
31d12 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
31d13 33 32 38 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20  328 = 0;}.      
31d14 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31d15 61 73 65 20 32 39 3a 20 2f 2a 20 69 66 6e 6f 74  ase 29: /* ifnot
31d16 65 78 69 73 74 73 20 3a 3a 3d 20 49 46 20 4e 4f  exists ::= IF NO
31d17 54 20 45 58 49 53 54 53 20 2a 2f 0a 20 20 20 20  T EXISTS */.    
31d18 20 20 63 61 73 65 20 33 30 3a 20 2f 2a 20 74 65    case 30: /* te
31d19 6d 70 20 3a 3a 3d 20 54 45 4d 50 20 2a 2f 20 79  mp ::= TEMP */ y
31d1a 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31d1b 6e 6f 3d 3d 33 30 29 3b 0a 20 20 20 20 20 20 63  no==30);.      c
31d1c 61 73 65 20 37 31 3a 20 2f 2a 20 61 75 74 6f 69  ase 71: /* autoi
31d1d 6e 63 20 3a 3a 3d 20 41 55 54 4f 49 4e 43 52 20  nc ::= AUTOINCR 
31d1e 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31d1f 72 75 6c 65 6e 6f 3d 3d 37 31 29 3b 0a 20 20 20  ruleno==71);.   
31d20 20 20 20 63 61 73 65 20 38 35 3a 20 2f 2a 20 69     case 85: /* i
31d21 6e 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65  nit_deferred_pre
31d22 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41  d_opt ::= INITIA
31d23 4c 4c 59 20 44 45 46 45 52 52 45 44 20 2a 2f 20  LLY DEFERRED */ 
31d24 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
31d25 65 6e 6f 3d 3d 38 35 29 3b 0a 20 20 20 20 20 20  eno==85);.      
31d26 63 61 73 65 20 31 30 37 3a 20 2f 2a 20 69 66 65  case 107: /* ife
31d27 78 69 73 74 73 20 3a 3a 3d 20 49 46 20 45 58 49  xists ::= IF EXI
31d28 53 54 53 20 2a 2f 20 79 79 74 65 73 74 63 61 73  STS */ yytestcas
31d29 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 30 37 29  e(yyruleno==107)
31d2a 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 38  ;.      case 118
31d2b 3a 20 2f 2a 20 64 69 73 74 69 6e 63 74 20 3a 3a  : /* distinct ::
31d2c 3d 20 44 49 53 54 49 4e 43 54 20 2a 2f 20 79 79  = DISTINCT */ yy
31d2d 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
31d2e 6f 3d 3d 31 31 38 29 3b 0a 20 20 20 20 20 20 63  o==118);.      c
31d2f 61 73 65 20 32 32 33 3a 20 2f 2a 20 62 65 74 77  ase 223: /* betw
31d30 65 65 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20 42  een_op ::= NOT B
31d31 45 54 57 45 45 4e 20 2a 2f 20 79 79 74 65 73 74  ETWEEN */ yytest
31d32 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
31d33 32 33 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  23);.      case 
31d34 32 32 36 3a 20 2f 2a 20 69 6e 5f 6f 70 20 3a 3a  226: /* in_op ::
31d35 3d 20 4e 4f 54 20 49 4e 20 2a 2f 20 79 79 74 65  = NOT IN */ yyte
31d36 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
31d37 3d 32 32 36 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69  =226);.{yygotomi
31d38 6e 6f 72 2e 79 79 33 32 38 20 3d 20 31 3b 7d 0a  nor.yy328 = 1;}.
31d39 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31d3a 20 20 20 20 20 63 61 73 65 20 33 32 3a 20 2f 2a       case 32: /*
31d3b 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72   create_table_ar
31d3c 67 73 20 3a 3a 3d 20 4c 50 20 63 6f 6c 75 6d 6e  gs ::= LP column
31d3d 6c 69 73 74 20 63 6f 6e 73 6c 69 73 74 5f 6f 70  list conslist_op
31d3e 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69  t RP */.{.  sqli
31d3f 74 65 33 45 6e 64 54 61 62 6c 65 28 70 50 61 72  te3EndTable(pPar
31d40 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  se,&yymsp[-1].mi
31d41 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30  nor.yy0,&yymsp[0
31d42 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 3b 0a  ].minor.yy0,0);.
31d43 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31d44 0a 20 20 20 20 20 20 63 61 73 65 20 33 33 3a 20  .      case 33: 
31d45 2f 2a 20 63 72 65 61 74 65 5f 74 61 62 6c 65 5f  /* create_table_
31d46 61 72 67 73 20 3a 3a 3d 20 41 53 20 73 65 6c 65  args ::= AS sele
31d47 63 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65  ct */.{.  sqlite
31d48 33 45 6e 64 54 61 62 6c 65 28 70 50 61 72 73 65  3EndTable(pParse
31d49 2c 30 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69  ,0,0,yymsp[0].mi
31d4a 6e 6f 72 2e 79 79 33 29 3b 0a 20 20 73 71 6c 69  nor.yy3);.  sqli
31d4b 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
31d4c 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73  pParse->db, yyms
31d4d 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29 3b  p[0].minor.yy3);
31d4e 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
31d4f 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 36 3a  ;.      case 36:
31d50 20 2f 2a 20 63 6f 6c 75 6d 6e 20 3a 3a 3d 20 63   /* column ::= c
31d51 6f 6c 75 6d 6e 69 64 20 74 79 70 65 20 63 61 72  olumnid type car
31d52 67 6c 69 73 74 20 2a 2f 0a 7b 0a 20 20 79 79 67  glist */.{.  yyg
31d53 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d  otominor.yy0.z =
31d54 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
31d55 2e 79 79 30 2e 7a 3b 0a 20 20 79 79 67 6f 74 6f  .yy0.z;.  yygoto
31d56 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 28 69  minor.yy0.n = (i
31d57 6e 74 29 28 70 50 61 72 73 65 2d 3e 73 4c 61 73  nt)(pParse->sLas
31d58 74 54 6f 6b 65 6e 2e 7a 2d 79 79 6d 73 70 5b 2d  tToken.z-yymsp[-
31d59 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29 20  2].minor.yy0.z) 
31d5a 2b 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54  + pParse->sLastT
31d5b 6f 6b 65 6e 2e 6e 3b 0a 7d 0a 20 20 20 20 20 20  oken.n;.}.      
31d5c 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31d5d 61 73 65 20 33 37 3a 20 2f 2a 20 63 6f 6c 75 6d  ase 37: /* colum
31d5e 6e 69 64 20 3a 3a 3d 20 6e 6d 20 2a 2f 0a 7b 0a  nid ::= nm */.{.
31d5f 20 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75    sqlite3AddColu
31d60 6d 6e 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70  mn(pParse,&yymsp
31d61 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
31d62 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
31d63 30 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0 = yymsp[0].min
31d64 6f 72 2e 79 79 30 3b 0a 7d 0a 20 20 20 20 20 20  or.yy0;.}.      
31d65 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31d66 61 73 65 20 33 38 3a 20 2f 2a 20 69 64 20 3a 3a  ase 38: /* id ::
31d67 3d 20 49 44 20 2a 2f 0a 20 20 20 20 20 20 63 61  = ID */.      ca
31d68 73 65 20 33 39 3a 20 2f 2a 20 69 64 20 3a 3a 3d  se 39: /* id ::=
31d69 20 49 4e 44 45 58 45 44 20 2a 2f 20 79 79 74 65   INDEXED */ yyte
31d6a 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
31d6b 3d 33 39 29 3b 0a 20 20 20 20 20 20 63 61 73 65  =39);.      case
31d6c 20 34 30 3a 20 2f 2a 20 69 64 73 20 3a 3a 3d 20   40: /* ids ::= 
31d6d 49 44 7c 53 54 52 49 4e 47 20 2a 2f 20 79 79 74  ID|STRING */ yyt
31d6e 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
31d6f 3d 3d 34 30 29 3b 0a 20 20 20 20 20 20 63 61 73  ==40);.      cas
31d70 65 20 34 31 3a 20 2f 2a 20 6e 6d 20 3a 3a 3d 20  e 41: /* nm ::= 
31d71 69 64 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  id */ yytestcase
31d72 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 31 29 3b 0a  (yyruleno==41);.
31d73 20 20 20 20 20 20 63 61 73 65 20 34 32 3a 20 2f        case 42: /
31d74 2a 20 6e 6d 20 3a 3a 3d 20 53 54 52 49 4e 47 20  * nm ::= STRING 
31d75 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31d76 72 75 6c 65 6e 6f 3d 3d 34 32 29 3b 0a 20 20 20  ruleno==42);.   
31d77 20 20 20 63 61 73 65 20 34 33 3a 20 2f 2a 20 6e     case 43: /* n
31d78 6d 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 2a 2f  m ::= JOIN_KW */
31d79 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
31d7a 6c 65 6e 6f 3d 3d 34 33 29 3b 0a 20 20 20 20 20  leno==43);.     
31d7b 20 63 61 73 65 20 34 36 3a 20 2f 2a 20 74 79 70   case 46: /* typ
31d7c 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e  etoken ::= typen
31d7d 61 6d 65 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ame */ yytestcas
31d7e 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 36 29 3b  e(yyruleno==46);
31d7f 0a 20 20 20 20 20 20 63 61 73 65 20 34 39 3a 20  .      case 49: 
31d80 2f 2a 20 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20  /* typename ::= 
31d81 69 64 73 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ids */ yytestcas
31d82 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 34 39 29 3b  e(yyruleno==49);
31d83 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 36 3a  .      case 126:
31d84 20 2f 2a 20 61 73 20 3a 3a 3d 20 41 53 20 6e 6d   /* as ::= AS nm
31d85 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
31d86 79 72 75 6c 65 6e 6f 3d 3d 31 32 36 29 3b 0a 20  yruleno==126);. 
31d87 20 20 20 20 20 63 61 73 65 20 31 32 37 3a 20 2f       case 127: /
31d88 2a 20 61 73 20 3a 3a 3d 20 69 64 73 20 2a 2f 20  * as ::= ids */ 
31d89 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
31d8a 65 6e 6f 3d 3d 31 32 37 29 3b 0a 20 20 20 20 20  eno==127);.     
31d8b 20 63 61 73 65 20 31 33 37 3a 20 2f 2a 20 64 62   case 137: /* db
31d8c 6e 6d 20 3a 3a 3d 20 44 4f 54 20 6e 6d 20 2a 2f  nm ::= DOT nm */
31d8d 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
31d8e 6c 65 6e 6f 3d 3d 31 33 37 29 3b 0a 20 20 20 20  leno==137);.    
31d8f 20 20 63 61 73 65 20 31 34 36 3a 20 2f 2a 20 69    case 146: /* i
31d90 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 49  ndexed_opt ::= I
31d91 4e 44 45 58 45 44 20 42 59 20 6e 6d 20 2a 2f 20  NDEXED BY nm */ 
31d92 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
31d93 65 6e 6f 3d 3d 31 34 36 29 3b 0a 20 20 20 20 20  eno==146);.     
31d94 20 63 61 73 65 20 32 35 31 3a 20 2f 2a 20 63 6f   case 251: /* co
31d95 6c 6c 61 74 65 20 3a 3a 3d 20 43 4f 4c 4c 41 54  llate ::= COLLAT
31d96 45 20 69 64 73 20 2a 2f 20 79 79 74 65 73 74 63  E ids */ yytestc
31d97 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 35  ase(yyruleno==25
31d98 31 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  1);.      case 2
31d99 36 30 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a 3d  60: /* nmnum ::=
31d9a 20 70 6c 75 73 5f 6e 75 6d 20 2a 2f 20 79 79 74   plus_num */ yyt
31d9b 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
31d9c 3d 3d 32 36 30 29 3b 0a 20 20 20 20 20 20 63 61  ==260);.      ca
31d9d 73 65 20 32 36 31 3a 20 2f 2a 20 6e 6d 6e 75 6d  se 261: /* nmnum
31d9e 20 3a 3a 3d 20 6e 6d 20 2a 2f 20 79 79 74 65 73   ::= nm */ yytes
31d9f 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
31da0 32 36 31 29 3b 0a 20 20 20 20 20 20 63 61 73 65  261);.      case
31da1 20 32 36 32 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a   262: /* nmnum :
31da2 3a 3d 20 4f 4e 20 2a 2f 20 79 79 74 65 73 74 63  := ON */ yytestc
31da3 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 36  ase(yyruleno==26
31da4 32 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  2);.      case 2
31da5 36 33 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a 3d  63: /* nmnum ::=
31da6 20 44 45 4c 45 54 45 20 2a 2f 20 79 79 74 65 73   DELETE */ yytes
31da7 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
31da8 32 36 33 29 3b 0a 20 20 20 20 20 20 63 61 73 65  263);.      case
31da9 20 32 36 34 3a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a   264: /* nmnum :
31daa 3a 3d 20 44 45 46 41 55 4c 54 20 2a 2f 20 79 79  := DEFAULT */ yy
31dab 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
31dac 6f 3d 3d 32 36 34 29 3b 0a 20 20 20 20 20 20 63  o==264);.      c
31dad 61 73 65 20 32 36 35 3a 20 2f 2a 20 70 6c 75 73  ase 265: /* plus
31dae 5f 6e 75 6d 20 3a 3a 3d 20 70 6c 75 73 5f 6f 70  _num ::= plus_op
31daf 74 20 6e 75 6d 62 65 72 20 2a 2f 20 79 79 74 65  t number */ yyte
31db0 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
31db1 3d 32 36 35 29 3b 0a 20 20 20 20 20 20 63 61 73  =265);.      cas
31db2 65 20 32 36 36 3a 20 2f 2a 20 6d 69 6e 75 73 5f  e 266: /* minus_
31db3 6e 75 6d 20 3a 3a 3d 20 4d 49 4e 55 53 20 6e 75  num ::= MINUS nu
31db4 6d 62 65 72 20 2a 2f 20 79 79 74 65 73 74 63 61  mber */ yytestca
31db5 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 36 36  se(yyruleno==266
31db6 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 36  );.      case 26
31db7 37 3a 20 2f 2a 20 6e 75 6d 62 65 72 20 3a 3a 3d  7: /* number ::=
31db8 20 49 4e 54 45 47 45 52 7c 46 4c 4f 41 54 20 2a   INTEGER|FLOAT *
31db9 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
31dba 75 6c 65 6e 6f 3d 3d 32 36 37 29 3b 0a 20 20 20  uleno==267);.   
31dbb 20 20 20 63 61 73 65 20 32 38 35 3a 20 2f 2a 20     case 285: /* 
31dbc 74 72 6e 6d 20 3a 3a 3d 20 6e 6d 20 2a 2f 20 79  trnm ::= nm */ y
31dbd 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31dbe 6e 6f 3d 3d 32 38 35 29 3b 0a 7b 79 79 67 6f 74  no==285);.{yygot
31dbf 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 79 79 6d  ominor.yy0 = yym
31dc0 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b  sp[0].minor.yy0;
31dc1 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31dc2 0a 20 20 20 20 20 20 63 61 73 65 20 34 35 3a 20  .      case 45: 
31dc3 2f 2a 20 74 79 70 65 20 3a 3a 3d 20 74 79 70 65  /* type ::= type
31dc4 74 6f 6b 65 6e 20 2a 2f 0a 7b 73 71 6c 69 74 65  token */.{sqlite
31dc5 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28 70  3AddColumnType(p
31dc6 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 30 5d 2e  Parse,&yymsp[0].
31dc7 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20  minor.yy0);}.   
31dc8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
31dc9 20 20 63 61 73 65 20 34 37 3a 20 2f 2a 20 74 79    case 47: /* ty
31dca 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65  petoken ::= type
31dcb 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65 64 20 52  name LP signed R
31dcc 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  P */.{.  yygotom
31dcd 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 79 79 6d  inor.yy0.z = yym
31dce 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
31dcf 2e 7a 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  .z;.  yygotomino
31dd0 72 2e 79 79 30 2e 6e 20 3d 20 28 69 6e 74 29 28  r.yy0.n = (int)(
31dd1 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
31dd2 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d  yy0.z[yymsp[0].m
31dd3 69 6e 6f 72 2e 79 79 30 2e 6e 5d 20 2d 20 79 79  inor.yy0.n] - yy
31dd4 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
31dd5 30 2e 7a 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  0.z);.}.        
31dd6 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
31dd7 65 20 34 38 3a 20 2f 2a 20 74 79 70 65 74 6f 6b  e 48: /* typetok
31dd8 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 20  en ::= typename 
31dd9 4c 50 20 73 69 67 6e 65 64 20 43 4f 4d 4d 41 20  LP signed COMMA 
31dda 73 69 67 6e 65 64 20 52 50 20 2a 2f 0a 7b 0a 20  signed RP */.{. 
31ddb 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30   yygotominor.yy0
31ddc 2e 7a 20 3d 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d  .z = yymsp[-5].m
31ddd 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 79 79  inor.yy0.z;.  yy
31dde 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20  gotominor.yy0.n 
31ddf 3d 20 28 69 6e 74 29 28 26 79 79 6d 73 70 5b 30  = (int)(&yymsp[0
31de0 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79  ].minor.yy0.z[yy
31de1 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
31de2 2e 6e 5d 20 2d 20 79 79 6d 73 70 5b 2d 35 5d 2e  .n] - yymsp[-5].
31de3 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29 3b 0a 7d 0a  minor.yy0.z);.}.
31de4 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31de5 20 20 20 20 20 63 61 73 65 20 35 30 3a 20 2f 2a       case 50: /*
31de6 20 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 74 79   typename ::= ty
31de7 70 65 6e 61 6d 65 20 69 64 73 20 2a 2f 0a 7b 79  pename ids */.{y
31de8 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a  ygotominor.yy0.z
31de9 3d 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  =yymsp[-1].minor
31dea 2e 79 79 30 2e 7a 3b 20 79 79 67 6f 74 6f 6d 69  .yy0.z; yygotomi
31deb 6e 6f 72 2e 79 79 30 2e 6e 3d 79 79 6d 73 70 5b  nor.yy0.n=yymsp[
31dec 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 2b 28  0].minor.yy0.n+(
31ded 69 6e 74 29 28 79 79 6d 73 70 5b 30 5d 2e 6d 69  int)(yymsp[0].mi
31dee 6e 6f 72 2e 79 79 30 2e 7a 2d 79 79 6d 73 70 5b  nor.yy0.z-yymsp[
31def 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 29  -1].minor.yy0.z)
31df0 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
31df1 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35 37 3a  ;.      case 57:
31df2 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45   /* ccons ::= DE
31df3 46 41 55 4c 54 20 74 65 72 6d 20 2a 2f 0a 20 20  FAULT term */.  
31df4 20 20 20 20 63 61 73 65 20 35 39 3a 20 2f 2a 20      case 59: /* 
31df5 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c  ccons ::= DEFAUL
31df6 54 20 50 4c 55 53 20 74 65 72 6d 20 2a 2f 20 79  T PLUS term */ y
31df7 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
31df8 6e 6f 3d 3d 35 39 29 3b 0a 7b 73 71 6c 69 74 65  no==59);.{sqlite
31df9 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65  3AddDefaultValue
31dfa 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 30  (pParse,&yymsp[0
31dfb 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 29 3b 7d  ].minor.yy346);}
31dfc 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31dfd 20 20 20 20 20 20 63 61 73 65 20 35 38 3a 20 2f        case 58: /
31dfe 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41  * ccons ::= DEFA
31dff 55 4c 54 20 4c 50 20 65 78 70 72 20 52 50 20 2a  ULT LP expr RP *
31e00 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 44 65 66  /.{sqlite3AddDef
31e01 61 75 6c 74 56 61 6c 75 65 28 70 50 61 72 73 65  aultValue(pParse
31e02 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ,&yymsp[-1].mino
31e03 72 2e 79 79 33 34 36 29 3b 7d 0a 20 20 20 20 20  r.yy346);}.     
31e04 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31e05 63 61 73 65 20 36 30 3a 20 2f 2a 20 63 63 6f 6e  case 60: /* ccon
31e06 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 4d 49  s ::= DEFAULT MI
31e07 4e 55 53 20 74 65 72 6d 20 2a 2f 0a 7b 0a 20 20  NUS term */.{.  
31e08 45 78 70 72 53 70 61 6e 20 76 3b 0a 20 20 76 2e  ExprSpan v;.  v.
31e09 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50  pExpr = sqlite3P
31e0a 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
31e0b 55 4d 49 4e 55 53 2c 20 79 79 6d 73 70 5b 30 5d  UMINUS, yymsp[0]
31e0c 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  .minor.yy346.pEx
31e0d 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 76 2e 7a  pr, 0, 0);.  v.z
31e0e 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 31  Start = yymsp[-1
31e0f 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20  ].minor.yy0.z;. 
31e10 20 76 2e 7a 45 6e 64 20 3d 20 79 79 6d 73 70 5b   v.zEnd = yymsp[
31e11 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a  0].minor.yy346.z
31e12 45 6e 64 3b 0a 20 20 73 71 6c 69 74 65 33 41 64  End;.  sqlite3Ad
31e13 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 70 50  dDefaultValue(pP
31e14 61 72 73 65 2c 26 76 29 3b 0a 7d 0a 20 20 20 20  arse,&v);.}.    
31e15 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
31e16 20 63 61 73 65 20 36 31 3a 20 2f 2a 20 63 63 6f   case 61: /* cco
31e17 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 69  ns ::= DEFAULT i
31e18 64 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 53 70 61  d */.{.  ExprSpa
31e19 6e 20 76 3b 0a 20 20 73 70 61 6e 45 78 70 72 28  n v;.  spanExpr(
31e1a 26 76 2c 20 70 50 61 72 73 65 2c 20 54 4b 5f 53  &v, pParse, TK_S
31e1b 54 52 49 4e 47 2c 20 26 79 79 6d 73 70 5b 30 5d  TRING, &yymsp[0]
31e1c 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73  .minor.yy0);.  s
31e1d 71 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74  qlite3AddDefault
31e1e 56 61 6c 75 65 28 70 50 61 72 73 65 2c 26 76 29  Value(pParse,&v)
31e1f 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
31e20 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 33  k;.      case 63
31e21 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 4e  : /* ccons ::= N
31e22 4f 54 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 20 2a  OT NULL onconf *
31e23 2f 0a 7b 73 71 6c 69 74 65 33 41 64 64 4e 6f 74  /.{sqlite3AddNot
31e24 4e 75 6c 6c 28 70 50 61 72 73 65 2c 20 79 79 6d  Null(pParse, yym
31e25 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  sp[0].minor.yy32
31e26 38 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  8);}.        bre
31e27 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36  ak;.      case 6
31e28 34 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20  4: /* ccons ::= 
31e29 50 52 49 4d 41 52 59 20 4b 45 59 20 73 6f 72 74  PRIMARY KEY sort
31e2a 6f 72 64 65 72 20 6f 6e 63 6f 6e 66 20 61 75 74  order onconf aut
31e2b 6f 69 6e 63 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  oinc */.{sqlite3
31e2c 41 64 64 50 72 69 6d 61 72 79 4b 65 79 28 70 50  AddPrimaryKey(pP
31e2d 61 72 73 65 2c 30 2c 79 79 6d 73 70 5b 2d 31 5d  arse,0,yymsp[-1]
31e2e 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 79 79 6d  .minor.yy328,yym
31e2f 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  sp[0].minor.yy32
31e30 38 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  8,yymsp[-2].mino
31e31 72 2e 79 79 33 32 38 29 3b 7d 0a 20 20 20 20 20  r.yy328);}.     
31e32 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31e33 63 61 73 65 20 36 35 3a 20 2f 2a 20 63 63 6f 6e  case 65: /* ccon
31e34 73 20 3a 3a 3d 20 55 4e 49 51 55 45 20 6f 6e 63  s ::= UNIQUE onc
31e35 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 43  onf */.{sqlite3C
31e36 72 65 61 74 65 49 6e 64 65 78 28 70 50 61 72 73  reateIndex(pPars
31e37 65 2c 30 2c 30 2c 30 2c 30 2c 79 79 6d 73 70 5b  e,0,0,0,0,yymsp[
31e38 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c 30  0].minor.yy328,0
31e39 2c 30 2c 30 2c 30 29 3b 7d 0a 20 20 20 20 20 20  ,0,0,0);}.      
31e3a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31e3b 61 73 65 20 36 36 3a 20 2f 2a 20 63 63 6f 6e 73  ase 66: /* ccons
31e3c 20 3a 3a 3d 20 43 48 45 43 4b 20 4c 50 20 65 78   ::= CHECK LP ex
31e3d 70 72 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65  pr RP */.{sqlite
31e3e 33 41 64 64 43 68 65 63 6b 43 6f 6e 73 74 72 61  3AddCheckConstra
31e3f 69 6e 74 28 70 50 61 72 73 65 2c 79 79 6d 73 70  int(pParse,yymsp
31e40 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  [-1].minor.yy346
31e41 2e 70 45 78 70 72 29 3b 7d 0a 20 20 20 20 20 20  .pExpr);}.      
31e42 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31e43 61 73 65 20 36 37 3a 20 2f 2a 20 63 63 6f 6e 73  ase 67: /* ccons
31e44 20 3a 3a 3d 20 52 45 46 45 52 45 4e 43 45 53 20   ::= REFERENCES 
31e45 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70 74 20 72  nm idxlist_opt r
31e46 65 66 61 72 67 73 20 2a 2f 0a 7b 73 71 6c 69 74  efargs */.{sqlit
31e47 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b  e3CreateForeignK
31e48 65 79 28 70 50 61 72 73 65 2c 30 2c 26 79 79 6d  ey(pParse,0,&yym
31e49 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
31e4a 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  ,yymsp[-1].minor
31e4b 2e 79 79 31 34 2c 79 79 6d 73 70 5b 30 5d 2e 6d  .yy14,yymsp[0].m
31e4c 69 6e 6f 72 2e 79 79 33 32 38 29 3b 7d 0a 20 20  inor.yy328);}.  
31e4d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31e4e 20 20 20 63 61 73 65 20 36 38 3a 20 2f 2a 20 63     case 68: /* c
31e4f 63 6f 6e 73 20 3a 3a 3d 20 64 65 66 65 72 5f 73  cons ::= defer_s
31e50 75 62 63 6c 61 75 73 65 20 2a 2f 0a 7b 73 71 6c  ubclause */.{sql
31e51 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e  ite3DeferForeign
31e52 4b 65 79 28 70 50 61 72 73 65 2c 79 79 6d 73 70  Key(pParse,yymsp
31e53 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29  [0].minor.yy328)
31e54 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
31e55 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36 39 3a  ;.      case 69:
31e56 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 43 4f   /* ccons ::= CO
31e57 4c 4c 41 54 45 20 69 64 73 20 2a 2f 0a 7b 73 71  LLATE ids */.{sq
31e58 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54  lite3AddCollateT
31e59 79 70 65 28 70 50 61 72 73 65 2c 20 26 79 79 6d  ype(pParse, &yym
31e5a 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
31e5b 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
31e5c 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37 32 3a  ;.      case 72:
31e5d 20 2f 2a 20 72 65 66 61 72 67 73 20 3a 3a 3d 20   /* refargs ::= 
31e5e 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.{ yygotominor
31e5f 2e 79 79 33 32 38 20 3d 20 4f 45 5f 4e 6f 6e 65  .yy328 = OE_None
31e60 2a 30 78 30 31 30 31 3b 20 2f 2a 20 45 56 3a 20  *0x0101; /* EV: 
31e61 52 2d 31 39 38 30 33 2d 34 35 38 38 34 20 2a 2f  R-19803-45884 */
31e62 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31e63 0a 20 20 20 20 20 20 63 61 73 65 20 37 33 3a 20  .      case 73: 
31e64 2f 2a 20 72 65 66 61 72 67 73 20 3a 3a 3d 20 72  /* refargs ::= r
31e65 65 66 61 72 67 73 20 72 65 66 61 72 67 20 2a 2f  efargs refarg */
31e66 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
31e67 79 33 32 38 20 3d 20 28 79 79 6d 73 70 5b 2d 31  y328 = (yymsp[-1
31e68 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 20 26 20  ].minor.yy328 & 
31e69 7e 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  ~yymsp[0].minor.
31e6a 79 79 34 32 39 2e 6d 61 73 6b 29 20 7c 20 79 79  yy429.mask) | yy
31e6b 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
31e6c 32 39 2e 76 61 6c 75 65 3b 20 7d 0a 20 20 20 20  29.value; }.    
31e6d 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
31e6e 20 63 61 73 65 20 37 34 3a 20 2f 2a 20 72 65 66   case 74: /* ref
31e6f 61 72 67 20 3a 3a 3d 20 4d 41 54 43 48 20 6e 6d  arg ::= MATCH nm
31e70 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
31e71 72 2e 79 79 34 32 39 2e 76 61 6c 75 65 20 3d 20  r.yy429.value = 
31e72 30 3b 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  0;     yygotomin
31e73 6f 72 2e 79 79 34 32 39 2e 6d 61 73 6b 20 3d 20  or.yy429.mask = 
31e74 30 78 30 30 30 30 30 30 3b 20 7d 0a 20 20 20 20  0x000000; }.    
31e75 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
31e76 20 63 61 73 65 20 37 35 3a 20 2f 2a 20 72 65 66   case 75: /* ref
31e77 61 72 67 20 3a 3a 3d 20 4f 4e 20 44 45 4c 45 54  arg ::= ON DELET
31e78 45 20 72 65 66 61 63 74 20 2a 2f 0a 7b 20 79 79  E refact */.{ yy
31e79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 32 39 2e  gotominor.yy429.
31e7a 76 61 6c 75 65 20 3d 20 79 79 6d 73 70 5b 30 5d  value = yymsp[0]
31e7b 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 20 20 20  .minor.yy328;   
31e7c 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
31e7d 34 32 39 2e 6d 61 73 6b 20 3d 20 30 78 30 30 30  429.mask = 0x000
31e7e 30 66 66 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  0ff; }.        b
31e7f 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31e80 20 37 36 3a 20 2f 2a 20 72 65 66 61 72 67 20 3a   76: /* refarg :
31e81 3a 3d 20 4f 4e 20 55 50 44 41 54 45 20 72 65 66  := ON UPDATE ref
31e82 61 63 74 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d  act */.{ yygotom
31e83 69 6e 6f 72 2e 79 79 34 32 39 2e 76 61 6c 75 65  inor.yy429.value
31e84 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
31e85 72 2e 79 79 33 32 38 3c 3c 38 3b 20 20 79 79 67  r.yy328<<8;  yyg
31e86 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 32 39 2e 6d  otominor.yy429.m
31e87 61 73 6b 20 3d 20 30 78 30 30 66 66 30 30 3b 20  ask = 0x00ff00; 
31e88 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31e89 0a 20 20 20 20 20 20 63 61 73 65 20 37 37 3a 20  .      case 77: 
31e8a 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20 53 45  /* refact ::= SE
31e8b 54 20 4e 55 4c 4c 20 2a 2f 0a 7b 20 79 79 67 6f  T NULL */.{ yygo
31e8c 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20  tominor.yy328 = 
31e8d 4f 45 5f 53 65 74 4e 75 6c 6c 3b 20 20 2f 2a 20  OE_SetNull;  /* 
31e8e 45 56 3a 20 52 2d 33 33 33 32 36 2d 34 35 32 35  EV: R-33326-4525
31e8f 32 20 2a 2f 7d 0a 20 20 20 20 20 20 20 20 62 72  2 */}.        br
31e90 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
31e91 37 38 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a  78: /* refact ::
31e92 3d 20 53 45 54 20 44 45 46 41 55 4c 54 20 2a 2f  = SET DEFAULT */
31e93 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .{ yygotominor.y
31e94 79 33 32 38 20 3d 20 4f 45 5f 53 65 74 44 66 6c  y328 = OE_SetDfl
31e95 74 3b 20 20 2f 2a 20 45 56 3a 20 52 2d 33 33 33  t;  /* EV: R-333
31e96 32 36 2d 34 35 32 35 32 20 2a 2f 7d 0a 20 20 20  26-45252 */}.   
31e97 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
31e98 20 20 63 61 73 65 20 37 39 3a 20 2f 2a 20 72 65    case 79: /* re
31e99 66 61 63 74 20 3a 3a 3d 20 43 41 53 43 41 44 45  fact ::= CASCADE
31e9a 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
31e9b 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f 43 61 73  r.yy328 = OE_Cas
31e9c 63 61 64 65 3b 20 20 2f 2a 20 45 56 3a 20 52 2d  cade;  /* EV: R-
31e9d 33 33 33 32 36 2d 34 35 32 35 32 20 2a 2f 7d 0a  33326-45252 */}.
31e9e 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31e9f 20 20 20 20 20 63 61 73 65 20 38 30 3a 20 2f 2a       case 80: /*
31ea0 20 72 65 66 61 63 74 20 3a 3a 3d 20 52 45 53 54   refact ::= REST
31ea1 52 49 43 54 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f  RICT */.{ yygoto
31ea2 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45  minor.yy328 = OE
31ea3 5f 52 65 73 74 72 69 63 74 3b 20 2f 2a 20 45 56  _Restrict; /* EV
31ea4 3a 20 52 2d 33 33 33 32 36 2d 34 35 32 35 32 20  : R-33326-45252 
31ea5 2a 2f 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  */}.        brea
31ea6 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38 31  k;.      case 81
31ea7 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a 3d 20  : /* refact ::= 
31ea8 4e 4f 20 41 43 54 49 4f 4e 20 2a 2f 0a 7b 20 79  NO ACTION */.{ y
31ea9 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38  ygotominor.yy328
31eaa 20 3d 20 4f 45 5f 4e 6f 6e 65 3b 20 20 20 20 20   = OE_None;     
31eab 2f 2a 20 45 56 3a 20 52 2d 33 33 33 32 36 2d 34  /* EV: R-33326-4
31eac 35 32 35 32 20 2a 2f 7d 0a 20 20 20 20 20 20 20  5252 */}.       
31ead 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31eae 73 65 20 38 33 3a 20 2f 2a 20 64 65 66 65 72 5f  se 83: /* defer_
31eaf 73 75 62 63 6c 61 75 73 65 20 3a 3a 3d 20 44 45  subclause ::= DE
31eb0 46 45 52 52 41 42 4c 45 20 69 6e 69 74 5f 64 65  FERRABLE init_de
31eb1 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20  ferred_pred_opt 
31eb2 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 39 38  */.      case 98
31eb3 3a 20 2f 2a 20 64 65 66 65 72 5f 73 75 62 63 6c  : /* defer_subcl
31eb4 61 75 73 65 5f 6f 70 74 20 3a 3a 3d 20 64 65 66  ause_opt ::= def
31eb5 65 72 5f 73 75 62 63 6c 61 75 73 65 20 2a 2f 20  er_subclause */ 
31eb6 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
31eb7 65 6e 6f 3d 3d 39 38 29 3b 0a 20 20 20 20 20 20  eno==98);.      
31eb8 63 61 73 65 20 31 30 30 3a 20 2f 2a 20 6f 6e 63  case 100: /* onc
31eb9 6f 6e 66 20 3a 3a 3d 20 4f 4e 20 43 4f 4e 46 4c  onf ::= ON CONFL
31eba 49 43 54 20 72 65 73 6f 6c 76 65 74 79 70 65 20  ICT resolvetype 
31ebb 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
31ebc 72 75 6c 65 6e 6f 3d 3d 31 30 30 29 3b 0a 20 20  ruleno==100);.  
31ebd 20 20 20 20 63 61 73 65 20 31 30 33 3a 20 2f 2a      case 103: /*
31ebe 20 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d   resolvetype ::=
31ebf 20 72 61 69 73 65 74 79 70 65 20 2a 2f 20 79 79   raisetype */ yy
31ec0 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
31ec1 6f 3d 3d 31 30 33 29 3b 0a 7b 79 79 67 6f 74 6f  o==103);.{yygoto
31ec2 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 79 79  minor.yy328 = yy
31ec3 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
31ec4 32 38 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  28;}.        bre
31ec5 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38  ak;.      case 8
31ec6 37 3a 20 2f 2a 20 63 6f 6e 73 6c 69 73 74 5f 6f  7: /* conslist_o
31ec7 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74  pt ::= */.{yygot
31ec8 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 30  ominor.yy0.n = 0
31ec9 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ; yygotominor.yy
31eca 30 2e 7a 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20  0.z = 0;}.      
31ecb 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31ecc 61 73 65 20 38 38 3a 20 2f 2a 20 63 6f 6e 73 6c  ase 88: /* consl
31ecd 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4d 4d  ist_opt ::= COMM
31ece 41 20 63 6f 6e 73 6c 69 73 74 20 2a 2f 0a 7b 79  A conslist */.{y
31ecf 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d  ygotominor.yy0 =
31ed0 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
31ed1 2e 79 79 30 3b 7d 0a 20 20 20 20 20 20 20 20 62  .yy0;}.        b
31ed2 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31ed3 20 39 33 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a   93: /* tcons ::
31ed4 3d 20 50 52 49 4d 41 52 59 20 4b 45 59 20 4c 50  = PRIMARY KEY LP
31ed5 20 69 64 78 6c 69 73 74 20 61 75 74 6f 69 6e 63   idxlist autoinc
31ed6 20 52 50 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73   RP onconf */.{s
31ed7 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72 79  qlite3AddPrimary
31ed8 4b 65 79 28 70 50 61 72 73 65 2c 79 79 6d 73 70  Key(pParse,yymsp
31ed9 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c  [-3].minor.yy14,
31eda 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
31edb 79 33 32 38 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d  y328,yymsp[-2].m
31edc 69 6e 6f 72 2e 79 79 33 32 38 2c 30 29 3b 7d 0a  inor.yy328,0);}.
31edd 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31ede 20 20 20 20 20 63 61 73 65 20 39 34 3a 20 2f 2a       case 94: /*
31edf 20 74 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49 51 55   tcons ::= UNIQU
31ee0 45 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20  E LP idxlist RP 
31ee1 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74  onconf */.{sqlit
31ee2 65 33 43 72 65 61 74 65 49 6e 64 65 78 28 70 50  e3CreateIndex(pP
31ee3 61 72 73 65 2c 30 2c 30 2c 30 2c 79 79 6d 73 70  arse,0,0,0,yymsp
31ee4 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c  [-2].minor.yy14,
31ee5 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
31ee6 79 33 32 38 2c 30 2c 30 2c 30 2c 30 29 3b 7d 0a  y328,0,0,0,0);}.
31ee7 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31ee8 20 20 20 20 20 63 61 73 65 20 39 35 3a 20 2f 2a       case 95: /*
31ee9 20 74 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43 4b   tcons ::= CHECK
31eea 20 4c 50 20 65 78 70 72 20 52 50 20 6f 6e 63 6f   LP expr RP onco
31eeb 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64  nf */.{sqlite3Ad
31eec 64 43 68 65 63 6b 43 6f 6e 73 74 72 61 69 6e 74  dCheckConstraint
31eed 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 32  (pParse,yymsp[-2
31eee 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  ].minor.yy346.pE
31eef 78 70 72 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  xpr);}.        b
31ef0 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31ef1 20 39 36 3a 20 2f 2a 20 74 63 6f 6e 73 20 3a 3a   96: /* tcons ::
31ef2 3d 20 46 4f 52 45 49 47 4e 20 4b 45 59 20 4c 50  = FOREIGN KEY LP
31ef3 20 69 64 78 6c 69 73 74 20 52 50 20 52 45 46 45   idxlist RP REFE
31ef4 52 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c 69 73  RENCES nm idxlis
31ef5 74 5f 6f 70 74 20 72 65 66 61 72 67 73 20 64 65  t_opt refargs de
31ef6 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70  fer_subclause_op
31ef7 74 20 2a 2f 0a 7b 0a 20 20 20 20 73 71 6c 69 74  t */.{.    sqlit
31ef8 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b  e3CreateForeignK
31ef9 65 79 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  ey(pParse, yymsp
31efa 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c  [-6].minor.yy14,
31efb 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f   &yymsp[-3].mino
31efc 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d 32 5d  r.yy0, yymsp[-2]
31efd 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d  .minor.yy14, yym
31efe 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
31eff 32 38 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  28);.    sqlite3
31f00 44 65 66 65 72 46 6f 72 65 69 67 6e 4b 65 79 28  DeferForeignKey(
31f01 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d  pParse, yymsp[0]
31f02 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b 0a 7d  .minor.yy328);.}
31f03 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
31f04 20 20 20 20 20 20 63 61 73 65 20 39 39 3a 20 2f        case 99: /
31f05 2a 20 6f 6e 63 6f 6e 66 20 3a 3a 3d 20 2a 2f 0a  * onconf ::= */.
31f06 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  {yygotominor.yy3
31f07 32 38 20 3d 20 4f 45 5f 44 65 66 61 75 6c 74 3b  28 = OE_Default;
31f08 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
31f09 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 31 3a  .      case 101:
31f0a 20 2f 2a 20 6f 72 63 6f 6e 66 20 3a 3a 3d 20 2a   /* orconf ::= *
31f0b 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
31f0c 79 31 38 36 20 3d 20 4f 45 5f 44 65 66 61 75 6c  y186 = OE_Defaul
31f0d 74 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  t;}.        brea
31f0e 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30  k;.      case 10
31f0f 32 3a 20 2f 2a 20 6f 72 63 6f 6e 66 20 3a 3a 3d  2: /* orconf ::=
31f10 20 4f 52 20 72 65 73 6f 6c 76 65 74 79 70 65 20   OR resolvetype 
31f11 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
31f12 79 79 31 38 36 20 3d 20 28 75 38 29 79 79 6d 73  yy186 = (u8)yyms
31f13 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  p[0].minor.yy328
31f14 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
31f15 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30 34  ;.      case 104
31f16 3a 20 2f 2a 20 72 65 73 6f 6c 76 65 74 79 70 65  : /* resolvetype
31f17 20 3a 3a 3d 20 49 47 4e 4f 52 45 20 2a 2f 0a 7b   ::= IGNORE */.{
31f18 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32  yygotominor.yy32
31f19 38 20 3d 20 4f 45 5f 49 67 6e 6f 72 65 3b 7d 0a  8 = OE_Ignore;}.
31f1a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31f1b 20 20 20 20 20 63 61 73 65 20 31 30 35 3a 20 2f       case 105: /
31f1c 2a 20 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a  * resolvetype ::
31f1d 3d 20 52 45 50 4c 41 43 45 20 2a 2f 0a 7b 79 79  = REPLACE */.{yy
31f1e 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20  gotominor.yy328 
31f1f 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b 7d 0a 20  = OE_Replace;}. 
31f20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31f21 20 20 20 20 63 61 73 65 20 31 30 36 3a 20 2f 2a      case 106: /*
31f22 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 41   cmd ::= DROP TA
31f23 42 4c 45 20 69 66 65 78 69 73 74 73 20 66 75 6c  BLE ifexists ful
31f24 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c  lname */.{.  sql
31f25 69 74 65 33 44 72 6f 70 54 61 62 6c 65 28 70 50  ite3DropTable(pP
31f26 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  arse, yymsp[0].m
31f27 69 6e 6f 72 2e 79 79 36 35 2c 20 30 2c 20 79 79  inor.yy65, 0, yy
31f28 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
31f29 33 32 38 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  328);.}.        
31f2a 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
31f2b 65 20 31 30 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 109: /* cmd ::
31f2c 3d 20 63 72 65 61 74 65 6b 77 20 74 65 6d 70 20  = createkw temp 
31f2d 56 49 45 57 20 69 66 6e 6f 74 65 78 69 73 74 73  VIEW ifnotexists
31f2e 20 6e 6d 20 64 62 6e 6d 20 41 53 20 73 65 6c 65   nm dbnm AS sele
31f2f 63 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65  ct */.{.  sqlite
31f30 33 43 72 65 61 74 65 56 69 65 77 28 70 50 61 72  3CreateView(pPar
31f31 73 65 2c 20 26 79 79 6d 73 70 5b 2d 37 5d 2e 6d  se, &yymsp[-7].m
31f32 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70  inor.yy0, &yymsp
31f33 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-3].minor.yy0, 
31f34 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
31f35 2e 79 79 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  .yy0, yymsp[0].m
31f36 69 6e 6f 72 2e 79 79 33 2c 20 79 79 6d 73 70 5b  inor.yy3, yymsp[
31f37 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 2c  -6].minor.yy328,
31f38 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72   yymsp[-4].minor
31f39 2e 79 79 33 32 38 29 3b 0a 7d 0a 20 20 20 20 20  .yy328);.}.     
31f3a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
31f3b 63 61 73 65 20 31 31 30 3a 20 2f 2a 20 63 6d 64  case 110: /* cmd
31f3c 20 3a 3a 3d 20 44 52 4f 50 20 56 49 45 57 20 69   ::= DROP VIEW i
31f3d 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65  fexists fullname
31f3e 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 44   */.{.  sqlite3D
31f3f 72 6f 70 54 61 62 6c 65 28 70 50 61 72 73 65 2c  ropTable(pParse,
31f40 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
31f41 79 79 36 35 2c 20 31 2c 20 79 79 6d 73 70 5b 2d  yy65, 1, yymsp[-
31f42 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29 3b  1].minor.yy328);
31f43 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
31f44 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 31  ;.      case 111
31f45 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 73 65 6c  : /* cmd ::= sel
31f46 65 63 74 20 2a 2f 0a 7b 0a 20 20 53 65 6c 65 63  ect */.{.  Selec
31f47 74 44 65 73 74 20 64 65 73 74 20 3d 20 7b 53 52  tDest dest = {SR
31f48 54 5f 4f 75 74 70 75 74 2c 20 30 2c 20 30 2c 20  T_Output, 0, 0, 
31f49 30 2c 20 30 7d 3b 0a 20 20 73 71 6c 69 74 65 33  0, 0};.  sqlite3
31f4a 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 79  Select(pParse, y
31f4b 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
31f4c 33 2c 20 26 64 65 73 74 29 3b 0a 20 20 73 71 6c  3, &dest);.  sql
31f4d 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
31f4e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d  (pParse->db, yym
31f4f 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 29  sp[0].minor.yy3)
31f50 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
31f51 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31  k;.      case 11
31f52 32 3a 20 2f 2a 20 73 65 6c 65 63 74 20 3a 3a 3d  2: /* select ::=
31f53 20 6f 6e 65 73 65 6c 65 63 74 20 2a 2f 0a 7b 79   oneselect */.{y
31f54 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 20 3d  ygotominor.yy3 =
31f55 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
31f56 79 79 33 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  yy3;}.        br
31f57 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
31f58 31 31 33 3a 20 2f 2a 20 73 65 6c 65 63 74 20 3a  113: /* select :
31f59 3a 3d 20 73 65 6c 65 63 74 20 6d 75 6c 74 69 73  := select multis
31f5a 65 6c 65 63 74 5f 6f 70 20 6f 6e 65 73 65 6c 65  elect_op onesele
31f5b 63 74 20 2a 2f 0a 7b 0a 20 20 69 66 28 20 79 79  ct */.{.  if( yy
31f5c 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
31f5d 20 29 7b 0a 20 20 20 20 79 79 6d 73 70 5b 30 5d   ){.    yymsp[0]
31f5e 2e 6d 69 6e 6f 72 2e 79 79 33 2d 3e 6f 70 20 3d  .minor.yy3->op =
31f5f 20 28 75 38 29 79 79 6d 73 70 5b 2d 31 5d 2e 6d   (u8)yymsp[-1].m
31f60 69 6e 6f 72 2e 79 79 33 32 38 3b 0a 20 20 20 20  inor.yy328;.    
31f61 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
31f62 79 33 2d 3e 70 50 72 69 6f 72 20 3d 20 79 79 6d  y3->pPrior = yym
31f63 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-2].minor.yy3
31f64 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
31f65 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
31f66 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79  te(pParse->db, y
31f67 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
31f68 79 33 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74  y3);.  }.  yygot
31f69 6f 6d 69 6e 6f 72 2e 79 79 33 20 3d 20 79 79 6d  ominor.yy3 = yym
31f6a 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 3b  sp[0].minor.yy3;
31f6b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
31f6c 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 35  ;.      case 115
31f6d 3a 20 2f 2a 20 6d 75 6c 74 69 73 65 6c 65 63 74  : /* multiselect
31f6e 5f 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20 41 4c  _op ::= UNION AL
31f6f 4c 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  L */.{yygotomino
31f70 72 2e 79 79 33 32 38 20 3d 20 54 4b 5f 41 4c 4c  r.yy328 = TK_ALL
31f71 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
31f72 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 31 37  ;.      case 117
31f73 3a 20 2f 2a 20 6f 6e 65 73 65 6c 65 63 74 20 3a  : /* oneselect :
31f74 3a 3d 20 53 45 4c 45 43 54 20 64 69 73 74 69 6e  := SELECT distin
31f75 63 74 20 73 65 6c 63 6f 6c 6c 69 73 74 20 66 72  ct selcollist fr
31f76 6f 6d 20 77 68 65 72 65 5f 6f 70 74 20 67 72 6f  om where_opt gro
31f77 75 70 62 79 5f 6f 70 74 20 68 61 76 69 6e 67 5f  upby_opt having_
31f78 6f 70 74 20 6f 72 64 65 72 62 79 5f 6f 70 74 20  opt orderby_opt 
31f79 6c 69 6d 69 74 5f 6f 70 74 20 2a 2f 0a 7b 0a 20  limit_opt */.{. 
31f7a 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
31f7b 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
31f7c 4e 65 77 28 70 50 61 72 73 65 2c 79 79 6d 73 70  New(pParse,yymsp
31f7d 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c  [-6].minor.yy14,
31f7e 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e  yymsp[-5].minor.
31f7f 79 79 36 35 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d  yy65,yymsp[-4].m
31f80 69 6e 6f 72 2e 79 79 31 33 32 2c 79 79 6d 73 70  inor.yy132,yymsp
31f81 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c  [-3].minor.yy14,
31f82 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
31f83 79 79 31 33 32 2c 79 79 6d 73 70 5b 2d 31 5d 2e  yy132,yymsp[-1].
31f84 6d 69 6e 6f 72 2e 79 79 31 34 2c 79 79 6d 73 70  minor.yy14,yymsp
31f85 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  [-7].minor.yy328
31f86 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  ,yymsp[0].minor.
31f87 79 79 34 37 36 2e 70 4c 69 6d 69 74 2c 79 79 6d  yy476.pLimit,yym
31f88 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37  sp[0].minor.yy47
31f89 36 2e 70 4f 66 66 73 65 74 29 3b 0a 7d 0a 20 20  6.pOffset);.}.  
31f8a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31f8b 20 20 20 63 61 73 65 20 31 32 31 3a 20 2f 2a 20     case 121: /* 
31f8c 73 63 6c 70 20 3a 3a 3d 20 73 65 6c 63 6f 6c 6c  sclp ::= selcoll
31f8d 69 73 74 20 43 4f 4d 4d 41 20 2a 2f 0a 20 20 20  ist COMMA */.   
31f8e 20 20 20 63 61 73 65 20 32 34 37 3a 20 2f 2a 20     case 247: /* 
31f8f 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20  idxlist_opt ::= 
31f90 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20 2a 2f  LP idxlist RP */
31f91 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
31f92 6c 65 6e 6f 3d 3d 32 34 37 29 3b 0a 7b 79 79 67  leno==247);.{yyg
31f93 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20  otominor.yy14 = 
31f94 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
31f95 79 79 31 34 3b 7d 0a 20 20 20 20 20 20 20 20 62  yy14;}.        b
31f96 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
31f97 20 31 32 32 3a 20 2f 2a 20 73 63 6c 70 20 3a 3a   122: /* sclp ::
31f98 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
31f99 31 35 30 3a 20 2f 2a 20 6f 72 64 65 72 62 79 5f  150: /* orderby_
31f9a 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73  opt ::= */ yytes
31f9b 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
31f9c 31 35 30 29 3b 0a 20 20 20 20 20 20 63 61 73 65  150);.      case
31f9d 20 31 35 38 3a 20 2f 2a 20 67 72 6f 75 70 62 79   158: /* groupby
31f9e 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65  _opt ::= */ yyte
31f9f 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
31fa0 3d 31 35 38 29 3b 0a 20 20 20 20 20 20 63 61 73  =158);.      cas
31fa1 65 20 32 34 30 3a 20 2f 2a 20 65 78 70 72 6c 69  e 240: /* exprli
31fa2 73 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  st ::= */ yytest
31fa3 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
31fa4 34 30 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20  40);.      case 
31fa5 32 34 36 3a 20 2f 2a 20 69 64 78 6c 69 73 74 5f  246: /* idxlist_
31fa6 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73  opt ::= */ yytes
31fa7 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
31fa8 32 34 36 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e  246);.{yygotomin
31fa9 6f 72 2e 79 79 31 34 20 3d 20 30 3b 7d 0a 20 20  or.yy14 = 0;}.  
31faa 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31fab 20 20 20 63 61 73 65 20 31 32 33 3a 20 2f 2a 20     case 123: /* 
31fac 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 73  selcollist ::= s
31fad 63 6c 70 20 65 78 70 72 20 61 73 20 2a 2f 0a 7b  clp expr as */.{
31fae 0a 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  .   yygotominor.
31faf 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78  yy14 = sqlite3Ex
31fb0 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
31fb1 72 73 65 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  rse, yymsp[-2].m
31fb2 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d 73 70  inor.yy14, yymsp
31fb3 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  [-1].minor.yy346
31fb4 2e 70 45 78 70 72 29 3b 0a 20 20 20 69 66 28 20  .pExpr);.   if( 
31fb5 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
31fb6 79 30 2e 6e 3e 30 20 29 20 73 71 6c 69 74 65 33  y0.n>0 ) sqlite3
31fb7 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28  ExprListSetName(
31fb8 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69  pParse, yygotomi
31fb9 6e 6f 72 2e 79 79 31 34 2c 20 26 79 79 6d 73 70  nor.yy14, &yymsp
31fba 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 31  [0].minor.yy0, 1
31fbb 29 3b 0a 20 20 20 73 71 6c 69 74 65 33 45 78 70  );.   sqlite3Exp
31fbc 72 4c 69 73 74 53 65 74 53 70 61 6e 28 70 50 61  rListSetSpan(pPa
31fbd 72 73 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  rse,yygotominor.
31fbe 79 79 31 34 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  yy14,&yymsp[-1].
31fbf 6d 69 6e 6f 72 2e 79 79 33 34 36 29 3b 0a 7d 0a  minor.yy346);.}.
31fc0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31fc1 20 20 20 20 20 63 61 73 65 20 31 32 34 3a 20 2f       case 124: /
31fc2 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d  * selcollist ::=
31fc3 20 73 63 6c 70 20 53 54 41 52 20 2a 2f 0a 7b 0a   sclp STAR */.{.
31fc4 20 20 45 78 70 72 20 2a 70 20 3d 20 73 71 6c 69    Expr *p = sqli
31fc5 74 65 33 45 78 70 72 28 70 50 61 72 73 65 2d 3e  te3Expr(pParse->
31fc6 64 62 2c 20 54 4b 5f 41 4c 4c 2c 20 30 29 3b 0a  db, TK_ALL, 0);.
31fc7 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
31fc8 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  14 = sqlite3Expr
31fc9 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
31fca 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  e, yymsp[-1].min
31fcb 6f 72 2e 79 79 31 34 2c 20 70 29 3b 0a 7d 0a 20  or.yy14, p);.}. 
31fcc 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
31fcd 20 20 20 20 63 61 73 65 20 31 32 35 3a 20 2f 2a      case 125: /*
31fce 20 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20   selcollist ::= 
31fcf 73 63 6c 70 20 6e 6d 20 44 4f 54 20 53 54 41 52  sclp nm DOT STAR
31fd0 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 70 52   */.{.  Expr *pR
31fd1 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 50 45  ight = sqlite3PE
31fd2 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 41  xpr(pParse, TK_A
31fd3 4c 4c 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70  LL, 0, 0, &yymsp
31fd4 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
31fd5 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d 20    Expr *pLeft = 
31fd6 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
31fd7 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30  rse, TK_ID, 0, 0
31fd8 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  , &yymsp[-2].min
31fd9 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78 70 72 20  or.yy0);.  Expr 
31fda 2a 70 44 6f 74 20 3d 20 73 71 6c 69 74 65 33 50  *pDot = sqlite3P
31fdb 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
31fdc 44 4f 54 2c 20 70 4c 65 66 74 2c 20 70 52 69 67  DOT, pLeft, pRig
31fdd 68 74 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f  ht, 0);.  yygoto
31fde 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c  minor.yy14 = sql
31fdf 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
31fe0 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  nd(pParse,yymsp[
31fe1 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20  -3].minor.yy14, 
31fe2 70 44 6f 74 29 3b 0a 7d 0a 20 20 20 20 20 20 20  pDot);.}.       
31fe3 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31fe4 73 65 20 31 32 38 3a 20 2f 2a 20 61 73 20 3a 3a  se 128: /* as ::
31fe5 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  = */.{yygotomino
31fe6 72 2e 79 79 30 2e 6e 20 3d 20 30 3b 7d 0a 20 20  r.yy0.n = 0;}.  
31fe7 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31fe8 20 20 20 63 61 73 65 20 31 32 39 3a 20 2f 2a 20     case 129: /* 
31fe9 66 72 6f 6d 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67  from ::= */.{yyg
31fea 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20 3d 20  otominor.yy65 = 
31feb 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
31fec 65 72 6f 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ero(pParse->db, 
31fed 73 69 7a 65 6f 66 28 2a 79 79 67 6f 74 6f 6d 69  sizeof(*yygotomi
31fee 6e 6f 72 2e 79 79 36 35 29 29 3b 7d 0a 20 20 20  nor.yy65));}.   
31fef 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
31ff0 20 20 63 61 73 65 20 31 33 30 3a 20 2f 2a 20 66    case 130: /* f
31ff1 72 6f 6d 20 3a 3a 3d 20 46 52 4f 4d 20 73 65 6c  rom ::= FROM sel
31ff2 74 61 62 6c 69 73 74 20 2a 2f 0a 7b 0a 20 20 79  tablist */.{.  y
31ff3 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20  ygotominor.yy65 
31ff4 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
31ff5 2e 79 79 36 35 3b 0a 20 20 73 71 6c 69 74 65 33  .yy65;.  sqlite3
31ff6 53 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e  SrcListShiftJoin
31ff7 54 79 70 65 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  Type(yygotominor
31ff8 2e 79 79 36 35 29 3b 0a 7d 0a 20 20 20 20 20 20  .yy65);.}.      
31ff9 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
31ffa 61 73 65 20 31 33 31 3a 20 2f 2a 20 73 74 6c 5f  ase 131: /* stl_
31ffb 70 72 65 66 69 78 20 3a 3a 3d 20 73 65 6c 74 61  prefix ::= selta
31ffc 62 6c 69 73 74 20 6a 6f 69 6e 6f 70 20 2a 2f 0a  blist joinop */.
31ffd 7b 0a 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  {.   yygotominor
31ffe 2e 79 79 36 35 20 3d 20 79 79 6d 73 70 5b 2d 31  .yy65 = yymsp[-1
31fff 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 3b 0a 20 20  ].minor.yy65;.  
32000 20 69 66 28 20 41 4c 57 41 59 53 28 79 79 67 6f   if( ALWAYS(yygo
32001 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20 26 26 20  tominor.yy65 && 
32002 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35  yygotominor.yy65
32003 2d 3e 6e 53 72 63 3e 30 29 20 29 20 79 79 67 6f  ->nSrc>0) ) yygo
32004 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 2d 3e 61 5b  tominor.yy65->a[
32005 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35  yygotominor.yy65
32006 2d 3e 6e 53 72 63 2d 31 5d 2e 6a 6f 69 6e 74 79  ->nSrc-1].jointy
32007 70 65 20 3d 20 28 75 38 29 79 79 6d 73 70 5b 30  pe = (u8)yymsp[0
32008 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 0a 7d  ].minor.yy328;.}
32009 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3200a 20 20 20 20 20 20 63 61 73 65 20 31 33 32 3a 20        case 132: 
3200b 2f 2a 20 73 74 6c 5f 70 72 65 66 69 78 20 3a 3a  /* stl_prefix ::
3200c 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  = */.{yygotomino
3200d 72 2e 79 79 36 35 20 3d 20 30 3b 7d 0a 20 20 20  r.yy65 = 0;}.   
3200e 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3200f 20 20 63 61 73 65 20 31 33 33 3a 20 2f 2a 20 73    case 133: /* s
32010 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20 73 74  eltablist ::= st
32011 6c 5f 70 72 65 66 69 78 20 6e 6d 20 64 62 6e 6d  l_prefix nm dbnm
32012 20 61 73 20 69 6e 64 65 78 65 64 5f 6f 70 74 20   as indexed_opt 
32013 6f 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74  on_opt using_opt
32014 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
32015 6e 6f 72 2e 79 79 36 35 20 3d 20 73 71 6c 69 74  nor.yy65 = sqlit
32016 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 46  e3SrcListAppendF
32017 72 6f 6d 54 65 72 6d 28 70 50 61 72 73 65 2c 79  romTerm(pParse,y
32018 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-6].minor.y
32019 79 36 35 2c 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d  y65,&yymsp[-5].m
3201a 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
3201b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -4].minor.yy0,&y
3201c 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
3201d 79 30 2c 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d  y0,0,yymsp[-1].m
3201e 69 6e 6f 72 2e 79 79 31 33 32 2c 79 79 6d 73 70  inor.yy132,yymsp
3201f 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 29  [0].minor.yy408)
32020 3b 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  ;.  sqlite3SrcLi
32021 73 74 49 6e 64 65 78 65 64 42 79 28 70 50 61 72  stIndexedBy(pPar
32022 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  se, yygotominor.
32023 79 79 36 35 2c 20 26 79 79 6d 73 70 5b 2d 32 5d  yy65, &yymsp[-2]
32024 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20  .minor.yy0);.}. 
32025 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32026 20 20 20 20 63 61 73 65 20 31 33 34 3a 20 2f 2a      case 134: /*
32027 20 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20   seltablist ::= 
32028 73 74 6c 5f 70 72 65 66 69 78 20 4c 50 20 73 65  stl_prefix LP se
32029 6c 65 63 74 20 52 50 20 61 73 20 6f 6e 5f 6f 70  lect RP as on_op
3202a 74 20 75 73 69 6e 67 5f 6f 70 74 20 2a 2f 0a 7b  t using_opt */.{
3202b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
3202c 2e 79 79 36 35 20 3d 20 73 71 6c 69 74 65 33 53  .yy65 = sqlite3S
3202d 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d  rcListAppendFrom
3202e 54 65 72 6d 28 70 50 61 72 73 65 2c 79 79 6d 73  Term(pParse,yyms
3202f 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35  p[-6].minor.yy65
32030 2c 30 2c 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e  ,0,0,&yymsp[-2].
32031 6d 69 6e 6f 72 2e 79 79 30 2c 79 79 6d 73 70 5b  minor.yy0,yymsp[
32032 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 2c 79 79  -4].minor.yy3,yy
32033 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
32034 31 33 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  132,yymsp[0].min
32035 6f 72 2e 79 79 34 30 38 29 3b 0a 20 20 7d 0a 20  or.yy408);.  }. 
32036 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32037 20 20 20 20 63 61 73 65 20 31 33 35 3a 20 2f 2a      case 135: /*
32038 20 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a 3d 20   seltablist ::= 
32039 73 74 6c 5f 70 72 65 66 69 78 20 4c 50 20 73 65  stl_prefix LP se
3203a 6c 74 61 62 6c 69 73 74 20 52 50 20 61 73 20 6f  ltablist RP as o
3203b 6e 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 20  n_opt using_opt 
3203c 2a 2f 0a 7b 0a 20 20 20 20 69 66 28 20 79 79 6d  */.{.    if( yym
3203d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 36  sp[-6].minor.yy6
3203e 35 3d 3d 30 20 26 26 20 79 79 6d 73 70 5b 2d 32  5==0 && yymsp[-2
3203f 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 3d 30  ].minor.yy0.n==0
32040 20 26 26 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69   && yymsp[-1].mi
32041 6e 6f 72 2e 79 79 31 33 32 3d 3d 30 20 26 26 20  nor.yy132==0 && 
32042 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32043 79 34 30 38 3d 3d 30 20 29 7b 0a 20 20 20 20 20  y408==0 ){.     
32044 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36   yygotominor.yy6
32045 35 20 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  5 = yymsp[-4].mi
32046 6e 6f 72 2e 79 79 36 35 3b 0a 20 20 20 20 7d 65  nor.yy65;.    }e
32047 6c 73 65 7b 0a 20 20 20 20 20 20 53 65 6c 65 63  lse{.      Selec
32048 74 20 2a 70 53 75 62 71 75 65 72 79 3b 0a 20 20  t *pSubquery;.  
32049 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69      sqlite3SrcLi
3204a 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28  stShiftJoinType(
3204b 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
3204c 79 79 36 35 29 3b 0a 20 20 20 20 20 20 70 53 75  yy65);.      pSu
3204d 62 71 75 65 72 79 20 3d 20 73 71 6c 69 74 65 33  bquery = sqlite3
3204e 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65  SelectNew(pParse
3204f 2c 30 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  ,0,yymsp[-4].min
32050 6f 72 2e 79 79 36 35 2c 30 2c 30 2c 30 2c 30 2c  or.yy65,0,0,0,0,
32051 30 2c 30 2c 30 29 3b 0a 20 20 20 20 20 20 79 79  0,0,0);.      yy
32052 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 36 35 20 3d  gotominor.yy65 =
32053 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
32054 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50  ppendFromTerm(pP
32055 61 72 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e 6d  arse,yymsp[-6].m
32056 69 6e 6f 72 2e 79 79 36 35 2c 30 2c 30 2c 26 79  inor.yy65,0,0,&y
32057 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
32058 79 30 2c 70 53 75 62 71 75 65 72 79 2c 79 79 6d  y0,pSubquery,yym
32059 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-1].minor.yy1
3205a 33 32 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  32,yymsp[0].mino
3205b 72 2e 79 79 34 30 38 29 3b 0a 20 20 20 20 7d 0a  r.yy408);.    }.
3205c 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
3205d 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 33  k;.      case 13
3205e 36 3a 20 2f 2a 20 64 62 6e 6d 20 3a 3a 3d 20 2a  6: /* dbnm ::= *
3205f 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 35  /.      case 145
32060 3a 20 2f 2a 20 69 6e 64 65 78 65 64 5f 6f 70 74  : /* indexed_opt
32061 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61   ::= */ yytestca
32062 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 34 35  se(yyruleno==145
32063 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  );.{yygotominor.
32064 79 79 30 2e 7a 3d 30 3b 20 79 79 67 6f 74 6f 6d  yy0.z=0; yygotom
32065 69 6e 6f 72 2e 79 79 30 2e 6e 3d 30 3b 7d 0a 20  inor.yy0.n=0;}. 
32066 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32067 20 20 20 20 63 61 73 65 20 31 33 38 3a 20 2f 2a      case 138: /*
32068 20 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 6e 6d   fullname ::= nm
32069 20 64 62 6e 6d 20 2a 2f 0a 7b 79 79 67 6f 74 6f   dbnm */.{yygoto
3206a 6d 69 6e 6f 72 2e 79 79 36 35 20 3d 20 73 71 6c  minor.yy65 = sql
3206b 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
3206c 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 30 2c 26  d(pParse->db,0,&
3206d 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3206e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy0,&yymsp[0].mi
3206f 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20  nor.yy0);}.     
32070 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32071 63 61 73 65 20 31 33 39 3a 20 2f 2a 20 6a 6f 69  case 139: /* joi
32072 6e 6f 70 20 3a 3a 3d 20 43 4f 4d 4d 41 7c 4a 4f  nop ::= COMMA|JO
32073 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69  IN */.{ yygotomi
32074 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4a 54 5f 49  nor.yy328 = JT_I
32075 4e 4e 45 52 3b 20 7d 0a 20 20 20 20 20 20 20 20  NNER; }.        
32076 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32077 65 20 31 34 30 3a 20 2f 2a 20 6a 6f 69 6e 6f 70  e 140: /* joinop
32078 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 4a 4f 49   ::= JOIN_KW JOI
32079 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  N */.{ yygotomin
3207a 6f 72 2e 79 79 33 32 38 20 3d 20 73 71 6c 69 74  or.yy328 = sqlit
3207b 65 33 4a 6f 69 6e 54 79 70 65 28 70 50 61 72 73  e3JoinType(pPars
3207c 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  e,&yymsp[-1].min
3207d 6f 72 2e 79 79 30 2c 30 2c 30 29 3b 20 7d 0a 20  or.yy0,0,0); }. 
3207e 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3207f 20 20 20 20 63 61 73 65 20 31 34 31 3a 20 2f 2a      case 141: /*
32080 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e   joinop ::= JOIN
32081 5f 4b 57 20 6e 6d 20 4a 4f 49 4e 20 2a 2f 0a 7b  _KW nm JOIN */.{
32082 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
32083 32 38 20 3d 20 73 71 6c 69 74 65 33 4a 6f 69 6e  28 = sqlite3Join
32084 54 79 70 65 28 70 50 61 72 73 65 2c 26 79 79 6d  Type(pParse,&yym
32085 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-2].minor.yy0
32086 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ,&yymsp[-1].mino
32087 72 2e 79 79 30 2c 30 29 3b 20 7d 0a 20 20 20 20  r.yy0,0); }.    
32088 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32089 20 63 61 73 65 20 31 34 32 3a 20 2f 2a 20 6a 6f   case 142: /* jo
3208a 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57  inop ::= JOIN_KW
3208b 20 6e 6d 20 6e 6d 20 4a 4f 49 4e 20 2a 2f 0a 7b   nm nm JOIN */.{
3208c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
3208d 32 38 20 3d 20 73 71 6c 69 74 65 33 4a 6f 69 6e  28 = sqlite3Join
3208e 54 79 70 65 28 70 50 61 72 73 65 2c 26 79 79 6d  Type(pParse,&yym
3208f 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
32090 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  ,&yymsp[-2].mino
32091 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31 5d  r.yy0,&yymsp[-1]
32092 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 20 7d 0a 20  .minor.yy0); }. 
32093 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32094 20 20 20 20 63 61 73 65 20 31 34 33 3a 20 2f 2a      case 143: /*
32095 20 6f 6e 5f 6f 70 74 20 3a 3a 3d 20 4f 4e 20 65   on_opt ::= ON e
32096 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  xpr */.      cas
32097 65 20 31 35 34 3a 20 2f 2a 20 73 6f 72 74 69 74  e 154: /* sortit
32098 65 6d 20 3a 3a 3d 20 65 78 70 72 20 2a 2f 20 79  em ::= expr */ y
32099 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
3209a 6e 6f 3d 3d 31 35 34 29 3b 0a 20 20 20 20 20 20  no==154);.      
3209b 63 61 73 65 20 31 36 31 3a 20 2f 2a 20 68 61 76  case 161: /* hav
3209c 69 6e 67 5f 6f 70 74 20 3a 3a 3d 20 48 41 56 49  ing_opt ::= HAVI
3209d 4e 47 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73  NG expr */ yytes
3209e 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3209f 31 36 31 29 3b 0a 20 20 20 20 20 20 63 61 73 65  161);.      case
320a0 20 31 36 38 3a 20 2f 2a 20 77 68 65 72 65 5f 6f   168: /* where_o
320a1 70 74 20 3a 3a 3d 20 57 48 45 52 45 20 65 78 70  pt ::= WHERE exp
320a2 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  r */ yytestcase(
320a3 79 79 72 75 6c 65 6e 6f 3d 3d 31 36 38 29 3b 0a  yyruleno==168);.
320a4 20 20 20 20 20 20 63 61 73 65 20 32 33 35 3a 20        case 235: 
320a5 2f 2a 20 63 61 73 65 5f 65 6c 73 65 20 3a 3a 3d  /* case_else ::=
320a6 20 45 4c 53 45 20 65 78 70 72 20 2a 2f 20 79 79   ELSE expr */ yy
320a7 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
320a8 6f 3d 3d 32 33 35 29 3b 0a 20 20 20 20 20 20 63  o==235);.      c
320a9 61 73 65 20 32 33 37 3a 20 2f 2a 20 63 61 73 65  ase 237: /* case
320aa 5f 6f 70 65 72 61 6e 64 20 3a 3a 3d 20 65 78 70  _operand ::= exp
320ab 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  r */ yytestcase(
320ac 79 79 72 75 6c 65 6e 6f 3d 3d 32 33 37 29 3b 0a  yyruleno==237);.
320ad 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31  {yygotominor.yy1
320ae 33 32 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  32 = yymsp[0].mi
320af 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 3b  nor.yy346.pExpr;
320b0 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
320b1 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 34 3a  .      case 144:
320b2 20 2f 2a 20 6f 6e 5f 6f 70 74 20 3a 3a 3d 20 2a   /* on_opt ::= *
320b3 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 30  /.      case 160
320b4 3a 20 2f 2a 20 68 61 76 69 6e 67 5f 6f 70 74 20  : /* having_opt 
320b5 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ::= */ yytestcas
320b6 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 36 30 29  e(yyruleno==160)
320b7 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 37  ;.      case 167
320b8 3a 20 2f 2a 20 77 68 65 72 65 5f 6f 70 74 20 3a  : /* where_opt :
320b9 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  := */ yytestcase
320ba 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 36 37 29 3b  (yyruleno==167);
320bb 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 36 3a  .      case 236:
320bc 20 2f 2a 20 63 61 73 65 5f 65 6c 73 65 20 3a 3a   /* case_else ::
320bd 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  = */ yytestcase(
320be 79 79 72 75 6c 65 6e 6f 3d 3d 32 33 36 29 3b 0a  yyruleno==236);.
320bf 20 20 20 20 20 20 63 61 73 65 20 32 33 38 3a 20        case 238: 
320c0 2f 2a 20 63 61 73 65 5f 6f 70 65 72 61 6e 64 20  /* case_operand 
320c1 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ::= */ yytestcas
320c2 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 33 38 29  e(yyruleno==238)
320c3 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  ;.{yygotominor.y
320c4 79 31 33 32 20 3d 20 30 3b 7d 0a 20 20 20 20 20  y132 = 0;}.     
320c5 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
320c6 63 61 73 65 20 31 34 37 3a 20 2f 2a 20 69 6e 64  case 147: /* ind
320c7 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 4e 4f 54  exed_opt ::= NOT
320c8 20 49 4e 44 45 58 45 44 20 2a 2f 0a 7b 79 79 67   INDEXED */.{yyg
320c9 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3d 30  otominor.yy0.z=0
320ca 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ; yygotominor.yy
320cb 30 2e 6e 3d 31 3b 7d 0a 20 20 20 20 20 20 20 20  0.n=1;}.        
320cc 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
320cd 65 20 31 34 38 3a 20 2f 2a 20 75 73 69 6e 67 5f  e 148: /* using_
320ce 6f 70 74 20 3a 3a 3d 20 55 53 49 4e 47 20 4c 50  opt ::= USING LP
320cf 20 69 6e 73 63 6f 6c 6c 69 73 74 20 52 50 20 2a   inscollist RP *
320d0 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 30  /.      case 180
320d1 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 5f  : /* inscollist_
320d2 6f 70 74 20 3a 3a 3d 20 4c 50 20 69 6e 73 63 6f  opt ::= LP insco
320d3 6c 6c 69 73 74 20 52 50 20 2a 2f 20 79 79 74 65  llist RP */ yyte
320d4 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
320d5 3d 31 38 30 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69  =180);.{yygotomi
320d6 6e 6f 72 2e 79 79 34 30 38 20 3d 20 79 79 6d 73  nor.yy408 = yyms
320d7 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30  p[-1].minor.yy40
320d8 38 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  8;}.        brea
320d9 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 34  k;.      case 14
320da 39 3a 20 2f 2a 20 75 73 69 6e 67 5f 6f 70 74 20  9: /* using_opt 
320db 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ::= */.      cas
320dc 65 20 31 37 39 3a 20 2f 2a 20 69 6e 73 63 6f 6c  e 179: /* inscol
320dd 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20  list_opt ::= */ 
320de 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
320df 65 6e 6f 3d 3d 31 37 39 29 3b 0a 7b 79 79 67 6f  eno==179);.{yygo
320e0 74 6f 6d 69 6e 6f 72 2e 79 79 34 30 38 20 3d 20  tominor.yy408 = 
320e1 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
320e2 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 35  k;.      case 15
320e3 31 3a 20 2f 2a 20 6f 72 64 65 72 62 79 5f 6f 70  1: /* orderby_op
320e4 74 20 3a 3a 3d 20 4f 52 44 45 52 20 42 59 20 73  t ::= ORDER BY s
320e5 6f 72 74 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20  ortlist */.     
320e6 20 63 61 73 65 20 31 35 39 3a 20 2f 2a 20 67 72   case 159: /* gr
320e7 6f 75 70 62 79 5f 6f 70 74 20 3a 3a 3d 20 47 52  oupby_opt ::= GR
320e8 4f 55 50 20 42 59 20 6e 65 78 70 72 6c 69 73 74  OUP BY nexprlist
320e9 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
320ea 79 72 75 6c 65 6e 6f 3d 3d 31 35 39 29 3b 0a 20  yruleno==159);. 
320eb 20 20 20 20 20 63 61 73 65 20 32 33 39 3a 20 2f       case 239: /
320ec 2a 20 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20 6e  * exprlist ::= n
320ed 65 78 70 72 6c 69 73 74 20 2a 2f 20 79 79 74 65  exprlist */ yyte
320ee 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
320ef 3d 32 33 39 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69  =239);.{yygotomi
320f0 6e 6f 72 2e 79 79 31 34 20 3d 20 79 79 6d 73 70  nor.yy14 = yymsp
320f1 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 3b 7d  [0].minor.yy14;}
320f2 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
320f3 20 20 20 20 20 20 63 61 73 65 20 31 35 32 3a 20        case 152: 
320f4 2f 2a 20 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20  /* sortlist ::= 
320f5 73 6f 72 74 6c 69 73 74 20 43 4f 4d 4d 41 20 73  sortlist COMMA s
320f6 6f 72 74 69 74 65 6d 20 73 6f 72 74 6f 72 64 65  ortitem sortorde
320f7 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  r */.{.  yygotom
320f8 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69  inor.yy14 = sqli
320f9 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
320fa 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  d(pParse,yymsp[-
320fb 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79 79  3].minor.yy14,yy
320fc 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
320fd 31 33 32 29 3b 0a 20 20 69 66 28 20 79 79 67 6f  132);.  if( yygo
320fe 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 29 20 79  tominor.yy14 ) y
320ff 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2d  ygotominor.yy14-
32100 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  >a[yygotominor.y
32101 79 31 34 2d 3e 6e 45 78 70 72 2d 31 5d 2e 73 6f  y14->nExpr-1].so
32102 72 74 4f 72 64 65 72 20 3d 20 28 75 38 29 79 79  rtOrder = (u8)yy
32103 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
32104 32 38 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  28;.}.        br
32105 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32106 31 35 33 3a 20 2f 2a 20 73 6f 72 74 6c 69 73 74  153: /* sortlist
32107 20 3a 3a 3d 20 73 6f 72 74 69 74 65 6d 20 73 6f   ::= sortitem so
32108 72 74 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20 20 79  rtorder */.{.  y
32109 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20  ygotominor.yy14 
3210a 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
3210b 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30  tAppend(pParse,0
3210c 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  ,yymsp[-1].minor
3210d 2e 79 79 31 33 32 29 3b 0a 20 20 69 66 28 20 79  .yy132);.  if( y
3210e 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20  ygotominor.yy14 
3210f 26 26 20 41 4c 57 41 59 53 28 79 79 67 6f 74 6f  && ALWAYS(yygoto
32110 6d 69 6e 6f 72 2e 79 79 31 34 2d 3e 61 29 20 29  minor.yy14->a) )
32111 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31   yygotominor.yy1
32112 34 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65  4->a[0].sortOrde
32113 72 20 3d 20 28 75 38 29 79 79 6d 73 70 5b 30 5d  r = (u8)yymsp[0]
32114 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 0a 7d 0a  .minor.yy328;.}.
32115 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32116 20 20 20 20 20 63 61 73 65 20 31 35 35 3a 20 2f       case 155: /
32117 2a 20 73 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20  * sortorder ::= 
32118 41 53 43 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ASC */.      cas
32119 65 20 31 35 37 3a 20 2f 2a 20 73 6f 72 74 6f 72  e 157: /* sortor
3211a 64 65 72 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73  der ::= */ yytes
3211b 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3211c 31 35 37 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e  157);.{yygotomin
3211d 6f 72 2e 79 79 33 32 38 20 3d 20 53 51 4c 49 54  or.yy328 = SQLIT
3211e 45 5f 53 4f 5f 41 53 43 3b 7d 0a 20 20 20 20 20  E_SO_ASC;}.     
3211f 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32120 63 61 73 65 20 31 35 36 3a 20 2f 2a 20 73 6f 72  case 156: /* sor
32121 74 6f 72 64 65 72 20 3a 3a 3d 20 44 45 53 43 20  torder ::= DESC 
32122 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
32123 79 79 33 32 38 20 3d 20 53 51 4c 49 54 45 5f 53  yy328 = SQLITE_S
32124 4f 5f 44 45 53 43 3b 7d 0a 20 20 20 20 20 20 20  O_DESC;}.       
32125 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32126 73 65 20 31 36 32 3a 20 2f 2a 20 6c 69 6d 69 74  se 162: /* limit
32127 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67  _opt ::= */.{yyg
32128 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 36 2e 70  otominor.yy476.p
32129 4c 69 6d 69 74 20 3d 20 30 3b 20 79 79 67 6f 74  Limit = 0; yygot
3212a 6f 6d 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4f 66  ominor.yy476.pOf
3212b 66 73 65 74 20 3d 20 30 3b 7d 0a 20 20 20 20 20  fset = 0;}.     
3212c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3212d 63 61 73 65 20 31 36 33 3a 20 2f 2a 20 6c 69 6d  case 163: /* lim
3212e 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54  it_opt ::= LIMIT
3212f 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f   expr */.{yygoto
32130 6d 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4c 69 6d  minor.yy476.pLim
32131 69 74 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  it = yymsp[0].mi
32132 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 3b  nor.yy346.pExpr;
32133 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
32134 37 36 2e 70 4f 66 66 73 65 74 20 3d 20 30 3b 7d  76.pOffset = 0;}
32135 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32136 20 20 20 20 20 20 63 61 73 65 20 31 36 34 3a 20        case 164: 
32137 2f 2a 20 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d  /* limit_opt ::=
32138 20 4c 49 4d 49 54 20 65 78 70 72 20 4f 46 46 53   LIMIT expr OFFS
32139 45 54 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f  ET expr */.{yygo
3213a 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 36 2e 70 4c  tominor.yy476.pL
3213b 69 6d 69 74 20 3d 20 79 79 6d 73 70 5b 2d 32 5d  imit = yymsp[-2]
3213c 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  .minor.yy346.pEx
3213d 70 72 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  pr; yygotominor.
3213e 79 79 34 37 36 2e 70 4f 66 66 73 65 74 20 3d 20  yy476.pOffset = 
3213f 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32140 79 33 34 36 2e 70 45 78 70 72 3b 7d 0a 20 20 20  y346.pExpr;}.   
32141 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32142 20 20 63 61 73 65 20 31 36 35 3a 20 2f 2a 20 6c    case 165: /* l
32143 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d  imit_opt ::= LIM
32144 49 54 20 65 78 70 72 20 43 4f 4d 4d 41 20 65 78  IT expr COMMA ex
32145 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  pr */.{yygotomin
32146 6f 72 2e 79 79 34 37 36 2e 70 4f 66 66 73 65 74  or.yy476.pOffset
32147 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e   = yymsp[-2].min
32148 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 3b 20  or.yy346.pExpr; 
32149 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37  yygotominor.yy47
3214a 36 2e 70 4c 69 6d 69 74 20 3d 20 79 79 6d 73 70  6.pLimit = yymsp
3214b 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  [0].minor.yy346.
3214c 70 45 78 70 72 3b 7d 0a 20 20 20 20 20 20 20 20  pExpr;}.        
3214d 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3214e 65 20 31 36 36 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 166: /* cmd ::
3214f 3d 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 66 75  = DELETE FROM fu
32150 6c 6c 6e 61 6d 65 20 69 6e 64 65 78 65 64 5f 6f  llname indexed_o
32151 70 74 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a  pt where_opt */.
32152 7b 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  {.  sqlite3SrcLi
32153 73 74 49 6e 64 65 78 65 64 42 79 28 70 50 61 72  stIndexedBy(pPar
32154 73 65 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  se, yymsp[-2].mi
32155 6e 6f 72 2e 79 79 36 35 2c 20 26 79 79 6d 73 70  nor.yy65, &yymsp
32156 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-1].minor.yy0);
32157 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  .  sqlite3Delete
32158 46 72 6f 6d 28 70 50 61 72 73 65 2c 79 79 6d 73  From(pParse,yyms
32159 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35  p[-2].minor.yy65
3215a 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  ,yymsp[0].minor.
3215b 79 79 31 33 32 29 3b 0a 7d 0a 20 20 20 20 20 20  yy132);.}.      
3215c 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3215d 61 73 65 20 31 36 39 3a 20 2f 2a 20 63 6d 64 20  ase 169: /* cmd 
3215e 3a 3a 3d 20 55 50 44 41 54 45 20 6f 72 63 6f 6e  ::= UPDATE orcon
3215f 66 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 64 65 78  f fullname index
32160 65 64 5f 6f 70 74 20 53 45 54 20 73 65 74 6c 69  ed_opt SET setli
32161 73 74 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a  st where_opt */.
32162 7b 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69  {.  sqlite3SrcLi
32163 73 74 49 6e 64 65 78 65 64 42 79 28 70 50 61 72  stIndexedBy(pPar
32164 73 65 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  se, yymsp[-4].mi
32165 6e 6f 72 2e 79 79 36 35 2c 20 26 79 79 6d 73 70  nor.yy65, &yymsp
32166 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-3].minor.yy0);
32167 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  .  sqlite3ExprLi
32168 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 70 50  stCheckLength(pP
32169 61 72 73 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d  arse,yymsp[-1].m
3216a 69 6e 6f 72 2e 79 79 31 34 2c 22 73 65 74 20 6c  inor.yy14,"set l
3216b 69 73 74 22 29 3b 20 0a 20 20 73 71 6c 69 74 65  ist"); .  sqlite
3216c 33 55 70 64 61 74 65 28 70 50 61 72 73 65 2c 79  3Update(pParse,y
3216d 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
3216e 79 36 35 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  y65,yymsp[-1].mi
3216f 6e 6f 72 2e 79 79 31 34 2c 79 79 6d 73 70 5b 30  nor.yy14,yymsp[0
32170 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c 79 79  ].minor.yy132,yy
32171 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-5].minor.yy
32172 31 38 36 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  186);.}.        
32173 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32174 65 20 31 37 30 3a 20 2f 2a 20 73 65 74 6c 69 73  e 170: /* setlis
32175 74 20 3a 3a 3d 20 73 65 74 6c 69 73 74 20 43 4f  t ::= setlist CO
32176 4d 4d 41 20 6e 6d 20 45 51 20 65 78 70 72 20 2a  MMA nm EQ expr *
32177 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
32178 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65 33  r.yy14 = sqlite3
32179 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
3217a 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 34 5d  Parse, yymsp[-4]
3217b 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79 79 6d  .minor.yy14, yym
3217c 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  sp[0].minor.yy34
3217d 36 2e 70 45 78 70 72 29 3b 0a 20 20 73 71 6c 69  6.pExpr);.  sqli
3217e 74 65 33 45 78 70 72 4c 69 73 74 53 65 74 4e 61  te3ExprListSetNa
3217f 6d 65 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74  me(pParse, yygot
32180 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26 79 79  ominor.yy14, &yy
32181 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
32182 30 2c 20 31 29 3b 0a 7d 0a 20 20 20 20 20 20 20  0, 1);.}.       
32183 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32184 73 65 20 31 37 31 3a 20 2f 2a 20 73 65 74 6c 69  se 171: /* setli
32185 73 74 20 3a 3a 3d 20 6e 6d 20 45 51 20 65 78 70  st ::= nm EQ exp
32186 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  r */.{.  yygotom
32187 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69  inor.yy14 = sqli
32188 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
32189 64 28 70 50 61 72 73 65 2c 20 30 2c 20 79 79 6d  d(pParse, 0, yym
3218a 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  sp[0].minor.yy34
3218b 36 2e 70 45 78 70 72 29 3b 0a 20 20 73 71 6c 69  6.pExpr);.  sqli
3218c 74 65 33 45 78 70 72 4c 69 73 74 53 65 74 4e 61  te3ExprListSetNa
3218d 6d 65 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74  me(pParse, yygot
3218e 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 26 79 79  ominor.yy14, &yy
3218f 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
32190 30 2c 20 31 29 3b 0a 7d 0a 20 20 20 20 20 20 20  0, 1);.}.       
32191 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32192 73 65 20 31 37 32 3a 20 2f 2a 20 63 6d 64 20 3a  se 172: /* cmd :
32193 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e  := insert_cmd IN
32194 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63  TO fullname insc
32195 6f 6c 6c 69 73 74 5f 6f 70 74 20 56 41 4c 55 45  ollist_opt VALUE
32196 53 20 4c 50 20 69 74 65 6d 6c 69 73 74 20 52 50  S LP itemlist RP
32197 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 49 6e 73 65   */.{sqlite3Inse
32198 72 74 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  rt(pParse, yymsp
32199 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c  [-5].minor.yy65,
3219a 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
3219b 2e 79 79 31 34 2c 20 30 2c 20 79 79 6d 73 70 5b  .yy14, 0, yymsp[
3219c 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 2c  -4].minor.yy408,
3219d 20 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72   yymsp[-7].minor
3219e 2e 79 79 31 38 36 29 3b 7d 0a 20 20 20 20 20 20  .yy186);}.      
3219f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
321a0 61 73 65 20 31 37 33 3a 20 2f 2a 20 63 6d 64 20  ase 173: /* cmd 
321a1 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49  ::= insert_cmd I
321a2 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73  NTO fullname ins
321a3 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73 65 6c 65  collist_opt sele
321a4 63 74 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 49 6e  ct */.{sqlite3In
321a5 73 65 72 74 28 70 50 61 72 73 65 2c 20 79 79 6d  sert(pParse, yym
321a6 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 36  sp[-2].minor.yy6
321a7 35 2c 20 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  5, 0, yymsp[0].m
321a8 69 6e 6f 72 2e 79 79 33 2c 20 79 79 6d 73 70 5b  inor.yy3, yymsp[
321a9 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 2c  -1].minor.yy408,
321aa 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72   yymsp[-4].minor
321ab 2e 79 79 31 38 36 29 3b 7d 0a 20 20 20 20 20 20  .yy186);}.      
321ac 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
321ad 61 73 65 20 31 37 34 3a 20 2f 2a 20 63 6d 64 20  ase 174: /* cmd 
321ae 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49  ::= insert_cmd I
321af 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73  NTO fullname ins
321b0 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 44 45 46 41  collist_opt DEFA
321b1 55 4c 54 20 56 41 4c 55 45 53 20 2a 2f 0a 7b 73  ULT VALUES */.{s
321b2 71 6c 69 74 65 33 49 6e 73 65 72 74 28 70 50 61  qlite3Insert(pPa
321b3 72 73 65 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d  rse, yymsp[-3].m
321b4 69 6e 6f 72 2e 79 79 36 35 2c 20 30 2c 20 30 2c  inor.yy65, 0, 0,
321b5 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
321b6 2e 79 79 34 30 38 2c 20 79 79 6d 73 70 5b 2d 35  .yy408, yymsp[-5
321b7 5d 2e 6d 69 6e 6f 72 2e 79 79 31 38 36 29 3b 7d  ].minor.yy186);}
321b8 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
321b9 20 20 20 20 20 20 63 61 73 65 20 31 37 35 3a 20        case 175: 
321ba 2f 2a 20 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a  /* insert_cmd ::
321bb 3d 20 49 4e 53 45 52 54 20 6f 72 63 6f 6e 66 20  = INSERT orconf 
321bc 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
321bd 79 79 31 38 36 20 3d 20 79 79 6d 73 70 5b 30 5d  yy186 = yymsp[0]
321be 2e 6d 69 6e 6f 72 2e 79 79 31 38 36 3b 7d 0a 20  .minor.yy186;}. 
321bf 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
321c0 20 20 20 20 63 61 73 65 20 31 37 36 3a 20 2f 2a      case 176: /*
321c1 20 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20   insert_cmd ::= 
321c2 52 45 50 4c 41 43 45 20 2a 2f 0a 7b 79 79 67 6f  REPLACE */.{yygo
321c3 74 6f 6d 69 6e 6f 72 2e 79 79 31 38 36 20 3d 20  tominor.yy186 = 
321c4 4f 45 5f 52 65 70 6c 61 63 65 3b 7d 0a 20 20 20  OE_Replace;}.   
321c5 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
321c6 20 20 63 61 73 65 20 31 37 37 3a 20 2f 2a 20 69    case 177: /* i
321c7 74 65 6d 6c 69 73 74 20 3a 3a 3d 20 69 74 65 6d  temlist ::= item
321c8 6c 69 73 74 20 43 4f 4d 4d 41 20 65 78 70 72 20  list COMMA expr 
321c9 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 34  */.      case 24
321ca 31 3a 20 2f 2a 20 6e 65 78 70 72 6c 69 73 74 20  1: /* nexprlist 
321cb 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74 20 43 4f  ::= nexprlist CO
321cc 4d 4d 41 20 65 78 70 72 20 2a 2f 20 79 79 74 65  MMA expr */ yyte
321cd 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
321ce 3d 32 34 31 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69  =241);.{yygotomi
321cf 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74  nor.yy14 = sqlit
321d0 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
321d1 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 32  (pParse,yymsp[-2
321d2 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 79 79 6d  ].minor.yy14,yym
321d3 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34  sp[0].minor.yy34
321d4 36 2e 70 45 78 70 72 29 3b 7d 0a 20 20 20 20 20  6.pExpr);}.     
321d5 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
321d6 63 61 73 65 20 31 37 38 3a 20 2f 2a 20 69 74 65  case 178: /* ite
321d7 6d 6c 69 73 74 20 3a 3a 3d 20 65 78 70 72 20 2a  mlist ::= expr *
321d8 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 32  /.      case 242
321d9 3a 20 2f 2a 20 6e 65 78 70 72 6c 69 73 74 20 3a  : /* nexprlist :
321da 3a 3d 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73  := expr */ yytes
321db 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
321dc 32 34 32 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e  242);.{yygotomin
321dd 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69 74 65  or.yy14 = sqlite
321de 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
321df 70 50 61 72 73 65 2c 30 2c 79 79 6d 73 70 5b 30  pParse,0,yymsp[0
321e0 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  ].minor.yy346.pE
321e1 78 70 72 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  xpr);}.        b
321e2 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
321e3 20 31 38 31 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c   181: /* inscoll
321e4 69 73 74 20 3a 3a 3d 20 69 6e 73 63 6f 6c 6c 69  ist ::= inscolli
321e5 73 74 20 43 4f 4d 4d 41 20 6e 6d 20 2a 2f 0a 7b  st COMMA nm */.{
321e6 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 30  yygotominor.yy40
321e7 38 20 3d 20 73 71 6c 69 74 65 33 49 64 4c 69 73  8 = sqlite3IdLis
321e8 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e  tAppend(pParse->
321e9 64 62 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  db,yymsp[-2].min
321ea 6f 72 2e 79 79 34 30 38 2c 26 79 79 6d 73 70 5b  or.yy408,&yymsp[
321eb 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a  0].minor.yy0);}.
321ec 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
321ed 20 20 20 20 20 63 61 73 65 20 31 38 32 3a 20 2f       case 182: /
321ee 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 20 3a 3a 3d  * inscollist ::=
321ef 20 6e 6d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69   nm */.{yygotomi
321f0 6e 6f 72 2e 79 79 34 30 38 20 3d 20 73 71 6c 69  nor.yy408 = sqli
321f1 74 65 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28  te3IdListAppend(
321f2 70 50 61 72 73 65 2d 3e 64 62 2c 30 2c 26 79 79  pParse->db,0,&yy
321f3 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
321f4 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
321f5 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 38  k;.      case 18
321f6 33 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 74  3: /* expr ::= t
321f7 65 72 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  erm */.      cas
321f8 65 20 32 31 31 3a 20 2f 2a 20 65 73 63 61 70 65  e 211: /* escape
321f9 20 3a 3a 3d 20 45 53 43 41 50 45 20 65 78 70 72   ::= ESCAPE expr
321fa 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79   */ yytestcase(y
321fb 79 72 75 6c 65 6e 6f 3d 3d 32 31 31 29 3b 0a 7b  yruleno==211);.{
321fc 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
321fd 36 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  6 = yymsp[0].min
321fe 6f 72 2e 79 79 33 34 36 3b 7d 0a 20 20 20 20 20  or.yy346;}.     
321ff 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32200 63 61 73 65 20 31 38 34 3a 20 2f 2a 20 65 78 70  case 184: /* exp
32201 72 20 3a 3a 3d 20 4c 50 20 65 78 70 72 20 52 50  r ::= LP expr RP
32202 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
32203 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 79  .yy346.pExpr = y
32204 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
32205 79 33 34 36 2e 70 45 78 70 72 3b 20 73 70 61 6e  y346.pExpr; span
32206 53 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72  Set(&yygotominor
32207 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b 2d 32  .yy346,&yymsp[-2
32208 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
32209 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
3220a 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3220b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 35  ;.      case 185
3220c 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 4e 55  : /* term ::= NU
3220d 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  LL */.      case
3220e 20 31 39 30 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a   190: /* term ::
3220f 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f 41 54 7c  = INTEGER|FLOAT|
32210 42 4c 4f 42 20 2a 2f 20 79 79 74 65 73 74 63 61  BLOB */ yytestca
32211 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 39 30  se(yyruleno==190
32212 29 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 39  );.      case 19
32213 31 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 53  1: /* term ::= S
32214 54 52 49 4e 47 20 2a 2f 20 79 79 74 65 73 74 63  TRING */ yytestc
32215 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 39  ase(yyruleno==19
32216 31 29 3b 0a 7b 73 70 61 6e 45 78 70 72 28 26 79  1);.{spanExpr(&y
32217 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
32218 2c 20 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  , pParse, yymsp[
32219 30 5d 2e 6d 61 6a 6f 72 2c 20 26 79 79 6d 73 70  0].major, &yymsp
3221a 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d  [0].minor.yy0);}
3221b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3221c 20 20 20 20 20 20 63 61 73 65 20 31 38 36 3a 20        case 186: 
3221d 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 69 64 20 2a  /* expr ::= id *
3221e 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 37  /.      case 187
3221f 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4a 4f  : /* expr ::= JO
32220 49 4e 5f 4b 57 20 2a 2f 20 79 79 74 65 73 74 63  IN_KW */ yytestc
32221 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 38  ase(yyruleno==18
32222 37 29 3b 0a 7b 73 70 61 6e 45 78 70 72 28 26 79  7);.{spanExpr(&y
32223 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
32224 2c 20 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c  , pParse, TK_ID,
32225 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
32226 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
32227 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
32228 65 20 31 38 38 3a 20 2f 2a 20 65 78 70 72 20 3a  e 188: /* expr :
32229 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 2a 2f 0a  := nm DOT nm */.
3222a 7b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70 31 20  {.  Expr *temp1 
3222b 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
3222c 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c  Parse, TK_ID, 0,
3222d 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d   0, &yymsp[-2].m
3222e 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78 70  inor.yy0);.  Exp
3222f 72 20 2a 74 65 6d 70 32 20 3d 20 73 71 6c 69 74  r *temp2 = sqlit
32230 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
32231 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79  TK_ID, 0, 0, &yy
32232 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
32233 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  );.  yygotominor
32234 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73  .yy346.pExpr = s
32235 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
32236 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 74 65 6d 70  se, TK_DOT, temp
32237 31 2c 20 74 65 6d 70 32 2c 20 30 29 3b 0a 20 20  1, temp2, 0);.  
32238 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d  spanSet(&yygotom
32239 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73  inor.yy346,&yyms
3223a 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-2].minor.yy0,
3223b 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
3223c 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy0);.}.        
3223d 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
3223e 65 20 31 38 39 3a 20 2f 2a 20 65 78 70 72 20 3a  e 189: /* expr :
3223f 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20 44 4f 54  := nm DOT nm DOT
32240 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20   nm */.{.  Expr 
32241 2a 74 65 6d 70 31 20 3d 20 73 71 6c 69 74 65 33  *temp1 = sqlite3
32242 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
32243 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73  _ID, 0, 0, &yyms
32244 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-4].minor.yy0)
32245 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70 32 20  ;.  Expr *temp2 
32246 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
32247 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c  Parse, TK_ID, 0,
32248 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d   0, &yymsp[-2].m
32249 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78 70  inor.yy0);.  Exp
3224a 72 20 2a 74 65 6d 70 33 20 3d 20 73 71 6c 69 74  r *temp3 = sqlit
3224b 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
3224c 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20 26 79 79  TK_ID, 0, 0, &yy
3224d 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
3224e 29 3b 0a 20 20 45 78 70 72 20 2a 74 65 6d 70 34  );.  Expr *temp4
3224f 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
32250 70 50 61 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20  pParse, TK_DOT, 
32251 74 65 6d 70 32 2c 20 74 65 6d 70 33 2c 20 30 29  temp2, temp3, 0)
32252 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
32253 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71  yy346.pExpr = sq
32254 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
32255 65 2c 20 54 4b 5f 44 4f 54 2c 20 74 65 6d 70 31  e, TK_DOT, temp1
32256 2c 20 74 65 6d 70 34 2c 20 30 29 3b 0a 20 20 73  , temp4, 0);.  s
32257 70 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d 69  panSet(&yygotomi
32258 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70  nor.yy346,&yymsp
32259 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-4].minor.yy0,&
3225a 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3225b 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  y0);.}.        b
3225c 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
3225d 20 31 39 32 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   192: /* expr ::
3225e 3d 20 52 45 47 49 53 54 45 52 20 2a 2f 0a 7b 0a  = REGISTER */.{.
3225f 20 20 2f 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20    /* When doing 
32260 61 20 6e 65 73 74 65 64 20 70 61 72 73 65 2c 20  a nested parse, 
32261 6f 6e 65 20 63 61 6e 20 69 6e 63 6c 75 64 65 20  one can include 
32262 74 65 72 6d 73 20 69 6e 20 61 6e 20 65 78 70 72  terms in an expr
32263 65 73 73 69 6f 6e 0a 20 20 2a 2a 20 74 68 61 74  ession.  ** that
32264 20 6c 6f 6f 6b 20 6c 69 6b 65 20 74 68 69 73 3a   look like this:
32265 20 20 20 23 31 20 23 32 20 2e 2e 2e 20 20 54 68     #1 #2 ...  Th
32266 65 73 65 20 74 65 72 6d 73 20 72 65 66 65 72 20  ese terms refer 
32267 74 6f 20 72 65 67 69 73 74 65 72 73 0a 20 20 2a  to registers.  *
32268 2a 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c  * in the virtual
32269 20 6d 61 63 68 69 6e 65 2e 20 20 23 4e 20 69 73   machine.  #N is
3226a 20 74 68 65 20 4e 2d 74 68 20 72 65 67 69 73 74   the N-th regist
3226b 65 72 2e 20 2a 2f 0a 20 20 69 66 28 20 70 50 61  er. */.  if( pPa
3226c 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29  rse->nested==0 )
3226d 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
3226e 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
3226f 65 61 72 20 5c 22 25 54 5c 22 3a 20 73 79 6e 74  ear \"%T\": synt
32270 61 78 20 65 72 72 6f 72 22 2c 20 26 79 79 6d 73  ax error", &yyms
32271 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
32272 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
32273 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 30  .yy346.pExpr = 0
32274 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 79  ;.  }else{.    y
32275 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
32276 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  .pExpr = sqlite3
32277 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
32278 5f 52 45 47 49 53 54 45 52 2c 20 30 2c 20 30 2c  _REGISTER, 0, 0,
32279 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72   &yymsp[0].minor
3227a 2e 79 79 30 29 3b 0a 20 20 20 20 69 66 28 20 79  .yy0);.    if( y
3227b 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
3227c 2e 70 45 78 70 72 20 29 20 73 71 6c 69 74 65 33  .pExpr ) sqlite3
3227d 47 65 74 49 6e 74 33 32 28 26 79 79 6d 73 70 5b  GetInt32(&yymsp[
3227e 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 31  0].minor.yy0.z[1
3227f 5d 2c 20 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ], &yygotominor.
32280 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 69 54 61  yy346.pExpr->iTa
32281 62 6c 65 29 3b 0a 20 20 7d 0a 20 20 73 70 61 6e  ble);.  }.  span
32282 53 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72  Set(&yygotominor
32283 2e 79 79 33 34 36 2c 20 26 79 79 6d 73 70 5b 30  .yy346, &yymsp[0
32284 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79  ].minor.yy0, &yy
32285 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
32286 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
32287 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
32288 39 33 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  93: /* expr ::= 
32289 56 41 52 49 41 42 4c 45 20 2a 2f 0a 7b 0a 20 20  VARIABLE */.{.  
3228a 73 70 61 6e 45 78 70 72 28 26 79 79 67 6f 74 6f  spanExpr(&yygoto
3228b 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 20 70 50 61  minor.yy346, pPa
3228c 72 73 65 2c 20 54 4b 5f 56 41 52 49 41 42 4c 45  rse, TK_VARIABLE
3228d 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
3228e 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69 74 65  r.yy0);.  sqlite
3228f 33 45 78 70 72 41 73 73 69 67 6e 56 61 72 4e 75  3ExprAssignVarNu
32290 6d 62 65 72 28 70 50 61 72 73 65 2c 20 79 79 67  mber(pParse, yyg
32291 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
32292 45 78 70 72 29 3b 0a 20 20 73 70 61 6e 53 65 74  Expr);.  spanSet
32293 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  (&yygotominor.yy
32294 33 34 36 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  346, &yymsp[0].m
32295 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70  inor.yy0, &yymsp
32296 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
32297 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32298 0a 20 20 20 20 20 20 63 61 73 65 20 31 39 34 3a  .      case 194:
32299 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70   /* expr ::= exp
3229a 72 20 43 4f 4c 4c 41 54 45 20 69 64 73 20 2a 2f  r COLLATE ids */
3229b 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
3229c 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73  .yy346.pExpr = s
3229d 71 6c 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c  qlite3ExprSetCol
3229e 6c 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b  l(pParse, yymsp[
3229f 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  -2].minor.yy346.
322a0 70 45 78 70 72 2c 20 26 79 79 6d 73 70 5b 30 5d  pExpr, &yymsp[0]
322a1 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 79  .minor.yy0);.  y
322a2 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
322a3 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b  .zStart = yymsp[
322a4 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  -2].minor.yy346.
322a5 7a 53 74 61 72 74 3b 0a 20 20 79 79 67 6f 74 6f  zStart;.  yygoto
322a6 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64  minor.yy346.zEnd
322a7 20 3d 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e   = &yymsp[0].min
322a8 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30  or.yy0.z[yymsp[0
322a9 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a  ].minor.yy0.n];.
322aa 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
322ab 0a 20 20 20 20 20 20 63 61 73 65 20 31 39 35 3a  .      case 195:
322ac 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 43 41 53   /* expr ::= CAS
322ad 54 20 4c 50 20 65 78 70 72 20 41 53 20 74 79 70  T LP expr AS typ
322ae 65 74 6f 6b 65 6e 20 52 50 20 2a 2f 0a 7b 0a 20  etoken RP */.{. 
322af 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
322b0 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74  46.pExpr = sqlit
322b1 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
322b2 54 4b 5f 43 41 53 54 2c 20 79 79 6d 73 70 5b 2d  TK_CAST, yymsp[-
322b3 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  3].minor.yy346.p
322b4 45 78 70 72 2c 20 30 2c 20 26 79 79 6d 73 70 5b  Expr, 0, &yymsp[
322b5 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  -1].minor.yy0);.
322b6 20 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74    spanSet(&yygot
322b7 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79 79  ominor.yy346,&yy
322b8 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-5].minor.yy
322b9 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
322ba 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20  r.yy0);.}.      
322bb 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
322bc 61 73 65 20 31 39 36 3a 20 2f 2a 20 65 78 70 72  ase 196: /* expr
322bd 20 3a 3a 3d 20 49 44 20 4c 50 20 64 69 73 74 69   ::= ID LP disti
322be 6e 63 74 20 65 78 70 72 6c 69 73 74 20 52 50 20  nct exprlist RP 
322bf 2a 2f 0a 7b 0a 20 20 69 66 28 20 79 79 6d 73 70  */.{.  if( yymsp
322c0 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 20  [-1].minor.yy14 
322c1 26 26 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  && yymsp[-1].min
322c2 6f 72 2e 79 79 31 34 2d 3e 6e 45 78 70 72 3e 70  or.yy14->nExpr>p
322c3 50 61 72 73 65 2d 3e 64 62 2d 3e 61 4c 69 6d 69  Parse->db->aLimi
322c4 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 46  t[SQLITE_LIMIT_F
322c5 55 4e 43 54 49 4f 4e 5f 41 52 47 5d 20 29 7b 0a  UNCTION_ARG] ){.
322c6 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
322c7 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f 6f  Msg(pParse, "too
322c8 20 6d 61 6e 79 20 61 72 67 75 6d 65 6e 74 73 20   many arguments 
322c9 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 25 54 22 2c  on function %T",
322ca 20 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f   &yymsp[-4].mino
322cb 72 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20 79 79  r.yy0);.  }.  yy
322cc 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
322cd 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45  pExpr = sqlite3E
322ce 78 70 72 46 75 6e 63 74 69 6f 6e 28 70 50 61 72  xprFunction(pPar
322cf 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  se, yymsp[-1].mi
322d0 6e 6f 72 2e 79 79 31 34 2c 20 26 79 79 6d 73 70  nor.yy14, &yymsp
322d1 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  [-4].minor.yy0);
322d2 0a 20 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f  .  spanSet(&yygo
322d3 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26 79  tominor.yy346,&y
322d4 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
322d5 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
322d6 6f 72 2e 79 79 30 29 3b 0a 20 20 69 66 28 20 79  or.yy0);.  if( y
322d7 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
322d8 79 33 32 38 20 26 26 20 79 79 67 6f 74 6f 6d 69  y328 && yygotomi
322d9 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20  nor.yy346.pExpr 
322da 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  ){.    yygotomin
322db 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e  or.yy346.pExpr->
322dc 66 6c 61 67 73 20 7c 3d 20 45 50 5f 44 69 73 74  flags |= EP_Dist
322dd 69 6e 63 74 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20  inct;.  }.}.    
322de 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
322df 20 63 61 73 65 20 31 39 37 3a 20 2f 2a 20 65 78   case 197: /* ex
322e0 70 72 20 3a 3a 3d 20 49 44 20 4c 50 20 53 54 41  pr ::= ID LP STA
322e1 52 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f  R RP */.{.  yygo
322e2 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
322e3 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70  xpr = sqlite3Exp
322e4 72 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65  rFunction(pParse
322e5 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e  , 0, &yymsp[-3].
322e6 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 70  minor.yy0);.  sp
322e7 61 6e 53 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e  anSet(&yygotomin
322e8 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b  or.yy346,&yymsp[
322e9 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -3].minor.yy0,&y
322ea 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
322eb 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
322ec 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
322ed 31 39 38 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d  198: /* term ::=
322ee 20 43 54 49 4d 45 5f 4b 57 20 2a 2f 0a 7b 0a 20   CTIME_KW */.{. 
322ef 20 2f 2a 20 54 68 65 20 43 55 52 52 45 4e 54 5f   /* The CURRENT_
322f0 54 49 4d 45 2c 20 43 55 52 52 45 4e 54 5f 44 41  TIME, CURRENT_DA
322f1 54 45 2c 20 61 6e 64 20 43 55 52 52 45 4e 54 5f  TE, and CURRENT_
322f2 54 49 4d 45 53 54 41 4d 50 20 76 61 6c 75 65 73  TIMESTAMP values
322f3 20 61 72 65 0a 20 20 2a 2a 20 74 72 65 61 74 65   are.  ** treate
322f4 64 20 61 73 20 66 75 6e 63 74 69 6f 6e 73 20 74  d as functions t
322f5 68 61 74 20 72 65 74 75 72 6e 20 63 6f 6e 73 74  hat return const
322f6 61 6e 74 73 20 2a 2f 0a 20 20 79 79 67 6f 74 6f  ants */.  yygoto
322f7 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
322f8 72 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 46  r = sqlite3ExprF
322f9 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  unction(pParse, 
322fa 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  0,&yymsp[0].mino
322fb 72 2e 79 79 30 29 3b 0a 20 20 69 66 28 20 79 79  r.yy0);.  if( yy
322fc 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
322fd 70 45 78 70 72 20 29 7b 0a 20 20 20 20 79 79 67  pExpr ){.    yyg
322fe 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
322ff 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f  Expr->op = TK_CO
32300 4e 53 54 5f 46 55 4e 43 3b 20 20 0a 20 20 7d 0a  NST_FUNC;  .  }.
32301 20 20 73 70 61 6e 53 65 74 28 26 79 79 67 6f 74    spanSet(&yygot
32302 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 20 26 79  ominor.yy346, &y
32303 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
32304 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0, &yymsp[0].min
32305 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20  or.yy0);.}.     
32306 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32307 63 61 73 65 20 31 39 39 3a 20 2f 2a 20 65 78 70  case 199: /* exp
32308 72 20 3a 3a 3d 20 65 78 70 72 20 41 4e 44 20 65  r ::= expr AND e
32309 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  xpr */.      cas
3230a 65 20 32 30 30 3a 20 2f 2a 20 65 78 70 72 20 3a  e 200: /* expr :
3230b 3a 3d 20 65 78 70 72 20 4f 52 20 65 78 70 72 20  := expr OR expr 
3230c 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
3230d 72 75 6c 65 6e 6f 3d 3d 32 30 30 29 3b 0a 20 20  ruleno==200);.  
3230e 20 20 20 20 63 61 73 65 20 32 30 31 3a 20 2f 2a      case 201: /*
3230f 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 4c   expr ::= expr L
32310 54 7c 47 54 7c 47 45 7c 4c 45 20 65 78 70 72 20  T|GT|GE|LE expr 
32311 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
32312 72 75 6c 65 6e 6f 3d 3d 32 30 31 29 3b 0a 20 20  ruleno==201);.  
32313 20 20 20 20 63 61 73 65 20 32 30 32 3a 20 2f 2a      case 202: /*
32314 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 45   expr ::= expr E
32315 51 7c 4e 45 20 65 78 70 72 20 2a 2f 20 79 79 74  Q|NE expr */ yyt
32316 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
32317 3d 3d 32 30 32 29 3b 0a 20 20 20 20 20 20 63 61  ==202);.      ca
32318 73 65 20 32 30 33 3a 20 2f 2a 20 65 78 70 72 20  se 203: /* expr 
32319 3a 3a 3d 20 65 78 70 72 20 42 49 54 41 4e 44 7c  ::= expr BITAND|
3231a 42 49 54 4f 52 7c 4c 53 48 49 46 54 7c 52 53 48  BITOR|LSHIFT|RSH
3231b 49 46 54 20 65 78 70 72 20 2a 2f 20 79 79 74 65  IFT expr */ yyte
3231c 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
3231d 3d 32 30 33 29 3b 0a 20 20 20 20 20 20 63 61 73  =203);.      cas
3231e 65 20 32 30 34 3a 20 2f 2a 20 65 78 70 72 20 3a  e 204: /* expr :
3231f 3a 3d 20 65 78 70 72 20 50 4c 55 53 7c 4d 49 4e  := expr PLUS|MIN
32320 55 53 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73  US expr */ yytes
32321 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
32322 32 30 34 29 3b 0a 20 20 20 20 20 20 63 61 73 65  204);.      case
32323 20 32 30 35 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   205: /* expr ::
32324 3d 20 65 78 70 72 20 53 54 41 52 7c 53 4c 41 53  = expr STAR|SLAS
32325 48 7c 52 45 4d 20 65 78 70 72 20 2a 2f 20 79 79  H|REM expr */ yy
32326 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
32327 6f 3d 3d 32 30 35 29 3b 0a 20 20 20 20 20 20 63  o==205);.      c
32328 61 73 65 20 32 30 36 3a 20 2f 2a 20 65 78 70 72  ase 206: /* expr
32329 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4e 43 41 54   ::= expr CONCAT
3232a 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63   expr */ yytestc
3232b 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 30  ase(yyruleno==20
3232c 36 29 3b 0a 7b 73 70 61 6e 42 69 6e 61 72 79 45  6);.{spanBinaryE
3232d 78 70 72 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72  xpr(&yygotominor
3232e 2e 79 79 33 34 36 2c 70 50 61 72 73 65 2c 79 79  .yy346,pParse,yy
3232f 6d 73 70 5b 2d 31 5d 2e 6d 61 6a 6f 72 2c 26 79  msp[-1].major,&y
32330 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
32331 79 33 34 36 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  y346,&yymsp[0].m
32332 69 6e 6f 72 2e 79 79 33 34 36 29 3b 7d 0a 20 20  inor.yy346);}.  
32333 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32334 20 20 20 63 61 73 65 20 32 30 37 3a 20 2f 2a 20     case 207: /* 
32335 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4c 49 4b 45 5f  likeop ::= LIKE_
32336 4b 57 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  KW */.      case
32337 20 32 30 39 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20   209: /* likeop 
32338 3a 3a 3d 20 4d 41 54 43 48 20 2a 2f 20 79 79 74  ::= MATCH */ yyt
32339 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
3233a 3d 3d 32 30 39 29 3b 0a 7b 79 79 67 6f 74 6f 6d  ==209);.{yygotom
3233b 69 6e 6f 72 2e 79 79 39 36 2e 65 4f 70 65 72 61  inor.yy96.eOpera
3233c 74 6f 72 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  tor = yymsp[0].m
3233d 69 6e 6f 72 2e 79 79 30 3b 20 79 79 67 6f 74 6f  inor.yy0; yygoto
3233e 6d 69 6e 6f 72 2e 79 79 39 36 2e 6e 6f 74 20 3d  minor.yy96.not =
3233f 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65   0;}.        bre
32340 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
32341 30 38 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a  08: /* likeop ::
32342 3d 20 4e 4f 54 20 4c 49 4b 45 5f 4b 57 20 2a 2f  = NOT LIKE_KW */
32343 0a 20 20 20 20 20 20 63 61 73 65 20 32 31 30 3a  .      case 210:
32344 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e   /* likeop ::= N
32345 4f 54 20 4d 41 54 43 48 20 2a 2f 20 79 79 74 65  OT MATCH */ yyte
32346 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
32347 3d 32 31 30 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69  =210);.{yygotomi
32348 6e 6f 72 2e 79 79 39 36 2e 65 4f 70 65 72 61 74  nor.yy96.eOperat
32349 6f 72 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  or = yymsp[0].mi
3234a 6e 6f 72 2e 79 79 30 3b 20 79 79 67 6f 74 6f 6d  nor.yy0; yygotom
3234b 69 6e 6f 72 2e 79 79 39 36 2e 6e 6f 74 20 3d 20  inor.yy96.not = 
3234c 31 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  1;}.        brea
3234d 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 31  k;.      case 21
3234e 32 3a 20 2f 2a 20 65 73 63 61 70 65 20 3a 3a 3d  2: /* escape ::=
3234f 20 2a 2f 0a 7b 6d 65 6d 73 65 74 28 26 79 79 67   */.{memset(&yyg
32350 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 30  otominor.yy346,0
32351 2c 73 69 7a 65 6f 66 28 79 79 67 6f 74 6f 6d 69  ,sizeof(yygotomi
32352 6e 6f 72 2e 79 79 33 34 36 29 29 3b 7d 0a 20 20  nor.yy346));}.  
32353 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32354 20 20 20 63 61 73 65 20 32 31 33 3a 20 2f 2a 20     case 213: /* 
32355 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 6c 69  expr ::= expr li
32356 6b 65 6f 70 20 65 78 70 72 20 65 73 63 61 70 65  keop expr escape
32357 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 4c 69 73 74   */.{.  ExprList
32358 20 2a 70 4c 69 73 74 3b 0a 20 20 70 4c 69 73 74   *pList;.  pList
32359 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
3235a 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
3235b 30 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  0, yymsp[-1].min
3235c 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b  or.yy346.pExpr);
3235d 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74  .  pList = sqlit
3235e 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
3235f 28 70 50 61 72 73 65 2c 70 4c 69 73 74 2c 20 79  (pParse,pList, y
32360 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
32361 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 69  y346.pExpr);.  i
32362 66 28 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  f( yymsp[0].mino
32363 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29 7b  r.yy346.pExpr ){
32364 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c  .    pList = sql
32365 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
32366 6e 64 28 70 50 61 72 73 65 2c 70 4c 69 73 74 2c  nd(pParse,pList,
32367 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
32368 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20  yy346.pExpr);.  
32369 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  }.  yygotominor.
3236a 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71  yy346.pExpr = sq
3236b 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f  lite3ExprFunctio
3236c 6e 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c  n(pParse, pList,
3236d 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f   &yymsp[-2].mino
3236e 72 2e 79 79 39 36 2e 65 4f 70 65 72 61 74 6f 72  r.yy96.eOperator
3236f 29 3b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d  );.  if( yymsp[-
32370 32 5d 2e 6d 69 6e 6f 72 2e 79 79 39 36 2e 6e 6f  2].minor.yy96.no
32371 74 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  t ) yygotominor.
32372 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20 73 71  yy346.pExpr = sq
32373 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
32374 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74  e, TK_NOT, yygot
32375 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
32376 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 79 79 67  pr, 0, 0);.  yyg
32377 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a  otominor.yy346.z
32378 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 33  Start = yymsp[-3
32379 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53  ].minor.yy346.zS
3237a 74 61 72 74 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  tart;.  yygotomi
3237b 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64 20 3d  nor.yy346.zEnd =
3237c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
3237d 2e 79 79 33 34 36 2e 7a 45 6e 64 3b 0a 20 20 69  .yy346.zEnd;.  i
3237e 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
3237f 79 33 34 36 2e 70 45 78 70 72 20 29 20 79 79 67  y346.pExpr ) yyg
32380 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
32381 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c 3d 20 45  Expr->flags |= E
32382 50 5f 49 6e 66 69 78 46 75 6e 63 3b 0a 7d 0a 20  P_InfixFunc;.}. 
32383 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32384 20 20 20 20 63 61 73 65 20 32 31 34 3a 20 2f 2a      case 214: /*
32385 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49   expr ::= expr I
32386 53 4e 55 4c 4c 7c 4e 4f 54 4e 55 4c 4c 20 2a 2f  SNULL|NOTNULL */
32387 0a 7b 73 70 61 6e 55 6e 61 72 79 50 6f 73 74 66  .{spanUnaryPostf
32388 69 78 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ix(&yygotominor.
32389 79 79 33 34 36 2c 70 50 61 72 73 65 2c 79 79 6d  yy346,pParse,yym
3238a 73 70 5b 30 5d 2e 6d 61 6a 6f 72 2c 26 79 79 6d  sp[0].major,&yym
3238b 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
3238c 34 36 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  46,&yymsp[0].min
3238d 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
3238e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3238f 61 73 65 20 32 31 35 3a 20 2f 2a 20 65 78 70 72  ase 215: /* expr
32390 20 3a 3a 3d 20 65 78 70 72 20 4e 4f 54 20 4e 55   ::= expr NOT NU
32391 4c 4c 20 2a 2f 0a 7b 73 70 61 6e 55 6e 61 72 79  LL */.{spanUnary
32392 50 6f 73 74 66 69 78 28 26 79 79 67 6f 74 6f 6d  Postfix(&yygotom
32393 69 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61 72 73  inor.yy346,pPars
32394 65 2c 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 26 79 79  e,TK_NOTNULL,&yy
32395 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
32396 33 34 36 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  346,&yymsp[0].mi
32397 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20  nor.yy0);}.     
32398 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
32399 63 61 73 65 20 32 31 36 3a 20 2f 2a 20 65 78 70  case 216: /* exp
3239a 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20 65 78  r ::= expr IS ex
3239b 70 72 20 2a 2f 0a 7b 0a 20 20 73 70 61 6e 42 69  pr */.{.  spanBi
3239c 6e 61 72 79 45 78 70 72 28 26 79 79 67 6f 74 6f  naryExpr(&yygoto
3239d 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 70 50 61 72  minor.yy346,pPar
3239e 73 65 2c 54 4b 5f 49 53 2c 26 79 79 6d 73 70 5b  se,TK_IS,&yymsp[
3239f 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2c  -2].minor.yy346,
323a0 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
323a1 79 79 33 34 36 29 3b 0a 20 20 69 66 28 20 70 50  yy346);.  if( pP
323a2 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  arse->db->malloc
323a3 46 61 69 6c 65 64 3d 3d 30 20 20 26 26 20 79 79  Failed==0  && yy
323a4 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
323a5 34 36 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  46.pExpr->op==TK
323a6 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 79 79 67  _NULL ){.    yyg
323a7 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
323a8 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 49 53  Expr->op = TK_IS
323a9 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20  NULL;.  }.}.    
323aa 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
323ab 20 63 61 73 65 20 32 31 37 3a 20 2f 2a 20 65 78   case 217: /* ex
323ac 70 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20 4e  pr ::= expr IS N
323ad 4f 54 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 73  OT expr */.{.  s
323ae 70 61 6e 42 69 6e 61 72 79 45 78 70 72 28 26 79  panBinaryExpr(&y
323af 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
323b0 2c 70 50 61 72 73 65 2c 54 4b 5f 49 53 4e 4f 54  ,pParse,TK_ISNOT
323b1 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  ,&yymsp[-3].mino
323b2 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b 30  r.yy346,&yymsp[0
323b3 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 29 3b 0a  ].minor.yy346);.
323b4 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62    if( pParse->db
323b5 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d  ->mallocFailed==
323b6 30 20 20 26 26 20 79 79 6d 73 70 5b 30 5d 2e 6d  0  && yymsp[0].m
323b7 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
323b8 2d 3e 6f 70 3d 3d 54 4b 5f 4e 55 4c 4c 20 29 7b  ->op==TK_NULL ){
323b9 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
323ba 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e 6f 70  .yy346.pExpr->op
323bb 20 3d 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3b 0a 20   = TK_NOTNULL;. 
323bc 20 7d 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65   }.}.        bre
323bd 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
323be 31 38 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  18: /* expr ::= 
323bf 4e 4f 54 20 65 78 70 72 20 2a 2f 0a 20 20 20 20  NOT expr */.    
323c0 20 20 63 61 73 65 20 32 31 39 3a 20 2f 2a 20 65    case 219: /* e
323c1 78 70 72 20 3a 3a 3d 20 42 49 54 4e 4f 54 20 65  xpr ::= BITNOT e
323c2 78 70 72 20 2a 2f 20 79 79 74 65 73 74 63 61 73  xpr */ yytestcas
323c3 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 31 39 29  e(yyruleno==219)
323c4 3b 0a 7b 73 70 61 6e 55 6e 61 72 79 50 72 65 66  ;.{spanUnaryPref
323c5 69 78 28 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ix(&yygotominor.
323c6 79 79 33 34 36 2c 70 50 61 72 73 65 2c 79 79 6d  yy346,pParse,yym
323c7 73 70 5b 2d 31 5d 2e 6d 61 6a 6f 72 2c 26 79 79  sp[-1].major,&yy
323c8 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
323c9 34 36 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  46,&yymsp[-1].mi
323ca 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20  nor.yy0);}.     
323cb 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
323cc 63 61 73 65 20 32 32 30 3a 20 2f 2a 20 65 78 70  case 220: /* exp
323cd 72 20 3a 3a 3d 20 4d 49 4e 55 53 20 65 78 70 72  r ::= MINUS expr
323ce 20 2a 2f 0a 7b 73 70 61 6e 55 6e 61 72 79 50 72   */.{spanUnaryPr
323cf 65 66 69 78 28 26 79 79 67 6f 74 6f 6d 69 6e 6f  efix(&yygotomino
323d0 72 2e 79 79 33 34 36 2c 70 50 61 72 73 65 2c 54  r.yy346,pParse,T
323d1 4b 5f 55 4d 49 4e 55 53 2c 26 79 79 6d 73 70 5b  K_UMINUS,&yymsp[
323d2 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2c 26  0].minor.yy346,&
323d3 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
323d4 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  yy0);}.        b
323d5 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
323d6 20 32 32 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   221: /* expr ::
323d7 3d 20 50 4c 55 53 20 65 78 70 72 20 2a 2f 0a 7b  = PLUS expr */.{
323d8 73 70 61 6e 55 6e 61 72 79 50 72 65 66 69 78 28  spanUnaryPrefix(
323d9 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  &yygotominor.yy3
323da 34 36 2c 70 50 61 72 73 65 2c 54 4b 5f 55 50 4c  46,pParse,TK_UPL
323db 55 53 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  US,&yymsp[0].min
323dc 6f 72 2e 79 79 33 34 36 2c 26 79 79 6d 73 70 5b  or.yy346,&yymsp[
323dd 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d  -1].minor.yy0);}
323de 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
323df 20 20 20 20 20 20 63 61 73 65 20 32 32 34 3a 20        case 224: 
323e0 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
323e1 20 62 65 74 77 65 65 6e 5f 6f 70 20 65 78 70 72   between_op expr
323e2 20 41 4e 44 20 65 78 70 72 20 2a 2f 0a 7b 0a 20   AND expr */.{. 
323e3 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
323e4 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
323e5 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
323e6 30 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  0, yymsp[-2].min
323e7 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b  or.yy346.pExpr);
323e8 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74  .  pList = sqlit
323e9 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64  e3ExprListAppend
323ea 28 70 50 61 72 73 65 2c 70 4c 69 73 74 2c 20 79  (pParse,pList, y
323eb 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
323ec 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 79 79  346.pExpr);.  yy
323ed 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
323ee 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50  pExpr = sqlite3P
323ef 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
323f0 42 45 54 57 45 45 4e 2c 20 79 79 6d 73 70 5b 2d  BETWEEN, yymsp[-
323f1 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  4].minor.yy346.p
323f2 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 69  Expr, 0, 0);.  i
323f3 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
323f4 79 33 34 36 2e 70 45 78 70 72 20 29 7b 0a 20 20  y346.pExpr ){.  
323f5 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
323f6 33 34 36 2e 70 45 78 70 72 2d 3e 78 2e 70 4c 69  346.pExpr->x.pLi
323f7 73 74 20 3d 20 70 4c 69 73 74 3b 0a 20 20 7d 65  st = pList;.  }e
323f8 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
323f9 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70  ExprListDelete(p
323fa 50 61 72 73 65 2d 3e 64 62 2c 20 70 4c 69 73 74  Parse->db, pList
323fb 29 3b 0a 20 20 7d 20 0a 20 20 69 66 28 20 79 79  );.  } .  if( yy
323fc 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
323fd 33 32 38 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  328 ) yygotomino
323fe 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20  r.yy346.pExpr = 
323ff 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
32400 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67  rse, TK_NOT, yyg
32401 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
32402 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 79  Expr, 0, 0);.  y
32403 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
32404 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b  .zStart = yymsp[
32405 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  -4].minor.yy346.
32406 7a 53 74 61 72 74 3b 0a 20 20 79 79 67 6f 74 6f  zStart;.  yygoto
32407 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64  minor.yy346.zEnd
32408 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
32409 72 2e 79 79 33 34 36 2e 7a 45 6e 64 3b 0a 7d 0a  r.yy346.zEnd;.}.
3240a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3240b 20 20 20 20 20 63 61 73 65 20 32 32 37 3a 20 2f       case 227: /
3240c 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20  * expr ::= expr 
3240d 69 6e 5f 6f 70 20 4c 50 20 65 78 70 72 6c 69 73  in_op LP exprlis
3240e 74 20 52 50 20 2a 2f 0a 7b 0a 20 20 20 20 79 79  t RP */.{.    yy
3240f 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
32410 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50  pExpr = sqlite3P
32411 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
32412 49 4e 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  IN, yymsp[-4].mi
32413 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c  nor.yy346.pExpr,
32414 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20   0, 0);.    if( 
32415 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
32416 36 2e 70 45 78 70 72 20 29 7b 0a 20 20 20 20 20  6.pExpr ){.     
32417 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
32418 34 36 2e 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73  46.pExpr->x.pLis
32419 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  t = yymsp[-1].mi
3241a 6e 6f 72 2e 79 79 31 34 3b 0a 20 20 20 20 20 20  nor.yy14;.      
3241b 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65  sqlite3ExprSetHe
3241c 69 67 68 74 28 70 50 61 72 73 65 2c 20 79 79 67  ight(pParse, yyg
3241d 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70  otominor.yy346.p
3241e 45 78 70 72 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Expr);.    }else
3241f 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
32420 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50  xprListDelete(pP
32421 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d 73 70 5b  arse->db, yymsp[
32422 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 29 3b  -1].minor.yy14);
32423 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 79  .    }.    if( y
32424 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
32425 79 33 32 38 20 29 20 79 79 67 6f 74 6f 6d 69 6e  y328 ) yygotomin
32426 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d  or.yy346.pExpr =
32427 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
32428 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79  arse, TK_NOT, yy
32429 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
3242a 70 45 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20  pExpr, 0, 0);.  
3242b 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3242c 33 34 36 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d  346.zStart = yym
3242d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-4].minor.yy3
3242e 34 36 2e 7a 53 74 61 72 74 3b 0a 20 20 20 20 79  46.zStart;.    y
3242f 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
32430 2e 7a 45 6e 64 20 3d 20 26 79 79 6d 73 70 5b 30  .zEnd = &yymsp[0
32431 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79  ].minor.yy0.z[yy
32432 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
32433 2e 6e 5d 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20  .n];.  }.       
32434 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32435 73 65 20 32 32 38 3a 20 2f 2a 20 65 78 70 72 20  se 228: /* expr 
32436 3a 3a 3d 20 4c 50 20 73 65 6c 65 63 74 20 52 50  ::= LP select RP
32437 20 2a 2f 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f   */.{.    yygoto
32438 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
32439 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  r = sqlite3PExpr
3243a 28 70 50 61 72 73 65 2c 20 54 4b 5f 53 45 4c 45  (pParse, TK_SELE
3243b 43 54 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  CT, 0, 0, 0);.  
3243c 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
3243d 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 29 7b  r.yy346.pExpr ){
3243e 0a 20 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  .      yygotomin
3243f 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2d 3e  or.yy346.pExpr->
32440 78 2e 70 53 65 6c 65 63 74 20 3d 20 79 79 6d 73  x.pSelect = yyms
32441 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 3b  p[-1].minor.yy3;
32442 0a 20 20 20 20 20 20 45 78 70 72 53 65 74 50 72  .      ExprSetPr
32443 6f 70 65 72 74 79 28 79 79 67 6f 74 6f 6d 69 6e  operty(yygotomin
32444 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20  or.yy346.pExpr, 
32445 45 50 5f 78 49 73 53 65 6c 65 63 74 29 3b 0a 20  EP_xIsSelect);. 
32446 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
32447 53 65 74 48 65 69 67 68 74 28 70 50 61 72 73 65  SetHeight(pParse
32448 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
32449 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 20 20  346.pExpr);.    
3244a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
3244b 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65  ite3SelectDelete
3244c 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79 6d  (pParse->db, yym
3244d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
3244e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 79 79 67  );.    }.    yyg
3244f 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 7a  otominor.yy346.z
32450 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b 2d 32  Start = yymsp[-2
32451 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20  ].minor.yy0.z;. 
32452 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
32453 79 33 34 36 2e 7a 45 6e 64 20 3d 20 26 79 79 6d  y346.zEnd = &yym
32454 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
32455 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  z[yymsp[0].minor
32456 2e 79 79 30 2e 6e 5d 3b 0a 20 20 7d 0a 20 20 20  .yy0.n];.  }.   
32457 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32458 20 20 63 61 73 65 20 32 32 39 3a 20 2f 2a 20 65    case 229: /* e
32459 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 69 6e 5f  xpr ::= expr in_
3245a 6f 70 20 4c 50 20 73 65 6c 65 63 74 20 52 50 20  op LP select RP 
3245b 2a 2f 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d  */.{.    yygotom
3245c 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
3245d 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
3245e 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 79  pParse, TK_IN, y
3245f 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
32460 79 33 34 36 2e 70 45 78 70 72 2c 20 30 2c 20 30  y346.pExpr, 0, 0
32461 29 3b 0a 20 20 20 20 69 66 28 20 79 79 67 6f 74  );.    if( yygot
32462 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
32463 70 72 20 29 7b 0a 20 20 20 20 20 20 79 79 67 6f  pr ){.      yygo
32464 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
32465 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d  xpr->x.pSelect =
32466 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
32467 2e 79 79 33 3b 0a 20 20 20 20 20 20 45 78 70 72  .yy3;.      Expr
32468 53 65 74 50 72 6f 70 65 72 74 79 28 79 79 67 6f  SetProperty(yygo
32469 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
3246a 78 70 72 2c 20 45 50 5f 78 49 73 53 65 6c 65 63  xpr, EP_xIsSelec
3246b 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  t);.      sqlite
3246c 33 45 78 70 72 53 65 74 48 65 69 67 68 74 28 70  3ExprSetHeight(p
3246d 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e  Parse, yygotomin
3246e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b  or.yy346.pExpr);
3246f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
32470 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
32471 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
32472 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
32473 72 2e 79 79 33 29 3b 0a 20 20 20 20 7d 0a 20 20  r.yy3);.    }.  
32474 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 33 5d 2e    if( yymsp[-3].
32475 6d 69 6e 6f 72 2e 79 79 33 32 38 20 29 20 79 79  minor.yy328 ) yy
32476 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  gotominor.yy346.
32477 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50  pExpr = sqlite3P
32478 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
32479 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  NOT, yygotominor
3247a 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20 30 2c  .yy346.pExpr, 0,
3247b 20 30 29 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d   0);.    yygotom
3247c 69 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72  inor.yy346.zStar
3247d 74 20 3d 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69  t = yymsp[-4].mi
3247e 6e 6f 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74  nor.yy346.zStart
3247f 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  ;.    yygotomino
32480 72 2e 79 79 33 34 36 2e 7a 45 6e 64 20 3d 20 26  r.yy346.zEnd = &
32481 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
32482 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69  y0.z[yymsp[0].mi
32483 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a 20 20 7d 0a  nor.yy0.n];.  }.
32484 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32485 20 20 20 20 20 63 61 73 65 20 32 33 30 3a 20 2f       case 230: /
32486 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20  * expr ::= expr 
32487 69 6e 5f 6f 70 20 6e 6d 20 64 62 6e 6d 20 2a 2f  in_op nm dbnm */
32488 0a 7b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a  .{.    SrcList *
32489 70 53 72 63 20 3d 20 73 71 6c 69 74 65 33 53 72  pSrc = sqlite3Sr
3248a 63 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  cListAppend(pPar
3248b 73 65 2d 3e 64 62 2c 20 30 2c 26 79 79 6d 73 70  se->db, 0,&yymsp
3248c 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26  [-1].minor.yy0,&
3248d 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
3248e 79 30 29 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d  y0);.    yygotom
3248f 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
32490 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
32491 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 79  pParse, TK_IN, y
32492 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
32493 79 33 34 36 2e 70 45 78 70 72 2c 20 30 2c 20 30  y346.pExpr, 0, 0
32494 29 3b 0a 20 20 20 20 69 66 28 20 79 79 67 6f 74  );.    if( yygot
32495 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
32496 70 72 20 29 7b 0a 20 20 20 20 20 20 79 79 67 6f  pr ){.      yygo
32497 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
32498 78 70 72 2d 3e 78 2e 70 53 65 6c 65 63 74 20 3d  xpr->x.pSelect =
32499 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65   sqlite3SelectNe
3249a 77 28 70 50 61 72 73 65 2c 20 30 2c 70 53 72 63  w(pParse, 0,pSrc
3249b 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 2c 30 29 3b  ,0,0,0,0,0,0,0);
3249c 0a 20 20 20 20 20 20 45 78 70 72 53 65 74 50 72  .      ExprSetPr
3249d 6f 70 65 72 74 79 28 79 79 67 6f 74 6f 6d 69 6e  operty(yygotomin
3249e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 2c 20  or.yy346.pExpr, 
3249f 45 50 5f 78 49 73 53 65 6c 65 63 74 29 3b 0a 20  EP_xIsSelect);. 
324a0 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
324a1 53 65 74 48 65 69 67 68 74 28 70 50 61 72 73 65  SetHeight(pParse
324a2 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
324a3 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 20 20  346.pExpr);.    
324a4 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
324a5 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
324a6 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 53  e(pParse->db, pS
324a7 72 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  rc);.    }.    i
324a8 66 28 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  f( yymsp[-2].min
324a9 6f 72 2e 79 79 33 32 38 20 29 20 79 79 67 6f 74  or.yy328 ) yygot
324aa 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
324ab 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  pr = sqlite3PExp
324ac 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54  r(pParse, TK_NOT
324ad 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
324ae 33 34 36 2e 70 45 78 70 72 2c 20 30 2c 20 30 29  346.pExpr, 0, 0)
324af 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  ;.    yygotomino
324b0 72 2e 79 79 33 34 36 2e 7a 53 74 61 72 74 20 3d  r.yy346.zStart =
324b1 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72   yymsp[-3].minor
324b2 2e 79 79 33 34 36 2e 7a 53 74 61 72 74 3b 0a 20  .yy346.zStart;. 
324b3 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
324b4 79 33 34 36 2e 7a 45 6e 64 20 3d 20 79 79 6d 73  y346.zEnd = yyms
324b5 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  p[0].minor.yy0.z
324b6 20 3f 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e   ? &yymsp[0].min
324b7 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30  or.yy0.z[yymsp[0
324b8 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 20 3a  ].minor.yy0.n] :
324b9 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f   &yymsp[-1].mino
324ba 72 2e 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 2d 31  r.yy0.z[yymsp[-1
324bb 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a  ].minor.yy0.n];.
324bc 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
324bd 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 33  k;.      case 23
324be 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 45  1: /* expr ::= E
324bf 58 49 53 54 53 20 4c 50 20 73 65 6c 65 63 74 20  XISTS LP select 
324c0 52 50 20 2a 2f 0a 7b 0a 20 20 20 20 45 78 70 72  RP */.{.    Expr
324c1 20 2a 70 20 3d 20 79 79 67 6f 74 6f 6d 69 6e 6f   *p = yygotomino
324c2 72 2e 79 79 33 34 36 2e 70 45 78 70 72 20 3d 20  r.yy346.pExpr = 
324c3 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
324c4 72 73 65 2c 20 54 4b 5f 45 58 49 53 54 53 2c 20  rse, TK_EXISTS, 
324c5 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69 66  0, 0, 0);.    if
324c6 28 20 70 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  ( p ){.      p->
324c7 78 2e 70 53 65 6c 65 63 74 20 3d 20 79 79 6d 73  x.pSelect = yyms
324c8 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 3b  p[-1].minor.yy3;
324c9 0a 20 20 20 20 20 20 45 78 70 72 53 65 74 50 72  .      ExprSetPr
324ca 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 78 49 73  operty(p, EP_xIs
324cb 53 65 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 73  Select);.      s
324cc 71 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69  qlite3ExprSetHei
324cd 67 68 74 28 70 50 61 72 73 65 2c 20 70 29 3b 0a  ght(pParse, p);.
324ce 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
324cf 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
324d0 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
324d1 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
324d2 2e 79 79 33 29 3b 0a 20 20 20 20 7d 0a 20 20 20  .yy3);.    }.   
324d3 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
324d4 34 36 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73  46.zStart = yyms
324d5 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  p[-3].minor.yy0.
324d6 7a 3b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e  z;.    yygotomin
324d7 6f 72 2e 79 79 33 34 36 2e 7a 45 6e 64 20 3d 20  or.yy346.zEnd = 
324d8 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
324d9 79 79 30 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d  yy0.z[yymsp[0].m
324da 69 6e 6f 72 2e 79 79 30 2e 6e 5d 3b 0a 20 20 7d  inor.yy0.n];.  }
324db 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
324dc 20 20 20 20 20 20 63 61 73 65 20 32 33 32 3a 20        case 232: 
324dd 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 43 41 53 45  /* expr ::= CASE
324de 20 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 63 61   case_operand ca
324df 73 65 5f 65 78 70 72 6c 69 73 74 20 63 61 73 65  se_exprlist case
324e0 5f 65 6c 73 65 20 45 4e 44 20 2a 2f 0a 7b 0a 20  _else END */.{. 
324e1 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
324e2 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74  46.pExpr = sqlit
324e3 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
324e4 54 4b 5f 43 41 53 45 2c 20 79 79 6d 73 70 5b 2d  TK_CASE, yymsp[-
324e5 33 5d 2e 6d 69 6e 6f 72 2e 79 79 31 33 32 2c 20  3].minor.yy132, 
324e6 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
324e7 79 79 31 33 32 2c 20 30 29 3b 0a 20 20 69 66 28  yy132, 0);.  if(
324e8 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
324e9 34 36 2e 70 45 78 70 72 20 29 7b 0a 20 20 20 20  46.pExpr ){.    
324ea 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
324eb 36 2e 70 45 78 70 72 2d 3e 78 2e 70 4c 69 73 74  6.pExpr->x.pList
324ec 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e   = yymsp[-2].min
324ed 6f 72 2e 79 79 31 34 3b 0a 20 20 20 20 73 71 6c  or.yy14;.    sql
324ee 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68  ite3ExprSetHeigh
324ef 74 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f  t(pParse, yygoto
324f0 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
324f1 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  r);.  }else{.   
324f2 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
324f3 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
324f4 62 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  b, yymsp[-2].min
324f5 6f 72 2e 79 79 31 34 29 3b 0a 20 20 7d 0a 20 20  or.yy14);.  }.  
324f6 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
324f7 36 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70  6.zStart = yymsp
324f8 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  [-4].minor.yy0.z
324f9 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
324fa 79 79 33 34 36 2e 7a 45 6e 64 20 3d 20 26 79 79  yy346.zEnd = &yy
324fb 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
324fc 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  .z[yymsp[0].mino
324fd 72 2e 79 79 30 2e 6e 5d 3b 0a 7d 0a 20 20 20 20  r.yy0.n];.}.    
324fe 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
324ff 20 63 61 73 65 20 32 33 33 3a 20 2f 2a 20 63 61   case 233: /* ca
32500 73 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d 20  se_exprlist ::= 
32501 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 57 48  case_exprlist WH
32502 45 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78 70  EN expr THEN exp
32503 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  r */.{.  yygotom
32504 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71 6c 69  inor.yy14 = sqli
32505 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
32506 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  d(pParse,yymsp[-
32507 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 79  4].minor.yy14, y
32508 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
32509 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 20 20 79  y346.pExpr);.  y
3250a 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20  ygotominor.yy14 
3250b 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
3250c 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79  tAppend(pParse,y
3250d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2c  ygotominor.yy14,
3250e 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
3250f 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a 7d 0a  yy346.pExpr);.}.
32510 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32511 20 20 20 20 20 63 61 73 65 20 32 33 34 3a 20 2f       case 234: /
32512 2a 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20  * case_exprlist 
32513 3a 3a 3d 20 57 48 45 4e 20 65 78 70 72 20 54 48  ::= WHEN expr TH
32514 45 4e 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 79  EN expr */.{.  y
32515 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20  ygotominor.yy14 
32516 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
32517 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30  tAppend(pParse,0
32518 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  , yymsp[-2].mino
32519 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a  r.yy346.pExpr);.
3251a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
3251b 31 34 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  14 = sqlite3Expr
3251c 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
3251d 65 2c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  e,yygotominor.yy
3251e 31 34 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  14, yymsp[0].min
3251f 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b  or.yy346.pExpr);
32520 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
32521 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 33  ;.      case 243
32522 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63 72 65  : /* cmd ::= cre
32523 61 74 65 6b 77 20 75 6e 69 71 75 65 66 6c 61 67  atekw uniqueflag
32524 20 49 4e 44 45 58 20 69 66 6e 6f 74 65 78 69 73   INDEX ifnotexis
32525 74 73 20 6e 6d 20 64 62 6e 6d 20 4f 4e 20 6e 6d  ts nm dbnm ON nm
32526 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 20 2a   LP idxlist RP *
32527 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 43 72 65  /.{.  sqlite3Cre
32528 61 74 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c  ateIndex(pParse,
32529 20 26 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f   &yymsp[-6].mino
3252a 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 2d 35  r.yy0, &yymsp[-5
3252b 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 0a 20 20  ].minor.yy0, .  
3252c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3252d 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73     sqlite3SrcLis
3252e 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e  tAppend(pParse->
3252f 64 62 2c 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e  db,0,&yymsp[-3].
32530 6d 69 6e 6f 72 2e 79 79 30 2c 30 29 2c 20 79 79  minor.yy0,0), yy
32531 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
32532 31 34 2c 20 79 79 6d 73 70 5b 2d 39 5d 2e 6d 69  14, yymsp[-9].mi
32533 6e 6f 72 2e 79 79 33 32 38 2c 0a 20 20 20 20 20  nor.yy328,.     
32534 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32535 20 26 79 79 6d 73 70 5b 2d 31 30 5d 2e 6d 69 6e   &yymsp[-10].min
32536 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b 30  or.yy0, &yymsp[0
32537 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 53 51 4c  ].minor.yy0, SQL
32538 49 54 45 5f 53 4f 5f 41 53 43 2c 20 79 79 6d 73  ITE_SO_ASC, yyms
32539 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32  p[-7].minor.yy32
3253a 38 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  8);.}.        br
3253b 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
3253c 32 34 34 3a 20 2f 2a 20 75 6e 69 71 75 65 66 6c  244: /* uniquefl
3253d 61 67 20 3a 3a 3d 20 55 4e 49 51 55 45 20 2a 2f  ag ::= UNIQUE */
3253e 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 38 3a  .      case 298:
3253f 20 2f 2a 20 72 61 69 73 65 74 79 70 65 20 3a 3a   /* raisetype ::
32540 3d 20 41 42 4f 52 54 20 2a 2f 20 79 79 74 65 73  = ABORT */ yytes
32541 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
32542 32 39 38 29 3b 0a 7b 79 79 67 6f 74 6f 6d 69 6e  298);.{yygotomin
32543 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f 41 62  or.yy328 = OE_Ab
32544 6f 72 74 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ort;}.        br
32545 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32546 32 34 35 3a 20 2f 2a 20 75 6e 69 71 75 65 66 6c  245: /* uniquefl
32547 61 67 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74  ag ::= */.{yygot
32548 6f 6d 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f  ominor.yy328 = O
32549 45 5f 4e 6f 6e 65 3b 7d 0a 20 20 20 20 20 20 20  E_None;}.       
3254a 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3254b 73 65 20 32 34 38 3a 20 2f 2a 20 69 64 78 6c 69  se 248: /* idxli
3254c 73 74 20 3a 3a 3d 20 69 64 78 6c 69 73 74 20 43  st ::= idxlist C
3254d 4f 4d 4d 41 20 6e 6d 20 63 6f 6c 6c 61 74 65 20  OMMA nm collate 
3254e 73 6f 72 74 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20  sortorder */.{. 
3254f 20 45 78 70 72 20 2a 70 20 3d 20 30 3b 0a 20 20   Expr *p = 0;.  
32550 69 66 28 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  if( yymsp[-1].mi
32551 6e 6f 72 2e 79 79 30 2e 6e 3e 30 20 29 7b 0a 20  nor.yy0.n>0 ){. 
32552 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 45 78     p = sqlite3Ex
32553 70 72 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 54  pr(pParse->db, T
32554 4b 5f 43 4f 4c 55 4d 4e 2c 20 30 29 3b 0a 20 20  K_COLUMN, 0);.  
32555 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74    sqlite3ExprSet
32556 43 6f 6c 6c 28 70 50 61 72 73 65 2c 20 70 2c 20  Coll(pParse, p, 
32557 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
32558 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20 79 79 67  .yy0);.  }.  yyg
32559 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20  otominor.yy14 = 
3255a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
3255b 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d  ppend(pParse,yym
3255c 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 31  sp[-4].minor.yy1
3255d 34 2c 20 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  4, p);.  sqlite3
3255e 45 78 70 72 4c 69 73 74 53 65 74 4e 61 6d 65 28  ExprListSetName(
3255f 70 50 61 72 73 65 2c 79 79 67 6f 74 6f 6d 69 6e  pParse,yygotomin
32560 6f 72 2e 79 79 31 34 2c 26 79 79 6d 73 70 5b 2d  or.yy14,&yymsp[-
32561 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 31 29 3b  2].minor.yy0,1);
32562 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  .  sqlite3ExprLi
32563 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 70 50  stCheckLength(pP
32564 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f  arse, yygotomino
32565 72 2e 79 79 31 34 2c 20 22 69 6e 64 65 78 22 29  r.yy14, "index")
32566 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69  ;.  if( yygotomi
32567 6e 6f 72 2e 79 79 31 34 20 29 20 79 79 67 6f 74  nor.yy14 ) yygot
32568 6f 6d 69 6e 6f 72 2e 79 79 31 34 2d 3e 61 5b 79  ominor.yy14->a[y
32569 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2d  ygotominor.yy14-
3256a 3e 6e 45 78 70 72 2d 31 5d 2e 73 6f 72 74 4f 72  >nExpr-1].sortOr
3256b 64 65 72 20 3d 20 28 75 38 29 79 79 6d 73 70 5b  der = (u8)yymsp[
3256c 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 0a  0].minor.yy328;.
3256d 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
3256e 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 39 3a  .      case 249:
3256f 20 2f 2a 20 69 64 78 6c 69 73 74 20 3a 3a 3d 20   /* idxlist ::= 
32570 6e 6d 20 63 6f 6c 6c 61 74 65 20 73 6f 72 74 6f  nm collate sorto
32571 72 64 65 72 20 2a 2f 0a 7b 0a 20 20 45 78 70 72  rder */.{.  Expr
32572 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28 20 79   *p = 0;.  if( y
32573 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
32574 79 30 2e 6e 3e 30 20 29 7b 0a 20 20 20 20 70 20  y0.n>0 ){.    p 
32575 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
32576 50 61 72 73 65 2c 20 54 4b 5f 43 4f 4c 55 4d 4e  Parse, TK_COLUMN
32577 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
32578 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 43 6f  sqlite3ExprSetCo
32579 6c 6c 28 70 50 61 72 73 65 2c 20 70 2c 20 26 79  ll(pParse, p, &y
3257a 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
3257b 79 30 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f 74  y0);.  }.  yygot
3257c 6f 6d 69 6e 6f 72 2e 79 79 31 34 20 3d 20 73 71  ominor.yy14 = sq
3257d 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
3257e 65 6e 64 28 70 50 61 72 73 65 2c 30 2c 20 70 29  end(pParse,0, p)
3257f 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  ;.  sqlite3ExprL
32580 69 73 74 53 65 74 4e 61 6d 65 28 70 50 61 72 73  istSetName(pPars
32581 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  e, yygotominor.y
32582 79 31 34 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e  y14, &yymsp[-2].
32583 6d 69 6e 6f 72 2e 79 79 30 2c 20 31 29 3b 0a 20  minor.yy0, 1);. 
32584 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
32585 43 68 65 63 6b 4c 65 6e 67 74 68 28 70 50 61 72  CheckLength(pPar
32586 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  se, yygotominor.
32587 79 79 31 34 2c 20 22 69 6e 64 65 78 22 29 3b 0a  yy14, "index");.
32588 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
32589 72 2e 79 79 31 34 20 29 20 79 79 67 6f 74 6f 6d  r.yy14 ) yygotom
3258a 69 6e 6f 72 2e 79 79 31 34 2d 3e 61 5b 79 79 67  inor.yy14->a[yyg
3258b 6f 74 6f 6d 69 6e 6f 72 2e 79 79 31 34 2d 3e 6e  otominor.yy14->n
3258c 45 78 70 72 2d 31 5d 2e 73 6f 72 74 4f 72 64 65  Expr-1].sortOrde
3258d 72 20 3d 20 28 75 38 29 79 79 6d 73 70 5b 30 5d  r = (u8)yymsp[0]
3258e 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 3b 0a 7d 0a  .minor.yy328;.}.
3258f 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32590 20 20 20 20 20 63 61 73 65 20 32 35 30 3a 20 2f       case 250: /
32591 2a 20 63 6f 6c 6c 61 74 65 20 3a 3a 3d 20 2a 2f  * collate ::= */
32592 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
32593 30 2e 7a 20 3d 20 30 3b 20 79 79 67 6f 74 6f 6d  0.z = 0; yygotom
32594 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20 30 3b 7d  inor.yy0.n = 0;}
32595 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32596 20 20 20 20 20 20 63 61 73 65 20 32 35 32 3a 20        case 252: 
32597 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20  /* cmd ::= DROP 
32598 49 4e 44 45 58 20 69 66 65 78 69 73 74 73 20 66  INDEX ifexists f
32599 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 73 71 6c 69  ullname */.{sqli
3259a 74 65 33 44 72 6f 70 49 6e 64 65 78 28 70 50 61  te3DropIndex(pPa
3259b 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  rse, yymsp[0].mi
3259c 6e 6f 72 2e 79 79 36 35 2c 20 79 79 6d 73 70 5b  nor.yy65, yymsp[
3259d 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38 29  -1].minor.yy328)
3259e 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
3259f 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 33  ;.      case 253
325a0 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 56 41 43  : /* cmd ::= VAC
325a1 55 55 4d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  UUM */.      cas
325a2 65 20 32 35 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 254: /* cmd ::
325a3 3d 20 56 41 43 55 55 4d 20 6e 6d 20 2a 2f 20 79  = VACUUM nm */ y
325a4 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
325a5 6e 6f 3d 3d 32 35 34 29 3b 0a 7b 73 71 6c 69 74  no==254);.{sqlit
325a6 65 33 56 61 63 75 75 6d 28 70 50 61 72 73 65 29  e3Vacuum(pParse)
325a7 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
325a8 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 35  ;.      case 255
325a9 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41  : /* cmd ::= PRA
325aa 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b  GMA nm dbnm */.{
325ab 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28 70 50  sqlite3Pragma(pP
325ac 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  arse,&yymsp[-1].
325ad 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
325ae 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 2c  [0].minor.yy0,0,
325af 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
325b0 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
325b1 35 36 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50  56: /* cmd ::= P
325b2 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51  RAGMA nm dbnm EQ
325b3 20 6e 6d 6e 75 6d 20 2a 2f 0a 7b 73 71 6c 69 74   nmnum */.{sqlit
325b4 65 33 50 72 61 67 6d 61 28 70 50 61 72 73 65 2c  e3Pragma(pParse,
325b5 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72  &yymsp[-3].minor
325b6 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e  .yy0,&yymsp[-2].
325b7 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
325b8 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 29  [0].minor.yy0,0)
325b9 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
325ba 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 37  ;.      case 257
325bb 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41  : /* cmd ::= PRA
325bc 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 4c 50 20 6e  GMA nm dbnm LP n
325bd 6d 6e 75 6d 20 52 50 20 2a 2f 0a 7b 73 71 6c 69  mnum RP */.{sqli
325be 74 65 33 50 72 61 67 6d 61 28 70 50 61 72 73 65  te3Pragma(pParse
325bf 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  ,&yymsp[-4].mino
325c0 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 33 5d  r.yy0,&yymsp[-3]
325c1 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
325c2 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-1].minor.yy0,
325c3 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
325c4 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
325c5 35 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50  58: /* cmd ::= P
325c6 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51  RAGMA nm dbnm EQ
325c7 20 6d 69 6e 75 73 5f 6e 75 6d 20 2a 2f 0a 7b 73   minus_num */.{s
325c8 71 6c 69 74 65 33 50 72 61 67 6d 61 28 70 50 61  qlite3Pragma(pPa
325c9 72 73 65 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d  rse,&yymsp[-3].m
325ca 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b  inor.yy0,&yymsp[
325cb 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -2].minor.yy0,&y
325cc 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
325cd 30 2c 31 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  0,1);}.        b
325ce 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
325cf 20 32 35 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   259: /* cmd ::=
325d0 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20   PRAGMA nm dbnm 
325d1 4c 50 20 6d 69 6e 75 73 5f 6e 75 6d 20 52 50 20  LP minus_num RP 
325d2 2a 2f 0a 7b 73 71 6c 69 74 65 33 50 72 61 67 6d  */.{sqlite3Pragm
325d3 61 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b  a(pParse,&yymsp[
325d4 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -4].minor.yy0,&y
325d5 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
325d6 79 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  y0,&yymsp[-1].mi
325d7 6e 6f 72 2e 79 79 30 2c 31 29 3b 7d 0a 20 20 20  nor.yy0,1);}.   
325d8 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
325d9 20 20 63 61 73 65 20 32 37 30 3a 20 2f 2a 20 63    case 270: /* c
325da 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 6b 77 20  md ::= createkw 
325db 74 72 69 67 67 65 72 5f 64 65 63 6c 20 42 45 47  trigger_decl BEG
325dc 49 4e 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c  IN trigger_cmd_l
325dd 69 73 74 20 45 4e 44 20 2a 2f 0a 7b 0a 20 20 54  ist END */.{.  T
325de 6f 6b 65 6e 20 61 6c 6c 3b 0a 20 20 61 6c 6c 2e  oken all;.  all.
325df 7a 20 3d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  z = yymsp[-3].mi
325e0 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 20 20 61 6c 6c  nor.yy0.z;.  all
325e1 2e 6e 20 3d 20 28 69 6e 74 29 28 79 79 6d 73 70  .n = (int)(yymsp
325e2 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20  [0].minor.yy0.z 
325e3 2d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  - yymsp[-3].mino
325e4 72 2e 79 79 30 2e 7a 29 20 2b 20 79 79 6d 73 70  r.yy0.z) + yymsp
325e5 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3b  [0].minor.yy0.n;
325e6 0a 20 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68  .  sqlite3Finish
325e7 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20  Trigger(pParse, 
325e8 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
325e9 79 79 34 37 33 2c 20 26 61 6c 6c 29 3b 0a 7d 0a  yy473, &all);.}.
325ea 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
325eb 20 20 20 20 20 63 61 73 65 20 32 37 31 3a 20 2f       case 271: /
325ec 2a 20 74 72 69 67 67 65 72 5f 64 65 63 6c 20 3a  * trigger_decl :
325ed 3a 3d 20 74 65 6d 70 20 54 52 49 47 47 45 52 20  := temp TRIGGER 
325ee 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64  ifnotexists nm d
325ef 62 6e 6d 20 74 72 69 67 67 65 72 5f 74 69 6d 65  bnm trigger_time
325f0 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20 4f   trigger_event O
325f1 4e 20 66 75 6c 6c 6e 61 6d 65 20 66 6f 72 65 61  N fullname forea
325f2 63 68 5f 63 6c 61 75 73 65 20 77 68 65 6e 5f 63  ch_clause when_c
325f3 6c 61 75 73 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c  lause */.{.  sql
325f4 69 74 65 33 42 65 67 69 6e 54 72 69 67 67 65 72  ite3BeginTrigger
325f5 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b  (pParse, &yymsp[
325f6 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26  -7].minor.yy0, &
325f7 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e  yymsp[-6].minor.
325f8 79 79 30 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e 6d  yy0, yymsp[-5].m
325f9 69 6e 6f 72 2e 79 79 33 32 38 2c 20 79 79 6d 73  inor.yy328, yyms
325fa 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 37  p[-4].minor.yy37
325fb 38 2e 61 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  8.a, yymsp[-4].m
325fc 69 6e 6f 72 2e 79 79 33 37 38 2e 62 2c 20 79 79  inor.yy378.b, yy
325fd 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
325fe 36 35 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  65, yymsp[0].min
325ff 6f 72 2e 79 79 31 33 32 2c 20 79 79 6d 73 70 5b  or.yy132, yymsp[
32600 2d 31 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 32 38  -10].minor.yy328
32601 2c 20 79 79 6d 73 70 5b 2d 38 5d 2e 6d 69 6e 6f  , yymsp[-8].mino
32602 72 2e 79 79 33 32 38 29 3b 0a 20 20 79 79 67 6f  r.yy328);.  yygo
32603 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 28 79  tominor.yy0 = (y
32604 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-6].minor.y
32605 79 30 2e 6e 3d 3d 30 3f 79 79 6d 73 70 5b 2d 37  y0.n==0?yymsp[-7
32606 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3a 79 79 6d 73  ].minor.yy0:yyms
32607 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-6].minor.yy0)
32608 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
32609 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 37  k;.      case 27
3260a 32 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 74 69  2: /* trigger_ti
3260b 6d 65 20 3a 3a 3d 20 42 45 46 4f 52 45 20 2a 2f  me ::= BEFORE */
3260c 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 35 3a  .      case 275:
3260d 20 2f 2a 20 74 72 69 67 67 65 72 5f 74 69 6d 65   /* trigger_time
3260e 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61   ::= */ yytestca
3260f 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 37 35  se(yyruleno==275
32610 29 3b 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  );.{ yygotominor
32611 2e 79 79 33 32 38 20 3d 20 54 4b 5f 42 45 46 4f  .yy328 = TK_BEFO
32612 52 45 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  RE; }.        br
32613 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
32614 32 37 33 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  273: /* trigger_
32615 74 69 6d 65 20 3a 3a 3d 20 41 46 54 45 52 20 2a  time ::= AFTER *
32616 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.{ yygotominor.
32617 79 79 33 32 38 20 3d 20 54 4b 5f 41 46 54 45 52  yy328 = TK_AFTER
32618 3b 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ;  }.        bre
32619 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
3261a 37 34 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 74  74: /* trigger_t
3261b 69 6d 65 20 3a 3a 3d 20 49 4e 53 54 45 41 44 20  ime ::= INSTEAD 
3261c 4f 46 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69  OF */.{ yygotomi
3261d 6e 6f 72 2e 79 79 33 32 38 20 3d 20 54 4b 5f 49  nor.yy328 = TK_I
3261e 4e 53 54 45 41 44 3b 7d 0a 20 20 20 20 20 20 20  NSTEAD;}.       
3261f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
32620 73 65 20 32 37 36 3a 20 2f 2a 20 74 72 69 67 67  se 276: /* trigg
32621 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 44 45 4c  er_event ::= DEL
32622 45 54 45 7c 49 4e 53 45 52 54 20 2a 2f 0a 20 20  ETE|INSERT */.  
32623 20 20 20 20 63 61 73 65 20 32 37 37 3a 20 2f 2a      case 277: /*
32624 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20 3a   trigger_event :
32625 3a 3d 20 55 50 44 41 54 45 20 2a 2f 20 79 79 74  := UPDATE */ yyt
32626 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
32627 3d 3d 32 37 37 29 3b 0a 7b 79 79 67 6f 74 6f 6d  ==277);.{yygotom
32628 69 6e 6f 72 2e 79 79 33 37 38 2e 61 20 3d 20 79  inor.yy378.a = y
32629 79 6d 73 70 5b 30 5d 2e 6d 61 6a 6f 72 3b 20 79  ymsp[0].major; y
3262a 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 37 38  ygotominor.yy378
3262b 2e 62 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  .b = 0;}.       
3262c 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3262d 73 65 20 32 37 38 3a 20 2f 2a 20 74 72 69 67 67  se 278: /* trigg
3262e 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 55 50 44  er_event ::= UPD
3262f 41 54 45 20 4f 46 20 69 6e 73 63 6f 6c 6c 69 73  ATE OF inscollis
32630 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  t */.{yygotomino
32631 72 2e 79 79 33 37 38 2e 61 20 3d 20 54 4b 5f 55  r.yy378.a = TK_U
32632 50 44 41 54 45 3b 20 79 79 67 6f 74 6f 6d 69 6e  PDATE; yygotomin
32633 6f 72 2e 79 79 33 37 38 2e 62 20 3d 20 79 79 6d  or.yy378.b = yym
32634 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30  sp[0].minor.yy40
32635 38 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  8;}.        brea
32636 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 38  k;.      case 28
32637 31 3a 20 2f 2a 20 77 68 65 6e 5f 63 6c 61 75 73  1: /* when_claus
32638 65 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63  e ::= */.      c
32639 61 73 65 20 33 30 33 3a 20 2f 2a 20 6b 65 79 5f  ase 303: /* key_
3263a 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73  opt ::= */ yytes
3263b 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
3263c 33 30 33 29 3b 0a 7b 20 79 79 67 6f 74 6f 6d 69  303);.{ yygotomi
3263d 6e 6f 72 2e 79 79 31 33 32 20 3d 20 30 3b 20 7d  nor.yy132 = 0; }
3263e 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
3263f 20 20 20 20 20 20 63 61 73 65 20 32 38 32 3a 20        case 282: 
32640 2f 2a 20 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a  /* when_clause :
32641 3a 3d 20 57 48 45 4e 20 65 78 70 72 20 2a 2f 0a  := WHEN expr */.
32642 20 20 20 20 20 20 63 61 73 65 20 33 30 34 3a 20        case 304: 
32643 2f 2a 20 6b 65 79 5f 6f 70 74 20 3a 3a 3d 20 4b  /* key_opt ::= K
32644 45 59 20 65 78 70 72 20 2a 2f 20 79 79 74 65 73  EY expr */ yytes
32645 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
32646 33 30 34 29 3b 0a 7b 20 79 79 67 6f 74 6f 6d 69  304);.{ yygotomi
32647 6e 6f 72 2e 79 79 31 33 32 20 3d 20 79 79 6d 73  nor.yy132 = yyms
32648 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36  p[0].minor.yy346
32649 2e 70 45 78 70 72 3b 20 7d 0a 20 20 20 20 20 20  .pExpr; }.      
3264a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3264b 61 73 65 20 32 38 33 3a 20 2f 2a 20 74 72 69 67  ase 283: /* trig
3264c 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d  ger_cmd_list ::=
3264d 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73   trigger_cmd_lis
3264e 74 20 74 72 69 67 67 65 72 5f 63 6d 64 20 53 45  t trigger_cmd SE
3264f 4d 49 20 2a 2f 0a 7b 0a 20 20 61 73 73 65 72 74  MI */.{.  assert
32650 28 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  ( yymsp[-2].mino
32651 72 2e 79 79 34 37 33 21 3d 30 20 29 3b 0a 20 20  r.yy473!=0 );.  
32652 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
32653 79 79 34 37 33 2d 3e 70 4c 61 73 74 2d 3e 70 4e  yy473->pLast->pN
32654 65 78 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e  ext = yymsp[-1].
32655 6d 69 6e 6f 72 2e 79 79 34 37 33 3b 0a 20 20 79  minor.yy473;.  y
32656 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
32657 79 34 37 33 2d 3e 70 4c 61 73 74 20 3d 20 79 79  y473->pLast = yy
32658 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
32659 34 37 33 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  473;.  yygotomin
3265a 6f 72 2e 79 79 34 37 33 20 3d 20 79 79 6d 73 70  or.yy473 = yymsp
3265b 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33  [-2].minor.yy473
3265c 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
3265d 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 38  k;.      case 28
3265e 34 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d  4: /* trigger_cm
3265f 64 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69 67 67  d_list ::= trigg
32660 65 72 5f 63 6d 64 20 53 45 4d 49 20 2a 2f 0a 7b  er_cmd SEMI */.{
32661 20 0a 20 20 61 73 73 65 72 74 28 20 79 79 6d 73   .  assert( yyms
32662 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37  p[-1].minor.yy47
32663 33 21 3d 30 20 29 3b 0a 20 20 79 79 6d 73 70 5b  3!=0 );.  yymsp[
32664 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 2d  -1].minor.yy473-
32665 3e 70 4c 61 73 74 20 3d 20 79 79 6d 73 70 5b 2d  >pLast = yymsp[-
32666 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 37 33 3b 0a  1].minor.yy473;.
32667 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
32668 34 37 33 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e  473 = yymsp[-1].
32669 6d 69 6e 6f 72 2e 79 79 34 37 33 3b 0a 7d 0a 20  minor.yy473;.}. 
3266a 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3266b 20 20 20 20 63 61 73 65 20 32 38 36 3a 20 2f 2a      case 286: /*
3266c 20 74 72 6e 6d 20 3a 3a 3d 20 6e 6d 20 44 4f 54   trnm ::= nm DOT
3266d 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74   nm */.{.  yygot
3266e 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20 79 79 6d  ominor.yy0 = yym
3266f 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b  sp[0].minor.yy0;
32670 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d  .  sqlite3ErrorM
32671 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  sg(pParse, .    
32672 20 20 20 20 22 71 75 61 6c 69 66 69 65 64 20 74      "qualified t
32673 61 62 6c 65 20 6e 61 6d 65 73 20 61 72 65 20 6e  able names are n
32674 6f 74 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 49 4e  ot allowed on IN
32675 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 61 6e  SERT, UPDATE, an
32676 64 20 44 45 4c 45 54 45 20 22 0a 20 20 20 20 20  d DELETE ".     
32677 20 20 20 22 73 74 61 74 65 6d 65 6e 74 73 20 77     "statements w
32678 69 74 68 69 6e 20 74 72 69 67 67 65 72 73 22 29  ithin triggers")
32679 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
3267a 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 38  k;.      case 28
3267b 38 3a 20 2f 2a 20 74 72 69 64 78 62 79 20 3a 3a  8: /* tridxby ::
3267c 3d 20 49 4e 44 45 58 45 44 20 42 59 20 6e 6d 20  = INDEXED BY nm 
3267d 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 45 72  */.{.  sqlite3Er
3267e 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 0a 20  rorMsg(pParse,. 
3267f 20 20 20 20 20 20 20 22 74 68 65 20 49 4e 44 45         "the INDE
32680 58 45 44 20 42 59 20 63 6c 61 75 73 65 20 69 73  XED BY clause is
32681 20 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 6f 6e 20   not allowed on 
32682 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54 45  UPDATE or DELETE
32683 20 73 74 61 74 65 6d 65 6e 74 73 20 22 0a 20 20   statements ".  
32684 20 20 20 20 20 20 22 77 69 74 68 69 6e 20 74 72        "within tr
32685 69 67 67 65 72 73 22 29 3b 0a 7d 0a 20 20 20 20  iggers");.}.    
32686 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32687 20 63 61 73 65 20 32 38 39 3a 20 2f 2a 20 74 72   case 289: /* tr
32688 69 64 78 62 79 20 3a 3a 3d 20 4e 4f 54 20 49 4e  idxby ::= NOT IN
32689 44 45 58 45 44 20 2a 2f 0a 7b 0a 20 20 73 71 6c  DEXED */.{.  sql
3268a 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
3268b 72 73 65 2c 0a 20 20 20 20 20 20 20 20 22 74 68  rse,.        "th
3268c 65 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c  e NOT INDEXED cl
3268d 61 75 73 65 20 69 73 20 6e 6f 74 20 61 6c 6c 6f  ause is not allo
3268e 77 65 64 20 6f 6e 20 55 50 44 41 54 45 20 6f 72  wed on UPDATE or
3268f 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e   DELETE statemen
32690 74 73 20 22 0a 20 20 20 20 20 20 20 20 22 77 69  ts ".        "wi
32691 74 68 69 6e 20 74 72 69 67 67 65 72 73 22 29 3b  thin triggers");
32692 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
32693 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 30  ;.      case 290
32694 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64  : /* trigger_cmd
32695 20 3a 3a 3d 20 55 50 44 41 54 45 20 6f 72 63 6f   ::= UPDATE orco
32696 6e 66 20 74 72 6e 6d 20 74 72 69 64 78 62 79 20  nf trnm tridxby 
32697 53 45 54 20 73 65 74 6c 69 73 74 20 77 68 65 72  SET setlist wher
32698 65 5f 6f 70 74 20 2a 2f 0a 7b 20 79 79 67 6f 74  e_opt */.{ yygot
32699 6f 6d 69 6e 6f 72 2e 79 79 34 37 33 20 3d 20 73  ominor.yy473 = s
3269a 71 6c 69 74 65 33 54 72 69 67 67 65 72 55 70 64  qlite3TriggerUpd
3269b 61 74 65 53 74 65 70 28 70 50 61 72 73 65 2d 3e  ateStep(pParse->
3269c 64 62 2c 20 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d  db, &yymsp[-4].m
3269d 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b  inor.yy0, yymsp[
3269e 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 31 34 2c 20  -1].minor.yy14, 
3269f 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
326a0 79 31 33 32 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e  y132, yymsp[-5].
326a1 6d 69 6e 6f 72 2e 79 79 31 38 36 29 3b 20 7d 0a  minor.yy186); }.
326a2 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
326a3 20 20 20 20 20 63 61 73 65 20 32 39 31 3a 20 2f       case 291: /
326a4 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a  * trigger_cmd ::
326a5 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54  = insert_cmd INT
326a6 4f 20 74 72 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73  O trnm inscollis
326a7 74 5f 6f 70 74 20 56 41 4c 55 45 53 20 4c 50 20  t_opt VALUES LP 
326a8 69 74 65 6d 6c 69 73 74 20 52 50 20 2a 2f 0a 7b  itemlist RP */.{
326a9 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 37  yygotominor.yy47
326aa 33 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67  3 = sqlite3Trigg
326ab 65 72 49 6e 73 65 72 74 53 74 65 70 28 70 50 61  erInsertStep(pPa
326ac 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70 5b  rse->db, &yymsp[
326ad 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79  -5].minor.yy0, y
326ae 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
326af 79 34 30 38 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  y408, yymsp[-1].
326b0 6d 69 6e 6f 72 2e 79 79 31 34 2c 20 30 2c 20 79  minor.yy14, 0, y
326b1 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-7].minor.y
326b2 79 31 38 36 29 3b 7d 0a 20 20 20 20 20 20 20 20  y186);}.        
326b3 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
326b4 65 20 32 39 32 3a 20 2f 2a 20 74 72 69 67 67 65  e 292: /* trigge
326b5 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74  r_cmd ::= insert
326b6 5f 63 6d 64 20 49 4e 54 4f 20 74 72 6e 6d 20 69  _cmd INTO trnm i
326b7 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 73 65  nscollist_opt se
326b8 6c 65 63 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  lect */.{yygotom
326b9 69 6e 6f 72 2e 79 79 34 37 33 20 3d 20 73 71 6c  inor.yy473 = sql
326ba 69 74 65 33 54 72 69 67 67 65 72 49 6e 73 65 72  ite3TriggerInser
326bb 74 53 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62  tStep(pParse->db
326bc 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  , &yymsp[-2].min
326bd 6f 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d 31  or.yy0, yymsp[-1
326be 5d 2e 6d 69 6e 6f 72 2e 79 79 34 30 38 2c 20 30  ].minor.yy408, 0
326bf 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
326c0 2e 79 79 33 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e  .yy3, yymsp[-4].
326c1 6d 69 6e 6f 72 2e 79 79 31 38 36 29 3b 7d 0a 20  minor.yy186);}. 
326c2 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
326c3 20 20 20 20 63 61 73 65 20 32 39 33 3a 20 2f 2a      case 293: /*
326c4 20 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d   trigger_cmd ::=
326c5 20 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 72 6e   DELETE FROM trn
326c6 6d 20 74 72 69 64 78 62 79 20 77 68 65 72 65 5f  m tridxby where_
326c7 6f 70 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  opt */.{yygotomi
326c8 6e 6f 72 2e 79 79 34 37 33 20 3d 20 73 71 6c 69  nor.yy473 = sqli
326c9 74 65 33 54 72 69 67 67 65 72 44 65 6c 65 74 65  te3TriggerDelete
326ca 53 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c  Step(pParse->db,
326cb 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f   &yymsp[-2].mino
326cc 72 2e 79 79 30 2c 20 79 79 6d 73 70 5b 30 5d 2e  r.yy0, yymsp[0].
326cd 6d 69 6e 6f 72 2e 79 79 31 33 32 29 3b 7d 0a 20  minor.yy132);}. 
326ce 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
326cf 20 20 20 20 63 61 73 65 20 32 39 34 3a 20 2f 2a      case 294: /*
326d0 20 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d   trigger_cmd ::=
326d1 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 79 79 67 6f   select */.{yygo
326d2 74 6f 6d 69 6e 6f 72 2e 79 79 34 37 33 20 3d 20  tominor.yy473 = 
326d3 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 53 65  sqlite3TriggerSe
326d4 6c 65 63 74 53 74 65 70 28 70 50 61 72 73 65 2d  lectStep(pParse-
326d5 3e 64 62 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  >db, yymsp[0].mi
326d6 6e 6f 72 2e 79 79 33 29 3b 20 7d 0a 20 20 20 20  nor.yy3); }.    
326d7 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
326d8 20 63 61 73 65 20 32 39 35 3a 20 2f 2a 20 65 78   case 295: /* ex
326d9 70 72 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20  pr ::= RAISE LP 
326da 49 47 4e 4f 52 45 20 52 50 20 2a 2f 0a 7b 0a 20  IGNORE RP */.{. 
326db 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33   yygotominor.yy3
326dc 34 36 2e 70 45 78 70 72 20 3d 20 73 71 6c 69 74  46.pExpr = sqlit
326dd 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
326de 54 4b 5f 52 41 49 53 45 2c 20 30 2c 20 30 2c 20  TK_RAISE, 0, 0, 
326df 30 29 3b 20 0a 20 20 69 66 28 20 79 79 67 6f 74  0); .  if( yygot
326e0 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78  ominor.yy346.pEx
326e1 70 72 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f  pr ){.    yygoto
326e2 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70  minor.yy346.pExp
326e3 72 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 4f 45  r->affinity = OE
326e4 5f 49 67 6e 6f 72 65 3b 0a 20 20 7d 0a 20 20 79  _Ignore;.  }.  y
326e5 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36  ygotominor.yy346
326e6 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70 5b  .zStart = yymsp[
326e7 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b  -3].minor.yy0.z;
326e8 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
326e9 79 33 34 36 2e 7a 45 6e 64 20 3d 20 26 79 79 6d  y346.zEnd = &yym
326ea 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
326eb 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  z[yymsp[0].minor
326ec 2e 79 79 30 2e 6e 5d 3b 0a 7d 0a 20 20 20 20 20  .yy0.n];.}.     
326ed 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
326ee 63 61 73 65 20 32 39 36 3a 20 2f 2a 20 65 78 70  case 296: /* exp
326ef 72 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20 72  r ::= RAISE LP r
326f0 61 69 73 65 74 79 70 65 20 43 4f 4d 4d 41 20 6e  aisetype COMMA n
326f1 6d 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f  m RP */.{.  yygo
326f2 74 6f 6d 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45  tominor.yy346.pE
326f3 78 70 72 20 3d 20 73 71 6c 69 74 65 33 50 45 78  xpr = sqlite3PEx
326f4 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 52 41  pr(pParse, TK_RA
326f5 49 53 45 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73  ISE, 0, 0, &yyms
326f6 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-1].minor.yy0)
326f7 3b 20 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d  ; .  if( yygotom
326f8 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
326f9 20 29 20 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d   ) {.    yygotom
326fa 69 6e 6f 72 2e 79 79 33 34 36 2e 70 45 78 70 72  inor.yy346.pExpr
326fb 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 28 63 68  ->affinity = (ch
326fc 61 72 29 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  ar)yymsp[-3].min
326fd 6f 72 2e 79 79 33 32 38 3b 0a 20 20 7d 0a 20 20  or.yy328;.  }.  
326fe 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 34  yygotominor.yy34
326ff 36 2e 7a 53 74 61 72 74 20 3d 20 79 79 6d 73 70  6.zStart = yymsp
32700 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  [-5].minor.yy0.z
32701 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
32702 79 79 33 34 36 2e 7a 45 6e 64 20 3d 20 26 79 79  yy346.zEnd = &yy
32703 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
32704 2e 7a 5b 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  .z[yymsp[0].mino
32705 72 2e 79 79 30 2e 6e 5d 3b 0a 7d 0a 20 20 20 20  r.yy0.n];.}.    
32706 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32707 20 63 61 73 65 20 32 39 37 3a 20 2f 2a 20 72 61   case 297: /* ra
32708 69 73 65 74 79 70 65 20 3a 3a 3d 20 52 4f 4c 4c  isetype ::= ROLL
32709 42 41 43 4b 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  BACK */.{yygotom
3270a 69 6e 6f 72 2e 79 79 33 32 38 20 3d 20 4f 45 5f  inor.yy328 = OE_
3270b 52 6f 6c 6c 62 61 63 6b 3b 7d 0a 20 20 20 20 20  Rollback;}.     
3270c 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
3270d 63 61 73 65 20 32 39 39 3a 20 2f 2a 20 72 61 69  case 299: /* rai
3270e 73 65 74 79 70 65 20 3a 3a 3d 20 46 41 49 4c 20  setype ::= FAIL 
3270f 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
32710 79 79 33 32 38 20 3d 20 4f 45 5f 46 61 69 6c 3b  yy328 = OE_Fail;
32711 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32712 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 30 3a  .      case 300:
32713 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50   /* cmd ::= DROP
32714 20 54 52 49 47 47 45 52 20 69 66 65 78 69 73 74   TRIGGER ifexist
32715 73 20 66 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a  s fullname */.{.
32716 20 20 73 71 6c 69 74 65 33 44 72 6f 70 54 72 69    sqlite3DropTri
32717 67 67 65 72 28 70 50 61 72 73 65 2c 79 79 6d 73  gger(pParse,yyms
32718 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 36 35 2c  p[0].minor.yy65,
32719 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
3271a 79 79 33 32 38 29 3b 0a 7d 0a 20 20 20 20 20 20  yy328);.}.      
3271b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
3271c 61 73 65 20 33 30 31 3a 20 2f 2a 20 63 6d 64 20  ase 301: /* cmd 
3271d 3a 3a 3d 20 41 54 54 41 43 48 20 64 61 74 61 62  ::= ATTACH datab
3271e 61 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72 20  ase_kw_opt expr 
3271f 41 53 20 65 78 70 72 20 6b 65 79 5f 6f 70 74 20  AS expr key_opt 
32720 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41 74  */.{.  sqlite3At
32721 74 61 63 68 28 70 50 61 72 73 65 2c 20 79 79 6d  tach(pParse, yym
32722 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-3].minor.yy3
32723 34 36 2e 70 45 78 70 72 2c 20 79 79 6d 73 70 5b  46.pExpr, yymsp[
32724 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 34 36 2e  -1].minor.yy346.
32725 70 45 78 70 72 2c 20 79 79 6d 73 70 5b 30 5d 2e  pExpr, yymsp[0].
32726 6d 69 6e 6f 72 2e 79 79 31 33 32 29 3b 0a 7d 0a  minor.yy132);.}.
32727 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
32728 20 20 20 20 20 63 61 73 65 20 33 30 32 3a 20 2f       case 302: /
32729 2a 20 63 6d 64 20 3a 3a 3d 20 44 45 54 41 43 48  * cmd ::= DETACH
3272a 20 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74   database_kw_opt
3272b 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 73 71 6c   expr */.{.  sql
3272c 69 74 65 33 44 65 74 61 63 68 28 70 50 61 72 73  ite3Detach(pPars
3272d 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  e, yymsp[0].mino
3272e 72 2e 79 79 33 34 36 2e 70 45 78 70 72 29 3b 0a  r.yy346.pExpr);.
3272f 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32730 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 37 3a  .      case 307:
32731 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 45 49 4e   /* cmd ::= REIN
32732 44 45 58 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 52  DEX */.{sqlite3R
32733 65 69 6e 64 65 78 28 70 50 61 72 73 65 2c 20 30  eindex(pParse, 0
32734 2c 20 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  , 0);}.        b
32735 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
32736 20 33 30 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   308: /* cmd ::=
32737 20 52 45 49 4e 44 45 58 20 6e 6d 20 64 62 6e 6d   REINDEX nm dbnm
32738 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 52 65 69 6e   */.{sqlite3Rein
32739 64 65 78 28 70 50 61 72 73 65 2c 20 26 79 79 6d  dex(pParse, &yym
3273a 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
3273b 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
3273c 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20  r.yy0);}.       
3273d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
3273e 73 65 20 33 30 39 3a 20 2f 2a 20 63 6d 64 20 3a  se 309: /* cmd :
3273f 3a 3d 20 41 4e 41 4c 59 5a 45 20 2a 2f 0a 7b 73  := ANALYZE */.{s
32740 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28 70 50  qlite3Analyze(pP
32741 61 72 73 65 2c 20 30 2c 20 30 29 3b 7d 0a 20 20  arse, 0, 0);}.  
32742 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
32743 20 20 20 63 61 73 65 20 33 31 30 3a 20 2f 2a 20     case 310: /* 
32744 63 6d 64 20 3a 3a 3d 20 41 4e 41 4c 59 5a 45 20  cmd ::= ANALYZE 
32745 6e 6d 20 64 62 6e 6d 20 2a 2f 0a 7b 73 71 6c 69  nm dbnm */.{sqli
32746 74 65 33 41 6e 61 6c 79 7a 65 28 70 50 61 72 73  te3Analyze(pPars
32747 65 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  e, &yymsp[-1].mi
32748 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b  nor.yy0, &yymsp[
32749 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a  0].minor.yy0);}.
3274a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3274b 20 20 20 20 20 63 61 73 65 20 33 31 31 3a 20 2f       case 311: /
3274c 2a 20 63 6d 64 20 3a 3a 3d 20 41 4c 54 45 52 20  * cmd ::= ALTER 
3274d 54 41 42 4c 45 20 66 75 6c 6c 6e 61 6d 65 20 52  TABLE fullname R
3274e 45 4e 41 4d 45 20 54 4f 20 6e 6d 20 2a 2f 0a 7b  ENAME TO nm */.{
3274f 0a 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72 52  .  sqlite3AlterR
32750 65 6e 61 6d 65 54 61 62 6c 65 28 70 50 61 72 73  enameTable(pPars
32751 65 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  e,yymsp[-3].mino
32752 72 2e 79 79 36 35 2c 26 79 79 6d 73 70 5b 30 5d  r.yy65,&yymsp[0]
32753 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20  .minor.yy0);.}. 
32754 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32755 20 20 20 20 63 61 73 65 20 33 31 32 3a 20 2f 2a      case 312: /*
32756 20 63 6d 64 20 3a 3a 3d 20 41 4c 54 45 52 20 54   cmd ::= ALTER T
32757 41 42 4c 45 20 61 64 64 5f 63 6f 6c 75 6d 6e 5f  ABLE add_column_
32758 66 75 6c 6c 6e 61 6d 65 20 41 44 44 20 6b 77 63  fullname ADD kwc
32759 6f 6c 75 6d 6e 5f 6f 70 74 20 63 6f 6c 75 6d 6e  olumn_opt column
3275a 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41   */.{.  sqlite3A
3275b 6c 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c  lterFinishAddCol
3275c 75 6d 6e 28 70 50 61 72 73 65 2c 20 26 79 79 6d  umn(pParse, &yym
3275d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
3275e 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
3275f 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 31  k;.      case 31
32760 33 3a 20 2f 2a 20 61 64 64 5f 63 6f 6c 75 6d 6e  3: /* add_column
32761 5f 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 66 75  _fullname ::= fu
32762 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 70 50  llname */.{.  pP
32763 61 72 73 65 2d 3e 64 62 2d 3e 6c 6f 6f 6b 61 73  arse->db->lookas
32764 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 30  ide.bEnabled = 0
32765 3b 0a 20 20 73 71 6c 69 74 65 33 41 6c 74 65 72  ;.  sqlite3Alter
32766 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28 70  BeginAddColumn(p
32767 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e  Parse, yymsp[0].
32768 6d 69 6e 6f 72 2e 79 79 36 35 29 3b 0a 7d 0a 20  minor.yy65);.}. 
32769 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
3276a 20 20 20 20 63 61 73 65 20 33 31 36 3a 20 2f 2a      case 316: /*
3276b 20 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f   cmd ::= create_
3276c 76 74 61 62 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  vtab */.{sqlite3
3276d 56 74 61 62 46 69 6e 69 73 68 50 61 72 73 65 28  VtabFinishParse(
3276e 70 50 61 72 73 65 2c 30 29 3b 7d 0a 20 20 20 20  pParse,0);}.    
3276f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
32770 20 63 61 73 65 20 33 31 37 3a 20 2f 2a 20 63 6d   case 317: /* cm
32771 64 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74 61  d ::= create_vta
32772 62 20 4c 50 20 76 74 61 62 61 72 67 6c 69 73 74  b LP vtabarglist
32773 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 56   RP */.{sqlite3V
32774 74 61 62 46 69 6e 69 73 68 50 61 72 73 65 28 70  tabFinishParse(p
32775 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 30 5d 2e  Parse,&yymsp[0].
32776 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20  minor.yy0);}.   
32777 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32778 20 20 63 61 73 65 20 33 31 38 3a 20 2f 2a 20 63    case 318: /* c
32779 72 65 61 74 65 5f 76 74 61 62 20 3a 3a 3d 20 63  reate_vtab ::= c
3277a 72 65 61 74 65 6b 77 20 56 49 52 54 55 41 4c 20  reatekw VIRTUAL 
3277b 54 41 42 4c 45 20 6e 6d 20 64 62 6e 6d 20 55 53  TABLE nm dbnm US
3277c 49 4e 47 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 20 20  ING nm */.{.    
3277d 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e  sqlite3VtabBegin
3277e 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 26 79  Parse(pParse, &y
3277f 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
32780 79 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  y0, &yymsp[-2].m
32781 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70  inor.yy0, &yymsp
32782 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
32783 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
32784 0a 20 20 20 20 20 20 63 61 73 65 20 33 32 31 3a  .      case 321:
32785 20 2f 2a 20 76 74 61 62 61 72 67 20 3a 3a 3d 20   /* vtabarg ::= 
32786 2a 2f 0a 7b 73 71 6c 69 74 65 33 56 74 61 62 41  */.{sqlite3VtabA
32787 72 67 49 6e 69 74 28 70 50 61 72 73 65 29 3b 7d  rgInit(pParse);}
32788 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
32789 20 20 20 20 20 20 63 61 73 65 20 33 32 33 3a 20        case 323: 
3278a 2f 2a 20 76 74 61 62 61 72 67 74 6f 6b 65 6e 20  /* vtabargtoken 
3278b 3a 3a 3d 20 41 4e 59 20 2a 2f 0a 20 20 20 20 20  ::= ANY */.     
3278c 20 63 61 73 65 20 33 32 34 3a 20 2f 2a 20 76 74   case 324: /* vt
3278d 61 62 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20 6c  abargtoken ::= l
3278e 70 20 61 6e 79 6c 69 73 74 20 52 50 20 2a 2f 20  p anylist RP */ 
3278f 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
32790 65 6e 6f 3d 3d 33 32 34 29 3b 0a 20 20 20 20 20  eno==324);.     
32791 20 63 61 73 65 20 33 32 35 3a 20 2f 2a 20 6c 70   case 325: /* lp
32792 20 3a 3a 3d 20 4c 50 20 2a 2f 20 79 79 74 65 73   ::= LP */ yytes
32793 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
32794 33 32 35 29 3b 0a 7b 73 71 6c 69 74 65 33 56 74  325);.{sqlite3Vt
32795 61 62 41 72 67 45 78 74 65 6e 64 28 70 50 61 72  abArgExtend(pPar
32796 73 65 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  se,&yymsp[0].min
32797 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
32798 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 64    break;.      d
32799 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 2f 2a  efault:.      /*
3279a 20 28 30 29 20 69 6e 70 75 74 20 3a 3a 3d 20 63   (0) input ::= c
3279b 6d 64 6c 69 73 74 20 2a 2f 20 79 79 74 65 73 74  mdlist */ yytest
3279c 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 30  case(yyruleno==0
3279d 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 31 29 20  );.      /* (1) 
3279e 63 6d 64 6c 69 73 74 20 3a 3a 3d 20 63 6d 64 6c  cmdlist ::= cmdl
3279f 69 73 74 20 65 63 6d 64 20 2a 2f 20 79 79 74 65  ist ecmd */ yyte
327a0 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
327a1 3d 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 32  =1);.      /* (2
327a2 29 20 63 6d 64 6c 69 73 74 20 3a 3a 3d 20 65 63  ) cmdlist ::= ec
327a3 6d 64 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  md */ yytestcase
327a4 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 29 3b 0a 20  (yyruleno==2);. 
327a5 20 20 20 20 20 2f 2a 20 28 33 29 20 65 63 6d 64       /* (3) ecmd
327a6 20 3a 3a 3d 20 53 45 4d 49 20 2a 2f 20 79 79 74   ::= SEMI */ yyt
327a7 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
327a8 3d 3d 33 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  ==3);.      /* (
327a9 34 29 20 65 63 6d 64 20 3a 3a 3d 20 65 78 70 6c  4) ecmd ::= expl
327aa 61 69 6e 20 63 6d 64 78 20 53 45 4d 49 20 2a 2f  ain cmdx SEMI */
327ab 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
327ac 6c 65 6e 6f 3d 3d 34 29 3b 0a 20 20 20 20 20 20  leno==4);.      
327ad 2f 2a 20 28 31 30 29 20 74 72 61 6e 73 5f 6f 70  /* (10) trans_op
327ae 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63  t ::= */ yytestc
327af 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 30  ase(yyruleno==10
327b0 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 31 31 29  );.      /* (11)
327b1 20 74 72 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54   trans_opt ::= T
327b2 52 41 4e 53 41 43 54 49 4f 4e 20 2a 2f 20 79 79  RANSACTION */ yy
327b3 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
327b4 6f 3d 3d 31 31 29 3b 0a 20 20 20 20 20 20 2f 2a  o==11);.      /*
327b5 20 28 31 32 29 20 74 72 61 6e 73 5f 6f 70 74 20   (12) trans_opt 
327b6 3a 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f 4e 20  ::= TRANSACTION 
327b7 6e 6d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  nm */ yytestcase
327b8 28 79 79 72 75 6c 65 6e 6f 3d 3d 31 32 29 3b 0a  (yyruleno==12);.
327b9 20 20 20 20 20 20 2f 2a 20 28 32 30 29 20 73 61        /* (20) sa
327ba 76 65 70 6f 69 6e 74 5f 6f 70 74 20 3a 3a 3d 20  vepoint_opt ::= 
327bb 53 41 56 45 50 4f 49 4e 54 20 2a 2f 20 79 79 74  SAVEPOINT */ yyt
327bc 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
327bd 3d 3d 32 30 29 3b 0a 20 20 20 20 20 20 2f 2a 20  ==20);.      /* 
327be 28 32 31 29 20 73 61 76 65 70 6f 69 6e 74 5f 6f  (21) savepoint_o
327bf 70 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74  pt ::= */ yytest
327c0 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 32  case(yyruleno==2
327c1 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 32 35  1);.      /* (25
327c2 29 20 63 6d 64 20 3a 3a 3d 20 63 72 65 61 74 65  ) cmd ::= create
327c3 5f 74 61 62 6c 65 20 63 72 65 61 74 65 5f 74 61  _table create_ta
327c4 62 6c 65 5f 61 72 67 73 20 2a 2f 20 79 79 74 65  ble_args */ yyte
327c5 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d  stcase(yyruleno=
327c6 3d 32 35 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  =25);.      /* (
327c7 33 34 29 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 3a  34) columnlist :
327c8 3a 3d 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 43 4f  := columnlist CO
327c9 4d 4d 41 20 63 6f 6c 75 6d 6e 20 2a 2f 20 79 79  MMA column */ yy
327ca 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e  testcase(yyrulen
327cb 6f 3d 3d 33 34 29 3b 0a 20 20 20 20 20 20 2f 2a  o==34);.      /*
327cc 20 28 33 35 29 20 63 6f 6c 75 6d 6e 6c 69 73 74   (35) columnlist
327cd 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 20 2a 2f 20 79   ::= column */ y
327ce 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
327cf 6e 6f 3d 3d 33 35 29 3b 0a 20 20 20 20 20 20 2f  no==35);.      /
327d0 2a 20 28 34 34 29 20 74 79 70 65 20 3a 3a 3d 20  * (44) type ::= 
327d1 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
327d2 72 75 6c 65 6e 6f 3d 3d 34 34 29 3b 0a 20 20 20  ruleno==44);.   
327d3 20 20 20 2f 2a 20 28 35 31 29 20 73 69 67 6e 65     /* (51) signe
327d4 64 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 20 2a  d ::= plus_num *
327d5 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
327d6 75 6c 65 6e 6f 3d 3d 35 31 29 3b 0a 20 20 20 20  uleno==51);.    
327d7 20 20 2f 2a 20 28 35 32 29 20 73 69 67 6e 65 64    /* (52) signed
327d8 20 3a 3a 3d 20 6d 69 6e 75 73 5f 6e 75 6d 20 2a   ::= minus_num *
327d9 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
327da 75 6c 65 6e 6f 3d 3d 35 32 29 3b 0a 20 20 20 20  uleno==52);.    
327db 20 20 2f 2a 20 28 35 33 29 20 63 61 72 67 6c 69    /* (53) cargli
327dc 73 74 20 3a 3a 3d 20 63 61 72 67 6c 69 73 74 20  st ::= carglist 
327dd 63 61 72 67 20 2a 2f 20 79 79 74 65 73 74 63 61  carg */ yytestca
327de 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 35 33 29  se(yyruleno==53)
327df 3b 0a 20 20 20 20 20 20 2f 2a 20 28 35 34 29 20  ;.      /* (54) 
327e0 63 61 72 67 6c 69 73 74 20 3a 3a 3d 20 2a 2f 20  carglist ::= */ 
327e1 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
327e2 65 6e 6f 3d 3d 35 34 29 3b 0a 20 20 20 20 20 20  eno==54);.      
327e3 2f 2a 20 28 35 35 29 20 63 61 72 67 20 3a 3a 3d  /* (55) carg ::=
327e4 20 43 4f 4e 53 54 52 41 49 4e 54 20 6e 6d 20 63   CONSTRAINT nm c
327e5 63 6f 6e 73 20 2a 2f 20 79 79 74 65 73 74 63 61  cons */ yytestca
327e6 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 35 35 29  se(yyruleno==55)
327e7 3b 0a 20 20 20 20 20 20 2f 2a 20 28 35 36 29 20  ;.      /* (56) 
327e8 63 61 72 67 20 3a 3a 3d 20 63 63 6f 6e 73 20 2a  carg ::= ccons *
327e9 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
327ea 75 6c 65 6e 6f 3d 3d 35 36 29 3b 0a 20 20 20 20  uleno==56);.    
327eb 20 20 2f 2a 20 28 36 32 29 20 63 63 6f 6e 73 20    /* (62) ccons 
327ec 3a 3a 3d 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 20  ::= NULL onconf 
327ed 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79  */ yytestcase(yy
327ee 72 75 6c 65 6e 6f 3d 3d 36 32 29 3b 0a 20 20 20  ruleno==62);.   
327ef 20 20 20 2f 2a 20 28 38 39 29 20 63 6f 6e 73 6c     /* (89) consl
327f0 69 73 74 20 3a 3a 3d 20 63 6f 6e 73 6c 69 73 74  ist ::= conslist
327f1 20 43 4f 4d 4d 41 20 74 63 6f 6e 73 20 2a 2f 20   COMMA tcons */ 
327f2 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
327f3 65 6e 6f 3d 3d 38 39 29 3b 0a 20 20 20 20 20 20  eno==89);.      
327f4 2f 2a 20 28 39 30 29 20 63 6f 6e 73 6c 69 73 74  /* (90) conslist
327f5 20 3a 3a 3d 20 63 6f 6e 73 6c 69 73 74 20 74 63   ::= conslist tc
327f6 6f 6e 73 20 2a 2f 20 79 79 74 65 73 74 63 61 73  ons */ yytestcas
327f7 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 39 30 29 3b  e(yyruleno==90);
327f8 0a 20 20 20 20 20 20 2f 2a 20 28 39 31 29 20 63  .      /* (91) c
327f9 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 74 63 6f 6e  onslist ::= tcon
327fa 73 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65 28  s */ yytestcase(
327fb 79 79 72 75 6c 65 6e 6f 3d 3d 39 31 29 3b 0a 20  yyruleno==91);. 
327fc 20 20 20 20 20 2f 2a 20 28 39 32 29 20 74 63 6f       /* (92) tco
327fd 6e 73 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49 4e  ns ::= CONSTRAIN
327fe 54 20 6e 6d 20 2a 2f 20 79 79 74 65 73 74 63 61  T nm */ yytestca
327ff 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 39 32 29  se(yyruleno==92)
32800 3b 0a 20 20 20 20 20 20 2f 2a 20 28 32 36 38 29  ;.      /* (268)
32801 20 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d 20 50 4c   plus_opt ::= PL
32802 55 53 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  US */ yytestcase
32803 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 36 38 29 3b  (yyruleno==268);
32804 0a 20 20 20 20 20 20 2f 2a 20 28 32 36 39 29 20  .      /* (269) 
32805 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20  plus_opt ::= */ 
32806 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
32807 65 6e 6f 3d 3d 32 36 39 29 3b 0a 20 20 20 20 20  eno==269);.     
32808 20 2f 2a 20 28 32 37 39 29 20 66 6f 72 65 61 63   /* (279) foreac
32809 68 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 2a 2f 20  h_clause ::= */ 
3280a 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
3280b 65 6e 6f 3d 3d 32 37 39 29 3b 0a 20 20 20 20 20  eno==279);.     
3280c 20 2f 2a 20 28 32 38 30 29 20 66 6f 72 65 61 63   /* (280) foreac
3280d 68 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 46 4f 52  h_clause ::= FOR
3280e 20 45 41 43 48 20 52 4f 57 20 2a 2f 20 79 79 74   EACH ROW */ yyt
3280f 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
32810 3d 3d 32 38 30 29 3b 0a 20 20 20 20 20 20 2f 2a  ==280);.      /*
32811 20 28 32 38 37 29 20 74 72 69 64 78 62 79 20 3a   (287) tridxby :
32812 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  := */ yytestcase
32813 28 79 79 72 75 6c 65 6e 6f 3d 3d 32 38 37 29 3b  (yyruleno==287);
32814 0a 20 20 20 20 20 20 2f 2a 20 28 33 30 35 29 20  .      /* (305) 
32815 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f 70 74 20  database_kw_opt 
32816 3a 3a 3d 20 44 41 54 41 42 41 53 45 20 2a 2f 20  ::= DATABASE */ 
32817 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
32818 65 6e 6f 3d 3d 33 30 35 29 3b 0a 20 20 20 20 20  eno==305);.     
32819 20 2f 2a 20 28 33 30 36 29 20 64 61 74 61 62 61   /* (306) databa
3281a 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20 2a 2f  se_kw_opt ::= */
3281b 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75   yytestcase(yyru
3281c 6c 65 6e 6f 3d 3d 33 30 36 29 3b 0a 20 20 20 20  leno==306);.    
3281d 20 20 2f 2a 20 28 33 31 34 29 20 6b 77 63 6f 6c    /* (314) kwcol
3281e 75 6d 6e 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 20 79  umn_opt ::= */ y
3281f 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c 65  ytestcase(yyrule
32820 6e 6f 3d 3d 33 31 34 29 3b 0a 20 20 20 20 20 20  no==314);.      
32821 2f 2a 20 28 33 31 35 29 20 6b 77 63 6f 6c 75 6d  /* (315) kwcolum
32822 6e 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4c 55 4d 4e  n_opt ::= COLUMN
32823 4b 57 20 2a 2f 20 79 79 74 65 73 74 63 61 73 65  KW */ yytestcase
32824 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 31 35 29 3b  (yyruleno==315);
32825 0a 20 20 20 20 20 20 2f 2a 20 28 33 31 39 29 20  .      /* (319) 
32826 76 74 61 62 61 72 67 6c 69 73 74 20 3a 3a 3d 20  vtabarglist ::= 
32827 76 74 61 62 61 72 67 20 2a 2f 20 79 79 74 65 73  vtabarg */ yytes
32828 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d  tcase(yyruleno==
32829 33 31 39 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28  319);.      /* (
3282a 33 32 30 29 20 76 74 61 62 61 72 67 6c 69 73 74  320) vtabarglist
3282b 20 3a 3a 3d 20 76 74 61 62 61 72 67 6c 69 73 74   ::= vtabarglist
3282c 20 43 4f 4d 4d 41 20 76 74 61 62 61 72 67 20 2a   COMMA vtabarg *
3282d 2f 20 79 79 74 65 73 74 63 61 73 65 28 79 79 72  / yytestcase(yyr
3282e 75 6c 65 6e 6f 3d 3d 33 32 30 29 3b 0a 20 20 20  uleno==320);.   
3282f 20 20 20 2f 2a 20 28 33 32 32 29 20 76 74 61 62     /* (322) vtab
32830 61 72 67 20 3a 3a 3d 20 76 74 61 62 61 72 67 20  arg ::= vtabarg 
32831 76 74 61 62 61 72 67 74 6f 6b 65 6e 20 2a 2f 20  vtabargtoken */ 
32832 79 79 74 65 73 74 63 61 73 65 28 79 79 72 75 6c  yytestcase(yyrul
32833 65 6e 6f 3d 3d 33 32 32 29 3b 0a 20 20 20 20 20  eno==322);.     
32834 20 2f 2a 20 28 33 32 36 29 20 61 6e 79 6c 69 73   /* (326) anylis
32835 74 20 3a 3a 3d 20 2a 2f 20 79 79 74 65 73 74 63  t ::= */ yytestc
32836 61 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32  ase(yyruleno==32
32837 36 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 32  6);.      /* (32
32838 37 29 20 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61  7) anylist ::= a
32839 6e 79 6c 69 73 74 20 4c 50 20 61 6e 79 6c 69 73  nylist LP anylis
3283a 74 20 52 50 20 2a 2f 20 79 79 74 65 73 74 63 61  t RP */ yytestca
3283b 73 65 28 79 79 72 75 6c 65 6e 6f 3d 3d 33 32 37  se(yyruleno==327
3283c 29 3b 0a 20 20 20 20 20 20 2f 2a 20 28 33 32 38  );.      /* (328
3283d 29 20 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 61 6e  ) anylist ::= an
3283e 79 6c 69 73 74 20 41 4e 59 20 2a 2f 20 79 79 74  ylist ANY */ yyt
3283f 65 73 74 63 61 73 65 28 79 79 72 75 6c 65 6e 6f  estcase(yyruleno
32840 3d 3d 33 32 38 29 3b 0a 20 20 20 20 20 20 20 20  ==328);.        
32841 62 72 65 61 6b 3b 0a 20 20 7d 3b 0a 20 20 79 79  break;.  };.  yy
32842 67 6f 74 6f 20 3d 20 79 79 52 75 6c 65 49 6e 66  goto = yyRuleInf
32843 6f 5b 79 79 72 75 6c 65 6e 6f 5d 2e 6c 68 73 3b  o[yyruleno].lhs;
32844 0a 20 20 79 79 73 69 7a 65 20 3d 20 79 79 52 75  .  yysize = yyRu
32845 6c 65 49 6e 66 6f 5b 79 79 72 75 6c 65 6e 6f 5d  leInfo[yyruleno]
32846 2e 6e 72 68 73 3b 0a 20 20 79 79 70 50 61 72 73  .nrhs;.  yypPars
32847 65 72 2d 3e 79 79 69 64 78 20 2d 3d 20 79 79 73  er->yyidx -= yys
32848 69 7a 65 3b 0a 20 20 79 79 61 63 74 20 3d 20 79  ize;.  yyact = y
32849 79 5f 66 69 6e 64 5f 72 65 64 75 63 65 5f 61 63  y_find_reduce_ac
3284a 74 69 6f 6e 28 79 79 6d 73 70 5b 2d 79 79 73 69  tion(yymsp[-yysi
3284b 7a 65 5d 2e 73 74 61 74 65 6e 6f 2c 28 59 59 43  ze].stateno,(YYC
3284c 4f 44 45 54 59 50 45 29 79 79 67 6f 74 6f 29 3b  ODETYPE)yygoto);
3284d 0a 20 20 69 66 28 20 79 79 61 63 74 20 3c 20 59  .  if( yyact < Y
3284e 59 4e 53 54 41 54 45 20 29 7b 0a 23 69 66 64 65  YNSTATE ){.#ifde
3284f 66 20 4e 44 45 42 55 47 0a 20 20 20 20 2f 2a 20  f NDEBUG.    /* 
32850 49 66 20 77 65 20 61 72 65 20 6e 6f 74 20 64 65  If we are not de
32851 62 75 67 67 69 6e 67 20 61 6e 64 20 74 68 65 20  bugging and the 
32852 72 65 64 75 63 65 20 61 63 74 69 6f 6e 20 70 6f  reduce action po
32853 70 70 65 64 20 61 74 20 6c 65 61 73 74 0a 20 20  pped at least.  
32854 20 20 2a 2a 20 6f 6e 65 20 65 6c 65 6d 65 6e 74    ** one element
32855 20 6f 66 66 20 74 68 65 20 73 74 61 63 6b 2c 20   off the stack, 
32856 74 68 65 6e 20 77 65 20 63 61 6e 20 70 75 73 68  then we can push
32857 20 74 68 65 20 6e 65 77 20 65 6c 65 6d 65 6e 74   the new element
32858 20 62 61 63 6b 0a 20 20 20 20 2a 2a 20 6f 6e 74   back.    ** ont
32859 6f 20 74 68 65 20 73 74 61 63 6b 20 68 65 72 65  o the stack here
3285a 2c 20 61 6e 64 20 73 6b 69 70 20 74 68 65 20 73  , and skip the s
3285b 74 61 63 6b 20 6f 76 65 72 66 6c 6f 77 20 74 65  tack overflow te
3285c 73 74 20 69 6e 20 79 79 5f 73 68 69 66 74 28 29  st in yy_shift()
3285d 2e 0a 20 20 20 20 2a 2a 20 54 68 61 74 20 67 69  ..    ** That gi
3285e 76 65 73 20 61 20 73 69 67 6e 69 66 69 63 61 6e  ves a significan
3285f 74 20 73 70 65 65 64 20 69 6d 70 72 6f 76 65 6d  t speed improvem
32860 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ent. */.    if( 
32861 79 79 73 69 7a 65 20 29 7b 0a 20 20 20 20 20 20  yysize ){.      
32862 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78  yypParser->yyidx
32863 2b 2b 3b 0a 20 20 20 20 20 20 79 79 6d 73 70 20  ++;.      yymsp 
32864 2d 3d 20 79 79 73 69 7a 65 2d 31 3b 0a 20 20 20  -= yysize-1;.   
32865 20 20 20 79 79 6d 73 70 2d 3e 73 74 61 74 65 6e     yymsp->staten
32866 6f 20 3d 20 28 59 59 41 43 54 49 4f 4e 54 59 50  o = (YYACTIONTYP
32867 45 29 79 79 61 63 74 3b 0a 20 20 20 20 20 20 79  E)yyact;.      y
32868 79 6d 73 70 2d 3e 6d 61 6a 6f 72 20 3d 20 28 59  ymsp->major = (Y
32869 59 43 4f 44 45 54 59 50 45 29 79 79 67 6f 74 6f  YCODETYPE)yygoto
3286a 3b 0a 20 20 20 20 20 20 79 79 6d 73 70 2d 3e 6d  ;.      yymsp->m
3286b 69 6e 6f 72 20 3d 20 79 79 67 6f 74 6f 6d 69 6e  inor = yygotomin
3286c 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 23 65  or;.    }else.#e
3286d 6e 64 69 66 0a 20 20 20 20 7b 0a 20 20 20 20 20  ndif.    {.     
3286e 20 79 79 5f 73 68 69 66 74 28 79 79 70 50 61 72   yy_shift(yypPar
3286f 73 65 72 2c 79 79 61 63 74 2c 79 79 67 6f 74 6f  ser,yyact,yygoto
32870 2c 26 79 79 67 6f 74 6f 6d 69 6e 6f 72 29 3b 0a  ,&yygotominor);.
32871 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20      }.  }else{. 
32872 20 20 20 61 73 73 65 72 74 28 20 79 79 61 63 74     assert( yyact
32873 20 3d 3d 20 59 59 4e 53 54 41 54 45 20 2b 20 59   == YYNSTATE + Y
32874 59 4e 52 55 4c 45 20 2b 20 31 20 29 3b 0a 20 20  YNRULE + 1 );.  
32875 20 20 79 79 5f 61 63 63 65 70 74 28 79 79 70 50    yy_accept(yypP
32876 61 72 73 65 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  arser);.  }.}../
32877 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
32878 6e 67 20 63 6f 64 65 20 65 78 65 63 75 74 65 73  ng code executes
32879 20 77 68 65 6e 20 74 68 65 20 70 61 72 73 65 20   when the parse 
3287a 66 61 69 6c 73 0a 2a 2f 0a 23 69 66 6e 64 65 66  fails.*/.#ifndef
3287b 20 59 59 4e 4f 45 52 52 4f 52 52 45 43 4f 56 45   YYNOERRORRECOVE
3287c 52 59 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79  RY.static void y
3287d 79 5f 70 61 72 73 65 5f 66 61 69 6c 65 64 28 0a  y_parse_failed(.
3287e 20 20 79 79 50 61 72 73 65 72 20 2a 79 79 70 50    yyParser *yypP
3287f 61 72 73 65 72 20 20 20 20 20 20 20 20 20 20 20  arser           
32880 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f  /* The parser */
32881 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 72  .){.  sqlite3Par
32882 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 23 69  serARG_FETCH;.#i
32883 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69  fndef NDEBUG.  i
32884 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29  f( yyTraceFILE )
32885 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79 79  {.    fprintf(yy
32886 54 72 61 63 65 46 49 4c 45 2c 22 25 73 46 61 69  TraceFILE,"%sFai
32887 6c 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f  l!\n",yyTracePro
32888 6d 70 74 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  mpt);.  }.#endif
32889 0a 20 20 77 68 69 6c 65 28 20 79 79 70 50 61 72  .  while( yypPar
3288a 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20  ser->yyidx>=0 ) 
3288b 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74  yy_pop_parser_st
3288c 61 63 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a  ack(yypParser);.
3288d 20 20 2f 2a 20 48 65 72 65 20 63 6f 64 65 20 69    /* Here code i
3288e 73 20 69 6e 73 65 72 74 65 64 20 77 68 69 63 68  s inserted which
3288f 20 77 69 6c 6c 20 62 65 20 65 78 65 63 75 74 65   will be execute
32890 64 20 77 68 65 6e 65 76 65 72 20 74 68 65 0a 20  d whenever the. 
32891 20 2a 2a 20 70 61 72 73 65 72 20 66 61 69 6c 73   ** parser fails
32892 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72   */.  sqlite3Par
32893 73 65 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a  serARG_STORE; /*
32894 20 53 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e   Suppress warnin
32895 67 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20 25  g about unused %
32896 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 76  extra_argument v
32897 61 72 69 61 62 6c 65 20 2a 2f 0a 7d 0a 23 65 6e  ariable */.}.#en
32898 64 69 66 20 2f 2a 20 59 59 4e 4f 45 52 52 4f 52  dif /* YYNOERROR
32899 52 45 43 4f 56 45 52 59 20 2a 2f 0a 0a 2f 2a 0a  RECOVERY */../*.
3289a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
3289b 20 63 6f 64 65 20 65 78 65 63 75 74 65 73 20 77   code executes w
3289c 68 65 6e 20 61 20 73 79 6e 74 61 78 20 65 72 72  hen a syntax err
3289d 6f 72 20 66 69 72 73 74 20 6f 63 63 75 72 73 2e  or first occurs.
3289e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3289f 79 79 5f 73 79 6e 74 61 78 5f 65 72 72 6f 72 28  yy_syntax_error(
328a0 0a 20 20 79 79 50 61 72 73 65 72 20 2a 79 79 70  .  yyParser *yyp
328a1 50 61 72 73 65 72 2c 20 20 20 20 20 20 20 20 20  Parser,         
328a2 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
328a3 2a 2f 0a 20 20 69 6e 74 20 79 79 6d 61 6a 6f 72  */.  int yymajor
328a4 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
328a5 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72      /* The major
328a6 20 74 79 70 65 20 6f 66 20 74 68 65 20 65 72 72   type of the err
328a7 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 59 59  or token */.  YY
328a8 4d 49 4e 4f 52 54 59 50 45 20 79 79 6d 69 6e 6f  MINORTYPE yymino
328a9 72 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r            /* 
328aa 54 68 65 20 6d 69 6e 6f 72 20 74 79 70 65 20 6f  The minor type o
328ab 66 20 74 68 65 20 65 72 72 6f 72 20 74 6f 6b 65  f the error toke
328ac 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65  n */.){.  sqlite
328ad 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48  3ParserARG_FETCH
328ae 3b 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e 20  ;.#define TOKEN 
328af 28 79 79 6d 69 6e 6f 72 2e 79 79 30 29 0a 0a 20  (yyminor.yy0).. 
328b0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
328b1 52 28 79 79 6d 61 6a 6f 72 29 3b 20 20 2f 2a 20  R(yymajor);  /* 
328b2 53 69 6c 65 6e 63 65 20 73 6f 6d 65 20 63 6f 6d  Silence some com
328b3 70 69 6c 65 72 20 77 61 72 6e 69 6e 67 73 20 2a  piler warnings *
328b4 2f 0a 20 20 61 73 73 65 72 74 28 20 54 4f 4b 45  /.  assert( TOKE
328b5 4e 2e 7a 5b 30 5d 20 29 3b 20 20 2f 2a 20 54 68  N.z[0] );  /* Th
328b6 65 20 74 6f 6b 65 6e 69 7a 65 72 20 61 6c 77 61  e tokenizer alwa
328b7 79 73 20 67 69 76 65 73 20 75 73 20 61 20 74 6f  ys gives us a to
328b8 6b 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ken */.  sqlite3
328b9 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
328ba 20 22 6e 65 61 72 20 5c 22 25 54 5c 22 3a 20 73   "near \"%T\": s
328bb 79 6e 74 61 78 20 65 72 72 6f 72 22 2c 20 26 54  yntax error", &T
328bc 4f 4b 45 4e 29 3b 0a 20 20 70 50 61 72 73 65 2d  OKEN);.  pParse-
328bd 3e 70 61 72 73 65 45 72 72 6f 72 20 3d 20 31 3b  >parseError = 1;
328be 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
328bf 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75  ARG_STORE; /* Su
328c0 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61  ppress warning a
328c1 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78 74  bout unused %ext
328c2 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 69  ra_argument vari
328c3 61 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a  able */.}../*.**
328c4 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   The following i
328c5 73 20 65 78 65 63 75 74 65 64 20 77 68 65 6e 20  s executed when 
328c6 74 68 65 20 70 61 72 73 65 72 20 61 63 63 65 70  the parser accep
328c7 74 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ts.*/.static voi
328c8 64 20 79 79 5f 61 63 63 65 70 74 28 0a 20 20 79  d yy_accept(.  y
328c9 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73  yParser *yypPars
328ca 65 72 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  er           /* 
328cb 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a 29 7b  The parser */.){
328cc 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72  .  sqlite3Parser
328cd 41 52 47 5f 46 45 54 43 48 3b 0a 23 69 66 6e 64  ARG_FETCH;.#ifnd
328ce 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20  ef NDEBUG.  if( 
328cf 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20  yyTraceFILE ){. 
328d0 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
328d1 63 65 46 49 4c 45 2c 22 25 73 41 63 63 65 70 74  ceFILE,"%sAccept
328d2 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d  !\n",yyTraceProm
328d3 70 74 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  pt);.  }.#endif.
328d4 20 20 77 68 69 6c 65 28 20 79 79 70 50 61 72 73    while( yypPars
328d5 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79  er->yyidx>=0 ) y
328d6 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61  y_pop_parser_sta
328d7 63 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20  ck(yypParser);. 
328d8 20 2f 2a 20 48 65 72 65 20 63 6f 64 65 20 69 73   /* Here code is
328d9 20 69 6e 73 65 72 74 65 64 20 77 68 69 63 68 20   inserted which 
328da 77 69 6c 6c 20 62 65 20 65 78 65 63 75 74 65 64  will be executed
328db 20 77 68 65 6e 65 76 65 72 20 74 68 65 0a 20 20   whenever the.  
328dc 2a 2a 20 70 61 72 73 65 72 20 61 63 63 65 70 74  ** parser accept
328dd 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61  s */.  sqlite3Pa
328de 72 73 65 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f  rserARG_STORE; /
328df 2a 20 53 75 70 70 72 65 73 73 20 77 61 72 6e 69  * Suppress warni
328e0 6e 67 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20  ng about unused 
328e1 25 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20  %extra_argument 
328e2 76 61 72 69 61 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f  variable */.}../
328e3 2a 20 54 68 65 20 6d 61 69 6e 20 70 61 72 73 65  * The main parse
328e4 72 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 54 68  r program..** Th
328e5 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
328e6 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
328e7 20 61 20 73 74 72 75 63 74 75 72 65 20 6f 62 74   a structure obt
328e8 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 22 73  ained from.** "s
328e9 71 6c 69 74 65 33 50 61 72 73 65 72 41 6c 6c 6f  qlite3ParserAllo
328ea 63 22 20 77 68 69 63 68 20 64 65 73 63 72 69 62  c" which describ
328eb 65 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  es the current s
328ec 74 61 74 65 20 6f 66 20 74 68 65 20 70 61 72 73  tate of the pars
328ed 65 72 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e  er..** The secon
328ee 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  d argument is th
328ef 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75  e major token nu
328f0 6d 62 65 72 2e 20 20 54 68 65 20 74 68 69 72 64  mber.  The third
328f1 20 69 73 0a 2a 2a 20 74 68 65 20 6d 69 6e 6f 72   is.** the minor
328f2 20 74 6f 6b 65 6e 2e 20 20 54 68 65 20 66 6f 75   token.  The fou
328f3 72 74 68 20 6f 70 74 69 6f 6e 61 6c 20 61 72 67  rth optional arg
328f4 75 6d 65 6e 74 20 69 73 20 77 68 61 74 65 76 65  ument is whateve
328f5 72 20 74 68 65 0a 2a 2a 20 75 73 65 72 20 77 61  r the.** user wa
328f6 6e 74 73 20 28 61 6e 64 20 73 70 65 63 69 66 69  nts (and specifi
328f7 65 64 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61  ed in the gramma
328f8 72 29 20 61 6e 64 20 69 73 20 61 76 61 69 6c 61  r) and is availa
328f9 62 6c 65 20 66 6f 72 0a 2a 2a 20 75 73 65 20 62  ble for.** use b
328fa 79 20 74 68 65 20 61 63 74 69 6f 6e 20 72 6f 75  y the action rou
328fb 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70  tines..**.** Inp
328fc 75 74 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20  uts:.** <ul>.** 
328fd 3c 6c 69 3e 20 41 20 70 6f 69 6e 74 65 72 20 74  <li> A pointer t
328fe 6f 20 74 68 65 20 70 61 72 73 65 72 20 28 61 6e  o the parser (an
328ff 20 6f 70 61 71 75 65 20 73 74 72 75 63 74 75 72   opaque structur
32900 65 2e 29 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20  e.).** <li> The 
32901 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62  major token numb
32902 65 72 2e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20  er..** <li> The 
32903 6d 69 6e 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62  minor token numb
32904 65 72 2e 0a 2a 2a 20 3c 6c 69 3e 20 41 6e 20 6f  er..** <li> An o
32905 70 74 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f  ption argument o
32906 66 20 61 20 67 72 61 6d 6d 61 72 2d 73 70 65 63  f a grammar-spec
32907 69 66 69 65 64 20 74 79 70 65 2e 0a 2a 2a 20 3c  ified type..** <
32908 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75  /ul>.**.** Outpu
32909 74 73 3a 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a  ts:.** None..*/.
3290a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
3290b 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65  oid sqlite3Parse
3290c 72 28 0a 20 20 76 6f 69 64 20 2a 79 79 70 2c 20  r(.  void *yyp, 
3290d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3290e 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
3290f 2a 2f 0a 20 20 69 6e 74 20 79 79 6d 61 6a 6f 72  */.  int yymajor
32910 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
32911 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74    /* The major t
32912 6f 6b 65 6e 20 63 6f 64 65 20 6e 75 6d 62 65 72  oken code number
32913 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72   */.  sqlite3Par
32914 73 65 72 54 4f 4b 45 4e 54 59 50 45 20 79 79 6d  serTOKENTYPE yym
32915 69 6e 6f 72 20 20 20 20 20 20 20 2f 2a 20 54 68  inor       /* Th
32916 65 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  e value for the 
32917 74 6f 6b 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 74  token */.  sqlit
32918 65 33 50 61 72 73 65 72 41 52 47 5f 50 44 45 43  e3ParserARG_PDEC
32919 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  L               
3291a 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 25 65 78 74  /* Optional %ext
3291b 72 61 5f 61 72 67 75 6d 65 6e 74 20 70 61 72 61  ra_argument para
3291c 6d 65 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 59 59  meter */.){.  YY
3291d 4d 49 4e 4f 52 54 59 50 45 20 79 79 6d 69 6e 6f  MINORTYPE yymino
3291e 72 75 6e 69 6f 6e 3b 0a 20 20 69 6e 74 20 79 79  runion;.  int yy
3291f 61 63 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  act;            
32920 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 61 63  /* The parser ac
32921 74 69 6f 6e 2e 20 2a 2f 0a 20 20 69 6e 74 20 79  tion. */.  int y
32922 79 65 6e 64 6f 66 69 6e 70 75 74 3b 20 20 20 20  yendofinput;    
32923 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 61   /* True if we a
32924 72 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  re at the end of
32925 20 69 6e 70 75 74 20 2a 2f 0a 23 69 66 64 65 66   input */.#ifdef
32926 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20   YYERRORSYMBOL. 
32927 20 69 6e 74 20 79 79 65 72 72 6f 72 68 69 74 20   int yyerrorhit 
32928 3d 20 30 3b 20 20 20 2f 2a 20 54 72 75 65 20 69  = 0;   /* True i
32929 66 20 79 79 6d 61 6a 6f 72 20 68 61 73 20 69 6e  f yymajor has in
3292a 76 6f 6b 65 64 20 61 6e 20 65 72 72 6f 72 20 2a  voked an error *
3292b 2f 0a 23 65 6e 64 69 66 0a 20 20 79 79 50 61 72  /.#endif.  yyPar
3292c 73 65 72 20 2a 79 79 70 50 61 72 73 65 72 3b 20  ser *yypParser; 
3292d 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a   /* The parser *
3292e 2f 0a 0a 20 20 2f 2a 20 28 72 65 29 69 6e 69 74  /..  /* (re)init
3292f 69 61 6c 69 7a 65 20 74 68 65 20 70 61 72 73 65  ialize the parse
32930 72 2c 20 69 66 20 6e 65 63 65 73 73 61 72 79 20  r, if necessary 
32931 2a 2f 0a 20 20 79 79 70 50 61 72 73 65 72 20 3d  */.  yypParser =
32932 20 28 79 79 50 61 72 73 65 72 2a 29 79 79 70 3b   (yyParser*)yyp;
32933 0a 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72  .  if( yypParser
32934 2d 3e 79 79 69 64 78 3c 30 20 29 7b 0a 23 69 66  ->yyidx<0 ){.#if
32935 20 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30   YYSTACKDEPTH<=0
32936 0a 20 20 20 20 69 66 28 20 79 79 70 50 61 72 73  .    if( yypPars
32937 65 72 2d 3e 79 79 73 74 6b 73 7a 20 3c 3d 30 20  er->yystksz <=0 
32938 29 7b 0a 20 20 20 20 20 20 2f 2a 6d 65 6d 73 65  ){.      /*memse
32939 74 28 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 2c  t(&yyminorunion,
3293a 20 30 2c 20 73 69 7a 65 6f 66 28 79 79 6d 69 6e   0, sizeof(yymin
3293b 6f 72 75 6e 69 6f 6e 29 29 3b 2a 2f 0a 20 20 20  orunion));*/.   
3293c 20 20 20 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 20     yyminorunion 
3293d 3d 20 79 79 7a 65 72 6f 6d 69 6e 6f 72 3b 0a 20  = yyzerominor;. 
3293e 20 20 20 20 20 79 79 53 74 61 63 6b 4f 76 65 72       yyStackOver
3293f 66 6c 6f 77 28 79 79 70 50 61 72 73 65 72 2c 20  flow(yypParser, 
32940 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a  &yyminorunion);.
32941 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
32942 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 79    }.#endif.    y
32943 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 20  ypParser->yyidx 
32944 3d 20 30 3b 0a 20 20 20 20 79 79 70 50 61 72 73  = 0;.    yypPars
32945 65 72 2d 3e 79 79 65 72 72 63 6e 74 20 3d 20 2d  er->yyerrcnt = -
32946 31 3b 0a 20 20 20 20 79 79 70 50 61 72 73 65 72  1;.    yypParser
32947 2d 3e 79 79 73 74 61 63 6b 5b 30 5d 2e 73 74 61  ->yystack[0].sta
32948 74 65 6e 6f 20 3d 20 30 3b 0a 20 20 20 20 79 79  teno = 0;.    yy
32949 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b  pParser->yystack
3294a 5b 30 5d 2e 6d 61 6a 6f 72 20 3d 20 30 3b 0a 20  [0].major = 0;. 
3294b 20 7d 0a 20 20 79 79 6d 69 6e 6f 72 75 6e 69 6f   }.  yyminorunio
3294c 6e 2e 79 79 30 20 3d 20 79 79 6d 69 6e 6f 72 3b  n.yy0 = yyminor;
3294d 0a 20 20 79 79 65 6e 64 6f 66 69 6e 70 75 74 20  .  yyendofinput 
3294e 3d 20 28 79 79 6d 61 6a 6f 72 3d 3d 30 29 3b 0a  = (yymajor==0);.
3294f 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
32950 52 47 5f 53 54 4f 52 45 3b 0a 0a 23 69 66 6e 64  RG_STORE;..#ifnd
32951 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20  ef NDEBUG.  if( 
32952 79 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20  yyTraceFILE ){. 
32953 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
32954 63 65 46 49 4c 45 2c 22 25 73 49 6e 70 75 74 20  ceFILE,"%sInput 
32955 25 73 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f  %s\n",yyTracePro
32956 6d 70 74 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b  mpt,yyTokenName[
32957 79 79 6d 61 6a 6f 72 5d 29 3b 0a 20 20 7d 0a 23  yymajor]);.  }.#
32958 65 6e 64 69 66 0a 0a 20 20 64 6f 7b 0a 20 20 20  endif..  do{.   
32959 20 79 79 61 63 74 20 3d 20 79 79 5f 66 69 6e 64   yyact = yy_find
3295a 5f 73 68 69 66 74 5f 61 63 74 69 6f 6e 28 79 79  _shift_action(yy
3295b 70 50 61 72 73 65 72 2c 28 59 59 43 4f 44 45 54  pParser,(YYCODET
3295c 59 50 45 29 79 79 6d 61 6a 6f 72 29 3b 0a 20 20  YPE)yymajor);.  
3295d 20 20 69 66 28 20 79 79 61 63 74 3c 59 59 4e 53    if( yyact<YYNS
3295e 54 41 54 45 20 29 7b 0a 20 20 20 20 20 20 61 73  TATE ){.      as
3295f 73 65 72 74 28 20 21 79 79 65 6e 64 6f 66 69 6e  sert( !yyendofin
32960 70 75 74 20 29 3b 20 20 2f 2a 20 49 6d 70 6f 73  put );  /* Impos
32961 73 69 62 6c 65 20 74 6f 20 73 68 69 66 74 20 74  sible to shift t
32962 68 65 20 24 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20  he $ token */.  
32963 20 20 20 20 79 79 5f 73 68 69 66 74 28 79 79 70      yy_shift(yyp
32964 50 61 72 73 65 72 2c 79 79 61 63 74 2c 79 79 6d  Parser,yyact,yym
32965 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69  ajor,&yyminoruni
32966 6f 6e 29 3b 0a 20 20 20 20 20 20 79 79 70 50 61  on);.      yypPa
32967 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 2d 2d  rser->yyerrcnt--
32968 3b 0a 20 20 20 20 20 20 79 79 6d 61 6a 6f 72 20  ;.      yymajor 
32969 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a 20 20 20 20  = YYNOCODE;.    
3296a 7d 65 6c 73 65 20 69 66 28 20 79 79 61 63 74 20  }else if( yyact 
3296b 3c 20 59 59 4e 53 54 41 54 45 20 2b 20 59 59 4e  < YYNSTATE + YYN
3296c 52 55 4c 45 20 29 7b 0a 20 20 20 20 20 20 79 79  RULE ){.      yy
3296d 5f 72 65 64 75 63 65 28 79 79 70 50 61 72 73 65  _reduce(yypParse
3296e 72 2c 79 79 61 63 74 2d 59 59 4e 53 54 41 54 45  r,yyact-YYNSTATE
3296f 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
32970 20 20 20 20 61 73 73 65 72 74 28 20 79 79 61 63      assert( yyac
32971 74 20 3d 3d 20 59 59 5f 45 52 52 4f 52 5f 41 43  t == YY_ERROR_AC
32972 54 49 4f 4e 20 29 3b 0a 23 69 66 64 65 66 20 59  TION );.#ifdef Y
32973 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 20  YERRORSYMBOL.   
32974 20 20 20 69 6e 74 20 79 79 6d 78 3b 0a 23 65 6e     int yymx;.#en
32975 64 69 66 0a 23 69 66 6e 64 65 66 20 4e 44 45 42  dif.#ifndef NDEB
32976 55 47 0a 20 20 20 20 20 20 69 66 28 20 79 79 54  UG.      if( yyT
32977 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20  raceFILE ){.    
32978 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72      fprintf(yyTr
32979 61 63 65 46 49 4c 45 2c 22 25 73 53 79 6e 74 61  aceFILE,"%sSynta
3297a 78 20 45 72 72 6f 72 21 5c 6e 22 2c 79 79 54 72  x Error!\n",yyTr
3297b 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20 20  acePrompt);.    
3297c 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 64 65    }.#endif.#ifde
3297d 66 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a  f YYERRORSYMBOL.
3297e 20 20 20 20 20 20 2f 2a 20 41 20 73 79 6e 74 61        /* A synta
3297f 78 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63 75  x error has occu
32980 72 72 65 64 2e 0a 20 20 20 20 20 20 2a 2a 20 54  rred..      ** T
32981 68 65 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 61  he response to a
32982 6e 20 65 72 72 6f 72 20 64 65 70 65 6e 64 73 20  n error depends 
32983 75 70 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20  upon whether or 
32984 6e 6f 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a  not the.      **
32985 20 67 72 61 6d 6d 61 72 20 64 65 66 69 6e 65 73   grammar defines
32986 20 61 6e 20 65 72 72 6f 72 20 74 6f 6b 65 6e 20   an error token 
32987 22 45 52 52 4f 52 22 2e 20 20 0a 20 20 20 20 20  "ERROR".  .     
32988 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68 69   **.      ** Thi
32989 73 20 69 73 20 77 68 61 74 20 77 65 20 64 6f 20  s is what we do 
3298a 69 66 20 74 68 65 20 67 72 61 6d 6d 61 72 20 64  if the grammar d
3298b 6f 65 73 20 64 65 66 69 6e 65 20 45 52 52 4f 52  oes define ERROR
3298c 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  :.      **.     
3298d 20 2a 2a 20 20 2a 20 43 61 6c 6c 20 74 68 65 20   **  * Call the 
3298e 25 73 79 6e 74 61 78 5f 65 72 72 6f 72 20 66 75  %syntax_error fu
3298f 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2a  nction..      **
32990 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 42 65 67  .      **  * Beg
32991 69 6e 20 70 6f 70 70 69 6e 67 20 74 68 65 20 73  in popping the s
32992 74 61 63 6b 20 75 6e 74 69 6c 20 77 65 20 65 6e  tack until we en
32993 74 65 72 20 61 20 73 74 61 74 65 20 77 68 65 72  ter a state wher
32994 65 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 69 74  e.      **    it
32995 20 69 73 20 6c 65 67 61 6c 20 74 6f 20 73 68 69   is legal to shi
32996 66 74 20 74 68 65 20 65 72 72 6f 72 20 73 79 6d  ft the error sym
32997 62 6f 6c 2c 20 74 68 65 6e 20 73 68 69 66 74 0a  bol, then shift.
32998 20 20 20 20 20 20 2a 2a 20 20 20 20 74 68 65 20        **    the 
32999 65 72 72 6f 72 20 73 79 6d 62 6f 6c 2e 0a 20 20  error symbol..  
3299a 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
3299b 20 2a 20 53 65 74 20 74 68 65 20 65 72 72 6f 72   * Set the error
3299c 20 63 6f 75 6e 74 20 74 6f 20 74 68 72 65 65 2e   count to three.
3299d 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
3299e 2a 2a 20 20 2a 20 42 65 67 69 6e 20 61 63 63 65  **  * Begin acce
3299f 70 74 69 6e 67 20 61 6e 64 20 73 68 69 66 74 69  pting and shifti
329a0 6e 67 20 6e 65 77 20 74 6f 6b 65 6e 73 2e 20 20  ng new tokens.  
329a1 4e 6f 20 6e 65 77 20 65 72 72 6f 72 0a 20 20 20  No new error.   
329a2 20 20 20 2a 2a 20 20 20 20 70 72 6f 63 65 73 73     **    process
329a3 69 6e 67 20 77 69 6c 6c 20 6f 63 63 75 72 20 75  ing will occur u
329a4 6e 74 69 6c 20 74 68 72 65 65 20 74 6f 6b 65 6e  ntil three token
329a5 73 20 68 61 76 65 20 62 65 65 6e 0a 20 20 20 20  s have been.    
329a6 20 20 2a 2a 20 20 20 20 73 68 69 66 74 65 64 20    **    shifted 
329a7 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 0a 20 20  successfully..  
329a8 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2f 0a      **.      */.
329a9 20 20 20 20 20 20 69 66 28 20 79 79 70 50 61 72        if( yypPar
329aa 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 3c 30 20  ser->yyerrcnt<0 
329ab 29 7b 0a 20 20 20 20 20 20 20 20 79 79 5f 73 79  ){.        yy_sy
329ac 6e 74 61 78 5f 65 72 72 6f 72 28 79 79 70 50 61  ntax_error(yypPa
329ad 72 73 65 72 2c 79 79 6d 61 6a 6f 72 2c 79 79 6d  rser,yymajor,yym
329ae 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20  inorunion);.    
329af 20 20 7d 0a 20 20 20 20 20 20 79 79 6d 78 20 3d    }.      yymx =
329b0 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74   yypParser->yyst
329b1 61 63 6b 5b 79 79 70 50 61 72 73 65 72 2d 3e 79  ack[yypParser->y
329b2 79 69 64 78 5d 2e 6d 61 6a 6f 72 3b 0a 20 20 20  yidx].major;.   
329b3 20 20 20 69 66 28 20 79 79 6d 78 3d 3d 59 59 45     if( yymx==YYE
329b4 52 52 4f 52 53 59 4d 42 4f 4c 20 7c 7c 20 79 79  RRORSYMBOL || yy
329b5 65 72 72 6f 72 68 69 74 20 29 7b 0a 23 69 66 6e  errorhit ){.#ifn
329b6 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20 20 20  def NDEBUG.     
329b7 20 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49     if( yyTraceFI
329b8 4c 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  LE ){.          
329b9 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46  fprintf(yyTraceF
329ba 49 4c 45 2c 22 25 73 44 69 73 63 61 72 64 20 69  ILE,"%sDiscard i
329bb 6e 70 75 74 20 74 6f 6b 65 6e 20 25 73 5c 6e 22  nput token %s\n"
329bc 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 79  ,.             y
329bd 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 79 79 54  yTracePrompt,yyT
329be 6f 6b 65 6e 4e 61 6d 65 5b 79 79 6d 61 6a 6f 72  okenName[yymajor
329bf 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65  ]);.        }.#e
329c0 6e 64 69 66 0a 20 20 20 20 20 20 20 20 79 79 5f  ndif.        yy_
329c1 64 65 73 74 72 75 63 74 6f 72 28 79 79 70 50 61  destructor(yypPa
329c2 72 73 65 72 2c 20 28 59 59 43 4f 44 45 54 59 50  rser, (YYCODETYP
329c3 45 29 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e  E)yymajor,&yymin
329c4 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20  orunion);.      
329c5 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f    yymajor = YYNO
329c6 43 4f 44 45 3b 0a 20 20 20 20 20 20 7d 65 6c 73  CODE;.      }els
329c7 65 7b 0a 20 20 20 20 20 20 20 20 20 77 68 69 6c  e{.         whil
329c8 65 28 0a 20 20 20 20 20 20 20 20 20 20 79 79 70  e(.          yyp
329c9 50 61 72 73 65 72 2d 3e 79 79 69 64 78 20 3e 3d  Parser->yyidx >=
329ca 20 30 20 26 26 0a 20 20 20 20 20 20 20 20 20 20   0 &&.          
329cb 79 79 6d 78 20 21 3d 20 59 59 45 52 52 4f 52 53  yymx != YYERRORS
329cc 59 4d 42 4f 4c 20 26 26 0a 20 20 20 20 20 20 20  YMBOL &&.       
329cd 20 20 20 28 79 79 61 63 74 20 3d 20 79 79 5f 66     (yyact = yy_f
329ce 69 6e 64 5f 72 65 64 75 63 65 5f 61 63 74 69 6f  ind_reduce_actio
329cf 6e 28 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n(.             
329d0 20 20 20 20 20 20 20 20 20 20 20 79 79 70 50 61             yypPa
329d1 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79  rser->yystack[yy
329d2 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d 2e  pParser->yyidx].
329d3 73 74 61 74 65 6e 6f 2c 0a 20 20 20 20 20 20 20  stateno,.       
329d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
329d5 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 29 29   YYERRORSYMBOL))
329d6 20 3e 3d 20 59 59 4e 53 54 41 54 45 0a 20 20 20   >= YYNSTATE.   
329d7 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
329d8 20 20 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f    yy_pop_parser_
329d9 73 74 61 63 6b 28 79 79 70 50 61 72 73 65 72 29  stack(yypParser)
329da 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
329db 20 20 20 20 69 66 28 20 79 79 70 50 61 72 73 65      if( yypParse
329dc 72 2d 3e 79 79 69 64 78 20 3c 20 30 20 7c 7c 20  r->yyidx < 0 || 
329dd 79 79 6d 61 6a 6f 72 3d 3d 30 20 29 7b 0a 20 20  yymajor==0 ){.  
329de 20 20 20 20 20 20 20 20 79 79 5f 64 65 73 74 72          yy_destr
329df 75 63 74 6f 72 28 79 79 70 50 61 72 73 65 72 2c  uctor(yypParser,
329e0 28 59 59 43 4f 44 45 54 59 50 45 29 79 79 6d 61  (YYCODETYPE)yyma
329e1 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f  jor,&yyminorunio
329e2 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 79 79  n);.          yy
329e3 5f 70 61 72 73 65 5f 66 61 69 6c 65 64 28 79 79  _parse_failed(yy
329e4 70 50 61 72 73 65 72 29 3b 0a 20 20 20 20 20 20  pParser);.      
329e5 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59      yymajor = YY
329e6 4e 4f 43 4f 44 45 3b 0a 20 20 20 20 20 20 20 20  NOCODE;.        
329e7 7d 65 6c 73 65 20 69 66 28 20 79 79 6d 78 21 3d  }else if( yymx!=
329e8 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 20 29 7b  YYERRORSYMBOL ){
329e9 0a 20 20 20 20 20 20 20 20 20 20 59 59 4d 49 4e  .          YYMIN
329ea 4f 52 54 59 50 45 20 75 32 3b 0a 20 20 20 20 20  ORTYPE u2;.     
329eb 20 20 20 20 20 75 32 2e 59 59 45 52 52 53 59 4d       u2.YYERRSYM
329ec 44 54 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  DT = 0;.        
329ed 20 20 79 79 5f 73 68 69 66 74 28 79 79 70 50 61    yy_shift(yypPa
329ee 72 73 65 72 2c 79 79 61 63 74 2c 59 59 45 52 52  rser,yyact,YYERR
329ef 4f 52 53 59 4d 42 4f 4c 2c 26 75 32 29 3b 0a 20  ORSYMBOL,&u2);. 
329f0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
329f1 0a 20 20 20 20 20 20 79 79 70 50 61 72 73 65 72  .      yypParser
329f2 2d 3e 79 79 65 72 72 63 6e 74 20 3d 20 33 3b 0a  ->yyerrcnt = 3;.
329f3 20 20 20 20 20 20 79 79 65 72 72 6f 72 68 69 74        yyerrorhit
329f4 20 3d 20 31 3b 0a 23 65 6c 69 66 20 64 65 66 69   = 1;.#elif defi
329f5 6e 65 64 28 59 59 4e 4f 45 52 52 4f 52 52 45 43  ned(YYNOERRORREC
329f6 4f 56 45 52 59 29 0a 20 20 20 20 20 20 2f 2a 20  OVERY).      /* 
329f7 49 66 20 74 68 65 20 59 59 4e 4f 45 52 52 4f 52  If the YYNOERROR
329f8 52 45 43 4f 56 45 52 59 20 6d 61 63 72 6f 20 69  RECOVERY macro i
329f9 73 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20  s defined, then 
329fa 64 6f 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74  do not attempt t
329fb 6f 0a 20 20 20 20 20 20 2a 2a 20 64 6f 20 61 6e  o.      ** do an
329fc 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 20  y kind of error 
329fd 72 65 63 6f 76 65 72 79 2e 20 20 49 6e 73 74 65  recovery.  Inste
329fe 61 64 2c 20 73 69 6d 70 6c 79 20 69 6e 76 6f 6b  ad, simply invok
329ff 65 20 74 68 65 20 73 79 6e 74 61 78 0a 20 20 20  e the syntax.   
32a00 20 20 20 2a 2a 20 65 72 72 6f 72 20 72 6f 75 74     ** error rout
32a01 69 6e 65 20 61 6e 64 20 63 6f 6e 74 69 6e 75 65  ine and continue
32a02 20 67 6f 69 6e 67 20 61 73 20 69 66 20 6e 6f 74   going as if not
32a03 68 69 6e 67 20 68 61 64 20 68 61 70 70 65 6e 65  hing had happene
32a04 64 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  d..      **.    
32a05 20 20 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f 6e    ** Application
32a06 73 20 63 61 6e 20 73 65 74 20 74 68 69 73 20 6d  s can set this m
32a07 61 63 72 6f 20 28 66 6f 72 20 65 78 61 6d 70 6c  acro (for exampl
32a08 65 20 69 6e 73 69 64 65 20 25 69 6e 63 6c 75 64  e inside %includ
32a09 65 29 20 69 66 0a 20 20 20 20 20 20 2a 2a 20 74  e) if.      ** t
32a0a 68 65 79 20 69 6e 74 65 6e 64 20 74 6f 20 61 62  hey intend to ab
32a0b 61 6e 64 6f 6e 20 74 68 65 20 70 61 72 73 65 20  andon the parse 
32a0c 75 70 6f 6e 20 74 68 65 20 66 69 72 73 74 20 73  upon the first s
32a0d 79 6e 74 61 78 20 65 72 72 6f 72 20 73 65 65 6e  yntax error seen
32a0e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
32a0f 20 79 79 5f 73 79 6e 74 61 78 5f 65 72 72 6f 72   yy_syntax_error
32a10 28 79 79 70 50 61 72 73 65 72 2c 79 79 6d 61 6a  (yypParser,yymaj
32a11 6f 72 2c 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29  or,yyminorunion)
32a12 3b 0a 20 20 20 20 20 20 79 79 5f 64 65 73 74 72  ;.      yy_destr
32a13 75 63 74 6f 72 28 79 79 70 50 61 72 73 65 72 2c  uctor(yypParser,
32a14 28 59 59 43 4f 44 45 54 59 50 45 29 79 79 6d 61  (YYCODETYPE)yyma
32a15 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75 6e 69 6f  jor,&yyminorunio
32a16 6e 29 3b 0a 20 20 20 20 20 20 79 79 6d 61 6a 6f  n);.      yymajo
32a17 72 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a 20 20  r = YYNOCODE;.  
32a18 20 20 20 20 0a 23 65 6c 73 65 20 20 2f 2a 20 59      .#else  /* Y
32a19 59 45 52 52 4f 52 53 59 4d 42 4f 4c 20 69 73 20  YERRORSYMBOL is 
32a1a 6e 6f 74 20 64 65 66 69 6e 65 64 20 2a 2f 0a 20  not defined */. 
32a1b 20 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20       /* This is 
32a1c 77 68 61 74 20 77 65 20 64 6f 20 69 66 20 74 68  what we do if th
32a1d 65 20 67 72 61 6d 6d 61 72 20 64 6f 65 73 20 6e  e grammar does n
32a1e 6f 74 20 64 65 66 69 6e 65 20 45 52 52 4f 52 3a  ot define ERROR:
32a1f 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
32a20 2a 2a 20 20 2a 20 52 65 70 6f 72 74 20 61 6e 20  **  * Report an 
32a21 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2c 20 61  error message, a
32a22 6e 64 20 74 68 72 6f 77 20 61 77 61 79 20 74 68  nd throw away th
32a23 65 20 69 6e 70 75 74 20 74 6f 6b 65 6e 2e 0a 20  e input token.. 
32a24 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
32a25 20 20 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74    * If the input
32a26 20 74 6f 6b 65 6e 20 69 73 20 24 2c 20 74 68 65   token is $, the
32a27 6e 20 66 61 69 6c 20 74 68 65 20 70 61 72 73 65  n fail the parse
32a28 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20  ..      **.     
32a29 20 2a 2a 20 41 73 20 62 65 66 6f 72 65 2c 20 73   ** As before, s
32a2a 75 62 73 65 71 75 65 6e 74 20 65 72 72 6f 72 20  ubsequent error 
32a2b 6d 65 73 73 61 67 65 73 20 61 72 65 20 73 75 70  messages are sup
32a2c 70 72 65 73 73 65 64 20 75 6e 74 69 6c 0a 20 20  pressed until.  
32a2d 20 20 20 20 2a 2a 20 74 68 72 65 65 20 69 6e 70      ** three inp
32a2e 75 74 20 74 6f 6b 65 6e 73 20 68 61 76 65 20 62  ut tokens have b
32a2f 65 65 6e 20 73 75 63 63 65 73 73 66 75 6c 6c 79  een successfully
32a30 20 73 68 69 66 74 65 64 2e 0a 20 20 20 20 20 20   shifted..      
32a31 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 79 79 70  */.      if( yyp
32a32 50 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74  Parser->yyerrcnt
32a33 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 79  <=0 ){.        y
32a34 79 5f 73 79 6e 74 61 78 5f 65 72 72 6f 72 28 79  y_syntax_error(y
32a35 79 70 50 61 72 73 65 72 2c 79 79 6d 61 6a 6f 72  ypParser,yymajor
32a36 2c 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a  ,yyminorunion);.
32a37 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 79 79        }.      yy
32a38 70 50 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e  pParser->yyerrcn
32a39 74 20 3d 20 33 3b 0a 20 20 20 20 20 20 79 79 5f  t = 3;.      yy_
32a3a 64 65 73 74 72 75 63 74 6f 72 28 79 79 70 50 61  destructor(yypPa
32a3b 72 73 65 72 2c 28 59 59 43 4f 44 45 54 59 50 45  rser,(YYCODETYPE
32a3c 29 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f  )yymajor,&yymino
32a3d 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 69  runion);.      i
32a3e 66 28 20 79 79 65 6e 64 6f 66 69 6e 70 75 74 20  f( yyendofinput 
32a3f 29 7b 0a 20 20 20 20 20 20 20 20 79 79 5f 70 61  ){.        yy_pa
32a40 72 73 65 5f 66 61 69 6c 65 64 28 79 79 70 50 61  rse_failed(yypPa
32a41 72 73 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  rser);.      }. 
32a42 20 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59       yymajor = Y
32a43 59 4e 4f 43 4f 44 45 3b 0a 23 65 6e 64 69 66 0a  YNOCODE;.#endif.
32a44 20 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20      }.  }while( 
32a45 79 79 6d 61 6a 6f 72 21 3d 59 59 4e 4f 43 4f 44  yymajor!=YYNOCOD
32a46 45 20 26 26 20 79 79 70 50 61 72 73 65 72 2d 3e  E && yypParser->
32a47 79 79 69 64 78 3e 3d 30 20 29 3b 0a 20 20 72 65  yyidx>=0 );.  re
32a48 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  turn;.}../******
32a49 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
32a4a 70 61 72 73 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  parse.c ********
32a4b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a4c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a4d 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
32a4e 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
32a4f 69 6c 65 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a  ile tokenize.c *
32a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a52 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
32a53 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
32a54 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
32a55 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
32a56 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
32a57 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
32a58 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
32a59 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
32a5a 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
32a5b 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
32a5c 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
32a5d 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
32a5e 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
32a5f 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
32a60 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
32a61 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
32a62 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
32a63 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
32a64 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
32a65 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
32a66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32a69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
32a6a 2a 2a 20 41 6e 20 74 6f 6b 65 6e 69 7a 65 72 20  ** An tokenizer 
32a6b 66 6f 72 20 53 51 4c 0a 2a 2a 0a 2a 2a 20 54 68  for SQL.**.** Th
32a6c 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
32a6d 20 43 20 63 6f 64 65 20 74 68 61 74 20 73 70 6c   C code that spl
32a6e 69 74 73 20 61 6e 20 53 51 4c 20 69 6e 70 75 74  its an SQL input
32a6f 20 73 74 72 69 6e 67 20 75 70 20 69 6e 74 6f 0a   string up into.
32a70 2a 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 74 6f  ** individual to
32a71 6b 65 6e 73 20 61 6e 64 20 73 65 6e 64 73 20 74  kens and sends t
32a72 68 6f 73 65 20 74 6f 6b 65 6e 73 20 6f 6e 65 2d  hose tokens one-
32a73 62 79 2d 6f 6e 65 20 6f 76 65 72 20 74 6f 20 74  by-one over to t
32a74 68 65 0a 2a 2a 20 70 61 72 73 65 72 20 66 6f 72  he.** parser for
32a75 20 61 6e 61 6c 79 73 69 73 2e 0a 2a 2a 0a 2a 2a   analysis..**.**
32a76 20 24 49 64 3a 20 74 6f 6b 65 6e 69 7a 65 2e 63   $Id: tokenize.c
32a77 2c 76 20 31 2e 31 36 33 20 32 30 30 39 2f 30 37  ,v 1.163 2009/07
32a78 2f 30 33 20 32 32 3a 35 34 3a 33 37 20 64 72 68  /03 22:54:37 drh
32a79 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
32a7a 20 54 68 65 20 63 68 61 72 4d 61 70 28 29 20 6d   The charMap() m
32a7b 61 63 72 6f 20 6d 61 70 73 20 61 6c 70 68 61 62  acro maps alphab
32a7c 65 74 69 63 20 63 68 61 72 61 63 74 65 72 73 20  etic characters 
32a7d 69 6e 74 6f 20 74 68 65 69 72 0a 2a 2a 20 6c 6f  into their.** lo
32a7e 77 65 72 2d 63 61 73 65 20 41 53 43 49 49 20 65  wer-case ASCII e
32a7f 71 75 69 76 61 6c 65 6e 74 2e 20 20 4f 6e 20 41  quivalent.  On A
32a80 53 43 49 49 20 6d 61 63 68 69 6e 65 73 2c 20 74  SCII machines, t
32a81 68 69 73 20 69 73 20 6a 75 73 74 0a 2a 2a 20 61  his is just.** a
32a82 6e 20 75 70 70 65 72 2d 74 6f 2d 6c 6f 77 65 72  n upper-to-lower
32a83 20 63 61 73 65 20 6d 61 70 2e 20 20 4f 6e 20 45   case map.  On E
32a84 42 43 44 49 43 20 6d 61 63 68 69 6e 65 73 20 77  BCDIC machines w
32a85 65 20 61 6c 73 6f 20 6e 65 65 64 0a 2a 2a 20 74  e also need.** t
32a86 6f 20 61 64 6a 75 73 74 20 74 68 65 20 65 6e 63  o adjust the enc
32a87 6f 64 69 6e 67 2e 20 20 4f 6e 6c 79 20 61 6c 70  oding.  Only alp
32a88 68 61 62 65 74 69 63 20 63 68 61 72 61 63 74 65  habetic characte
32a89 72 73 20 61 6e 64 20 75 6e 64 65 72 73 63 6f 72  rs and underscor
32a8a 65 73 0a 2a 2a 20 6e 65 65 64 20 74 6f 20 62 65  es.** need to be
32a8b 20 74 72 61 6e 73 6c 61 74 65 64 2e 0a 2a 2f 0a   translated..*/.
32a8c 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53  #ifdef SQLITE_AS
32a8d 43 49 49 0a 23 20 64 65 66 69 6e 65 20 63 68 61  CII.# define cha
32a8e 72 4d 61 70 28 58 29 20 73 71 6c 69 74 65 33 55  rMap(X) sqlite3U
32a8f 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73  pperToLower[(uns
32a90 69 67 6e 65 64 20 63 68 61 72 29 58 5d 0a 23 65  igned char)X].#e
32a91 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
32a92 54 45 5f 45 42 43 44 49 43 0a 23 20 64 65 66 69  TE_EBCDIC.# defi
32a93 6e 65 20 63 68 61 72 4d 61 70 28 58 29 20 65 62  ne charMap(X) eb
32a94 63 64 69 63 54 6f 41 73 63 69 69 5b 28 75 6e 73  cdicToAscii[(uns
32a95 69 67 6e 65 64 20 63 68 61 72 29 58 5d 0a 63 6f  igned char)X].co
32a96 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
32a97 72 20 65 62 63 64 69 63 54 6f 41 73 63 69 69 5b  r ebcdicToAscii[
32a98 5d 20 3d 20 7b 0a 2f 2a 20 30 20 20 20 31 20 20  ] = {./* 0   1  
32a99 20 32 20 20 20 33 20 20 20 34 20 20 20 35 20 20   2   3   4   5  
32a9a 20 36 20 20 20 37 20 20 20 38 20 20 20 39 20 20   6   7   8   9  
32a9b 20 41 20 20 20 42 20 20 20 43 20 20 20 44 20 20   A   B   C   D  
32a9c 20 45 20 20 20 46 20 2a 2f 0a 20 20 20 30 2c 20   E   F */.   0, 
32a9d 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32a9e 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32a9f 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32aa0 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20   0,  0,  0,  /* 
32aa1 30 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20  0x */.   0,  0, 
32aa2 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32aa3 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32aa4 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32aa5 20 30 2c 20 20 30 2c 20 20 2f 2a 20 31 78 20 2a   0,  0,  /* 1x *
32aa6 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20  /.   0,  0,  0, 
32aa7 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32aa8 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32aa9 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32aaa 20 30 2c 20 20 2f 2a 20 32 78 20 2a 2f 0a 20 20   0,  /* 2x */.  
32aab 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32aac 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32aad 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32aae 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32aaf 20 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 30 2c 20   /* 3x */.   0, 
32ab0 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ab1 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ab2 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ab3 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20   0,  0,  0,  /* 
32ab4 34 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20  4x */.   0,  0, 
32ab5 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ab6 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ab7 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ab8 20 30 2c 20 20 30 2c 20 20 2f 2a 20 35 78 20 2a   0,  0,  /* 5x *
32ab9 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20  /.   0,  0,  0, 
32aba 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32abb 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32abc 20 30 2c 20 20 30 2c 20 39 35 2c 20 20 30 2c 20   0,  0, 95,  0, 
32abd 20 30 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20   0,  /* 6x */.  
32abe 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32abf 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ac0 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ac1 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ac2 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 30 2c 20   /* 7x */.   0, 
32ac3 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31  97, 98, 99,100,1
32ac4 30 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31  01,102,103,104,1
32ac5 30 35 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20  05,  0,  0,  0, 
32ac6 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20   0,  0,  0,  /* 
32ac7 38 78 20 2a 2f 0a 20 20 20 30 2c 31 30 36 2c 31  8x */.   0,106,1
32ac8 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31  07,108,109,110,1
32ac9 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 20  11,112,113,114, 
32aca 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32acb 20 30 2c 20 20 30 2c 20 20 2f 2a 20 39 78 20 2a   0,  0,  /* 9x *
32acc 2f 0a 20 20 20 30 2c 20 20 30 2c 31 31 35 2c 31  /.   0,  0,115,1
32acd 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31  16,117,118,119,1
32ace 32 30 2c 31 32 31 2c 31 32 32 2c 20 20 30 2c 20  20,121,122,  0, 
32acf 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ad0 20 30 2c 20 20 2f 2a 20 41 78 20 2a 2f 0a 20 20   0,  /* Ax */.  
32ad1 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ad2 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ad3 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ad4 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ad5 20 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 30 2c 20   /* Bx */.   0, 
32ad6 39 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31  97, 98, 99,100,1
32ad7 30 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31  01,102,103,104,1
32ad8 30 35 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20  05,  0,  0,  0, 
32ad9 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20   0,  0,  0,  /* 
32ada 43 78 20 2a 2f 0a 20 20 20 30 2c 31 30 36 2c 31  Cx */.   0,106,1
32adb 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31  07,108,109,110,1
32adc 31 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 20  11,112,113,114, 
32add 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ade 20 30 2c 20 20 30 2c 20 20 2f 2a 20 44 78 20 2a   0,  0,  /* Dx *
32adf 2f 0a 20 20 20 30 2c 20 20 30 2c 31 31 35 2c 31  /.   0,  0,115,1
32ae0 31 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31  16,117,118,119,1
32ae1 32 30 2c 31 32 31 2c 31 32 32 2c 20 20 30 2c 20  20,121,122,  0, 
32ae2 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ae3 20 30 2c 20 20 2f 2a 20 45 78 20 2a 2f 0a 20 20   0,  /* Ex */.  
32ae4 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ae5 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ae6 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ae7 20 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20   0,  0,  0,  0, 
32ae8 20 2f 2a 20 46 78 20 2a 2f 0a 7d 3b 0a 23 65 6e   /* Fx */.};.#en
32ae9 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  dif../*.** The s
32aea 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f 64  qlite3KeywordCod
32aeb 65 20 66 75 6e 63 74 69 6f 6e 20 6c 6f 6f 6b 73  e function looks
32aec 20 75 70 20 61 6e 20 69 64 65 6e 74 69 66 69 65   up an identifie
32aed 72 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69  r to determine i
32aee 66 0a 2a 2a 20 69 74 20 69 73 20 61 20 6b 65 79  f.** it is a key
32aef 77 6f 72 64 2e 20 20 49 66 20 69 74 20 69 73 20  word.  If it is 
32af0 61 20 6b 65 79 77 6f 72 64 2c 20 74 68 65 20 74  a keyword, the t
32af1 6f 6b 65 6e 20 63 6f 64 65 20 6f 66 20 74 68 61  oken code of tha
32af2 74 20 6b 65 79 77 6f 72 64 20 69 73 20 0a 2a 2a  t keyword is .**
32af3 20 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74   returned.  If t
32af4 68 65 20 69 6e 70 75 74 20 69 73 20 6e 6f 74 20  he input is not 
32af5 61 20 6b 65 79 77 6f 72 64 2c 20 54 4b 5f 49 44  a keyword, TK_ID
32af6 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
32af7 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e  .** The implemen
32af8 74 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 72  tation of this r
32af9 6f 75 74 69 6e 65 20 77 61 73 20 67 65 6e 65 72  outine was gener
32afa 61 74 65 64 20 62 79 20 61 20 70 72 6f 67 72 61  ated by a progra
32afb 6d 2c 0a 2a 2a 20 6d 6b 6b 65 79 77 6f 72 64 68  m,.** mkkeywordh
32afc 61 73 68 2e 68 2c 20 6c 6f 63 61 74 65 64 20 69  ash.h, located i
32afd 6e 20 74 68 65 20 74 6f 6f 6c 20 73 75 62 64 69  n the tool subdi
32afe 72 65 63 74 6f 72 79 20 6f 66 20 74 68 65 20 64  rectory of the d
32aff 69 73 74 72 69 62 75 74 69 6f 6e 2e 0a 2a 2a 20  istribution..** 
32b00 54 68 65 20 6f 75 74 70 75 74 20 6f 66 20 74 68  The output of th
32b01 65 20 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e  e mkkeywordhash.
32b02 63 20 70 72 6f 67 72 61 6d 20 69 73 20 77 72 69  c program is wri
32b03 74 74 65 6e 20 69 6e 74 6f 20 61 20 66 69 6c 65  tten into a file
32b04 0a 2a 2a 20 6e 61 6d 65 64 20 6b 65 79 77 6f 72  .** named keywor
32b05 64 68 61 73 68 2e 68 20 61 6e 64 20 74 68 65 6e  dhash.h and then
32b06 20 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74   included into t
32b07 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 20  his source file 
32b08 62 79 0a 2a 2a 20 74 68 65 20 23 69 6e 63 6c 75  by.** the #inclu
32b09 64 65 20 62 65 6c 6f 77 2e 0a 2a 2f 0a 2f 2a 2a  de below..*/./**
32b0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
32b0b 6c 75 64 65 20 6b 65 79 77 6f 72 64 68 61 73 68  lude keywordhash
32b0c 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
32b0d 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a   of tokenize.c *
32b0e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
32b0f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
32b10 69 6e 20 66 69 6c 65 20 6b 65 79 77 6f 72 64 68  in file keywordh
32b11 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ash.h **********
32b12 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32b13 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
32b14 2a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  *** This file co
32b15 6e 74 61 69 6e 73 20 61 75 74 6f 6d 61 74 69 63  ntains automatic
32b16 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20 63  ally generated c
32b17 6f 64 65 20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  ode ******.**.**
32b18 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69   The code in thi
32b19 73 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20  s file has been 
32b1a 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65  automatically ge
32b1b 6e 65 72 61 74 65 64 20 62 79 0a 2a 2a 0a 2a 2a  nerated by.**.**
32b1c 20 20 20 20 20 24 48 65 61 64 65 72 3a 20 2f 68       $Header: /h
32b1d 6f 6d 65 2f 64 72 68 2f 73 71 6c 69 74 65 2f 74  ome/drh/sqlite/t
32b1e 72 61 6e 73 2f 63 76 73 2f 73 71 6c 69 74 65 2f  rans/cvs/sqlite/
32b1f 73 71 6c 69 74 65 2f 74 6f 6f 6c 2f 6d 6b 6b 65  sqlite/tool/mkke
32b20 79 77 6f 72 64 68 61 73 68 2e 63 2c 76 20 31 2e  ywordhash.c,v 1.
32b21 33 38 20 32 30 30 39 2f 30 36 2f 30 39 20 31 34  38 2009/06/09 14
32b22 3a 32 37 3a 34 31 20 64 72 68 20 45 78 70 20 24  :27:41 drh Exp $
32b23 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  .**.** The code 
32b24 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 6d 70  in this file imp
32b25 6c 65 6d 65 6e 74 73 20 61 20 66 75 6e 63 74 69  lements a functi
32b26 6f 6e 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e  on that determin
32b27 65 73 20 77 68 65 74 68 65 72 0a 2a 2a 20 6f 72  es whether.** or
32b28 20 6e 6f 74 20 61 20 67 69 76 65 6e 20 69 64 65   not a given ide
32b29 6e 74 69 66 69 65 72 20 69 73 20 72 65 61 6c 6c  ntifier is reall
32b2a 79 20 61 6e 20 53 51 4c 20 6b 65 79 77 6f 72 64  y an SQL keyword
32b2b 2e 20 20 54 68 65 20 73 61 6d 65 20 74 68 69 6e  .  The same thin
32b2c 67 0a 2a 2a 20 6d 69 67 68 74 20 62 65 20 69 6d  g.** might be im
32b2d 70 6c 65 6d 65 6e 74 65 64 20 6d 6f 72 65 20 64  plemented more d
32b2e 69 72 65 63 74 6c 79 20 75 73 69 6e 67 20 61 20  irectly using a 
32b2f 68 61 6e 64 2d 77 72 69 74 74 65 6e 20 68 61 73  hand-written has
32b30 68 20 74 61 62 6c 65 2e 0a 2a 2a 20 42 75 74 20  h table..** But 
32b31 62 79 20 75 73 69 6e 67 20 74 68 69 73 20 61 75  by using this au
32b32 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e 65  tomatically gene
32b33 72 61 74 65 64 20 63 6f 64 65 2c 20 74 68 65 20  rated code, the 
32b34 73 69 7a 65 20 6f 66 20 74 68 65 20 63 6f 64 65  size of the code
32b35 0a 2a 2a 20 69 73 20 73 75 62 73 74 61 6e 74 69  .** is substanti
32b36 61 6c 6c 79 20 72 65 64 75 63 65 64 2e 20 20 54  ally reduced.  T
32b37 68 69 73 20 69 73 20 69 6d 70 6f 72 74 61 6e 74  his is important
32b38 20 66 6f 72 20 65 6d 62 65 64 64 65 64 20 61 70   for embedded ap
32b39 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 6f 6e  plications.** on
32b3a 20 70 6c 61 74 66 6f 72 6d 73 20 77 69 74 68 20   platforms with 
32b3b 6c 69 6d 69 74 65 64 20 6d 65 6d 6f 72 79 2e 0a  limited memory..
32b3c 2a 2f 0a 2f 2a 20 48 61 73 68 20 73 63 6f 72 65  */./* Hash score
32b3d 3a 20 31 37 35 20 2a 2f 0a 73 74 61 74 69 63 20  : 175 */.static 
32b3e 69 6e 74 20 6b 65 79 77 6f 72 64 43 6f 64 65 28  int keywordCode(
32b3f 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
32b40 6e 74 20 6e 29 7b 0a 20 20 2f 2a 20 7a 54 65 78  nt n){.  /* zTex
32b41 74 5b 5d 20 65 6e 63 6f 64 65 73 20 38 31 31 20  t[] encodes 811 
32b42 62 79 74 65 73 20 6f 66 20 6b 65 79 77 6f 72 64  bytes of keyword
32b43 73 20 69 6e 20 35 34 31 20 62 79 74 65 73 20 2a  s in 541 bytes *
32b44 2f 0a 20 20 2f 2a 20 20 20 52 45 49 4e 44 45 58  /.  /*   REINDEX
32b45 45 44 45 53 43 41 50 45 41 43 48 45 43 4b 45 59  EDESCAPEACHECKEY
32b46 42 45 46 4f 52 45 49 47 4e 4f 52 45 47 45 58 50  BEFOREIGNOREGEXP
32b47 4c 41 49 4e 53 54 45 41 44 44 41 54 41 42 41 53  LAINSTEADDATABAS
32b48 45 4c 45 43 54 20 20 20 20 20 20 20 2a 2f 0a 20  ELECT       */. 
32b49 20 2f 2a 20 20 20 41 42 4c 45 46 54 48 45 4e 44   /*   ABLEFTHEND
32b4a 45 46 45 52 52 41 42 4c 45 4c 53 45 58 43 45 50  EFERRABLELSEXCEP
32b4b 54 52 41 4e 53 41 43 54 49 4f 4e 41 54 55 52 41  TRANSACTIONATURA
32b4c 4c 54 45 52 41 49 53 45 58 43 4c 55 53 49 56 45  LTERAISEXCLUSIVE
32b4d 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a           */.  /*
32b4e 20 20 20 58 49 53 54 53 41 56 45 50 4f 49 4e 54     XISTSAVEPOINT
32b4f 45 52 53 45 43 54 52 49 47 47 45 52 45 46 45 52  ERSECTRIGGEREFER
32b50 45 4e 43 45 53 43 4f 4e 53 54 52 41 49 4e 54 4f  ENCESCONSTRAINTO
32b51 46 46 53 45 54 45 4d 50 4f 52 41 52 59 20 20 20  FFSETEMPORARY   
32b52 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20        */.  /*   
32b53 55 4e 49 51 55 45 52 59 41 54 54 41 43 48 41 56  UNIQUERYATTACHAV
32b54 49 4e 47 52 4f 55 50 44 41 54 45 42 45 47 49 4e  INGROUPDATEBEGIN
32b55 4e 45 52 45 4c 45 41 53 45 42 45 54 57 45 45 4e  NERELEASEBETWEEN
32b56 4f 54 4e 55 4c 4c 49 4b 45 20 20 20 20 20 20 20  OTNULLIKE       
32b57 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 43 41 53     */.  /*   CAS
32b58 43 41 44 45 4c 45 54 45 43 41 53 45 43 4f 4c 4c  CADELETECASECOLL
32b59 41 54 45 43 52 45 41 54 45 43 55 52 52 45 4e 54  ATECREATECURRENT
32b5a 5f 44 41 54 45 44 45 54 41 43 48 49 4d 4d 45 44  _DATEDETACHIMMED
32b5b 49 41 54 45 4a 4f 49 4e 20 20 20 20 20 20 20 20  IATEJOIN        
32b5c 2a 2f 0a 20 20 2f 2a 20 20 20 53 45 52 54 4d 41  */.  /*   SERTMA
32b5d 54 43 48 50 4c 41 4e 41 4c 59 5a 45 50 52 41 47  TCHPLANALYZEPRAG
32b5e 4d 41 42 4f 52 54 56 41 4c 55 45 53 56 49 52 54  MABORTVALUESVIRT
32b5f 55 41 4c 49 4d 49 54 57 48 45 4e 57 48 45 52 45  UALIMITWHENWHERE
32b60 4e 41 4d 45 20 20 20 20 20 20 20 20 20 2a 2f 0a  NAME         */.
32b61 20 20 2f 2a 20 20 20 41 46 54 45 52 45 50 4c 41    /*   AFTEREPLA
32b62 43 45 41 4e 44 45 46 41 55 4c 54 41 55 54 4f 49  CEANDEFAULTAUTOI
32b63 4e 43 52 45 4d 45 4e 54 43 41 53 54 43 4f 4c 55  NCREMENTCASTCOLU
32b64 4d 4e 43 4f 4d 4d 49 54 43 4f 4e 46 4c 49 43 54  MNCOMMITCONFLICT
32b65 43 52 4f 53 53 20 20 20 20 20 2a 2f 0a 20 20 2f  CROSS     */.  /
32b66 2a 20 20 20 43 55 52 52 45 4e 54 5f 54 49 4d 45  *   CURRENT_TIME
32b67 53 54 41 4d 50 52 49 4d 41 52 59 44 45 46 45 52  STAMPRIMARYDEFER
32b68 52 45 44 49 53 54 49 4e 43 54 44 52 4f 50 46 41  REDISTINCTDROPFA
32b69 49 4c 46 52 4f 4d 46 55 4c 4c 47 4c 4f 42 59 49  ILFROMFULLGLOBYI
32b6a 46 20 20 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20  F      */.  /*  
32b6b 20 49 53 4e 55 4c 4c 4f 52 44 45 52 45 53 54 52   ISNULLORDERESTR
32b6c 49 43 54 4f 55 54 45 52 49 47 48 54 52 4f 4c 4c  ICTOUTERIGHTROLL
32b6d 42 41 43 4b 52 4f 57 55 4e 49 4f 4e 55 53 49 4e  BACKROWUNIONUSIN
32b6e 47 56 41 43 55 55 4d 56 49 45 57 20 20 20 20 20  GVACUUMVIEW     
32b6f 20 20 20 20 2a 2f 0a 20 20 2f 2a 20 20 20 49 4e      */.  /*   IN
32b70 49 54 49 41 4c 4c 59 20 20 20 20 20 20 20 20 20  ITIALLY         
32b71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32b74 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   */.  static con
32b75 73 74 20 63 68 61 72 20 7a 54 65 78 74 5b 35 34  st char zText[54
32b76 30 5d 20 3d 20 7b 0a 20 20 20 20 27 52 27 2c 27  0] = {.    'R','
32b77 45 27 2c 27 49 27 2c 27 4e 27 2c 27 44 27 2c 27  E','I','N','D','
32b78 45 27 2c 27 58 27 2c 27 45 27 2c 27 44 27 2c 27  E','X','E','D','
32b79 45 27 2c 27 53 27 2c 27 43 27 2c 27 41 27 2c 27  E','S','C','A','
32b7a 50 27 2c 27 45 27 2c 27 41 27 2c 27 43 27 2c 27  P','E','A','C','
32b7b 48 27 2c 0a 20 20 20 20 27 45 27 2c 27 43 27 2c  H',.    'E','C',
32b7c 27 4b 27 2c 27 45 27 2c 27 59 27 2c 27 42 27 2c  'K','E','Y','B',
32b7d 27 45 27 2c 27 46 27 2c 27 4f 27 2c 27 52 27 2c  'E','F','O','R',
32b7e 27 45 27 2c 27 49 27 2c 27 47 27 2c 27 4e 27 2c  'E','I','G','N',
32b7f 27 4f 27 2c 27 52 27 2c 27 45 27 2c 27 47 27 2c  'O','R','E','G',
32b80 0a 20 20 20 20 27 45 27 2c 27 58 27 2c 27 50 27  .    'E','X','P'
32b81 2c 27 4c 27 2c 27 41 27 2c 27 49 27 2c 27 4e 27  ,'L','A','I','N'
32b82 2c 27 53 27 2c 27 54 27 2c 27 45 27 2c 27 41 27  ,'S','T','E','A'
32b83 2c 27 44 27 2c 27 44 27 2c 27 41 27 2c 27 54 27  ,'D','D','A','T'
32b84 2c 27 41 27 2c 27 42 27 2c 27 41 27 2c 0a 20 20  ,'A','B','A',.  
32b85 20 20 27 53 27 2c 27 45 27 2c 27 4c 27 2c 27 45    'S','E','L','E
32b86 27 2c 27 43 27 2c 27 54 27 2c 27 41 27 2c 27 42  ','C','T','A','B
32b87 27 2c 27 4c 27 2c 27 45 27 2c 27 46 27 2c 27 54  ','L','E','F','T
32b88 27 2c 27 48 27 2c 27 45 27 2c 27 4e 27 2c 27 44  ','H','E','N','D
32b89 27 2c 27 45 27 2c 27 46 27 2c 0a 20 20 20 20 27  ','E','F',.    '
32b8a 45 27 2c 27 52 27 2c 27 52 27 2c 27 41 27 2c 27  E','R','R','A','
32b8b 42 27 2c 27 4c 27 2c 27 45 27 2c 27 4c 27 2c 27  B','L','E','L','
32b8c 53 27 2c 27 45 27 2c 27 58 27 2c 27 43 27 2c 27  S','E','X','C','
32b8d 45 27 2c 27 50 27 2c 27 54 27 2c 27 52 27 2c 27  E','P','T','R','
32b8e 41 27 2c 27 4e 27 2c 0a 20 20 20 20 27 53 27 2c  A','N',.    'S',
32b8f 27 41 27 2c 27 43 27 2c 27 54 27 2c 27 49 27 2c  'A','C','T','I',
32b90 27 4f 27 2c 27 4e 27 2c 27 41 27 2c 27 54 27 2c  'O','N','A','T',
32b91 27 55 27 2c 27 52 27 2c 27 41 27 2c 27 4c 27 2c  'U','R','A','L',
32b92 27 54 27 2c 27 45 27 2c 27 52 27 2c 27 41 27 2c  'T','E','R','A',
32b93 27 49 27 2c 0a 20 20 20 20 27 53 27 2c 27 45 27  'I',.    'S','E'
32b94 2c 27 58 27 2c 27 43 27 2c 27 4c 27 2c 27 55 27  ,'X','C','L','U'
32b95 2c 27 53 27 2c 27 49 27 2c 27 56 27 2c 27 45 27  ,'S','I','V','E'
32b96 2c 27 58 27 2c 27 49 27 2c 27 53 27 2c 27 54 27  ,'X','I','S','T'
32b97 2c 27 53 27 2c 27 41 27 2c 27 56 27 2c 27 45 27  ,'S','A','V','E'
32b98 2c 0a 20 20 20 20 27 50 27 2c 27 4f 27 2c 27 49  ,.    'P','O','I
32b99 27 2c 27 4e 27 2c 27 54 27 2c 27 45 27 2c 27 52  ','N','T','E','R
32b9a 27 2c 27 53 27 2c 27 45 27 2c 27 43 27 2c 27 54  ','S','E','C','T
32b9b 27 2c 27 52 27 2c 27 49 27 2c 27 47 27 2c 27 47  ','R','I','G','G
32b9c 27 2c 27 45 27 2c 27 52 27 2c 27 45 27 2c 0a 20  ','E','R','E',. 
32b9d 20 20 20 27 46 27 2c 27 45 27 2c 27 52 27 2c 27     'F','E','R','
32b9e 45 27 2c 27 4e 27 2c 27 43 27 2c 27 45 27 2c 27  E','N','C','E','
32b9f 53 27 2c 27 43 27 2c 27 4f 27 2c 27 4e 27 2c 27  S','C','O','N','
32ba0 53 27 2c 27 54 27 2c 27 52 27 2c 27 41 27 2c 27  S','T','R','A','
32ba1 49 27 2c 27 4e 27 2c 27 54 27 2c 0a 20 20 20 20  I','N','T',.    
32ba2 27 4f 27 2c 27 46 27 2c 27 46 27 2c 27 53 27 2c  'O','F','F','S',
32ba3 27 45 27 2c 27 54 27 2c 27 45 27 2c 27 4d 27 2c  'E','T','E','M',
32ba4 27 50 27 2c 27 4f 27 2c 27 52 27 2c 27 41 27 2c  'P','O','R','A',
32ba5 27 52 27 2c 27 59 27 2c 27 55 27 2c 27 4e 27 2c  'R','Y','U','N',
32ba6 27 49 27 2c 27 51 27 2c 0a 20 20 20 20 27 55 27  'I','Q',.    'U'
32ba7 2c 27 45 27 2c 27 52 27 2c 27 59 27 2c 27 41 27  ,'E','R','Y','A'
32ba8 2c 27 54 27 2c 27 54 27 2c 27 41 27 2c 27 43 27  ,'T','T','A','C'
32ba9 2c 27 48 27 2c 27 41 27 2c 27 56 27 2c 27 49 27  ,'H','A','V','I'
32baa 2c 27 4e 27 2c 27 47 27 2c 27 52 27 2c 27 4f 27  ,'N','G','R','O'
32bab 2c 27 55 27 2c 0a 20 20 20 20 27 50 27 2c 27 44  ,'U',.    'P','D
32bac 27 2c 27 41 27 2c 27 54 27 2c 27 45 27 2c 27 42  ','A','T','E','B
32bad 27 2c 27 45 27 2c 27 47 27 2c 27 49 27 2c 27 4e  ','E','G','I','N
32bae 27 2c 27 4e 27 2c 27 45 27 2c 27 52 27 2c 27 45  ','N','E','R','E
32baf 27 2c 27 4c 27 2c 27 45 27 2c 27 41 27 2c 27 53  ','L','E','A','S
32bb0 27 2c 0a 20 20 20 20 27 45 27 2c 27 42 27 2c 27  ',.    'E','B','
32bb1 45 27 2c 27 54 27 2c 27 57 27 2c 27 45 27 2c 27  E','T','W','E','
32bb2 45 27 2c 27 4e 27 2c 27 4f 27 2c 27 54 27 2c 27  E','N','O','T','
32bb3 4e 27 2c 27 55 27 2c 27 4c 27 2c 27 4c 27 2c 27  N','U','L','L','
32bb4 49 27 2c 27 4b 27 2c 27 45 27 2c 27 43 27 2c 0a  I','K','E','C',.
32bb5 20 20 20 20 27 41 27 2c 27 53 27 2c 27 43 27 2c      'A','S','C',
32bb6 27 41 27 2c 27 44 27 2c 27 45 27 2c 27 4c 27 2c  'A','D','E','L',
32bb7 27 45 27 2c 27 54 27 2c 27 45 27 2c 27 43 27 2c  'E','T','E','C',
32bb8 27 41 27 2c 27 53 27 2c 27 45 27 2c 27 43 27 2c  'A','S','E','C',
32bb9 27 4f 27 2c 27 4c 27 2c 27 4c 27 2c 0a 20 20 20  'O','L','L',.   
32bba 20 27 41 27 2c 27 54 27 2c 27 45 27 2c 27 43 27   'A','T','E','C'
32bbb 2c 27 52 27 2c 27 45 27 2c 27 41 27 2c 27 54 27  ,'R','E','A','T'
32bbc 2c 27 45 27 2c 27 43 27 2c 27 55 27 2c 27 52 27  ,'E','C','U','R'
32bbd 2c 27 52 27 2c 27 45 27 2c 27 4e 27 2c 27 54 27  ,'R','E','N','T'
32bbe 2c 27 5f 27 2c 27 44 27 2c 0a 20 20 20 20 27 41  ,'_','D',.    'A
32bbf 27 2c 27 54 27 2c 27 45 27 2c 27 44 27 2c 27 45  ','T','E','D','E
32bc0 27 2c 27 54 27 2c 27 41 27 2c 27 43 27 2c 27 48  ','T','A','C','H
32bc1 27 2c 27 49 27 2c 27 4d 27 2c 27 4d 27 2c 27 45  ','I','M','M','E
32bc2 27 2c 27 44 27 2c 27 49 27 2c 27 41 27 2c 27 54  ','D','I','A','T
32bc3 27 2c 27 45 27 2c 0a 20 20 20 20 27 4a 27 2c 27  ','E',.    'J','
32bc4 4f 27 2c 27 49 27 2c 27 4e 27 2c 27 53 27 2c 27  O','I','N','S','
32bc5 45 27 2c 27 52 27 2c 27 54 27 2c 27 4d 27 2c 27  E','R','T','M','
32bc6 41 27 2c 27 54 27 2c 27 43 27 2c 27 48 27 2c 27  A','T','C','H','
32bc7 50 27 2c 27 4c 27 2c 27 41 27 2c 27 4e 27 2c 27  P','L','A','N','
32bc8 41 27 2c 0a 20 20 20 20 27 4c 27 2c 27 59 27 2c  A',.    'L','Y',
32bc9 27 5a 27 2c 27 45 27 2c 27 50 27 2c 27 52 27 2c  'Z','E','P','R',
32bca 27 41 27 2c 27 47 27 2c 27 4d 27 2c 27 41 27 2c  'A','G','M','A',
32bcb 27 42 27 2c 27 4f 27 2c 27 52 27 2c 27 54 27 2c  'B','O','R','T',
32bcc 27 56 27 2c 27 41 27 2c 27 4c 27 2c 27 55 27 2c  'V','A','L','U',
32bcd 0a 20 20 20 20 27 45 27 2c 27 53 27 2c 27 56 27  .    'E','S','V'
32bce 2c 27 49 27 2c 27 52 27 2c 27 54 27 2c 27 55 27  ,'I','R','T','U'
32bcf 2c 27 41 27 2c 27 4c 27 2c 27 49 27 2c 27 4d 27  ,'A','L','I','M'
32bd0 2c 27 49 27 2c 27 54 27 2c 27 57 27 2c 27 48 27  ,'I','T','W','H'
32bd1 2c 27 45 27 2c 27 4e 27 2c 27 57 27 2c 0a 20 20  ,'E','N','W',.  
32bd2 20 20 27 48 27 2c 27 45 27 2c 27 52 27 2c 27 45    'H','E','R','E
32bd3 27 2c 27 4e 27 2c 27 41 27 2c 27 4d 27 2c 27 45  ','N','A','M','E
32bd4 27 2c 27 41 27 2c 27 46 27 2c 27 54 27 2c 27 45  ','A','F','T','E
32bd5 27 2c 27 52 27 2c 27 45 27 2c 27 50 27 2c 27 4c  ','R','E','P','L
32bd6 27 2c 27 41 27 2c 27 43 27 2c 0a 20 20 20 20 27  ','A','C',.    '
32bd7 45 27 2c 27 41 27 2c 27 4e 27 2c 27 44 27 2c 27  E','A','N','D','
32bd8 45 27 2c 27 46 27 2c 27 41 27 2c 27 55 27 2c 27  E','F','A','U','
32bd9 4c 27 2c 27 54 27 2c 27 41 27 2c 27 55 27 2c 27  L','T','A','U','
32bda 54 27 2c 27 4f 27 2c 27 49 27 2c 27 4e 27 2c 27  T','O','I','N','
32bdb 43 27 2c 27 52 27 2c 0a 20 20 20 20 27 45 27 2c  C','R',.    'E',
32bdc 27 4d 27 2c 27 45 27 2c 27 4e 27 2c 27 54 27 2c  'M','E','N','T',
32bdd 27 43 27 2c 27 41 27 2c 27 53 27 2c 27 54 27 2c  'C','A','S','T',
32bde 27 43 27 2c 27 4f 27 2c 27 4c 27 2c 27 55 27 2c  'C','O','L','U',
32bdf 27 4d 27 2c 27 4e 27 2c 27 43 27 2c 27 4f 27 2c  'M','N','C','O',
32be0 27 4d 27 2c 0a 20 20 20 20 27 4d 27 2c 27 49 27  'M',.    'M','I'
32be1 2c 27 54 27 2c 27 43 27 2c 27 4f 27 2c 27 4e 27  ,'T','C','O','N'
32be2 2c 27 46 27 2c 27 4c 27 2c 27 49 27 2c 27 43 27  ,'F','L','I','C'
32be3 2c 27 54 27 2c 27 43 27 2c 27 52 27 2c 27 4f 27  ,'T','C','R','O'
32be4 2c 27 53 27 2c 27 53 27 2c 27 43 27 2c 27 55 27  ,'S','S','C','U'
32be5 2c 0a 20 20 20 20 27 52 27 2c 27 52 27 2c 27 45  ,.    'R','R','E
32be6 27 2c 27 4e 27 2c 27 54 27 2c 27 5f 27 2c 27 54  ','N','T','_','T
32be7 27 2c 27 49 27 2c 27 4d 27 2c 27 45 27 2c 27 53  ','I','M','E','S
32be8 27 2c 27 54 27 2c 27 41 27 2c 27 4d 27 2c 27 50  ','T','A','M','P
32be9 27 2c 27 52 27 2c 27 49 27 2c 27 4d 27 2c 0a 20  ','R','I','M',. 
32bea 20 20 20 27 41 27 2c 27 52 27 2c 27 59 27 2c 27     'A','R','Y','
32beb 44 27 2c 27 45 27 2c 27 46 27 2c 27 45 27 2c 27  D','E','F','E','
32bec 52 27 2c 27 52 27 2c 27 45 27 2c 27 44 27 2c 27  R','R','E','D','
32bed 49 27 2c 27 53 27 2c 27 54 27 2c 27 49 27 2c 27  I','S','T','I','
32bee 4e 27 2c 27 43 27 2c 27 54 27 2c 0a 20 20 20 20  N','C','T',.    
32bef 27 44 27 2c 27 52 27 2c 27 4f 27 2c 27 50 27 2c  'D','R','O','P',
32bf0 27 46 27 2c 27 41 27 2c 27 49 27 2c 27 4c 27 2c  'F','A','I','L',
32bf1 27 46 27 2c 27 52 27 2c 27 4f 27 2c 27 4d 27 2c  'F','R','O','M',
32bf2 27 46 27 2c 27 55 27 2c 27 4c 27 2c 27 4c 27 2c  'F','U','L','L',
32bf3 27 47 27 2c 27 4c 27 2c 0a 20 20 20 20 27 4f 27  'G','L',.    'O'
32bf4 2c 27 42 27 2c 27 59 27 2c 27 49 27 2c 27 46 27  ,'B','Y','I','F'
32bf5 2c 27 49 27 2c 27 53 27 2c 27 4e 27 2c 27 55 27  ,'I','S','N','U'
32bf6 2c 27 4c 27 2c 27 4c 27 2c 27 4f 27 2c 27 52 27  ,'L','L','O','R'
32bf7 2c 27 44 27 2c 27 45 27 2c 27 52 27 2c 27 45 27  ,'D','E','R','E'
32bf8 2c 27 53 27 2c 0a 20 20 20 20 27 54 27 2c 27 52  ,'S',.    'T','R
32bf9 27 2c 27 49 27 2c 27 43 27 2c 27 54 27 2c 27 4f  ','I','C','T','O
32bfa 27 2c 27 55 27 2c 27 54 27 2c 27 45 27 2c 27 52  ','U','T','E','R
32bfb 27 2c 27 49 27 2c 27 47 27 2c 27 48 27 2c 27 54  ','I','G','H','T
32bfc 27 2c 27 52 27 2c 27 4f 27 2c 27 4c 27 2c 27 4c  ','R','O','L','L
32bfd 27 2c 0a 20 20 20 20 27 42 27 2c 27 41 27 2c 27  ',.    'B','A','
32bfe 43 27 2c 27 4b 27 2c 27 52 27 2c 27 4f 27 2c 27  C','K','R','O','
32bff 57 27 2c 27 55 27 2c 27 4e 27 2c 27 49 27 2c 27  W','U','N','I','
32c00 4f 27 2c 27 4e 27 2c 27 55 27 2c 27 53 27 2c 27  O','N','U','S','
32c01 49 27 2c 27 4e 27 2c 27 47 27 2c 27 56 27 2c 0a  I','N','G','V',.
32c02 20 20 20 20 27 41 27 2c 27 43 27 2c 27 55 27 2c      'A','C','U',
32c03 27 55 27 2c 27 4d 27 2c 27 56 27 2c 27 49 27 2c  'U','M','V','I',
32c04 27 45 27 2c 27 57 27 2c 27 49 27 2c 27 4e 27 2c  'E','W','I','N',
32c05 27 49 27 2c 27 54 27 2c 27 49 27 2c 27 41 27 2c  'I','T','I','A',
32c06 27 4c 27 2c 27 4c 27 2c 27 59 27 2c 0a 20 20 7d  'L','L','Y',.  }
32c07 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
32c08 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61   unsigned char a
32c09 48 61 73 68 5b 31 32 37 5d 20 3d 20 7b 0a 20 20  Hash[127] = {.  
32c0a 20 20 20 20 37 32 2c 20 31 30 31 2c 20 31 31 34      72, 101, 114
32c0b 2c 20 20 37 30 2c 20 20 20 30 2c 20 20 34 34 2c  ,  70,   0,  44,
32c0c 20 20 20 30 2c 20 20 20 30 2c 20 20 37 38 2c 20     0,   0,  78, 
32c0d 20 20 30 2c 20 20 37 33 2c 20 20 20 30 2c 20 20    0,  73,   0,  
32c0e 20 30 2c 0a 20 20 20 20 20 20 34 32 2c 20 20 31   0,.      42,  1
32c0f 32 2c 20 20 37 34 2c 20 20 31 35 2c 20 20 20 30  2,  74,  15,   0
32c10 2c 20 31 31 33 2c 20 20 38 31 2c 20 20 35 30 2c  , 113,  81,  50,
32c11 20 31 30 38 2c 20 20 20 30 2c 20 20 31 39 2c 20   108,   0,  19, 
32c12 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 31    0,   0,.     1
32c13 31 38 2c 20 20 20 30 2c 20 31 31 36 2c 20 31 31  18,   0, 116, 11
32c14 31 2c 20 20 20 30 2c 20 20 32 32 2c 20 20 38 39  1,   0,  22,  89
32c15 2c 20 20 20 30 2c 20 20 20 39 2c 20 20 20 30 2c  ,   0,   9,   0,
32c16 20 20 20 30 2c 20 20 36 36 2c 20 20 36 37 2c 0a     0,  66,  67,.
32c17 20 20 20 20 20 20 20 30 2c 20 20 36 35 2c 20 20         0,  65,  
32c18 20 36 2c 20 20 20 30 2c 20 20 34 38 2c 20 20 38   6,   0,  48,  8
32c19 36 2c 20 20 39 38 2c 20 20 20 30 2c 20 31 31 35  6,  98,   0, 115
32c1a 2c 20 20 39 37 2c 20 20 20 30 2c 20 20 20 30 2c  ,  97,   0,   0,
32c1b 20 20 34 35 2c 0a 20 20 20 20 20 20 20 30 2c 20    45,.       0, 
32c1c 20 39 39 2c 20 20 32 34 2c 20 20 20 30 2c 20 20   99,  24,   0,  
32c1d 31 37 2c 20 20 20 30 2c 20 31 31 39 2c 20 20 34  17,   0, 119,  4
32c1e 39 2c 20 20 32 33 2c 20 20 20 30 2c 20 20 20 35  9,  23,   0,   5
32c1f 2c 20 31 30 36 2c 20 20 32 35 2c 0a 20 20 20 20  , 106,  25,.    
32c20 20 20 39 32 2c 20 20 20 30 2c 20 20 20 30 2c 20    92,   0,   0, 
32c21 31 32 31 2c 20 31 30 32 2c 20 20 35 36 2c 20 31  121, 102,  56, 1
32c22 32 30 2c 20 20 35 33 2c 20 20 32 38 2c 20 20 35  20,  53,  28,  5
32c23 31 2c 20 20 20 30 2c 20 20 38 37 2c 20 20 20 30  1,   0,  87,   0
32c24 2c 0a 20 20 20 20 20 20 39 36 2c 20 20 32 36 2c  ,.      96,  26,
32c25 20 20 20 30 2c 20 20 39 35 2c 20 20 20 30 2c 20     0,  95,   0, 
32c26 20 20 30 2c 20 20 20 30 2c 20 20 39 31 2c 20 20    0,   0,  91,  
32c27 38 38 2c 20 20 39 33 2c 20 20 38 34 2c 20 31 30  88,  93,  84, 10
32c28 35 2c 20 20 31 34 2c 0a 20 20 20 20 20 20 33 39  5,  14,.      39
32c29 2c 20 31 30 34 2c 20 20 20 30 2c 20 20 37 37 2c  , 104,   0,  77,
32c2a 20 20 20 30 2c 20 20 31 38 2c 20 20 38 35 2c 20     0,  18,  85, 
32c2b 31 30 37 2c 20 20 33 32 2c 20 20 20 30 2c 20 31  107,  32,   0, 1
32c2c 31 37 2c 20 20 37 36 2c 20 31 30 39 2c 0a 20 20  17,  76, 109,.  
32c2d 20 20 20 20 35 37 2c 20 20 34 36 2c 20 20 38 30      57,  46,  80
32c2e 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 39 30 2c  ,   0,   0,  90,
32c2f 20 20 34 30 2c 20 20 20 30 2c 20 31 31 32 2c 20    40,   0, 112, 
32c30 20 20 30 2c 20 20 33 36 2c 20 20 20 30 2c 20 20    0,  36,   0,  
32c31 20 30 2c 0a 20 20 20 20 20 20 32 39 2c 20 20 20   0,.      29,   
32c32 30 2c 20 20 38 32 2c 20 20 35 39 2c 20 20 36 30  0,  82,  59,  60
32c33 2c 20 20 20 30 2c 20 20 32 30 2c 20 20 35 38 2c  ,   0,  20,  58,
32c34 20 20 20 30 2c 20 20 35 32 2c 0a 20 20 7d 3b 0a     0,  52,.  };.
32c35 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
32c36 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 4e 65  nsigned char aNe
32c37 78 74 5b 31 32 31 5d 20 3d 20 7b 0a 20 20 20 20  xt[121] = {.    
32c38 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
32c39 20 20 30 2c 20 20 20 34 2c 20 20 20 30 2c 20 20    0,   4,   0,  
32c3a 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
32c3b 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
32c3c 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 20 32 2c  ,.       0,   2,
32c3d 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
32c3e 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
32c3f 31 33 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20  13,   0,   0,   
32c40 30 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20 30  0,   0,.       0
32c41 2c 20 20 20 37 2c 20 20 20 30 2c 20 20 20 30 2c  ,   7,   0,   0,
32c42 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
32c43 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
32c44 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20 20   0,   0,   0,.  
32c45 20 20 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30       0,   0,   0
32c46 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 33 33 2c  ,   0,   0,  33,
32c47 20 20 32 31 2c 20 20 20 30 2c 20 20 20 30 2c 20    21,   0,   0, 
32c48 20 20 30 2c 20 20 34 33 2c 20 20 20 33 2c 20 20    0,  43,   3,  
32c49 34 37 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 20  47,.       0,   
32c4a 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
32c4b 2c 20 20 33 30 2c 20 20 35 34 2c 20 20 20 30 2c  ,  30,  54,   0,
32c4c 20 20 33 38 2c 20 20 20 30 2c 20 20 20 30 2c 20    38,   0,   0, 
32c4d 20 20 30 2c 20 20 20 31 2c 0a 20 20 20 20 20 20    0,   1,.      
32c4e 36 32 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 36  62,   0,   0,  6
32c4f 33 2c 20 20 20 30 2c 20 20 34 31 2c 20 20 20 30  3,   0,  41,   0
32c50 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
32c51 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a     0,   0,   0,.
32c52 20 20 20 20 20 20 36 31 2c 20 20 20 30 2c 20 20        61,   0,  
32c53 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 33   0,   0,   0,  3
32c54 31 2c 20 20 35 35 2c 20 20 31 36 2c 20 20 33 34  1,  55,  16,  34
32c55 2c 20 20 31 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,  10,   0,   0,
32c56 20 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c 20     0,.       0, 
32c57 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
32c58 31 31 2c 20 20 36 38 2c 20 20 37 35 2c 20 20 20  11,  68,  75,   
32c59 30 2c 20 20 20 38 2c 20 20 20 30 2c 20 31 30 30  0,   8,   0, 100
32c5a 2c 20 20 39 34 2c 20 20 20 30 2c 0a 20 20 20 20  ,  94,   0,.    
32c5b 20 31 30 33 2c 20 20 20 30 2c 20 20 38 33 2c 20   103,   0,  83, 
32c5c 20 20 30 2c 20 20 37 31 2c 20 20 20 30 2c 20 20    0,  71,   0,  
32c5d 20 30 2c 20 31 31 30 2c 20 20 32 37 2c 20 20 33   0, 110,  27,  3
32c5e 37 2c 20 20 36 39 2c 20 20 37 39 2c 20 20 20 30  7,  69,  79,   0
32c5f 2c 0a 20 20 20 20 20 20 33 35 2c 20 20 36 34 2c  ,.      35,  64,
32c60 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 7d 3b 0a     0,   0,.  };.
32c61 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
32c62 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 4c 65  nsigned char aLe
32c63 6e 5b 31 32 31 5d 20 3d 20 7b 0a 20 20 20 20 20  n[121] = {.     
32c64 20 20 37 2c 20 20 20 37 2c 20 20 20 35 2c 20 20    7,   7,   5,  
32c65 20 34 2c 20 20 20 36 2c 20 20 20 34 2c 20 20 20   4,   6,   4,   
32c66 35 2c 20 20 20 33 2c 20 20 20 36 2c 20 20 20 37  5,   3,   6,   7
32c67 2c 20 20 20 33 2c 20 20 20 36 2c 20 20 20 36 2c  ,   3,   6,   6,
32c68 0a 20 20 20 20 20 20 20 37 2c 20 20 20 37 2c 20  .       7,   7, 
32c69 20 20 33 2c 20 20 20 38 2c 20 20 20 32 2c 20 20    3,   8,   2,  
32c6a 20 36 2c 20 20 20 35 2c 20 20 20 34 2c 20 20 20   6,   5,   4,   
32c6b 34 2c 20 20 20 33 2c 20 20 31 30 2c 20 20 20 34  4,   3,  10,   4
32c6c 2c 20 20 20 36 2c 0a 20 20 20 20 20 20 31 31 2c  ,   6,.      11,
32c6d 20 20 20 36 2c 20 20 20 32 2c 20 20 20 37 2c 20     6,   2,   7, 
32c6e 20 20 35 2c 20 20 20 35 2c 20 20 20 39 2c 20 20    5,   5,   9,  
32c6f 20 36 2c 20 20 20 39 2c 20 20 20 39 2c 20 20 20   6,   9,   9,   
32c70 37 2c 20 20 31 30 2c 20 20 31 30 2c 0a 20 20 20  7,  10,  10,.   
32c71 20 20 20 20 34 2c 20 20 20 36 2c 20 20 20 32 2c      4,   6,   2,
32c72 20 20 20 33 2c 20 20 20 34 2c 20 20 20 39 2c 20     3,   4,   9, 
32c73 20 20 32 2c 20 20 20 36 2c 20 20 20 35 2c 20 20    2,   6,   5,  
32c74 20 36 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20   6,   6,   5,   
32c75 36 2c 0a 20 20 20 20 20 20 20 35 2c 20 20 20 35  6,.       5,   5
32c76 2c 20 20 20 37 2c 20 20 20 37 2c 20 20 20 33 2c  ,   7,   7,   3,
32c77 20 20 20 37 2c 20 20 20 32 2c 20 20 20 34 2c 20     7,   2,   4, 
32c78 20 20 34 2c 20 20 20 37 2c 20 20 20 33 2c 20 20    4,   7,   3,  
32c79 20 36 2c 20 20 20 34 2c 0a 20 20 20 20 20 20 20   6,   4,.       
32c7a 37 2c 20 20 20 36 2c 20 20 31 32 2c 20 20 20 36  7,   6,  12,   6
32c7b 2c 20 20 20 39 2c 20 20 20 34 2c 20 20 20 36 2c  ,   9,   4,   6,
32c7c 20 20 20 35 2c 20 20 20 34 2c 20 20 20 37 2c 20     5,   4,   7, 
32c7d 20 20 36 2c 20 20 20 35 2c 20 20 20 36 2c 0a 20    6,   5,   6,. 
32c7e 20 20 20 20 20 20 37 2c 20 20 20 35 2c 20 20 20        7,   5,   
32c7f 34 2c 20 20 20 35 2c 20 20 20 36 2c 20 20 20 35  4,   5,   6,   5
32c80 2c 20 20 20 37 2c 20 20 20 33 2c 20 20 20 37 2c  ,   7,   3,   7,
32c81 20 20 31 33 2c 20 20 20 32 2c 20 20 20 32 2c 20    13,   2,   2, 
32c82 20 20 34 2c 0a 20 20 20 20 20 20 20 36 2c 20 20    4,.       6,  
32c83 20 36 2c 20 20 20 38 2c 20 20 20 35 2c 20 20 31   6,   8,   5,  1
32c84 37 2c 20 20 31 32 2c 20 20 20 37 2c 20 20 20 38  7,  12,   7,   8
32c85 2c 20 20 20 38 2c 20 20 20 32 2c 20 20 20 34 2c  ,   8,   2,   4,
32c86 20 20 20 34 2c 20 20 20 34 2c 0a 20 20 20 20 20     4,   4,.     
32c87 20 20 34 2c 20 20 20 34 2c 20 20 20 32 2c 20 20    4,   4,   2,  
32c88 20 32 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20   2,   6,   5,   
32c89 38 2c 20 20 20 35 2c 20 20 20 35 2c 20 20 20 38  8,   5,   5,   8
32c8a 2c 20 20 20 33 2c 20 20 20 35 2c 20 20 20 35 2c  ,   3,   5,   5,
32c8b 0a 20 20 20 20 20 20 20 36 2c 20 20 20 34 2c 20  .       6,   4, 
32c8c 20 20 39 2c 20 20 20 33 2c 0a 20 20 7d 3b 0a 20    9,   3,.  };. 
32c8d 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
32c8e 73 69 67 6e 65 64 20 73 68 6f 72 74 20 69 6e 74  signed short int
32c8f 20 61 4f 66 66 73 65 74 5b 31 32 31 5d 20 3d 20   aOffset[121] = 
32c90 7b 0a 20 20 20 20 20 20 20 30 2c 20 20 20 32 2c  {.       0,   2,
32c91 20 20 20 32 2c 20 20 20 38 2c 20 20 20 39 2c 20     2,   8,   9, 
32c92 20 31 34 2c 20 20 31 36 2c 20 20 32 30 2c 20 20   14,  16,  20,  
32c93 32 33 2c 20 20 32 35 2c 20 20 32 35 2c 20 20 32  23,  25,  25,  2
32c94 39 2c 20 20 33 33 2c 0a 20 20 20 20 20 20 33 36  9,  33,.      36
32c95 2c 20 20 34 31 2c 20 20 34 36 2c 20 20 34 38 2c  ,  41,  46,  48,
32c96 20 20 35 33 2c 20 20 35 34 2c 20 20 35 39 2c 20    53,  54,  59, 
32c97 20 36 32 2c 20 20 36 35 2c 20 20 36 37 2c 20 20   62,  65,  67,  
32c98 36 39 2c 20 20 37 38 2c 20 20 38 31 2c 0a 20 20  69,  78,  81,.  
32c99 20 20 20 20 38 36 2c 20 20 39 31 2c 20 20 39 35      86,  91,  95
32c9a 2c 20 20 39 36 2c 20 31 30 31 2c 20 31 30 35 2c  ,  96, 101, 105,
32c9b 20 31 30 39 2c 20 31 31 37 2c 20 31 32 32 2c 20   109, 117, 122, 
32c9c 31 32 38 2c 20 31 33 36 2c 20 31 34 32 2c 20 31  128, 136, 142, 1
32c9d 35 32 2c 0a 20 20 20 20 20 31 35 39 2c 20 31 36  52,.     159, 16
32c9e 32 2c 20 31 36 32 2c 20 31 36 35 2c 20 31 36 37  2, 162, 165, 167
32c9f 2c 20 31 36 37 2c 20 31 37 31 2c 20 31 37 36 2c  , 167, 171, 176,
32ca0 20 31 37 39 2c 20 31 38 34 2c 20 31 38 39 2c 20   179, 184, 189, 
32ca1 31 39 34 2c 20 31 39 37 2c 0a 20 20 20 20 20 32  194, 197,.     2
32ca2 30 33 2c 20 32 30 36 2c 20 32 31 30 2c 20 32 31  03, 206, 210, 21
32ca3 37 2c 20 32 32 33 2c 20 32 32 33 2c 20 32 32 33  7, 223, 223, 223
32ca4 2c 20 32 32 36 2c 20 32 32 39 2c 20 32 33 33 2c  , 226, 229, 233,
32ca5 20 32 33 34 2c 20 32 33 38 2c 20 32 34 34 2c 0a   234, 238, 244,.
32ca6 20 20 20 20 20 32 34 38 2c 20 32 35 35 2c 20 32       248, 255, 2
32ca7 36 31 2c 20 32 37 33 2c 20 32 37 39 2c 20 32 38  61, 273, 279, 28
32ca8 38 2c 20 32 39 30 2c 20 32 39 36 2c 20 33 30 31  8, 290, 296, 301
32ca9 2c 20 33 30 33 2c 20 33 31 30 2c 20 33 31 35 2c  , 303, 310, 315,
32caa 20 33 32 30 2c 0a 20 20 20 20 20 33 32 36 2c 20   320,.     326, 
32cab 33 33 32 2c 20 33 33 37 2c 20 33 34 31 2c 20 33  332, 337, 341, 3
32cac 34 34 2c 20 33 35 30 2c 20 33 35 34 2c 20 33 36  44, 350, 354, 36
32cad 31 2c 20 33 36 33 2c 20 33 37 30 2c 20 33 37 32  1, 363, 370, 372
32cae 2c 20 33 37 34 2c 20 33 38 33 2c 0a 20 20 20 20  , 374, 383,.    
32caf 20 33 38 37 2c 20 33 39 33 2c 20 33 39 39 2c 20   387, 393, 399, 
32cb0 34 30 37 2c 20 34 31 32 2c 20 34 31 32 2c 20 34  407, 412, 412, 4
32cb1 32 38 2c 20 34 33 35 2c 20 34 34 32 2c 20 34 34  28, 435, 442, 44
32cb2 33 2c 20 34 35 30 2c 20 34 35 34 2c 20 34 35 38  3, 450, 454, 458
32cb3 2c 0a 20 20 20 20 20 34 36 32 2c 20 34 36 36 2c  ,.     462, 466,
32cb4 20 34 36 39 2c 20 34 37 31 2c 20 34 37 33 2c 20   469, 471, 473, 
32cb5 34 37 39 2c 20 34 38 33 2c 20 34 39 31 2c 20 34  479, 483, 491, 4
32cb6 39 35 2c 20 35 30 30 2c 20 35 30 38 2c 20 35 31  95, 500, 508, 51
32cb7 31 2c 20 35 31 36 2c 0a 20 20 20 20 20 35 32 31  1, 516,.     521
32cb8 2c 20 35 32 37 2c 20 35 33 31 2c 20 35 33 36 2c  , 527, 531, 536,
32cb9 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63  .  };.  static c
32cba 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
32cbb 61 72 20 61 43 6f 64 65 5b 31 32 31 5d 20 3d 20  ar aCode[121] = 
32cbc 7b 0a 20 20 20 20 54 4b 5f 52 45 49 4e 44 45 58  {.    TK_REINDEX
32cbd 2c 20 20 20 20 54 4b 5f 49 4e 44 45 58 45 44 2c  ,    TK_INDEXED,
32cbe 20 20 20 20 54 4b 5f 49 4e 44 45 58 2c 20 20 20      TK_INDEX,   
32cbf 20 20 20 54 4b 5f 44 45 53 43 2c 20 20 20 20 20     TK_DESC,     
32cc0 20 20 54 4b 5f 45 53 43 41 50 45 2c 20 20 20 20    TK_ESCAPE,    
32cc1 20 0a 20 20 20 20 54 4b 5f 45 41 43 48 2c 20 20   .    TK_EACH,  
32cc2 20 20 20 20 20 54 4b 5f 43 48 45 43 4b 2c 20 20       TK_CHECK,  
32cc3 20 20 20 20 54 4b 5f 4b 45 59 2c 20 20 20 20 20      TK_KEY,     
32cc4 20 20 20 54 4b 5f 42 45 46 4f 52 45 2c 20 20 20     TK_BEFORE,   
32cc5 20 20 54 4b 5f 46 4f 52 45 49 47 4e 2c 20 20 20    TK_FOREIGN,   
32cc6 20 0a 20 20 20 20 54 4b 5f 46 4f 52 2c 20 20 20   .    TK_FOR,   
32cc7 20 20 20 20 20 54 4b 5f 49 47 4e 4f 52 45 2c 20       TK_IGNORE, 
32cc8 20 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c 20      TK_LIKE_KW, 
32cc9 20 20 20 54 4b 5f 45 58 50 4c 41 49 4e 2c 20 20     TK_EXPLAIN,  
32cca 20 20 54 4b 5f 49 4e 53 54 45 41 44 2c 20 20 20    TK_INSTEAD,   
32ccb 20 0a 20 20 20 20 54 4b 5f 41 44 44 2c 20 20 20   .    TK_ADD,   
32ccc 20 20 20 20 20 54 4b 5f 44 41 54 41 42 41 53 45       TK_DATABASE
32ccd 2c 20 20 20 54 4b 5f 41 53 2c 20 20 20 20 20 20  ,   TK_AS,      
32cce 20 20 20 54 4b 5f 53 45 4c 45 43 54 2c 20 20 20     TK_SELECT,   
32ccf 20 20 54 4b 5f 54 41 42 4c 45 2c 20 20 20 20 20    TK_TABLE,     
32cd0 20 0a 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57   .    TK_JOIN_KW
32cd1 2c 20 20 20 20 54 4b 5f 54 48 45 4e 2c 20 20 20  ,    TK_THEN,   
32cd2 20 20 20 20 54 4b 5f 45 4e 44 2c 20 20 20 20 20      TK_END,     
32cd3 20 20 20 54 4b 5f 44 45 46 45 52 52 41 42 4c 45     TK_DEFERRABLE
32cd4 2c 20 54 4b 5f 45 4c 53 45 2c 20 20 20 20 20 20  , TK_ELSE,      
32cd5 20 0a 20 20 20 20 54 4b 5f 45 58 43 45 50 54 2c   .    TK_EXCEPT,
32cd6 20 20 20 20 20 54 4b 5f 54 52 41 4e 53 41 43 54       TK_TRANSACT
32cd7 49 4f 4e 2c 54 4b 5f 41 43 54 49 4f 4e 2c 20 20  ION,TK_ACTION,  
32cd8 20 20 20 54 4b 5f 4f 4e 2c 20 20 20 20 20 20 20     TK_ON,       
32cd9 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20    TK_JOIN_KW,   
32cda 20 0a 20 20 20 20 54 4b 5f 41 4c 54 45 52 2c 20   .    TK_ALTER, 
32cdb 20 20 20 20 20 54 4b 5f 52 41 49 53 45 2c 20 20       TK_RAISE,  
32cdc 20 20 20 20 54 4b 5f 45 58 43 4c 55 53 49 56 45      TK_EXCLUSIVE
32cdd 2c 20 20 54 4b 5f 45 58 49 53 54 53 2c 20 20 20  ,  TK_EXISTS,   
32cde 20 20 54 4b 5f 53 41 56 45 50 4f 49 4e 54 2c 20    TK_SAVEPOINT, 
32cdf 20 0a 20 20 20 20 54 4b 5f 49 4e 54 45 52 53 45   .    TK_INTERSE
32ce0 43 54 2c 20 20 54 4b 5f 54 52 49 47 47 45 52 2c  CT,  TK_TRIGGER,
32ce1 20 20 20 20 54 4b 5f 52 45 46 45 52 45 4e 43 45      TK_REFERENCE
32ce2 53 2c 20 54 4b 5f 43 4f 4e 53 54 52 41 49 4e 54  S, TK_CONSTRAINT
32ce3 2c 20 54 4b 5f 49 4e 54 4f 2c 20 20 20 20 20 20  , TK_INTO,      
32ce4 20 0a 20 20 20 20 54 4b 5f 4f 46 46 53 45 54 2c   .    TK_OFFSET,
32ce5 20 20 20 20 20 54 4b 5f 4f 46 2c 20 20 20 20 20       TK_OF,     
32ce6 20 20 20 20 54 4b 5f 53 45 54 2c 20 20 20 20 20      TK_SET,     
32ce7 20 20 20 54 4b 5f 54 45 4d 50 2c 20 20 20 20 20     TK_TEMP,     
32ce8 20 20 54 4b 5f 54 45 4d 50 2c 20 20 20 20 20 20    TK_TEMP,      
32ce9 20 0a 20 20 20 20 54 4b 5f 4f 52 2c 20 20 20 20   .    TK_OR,    
32cea 20 20 20 20 20 54 4b 5f 55 4e 49 51 55 45 2c 20       TK_UNIQUE, 
32ceb 20 20 20 20 54 4b 5f 51 55 45 52 59 2c 20 20 20      TK_QUERY,   
32cec 20 20 20 54 4b 5f 41 54 54 41 43 48 2c 20 20 20     TK_ATTACH,   
32ced 20 20 54 4b 5f 48 41 56 49 4e 47 2c 20 20 20 20    TK_HAVING,    
32cee 20 0a 20 20 20 20 54 4b 5f 47 52 4f 55 50 2c 20   .    TK_GROUP, 
32cef 20 20 20 20 20 54 4b 5f 55 50 44 41 54 45 2c 20       TK_UPDATE, 
32cf0 20 20 20 20 54 4b 5f 42 45 47 49 4e 2c 20 20 20      TK_BEGIN,   
32cf1 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20     TK_JOIN_KW,  
32cf2 20 20 54 4b 5f 52 45 4c 45 41 53 45 2c 20 20 20    TK_RELEASE,   
32cf3 20 0a 20 20 20 20 54 4b 5f 42 45 54 57 45 45 4e   .    TK_BETWEEN
32cf4 2c 20 20 20 20 54 4b 5f 4e 4f 54 2c 20 20 20 20  ,    TK_NOT,    
32cf5 20 20 20 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20      TK_NOTNULL, 
32cf6 20 20 20 54 4b 5f 4e 4f 2c 20 20 20 20 20 20 20     TK_NO,       
32cf7 20 20 54 4b 5f 4e 55 4c 4c 2c 20 20 20 20 20 20    TK_NULL,      
32cf8 20 0a 20 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57   .    TK_LIKE_KW
32cf9 2c 20 20 20 20 54 4b 5f 43 41 53 43 41 44 45 2c  ,    TK_CASCADE,
32cfa 20 20 20 20 54 4b 5f 41 53 43 2c 20 20 20 20 20      TK_ASC,     
32cfb 20 20 20 54 4b 5f 44 45 4c 45 54 45 2c 20 20 20     TK_DELETE,   
32cfc 20 20 54 4b 5f 43 41 53 45 2c 20 20 20 20 20 20    TK_CASE,      
32cfd 20 0a 20 20 20 20 54 4b 5f 43 4f 4c 4c 41 54 45   .    TK_COLLATE
32cfe 2c 20 20 20 20 54 4b 5f 43 52 45 41 54 45 2c 20  ,    TK_CREATE, 
32cff 20 20 20 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 2c      TK_CTIME_KW,
32d00 20 20 20 54 4b 5f 44 45 54 41 43 48 2c 20 20 20     TK_DETACH,   
32d01 20 20 54 4b 5f 49 4d 4d 45 44 49 41 54 45 2c 20    TK_IMMEDIATE, 
32d02 20 0a 20 20 20 20 54 4b 5f 4a 4f 49 4e 2c 20 20   .    TK_JOIN,  
32d03 20 20 20 20 20 54 4b 5f 49 4e 53 45 52 54 2c 20       TK_INSERT, 
32d04 20 20 20 20 54 4b 5f 4d 41 54 43 48 2c 20 20 20      TK_MATCH,   
32d05 20 20 20 54 4b 5f 50 4c 41 4e 2c 20 20 20 20 20     TK_PLAN,     
32d06 20 20 54 4b 5f 41 4e 41 4c 59 5a 45 2c 20 20 20    TK_ANALYZE,   
32d07 20 0a 20 20 20 20 54 4b 5f 50 52 41 47 4d 41 2c   .    TK_PRAGMA,
32d08 20 20 20 20 20 54 4b 5f 41 42 4f 52 54 2c 20 20       TK_ABORT,  
32d09 20 20 20 20 54 4b 5f 56 41 4c 55 45 53 2c 20 20      TK_VALUES,  
32d0a 20 20 20 54 4b 5f 56 49 52 54 55 41 4c 2c 20 20     TK_VIRTUAL,  
32d0b 20 20 54 4b 5f 4c 49 4d 49 54 2c 20 20 20 20 20    TK_LIMIT,     
32d0c 20 0a 20 20 20 20 54 4b 5f 57 48 45 4e 2c 20 20   .    TK_WHEN,  
32d0d 20 20 20 20 20 54 4b 5f 57 48 45 52 45 2c 20 20       TK_WHERE,  
32d0e 20 20 20 20 54 4b 5f 52 45 4e 41 4d 45 2c 20 20      TK_RENAME,  
32d0f 20 20 20 54 4b 5f 41 46 54 45 52 2c 20 20 20 20     TK_AFTER,    
32d10 20 20 54 4b 5f 52 45 50 4c 41 43 45 2c 20 20 20    TK_REPLACE,   
32d11 20 0a 20 20 20 20 54 4b 5f 41 4e 44 2c 20 20 20   .    TK_AND,   
32d12 20 20 20 20 20 54 4b 5f 44 45 46 41 55 4c 54 2c       TK_DEFAULT,
32d13 20 20 20 20 54 4b 5f 41 55 54 4f 49 4e 43 52 2c      TK_AUTOINCR,
32d14 20 20 20 54 4b 5f 54 4f 2c 20 20 20 20 20 20 20     TK_TO,       
32d15 20 20 54 4b 5f 49 4e 2c 20 20 20 20 20 20 20 20    TK_IN,        
32d16 20 0a 20 20 20 20 54 4b 5f 43 41 53 54 2c 20 20   .    TK_CAST,  
32d17 20 20 20 20 20 54 4b 5f 43 4f 4c 55 4d 4e 4b 57       TK_COLUMNKW
32d18 2c 20 20 20 54 4b 5f 43 4f 4d 4d 49 54 2c 20 20  ,   TK_COMMIT,  
32d19 20 20 20 54 4b 5f 43 4f 4e 46 4c 49 43 54 2c 20     TK_CONFLICT, 
32d1a 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20    TK_JOIN_KW,   
32d1b 20 0a 20 20 20 20 54 4b 5f 43 54 49 4d 45 5f 4b   .    TK_CTIME_K
32d1c 57 2c 20 20 20 54 4b 5f 43 54 49 4d 45 5f 4b 57  W,   TK_CTIME_KW
32d1d 2c 20 20 20 54 4b 5f 50 52 49 4d 41 52 59 2c 20  ,   TK_PRIMARY, 
32d1e 20 20 20 54 4b 5f 44 45 46 45 52 52 45 44 2c 20     TK_DEFERRED, 
32d1f 20 20 54 4b 5f 44 49 53 54 49 4e 43 54 2c 20 20    TK_DISTINCT,  
32d20 20 0a 20 20 20 20 54 4b 5f 49 53 2c 20 20 20 20   .    TK_IS,    
32d21 20 20 20 20 20 54 4b 5f 44 52 4f 50 2c 20 20 20       TK_DROP,   
32d22 20 20 20 20 54 4b 5f 46 41 49 4c 2c 20 20 20 20      TK_FAIL,    
32d23 20 20 20 54 4b 5f 46 52 4f 4d 2c 20 20 20 20 20     TK_FROM,     
32d24 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20 20 20    TK_JOIN_KW,   
32d25 20 0a 20 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57   .    TK_LIKE_KW
32d26 2c 20 20 20 20 54 4b 5f 42 59 2c 20 20 20 20 20  ,    TK_BY,     
32d27 20 20 20 20 54 4b 5f 49 46 2c 20 20 20 20 20 20      TK_IF,      
32d28 20 20 20 54 4b 5f 49 53 4e 55 4c 4c 2c 20 20 20     TK_ISNULL,   
32d29 20 20 54 4b 5f 4f 52 44 45 52 2c 20 20 20 20 20    TK_ORDER,     
32d2a 20 0a 20 20 20 20 54 4b 5f 52 45 53 54 52 49 43   .    TK_RESTRIC
32d2b 54 2c 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c  T,   TK_JOIN_KW,
32d2c 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c 20      TK_JOIN_KW, 
32d2d 20 20 20 54 4b 5f 52 4f 4c 4c 42 41 43 4b 2c 20     TK_ROLLBACK, 
32d2e 20 20 54 4b 5f 52 4f 57 2c 20 20 20 20 20 20 20    TK_ROW,       
32d2f 20 0a 20 20 20 20 54 4b 5f 55 4e 49 4f 4e 2c 20   .    TK_UNION, 
32d30 20 20 20 20 20 54 4b 5f 55 53 49 4e 47 2c 20 20       TK_USING,  
32d31 20 20 20 20 54 4b 5f 56 41 43 55 55 4d 2c 20 20      TK_VACUUM,  
32d32 20 20 20 54 4b 5f 56 49 45 57 2c 20 20 20 20 20     TK_VIEW,     
32d33 20 20 54 4b 5f 49 4e 49 54 49 41 4c 4c 59 2c 20    TK_INITIALLY, 
32d34 20 0a 20 20 20 20 54 4b 5f 41 4c 4c 2c 20 20 20   .    TK_ALL,   
32d35 20 20 20 20 20 0a 20 20 7d 3b 0a 20 20 69 6e 74       .  };.  int
32d36 20 68 2c 20 69 3b 0a 20 20 69 66 28 20 6e 3c 32   h, i;.  if( n<2
32d37 20 29 20 72 65 74 75 72 6e 20 54 4b 5f 49 44 3b   ) return TK_ID;
32d38 0a 20 20 68 20 3d 20 28 28 63 68 61 72 4d 61 70  .  h = ((charMap
32d39 28 7a 5b 30 5d 29 2a 34 29 20 5e 0a 20 20 20 20  (z[0])*4) ^.    
32d3a 20 20 28 63 68 61 72 4d 61 70 28 7a 5b 6e 2d 31    (charMap(z[n-1
32d3b 5d 29 2a 33 29 20 5e 0a 20 20 20 20 20 20 6e 29  ])*3) ^.      n)
32d3c 20 25 20 31 32 37 3b 0a 20 20 66 6f 72 28 69 3d   % 127;.  for(i=
32d3d 28 28 69 6e 74 29 61 48 61 73 68 5b 68 5d 29 2d  ((int)aHash[h])-
32d3e 31 3b 20 69 3e 3d 30 3b 20 69 3d 28 28 69 6e 74  1; i>=0; i=((int
32d3f 29 61 4e 65 78 74 5b 69 5d 29 2d 31 29 7b 0a 20  )aNext[i])-1){. 
32d40 20 20 20 69 66 28 20 61 4c 65 6e 5b 69 5d 3d 3d     if( aLen[i]==
32d41 6e 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e  n && sqlite3StrN
32d42 49 43 6d 70 28 26 7a 54 65 78 74 5b 61 4f 66 66  ICmp(&zText[aOff
32d43 73 65 74 5b 69 5d 5d 2c 7a 2c 6e 29 3d 3d 30 20  set[i]],z,n)==0 
32d44 29 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  ){.      testcas
32d45 65 28 20 69 3d 3d 30 20 29 3b 20 2f 2a 20 52 45  e( i==0 ); /* RE
32d46 49 4e 44 45 58 20 2a 2f 0a 20 20 20 20 20 20 74  INDEX */.      t
32d47 65 73 74 63 61 73 65 28 20 69 3d 3d 31 20 29 3b  estcase( i==1 );
32d48 20 2f 2a 20 49 4e 44 45 58 45 44 20 2a 2f 0a 20   /* INDEXED */. 
32d49 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32d4a 3d 3d 32 20 29 3b 20 2f 2a 20 49 4e 44 45 58 20  ==2 ); /* INDEX 
32d4b 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32d4c 65 28 20 69 3d 3d 33 20 29 3b 20 2f 2a 20 44 45  e( i==3 ); /* DE
32d4d 53 43 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  SC */.      test
32d4e 63 61 73 65 28 20 69 3d 3d 34 20 29 3b 20 2f 2a  case( i==4 ); /*
32d4f 20 45 53 43 41 50 45 20 2a 2f 0a 20 20 20 20 20   ESCAPE */.     
32d50 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 20   testcase( i==5 
32d51 29 3b 20 2f 2a 20 45 41 43 48 20 2a 2f 0a 20 20  ); /* EACH */.  
32d52 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32d53 3d 36 20 29 3b 20 2f 2a 20 43 48 45 43 4b 20 2a  =6 ); /* CHECK *
32d54 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32d55 28 20 69 3d 3d 37 20 29 3b 20 2f 2a 20 4b 45 59  ( i==7 ); /* KEY
32d56 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32d57 73 65 28 20 69 3d 3d 38 20 29 3b 20 2f 2a 20 42  se( i==8 ); /* B
32d58 45 46 4f 52 45 20 2a 2f 0a 20 20 20 20 20 20 74  EFORE */.      t
32d59 65 73 74 63 61 73 65 28 20 69 3d 3d 39 20 29 3b  estcase( i==9 );
32d5a 20 2f 2a 20 46 4f 52 45 49 47 4e 20 2a 2f 0a 20   /* FOREIGN */. 
32d5b 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32d5c 3d 3d 31 30 20 29 3b 20 2f 2a 20 46 4f 52 20 2a  ==10 ); /* FOR *
32d5d 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32d5e 28 20 69 3d 3d 31 31 20 29 3b 20 2f 2a 20 49 47  ( i==11 ); /* IG
32d5f 4e 4f 52 45 20 2a 2f 0a 20 20 20 20 20 20 74 65  NORE */.      te
32d60 73 74 63 61 73 65 28 20 69 3d 3d 31 32 20 29 3b  stcase( i==12 );
32d61 20 2f 2a 20 52 45 47 45 58 50 20 2a 2f 0a 20 20   /* REGEXP */.  
32d62 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32d63 3d 31 33 20 29 3b 20 2f 2a 20 45 58 50 4c 41 49  =13 ); /* EXPLAI
32d64 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  N */.      testc
32d65 61 73 65 28 20 69 3d 3d 31 34 20 29 3b 20 2f 2a  ase( i==14 ); /*
32d66 20 49 4e 53 54 45 41 44 20 2a 2f 0a 20 20 20 20   INSTEAD */.    
32d67 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
32d68 35 20 29 3b 20 2f 2a 20 41 44 44 20 2a 2f 0a 20  5 ); /* ADD */. 
32d69 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32d6a 3d 3d 31 36 20 29 3b 20 2f 2a 20 44 41 54 41 42  ==16 ); /* DATAB
32d6b 41 53 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ASE */.      tes
32d6c 74 63 61 73 65 28 20 69 3d 3d 31 37 20 29 3b 20  tcase( i==17 ); 
32d6d 2f 2a 20 41 53 20 2a 2f 0a 20 20 20 20 20 20 74  /* AS */.      t
32d6e 65 73 74 63 61 73 65 28 20 69 3d 3d 31 38 20 29  estcase( i==18 )
32d6f 3b 20 2f 2a 20 53 45 4c 45 43 54 20 2a 2f 0a 20  ; /* SELECT */. 
32d70 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32d71 3d 3d 31 39 20 29 3b 20 2f 2a 20 54 41 42 4c 45  ==19 ); /* TABLE
32d72 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32d73 73 65 28 20 69 3d 3d 32 30 20 29 3b 20 2f 2a 20  se( i==20 ); /* 
32d74 4c 45 46 54 20 2a 2f 0a 20 20 20 20 20 20 74 65  LEFT */.      te
32d75 73 74 63 61 73 65 28 20 69 3d 3d 32 31 20 29 3b  stcase( i==21 );
32d76 20 2f 2a 20 54 48 45 4e 20 2a 2f 0a 20 20 20 20   /* THEN */.    
32d77 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 32    testcase( i==2
32d78 32 20 29 3b 20 2f 2a 20 45 4e 44 20 2a 2f 0a 20  2 ); /* END */. 
32d79 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32d7a 3d 3d 32 33 20 29 3b 20 2f 2a 20 44 45 46 45 52  ==23 ); /* DEFER
32d7b 52 41 42 4c 45 20 2a 2f 0a 20 20 20 20 20 20 74  RABLE */.      t
32d7c 65 73 74 63 61 73 65 28 20 69 3d 3d 32 34 20 29  estcase( i==24 )
32d7d 3b 20 2f 2a 20 45 4c 53 45 20 2a 2f 0a 20 20 20  ; /* ELSE */.   
32d7e 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32d7f 32 35 20 29 3b 20 2f 2a 20 45 58 43 45 50 54 20  25 ); /* EXCEPT 
32d80 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32d81 65 28 20 69 3d 3d 32 36 20 29 3b 20 2f 2a 20 54  e( i==26 ); /* T
32d82 52 41 4e 53 41 43 54 49 4f 4e 20 2a 2f 0a 20 20  RANSACTION */.  
32d83 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32d84 3d 32 37 20 29 3b 20 2f 2a 20 41 43 54 49 4f 4e  =27 ); /* ACTION
32d85 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32d86 73 65 28 20 69 3d 3d 32 38 20 29 3b 20 2f 2a 20  se( i==28 ); /* 
32d87 4f 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  ON */.      test
32d88 63 61 73 65 28 20 69 3d 3d 32 39 20 29 3b 20 2f  case( i==29 ); /
32d89 2a 20 4e 41 54 55 52 41 4c 20 2a 2f 0a 20 20 20  * NATURAL */.   
32d8a 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32d8b 33 30 20 29 3b 20 2f 2a 20 41 4c 54 45 52 20 2a  30 ); /* ALTER *
32d8c 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32d8d 28 20 69 3d 3d 33 31 20 29 3b 20 2f 2a 20 52 41  ( i==31 ); /* RA
32d8e 49 53 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ISE */.      tes
32d8f 74 63 61 73 65 28 20 69 3d 3d 33 32 20 29 3b 20  tcase( i==32 ); 
32d90 2f 2a 20 45 58 43 4c 55 53 49 56 45 20 2a 2f 0a  /* EXCLUSIVE */.
32d91 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32d92 69 3d 3d 33 33 20 29 3b 20 2f 2a 20 45 58 49 53  i==33 ); /* EXIS
32d93 54 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  TS */.      test
32d94 63 61 73 65 28 20 69 3d 3d 33 34 20 29 3b 20 2f  case( i==34 ); /
32d95 2a 20 53 41 56 45 50 4f 49 4e 54 20 2a 2f 0a 20  * SAVEPOINT */. 
32d96 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32d97 3d 3d 33 35 20 29 3b 20 2f 2a 20 49 4e 54 45 52  ==35 ); /* INTER
32d98 53 45 43 54 20 2a 2f 0a 20 20 20 20 20 20 74 65  SECT */.      te
32d99 73 74 63 61 73 65 28 20 69 3d 3d 33 36 20 29 3b  stcase( i==36 );
32d9a 20 2f 2a 20 54 52 49 47 47 45 52 20 2a 2f 0a 20   /* TRIGGER */. 
32d9b 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32d9c 3d 3d 33 37 20 29 3b 20 2f 2a 20 52 45 46 45 52  ==37 ); /* REFER
32d9d 45 4e 43 45 53 20 2a 2f 0a 20 20 20 20 20 20 74  ENCES */.      t
32d9e 65 73 74 63 61 73 65 28 20 69 3d 3d 33 38 20 29  estcase( i==38 )
32d9f 3b 20 2f 2a 20 43 4f 4e 53 54 52 41 49 4e 54 20  ; /* CONSTRAINT 
32da0 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32da1 65 28 20 69 3d 3d 33 39 20 29 3b 20 2f 2a 20 49  e( i==39 ); /* I
32da2 4e 54 4f 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  NTO */.      tes
32da3 74 63 61 73 65 28 20 69 3d 3d 34 30 20 29 3b 20  tcase( i==40 ); 
32da4 2f 2a 20 4f 46 46 53 45 54 20 2a 2f 0a 20 20 20  /* OFFSET */.   
32da5 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32da6 34 31 20 29 3b 20 2f 2a 20 4f 46 20 2a 2f 0a 20  41 ); /* OF */. 
32da7 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32da8 3d 3d 34 32 20 29 3b 20 2f 2a 20 53 45 54 20 2a  ==42 ); /* SET *
32da9 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32daa 28 20 69 3d 3d 34 33 20 29 3b 20 2f 2a 20 54 45  ( i==43 ); /* TE
32dab 4d 50 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  MP */.      test
32dac 63 61 73 65 28 20 69 3d 3d 34 34 20 29 3b 20 2f  case( i==44 ); /
32dad 2a 20 54 45 4d 50 4f 52 41 52 59 20 2a 2f 0a 20  * TEMPORARY */. 
32dae 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32daf 3d 3d 34 35 20 29 3b 20 2f 2a 20 4f 52 20 2a 2f  ==45 ); /* OR */
32db0 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32db1 20 69 3d 3d 34 36 20 29 3b 20 2f 2a 20 55 4e 49   i==46 ); /* UNI
32db2 51 55 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  QUE */.      tes
32db3 74 63 61 73 65 28 20 69 3d 3d 34 37 20 29 3b 20  tcase( i==47 ); 
32db4 2f 2a 20 51 55 45 52 59 20 2a 2f 0a 20 20 20 20  /* QUERY */.    
32db5 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 34    testcase( i==4
32db6 38 20 29 3b 20 2f 2a 20 41 54 54 41 43 48 20 2a  8 ); /* ATTACH *
32db7 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32db8 28 20 69 3d 3d 34 39 20 29 3b 20 2f 2a 20 48 41  ( i==49 ); /* HA
32db9 56 49 4e 47 20 2a 2f 0a 20 20 20 20 20 20 74 65  VING */.      te
32dba 73 74 63 61 73 65 28 20 69 3d 3d 35 30 20 29 3b  stcase( i==50 );
32dbb 20 2f 2a 20 47 52 4f 55 50 20 2a 2f 0a 20 20 20   /* GROUP */.   
32dbc 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32dbd 35 31 20 29 3b 20 2f 2a 20 55 50 44 41 54 45 20  51 ); /* UPDATE 
32dbe 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32dbf 65 28 20 69 3d 3d 35 32 20 29 3b 20 2f 2a 20 42  e( i==52 ); /* B
32dc0 45 47 49 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65  EGIN */.      te
32dc1 73 74 63 61 73 65 28 20 69 3d 3d 35 33 20 29 3b  stcase( i==53 );
32dc2 20 2f 2a 20 49 4e 4e 45 52 20 2a 2f 0a 20 20 20   /* INNER */.   
32dc3 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32dc4 35 34 20 29 3b 20 2f 2a 20 52 45 4c 45 41 53 45  54 ); /* RELEASE
32dc5 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32dc6 73 65 28 20 69 3d 3d 35 35 20 29 3b 20 2f 2a 20  se( i==55 ); /* 
32dc7 42 45 54 57 45 45 4e 20 2a 2f 0a 20 20 20 20 20  BETWEEN */.     
32dc8 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 35 36   testcase( i==56
32dc9 20 29 3b 20 2f 2a 20 4e 4f 54 20 2a 2f 0a 20 20   ); /* NOT */.  
32dca 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32dcb 3d 35 37 20 29 3b 20 2f 2a 20 4e 4f 54 4e 55 4c  =57 ); /* NOTNUL
32dcc 4c 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  L */.      testc
32dcd 61 73 65 28 20 69 3d 3d 35 38 20 29 3b 20 2f 2a  ase( i==58 ); /*
32dce 20 4e 4f 20 2a 2f 0a 20 20 20 20 20 20 74 65 73   NO */.      tes
32dcf 74 63 61 73 65 28 20 69 3d 3d 35 39 20 29 3b 20  tcase( i==59 ); 
32dd0 2f 2a 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20 20  /* NULL */.     
32dd1 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 30   testcase( i==60
32dd2 20 29 3b 20 2f 2a 20 4c 49 4b 45 20 2a 2f 0a 20   ); /* LIKE */. 
32dd3 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32dd4 3d 3d 36 31 20 29 3b 20 2f 2a 20 43 41 53 43 41  ==61 ); /* CASCA
32dd5 44 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  DE */.      test
32dd6 63 61 73 65 28 20 69 3d 3d 36 32 20 29 3b 20 2f  case( i==62 ); /
32dd7 2a 20 41 53 43 20 2a 2f 0a 20 20 20 20 20 20 74  * ASC */.      t
32dd8 65 73 74 63 61 73 65 28 20 69 3d 3d 36 33 20 29  estcase( i==63 )
32dd9 3b 20 2f 2a 20 44 45 4c 45 54 45 20 2a 2f 0a 20  ; /* DELETE */. 
32dda 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32ddb 3d 3d 36 34 20 29 3b 20 2f 2a 20 43 41 53 45 20  ==64 ); /* CASE 
32ddc 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  */.      testcas
32ddd 65 28 20 69 3d 3d 36 35 20 29 3b 20 2f 2a 20 43  e( i==65 ); /* C
32dde 4f 4c 4c 41 54 45 20 2a 2f 0a 20 20 20 20 20 20  OLLATE */.      
32ddf 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36 36 20  testcase( i==66 
32de0 29 3b 20 2f 2a 20 43 52 45 41 54 45 20 2a 2f 0a  ); /* CREATE */.
32de1 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32de2 69 3d 3d 36 37 20 29 3b 20 2f 2a 20 43 55 52 52  i==67 ); /* CURR
32de3 45 4e 54 5f 44 41 54 45 20 2a 2f 0a 20 20 20 20  ENT_DATE */.    
32de4 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 36    testcase( i==6
32de5 38 20 29 3b 20 2f 2a 20 44 45 54 41 43 48 20 2a  8 ); /* DETACH *
32de6 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32de7 28 20 69 3d 3d 36 39 20 29 3b 20 2f 2a 20 49 4d  ( i==69 ); /* IM
32de8 4d 45 44 49 41 54 45 20 2a 2f 0a 20 20 20 20 20  MEDIATE */.     
32de9 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 30   testcase( i==70
32dea 20 29 3b 20 2f 2a 20 4a 4f 49 4e 20 2a 2f 0a 20   ); /* JOIN */. 
32deb 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69       testcase( i
32dec 3d 3d 37 31 20 29 3b 20 2f 2a 20 49 4e 53 45 52  ==71 ); /* INSER
32ded 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  T */.      testc
32dee 61 73 65 28 20 69 3d 3d 37 32 20 29 3b 20 2f 2a  ase( i==72 ); /*
32def 20 4d 41 54 43 48 20 2a 2f 0a 20 20 20 20 20 20   MATCH */.      
32df0 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 33 20  testcase( i==73 
32df1 29 3b 20 2f 2a 20 50 4c 41 4e 20 2a 2f 0a 20 20  ); /* PLAN */.  
32df2 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
32df3 3d 37 34 20 29 3b 20 2f 2a 20 41 4e 41 4c 59 5a  =74 ); /* ANALYZ
32df4 45 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  E */.      testc
32df5 61 73 65 28 20 69 3d 3d 37 35 20 29 3b 20 2f 2a  ase( i==75 ); /*
32df6 20 50 52 41 47 4d 41 20 2a 2f 0a 20 20 20 20 20   PRAGMA */.     
32df7 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 37 36   testcase( i==76
32df8 20 29 3b 20 2f 2a 20 41 42 4f 52 54 20 2a 2f 0a   ); /* ABORT */.
32df9 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32dfa 69 3d 3d 37 37 20 29 3b 20 2f 2a 20 56 41 4c 55  i==77 ); /* VALU
32dfb 45 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  ES */.      test
32dfc 63 61 73 65 28 20 69 3d 3d 37 38 20 29 3b 20 2f  case( i==78 ); /
32dfd 2a 20 56 49 52 54 55 41 4c 20 2a 2f 0a 20 20 20  * VIRTUAL */.   
32dfe 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32dff 37 39 20 29 3b 20 2f 2a 20 4c 49 4d 49 54 20 2a  79 ); /* LIMIT *
32e00 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32e01 28 20 69 3d 3d 38 30 20 29 3b 20 2f 2a 20 57 48  ( i==80 ); /* WH
32e02 45 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  EN */.      test
32e03 63 61 73 65 28 20 69 3d 3d 38 31 20 29 3b 20 2f  case( i==81 ); /
32e04 2a 20 57 48 45 52 45 20 2a 2f 0a 20 20 20 20 20  * WHERE */.     
32e05 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 38 32   testcase( i==82
32e06 20 29 3b 20 2f 2a 20 52 45 4e 41 4d 45 20 2a 2f   ); /* RENAME */
32e07 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32e08 20 69 3d 3d 38 33 20 29 3b 20 2f 2a 20 41 46 54   i==83 ); /* AFT
32e09 45 52 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  ER */.      test
32e0a 63 61 73 65 28 20 69 3d 3d 38 34 20 29 3b 20 2f  case( i==84 ); /
32e0b 2a 20 52 45 50 4c 41 43 45 20 2a 2f 0a 20 20 20  * REPLACE */.   
32e0c 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32e0d 38 35 20 29 3b 20 2f 2a 20 41 4e 44 20 2a 2f 0a  85 ); /* AND */.
32e0e 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32e0f 69 3d 3d 38 36 20 29 3b 20 2f 2a 20 44 45 46 41  i==86 ); /* DEFA
32e10 55 4c 54 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  ULT */.      tes
32e11 74 63 61 73 65 28 20 69 3d 3d 38 37 20 29 3b 20  tcase( i==87 ); 
32e12 2f 2a 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54  /* AUTOINCREMENT
32e13 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61   */.      testca
32e14 73 65 28 20 69 3d 3d 38 38 20 29 3b 20 2f 2a 20  se( i==88 ); /* 
32e15 54 4f 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  TO */.      test
32e16 63 61 73 65 28 20 69 3d 3d 38 39 20 29 3b 20 2f  case( i==89 ); /
32e17 2a 20 49 4e 20 2a 2f 0a 20 20 20 20 20 20 74 65  * IN */.      te
32e18 73 74 63 61 73 65 28 20 69 3d 3d 39 30 20 29 3b  stcase( i==90 );
32e19 20 2f 2a 20 43 41 53 54 20 2a 2f 0a 20 20 20 20   /* CAST */.    
32e1a 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 39    testcase( i==9
32e1b 31 20 29 3b 20 2f 2a 20 43 4f 4c 55 4d 4e 20 2a  1 ); /* COLUMN *
32e1c 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32e1d 28 20 69 3d 3d 39 32 20 29 3b 20 2f 2a 20 43 4f  ( i==92 ); /* CO
32e1e 4d 4d 49 54 20 2a 2f 0a 20 20 20 20 20 20 74 65  MMIT */.      te
32e1f 73 74 63 61 73 65 28 20 69 3d 3d 39 33 20 29 3b  stcase( i==93 );
32e20 20 2f 2a 20 43 4f 4e 46 4c 49 43 54 20 2a 2f 0a   /* CONFLICT */.
32e21 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32e22 69 3d 3d 39 34 20 29 3b 20 2f 2a 20 43 52 4f 53  i==94 ); /* CROS
32e23 53 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  S */.      testc
32e24 61 73 65 28 20 69 3d 3d 39 35 20 29 3b 20 2f 2a  ase( i==95 ); /*
32e25 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 53 54 41   CURRENT_TIMESTA
32e26 4d 50 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  MP */.      test
32e27 63 61 73 65 28 20 69 3d 3d 39 36 20 29 3b 20 2f  case( i==96 ); /
32e28 2a 20 43 55 52 52 45 4e 54 5f 54 49 4d 45 20 2a  * CURRENT_TIME *
32e29 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32e2a 28 20 69 3d 3d 39 37 20 29 3b 20 2f 2a 20 50 52  ( i==97 ); /* PR
32e2b 49 4d 41 52 59 20 2a 2f 0a 20 20 20 20 20 20 74  IMARY */.      t
32e2c 65 73 74 63 61 73 65 28 20 69 3d 3d 39 38 20 29  estcase( i==98 )
32e2d 3b 20 2f 2a 20 44 45 46 45 52 52 45 44 20 2a 2f  ; /* DEFERRED */
32e2e 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32e2f 20 69 3d 3d 39 39 20 29 3b 20 2f 2a 20 44 49 53   i==99 ); /* DIS
32e30 54 49 4e 43 54 20 2a 2f 0a 20 20 20 20 20 20 74  TINCT */.      t
32e31 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30 30 20  estcase( i==100 
32e32 29 3b 20 2f 2a 20 49 53 20 2a 2f 0a 20 20 20 20  ); /* IS */.    
32e33 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
32e34 30 31 20 29 3b 20 2f 2a 20 44 52 4f 50 20 2a 2f  01 ); /* DROP */
32e35 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32e36 20 69 3d 3d 31 30 32 20 29 3b 20 2f 2a 20 46 41   i==102 ); /* FA
32e37 49 4c 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  IL */.      test
32e38 63 61 73 65 28 20 69 3d 3d 31 30 33 20 29 3b 20  case( i==103 ); 
32e39 2f 2a 20 46 52 4f 4d 20 2a 2f 0a 20 20 20 20 20  /* FROM */.     
32e3a 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30   testcase( i==10
32e3b 34 20 29 3b 20 2f 2a 20 46 55 4c 4c 20 2a 2f 0a  4 ); /* FULL */.
32e3c 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32e3d 69 3d 3d 31 30 35 20 29 3b 20 2f 2a 20 47 4c 4f  i==105 ); /* GLO
32e3e 42 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74 63  B */.      testc
32e3f 61 73 65 28 20 69 3d 3d 31 30 36 20 29 3b 20 2f  ase( i==106 ); /
32e40 2a 20 42 59 20 2a 2f 0a 20 20 20 20 20 20 74 65  * BY */.      te
32e41 73 74 63 61 73 65 28 20 69 3d 3d 31 30 37 20 29  stcase( i==107 )
32e42 3b 20 2f 2a 20 49 46 20 2a 2f 0a 20 20 20 20 20  ; /* IF */.     
32e43 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 30   testcase( i==10
32e44 38 20 29 3b 20 2f 2a 20 49 53 4e 55 4c 4c 20 2a  8 ); /* ISNULL *
32e45 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32e46 28 20 69 3d 3d 31 30 39 20 29 3b 20 2f 2a 20 4f  ( i==109 ); /* O
32e47 52 44 45 52 20 2a 2f 0a 20 20 20 20 20 20 74 65  RDER */.      te
32e48 73 74 63 61 73 65 28 20 69 3d 3d 31 31 30 20 29  stcase( i==110 )
32e49 3b 20 2f 2a 20 52 45 53 54 52 49 43 54 20 2a 2f  ; /* RESTRICT */
32e4a 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32e4b 20 69 3d 3d 31 31 31 20 29 3b 20 2f 2a 20 4f 55   i==111 ); /* OU
32e4c 54 45 52 20 2a 2f 0a 20 20 20 20 20 20 74 65 73  TER */.      tes
32e4d 74 63 61 73 65 28 20 69 3d 3d 31 31 32 20 29 3b  tcase( i==112 );
32e4e 20 2f 2a 20 52 49 47 48 54 20 2a 2f 0a 20 20 20   /* RIGHT */.   
32e4f 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32e50 31 31 33 20 29 3b 20 2f 2a 20 52 4f 4c 4c 42 41  113 ); /* ROLLBA
32e51 43 4b 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  CK */.      test
32e52 63 61 73 65 28 20 69 3d 3d 31 31 34 20 29 3b 20  case( i==114 ); 
32e53 2f 2a 20 52 4f 57 20 2a 2f 0a 20 20 20 20 20 20  /* ROW */.      
32e54 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31 31 35  testcase( i==115
32e55 20 29 3b 20 2f 2a 20 55 4e 49 4f 4e 20 2a 2f 0a   ); /* UNION */.
32e56 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32e57 69 3d 3d 31 31 36 20 29 3b 20 2f 2a 20 55 53 49  i==116 ); /* USI
32e58 4e 47 20 2a 2f 0a 20 20 20 20 20 20 74 65 73 74  NG */.      test
32e59 63 61 73 65 28 20 69 3d 3d 31 31 37 20 29 3b 20  case( i==117 ); 
32e5a 2f 2a 20 56 41 43 55 55 4d 20 2a 2f 0a 20 20 20  /* VACUUM */.   
32e5b 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d     testcase( i==
32e5c 31 31 38 20 29 3b 20 2f 2a 20 56 49 45 57 20 2a  118 ); /* VIEW *
32e5d 2f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  /.      testcase
32e5e 28 20 69 3d 3d 31 31 39 20 29 3b 20 2f 2a 20 49  ( i==119 ); /* I
32e5f 4e 49 54 49 41 4c 4c 59 20 2a 2f 0a 20 20 20 20  NITIALLY */.    
32e60 20 20 74 65 73 74 63 61 73 65 28 20 69 3d 3d 31    testcase( i==1
32e61 32 30 20 29 3b 20 2f 2a 20 41 4c 4c 20 2a 2f 0a  20 ); /* ALL */.
32e62 20 20 20 20 20 20 72 65 74 75 72 6e 20 61 43 6f        return aCo
32e63 64 65 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d  de[i];.    }.  }
32e64 0a 20 20 72 65 74 75 72 6e 20 54 4b 5f 49 44 3b  .  return TK_ID;
32e65 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
32e66 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4b 65 79  E int sqlite3Key
32e67 77 6f 72 64 43 6f 64 65 28 63 6f 6e 73 74 20 75  wordCode(const u
32e68 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 2c  nsigned char *z,
32e69 20 69 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75 72   int n){.  retur
32e6a 6e 20 6b 65 79 77 6f 72 64 43 6f 64 65 28 28 63  n keywordCode((c
32e6b 68 61 72 2a 29 7a 2c 20 6e 29 3b 0a 7d 0a 0a 2f  har*)z, n);.}../
32e6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
32e6d 6e 64 20 6f 66 20 6b 65 79 77 6f 72 64 68 61 73  nd of keywordhas
32e6e 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h.h ************
32e6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
32e71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
32e72 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
32e73 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 74  we left off in t
32e74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a  okenize.c ******
32e75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
32e76 0a 2f 2a 0a 2a 2a 20 49 66 20 58 20 69 73 20 61  ./*.** If X is a
32e77 20 63 68 61 72 61 63 74 65 72 20 74 68 61 74 20   character that 
32e78 63 61 6e 20 62 65 20 75 73 65 64 20 69 6e 20 61  can be used in a
32e79 6e 20 69 64 65 6e 74 69 66 69 65 72 20 74 68 65  n identifier the
32e7a 6e 0a 2a 2a 20 49 64 43 68 61 72 28 58 29 20 77  n.** IdChar(X) w
32e7b 69 6c 6c 20 62 65 20 74 72 75 65 2e 20 20 4f 74  ill be true.  Ot
32e7c 68 65 72 77 69 73 65 20 69 74 20 69 73 20 66 61  herwise it is fa
32e7d 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 41  lse..**.** For A
32e7e 53 43 49 49 2c 20 61 6e 79 20 63 68 61 72 61 63  SCII, any charac
32e7f 74 65 72 20 77 69 74 68 20 74 68 65 20 68 69 67  ter with the hig
32e80 68 2d 6f 72 64 65 72 20 62 69 74 20 73 65 74 20  h-order bit set 
32e81 69 73 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20 69 6e  is.** allowed in
32e82 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 2e 20   an identifier. 
32e83 20 46 6f 72 20 37 2d 62 69 74 20 63 68 61 72 61   For 7-bit chara
32e84 63 74 65 72 73 2c 20 0a 2a 2a 20 73 71 6c 69 74  cters, .** sqlit
32e85 65 33 49 73 49 64 43 68 61 72 5b 58 5d 20 6d 75  e3IsIdChar[X] mu
32e86 73 74 20 62 65 20 31 2e 0a 2a 2a 0a 2a 2a 20 46  st be 1..**.** F
32e87 6f 72 20 45 42 43 44 49 43 2c 20 74 68 65 20 72  or EBCDIC, the r
32e88 75 6c 65 73 20 61 72 65 20 6d 6f 72 65 20 63 6f  ules are more co
32e89 6d 70 6c 65 78 20 62 75 74 20 68 61 76 65 20 74  mplex but have t
32e8a 68 65 20 73 61 6d 65 0a 2a 2a 20 65 6e 64 20 72  he same.** end r
32e8b 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 54 69 63  esult..**.** Tic
32e8c 6b 65 74 20 23 31 30 36 36 2e 20 20 74 68 65 20  ket #1066.  the 
32e8d 53 51 4c 20 73 74 61 6e 64 61 72 64 20 64 6f 65  SQL standard doe
32e8e 73 20 6e 6f 74 20 61 6c 6c 6f 77 20 27 24 27 20  s not allow '$' 
32e8f 69 6e 20 74 68 65 0a 2a 2a 20 6d 69 64 64 6c 65  in the.** middle
32e90 20 6f 66 20 69 64 65 6e 74 66 69 65 72 73 2e 20   of identfiers. 
32e91 20 42 75 74 20 6d 61 6e 79 20 53 51 4c 20 69 6d   But many SQL im
32e92 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 64 6f  plementations do
32e93 2e 20 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c  . .** SQLite wil
32e94 6c 20 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20 69  l allow '$' in i
32e95 64 65 6e 74 69 66 69 65 72 73 20 66 6f 72 20 63  dentifiers for c
32e96 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a  ompatibility..**
32e97 20 42 75 74 20 74 68 65 20 66 65 61 74 75 72 65   But the feature
32e98 20 69 73 20 75 6e 64 6f 63 75 6d 65 6e 74 65 64   is undocumented
32e99 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
32e9a 54 45 5f 41 53 43 49 49 0a 53 51 4c 49 54 45 5f  TE_ASCII.SQLITE_
32e9b 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
32e9c 61 72 20 73 71 6c 69 74 65 33 49 73 41 73 63 69  ar sqlite3IsAsci
32e9d 69 49 64 43 68 61 72 5b 5d 20 3d 20 7b 0a 2f 2a  iIdChar[] = {./*
32e9e 20 78 30 20 78 31 20 78 32 20 78 33 20 78 34 20   x0 x1 x2 x3 x4 
32e9f 78 35 20 78 36 20 78 37 20 78 38 20 78 39 20 78  x5 x6 x7 x8 x9 x
32ea0 41 20 78 42 20 78 43 20 78 44 20 78 45 20 78 46  A xB xC xD xE xF
32ea1 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c   */.    0, 0, 0,
32ea2 20 30 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 1, 0, 0, 0, 
32ea3 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
32ea4 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 32 78 20 2a  , 0, 0,  /* 2x *
32ea5 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31  /.    1, 1, 1, 1
32ea6 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
32ea7 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 0, 0, 0, 0, 
32ea8 30 2c 20 30 2c 20 20 2f 2a 20 33 78 20 2a 2f 0a  0, 0,  /* 3x */.
32ea9 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20      0, 1, 1, 1, 
32eaa 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
32eab 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
32eac 20 31 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20   1,  /* 4x */.  
32ead 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    1, 1, 1, 1, 1,
32eae 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32eaf 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31  1, 0, 0, 0, 0, 1
32eb0 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20  ,  /* 5x */.    
32eb1 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
32eb2 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
32eb3 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32eb4 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20 31 2c   /* 6x */.    1,
32eb5 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32eb6 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  1, 1, 1, 1, 1, 0
32eb7 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
32eb8 2a 20 37 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69  * 7x */.};.#defi
32eb9 6e 65 20 49 64 43 68 61 72 28 43 29 20 20 28 28  ne IdChar(C)  ((
32eba 28 63 3d 43 29 26 30 78 38 30 29 21 3d 30 20 7c  (c=C)&0x80)!=0 |
32ebb 7c 20 28 63 3e 30 78 31 66 20 26 26 20 73 71 6c  | (c>0x1f && sql
32ebc 69 74 65 33 49 73 41 73 63 69 69 49 64 43 68 61  ite3IsAsciiIdCha
32ebd 72 5b 63 2d 30 78 32 30 5d 29 29 0a 23 65 6e 64  r[c-0x20])).#end
32ebe 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  if.#ifdef SQLITE
32ebf 5f 45 42 43 44 49 43 0a 53 51 4c 49 54 45 5f 50  _EBCDIC.SQLITE_P
32ec0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
32ec1 72 20 73 71 6c 69 74 65 33 49 73 45 62 63 64 69  r sqlite3IsEbcdi
32ec2 63 49 64 43 68 61 72 5b 5d 20 3d 20 7b 0a 2f 2a  cIdChar[] = {./*
32ec3 20 78 30 20 78 31 20 78 32 20 78 33 20 78 34 20   x0 x1 x2 x3 x4 
32ec4 78 35 20 78 36 20 78 37 20 78 38 20 78 39 20 78  x5 x6 x7 x8 x9 x
32ec5 41 20 78 42 20 78 43 20 78 44 20 78 45 20 78 46  A xB xC xD xE xF
32ec6 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c   */.    0, 0, 1,
32ec7 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32ec8 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 1, 0, 0, 0, 0
32ec9 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 34 78 20 2a  , 0, 0,  /* 4x *
32eca 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31  /.    0, 1, 1, 1
32ecb 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
32ecc 20 31 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c 20   1, 0, 1, 0, 0, 
32ecd 30 2c 20 30 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a  0, 0,  /* 5x */.
32ece 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20      0, 0, 1, 1, 
32ecf 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
32ed0 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c  , 0, 0, 0, 1, 0,
32ed1 20 30 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20   0,  /* 6x */.  
32ed2 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
32ed3 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
32ed4 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
32ed5 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 20  ,  /* 7x */.    
32ed6 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
32ed7 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
32ed8 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   0, 1, 1, 1, 0, 
32ed9 20 2f 2a 20 38 78 20 2a 2f 0a 20 20 20 20 30 2c   /* 8x */.    0,
32eda 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32edb 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30  1, 1, 1, 1, 0, 0
32edc 2c 20 31 2c 20 30 2c 20 31 2c 20 30 2c 20 20 2f  , 1, 0, 1, 0,  /
32edd 2a 20 39 78 20 2a 2f 0a 20 20 20 20 31 2c 20 30  * 9x */.    1, 0
32ede 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
32edf 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   1, 1, 1, 1, 0, 
32ee0 31 2c 20 31 2c 20 31 2c 20 30 2c 20 20 2f 2a 20  1, 1, 1, 0,  /* 
32ee1 41 78 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20  Ax */.    0, 0, 
32ee2 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
32ee3 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
32ee4 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 42 78   0, 0, 0,  /* Bx
32ee5 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
32ee6 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32ee7 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31  1, 1, 0, 1, 1, 1
32ee8 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 43 78 20 2a  , 1, 1,  /* Cx *
32ee9 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31  /.    0, 1, 1, 1
32eea 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
32eeb 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 0, 1, 1, 1, 
32eec 31 2c 20 31 2c 20 20 2f 2a 20 44 78 20 2a 2f 0a  1, 1,  /* Dx */.
32eed 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20      0, 0, 1, 1, 
32eee 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
32eef 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 0, 1, 1, 1, 1,
32ef0 20 31 2c 20 20 2f 2a 20 45 78 20 2a 2f 0a 20 20   1,  /* Ex */.  
32ef1 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    1, 1, 1, 1, 1,
32ef2 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
32ef3 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30  0, 1, 1, 1, 1, 0
32ef4 2c 20 20 2f 2a 20 46 78 20 2a 2f 0a 7d 3b 0a 23  ,  /* Fx */.};.#
32ef5 64 65 66 69 6e 65 20 49 64 43 68 61 72 28 43 29  define IdChar(C)
32ef6 20 20 28 28 28 63 3d 43 29 3e 3d 30 78 34 32 20    (((c=C)>=0x42 
32ef7 26 26 20 73 71 6c 69 74 65 33 49 73 45 62 63 64  && sqlite3IsEbcd
32ef8 69 63 49 64 43 68 61 72 5b 63 2d 30 78 34 30 5d  icIdChar[c-0x40]
32ef9 29 29 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  )).#endif.../*.*
32efa 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6c 65 6e  * Return the len
32efb 67 74 68 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  gth of the token
32efc 20 74 68 61 74 20 62 65 67 69 6e 73 20 61 74 20   that begins at 
32efd 7a 5b 30 5d 2e 20 0a 2a 2a 20 53 74 6f 72 65 20  z[0]. .** Store 
32efe 74 68 65 20 74 6f 6b 65 6e 20 74 79 70 65 20 69  the token type i
32eff 6e 20 2a 74 6f 6b 65 6e 54 79 70 65 20 62 65 66  n *tokenType bef
32f00 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a  ore returning..*
32f01 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
32f02 20 69 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54   int sqlite3GetT
32f03 6f 6b 65 6e 28 63 6f 6e 73 74 20 75 6e 73 69 67  oken(const unsig
32f04 6e 65 64 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  ned char *z, int
32f05 20 2a 74 6f 6b 65 6e 54 79 70 65 29 7b 0a 20 20   *tokenType){.  
32f06 69 6e 74 20 69 2c 20 63 3b 0a 20 20 73 77 69 74  int i, c;.  swit
32f07 63 68 28 20 2a 7a 20 29 7b 0a 20 20 20 20 63 61  ch( *z ){.    ca
32f08 73 65 20 27 20 27 3a 20 63 61 73 65 20 27 5c 74  se ' ': case '\t
32f09 27 3a 20 63 61 73 65 20 27 5c 6e 27 3a 20 63 61  ': case '\n': ca
32f0a 73 65 20 27 5c 66 27 3a 20 63 61 73 65 20 27 5c  se '\f': case '\
32f0b 72 27 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  r': {.      test
32f0c 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 20 27 20  case( z[0]==' ' 
32f0d 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
32f0e 65 28 20 7a 5b 30 5d 3d 3d 27 5c 74 27 20 29 3b  e( z[0]=='\t' );
32f0f 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32f10 20 7a 5b 30 5d 3d 3d 27 5c 6e 27 20 29 3b 0a 20   z[0]=='\n' );. 
32f11 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 7a       testcase( z
32f12 5b 30 5d 3d 3d 27 5c 66 27 20 29 3b 0a 20 20 20  [0]=='\f' );.   
32f13 20 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30     testcase( z[0
32f14 5d 3d 3d 27 5c 72 27 20 29 3b 0a 20 20 20 20 20  ]=='\r' );.     
32f15 20 66 6f 72 28 69 3d 31 3b 20 73 71 6c 69 74 65   for(i=1; sqlite
32f16 33 49 73 73 70 61 63 65 28 7a 5b 69 5d 29 3b 20  3Isspace(z[i]); 
32f17 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74 6f  i++){}.      *to
32f18 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 50 41  kenType = TK_SPA
32f19 43 45 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  CE;.      return
32f1a 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   i;.    }.    ca
32f1b 73 65 20 27 2d 27 3a 20 7b 0a 20 20 20 20 20 20  se '-': {.      
32f1c 69 66 28 20 7a 5b 31 5d 3d 3d 27 2d 27 20 29 7b  if( z[1]=='-' ){
32f1d 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 32  .        for(i=2
32f1e 3b 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 20 26 26  ; (c=z[i])!=0 &&
32f1f 20 63 21 3d 27 5c 6e 27 3b 20 69 2b 2b 29 7b 7d   c!='\n'; i++){}
32f20 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
32f21 79 70 65 20 3d 20 54 4b 5f 53 50 41 43 45 3b 0a  ype = TK_SPACE;.
32f22 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69          return i
32f23 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
32f24 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
32f25 4d 49 4e 55 53 3b 0a 20 20 20 20 20 20 72 65 74  MINUS;.      ret
32f26 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
32f27 20 63 61 73 65 20 27 28 27 3a 20 7b 0a 20 20 20   case '(': {.   
32f28 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
32f29 54 4b 5f 4c 50 3b 0a 20 20 20 20 20 20 72 65 74  TK_LP;.      ret
32f2a 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
32f2b 20 63 61 73 65 20 27 29 27 3a 20 7b 0a 20 20 20   case ')': {.   
32f2c 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
32f2d 54 4b 5f 52 50 3b 0a 20 20 20 20 20 20 72 65 74  TK_RP;.      ret
32f2e 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
32f2f 20 63 61 73 65 20 27 3b 27 3a 20 7b 0a 20 20 20   case ';': {.   
32f30 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
32f31 54 4b 5f 53 45 4d 49 3b 0a 20 20 20 20 20 20 72  TK_SEMI;.      r
32f32 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
32f33 20 20 20 63 61 73 65 20 27 2b 27 3a 20 7b 0a 20     case '+': {. 
32f34 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
32f35 3d 20 54 4b 5f 50 4c 55 53 3b 0a 20 20 20 20 20  = TK_PLUS;.     
32f36 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
32f37 0a 20 20 20 20 63 61 73 65 20 27 2a 27 3a 20 7b  .    case '*': {
32f38 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
32f39 65 20 3d 20 54 4b 5f 53 54 41 52 3b 0a 20 20 20  e = TK_STAR;.   
32f3a 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
32f3b 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2f 27 3a   }.    case '/':
32f3c 20 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31   {.      if( z[1
32f3d 5d 21 3d 27 2a 27 20 7c 7c 20 7a 5b 32 5d 3d 3d  ]!='*' || z[2]==
32f3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f  0 ){.        *to
32f3f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53 4c 41  kenType = TK_SLA
32f40 53 48 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  SH;.        retu
32f41 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 1;.      }.  
32f42 20 20 20 20 66 6f 72 28 69 3d 33 2c 20 63 3d 7a      for(i=3, c=z
32f43 5b 32 5d 3b 20 28 63 21 3d 27 2a 27 20 7c 7c 20  [2]; (c!='*' || 
32f44 7a 5b 69 5d 21 3d 27 2f 27 29 20 26 26 20 28 63  z[i]!='/') && (c
32f45 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b  =z[i])!=0; i++){
32f46 7d 0a 20 20 20 20 20 20 69 66 28 20 63 20 29 20  }.      if( c ) 
32f47 69 2b 2b 3b 0a 20 20 20 20 20 20 2a 74 6f 6b 65  i++;.      *toke
32f48 6e 54 79 70 65 20 3d 20 54 4b 5f 53 50 41 43 45  nType = TK_SPACE
32f49 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  ;.      return i
32f4a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
32f4b 20 27 25 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74   '%': {.      *t
32f4c 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 52 45  okenType = TK_RE
32f4d 4d 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  M;.      return 
32f4e 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  1;.    }.    cas
32f4f 65 20 27 3d 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e '=': {.      *
32f50 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 45  tokenType = TK_E
32f51 51 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  Q;.      return 
32f52 31 20 2b 20 28 7a 5b 31 5d 3d 3d 27 3d 27 29 3b  1 + (z[1]=='=');
32f53 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
32f54 27 3c 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28  '<': {.      if(
32f55 20 28 63 3d 7a 5b 31 5d 29 3d 3d 27 3d 27 20 29   (c=z[1])=='=' )
32f56 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
32f57 54 79 70 65 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20  Type = TK_LE;.  
32f58 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a        return 2;.
32f59 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
32f5a 63 3d 3d 27 3e 27 20 29 7b 0a 20 20 20 20 20 20  c=='>' ){.      
32f5b 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
32f5c 4b 5f 4e 45 3b 0a 20 20 20 20 20 20 20 20 72 65  K_NE;.        re
32f5d 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65  turn 2;.      }e
32f5e 6c 73 65 20 69 66 28 20 63 3d 3d 27 3c 27 20 29  lse if( c=='<' )
32f5f 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
32f60 54 79 70 65 20 3d 20 54 4b 5f 4c 53 48 49 46 54  Type = TK_LSHIFT
32f61 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
32f62 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   2;.      }else{
32f63 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  .        *tokenT
32f64 79 70 65 20 3d 20 54 4b 5f 4c 54 3b 0a 20 20 20  ype = TK_LT;.   
32f65 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
32f66 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
32f67 20 63 61 73 65 20 27 3e 27 3a 20 7b 0a 20 20 20   case '>': {.   
32f68 20 20 20 69 66 28 20 28 63 3d 7a 5b 31 5d 29 3d     if( (c=z[1])=
32f69 3d 27 3d 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='=' ){.        
32f6a 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
32f6b 47 45 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  GE;.        retu
32f6c 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73  rn 2;.      }els
32f6d 65 20 69 66 28 20 63 3d 3d 27 3e 27 20 29 7b 0a  e if( c=='>' ){.
32f6e 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
32f6f 70 65 20 3d 20 54 4b 5f 52 53 48 49 46 54 3b 0a  pe = TK_RSHIFT;.
32f70 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 32          return 2
32f71 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
32f72 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
32f73 65 20 3d 20 54 4b 5f 47 54 3b 0a 20 20 20 20 20  e = TK_GT;.     
32f74 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
32f75 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63     }.    }.    c
32f76 61 73 65 20 27 21 27 3a 20 7b 0a 20 20 20 20 20  ase '!': {.     
32f77 20 69 66 28 20 7a 5b 31 5d 21 3d 27 3d 27 20 29   if( z[1]!='=' )
32f78 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
32f79 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41  Type = TK_ILLEGA
32f7a 4c 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  L;.        retur
32f7b 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  n 2;.      }else
32f7c 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
32f7d 54 79 70 65 20 3d 20 54 4b 5f 4e 45 3b 0a 20 20  Type = TK_NE;.  
32f7e 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a        return 2;.
32f7f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
32f80 20 20 63 61 73 65 20 27 7c 27 3a 20 7b 0a 20 20    case '|': {.  
32f81 20 20 20 20 69 66 28 20 7a 5b 31 5d 21 3d 27 7c      if( z[1]!='|
32f82 27 20 29 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f  ' ){.        *to
32f83 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42 49 54  kenType = TK_BIT
32f84 4f 52 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  OR;.        retu
32f85 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73  rn 1;.      }els
32f86 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65  e{.        *toke
32f87 6e 54 79 70 65 20 3d 20 54 4b 5f 43 4f 4e 43 41  nType = TK_CONCA
32f88 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  T;.        retur
32f89 6e 20 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 2;.      }.   
32f8a 20 7d 0a 20 20 20 20 63 61 73 65 20 27 2c 27 3a   }.    case ',':
32f8b 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54   {.      *tokenT
32f8c 79 70 65 20 3d 20 54 4b 5f 43 4f 4d 4d 41 3b 0a  ype = TK_COMMA;.
32f8d 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
32f8e 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
32f8f 26 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b  &': {.      *tok
32f90 65 6e 54 79 70 65 20 3d 20 54 4b 5f 42 49 54 41  enType = TK_BITA
32f91 4e 44 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ND;.      return
32f92 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61   1;.    }.    ca
32f93 73 65 20 27 7e 27 3a 20 7b 0a 20 20 20 20 20 20  se '~': {.      
32f94 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
32f95 42 49 54 4e 4f 54 3b 0a 20 20 20 20 20 20 72 65  BITNOT;.      re
32f96 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
32f97 20 20 63 61 73 65 20 27 60 27 3a 0a 20 20 20 20    case '`':.    
32f98 63 61 73 65 20 27 5c 27 27 3a 0a 20 20 20 20 63  case '\'':.    c
32f99 61 73 65 20 27 22 27 3a 20 7b 0a 20 20 20 20 20  ase '"': {.     
32f9a 20 69 6e 74 20 64 65 6c 69 6d 20 3d 20 7a 5b 30   int delim = z[0
32f9b 5d 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  ];.      testcas
32f9c 65 28 20 64 65 6c 69 6d 3d 3d 27 60 27 20 29 3b  e( delim=='`' );
32f9d 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
32f9e 20 64 65 6c 69 6d 3d 3d 27 5c 27 27 20 29 3b 0a   delim=='\'' );.
32f9f 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32fa0 64 65 6c 69 6d 3d 3d 27 22 27 20 29 3b 0a 20 20  delim=='"' );.  
32fa1 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 28 63 3d      for(i=1; (c=
32fa2 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a  z[i])!=0; i++){.
32fa3 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d 64          if( c==d
32fa4 65 6c 69 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  elim ){.        
32fa5 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 64 65    if( z[i+1]==de
32fa6 6c 69 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20  lim ){.         
32fa7 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20     i++;.        
32fa8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
32fa9 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
32faa 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
32fab 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
32fac 69 66 28 20 63 3d 3d 27 5c 27 27 20 29 7b 0a 20  if( c=='\'' ){. 
32fad 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
32fae 65 20 3d 20 54 4b 5f 53 54 52 49 4e 47 3b 0a 20  e = TK_STRING;. 
32faf 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 2b         return i+
32fb0 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  1;.      }else i
32fb1 66 28 20 63 21 3d 30 20 29 7b 0a 20 20 20 20 20  f( c!=0 ){.     
32fb2 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
32fb3 54 4b 5f 49 44 3b 0a 20 20 20 20 20 20 20 20 72  TK_ID;.        r
32fb4 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 20  eturn i+1;.     
32fb5 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
32fb6 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
32fb7 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20  ILLEGAL;.       
32fb8 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 20   return i;.     
32fb9 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73   }.    }.    cas
32fba 65 20 27 2e 27 3a 20 7b 0a 23 69 66 6e 64 65 66  e '.': {.#ifndef
32fbb 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
32fbc 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20  ATING_POINT.    
32fbd 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 49 73    if( !sqlite3Is
32fbe 64 69 67 69 74 28 7a 5b 31 5d 29 20 29 0a 23 65  digit(z[1]) ).#e
32fbf 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20  ndif.      {.   
32fc0 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
32fc1 3d 20 54 4b 5f 44 4f 54 3b 0a 20 20 20 20 20 20  = TK_DOT;.      
32fc2 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
32fc3 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20    }.      /* If 
32fc4 74 68 65 20 6e 65 78 74 20 63 68 61 72 61 63 74  the next charact
32fc5 65 72 20 69 73 20 61 20 64 69 67 69 74 2c 20 74  er is a digit, t
32fc6 68 69 73 20 69 73 20 61 20 66 6c 6f 61 74 69 6e  his is a floatin
32fc7 67 20 70 6f 69 6e 74 0a 20 20 20 20 20 20 2a 2a  g point.      **
32fc8 20 6e 75 6d 62 65 72 20 74 68 61 74 20 62 65 67   number that beg
32fc9 69 6e 73 20 77 69 74 68 20 22 2e 22 2e 20 20 46  ins with ".".  F
32fca 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 74 68  all thru into th
32fcb 65 20 6e 65 78 74 20 63 61 73 65 20 2a 2f 0a 20  e next case */. 
32fcc 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 30     }.    case '0
32fcd 27 3a 20 63 61 73 65 20 27 31 27 3a 20 63 61 73  ': case '1': cas
32fce 65 20 27 32 27 3a 20 63 61 73 65 20 27 33 27 3a  e '2': case '3':
32fcf 20 63 61 73 65 20 27 34 27 3a 0a 20 20 20 20 63   case '4':.    c
32fd0 61 73 65 20 27 35 27 3a 20 63 61 73 65 20 27 36  ase '5': case '6
32fd1 27 3a 20 63 61 73 65 20 27 37 27 3a 20 63 61 73  ': case '7': cas
32fd2 65 20 27 38 27 3a 20 63 61 73 65 20 27 39 27 3a  e '8': case '9':
32fd3 20 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73   {.      testcas
32fd4 65 28 20 7a 5b 30 5d 3d 3d 27 30 27 20 29 3b 20  e( z[0]=='0' ); 
32fd5 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d   testcase( z[0]=
32fd6 3d 27 31 27 20 29 3b 20 20 74 65 73 74 63 61 73  ='1' );  testcas
32fd7 65 28 20 7a 5b 30 5d 3d 3d 27 32 27 20 29 3b 0a  e( z[0]=='2' );.
32fd8 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
32fd9 7a 5b 30 5d 3d 3d 27 33 27 20 29 3b 20 20 74 65  z[0]=='3' );  te
32fda 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 34  stcase( z[0]=='4
32fdb 27 20 29 3b 20 20 74 65 73 74 63 61 73 65 28 20  ' );  testcase( 
32fdc 7a 5b 30 5d 3d 3d 27 35 27 20 29 3b 0a 20 20 20  z[0]=='5' );.   
32fdd 20 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30     testcase( z[0
32fde 5d 3d 3d 27 36 27 20 29 3b 20 20 74 65 73 74 63  ]=='6' );  testc
32fdf 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 37 27 20 29  ase( z[0]=='7' )
32fe0 3b 20 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30  ;  testcase( z[0
32fe1 5d 3d 3d 27 38 27 20 29 3b 0a 20 20 20 20 20 20  ]=='8' );.      
32fe2 74 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d  testcase( z[0]==
32fe3 27 39 27 20 29 3b 0a 20 20 20 20 20 20 2a 74 6f  '9' );.      *to
32fe4 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4e 54  kenType = TK_INT
32fe5 45 47 45 52 3b 0a 20 20 20 20 20 20 66 6f 72 28  EGER;.      for(
32fe6 69 3d 30 3b 20 73 71 6c 69 74 65 33 49 73 64 69  i=0; sqlite3Isdi
32fe7 67 69 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b  git(z[i]); i++){
32fe8 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
32fe9 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
32fea 4f 49 4e 54 0a 20 20 20 20 20 20 69 66 28 20 7a  OINT.      if( z
32feb 5b 69 5d 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20  [i]=='.' ){.    
32fec 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20      i++;.       
32fed 20 77 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49   while( sqlite3I
32fee 73 64 69 67 69 74 28 7a 5b 69 5d 29 20 29 7b 20  sdigit(z[i]) ){ 
32fef 69 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 2a  i++; }.        *
32ff0 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 46  tokenType = TK_F
32ff1 4c 4f 41 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20  LOAT;.      }.  
32ff2 20 20 20 20 69 66 28 20 28 7a 5b 69 5d 3d 3d 27      if( (z[i]=='
32ff3 65 27 20 7c 7c 20 7a 5b 69 5d 3d 3d 27 45 27 29  e' || z[i]=='E')
32ff4 20 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 28   &&.           (
32ff5 20 73 71 6c 69 74 65 33 49 73 64 69 67 69 74 28   sqlite3Isdigit(
32ff6 7a 5b 69 2b 31 5d 29 20 0a 20 20 20 20 20 20 20  z[i+1]) .       
32ff7 20 20 20 20 20 7c 7c 20 28 28 7a 5b 69 2b 31 5d       || ((z[i+1]
32ff8 3d 3d 27 2b 27 20 7c 7c 20 7a 5b 69 2b 31 5d 3d  =='+' || z[i+1]=
32ff9 3d 27 2d 27 29 20 26 26 20 73 71 6c 69 74 65 33  ='-') && sqlite3
32ffa 49 73 64 69 67 69 74 28 7a 5b 69 2b 32 5d 29 29  Isdigit(z[i+2]))
32ffb 0a 20 20 20 20 20 20 20 20 20 20 20 29 0a 20 20  .           ).  
32ffc 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 69      ){.        i
32ffd 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 77   += 2;.        w
32ffe 68 69 6c 65 28 20 73 71 6c 69 74 65 33 49 73 64  hile( sqlite3Isd
32fff 69 67 69 74 28 7a 5b 69 5d 29 20 29 7b 20 69 2b  igit(z[i]) ){ i+
33000 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 2a 74 6f  +; }.        *to
33001 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 46 4c 4f  kenType = TK_FLO
33002 41 54 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64  AT;.      }.#end
33003 69 66 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  if.      while( 
33004 49 64 43 68 61 72 28 7a 5b 69 5d 29 20 29 7b 0a  IdChar(z[i]) ){.
33005 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
33006 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b  pe = TK_ILLEGAL;
33007 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20  .        i++;.  
33008 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75      }.      retu
33009 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn i;.    }.    
3300a 63 61 73 65 20 27 5b 27 3a 20 7b 0a 20 20 20 20  case '[': {.    
3300b 20 20 66 6f 72 28 69 3d 31 2c 20 63 3d 7a 5b 30    for(i=1, c=z[0
3300c 5d 3b 20 63 21 3d 27 5d 27 20 26 26 20 28 63 3d  ]; c!=']' && (c=
3300d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 7d  z[i])!=0; i++){}
3300e 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
3300f 65 20 3d 20 63 3d 3d 27 5d 27 20 3f 20 54 4b 5f  e = c==']' ? TK_
33010 49 44 20 3a 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b  ID : TK_ILLEGAL;
33011 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b  .      return i;
33012 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
33013 27 3f 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f  '?': {.      *to
33014 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 56 41 52  kenType = TK_VAR
33015 49 41 42 4c 45 3b 0a 20 20 20 20 20 20 66 6f 72  IABLE;.      for
33016 28 69 3d 31 3b 20 73 71 6c 69 74 65 33 49 73 64  (i=1; sqlite3Isd
33017 69 67 69 74 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29  igit(z[i]); i++)
33018 7b 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  {}.      return 
33019 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  i;.    }.    cas
3301a 65 20 27 23 27 3a 20 7b 0a 20 20 20 20 20 20 66  e '#': {.      f
3301b 6f 72 28 69 3d 31 3b 20 73 71 6c 69 74 65 33 49  or(i=1; sqlite3I
3301c 73 64 69 67 69 74 28 7a 5b 69 5d 29 3b 20 69 2b  sdigit(z[i]); i+
3301d 2b 29 7b 7d 0a 20 20 20 20 20 20 69 66 28 20 69  +){}.      if( i
3301e 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  >1 ){.        /*
3301f 20 50 61 72 61 6d 65 74 65 72 73 20 6f 66 20 74   Parameters of t
33020 68 65 20 66 6f 72 6d 20 23 4e 4e 4e 20 28 77 68  he form #NNN (wh
33021 65 72 65 20 4e 4e 4e 20 69 73 20 61 20 6e 75 6d  ere NNN is a num
33022 62 65 72 29 20 61 72 65 20 75 73 65 64 0a 20 20  ber) are used.  
33023 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 72 6e 61        ** interna
33024 6c 6c 79 20 62 79 20 73 71 6c 69 74 65 33 4e 65  lly by sqlite3Ne
33025 73 74 65 64 50 61 72 73 65 2e 20 20 2a 2f 0a 20  stedParse.  */. 
33026 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
33027 65 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b  e = TK_REGISTER;
33028 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
33029 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  i;.      }.     
3302a 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67 68   /* Fall through
3302b 20 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63   into the next c
3302c 61 73 65 20 69 66 20 74 68 65 20 27 23 27 20 69  ase if the '#' i
3302d 73 20 6e 6f 74 20 66 6f 6c 6c 6f 77 65 64 20 62  s not followed b
3302e 79 0a 20 20 20 20 20 20 2a 2a 20 61 20 64 69 67  y.      ** a dig
3302f 69 74 2e 20 54 72 79 20 74 6f 20 6d 61 74 63 68  it. Try to match
33030 20 23 41 41 41 41 20 77 68 65 72 65 20 41 41 41   #AAAA where AAA
33031 41 20 69 73 20 61 20 70 61 72 61 6d 65 74 65 72  A is a parameter
33032 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 7d 0a   name. */.    }.
33033 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
33034 4d 49 54 5f 54 43 4c 5f 56 41 52 49 41 42 4c 45  MIT_TCL_VARIABLE
33035 0a 20 20 20 20 63 61 73 65 20 27 24 27 3a 0a 23  .    case '$':.#
33036 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20 27  endif.    case '
33037 40 27 3a 20 20 2f 2a 20 46 6f 72 20 63 6f 6d 70  @':  /* For comp
33038 61 74 69 62 69 6c 69 74 79 20 77 69 74 68 20 4d  atibility with M
33039 53 20 53 51 4c 20 53 65 72 76 65 72 20 2a 2f 0a  S SQL Server */.
3303a 20 20 20 20 63 61 73 65 20 27 3a 27 3a 20 7b 0a      case ':': {.
3303b 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 30 3b        int n = 0;
3303c 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
3303d 20 7a 5b 30 5d 3d 3d 27 24 27 20 29 3b 20 20 74   z[0]=='$' );  t
3303e 65 73 74 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27  estcase( z[0]=='
3303f 40 27 20 29 3b 20 20 74 65 73 74 63 61 73 65 28  @' );  testcase(
33040 20 7a 5b 30 5d 3d 3d 27 3a 27 20 29 3b 0a 20 20   z[0]==':' );.  
33041 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
33042 20 54 4b 5f 56 41 52 49 41 42 4c 45 3b 0a 20 20   TK_VARIABLE;.  
33043 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 28 63 3d      for(i=1; (c=
33044 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a  z[i])!=0; i++){.
33045 20 20 20 20 20 20 20 20 69 66 28 20 49 64 43 68          if( IdCh
33046 61 72 28 63 29 20 29 7b 0a 20 20 20 20 20 20 20  ar(c) ){.       
33047 20 20 20 6e 2b 2b 3b 0a 23 69 66 6e 64 65 66 20     n++;.#ifndef 
33048 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 43 4c 5f  SQLITE_OMIT_TCL_
33049 56 41 52 49 41 42 4c 45 0a 20 20 20 20 20 20 20  VARIABLE.       
3304a 20 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 28   }else if( c=='(
3304b 27 20 26 26 20 6e 3e 30 20 29 7b 0a 20 20 20 20  ' && n>0 ){.    
3304c 20 20 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20        do{.      
3304d 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20        i++;.     
3304e 20 20 20 20 20 7d 77 68 69 6c 65 28 20 28 63 3d       }while( (c=
3304f 7a 5b 69 5d 29 21 3d 30 20 26 26 20 21 73 71 6c  z[i])!=0 && !sql
33050 69 74 65 33 49 73 73 70 61 63 65 28 63 29 20 26  ite3Isspace(c) &
33051 26 20 63 21 3d 27 29 27 20 29 3b 0a 20 20 20 20  & c!=')' );.    
33052 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 29 27        if( c==')'
33053 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
33054 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  i++;.          }
33055 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
33056 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
33057 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20  K_ILLEGAL;.     
33058 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
33059 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
3305a 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 27 3a 27  }else if( c==':'
3305b 20 26 26 20 7a 5b 69 2b 31 5d 3d 3d 27 3a 27 20   && z[i+1]==':' 
3305c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 2b 2b  ){.          i++
3305d 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  ;.#endif.       
3305e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
3305f 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
33060 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
33061 20 69 66 28 20 6e 3d 3d 30 20 29 20 2a 74 6f 6b   if( n==0 ) *tok
33062 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45  enType = TK_ILLE
33063 47 41 4c 3b 0a 20 20 20 20 20 20 72 65 74 75 72  GAL;.      retur
33064 6e 20 69 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64  n i;.    }.#ifnd
33065 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42  ef SQLITE_OMIT_B
33066 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 20 20 20 20  LOB_LITERAL.    
33067 63 61 73 65 20 27 78 27 3a 20 63 61 73 65 20 27  case 'x': case '
33068 58 27 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  X': {.      test
33069 63 61 73 65 28 20 7a 5b 30 5d 3d 3d 27 78 27 20  case( z[0]=='x' 
3306a 29 3b 20 74 65 73 74 63 61 73 65 28 20 7a 5b 30  ); testcase( z[0
3306b 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20 20 20 20  ]=='X' );.      
3306c 69 66 28 20 7a 5b 31 5d 3d 3d 27 5c 27 27 20 29  if( z[1]=='\'' )
3306d 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
3306e 54 79 70 65 20 3d 20 54 4b 5f 42 4c 4f 42 3b 0a  Type = TK_BLOB;.
3306f 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 32 3b          for(i=2;
33070 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 20 26 26 20   (c=z[i])!=0 && 
33071 63 21 3d 27 5c 27 27 3b 20 69 2b 2b 29 7b 0a 20  c!='\''; i++){. 
33072 20 20 20 20 20 20 20 20 20 69 66 28 20 21 73 71           if( !sq
33073 6c 69 74 65 33 49 73 78 64 69 67 69 74 28 63 29  lite3Isxdigit(c)
33074 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
33075 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
33076 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20  ILLEGAL;.       
33077 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
33078 20 20 20 20 20 20 20 69 66 28 20 69 25 32 20 7c         if( i%2 |
33079 7c 20 21 63 20 29 20 2a 74 6f 6b 65 6e 54 79 70  | !c ) *tokenTyp
3307a 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a  e = TK_ILLEGAL;.
3307b 20 20 20 20 20 20 20 20 69 66 28 20 63 20 29 20          if( c ) 
3307c 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 72 65 74  i++;.        ret
3307d 75 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20  urn i;.      }. 
3307e 20 20 20 20 20 2f 2a 20 4f 74 68 65 72 77 69 73       /* Otherwis
3307f 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74  e fall through t
33080 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 20  o the next case 
33081 2a 2f 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  */.    }.#endif.
33082 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
33083 20 20 20 20 20 69 66 28 20 21 49 64 43 68 61 72       if( !IdChar
33084 28 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20 20 20  (*z) ){.        
33085 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
33086 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 49 64       for(i=1; Id
33087 43 68 61 72 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29  Char(z[i]); i++)
33088 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  {}.      *tokenT
33089 79 70 65 20 3d 20 6b 65 79 77 6f 72 64 43 6f 64  ype = keywordCod
3308a 65 28 28 63 68 61 72 2a 29 7a 2c 20 69 29 3b 0a  e((char*)z, i);.
3308b 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a        return i;.
3308c 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 74 6f 6b      }.  }.  *tok
3308d 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45  enType = TK_ILLE
3308e 47 41 4c 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  GAL;.  return 1;
3308f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 75 6e 20 74 68  .}../*.** Run th
33090 65 20 70 61 72 73 65 72 20 6f 6e 20 74 68 65 20  e parser on the 
33091 67 69 76 65 6e 20 53 51 4c 20 73 74 72 69 6e 67  given SQL string
33092 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 73 74  .  The parser st
33093 72 75 63 74 75 72 65 20 69 73 0a 2a 2a 20 70 61  ructure is.** pa
33094 73 73 65 64 20 69 6e 2e 20 20 41 6e 20 53 51 4c  ssed in.  An SQL
33095 49 54 45 5f 20 73 74 61 74 75 73 20 63 6f 64 65  ITE_ status code
33096 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49   is returned.  I
33097 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
33098 73 0a 2a 2a 20 74 68 65 6e 20 61 6e 20 61 6e 64  s.** then an and
33099 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
3309a 20 74 6f 20 77 72 69 74 65 20 61 6e 20 65 72 72   to write an err
3309b 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 74 6f 20  or message into 
3309c 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69  .** memory obtai
3309d 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
3309e 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 74 6f  _malloc() and to
3309f 20 6d 61 6b 65 20 2a 70 7a 45 72 72 4d 73 67 20   make *pzErrMsg 
330a0 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 0a 2a 2a  point to that.**
330a1 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a   error message..
330a2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
330a3 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 75 6e  E int sqlite3Run
330a4 50 61 72 73 65 72 28 50 61 72 73 65 20 2a 70 50  Parser(Parse *pP
330a5 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  arse, const char
330a6 20 2a 7a 53 71 6c 2c 20 63 68 61 72 20 2a 2a 70   *zSql, char **p
330a7 7a 45 72 72 4d 73 67 29 7b 0a 20 20 69 6e 74 20  zErrMsg){.  int 
330a8 6e 45 72 72 20 3d 20 30 3b 20 20 20 20 20 20 20  nErr = 0;       
330a9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
330aa 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20  umber of errors 
330ab 65 6e 63 6f 75 6e 74 65 72 65 64 20 2a 2f 0a 20  encountered */. 
330ac 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
330ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
330ae 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
330af 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 45 6e 67   */.  void *pEng
330b0 69 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ine;            
330b1 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c 45 4d        /* The LEM
330b2 4f 4e 2d 67 65 6e 65 72 61 74 65 64 20 4c 41 4c  ON-generated LAL
330b3 52 28 31 29 20 70 61 72 73 65 72 20 2a 2f 0a 20  R(1) parser */. 
330b4 20 69 6e 74 20 74 6f 6b 65 6e 54 79 70 65 3b 20   int tokenType; 
330b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
330b6 20 2f 2a 20 74 79 70 65 20 6f 66 20 74 68 65 20   /* type of the 
330b7 6e 65 78 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20  next token */.  
330b8 69 6e 74 20 6c 61 73 74 54 6f 6b 65 6e 50 61 72  int lastTokenPar
330b9 73 65 64 20 3d 20 2d 31 3b 20 20 20 20 20 20 20  sed = -1;       
330ba 2f 2a 20 74 79 70 65 20 6f 66 20 74 68 65 20 70  /* type of the p
330bb 72 65 76 69 6f 75 73 20 74 6f 6b 65 6e 20 2a 2f  revious token */
330bc 0a 20 20 75 38 20 65 6e 61 62 6c 65 4c 6f 6f 6b  .  u8 enableLook
330bd 61 73 69 64 65 3b 20 20 20 20 20 20 20 20 20 20  aside;          
330be 20 20 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75     /* Saved valu
330bf 65 20 6f 66 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  e of db->lookasi
330c0 64 65 2e 62 45 6e 61 62 6c 65 64 20 2a 2f 0a 20  de.bEnabled */. 
330c1 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
330c2 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20 20  Parse->db;      
330c3 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
330c4 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
330c5 20 69 6e 74 20 6d 78 53 71 6c 4c 65 6e 3b 20 20   int mxSqlLen;  
330c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
330c7 20 2f 2a 20 4d 61 78 20 6c 65 6e 67 74 68 20 6f   /* Max length o
330c8 66 20 61 6e 20 53 51 4c 20 73 74 72 69 6e 67 20  f an SQL string 
330c9 2a 2f 0a 0a 0a 20 20 6d 78 53 71 6c 4c 65 6e 20  */...  mxSqlLen 
330ca 3d 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  = db->aLimit[SQL
330cb 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45  ITE_LIMIT_SQL_LE
330cc 4e 47 54 48 5d 3b 0a 20 20 69 66 28 20 64 62 2d  NGTH];.  if( db-
330cd 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74 3d 3d  >activeVdbeCnt==
330ce 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 75 31 2e  0 ){.    db->u1.
330cf 69 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20  isInterrupted = 
330d0 30 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d  0;.  }.  pParse-
330d1 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  >rc = SQLITE_OK;
330d2 0a 20 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c  .  pParse->zTail
330d3 20 3d 20 7a 53 71 6c 3b 0a 20 20 69 20 3d 20 30   = zSql;.  i = 0
330d4 3b 0a 20 20 61 73 73 65 72 74 28 20 70 7a 45 72  ;.  assert( pzEr
330d5 72 4d 73 67 21 3d 30 20 29 3b 0a 20 20 70 45 6e  rMsg!=0 );.  pEn
330d6 67 69 6e 65 20 3d 20 73 71 6c 69 74 65 33 50 61  gine = sqlite3Pa
330d7 72 73 65 72 41 6c 6c 6f 63 28 28 76 6f 69 64 2a  rserAlloc((void*
330d8 28 2a 29 28 73 69 7a 65 5f 74 29 29 73 71 6c 69  (*)(size_t))sqli
330d9 74 65 33 4d 61 6c 6c 6f 63 29 3b 0a 20 20 69 66  te3Malloc);.  if
330da 28 20 70 45 6e 67 69 6e 65 3d 3d 30 20 29 7b 0a  ( pEngine==0 ){.
330db 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
330dc 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65  iled = 1;.    re
330dd 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
330de 4d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  M;.  }.  assert(
330df 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
330e0 6c 65 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  le==0 );.  asser
330e1 74 28 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54  t( pParse->pNewT
330e2 72 69 67 67 65 72 3d 3d 30 20 29 3b 0a 20 20 61  rigger==0 );.  a
330e3 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e  ssert( pParse->n
330e4 56 61 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65  Var==0 );.  asse
330e5 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 56 61 72  rt( pParse->nVar
330e6 45 78 70 72 3d 3d 30 20 29 3b 0a 20 20 61 73 73  Expr==0 );.  ass
330e7 65 72 74 28 20 70 50 61 72 73 65 2d 3e 6e 56 61  ert( pParse->nVa
330e8 72 45 78 70 72 41 6c 6c 6f 63 3d 3d 30 20 29 3b  rExprAlloc==0 );
330e9 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73  .  assert( pPars
330ea 65 2d 3e 61 70 56 61 72 45 78 70 72 3d 3d 30 20  e->apVarExpr==0 
330eb 29 3b 0a 20 20 65 6e 61 62 6c 65 4c 6f 6f 6b 61  );.  enableLooka
330ec 73 69 64 65 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61  side = db->looka
330ed 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 3b 0a 20  side.bEnabled;. 
330ee 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69   if( db->lookasi
330ef 64 65 2e 70 53 74 61 72 74 20 29 20 64 62 2d 3e  de.pStart ) db->
330f0 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c  lookaside.bEnabl
330f1 65 64 20 3d 20 31 3b 0a 20 20 77 68 69 6c 65 28  ed = 1;.  while(
330f2 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
330f3 65 64 20 26 26 20 7a 53 71 6c 5b 69 5d 21 3d 30  ed && zSql[i]!=0
330f4 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
330f5 69 3e 3d 30 20 29 3b 0a 20 20 20 20 70 50 61 72  i>=0 );.    pPar
330f6 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a  se->sLastToken.z
330f7 20 3d 20 26 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20   = &zSql[i];.   
330f8 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f   pParse->sLastTo
330f9 6b 65 6e 2e 6e 20 3d 20 73 71 6c 69 74 65 33 47  ken.n = sqlite3G
330fa 65 74 54 6f 6b 65 6e 28 28 75 6e 73 69 67 6e 65  etToken((unsigne
330fb 64 20 63 68 61 72 2a 29 26 7a 53 71 6c 5b 69 5d  d char*)&zSql[i]
330fc 2c 26 74 6f 6b 65 6e 54 79 70 65 29 3b 0a 20 20  ,&tokenType);.  
330fd 20 20 69 20 2b 3d 20 70 50 61 72 73 65 2d 3e 73    i += pParse->s
330fe 4c 61 73 74 54 6f 6b 65 6e 2e 6e 3b 0a 20 20 20  LastToken.n;.   
330ff 20 69 66 28 20 69 3e 6d 78 53 71 6c 4c 65 6e 20   if( i>mxSqlLen 
33100 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  ){.      pParse-
33101 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f  >rc = SQLITE_TOO
33102 42 49 47 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  BIG;.      break
33103 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 77 69 74  ;.    }.    swit
33104 63 68 28 20 74 6f 6b 65 6e 54 79 70 65 20 29 7b  ch( tokenType ){
33105 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f 53  .      case TK_S
33106 50 41 43 45 3a 20 7b 0a 20 20 20 20 20 20 20 20  PACE: {.        
33107 69 66 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74  if( db->u1.isInt
33108 65 72 72 75 70 74 65 64 20 29 7b 0a 20 20 20 20  errupted ){.    
33109 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
3310a 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69  orMsg(pParse, "i
3310b 6e 74 65 72 72 75 70 74 22 29 3b 0a 20 20 20 20  nterrupt");.    
3310c 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63        pParse->rc
3310d 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52   = SQLITE_INTERR
3310e 55 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67  UPT;.          g
3310f 6f 74 6f 20 61 62 6f 72 74 5f 70 61 72 73 65 3b  oto abort_parse;
33110 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
33111 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
33112 7d 0a 20 20 20 20 20 20 63 61 73 65 20 54 4b 5f  }.      case TK_
33113 49 4c 4c 45 47 41 4c 3a 20 7b 0a 20 20 20 20 20  ILLEGAL: {.     
33114 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
33115 28 64 62 2c 20 2a 70 7a 45 72 72 4d 73 67 29 3b  (db, *pzErrMsg);
33116 0a 20 20 20 20 20 20 20 20 2a 70 7a 45 72 72 4d  .        *pzErrM
33117 73 67 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  sg = sqlite3MPri
33118 6e 74 66 28 64 62 2c 20 22 75 6e 72 65 63 6f 67  ntf(db, "unrecog
33119 6e 69 7a 65 64 20 74 6f 6b 65 6e 3a 20 5c 22 25  nized token: \"%
3311a 54 5c 22 22 2c 0a 20 20 20 20 20 20 20 20 20 20  T\"",.          
3311b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 70                &p
3311c 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65  Parse->sLastToke
3311d 6e 29 3b 0a 20 20 20 20 20 20 20 20 6e 45 72 72  n);.        nErr
3311e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  ++;.        goto
3311f 20 61 62 6f 72 74 5f 70 61 72 73 65 3b 0a 20 20   abort_parse;.  
33120 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
33121 20 54 4b 5f 53 45 4d 49 3a 20 7b 0a 20 20 20 20   TK_SEMI: {.    
33122 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 54 61 69      pParse->zTai
33123 6c 20 3d 20 26 7a 53 71 6c 5b 69 5d 3b 0a 20 20  l = &zSql[i];.  
33124 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20 74 68        /* Fall th
33125 72 75 20 69 6e 74 6f 20 74 68 65 20 64 65 66 61  ru into the defa
33126 75 6c 74 20 63 61 73 65 20 2a 2f 0a 20 20 20 20  ult case */.    
33127 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c    }.      defaul
33128 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  t: {.        sql
33129 69 74 65 33 50 61 72 73 65 72 28 70 45 6e 67 69  ite3Parser(pEngi
3312a 6e 65 2c 20 74 6f 6b 65 6e 54 79 70 65 2c 20 70  ne, tokenType, p
3312b 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65  Parse->sLastToke
3312c 6e 2c 20 70 50 61 72 73 65 29 3b 0a 20 20 20 20  n, pParse);.    
3312d 20 20 20 20 6c 61 73 74 54 6f 6b 65 6e 50 61 72      lastTokenPar
3312e 73 65 64 20 3d 20 74 6f 6b 65 6e 54 79 70 65 3b  sed = tokenType;
3312f 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 61  .        if( pPa
33130 72 73 65 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f  rse->rc!=SQLITE_
33131 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
33132 67 6f 74 6f 20 61 62 6f 72 74 5f 70 61 72 73 65  goto abort_parse
33133 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
33134 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
33135 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 61 62 6f   }.    }.  }.abo
33136 72 74 5f 70 61 72 73 65 3a 0a 20 20 69 66 28 20  rt_parse:.  if( 
33137 7a 53 71 6c 5b 69 5d 3d 3d 30 20 26 26 20 6e 45  zSql[i]==0 && nE
33138 72 72 3d 3d 30 20 26 26 20 70 50 61 72 73 65 2d  rr==0 && pParse-
33139 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  >rc==SQLITE_OK )
3313a 7b 0a 20 20 20 20 69 66 28 20 6c 61 73 74 54 6f  {.    if( lastTo
3313b 6b 65 6e 50 61 72 73 65 64 21 3d 54 4b 5f 53 45  kenParsed!=TK_SE
3313c 4d 49 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  MI ){.      sqli
3313d 74 65 33 50 61 72 73 65 72 28 70 45 6e 67 69 6e  te3Parser(pEngin
3313e 65 2c 20 54 4b 5f 53 45 4d 49 2c 20 70 50 61 72  e, TK_SEMI, pPar
3313f 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c 20  se->sLastToken, 
33140 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 70  pParse);.      p
33141 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20 3d 20 26  Parse->zTail = &
33142 7a 53 71 6c 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20  zSql[i];.    }. 
33143 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72     sqlite3Parser
33144 28 70 45 6e 67 69 6e 65 2c 20 30 2c 20 70 50 61  (pEngine, 0, pPa
33145 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2c  rse->sLastToken,
33146 20 70 50 61 72 73 65 29 3b 0a 20 20 7d 0a 23 69   pParse);.  }.#i
33147 66 64 65 66 20 59 59 54 52 41 43 4b 4d 41 58 53  fdef YYTRACKMAXS
33148 54 41 43 4b 44 45 50 54 48 0a 20 20 73 71 6c 69  TACKDEPTH.  sqli
33149 74 65 33 53 74 61 74 75 73 53 65 74 28 53 51 4c  te3StatusSet(SQL
3314a 49 54 45 5f 53 54 41 54 55 53 5f 50 41 52 53 45  ITE_STATUS_PARSE
3314b 52 5f 53 54 41 43 4b 2c 0a 20 20 20 20 20 20 73  R_STACK,.      s
3314c 71 6c 69 74 65 33 50 61 72 73 65 72 53 74 61 63  qlite3ParserStac
3314d 6b 50 65 61 6b 28 70 45 6e 67 69 6e 65 29 0a 20  kPeak(pEngine). 
3314e 20 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 59 59   );.#endif /* YY
3314f 44 45 42 55 47 20 2a 2f 0a 20 20 73 71 6c 69 74  DEBUG */.  sqlit
33150 65 33 50 61 72 73 65 72 46 72 65 65 28 70 45 6e  e3ParserFree(pEn
33151 67 69 6e 65 2c 20 73 71 6c 69 74 65 33 5f 66 72  gine, sqlite3_fr
33152 65 65 29 3b 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61  ee);.  db->looka
33153 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20  side.bEnabled = 
33154 65 6e 61 62 6c 65 4c 6f 6f 6b 61 73 69 64 65 3b  enableLookaside;
33155 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
33156 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70  cFailed ){.    p
33157 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49  Parse->rc = SQLI
33158 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
33159 69 66 28 20 70 50 61 72 73 65 2d 3e 72 63 21 3d  if( pParse->rc!=
3315a 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 50 61  SQLITE_OK && pPa
3315b 72 73 65 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f  rse->rc!=SQLITE_
3315c 44 4f 4e 45 20 26 26 20 70 50 61 72 73 65 2d 3e  DONE && pParse->
3315d 7a 45 72 72 4d 73 67 3d 3d 30 20 29 7b 0a 20 20  zErrMsg==0 ){.  
3315e 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
3315f 6e 67 28 26 70 50 61 72 73 65 2d 3e 7a 45 72 72  ng(&pParse->zErr
33160 4d 73 67 2c 20 64 62 2c 20 22 25 73 22 2c 20 73  Msg, db, "%s", s
33161 71 6c 69 74 65 33 45 72 72 53 74 72 28 70 50 61  qlite3ErrStr(pPa
33162 72 73 65 2d 3e 72 63 29 29 3b 0a 20 20 7d 0a 20  rse->rc));.  }. 
33163 20 61 73 73 65 72 74 28 20 70 7a 45 72 72 4d 73   assert( pzErrMs
33164 67 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 50  g!=0 );.  if( pP
33165 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 29 7b  arse->zErrMsg ){
33166 0a 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d  .    *pzErrMsg =
33167 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
33168 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 45  ;.    pParse->zE
33169 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 6e  rrMsg = 0;.    n
3316a 45 72 72 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28  Err++;.  }.  if(
3316b 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 20 26   pParse->pVdbe &
3316c 26 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3e 30  & pParse->nErr>0
3316d 20 26 26 20 70 50 61 72 73 65 2d 3e 6e 65 73 74   && pParse->nest
3316e 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  ed==0 ){.    sql
3316f 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 28 70  ite3VdbeDelete(p
33170 50 61 72 73 65 2d 3e 70 56 64 62 65 29 3b 0a 20  Parse->pVdbe);. 
33171 20 20 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65     pParse->pVdbe
33172 20 3d 20 30 3b 0a 20 20 7d 0a 23 69 66 6e 64 65   = 0;.  }.#ifnde
33173 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
33174 41 52 45 44 5f 43 41 43 48 45 0a 20 20 69 66 28  ARED_CACHE.  if(
33175 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d   pParse->nested=
33176 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
33177 33 44 62 46 72 65 65 28 64 62 2c 20 70 50 61 72  3DbFree(db, pPar
33178 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 29 3b  se->aTableLock);
33179 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61 54 61  .    pParse->aTa
3317a 62 6c 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20  bleLock = 0;.   
3317b 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c   pParse->nTableL
3317c 6f 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e  ock = 0;.  }.#en
3317d 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  dif.#ifndef SQLI
3317e 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
3317f 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 44 62  ABLE.  sqlite3Db
33180 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d  Free(db, pParse-
33181 3e 61 70 56 74 61 62 4c 6f 63 6b 29 3b 0a 23 65  >apVtabLock);.#e
33182 6e 64 69 66 0a 0a 20 20 69 66 28 20 21 49 4e 5f  ndif..  if( !IN_
33183 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a  DECLARE_VTAB ){.
33184 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 70 50      /* If the pP
33185 61 72 73 65 2d 3e 64 65 63 6c 61 72 65 56 74 61  arse->declareVta
33186 62 20 66 6c 61 67 20 69 73 20 73 65 74 2c 20 64  b flag is set, d
33187 6f 20 6e 6f 74 20 64 65 6c 65 74 65 20 61 6e 79  o not delete any
33188 20 74 61 62 6c 65 20 0a 20 20 20 20 2a 2a 20 73   table .    ** s
33189 74 72 75 63 74 75 72 65 20 62 75 69 6c 74 20 75  tructure built u
3318a 70 20 69 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65  p in pParse->pNe
3318b 77 54 61 62 6c 65 2e 20 54 68 65 20 63 61 6c 6c  wTable. The call
3318c 69 6e 67 20 63 6f 64 65 20 28 73 65 65 20 76 74  ing code (see vt
3318d 61 62 2e 63 29 0a 20 20 20 20 2a 2a 20 77 69 6c  ab.c).    ** wil
3318e 6c 20 74 61 6b 65 20 72 65 73 70 6f 6e 73 69 62  l take responsib
3318f 69 6c 69 74 79 20 66 6f 72 20 66 72 65 65 69 6e  ility for freein
33190 67 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75  g the Table stru
33191 63 74 75 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20  cture..    */.  
33192 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
33193 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 70 4e 65  able(pParse->pNe
33194 77 54 61 62 6c 65 29 3b 0a 20 20 7d 0a 0a 20 20  wTable);.  }..  
33195 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69  sqlite3DeleteTri
33196 67 67 65 72 28 64 62 2c 20 70 50 61 72 73 65 2d  gger(db, pParse-
33197 3e 70 4e 65 77 54 72 69 67 67 65 72 29 3b 0a 20  >pNewTrigger);. 
33198 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
33199 62 2c 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72  b, pParse->apVar
3319a 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  Expr);.  sqlite3
3319b 44 62 46 72 65 65 28 64 62 2c 20 70 50 61 72 73  DbFree(db, pPars
3319c 65 2d 3e 61 41 6c 69 61 73 29 3b 0a 20 20 77 68  e->aAlias);.  wh
3319d 69 6c 65 28 20 70 50 61 72 73 65 2d 3e 70 41 69  ile( pParse->pAi
3319e 6e 63 20 29 7b 0a 20 20 20 20 41 75 74 6f 69 6e  nc ){.    Autoin
3319f 63 49 6e 66 6f 20 2a 70 20 3d 20 70 50 61 72 73  cInfo *p = pPars
331a0 65 2d 3e 70 41 69 6e 63 3b 0a 20 20 20 20 70 50  e->pAinc;.    pP
331a1 61 72 73 65 2d 3e 70 41 69 6e 63 20 3d 20 70 2d  arse->pAinc = p-
331a2 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71 6c 69  >pNext;.    sqli
331a3 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 29  te3DbFree(db, p)
331a4 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 70  ;.  }.  while( p
331a5 50 61 72 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61  Parse->pZombieTa
331a6 62 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a  b ){.    Table *
331a7 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 5a 6f 6d  p = pParse->pZom
331a8 62 69 65 54 61 62 3b 0a 20 20 20 20 70 50 61 72  bieTab;.    pPar
331a9 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 20 3d  se->pZombieTab =
331aa 20 70 2d 3e 70 4e 65 78 74 5a 6f 6d 62 69 65 3b   p->pNextZombie;
331ab 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65  .    sqlite3Dele
331ac 74 65 54 61 62 6c 65 28 70 29 3b 0a 20 20 7d 0a  teTable(p);.  }.
331ad 20 20 69 66 28 20 6e 45 72 72 3e 30 20 26 26 20    if( nErr>0 && 
331ae 70 50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49  pParse->rc==SQLI
331af 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61  TE_OK ){.    pPa
331b0 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45  rse->rc = SQLITE
331b1 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65  _ERROR;.  }.  re
331b2 74 75 72 6e 20 6e 45 72 72 3b 0a 7d 0a 0a 2f 2a  turn nErr;.}../*
331b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
331b4 64 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 2e 63 20  d of tokenize.c 
331b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
331b8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
331b9 67 69 6e 20 66 69 6c 65 20 63 6f 6d 70 6c 65 74  gin file complet
331ba 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
331bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
331bd 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
331be 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
331bf 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
331c0 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
331c1 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
331c2 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
331c3 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
331c4 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
331c5 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
331c6 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
331c7 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
331c8 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
331c9 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
331ca 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
331cb 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
331cc 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
331cd 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
331ce 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
331cf 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
331d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
331d4 2a 2a 2a 2a 0a 2a 2a 20 41 6e 20 74 6f 6b 65 6e  ****.** An token
331d5 69 7a 65 72 20 66 6f 72 20 53 51 4c 0a 2a 2a 0a  izer for SQL.**.
331d6 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
331d7 74 61 69 6e 73 20 43 20 63 6f 64 65 20 74 68 61  tains C code tha
331d8 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  t implements the
331d9 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
331da 65 28 29 20 41 50 49 2e 0a 2a 2a 20 54 68 69 73  e() API..** This
331db 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20 62 65   code used to be
331dc 20 70 61 72 74 20 6f 66 20 74 68 65 20 74 6f 6b   part of the tok
331dd 65 6e 69 7a 65 72 2e 63 20 73 6f 75 72 63 65 20  enizer.c source 
331de 66 69 6c 65 2e 20 20 42 75 74 20 62 79 0a 2a 2a  file.  But by.**
331df 20 73 65 70 61 72 61 74 69 6e 67 20 69 74 20 6f   separating it o
331e0 75 74 2c 20 74 68 65 20 63 6f 64 65 20 77 69 6c  ut, the code wil
331e1 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  l be automatical
331e2 6c 79 20 6f 6d 69 74 74 65 64 20 66 72 6f 6d 0a  ly omitted from.
331e3 2a 2a 20 73 74 61 74 69 63 20 6c 69 6e 6b 73 20  ** static links 
331e4 74 68 61 74 20 64 6f 20 6e 6f 74 20 75 73 65 20  that do not use 
331e5 69 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 63  it..**.** $Id: c
331e6 6f 6d 70 6c 65 74 65 2e 63 2c 76 20 31 2e 38 20  omplete.c,v 1.8 
331e7 32 30 30 39 2f 30 34 2f 32 38 20 30 34 3a 34 36  2009/04/28 04:46
331e8 3a 34 32 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :42 drh Exp $.*/
331e9 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
331ea 4f 4d 49 54 5f 43 4f 4d 50 4c 45 54 45 0a 0a 2f  OMIT_COMPLETE../
331eb 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 64 65 66  *.** This is def
331ec 69 6e 65 64 20 69 6e 20 74 6f 6b 65 6e 69 7a 65  ined in tokenize
331ed 2e 63 2e 20 20 57 65 20 6a 75 73 74 20 68 61 76  .c.  We just hav
331ee 65 20 74 6f 20 69 6d 70 6f 72 74 20 74 68 65 20  e to import the 
331ef 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 23  definition..*/.#
331f0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d  ifndef SQLITE_AM
331f1 41 4c 47 41 4d 41 54 49 4f 4e 0a 23 69 66 64 65  ALGAMATION.#ifde
331f2 66 20 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 53  f SQLITE_ASCII.S
331f3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
331f4 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 65 33  nst char sqlite3
331f5 49 73 41 73 63 69 69 49 64 43 68 61 72 5b 5d 3b  IsAsciiIdChar[];
331f6 0a 23 64 65 66 69 6e 65 20 49 64 43 68 61 72 28  .#define IdChar(
331f7 43 29 20 20 28 28 28 63 3d 43 29 26 30 78 38 30  C)  (((c=C)&0x80
331f8 29 21 3d 30 20 7c 7c 20 28 63 3e 30 78 31 66 20  )!=0 || (c>0x1f 
331f9 26 26 20 73 71 6c 69 74 65 33 49 73 41 73 63 69  && sqlite3IsAsci
331fa 69 49 64 43 68 61 72 5b 63 2d 30 78 32 30 5d 29  iIdChar[c-0x20])
331fb 29 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20  ).#endif.#ifdef 
331fc 53 51 4c 49 54 45 5f 45 42 43 44 49 43 0a 53 51  SQLITE_EBCDIC.SQ
331fd 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
331fe 73 74 20 63 68 61 72 20 73 71 6c 69 74 65 33 49  st char sqlite3I
331ff 73 45 62 63 64 69 63 49 64 43 68 61 72 5b 5d 3b  sEbcdicIdChar[];
33200 0a 23 64 65 66 69 6e 65 20 49 64 43 68 61 72 28  .#define IdChar(
33201 43 29 20 20 28 28 28 63 3d 43 29 3e 3d 30 78 34  C)  (((c=C)>=0x4
33202 32 20 26 26 20 73 71 6c 69 74 65 33 49 73 45 62  2 && sqlite3IsEb
33203 63 64 69 63 49 64 43 68 61 72 5b 63 2d 30 78 34  cdicIdChar[c-0x4
33204 30 5d 29 29 0a 23 65 6e 64 69 66 0a 23 65 6e 64  0])).#endif.#end
33205 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 41 4d 41  if /* SQLITE_AMA
33206 4c 47 41 4d 41 54 49 4f 4e 20 2a 2f 0a 0a 0a 2f  LGAMATION */.../
33207 2a 0a 2a 2a 20 54 6f 6b 65 6e 20 74 79 70 65 73  *.** Token types
33208 20 75 73 65 64 20 62 79 20 74 68 65 20 73 71 6c   used by the sql
33209 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 20  ite3_complete() 
3320a 72 6f 75 74 69 6e 65 2e 20 20 53 65 65 20 74 68  routine.  See th
3320b 65 20 68 65 61 64 65 72 0a 2a 2a 20 63 6f 6d 6d  e header.** comm
3320c 65 6e 74 73 20 6f 6e 20 74 68 61 74 20 70 72 6f  ents on that pro
3320d 63 65 64 75 72 65 20 66 6f 72 20 61 64 64 69 74  cedure for addit
3320e 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  ional informatio
3320f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 74 6b  n..*/.#define tk
33210 53 45 4d 49 20 20 20 20 30 0a 23 64 65 66 69 6e  SEMI    0.#defin
33211 65 20 74 6b 57 53 20 20 20 20 20 20 31 0a 23 64  e tkWS      1.#d
33212 65 66 69 6e 65 20 74 6b 4f 54 48 45 52 20 20 20  efine tkOTHER   
33213 32 0a 23 64 65 66 69 6e 65 20 74 6b 45 58 50 4c  2.#define tkEXPL
33214 41 49 4e 20 33 0a 23 64 65 66 69 6e 65 20 74 6b  AIN 3.#define tk
33215 43 52 45 41 54 45 20 20 34 0a 23 64 65 66 69 6e  CREATE  4.#defin
33216 65 20 74 6b 54 45 4d 50 20 20 20 20 35 0a 23 64  e tkTEMP    5.#d
33217 65 66 69 6e 65 20 74 6b 54 52 49 47 47 45 52 20  efine tkTRIGGER 
33218 36 0a 23 64 65 66 69 6e 65 20 74 6b 45 4e 44 20  6.#define tkEND 
33219 20 20 20 20 37 0a 0a 2f 2a 0a 2a 2a 20 52 65 74      7../*.** Ret
3321a 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20  urn TRUE if the 
3321b 67 69 76 65 6e 20 53 51 4c 20 73 74 72 69 6e 67  given SQL string
3321c 20 65 6e 64 73 20 69 6e 20 61 20 73 65 6d 69 63   ends in a semic
3321d 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 70 65 63  olon..**.** Spec
3321e 69 61 6c 20 68 61 6e 64 6c 69 6e 67 20 69 73 20  ial handling is 
3321f 72 65 71 75 69 72 65 20 66 6f 72 20 43 52 45 41  require for CREA
33220 54 45 20 54 52 49 47 47 45 52 20 73 74 61 74 65  TE TRIGGER state
33221 6d 65 6e 74 73 2e 0a 2a 2a 20 57 68 65 6e 65 76  ments..** Whenev
33222 65 72 20 74 68 65 20 43 52 45 41 54 45 20 54 52  er the CREATE TR
33223 49 47 47 45 52 20 6b 65 79 77 6f 72 64 73 20 61  IGGER keywords a
33224 72 65 20 73 65 65 6e 2c 20 74 68 65 20 73 74 61  re seen, the sta
33225 74 65 6d 65 6e 74 0a 2a 2a 20 6d 75 73 74 20 65  tement.** must e
33226 6e 64 20 77 69 74 68 20 22 3b 45 4e 44 3b 22 2e  nd with ";END;".
33227 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c  .**.** This impl
33228 65 6d 65 6e 74 61 74 69 6f 6e 20 75 73 65 73 20  ementation uses 
33229 61 20 73 74 61 74 65 20 6d 61 63 68 69 6e 65 20  a state machine 
3322a 77 69 74 68 20 37 20 73 74 61 74 65 73 3a 0a 2a  with 7 states:.*
3322b 2a 0a 2a 2a 20 20 20 28 30 29 20 53 54 41 52 54  *.**   (0) START
3322c 20 20 20 20 20 41 74 20 74 68 65 20 62 65 67 69       At the begi
3322d 6e 6e 69 6e 67 20 6f 72 20 65 6e 64 20 6f 66 20  nning or end of 
3322e 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  an SQL statement
3322f 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 0a  .  This routine.
33230 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
33231 20 20 20 72 65 74 75 72 6e 73 20 31 20 69 66 20     returns 1 if 
33232 69 74 20 65 6e 64 73 20 69 6e 20 74 68 65 20 53  it ends in the S
33233 54 41 52 54 20 73 74 61 74 65 20 61 6e 64 20 30  TART state and 0
33234 20 69 66 20 69 74 20 65 6e 64 73 0a 2a 2a 20 20   if it ends.**  
33235 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
33236 6e 20 61 6e 79 20 6f 74 68 65 72 20 73 74 61 74  n any other stat
33237 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 4e  e..**.**   (1) N
33238 4f 52 4d 41 4c 20 20 20 20 57 65 20 61 72 65 20  ORMAL    We are 
33239 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
3323a 20 73 74 61 74 65 6d 65 6e 74 20 77 68 69 63 68   statement which
3323b 20 65 6e 64 73 20 77 69 74 68 20 61 20 73 69 6e   ends with a sin
3323c 67 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  gle.**          
3323d 20 20 20 20 20 20 20 73 65 6d 69 63 6f 6c 6f 6e         semicolon
3323e 2e 0a 2a 2a 0a 2a 2a 20 20 20 28 32 29 20 45 58  ..**.**   (2) EX
3323f 50 4c 41 49 4e 20 20 20 54 68 65 20 6b 65 79 77  PLAIN   The keyw
33240 6f 72 64 20 45 58 50 4c 41 49 4e 20 68 61 73 20  ord EXPLAIN has 
33241 62 65 65 6e 20 73 65 65 6e 20 61 74 20 74 68 65  been seen at the
33242 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 0a 2a   beginning of .*
33243 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
33244 20 20 61 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a    a statement..*
33245 2a 0a 2a 2a 20 20 20 28 33 29 20 43 52 45 41 54  *.**   (3) CREAT
33246 45 20 20 20 20 54 68 65 20 6b 65 79 77 6f 72 64  E    The keyword
33247 20 43 52 45 41 54 45 20 68 61 73 20 62 65 65 6e   CREATE has been
33248 20 73 65 65 6e 20 61 74 20 74 68 65 20 62 65 67   seen at the beg
33249 69 6e 6e 69 6e 67 20 6f 66 20 61 0a 2a 2a 20 20  inning of a.**  
3324a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
3324b 74 61 74 65 6d 65 6e 74 2c 20 70 6f 73 73 69 62  tatement, possib
3324c 6c 79 20 70 72 65 63 65 65 64 65 64 20 62 79 20  ly preceeded by 
3324d 45 58 50 4c 41 49 4e 20 61 6e 64 2f 6f 72 20 66  EXPLAIN and/or f
3324e 6f 6c 6c 6f 77 65 64 20 62 79 0a 2a 2a 20 20 20  ollowed by.**   
3324f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54 45                TE
33250 4d 50 20 6f 72 20 54 45 4d 50 4f 52 41 52 59 0a  MP or TEMPORARY.
33251 2a 2a 0a 2a 2a 20 20 20 28 34 29 20 54 52 49 47  **.**   (4) TRIG
33252 47 45 52 20 20 20 57 65 20 61 72 65 20 69 6e 20  GER   We are in 
33253 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 61 20  the middle of a 
33254 74 72 69 67 67 65 72 20 64 65 66 69 6e 69 74 69  trigger definiti
33255 6f 6e 20 74 68 61 74 20 6d 75 73 74 20 62 65 0a  on that must be.
33256 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
33257 20 20 20 65 6e 64 65 64 20 62 79 20 61 20 73 65     ended by a se
33258 6d 69 63 6f 6c 6f 6e 2c 20 74 68 65 20 6b 65 79  micolon, the key
33259 77 6f 72 64 20 45 4e 44 2c 20 61 6e 64 20 61 6e  word END, and an
3325a 6f 74 68 65 72 20 73 65 6d 69 63 6f 6c 6f 6e 2e  other semicolon.
3325b 0a 2a 2a 0a 2a 2a 20 20 20 28 35 29 20 53 45 4d  .**.**   (5) SEM
3325c 49 20 20 20 20 20 20 57 65 27 76 65 20 73 65 65  I      We've see
3325d 6e 20 74 68 65 20 66 69 72 73 74 20 73 65 6d 69  n the first semi
3325e 63 6f 6c 6f 6e 20 69 6e 20 74 68 65 20 22 3b 45  colon in the ";E
3325f 4e 44 3b 22 20 74 68 61 74 20 6f 63 63 75 72 73  ND;" that occurs
33260 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   at.**          
33261 20 20 20 20 20 20 20 74 68 65 20 65 6e 64 20 6f         the end o
33262 66 20 61 20 74 72 69 67 67 65 72 20 64 65 66 69  f a trigger defi
33263 6e 69 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20  nition..**.**   
33264 28 36 29 20 45 4e 44 20 20 20 20 20 20 20 57 65  (6) END       We
33265 27 76 65 20 73 65 65 6e 20 74 68 65 20 22 3b 45  've seen the ";E
33266 4e 44 22 20 6f 66 20 74 68 65 20 22 3b 45 4e 44  ND" of the ";END
33267 3b 22 20 74 68 61 74 20 6f 63 63 75 72 73 20 61  ;" that occurs a
33268 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 20 20 20  t the end.**    
33269 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20               of 
3326a 61 20 74 72 69 67 67 65 72 20 64 69 66 69 6e 69  a trigger difini
3326b 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 72 61 6e  tion..**.** Tran
3326c 73 69 74 69 6f 6e 73 20 62 65 74 77 65 65 6e 20  sitions between 
3326d 73 74 61 74 65 73 20 61 62 6f 76 65 20 61 72 65  states above are
3326e 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74   determined by t
3326f 6f 6b 65 6e 73 20 65 78 74 72 61 63 74 65 64 0a  okens extracted.
33270 2a 2a 20 66 72 6f 6d 20 74 68 65 20 69 6e 70 75  ** from the inpu
33271 74 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  t.  The followin
33272 67 20 74 6f 6b 65 6e 73 20 61 72 65 20 73 69 67  g tokens are sig
33273 6e 69 66 69 63 61 6e 74 3a 0a 2a 2a 0a 2a 2a 20  nificant:.**.** 
33274 20 20 28 30 29 20 74 6b 53 45 4d 49 20 20 20 20    (0) tkSEMI    
33275 20 20 41 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a    A semicolon..*
33276 2a 20 20 20 28 31 29 20 74 6b 57 53 20 20 20 20  *   (1) tkWS    
33277 20 20 20 20 57 68 69 74 65 73 70 61 63 65 0a 2a      Whitespace.*
33278 2a 20 20 20 28 32 29 20 74 6b 4f 54 48 45 52 20  *   (2) tkOTHER 
33279 20 20 20 20 41 6e 79 20 6f 74 68 65 72 20 53 51      Any other SQ
3327a 4c 20 74 6f 6b 65 6e 2e 0a 2a 2a 20 20 20 28 33  L token..**   (3
3327b 29 20 74 6b 45 58 50 4c 41 49 4e 20 20 20 54 68  ) tkEXPLAIN   Th
3327c 65 20 22 65 78 70 6c 61 69 6e 22 20 6b 65 79 77  e "explain" keyw
3327d 6f 72 64 2e 0a 2a 2a 20 20 20 28 34 29 20 74 6b  ord..**   (4) tk
3327e 43 52 45 41 54 45 20 20 20 20 54 68 65 20 22 63  CREATE    The "c
3327f 72 65 61 74 65 22 20 6b 65 79 77 6f 72 64 2e 0a  reate" keyword..
33280 2a 2a 20 20 20 28 35 29 20 74 6b 54 45 4d 50 20  **   (5) tkTEMP 
33281 20 20 20 20 20 54 68 65 20 22 74 65 6d 70 22 20       The "temp" 
33282 6f 72 20 22 74 65 6d 70 6f 72 61 72 79 22 20 6b  or "temporary" k
33283 65 79 77 6f 72 64 2e 0a 2a 2a 20 20 20 28 36 29  eyword..**   (6)
33284 20 74 6b 54 52 49 47 47 45 52 20 20 20 54 68 65   tkTRIGGER   The
33285 20 22 74 72 69 67 67 65 72 22 20 6b 65 79 77 6f   "trigger" keywo
33286 72 64 2e 0a 2a 2a 20 20 20 28 37 29 20 74 6b 45  rd..**   (7) tkE
33287 4e 44 20 20 20 20 20 20 20 54 68 65 20 22 65 6e  ND       The "en
33288 64 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 0a 2a  d" keyword..**.*
33289 2a 20 57 68 69 74 65 73 70 61 63 65 20 6e 65 76  * Whitespace nev
3328a 65 72 20 63 61 75 73 65 73 20 61 20 73 74 61 74  er causes a stat
3328b 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 61 6e 64  e transition and
3328c 20 69 73 20 61 6c 77 61 79 73 20 69 67 6e 6f 72   is always ignor
3328d 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77 65 20  ed..**.** If we 
3328e 63 6f 6d 70 69 6c 65 20 77 69 74 68 20 53 51 4c  compile with SQL
3328f 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
33290 2c 20 61 6c 6c 20 6f 66 20 74 68 65 20 63 6f 6d  , all of the com
33291 70 75 74 61 74 69 6f 6e 20 6e 65 65 64 65 64 0a  putation needed.
33292 2a 2a 20 74 6f 20 72 65 63 6f 67 6e 69 7a 65 20  ** to recognize 
33293 74 68 65 20 65 6e 64 20 6f 66 20 61 20 74 72 69  the end of a tri
33294 67 67 65 72 20 63 61 6e 20 62 65 20 6f 6d 69 74  gger can be omit
33295 74 65 64 2e 20 20 41 6c 6c 20 77 65 20 68 61 76  ted.  All we hav
33296 65 20 74 6f 20 64 6f 0a 2a 2a 20 69 73 20 6c 6f  e to do.** is lo
33297 6f 6b 20 66 6f 72 20 61 20 73 65 6d 69 63 6f 6c  ok for a semicol
33298 6f 6e 20 74 68 61 74 20 69 73 20 6e 6f 74 20 70  on that is not p
33299 61 72 74 20 6f 66 20 61 6e 20 73 74 72 69 6e 67  art of an string
3329a 20 6f 72 20 63 6f 6d 6d 65 6e 74 2e 0a 2a 2f 0a   or comment..*/.
3329b 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
3329c 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28  qlite3_complete(
3329d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
3329e 29 7b 0a 20 20 75 38 20 73 74 61 74 65 20 3d 20  ){.  u8 state = 
3329f 30 3b 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20  0;   /* Current 
332a0 73 74 61 74 65 2c 20 75 73 69 6e 67 20 6e 75 6d  state, using num
332a1 62 65 72 73 20 64 65 66 69 6e 65 64 20 69 6e 20  bers defined in 
332a2 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20 2a  header comment *
332a3 2f 0a 20 20 75 38 20 74 6f 6b 65 6e 3b 20 20 20  /.  u8 token;   
332a4 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f 66 20      /* Value of 
332a5 74 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 2a  the next token *
332a6 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
332a7 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20  E_OMIT_TRIGGER. 
332a8 20 2f 2a 20 41 20 63 6f 6d 70 6c 65 78 20 73 74   /* A complex st
332a9 61 74 65 6d 65 6e 74 20 6d 61 63 68 69 6e 65 20  atement machine 
332aa 75 73 65 64 20 74 6f 20 64 65 74 65 63 74 20 74  used to detect t
332ab 68 65 20 65 6e 64 20 6f 66 20 61 20 43 52 45 41  he end of a CREA
332ac 54 45 20 54 52 49 47 47 45 52 0a 20 20 2a 2a 20  TE TRIGGER.  ** 
332ad 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 69 73  statement.  This
332ae 20 69 73 20 74 68 65 20 6e 6f 72 6d 61 6c 20 63   is the normal c
332af 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 73 74 61 74  ase..  */.  stat
332b0 69 63 20 63 6f 6e 73 74 20 75 38 20 74 72 61 6e  ic const u8 tran
332b1 73 5b 37 5d 5b 38 5d 20 3d 20 7b 0a 20 20 20 20  s[7][8] = {.    
332b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332b3 20 2f 2a 20 54 6f 6b 65 6e 3a 20 20 20 20 20 20   /* Token:      
332b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332b6 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
332b7 20 20 2f 2a 20 53 74 61 74 65 3a 20 20 20 20 20    /* State:     
332b8 20 20 2a 2a 20 20 53 45 4d 49 20 20 57 53 20 20    **  SEMI  WS  
332b9 4f 54 48 45 52 20 45 58 50 4c 41 49 4e 20 20 43  OTHER EXPLAIN  C
332ba 52 45 41 54 45 20 20 54 45 4d 50 20 20 54 52 49  REATE  TEMP  TRI
332bb 47 47 45 52 20 20 45 4e 44 20 20 2a 2f 0a 20 20  GGER  END  */.  
332bc 20 20 20 2f 2a 20 30 20 20 20 53 54 41 52 54 3a     /* 0   START:
332bd 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 30 2c 20   */ {    0,  0, 
332be 20 20 20 20 31 2c 20 20 20 20 20 20 32 2c 20 20      1,      2,  
332bf 20 20 20 20 33 2c 20 20 20 20 31 2c 20 20 20 20      3,    1,    
332c0 20 20 20 31 2c 20 20 20 31 2c 20 20 7d 2c 0a 20     1,   1,  },. 
332c1 20 20 20 20 2f 2a 20 31 20 20 4e 4f 52 4d 41 4c      /* 1  NORMAL
332c2 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 31 2c  : */ {    0,  1,
332c3 20 20 20 20 20 31 2c 20 20 20 20 20 20 31 2c 20       1,      1, 
332c4 20 20 20 20 20 31 2c 20 20 20 20 31 2c 20 20 20       1,    1,   
332c5 20 20 20 20 31 2c 20 20 20 31 2c 20 20 7d 2c 0a      1,   1,  },.
332c6 20 20 20 20 20 2f 2a 20 32 20 45 58 50 4c 41 49       /* 2 EXPLAI
332c7 4e 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 32  N: */ {    0,  2
332c8 2c 20 20 20 20 20 32 2c 20 20 20 20 20 20 31 2c  ,     2,      1,
332c9 20 20 20 20 20 20 33 2c 20 20 20 20 31 2c 20 20        3,    1,  
332ca 20 20 20 20 20 31 2c 20 20 20 31 2c 20 20 7d 2c       1,   1,  },
332cb 0a 20 20 20 20 20 2f 2a 20 33 20 20 43 52 45 41  .     /* 3  CREA
332cc 54 45 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20  TE: */ {    0,  
332cd 33 2c 20 20 20 20 20 31 2c 20 20 20 20 20 20 31  3,     1,      1
332ce 2c 20 20 20 20 20 20 31 2c 20 20 20 20 33 2c 20  ,      1,    3, 
332cf 20 20 20 20 20 20 34 2c 20 20 20 31 2c 20 20 7d        4,   1,  }
332d0 2c 0a 20 20 20 20 20 2f 2a 20 34 20 54 52 49 47  ,.     /* 4 TRIG
332d1 47 45 52 3a 20 2a 2f 20 7b 20 20 20 20 35 2c 20  GER: */ {    5, 
332d2 20 34 2c 20 20 20 20 20 34 2c 20 20 20 20 20 20   4,     4,      
332d3 34 2c 20 20 20 20 20 20 34 2c 20 20 20 20 34 2c  4,      4,    4,
332d4 20 20 20 20 20 20 20 34 2c 20 20 20 34 2c 20 20         4,   4,  
332d5 7d 2c 0a 20 20 20 20 20 2f 2a 20 35 20 20 20 20  },.     /* 5    
332d6 53 45 4d 49 3a 20 2a 2f 20 7b 20 20 20 20 35 2c  SEMI: */ {    5,
332d7 20 20 35 2c 20 20 20 20 20 34 2c 20 20 20 20 20    5,     4,     
332d8 20 34 2c 20 20 20 20 20 20 34 2c 20 20 20 20 34   4,      4,    4
332d9 2c 20 20 20 20 20 20 20 34 2c 20 20 20 36 2c 20  ,       4,   6, 
332da 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 36 20 20 20   },.     /* 6   
332db 20 20 45 4e 44 3a 20 2a 2f 20 7b 20 20 20 20 30    END: */ {    0
332dc 2c 20 20 36 2c 20 20 20 20 20 34 2c 20 20 20 20  ,  6,     4,    
332dd 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20 20 20    4,      4,    
332de 34 2c 20 20 20 20 20 20 20 34 2c 20 20 20 34 2c  4,       4,   4,
332df 20 20 7d 2c 0a 20 20 7d 3b 0a 23 65 6c 73 65 0a    },.  };.#else.
332e0 20 20 2f 2a 20 49 66 20 74 72 69 67 67 65 72 73    /* If triggers
332e1 20 61 72 65 20 6e 6f 74 20 73 75 70 70 6f 72 65   are not suppore
332e2 64 20 62 79 20 74 68 69 73 20 63 6f 6d 70 69 6c  d by this compil
332e3 65 20 74 68 65 6e 20 74 68 65 20 73 74 61 74 65  e then the state
332e4 6d 65 6e 74 20 6d 61 63 68 69 6e 65 0a 20 20 2a  ment machine.  *
332e5 2a 20 75 73 65 64 20 74 6f 20 64 65 74 65 63 74  * used to detect
332e6 20 74 68 65 20 65 6e 64 20 6f 66 20 61 20 73 74   the end of a st
332e7 61 74 65 6d 65 6e 74 20 69 73 20 6d 75 63 68 20  atement is much 
332e8 73 69 6d 70 6c 69 65 72 0a 20 20 2a 2f 0a 20 20  simplier.  */.  
332e9 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38 20  static const u8 
332ea 74 72 61 6e 73 5b 32 5d 5b 33 5d 20 3d 20 7b 0a  trans[2][3] = {.
332eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
332ec 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 3a 20 20       /* Token:  
332ed 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
332ee 20 2f 2a 20 53 74 61 74 65 3a 20 20 20 20 20 20   /* State:      
332ef 20 2a 2a 20 20 53 45 4d 49 20 20 57 53 20 20 4f   **  SEMI  WS  O
332f0 54 48 45 52 20 2a 2f 0a 20 20 20 20 20 2f 2a 20  THER */.     /* 
332f1 30 20 20 20 53 54 41 52 54 3a 20 2a 2f 20 7b 20  0   START: */ { 
332f2 20 20 20 30 2c 20 20 30 2c 20 20 20 20 20 31 2c     0,  0,     1,
332f3 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 31 20 20 4e   },.     /* 1  N
332f4 4f 52 4d 41 4c 3a 20 2a 2f 20 7b 20 20 20 20 30  ORMAL: */ {    0
332f5 2c 20 20 31 2c 20 20 20 20 20 31 2c 20 7d 2c 0a  ,  1,     1, },.
332f6 20 20 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53    };.#endif /* S
332f7 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
332f8 45 52 20 2a 2f 0a 0a 20 20 77 68 69 6c 65 28 20  ER */..  while( 
332f9 2a 7a 53 71 6c 20 29 7b 0a 20 20 20 20 73 77 69  *zSql ){.    swi
332fa 74 63 68 28 20 2a 7a 53 71 6c 20 29 7b 0a 20 20  tch( *zSql ){.  
332fb 20 20 20 20 63 61 73 65 20 27 3b 27 3a 20 7b 20      case ';': { 
332fc 20 2f 2a 20 41 20 73 65 6d 69 63 6f 6c 6f 6e 20   /* A semicolon 
332fd 2a 2f 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e  */.        token
332fe 20 3d 20 74 6b 53 45 4d 49 3b 0a 20 20 20 20 20   = tkSEMI;.     
332ff 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
33300 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27 20 27  }.      case ' '
33301 3a 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c 72  :.      case '\r
33302 27 3a 0a 20 20 20 20 20 20 63 61 73 65 20 27 5c  ':.      case '\
33303 74 27 3a 0a 20 20 20 20 20 20 63 61 73 65 20 27  t':.      case '
33304 5c 6e 27 3a 0a 20 20 20 20 20 20 63 61 73 65 20  \n':.      case 
33305 27 5c 66 27 3a 20 7b 20 20 2f 2a 20 57 68 69 74  '\f': {  /* Whit
33306 65 20 73 70 61 63 65 20 69 73 20 69 67 6e 6f 72  e space is ignor
33307 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 74 6f  ed */.        to
33308 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a 20 20 20 20  ken = tkWS;.    
33309 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3330a 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 27 2f   }.      case '/
3330b 27 3a 20 7b 20 20 20 2f 2a 20 43 2d 73 74 79 6c  ': {   /* C-styl
3330c 65 20 63 6f 6d 6d 65 6e 74 73 20 2a 2f 0a 20 20  e comments */.  
3330d 20 20 20 20 20 20 69 66 28 20 7a 53 71 6c 5b 31        if( zSql[1
3330e 5d 21 3d 27 2a 27 20 29 7b 0a 20 20 20 20 20 20  ]!='*' ){.      
3330f 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54      token = tkOT
33310 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20 62  HER;.          b
33311 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
33312 20 20 20 20 20 20 20 20 7a 53 71 6c 20 2b 3d 20          zSql += 
33313 32 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65  2;.        while
33314 28 20 7a 53 71 6c 5b 30 5d 20 26 26 20 28 7a 53  ( zSql[0] && (zS
33315 71 6c 5b 30 5d 21 3d 27 2a 27 20 7c 7c 20 7a 53  ql[0]!='*' || zS
33316 71 6c 5b 31 5d 21 3d 27 2f 27 29 20 29 7b 20 7a  ql[1]!='/') ){ z
33317 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20  Sql++; }.       
33318 20 69 66 28 20 7a 53 71 6c 5b 30 5d 3d 3d 30 20   if( zSql[0]==0 
33319 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
3331a 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20      zSql++;.    
3331b 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 57 53      token = tkWS
3331c 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
3331d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63  .      }.      c
3331e 61 73 65 20 27 2d 27 3a 20 7b 20 20 20 2f 2a 20  ase '-': {   /* 
3331f 53 51 4c 2d 73 74 79 6c 65 20 63 6f 6d 6d 65 6e  SQL-style commen
33320 74 73 20 66 72 6f 6d 20 22 2d 2d 22 20 74 6f 20  ts from "--" to 
33321 65 6e 64 20 6f 66 20 6c 69 6e 65 20 2a 2f 0a 20  end of line */. 
33322 20 20 20 20 20 20 20 69 66 28 20 7a 53 71 6c 5b         if( zSql[
33323 31 5d 21 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20  1]!='-' ){.     
33324 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f       token = tkO
33325 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20  THER;.          
33326 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
33327 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
33328 2a 7a 53 71 6c 20 26 26 20 2a 7a 53 71 6c 21 3d  *zSql && *zSql!=
33329 27 5c 6e 27 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20  '\n' ){ zSql++; 
3332a 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 2a 7a  }.        if( *z
3332b 53 71 6c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  Sql==0 ) return 
3332c 73 74 61 74 65 3d 3d 30 3b 0a 20 20 20 20 20 20  state==0;.      
3332d 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 57 53 3b 0a    token = tkWS;.
3332e 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3332f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
33330 65 20 27 5b 27 3a 20 7b 20 20 20 2f 2a 20 4d 69  e '[': {   /* Mi
33331 63 72 6f 73 6f 66 74 2d 73 74 79 6c 65 20 69 64  crosoft-style id
33332 65 6e 74 69 66 69 65 72 73 20 69 6e 20 5b 2e 2e  entifiers in [..
33333 2e 5d 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a 53  .] */.        zS
33334 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 68  ql++;.        wh
33335 69 6c 65 28 20 2a 7a 53 71 6c 20 26 26 20 2a 7a  ile( *zSql && *z
33336 53 71 6c 21 3d 27 5d 27 20 29 7b 20 7a 53 71 6c  Sql!=']' ){ zSql
33337 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66  ++; }.        if
33338 28 20 2a 7a 53 71 6c 3d 3d 30 20 29 20 72 65 74  ( *zSql==0 ) ret
33339 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 74  urn 0;.        t
3333a 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a  oken = tkOTHER;.
3333b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
3333c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
3333d 65 20 27 60 27 3a 20 20 20 20 20 2f 2a 20 47 72  e '`':     /* Gr
3333e 61 76 65 2d 61 63 63 65 6e 74 20 71 75 6f 74 65  ave-accent quote
3333f 64 20 73 79 6d 62 6f 6c 73 20 75 73 65 64 20 62  d symbols used b
33340 79 20 4d 79 53 51 4c 20 2a 2f 0a 20 20 20 20 20  y MySQL */.     
33341 20 63 61 73 65 20 27 22 27 3a 20 20 20 20 20 2f   case '"':     /
33342 2a 20 73 69 6e 67 6c 65 2d 20 61 6e 64 20 64 6f  * single- and do
33343 75 62 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69  uble-quoted stri
33344 6e 67 73 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ngs */.      cas
33345 65 20 27 5c 27 27 3a 20 7b 0a 20 20 20 20 20 20  e '\'': {.      
33346 20 20 69 6e 74 20 63 20 3d 20 2a 7a 53 71 6c 3b    int c = *zSql;
33347 0a 20 20 20 20 20 20 20 20 7a 53 71 6c 2b 2b 3b  .        zSql++;
33348 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20  .        while( 
33349 2a 7a 53 71 6c 20 26 26 20 2a 7a 53 71 6c 21 3d  *zSql && *zSql!=
3334a 63 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20  c ){ zSql++; }. 
3334b 20 20 20 20 20 20 20 69 66 28 20 2a 7a 53 71 6c         if( *zSql
3334c 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
3334d 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
3334e 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20  tkOTHER;.       
3334f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
33350 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b        default: {
33351 0a 20 20 20 20 20 20 20 20 69 6e 74 20 63 3b 0a  .        int c;.
33352 20 20 20 20 20 20 20 20 69 66 28 20 49 64 43 68          if( IdCh
33353 61 72 28 28 75 38 29 2a 7a 53 71 6c 29 20 29 7b  ar((u8)*zSql) ){
33354 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65  .          /* Ke
33355 79 77 6f 72 64 73 20 61 6e 64 20 75 6e 71 75 6f  ywords and unquo
33356 74 65 64 20 69 64 65 6e 74 69 66 69 65 72 73 20  ted identifiers 
33357 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  */.          int
33358 20 6e 49 64 3b 0a 20 20 20 20 20 20 20 20 20 20   nId;.          
33359 66 6f 72 28 6e 49 64 3d 31 3b 20 49 64 43 68 61  for(nId=1; IdCha
3335a 72 28 7a 53 71 6c 5b 6e 49 64 5d 29 3b 20 6e 49  r(zSql[nId]); nI
3335b 64 2b 2b 29 7b 7d 0a 23 69 66 64 65 66 20 53 51  d++){}.#ifdef SQ
3335c 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
3335d 52 0a 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65  R.          toke
3335e 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 23 65 6c  n = tkOTHER;.#el
3335f 73 65 0a 20 20 20 20 20 20 20 20 20 20 73 77 69  se.          swi
33360 74 63 68 28 20 2a 7a 53 71 6c 20 29 7b 0a 20 20  tch( *zSql ){.  
33361 20 20 20 20 20 20 20 20 20 20 63 61 73 65 20 27            case '
33362 63 27 3a 20 63 61 73 65 20 27 43 27 3a 20 7b 0a  c': case 'C': {.
33363 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
33364 28 20 6e 49 64 3d 3d 36 20 26 26 20 73 71 6c 69  ( nId==6 && sqli
33365 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c  te3StrNICmp(zSql
33366 2c 20 22 63 72 65 61 74 65 22 2c 20 36 29 3d 3d  , "create", 6)==
33367 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
33368 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 43       token = tkC
33369 52 45 41 54 45 3b 0a 20 20 20 20 20 20 20 20 20  REATE;.         
3336a 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
3336b 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65              toke
3336c 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20  n = tkOTHER;.   
3336d 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
3336e 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
3336f 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
33370 20 20 20 20 20 20 20 20 20 20 20 20 63 61 73 65              case
33371 20 27 74 27 3a 20 63 61 73 65 20 27 54 27 3a 20   't': case 'T': 
33372 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
33373 69 66 28 20 6e 49 64 3d 3d 37 20 26 26 20 73 71  if( nId==7 && sq
33374 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53  lite3StrNICmp(zS
33375 71 6c 2c 20 22 74 72 69 67 67 65 72 22 2c 20 37  ql, "trigger", 7
33376 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
33377 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
33378 74 6b 54 52 49 47 47 45 52 3b 0a 20 20 20 20 20  tkTRIGGER;.     
33379 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69           }else i
3337a 66 28 20 6e 49 64 3d 3d 34 20 26 26 20 73 71 6c  f( nId==4 && sql
3337b 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53 71  ite3StrNICmp(zSq
3337c 6c 2c 20 22 74 65 6d 70 22 2c 20 34 29 3d 3d 30  l, "temp", 4)==0
3337d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
3337e 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 54 45      token = tkTE
3337f 4d 50 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  MP;.            
33380 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 49 64 3d    }else if( nId=
33381 3d 39 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  =9 && sqlite3Str
33382 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 74 65 6d  NICmp(zSql, "tem
33383 70 6f 72 61 72 79 22 2c 20 39 29 3d 3d 30 20 29  porary", 9)==0 )
33384 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
33385 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 54 45 4d 50    token = tkTEMP
33386 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
33387 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
33388 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
33389 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20  kOTHER;.        
3338a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
3338b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3338c 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
3338d 20 20 20 20 20 20 20 63 61 73 65 20 27 65 27 3a         case 'e':
3338e 20 20 63 61 73 65 20 27 45 27 3a 20 7b 0a 20 20    case 'E': {.  
3338f 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
33390 6e 49 64 3d 3d 33 20 26 26 20 73 71 6c 69 74 65  nId==3 && sqlite
33391 33 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c 20  3StrNICmp(zSql, 
33392 22 65 6e 64 22 2c 20 33 29 3d 3d 30 20 29 7b 0a  "end", 3)==0 ){.
33393 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33394 74 6f 6b 65 6e 20 3d 20 74 6b 45 4e 44 3b 0a 20  token = tkEND;. 
33395 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c               }el
33396 73 65 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  se.#ifndef SQLIT
33397 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20  E_OMIT_EXPLAIN. 
33398 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28               if(
33399 20 6e 49 64 3d 3d 37 20 26 26 20 73 71 6c 69 74   nId==7 && sqlit
3339a 65 33 53 74 72 4e 49 43 6d 70 28 7a 53 71 6c 2c  e3StrNICmp(zSql,
3339b 20 22 65 78 70 6c 61 69 6e 22 2c 20 37 29 3d 3d   "explain", 7)==
3339c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
3339d 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 45       token = tkE
3339e 58 50 4c 41 49 4e 3b 0a 20 20 20 20 20 20 20 20  XPLAIN;.        
3339f 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64        }else.#end
333a0 69 66 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  if.             
333a1 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   {.             
333a2 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48     token = tkOTH
333a3 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ER;.            
333a4 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
333a5 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
333a6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
333a7 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
333a8 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65              toke
333a9 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20  n = tkOTHER;.   
333aa 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
333ab 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a  ;.            }.
333ac 20 20 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64            }.#end
333ad 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
333ae 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a 20 20 20  T_TRIGGER */.   
333af 20 20 20 20 20 20 20 7a 53 71 6c 20 2b 3d 20 6e         zSql += n
333b0 49 64 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 65  Id-1;.        }e
333b1 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  lse{.          /
333b2 2a 20 4f 70 65 72 61 74 6f 72 73 20 61 6e 64 20  * Operators and 
333b3 73 70 65 63 69 61 6c 20 73 79 6d 62 6f 6c 73 20  special symbols 
333b4 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 74 6f 6b  */.          tok
333b5 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20  en = tkOTHER;.  
333b6 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
333b7 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
333b8 20 20 20 7d 0a 20 20 20 20 73 74 61 74 65 20 3d     }.    state =
333b9 20 74 72 61 6e 73 5b 73 74 61 74 65 5d 5b 74 6f   trans[state][to
333ba 6b 65 6e 5d 3b 0a 20 20 20 20 7a 53 71 6c 2b 2b  ken];.    zSql++
333bb 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73  ;.  }.  return s
333bc 74 61 74 65 3d 3d 30 3b 0a 7d 0a 0a 23 69 66 6e  tate==0;.}..#ifn
333bd 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
333be 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 54 68 69 73  UTF16./*.** This
333bf 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20   routine is the 
333c0 73 61 6d 65 20 61 73 20 74 68 65 20 73 71 6c 69  same as the sqli
333c1 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 20 72  te3_complete() r
333c2 6f 75 74 69 6e 65 20 64 65 73 63 72 69 62 65 64  outine described
333c3 0a 2a 2a 20 61 62 6f 76 65 2c 20 65 78 63 65 70  .** above, excep
333c4 74 20 74 68 61 74 20 74 68 65 20 70 61 72 61 6d  t that the param
333c5 65 74 65 72 20 69 73 20 72 65 71 75 69 72 65 64  eter is required
333c6 20 74 6f 20 62 65 20 55 54 46 2d 31 36 20 65 6e   to be UTF-16 en
333c7 63 6f 64 65 64 2c 20 6e 6f 74 0a 2a 2a 20 55 54  coded, not.** UT
333c8 46 2d 38 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  F-8..*/.SQLITE_A
333c9 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
333ca 6f 6d 70 6c 65 74 65 31 36 28 63 6f 6e 73 74 20  omplete16(const 
333cb 76 6f 69 64 20 2a 7a 53 71 6c 29 7b 0a 20 20 73  void *zSql){.  s
333cc 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
333cd 61 6c 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74  al;.  char const
333ce 20 2a 7a 53 71 6c 38 3b 0a 20 20 69 6e 74 20 72   *zSql8;.  int r
333cf 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
333d0 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
333d1 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a  E_OMIT_AUTOINIT.
333d2 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69    rc = sqlite3_i
333d3 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69  nitialize();.  i
333d4 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
333d5 63 3b 0a 23 65 6e 64 69 66 0a 20 20 70 56 61 6c  c;.#endif.  pVal
333d6 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e   = sqlite3ValueN
333d7 65 77 28 30 29 3b 0a 20 20 73 71 6c 69 74 65 33  ew(0);.  sqlite3
333d8 56 61 6c 75 65 53 65 74 53 74 72 28 70 56 61 6c  ValueSetStr(pVal
333d9 2c 20 2d 31 2c 20 7a 53 71 6c 2c 20 53 51 4c 49  , -1, zSql, SQLI
333da 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20  TE_UTF16NATIVE, 
333db 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
333dc 20 20 7a 53 71 6c 38 20 3d 20 73 71 6c 69 74 65    zSql8 = sqlite
333dd 33 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c  3ValueText(pVal,
333de 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20   SQLITE_UTF8);. 
333df 20 69 66 28 20 7a 53 71 6c 38 20 29 7b 0a 20 20   if( zSql8 ){.  
333e0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63    rc = sqlite3_c
333e1 6f 6d 70 6c 65 74 65 28 7a 53 71 6c 38 29 3b 0a  omplete(zSql8);.
333e2 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
333e3 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
333e4 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 61 6c    }.  sqlite3Val
333e5 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20  ueFree(pVal);.  
333e6 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 70  return sqlite3Ap
333e7 69 45 78 69 74 28 30 2c 20 72 63 29 3b 0a 7d 0a  iExit(0, rc);.}.
333e8 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
333e9 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 23  _OMIT_UTF16 */.#
333ea 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
333eb 4f 4d 49 54 5f 43 4f 4d 50 4c 45 54 45 20 2a 2f  OMIT_COMPLETE */
333ec 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
333ed 2a 20 45 6e 64 20 6f 66 20 63 6f 6d 70 6c 65 74  * End of complet
333ee 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
333ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333f1 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
333f2 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 61 69  * Begin file mai
333f3 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
333f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
333f6 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
333f7 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
333f8 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
333f9 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
333fa 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
333fb 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
333fc 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
333fd 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
333fe 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
333ff 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
33400 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
33401 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
33402 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
33403 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
33404 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
33405 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
33406 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
33407 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
33408 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
33409 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3340a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3340b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3340c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3340d 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 4d 61 69 6e  ********.** Main
3340e 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 53 51   file for the SQ
3340f 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54  Lite library.  T
33410 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74  he routines in t
33411 68 69 73 20 66 69 6c 65 0a 2a 2a 20 69 6d 70 6c  his file.** impl
33412 65 6d 65 6e 74 20 74 68 65 20 70 72 6f 67 72 61  ement the progra
33413 6d 6d 65 72 20 69 6e 74 65 72 66 61 63 65 20 74  mmer interface t
33414 6f 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 20  o the library.  
33415 52 6f 75 74 69 6e 65 73 20 69 6e 0a 2a 2a 20 6f  Routines in.** o
33416 74 68 65 72 20 66 69 6c 65 73 20 61 72 65 20 66  ther files are f
33417 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20  or internal use 
33418 62 79 20 53 51 4c 69 74 65 20 61 6e 64 20 73 68  by SQLite and sh
33419 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 61  ould not be.** a
3341a 63 63 65 73 73 65 64 20 62 79 20 75 73 65 72 73  ccessed by users
3341b 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e   of the library.
3341c 0a 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  .*/..#ifdef SQLI
3341d 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 0a 2f  TE_ENABLE_FTS3./
3341e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
3341f 6e 63 6c 75 64 65 20 66 74 73 33 2e 68 20 69 6e  nclude fts3.h in
33420 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6d   the middle of m
33421 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ain.c **********
33422 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
33423 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
33424 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 2e 68  egin file fts3.h
33425 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
33426 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33427 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
33428 2a 0a 2a 2a 20 32 30 30 36 20 4f 63 74 20 31 30  *.** 2006 Oct 10
33429 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
3342a 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
3342b 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
3342c 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
3342d 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
3342e 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
3342f 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
33430 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
33431 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
33432 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
33433 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
33434 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
33435 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
33436 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
33437 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
33438 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
33439 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
3343a 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
3343b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3343c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3343d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3343e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3343f 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
33440 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 20 75  header file is u
33441 73 65 64 20 62 79 20 70 72 6f 67 72 61 6d 73 20  sed by programs 
33442 74 68 61 74 20 77 61 6e 74 20 74 6f 20 6c 69 6e  that want to lin
33443 6b 20 61 67 61 69 6e 73 74 20 74 68 65 0a 2a 2a  k against the.**
33444 20 46 54 53 33 20 6c 69 62 72 61 72 79 2e 20 20   FTS3 library.  
33445 41 6c 6c 20 69 74 20 64 6f 65 73 20 69 73 20 64  All it does is d
33446 65 63 6c 61 72 65 20 74 68 65 20 73 71 6c 69 74  eclare the sqlit
33447 65 33 46 74 73 33 49 6e 69 74 28 29 20 69 6e 74  e3Fts3Init() int
33448 65 72 66 61 63 65 2e 0a 2a 2f 0a 0a 23 69 66 20  erface..*/..#if 
33449 30 0a 65 78 74 65 72 6e 20 22 43 22 20 7b 0a 23  0.extern "C" {.#
3344a 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75  endif  /* __cplu
3344b 73 70 6c 75 73 20 2a 2f 0a 0a 53 51 4c 49 54 45  splus */..SQLITE
3344c 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
3344d 69 74 65 33 46 74 73 33 49 6e 69 74 28 73 71 6c  ite3Fts3Init(sql
3344e 69 74 65 33 20 2a 64 62 29 3b 0a 0a 23 69 66 20  ite3 *db);..#if 
3344f 30 0a 7d 20 20 2f 2a 20 65 78 74 65 72 6e 20 22  0.}  /* extern "
33450 43 22 20 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a  C" */.#endif  /*
33451 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0a   __cplusplus */.
33452 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
33453 20 45 6e 64 20 6f 66 20 66 74 73 33 2e 68 20 2a   End of fts3.h *
33454 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33455 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33456 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
33457 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
33458 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72   Continuing wher
33459 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e  e we left off in
3345a 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   main.c ********
3345b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
3345c 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
3345d 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 52 54 52  QLITE_ENABLE_RTR
3345e 45 45 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  EE./************
3345f 2a 2a 20 49 6e 63 6c 75 64 65 20 72 74 72 65 65  ** Include rtree
33460 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
33461 20 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a   of main.c *****
33462 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33463 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
33464 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 72 74  ** Begin file rt
33465 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ree.h **********
33466 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33467 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33468 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61  */./*.** 2008 Ma
33469 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  y 26.**.** The a
3346a 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
3346b 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
3346c 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
3346d 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
3346e 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
3346f 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
33470 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
33471 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
33472 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
33473 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
33474 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
33475 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
33476 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
33477 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
33478 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
33479 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
3347a 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
3347b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3347c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3347d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3347e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3347f 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
33480 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20  his header file 
33481 69 73 20 75 73 65 64 20 62 79 20 70 72 6f 67 72  is used by progr
33482 61 6d 73 20 74 68 61 74 20 77 61 6e 74 20 74 6f  ams that want to
33483 20 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68   link against th
33484 65 0a 2a 2a 20 52 54 52 45 45 20 6c 69 62 72 61  e.** RTREE libra
33485 72 79 2e 20 20 41 6c 6c 20 69 74 20 64 6f 65 73  ry.  All it does
33486 20 69 73 20 64 65 63 6c 61 72 65 20 74 68 65 20   is declare the 
33487 73 71 6c 69 74 65 33 52 74 72 65 65 49 6e 69 74  sqlite3RtreeInit
33488 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f  () interface..*/
33489 0a 0a 23 69 66 20 30 0a 65 78 74 65 72 6e 20 22  ..#if 0.extern "
3348a 43 22 20 7b 0a 23 65 6e 64 69 66 20 20 2f 2a 20  C" {.#endif  /* 
3348b 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a  __cplusplus */..
3348c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
3348d 6e 74 20 73 71 6c 69 74 65 33 52 74 72 65 65 49  nt sqlite3RtreeI
3348e 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29  nit(sqlite3 *db)
3348f 3b 0a 0a 23 69 66 20 30 0a 7d 20 20 2f 2a 20 65  ;..#if 0.}  /* e
33490 78 74 65 72 6e 20 22 43 22 20 2a 2f 0a 23 65 6e  xtern "C" */.#en
33491 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70  dif  /* __cplusp
33492 6c 75 73 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  lus */../*******
33493 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 72  ******* End of r
33494 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  tree.h *********
33495 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33496 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
33497 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
33498 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
33499 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
3349a 20 6f 66 66 20 69 6e 20 6d 61 69 6e 2e 63 20 2a   off in main.c *
3349b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3349c 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66 0a 23  ******/.#endif.#
3349d 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
3349e 42 4c 45 5f 49 43 55 0a 2f 2a 2a 2a 2a 2a 2a 2a  BLE_ICU./*******
3349f 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
334a0 73 71 6c 69 74 65 69 63 75 2e 68 20 69 6e 20 74  sqliteicu.h in t
334a1 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 6d 61 69  he middle of mai
334a2 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
334a3 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
334a4 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
334a5 6c 65 20 73 71 6c 69 74 65 69 63 75 2e 68 20 2a  le sqliteicu.h *
334a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334a8 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
334a9 30 38 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20  08 May 26.**.** 
334aa 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
334ab 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
334ac 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
334ad 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
334ae 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
334af 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
334b0 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
334b1 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
334b2 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
334b3 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
334b4 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
334b5 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
334b6 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
334b7 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
334b8 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
334b9 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
334ba 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
334bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
334c0 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20  .** This header 
334c1 66 69 6c 65 20 69 73 20 75 73 65 64 20 62 79 20  file is used by 
334c2 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20 77 61  programs that wa
334c3 6e 74 20 74 6f 20 6c 69 6e 6b 20 61 67 61 69 6e  nt to link again
334c4 73 74 20 74 68 65 0a 2a 2a 20 49 43 55 20 65 78  st the.** ICU ex
334c5 74 65 6e 73 69 6f 6e 2e 20 20 41 6c 6c 20 69 74  tension.  All it
334c6 20 64 6f 65 73 20 69 73 20 64 65 63 6c 61 72 65   does is declare
334c7 20 74 68 65 20 73 71 6c 69 74 65 33 49 63 75 49   the sqlite3IcuI
334c8 6e 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 2e  nit() interface.
334c9 0a 2a 2f 0a 0a 23 69 66 20 30 0a 65 78 74 65 72  .*/..#if 0.exter
334ca 6e 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 20 20  n "C" {.#endif  
334cb 2f 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a  /* __cplusplus *
334cc 2f 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  /..SQLITE_PRIVAT
334cd 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 63 75  E int sqlite3Icu
334ce 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62  Init(sqlite3 *db
334cf 29 3b 0a 0a 23 69 66 20 30 0a 7d 20 20 2f 2a 20  );..#if 0.}  /* 
334d0 65 78 74 65 72 6e 20 22 43 22 20 2a 2f 0a 23 65  extern "C" */.#e
334d1 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73  ndif  /* __cplus
334d2 70 6c 75 73 20 2a 2f 0a 0a 0a 2f 2a 2a 2a 2a 2a  plus */.../*****
334d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
334d4 20 73 71 6c 69 74 65 69 63 75 2e 68 20 2a 2a 2a   sqliteicu.h ***
334d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
334d7 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
334d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
334d9 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
334da 66 74 20 6f 66 66 20 69 6e 20 6d 61 69 6e 2e 63  ft off in main.c
334db 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
334dc 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66  ********/.#endif
334dd 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 76 65 72 73  ../*.** The vers
334de 69 6f 6e 20 6f 66 20 74 68 65 20 6c 69 62 72 61  ion of the libra
334df 72 79 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  ry.*/.#ifndef SQ
334e0 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f  LITE_AMALGAMATIO
334e1 4e 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  N.SQLITE_API con
334e2 73 74 20 63 68 61 72 20 73 71 6c 69 74 65 33 5f  st char sqlite3_
334e3 76 65 72 73 69 6f 6e 5b 5d 20 3d 20 53 51 4c 49  version[] = SQLI
334e4 54 45 5f 56 45 52 53 49 4f 4e 3b 0a 23 65 6e 64  TE_VERSION;.#end
334e5 69 66 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  if.SQLITE_API co
334e6 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
334e7 33 5f 6c 69 62 76 65 72 73 69 6f 6e 28 76 6f 69  3_libversion(voi
334e8 64 29 7b 20 72 65 74 75 72 6e 20 73 71 6c 69 74  d){ return sqlit
334e9 65 33 5f 76 65 72 73 69 6f 6e 3b 20 7d 0a 53 51  e3_version; }.SQ
334ea 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63  LITE_API const c
334eb 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 6f 75  har *sqlite3_sou
334ec 72 63 65 69 64 28 76 6f 69 64 29 7b 20 72 65 74  rceid(void){ ret
334ed 75 72 6e 20 53 51 4c 49 54 45 5f 53 4f 55 52 43  urn SQLITE_SOURC
334ee 45 5f 49 44 3b 20 7d 0a 53 51 4c 49 54 45 5f 41  E_ID; }.SQLITE_A
334ef 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6c  PI int sqlite3_l
334f0 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72  ibversion_number
334f1 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53  (void){ return S
334f2 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55  QLITE_VERSION_NU
334f3 4d 42 45 52 3b 20 7d 0a 53 51 4c 49 54 45 5f 41  MBER; }.SQLITE_A
334f4 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74  PI int sqlite3_t
334f5 68 72 65 61 64 73 61 66 65 28 76 6f 69 64 29 7b  hreadsafe(void){
334f6 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 54   return SQLITE_T
334f7 48 52 45 41 44 53 41 46 45 3b 20 7d 0a 0a 23 69  HREADSAFE; }..#i
334f8 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  f !defined(SQLIT
334f9 45 5f 4f 4d 49 54 5f 54 52 41 43 45 29 20 26 26  E_OMIT_TRACE) &&
334fa 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
334fb 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 29 0a  ENABLE_IOTRACE).
334fc 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c  /*.** If the fol
334fd 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  lowing function 
334fe 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74 20 4e  pointer is not N
334ff 55 4c 4c 20 61 6e 64 20 69 66 0a 2a 2a 20 53 51  ULL and if.** SQ
33500 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52  LITE_ENABLE_IOTR
33501 41 43 45 20 69 73 20 65 6e 61 62 6c 65 64 2c 20  ACE is enabled, 
33502 74 68 65 6e 20 6d 65 73 73 61 67 65 73 20 64 65  then messages de
33503 73 63 72 69 62 69 6e 67 0a 2a 2a 20 49 2f 4f 20  scribing.** I/O 
33504 61 63 74 69 76 65 20 61 72 65 20 77 72 69 74 74  active are writt
33505 65 6e 20 75 73 69 6e 67 20 74 68 69 73 20 66 75  en using this fu
33506 6e 63 74 69 6f 6e 2e 20 20 54 68 65 73 65 20 6d  nction.  These m
33507 65 73 73 61 67 65 73 0a 2a 2a 20 61 72 65 20 69  essages.** are i
33508 6e 74 65 6e 64 65 64 20 66 6f 72 20 64 65 62 75  ntended for debu
33509 67 67 69 6e 67 20 61 63 74 69 76 69 74 79 20 6f  gging activity o
3350a 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nly..*/.SQLITE_P
3350b 52 49 56 41 54 45 20 76 6f 69 64 20 28 2a 73 71  RIVATE void (*sq
3350c 6c 69 74 65 33 49 6f 54 72 61 63 65 29 28 63 6f  lite3IoTrace)(co
3350d 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 20  nst char*, ...) 
3350e 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  = 0;.#endif../*.
3350f 2a 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ** If the follow
33510 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  ing global varia
33511 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  ble points to a 
33512 73 74 72 69 6e 67 20 77 68 69 63 68 20 69 73 20  string which is 
33513 74 68 65 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 61  the.** name of a
33514 20 64 69 72 65 63 74 6f 72 79 2c 20 74 68 65 6e   directory, then
33515 20 74 68 61 74 20 64 69 72 65 63 74 6f 72 79 20   that directory 
33516 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74 6f 20  will be used to 
33517 73 74 6f 72 65 0a 2a 2a 20 74 65 6d 70 6f 72 61  store.** tempora
33518 72 79 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  ry files..**.** 
33519 53 65 65 20 61 6c 73 6f 20 74 68 65 20 22 50 52  See also the "PR
3351a 41 47 4d 41 20 74 65 6d 70 5f 73 74 6f 72 65 5f  AGMA temp_store_
3351b 64 69 72 65 63 74 6f 72 79 22 20 53 51 4c 20 63  directory" SQL c
3351c 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54  ommand..*/.SQLIT
3351d 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69  E_API char *sqli
3351e 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f  te3_temp_directo
3351f 72 79 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 49  ry = 0;../*.** I
33520 6e 69 74 69 61 6c 69 7a 65 20 53 51 4c 69 74 65  nitialize SQLite
33521 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  .  .**.** This r
33522 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65 20 63  outine must be c
33523 61 6c 6c 65 64 20 74 6f 20 69 6e 69 74 69 61 6c  alled to initial
33524 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61  ize the memory a
33525 6c 6c 6f 63 61 74 69 6f 6e 2c 0a 2a 2a 20 56 46  llocation,.** VF
33526 53 2c 20 61 6e 64 20 6d 75 74 65 78 20 73 75 62  S, and mutex sub
33527 73 79 73 74 65 6d 73 20 70 72 69 6f 72 20 74 6f  systems prior to
33528 20 64 6f 69 6e 67 20 61 6e 79 20 73 65 72 69 6f   doing any serio
33529 75 73 20 77 6f 72 6b 20 77 69 74 68 0a 2a 2a 20  us work with.** 
3352a 53 51 4c 69 74 65 2e 20 20 42 75 74 20 61 73 20  SQLite.  But as 
3352b 6c 6f 6e 67 20 61 73 20 79 6f 75 20 64 6f 20 6e  long as you do n
3352c 6f 74 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20  ot compile with 
3352d 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
3352e 49 4e 49 54 0a 2a 2a 20 74 68 69 73 20 72 6f 75  INIT.** this rou
3352f 74 69 6e 65 20 77 69 6c 6c 20 62 65 20 63 61 6c  tine will be cal
33530 6c 65 64 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  led automaticall
33531 79 20 62 79 20 6b 65 79 20 72 6f 75 74 69 6e 65  y by key routine
33532 73 20 73 75 63 68 20 61 73 0a 2a 2a 20 73 71 6c  s such as.** sql
33533 69 74 65 33 5f 6f 70 65 6e 28 29 2e 20 20 0a 2a  ite3_open().  .*
33534 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
33535 65 20 69 73 20 61 20 6e 6f 2d 6f 70 20 65 78 63  e is a no-op exc
33536 65 70 74 20 6f 6e 20 69 74 73 20 76 65 72 79 20  ept on its very 
33537 66 69 72 73 74 20 63 61 6c 6c 20 66 6f 72 20 74  first call for t
33538 68 65 20 70 72 6f 63 65 73 73 2c 0a 2a 2a 20 6f  he process,.** o
33539 72 20 66 6f 72 20 74 68 65 20 66 69 72 73 74 20  r for the first 
3353a 63 61 6c 6c 20 61 66 74 65 72 20 61 20 63 61 6c  call after a cal
3353b 6c 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 68 75  l to sqlite3_shu
3353c 74 64 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tdown..**.** The
3353d 20 66 69 72 73 74 20 74 68 72 65 61 64 20 74 6f   first thread to
3353e 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69   call this routi
3353f 6e 65 20 72 75 6e 73 20 74 68 65 20 69 6e 69 74  ne runs the init
33540 69 61 6c 69 7a 61 74 69 6f 6e 20 74 6f 0a 2a 2a  ialization to.**
33541 20 63 6f 6d 70 6c 65 74 69 6f 6e 2e 20 20 49 66   completion.  If
33542 20 73 75 62 73 65 71 75 65 6e 74 20 74 68 72 65   subsequent thre
33543 61 64 73 20 63 61 6c 6c 20 74 68 69 73 20 72 6f  ads call this ro
33544 75 74 69 6e 65 20 62 65 66 6f 72 65 20 74 68 65  utine before the
33545 20 66 69 72 73 74 0a 2a 2a 20 74 68 72 65 61 64   first.** thread
33546 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 74 68   has finished th
33547 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  e initialization
33548 20 70 72 6f 63 65 73 73 2c 20 74 68 65 6e 20 74   process, then t
33549 68 65 20 73 75 62 73 65 71 75 65 6e 74 0a 2a 2a  he subsequent.**
3354a 20 74 68 72 65 61 64 73 20 6d 75 73 74 20 62 6c   threads must bl
3354b 6f 63 6b 20 75 6e 74 69 6c 20 74 68 65 20 66 69  ock until the fi
3354c 72 73 74 20 74 68 72 65 61 64 20 66 69 6e 69 73  rst thread finis
3354d 68 65 73 20 77 69 74 68 20 74 68 65 20 69 6e 69  hes with the ini
3354e 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 0a  tialization..**.
3354f 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 68 72  ** The first thr
33550 65 61 64 20 6d 69 67 68 74 20 63 61 6c 6c 20 74  ead might call t
33551 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 63 75  his routine recu
33552 72 73 69 76 65 6c 79 2e 20 20 52 65 63 75 72 73  rsively.  Recurs
33553 69 76 65 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20  ive.** calls to 
33554 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f  this routine sho
33555 75 6c 64 20 6e 6f 74 20 62 6c 6f 63 6b 2c 20 6f  uld not block, o
33556 66 20 63 6f 75 72 73 65 2e 20 20 4f 74 68 65 72  f course.  Other
33557 77 69 73 65 20 74 68 65 0a 2a 2a 20 69 6e 69 74  wise the.** init
33558 69 61 6c 69 7a 61 74 69 6f 6e 20 70 72 6f 63 65  ialization proce
33559 73 73 20 77 6f 75 6c 64 20 6e 65 76 65 72 20 63  ss would never c
3355a 6f 6d 70 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a 20 4c  omplete..**.** L
3355b 65 74 20 58 20 62 65 20 74 68 65 20 66 69 72 73  et X be the firs
3355c 74 20 74 68 72 65 61 64 20 74 6f 20 65 6e 74 65  t thread to ente
3355d 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20  r this routine. 
3355e 20 4c 65 74 20 59 20 62 65 20 73 6f 6d 65 20 6f   Let Y be some o
3355f 74 68 65 72 0a 2a 2a 20 74 68 72 65 61 64 2e 20  ther.** thread. 
33560 20 54 68 65 6e 20 77 68 69 6c 65 20 74 68 65 20   Then while the 
33561 69 6e 69 74 69 61 6c 20 69 6e 76 6f 63 61 74 69  initial invocati
33562 6f 6e 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69  on of this routi
33563 6e 65 20 62 79 20 58 20 69 73 0a 2a 2a 20 69 6e  ne by X is.** in
33564 63 6f 6d 70 6c 65 74 65 2c 20 69 74 20 69 73 20  complete, it is 
33565 72 65 71 75 69 72 65 64 20 74 68 61 74 3a 0a 2a  required that:.*
33566 2a 0a 2a 2a 20 20 20 20 2a 20 20 43 61 6c 6c 73  *.**    *  Calls
33567 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   to this routine
33568 20 66 72 6f 6d 20 59 20 6d 75 73 74 20 62 6c 6f   from Y must blo
33569 63 6b 20 75 6e 74 69 6c 20 74 68 65 20 6f 75 74  ck until the out
3356a 65 72 2d 6d 6f 73 74 0a 2a 2a 20 20 20 20 20 20  er-most.**      
3356b 20 63 61 6c 6c 20 62 79 20 58 20 63 6f 6d 70 6c   call by X compl
3356c 65 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a  etes..**.**    *
3356d 20 20 52 65 63 75 72 73 69 76 65 20 63 61 6c 6c    Recursive call
3356e 73 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  s to this routin
3356f 65 20 66 72 6f 6d 20 74 68 72 65 61 64 20 58 20  e from thread X 
33570 72 65 74 75 72 6e 20 69 6d 6d 65 64 69 61 74 65  return immediate
33571 6c 79 0a 2a 2a 20 20 20 20 20 20 20 77 69 74 68  ly.**       with
33572 6f 75 74 20 62 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f  out blocking..*/
33573 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
33574 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
33575 7a 65 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69  ze(void){.  sqli
33576 74 65 33 5f 6d 75 74 65 78 20 2a 70 4d 61 73 74  te3_mutex *pMast
33577 65 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  er;             
33578 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
33579 6d 61 69 6e 20 73 74 61 74 69 63 20 6d 75 74 65  main static mute
3357a 78 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  x */.  int rc;  
3357b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3357c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3357d 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f      /* Result co
3357e 64 65 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51  de */..#ifdef SQ
3357f 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 20 20  LITE_OMIT_WSD.  
33580 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 77 73 64  rc = sqlite3_wsd
33581 5f 69 6e 69 74 28 34 30 39 36 2c 20 32 34 29 3b  _init(4096, 24);
33582 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
33583 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
33584 72 6e 20 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69  rn rc;.  }.#endi
33585 66 0a 0a 20 20 2f 2a 20 49 66 20 53 51 4c 69 74  f..  /* If SQLit
33586 65 20 69 73 20 61 6c 72 65 61 64 79 20 63 6f 6d  e is already com
33587 70 6c 65 74 65 6c 79 20 69 6e 69 74 69 61 6c 69  pletely initiali
33588 7a 65 64 2c 20 74 68 65 6e 20 74 68 69 73 20 63  zed, then this c
33589 61 6c 6c 0a 20 20 2a 2a 20 74 6f 20 73 71 6c 69  all.  ** to sqli
3358a 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
3358b 20 73 68 6f 75 6c 64 20 62 65 20 61 20 6e 6f 2d   should be a no-
3358c 6f 70 2e 20 20 42 75 74 20 74 68 65 20 69 6e 69  op.  But the ini
3358d 74 69 61 6c 69 7a 61 74 69 6f 6e 0a 20 20 2a 2a  tialization.  **
3358e 20 6d 75 73 74 20 62 65 20 63 6f 6d 70 6c 65 74   must be complet
3358f 65 2e 20 20 53 6f 20 69 73 49 6e 69 74 20 6d 75  e.  So isInit mu
33590 73 74 20 6e 6f 74 20 62 65 20 73 65 74 20 75 6e  st not be set un
33591 74 69 6c 20 74 68 65 20 76 65 72 79 20 65 6e 64  til the very end
33592 0a 20 20 2a 2a 20 6f 66 20 74 68 69 73 20 72 6f  .  ** of this ro
33593 75 74 69 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  utine..  */.  if
33594 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
33595 6f 6e 66 69 67 2e 69 73 49 6e 69 74 20 29 20 72  onfig.isInit ) r
33596 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
33597 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
33598 20 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79   the mutex subsy
33599 73 74 65 6d 20 69 73 20 69 6e 69 74 69 61 6c 69  stem is initiali
3359a 7a 65 64 2e 20 20 49 66 20 75 6e 61 62 6c 65 20  zed.  If unable 
3359b 74 6f 20 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c  to .  ** initial
3359c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20 73 75  ize the mutex su
3359d 62 73 79 73 74 65 6d 2c 20 72 65 74 75 72 6e 20  bsystem, return 
3359e 65 61 72 6c 79 20 77 69 74 68 20 74 68 65 20 65  early with the e
3359f 72 72 6f 72 2e 0a 20 20 2a 2a 20 49 66 20 74 68  rror..  ** If th
335a0 65 20 73 79 73 74 65 6d 20 69 73 20 73 6f 20 73  e system is so s
335a1 69 63 6b 20 74 68 61 74 20 77 65 20 61 72 65 20  ick that we are 
335a2 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61  unable to alloca
335a3 74 65 20 61 20 6d 75 74 65 78 2c 0a 20 20 2a 2a  te a mutex,.  **
335a4 20 74 68 65 72 65 20 69 73 20 6e 6f 74 20 6d 75   there is not mu
335a5 63 68 20 53 51 4c 69 74 65 20 69 73 20 67 6f 69  ch SQLite is goi
335a6 6e 67 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f  ng to be able to
335a7 20 64 6f 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54   do..  **.  ** T
335a8 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74  he mutex subsyst
335a9 65 6d 20 6d 75 73 74 20 74 61 6b 65 20 63 61 72  em must take car
335aa 65 20 6f 66 20 73 65 72 69 61 6c 69 7a 69 6e 67  e of serializing
335ab 20 69 74 73 20 6f 77 6e 0a 20 20 2a 2a 20 69 6e   its own.  ** in
335ac 69 74 69 61 6c 69 7a 61 74 69 6f 6e 2e 0a 20 20  itialization..  
335ad 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  */.  rc = sqlite
335ae 33 4d 75 74 65 78 49 6e 69 74 28 29 3b 0a 20 20  3MutexInit();.  
335af 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
335b0 72 63 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61  rc;..  /* Initia
335b1 6c 69 7a 65 20 74 68 65 20 6d 61 6c 6c 6f 63 28  lize the malloc(
335b2 29 20 73 79 73 74 65 6d 20 61 6e 64 20 74 68 65  ) system and the
335b3 20 72 65 63 75 72 73 69 76 65 20 70 49 6e 69 74   recursive pInit
335b4 4d 75 74 65 78 20 6d 75 74 65 78 2e 0a 20 20 2a  Mutex mutex..  *
335b5 2a 20 54 68 69 73 20 6f 70 65 72 61 74 69 6f 6e  * This operation
335b6 20 69 73 20 70 72 6f 74 65 63 74 65 64 20 62 79   is protected by
335b7 20 74 68 65 20 53 54 41 54 49 43 5f 4d 41 53 54   the STATIC_MAST
335b8 45 52 20 6d 75 74 65 78 2e 20 20 4e 6f 74 65 20  ER mutex.  Note 
335b9 74 68 61 74 0a 20 20 2a 2a 20 4d 75 74 65 78 41  that.  ** MutexA
335ba 6c 6c 6f 63 28 29 20 69 73 20 63 61 6c 6c 65 64  lloc() is called
335bb 20 66 6f 72 20 61 20 73 74 61 74 69 63 20 6d 75   for a static mu
335bc 74 65 78 20 70 72 69 6f 72 20 74 6f 20 69 6e 69  tex prior to ini
335bd 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 0a 20 20  tializing the.  
335be 2a 2a 20 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73  ** malloc subsys
335bf 74 65 6d 20 2d 20 74 68 69 73 20 69 6d 70 6c 69  tem - this impli
335c0 65 73 20 74 68 61 74 20 74 68 65 20 61 6c 6c 6f  es that the allo
335c1 63 61 74 69 6f 6e 20 6f 66 20 61 20 73 74 61 74  cation of a stat
335c2 69 63 0a 20 20 2a 2a 20 6d 75 74 65 78 20 6d 75  ic.  ** mutex mu
335c3 73 74 20 6e 6f 74 20 72 65 71 75 69 72 65 20 73  st not require s
335c4 75 70 70 6f 72 74 20 66 72 6f 6d 20 74 68 65 20  upport from the 
335c5 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73 74 65 6d  malloc subsystem
335c6 2e 0a 20 20 2a 2f 0a 20 20 70 4d 61 73 74 65 72  ..  */.  pMaster
335c7 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
335c8 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
335c9 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
335ca 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
335cb 78 5f 65 6e 74 65 72 28 70 4d 61 73 74 65 72 29  x_enter(pMaster)
335cc 3b 0a 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  ;.  sqlite3Globa
335cd 6c 43 6f 6e 66 69 67 2e 69 73 4d 75 74 65 78 49  lConfig.isMutexI
335ce 6e 69 74 20 3d 20 31 3b 0a 20 20 69 66 28 20 21  nit = 1;.  if( !
335cf 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
335d0 66 69 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69 74  fig.isMallocInit
335d1 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
335d2 69 74 65 33 4d 61 6c 6c 6f 63 49 6e 69 74 28 29  ite3MallocInit()
335d3 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d  ;.  }.  if( rc==
335d4 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
335d5 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
335d6 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69  nfig.isMallocIni
335d7 74 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 21  t = 1;.    if( !
335d8 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
335d9 66 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 20 29  fig.pInitMutex )
335da 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  {.      sqlite3G
335db 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69  lobalConfig.pIni
335dc 74 4d 75 74 65 78 20 3d 0a 20 20 20 20 20 20 20  tMutex =.       
335dd 20 20 20 20 73 71 6c 69 74 65 33 4d 75 74 65 78      sqlite3Mutex
335de 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
335df 45 58 5f 52 45 43 55 52 53 49 56 45 29 3b 0a 20  EX_RECURSIVE);. 
335e0 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
335e1 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f  GlobalConfig.bCo
335e2 72 65 4d 75 74 65 78 20 26 26 20 21 73 71 6c 69  reMutex && !sqli
335e3 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
335e4 70 49 6e 69 74 4d 75 74 65 78 20 29 7b 0a 20 20  pInitMutex ){.  
335e5 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
335e6 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
335e7 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
335e8 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
335e9 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  {.    sqlite3Glo
335ea 62 61 6c 43 6f 6e 66 69 67 2e 6e 52 65 66 49 6e  balConfig.nRefIn
335eb 69 74 4d 75 74 65 78 2b 2b 3b 0a 20 20 7d 0a 20  itMutex++;.  }. 
335ec 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
335ed 65 61 76 65 28 70 4d 61 73 74 65 72 29 3b 0a 0a  eave(pMaster);..
335ee 20 20 2f 2a 20 49 66 20 72 63 20 69 73 20 6e 6f    /* If rc is no
335ef 74 20 53 51 4c 49 54 45 5f 4f 4b 20 61 74 20 74  t SQLITE_OK at t
335f0 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65 6e 20  his point, then 
335f1 65 69 74 68 65 72 20 74 68 65 20 6d 61 6c 6c 6f  either the mallo
335f2 63 0a 20 20 2a 2a 20 73 75 62 73 79 73 74 65 6d  c.  ** subsystem
335f3 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 69 6e   could not be in
335f4 69 74 69 61 6c 69 7a 65 64 20 6f 72 20 74 68 65  itialized or the
335f5 20 73 79 73 74 65 6d 20 66 61 69 6c 65 64 20 74   system failed t
335f6 6f 20 61 6c 6c 6f 63 61 74 65 0a 20 20 2a 2a 20  o allocate.  ** 
335f7 74 68 65 20 70 49 6e 69 74 4d 75 74 65 78 20 6d  the pInitMutex m
335f8 75 74 65 78 2e 20 52 65 74 75 72 6e 20 61 6e 20  utex. Return an 
335f9 65 72 72 6f 72 20 69 6e 20 65 69 74 68 65 72 20  error in either 
335fa 63 61 73 65 2e 20 20 2a 2f 0a 20 20 69 66 28 20  case.  */.  if( 
335fb 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
335fc 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
335fd 20 20 7d 0a 0a 20 20 2f 2a 20 44 6f 20 74 68 65    }..  /* Do the
335fe 20 72 65 73 74 20 6f 66 20 74 68 65 20 69 6e 69   rest of the ini
335ff 74 69 61 6c 69 7a 61 74 69 6f 6e 20 75 6e 64 65  tialization unde
33600 72 20 74 68 65 20 72 65 63 75 72 73 69 76 65 20  r the recursive 
33601 6d 75 74 65 78 20 73 6f 0a 20 20 2a 2a 20 74 68  mutex so.  ** th
33602 61 74 20 77 65 20 77 69 6c 6c 20 62 65 20 61 62  at we will be ab
33603 6c 65 20 74 6f 20 68 61 6e 64 6c 65 20 72 65 63  le to handle rec
33604 75 72 73 69 76 65 20 63 61 6c 6c 73 20 69 6e 74  ursive calls int
33605 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 69  o.  ** sqlite3_i
33606 6e 69 74 69 61 6c 69 7a 65 28 29 2e 20 20 54 68  nitialize().  Th
33607 65 20 72 65 63 75 72 73 69 76 65 20 63 61 6c 6c  e recursive call
33608 73 20 6e 6f 72 6d 61 6c 6c 79 20 63 6f 6d 65 20  s normally come 
33609 74 68 72 6f 75 67 68 0a 20 20 2a 2a 20 73 71 6c  through.  ** sql
3360a 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20 77  ite3_os_init() w
3360b 68 65 6e 20 69 74 20 69 6e 76 6f 6b 65 73 20 73  hen it invokes s
3360c 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73  qlite3_vfs_regis
3360d 74 65 72 28 29 2c 20 62 75 74 20 6f 74 68 65 72  ter(), but other
3360e 0a 20 20 2a 2a 20 72 65 63 75 72 73 69 76 65 20  .  ** recursive 
3360f 63 61 6c 6c 73 20 6d 69 67 68 74 20 61 6c 73 6f  calls might also
33610 20 62 65 20 70 6f 73 73 69 62 6c 65 2e 0a 20 20   be possible..  
33611 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  */.  sqlite3_mut
33612 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33  ex_enter(sqlite3
33613 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e  GlobalConfig.pIn
33614 69 74 4d 75 74 65 78 29 3b 0a 20 20 69 66 28 20  itMutex);.  if( 
33615 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
33616 66 69 67 2e 69 73 49 6e 69 74 3d 3d 30 20 26 26  fig.isInit==0 &&
33617 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
33618 6e 66 69 67 2e 69 6e 50 72 6f 67 72 65 73 73 3d  nfig.inProgress=
33619 3d 30 20 29 7b 0a 20 20 20 20 46 75 6e 63 44 65  =0 ){.    FuncDe
3361a 66 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26  fHash *pHash = &
3361b 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 48 61  GLOBAL(FuncDefHa
3361c 73 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  sh, sqlite3Globa
3361d 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a 20 20 20  lFunctions);.   
3361e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3361f 6e 66 69 67 2e 69 6e 50 72 6f 67 72 65 73 73 20  nfig.inProgress 
33620 3d 20 31 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  = 1;.    memset(
33621 70 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f 66  pHash, 0, sizeof
33622 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75  (sqlite3GlobalFu
33623 6e 63 74 69 6f 6e 73 29 29 3b 0a 20 20 20 20 73  nctions));.    s
33624 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 47 6c  qlite3RegisterGl
33625 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28 29 3b  obalFunctions();
33626 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
33627 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 50  GlobalConfig.isP
33628 43 61 63 68 65 49 6e 69 74 3d 3d 30 20 29 7b 0a  CacheInit==0 ){.
33629 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
3362a 65 33 50 63 61 63 68 65 49 6e 69 74 69 61 6c 69  e3PcacheInitiali
3362b 7a 65 28 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ze();.    }.    
3362c 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3362d 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  K ){.      sqlit
3362e 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69  e3GlobalConfig.i
3362f 73 50 43 61 63 68 65 49 6e 69 74 20 3d 20 31 3b  sPCacheInit = 1;
33630 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
33631 74 65 33 4f 73 49 6e 69 74 28 29 3b 0a 20 20 20  te3OsInit();.   
33632 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
33633 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
33634 20 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 42    sqlite3PCacheB
33635 75 66 66 65 72 53 65 74 75 70 28 20 73 71 6c 69  ufferSetup( sqli
33636 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
33637 70 50 61 67 65 2c 20 0a 20 20 20 20 20 20 20 20  pPage, .        
33638 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
33639 6f 6e 66 69 67 2e 73 7a 50 61 67 65 2c 20 73 71  onfig.szPage, sq
3363a 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
3363b 67 2e 6e 50 61 67 65 29 3b 0a 20 20 20 20 20 20  g.nPage);.      
3363c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3363d 66 69 67 2e 69 73 49 6e 69 74 20 3d 20 31 3b 0a  fig.isInit = 1;.
3363e 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
3363f 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 6e  3GlobalConfig.in
33640 50 72 6f 67 72 65 73 73 20 3d 20 30 3b 0a 20 20  Progress = 0;.  
33641 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
33642 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 47  x_leave(sqlite3G
33643 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69  lobalConfig.pIni
33644 74 4d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 47  tMutex);..  /* G
33645 6f 20 62 61 63 6b 20 75 6e 64 65 72 20 74 68 65  o back under the
33646 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 61 6e   static mutex an
33647 64 20 63 6c 65 61 6e 20 75 70 20 74 68 65 20 72  d clean up the r
33648 65 63 75 72 73 69 76 65 0a 20 20 2a 2a 20 6d 75  ecursive.  ** mu
33649 74 65 78 20 74 6f 20 70 72 65 76 65 6e 74 20 61  tex to prevent a
3364a 20 72 65 73 6f 75 72 63 65 20 6c 65 61 6b 2e 0a   resource leak..
3364b 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d    */.  sqlite3_m
3364c 75 74 65 78 5f 65 6e 74 65 72 28 70 4d 61 73 74  utex_enter(pMast
3364d 65 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 47 6c  er);.  sqlite3Gl
3364e 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 52 65 66 49  obalConfig.nRefI
3364f 6e 69 74 4d 75 74 65 78 2d 2d 3b 0a 20 20 69 66  nitMutex--;.  if
33650 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
33651 6f 6e 66 69 67 2e 6e 52 65 66 49 6e 69 74 4d 75  onfig.nRefInitMu
33652 74 65 78 3c 3d 30 20 29 7b 0a 20 20 20 20 61 73  tex<=0 ){.    as
33653 73 65 72 74 28 20 73 71 6c 69 74 65 33 47 6c 6f  sert( sqlite3Glo
33654 62 61 6c 43 6f 6e 66 69 67 2e 6e 52 65 66 49 6e  balConfig.nRefIn
33655 69 74 4d 75 74 65 78 3d 3d 30 20 29 3b 0a 20 20  itMutex==0 );.  
33656 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
33657 66 72 65 65 28 73 71 6c 69 74 65 33 47 6c 6f 62  free(sqlite3Glob
33658 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75  alConfig.pInitMu
33659 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  tex);.    sqlite
3365a 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49  3GlobalConfig.pI
3365b 6e 69 74 4d 75 74 65 78 20 3d 20 30 3b 0a 20 20  nitMutex = 0;.  
3365c 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
3365d 78 5f 6c 65 61 76 65 28 70 4d 61 73 74 65 72 29  x_leave(pMaster)
3365e 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c  ;..  /* The foll
3365f 6f 77 69 6e 67 20 69 73 20 6a 75 73 74 20 61 20  owing is just a 
33660 73 61 6e 69 74 79 20 63 68 65 63 6b 20 74 6f 20  sanity check to 
33661 6d 61 6b 65 20 73 75 72 65 20 53 51 4c 69 74 65  make sure SQLite
33662 20 68 61 73 0a 20 20 2a 2a 20 62 65 65 6e 20 63   has.  ** been c
33663 6f 6d 70 69 6c 65 64 20 63 6f 72 72 65 63 74 6c  ompiled correctl
33664 79 2e 20 20 49 74 20 69 73 20 69 6d 70 6f 72 74  y.  It is import
33665 61 6e 74 20 74 6f 20 72 75 6e 20 74 68 69 73 20  ant to run this 
33666 63 6f 64 65 2c 20 62 75 74 0a 20 20 2a 2a 20 77  code, but.  ** w
33667 65 20 64 6f 6e 27 74 20 77 61 6e 74 20 74 6f 20  e don't want to 
33668 72 75 6e 20 69 74 20 74 6f 6f 20 6f 66 74 65 6e  run it too often
33669 20 61 6e 64 20 73 6f 61 6b 20 75 70 20 43 50 55   and soak up CPU
3366a 20 63 79 63 6c 65 73 20 66 6f 72 20 6e 6f 0a 20   cycles for no. 
3366b 20 2a 2a 20 72 65 61 73 6f 6e 2e 20 20 53 6f 20   ** reason.  So 
3366c 77 65 20 72 75 6e 20 69 74 20 6f 6e 63 65 20 64  we run it once d
3366d 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61  uring initializa
3366e 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64  tion..  */.#ifnd
3366f 65 66 20 4e 44 45 42 55 47 0a 23 69 66 6e 64 65  ef NDEBUG.#ifnde
33670 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
33671 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 2f  OATING_POINT.  /
33672 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e 20 6f  * This section o
33673 66 20 63 6f 64 65 27 73 20 6f 6e 6c 79 20 22 6f  f code's only "o
33674 75 74 70 75 74 22 20 69 73 20 76 69 61 20 61 73  utput" is via as
33675 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74  sert() statement
33676 73 2e 20 2a 2f 0a 20 20 69 66 20 28 20 72 63 3d  s. */.  if ( rc=
33677 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
33678 20 20 75 36 34 20 78 20 3d 20 28 28 28 75 36 34    u64 x = (((u64
33679 29 31 29 3c 3c 36 33 29 2d 31 3b 0a 20 20 20 20  )1)<<63)-1;.    
3367a 64 6f 75 62 6c 65 20 79 3b 0a 20 20 20 20 61 73  double y;.    as
3367b 73 65 72 74 28 73 69 7a 65 6f 66 28 78 29 3d 3d  sert(sizeof(x)==
3367c 38 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 73  8);.    assert(s
3367d 69 7a 65 6f 66 28 78 29 3d 3d 73 69 7a 65 6f 66  izeof(x)==sizeof
3367e 28 79 29 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  (y));.    memcpy
3367f 28 26 79 2c 20 26 78 2c 20 38 29 3b 0a 20 20 20  (&y, &x, 8);.   
33680 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
33681 49 73 4e 61 4e 28 79 29 20 29 3b 0a 20 20 7d 0a  IsNaN(y) );.  }.
33682 23 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 20  #endif.#endif.. 
33683 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
33684 2a 0a 2a 2a 20 55 6e 64 6f 20 74 68 65 20 65 66  *.** Undo the ef
33685 66 65 63 74 73 20 6f 66 20 73 71 6c 69 74 65 33  fects of sqlite3
33686 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 20 20  _initialize().  
33687 4d 75 73 74 20 6e 6f 74 20 62 65 20 63 61 6c 6c  Must not be call
33688 65 64 20 77 68 69 6c 65 0a 2a 2a 20 74 68 65 72  ed while.** ther
33689 65 20 61 72 65 20 6f 75 74 73 74 61 6e 64 69 6e  e are outstandin
3368a 67 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  g database conne
3368b 63 74 69 6f 6e 73 20 6f 72 20 6d 65 6d 6f 72 79  ctions or memory
3368c 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f 72 0a   allocations or.
3368d 2a 2a 20 77 68 69 6c 65 20 61 6e 79 20 70 61 72  ** while any par
3368e 74 20 6f 66 20 53 51 4c 69 74 65 20 69 73 20 6f  t of SQLite is o
3368f 74 68 65 72 77 69 73 65 20 69 6e 20 75 73 65 20  therwise in use 
33690 69 6e 20 61 6e 79 20 74 68 72 65 61 64 2e 20 20  in any thread.  
33691 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20  This.** routine 
33692 69 73 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66  is not threadsaf
33693 65 2e 20 20 42 75 74 20 69 74 20 69 73 20 73 61  e.  But it is sa
33694 66 65 20 74 6f 20 69 6e 76 6f 6b 65 20 74 68 69  fe to invoke thi
33695 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 6f 6e 20  s routine.** on 
33696 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 61  when SQLite is a
33697 6c 72 65 61 64 79 20 73 68 75 74 20 64 6f 77 6e  lready shut down
33698 2e 20 20 49 66 20 53 51 4c 69 74 65 20 69 73 20  .  If SQLite is 
33699 61 6c 72 65 61 64 79 20 73 68 75 74 20 64 6f 77  already shut dow
3369a 6e 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 72  n.** when this r
3369b 6f 75 74 69 6e 65 20 69 73 20 69 6e 76 6f 6b 65  outine is invoke
3369c 64 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  d, then this rou
3369d 74 69 6e 65 20 69 73 20 61 20 68 61 72 6d 6c 65  tine is a harmle
3369e 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 53 51 4c  ss no-op..*/.SQL
3369f 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
336a0 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 76 6f 69  te3_shutdown(voi
336a1 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  d){.  if( sqlite
336a2 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73  3GlobalConfig.is
336a3 49 6e 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  Init ){.    sqli
336a4 74 65 33 5f 6f 73 5f 65 6e 64 28 29 3b 0a 20 20  te3_os_end();.  
336a5 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 5f    sqlite3_reset_
336a6 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e 28 29  auto_extension()
336a7 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f  ;.    sqlite3Glo
336a8 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74  balConfig.isInit
336a9 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20   = 0;.  }.  if( 
336aa 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
336ab 66 69 67 2e 69 73 50 43 61 63 68 65 49 6e 69 74  fig.isPCacheInit
336ac 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50   ){.    sqlite3P
336ad 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28 29 3b  cacheShutdown();
336ae 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
336af 61 6c 43 6f 6e 66 69 67 2e 69 73 50 43 61 63 68  alConfig.isPCach
336b0 65 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 20  eInit = 0;.  }. 
336b1 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
336b2 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f  alConfig.isMallo
336b3 63 49 6e 69 74 20 29 7b 0a 20 20 20 20 73 71 6c  cInit ){.    sql
336b4 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28 29 3b  ite3MallocEnd();
336b5 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
336b6 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f  alConfig.isMallo
336b7 63 49 6e 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 20  cInit = 0;.  }. 
336b8 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
336b9 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 75 74 65 78  alConfig.isMutex
336ba 49 6e 69 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  Init ){.    sqli
336bb 74 65 33 4d 75 74 65 78 45 6e 64 28 29 3b 0a 20  te3MutexEnd();. 
336bc 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
336bd 43 6f 6e 66 69 67 2e 69 73 4d 75 74 65 78 49 6e  Config.isMutexIn
336be 69 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 72  it = 0;.  }..  r
336bf 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
336c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 41  .}../*.** This A
336c1 50 49 20 61 6c 6c 6f 77 73 20 61 70 70 6c 69 63  PI allows applic
336c2 61 74 69 6f 6e 73 20 74 6f 20 6d 6f 64 69 66 79  ations to modify
336c3 20 74 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66   the global conf
336c4 69 67 75 72 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20  iguration of.** 
336c5 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
336c6 72 79 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 0a  ry at run-time..
336c7 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
336c8 6e 65 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62  ne should only b
336c9 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68  e called when th
336ca 65 72 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74  ere are no outst
336cb 61 6e 64 69 6e 67 0a 2a 2a 20 64 61 74 61 62 61  anding.** databa
336cc 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f  se connections o
336cd 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  r memory allocat
336ce 69 6f 6e 73 2e 20 20 54 68 69 73 20 72 6f 75 74  ions.  This rout
336cf 69 6e 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 74 68  ine is not.** th
336d0 72 65 61 64 73 61 66 65 2e 20 20 46 61 69 6c 75  readsafe.  Failu
336d1 72 65 20 74 6f 20 68 65 65 64 20 74 68 65 73 65  re to heed these
336d2 20 77 61 72 6e 69 6e 67 73 20 63 61 6e 20 6c 65   warnings can le
336d3 61 64 20 74 6f 20 75 6e 70 72 65 64 69 63 74 61  ad to unpredicta
336d4 62 6c 65 0a 2a 2a 20 62 65 68 61 76 69 6f 72 2e  ble.** behavior.
336d5 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
336d6 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  nt sqlite3_confi
336d7 67 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a  g(int op, ...){.
336d8 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20    va_list ap;.  
336d9 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
336da 4f 4b 3b 0a 0a 20 20 2f 2a 20 73 71 6c 69 74 65  OK;..  /* sqlite
336db 33 5f 63 6f 6e 66 69 67 28 29 20 73 68 61 6c 6c  3_config() shall
336dc 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d   return SQLITE_M
336dd 49 53 55 53 45 20 69 66 20 69 74 20 69 73 20 69  ISUSE if it is i
336de 6e 76 6f 6b 65 64 20 77 68 69 6c 65 0a 20 20 2a  nvoked while.  *
336df 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62  * the SQLite lib
336e0 72 61 72 79 20 69 73 20 69 6e 20 75 73 65 2e 20  rary is in use. 
336e1 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
336e2 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49  GlobalConfig.isI
336e3 6e 69 74 20 29 20 72 65 74 75 72 6e 20 53 51 4c  nit ) return SQL
336e4 49 54 45 5f 4d 49 53 55 53 45 3b 0a 0a 20 20 76  ITE_MISUSE;..  v
336e5 61 5f 73 74 61 72 74 28 61 70 2c 20 6f 70 29 3b  a_start(ap, op);
336e6 0a 20 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b  .  switch( op ){
336e7 0a 0a 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63  ..    /* Mutex c
336e8 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74  onfiguration opt
336e9 69 6f 6e 73 20 61 72 65 20 6f 6e 6c 79 20 61 76  ions are only av
336ea 61 69 6c 61 62 6c 65 20 69 6e 20 61 20 74 68 72  ailable in a thr
336eb 65 61 64 73 61 66 65 0a 20 20 20 20 2a 2a 20 63  eadsafe.    ** c
336ec 6f 6d 70 69 6c 65 2e 20 0a 20 20 20 20 2a 2f 0a  ompile. .    */.
336ed 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
336ee 44 53 41 46 45 0a 20 20 20 20 63 61 73 65 20 53  DSAFE.    case S
336ef 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e  QLITE_CONFIG_SIN
336f0 47 4c 45 54 48 52 45 41 44 3a 20 7b 0a 20 20 20  GLETHREAD: {.   
336f1 20 20 20 2f 2a 20 44 69 73 61 62 6c 65 20 61 6c     /* Disable al
336f2 6c 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20  l mutexing */.  
336f3 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
336f4 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74  lConfig.bCoreMut
336f5 65 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71  ex = 0;.      sq
336f6 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
336f7 67 2e 62 46 75 6c 6c 4d 75 74 65 78 20 3d 20 30  g.bFullMutex = 0
336f8 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
336f9 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
336fa 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54  LITE_CONFIG_MULT
336fb 49 54 48 52 45 41 44 3a 20 7b 0a 20 20 20 20 20  ITHREAD: {.     
336fc 20 2f 2a 20 44 69 73 61 62 6c 65 20 6d 75 74 65   /* Disable mute
336fd 78 69 6e 67 20 6f 66 20 64 61 74 61 62 61 73 65  xing of database
336fe 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a   connections */.
336ff 20 20 20 20 20 20 2f 2a 20 45 6e 61 62 6c 65 20        /* Enable 
33700 6d 75 74 65 78 69 6e 67 20 6f 66 20 63 6f 72 65  mutexing of core
33701 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73   data structures
33702 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65   */.      sqlite
33703 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43  3GlobalConfig.bC
33704 6f 72 65 4d 75 74 65 78 20 3d 20 31 3b 0a 20 20  oreMutex = 1;.  
33705 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
33706 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74  lConfig.bFullMut
33707 65 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 62 72  ex = 0;.      br
33708 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
33709 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ase SQLITE_CONFI
3370a 47 5f 53 45 52 49 41 4c 49 5a 45 44 3a 20 7b 0a  G_SERIALIZED: {.
3370b 20 20 20 20 20 20 2f 2a 20 45 6e 61 62 6c 65 20        /* Enable 
3370c 61 6c 6c 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a  all mutexing */.
3370d 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
3370e 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d  balConfig.bCoreM
3370f 75 74 65 78 20 3d 20 31 3b 0a 20 20 20 20 20 20  utex = 1;.      
33710 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
33711 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78 20 3d  fig.bFullMutex =
33712 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   1;.      break;
33713 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
33714 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55  SQLITE_CONFIG_MU
33715 54 45 58 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  TEX: {.      /* 
33716 53 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72  Specify an alter
33717 6e 61 74 69 76 65 20 6d 75 74 65 78 20 69 6d 70  native mutex imp
33718 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20  lementation */. 
33719 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
3371a 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78 20 3d  alConfig.mutex =
3371b 20 2a 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c   *va_arg(ap, sql
3371c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
3371d 64 73 2a 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ds*);.      brea
3371e 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
3371f 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
33720 47 45 54 4d 55 54 45 58 3a 20 7b 0a 20 20 20 20  GETMUTEX: {.    
33721 20 20 2f 2a 20 52 65 74 72 69 65 76 65 20 74 68    /* Retrieve th
33722 65 20 63 75 72 72 65 6e 74 20 6d 75 74 65 78 20  e current mutex 
33723 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a  implementation *
33724 2f 0a 20 20 20 20 20 20 2a 76 61 5f 61 72 67 28  /.      *va_arg(
33725 61 70 2c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ap, sqlite3_mute
33726 78 5f 6d 65 74 68 6f 64 73 2a 29 20 3d 20 73 71  x_methods*) = sq
33727 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
33728 67 2e 6d 75 74 65 78 3b 0a 20 20 20 20 20 20 62  g.mutex;.      b
33729 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  reak;.    }.#end
3372a 69 66 0a 0a 0a 20 20 20 20 63 61 73 65 20 53 51  if...    case SQ
3372b 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c  LITE_CONFIG_MALL
3372c 4f 43 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 53  OC: {.      /* S
3372d 70 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e  pecify an altern
3372e 61 74 69 76 65 20 6d 61 6c 6c 6f 63 20 69 6d 70  ative malloc imp
3372f 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20  lementation */. 
33730 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
33731 61 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20 2a 76 61  alConfig.m = *va
33732 5f 61 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33  _arg(ap, sqlite3
33733 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 29 3b 0a  _mem_methods*);.
33734 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
33735 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49   }.    case SQLI
33736 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c  TE_CONFIG_GETMAL
33737 4c 4f 43 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  LOC: {.      /* 
33738 52 65 74 72 69 65 76 65 20 74 68 65 20 63 75 72  Retrieve the cur
33739 72 65 6e 74 20 6d 61 6c 6c 6f 63 28 29 20 69 6d  rent malloc() im
3373a 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a  plementation */.
3373b 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
3373c 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e  3GlobalConfig.m.
3373d 78 4d 61 6c 6c 6f 63 3d 3d 30 20 29 20 73 71 6c  xMalloc==0 ) sql
3373e 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c  ite3MemSetDefaul
3373f 74 28 29 3b 0a 20 20 20 20 20 20 2a 76 61 5f 61  t();.      *va_a
33740 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 6d  rg(ap, sqlite3_m
33741 65 6d 5f 6d 65 74 68 6f 64 73 2a 29 20 3d 20 73  em_methods*) = s
33742 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
33743 69 67 2e 6d 3b 0a 20 20 20 20 20 20 62 72 65 61  ig.m;.      brea
33744 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
33745 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
33746 4d 45 4d 53 54 41 54 55 53 3a 20 7b 0a 20 20 20  MEMSTATUS: {.   
33747 20 20 20 2f 2a 20 45 6e 61 62 6c 65 20 6f 72 20     /* Enable or 
33748 64 69 73 61 62 6c 65 20 74 68 65 20 6d 61 6c 6c  disable the mall
33749 6f 63 20 73 74 61 74 75 73 20 63 6f 6c 6c 65 63  oc status collec
3374a 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71  tion */.      sq
3374b 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
3374c 67 2e 62 4d 65 6d 73 74 61 74 20 3d 20 76 61 5f  g.bMemstat = va_
3374d 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
3374e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
3374f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
33750 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 3a  _CONFIG_SCRATCH:
33751 20 7b 0a 20 20 20 20 20 20 2f 2a 20 44 65 73 69   {.      /* Desi
33752 67 6e 61 74 65 20 61 20 62 75 66 66 65 72 20 66  gnate a buffer f
33753 6f 72 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72  or scratch memor
33754 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20  y space */.     
33755 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
33756 6e 66 69 67 2e 70 53 63 72 61 74 63 68 20 3d 20  nfig.pScratch = 
33757 76 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 2a  va_arg(ap, void*
33758 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
33759 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53  GlobalConfig.szS
3375a 63 72 61 74 63 68 20 3d 20 76 61 5f 61 72 67 28  cratch = va_arg(
3375b 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ap, int);.      
3375c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
3375d 66 69 67 2e 6e 53 63 72 61 74 63 68 20 3d 20 76  fig.nScratch = v
3375e 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a  a_arg(ap, int);.
3375f 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
33760 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49   }.    case SQLI
33761 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41  TE_CONFIG_PAGECA
33762 43 48 45 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  CHE: {.      /* 
33763 44 65 73 69 67 6e 61 74 65 20 61 20 62 75 66 66  Designate a buff
33764 65 72 20 66 6f 72 20 70 61 67 65 20 63 61 63 68  er for page cach
33765 65 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20 2a  e memory space *
33766 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  /.      sqlite3G
33767 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67  lobalConfig.pPag
33768 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76  e = va_arg(ap, v
33769 6f 69 64 2a 29 3b 0a 20 20 20 20 20 20 73 71 6c  oid*);.      sql
3376a 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
3376b 2e 73 7a 50 61 67 65 20 3d 20 76 61 5f 61 72 67  .szPage = va_arg
3376c 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20  (ap, int);.     
3376d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3376e 6e 66 69 67 2e 6e 50 61 67 65 20 3d 20 76 61 5f  nfig.nPage = va_
3376f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
33770 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
33771 0a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  ..    case SQLIT
33772 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 3a  E_CONFIG_PCACHE:
33773 20 7b 0a 20 20 20 20 20 20 2f 2a 20 53 70 65 63   {.      /* Spec
33774 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69  ify an alternati
33775 76 65 20 70 61 67 65 20 63 61 63 68 65 20 69 6d  ve page cache im
33776 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a  plementation */.
33777 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f        sqlite3Glo
33778 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65  balConfig.pcache
33779 20 3d 20 2a 76 61 5f 61 72 67 28 61 70 2c 20 73   = *va_arg(ap, s
3377a 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
3377b 74 68 6f 64 73 2a 29 3b 0a 20 20 20 20 20 20 62  thods*);.      b
3377c 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
3377d 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e   case SQLITE_CON
3377e 46 49 47 5f 47 45 54 50 43 41 43 48 45 3a 20 7b  FIG_GETPCACHE: {
3377f 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  .      if( sqlit
33780 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
33781 63 61 63 68 65 2e 78 49 6e 69 74 3d 3d 30 20 29  cache.xInit==0 )
33782 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
33783 33 50 43 61 63 68 65 53 65 74 44 65 66 61 75 6c  3PCacheSetDefaul
33784 74 28 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  t();.      }.   
33785 20 20 20 2a 76 61 5f 61 72 67 28 61 70 2c 20 73     *va_arg(ap, s
33786 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65  qlite3_pcache_me
33787 74 68 6f 64 73 2a 29 20 3d 20 73 71 6c 69 74 65  thods*) = sqlite
33788 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
33789 61 63 68 65 3b 0a 20 20 20 20 20 20 62 72 65 61  ache;.      brea
3378a 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 20 64 65  k;.    }..#if de
3378b 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
3378c 42 4c 45 5f 4d 45 4d 53 59 53 33 29 20 7c 7c 20  BLE_MEMSYS3) || 
3378d 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
3378e 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 29 0a 20  NABLE_MEMSYS5). 
3378f 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43     case SQLITE_C
33790 4f 4e 46 49 47 5f 48 45 41 50 3a 20 7b 0a 20 20  ONFIG_HEAP: {.  
33791 20 20 20 20 2f 2a 20 44 65 73 69 67 6e 61 74 65      /* Designate
33792 20 61 20 62 75 66 66 65 72 20 66 6f 72 20 68 65   a buffer for he
33793 61 70 20 6d 65 6d 6f 72 79 20 73 70 61 63 65 20  ap memory space 
33794 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
33795 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65  GlobalConfig.pHe
33796 61 70 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  ap = va_arg(ap, 
33797 76 6f 69 64 2a 29 3b 0a 20 20 20 20 20 20 73 71  void*);.      sq
33798 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
33799 67 2e 6e 48 65 61 70 20 3d 20 76 61 5f 61 72 67  g.nHeap = va_arg
3379a 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20  (ap, int);.     
3379b 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
3379c 6e 66 69 67 2e 6d 6e 52 65 71 20 3d 20 76 61 5f  nfig.mnReq = va_
3379d 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 0a 20  arg(ap, int);.. 
3379e 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
3379f 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 48 65  GlobalConfig.pHe
337a0 61 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ap==0 ){.       
337a1 20 2f 2a 20 49 66 20 74 68 65 20 68 65 61 70 20   /* If the heap 
337a2 70 6f 69 6e 74 65 72 20 69 73 20 4e 55 4c 4c 2c  pointer is NULL,
337a3 20 74 68 65 6e 20 72 65 73 74 6f 72 65 20 74 68   then restore th
337a4 65 20 6d 61 6c 6c 6f 63 20 69 6d 70 6c 65 6d 65  e malloc impleme
337a5 6e 74 61 74 69 6f 6e 0a 20 20 20 20 20 20 20 20  ntation.        
337a6 2a 2a 20 62 61 63 6b 20 74 6f 20 4e 55 4c 4c 20  ** back to NULL 
337a7 70 6f 69 6e 74 65 72 73 20 74 6f 6f 2e 20 20 54  pointers too.  T
337a8 68 69 73 20 77 69 6c 6c 20 63 61 75 73 65 20 74  his will cause t
337a9 68 65 20 6d 61 6c 6c 6f 63 20 74 6f 20 67 6f 0a  he malloc to go.
337aa 20 20 20 20 20 20 20 20 2a 2a 20 62 61 63 6b 20          ** back 
337ab 74 6f 20 69 74 73 20 64 65 66 61 75 6c 74 20 69  to its default i
337ac 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 68  mplementation wh
337ad 65 6e 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  en sqlite3_initi
337ae 61 6c 69 7a 65 28 29 20 69 73 0a 20 20 20 20 20  alize() is.     
337af 20 20 20 2a 2a 20 72 75 6e 2e 0a 20 20 20 20 20     ** run..     
337b0 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 6d 65     */.        me
337b1 6d 73 65 74 28 26 73 71 6c 69 74 65 33 47 6c 6f  mset(&sqlite3Glo
337b2 62 61 6c 43 6f 6e 66 69 67 2e 6d 2c 20 30 2c 20  balConfig.m, 0, 
337b3 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 47 6c  sizeof(sqlite3Gl
337b4 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 29 29 3b 0a  obalConfig.m));.
337b5 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
337b6 20 20 20 20 20 2f 2a 20 54 68 65 20 68 65 61 70       /* The heap
337b7 20 70 6f 69 6e 74 65 72 20 69 73 20 6e 6f 74 20   pointer is not 
337b8 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 6e 73 74 61  NULL, then insta
337b9 6c 6c 20 6f 6e 65 20 6f 66 20 74 68 65 0a 20 20  ll one of the.  
337ba 20 20 20 20 20 20 2a 2a 20 6d 65 6d 35 2e 63 2f        ** mem5.c/
337bb 6d 65 6d 33 2e 63 20 6d 65 74 68 6f 64 73 2e 20  mem3.c methods. 
337bc 49 66 20 6e 65 69 74 68 65 72 20 45 4e 41 42 4c  If neither ENABL
337bd 45 5f 4d 45 4d 53 59 53 33 20 6e 6f 72 0a 20 20  E_MEMSYS3 nor.  
337be 20 20 20 20 20 20 2a 2a 20 45 4e 41 42 4c 45 5f        ** ENABLE_
337bf 4d 45 4d 53 59 53 35 20 69 73 20 64 65 66 69 6e  MEMSYS5 is defin
337c0 65 64 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72  ed, return an er
337c1 72 6f 72 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ror..        */.
337c2 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
337c3 41 42 4c 45 5f 4d 45 4d 53 59 53 33 0a 20 20 20  ABLE_MEMSYS3.   
337c4 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
337c5 61 6c 43 6f 6e 66 69 67 2e 6d 20 3d 20 2a 73 71  alConfig.m = *sq
337c6 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79  lite3MemGetMemsy
337c7 73 33 28 29 3b 0a 23 65 6e 64 69 66 0a 23 69 66  s3();.#endif.#if
337c8 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
337c9 45 5f 4d 45 4d 53 59 53 35 0a 20 20 20 20 20 20  E_MEMSYS5.      
337ca 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
337cb 6f 6e 66 69 67 2e 6d 20 3d 20 2a 73 71 6c 69 74  onfig.m = *sqlit
337cc 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35 28  e3MemGetMemsys5(
337cd 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20  );.#endif.      
337ce 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
337cf 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
337d0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e   case SQLITE_CON
337d1 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 3a 20 7b  FIG_LOOKASIDE: {
337d2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
337d3 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 4c 6f 6f  obalConfig.szLoo
337d4 6b 61 73 69 64 65 20 3d 20 76 61 5f 61 72 67 28  kaside = va_arg(
337d5 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ap, int);.      
337d6 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
337d7 66 69 67 2e 6e 4c 6f 6f 6b 61 73 69 64 65 20 3d  fig.nLookaside =
337d8 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29   va_arg(ap, int)
337d9 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
337da 20 20 20 7d 0a 0a 20 20 20 20 64 65 66 61 75 6c     }..    defaul
337db 74 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  t: {.      rc = 
337dc 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
337dd 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
337de 0a 20 20 7d 0a 20 20 76 61 5f 65 6e 64 28 61 70  .  }.  va_end(ap
337df 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
337e0 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75 70 20  }../*.** Set up 
337e1 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 62 75  the lookaside bu
337e2 66 66 65 72 73 20 66 6f 72 20 61 20 64 61 74 61  ffers for a data
337e3 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
337e4 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54  .** Return SQLIT
337e5 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 2e  E_OK on success.
337e6 20 20 0a 2a 2a 20 49 66 20 6c 6f 6f 6b 61 73 69    .** If lookasi
337e7 64 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 63  de is already ac
337e8 74 69 76 65 2c 20 72 65 74 75 72 6e 20 53 51 4c  tive, return SQL
337e9 49 54 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20  ITE_BUSY..**.** 
337ea 54 68 65 20 73 7a 20 70 61 72 61 6d 65 74 65 72  The sz parameter
337eb 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
337ec 66 20 62 79 74 65 73 20 69 6e 20 65 61 63 68 20  f bytes in each 
337ed 6c 6f 6f 6b 61 73 69 64 65 20 73 6c 6f 74 2e 0a  lookaside slot..
337ee 2a 2a 20 54 68 65 20 63 6e 74 20 70 61 72 61 6d  ** The cnt param
337ef 65 74 65 72 20 69 73 20 74 68 65 20 6e 75 6d 62  eter is the numb
337f0 65 72 20 6f 66 20 73 6c 6f 74 73 2e 20 20 49 66  er of slots.  If
337f1 20 70 53 74 61 72 74 20 69 73 20 4e 55 4c 4c 20   pStart is NULL 
337f2 74 68 65 0a 2a 2a 20 73 70 61 63 65 20 66 6f 72  the.** space for
337f3 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 6d   the lookaside m
337f4 65 6d 6f 72 79 20 69 73 20 6f 62 74 61 69 6e 65  emory is obtaine
337f5 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d  d from sqlite3_m
337f6 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66 20 70  alloc()..** If p
337f7 53 74 61 72 74 20 69 73 20 6e 6f 74 20 4e 55 4c  Start is not NUL
337f8 4c 20 74 68 65 6e 20 69 74 20 69 73 20 73 7a 2a  L then it is sz*
337f9 63 6e 74 20 62 79 74 65 73 20 6f 66 20 6d 65 6d  cnt bytes of mem
337fa 6f 72 79 20 74 6f 20 75 73 65 20 66 6f 72 0a 2a  ory to use for.*
337fb 2a 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20  * the lookaside 
337fc 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69  memory..*/.stati
337fd 63 20 69 6e 74 20 73 65 74 75 70 4c 6f 6f 6b 61  c int setupLooka
337fe 73 69 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62  side(sqlite3 *db
337ff 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e  , void *pBuf, in
33800 74 20 73 7a 2c 20 69 6e 74 20 63 6e 74 29 7b 0a  t sz, int cnt){.
33801 20 20 76 6f 69 64 20 2a 70 53 74 61 72 74 3b 0a    void *pStart;.
33802 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73    if( db->lookas
33803 69 64 65 2e 6e 4f 75 74 20 29 7b 0a 20 20 20 20  ide.nOut ){.    
33804 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55  return SQLITE_BU
33805 53 59 3b 0a 20 20 7d 0a 20 20 2f 2a 20 46 72 65  SY;.  }.  /* Fre
33806 65 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 6c  e any existing l
33807 6f 6f 6b 61 73 69 64 65 20 62 75 66 66 65 72 20  ookaside buffer 
33808 66 6f 72 20 74 68 69 73 20 68 61 6e 64 6c 65 20  for this handle 
33809 62 65 66 6f 72 65 0a 20 20 2a 2a 20 61 6c 6c 6f  before.  ** allo
3380a 63 61 74 69 6e 67 20 61 20 6e 65 77 20 6f 6e 65  cating a new one
3380b 20 73 6f 20 77 65 20 64 6f 6e 27 74 20 68 61 76   so we don't hav
3380c 65 20 74 6f 20 68 61 76 65 20 73 70 61 63 65 20  e to have space 
3380d 66 6f 72 20 0a 20 20 2a 2a 20 62 6f 74 68 20 61  for .  ** both a
3380e 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e  t the same time.
3380f 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  .  */.  if( db->
33810 6c 6f 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c 6f  lookaside.bMallo
33811 63 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ced ){.    sqlit
33812 65 33 5f 66 72 65 65 28 64 62 2d 3e 6c 6f 6f 6b  e3_free(db->look
33813 61 73 69 64 65 2e 70 53 74 61 72 74 29 3b 0a 20  aside.pStart);. 
33814 20 7d 0a 20 20 2f 2a 20 54 68 65 20 73 69 7a 65   }.  /* The size
33815 20 6f 66 20 61 20 6c 6f 6f 6b 61 73 69 64 65 20   of a lookaside 
33816 73 6c 6f 74 20 6e 65 65 64 73 20 74 6f 20 62 65  slot needs to be
33817 20 6c 61 72 67 65 72 20 74 68 61 6e 20 61 20 70   larger than a p
33818 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 74 6f 20 62  ointer.  ** to b
33819 65 20 75 73 65 66 75 6c 2e 0a 20 20 2a 2f 0a 20  e useful..  */. 
3381a 20 69 66 28 20 73 7a 3c 3d 28 69 6e 74 29 73 69   if( sz<=(int)si
3381b 7a 65 6f 66 28 4c 6f 6f 6b 61 73 69 64 65 53 6c  zeof(LookasideSl
3381c 6f 74 2a 29 20 29 20 73 7a 20 3d 20 30 3b 0a 20  ot*) ) sz = 0;. 
3381d 20 69 66 28 20 63 6e 74 3c 30 20 29 20 63 6e 74   if( cnt<0 ) cnt
3381e 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 7a 3d 3d   = 0;.  if( sz==
3381f 30 20 7c 7c 20 63 6e 74 3d 3d 30 20 29 7b 0a 20  0 || cnt==0 ){. 
33820 20 20 20 73 7a 20 3d 20 30 3b 0a 20 20 20 20 70     sz = 0;.    p
33821 53 74 61 72 74 20 3d 20 30 3b 0a 20 20 7d 65 6c  Start = 0;.  }el
33822 73 65 20 69 66 28 20 70 42 75 66 3d 3d 30 20 29  se if( pBuf==0 )
33823 7b 0a 20 20 20 20 73 7a 20 3d 20 52 4f 55 4e 44  {.    sz = ROUND
33824 38 28 73 7a 29 3b 0a 20 20 20 20 73 71 6c 69 74  8(sz);.    sqlit
33825 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c  e3BeginBenignMal
33826 6c 6f 63 28 29 3b 0a 20 20 20 20 70 53 74 61 72  loc();.    pStar
33827 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  t = sqlite3Mallo
33828 63 28 20 73 7a 2a 63 6e 74 20 29 3b 0a 20 20 20  c( sz*cnt );.   
33829 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
3382a 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 7d 65 6c  nMalloc();.  }el
3382b 73 65 7b 0a 20 20 20 20 73 7a 20 3d 20 52 4f 55  se{.    sz = ROU
3382c 4e 44 44 4f 57 4e 38 28 73 7a 29 3b 0a 20 20 20  NDDOWN8(sz);.   
3382d 20 70 53 74 61 72 74 20 3d 20 70 42 75 66 3b 0a   pStart = pBuf;.
3382e 20 20 7d 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73    }.  db->lookas
3382f 69 64 65 2e 70 53 74 61 72 74 20 3d 20 70 53 74  ide.pStart = pSt
33830 61 72 74 3b 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61  art;.  db->looka
33831 73 69 64 65 2e 70 46 72 65 65 20 3d 20 30 3b 0a  side.pFree = 0;.
33832 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
33833 73 7a 20 3d 20 28 75 31 36 29 73 7a 3b 0a 20 20  sz = (u16)sz;.  
33834 69 66 28 20 70 53 74 61 72 74 20 29 7b 0a 20 20  if( pStart ){.  
33835 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 4c 6f 6f    int i;.    Loo
33836 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 3b 0a 20  kasideSlot *p;. 
33837 20 20 20 61 73 73 65 72 74 28 20 73 7a 20 3e 20     assert( sz > 
33838 28 69 6e 74 29 73 69 7a 65 6f 66 28 4c 6f 6f 6b  (int)sizeof(Look
33839 61 73 69 64 65 53 6c 6f 74 2a 29 20 29 3b 0a 20  asideSlot*) );. 
3383a 20 20 20 70 20 3d 20 28 4c 6f 6f 6b 61 73 69 64     p = (Lookasid
3383b 65 53 6c 6f 74 2a 29 70 53 74 61 72 74 3b 0a 20  eSlot*)pStart;. 
3383c 20 20 20 66 6f 72 28 69 3d 63 6e 74 2d 31 3b 20     for(i=cnt-1; 
3383d 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20  i>=0; i--){.    
3383e 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d    p->pNext = db-
3383f 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65  >lookaside.pFree
33840 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b  ;.      db->look
33841 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20 70 3b  aside.pFree = p;
33842 0a 20 20 20 20 20 20 70 20 3d 20 28 4c 6f 6f 6b  .      p = (Look
33843 61 73 69 64 65 53 6c 6f 74 2a 29 26 28 28 75 38  asideSlot*)&((u8
33844 2a 29 70 29 5b 73 7a 5d 3b 0a 20 20 20 20 7d 0a  *)p)[sz];.    }.
33845 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
33846 65 2e 70 45 6e 64 20 3d 20 70 3b 0a 20 20 20 20  e.pEnd = p;.    
33847 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45  db->lookaside.bE
33848 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  nabled = 1;.    
33849 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 4d  db->lookaside.bM
3384a 61 6c 6c 6f 63 65 64 20 3d 20 70 42 75 66 3d 3d  alloced = pBuf==
3384b 30 20 3f 31 3a 30 3b 0a 20 20 7d 65 6c 73 65 7b  0 ?1:0;.  }else{
3384c 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  .    db->lookasi
3384d 64 65 2e 70 45 6e 64 20 3d 20 30 3b 0a 20 20 20  de.pEnd = 0;.   
3384e 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62   db->lookaside.b
3384f 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 20 20 20  Enabled = 0;.   
33850 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62   db->lookaside.b
33851 4d 61 6c 6c 6f 63 65 64 20 3d 20 30 3b 0a 20 20  Malloced = 0;.  
33852 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
33853 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  E_OK;.}../*.** R
33854 65 74 75 72 6e 20 74 68 65 20 6d 75 74 65 78 20  eturn the mutex 
33855 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
33856 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  a database conne
33857 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ction..*/.SQLITE
33858 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 6d 75 74  _API sqlite3_mut
33859 65 78 20 2a 73 71 6c 69 74 65 33 5f 64 62 5f 6d  ex *sqlite3_db_m
3385a 75 74 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62  utex(sqlite3 *db
3385b 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e  ){.  return db->
3385c 6d 75 74 65 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  mutex;.}../*.** 
3385d 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 73 65  Configuration se
3385e 74 74 69 6e 67 73 20 66 6f 72 20 61 6e 20 69 6e  ttings for an in
3385f 64 69 76 69 64 75 61 6c 20 64 61 74 61 62 61 73  dividual databas
33860 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2f 0a  e connection.*/.
33861 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
33862 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67  qlite3_db_config
33863 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
33864 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61  t op, ...){.  va
33865 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 6e 74 20  _list ap;.  int 
33866 72 63 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61  rc;.  va_start(a
33867 70 2c 20 6f 70 29 3b 0a 20 20 73 77 69 74 63 68  p, op);.  switch
33868 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65  ( op ){.    case
33869 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47   SQLITE_DBCONFIG
3386a 5f 4c 4f 4f 4b 41 53 49 44 45 3a 20 7b 0a 20 20  _LOOKASIDE: {.  
3386b 20 20 20 20 76 6f 69 64 20 2a 70 42 75 66 20 3d      void *pBuf =
3386c 20 76 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64   va_arg(ap, void
3386d 2a 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 73 7a  *);.      int sz
3386e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
3386f 74 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 63 6e  t);.      int cn
33870 74 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69  t = va_arg(ap, i
33871 6e 74 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  nt);.      rc = 
33872 73 65 74 75 70 4c 6f 6f 6b 61 73 69 64 65 28 64  setupLookaside(d
33873 62 2c 20 70 42 75 66 2c 20 73 7a 2c 20 63 6e 74  b, pBuf, sz, cnt
33874 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
33875 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75 6c      }.    defaul
33876 74 3a 20 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  t: {.      rc = 
33877 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
33878 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
33879 0a 20 20 7d 0a 20 20 76 61 5f 65 6e 64 28 61 70  .  }.  va_end(ap
3387a 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
3387b 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  }.../*.** Return
3387c 20 74 72 75 65 20 69 66 20 74 68 65 20 62 75 66   true if the buf
3387d 66 65 72 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 63 6f  fer z[0..n-1] co
3387e 6e 74 61 69 6e 73 20 61 6c 6c 20 73 70 61 63 65  ntains all space
3387f 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
33880 20 61 6c 6c 53 70 61 63 65 73 28 63 6f 6e 73 74   allSpaces(const
33881 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29   char *z, int n)
33882 7b 0a 20 20 77 68 69 6c 65 28 20 6e 3e 30 20 26  {.  while( n>0 &
33883 26 20 7a 5b 6e 2d 31 5d 3d 3d 27 20 27 20 29 7b  & z[n-1]==' ' ){
33884 20 6e 2d 2d 3b 20 7d 0a 20 20 72 65 74 75 72 6e   n--; }.  return
33885 20 6e 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   n==0;.}../*.** 
33886 54 68 69 73 20 69 73 20 74 68 65 20 64 65 66 61  This is the defa
33887 75 6c 74 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75  ult collating fu
33888 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20 22 42 49  nction named "BI
33889 4e 41 52 59 22 20 77 68 69 63 68 20 69 73 20 61  NARY" which is a
3388a 6c 77 61 79 73 0a 2a 2a 20 61 76 61 69 6c 61 62  lways.** availab
3388b 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  le..**.** If the
3388c 20 70 61 64 46 6c 61 67 20 61 72 67 75 6d 65 6e   padFlag argumen
3388d 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 74 68  t is not NULL th
3388e 65 6e 20 73 70 61 63 65 20 70 61 64 64 69 6e 67  en space padding
3388f 20 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a 20 6f   at the end.** o
33890 66 20 73 74 72 69 6e 67 73 20 69 73 20 69 67 6e  f strings is ign
33891 6f 72 65 64 2e 20 20 54 68 69 73 20 69 6d 70 6c  ored.  This impl
33892 65 6d 65 6e 74 73 20 74 68 65 20 52 54 52 49 4d  ements the RTRIM
33893 20 63 6f 6c 6c 61 74 69 6f 6e 2e 0a 2a 2f 0a 73   collation..*/.s
33894 74 61 74 69 63 20 69 6e 74 20 62 69 6e 43 6f 6c  tatic int binCol
33895 6c 46 75 6e 63 28 0a 20 20 76 6f 69 64 20 2a 70  lFunc(.  void *p
33896 61 64 46 6c 61 67 2c 0a 20 20 69 6e 74 20 6e 4b  adFlag,.  int nK
33897 65 79 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  ey1, const void 
33898 2a 70 4b 65 79 31 2c 0a 20 20 69 6e 74 20 6e 4b  *pKey1,.  int nK
33899 65 79 32 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  ey2, const void 
3389a 2a 70 4b 65 79 32 0a 29 7b 0a 20 20 69 6e 74 20  *pKey2.){.  int 
3389b 72 63 2c 20 6e 3b 0a 20 20 6e 20 3d 20 6e 4b 65  rc, n;.  n = nKe
3389c 79 31 3c 6e 4b 65 79 32 20 3f 20 6e 4b 65 79 31  y1<nKey2 ? nKey1
3389d 20 3a 20 6e 4b 65 79 32 3b 0a 20 20 72 63 20 3d   : nKey2;.  rc =
3389e 20 6d 65 6d 63 6d 70 28 70 4b 65 79 31 2c 20 70   memcmp(pKey1, p
3389f 4b 65 79 32 2c 20 6e 29 3b 0a 20 20 69 66 28 20  Key2, n);.  if( 
338a0 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  rc==0 ){.    if(
338a1 20 70 61 64 46 6c 61 67 0a 20 20 20 20 20 26 26   padFlag.     &&
338a2 20 61 6c 6c 53 70 61 63 65 73 28 28 28 63 68 61   allSpaces(((cha
338a3 72 2a 29 70 4b 65 79 31 29 2b 6e 2c 20 6e 4b 65  r*)pKey1)+n, nKe
338a4 79 31 2d 6e 29 0a 20 20 20 20 20 26 26 20 61 6c  y1-n).     && al
338a5 6c 53 70 61 63 65 73 28 28 28 63 68 61 72 2a 29  lSpaces(((char*)
338a6 70 4b 65 79 32 29 2b 6e 2c 20 6e 4b 65 79 32 2d  pKey2)+n, nKey2-
338a7 6e 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  n).    ){.      
338a8 2f 2a 20 4c 65 61 76 65 20 72 63 20 75 6e 63 68  /* Leave rc unch
338a9 61 6e 67 65 64 20 61 74 20 30 20 2a 2f 0a 20 20  anged at 0 */.  
338aa 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
338ab 63 20 3d 20 6e 4b 65 79 31 20 2d 20 6e 4b 65 79  c = nKey1 - nKey
338ac 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  2;.    }.  }.  r
338ad 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
338ae 2a 2a 20 41 6e 6f 74 68 65 72 20 62 75 69 6c 74  ** Another built
338af 2d 69 6e 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65  -in collating se
338b0 71 75 65 6e 63 65 3a 20 4e 4f 43 41 53 45 2e 20  quence: NOCASE. 
338b1 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 6f 6c 6c  .**.** This coll
338b2 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69  ating sequence i
338b3 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65  s intended to be
338b4 20 75 73 65 64 20 66 6f 72 20 22 63 61 73 65 20   used for "case 
338b5 69 6e 64 65 70 65 6e 64 61 6e 74 0a 2a 2a 20 63  independant.** c
338b6 6f 6d 70 61 72 69 73 6f 6e 22 2e 20 53 51 4c 69  omparison". SQLi
338b7 74 65 27 73 20 6b 6e 6f 77 6c 65 64 67 65 20 6f  te's knowledge o
338b8 66 20 75 70 70 65 72 20 61 6e 64 20 6c 6f 77 65  f upper and lowe
338b9 72 20 63 61 73 65 20 65 71 75 69 76 61 6c 65 6e  r case equivalen
338ba 74 73 0a 2a 2a 20 65 78 74 65 6e 64 73 20 6f 6e  ts.** extends on
338bb 6c 79 20 74 6f 20 74 68 65 20 32 36 20 63 68 61  ly to the 26 cha
338bc 72 61 63 74 65 72 73 20 75 73 65 64 20 69 6e 20  racters used in 
338bd 74 68 65 20 45 6e 67 6c 69 73 68 20 6c 61 6e 67  the English lang
338be 75 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74  uage..**.** At t
338bf 68 65 20 6d 6f 6d 65 6e 74 20 74 68 65 72 65 20  he moment there 
338c0 69 73 20 6f 6e 6c 79 20 61 20 55 54 46 2d 38 20  is only a UTF-8 
338c1 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 0a  implementation..
338c2 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f  */.static int no
338c3 63 61 73 65 43 6f 6c 6c 61 74 69 6e 67 46 75 6e  caseCollatingFun
338c4 63 28 0a 20 20 76 6f 69 64 20 2a 4e 6f 74 55 73  c(.  void *NotUs
338c5 65 64 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 31 2c  ed,.  int nKey1,
338c6 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
338c7 79 31 2c 0a 20 20 69 6e 74 20 6e 4b 65 79 32 2c  y1,.  int nKey2,
338c8 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
338c9 79 32 0a 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20  y2.){.  int r = 
338ca 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
338cb 0a 20 20 20 20 20 20 28 63 6f 6e 73 74 20 63 68  .      (const ch
338cc 61 72 20 2a 29 70 4b 65 79 31 2c 20 28 63 6f 6e  ar *)pKey1, (con
338cd 73 74 20 63 68 61 72 20 2a 29 70 4b 65 79 32 2c  st char *)pKey2,
338ce 20 28 6e 4b 65 79 31 3c 6e 4b 65 79 32 29 3f 6e   (nKey1<nKey2)?n
338cf 4b 65 79 31 3a 6e 4b 65 79 32 29 3b 0a 20 20 55  Key1:nKey2);.  U
338d0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
338d1 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28 20  NotUsed);.  if( 
338d2 30 3d 3d 72 20 29 7b 0a 20 20 20 20 72 20 3d 20  0==r ){.    r = 
338d3 6e 4b 65 79 31 2d 6e 4b 65 79 32 3b 0a 20 20 7d  nKey1-nKey2;.  }
338d4 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a 0a  .  return r;.}..
338d5 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
338d6 20 52 4f 57 49 44 20 6f 66 20 74 68 65 20 6d 6f   ROWID of the mo
338d7 73 74 20 72 65 63 65 6e 74 20 69 6e 73 65 72 74  st recent insert
338d8 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  .*/.SQLITE_API s
338d9 71 6c 69 74 65 5f 69 6e 74 36 34 20 73 71 6c 69  qlite_int64 sqli
338da 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f  te3_last_insert_
338db 72 6f 77 69 64 28 73 71 6c 69 74 65 33 20 2a 64  rowid(sqlite3 *d
338dc 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 2d  b){.  return db-
338dd 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 7d 0a 0a 2f  >lastRowid;.}../
338de 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
338df 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 6e 67 65  number of change
338e0 73 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65  s in the most re
338e1 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c  cent call to sql
338e2 69 74 65 33 5f 65 78 65 63 28 29 2e 0a 2a 2f 0a  ite3_exec()..*/.
338e3 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
338e4 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 73  qlite3_changes(s
338e5 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 72  qlite3 *db){.  r
338e6 65 74 75 72 6e 20 64 62 2d 3e 6e 43 68 61 6e 67  eturn db->nChang
338e7 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  e;.}../*.** Retu
338e8 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
338e9 20 63 68 61 6e 67 65 73 20 73 69 6e 63 65 20 74   changes since t
338ea 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
338eb 6c 65 20 77 61 73 20 6f 70 65 6e 65 64 2e 0a 2a  le was opened..*
338ec 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
338ed 20 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63   sqlite3_total_c
338ee 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33 20 2a  hanges(sqlite3 *
338ef 64 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62  db){.  return db
338f0 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b 0a  ->nTotalChange;.
338f1 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61  }../*.** Close a
338f2 6c 6c 20 6f 70 65 6e 20 73 61 76 65 70 6f 69 6e  ll open savepoin
338f3 74 73 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ts. This functio
338f4 6e 20 6f 6e 6c 79 20 6d 61 6e 69 70 75 6c 61 74  n only manipulat
338f5 65 73 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65  es fields of the
338f6 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 68 61 6e  .** database han
338f7 64 6c 65 20 6f 62 6a 65 63 74 2c 20 69 74 20 64  dle object, it d
338f8 6f 65 73 20 6e 6f 74 20 63 6c 6f 73 65 20 61 6e  oes not close an
338f9 79 20 73 61 76 65 70 6f 69 6e 74 73 20 74 68 61  y savepoints tha
338fa 74 20 6d 61 79 20 62 65 20 6f 70 65 6e 0a 2a 2a  t may be open.**
338fb 20 61 74 20 74 68 65 20 62 2d 74 72 65 65 2f 70   at the b-tree/p
338fc 61 67 65 72 20 6c 65 76 65 6c 2e 0a 2a 2f 0a 53  ager level..*/.S
338fd 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
338fe 69 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 53  id sqlite3CloseS
338ff 61 76 65 70 6f 69 6e 74 73 28 73 71 6c 69 74 65  avepoints(sqlite
33900 33 20 2a 64 62 29 7b 0a 20 20 77 68 69 6c 65 28  3 *db){.  while(
33901 20 64 62 2d 3e 70 53 61 76 65 70 6f 69 6e 74 20   db->pSavepoint 
33902 29 7b 0a 20 20 20 20 53 61 76 65 70 6f 69 6e 74  ){.    Savepoint
33903 20 2a 70 54 6d 70 20 3d 20 64 62 2d 3e 70 53 61   *pTmp = db->pSa
33904 76 65 70 6f 69 6e 74 3b 0a 20 20 20 20 64 62 2d  vepoint;.    db-
33905 3e 70 53 61 76 65 70 6f 69 6e 74 20 3d 20 70 54  >pSavepoint = pT
33906 6d 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73  mp->pNext;.    s
33907 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
33908 20 70 54 6d 70 29 3b 0a 20 20 7d 0a 20 20 64 62   pTmp);.  }.  db
33909 2d 3e 6e 53 61 76 65 70 6f 69 6e 74 20 3d 20 30  ->nSavepoint = 0
3390a 3b 0a 20 20 64 62 2d 3e 6e 53 74 61 74 65 6d 65  ;.  db->nStateme
3390b 6e 74 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 69 73  nt = 0;.  db->is
3390c 54 72 61 6e 73 61 63 74 69 6f 6e 53 61 76 65 70  TransactionSavep
3390d 6f 69 6e 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  oint = 0;.}../*.
3390e 2a 2a 20 43 6c 6f 73 65 20 61 6e 20 65 78 69 73  ** Close an exis
3390f 74 69 6e 67 20 53 51 4c 69 74 65 20 64 61 74 61  ting SQLite data
33910 62 61 73 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  base.*/.SQLITE_A
33911 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
33912 6c 6f 73 65 28 73 71 6c 69 74 65 33 20 2a 64 62  lose(sqlite3 *db
33913 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 69  ){.  HashElem *i
33914 3b 0a 20 20 69 6e 74 20 6a 3b 0a 0a 20 20 69 66  ;.  int j;..  if
33915 28 20 21 64 62 20 29 7b 0a 20 20 20 20 72 65 74  ( !db ){.    ret
33916 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
33917 20 7d 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65   }.  if( !sqlite
33918 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b  3SafetyCheckSick
33919 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20 20  OrOk(db) ){.    
3391a 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
3391b 53 55 53 45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  SUSE;.  }.  sqli
3391c 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
3391d 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 0a 20 20 73  db->mutex);..  s
3391e 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
3391f 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29  nalSchema(db, 0)
33920 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 20 74 72 61  ;..  /* If a tra
33921 6e 73 61 63 74 69 6f 6e 20 69 73 20 6f 70 65 6e  nsaction is open
33922 2c 20 74 68 65 20 52 65 73 65 74 49 6e 74 65 72  , the ResetInter
33923 6e 61 6c 53 63 68 65 6d 61 28 29 20 63 61 6c 6c  nalSchema() call
33924 20 61 62 6f 76 65 0a 20 20 2a 2a 20 77 69 6c 6c   above.  ** will
33925 20 6e 6f 74 20 68 61 76 65 20 63 61 6c 6c 65 64   not have called
33926 20 74 68 65 20 78 44 69 73 63 6f 6e 6e 65 63 74   the xDisconnect
33927 28 29 20 6d 65 74 68 6f 64 20 6f 6e 20 61 6e 79  () method on any
33928 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a 20 74 61   virtual.  ** ta
33929 62 6c 65 73 20 69 6e 20 74 68 65 20 64 62 2d 3e  bles in the db->
3392a 61 56 54 72 61 6e 73 5b 5d 20 61 72 72 61 79 2e  aVTrans[] array.
3392b 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   The following s
3392c 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62 61  qlite3VtabRollba
3392d 63 6b 28 29 0a 20 20 2a 2a 20 63 61 6c 6c 20 77  ck().  ** call w
3392e 69 6c 6c 20 64 6f 20 73 6f 2e 20 57 65 20 6e 65  ill do so. We ne
3392f 65 64 20 74 6f 20 64 6f 20 74 68 69 73 20 62 65  ed to do this be
33930 66 6f 72 65 20 74 68 65 20 63 68 65 63 6b 20 66  fore the check f
33931 6f 72 20 61 63 74 69 76 65 0a 20 20 2a 2a 20 53  or active.  ** S
33932 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 62 65  QL statements be
33933 6c 6f 77 2c 20 61 73 20 74 68 65 20 76 2d 74 61  low, as the v-ta
33934 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ble implementati
33935 6f 6e 20 6d 61 79 20 62 65 20 73 74 6f 72 69 6e  on may be storin
33936 67 0a 20 20 2a 2a 20 73 6f 6d 65 20 70 72 65 70  g.  ** some prep
33937 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20  ared statements 
33938 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 2a 2f  internally..  */
33939 0a 20 20 73 71 6c 69 74 65 33 56 74 61 62 52 6f  .  sqlite3VtabRo
3393a 6c 6c 62 61 63 6b 28 64 62 29 3b 0a 0a 20 20 2f  llback(db);..  /
3393b 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20 61  * If there are a
3393c 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 56  ny outstanding V
3393d 4d 73 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  Ms, return SQLIT
3393e 45 5f 42 55 53 59 2e 20 2a 2f 0a 20 20 69 66 28  E_BUSY. */.  if(
3393f 20 64 62 2d 3e 70 56 64 62 65 20 29 7b 0a 20 20   db->pVdbe ){.  
33940 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
33941 62 2c 20 53 51 4c 49 54 45 5f 42 55 53 59 2c 20  b, SQLITE_BUSY, 
33942 0a 20 20 20 20 20 20 20 20 22 75 6e 61 62 6c 65  .        "unable
33943 20 74 6f 20 63 6c 6f 73 65 20 64 75 65 20 74 6f   to close due to
33944 20 75 6e 66 69 6e 61 6c 69 73 65 64 20 73 74 61   unfinalised sta
33945 74 65 6d 65 6e 74 73 22 29 3b 0a 20 20 20 20 73  tements");.    s
33946 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
33947 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
33948 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
33949 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 61 73 73  _BUSY;.  }.  ass
3394a 65 72 74 28 20 73 71 6c 69 74 65 33 53 61 66 65  ert( sqlite3Safe
3394b 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28  tyCheckSickOrOk(
3394c 64 62 29 20 29 3b 0a 0a 20 20 66 6f 72 28 6a 3d  db) );..  for(j=
3394d 30 3b 20 6a 3c 64 62 2d 3e 6e 44 62 3b 20 6a 2b  0; j<db->nDb; j+
3394e 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70  +){.    Btree *p
3394f 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 6a 5d 2e  Bt = db->aDb[j].
33950 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 42 74  pBt;.    if( pBt
33951 20 26 26 20 73 71 6c 69 74 65 33 42 74 72 65 65   && sqlite3Btree
33952 49 73 49 6e 42 61 63 6b 75 70 28 70 42 74 29 20  IsInBackup(pBt) 
33953 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
33954 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45  Error(db, SQLITE
33955 5f 42 55 53 59 2c 20 0a 20 20 20 20 20 20 20 20  _BUSY, .        
33956 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 63 6c 6f    "unable to clo
33957 73 65 20 64 75 65 20 74 6f 20 75 6e 66 69 6e 69  se due to unfini
33958 73 68 65 64 20 62 61 63 6b 75 70 20 6f 70 65 72  shed backup oper
33959 61 74 69 6f 6e 22 29 3b 0a 20 20 20 20 20 20 73  ation");.      s
3395a 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
3395b 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
3395c 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
3395d 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20  TE_BUSY;.    }. 
3395e 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e   }..  /* Free an
3395f 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 53 61  y outstanding Sa
33960 76 65 70 6f 69 6e 74 20 73 74 72 75 63 74 75 72  vepoint structur
33961 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  es. */.  sqlite3
33962 43 6c 6f 73 65 53 61 76 65 70 6f 69 6e 74 73 28  CloseSavepoints(
33963 64 62 29 3b 0a 0a 20 20 66 6f 72 28 6a 3d 30 3b  db);..  for(j=0;
33964 20 6a 3c 64 62 2d 3e 6e 44 62 3b 20 6a 2b 2b 29   j<db->nDb; j++)
33965 7b 0a 20 20 20 20 73 74 72 75 63 74 20 44 62 20  {.    struct Db 
33966 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b  *pDb = &db->aDb[
33967 6a 5d 3b 0a 20 20 20 20 69 66 28 20 70 44 62 2d  j];.    if( pDb-
33968 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 73 71  >pBt ){.      sq
33969 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28  lite3BtreeClose(
3396a 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20  pDb->pBt);.     
3396b 20 70 44 62 2d 3e 70 42 74 20 3d 20 30 3b 0a 20   pDb->pBt = 0;. 
3396c 20 20 20 20 20 69 66 28 20 6a 21 3d 31 20 29 7b       if( j!=1 ){
3396d 0a 20 20 20 20 20 20 20 20 70 44 62 2d 3e 70 53  .        pDb->pS
3396e 63 68 65 6d 61 20 3d 20 30 3b 0a 20 20 20 20 20  chema = 0;.     
3396f 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73   }.    }.  }.  s
33970 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
33971 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29  nalSchema(db, 0)
33972 3b 0a 0a 20 20 2f 2a 20 54 65 6c 6c 20 74 68 65  ;..  /* Tell the
33973 20 63 6f 64 65 20 69 6e 20 6e 6f 74 69 66 79 2e   code in notify.
33974 63 20 74 68 61 74 20 74 68 65 20 63 6f 6e 6e 65  c that the conne
33975 63 74 69 6f 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20  ction no longer 
33976 68 6f 6c 64 73 20 61 6e 79 0a 20 20 2a 2a 20 6c  holds any.  ** l
33977 6f 63 6b 73 20 61 6e 64 20 64 6f 65 73 20 6e 6f  ocks and does no
33978 74 20 72 65 71 75 69 72 65 20 61 6e 79 20 66 75  t require any fu
33979 72 74 68 65 72 20 75 6e 6c 6f 63 6b 2d 6e 6f 74  rther unlock-not
3397a 69 66 79 20 63 61 6c 6c 62 61 63 6b 73 2e 0a 20  ify callbacks.. 
3397b 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 43 6f 6e   */.  sqlite3Con
3397c 6e 65 63 74 69 6f 6e 43 6c 6f 73 65 64 28 64 62  nectionClosed(db
3397d 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62  );..  assert( db
3397e 2d 3e 6e 44 62 3c 3d 32 20 29 3b 0a 20 20 61 73  ->nDb<=2 );.  as
3397f 73 65 72 74 28 20 64 62 2d 3e 61 44 62 3d 3d 64  sert( db->aDb==d
33980 62 2d 3e 61 44 62 53 74 61 74 69 63 20 29 3b 0a  b->aDbStatic );.
33981 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 41 72 72    for(j=0; j<Arr
33982 61 79 53 69 7a 65 28 64 62 2d 3e 61 46 75 6e 63  aySize(db->aFunc
33983 2e 61 29 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 46  .a); j++){.    F
33984 75 6e 63 44 65 66 20 2a 70 4e 65 78 74 2c 20 2a  uncDef *pNext, *
33985 70 48 61 73 68 2c 20 2a 70 3b 0a 20 20 20 20 66  pHash, *p;.    f
33986 6f 72 28 70 3d 64 62 2d 3e 61 46 75 6e 63 2e 61  or(p=db->aFunc.a
33987 5b 6a 5d 3b 20 70 3b 20 70 3d 70 48 61 73 68 29  [j]; p; p=pHash)
33988 7b 0a 20 20 20 20 20 20 70 48 61 73 68 20 3d 20  {.      pHash = 
33989 70 2d 3e 70 48 61 73 68 3b 0a 20 20 20 20 20 20  p->pHash;.      
3398a 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20  while( p ){.    
3398b 20 20 20 20 70 4e 65 78 74 20 3d 20 70 2d 3e 70      pNext = p->p
3398c 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 73 71  Next;.        sq
3398d 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
3398e 70 29 3b 0a 20 20 20 20 20 20 20 20 70 20 3d 20  p);.        p = 
3398f 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20  pNext;.      }. 
33990 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69     }.  }.  for(i
33991 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
33992 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b  (&db->aCollSeq);
33993 20 69 3b 20 69 3d 73 71 6c 69 74 65 48 61 73 68   i; i=sqliteHash
33994 4e 65 78 74 28 69 29 29 7b 0a 20 20 20 20 43 6f  Next(i)){.    Co
33995 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 28  llSeq *pColl = (
33996 43 6f 6c 6c 53 65 71 20 2a 29 73 71 6c 69 74 65  CollSeq *)sqlite
33997 48 61 73 68 44 61 74 61 28 69 29 3b 0a 20 20 20  HashData(i);.   
33998 20 2f 2a 20 49 6e 76 6f 6b 65 20 61 6e 79 20 64   /* Invoke any d
33999 65 73 74 72 75 63 74 6f 72 73 20 72 65 67 69 73  estructors regis
3399a 74 65 72 65 64 20 66 6f 72 20 63 6f 6c 6c 61 74  tered for collat
3399b 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 75 73 65  ion sequence use
3399c 72 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20 20 66  r data. */.    f
3399d 6f 72 28 6a 3d 30 3b 20 6a 3c 33 3b 20 6a 2b 2b  or(j=0; j<3; j++
3399e 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 6f  ){.      if( pCo
3399f 6c 6c 5b 6a 5d 2e 78 44 65 6c 20 29 7b 0a 20 20  ll[j].xDel ){.  
339a0 20 20 20 20 20 20 70 43 6f 6c 6c 5b 6a 5d 2e 78        pColl[j].x
339a1 44 65 6c 28 70 43 6f 6c 6c 5b 6a 5d 2e 70 55 73  Del(pColl[j].pUs
339a2 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  er);.      }.   
339a3 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62   }.    sqlite3Db
339a4 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 6c 29 3b  Free(db, pColl);
339a5 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 48 61  .  }.  sqlite3Ha
339a6 73 68 43 6c 65 61 72 28 26 64 62 2d 3e 61 43 6f  shClear(&db->aCo
339a7 6c 6c 53 65 71 29 3b 0a 23 69 66 6e 64 65 66 20  llSeq);.#ifndef 
339a8 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
339a9 55 41 4c 54 41 42 4c 45 0a 20 20 66 6f 72 28 69  UALTABLE.  for(i
339aa 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
339ab 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 29 3b 20  (&db->aModule); 
339ac 69 3b 20 69 3d 73 71 6c 69 74 65 48 61 73 68 4e  i; i=sqliteHashN
339ad 65 78 74 28 69 29 29 7b 0a 20 20 20 20 4d 6f 64  ext(i)){.    Mod
339ae 75 6c 65 20 2a 70 4d 6f 64 20 3d 20 28 4d 6f 64  ule *pMod = (Mod
339af 75 6c 65 20 2a 29 73 71 6c 69 74 65 48 61 73 68  ule *)sqliteHash
339b0 44 61 74 61 28 69 29 3b 0a 20 20 20 20 69 66 28  Data(i);.    if(
339b1 20 70 4d 6f 64 2d 3e 78 44 65 73 74 72 6f 79 20   pMod->xDestroy 
339b2 29 7b 0a 20 20 20 20 20 20 70 4d 6f 64 2d 3e 78  ){.      pMod->x
339b3 44 65 73 74 72 6f 79 28 70 4d 6f 64 2d 3e 70 41  Destroy(pMod->pA
339b4 75 78 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  ux);.    }.    s
339b5 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
339b6 20 70 4d 6f 64 29 3b 0a 20 20 7d 0a 20 20 73 71   pMod);.  }.  sq
339b7 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 26  lite3HashClear(&
339b8 64 62 2d 3e 61 4d 6f 64 75 6c 65 29 3b 0a 23 65  db->aModule);.#e
339b9 6e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 45  ndif..  sqlite3E
339ba 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f  rror(db, SQLITE_
339bb 4f 4b 2c 20 30 29 3b 20 2f 2a 20 44 65 61 6c 6c  OK, 0); /* Deall
339bc 6f 63 61 74 65 73 20 61 6e 79 20 63 61 63 68 65  ocates any cache
339bd 64 20 65 72 72 6f 72 20 73 74 72 69 6e 67 73 2e  d error strings.
339be 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 70 45   */.  if( db->pE
339bf 72 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  rr ){.    sqlite
339c0 33 56 61 6c 75 65 46 72 65 65 28 64 62 2d 3e 70  3ValueFree(db->p
339c1 45 72 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  Err);.  }.  sqli
339c2 74 65 33 43 6c 6f 73 65 45 78 74 65 6e 73 69 6f  te3CloseExtensio
339c3 6e 73 28 64 62 29 3b 0a 0a 20 20 64 62 2d 3e 6d  ns(db);..  db->m
339c4 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41  agic = SQLITE_MA
339c5 47 49 43 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f 2a  GIC_ERROR;..  /*
339c6 20 54 68 65 20 74 65 6d 70 2d 64 61 74 61 62 61   The temp-databa
339c7 73 65 20 73 63 68 65 6d 61 20 69 73 20 61 6c 6c  se schema is all
339c8 6f 63 61 74 65 64 20 64 69 66 66 65 72 65 6e 74  ocated different
339c9 6c 79 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65  ly from the othe
339ca 72 20 73 63 68 65 6d 61 0a 20 20 2a 2a 20 6f 62  r schema.  ** ob
339cb 6a 65 63 74 73 20 28 75 73 69 6e 67 20 73 71 6c  jects (using sql
339cc 69 74 65 4d 61 6c 6c 6f 63 28 29 20 64 69 72 65  iteMalloc() dire
339cd 63 74 6c 79 2c 20 69 6e 73 74 65 61 64 20 6f 66  ctly, instead of
339ce 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68   sqlite3BtreeSch
339cf 65 6d 61 28 29 29 2e 0a 20 20 2a 2a 20 53 6f 20  ema())..  ** So 
339d0 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 66  it needs to be f
339d1 72 65 65 64 20 68 65 72 65 2e 20 54 6f 64 6f 3a  reed here. Todo:
339d2 20 57 68 79 20 6e 6f 74 20 72 6f 6c 6c 20 74 68   Why not roll th
339d3 65 20 74 65 6d 70 20 73 63 68 65 6d 61 20 69 6e  e temp schema in
339d4 74 6f 0a 20 20 2a 2a 20 74 68 65 20 73 61 6d 65  to.  ** the same
339d5 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20   sqliteMalloc() 
339d6 61 73 20 74 68 65 20 6f 6e 65 20 74 68 61 74 20  as the one that 
339d7 61 6c 6c 6f 63 61 74 65 73 20 74 68 65 20 64 61  allocates the da
339d8 74 61 62 61 73 65 20 0a 20 20 2a 2a 20 73 74 72  tabase .  ** str
339d9 75 63 74 75 72 65 3f 0a 20 20 2a 2f 0a 20 20 73  ucture?.  */.  s
339da 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
339db 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68   db->aDb[1].pSch
339dc 65 6d 61 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ema);.  sqlite3_
339dd 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
339de 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 6d 61  mutex);.  db->ma
339df 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47  gic = SQLITE_MAG
339e0 49 43 5f 43 4c 4f 53 45 44 3b 0a 20 20 73 71 6c  IC_CLOSED;.  sql
339e1 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28  ite3_mutex_free(
339e2 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73  db->mutex);.  as
339e3 73 65 72 74 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73  sert( db->lookas
339e4 69 64 65 2e 6e 4f 75 74 3d 3d 30 20 29 3b 20 20  ide.nOut==0 );  
339e5 2f 2a 20 46 61 69 6c 73 20 6f 6e 20 61 20 6c 6f  /* Fails on a lo
339e6 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 6c  okaside memory l
339e7 65 61 6b 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d  eak */.  if( db-
339e8 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c  >lookaside.bMall
339e9 6f 63 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69  oced ){.    sqli
339ea 74 65 33 5f 66 72 65 65 28 64 62 2d 3e 6c 6f 6f  te3_free(db->loo
339eb 6b 61 73 69 64 65 2e 70 53 74 61 72 74 29 3b 0a  kaside.pStart);.
339ec 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72    }.  sqlite3_fr
339ed 65 65 28 64 62 29 3b 0a 20 20 72 65 74 75 72 6e  ee(db);.  return
339ee 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
339ef 2a 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6c  *.** Rollback al
339f0 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 73  l database files
339f1 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
339f2 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
339f3 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 73 71 6c 69  RollbackAll(sqli
339f4 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20  te3 *db){.  int 
339f5 69 3b 0a 20 20 69 6e 74 20 69 6e 54 72 61 6e 73  i;.  int inTrans
339f6 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
339f7 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
339f8 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
339f9 0a 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42  .  sqlite3BeginB
339fa 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
339fb 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e   for(i=0; i<db->
339fc 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  nDb; i++){.    i
339fd 66 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42  f( db->aDb[i].pB
339fe 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  t ){.      if( s
339ff 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54  qlite3BtreeIsInT
33a00 72 61 6e 73 28 64 62 2d 3e 61 44 62 5b 69 5d 2e  rans(db->aDb[i].
33a01 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20  pBt) ){.        
33a02 69 6e 54 72 61 6e 73 20 3d 20 31 3b 0a 20 20 20  inTrans = 1;.   
33a03 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
33a04 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28  e3BtreeRollback(
33a05 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 29 3b  db->aDb[i].pBt);
33a06 0a 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69  .      db->aDb[i
33a07 5d 2e 69 6e 54 72 61 6e 73 20 3d 20 30 3b 0a 20  ].inTrans = 0;. 
33a08 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
33a09 65 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 64  e3VtabRollback(d
33a0a 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 6e 64  b);.  sqlite3End
33a0b 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
33a0c 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73  .  if( db->flags
33a0d 26 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68  &SQLITE_InternCh
33a0e 61 6e 67 65 73 20 29 7b 0a 20 20 20 20 73 71 6c  anges ){.    sql
33a0f 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72  ite3ExpirePrepar
33a10 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29  edStatements(db)
33a11 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 73  ;.    sqlite3Res
33a12 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
33a13 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20  (db, 0);.  }..  
33a14 2f 2a 20 41 6e 79 20 64 65 66 65 72 72 65 64 20  /* Any deferred 
33a15 63 6f 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61  constraint viola
33a16 74 69 6f 6e 73 20 68 61 76 65 20 6e 6f 77 20 62  tions have now b
33a17 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 20 2a 2f  een resolved. */
33a18 0a 20 20 64 62 2d 3e 6e 44 65 66 65 72 72 65 64  .  db->nDeferred
33a19 43 6f 6e 73 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20  Cons = 0;..  /* 
33a1a 49 66 20 6f 6e 65 20 68 61 73 20 62 65 65 6e 20  If one has been 
33a1b 63 6f 6e 66 69 67 75 72 65 64 2c 20 69 6e 76 6f  configured, invo
33a1c 6b 65 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 2d  ke the rollback-
33a1d 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b 20 2a 2f  hook callback */
33a1e 0a 20 20 69 66 28 20 64 62 2d 3e 78 52 6f 6c 6c  .  if( db->xRoll
33a1f 62 61 63 6b 43 61 6c 6c 62 61 63 6b 20 26 26 20  backCallback && 
33a20 28 69 6e 54 72 61 6e 73 20 7c 7c 20 21 64 62 2d  (inTrans || !db-
33a21 3e 61 75 74 6f 43 6f 6d 6d 69 74 29 20 29 7b 0a  >autoCommit) ){.
33a22 20 20 20 20 64 62 2d 3e 78 52 6f 6c 6c 62 61 63      db->xRollbac
33a23 6b 43 61 6c 6c 62 61 63 6b 28 64 62 2d 3e 70 52  kCallback(db->pR
33a24 6f 6c 6c 62 61 63 6b 41 72 67 29 3b 0a 20 20 7d  ollbackArg);.  }
33a25 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
33a26 20 61 20 73 74 61 74 69 63 20 73 74 72 69 6e 67   a static string
33a27 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20   that describes 
33a28 74 68 65 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f  the kind of erro
33a29 72 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74  r specified in t
33a2a 68 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e 0a  he.** argument..
33a2b 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
33a2c 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  E const char *sq
33a2d 6c 69 74 65 33 45 72 72 53 74 72 28 69 6e 74 20  lite3ErrStr(int 
33a2e 72 63 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f  rc){.  static co
33a2f 6e 73 74 20 63 68 61 72 2a 20 63 6f 6e 73 74 20  nst char* const 
33a30 61 4d 73 67 5b 5d 20 3d 20 7b 0a 20 20 20 20 2f  aMsg[] = {.    /
33a31 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20 20  * SQLITE_OK     
33a32 20 20 20 20 20 2a 2f 20 22 6e 6f 74 20 61 6e 20       */ "not an 
33a33 65 72 72 6f 72 22 2c 0a 20 20 20 20 2f 2a 20 53  error",.    /* S
33a34 51 4c 49 54 45 5f 45 52 52 4f 52 20 20 20 20 20  QLITE_ERROR     
33a35 20 20 2a 2f 20 22 53 51 4c 20 6c 6f 67 69 63 20    */ "SQL logic 
33a36 65 72 72 6f 72 20 6f 72 20 6d 69 73 73 69 6e 67  error or missing
33a37 20 64 61 74 61 62 61 73 65 22 2c 0a 20 20 20 20   database",.    
33a38 2f 2a 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 4e  /* SQLITE_INTERN
33a39 41 4c 20 20 20 20 2a 2f 20 30 2c 0a 20 20 20 20  AL    */ 0,.    
33a3a 2f 2a 20 53 51 4c 49 54 45 5f 50 45 52 4d 20 20  /* SQLITE_PERM  
33a3b 20 20 20 20 20 20 2a 2f 20 22 61 63 63 65 73 73        */ "access
33a3c 20 70 65 72 6d 69 73 73 69 6f 6e 20 64 65 6e 69   permission deni
33a3d 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49  ed",.    /* SQLI
33a3e 54 45 5f 41 42 4f 52 54 20 20 20 20 20 20 20 2a  TE_ABORT       *
33a3f 2f 20 22 63 61 6c 6c 62 61 63 6b 20 72 65 71 75  / "callback requ
33a40 65 73 74 65 64 20 71 75 65 72 79 20 61 62 6f 72  ested query abor
33a41 74 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54  t",.    /* SQLIT
33a42 45 5f 42 55 53 59 20 20 20 20 20 20 20 20 2a 2f  E_BUSY        */
33a43 20 22 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f   "database is lo
33a44 63 6b 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53 51  cked",.    /* SQ
33a45 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 20 20 20 20  LITE_LOCKED     
33a46 20 2a 2f 20 22 64 61 74 61 62 61 73 65 20 74 61   */ "database ta
33a47 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 22 2c 0a  ble is locked",.
33a48 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4e 4f      /* SQLITE_NO
33a49 4d 45 4d 20 20 20 20 20 20 20 2a 2f 20 22 6f 75  MEM       */ "ou
33a4a 74 20 6f 66 20 6d 65 6d 6f 72 79 22 2c 0a 20 20  t of memory",.  
33a4b 20 20 2f 2a 20 53 51 4c 49 54 45 5f 52 45 41 44    /* SQLITE_READ
33a4c 4f 4e 4c 59 20 20 20 20 2a 2f 20 22 61 74 74 65  ONLY    */ "atte
33a4d 6d 70 74 20 74 6f 20 77 72 69 74 65 20 61 20 72  mpt to write a r
33a4e 65 61 64 6f 6e 6c 79 20 64 61 74 61 62 61 73 65  eadonly database
33a4f 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45  ",.    /* SQLITE
33a50 5f 49 4e 54 45 52 52 55 50 54 20 20 20 2a 2f 20  _INTERRUPT   */ 
33a51 22 69 6e 74 65 72 72 75 70 74 65 64 22 2c 0a 20  "interrupted",. 
33a52 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 49 4f 45     /* SQLITE_IOE
33a53 52 52 20 20 20 20 20 20 20 2a 2f 20 22 64 69 73  RR       */ "dis
33a54 6b 20 49 2f 4f 20 65 72 72 6f 72 22 2c 0a 20 20  k I/O error",.  
33a55 20 20 2f 2a 20 53 51 4c 49 54 45 5f 43 4f 52 52    /* SQLITE_CORR
33a56 55 50 54 20 20 20 20 20 2a 2f 20 22 64 61 74 61  UPT     */ "data
33a57 62 61 73 65 20 64 69 73 6b 20 69 6d 61 67 65 20  base disk image 
33a58 69 73 20 6d 61 6c 66 6f 72 6d 65 64 22 2c 0a 20  is malformed",. 
33a59 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4e 4f 54     /* SQLITE_NOT
33a5a 46 4f 55 4e 44 20 20 20 20 2a 2f 20 30 2c 0a 20  FOUND    */ 0,. 
33a5b 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 46 55 4c     /* SQLITE_FUL
33a5c 4c 20 20 20 20 20 20 20 20 2a 2f 20 22 64 61 74  L        */ "dat
33a5d 61 62 61 73 65 20 6f 72 20 64 69 73 6b 20 69 73  abase or disk is
33a5e 20 66 75 6c 6c 22 2c 0a 20 20 20 20 2f 2a 20 53   full",.    /* S
33a5f 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 20 20  QLITE_CANTOPEN  
33a60 20 20 2a 2f 20 22 75 6e 61 62 6c 65 20 74 6f 20    */ "unable to 
33a61 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 66 69  open database fi
33a62 6c 65 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49  le",.    /* SQLI
33a63 54 45 5f 50 52 4f 54 4f 43 4f 4c 20 20 20 20 2a  TE_PROTOCOL    *
33a64 2f 20 30 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49  / 0,.    /* SQLI
33a65 54 45 5f 45 4d 50 54 59 20 20 20 20 20 20 20 2a  TE_EMPTY       *
33a66 2f 20 22 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  / "table contain
33a67 73 20 6e 6f 20 64 61 74 61 22 2c 0a 20 20 20 20  s no data",.    
33a68 2f 2a 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  /* SQLITE_SCHEMA
33a69 20 20 20 20 20 20 2a 2f 20 22 64 61 74 61 62 61        */ "databa
33a6a 73 65 20 73 63 68 65 6d 61 20 68 61 73 20 63 68  se schema has ch
33a6b 61 6e 67 65 64 22 2c 0a 20 20 20 20 2f 2a 20 53  anged",.    /* S
33a6c 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 20 20 20  QLITE_TOOBIG    
33a6d 20 20 2a 2f 20 22 73 74 72 69 6e 67 20 6f 72 20    */ "string or 
33a6e 62 6c 6f 62 20 74 6f 6f 20 62 69 67 22 2c 0a 20  blob too big",. 
33a6f 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 43 4f 4e     /* SQLITE_CON
33a70 53 54 52 41 49 4e 54 20 20 2a 2f 20 22 63 6f 6e  STRAINT  */ "con
33a71 73 74 72 61 69 6e 74 20 66 61 69 6c 65 64 22 2c  straint failed",
33a72 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4d  .    /* SQLITE_M
33a73 49 53 4d 41 54 43 48 20 20 20 20 2a 2f 20 22 64  ISMATCH    */ "d
33a74 61 74 61 74 79 70 65 20 6d 69 73 6d 61 74 63 68  atatype mismatch
33a75 22 2c 0a 20 20 20 20 2f 2a 20 53 51 4c 49 54 45  ",.    /* SQLITE
33a76 5f 4d 49 53 55 53 45 20 20 20 20 20 20 2a 2f 20  _MISUSE      */ 
33a77 22 6c 69 62 72 61 72 79 20 72 6f 75 74 69 6e 65  "library routine
33a78 20 63 61 6c 6c 65 64 20 6f 75 74 20 6f 66 20 73   called out of s
33a79 65 71 75 65 6e 63 65 22 2c 0a 20 20 20 20 2f 2a  equence",.    /*
33a7a 20 53 51 4c 49 54 45 5f 4e 4f 4c 46 53 20 20 20   SQLITE_NOLFS   
33a7b 20 20 20 20 2a 2f 20 22 6c 61 72 67 65 20 66 69      */ "large fi
33a7c 6c 65 20 73 75 70 70 6f 72 74 20 69 73 20 64 69  le support is di
33a7d 73 61 62 6c 65 64 22 2c 0a 20 20 20 20 2f 2a 20  sabled",.    /* 
33a7e 53 51 4c 49 54 45 5f 41 55 54 48 20 20 20 20 20  SQLITE_AUTH     
33a7f 20 20 20 2a 2f 20 22 61 75 74 68 6f 72 69 7a 61     */ "authoriza
33a80 74 69 6f 6e 20 64 65 6e 69 65 64 22 2c 0a 20 20  tion denied",.  
33a81 20 20 2f 2a 20 53 51 4c 49 54 45 5f 46 4f 52 4d    /* SQLITE_FORM
33a82 41 54 20 20 20 20 20 20 2a 2f 20 22 61 75 78 69  AT      */ "auxi
33a83 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66  liary database f
33a84 6f 72 6d 61 74 20 65 72 72 6f 72 22 2c 0a 20 20  ormat error",.  
33a85 20 20 2f 2a 20 53 51 4c 49 54 45 5f 52 41 4e 47    /* SQLITE_RANG
33a86 45 20 20 20 20 20 20 20 2a 2f 20 22 62 69 6e 64  E       */ "bind
33a87 20 6f 72 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78   or column index
33a88 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 22 2c 0a   out of range",.
33a89 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 4e 4f      /* SQLITE_NO
33a8a 54 41 44 42 20 20 20 20 20 20 2a 2f 20 22 66 69  TADB      */ "fi
33a8b 6c 65 20 69 73 20 65 6e 63 72 79 70 74 65 64 20  le is encrypted 
33a8c 6f 72 20 69 73 20 6e 6f 74 20 61 20 64 61 74 61  or is not a data
33a8d 62 61 73 65 22 2c 0a 20 20 7d 3b 0a 20 20 72 63  base",.  };.  rc
33a8e 20 26 3d 20 30 78 66 66 3b 0a 20 20 69 66 28 20   &= 0xff;.  if( 
33a8f 41 4c 57 41 59 53 28 72 63 3e 3d 30 29 20 26 26  ALWAYS(rc>=0) &&
33a90 20 72 63 3c 28 69 6e 74 29 28 73 69 7a 65 6f 66   rc<(int)(sizeof
33a91 28 61 4d 73 67 29 2f 73 69 7a 65 6f 66 28 61 4d  (aMsg)/sizeof(aM
33a92 73 67 5b 30 5d 29 29 20 26 26 20 61 4d 73 67 5b  sg[0])) && aMsg[
33a93 72 63 5d 21 3d 30 20 29 7b 0a 20 20 20 20 72 65  rc]!=0 ){.    re
33a94 74 75 72 6e 20 61 4d 73 67 5b 72 63 5d 3b 0a 20  turn aMsg[rc];. 
33a95 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
33a96 72 6e 20 22 75 6e 6b 6e 6f 77 6e 20 65 72 72 6f  rn "unknown erro
33a97 72 22 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  r";.  }.}../*.**
33a98 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d   This routine im
33a99 70 6c 65 6d 65 6e 74 73 20 61 20 62 75 73 79 20  plements a busy 
33a9a 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20 73 6c  callback that sl
33a9b 65 65 70 73 20 61 6e 64 20 74 72 69 65 73 0a 2a  eeps and tries.*
33a9c 2a 20 61 67 61 69 6e 20 75 6e 74 69 6c 20 61 20  * again until a 
33a9d 74 69 6d 65 6f 75 74 20 76 61 6c 75 65 20 69 73  timeout value is
33a9e 20 72 65 61 63 68 65 64 2e 20 20 54 68 65 20 74   reached.  The t
33a9f 69 6d 65 6f 75 74 20 76 61 6c 75 65 20 69 73 0a  imeout value is.
33aa0 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 20 6e 75  ** an integer nu
33aa1 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63  mber of millisec
33aa2 6f 6e 64 73 20 70 61 73 73 65 64 20 69 6e 20 61  onds passed in a
33aa3 73 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61  s the first.** a
33aa4 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74  rgument..*/.stat
33aa5 69 63 20 69 6e 74 20 73 71 6c 69 74 65 44 65 66  ic int sqliteDef
33aa6 61 75 6c 74 42 75 73 79 43 61 6c 6c 62 61 63 6b  aultBusyCallback
33aa7 28 0a 20 76 6f 69 64 20 2a 70 74 72 2c 20 20 20  (. void *ptr,   
33aa8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
33aa9 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
33aaa 6f 6e 20 2a 2f 0a 20 69 6e 74 20 63 6f 75 6e 74  on */. int count
33aab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33aac 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d  /* Number of tim
33aad 65 73 20 74 61 62 6c 65 20 68 61 73 20 62 65 65  es table has bee
33aae 6e 20 62 75 73 79 20 2a 2f 0a 29 7b 0a 23 69 66  n busy */.){.#if
33aaf 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20 7c   SQLITE_OS_WIN |
33ab0 7c 20 28 64 65 66 69 6e 65 64 28 48 41 56 45 5f  | (defined(HAVE_
33ab1 55 53 4c 45 45 50 29 20 26 26 20 48 41 56 45 5f  USLEEP) && HAVE_
33ab2 55 53 4c 45 45 50 29 0a 20 20 73 74 61 74 69 63  USLEEP).  static
33ab3 20 63 6f 6e 73 74 20 75 38 20 64 65 6c 61 79 73   const u8 delays
33ab4 5b 5d 20 3d 0a 20 20 20 20 20 7b 20 31 2c 20 32  [] =.     { 1, 2
33ab5 2c 20 35 2c 20 31 30 2c 20 31 35 2c 20 32 30 2c  , 5, 10, 15, 20,
33ab6 20 32 35 2c 20 32 35 2c 20 20 32 35 2c 20 20 35   25, 25,  25,  5
33ab7 30 2c 20 20 35 30 2c 20 31 30 30 20 7d 3b 0a 20  0,  50, 100 };. 
33ab8 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38   static const u8
33ab9 20 74 6f 74 61 6c 73 5b 5d 20 3d 0a 20 20 20 20   totals[] =.    
33aba 20 7b 20 30 2c 20 31 2c 20 33 2c 20 20 38 2c 20   { 0, 1, 3,  8, 
33abb 31 38 2c 20 33 33 2c 20 35 33 2c 20 37 38 2c 20  18, 33, 53, 78, 
33abc 31 30 33 2c 20 31 32 38 2c 20 31 37 38 2c 20 32  103, 128, 178, 2
33abd 32 38 20 7d 3b 0a 23 20 64 65 66 69 6e 65 20 4e  28 };.# define N
33abe 44 45 4c 41 59 20 28 73 69 7a 65 6f 66 28 64 65  DELAY (sizeof(de
33abf 6c 61 79 73 29 2f 73 69 7a 65 6f 66 28 64 65 6c  lays)/sizeof(del
33ac0 61 79 73 5b 30 5d 29 29 0a 20 20 73 71 6c 69 74  ays[0])).  sqlit
33ac1 65 33 20 2a 64 62 20 3d 20 28 73 71 6c 69 74 65  e3 *db = (sqlite
33ac2 33 20 2a 29 70 74 72 3b 0a 20 20 69 6e 74 20 74  3 *)ptr;.  int t
33ac3 69 6d 65 6f 75 74 20 3d 20 64 62 2d 3e 62 75 73  imeout = db->bus
33ac4 79 54 69 6d 65 6f 75 74 3b 0a 20 20 69 6e 74 20  yTimeout;.  int 
33ac5 64 65 6c 61 79 2c 20 70 72 69 6f 72 3b 0a 0a 20  delay, prior;.. 
33ac6 20 61 73 73 65 72 74 28 20 63 6f 75 6e 74 3e 3d   assert( count>=
33ac7 30 20 29 3b 0a 20 20 69 66 28 20 63 6f 75 6e 74  0 );.  if( count
33ac8 20 3c 20 4e 44 45 4c 41 59 20 29 7b 0a 20 20 20   < NDELAY ){.   
33ac9 20 64 65 6c 61 79 20 3d 20 64 65 6c 61 79 73 5b   delay = delays[
33aca 63 6f 75 6e 74 5d 3b 0a 20 20 20 20 70 72 69 6f  count];.    prio
33acb 72 20 3d 20 74 6f 74 61 6c 73 5b 63 6f 75 6e 74  r = totals[count
33acc 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ];.  }else{.    
33acd 64 65 6c 61 79 20 3d 20 64 65 6c 61 79 73 5b 4e  delay = delays[N
33ace 44 45 4c 41 59 2d 31 5d 3b 0a 20 20 20 20 70 72  DELAY-1];.    pr
33acf 69 6f 72 20 3d 20 74 6f 74 61 6c 73 5b 4e 44 45  ior = totals[NDE
33ad0 4c 41 59 2d 31 5d 20 2b 20 64 65 6c 61 79 2a 28  LAY-1] + delay*(
33ad1 63 6f 75 6e 74 2d 28 4e 44 45 4c 41 59 2d 31 29  count-(NDELAY-1)
33ad2 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 72 69  );.  }.  if( pri
33ad3 6f 72 20 2b 20 64 65 6c 61 79 20 3e 20 74 69 6d  or + delay > tim
33ad4 65 6f 75 74 20 29 7b 0a 20 20 20 20 64 65 6c 61  eout ){.    dela
33ad5 79 20 3d 20 74 69 6d 65 6f 75 74 20 2d 20 70 72  y = timeout - pr
33ad6 69 6f 72 3b 0a 20 20 20 20 69 66 28 20 64 65 6c  ior;.    if( del
33ad7 61 79 3c 3d 30 20 29 20 72 65 74 75 72 6e 20 30  ay<=0 ) return 0
33ad8 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 4f  ;.  }.  sqlite3O
33ad9 73 53 6c 65 65 70 28 64 62 2d 3e 70 56 66 73 2c  sSleep(db->pVfs,
33ada 20 64 65 6c 61 79 2a 31 30 30 30 29 3b 0a 20 20   delay*1000);.  
33adb 72 65 74 75 72 6e 20 31 3b 0a 23 65 6c 73 65 0a  return 1;.#else.
33adc 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
33add 28 73 71 6c 69 74 65 33 20 2a 29 70 74 72 3b 0a  (sqlite3 *)ptr;.
33ade 20 20 69 6e 74 20 74 69 6d 65 6f 75 74 20 3d 20    int timeout = 
33adf 28 28 73 71 6c 69 74 65 33 20 2a 29 70 74 72 29  ((sqlite3 *)ptr)
33ae0 2d 3e 62 75 73 79 54 69 6d 65 6f 75 74 3b 0a 20  ->busyTimeout;. 
33ae1 20 69 66 28 20 28 63 6f 75 6e 74 2b 31 29 2a 31   if( (count+1)*1
33ae2 30 30 30 20 3e 20 74 69 6d 65 6f 75 74 20 29 7b  000 > timeout ){
33ae3 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
33ae4 20 7d 0a 20 20 73 71 6c 69 74 65 33 4f 73 53 6c   }.  sqlite3OsSl
33ae5 65 65 70 28 64 62 2d 3e 70 56 66 73 2c 20 31 30  eep(db->pVfs, 10
33ae6 30 30 30 30 30 29 3b 0a 20 20 72 65 74 75 72 6e  00000);.  return
33ae7 20 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a   1;.#endif.}../*
33ae8 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 67  .** Invoke the g
33ae9 69 76 65 6e 20 62 75 73 79 20 68 61 6e 64 6c 65  iven busy handle
33aea 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  r..**.** This ro
33aeb 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20  utine is called 
33aec 77 68 65 6e 20 61 6e 20 6f 70 65 72 61 74 69 6f  when an operatio
33aed 6e 20 66 61 69 6c 65 64 20 77 69 74 68 20 61 20  n failed with a 
33aee 6c 6f 63 6b 2e 0a 2a 2a 20 49 66 20 74 68 69 73  lock..** If this
33aef 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
33af0 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 6c   non-zero, the l
33af1 6f 63 6b 20 69 73 20 72 65 74 72 69 65 64 2e 20  ock is retried. 
33af2 20 49 66 20 69 74 0a 2a 2a 20 72 65 74 75 72 6e   If it.** return
33af3 73 20 30 2c 20 74 68 65 20 6f 70 65 72 61 74 69  s 0, the operati
33af4 6f 6e 20 61 62 6f 72 74 73 20 77 69 74 68 20 61  on aborts with a
33af5 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 20 65 72  n SQLITE_BUSY er
33af6 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ror..*/.SQLITE_P
33af7 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
33af8 65 33 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64  e3InvokeBusyHand
33af9 6c 65 72 28 42 75 73 79 48 61 6e 64 6c 65 72 20  ler(BusyHandler 
33afa 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  *p){.  int rc;. 
33afb 20 69 66 28 20 4e 45 56 45 52 28 70 3d 3d 30 29   if( NEVER(p==0)
33afc 20 7c 7c 20 70 2d 3e 78 46 75 6e 63 3d 3d 30 20   || p->xFunc==0 
33afd 7c 7c 20 70 2d 3e 6e 42 75 73 79 3c 30 20 29 20  || p->nBusy<0 ) 
33afe 72 65 74 75 72 6e 20 30 3b 0a 20 20 72 63 20 3d  return 0;.  rc =
33aff 20 70 2d 3e 78 46 75 6e 63 28 70 2d 3e 70 41 72   p->xFunc(p->pAr
33b00 67 2c 20 70 2d 3e 6e 42 75 73 79 29 3b 0a 20 20  g, p->nBusy);.  
33b01 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20  if( rc==0 ){.   
33b02 20 70 2d 3e 6e 42 75 73 79 20 3d 20 2d 31 3b 0a   p->nBusy = -1;.
33b03 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 2d 3e    }else{.    p->
33b04 6e 42 75 73 79 2b 2b 3b 0a 20 20 7d 0a 20 20 72  nBusy++;.  }.  r
33b05 65 74 75 72 6e 20 72 63 3b 20 0a 7d 0a 0a 2f 2a  eturn rc; .}../*
33b06 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
33b07 20 73 65 74 73 20 74 68 65 20 62 75 73 79 20 63   sets the busy c
33b08 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e 20 53  allback for an S
33b09 71 6c 69 74 65 20 64 61 74 61 62 61 73 65 20 74  qlite database t
33b0a 6f 20 74 68 65 0a 2a 2a 20 67 69 76 65 6e 20 63  o the.** given c
33b0b 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
33b0c 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20   with the given 
33b0d 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  argument..*/.SQL
33b0e 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
33b0f 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72  te3_busy_handler
33b10 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
33b11 0a 20 20 69 6e 74 20 28 2a 78 42 75 73 79 29 28  .  int (*xBusy)(
33b12 76 6f 69 64 2a 2c 69 6e 74 29 2c 0a 20 20 76 6f  void*,int),.  vo
33b13 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20 73 71  id *pArg.){.  sq
33b14 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
33b15 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
33b16 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e  db->busyHandler.
33b17 78 46 75 6e 63 20 3d 20 78 42 75 73 79 3b 0a 20  xFunc = xBusy;. 
33b18 20 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72   db->busyHandler
33b19 2e 70 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20  .pArg = pArg;.  
33b1a 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 2e  db->busyHandler.
33b1b 6e 42 75 73 79 20 3d 20 30 3b 0a 20 20 73 71 6c  nBusy = 0;.  sql
33b1c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
33b1d 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
33b1e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
33b1f 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
33b20 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53  TE_OMIT_PROGRESS
33b21 5f 43 41 4c 4c 42 41 43 4b 0a 2f 2a 0a 2a 2a 20  _CALLBACK./*.** 
33b22 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74  This routine set
33b23 73 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 63  s the progress c
33b24 61 6c 6c 62 61 63 6b 20 66 6f 72 20 61 6e 20 53  allback for an S
33b25 71 6c 69 74 65 20 64 61 74 61 62 61 73 65 20 74  qlite database t
33b26 6f 20 74 68 65 0a 2a 2a 20 67 69 76 65 6e 20 63  o the.** given c
33b27 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
33b28 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e 20   with the given 
33b29 61 72 67 75 6d 65 6e 74 2e 20 54 68 65 20 70 72  argument. The pr
33b2a 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20  ogress callback 
33b2b 77 69 6c 6c 0a 2a 2a 20 62 65 20 69 6e 76 6f 6b  will.** be invok
33b2c 65 64 20 65 76 65 72 79 20 6e 4f 70 73 20 6f 70  ed every nOps op
33b2d 63 6f 64 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  codes..*/.SQLITE
33b2e 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
33b2f 33 5f 70 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c  3_progress_handl
33b30 65 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  er(.  sqlite3 *d
33b31 62 2c 20 0a 20 20 69 6e 74 20 6e 4f 70 73 2c 0a  b, .  int nOps,.
33b32 20 20 69 6e 74 20 28 2a 78 50 72 6f 67 72 65 73    int (*xProgres
33b33 73 29 28 76 6f 69 64 2a 29 2c 20 0a 20 20 76 6f  s)(void*), .  vo
33b34 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20 73 71  id *pArg.){.  sq
33b35 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
33b36 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
33b37 69 66 28 20 6e 4f 70 73 3e 30 20 29 7b 0a 20 20  if( nOps>0 ){.  
33b38 20 20 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 20    db->xProgress 
33b39 3d 20 78 50 72 6f 67 72 65 73 73 3b 0a 20 20 20  = xProgress;.   
33b3a 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73 4f 70   db->nProgressOp
33b3b 73 20 3d 20 6e 4f 70 73 3b 0a 20 20 20 20 64 62  s = nOps;.    db
33b3c 2d 3e 70 50 72 6f 67 72 65 73 73 41 72 67 20 3d  ->pProgressArg =
33b3d 20 70 41 72 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a   pArg;.  }else{.
33b3e 20 20 20 20 64 62 2d 3e 78 50 72 6f 67 72 65 73      db->xProgres
33b3f 73 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 6e  s = 0;.    db->n
33b40 50 72 6f 67 72 65 73 73 4f 70 73 20 3d 20 30 3b  ProgressOps = 0;
33b41 0a 20 20 20 20 64 62 2d 3e 70 50 72 6f 67 72 65  .    db->pProgre
33b42 73 73 41 72 67 20 3d 20 30 3b 0a 20 20 7d 0a 20  ssArg = 0;.  }. 
33b43 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
33b44 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
33b45 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  .}.#endif.../*.*
33b46 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
33b47 6e 73 74 61 6c 6c 73 20 61 20 64 65 66 61 75 6c  nstalls a defaul
33b48 74 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 74  t busy handler t
33b49 68 61 74 20 77 61 69 74 73 20 66 6f 72 20 74 68  hat waits for th
33b4a 65 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 6e  e.** specified n
33b4b 75 6d 62 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65  umber of millise
33b4c 63 6f 6e 64 73 20 62 65 66 6f 72 65 20 72 65 74  conds before ret
33b4d 75 72 6e 69 6e 67 20 30 2e 0a 2a 2f 0a 53 51 4c  urning 0..*/.SQL
33b4e 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
33b4f 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74  te3_busy_timeout
33b50 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
33b51 74 20 6d 73 29 7b 0a 20 20 69 66 28 20 6d 73 3e  t ms){.  if( ms>
33b52 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 62 75 73  0 ){.    db->bus
33b53 79 54 69 6d 65 6f 75 74 20 3d 20 6d 73 3b 0a 20  yTimeout = ms;. 
33b54 20 20 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f     sqlite3_busy_
33b55 68 61 6e 64 6c 65 72 28 64 62 2c 20 73 71 6c 69  handler(db, sqli
33b56 74 65 44 65 66 61 75 6c 74 42 75 73 79 43 61 6c  teDefaultBusyCal
33b57 6c 62 61 63 6b 2c 20 28 76 6f 69 64 2a 29 64 62  lback, (void*)db
33b58 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
33b59 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 68 61 6e  sqlite3_busy_han
33b5a 64 6c 65 72 28 64 62 2c 20 30 2c 20 30 29 3b 0a  dler(db, 0, 0);.
33b5b 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
33b5c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
33b5d 20 43 61 75 73 65 20 61 6e 79 20 70 65 6e 64 69   Cause any pendi
33b5e 6e 67 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20  ng operation to 
33b5f 73 74 6f 70 20 61 74 20 69 74 73 20 65 61 72 6c  stop at its earl
33b60 69 65 73 74 20 6f 70 70 6f 72 74 75 6e 69 74 79  iest opportunity
33b61 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
33b62 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 69 6e 74  void sqlite3_int
33b63 65 72 72 75 70 74 28 73 71 6c 69 74 65 33 20 2a  errupt(sqlite3 *
33b64 64 62 29 7b 0a 20 20 64 62 2d 3e 75 31 2e 69 73  db){.  db->u1.is
33b65 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 31 3b  Interrupted = 1;
33b66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .}.../*.** This 
33b67 66 75 6e 63 74 69 6f 6e 20 69 73 20 65 78 61 63  function is exac
33b68 74 6c 79 20 74 68 65 20 73 61 6d 65 20 61 73 20  tly the same as 
33b69 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
33b6a 75 6e 63 74 69 6f 6e 28 29 2c 20 65 78 63 65 70  unction(), excep
33b6b 74 0a 2a 2a 20 74 68 61 74 20 69 74 20 69 73 20  t.** that it is 
33b6c 64 65 73 69 67 6e 65 64 20 74 6f 20 62 65 20 63  designed to be c
33b6d 61 6c 6c 65 64 20 62 79 20 69 6e 74 65 72 6e 61  alled by interna
33b6e 6c 20 63 6f 64 65 2e 20 54 68 65 20 64 69 66 66  l code. The diff
33b6f 65 72 65 6e 63 65 20 69 73 0a 2a 2a 20 74 68 61  erence is.** tha
33b70 74 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20  t if a malloc() 
33b71 66 61 69 6c 73 20 69 6e 20 73 71 6c 69 74 65 33  fails in sqlite3
33b72 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
33b73 28 29 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  (), an error cod
33b74 65 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  e.** is returned
33b75 20 61 6e 64 20 74 68 65 20 6d 61 6c 6c 6f 63 46   and the mallocF
33b76 61 69 6c 65 64 20 66 6c 61 67 20 63 6c 65 61 72  ailed flag clear
33b77 65 64 2e 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ed. .*/.SQLITE_P
33b78 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
33b79 65 33 43 72 65 61 74 65 46 75 6e 63 28 0a 20 20  e3CreateFunc(.  
33b7a 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63  sqlite3 *db,.  c
33b7b 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63  onst char *zFunc
33b7c 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  tionName,.  int 
33b7d 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 6e 63 2c  nArg,.  int enc,
33b7e 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72 44 61  .  void *pUserDa
33b7f 74 61 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75  ta,.  void (*xFu
33b80 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nc)(sqlite3_cont
33b81 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
33b82 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f  _value **),.  vo
33b83 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69  id (*xStep)(sqli
33b84 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
33b85 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  ,sqlite3_value *
33b86 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69  *),.  void (*xFi
33b87 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  nal)(sqlite3_con
33b88 74 65 78 74 2a 29 0a 29 7b 0a 20 20 46 75 6e 63  text*).){.  Func
33b89 44 65 66 20 2a 70 3b 0a 20 20 69 6e 74 20 6e 4e  Def *p;.  int nN
33b8a 61 6d 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ame;..  assert( 
33b8b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
33b8c 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
33b8d 0a 20 20 69 66 28 20 7a 46 75 6e 63 74 69 6f 6e  .  if( zFunction
33b8e 4e 61 6d 65 3d 3d 30 20 7c 7c 0a 20 20 20 20 20  Name==0 ||.     
33b8f 20 28 78 46 75 6e 63 20 26 26 20 28 78 46 69 6e   (xFunc && (xFin
33b90 61 6c 20 7c 7c 20 78 53 74 65 70 29 29 20 7c 7c  al || xStep)) ||
33b91 20 0a 20 20 20 20 20 20 28 21 78 46 75 6e 63 20   .      (!xFunc 
33b92 26 26 20 28 78 46 69 6e 61 6c 20 26 26 20 21 78  && (xFinal && !x
33b93 53 74 65 70 29 29 20 7c 7c 0a 20 20 20 20 20 20  Step)) ||.      
33b94 28 21 78 46 75 6e 63 20 26 26 20 28 21 78 46 69  (!xFunc && (!xFi
33b95 6e 61 6c 20 26 26 20 78 53 74 65 70 29 29 20 7c  nal && xStep)) |
33b96 7c 0a 20 20 20 20 20 20 28 6e 41 72 67 3c 2d 31  |.      (nArg<-1
33b97 20 7c 7c 20 6e 41 72 67 3e 53 51 4c 49 54 45 5f   || nArg>SQLITE_
33b98 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47  MAX_FUNCTION_ARG
33b99 29 20 7c 7c 0a 20 20 20 20 20 20 28 32 35 35 3c  ) ||.      (255<
33b9a 28 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  (nName = sqlite3
33b9b 53 74 72 6c 65 6e 33 30 28 20 7a 46 75 6e 63 74  Strlen30( zFunct
33b9c 69 6f 6e 4e 61 6d 65 29 29 29 20 29 7b 0a 20 20  ionName))) ){.  
33b9d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
33b9e 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 0a 23  MISUSE;.  }.  .#
33b9f 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
33ba0 49 54 5f 55 54 46 31 36 0a 20 20 2f 2a 20 49 66  IT_UTF16.  /* If
33ba1 20 53 51 4c 49 54 45 5f 55 54 46 31 36 20 69 73   SQLITE_UTF16 is
33ba2 20 73 70 65 63 69 66 69 65 64 20 61 73 20 74 68   specified as th
33ba3 65 20 65 6e 63 6f 64 69 6e 67 20 74 79 70 65 2c  e encoding type,
33ba4 20 74 72 61 6e 73 66 6f 72 6d 20 74 68 69 73 0a   transform this.
33ba5 20 20 2a 2a 20 74 6f 20 6f 6e 65 20 6f 66 20 53    ** to one of S
33ba6 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72  QLITE_UTF16LE or
33ba7 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20   SQLITE_UTF16BE 
33ba8 75 73 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 53  using the.  ** S
33ba9 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
33baa 45 20 6d 61 63 72 6f 2e 20 53 51 4c 49 54 45 5f  E macro. SQLITE_
33bab 55 54 46 31 36 20 69 73 20 6e 6f 74 20 75 73 65  UTF16 is not use
33bac 64 20 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20 20  d internally..  
33bad 2a 2a 0a 20 20 2a 2a 20 49 66 20 53 51 4c 49 54  **.  ** If SQLIT
33bae 45 5f 41 4e 59 20 69 73 20 73 70 65 63 69 66 69  E_ANY is specifi
33baf 65 64 2c 20 61 64 64 20 74 68 72 65 65 20 76 65  ed, add three ve
33bb0 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 75  rsions of the fu
33bb1 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20 74  nction.  ** to t
33bb2 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 20  he hash table.. 
33bb3 20 2a 2f 0a 20 20 69 66 28 20 65 6e 63 3d 3d 53   */.  if( enc==S
33bb4 51 4c 49 54 45 5f 55 54 46 31 36 20 29 7b 0a 20  QLITE_UTF16 ){. 
33bb5 20 20 20 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f     enc = SQLITE_
33bb6 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 7d  UTF16NATIVE;.  }
33bb7 65 6c 73 65 20 69 66 28 20 65 6e 63 3d 3d 53 51  else if( enc==SQ
33bb8 4c 49 54 45 5f 41 4e 59 20 29 7b 0a 20 20 20 20  LITE_ANY ){.    
33bb9 69 6e 74 20 72 63 3b 0a 20 20 20 20 72 63 20 3d  int rc;.    rc =
33bba 20 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75   sqlite3CreateFu
33bbb 6e 63 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e  nc(db, zFunction
33bbc 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 53 51 4c 49  Name, nArg, SQLI
33bbd 54 45 5f 55 54 46 38 2c 0a 20 20 20 20 20 20 20  TE_UTF8,.       
33bbe 20 20 70 55 73 65 72 44 61 74 61 2c 20 78 46 75    pUserData, xFu
33bbf 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61  nc, xStep, xFina
33bc0 6c 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  l);.    if( rc==
33bc1 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
33bc2 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 43     rc = sqlite3C
33bc3 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 7a 46  reateFunc(db, zF
33bc4 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e 41 72  unctionName, nAr
33bc5 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c  g, SQLITE_UTF16L
33bc6 45 2c 0a 20 20 20 20 20 20 20 20 20 20 70 55 73  E,.          pUs
33bc7 65 72 44 61 74 61 2c 20 78 46 75 6e 63 2c 20 78  erData, xFunc, x
33bc8 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a 20  Step, xFinal);. 
33bc9 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21     }.    if( rc!
33bca 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
33bcb 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
33bcc 20 20 20 7d 0a 20 20 20 20 65 6e 63 20 3d 20 53     }.    enc = S
33bcd 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b 0a 20  QLITE_UTF16BE;. 
33bce 20 7d 0a 23 65 6c 73 65 0a 20 20 65 6e 63 20 3d   }.#else.  enc =
33bcf 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 23 65   SQLITE_UTF8;.#e
33bd0 6e 64 69 66 0a 20 20 0a 20 20 2f 2a 20 43 68 65  ndif.  .  /* Che
33bd1 63 6b 20 69 66 20 61 6e 20 65 78 69 73 74 69 6e  ck if an existin
33bd2 67 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65  g function is be
33bd3 69 6e 67 20 6f 76 65 72 72 69 64 64 65 6e 20 6f  ing overridden o
33bd4 72 20 64 65 6c 65 74 65 64 2e 20 49 66 20 73 6f  r deleted. If so
33bd5 2c 0a 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 65  ,.  ** and there
33bd6 20 61 72 65 20 61 63 74 69 76 65 20 56 4d 73 2c   are active VMs,
33bd7 20 74 68 65 6e 20 72 65 74 75 72 6e 20 53 51 4c   then return SQL
33bd8 49 54 45 5f 42 55 53 59 2e 20 49 66 20 61 20 66  ITE_BUSY. If a f
33bd9 75 6e 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20  unction.  ** is 
33bda 62 65 69 6e 67 20 6f 76 65 72 72 69 64 64 65 6e  being overridden
33bdb 2f 64 65 6c 65 74 65 64 20 62 75 74 20 74 68 65  /deleted but the
33bdc 72 65 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65  re are no active
33bdd 20 56 4d 73 2c 20 61 6c 6c 6f 77 20 74 68 65 0a   VMs, allow the.
33bde 20 20 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 74    ** operation t
33bdf 6f 20 63 6f 6e 74 69 6e 75 65 20 62 75 74 20 69  o continue but i
33be0 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 70 72  nvalidate all pr
33be1 65 63 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d  ecompiled statem
33be2 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d  ents..  */.  p =
33be3 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63   sqlite3FindFunc
33be4 74 69 6f 6e 28 64 62 2c 20 7a 46 75 6e 63 74 69  tion(db, zFuncti
33be5 6f 6e 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e  onName, nName, n
33be6 41 72 67 2c 20 28 75 38 29 65 6e 63 2c 20 30 29  Arg, (u8)enc, 0)
33be7 3b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e  ;.  if( p && p->
33be8 69 50 72 65 66 45 6e 63 3d 3d 65 6e 63 20 26 26  iPrefEnc==enc &&
33be9 20 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 29   p->nArg==nArg )
33bea 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63  {.    if( db->ac
33beb 74 69 76 65 56 64 62 65 43 6e 74 20 29 7b 0a 20  tiveVdbeCnt ){. 
33bec 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
33bed 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 42 55 53  r(db, SQLITE_BUS
33bee 59 2c 20 0a 20 20 20 20 20 20 20 20 22 75 6e 61  Y, .        "una
33bef 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 2f 6d 6f  ble to delete/mo
33bf0 64 69 66 79 20 75 73 65 72 2d 66 75 6e 63 74 69  dify user-functi
33bf1 6f 6e 20 64 75 65 20 74 6f 20 61 63 74 69 76 65  on due to active
33bf2 20 73 74 61 74 65 6d 65 6e 74 73 22 29 3b 0a 20   statements");. 
33bf3 20 20 20 20 20 61 73 73 65 72 74 28 20 21 64 62       assert( !db
33bf4 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
33bf5 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
33bf6 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20  QLITE_BUSY;.    
33bf7 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
33bf8 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72  ite3ExpirePrepar
33bf9 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29  edStatements(db)
33bfa 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70  ;.    }.  }..  p
33bfb 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 46 75   = sqlite3FindFu
33bfc 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 46 75 6e 63  nction(db, zFunc
33bfd 74 69 6f 6e 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c  tionName, nName,
33bfe 20 6e 41 72 67 2c 20 28 75 38 29 65 6e 63 2c 20   nArg, (u8)enc, 
33bff 31 29 3b 0a 20 20 61 73 73 65 72 74 28 70 20 7c  1);.  assert(p |
33c00 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
33c01 65 64 29 3b 0a 20 20 69 66 28 20 21 70 20 29 7b  ed);.  if( !p ){
33c02 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
33c03 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
33c04 70 2d 3e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20  p->flags = 0;.  
33c05 70 2d 3e 78 46 75 6e 63 20 3d 20 78 46 75 6e 63  p->xFunc = xFunc
33c06 3b 0a 20 20 70 2d 3e 78 53 74 65 70 20 3d 20 78  ;.  p->xStep = x
33c07 53 74 65 70 3b 0a 20 20 70 2d 3e 78 46 69 6e 61  Step;.  p->xFina
33c08 6c 69 7a 65 20 3d 20 78 46 69 6e 61 6c 3b 0a 20  lize = xFinal;. 
33c09 20 70 2d 3e 70 55 73 65 72 44 61 74 61 20 3d 20   p->pUserData = 
33c0a 70 55 73 65 72 44 61 74 61 3b 0a 20 20 70 2d 3e  pUserData;.  p->
33c0b 6e 41 72 67 20 3d 20 28 75 31 36 29 6e 41 72 67  nArg = (u16)nArg
33c0c 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
33c0d 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
33c0e 72 65 61 74 65 20 6e 65 77 20 75 73 65 72 20 66  reate new user f
33c0f 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c  unctions..*/.SQL
33c10 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
33c11 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
33c12 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ion(.  sqlite3 *
33c13 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  db,.  const char
33c14 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c   *zFunctionName,
33c15 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69  .  int nArg,.  i
33c16 6e 74 20 65 6e 63 2c 0a 20 20 76 6f 69 64 20 2a  nt enc,.  void *
33c17 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75 6e  p,.  void (*xFun
33c18 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  c)(sqlite3_conte
33c19 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  xt*,int,sqlite3_
33c1a 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69  value **),.  voi
33c1b 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74  d (*xStep)(sqlit
33c1c 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
33c1d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
33c1e 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e  ),.  void (*xFin
33c1f 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  al)(sqlite3_cont
33c20 65 78 74 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72  ext*).){.  int r
33c21 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  c;.  sqlite3_mut
33c22 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
33c23 65 78 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  ex);.  rc = sqli
33c24 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62  te3CreateFunc(db
33c25 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c  , zFunctionName,
33c26 20 6e 41 72 67 2c 20 65 6e 63 2c 20 70 2c 20 78   nArg, enc, p, x
33c27 46 75 6e 63 2c 20 78 53 74 65 70 2c 20 78 46 69  Func, xStep, xFi
33c28 6e 61 6c 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  nal);.  rc = sql
33c29 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
33c2a 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  rc);.  sqlite3_m
33c2b 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
33c2c 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
33c2d 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53  rc;.}..#ifndef S
33c2e 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
33c2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
33c30 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
33c31 75 6e 63 74 69 6f 6e 31 36 28 0a 20 20 73 71 6c  unction16(.  sql
33c32 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e 73  ite3 *db,.  cons
33c33 74 20 76 6f 69 64 20 2a 7a 46 75 6e 63 74 69 6f  t void *zFunctio
33c34 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41 72  nName,.  int nAr
33c35 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 52 65  g,.  int eTextRe
33c36 70 2c 0a 20 20 76 6f 69 64 20 2a 70 2c 0a 20 20  p,.  void *p,.  
33c37 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71  void (*xFunc)(sq
33c38 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
33c39 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
33c3a 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53  **),.  void (*xS
33c3b 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  tep)(sqlite3_con
33c3c 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
33c3d 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f  3_value**),.  vo
33c3e 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c  id (*xFinal)(sql
33c3f 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29  ite3_context*).)
33c40 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 63 68  {.  int rc;.  ch
33c41 61 72 20 2a 7a 46 75 6e 63 38 3b 0a 20 20 73 71  ar *zFunc8;.  sq
33c42 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
33c43 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
33c44 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c  assert( !db->mal
33c45 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 7a  locFailed );.  z
33c46 46 75 6e 63 38 20 3d 20 73 71 6c 69 74 65 33 55  Func8 = sqlite3U
33c47 74 66 31 36 74 6f 38 28 64 62 2c 20 7a 46 75 6e  tf16to8(db, zFun
33c48 63 74 69 6f 6e 4e 61 6d 65 2c 20 2d 31 29 3b 0a  ctionName, -1);.
33c49 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 43 72    rc = sqlite3Cr
33c4a 65 61 74 65 46 75 6e 63 28 64 62 2c 20 7a 46 75  eateFunc(db, zFu
33c4b 6e 63 38 2c 20 6e 41 72 67 2c 20 65 54 65 78 74  nc8, nArg, eText
33c4c 52 65 70 2c 20 70 2c 20 78 46 75 6e 63 2c 20 78  Rep, p, xFunc, x
33c4d 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a 20  Step, xFinal);. 
33c4e 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
33c4f 62 2c 20 7a 46 75 6e 63 38 29 3b 0a 20 20 72 63  b, zFunc8);.  rc
33c50 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
33c51 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c  t(db, rc);.  sql
33c52 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
33c53 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
33c54 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64  eturn rc;.}.#end
33c55 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6c 61  if.../*.** Decla
33c56 72 65 20 74 68 61 74 20 61 20 66 75 6e 63 74 69  re that a functi
33c57 6f 6e 20 68 61 73 20 62 65 65 6e 20 6f 76 65 72  on has been over
33c58 6c 6f 61 64 65 64 20 62 79 20 61 20 76 69 72 74  loaded by a virt
33c59 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  ual table..**.**
33c5a 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   If the function
33c5b 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20   already exists 
33c5c 61 73 20 61 20 72 65 67 75 6c 61 72 20 67 6c 6f  as a regular glo
33c5d 62 61 6c 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68  bal function, th
33c5e 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69  en.** this routi
33c5f 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20  ne is a no-op.  
33c60 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  If the function 
33c61 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20  does not exist, 
33c62 74 68 65 6e 20 63 72 65 61 74 65 0a 2a 2a 20 61  then create.** a
33c63 20 6e 65 77 20 6f 6e 65 20 74 68 61 74 20 61 6c   new one that al
33c64 77 61 79 73 20 74 68 72 6f 77 73 20 61 20 72 75  ways throws a ru
33c65 6e 2d 74 69 6d 65 20 65 72 72 6f 72 2e 20 20 0a  n-time error.  .
33c66 2a 2a 0a 2a 2a 20 57 68 65 6e 20 76 69 72 74 75  **.** When virtu
33c67 61 6c 20 74 61 62 6c 65 73 20 69 6e 74 65 6e 64  al tables intend
33c68 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e 20 6f   to provide an o
33c69 76 65 72 6c 6f 61 64 65 64 20 66 75 6e 63 74 69  verloaded functi
33c6a 6f 6e 2c 20 74 68 65 79 0a 2a 2a 20 73 68 6f 75  on, they.** shou
33c6b 6c 64 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75  ld call this rou
33c6c 74 69 6e 65 20 74 6f 20 6d 61 6b 65 20 73 75 72  tine to make sur
33c6d 65 20 74 68 65 20 67 6c 6f 62 61 6c 20 66 75 6e  e the global fun
33c6e 63 74 69 6f 6e 20 65 78 69 73 74 73 2e 0a 2a 2a  ction exists..**
33c6f 20 41 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69   A global functi
33c70 6f 6e 20 6d 75 73 74 20 65 78 69 73 74 20 69 6e  on must exist in
33c71 20 6f 72 64 65 72 20 66 6f 72 20 6e 61 6d 65 20   order for name 
33c72 72 65 73 6f 6c 75 74 69 6f 6e 20 74 6f 20 77 6f  resolution to wo
33c73 72 6b 0a 2a 2a 20 70 72 6f 70 65 72 6c 79 2e 0a  rk.** properly..
33c74 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
33c75 74 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f  t sqlite3_overlo
33c76 61 64 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 73  ad_function(.  s
33c77 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f  qlite3 *db,.  co
33c78 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
33c79 0a 20 20 69 6e 74 20 6e 41 72 67 0a 29 7b 0a 20  .  int nArg.){. 
33c7a 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c   int nName = sql
33c7b 69 74 65 33 53 74 72 6c 65 6e 33 30 28 7a 4e 61  ite3Strlen30(zNa
33c7c 6d 65 29 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  me);.  int rc;. 
33c7d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
33c7e 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
33c7f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69  .  if( sqlite3Fi
33c80 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a  ndFunction(db, z
33c81 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e 41 72  Name, nName, nAr
33c82 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  g, SQLITE_UTF8, 
33c83 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  0)==0 ){.    sql
33c84 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64  ite3CreateFunc(d
33c85 62 2c 20 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20  b, zName, nArg, 
33c86 53 51 4c 49 54 45 5f 55 54 46 38 2c 0a 20 20 20  SQLITE_UTF8,.   
33c87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33c88 20 20 20 30 2c 20 73 71 6c 69 74 65 33 49 6e 76     0, sqlite3Inv
33c89 61 6c 69 64 46 75 6e 63 74 69 6f 6e 2c 20 30 2c  alidFunction, 0,
33c8a 20 30 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20   0);.  }.  rc = 
33c8b 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64  sqlite3ApiExit(d
33c8c 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 20  b, SQLITE_OK);. 
33c8d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
33c8e 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
33c8f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
33c90 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
33c91 4f 4d 49 54 5f 54 52 41 43 45 0a 2f 2a 0a 2a 2a  OMIT_TRACE./*.**
33c92 20 52 65 67 69 73 74 65 72 20 61 20 74 72 61 63   Register a trac
33c93 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 65  e function.  The
33c94 20 70 41 72 67 20 66 72 6f 6d 20 74 68 65 20 70   pArg from the p
33c95 72 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74  reviously regist
33c96 65 72 65 64 20 74 72 61 63 65 0a 2a 2a 20 69 73  ered trace.** is
33c97 20 72 65 74 75 72 6e 65 64 2e 20 20 0a 2a 2a 0a   returned.  .**.
33c98 2a 2a 20 41 20 4e 55 4c 4c 20 74 72 61 63 65 20  ** A NULL trace 
33c99 66 75 6e 63 74 69 6f 6e 20 6d 65 61 6e 73 20 74  function means t
33c9a 68 61 74 20 6e 6f 20 74 72 61 63 69 6e 67 20 69  hat no tracing i
33c9b 73 20 65 78 65 63 75 74 65 73 2e 20 20 41 20 6e  s executes.  A n
33c9c 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 74 72 61 63 65  on-NULL.** trace
33c9d 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
33c9e 20 61 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74   a function that
33c9f 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74 20 74   is invoked at t
33ca0 68 65 20 73 74 61 72 74 20 6f 66 20 65 61 63 68  he start of each
33ca1 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  .** SQL statemen
33ca2 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
33ca3 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 74   void *sqlite3_t
33ca4 72 61 63 65 28 73 71 6c 69 74 65 33 20 2a 64 62  race(sqlite3 *db
33ca5 2c 20 76 6f 69 64 20 28 2a 78 54 72 61 63 65 29  , void (*xTrace)
33ca6 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61  (void*,const cha
33ca7 72 2a 29 2c 20 76 6f 69 64 20 2a 70 41 72 67 29  r*), void *pArg)
33ca8 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c 64 3b 0a  {.  void *pOld;.
33ca9 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
33caa 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
33cab 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62 2d 3e 70  ;.  pOld = db->p
33cac 54 72 61 63 65 41 72 67 3b 0a 20 20 64 62 2d 3e  TraceArg;.  db->
33cad 78 54 72 61 63 65 20 3d 20 78 54 72 61 63 65 3b  xTrace = xTrace;
33cae 0a 20 20 64 62 2d 3e 70 54 72 61 63 65 41 72 67  .  db->pTraceArg
33caf 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74   = pArg;.  sqlit
33cb0 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
33cb1 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
33cb2 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 2f 2a 0a 2a  urn pOld;.}./*.*
33cb3 2a 20 52 65 67 69 73 74 65 72 20 61 20 70 72 6f  * Register a pro
33cb4 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20  file function.  
33cb5 54 68 65 20 70 41 72 67 20 66 72 6f 6d 20 74 68  The pArg from th
33cb6 65 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67  e previously reg
33cb7 69 73 74 65 72 65 64 20 0a 2a 2a 20 70 72 6f 66  istered .** prof
33cb8 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  ile function is 
33cb9 72 65 74 75 72 6e 65 64 2e 20 20 0a 2a 2a 0a 2a  returned.  .**.*
33cba 2a 20 41 20 4e 55 4c 4c 20 70 72 6f 66 69 6c 65  * A NULL profile
33cbb 20 66 75 6e 63 74 69 6f 6e 20 6d 65 61 6e 73 20   function means 
33cbc 74 68 61 74 20 6e 6f 20 70 72 6f 66 69 6c 69 6e  that no profilin
33cbd 67 20 69 73 20 65 78 65 63 75 74 65 73 2e 20 20  g is executes.  
33cbe 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 70 72  A non-NULL.** pr
33cbf 6f 66 69 6c 65 20 69 73 20 61 20 70 6f 69 6e 74  ofile is a point
33cc0 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e  er to a function
33cc1 20 74 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64   that is invoked
33cc2 20 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69   at the conclusi
33cc3 6f 6e 20 6f 66 0a 2a 2a 20 65 61 63 68 20 53 51  on of.** each SQ
33cc4 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  L statement that
33cc5 20 69 73 20 72 75 6e 2e 0a 2a 2f 0a 53 51 4c 49   is run..*/.SQLI
33cc6 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
33cc7 69 74 65 33 5f 70 72 6f 66 69 6c 65 28 0a 20 20  ite3_profile(.  
33cc8 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 76  sqlite3 *db,.  v
33cc9 6f 69 64 20 28 2a 78 50 72 6f 66 69 6c 65 29 28  oid (*xProfile)(
33cca 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72  void*,const char
33ccb 2a 2c 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29  *,sqlite_uint64)
33ccc 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a 29  ,.  void *pArg.)
33ccd 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c 64 3b 0a  {.  void *pOld;.
33cce 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
33ccf 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
33cd0 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62 2d 3e 70  ;.  pOld = db->p
33cd1 50 72 6f 66 69 6c 65 41 72 67 3b 0a 20 20 64 62  ProfileArg;.  db
33cd2 2d 3e 78 50 72 6f 66 69 6c 65 20 3d 20 78 50 72  ->xProfile = xPr
33cd3 6f 66 69 6c 65 3b 0a 20 20 64 62 2d 3e 70 50 72  ofile;.  db->pPr
33cd4 6f 66 69 6c 65 41 72 67 20 3d 20 70 41 72 67 3b  ofileArg = pArg;
33cd5 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
33cd6 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
33cd7 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4f 6c 64  );.  return pOld
33cd8 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
33cd9 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20  LITE_OMIT_TRACE 
33cda 2a 2f 0a 0a 2f 2a 2a 2a 20 45 58 50 45 52 49 4d  */../*** EXPERIM
33cdb 45 4e 54 41 4c 20 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  ENTAL ***.**.** 
33cdc 52 65 67 69 73 74 65 72 20 61 20 66 75 6e 63 74  Register a funct
33cdd 69 6f 6e 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65  ion to be invoke
33cde 64 20 77 68 65 6e 20 61 20 74 72 61 6e 73 61 63  d when a transac
33cdf 74 69 6f 6e 20 63 6f 6d 6d 65 6e 74 73 2e 0a 2a  tion comments..*
33ce0 2a 20 49 66 20 74 68 65 20 69 6e 76 6f 6b 65 64  * If the invoked
33ce1 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
33ce2 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
33ce3 20 74 68 65 20 63 6f 6d 6d 69 74 20 62 65 63 6f   the commit beco
33ce4 6d 65 73 20 61 0a 2a 2a 20 72 6f 6c 6c 62 61 63  mes a.** rollbac
33ce5 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  k..*/.SQLITE_API
33ce6 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63   void *sqlite3_c
33ce7 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 0a 20 20 73 71  ommit_hook(.  sq
33ce8 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
33ce9 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74 61 63          /* Attac
33cea 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f 20 74 68  h the hook to th
33ceb 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  is database */. 
33cec 20 69 6e 74 20 28 2a 78 43 61 6c 6c 62 61 63 6b   int (*xCallback
33ced 29 28 76 6f 69 64 2a 29 2c 20 20 2f 2a 20 46 75  )(void*),  /* Fu
33cee 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65  nction to invoke
33cef 20 6f 6e 20 65 61 63 68 20 63 6f 6d 6d 69 74 20   on each commit 
33cf0 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 20  */.  void *pArg 
33cf1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
33cf2 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  * Argument to th
33cf3 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b  e function */.){
33cf4 0a 20 20 76 6f 69 64 20 2a 70 4f 6c 64 3b 0a 20  .  void *pOld;. 
33cf5 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
33cf6 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
33cf7 0a 20 20 70 4f 6c 64 20 3d 20 64 62 2d 3e 70 43  .  pOld = db->pC
33cf8 6f 6d 6d 69 74 41 72 67 3b 0a 20 20 64 62 2d 3e  ommitArg;.  db->
33cf9 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61 63 6b 20  xCommitCallback 
33cfa 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 64  = xCallback;.  d
33cfb 62 2d 3e 70 43 6f 6d 6d 69 74 41 72 67 20 3d 20  b->pCommitArg = 
33cfc 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f  pArg;.  sqlite3_
33cfd 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
33cfe 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
33cff 20 70 4f 6c 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   pOld;.}../*.** 
33d00 52 65 67 69 73 74 65 72 20 61 20 63 61 6c 6c 62  Register a callb
33d01 61 63 6b 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65  ack to be invoke
33d02 64 20 65 61 63 68 20 74 69 6d 65 20 61 20 72 6f  d each time a ro
33d03 77 20 69 73 20 75 70 64 61 74 65 64 2c 0a 2a 2a  w is updated,.**
33d04 20 69 6e 73 65 72 74 65 64 20 6f 72 20 64 65 6c   inserted or del
33d05 65 74 65 64 20 75 73 69 6e 67 20 74 68 69 73 20  eted using this 
33d06 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
33d07 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ion..*/.SQLITE_A
33d08 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  PI void *sqlite3
33d09 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28 0a 20 20  _update_hook(.  
33d0a 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
33d0b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74            /* Att
33d0c 61 63 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f 20  ach the hook to 
33d0d 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f  this database */
33d0e 0a 20 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62  .  void (*xCallb
33d0f 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63  ack)(void*,int,c
33d10 68 61 72 20 63 6f 6e 73 74 20 2a 2c 63 68 61 72  har const *,char
33d11 20 63 6f 6e 73 74 20 2a 2c 73 71 6c 69 74 65 5f   const *,sqlite_
33d12 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69 64 20 2a  int64),.  void *
33d13 70 41 72 67 20 20 20 20 20 20 20 20 20 20 20 20  pArg            
33d14 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20      /* Argument 
33d15 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
33d16 2a 2f 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 52  */.){.  void *pR
33d17 65 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  et;.  sqlite3_mu
33d18 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
33d19 74 65 78 29 3b 0a 20 20 70 52 65 74 20 3d 20 64  tex);.  pRet = d
33d1a 62 2d 3e 70 55 70 64 61 74 65 41 72 67 3b 0a 20  b->pUpdateArg;. 
33d1b 20 64 62 2d 3e 78 55 70 64 61 74 65 43 61 6c 6c   db->xUpdateCall
33d1c 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b  back = xCallback
33d1d 3b 0a 20 20 64 62 2d 3e 70 55 70 64 61 74 65 41  ;.  db->pUpdateA
33d1e 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c  rg = pArg;.  sql
33d1f 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
33d20 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
33d21 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f  eturn pRet;.}../
33d22 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20  *.** Register a 
33d23 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69  callback to be i
33d24 6e 76 6f 6b 65 64 20 65 61 63 68 20 74 69 6d 65  nvoked each time
33d25 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
33d26 73 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62 61 63 6b  s rolled.** back
33d27 20 62 79 20 74 68 69 73 20 64 61 74 61 62 61 73   by this databas
33d28 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f  e connection..*/
33d29 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
33d2a 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62 61   *sqlite3_rollba
33d2b 63 6b 5f 68 6f 6f 6b 28 0a 20 20 73 71 6c 69 74  ck_hook(.  sqlit
33d2c 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
33d2d 20 20 20 20 20 2f 2a 20 41 74 74 61 63 68 20 74       /* Attach t
33d2e 68 65 20 68 6f 6f 6b 20 74 6f 20 74 68 69 73 20  he hook to this 
33d2f 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 76 6f  database */.  vo
33d30 69 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28  id (*xCallback)(
33d31 76 6f 69 64 2a 29 2c 20 2f 2a 20 43 61 6c 6c 62  void*), /* Callb
33d32 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ack function */.
33d33 20 20 76 6f 69 64 20 2a 70 41 72 67 20 20 20 20    void *pArg    
33d34 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
33d35 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66  rgument to the f
33d36 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20  unction */.){.  
33d37 76 6f 69 64 20 2a 70 52 65 74 3b 0a 20 20 73 71  void *pRet;.  sq
33d38 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
33d39 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  r(db->mutex);.  
33d3a 70 52 65 74 20 3d 20 64 62 2d 3e 70 52 6f 6c 6c  pRet = db->pRoll
33d3b 62 61 63 6b 41 72 67 3b 0a 20 20 64 62 2d 3e 78  backArg;.  db->x
33d3c 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63 6b  RollbackCallback
33d3d 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20   = xCallback;.  
33d3e 64 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b 41 72 67  db->pRollbackArg
33d3f 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74   = pArg;.  sqlit
33d40 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
33d41 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
33d42 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f 2a 0a  urn pRet;.}../*.
33d43 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
33d44 20 72 65 74 75 72 6e 73 20 74 72 75 65 20 69 66   returns true if
33d45 20 6d 61 69 6e 2d 6d 65 6d 6f 72 79 20 73 68 6f   main-memory sho
33d46 75 6c 64 20 62 65 20 75 73 65 64 20 69 6e 73 74  uld be used inst
33d47 65 61 64 20 6f 66 0a 2a 2a 20 61 20 74 65 6d 70  ead of.** a temp
33d48 6f 72 61 72 79 20 66 69 6c 65 20 66 6f 72 20 74  orary file for t
33d49 72 61 6e 73 69 65 6e 74 20 70 61 67 65 72 20 66  ransient pager f
33d4a 69 6c 65 73 20 61 6e 64 20 73 74 61 74 65 6d 65  iles and stateme
33d4b 6e 74 20 6a 6f 75 72 6e 61 6c 73 2e 0a 2a 2a 20  nt journals..** 
33d4c 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  The value return
33d4d 65 64 20 64 65 70 65 6e 64 73 20 6f 6e 20 74 68  ed depends on th
33d4e 65 20 76 61 6c 75 65 20 6f 66 20 64 62 2d 3e 74  e value of db->t
33d4f 65 6d 70 5f 73 74 6f 72 65 20 28 72 75 6e 74 69  emp_store (runti
33d50 6d 65 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72 29  me.** parameter)
33d51 20 61 6e 64 20 74 68 65 20 63 6f 6d 70 69 6c 65   and the compile
33d52 20 74 69 6d 65 20 76 61 6c 75 65 20 6f 66 20 53   time value of S
33d53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45  QLITE_TEMP_STORE
33d54 2e 20 54 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69  . The.** followi
33d55 6e 67 20 74 61 62 6c 65 20 64 65 73 63 72 69 62  ng table describ
33d56 65 73 20 74 68 65 20 72 65 6c 61 74 69 6f 6e 73  es the relations
33d57 68 69 70 20 62 65 74 77 65 65 6e 20 74 68 65 73  hip between thes
33d58 65 20 74 77 6f 20 76 61 6c 75 65 73 0a 2a 2a 20  e two values.** 
33d59 61 6e 64 20 74 68 69 73 20 66 75 6e 63 74 69 6f  and this functio
33d5a 6e 73 20 72 65 74 75 72 6e 20 76 61 6c 75 65 2e  ns return value.
33d5b 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f  .**.**   SQLITE_
33d5c 54 45 4d 50 5f 53 54 4f 52 45 20 20 20 20 20 64  TEMP_STORE     d
33d5d 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 20 20 20  b->temp_store   
33d5e 20 20 4c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 65    Location of te
33d5f 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65  mporary database
33d60 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .**   ----------
33d61 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d 2d  -------     ----
33d62 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d  ----------     -
33d63 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
33d64 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
33d65 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20     0            
33d66 20 20 20 20 20 20 20 20 20 61 6e 79 20 20 20 20           any    
33d67 20 20 20 20 20 20 20 20 20 20 20 20 66 69 6c 65              file
33d68 20 20 20 20 20 20 28 72 65 74 75 72 6e 20 30 29        (return 0)
33d69 0a 2a 2a 20 20 20 31 20 20 20 20 20 20 20 20 20  .**   1         
33d6a 20 20 20 20 20 20 20 20 20 20 20 20 31 20 20 20              1   
33d6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66                 f
33d6c 69 6c 65 20 20 20 20 20 20 28 72 65 74 75 72 6e  ile      (return
33d6d 20 30 29 0a 2a 2a 20 20 20 31 20 20 20 20 20 20   0).**   1      
33d6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32                 2
33d6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d70 20 20 6d 65 6d 6f 72 79 20 20 20 20 28 72 65 74    memory    (ret
33d71 75 72 6e 20 31 29 0a 2a 2a 20 20 20 31 20 20 20  urn 1).**   1   
33d72 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d73 20 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20    0             
33d74 20 20 20 20 20 66 69 6c 65 20 20 20 20 20 20 28       file      (
33d75 72 65 74 75 72 6e 20 30 29 0a 2a 2a 20 20 20 32  return 0).**   2
33d76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33d77 20 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20       1          
33d78 20 20 20 20 20 20 20 20 66 69 6c 65 20 20 20 20          file    
33d79 20 20 28 72 65 74 75 72 6e 20 30 29 0a 2a 2a 20    (return 0).** 
33d7a 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20    2             
33d7b 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20 20          2       
33d7c 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 6f 72             memor
33d7d 79 20 20 20 20 28 72 65 74 75 72 6e 20 31 29 0a  y    (return 1).
33d7e 2a 2a 20 20 20 32 20 20 20 20 20 20 20 20 20 20  **   2          
33d7f 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20 20             0    
33d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65                me
33d81 6d 6f 72 79 20 20 20 20 28 72 65 74 75 72 6e 20  mory    (return 
33d82 31 29 0a 2a 2a 20 20 20 33 20 20 20 20 20 20 20  1).**   3       
33d83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 6e                an
33d84 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y               
33d85 20 6d 65 6d 6f 72 79 20 20 20 20 28 72 65 74 75   memory    (retu
33d86 72 6e 20 31 29 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rn 1).*/.SQLITE_
33d87 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
33d88 74 65 33 54 65 6d 70 49 6e 4d 65 6d 6f 72 79 28  te3TempInMemory(
33d89 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 20 2a 64  const sqlite3 *d
33d8a 62 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  b){.#if SQLITE_T
33d8b 45 4d 50 5f 53 54 4f 52 45 3d 3d 31 0a 20 20 72  EMP_STORE==1.  r
33d8c 65 74 75 72 6e 20 28 20 64 62 2d 3e 74 65 6d 70  eturn ( db->temp
33d8d 5f 73 74 6f 72 65 3d 3d 32 20 29 3b 0a 23 65 6e  _store==2 );.#en
33d8e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  dif.#if SQLITE_T
33d8f 45 4d 50 5f 53 54 4f 52 45 3d 3d 32 0a 20 20 72  EMP_STORE==2.  r
33d90 65 74 75 72 6e 20 28 20 64 62 2d 3e 74 65 6d 70  eturn ( db->temp
33d91 5f 73 74 6f 72 65 21 3d 31 20 29 3b 0a 23 65 6e  _store!=1 );.#en
33d92 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  dif.#if SQLITE_T
33d93 45 4d 50 5f 53 54 4f 52 45 3d 3d 33 0a 20 20 72  EMP_STORE==3.  r
33d94 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a  eturn 1;.#endif.
33d95 23 69 66 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f  #if SQLITE_TEMP_
33d96 53 54 4f 52 45 3c 31 20 7c 7c 20 53 51 4c 49 54  STORE<1 || SQLIT
33d97 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3e 33 0a 20  E_TEMP_STORE>3. 
33d98 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69   return 0;.#endi
33d99 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  f.}../*.** This 
33d9a 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
33d9b 64 20 74 6f 20 63 72 65 61 74 65 20 61 20 63 6f  d to create a co
33d9c 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 61 20 64 61  nnection to a da
33d9d 74 61 62 61 73 65 20 42 54 72 65 65 0a 2a 2a 20  tabase BTree.** 
33d9e 64 72 69 76 65 72 2e 20 20 49 66 20 7a 46 69 6c  driver.  If zFil
33d9f 65 6e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d  ename is the nam
33da0 65 20 6f 66 20 61 20 66 69 6c 65 2c 20 74 68 65  e of a file, the
33da1 6e 20 74 68 61 74 20 66 69 6c 65 20 69 73 0a 2a  n that file is.*
33da2 2a 20 6f 70 65 6e 65 64 20 61 6e 64 20 75 73 65  * opened and use
33da3 64 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65  d.  If zFilename
33da4 20 69 73 20 74 68 65 20 6d 61 67 69 63 20 6e 61   is the magic na
33da5 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 74 68  me ":memory:" th
33da6 65 6e 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61  en.** the databa
33da7 73 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  se is stored in 
33da8 6d 65 6d 6f 72 79 20 28 61 6e 64 20 69 73 20 74  memory (and is t
33da9 68 75 73 20 66 6f 72 67 6f 74 74 65 6e 20 61 73  hus forgotten as
33daa 20 73 6f 6f 6e 20 61 73 0a 2a 2a 20 74 68 65 20   soon as.** the 
33dab 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c  connection is cl
33dac 6f 73 65 64 2e 29 20 20 49 66 20 7a 46 69 6c 65  osed.)  If zFile
33dad 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 20 74 68 65  name is NULL the
33dae 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a  n the database.*
33daf 2a 20 69 73 20 61 20 22 76 69 72 74 75 61 6c 22  * is a "virtual"
33db0 20 64 61 74 61 62 61 73 65 20 66 6f 72 20 74 72   database for tr
33db1 61 6e 73 69 65 6e 74 20 75 73 65 20 6f 6e 6c 79  ansient use only
33db2 20 61 6e 64 20 69 73 20 64 65 6c 65 74 65 64 20   and is deleted 
33db3 61 73 0a 2a 2a 20 73 6f 6f 6e 20 61 73 20 74 68  as.** soon as th
33db4 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
33db5 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20  closed..**.** A 
33db6 76 69 72 74 75 61 6c 20 64 61 74 61 62 61 73 65  virtual database
33db7 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61   can be either a
33db8 20 64 69 73 6b 20 66 69 6c 65 20 28 74 68 61 74   disk file (that
33db9 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   is automaticall
33dba 79 0a 2a 2a 20 64 65 6c 65 74 65 64 20 77 68 65  y.** deleted whe
33dbb 6e 20 74 68 65 20 66 69 6c 65 20 69 73 20 63 6c  n the file is cl
33dbc 6f 73 65 64 29 20 6f 72 20 69 74 20 61 6e 20 62  osed) or it an b
33dbd 65 20 68 65 6c 64 20 65 6e 74 69 72 65 6c 79 20  e held entirely 
33dbe 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 54 68  in memory..** Th
33dbf 65 20 73 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d  e sqlite3TempInM
33dc0 65 6d 6f 72 79 28 29 20 66 75 6e 63 74 69 6f 6e  emory() function
33dc1 20 69 73 20 75 73 65 64 20 74 6f 20 64 65 74 65   is used to dete
33dc2 72 6d 69 6e 65 20 77 68 69 63 68 2e 0a 2a 2f 0a  rmine which..*/.
33dc3 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
33dc4 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 46  nt sqlite3BtreeF
33dc5 61 63 74 6f 72 79 28 0a 20 20 63 6f 6e 73 74 20  actory(.  const 
33dc6 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
33dc7 20 20 20 20 2f 2a 20 4d 61 69 6e 20 64 61 74 61      /* Main data
33dc8 62 61 73 65 20 77 68 65 6e 20 6f 70 65 6e 69 6e  base when openin
33dc9 67 20 61 75 78 20 6f 74 68 65 72 77 69 73 65 20  g aux otherwise 
33dca 30 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  0 */.  const cha
33dcb 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20  r *zFilename,   
33dcc 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
33dcd 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  file containing 
33dce 74 68 65 20 42 54 72 65 65 20 64 61 74 61 62 61  the BTree databa
33dcf 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 6d 69 74  se */.  int omit
33dd0 4a 6f 75 72 6e 61 6c 2c 20 20 20 20 20 20 20 20  Journal,        
33dd1 20 20 2f 2a 20 69 66 20 54 52 55 45 20 74 68 65    /* if TRUE the
33dd2 6e 20 64 6f 20 6e 6f 74 20 6a 6f 75 72 6e 61 6c  n do not journal
33dd3 20 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20   this file */.  
33dd4 69 6e 74 20 6e 43 61 63 68 65 2c 20 20 20 20 20  int nCache,     
33dd5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77            /* How
33dd6 20 6d 61 6e 79 20 70 61 67 65 73 20 69 6e 20 74   many pages in t
33dd7 68 65 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f  he page cache */
33dd8 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73 2c  .  int vfsFlags,
33dd9 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
33dda 46 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72  Flags passed thr
33ddb 6f 75 67 68 20 74 6f 20 76 66 73 4f 70 65 6e 20  ough to vfsOpen 
33ddc 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 42  */.  Btree **ppB
33ddd 74 72 65 65 20 20 20 20 20 20 20 20 20 20 20 2f  tree           /
33dde 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 6e 65 77  * Pointer to new
33ddf 20 42 74 72 65 65 20 6f 62 6a 65 63 74 20 77 72   Btree object wr
33de0 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 29 7b  itten here */.){
33de1 0a 20 20 69 6e 74 20 62 74 46 6c 61 67 73 20 3d  .  int btFlags =
33de2 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20   0;.  int rc;.  
33de3 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
33de4 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62  e3_mutex_held(db
33de5 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73  ->mutex) );.  as
33de6 73 65 72 74 28 20 70 70 42 74 72 65 65 20 21 3d  sert( ppBtree !=
33de7 20 30 29 3b 0a 20 20 69 66 28 20 6f 6d 69 74 4a   0);.  if( omitJ
33de8 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 62 74  ournal ){.    bt
33de9 46 6c 61 67 73 20 7c 3d 20 42 54 52 45 45 5f 4f  Flags |= BTREE_O
33dea 4d 49 54 5f 4a 4f 55 52 4e 41 4c 3b 0a 20 20 7d  MIT_JOURNAL;.  }
33deb 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73  .  if( db->flags
33dec 20 26 20 53 51 4c 49 54 45 5f 4e 6f 52 65 61 64   & SQLITE_NoRead
33ded 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 62 74 46 6c  lock ){.    btFl
33dee 61 67 73 20 7c 3d 20 42 54 52 45 45 5f 4e 4f 5f  ags |= BTREE_NO_
33def 52 45 41 44 4c 4f 43 4b 3b 0a 20 20 7d 0a 23 69  READLOCK;.  }.#i
33df0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
33df1 54 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 69 66 28  T_MEMORYDB.  if(
33df2 20 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 26 26   zFilename==0 &&
33df3 20 73 71 6c 69 74 65 33 54 65 6d 70 49 6e 4d 65   sqlite3TempInMe
33df4 6d 6f 72 79 28 64 62 29 20 29 7b 0a 20 20 20 20  mory(db) ){.    
33df5 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 22 3a 6d 65  zFilename = ":me
33df6 6d 6f 72 79 3a 22 3b 0a 20 20 7d 0a 23 65 6e 64  mory:";.  }.#end
33df7 69 66 0a 0a 20 20 69 66 28 20 28 76 66 73 46 6c  if..  if( (vfsFl
33df8 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
33df9 4e 5f 4d 41 49 4e 5f 44 42 29 21 3d 30 20 26 26  N_MAIN_DB)!=0 &&
33dfa 20 28 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c   (zFilename==0 |
33dfb 7c 20 2a 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 29  | *zFilename==0)
33dfc 20 29 7b 0a 20 20 20 20 76 66 73 46 6c 61 67 73   ){.    vfsFlags
33dfd 20 3d 20 28 76 66 73 46 6c 61 67 73 20 26 20 7e   = (vfsFlags & ~
33dfe 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
33dff 5f 44 42 29 20 7c 20 53 51 4c 49 54 45 5f 4f 50  _DB) | SQLITE_OP
33e00 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a 20 20 7d 0a  EN_TEMP_DB;.  }.
33e01 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
33e02 72 65 65 4f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d  reeOpen(zFilenam
33e03 65 2c 20 28 73 71 6c 69 74 65 33 20 2a 29 64 62  e, (sqlite3 *)db
33e04 2c 20 70 70 42 74 72 65 65 2c 20 62 74 46 6c 61  , ppBtree, btFla
33e05 67 73 2c 20 76 66 73 46 6c 61 67 73 29 3b 0a 0a  gs, vfsFlags);..
33e06 20 20 2f 2a 20 49 66 20 74 68 65 20 42 2d 54 72    /* If the B-Tr
33e07 65 65 20 77 61 73 20 73 75 63 63 65 73 73 66 75  ee was successfu
33e08 6c 6c 79 20 6f 70 65 6e 65 64 2c 20 73 65 74 20  lly opened, set 
33e09 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20  the pager-cache 
33e0a 73 69 7a 65 20 74 6f 20 74 68 65 0a 20 20 2a 2a  size to the.  **
33e0b 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 2e 20   default value. 
33e0c 45 78 63 65 70 74 2c 20 69 66 20 74 68 65 20 63  Except, if the c
33e0d 61 6c 6c 20 74 6f 20 42 74 72 65 65 4f 70 65 6e  all to BtreeOpen
33e0e 28 29 20 72 65 74 75 72 6e 65 64 20 61 20 68 61  () returned a ha
33e0f 6e 64 6c 65 0a 20 20 2a 2a 20 6f 70 65 6e 20 6f  ndle.  ** open o
33e10 6e 20 61 6e 20 65 78 69 73 74 69 6e 67 20 73 68  n an existing sh
33e11 61 72 65 64 20 70 61 67 65 72 2d 63 61 63 68 65  ared pager-cache
33e12 2c 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20  , do not change 
33e13 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20  the pager-cache 
33e14 0a 20 20 2a 2a 20 73 69 7a 65 2e 0a 20 20 2a 2f  .  ** size..  */
33e15 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
33e16 45 5f 4f 4b 20 26 26 20 30 3d 3d 73 71 6c 69 74  E_OK && 0==sqlit
33e17 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 2a 70  e3BtreeSchema(*p
33e18 70 42 74 72 65 65 2c 20 30 2c 20 30 29 20 29 7b  pBtree, 0, 0) ){
33e19 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
33e1a 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 2a 70  eSetCacheSize(*p
33e1b 70 42 74 72 65 65 2c 20 6e 43 61 63 68 65 29 3b  pBtree, nCache);
33e1c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
33e1d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
33e1e 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  n UTF-8 encoded 
33e1f 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65  English language
33e20 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20 6f 66 20   explanation of 
33e21 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 0a  the most recent.
33e22 2a 2a 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c  ** error..*/.SQL
33e23 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
33e24 61 72 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d  ar *sqlite3_errm
33e25 73 67 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  sg(sqlite3 *db){
33e26 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
33e27 3b 0a 20 20 69 66 28 20 21 64 62 20 29 7b 0a 20  ;.  if( !db ){. 
33e28 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65     return sqlite
33e29 33 45 72 72 53 74 72 28 53 51 4c 49 54 45 5f 4e  3ErrStr(SQLITE_N
33e2a 4f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 69 66 28  OMEM);.  }.  if(
33e2b 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79 43   !sqlite3SafetyC
33e2c 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29  heckSickOrOk(db)
33e2d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73   ){.    return s
33e2e 71 6c 69 74 65 33 45 72 72 53 74 72 28 53 51 4c  qlite3ErrStr(SQL
33e2f 49 54 45 5f 4d 49 53 55 53 45 29 3b 0a 20 20 7d  ITE_MISUSE);.  }
33e30 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
33e31 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
33e32 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  );.  if( db->mal
33e33 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
33e34 20 7a 20 3d 20 73 71 6c 69 74 65 33 45 72 72 53   z = sqlite3ErrS
33e35 74 72 28 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29  tr(SQLITE_NOMEM)
33e36 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a  ;.  }else{.    z
33e37 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65   = (char*)sqlite
33e38 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 64 62 2d  3_value_text(db-
33e39 3e 70 45 72 72 29 3b 0a 20 20 20 20 61 73 73 65  >pErr);.    asse
33e3a 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rt( !db->mallocF
33e3b 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 69 66 28  ailed );.    if(
33e3c 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a   z==0 ){.      z
33e3d 20 3d 20 73 71 6c 69 74 65 33 45 72 72 53 74 72   = sqlite3ErrStr
33e3e 28 64 62 2d 3e 65 72 72 43 6f 64 65 29 3b 0a 20  (db->errCode);. 
33e3f 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
33e40 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
33e41 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
33e42 75 72 6e 20 7a 3b 0a 7d 0a 0a 23 69 66 6e 64 65  urn z;.}..#ifnde
33e43 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
33e44 46 31 36 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  F16./*.** Return
33e45 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20   UTF-16 encoded 
33e46 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65  English language
33e47 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20 6f 66 20   explanation of 
33e48 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 0a  the most recent.
33e49 2a 2a 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c  ** error..*/.SQL
33e4a 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
33e4b 69 64 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d  id *sqlite3_errm
33e4c 73 67 31 36 28 73 71 6c 69 74 65 33 20 2a 64 62  sg16(sqlite3 *db
33e4d 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ){.  static cons
33e4e 74 20 75 31 36 20 6f 75 74 4f 66 4d 65 6d 5b 5d  t u16 outOfMem[]
33e4f 20 3d 20 7b 0a 20 20 20 20 27 6f 27 2c 20 27 75   = {.    'o', 'u
33e50 27 2c 20 27 74 27 2c 20 27 20 27 2c 20 27 6f 27  ', 't', ' ', 'o'
33e51 2c 20 27 66 27 2c 20 27 20 27 2c 20 27 6d 27 2c  , 'f', ' ', 'm',
33e52 20 27 65 27 2c 20 27 6d 27 2c 20 27 6f 27 2c 20   'e', 'm', 'o', 
33e53 27 72 27 2c 20 27 79 27 2c 20 30 0a 20 20 7d 3b  'r', 'y', 0.  };
33e54 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
33e55 75 31 36 20 6d 69 73 75 73 65 5b 5d 20 3d 20 7b  u16 misuse[] = {
33e56 0a 20 20 20 20 27 6c 27 2c 20 27 69 27 2c 20 27  .    'l', 'i', '
33e57 62 27 2c 20 27 72 27 2c 20 27 61 27 2c 20 27 72  b', 'r', 'a', 'r
33e58 27 2c 20 27 79 27 2c 20 27 20 27 2c 20 0a 20 20  ', 'y', ' ', .  
33e59 20 20 27 72 27 2c 20 27 6f 27 2c 20 27 75 27 2c    'r', 'o', 'u',
33e5a 20 27 74 27 2c 20 27 69 27 2c 20 27 6e 27 2c 20   't', 'i', 'n', 
33e5b 27 65 27 2c 20 27 20 27 2c 20 0a 20 20 20 20 27  'e', ' ', .    '
33e5c 63 27 2c 20 27 61 27 2c 20 27 6c 27 2c 20 27 6c  c', 'a', 'l', 'l
33e5d 27 2c 20 27 65 27 2c 20 27 64 27 2c 20 27 20 27  ', 'e', 'd', ' '
33e5e 2c 20 0a 20 20 20 20 27 6f 27 2c 20 27 75 27 2c  , .    'o', 'u',
33e5f 20 27 74 27 2c 20 27 20 27 2c 20 0a 20 20 20 20   't', ' ', .    
33e60 27 6f 27 2c 20 27 66 27 2c 20 27 20 27 2c 20 0a  'o', 'f', ' ', .
33e61 20 20 20 20 27 73 27 2c 20 27 65 27 2c 20 27 71      's', 'e', 'q
33e62 27 2c 20 27 75 27 2c 20 27 65 27 2c 20 27 6e 27  ', 'u', 'e', 'n'
33e63 2c 20 27 63 27 2c 20 27 65 27 2c 20 30 0a 20 20  , 'c', 'e', 0.  
33e64 7d 3b 0a 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  };..  const void
33e65 20 2a 7a 3b 0a 20 20 69 66 28 20 21 64 62 20 29   *z;.  if( !db )
33e66 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f  {.    return (vo
33e67 69 64 20 2a 29 6f 75 74 4f 66 4d 65 6d 3b 0a 20  id *)outOfMem;. 
33e68 20 7d 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65   }.  if( !sqlite
33e69 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b  3SafetyCheckSick
33e6a 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20 20  OrOk(db) ){.    
33e6b 72 65 74 75 72 6e 20 28 76 6f 69 64 20 2a 29 6d  return (void *)m
33e6c 69 73 75 73 65 3b 0a 20 20 7d 0a 20 20 73 71 6c  isuse;.  }.  sql
33e6d 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
33e6e 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69  (db->mutex);.  i
33e6f 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
33e70 6c 65 64 20 29 7b 0a 20 20 20 20 7a 20 3d 20 28  led ){.    z = (
33e71 76 6f 69 64 20 2a 29 6f 75 74 4f 66 4d 65 6d 3b  void *)outOfMem;
33e72 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 20  .  }else{.    z 
33e73 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
33e74 74 65 78 74 31 36 28 64 62 2d 3e 70 45 72 72 29  text16(db->pErr)
33e75 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29  ;.    if( z==0 )
33e76 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
33e77 61 6c 75 65 53 65 74 53 74 72 28 64 62 2d 3e 70  alueSetStr(db->p
33e78 45 72 72 2c 20 2d 31 2c 20 73 71 6c 69 74 65 33  Err, -1, sqlite3
33e79 45 72 72 53 74 72 28 64 62 2d 3e 65 72 72 43 6f  ErrStr(db->errCo
33e7a 64 65 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  de),.           
33e7b 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c  SQLITE_UTF8, SQL
33e7c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
33e7d 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76     z = sqlite3_v
33e7e 61 6c 75 65 5f 74 65 78 74 31 36 28 64 62 2d 3e  alue_text16(db->
33e7f 70 45 72 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20  pErr);.    }.   
33e80 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 28 29 20 6d   /* A malloc() m
33e81 61 79 20 68 61 76 65 20 66 61 69 6c 65 64 20 77  ay have failed w
33e82 69 74 68 69 6e 20 74 68 65 20 63 61 6c 6c 20 74  ithin the call t
33e83 6f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  o sqlite3_value_
33e84 74 65 78 74 31 36 28 29 0a 20 20 20 20 2a 2a 20  text16().    ** 
33e85 61 62 6f 76 65 2e 20 49 66 20 74 68 69 73 20 69  above. If this i
33e86 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e  s the case, then
33e87 20 74 68 65 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   the db->mallocF
33e88 61 69 6c 65 64 20 66 6c 61 67 20 6e 65 65 64 73  ailed flag needs
33e89 20 74 6f 0a 20 20 20 20 2a 2a 20 62 65 20 63 6c   to.    ** be cl
33e8a 65 61 72 65 64 20 62 65 66 6f 72 65 20 72 65 74  eared before ret
33e8b 75 72 6e 69 6e 67 2e 20 44 6f 20 74 68 69 73 20  urning. Do this 
33e8c 64 69 72 65 63 74 6c 79 2c 20 69 6e 73 74 65 61  directly, instea
33e8d 64 20 6f 66 20 76 69 61 0a 20 20 20 20 2a 2a 20  d of via.    ** 
33e8e 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 29  sqlite3ApiExit()
33e8f 2c 20 74 6f 20 61 76 6f 69 64 20 73 65 74 74 69  , to avoid setti
33e90 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ng the database 
33e91 68 61 6e 64 6c 65 20 65 72 72 6f 72 20 6d 65 73  handle error mes
33e92 73 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  sage..    */.   
33e93 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
33e94 64 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c  d = 0;.  }.  sql
33e95 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
33e96 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
33e97 65 74 75 72 6e 20 7a 3b 0a 7d 0a 23 65 6e 64 69  eturn z;.}.#endi
33e98 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
33e99 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  _UTF16 */../*.**
33e9a 20 52 65 74 75 72 6e 20 74 68 65 20 6d 6f 73 74   Return the most
33e9b 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 63 6f   recent error co
33e9c 64 65 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  de generated by 
33e9d 61 6e 20 53 51 4c 69 74 65 20 72 6f 75 74 69 6e  an SQLite routin
33e9e 65 2e 20 49 66 20 4e 55 4c 4c 20 69 73 0a 2a 2a  e. If NULL is.**
33e9f 20 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20   passed to this 
33ea0 66 75 6e 63 74 69 6f 6e 2c 20 77 65 20 61 73 73  function, we ass
33ea1 75 6d 65 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66  ume a malloc() f
33ea2 61 69 6c 65 64 20 64 75 72 69 6e 67 20 73 71 6c  ailed during sql
33ea3 69 74 65 33 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a  ite3_open()..*/.
33ea4 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
33ea5 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 73  qlite3_errcode(s
33ea6 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69  qlite3 *db){.  i
33ea7 66 28 20 64 62 20 26 26 20 21 73 71 6c 69 74 65  f( db && !sqlite
33ea8 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b  3SafetyCheckSick
33ea9 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20 20  OrOk(db) ){.    
33eaa 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
33eab 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20  SUSE;.  }.  if( 
33eac 21 64 62 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  !db || db->mallo
33ead 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72  cFailed ){.    r
33eae 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
33eaf 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  EM;.  }.  return
33eb0 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 26 20 64   db->errCode & d
33eb1 62 2d 3e 65 72 72 4d 61 73 6b 3b 0a 7d 0a 53 51  b->errMask;.}.SQ
33eb2 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
33eb3 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72  ite3_extended_er
33eb4 72 63 6f 64 65 28 73 71 6c 69 74 65 33 20 2a 64  rcode(sqlite3 *d
33eb5 62 29 7b 0a 20 20 69 66 28 20 64 62 20 26 26 20  b){.  if( db && 
33eb6 21 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68  !sqlite3SafetyCh
33eb7 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20  eckSickOrOk(db) 
33eb8 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
33eb9 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d  LITE_MISUSE;.  }
33eba 0a 20 20 69 66 28 20 21 64 62 20 7c 7c 20 64 62  .  if( !db || db
33ebb 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
33ebc 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
33ebd 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
33ebe 20 72 65 74 75 72 6e 20 64 62 2d 3e 65 72 72 43   return db->errC
33ebf 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72  ode;.}../*.** Cr
33ec0 65 61 74 65 20 61 20 6e 65 77 20 63 6f 6c 6c 61  eate a new colla
33ec1 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 66 6f  ting function fo
33ec2 72 20 64 61 74 61 62 61 73 65 20 22 64 62 22 2e  r database "db".
33ec3 20 20 54 68 65 20 6e 61 6d 65 20 69 73 20 7a 4e    The name is zN
33ec4 61 6d 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20 65  ame.** and the e
33ec5 6e 63 6f 64 69 6e 67 20 69 73 20 65 6e 63 2e 0a  ncoding is enc..
33ec6 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 72  */.static int cr
33ec7 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 0a 20  eateCollation(. 
33ec8 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 0a 20 20   sqlite3* db,.  
33ec9 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
33eca 65 2c 20 0a 20 20 75 38 20 65 6e 63 2c 0a 20 20  e, .  u8 enc,.  
33ecb 75 38 20 63 6f 6c 6c 54 79 70 65 2c 0a 20 20 76  u8 collType,.  v
33ecc 6f 69 64 2a 20 70 43 74 78 2c 0a 20 20 69 6e 74  oid* pCtx,.  int
33ecd 28 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64  (*xCompare)(void
33ece 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
33ecf 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64  *,int,const void
33ed0 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78 44 65 6c  *),.  void(*xDel
33ed1 29 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 43 6f  )(void*).){.  Co
33ed2 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20  llSeq *pColl;.  
33ed3 69 6e 74 20 65 6e 63 32 3b 0a 20 20 69 6e 74 20  int enc2;.  int 
33ed4 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53  nName = sqlite3S
33ed5 74 72 6c 65 6e 33 30 28 7a 4e 61 6d 65 29 3b 0a  trlen30(zName);.
33ed6 20 20 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c    .  assert( sql
33ed7 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
33ed8 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  db->mutex) );.. 
33ed9 20 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 55 54   /* If SQLITE_UT
33eda 46 31 36 20 69 73 20 73 70 65 63 69 66 69 65 64  F16 is specified
33edb 20 61 73 20 74 68 65 20 65 6e 63 6f 64 69 6e 67   as the encoding
33edc 20 74 79 70 65 2c 20 74 72 61 6e 73 66 6f 72 6d   type, transform
33edd 20 74 68 69 73 0a 20 20 2a 2a 20 74 6f 20 6f 6e   this.  ** to on
33ede 65 20 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 31  e of SQLITE_UTF1
33edf 36 4c 45 20 6f 72 20 53 51 4c 49 54 45 5f 55 54  6LE or SQLITE_UT
33ee0 46 31 36 42 45 20 75 73 69 6e 67 20 74 68 65 0a  F16BE using the.
33ee1 20 20 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 31    ** SQLITE_UTF1
33ee2 36 4e 41 54 49 56 45 20 6d 61 63 72 6f 2e 20 53  6NATIVE macro. S
33ee3 51 4c 49 54 45 5f 55 54 46 31 36 20 69 73 20 6e  QLITE_UTF16 is n
33ee4 6f 74 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c  ot used internal
33ee5 6c 79 2e 0a 20 20 2a 2f 0a 20 20 65 6e 63 32 20  ly..  */.  enc2 
33ee6 3d 20 65 6e 63 3b 0a 20 20 74 65 73 74 63 61 73  = enc;.  testcas
33ee7 65 28 20 65 6e 63 32 3d 3d 53 51 4c 49 54 45 5f  e( enc2==SQLITE_
33ee8 55 54 46 31 36 20 29 3b 0a 20 20 74 65 73 74 63  UTF16 );.  testc
33ee9 61 73 65 28 20 65 6e 63 32 3d 3d 53 51 4c 49 54  ase( enc2==SQLIT
33eea 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 20  E_UTF16_ALIGNED 
33eeb 29 3b 0a 20 20 69 66 28 20 65 6e 63 32 3d 3d 53  );.  if( enc2==S
33eec 51 4c 49 54 45 5f 55 54 46 31 36 20 7c 7c 20 65  QLITE_UTF16 || e
33eed 6e 63 32 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31  nc2==SQLITE_UTF1
33eee 36 5f 41 4c 49 47 4e 45 44 20 29 7b 0a 20 20 20  6_ALIGNED ){.   
33eef 20 65 6e 63 32 20 3d 20 53 51 4c 49 54 45 5f 55   enc2 = SQLITE_U
33ef0 54 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 7d 0a  TF16NATIVE;.  }.
33ef1 20 20 69 66 28 20 65 6e 63 32 3c 53 51 4c 49 54    if( enc2<SQLIT
33ef2 45 5f 55 54 46 38 20 7c 7c 20 65 6e 63 32 3e 53  E_UTF8 || enc2>S
33ef3 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 29 7b  QLITE_UTF16BE ){
33ef4 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
33ef5 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 0a  TE_MISUSE;.  }..
33ef6 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68    /* Check if th
33ef7 69 73 20 63 61 6c 6c 20 69 73 20 72 65 6d 6f 76  is call is remov
33ef8 69 6e 67 20 6f 72 20 72 65 70 6c 61 63 69 6e 67  ing or replacing
33ef9 20 61 6e 20 65 78 69 73 74 69 6e 67 20 63 6f 6c   an existing col
33efa 6c 61 74 69 6f 6e 20 0a 20 20 2a 2a 20 73 65 71  lation .  ** seq
33efb 75 65 6e 63 65 2e 20 49 66 20 73 6f 2c 20 61 6e  uence. If so, an
33efc 64 20 74 68 65 72 65 20 61 72 65 20 61 63 74 69  d there are acti
33efd 76 65 20 56 4d 73 2c 20 72 65 74 75 72 6e 20 62  ve VMs, return b
33efe 75 73 79 2e 20 49 66 20 74 68 65 72 65 0a 20 20  usy. If there.  
33eff 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65  ** are no active
33f00 20 56 4d 73 2c 20 69 6e 76 61 6c 69 64 61 74 65   VMs, invalidate
33f01 20 61 6e 79 20 70 72 65 2d 63 6f 6d 70 69 6c 65   any pre-compile
33f02 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20  d statements..  
33f03 2a 2f 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c  */.  pColl = sql
33f04 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
33f05 64 62 2c 20 28 75 38 29 65 6e 63 32 2c 20 7a 4e  db, (u8)enc2, zN
33f06 61 6d 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 70  ame, 0);.  if( p
33f07 43 6f 6c 6c 20 26 26 20 70 43 6f 6c 6c 2d 3e 78  Coll && pColl->x
33f08 43 6d 70 20 29 7b 0a 20 20 20 20 69 66 28 20 64  Cmp ){.    if( d
33f09 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e 74  b->activeVdbeCnt
33f0a 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
33f0b 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
33f0c 45 5f 42 55 53 59 2c 20 0a 20 20 20 20 20 20 20  E_BUSY, .       
33f0d 20 22 75 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65   "unable to dele
33f0e 74 65 2f 6d 6f 64 69 66 79 20 63 6f 6c 6c 61 74  te/modify collat
33f0f 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 64 75 65  ion sequence due
33f10 20 74 6f 20 61 63 74 69 76 65 20 73 74 61 74 65   to active state
33f11 6d 65 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 72  ments");.      r
33f12 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
33f13 59 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  Y;.    }.    sql
33f14 69 74 65 33 45 78 70 69 72 65 50 72 65 70 61 72  ite3ExpirePrepar
33f15 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64 62 29  edStatements(db)
33f16 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 63 6f 6c  ;..    /* If col
33f17 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
33f18 70 43 6f 6c 6c 20 77 61 73 20 63 72 65 61 74 65  pColl was create
33f19 64 20 64 69 72 65 63 74 6c 79 20 62 79 20 61 20  d directly by a 
33f1a 63 61 6c 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73  call to.    ** s
33f1b 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
33f1c 6c 6c 61 74 69 6f 6e 2c 20 61 6e 64 20 6e 6f 74  llation, and not
33f1d 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 73 79   generated by sy
33f1e 6e 74 68 43 6f 6c 6c 53 65 71 28 29 2c 0a 20 20  nthCollSeq(),.  
33f1f 20 20 2a 2a 20 74 68 65 6e 20 61 6e 79 20 63 6f    ** then any co
33f20 70 69 65 73 20 6d 61 64 65 20 62 79 20 73 79 6e  pies made by syn
33f21 74 68 43 6f 6c 6c 53 65 71 28 29 20 6e 65 65 64  thCollSeq() need
33f22 20 74 6f 20 62 65 20 69 6e 76 61 6c 69 64 61 74   to be invalidat
33f23 65 64 2e 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c  ed..    ** Also,
33f24 20 63 6f 6c 6c 61 74 69 6f 6e 20 64 65 73 74 72   collation destr
33f25 75 63 74 6f 72 20 2d 20 43 6f 6c 6c 53 65 71 2e  uctor - CollSeq.
33f26 78 44 65 6c 28 29 20 2d 20 66 75 6e 63 74 69 6f  xDel() - functio
33f27 6e 20 6d 61 79 20 6e 65 65 64 0a 20 20 20 20 2a  n may need.    *
33f28 2a 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 2e 0a  * to be called..
33f29 20 20 20 20 2a 2f 20 0a 20 20 20 20 69 66 28 20      */ .    if( 
33f2a 28 70 43 6f 6c 6c 2d 3e 65 6e 63 20 26 20 7e 53  (pColl->enc & ~S
33f2b 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49 47  QLITE_UTF16_ALIG
33f2c 4e 45 44 29 3d 3d 65 6e 63 32 20 29 7b 0a 20 20  NED)==enc2 ){.  
33f2d 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f      CollSeq *aCo
33f2e 6c 6c 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68  ll = sqlite3Hash
33f2f 46 69 6e 64 28 26 64 62 2d 3e 61 43 6f 6c 6c 53  Find(&db->aCollS
33f30 65 71 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  eq, zName, nName
33f31 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a  );.      int j;.
33f32 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
33f33 3c 33 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  <3; j++){.      
33f34 20 20 43 6f 6c 6c 53 65 71 20 2a 70 20 3d 20 26    CollSeq *p = &
33f35 61 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20  aColl[j];.      
33f36 20 20 69 66 28 20 70 2d 3e 65 6e 63 3d 3d 70 43    if( p->enc==pC
33f37 6f 6c 6c 2d 3e 65 6e 63 20 29 7b 0a 20 20 20 20  oll->enc ){.    
33f38 20 20 20 20 20 20 69 66 28 20 70 2d 3e 78 44 65        if( p->xDe
33f39 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  l ){.           
33f3a 20 70 2d 3e 78 44 65 6c 28 70 2d 3e 70 55 73 65   p->xDel(p->pUse
33f3b 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  r);.          }.
33f3c 20 20 20 20 20 20 20 20 20 20 70 2d 3e 78 43 6d            p->xCm
33f3d 70 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d  p = 0;.        }
33f3e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
33f3f 20 7d 0a 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71   }..  pColl = sq
33f40 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71  lite3FindCollSeq
33f41 28 64 62 2c 20 28 75 38 29 65 6e 63 32 2c 20 7a  (db, (u8)enc2, z
33f42 4e 61 6d 65 2c 20 31 29 3b 0a 20 20 69 66 28 20  Name, 1);.  if( 
33f43 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43 6f  pColl ){.    pCo
33f44 6c 6c 2d 3e 78 43 6d 70 20 3d 20 78 43 6f 6d 70  ll->xCmp = xComp
33f45 61 72 65 3b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e  are;.    pColl->
33f46 70 55 73 65 72 20 3d 20 70 43 74 78 3b 0a 20 20  pUser = pCtx;.  
33f47 20 20 70 43 6f 6c 6c 2d 3e 78 44 65 6c 20 3d 20    pColl->xDel = 
33f48 78 44 65 6c 3b 0a 20 20 20 20 70 43 6f 6c 6c 2d  xDel;.    pColl-
33f49 3e 65 6e 63 20 3d 20 28 75 38 29 28 65 6e 63 32  >enc = (u8)(enc2
33f4a 20 7c 20 28 65 6e 63 20 26 20 53 51 4c 49 54 45   | (enc & SQLITE
33f4b 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 29  _UTF16_ALIGNED))
33f4c 3b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e 74 79 70  ;.    pColl->typ
33f4d 65 20 3d 20 63 6f 6c 6c 54 79 70 65 3b 0a 20 20  e = collType;.  
33f4e 7d 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  }.  sqlite3Error
33f4f 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20  (db, SQLITE_OK, 
33f50 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  0);.  return SQL
33f51 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  ITE_OK;.}.../*.*
33f52 2a 20 54 68 69 73 20 61 72 72 61 79 20 64 65 66  * This array def
33f53 69 6e 65 73 20 68 61 72 64 20 75 70 70 65 72 20  ines hard upper 
33f54 62 6f 75 6e 64 73 20 6f 6e 20 6c 69 6d 69 74 20  bounds on limit 
33f55 76 61 6c 75 65 73 2e 20 20 54 68 65 0a 2a 2a 20  values.  The.** 
33f56 69 6e 69 74 69 61 6c 69 7a 65 72 20 6d 75 73 74  initializer must
33f57 20 62 65 20 6b 65 70 74 20 69 6e 20 73 79 6e 63   be kept in sync
33f58 20 77 69 74 68 20 74 68 65 20 53 51 4c 49 54 45   with the SQLITE
33f59 5f 4c 49 4d 49 54 5f 2a 0a 2a 2a 20 23 64 65 66  _LIMIT_*.** #def
33f5a 69 6e 65 73 20 69 6e 20 73 71 6c 69 74 65 33 2e  ines in sqlite3.
33f5b 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  h..*/.static con
33f5c 73 74 20 69 6e 74 20 61 48 61 72 64 4c 69 6d 69  st int aHardLimi
33f5d 74 5b 5d 20 3d 20 7b 0a 20 20 53 51 4c 49 54 45  t[] = {.  SQLITE
33f5e 5f 4d 41 58 5f 4c 45 4e 47 54 48 2c 0a 20 20 53  _MAX_LENGTH,.  S
33f5f 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45  QLITE_MAX_SQL_LE
33f60 4e 47 54 48 2c 0a 20 20 53 51 4c 49 54 45 5f 4d  NGTH,.  SQLITE_M
33f61 41 58 5f 43 4f 4c 55 4d 4e 2c 0a 20 20 53 51 4c  AX_COLUMN,.  SQL
33f62 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50  ITE_MAX_EXPR_DEP
33f63 54 48 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58  TH,.  SQLITE_MAX
33f64 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
33f65 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56  ,.  SQLITE_MAX_V
33f66 44 42 45 5f 4f 50 2c 0a 20 20 53 51 4c 49 54 45  DBE_OP,.  SQLITE
33f67 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52  _MAX_FUNCTION_AR
33f68 47 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58 5f  G,.  SQLITE_MAX_
33f69 41 54 54 41 43 48 45 44 2c 0a 20 20 53 51 4c 49  ATTACHED,.  SQLI
33f6a 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54  TE_MAX_LIKE_PATT
33f6b 45 52 4e 5f 4c 45 4e 47 54 48 2c 0a 20 20 53 51  ERN_LENGTH,.  SQ
33f6c 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c  LITE_MAX_VARIABL
33f6d 45 5f 4e 55 4d 42 45 52 2c 0a 20 20 53 51 4c 49  E_NUMBER,.  SQLI
33f6e 54 45 5f 4d 41 58 5f 54 52 49 47 47 45 52 5f 44  TE_MAX_TRIGGER_D
33f6f 45 50 54 48 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  EPTH,.};../*.** 
33f70 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 68 61  Make sure the ha
33f71 72 64 20 6c 69 6d 69 74 73 20 61 72 65 20 73 65  rd limits are se
33f72 74 20 74 6f 20 72 65 61 73 6f 6e 61 62 6c 65 20  t to reasonable 
33f73 76 61 6c 75 65 73 0a 2a 2f 0a 23 69 66 20 53 51  values.*/.#if SQ
33f74 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 3c  LITE_MAX_LENGTH<
33f75 31 30 30 0a 23 20 65 72 72 6f 72 20 53 51 4c 49  100.# error SQLI
33f76 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 6d 75  TE_MAX_LENGTH mu
33f77 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 31  st be at least 1
33f78 30 30 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51  00.#endif.#if SQ
33f79 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e  LITE_MAX_SQL_LEN
33f7a 47 54 48 3c 31 30 30 0a 23 20 65 72 72 6f 72 20  GTH<100.# error 
33f7b 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c  SQLITE_MAX_SQL_L
33f7c 45 4e 47 54 48 20 6d 75 73 74 20 62 65 20 61 74  ENGTH must be at
33f7d 20 6c 65 61 73 74 20 31 30 30 0a 23 65 6e 64 69   least 100.#endi
33f7e 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  f.#if SQLITE_MAX
33f7f 5f 53 51 4c 5f 4c 45 4e 47 54 48 3e 53 51 4c 49  _SQL_LENGTH>SQLI
33f80 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 23 20  TE_MAX_LENGTH.# 
33f81 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58  error SQLITE_MAX
33f82 5f 53 51 4c 5f 4c 45 4e 47 54 48 20 6d 75 73 74  _SQL_LENGTH must
33f83 20 6e 6f 74 20 62 65 20 67 72 65 61 74 65 72 20   not be greater 
33f84 74 68 61 6e 20 53 51 4c 49 54 45 5f 4d 41 58 5f  than SQLITE_MAX_
33f85 4c 45 4e 47 54 48 0a 23 65 6e 64 69 66 0a 23 69  LENGTH.#endif.#i
33f86 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4d  f SQLITE_MAX_COM
33f87 50 4f 55 4e 44 5f 53 45 4c 45 43 54 3c 32 0a 23  POUND_SELECT<2.#
33f88 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41   error SQLITE_MA
33f89 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43  X_COMPOUND_SELEC
33f8a 54 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61  T must be at lea
33f8b 73 74 20 32 0a 23 65 6e 64 69 66 0a 23 69 66 20  st 2.#endif.#if 
33f8c 53 51 4c 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f  SQLITE_MAX_VDBE_
33f8d 4f 50 3c 34 30 0a 23 20 65 72 72 6f 72 20 53 51  OP<40.# error SQ
33f8e 4c 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50  LITE_MAX_VDBE_OP
33f8f 20 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73   must be at leas
33f90 74 20 34 30 0a 23 65 6e 64 69 66 0a 23 69 66 20  t 40.#endif.#if 
33f91 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54  SQLITE_MAX_FUNCT
33f92 49 4f 4e 5f 41 52 47 3c 30 20 7c 7c 20 53 51 4c  ION_ARG<0 || SQL
33f93 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e  ITE_MAX_FUNCTION
33f94 5f 41 52 47 3e 31 30 30 30 0a 23 20 65 72 72 6f  _ARG>1000.# erro
33f95 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e  r SQLITE_MAX_FUN
33f96 43 54 49 4f 4e 5f 41 52 47 20 6d 75 73 74 20 62  CTION_ARG must b
33f97 65 20 62 65 74 77 65 65 6e 20 30 20 61 6e 64 20  e between 0 and 
33f98 31 30 30 30 0a 23 65 6e 64 69 66 0a 23 69 66 20  1000.#endif.#if 
33f99 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43  SQLITE_MAX_ATTAC
33f9a 48 45 44 3c 30 20 7c 7c 20 53 51 4c 49 54 45 5f  HED<0 || SQLITE_
33f9b 4d 41 58 5f 41 54 54 41 43 48 45 44 3e 33 30 0a  MAX_ATTACHED>30.
33f9c 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d  # error SQLITE_M
33f9d 41 58 5f 41 54 54 41 43 48 45 44 20 6d 75 73 74  AX_ATTACHED must
33f9e 20 62 65 20 62 65 74 77 65 65 6e 20 30 20 61 6e   be between 0 an
33f9f 64 20 33 30 0a 23 65 6e 64 69 66 0a 23 69 66 20  d 30.#endif.#if 
33fa0 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f  SQLITE_MAX_LIKE_
33fa1 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 3c 31  PATTERN_LENGTH<1
33fa2 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f  .# error SQLITE_
33fa3 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e  MAX_LIKE_PATTERN
33fa4 5f 4c 45 4e 47 54 48 20 6d 75 73 74 20 62 65 20  _LENGTH must be 
33fa5 61 74 20 6c 65 61 73 74 20 31 0a 23 65 6e 64 69  at least 1.#endi
33fa6 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  f.#if SQLITE_MAX
33fa7 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
33fa8 3c 31 20 7c 7c 20 53 51 4c 49 54 45 5f 4d 41 58  <1 || SQLITE_MAX
33fa9 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
33faa 3e 33 32 37 36 37 0a 23 20 65 72 72 6f 72 20 53  >32767.# error S
33fab 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41 42  QLITE_MAX_VARIAB
33fac 4c 45 5f 4e 55 4d 42 45 52 20 6d 75 73 74 20 62  LE_NUMBER must b
33fad 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20  e between 1 and 
33fae 33 32 37 36 37 0a 23 65 6e 64 69 66 0a 23 69 66  32767.#endif.#if
33faf 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55   SQLITE_MAX_COLU
33fb0 4d 4e 3e 33 32 37 36 37 0a 23 20 65 72 72 6f 72  MN>32767.# error
33fb1 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55   SQLITE_MAX_COLU
33fb2 4d 4e 20 6d 75 73 74 20 6e 6f 74 20 65 78 63 65  MN must not exce
33fb3 65 64 20 33 32 37 36 37 0a 23 65 6e 64 69 66 0a  ed 32767.#endif.
33fb4 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 54  #if SQLITE_MAX_T
33fb5 52 49 47 47 45 52 5f 44 45 50 54 48 3c 31 0a 23  RIGGER_DEPTH<1.#
33fb6 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41   error SQLITE_MA
33fb7 58 5f 54 52 49 47 47 45 52 5f 44 45 50 54 48 20  X_TRIGGER_DEPTH 
33fb8 6d 75 73 74 20 62 65 20 61 74 20 6c 65 61 73 74  must be at least
33fb9 20 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a   1.#endif.../*.*
33fba 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c  * Change the val
33fbb 75 65 20 6f 66 20 61 20 6c 69 6d 69 74 2e 20 20  ue of a limit.  
33fbc 52 65 70 6f 72 74 20 74 68 65 20 6f 6c 64 20 76  Report the old v
33fbd 61 6c 75 65 2e 0a 2a 2a 20 49 66 20 61 6e 20 69  alue..** If an i
33fbe 6e 76 61 6c 69 64 20 6c 69 6d 69 74 20 69 6e 64  nvalid limit ind
33fbf 65 78 20 69 73 20 73 75 70 70 6c 69 65 64 2c 20  ex is supplied, 
33fc0 72 65 70 6f 72 74 20 2d 31 2e 0a 2a 2a 20 4d 61  report -1..** Ma
33fc1 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 62 75  ke no changes bu
33fc2 74 20 73 74 69 6c 6c 20 72 65 70 6f 72 74 20 74  t still report t
33fc3 68 65 20 6f 6c 64 20 76 61 6c 75 65 20 69 66 20  he old value if 
33fc4 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 69 6d 69 74  the.** new limit
33fc5 20 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a   is negative..**
33fc6 0a 2a 2a 20 41 20 6e 65 77 20 6c 6f 77 65 72 20  .** A new lower 
33fc7 6c 69 6d 69 74 20 64 6f 65 73 20 6e 6f 74 20 73  limit does not s
33fc8 68 72 69 6e 6b 20 65 78 69 73 74 69 6e 67 20 63  hrink existing c
33fc9 6f 6e 73 74 72 75 63 74 73 2e 0a 2a 2a 20 49 74  onstructs..** It
33fca 20 6d 65 72 65 6c 79 20 70 72 65 76 65 6e 74 73   merely prevents
33fcb 20 6e 65 77 20 63 6f 6e 73 74 72 75 63 74 73 20   new constructs 
33fcc 74 68 61 74 20 65 78 63 65 65 64 20 74 68 65 20  that exceed the 
33fcd 6c 69 6d 69 74 0a 2a 2a 20 66 72 6f 6d 20 66 6f  limit.** from fo
33fce 72 6d 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rming..*/.SQLITE
33fcf 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
33fd0 5f 6c 69 6d 69 74 28 73 71 6c 69 74 65 33 20 2a  _limit(sqlite3 *
33fd1 64 62 2c 20 69 6e 74 20 6c 69 6d 69 74 49 64 2c  db, int limitId,
33fd2 20 69 6e 74 20 6e 65 77 4c 69 6d 69 74 29 7b 0a   int newLimit){.
33fd3 20 20 69 6e 74 20 6f 6c 64 4c 69 6d 69 74 3b 0a    int oldLimit;.
33fd4 20 20 69 66 28 20 6c 69 6d 69 74 49 64 3c 30 20    if( limitId<0 
33fd5 7c 7c 20 6c 69 6d 69 74 49 64 3e 3d 53 51 4c 49  || limitId>=SQLI
33fd6 54 45 5f 4e 5f 4c 49 4d 49 54 20 29 7b 0a 20 20  TE_N_LIMIT ){.  
33fd7 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d    return -1;.  }
33fd8 0a 20 20 6f 6c 64 4c 69 6d 69 74 20 3d 20 64 62  .  oldLimit = db
33fd9 2d 3e 61 4c 69 6d 69 74 5b 6c 69 6d 69 74 49 64  ->aLimit[limitId
33fda 5d 3b 0a 20 20 69 66 28 20 6e 65 77 4c 69 6d 69  ];.  if( newLimi
33fdb 74 3e 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  t>=0 ){.    if( 
33fdc 6e 65 77 4c 69 6d 69 74 3e 61 48 61 72 64 4c 69  newLimit>aHardLi
33fdd 6d 69 74 5b 6c 69 6d 69 74 49 64 5d 20 29 7b 0a  mit[limitId] ){.
33fde 20 20 20 20 20 20 6e 65 77 4c 69 6d 69 74 20 3d        newLimit =
33fdf 20 61 48 61 72 64 4c 69 6d 69 74 5b 6c 69 6d 69   aHardLimit[limi
33fe0 74 49 64 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  tId];.    }.    
33fe1 64 62 2d 3e 61 4c 69 6d 69 74 5b 6c 69 6d 69 74  db->aLimit[limit
33fe2 49 64 5d 20 3d 20 6e 65 77 4c 69 6d 69 74 3b 0a  Id] = newLimit;.
33fe3 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6f 6c 64    }.  return old
33fe4 4c 69 6d 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Limit;.}../*.** 
33fe5 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65  This routine doe
33fe6 73 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 6f 70  s the work of op
33fe7 65 6e 69 6e 67 20 61 20 64 61 74 61 62 61 73 65  ening a database
33fe8 20 6f 6e 20 62 65 68 61 6c 66 20 6f 66 0a 2a 2a   on behalf of.**
33fe9 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20   sqlite3_open() 
33fea 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  and sqlite3_open
33feb 31 36 28 29 2e 20 54 68 65 20 64 61 74 61 62 61  16(). The databa
33fec 73 65 20 66 69 6c 65 6e 61 6d 65 20 22 7a 46 69  se filename "zFi
33fed 6c 65 6e 61 6d 65 22 20 20 0a 2a 2a 20 69 73 20  lename"  .** is 
33fee 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 2e 0a 2a  UTF-8 encoded..*
33fef 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65  /.static int ope
33ff0 6e 44 61 74 61 62 61 73 65 28 0a 20 20 63 6f 6e  nDatabase(.  con
33ff1 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
33ff2 6d 65 2c 20 2f 2a 20 44 61 74 61 62 61 73 65 20  me, /* Database 
33ff3 66 69 6c 65 6e 61 6d 65 20 55 54 46 2d 38 20 65  filename UTF-8 e
33ff4 6e 63 6f 64 65 64 20 2a 2f 0a 20 20 73 71 6c 69  ncoded */.  sqli
33ff5 74 65 33 20 2a 2a 70 70 44 62 2c 20 20 20 20 20  te3 **ppDb,     
33ff6 20 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72     /* OUT: Retur
33ff7 6e 65 64 20 64 61 74 61 62 61 73 65 20 68 61 6e  ned database han
33ff8 64 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  dle */.  unsigne
33ff9 64 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20  d flags,        
33ffa 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 61 6c 20 66  /* Operational f
33ffb 6c 61 67 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  lags */.  const 
33ffc 63 68 61 72 20 2a 7a 56 66 73 20 20 20 20 20 20  char *zVfs      
33ffd 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
33ffe 56 46 53 20 74 6f 20 75 73 65 20 2a 2f 0a 29 7b  VFS to use */.){
33fff 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
34000 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20    int rc;.  int 
34001 69 73 54 68 72 65 61 64 73 61 66 65 3b 0a 0a 20  isThreadsafe;.. 
34002 20 2a 70 70 44 62 20 3d 20 30 3b 0a 23 69 66 6e   *ppDb = 0;.#ifn
34003 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
34004 41 55 54 4f 49 4e 49 54 0a 20 20 72 63 20 3d 20  AUTOINIT.  rc = 
34005 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
34006 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29  ze();.  if( rc )
34007 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64   return rc;.#end
34008 69 66 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65  if..  if( sqlite
34009 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43  3GlobalConfig.bC
3400a 6f 72 65 4d 75 74 65 78 3d 3d 30 20 29 7b 0a 20  oreMutex==0 ){. 
3400b 20 20 20 69 73 54 68 72 65 61 64 73 61 66 65 20     isThreadsafe 
3400c 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  = 0;.  }else if(
3400d 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f   flags & SQLITE_
3400e 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 20 29 7b 0a  OPEN_NOMUTEX ){.
3400f 20 20 20 20 69 73 54 68 72 65 61 64 73 61 66 65      isThreadsafe
34010 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66   = 0;.  }else if
34011 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ( flags & SQLITE
34012 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 20  _OPEN_FULLMUTEX 
34013 29 7b 0a 20 20 20 20 69 73 54 68 72 65 61 64 73  ){.    isThreads
34014 61 66 65 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  afe = 1;.  }else
34015 7b 0a 20 20 20 20 69 73 54 68 72 65 61 64 73 61  {.    isThreadsa
34016 66 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  fe = sqlite3Glob
34017 61 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75  alConfig.bFullMu
34018 74 65 78 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66  tex;.  }.  if( f
34019 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50  lags & SQLITE_OP
3401a 45 4e 5f 50 52 49 56 41 54 45 43 41 43 48 45 20  EN_PRIVATECACHE 
3401b 29 7b 0a 20 20 20 20 66 6c 61 67 73 20 26 3d 20  ){.    flags &= 
3401c 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 48 41  ~SQLITE_OPEN_SHA
3401d 52 45 44 43 41 43 48 45 3b 0a 20 20 7d 65 6c 73  REDCACHE;.  }els
3401e 65 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f  e if( sqlite3Glo
3401f 62 61 6c 43 6f 6e 66 69 67 2e 73 68 61 72 65 64  balConfig.shared
34020 43 61 63 68 65 45 6e 61 62 6c 65 64 20 29 7b 0a  CacheEnabled ){.
34021 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 53 51 4c      flags |= SQL
34022 49 54 45 5f 4f 50 45 4e 5f 53 48 41 52 45 44 43  ITE_OPEN_SHAREDC
34023 41 43 48 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ACHE;.  }..  /* 
34024 52 65 6d 6f 76 65 20 68 61 72 6d 66 75 6c 20 62  Remove harmful b
34025 69 74 73 20 66 72 6f 6d 20 74 68 65 20 66 6c 61  its from the fla
34026 67 73 20 70 61 72 61 6d 65 74 65 72 0a 20 20 2a  gs parameter.  *
34027 2a 0a 20 20 2a 2a 20 54 68 65 20 53 51 4c 49 54  *.  ** The SQLIT
34028 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 20 61  E_OPEN_NOMUTEX a
34029 6e 64 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46  nd SQLITE_OPEN_F
3402a 55 4c 4c 4d 55 54 45 58 20 66 6c 61 67 73 20 77  ULLMUTEX flags w
3402b 65 72 65 0a 20 20 2a 2a 20 64 65 61 6c 74 20 77  ere.  ** dealt w
3402c 69 74 68 20 69 6e 20 74 68 65 20 70 72 65 76 69  ith in the previ
3402d 6f 75 73 20 63 6f 64 65 20 62 6c 6f 63 6b 2e 20  ous code block. 
3402e 20 42 65 73 69 64 65 73 20 74 68 65 73 65 2c 20   Besides these, 
3402f 74 68 65 20 6f 6e 6c 79 0a 20 20 2a 2a 20 76 61  the only.  ** va
34030 6c 69 64 20 69 6e 70 75 74 20 66 6c 61 67 73 20  lid input flags 
34031 66 6f 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  for sqlite3_open
34032 5f 76 32 28 29 20 61 72 65 20 53 51 4c 49 54 45  _v2() are SQLITE
34033 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 2c 0a  _OPEN_READONLY,.
34034 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 50 45 4e    ** SQLITE_OPEN
34035 5f 52 45 41 44 57 52 49 54 45 2c 20 61 6e 64 20  _READWRITE, and 
34036 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
34037 54 45 2e 20 20 53 69 6c 65 6e 74 6c 79 20 6d 61  TE.  Silently ma
34038 73 6b 0a 20 20 2a 2a 20 6f 66 66 20 61 6c 6c 20  sk.  ** off all 
34039 6f 74 68 65 72 20 66 6c 61 67 73 2e 0a 20 20 2a  other flags..  *
3403a 2f 0a 20 20 66 6c 61 67 73 20 26 3d 20 20 7e 28  /.  flags &=  ~(
3403b 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c   SQLITE_OPEN_DEL
3403c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c 0a 20 20 20  ETEONCLOSE |.   
3403d 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c 49              SQLI
3403e 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56  TE_OPEN_EXCLUSIV
3403f 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20  E |.            
34040 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d     SQLITE_OPEN_M
34041 41 49 4e 5f 44 42 20 7c 0a 20 20 20 20 20 20 20  AIN_DB |.       
34042 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f          SQLITE_O
34043 50 45 4e 5f 54 45 4d 50 5f 44 42 20 7c 20 0a 20  PEN_TEMP_DB | . 
34044 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
34045 4c 49 54 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49  LITE_OPEN_TRANSI
34046 45 4e 54 5f 44 42 20 7c 20 0a 20 20 20 20 20 20  ENT_DB | .      
34047 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
34048 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41  OPEN_MAIN_JOURNA
34049 4c 20 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20  L | .           
3404a 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
3404b 54 45 4d 50 5f 4a 4f 55 52 4e 41 4c 20 7c 20 0a  TEMP_JOURNAL | .
3404c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
3404d 51 4c 49 54 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f  QLITE_OPEN_SUBJO
3404e 55 52 4e 41 4c 20 7c 20 0a 20 20 20 20 20 20 20  URNAL | .       
3404f 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f          SQLITE_O
34050 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f 55 52 4e  PEN_MASTER_JOURN
34051 41 4c 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20  AL |.           
34052 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
34053 4e 4f 4d 55 54 45 58 20 7c 0a 20 20 20 20 20 20  NOMUTEX |.      
34054 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
34055 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 0a 20  OPEN_FULLMUTEX. 
34056 20 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a 0a              );..
34057 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74 68    /* Allocate th
34058 65 20 73 71 6c 69 74 65 20 64 61 74 61 20 73 74  e sqlite data st
34059 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 64 62 20  ructure */.  db 
3405a 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a  = sqlite3MallocZ
3405b 65 72 6f 28 20 73 69 7a 65 6f 66 28 73 71 6c 69  ero( sizeof(sqli
3405c 74 65 33 29 20 29 3b 0a 20 20 69 66 28 20 64 62  te3) );.  if( db
3405d 3d 3d 30 20 29 20 67 6f 74 6f 20 6f 70 65 6e 64  ==0 ) goto opend
3405e 62 5f 6f 75 74 3b 0a 20 20 69 66 28 20 69 73 54  b_out;.  if( isT
3405f 68 72 65 61 64 73 61 66 65 20 29 7b 0a 20 20 20  hreadsafe ){.   
34060 20 64 62 2d 3e 6d 75 74 65 78 20 3d 20 73 71 6c   db->mutex = sql
34061 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
34062 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
34063 52 53 49 56 45 29 3b 0a 20 20 20 20 69 66 28 20  RSIVE);.    if( 
34064 64 62 2d 3e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a  db->mutex==0 ){.
34065 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72        sqlite3_fr
34066 65 65 28 64 62 29 3b 0a 20 20 20 20 20 20 64 62  ee(db);.      db
34067 20 3d 20 30 3b 0a 20 20 20 20 20 20 67 6f 74 6f   = 0;.      goto
34068 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 20   opendb_out;.   
34069 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
3406a 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
3406b 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 65  >mutex);.  db->e
3406c 72 72 4d 61 73 6b 20 3d 20 30 78 66 66 3b 0a 20  rrMask = 0xff;. 
3406d 20 64 62 2d 3e 6e 44 62 20 3d 20 32 3b 0a 20 20   db->nDb = 2;.  
3406e 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49  db->magic = SQLI
3406f 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 3b 0a 20  TE_MAGIC_BUSY;. 
34070 20 64 62 2d 3e 61 44 62 20 3d 20 64 62 2d 3e 61   db->aDb = db->a
34071 44 62 53 74 61 74 69 63 3b 0a 0a 20 20 61 73 73  DbStatic;..  ass
34072 65 72 74 28 20 73 69 7a 65 6f 66 28 64 62 2d 3e  ert( sizeof(db->
34073 61 4c 69 6d 69 74 29 3d 3d 73 69 7a 65 6f 66 28  aLimit)==sizeof(
34074 61 48 61 72 64 4c 69 6d 69 74 29 20 29 3b 0a 20  aHardLimit) );. 
34075 20 6d 65 6d 63 70 79 28 64 62 2d 3e 61 4c 69 6d   memcpy(db->aLim
34076 69 74 2c 20 61 48 61 72 64 4c 69 6d 69 74 2c 20  it, aHardLimit, 
34077 73 69 7a 65 6f 66 28 64 62 2d 3e 61 4c 69 6d 69  sizeof(db->aLimi
34078 74 29 29 3b 0a 20 20 64 62 2d 3e 61 75 74 6f 43  t));.  db->autoC
34079 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 64 62 2d  ommit = 1;.  db-
3407a 3e 6e 65 78 74 41 75 74 6f 76 61 63 20 3d 20 2d  >nextAutovac = -
3407b 31 3b 0a 20 20 64 62 2d 3e 6e 65 78 74 50 61 67  1;.  db->nextPag
3407c 65 73 69 7a 65 20 3d 20 30 3b 0a 20 20 64 62 2d  esize = 0;.  db-
3407d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45  >flags |= SQLITE
3407e 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 0a 23  _ShortColNames.#
3407f 69 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  if SQLITE_DEFAUL
34080 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 3c 34 0a  T_FILE_FORMAT<4.
34081 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34082 20 7c 20 53 51 4c 49 54 45 5f 4c 65 67 61 63 79   | SQLITE_Legacy
34083 46 69 6c 65 46 6d 74 0a 23 65 6e 64 69 66 0a 23  FileFmt.#endif.#
34084 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
34085 42 4c 45 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  BLE_LOAD_EXTENSI
34086 4f 4e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ON.             
34087 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 4c 6f 61      | SQLITE_Loa
34088 64 45 78 74 65 6e 73 69 6f 6e 0a 23 65 6e 64 69  dExtension.#endi
34089 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 44 45 46  f.#if SQLITE_DEF
3408a 41 55 4c 54 5f 52 45 43 55 52 53 49 56 45 5f 54  AULT_RECURSIVE_T
3408b 52 49 47 47 45 52 53 0a 20 20 20 20 20 20 20 20  RIGGERS.        
3408c 20 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54           | SQLIT
3408d 45 5f 52 65 63 54 72 69 67 67 65 72 73 0a 23 65  E_RecTriggers.#e
3408e 6e 64 69 66 0a 20 20 20 20 20 20 3b 0a 20 20 73  ndif.      ;.  s
3408f 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26  qlite3HashInit(&
34090 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b 0a 23  db->aCollSeq);.#
34091 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
34092 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
34093 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69    sqlite3HashIni
34094 74 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65 29 3b  t(&db->aModule);
34095 0a 23 65 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 70  .#endif..  db->p
34096 56 66 73 20 3d 20 73 71 6c 69 74 65 33 5f 76 66  Vfs = sqlite3_vf
34097 73 5f 66 69 6e 64 28 7a 56 66 73 29 3b 0a 20 20  s_find(zVfs);.  
34098 69 66 28 20 21 64 62 2d 3e 70 56 66 73 20 29 7b  if( !db->pVfs ){
34099 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
3409a 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69  _ERROR;.    sqli
3409b 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c  te3Error(db, rc,
3409c 20 22 6e 6f 20 73 75 63 68 20 76 66 73 3a 20 25   "no such vfs: %
3409d 73 22 2c 20 7a 56 66 73 29 3b 0a 20 20 20 20 67  s", zVfs);.    g
3409e 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a  oto opendb_out;.
3409f 20 20 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68    }..  /* Add th
340a0 65 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74  e default collat
340a1 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 42 49 4e  ion sequence BIN
340a2 41 52 59 2e 20 42 49 4e 41 52 59 20 77 6f 72 6b  ARY. BINARY work
340a3 73 20 66 6f 72 20 62 6f 74 68 20 55 54 46 2d 38  s for both UTF-8
340a4 0a 20 20 2a 2a 20 61 6e 64 20 55 54 46 2d 31 36  .  ** and UTF-16
340a5 2c 20 73 6f 20 61 64 64 20 61 20 76 65 72 73 69  , so add a versi
340a6 6f 6e 20 66 6f 72 20 65 61 63 68 20 74 6f 20 61  on for each to a
340a7 76 6f 69 64 20 61 6e 79 20 75 6e 6e 65 63 65 73  void any unneces
340a8 73 61 72 79 0a 20 20 2a 2a 20 63 6f 6e 76 65 72  sary.  ** conver
340a9 73 69 6f 6e 73 2e 20 54 68 65 20 6f 6e 6c 79 20  sions. The only 
340aa 65 72 72 6f 72 20 74 68 61 74 20 63 61 6e 20 6f  error that can o
340ab 63 63 75 72 20 68 65 72 65 20 69 73 20 61 20 6d  ccur here is a m
340ac 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65 2e  alloc() failure.
340ad 0a 20 20 2a 2f 0a 20 20 63 72 65 61 74 65 43 6f  .  */.  createCo
340ae 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22 42 49 4e  llation(db, "BIN
340af 41 52 59 22 2c 20 53 51 4c 49 54 45 5f 55 54 46  ARY", SQLITE_UTF
340b0 38 2c 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42  8, SQLITE_COLL_B
340b1 49 4e 41 52 59 2c 20 30 2c 0a 20 20 20 20 20 20  INARY, 0,.      
340b2 20 20 20 20 20 20 20 20 20 20 20 20 62 69 6e 43              binC
340b3 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20 20 63  ollFunc, 0);.  c
340b4 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64  reateCollation(d
340b5 62 2c 20 22 42 49 4e 41 52 59 22 2c 20 53 51 4c  b, "BINARY", SQL
340b6 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53 51 4c  ITE_UTF16BE, SQL
340b7 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 2c  ITE_COLL_BINARY,
340b8 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
340b9 20 20 20 20 20 20 62 69 6e 43 6f 6c 6c 46 75 6e        binCollFun
340ba 63 2c 20 30 29 3b 0a 20 20 63 72 65 61 74 65 43  c, 0);.  createC
340bb 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 22 42 49  ollation(db, "BI
340bc 4e 41 52 59 22 2c 20 53 51 4c 49 54 45 5f 55 54  NARY", SQLITE_UT
340bd 46 31 36 4c 45 2c 20 53 51 4c 49 54 45 5f 43 4f  F16LE, SQLITE_CO
340be 4c 4c 5f 42 49 4e 41 52 59 2c 20 30 2c 0a 20 20  LL_BINARY, 0,.  
340bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
340c0 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b  binCollFunc, 0);
340c1 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69  .  createCollati
340c2 6f 6e 28 64 62 2c 20 22 52 54 52 49 4d 22 2c 20  on(db, "RTRIM", 
340c3 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c  SQLITE_UTF8, SQL
340c4 49 54 45 5f 43 4f 4c 4c 5f 55 53 45 52 2c 20 28  ITE_COLL_USER, (
340c5 76 6f 69 64 2a 29 31 2c 0a 20 20 20 20 20 20 20  void*)1,.       
340c6 20 20 20 20 20 20 20 20 20 20 20 62 69 6e 43 6f             binCo
340c7 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20 20 69 66  llFunc, 0);.  if
340c8 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
340c9 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6f  ed ){.    goto o
340ca 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 20  pendb_out;.  }. 
340cb 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 20 3d   db->pDfltColl =
340cc 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
340cd 53 65 71 28 64 62 2c 20 53 51 4c 49 54 45 5f 55  Seq(db, SQLITE_U
340ce 54 46 38 2c 20 22 42 49 4e 41 52 59 22 2c 20 30  TF8, "BINARY", 0
340cf 29 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  );.  assert( db-
340d0 3e 70 44 66 6c 74 43 6f 6c 6c 21 3d 30 20 29 3b  >pDfltColl!=0 );
340d1 0a 0a 20 20 2f 2a 20 41 6c 73 6f 20 61 64 64 20  ..  /* Also add 
340d2 61 20 55 54 46 2d 38 20 63 61 73 65 2d 69 6e 73  a UTF-8 case-ins
340d3 65 6e 73 69 74 69 76 65 20 63 6f 6c 6c 61 74 69  ensitive collati
340d4 6f 6e 20 73 65 71 75 65 6e 63 65 2e 20 2a 2f 0a  on sequence. */.
340d5 20 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f    createCollatio
340d6 6e 28 64 62 2c 20 22 4e 4f 43 41 53 45 22 2c 20  n(db, "NOCASE", 
340d7 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c  SQLITE_UTF8, SQL
340d8 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 2c  ITE_COLL_NOCASE,
340d9 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
340da 20 20 20 20 20 20 6e 6f 63 61 73 65 43 6f 6c 6c        nocaseColl
340db 61 74 69 6e 67 46 75 6e 63 2c 20 30 29 3b 0a 0a  atingFunc, 0);..
340dc 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 62 61    /* Open the ba
340dd 63 6b 65 6e 64 20 64 61 74 61 62 61 73 65 20 64  ckend database d
340de 72 69 76 65 72 20 2a 2f 0a 20 20 64 62 2d 3e 6f  river */.  db->o
340df 70 65 6e 46 6c 61 67 73 20 3d 20 66 6c 61 67 73  penFlags = flags
340e0 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
340e1 42 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c  BtreeFactory(db,
340e2 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 30 2c 20 53   zFilename, 0, S
340e3 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41  QLITE_DEFAULT_CA
340e4 43 48 45 5f 53 49 5a 45 2c 20 0a 20 20 20 20 20  CHE_SIZE, .     
340e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
340e6 20 20 20 20 20 20 66 6c 61 67 73 20 7c 20 53 51        flags | SQ
340e7 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44  LITE_OPEN_MAIN_D
340e8 42 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  B,.             
340e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 64                &d
340ea 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29 3b 0a  b->aDb[0].pBt);.
340eb 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
340ec 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 72  _OK ){.    if( r
340ed 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  c==SQLITE_IOERR_
340ee 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 72  NOMEM ){.      r
340ef 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
340f0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
340f1 74 65 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c  te3Error(db, rc,
340f2 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20 6f 70   0);.    goto op
340f3 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20  endb_out;.  }.  
340f4 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 53 63 68 65  db->aDb[0].pSche
340f5 6d 61 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  ma = sqlite3Sche
340f6 6d 61 47 65 74 28 64 62 2c 20 64 62 2d 3e 61 44  maGet(db, db->aD
340f7 62 5b 30 5d 2e 70 42 74 29 3b 0a 20 20 64 62 2d  b[0].pBt);.  db-
340f8 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20  >aDb[1].pSchema 
340f9 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 47  = sqlite3SchemaG
340fa 65 74 28 64 62 2c 20 30 29 3b 0a 0a 0a 20 20 2f  et(db, 0);...  /
340fb 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 73 61  * The default sa
340fc 66 65 74 79 5f 6c 65 76 65 6c 20 66 6f 72 20 74  fety_level for t
340fd 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
340fe 20 69 73 20 27 66 75 6c 6c 27 3b 20 66 6f 72 20   is 'full'; for 
340ff 74 68 65 20 74 65 6d 70 0a 20 20 2a 2a 20 64 61  the temp.  ** da
34100 74 61 62 61 73 65 20 69 74 20 69 73 20 27 4e 4f  tabase it is 'NO
34101 4e 45 27 2e 20 54 68 69 73 20 6d 61 74 63 68 65  NE'. This matche
34102 73 20 74 68 65 20 70 61 67 65 72 20 6c 61 79 65  s the pager laye
34103 72 20 64 65 66 61 75 6c 74 73 2e 20 20 0a 20 20  r defaults.  .  
34104 2a 2f 0a 20 20 64 62 2d 3e 61 44 62 5b 30 5d 2e  */.  db->aDb[0].
34105 7a 4e 61 6d 65 20 3d 20 22 6d 61 69 6e 22 3b 0a  zName = "main";.
34106 20 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 73 61 66    db->aDb[0].saf
34107 65 74 79 5f 6c 65 76 65 6c 20 3d 20 33 3b 0a 20  ety_level = 3;. 
34108 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 7a 4e 61 6d   db->aDb[1].zNam
34109 65 20 3d 20 22 74 65 6d 70 22 3b 0a 20 20 64 62  e = "temp";.  db
3410a 2d 3e 61 44 62 5b 31 5d 2e 73 61 66 65 74 79 5f  ->aDb[1].safety_
3410b 6c 65 76 65 6c 20 3d 20 31 3b 0a 0a 20 20 64 62  level = 1;..  db
3410c 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45  ->magic = SQLITE
3410d 5f 4d 41 47 49 43 5f 4f 50 45 4e 3b 0a 20 20 69  _MAGIC_OPEN;.  i
3410e 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
3410f 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  led ){.    goto 
34110 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a  opendb_out;.  }.
34111 0a 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 61  .  /* Register a
34112 6c 6c 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63  ll built-in func
34113 74 69 6f 6e 73 2c 20 62 75 74 20 64 6f 20 6e 6f  tions, but do no
34114 74 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 61  t attempt to rea
34115 64 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 62  d the.  ** datab
34116 61 73 65 20 73 63 68 65 6d 61 20 79 65 74 2e 20  ase schema yet. 
34117 54 68 69 73 20 69 73 20 64 65 6c 61 79 65 64 20  This is delayed 
34118 75 6e 74 69 6c 20 74 68 65 20 66 69 72 73 74 20  until the first 
34119 74 69 6d 65 20 74 68 65 20 64 61 74 61 62 61 73  time the databas
3411a 65 0a 20 20 2a 2a 20 69 73 20 61 63 63 65 73 73  e.  ** is access
3411b 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ed..  */.  sqlit
3411c 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49  e3Error(db, SQLI
3411d 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 73 71 6c  TE_OK, 0);.  sql
3411e 69 74 65 33 52 65 67 69 73 74 65 72 42 75 69 6c  ite3RegisterBuil
3411f 74 69 6e 46 75 6e 63 74 69 6f 6e 73 28 64 62 29  tinFunctions(db)
34120 3b 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 61 75 74  ;..  /* Load aut
34121 6f 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e  omatic extension
34122 73 20 2d 20 65 78 74 65 6e 73 69 6f 6e 73 20 74  s - extensions t
34123 68 61 74 20 68 61 76 65 20 62 65 65 6e 20 72 65  hat have been re
34124 67 69 73 74 65 72 65 64 0a 20 20 2a 2a 20 75 73  gistered.  ** us
34125 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ing the sqlite3_
34126 61 75 74 6f 6d 61 74 69 63 5f 65 78 74 65 6e 73  automatic_extens
34127 69 6f 6e 28 29 20 41 50 49 2e 0a 20 20 2a 2f 0a  ion() API..  */.
34128 20 20 73 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61    sqlite3AutoLoa
34129 64 45 78 74 65 6e 73 69 6f 6e 73 28 64 62 29 3b  dExtensions(db);
3412a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
3412b 65 72 72 63 6f 64 65 28 64 62 29 3b 0a 20 20 69  errcode(db);.  i
3412c 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3412d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65   ){.    goto ope
3412e 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 23 69  ndb_out;.  }..#i
3412f 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
34130 4c 45 5f 46 54 53 31 0a 20 20 69 66 28 20 21 64  LE_FTS1.  if( !d
34131 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
34132 29 7b 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e  ){.    extern in
34133 74 20 73 71 6c 69 74 65 33 46 74 73 31 49 6e 69  t sqlite3Fts1Ini
34134 74 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 20  t(sqlite3*);.   
34135 20 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73   rc = sqlite3Fts
34136 31 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23  1Init(db);.  }.#
34137 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
34138 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 32  LITE_ENABLE_FTS2
34139 0a 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c  .  if( !db->mall
3413a 6f 63 46 61 69 6c 65 64 20 26 26 20 72 63 3d 3d  ocFailed && rc==
3413b 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
3413c 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c 69   extern int sqli
3413d 74 65 33 46 74 73 32 49 6e 69 74 28 73 71 6c 69  te3Fts2Init(sqli
3413e 74 65 33 2a 29 3b 0a 20 20 20 20 72 63 20 3d 20  te3*);.    rc = 
3413f 73 71 6c 69 74 65 33 46 74 73 32 49 6e 69 74 28  sqlite3Fts2Init(
34140 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  db);.  }.#endif.
34141 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
34142 4e 41 42 4c 45 5f 46 54 53 33 0a 20 20 69 66 28  NABLE_FTS3.  if(
34143 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
34144 65 64 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45  ed && rc==SQLITE
34145 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  _OK ){.    rc = 
34146 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 28  sqlite3Fts3Init(
34147 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  db);.  }.#endif.
34148 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
34149 4e 41 42 4c 45 5f 49 43 55 0a 20 20 69 66 28 20  NABLE_ICU.  if( 
3414a 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
3414b 64 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f  d && rc==SQLITE_
3414c 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  OK ){.    rc = s
3414d 71 6c 69 74 65 33 49 63 75 49 6e 69 74 28 64 62  qlite3IcuInit(db
3414e 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23  );.  }.#endif..#
3414f 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
34150 42 4c 45 5f 52 54 52 45 45 0a 20 20 69 66 28 20  BLE_RTREE.  if( 
34151 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
34152 64 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f  d && rc==SQLITE_
34153 4f 4b 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  OK){.    rc = sq
34154 6c 69 74 65 33 52 74 72 65 65 49 6e 69 74 28 64  lite3RtreeInit(d
34155 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  b);.  }.#endif..
34156 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
34157 62 2c 20 72 63 2c 20 30 29 3b 0a 0a 20 20 2f 2a  b, rc, 0);..  /*
34158 20 2d 44 53 51 4c 49 54 45 5f 44 45 46 41 55 4c   -DSQLITE_DEFAUL
34159 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 3d 31  T_LOCKING_MODE=1
3415a 20 6d 61 6b 65 73 20 45 58 43 4c 55 53 49 56 45   makes EXCLUSIVE
3415b 20 74 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63   the default loc
3415c 6b 69 6e 67 0a 20 20 2a 2a 20 6d 6f 64 65 2e 20  king.  ** mode. 
3415d 20 2d 44 53 51 4c 49 54 45 5f 44 45 46 41 55 4c   -DSQLITE_DEFAUL
3415e 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 3d 30  T_LOCKING_MODE=0
3415f 20 6d 61 6b 65 20 4e 4f 52 4d 41 4c 20 74 68 65   make NORMAL the
34160 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67   default locking
34161 0a 20 20 2a 2a 20 6d 6f 64 65 2e 20 20 44 6f 69  .  ** mode.  Doi
34162 6e 67 20 6e 6f 74 68 69 6e 67 20 61 74 20 61 6c  ng nothing at al
34163 6c 20 61 6c 73 6f 20 6d 61 6b 65 73 20 4e 4f 52  l also makes NOR
34164 4d 41 4c 20 74 68 65 20 64 65 66 61 75 6c 74 2e  MAL the default.
34165 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  .  */.#ifdef SQL
34166 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43 4b  ITE_DEFAULT_LOCK
34167 49 4e 47 5f 4d 4f 44 45 0a 20 20 64 62 2d 3e 64  ING_MODE.  db->d
34168 66 6c 74 4c 6f 63 6b 4d 6f 64 65 20 3d 20 53 51  fltLockMode = SQ
34169 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4c 4f 43  LITE_DEFAULT_LOC
3416a 4b 49 4e 47 5f 4d 4f 44 45 3b 0a 20 20 73 71 6c  KING_MODE;.  sql
3416b 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67  ite3PagerLocking
3416c 4d 6f 64 65 28 73 71 6c 69 74 65 33 42 74 72 65  Mode(sqlite3Btre
3416d 65 50 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 30  ePager(db->aDb[0
3416e 5d 2e 70 42 74 29 2c 0a 20 20 20 20 20 20 20 20  ].pBt),.        
3416f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34170 20 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54    SQLITE_DEFAULT
34171 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 29 3b 0a  _LOCKING_MODE);.
34172 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 45 6e 61  #endif..  /* Ena
34173 62 6c 65 20 74 68 65 20 6c 6f 6f 6b 61 73 69 64  ble the lookasid
34174 65 2d 6d 61 6c 6c 6f 63 20 73 75 62 73 79 73 74  e-malloc subsyst
34175 65 6d 20 2a 2f 0a 20 20 73 65 74 75 70 4c 6f 6f  em */.  setupLoo
34176 6b 61 73 69 64 65 28 64 62 2c 20 30 2c 20 73 71  kaside(db, 0, sq
34177 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
34178 67 2e 73 7a 4c 6f 6f 6b 61 73 69 64 65 2c 0a 20  g.szLookaside,. 
34179 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3417a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c         sqlite3Gl
3417b 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 4c 6f 6f 6b  obalConfig.nLook
3417c 61 73 69 64 65 29 3b 0a 0a 6f 70 65 6e 64 62 5f  aside);..opendb_
3417d 6f 75 74 3a 0a 20 20 69 66 28 20 64 62 20 29 7b  out:.  if( db ){
3417e 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d  .    assert( db-
3417f 3e 6d 75 74 65 78 21 3d 30 20 7c 7c 20 69 73 54  >mutex!=0 || isT
34180 68 72 65 61 64 73 61 66 65 3d 3d 30 20 7c 7c 20  hreadsafe==0 || 
34181 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
34182 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78 3d 3d  fig.bFullMutex==
34183 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 );.    sqlite3
34184 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
34185 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72  >mutex);.  }.  r
34186 63 20 3d 20 73 71 6c 69 74 65 33 5f 65 72 72 63  c = sqlite3_errc
34187 6f 64 65 28 64 62 29 3b 0a 20 20 69 66 28 20 72  ode(db);.  if( r
34188 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
34189 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 63  ){.    sqlite3_c
3418a 6c 6f 73 65 28 64 62 29 3b 0a 20 20 20 20 64 62  lose(db);.    db
3418b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66   = 0;.  }else if
3418c 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
3418d 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63  ){.    db->magic
3418e 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f   = SQLITE_MAGIC_
3418f 53 49 43 4b 3b 0a 20 20 7d 0a 20 20 2a 70 70 44  SICK;.  }.  *ppD
34190 62 20 3d 20 64 62 3b 0a 20 20 72 65 74 75 72 6e  b = db;.  return
34191 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
34192 30 2c 20 72 63 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  0, rc);.}../*.**
34193 20 4f 70 65 6e 20 61 20 6e 65 77 20 64 61 74 61   Open a new data
34194 62 61 73 65 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a  base handle..*/.
34195 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
34196 71 6c 69 74 65 33 5f 6f 70 65 6e 28 0a 20 20 63  qlite3_open(.  c
34197 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
34198 6e 61 6d 65 2c 20 0a 20 20 73 71 6c 69 74 65 33  name, .  sqlite3
34199 20 2a 2a 70 70 44 62 20 0a 29 7b 0a 20 20 72 65   **ppDb .){.  re
3419a 74 75 72 6e 20 6f 70 65 6e 44 61 74 61 62 61 73  turn openDatabas
3419b 65 28 7a 46 69 6c 65 6e 61 6d 65 2c 20 70 70 44  e(zFilename, ppD
3419c 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  b,.             
3419d 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
3419e 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 7c  OPEN_READWRITE |
3419f 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45   SQLITE_OPEN_CRE
341a0 41 54 45 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54  ATE, 0);.}.SQLIT
341a1 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
341a2 33 5f 6f 70 65 6e 5f 76 32 28 0a 20 20 63 6f 6e  3_open_v2(.  con
341a3 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e 61 6d  st char *filenam
341a4 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65  e,   /* Database
341a5 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d 38   filename (UTF-8
341a6 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  ) */.  sqlite3 *
341a7 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20 20 2f  *ppDb,         /
341a8 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62  * OUT: SQLite db
341a9 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69 6e 74   handle */.  int
341aa 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
341ab 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 2a 2f       /* Flags */
341ac 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
341ad 56 66 73 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  Vfs        /* Na
341ae 6d 65 20 6f 66 20 56 46 53 20 6d 6f 64 75 6c 65  me of VFS module
341af 20 74 6f 20 75 73 65 20 2a 2f 0a 29 7b 0a 20 20   to use */.){.  
341b0 72 65 74 75 72 6e 20 6f 70 65 6e 44 61 74 61 62  return openDatab
341b1 61 73 65 28 66 69 6c 65 6e 61 6d 65 2c 20 70 70  ase(filename, pp
341b2 44 62 2c 20 66 6c 61 67 73 2c 20 7a 56 66 73 29  Db, flags, zVfs)
341b3 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
341b4 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f  ITE_OMIT_UTF16./
341b5 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 77 20  *.** Open a new 
341b6 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 2e  database handle.
341b7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
341b8 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  nt sqlite3_open1
341b9 36 28 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  6(.  const void 
341ba 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 0a 20 20 73  *zFilename, .  s
341bb 71 6c 69 74 65 33 20 2a 2a 70 70 44 62 0a 29 7b  qlite3 **ppDb.){
341bc 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a  .  char const *z
341bd 46 69 6c 65 6e 61 6d 65 38 3b 20 20 20 2f 2a 20  Filename8;   /* 
341be 7a 46 69 6c 65 6e 61 6d 65 20 65 6e 63 6f 64 65  zFilename encode
341bf 64 20 69 6e 20 55 54 46 2d 38 20 69 6e 73 74 65  d in UTF-8 inste
341c0 61 64 20 6f 66 20 55 54 46 2d 31 36 20 2a 2f 0a  ad of UTF-16 */.
341c1 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
341c2 2a 70 56 61 6c 3b 0a 20 20 69 6e 74 20 72 63 3b  *pVal;.  int rc;
341c3 0a 0a 20 20 61 73 73 65 72 74 28 20 7a 46 69 6c  ..  assert( zFil
341c4 65 6e 61 6d 65 20 29 3b 0a 20 20 61 73 73 65 72  ename );.  asser
341c5 74 28 20 70 70 44 62 20 29 3b 0a 20 20 2a 70 70  t( ppDb );.  *pp
341c6 44 62 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20  Db = 0;.#ifndef 
341c7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
341c8 49 4e 49 54 0a 20 20 72 63 20 3d 20 73 71 6c 69  INIT.  rc = sqli
341c9 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
341ca 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74  ;.  if( rc ) ret
341cb 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69 66 0a 20  urn rc;.#endif. 
341cc 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56   pVal = sqlite3V
341cd 61 6c 75 65 4e 65 77 28 30 29 3b 0a 20 20 73 71  alueNew(0);.  sq
341ce 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72  lite3ValueSetStr
341cf 28 70 56 61 6c 2c 20 2d 31 2c 20 7a 46 69 6c 65  (pVal, -1, zFile
341d0 6e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 55 54 46  name, SQLITE_UTF
341d1 31 36 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45  16NATIVE, SQLITE
341d2 5f 53 54 41 54 49 43 29 3b 0a 20 20 7a 46 69 6c  _STATIC);.  zFil
341d3 65 6e 61 6d 65 38 20 3d 20 73 71 6c 69 74 65 33  ename8 = sqlite3
341d4 56 61 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20  ValueText(pVal, 
341d5 53 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20  SQLITE_UTF8);.  
341d6 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 38 20 29  if( zFilename8 )
341d7 7b 0a 20 20 20 20 72 63 20 3d 20 6f 70 65 6e 44  {.    rc = openD
341d8 61 74 61 62 61 73 65 28 7a 46 69 6c 65 6e 61 6d  atabase(zFilenam
341d9 65 38 2c 20 70 70 44 62 2c 0a 20 20 20 20 20 20  e8, ppDb,.      
341da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
341db 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
341dc 57 52 49 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f  WRITE | SQLITE_O
341dd 50 45 4e 5f 43 52 45 41 54 45 2c 20 30 29 3b 0a  PEN_CREATE, 0);.
341de 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 70 44      assert( *ppD
341df 62 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f  b || rc==SQLITE_
341e0 4e 4f 4d 45 4d 20 29 3b 0a 20 20 20 20 69 66 28  NOMEM );.    if(
341e1 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
341e2 26 20 21 44 62 48 61 73 50 72 6f 70 65 72 74 79  & !DbHasProperty
341e3 28 2a 70 70 44 62 2c 20 30 2c 20 44 42 5f 53 63  (*ppDb, 0, DB_Sc
341e4 68 65 6d 61 4c 6f 61 64 65 64 29 20 29 7b 0a 20  hemaLoaded) ){. 
341e5 20 20 20 20 20 45 4e 43 28 2a 70 70 44 62 29 20       ENC(*ppDb) 
341e6 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  = SQLITE_UTF16NA
341e7 54 49 56 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  TIVE;.    }.  }e
341e8 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  lse{.    rc = SQ
341e9 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
341ea 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72    sqlite3ValueFr
341eb 65 65 28 70 56 61 6c 29 3b 0a 0a 20 20 72 65 74  ee(pVal);..  ret
341ec 75 72 6e 20 73 71 6c 69 74 65 33 41 70 69 45 78  urn sqlite3ApiEx
341ed 69 74 28 30 2c 20 72 63 29 3b 0a 7d 0a 23 65 6e  it(0, rc);.}.#en
341ee 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
341ef 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a  IT_UTF16 */../*.
341f0 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 6e 65  ** Register a ne
341f1 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  w collation sequ
341f2 65 6e 63 65 20 77 69 74 68 20 74 68 65 20 64 61  ence with the da
341f3 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64 62  tabase handle db
341f4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
341f5 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
341f6 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20  te_collation(.  
341f7 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 0a 20 20  sqlite3* db, .  
341f8 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
341f9 65 2c 20 0a 20 20 69 6e 74 20 65 6e 63 2c 20 0a  e, .  int enc, .
341fa 20 20 76 6f 69 64 2a 20 70 43 74 78 2c 0a 20 20    void* pCtx,.  
341fb 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76  int(*xCompare)(v
341fc 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
341fd 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
341fe 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20 72  oid*).){.  int r
341ff 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  c;.  sqlite3_mut
34200 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
34201 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  ex);.  assert( !
34202 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
34203 20 29 3b 0a 20 20 72 63 20 3d 20 63 72 65 61 74   );.  rc = creat
34204 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20 7a  eCollation(db, z
34205 4e 61 6d 65 2c 20 28 75 38 29 65 6e 63 2c 20 53  Name, (u8)enc, S
34206 51 4c 49 54 45 5f 43 4f 4c 4c 5f 55 53 45 52 2c  QLITE_COLL_USER,
34207 20 70 43 74 78 2c 20 78 43 6f 6d 70 61 72 65 2c   pCtx, xCompare,
34208 20 30 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69   0);.  rc = sqli
34209 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
3420a 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  c);.  sqlite3_mu
3420b 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
3420c 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
3420d 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69  c;.}../*.** Regi
3420e 73 74 65 72 20 61 20 6e 65 77 20 63 6f 6c 6c 61  ster a new colla
3420f 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69  tion sequence wi
34210 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20  th the database 
34211 68 61 6e 64 6c 65 20 64 62 2e 0a 2a 2f 0a 53 51  handle db..*/.SQ
34212 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
34213 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c  ite3_create_coll
34214 61 74 69 6f 6e 5f 76 32 28 0a 20 20 73 71 6c 69  ation_v2(.  sqli
34215 74 65 33 2a 20 64 62 2c 20 0a 20 20 63 6f 6e 73  te3* db, .  cons
34216 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a  t char *zName, .
34217 20 20 69 6e 74 20 65 6e 63 2c 20 0a 20 20 76 6f    int enc, .  vo
34218 69 64 2a 20 70 43 74 78 2c 0a 20 20 69 6e 74 28  id* pCtx,.  int(
34219 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a  *xCompare)(void*
3421a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
3421b 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
3421c 29 2c 0a 20 20 76 6f 69 64 28 2a 78 44 65 6c 29  ),.  void(*xDel)
3421d 28 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74  (void*).){.  int
3421e 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d   rc;.  sqlite3_m
3421f 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
34220 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28  utex);.  assert(
34221 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
34222 65 64 20 29 3b 0a 20 20 72 63 20 3d 20 63 72 65  ed );.  rc = cre
34223 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c  ateCollation(db,
34224 20 7a 4e 61 6d 65 2c 20 28 75 38 29 65 6e 63 2c   zName, (u8)enc,
34225 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 55 53 45   SQLITE_COLL_USE
34226 52 2c 20 70 43 74 78 2c 20 78 43 6f 6d 70 61 72  R, pCtx, xCompar
34227 65 2c 20 78 44 65 6c 29 3b 0a 20 20 72 63 20 3d  e, xDel);.  rc =
34228 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74 28   sqlite3ApiExit(
34229 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74  db, rc);.  sqlit
3422a 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
3422b 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
3422c 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64  urn rc;.}..#ifnd
3422d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  ef SQLITE_OMIT_U
3422e 54 46 31 36 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73  TF16./*.** Regis
3422f 74 65 72 20 61 20 6e 65 77 20 63 6f 6c 6c 61 74  ter a new collat
34230 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 74  ion sequence wit
34231 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  h the database h
34232 61 6e 64 6c 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c  andle db..*/.SQL
34233 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
34234 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
34235 74 69 6f 6e 31 36 28 0a 20 20 73 71 6c 69 74 65  tion16(.  sqlite
34236 33 2a 20 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20  3* db, .  const 
34237 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a 20 20 69  void *zName,.  i
34238 6e 74 20 65 6e 63 2c 20 0a 20 20 76 6f 69 64 2a  nt enc, .  void*
34239 20 70 43 74 78 2c 0a 20 20 69 6e 74 28 2a 78 43   pCtx,.  int(*xC
3423a 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c 69 6e  ompare)(void*,in
3423b 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  t,const void*,in
3423c 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 0a 29  t,const void*).)
3423d 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
3423e 49 54 45 5f 4f 4b 3b 0a 20 20 63 68 61 72 20 2a  ITE_OK;.  char *
3423f 7a 4e 61 6d 65 38 3b 0a 20 20 73 71 6c 69 74 65  zName8;.  sqlite
34240 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
34241 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65  ->mutex);.  asse
34242 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rt( !db->mallocF
34243 61 69 6c 65 64 20 29 3b 0a 20 20 7a 4e 61 6d 65  ailed );.  zName
34244 38 20 3d 20 73 71 6c 69 74 65 33 55 74 66 31 36  8 = sqlite3Utf16
34245 74 6f 38 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 2d  to8(db, zName, -
34246 31 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65 38  1);.  if( zName8
34247 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 63 72 65   ){.    rc = cre
34248 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c  ateCollation(db,
34249 20 7a 4e 61 6d 65 38 2c 20 28 75 38 29 65 6e 63   zName8, (u8)enc
3424a 2c 20 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 55 53  , SQLITE_COLL_US
3424b 45 52 2c 20 70 43 74 78 2c 20 78 43 6f 6d 70 61  ER, pCtx, xCompa
3424c 72 65 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69  re, 0);.    sqli
3424d 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e  te3DbFree(db, zN
3424e 61 6d 65 38 29 3b 0a 20 20 7d 0a 20 20 72 63 20  ame8);.  }.  rc 
3424f 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
34250 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69  (db, rc);.  sqli
34251 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
34252 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
34253 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69  turn rc;.}.#endi
34254 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
34255 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  _UTF16 */../*.**
34256 20 52 65 67 69 73 74 65 72 20 61 20 63 6f 6c 6c   Register a coll
34257 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66  ation sequence f
34258 61 63 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b 20  actory callback 
34259 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73  with the databas
3425a 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20  e handle.** db. 
3425b 52 65 70 6c 61 63 65 20 61 6e 79 20 70 72 65 76  Replace any prev
3425c 69 6f 75 73 6c 79 20 69 6e 73 74 61 6c 6c 65 64  iously installed
3425d 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
3425e 6e 63 65 20 66 61 63 74 6f 72 79 2e 0a 2a 2f 0a  nce factory..*/.
3425f 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
34260 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e  qlite3_collation
34261 5f 6e 65 65 64 65 64 28 0a 20 20 73 71 6c 69 74  _needed(.  sqlit
34262 65 33 20 2a 64 62 2c 20 0a 20 20 76 6f 69 64 20  e3 *db, .  void 
34263 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 2c  *pCollNeededArg,
34264 20 0a 20 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e   .  void(*xCollN
34265 65 65 64 65 64 29 28 76 6f 69 64 2a 2c 73 71 6c  eeded)(void*,sql
34266 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52  ite3*,int eTextR
34267 65 70 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 0a  ep,const char*).
34268 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ){.  sqlite3_mut
34269 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
3426a 65 78 29 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c  ex);.  db->xColl
3426b 4e 65 65 64 65 64 20 3d 20 78 43 6f 6c 6c 4e 65  Needed = xCollNe
3426c 65 64 65 64 3b 0a 20 20 64 62 2d 3e 78 43 6f 6c  eded;.  db->xCol
3426d 6c 4e 65 65 64 65 64 31 36 20 3d 20 30 3b 0a 20  lNeeded16 = 0;. 
3426e 20 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65 64 65 64   db->pCollNeeded
3426f 41 72 67 20 3d 20 70 43 6f 6c 6c 4e 65 65 64 65  Arg = pCollNeede
34270 64 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f  dArg;.  sqlite3_
34271 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
34272 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
34273 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23   SQLITE_OK;.}..#
34274 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
34275 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 52  IT_UTF16./*.** R
34276 65 67 69 73 74 65 72 20 61 20 63 6f 6c 6c 61 74  egister a collat
34277 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 66 61 63  ion sequence fac
34278 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b 20 77 69  tory callback wi
34279 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20  th the database 
3427a 68 61 6e 64 6c 65 0a 2a 2a 20 64 62 2e 20 52 65  handle.** db. Re
3427b 70 6c 61 63 65 20 61 6e 79 20 70 72 65 76 69 6f  place any previo
3427c 75 73 6c 79 20 69 6e 73 74 61 6c 6c 65 64 20 63  usly installed c
3427d 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
3427e 65 20 66 61 63 74 6f 72 79 2e 0a 2a 2f 0a 53 51  e factory..*/.SQ
3427f 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
34280 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
34281 65 65 64 65 64 31 36 28 0a 20 20 73 71 6c 69 74  eeded16(.  sqlit
34282 65 33 20 2a 64 62 2c 20 0a 20 20 76 6f 69 64 20  e3 *db, .  void 
34283 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 2c  *pCollNeededArg,
34284 20 0a 20 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e   .  void(*xCollN
34285 65 65 64 65 64 31 36 29 28 76 6f 69 64 2a 2c 73  eeded16)(void*,s
34286 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65 78  qlite3*,int eTex
34287 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  tRep,const void*
34288 29 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ).){.  sqlite3_m
34289 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
3428a 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 78 43 6f  utex);.  db->xCo
3428b 6c 6c 4e 65 65 64 65 64 20 3d 20 30 3b 0a 20 20  llNeeded = 0;.  
3428c 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 31  db->xCollNeeded1
3428d 36 20 3d 20 78 43 6f 6c 6c 4e 65 65 64 65 64 31  6 = xCollNeeded1
3428e 36 3b 0a 20 20 64 62 2d 3e 70 43 6f 6c 6c 4e 65  6;.  db->pCollNe
3428f 65 64 65 64 41 72 67 20 3d 20 70 43 6f 6c 6c 4e  ededArg = pCollN
34290 65 65 64 65 64 41 72 67 3b 0a 20 20 73 71 6c 69  eededArg;.  sqli
34291 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
34292 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
34293 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
34294 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
34295 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
34296 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
34297 5f 4f 4d 49 54 5f 47 4c 4f 42 41 4c 52 45 43 4f  _OMIT_GLOBALRECO
34298 56 45 52 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  VER.#ifndef SQLI
34299 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54  TE_OMIT_DEPRECAT
3429a 45 44 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  ED./*.** This fu
3429b 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 77 20 61 6e  nction is now an
3429c 20 61 6e 61 63 68 72 6f 6e 69 73 6d 2e 20 49 74   anachronism. It
3429d 20 75 73 65 64 20 74 6f 20 62 65 20 75 73 65 64   used to be used
3429e 20 74 6f 20 72 65 63 6f 76 65 72 20 66 72 6f 6d   to recover from
3429f 20 61 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 66   a.** malloc() f
342a0 61 69 6c 75 72 65 2c 20 62 75 74 20 53 51 4c 69  ailure, but SQLi
342a1 74 65 20 6e 6f 77 20 64 6f 65 73 20 74 68 69 73  te now does this
342a2 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 0a   automatically..
342a3 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
342a4 74 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62 61 6c  t sqlite3_global
342a5 5f 72 65 63 6f 76 65 72 28 76 6f 69 64 29 7b 0a  _recover(void){.
342a6 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
342a7 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 23 65 6e  OK;.}.#endif.#en
342a8 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74 20  dif../*.** Test 
342a9 74 6f 20 73 65 65 20 77 68 65 74 68 65 72 20 6f  to see whether o
342aa 72 20 6e 6f 74 20 74 68 65 20 64 61 74 61 62 61  r not the databa
342ab 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  se connection is
342ac 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69 74 0a 2a   in autocommit.*
342ad 2a 20 6d 6f 64 65 2e 20 20 52 65 74 75 72 6e 20  * mode.  Return 
342ae 54 52 55 45 20 69 66 20 69 74 20 69 73 20 61 6e  TRUE if it is an
342af 64 20 46 41 4c 53 45 20 69 66 20 6e 6f 74 2e 20  d FALSE if not. 
342b0 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65   Autocommit mode
342b1 20 69 73 20 6f 6e 0a 2a 2a 20 62 79 20 64 65 66   is on.** by def
342b2 61 75 6c 74 2e 20 20 41 75 74 6f 63 6f 6d 6d 69  ault.  Autocommi
342b3 74 20 69 73 20 64 69 73 61 62 6c 65 64 20 62 79  t is disabled by
342b4 20 61 20 42 45 47 49 4e 20 73 74 61 74 65 6d 65   a BEGIN stateme
342b5 6e 74 20 61 6e 64 20 72 65 65 6e 61 62 6c 65 64  nt and reenabled
342b6 0a 2a 2a 20 62 79 20 74 68 65 20 6e 65 78 74 20  .** by the next 
342b7 43 4f 4d 4d 49 54 20 6f 72 20 52 4f 4c 4c 42 41  COMMIT or ROLLBA
342b8 43 4b 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 20 54  CK..**.******* T
342b9 48 49 53 20 49 53 20 41 4e 20 45 58 50 45 52 49  HIS IS AN EXPERI
342ba 4d 45 4e 54 41 4c 20 41 50 49 20 41 4e 44 20 49  MENTAL API AND I
342bb 53 20 53 55 42 4a 45 43 54 20 54 4f 20 43 48 41  S SUBJECT TO CHA
342bc 4e 47 45 20 2a 2a 2a 2a 2a 2a 0a 2a 2f 0a 53 51  NGE ******.*/.SQ
342bd 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
342be 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d  ite3_get_autocom
342bf 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29  mit(sqlite3 *db)
342c0 7b 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 61  {.  return db->a
342c1 75 74 6f 43 6f 6d 6d 69 74 3b 0a 7d 0a 0a 23 69  utoCommit;.}..#i
342c2 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
342c3 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  G./*.** The foll
342c4 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 69 73  owing routine is
342c5 20 73 75 62 74 69 74 75 74 65 64 20 66 6f 72 20   subtituted for 
342c6 63 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f  constant SQLITE_
342c7 43 4f 52 52 55 50 54 20 69 6e 0a 2a 2a 20 64 65  CORRUPT in.** de
342c8 62 75 67 67 69 6e 67 20 62 75 69 6c 64 73 2e 20  bugging builds. 
342c9 20 54 68 69 73 20 70 72 6f 76 69 64 65 73 20 61   This provides a
342ca 20 77 61 79 20 74 6f 20 73 65 74 20 61 20 62 72   way to set a br
342cb 65 61 6b 70 6f 69 6e 74 20 66 6f 72 20 77 68 65  eakpoint for whe
342cc 6e 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e 20  n.** corruption 
342cd 69 73 20 66 69 72 73 74 20 64 65 74 65 63 74 65  is first detecte
342ce 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
342cf 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
342d0 43 6f 72 72 75 70 74 28 76 6f 69 64 29 7b 0a 20  Corrupt(void){. 
342d1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
342d2 4f 52 52 55 50 54 3b 0a 7d 0a 23 65 6e 64 69 66  ORRUPT;.}.#endif
342d3 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
342d4 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
342d5 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 61  ./*.** This is a
342d6 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 72 6f 75   convenience rou
342d7 74 69 6e 65 20 74 68 61 74 20 6d 61 6b 65 73 20  tine that makes 
342d8 73 75 72 65 20 74 68 61 74 20 61 6c 6c 20 74 68  sure that all th
342d9 72 65 61 64 2d 73 70 65 63 69 66 69 63 0a 2a 2a  read-specific.**
342da 20 64 61 74 61 20 66 6f 72 20 74 68 69 73 20 74   data for this t
342db 68 72 65 61 64 20 68 61 73 20 62 65 65 6e 20 64  hread has been d
342dc 65 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a 2a 0a 2a  eallocated..**.*
342dd 2a 20 53 51 4c 69 74 65 20 6e 6f 20 6c 6f 6e 67  * SQLite no long
342de 65 72 20 75 73 65 73 20 74 68 72 65 61 64 2d 73  er uses thread-s
342df 70 65 63 69 66 69 63 20 64 61 74 61 20 73 6f 20  pecific data so 
342e0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
342e1 6e 6f 77 20 61 0a 2a 2a 20 6e 6f 2d 6f 70 2e 20  now a.** no-op. 
342e2 20 49 74 20 69 73 20 72 65 74 61 69 6e 65 64 20   It is retained 
342e3 66 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 20 63  for historical c
342e4 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2f  ompatibility..*/
342e5 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
342e6 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64 5f   sqlite3_thread_
342e7 63 6c 65 61 6e 75 70 28 76 6f 69 64 29 7b 0a 7d  cleanup(void){.}
342e8 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
342e9 65 74 75 72 6e 20 6d 65 74 61 20 69 6e 66 6f 72  eturn meta infor
342ea 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 20 73  mation about a s
342eb 70 65 63 69 66 69 63 20 63 6f 6c 75 6d 6e 20 6f  pecific column o
342ec 66 20 61 20 64 61 74 61 62 61 73 65 20 74 61 62  f a database tab
342ed 6c 65 2e 0a 2a 2a 20 53 65 65 20 63 6f 6d 6d 65  le..** See comme
342ee 6e 74 20 69 6e 20 73 71 6c 69 74 65 33 2e 68 20  nt in sqlite3.h 
342ef 28 73 71 6c 69 74 65 2e 68 2e 69 6e 29 20 66 6f  (sqlite.h.in) fo
342f0 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23 69  r details..*/.#i
342f1 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
342f2 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41  LE_COLUMN_METADA
342f3 54 41 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  TA.SQLITE_API in
342f4 74 20 73 71 6c 69 74 65 33 5f 74 61 62 6c 65 5f  t sqlite3_table_
342f5 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 28  column_metadata(
342f6 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
342f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
342f8 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 68 61 6e  * Connection han
342f9 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  dle */.  const c
342fa 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 20 20  har *zDbName,   
342fb 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
342fc 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f   name or NULL */
342fd 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
342fe 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20 20 2f  TableName,     /
342ff 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a  * Table name */.
34300 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
34301 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20 20 2f 2a  olumnName,    /*
34302 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20 2a 2f 0a   Column name */.
34303 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70    char const **p
34304 7a 44 61 74 61 54 79 70 65 2c 20 20 20 20 2f 2a  zDataType,    /*
34305 20 4f 55 54 50 55 54 3a 20 44 65 63 6c 61 72 65   OUTPUT: Declare
34306 64 20 64 61 74 61 20 74 79 70 65 20 2a 2f 0a 20  d data type */. 
34307 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 2a 70 7a   char const **pz
34308 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20 2f 2a 20  CollSeq,     /* 
34309 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61 74 69 6f  OUTPUT: Collatio
3430a 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 20  n sequence name 
3430b 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f 74 4e 75  */.  int *pNotNu
3430c 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ll,             
3430d 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65   /* OUTPUT: True
3430e 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6e   if NOT NULL con
3430f 73 74 72 61 69 6e 74 20 65 78 69 73 74 73 20 2a  straint exists *
34310 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69 6d 61 72  /.  int *pPrimar
34311 79 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20  yKey,           
34312 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65 20  /* OUTPUT: True 
34313 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72 74 20 6f  if column part o
34314 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  f PK */.  int *p
34315 41 75 74 6f 69 6e 63 20 20 20 20 20 20 20 20 20  Autoinc         
34316 20 20 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a        /* OUTPUT:
34317 20 54 72 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20   True if column 
34318 69 73 20 61 75 74 6f 2d 69 6e 63 72 65 6d 65 6e  is auto-incremen
34319 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63  t */.){.  int rc
3431a 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 4d 73  ;.  char *zErrMs
3431b 67 20 3d 20 30 3b 0a 20 20 54 61 62 6c 65 20 2a  g = 0;.  Table *
3431c 70 54 61 62 20 3d 20 30 3b 0a 20 20 43 6f 6c 75  pTab = 0;.  Colu
3431d 6d 6e 20 2a 70 43 6f 6c 20 3d 20 30 3b 0a 20 20  mn *pCol = 0;.  
3431e 69 6e 74 20 69 43 6f 6c 3b 0a 0a 20 20 63 68 61  int iCol;..  cha
3431f 72 20 63 6f 6e 73 74 20 2a 7a 44 61 74 61 54 79  r const *zDataTy
34320 70 65 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63  pe = 0;.  char c
34321 6f 6e 73 74 20 2a 7a 43 6f 6c 6c 53 65 71 20 3d  onst *zCollSeq =
34322 20 30 3b 0a 20 20 69 6e 74 20 6e 6f 74 6e 75 6c   0;.  int notnul
34323 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20 70 72 69  l = 0;.  int pri
34324 6d 61 72 79 6b 65 79 20 3d 20 30 3b 0a 20 20 69  marykey = 0;.  i
34325 6e 74 20 61 75 74 6f 69 6e 63 20 3d 20 30 3b 0a  nt autoinc = 0;.
34326 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20 74 68 65  .  /* Ensure the
34327 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
34328 20 68 61 73 20 62 65 65 6e 20 6c 6f 61 64 65 64   has been loaded
34329 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   */.  sqlite3_mu
3432a 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
3432b 74 65 78 29 3b 0a 20 20 28 76 6f 69 64 29 73 71  tex);.  (void)sq
3432c 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
3432d 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
3432e 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b 0a 20  eEnterAll(db);. 
3432f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 49 6e 69   rc = sqlite3Ini
34330 74 28 64 62 2c 20 26 7a 45 72 72 4d 73 67 29 3b  t(db, &zErrMsg);
34331 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
34332 21 3d 72 63 20 29 7b 0a 20 20 20 20 67 6f 74 6f  !=rc ){.    goto
34333 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a   error_out;.  }.
34334 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65  .  /* Locate the
34335 20 74 61 62 6c 65 20 69 6e 20 71 75 65 73 74 69   table in questi
34336 6f 6e 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73  on */.  pTab = s
34337 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
34338 64 62 2c 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20  db, zTableName, 
34339 7a 44 62 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20  zDbName);.  if( 
3433a 21 70 54 61 62 20 7c 7c 20 70 54 61 62 2d 3e 70  !pTab || pTab->p
3433b 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 70 54  Select ){.    pT
3433c 61 62 20 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f  ab = 0;.    goto
3433d 20 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a   error_out;.  }.
3433e 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 63  .  /* Find the c
3433f 6f 6c 75 6d 6e 20 66 6f 72 20 77 68 69 63 68 20  olumn for which 
34340 69 6e 66 6f 20 69 73 20 72 65 71 75 65 73 74 65  info is requeste
34341 64 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74  d */.  if( sqlit
34342 65 33 49 73 52 6f 77 69 64 28 7a 43 6f 6c 75 6d  e3IsRowid(zColum
34343 6e 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 69 43  nName) ){.    iC
34344 6f 6c 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79  ol = pTab->iPKey
34345 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d  ;.    if( iCol>=
34346 30 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 20  0 ){.      pCol 
34347 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43  = &pTab->aCol[iC
34348 6f 6c 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  ol];.    }.  }el
34349 73 65 7b 0a 20 20 20 20 66 6f 72 28 69 43 6f 6c  se{.    for(iCol
3434a 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e  =0; iCol<pTab->n
3434b 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20  Col; iCol++){.  
3434c 20 20 20 20 70 43 6f 6c 20 3d 20 26 70 54 61 62      pCol = &pTab
3434d 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20  ->aCol[iCol];.  
3434e 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74      if( 0==sqlit
3434f 65 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e  e3StrICmp(pCol->
34350 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 4e 61  zName, zColumnNa
34351 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62  me) ){.        b
34352 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
34353 20 20 7d 0a 20 20 20 20 69 66 28 20 69 43 6f 6c    }.    if( iCol
34354 3d 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a  ==pTab->nCol ){.
34355 20 20 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0a        pTab = 0;.
34356 20 20 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72        goto error
34357 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  _out;.    }.  }.
34358 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  /* The follow
34359 69 6e 67 20 62 6c 6f 63 6b 20 73 74 6f 72 65 73  ing block stores
3435a 20 74 68 65 20 6d 65 74 61 20 69 6e 66 6f 72 6d   the meta inform
3435b 61 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20  ation that will 
3435c 62 65 20 72 65 74 75 72 6e 65 64 0a 20 20 2a 2a  be returned.  **
3435d 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 69   to the caller i
3435e 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65  n local variable
3435f 73 20 7a 44 61 74 61 54 79 70 65 2c 20 7a 43 6f  s zDataType, zCo
34360 6c 6c 53 65 71 2c 20 6e 6f 74 6e 75 6c 6c 2c 20  llSeq, notnull, 
34361 70 72 69 6d 61 72 79 6b 65 79 0a 20 20 2a 2a 20  primarykey.  ** 
34362 61 6e 64 20 61 75 74 6f 69 6e 63 2e 20 41 74 20  and autoinc. At 
34363 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65 72 65  this point there
34364 20 61 72 65 20 74 77 6f 20 70 6f 73 73 69 62 69   are two possibi
34365 6c 69 74 69 65 73 3a 0a 20 20 2a 2a 20 0a 20 20  lities:.  ** .  
34366 2a 2a 20 20 20 20 20 31 2e 20 54 68 65 20 73 70  **     1. The sp
34367 65 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20 6e  ecified column n
34368 61 6d 65 20 77 61 73 20 72 6f 77 69 64 22 2c 20  ame was rowid", 
34369 22 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77 69 64  "oid" or "_rowid
3436a 5f 22 20 0a 20 20 2a 2a 20 20 20 20 20 20 20 20  _" .  **        
3436b 61 6e 64 20 74 68 65 72 65 20 69 73 20 6e 6f 20  and there is no 
3436c 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61  explicitly decla
3436d 72 65 64 20 49 50 4b 20 63 6f 6c 75 6d 6e 2e 20  red IPK column. 
3436e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 32  .  **.  **     2
3436f 2e 20 54 68 65 20 74 61 62 6c 65 20 69 73 20 6e  . The table is n
34370 6f 74 20 61 20 76 69 65 77 20 61 6e 64 20 74 68  ot a view and th
34371 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 69 64  e column name id
34372 65 6e 74 69 66 69 65 64 20 61 6e 20 0a 20 20 2a  entified an .  *
34373 2a 20 20 20 20 20 20 20 20 65 78 70 6c 69 63 69  *        explici
34374 74 6c 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c  tly declared col
34375 75 6d 6e 2e 20 43 6f 70 79 20 6d 65 74 61 20 69  umn. Copy meta i
34376 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20  nformation from 
34377 2a 70 43 6f 6c 2e 0a 20 20 2a 2f 20 0a 20 20 69  *pCol..  */ .  i
34378 66 28 20 70 43 6f 6c 20 29 7b 0a 20 20 20 20 7a  f( pCol ){.    z
34379 44 61 74 61 54 79 70 65 20 3d 20 70 43 6f 6c 2d  DataType = pCol-
3437a 3e 7a 54 79 70 65 3b 0a 20 20 20 20 7a 43 6f 6c  >zType;.    zCol
3437b 6c 53 65 71 20 3d 20 70 43 6f 6c 2d 3e 7a 43 6f  lSeq = pCol->zCo
3437c 6c 6c 3b 0a 20 20 20 20 6e 6f 74 6e 75 6c 6c 20  ll;.    notnull 
3437d 3d 20 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 21  = pCol->notNull!
3437e 3d 30 3b 0a 20 20 20 20 70 72 69 6d 61 72 79 6b  =0;.    primaryk
3437f 65 79 20 20 3d 20 70 43 6f 6c 2d 3e 69 73 50 72  ey  = pCol->isPr
34380 69 6d 4b 65 79 21 3d 30 3b 0a 20 20 20 20 61 75  imKey!=0;.    au
34381 74 6f 69 6e 63 20 3d 20 70 54 61 62 2d 3e 69 50  toinc = pTab->iP
34382 4b 65 79 3d 3d 69 43 6f 6c 20 26 26 20 28 70 54  Key==iCol && (pT
34383 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54  ab->tabFlags & T
34384 46 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 29  F_Autoincrement)
34385 21 3d 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  !=0;.  }else{.  
34386 20 20 7a 44 61 74 61 54 79 70 65 20 3d 20 22 49    zDataType = "I
34387 4e 54 45 47 45 52 22 3b 0a 20 20 20 20 70 72 69  NTEGER";.    pri
34388 6d 61 72 79 6b 65 79 20 3d 20 31 3b 0a 20 20 7d  marykey = 1;.  }
34389 0a 20 20 69 66 28 20 21 7a 43 6f 6c 6c 53 65 71  .  if( !zCollSeq
3438a 20 29 7b 0a 20 20 20 20 7a 43 6f 6c 6c 53 65 71   ){.    zCollSeq
3438b 20 3d 20 22 42 49 4e 41 52 59 22 3b 0a 20 20 7d   = "BINARY";.  }
3438c 0a 0a 65 72 72 6f 72 5f 6f 75 74 3a 0a 20 20 73  ..error_out:.  s
3438d 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
3438e 41 6c 6c 28 64 62 29 3b 0a 20 20 28 76 6f 69 64  All(db);.  (void
3438f 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66  )sqlite3SafetyOf
34390 66 28 64 62 29 3b 0a 0a 20 20 2f 2a 20 57 68 65  f(db);..  /* Whe
34391 74 68 65 72 20 74 68 65 20 66 75 6e 63 74 69 6f  ther the functio
34392 6e 20 63 61 6c 6c 20 73 75 63 63 65 65 64 65 64  n call succeeded
34393 20 6f 72 20 66 61 69 6c 65 64 2c 20 73 65 74 20   or failed, set 
34394 74 68 65 20 6f 75 74 70 75 74 20 70 61 72 61 6d  the output param
34395 65 74 65 72 73 0a 20 20 2a 2a 20 74 6f 20 77 68  eters.  ** to wh
34396 61 74 65 76 65 72 20 74 68 65 69 72 20 6c 6f 63  atever their loc
34397 61 6c 20 63 6f 75 6e 74 65 72 70 61 72 74 73 20  al counterparts 
34398 63 6f 6e 74 61 69 6e 2e 20 49 66 20 61 6e 20 65  contain. If an e
34399 72 72 6f 72 20 64 69 64 20 6f 63 63 75 72 2c 0a  rror did occur,.
3439a 20 20 2a 2a 20 74 68 69 73 20 68 61 73 20 74 68    ** this has th
3439b 65 20 65 66 66 65 63 74 20 6f 66 20 7a 65 72 6f  e effect of zero
3439c 69 6e 67 20 61 6c 6c 20 6f 75 74 70 75 74 20 70  ing all output p
3439d 61 72 61 6d 65 74 65 72 73 2e 0a 20 20 2a 2f 0a  arameters..  */.
3439e 20 20 69 66 28 20 70 7a 44 61 74 61 54 79 70 65    if( pzDataType
3439f 20 29 20 2a 70 7a 44 61 74 61 54 79 70 65 20 3d   ) *pzDataType =
343a0 20 7a 44 61 74 61 54 79 70 65 3b 0a 20 20 69 66   zDataType;.  if
343a1 28 20 70 7a 43 6f 6c 6c 53 65 71 20 29 20 2a 70  ( pzCollSeq ) *p
343a2 7a 43 6f 6c 6c 53 65 71 20 3d 20 7a 43 6f 6c 6c  zCollSeq = zColl
343a3 53 65 71 3b 0a 20 20 69 66 28 20 70 4e 6f 74 4e  Seq;.  if( pNotN
343a4 75 6c 6c 20 29 20 2a 70 4e 6f 74 4e 75 6c 6c 20  ull ) *pNotNull 
343a5 3d 20 6e 6f 74 6e 75 6c 6c 3b 0a 20 20 69 66 28  = notnull;.  if(
343a6 20 70 50 72 69 6d 61 72 79 4b 65 79 20 29 20 2a   pPrimaryKey ) *
343a7 70 50 72 69 6d 61 72 79 4b 65 79 20 3d 20 70 72  pPrimaryKey = pr
343a8 69 6d 61 72 79 6b 65 79 3b 0a 20 20 69 66 28 20  imarykey;.  if( 
343a9 70 41 75 74 6f 69 6e 63 20 29 20 2a 70 41 75 74  pAutoinc ) *pAut
343aa 6f 69 6e 63 20 3d 20 61 75 74 6f 69 6e 63 3b 0a  oinc = autoinc;.
343ab 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
343ac 3d 3d 72 63 20 26 26 20 21 70 54 61 62 20 29 7b  ==rc && !pTab ){
343ad 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
343ae 65 65 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b  ee(db, zErrMsg);
343af 0a 20 20 20 20 7a 45 72 72 4d 73 67 20 3d 20 73  .    zErrMsg = s
343b0 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
343b1 2c 20 22 6e 6f 20 73 75 63 68 20 74 61 62 6c 65  , "no such table
343b2 20 63 6f 6c 75 6d 6e 3a 20 25 73 2e 25 73 22 2c   column: %s.%s",
343b3 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 0a 20 20 20   zTableName,.   
343b4 20 20 20 20 20 7a 43 6f 6c 75 6d 6e 4e 61 6d 65       zColumnName
343b5 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
343b6 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
343b7 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c  sqlite3Error(db,
343b8 20 72 63 2c 20 28 7a 45 72 72 4d 73 67 3f 22 25   rc, (zErrMsg?"%
343b9 73 22 3a 30 29 2c 20 7a 45 72 72 4d 73 67 29 3b  s":0), zErrMsg);
343ba 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
343bb 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20  (db, zErrMsg);. 
343bc 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69   rc = sqlite3Api
343bd 45 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20  Exit(db, rc);.  
343be 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
343bf 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
343c0 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23    return rc;.}.#
343c1 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65  endif../*.** Sle
343c2 65 70 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20  ep for a little 
343c3 77 68 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74  while.  Return t
343c4 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d  he amount of tim
343c5 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 53 51 4c 49  e slept..*/.SQLI
343c6 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
343c7 65 33 5f 73 6c 65 65 70 28 69 6e 74 20 6d 73 29  e3_sleep(int ms)
343c8 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  {.  sqlite3_vfs 
343c9 2a 70 56 66 73 3b 0a 20 20 69 6e 74 20 72 63 3b  *pVfs;.  int rc;
343ca 0a 20 20 70 56 66 73 20 3d 20 73 71 6c 69 74 65  .  pVfs = sqlite
343cb 33 5f 76 66 73 5f 66 69 6e 64 28 30 29 3b 0a 20  3_vfs_find(0);. 
343cc 20 69 66 28 20 70 56 66 73 3d 3d 30 20 29 20 72   if( pVfs==0 ) r
343cd 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 54  eturn 0;..  /* T
343ce 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 6f 72  his function wor
343cf 6b 73 20 69 6e 20 6d 69 6c 6c 69 73 65 63 6f 6e  ks in millisecon
343d0 64 73 2c 20 62 75 74 20 74 68 65 20 75 6e 64 65  ds, but the unde
343d1 72 6c 79 69 6e 67 20 4f 73 53 6c 65 65 70 28 29  rlying OsSleep()
343d2 20 0a 20 20 2a 2a 20 41 50 49 20 75 73 65 73 20   .  ** API uses 
343d3 6d 69 63 72 6f 73 65 63 6f 6e 64 73 2e 20 48 65  microseconds. He
343d4 6e 63 65 20 74 68 65 20 31 30 30 30 27 73 2e 0a  nce the 1000's..
343d5 20 20 2a 2f 0a 20 20 72 63 20 3d 20 28 73 71 6c    */.  rc = (sql
343d6 69 74 65 33 4f 73 53 6c 65 65 70 28 70 56 66 73  ite3OsSleep(pVfs
343d7 2c 20 31 30 30 30 2a 6d 73 29 2f 31 30 30 30 29  , 1000*ms)/1000)
343d8 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
343d9 0a 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c 65 20 6f  ../*.** Enable o
343da 72 20 64 69 73 61 62 6c 65 20 74 68 65 20 65 78  r disable the ex
343db 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f  tended result co
343dc 64 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  des..*/.SQLITE_A
343dd 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65  PI int sqlite3_e
343de 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63  xtended_result_c
343df 6f 64 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62  odes(sqlite3 *db
343e0 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 7b 0a 20 20  , int onoff){.  
343e1 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
343e2 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
343e3 20 20 64 62 2d 3e 65 72 72 4d 61 73 6b 20 3d 20    db->errMask = 
343e4 6f 6e 6f 66 66 20 3f 20 30 78 66 66 66 66 66 66  onoff ? 0xffffff
343e5 66 66 20 3a 20 30 78 66 66 3b 0a 20 20 73 71 6c  ff : 0xff;.  sql
343e6 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
343e7 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72  (db->mutex);.  r
343e8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
343e9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65  .}../*.** Invoke
343ea 20 74 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f   the xFileContro
343eb 6c 20 6d 65 74 68 6f 64 20 6f 6e 20 61 20 70 61  l method on a pa
343ec 72 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73  rticular databas
343ed 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  e..*/.SQLITE_API
343ee 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 69 6c   int sqlite3_fil
343ef 65 5f 63 6f 6e 74 72 6f 6c 28 73 71 6c 69 74 65  e_control(sqlite
343f0 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61  3 *db, const cha
343f1 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 69 6e 74 20  r *zDbName, int 
343f2 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29 7b  op, void *pArg){
343f3 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
343f4 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 6e 74 20  TE_ERROR;.  int 
343f5 69 44 62 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  iDb;.  sqlite3_m
343f6 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
343f7 75 74 65 78 29 3b 0a 20 20 69 66 28 20 7a 44 62  utex);.  if( zDb
343f8 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 69  Name==0 ){.    i
343f9 44 62 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  Db = 0;.  }else{
343fa 0a 20 20 20 20 66 6f 72 28 69 44 62 3d 30 3b 20  .    for(iDb=0; 
343fb 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 69 44 62  iDb<db->nDb; iDb
343fc 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73  ++){.      if( s
343fd 74 72 63 6d 70 28 64 62 2d 3e 61 44 62 5b 69 44  trcmp(db->aDb[iD
343fe 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 44 62 4e 61 6d  b].zName, zDbNam
343ff 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20  e)==0 ) break;. 
34400 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 69     }.  }.  if( i
34401 44 62 3c 64 62 2d 3e 6e 44 62 20 29 7b 0a 20 20  Db<db->nDb ){.  
34402 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 20    Btree *pBtree 
34403 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  = db->aDb[iDb].p
34404 42 74 3b 0a 20 20 20 20 69 66 28 20 70 42 74 72  Bt;.    if( pBtr
34405 65 65 20 29 7b 0a 20 20 20 20 20 20 50 61 67 65  ee ){.      Page
34406 72 20 2a 70 50 61 67 65 72 3b 0a 20 20 20 20 20  r *pPager;.     
34407 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66   sqlite3_file *f
34408 64 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  d;.      sqlite3
34409 42 74 72 65 65 45 6e 74 65 72 28 70 42 74 72 65  BtreeEnter(pBtre
3440a 65 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72  e);.      pPager
3440b 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50   = sqlite3BtreeP
3440c 61 67 65 72 28 70 42 74 72 65 65 29 3b 0a 20 20  ager(pBtree);.  
3440d 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
3440e 65 72 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 66  er!=0 );.      f
3440f 64 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  d = sqlite3Pager
34410 46 69 6c 65 28 70 50 61 67 65 72 29 3b 0a 20 20  File(pPager);.  
34411 20 20 20 20 61 73 73 65 72 74 28 20 66 64 21 3d      assert( fd!=
34412 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 66  0 );.      if( f
34413 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20  d->pMethods ){. 
34414 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
34415 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c  te3OsFileControl
34416 28 66 64 2c 20 6f 70 2c 20 70 41 72 67 29 3b 0a  (fd, op, pArg);.
34417 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
34418 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
34419 70 42 74 72 65 65 29 3b 0a 20 20 20 20 7d 0a 20  pBtree);.    }. 
3441a 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
3441b 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
3441c 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
3441d 3b 20 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  ;   .}../*.** In
3441e 74 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 74  terface to the t
3441f 65 73 74 69 6e 67 20 6c 6f 67 69 63 2e 0a 2a 2f  esting logic..*/
34420 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
34421 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
34422 74 72 6f 6c 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e  trol(int op, ...
34423 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b  ){.  int rc = 0;
34424 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
34425 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53  OMIT_BUILTIN_TES
34426 54 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a  T.  va_list ap;.
34427 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20 6f    va_start(ap, o
34428 70 29 3b 0a 20 20 73 77 69 74 63 68 28 20 6f 70  p);.  switch( op
34429 20 29 7b 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20   ){..    /*.    
3442a 2a 2a 20 53 61 76 65 20 74 68 65 20 63 75 72 72  ** Save the curr
3442b 65 6e 74 20 73 74 61 74 65 20 6f 66 20 74 68 65  ent state of the
3442c 20 50 52 4e 47 2e 0a 20 20 20 20 2a 2f 0a 20 20   PRNG..    */.  
3442d 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45    case SQLITE_TE
3442e 53 54 43 54 52 4c 5f 50 52 4e 47 5f 53 41 56 45  STCTRL_PRNG_SAVE
3442f 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  : {.      sqlite
34430 33 50 72 6e 67 53 61 76 65 53 74 61 74 65 28 29  3PrngSaveState()
34431 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
34432 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20     }..    /*.   
34433 20 2a 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20   ** Restore the 
34434 73 74 61 74 65 20 6f 66 20 74 68 65 20 50 52 4e  state of the PRN
34435 47 20 74 6f 20 74 68 65 20 6c 61 73 74 20 73 74  G to the last st
34436 61 74 65 20 73 61 76 65 64 20 75 73 69 6e 67 0a  ate saved using.
34437 20 20 20 20 2a 2a 20 50 52 4e 47 5f 53 41 56 45      ** PRNG_SAVE
34438 2e 20 20 49 66 20 50 52 4e 47 5f 53 41 56 45 20  .  If PRNG_SAVE 
34439 68 61 73 20 6e 65 76 65 72 20 62 65 66 6f 72 65  has never before
3443a 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 20 74 68   been called, th
3443b 65 6e 0a 20 20 20 20 2a 2a 20 74 68 69 73 20 76  en.    ** this v
3443c 65 72 62 20 61 63 74 73 20 6c 69 6b 65 20 50 52  erb acts like PR
3443d 4e 47 5f 52 45 53 45 54 2e 0a 20 20 20 20 2a 2f  NG_RESET..    */
3443e 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
3443f 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52  _TESTCTRL_PRNG_R
34440 45 53 54 4f 52 45 3a 20 7b 0a 20 20 20 20 20 20  ESTORE: {.      
34441 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f  sqlite3PrngResto
34442 72 65 53 74 61 74 65 28 29 3b 0a 20 20 20 20 20  reState();.     
34443 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
34444 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 52 65 73     /*.    ** Res
34445 65 74 20 74 68 65 20 50 52 4e 47 20 62 61 63 6b  et the PRNG back
34446 20 74 6f 20 69 74 73 20 75 6e 69 6e 69 74 69 61   to its uninitia
34447 6c 69 7a 65 64 20 73 74 61 74 65 2e 20 20 54 68  lized state.  Th
34448 65 20 6e 65 78 74 20 63 61 6c 6c 0a 20 20 20 20  e next call.    
34449 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 72 61  ** to sqlite3_ra
3444a 6e 64 6f 6d 6e 65 73 73 28 29 20 77 69 6c 6c 20  ndomness() will 
3444b 72 65 73 65 65 64 20 74 68 65 20 50 52 4e 47 20  reseed the PRNG 
3444c 75 73 69 6e 67 20 61 20 73 69 6e 67 6c 65 20 63  using a single c
3444d 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68  all.    ** to th
3444e 65 20 78 52 61 6e 64 6f 6d 6e 65 73 73 20 6d 65  e xRandomness me
3444f 74 68 6f 64 20 6f 66 20 74 68 65 20 64 65 66 61  thod of the defa
34450 75 6c 74 20 56 46 53 2e 0a 20 20 20 20 2a 2f 0a  ult VFS..    */.
34451 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
34452 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 52 45  TESTCTRL_PRNG_RE
34453 53 45 54 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  SET: {.      sql
34454 69 74 65 33 50 72 6e 67 52 65 73 65 74 53 74 61  ite3PrngResetSta
34455 74 65 28 29 3b 0a 20 20 20 20 20 20 62 72 65 61  te();.      brea
34456 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
34457 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69 74 65 33  .    **  sqlite3
34458 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 42 49  _test_control(BI
34459 54 56 45 43 5f 54 45 53 54 2c 20 73 69 7a 65 2c  TVEC_TEST, size,
3445a 20 70 72 6f 67 72 61 6d 29 0a 20 20 20 20 2a 2a   program).    **
3445b 0a 20 20 20 20 2a 2a 20 52 75 6e 20 61 20 74 65  .    ** Run a te
3445c 73 74 20 61 67 61 69 6e 73 74 20 61 20 42 69 74  st against a Bit
3445d 76 65 63 20 6f 62 6a 65 63 74 20 6f 66 20 73 69  vec object of si
3445e 7a 65 2e 20 20 54 68 65 20 70 72 6f 67 72 61 6d  ze.  The program
3445f 20 61 72 67 75 6d 65 6e 74 0a 20 20 20 20 2a 2a   argument.    **
34460 20 69 73 20 61 6e 20 61 72 72 61 79 20 6f 66 20   is an array of 
34461 69 6e 74 65 67 65 72 73 20 74 68 61 74 20 64 65  integers that de
34462 66 69 6e 65 73 20 74 68 65 20 74 65 73 74 2e 20  fines the test. 
34463 20 52 65 74 75 72 6e 20 2d 31 20 6f 6e 20 61 0a   Return -1 on a.
34464 20 20 20 20 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c      ** memory al
34465 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 2c 20  location error, 
34466 30 20 6f 6e 20 73 75 63 63 65 73 73 2c 20 6f 72  0 on success, or
34467 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f 72 20 61 6e   non-zero for an
34468 20 65 72 72 6f 72 2e 0a 20 20 20 20 2a 2a 20 53   error..    ** S
34469 65 65 20 74 68 65 20 73 71 6c 69 74 65 33 42 69  ee the sqlite3Bi
3446a 74 76 65 63 42 75 69 6c 74 69 6e 54 65 73 74 28  tvecBuiltinTest(
3446b 29 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  ) for additional
3446c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 20 20   information..  
3446d 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51    */.    case SQ
3446e 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 49  LITE_TESTCTRL_BI
3446f 54 56 45 43 5f 54 45 53 54 3a 20 7b 0a 20 20 20  TVEC_TEST: {.   
34470 20 20 20 69 6e 74 20 73 7a 20 3d 20 76 61 5f 61     int sz = va_a
34471 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20  rg(ap, int);.   
34472 20 20 20 69 6e 74 20 2a 61 50 72 6f 67 20 3d 20     int *aProg = 
34473 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 2a 29  va_arg(ap, int*)
34474 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
34475 69 74 65 33 42 69 74 76 65 63 42 75 69 6c 74 69  ite3BitvecBuilti
34476 6e 54 65 73 74 28 73 7a 2c 20 61 50 72 6f 67 29  nTest(sz, aProg)
34477 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
34478 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20     }..    /*.   
34479 20 2a 2a 20 20 73 71 6c 69 74 65 33 5f 74 65 73   **  sqlite3_tes
3447a 74 5f 63 6f 6e 74 72 6f 6c 28 42 45 4e 49 47 4e  t_control(BENIGN
3447b 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 2c 20 78  _MALLOC_HOOKS, x
3447c 42 65 67 69 6e 2c 20 78 45 6e 64 29 0a 20 20 20  Begin, xEnd).   
3447d 20 2a 2a 0a 20 20 20 20 2a 2a 20 52 65 67 69 73   **.    ** Regis
3447e 74 65 72 20 68 6f 6f 6b 73 20 74 6f 20 63 61 6c  ter hooks to cal
3447f 6c 20 74 6f 20 69 6e 64 69 63 61 74 65 20 77 68  l to indicate wh
34480 69 63 68 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69  ich malloc() fai
34481 6c 75 72 65 73 20 0a 20 20 20 20 2a 2a 20 61 72  lures .    ** ar
34482 65 20 62 65 6e 69 67 6e 2e 0a 20 20 20 20 2a 2f  e benign..    */
34483 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
34484 5f 54 45 53 54 43 54 52 4c 5f 42 45 4e 49 47 4e  _TESTCTRL_BENIGN
34485 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53 3a 20 7b  _MALLOC_HOOKS: {
34486 0a 20 20 20 20 20 20 74 79 70 65 64 65 66 20 76  .      typedef v
34487 6f 69 64 20 28 2a 76 6f 69 64 5f 66 75 6e 63 74  oid (*void_funct
34488 69 6f 6e 29 28 76 6f 69 64 29 3b 0a 20 20 20 20  ion)(void);.    
34489 20 20 76 6f 69 64 5f 66 75 6e 63 74 69 6f 6e 20    void_function 
3448a 78 42 65 6e 69 67 6e 42 65 67 69 6e 3b 0a 20 20  xBenignBegin;.  
3448b 20 20 20 20 76 6f 69 64 5f 66 75 6e 63 74 69 6f      void_functio
3448c 6e 20 78 42 65 6e 69 67 6e 45 6e 64 3b 0a 20 20  n xBenignEnd;.  
3448d 20 20 20 20 78 42 65 6e 69 67 6e 42 65 67 69 6e      xBenignBegin
3448e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76 6f   = va_arg(ap, vo
3448f 69 64 5f 66 75 6e 63 74 69 6f 6e 29 3b 0a 20 20  id_function);.  
34490 20 20 20 20 78 42 65 6e 69 67 6e 45 6e 64 20 3d      xBenignEnd =
34491 20 76 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64   va_arg(ap, void
34492 5f 66 75 6e 63 74 69 6f 6e 29 3b 0a 20 20 20 20  _function);.    
34493 20 20 73 71 6c 69 74 65 33 42 65 6e 69 67 6e 4d    sqlite3BenignM
34494 61 6c 6c 6f 63 48 6f 6f 6b 73 28 78 42 65 6e 69  allocHooks(xBeni
34495 67 6e 42 65 67 69 6e 2c 20 78 42 65 6e 69 67 6e  gnBegin, xBenign
34496 45 6e 64 29 3b 0a 20 20 20 20 20 20 62 72 65 61  End);.      brea
34497 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  k;.    }..    /*
34498 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69 74 65 33  .    **  sqlite3
34499 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 53 51  _test_control(SQ
3449a 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 45  LITE_TESTCTRL_PE
3449b 4e 44 49 4e 47 5f 42 59 54 45 2c 20 75 6e 73 69  NDING_BYTE, unsi
3449c 67 6e 65 64 20 69 6e 74 20 58 29 0a 20 20 20 20  gned int X).    
3449d 2a 2a 0a 20 20 20 20 2a 2a 20 53 65 74 20 74 68  **.    ** Set th
3449e 65 20 50 45 4e 44 49 4e 47 20 62 79 74 65 20 74  e PENDING byte t
3449f 6f 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 74  o the value in t
344a0 68 65 20 61 72 67 75 6d 65 6e 74 2c 20 69 66 20  he argument, if 
344a1 58 3e 30 2e 0a 20 20 20 20 2a 2a 20 4d 61 6b 65  X>0..    ** Make
344a2 20 6e 6f 20 63 68 61 6e 67 65 73 20 69 66 20 58   no changes if X
344a3 3d 3d 30 2e 20 20 52 65 74 75 72 6e 20 74 68 65  ==0.  Return the
344a4 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 70 65   value of the pe
344a5 6e 64 69 6e 67 20 62 79 74 65 0a 20 20 20 20 2a  nding byte.    *
344a6 2a 20 61 73 20 69 74 20 65 78 69 73 74 69 6e 67  * as it existing
344a7 20 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f 75   before this rou
344a8 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 2e  tine was called.
344a9 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49  .    **.    ** I
344aa 4d 50 4f 52 54 41 4e 54 3a 20 20 43 68 61 6e 67  MPORTANT:  Chang
344ab 69 6e 67 20 74 68 65 20 50 45 4e 44 49 4e 47 20  ing the PENDING 
344ac 62 79 74 65 20 66 72 6f 6d 20 30 78 34 30 30 30  byte from 0x4000
344ad 30 30 30 30 20 72 65 73 75 6c 74 73 20 69 6e 0a  0000 results in.
344ae 20 20 20 20 2a 2a 20 61 6e 20 69 6e 63 6f 6d 70      ** an incomp
344af 61 74 69 62 6c 65 20 64 61 74 61 62 61 73 65 20  atible database 
344b0 66 69 6c 65 20 66 6f 72 6d 61 74 2e 20 20 43 68  file format.  Ch
344b1 61 6e 67 69 6e 67 20 74 68 65 20 50 45 4e 44 49  anging the PENDI
344b2 4e 47 20 62 79 74 65 0a 20 20 20 20 2a 2a 20 77  NG byte.    ** w
344b3 68 69 6c 65 20 61 6e 79 20 64 61 74 61 62 61 73  hile any databas
344b4 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
344b5 6f 70 65 6e 20 72 65 73 75 6c 74 73 20 69 6e 20  open results in 
344b6 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 0a 20 20  undefined and.  
344b7 20 20 2a 2a 20 64 69 6c 65 74 65 72 69 6f 75 73    ** dileterious
344b8 20 62 65 68 61 76 69 6f 72 2e 0a 20 20 20 20 2a   behavior..    *
344b9 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  /.    case SQLIT
344ba 45 5f 54 45 53 54 43 54 52 4c 5f 50 45 4e 44 49  E_TESTCTRL_PENDI
344bb 4e 47 5f 42 59 54 45 3a 20 7b 0a 20 20 20 20 20  NG_BYTE: {.     
344bc 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6e 65   unsigned int ne
344bd 77 56 61 6c 20 3d 20 76 61 5f 61 72 67 28 61 70  wVal = va_arg(ap
344be 2c 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 29 3b  , unsigned int);
344bf 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
344c0 74 65 33 50 65 6e 64 69 6e 67 42 79 74 65 3b 0a  te3PendingByte;.
344c1 20 20 20 20 20 20 69 66 28 20 6e 65 77 56 61 6c        if( newVal
344c2 20 29 20 73 71 6c 69 74 65 33 50 65 6e 64 69 6e   ) sqlite3Pendin
344c3 67 42 79 74 65 20 3d 20 6e 65 77 56 61 6c 3b 0a  gByte = newVal;.
344c4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
344c5 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a   }..    /*.    *
344c6 2a 20 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f  *  sqlite3_test_
344c7 63 6f 6e 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54  control(SQLITE_T
344c8 45 53 54 43 54 52 4c 5f 41 53 53 45 52 54 2c 20  ESTCTRL_ASSERT, 
344c9 69 6e 74 20 58 29 0a 20 20 20 20 2a 2a 0a 20 20  int X).    **.  
344ca 20 20 2a 2a 20 54 68 69 73 20 61 63 74 69 6f 6e    ** This action
344cb 20 70 72 6f 76 69 64 65 73 20 61 20 72 75 6e 2d   provides a run-
344cc 74 69 6d 65 20 74 65 73 74 20 74 6f 20 73 65 65  time test to see
344cd 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a   whether or not.
344ce 20 20 20 20 2a 2a 20 61 73 73 65 72 74 28 29 20      ** assert() 
344cf 77 61 73 20 65 6e 61 62 6c 65 64 20 61 74 20 63  was enabled at c
344d0 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 20 20 49 66  ompile-time.  If
344d1 20 58 20 69 73 20 74 72 75 65 20 61 6e 64 20 61   X is true and a
344d2 73 73 65 72 74 28 29 0a 20 20 20 20 2a 2a 20 69  ssert().    ** i
344d3 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 20  s enabled, then 
344d4 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
344d5 20 69 73 20 74 72 75 65 2e 20 20 49 66 20 58 20   is true.  If X 
344d6 69 73 20 74 72 75 65 20 61 6e 64 0a 20 20 20 20  is true and.    
344d7 2a 2a 20 61 73 73 65 72 74 28 29 20 69 73 20 64  ** assert() is d
344d8 69 73 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68  isabled, then th
344d9 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69  e return value i
344da 73 20 7a 65 72 6f 2e 20 20 49 66 20 58 20 69 73  s zero.  If X is
344db 0a 20 20 20 20 2a 2a 20 66 61 6c 73 65 20 61 6e  .    ** false an
344dc 64 20 61 73 73 65 72 74 28 29 20 69 73 20 65 6e  d assert() is en
344dd 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 20  abled, then the 
344de 61 73 73 65 72 74 69 6f 6e 20 66 69 72 65 73 20  assertion fires 
344df 61 6e 64 20 74 68 65 0a 20 20 20 20 2a 2a 20 70  and the.    ** p
344e0 72 6f 63 65 73 73 20 61 62 6f 72 74 73 2e 20 20  rocess aborts.  
344e1 49 66 20 58 20 69 73 20 66 61 6c 73 65 20 61 6e  If X is false an
344e2 64 20 61 73 73 65 72 74 28 29 20 69 73 20 64 69  d assert() is di
344e3 73 61 62 6c 65 64 2c 20 74 68 65 6e 20 74 68 65  sabled, then the
344e4 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 76  .    ** return v
344e5 61 6c 75 65 20 69 73 20 7a 65 72 6f 2e 0a 20 20  alue is zero..  
344e6 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51    */.    case SQ
344e7 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41 53  LITE_TESTCTRL_AS
344e8 53 45 52 54 3a 20 7b 0a 20 20 20 20 20 20 76 6f  SERT: {.      vo
344e9 6c 61 74 69 6c 65 20 69 6e 74 20 78 20 3d 20 30  latile int x = 0
344ea 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
344eb 28 78 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 69  (x = va_arg(ap,i
344ec 6e 74 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 20  nt))!=0 );.     
344ed 20 72 63 20 3d 20 78 3b 0a 20 20 20 20 20 20 62   rc = x;.      b
344ee 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 0a 20 20  reak;.    }...  
344ef 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 20 73 71 6c    /*.    **  sql
344f0 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f  ite3_test_contro
344f1 6c 28 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52  l(SQLITE_TESTCTR
344f2 4c 5f 41 4c 57 41 59 53 2c 20 69 6e 74 20 58 29  L_ALWAYS, int X)
344f3 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
344f4 68 69 73 20 61 63 74 69 6f 6e 20 70 72 6f 76 69  his action provi
344f5 64 65 73 20 61 20 72 75 6e 2d 74 69 6d 65 20 74  des a run-time t
344f6 65 73 74 20 74 6f 20 73 65 65 20 68 6f 77 20 74  est to see how t
344f7 68 65 20 41 4c 57 41 59 53 20 61 6e 64 0a 20 20  he ALWAYS and.  
344f8 20 20 2a 2a 20 4e 45 56 45 52 20 6d 61 63 72 6f    ** NEVER macro
344f9 73 20 77 65 72 65 20 64 65 66 69 6e 65 64 20 61  s were defined a
344fa 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a  t compile-time..
344fb 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68      **.    ** Th
344fc 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69  e return value i
344fd 73 20 41 4c 57 41 59 53 28 58 29 2e 20 20 0a 20  s ALWAYS(X).  . 
344fe 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65     **.    ** The
344ff 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 74 65 73   recommended tes
34500 74 20 69 73 20 58 3d 3d 32 2e 20 20 49 66 20 74  t is X==2.  If t
34501 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
34502 69 73 20 32 2c 20 74 68 61 74 20 6d 65 61 6e 73  is 2, that means
34503 0a 20 20 20 20 2a 2a 20 41 4c 57 41 59 53 28 29  .    ** ALWAYS()
34504 20 61 6e 64 20 4e 45 56 45 52 28 29 20 61 72 65   and NEVER() are
34505 20 62 6f 74 68 20 6e 6f 2d 6f 70 20 70 61 73 73   both no-op pass
34506 2d 74 68 72 6f 75 67 68 20 6d 61 63 72 6f 73 2c  -through macros,
34507 20 77 68 69 63 68 20 69 73 20 74 68 65 0a 20 20   which is the.  
34508 20 20 2a 2a 20 64 65 66 61 75 6c 74 20 73 65 74    ** default set
34509 74 69 6e 67 2e 20 20 49 66 20 74 68 65 20 72 65  ting.  If the re
3450a 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 31 2c  turn value is 1,
3450b 20 74 68 65 6e 20 41 4c 57 41 59 53 28 29 20 69   then ALWAYS() i
3450c 73 20 65 69 74 68 65 72 0a 20 20 20 20 2a 2a 20  s either.    ** 
3450d 68 61 72 64 2d 63 6f 64 65 64 20 74 6f 20 74 72  hard-coded to tr
3450e 75 65 20 6f 72 20 65 6c 73 65 20 69 74 20 61 73  ue or else it as
3450f 73 65 72 74 73 20 69 66 20 69 74 73 20 61 72 67  serts if its arg
34510 75 6d 65 6e 74 20 69 73 20 66 61 6c 73 65 2e 0a  ument is false..
34511 20 20 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74      ** The first
34512 20 62 65 68 61 76 69 6f 72 20 28 68 61 72 64 2d   behavior (hard-
34513 63 6f 64 65 64 20 74 6f 20 74 72 75 65 29 20 69  coded to true) i
34514 73 20 74 68 65 20 63 61 73 65 20 69 66 0a 20 20  s the case if.  
34515 20 20 2a 2a 20 53 51 4c 49 54 45 5f 54 45 53 54    ** SQLITE_TEST
34516 43 54 52 4c 5f 41 53 53 45 52 54 20 73 68 6f 77  CTRL_ASSERT show
34517 73 20 74 68 61 74 20 61 73 73 65 72 74 28 29 20  s that assert() 
34518 69 73 20 64 69 73 61 62 6c 65 64 20 61 6e 64 20  is disabled and 
34519 74 68 65 20 73 65 63 6f 6e 64 0a 20 20 20 20 2a  the second.    *
3451a 2a 20 62 65 68 61 76 69 6f 72 20 28 61 73 73 65  * behavior (asse
3451b 72 74 20 69 66 20 74 68 65 20 61 72 67 75 6d 65  rt if the argume
3451c 6e 74 20 74 6f 20 41 4c 57 41 59 53 28 29 20 69  nt to ALWAYS() i
3451d 73 20 66 61 6c 73 65 29 20 69 73 20 74 68 65 20  s false) is the 
3451e 63 61 73 65 20 69 66 0a 20 20 20 20 2a 2a 20 53  case if.    ** S
3451f 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 41  QLITE_TESTCTRL_A
34520 53 53 45 52 54 20 73 68 6f 77 73 20 74 68 61 74  SSERT shows that
34521 20 61 73 73 65 72 74 28 29 20 69 73 20 65 6e 61   assert() is ena
34522 62 6c 65 64 2e 0a 20 20 20 20 2a 2a 0a 20 20 20  bled..    **.   
34523 20 2a 2a 20 54 68 65 20 72 75 6e 2d 74 69 6d 65   ** The run-time
34524 20 74 65 73 74 20 70 72 6f 63 65 64 75 72 65 20   test procedure 
34525 6d 69 67 68 74 20 6c 6f 6f 6b 20 73 6f 6d 65 74  might look somet
34526 68 69 6e 67 20 6c 69 6b 65 20 74 68 69 73 3a 0a  hing like this:.
34527 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
34528 20 69 66 28 20 73 71 6c 69 74 65 33 5f 74 65 73   if( sqlite3_tes
34529 74 5f 63 6f 6e 74 72 6f 6c 28 53 51 4c 49 54 45  t_control(SQLITE
3452a 5f 54 45 53 54 43 54 52 4c 5f 41 4c 57 41 59 53  _TESTCTRL_ALWAYS
3452b 2c 20 32 29 3d 3d 32 20 29 7b 0a 20 20 20 20 2a  , 2)==2 ){.    *
3452c 2a 20 20 20 20 20 20 2f 2f 20 41 4c 57 41 59 53  *      // ALWAYS
3452d 28 29 20 61 6e 64 20 4e 45 56 45 52 28 29 20 61  () and NEVER() a
3452e 72 65 20 6e 6f 2d 6f 70 20 70 61 73 73 2d 74 68  re no-op pass-th
3452f 72 6f 75 67 68 20 6d 61 63 72 6f 73 0a 20 20 20  rough macros.   
34530 20 2a 2a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   **    }else if(
34531 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f   sqlite3_test_co
34532 6e 74 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53  ntrol(SQLITE_TES
34533 54 43 54 52 4c 5f 41 53 53 45 52 54 2c 20 31 29  TCTRL_ASSERT, 1)
34534 20 29 7b 0a 20 20 20 20 2a 2a 20 20 20 20 20 20   ){.    **      
34535 2f 2f 20 41 4c 57 41 59 53 28 78 29 20 61 73 73  // ALWAYS(x) ass
34536 65 72 74 73 20 74 68 61 74 20 78 20 69 73 20 74  erts that x is t
34537 72 75 65 2e 20 4e 45 56 45 52 28 78 29 20 61 73  rue. NEVER(x) as
34538 73 65 72 74 73 20 78 20 69 73 20 66 61 6c 73 65  serts x is false
34539 2e 0a 20 20 20 20 2a 2a 20 20 20 20 7d 65 6c 73  ..    **    }els
3453a 65 7b 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 2f  e{.    **      /
3453b 2f 20 41 4c 57 41 59 53 28 78 29 20 69 73 20 61  / ALWAYS(x) is a
3453c 20 63 6f 6e 73 74 61 6e 74 20 31 2e 20 20 4e 45   constant 1.  NE
3453d 56 45 52 28 78 29 20 69 73 20 61 20 63 6f 6e 73  VER(x) is a cons
3453e 74 61 6e 74 20 30 2e 0a 20 20 20 20 2a 2a 20 20  tant 0..    **  
3453f 20 20 7d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63    }.    */.    c
34540 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ase SQLITE_TESTC
34541 54 52 4c 5f 41 4c 57 41 59 53 3a 20 7b 0a 20 20  TRL_ALWAYS: {.  
34542 20 20 20 20 69 6e 74 20 78 20 3d 20 76 61 5f 61      int x = va_a
34543 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20  rg(ap,int);.    
34544 20 20 72 63 20 3d 20 41 4c 57 41 59 53 28 78 29    rc = ALWAYS(x)
34545 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
34546 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 20 20 73     }..    /*   s
34547 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74  qlite3_test_cont
34548 72 6f 6c 28 53 51 4c 49 54 45 5f 54 45 53 54 43  rol(SQLITE_TESTC
34549 54 52 4c 5f 52 45 53 45 52 56 45 2c 20 73 71 6c  TRL_RESERVE, sql
3454a 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 4e 29  ite3 *db, int N)
3454b 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 53  .    **.    ** S
3454c 65 74 20 74 68 65 20 6e 52 65 73 65 72 76 65 20  et the nReserve 
3454d 73 69 7a 65 20 74 6f 20 4e 20 66 6f 72 20 74 68  size to N for th
3454e 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
3454f 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 0a  on the database.
34550 20 20 20 20 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f      ** connectio
34551 6e 20 64 62 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  n db..    */.   
34552 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45 53   case SQLITE_TES
34553 54 43 54 52 4c 5f 52 45 53 45 52 56 45 3a 20 7b  TCTRL_RESERVE: {
34554 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 20 2a  .      sqlite3 *
34555 64 62 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  db = va_arg(ap, 
34556 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 20 20 20  sqlite3*);.     
34557 20 69 6e 74 20 78 20 3d 20 76 61 5f 61 72 67 28   int x = va_arg(
34558 61 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 73  ap,int);.      s
34559 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
3455a 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
3455b 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
3455c 65 53 65 74 50 61 67 65 53 69 7a 65 28 64 62 2d  eSetPageSize(db-
3455d 3e 61 44 62 5b 30 5d 2e 70 42 74 2c 20 30 2c 20  >aDb[0].pBt, 0, 
3455e 78 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  x, 0);.      sql
3455f 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
34560 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  (db->mutex);.   
34561 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
34562 0a 20 20 7d 0a 20 20 76 61 5f 65 6e 64 28 61 70  .  }.  va_end(ap
34563 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  );.#endif /* SQL
34564 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e  ITE_OMIT_BUILTIN
34565 5f 54 45 53 54 20 2a 2f 0a 20 20 72 65 74 75 72  _TEST */.  retur
34566 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  n rc;.}../******
34567 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
34568 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  main.c *********
34569 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3456a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3456b 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
3456c 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
3456d 69 6c 65 20 6e 6f 74 69 66 79 2e 63 20 2a 2a 2a  ile notify.c ***
3456e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3456f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34570 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
34571 30 30 39 20 4d 61 72 63 68 20 33 0a 2a 2a 0a 2a  009 March 3.**.*
34572 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
34573 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
34574 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
34575 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
34576 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
34577 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
34578 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
34579 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
3457a 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
3457b 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
3457c 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
3457d 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
3457e 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
3457f 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
34580 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
34581 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
34582 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
34583 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
34584 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34585 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34586 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34587 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
34588 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
34589 69 6e 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  ins the implemen
3458a 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71  tation of the sq
3458b 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b 5f 6e 6f 74  lite3_unlock_not
3458c 69 66 79 28 29 0a 2a 2a 20 41 50 49 20 6d 65 74  ify().** API met
3458d 68 6f 64 20 61 6e 64 20 69 74 73 20 61 73 73 6f  hod and its asso
3458e 63 69 61 74 65 64 20 66 75 6e 63 74 69 6f 6e 61  ciated functiona
3458f 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  lity..**.** $Id:
34590 20 6e 6f 74 69 66 79 2e 63 2c 76 20 31 2e 34 20   notify.c,v 1.4 
34591 32 30 30 39 2f 30 34 2f 30 37 20 32 32 3a 30 36  2009/04/07 22:06
34592 3a 35 37 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :57 drh Exp $.*/
34593 0a 0a 2f 2a 20 4f 6d 69 74 20 74 68 69 73 20 65  ../* Omit this e
34594 6e 74 69 72 65 20 66 69 6c 65 20 69 66 20 53 51  ntire file if SQ
34595 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 4e 4c 4f  LITE_ENABLE_UNLO
34596 43 4b 5f 4e 4f 54 49 46 59 20 69 73 20 6e 6f 74  CK_NOTIFY is not
34597 20 64 65 66 69 6e 65 64 2e 20 2a 2f 0a 23 69 66   defined. */.#if
34598 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
34599 45 5f 55 4e 4c 4f 43 4b 5f 4e 4f 54 49 46 59 0a  E_UNLOCK_NOTIFY.
3459a 0a 2f 2a 0a 2a 2a 20 50 75 62 6c 69 63 20 69 6e  ./*.** Public in
3459b 74 65 72 66 61 63 65 73 3a 0a 2a 2a 0a 2a 2a 20  terfaces:.**.** 
3459c 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74    sqlite3Connect
3459d 69 6f 6e 42 6c 6f 63 6b 65 64 28 29 0a 2a 2a 20  ionBlocked().** 
3459e 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63 74    sqlite3Connect
3459f 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 29 0a 2a 2a  ionUnlocked().**
345a0 20 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65 63     sqlite3Connec
345a1 74 69 6f 6e 43 6c 6f 73 65 64 28 29 0a 2a 2a 20  tionClosed().** 
345a2 20 20 73 71 6c 69 74 65 33 5f 75 6e 6c 6f 63 6b    sqlite3_unlock
345a3 5f 6e 6f 74 69 66 79 28 29 0a 2a 2f 0a 0a 23 64  _notify().*/..#d
345a4 65 66 69 6e 65 20 61 73 73 65 72 74 4d 75 74 65  efine assertMute
345a5 78 48 65 6c 64 28 29 20 5c 0a 20 20 61 73 73 65  xHeld() \.  asse
345a6 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
345a7 78 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 4d 75  x_held(sqlite3Mu
345a8 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
345a9 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53  MUTEX_STATIC_MAS
345aa 54 45 52 29 29 20 29 0a 0a 2f 2a 0a 2a 2a 20 48  TER)) )../*.** H
345ab 65 61 64 20 6f 66 20 61 20 6c 69 6e 6b 65 64 20  ead of a linked 
345ac 6c 69 73 74 20 6f 66 20 61 6c 6c 20 73 71 6c 69  list of all sqli
345ad 74 65 33 20 6f 62 6a 65 63 74 73 20 63 72 65 61  te3 objects crea
345ae 74 65 64 20 62 79 20 74 68 69 73 20 70 72 6f 63  ted by this proc
345af 65 73 73 0a 2a 2a 20 66 6f 72 20 77 68 69 63 68  ess.** for which
345b0 20 65 69 74 68 65 72 20 73 71 6c 69 74 65 33 2e   either sqlite3.
345b1 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74  pBlockingConnect
345b2 69 6f 6e 20 6f 72 20 73 71 6c 69 74 65 33 2e 70  ion or sqlite3.p
345b3 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e  UnlockConnection
345b4 0a 2a 2a 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e  .** is not NULL.
345b5 20 54 68 69 73 20 76 61 72 69 61 62 6c 65 20 6d   This variable m
345b6 61 79 20 6f 6e 6c 79 20 61 63 63 65 73 73 65 64  ay only accessed
345b7 20 77 68 69 6c 65 20 74 68 65 20 53 54 41 54 49   while the STATI
345b8 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 6d 75 74 65  C_MASTER.** mute
345b9 78 20 69 73 20 68 65 6c 64 2e 0a 2a 2f 0a 73 74  x is held..*/.st
345ba 61 74 69 63 20 73 71 6c 69 74 65 33 20 2a 53 51  atic sqlite3 *SQ
345bb 4c 49 54 45 5f 57 53 44 20 73 71 6c 69 74 65 33  LITE_WSD sqlite3
345bc 42 6c 6f 63 6b 65 64 4c 69 73 74 20 3d 20 30 3b  BlockedList = 0;
345bd 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
345be 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
345bf 74 69 6f 6e 20 69 73 20 61 20 63 6f 6d 70 6c 65  tion is a comple
345c0 78 20 61 73 73 65 72 74 28 29 20 74 68 61 74 20  x assert() that 
345c1 76 65 72 69 66 69 65 73 20 74 68 65 20 66 6f 6c  verifies the fol
345c2 6c 6f 77 69 6e 67 20 0a 2a 2a 20 70 72 6f 70 65  lowing .** prope
345c3 72 74 69 65 73 20 6f 66 20 74 68 65 20 62 6c 6f  rties of the blo
345c4 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  cked connections
345c5 20 6c 69 73 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 31   list:.**.**   1
345c6 29 20 45 61 63 68 20 65 6e 74 72 79 20 69 6e 20  ) Each entry in 
345c7 74 68 65 20 6c 69 73 74 20 68 61 73 20 61 20 6e  the list has a n
345c8 6f 6e 2d 4e 55 4c 4c 20 76 61 6c 75 65 20 66 6f  on-NULL value fo
345c9 72 20 65 69 74 68 65 72 20 0a 2a 2a 20 20 20 20  r either .**    
345ca 20 20 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74    pUnlockConnect
345cb 69 6f 6e 20 6f 72 20 70 42 6c 6f 63 6b 69 6e 67  ion or pBlocking
345cc 43 6f 6e 6e 65 63 74 69 6f 6e 2c 20 6f 72 20 62  Connection, or b
345cd 6f 74 68 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29 20  oth..**.**   2) 
345ce 41 6c 6c 20 65 6e 74 72 69 65 73 20 69 6e 20 74  All entries in t
345cf 68 65 20 6c 69 73 74 20 74 68 61 74 20 73 68 61  he list that sha
345d0 72 65 20 61 20 63 6f 6d 6d 6f 6e 20 76 61 6c 75  re a common valu
345d1 65 20 66 6f 72 20 0a 2a 2a 20 20 20 20 20 20 78  e for .**      x
345d2 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 61 72 65  UnlockNotify are
345d3 20 67 72 6f 75 70 65 64 20 74 6f 67 65 74 68 65   grouped togethe
345d4 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 29 20 49 66  r..**.**   3) If
345d5 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 64 62   the argument db
345d6 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
345d7 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 65  en none of the e
345d8 6e 74 72 69 65 73 20 69 6e 20 74 68 65 0a 2a 2a  ntries in the.**
345d9 20 20 20 20 20 20 62 6c 6f 63 6b 65 64 20 63 6f        blocked co
345da 6e 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74 20 68  nnections list h
345db 61 76 65 20 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65  ave pUnlockConne
345dc 63 74 69 6f 6e 20 6f 72 20 70 42 6c 6f 63 6b 69  ction or pBlocki
345dd 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20  ngConnection.** 
345de 20 20 20 20 20 73 65 74 20 74 6f 20 64 62 2e 20       set to db. 
345df 54 68 69 73 20 69 73 20 75 73 65 64 20 77 68 65  This is used whe
345e0 6e 20 63 6c 6f 73 69 6e 67 20 63 6f 6e 6e 65 63  n closing connec
345e1 74 69 6f 6e 20 64 62 2e 0a 2a 2f 0a 73 74 61 74  tion db..*/.stat
345e2 69 63 20 76 6f 69 64 20 63 68 65 63 6b 4c 69 73  ic void checkLis
345e3 74 50 72 6f 70 65 72 74 69 65 73 28 73 71 6c 69  tProperties(sqli
345e4 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69  te3 *db){.  sqli
345e5 74 65 33 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d  te3 *p;.  for(p=
345e6 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69  sqlite3BlockedLi
345e7 73 74 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78  st; p; p=p->pNex
345e8 74 42 6c 6f 63 6b 65 64 29 7b 0a 20 20 20 20 69  tBlocked){.    i
345e9 6e 74 20 73 65 65 6e 20 3d 20 30 3b 0a 20 20 20  nt seen = 0;.   
345ea 20 73 71 6c 69 74 65 33 20 2a 70 32 3b 0a 0a 20   sqlite3 *p2;.. 
345eb 20 20 20 2f 2a 20 56 65 72 69 66 79 20 70 72 6f     /* Verify pro
345ec 70 65 72 74 79 20 28 31 29 20 2a 2f 0a 20 20 20  perty (1) */.   
345ed 20 61 73 73 65 72 74 28 20 70 2d 3e 70 55 6e 6c   assert( p->pUnl
345ee 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 20 7c 7c  ockConnection ||
345ef 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e   p->pBlockingCon
345f0 6e 65 63 74 69 6f 6e 20 29 3b 0a 0a 20 20 20 20  nection );..    
345f1 2f 2a 20 56 65 72 69 66 79 20 70 72 6f 70 65 72  /* Verify proper
345f2 74 79 20 28 32 29 20 2a 2f 0a 20 20 20 20 66 6f  ty (2) */.    fo
345f3 72 28 70 32 3d 73 71 6c 69 74 65 33 42 6c 6f 63  r(p2=sqlite3Bloc
345f4 6b 65 64 4c 69 73 74 3b 20 70 32 21 3d 70 3b 20  kedList; p2!=p; 
345f5 70 32 3d 70 32 2d 3e 70 4e 65 78 74 42 6c 6f 63  p2=p2->pNextBloc
345f6 6b 65 64 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ked){.      if( 
345f7 70 32 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66  p2->xUnlockNotif
345f8 79 3d 3d 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74  y==p->xUnlockNot
345f9 69 66 79 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a  ify ) seen = 1;.
345fa 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 32        assert( p2
345fb 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 3d  ->xUnlockNotify=
345fc 3d 70 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66  =p->xUnlockNotif
345fd 79 20 7c 7c 20 21 73 65 65 6e 20 29 3b 0a 20 20  y || !seen );.  
345fe 20 20 20 20 61 73 73 65 72 74 28 20 64 62 3d 3d      assert( db==
345ff 30 20 7c 7c 20 70 2d 3e 70 55 6e 6c 6f 63 6b 43  0 || p->pUnlockC
34600 6f 6e 6e 65 63 74 69 6f 6e 21 3d 64 62 20 29 3b  onnection!=db );
34601 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 64  .      assert( d
34602 62 3d 3d 30 20 7c 7c 20 70 2d 3e 70 42 6c 6f 63  b==0 || p->pBloc
34603 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 21 3d  kingConnection!=
34604 64 62 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  db );.    }.  }.
34605 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  }.#else.# define
34606 20 63 68 65 63 6b 4c 69 73 74 50 72 6f 70 65 72   checkListProper
34607 74 69 65 73 28 78 29 0a 23 65 6e 64 69 66 0a 0a  ties(x).#endif..
34608 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 63 6f 6e  /*.** Remove con
34609 6e 65 63 74 69 6f 6e 20 64 62 20 66 72 6f 6d 20  nection db from 
3460a 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e 6e  the blocked conn
3460b 65 63 74 69 6f 6e 73 20 6c 69 73 74 2e 20 49 66  ections list. If
3460c 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a 20 64   connection.** d
3460d 62 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74  b is not current
3460e 6c 79 20 61 20 70 61 72 74 20 6f 66 20 74 68 65  ly a part of the
3460f 20 6c 69 73 74 2c 20 74 68 69 73 20 66 75 6e 63   list, this func
34610 74 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e  tion is a no-op.
34611 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
34612 72 65 6d 6f 76 65 46 72 6f 6d 42 6c 6f 63 6b 65  removeFromBlocke
34613 64 4c 69 73 74 28 73 71 6c 69 74 65 33 20 2a 64  dList(sqlite3 *d
34614 62 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a  b){.  sqlite3 **
34615 70 70 3b 0a 20 20 61 73 73 65 72 74 4d 75 74 65  pp;.  assertMute
34616 78 48 65 6c 64 28 29 3b 0a 20 20 66 6f 72 28 70  xHeld();.  for(p
34617 70 3d 26 73 71 6c 69 74 65 33 42 6c 6f 63 6b 65  p=&sqlite3Blocke
34618 64 4c 69 73 74 3b 20 2a 70 70 3b 20 70 70 20 3d  dList; *pp; pp =
34619 20 26 28 2a 70 70 29 2d 3e 70 4e 65 78 74 42 6c   &(*pp)->pNextBl
3461a 6f 63 6b 65 64 29 7b 0a 20 20 20 20 69 66 28 20  ocked){.    if( 
3461b 2a 70 70 3d 3d 64 62 20 29 7b 0a 20 20 20 20 20  *pp==db ){.     
3461c 20 2a 70 70 20 3d 20 28 2a 70 70 29 2d 3e 70 4e   *pp = (*pp)->pN
3461d 65 78 74 42 6c 6f 63 6b 65 64 3b 0a 20 20 20 20  extBlocked;.    
3461e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
3461f 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20   }.}../*.** Add 
34620 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20 74 6f  connection db to
34621 20 74 68 65 20 62 6c 6f 63 6b 65 64 20 63 6f 6e   the blocked con
34622 6e 65 63 74 69 6f 6e 73 20 6c 69 73 74 2e 20 49  nections list. I
34623 74 20 69 73 20 61 73 73 75 6d 65 64 0a 2a 2a 20  t is assumed.** 
34624 74 68 61 74 20 69 74 20 69 73 20 6e 6f 74 20 61  that it is not a
34625 6c 72 65 61 64 79 20 61 20 70 61 72 74 20 6f 66  lready a part of
34626 20 74 68 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74   the list..*/.st
34627 61 74 69 63 20 76 6f 69 64 20 61 64 64 54 6f 42  atic void addToB
34628 6c 6f 63 6b 65 64 4c 69 73 74 28 73 71 6c 69 74  lockedList(sqlit
34629 65 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69 74  e3 *db){.  sqlit
3462a 65 33 20 2a 2a 70 70 3b 0a 20 20 61 73 73 65 72  e3 **pp;.  asser
3462b 74 4d 75 74 65 78 48 65 6c 64 28 29 3b 0a 20 20  tMutexHeld();.  
3462c 66 6f 72 28 0a 20 20 20 20 70 70 3d 26 73 71 6c  for(.    pp=&sql
3462d 69 74 65 33 42 6c 6f 63 6b 65 64 4c 69 73 74 3b  ite3BlockedList;
3462e 20 0a 20 20 20 20 2a 70 70 20 26 26 20 28 2a 70   .    *pp && (*p
3462f 70 29 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66  p)->xUnlockNotif
34630 79 21 3d 64 62 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f  y!=db->xUnlockNo
34631 74 69 66 79 3b 20 0a 20 20 20 20 70 70 3d 26 28  tify; .    pp=&(
34632 2a 70 70 29 2d 3e 70 4e 65 78 74 42 6c 6f 63 6b  *pp)->pNextBlock
34633 65 64 0a 20 20 29 3b 0a 20 20 64 62 2d 3e 70 4e  ed.  );.  db->pN
34634 65 78 74 42 6c 6f 63 6b 65 64 20 3d 20 2a 70 70  extBlocked = *pp
34635 3b 0a 20 20 2a 70 70 20 3d 20 64 62 3b 0a 7d 0a  ;.  *pp = db;.}.
34636 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 74 68  ./*.** Obtain th
34637 65 20 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20  e STATIC_MASTER 
34638 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 74 69 63  mutex..*/.static
34639 20 76 6f 69 64 20 65 6e 74 65 72 4d 75 74 65 78   void enterMutex
3463a 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65  (void){.  sqlite
3463b 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71  3_mutex_enter(sq
3463c 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
3463d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
3463e 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20  TIC_MASTER));.  
3463f 63 68 65 63 6b 4c 69 73 74 50 72 6f 70 65 72 74  checkListPropert
34640 69 65 73 28 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ies(0);.}../*.**
34641 20 52 65 6c 65 61 73 65 20 74 68 65 20 53 54 41   Release the STA
34642 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78  TIC_MASTER mutex
34643 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
34644 20 6c 65 61 76 65 4d 75 74 65 78 28 76 6f 69 64   leaveMutex(void
34645 29 7b 0a 20 20 61 73 73 65 72 74 4d 75 74 65 78  ){.  assertMutex
34646 48 65 6c 64 28 29 3b 0a 20 20 63 68 65 63 6b 4c  Held();.  checkL
34647 69 73 74 50 72 6f 70 65 72 74 69 65 73 28 30 29  istProperties(0)
34648 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
34649 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 4d  x_leave(sqlite3M
3464a 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
3464b 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
3464c 53 54 45 52 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  STER));.}../*.**
3464d 20 52 65 67 69 73 74 65 72 20 61 6e 20 75 6e 6c   Register an unl
3464e 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
3464f 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ack..**.** This 
34650 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  is called after 
34651 63 6f 6e 6e 65 63 74 69 6f 6e 20 22 64 62 22 20  connection "db" 
34652 68 61 73 20 61 74 74 65 6d 70 74 65 64 20 73 6f  has attempted so
34653 6d 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a 2a 20  me operation.** 
34654 62 75 74 20 68 61 73 20 72 65 63 65 69 76 65 64  but has received
34655 20 61 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45   an SQLITE_LOCKE
34656 44 20 65 72 72 6f 72 20 62 65 63 61 75 73 65 20  D error because 
34657 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63 74 69  another connecti
34658 6f 6e 0a 2a 2a 20 28 63 61 6c 6c 20 69 74 20 70  on.** (call it p
34659 4f 74 68 65 72 29 20 69 6e 20 74 68 65 20 73 61  Other) in the sa
3465a 6d 65 20 70 72 6f 63 65 73 73 20 77 61 73 20 62  me process was b
3465b 75 73 79 20 75 73 69 6e 67 20 74 68 65 20 73 61  usy using the sa
3465c 6d 65 20 73 68 61 72 65 64 0a 2a 2a 20 63 61 63  me shared.** cac
3465d 68 65 2e 20 20 70 4f 74 68 65 72 20 69 73 20 66  he.  pOther is f
3465e 6f 75 6e 64 20 62 79 20 6c 6f 6f 6b 69 6e 67 20  ound by looking 
3465f 61 74 20 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67  at db->pBlocking
34660 43 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  Connection..**.*
34661 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f  * If there is no
34662 20 62 6c 6f 63 6b 69 6e 67 20 63 6f 6e 6e 65 63   blocking connec
34663 74 69 6f 6e 2c 20 74 68 65 20 63 61 6c 6c 62 61  tion, the callba
34664 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64 20 69 6d  ck is invoked im
34665 6d 65 64 69 61 74 65 6c 79 2c 0a 2a 2a 20 62 65  mediately,.** be
34666 66 6f 72 65 20 74 68 69 73 20 72 6f 75 74 69 6e  fore this routin
34667 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a  e returns..**.**
34668 20 49 66 20 70 4f 74 68 65 72 20 69 73 20 61 6c   If pOther is al
34669 72 65 61 64 79 20 62 6c 6f 63 6b 65 64 20 6f 6e  ready blocked on
3466a 20 64 62 2c 20 74 68 65 6e 20 72 65 70 6f 72 74   db, then report
3466b 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 2c 20   SQLITE_LOCKED, 
3466c 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 61  to indicate.** a
3466d 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a   deadlock..**.**
3466e 20 4f 74 68 65 72 77 69 73 65 2c 20 6d 61 6b 65   Otherwise, make
3466f 20 61 72 72 61 6e 67 65 6d 65 6e 74 73 20 74 6f   arrangements to
34670 20 69 6e 76 6f 6b 65 20 78 4e 6f 74 69 66 79 20   invoke xNotify 
34671 77 68 65 6e 20 70 4f 74 68 65 72 20 64 72 6f 70  when pOther drop
34672 73 0a 2a 2a 20 69 74 73 20 6c 6f 63 6b 73 2e 0a  s.** its locks..
34673 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 61 6c 6c 20  **.** Each call 
34674 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  to this routine 
34675 6f 76 65 72 72 69 64 65 73 20 61 6e 79 20 70 72  overrides any pr
34676 69 6f 72 20 63 61 6c 6c 62 61 63 6b 73 20 72 65  ior callbacks re
34677 67 69 73 74 65 72 65 64 0a 2a 2a 20 6f 6e 20 74  gistered.** on t
34678 68 65 20 73 61 6d 65 20 22 64 62 22 2e 20 20 49  he same "db".  I
34679 66 20 78 4e 6f 74 69 66 79 3d 3d 30 20 74 68 65  f xNotify==0 the
3467a 6e 20 61 6e 79 20 70 72 69 6f 72 20 63 61 6c 6c  n any prior call
3467b 62 61 63 6b 73 20 61 72 65 20 69 6d 6d 65 64 69  backs are immedi
3467c 61 74 65 6c 79 0a 2a 2a 20 63 61 6e 63 65 6c 6c  ately.** cancell
3467d 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ed..*/.SQLITE_AP
3467e 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 75 6e  I int sqlite3_un
3467f 6c 6f 63 6b 5f 6e 6f 74 69 66 79 28 0a 20 20 73  lock_notify(.  s
34680 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 76 6f  qlite3 *db,.  vo
34681 69 64 20 28 2a 78 4e 6f 74 69 66 79 29 28 76 6f  id (*xNotify)(vo
34682 69 64 20 2a 2a 2c 20 69 6e 74 29 2c 0a 20 20 76  id **, int),.  v
34683 6f 69 64 20 2a 70 41 72 67 0a 29 7b 0a 20 20 69  oid *pArg.){.  i
34684 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
34685 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  K;..  sqlite3_mu
34686 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
34687 74 65 78 29 3b 0a 20 20 65 6e 74 65 72 4d 75 74  tex);.  enterMut
34688 65 78 28 29 3b 0a 0a 20 20 69 66 28 20 78 4e 6f  ex();..  if( xNo
34689 74 69 66 79 3d 3d 30 20 29 7b 0a 20 20 20 20 72  tify==0 ){.    r
3468a 65 6d 6f 76 65 46 72 6f 6d 42 6c 6f 63 6b 65 64  emoveFromBlocked
3468b 4c 69 73 74 28 64 62 29 3b 0a 20 20 20 20 64 62  List(db);.    db
3468c 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74  ->pUnlockConnect
3468d 69 6f 6e 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d  ion = 0;.    db-
3468e 3e 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 3d  >xUnlockNotify =
3468f 20 30 3b 0a 20 20 20 20 64 62 2d 3e 70 55 6e 6c   0;.    db->pUnl
34690 6f 63 6b 41 72 67 20 3d 20 30 3b 0a 20 20 7d 65  ockArg = 0;.  }e
34691 6c 73 65 20 69 66 28 20 30 3d 3d 64 62 2d 3e 70  lse if( 0==db->p
34692 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74 69  BlockingConnecti
34693 6f 6e 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  on ){.    /* The
34694 20 62 6c 6f 63 6b 69 6e 67 20 74 72 61 6e 73 61   blocking transa
34695 63 74 69 6f 6e 20 68 61 73 20 62 65 65 6e 20 63  ction has been c
34696 6f 6e 63 6c 75 64 65 64 2e 20 4f 72 20 74 68 65  oncluded. Or the
34697 72 65 20 6e 65 76 65 72 20 77 61 73 20 61 20 0a  re never was a .
34698 20 20 20 20 2a 2a 20 62 6c 6f 63 6b 69 6e 67 20      ** blocking 
34699 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 49 6e 20  transaction. In 
3469a 65 69 74 68 65 72 20 63 61 73 65 2c 20 69 6e 76  either case, inv
3469b 6f 6b 65 20 74 68 65 20 6e 6f 74 69 66 79 20 63  oke the notify c
3469c 61 6c 6c 62 61 63 6b 0a 20 20 20 20 2a 2a 20 69  allback.    ** i
3469d 6d 6d 65 64 69 61 74 65 6c 79 2e 20 0a 20 20 20  mmediately. .   
3469e 20 2a 2f 0a 20 20 20 20 78 4e 6f 74 69 66 79 28   */.    xNotify(
3469f 26 70 41 72 67 2c 20 31 29 3b 0a 20 20 7d 65 6c  &pArg, 1);.  }el
346a0 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20  se{.    sqlite3 
346a1 2a 70 3b 0a 0a 20 20 20 20 66 6f 72 28 70 3d 64  *p;..    for(p=d
346a2 62 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e  b->pBlockingConn
346a3 65 63 74 69 6f 6e 3b 20 70 20 26 26 20 70 21 3d  ection; p && p!=
346a4 64 62 3b 20 70 3d 70 2d 3e 70 55 6e 6c 6f 63 6b  db; p=p->pUnlock
346a5 43 6f 6e 6e 65 63 74 69 6f 6e 29 7b 7d 0a 20 20  Connection){}.  
346a6 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20    if( p ){.     
346a7 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4c 4f 43   rc = SQLITE_LOC
346a8 4b 45 44 3b 20 20 20 20 20 20 20 20 20 20 20 20  KED;            
346a9 20 20 2f 2a 20 44 65 61 64 6c 6f 63 6b 20 64 65    /* Deadlock de
346aa 74 65 63 74 65 64 2e 20 2a 2f 0a 20 20 20 20 7d  tected. */.    }
346ab 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d 3e  else{.      db->
346ac 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f  pUnlockConnectio
346ad 6e 20 3d 20 64 62 2d 3e 70 42 6c 6f 63 6b 69 6e  n = db->pBlockin
346ae 67 43 6f 6e 6e 65 63 74 69 6f 6e 3b 0a 20 20 20  gConnection;.   
346af 20 20 20 64 62 2d 3e 78 55 6e 6c 6f 63 6b 4e 6f     db->xUnlockNo
346b0 74 69 66 79 20 3d 20 78 4e 6f 74 69 66 79 3b 0a  tify = xNotify;.
346b1 20 20 20 20 20 20 64 62 2d 3e 70 55 6e 6c 6f 63        db->pUnloc
346b2 6b 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 20  kArg = pArg;.   
346b3 20 20 20 72 65 6d 6f 76 65 46 72 6f 6d 42 6c 6f     removeFromBlo
346b4 63 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a 20 20  ckedList(db);.  
346b5 20 20 20 20 61 64 64 54 6f 42 6c 6f 63 6b 65 64      addToBlocked
346b6 4c 69 73 74 28 64 62 29 3b 0a 20 20 20 20 7d 0a  List(db);.    }.
346b7 20 20 7d 0a 0a 20 20 6c 65 61 76 65 4d 75 74 65    }..  leaveMute
346b8 78 28 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21  x();.  assert( !
346b9 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
346ba 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 72 72   );.  sqlite3Err
346bb 6f 72 28 64 62 2c 20 72 63 2c 20 28 72 63 3f 22  or(db, rc, (rc?"
346bc 64 61 74 61 62 61 73 65 20 69 73 20 64 65 61 64  database is dead
346bd 6c 6f 63 6b 65 64 22 3a 30 29 29 3b 0a 20 20 73  locked":0));.  s
346be 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
346bf 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
346c0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
346c1 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
346c2 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 77 68 69  on is called whi
346c3 6c 65 20 73 74 65 70 70 69 6e 67 20 6f 72 20 70  le stepping or p
346c4 72 65 70 61 72 69 6e 67 20 61 20 73 74 61 74 65  reparing a state
346c5 6d 65 6e 74 20 0a 2a 2a 20 61 73 73 6f 63 69 61  ment .** associa
346c6 74 65 64 20 77 69 74 68 20 63 6f 6e 6e 65 63 74  ted with connect
346c7 69 6f 6e 20 64 62 2e 20 54 68 65 20 6f 70 65 72  ion db. The oper
346c8 61 74 69 6f 6e 20 77 69 6c 6c 20 72 65 74 75 72  ation will retur
346c9 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 0a  n SQLITE_LOCKED.
346ca 2a 2a 20 74 6f 20 74 68 65 20 75 73 65 72 20 62  ** to the user b
346cb 65 63 61 75 73 65 20 69 74 20 72 65 71 75 69 72  ecause it requir
346cc 65 73 20 61 20 6c 6f 63 6b 20 74 68 61 74 20 77  es a lock that w
346cd 69 6c 6c 20 6e 6f 74 20 62 65 20 61 76 61 69 6c  ill not be avail
346ce 61 62 6c 65 0a 2a 2a 20 75 6e 74 69 6c 20 63 6f  able.** until co
346cf 6e 6e 65 63 74 69 6f 6e 20 70 42 6c 6f 63 6b 65  nnection pBlocke
346d0 72 20 63 6f 6e 63 6c 75 64 65 73 20 69 74 73 20  r concludes its 
346d1 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
346d2 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
346d3 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
346d4 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 42 6c 6f  te3ConnectionBlo
346d5 63 6b 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62  cked(sqlite3 *db
346d6 2c 20 73 71 6c 69 74 65 33 20 2a 70 42 6c 6f 63  , sqlite3 *pBloc
346d7 6b 65 72 29 7b 0a 20 20 65 6e 74 65 72 4d 75 74  ker){.  enterMut
346d8 65 78 28 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e  ex();.  if( db->
346d9 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74  pBlockingConnect
346da 69 6f 6e 3d 3d 30 20 26 26 20 64 62 2d 3e 70 55  ion==0 && db->pU
346db 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d  nlockConnection=
346dc 3d 30 20 29 7b 0a 20 20 20 20 61 64 64 54 6f 42  =0 ){.    addToB
346dd 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62 29 3b 0a  lockedList(db);.
346de 20 20 7d 0a 20 20 64 62 2d 3e 70 42 6c 6f 63 6b    }.  db->pBlock
346df 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20  ingConnection = 
346e0 70 42 6c 6f 63 6b 65 72 3b 0a 20 20 6c 65 61 76  pBlocker;.  leav
346e1 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a  eMutex();.}../*.
346e2 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
346e3 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 0a   is called when.
346e4 2a 2a 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ** the transacti
346e5 6f 6e 20 6f 70 65 6e 65 64 20 62 79 20 64 61 74  on opened by dat
346e6 61 62 61 73 65 20 64 62 20 68 61 73 20 6a 75 73  abase db has jus
346e7 74 20 66 69 6e 69 73 68 65 64 2e 20 4c 6f 63 6b  t finished. Lock
346e8 73 20 68 65 6c 64 20 0a 2a 2a 20 62 79 20 64 61  s held .** by da
346e9 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
346ea 6e 20 64 62 20 68 61 76 65 20 62 65 65 6e 20 72  n db have been r
346eb 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  eleased..**.** T
346ec 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6c 6f 6f  his function loo
346ed 70 73 20 74 68 72 6f 75 67 68 20 65 61 63 68 20  ps through each 
346ee 65 6e 74 72 79 20 69 6e 20 74 68 65 20 62 6c 6f  entry in the blo
346ef 63 6b 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  cked connections
346f0 0a 2a 2a 20 6c 69 73 74 20 61 6e 64 20 64 6f 65  .** list and doe
346f1 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  s the following:
346f2 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 49 66 20 74  .**.**   1) If t
346f3 68 65 20 73 71 6c 69 74 65 33 2e 70 42 6c 6f 63  he sqlite3.pBloc
346f4 6b 69 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 20 6d  kingConnection m
346f5 65 6d 62 65 72 20 6f 66 20 61 20 6c 69 73 74 20  ember of a list 
346f6 65 6e 74 72 79 20 69 73 0a 2a 2a 20 20 20 20 20  entry is.**     
346f7 20 73 65 74 20 74 6f 20 64 62 2c 20 74 68 65 6e   set to db, then
346f8 20 73 65 74 20 70 42 6c 6f 63 6b 69 6e 67 43 6f   set pBlockingCo
346f9 6e 6e 65 63 74 69 6f 6e 3d 30 2e 0a 2a 2a 0a 2a  nnection=0..**.*
346fa 2a 20 20 20 32 29 20 49 66 20 74 68 65 20 73 71  *   2) If the sq
346fb 6c 69 74 65 33 2e 70 55 6e 6c 6f 63 6b 43 6f 6e  lite3.pUnlockCon
346fc 6e 65 63 74 69 6f 6e 20 6d 65 6d 62 65 72 20 6f  nection member o
346fd 66 20 61 20 6c 69 73 74 20 65 6e 74 72 79 20 69  f a list entry i
346fe 73 0a 2a 2a 20 20 20 20 20 20 73 65 74 20 74 6f  s.**      set to
346ff 20 64 62 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65   db, then invoke
34700 20 74 68 65 20 63 6f 6e 66 69 67 75 72 65 64 20   the configured 
34701 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61  unlock-notify ca
34702 6c 6c 62 61 63 6b 20 61 6e 64 0a 2a 2a 20 20 20  llback and.**   
34703 20 20 20 73 65 74 20 70 55 6e 6c 6f 63 6b 43 6f     set pUnlockCo
34704 6e 6e 65 63 74 69 6f 6e 3d 30 2e 0a 2a 2a 0a 2a  nnection=0..**.*
34705 2a 20 20 20 33 29 20 49 66 20 74 68 65 20 74 77  *   3) If the tw
34706 6f 20 73 74 65 70 73 20 61 62 6f 76 65 20 6d 65  o steps above me
34707 61 6e 20 74 68 61 74 20 70 42 6c 6f 63 6b 69 6e  an that pBlockin
34708 67 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30 20 61  gConnection==0 a
34709 6e 64 0a 2a 2a 20 20 20 20 20 20 70 55 6e 6c 6f  nd.**      pUnlo
3470a 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d 30 2c  ckConnection==0,
3470b 20 72 65 6d 6f 76 65 20 74 68 65 20 65 6e 74 72   remove the entr
3470c 79 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b  y from the block
3470d 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 0a 2a  ed connections.*
3470e 2a 20 20 20 20 20 20 6c 69 73 74 2e 0a 2a 2f 0a  *      list..*/.
3470f 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
34710 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6e 6e 65  oid sqlite3Conne
34711 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 73 71  ctionUnlocked(sq
34712 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 76 6f  lite3 *db){.  vo
34713 69 64 20 28 2a 78 55 6e 6c 6f 63 6b 4e 6f 74 69  id (*xUnlockNoti
34714 66 79 29 28 76 6f 69 64 20 2a 2a 2c 20 69 6e 74  fy)(void **, int
34715 29 20 3d 20 30 3b 20 2f 2a 20 55 6e 6c 6f 63 6b  ) = 0; /* Unlock
34716 2d 6e 6f 74 69 66 79 20 63 62 20 74 6f 20 69 6e  -notify cb to in
34717 76 6f 6b 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 41  voke */.  int nA
34718 72 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  rg = 0;         
34719 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3471a 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
3471b 65 6e 74 72 69 65 73 20 69 6e 20 61 41 72 67 5b  entries in aArg[
3471c 5d 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  ] */.  sqlite3 *
3471d 2a 70 70 3b 20 20 20 20 20 20 20 20 20 20 20 20  *pp;            
3471e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3471f 2f 2a 20 49 74 65 72 61 74 6f 72 20 76 61 72 69  /* Iterator vari
34720 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  able */.  void *
34721 2a 61 41 72 67 3b 20 20 20 20 20 20 20 20 20 20  *aArg;          
34722 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
34723 73 20 74 6f 20 74 68 65 20 75 6e 6c 6f 63 6b 20  s to the unlock 
34724 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 76 6f  callback */.  vo
34725 69 64 20 2a 2a 61 44 79 6e 20 3d 20 30 3b 20 20  id **aDyn = 0;  
34726 20 20 20 20 20 20 20 20 20 2f 2a 20 44 79 6e 61           /* Dyna
34727 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  mically allocate
34728 64 20 73 70 61 63 65 20 66 6f 72 20 61 41 72 67  d space for aArg
34729 5b 5d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 61 53  [] */.  void *aS
3472a 74 61 74 69 63 5b 31 36 5d 3b 20 20 20 20 20 20  tatic[16];      
3472b 20 20 20 2f 2a 20 53 74 61 72 74 65 72 20 73 70     /* Starter sp
3472c 61 63 65 20 66 6f 72 20 61 41 72 67 5b 5d 2e 20  ace for aArg[]. 
3472d 20 4e 6f 20 6d 61 6c 6c 6f 63 20 72 65 71 75 69   No malloc requi
3472e 72 65 64 20 2a 2f 0a 0a 20 20 61 41 72 67 20 3d  red */..  aArg =
3472f 20 61 53 74 61 74 69 63 3b 0a 20 20 65 6e 74 65   aStatic;.  ente
34730 72 4d 75 74 65 78 28 29 3b 20 20 20 20 20 20 20  rMutex();       
34731 20 20 2f 2a 20 45 6e 74 65 72 20 53 54 41 54 49    /* Enter STATI
34732 43 5f 4d 41 53 54 45 52 20 6d 75 74 65 78 20 2a  C_MASTER mutex *
34733 2f 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f  /..  /* This loo
34734 70 20 72 75 6e 73 20 6f 6e 63 65 20 66 6f 72 20  p runs once for 
34735 65 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74 68  each entry in th
34736 65 20 62 6c 6f 63 6b 65 64 2d 63 6f 6e 6e 65 63  e blocked-connec
34737 74 69 6f 6e 73 20 6c 69 73 74 2e 20 2a 2f 0a 20  tions list. */. 
34738 20 66 6f 72 28 70 70 3d 26 73 71 6c 69 74 65 33   for(pp=&sqlite3
34739 42 6c 6f 63 6b 65 64 4c 69 73 74 3b 20 2a 70 70  BlockedList; *pp
3473a 3b 20 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 20 29 7b  ; /* no-op */ ){
3473b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 70 20  .    sqlite3 *p 
3473c 3d 20 2a 70 70 3b 0a 0a 20 20 20 20 2f 2a 20 53  = *pp;..    /* S
3473d 74 65 70 20 31 2e 20 2a 2f 0a 20 20 20 20 69 66  tep 1. */.    if
3473e 28 20 70 2d 3e 70 42 6c 6f 63 6b 69 6e 67 43 6f  ( p->pBlockingCo
3473f 6e 6e 65 63 74 69 6f 6e 3d 3d 64 62 20 29 7b 0a  nnection==db ){.
34740 20 20 20 20 20 20 70 2d 3e 70 42 6c 6f 63 6b 69        p->pBlocki
34741 6e 67 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 30  ngConnection = 0
34742 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
34743 53 74 65 70 20 32 2e 20 2a 2f 0a 20 20 20 20 69  Step 2. */.    i
34744 66 28 20 70 2d 3e 70 55 6e 6c 6f 63 6b 43 6f 6e  f( p->pUnlockCon
34745 6e 65 63 74 69 6f 6e 3d 3d 64 62 20 29 7b 0a 20  nection==db ){. 
34746 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
34747 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 29 3b  xUnlockNotify );
34748 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 78 55  .      if( p->xU
34749 6e 6c 6f 63 6b 4e 6f 74 69 66 79 21 3d 78 55 6e  nlockNotify!=xUn
3474a 6c 6f 63 6b 4e 6f 74 69 66 79 20 26 26 20 6e 41  lockNotify && nA
3474b 72 67 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  rg!=0 ){.       
3474c 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 28 61   xUnlockNotify(a
3474d 41 72 67 2c 20 6e 41 72 67 29 3b 0a 20 20 20 20  Arg, nArg);.    
3474e 20 20 20 20 6e 41 72 67 20 3d 20 30 3b 0a 20 20      nArg = 0;.  
3474f 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 73 71 6c      }..      sql
34750 69 74 65 33 42 65 67 69 6e 42 65 6e 69 67 6e 4d  ite3BeginBenignM
34751 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20 20 61  alloc();.      a
34752 73 73 65 72 74 28 20 61 41 72 67 3d 3d 61 44 79  ssert( aArg==aDy
34753 6e 20 7c 7c 20 28 61 44 79 6e 3d 3d 30 20 26 26  n || (aDyn==0 &&
34754 20 61 41 72 67 3d 3d 61 53 74 61 74 69 63 29 20   aArg==aStatic) 
34755 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
34756 20 6e 41 72 67 3c 3d 28 69 6e 74 29 41 72 72 61   nArg<=(int)Arra
34757 79 53 69 7a 65 28 61 53 74 61 74 69 63 29 20 7c  ySize(aStatic) |
34758 7c 20 61 41 72 67 3d 3d 61 44 79 6e 20 29 3b 0a  | aArg==aDyn );.
34759 20 20 20 20 20 20 69 66 28 20 28 21 61 44 79 6e        if( (!aDyn
3475a 20 26 26 20 6e 41 72 67 3d 3d 28 69 6e 74 29 41   && nArg==(int)A
3475b 72 72 61 79 53 69 7a 65 28 61 53 74 61 74 69 63  rraySize(aStatic
3475c 29 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 61 44  )).       || (aD
3475d 79 6e 20 26 26 20 6e 41 72 67 3d 3d 28 69 6e 74  yn && nArg==(int
3475e 29 28 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  )(sqlite3DbMallo
3475f 63 53 69 7a 65 28 64 62 2c 20 61 44 79 6e 29 2f  cSize(db, aDyn)/
34760 73 69 7a 65 6f 66 28 76 6f 69 64 2a 29 29 29 0a  sizeof(void*))).
34761 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
34762 20 2f 2a 20 54 68 65 20 61 41 72 67 5b 5d 20 61   /* The aArg[] a
34763 72 72 61 79 20 6e 65 65 64 73 20 74 6f 20 67 72  rray needs to gr
34764 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 76  ow. */.        v
34765 6f 69 64 20 2a 2a 70 4e 65 77 20 3d 20 28 76 6f  oid **pNew = (vo
34766 69 64 20 2a 2a 29 73 71 6c 69 74 65 33 4d 61 6c  id **)sqlite3Mal
34767 6c 6f 63 28 6e 41 72 67 2a 73 69 7a 65 6f 66 28  loc(nArg*sizeof(
34768 76 6f 69 64 20 2a 29 2a 32 29 3b 0a 20 20 20 20  void *)*2);.    
34769 20 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a      if( pNew ){.
3476a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79            memcpy
3476b 28 70 4e 65 77 2c 20 61 41 72 67 2c 20 6e 41 72  (pNew, aArg, nAr
3476c 67 2a 73 69 7a 65 6f 66 28 76 6f 69 64 20 2a 29  g*sizeof(void *)
3476d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
3476e 69 74 65 33 5f 66 72 65 65 28 61 44 79 6e 29 3b  ite3_free(aDyn);
3476f 0a 20 20 20 20 20 20 20 20 20 20 61 44 79 6e 20  .          aDyn 
34770 3d 20 61 41 72 67 20 3d 20 70 4e 65 77 3b 0a 20  = aArg = pNew;. 
34771 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
34772 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
34773 6f 63 63 75 72 73 20 77 68 65 6e 20 74 68 65 20  occurs when the 
34774 61 72 72 61 79 20 6f 66 20 63 6f 6e 74 65 78 74  array of context
34775 20 70 6f 69 6e 74 65 72 73 20 74 68 61 74 20 6e   pointers that n
34776 65 65 64 20 74 6f 0a 20 20 20 20 20 20 20 20 20  eed to.         
34777 20 2a 2a 20 62 65 20 70 61 73 73 65 64 20 74 6f   ** be passed to
34778 20 74 68 65 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69   the unlock-noti
34779 66 79 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6c  fy callback is l
3477a 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 0a 20  arger than the. 
3477b 20 20 20 20 20 20 20 20 20 2a 2a 20 61 53 74 61           ** aSta
3477c 74 69 63 5b 5d 20 61 72 72 61 79 20 61 6c 6c 6f  tic[] array allo
3477d 63 61 74 65 64 20 6f 6e 20 74 68 65 20 73 74 61  cated on the sta
3477e 63 6b 20 61 6e 64 20 74 68 65 20 61 74 74 65 6d  ck and the attem
3477f 70 74 20 74 6f 20 0a 20 20 20 20 20 20 20 20 20  pt to .         
34780 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 20 61 20 6c   ** allocate a l
34781 61 72 67 65 72 20 61 72 72 61 79 20 66 72 6f 6d  arger array from
34782 20 74 68 65 20 68 65 61 70 20 68 61 73 20 66 61   the heap has fa
34783 69 6c 65 64 2e 0a 20 20 20 20 20 20 20 20 20 20  iled..          
34784 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  **.          ** 
34785 54 68 69 73 20 69 73 20 61 20 64 69 66 66 69 63  This is a diffic
34786 75 6c 74 20 73 69 74 75 61 74 69 6f 6e 20 74 6f  ult situation to
34787 20 68 61 6e 64 6c 65 2e 20 52 65 74 75 72 6e 69   handle. Returni
34788 6e 67 20 61 6e 20 65 72 72 6f 72 0a 20 20 20 20  ng an error.    
34789 20 20 20 20 20 20 2a 2a 20 63 6f 64 65 20 74 6f        ** code to
3478a 20 74 68 65 20 63 61 6c 6c 65 72 20 69 73 20 69   the caller is i
3478b 6e 73 75 66 66 69 63 69 65 6e 74 2c 20 61 73 20  nsufficient, as 
3478c 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f 72  even if an error
3478d 20 63 6f 64 65 0a 20 20 20 20 20 20 20 20 20 20   code.          
3478e 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 74  ** is returned t
3478f 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f  he transaction o
34790 6e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 64 62 20  n connection db 
34791 77 69 6c 6c 20 73 74 69 6c 6c 20 62 65 0a 20 20  will still be.  
34792 20 20 20 20 20 20 20 20 2a 2a 20 63 6c 6f 73 65          ** close
34793 64 20 61 6e 64 20 74 68 65 20 75 6e 6c 6f 63 6b  d and the unlock
34794 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62 61 63 6b  -notify callback
34795 73 20 6f 6e 20 62 6c 6f 63 6b 65 64 20 63 6f 6e  s on blocked con
34796 6e 65 63 74 69 6f 6e 73 0a 20 20 20 20 20 20 20  nections.       
34797 20 20 20 2a 2a 20 77 69 6c 6c 20 67 6f 20 75 6e     ** will go un
34798 69 73 73 75 65 64 2e 20 54 68 69 73 20 6d 69 67  issued. This mig
34799 68 74 20 63 61 75 73 65 20 74 68 65 20 61 70 70  ht cause the app
3479a 6c 69 63 61 74 69 6f 6e 20 74 6f 20 77 61 69 74  lication to wait
3479b 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 6e  .          ** in
3479c 64 65 66 69 6e 69 74 65 6c 79 20 66 6f 72 20 61  definitely for a
3479d 6e 20 75 6e 6c 6f 63 6b 2d 6e 6f 74 69 66 79 20  n unlock-notify 
3479e 63 61 6c 6c 62 61 63 6b 20 74 68 61 74 20 77 69  callback that wi
3479f 6c 6c 20 6e 65 76 65 72 20 0a 20 20 20 20 20 20  ll never .      
347a0 20 20 20 20 2a 2a 20 61 72 72 69 76 65 2e 0a 20      ** arrive.. 
347a1 20 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20           **.    
347a2 20 20 20 20 20 20 2a 2a 20 49 6e 73 74 65 61 64        ** Instead
347a3 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 75 6e 6c  , invoke the unl
347a4 6f 63 6b 2d 6e 6f 74 69 66 79 20 63 61 6c 6c 62  ock-notify callb
347a5 61 63 6b 20 77 69 74 68 20 74 68 65 20 63 6f 6e  ack with the con
347a6 74 65 78 74 0a 20 20 20 20 20 20 20 20 20 20 2a  text.          *
347a7 2a 20 61 72 72 61 79 20 61 6c 72 65 61 64 79 20  * array already 
347a8 61 63 63 75 6d 75 6c 61 74 65 64 2e 20 57 65 20  accumulated. We 
347a9 63 61 6e 20 74 68 65 6e 20 63 6c 65 61 72 20 74  can then clear t
347aa 68 65 20 61 72 72 61 79 20 61 6e 64 0a 20 20 20  he array and.   
347ab 20 20 20 20 20 20 20 2a 2a 20 62 65 67 69 6e 20         ** begin 
347ac 61 63 63 75 6d 75 6c 61 74 69 6e 67 20 61 6e 79  accumulating any
347ad 20 66 75 72 74 68 65 72 20 63 6f 6e 74 65 78 74   further context
347ae 20 70 6f 69 6e 74 65 72 73 20 77 69 74 68 6f 75   pointers withou
347af 74 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  t .          ** 
347b0 72 65 71 75 69 72 69 6e 67 20 61 6e 79 20 64 79  requiring any dy
347b1 6e 61 6d 69 63 20 61 6c 6c 6f 63 61 74 69 6f 6e  namic allocation
347b2 2e 20 54 68 69 73 20 69 73 20 73 75 62 2d 6f 70  . This is sub-op
347b3 74 69 6d 61 6c 20 62 65 63 61 75 73 65 0a 20 20  timal because.  
347b4 20 20 20 20 20 20 20 20 2a 2a 20 69 74 20 6d 65          ** it me
347b5 61 6e 73 20 74 68 61 74 20 69 6e 73 74 65 61 64  ans that instead
347b6 20 6f 66 20 6f 6e 65 20 63 61 6c 6c 62 61 63 6b   of one callback
347b7 20 77 69 74 68 20 61 20 6c 61 72 67 65 20 61 72   with a large ar
347b8 72 61 79 20 6f 66 0a 20 20 20 20 20 20 20 20 20  ray of.         
347b9 20 2a 2a 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e   ** context poin
347ba 74 65 72 73 20 74 68 65 20 61 70 70 6c 69 63 61  ters the applica
347bb 74 69 6f 6e 20 77 69 6c 6c 20 72 65 63 65 69 76  tion will receiv
347bc 65 20 74 77 6f 20 6f 72 20 6d 6f 72 65 0a 20 20  e two or more.  
347bd 20 20 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 62          ** callb
347be 61 63 6b 73 20 77 69 74 68 20 73 6d 61 6c 6c 65  acks with smalle
347bf 72 20 61 72 72 61 79 73 20 6f 66 20 63 6f 6e 74  r arrays of cont
347c0 65 78 74 20 70 6f 69 6e 74 65 72 73 2c 20 77 68  ext pointers, wh
347c1 69 63 68 20 77 69 6c 6c 0a 20 20 20 20 20 20 20  ich will.       
347c2 20 20 20 2a 2a 20 72 65 64 75 63 65 20 74 68 65     ** reduce the
347c3 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 61 62   applications ab
347c4 69 6c 69 74 79 20 74 6f 20 70 72 69 6f 72 69 74  ility to priorit
347c5 69 7a 65 20 6d 75 6c 74 69 70 6c 65 20 0a 20 20  ize multiple .  
347c6 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 6e 65          ** conne
347c7 63 74 69 6f 6e 73 2e 20 42 75 74 20 69 74 20 69  ctions. But it i
347c8 73 20 74 68 65 20 62 65 73 74 20 74 68 61 74 20  s the best that 
347c9 63 61 6e 20 62 65 20 64 6f 6e 65 20 75 6e 64 65  can be done unde
347ca 72 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20  r the.          
347cb 2a 2a 20 63 69 72 63 75 6d 73 74 61 6e 63 65 73  ** circumstances
347cc 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
347cd 20 20 20 20 20 20 20 20 20 78 55 6e 6c 6f 63 6b           xUnlock
347ce 4e 6f 74 69 66 79 28 61 41 72 67 2c 20 6e 41 72  Notify(aArg, nAr
347cf 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 41  g);.          nA
347d0 72 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  rg = 0;.        
347d1 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
347d2 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e  sqlite3EndBenign
347d3 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 20 20 20  Malloc();..     
347d4 20 61 41 72 67 5b 6e 41 72 67 2b 2b 5d 20 3d 20   aArg[nArg++] = 
347d5 70 2d 3e 70 55 6e 6c 6f 63 6b 41 72 67 3b 0a 20  p->pUnlockArg;. 
347d6 20 20 20 20 20 78 55 6e 6c 6f 63 6b 4e 6f 74 69       xUnlockNoti
347d7 66 79 20 3d 20 70 2d 3e 78 55 6e 6c 6f 63 6b 4e  fy = p->xUnlockN
347d8 6f 74 69 66 79 3b 0a 20 20 20 20 20 20 70 2d 3e  otify;.      p->
347d9 70 55 6e 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f  pUnlockConnectio
347da 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e  n = 0;.      p->
347db 78 55 6e 6c 6f 63 6b 4e 6f 74 69 66 79 20 3d 20  xUnlockNotify = 
347dc 30 3b 0a 20 20 20 20 20 20 70 2d 3e 70 55 6e 6c  0;.      p->pUnl
347dd 6f 63 6b 41 72 67 20 3d 20 30 3b 0a 20 20 20 20  ockArg = 0;.    
347de 7d 0a 0a 20 20 20 20 2f 2a 20 53 74 65 70 20 33  }..    /* Step 3
347df 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e  . */.    if( p->
347e0 70 42 6c 6f 63 6b 69 6e 67 43 6f 6e 6e 65 63 74  pBlockingConnect
347e1 69 6f 6e 3d 3d 30 20 26 26 20 70 2d 3e 70 55 6e  ion==0 && p->pUn
347e2 6c 6f 63 6b 43 6f 6e 6e 65 63 74 69 6f 6e 3d 3d  lockConnection==
347e3 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 52 65  0 ){.      /* Re
347e4 6d 6f 76 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  move connection 
347e5 70 20 66 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b  p from the block
347e6 65 64 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6c  ed connections l
347e7 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20 2a 70  ist. */.      *p
347e8 70 20 3d 20 70 2d 3e 70 4e 65 78 74 42 6c 6f 63  p = p->pNextBloc
347e9 6b 65 64 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4e  ked;.      p->pN
347ea 65 78 74 42 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a  extBlocked = 0;.
347eb 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
347ec 20 70 70 20 3d 20 26 70 2d 3e 70 4e 65 78 74 42   pp = &p->pNextB
347ed 6c 6f 63 6b 65 64 3b 0a 20 20 20 20 7d 0a 20 20  locked;.    }.  
347ee 7d 0a 0a 20 20 69 66 28 20 6e 41 72 67 21 3d 30  }..  if( nArg!=0
347ef 20 29 7b 0a 20 20 20 20 78 55 6e 6c 6f 63 6b 4e   ){.    xUnlockN
347f0 6f 74 69 66 79 28 61 41 72 67 2c 20 6e 41 72 67  otify(aArg, nArg
347f1 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
347f2 5f 66 72 65 65 28 61 44 79 6e 29 3b 0a 20 20 6c  _free(aDyn);.  l
347f3 65 61 76 65 4d 75 74 65 78 28 29 3b 20 20 20 20  eaveMutex();    
347f4 20 20 20 20 20 2f 2a 20 4c 65 61 76 65 20 53 54       /* Leave ST
347f5 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75 74 65  ATIC_MASTER mute
347f6 78 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  x */.}../*.** Th
347f7 69 73 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65  is is called whe
347f8 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 63  n the database c
347f9 6f 6e 6e 65 63 74 69 6f 6e 20 70 61 73 73 65 64  onnection passed
347fa 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74 20   as an argument 
347fb 69 73 20 0a 2a 2a 20 62 65 69 6e 67 20 63 6c 6f  is .** being clo
347fc 73 65 64 2e 20 54 68 65 20 63 6f 6e 6e 65 63 74  sed. The connect
347fd 69 6f 6e 20 69 73 20 72 65 6d 6f 76 65 64 20 66  ion is removed f
347fe 72 6f 6d 20 74 68 65 20 62 6c 6f 63 6b 65 64 20  rom the blocked 
347ff 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  list..*/.SQLITE_
34800 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
34801 69 74 65 33 43 6f 6e 6e 65 63 74 69 6f 6e 43 6c  ite3ConnectionCl
34802 6f 73 65 64 28 73 71 6c 69 74 65 33 20 2a 64 62  osed(sqlite3 *db
34803 29 7b 0a 20 20 73 71 6c 69 74 65 33 43 6f 6e 6e  ){.  sqlite3Conn
34804 65 63 74 69 6f 6e 55 6e 6c 6f 63 6b 65 64 28 64  ectionUnlocked(d
34805 62 29 3b 0a 20 20 65 6e 74 65 72 4d 75 74 65 78  b);.  enterMutex
34806 28 29 3b 0a 20 20 72 65 6d 6f 76 65 46 72 6f 6d  ();.  removeFrom
34807 42 6c 6f 63 6b 65 64 4c 69 73 74 28 64 62 29 3b  BlockedList(db);
34808 0a 20 20 63 68 65 63 6b 4c 69 73 74 50 72 6f 70  .  checkListProp
34809 65 72 74 69 65 73 28 64 62 29 3b 0a 20 20 6c 65  erties(db);.  le
3480a 61 76 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 23 65  aveMutex();.}.#e
3480b 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
3480c 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6e 6f 74  ***** End of not
3480d 69 66 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ify.c **********
3480e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3480f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34810 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
34811 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
34812 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   fts3.c ********
34813 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34814 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34815 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36  ****/./*.** 2006
34816 20 4f 63 74 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68   Oct 10.**.** Th
34817 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
34818 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
34819 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
3481a 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
3481b 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
3481c 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
3481d 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
3481e 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
3481f 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
34820 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
34821 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
34822 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
34823 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
34824 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
34825 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
34826 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
34827 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
34828 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34829 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3482a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3482b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3482c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
3482d 2a 20 54 68 69 73 20 69 73 20 61 6e 20 53 51 4c  * This is an SQL
3482e 69 74 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65  ite module imple
3482f 6d 65 6e 74 69 6e 67 20 66 75 6c 6c 2d 74 65 78  menting full-tex
34830 74 20 73 65 61 72 63 68 2e 0a 2a 2f 0a 0a 2f 2a  t search..*/../*
34831 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
34832 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c  this file is onl
34833 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a  y compiled if:.*
34834 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46  *.**     * The F
34835 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65  TS3 module is be
34836 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61 6e 20  ing built as an 
34837 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20  extension.**    
34838 20 20 20 28 69 6e 20 77 68 69 63 68 20 63 61 73     (in which cas
34839 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 69 73  e SQLITE_CORE is
3483a 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 6f   not defined), o
3483b 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68  r.**.**     * Th
3483c 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73  e FTS3 module is
3483d 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69 6e 74   being built int
3483e 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a  o the core of.**
3483f 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 28 69         SQLite (i
34840 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c  n which case SQL
34841 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20  ITE_ENABLE_FTS3 
34842 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a  is defined)..*/.
34843 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
34844 43 6f 6e 73 69 64 65 72 20 65 78 70 6f 72 74 69  Consider exporti
34845 6e 67 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 20  ng this comment 
34846 74 6f 20 61 6e 20 48 54 4d 4c 20 66 69 6c 65 20  to an HTML file 
34847 6f 72 20 74 68 65 0a 2a 2a 20 77 69 6b 69 2e 0a  or the.** wiki..
34848 2a 2f 0a 2f 2a 20 54 68 65 20 66 75 6c 6c 2d 74  */./* The full-t
34849 65 78 74 20 69 6e 64 65 78 20 69 73 20 73 74 6f  ext index is sto
3484a 72 65 64 20 69 6e 20 61 20 73 65 72 69 65 73 20  red in a series 
3484b 6f 66 20 62 2b 74 72 65 65 20 28 2d 6c 69 6b 65  of b+tree (-like
3484c 29 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20  ).** structures 
3484d 63 61 6c 6c 65 64 20 73 65 67 6d 65 6e 74 73 20  called segments 
3484e 77 68 69 63 68 20 6d 61 70 20 74 65 72 6d 73 20  which map terms 
3484f 74 6f 20 64 6f 63 6c 69 73 74 73 2e 20 20 54 68  to doclists.  Th
34850 65 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20  e.** structures 
34851 61 72 65 20 6c 69 6b 65 20 62 2b 74 72 65 65 73  are like b+trees
34852 20 69 6e 20 6c 61 79 6f 75 74 2c 20 62 75 74 20   in layout, but 
34853 61 72 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20  are constructed 
34854 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 62 6f 74 74  from the.** bott
34855 6f 6d 20 75 70 20 69 6e 20 6f 70 74 69 6d 61 6c  om up in optimal
34856 20 66 61 73 68 69 6f 6e 20 61 6e 64 20 61 72 65   fashion and are
34857 20 6e 6f 74 20 75 70 64 61 74 61 62 6c 65 2e 20   not updatable. 
34858 20 53 69 6e 63 65 20 74 72 65 65 73 0a 2a 2a 20   Since trees.** 
34859 61 72 65 20 62 75 69 6c 74 20 66 72 6f 6d 20 74  are built from t
3485a 68 65 20 62 6f 74 74 6f 6d 20 75 70 2c 20 74 68  he bottom up, th
3485b 69 6e 67 73 20 77 69 6c 6c 20 62 65 20 64 65 73  ings will be des
3485c 63 72 69 62 65 64 20 66 72 6f 6d 20 74 68 65 0a  cribed from the.
3485d 2a 2a 20 62 6f 74 74 6f 6d 20 75 70 2e 0a 2a 2a  ** bottom up..**
3485e 0a 2a 2a 0a 2a 2a 2a 2a 20 56 61 72 69 6e 74 73  .**.**** Varints
3485f 20 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73   ****.** The bas
34860 69 63 20 75 6e 69 74 20 6f 66 20 65 6e 63 6f 64  ic unit of encod
34861 69 6e 67 20 69 73 20 61 20 76 61 72 69 61 62 6c  ing is a variabl
34862 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
34863 20 63 61 6c 6c 65 64 20 61 0a 2a 2a 20 76 61 72   called a.** var
34864 69 6e 74 2e 20 20 57 65 20 65 6e 63 6f 64 65 20  int.  We encode 
34865 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20  variable-length 
34866 69 6e 74 65 67 65 72 73 20 69 6e 20 6c 69 74 74  integers in litt
34867 6c 65 2d 65 6e 64 69 61 6e 20 6f 72 64 65 72 0a  le-endian order.
34868 2a 2a 20 75 73 69 6e 67 20 73 65 76 65 6e 20 62  ** using seven b
34869 69 74 73 20 2a 20 70 65 72 20 62 79 74 65 20 61  its * per byte a
3486a 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
3486b 20 4b 45 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20   KEY:.**        
3486c 20 41 20 3d 20 30 78 78 78 78 78 78 78 20 20 20   A = 0xxxxxxx   
3486d 20 37 20 62 69 74 73 20 6f 66 20 64 61 74 61 20   7 bits of data 
3486e 61 6e 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74  and one flag bit
3486f 0a 2a 2a 20 20 20 20 20 20 20 20 20 42 20 3d 20  .**         B = 
34870 31 78 78 78 78 78 78 78 20 20 20 20 37 20 62 69  1xxxxxxx    7 bi
34871 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f  ts of data and o
34872 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a 0a 2a  ne flag bit.**.*
34873 2a 20 20 37 20 62 69 74 73 20 2d 20 41 0a 2a 2a  *  7 bits - A.**
34874 20 31 34 20 62 69 74 73 20 2d 20 42 41 0a 2a 2a   14 bits - BA.**
34875 20 32 31 20 62 69 74 73 20 2d 20 42 42 41 0a 2a   21 bits - BBA.*
34876 2a 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a  * and so on..**.
34877 2a 2a 20 54 68 69 73 20 69 73 20 69 64 65 6e 74  ** This is ident
34878 69 63 61 6c 20 74 6f 20 68 6f 77 20 73 71 6c 69  ical to how sqli
34879 74 65 20 65 6e 63 6f 64 65 73 20 76 61 72 69 6e  te encodes varin
3487a 74 73 20 28 73 65 65 20 75 74 69 6c 2e 63 29 2e  ts (see util.c).
3487b 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 44 6f 63 75  .**.**.**** Docu
3487c 6d 65 6e 74 20 6c 69 73 74 73 20 2a 2a 2a 2a 0a  ment lists ****.
3487d 2a 2a 20 41 20 64 6f 63 6c 69 73 74 20 28 64 6f  ** A doclist (do
3487e 63 75 6d 65 6e 74 20 6c 69 73 74 29 20 68 6f 6c  cument list) hol
3487f 64 73 20 61 20 64 6f 63 69 64 2d 73 6f 72 74 65  ds a docid-sorte
34880 64 20 6c 69 73 74 20 6f 66 20 68 69 74 73 20 66  d list of hits f
34881 6f 72 20 61 0a 2a 2a 20 67 69 76 65 6e 20 74 65  or a.** given te
34882 72 6d 2e 20 20 44 6f 63 6c 69 73 74 73 20 68 6f  rm.  Doclists ho
34883 6c 64 20 64 6f 63 69 64 73 2c 20 61 6e 64 20 63  ld docids, and c
34884 61 6e 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 61 73  an optionally as
34885 73 6f 63 69 61 74 65 0a 2a 2a 20 74 6f 6b 65 6e  sociate.** token
34886 20 70 6f 73 69 74 69 6f 6e 73 20 61 6e 64 20 6f   positions and o
34887 66 66 73 65 74 73 20 77 69 74 68 20 64 6f 63 69  ffsets with doci
34888 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 44 4c 5f 50  ds..**.** A DL_P
34889 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53  OSITIONS_OFFSETS
3488a 20 64 6f 63 6c 69 73 74 20 69 73 20 73 74 6f 72   doclist is stor
3488b 65 64 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a  ed like this:.**
3488c 0a 2a 2a 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20  .** array {.**  
3488d 20 76 61 72 69 6e 74 20 64 6f 63 69 64 3b 0a 2a   varint docid;.*
3488e 2a 20 20 20 61 72 72 61 79 20 7b 20 20 20 20 20  *   array {     
3488f 20 20 20 20 20 20 20 20 20 20 20 28 70 6f 73 69             (posi
34890 74 69 6f 6e 20 6c 69 73 74 20 66 6f 72 20 63 6f  tion list for co
34891 6c 75 6d 6e 20 30 29 0a 2a 2a 20 20 20 20 20 76  lumn 0).**     v
34892 61 72 69 6e 74 20 70 6f 73 69 74 69 6f 6e 3b 20  arint position; 
34893 20 20 20 20 28 64 65 6c 74 61 20 66 72 6f 6d 20      (delta from 
34894 70 72 65 76 69 6f 75 73 20 70 6f 73 69 74 69 6f  previous positio
34895 6e 20 70 6c 75 73 20 50 4f 53 5f 42 41 53 45 29  n plus POS_BASE)
34896 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20 73  .**     varint s
34897 74 61 72 74 4f 66 66 73 65 74 3b 20 20 28 64 65  tartOffset;  (de
34898 6c 74 61 20 66 72 6f 6d 20 70 72 65 76 69 6f 75  lta from previou
34899 73 20 73 74 61 72 74 4f 66 66 73 65 74 29 0a 2a  s startOffset).*
3489a 2a 20 20 20 20 20 76 61 72 69 6e 74 20 65 6e 64  *     varint end
3489b 4f 66 66 73 65 74 3b 20 20 20 20 28 64 65 6c 74  Offset;    (delt
3489c 61 20 66 72 6f 6d 20 73 74 61 72 74 4f 66 66 73  a from startOffs
3489d 65 74 29 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 20 20  et).**   }.**   
3489e 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20 20 76  array {.**     v
3489f 61 72 69 6e 74 20 50 4f 53 5f 43 4f 4c 55 4d 4e  arint POS_COLUMN
348a0 3b 20 20 20 28 6d 61 72 6b 73 20 73 74 61 72 74  ;   (marks start
348a1 20 6f 66 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73   of position lis
348a2 74 20 66 6f 72 20 6e 65 77 20 63 6f 6c 75 6d 6e  t for new column
348a3 29 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20  ).**     varint 
348a4 63 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 28 69  column;       (i
348a5 6e 64 65 78 20 6f 66 20 6e 65 77 20 63 6f 6c 75  ndex of new colu
348a6 6d 6e 29 0a 2a 2a 20 20 20 20 20 61 72 72 61 79  mn).**     array
348a7 20 7b 0a 2a 2a 20 20 20 20 20 20 20 76 61 72 69   {.**       vari
348a8 6e 74 20 70 6f 73 69 74 69 6f 6e 3b 20 20 20 28  nt position;   (
348a9 64 65 6c 74 61 20 66 72 6f 6d 20 70 72 65 76 69  delta from previ
348aa 6f 75 73 20 70 6f 73 69 74 69 6f 6e 20 70 6c 75  ous position plu
348ab 73 20 50 4f 53 5f 42 41 53 45 29 0a 2a 2a 20 20  s POS_BASE).**  
348ac 20 20 20 20 20 76 61 72 69 6e 74 20 73 74 61 72       varint star
348ad 74 4f 66 66 73 65 74 3b 28 64 65 6c 74 61 20 66  tOffset;(delta f
348ae 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 73 74 61  rom previous sta
348af 72 74 4f 66 66 73 65 74 29 0a 2a 2a 20 20 20 20  rtOffset).**    
348b0 20 20 20 76 61 72 69 6e 74 20 65 6e 64 4f 66 66     varint endOff
348b1 73 65 74 3b 20 20 28 64 65 6c 74 61 20 66 72 6f  set;  (delta fro
348b2 6d 20 73 74 61 72 74 4f 66 66 73 65 74 29 0a 2a  m startOffset).*
348b3 2a 20 20 20 20 20 7d 0a 2a 2a 20 20 20 7d 0a 2a  *     }.**   }.*
348b4 2a 20 20 20 76 61 72 69 6e 74 20 50 4f 53 5f 45  *   varint POS_E
348b5 4e 44 3b 20 20 20 20 20 20 20 20 28 6d 61 72 6b  ND;        (mark
348b6 73 20 65 6e 64 20 6f 66 20 70 6f 73 69 74 69 6f  s end of positio
348b7 6e 73 20 66 6f 72 20 74 68 69 73 20 64 6f 63 75  ns for this docu
348b8 6d 65 6e 74 2e 0a 2a 2a 20 7d 0a 2a 2a 0a 2a 2a  ment..** }.**.**
348b9 20 48 65 72 65 2c 20 61 72 72 61 79 20 7b 20 58   Here, array { X
348ba 20 7d 20 6d 65 61 6e 73 20 7a 65 72 6f 20 6f 72   } means zero or
348bb 20 6d 6f 72 65 20 6f 63 63 75 72 72 65 6e 63 65   more occurrence
348bc 73 20 6f 66 20 58 2c 20 61 64 6a 61 63 65 6e 74  s of X, adjacent
348bd 20 69 6e 0a 2a 2a 20 6d 65 6d 6f 72 79 2e 20 20   in.** memory.  
348be 41 20 22 70 6f 73 69 74 69 6f 6e 22 20 69 73 20  A "position" is 
348bf 61 6e 20 69 6e 64 65 78 20 6f 66 20 61 20 74 6f  an index of a to
348c0 6b 65 6e 20 69 6e 20 74 68 65 20 74 6f 6b 65 6e  ken in the token
348c1 20 73 74 72 65 61 6d 0a 2a 2a 20 67 65 6e 65 72   stream.** gener
348c2 61 74 65 64 20 62 79 20 74 68 65 20 74 6f 6b 65  ated by the toke
348c3 6e 69 7a 65 72 2c 20 77 68 69 6c 65 20 61 6e 20  nizer, while an 
348c4 22 6f 66 66 73 65 74 22 20 69 73 20 61 20 62 79  "offset" is a by
348c5 74 65 20 6f 66 66 73 65 74 2c 0a 2a 2a 20 62 6f  te offset,.** bo
348c6 74 68 20 62 61 73 65 64 20 61 74 20 30 2e 20 20  th based at 0.  
348c7 4e 6f 74 65 20 74 68 61 74 20 50 4f 53 5f 45 4e  Note that POS_EN
348c8 44 20 61 6e 64 20 50 4f 53 5f 43 4f 4c 55 4d 4e  D and POS_COLUMN
348c9 20 6f 63 63 75 72 20 69 6e 20 74 68 65 0a 2a 2a   occur in the.**
348ca 20 73 61 6d 65 20 6c 6f 67 69 63 61 6c 20 70 6c   same logical pl
348cb 61 63 65 20 61 73 20 74 68 65 20 70 6f 73 69 74  ace as the posit
348cc 69 6f 6e 20 65 6c 65 6d 65 6e 74 2c 20 61 6e 64  ion element, and
348cd 20 61 63 74 20 61 73 20 73 65 6e 74 69 6e 61 6c   act as sentinal
348ce 73 0a 2a 2a 20 65 6e 64 69 6e 67 20 61 20 70 6f  s.** ending a po
348cf 73 69 74 69 6f 6e 20 6c 69 73 74 20 61 72 72 61  sition list arra
348d0 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 44 4c 5f 50 4f  y..**.** A DL_PO
348d1 53 49 54 49 4f 4e 53 20 64 6f 63 6c 69 73 74 20  SITIONS doclist 
348d2 6f 6d 69 74 73 20 74 68 65 20 73 74 61 72 74 4f  omits the startO
348d3 66 66 73 65 74 20 61 6e 64 20 65 6e 64 4f 66 66  ffset and endOff
348d4 73 65 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  set.** informati
348d5 6f 6e 2e 20 20 41 20 44 4c 5f 44 4f 43 49 44 53  on.  A DL_DOCIDS
348d6 20 64 6f 63 6c 69 73 74 20 6f 6d 69 74 73 20 62   doclist omits b
348d7 6f 74 68 20 74 68 65 20 70 6f 73 69 74 69 6f 6e  oth the position
348d8 20 61 6e 64 0a 2a 2a 20 6f 66 66 73 65 74 20 69   and.** offset i
348d9 6e 66 6f 72 6d 61 74 69 6f 6e 2c 20 62 65 63 6f  nformation, beco
348da 6d 69 6e 67 20 61 6e 20 61 72 72 61 79 20 6f 66  ming an array of
348db 20 76 61 72 69 6e 74 2d 65 6e 63 6f 64 65 64 20   varint-encoded 
348dc 64 6f 63 69 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e  docids..**.** On
348dd 2d 64 69 73 6b 20 64 61 74 61 20 69 73 20 73 74  -disk data is st
348de 6f 72 65 64 20 61 73 20 74 79 70 65 20 44 4c 5f  ored as type DL_
348df 44 45 46 41 55 4c 54 2c 20 73 6f 20 77 65 20 64  DEFAULT, so we d
348e0 6f 6e 27 74 20 73 65 72 69 61 6c 69 7a 65 0a 2a  on't serialize.*
348e1 2a 20 74 68 65 20 74 79 70 65 2e 20 20 44 75 65  * the type.  Due
348e2 20 74 6f 20 68 6f 77 20 64 65 6c 65 74 69 6f 6e   to how deletion
348e3 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20   is implemented 
348e4 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74 61 74  in the segmentat
348e5 69 6f 6e 0a 2a 2a 20 73 79 73 74 65 6d 2c 20 6f  ion.** system, o
348e6 6e 2d 64 69 73 6b 20 64 6f 63 6c 69 73 74 73 20  n-disk doclists 
348e7 4d 55 53 54 20 73 74 6f 72 65 20 61 74 20 6c 65  MUST store at le
348e8 61 73 74 20 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a  ast positions..*
348e9 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e  *.**.**** Segmen
348ea 74 20 6c 65 61 66 20 6e 6f 64 65 73 20 2a 2a 2a  t leaf nodes ***
348eb 2a 0a 2a 2a 20 53 65 67 6d 65 6e 74 20 6c 65 61  *.** Segment lea
348ec 66 20 6e 6f 64 65 73 20 73 74 6f 72 65 20 74 65  f nodes store te
348ed 72 6d 73 20 61 6e 64 20 64 6f 63 6c 69 73 74 73  rms and doclists
348ee 2c 20 6f 72 64 65 72 65 64 20 62 79 20 74 65 72  , ordered by ter
348ef 6d 2e 20 20 4c 65 61 66 0a 2a 2a 20 6e 6f 64 65  m.  Leaf.** node
348f0 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 75 73  s are written us
348f1 69 6e 67 20 4c 65 61 66 57 72 69 74 65 72 2c 20  ing LeafWriter, 
348f2 61 6e 64 20 72 65 61 64 20 75 73 69 6e 67 20 4c  and read using L
348f3 65 61 66 52 65 61 64 65 72 20 28 74 6f 0a 2a 2a  eafReader (to.**
348f4 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67 68   iterate through
348f5 20 61 20 73 69 6e 67 6c 65 20 6c 65 61 66 20 6e   a single leaf n
348f6 6f 64 65 27 73 20 64 61 74 61 29 20 61 6e 64 20  ode's data) and 
348f7 4c 65 61 76 65 73 52 65 61 64 65 72 20 28 74 6f  LeavesReader (to
348f8 0a 2a 2a 20 69 74 65 72 61 74 65 20 74 68 72 6f  .** iterate thro
348f9 75 67 68 20 61 20 73 65 67 6d 65 6e 74 27 73 20  ugh a segment's 
348fa 65 6e 74 69 72 65 20 6c 65 61 66 20 6c 61 79 65  entire leaf laye
348fb 72 29 2e 20 20 4c 65 61 66 20 6e 6f 64 65 73 20  r).  Leaf nodes 
348fc 68 61 76 65 0a 2a 2a 20 74 68 65 20 66 6f 72 6d  have.** the form
348fd 61 74 3a 0a 2a 2a 0a 2a 2a 20 76 61 72 69 6e 74  at:.**.** varint
348fe 20 69 48 65 69 67 68 74 3b 20 20 20 20 20 20 20   iHeight;       
348ff 20 20 20 20 20 20 28 68 65 69 67 68 74 20 66 72        (height fr
34900 6f 6d 20 6c 65 61 66 20 6c 65 76 65 6c 2c 20 61  om leaf level, a
34901 6c 77 61 79 73 20 30 29 0a 2a 2a 20 76 61 72 69  lways 0).** vari
34902 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20 20 20  nt nTerm;       
34903 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20          (length 
34904 6f 66 20 66 69 72 73 74 20 74 65 72 6d 29 0a 2a  of first term).*
34905 2a 20 63 68 61 72 20 70 54 65 72 6d 5b 6e 54 65  * char pTerm[nTe
34906 72 6d 5d 3b 20 20 20 20 20 20 20 20 20 20 28 63  rm];          (c
34907 6f 6e 74 65 6e 74 20 6f 66 20 66 69 72 73 74 20  ontent of first 
34908 74 65 72 6d 29 0a 2a 2a 20 76 61 72 69 6e 74 20  term).** varint 
34909 6e 44 6f 63 6c 69 73 74 3b 20 20 20 20 20 20 20  nDoclist;       
3490a 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20       (length of 
3490b 74 65 72 6d 27 73 20 61 73 73 6f 63 69 61 74 65  term's associate
3490c 64 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 63 68  d doclist).** ch
3490d 61 72 20 70 44 6f 63 6c 69 73 74 5b 6e 44 6f 63  ar pDoclist[nDoc
3490e 6c 69 73 74 5d 3b 20 20 20 20 28 63 6f 6e 74 65  list];    (conte
3490f 6e 74 20 6f 66 20 64 6f 63 6c 69 73 74 29 0a 2a  nt of doclist).*
34910 2a 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20  * array {.**    
34911 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34912 20 20 20 20 20 20 20 20 20 28 66 75 72 74 68 65           (furthe
34913 72 20 74 65 72 6d 73 20 61 72 65 20 64 65 6c 74  r terms are delt
34914 61 2d 65 6e 63 6f 64 65 64 29 0a 2a 2a 20 20 20  a-encoded).**   
34915 76 61 72 69 6e 74 20 6e 50 72 65 66 69 78 3b 20  varint nPrefix; 
34916 20 20 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74            (lengt
34917 68 20 6f 66 20 70 72 65 66 69 78 20 73 68 61 72  h of prefix shar
34918 65 64 20 77 69 74 68 20 70 72 65 76 69 6f 75 73  ed with previous
34919 20 74 65 72 6d 29 0a 2a 2a 20 20 20 76 61 72 69   term).**   vari
3491a 6e 74 20 6e 53 75 66 66 69 78 3b 20 20 20 20 20  nt nSuffix;     
3491b 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66        (length of
3491c 20 75 6e 73 68 61 72 65 64 20 73 75 66 66 69 78   unshared suffix
3491d 29 0a 2a 2a 20 20 20 63 68 61 72 20 70 54 65 72  ).**   char pTer
3491e 6d 53 75 66 66 69 78 5b 6e 53 75 66 66 69 78 5d  mSuffix[nSuffix]
3491f 3b 28 75 6e 73 68 61 72 65 64 20 73 75 66 66 69  ;(unshared suffi
34920 78 20 6f 66 20 6e 65 78 74 20 74 65 72 6d 29 0a  x of next term).
34921 2a 2a 20 20 20 76 61 72 69 6e 74 20 6e 44 6f 63  **   varint nDoc
34922 6c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 28  list;          (
34923 6c 65 6e 67 74 68 20 6f 66 20 74 65 72 6d 27 73  length of term's
34924 20 61 73 73 6f 63 69 61 74 65 64 20 64 6f 63 6c   associated docl
34925 69 73 74 29 0a 2a 2a 20 20 20 63 68 61 72 20 70  ist).**   char p
34926 44 6f 63 6c 69 73 74 5b 6e 44 6f 63 6c 69 73 74  Doclist[nDoclist
34927 5d 3b 20 20 28 63 6f 6e 74 65 6e 74 20 6f 66 20  ];  (content of 
34928 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 7d 0a 2a 2a  doclist).** }.**
34929 0a 2a 2a 20 48 65 72 65 2c 20 61 72 72 61 79 20  .** Here, array 
3492a 7b 20 58 20 7d 20 6d 65 61 6e 73 20 7a 65 72 6f  { X } means zero
3492b 20 6f 72 20 6d 6f 72 65 20 6f 63 63 75 72 72 65   or more occurre
3492c 6e 63 65 73 20 6f 66 20 58 2c 20 61 64 6a 61 63  nces of X, adjac
3492d 65 6e 74 20 69 6e 0a 2a 2a 20 6d 65 6d 6f 72 79  ent in.** memory
3492e 2e 0a 2a 2a 0a 2a 2a 20 4c 65 61 66 20 6e 6f 64  ..**.** Leaf nod
3492f 65 73 20 61 72 65 20 62 72 6f 6b 65 6e 20 69 6e  es are broken in
34930 74 6f 20 62 6c 6f 63 6b 73 20 77 68 69 63 68 20  to blocks which 
34931 61 72 65 20 73 74 6f 72 65 64 20 63 6f 6e 74 69  are stored conti
34932 67 75 6f 75 73 6c 79 20 69 6e 0a 2a 2a 20 74 68  guously in.** th
34933 65 20 25 5f 73 65 67 6d 65 6e 74 73 20 74 61 62  e %_segments tab
34934 6c 65 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64  le in sorted ord
34935 65 72 2e 20 20 54 68 69 73 20 6d 65 61 6e 73 20  er.  This means 
34936 74 68 61 74 20 77 68 65 6e 20 74 68 65 20 65 6e  that when the en
34937 64 0a 2a 2a 20 6f 66 20 61 20 6e 6f 64 65 20 69  d.** of a node i
34938 73 20 72 65 61 63 68 65 64 2c 20 74 68 65 20 6e  s reached, the n
34939 65 78 74 20 74 65 72 6d 20 69 73 20 69 6e 20 74  ext term is in t
3493a 68 65 20 6e 6f 64 65 20 77 69 74 68 20 74 68 65  he node with the
3493b 20 6e 65 78 74 0a 2a 2a 20 67 72 65 61 74 65 72   next.** greater
3493c 20 6e 6f 64 65 20 69 64 2e 0a 2a 2a 0a 2a 2a 20   node id..**.** 
3493d 4e 65 77 20 64 61 74 61 20 69 73 20 73 70 69 6c  New data is spil
3493e 6c 65 64 20 74 6f 20 61 20 6e 65 77 20 6c 65 61  led to a new lea
3493f 66 20 6e 6f 64 65 20 77 68 65 6e 20 74 68 65 20  f node when the 
34940 63 75 72 72 65 6e 74 20 6e 6f 64 65 0a 2a 2a 20  current node.** 
34941 65 78 63 65 65 64 73 20 4c 45 41 46 5f 4d 41 58  exceeds LEAF_MAX
34942 20 62 79 74 65 73 20 28 64 65 66 61 75 6c 74 20   bytes (default 
34943 32 30 34 38 29 2e 20 20 4e 65 77 20 64 61 74 61  2048).  New data
34944 20 77 68 69 63 68 20 69 74 73 65 6c 66 20 69 73   which itself is
34945 0a 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20  .** larger than 
34946 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 28  STANDALONE_MIN (
34947 64 65 66 61 75 6c 74 20 31 30 32 34 29 20 69 73  default 1024) is
34948 20 70 6c 61 63 65 64 20 69 6e 20 61 20 73 74 61   placed in a sta
34949 6e 64 61 6c 6f 6e 65 0a 2a 2a 20 6e 6f 64 65 20  ndalone.** node 
3494a 28 61 20 6c 65 61 66 20 6e 6f 64 65 20 77 69 74  (a leaf node wit
3494b 68 20 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20  h a single term 
3494c 61 6e 64 20 64 6f 63 6c 69 73 74 29 2e 20 20 54  and doclist).  T
3494d 68 65 20 67 6f 61 6c 20 6f 66 0a 2a 2a 20 74 68  he goal of.** th
3494e 65 73 65 20 73 65 74 74 69 6e 67 73 20 69 73 20  ese settings is 
3494f 74 6f 20 70 61 63 6b 20 74 6f 67 65 74 68 65 72  to pack together
34950 20 67 72 6f 75 70 73 20 6f 66 20 73 6d 61 6c 6c   groups of small
34951 20 64 6f 63 6c 69 73 74 73 20 77 68 69 6c 65 0a   doclists while.
34952 2a 2a 20 6d 61 6b 69 6e 67 20 69 74 20 65 66 66  ** making it eff
34953 69 63 69 65 6e 74 20 74 6f 20 64 69 72 65 63 74  icient to direct
34954 6c 79 20 61 63 63 65 73 73 20 6c 61 72 67 65 20  ly access large 
34955 64 6f 63 6c 69 73 74 73 2e 20 20 54 68 65 0a 2a  doclists.  The.*
34956 2a 20 61 73 73 75 6d 70 74 69 6f 6e 20 69 73 20  * assumption is 
34957 74 68 61 74 20 6c 61 72 67 65 20 64 6f 63 6c 69  that large docli
34958 73 74 73 20 72 65 70 72 65 73 65 6e 74 20 74 65  sts represent te
34959 72 6d 73 20 77 68 69 63 68 20 61 72 65 20 6d 6f  rms which are mo
3495a 72 65 0a 2a 2a 20 6c 69 6b 65 6c 79 20 74 6f 20  re.** likely to 
3495b 62 65 20 71 75 65 72 79 20 74 61 72 67 65 74 73  be query targets
3495c 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68 65  ..**.** TODO(she
3495d 73 73 29 20 49 74 20 6d 61 79 20 62 65 20 75 73  ss) It may be us
3495e 65 66 75 6c 20 66 6f 72 20 62 6c 6f 63 6b 69 6e  eful for blockin
3495f 67 20 64 65 63 69 73 69 6f 6e 73 20 74 6f 20 62  g decisions to b
34960 65 20 6d 6f 72 65 0a 2a 2a 20 64 79 6e 61 6d 69  e more.** dynami
34961 63 2e 20 20 46 6f 72 20 69 6e 73 74 61 6e 63 65  c.  For instance
34962 2c 20 69 74 20 6d 61 79 20 6d 61 6b 65 20 6d 6f  , it may make mo
34963 72 65 20 73 65 6e 73 65 20 74 6f 20 68 61 76 65  re sense to have
34964 20 61 20 32 2e 35 6b 20 6c 65 61 66 0a 2a 2a 20   a 2.5k leaf.** 
34965 6e 6f 64 65 20 72 61 74 68 65 72 20 74 68 61 6e  node rather than
34966 20 73 70 6c 69 74 74 69 6e 67 20 69 6e 74 6f 20   splitting into 
34967 32 6b 20 61 6e 64 20 2e 35 6b 20 6e 6f 64 65 73  2k and .5k nodes
34968 2e 20 20 4d 79 20 69 6e 74 75 69 74 69 6f 6e 20  .  My intuition 
34969 69 73 0a 2a 2a 20 74 68 61 74 20 74 68 69 73 20  is.** that this 
3496a 6d 69 67 68 74 20 65 78 74 65 6e 64 20 74 68 72  might extend thr
3496b 6f 75 67 68 20 32 78 20 6f 72 20 34 78 20 74 68  ough 2x or 4x th
3496c 65 20 70 61 67 65 73 69 7a 65 2e 0a 2a 2a 0a 2a  e pagesize..**.*
3496d 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 69  *.**** Segment i
3496e 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 2a 2a  nterior nodes **
3496f 2a 2a 0a 2a 2a 20 53 65 67 6d 65 6e 74 20 69 6e  **.** Segment in
34970 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 73 74 6f  terior nodes sto
34971 72 65 20 62 6c 6f 63 6b 69 64 73 20 66 6f 72 20  re blockids for 
34972 73 75 62 74 72 65 65 20 6e 6f 64 65 73 20 61 6e  subtree nodes an
34973 64 20 74 65 72 6d 73 0a 2a 2a 20 74 6f 20 64 65  d terms.** to de
34974 73 63 72 69 62 65 20 77 68 61 74 20 64 61 74 61  scribe what data
34975 20 69 73 20 73 74 6f 72 65 64 20 62 79 20 74 68   is stored by th
34976 65 20 65 61 63 68 20 73 75 62 74 72 65 65 2e 20  e each subtree. 
34977 20 49 6e 74 65 72 69 6f 72 0a 2a 2a 20 6e 6f 64   Interior.** nod
34978 65 73 20 61 72 65 20 77 72 69 74 74 65 6e 20 75  es are written u
34979 73 69 6e 67 20 49 6e 74 65 72 69 6f 72 57 72 69  sing InteriorWri
3497a 74 65 72 2c 20 61 6e 64 20 72 65 61 64 20 75 73  ter, and read us
3497b 69 6e 67 0a 2a 2a 20 49 6e 74 65 72 69 6f 72 52  ing.** InteriorR
3497c 65 61 64 65 72 2e 20 20 49 6e 74 65 72 69 6f 72  eader.  Interior
3497d 57 72 69 74 65 72 73 20 61 72 65 20 63 72 65 61  Writers are crea
3497e 74 65 64 20 61 73 20 6e 65 65 64 65 64 20 77 68  ted as needed wh
3497f 65 6e 0a 2a 2a 20 53 65 67 6d 65 6e 74 57 72 69  en.** SegmentWri
34980 74 65 72 20 63 72 65 61 74 65 73 20 6e 65 77 20  ter creates new 
34981 6c 65 61 66 20 6e 6f 64 65 73 2c 20 6f 72 20 77  leaf nodes, or w
34982 68 65 6e 20 61 6e 20 69 6e 74 65 72 69 6f 72 20  hen an interior 
34983 6e 6f 64 65 0a 2a 2a 20 69 74 73 65 6c 66 20 67  node.** itself g
34984 72 6f 77 73 20 74 6f 6f 20 62 69 67 20 61 6e 64  rows too big and
34985 20 6d 75 73 74 20 62 65 20 73 70 6c 69 74 2e 20   must be split. 
34986 20 54 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 69   The format of i
34987 6e 74 65 72 69 6f 72 0a 2a 2a 20 6e 6f 64 65 73  nterior.** nodes
34988 3a 0a 2a 2a 0a 2a 2a 20 76 61 72 69 6e 74 20 69  :.**.** varint i
34989 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 20  Height;         
3498a 20 20 28 68 65 69 67 68 74 20 66 72 6f 6d 20 6c    (height from l
3498b 65 61 66 20 6c 65 76 65 6c 2c 20 61 6c 77 61 79  eaf level, alway
3498c 73 20 3e 30 29 0a 2a 2a 20 76 61 72 69 6e 74 20  s >0).** varint 
3498d 69 42 6c 6f 63 6b 69 64 3b 20 20 20 20 20 20 20  iBlockid;       
3498e 20 20 20 28 62 6c 6f 63 6b 20 69 64 20 6f 66 20     (block id of 
3498f 6e 6f 64 65 27 73 20 6c 65 66 74 6d 6f 73 74 20  node's leftmost 
34990 73 75 62 74 72 65 65 29 0a 2a 2a 20 6f 70 74 69  subtree).** opti
34991 6f 6e 61 6c 20 7b 0a 2a 2a 20 20 20 76 61 72 69  onal {.**   vari
34992 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20 20 20 20  nt nTerm;       
34993 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 66      (length of f
34994 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a 20 20 20  irst term).**   
34995 63 68 61 72 20 70 54 65 72 6d 5b 6e 54 65 72 6d  char pTerm[nTerm
34996 5d 3b 20 20 20 20 20 20 28 63 6f 6e 74 65 6e 74  ];      (content
34997 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d 29 0a   of first term).
34998 2a 2a 20 20 20 61 72 72 61 79 20 7b 0a 2a 2a 20  **   array {.** 
34999 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3499a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28                 (
3499b 66 75 72 74 68 65 72 20 74 65 72 6d 73 20 61 72  further terms ar
3499c 65 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 29  e delta-encoded)
3499d 0a 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20 6e  .**     varint n
3499e 50 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 20  Prefix;         
3499f 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 73 68     (length of sh
349a0 61 72 65 64 20 70 72 65 66 69 78 20 77 69 74 68  ared prefix with
349a1 20 70 72 65 76 69 6f 75 73 20 74 65 72 6d 29 0a   previous term).
349a2 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20 6e 53  **     varint nS
349a3 75 66 66 69 78 3b 20 20 20 20 20 20 20 20 20 20  uffix;          
349a4 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 75 6e 73    (length of uns
349a5 68 61 72 65 64 20 73 75 66 66 69 78 29 0a 2a 2a  hared suffix).**
349a6 20 20 20 20 20 63 68 61 72 20 70 54 65 72 6d 53       char pTermS
349a7 75 66 66 69 78 5b 6e 53 75 66 66 69 78 5d 3b 20  uffix[nSuffix]; 
349a8 28 75 6e 73 68 61 72 65 64 20 73 75 66 66 69 78  (unshared suffix
349a9 20 6f 66 20 6e 65 78 74 20 74 65 72 6d 29 0a 2a   of next term).*
349aa 2a 20 20 20 7d 0a 2a 2a 20 7d 0a 2a 2a 0a 2a 2a  *   }.** }.**.**
349ab 20 48 65 72 65 2c 20 6f 70 74 69 6f 6e 61 6c 20   Here, optional 
349ac 7b 20 58 20 7d 20 6d 65 61 6e 73 20 61 6e 20 6f  { X } means an o
349ad 70 74 69 6f 6e 61 6c 20 65 6c 65 6d 65 6e 74 2c  ptional element,
349ae 20 77 68 69 6c 65 20 61 72 72 61 79 20 7b 20 58   while array { X
349af 20 7d 0a 2a 2a 20 6d 65 61 6e 73 20 7a 65 72 6f   }.** means zero
349b0 20 6f 72 20 6d 6f 72 65 20 6f 63 63 75 72 72 65   or more occurre
349b1 6e 63 65 73 20 6f 66 20 58 2c 20 61 64 6a 61 63  nces of X, adjac
349b2 65 6e 74 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a  ent in memory..*
349b3 2a 0a 2a 2a 20 41 6e 20 69 6e 74 65 72 69 6f 72  *.** An interior
349b4 20 6e 6f 64 65 20 65 6e 63 6f 64 65 73 20 6e 20   node encodes n 
349b5 74 65 72 6d 73 20 73 65 70 61 72 61 74 69 6e 67  terms separating
349b6 20 6e 2b 31 20 73 75 62 74 72 65 65 73 2e 20 20   n+1 subtrees.  
349b7 54 68 65 0a 2a 2a 20 73 75 62 74 72 65 65 20 62  The.** subtree b
349b8 6c 6f 63 6b 73 20 61 72 65 20 63 6f 6e 74 69 67  locks are contig
349b9 75 6f 75 73 2c 20 73 6f 20 6f 6e 6c 79 20 74 68  uous, so only th
349ba 65 20 66 69 72 73 74 20 73 75 62 74 72 65 65 27  e first subtree'
349bb 73 20 62 6c 6f 63 6b 69 64 0a 2a 2a 20 69 73 20  s blockid.** is 
349bc 65 6e 63 6f 64 65 64 2e 20 20 54 68 65 20 73 75  encoded.  The su
349bd 62 74 72 65 65 20 61 74 20 69 42 6c 6f 63 6b 69  btree at iBlocki
349be 64 20 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61  d will contain a
349bf 6c 6c 20 74 65 72 6d 73 20 6c 65 73 73 0a 2a 2a  ll terms less.**
349c0 20 74 68 61 6e 20 74 68 65 20 66 69 72 73 74 20   than the first 
349c1 74 65 72 6d 20 65 6e 63 6f 64 65 64 20 28 6f 72  term encoded (or
349c2 20 61 6c 6c 20 74 65 72 6d 73 20 69 66 20 6e 6f   all terms if no
349c3 20 74 65 72 6d 20 69 73 20 65 6e 63 6f 64 65 64   term is encoded
349c4 29 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c  )..** Otherwise,
349c5 20 66 6f 72 20 74 65 72 6d 73 20 67 72 65 61 74   for terms great
349c6 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c  er than or equal
349c7 20 74 6f 20 70 54 65 72 6d 5b 69 5d 20 62 75 74   to pTerm[i] but
349c8 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 70 54   less.** than pT
349c9 65 72 6d 5b 69 2b 31 5d 2c 20 74 68 65 20 73 75  erm[i+1], the su
349ca 62 74 72 65 65 20 66 6f 72 20 74 68 61 74 20 74  btree for that t
349cb 65 72 6d 20 77 69 6c 6c 20 62 65 20 72 6f 6f 74  erm will be root
349cc 65 64 20 61 74 0a 2a 2a 20 69 42 6c 6f 63 6b 69  ed at.** iBlocki
349cd 64 2b 69 2e 20 20 49 6e 74 65 72 69 6f 72 20 6e  d+i.  Interior n
349ce 6f 64 65 73 20 6f 6e 6c 79 20 73 74 6f 72 65 20  odes only store 
349cf 65 6e 6f 75 67 68 20 74 65 72 6d 20 64 61 74 61  enough term data
349d0 20 74 6f 0a 2a 2a 20 64 69 73 74 69 6e 67 75 69   to.** distingui
349d1 73 68 20 61 64 6a 61 63 65 6e 74 20 63 68 69 6c  sh adjacent chil
349d2 64 72 65 6e 20 28 69 66 20 74 68 65 20 72 69 67  dren (if the rig
349d3 68 74 6d 6f 73 74 20 74 65 72 6d 20 6f 66 20 74  htmost term of t
349d4 68 65 20 6c 65 66 74 0a 2a 2a 20 63 68 69 6c 64  he left.** child
349d5 20 69 73 20 22 73 6f 6d 65 74 68 69 6e 67 22 2c   is "something",
349d6 20 61 6e 64 20 74 68 65 20 6c 65 66 74 6d 6f 73   and the leftmos
349d7 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 72 69  t term of the ri
349d8 67 68 74 20 63 68 69 6c 64 20 69 73 0a 2a 2a 20  ght child is.** 
349d9 22 77 69 63 6b 65 64 22 2c 20 6f 6e 6c 79 20 22  "wicked", only "
349da 77 22 20 69 73 20 73 74 6f 72 65 64 29 2e 0a 2a  w" is stored)..*
349db 2a 0a 2a 2a 20 4e 65 77 20 64 61 74 61 20 69 73  *.** New data is
349dc 20 73 70 69 6c 6c 65 64 20 74 6f 20 61 20 6e 65   spilled to a ne
349dd 77 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20  w interior node 
349de 61 74 20 74 68 65 20 73 61 6d 65 20 68 65 69 67  at the same heig
349df 68 74 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 63  ht when.** the c
349e0 75 72 72 65 6e 74 20 6e 6f 64 65 20 65 78 63 65  urrent node exce
349e1 65 64 73 20 49 4e 54 45 52 49 4f 52 5f 4d 41 58  eds INTERIOR_MAX
349e2 20 62 79 74 65 73 20 28 64 65 66 61 75 6c 74 20   bytes (default 
349e3 32 30 34 38 29 2e 0a 2a 2a 20 49 4e 54 45 52 49  2048)..** INTERI
349e4 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53 20 28 64 65  OR_MIN_TERMS (de
349e5 66 61 75 6c 74 20 37 29 20 6b 65 65 70 73 20 6c  fault 7) keeps l
349e6 61 72 67 65 20 74 65 72 6d 73 20 66 72 6f 6d 20  arge terms from 
349e7 6d 6f 6e 6f 70 6f 6c 69 7a 69 6e 67 0a 2a 2a 20  monopolizing.** 
349e8 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 61  interior nodes a
349e9 6e 64 20 6d 61 6b 69 6e 67 20 74 68 65 20 74 72  nd making the tr
349ea 65 65 20 74 6f 6f 20 73 6b 69 6e 6e 79 2e 20 20  ee too skinny.  
349eb 54 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  The interior nod
349ec 65 73 0a 2a 2a 20 61 74 20 61 20 67 69 76 65 6e  es.** at a given
349ed 20 68 65 69 67 68 74 20 61 72 65 20 6e 61 74 75   height are natu
349ee 72 61 6c 6c 79 20 74 72 61 63 6b 65 64 20 62 79  rally tracked by
349ef 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20   interior nodes 
349f0 61 74 0a 2a 2a 20 68 65 69 67 68 74 2b 31 2c 20  at.** height+1, 
349f1 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a  and so on..**.**
349f2 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 64 69  .**** Segment di
349f3 72 65 63 74 6f 72 79 20 2a 2a 2a 2a 0a 2a 2a 20  rectory ****.** 
349f4 54 68 65 20 73 65 67 6d 65 6e 74 20 64 69 72 65  The segment dire
349f5 63 74 6f 72 79 20 69 6e 20 74 61 62 6c 65 20 25  ctory in table %
349f6 5f 73 65 67 64 69 72 20 73 74 6f 72 65 73 20 6d  _segdir stores m
349f7 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  eta-information 
349f8 66 6f 72 0a 2a 2a 20 6d 65 72 67 69 6e 67 20 61  for.** merging a
349f9 6e 64 20 64 65 6c 65 74 69 6e 67 20 73 65 67 6d  nd deleting segm
349fa 65 6e 74 73 2c 20 61 6e 64 20 61 6c 73 6f 20 74  ents, and also t
349fb 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20  he root node of 
349fc 74 68 65 0a 2a 2a 20 73 65 67 6d 65 6e 74 27 73  the.** segment's
349fd 20 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   tree..**.** The
349fe 20 72 6f 6f 74 20 6e 6f 64 65 20 69 73 20 74 68   root node is th
349ff 65 20 74 6f 70 20 6e 6f 64 65 20 6f 66 20 74 68  e top node of th
34a00 65 20 73 65 67 6d 65 6e 74 27 73 20 74 72 65 65  e segment's tree
34a01 20 61 66 74 65 72 20 65 6e 63 6f 64 69 6e 67 0a   after encoding.
34a02 2a 2a 20 74 68 65 20 65 6e 74 69 72 65 20 73 65  ** the entire se
34a03 67 6d 65 6e 74 2c 20 72 65 73 74 72 69 63 74 65  gment, restricte
34a04 64 20 74 6f 20 52 4f 4f 54 5f 4d 41 58 20 62 79  d to ROOT_MAX by
34a05 74 65 73 20 28 64 65 66 61 75 6c 74 20 31 30 32  tes (default 102
34a06 34 29 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6c  4)..** This coul
34a07 64 20 62 65 20 65 69 74 68 65 72 20 61 20 6c 65  d be either a le
34a08 61 66 20 6e 6f 64 65 20 6f 72 20 61 6e 20 69 6e  af node or an in
34a09 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20 20 49 66  terior node.  If
34a0a 20 74 68 65 20 74 6f 70 0a 2a 2a 20 6e 6f 64 65   the top.** node
34a0b 20 72 65 71 75 69 72 65 73 20 6d 6f 72 65 20 74   requires more t
34a0c 68 61 6e 20 52 4f 4f 54 5f 4d 41 58 20 62 79 74  han ROOT_MAX byt
34a0d 65 73 2c 20 69 74 20 69 73 20 66 6c 75 73 68 65  es, it is flushe
34a0e 64 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 0a  d to %_segments.
34a0f 2a 2a 20 61 6e 64 20 61 20 6e 65 77 20 72 6f 6f  ** and a new roo
34a10 74 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20  t interior node 
34a11 69 73 20 67 65 6e 65 72 61 74 65 64 20 28 77 68  is generated (wh
34a12 69 63 68 20 73 68 6f 75 6c 64 20 61 6c 77 61 79  ich should alway
34a13 73 20 66 69 74 0a 2a 2a 20 77 69 74 68 69 6e 20  s fit.** within 
34a14 52 4f 4f 54 5f 4d 41 58 20 62 65 63 61 75 73 65  ROOT_MAX because
34a15 20 69 74 20 6f 6e 6c 79 20 6e 65 65 64 73 20 73   it only needs s
34a16 70 61 63 65 20 66 6f 72 20 32 20 76 61 72 69 6e  pace for 2 varin
34a17 74 73 2c 20 74 68 65 0a 2a 2a 20 68 65 69 67 68  ts, the.** heigh
34a18 74 20 61 6e 64 20 74 68 65 20 62 6c 6f 63 6b 69  t and the blocki
34a19 64 20 6f 66 20 74 68 65 20 70 72 65 76 69 6f 75  d of the previou
34a1a 73 20 72 6f 6f 74 29 2e 0a 2a 2a 0a 2a 2a 20 54  s root)..**.** T
34a1b 68 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74  he meta-informat
34a1c 69 6f 6e 20 69 6e 20 74 68 65 20 73 65 67 6d 65  ion in the segme
34a1d 6e 74 20 64 69 72 65 63 74 6f 72 79 20 69 73 3a  nt directory is:
34a1e 0a 2a 2a 20 20 20 6c 65 76 65 6c 20 20 20 20 20  .**   level     
34a1f 20 20 20 20 20 20 20 20 20 20 2d 20 73 65 67 6d            - segm
34a20 65 6e 74 20 6c 65 76 65 6c 20 28 73 65 65 20 62  ent level (see b
34a21 65 6c 6f 77 29 0a 2a 2a 20 20 20 69 64 78 20 20  elow).**   idx  
34a22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d                 -
34a23 20 69 6e 64 65 78 20 77 69 74 68 69 6e 20 6c 65   index within le
34a24 76 65 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  vel.**          
34a25 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 20 28               - (
34a26 6c 65 76 65 6c 2c 69 64 78 20 75 6e 69 71 75 65  level,idx unique
34a27 6c 79 20 69 64 65 6e 74 69 66 79 20 61 20 73 65  ly identify a se
34a28 67 6d 65 6e 74 29 0a 2a 2a 20 20 20 73 74 61 72  gment).**   star
34a29 74 5f 62 6c 6f 63 6b 20 20 20 20 20 20 20 20 20  t_block         
34a2a 2d 20 66 69 72 73 74 20 6c 65 61 66 20 6e 6f 64  - first leaf nod
34a2b 65 0a 2a 2a 20 20 20 6c 65 61 76 65 73 5f 65 6e  e.**   leaves_en
34a2c 64 5f 62 6c 6f 63 6b 20 20 20 20 2d 20 6c 61 73  d_block    - las
34a2d 74 20 6c 65 61 66 20 6e 6f 64 65 0a 2a 2a 20 20  t leaf node.**  
34a2e 20 65 6e 64 5f 62 6c 6f 63 6b 20 20 20 20 20 20   end_block      
34a2f 20 20 20 20 20 2d 20 6c 61 73 74 20 62 6c 6f 63       - last bloc
34a30 6b 20 28 69 6e 63 6c 75 64 69 6e 67 20 69 6e 74  k (including int
34a31 65 72 69 6f 72 20 6e 6f 64 65 73 29 0a 2a 2a 20  erior nodes).** 
34a32 20 20 72 6f 6f 74 20 20 20 20 20 20 20 20 20 20    root          
34a33 20 20 20 20 20 20 2d 20 63 6f 6e 74 65 6e 74 73        - contents
34a34 20 6f 66 20 72 6f 6f 74 20 6e 6f 64 65 0a 2a 2a   of root node.**
34a35 0a 2a 2a 20 49 66 20 74 68 65 20 72 6f 6f 74 20  .** If the root 
34a36 6e 6f 64 65 20 69 73 20 61 20 6c 65 61 66 20 6e  node is a leaf n
34a37 6f 64 65 2c 20 74 68 65 6e 20 73 74 61 72 74 5f  ode, then start_
34a38 62 6c 6f 63 6b 2c 0a 2a 2a 20 6c 65 61 76 65 73  block,.** leaves
34a39 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 61 6e 64 20  _end_block, and 
34a3a 65 6e 64 5f 62 6c 6f 63 6b 20 61 72 65 20 61 6c  end_block are al
34a3b 6c 20 30 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20  l 0..**.**.**** 
34a3c 53 65 67 6d 65 6e 74 20 6d 65 72 67 69 6e 67 20  Segment merging 
34a3d 2a 2a 2a 2a 0a 2a 2a 20 54 6f 20 61 6d 6f 72 74  ****.** To amort
34a3e 69 7a 65 20 75 70 64 61 74 65 20 63 6f 73 74 73  ize update costs
34a3f 2c 20 73 65 67 6d 65 6e 74 73 20 61 72 65 20 67  , segments are g
34a40 72 6f 75 70 65 64 20 69 6e 74 6f 20 6c 65 76 65  rouped into leve
34a41 6c 73 20 61 6e 64 0a 2a 2a 20 6d 65 72 67 65 64  ls and.** merged
34a42 20 69 6e 20 62 61 74 63 68 65 73 2e 20 20 45 61   in batches.  Ea
34a43 63 68 20 69 6e 63 72 65 61 73 65 20 69 6e 20 6c  ch increase in l
34a44 65 76 65 6c 20 72 65 70 72 65 73 65 6e 74 73 20  evel represents 
34a45 65 78 70 6f 6e 65 6e 74 69 61 6c 6c 79 0a 2a 2a  exponentially.**
34a46 20 6d 6f 72 65 20 64 6f 63 75 6d 65 6e 74 73 2e   more documents.
34a47 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 64 6f 63 75 6d  .**.** New docum
34a48 65 6e 74 73 20 28 61 63 74 75 61 6c 6c 79 2c 20  ents (actually, 
34a49 64 6f 63 75 6d 65 6e 74 20 75 70 64 61 74 65 73  document updates
34a4a 29 20 61 72 65 20 74 6f 6b 65 6e 69 7a 65 64 20  ) are tokenized 
34a4b 61 6e 64 0a 2a 2a 20 77 72 69 74 74 65 6e 20 69  and.** written i
34a4c 6e 64 69 76 69 64 75 61 6c 6c 79 20 28 75 73 69  ndividually (usi
34a4d 6e 67 20 4c 65 61 66 57 72 69 74 65 72 29 20 74  ng LeafWriter) t
34a4e 6f 20 61 20 6c 65 76 65 6c 20 30 20 73 65 67 6d  o a level 0 segm
34a4f 65 6e 74 2c 20 77 69 74 68 0a 2a 2a 20 69 6e 63  ent, with.** inc
34a50 72 65 6d 65 6e 74 69 6e 67 20 69 64 78 2e 20 20  rementing idx.  
34a51 57 68 65 6e 20 69 64 78 20 72 65 61 63 68 65 73  When idx reaches
34a52 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 28 64 65   MERGE_COUNT (de
34a53 66 61 75 6c 74 20 31 36 29 2c 20 61 6c 6c 0a 2a  fault 16), all.*
34a54 2a 20 6c 65 76 65 6c 20 30 20 73 65 67 6d 65 6e  * level 0 segmen
34a55 74 73 20 61 72 65 20 6d 65 72 67 65 64 20 69 6e  ts are merged in
34a56 74 6f 20 61 20 73 69 6e 67 6c 65 20 6c 65 76 65  to a single leve
34a57 6c 20 31 20 73 65 67 6d 65 6e 74 2e 20 20 4c 65  l 1 segment.  Le
34a58 76 65 6c 20 31 0a 2a 2a 20 69 73 20 70 6f 70 75  vel 1.** is popu
34a59 6c 61 74 65 64 20 6c 69 6b 65 20 6c 65 76 65 6c  lated like level
34a5a 20 30 2c 20 61 6e 64 20 65 76 65 6e 74 75 61 6c   0, and eventual
34a5b 6c 79 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 6c  ly MERGE_COUNT l
34a5c 65 76 65 6c 20 31 0a 2a 2a 20 73 65 67 6d 65 6e  evel 1.** segmen
34a5d 74 73 20 61 72 65 20 6d 65 72 67 65 64 20 74 6f  ts are merged to
34a5e 20 61 20 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20   a single level 
34a5f 32 20 73 65 67 6d 65 6e 74 20 28 72 65 70 72 65  2 segment (repre
34a60 73 65 6e 74 69 6e 67 0a 2a 2a 20 4d 45 52 47 45  senting.** MERGE
34a61 5f 43 4f 55 4e 54 5e 32 20 75 70 64 61 74 65 73  _COUNT^2 updates
34a62 29 2c 20 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a  ), and so on..**
34a63 0a 2a 2a 20 41 20 73 65 67 6d 65 6e 74 20 6d 65  .** A segment me
34a64 72 67 65 20 74 72 61 76 65 72 73 65 73 20 61 6c  rge traverses al
34a65 6c 20 73 65 67 6d 65 6e 74 73 20 61 74 20 61 20  l segments at a 
34a66 67 69 76 65 6e 20 6c 65 76 65 6c 20 69 6e 0a 2a  given level in.*
34a67 2a 20 70 61 72 61 6c 6c 65 6c 2c 20 70 65 72 66  * parallel, perf
34a68 6f 72 6d 69 6e 67 20 61 20 73 74 72 61 69 67 68  orming a straigh
34a69 74 66 6f 72 77 61 72 64 20 73 6f 72 74 65 64 20  tforward sorted 
34a6a 6d 65 72 67 65 2e 20 20 53 69 6e 63 65 20 73 65  merge.  Since se
34a6b 67 6d 65 6e 74 0a 2a 2a 20 6c 65 61 66 20 6e 6f  gment.** leaf no
34a6c 64 65 73 20 61 72 65 20 77 72 69 74 74 65 6e 20  des are written 
34a6d 69 6e 20 74 6f 20 74 68 65 20 25 5f 73 65 67 6d  in to the %_segm
34a6e 65 6e 74 73 20 74 61 62 6c 65 20 69 6e 20 6f 72  ents table in or
34a6f 64 65 72 2c 20 74 68 69 73 0a 2a 2a 20 6d 65 72  der, this.** mer
34a70 67 65 20 74 72 61 76 65 72 73 65 73 20 74 68 65  ge traverses the
34a71 20 75 6e 64 65 72 6c 79 69 6e 67 20 73 71 6c 69   underlying sqli
34a72 74 65 20 64 69 73 6b 20 73 74 72 75 63 74 75 72  te disk structur
34a73 65 73 20 65 66 66 69 63 69 65 6e 74 6c 79 2e 0a  es efficiently..
34a74 2a 2a 20 41 66 74 65 72 20 74 68 65 20 6d 65 72  ** After the mer
34a75 67 65 2c 20 61 6c 6c 20 73 65 67 6d 65 6e 74 20  ge, all segment 
34a76 62 6c 6f 63 6b 73 20 66 72 6f 6d 20 74 68 65 20  blocks from the 
34a77 6d 65 72 67 65 64 20 6c 65 76 65 6c 20 61 72 65  merged level are
34a78 0a 2a 2a 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a  .** deleted..**.
34a79 2a 2a 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 63  ** MERGE_COUNT c
34a7a 6f 6e 74 72 6f 6c 73 20 68 6f 77 20 6f 66 74 65  ontrols how ofte
34a7b 6e 20 77 65 20 6d 65 72 67 65 20 73 65 67 6d 65  n we merge segme
34a7c 6e 74 73 2e 20 20 31 36 20 73 65 65 6d 73 20 74  nts.  16 seems t
34a7d 6f 20 62 65 0a 2a 2a 20 73 6f 6d 65 77 68 61 74  o be.** somewhat
34a7e 20 6f 66 20 61 20 73 77 65 65 74 20 73 70 6f 74   of a sweet spot
34a7f 20 66 6f 72 20 69 6e 73 65 72 74 69 6f 6e 20 70   for insertion p
34a80 65 72 66 6f 72 6d 61 6e 63 65 2e 20 20 33 32 20  erformance.  32 
34a81 61 6e 64 20 36 34 20 73 68 6f 77 0a 2a 2a 20 76  and 64 show.** v
34a82 65 72 79 20 73 69 6d 69 6c 61 72 20 70 65 72 66  ery similar perf
34a83 6f 72 6d 61 6e 63 65 20 6e 75 6d 62 65 72 73 20  ormance numbers 
34a84 74 6f 20 31 36 20 6f 6e 20 69 6e 73 65 72 74 69  to 16 on inserti
34a85 6f 6e 2c 20 74 68 6f 75 67 68 20 74 68 65 79 27  on, though they'
34a86 72 65 0a 2a 2a 20 61 20 74 69 6e 79 20 62 69 74  re.** a tiny bit
34a87 20 73 6c 6f 77 65 72 20 28 70 65 72 68 61 70 73   slower (perhaps
34a88 20 64 75 65 20 74 6f 20 6d 6f 72 65 20 6f 76 65   due to more ove
34a89 72 68 65 61 64 20 69 6e 20 6d 65 72 67 65 2d 74  rhead in merge-t
34a8a 69 6d 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 29 2e  ime.** sorting).
34a8b 20 20 38 20 69 73 20 61 62 6f 75 74 20 32 30 25    8 is about 20%
34a8c 20 73 6c 6f 77 65 72 20 74 68 61 6e 20 31 36 2c   slower than 16,
34a8d 20 34 20 61 62 6f 75 74 20 35 30 25 20 73 6c 6f   4 about 50% slo
34a8e 77 65 72 20 74 68 61 6e 0a 2a 2a 20 31 36 2c 20  wer than.** 16, 
34a8f 32 20 61 62 6f 75 74 20 36 36 25 20 73 6c 6f 77  2 about 66% slow
34a90 65 72 20 74 68 61 6e 20 31 36 2e 0a 2a 2a 0a 2a  er than 16..**.*
34a91 2a 20 41 74 20 71 75 65 72 79 20 74 69 6d 65 2c  * At query time,
34a92 20 68 69 67 68 20 4d 45 52 47 45 5f 43 4f 55 4e   high MERGE_COUN
34a93 54 20 69 6e 63 72 65 61 73 65 73 20 74 68 65 20  T increases the 
34a94 6e 75 6d 62 65 72 20 6f 66 20 73 65 67 6d 65 6e  number of segmen
34a95 74 73 0a 2a 2a 20 77 68 69 63 68 20 6e 65 65 64  ts.** which need
34a96 20 74 6f 20 62 65 20 73 63 61 6e 6e 65 64 20 61   to be scanned a
34a97 6e 64 20 6d 65 72 67 65 64 2e 20 20 46 6f 72 20  nd merged.  For 
34a98 69 6e 73 74 61 6e 63 65 2c 20 77 69 74 68 20 31  instance, with 1
34a99 30 30 6b 20 64 6f 63 73 0a 2a 2a 20 69 6e 73 65  00k docs.** inse
34a9a 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  rted:.**.**    M
34a9b 45 52 47 45 5f 43 4f 55 4e 54 20 20 20 73 65 67  ERGE_COUNT   seg
34a9c 6d 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20 20 31  ments.**       1
34a9d 36 20 20 20 20 20 20 20 20 20 20 20 32 35 0a 2a  6           25.*
34a9e 2a 20 20 20 20 20 20 20 20 38 20 20 20 20 20 20  *        8      
34a9f 20 20 20 20 20 31 32 0a 2a 2a 20 20 20 20 20 20       12.**      
34aa0 20 20 34 20 20 20 20 20 20 20 20 20 20 20 31 30    4           10
34aa1 0a 2a 2a 20 20 20 20 20 20 20 20 32 20 20 20 20  .**        2    
34aa2 20 20 20 20 20 20 20 20 36 0a 2a 2a 0a 2a 2a 20          6.**.** 
34aa3 54 68 69 73 20 61 70 70 65 61 72 73 20 74 6f 20  This appears to 
34aa4 68 61 76 65 20 6f 6e 6c 79 20 61 20 6d 6f 64 65  have only a mode
34aa5 72 61 74 65 20 69 6d 70 61 63 74 20 6f 6e 20 71  rate impact on q
34aa6 75 65 72 69 65 73 20 66 6f 72 20 76 65 72 79 0a  ueries for very.
34aa7 2a 2a 20 66 72 65 71 75 65 6e 74 20 74 65 72 6d  ** frequent term
34aa8 73 20 28 77 68 69 63 68 20 61 72 65 20 73 6f 6d  s (which are som
34aa9 65 77 68 61 74 20 64 6f 6d 69 6e 61 74 65 64 20  ewhat dominated 
34aaa 62 79 20 73 65 67 6d 65 6e 74 20 6d 65 72 67 65  by segment merge
34aab 0a 2a 2a 20 63 6f 73 74 73 29 2c 20 61 6e 64 20  .** costs), and 
34aac 69 6e 66 72 65 71 75 65 6e 74 20 61 6e 64 20 6e  infrequent and n
34aad 6f 6e 2d 65 78 69 73 74 65 6e 74 20 74 65 72 6d  on-existent term
34aae 73 20 73 74 69 6c 6c 20 73 65 65 6d 20 74 6f 20  s still seem to 
34aaf 62 65 20 66 61 73 74 0a 2a 2a 20 65 76 65 6e 20  be fast.** even 
34ab0 77 69 74 68 20 6d 61 6e 79 20 73 65 67 6d 65 6e  with many segmen
34ab1 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73  ts..**.** TODO(s
34ab2 68 65 73 73 29 20 54 68 61 74 20 73 61 69 64 2c  hess) That said,
34ab3 20 69 74 20 77 6f 75 6c 64 20 62 65 20 6e 69 63   it would be nic
34ab4 65 20 74 6f 20 68 61 76 65 20 61 20 62 65 74 74  e to have a bett
34ab5 65 72 20 71 75 65 72 79 2d 73 69 64 65 0a 2a 2a  er query-side.**
34ab6 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20 4d 45   argument for ME
34ab7 52 47 45 5f 43 4f 55 4e 54 20 6f 66 20 31 36 2e  RGE_COUNT of 16.
34ab8 20 20 41 6c 73 6f 2c 20 69 74 20 69 73 20 70 6f    Also, it is po
34ab9 73 73 69 62 6c 65 2f 6c 69 6b 65 6c 79 20 74 68  ssible/likely th
34aba 61 74 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69  at.** optimizati
34abb 6f 6e 73 20 74 6f 20 74 68 69 6e 67 73 20 6c 69  ons to things li
34abc 6b 65 20 64 6f 63 6c 69 73 74 20 6d 65 72 67 69  ke doclist mergi
34abd 6e 67 20 77 69 6c 6c 20 73 77 69 6e 67 20 74 68  ng will swing th
34abe 65 20 73 77 65 65 74 0a 2a 2a 20 73 70 6f 74 20  e sweet.** spot 
34abf 61 72 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a  around..**.**.**
34ac0 0a 2a 2a 2a 2a 20 48 61 6e 64 6c 69 6e 67 20 6f  .**** Handling o
34ac1 66 20 64 65 6c 65 74 69 6f 6e 73 20 61 6e 64 20  f deletions and 
34ac2 75 70 64 61 74 65 73 20 2a 2a 2a 2a 0a 2a 2a 20  updates ****.** 
34ac3 53 69 6e 63 65 20 77 65 27 72 65 20 75 73 69 6e  Since we're usin
34ac4 67 20 61 20 73 65 67 6d 65 6e 74 65 64 20 73 74  g a segmented st
34ac5 72 75 63 74 75 72 65 2c 20 77 69 74 68 20 6e 6f  ructure, with no
34ac6 20 64 6f 63 69 64 2d 6f 72 69 65 6e 74 65 64 0a   docid-oriented.
34ac7 2a 2a 20 69 6e 64 65 78 20 69 6e 74 6f 20 74 68  ** index into th
34ac8 65 20 74 65 72 6d 20 69 6e 64 65 78 2c 20 77 65  e term index, we
34ac9 20 63 6c 65 61 72 6c 79 20 63 61 6e 6e 6f 74 20   clearly cannot 
34aca 73 69 6d 70 6c 79 20 75 70 64 61 74 65 20 74 68  simply update th
34acb 65 20 74 65 72 6d 0a 2a 2a 20 69 6e 64 65 78 20  e term.** index 
34acc 77 68 65 6e 20 61 20 64 6f 63 75 6d 65 6e 74 20  when a document 
34acd 69 73 20 64 65 6c 65 74 65 64 20 6f 72 20 75 70  is deleted or up
34ace 64 61 74 65 64 2e 20 20 46 6f 72 20 64 65 6c 65  dated.  For dele
34acf 74 69 6f 6e 73 2c 20 77 65 0a 2a 2a 20 77 72 69  tions, we.** wri
34ad0 74 65 20 61 6e 20 65 6d 70 74 79 20 64 6f 63 6c  te an empty docl
34ad1 69 73 74 20 28 76 61 72 69 6e 74 28 64 6f 63 69  ist (varint(doci
34ad2 64 29 20 76 61 72 69 6e 74 28 50 4f 53 5f 45 4e  d) varint(POS_EN
34ad3 44 29 29 2c 20 66 6f 72 20 75 70 64 61 74 65 73  D)), for updates
34ad4 0a 2a 2a 20 77 65 20 73 69 6d 70 6c 79 20 77 72  .** we simply wr
34ad5 69 74 65 20 74 68 65 20 6e 65 77 20 64 6f 63 6c  ite the new docl
34ad6 69 73 74 2e 20 20 53 65 67 6d 65 6e 74 20 6d 65  ist.  Segment me
34ad7 72 67 65 73 20 6f 76 65 72 77 72 69 74 65 20 6f  rges overwrite o
34ad8 6c 64 65 72 0a 2a 2a 20 64 61 74 61 20 66 6f 72  lder.** data for
34ad9 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 6f   a particular do
34ada 63 69 64 20 77 69 74 68 20 6e 65 77 65 72 20 64  cid with newer d
34adb 61 74 61 2c 20 73 6f 20 64 65 6c 65 74 65 73 20  ata, so deletes 
34adc 6f 72 20 75 70 64 61 74 65 73 0a 2a 2a 20 77 69  or updates.** wi
34add 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 20 6f 76  ll eventually ov
34ade 65 72 74 61 6b 65 20 74 68 65 20 65 61 72 6c 69  ertake the earli
34adf 65 72 20 64 61 74 61 20 61 6e 64 20 6b 6e 6f 63  er data and knoc
34ae0 6b 20 69 74 20 6f 75 74 2e 20 20 54 68 65 0a 2a  k it out.  The.*
34ae1 2a 20 71 75 65 72 79 20 6c 6f 67 69 63 20 6c 69  * query logic li
34ae2 6b 65 77 69 73 65 20 6d 65 72 67 65 73 20 64 6f  kewise merges do
34ae3 63 6c 69 73 74 73 20 73 6f 20 74 68 61 74 20 6e  clists so that n
34ae4 65 77 65 72 20 64 61 74 61 20 6b 6e 6f 63 6b 73  ewer data knocks
34ae5 20 6f 75 74 0a 2a 2a 20 6f 6c 64 65 72 20 64 61   out.** older da
34ae6 74 61 2e 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73  ta..**.** TODO(s
34ae7 68 65 73 73 29 20 50 72 6f 76 69 64 65 20 61 20  hess) Provide a 
34ae8 56 41 43 55 55 4d 20 74 79 70 65 20 6f 70 65 72  VACUUM type oper
34ae9 61 74 69 6f 6e 20 74 6f 20 63 6c 65 61 72 20 6f  ation to clear o
34aea 75 74 20 61 6c 6c 0a 2a 2a 20 64 65 6c 65 74 69  ut all.** deleti
34aeb 6f 6e 73 20 61 6e 64 20 64 75 70 6c 69 63 61 74  ons and duplicat
34aec 69 6f 6e 73 2e 20 20 54 68 69 73 20 77 6f 75 6c  ions.  This woul
34aed 64 20 62 61 73 69 63 61 6c 6c 79 20 62 65 20 61  d basically be a
34aee 20 66 6f 72 63 65 64 20 6d 65 72 67 65 0a 2a 2a   forced merge.**
34aef 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73   into a single s
34af0 65 67 6d 65 6e 74 2e 0a 2a 2f 0a 0a 23 69 66 20  egment..*/..#if 
34af1 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
34af2 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64  CORE) || defined
34af3 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46  (SQLITE_ENABLE_F
34af4 54 53 33 29 0a 0a 23 69 66 20 64 65 66 69 6e 65  TS3)..#if define
34af5 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
34af6 46 54 53 33 29 20 26 26 20 21 64 65 66 69 6e 65  FTS3) && !define
34af7 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 0a 23  d(SQLITE_CORE).#
34af8 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43   define SQLITE_C
34af9 4f 52 45 20 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f  ORE 1.#endif.../
34afa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
34afb 6e 63 6c 75 64 65 20 66 74 73 33 5f 65 78 70 72  nclude fts3_expr
34afc 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
34afd 20 6f 66 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a   of fts3.c *****
34afe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
34aff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
34b00 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 65  egin file fts3_e
34b01 78 70 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  xpr.h **********
34b02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b03 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
34b04 2a 0a 2a 2a 20 32 30 30 38 20 4e 6f 76 20 32 38  *.** 2008 Nov 28
34b05 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
34b06 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
34b07 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
34b08 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
34b09 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
34b0a 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
34b0b 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
34b0c 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
34b0d 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
34b0e 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
34b0f 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
34b10 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
34b11 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
34b12 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
34b13 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
34b14 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
34b15 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
34b16 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
34b17 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b18 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b19 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b1a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b1b 2a 2a 2a 2a 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 2a 2a  ****.**.*/../***
34b1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
34b1d 75 64 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  ude fts3_tokeniz
34b1e 65 72 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  er.h in the midd
34b1f 6c 65 20 6f 66 20 66 74 73 33 5f 65 78 70 72 2e  le of fts3_expr.
34b20 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  h ********/./***
34b21 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
34b22 6e 20 66 69 6c 65 20 66 74 73 33 5f 74 6f 6b 65  n file fts3_toke
34b23 6e 69 7a 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  nizer.h ********
34b24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b25 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
34b26 2a 20 32 30 30 36 20 4a 75 6c 79 20 31 30 0a 2a  * 2006 July 10.*
34b27 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
34b28 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
34b29 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
34b2a 63 65 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ce code..**.****
34b2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b2c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b2d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b2e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34b2f 2a 2a 2a 2a 2a 0a 2a 2a 20 44 65 66 69 6e 65 73  *****.** Defines
34b30 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74   the interface t
34b31 6f 20 74 6f 6b 65 6e 69 7a 65 72 73 20 75 73 65  o tokenizers use
34b32 64 20 62 79 20 66 75 6c 6c 74 65 78 74 2d 73 65  d by fulltext-se
34b33 61 72 63 68 2e 20 20 54 68 65 72 65 0a 2a 2a 20  arch.  There.** 
34b34 61 72 65 20 74 68 72 65 65 20 62 61 73 69 63 20  are three basic 
34b35 63 6f 6d 70 6f 6e 65 6e 74 73 3a 0a 2a 2a 0a 2a  components:.**.*
34b36 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  * sqlite3_tokeni
34b37 7a 65 72 5f 6d 6f 64 75 6c 65 20 69 73 20 61 20  zer_module is a 
34b38 73 69 6e 67 6c 65 74 6f 6e 20 64 65 66 69 6e 69  singleton defini
34b39 6e 67 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72  ng the tokenizer
34b3a 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 66 75  .** interface fu
34b3b 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 69  nctions.  This i
34b3c 73 20 65 73 73 65 6e 74 69 61 6c 6c 79 20 74 68  s essentially th
34b3d 65 20 63 6c 61 73 73 20 73 74 72 75 63 74 75 72  e class structur
34b3e 65 20 66 6f 72 0a 2a 2a 20 74 6f 6b 65 6e 69 7a  e for.** tokeniz
34b3f 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74  ers..**.** sqlit
34b40 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 69 73 20  e3_tokenizer is 
34b41 75 73 65 64 20 74 6f 20 64 65 66 69 6e 65 20 61  used to define a
34b42 20 70 61 72 74 69 63 75 6c 61 72 20 74 6f 6b 65   particular toke
34b43 6e 69 7a 65 72 2c 20 70 65 72 68 61 70 73 0a 2a  nizer, perhaps.*
34b44 2a 20 69 6e 63 6c 75 64 69 6e 67 20 63 75 73 74  * including cust
34b45 6f 6d 69 7a 61 74 69 6f 6e 20 69 6e 66 6f 72 6d  omization inform
34b46 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20 61 74  ation defined at
34b47 20 63 72 65 61 74 69 6f 6e 20 74 69 6d 65 2e 0a   creation time..
34b48 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f  **.** sqlite3_to
34b49 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 69  kenizer_cursor i
34b4a 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 61  s generated by a
34b4b 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f 20 67 65   tokenizer to ge
34b4c 6e 65 72 61 74 65 0a 2a 2a 20 74 6f 6b 65 6e 73  nerate.** tokens
34b4d 20 66 72 6f 6d 20 61 20 70 61 72 74 69 63 75 6c   from a particul
34b4e 61 72 20 69 6e 70 75 74 2e 0a 2a 2f 0a 23 69 66  ar input..*/.#if
34b4f 6e 64 65 66 20 5f 46 54 53 33 5f 54 4f 4b 45 4e  ndef _FTS3_TOKEN
34b50 49 5a 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20  IZER_H_.#define 
34b51 5f 46 54 53 33 5f 54 4f 4b 45 4e 49 5a 45 52 5f  _FTS3_TOKENIZER_
34b52 48 5f 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  H_../* TODO(shes
34b53 73 29 20 4f 6e 6c 79 20 75 73 65 64 20 66 6f 72  s) Only used for
34b54 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 53   SQLITE_OK and S
34b55 51 4c 49 54 45 5f 44 4f 4e 45 20 61 74 20 74 68  QLITE_DONE at th
34b56 69 73 20 74 69 6d 65 2e 0a 2a 2a 20 49 66 20 74  is time..** If t
34b57 6f 6b 65 6e 69 7a 65 72 73 20 61 72 65 20 74 6f  okenizers are to
34b58 20 62 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 63   be allowed to c
34b59 61 6c 6c 20 73 71 6c 69 74 65 33 5f 2a 28 29 20  all sqlite3_*() 
34b5a 66 75 6e 63 74 69 6f 6e 73 2c 20 74 68 65 6e 0a  functions, then.
34b5b 2a 2a 20 77 65 20 77 69 6c 6c 20 6e 65 65 64 20  ** we will need 
34b5c 61 20 77 61 79 20 74 6f 20 72 65 67 69 73 74 65  a way to registe
34b5d 72 20 74 68 65 20 41 50 49 20 63 6f 6e 73 69 73  r the API consis
34b5e 74 65 6e 74 6c 79 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  tently..*/../*.*
34b5f 2a 20 53 74 72 75 63 74 75 72 65 73 20 75 73 65  * Structures use
34b60 64 20 62 79 20 74 68 65 20 74 6f 6b 65 6e 69 7a  d by the tokeniz
34b61 65 72 20 69 6e 74 65 72 66 61 63 65 2e 20 57 68  er interface. Wh
34b62 65 6e 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a  en a new tokeniz
34b63 65 72 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  er.** implementa
34b64 74 69 6f 6e 20 69 73 20 72 65 67 69 73 74 65 72  tion is register
34b65 65 64 2c 20 74 68 65 20 63 61 6c 6c 65 72 20 70  ed, the caller p
34b66 72 6f 76 69 64 65 73 20 61 20 70 6f 69 6e 74 65  rovides a pointe
34b67 72 20 74 6f 0a 2a 2a 20 61 6e 20 73 71 6c 69 74  r to.** an sqlit
34b68 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
34b69 75 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70  ule containing p
34b6a 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 63  ointers to the c
34b6b 61 6c 6c 62 61 63 6b 0a 2a 2a 20 66 75 6e 63 74  allback.** funct
34b6c 69 6f 6e 73 20 74 68 61 74 20 6d 61 6b 65 20 75  ions that make u
34b6d 70 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74  p an implementat
34b6e 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  ion..**.** When 
34b6f 61 6e 20 66 74 73 33 20 74 61 62 6c 65 20 69 73  an fts3 table is
34b70 20 63 72 65 61 74 65 64 2c 20 69 74 20 70 61 73   created, it pas
34b71 73 65 73 20 61 6e 79 20 61 72 67 75 6d 65 6e 74  ses any argument
34b72 73 20 70 61 73 73 65 64 20 74 6f 0a 2a 2a 20 74  s passed to.** t
34b73 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 63 6c 61  he tokenizer cla
34b74 75 73 65 20 6f 66 20 74 68 65 20 43 52 45 41 54  use of the CREAT
34b75 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20  E VIRTUAL TABLE 
34b76 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 74 68 65  statement to the
34b77 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  .** sqlite3_toke
34b78 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 2e 78 43 72  nizer_module.xCr
34b79 65 61 74 65 28 29 20 66 75 6e 63 74 69 6f 6e 20  eate() function 
34b7a 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  of the requested
34b7b 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20 69 6d   tokenizer.** im
34b7c 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 54 68  plementation. Th
34b7d 65 20 78 43 72 65 61 74 65 28 29 20 66 75 6e 63  e xCreate() func
34b7e 74 69 6f 6e 20 69 6e 20 74 75 72 6e 20 72 65 74  tion in turn ret
34b7f 75 72 6e 73 20 61 6e 20 0a 2a 2a 20 73 71 6c 69  urns an .** sqli
34b80 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 73 74  te3_tokenizer st
34b81 72 75 63 74 75 72 65 20 72 65 70 72 65 73 65 6e  ructure represen
34b82 74 69 6e 67 20 74 68 65 20 73 70 65 63 69 66 69  ting the specifi
34b83 63 20 74 6f 6b 65 6e 69 7a 65 72 20 74 6f 0a 2a  c tokenizer to.*
34b84 2a 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68  * be used for th
34b85 65 20 66 74 73 33 20 74 61 62 6c 65 20 28 63 75  e fts3 table (cu
34b86 73 74 6f 6d 69 7a 65 64 20 62 79 20 74 68 65 20  stomized by the 
34b87 74 6f 6b 65 6e 69 7a 65 72 20 63 6c 61 75 73 65  tokenizer clause
34b88 20 61 72 67 75 6d 65 6e 74 73 29 2e 0a 2a 2a 0a   arguments)..**.
34b89 2a 2a 20 54 6f 20 74 6f 6b 65 6e 69 7a 65 20 61  ** To tokenize a
34b8a 6e 20 69 6e 70 75 74 20 62 75 66 66 65 72 2c 20  n input buffer, 
34b8b 74 68 65 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  the sqlite3_toke
34b8c 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 2e 78 4f 70  nizer_module.xOp
34b8d 65 6e 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 69  en().** method i
34b8e 73 20 63 61 6c 6c 65 64 2e 20 49 74 20 72 65 74  s called. It ret
34b8f 75 72 6e 73 20 61 6e 20 73 71 6c 69 74 65 33 5f  urns an sqlite3_
34b90 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
34b91 20 6f 62 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20   object.** that 
34b92 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f 20 74  may be used to t
34b93 6f 6b 65 6e 69 7a 65 20 61 20 73 70 65 63 69 66  okenize a specif
34b94 69 63 20 69 6e 70 75 74 20 62 75 66 66 65 72 20  ic input buffer 
34b95 62 61 73 65 64 20 6f 6e 0a 2a 2a 20 74 68 65 20  based on.** the 
34b96 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 72 75 6c  tokenization rul
34b97 65 73 20 73 75 70 70 6c 69 65 64 20 62 79 20 61  es supplied by a
34b98 20 73 70 65 63 69 66 69 63 20 73 71 6c 69 74 65   specific sqlite
34b99 33 5f 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20 6f  3_tokenizer.** o
34b9a 62 6a 65 63 74 2e 0a 2a 2f 0a 74 79 70 65 64 65  bject..*/.typede
34b9b 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  f struct sqlite3
34b9c 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
34b9d 65 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e sqlite3_tokeni
34b9e 7a 65 72 5f 6d 6f 64 75 6c 65 3b 0a 74 79 70 65  zer_module;.type
34b9f 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
34ba0 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 73 71 6c  e3_tokenizer sql
34ba1 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 3b 0a  ite3_tokenizer;.
34ba2 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
34ba3 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
34ba4 5f 63 75 72 73 6f 72 20 73 71 6c 69 74 65 33 5f  _cursor sqlite3_
34ba5 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
34ba6 3b 0a 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  ;..struct sqlite
34ba7 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
34ba8 6c 65 20 7b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  le {..  /*.  ** 
34ba9 53 74 72 75 63 74 75 72 65 20 76 65 72 73 69 6f  Structure versio
34baa 6e 2e 20 53 68 6f 75 6c 64 20 61 6c 77 61 79 73  n. Should always
34bab 20 62 65 20 73 65 74 20 74 6f 20 30 2e 0a 20 20   be set to 0..  
34bac 2a 2f 0a 20 20 69 6e 74 20 69 56 65 72 73 69 6f  */.  int iVersio
34bad 6e 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 72  n;..  /*.  ** Cr
34bae 65 61 74 65 20 61 20 6e 65 77 20 74 6f 6b 65 6e  eate a new token
34baf 69 7a 65 72 2e 20 54 68 65 20 76 61 6c 75 65 73  izer. The values
34bb0 20 69 6e 20 74 68 65 20 61 72 67 76 5b 5d 20 61   in the argv[] a
34bb1 72 72 61 79 20 61 72 65 20 74 68 65 0a 20 20 2a  rray are the.  *
34bb2 2a 20 61 72 67 75 6d 65 6e 74 73 20 70 61 73 73  * arguments pass
34bb3 65 64 20 74 6f 20 74 68 65 20 22 74 6f 6b 65 6e  ed to the "token
34bb4 69 7a 65 72 22 20 63 6c 61 75 73 65 20 6f 66 20  izer" clause of 
34bb5 74 68 65 20 43 52 45 41 54 45 20 56 49 52 54 55  the CREATE VIRTU
34bb6 41 4c 0a 20 20 2a 2a 20 54 41 42 4c 45 20 73 74  AL.  ** TABLE st
34bb7 61 74 65 6d 65 6e 74 20 74 68 61 74 20 63 72 65  atement that cre
34bb8 61 74 65 64 20 74 68 65 20 66 74 73 33 20 74 61  ated the fts3 ta
34bb9 62 6c 65 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  ble. For example
34bba 2c 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 66 6f  , if.  ** the fo
34bbb 6c 6c 6f 77 69 6e 67 20 53 51 4c 20 69 73 20 65  llowing SQL is e
34bbc 78 65 63 75 74 65 64 3a 0a 20 20 2a 2a 0a 20 20  xecuted:.  **.  
34bbd 2a 2a 20 20 20 43 52 45 41 54 45 20 2e 2e 20 55  **   CREATE .. U
34bbe 53 49 4e 47 20 66 74 73 33 28 20 2e 2e 2e 20 2c  SING fts3( ... ,
34bbf 20 74 6f 6b 65 6e 69 7a 65 72 20 3c 74 6f 6b 65   tokenizer <toke
34bc0 6e 69 7a 65 72 2d 6e 61 6d 65 3e 20 61 72 67 31  nizer-name> arg1
34bc1 20 61 72 67 32 29 0a 20 20 2a 2a 0a 20 20 2a 2a   arg2).  **.  **
34bc2 20 74 68 65 6e 20 61 72 67 63 20 69 73 20 73 65   then argc is se
34bc3 74 20 74 6f 20 32 2c 20 61 6e 64 20 74 68 65 20  t to 2, and the 
34bc4 61 72 67 76 5b 5d 20 61 72 72 61 79 20 63 6f 6e  argv[] array con
34bc5 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 0a 20  tains pointers. 
34bc6 20 2a 2a 20 74 6f 20 74 68 65 20 73 74 72 69 6e   ** to the strin
34bc7 67 73 20 22 61 72 67 31 22 20 61 6e 64 20 22 61  gs "arg1" and "a
34bc8 72 67 32 22 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  rg2"..  **.  ** 
34bc9 54 68 69 73 20 6d 65 74 68 6f 64 20 73 68 6f 75  This method shou
34bca 6c 64 20 72 65 74 75 72 6e 20 65 69 74 68 65 72  ld return either
34bcb 20 53 51 4c 49 54 45 5f 4f 4b 20 28 30 29 2c 20   SQLITE_OK (0), 
34bcc 6f 72 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72  or an SQLite err
34bcd 6f 72 20 0a 20 20 2a 2a 20 63 6f 64 65 2e 20 49  or .  ** code. I
34bce 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  f SQLITE_OK is r
34bcf 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20 2a 70  eturned, then *p
34bd0 70 54 6f 6b 65 6e 69 7a 65 72 20 73 68 6f 75 6c  pTokenizer shoul
34bd1 64 20 62 65 20 73 65 74 0a 20 20 2a 2a 20 74 6f  d be set.  ** to
34bd2 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20 6e 65   point at the ne
34bd3 77 6c 79 20 63 72 65 61 74 65 64 20 74 6f 6b 65  wly created toke
34bd4 6e 69 7a 65 72 20 73 74 72 75 63 74 75 72 65 2e  nizer structure.
34bd5 20 54 68 65 20 67 65 6e 65 72 69 63 0a 20 20 2a   The generic.  *
34bd6 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  * sqlite3_tokeni
34bd7 7a 65 72 2e 70 4d 6f 64 75 6c 65 20 76 61 72 69  zer.pModule vari
34bd8 61 62 6c 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20  able should not 
34bd9 62 65 20 69 6e 69 74 69 61 6c 69 73 65 64 20 62  be initialised b
34bda 79 0a 20 20 2a 2a 20 74 68 69 73 20 63 61 6c 6c  y.  ** this call
34bdb 62 61 63 6b 2e 20 54 68 65 20 63 61 6c 6c 65 72  back. The caller
34bdc 20 77 69 6c 6c 20 64 6f 20 73 6f 2e 0a 20 20 2a   will do so..  *
34bdd 2f 0a 20 20 69 6e 74 20 28 2a 78 43 72 65 61 74  /.  int (*xCreat
34bde 65 29 28 0a 20 20 20 20 69 6e 74 20 61 72 67 63  e)(.    int argc
34bdf 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
34be0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
34be1 69 7a 65 20 6f 66 20 61 72 67 76 20 61 72 72 61  ize of argv arra
34be2 79 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63  y */.    const c
34be3 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c  har *const*argv,
34be4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
34be5 54 6f 6b 65 6e 69 7a 65 72 20 61 72 67 75 6d 65  Tokenizer argume
34be6 6e 74 20 73 74 72 69 6e 67 73 20 2a 2f 0a 20 20  nt strings */.  
34be7 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
34be8 7a 65 72 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65  zer **ppTokenize
34be9 72 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 43 72  r     /* OUT: Cr
34bea 65 61 74 65 64 20 74 6f 6b 65 6e 69 7a 65 72 20  eated tokenizer 
34beb 2a 2f 0a 20 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20  */.  );..  /*.  
34bec 2a 2a 20 44 65 73 74 72 6f 79 20 61 6e 20 65 78  ** Destroy an ex
34bed 69 73 74 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72  isting tokenizer
34bee 2e 20 54 68 65 20 66 74 73 33 20 6d 6f 64 75 6c  . The fts3 modul
34bef 65 20 63 61 6c 6c 73 20 74 68 69 73 20 6d 65 74  e calls this met
34bf0 68 6f 64 0a 20 20 2a 2a 20 65 78 61 63 74 6c 79  hod.  ** exactly
34bf1 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 73   once for each s
34bf2 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74  uccessful call t
34bf3 6f 20 78 43 72 65 61 74 65 28 29 2e 0a 20 20 2a  o xCreate()..  *
34bf4 2f 0a 20 20 69 6e 74 20 28 2a 78 44 65 73 74 72  /.  int (*xDestr
34bf5 6f 79 29 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65  oy)(sqlite3_toke
34bf6 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65  nizer *pTokenize
34bf7 72 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43  r);..  /*.  ** C
34bf8 72 65 61 74 65 20 61 20 74 6f 6b 65 6e 69 7a 65  reate a tokenize
34bf9 72 20 63 75 72 73 6f 72 20 74 6f 20 74 6f 6b 65  r cursor to toke
34bfa 6e 69 7a 65 20 61 6e 20 69 6e 70 75 74 20 62 75  nize an input bu
34bfb 66 66 65 72 2e 20 54 68 65 20 63 61 6c 6c 65 72  ffer. The caller
34bfc 0a 20 20 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73  .  ** is respons
34bfd 69 62 6c 65 20 66 6f 72 20 65 6e 73 75 72 69 6e  ible for ensurin
34bfe 67 20 74 68 61 74 20 74 68 65 20 69 6e 70 75 74  g that the input
34bff 20 62 75 66 66 65 72 20 72 65 6d 61 69 6e 73 20   buffer remains 
34c00 76 61 6c 69 64 0a 20 20 2a 2a 20 75 6e 74 69 6c  valid.  ** until
34c01 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 63   the cursor is c
34c02 6c 6f 73 65 64 20 28 75 73 69 6e 67 20 74 68 65  losed (using the
34c03 20 78 43 6c 6f 73 65 28 29 20 6d 65 74 68 6f 64   xClose() method
34c04 29 2e 20 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28  ). .  */.  int (
34c05 2a 78 4f 70 65 6e 29 28 0a 20 20 20 20 73 71 6c  *xOpen)(.    sql
34c06 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
34c07 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20  pTokenizer,     
34c08 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 6f    /* Tokenizer o
34c09 62 6a 65 63 74 20 2a 2f 0a 20 20 20 20 63 6f 6e  bject */.    con
34c0a 73 74 20 63 68 61 72 20 2a 70 49 6e 70 75 74 2c  st char *pInput,
34c0b 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20   int nBytes,    
34c0c 20 20 2f 2a 20 49 6e 70 75 74 20 62 75 66 66 65    /* Input buffe
34c0d 72 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  r */.    sqlite3
34c0e 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
34c0f 72 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 2f 2a  r **ppCursor  /*
34c10 20 4f 55 54 3a 20 43 72 65 61 74 65 64 20 74 6f   OUT: Created to
34c11 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72 20 2a  kenizer cursor *
34c12 2f 0a 20 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  /.  );..  /*.  *
34c13 2a 20 44 65 73 74 72 6f 79 20 61 6e 20 65 78 69  * Destroy an exi
34c14 73 74 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72 20  sting tokenizer 
34c15 63 75 72 73 6f 72 2e 20 54 68 65 20 66 74 73 33  cursor. The fts3
34c16 20 6d 6f 64 75 6c 65 20 63 61 6c 6c 73 20 74 68   module calls th
34c17 69 73 20 0a 20 20 2a 2a 20 6d 65 74 68 6f 64 20  is .  ** method 
34c18 65 78 61 63 74 6c 79 20 6f 6e 63 65 20 66 6f 72  exactly once for
34c19 20 65 61 63 68 20 73 75 63 63 65 73 73 66 75 6c   each successful
34c1a 20 63 61 6c 6c 20 74 6f 20 78 4f 70 65 6e 28 29   call to xOpen()
34c1b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78  ..  */.  int (*x
34c1c 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 74  Close)(sqlite3_t
34c1d 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
34c1e 2a 70 43 75 72 73 6f 72 29 3b 0a 0a 20 20 2f 2a  *pCursor);..  /*
34c1f 0a 20 20 2a 2a 20 52 65 74 72 69 65 76 65 20 74  .  ** Retrieve t
34c20 68 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72  he next token fr
34c21 6f 6d 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72  om the tokenizer
34c22 20 63 75 72 73 6f 72 20 70 43 75 72 73 6f 72 2e   cursor pCursor.
34c23 20 54 68 69 73 0a 20 20 2a 2a 20 6d 65 74 68 6f   This.  ** metho
34c24 64 20 73 68 6f 75 6c 64 20 65 69 74 68 65 72 20  d should either 
34c25 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
34c26 20 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c   and set the val
34c27 75 65 73 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20  ues of the.  ** 
34c28 22 4f 55 54 22 20 76 61 72 69 61 62 6c 65 73 20  "OUT" variables 
34c29 69 64 65 6e 74 69 66 69 65 64 20 62 65 6c 6f 77  identified below
34c2a 2c 20 6f 72 20 53 51 4c 49 54 45 5f 44 4f 4e 45  , or SQLITE_DONE
34c2b 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74 68 61   to indicate tha
34c2c 74 0a 20 20 2a 2a 20 74 68 65 20 65 6e 64 20 6f  t.  ** the end o
34c2d 66 20 74 68 65 20 62 75 66 66 65 72 20 68 61 73  f the buffer has
34c2e 20 62 65 65 6e 20 72 65 61 63 68 65 64 2c 20 6f   been reached, o
34c2f 72 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f  r an SQLite erro
34c30 72 20 63 6f 64 65 2e 0a 20 20 2a 2a 0a 20 20 2a  r code..  **.  *
34c31 2a 20 2a 70 70 54 6f 6b 65 6e 20 73 68 6f 75 6c  * *ppToken shoul
34c32 64 20 62 65 20 73 65 74 20 74 6f 20 70 6f 69 6e  d be set to poin
34c33 74 20 61 74 20 61 20 62 75 66 66 65 72 20 63 6f  t at a buffer co
34c34 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 0a 20 20  ntaining the .  
34c35 2a 2a 20 6e 6f 72 6d 61 6c 69 7a 65 64 20 76 65  ** normalized ve
34c36 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 74 6f 6b  rsion of the tok
34c37 65 6e 20 28 69 2e 65 2e 20 61 66 74 65 72 20 61  en (i.e. after a
34c38 6e 79 20 63 61 73 65 2d 66 6f 6c 64 69 6e 67 20  ny case-folding 
34c39 61 6e 64 2f 6f 72 0a 20 20 2a 2a 20 73 74 65 6d  and/or.  ** stem
34c3a 6d 69 6e 67 20 68 61 73 20 62 65 65 6e 20 70 65  ming has been pe
34c3b 72 66 6f 72 6d 65 64 29 2e 20 2a 70 6e 42 79 74  rformed). *pnByt
34c3c 65 73 20 73 68 6f 75 6c 64 20 62 65 20 73 65 74  es should be set
34c3d 20 74 6f 20 74 68 65 20 6c 65 6e 67 74 68 0a 20   to the length. 
34c3e 20 2a 2a 20 6f 66 20 74 68 69 73 20 62 75 66 66   ** of this buff
34c3f 65 72 20 69 6e 20 62 79 74 65 73 2e 20 54 68 65  er in bytes. The
34c40 20 69 6e 70 75 74 20 74 65 78 74 20 74 68 61 74   input text that
34c41 20 67 65 6e 65 72 61 74 65 64 20 74 68 65 20 74   generated the t
34c42 6f 6b 65 6e 20 69 73 0a 20 20 2a 2a 20 69 64 65  oken is.  ** ide
34c43 6e 74 69 66 69 65 64 20 62 79 20 74 68 65 20 62  ntified by the b
34c44 79 74 65 20 6f 66 66 73 65 74 73 20 72 65 74 75  yte offsets retu
34c45 72 6e 65 64 20 69 6e 20 2a 70 69 53 74 61 72 74  rned in *piStart
34c46 4f 66 66 73 65 74 20 61 6e 64 0a 20 20 2a 2a 20  Offset and.  ** 
34c47 2a 70 69 45 6e 64 4f 66 66 73 65 74 2e 20 2a 70  *piEndOffset. *p
34c48 69 53 74 61 72 74 4f 66 66 73 65 74 20 73 68 6f  iStartOffset sho
34c49 75 6c 64 20 62 65 20 73 65 74 20 74 6f 20 74 68  uld be set to th
34c4a 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 66  e index of the f
34c4b 69 72 73 74 0a 20 20 2a 2a 20 62 79 74 65 20 6f  irst.  ** byte o
34c4c 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 6e 20 74  f the token in t
34c4d 68 65 20 69 6e 70 75 74 20 62 75 66 66 65 72 2e  he input buffer.
34c4e 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 20 73 68   *piEndOffset sh
34c4f 6f 75 6c 64 20 62 65 20 73 65 74 0a 20 20 2a 2a  ould be set.  **
34c50 20 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66   to the index of
34c51 20 74 68 65 20 66 69 72 73 74 20 62 79 74 65 20   the first byte 
34c52 6a 75 73 74 20 70 61 73 74 20 74 68 65 20 65 6e  just past the en
34c53 64 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 69  d of the token i
34c54 6e 0a 20 20 2a 2a 20 74 68 65 20 69 6e 70 75 74  n.  ** the input
34c55 20 62 75 66 66 65 72 2e 0a 20 20 2a 2a 0a 20 20   buffer..  **.  
34c56 2a 2a 20 54 68 65 20 62 75 66 66 65 72 20 2a 70  ** The buffer *p
34c57 70 54 6f 6b 65 6e 20 69 73 20 73 65 74 20 74 6f  pToken is set to
34c58 20 70 6f 69 6e 74 20 61 74 20 69 73 20 6d 61 6e   point at is man
34c59 61 67 65 64 20 62 79 20 74 68 65 20 74 6f 6b 65  aged by the toke
34c5a 6e 69 7a 65 72 0a 20 20 2a 2a 20 69 6d 70 6c 65  nizer.  ** imple
34c5b 6d 65 6e 74 61 74 69 6f 6e 2e 20 49 74 20 69 73  mentation. It is
34c5c 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 20 74   only required t
34c5d 6f 20 62 65 20 76 61 6c 69 64 20 75 6e 74 69 6c  o be valid until
34c5e 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 0a 20   the next call. 
34c5f 20 2a 2a 20 74 6f 20 78 4e 65 78 74 28 29 20 6f   ** to xNext() o
34c60 72 20 78 43 6c 6f 73 65 28 29 2e 20 0a 20 20 2a  r xClose(). .  *
34c61 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73  /.  /* TODO(shes
34c62 73 29 20 63 75 72 72 65 6e 74 20 69 6d 70 6c 65  s) current imple
34c63 6d 65 6e 74 61 74 69 6f 6e 20 72 65 71 75 69 72  mentation requir
34c64 65 73 20 70 49 6e 70 75 74 20 74 6f 20 62 65 0a  es pInput to be.
34c65 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61    ** nul-termina
34c66 74 65 64 2e 20 20 54 68 69 73 20 73 68 6f 75 6c  ted.  This shoul
34c67 64 20 65 69 74 68 65 72 20 62 65 20 66 69 78 65  d either be fixe
34c68 64 2c 20 6f 72 20 70 49 6e 70 75 74 2f 6e 42 79  d, or pInput/nBy
34c69 74 65 73 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20  tes.  ** should 
34c6a 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f 20  be converted to 
34c6b 7a 49 6e 70 75 74 2e 0a 20 20 2a 2f 0a 20 20 69  zInput..  */.  i
34c6c 6e 74 20 28 2a 78 4e 65 78 74 29 28 0a 20 20 20  nt (*xNext)(.   
34c6d 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
34c6e 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  er_cursor *pCurs
34c6f 6f 72 2c 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a  or,   /* Tokeniz
34c70 65 72 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 20  er cursor */.   
34c71 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 70   const char **pp
34c72 54 6f 6b 65 6e 2c 20 69 6e 74 20 2a 70 6e 42 79  Token, int *pnBy
34c73 74 65 73 2c 20 20 2f 2a 20 4f 55 54 3a 20 4e 6f  tes,  /* OUT: No
34c74 72 6d 61 6c 69 7a 65 64 20 74 65 78 74 20 66 6f  rmalized text fo
34c75 72 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 69  r token */.    i
34c76 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66 73 65  nt *piStartOffse
34c77 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 42 79 74 65  t,  /* OUT: Byte
34c78 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e   offset of token
34c79 20 69 6e 20 69 6e 70 75 74 20 62 75 66 66 65 72   in input buffer
34c7a 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 70 69 45   */.    int *piE
34c7b 6e 64 4f 66 66 73 65 74 2c 20 20 20 20 2f 2a 20  ndOffset,    /* 
34c7c 4f 55 54 3a 20 42 79 74 65 20 6f 66 66 73 65 74  OUT: Byte offset
34c7d 20 6f 66 20 65 6e 64 20 6f 66 20 74 6f 6b 65 6e   of end of token
34c7e 20 69 6e 20 69 6e 70 75 74 20 62 75 66 66 65 72   in input buffer
34c7f 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 70 69 50   */.    int *piP
34c80 6f 73 69 74 69 6f 6e 20 20 20 20 20 20 2f 2a 20  osition      /* 
34c81 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 74  OUT: Number of t
34c82 6f 6b 65 6e 73 20 72 65 74 75 72 6e 65 64 20 62  okens returned b
34c83 65 66 6f 72 65 20 74 68 69 73 20 6f 6e 65 20 2a  efore this one *
34c84 2f 0a 20 20 29 3b 0a 7d 3b 0a 0a 73 74 72 75 63  /.  );.};..struc
34c85 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
34c86 7a 65 72 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71  zer {.  const sq
34c87 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
34c88 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b  module *pModule;
34c89 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20    /* The module 
34c8a 66 6f 72 20 74 68 69 73 20 74 6f 6b 65 6e 69 7a  for this tokeniz
34c8b 65 72 20 2a 2f 0a 20 20 2f 2a 20 54 6f 6b 65 6e  er */.  /* Token
34c8c 69 7a 65 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74  izer implementat
34c8d 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69 63 61  ions will typica
34c8e 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e  lly add addition
34c8f 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d 3b 0a  al fields */.};.
34c90 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
34c91 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
34c92 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b   {.  sqlite3_tok
34c93 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a  enizer *pTokeniz
34c94 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b  er;       /* Tok
34c95 65 6e 69 7a 65 72 20 66 6f 72 20 74 68 69 73 20  enizer for this 
34c96 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 2f 2a 20  cursor. */.  /* 
34c97 54 6f 6b 65 6e 69 7a 65 72 20 69 6d 70 6c 65 6d  Tokenizer implem
34c98 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74  entations will t
34c99 79 70 69 63 61 6c 6c 79 20 61 64 64 20 61 64 64  ypically add add
34c9a 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a  itional fields *
34c9b 2f 0a 7d 3b 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  /.};..#endif /* 
34c9c 5f 46 54 53 33 5f 54 4f 4b 45 4e 49 5a 45 52 5f  _FTS3_TOKENIZER_
34c9d 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  H_ */../********
34c9e 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74  ****** End of ft
34c9f 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 68 20 2a  s3_tokenizer.h *
34ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34ca1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34ca2 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
34ca3 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
34ca4 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
34ca5 6f 66 66 20 69 6e 20 66 74 73 33 5f 65 78 70 72  off in fts3_expr
34ca6 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
34ca7 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  *****/../*.** Th
34ca8 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64 65 73 63  e following desc
34ca9 72 69 62 65 73 20 74 68 65 20 73 79 6e 74 61 78  ribes the syntax
34caa 20 73 75 70 70 6f 72 74 65 64 20 62 79 20 74 68   supported by th
34cab 65 20 66 74 73 33 20 4d 41 54 43 48 0a 2a 2a 20  e fts3 MATCH.** 
34cac 6f 70 65 72 61 74 6f 72 20 69 6e 20 61 20 73 69  operator in a si
34cad 6d 69 6c 61 72 20 66 6f 72 6d 61 74 20 74 6f 20  milar format to 
34cae 74 68 61 74 20 75 73 65 64 20 62 79 20 74 68 65  that used by the
34caf 20 6c 65 6d 6f 6e 20 70 61 72 73 65 72 0a 2a 2a   lemon parser.**
34cb0 20 67 65 6e 65 72 61 74 6f 72 2e 20 54 68 69 73   generator. This
34cb1 20 6d 6f 64 75 6c 65 20 64 6f 65 73 20 6e 6f 74   module does not
34cb2 20 75 73 65 20 61 63 74 75 61 6c 6c 79 20 6c 65   use actually le
34cb3 6d 6f 6e 2c 20 69 74 20 75 73 65 73 20 61 0a 2a  mon, it uses a.*
34cb4 2a 20 63 75 73 74 6f 6d 20 70 61 72 73 65 72 2e  * custom parser.
34cb5 0a 2a 2a 0a 2a 2a 20 20 20 71 75 65 72 79 20 3a  .**.**   query :
34cb6 3a 3d 20 61 6e 64 65 78 70 72 20 28 4f 52 20 61  := andexpr (OR a
34cb7 6e 64 65 78 70 72 29 2a 2e 0a 2a 2a 0a 2a 2a 20  ndexpr)*..**.** 
34cb8 20 20 61 6e 64 65 78 70 72 20 3a 3a 3d 20 6e 6f    andexpr ::= no
34cb9 74 65 78 70 72 20 28 41 4e 44 3f 20 6e 6f 74 65  texpr (AND? note
34cba 78 70 72 29 2a 2e 0a 2a 2a 0a 2a 2a 20 20 20 6e  xpr)*..**.**   n
34cbb 6f 74 65 78 70 72 20 3a 3a 3d 20 6e 65 61 72 65  otexpr ::= neare
34cbc 78 70 72 20 28 4e 4f 54 20 6e 65 61 72 65 78 70  xpr (NOT nearexp
34cbd 72 7c 2d 54 4f 4b 45 4e 29 2a 2e 0a 2a 2a 20 20  r|-TOKEN)*..**  
34cbe 20 6e 6f 74 65 78 70 72 20 3a 3a 3d 20 4c 50 20   notexpr ::= LP 
34cbf 71 75 65 72 79 20 52 50 2e 0a 2a 2a 0a 2a 2a 20  query RP..**.** 
34cc0 20 20 6e 65 61 72 65 78 70 72 20 3a 3a 3d 20 70    nearexpr ::= p
34cc1 68 72 61 73 65 20 28 4e 45 41 52 20 64 69 73 74  hrase (NEAR dist
34cc2 61 6e 63 65 5f 6f 70 74 20 6e 65 61 72 65 78 70  ance_opt nearexp
34cc3 72 29 2a 2e 0a 2a 2a 0a 2a 2a 20 20 20 64 69 73  r)*..**.**   dis
34cc4 74 61 6e 63 65 5f 6f 70 74 20 3a 3a 3d 20 2e 0a  tance_opt ::= ..
34cc5 2a 2a 20 20 20 64 69 73 74 61 6e 63 65 5f 6f 70  **   distance_op
34cc6 74 20 3a 3a 3d 20 2f 20 49 4e 54 45 47 45 52 2e  t ::= / INTEGER.
34cc7 0a 2a 2a 0a 2a 2a 20 20 20 70 68 72 61 73 65 20  .**.**   phrase 
34cc8 3a 3a 3d 20 54 4f 4b 45 4e 2e 0a 2a 2a 20 20 20  ::= TOKEN..**   
34cc9 70 68 72 61 73 65 20 3a 3a 3d 20 43 4f 4c 55 4d  phrase ::= COLUM
34cca 4e 3a 54 4f 4b 45 4e 2e 0a 2a 2a 20 20 20 70 68  N:TOKEN..**   ph
34ccb 72 61 73 65 20 3a 3a 3d 20 22 54 4f 4b 45 4e 20  rase ::= "TOKEN 
34ccc 54 4f 4b 45 4e 20 54 4f 4b 45 4e 2e 2e 2e 22 2e  TOKEN TOKEN...".
34ccd 0a 2a 2f 0a 0a 74 79 70 65 64 65 66 20 73 74 72  .*/..typedef str
34cce 75 63 74 20 46 74 73 33 45 78 70 72 20 46 74 73  uct Fts3Expr Fts
34ccf 33 45 78 70 72 3b 0a 74 79 70 65 64 65 66 20 73  3Expr;.typedef s
34cd0 74 72 75 63 74 20 46 74 73 33 50 68 72 61 73 65  truct Fts3Phrase
34cd1 20 46 74 73 33 50 68 72 61 73 65 3b 0a 0a 2f 2a   Fts3Phrase;../*
34cd2 0a 2a 2a 20 41 20 22 70 68 72 61 73 65 22 20 69  .** A "phrase" i
34cd3 73 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66 20  s a sequence of 
34cd4 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 6f 6b 65  one or more toke
34cd5 6e 73 20 74 68 61 74 20 6d 75 73 74 20 6d 61 74  ns that must mat
34cd6 63 68 20 69 6e 0a 2a 2a 20 73 65 71 75 65 6e 63  ch in.** sequenc
34cd7 65 2e 20 20 41 20 73 69 6e 67 6c 65 20 74 6f 6b  e.  A single tok
34cd8 65 6e 20 69 73 20 74 68 65 20 62 61 73 65 20 63  en is the base c
34cd9 61 73 65 20 61 6e 64 20 74 68 65 20 6d 6f 73 74  ase and the most
34cda 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 2e 0a 2a 2a   common case..**
34cdb 20 46 6f 72 20 61 20 73 65 71 75 65 6e 63 65 20   For a sequence 
34cdc 6f 66 20 74 6f 6b 65 6e 73 20 63 6f 6e 74 61 69  of tokens contai
34cdd 6e 65 64 20 69 6e 20 22 2e 2e 2e 22 2c 20 6e 54  ned in "...", nT
34cde 6f 6b 65 6e 20 77 69 6c 6c 20 62 65 20 74 68 65  oken will be the
34cdf 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20 74 6f   number.** of to
34ce0 6b 65 6e 73 20 69 6e 20 74 68 65 20 73 74 72 69  kens in the stri
34ce1 6e 67 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 74  ng..*/.struct Ft
34ce2 73 33 50 68 72 61 73 65 20 7b 0a 20 20 69 6e 74  s3Phrase {.  int
34ce3 20 6e 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20   nToken;        
34ce4 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
34ce5 6f 6b 65 6e 73 20 69 6e 20 74 68 65 20 70 68 72  okens in the phr
34ce6 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f  ase */.  int iCo
34ce7 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a  lumn;         /*
34ce8 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75 6d 6e   Index of column
34ce9 20 74 68 69 73 20 70 68 72 61 73 65 20 6d 75 73   this phrase mus
34cea 74 20 6d 61 74 63 68 20 2a 2f 0a 20 20 69 6e 74  t match */.  int
34ceb 20 69 73 4e 6f 74 3b 20 20 20 20 20 20 20 20 20   isNot;         
34cec 20 20 2f 2a 20 50 68 72 61 73 65 20 70 72 65 66    /* Phrase pref
34ced 69 78 65 64 20 62 79 20 75 6e 61 72 79 20 6e 6f  ixed by unary no
34cee 74 20 28 2d 29 20 6f 70 65 72 61 74 6f 72 20 2a  t (-) operator *
34cef 2f 0a 20 20 73 74 72 75 63 74 20 50 68 72 61 73  /.  struct Phras
34cf0 65 54 6f 6b 65 6e 20 7b 0a 20 20 20 20 63 68 61  eToken {.    cha
34cf1 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20 20  r *z;           
34cf2 20 20 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68     /* Text of th
34cf3 65 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 69  e token */.    i
34cf4 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20  nt n;           
34cf5 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
34cf6 66 20 62 79 74 65 73 20 69 6e 20 62 75 66 66 65  f bytes in buffe
34cf7 72 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  r pointed to by 
34cf8 7a 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 73 50  z */.    int isP
34cf9 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 2f  refix;         /
34cfa 2a 20 54 72 75 65 20 69 66 20 74 6f 6b 65 6e 20  * True if token 
34cfb 65 6e 64 73 20 69 6e 20 77 69 74 68 20 61 20 22  ends in with a "
34cfc 2a 22 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a  *" character */.
34cfd 20 20 7d 20 61 54 6f 6b 65 6e 5b 31 5d 3b 20 20    } aToken[1];  
34cfe 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e         /* One en
34cff 74 72 79 20 66 6f 72 20 65 61 63 68 20 74 6f 6b  try for each tok
34d00 65 6e 20 69 6e 20 74 68 65 20 70 68 72 61 73 65  en in the phrase
34d01 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20   */.};../*.** A 
34d02 74 72 65 65 20 6f 66 20 74 68 65 73 65 20 6f 62  tree of these ob
34d03 6a 65 63 74 73 20 66 6f 72 6d 73 20 74 68 65 20  jects forms the 
34d04 52 48 53 20 6f 66 20 61 20 4d 41 54 43 48 20 6f  RHS of a MATCH o
34d05 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 72 75  perator..*/.stru
34d06 63 74 20 46 74 73 33 45 78 70 72 20 7b 0a 20 20  ct Fts3Expr {.  
34d07 69 6e 74 20 65 54 79 70 65 3b 20 20 20 20 20 20  int eType;      
34d08 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
34d09 65 20 6f 66 20 74 68 65 20 46 54 53 51 55 45 52  e of the FTSQUER
34d0a 59 5f 58 58 58 20 76 61 6c 75 65 73 20 64 65 66  Y_XXX values def
34d0b 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 20 20  ined below */.  
34d0c 69 6e 74 20 6e 4e 65 61 72 3b 20 20 20 20 20 20  int nNear;      
34d0d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 61             /* Va
34d0e 6c 69 64 20 69 66 20 65 54 79 70 65 3d 3d 46 54  lid if eType==FT
34d0f 53 51 55 45 52 59 5f 4e 45 41 52 20 2a 2f 0a 20  SQUERY_NEAR */. 
34d10 20 46 74 73 33 45 78 70 72 20 2a 70 50 61 72 65   Fts3Expr *pPare
34d11 6e 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 70  nt;         /* p
34d12 50 61 72 65 6e 74 2d 3e 70 4c 65 66 74 3d 3d 74  Parent->pLeft==t
34d13 68 69 73 20 6f 72 20 70 50 61 72 65 6e 74 2d 3e  his or pParent->
34d14 70 52 69 67 68 74 3d 3d 74 68 69 73 20 2a 2f 0a  pRight==this */.
34d15 20 20 46 74 73 33 45 78 70 72 20 2a 70 4c 65 66    Fts3Expr *pLef
34d16 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
34d17 4c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  Left operand */.
34d18 20 20 46 74 73 33 45 78 70 72 20 2a 70 52 69 67    Fts3Expr *pRig
34d19 68 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ht;          /* 
34d1a 52 69 67 68 74 20 6f 70 65 72 61 6e 64 20 2a 2f  Right operand */
34d1b 0a 20 20 46 74 73 33 50 68 72 61 73 65 20 2a 70  .  Fts3Phrase *p
34d1c 50 68 72 61 73 65 3b 20 20 20 20 20 20 20 2f 2a  Phrase;       /*
34d1d 20 56 61 6c 69 64 20 69 66 20 65 54 79 70 65 3d   Valid if eType=
34d1e 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45  =FTSQUERY_PHRASE
34d1f 20 2a 2f 0a 7d 3b 0a 0a 53 51 4c 49 54 45 5f 50   */.};..SQLITE_P
34d20 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
34d21 65 33 46 74 73 33 45 78 70 72 50 61 72 73 65 28  e3Fts3ExprParse(
34d22 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
34d23 72 20 2a 2c 20 63 68 61 72 20 2a 2a 2c 20 69 6e  r *, char **, in
34d24 74 2c 20 69 6e 74 2c 20 0a 20 20 20 20 20 20 20  t, int, .       
34d25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34d26 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20    const char *, 
34d27 69 6e 74 2c 20 46 74 73 33 45 78 70 72 20 2a 2a  int, Fts3Expr **
34d28 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
34d29 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74  E void sqlite3Ft
34d2a 73 33 45 78 70 72 46 72 65 65 28 46 74 73 33 45  s3ExprFree(Fts3E
34d2b 78 70 72 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  xpr *);../*.** C
34d2c 61 6e 64 69 64 61 74 65 20 76 61 6c 75 65 73 20  andidate values 
34d2d 66 6f 72 20 46 74 73 33 51 75 65 72 79 2e 65 54  for Fts3Query.eT
34d2e 79 70 65 2e 20 4e 6f 74 65 20 74 68 61 74 20 74  ype. Note that t
34d2f 68 65 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20  he order of the 
34d30 66 69 72 73 74 0a 2a 2a 20 66 6f 75 72 20 76 61  first.** four va
34d31 6c 75 65 73 20 69 73 20 69 6e 20 6f 72 64 65 72  lues is in order
34d32 20 6f 66 20 70 72 65 63 65 64 65 6e 63 65 20 77   of precedence w
34d33 68 65 6e 20 70 61 72 73 69 6e 67 20 65 78 70 72  hen parsing expr
34d34 65 73 73 69 6f 6e 73 2e 20 46 6f 72 20 0a 2a 2a  essions. For .**
34d35 20 65 78 61 6d 70 6c 65 2c 20 74 68 65 20 66 6f   example, the fo
34d36 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  llowing:.**.**  
34d37 20 22 61 20 4f 52 20 62 20 41 4e 44 20 63 20 4e   "a OR b AND c N
34d38 4f 54 20 64 20 4e 45 41 52 20 65 22 0a 2a 2a 0a  OT d NEAR e".**.
34d39 2a 2a 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74  ** is equivalent
34d3a 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 61 20   to:.**.**   "a 
34d3b 4f 52 20 28 62 20 41 4e 44 20 28 63 20 4e 4f 54  OR (b AND (c NOT
34d3c 20 28 64 20 4e 45 41 52 20 65 29 29 29 22 0a 2a   (d NEAR e)))".*
34d3d 2f 0a 23 64 65 66 69 6e 65 20 46 54 53 51 55 45  /.#define FTSQUE
34d3e 52 59 5f 4e 45 41 52 20 20 20 31 0a 23 64 65 66  RY_NEAR   1.#def
34d3f 69 6e 65 20 46 54 53 51 55 45 52 59 5f 4e 4f 54  ine FTSQUERY_NOT
34d40 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 46 54      2.#define FT
34d41 53 51 55 45 52 59 5f 41 4e 44 20 20 20 20 33 0a  SQUERY_AND    3.
34d42 23 64 65 66 69 6e 65 20 46 54 53 51 55 45 52 59  #define FTSQUERY
34d43 5f 4f 52 20 20 20 20 20 34 0a 23 64 65 66 69 6e  _OR     4.#defin
34d44 65 20 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  e FTSQUERY_PHRAS
34d45 45 20 35 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  E 5..#ifdef SQLI
34d46 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50  TE_TEST.SQLITE_P
34d47 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
34d48 74 65 33 46 74 73 33 45 78 70 72 49 6e 69 74 54  te3Fts3ExprInitT
34d49 65 73 74 49 6e 74 65 72 66 61 63 65 28 73 71 6c  estInterface(sql
34d4a 69 74 65 33 20 2a 64 62 29 3b 0a 23 65 6e 64 69  ite3 *db);.#endi
34d4b 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
34d4c 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 65  ** End of fts3_e
34d4d 78 70 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  xpr.h **********
34d4e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d50 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
34d51 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
34d52 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
34d53 69 6e 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a  in fts3.c ******
34d54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d55 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
34d56 2a 2a 20 49 6e 63 6c 75 64 65 20 66 74 73 33 5f  ** Include fts3_
34d57 68 61 73 68 2e 68 20 69 6e 20 74 68 65 20 6d 69  hash.h in the mi
34d58 64 64 6c 65 20 6f 66 20 66 74 73 33 2e 63 20 2a  ddle of fts3.c *
34d59 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d5a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
34d5b 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74  ** Begin file ft
34d5c 73 33 5f 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a  s3_hash.h ******
34d5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d5f 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
34d60 70 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a  ptember 22.**.**
34d61 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
34d62 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
34d63 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
34d64 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
34d65 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
34d66 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
34d67 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
34d68 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
34d69 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
34d6a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
34d6b 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
34d6c 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
34d6d 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
34d6e 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
34d6f 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
34d70 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
34d71 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
34d72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d73 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34d76 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
34d77 73 20 69 73 20 74 68 65 20 68 65 61 64 65 72 20  s is the header 
34d78 66 69 6c 65 20 66 6f 72 20 74 68 65 20 67 65 6e  file for the gen
34d79 65 72 69 63 20 68 61 73 68 2d 74 61 62 6c 65 20  eric hash-table 
34d7a 69 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 0a 2a 2a  implemenation.**
34d7b 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65 2e   used in SQLite.
34d7c 20 20 57 65 27 76 65 20 6d 6f 64 69 66 69 65 64    We've modified
34d7d 20 69 74 20 73 6c 69 67 68 74 6c 79 20 74 6f 20   it slightly to 
34d7e 73 65 72 76 65 20 61 73 20 61 20 73 74 61 6e 64  serve as a stand
34d7f 61 6c 6f 6e 65 0a 2a 2a 20 68 61 73 68 20 74 61  alone.** hash ta
34d80 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ble implementati
34d81 6f 6e 20 66 6f 72 20 74 68 65 20 66 75 6c 6c 2d  on for the full-
34d82 74 65 78 74 20 69 6e 64 65 78 69 6e 67 20 6d 6f  text indexing mo
34d83 64 75 6c 65 2e 0a 2a 2a 0a 2a 2f 0a 23 69 66 6e  dule..**.*/.#ifn
34d84 64 65 66 20 5f 46 54 53 33 5f 48 41 53 48 5f 48  def _FTS3_HASH_H
34d85 5f 0a 23 64 65 66 69 6e 65 20 5f 46 54 53 33 5f  _.#define _FTS3_
34d86 48 41 53 48 5f 48 5f 0a 0a 2f 2a 20 46 6f 72 77  HASH_H_../* Forw
34d87 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 73  ard declarations
34d88 20 6f 66 20 73 74 72 75 63 74 75 72 65 73 2e 20   of structures. 
34d89 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
34d8a 74 20 66 74 73 33 48 61 73 68 20 66 74 73 33 48  t fts3Hash fts3H
34d8b 61 73 68 3b 0a 74 79 70 65 64 65 66 20 73 74 72  ash;.typedef str
34d8c 75 63 74 20 66 74 73 33 48 61 73 68 45 6c 65 6d  uct fts3HashElem
34d8d 20 66 74 73 33 48 61 73 68 45 6c 65 6d 3b 0a 0a   fts3HashElem;..
34d8e 2f 2a 20 41 20 63 6f 6d 70 6c 65 74 65 20 68 61  /* A complete ha
34d8f 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e 20 69  sh table is an i
34d90 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
34d91 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
34d92 72 65 2e 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72  re..** The inter
34d93 6e 61 6c 73 20 6f 66 20 74 68 69 73 20 73 74 72  nals of this str
34d94 75 63 74 75 72 65 20 61 72 65 20 69 6e 74 65 6e  ucture are inten
34d95 64 65 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65  ded to be opaque
34d96 20 2d 2d 20 63 6c 69 65 6e 74 0a 2a 2a 20 63 6f   -- client.** co
34d97 64 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 61 74  de should not at
34d98 74 65 6d 70 74 20 74 6f 20 61 63 63 65 73 73 20  tempt to access 
34d99 6f 72 20 6d 6f 64 69 66 79 20 74 68 65 20 66 69  or modify the fi
34d9a 65 6c 64 73 20 6f 66 20 74 68 69 73 20 73 74 72  elds of this str
34d9b 75 63 74 75 72 65 0a 2a 2a 20 64 69 72 65 63 74  ucture.** direct
34d9c 6c 79 2e 20 20 43 68 61 6e 67 65 20 74 68 69 73  ly.  Change this
34d9d 20 73 74 72 75 63 74 75 72 65 20 6f 6e 6c 79 20   structure only 
34d9e 62 79 20 75 73 69 6e 67 20 74 68 65 20 72 6f 75  by using the rou
34d9f 74 69 6e 65 73 20 62 65 6c 6f 77 2e 0a 2a 2a 20  tines below..** 
34da0 48 6f 77 65 76 65 72 2c 20 6d 61 6e 79 20 6f 66  However, many of
34da1 20 74 68 65 20 22 70 72 6f 63 65 64 75 72 65 73   the "procedures
34da2 22 20 61 6e 64 20 22 66 75 6e 63 74 69 6f 6e 73  " and "functions
34da3 22 20 66 6f 72 20 6d 6f 64 69 66 79 69 6e 67 20  " for modifying 
34da4 61 6e 64 0a 2a 2a 20 61 63 63 65 73 73 69 6e 67  and.** accessing
34da5 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
34da6 61 72 65 20 72 65 61 6c 6c 79 20 6d 61 63 72 6f  are really macro
34da7 73 2c 20 73 6f 20 77 65 20 63 61 6e 27 74 20 72  s, so we can't r
34da8 65 61 6c 6c 79 20 6d 61 6b 65 0a 2a 2a 20 74 68  eally make.** th
34da9 69 73 20 73 74 72 75 63 74 75 72 65 20 6f 70 61  is structure opa
34daa 71 75 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 66  que..*/.struct f
34dab 74 73 33 48 61 73 68 20 7b 0a 20 20 63 68 61 72  ts3Hash {.  char
34dac 20 6b 65 79 43 6c 61 73 73 3b 20 20 20 20 20 20   keyClass;      
34dad 20 20 20 20 2f 2a 20 48 41 53 48 5f 49 4e 54 2c      /* HASH_INT,
34dae 20 5f 50 4f 49 4e 54 45 52 2c 20 5f 53 54 52 49   _POINTER, _STRI
34daf 4e 47 2c 20 5f 42 49 4e 41 52 59 20 2a 2f 0a 20  NG, _BINARY */. 
34db0 20 63 68 61 72 20 63 6f 70 79 4b 65 79 3b 20 20   char copyKey;  
34db1 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
34db2 20 69 66 20 63 6f 70 79 20 6f 66 20 6b 65 79 20   if copy of key 
34db3 6d 61 64 65 20 6f 6e 20 69 6e 73 65 72 74 20 2a  made on insert *
34db4 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20  /.  int count;  
34db5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
34db6 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
34db7 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a   in this table *
34db8 2f 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d  /.  fts3HashElem
34db9 20 2a 66 69 72 73 74 3b 20 20 20 20 2f 2a 20 54   *first;    /* T
34dba 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74  he first element
34dbb 20 6f 66 20 74 68 65 20 61 72 72 61 79 20 2a 2f   of the array */
34dbc 0a 20 20 69 6e 74 20 68 74 73 69 7a 65 3b 20 20  .  int htsize;  
34dbd 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
34dbe 6d 62 65 72 20 6f 66 20 62 75 63 6b 65 74 73 20  mber of buckets 
34dbf 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  in the hash tabl
34dc0 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f 66  e */.  struct _f
34dc1 74 73 33 68 74 20 7b 20 20 20 20 20 20 20 20 2f  ts3ht {        /
34dc2 2a 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  * the hash table
34dc3 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 6f 75 6e   */.    int coun
34dc4 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
34dc5 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
34dc6 74 72 69 65 73 20 77 69 74 68 20 74 68 69 73 20  tries with this 
34dc7 68 61 73 68 20 2a 2f 0a 20 20 20 20 66 74 73 33  hash */.    fts3
34dc8 48 61 73 68 45 6c 65 6d 20 2a 63 68 61 69 6e 3b  HashElem *chain;
34dc9 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
34dca 74 6f 20 66 69 72 73 74 20 65 6e 74 72 79 20 77  to first entry w
34dcb 69 74 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f  ith this hash */
34dcc 0a 20 20 7d 20 2a 68 74 3b 0a 7d 3b 0a 0a 2f 2a  .  } *ht;.};../*
34dcd 20 45 61 63 68 20 65 6c 65 6d 65 6e 74 20 69 6e   Each element in
34dce 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
34dcf 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  is an instance o
34dd0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
34dd1 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20  .** structure.  
34dd2 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 61 72 65  All elements are
34dd3 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 73 69 6e   stored on a sin
34dd4 67 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 6e 6b 65  gle doubly-linke
34dd5 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41 67  d list..**.** Ag
34dd6 61 69 6e 2c 20 74 68 69 73 20 73 74 72 75 63 74  ain, this struct
34dd7 75 72 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20  ure is intended 
34dd8 74 6f 20 62 65 20 6f 70 61 71 75 65 2c 20 62 75  to be opaque, bu
34dd9 74 20 69 74 20 63 61 6e 27 74 20 72 65 61 6c 6c  t it can't reall
34dda 79 0a 2a 2a 20 62 65 20 6f 70 61 71 75 65 20 62  y.** be opaque b
34ddb 65 63 61 75 73 65 20 69 74 20 69 73 20 75 73 65  ecause it is use
34ddc 64 20 62 79 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a  d by macros..*/.
34ddd 73 74 72 75 63 74 20 66 74 73 33 48 61 73 68 45  struct fts3HashE
34dde 6c 65 6d 20 7b 0a 20 20 66 74 73 33 48 61 73 68  lem {.  fts3Hash
34ddf 45 6c 65 6d 20 2a 6e 65 78 74 2c 20 2a 70 72 65  Elem *next, *pre
34de0 76 3b 20 2f 2a 20 4e 65 78 74 20 61 6e 64 20 70  v; /* Next and p
34de1 72 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74 73  revious elements
34de2 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 2a 2f   in the table */
34de3 0a 20 20 76 6f 69 64 20 2a 64 61 74 61 3b 20 20  .  void *data;  
34de4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
34de5 20 44 61 74 61 20 61 73 73 6f 63 69 61 74 65 64   Data associated
34de6 20 77 69 74 68 20 74 68 69 73 20 65 6c 65 6d 65   with this eleme
34de7 6e 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4b  nt */.  void *pK
34de8 65 79 3b 20 69 6e 74 20 6e 4b 65 79 3b 20 20 20  ey; int nKey;   
34de9 20 20 20 2f 2a 20 4b 65 79 20 61 73 73 6f 63 69     /* Key associ
34dea 61 74 65 64 20 77 69 74 68 20 74 68 69 73 20 65  ated with this e
34deb 6c 65 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  lement */.};../*
34dec 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20 32 20  .** There are 2 
34ded 64 69 66 66 65 72 65 6e 74 20 6d 6f 64 65 73 20  different modes 
34dee 6f 66 20 6f 70 65 72 61 74 69 6f 6e 20 66 6f 72  of operation for
34def 20 61 20 68 61 73 68 20 74 61 62 6c 65 3a 0a 2a   a hash table:.*
34df0 2a 0a 2a 2a 20 20 20 46 54 53 33 5f 48 41 53 48  *.**   FTS3_HASH
34df1 5f 53 54 52 49 4e 47 20 20 20 20 20 20 20 20 70  _STRING        p
34df2 4b 65 79 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  Key points to a 
34df3 73 74 72 69 6e 67 20 74 68 61 74 20 69 73 20 6e  string that is n
34df4 4b 65 79 20 62 79 74 65 73 20 6c 6f 6e 67 0a 2a  Key bytes long.*
34df5 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
34df6 20 20 20 20 20 20 20 20 20 20 20 20 28 69 6e 63              (inc
34df7 6c 75 64 69 6e 67 20 74 68 65 20 6e 75 6c 6c 2d  luding the null-
34df8 74 65 72 6d 69 6e 61 74 6f 72 2c 20 69 66 20 61  terminator, if a
34df9 6e 79 29 2e 20 20 43 61 73 65 0a 2a 2a 20 20 20  ny).  Case.**   
34dfa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34dfb 20 20 20 20 20 20 20 20 69 73 20 72 65 73 70 65          is respe
34dfc 63 74 65 64 20 69 6e 20 63 6f 6d 70 61 72 69 73  cted in comparis
34dfd 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 46 54 53  ons..**.**   FTS
34dfe 33 5f 48 41 53 48 5f 42 49 4e 41 52 59 20 20 20  3_HASH_BINARY   
34dff 20 20 20 20 20 70 4b 65 79 20 70 6f 69 6e 74 73       pKey points
34e00 20 74 6f 20 62 69 6e 61 72 79 20 64 61 74 61 20   to binary data 
34e01 6e 4b 65 79 20 62 79 74 65 73 20 6c 6f 6e 67 2e  nKey bytes long.
34e02 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20   .**            
34e03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d                 m
34e04 65 6d 63 6d 70 28 29 20 69 73 20 75 73 65 64 20  emcmp() is used 
34e05 74 6f 20 63 6f 6d 70 61 72 65 20 6b 65 79 73 2e  to compare keys.
34e06 0a 2a 2a 0a 2a 2a 20 41 20 63 6f 70 79 20 6f 66  .**.** A copy of
34e07 20 74 68 65 20 6b 65 79 20 69 73 20 6d 61 64 65   the key is made
34e08 20 69 66 20 74 68 65 20 63 6f 70 79 4b 65 79 20   if the copyKey 
34e09 70 61 72 61 6d 65 74 65 72 20 74 6f 20 66 74 73  parameter to fts
34e0a 33 48 61 73 68 49 6e 69 74 20 69 73 20 31 2e 20  3HashInit is 1. 
34e0b 20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 54 53   .*/.#define FTS
34e0c 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 20 20 20  3_HASH_STRING   
34e0d 20 31 0a 23 64 65 66 69 6e 65 20 46 54 53 33 5f   1.#define FTS3_
34e0e 48 41 53 48 5f 42 49 4e 41 52 59 20 20 20 20 32  HASH_BINARY    2
34e0f 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65 73 73 20 72  ../*.** Access r
34e10 6f 75 74 69 6e 65 73 2e 20 20 54 6f 20 64 65 6c  outines.  To del
34e11 65 74 65 2c 20 69 6e 73 65 72 74 20 61 20 4e 55  ete, insert a NU
34e12 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53  LL pointer..*/.S
34e13 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
34e14 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 48 61  id sqlite3Fts3Ha
34e15 73 68 49 6e 69 74 28 66 74 73 33 48 61 73 68 2a  shInit(fts3Hash*
34e16 2c 20 69 6e 74 20 6b 65 79 74 79 70 65 2c 20 69  , int keytype, i
34e17 6e 74 20 63 6f 70 79 4b 65 79 29 3b 0a 53 51 4c  nt copyKey);.SQL
34e18 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
34e19 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48 61 73   *sqlite3Fts3Has
34e1a 68 49 6e 73 65 72 74 28 66 74 73 33 48 61 73 68  hInsert(fts3Hash
34e1b 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  *, const void *p
34e1c 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20 76  Key, int nKey, v
34e1d 6f 69 64 20 2a 70 44 61 74 61 29 3b 0a 53 51 4c  oid *pData);.SQL
34e1e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
34e1f 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48 61 73   *sqlite3Fts3Has
34e20 68 46 69 6e 64 28 63 6f 6e 73 74 20 66 74 73 33  hFind(const fts3
34e21 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20 76 6f 69  Hash*, const voi
34e22 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65  d *pKey, int nKe
34e23 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  y);.SQLITE_PRIVA
34e24 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
34e25 74 73 33 48 61 73 68 43 6c 65 61 72 28 66 74 73  ts3HashClear(fts
34e26 33 48 61 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  3Hash*);../*.** 
34e27 53 68 6f 72 74 68 61 6e 64 20 66 6f 72 20 74 68  Shorthand for th
34e28 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 62 6f 76  e functions abov
34e29 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 74 73  e.*/.#define fts
34e2a 33 48 61 73 68 49 6e 69 74 20 20 20 73 71 6c 69  3HashInit   sqli
34e2b 74 65 33 46 74 73 33 48 61 73 68 49 6e 69 74 0a  te3Fts3HashInit.
34e2c 23 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68  #define fts3Hash
34e2d 49 6e 73 65 72 74 20 73 71 6c 69 74 65 33 46 74  Insert sqlite3Ft
34e2e 73 33 48 61 73 68 49 6e 73 65 72 74 0a 23 64 65  s3HashInsert.#de
34e2f 66 69 6e 65 20 66 74 73 33 48 61 73 68 46 69 6e  fine fts3HashFin
34e30 64 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 48  d   sqlite3Fts3H
34e31 61 73 68 46 69 6e 64 0a 23 64 65 66 69 6e 65 20  ashFind.#define 
34e32 66 74 73 33 48 61 73 68 43 6c 65 61 72 20 20 73  fts3HashClear  s
34e33 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 43 6c  qlite3Fts3HashCl
34e34 65 61 72 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f  ear../*.** Macro
34e35 73 20 66 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76  s for looping ov
34e36 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74 73 20  er all elements 
34e37 6f 66 20 61 20 68 61 73 68 20 74 61 62 6c 65 2e  of a hash table.
34e38 20 20 54 68 65 20 69 64 69 6f 6d 20 69 73 0a 2a    The idiom is.*
34e39 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a  * like this:.**.
34e3a 2a 2a 20 20 20 66 74 73 33 48 61 73 68 20 68 3b  **   fts3Hash h;
34e3b 0a 2a 2a 20 20 20 66 74 73 33 48 61 73 68 45 6c  .**   fts3HashEl
34e3c 65 6d 20 2a 70 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a  em *p;.**   ....
34e3d 2a 2a 20 20 20 66 6f 72 28 70 3d 66 74 73 33 48  **   for(p=fts3H
34e3e 61 73 68 46 69 72 73 74 28 26 68 29 3b 20 70 3b  ashFirst(&h); p;
34e3f 20 70 3d 66 74 73 33 48 61 73 68 4e 65 78 74 28   p=fts3HashNext(
34e40 70 29 29 7b 0a 2a 2a 20 20 20 20 20 53 6f 6d 65  p)){.**     Some
34e41 53 74 72 75 63 74 75 72 65 20 2a 70 44 61 74 61  Structure *pData
34e42 20 3d 20 66 74 73 33 48 61 73 68 44 61 74 61 28   = fts3HashData(
34e43 70 29 3b 0a 2a 2a 20 20 20 20 20 2f 2f 20 64 6f  p);.**     // do
34e44 20 73 6f 6d 65 74 68 69 6e 67 20 77 69 74 68 20   something with 
34e45 70 44 61 74 61 0a 2a 2a 20 20 20 7d 0a 2a 2f 0a  pData.**   }.*/.
34e46 23 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68  #define fts3Hash
34e47 46 69 72 73 74 28 48 29 20 20 28 28 48 29 2d 3e  First(H)  ((H)->
34e48 66 69 72 73 74 29 0a 23 64 65 66 69 6e 65 20 66  first).#define f
34e49 74 73 33 48 61 73 68 4e 65 78 74 28 45 29 20 20  ts3HashNext(E)  
34e4a 20 28 28 45 29 2d 3e 6e 65 78 74 29 0a 23 64 65   ((E)->next).#de
34e4b 66 69 6e 65 20 66 74 73 33 48 61 73 68 44 61 74  fine fts3HashDat
34e4c 61 28 45 29 20 20 20 28 28 45 29 2d 3e 64 61 74  a(E)   ((E)->dat
34e4d 61 29 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48  a).#define fts3H
34e4e 61 73 68 4b 65 79 28 45 29 20 20 20 20 28 28 45  ashKey(E)    ((E
34e4f 29 2d 3e 70 4b 65 79 29 0a 23 64 65 66 69 6e 65  )->pKey).#define
34e50 20 66 74 73 33 48 61 73 68 4b 65 79 73 69 7a 65   fts3HashKeysize
34e51 28 45 29 20 28 28 45 29 2d 3e 6e 4b 65 79 29 0a  (E) ((E)->nKey).
34e52 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66  ./*.** Number of
34e53 20 65 6e 74 72 69 65 73 20 69 6e 20 61 20 68 61   entries in a ha
34e54 73 68 20 74 61 62 6c 65 0a 2a 2f 0a 23 64 65 66  sh table.*/.#def
34e55 69 6e 65 20 66 74 73 33 48 61 73 68 43 6f 75 6e  ine fts3HashCoun
34e56 74 28 48 29 20 20 28 28 48 29 2d 3e 63 6f 75 6e  t(H)  ((H)->coun
34e57 74 29 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 46  t)..#endif /* _F
34e58 54 53 33 5f 48 41 53 48 5f 48 5f 20 2a 2f 0a 0a  TS3_HASH_H_ */..
34e59 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
34e5a 45 6e 64 20 6f 66 20 66 74 73 33 5f 68 61 73 68  End of fts3_hash
34e5b 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
34e5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34e5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
34e5e 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
34e5f 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
34e60 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
34e61 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  fts3.c *********
34e62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
34e63 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43  #ifndef SQLITE_C
34e64 4f 52 45 20 0a 20 20 53 51 4c 49 54 45 5f 45 58  ORE .  SQLITE_EX
34e65 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 31 0a 23 65  TENSION_INIT1.#e
34e66 6e 64 69 66 0a 0a 0a 2f 2a 20 54 4f 44 4f 28 73  ndif.../* TODO(s
34e67 68 65 73 73 29 20 4d 41 4e 2c 20 74 68 69 73 20  hess) MAN, this 
34e68 74 68 69 6e 67 20 6e 65 65 64 73 20 73 6f 6d 65  thing needs some
34e69 20 72 65 66 61 63 74 6f 72 69 6e 67 2e 20 20 41   refactoring.  A
34e6a 74 20 6d 69 6e 69 6d 75 6d 2c 20 69 74 0a 2a 2a  t minimum, it.**
34e6b 20 77 6f 75 6c 64 20 62 65 20 6e 69 63 65 20 74   would be nice t
34e6c 6f 20 6f 72 64 65 72 20 74 68 65 20 66 69 6c 65  o order the file
34e6d 20 62 65 74 74 65 72 2c 20 70 65 72 68 61 70 73   better, perhaps
34e6e 20 73 6f 6d 65 74 68 69 6e 67 20 61 6c 6f 6e 67   something along
34e6f 20 74 68 65 0a 2a 2a 20 6c 69 6e 65 73 20 6f 66   the.** lines of
34e70 3a 0a 2a 2a 0a 2a 2a 20 20 2d 20 75 74 69 6c 69  :.**.**  - utili
34e71 74 79 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20  ty functions.** 
34e72 20 2d 20 74 61 62 6c 65 20 73 65 74 75 70 20 66   - table setup f
34e73 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 20 2d 20 74  unctions.**  - t
34e74 61 62 6c 65 20 75 70 64 61 74 65 20 66 75 6e 63  able update func
34e75 74 69 6f 6e 73 0a 2a 2a 20 20 2d 20 74 61 62 6c  tions.**  - tabl
34e76 65 20 71 75 65 72 79 20 66 75 6e 63 74 69 6f 6e  e query function
34e77 73 0a 2a 2a 0a 2a 2a 20 50 75 74 20 74 68 65 20  s.**.** Put the 
34e78 71 75 65 72 79 20 66 75 6e 63 74 69 6f 6e 73 20  query functions 
34e79 6c 61 73 74 20 62 65 63 61 75 73 65 20 74 68 65  last because the
34e7a 79 27 72 65 20 6c 69 6b 65 6c 79 20 74 6f 20 72  y're likely to r
34e7b 65 66 65 72 65 6e 63 65 0a 2a 2a 20 74 79 70 65  eference.** type
34e7c 64 65 66 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e  defs or function
34e7d 73 20 66 72 6f 6d 20 74 68 65 20 74 61 62 6c 65  s from the table
34e7e 20 75 70 64 61 74 65 20 73 65 63 74 69 6f 6e 2e   update section.
34e7f 0a 2a 2f 0a 0a 23 69 66 20 30 0a 23 20 64 65 66  .*/..#if 0.# def
34e80 69 6e 65 20 46 54 53 54 52 41 43 45 28 41 29 20  ine FTSTRACE(A) 
34e81 20 70 72 69 6e 74 66 20 41 3b 20 66 66 6c 75 73   printf A; fflus
34e82 68 28 73 74 64 6f 75 74 29 0a 23 65 6c 73 65 0a  h(stdout).#else.
34e83 23 20 64 65 66 69 6e 65 20 46 54 53 54 52 41 43  # define FTSTRAC
34e84 45 28 41 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20  E(A).#endif../* 
34e85 49 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74  It is not safe t
34e86 6f 20 63 61 6c 6c 20 69 73 73 70 61 63 65 28 29  o call isspace()
34e87 2c 20 74 6f 6c 6f 77 65 72 28 29 2c 20 6f 72 20  , tolower(), or 
34e88 69 73 61 6c 6e 75 6d 28 29 20 6f 6e 0a 2a 2a 20  isalnum() on.** 
34e89 68 69 2d 62 69 74 2d 73 65 74 20 63 68 61 72 61  hi-bit-set chara
34e8a 63 74 65 72 73 2e 20 20 54 68 69 73 20 69 73 20  cters.  This is 
34e8b 74 68 65 20 73 61 6d 65 20 73 6f 6c 75 74 69 6f  the same solutio
34e8c 6e 20 75 73 65 64 20 69 6e 20 74 68 65 0a 2a 2a  n used in the.**
34e8d 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2f 0a 2f   tokenizer..*/./
34e8e 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68  * TODO(shess) Th
34e8f 65 20 73 6e 69 70 70 65 74 2d 67 65 6e 65 72 61  e snippet-genera
34e90 74 69 6f 6e 20 63 6f 64 65 20 73 68 6f 75 6c 64  tion code should
34e91 20 62 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a   be using the.**
34e92 20 74 6f 6b 65 6e 69 7a 65 72 2d 67 65 6e 65 72   tokenizer-gener
34e93 61 74 65 64 20 74 6f 6b 65 6e 73 20 72 61 74 68  ated tokens rath
34e94 65 72 20 74 68 61 6e 20 64 6f 69 6e 67 20 69 74  er than doing it
34e95 73 20 6f 77 6e 20 6c 6f 63 61 6c 0a 2a 2a 20 74  s own local.** t
34e96 6f 6b 65 6e 69 7a 61 74 69 6f 6e 2e 0a 2a 2f 0a  okenization..*/.
34e97 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 49  /* TODO(shess) I
34e98 73 20 5f 5f 69 73 61 73 63 69 69 28 29 20 61 20  s __isascii() a 
34e99 70 6f 72 74 61 62 6c 65 20 76 65 72 73 69 6f 6e  portable version
34e9a 20 6f 66 20 28 63 26 30 78 38 30 29 3d 3d 30 3f   of (c&0x80)==0?
34e9b 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73   */.static int s
34e9c 61 66 65 5f 69 73 73 70 61 63 65 28 63 68 61 72  afe_isspace(char
34e9d 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 63   c){.  return (c
34e9e 26 30 78 38 30 29 3d 3d 30 20 3f 20 69 73 73 70  &0x80)==0 ? issp
34e9f 61 63 65 28 63 29 20 3a 20 30 3b 0a 7d 0a 73 74  ace(c) : 0;.}.st
34ea0 61 74 69 63 20 69 6e 74 20 73 61 66 65 5f 74 6f  atic int safe_to
34ea1 6c 6f 77 65 72 28 63 68 61 72 20 63 29 7b 0a 20  lower(char c){. 
34ea2 20 72 65 74 75 72 6e 20 28 63 26 30 78 38 30 29   return (c&0x80)
34ea3 3d 3d 30 20 3f 20 74 6f 6c 6f 77 65 72 28 63 29  ==0 ? tolower(c)
34ea4 20 3a 20 63 3b 0a 7d 0a 73 74 61 74 69 63 20 69   : c;.}.static i
34ea5 6e 74 20 73 61 66 65 5f 69 73 61 6c 6e 75 6d 28  nt safe_isalnum(
34ea6 63 68 61 72 20 63 29 7b 0a 20 20 72 65 74 75 72  char c){.  retur
34ea7 6e 20 28 63 26 30 78 38 30 29 3d 3d 30 20 3f 20  n (c&0x80)==0 ? 
34ea8 69 73 61 6c 6e 75 6d 28 63 29 20 3a 20 30 3b 0a  isalnum(c) : 0;.
34ea9 7d 0a 0a 74 79 70 65 64 65 66 20 65 6e 75 6d 20  }..typedef enum 
34eaa 44 6f 63 4c 69 73 74 54 79 70 65 20 7b 0a 20 20  DocListType {.  
34eab 44 4c 5f 44 4f 43 49 44 53 2c 20 20 20 20 20 20  DL_DOCIDS,      
34eac 20 20 20 20 20 20 20 20 2f 2a 20 64 6f 63 69 64          /* docid
34ead 73 20 6f 6e 6c 79 20 2a 2f 0a 20 20 44 4c 5f 50  s only */.  DL_P
34eae 4f 53 49 54 49 4f 4e 53 2c 20 20 20 20 20 20 20  OSITIONS,       
34eaf 20 20 20 20 2f 2a 20 64 6f 63 69 64 73 20 2b 20      /* docids + 
34eb0 70 6f 73 69 74 69 6f 6e 73 20 2a 2f 0a 20 20 44  positions */.  D
34eb1 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53  L_POSITIONS_OFFS
34eb2 45 54 53 20 20 20 20 2f 2a 20 64 6f 63 69 64 73  ETS    /* docids
34eb3 20 2b 20 70 6f 73 69 74 69 6f 6e 73 20 2b 20 6f   + positions + o
34eb4 66 66 73 65 74 73 20 2a 2f 0a 7d 20 44 6f 63 4c  ffsets */.} DocL
34eb5 69 73 74 54 79 70 65 3b 0a 0a 2f 2a 0a 2a 2a 20  istType;../*.** 
34eb6 42 79 20 64 65 66 61 75 6c 74 2c 20 6f 6e 6c 79  By default, only
34eb7 20 70 6f 73 69 74 69 6f 6e 73 20 61 6e 64 20 6e   positions and n
34eb8 6f 74 20 6f 66 66 73 65 74 73 20 61 72 65 20 73  ot offsets are s
34eb9 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64 6f 63  tored in the doc
34eba 6c 69 73 74 73 2e 0a 2a 2a 20 54 6f 20 63 68 61  lists..** To cha
34ebb 6e 67 65 20 74 68 69 73 20 73 6f 20 74 68 61 74  nge this so that
34ebc 20 6f 66 66 73 65 74 73 20 61 72 65 20 73 74 6f   offsets are sto
34ebd 72 65 64 20 74 6f 6f 2c 20 63 6f 6d 70 69 6c 65  red too, compile
34ebe 20 77 69 74 68 0a 2a 2a 0a 2a 2a 20 20 20 20 20   with.**.**     
34ebf 20 20 20 20 20 2d 44 44 4c 5f 44 45 46 41 55 4c       -DDL_DEFAUL
34ec0 54 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f  T=DL_POSITIONS_O
34ec1 46 46 53 45 54 53 0a 2a 2a 0a 2a 2a 20 49 66 20  FFSETS.**.** If 
34ec2 44 4c 5f 44 45 46 41 55 4c 54 20 69 73 20 73 65  DL_DEFAULT is se
34ec3 74 20 74 6f 20 44 4c 5f 44 4f 43 49 44 53 2c 20  t to DL_DOCIDS, 
34ec4 79 6f 75 72 20 74 61 62 6c 65 20 63 61 6e 20 6f  your table can o
34ec5 6e 6c 79 20 62 65 20 69 6e 73 65 72 74 65 64 0a  nly be inserted.
34ec6 2a 2a 20 69 6e 74 6f 20 28 6e 6f 20 64 65 6c 65  ** into (no dele
34ec7 74 65 73 20 6f 72 20 75 70 64 61 74 65 73 29 2e  tes or updates).
34ec8 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 44 4c 5f 44  .*/.#ifndef DL_D
34ec9 45 46 41 55 4c 54 0a 23 20 64 65 66 69 6e 65 20  EFAULT.# define 
34eca 44 4c 5f 44 45 46 41 55 4c 54 20 44 4c 5f 50 4f  DL_DEFAULT DL_PO
34ecb 53 49 54 49 4f 4e 53 0a 23 65 6e 64 69 66 0a 0a  SITIONS.#endif..
34ecc 65 6e 75 6d 20 7b 0a 20 20 50 4f 53 5f 45 4e 44  enum {.  POS_END
34ecd 20 3d 20 30 2c 20 20 20 20 20 20 20 20 2f 2a 20   = 0,        /* 
34ece 65 6e 64 20 6f 66 20 74 68 69 73 20 70 6f 73 69  end of this posi
34ecf 74 69 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 50  tion list */.  P
34ed0 4f 53 5f 43 4f 4c 55 4d 4e 2c 20 20 20 20 20 20  OS_COLUMN,      
34ed1 20 20 20 2f 2a 20 66 6f 6c 6c 6f 77 65 64 20 62     /* followed b
34ed2 79 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 6e 75 6d  y new column num
34ed3 62 65 72 20 2a 2f 0a 20 20 50 4f 53 5f 42 41 53  ber */.  POS_BAS
34ed4 45 0a 7d 3b 0a 0a 2f 2a 20 4d 45 52 47 45 5f 43  E.};../* MERGE_C
34ed5 4f 55 4e 54 20 63 6f 6e 74 72 6f 6c 73 20 68 6f  OUNT controls ho
34ed6 77 20 6f 66 74 65 6e 20 77 65 20 6d 65 72 67 65  w often we merge
34ed7 20 73 65 67 6d 65 6e 74 73 20 28 73 65 65 20 63   segments (see c
34ed8 6f 6d 6d 65 6e 74 20 61 74 0a 2a 2a 20 74 6f 70  omment at.** top
34ed9 20 6f 66 20 66 69 6c 65 29 2e 0a 2a 2f 0a 23 64   of file)..*/.#d
34eda 65 66 69 6e 65 20 4d 45 52 47 45 5f 43 4f 55 4e  efine MERGE_COUN
34edb 54 20 31 36 0a 0a 2f 2a 20 75 74 69 6c 69 74 79  T 16../* utility
34edc 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 0a 2f   functions */../
34edd 2a 20 43 4c 45 41 52 28 29 20 61 6e 64 20 53 43  * CLEAR() and SC
34ede 52 41 4d 42 4c 45 28 29 20 61 62 73 74 72 61 63  RAMBLE() abstrac
34edf 74 20 6d 65 6d 73 65 74 28 29 20 6f 6e 20 61 20  t memset() on a 
34ee0 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 69 6e  pointer to a sin
34ee1 67 6c 65 0a 2a 2a 20 72 65 63 6f 72 64 20 74 6f  gle.** record to
34ee2 20 70 72 65 76 65 6e 74 20 65 72 72 6f 72 73 20   prevent errors 
34ee3 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a  of the form:.**.
34ee4 2a 2a 20 6d 79 5f 66 75 6e 63 74 69 6f 6e 28 53  ** my_function(S
34ee5 6f 6d 65 54 79 70 65 20 2a 62 29 7b 0a 2a 2a 20  omeType *b){.** 
34ee6 20 20 6d 65 6d 73 65 74 28 62 2c 20 27 5c 30 27    memset(b, '\0'
34ee7 2c 20 73 69 7a 65 6f 66 28 62 29 29 3b 20 20 2f  , sizeof(b));  /
34ee8 2f 20 73 69 7a 65 6f 66 28 62 29 21 3d 73 69 7a  / sizeof(b)!=siz
34ee9 65 6f 66 28 2a 62 29 0a 2a 2a 20 7d 0a 2a 2f 0a  eof(*b).** }.*/.
34eea 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 4f  /* TODO(shess) O
34eeb 62 76 69 6f 75 73 20 63 61 6e 64 69 64 61 74 65  bvious candidate
34eec 73 20 66 6f 72 20 61 20 68 65 61 64 65 72 20 66  s for a header f
34eed 69 6c 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ile. */.#define 
34eee 43 4c 45 41 52 28 62 29 20 6d 65 6d 73 65 74 28  CLEAR(b) memset(
34eef 62 2c 20 27 5c 30 27 2c 20 73 69 7a 65 6f 66 28  b, '\0', sizeof(
34ef0 2a 28 62 29 29 29 0a 0a 23 69 66 6e 64 65 66 20  *(b)))..#ifndef 
34ef1 4e 44 45 42 55 47 0a 23 20 20 64 65 66 69 6e 65  NDEBUG.#  define
34ef2 20 53 43 52 41 4d 42 4c 45 28 62 29 20 6d 65 6d   SCRAMBLE(b) mem
34ef3 73 65 74 28 62 2c 20 30 78 35 35 2c 20 73 69 7a  set(b, 0x55, siz
34ef4 65 6f 66 28 2a 28 62 29 29 29 0a 23 65 6c 73 65  eof(*(b))).#else
34ef5 0a 23 20 20 64 65 66 69 6e 65 20 53 43 52 41 4d  .#  define SCRAM
34ef6 42 4c 45 28 62 29 0a 23 65 6e 64 69 66 0a 0a 2f  BLE(b).#endif../
34ef7 2a 20 57 65 20 6d 61 79 20 6e 65 65 64 20 75 70  * We may need up
34ef8 20 74 6f 20 56 41 52 49 4e 54 5f 4d 41 58 20 62   to VARINT_MAX b
34ef9 79 74 65 73 20 74 6f 20 73 74 6f 72 65 20 61 6e  ytes to store an
34efa 20 65 6e 63 6f 64 65 64 20 36 34 2d 62 69 74 20   encoded 64-bit 
34efb 69 6e 74 65 67 65 72 2e 20 2a 2f 0a 23 64 65 66  integer. */.#def
34efc 69 6e 65 20 56 41 52 49 4e 54 5f 4d 41 58 20 31  ine VARINT_MAX 1
34efd 30 0a 0a 2f 2a 20 57 72 69 74 65 20 61 20 36 34  0../* Write a 64
34efe 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65  -bit variable-le
34eff 6e 67 74 68 20 69 6e 74 65 67 65 72 20 74 6f 20  ngth integer to 
34f00 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20  memory starting 
34f01 61 74 20 70 5b 30 5d 2e 0a 20 2a 20 54 68 65 20  at p[0].. * The 
34f02 6c 65 6e 67 74 68 20 6f 66 20 64 61 74 61 20 77  length of data w
34f03 72 69 74 74 65 6e 20 77 69 6c 6c 20 62 65 20 62  ritten will be b
34f04 65 74 77 65 65 6e 20 31 20 61 6e 64 20 56 41 52  etween 1 and VAR
34f05 49 4e 54 5f 4d 41 58 20 62 79 74 65 73 2e 0a 20  INT_MAX bytes.. 
34f06 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * The number of 
34f07 62 79 74 65 73 20 77 72 69 74 74 65 6e 20 69 73  bytes written is
34f08 20 72 65 74 75 72 6e 65 64 2e 20 2a 2f 0a 73 74   returned. */.st
34f09 61 74 69 63 20 69 6e 74 20 66 74 73 33 50 75 74  atic int fts3Put
34f0a 56 61 72 69 6e 74 28 63 68 61 72 20 2a 70 2c 20  Varint(char *p, 
34f0b 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 76 29 7b  sqlite_int64 v){
34f0c 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
34f0d 20 2a 71 20 3d 20 28 75 6e 73 69 67 6e 65 64 20   *q = (unsigned 
34f0e 63 68 61 72 20 2a 29 20 70 3b 0a 20 20 73 71 6c  char *) p;.  sql
34f0f 69 74 65 5f 75 69 6e 74 36 34 20 76 75 20 3d 20  ite_uint64 vu = 
34f10 76 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 2a 71 2b  v;.  do{.    *q+
34f11 2b 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  + = (unsigned ch
34f12 61 72 29 20 28 28 76 75 20 26 20 30 78 37 66 29  ar) ((vu & 0x7f)
34f13 20 7c 20 30 78 38 30 29 3b 0a 20 20 20 20 76 75   | 0x80);.    vu
34f14 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65   >>= 7;.  }while
34f15 28 20 76 75 21 3d 30 20 29 3b 0a 20 20 71 5b 2d  ( vu!=0 );.  q[-
34f16 31 5d 20 26 3d 20 30 78 37 66 3b 20 20 2f 2a 20  1] &= 0x7f;  /* 
34f17 74 75 72 6e 20 6f 66 66 20 68 69 67 68 20 62 69  turn off high bi
34f18 74 20 69 6e 20 66 69 6e 61 6c 20 62 79 74 65 20  t in final byte 
34f19 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 71 20 2d  */.  assert( q -
34f1a 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
34f1b 2a 29 70 20 3c 3d 20 56 41 52 49 4e 54 5f 4d 41  *)p <= VARINT_MA
34f1c 58 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 69  X );.  return (i
34f1d 6e 74 29 20 28 71 20 2d 20 28 75 6e 73 69 67 6e  nt) (q - (unsign
34f1e 65 64 20 63 68 61 72 20 2a 29 70 29 3b 0a 7d 0a  ed char *)p);.}.
34f1f 0a 2f 2a 20 52 65 61 64 20 61 20 36 34 2d 62 69  ./* Read a 64-bi
34f20 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  t variable-lengt
34f21 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d  h integer from m
34f22 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61  emory starting a
34f23 74 20 70 5b 30 5d 2e 0a 20 2a 20 52 65 74 75 72  t p[0].. * Retur
34f24 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
34f25 62 79 74 65 73 20 72 65 61 64 2c 20 6f 72 20 30  bytes read, or 0
34f26 20 6f 6e 20 65 72 72 6f 72 2e 0a 20 2a 20 54 68   on error.. * Th
34f27 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65  e value is store
34f28 64 20 69 6e 20 2a 76 2e 20 2a 2f 0a 73 74 61 74  d in *v. */.stat
34f29 69 63 20 69 6e 74 20 66 74 73 33 47 65 74 56 61  ic int fts3GetVa
34f2a 72 69 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20  rint(const char 
34f2b 2a 70 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  *p, sqlite_int64
34f2c 20 2a 76 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e   *v){.  const un
34f2d 73 69 67 6e 65 64 20 63 68 61 72 20 2a 71 20 3d  signed char *q =
34f2e 20 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64   (const unsigned
34f2f 20 63 68 61 72 20 2a 29 20 70 3b 0a 20 20 73 71   char *) p;.  sq
34f30 6c 69 74 65 5f 75 69 6e 74 36 34 20 78 20 3d 20  lite_uint64 x = 
34f31 30 2c 20 79 20 3d 20 31 3b 0a 20 20 77 68 69 6c  0, y = 1;.  whil
34f32 65 28 20 28 2a 71 20 26 20 30 78 38 30 29 20 3d  e( (*q & 0x80) =
34f33 3d 20 30 78 38 30 20 29 7b 0a 20 20 20 20 78 20  = 0x80 ){.    x 
34f34 2b 3d 20 79 20 2a 20 28 2a 71 2b 2b 20 26 20 30  += y * (*q++ & 0
34f35 78 37 66 29 3b 0a 20 20 20 20 79 20 3c 3c 3d 20  x7f);.    y <<= 
34f36 37 3b 0a 20 20 20 20 69 66 28 20 71 20 2d 20 28  7;.    if( q - (
34f37 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
34f38 70 20 3e 3d 20 56 41 52 49 4e 54 5f 4d 41 58 20  p >= VARINT_MAX 
34f39 29 7b 20 20 2f 2a 20 62 61 64 20 64 61 74 61 20  ){  /* bad data 
34f3a 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
34f3b 20 30 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75   0 );.      retu
34f3c 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn 0;.    }.  }.
34f3d 20 20 78 20 2b 3d 20 79 20 2a 20 28 2a 71 2b 2b    x += y * (*q++
34f3e 29 3b 0a 20 20 2a 76 20 3d 20 28 73 71 6c 69 74  );.  *v = (sqlit
34f3f 65 5f 69 6e 74 36 34 29 20 78 3b 0a 20 20 72 65  e_int64) x;.  re
34f40 74 75 72 6e 20 28 69 6e 74 29 20 28 71 20 2d 20  turn (int) (q - 
34f41 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a  (unsigned char *
34f42 29 70 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69  )p);.}..static i
34f43 6e 74 20 66 74 73 33 47 65 74 56 61 72 69 6e 74  nt fts3GetVarint
34f44 33 32 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  32(const char *p
34f45 2c 20 69 6e 74 20 2a 70 69 29 7b 0a 20 73 71 6c  , int *pi){. sql
34f46 69 74 65 5f 69 6e 74 36 34 20 69 3b 0a 20 69 6e  ite_int64 i;. in
34f47 74 20 72 65 74 20 3d 20 66 74 73 33 47 65 74 56  t ret = fts3GetV
34f48 61 72 69 6e 74 28 70 2c 20 26 69 29 3b 0a 20 2a  arint(p, &i);. *
34f49 70 69 20 3d 20 28 69 6e 74 29 20 69 3b 0a 20 61  pi = (int) i;. a
34f4a 73 73 65 72 74 28 20 2a 70 69 3d 3d 69 20 29 3b  ssert( *pi==i );
34f4b 0a 20 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a  . return ret;.}.
34f4c 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
34f4d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f4e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
34f50 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 61 74 61 42 75  *****/./* DataBu
34f51 66 66 65 72 20 69 73 20 75 73 65 64 20 74 6f 20  ffer is used to 
34f52 63 6f 6c 6c 65 63 74 20 64 61 74 61 20 69 6e 74  collect data int
34f53 6f 20 61 20 62 75 66 66 65 72 20 69 6e 20 70 69  o a buffer in pi
34f54 65 63 65 6d 65 61 6c 0a 2a 2a 20 66 61 73 68 69  ecemeal.** fashi
34f55 6f 6e 2e 20 20 49 74 20 69 6d 70 6c 65 6d 65 6e  on.  It implemen
34f56 74 73 20 74 68 65 20 75 73 75 61 6c 20 64 69 73  ts the usual dis
34f57 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 6e  tinction between
34f58 20 61 6d 6f 75 6e 74 20 6f 66 0a 2a 2a 20 64 61   amount of.** da
34f59 74 61 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f  ta currently sto
34f5a 72 65 64 20 28 6e 44 61 74 61 29 20 61 6e 64 20  red (nData) and 
34f5b 62 75 66 66 65 72 20 63 61 70 61 63 69 74 79 20  buffer capacity 
34f5c 28 6e 43 61 70 61 63 69 74 79 29 2e 0a 2a 2a 0a  (nCapacity)..**.
34f5d 2a 2a 20 64 61 74 61 42 75 66 66 65 72 49 6e 69  ** dataBufferIni
34f5e 74 20 2d 20 63 72 65 61 74 65 20 61 20 62 75 66  t - create a buf
34f5f 66 65 72 20 77 69 74 68 20 67 69 76 65 6e 20 69  fer with given i
34f60 6e 69 74 69 61 6c 20 63 61 70 61 63 69 74 79 2e  nitial capacity.
34f61 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72 52 65  .** dataBufferRe
34f62 73 65 74 20 2d 20 66 6f 72 67 65 74 20 62 75 66  set - forget buf
34f63 66 65 72 27 73 20 64 61 74 61 2c 20 72 65 74 61  fer's data, reta
34f64 69 6e 69 6e 67 20 63 61 70 61 63 69 74 79 2e 0a  ining capacity..
34f65 2a 2a 20 64 61 74 61 42 75 66 66 65 72 44 65 73  ** dataBufferDes
34f66 74 72 6f 79 20 2d 20 66 72 65 65 20 62 75 66 66  troy - free buff
34f67 65 72 27 73 20 64 61 74 61 2e 0a 2a 2a 20 64 61  er's data..** da
34f68 74 61 42 75 66 66 65 72 53 77 61 70 20 2d 20 73  taBufferSwap - s
34f69 77 61 70 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  wap contents of 
34f6a 74 77 6f 20 62 75 66 66 65 72 73 2e 0a 2a 2a 20  two buffers..** 
34f6b 64 61 74 61 42 75 66 66 65 72 45 78 70 61 6e 64  dataBufferExpand
34f6c 20 2d 20 65 78 70 61 6e 64 20 63 61 70 61 63 69   - expand capaci
34f6d 74 79 20 77 69 74 68 6f 75 74 20 61 64 64 69 6e  ty without addin
34f6e 67 20 64 61 74 61 2e 0a 2a 2a 20 64 61 74 61 42  g data..** dataB
34f6f 75 66 66 65 72 41 70 70 65 6e 64 20 2d 20 61 70  ufferAppend - ap
34f70 70 65 6e 64 20 64 61 74 61 2e 0a 2a 2a 20 64 61  pend data..** da
34f71 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32 20  taBufferAppend2 
34f72 2d 20 61 70 70 65 6e 64 20 74 77 6f 20 70 69 65  - append two pie
34f73 63 65 73 20 6f 66 20 64 61 74 61 20 61 74 20 6f  ces of data at o
34f74 6e 63 65 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66  nce..** dataBuff
34f75 65 72 52 65 70 6c 61 63 65 20 2d 20 72 65 70 6c  erReplace - repl
34f76 61 63 65 20 62 75 66 66 65 72 27 73 20 64 61 74  ace buffer's dat
34f77 61 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  a..*/.typedef st
34f78 72 75 63 74 20 44 61 74 61 42 75 66 66 65 72 20  ruct DataBuffer 
34f79 7b 0a 20 20 63 68 61 72 20 2a 70 44 61 74 61 3b  {.  char *pData;
34f7a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
34f7b 6e 74 65 72 20 74 6f 20 6d 61 6c 6c 6f 63 27 65  nter to malloc'e
34f7c 64 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 69  d buffer. */.  i
34f7d 6e 74 20 6e 43 61 70 61 63 69 74 79 3b 20 20 20  nt nCapacity;   
34f7e 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
34f7f 70 44 61 74 61 20 62 75 66 66 65 72 2e 20 2a 2f  pData buffer. */
34f80 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 20 20 20  .  int nData;   
34f81 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 20           /* End 
34f82 6f 66 20 64 61 74 61 20 6c 6f 61 64 65 64 20 69  of data loaded i
34f83 6e 74 6f 20 70 44 61 74 61 2e 20 2a 2f 0a 7d 20  nto pData. */.} 
34f84 44 61 74 61 42 75 66 66 65 72 3b 0a 0a 73 74 61  DataBuffer;..sta
34f85 74 69 63 20 76 6f 69 64 20 64 61 74 61 42 75 66  tic void dataBuf
34f86 66 65 72 49 6e 69 74 28 44 61 74 61 42 75 66 66  ferInit(DataBuff
34f87 65 72 20 2a 70 42 75 66 66 65 72 2c 20 69 6e 74  er *pBuffer, int
34f88 20 6e 43 61 70 61 63 69 74 79 29 7b 0a 20 20 61   nCapacity){.  a
34f89 73 73 65 72 74 28 20 6e 43 61 70 61 63 69 74 79  ssert( nCapacity
34f8a 3e 3d 30 20 29 3b 0a 20 20 70 42 75 66 66 65 72  >=0 );.  pBuffer
34f8b 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 70  ->nData = 0;.  p
34f8c 42 75 66 66 65 72 2d 3e 6e 43 61 70 61 63 69 74  Buffer->nCapacit
34f8d 79 20 3d 20 6e 43 61 70 61 63 69 74 79 3b 0a 20  y = nCapacity;. 
34f8e 20 70 42 75 66 66 65 72 2d 3e 70 44 61 74 61 20   pBuffer->pData 
34f8f 3d 20 6e 43 61 70 61 63 69 74 79 3d 3d 30 20 3f  = nCapacity==0 ?
34f90 20 4e 55 4c 4c 20 3a 20 73 71 6c 69 74 65 33 5f   NULL : sqlite3_
34f91 6d 61 6c 6c 6f 63 28 6e 43 61 70 61 63 69 74 79  malloc(nCapacity
34f92 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  );.}.static void
34f93 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74   dataBufferReset
34f94 28 44 61 74 61 42 75 66 66 65 72 20 2a 70 42 75  (DataBuffer *pBu
34f95 66 66 65 72 29 7b 0a 20 20 70 42 75 66 66 65 72  ffer){.  pBuffer
34f96 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a 7d 0a 73  ->nData = 0;.}.s
34f97 74 61 74 69 63 20 76 6f 69 64 20 64 61 74 61 42  tatic void dataB
34f98 75 66 66 65 72 44 65 73 74 72 6f 79 28 44 61 74  ufferDestroy(Dat
34f99 61 42 75 66 66 65 72 20 2a 70 42 75 66 66 65 72  aBuffer *pBuffer
34f9a 29 7b 0a 20 20 69 66 28 20 70 42 75 66 66 65 72  ){.  if( pBuffer
34f9b 2d 3e 70 44 61 74 61 21 3d 4e 55 4c 4c 20 29 20  ->pData!=NULL ) 
34f9c 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 75  sqlite3_free(pBu
34f9d 66 66 65 72 2d 3e 70 44 61 74 61 29 3b 0a 20 20  ffer->pData);.  
34f9e 53 43 52 41 4d 42 4c 45 28 70 42 75 66 66 65 72  SCRAMBLE(pBuffer
34f9f 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  );.}.static void
34fa0 20 64 61 74 61 42 75 66 66 65 72 53 77 61 70 28   dataBufferSwap(
34fa1 44 61 74 61 42 75 66 66 65 72 20 2a 70 42 75 66  DataBuffer *pBuf
34fa2 66 65 72 31 2c 20 44 61 74 61 42 75 66 66 65 72  fer1, DataBuffer
34fa3 20 2a 70 42 75 66 66 65 72 32 29 7b 0a 20 20 44   *pBuffer2){.  D
34fa4 61 74 61 42 75 66 66 65 72 20 74 6d 70 20 3d 20  ataBuffer tmp = 
34fa5 2a 70 42 75 66 66 65 72 31 3b 0a 20 20 2a 70 42  *pBuffer1;.  *pB
34fa6 75 66 66 65 72 31 20 3d 20 2a 70 42 75 66 66 65  uffer1 = *pBuffe
34fa7 72 32 3b 0a 20 20 2a 70 42 75 66 66 65 72 32 20  r2;.  *pBuffer2 
34fa8 3d 20 74 6d 70 3b 0a 7d 0a 73 74 61 74 69 63 20  = tmp;.}.static 
34fa9 76 6f 69 64 20 64 61 74 61 42 75 66 66 65 72 45  void dataBufferE
34faa 78 70 61 6e 64 28 44 61 74 61 42 75 66 66 65 72  xpand(DataBuffer
34fab 20 2a 70 42 75 66 66 65 72 2c 20 69 6e 74 20 6e   *pBuffer, int n
34fac 41 64 64 43 61 70 61 63 69 74 79 29 7b 0a 20 20  AddCapacity){.  
34fad 61 73 73 65 72 74 28 20 6e 41 64 64 43 61 70 61  assert( nAddCapa
34fae 63 69 74 79 3e 30 20 29 3b 0a 20 20 2f 2a 20 54  city>0 );.  /* T
34faf 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e 73 69  ODO(shess) Consi
34fb0 64 65 72 20 65 78 70 61 6e 64 69 6e 67 20 6d 6f  der expanding mo
34fb1 72 65 20 61 67 67 72 65 73 73 69 76 65 6c 79 2e  re aggressively.
34fb2 20 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 0a    Note that the.
34fb3 20 20 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20    ** underlying 
34fb4 6d 61 6c 6c 6f 63 20 69 6d 70 6c 65 6d 65 6e 74  malloc implement
34fb5 61 74 69 6f 6e 20 6d 61 79 20 74 61 6b 65 20 63  ation may take c
34fb6 61 72 65 20 6f 66 20 73 75 63 68 20 74 68 69 6e  are of such thin
34fb7 67 73 20 66 6f 72 0a 20 20 2a 2a 20 75 73 20 61  gs for.  ** us a
34fb8 6c 72 65 61 64 79 2e 0a 20 20 2a 2f 0a 20 20 69  lready..  */.  i
34fb9 66 28 20 70 42 75 66 66 65 72 2d 3e 6e 44 61 74  f( pBuffer->nDat
34fba 61 2b 6e 41 64 64 43 61 70 61 63 69 74 79 3e 70  a+nAddCapacity>p
34fbb 42 75 66 66 65 72 2d 3e 6e 43 61 70 61 63 69 74  Buffer->nCapacit
34fbc 79 20 29 7b 0a 20 20 20 20 70 42 75 66 66 65 72  y ){.    pBuffer
34fbd 2d 3e 6e 43 61 70 61 63 69 74 79 20 3d 20 70 42  ->nCapacity = pB
34fbe 75 66 66 65 72 2d 3e 6e 44 61 74 61 2b 6e 41 64  uffer->nData+nAd
34fbf 64 43 61 70 61 63 69 74 79 3b 0a 20 20 20 20 70  dCapacity;.    p
34fc0 42 75 66 66 65 72 2d 3e 70 44 61 74 61 20 3d 20  Buffer->pData = 
34fc1 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
34fc2 70 42 75 66 66 65 72 2d 3e 70 44 61 74 61 2c 20  pBuffer->pData, 
34fc3 70 42 75 66 66 65 72 2d 3e 6e 43 61 70 61 63 69  pBuffer->nCapaci
34fc4 74 79 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69  ty);.  }.}.stati
34fc5 63 20 76 6f 69 64 20 64 61 74 61 42 75 66 66 65  c void dataBuffe
34fc6 72 41 70 70 65 6e 64 28 44 61 74 61 42 75 66 66  rAppend(DataBuff
34fc7 65 72 20 2a 70 42 75 66 66 65 72 2c 0a 20 20 20  er *pBuffer,.   
34fc8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34fc9 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
34fca 63 68 61 72 20 2a 70 53 6f 75 72 63 65 2c 20 69  char *pSource, i
34fcb 6e 74 20 6e 53 6f 75 72 63 65 29 7b 0a 20 20 61  nt nSource){.  a
34fcc 73 73 65 72 74 28 20 6e 53 6f 75 72 63 65 3e 30  ssert( nSource>0
34fcd 20 26 26 20 70 53 6f 75 72 63 65 21 3d 4e 55 4c   && pSource!=NUL
34fce 4c 20 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  L );.  dataBuffe
34fcf 72 45 78 70 61 6e 64 28 70 42 75 66 66 65 72 2c  rExpand(pBuffer,
34fd0 20 6e 53 6f 75 72 63 65 29 3b 0a 20 20 6d 65 6d   nSource);.  mem
34fd1 63 70 79 28 70 42 75 66 66 65 72 2d 3e 70 44 61  cpy(pBuffer->pDa
34fd2 74 61 2b 70 42 75 66 66 65 72 2d 3e 6e 44 61 74  ta+pBuffer->nDat
34fd3 61 2c 20 70 53 6f 75 72 63 65 2c 20 6e 53 6f 75  a, pSource, nSou
34fd4 72 63 65 29 3b 0a 20 20 70 42 75 66 66 65 72 2d  rce);.  pBuffer-
34fd5 3e 6e 44 61 74 61 20 2b 3d 20 6e 53 6f 75 72 63  >nData += nSourc
34fd6 65 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  e;.}.static void
34fd7 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
34fd8 64 32 28 44 61 74 61 42 75 66 66 65 72 20 2a 70  d2(DataBuffer *p
34fd9 42 75 66 66 65 72 2c 0a 20 20 20 20 20 20 20 20  Buffer,.        
34fda 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34fdb 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
34fdc 20 2a 70 53 6f 75 72 63 65 31 2c 20 69 6e 74 20   *pSource1, int 
34fdd 6e 53 6f 75 72 63 65 31 2c 0a 20 20 20 20 20 20  nSource1,.      
34fde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34fdf 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
34fe0 61 72 20 2a 70 53 6f 75 72 63 65 32 2c 20 69 6e  ar *pSource2, in
34fe1 74 20 6e 53 6f 75 72 63 65 32 29 7b 0a 20 20 61  t nSource2){.  a
34fe2 73 73 65 72 74 28 20 6e 53 6f 75 72 63 65 31 3e  ssert( nSource1>
34fe3 30 20 26 26 20 70 53 6f 75 72 63 65 31 21 3d 4e  0 && pSource1!=N
34fe4 55 4c 4c 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ULL );.  assert(
34fe5 20 6e 53 6f 75 72 63 65 32 3e 30 20 26 26 20 70   nSource2>0 && p
34fe6 53 6f 75 72 63 65 32 21 3d 4e 55 4c 4c 20 29 3b  Source2!=NULL );
34fe7 0a 20 20 64 61 74 61 42 75 66 66 65 72 45 78 70  .  dataBufferExp
34fe8 61 6e 64 28 70 42 75 66 66 65 72 2c 20 6e 53 6f  and(pBuffer, nSo
34fe9 75 72 63 65 31 2b 6e 53 6f 75 72 63 65 32 29 3b  urce1+nSource2);
34fea 0a 20 20 6d 65 6d 63 70 79 28 70 42 75 66 66 65  .  memcpy(pBuffe
34feb 72 2d 3e 70 44 61 74 61 2b 70 42 75 66 66 65 72  r->pData+pBuffer
34fec 2d 3e 6e 44 61 74 61 2c 20 70 53 6f 75 72 63 65  ->nData, pSource
34fed 31 2c 20 6e 53 6f 75 72 63 65 31 29 3b 0a 20 20  1, nSource1);.  
34fee 6d 65 6d 63 70 79 28 70 42 75 66 66 65 72 2d 3e  memcpy(pBuffer->
34fef 70 44 61 74 61 2b 70 42 75 66 66 65 72 2d 3e 6e  pData+pBuffer->n
34ff0 44 61 74 61 2b 6e 53 6f 75 72 63 65 31 2c 20 70  Data+nSource1, p
34ff1 53 6f 75 72 63 65 32 2c 20 6e 53 6f 75 72 63 65  Source2, nSource
34ff2 32 29 3b 0a 20 20 70 42 75 66 66 65 72 2d 3e 6e  2);.  pBuffer->n
34ff3 44 61 74 61 20 2b 3d 20 6e 53 6f 75 72 63 65 31  Data += nSource1
34ff4 2b 6e 53 6f 75 72 63 65 32 3b 0a 7d 0a 73 74 61  +nSource2;.}.sta
34ff5 74 69 63 20 76 6f 69 64 20 64 61 74 61 42 75 66  tic void dataBuf
34ff6 66 65 72 52 65 70 6c 61 63 65 28 44 61 74 61 42  ferReplace(DataB
34ff7 75 66 66 65 72 20 2a 70 42 75 66 66 65 72 2c 0a  uffer *pBuffer,.
34ff8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
34ff9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
34ffa 6e 73 74 20 63 68 61 72 20 2a 70 53 6f 75 72 63  nst char *pSourc
34ffb 65 2c 20 69 6e 74 20 6e 53 6f 75 72 63 65 29 7b  e, int nSource){
34ffc 0a 20 20 64 61 74 61 42 75 66 66 65 72 52 65 73  .  dataBufferRes
34ffd 65 74 28 70 42 75 66 66 65 72 29 3b 0a 20 20 64  et(pBuffer);.  d
34ffe 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
34fff 70 42 75 66 66 65 72 2c 20 70 53 6f 75 72 63 65  pBuffer, pSource
35000 2c 20 6e 53 6f 75 72 63 65 29 3b 0a 7d 0a 0a 2f  , nSource);.}../
35001 2a 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 69  * StringBuffer i
35002 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61  s a null-termina
35003 74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 44  ted version of D
35004 61 74 61 42 75 66 66 65 72 2e 20 2a 2f 0a 74 79  ataBuffer. */.ty
35005 70 65 64 65 66 20 73 74 72 75 63 74 20 53 74 72  pedef struct Str
35006 69 6e 67 42 75 66 66 65 72 20 7b 0a 20 20 44 61  ingBuffer {.  Da
35007 74 61 42 75 66 66 65 72 20 62 3b 20 20 20 20 20  taBuffer b;     
35008 20 20 20 20 20 20 20 2f 2a 20 49 6e 63 6c 75 64         /* Includ
35009 65 73 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74  es null terminat
3500a 6f 72 2e 20 2a 2f 0a 7d 20 53 74 72 69 6e 67 42  or. */.} StringB
3500b 75 66 66 65 72 3b 0a 0a 73 74 61 74 69 63 20 76  uffer;..static v
3500c 6f 69 64 20 69 6e 69 74 53 74 72 69 6e 67 42 75  oid initStringBu
3500d 66 66 65 72 28 53 74 72 69 6e 67 42 75 66 66 65  ffer(StringBuffe
3500e 72 20 2a 73 62 29 7b 0a 20 20 64 61 74 61 42 75  r *sb){.  dataBu
3500f 66 66 65 72 49 6e 69 74 28 26 73 62 2d 3e 62 2c  fferInit(&sb->b,
35010 20 31 30 30 29 3b 0a 20 20 64 61 74 61 42 75 66   100);.  dataBuf
35011 66 65 72 52 65 70 6c 61 63 65 28 26 73 62 2d 3e  ferReplace(&sb->
35012 62 2c 20 22 22 2c 20 31 29 3b 0a 7d 0a 73 74 61  b, "", 1);.}.sta
35013 74 69 63 20 69 6e 74 20 73 74 72 69 6e 67 42 75  tic int stringBu
35014 66 66 65 72 4c 65 6e 67 74 68 28 53 74 72 69 6e  fferLength(Strin
35015 67 42 75 66 66 65 72 20 2a 73 62 29 7b 0a 20 20  gBuffer *sb){.  
35016 72 65 74 75 72 6e 20 73 62 2d 3e 62 2e 6e 44 61  return sb->b.nDa
35017 74 61 2d 31 3b 0a 7d 0a 73 74 61 74 69 63 20 63  ta-1;.}.static c
35018 68 61 72 20 2a 73 74 72 69 6e 67 42 75 66 66 65  har *stringBuffe
35019 72 44 61 74 61 28 53 74 72 69 6e 67 42 75 66 66  rData(StringBuff
3501a 65 72 20 2a 73 62 29 7b 0a 20 20 72 65 74 75 72  er *sb){.  retur
3501b 6e 20 73 62 2d 3e 62 2e 70 44 61 74 61 3b 0a 7d  n sb->b.pData;.}
3501c 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 74 72  .static void str
3501d 69 6e 67 42 75 66 66 65 72 44 65 73 74 72 6f 79  ingBufferDestroy
3501e 28 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a 73  (StringBuffer *s
3501f 62 29 7b 0a 20 20 64 61 74 61 42 75 66 66 65 72  b){.  dataBuffer
35020 44 65 73 74 72 6f 79 28 26 73 62 2d 3e 62 29 3b  Destroy(&sb->b);
35021 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .}..static void 
35022 6e 61 70 70 65 6e 64 28 53 74 72 69 6e 67 42 75  nappend(StringBu
35023 66 66 65 72 20 2a 73 62 2c 20 63 6f 6e 73 74 20  ffer *sb, const 
35024 63 68 61 72 20 2a 7a 46 72 6f 6d 2c 20 69 6e 74  char *zFrom, int
35025 20 6e 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72   nFrom){.  asser
35026 74 28 20 73 62 2d 3e 62 2e 6e 44 61 74 61 3e 30  t( sb->b.nData>0
35027 20 29 3b 0a 20 20 69 66 28 20 6e 46 72 6f 6d 3e   );.  if( nFrom>
35028 30 20 29 7b 0a 20 20 20 20 73 62 2d 3e 62 2e 6e  0 ){.    sb->b.n
35029 44 61 74 61 2d 2d 3b 0a 20 20 20 20 64 61 74 61  Data--;.    data
3502a 42 75 66 66 65 72 41 70 70 65 6e 64 32 28 26 73  BufferAppend2(&s
3502b 62 2d 3e 62 2c 20 7a 46 72 6f 6d 2c 20 6e 46 72  b->b, zFrom, nFr
3502c 6f 6d 2c 20 22 22 2c 20 31 29 3b 0a 20 20 7d 0a  om, "", 1);.  }.
3502d 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 70  }.static void ap
3502e 70 65 6e 64 28 53 74 72 69 6e 67 42 75 66 66 65  pend(StringBuffe
3502f 72 20 2a 73 62 2c 20 63 6f 6e 73 74 20 63 68 61  r *sb, const cha
35030 72 20 2a 7a 46 72 6f 6d 29 7b 0a 20 20 6e 61 70  r *zFrom){.  nap
35031 70 65 6e 64 28 73 62 2c 20 7a 46 72 6f 6d 2c 20  pend(sb, zFrom, 
35032 73 74 72 6c 65 6e 28 7a 46 72 6f 6d 29 29 3b 0a  strlen(zFrom));.
35033 7d 0a 0a 2f 2a 20 41 70 70 65 6e 64 20 61 20 6c  }../* Append a l
35034 69 73 74 20 6f 66 20 73 74 72 69 6e 67 73 20 73  ist of strings s
35035 65 70 61 72 61 74 65 64 20 62 79 20 63 6f 6d 6d  eparated by comm
35036 61 73 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f  as. */.static vo
35037 69 64 20 61 70 70 65 6e 64 4c 69 73 74 28 53 74  id appendList(St
35038 72 69 6e 67 42 75 66 66 65 72 20 2a 73 62 2c 20  ringBuffer *sb, 
35039 69 6e 74 20 6e 53 74 72 69 6e 67 2c 20 63 68 61  int nString, cha
3503a 72 20 2a 2a 61 7a 53 74 72 69 6e 67 29 7b 0a 20  r **azString){. 
3503b 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d   int i;.  for(i=
3503c 30 3b 20 69 3c 6e 53 74 72 69 6e 67 3b 20 2b 2b  0; i<nString; ++
3503d 69 29 7b 0a 20 20 20 20 69 66 28 20 69 3e 30 20  i){.    if( i>0 
3503e 29 20 61 70 70 65 6e 64 28 73 62 2c 20 22 2c 20  ) append(sb, ", 
3503f 22 29 3b 0a 20 20 20 20 61 70 70 65 6e 64 28 73  ");.    append(s
35040 62 2c 20 61 7a 53 74 72 69 6e 67 5b 69 5d 29 3b  b, azString[i]);
35041 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 69  .  }.}..static i
35042 6e 74 20 65 6e 64 73 49 6e 57 68 69 74 65 53 70  nt endsInWhiteSp
35043 61 63 65 28 53 74 72 69 6e 67 42 75 66 66 65 72  ace(StringBuffer
35044 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73   *p){.  return s
35045 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e 67 74  tringBufferLengt
35046 68 28 70 29 3e 30 20 26 26 0a 20 20 20 20 73 61  h(p)>0 &&.    sa
35047 66 65 5f 69 73 73 70 61 63 65 28 73 74 72 69 6e  fe_isspace(strin
35048 67 42 75 66 66 65 72 44 61 74 61 28 70 29 5b 73  gBufferData(p)[s
35049 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e 67 74  tringBufferLengt
3504a 68 28 70 29 2d 31 5d 29 3b 0a 7d 0a 0a 2f 2a 20  h(p)-1]);.}../* 
3504b 49 66 20 74 68 65 20 53 74 72 69 6e 67 42 75 66  If the StringBuf
3504c 66 65 72 20 65 6e 64 73 20 69 6e 20 73 6f 6d 65  fer ends in some
3504d 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e  thing other than
3504e 20 77 68 69 74 65 20 73 70 61 63 65 2c 20 61 64   white space, ad
3504f 64 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 73 70  d a.** single sp
35050 61 63 65 20 63 68 61 72 61 63 74 65 72 20 74 6f  ace character to
35051 20 74 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61   the end..*/.sta
35052 74 69 63 20 76 6f 69 64 20 61 70 70 65 6e 64 57  tic void appendW
35053 68 69 74 65 53 70 61 63 65 28 53 74 72 69 6e 67  hiteSpace(String
35054 42 75 66 66 65 72 20 2a 70 29 7b 0a 20 20 69 66  Buffer *p){.  if
35055 28 20 73 74 72 69 6e 67 42 75 66 66 65 72 4c 65  ( stringBufferLe
35056 6e 67 74 68 28 70 29 3d 3d 30 20 29 20 72 65 74  ngth(p)==0 ) ret
35057 75 72 6e 3b 0a 20 20 69 66 28 20 21 65 6e 64 73  urn;.  if( !ends
35058 49 6e 57 68 69 74 65 53 70 61 63 65 28 70 29 20  InWhiteSpace(p) 
35059 29 20 61 70 70 65 6e 64 28 70 2c 20 22 20 22 29  ) append(p, " ")
3505a 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 77  ;.}../* Remove w
3505b 68 69 74 65 20 73 70 61 63 65 20 66 72 6f 6d 20  hite space from 
3505c 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 53  the end of the S
3505d 74 72 69 6e 67 42 75 66 66 65 72 20 2a 2f 0a 73  tringBuffer */.s
3505e 74 61 74 69 63 20 76 6f 69 64 20 74 72 69 6d 57  tatic void trimW
3505f 68 69 74 65 53 70 61 63 65 28 53 74 72 69 6e 67  hiteSpace(String
35060 42 75 66 66 65 72 20 2a 70 29 7b 0a 20 20 77 68  Buffer *p){.  wh
35061 69 6c 65 28 20 65 6e 64 73 49 6e 57 68 69 74 65  ile( endsInWhite
35062 53 70 61 63 65 28 70 29 20 29 7b 0a 20 20 20 20  Space(p) ){.    
35063 70 2d 3e 62 2e 70 44 61 74 61 5b 2d 2d 70 2d 3e  p->b.pData[--p->
35064 62 2e 6e 44 61 74 61 2d 31 5d 20 3d 20 27 5c 30  b.nData-1] = '\0
35065 27 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  ';.  }.}../*****
35066 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35067 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35068 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35069 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
3506a 2f 2a 20 44 4c 52 65 61 64 65 72 20 69 73 20 75  /* DLReader is u
3506b 73 65 64 20 74 6f 20 72 65 61 64 20 64 6f 63 75  sed to read docu
3506c 6d 65 6e 74 20 65 6c 65 6d 65 6e 74 73 20 66 72  ment elements fr
3506d 6f 6d 20 61 20 64 6f 63 6c 69 73 74 2e 20 20 54  om a doclist.  T
3506e 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 64 6f  he.** current do
3506f 63 69 64 20 69 73 20 63 61 63 68 65 64 2c 20 73  cid is cached, s
35070 6f 20 64 6c 72 44 6f 63 69 64 28 29 20 69 73 20  o dlrDocid() is 
35071 66 61 73 74 2e 20 20 44 4c 52 65 61 64 65 72 20  fast.  DLReader 
35072 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6f 77 6e 20  does not.** own 
35073 74 68 65 20 64 6f 63 6c 69 73 74 20 62 75 66 66  the doclist buff
35074 65 72 2e 0a 2a 2a 0a 2a 2a 20 64 6c 72 41 74 45  er..**.** dlrAtE
35075 6e 64 20 2d 20 74 72 75 65 20 69 66 20 74 68 65  nd - true if the
35076 72 65 27 73 20 6e 6f 20 6d 6f 72 65 20 64 61 74  re's no more dat
35077 61 20 74 6f 20 72 65 61 64 2e 0a 2a 2a 20 64 6c  a to read..** dl
35078 72 44 6f 63 69 64 20 2d 20 64 6f 63 69 64 20 6f  rDocid - docid o
35079 66 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d 65  f current docume
3507a 6e 74 2e 0a 2a 2a 20 64 6c 72 44 6f 63 44 61 74  nt..** dlrDocDat
3507b 61 20 2d 20 64 6f 63 6c 69 73 74 20 64 61 74 61  a - doclist data
3507c 20 66 6f 72 20 63 75 72 72 65 6e 74 20 64 6f 63   for current doc
3507d 75 6d 65 6e 74 20 28 69 6e 63 6c 75 64 69 6e 67  ument (including
3507e 20 64 6f 63 69 64 29 2e 0a 2a 2a 20 64 6c 72 44   docid)..** dlrD
3507f 6f 63 44 61 74 61 42 79 74 65 73 20 2d 20 6c 65  ocDataBytes - le
35080 6e 67 74 68 20 6f 66 20 73 61 6d 65 2e 0a 2a 2a  ngth of same..**
35081 20 64 6c 72 41 6c 6c 44 61 74 61 42 79 74 65 73   dlrAllDataBytes
35082 20 2d 20 6c 65 6e 67 74 68 20 6f 66 20 61 6c 6c   - length of all
35083 20 72 65 6d 61 69 6e 69 6e 67 20 64 61 74 61 2e   remaining data.
35084 0a 2a 2a 20 64 6c 72 50 6f 73 44 61 74 61 20 2d  .** dlrPosData -
35085 20 70 6f 73 69 74 69 6f 6e 20 64 61 74 61 20 66   position data f
35086 6f 72 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d  or current docum
35087 65 6e 74 2e 0a 2a 2a 20 64 6c 72 50 6f 73 44 61  ent..** dlrPosDa
35088 74 61 4c 65 6e 20 2d 20 6c 65 6e 67 74 68 20 6f  taLen - length o
35089 66 20 70 6f 73 20 64 61 74 61 20 66 6f 72 20 63  f pos data for c
3508a 75 72 72 65 6e 74 20 64 6f 63 75 6d 65 6e 74 20  urrent document 
3508b 28 69 6e 63 6c 20 50 4f 53 5f 45 4e 44 29 2e 0a  (incl POS_END)..
3508c 2a 2a 20 64 6c 72 53 74 65 70 20 2d 20 73 74 65  ** dlrStep - ste
3508d 70 20 74 6f 20 63 75 72 72 65 6e 74 20 64 6f 63  p to current doc
3508e 75 6d 65 6e 74 2e 0a 2a 2a 20 64 6c 72 49 6e 69  ument..** dlrIni
3508f 74 20 2d 20 69 6e 69 74 69 61 6c 20 66 6f 72 20  t - initial for 
35090 64 6f 63 6c 69 73 74 20 6f 66 20 67 69 76 65 6e  doclist of given
35091 20 74 79 70 65 20 61 67 61 69 6e 73 74 20 67 69   type against gi
35092 76 65 6e 20 64 61 74 61 2e 0a 2a 2a 20 64 6c 72  ven data..** dlr
35093 44 65 73 74 72 6f 79 20 2d 20 63 6c 65 61 6e 20  Destroy - clean 
35094 75 70 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 65 63 74  up..**.** Expect
35095 65 64 20 75 73 61 67 65 20 69 73 20 73 6f 6d 65  ed usage is some
35096 74 68 69 6e 67 20 6c 69 6b 65 3a 0a 2a 2a 0a 2a  thing like:.**.*
35097 2a 20 20 20 44 4c 52 65 61 64 65 72 20 72 65 61  *   DLReader rea
35098 64 65 72 3b 0a 2a 2a 20 20 20 64 6c 72 49 6e 69  der;.**   dlrIni
35099 74 28 26 72 65 61 64 65 72 2c 20 70 44 61 74 61  t(&reader, pData
3509a 2c 20 6e 44 61 74 61 29 3b 0a 2a 2a 20 20 20 77  , nData);.**   w
3509b 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28  hile( !dlrAtEnd(
3509c 26 72 65 61 64 65 72 29 20 29 7b 0a 2a 2a 20 20  &reader) ){.**  
3509d 20 20 20 2f 2f 20 63 61 6c 6c 73 20 74 6f 20 64     // calls to d
3509e 6c 72 44 6f 63 69 64 28 29 20 61 6e 64 20 6b 69  lrDocid() and ki
3509f 6e 2e 0a 2a 2a 20 20 20 20 20 64 6c 72 53 74 65  n..**     dlrSte
350a0 70 28 26 72 65 61 64 65 72 29 3b 0a 2a 2a 20 20  p(&reader);.**  
350a1 20 7d 0a 2a 2a 20 20 20 64 6c 72 44 65 73 74 72   }.**   dlrDestr
350a2 6f 79 28 26 72 65 61 64 65 72 29 3b 0a 2a 2f 0a  oy(&reader);.*/.
350a3 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44  typedef struct D
350a4 4c 52 65 61 64 65 72 20 7b 0a 20 20 44 6f 63 4c  LReader {.  DocL
350a5 69 73 74 54 79 70 65 20 69 54 79 70 65 3b 0a 20  istType iType;. 
350a6 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61   const char *pDa
350a7 74 61 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b  ta;.  int nData;
350a8 0a 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  ..  sqlite_int64
350a9 20 69 44 6f 63 69 64 3b 0a 20 20 69 6e 74 20 6e   iDocid;.  int n
350aa 45 6c 65 6d 65 6e 74 3b 0a 7d 20 44 4c 52 65 61  Element;.} DLRea
350ab 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74  der;..static int
350ac 20 64 6c 72 41 74 45 6e 64 28 44 4c 52 65 61 64   dlrAtEnd(DLRead
350ad 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
350ae 61 73 73 65 72 74 28 20 70 52 65 61 64 65 72 2d  assert( pReader-
350af 3e 6e 44 61 74 61 3e 3d 30 20 29 3b 0a 20 20 72  >nData>=0 );.  r
350b0 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 6e  eturn pReader->n
350b1 44 61 74 61 3d 3d 30 3b 0a 7d 0a 73 74 61 74 69  Data==0;.}.stati
350b2 63 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 64  c sqlite_int64 d
350b3 6c 72 44 6f 63 69 64 28 44 4c 52 65 61 64 65 72  lrDocid(DLReader
350b4 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73   *pReader){.  as
350b5 73 65 72 74 28 20 21 64 6c 72 41 74 45 6e 64 28  sert( !dlrAtEnd(
350b6 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65  pReader) );.  re
350b7 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 69 44  turn pReader->iD
350b8 6f 63 69 64 3b 0a 7d 0a 73 74 61 74 69 63 20 63  ocid;.}.static c
350b9 6f 6e 73 74 20 63 68 61 72 20 2a 64 6c 72 44 6f  onst char *dlrDo
350ba 63 44 61 74 61 28 44 4c 52 65 61 64 65 72 20 2a  cData(DLReader *
350bb 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65  pReader){.  asse
350bc 72 74 28 20 21 64 6c 72 41 74 45 6e 64 28 70 52  rt( !dlrAtEnd(pR
350bd 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75  eader) );.  retu
350be 72 6e 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74  rn pReader->pDat
350bf 61 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  a;.}.static int 
350c0 64 6c 72 44 6f 63 44 61 74 61 42 79 74 65 73 28  dlrDocDataBytes(
350c1 44 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  DLReader *pReade
350c2 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 64  r){.  assert( !d
350c3 6c 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29  lrAtEnd(pReader)
350c4 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65   );.  return pRe
350c5 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3b 0a  ader->nElement;.
350c6 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6c 72  }.static int dlr
350c7 41 6c 6c 44 61 74 61 42 79 74 65 73 28 44 4c 52  AllDataBytes(DLR
350c8 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
350c9 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c 72 41  .  assert( !dlrA
350ca 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
350cb 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65  .  return pReade
350cc 72 2d 3e 6e 44 61 74 61 3b 0a 7d 0a 2f 2a 20 54  r->nData;.}./* T
350cd 4f 44 4f 28 73 68 65 73 73 29 20 43 6f 6e 73 69  ODO(shess) Consi
350ce 64 65 72 20 61 64 64 69 6e 67 20 61 20 66 69 65  der adding a fie
350cf 6c 64 20 74 6f 20 74 72 61 63 6b 20 69 44 6f 63  ld to track iDoc
350d0 69 64 20 76 61 72 69 6e 74 20 6c 65 6e 67 74 68  id varint length
350d1 0a 2a 2a 20 74 6f 20 6d 61 6b 65 20 74 68 65 73  .** to make thes
350d2 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20  e two functions 
350d3 66 61 73 74 65 72 2e 20 20 54 68 69 73 20 6d 69  faster.  This mi
350d4 67 68 74 20 6d 61 74 74 65 72 20 28 61 20 74 69  ght matter (a ti
350d5 6e 79 20 62 69 74 29 0a 2a 2a 20 66 6f 72 20 71  ny bit).** for q
350d6 75 65 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  ueries..*/.stati
350d7 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 6c  c const char *dl
350d8 72 50 6f 73 44 61 74 61 28 44 4c 52 65 61 64 65  rPosData(DLReade
350d9 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 73  r *pReader){.  s
350da 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 75 6d  qlite_int64 iDum
350db 6d 79 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 66 74  my;.  int n = ft
350dc 73 33 47 65 74 56 61 72 69 6e 74 28 70 52 65 61  s3GetVarint(pRea
350dd 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 69 44 75  der->pData, &iDu
350de 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20  mmy);.  assert( 
350df 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61 64 65  !dlrAtEnd(pReade
350e0 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  r) );.  return p
350e1 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 3b  Reader->pData+n;
350e2 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6c  .}.static int dl
350e3 72 50 6f 73 44 61 74 61 4c 65 6e 28 44 4c 52 65  rPosDataLen(DLRe
350e4 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
350e5 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
350e6 44 75 6d 6d 79 3b 0a 20 20 69 6e 74 20 6e 20 3d  Dummy;.  int n =
350e7 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 28 70   fts3GetVarint(p
350e8 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26  Reader->pData, &
350e9 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72  iDummy);.  asser
350ea 74 28 20 21 64 6c 72 41 74 45 6e 64 28 70 52 65  t( !dlrAtEnd(pRe
350eb 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72  ader) );.  retur
350ec 6e 20 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d  n pReader->nElem
350ed 65 6e 74 2d 6e 3b 0a 7d 0a 73 74 61 74 69 63 20  ent-n;.}.static 
350ee 76 6f 69 64 20 64 6c 72 53 74 65 70 28 44 4c 52  void dlrStep(DLR
350ef 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
350f0 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c 72 41  .  assert( !dlrA
350f1 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
350f2 0a 0a 20 20 2f 2a 20 53 6b 69 70 20 70 61 73 74  ..  /* Skip past
350f3 20 63 75 72 72 65 6e 74 20 64 6f 63 6c 69 73 74   current doclist
350f4 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 61   element. */.  a
350f5 73 73 65 72 74 28 20 70 52 65 61 64 65 72 2d 3e  ssert( pReader->
350f6 6e 45 6c 65 6d 65 6e 74 3c 3d 70 52 65 61 64 65  nElement<=pReade
350f7 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 70 52  r->nData );.  pR
350f8 65 61 64 65 72 2d 3e 70 44 61 74 61 20 2b 3d 20  eader->pData += 
350f9 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e  pReader->nElemen
350fa 74 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 6e 44  t;.  pReader->nD
350fb 61 74 61 20 2d 3d 20 70 52 65 61 64 65 72 2d 3e  ata -= pReader->
350fc 6e 45 6c 65 6d 65 6e 74 3b 0a 0a 20 20 2f 2a 20  nElement;..  /* 
350fd 49 66 20 74 68 65 72 65 20 69 73 20 6d 6f 72 65  If there is more
350fe 20 64 61 74 61 2c 20 72 65 61 64 20 74 68 65 20   data, read the 
350ff 6e 65 78 74 20 64 6f 63 6c 69 73 74 20 65 6c 65  next doclist ele
35100 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70  ment. */.  if( p
35101 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 21 3d 30  Reader->nData!=0
35102 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69   ){.    sqlite_i
35103 6e 74 36 34 20 69 44 6f 63 69 64 44 65 6c 74 61  nt64 iDocidDelta
35104 3b 0a 20 20 20 20 69 6e 74 20 69 44 75 6d 6d 79  ;.    int iDummy
35105 2c 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72  , n = fts3GetVar
35106 69 6e 74 28 70 52 65 61 64 65 72 2d 3e 70 44 61  int(pReader->pDa
35107 74 61 2c 20 26 69 44 6f 63 69 64 44 65 6c 74 61  ta, &iDocidDelta
35108 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  );.    pReader->
35109 69 44 6f 63 69 64 20 2b 3d 20 69 44 6f 63 69 64  iDocid += iDocid
3510a 44 65 6c 74 61 3b 0a 20 20 20 20 69 66 28 20 70  Delta;.    if( p
3510b 52 65 61 64 65 72 2d 3e 69 54 79 70 65 3e 3d 44  Reader->iType>=D
3510c 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 29 7b 0a 20  L_POSITIONS ){. 
3510d 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 70       assert( n<p
3510e 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b  Reader->nData );
3510f 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 31 20  .      while( 1 
35110 29 7b 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20  ){.        n += 
35111 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
35112 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e  pReader->pData+n
35113 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20  , &iDummy);.    
35114 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c 3d 70      assert( n<=p
35115 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b  Reader->nData );
35116 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 44 75  .        if( iDu
35117 6d 6d 79 3d 3d 50 4f 53 5f 45 4e 44 20 29 20 62  mmy==POS_END ) b
35118 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66  reak;.        if
35119 28 20 69 44 75 6d 6d 79 3d 3d 50 4f 53 5f 43 4f  ( iDummy==POS_CO
3511a 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20 20 20  LUMN ){.        
3511b 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61    n += fts3GetVa
3511c 72 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e  rint32(pReader->
3511d 70 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79  pData+n, &iDummy
3511e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  );.          ass
3511f 65 72 74 28 20 6e 3c 70 52 65 61 64 65 72 2d 3e  ert( n<pReader->
35120 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 20 20 20  nData );.       
35121 20 7d 65 6c 73 65 20 69 66 28 20 70 52 65 61 64   }else if( pRead
35122 65 72 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f  er->iType==DL_PO
35123 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20  SITIONS_OFFSETS 
35124 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b  ){.          n +
35125 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33  = fts3GetVarint3
35126 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61  2(pReader->pData
35127 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20  +n, &iDummy);.  
35128 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66 74 73          n += fts
35129 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65  3GetVarint32(pRe
3512a 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26  ader->pData+n, &
3512b 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20  iDummy);.       
3512c 20 20 20 61 73 73 65 72 74 28 20 6e 3c 70 52 65     assert( n<pRe
3512d 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20  ader->nData );. 
3512e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
3512f 0a 20 20 20 20 7d 0a 20 20 20 20 70 52 65 61 64  .    }.    pRead
35130 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 20 3d 20 6e  er->nElement = n
35131 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 52  ;.    assert( pR
35132 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3c  eader->nElement<
35133 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20  =pReader->nData 
35134 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20  );.  }.}.static 
35135 76 6f 69 64 20 64 6c 72 49 6e 69 74 28 44 4c 52  void dlrInit(DLR
35136 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 2c 20  eader *pReader, 
35137 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79 70  DocListType iTyp
35138 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
35139 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
3513a 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44  r *pData, int nD
3513b 61 74 61 29 7b 0a 20 20 61 73 73 65 72 74 28 20  ata){.  assert( 
3513c 70 44 61 74 61 21 3d 4e 55 4c 4c 20 26 26 20 6e  pData!=NULL && n
3513d 44 61 74 61 21 3d 30 20 29 3b 0a 20 20 70 52 65  Data!=0 );.  pRe
3513e 61 64 65 72 2d 3e 69 54 79 70 65 20 3d 20 69 54  ader->iType = iT
3513f 79 70 65 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  ype;.  pReader->
35140 70 44 61 74 61 20 3d 20 70 44 61 74 61 3b 0a 20  pData = pData;. 
35141 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20   pReader->nData 
35142 3d 20 6e 44 61 74 61 3b 0a 20 20 70 52 65 61 64  = nData;.  pRead
35143 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 20 3d 20 30  er->nElement = 0
35144 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 44 6f  ;.  pReader->iDo
35145 63 69 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 4c  cid = 0;..  /* L
35146 6f 61 64 20 74 68 65 20 66 69 72 73 74 20 65 6c  oad the first el
35147 65 6d 65 6e 74 27 73 20 64 61 74 61 2e 20 20 54  ement's data.  T
35148 68 65 72 65 20 6d 75 73 74 20 62 65 20 61 20 66  here must be a f
35149 69 72 73 74 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f  irst element. */
3514a 0a 20 20 64 6c 72 53 74 65 70 28 70 52 65 61 64  .  dlrStep(pRead
3514b 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  er);.}.static vo
3514c 69 64 20 64 6c 72 44 65 73 74 72 6f 79 28 44 4c  id dlrDestroy(DL
3514d 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
3514e 7b 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 52 65  {.  SCRAMBLE(pRe
3514f 61 64 65 72 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65  ader);.}..#ifnde
35150 66 20 4e 44 45 42 55 47 0a 2f 2a 20 56 65 72 69  f NDEBUG./* Veri
35151 66 79 20 74 68 61 74 20 74 68 65 20 64 6f 63 6c  fy that the docl
35152 69 73 74 20 63 61 6e 20 62 65 20 76 61 6c 69 64  ist can be valid
35153 6c 79 20 64 65 63 6f 64 65 64 2e 20 20 41 6c 73  ly decoded.  Als
35154 6f 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a  o returns the.**
35155 20 6c 61 73 74 20 64 6f 63 69 64 20 66 6f 75 6e   last docid foun
35156 64 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20  d because it is 
35157 63 6f 6e 76 65 6e 69 65 6e 74 20 69 6e 20 6f 74  convenient in ot
35158 68 65 72 20 61 73 73 65 72 74 69 6f 6e 73 20 66  her assertions f
35159 6f 72 0a 2a 2a 20 44 4c 57 72 69 74 65 72 2e 0a  or.** DLWriter..
3515a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  */.static void d
3515b 6f 63 4c 69 73 74 56 61 6c 69 64 61 74 65 28 44  ocListValidate(D
3515c 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79 70 65  ocListType iType
3515d 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44  , const char *pD
3515e 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a  ata, int nData,.
3515f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35160 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
35161 74 65 5f 69 6e 74 36 34 20 2a 70 4c 61 73 74 44  te_int64 *pLastD
35162 6f 63 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 5f  ocid){.  sqlite_
35163 69 6e 74 36 34 20 69 50 72 65 76 44 6f 63 69 64  int64 iPrevDocid
35164 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20   = 0;.  assert( 
35165 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 61 73 73  nData>0 );.  ass
35166 65 72 74 28 20 70 44 61 74 61 21 3d 30 20 29 3b  ert( pData!=0 );
35167 0a 20 20 61 73 73 65 72 74 28 20 70 44 61 74 61  .  assert( pData
35168 2b 6e 44 61 74 61 3e 70 44 61 74 61 20 29 3b 0a  +nData>pData );.
35169 20 20 77 68 69 6c 65 28 20 6e 44 61 74 61 21 3d    while( nData!=
3516a 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 5f  0 ){.    sqlite_
3516b 69 6e 74 36 34 20 69 44 6f 63 69 64 44 65 6c 74  int64 iDocidDelt
3516c 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 66  a;.    int n = f
3516d 74 73 33 47 65 74 56 61 72 69 6e 74 28 70 44 61  ts3GetVarint(pDa
3516e 74 61 2c 20 26 69 44 6f 63 69 64 44 65 6c 74 61  ta, &iDocidDelta
3516f 29 3b 0a 20 20 20 20 69 50 72 65 76 44 6f 63 69  );.    iPrevDoci
35170 64 20 2b 3d 20 69 44 6f 63 69 64 44 65 6c 74 61  d += iDocidDelta
35171 3b 0a 20 20 20 20 69 66 28 20 69 54 79 70 65 3e  ;.    if( iType>
35172 44 4c 5f 44 4f 43 49 44 53 20 29 7b 0a 20 20 20  DL_DOCIDS ){.   
35173 20 20 20 69 6e 74 20 69 44 75 6d 6d 79 3b 0a 20     int iDummy;. 
35174 20 20 20 20 20 77 68 69 6c 65 28 20 31 20 29 7b       while( 1 ){
35175 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66 74  .        n += ft
35176 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 44  s3GetVarint32(pD
35177 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b  ata+n, &iDummy);
35178 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 44 75  .        if( iDu
35179 6d 6d 79 3d 3d 50 4f 53 5f 45 4e 44 20 29 20 62  mmy==POS_END ) b
3517a 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66  reak;.        if
3517b 28 20 69 44 75 6d 6d 79 3d 3d 50 4f 53 5f 43 4f  ( iDummy==POS_CO
3517c 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20 20 20  LUMN ){.        
3517d 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61    n += fts3GetVa
3517e 72 69 6e 74 33 32 28 70 44 61 74 61 2b 6e 2c 20  rint32(pData+n, 
3517f 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20  &iDummy);.      
35180 20 20 7d 65 6c 73 65 20 69 66 28 20 69 54 79 70    }else if( iTyp
35181 65 3e 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 29  e>DL_POSITIONS )
35182 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d  {.          n +=
35183 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
35184 28 70 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d  (pData+n, &iDumm
35185 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 20  y);.          n 
35186 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74  += fts3GetVarint
35187 33 32 28 70 44 61 74 61 2b 6e 2c 20 26 69 44 75  32(pData+n, &iDu
35188 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  mmy);.        }.
35189 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
3518a 6e 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20 20 20  n<=nData );.    
3518b 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73    }.    }.    as
3518c 73 65 72 74 28 20 6e 3c 3d 6e 44 61 74 61 20 29  sert( n<=nData )
3518d 3b 0a 20 20 20 20 70 44 61 74 61 20 2b 3d 20 6e  ;.    pData += n
3518e 3b 0a 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e  ;.    nData -= n
3518f 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 4c 61 73  ;.  }.  if( pLas
35190 74 44 6f 63 69 64 20 29 20 2a 70 4c 61 73 74 44  tDocid ) *pLastD
35191 6f 63 69 64 20 3d 20 69 50 72 65 76 44 6f 63 69  ocid = iPrevDoci
35192 64 3b 0a 7d 0a 23 64 65 66 69 6e 65 20 41 53 53  d;.}.#define ASS
35193 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53  ERT_VALID_DOCLIS
35194 54 28 69 2c 20 70 2c 20 6e 2c 20 6f 29 20 64 6f  T(i, p, n, o) do
35195 63 4c 69 73 74 56 61 6c 69 64 61 74 65 28 69 2c  cListValidate(i,
35196 20 70 2c 20 6e 2c 20 6f 29 0a 23 65 6c 73 65 0a   p, n, o).#else.
35197 23 64 65 66 69 6e 65 20 41 53 53 45 52 54 5f 56  #define ASSERT_V
35198 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 69 2c 20  ALID_DOCLIST(i, 
35199 70 2c 20 6e 2c 20 6f 29 20 61 73 73 65 72 74 28  p, n, o) assert(
3519a 20 31 20 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a   1 ).#endif../**
3519b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3519c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3519d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3519e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3519f 2a 2f 0a 2f 2a 20 44 4c 57 72 69 74 65 72 20 69  */./* DLWriter i
351a0 73 20 75 73 65 64 20 74 6f 20 77 72 69 74 65 20  s used to write 
351a1 64 6f 63 6c 69 73 74 20 64 61 74 61 20 74 6f 20  doclist data to 
351a2 61 20 44 61 74 61 42 75 66 66 65 72 2e 20 20 44  a DataBuffer.  D
351a3 4c 57 72 69 74 65 72 0a 2a 2a 20 61 6c 77 61 79  LWriter.** alway
351a4 73 20 61 70 70 65 6e 64 73 20 74 6f 20 74 68 65  s appends to the
351a5 20 62 75 66 66 65 72 20 61 6e 64 20 64 6f 65 73   buffer and does
351a6 20 6e 6f 74 20 6f 77 6e 20 69 74 2e 0a 2a 2a 0a   not own it..**.
351a7 2a 2a 20 64 6c 77 49 6e 69 74 20 2d 20 69 6e 69  ** dlwInit - ini
351a8 74 69 61 6c 69 7a 65 20 74 6f 20 77 72 69 74 65  tialize to write
351a9 20 61 20 67 69 76 65 6e 20 74 79 70 65 20 64 6f   a given type do
351aa 63 6c 69 73 74 74 6f 20 61 20 62 75 66 66 65 72  clistto a buffer
351ab 2e 0a 2a 2a 20 64 6c 77 44 65 73 74 72 6f 79 20  ..** dlwDestroy 
351ac 2d 20 63 6c 65 61 72 20 74 68 65 20 77 72 69 74  - clear the writ
351ad 65 72 27 73 20 6d 65 6d 6f 72 79 2e 20 20 44 6f  er's memory.  Do
351ae 65 73 20 6e 6f 74 20 66 72 65 65 20 62 75 66 66  es not free buff
351af 65 72 2e 0a 2a 2a 20 64 6c 77 41 70 70 65 6e 64  er..** dlwAppend
351b0 20 2d 20 61 70 70 65 6e 64 20 72 61 77 20 64 6f   - append raw do
351b1 63 6c 69 73 74 20 64 61 74 61 20 74 6f 20 62 75  clist data to bu
351b2 66 66 65 72 2e 0a 2a 2a 20 64 6c 77 43 6f 70 79  ffer..** dlwCopy
351b3 20 2d 20 63 6f 70 79 20 6e 65 78 74 20 64 6f 63   - copy next doc
351b4 6c 69 73 74 20 66 72 6f 6d 20 72 65 61 64 65 72  list from reader
351b5 20 74 6f 20 77 72 69 74 65 72 2e 0a 2a 2a 20 64   to writer..** d
351b6 6c 77 41 64 64 20 2d 20 63 6f 6e 73 74 72 75 63  lwAdd - construc
351b7 74 20 64 6f 63 6c 69 73 74 20 65 6c 65 6d 65 6e  t doclist elemen
351b8 74 20 61 6e 64 20 61 70 70 65 6e 64 20 74 6f 20  t and append to 
351b9 62 75 66 66 65 72 2e 0a 2a 2a 20 20 20 20 4f 6e  buffer..**    On
351ba 6c 79 20 61 70 70 6c 79 20 64 6c 77 41 64 64 28  ly apply dlwAdd(
351bb 29 20 74 6f 20 44 4c 5f 44 4f 43 49 44 53 20 64  ) to DL_DOCIDS d
351bc 6f 63 6c 69 73 74 73 20 28 65 6c 73 65 20 75 73  oclists (else us
351bd 65 20 50 4c 57 72 69 74 65 72 29 2e 0a 2a 2f 0a  e PLWriter)..*/.
351be 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44  typedef struct D
351bf 4c 57 72 69 74 65 72 20 7b 0a 20 20 44 6f 63 4c  LWriter {.  DocL
351c0 69 73 74 54 79 70 65 20 69 54 79 70 65 3b 0a 20  istType iType;. 
351c1 20 44 61 74 61 42 75 66 66 65 72 20 2a 62 3b 0a   DataBuffer *b;.
351c2 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
351c3 50 72 65 76 44 6f 63 69 64 3b 0a 23 69 66 6e 64  PrevDocid;.#ifnd
351c4 65 66 20 4e 44 45 42 55 47 0a 20 20 69 6e 74 20  ef NDEBUG.  int 
351c5 68 61 73 5f 69 50 72 65 76 44 6f 63 69 64 3b 0a  has_iPrevDocid;.
351c6 23 65 6e 64 69 66 0a 7d 20 44 4c 57 72 69 74 65  #endif.} DLWrite
351c7 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20  r;..static void 
351c8 64 6c 77 49 6e 69 74 28 44 4c 57 72 69 74 65 72  dlwInit(DLWriter
351c9 20 2a 70 57 72 69 74 65 72 2c 20 44 6f 63 4c 69   *pWriter, DocLi
351ca 73 74 54 79 70 65 20 69 54 79 70 65 2c 20 44 61  stType iType, Da
351cb 74 61 42 75 66 66 65 72 20 2a 62 29 7b 0a 20 20  taBuffer *b){.  
351cc 70 57 72 69 74 65 72 2d 3e 62 20 3d 20 62 3b 0a  pWriter->b = b;.
351cd 20 20 70 57 72 69 74 65 72 2d 3e 69 54 79 70 65    pWriter->iType
351ce 20 3d 20 69 54 79 70 65 3b 0a 20 20 70 57 72 69   = iType;.  pWri
351cf 74 65 72 2d 3e 69 50 72 65 76 44 6f 63 69 64 20  ter->iPrevDocid 
351d0 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  = 0;.#ifndef NDE
351d1 42 55 47 0a 20 20 70 57 72 69 74 65 72 2d 3e 68  BUG.  pWriter->h
351d2 61 73 5f 69 50 72 65 76 44 6f 63 69 64 20 3d 20  as_iPrevDocid = 
351d3 30 3b 0a 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74  0;.#endif.}.stat
351d4 69 63 20 76 6f 69 64 20 64 6c 77 44 65 73 74 72  ic void dlwDestr
351d5 6f 79 28 44 4c 57 72 69 74 65 72 20 2a 70 57 72  oy(DLWriter *pWr
351d6 69 74 65 72 29 7b 0a 20 20 53 43 52 41 4d 42 4c  iter){.  SCRAMBL
351d7 45 28 70 57 72 69 74 65 72 29 3b 0a 7d 0a 2f 2a  E(pWriter);.}./*
351d8 20 69 46 69 72 73 74 44 6f 63 69 64 20 69 73 20   iFirstDocid is 
351d9 74 68 65 20 66 69 72 73 74 20 64 6f 63 69 64 20  the first docid 
351da 69 6e 20 74 68 65 20 64 6f 63 6c 69 73 74 20 69  in the doclist i
351db 6e 20 70 44 61 74 61 2e 20 20 49 74 20 69 73 0a  n pData.  It is.
351dc 2a 2a 20 6e 65 65 64 65 64 20 62 65 63 61 75 73  ** needed becaus
351dd 65 20 70 44 61 74 61 20 6d 61 79 20 70 6f 69 6e  e pData may poin
351de 74 20 77 69 74 68 69 6e 20 61 20 6c 61 72 67 65  t within a large
351df 72 20 64 6f 63 6c 69 73 74 2c 20 69 6e 20 77 68  r doclist, in wh
351e0 69 63 68 0a 2a 2a 20 63 61 73 65 20 74 68 65 20  ich.** case the 
351e1 66 69 72 73 74 20 69 74 65 6d 20 77 6f 75 6c 64  first item would
351e2 20 62 65 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65   be delta-encode
351e3 64 2e 0a 2a 2a 0a 2a 2a 20 69 4c 61 73 74 44 6f  d..**.** iLastDo
351e4 63 69 64 20 69 73 20 74 68 65 20 66 69 6e 61 6c  cid is the final
351e5 20 64 6f 63 69 64 20 69 6e 20 74 68 65 20 64 6f   docid in the do
351e6 63 6c 69 73 74 20 69 6e 20 70 44 61 74 61 2e 20  clist in pData. 
351e7 20 49 74 20 69 73 0a 2a 2a 20 6e 65 65 64 65 64   It is.** needed
351e8 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 6e   to create the n
351e9 65 77 20 69 50 72 65 76 44 6f 63 69 64 20 66 6f  ew iPrevDocid fo
351ea 72 20 66 75 74 75 72 65 20 64 65 6c 74 61 2d 65  r future delta-e
351eb 6e 63 6f 64 69 6e 67 2e 20 20 54 68 65 0a 2a 2a  ncoding.  The.**
351ec 20 63 6f 64 65 20 63 6f 75 6c 64 20 64 65 63 6f   code could deco
351ed 64 65 20 74 68 65 20 70 61 73 73 65 64 20 64 6f  de the passed do
351ee 63 6c 69 73 74 20 74 6f 20 72 65 63 72 65 61 74  clist to recreat
351ef 65 20 69 4c 61 73 74 44 6f 63 69 64 2c 20 62 75  e iLastDocid, bu
351f0 74 0a 2a 2a 20 74 68 65 20 6f 6e 6c 79 20 63 75  t.** the only cu
351f1 72 72 65 6e 74 20 75 73 65 72 20 28 64 6f 63 4c  rrent user (docL
351f2 69 73 74 4d 65 72 67 65 29 20 61 6c 72 65 61 64  istMerge) alread
351f3 79 20 68 61 73 20 64 65 63 6f 64 65 64 20 74 68  y has decoded th
351f4 69 73 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f  is.** informatio
351f5 6e 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68  n..*/./* TODO(sh
351f6 65 73 73 29 20 54 68 69 73 20 68 61 73 20 62 65  ess) This has be
351f7 63 6f 6d 65 20 6a 75 73 74 20 61 20 68 65 6c 70  come just a help
351f8 65 72 20 66 6f 72 20 64 6f 63 4c 69 73 74 4d 65  er for docListMe
351f9 72 67 65 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72  rge..** Consider
351fa 20 61 20 72 65 66 61 63 74 6f 72 20 74 6f 20 6d   a refactor to m
351fb 61 6b 65 20 74 68 69 73 20 63 6c 65 61 6e 65 72  ake this cleaner
351fc 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
351fd 20 64 6c 77 41 70 70 65 6e 64 28 44 4c 57 72 69   dlwAppend(DLWri
351fe 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20  ter *pWriter,.  
351ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35200 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
35201 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61  pData, int nData
35202 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
35203 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69          sqlite_i
35204 6e 74 36 34 20 69 46 69 72 73 74 44 6f 63 69 64  nt64 iFirstDocid
35205 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  , sqlite_int64 i
35206 4c 61 73 74 44 6f 63 69 64 29 7b 0a 20 20 73 71  LastDocid){.  sq
35207 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69  lite_int64 iDoci
35208 64 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 5b  d = 0;.  char c[
35209 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69  VARINT_MAX];.  i
3520a 6e 74 20 6e 46 69 72 73 74 4f 6c 64 2c 20 6e 46  nt nFirstOld, nF
3520b 69 72 73 74 4e 65 77 3b 20 20 20 20 20 2f 2a 20  irstNew;     /* 
3520c 4f 6c 64 20 61 6e 64 20 6e 65 77 20 76 61 72 69  Old and new vari
3520d 6e 74 20 6c 65 6e 20 6f 66 20 66 69 72 73 74 20  nt len of first 
3520e 64 6f 63 69 64 2e 20 2a 2f 0a 23 69 66 6e 64 65  docid. */.#ifnde
3520f 66 20 4e 44 45 42 55 47 0a 20 20 73 71 6c 69 74  f NDEBUG.  sqlit
35210 65 5f 69 6e 74 36 34 20 69 4c 61 73 74 44 6f 63  e_int64 iLastDoc
35211 69 64 44 65 6c 74 61 3b 0a 23 65 6e 64 69 66 0a  idDelta;.#endif.
35212 0a 20 20 2f 2a 20 52 65 63 6f 64 65 20 74 68 65  .  /* Recode the
35213 20 69 6e 69 74 69 61 6c 20 64 6f 63 69 64 20 61   initial docid a
35214 73 20 64 65 6c 74 61 20 66 72 6f 6d 20 69 50 72  s delta from iPr
35215 65 76 44 6f 63 69 64 2e 20 2a 2f 0a 20 20 6e 46  evDocid. */.  nF
35216 69 72 73 74 4f 6c 64 20 3d 20 66 74 73 33 47 65  irstOld = fts3Ge
35217 74 56 61 72 69 6e 74 28 70 44 61 74 61 2c 20 26  tVarint(pData, &
35218 69 44 6f 63 69 64 29 3b 0a 20 20 61 73 73 65 72  iDocid);.  asser
35219 74 28 20 6e 46 69 72 73 74 4f 6c 64 3c 6e 44 61  t( nFirstOld<nDa
3521a 74 61 20 7c 7c 20 28 6e 46 69 72 73 74 4f 6c 64  ta || (nFirstOld
3521b 3d 3d 6e 44 61 74 61 20 26 26 20 70 57 72 69 74  ==nData && pWrit
3521c 65 72 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 44 4f  er->iType==DL_DO
3521d 43 49 44 53 29 20 29 3b 0a 20 20 6e 46 69 72 73  CIDS) );.  nFirs
3521e 74 4e 65 77 20 3d 20 66 74 73 33 50 75 74 56 61  tNew = fts3PutVa
3521f 72 69 6e 74 28 63 2c 20 69 46 69 72 73 74 44 6f  rint(c, iFirstDo
35220 63 69 64 2d 70 57 72 69 74 65 72 2d 3e 69 50 72  cid-pWriter->iPr
35221 65 76 44 6f 63 69 64 29 3b 0a 0a 20 20 2f 2a 20  evDocid);..  /* 
35222 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20  Verify that the 
35223 69 6e 63 6f 6d 69 6e 67 20 64 6f 63 6c 69 73 74  incoming doclist
35224 20 69 73 20 76 61 6c 69 64 20 41 4e 44 20 74 68   is valid AND th
35225 61 74 20 69 74 20 65 6e 64 73 20 77 69 74 68 0a  at it ends with.
35226 20 20 2a 2a 20 74 68 65 20 65 78 70 65 63 74 65    ** the expecte
35227 64 20 64 6f 63 69 64 2e 20 20 54 68 69 73 20 69  d docid.  This i
35228 73 20 65 73 73 65 6e 74 69 61 6c 20 62 65 63 61  s essential beca
35229 75 73 65 20 77 65 27 6c 6c 20 74 72 75 73 74 20  use we'll trust 
3522a 74 68 69 73 0a 20 20 2a 2a 20 64 6f 63 69 64 20  this.  ** docid 
3522b 69 6e 20 66 75 74 75 72 65 20 64 65 6c 74 61 2d  in future delta-
3522c 65 6e 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20  encoding..  */. 
3522d 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f   ASSERT_VALID_DO
3522e 43 4c 49 53 54 28 70 57 72 69 74 65 72 2d 3e 69  CLIST(pWriter->i
3522f 54 79 70 65 2c 20 70 44 61 74 61 2c 20 6e 44 61  Type, pData, nDa
35230 74 61 2c 20 26 69 4c 61 73 74 44 6f 63 69 64 44  ta, &iLastDocidD
35231 65 6c 74 61 29 3b 0a 20 20 61 73 73 65 72 74 28  elta);.  assert(
35232 20 69 4c 61 73 74 44 6f 63 69 64 3d 3d 69 46 69   iLastDocid==iFi
35233 72 73 74 44 6f 63 69 64 2d 69 44 6f 63 69 64 2b  rstDocid-iDocid+
35234 69 4c 61 73 74 44 6f 63 69 64 44 65 6c 74 61 20  iLastDocidDelta 
35235 29 3b 0a 0a 20 20 2f 2a 20 41 70 70 65 6e 64 20  );..  /* Append 
35236 72 65 63 6f 64 65 64 20 69 6e 69 74 69 61 6c 20  recoded initial 
35237 64 6f 63 69 64 20 61 6e 64 20 65 76 65 72 79 74  docid and everyt
35238 68 69 6e 67 20 65 6c 73 65 2e 20 20 52 65 73 74  hing else.  Rest
35239 20 6f 66 20 64 6f 63 69 64 73 0a 20 20 2a 2a 20   of docids.  ** 
3523a 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e  should have been
3523b 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 20 66   delta-encoded f
3523c 72 6f 6d 20 70 72 65 76 69 6f 75 73 20 69 6e 69  rom previous ini
3523d 74 69 61 6c 20 64 6f 63 69 64 2e 0a 20 20 2a 2f  tial docid..  */
3523e 0a 20 20 69 66 28 20 6e 46 69 72 73 74 4f 6c 64  .  if( nFirstOld
3523f 3c 6e 44 61 74 61 20 29 7b 0a 20 20 20 20 64 61  <nData ){.    da
35240 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32 28  taBufferAppend2(
35241 70 57 72 69 74 65 72 2d 3e 62 2c 20 63 2c 20 6e  pWriter->b, c, n
35242 46 69 72 73 74 4e 65 77 2c 0a 20 20 20 20 20 20  FirstNew,.      
35243 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35244 70 44 61 74 61 2b 6e 46 69 72 73 74 4f 6c 64 2c  pData+nFirstOld,
35245 20 6e 44 61 74 61 2d 6e 46 69 72 73 74 4f 6c 64   nData-nFirstOld
35246 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
35247 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
35248 28 70 57 72 69 74 65 72 2d 3e 62 2c 20 63 2c 20  (pWriter->b, c, 
35249 6e 46 69 72 73 74 4e 65 77 29 3b 0a 20 20 7d 0a  nFirstNew);.  }.
3524a 20 20 70 57 72 69 74 65 72 2d 3e 69 50 72 65 76    pWriter->iPrev
3524b 44 6f 63 69 64 20 3d 20 69 4c 61 73 74 44 6f 63  Docid = iLastDoc
3524c 69 64 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  id;.}.static voi
3524d 64 20 64 6c 77 43 6f 70 79 28 44 4c 57 72 69 74  d dlwCopy(DLWrit
3524e 65 72 20 2a 70 57 72 69 74 65 72 2c 20 44 4c 52  er *pWriter, DLR
3524f 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
35250 0a 20 20 64 6c 77 41 70 70 65 6e 64 28 70 57 72  .  dlwAppend(pWr
35251 69 74 65 72 2c 20 64 6c 72 44 6f 63 44 61 74 61  iter, dlrDocData
35252 28 70 52 65 61 64 65 72 29 2c 20 64 6c 72 44 6f  (pReader), dlrDo
35253 63 44 61 74 61 42 79 74 65 73 28 70 52 65 61 64  cDataBytes(pRead
35254 65 72 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  er),.           
35255 20 64 6c 72 44 6f 63 69 64 28 70 52 65 61 64 65   dlrDocid(pReade
35256 72 29 2c 20 64 6c 72 44 6f 63 69 64 28 70 52 65  r), dlrDocid(pRe
35257 61 64 65 72 29 29 3b 0a 7d 0a 73 74 61 74 69 63  ader));.}.static
35258 20 76 6f 69 64 20 64 6c 77 41 64 64 28 44 4c 57   void dlwAdd(DLW
35259 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c 20  riter *pWriter, 
3525a 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f  sqlite_int64 iDo
3525b 63 69 64 29 7b 0a 20 20 63 68 61 72 20 63 5b 56  cid){.  char c[V
3525c 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e  ARINT_MAX];.  in
3525d 74 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72  t n = fts3PutVar
3525e 69 6e 74 28 63 2c 20 69 44 6f 63 69 64 2d 70 57  int(c, iDocid-pW
3525f 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f 63 69  riter->iPrevDoci
35260 64 29 3b 0a 0a 20 20 2f 2a 20 44 6f 63 69 64 73  d);..  /* Docids
35261 20 6d 75 73 74 20 61 73 63 65 6e 64 2e 20 2a 2f   must ascend. */
35262 0a 20 20 61 73 73 65 72 74 28 20 21 70 57 72 69  .  assert( !pWri
35263 74 65 72 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f  ter->has_iPrevDo
35264 63 69 64 20 7c 7c 20 69 44 6f 63 69 64 3e 70 57  cid || iDocid>pW
35265 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f 63 69  riter->iPrevDoci
35266 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  d );.  assert( p
35267 57 72 69 74 65 72 2d 3e 69 54 79 70 65 3d 3d 44  Writer->iType==D
35268 4c 5f 44 4f 43 49 44 53 20 29 3b 0a 0a 20 20 64  L_DOCIDS );..  d
35269 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
3526a 70 57 72 69 74 65 72 2d 3e 62 2c 20 63 2c 20 6e  pWriter->b, c, n
3526b 29 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50  );.  pWriter->iP
3526c 72 65 76 44 6f 63 69 64 20 3d 20 69 44 6f 63 69  revDocid = iDoci
3526d 64 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  d;.#ifndef NDEBU
3526e 47 0a 20 20 70 57 72 69 74 65 72 2d 3e 68 61 73  G.  pWriter->has
3526f 5f 69 50 72 65 76 44 6f 63 69 64 20 3d 20 31 3b  _iPrevDocid = 1;
35270 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 2a 2a 2a  .#endif.}../****
35271 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35272 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35273 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35274 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
35275 0a 2f 2a 20 50 4c 52 65 61 64 65 72 20 69 73 20  ./* PLReader is 
35276 75 73 65 64 20 74 6f 20 72 65 61 64 20 64 61 74  used to read dat
35277 61 20 66 72 6f 6d 20 61 20 64 6f 63 75 6d 65 6e  a from a documen
35278 74 27 73 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73  t's position lis
35279 74 2e 20 20 41 73 0a 2a 2a 20 74 68 65 20 63 61  t.  As.** the ca
3527a 6c 6c 65 72 20 73 74 65 70 73 20 74 68 72 6f 75  ller steps throu
3527b 67 68 20 74 68 65 20 6c 69 73 74 2c 20 64 61 74  gh the list, dat
3527c 61 20 69 73 20 63 61 63 68 65 64 20 73 6f 20 74  a is cached so t
3527d 68 61 74 20 76 61 72 69 6e 74 73 0a 2a 2a 20 6f  hat varints.** o
3527e 6e 6c 79 20 6e 65 65 64 20 74 6f 20 62 65 20 64  nly need to be d
3527f 65 63 6f 64 65 64 20 6f 6e 63 65 2e 0a 2a 2a 0a  ecoded once..**.
35280 2a 2a 20 70 6c 72 49 6e 69 74 2c 20 70 6c 72 44  ** plrInit, plrD
35281 65 73 74 72 6f 79 20 2d 20 63 72 65 61 74 65 2f  estroy - create/
35282 64 65 73 74 72 6f 79 20 61 20 72 65 61 64 65 72  destroy a reader
35283 2e 0a 2a 2a 20 70 6c 72 43 6f 6c 75 6d 6e 2c 20  ..** plrColumn, 
35284 70 6c 72 50 6f 73 69 74 69 6f 6e 2c 20 70 6c 72  plrPosition, plr
35285 53 74 61 72 74 4f 66 66 73 65 74 2c 20 70 6c 72  StartOffset, plr
35286 45 6e 64 4f 66 66 73 65 74 20 2d 20 61 63 63 65  EndOffset - acce
35287 73 73 6f 72 73 0a 2a 2a 20 70 6c 72 41 74 45 6e  ssors.** plrAtEn
35288 64 20 2d 20 61 74 20 65 6e 64 20 6f 66 20 73 74  d - at end of st
35289 72 65 61 6d 2c 20 6f 6e 6c 79 20 63 61 6c 6c 20  ream, only call 
3528a 70 6c 72 44 65 73 74 72 6f 79 20 6f 6e 63 65 20  plrDestroy once 
3528b 74 72 75 65 2e 0a 2a 2a 20 70 6c 72 53 74 65 70  true..** plrStep
3528c 20 2d 20 73 74 65 70 20 74 6f 20 74 68 65 20 6e   - step to the n
3528d 65 78 74 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a  ext element..*/.
3528e 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50  typedef struct P
3528f 4c 52 65 61 64 65 72 20 7b 0a 20 20 2f 2a 20 54  LReader {.  /* T
35290 68 65 73 65 20 72 65 66 65 72 20 74 6f 20 74 68  hese refer to th
35291 65 20 6e 65 78 74 20 70 6f 73 69 74 69 6f 6e 27  e next position'
35292 73 20 64 61 74 61 2e 20 20 6e 44 61 74 61 20 77  s data.  nData w
35293 69 6c 6c 20 72 65 61 63 68 20 30 20 77 68 65 6e  ill reach 0 when
35294 0a 20 20 2a 2a 20 72 65 61 64 69 6e 67 20 74 68  .  ** reading th
35295 65 20 6c 61 73 74 20 70 6f 73 69 74 69 6f 6e 2c  e last position,
35296 20 73 6f 20 70 6c 72 53 74 65 70 28 29 20 73 69   so plrStep() si
35297 67 6e 61 6c 73 20 45 4f 46 20 62 79 20 73 65 74  gnals EOF by set
35298 74 69 6e 67 0a 20 20 2a 2a 20 70 44 61 74 61 20  ting.  ** pData 
35299 74 6f 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20  to NULL..  */.  
3529a 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
3529b 61 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a  a;.  int nData;.
3529c 0a 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69  .  DocListType i
3529d 54 79 70 65 3b 0a 20 20 69 6e 74 20 69 43 6f 6c  Type;.  int iCol
3529e 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  umn;         /* 
3529f 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20  the last column 
352a0 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20 69 50  read */.  int iP
352a1 6f 73 69 74 69 6f 6e 3b 20 20 20 20 20 20 20 2f  osition;       /
352a2 2a 20 74 68 65 20 6c 61 73 74 20 70 6f 73 69 74  * the last posit
352a3 69 6f 6e 20 72 65 61 64 20 2a 2f 0a 20 20 69 6e  ion read */.  in
352a4 74 20 69 53 74 61 72 74 4f 66 66 73 65 74 3b 20  t iStartOffset; 
352a5 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 73     /* the last s
352a6 74 61 72 74 20 6f 66 66 73 65 74 20 72 65 61 64  tart offset read
352a7 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e 64 4f 66   */.  int iEndOf
352a8 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 74 68  fset;      /* th
352a9 65 20 6c 61 73 74 20 65 6e 64 20 6f 66 66 73 65  e last end offse
352aa 74 20 72 65 61 64 20 2a 2f 0a 7d 20 50 4c 52 65  t read */.} PLRe
352ab 61 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 69 6e  ader;..static in
352ac 74 20 70 6c 72 41 74 45 6e 64 28 50 4c 52 65 61  t plrAtEnd(PLRea
352ad 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
352ae 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d   return pReader-
352af 3e 70 44 61 74 61 3d 3d 4e 55 4c 4c 3b 0a 7d 0a  >pData==NULL;.}.
352b0 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72 43 6f  static int plrCo
352b1 6c 75 6d 6e 28 50 4c 52 65 61 64 65 72 20 2a 70  lumn(PLReader *p
352b2 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72  Reader){.  asser
352b3 74 28 20 21 70 6c 72 41 74 45 6e 64 28 70 52 65  t( !plrAtEnd(pRe
352b4 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72  ader) );.  retur
352b5 6e 20 70 52 65 61 64 65 72 2d 3e 69 43 6f 6c 75  n pReader->iColu
352b6 6d 6e 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74  mn;.}.static int
352b7 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 50 4c 52   plrPosition(PLR
352b8 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
352b9 0a 20 20 61 73 73 65 72 74 28 20 21 70 6c 72 41  .  assert( !plrA
352ba 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
352bb 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64 65  .  return pReade
352bc 72 2d 3e 69 50 6f 73 69 74 69 6f 6e 3b 0a 7d 0a  r->iPosition;.}.
352bd 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72 53 74  static int plrSt
352be 61 72 74 4f 66 66 73 65 74 28 50 4c 52 65 61 64  artOffset(PLRead
352bf 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
352c0 61 73 73 65 72 74 28 20 21 70 6c 72 41 74 45 6e  assert( !plrAtEn
352c1 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20  d(pReader) );.  
352c2 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
352c3 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a 7d 0a  iStartOffset;.}.
352c4 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72 45 6e  static int plrEn
352c5 64 4f 66 66 73 65 74 28 50 4c 52 65 61 64 65 72  dOffset(PLReader
352c6 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73   *pReader){.  as
352c7 73 65 72 74 28 20 21 70 6c 72 41 74 45 6e 64 28  sert( !plrAtEnd(
352c8 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65  pReader) );.  re
352c9 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 69 45  turn pReader->iE
352ca 6e 64 4f 66 66 73 65 74 3b 0a 7d 0a 73 74 61 74  ndOffset;.}.stat
352cb 69 63 20 76 6f 69 64 20 70 6c 72 53 74 65 70 28  ic void plrStep(
352cc 50 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  PLReader *pReade
352cd 72 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a  r){.  int i, n;.
352ce 0a 20 20 61 73 73 65 72 74 28 20 21 70 6c 72 41  .  assert( !plrA
352cf 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b  tEnd(pReader) );
352d0 0a 0a 20 20 69 66 28 20 70 52 65 61 64 65 72 2d  ..  if( pReader-
352d1 3e 6e 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20  >nData==0 ){.   
352d2 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 20   pReader->pData 
352d3 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72 65 74 75  = NULL;.    retu
352d4 72 6e 3b 0a 20 20 7d 0a 0a 20 20 6e 20 3d 20 66  rn;.  }..  n = f
352d5 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
352d6 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26  Reader->pData, &
352d7 69 29 3b 0a 20 20 69 66 28 20 69 3d 3d 50 4f 53  i);.  if( i==POS
352d8 5f 43 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 6e  _COLUMN ){.    n
352d9 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e   += fts3GetVarin
352da 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61  t32(pReader->pDa
352db 74 61 2b 6e 2c 20 26 70 52 65 61 64 65 72 2d 3e  ta+n, &pReader->
352dc 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 70 52  iColumn);.    pR
352dd 65 61 64 65 72 2d 3e 69 50 6f 73 69 74 69 6f 6e  eader->iPosition
352de 20 3d 20 30 3b 0a 20 20 20 20 70 52 65 61 64 65   = 0;.    pReade
352df 72 2d 3e 69 53 74 61 72 74 4f 66 66 73 65 74 20  r->iStartOffset 
352e0 3d 20 30 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74  = 0;.    n += ft
352e1 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52  s3GetVarint32(pR
352e2 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20  eader->pData+n, 
352e3 26 69 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 68  &i);.  }.  /* Sh
352e4 6f 75 6c 64 20 6e 65 76 65 72 20 73 65 65 20 61  ould never see a
352e5 64 6a 61 63 65 6e 74 20 63 6f 6c 75 6d 6e 20 63  djacent column c
352e6 68 61 6e 67 65 73 2e 20 2a 2f 0a 20 20 61 73 73  hanges. */.  ass
352e7 65 72 74 28 20 69 21 3d 50 4f 53 5f 43 4f 4c 55  ert( i!=POS_COLU
352e8 4d 4e 20 29 3b 0a 0a 20 20 69 66 28 20 69 3d 3d  MN );..  if( i==
352e9 50 4f 53 5f 45 4e 44 20 29 7b 0a 20 20 20 20 70  POS_END ){.    p
352ea 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20  Reader->nData = 
352eb 30 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  0;.    pReader->
352ec 70 44 61 74 61 20 3d 20 4e 55 4c 4c 3b 0a 20 20  pData = NULL;.  
352ed 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
352ee 20 70 52 65 61 64 65 72 2d 3e 69 50 6f 73 69 74   pReader->iPosit
352ef 69 6f 6e 20 2b 3d 20 69 2d 50 4f 53 5f 42 41 53  ion += i-POS_BAS
352f0 45 3b 0a 20 20 69 66 28 20 70 52 65 61 64 65 72  E;.  if( pReader
352f1 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49  ->iType==DL_POSI
352f2 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 7b  TIONS_OFFSETS ){
352f3 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65  .    n += fts3Ge
352f4 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65  tVarint32(pReade
352f5 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69 29 3b  r->pData+n, &i);
352f6 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 69 53  .    pReader->iS
352f7 74 61 72 74 4f 66 66 73 65 74 20 2b 3d 20 69 3b  tartOffset += i;
352f8 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65  .    n += fts3Ge
352f9 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64 65  tVarint32(pReade
352fa 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69 29 3b  r->pData+n, &i);
352fb 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 69 45  .    pReader->iE
352fc 6e 64 4f 66 66 73 65 74 20 3d 20 70 52 65 61 64  ndOffset = pRead
352fd 65 72 2d 3e 69 53 74 61 72 74 4f 66 66 73 65 74  er->iStartOffset
352fe 2b 69 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  +i;.  }.  assert
352ff 28 20 6e 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44  ( n<=pReader->nD
35300 61 74 61 20 29 3b 0a 20 20 70 52 65 61 64 65 72  ata );.  pReader
35301 2d 3e 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20 20  ->pData += n;.  
35302 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 2d  pReader->nData -
35303 3d 20 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76  = n;.}..static v
35304 6f 69 64 20 70 6c 72 49 6e 69 74 28 50 4c 52 65  oid plrInit(PLRe
35305 61 64 65 72 20 2a 70 52 65 61 64 65 72 2c 20 44  ader *pReader, D
35306 4c 52 65 61 64 65 72 20 2a 70 44 4c 52 65 61 64  LReader *pDLRead
35307 65 72 29 7b 0a 20 20 70 52 65 61 64 65 72 2d 3e  er){.  pReader->
35308 70 44 61 74 61 20 3d 20 64 6c 72 50 6f 73 44 61  pData = dlrPosDa
35309 74 61 28 70 44 4c 52 65 61 64 65 72 29 3b 0a 20  ta(pDLReader);. 
3530a 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20   pReader->nData 
3530b 3d 20 64 6c 72 50 6f 73 44 61 74 61 4c 65 6e 28  = dlrPosDataLen(
3530c 70 44 4c 52 65 61 64 65 72 29 3b 0a 20 20 70 52  pDLReader);.  pR
3530d 65 61 64 65 72 2d 3e 69 54 79 70 65 20 3d 20 70  eader->iType = p
3530e 44 4c 52 65 61 64 65 72 2d 3e 69 54 79 70 65 3b  DLReader->iType;
3530f 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 43 6f 6c  .  pReader->iCol
35310 75 6d 6e 20 3d 20 30 3b 0a 20 20 70 52 65 61 64  umn = 0;.  pRead
35311 65 72 2d 3e 69 50 6f 73 69 74 69 6f 6e 20 3d 20  er->iPosition = 
35312 30 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 53  0;.  pReader->iS
35313 74 61 72 74 4f 66 66 73 65 74 20 3d 20 30 3b 0a  tartOffset = 0;.
35314 20 20 70 52 65 61 64 65 72 2d 3e 69 45 6e 64 4f    pReader->iEndO
35315 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 70 6c 72  ffset = 0;.  plr
35316 53 74 65 70 28 70 52 65 61 64 65 72 29 3b 0a 7d  Step(pReader);.}
35317 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 6c 72  .static void plr
35318 44 65 73 74 72 6f 79 28 50 4c 52 65 61 64 65 72  Destroy(PLReader
35319 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 53 43   *pReader){.  SC
3531a 52 41 4d 42 4c 45 28 70 52 65 61 64 65 72 29 3b  RAMBLE(pReader);
3531b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
3531c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3531d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3531e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3531f 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 50 4c 57  ********/./* PLW
35320 72 69 74 65 72 20 69 73 20 75 73 65 64 20 69 6e  riter is used in
35321 20 63 6f 6e 73 74 72 75 63 74 69 6e 67 20 61 20   constructing a 
35322 64 6f 63 75 6d 65 6e 74 27 73 20 70 6f 73 69 74  document's posit
35323 69 6f 6e 20 6c 69 73 74 2e 20 20 41 73 20 61 0a  ion list.  As a.
35324 2a 2a 20 63 6f 6e 76 65 6e 69 65 6e 63 65 2c 20  ** convenience, 
35325 69 66 20 69 54 79 70 65 20 69 73 20 44 4c 5f 44  if iType is DL_D
35326 4f 43 49 44 53 2c 20 50 4c 57 72 69 74 65 72 20  OCIDS, PLWriter 
35327 62 65 63 6f 6d 65 73 20 61 20 6e 6f 2d 6f 70 2e  becomes a no-op.
35328 0a 2a 2a 20 50 4c 57 72 69 74 65 72 20 77 72 69  .** PLWriter wri
35329 74 65 73 20 74 6f 20 74 68 65 20 61 73 73 6f 63  tes to the assoc
3532a 69 61 74 65 64 20 44 4c 57 72 69 74 65 72 27 73  iated DLWriter's
3532b 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70   buffer..**.** p
3532c 6c 77 49 6e 69 74 20 2d 20 69 6e 69 74 20 66 6f  lwInit - init fo
3532d 72 20 77 72 69 74 69 6e 67 20 61 20 64 6f 63 75  r writing a docu
3532e 6d 65 6e 74 27 73 20 70 6f 73 6c 69 73 74 2e 0a  ment's poslist..
3532f 2a 2a 20 70 6c 77 44 65 73 74 72 6f 79 20 2d 20  ** plwDestroy - 
35330 63 6c 65 61 72 20 61 20 77 72 69 74 65 72 2e 0a  clear a writer..
35331 2a 2a 20 70 6c 77 41 64 64 20 2d 20 61 70 70 65  ** plwAdd - appe
35332 6e 64 20 70 6f 73 69 74 69 6f 6e 20 61 6e 64 20  nd position and 
35333 6f 66 66 73 65 74 20 69 6e 66 6f 72 6d 61 74 69  offset informati
35334 6f 6e 2e 0a 2a 2a 20 70 6c 77 43 6f 70 79 20 2d  on..** plwCopy -
35335 20 63 6f 70 79 20 6e 65 78 74 20 70 6f 73 69 74   copy next posit
35336 69 6f 6e 27 73 20 64 61 74 61 20 66 72 6f 6d 20  ion's data from 
35337 72 65 61 64 65 72 20 74 6f 20 77 72 69 74 65 72  reader to writer
35338 2e 0a 2a 2a 20 70 6c 77 54 65 72 6d 69 6e 61 74  ..** plwTerminat
35339 65 20 2d 20 61 64 64 20 61 6e 79 20 6e 65 63 65  e - add any nece
3533a 73 73 61 72 79 20 64 6f 63 6c 69 73 74 20 74 65  ssary doclist te
3533b 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20  rminator..**.** 
3533c 43 61 6c 6c 69 6e 67 20 70 6c 77 41 64 64 28 29  Calling plwAdd()
3533d 20 61 66 74 65 72 20 70 6c 77 54 65 72 6d 69 6e   after plwTermin
3533e 61 74 65 28 29 20 6d 61 79 20 72 65 73 75 6c 74  ate() may result
3533f 20 69 6e 20 61 20 63 6f 72 72 75 70 74 0a 2a 2a   in a corrupt.**
35340 20 64 6f 63 6c 69 73 74 2e 0a 2a 2f 0a 2f 2a 20   doclist..*/./* 
35341 54 4f 44 4f 28 73 68 65 73 73 29 20 55 6e 74 69  TODO(shess) Unti
35342 6c 20 77 65 27 76 65 20 77 72 69 74 74 65 6e 20  l we've written 
35343 74 68 65 20 73 65 63 6f 6e 64 20 69 74 65 6d 2c  the second item,
35344 20 77 65 20 63 61 6e 20 63 61 63 68 65 20 74 68   we can cache th
35345 65 0a 2a 2a 20 66 69 72 73 74 20 69 74 65 6d 27  e.** first item'
35346 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20  s information.  
35347 54 68 65 6e 20 77 65 27 64 20 68 61 76 65 20 74  Then we'd have t
35348 68 72 65 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a  hree states:.**.
35349 2a 2a 20 2d 20 69 6e 69 74 69 61 6c 69 7a 65 64  ** - initialized
3534a 20 77 69 74 68 20 64 6f 63 69 64 2c 20 6e 6f 20   with docid, no 
3534b 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a 2a 20 2d 20  positions..** - 
3534c 64 6f 63 69 64 20 61 6e 64 20 6f 6e 65 20 70 6f  docid and one po
3534d 73 69 74 69 6f 6e 2e 0a 2a 2a 20 2d 20 64 6f 63  sition..** - doc
3534e 69 64 20 61 6e 64 20 6d 75 6c 74 69 70 6c 65 20  id and multiple 
3534f 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  positions..**.**
35350 20 4f 6e 6c 79 20 74 68 65 20 6c 61 73 74 20 73   Only the last s
35351 74 61 74 65 20 6e 65 65 64 73 20 74 6f 20 61 63  tate needs to ac
35352 74 75 61 6c 6c 79 20 77 72 69 74 65 20 74 6f 20  tually write to 
35353 64 6c 77 2d 3e 62 2c 20 77 68 69 63 68 20 77 6f  dlw->b, which wo
35354 75 6c 64 0a 2a 2a 20 62 65 20 61 6e 20 69 6d 70  uld.** be an imp
35355 72 6f 76 65 6d 65 6e 74 20 69 6e 20 74 68 65 20  rovement in the 
35356 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 63 61 73 65  DLCollector case
35357 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
35358 75 63 74 20 50 4c 57 72 69 74 65 72 20 7b 0a 20  uct PLWriter {. 
35359 20 44 4c 57 72 69 74 65 72 20 2a 64 6c 77 3b 0a   DLWriter *dlw;.
3535a 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20  .  int iColumn; 
3535b 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 63     /* the last c
3535c 6f 6c 75 6d 6e 20 77 72 69 74 74 65 6e 20 2a 2f  olumn written */
3535d 0a 20 20 69 6e 74 20 69 50 6f 73 3b 20 20 20 20  .  int iPos;    
3535e 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 70     /* the last p
3535f 6f 73 69 74 69 6f 6e 20 77 72 69 74 74 65 6e 20  osition written 
35360 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74  */.  int iOffset
35361 3b 20 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74  ;    /* the last
35362 20 73 74 61 72 74 20 6f 66 66 73 65 74 20 77 72   start offset wr
35363 69 74 74 65 6e 20 2a 2f 0a 7d 20 50 4c 57 72 69  itten */.} PLWri
35364 74 65 72 3b 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68  ter;../* TODO(sh
35365 65 73 73 29 20 49 6e 20 74 68 65 20 63 61 73 65  ess) In the case
35366 20 77 68 65 72 65 20 74 68 65 20 70 61 72 65 6e   where the paren
35367 74 20 69 73 20 72 65 61 64 69 6e 67 20 74 68 65  t is reading the
35368 73 65 20 76 61 6c 75 65 73 0a 2a 2a 20 66 72 6f  se values.** fro
35369 6d 20 61 20 50 4c 52 65 61 64 65 72 2c 20 77 65  m a PLReader, we
3536a 20 63 6f 75 6c 64 20 6f 70 74 69 6d 69 7a 65 20   could optimize 
3536b 74 6f 20 61 20 63 6f 70 79 20 69 66 20 74 68 61  to a copy if tha
3536c 74 20 50 4c 52 65 61 64 65 72 20 68 61 73 0a 2a  t PLReader has.*
3536d 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20  * the same type 
3536e 61 73 20 70 57 72 69 74 65 72 2e 0a 2a 2f 0a 73  as pWriter..*/.s
3536f 74 61 74 69 63 20 76 6f 69 64 20 70 6c 77 41 64  tatic void plwAd
35370 64 28 50 4c 57 72 69 74 65 72 20 2a 70 57 72 69  d(PLWriter *pWri
35371 74 65 72 2c 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  ter, int iColumn
35372 2c 20 69 6e 74 20 69 50 6f 73 2c 0a 20 20 20 20  , int iPos,.    
35373 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
35374 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65 74 2c  nt iStartOffset,
35375 20 69 6e 74 20 69 45 6e 64 4f 66 66 73 65 74 29   int iEndOffset)
35376 7b 0a 20 20 2f 2a 20 57 6f 72 73 74 2d 63 61 73  {.  /* Worst-cas
35377 65 20 73 70 61 63 65 20 66 6f 72 20 50 4f 53 5f  e space for POS_
35378 43 4f 4c 55 4d 4e 2c 20 69 43 6f 6c 75 6d 6e 2c  COLUMN, iColumn,
35379 20 69 50 6f 73 44 65 6c 74 61 2c 0a 20 20 2a 2a   iPosDelta,.  **
3537a 20 69 53 74 61 72 74 4f 66 66 73 65 74 44 65 6c   iStartOffsetDel
3537b 74 61 2c 20 61 6e 64 20 69 45 6e 64 4f 66 66 73  ta, and iEndOffs
3537c 65 74 44 65 6c 74 61 2e 0a 20 20 2a 2f 0a 20 20  etDelta..  */.  
3537d 63 68 61 72 20 63 5b 35 2a 56 41 52 49 4e 54 5f  char c[5*VARINT_
3537e 4d 41 58 5d 3b 0a 20 20 69 6e 74 20 6e 20 3d 20  MAX];.  int n = 
3537f 30 3b 0a 0a 20 20 2f 2a 20 42 61 6e 20 70 6c 77  0;..  /* Ban plw
35380 41 64 64 28 29 20 61 66 74 65 72 20 70 6c 77 54  Add() after plwT
35381 65 72 6d 69 6e 61 74 65 28 29 2e 20 2a 2f 0a 20  erminate(). */. 
35382 20 61 73 73 65 72 74 28 20 70 57 72 69 74 65 72   assert( pWriter
35383 2d 3e 69 50 6f 73 21 3d 2d 31 20 29 3b 0a 0a 20  ->iPos!=-1 );.. 
35384 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e 64 6c   if( pWriter->dl
35385 77 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 44 4f 43  w->iType==DL_DOC
35386 49 44 53 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20  IDS ) return;.. 
35387 20 69 66 28 20 69 43 6f 6c 75 6d 6e 21 3d 70 57   if( iColumn!=pW
35388 72 69 74 65 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29  riter->iColumn )
35389 7b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50  {.    n += fts3P
3538a 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 50 4f  utVarint(c+n, PO
3538b 53 5f 43 4f 4c 55 4d 4e 29 3b 0a 20 20 20 20 6e  S_COLUMN);.    n
3538c 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e   += fts3PutVarin
3538d 74 28 63 2b 6e 2c 20 69 43 6f 6c 75 6d 6e 29 3b  t(c+n, iColumn);
3538e 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69 43  .    pWriter->iC
3538f 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 75 6d 6e 3b  olumn = iColumn;
35390 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69 50  .    pWriter->iP
35391 6f 73 20 3d 20 30 3b 0a 20 20 20 20 70 57 72 69  os = 0;.    pWri
35392 74 65 72 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30  ter->iOffset = 0
35393 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
35394 69 50 6f 73 3e 3d 70 57 72 69 74 65 72 2d 3e 69  iPos>=pWriter->i
35395 50 6f 73 20 29 3b 0a 20 20 6e 20 2b 3d 20 66 74  Pos );.  n += ft
35396 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c  s3PutVarint(c+n,
35397 20 50 4f 53 5f 42 41 53 45 2b 28 69 50 6f 73 2d   POS_BASE+(iPos-
35398 70 57 72 69 74 65 72 2d 3e 69 50 6f 73 29 29 3b  pWriter->iPos));
35399 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50 6f 73  .  pWriter->iPos
3539a 20 3d 20 69 50 6f 73 3b 0a 20 20 69 66 28 20 70   = iPos;.  if( p
3539b 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 54 79  Writer->dlw->iTy
3539c 70 65 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53  pe==DL_POSITIONS
3539d 5f 4f 46 46 53 45 54 53 20 29 7b 0a 20 20 20 20  _OFFSETS ){.    
3539e 61 73 73 65 72 74 28 20 69 53 74 61 72 74 4f 66  assert( iStartOf
3539f 66 73 65 74 3e 3d 70 57 72 69 74 65 72 2d 3e 69  fset>=pWriter->i
353a0 4f 66 66 73 65 74 20 29 3b 0a 20 20 20 20 6e 20  Offset );.    n 
353a1 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74  += fts3PutVarint
353a2 28 63 2b 6e 2c 20 69 53 74 61 72 74 4f 66 66 73  (c+n, iStartOffs
353a3 65 74 2d 70 57 72 69 74 65 72 2d 3e 69 4f 66 66  et-pWriter->iOff
353a4 73 65 74 29 3b 0a 20 20 20 20 70 57 72 69 74 65  set);.    pWrite
353a5 72 2d 3e 69 4f 66 66 73 65 74 20 3d 20 69 53 74  r->iOffset = iSt
353a6 61 72 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 61  artOffset;.    a
353a7 73 73 65 72 74 28 20 69 45 6e 64 4f 66 66 73 65  ssert( iEndOffse
353a8 74 3e 3d 69 53 74 61 72 74 4f 66 66 73 65 74 20  t>=iStartOffset 
353a9 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33  );.    n += fts3
353aa 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 69  PutVarint(c+n, i
353ab 45 6e 64 4f 66 66 73 65 74 2d 69 53 74 61 72 74  EndOffset-iStart
353ac 4f 66 66 73 65 74 29 3b 0a 20 20 7d 0a 20 20 64  Offset);.  }.  d
353ad 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
353ae 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 62 2c  pWriter->dlw->b,
353af 20 63 2c 20 6e 29 3b 0a 7d 0a 73 74 61 74 69 63   c, n);.}.static
353b0 20 76 6f 69 64 20 70 6c 77 43 6f 70 79 28 50 4c   void plwCopy(PL
353b1 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c  Writer *pWriter,
353b2 20 50 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64   PLReader *pRead
353b3 65 72 29 7b 0a 20 20 70 6c 77 41 64 64 28 70 57  er){.  plwAdd(pW
353b4 72 69 74 65 72 2c 20 70 6c 72 43 6f 6c 75 6d 6e  riter, plrColumn
353b5 28 70 52 65 61 64 65 72 29 2c 20 70 6c 72 50 6f  (pReader), plrPo
353b6 73 69 74 69 6f 6e 28 70 52 65 61 64 65 72 29 2c  sition(pReader),
353b7 0a 20 20 20 20 20 20 20 20 20 70 6c 72 53 74 61  .         plrSta
353b8 72 74 4f 66 66 73 65 74 28 70 52 65 61 64 65 72  rtOffset(pReader
353b9 29 2c 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 28  ), plrEndOffset(
353ba 70 52 65 61 64 65 72 29 29 3b 0a 7d 0a 73 74 61  pReader));.}.sta
353bb 74 69 63 20 76 6f 69 64 20 70 6c 77 49 6e 69 74  tic void plwInit
353bc 28 50 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74  (PLWriter *pWrit
353bd 65 72 2c 20 44 4c 57 72 69 74 65 72 20 2a 64 6c  er, DLWriter *dl
353be 77 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  w, sqlite_int64 
353bf 69 44 6f 63 69 64 29 7b 0a 20 20 63 68 61 72 20  iDocid){.  char 
353c0 63 5b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20  c[VARINT_MAX];. 
353c1 20 69 6e 74 20 6e 3b 0a 0a 20 20 70 57 72 69 74   int n;..  pWrit
353c2 65 72 2d 3e 64 6c 77 20 3d 20 64 6c 77 3b 0a 0a  er->dlw = dlw;..
353c3 20 20 2f 2a 20 44 6f 63 69 64 73 20 6d 75 73 74    /* Docids must
353c4 20 61 73 63 65 6e 64 2e 20 2a 2f 0a 20 20 61 73   ascend. */.  as
353c5 73 65 72 74 28 20 21 70 57 72 69 74 65 72 2d 3e  sert( !pWriter->
353c6 64 6c 77 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f  dlw->has_iPrevDo
353c7 63 69 64 20 7c 7c 20 69 44 6f 63 69 64 3e 70 57  cid || iDocid>pW
353c8 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 50 72 65  riter->dlw->iPre
353c9 76 44 6f 63 69 64 20 29 3b 0a 20 20 6e 20 3d 20  vDocid );.  n = 
353ca 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c  fts3PutVarint(c,
353cb 20 69 44 6f 63 69 64 2d 70 57 72 69 74 65 72 2d   iDocid-pWriter-
353cc 3e 64 6c 77 2d 3e 69 50 72 65 76 44 6f 63 69 64  >dlw->iPrevDocid
353cd 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 41  );.  dataBufferA
353ce 70 70 65 6e 64 28 70 57 72 69 74 65 72 2d 3e 64  ppend(pWriter->d
353cf 6c 77 2d 3e 62 2c 20 63 2c 20 6e 29 3b 0a 20 20  lw->b, c, n);.  
353d0 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 50  pWriter->dlw->iP
353d1 72 65 76 44 6f 63 69 64 20 3d 20 69 44 6f 63 69  revDocid = iDoci
353d2 64 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  d;.#ifndef NDEBU
353d3 47 0a 20 20 70 57 72 69 74 65 72 2d 3e 64 6c 77  G.  pWriter->dlw
353d4 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f 63 69 64  ->has_iPrevDocid
353d5 20 3d 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20   = 1;.#endif..  
353d6 70 57 72 69 74 65 72 2d 3e 69 43 6f 6c 75 6d 6e  pWriter->iColumn
353d7 20 3d 20 30 3b 0a 20 20 70 57 72 69 74 65 72 2d   = 0;.  pWriter-
353d8 3e 69 50 6f 73 20 3d 20 30 3b 0a 20 20 70 57 72  >iPos = 0;.  pWr
353d9 69 74 65 72 2d 3e 69 4f 66 66 73 65 74 20 3d 20  iter->iOffset = 
353da 30 3b 0a 7d 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  0;.}./* TODO(she
353db 73 73 29 20 53 68 6f 75 6c 64 20 70 6c 77 44 65  ss) Should plwDe
353dc 73 74 72 6f 79 28 29 20 61 6c 73 6f 20 74 65 72  stroy() also ter
353dd 6d 69 6e 61 74 65 20 74 68 65 20 64 6f 63 6c 69  minate the docli
353de 73 74 3f 20 20 42 75 74 0a 2a 2a 20 74 68 65 6e  st?  But.** then
353df 20 70 6c 77 44 65 73 74 72 6f 79 28 29 20 77 6f   plwDestroy() wo
353e0 75 6c 64 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65  uld no longer be
353e1 20 6a 75 73 74 20 61 20 64 65 73 74 72 75 63 74   just a destruct
353e2 6f 72 2c 20 69 74 20 77 6f 75 6c 64 0a 2a 2a 20  or, it would.** 
353e3 61 6c 73 6f 20 62 65 20 64 6f 69 6e 67 20 77 6f  also be doing wo
353e4 72 6b 2c 20 77 68 69 63 68 20 69 73 6e 27 74 20  rk, which isn't 
353e5 63 6f 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20  consistent with 
353e6 74 68 65 20 6f 76 65 72 61 6c 6c 20 69 64 69 6f  the overall idio
353e7 6d 2e 0a 2a 2a 20 41 6e 6f 74 68 65 72 20 6f 70  m..** Another op
353e8 74 69 6f 6e 20 77 6f 75 6c 64 20 62 65 20 66 6f  tion would be fo
353e9 72 20 70 6c 77 41 64 64 28 29 20 74 6f 20 61 6c  r plwAdd() to al
353ea 77 61 79 73 20 61 70 70 65 6e 64 20 61 6e 79 20  ways append any 
353eb 6e 65 63 65 73 73 61 72 79 0a 2a 2a 20 74 65 72  necessary.** ter
353ec 6d 69 6e 61 74 6f 72 2c 20 73 6f 20 74 68 61 74  minator, so that
353ed 20 74 68 65 20 6f 75 74 70 75 74 20 69 73 20 61   the output is a
353ee 6c 77 61 79 73 20 63 6f 72 72 65 63 74 2e 20 20  lways correct.  
353ef 42 75 74 20 74 68 61 74 20 77 6f 75 6c 64 0a 2a  But that would.*
353f0 2a 20 61 64 64 20 69 6e 63 72 65 6d 65 6e 74 61  * add incrementa
353f1 6c 20 77 6f 72 6b 20 74 6f 20 74 68 65 20 63 6f  l work to the co
353f2 6d 6d 6f 6e 20 63 61 73 65 20 77 69 74 68 20 74  mmon case with t
353f3 68 65 20 6f 6e 6c 79 20 62 65 6e 65 66 69 74 20  he only benefit 
353f4 62 65 69 6e 67 0a 2a 2a 20 41 50 49 20 65 6c 65  being.** API ele
353f5 67 61 6e 63 65 2e 20 20 50 75 6e 74 20 66 6f 72  gance.  Punt for
353f6 20 6e 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   now..*/.static 
353f7 76 6f 69 64 20 70 6c 77 54 65 72 6d 69 6e 61 74  void plwTerminat
353f8 65 28 50 4c 57 72 69 74 65 72 20 2a 70 57 72 69  e(PLWriter *pWri
353f9 74 65 72 29 7b 0a 20 20 69 66 28 20 70 57 72 69  ter){.  if( pWri
353fa 74 65 72 2d 3e 64 6c 77 2d 3e 69 54 79 70 65 3e  ter->dlw->iType>
353fb 44 4c 5f 44 4f 43 49 44 53 20 29 7b 0a 20 20 20  DL_DOCIDS ){.   
353fc 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d   char c[VARINT_M
353fd 41 58 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d  AX];.    int n =
353fe 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
353ff 2c 20 50 4f 53 5f 45 4e 44 29 3b 0a 20 20 20 20  , POS_END);.    
35400 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
35401 28 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 62  (pWriter->dlw->b
35402 2c 20 63 2c 20 6e 29 3b 0a 20 20 7d 0a 23 69 66  , c, n);.  }.#if
35403 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a  ndef NDEBUG.  /*
35404 20 4d 61 72 6b 20 61 73 20 74 65 72 6d 69 6e 61   Mark as termina
35405 74 65 64 20 66 6f 72 20 61 73 73 65 72 74 20 69  ted for assert i
35406 6e 20 70 6c 77 41 64 64 28 29 2e 20 2a 2f 0a 20  n plwAdd(). */. 
35407 20 70 57 72 69 74 65 72 2d 3e 69 50 6f 73 20 3d   pWriter->iPos =
35408 20 2d 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 73 74   -1;.#endif.}.st
35409 61 74 69 63 20 76 6f 69 64 20 70 6c 77 44 65 73  atic void plwDes
3540a 74 72 6f 79 28 50 4c 57 72 69 74 65 72 20 2a 70  troy(PLWriter *p
3540b 57 72 69 74 65 72 29 7b 0a 20 20 53 43 52 41 4d  Writer){.  SCRAM
3540c 42 4c 45 28 70 57 72 69 74 65 72 29 3b 0a 7d 0a  BLE(pWriter);.}.
3540d 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
3540e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3540f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
35411 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 4c 43 6f 6c 6c  *****/./* DLColl
35412 65 63 74 6f 72 20 77 72 61 70 73 20 50 4c 57 72  ector wraps PLWr
35413 69 74 65 72 20 61 6e 64 20 44 4c 57 72 69 74 65  iter and DLWrite
35414 72 20 74 6f 20 70 72 6f 76 69 64 65 20 61 0a 2a  r to provide a.*
35415 2a 20 64 79 6e 61 6d 69 63 61 6c 6c 79 2d 61 6c  * dynamically-al
35416 6c 6f 63 61 74 65 64 20 64 6f 63 6c 69 73 74 20  located doclist 
35417 61 72 65 61 20 74 6f 20 75 73 65 20 64 75 72 69  area to use duri
35418 6e 67 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 2e  ng tokenization.
35419 0a 2a 2a 0a 2a 2a 20 64 6c 63 4e 65 77 20 2d 20  .**.** dlcNew - 
3541a 6d 61 6c 6c 6f 63 20 75 70 20 61 6e 64 20 69 6e  malloc up and in
3541b 69 74 69 61 6c 69 7a 65 20 61 20 63 6f 6c 6c 65  itialize a colle
3541c 63 74 6f 72 2e 0a 2a 2a 20 64 6c 63 44 65 6c 65  ctor..** dlcDele
3541d 74 65 20 2d 20 64 65 73 74 72 6f 79 20 61 20 63  te - destroy a c
3541e 6f 6c 6c 65 63 74 6f 72 20 61 6e 64 20 61 6c 6c  ollector and all
3541f 20 63 6f 6e 74 61 69 6e 65 64 20 69 74 65 6d 73   contained items
35420 2e 0a 2a 2a 20 64 6c 63 41 64 64 50 6f 73 20 2d  ..** dlcAddPos -
35421 20 61 70 70 65 6e 64 20 70 6f 73 69 74 69 6f 6e   append position
35422 20 61 6e 64 20 6f 66 66 73 65 74 20 69 6e 66 6f   and offset info
35423 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 20 64 6c 63 41  rmation..** dlcA
35424 64 64 44 6f 63 6c 69 73 74 20 2d 20 61 64 64 20  ddDoclist - add 
35425 74 68 65 20 63 6f 6c 6c 65 63 74 65 64 20 64 6f  the collected do
35426 63 6c 69 73 74 20 74 6f 20 74 68 65 20 67 69 76  clist to the giv
35427 65 6e 20 62 75 66 66 65 72 2e 0a 2a 2a 20 64 6c  en buffer..** dl
35428 63 4e 65 78 74 20 2d 20 74 65 72 6d 69 6e 61 74  cNext - terminat
35429 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 64 6f  e the current do
3542a 63 75 6d 65 6e 74 20 61 6e 64 20 6f 70 65 6e 20  cument and open 
3542b 61 6e 6f 74 68 65 72 2e 0a 2a 2f 0a 74 79 70 65  another..*/.type
3542c 64 65 66 20 73 74 72 75 63 74 20 44 4c 43 6f 6c  def struct DLCol
3542d 6c 65 63 74 6f 72 20 7b 0a 20 20 44 61 74 61 42  lector {.  DataB
3542e 75 66 66 65 72 20 62 3b 0a 20 20 44 4c 57 72 69  uffer b;.  DLWri
3542f 74 65 72 20 64 6c 77 3b 0a 20 20 50 4c 57 72 69  ter dlw;.  PLWri
35430 74 65 72 20 70 6c 77 3b 0a 7d 20 44 4c 43 6f 6c  ter plw;.} DLCol
35431 6c 65 63 74 6f 72 3b 0a 0a 2f 2a 20 54 4f 44 4f  lector;../* TODO
35432 28 73 68 65 73 73 29 20 54 68 69 73 20 63 6f 75  (shess) This cou
35433 6c 64 20 61 6c 73 6f 20 62 65 20 64 6f 6e 65 20  ld also be done 
35434 62 79 20 63 61 6c 6c 69 6e 67 20 70 6c 77 54 65  by calling plwTe
35435 72 6d 69 6e 61 74 65 28 29 20 61 6e 64 0a 2a 2a  rminate() and.**
35436 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
35437 64 28 29 2e 20 20 49 20 74 72 69 65 64 20 74 68  d().  I tried th
35438 61 74 2c 20 65 78 70 65 63 74 69 6e 67 20 6e 6f  at, expecting no
35439 6d 69 6e 61 6c 20 70 65 72 66 6f 72 6d 61 6e 63  minal performanc
3543a 65 0a 2a 2a 20 64 69 66 66 65 72 65 6e 63 65 73  e.** differences
3543b 2c 20 62 75 74 20 69 74 20 73 65 65 6d 65 64 20  , but it seemed 
3543c 74 6f 20 70 72 65 74 74 79 20 72 65 6c 69 61 62  to pretty reliab
3543d 6c 79 20 62 65 20 77 6f 72 74 68 20 31 25 20 74  ly be worth 1% t
3543e 6f 20 63 6f 64 65 0a 2a 2a 20 69 74 20 74 68 69  o code.** it thi
3543f 73 20 77 61 79 2e 20 20 49 20 73 75 73 70 65 63  s way.  I suspec
35440 74 20 69 74 20 69 73 20 74 68 65 20 69 6e 63 72  t it is the incr
35441 65 6d 65 6e 74 61 6c 20 6d 61 6c 6c 6f 63 20 6f  emental malloc o
35442 76 65 72 68 65 61 64 20 28 73 6f 6d 65 0a 2a 2a  verhead (some.**
35443 20 70 65 72 63 65 6e 74 61 67 65 20 6f 66 20 74   percentage of t
35444 68 65 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28  he plwTerminate(
35445 29 20 63 61 6c 6c 73 20 77 69 6c 6c 20 63 61 75  ) calls will cau
35446 73 65 20 61 20 72 65 61 6c 6c 6f 63 29 2c 20 73  se a realloc), s
35447 6f 0a 2a 2a 20 74 68 69 73 20 6d 69 67 68 74 20  o.** this might 
35448 62 65 20 77 6f 72 74 68 20 72 65 76 69 73 69 74  be worth revisit
35449 69 6e 67 20 69 66 20 74 68 65 20 44 61 74 61 42  ing if the DataB
3544a 75 66 66 65 72 20 69 6d 70 6c 65 6d 65 6e 74 61  uffer implementa
3544b 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65 73 2e  tion.** changes.
3544c 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3544d 64 6c 63 41 64 64 44 6f 63 6c 69 73 74 28 44 4c  dlcAddDoclist(DL
3544e 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c  Collector *pColl
3544f 65 63 74 6f 72 2c 20 44 61 74 61 42 75 66 66 65  ector, DataBuffe
35450 72 20 2a 62 29 7b 0a 20 20 69 66 28 20 70 43 6f  r *b){.  if( pCo
35451 6c 6c 65 63 74 6f 72 2d 3e 64 6c 77 2e 69 54 79  llector->dlw.iTy
35452 70 65 3e 44 4c 5f 44 4f 43 49 44 53 20 29 7b 0a  pe>DL_DOCIDS ){.
35453 20 20 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e      char c[VARIN
35454 54 5f 4d 41 58 5d 3b 0a 20 20 20 20 69 6e 74 20  T_MAX];.    int 
35455 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e  n = fts3PutVarin
35456 74 28 63 2c 20 50 4f 53 5f 45 4e 44 29 3b 0a 20  t(c, POS_END);. 
35457 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70     dataBufferApp
35458 65 6e 64 32 28 62 2c 20 70 43 6f 6c 6c 65 63 74  end2(b, pCollect
35459 6f 72 2d 3e 62 2e 70 44 61 74 61 2c 20 70 43 6f  or->b.pData, pCo
3545a 6c 6c 65 63 74 6f 72 2d 3e 62 2e 6e 44 61 74 61  llector->b.nData
3545b 2c 20 63 2c 20 6e 29 3b 0a 20 20 7d 65 6c 73 65  , c, n);.  }else
3545c 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  {.    dataBuffer
3545d 41 70 70 65 6e 64 28 62 2c 20 70 43 6f 6c 6c 65  Append(b, pColle
3545e 63 74 6f 72 2d 3e 62 2e 70 44 61 74 61 2c 20 70  ctor->b.pData, p
3545f 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62 2e 6e 44 61  Collector->b.nDa
35460 74 61 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69  ta);.  }.}.stati
35461 63 20 76 6f 69 64 20 64 6c 63 4e 65 78 74 28 44  c void dlcNext(D
35462 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c  LCollector *pCol
35463 6c 65 63 74 6f 72 2c 20 73 71 6c 69 74 65 5f 69  lector, sqlite_i
35464 6e 74 36 34 20 69 44 6f 63 69 64 29 7b 0a 20 20  nt64 iDocid){.  
35465 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 26 70 43  plwTerminate(&pC
35466 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a  ollector->plw);.
35467 20 20 70 6c 77 44 65 73 74 72 6f 79 28 26 70 43    plwDestroy(&pC
35468 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a  ollector->plw);.
35469 20 20 70 6c 77 49 6e 69 74 28 26 70 43 6f 6c 6c    plwInit(&pColl
3546a 65 63 74 6f 72 2d 3e 70 6c 77 2c 20 26 70 43 6f  ector->plw, &pCo
3546b 6c 6c 65 63 74 6f 72 2d 3e 64 6c 77 2c 20 69 44  llector->dlw, iD
3546c 6f 63 69 64 29 3b 0a 7d 0a 73 74 61 74 69 63 20  ocid);.}.static 
3546d 76 6f 69 64 20 64 6c 63 41 64 64 50 6f 73 28 44  void dlcAddPos(D
3546e 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c  LCollector *pCol
3546f 6c 65 63 74 6f 72 2c 20 69 6e 74 20 69 43 6f 6c  lector, int iCol
35470 75 6d 6e 2c 20 69 6e 74 20 69 50 6f 73 2c 0a 20  umn, int iPos,. 
35471 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35472 20 20 20 20 20 69 6e 74 20 69 53 74 61 72 74 4f       int iStartO
35473 66 66 73 65 74 2c 20 69 6e 74 20 69 45 6e 64 4f  ffset, int iEndO
35474 66 66 73 65 74 29 7b 0a 20 20 70 6c 77 41 64 64  ffset){.  plwAdd
35475 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c  (&pCollector->pl
35476 77 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69 50 6f 73  w, iColumn, iPos
35477 2c 20 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20  , iStartOffset, 
35478 69 45 6e 64 4f 66 66 73 65 74 29 3b 0a 7d 0a 0a  iEndOffset);.}..
35479 73 74 61 74 69 63 20 44 4c 43 6f 6c 6c 65 63 74  static DLCollect
3547a 6f 72 20 2a 64 6c 63 4e 65 77 28 73 71 6c 69 74  or *dlcNew(sqlit
3547b 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 2c 20  e_int64 iDocid, 
3547c 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79 70  DocListType iTyp
3547d 65 29 7b 0a 20 20 44 4c 43 6f 6c 6c 65 63 74 6f  e){.  DLCollecto
3547e 72 20 2a 70 43 6f 6c 6c 65 63 74 6f 72 20 3d 20  r *pCollector = 
3547f 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
35480 69 7a 65 6f 66 28 44 4c 43 6f 6c 6c 65 63 74 6f  izeof(DLCollecto
35481 72 29 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  r));.  dataBuffe
35482 72 49 6e 69 74 28 26 70 43 6f 6c 6c 65 63 74 6f  rInit(&pCollecto
35483 72 2d 3e 62 2c 20 30 29 3b 0a 20 20 64 6c 77 49  r->b, 0);.  dlwI
35484 6e 69 74 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d  nit(&pCollector-
35485 3e 64 6c 77 2c 20 69 54 79 70 65 2c 20 26 70 43  >dlw, iType, &pC
35486 6f 6c 6c 65 63 74 6f 72 2d 3e 62 29 3b 0a 20 20  ollector->b);.  
35487 70 6c 77 49 6e 69 74 28 26 70 43 6f 6c 6c 65 63  plwInit(&pCollec
35488 74 6f 72 2d 3e 70 6c 77 2c 20 26 70 43 6f 6c 6c  tor->plw, &pColl
35489 65 63 74 6f 72 2d 3e 64 6c 77 2c 20 69 44 6f 63  ector->dlw, iDoc
3548a 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 43  id);.  return pC
3548b 6f 6c 6c 65 63 74 6f 72 3b 0a 7d 0a 73 74 61 74  ollector;.}.stat
3548c 69 63 20 76 6f 69 64 20 64 6c 63 44 65 6c 65 74  ic void dlcDelet
3548d 65 28 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70  e(DLCollector *p
3548e 43 6f 6c 6c 65 63 74 6f 72 29 7b 0a 20 20 70 6c  Collector){.  pl
3548f 77 44 65 73 74 72 6f 79 28 26 70 43 6f 6c 6c 65  wDestroy(&pColle
35490 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a 20 20 64 6c  ctor->plw);.  dl
35491 77 44 65 73 74 72 6f 79 28 26 70 43 6f 6c 6c 65  wDestroy(&pColle
35492 63 74 6f 72 2d 3e 64 6c 77 29 3b 0a 20 20 64 61  ctor->dlw);.  da
35493 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
35494 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62 29 3b  &pCollector->b);
35495 0a 20 20 53 43 52 41 4d 42 4c 45 28 70 43 6f 6c  .  SCRAMBLE(pCol
35496 6c 65 63 74 6f 72 29 3b 0a 20 20 73 71 6c 69 74  lector);.  sqlit
35497 65 33 5f 66 72 65 65 28 70 43 6f 6c 6c 65 63 74  e3_free(pCollect
35498 6f 72 29 3b 0a 7d 0a 0a 0a 2f 2a 20 43 6f 70 79  or);.}.../* Copy
35499 20 74 68 65 20 64 6f 63 6c 69 73 74 20 64 61 74   the doclist dat
3549a 61 20 6f 66 20 69 54 79 70 65 20 69 6e 20 70 44  a of iType in pD
3549b 61 74 61 2f 6e 44 61 74 61 20 69 6e 74 6f 20 2a  ata/nData into *
3549c 6f 75 74 2c 20 74 72 69 6d 6d 69 6e 67 0a 2a 2a  out, trimming.**
3549d 20 75 6e 6e 65 63 65 73 73 61 72 79 20 64 61 74   unnecessary dat
3549e 61 20 61 73 20 77 65 20 67 6f 2e 20 20 4f 6e 6c  a as we go.  Onl
3549f 79 20 63 6f 6c 75 6d 6e 73 20 6d 61 74 63 68 69  y columns matchi
354a0 6e 67 20 69 43 6f 6c 75 6d 6e 20 61 72 65 0a 2a  ng iColumn are.*
354a1 2a 20 63 6f 70 69 65 64 2c 20 61 6c 6c 20 63 6f  * copied, all co
354a2 6c 75 6d 6e 73 20 63 6f 70 69 65 64 20 69 66 20  lumns copied if 
354a3 69 43 6f 6c 75 6d 6e 20 69 73 20 2d 31 2e 20 20  iColumn is -1.  
354a4 45 6c 65 6d 65 6e 74 73 20 77 69 74 68 20 6e 6f  Elements with no
354a5 0a 2a 2a 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c  .** matching col
354a6 75 6d 6e 73 20 61 72 65 20 64 72 6f 70 70 65 64  umns are dropped
354a7 2e 20 20 54 68 65 20 6f 75 74 70 75 74 20 69 73  .  The output is
354a8 20 61 6e 20 69 4f 75 74 54 79 70 65 20 64 6f 63   an iOutType doc
354a9 6c 69 73 74 2e 0a 2a 2f 0a 2f 2a 20 4e 4f 54 45  list..*/./* NOTE
354aa 28 73 68 65 73 73 29 20 54 68 69 73 20 63 6f 64  (shess) This cod
354ab 65 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20  e is only valid 
354ac 61 66 74 65 72 20 61 6c 6c 20 64 6f 63 6c 69 73  after all doclis
354ad 74 73 20 61 72 65 20 6d 65 72 67 65 64 2e 0a 2a  ts are merged..*
354ae 2a 20 49 66 20 74 68 69 73 20 69 73 20 72 75 6e  * If this is run
354af 20 62 65 66 6f 72 65 20 6d 65 72 67 65 73 2c 20   before merges, 
354b0 74 68 65 6e 20 64 6f 63 6c 69 73 74 20 69 74 65  then doclist ite
354b1 6d 73 20 77 68 69 63 68 20 72 65 70 72 65 73 65  ms which represe
354b2 6e 74 0a 2a 2a 20 64 65 6c 65 74 69 6f 6e 20 77  nt.** deletion w
354b3 69 6c 6c 20 62 65 20 74 72 69 6d 6d 65 64 2c 20  ill be trimmed, 
354b4 61 6e 64 20 77 69 6c 6c 20 74 68 75 73 20 6e 6f  and will thus no
354b5 74 20 65 66 66 65 63 74 20 61 20 64 65 6c 65 74  t effect a delet
354b6 69 6f 6e 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68  ion.** during th
354b7 65 20 6d 65 72 67 65 2e 0a 2a 2f 0a 73 74 61 74  e merge..*/.stat
354b8 69 63 20 76 6f 69 64 20 64 6f 63 4c 69 73 74 54  ic void docListT
354b9 72 69 6d 28 44 6f 63 4c 69 73 74 54 79 70 65 20  rim(DocListType 
354ba 69 54 79 70 65 2c 20 63 6f 6e 73 74 20 63 68 61  iType, const cha
354bb 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44  r *pData, int nD
354bc 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
354bd 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
354be 20 69 43 6f 6c 75 6d 6e 2c 20 44 6f 63 4c 69 73   iColumn, DocLis
354bf 74 54 79 70 65 20 69 4f 75 74 54 79 70 65 2c 20  tType iOutType, 
354c0 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75 74 29  DataBuffer *out)
354c1 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 64 6c 52  {.  DLReader dlR
354c2 65 61 64 65 72 3b 0a 20 20 44 4c 57 72 69 74 65  eader;.  DLWrite
354c3 72 20 64 6c 57 72 69 74 65 72 3b 0a 0a 20 20 61  r dlWriter;..  a
354c4 73 73 65 72 74 28 20 69 4f 75 74 54 79 70 65 3c  ssert( iOutType<
354c5 3d 69 54 79 70 65 20 29 3b 0a 0a 20 20 64 6c 72  =iType );..  dlr
354c6 49 6e 69 74 28 26 64 6c 52 65 61 64 65 72 2c 20  Init(&dlReader, 
354c7 69 54 79 70 65 2c 20 70 44 61 74 61 2c 20 6e 44  iType, pData, nD
354c8 61 74 61 29 3b 0a 20 20 64 6c 77 49 6e 69 74 28  ata);.  dlwInit(
354c9 26 64 6c 57 72 69 74 65 72 2c 20 69 4f 75 74 54  &dlWriter, iOutT
354ca 79 70 65 2c 20 6f 75 74 29 3b 0a 0a 20 20 77 68  ype, out);..  wh
354cb 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28 26  ile( !dlrAtEnd(&
354cc 64 6c 52 65 61 64 65 72 29 20 29 7b 0a 20 20 20  dlReader) ){.   
354cd 20 50 4c 52 65 61 64 65 72 20 70 6c 52 65 61 64   PLReader plRead
354ce 65 72 3b 0a 20 20 20 20 50 4c 57 72 69 74 65 72  er;.    PLWriter
354cf 20 70 6c 57 72 69 74 65 72 3b 0a 20 20 20 20 69   plWriter;.    i
354d0 6e 74 20 6d 61 74 63 68 20 3d 20 30 3b 0a 0a 20  nt match = 0;.. 
354d1 20 20 20 70 6c 72 49 6e 69 74 28 26 70 6c 52 65     plrInit(&plRe
354d2 61 64 65 72 2c 20 26 64 6c 52 65 61 64 65 72 29  ader, &dlReader)
354d3 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 21 70  ;..    while( !p
354d4 6c 72 41 74 45 6e 64 28 26 70 6c 52 65 61 64 65  lrAtEnd(&plReade
354d5 72 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  r) ){.      if( 
354d6 69 43 6f 6c 75 6d 6e 3d 3d 2d 31 20 7c 7c 20 70  iColumn==-1 || p
354d7 6c 72 43 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64  lrColumn(&plRead
354d8 65 72 29 3d 3d 69 43 6f 6c 75 6d 6e 20 29 7b 0a  er)==iColumn ){.
354d9 20 20 20 20 20 20 20 20 69 66 28 20 21 6d 61 74          if( !mat
354da 63 68 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ch ){.          
354db 70 6c 77 49 6e 69 74 28 26 70 6c 57 72 69 74 65  plwInit(&plWrite
354dc 72 2c 20 26 64 6c 57 72 69 74 65 72 2c 20 64 6c  r, &dlWriter, dl
354dd 72 44 6f 63 69 64 28 26 64 6c 52 65 61 64 65 72  rDocid(&dlReader
354de 29 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6d 61  ));.          ma
354df 74 63 68 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  tch = 1;.       
354e0 20 7d 0a 20 20 20 20 20 20 20 20 70 6c 77 41 64   }.        plwAd
354e1 64 28 26 70 6c 57 72 69 74 65 72 2c 20 70 6c 72  d(&plWriter, plr
354e2 43 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65 72  Column(&plReader
354e3 29 2c 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26  ), plrPosition(&
354e4 70 6c 52 65 61 64 65 72 29 2c 0a 20 20 20 20 20  plReader),.     
354e5 20 20 20 20 20 20 20 20 20 20 70 6c 72 53 74 61            plrSta
354e6 72 74 4f 66 66 73 65 74 28 26 70 6c 52 65 61 64  rtOffset(&plRead
354e7 65 72 29 2c 20 70 6c 72 45 6e 64 4f 66 66 73 65  er), plrEndOffse
354e8 74 28 26 70 6c 52 65 61 64 65 72 29 29 3b 0a 20  t(&plReader));. 
354e9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 6c 72       }.      plr
354ea 53 74 65 70 28 26 70 6c 52 65 61 64 65 72 29 3b  Step(&plReader);
354eb 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d  .    }.    if( m
354ec 61 74 63 68 20 29 7b 0a 20 20 20 20 20 20 70 6c  atch ){.      pl
354ed 77 54 65 72 6d 69 6e 61 74 65 28 26 70 6c 57 72  wTerminate(&plWr
354ee 69 74 65 72 29 3b 0a 20 20 20 20 20 20 70 6c 77  iter);.      plw
354ef 44 65 73 74 72 6f 79 28 26 70 6c 57 72 69 74 65  Destroy(&plWrite
354f0 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70  r);.    }..    p
354f1 6c 72 44 65 73 74 72 6f 79 28 26 70 6c 52 65 61  lrDestroy(&plRea
354f2 64 65 72 29 3b 0a 20 20 20 20 64 6c 72 53 74 65  der);.    dlrSte
354f3 70 28 26 64 6c 52 65 61 64 65 72 29 3b 0a 20 20  p(&dlReader);.  
354f4 7d 0a 20 20 64 6c 77 44 65 73 74 72 6f 79 28 26  }.  dlwDestroy(&
354f5 64 6c 57 72 69 74 65 72 29 3b 0a 20 20 64 6c 72  dlWriter);.  dlr
354f6 44 65 73 74 72 6f 79 28 26 64 6c 52 65 61 64 65  Destroy(&dlReade
354f7 72 29 3b 0a 7d 0a 0a 2f 2a 20 55 73 65 64 20 62  r);.}../* Used b
354f8 79 20 64 6f 63 4c 69 73 74 4d 65 72 67 65 28 29  y docListMerge()
354f9 20 74 6f 20 6b 65 65 70 20 64 6f 63 6c 69 73 74   to keep doclist
354fa 73 20 69 6e 20 74 68 65 20 61 73 63 65 6e 64 69  s in the ascendi
354fb 6e 67 20 6f 72 64 65 72 20 62 79 0a 2a 2a 20 64  ng order by.** d
354fc 6f 63 69 64 2c 20 74 68 65 6e 20 61 73 63 65 6e  ocid, then ascen
354fd 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 61 67  ding order by ag
354fe 65 20 28 73 6f 20 74 68 65 20 6e 65 77 65 73 74  e (so the newest
354ff 20 63 6f 6d 65 73 20 66 69 72 73 74 29 2e 0a 2a   comes first)..*
35500 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  /.typedef struct
35501 20 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72   OrderedDLReader
35502 20 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 2a 70   {.  DLReader *p
35503 52 65 61 64 65 72 3b 0a 0a 20 20 2f 2a 20 54 4f  Reader;..  /* TO
35504 44 4f 28 73 68 65 73 73 29 20 49 66 20 77 65 20  DO(shess) If we 
35505 61 73 73 75 6d 65 20 74 68 61 74 20 64 6f 63 4c  assume that docL
35506 69 73 74 4d 65 72 67 65 20 70 52 65 61 64 65 72  istMerge pReader
35507 73 20 69 73 20 6f 72 64 65 72 65 64 20 62 79 0a  s is ordered by.
35508 20 20 2a 2a 20 61 67 65 20 28 77 68 69 63 68 20    ** age (which 
35509 77 65 20 64 6f 29 2c 20 74 68 65 6e 20 77 65 20  we do), then we 
3550a 63 6f 75 6c 64 20 75 73 65 20 70 52 65 61 64 65  could use pReade
3550b 72 20 63 6f 6d 70 61 72 69 73 6f 6e 73 20 74 6f  r comparisons to
3550c 20 62 72 65 61 6b 0a 20 20 2a 2a 20 74 69 65 73   break.  ** ties
3550d 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78  ..  */.  int idx
3550e 3b 0a 7d 20 4f 72 64 65 72 65 64 44 4c 52 65 61  ;.} OrderedDLRea
3550f 64 65 72 3b 0a 0a 2f 2a 20 4f 72 64 65 72 20 65  der;../* Order e
35510 6f 66 20 74 6f 20 65 6e 64 2c 20 74 68 65 6e 20  of to end, then 
35511 62 79 20 64 6f 63 69 64 20 61 73 63 2c 20 69 64  by docid asc, id
35512 78 20 64 65 73 63 2e 20 2a 2f 0a 73 74 61 74 69  x desc. */.stati
35513 63 20 69 6e 74 20 6f 72 64 65 72 65 64 44 4c 52  c int orderedDLR
35514 65 61 64 65 72 43 6d 70 28 4f 72 64 65 72 65 64  eaderCmp(Ordered
35515 44 4c 52 65 61 64 65 72 20 2a 72 31 2c 20 4f 72  DLReader *r1, Or
35516 64 65 72 65 64 44 4c 52 65 61 64 65 72 20 2a 72  deredDLReader *r
35517 32 29 7b 0a 20 20 69 66 28 20 64 6c 72 41 74 45  2){.  if( dlrAtE
35518 6e 64 28 72 31 2d 3e 70 52 65 61 64 65 72 29 20  nd(r1->pReader) 
35519 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72 41 74  ){.    if( dlrAt
3551a 45 6e 64 28 72 32 2d 3e 70 52 65 61 64 65 72 29  End(r2->pReader)
3551b 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a   ) return 0;  /*
3551c 20 42 6f 74 68 20 61 74 45 6e 64 28 29 2e 20 2a   Both atEnd(). *
3551d 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 20  /.    return 1; 
3551e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3551f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
35520 4f 6e 6c 79 20 72 31 20 61 74 45 6e 64 28 29 2e  Only r1 atEnd().
35521 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 64 6c   */.  }.  if( dl
35522 72 41 74 45 6e 64 28 72 32 2d 3e 70 52 65 61 64  rAtEnd(r2->pRead
35523 65 72 29 20 29 20 72 65 74 75 72 6e 20 2d 31 3b  er) ) return -1;
35524 20 20 20 2f 2a 20 4f 6e 6c 79 20 72 32 20 61 74     /* Only r2 at
35525 45 6e 64 28 29 2e 20 2a 2f 0a 0a 20 20 69 66 28  End(). */..  if(
35526 20 64 6c 72 44 6f 63 69 64 28 72 31 2d 3e 70 52   dlrDocid(r1->pR
35527 65 61 64 65 72 29 3c 64 6c 72 44 6f 63 69 64 28  eader)<dlrDocid(
35528 72 32 2d 3e 70 52 65 61 64 65 72 29 20 29 20 72  r2->pReader) ) r
35529 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66 28 20  eturn -1;.  if( 
3552a 64 6c 72 44 6f 63 69 64 28 72 31 2d 3e 70 52 65  dlrDocid(r1->pRe
3552b 61 64 65 72 29 3e 64 6c 72 44 6f 63 69 64 28 72  ader)>dlrDocid(r
3552c 32 2d 3e 70 52 65 61 64 65 72 29 20 29 20 72 65  2->pReader) ) re
3552d 74 75 72 6e 20 31 3b 0a 0a 20 20 2f 2a 20 44 65  turn 1;..  /* De
3552e 73 63 65 6e 64 69 6e 67 20 6f 6e 20 69 64 78 2e  scending on idx.
3552f 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 32 2d   */.  return r2-
35530 3e 69 64 78 2d 72 31 2d 3e 69 64 78 3b 0a 7d 0a  >idx-r1->idx;.}.
35531 0a 2f 2a 20 42 75 62 62 6c 65 20 70 5b 30 5d 20  ./* Bubble p[0] 
35532 74 6f 20 61 70 70 72 6f 70 72 69 61 74 65 20 70  to appropriate p
35533 6c 61 63 65 20 69 6e 20 70 5b 31 2e 2e 6e 2d 31  lace in p[1..n-1
35534 5d 2e 20 20 41 73 73 75 6d 65 73 20 74 68 61 74  ].  Assumes that
35535 0a 2a 2a 20 70 5b 31 2e 2e 6e 2d 31 5d 20 69 73  .** p[1..n-1] is
35536 20 61 6c 72 65 61 64 79 20 73 6f 72 74 65 64 2e   already sorted.
35537 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .*/./* TODO(shes
35538 73 29 20 49 73 20 74 68 69 73 20 66 72 65 71 75  s) Is this frequ
35539 65 6e 74 20 65 6e 6f 75 67 68 20 74 6f 20 77 61  ent enough to wa
3553a 72 72 61 6e 74 20 61 20 62 69 6e 61 72 79 20 73  rrant a binary s
3553b 65 61 72 63 68 3f 0a 2a 2a 20 42 65 66 6f 72 65  earch?.** Before
3553c 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68   implementing th
3553d 61 74 2c 20 69 6e 73 74 72 75 6d 65 6e 74 20 74  at, instrument t
3553e 68 65 20 63 6f 64 65 20 74 6f 20 63 68 65 63 6b  he code to check
3553f 2e 20 20 49 6e 20 6d 6f 73 74 0a 2a 2a 20 63 75  .  In most.** cu
35540 72 72 65 6e 74 20 75 73 61 67 65 2c 20 49 20 65  rrent usage, I e
35541 78 70 65 63 74 20 74 68 61 74 20 70 5b 30 5d 20  xpect that p[0] 
35542 77 69 6c 6c 20 62 65 20 6c 65 73 73 20 74 68 61  will be less tha
35543 6e 20 70 5b 31 5d 20 61 20 76 65 72 79 0a 2a 2a  n p[1] a very.**
35544 20 68 69 67 68 20 70 72 6f 70 6f 72 74 69 6f 6e   high proportion
35545 20 6f 66 20 74 68 65 20 74 69 6d 65 2e 0a 2a 2f   of the time..*/
35546 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 72 64  .static void ord
35547 65 72 65 64 44 4c 52 65 61 64 65 72 52 65 6f 72  eredDLReaderReor
35548 64 65 72 28 4f 72 64 65 72 65 64 44 4c 52 65 61  der(OrderedDLRea
35549 64 65 72 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a  der *p, int n){.
3554a 20 20 77 68 69 6c 65 28 20 6e 3e 31 20 26 26 20    while( n>1 && 
3554b 6f 72 64 65 72 65 64 44 4c 52 65 61 64 65 72 43  orderedDLReaderC
3554c 6d 70 28 70 2c 20 70 2b 31 29 3e 30 20 29 7b 0a  mp(p, p+1)>0 ){.
3554d 20 20 20 20 4f 72 64 65 72 65 64 44 4c 52 65 61      OrderedDLRea
3554e 64 65 72 20 74 6d 70 20 3d 20 70 5b 30 5d 3b 0a  der tmp = p[0];.
3554f 20 20 20 20 70 5b 30 5d 20 3d 20 70 5b 31 5d 3b      p[0] = p[1];
35550 0a 20 20 20 20 70 5b 31 5d 20 3d 20 74 6d 70 3b  .    p[1] = tmp;
35551 0a 20 20 20 20 6e 2d 2d 3b 0a 20 20 20 20 70 2b  .    n--;.    p+
35552 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 47 69 76  +;.  }.}../* Giv
35553 65 6e 20 61 6e 20 61 72 72 61 79 20 6f 66 20 64  en an array of d
35554 6f 63 6c 69 73 74 20 72 65 61 64 65 72 73 2c 20  oclist readers, 
35555 6d 65 72 67 65 20 74 68 65 69 72 20 64 6f 63 6c  merge their docl
35556 69 73 74 20 65 6c 65 6d 65 6e 74 73 0a 2a 2a 20  ist elements.** 
35557 69 6e 74 6f 20 6f 75 74 20 69 6e 20 73 6f 72 74  into out in sort
35558 65 64 20 6f 72 64 65 72 20 28 62 79 20 64 6f 63  ed order (by doc
35559 69 64 29 2c 20 64 72 6f 70 70 69 6e 67 20 65 6c  id), dropping el
3555a 65 6d 65 6e 74 73 20 66 72 6f 6d 20 6f 6c 64 65  ements from olde
3555b 72 0a 2a 2a 20 72 65 61 64 65 72 73 20 77 68 65  r.** readers whe
3555c 6e 20 74 68 65 72 65 20 69 73 20 61 20 64 75 70  n there is a dup
3555d 6c 69 63 61 74 65 20 64 6f 63 69 64 2e 20 20 70  licate docid.  p
3555e 52 65 61 64 65 72 73 20 69 73 20 61 73 73 75 6d  Readers is assum
3555f 65 64 20 74 6f 20 62 65 0a 2a 2a 20 6f 72 64 65  ed to be.** orde
35560 72 65 64 20 62 79 20 61 67 65 2c 20 6f 6c 64 65  red by age, olde
35561 73 74 20 66 69 72 73 74 2e 0a 2a 2f 0a 2f 2a 20  st first..*/./* 
35562 54 4f 44 4f 28 73 68 65 73 73 29 20 6e 52 65 61  TODO(shess) nRea
35563 64 65 72 73 20 6d 75 73 74 20 62 65 20 3c 3d 20  ders must be <= 
35564 4d 45 52 47 45 5f 43 4f 55 4e 54 2e 20 20 54 68  MERGE_COUNT.  Th
35565 69 73 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 62  is should probab
35566 6c 79 0a 2a 2a 20 62 65 20 66 69 78 65 64 2e 0a  ly.** be fixed..
35567 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  */.static void d
35568 6f 63 4c 69 73 74 4d 65 72 67 65 28 44 61 74 61  ocListMerge(Data
35569 42 75 66 66 65 72 20 2a 6f 75 74 2c 0a 20 20 20  Buffer *out,.   
3556a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3556b 20 20 20 20 20 20 44 4c 52 65 61 64 65 72 20 2a        DLReader *
3556c 70 52 65 61 64 65 72 73 2c 20 69 6e 74 20 6e 52  pReaders, int nR
3556d 65 61 64 65 72 73 29 7b 0a 20 20 4f 72 64 65 72  eaders){.  Order
3556e 65 64 44 4c 52 65 61 64 65 72 20 72 65 61 64 65  edDLReader reade
3556f 72 73 5b 4d 45 52 47 45 5f 43 4f 55 4e 54 5d 3b  rs[MERGE_COUNT];
35570 0a 20 20 44 4c 57 72 69 74 65 72 20 77 72 69 74  .  DLWriter writ
35571 65 72 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a  er;.  int i, n;.
35572 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 53    const char *pS
35573 74 61 72 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20  tart = 0;.  int 
35574 6e 53 74 61 72 74 20 3d 20 30 3b 0a 20 20 73 71  nStart = 0;.  sq
35575 6c 69 74 65 5f 69 6e 74 36 34 20 69 46 69 72 73  lite_int64 iFirs
35576 74 44 6f 63 69 64 20 3d 20 30 2c 20 69 4c 61 73  tDocid = 0, iLas
35577 74 44 6f 63 69 64 20 3d 20 30 3b 0a 0a 20 20 61  tDocid = 0;..  a
35578 73 73 65 72 74 28 20 6e 52 65 61 64 65 72 73 3e  ssert( nReaders>
35579 30 20 29 3b 0a 20 20 69 66 28 20 6e 52 65 61 64  0 );.  if( nRead
3557a 65 72 73 3d 3d 31 20 29 7b 0a 20 20 20 20 64 61  ers==1 ){.    da
3557b 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 6f  taBufferAppend(o
3557c 75 74 2c 20 64 6c 72 44 6f 63 44 61 74 61 28 70  ut, dlrDocData(p
3557d 52 65 61 64 65 72 73 29 2c 20 64 6c 72 41 6c 6c  Readers), dlrAll
3557e 44 61 74 61 42 79 74 65 73 28 70 52 65 61 64 65  DataBytes(pReade
3557f 72 73 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  rs));.    return
35580 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28  ;.  }..  assert(
35581 20 6e 52 65 61 64 65 72 73 3c 3d 4d 45 52 47 45   nReaders<=MERGE
35582 5f 43 4f 55 4e 54 20 29 3b 0a 20 20 6e 20 3d 20  _COUNT );.  n = 
35583 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  0;.  for(i=0; i<
35584 6e 52 65 61 64 65 72 73 3b 20 69 2b 2b 29 7b 0a  nReaders; i++){.
35585 20 20 20 20 61 73 73 65 72 74 28 20 70 52 65 61      assert( pRea
35586 64 65 72 73 5b 69 5d 2e 69 54 79 70 65 3d 3d 70  ders[i].iType==p
35587 52 65 61 64 65 72 73 5b 30 5d 2e 69 54 79 70 65  Readers[0].iType
35588 20 29 3b 0a 20 20 20 20 72 65 61 64 65 72 73 5b   );.    readers[
35589 69 5d 2e 70 52 65 61 64 65 72 20 3d 20 70 52 65  i].pReader = pRe
3558a 61 64 65 72 73 2b 69 3b 0a 20 20 20 20 72 65 61  aders+i;.    rea
3558b 64 65 72 73 5b 69 5d 2e 69 64 78 20 3d 20 69 3b  ders[i].idx = i;
3558c 0a 20 20 20 20 6e 20 2b 3d 20 64 6c 72 41 6c 6c  .    n += dlrAll
3558d 44 61 74 61 42 79 74 65 73 28 26 70 52 65 61 64  DataBytes(&pRead
3558e 65 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 2f  ers[i]);.  }.  /
3558f 2a 20 43 6f 6e 73 65 72 76 61 74 69 76 65 6c 79  * Conservatively
35590 20 73 69 7a 65 20 6f 75 74 70 75 74 20 74 6f 20   size output to 
35591 73 75 6d 20 6f 66 20 69 6e 70 75 74 73 2e 20 20  sum of inputs.  
35592 4f 75 74 70 75 74 20 73 68 6f 75 6c 64 20 65 6e  Output should en
35593 64 0a 20 20 2a 2a 20 75 70 20 73 74 72 69 63 74  d.  ** up strict
35594 6c 79 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20  ly smaller than 
35595 69 6e 70 75 74 2e 0a 20 20 2a 2f 0a 20 20 64 61  input..  */.  da
35596 74 61 42 75 66 66 65 72 45 78 70 61 6e 64 28 6f  taBufferExpand(o
35597 75 74 2c 20 6e 29 3b 0a 0a 20 20 2f 2a 20 47 65  ut, n);..  /* Ge
35598 74 20 74 68 65 20 72 65 61 64 65 72 73 20 69 6e  t the readers in
35599 74 6f 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e  to sorted order.
3559a 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69 2d 2d   */.  while( i--
3559b 3e 30 20 29 7b 0a 20 20 20 20 6f 72 64 65 72 65  >0 ){.    ordere
3559c 64 44 4c 52 65 61 64 65 72 52 65 6f 72 64 65 72  dDLReaderReorder
3559d 28 72 65 61 64 65 72 73 2b 69 2c 20 6e 52 65 61  (readers+i, nRea
3559e 64 65 72 73 2d 69 29 3b 0a 20 20 7d 0a 0a 20 20  ders-i);.  }..  
3559f 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c  dlwInit(&writer,
355a0 20 70 52 65 61 64 65 72 73 5b 30 5d 2e 69 54 79   pReaders[0].iTy
355a1 70 65 2c 20 6f 75 74 29 3b 0a 20 20 77 68 69 6c  pe, out);.  whil
355a2 65 28 20 21 64 6c 72 41 74 45 6e 64 28 72 65 61  e( !dlrAtEnd(rea
355a3 64 65 72 73 5b 30 5d 2e 70 52 65 61 64 65 72 29  ders[0].pReader)
355a4 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69   ){.    sqlite_i
355a5 6e 74 36 34 20 69 44 6f 63 69 64 20 3d 20 64 6c  nt64 iDocid = dl
355a6 72 44 6f 63 69 64 28 72 65 61 64 65 72 73 5b 30  rDocid(readers[0
355a7 5d 2e 70 52 65 61 64 65 72 29 3b 0a 0a 20 20 20  ].pReader);..   
355a8 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61   /* If this is a
355a9 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e 20 6f 66   continuation of
355aa 20 74 68 65 20 63 75 72 72 65 6e 74 20 62 75 66   the current buf
355ab 66 65 72 20 74 6f 20 63 6f 70 79 2c 20 65 78 74  fer to copy, ext
355ac 65 6e 64 0a 20 20 20 20 2a 2a 20 74 68 61 74 20  end.    ** that 
355ad 62 75 66 66 65 72 2e 20 20 6d 65 6d 63 70 79 28  buffer.  memcpy(
355ae 29 20 73 65 65 6d 73 20 74 6f 20 62 65 20 6d 6f  ) seems to be mo
355af 72 65 20 65 66 66 69 63 69 65 6e 74 20 69 66 20  re efficient if 
355b0 69 74 20 68 61 73 20 61 0a 20 20 20 20 2a 2a 20  it has a.    ** 
355b1 6c 6f 74 73 20 6f 66 20 64 61 74 61 20 74 6f 20  lots of data to 
355b2 63 6f 70 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  copy..    */.   
355b3 20 69 66 28 20 64 6c 72 44 6f 63 44 61 74 61 28   if( dlrDocData(
355b4 72 65 61 64 65 72 73 5b 30 5d 2e 70 52 65 61 64  readers[0].pRead
355b5 65 72 29 3d 3d 70 53 74 61 72 74 2b 6e 53 74 61  er)==pStart+nSta
355b6 72 74 20 29 7b 0a 20 20 20 20 20 20 6e 53 74 61  rt ){.      nSta
355b7 72 74 20 2b 3d 20 64 6c 72 44 6f 63 44 61 74 61  rt += dlrDocData
355b8 42 79 74 65 73 28 72 65 61 64 65 72 73 5b 30 5d  Bytes(readers[0]
355b9 2e 70 52 65 61 64 65 72 29 3b 0a 20 20 20 20 7d  .pReader);.    }
355ba 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20  else{.      if( 
355bb 70 53 74 61 72 74 21 3d 30 20 29 7b 0a 20 20 20  pStart!=0 ){.   
355bc 20 20 20 20 20 64 6c 77 41 70 70 65 6e 64 28 26       dlwAppend(&
355bd 77 72 69 74 65 72 2c 20 70 53 74 61 72 74 2c 20  writer, pStart, 
355be 6e 53 74 61 72 74 2c 20 69 46 69 72 73 74 44 6f  nStart, iFirstDo
355bf 63 69 64 2c 20 69 4c 61 73 74 44 6f 63 69 64 29  cid, iLastDocid)
355c0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
355c1 70 53 74 61 72 74 20 3d 20 64 6c 72 44 6f 63 44  pStart = dlrDocD
355c2 61 74 61 28 72 65 61 64 65 72 73 5b 30 5d 2e 70  ata(readers[0].p
355c3 52 65 61 64 65 72 29 3b 0a 20 20 20 20 20 20 6e  Reader);.      n
355c4 53 74 61 72 74 20 3d 20 64 6c 72 44 6f 63 44 61  Start = dlrDocDa
355c5 74 61 42 79 74 65 73 28 72 65 61 64 65 72 73 5b  taBytes(readers[
355c6 30 5d 2e 70 52 65 61 64 65 72 29 3b 0a 20 20 20  0].pReader);.   
355c7 20 20 20 69 46 69 72 73 74 44 6f 63 69 64 20 3d     iFirstDocid =
355c8 20 69 44 6f 63 69 64 3b 0a 20 20 20 20 7d 0a 20   iDocid;.    }. 
355c9 20 20 20 69 4c 61 73 74 44 6f 63 69 64 20 3d 20     iLastDocid = 
355ca 69 44 6f 63 69 64 3b 0a 20 20 20 20 64 6c 72 53  iDocid;.    dlrS
355cb 74 65 70 28 72 65 61 64 65 72 73 5b 30 5d 2e 70  tep(readers[0].p
355cc 52 65 61 64 65 72 29 3b 0a 0a 20 20 20 20 2f 2a  Reader);..    /*
355cd 20 44 72 6f 70 20 61 6c 6c 20 6f 66 20 74 68 65   Drop all of the
355ce 20 6f 6c 64 65 72 20 65 6c 65 6d 65 6e 74 73 20   older elements 
355cf 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 64 6f  with the same do
355d0 63 69 64 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28  cid. */.    for(
355d1 69 3d 31 3b 20 69 3c 6e 52 65 61 64 65 72 73 20  i=1; i<nReaders 
355d2 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  &&.             
355d3 21 64 6c 72 41 74 45 6e 64 28 72 65 61 64 65 72  !dlrAtEnd(reader
355d4 73 5b 69 5d 2e 70 52 65 61 64 65 72 29 20 26 26  s[i].pReader) &&
355d5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 64 6c  .             dl
355d6 72 44 6f 63 69 64 28 72 65 61 64 65 72 73 5b 69  rDocid(readers[i
355d7 5d 2e 70 52 65 61 64 65 72 29 3d 3d 69 44 6f 63  ].pReader)==iDoc
355d8 69 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  id; i++){.      
355d9 64 6c 72 53 74 65 70 28 72 65 61 64 65 72 73 5b  dlrStep(readers[
355da 69 5d 2e 70 52 65 61 64 65 72 29 3b 0a 20 20 20  i].pReader);.   
355db 20 7d 0a 0a 20 20 20 20 2f 2a 20 47 65 74 20 74   }..    /* Get t
355dc 68 65 20 72 65 61 64 65 72 73 20 62 61 63 6b 20  he readers back 
355dd 69 6e 74 6f 20 6f 72 64 65 72 2e 20 2a 2f 0a 20  into order. */. 
355de 20 20 20 77 68 69 6c 65 28 20 69 2d 2d 3e 30 20     while( i-->0 
355df 29 7b 0a 20 20 20 20 20 20 6f 72 64 65 72 65 64  ){.      ordered
355e0 44 4c 52 65 61 64 65 72 52 65 6f 72 64 65 72 28  DLReaderReorder(
355e1 72 65 61 64 65 72 73 2b 69 2c 20 6e 52 65 61 64  readers+i, nRead
355e2 65 72 73 2d 69 29 3b 0a 20 20 20 20 7d 0a 20 20  ers-i);.    }.  
355e3 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 6f 76 65  }..  /* Copy ove
355e4 72 20 61 6e 79 20 72 65 6d 61 69 6e 69 6e 67 20  r any remaining 
355e5 65 6c 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20 69  elements. */.  i
355e6 66 28 20 6e 53 74 61 72 74 3e 30 20 29 20 64 6c  f( nStart>0 ) dl
355e7 77 41 70 70 65 6e 64 28 26 77 72 69 74 65 72 2c  wAppend(&writer,
355e8 20 70 53 74 61 72 74 2c 20 6e 53 74 61 72 74 2c   pStart, nStart,
355e9 20 69 46 69 72 73 74 44 6f 63 69 64 2c 20 69 4c   iFirstDocid, iL
355ea 61 73 74 44 6f 63 69 64 29 3b 0a 20 20 64 6c 77  astDocid);.  dlw
355eb 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29  Destroy(&writer)
355ec 3b 0a 7d 0a 0a 2f 2a 20 48 65 6c 70 65 72 20 66  ;.}../* Helper f
355ed 75 6e 63 74 69 6f 6e 20 66 6f 72 20 70 6f 73 4c  unction for posL
355ee 69 73 74 55 6e 69 6f 6e 28 29 2e 20 20 43 6f 6d  istUnion().  Com
355ef 70 61 72 65 73 20 74 68 65 20 63 75 72 72 65 6e  pares the curren
355f0 74 20 70 6f 73 69 74 69 6f 6e 0a 2a 2a 20 62 65  t position.** be
355f1 74 77 65 65 6e 20 6c 65 66 74 20 61 6e 64 20 72  tween left and r
355f2 69 67 68 74 2c 20 72 65 74 75 72 6e 69 6e 67 20  ight, returning 
355f3 61 73 20 73 74 61 6e 64 61 72 64 20 43 20 69 64  as standard C id
355f4 69 6f 6d 20 6f 66 20 3c 30 20 69 66 0a 2a 2a 20  iom of <0 if.** 
355f5 6c 65 66 74 3c 72 69 67 68 74 2c 20 3e 30 20 69  left<right, >0 i
355f6 66 20 6c 65 66 74 3e 72 69 67 68 74 2c 20 61 6e  f left>right, an
355f7 64 20 30 20 69 66 20 6c 65 66 74 3d 3d 72 69 67  d 0 if left==rig
355f8 68 74 2e 20 20 22 45 6e 64 22 20 61 6c 77 61 79  ht.  "End" alway
355f9 73 0a 2a 2a 20 63 6f 6d 70 61 72 65 73 20 67 72  s.** compares gr
355fa 65 61 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  eater..*/.static
355fb 20 69 6e 74 20 70 6f 73 4c 69 73 74 43 6d 70 28   int posListCmp(
355fc 50 4c 52 65 61 64 65 72 20 2a 70 4c 65 66 74 2c  PLReader *pLeft,
355fd 20 50 4c 52 65 61 64 65 72 20 2a 70 52 69 67 68   PLReader *pRigh
355fe 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4c  t){.  assert( pL
355ff 65 66 74 2d 3e 69 54 79 70 65 3d 3d 70 52 69 67  eft->iType==pRig
35600 68 74 2d 3e 69 54 79 70 65 20 29 3b 0a 20 20 69  ht->iType );.  i
35601 66 28 20 70 4c 65 66 74 2d 3e 69 54 79 70 65 3d  f( pLeft->iType=
35602 3d 44 4c 5f 44 4f 43 49 44 53 20 29 20 72 65 74  =DL_DOCIDS ) ret
35603 75 72 6e 20 30 3b 0a 0a 20 20 69 66 28 20 70 6c  urn 0;..  if( pl
35604 72 41 74 45 6e 64 28 70 4c 65 66 74 29 20 29 20  rAtEnd(pLeft) ) 
35605 72 65 74 75 72 6e 20 70 6c 72 41 74 45 6e 64 28  return plrAtEnd(
35606 70 52 69 67 68 74 29 20 3f 20 30 20 3a 20 31 3b  pRight) ? 0 : 1;
35607 0a 20 20 69 66 28 20 70 6c 72 41 74 45 6e 64 28  .  if( plrAtEnd(
35608 70 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e  pRight) ) return
35609 20 2d 31 3b 0a 0a 20 20 69 66 28 20 70 6c 72 43   -1;..  if( plrC
3560a 6f 6c 75 6d 6e 28 70 4c 65 66 74 29 3c 70 6c 72  olumn(pLeft)<plr
3560b 43 6f 6c 75 6d 6e 28 70 52 69 67 68 74 29 20 29  Column(pRight) )
3560c 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66   return -1;.  if
3560d 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c 65 66  ( plrColumn(pLef
3560e 74 29 3e 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 69  t)>plrColumn(pRi
3560f 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b  ght) ) return 1;
35610 0a 0a 20 20 69 66 28 20 70 6c 72 50 6f 73 69 74  ..  if( plrPosit
35611 69 6f 6e 28 70 4c 65 66 74 29 3c 70 6c 72 50 6f  ion(pLeft)<plrPo
35612 73 69 74 69 6f 6e 28 70 52 69 67 68 74 29 20 29  sition(pRight) )
35613 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66   return -1;.  if
35614 28 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 4c  ( plrPosition(pL
35615 65 66 74 29 3e 70 6c 72 50 6f 73 69 74 69 6f 6e  eft)>plrPosition
35616 28 70 52 69 67 68 74 29 20 29 20 72 65 74 75 72  (pRight) ) retur
35617 6e 20 31 3b 0a 20 20 69 66 28 20 70 4c 65 66 74  n 1;.  if( pLeft
35618 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49  ->iType==DL_POSI
35619 54 49 4f 4e 53 20 29 20 72 65 74 75 72 6e 20 30  TIONS ) return 0
3561a 3b 0a 0a 20 20 69 66 28 20 70 6c 72 53 74 61 72  ;..  if( plrStar
3561b 74 4f 66 66 73 65 74 28 70 4c 65 66 74 29 3c 70  tOffset(pLeft)<p
3561c 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28 70 52  lrStartOffset(pR
3561d 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 2d  ight) ) return -
3561e 31 3b 0a 20 20 69 66 28 20 70 6c 72 53 74 61 72  1;.  if( plrStar
3561f 74 4f 66 66 73 65 74 28 70 4c 65 66 74 29 3e 70  tOffset(pLeft)>p
35620 6c 72 53 74 61 72 74 4f 66 66 73 65 74 28 70 52  lrStartOffset(pR
35621 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 31  ight) ) return 1
35622 3b 0a 0a 20 20 69 66 28 20 70 6c 72 45 6e 64 4f  ;..  if( plrEndO
35623 66 66 73 65 74 28 70 4c 65 66 74 29 3c 70 6c 72  ffset(pLeft)<plr
35624 45 6e 64 4f 66 66 73 65 74 28 70 52 69 67 68 74  EndOffset(pRight
35625 29 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20  ) ) return -1;. 
35626 20 69 66 28 20 70 6c 72 45 6e 64 4f 66 66 73 65   if( plrEndOffse
35627 74 28 70 4c 65 66 74 29 3e 70 6c 72 45 6e 64 4f  t(pLeft)>plrEndO
35628 66 66 73 65 74 28 70 52 69 67 68 74 29 20 29 20  ffset(pRight) ) 
35629 72 65 74 75 72 6e 20 31 3b 0a 0a 20 20 72 65 74  return 1;..  ret
3562a 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 57 72 69  urn 0;.}../* Wri
3562b 74 65 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20  te the union of 
3562c 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 73 20 69  position lists i
3562d 6e 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67  n pLeft and pRig
3562e 68 74 20 74 6f 20 70 4f 75 74 2e 0a 2a 2a 20 22  ht to pOut..** "
3562f 55 6e 69 6f 6e 22 20 69 6e 20 74 68 69 73 20 63  Union" in this c
35630 61 73 65 20 6d 65 61 6e 69 6e 67 20 22 41 6c 6c  ase meaning "All
35631 20 75 6e 69 71 75 65 20 70 6f 73 69 74 69 6f 6e   unique position
35632 20 74 75 70 6c 65 73 22 2e 20 20 53 68 6f 75 6c   tuples".  Shoul
35633 64 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68 20 61  d.** work with a
35634 6e 79 20 64 6f 63 6c 69 73 74 20 74 79 70 65 2c  ny doclist type,
35635 20 74 68 6f 75 67 68 20 62 6f 74 68 20 69 6e 70   though both inp
35636 75 74 73 20 61 6e 64 20 74 68 65 20 6f 75 74 70  uts and the outp
35637 75 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20  ut.** should be 
35638 74 68 65 20 73 61 6d 65 20 74 79 70 65 2e 0a 2a  the same type..*
35639 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 6f  /.static void po
3563a 73 4c 69 73 74 55 6e 69 6f 6e 28 44 4c 52 65 61  sListUnion(DLRea
3563b 64 65 72 20 2a 70 4c 65 66 74 2c 20 44 4c 52 65  der *pLeft, DLRe
3563c 61 64 65 72 20 2a 70 52 69 67 68 74 2c 20 44 4c  ader *pRight, DL
3563d 57 72 69 74 65 72 20 2a 70 4f 75 74 29 7b 0a 20  Writer *pOut){. 
3563e 20 50 4c 52 65 61 64 65 72 20 6c 65 66 74 2c 20   PLReader left, 
3563f 72 69 67 68 74 3b 0a 20 20 50 4c 57 72 69 74 65  right;.  PLWrite
35640 72 20 77 72 69 74 65 72 3b 0a 0a 20 20 61 73 73  r writer;..  ass
35641 65 72 74 28 20 64 6c 72 44 6f 63 69 64 28 70 4c  ert( dlrDocid(pL
35642 65 66 74 29 3d 3d 64 6c 72 44 6f 63 69 64 28 70  eft)==dlrDocid(p
35643 52 69 67 68 74 29 20 29 3b 0a 20 20 61 73 73 65  Right) );.  asse
35644 72 74 28 20 70 4c 65 66 74 2d 3e 69 54 79 70 65  rt( pLeft->iType
35645 3d 3d 70 52 69 67 68 74 2d 3e 69 54 79 70 65 20  ==pRight->iType 
35646 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65  );.  assert( pLe
35647 66 74 2d 3e 69 54 79 70 65 3d 3d 70 4f 75 74 2d  ft->iType==pOut-
35648 3e 69 54 79 70 65 20 29 3b 0a 0a 20 20 70 6c 72  >iType );..  plr
35649 49 6e 69 74 28 26 6c 65 66 74 2c 20 70 4c 65 66  Init(&left, pLef
3564a 74 29 3b 0a 20 20 70 6c 72 49 6e 69 74 28 26 72  t);.  plrInit(&r
3564b 69 67 68 74 2c 20 70 52 69 67 68 74 29 3b 0a 20  ight, pRight);. 
3564c 20 70 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72   plwInit(&writer
3564d 2c 20 70 4f 75 74 2c 20 64 6c 72 44 6f 63 69 64  , pOut, dlrDocid
3564e 28 70 4c 65 66 74 29 29 3b 0a 0a 20 20 77 68 69  (pLeft));..  whi
3564f 6c 65 28 20 21 70 6c 72 41 74 45 6e 64 28 26 6c  le( !plrAtEnd(&l
35650 65 66 74 29 20 7c 7c 20 21 70 6c 72 41 74 45 6e  eft) || !plrAtEn
35651 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20  d(&right) ){.   
35652 20 69 6e 74 20 63 20 3d 20 70 6f 73 4c 69 73 74   int c = posList
35653 43 6d 70 28 26 6c 65 66 74 2c 20 26 72 69 67 68  Cmp(&left, &righ
35654 74 29 3b 0a 20 20 20 20 69 66 28 20 63 3c 30 20  t);.    if( c<0 
35655 29 7b 0a 20 20 20 20 20 20 70 6c 77 43 6f 70 79  ){.      plwCopy
35656 28 26 77 72 69 74 65 72 2c 20 26 6c 65 66 74 29  (&writer, &left)
35657 3b 0a 20 20 20 20 20 20 70 6c 72 53 74 65 70 28  ;.      plrStep(
35658 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73  &left);.    }els
35659 65 20 69 66 28 20 63 3e 30 20 29 7b 0a 20 20 20  e if( c>0 ){.   
3565a 20 20 20 70 6c 77 43 6f 70 79 28 26 77 72 69 74     plwCopy(&writ
3565b 65 72 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20  er, &right);.   
3565c 20 20 20 70 6c 72 53 74 65 70 28 26 72 69 67 68     plrStep(&righ
3565d 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  t);.    }else{. 
3565e 20 20 20 20 20 70 6c 77 43 6f 70 79 28 26 77 72       plwCopy(&wr
3565f 69 74 65 72 2c 20 26 6c 65 66 74 29 3b 0a 20 20  iter, &left);.  
35660 20 20 20 20 70 6c 72 53 74 65 70 28 26 6c 65 66      plrStep(&lef
35661 74 29 3b 0a 20 20 20 20 20 20 70 6c 72 53 74 65  t);.      plrSte
35662 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d  p(&right);.    }
35663 0a 20 20 7d 0a 0a 20 20 70 6c 77 54 65 72 6d 69  .  }..  plwTermi
35664 6e 61 74 65 28 26 77 72 69 74 65 72 29 3b 0a 20  nate(&writer);. 
35665 20 70 6c 77 44 65 73 74 72 6f 79 28 26 77 72 69   plwDestroy(&wri
35666 74 65 72 29 3b 0a 20 20 70 6c 72 44 65 73 74 72  ter);.  plrDestr
35667 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20 70 6c 72  oy(&left);.  plr
35668 44 65 73 74 72 6f 79 28 26 72 69 67 68 74 29 3b  Destroy(&right);
35669 0a 7d 0a 0a 2f 2a 20 57 72 69 74 65 20 74 68 65  .}../* Write the
3566a 20 75 6e 69 6f 6e 20 6f 66 20 64 6f 63 6c 69 73   union of doclis
3566b 74 73 20 69 6e 20 70 4c 65 66 74 20 61 6e 64 20  ts in pLeft and 
3566c 70 52 69 67 68 74 20 74 6f 20 70 4f 75 74 2e 20  pRight to pOut. 
3566d 20 46 6f 72 0a 2a 2a 20 64 6f 63 69 64 73 20 69   For.** docids i
3566e 6e 20 63 6f 6d 6d 6f 6e 20 62 65 74 77 65 65 6e  n common between
3566f 20 74 68 65 20 69 6e 70 75 74 73 2c 20 74 68 65   the inputs, the
35670 20 75 6e 69 6f 6e 20 6f 66 20 74 68 65 20 70 6f   union of the po
35671 73 69 74 69 6f 6e 0a 2a 2a 20 6c 69 73 74 73 20  sition.** lists 
35672 69 73 20 77 72 69 74 74 65 6e 2e 20 20 49 6e 70  is written.  Inp
35673 75 74 73 20 61 6e 64 20 6f 75 74 70 75 74 73 20  uts and outputs 
35674 61 72 65 20 61 6c 77 61 79 73 20 74 79 70 65 20  are always type 
35675 44 4c 5f 44 45 46 41 55 4c 54 2e 0a 2a 2f 0a 73  DL_DEFAULT..*/.s
35676 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c 69  tatic void docLi
35677 73 74 55 6e 69 6f 6e 28 0a 20 20 63 6f 6e 73 74  stUnion(.  const
35678 20 63 68 61 72 20 2a 70 4c 65 66 74 2c 20 69 6e   char *pLeft, in
35679 74 20 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74  t nLeft,.  const
3567a 20 63 68 61 72 20 2a 70 52 69 67 68 74 2c 20 69   char *pRight, i
3567b 6e 74 20 6e 52 69 67 68 74 2c 0a 20 20 44 61 74  nt nRight,.  Dat
3567c 61 42 75 66 66 65 72 20 2a 70 4f 75 74 20 20 20  aBuffer *pOut   
3567d 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20     /* Write the 
3567e 63 6f 6d 62 69 6e 65 64 20 64 6f 63 6c 69 73 74  combined doclist
3567f 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 44 4c   here */.){.  DL
35680 52 65 61 64 65 72 20 6c 65 66 74 2c 20 72 69 67  Reader left, rig
35681 68 74 3b 0a 20 20 44 4c 57 72 69 74 65 72 20 77  ht;.  DLWriter w
35682 72 69 74 65 72 3b 0a 0a 20 20 69 66 28 20 6e 4c  riter;..  if( nL
35683 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  eft==0 ){.    if
35684 28 20 6e 52 69 67 68 74 21 3d 30 29 20 64 61 74  ( nRight!=0) dat
35685 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 4f  aBufferAppend(pO
35686 75 74 2c 20 70 52 69 67 68 74 2c 20 6e 52 69 67  ut, pRight, nRig
35687 68 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ht);.    return;
35688 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52 69 67 68  .  }.  if( nRigh
35689 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64 61 74 61  t==0 ){.    data
3568a 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 4f 75  BufferAppend(pOu
3568b 74 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29  t, pLeft, nLeft)
3568c 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
3568d 7d 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26 6c 65  }..  dlrInit(&le
3568e 66 74 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20  ft, DL_DEFAULT, 
3568f 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20  pLeft, nLeft);. 
35690 20 64 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c   dlrInit(&right,
35691 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70 52 69   DL_DEFAULT, pRi
35692 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20  ght, nRight);.  
35693 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c  dlwInit(&writer,
35694 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70 4f 75   DL_DEFAULT, pOu
35695 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21 64  t);..  while( !d
35696 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29 20 7c  lrAtEnd(&left) |
35697 7c 20 21 64 6c 72 41 74 45 6e 64 28 26 72 69 67  | !dlrAtEnd(&rig
35698 68 74 29 20 29 7b 0a 20 20 20 20 69 66 28 20 64  ht) ){.    if( d
35699 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29 20  lrAtEnd(&right) 
3569a 29 7b 0a 20 20 20 20 20 20 64 6c 77 43 6f 70 79  ){.      dlwCopy
3569b 28 26 77 72 69 74 65 72 2c 20 26 6c 65 66 74 29  (&writer, &left)
3569c 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28  ;.      dlrStep(
3569d 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73  &left);.    }els
3569e 65 20 69 66 28 20 64 6c 72 41 74 45 6e 64 28 26  e if( dlrAtEnd(&
3569f 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64  left) ){.      d
356a0 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72 2c 20  lwCopy(&writer, 
356a1 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20 20 64  &right);.      d
356a2 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
356a3 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6c      }else if( dl
356a4 72 44 6f 63 69 64 28 26 6c 65 66 74 29 3c 64 6c  rDocid(&left)<dl
356a5 72 44 6f 63 69 64 28 26 72 69 67 68 74 29 20 29  rDocid(&right) )
356a6 7b 0a 20 20 20 20 20 20 64 6c 77 43 6f 70 79 28  {.      dlwCopy(
356a7 26 77 72 69 74 65 72 2c 20 26 6c 65 66 74 29 3b  &writer, &left);
356a8 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26  .      dlrStep(&
356a9 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  left);.    }else
356aa 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28 26 6c   if( dlrDocid(&l
356ab 65 66 74 29 3e 64 6c 72 44 6f 63 69 64 28 26 72  eft)>dlrDocid(&r
356ac 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 64  ight) ){.      d
356ad 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72 2c 20  lwCopy(&writer, 
356ae 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20 20 64  &right);.      d
356af 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
356b0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
356b1 20 70 6f 73 4c 69 73 74 55 6e 69 6f 6e 28 26 6c   posListUnion(&l
356b2 65 66 74 2c 20 26 72 69 67 68 74 2c 20 26 77 72  eft, &right, &wr
356b3 69 74 65 72 29 3b 0a 20 20 20 20 20 20 64 6c 72  iter);.      dlr
356b4 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20  Step(&left);.   
356b5 20 20 20 64 6c 72 53 74 65 70 28 26 72 69 67 68     dlrStep(&righ
356b6 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  t);.    }.  }.. 
356b7 20 64 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66   dlrDestroy(&lef
356b8 74 29 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f 79  t);.  dlrDestroy
356b9 28 26 72 69 67 68 74 29 3b 0a 20 20 64 6c 77 44  (&right);.  dlwD
356ba 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b  estroy(&writer);
356bb 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20  .}../* .** This 
356bc 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64  function is used
356bd 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20   as part of the 
356be 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
356bf 66 20 70 68 72 61 73 65 20 61 6e 64 0a 2a 2a 20  f phrase and.** 
356c0 4e 45 41 52 20 6d 61 74 63 68 69 6e 67 2e 0a 2a  NEAR matching..*
356c1 2a 0a 2a 2a 20 70 4c 65 66 74 20 61 6e 64 20 70  *.** pLeft and p
356c2 52 69 67 68 74 20 61 72 65 20 44 4c 52 65 61 64  Right are DLRead
356c3 65 72 73 20 70 6f 73 69 74 69 6f 6e 65 64 20 74  ers positioned t
356c4 6f 20 74 68 65 20 73 61 6d 65 20 64 6f 63 69 64  o the same docid
356c5 20 69 6e 0a 2a 2a 20 6c 69 73 74 73 20 6f 66 20   in.** lists of 
356c6 74 79 70 65 20 44 4c 5f 50 4f 53 49 54 49 4f 4e  type DL_POSITION
356c7 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  . This function 
356c8 77 72 69 74 65 73 20 61 6e 20 65 6e 74 72 79 20  writes an entry 
356c9 74 6f 20 74 68 65 0a 2a 2a 20 44 4c 57 72 69 74  to the.** DLWrit
356ca 65 72 20 70 4f 75 74 20 66 6f 72 20 65 61 63 68  er pOut for each
356cb 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 70 52 69   position in pRi
356cc 67 68 74 20 74 68 61 74 20 69 73 20 6c 65 73 73  ght that is less
356cd 20 74 68 61 6e 0a 2a 2a 20 28 6e 4e 65 61 72 2b   than.** (nNear+
356ce 31 29 20 67 72 65 61 74 65 72 20 28 62 75 74 20  1) greater (but 
356cf 6e 6f 74 20 65 71 75 61 6c 20 74 6f 20 6f 72 20  not equal to or 
356d0 73 6d 61 6c 6c 65 72 29 20 74 68 61 6e 20 61 20  smaller) than a 
356d1 70 6f 73 69 74 69 6f 6e 20 0a 2a 2a 20 69 6e 20  position .** in 
356d2 70 4c 65 66 74 2e 20 46 6f 72 20 65 78 61 6d 70  pLeft. For examp
356d3 6c 65 2c 20 69 66 20 6e 4e 65 61 72 20 69 73 20  le, if nNear is 
356d4 30 2c 20 61 6e 64 20 74 68 65 20 70 6f 73 69 74  0, and the posit
356d5 69 6f 6e 73 20 63 6f 6e 74 61 69 6e 65 64 0a 2a  ions contained.*
356d6 2a 20 62 79 20 70 4c 65 66 74 20 61 6e 64 20 70  * by pLeft and p
356d7 52 69 67 68 74 20 61 72 65 3a 0a 2a 2a 0a 2a 2a  Right are:.**.**
356d8 20 20 20 20 70 4c 65 66 74 3a 20 20 35 20 31 30      pLeft:  5 10
356d9 20 31 35 20 32 30 0a 2a 2a 20 20 20 20 70 52 69   15 20.**    pRi
356da 67 68 74 3a 20 36 20 20 39 20 31 37 20 32 31 0a  ght: 6  9 17 21.
356db 2a 2a 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 64  **.** then the d
356dc 6f 63 69 64 20 69 73 20 61 64 64 65 64 20 74 6f  ocid is added to
356dd 20 70 4f 75 74 2e 20 49 66 20 70 4f 75 74 20 69   pOut. If pOut i
356de 73 20 6f 66 20 74 79 70 65 20 44 4c 5f 50 4f 53  s of type DL_POS
356df 49 54 49 4f 4e 53 2c 0a 2a 2a 20 74 68 65 6e 20  ITIONS,.** then 
356e0 61 20 70 6f 73 69 74 69 6f 6e 69 64 73 20 22 36  a positionids "6
356e1 22 20 61 6e 64 20 22 32 31 22 20 61 72 65 20 61  " and "21" are a
356e2 6c 73 6f 20 61 64 64 65 64 20 74 6f 20 70 4f 75  lso added to pOu
356e3 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f 6f 6c  t..**.** If bool
356e4 65 61 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 53  ean argument isS
356e5 61 76 65 4c 65 66 74 20 69 73 20 74 72 75 65 2c  aveLeft is true,
356e6 20 74 68 65 6e 20 70 6f 73 69 74 69 6f 6e 69 64   then positionid
356e7 73 20 61 72 65 20 63 6f 70 69 65 64 0a 2a 2a 20  s are copied.** 
356e8 66 72 6f 6d 20 70 4c 65 66 74 20 69 6e 73 74 65  from pLeft inste
356e9 61 64 20 6f 66 20 70 52 69 67 68 74 2e 20 49 6e  ad of pRight. In
356ea 20 74 68 65 20 65 78 61 6d 70 6c 65 20 61 62 6f   the example abo
356eb 76 65 2c 20 74 68 65 20 70 6f 73 69 74 69 6f 6e  ve, the position
356ec 73 20 22 35 22 0a 2a 2a 20 61 6e 64 20 22 32 30  s "5".** and "20
356ed 22 20 77 6f 75 6c 64 20 62 65 20 61 64 64 65 64  " would be added
356ee 20 69 6e 73 74 65 61 64 20 6f 66 20 22 36 22 20   instead of "6" 
356ef 61 6e 64 20 22 32 31 22 2e 0a 2a 2f 0a 73 74 61  and "21"..*/.sta
356f0 74 69 63 20 76 6f 69 64 20 70 6f 73 4c 69 73 74  tic void posList
356f1 50 68 72 61 73 65 4d 65 72 67 65 28 0a 20 20 44  PhraseMerge(.  D
356f2 4c 52 65 61 64 65 72 20 2a 70 4c 65 66 74 2c 20  LReader *pLeft, 
356f3 0a 20 20 44 4c 52 65 61 64 65 72 20 2a 70 52 69  .  DLReader *pRi
356f4 67 68 74 2c 0a 20 20 69 6e 74 20 6e 4e 65 61 72  ght,.  int nNear
356f5 2c 0a 20 20 69 6e 74 20 69 73 53 61 76 65 4c 65  ,.  int isSaveLe
356f6 66 74 2c 0a 20 20 44 4c 57 72 69 74 65 72 20 2a  ft,.  DLWriter *
356f7 70 4f 75 74 0a 29 7b 0a 20 20 50 4c 52 65 61 64  pOut.){.  PLRead
356f8 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a  er left, right;.
356f9 20 20 50 4c 57 72 69 74 65 72 20 77 72 69 74 65    PLWriter write
356fa 72 3b 0a 20 20 69 6e 74 20 6d 61 74 63 68 20 3d  r;.  int match =
356fb 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64   0;..  assert( d
356fc 6c 72 44 6f 63 69 64 28 70 4c 65 66 74 29 3d 3d  lrDocid(pLeft)==
356fd 64 6c 72 44 6f 63 69 64 28 70 52 69 67 68 74 29  dlrDocid(pRight)
356fe 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
356ff 75 74 2d 3e 69 54 79 70 65 21 3d 44 4c 5f 50 4f  ut->iType!=DL_PO
35700 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20  SITIONS_OFFSETS 
35701 29 3b 0a 0a 20 20 70 6c 72 49 6e 69 74 28 26 6c  );..  plrInit(&l
35702 65 66 74 2c 20 70 4c 65 66 74 29 3b 0a 20 20 70  eft, pLeft);.  p
35703 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c 20 70  lrInit(&right, p
35704 52 69 67 68 74 29 3b 0a 0a 20 20 77 68 69 6c 65  Right);..  while
35705 28 20 21 70 6c 72 41 74 45 6e 64 28 26 6c 65 66  ( !plrAtEnd(&lef
35706 74 29 20 26 26 20 21 70 6c 72 41 74 45 6e 64 28  t) && !plrAtEnd(
35707 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 69  &right) ){.    i
35708 66 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 6c 65  f( plrColumn(&le
35709 66 74 29 3c 70 6c 72 43 6f 6c 75 6d 6e 28 26 72  ft)<plrColumn(&r
3570a 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20 20 70  ight) ){.      p
3570b 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20  lrStep(&left);. 
3570c 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 6c 72     }else if( plr
3570d 43 6f 6c 75 6d 6e 28 26 6c 65 66 74 29 3e 70 6c  Column(&left)>pl
3570e 72 43 6f 6c 75 6d 6e 28 26 72 69 67 68 74 29 20  rColumn(&right) 
3570f 29 7b 0a 20 20 20 20 20 20 70 6c 72 53 74 65 70  ){.      plrStep
35710 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 65  (&right);.    }e
35711 6c 73 65 20 69 66 28 20 70 6c 72 50 6f 73 69 74  lse if( plrPosit
35712 69 6f 6e 28 26 6c 65 66 74 29 3e 3d 70 6c 72 50  ion(&left)>=plrP
35713 6f 73 69 74 69 6f 6e 28 26 72 69 67 68 74 29 20  osition(&right) 
35714 29 7b 0a 20 20 20 20 20 20 70 6c 72 53 74 65 70  ){.      plrStep
35715 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 65  (&right);.    }e
35716 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 28  lse{.      if( (
35717 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26 72 69 67  plrPosition(&rig
35718 68 74 29 2d 70 6c 72 50 6f 73 69 74 69 6f 6e 28  ht)-plrPosition(
35719 26 6c 65 66 74 29 29 3c 3d 28 6e 4e 65 61 72 2b  &left))<=(nNear+
3571a 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  1) ){.        if
3571b 28 20 21 6d 61 74 63 68 20 29 7b 0a 20 20 20 20  ( !match ){.    
3571c 20 20 20 20 20 20 70 6c 77 49 6e 69 74 28 26 77        plwInit(&w
3571d 72 69 74 65 72 2c 20 70 4f 75 74 2c 20 64 6c 72  riter, pOut, dlr
3571e 44 6f 63 69 64 28 70 4c 65 66 74 29 29 3b 0a 20  Docid(pLeft));. 
3571f 20 20 20 20 20 20 20 20 20 6d 61 74 63 68 20 3d           match =
35720 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   1;.        }.  
35721 20 20 20 20 20 20 69 66 28 20 21 69 73 53 61 76        if( !isSav
35722 65 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20  eLeft ){.       
35723 20 20 20 70 6c 77 41 64 64 28 26 77 72 69 74 65     plwAdd(&write
35724 72 2c 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 72 69  r, plrColumn(&ri
35725 67 68 74 29 2c 20 70 6c 72 50 6f 73 69 74 69 6f  ght), plrPositio
35726 6e 28 26 72 69 67 68 74 29 2c 20 30 2c 20 30 29  n(&right), 0, 0)
35727 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
35728 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77 41 64  .          plwAd
35729 64 28 26 77 72 69 74 65 72 2c 20 70 6c 72 43 6f  d(&writer, plrCo
3572a 6c 75 6d 6e 28 26 6c 65 66 74 29 2c 20 70 6c 72  lumn(&left), plr
3572b 50 6f 73 69 74 69 6f 6e 28 26 6c 65 66 74 29 2c  Position(&left),
3572c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   0, 0);.        
3572d 7d 0a 20 20 20 20 20 20 20 20 70 6c 72 53 74 65  }.        plrSte
3572e 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20  p(&right);.     
3572f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
35730 70 6c 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a  plrStep(&left);.
35731 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
35732 7d 0a 0a 20 20 69 66 28 20 6d 61 74 63 68 20 29  }..  if( match )
35733 7b 0a 20 20 20 20 70 6c 77 54 65 72 6d 69 6e 61  {.    plwTermina
35734 74 65 28 26 77 72 69 74 65 72 29 3b 0a 20 20 20  te(&writer);.   
35735 20 70 6c 77 44 65 73 74 72 6f 79 28 26 77 72 69   plwDestroy(&wri
35736 74 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 70 6c 72  ter);.  }..  plr
35737 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a  Destroy(&left);.
35738 20 20 70 6c 72 44 65 73 74 72 6f 79 28 26 72 69    plrDestroy(&ri
35739 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  ght);.}../*.** C
3573a 6f 6d 70 61 72 65 20 74 68 65 20 76 61 6c 75 65  ompare the value
3573b 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  s pointed to by 
3573c 74 68 65 20 50 4c 52 65 61 64 65 72 73 20 70 61  the PLReaders pa
3573d 73 73 65 64 20 61 73 20 61 72 67 75 6d 65 6e 74  ssed as argument
3573e 73 2e 20 0a 2a 2a 20 52 65 74 75 72 6e 20 2d 31  s. .** Return -1
3573f 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 70 6f   if the value po
35740 69 6e 74 65 64 20 74 6f 20 62 79 20 70 4c 65 66  inted to by pLef
35741 74 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20  t is considered 
35742 6c 65 73 73 20 74 68 61 6e 0a 2a 2a 20 74 68 65  less than.** the
35743 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 64 20 74   value pointed t
35744 6f 20 62 79 20 70 52 69 67 68 74 2c 20 2b 31 20  o by pRight, +1 
35745 69 66 20 69 74 20 69 73 20 63 6f 6e 73 69 64 65  if it is conside
35746 72 65 64 20 67 72 65 61 74 65 72 0a 2a 2a 20 74  red greater.** t
35747 68 61 6e 20 69 74 2c 20 6f 72 20 30 20 69 66 20  han it, or 0 if 
35748 69 74 20 69 73 20 65 71 75 61 6c 2e 20 69 2e 65  it is equal. i.e
35749 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 2a 70 4c  ..**.**     (*pL
3574a 65 66 74 20 2d 20 2a 70 52 69 67 68 74 29 0a 2a  eft - *pRight).*
3574b 2a 0a 2a 2a 20 41 20 50 4c 52 65 61 64 65 72 20  *.** A PLReader 
3574c 74 68 61 74 20 69 73 20 69 6e 20 74 68 65 20 45  that is in the E
3574d 4f 46 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 20  OF condition is 
3574e 63 6f 6e 73 69 64 65 72 65 64 20 67 72 65 61 74  considered great
3574f 65 72 20 74 68 61 6e 0a 2a 2a 20 61 6e 79 20 6f  er than.** any o
35750 74 68 65 72 2e 20 49 66 20 6e 65 69 74 68 65 72  ther. If neither
35751 20 61 72 67 75 6d 65 6e 74 20 69 73 20 69 6e 20   argument is in 
35752 45 4f 46 20 73 74 61 74 65 2c 20 74 68 65 20 72  EOF state, the r
35753 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 0a 2a  eturn value of.*
35754 2a 20 70 6c 72 43 6f 6c 75 6d 6e 28 29 20 69 73  * plrColumn() is
35755 20 75 73 65 64 2e 20 49 66 20 74 68 65 20 70 6c   used. If the pl
35756 72 43 6f 6c 75 6d 6e 28 29 20 76 61 6c 75 65 73  rColumn() values
35757 20 61 72 65 20 65 71 75 61 6c 2c 20 74 68 65 0a   are equal, the.
35758 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73  ** comparison is
35759 20 6f 6e 20 74 68 65 20 62 61 73 69 73 20 6f 66   on the basis of
3575a 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 29 2e 0a   plrPosition()..
3575b 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6c  */.static int pl
3575c 72 43 6f 6d 70 61 72 65 28 50 4c 52 65 61 64 65  rCompare(PLReade
3575d 72 20 2a 70 4c 65 66 74 2c 20 50 4c 52 65 61 64  r *pLeft, PLRead
3575e 65 72 20 2a 70 52 69 67 68 74 29 7b 0a 20 20 61  er *pRight){.  a
3575f 73 73 65 72 74 28 21 70 6c 72 41 74 45 6e 64 28  ssert(!plrAtEnd(
35760 70 4c 65 66 74 29 20 7c 7c 20 21 70 6c 72 41 74  pLeft) || !plrAt
35761 45 6e 64 28 70 52 69 67 68 74 29 29 3b 0a 0a 20  End(pRight));.. 
35762 20 69 66 28 20 70 6c 72 41 74 45 6e 64 28 70 52   if( plrAtEnd(pR
35763 69 67 68 74 29 20 7c 7c 20 70 6c 72 41 74 45 6e  ight) || plrAtEn
35764 64 28 70 4c 65 66 74 29 20 29 7b 0a 20 20 20 20  d(pLeft) ){.    
35765 72 65 74 75 72 6e 20 28 70 6c 72 41 74 45 6e 64  return (plrAtEnd
35766 28 70 52 69 67 68 74 29 20 3f 20 2d 31 20 3a 20  (pRight) ? -1 : 
35767 31 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 6c  1);.  }.  if( pl
35768 72 43 6f 6c 75 6d 6e 28 70 4c 65 66 74 29 21 3d  rColumn(pLeft)!=
35769 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 69 67 68 74  plrColumn(pRight
3576a 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
3576b 28 28 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c 65 66  ((plrColumn(pLef
3576c 74 29 3c 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 69  t)<plrColumn(pRi
3576d 67 68 74 29 29 20 3f 20 2d 31 20 3a 20 31 29 3b  ght)) ? -1 : 1);
3576e 0a 20 20 7d 0a 20 20 69 66 28 20 70 6c 72 50 6f  .  }.  if( plrPo
3576f 73 69 74 69 6f 6e 28 70 4c 65 66 74 29 21 3d 70  sition(pLeft)!=p
35770 6c 72 50 6f 73 69 74 69 6f 6e 28 70 52 69 67 68  lrPosition(pRigh
35771 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  t) ){.    return
35772 20 28 28 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70   ((plrPosition(p
35773 4c 65 66 74 29 3c 70 6c 72 50 6f 73 69 74 69 6f  Left)<plrPositio
35774 6e 28 70 52 69 67 68 74 29 29 20 3f 20 2d 31 20  n(pRight)) ? -1 
35775 3a 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  : 1);.  }.  retu
35776 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 57 65 20 68  rn 0;.}../* We h
35777 61 76 65 20 74 77 6f 20 64 6f 63 6c 69 73 74 73  ave two doclists
35778 20 77 69 74 68 20 70 6f 73 69 74 69 6f 6e 73 3a   with positions:
35779 20 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67    pLeft and pRig
3577a 68 74 2e 20 44 65 70 65 6e 64 69 6e 67 0a 2a 2a  ht. Depending.**
3577b 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66   on the value of
3577c 20 74 68 65 20 6e 4e 65 61 72 20 70 61 72 61 6d   the nNear param
3577d 65 74 65 72 2c 20 70 65 72 66 6f 72 6d 20 65 69  eter, perform ei
3577e 74 68 65 72 20 61 20 70 68 72 61 73 65 0a 2a 2a  ther a phrase.**
3577f 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 28 69   intersection (i
35780 66 20 6e 4e 65 61 72 3d 3d 30 29 20 6f 72 20 61  f nNear==0) or a
35781 20 4e 45 41 52 20 69 6e 74 65 72 73 65 63 74 69   NEAR intersecti
35782 6f 6e 20 28 69 66 20 6e 4e 65 61 72 3e 30 29 0a  on (if nNear>0).
35783 2a 2a 20 61 6e 64 20 77 72 69 74 65 20 74 68 65  ** and write the
35784 20 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 70 4f   results into pO
35785 75 74 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 68 72 61  ut..**.** A phra
35786 73 65 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20  se intersection 
35787 6d 65 61 6e 73 20 74 68 61 74 20 74 77 6f 20 64  means that two d
35788 6f 63 75 6d 65 6e 74 73 20 6f 6e 6c 79 20 6d 61  ocuments only ma
35789 74 63 68 0a 2a 2a 20 69 66 20 70 4c 65 66 74 2e  tch.** if pLeft.
3578a 69 50 6f 73 2b 31 3d 3d 70 52 69 67 68 74 2e 69  iPos+1==pRight.i
3578b 50 6f 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 45 41  Pos..**.** A NEA
3578c 52 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 6d  R intersection m
3578d 65 61 6e 73 20 74 68 61 74 20 74 77 6f 20 64 6f  eans that two do
3578e 63 75 6d 65 6e 74 73 20 6f 6e 6c 79 20 6d 61 74  cuments only mat
3578f 63 68 20 69 66 20 0a 2a 2a 20 28 61 62 73 28 70  ch if .** (abs(p
35790 4c 65 66 74 2e 69 50 6f 73 2d 70 52 69 67 68 74  Left.iPos-pRight
35791 2e 69 50 6f 73 29 3c 6e 4e 65 61 72 29 2e 0a 2a  .iPos)<nNear)..*
35792 2a 0a 2a 2a 20 49 66 20 61 20 4e 45 41 52 20 69  *.** If a NEAR i
35793 6e 74 65 72 73 65 63 74 69 6f 6e 20 69 73 20 72  ntersection is r
35794 65 71 75 65 73 74 65 64 2c 20 74 68 65 6e 20 74  equested, then t
35795 68 65 20 6e 50 68 72 61 73 65 20 61 72 67 75 6d  he nPhrase argum
35796 65 6e 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65  ent should.** be
35797 20 70 61 73 73 65 64 20 74 68 65 20 6e 75 6d 62   passed the numb
35798 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20  er of tokens in 
35799 74 68 65 20 74 77 6f 20 6f 70 65 72 61 6e 64 73  the two operands
3579a 20 74 6f 20 74 68 65 20 4e 45 41 52 20 6f 70 65   to the NEAR ope
3579b 72 61 74 6f 72 0a 2a 2a 20 63 6f 6d 62 69 6e 65  rator.** combine
3579c 64 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a  d. For example:.
3579d 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 51 75 65 72  **.**       Quer
3579e 79 20 73 79 6e 74 61 78 20 20 20 20 20 20 20 20  y syntax        
3579f 20 20 20 20 20 20 20 6e 50 68 72 61 73 65 0a 2a         nPhrase.*
357a0 2a 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  *      ---------
357a1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
357a2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
357a3 20 20 20 20 20 22 41 20 42 20 43 22 20 4e 45 41       "A B C" NEA
357a4 52 20 22 44 20 45 22 20 20 20 20 20 20 20 20 20  R "D E"         
357a5 35 0a 2a 2a 20 20 20 20 20 20 20 41 20 4e 45 41  5.**       A NEA
357a6 52 20 42 20 20 20 20 20 20 20 20 20 20 20 20 20  R B             
357a7 20 20 20 20 20 20 32 0a 2a 2a 0a 2a 2a 20 69 54        2.**.** iT
357a8 79 70 65 20 63 6f 6e 74 72 6f 6c 73 20 74 68 65  ype controls the
357a9 20 74 79 70 65 20 6f 66 20 64 61 74 61 20 77 72   type of data wr
357aa 69 74 74 65 6e 20 74 6f 20 70 4f 75 74 2e 20 20  itten to pOut.  
357ab 49 66 20 69 54 79 70 65 20 69 73 0a 2a 2a 20 44  If iType is.** D
357ac 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c 20 74 68 65  L_POSITIONS, the
357ad 20 70 6f 73 69 74 69 6f 6e 73 20 61 72 65 20 74   positions are t
357ae 68 6f 73 65 20 66 72 6f 6d 20 70 52 69 67 68 74  hose from pRight
357af 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
357b0 20 64 6f 63 4c 69 73 74 50 68 72 61 73 65 4d 65   docListPhraseMe
357b1 72 67 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61  rge(.  const cha
357b2 72 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c  r *pLeft, int nL
357b3 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  eft,.  const cha
357b4 72 20 2a 70 52 69 67 68 74 2c 20 69 6e 74 20 6e  r *pRight, int n
357b5 52 69 67 68 74 2c 0a 20 20 69 6e 74 20 6e 4e 65  Right,.  int nNe
357b6 61 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ar,            /
357b7 2a 20 30 20 66 6f 72 20 61 20 70 68 72 61 73 65  * 0 for a phrase
357b8 20 6d 65 72 67 65 2c 20 6e 6f 6e 2d 7a 65 72 6f   merge, non-zero
357b9 20 66 6f 72 20 61 20 4e 45 41 52 20 6d 65 72 67   for a NEAR merg
357ba 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 68 72 61  e */.  int nPhra
357bb 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  se,          /* 
357bc 4e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73  Number of tokens
357bd 20 69 6e 20 6c 65 66 74 2b 72 69 67 68 74 20 6f   in left+right o
357be 70 65 72 61 6e 64 73 20 74 6f 20 4e 45 41 52 20  perands to NEAR 
357bf 2a 2f 0a 20 20 44 6f 63 4c 69 73 74 54 79 70 65  */.  DocListType
357c0 20 69 54 79 70 65 2c 20 20 20 20 2f 2a 20 54 79   iType,    /* Ty
357c1 70 65 20 6f 66 20 64 6f 63 6c 69 73 74 20 74 6f  pe of doclist to
357c2 20 77 72 69 74 65 20 74 6f 20 70 4f 75 74 20 2a   write to pOut *
357c3 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 2a  /.  DataBuffer *
357c4 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57 72 69  pOut      /* Wri
357c5 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65 64 20  te the combined 
357c6 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a  doclist here */.
357c7 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 6c 65  ){.  DLReader le
357c8 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44 4c 57  ft, right;.  DLW
357c9 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a 0a 20  riter writer;.. 
357ca 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20 7c 7c   if( nLeft==0 ||
357cb 20 6e 52 69 67 68 74 3d 3d 30 20 29 20 72 65 74   nRight==0 ) ret
357cc 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  urn;..  assert( 
357cd 69 54 79 70 65 21 3d 44 4c 5f 50 4f 53 49 54 49  iType!=DL_POSITI
357ce 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 3b 0a 0a  ONS_OFFSETS );..
357cf 20 20 64 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c    dlrInit(&left,
357d0 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c 20 70   DL_POSITIONS, p
357d1 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20  Left, nLeft);.  
357d2 64 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c 20  dlrInit(&right, 
357d3 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c 20 70 52  DL_POSITIONS, pR
357d4 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20  ight, nRight);. 
357d5 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72   dlwInit(&writer
357d6 2c 20 69 54 79 70 65 2c 20 70 4f 75 74 29 3b 0a  , iType, pOut);.
357d7 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72 41 74  .  while( !dlrAt
357d8 45 6e 64 28 26 6c 65 66 74 29 20 26 26 20 21 64  End(&left) && !d
357d9 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29 20  lrAtEnd(&right) 
357da 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72 44 6f  ){.    if( dlrDo
357db 63 69 64 28 26 6c 65 66 74 29 3c 64 6c 72 44 6f  cid(&left)<dlrDo
357dc 63 69 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20  cid(&right) ){. 
357dd 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65       dlrStep(&le
357de 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ft);.    }else i
357df 66 28 20 64 6c 72 44 6f 63 69 64 28 26 72 69 67  f( dlrDocid(&rig
357e0 68 74 29 3c 64 6c 72 44 6f 63 69 64 28 26 6c 65  ht)<dlrDocid(&le
357e1 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 72  ft) ){.      dlr
357e2 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20  Step(&right);.  
357e3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
357e4 66 28 20 6e 4e 65 61 72 3d 3d 30 20 29 7b 0a 20  f( nNear==0 ){. 
357e5 20 20 20 20 20 20 20 70 6f 73 4c 69 73 74 50 68         posListPh
357e6 72 61 73 65 4d 65 72 67 65 28 26 6c 65 66 74 2c  raseMerge(&left,
357e7 20 26 72 69 67 68 74 2c 20 30 2c 20 30 2c 20 26   &right, 0, 0, &
357e8 77 72 69 74 65 72 29 3b 0a 20 20 20 20 20 20 7d  writer);.      }
357e9 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
357ea 20 54 68 69 73 20 63 61 73 65 20 6f 63 63 75 72   This case occur
357eb 73 20 77 68 65 6e 20 74 77 6f 20 74 65 72 6d 73  s when two terms
357ec 20 28 73 69 6d 70 6c 65 20 74 65 72 6d 73 20 6f   (simple terms o
357ed 72 20 70 68 72 61 73 65 73 29 20 61 72 65 0a 20  r phrases) are. 
357ee 20 20 20 20 20 20 20 20 2a 20 63 6f 6e 6e 65 63          * connec
357ef 74 65 64 20 62 79 20 61 20 4e 45 41 52 20 6f 70  ted by a NEAR op
357f0 65 72 61 74 6f 72 2c 20 73 70 61 6e 20 28 6e 4e  erator, span (nN
357f1 65 61 72 2b 31 29 2e 20 69 2e 65 2e 0a 20 20 20  ear+1). i.e..   
357f2 20 20 20 20 20 20 2a 0a 20 20 20 20 20 20 20 20        *.        
357f3 20 2a 20 20 20 20 20 27 22 74 65 72 72 69 62 6c   *     '"terribl
357f4 65 20 63 6f 6d 70 61 6e 79 22 20 4e 45 41 52 20  e company" NEAR 
357f5 77 69 64 67 65 74 27 0a 20 20 20 20 20 20 20 20  widget'.        
357f6 20 2a 2f 0a 20 20 20 20 20 20 20 20 44 61 74 61   */.        Data
357f7 42 75 66 66 65 72 20 6f 6e 65 20 3d 20 7b 30 2c  Buffer one = {0,
357f8 20 30 2c 20 30 7d 3b 0a 20 20 20 20 20 20 20 20   0, 0};.        
357f9 44 61 74 61 42 75 66 66 65 72 20 74 77 6f 20 3d  DataBuffer two =
357fa 20 7b 30 2c 20 30 2c 20 30 7d 3b 0a 0a 20 20 20   {0, 0, 0};..   
357fb 20 20 20 20 20 44 4c 57 72 69 74 65 72 20 64 6c       DLWriter dl
357fc 77 72 69 74 65 72 32 3b 0a 20 20 20 20 20 20 20  writer2;.       
357fd 20 44 4c 52 65 61 64 65 72 20 64 72 31 20 3d 20   DLReader dr1 = 
357fe 7b 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 7d 3b  {0, 0, 0, 0, 0};
357ff 20 0a 20 20 20 20 20 20 20 20 44 4c 52 65 61 64   .        DLRead
35800 65 72 20 64 72 32 20 3d 20 7b 30 2c 20 30 2c 20  er dr2 = {0, 0, 
35801 30 2c 20 30 2c 20 30 7d 3b 0a 0a 20 20 20 20 20  0, 0, 0};..     
35802 20 20 20 64 6c 77 49 6e 69 74 28 26 64 6c 77 72     dlwInit(&dlwr
35803 69 74 65 72 32 2c 20 69 54 79 70 65 2c 20 26 6f  iter2, iType, &o
35804 6e 65 29 3b 0a 20 20 20 20 20 20 20 20 70 6f 73  ne);.        pos
35805 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67 65 28  ListPhraseMerge(
35806 26 72 69 67 68 74 2c 20 26 6c 65 66 74 2c 20 6e  &right, &left, n
35807 4e 65 61 72 2d 33 2b 6e 50 68 72 61 73 65 2c 20  Near-3+nPhrase, 
35808 31 2c 20 26 64 6c 77 72 69 74 65 72 32 29 3b 0a  1, &dlwriter2);.
35809 20 20 20 20 20 20 20 20 64 6c 77 49 6e 69 74 28          dlwInit(
3580a 26 64 6c 77 72 69 74 65 72 32 2c 20 69 54 79 70  &dlwriter2, iTyp
3580b 65 2c 20 26 74 77 6f 29 3b 0a 20 20 20 20 20 20  e, &two);.      
3580c 20 20 70 6f 73 4c 69 73 74 50 68 72 61 73 65 4d    posListPhraseM
3580d 65 72 67 65 28 26 6c 65 66 74 2c 20 26 72 69 67  erge(&left, &rig
3580e 68 74 2c 20 6e 4e 65 61 72 2d 31 2c 20 30 2c 20  ht, nNear-1, 0, 
3580f 26 64 6c 77 72 69 74 65 72 32 29 3b 0a 0a 20 20  &dlwriter2);..  
35810 20 20 20 20 20 20 69 66 28 20 6f 6e 65 2e 6e 44        if( one.nD
35811 61 74 61 29 20 64 6c 72 49 6e 69 74 28 26 64 72  ata) dlrInit(&dr
35812 31 2c 20 69 54 79 70 65 2c 20 6f 6e 65 2e 70 44  1, iType, one.pD
35813 61 74 61 2c 20 6f 6e 65 2e 6e 44 61 74 61 29 3b  ata, one.nData);
35814 0a 20 20 20 20 20 20 20 20 69 66 28 20 74 77 6f  .        if( two
35815 2e 6e 44 61 74 61 29 20 64 6c 72 49 6e 69 74 28  .nData) dlrInit(
35816 26 64 72 32 2c 20 69 54 79 70 65 2c 20 74 77 6f  &dr2, iType, two
35817 2e 70 44 61 74 61 2c 20 74 77 6f 2e 6e 44 61 74  .pData, two.nDat
35818 61 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28  a);..        if(
35819 20 21 64 6c 72 41 74 45 6e 64 28 26 64 72 31 29   !dlrAtEnd(&dr1)
3581a 20 7c 7c 20 21 64 6c 72 41 74 45 6e 64 28 26 64   || !dlrAtEnd(&d
3581b 72 32 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  r2) ){.         
3581c 20 50 4c 52 65 61 64 65 72 20 70 72 31 20 3d 20   PLReader pr1 = 
3581d 7b 30 7d 3b 0a 20 20 20 20 20 20 20 20 20 20 50  {0};.          P
3581e 4c 52 65 61 64 65 72 20 70 72 32 20 3d 20 7b 30  LReader pr2 = {0
3581f 7d 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 50 4c  };..          PL
35820 57 72 69 74 65 72 20 70 6c 77 72 69 74 65 72 3b  Writer plwriter;
35821 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77 49 6e  .          plwIn
35822 69 74 28 26 70 6c 77 72 69 74 65 72 2c 20 26 77  it(&plwriter, &w
35823 72 69 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28  riter, dlrDocid(
35824 64 6c 72 41 74 45 6e 64 28 26 64 72 31 29 3f 26  dlrAtEnd(&dr1)?&
35825 64 72 32 3a 26 64 72 31 29 29 3b 0a 0a 20 20 20  dr2:&dr1));..   
35826 20 20 20 20 20 20 20 69 66 28 20 6f 6e 65 2e 6e         if( one.n
35827 44 61 74 61 20 29 20 70 6c 72 49 6e 69 74 28 26  Data ) plrInit(&
35828 70 72 31 2c 20 26 64 72 31 29 3b 0a 20 20 20 20  pr1, &dr1);.    
35829 20 20 20 20 20 20 69 66 28 20 74 77 6f 2e 6e 44        if( two.nD
3582a 61 74 61 20 29 20 70 6c 72 49 6e 69 74 28 26 70  ata ) plrInit(&p
3582b 72 32 2c 20 26 64 72 32 29 3b 0a 20 20 20 20 20  r2, &dr2);.     
3582c 20 20 20 20 20 77 68 69 6c 65 28 20 21 70 6c 72       while( !plr
3582d 41 74 45 6e 64 28 26 70 72 31 29 20 7c 7c 20 21  AtEnd(&pr1) || !
3582e 70 6c 72 41 74 45 6e 64 28 26 70 72 32 29 20 29  plrAtEnd(&pr2) )
3582f 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e  {.            in
35830 74 20 69 43 6f 6d 70 61 72 65 20 3d 20 70 6c 72  t iCompare = plr
35831 43 6f 6d 70 61 72 65 28 26 70 72 31 2c 20 26 70  Compare(&pr1, &p
35832 72 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  r2);.           
35833 20 73 77 69 74 63 68 28 20 69 43 6f 6d 70 61 72   switch( iCompar
35834 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  e ){.           
35835 20 20 20 63 61 73 65 20 2d 31 3a 0a 20 20 20 20     case -1:.    
35836 20 20 20 20 20 20 20 20 20 20 20 20 70 6c 77 43              plwC
35837 6f 70 79 28 26 70 6c 77 72 69 74 65 72 2c 20 26  opy(&plwriter, &
35838 70 72 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20  pr1);.          
35839 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 70        plrStep(&p
3583a 72 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  r1);.           
3583b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3583c 20 20 20 20 20 20 20 20 20 20 63 61 73 65 20 31            case 1
3583d 3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  :.              
3583e 20 20 70 6c 77 43 6f 70 79 28 26 70 6c 77 72 69    plwCopy(&plwri
3583f 74 65 72 2c 20 26 70 72 32 29 3b 0a 20 20 20 20  ter, &pr2);.    
35840 20 20 20 20 20 20 20 20 20 20 20 20 70 6c 72 53              plrS
35841 74 65 70 28 26 70 72 32 29 3b 0a 20 20 20 20 20  tep(&pr2);.     
35842 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
35843 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
35844 63 61 73 65 20 30 3a 0a 20 20 20 20 20 20 20 20  case 0:.        
35845 20 20 20 20 20 20 20 20 70 6c 77 43 6f 70 79 28          plwCopy(
35846 26 70 6c 77 72 69 74 65 72 2c 20 26 70 72 31 29  &plwriter, &pr1)
35847 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
35848 20 20 70 6c 72 53 74 65 70 28 26 70 72 31 29 3b    plrStep(&pr1);
35849 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3584a 20 70 6c 72 53 74 65 70 28 26 70 72 32 29 3b 0a   plrStep(&pr2);.
3584b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3584c 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20  break;.         
3584d 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d     }.          }
3584e 0a 20 20 20 20 20 20 20 20 20 20 70 6c 77 54 65  .          plwTe
3584f 72 6d 69 6e 61 74 65 28 26 70 6c 77 72 69 74 65  rminate(&plwrite
35850 72 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  r);.        }.  
35851 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72        dataBuffer
35852 44 65 73 74 72 6f 79 28 26 6f 6e 65 29 3b 0a 20  Destroy(&one);. 
35853 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65         dataBuffe
35854 72 44 65 73 74 72 6f 79 28 26 74 77 6f 29 3b 0a  rDestroy(&two);.
35855 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 6c        }.      dl
35856 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20  rStep(&left);.  
35857 20 20 20 20 64 6c 72 53 74 65 70 28 26 72 69 67      dlrStep(&rig
35858 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  ht);.    }.  }..
35859 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 6c 65    dlrDestroy(&le
3585a 66 74 29 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f  ft);.  dlrDestro
3585b 79 28 26 72 69 67 68 74 29 3b 0a 20 20 64 6c 77  y(&right);.  dlw
3585c 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72 29  Destroy(&writer)
3585d 3b 0a 7d 0a 0a 2f 2a 20 57 65 20 68 61 76 65 20  ;.}../* We have 
3585e 74 77 6f 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f  two DL_DOCIDS do
3585f 63 6c 69 73 74 73 3a 20 20 70 4c 65 66 74 20 61  clists:  pLeft a
35860 6e 64 20 70 52 69 67 68 74 2e 0a 2a 2a 20 57 72  nd pRight..** Wr
35861 69 74 65 20 74 68 65 20 69 6e 74 65 72 73 65 63  ite the intersec
35862 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 74 77  tion of these tw
35863 6f 20 64 6f 63 6c 69 73 74 73 20 69 6e 74 6f 20  o doclists into 
35864 70 4f 75 74 20 61 73 20 61 0a 2a 2a 20 44 4c 5f  pOut as a.** DL_
35865 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 2e 0a  DOCIDS doclist..
35866 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  */.static void d
35867 6f 63 4c 69 73 74 41 6e 64 4d 65 72 67 65 28 0a  ocListAndMerge(.
35868 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4c    const char *pL
35869 65 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74 2c 0a  eft, int nLeft,.
3586a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52    const char *pR
3586b 69 67 68 74 2c 20 69 6e 74 20 6e 52 69 67 68 74  ight, int nRight
3586c 2c 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 2a  ,.  DataBuffer *
3586d 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57 72 69  pOut      /* Wri
3586e 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65 64 20  te the combined 
3586f 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a  doclist here */.
35870 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 6c 65  ){.  DLReader le
35871 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44 4c 57  ft, right;.  DLW
35872 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a 0a 20  riter writer;.. 
35873 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20 7c 7c   if( nLeft==0 ||
35874 20 6e 52 69 67 68 74 3d 3d 30 20 29 20 72 65 74   nRight==0 ) ret
35875 75 72 6e 3b 0a 0a 20 20 64 6c 72 49 6e 69 74 28  urn;..  dlrInit(
35876 26 6c 65 66 74 2c 20 44 4c 5f 44 4f 43 49 44 53  &left, DL_DOCIDS
35877 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b  , pLeft, nLeft);
35878 0a 20 20 64 6c 72 49 6e 69 74 28 26 72 69 67 68  .  dlrInit(&righ
35879 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 52  t, DL_DOCIDS, pR
3587a 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20  ight, nRight);. 
3587b 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72   dlwInit(&writer
3587c 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 4f 75  , DL_DOCIDS, pOu
3587d 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21 64  t);..  while( !d
3587e 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29 20 26  lrAtEnd(&left) &
3587f 26 20 21 64 6c 72 41 74 45 6e 64 28 26 72 69 67  & !dlrAtEnd(&rig
35880 68 74 29 20 29 7b 0a 20 20 20 20 69 66 28 20 64  ht) ){.    if( d
35881 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 3c 64  lrDocid(&left)<d
35882 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74 29 20  lrDocid(&right) 
35883 29 7b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70  ){.      dlrStep
35884 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c  (&left);.    }el
35885 73 65 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28  se if( dlrDocid(
35886 26 72 69 67 68 74 29 3c 64 6c 72 44 6f 63 69 64  &right)<dlrDocid
35887 28 26 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20  (&left) ){.     
35888 20 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29   dlrStep(&right)
35889 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
3588a 20 20 20 64 6c 77 41 64 64 28 26 77 72 69 74 65     dlwAdd(&write
3588b 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66  r, dlrDocid(&lef
3588c 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74  t));.      dlrSt
3588d 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 20  ep(&left);.     
3588e 20 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29   dlrStep(&right)
3588f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 64  ;.    }.  }..  d
35890 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29  lrDestroy(&left)
35891 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26  ;.  dlrDestroy(&
35892 72 69 67 68 74 29 3b 0a 20 20 64 6c 77 44 65 73  right);.  dlwDes
35893 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d  troy(&writer);.}
35894 0a 0a 2f 2a 20 57 65 20 68 61 76 65 20 74 77 6f  ../* We have two
35895 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69   DL_DOCIDS docli
35896 73 74 73 3a 20 20 70 4c 65 66 74 20 61 6e 64 20  sts:  pLeft and 
35897 70 52 69 67 68 74 2e 0a 2a 2a 20 57 72 69 74 65  pRight..** Write
35898 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20 74 68   the union of th
35899 65 73 65 20 74 77 6f 20 64 6f 63 6c 69 73 74 73  ese two doclists
3589a 20 69 6e 74 6f 20 70 4f 75 74 20 61 73 20 61 0a   into pOut as a.
3589b 2a 2a 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63  ** DL_DOCIDS doc
3589c 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  list..*/.static 
3589d 76 6f 69 64 20 64 6f 63 4c 69 73 74 4f 72 4d 65  void docListOrMe
3589e 72 67 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61  rge(.  const cha
3589f 72 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c  r *pLeft, int nL
358a0 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  eft,.  const cha
358a1 72 20 2a 70 52 69 67 68 74 2c 20 69 6e 74 20 6e  r *pRight, int n
358a2 52 69 67 68 74 2c 0a 20 20 44 61 74 61 42 75 66  Right,.  DataBuf
358a3 66 65 72 20 2a 70 4f 75 74 20 20 20 20 20 20 2f  fer *pOut      /
358a4 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6d 62  * Write the comb
358a5 69 6e 65 64 20 64 6f 63 6c 69 73 74 20 68 65 72  ined doclist her
358a6 65 20 2a 2f 0a 29 7b 0a 20 20 44 4c 52 65 61 64  e */.){.  DLRead
358a7 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a  er left, right;.
358a8 20 20 44 4c 57 72 69 74 65 72 20 77 72 69 74 65    DLWriter write
358a9 72 3b 0a 0a 20 20 69 66 28 20 6e 4c 65 66 74 3d  r;..  if( nLeft=
358aa 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 52  =0 ){.    if( nR
358ab 69 67 68 74 21 3d 30 20 29 20 64 61 74 61 42 75  ight!=0 ) dataBu
358ac 66 66 65 72 41 70 70 65 6e 64 28 70 4f 75 74 2c  fferAppend(pOut,
358ad 20 70 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29   pRight, nRight)
358ae 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
358af 7d 0a 20 20 69 66 28 20 6e 52 69 67 68 74 3d 3d  }.  if( nRight==
358b0 30 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66  0 ){.    dataBuf
358b1 66 65 72 41 70 70 65 6e 64 28 70 4f 75 74 2c 20  ferAppend(pOut, 
358b2 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20  pLeft, nLeft);. 
358b3 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a     return;.  }..
358b4 20 20 64 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c    dlrInit(&left,
358b5 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 4c 65 66   DL_DOCIDS, pLef
358b6 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 64 6c 72  t, nLeft);.  dlr
358b7 49 6e 69 74 28 26 72 69 67 68 74 2c 20 44 4c 5f  Init(&right, DL_
358b8 44 4f 43 49 44 53 2c 20 70 52 69 67 68 74 2c 20  DOCIDS, pRight, 
358b9 6e 52 69 67 68 74 29 3b 0a 20 20 64 6c 77 49 6e  nRight);.  dlwIn
358ba 69 74 28 26 77 72 69 74 65 72 2c 20 44 4c 5f 44  it(&writer, DL_D
358bb 4f 43 49 44 53 2c 20 70 4f 75 74 29 3b 0a 0a 20  OCIDS, pOut);.. 
358bc 20 77 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e   while( !dlrAtEn
358bd 64 28 26 6c 65 66 74 29 20 7c 7c 20 21 64 6c 72  d(&left) || !dlr
358be 41 74 45 6e 64 28 26 72 69 67 68 74 29 20 29 7b  AtEnd(&right) ){
358bf 0a 20 20 20 20 69 66 28 20 64 6c 72 41 74 45 6e  .    if( dlrAtEn
358c0 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20  d(&right) ){.   
358c1 20 20 20 64 6c 77 41 64 64 28 26 77 72 69 74 65     dlwAdd(&write
358c2 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66  r, dlrDocid(&lef
358c3 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74  t));.      dlrSt
358c4 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d  ep(&left);.    }
358c5 65 6c 73 65 20 69 66 28 20 64 6c 72 41 74 45 6e  else if( dlrAtEn
358c6 64 28 26 6c 65 66 74 29 20 29 7b 0a 20 20 20 20  d(&left) ){.    
358c7 20 20 64 6c 77 41 64 64 28 26 77 72 69 74 65 72    dlwAdd(&writer
358c8 2c 20 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68  , dlrDocid(&righ
358c9 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74  t));.      dlrSt
358ca 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20  ep(&right);.    
358cb 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 44 6f 63  }else if( dlrDoc
358cc 69 64 28 26 6c 65 66 74 29 3c 64 6c 72 44 6f 63  id(&left)<dlrDoc
358cd 69 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20  id(&right) ){.  
358ce 20 20 20 20 64 6c 77 41 64 64 28 26 77 72 69 74      dlwAdd(&writ
358cf 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 6c 65  er, dlrDocid(&le
358d0 66 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72 53  ft));.      dlrS
358d1 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20  tep(&left);.    
358d2 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 44 6f 63  }else if( dlrDoc
358d3 69 64 28 26 72 69 67 68 74 29 3c 64 6c 72 44 6f  id(&right)<dlrDo
358d4 63 69 64 28 26 6c 65 66 74 29 20 29 7b 0a 20 20  cid(&left) ){.  
358d5 20 20 20 20 64 6c 77 41 64 64 28 26 77 72 69 74      dlwAdd(&writ
358d6 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 26 72 69  er, dlrDocid(&ri
358d7 67 68 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72  ght));.      dlr
358d8 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20  Step(&right);.  
358d9 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64    }else{.      d
358da 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c 20 64  lwAdd(&writer, d
358db 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 29 3b  lrDocid(&left));
358dc 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26  .      dlrStep(&
358dd 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 64 6c 72  left);.      dlr
358de 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20  Step(&right);.  
358df 20 20 7d 0a 20 20 7d 0a 0a 20 20 64 6c 72 44 65    }.  }..  dlrDe
358e0 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20  stroy(&left);.  
358e1 64 6c 72 44 65 73 74 72 6f 79 28 26 72 69 67 68  dlrDestroy(&righ
358e2 74 29 3b 0a 20 20 64 6c 77 44 65 73 74 72 6f 79  t);.  dlwDestroy
358e3 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a 0a 2f 2a  (&writer);.}../*
358e4 20 57 65 20 68 61 76 65 20 74 77 6f 20 44 4c 5f   We have two DL_
358e5 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74 73 3a  DOCIDS doclists:
358e6 20 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67    pLeft and pRig
358e7 68 74 2e 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74  ht..** Write int
358e8 6f 20 70 4f 75 74 20 61 73 20 44 4c 5f 44 4f 43  o pOut as DL_DOC
358e9 49 44 53 20 64 6f 63 6c 69 73 74 20 63 6f 6e 74  IDS doclist cont
358ea 61 69 6e 69 6e 67 20 61 6c 6c 20 64 6f 63 75 6d  aining all docum
358eb 65 6e 74 73 20 74 68 61 74 0a 2a 2a 20 6f 63 63  ents that.** occ
358ec 75 72 20 69 6e 20 70 4c 65 66 74 20 62 75 74 20  ur in pLeft but 
358ed 6e 6f 74 20 69 6e 20 70 52 69 67 68 74 2e 0a 2a  not in pRight..*
358ee 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f  /.static void do
358ef 63 4c 69 73 74 45 78 63 65 70 74 4d 65 72 67 65  cListExceptMerge
358f0 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  (.  const char *
358f1 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74  pLeft, int nLeft
358f2 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
358f3 70 52 69 67 68 74 2c 20 69 6e 74 20 6e 52 69 67  pRight, int nRig
358f4 68 74 2c 0a 20 20 44 61 74 61 42 75 66 66 65 72  ht,.  DataBuffer
358f5 20 2a 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57   *pOut      /* W
358f6 72 69 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65  rite the combine
358f7 64 20 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a  d doclist here *
358f8 2f 0a 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20  /.){.  DLReader 
358f9 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44  left, right;.  D
358fa 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a  LWriter writer;.
358fb 0a 20 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20  .  if( nLeft==0 
358fc 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
358fd 6e 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20  nRight==0 ){.   
358fe 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
358ff 64 28 70 4f 75 74 2c 20 70 4c 65 66 74 2c 20 6e  d(pOut, pLeft, n
35900 4c 65 66 74 29 3b 0a 20 20 20 20 72 65 74 75 72  Left);.    retur
35901 6e 3b 0a 20 20 7d 0a 0a 20 20 64 6c 72 49 6e 69  n;.  }..  dlrIni
35902 74 28 26 6c 65 66 74 2c 20 44 4c 5f 44 4f 43 49  t(&left, DL_DOCI
35903 44 53 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74  DS, pLeft, nLeft
35904 29 3b 0a 20 20 64 6c 72 49 6e 69 74 28 26 72 69  );.  dlrInit(&ri
35905 67 68 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20  ght, DL_DOCIDS, 
35906 70 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b  pRight, nRight);
35907 0a 20 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74  .  dlwInit(&writ
35908 65 72 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70  er, DL_DOCIDS, p
35909 4f 75 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20  Out);..  while( 
3590a 21 64 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29  !dlrAtEnd(&left)
3590b 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 21   ){.    while( !
3590c 64 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74 29  dlrAtEnd(&right)
3590d 20 26 26 20 64 6c 72 44 6f 63 69 64 28 26 72 69   && dlrDocid(&ri
3590e 67 68 74 29 3c 64 6c 72 44 6f 63 69 64 28 26 6c  ght)<dlrDocid(&l
3590f 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c  eft) ){.      dl
35910 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20  rStep(&right);. 
35911 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64 6c 72     }.    if( dlr
35912 41 74 45 6e 64 28 26 72 69 67 68 74 29 20 7c 7c  AtEnd(&right) ||
35913 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29   dlrDocid(&left)
35914 3c 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74  <dlrDocid(&right
35915 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 77 41 64  ) ){.      dlwAd
35916 64 28 26 77 72 69 74 65 72 2c 20 64 6c 72 44 6f  d(&writer, dlrDo
35917 63 69 64 28 26 6c 65 66 74 29 29 3b 0a 20 20 20  cid(&left));.   
35918 20 7d 0a 20 20 20 20 64 6c 72 53 74 65 70 28 26   }.    dlrStep(&
35919 6c 65 66 74 29 3b 0a 20 20 7d 0a 0a 20 20 64 6c  left);.  }..  dl
3591a 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b  rDestroy(&left);
3591b 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72  .  dlrDestroy(&r
3591c 69 67 68 74 29 3b 0a 20 20 64 6c 77 44 65 73 74  ight);.  dlwDest
3591d 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a  roy(&writer);.}.
3591e 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 73 74  .static char *st
3591f 72 69 6e 67 5f 64 75 70 5f 6e 28 63 6f 6e 73 74  ring_dup_n(const
35920 20 63 68 61 72 20 2a 73 2c 20 69 6e 74 20 6e 29   char *s, int n)
35921 7b 0a 20 20 63 68 61 72 20 2a 73 74 72 20 3d 20  {.  char *str = 
35922 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
35923 20 2b 20 31 29 3b 0a 20 20 6d 65 6d 63 70 79 28   + 1);.  memcpy(
35924 73 74 72 2c 20 73 2c 20 6e 29 3b 0a 20 20 73 74  str, s, n);.  st
35925 72 5b 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 72  r[n] = '\0';.  r
35926 65 74 75 72 6e 20 73 74 72 3b 0a 7d 0a 0a 2f 2a  eturn str;.}../*
35927 20 44 75 70 6c 69 63 61 74 65 20 61 20 73 74 72   Duplicate a str
35928 69 6e 67 3b 20 74 68 65 20 63 61 6c 6c 65 72 20  ing; the caller 
35929 6d 75 73 74 20 66 72 65 65 28 29 20 74 68 65 20  must free() the 
3592a 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 2e  returned string.
3592b 0a 20 2a 20 28 57 65 20 64 6f 6e 27 74 20 75 73  . * (We don't us
3592c 65 20 73 74 72 64 75 70 28 29 20 73 69 6e 63 65  e strdup() since
3592d 20 69 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20   it is not part 
3592e 6f 66 20 74 68 65 20 73 74 61 6e 64 61 72 64 20  of the standard 
3592f 43 20 6c 69 62 72 61 72 79 20 61 6e 64 0a 20 2a  C library and. *
35930 20 6d 61 79 20 6e 6f 74 20 62 65 20 61 76 61 69   may not be avai
35931 6c 61 62 6c 65 20 65 76 65 72 79 77 68 65 72 65  lable everywhere
35932 2e 29 20 2a 2f 0a 73 74 61 74 69 63 20 63 68 61  .) */.static cha
35933 72 20 2a 73 74 72 69 6e 67 5f 64 75 70 28 63 6f  r *string_dup(co
35934 6e 73 74 20 63 68 61 72 20 2a 73 29 7b 0a 20 20  nst char *s){.  
35935 72 65 74 75 72 6e 20 73 74 72 69 6e 67 5f 64 75  return string_du
35936 70 5f 6e 28 73 2c 20 73 74 72 6c 65 6e 28 73 29  p_n(s, strlen(s)
35937 29 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 6d 61 74 20  );.}../* Format 
35938 61 20 73 74 72 69 6e 67 2c 20 72 65 70 6c 61 63  a string, replac
35939 69 6e 67 20 65 61 63 68 20 6f 63 63 75 72 72 65  ing each occurre
3593a 6e 63 65 20 6f 66 20 74 68 65 20 25 20 63 68 61  nce of the % cha
3593b 72 61 63 74 65 72 20 77 69 74 68 0a 20 2a 20 7a  racter with. * z
3593c 44 62 2e 7a 4e 61 6d 65 2e 20 20 54 68 69 73 20  Db.zName.  This 
3593d 6d 61 79 20 62 65 20 6d 6f 72 65 20 63 6f 6e 76  may be more conv
3593e 65 6e 69 65 6e 74 20 74 68 61 6e 20 73 71 6c 69  enient than sqli
3593f 74 65 5f 6d 70 72 69 6e 74 66 28 29 0a 20 2a 20  te_mprintf(). * 
35940 77 68 65 6e 20 6f 6e 65 20 73 74 72 69 6e 67 20  when one string 
35941 69 73 20 75 73 65 64 20 72 65 70 65 61 74 65 64  is used repeated
35942 6c 79 20 69 6e 20 61 20 66 6f 72 6d 61 74 20 73  ly in a format s
35943 74 72 69 6e 67 2e 0a 20 2a 20 54 68 65 20 63 61  tring.. * The ca
35944 6c 6c 65 72 20 6d 75 73 74 20 66 72 65 65 28 29  ller must free()
35945 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74   the returned st
35946 72 69 6e 67 2e 20 2a 2f 0a 73 74 61 74 69 63 20  ring. */.static 
35947 63 68 61 72 20 2a 73 74 72 69 6e 67 5f 66 6f 72  char *string_for
35948 6d 61 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  mat(const char *
35949 7a 46 6f 72 6d 61 74 2c 0a 20 20 20 20 20 20 20  zFormat,.       
3594a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3594b 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
3594c 7a 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  zDb, const char 
3594d 2a 7a 4e 61 6d 65 29 7b 0a 20 20 63 6f 6e 73 74  *zName){.  const
3594e 20 63 68 61 72 20 2a 70 3b 0a 20 20 73 69 7a 65   char *p;.  size
3594f 5f 74 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 73 69  _t len = 0;.  si
35950 7a 65 5f 74 20 6e 44 62 20 3d 20 73 74 72 6c 65  ze_t nDb = strle
35951 6e 28 7a 44 62 29 3b 0a 20 20 73 69 7a 65 5f 74  n(zDb);.  size_t
35952 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28   nName = strlen(
35953 7a 4e 61 6d 65 29 3b 0a 20 20 73 69 7a 65 5f 74  zName);.  size_t
35954 20 6e 46 75 6c 6c 54 61 62 6c 65 4e 61 6d 65 20   nFullTableName 
35955 3d 20 6e 44 62 2b 31 2b 6e 4e 61 6d 65 3b 0a 20  = nDb+1+nName;. 
35956 20 63 68 61 72 20 2a 72 65 73 75 6c 74 3b 0a 20   char *result;. 
35957 20 63 68 61 72 20 2a 72 3b 0a 0a 20 20 2f 2a 20   char *r;..  /* 
35958 66 69 72 73 74 20 63 6f 6d 70 75 74 65 20 6c 65  first compute le
35959 6e 67 74 68 20 6e 65 65 64 65 64 20 2a 2f 0a 20  ngth needed */. 
3595a 20 66 6f 72 28 70 20 3d 20 7a 46 6f 72 6d 61 74   for(p = zFormat
3595b 20 3b 20 2a 70 20 3b 20 2b 2b 70 29 7b 0a 20 20   ; *p ; ++p){.  
3595c 20 20 6c 65 6e 20 2b 3d 20 28 2a 70 3d 3d 27 25    len += (*p=='%
3595d 27 20 3f 20 6e 46 75 6c 6c 54 61 62 6c 65 4e 61  ' ? nFullTableNa
3595e 6d 65 20 3a 20 31 29 3b 0a 20 20 7d 0a 20 20 6c  me : 1);.  }.  l
3595f 65 6e 20 2b 3d 20 31 3b 20 20 2f 2a 20 66 6f 72  en += 1;  /* for
35960 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74 6f 72   null terminator
35961 20 2a 2f 0a 0a 20 20 72 20 3d 20 72 65 73 75 6c   */..  r = resul
35962 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  t = sqlite3_mall
35963 6f 63 28 6c 65 6e 29 3b 0a 20 20 66 6f 72 28 70  oc(len);.  for(p
35964 20 3d 20 7a 46 6f 72 6d 61 74 3b 20 2a 70 3b 20   = zFormat; *p; 
35965 2b 2b 70 29 7b 0a 20 20 20 20 69 66 28 20 2a 70  ++p){.    if( *p
35966 3d 3d 27 25 27 20 29 7b 0a 20 20 20 20 20 20 6d  =='%' ){.      m
35967 65 6d 63 70 79 28 72 2c 20 7a 44 62 2c 20 6e 44  emcpy(r, zDb, nD
35968 62 29 3b 0a 20 20 20 20 20 20 72 20 2b 3d 20 6e  b);.      r += n
35969 44 62 3b 0a 20 20 20 20 20 20 2a 72 2b 2b 20 3d  Db;.      *r++ =
3596a 20 27 2e 27 3b 0a 20 20 20 20 20 20 6d 65 6d 63   '.';.      memc
3596b 70 79 28 72 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  py(r, zName, nNa
3596c 6d 65 29 3b 0a 20 20 20 20 20 20 72 20 2b 3d 20  me);.      r += 
3596d 6e 4e 61 6d 65 3b 0a 20 20 20 20 7d 20 65 6c 73  nName;.    } els
3596e 65 20 7b 0a 20 20 20 20 20 20 2a 72 2b 2b 20 3d  e {.      *r++ =
3596f 20 2a 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   *p;.    }.  }. 
35970 20 2a 72 2b 2b 20 3d 20 27 5c 30 27 3b 0a 20 20   *r++ = '\0';.  
35971 61 73 73 65 72 74 28 20 72 20 3d 3d 20 72 65 73  assert( r == res
35972 75 6c 74 20 2b 20 6c 65 6e 20 29 3b 0a 20 20 72  ult + len );.  r
35973 65 74 75 72 6e 20 72 65 73 75 6c 74 3b 0a 7d 0a  eturn result;.}.
35974 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 5f  .static int sql_
35975 65 78 65 63 28 73 71 6c 69 74 65 33 20 2a 64 62  exec(sqlite3 *db
35976 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  , const char *zD
35977 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
35978 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20  Name,.          
35979 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
3597a 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 29 7b 0a  char *zFormat){.
3597b 20 20 63 68 61 72 20 2a 7a 43 6f 6d 6d 61 6e 64    char *zCommand
3597c 20 3d 20 73 74 72 69 6e 67 5f 66 6f 72 6d 61 74   = string_format
3597d 28 7a 46 6f 72 6d 61 74 2c 20 7a 44 62 2c 20 7a  (zFormat, zDb, z
3597e 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20 72 63 3b  Name);.  int rc;
3597f 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54  .  FTSTRACE(("FT
35980 53 33 20 73 71 6c 3a 20 25 73 5c 6e 22 2c 20 7a  S3 sql: %s\n", z
35981 43 6f 6d 6d 61 6e 64 29 29 3b 0a 20 20 72 63 20  Command));.  rc 
35982 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64  = sqlite3_exec(d
35983 62 2c 20 7a 43 6f 6d 6d 61 6e 64 2c 20 4e 55 4c  b, zCommand, NUL
35984 4c 2c 20 30 2c 20 4e 55 4c 4c 29 3b 0a 20 20 73  L, 0, NULL);.  s
35985 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6d  qlite3_free(zCom
35986 6d 61 6e 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  mand);.  return 
35987 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  rc;.}..static in
35988 74 20 73 71 6c 5f 70 72 65 70 61 72 65 28 73 71  t sql_prepare(sq
35989 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
3598a 20 63 68 61 72 20 2a 7a 44 62 2c 20 63 6f 6e 73   char *zDb, cons
3598b 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20  t char *zName,. 
3598c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3598d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74        sqlite3_st
3598e 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 63 6f 6e  mt **ppStmt, con
3598f 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
35990 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6d 6d  ){.  char *zComm
35991 61 6e 64 20 3d 20 73 74 72 69 6e 67 5f 66 6f 72  and = string_for
35992 6d 61 74 28 7a 46 6f 72 6d 61 74 2c 20 7a 44 62  mat(zFormat, zDb
35993 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20  , zName);.  int 
35994 72 63 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28  rc;.  FTSTRACE((
35995 22 46 54 53 33 20 70 72 65 70 61 72 65 3a 20 25  "FTS3 prepare: %
35996 73 5c 6e 22 2c 20 7a 43 6f 6d 6d 61 6e 64 29 29  s\n", zCommand))
35997 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
35998 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 20  _prepare_v2(db, 
35999 7a 43 6f 6d 6d 61 6e 64 2c 20 2d 31 2c 20 70 70  zCommand, -1, pp
3599a 53 74 6d 74 2c 20 4e 55 4c 4c 29 3b 0a 20 20 73  Stmt, NULL);.  s
3599b 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6d  qlite3_free(zCom
3599c 6d 61 6e 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  mand);.  return 
3599d 72 63 3b 0a 7d 0a 0a 2f 2a 20 65 6e 64 20 75 74  rc;.}../* end ut
3599e 69 6c 69 74 79 20 66 75 6e 63 74 69 6f 6e 73 20  ility functions 
3599f 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72  */../* Forward r
359a0 65 66 65 72 65 6e 63 65 20 2a 2f 0a 74 79 70 65  eference */.type
359a1 64 65 66 20 73 74 72 75 63 74 20 66 75 6c 6c 74  def struct fullt
359a2 65 78 74 5f 76 74 61 62 20 66 75 6c 6c 74 65 78  ext_vtab fulltex
359a3 74 5f 76 74 61 62 3b 0a 0a 2f 2a 0a 2a 2a 20 41  t_vtab;../*.** A
359a4 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
359a5 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
359a6 63 74 75 72 65 20 6b 65 65 70 73 20 74 72 61 63  cture keeps trac
359a7 6b 20 6f 66 20 67 65 6e 65 72 61 74 65 64 0a 2a  k of generated.*
359a8 2a 20 6d 61 74 63 68 69 6e 67 2d 77 6f 72 64 20  * matching-word 
359a9 6f 66 66 73 65 74 20 69 6e 66 6f 72 6d 61 74 69  offset informati
359aa 6f 6e 20 61 6e 64 20 73 6e 69 70 70 65 74 73 2e  on and snippets.
359ab 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
359ac 63 74 20 53 6e 69 70 70 65 74 20 7b 0a 20 20 69  ct Snippet {.  i
359ad 6e 74 20 6e 4d 61 74 63 68 3b 20 20 20 20 20 2f  nt nMatch;     /
359ae 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  * Total number o
359af 66 20 6d 61 74 63 68 65 73 20 2a 2f 0a 20 20 69  f matches */.  i
359b0 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 2f  nt nAlloc;     /
359b1 2a 20 53 70 61 63 65 20 61 6c 6c 6f 63 61 74 65  * Space allocate
359b2 64 20 66 6f 72 20 61 4d 61 74 63 68 5b 5d 20 2a  d for aMatch[] *
359b3 2f 0a 20 20 73 74 72 75 63 74 20 73 6e 69 70 70  /.  struct snipp
359b4 65 74 4d 61 74 63 68 20 7b 20 2f 2a 20 4f 6e 65  etMatch { /* One
359b5 20 65 6e 74 72 79 20 66 6f 72 20 65 61 63 68 20   entry for each 
359b6 6d 61 74 63 68 69 6e 67 20 74 65 72 6d 20 2a 2f  matching term */
359b7 0a 20 20 20 20 63 68 61 72 20 73 6e 53 74 61 74  .    char snStat
359b8 75 73 3b 20 20 20 20 20 20 20 2f 2a 20 53 74 61  us;       /* Sta
359b9 74 75 73 20 66 6c 61 67 20 66 6f 72 20 75 73 65  tus flag for use
359ba 20 77 68 69 6c 65 20 63 6f 6e 73 74 72 75 63 74   while construct
359bb 69 6e 67 20 73 6e 69 70 70 65 74 73 20 2a 2f 0a  ing snippets */.
359bc 20 20 20 20 73 68 6f 72 74 20 69 6e 74 20 69 43      short int iC
359bd 6f 6c 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ol;      /* The 
359be 63 6f 6c 75 6d 6e 20 74 68 61 74 20 63 6f 6e 74  column that cont
359bf 61 69 6e 73 20 74 68 65 20 6d 61 74 63 68 20 2a  ains the match *
359c0 2f 0a 20 20 20 20 73 68 6f 72 74 20 69 6e 74 20  /.    short int 
359c1 69 54 65 72 6d 3b 20 20 20 20 20 2f 2a 20 54 68  iTerm;     /* Th
359c2 65 20 69 6e 64 65 78 20 69 6e 20 51 75 65 72 79  e index in Query
359c3 2e 70 54 65 72 6d 73 5b 5d 20 6f 66 20 74 68 65  .pTerms[] of the
359c4 20 6d 61 74 63 68 69 6e 67 20 74 65 72 6d 20 2a   matching term *
359c5 2f 0a 20 20 20 20 69 6e 74 20 69 54 6f 6b 65 6e  /.    int iToken
359c6 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  ;          /* Th
359c7 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6d  e index of the m
359c8 61 74 63 68 69 6e 67 20 64 6f 63 75 6d 65 6e 74  atching document
359c9 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 73 68   token */.    sh
359ca 6f 72 74 20 69 6e 74 20 6e 42 79 74 65 3b 20 20  ort int nByte;  
359cb 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
359cc 62 79 74 65 73 20 69 6e 20 74 68 65 20 74 65 72  bytes in the ter
359cd 6d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53 74  m */.    int iSt
359ce 61 72 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  art;          /*
359cf 20 54 68 65 20 6f 66 66 73 65 74 20 74 6f 20 74   The offset to t
359d0 68 65 20 66 69 72 73 74 20 63 68 61 72 61 63 74  he first charact
359d1 65 72 20 6f 66 20 74 68 65 20 74 65 72 6d 20 2a  er of the term *
359d2 2f 0a 20 20 7d 20 2a 61 4d 61 74 63 68 3b 20 20  /.  } *aMatch;  
359d3 20 20 20 20 2f 2a 20 50 6f 69 6e 74 73 20 74 6f      /* Points to
359d4 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64 20   space obtained 
359d5 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 2a 2f 0a 20  from malloc */. 
359d6 20 63 68 61 72 20 2a 7a 4f 66 66 73 65 74 3b 20   char *zOffset; 
359d7 20 2f 2a 20 54 65 78 74 20 72 65 6e 64 65 72 69   /* Text renderi
359d8 6e 67 20 6f 66 20 61 4d 61 74 63 68 5b 5d 20 2a  ng of aMatch[] *
359d9 2f 0a 20 20 69 6e 74 20 6e 4f 66 66 73 65 74 3b  /.  int nOffset;
359da 20 20 20 20 2f 2a 20 73 74 72 6c 65 6e 28 7a 4f      /* strlen(zO
359db 66 66 73 65 74 29 20 2a 2f 0a 20 20 63 68 61 72  ffset) */.  char
359dc 20 2a 7a 53 6e 69 70 70 65 74 3b 20 2f 2a 20 53   *zSnippet; /* S
359dd 6e 69 70 70 65 74 20 74 65 78 74 20 2a 2f 0a 20  nippet text */. 
359de 20 69 6e 74 20 6e 53 6e 69 70 70 65 74 3b 20 20   int nSnippet;  
359df 20 2f 2a 20 73 74 72 6c 65 6e 28 7a 53 6e 69 70   /* strlen(zSnip
359e0 70 65 74 29 20 2a 2f 0a 7d 20 53 6e 69 70 70 65  pet) */.} Snippe
359e1 74 3b 0a 0a 0a 74 79 70 65 64 65 66 20 65 6e 75  t;...typedef enu
359e2 6d 20 51 75 65 72 79 54 79 70 65 20 7b 0a 20 20  m QueryType {.  
359e3 51 55 45 52 59 5f 47 45 4e 45 52 49 43 2c 20 20  QUERY_GENERIC,  
359e4 20 2f 2a 20 74 61 62 6c 65 20 73 63 61 6e 20 2a   /* table scan *
359e5 2f 0a 20 20 51 55 45 52 59 5f 44 4f 43 49 44 2c  /.  QUERY_DOCID,
359e6 20 20 20 20 20 2f 2a 20 6c 6f 6f 6b 75 70 20 62       /* lookup b
359e7 79 20 64 6f 63 69 64 20 2a 2f 0a 20 20 51 55 45  y docid */.  QUE
359e8 52 59 5f 46 55 4c 4c 54 45 58 54 20 20 20 2f 2a  RY_FULLTEXT   /*
359e9 20 51 55 45 52 59 5f 46 55 4c 4c 54 45 58 54 20   QUERY_FULLTEXT 
359ea 2b 20 5b 69 5d 20 69 73 20 61 20 66 75 6c 6c 2d  + [i] is a full-
359eb 74 65 78 74 20 73 65 61 72 63 68 20 66 6f 72 20  text search for 
359ec 63 6f 6c 75 6d 6e 20 69 2a 2f 0a 7d 20 51 75 65  column i*/.} Que
359ed 72 79 54 79 70 65 3b 0a 0a 74 79 70 65 64 65 66  ryType;..typedef
359ee 20 65 6e 75 6d 20 66 75 6c 6c 74 65 78 74 5f 73   enum fulltext_s
359ef 74 61 74 65 6d 65 6e 74 20 7b 0a 20 20 43 4f 4e  tatement {.  CON
359f0 54 45 4e 54 5f 49 4e 53 45 52 54 5f 53 54 4d 54  TENT_INSERT_STMT
359f1 2c 0a 20 20 43 4f 4e 54 45 4e 54 5f 53 45 4c 45  ,.  CONTENT_SELE
359f2 43 54 5f 53 54 4d 54 2c 0a 20 20 43 4f 4e 54 45  CT_STMT,.  CONTE
359f3 4e 54 5f 55 50 44 41 54 45 5f 53 54 4d 54 2c 0a  NT_UPDATE_STMT,.
359f4 20 20 43 4f 4e 54 45 4e 54 5f 44 45 4c 45 54 45    CONTENT_DELETE
359f5 5f 53 54 4d 54 2c 0a 20 20 43 4f 4e 54 45 4e 54  _STMT,.  CONTENT
359f6 5f 45 58 49 53 54 53 5f 53 54 4d 54 2c 0a 0a 20  _EXISTS_STMT,.. 
359f7 20 42 4c 4f 43 4b 5f 49 4e 53 45 52 54 5f 53 54   BLOCK_INSERT_ST
359f8 4d 54 2c 0a 20 20 42 4c 4f 43 4b 5f 53 45 4c 45  MT,.  BLOCK_SELE
359f9 43 54 5f 53 54 4d 54 2c 0a 20 20 42 4c 4f 43 4b  CT_STMT,.  BLOCK
359fa 5f 44 45 4c 45 54 45 5f 53 54 4d 54 2c 0a 20 20  _DELETE_STMT,.  
359fb 42 4c 4f 43 4b 5f 44 45 4c 45 54 45 5f 41 4c 4c  BLOCK_DELETE_ALL
359fc 5f 53 54 4d 54 2c 0a 0a 20 20 53 45 47 44 49 52  _STMT,..  SEGDIR
359fd 5f 4d 41 58 5f 49 4e 44 45 58 5f 53 54 4d 54 2c  _MAX_INDEX_STMT,
359fe 0a 20 20 53 45 47 44 49 52 5f 53 45 54 5f 53 54  .  SEGDIR_SET_ST
359ff 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 53 45 4c  MT,.  SEGDIR_SEL
35a00 45 43 54 5f 4c 45 56 45 4c 5f 53 54 4d 54 2c 0a  ECT_LEVEL_STMT,.
35a01 20 20 53 45 47 44 49 52 5f 53 50 41 4e 5f 53 54    SEGDIR_SPAN_ST
35a02 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 44 45 4c  MT,.  SEGDIR_DEL
35a03 45 54 45 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44  ETE_STMT,.  SEGD
35a04 49 52 5f 53 45 4c 45 43 54 5f 53 45 47 4d 45 4e  IR_SELECT_SEGMEN
35a05 54 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52  T_STMT,.  SEGDIR
35a06 5f 53 45 4c 45 43 54 5f 41 4c 4c 5f 53 54 4d 54  _SELECT_ALL_STMT
35a07 2c 0a 20 20 53 45 47 44 49 52 5f 44 45 4c 45 54  ,.  SEGDIR_DELET
35a08 45 5f 41 4c 4c 5f 53 54 4d 54 2c 0a 20 20 53 45  E_ALL_STMT,.  SE
35a09 47 44 49 52 5f 43 4f 55 4e 54 5f 53 54 4d 54 2c  GDIR_COUNT_STMT,
35a0a 0a 0a 20 20 4d 41 58 5f 53 54 4d 54 20 20 20 20  ..  MAX_STMT    
35a0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35a0c 20 2f 2a 20 41 6c 77 61 79 73 20 61 74 20 65 6e   /* Always at en
35a0d 64 21 20 2a 2f 0a 7d 20 66 75 6c 6c 74 65 78 74  d! */.} fulltext
35a0e 5f 73 74 61 74 65 6d 65 6e 74 3b 0a 0a 2f 2a 20  _statement;../* 
35a0f 54 68 65 73 65 20 6d 75 73 74 20 65 78 61 63 74  These must exact
35a10 6c 79 20 6d 61 74 63 68 20 74 68 65 20 65 6e 75  ly match the enu
35a11 6d 20 61 62 6f 76 65 2e 20 2a 2f 0a 2f 2a 20 54  m above. */./* T
35a12 4f 44 4f 28 73 68 65 73 73 29 3a 20 49 73 20 74  ODO(shess): Is t
35a13 68 65 72 65 20 73 6f 6d 65 20 72 69 73 6b 20 74  here some risk t
35a14 68 61 74 20 61 20 73 74 61 74 65 6d 65 6e 74 20  hat a statement 
35a15 77 69 6c 6c 20 62 65 20 75 73 65 64 20 69 6e 20  will be used in 
35a16 74 77 6f 0a 2a 2a 20 63 75 72 73 6f 72 73 20 61  two.** cursors a
35a17 74 20 6f 6e 63 65 2c 20 65 2e 67 2e 20 20 69 66  t once, e.g.  if
35a18 20 61 20 71 75 65 72 79 20 6a 6f 69 6e 73 20 61   a query joins a
35a19 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 74   virtual table t
35a1a 6f 20 69 74 73 65 6c 66 3f 0a 2a 2a 20 49 66 20  o itself?.** If 
35a1b 73 6f 20 70 65 72 68 61 70 73 20 77 65 20 73 68  so perhaps we sh
35a1c 6f 75 6c 64 20 6d 6f 76 65 20 73 6f 6d 65 20 6f  ould move some o
35a1d 66 20 74 68 65 73 65 20 74 6f 20 74 68 65 20 63  f these to the c
35a1e 75 72 73 6f 72 20 6f 62 6a 65 63 74 2e 0a 2a 2f  ursor object..*/
35a1f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68  .static const ch
35a20 61 72 20 2a 63 6f 6e 73 74 20 66 75 6c 6c 74 65  ar *const fullte
35a21 78 74 5f 7a 53 74 61 74 65 6d 65 6e 74 5b 4d 41  xt_zStatement[MA
35a22 58 5f 53 54 4d 54 5d 20 3d 20 7b 0a 20 20 2f 2a  X_STMT] = {.  /*
35a23 20 43 4f 4e 54 45 4e 54 5f 49 4e 53 45 52 54 20   CONTENT_INSERT 
35a24 2a 2f 20 4e 55 4c 4c 2c 20 20 2f 2a 20 67 65 6e  */ NULL,  /* gen
35a25 65 72 61 74 65 64 20 69 6e 20 63 6f 6e 74 65 6e  erated in conten
35a26 74 49 6e 73 65 72 74 53 74 61 74 65 6d 65 6e 74  tInsertStatement
35a27 28 29 20 2a 2f 0a 20 20 2f 2a 20 43 4f 4e 54 45  () */.  /* CONTE
35a28 4e 54 5f 53 45 4c 45 43 54 20 2a 2f 20 4e 55 4c  NT_SELECT */ NUL
35a29 4c 2c 20 20 2f 2a 20 67 65 6e 65 72 61 74 65 64  L,  /* generated
35a2a 20 69 6e 20 63 6f 6e 74 65 6e 74 53 65 6c 65 63   in contentSelec
35a2b 74 53 74 61 74 65 6d 65 6e 74 28 29 20 2a 2f 0a  tStatement() */.
35a2c 20 20 2f 2a 20 43 4f 4e 54 45 4e 54 5f 55 50 44    /* CONTENT_UPD
35a2d 41 54 45 20 2a 2f 20 4e 55 4c 4c 2c 20 20 2f 2a  ATE */ NULL,  /*
35a2e 20 67 65 6e 65 72 61 74 65 64 20 69 6e 20 63 6f   generated in co
35a2f 6e 74 65 6e 74 55 70 64 61 74 65 53 74 61 74 65  ntentUpdateState
35a30 6d 65 6e 74 28 29 20 2a 2f 0a 20 20 2f 2a 20 43  ment() */.  /* C
35a31 4f 4e 54 45 4e 54 5f 44 45 4c 45 54 45 20 2a 2f  ONTENT_DELETE */
35a32 20 22 64 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f   "delete from %_
35a33 63 6f 6e 74 65 6e 74 20 77 68 65 72 65 20 64 6f  content where do
35a34 63 69 64 20 3d 20 3f 22 2c 0a 20 20 2f 2a 20 43  cid = ?",.  /* C
35a35 4f 4e 54 45 4e 54 5f 45 58 49 53 54 53 20 2a 2f  ONTENT_EXISTS */
35a36 20 22 73 65 6c 65 63 74 20 64 6f 63 69 64 20 66   "select docid f
35a37 72 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74 20 6c 69  rom %_content li
35a38 6d 69 74 20 31 22 2c 0a 0a 20 20 2f 2a 20 42 4c  mit 1",..  /* BL
35a39 4f 43 4b 5f 49 4e 53 45 52 54 20 2a 2f 0a 20 20  OCK_INSERT */.  
35a3a 22 69 6e 73 65 72 74 20 69 6e 74 6f 20 25 5f 73  "insert into %_s
35a3b 65 67 6d 65 6e 74 73 20 28 62 6c 6f 63 6b 69 64  egments (blockid
35a3c 2c 20 62 6c 6f 63 6b 29 20 76 61 6c 75 65 73 20  , block) values 
35a3d 28 6e 75 6c 6c 2c 20 3f 29 22 2c 0a 20 20 2f 2a  (null, ?)",.  /*
35a3e 20 42 4c 4f 43 4b 5f 53 45 4c 45 43 54 20 2a 2f   BLOCK_SELECT */
35a3f 20 22 73 65 6c 65 63 74 20 62 6c 6f 63 6b 20 66   "select block f
35a40 72 6f 6d 20 25 5f 73 65 67 6d 65 6e 74 73 20 77  rom %_segments w
35a41 68 65 72 65 20 62 6c 6f 63 6b 69 64 20 3d 20 3f  here blockid = ?
35a42 22 2c 0a 20 20 2f 2a 20 42 4c 4f 43 4b 5f 44 45  ",.  /* BLOCK_DE
35a43 4c 45 54 45 20 2a 2f 20 22 64 65 6c 65 74 65 20  LETE */ "delete 
35a44 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e 74 73 20  from %_segments 
35a45 77 68 65 72 65 20 62 6c 6f 63 6b 69 64 20 62 65  where blockid be
35a46 74 77 65 65 6e 20 3f 20 61 6e 64 20 3f 22 2c 0a  tween ? and ?",.
35a47 20 20 2f 2a 20 42 4c 4f 43 4b 5f 44 45 4c 45 54    /* BLOCK_DELET
35a48 45 5f 41 4c 4c 20 2a 2f 20 22 64 65 6c 65 74 65  E_ALL */ "delete
35a49 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e 74 73   from %_segments
35a4a 22 2c 0a 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f  ",..  /* SEGDIR_
35a4b 4d 41 58 5f 49 4e 44 45 58 20 2a 2f 20 22 73 65  MAX_INDEX */ "se
35a4c 6c 65 63 74 20 6d 61 78 28 69 64 78 29 20 66 72  lect max(idx) fr
35a4d 6f 6d 20 25 5f 73 65 67 64 69 72 20 77 68 65 72  om %_segdir wher
35a4e 65 20 6c 65 76 65 6c 20 3d 20 3f 22 2c 0a 20 20  e level = ?",.  
35a4f 2f 2a 20 53 45 47 44 49 52 5f 53 45 54 20 2a 2f  /* SEGDIR_SET */
35a50 20 22 69 6e 73 65 72 74 20 69 6e 74 6f 20 25 5f   "insert into %_
35a51 73 65 67 64 69 72 20 76 61 6c 75 65 73 20 28 3f  segdir values (?
35a52 2c 20 3f 2c 20 3f 2c 20 3f 2c 20 3f 2c 20 3f 29  , ?, ?, ?, ?, ?)
35a53 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f 53  ",.  /* SEGDIR_S
35a54 45 4c 45 43 54 5f 4c 45 56 45 4c 20 2a 2f 0a 20  ELECT_LEVEL */. 
35a55 20 22 73 65 6c 65 63 74 20 73 74 61 72 74 5f 62   "select start_b
35a56 6c 6f 63 6b 2c 20 6c 65 61 76 65 73 5f 65 6e 64  lock, leaves_end
35a57 5f 62 6c 6f 63 6b 2c 20 72 6f 6f 74 20 66 72 6f  _block, root fro
35a58 6d 20 25 5f 73 65 67 64 69 72 20 22 0a 20 20 22  m %_segdir ".  "
35a59 20 77 68 65 72 65 20 6c 65 76 65 6c 20 3d 20 3f   where level = ?
35a5a 20 6f 72 64 65 72 20 62 79 20 69 64 78 22 2c 0a   order by idx",.
35a5b 20 20 2f 2a 20 53 45 47 44 49 52 5f 53 50 41 4e    /* SEGDIR_SPAN
35a5c 20 2a 2f 0a 20 20 22 73 65 6c 65 63 74 20 6d 69   */.  "select mi
35a5d 6e 28 73 74 61 72 74 5f 62 6c 6f 63 6b 29 2c 20  n(start_block), 
35a5e 6d 61 78 28 65 6e 64 5f 62 6c 6f 63 6b 29 20 66  max(end_block) f
35a5f 72 6f 6d 20 25 5f 73 65 67 64 69 72 20 22 0a 20  rom %_segdir ". 
35a60 20 22 20 77 68 65 72 65 20 6c 65 76 65 6c 20 3d   " where level =
35a61 20 3f 20 61 6e 64 20 73 74 61 72 74 5f 62 6c 6f   ? and start_blo
35a62 63 6b 20 3c 3e 20 30 22 2c 0a 20 20 2f 2a 20 53  ck <> 0",.  /* S
35a63 45 47 44 49 52 5f 44 45 4c 45 54 45 20 2a 2f 20  EGDIR_DELETE */ 
35a64 22 64 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f 73  "delete from %_s
35a65 65 67 64 69 72 20 77 68 65 72 65 20 6c 65 76 65  egdir where leve
35a66 6c 20 3d 20 3f 22 2c 0a 0a 20 20 2f 2a 20 4e 4f  l = ?",..  /* NO
35a67 54 45 28 73 68 65 73 73 29 3a 20 54 68 65 20 66  TE(shess): The f
35a68 69 72 73 74 20 74 68 72 65 65 20 72 65 73 75 6c  irst three resul
35a69 74 73 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ts of the follow
35a6a 69 6e 67 20 74 77 6f 0a 20 20 2a 2a 20 73 74 61  ing two.  ** sta
35a6b 74 65 6d 65 6e 74 73 20 6d 75 73 74 20 6d 61 74  tements must mat
35a6c 63 68 2e 0a 20 20 2a 2f 0a 20 20 2f 2a 20 53 45  ch..  */.  /* SE
35a6d 47 44 49 52 5f 53 45 4c 45 43 54 5f 53 45 47 4d  GDIR_SELECT_SEGM
35a6e 45 4e 54 20 2a 2f 0a 20 20 22 73 65 6c 65 63 74  ENT */.  "select
35a6f 20 73 74 61 72 74 5f 62 6c 6f 63 6b 2c 20 6c 65   start_block, le
35a70 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20  aves_end_block, 
35a71 72 6f 6f 74 20 66 72 6f 6d 20 25 5f 73 65 67 64  root from %_segd
35a72 69 72 20 22 0a 20 20 22 20 77 68 65 72 65 20 6c  ir ".  " where l
35a73 65 76 65 6c 20 3d 20 3f 20 61 6e 64 20 69 64 78  evel = ? and idx
35a74 20 3d 20 3f 22 2c 0a 20 20 2f 2a 20 53 45 47 44   = ?",.  /* SEGD
35a75 49 52 5f 53 45 4c 45 43 54 5f 41 4c 4c 20 2a 2f  IR_SELECT_ALL */
35a76 0a 20 20 22 73 65 6c 65 63 74 20 73 74 61 72 74  .  "select start
35a77 5f 62 6c 6f 63 6b 2c 20 6c 65 61 76 65 73 5f 65  _block, leaves_e
35a78 6e 64 5f 62 6c 6f 63 6b 2c 20 72 6f 6f 74 20 66  nd_block, root f
35a79 72 6f 6d 20 25 5f 73 65 67 64 69 72 20 22 0a 20  rom %_segdir ". 
35a7a 20 22 20 6f 72 64 65 72 20 62 79 20 6c 65 76 65   " order by leve
35a7b 6c 20 64 65 73 63 2c 20 69 64 78 20 61 73 63 22  l desc, idx asc"
35a7c 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f 44 45  ,.  /* SEGDIR_DE
35a7d 4c 45 54 45 5f 41 4c 4c 20 2a 2f 20 22 64 65 6c  LETE_ALL */ "del
35a7e 65 74 65 20 66 72 6f 6d 20 25 5f 73 65 67 64 69  ete from %_segdi
35a7f 72 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f  r",.  /* SEGDIR_
35a80 43 4f 55 4e 54 20 2a 2f 20 22 73 65 6c 65 63 74  COUNT */ "select
35a81 20 63 6f 75 6e 74 28 2a 29 2c 20 69 66 6e 75 6c   count(*), ifnul
35a82 6c 28 6d 61 78 28 6c 65 76 65 6c 29 2c 30 29 20  l(max(level),0) 
35a83 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 22 2c 0a  from %_segdir",.
35a84 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6e 6e  };../*.** A conn
35a85 65 63 74 69 6f 6e 20 74 6f 20 61 20 66 75 6c 6c  ection to a full
35a86 74 65 78 74 20 69 6e 64 65 78 20 69 73 20 61 6e  text index is an
35a87 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
35a88 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74   following.** st
35a89 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 78 43  ructure.  The xC
35a8a 72 65 61 74 65 20 61 6e 64 20 78 43 6f 6e 6e 65  reate and xConne
35a8b 63 74 20 6d 65 74 68 6f 64 73 20 63 72 65 61 74  ct methods creat
35a8c 65 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a  e an instance.**
35a8d 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75   of this structu
35a8e 72 65 20 61 6e 64 20 78 44 65 73 74 72 6f 79 20  re and xDestroy 
35a8f 61 6e 64 20 78 44 69 73 63 6f 6e 6e 65 63 74 20  and xDisconnect 
35a90 66 72 65 65 20 74 68 61 74 20 69 6e 73 74 61 6e  free that instan
35a91 63 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74 68 65 72  ce..** All other
35a92 20 6d 65 74 68 6f 64 73 20 72 65 63 65 69 76 65   methods receive
35a93 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
35a94 65 20 73 74 72 75 63 74 75 72 65 20 61 73 20 6f  e structure as o
35a95 6e 65 20 6f 66 20 74 68 65 69 72 0a 2a 2a 20 61  ne of their.** a
35a96 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 72  rguments..*/.str
35a97 75 63 74 20 66 75 6c 6c 74 65 78 74 5f 76 74 61  uct fulltext_vta
35a98 62 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  b {.  sqlite3_vt
35a99 61 62 20 62 61 73 65 3b 20 20 20 20 20 20 20 20  ab base;        
35a9a 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 63         /* Base c
35a9b 6c 61 73 73 20 75 73 65 64 20 62 79 20 53 51 4c  lass used by SQL
35a9c 69 74 65 20 63 6f 72 65 20 2a 2f 0a 20 20 73 71  ite core */.  sq
35a9d 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20  lite3 *db;      
35a9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35a9f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
35aa0 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63  onnection */.  c
35aa1 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20  onst char *zDb; 
35aa2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35aa3 2f 2a 20 6c 6f 67 69 63 61 6c 20 64 61 74 61 62  /* logical datab
35aa4 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f  ase name */.  co
35aa5 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  nst char *zName;
35aa6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
35aa7 2a 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  * virtual table 
35aa8 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  name */.  int nC
35aa9 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20  olumn;          
35aaa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 75             /* nu
35aab 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
35aac 69 6e 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  in virtual table
35aad 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43   */.  char **azC
35aae 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20  olumn;          
35aaf 20 20 20 20 20 20 20 2f 2a 20 63 6f 6c 75 6d 6e         /* column
35ab0 20 6e 61 6d 65 73 2e 20 20 6d 61 6c 6c 6f 63 65   names.  malloce
35ab1 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a  d */.  char **az
35ab2 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 3b 20 20  ContentColumn;  
35ab3 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6c 75 6d          /* colum
35ab4 6e 20 6e 61 6d 65 73 20 69 6e 20 63 6f 6e 74 65  n names in conte
35ab5 6e 74 20 74 61 62 6c 65 3b 20 6d 61 6c 6c 6f 63  nt table; malloc
35ab6 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ed */.  sqlite3_
35ab7 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65  tokenizer *pToke
35ab8 6e 69 7a 65 72 3b 20 20 20 2f 2a 20 74 6f 6b 65  nizer;   /* toke
35ab9 6e 69 7a 65 72 20 66 6f 72 20 69 6e 73 65 72 74  nizer for insert
35aba 73 20 61 6e 64 20 71 75 65 72 69 65 73 20 2a 2f  s and queries */
35abb 0a 0a 20 20 2f 2a 20 50 72 65 63 6f 6d 70 69 6c  ..  /* Precompil
35abc 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 77 68  ed statements wh
35abd 69 63 68 20 77 65 20 6b 65 65 70 20 61 73 20 6c  ich we keep as l
35abe 6f 6e 67 20 61 73 20 74 68 65 20 74 61 62 6c 65  ong as the table
35abf 20 69 73 0a 20 20 2a 2a 20 6f 70 65 6e 2e 0a 20   is.  ** open.. 
35ac0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
35ac1 6d 74 20 2a 70 46 75 6c 6c 74 65 78 74 53 74 61  mt *pFulltextSta
35ac2 74 65 6d 65 6e 74 73 5b 4d 41 58 5f 53 54 4d 54  tements[MAX_STMT
35ac3 5d 3b 0a 0a 20 20 2f 2a 20 50 72 65 63 6f 6d 70  ];..  /* Precomp
35ac4 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20  iled statements 
35ac5 75 73 65 64 20 66 6f 72 20 73 65 67 6d 65 6e 74  used for segment
35ac6 20 6d 65 72 67 65 73 2e 20 20 57 65 20 72 75 6e   merges.  We run
35ac7 20 61 0a 20 20 2a 2a 20 73 65 70 61 72 61 74 65   a.  ** separate
35ac8 20 73 65 6c 65 63 74 20 61 63 72 6f 73 73 20 74   select across t
35ac9 68 65 20 6c 65 61 66 20 6c 65 76 65 6c 20 6f 66  he leaf level of
35aca 20 65 61 63 68 20 74 72 65 65 20 62 65 69 6e 67   each tree being
35acb 20 6d 65 72 67 65 64 2e 0a 20 20 2a 2f 0a 20 20   merged..  */.  
35acc 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 4c  sqlite3_stmt *pL
35acd 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73 5b 4d  eafSelectStmts[M
35ace 45 52 47 45 5f 43 4f 55 4e 54 5d 3b 0a 20 20 2f  ERGE_COUNT];.  /
35acf 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20  * The statement 
35ad0 75 73 65 64 20 74 6f 20 70 72 65 70 61 72 65 20  used to prepare 
35ad1 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73  pLeafSelectStmts
35ad2 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 45 41  . */.#define LEA
35ad3 46 5f 53 45 4c 45 43 54 20 5c 0a 20 20 22 73 65  F_SELECT \.  "se
35ad4 6c 65 63 74 20 62 6c 6f 63 6b 20 66 72 6f 6d 20  lect block from 
35ad5 25 5f 73 65 67 6d 65 6e 74 73 20 77 68 65 72 65  %_segments where
35ad6 20 62 6c 6f 63 6b 69 64 20 62 65 74 77 65 65 6e   blockid between
35ad7 20 3f 20 61 6e 64 20 3f 20 6f 72 64 65 72 20 62   ? and ? order b
35ad8 79 20 62 6c 6f 63 6b 69 64 22 0a 0a 20 20 2f 2a  y blockid"..  /*
35ad9 20 54 68 65 73 65 20 62 75 66 66 65 72 20 70 65   These buffer pe
35ada 6e 64 69 6e 67 20 69 6e 64 65 78 20 75 70 64 61  nding index upda
35adb 74 65 73 20 64 75 72 69 6e 67 20 74 72 61 6e 73  tes during trans
35adc 61 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a 20 6e 50  actions..  ** nP
35add 65 6e 64 69 6e 67 44 61 74 61 20 65 73 74 69 6d  endingData estim
35ade 61 74 65 73 20 74 68 65 20 6d 65 6d 6f 72 79 20  ates the memory 
35adf 73 69 7a 65 20 6f 66 20 74 68 65 20 70 65 6e 64  size of the pend
35ae0 69 6e 67 20 64 61 74 61 2e 20 20 49 74 0a 20 20  ing data.  It.  
35ae1 2a 2a 20 64 6f 65 73 6e 27 74 20 69 6e 63 6c 75  ** doesn't inclu
35ae2 64 65 20 74 68 65 20 68 61 73 68 2d 62 75 63 6b  de the hash-buck
35ae3 65 74 20 6f 76 65 72 68 65 61 64 2c 20 6e 6f 72  et overhead, nor
35ae4 20 61 6e 79 20 6d 61 6c 6c 6f 63 0a 20 20 2a 2a   any malloc.  **
35ae5 20 6f 76 65 72 68 65 61 64 2e 20 20 57 68 65 6e   overhead.  When
35ae6 20 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 65 78   nPendingData ex
35ae7 63 65 65 64 73 20 6b 50 65 6e 64 69 6e 67 54 68  ceeds kPendingTh
35ae8 72 65 73 68 6f 6c 64 2c 20 74 68 65 0a 20 20 2a  reshold, the.  *
35ae9 2a 20 62 75 66 66 65 72 20 69 73 20 66 6c 75 73  * buffer is flus
35aea 68 65 64 20 65 76 65 6e 20 62 65 66 6f 72 65 20  hed even before 
35aeb 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
35aec 63 6c 6f 73 65 73 2e 0a 20 20 2a 2a 20 70 65 6e  closes..  ** pen
35aed 64 69 6e 67 54 65 72 6d 73 20 73 74 6f 72 65 73  dingTerms stores
35aee 20 74 68 65 20 64 61 74 61 2c 20 61 6e 64 20 69   the data, and i
35aef 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20 77 68 65  s only valid whe
35af0 6e 20 6e 50 65 6e 64 69 6e 67 44 61 74 61 0a 20  n nPendingData. 
35af1 20 2a 2a 20 69 73 20 3e 3d 30 20 28 6e 50 65 6e   ** is >=0 (nPen
35af2 64 69 6e 67 44 61 74 61 3c 30 20 6d 65 61 6e 73  dingData<0 means
35af3 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 68 61   pendingTerms ha
35af4 73 20 6e 6f 74 20 62 65 65 6e 0a 20 20 2a 2a 20  s not been.  ** 
35af5 69 6e 69 74 69 61 6c 69 7a 65 64 29 2e 20 20 69  initialized).  i
35af6 50 72 65 76 44 6f 63 69 64 20 69 73 20 74 68 65  PrevDocid is the
35af7 20 6c 61 73 74 20 64 6f 63 69 64 20 77 72 69 74   last docid writ
35af8 74 65 6e 2c 20 75 73 65 64 20 74 6f 20 6d 61 6b  ten, used to mak
35af9 65 0a 20 20 2a 2a 20 63 65 72 74 61 69 6e 20 77  e.  ** certain w
35afa 65 27 72 65 20 69 6e 73 65 72 74 69 6e 67 20 69  e're inserting i
35afb 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 0a  n sorted order..
35afc 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 50 65 6e 64    */.  int nPend
35afd 69 6e 67 44 61 74 61 3b 0a 23 64 65 66 69 6e 65  ingData;.#define
35afe 20 6b 50 65 6e 64 69 6e 67 54 68 72 65 73 68 6f   kPendingThresho
35aff 6c 64 20 28 31 2a 31 30 32 34 2a 31 30 32 34 29  ld (1*1024*1024)
35b00 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
35b01 69 50 72 65 76 44 6f 63 69 64 3b 0a 20 20 66 74  iPrevDocid;.  ft
35b02 73 33 48 61 73 68 20 70 65 6e 64 69 6e 67 54 65  s3Hash pendingTe
35b03 72 6d 73 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 57  rms;.};../*.** W
35b04 68 65 6e 20 74 68 65 20 63 6f 72 65 20 77 61 6e  hen the core wan
35b05 74 73 20 74 6f 20 64 6f 20 61 20 71 75 65 72 79  ts to do a query
35b06 2c 20 69 74 20 63 72 65 61 74 65 20 61 20 63 75  , it create a cu
35b07 72 73 6f 72 20 75 73 69 6e 67 20 61 0a 2a 2a 20  rsor using a.** 
35b08 63 61 6c 6c 20 74 6f 20 78 4f 70 65 6e 2e 20 20  call to xOpen.  
35b09 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  This structure i
35b0a 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  s an instance of
35b0b 20 61 20 63 75 72 73 6f 72 2e 20 20 49 74 0a 2a   a cursor.  It.*
35b0c 2a 20 69 73 20 64 65 73 74 72 6f 79 65 64 20 62  * is destroyed b
35b0d 79 20 78 43 6c 6f 73 65 2e 0a 2a 2f 0a 74 79 70  y xClose..*/.typ
35b0e 65 64 65 66 20 73 74 72 75 63 74 20 66 75 6c 6c  edef struct full
35b0f 74 65 78 74 5f 63 75 72 73 6f 72 20 7b 0a 20 20  text_cursor {.  
35b10 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
35b11 73 6f 72 20 62 61 73 65 3b 20 20 20 20 20 20 20  sor base;       
35b12 20 2f 2a 20 42 61 73 65 20 63 6c 61 73 73 20 75   /* Base class u
35b13 73 65 64 20 62 79 20 53 51 4c 69 74 65 20 63 6f  sed by SQLite co
35b14 72 65 20 2a 2f 0a 20 20 51 75 65 72 79 54 79 70  re */.  QueryTyp
35b15 65 20 69 43 75 72 73 6f 72 54 79 70 65 3b 20 20  e iCursorType;  
35b16 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79           /* Copy
35b17 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6e 64 65   of sqlite3_inde
35b18 78 5f 69 6e 66 6f 2e 69 64 78 4e 75 6d 20 2a 2f  x_info.idxNum */
35b19 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
35b1a 2a 70 53 74 6d 74 3b 20 20 20 20 20 20 20 20 20  *pStmt;         
35b1b 20 20 20 20 2f 2a 20 50 72 65 70 61 72 65 64 20      /* Prepared 
35b1c 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 75 73 65  statement in use
35b1d 20 62 79 20 74 68 65 20 63 75 72 73 6f 72 20 2a   by the cursor *
35b1e 2f 0a 20 20 69 6e 74 20 65 6f 66 3b 20 20 20 20  /.  int eof;    
35b1f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35b20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
35b21 61 74 20 45 6e 64 20 4f 66 20 52 65 73 75 6c 74  at End Of Result
35b22 73 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20  s */.  Fts3Expr 
35b23 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20  *pExpr;         
35b24 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65          /* Parse
35b25 64 20 4d 41 54 43 48 20 71 75 65 72 79 20 73 74  d MATCH query st
35b26 72 69 6e 67 20 2a 2f 0a 20 20 53 6e 69 70 70 65  ring */.  Snippe
35b27 74 20 73 6e 69 70 70 65 74 3b 20 20 20 20 20 20  t snippet;      
35b28 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 61             /* Ca
35b29 63 68 65 64 20 73 6e 69 70 70 65 74 20 66 6f 72  ched snippet for
35b2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77   the current row
35b2b 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d   */.  int iColum
35b2c 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
35b2d 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e         /* Column
35b2e 20 62 65 69 6e 67 20 73 65 61 72 63 68 65 64 20   being searched 
35b2f 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72 20  */.  DataBuffer 
35b30 72 65 73 75 6c 74 3b 20 20 20 20 20 20 20 20 20  result;         
35b31 20 20 20 20 20 20 2f 2a 20 44 6f 63 6c 69 73 74        /* Doclist
35b32 20 72 65 73 75 6c 74 73 20 66 72 6f 6d 20 66 75   results from fu
35b33 6c 6c 74 65 78 74 51 75 65 72 79 20 2a 2f 0a 20  lltextQuery */. 
35b34 20 44 4c 52 65 61 64 65 72 20 72 65 61 64 65 72   DLReader reader
35b35 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
35b36 20 20 2f 2a 20 52 65 73 75 6c 74 20 72 65 61 64    /* Result read
35b37 65 72 20 69 66 20 72 65 73 75 6c 74 20 6e 6f 74  er if result not
35b38 20 65 6d 70 74 79 20 2a 2f 0a 7d 20 66 75 6c 6c   empty */.} full
35b39 74 65 78 74 5f 63 75 72 73 6f 72 3b 0a 0a 73 74  text_cursor;..st
35b3a 61 74 69 63 20 66 75 6c 6c 74 65 78 74 5f 76 74  atic fulltext_vt
35b3b 61 62 20 2a 63 75 72 73 6f 72 5f 76 74 61 62 28  ab *cursor_vtab(
35b3c 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20  fulltext_cursor 
35b3d 2a 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 66  *c){.  return (f
35b3e 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 20  ulltext_vtab *) 
35b3f 63 2d 3e 62 61 73 65 2e 70 56 74 61 62 3b 0a 7d  c->base.pVtab;.}
35b40 0a 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73  ..static const s
35b41 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 66 74  qlite3_module ft
35b42 73 33 4d 6f 64 75 6c 65 3b 20 20 20 2f 2a 20 66  s3Module;   /* f
35b43 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
35b44 6f 6e 20 2a 2f 0a 0a 2f 2a 20 52 65 74 75 72 6e  on */../* Return
35b45 20 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 67   a dynamically g
35b46 65 6e 65 72 61 74 65 64 20 73 74 61 74 65 6d 65  enerated stateme
35b47 6e 74 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20  nt of the form. 
35b48 2a 20 20 20 69 6e 73 65 72 74 20 69 6e 74 6f 20  *   insert into 
35b49 25 5f 63 6f 6e 74 65 6e 74 20 28 64 6f 63 69 64  %_content (docid
35b4a 2c 20 2e 2e 2e 29 20 76 61 6c 75 65 73 20 28 3f  , ...) values (?
35b4b 2c 20 2e 2e 2e 29 0a 20 2a 2f 0a 73 74 61 74 69  , ...). */.stati
35b4c 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f  c const char *co
35b4d 6e 74 65 6e 74 49 6e 73 65 72 74 53 74 61 74 65  ntentInsertState
35b4e 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76 74  ment(fulltext_vt
35b4f 61 62 20 2a 76 29 7b 0a 20 20 53 74 72 69 6e 67  ab *v){.  String
35b50 42 75 66 66 65 72 20 73 62 3b 0a 20 20 69 6e 74  Buffer sb;.  int
35b51 20 69 3b 0a 0a 20 20 69 6e 69 74 53 74 72 69 6e   i;..  initStrin
35b52 67 42 75 66 66 65 72 28 26 73 62 29 3b 0a 20 20  gBuffer(&sb);.  
35b53 61 70 70 65 6e 64 28 26 73 62 2c 20 22 69 6e 73  append(&sb, "ins
35b54 65 72 74 20 69 6e 74 6f 20 25 5f 63 6f 6e 74 65  ert into %_conte
35b55 6e 74 20 28 64 6f 63 69 64 2c 20 22 29 3b 0a 20  nt (docid, ");. 
35b56 20 61 70 70 65 6e 64 4c 69 73 74 28 26 73 62 2c   appendList(&sb,
35b57 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76 2d 3e   v->nColumn, v->
35b58 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29  azContentColumn)
35b59 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20  ;.  append(&sb, 
35b5a 22 29 20 76 61 6c 75 65 73 20 28 3f 22 29 3b 0a  ") values (?");.
35b5b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 76 2d 3e    for(i=0; i<v->
35b5c 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 0a 20 20  nColumn; ++i).  
35b5d 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 2c    append(&sb, ",
35b5e 20 3f 22 29 3b 0a 20 20 61 70 70 65 6e 64 28 26   ?");.  append(&
35b5f 73 62 2c 20 22 29 22 29 3b 0a 20 20 72 65 74 75  sb, ")");.  retu
35b60 72 6e 20 73 74 72 69 6e 67 42 75 66 66 65 72 44  rn stringBufferD
35b61 61 74 61 28 26 73 62 29 3b 0a 7d 0a 0a 2f 2a 20  ata(&sb);.}../* 
35b62 52 65 74 75 72 6e 20 61 20 64 79 6e 61 6d 69 63  Return a dynamic
35b63 61 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20 73  ally generated s
35b64 74 61 74 65 6d 65 6e 74 20 6f 66 20 74 68 65 20  tatement of the 
35b65 66 6f 72 6d 0a 20 2a 20 20 20 73 65 6c 65 63 74  form. *   select
35b66 20 3c 63 6f 6e 74 65 6e 74 20 63 6f 6c 75 6d 6e   <content column
35b67 73 3e 20 66 72 6f 6d 20 25 5f 63 6f 6e 74 65 6e  s> from %_conten
35b68 74 20 77 68 65 72 65 20 64 6f 63 69 64 20 3d 20  t where docid = 
35b69 3f 0a 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  ?. */.static con
35b6a 73 74 20 63 68 61 72 20 2a 63 6f 6e 74 65 6e 74  st char *content
35b6b 53 65 6c 65 63 74 53 74 61 74 65 6d 65 6e 74 28  SelectStatement(
35b6c 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
35b6d 29 7b 0a 20 20 53 74 72 69 6e 67 42 75 66 66 65  ){.  StringBuffe
35b6e 72 20 73 62 3b 0a 20 20 69 6e 69 74 53 74 72 69  r sb;.  initStri
35b6f 6e 67 42 75 66 66 65 72 28 26 73 62 29 3b 0a 20  ngBuffer(&sb);. 
35b70 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 53 45   append(&sb, "SE
35b71 4c 45 43 54 20 22 29 3b 0a 20 20 61 70 70 65 6e  LECT ");.  appen
35b72 64 4c 69 73 74 28 26 73 62 2c 20 76 2d 3e 6e 43  dList(&sb, v->nC
35b73 6f 6c 75 6d 6e 2c 20 76 2d 3e 61 7a 43 6f 6e 74  olumn, v->azCont
35b74 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20 61 70  entColumn);.  ap
35b75 70 65 6e 64 28 26 73 62 2c 20 22 20 46 52 4f 4d  pend(&sb, " FROM
35b76 20 25 5f 63 6f 6e 74 65 6e 74 20 57 48 45 52 45   %_content WHERE
35b77 20 64 6f 63 69 64 20 3d 20 3f 22 29 3b 0a 20 20   docid = ?");.  
35b78 72 65 74 75 72 6e 20 73 74 72 69 6e 67 42 75 66  return stringBuf
35b79 66 65 72 44 61 74 61 28 26 73 62 29 3b 0a 7d 0a  ferData(&sb);.}.
35b7a 0a 2f 2a 20 52 65 74 75 72 6e 20 61 20 64 79 6e  ./* Return a dyn
35b7b 61 6d 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74  amically generat
35b7c 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6f 66 20  ed statement of 
35b7d 74 68 65 20 66 6f 72 6d 0a 20 2a 20 20 20 75 70  the form. *   up
35b7e 64 61 74 65 20 25 5f 63 6f 6e 74 65 6e 74 20 73  date %_content s
35b7f 65 74 20 5b 63 6f 6c 5f 30 5d 20 3d 20 3f 2c 20  et [col_0] = ?, 
35b80 5b 63 6f 6c 5f 31 5d 20 3d 20 3f 2c 20 2e 2e 2e  [col_1] = ?, ...
35b81 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  . *             
35b82 20 20 20 20 20 20 20 77 68 65 72 65 20 64 6f 63         where doc
35b83 69 64 20 3d 20 3f 0a 20 2a 2f 0a 73 74 61 74 69  id = ?. */.stati
35b84 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f  c const char *co
35b85 6e 74 65 6e 74 55 70 64 61 74 65 53 74 61 74 65  ntentUpdateState
35b86 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76 74  ment(fulltext_vt
35b87 61 62 20 2a 76 29 7b 0a 20 20 53 74 72 69 6e 67  ab *v){.  String
35b88 42 75 66 66 65 72 20 73 62 3b 0a 20 20 69 6e 74  Buffer sb;.  int
35b89 20 69 3b 0a 0a 20 20 69 6e 69 74 53 74 72 69 6e   i;..  initStrin
35b8a 67 42 75 66 66 65 72 28 26 73 62 29 3b 0a 20 20  gBuffer(&sb);.  
35b8b 61 70 70 65 6e 64 28 26 73 62 2c 20 22 75 70 64  append(&sb, "upd
35b8c 61 74 65 20 25 5f 63 6f 6e 74 65 6e 74 20 73 65  ate %_content se
35b8d 74 20 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  t ");.  for(i=0;
35b8e 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b   i<v->nColumn; +
35b8f 2b 69 29 20 7b 0a 20 20 20 20 69 66 28 20 69 3e  +i) {.    if( i>
35b90 30 20 29 7b 0a 20 20 20 20 20 20 61 70 70 65 6e  0 ){.      appen
35b91 64 28 26 73 62 2c 20 22 2c 20 22 29 3b 0a 20 20  d(&sb, ", ");.  
35b92 20 20 7d 0a 20 20 20 20 61 70 70 65 6e 64 28 26    }.    append(&
35b93 73 62 2c 20 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74  sb, v->azContent
35b94 43 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a 20 20 20 20  Column[i]);.    
35b95 61 70 70 65 6e 64 28 26 73 62 2c 20 22 20 3d 20  append(&sb, " = 
35b96 3f 22 29 3b 0a 20 20 7d 0a 20 20 61 70 70 65 6e  ?");.  }.  appen
35b97 64 28 26 73 62 2c 20 22 20 77 68 65 72 65 20 64  d(&sb, " where d
35b98 6f 63 69 64 20 3d 20 3f 22 29 3b 0a 20 20 72 65  ocid = ?");.  re
35b99 74 75 72 6e 20 73 74 72 69 6e 67 42 75 66 66 65  turn stringBuffe
35b9a 72 44 61 74 61 28 26 73 62 29 3b 0a 7d 0a 0a 2f  rData(&sb);.}../
35b9b 2a 20 50 75 74 73 20 61 20 66 72 65 73 68 6c 79  * Puts a freshly
35b9c 2d 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  -prepared statem
35b9d 65 6e 74 20 64 65 74 65 72 6d 69 6e 65 64 20 62  ent determined b
35b9e 79 20 69 53 74 6d 74 20 69 6e 20 2a 70 70 53 74  y iStmt in *ppSt
35b9f 6d 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e  mt..** If the in
35ba0 64 69 63 61 74 65 64 20 73 74 61 74 65 6d 65 6e  dicated statemen
35ba1 74 20 68 61 73 20 6e 65 76 65 72 20 62 65 65 6e  t has never been
35ba2 20 70 72 65 70 61 72 65 64 2c 20 69 74 20 69 73   prepared, it is
35ba3 20 70 72 65 70 61 72 65 64 0a 2a 2a 20 61 6e 64   prepared.** and
35ba4 20 63 61 63 68 65 64 2c 20 6f 74 68 65 72 77 69   cached, otherwi
35ba5 73 65 20 74 68 65 20 63 61 63 68 65 64 20 76 65  se the cached ve
35ba6 72 73 69 6f 6e 20 69 73 20 72 65 73 65 74 2e 0a  rsion is reset..
35ba7 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71  */.static int sq
35ba8 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28  l_get_statement(
35ba9 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
35baa 2c 20 66 75 6c 6c 74 65 78 74 5f 73 74 61 74 65  , fulltext_state
35bab 6d 65 6e 74 20 69 53 74 6d 74 2c 0a 20 20 20 20  ment iStmt,.    
35bac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35bad 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
35bae 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 29 7b  _stmt **ppStmt){
35baf 0a 20 20 61 73 73 65 72 74 28 20 69 53 74 6d 74  .  assert( iStmt
35bb0 3c 4d 41 58 5f 53 54 4d 54 20 29 3b 0a 20 20 69  <MAX_STMT );.  i
35bb1 66 28 20 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53  f( v->pFulltextS
35bb2 74 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d 74 5d  tatements[iStmt]
35bb3 3d 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 63 6f  ==NULL ){.    co
35bb4 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b  nst char *zStmt;
35bb5 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20  .    int rc;.   
35bb6 20 73 77 69 74 63 68 28 20 69 53 74 6d 74 20 29   switch( iStmt )
35bb7 7b 0a 20 20 20 20 20 20 63 61 73 65 20 43 4f 4e  {.      case CON
35bb8 54 45 4e 54 5f 49 4e 53 45 52 54 5f 53 54 4d 54  TENT_INSERT_STMT
35bb9 3a 0a 20 20 20 20 20 20 20 20 7a 53 74 6d 74 20  :.        zStmt 
35bba 3d 20 63 6f 6e 74 65 6e 74 49 6e 73 65 72 74 53  = contentInsertS
35bbb 74 61 74 65 6d 65 6e 74 28 76 29 3b 20 62 72 65  tatement(v); bre
35bbc 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 43  ak;.      case C
35bbd 4f 4e 54 45 4e 54 5f 53 45 4c 45 43 54 5f 53 54  ONTENT_SELECT_ST
35bbe 4d 54 3a 0a 20 20 20 20 20 20 20 20 7a 53 74 6d  MT:.        zStm
35bbf 74 20 3d 20 63 6f 6e 74 65 6e 74 53 65 6c 65 63  t = contentSelec
35bc0 74 53 74 61 74 65 6d 65 6e 74 28 76 29 3b 20 62  tStatement(v); b
35bc1 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
35bc2 20 43 4f 4e 54 45 4e 54 5f 55 50 44 41 54 45 5f   CONTENT_UPDATE_
35bc3 53 54 4d 54 3a 0a 20 20 20 20 20 20 20 20 7a 53  STMT:.        zS
35bc4 74 6d 74 20 3d 20 63 6f 6e 74 65 6e 74 55 70 64  tmt = contentUpd
35bc5 61 74 65 53 74 61 74 65 6d 65 6e 74 28 76 29 3b  ateStatement(v);
35bc6 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 64 65   break;.      de
35bc7 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 7a  fault:.        z
35bc8 53 74 6d 74 20 3d 20 66 75 6c 6c 74 65 78 74 5f  Stmt = fulltext_
35bc9 7a 53 74 61 74 65 6d 65 6e 74 5b 69 53 74 6d 74  zStatement[iStmt
35bca 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20  ];.    }.    rc 
35bcb 3d 20 73 71 6c 5f 70 72 65 70 61 72 65 28 76 2d  = sql_prepare(v-
35bcc 3e 64 62 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e  >db, v->zDb, v->
35bcd 7a 4e 61 6d 65 2c 20 26 76 2d 3e 70 46 75 6c 6c  zName, &v->pFull
35bce 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73 5b 69  textStatements[i
35bcf 53 74 6d 74 5d 2c 0a 20 20 20 20 20 20 20 20 20  Stmt],.         
35bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35bd1 7a 53 74 6d 74 29 3b 0a 20 20 20 20 69 66 28 20  zStmt);.    if( 
35bd2 7a 53 74 6d 74 20 21 3d 20 66 75 6c 6c 74 65 78  zStmt != fulltex
35bd3 74 5f 7a 53 74 61 74 65 6d 65 6e 74 5b 69 53 74  t_zStatement[iSt
35bd4 6d 74 5d 29 20 73 71 6c 69 74 65 33 5f 66 72 65  mt]) sqlite3_fre
35bd5 65 28 28 76 6f 69 64 20 2a 29 20 7a 53 74 6d 74  e((void *) zStmt
35bd6 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  );.    if( rc!=S
35bd7 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
35bd8 6e 20 72 63 3b 0a 20 20 7d 20 65 6c 73 65 20 7b  n rc;.  } else {
35bd9 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71  .    int rc = sq
35bda 6c 69 74 65 33 5f 72 65 73 65 74 28 76 2d 3e 70  lite3_reset(v->p
35bdb 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e  FulltextStatemen
35bdc 74 73 5b 69 53 74 6d 74 5d 29 3b 0a 20 20 20 20  ts[iStmt]);.    
35bdd 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
35bde 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
35bdf 20 7d 0a 0a 20 20 2a 70 70 53 74 6d 74 20 3d 20   }..  *ppStmt = 
35be0 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74  v->pFulltextStat
35be1 65 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 3b 0a 20  ements[iStmt];. 
35be2 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
35be3 4b 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b 65 20 73 71  K;.}../* Like sq
35be4 6c 69 74 65 33 5f 73 74 65 70 28 29 2c 20 62 75  lite3_step(), bu
35be5 74 20 63 6f 6e 76 65 72 74 20 53 51 4c 49 54 45  t convert SQLITE
35be6 5f 44 4f 4e 45 20 74 6f 20 53 51 4c 49 54 45 5f  _DONE to SQLITE_
35be7 4f 4b 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45  OK and.** SQLITE
35be8 5f 52 4f 57 20 74 6f 20 53 51 4c 49 54 45 5f 45  _ROW to SQLITE_E
35be9 52 52 4f 52 2e 20 20 55 73 65 66 75 6c 20 66 6f  RROR.  Useful fo
35bea 72 20 73 74 61 74 65 6d 65 6e 74 73 20 6c 69 6b  r statements lik
35beb 65 20 55 50 44 41 54 45 2c 0a 2a 2a 20 77 68 65  e UPDATE,.** whe
35bec 72 65 20 77 65 20 65 78 70 65 63 74 20 6e 6f 20  re we expect no 
35bed 72 65 73 75 6c 74 73 2e 0a 2a 2f 0a 73 74 61 74  results..*/.stat
35bee 69 63 20 69 6e 74 20 73 71 6c 5f 73 69 6e 67 6c  ic int sql_singl
35bef 65 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73  e_step(sqlite3_s
35bf0 74 6d 74 20 2a 73 29 7b 0a 20 20 69 6e 74 20 72  tmt *s){.  int r
35bf1 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
35bf2 28 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 72  (s);.  return (r
35bf3 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 29 20  c==SQLITE_DONE) 
35bf4 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 72 63  ? SQLITE_OK : rc
35bf5 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b 65 20 73 71 6c  ;.}../* Like sql
35bf6 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 29  _get_statement()
35bf7 2c 20 62 75 74 20 66 6f 72 20 73 70 65 63 69 61  , but for specia
35bf8 6c 20 72 65 70 6c 69 63 61 74 65 64 20 4c 45 41  l replicated LEA
35bf9 46 5f 53 45 4c 45 43 54 0a 2a 2a 20 73 74 61 74  F_SELECT.** stat
35bfa 65 6d 65 6e 74 73 2e 20 20 69 64 78 20 2d 31 20  ements.  idx -1 
35bfb 69 73 20 61 20 73 70 65 63 69 61 6c 20 63 61 73  is a special cas
35bfc 65 20 66 6f 72 20 61 6e 20 75 6e 63 61 63 68 65  e for an uncache
35bfd 64 20 76 65 72 73 69 6f 6e 20 6f 66 0a 2a 2a 20  d version of.** 
35bfe 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 28 75  the statement (u
35bff 73 65 64 20 69 6e 20 74 68 65 20 6f 70 74 69 6d  sed in the optim
35c00 69 7a 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ize implementati
35c01 6f 6e 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28  on)..*/./* TODO(
35c02 73 68 65 73 73 29 20 57 72 69 74 65 20 76 65 72  shess) Write ver
35c03 73 69 6f 6e 20 66 6f 72 20 67 65 6e 65 72 69 63  sion for generic
35c04 20 73 74 61 74 65 6d 65 6e 74 73 20 61 6e 64 20   statements and 
35c05 74 68 65 6e 20 73 68 61 72 65 0a 2a 2a 20 74 68  then share.** th
35c06 61 74 20 62 65 74 77 65 65 6e 20 74 68 65 20 63  at between the c
35c07 61 63 68 65 64 2d 73 74 61 74 65 6d 65 6e 74 20  ached-statement 
35c08 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74  functions..*/.st
35c09 61 74 69 63 20 69 6e 74 20 73 71 6c 5f 67 65 74  atic int sql_get
35c0a 5f 6c 65 61 66 5f 73 74 61 74 65 6d 65 6e 74 28  _leaf_statement(
35c0b 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
35c0c 2c 20 69 6e 74 20 69 64 78 2c 0a 20 20 20 20 20  , int idx,.     
35c0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c0e 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
35c0f 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74  ite3_stmt **ppSt
35c10 6d 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69  mt){.  assert( i
35c11 64 78 3e 3d 2d 31 20 26 26 20 69 64 78 3c 4d 45  dx>=-1 && idx<ME
35c12 52 47 45 5f 43 4f 55 4e 54 20 29 3b 0a 20 20 69  RGE_COUNT );.  i
35c13 66 28 20 69 64 78 3d 3d 2d 31 20 29 7b 0a 20 20  f( idx==-1 ){.  
35c14 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 70 72 65    return sql_pre
35c15 70 61 72 65 28 76 2d 3e 64 62 2c 20 76 2d 3e 7a  pare(v->db, v->z
35c16 44 62 2c 20 76 2d 3e 7a 4e 61 6d 65 2c 20 70 70  Db, v->zName, pp
35c17 53 74 6d 74 2c 20 4c 45 41 46 5f 53 45 4c 45 43  Stmt, LEAF_SELEC
35c18 54 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  T);.  }else if( 
35c19 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74 53 74  v->pLeafSelectSt
35c1a 6d 74 73 5b 69 64 78 5d 3d 3d 4e 55 4c 4c 20 29  mts[idx]==NULL )
35c1b 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73  {.    int rc = s
35c1c 71 6c 5f 70 72 65 70 61 72 65 28 76 2d 3e 64 62  ql_prepare(v->db
35c1d 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61  , v->zDb, v->zNa
35c1e 6d 65 2c 20 26 76 2d 3e 70 4c 65 61 66 53 65 6c  me, &v->pLeafSel
35c1f 65 63 74 53 74 6d 74 73 5b 69 64 78 5d 2c 0a 20  ectStmts[idx],. 
35c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c21 20 20 20 20 20 20 20 20 4c 45 41 46 5f 53 45 4c          LEAF_SEL
35c22 45 43 54 29 3b 0a 20 20 20 20 69 66 28 20 72 63  ECT);.    if( rc
35c23 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
35c24 74 75 72 6e 20 72 63 3b 0a 20 20 7d 65 6c 73 65  turn rc;.  }else
35c25 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73  {.    int rc = s
35c26 71 6c 69 74 65 33 5f 72 65 73 65 74 28 76 2d 3e  qlite3_reset(v->
35c27 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73  pLeafSelectStmts
35c28 5b 69 64 78 5d 29 3b 0a 20 20 20 20 69 66 28 20  [idx]);.    if( 
35c29 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
35c2a 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a  return rc;.  }..
35c2b 20 20 2a 70 70 53 74 6d 74 20 3d 20 76 2d 3e 70    *ppStmt = v->p
35c2c 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73 5b  LeafSelectStmts[
35c2d 69 64 78 5d 3b 0a 20 20 72 65 74 75 72 6e 20 53  idx];.  return S
35c2e 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20  QLITE_OK;.}../* 
35c2f 69 6e 73 65 72 74 20 69 6e 74 6f 20 25 5f 63 6f  insert into %_co
35c30 6e 74 65 6e 74 20 28 64 6f 63 69 64 2c 20 2e 2e  ntent (docid, ..
35c31 2e 29 20 76 61 6c 75 65 73 20 28 5b 64 6f 63 69  .) values ([doci
35c32 64 5d 2c 20 5b 70 56 61 6c 75 65 73 5d 29 0a 2a  d], [pValues]).*
35c33 2a 20 49 66 20 74 68 65 20 64 6f 63 69 64 20 63  * If the docid c
35c34 6f 6e 74 61 69 6e 73 20 53 51 4c 20 4e 55 4c 4c  ontains SQL NULL
35c35 2c 20 74 68 65 6e 20 61 20 75 6e 69 71 75 65 20  , then a unique 
35c36 64 6f 63 69 64 20 77 69 6c 6c 20 62 65 0a 2a 2a  docid will be.**
35c37 20 67 65 6e 65 72 61 74 65 64 2e 0a 2a 2f 0a 73   generated..*/.s
35c38 74 61 74 69 63 20 69 6e 74 20 63 6f 6e 74 65 6e  tatic int conten
35c39 74 5f 69 6e 73 65 72 74 28 66 75 6c 6c 74 65 78  t_insert(fulltex
35c3a 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74  t_vtab *v, sqlit
35c3b 65 33 5f 76 61 6c 75 65 20 2a 64 6f 63 69 64 2c  e3_value *docid,
35c3c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
35c3d 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
35c3e 65 33 5f 76 61 6c 75 65 20 2a 2a 70 56 61 6c 75  e3_value **pValu
35c3f 65 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73  es){.  sqlite3_s
35c40 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 69 3b  tmt *s;.  int i;
35c41 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f  .  int rc = sql_
35c42 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c  get_statement(v,
35c43 20 43 4f 4e 54 45 4e 54 5f 49 4e 53 45 52 54 5f   CONTENT_INSERT_
35c44 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28  STMT, &s);.  if(
35c45 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
35c46 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
35c47 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  c = sqlite3_bind
35c48 5f 76 61 6c 75 65 28 73 2c 20 31 2c 20 64 6f 63  _value(s, 1, doc
35c49 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  id);.  if( rc!=S
35c4a 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
35c4b 6e 20 72 63 3b 0a 0a 20 20 66 6f 72 28 69 3d 30  n rc;..  for(i=0
35c4c 3b 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  ; i<v->nColumn; 
35c4d 2b 2b 69 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  ++i){.    rc = s
35c4e 71 6c 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75  qlite3_bind_valu
35c4f 65 28 73 2c 20 32 2b 69 2c 20 70 56 61 6c 75 65  e(s, 2+i, pValue
35c50 73 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 72  s[i]);.    if( r
35c51 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
35c52 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
35c53 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67   return sql_sing
35c54 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f  le_step(s);.}../
35c55 2a 20 75 70 64 61 74 65 20 25 5f 63 6f 6e 74 65  * update %_conte
35c56 6e 74 20 73 65 74 20 63 6f 6c 30 20 3d 20 70 56  nt set col0 = pV
35c57 61 6c 75 65 73 5b 30 5d 2c 20 63 6f 6c 31 20 3d  alues[0], col1 =
35c58 20 70 56 61 6c 75 65 73 5b 31 5d 2c 20 2e 2e 2e   pValues[1], ...
35c59 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  . *             
35c5a 20 20 20 20 20 77 68 65 72 65 20 64 6f 63 69 64       where docid
35c5b 20 3d 20 5b 69 44 6f 63 69 64 5d 20 2a 2f 0a 73   = [iDocid] */.s
35c5c 74 61 74 69 63 20 69 6e 74 20 63 6f 6e 74 65 6e  tatic int conten
35c5d 74 5f 75 70 64 61 74 65 28 66 75 6c 6c 74 65 78  t_update(fulltex
35c5e 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74  t_vtab *v, sqlit
35c5f 65 33 5f 76 61 6c 75 65 20 2a 2a 70 56 61 6c 75  e3_value **pValu
35c60 65 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  es,.            
35c61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
35c62 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69  lite_int64 iDoci
35c63 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  d){.  sqlite3_st
35c64 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 69 3b 0a  mt *s;.  int i;.
35c65 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67    int rc = sql_g
35c66 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
35c67 43 4f 4e 54 45 4e 54 5f 55 50 44 41 54 45 5f 53  CONTENT_UPDATE_S
35c68 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20  TMT, &s);.  if( 
35c69 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
35c6a 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 66 6f  return rc;..  fo
35c6b 72 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c  r(i=0; i<v->nCol
35c6c 75 6d 6e 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 72  umn; ++i){.    r
35c6d 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  c = sqlite3_bind
35c6e 5f 76 61 6c 75 65 28 73 2c 20 31 2b 69 2c 20 70  _value(s, 1+i, p
35c6f 56 61 6c 75 65 73 5b 69 5d 29 3b 0a 20 20 20 20  Values[i]);.    
35c70 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
35c71 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
35c72 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   }..  rc = sqlit
35c73 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c  e3_bind_int64(s,
35c74 20 31 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 69   1+v->nColumn, i
35c75 44 6f 63 69 64 29 3b 0a 20 20 69 66 28 20 72 63  Docid);.  if( rc
35c76 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
35c77 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75  turn rc;..  retu
35c78 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74  rn sql_single_st
35c79 65 70 28 73 29 3b 0a 7d 0a 0a 73 74 61 74 69 63  ep(s);.}..static
35c7a 20 76 6f 69 64 20 66 72 65 65 53 74 72 69 6e 67   void freeString
35c7b 41 72 72 61 79 28 69 6e 74 20 6e 53 74 72 69 6e  Array(int nStrin
35c7c 67 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  g, const char **
35c7d 70 53 74 72 69 6e 67 29 7b 0a 20 20 69 6e 74 20  pString){.  int 
35c7e 69 3b 0a 0a 20 20 66 6f 72 20 28 69 3d 30 20 3b  i;..  for (i=0 ;
35c7f 20 69 20 3c 20 6e 53 74 72 69 6e 67 20 3b 20 2b   i < nString ; +
35c80 2b 69 29 20 7b 0a 20 20 20 20 69 66 28 20 70 53  +i) {.    if( pS
35c81 74 72 69 6e 67 5b 69 5d 21 3d 4e 55 4c 4c 20 29  tring[i]!=NULL )
35c82 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 28 76   sqlite3_free((v
35c83 6f 69 64 20 2a 29 20 70 53 74 72 69 6e 67 5b 69  oid *) pString[i
35c84 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ]);.  }.  sqlite
35c85 33 5f 66 72 65 65 28 28 76 6f 69 64 20 2a 29 20  3_free((void *) 
35c86 70 53 74 72 69 6e 67 29 3b 0a 7d 0a 0a 2f 2a 20  pString);.}../* 
35c87 73 65 6c 65 63 74 20 2a 20 66 72 6f 6d 20 25 5f  select * from %_
35c88 63 6f 6e 74 65 6e 74 20 77 68 65 72 65 20 64 6f  content where do
35c89 63 69 64 20 3d 20 5b 69 44 6f 63 69 64 5d 0a 20  cid = [iDocid]. 
35c8a 2a 20 54 68 65 20 63 61 6c 6c 65 72 20 6d 75 73  * The caller mus
35c8b 74 20 64 65 6c 65 74 65 20 74 68 65 20 72 65 74  t delete the ret
35c8c 75 72 6e 65 64 20 61 72 72 61 79 20 61 6e 64 20  urned array and 
35c8d 61 6c 6c 20 73 74 72 69 6e 67 73 20 69 6e 20 69  all strings in i
35c8e 74 2e 0a 20 2a 20 6e 75 6c 6c 20 66 69 65 6c 64  t.. * null field
35c8f 73 20 77 69 6c 6c 20 62 65 20 4e 55 4c 4c 20 69  s will be NULL i
35c90 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 61  n the returned a
35c91 72 72 61 79 2e 0a 20 2a 0a 20 2a 20 54 4f 44 4f  rray.. *. * TODO
35c92 3a 20 50 65 72 68 61 70 73 20 77 65 20 73 68 6f  : Perhaps we sho
35c93 75 6c 64 20 72 65 74 75 72 6e 20 70 6f 69 6e 74  uld return point
35c94 65 72 2f 6c 65 6e 67 74 68 20 73 74 72 69 6e 67  er/length string
35c95 73 20 68 65 72 65 20 66 6f 72 20 63 6f 6e 73 69  s here for consi
35c96 73 74 65 6e 63 79 0a 20 2a 20 77 69 74 68 20 6f  stency. * with o
35c97 74 68 65 72 20 63 6f 64 65 20 77 68 69 63 68 20  ther code which 
35c98 75 73 65 73 20 70 6f 69 6e 74 65 72 2f 6c 65 6e  uses pointer/len
35c99 67 74 68 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  gth. */.static i
35c9a 6e 74 20 63 6f 6e 74 65 6e 74 5f 73 65 6c 65 63  nt content_selec
35c9b 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  t(fulltext_vtab 
35c9c 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  *v, sqlite_int64
35c9d 20 69 44 6f 63 69 64 2c 0a 20 20 20 20 20 20 20   iDocid,.       
35c9e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35c9f 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a     const char **
35ca0 2a 70 56 61 6c 75 65 73 29 7b 0a 20 20 73 71 6c  *pValues){.  sql
35ca1 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20  ite3_stmt *s;.  
35ca2 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 76 61 6c  const char **val
35ca3 75 65 73 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  ues;.  int i;.  
35ca4 69 6e 74 20 72 63 3b 0a 0a 20 20 2a 70 56 61 6c  int rc;..  *pVal
35ca5 75 65 73 20 3d 20 4e 55 4c 4c 3b 0a 0a 20 20 72  ues = NULL;..  r
35ca6 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74  c = sql_get_stat
35ca7 65 6d 65 6e 74 28 76 2c 20 43 4f 4e 54 45 4e 54  ement(v, CONTENT
35ca8 5f 53 45 4c 45 43 54 5f 53 54 4d 54 2c 20 26 73  _SELECT_STMT, &s
35ca9 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
35caa 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
35cab 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  rc;..  rc = sqli
35cac 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73  te3_bind_int64(s
35cad 2c 20 31 2c 20 69 44 6f 63 69 64 29 3b 0a 20 20  , 1, iDocid);.  
35cae 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
35caf 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
35cb0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73    rc = sqlite3_s
35cb1 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63  tep(s);.  if( rc
35cb2 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72  !=SQLITE_ROW ) r
35cb3 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 76 61 6c  eturn rc;..  val
35cb4 75 65 73 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  ues = (const cha
35cb5 72 20 2a 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61  r **) sqlite3_ma
35cb6 6c 6c 6f 63 28 76 2d 3e 6e 43 6f 6c 75 6d 6e 20  lloc(v->nColumn 
35cb7 2a 20 73 69 7a 65 6f 66 28 63 6f 6e 73 74 20 63  * sizeof(const c
35cb8 68 61 72 20 2a 29 29 3b 0a 20 20 66 6f 72 28 69  har *));.  for(i
35cb9 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e  =0; i<v->nColumn
35cba 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 69 66 28 20  ; ++i){.    if( 
35cbb 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
35cbc 79 70 65 28 73 2c 20 69 29 3d 3d 53 51 4c 49 54  ype(s, i)==SQLIT
35cbd 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20  E_NULL ){.      
35cbe 76 61 6c 75 65 73 5b 69 5d 20 3d 20 4e 55 4c 4c  values[i] = NULL
35cbf 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
35cc0 20 20 20 76 61 6c 75 65 73 5b 69 5d 20 3d 20 73     values[i] = s
35cc1 74 72 69 6e 67 5f 64 75 70 28 28 63 68 61 72 2a  tring_dup((char*
35cc2 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  )sqlite3_column_
35cc3 74 65 78 74 28 73 2c 20 69 29 29 3b 0a 20 20 20  text(s, i));.   
35cc4 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 20   }.  }..  /* We 
35cc5 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20  expect only one 
35cc6 72 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65 78  row.  We must ex
35cc7 65 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73 71  ecute another sq
35cc8 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20 20  lite3_step().   
35cc9 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68  * to complete th
35cca 65 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68  e iteration; oth
35ccb 65 72 77 69 73 65 20 74 68 65 20 74 61 62 6c 65  erwise the table
35ccc 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f 63   will remain loc
35ccd 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73  ked. */.  rc = s
35cce 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a  qlite3_step(s);.
35ccf 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
35cd0 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 2a 70 56  _DONE ){.    *pV
35cd1 61 6c 75 65 73 20 3d 20 76 61 6c 75 65 73 3b 0a  alues = values;.
35cd2 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
35cd3 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 66 72 65  E_OK;.  }..  fre
35cd4 65 53 74 72 69 6e 67 41 72 72 61 79 28 76 2d 3e  eStringArray(v->
35cd5 6e 43 6f 6c 75 6d 6e 2c 20 76 61 6c 75 65 73 29  nColumn, values)
35cd6 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
35cd7 0a 0a 2f 2a 20 64 65 6c 65 74 65 20 66 72 6f 6d  ../* delete from
35cd8 20 25 5f 63 6f 6e 74 65 6e 74 20 77 68 65 72 65   %_content where
35cd9 20 64 6f 63 69 64 20 3d 20 5b 69 44 6f 63 69 64   docid = [iDocid
35cda 20 5d 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74   ] */.static int
35cdb 20 63 6f 6e 74 65 6e 74 5f 64 65 6c 65 74 65 28   content_delete(
35cdc 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
35cdd 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  , sqlite_int64 i
35cde 44 6f 63 69 64 29 7b 0a 20 20 73 71 6c 69 74 65  Docid){.  sqlite
35cdf 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74  3_stmt *s;.  int
35ce0 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
35ce1 61 74 65 6d 65 6e 74 28 76 2c 20 43 4f 4e 54 45  atement(v, CONTE
35ce2 4e 54 5f 44 45 4c 45 54 45 5f 53 54 4d 54 2c 20  NT_DELETE_STMT, 
35ce3 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &s);.  if( rc!=S
35ce4 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
35ce5 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
35ce6 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
35ce7 28 73 2c 20 31 2c 20 69 44 6f 63 69 64 29 3b 0a  (s, 1, iDocid);.
35ce8 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
35ce9 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
35cea 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73  ..  return sql_s
35ceb 69 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d  ingle_step(s);.}
35cec 0a 0a 2f 2a 20 52 65 74 75 72 6e 73 20 53 51 4c  ../* Returns SQL
35ced 49 54 45 5f 52 4f 57 20 69 66 20 61 6e 79 20 72  ITE_ROW if any r
35cee 6f 77 73 20 65 78 69 73 74 20 69 6e 20 25 5f 63  ows exist in %_c
35cef 6f 6e 74 65 6e 74 2c 20 53 51 4c 49 54 45 5f 44  ontent, SQLITE_D
35cf0 4f 4e 45 20 69 66 0a 2a 2a 20 6e 6f 20 72 6f 77  ONE if.** no row
35cf1 73 20 65 78 69 73 74 2c 20 61 6e 64 20 61 6e 79  s exist, and any
35cf2 20 65 72 72 6f 72 20 69 6e 20 63 61 73 65 20 6f   error in case o
35cf3 66 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74  f failure..*/.st
35cf4 61 74 69 63 20 69 6e 74 20 63 6f 6e 74 65 6e 74  atic int content
35cf5 5f 65 78 69 73 74 73 28 66 75 6c 6c 74 65 78 74  _exists(fulltext
35cf6 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 73 71 6c  _vtab *v){.  sql
35cf7 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20  ite3_stmt *s;.  
35cf8 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74  int rc = sql_get
35cf9 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 43 4f  _statement(v, CO
35cfa 4e 54 45 4e 54 5f 45 58 49 53 54 53 5f 53 54 4d  NTENT_EXISTS_STM
35cfb 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63  T, &s);.  if( rc
35cfc 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
35cfd 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
35cfe 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
35cff 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
35d00 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20  TE_ROW ) return 
35d01 72 63 3b 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70  rc;..  /* We exp
35d02 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77  ect only one row
35d03 2e 20 20 57 65 20 6d 75 73 74 20 65 78 65 63 75  .  We must execu
35d04 74 65 20 61 6e 6f 74 68 65 72 20 73 71 6c 69 74  te another sqlit
35d05 65 33 5f 73 74 65 70 28 29 0a 20 20 20 2a 20 74  e3_step().   * t
35d06 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 69  o complete the i
35d07 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77  teration; otherw
35d08 69 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69  ise the table wi
35d09 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f 63 6b 65 64  ll remain locked
35d0a 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69  . */.  rc = sqli
35d0b 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69  te3_step(s);.  i
35d0c 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  f( rc==SQLITE_DO
35d0d 4e 45 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  NE ) return SQLI
35d0e 54 45 5f 52 4f 57 3b 0a 20 20 69 66 28 20 72 63  TE_ROW;.  if( rc
35d0f 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72  ==SQLITE_ROW ) r
35d10 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
35d11 4f 52 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  OR;.  return rc;
35d12 0a 7d 0a 0a 2f 2a 20 69 6e 73 65 72 74 20 69 6e  .}../* insert in
35d13 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 20 76 61  to %_segments va
35d14 6c 75 65 73 20 28 5b 70 44 61 74 61 5d 29 0a 2a  lues ([pData]).*
35d15 2a 20 20 20 72 65 74 75 72 6e 73 20 61 73 73 69  *   returns assi
35d16 67 6e 65 64 20 62 6c 6f 63 6b 69 64 20 69 6e 20  gned blockid in 
35d17 2a 70 69 42 6c 6f 63 6b 69 64 0a 2a 2f 0a 73 74  *piBlockid.*/.st
35d18 61 74 69 63 20 69 6e 74 20 62 6c 6f 63 6b 5f 69  atic int block_i
35d19 6e 73 65 72 74 28 66 75 6c 6c 74 65 78 74 5f 76  nsert(fulltext_v
35d1a 74 61 62 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68  tab *v, const ch
35d1b 61 72 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e  ar *pData, int n
35d1c 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20  Data,.          
35d1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
35d1e 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69 42 6c  lite_int64 *piBl
35d1f 6f 63 6b 69 64 29 7b 0a 20 20 73 71 6c 69 74 65  ockid){.  sqlite
35d20 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74  3_stmt *s;.  int
35d21 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
35d22 61 74 65 6d 65 6e 74 28 76 2c 20 42 4c 4f 43 4b  atement(v, BLOCK
35d23 5f 49 4e 53 45 52 54 5f 53 54 4d 54 2c 20 26 73  _INSERT_STMT, &s
35d24 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
35d25 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
35d26 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  rc;..  rc = sqli
35d27 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 73 2c  te3_bind_blob(s,
35d28 20 31 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61   1, pData, nData
35d29 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
35d2a 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
35d2b 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
35d2c 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
35d2d 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66  e3_step(s);.  if
35d2e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc==SQLITE_ROW
35d2f 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
35d30 5f 45 52 52 4f 52 3b 0a 20 20 69 66 28 20 72 63  _ERROR;.  if( rc
35d31 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 20  !=SQLITE_DONE ) 
35d32 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a  return rc;..  /*
35d33 20 62 6c 6f 63 6b 69 64 20 63 6f 6c 75 6d 6e 20   blockid column 
35d34 69 73 20 61 6e 20 61 6c 69 61 73 20 66 6f 72 20  is an alias for 
35d35 72 6f 77 69 64 2e 20 2a 2f 0a 20 20 2a 70 69 42  rowid. */.  *piB
35d36 6c 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74 65 33  lockid = sqlite3
35d37 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77  _last_insert_row
35d38 69 64 28 76 2d 3e 64 62 29 3b 0a 20 20 72 65 74  id(v->db);.  ret
35d39 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
35d3a 0a 0a 2f 2a 20 64 65 6c 65 74 65 20 66 72 6f 6d  ../* delete from
35d3b 20 25 5f 73 65 67 6d 65 6e 74 73 0a 2a 2a 20 20   %_segments.**  
35d3c 20 77 68 65 72 65 20 62 6c 6f 63 6b 69 64 20 62   where blockid b
35d3d 65 74 77 65 65 6e 20 5b 69 53 74 61 72 74 42 6c  etween [iStartBl
35d3e 6f 63 6b 69 64 5d 20 61 6e 64 20 5b 69 45 6e 64  ockid] and [iEnd
35d3f 42 6c 6f 63 6b 69 64 5d 0a 2a 2a 0a 2a 2a 20 44  Blockid].**.** D
35d40 65 6c 65 74 65 73 20 74 68 65 20 72 61 6e 67 65  eletes the range
35d41 20 6f 66 20 62 6c 6f 63 6b 73 2c 20 69 6e 63 6c   of blocks, incl
35d42 75 73 69 76 65 2c 20 75 73 65 64 20 74 6f 20 64  usive, used to d
35d43 65 6c 65 74 65 20 74 68 65 20 62 6c 6f 63 6b 73  elete the blocks
35d44 0a 2a 2a 20 77 68 69 63 68 20 66 6f 72 6d 20 61  .** which form a
35d45 20 73 65 67 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61   segment..*/.sta
35d46 74 69 63 20 69 6e 74 20 62 6c 6f 63 6b 5f 64 65  tic int block_de
35d47 6c 65 74 65 28 66 75 6c 6c 74 65 78 74 5f 76 74  lete(fulltext_vt
35d48 61 62 20 2a 76 2c 0a 20 20 20 20 20 20 20 20 20  ab *v,.         
35d49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
35d4a 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61  qlite_int64 iSta
35d4b 72 74 42 6c 6f 63 6b 69 64 2c 20 73 71 6c 69 74  rtBlockid, sqlit
35d4c 65 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f 63  e_int64 iEndBloc
35d4d 6b 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  kid){.  sqlite3_
35d4e 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72  stmt *s;.  int r
35d4f 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74  c = sql_get_stat
35d50 65 6d 65 6e 74 28 76 2c 20 42 4c 4f 43 4b 5f 44  ement(v, BLOCK_D
35d51 45 4c 45 54 45 5f 53 54 4d 54 2c 20 26 73 29 3b  ELETE_STMT, &s);
35d52 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
35d53 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
35d54 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
35d55 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20  3_bind_int64(s, 
35d56 31 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64  1, iStartBlockid
35d57 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
35d58 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
35d59 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  rc;..  rc = sqli
35d5a 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73  te3_bind_int64(s
35d5b 2c 20 32 2c 20 69 45 6e 64 42 6c 6f 63 6b 69 64  , 2, iEndBlockid
35d5c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
35d5d 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
35d5e 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73 71  rc;..  return sq
35d5f 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73 29  l_single_step(s)
35d60 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 73 20  ;.}../* Returns 
35d61 53 51 4c 49 54 45 5f 52 4f 57 20 77 69 74 68 20  SQLITE_ROW with 
35d62 2a 70 69 64 78 20 73 65 74 20 74 6f 20 74 68 65  *pidx set to the
35d63 20 6d 61 78 69 6d 75 6d 20 73 65 67 6d 65 6e 74   maximum segment
35d64 20 69 64 78 20 66 6f 75 6e 64 0a 2a 2a 20 61 74   idx found.** at
35d65 20 69 4c 65 76 65 6c 2e 20 20 52 65 74 75 72 6e   iLevel.  Return
35d66 73 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 66  s SQLITE_DONE if
35d67 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 73 65   there are no se
35d68 67 6d 65 6e 74 73 20 61 74 0a 2a 2a 20 69 4c 65  gments at.** iLe
35d69 76 65 6c 2e 20 20 4f 74 68 65 72 77 69 73 65 20  vel.  Otherwise 
35d6a 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72  returns an error
35d6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
35d6c 73 65 67 64 69 72 5f 6d 61 78 5f 69 6e 64 65 78  segdir_max_index
35d6d 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
35d6e 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20 69  v, int iLevel, i
35d6f 6e 74 20 2a 70 69 64 78 29 7b 0a 20 20 73 71 6c  nt *pidx){.  sql
35d70 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20  ite3_stmt *s;.  
35d71 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74  int rc = sql_get
35d72 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45  _statement(v, SE
35d73 47 44 49 52 5f 4d 41 58 5f 49 4e 44 45 58 5f 53  GDIR_MAX_INDEX_S
35d74 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20  TMT, &s);.  if( 
35d75 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
35d76 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
35d77 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f   = sqlite3_bind_
35d78 69 6e 74 28 73 2c 20 31 2c 20 69 4c 65 76 65 6c  int(s, 1, iLevel
35d79 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
35d7a 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
35d7b 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  rc;..  rc = sqli
35d7c 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 2f  te3_step(s);.  /
35d7d 2a 20 53 68 6f 75 6c 64 20 61 6c 77 61 79 73 20  * Should always 
35d7e 67 65 74 20 61 74 20 6c 65 61 73 74 20 6f 6e 65  get at least one
35d7f 20 72 6f 77 20 64 75 65 20 74 6f 20 68 6f 77 20   row due to how 
35d80 6d 61 78 28 29 20 77 6f 72 6b 73 2e 20 2a 2f 0a  max() works. */.
35d81 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
35d82 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 53  _DONE ) return S
35d83 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 69 66  QLITE_DONE;.  if
35d84 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc!=SQLITE_ROW
35d85 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
35d86 20 2f 2a 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 74   /* NULL means t
35d87 68 61 74 20 74 68 65 72 65 20 77 65 72 65 20 6e  hat there were n
35d88 6f 20 69 6e 70 75 74 73 20 74 6f 20 6d 61 78 28  o inputs to max(
35d89 29 2e 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49  ). */.  if( SQLI
35d8a 54 45 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33  TE_NULL==sqlite3
35d8b 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 2c 20  _column_type(s, 
35d8c 30 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  0) ){.    rc = s
35d8d 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a  qlite3_step(s);.
35d8e 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
35d8f 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20  TE_ROW ) return 
35d90 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
35d91 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
35d92 0a 0a 20 20 2a 70 69 64 78 20 3d 20 73 71 6c 69  ..  *pidx = sqli
35d93 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73  te3_column_int(s
35d94 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 65  , 0);..  /* We e
35d95 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72  xpect only one r
35d96 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65 78 65  ow.  We must exe
35d97 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73 71 6c  cute another sql
35d98 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20 20 2a  ite3_step().   *
35d99 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65   to complete the
35d9a 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65   iteration; othe
35d9b 72 77 69 73 65 20 74 68 65 20 74 61 62 6c 65 20  rwise the table 
35d9c 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f 63 6b  will remain lock
35d9d 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71  ed. */.  rc = sq
35d9e 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20  lite3_step(s);. 
35d9f 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
35da0 52 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ROW ) return SQL
35da1 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 66 28  ITE_ERROR;.  if(
35da2 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc!=SQLITE_DONE
35da3 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
35da4 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 4f  return SQLITE_RO
35da5 57 3b 0a 7d 0a 0a 2f 2a 20 69 6e 73 65 72 74 20  W;.}../* insert 
35da6 69 6e 74 6f 20 25 5f 73 65 67 64 69 72 20 76 61  into %_segdir va
35da7 6c 75 65 73 20 28 0a 2a 2a 20 20 20 5b 69 4c 65  lues (.**   [iLe
35da8 76 65 6c 5d 2c 20 5b 69 64 78 5d 2c 0a 2a 2a 20  vel], [idx],.** 
35da9 20 20 5b 69 53 74 61 72 74 42 6c 6f 63 6b 69 64    [iStartBlockid
35daa 5d 2c 20 5b 69 4c 65 61 76 65 73 45 6e 64 42 6c  ], [iLeavesEndBl
35dab 6f 63 6b 69 64 5d 2c 20 5b 69 45 6e 64 42 6c 6f  ockid], [iEndBlo
35dac 63 6b 69 64 5d 2c 0a 2a 2a 20 20 20 5b 70 52 6f  ckid],.**   [pRo
35dad 6f 74 44 61 74 61 5d 0a 2a 2a 20 29 0a 2a 2f 0a  otData].** ).*/.
35dae 73 74 61 74 69 63 20 69 6e 74 20 73 65 67 64 69  static int segdi
35daf 72 5f 73 65 74 28 66 75 6c 6c 74 65 78 74 5f 76  r_set(fulltext_v
35db0 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76  tab *v, int iLev
35db1 65 6c 2c 20 69 6e 74 20 69 64 78 2c 0a 20 20 20  el, int idx,.   
35db2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35db3 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
35db4 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c 0a 20  iStartBlockid,. 
35db5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35db6 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
35db7 34 20 69 4c 65 61 76 65 73 45 6e 64 42 6c 6f 63  4 iLeavesEndBloc
35db8 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20  kid,.           
35db9 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
35dba 65 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f 63  e_int64 iEndBloc
35dbb 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20  kid,.           
35dbc 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
35dbd 20 63 68 61 72 20 2a 70 52 6f 6f 74 44 61 74 61   char *pRootData
35dbe 2c 20 69 6e 74 20 6e 52 6f 6f 74 44 61 74 61 29  , int nRootData)
35dbf 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  {.  sqlite3_stmt
35dc0 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20   *s;.  int rc = 
35dc1 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e  sql_get_statemen
35dc2 74 28 76 2c 20 53 45 47 44 49 52 5f 53 45 54 5f  t(v, SEGDIR_SET_
35dc3 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28  STMT, &s);.  if(
35dc4 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
35dc5 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
35dc6 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  c = sqlite3_bind
35dc7 5f 69 6e 74 28 73 2c 20 31 2c 20 69 4c 65 76 65  _int(s, 1, iLeve
35dc8 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  l);.  if( rc!=SQ
35dc9 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
35dca 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
35dcb 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c  ite3_bind_int(s,
35dcc 20 32 2c 20 69 64 78 29 3b 0a 20 20 69 66 28 20   2, idx);.  if( 
35dcd 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
35dce 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63  return rc;..  rc
35dcf 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f   = sqlite3_bind_
35dd0 69 6e 74 36 34 28 73 2c 20 33 2c 20 69 53 74 61  int64(s, 3, iSta
35dd1 72 74 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66  rtBlockid);.  if
35dd2 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
35dd3 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
35dd4 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e  rc = sqlite3_bin
35dd5 64 5f 69 6e 74 36 34 28 73 2c 20 34 2c 20 69 4c  d_int64(s, 4, iL
35dd6 65 61 76 65 73 45 6e 64 42 6c 6f 63 6b 69 64 29  eavesEndBlockid)
35dd7 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
35dd8 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
35dd9 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
35dda 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c  e3_bind_int64(s,
35ddb 20 35 2c 20 69 45 6e 64 42 6c 6f 63 6b 69 64 29   5, iEndBlockid)
35ddc 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
35ddd 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
35dde 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
35ddf 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 73 2c 20  e3_bind_blob(s, 
35de0 36 2c 20 70 52 6f 6f 74 44 61 74 61 2c 20 6e 52  6, pRootData, nR
35de1 6f 6f 74 44 61 74 61 2c 20 53 51 4c 49 54 45 5f  ootData, SQLITE_
35de2 53 54 41 54 49 43 29 3b 0a 20 20 69 66 28 20 72  STATIC);.  if( r
35de3 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
35de4 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74  eturn rc;..  ret
35de5 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73  urn sql_single_s
35de6 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f 2a 20 51 75  tep(s);.}../* Qu
35de7 65 72 69 65 73 20 25 5f 73 65 67 64 69 72 20 66  eries %_segdir f
35de8 6f 72 20 74 68 65 20 62 6c 6f 63 6b 20 73 70 61  or the block spa
35de9 6e 20 6f 66 20 74 68 65 20 73 65 67 6d 65 6e 74  n of the segment
35dea 73 20 69 6e 20 6c 65 76 65 6c 0a 2a 2a 20 69 4c  s in level.** iL
35deb 65 76 65 6c 2e 20 20 52 65 74 75 72 6e 73 20 53  evel.  Returns S
35dec 51 4c 49 54 45 5f 44 4f 4e 45 20 69 66 20 74 68  QLITE_DONE if th
35ded 65 72 65 20 61 72 65 20 6e 6f 20 62 6c 6f 63 6b  ere are no block
35dee 73 20 66 6f 72 20 69 4c 65 76 65 6c 2c 0a 2a 2a  s for iLevel,.**
35def 20 53 51 4c 49 54 45 5f 52 4f 57 20 69 66 20 74   SQLITE_ROW if t
35df0 68 65 72 65 20 61 72 65 20 62 6c 6f 63 6b 73 2c  here are blocks,
35df1 20 65 6c 73 65 20 61 6e 20 65 72 72 6f 72 2e 0a   else an error..
35df2 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65  */.static int se
35df3 67 64 69 72 5f 73 70 61 6e 28 66 75 6c 6c 74 65  gdir_span(fullte
35df4 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20  xt_vtab *v, int 
35df5 69 4c 65 76 65 6c 2c 0a 20 20 20 20 20 20 20 20  iLevel,.        
35df6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
35df7 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69 53  qlite_int64 *piS
35df8 74 61 72 74 42 6c 6f 63 6b 69 64 2c 0a 20 20 20  tartBlockid,.   
35df9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
35dfa 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
35dfb 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64 29 7b   *piEndBlockid){
35dfc 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
35dfd 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73  *s;.  int rc = s
35dfe 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
35dff 28 76 2c 20 53 45 47 44 49 52 5f 53 50 41 4e 5f  (v, SEGDIR_SPAN_
35e00 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28  STMT, &s);.  if(
35e01 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
35e02 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
35e03 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  c = sqlite3_bind
35e04 5f 69 6e 74 28 73 2c 20 31 2c 20 69 4c 65 76 65  _int(s, 1, iLeve
35e05 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  l);.  if( rc!=SQ
35e06 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
35e07 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
35e08 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20  ite3_step(s);.  
35e09 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44  if( rc==SQLITE_D
35e0a 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ONE ) return SQL
35e0b 49 54 45 5f 44 4f 4e 45 3b 20 20 2f 2a 20 53 68  ITE_DONE;  /* Sh
35e0c 6f 75 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65  ould never happe
35e0d 6e 20 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d 53  n */.  if( rc!=S
35e0e 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75  QLITE_ROW ) retu
35e0f 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 69  rn rc;..  /* Thi
35e10 73 20 68 61 70 70 65 6e 73 20 69 66 20 61 6c 6c  s happens if all
35e11 20 73 65 67 6d 65 6e 74 73 20 61 74 20 74 68 69   segments at thi
35e12 73 20 6c 65 76 65 6c 20 61 72 65 20 65 6e 74 69  s level are enti
35e13 72 65 6c 79 20 69 6e 6c 69 6e 65 2e 20 2a 2f 0a  rely inline. */.
35e14 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c    if( SQLITE_NUL
35e15 4c 3d 3d 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  L==sqlite3_colum
35e16 6e 5f 74 79 70 65 28 73 2c 20 30 29 20 29 7b 0a  n_type(s, 0) ){.
35e17 20 20 20 20 2f 2a 20 57 65 20 65 78 70 65 63 74      /* We expect
35e18 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77 2e 20 20   only one row.  
35e19 57 65 20 6d 75 73 74 20 65 78 65 63 75 74 65 20  We must execute 
35e1a 61 6e 6f 74 68 65 72 20 73 71 6c 69 74 65 33 5f  another sqlite3_
35e1b 73 74 65 70 28 29 0a 20 20 20 20 20 2a 20 74 6f  step().     * to
35e1c 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 69 74   complete the it
35e1d 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77 69  eration; otherwi
35e1e 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 6c  se the table wil
35e1f 6c 20 72 65 6d 61 69 6e 20 6c 6f 63 6b 65 64 2e  l remain locked.
35e20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72 63 32 20   */.    int rc2 
35e21 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73  = sqlite3_step(s
35e22 29 3b 0a 20 20 20 20 69 66 28 20 72 63 32 3d 3d  );.    if( rc2==
35e23 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74  SQLITE_ROW ) ret
35e24 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
35e25 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 32  ;.    return rc2
35e26 3b 0a 20 20 7d 0a 0a 20 20 2a 70 69 53 74 61 72  ;.  }..  *piStar
35e27 74 42 6c 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74  tBlockid = sqlit
35e28 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28  e3_column_int64(
35e29 73 2c 20 30 29 3b 0a 20 20 2a 70 69 45 6e 64 42  s, 0);.  *piEndB
35e2a 6c 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74 65 33  lockid = sqlite3
35e2b 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c  _column_int64(s,
35e2c 20 31 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 65 78   1);..  /* We ex
35e2d 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f  pect only one ro
35e2e 77 2e 20 20 57 65 20 6d 75 73 74 20 65 78 65 63  w.  We must exec
35e2f 75 74 65 20 61 6e 6f 74 68 65 72 20 73 71 6c 69  ute another sqli
35e30 74 65 33 5f 73 74 65 70 28 29 0a 20 20 20 2a 20  te3_step().   * 
35e31 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20  to complete the 
35e32 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72  iteration; other
35e33 77 69 73 65 20 74 68 65 20 74 61 62 6c 65 20 77  wise the table w
35e34 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f 63 6b 65  ill remain locke
35e35 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  d. */.  rc = sql
35e36 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20  ite3_step(s);.  
35e37 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52  if( rc==SQLITE_R
35e38 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  OW ) return SQLI
35e39 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 66 28 20  TE_ERROR;.  if( 
35e3a 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc!=SQLITE_DONE 
35e3b 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72  ) return rc;.  r
35e3c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 4f 57  eturn SQLITE_ROW
35e3d 3b 0a 7d 0a 0a 2f 2a 20 44 65 6c 65 74 65 20 74  ;.}../* Delete t
35e3e 68 65 20 73 65 67 6d 65 6e 74 20 62 6c 6f 63 6b  he segment block
35e3f 73 20 61 6e 64 20 73 65 67 6d 65 6e 74 20 64 69  s and segment di
35e40 72 65 63 74 6f 72 79 20 72 65 63 6f 72 64 73 20  rectory records 
35e41 66 6f 72 20 61 6c 6c 0a 2a 2a 20 73 65 67 6d 65  for all.** segme
35e42 6e 74 73 20 61 74 20 69 4c 65 76 65 6c 2e 0a 2a  nts at iLevel..*
35e43 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 67  /.static int seg
35e44 64 69 72 5f 64 65 6c 65 74 65 28 66 75 6c 6c 74  dir_delete(fullt
35e45 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74  ext_vtab *v, int
35e46 20 69 4c 65 76 65 6c 29 7b 0a 20 20 73 71 6c 69   iLevel){.  sqli
35e47 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 73  te3_stmt *s;.  s
35e48 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61  qlite_int64 iSta
35e49 72 74 42 6c 6f 63 6b 69 64 2c 20 69 45 6e 64 42  rtBlockid, iEndB
35e4a 6c 6f 63 6b 69 64 3b 0a 20 20 69 6e 74 20 72 63  lockid;.  int rc
35e4b 20 3d 20 73 65 67 64 69 72 5f 73 70 61 6e 28 76   = segdir_span(v
35e4c 2c 20 69 4c 65 76 65 6c 2c 20 26 69 53 74 61 72  , iLevel, &iStar
35e4d 74 42 6c 6f 63 6b 69 64 2c 20 26 69 45 6e 64 42  tBlockid, &iEndB
35e4e 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72  lockid);.  if( r
35e4f 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 26 26  c!=SQLITE_ROW &&
35e50 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45   rc!=SQLITE_DONE
35e51 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
35e52 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
35e53 52 4f 57 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ROW ){.    rc = 
35e54 62 6c 6f 63 6b 5f 64 65 6c 65 74 65 28 76 2c 20  block_delete(v, 
35e55 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c 20 69  iStartBlockid, i
35e56 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 20  EndBlockid);.   
35e57 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35e58 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
35e59 20 20 7d 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65    }..  /* Delete
35e5a 20 74 68 65 20 73 65 67 6d 65 6e 74 20 64 69 72   the segment dir
35e5b 65 63 74 6f 72 79 20 69 74 73 65 6c 66 2e 20 2a  ectory itself. *
35e5c 2f 0a 20 20 72 63 20 3d 20 73 71 6c 5f 67 65 74  /.  rc = sql_get
35e5d 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45  _statement(v, SE
35e5e 47 44 49 52 5f 44 45 4c 45 54 45 5f 53 54 4d 54  GDIR_DELETE_STMT
35e5f 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
35e60 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
35e61 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
35e62 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
35e63 36 34 28 73 2c 20 31 2c 20 69 4c 65 76 65 6c 29  64(s, 1, iLevel)
35e64 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
35e65 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
35e66 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  c;..  return sql
35e67 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b  _single_step(s);
35e68 0a 7d 0a 0a 2f 2a 20 44 65 6c 65 74 65 20 65 6e  .}../* Delete en
35e69 74 69 72 65 20 66 74 73 20 69 6e 64 65 78 2c 20  tire fts index, 
35e6a 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63  SQLITE_OK on suc
35e6b 63 65 73 73 2c 20 72 65 6c 65 76 61 6e 74 20 65  cess, relevant e
35e6c 72 72 6f 72 20 6f 6e 0a 2a 2a 20 66 61 69 6c 75  rror on.** failu
35e6d 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  re..*/.static in
35e6e 74 20 73 65 67 64 69 72 5f 64 65 6c 65 74 65 5f  t segdir_delete_
35e6f 61 6c 6c 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  all(fulltext_vta
35e70 62 20 2a 76 29 7b 0a 20 20 73 71 6c 69 74 65 33  b *v){.  sqlite3
35e71 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20  _stmt *s;.  int 
35e72 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61  rc = sql_get_sta
35e73 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52  tement(v, SEGDIR
35e74 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f 53 54 4d 54  _DELETE_ALL_STMT
35e75 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
35e76 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
35e77 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
35e78 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28  sql_single_step(
35e79 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  s);.  if( rc!=SQ
35e7a 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
35e7b 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
35e7c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
35e7d 2c 20 42 4c 4f 43 4b 5f 44 45 4c 45 54 45 5f 41  , BLOCK_DELETE_A
35e7e 4c 4c 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20  LL_STMT, &s);.  
35e7f 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
35e80 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
35e81 20 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69 6e    return sql_sin
35e82 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a 0a  gle_step(s);.}..
35e83 2f 2a 20 52 65 74 75 72 6e 73 20 53 51 4c 49 54  /* Returns SQLIT
35e84 45 5f 4f 4b 20 77 69 74 68 20 2a 70 6e 53 65 67  E_OK with *pnSeg
35e85 6d 65 6e 74 73 20 73 65 74 20 74 6f 20 74 68 65  ments set to the
35e86 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   number of entri
35e87 65 73 20 69 6e 0a 2a 2a 20 25 5f 73 65 67 64 69  es in.** %_segdi
35e88 72 20 61 6e 64 20 2a 70 69 4d 61 78 4c 65 76 65  r and *piMaxLeve
35e89 6c 20 73 65 74 20 74 6f 20 74 68 65 20 68 69 67  l set to the hig
35e8a 68 65 73 74 20 6c 65 76 65 6c 20 77 68 69 63 68  hest level which
35e8b 20 68 61 73 20 61 0a 2a 2a 20 73 65 67 6d 65 6e   has a.** segmen
35e8c 74 2e 20 20 4f 74 68 65 72 77 69 73 65 20 72 65  t.  Otherwise re
35e8d 74 75 72 6e 73 20 74 68 65 20 53 51 4c 69 74 65  turns the SQLite
35e8e 20 65 72 72 6f 72 20 77 68 69 63 68 20 63 61 75   error which cau
35e8f 73 65 64 20 66 61 69 6c 75 72 65 2e 0a 2a 2f 0a  sed failure..*/.
35e90 73 74 61 74 69 63 20 69 6e 74 20 73 65 67 64 69  static int segdi
35e91 72 5f 63 6f 75 6e 74 28 66 75 6c 6c 74 65 78 74  r_count(fulltext
35e92 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 2a 70  _vtab *v, int *p
35e93 6e 53 65 67 6d 65 6e 74 73 2c 20 69 6e 74 20 2a  nSegments, int *
35e94 70 69 4d 61 78 4c 65 76 65 6c 29 7b 0a 20 20 73  piMaxLevel){.  s
35e95 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a  qlite3_stmt *s;.
35e96 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67    int rc = sql_g
35e97 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
35e98 53 45 47 44 49 52 5f 43 4f 55 4e 54 5f 53 54 4d  SEGDIR_COUNT_STM
35e99 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63  T, &s);.  if( rc
35e9a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
35e9b 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
35e9c 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
35e9d 3b 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73  ;.  /* TODO(shes
35e9e 73 29 3a 20 54 68 69 73 20 63 61 73 65 20 73 68  s): This case sh
35e9f 6f 75 6c 64 20 6e 6f 74 20 62 65 20 70 6f 73 73  ould not be poss
35ea0 69 62 6c 65 3f 20 20 53 68 6f 75 6c 64 20 73 74  ible?  Should st
35ea1 72 6f 6e 67 65 72 0a 20 20 2a 2a 20 6d 65 61 73  ronger.  ** meas
35ea2 75 72 65 73 20 62 65 20 74 61 6b 65 6e 20 69 66  ures be taken if
35ea3 20 69 74 20 68 61 70 70 65 6e 73 3f 0a 20 20 2a   it happens?.  *
35ea4 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
35ea5 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 2a  TE_DONE ){.    *
35ea6 70 6e 53 65 67 6d 65 6e 74 73 20 3d 20 30 3b 0a  pnSegments = 0;.
35ea7 20 20 20 20 2a 70 69 4d 61 78 4c 65 76 65 6c 20      *piMaxLevel 
35ea8 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 0;.    return 
35ea9 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
35eaa 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
35eab 52 4f 57 20 29 20 72 65 74 75 72 6e 20 72 63 3b  ROW ) return rc;
35eac 0a 0a 20 20 2a 70 6e 53 65 67 6d 65 6e 74 73 20  ..  *pnSegments 
35ead 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
35eae 5f 69 6e 74 28 73 2c 20 30 29 3b 0a 20 20 2a 70  _int(s, 0);.  *p
35eaf 69 4d 61 78 4c 65 76 65 6c 20 3d 20 73 71 6c 69  iMaxLevel = sqli
35eb0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 73  te3_column_int(s
35eb1 2c 20 31 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 65  , 1);..  /* We e
35eb2 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72  xpect only one r
35eb3 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65 78 65  ow.  We must exe
35eb4 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73 71 6c  cute another sql
35eb5 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20 20 2a  ite3_step().   *
35eb6 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65   to complete the
35eb7 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65   iteration; othe
35eb8 72 77 69 73 65 20 74 68 65 20 74 61 62 6c 65 20  rwise the table 
35eb9 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f 63 6b  will remain lock
35eba 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71  ed. */.  rc = sq
35ebb 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 20  lite3_step(s);. 
35ebc 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
35ebd 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 53 51  DONE ) return SQ
35ebe 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 72  LITE_OK;.  if( r
35ebf 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20  c==SQLITE_ROW ) 
35ec0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
35ec1 52 4f 52 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ROR;.  return rc
35ec2 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  ;.}../* TODO(she
35ec3 73 73 29 20 63 6c 65 61 72 50 65 6e 64 69 6e 67  ss) clearPending
35ec4 54 65 72 6d 73 28 29 20 69 73 20 66 61 72 20 64  Terms() is far d
35ec5 6f 77 6e 20 74 68 65 20 66 69 6c 65 20 62 65 63  own the file bec
35ec6 61 75 73 65 0a 2a 2a 20 77 72 69 74 65 5a 65 72  ause.** writeZer
35ec7 6f 53 65 67 6d 65 6e 74 28 29 20 69 73 20 66 61  oSegment() is fa
35ec8 72 20 64 6f 77 6e 20 74 68 65 20 66 69 6c 65 20  r down the file 
35ec9 62 65 63 61 75 73 65 20 4c 65 61 66 57 72 69 74  because LeafWrit
35eca 65 72 20 69 73 20 66 61 72 0a 2a 2a 20 64 6f 77  er is far.** dow
35ecb 6e 20 74 68 65 20 66 69 6c 65 2e 20 20 43 6f 6e  n the file.  Con
35ecc 73 69 64 65 72 20 72 65 66 61 63 74 6f 72 69 6e  sider refactorin
35ecd 67 20 74 68 65 20 63 6f 64 65 20 74 6f 20 6d 6f  g the code to mo
35ece 76 65 20 74 68 65 20 6e 6f 6e 2d 76 74 61 62 0a  ve the non-vtab.
35ecf 2a 2a 20 63 6f 64 65 20 61 62 6f 76 65 20 74 68  ** code above th
35ed0 65 20 76 74 61 62 20 63 6f 64 65 20 73 6f 20 74  e vtab code so t
35ed1 68 61 74 20 77 65 20 64 6f 6e 27 74 20 6e 65 65  hat we don't nee
35ed2 64 20 74 68 69 73 20 66 6f 72 77 61 72 64 0a 2a  d this forward.*
35ed3 2a 20 72 65 66 65 72 65 6e 63 65 2e 0a 2a 2f 0a  * reference..*/.
35ed4 73 74 61 74 69 63 20 69 6e 74 20 63 6c 65 61 72  static int clear
35ed5 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 66 75 6c  PendingTerms(ful
35ed6 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29 3b 0a  ltext_vtab *v);.
35ed7 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 74 68 65 20  ./*.** Free the 
35ed8 6d 65 6d 6f 72 79 20 75 73 65 64 20 74 6f 20 63  memory used to c
35ed9 6f 6e 74 61 69 6e 20 61 20 66 75 6c 6c 74 65 78  ontain a fulltex
35eda 74 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65  t_vtab structure
35edb 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
35edc 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 5f 64   fulltext_vtab_d
35edd 65 73 74 72 6f 79 28 66 75 6c 6c 74 65 78 74 5f  estroy(fulltext_
35ede 76 74 61 62 20 2a 76 29 7b 0a 20 20 69 6e 74 20  vtab *v){.  int 
35edf 69 53 74 6d 74 2c 20 69 3b 0a 0a 20 20 46 54 53  iStmt, i;..  FTS
35ee0 54 52 41 43 45 28 28 22 46 54 53 33 20 44 65 73  TRACE(("FTS3 Des
35ee1 74 72 6f 79 20 25 70 5c 6e 22 2c 20 76 29 29 3b  troy %p\n", v));
35ee2 0a 20 20 66 6f 72 28 20 69 53 74 6d 74 3d 30 3b  .  for( iStmt=0;
35ee3 20 69 53 74 6d 74 3c 4d 41 58 5f 53 54 4d 54 3b   iStmt<MAX_STMT;
35ee4 20 69 53 74 6d 74 2b 2b 20 29 7b 0a 20 20 20 20   iStmt++ ){.    
35ee5 69 66 28 20 76 2d 3e 70 46 75 6c 6c 74 65 78 74  if( v->pFulltext
35ee6 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d 74  Statements[iStmt
35ee7 5d 21 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20  ]!=NULL ){.     
35ee8 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
35ee9 65 28 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74  e(v->pFulltextSt
35eea 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 29  atements[iStmt])
35eeb 3b 0a 20 20 20 20 20 20 76 2d 3e 70 46 75 6c 6c  ;.      v->pFull
35eec 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73 5b 69  textStatements[i
35eed 53 74 6d 74 5d 20 3d 20 4e 55 4c 4c 3b 0a 20 20  Stmt] = NULL;.  
35eee 20 20 7d 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 20    }.  }..  for( 
35eef 69 3d 30 3b 20 69 3c 4d 45 52 47 45 5f 43 4f 55  i=0; i<MERGE_COU
35ef0 4e 54 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 69  NT; i++ ){.    i
35ef1 66 28 20 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63  f( v->pLeafSelec
35ef2 74 53 74 6d 74 73 5b 69 5d 21 3d 4e 55 4c 4c 20  tStmts[i]!=NULL 
35ef3 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
35ef4 5f 66 69 6e 61 6c 69 7a 65 28 76 2d 3e 70 4c 65  _finalize(v->pLe
35ef5 61 66 53 65 6c 65 63 74 53 74 6d 74 73 5b 69 5d  afSelectStmts[i]
35ef6 29 3b 0a 20 20 20 20 20 20 76 2d 3e 70 4c 65 61  );.      v->pLea
35ef7 66 53 65 6c 65 63 74 53 74 6d 74 73 5b 69 5d 20  fSelectStmts[i] 
35ef8 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20  = NULL;.    }.  
35ef9 7d 0a 0a 20 20 69 66 28 20 76 2d 3e 70 54 6f 6b  }..  if( v->pTok
35efa 65 6e 69 7a 65 72 21 3d 4e 55 4c 4c 20 29 7b 0a  enizer!=NULL ){.
35efb 20 20 20 20 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65      v->pTokenize
35efc 72 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 65 73  r->pModule->xDes
35efd 74 72 6f 79 28 76 2d 3e 70 54 6f 6b 65 6e 69 7a  troy(v->pTokeniz
35efe 65 72 29 3b 0a 20 20 20 20 76 2d 3e 70 54 6f 6b  er);.    v->pTok
35eff 65 6e 69 7a 65 72 20 3d 20 4e 55 4c 4c 3b 0a 20  enizer = NULL;. 
35f00 20 7d 0a 0a 20 20 63 6c 65 61 72 50 65 6e 64 69   }..  clearPendi
35f01 6e 67 54 65 72 6d 73 28 76 29 3b 0a 0a 20 20 73  ngTerms(v);..  s
35f02 71 6c 69 74 65 33 5f 66 72 65 65 28 76 2d 3e 61  qlite3_free(v->a
35f03 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20 66 6f 72 28  zColumn);.  for(
35f04 69 20 3d 20 30 3b 20 69 20 3c 20 76 2d 3e 6e 43  i = 0; i < v->nC
35f05 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 20 7b 0a 20 20  olumn; ++i) {.  
35f06 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76    sqlite3_free(v
35f07 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d  ->azContentColum
35f08 6e 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  n[i]);.  }.  sql
35f09 69 74 65 33 5f 66 72 65 65 28 76 2d 3e 61 7a 43  ite3_free(v->azC
35f0a 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20  ontentColumn);. 
35f0b 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76 29   sqlite3_free(v)
35f0c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 6f 6b 65 6e  ;.}../*.** Token
35f0d 20 74 79 70 65 73 20 66 6f 72 20 70 61 72 73 69   types for parsi
35f0e 6e 67 20 74 68 65 20 61 72 67 75 6d 65 6e 74 73  ng the arguments
35f0f 20 74 6f 20 78 43 6f 6e 6e 65 63 74 20 6f 72 20   to xConnect or 
35f10 78 43 72 65 61 74 65 2e 0a 2a 2f 0a 23 64 65 66  xCreate..*/.#def
35f11 69 6e 65 20 54 4f 4b 45 4e 5f 45 4f 46 20 20 20  ine TOKEN_EOF   
35f12 20 20 20 20 20 20 30 20 20 20 20 2f 2a 20 45 6e        0    /* En
35f13 64 20 6f 66 20 66 69 6c 65 20 2a 2f 0a 23 64 65  d of file */.#de
35f14 66 69 6e 65 20 54 4f 4b 45 4e 5f 53 50 41 43 45  fine TOKEN_SPACE
35f15 20 20 20 20 20 20 20 31 20 20 20 20 2f 2a 20 41         1    /* A
35f16 6e 79 20 6b 69 6e 64 20 6f 66 20 77 68 69 74 65  ny kind of white
35f17 73 70 61 63 65 20 2a 2f 0a 23 64 65 66 69 6e 65  space */.#define
35f18 20 54 4f 4b 45 4e 5f 49 44 20 20 20 20 20 20 20   TOKEN_ID       
35f19 20 20 20 32 20 20 20 20 2f 2a 20 41 6e 20 69 64     2    /* An id
35f1a 65 6e 74 69 66 69 65 72 20 2a 2f 0a 23 64 65 66  entifier */.#def
35f1b 69 6e 65 20 54 4f 4b 45 4e 5f 53 54 52 49 4e 47  ine TOKEN_STRING
35f1c 20 20 20 20 20 20 33 20 20 20 20 2f 2a 20 41 20        3    /* A 
35f1d 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20 2a  string literal *
35f1e 2f 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e 5f  /.#define TOKEN_
35f1f 50 55 4e 43 54 20 20 20 20 20 20 20 34 20 20 20  PUNCT       4   
35f20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 70 75 6e   /* A single pun
35f21 63 74 75 61 74 69 6f 6e 20 63 68 61 72 61 63 74  ctuation charact
35f22 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  er */../*.** If 
35f23 58 20 69 73 20 61 20 63 68 61 72 61 63 74 65 72  X is a character
35f24 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 73 65   that can be use
35f25 64 20 69 6e 20 61 6e 20 69 64 65 6e 74 69 66 69  d in an identifi
35f26 65 72 20 74 68 65 6e 0a 2a 2a 20 66 74 73 49 64  er then.** ftsId
35f27 43 68 61 72 28 58 29 20 77 69 6c 6c 20 62 65 20  Char(X) will be 
35f28 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69 73 65  true.  Otherwise
35f29 20 69 74 20 69 73 20 66 61 6c 73 65 2e 0a 2a 2a   it is false..**
35f2a 0a 2a 2a 20 46 6f 72 20 41 53 43 49 49 2c 20 61  .** For ASCII, a
35f2b 6e 79 20 63 68 61 72 61 63 74 65 72 20 77 69 74  ny character wit
35f2c 68 20 74 68 65 20 68 69 67 68 2d 6f 72 64 65 72  h the high-order
35f2d 20 62 69 74 20 73 65 74 20 69 73 0a 2a 2a 20 61   bit set is.** a
35f2e 6c 6c 6f 77 65 64 20 69 6e 20 61 6e 20 69 64 65  llowed in an ide
35f2f 6e 74 69 66 69 65 72 2e 20 20 46 6f 72 20 37 2d  ntifier.  For 7-
35f30 62 69 74 20 63 68 61 72 61 63 74 65 72 73 2c 20  bit characters, 
35f31 0a 2a 2a 20 69 73 46 74 73 49 64 43 68 61 72 5b  .** isFtsIdChar[
35f32 58 5d 20 6d 75 73 74 20 62 65 20 31 2e 0a 2a 2a  X] must be 1..**
35f33 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31 30 36 36  .** Ticket #1066
35f34 2e 20 20 74 68 65 20 53 51 4c 20 73 74 61 6e 64  .  the SQL stand
35f35 61 72 64 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c  ard does not all
35f36 6f 77 20 27 24 27 20 69 6e 20 74 68 65 0a 2a 2a  ow '$' in the.**
35f37 20 6d 69 64 64 6c 65 20 6f 66 20 69 64 65 6e 74   middle of ident
35f38 66 69 65 72 73 2e 20 20 42 75 74 20 6d 61 6e 79  fiers.  But many
35f39 20 53 51 4c 20 69 6d 70 6c 65 6d 65 6e 74 61 74   SQL implementat
35f3a 69 6f 6e 73 20 64 6f 2e 20 0a 2a 2a 20 53 51 4c  ions do. .** SQL
35f3b 69 74 65 20 77 69 6c 6c 20 61 6c 6c 6f 77 20 27  ite will allow '
35f3c 24 27 20 69 6e 20 69 64 65 6e 74 69 66 69 65 72  $' in identifier
35f3d 73 20 66 6f 72 20 63 6f 6d 70 61 74 69 62 69 6c  s for compatibil
35f3e 69 74 79 2e 0a 2a 2a 20 42 75 74 20 74 68 65 20  ity..** But the 
35f3f 66 65 61 74 75 72 65 20 69 73 20 75 6e 64 6f 63  feature is undoc
35f40 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a 73 74 61 74  umented..*/.stat
35f41 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 69 73  ic const char is
35f42 46 74 73 49 64 43 68 61 72 5b 5d 20 3d 20 7b 0a  FtsIdChar[] = {.
35f43 2f 2a 20 78 30 20 78 31 20 78 32 20 78 33 20 78  /* x0 x1 x2 x3 x
35f44 34 20 78 35 20 78 36 20 78 37 20 78 38 20 78 39  4 x5 x6 x7 x8 x9
35f45 20 78 41 20 78 42 20 78 43 20 78 44 20 78 45 20   xA xB xC xD xE 
35f46 78 46 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20  xF */.    0, 0, 
35f47 30 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c 20 30  0, 0, 1, 0, 0, 0
35f48 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
35f49 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 32 78   0, 0, 0,  /* 2x
35f4a 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c   */.    1, 1, 1,
35f4b 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
35f4c 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 1, 0, 0, 0, 0
35f4d 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 33 78 20 2a  , 0, 0,  /* 3x *
35f4e 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31  /.    0, 1, 1, 1
35f4f 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
35f50 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
35f51 31 2c 20 31 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a  1, 1,  /* 4x */.
35f52 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20      1, 1, 1, 1, 
35f53 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
35f54 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 0, 0, 0, 0,
35f55 20 31 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20   1,  /* 5x */.  
35f56 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
35f57 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
35f58 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
35f59 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20  ,  /* 6x */.    
35f5a 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
35f5b 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
35f5c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
35f5d 20 2f 2a 20 37 78 20 2a 2f 0a 7d 3b 0a 23 64 65   /* 7x */.};.#de
35f5e 66 69 6e 65 20 66 74 73 49 64 43 68 61 72 28 43  fine ftsIdChar(C
35f5f 29 20 20 28 28 28 63 3d 43 29 26 30 78 38 30 29  )  (((c=C)&0x80)
35f60 21 3d 30 20 7c 7c 20 28 63 3e 30 78 31 66 20 26  !=0 || (c>0x1f &
35f61 26 20 69 73 46 74 73 49 64 43 68 61 72 5b 63 2d  & isFtsIdChar[c-
35f62 30 78 32 30 5d 29 29 0a 0a 0a 2f 2a 0a 2a 2a 20  0x20])).../*.** 
35f63 52 65 74 75 72 6e 20 74 68 65 20 6c 65 6e 67 74  Return the lengt
35f64 68 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 74  h of the token t
35f65 68 61 74 20 62 65 67 69 6e 73 20 61 74 20 7a 5b  hat begins at z[
35f66 30 5d 2e 20 0a 2a 2a 20 53 74 6f 72 65 20 74 68  0]. .** Store th
35f67 65 20 74 6f 6b 65 6e 20 74 79 70 65 20 69 6e 20  e token type in 
35f68 2a 74 6f 6b 65 6e 54 79 70 65 20 62 65 66 6f 72  *tokenType befor
35f69 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a  e returning..*/.
35f6a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 47 65  static int ftsGe
35f6b 74 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 63 68 61  tToken(const cha
35f6c 72 20 2a 7a 2c 20 69 6e 74 20 2a 74 6f 6b 65 6e  r *z, int *token
35f6d 54 79 70 65 29 7b 0a 20 20 69 6e 74 20 69 2c 20  Type){.  int i, 
35f6e 63 3b 0a 20 20 73 77 69 74 63 68 28 20 2a 7a 20  c;.  switch( *z 
35f6f 29 7b 0a 20 20 20 20 63 61 73 65 20 30 3a 20 7b  ){.    case 0: {
35f70 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
35f71 65 20 3d 20 54 4f 4b 45 4e 5f 45 4f 46 3b 0a 20  e = TOKEN_EOF;. 
35f72 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
35f73 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 20     }.    case ' 
35f74 27 3a 20 63 61 73 65 20 27 5c 74 27 3a 20 63 61  ': case '\t': ca
35f75 73 65 20 27 5c 6e 27 3a 20 63 61 73 65 20 27 5c  se '\n': case '\
35f76 66 27 3a 20 63 61 73 65 20 27 5c 72 27 3a 20 7b  f': case '\r': {
35f77 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20  .      for(i=1; 
35f78 73 61 66 65 5f 69 73 73 70 61 63 65 28 7a 5b 69  safe_isspace(z[i
35f79 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20  ]); i++){}.     
35f7a 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4f   *tokenType = TO
35f7b 4b 45 4e 5f 53 50 41 43 45 3b 0a 20 20 20 20 20  KEN_SPACE;.     
35f7c 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d   return i;.    }
35f7d 0a 20 20 20 20 63 61 73 65 20 27 60 27 3a 0a 20  .    case '`':. 
35f7e 20 20 20 63 61 73 65 20 27 5c 27 27 3a 0a 20 20     case '\'':.  
35f7f 20 20 63 61 73 65 20 27 22 27 3a 20 7b 0a 20 20    case '"': {.  
35f80 20 20 20 20 69 6e 74 20 64 65 6c 69 6d 20 3d 20      int delim = 
35f81 7a 5b 30 5d 3b 0a 20 20 20 20 20 20 66 6f 72 28  z[0];.      for(
35f82 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d 29 21 3d 30  i=1; (c=z[i])!=0
35f83 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
35f84 69 66 28 20 63 3d 3d 64 65 6c 69 6d 20 29 7b 0a  if( c==delim ){.
35f85 20 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 5b            if( z[
35f86 69 2b 31 5d 3d 3d 64 65 6c 69 6d 20 29 7b 0a 20  i+1]==delim ){. 
35f87 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a             i++;.
35f88 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
35f89 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
35f8a 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ak;.          }.
35f8b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
35f8c 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  }.      *tokenTy
35f8d 70 65 20 3d 20 54 4f 4b 45 4e 5f 53 54 52 49 4e  pe = TOKEN_STRIN
35f8e 47 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  G;.      return 
35f8f 69 20 2b 20 28 63 21 3d 30 29 3b 0a 20 20 20 20  i + (c!=0);.    
35f90 7d 0a 20 20 20 20 63 61 73 65 20 27 5b 27 3a 20  }.    case '[': 
35f91 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 2c  {.      for(i=1,
35f92 20 63 3d 7a 5b 30 5d 3b 20 63 21 3d 27 5d 27 20   c=z[0]; c!=']' 
35f93 26 26 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20  && (c=z[i])!=0; 
35f94 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74 6f  i++){}.      *to
35f95 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e 5f  kenType = TOKEN_
35f96 49 44 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ID;.      return
35f97 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65   i;.    }.    de
35f98 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 69  fault: {.      i
35f99 66 28 20 21 66 74 73 49 64 43 68 61 72 28 2a 7a  f( !ftsIdChar(*z
35f9a 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72 65  ) ){.        bre
35f9b 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
35f9c 20 20 66 6f 72 28 69 3d 31 3b 20 66 74 73 49 64    for(i=1; ftsId
35f9d 43 68 61 72 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29  Char(z[i]); i++)
35f9e 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54  {}.      *tokenT
35f9f 79 70 65 20 3d 20 54 4f 4b 45 4e 5f 49 44 3b 0a  ype = TOKEN_ID;.
35fa0 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a        return i;.
35fa1 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 74 6f 6b      }.  }.  *tok
35fa2 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e 5f 50  enType = TOKEN_P
35fa3 55 4e 43 54 3b 0a 20 20 72 65 74 75 72 6e 20 31  UNCT;.  return 1
35fa4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 6f 6b  ;.}../*.** A tok
35fa5 65 6e 20 65 78 74 72 61 63 74 65 64 20 66 72 6f  en extracted fro
35fa6 6d 20 61 20 73 74 72 69 6e 67 20 69 73 20 61 6e  m a string is an
35fa7 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
35fa8 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74   following.** st
35fa9 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70 65  ructure..*/.type
35faa 64 65 66 20 73 74 72 75 63 74 20 46 74 73 54 6f  def struct FtsTo
35fab 6b 65 6e 20 7b 0a 20 20 63 6f 6e 73 74 20 63 68  ken {.  const ch
35fac 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 2f 2a 20  ar *z;       /* 
35fad 50 6f 69 6e 74 65 72 20 74 6f 20 74 6f 6b 65 6e  Pointer to token
35fae 20 74 65 78 74 2e 20 20 4e 6f 74 20 27 5c 30 30   text.  Not '\00
35faf 30 27 20 74 65 72 6d 69 6e 61 74 65 64 20 2a 2f  0' terminated */
35fb0 0a 20 20 73 68 6f 72 74 20 69 6e 74 20 6e 3b 20  .  short int n; 
35fb1 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
35fb2 68 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 74  h of the token t
35fb3 65 78 74 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f  ext in bytes. */
35fb4 0a 7d 20 46 74 73 54 6f 6b 65 6e 3b 0a 0a 2f 2a  .} FtsToken;../*
35fb5 0a 2a 2a 20 47 69 76 65 6e 20 61 20 69 6e 70 75  .** Given a inpu
35fb6 74 20 73 74 72 69 6e 67 20 28 77 68 69 63 68 20  t string (which 
35fb7 69 73 20 72 65 61 6c 6c 79 20 6f 6e 65 20 6f 66  is really one of
35fb8 20 74 68 65 20 61 72 67 76 5b 5d 20 70 61 72 61   the argv[] para
35fb9 6d 65 74 65 72 73 0a 2a 2a 20 70 61 73 73 65 64  meters.** passed
35fba 20 69 6e 74 6f 20 78 43 6f 6e 6e 65 63 74 20 6f   into xConnect o
35fbb 72 20 78 43 72 65 61 74 65 29 20 73 70 6c 69 74  r xCreate) split
35fbc 20 74 68 65 20 73 74 72 69 6e 67 20 75 70 20 69   the string up i
35fbd 6e 74 6f 20 74 6f 6b 65 6e 73 2e 0a 2a 2a 20 52  nto tokens..** R
35fbe 65 74 75 72 6e 20 61 6e 20 61 72 72 61 79 20 6f  eturn an array o
35fbf 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 27 5c  f pointers to '\
35fc0 30 30 30 27 20 74 65 72 6d 69 6e 61 74 65 64 20  000' terminated 
35fc1 73 74 72 69 6e 67 73 2c 20 6f 6e 65 20 73 74 72  strings, one str
35fc2 69 6e 67 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20  ing.** for each 
35fc3 6e 6f 6e 2d 77 68 69 74 65 73 70 61 63 65 20 74  non-whitespace t
35fc4 6f 6b 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  oken..**.** The 
35fc5 72 65 74 75 72 6e 65 64 20 61 72 72 61 79 20 69  returned array i
35fc6 73 20 74 65 72 6d 69 6e 61 74 65 64 20 62 79 20  s terminated by 
35fc7 61 20 73 69 6e 67 6c 65 20 4e 55 4c 4c 20 70 6f  a single NULL po
35fc8 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 53 70 61  inter..**.** Spa
35fc9 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ce to hold the r
35fca 65 74 75 72 6e 65 64 20 61 72 72 61 79 20 69 73  eturned array is
35fcb 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61   obtained from a
35fcc 20 73 69 6e 67 6c 65 0a 2a 2a 20 6d 61 6c 6c 6f   single.** mallo
35fcd 63 20 61 6e 64 20 73 68 6f 75 6c 64 20 62 65 20  c and should be 
35fce 66 72 65 65 64 20 62 79 20 70 61 73 73 69 6e 67  freed by passing
35fcf 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
35fd0 65 20 74 6f 20 66 72 65 65 28 29 2e 0a 2a 2a 20  e to free()..** 
35fd1 54 68 65 20 69 6e 64 69 76 69 64 75 61 6c 20 73  The individual s
35fd2 74 72 69 6e 67 73 20 77 69 74 68 69 6e 20 74 68  trings within th
35fd3 65 20 74 6f 6b 65 6e 20 6c 69 73 74 20 61 72 65  e token list are
35fd4 20 61 6c 6c 20 61 20 70 61 72 74 20 6f 66 0a 2a   all a part of.*
35fd5 2a 20 74 68 65 20 73 69 6e 67 6c 65 20 6d 65 6d  * the single mem
35fd6 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 61  ory allocation a
35fd7 6e 64 20 77 69 6c 6c 20 61 6c 6c 20 62 65 20 66  nd will all be f
35fd8 72 65 65 64 20 61 74 20 6f 6e 63 65 2e 0a 2a 2f  reed at once..*/
35fd9 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 2a 74  .static char **t
35fda 6f 6b 65 6e 69 7a 65 53 74 72 69 6e 67 28 63 6f  okenizeString(co
35fdb 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
35fdc 20 2a 70 6e 54 6f 6b 65 6e 29 7b 0a 20 20 69 6e   *pnToken){.  in
35fdd 74 20 6e 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20  t nToken = 0;.  
35fde 46 74 73 54 6f 6b 65 6e 20 2a 61 54 6f 6b 65 6e  FtsToken *aToken
35fdf 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   = sqlite3_mallo
35fe0 63 28 20 73 74 72 6c 65 6e 28 7a 29 20 2a 20 73  c( strlen(z) * s
35fe1 69 7a 65 6f 66 28 61 54 6f 6b 65 6e 5b 30 5d 29  izeof(aToken[0])
35fe2 20 29 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 31 3b   );.  int n = 1;
35fe3 0a 20 20 69 6e 74 20 65 2c 20 69 3b 0a 20 20 69  .  int e, i;.  i
35fe4 6e 74 20 74 6f 74 61 6c 53 69 7a 65 20 3d 20 30  nt totalSize = 0
35fe5 3b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 54 6f 6b  ;.  char **azTok
35fe6 65 6e 3b 0a 20 20 63 68 61 72 20 2a 7a 43 6f 70  en;.  char *zCop
35fe7 79 3b 0a 20 20 77 68 69 6c 65 28 20 6e 3e 30 20  y;.  while( n>0 
35fe8 29 7b 0a 20 20 20 20 6e 20 3d 20 66 74 73 47 65  ){.    n = ftsGe
35fe9 74 54 6f 6b 65 6e 28 7a 2c 20 26 65 29 3b 0a 20  tToken(z, &e);. 
35fea 20 20 20 69 66 28 20 65 21 3d 54 4f 4b 45 4e 5f     if( e!=TOKEN_
35feb 53 50 41 43 45 20 29 7b 0a 20 20 20 20 20 20 61  SPACE ){.      a
35fec 54 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d 2e 7a 20  Token[nToken].z 
35fed 3d 20 7a 3b 0a 20 20 20 20 20 20 61 54 6f 6b 65  = z;.      aToke
35fee 6e 5b 6e 54 6f 6b 65 6e 5d 2e 6e 20 3d 20 6e 3b  n[nToken].n = n;
35fef 0a 20 20 20 20 20 20 6e 54 6f 6b 65 6e 2b 2b 3b  .      nToken++;
35ff0 0a 20 20 20 20 20 20 74 6f 74 61 6c 53 69 7a 65  .      totalSize
35ff1 20 2b 3d 20 6e 2b 31 3b 0a 20 20 20 20 7d 0a 20   += n+1;.    }. 
35ff2 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20 20 7d 0a 20     z += n;.  }. 
35ff3 20 61 7a 54 6f 6b 65 6e 20 3d 20 28 63 68 61 72   azToken = (char
35ff4 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  **)sqlite3_mallo
35ff5 63 28 20 6e 54 6f 6b 65 6e 2a 73 69 7a 65 6f 66  c( nToken*sizeof
35ff6 28 63 68 61 72 2a 29 20 2b 20 74 6f 74 61 6c 53  (char*) + totalS
35ff7 69 7a 65 20 29 3b 0a 20 20 7a 43 6f 70 79 20 3d  ize );.  zCopy =
35ff8 20 28 63 68 61 72 2a 29 26 61 7a 54 6f 6b 65 6e   (char*)&azToken
35ff9 5b 6e 54 6f 6b 65 6e 5d 3b 0a 20 20 6e 54 6f 6b  [nToken];.  nTok
35ffa 65 6e 2d 2d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  en--;.  for(i=0;
35ffb 20 69 3c 6e 54 6f 6b 65 6e 3b 20 69 2b 2b 29 7b   i<nToken; i++){
35ffc 0a 20 20 20 20 61 7a 54 6f 6b 65 6e 5b 69 5d 20  .    azToken[i] 
35ffd 3d 20 7a 43 6f 70 79 3b 0a 20 20 20 20 6e 20 3d  = zCopy;.    n =
35ffe 20 61 54 6f 6b 65 6e 5b 69 5d 2e 6e 3b 0a 20 20   aToken[i].n;.  
35fff 20 20 6d 65 6d 63 70 79 28 7a 43 6f 70 79 2c 20    memcpy(zCopy, 
36000 61 54 6f 6b 65 6e 5b 69 5d 2e 7a 2c 20 6e 29 3b  aToken[i].z, n);
36001 0a 20 20 20 20 7a 43 6f 70 79 5b 6e 5d 20 3d 20  .    zCopy[n] = 
36002 30 3b 0a 20 20 20 20 7a 43 6f 70 79 20 2b 3d 20  0;.    zCopy += 
36003 6e 2b 31 3b 0a 20 20 7d 0a 20 20 61 7a 54 6f 6b  n+1;.  }.  azTok
36004 65 6e 5b 6e 54 6f 6b 65 6e 5d 20 3d 20 30 3b 0a  en[nToken] = 0;.
36005 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61    sqlite3_free(a
36006 54 6f 6b 65 6e 29 3b 0a 20 20 2a 70 6e 54 6f 6b  Token);.  *pnTok
36007 65 6e 20 3d 20 6e 54 6f 6b 65 6e 3b 0a 20 20 72  en = nToken;.  r
36008 65 74 75 72 6e 20 61 7a 54 6f 6b 65 6e 3b 0a 7d  eturn azToken;.}
36009 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ../*.** Convert 
3600a 61 6e 20 53 51 4c 2d 73 74 79 6c 65 20 71 75 6f  an SQL-style quo
3600b 74 65 64 20 73 74 72 69 6e 67 20 69 6e 74 6f 20  ted string into 
3600c 61 20 6e 6f 72 6d 61 6c 20 73 74 72 69 6e 67 20  a normal string 
3600d 62 79 20 72 65 6d 6f 76 69 6e 67 0a 2a 2a 20 74  by removing.** t
3600e 68 65 20 71 75 6f 74 65 20 63 68 61 72 61 63 74  he quote charact
3600f 65 72 73 2e 20 20 54 68 65 20 63 6f 6e 76 65 72  ers.  The conver
36010 73 69 6f 6e 20 69 73 20 64 6f 6e 65 20 69 6e 2d  sion is done in-
36011 70 6c 61 63 65 2e 20 20 49 66 20 74 68 65 0a 2a  place.  If the.*
36012 2a 20 69 6e 70 75 74 20 64 6f 65 73 20 6e 6f 74  * input does not
36013 20 62 65 67 69 6e 20 77 69 74 68 20 61 20 71 75   begin with a qu
36014 6f 74 65 20 63 68 61 72 61 63 74 65 72 2c 20 74  ote character, t
36015 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
36016 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  .** is a no-op..
36017 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 73 3a 0a  **.** Examples:.
36018 2a 2a 0a 2a 2a 20 20 20 20 20 22 61 62 63 22 20  **.**     "abc" 
36019 20 20 62 65 63 6f 6d 65 73 20 20 20 61 62 63 0a    becomes   abc.
3601a 2a 2a 20 20 20 20 20 27 78 79 7a 27 20 20 20 62  **     'xyz'   b
3601b 65 63 6f 6d 65 73 20 20 20 78 79 7a 0a 2a 2a 20  ecomes   xyz.** 
3601c 20 20 20 20 5b 70 71 72 5d 20 20 20 62 65 63 6f      [pqr]   beco
3601d 6d 65 73 20 20 20 70 71 72 0a 2a 2a 20 20 20 20  mes   pqr.**    
3601e 20 60 6d 6e 6f 60 20 20 20 62 65 63 6f 6d 65 73   `mno`   becomes
3601f 20 20 20 6d 6e 6f 0a 2a 2f 0a 73 74 61 74 69 63     mno.*/.static
36020 20 76 6f 69 64 20 64 65 71 75 6f 74 65 53 74 72   void dequoteStr
36021 69 6e 67 28 63 68 61 72 20 2a 7a 29 7b 0a 20 20  ing(char *z){.  
36022 69 6e 74 20 71 75 6f 74 65 3b 0a 20 20 69 6e 74  int quote;.  int
36023 20 69 2c 20 6a 3b 0a 20 20 69 66 28 20 7a 3d 3d   i, j;.  if( z==
36024 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 71 75  0 ) return;.  qu
36025 6f 74 65 20 3d 20 7a 5b 30 5d 3b 0a 20 20 73 77  ote = z[0];.  sw
36026 69 74 63 68 28 20 71 75 6f 74 65 20 29 7b 0a 20  itch( quote ){. 
36027 20 20 20 63 61 73 65 20 27 5c 27 27 3a 20 20 62     case '\'':  b
36028 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27  reak;.    case '
36029 22 27 3a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  "':   break;.   
3602a 20 63 61 73 65 20 27 60 27 3a 20 20 20 62 72 65   case '`':   bre
3602b 61 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ak;             
3602c 20 20 20 2f 2a 20 46 6f 72 20 4d 79 53 51 4c 20     /* For MySQL 
3602d 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f  compatibility */
3602e 0a 20 20 20 20 63 61 73 65 20 27 5b 27 3a 20 20  .    case '[':  
3602f 20 71 75 6f 74 65 20 3d 20 27 5d 27 3b 20 20 62   quote = ']';  b
36030 72 65 61 6b 3b 20 20 2f 2a 20 46 6f 72 20 4d 53  reak;  /* For MS
36031 20 53 71 6c 53 65 72 76 65 72 20 63 6f 6d 70 61   SqlServer compa
36032 74 69 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20  tibility */.    
36033 64 65 66 61 75 6c 74 3a 20 20 20 20 72 65 74 75  default:    retu
36034 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  rn;.  }.  for(i=
36035 31 2c 20 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b  1, j=0; z[i]; i+
36036 2b 29 7b 0a 20 20 20 20 69 66 28 20 7a 5b 69 5d  +){.    if( z[i]
36037 3d 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 20  ==quote ){.     
36038 20 69 66 28 20 7a 5b 69 2b 31 5d 3d 3d 71 75 6f   if( z[i+1]==quo
36039 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 5b  te ){.        z[
3603a 6a 2b 2b 5d 20 3d 20 71 75 6f 74 65 3b 0a 20 20  j++] = quote;.  
3603b 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20        i++;.     
3603c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
3603d 7a 5b 6a 2b 2b 5d 20 3d 20 30 3b 0a 20 20 20 20  z[j++] = 0;.    
3603e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
3603f 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
36040 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69      z[j++] = z[i
36041 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  ];.    }.  }.}..
36042 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20  /*.** The input 
36043 61 7a 49 6e 20 69 73 20 61 20 4e 55 4c 4c 2d 74  azIn is a NULL-t
36044 65 72 6d 69 6e 61 74 65 64 20 6c 69 73 74 20 6f  erminated list o
36045 66 20 74 6f 6b 65 6e 73 2e 20 20 52 65 6d 6f 76  f tokens.  Remov
36046 65 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 74  e the first.** t
36047 6f 6b 65 6e 20 61 6e 64 20 61 6c 6c 20 70 75 6e  oken and all pun
36048 63 74 75 61 74 69 6f 6e 20 74 6f 6b 65 6e 73 2e  ctuation tokens.
36049 20 20 52 65 6d 6f 76 65 20 74 68 65 20 71 75 6f    Remove the quo
3604a 74 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 72 6f 75  tes from.** arou
3604b 6e 64 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61  nd string litera
3604c 6c 20 74 6f 6b 65 6e 73 2e 0a 2a 2a 0a 2a 2a 20  l tokens..**.** 
3604d 45 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20  Example:.**.**  
3604e 20 20 20 69 6e 70 75 74 3a 20 20 20 20 20 20 74     input:      t
3604f 6f 6b 65 6e 69 7a 65 20 63 68 69 6e 65 73 65 20  okenize chinese 
36050 28 20 27 73 69 6d 70 6c 69 66 65 64 27 20 2c 20  ( 'simplifed' , 
36051 27 6d 69 78 65 64 27 20 29 0a 2a 2a 20 20 20 20  'mixed' ).**    
36052 20 6f 75 74 70 75 74 3a 20 20 20 20 20 63 68 69   output:     chi
36053 6e 65 73 65 20 73 69 6d 70 6c 69 66 65 64 20 6d  nese simplifed m
36054 69 78 65 64 0a 2a 2a 0a 2a 2a 20 41 6e 6f 74 68  ixed.**.** Anoth
36055 65 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a  er example:.**.*
36056 2a 20 20 20 20 20 69 6e 70 75 74 3a 20 20 20 20  *     input:    
36057 20 20 64 65 6c 69 6d 69 74 65 72 73 20 28 20 27    delimiters ( '
36058 5b 27 20 2c 20 27 5d 27 20 2c 20 27 2e 2e 2e 27  [' , ']' , '...'
36059 20 29 0a 2a 2a 20 20 20 20 20 6f 75 74 70 75 74   ).**     output
3605a 3a 20 20 20 20 20 5b 20 5d 20 2e 2e 2e 0a 2a 2f  :     [ ] ....*/
3605b 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 6f 6b  .static void tok
3605c 65 6e 4c 69 73 74 54 6f 49 64 4c 69 73 74 28 63  enListToIdList(c
3605d 68 61 72 20 2a 2a 61 7a 49 6e 29 7b 0a 20 20 69  har **azIn){.  i
3605e 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28 20 61  nt i, j;.  if( a
3605f 7a 49 6e 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  zIn ){.    for(i
36060 3d 30 2c 20 6a 3d 2d 31 3b 20 61 7a 49 6e 5b 69  =0, j=-1; azIn[i
36061 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  ]; i++){.      i
36062 66 28 20 73 61 66 65 5f 69 73 61 6c 6e 75 6d 28  f( safe_isalnum(
36063 61 7a 49 6e 5b 69 5d 5b 30 5d 29 20 7c 7c 20 61  azIn[i][0]) || a
36064 7a 49 6e 5b 69 5d 5b 31 5d 20 29 7b 0a 20 20 20  zIn[i][1] ){.   
36065 20 20 20 20 20 64 65 71 75 6f 74 65 53 74 72 69       dequoteStri
36066 6e 67 28 61 7a 49 6e 5b 69 5d 29 3b 0a 20 20 20  ng(azIn[i]);.   
36067 20 20 20 20 20 69 66 28 20 6a 3e 3d 30 20 29 7b       if( j>=0 ){
36068 0a 20 20 20 20 20 20 20 20 20 20 61 7a 49 6e 5b  .          azIn[
36069 6a 5d 20 3d 20 61 7a 49 6e 5b 69 5d 3b 0a 20 20  j] = azIn[i];.  
3606a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
3606b 6a 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  j++;.      }.   
3606c 20 7d 0a 20 20 20 20 61 7a 49 6e 5b 6a 5d 20 3d   }.    azIn[j] =
3606d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a   0;.  }.}.../*.*
3606e 2a 20 46 69 6e 64 20 74 68 65 20 66 69 72 73 74  * Find the first
3606f 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 20 74 6f   alphanumeric to
36070 6b 65 6e 20 69 6e 20 74 68 65 20 73 74 72 69 6e  ken in the strin
36071 67 20 7a 49 6e 2e 20 20 4e 75 6c 6c 2d 74 65 72  g zIn.  Null-ter
36072 6d 69 6e 61 74 65 0a 2a 2a 20 74 68 69 73 20 74  minate.** this t
36073 6f 6b 65 6e 2e 20 20 52 65 6d 6f 76 65 20 61 6e  oken.  Remove an
36074 79 20 71 75 6f 74 61 74 69 6f 6e 20 6d 61 72 6b  y quotation mark
36075 73 2e 20 20 41 6e 64 20 72 65 74 75 72 6e 20 61  s.  And return a
36076 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74   pointer to.** t
36077 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a 73 74  he result..*/.st
36078 61 74 69 63 20 63 68 61 72 20 2a 66 69 72 73 74  atic char *first
36079 54 6f 6b 65 6e 28 63 68 61 72 20 2a 7a 49 6e 2c  Token(char *zIn,
3607a 20 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 29 7b   char **pzTail){
3607b 0a 20 20 69 6e 74 20 6e 2c 20 74 74 79 70 65 3b  .  int n, ttype;
3607c 0a 20 20 77 68 69 6c 65 28 31 29 7b 0a 20 20 20  .  while(1){.   
3607d 20 6e 20 3d 20 66 74 73 47 65 74 54 6f 6b 65 6e   n = ftsGetToken
3607e 28 7a 49 6e 2c 20 26 74 74 79 70 65 29 3b 0a 20  (zIn, &ttype);. 
3607f 20 20 20 69 66 28 20 74 74 79 70 65 3d 3d 54 4f     if( ttype==TO
36080 4b 45 4e 5f 53 50 41 43 45 20 29 7b 0a 20 20 20  KEN_SPACE ){.   
36081 20 20 20 7a 49 6e 20 2b 3d 20 6e 3b 0a 20 20 20     zIn += n;.   
36082 20 7d 65 6c 73 65 20 69 66 28 20 74 74 79 70 65   }else if( ttype
36083 3d 3d 54 4f 4b 45 4e 5f 45 4f 46 20 29 7b 0a 20  ==TOKEN_EOF ){. 
36084 20 20 20 20 20 2a 70 7a 54 61 69 6c 20 3d 20 7a       *pzTail = z
36085 49 6e 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  In;.      return
36086 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   0;.    }else{. 
36087 20 20 20 20 20 7a 49 6e 5b 6e 5d 20 3d 20 30 3b       zIn[n] = 0;
36088 0a 20 20 20 20 20 20 2a 70 7a 54 61 69 6c 20 3d  .      *pzTail =
36089 20 26 7a 49 6e 5b 31 5d 3b 0a 20 20 20 20 20 20   &zIn[1];.      
3608a 64 65 71 75 6f 74 65 53 74 72 69 6e 67 28 7a 49  dequoteString(zI
3608b 6e 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  n);.      return
3608c 20 7a 49 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   zIn;.    }.  }.
3608d 20 20 2f 2a 4e 4f 54 52 45 41 43 48 45 44 2a 2f    /*NOTREACHED*/
3608e 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 74 72  .}../* Return tr
3608f 75 65 20 69 66 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 20  ue if....**.**  
36090 20 2a 20 20 73 20 62 65 67 69 6e 73 20 77 69 74   *  s begins wit
36091 68 20 74 68 65 20 73 74 72 69 6e 67 20 74 2c 20  h the string t, 
36092 69 67 6e 6f 72 69 6e 67 20 63 61 73 65 0a 2a 2a  ignoring case.**
36093 20 20 20 2a 20 20 73 20 69 73 20 6c 6f 6e 67 65     *  s is longe
36094 72 20 74 68 61 6e 20 74 0a 2a 2a 20 20 20 2a 20  r than t.**   * 
36095 20 54 68 65 20 66 69 72 73 74 20 63 68 61 72 61   The first chara
36096 63 74 65 72 20 6f 66 20 73 20 62 65 79 6f 6e 64  cter of s beyond
36097 20 74 20 69 73 20 6e 6f 74 20 61 20 61 6c 70 68   t is not a alph
36098 61 6e 75 6d 65 72 69 63 0a 2a 2a 20 0a 2a 2a 20  anumeric.** .** 
36099 49 67 6e 6f 72 65 20 6c 65 61 64 69 6e 67 20 73  Ignore leading s
3609a 70 61 63 65 20 69 6e 20 2a 73 2e 0a 2a 2a 0a 2a  pace in *s..**.*
3609b 2a 20 54 6f 20 70 75 74 20 69 74 20 61 6e 6f 74  * To put it anot
3609c 68 65 72 20 77 61 79 2c 20 72 65 74 75 72 6e 20  her way, return 
3609d 74 72 75 65 20 69 66 20 74 68 65 20 66 69 72 73  true if the firs
3609e 74 20 74 6f 6b 65 6e 20 6f 66 0a 2a 2a 20 73 5b  t token of.** s[
3609f 5d 20 69 73 20 74 5b 5d 2e 0a 2a 2f 0a 73 74 61  ] is t[]..*/.sta
360a0 74 69 63 20 69 6e 74 20 73 74 61 72 74 73 57 69  tic int startsWi
360a1 74 68 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  th(const char *s
360a2 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 74 29  , const char *t)
360a3 7b 0a 20 20 77 68 69 6c 65 28 20 73 61 66 65 5f  {.  while( safe_
360a4 69 73 73 70 61 63 65 28 2a 73 29 20 29 7b 20 73  isspace(*s) ){ s
360a5 2b 2b 3b 20 7d 0a 20 20 77 68 69 6c 65 28 20 2a  ++; }.  while( *
360a6 74 20 29 7b 0a 20 20 20 20 69 66 28 20 73 61 66  t ){.    if( saf
360a7 65 5f 74 6f 6c 6f 77 65 72 28 2a 73 2b 2b 29 21  e_tolower(*s++)!
360a8 3d 73 61 66 65 5f 74 6f 6c 6f 77 65 72 28 2a 74  =safe_tolower(*t
360a9 2b 2b 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ++) ) return 0;.
360aa 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 73 21    }.  return *s!
360ab 3d 27 5f 27 20 26 26 20 21 73 61 66 65 5f 69 73  ='_' && !safe_is
360ac 61 6c 6e 75 6d 28 2a 73 29 3b 0a 7d 0a 0a 2f 2a  alnum(*s);.}../*
360ad 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
360ae 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
360af 65 20 64 65 66 69 6e 65 73 20 74 68 65 20 22 73  e defines the "s
360b0 70 65 63 22 20 6f 66 20 61 0a 2a 2a 20 66 75 6c  pec" of a.** ful
360b1 6c 20 74 65 78 74 20 69 6e 64 65 78 2e 20 20 54  l text index.  T
360b2 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73  his structure is
360b3 20 70 6f 70 75 6c 61 74 65 64 20 62 79 20 70 61   populated by pa
360b4 72 73 65 53 70 65 63 0a 2a 2a 20 61 6e 64 20 75  rseSpec.** and u
360b5 73 65 20 62 79 20 66 75 6c 6c 74 65 78 74 43 6f  se by fulltextCo
360b6 6e 6e 65 63 74 20 61 6e 64 20 66 75 6c 6c 74 65  nnect and fullte
360b7 78 74 43 72 65 61 74 65 2e 0a 2a 2f 0a 74 79 70  xtCreate..*/.typ
360b8 65 64 65 66 20 73 74 72 75 63 74 20 54 61 62 6c  edef struct Tabl
360b9 65 53 70 65 63 20 7b 0a 20 20 63 6f 6e 73 74 20  eSpec {.  const 
360ba 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20  char *zDb;      
360bb 20 20 20 2f 2a 20 4c 6f 67 69 63 61 6c 20 64 61     /* Logical da
360bc 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20  tabase name */. 
360bd 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
360be 6d 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  me;       /* Nam
360bf 65 20 6f 66 20 74 68 65 20 66 75 6c 6c 2d 74 65  e of the full-te
360c0 78 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e  xt index */.  in
360c1 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20  t nColumn;      
360c2 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
360c3 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62   of columns to b
360c4 65 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 63  e indexed */.  c
360c5 68 61 72 20 2a 2a 61 7a 43 6f 6c 75 6d 6e 3b 20  har **azColumn; 
360c6 20 20 20 20 20 20 20 20 2f 2a 20 4f 72 69 67 69          /* Origi
360c7 6e 61 6c 20 6e 61 6d 65 73 20 6f 66 20 63 6f 6c  nal names of col
360c8 75 6d 6e 73 20 74 6f 20 62 65 20 69 6e 64 65 78  umns to be index
360c9 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61  ed */.  char **a
360ca 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 3b 20  zContentColumn; 
360cb 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73   /* Column names
360cc 20 66 6f 72 20 25 5f 63 6f 6e 74 65 6e 74 20 2a   for %_content *
360cd 2f 0a 20 20 63 68 61 72 20 2a 2a 61 7a 54 6f 6b  /.  char **azTok
360ce 65 6e 69 7a 65 72 3b 20 20 20 20 20 20 2f 2a 20  enizer;      /* 
360cf 4e 61 6d 65 20 6f 66 20 74 6f 6b 65 6e 69 7a 65  Name of tokenize
360d0 72 20 61 6e 64 20 69 74 73 20 61 72 67 75 6d 65  r and its argume
360d1 6e 74 73 20 2a 2f 0a 7d 20 54 61 62 6c 65 53 70  nts */.} TableSp
360d2 65 63 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6c 61  ec;../*.** Recla
360d3 69 6d 20 61 6c 6c 20 6f 66 20 74 68 65 20 6d 65  im all of the me
360d4 6d 6f 72 79 20 75 73 65 64 20 62 79 20 61 20 54  mory used by a T
360d5 61 62 6c 65 53 70 65 63 0a 2a 2f 0a 73 74 61 74  ableSpec.*/.stat
360d6 69 63 20 76 6f 69 64 20 63 6c 65 61 72 54 61 62  ic void clearTab
360d7 6c 65 53 70 65 63 28 54 61 62 6c 65 53 70 65 63  leSpec(TableSpec
360d8 20 2a 70 29 20 7b 0a 20 20 73 71 6c 69 74 65 33   *p) {.  sqlite3
360d9 5f 66 72 65 65 28 70 2d 3e 61 7a 43 6f 6c 75 6d  _free(p->azColum
360da 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  n);.  sqlite3_fr
360db 65 65 28 70 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43  ee(p->azContentC
360dc 6f 6c 75 6d 6e 29 3b 0a 20 20 73 71 6c 69 74 65  olumn);.  sqlite
360dd 33 5f 66 72 65 65 28 70 2d 3e 61 7a 54 6f 6b 65  3_free(p->azToke
360de 6e 69 7a 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 50 61  nizer);.}../* Pa
360df 72 73 65 20 61 20 43 52 45 41 54 45 20 56 49 52  rse a CREATE VIR
360e0 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65  TUAL TABLE state
360e1 6d 65 6e 74 2c 20 77 68 69 63 68 20 6c 6f 6f 6b  ment, which look
360e2 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20 2a 0a  s like this:. *.
360e3 20 2a 20 43 52 45 41 54 45 20 56 49 52 54 55 41   * CREATE VIRTUA
360e4 4c 20 54 41 42 4c 45 20 65 6d 61 69 6c 0a 20 2a  L TABLE email. *
360e5 20 20 20 20 20 20 20 20 55 53 49 4e 47 20 66 74          USING ft
360e6 73 33 28 73 75 62 6a 65 63 74 2c 20 62 6f 64 79  s3(subject, body
360e7 2c 20 74 6f 6b 65 6e 69 7a 65 20 6d 79 74 6f 6b  , tokenize mytok
360e8 65 6e 69 7a 65 72 28 6d 79 61 72 67 29 29 0a 20  enizer(myarg)). 
360e9 2a 0a 20 2a 20 57 65 20 72 65 74 75 72 6e 20 70  *. * We return p
360ea 61 72 73 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f  arsed informatio
360eb 6e 20 69 6e 20 61 20 54 61 62 6c 65 53 70 65 63  n in a TableSpec
360ec 20 73 74 72 75 63 74 75 72 65 2e 0a 20 2a 20 0a   structure.. * .
360ed 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70   */.static int p
360ee 61 72 73 65 53 70 65 63 28 54 61 62 6c 65 53 70  arseSpec(TableSp
360ef 65 63 20 2a 70 53 70 65 63 2c 20 69 6e 74 20 61  ec *pSpec, int a
360f0 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rgc, const char 
360f1 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 20  *const*argv,.   
360f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
360f3 20 20 63 68 61 72 2a 2a 70 7a 45 72 72 29 7b 0a    char**pzErr){.
360f4 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 63 68    int i, n;.  ch
360f5 61 72 20 2a 7a 2c 20 2a 7a 44 75 6d 6d 79 3b 0a  ar *z, *zDummy;.
360f6 20 20 63 68 61 72 20 2a 2a 61 7a 41 72 67 3b 0a    char **azArg;.
360f7 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54    const char *zT
360f8 6f 6b 65 6e 69 7a 65 72 20 3d 20 30 3b 20 20 20  okenizer = 0;   
360f9 20 2f 2a 20 61 72 67 76 5b 5d 20 65 6e 74 72 79   /* argv[] entry
360fa 20 64 65 73 63 72 69 62 69 6e 67 20 74 68 65 20   describing the 
360fb 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 0a 20 20  tokenizer */..  
360fc 61 73 73 65 72 74 28 20 61 72 67 63 3e 3d 33 20  assert( argc>=3 
360fd 29 3b 0a 20 20 2f 2a 20 43 75 72 72 65 6e 74 20  );.  /* Current 
360fe 69 6e 74 65 72 66 61 63 65 3a 0a 20 20 2a 2a 20  interface:.  ** 
360ff 61 72 67 76 5b 30 5d 20 2d 20 6d 6f 64 75 6c 65  argv[0] - module
36100 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 72 67 76 5b   name.  ** argv[
36101 31 5d 20 2d 20 64 61 74 61 62 61 73 65 20 6e 61  1] - database na
36102 6d 65 0a 20 20 2a 2a 20 61 72 67 76 5b 32 5d 20  me.  ** argv[2] 
36103 2d 20 74 61 62 6c 65 20 6e 61 6d 65 0a 20 20 2a  - table name.  *
36104 2a 20 61 72 67 76 5b 33 2e 2e 5d 20 2d 20 63 6f  * argv[3..] - co
36105 6c 75 6d 6e 73 2c 20 6f 70 74 69 6f 6e 61 6c 6c  lumns, optionall
36106 79 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 74 6f  y followed by to
36107 6b 65 6e 69 7a 65 72 20 73 70 65 63 69 66 69 63  kenizer specific
36108 61 74 69 6f 6e 0a 20 20 2a 2a 20 20 20 20 20 20  ation.  **      
36109 20 20 20 20 20 20 20 61 6e 64 20 73 6e 69 70 70         and snipp
3610a 65 74 20 64 65 6c 69 6d 69 74 65 72 73 20 73 70  et delimiters sp
3610b 65 63 69 66 69 63 61 74 69 6f 6e 2e 0a 20 20 2a  ecification..  *
3610c 2f 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 20 63  /..  /* Make a c
3610d 6f 70 79 20 6f 66 20 74 68 65 20 63 6f 6d 70 6c  opy of the compl
3610e 65 74 65 20 61 72 67 76 5b 5d 5b 5d 20 61 72 72  ete argv[][] arr
3610f 61 79 20 69 6e 20 61 20 73 69 6e 67 6c 65 20 61  ay in a single a
36110 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 20 20 2a 2a 20  llocation..  ** 
36111 54 68 65 20 61 72 67 76 5b 5d 5b 5d 20 61 72 72  The argv[][] arr
36112 61 79 20 69 73 20 72 65 61 64 2d 6f 6e 6c 79 20  ay is read-only 
36113 61 6e 64 20 74 72 61 6e 73 69 65 6e 74 2e 20 20  and transient.  
36114 57 65 20 63 61 6e 20 77 72 69 74 65 20 74 6f 20  We can write to 
36115 74 68 65 0a 20 20 2a 2a 20 63 6f 70 79 20 69 6e  the.  ** copy in
36116 20 6f 72 64 65 72 20 74 6f 20 6d 6f 64 69 66 79   order to modify
36117 20 74 68 69 6e 67 73 20 61 6e 64 20 74 68 65 20   things and the 
36118 63 6f 70 79 20 69 73 20 70 65 72 73 69 73 74 65  copy is persiste
36119 6e 74 2e 0a 20 20 2a 2f 0a 20 20 43 4c 45 41 52  nt..  */.  CLEAR
3611a 28 70 53 70 65 63 29 3b 0a 20 20 66 6f 72 28 69  (pSpec);.  for(i
3611b 3d 6e 3d 30 3b 20 69 3c 61 72 67 63 3b 20 69 2b  =n=0; i<argc; i+
3611c 2b 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 73 74 72  +){.    n += str
3611d 6c 65 6e 28 61 72 67 76 5b 69 5d 29 20 2b 20 31  len(argv[i]) + 1
3611e 3b 0a 20 20 7d 0a 20 20 61 7a 41 72 67 20 3d 20  ;.  }.  azArg = 
3611f 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20  sqlite3_malloc( 
36120 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a 61 72  sizeof(char*)*ar
36121 67 63 20 2b 20 6e 20 29 3b 0a 20 20 69 66 28 20  gc + n );.  if( 
36122 61 7a 41 72 67 3d 3d 30 20 29 7b 0a 20 20 20 20  azArg==0 ){.    
36123 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
36124 4d 45 4d 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 28  MEM;.  }.  z = (
36125 63 68 61 72 2a 29 26 61 7a 41 72 67 5b 61 72 67  char*)&azArg[arg
36126 63 5d 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  c];.  for(i=0; i
36127 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20  <argc; i++){.   
36128 20 61 7a 41 72 67 5b 69 5d 20 3d 20 7a 3b 0a 20   azArg[i] = z;. 
36129 20 20 20 73 74 72 63 70 79 28 7a 2c 20 61 72 67     strcpy(z, arg
3612a 76 5b 69 5d 29 3b 0a 20 20 20 20 7a 20 2b 3d 20  v[i]);.    z += 
3612b 73 74 72 6c 65 6e 28 7a 29 2b 31 3b 0a 20 20 7d  strlen(z)+1;.  }
3612c 0a 0a 20 20 2f 2a 20 49 64 65 6e 74 69 66 79 20  ..  /* Identify 
3612d 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  the column names
3612e 20 61 6e 64 20 74 68 65 20 74 6f 6b 65 6e 69 7a   and the tokeniz
3612f 65 72 20 61 6e 64 20 64 65 6c 69 6d 69 74 65 72  er and delimiter
36130 20 61 72 67 75 6d 65 6e 74 73 0a 20 20 2a 2a 20   arguments.  ** 
36131 69 6e 20 74 68 65 20 61 72 67 76 5b 5d 5b 5d 20  in the argv[][] 
36132 61 72 72 61 79 2e 0a 20 20 2a 2f 0a 20 20 70 53  array..  */.  pS
36133 70 65 63 2d 3e 7a 44 62 20 3d 20 61 7a 41 72 67  pec->zDb = azArg
36134 5b 31 5d 3b 0a 20 20 70 53 70 65 63 2d 3e 7a 4e  [1];.  pSpec->zN
36135 61 6d 65 20 3d 20 61 7a 41 72 67 5b 32 5d 3b 0a  ame = azArg[2];.
36136 20 20 70 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e    pSpec->nColumn
36137 20 3d 20 30 3b 0a 20 20 70 53 70 65 63 2d 3e 61   = 0;.  pSpec->a
36138 7a 43 6f 6c 75 6d 6e 20 3d 20 61 7a 41 72 67 3b  zColumn = azArg;
36139 0a 20 20 7a 54 6f 6b 65 6e 69 7a 65 72 20 3d 20  .  zTokenizer = 
3613a 22 74 6f 6b 65 6e 69 7a 65 20 73 69 6d 70 6c 65  "tokenize simple
3613b 22 3b 0a 20 20 66 6f 72 28 69 3d 33 3b 20 69 3c  ";.  for(i=3; i<
3613c 61 72 67 63 3b 20 2b 2b 69 29 7b 0a 20 20 20 20  argc; ++i){.    
3613d 69 66 28 20 73 74 61 72 74 73 57 69 74 68 28 61  if( startsWith(a
3613e 7a 41 72 67 5b 69 5d 2c 22 74 6f 6b 65 6e 69 7a  zArg[i],"tokeniz
3613f 65 22 29 20 29 7b 0a 20 20 20 20 20 20 7a 54 6f  e") ){.      zTo
36140 6b 65 6e 69 7a 65 72 20 3d 20 61 7a 41 72 67 5b  kenizer = azArg[
36141 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  i];.    }else{. 
36142 20 20 20 20 20 7a 20 3d 20 61 7a 41 72 67 5b 70       z = azArg[p
36143 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d  Spec->nColumn] =
36144 20 66 69 72 73 74 54 6f 6b 65 6e 28 61 7a 41 72   firstToken(azAr
36145 67 5b 69 5d 2c 20 26 7a 44 75 6d 6d 79 29 3b 0a  g[i], &zDummy);.
36146 20 20 20 20 20 20 70 53 70 65 63 2d 3e 6e 43 6f        pSpec->nCo
36147 6c 75 6d 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  lumn++;.    }.  
36148 7d 0a 20 20 69 66 28 20 70 53 70 65 63 2d 3e 6e  }.  if( pSpec->n
36149 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20  Column==0 ){.   
3614a 20 61 7a 41 72 67 5b 30 5d 20 3d 20 22 63 6f 6e   azArg[0] = "con
3614b 74 65 6e 74 22 3b 0a 20 20 20 20 70 53 70 65 63  tent";.    pSpec
3614c 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 31 3b 0a 20  ->nColumn = 1;. 
3614d 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6f   }..  /*.  ** Co
3614e 6e 73 74 72 75 63 74 20 74 68 65 20 6c 69 73 74  nstruct the list
3614f 20 6f 66 20 63 6f 6e 74 65 6e 74 20 63 6f 6c 75   of content colu
36150 6d 6e 20 6e 61 6d 65 73 2e 0a 20 20 2a 2a 0a 20  mn names..  **. 
36151 20 2a 2a 20 45 61 63 68 20 63 6f 6e 74 65 6e 74   ** Each content
36152 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 77 69 6c   column name wil
36153 6c 20 62 65 20 6f 66 20 74 68 65 20 66 6f 72 6d  l be of the form
36154 20 63 4e 4e 41 41 41 41 0a 20 20 2a 2a 20 77 68   cNNAAAA.  ** wh
36155 65 72 65 20 4e 4e 20 69 73 20 74 68 65 20 63 6f  ere NN is the co
36156 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 61 6e 64 20  lumn number and 
36157 41 41 41 41 20 69 73 20 74 68 65 20 73 61 6e 69  AAAA is the sani
36158 74 69 7a 65 64 0a 20 20 2a 2a 20 63 6f 6c 75 6d  tized.  ** colum
36159 6e 20 6e 61 6d 65 2e 20 20 22 73 61 6e 69 74 69  n name.  "saniti
3615a 7a 65 64 22 20 6d 65 61 6e 73 20 74 68 61 74 20  zed" means that 
3615b 73 70 65 63 69 61 6c 20 63 68 61 72 61 63 74 65  special characte
3615c 72 73 20 61 72 65 0a 20 20 2a 2a 20 63 6f 6e 76  rs are.  ** conv
3615d 65 72 74 65 64 20 74 6f 20 22 5f 22 2e 20 20 54  erted to "_".  T
3615e 68 65 20 63 4e 4e 20 70 72 65 66 69 78 20 67 75  he cNN prefix gu
3615f 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 61 6c  arantees that al
36160 6c 20 63 6f 6c 75 6d 6e 0a 20 20 2a 2a 20 6e 61  l column.  ** na
36161 6d 65 73 20 61 72 65 20 75 6e 69 71 75 65 2e 0a  mes are unique..
36162 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 41 41    **.  ** The AA
36163 41 41 20 73 75 66 66 69 78 20 69 73 20 6e 6f 74  AA suffix is not
36164 20 73 74 72 69 63 74 6c 79 20 6e 65 63 65 73 73   strictly necess
36165 61 72 79 2e 20 20 49 74 20 69 73 20 69 6e 63 6c  ary.  It is incl
36166 75 64 65 64 0a 20 20 2a 2a 20 66 6f 72 20 74 68  uded.  ** for th
36167 65 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20 6f 66  e convenience of
36168 20 70 65 6f 70 6c 65 20 77 68 6f 20 6d 69 67 68   people who migh
36169 74 20 65 78 61 6d 69 6e 65 20 74 68 65 20 67 65  t examine the ge
3616a 6e 65 72 61 74 65 64 0a 20 20 2a 2a 20 25 5f 63  nerated.  ** %_c
3616b 6f 6e 74 65 6e 74 20 74 61 62 6c 65 20 61 6e 64  ontent table and
3616c 20 77 6f 6e 64 65 72 20 77 68 61 74 20 74 68 65   wonder what the
3616d 20 63 6f 6c 75 6d 6e 73 20 61 72 65 20 75 73 65   columns are use
3616e 64 20 66 6f 72 2e 0a 20 20 2a 2f 0a 20 20 70 53  d for..  */.  pS
3616f 70 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f  pec->azContentCo
36170 6c 75 6d 6e 20 3d 20 73 71 6c 69 74 65 33 5f 6d  lumn = sqlite3_m
36171 61 6c 6c 6f 63 28 20 70 53 70 65 63 2d 3e 6e 43  alloc( pSpec->nC
36172 6f 6c 75 6d 6e 20 2a 20 73 69 7a 65 6f 66 28 63  olumn * sizeof(c
36173 68 61 72 20 2a 29 20 29 3b 0a 20 20 69 66 28 20  har *) );.  if( 
36174 70 53 70 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74  pSpec->azContent
36175 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20  Column==0 ){.   
36176 20 63 6c 65 61 72 54 61 62 6c 65 53 70 65 63 28   clearTableSpec(
36177 70 53 70 65 63 29 3b 0a 20 20 20 20 72 65 74 75  pSpec);.    retu
36178 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
36179 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20  .  }.  for(i=0; 
3617a 69 3c 70 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e  i<pSpec->nColumn
3617b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61 72  ; i++){.    char
3617c 20 2a 70 3b 0a 20 20 20 20 70 53 70 65 63 2d 3e   *p;.    pSpec->
3617d 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 5b  azContentColumn[
3617e 69 5d 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  i] = sqlite3_mpr
3617f 69 6e 74 66 28 22 63 25 64 25 73 22 2c 20 69 2c  intf("c%d%s", i,
36180 20 61 7a 41 72 67 5b 69 5d 29 3b 0a 20 20 20 20   azArg[i]);.    
36181 66 6f 72 20 28 70 20 3d 20 70 53 70 65 63 2d 3e  for (p = pSpec->
36182 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 5b  azContentColumn[
36183 69 5d 3b 20 2a 70 20 3b 20 2b 2b 70 29 20 7b 0a  i]; *p ; ++p) {.
36184 20 20 20 20 20 20 69 66 28 20 21 73 61 66 65 5f        if( !safe_
36185 69 73 61 6c 6e 75 6d 28 2a 70 29 20 29 20 2a 70  isalnum(*p) ) *p
36186 20 3d 20 27 5f 27 3b 0a 20 20 20 20 7d 0a 20 20   = '_';.    }.  
36187 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 61 72  }..  /*.  ** Par
36188 73 65 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72  se the tokenizer
36189 20 73 70 65 63 69 66 69 63 61 74 69 6f 6e 20 73   specification s
3618a 74 72 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 70 53  tring..  */.  pS
3618b 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72  pec->azTokenizer
3618c 20 3d 20 74 6f 6b 65 6e 69 7a 65 53 74 72 69 6e   = tokenizeStrin
3618d 67 28 7a 54 6f 6b 65 6e 69 7a 65 72 2c 20 26 6e  g(zTokenizer, &n
3618e 29 3b 0a 20 20 74 6f 6b 65 6e 4c 69 73 74 54 6f  );.  tokenListTo
3618f 49 64 4c 69 73 74 28 70 53 70 65 63 2d 3e 61 7a  IdList(pSpec->az
36190 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 0a 20 20 72  Tokenizer);..  r
36191 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
36192 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
36193 74 65 20 61 20 43 52 45 41 54 45 20 54 41 42 4c  te a CREATE TABL
36194 45 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  E statement that
36195 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 73   describes the s
36196 63 68 65 6d 61 20 6f 66 0a 2a 2a 20 74 68 65 20  chema of.** the 
36197 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20  virtual table.  
36198 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
36199 20 74 6f 20 74 68 69 73 20 73 63 68 65 6d 61 20   to this schema 
3619a 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 70  string..**.** Sp
3619b 61 63 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20  ace is obtained 
3619c 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 70 72  from sqlite3_mpr
3619d 69 6e 74 66 28 29 20 61 6e 64 20 73 68 6f 75 6c  intf() and shoul
3619e 64 20 62 65 20 66 72 65 65 64 0a 2a 2a 20 75 73  d be freed.** us
3619f 69 6e 67 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ing sqlite3_free
361a0 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68  ()..*/.static ch
361a1 61 72 20 2a 66 75 6c 6c 74 65 78 74 53 63 68 65  ar *fulltextSche
361a2 6d 61 28 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d  ma(.  int nColum
361a3 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
361a4 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
361a5 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 63 6f   columns */.  co
361a6 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a  nst char *const*
361a7 20 61 7a 43 6f 6c 75 6d 6e 2c 20 20 2f 2a 20 4c   azColumn,  /* L
361a8 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a  ist of columns *
361a9 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
361aa 7a 54 61 62 6c 65 4e 61 6d 65 20 20 20 20 20 20  zTableName      
361ab 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
361ac 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69   table */.){.  i
361ad 6e 74 20 69 3b 0a 20 20 63 68 61 72 20 2a 7a 53  nt i;.  char *zS
361ae 63 68 65 6d 61 2c 20 2a 7a 4e 65 78 74 3b 0a 20  chema, *zNext;. 
361af 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 65   const char *zSe
361b0 70 20 3d 20 22 28 22 3b 0a 20 20 7a 53 63 68 65  p = "(";.  zSche
361b1 6d 61 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  ma = sqlite3_mpr
361b2 69 6e 74 66 28 22 43 52 45 41 54 45 20 54 41 42  intf("CREATE TAB
361b3 4c 45 20 78 22 29 3b 0a 20 20 66 6f 72 28 69 3d  LE x");.  for(i=
361b4 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b  0; i<nColumn; i+
361b5 2b 29 7b 0a 20 20 20 20 7a 4e 65 78 74 20 3d 20  +){.    zNext = 
361b6 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
361b7 22 25 73 25 73 25 51 22 2c 20 7a 53 63 68 65 6d  "%s%s%Q", zSchem
361b8 61 2c 20 7a 53 65 70 2c 20 61 7a 43 6f 6c 75 6d  a, zSep, azColum
361b9 6e 5b 69 5d 29 3b 0a 20 20 20 20 73 71 6c 69 74  n[i]);.    sqlit
361ba 65 33 5f 66 72 65 65 28 7a 53 63 68 65 6d 61 29  e3_free(zSchema)
361bb 3b 0a 20 20 20 20 7a 53 63 68 65 6d 61 20 3d 20  ;.    zSchema = 
361bc 7a 4e 65 78 74 3b 0a 20 20 20 20 7a 53 65 70 20  zNext;.    zSep 
361bd 3d 20 22 2c 22 3b 0a 20 20 7d 0a 20 20 7a 4e 65  = ",";.  }.  zNe
361be 78 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  xt = sqlite3_mpr
361bf 69 6e 74 66 28 22 25 73 2c 25 51 20 48 49 44 44  intf("%s,%Q HIDD
361c0 45 4e 22 2c 20 7a 53 63 68 65 6d 61 2c 20 7a 54  EN", zSchema, zT
361c1 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 73 71 6c  ableName);.  sql
361c2 69 74 65 33 5f 66 72 65 65 28 7a 53 63 68 65 6d  ite3_free(zSchem
361c3 61 29 3b 0a 20 20 7a 53 63 68 65 6d 61 20 3d 20  a);.  zSchema = 
361c4 7a 4e 65 78 74 3b 0a 20 20 7a 4e 65 78 74 20 3d  zNext;.  zNext =
361c5 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
361c6 28 22 25 73 2c 64 6f 63 69 64 20 48 49 44 44 45  ("%s,docid HIDDE
361c7 4e 29 22 2c 20 7a 53 63 68 65 6d 61 29 3b 0a 20  N)", zSchema);. 
361c8 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53   sqlite3_free(zS
361c9 63 68 65 6d 61 29 3b 0a 20 20 72 65 74 75 72 6e  chema);.  return
361ca 20 7a 4e 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   zNext;.}../*.**
361cb 20 42 75 69 6c 64 20 61 20 6e 65 77 20 73 71 6c   Build a new sql
361cc 69 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74  ite3_vtab struct
361cd 75 72 65 20 74 68 61 74 20 77 69 6c 6c 20 64 65  ure that will de
361ce 73 63 72 69 62 65 20 74 68 65 0a 2a 2a 20 66 75  scribe the.** fu
361cf 6c 6c 74 65 78 74 20 69 6e 64 65 78 20 64 65 66  lltext index def
361d0 69 6e 65 64 20 62 79 20 73 70 65 63 2e 0a 2a 2f  ined by spec..*/
361d1 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6e 73  .static int cons
361d2 74 72 75 63 74 56 74 61 62 28 0a 20 20 73 71 6c  tructVtab(.  sql
361d3 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
361d4 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53 51         /* The SQ
361d5 4c 69 74 65 20 64 61 74 61 62 61 73 65 20 63 6f  Lite database co
361d6 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 66 74  nnection */.  ft
361d7 73 33 48 61 73 68 20 2a 70 48 61 73 68 2c 20 20  s3Hash *pHash,  
361d8 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20          /* Hash 
361d9 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  table containing
361da 20 74 6f 6b 65 6e 69 7a 65 72 73 20 2a 2f 0a 20   tokenizers */. 
361db 20 54 61 62 6c 65 53 70 65 63 20 2a 73 70 65 63   TableSpec *spec
361dc 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61  ,          /* Pa
361dd 72 73 65 64 20 73 70 65 63 20 69 6e 66 6f 72 6d  rsed spec inform
361de 61 74 69 6f 6e 20 66 72 6f 6d 20 70 61 72 73 65  ation from parse
361df 53 70 65 63 28 29 20 2a 2f 0a 20 20 73 71 6c 69  Spec() */.  sqli
361e0 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54 61  te3_vtab **ppVTa
361e1 62 2c 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74  b,    /* Write t
361e2 68 65 20 72 65 73 75 6c 74 69 6e 67 20 76 74 61  he resulting vta
361e3 62 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65  b structure here
361e4 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45   */.  char **pzE
361e5 72 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rr              
361e6 2f 2a 20 57 72 69 74 65 20 61 6e 79 20 65 72 72  /* Write any err
361e7 6f 72 20 6d 65 73 73 61 67 65 20 68 65 72 65 20  or message here 
361e8 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
361e9 20 20 69 6e 74 20 6e 3b 0a 20 20 66 75 6c 6c 74    int n;.  fullt
361ea 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20 30 3b  ext_vtab *v = 0;
361eb 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
361ec 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
361ed 65 20 2a 6d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 63  e *m = NULL;.  c
361ee 68 61 72 20 2a 73 63 68 65 6d 61 3b 0a 0a 20 20  har *schema;..  
361ef 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54 6f 6b  char const *zTok
361f0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  ;         /* Nam
361f1 65 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 72 20 74  e of tokenizer t
361f2 6f 20 75 73 65 20 66 6f 72 20 74 68 69 73 20 66  o use for this f
361f3 74 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  ts table */.  in
361f4 74 20 6e 54 6f 6b 3b 20 20 20 20 20 20 20 20 20  t nTok;         
361f5 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
361f6 68 20 6f 66 20 7a 54 6f 6b 2c 20 69 6e 63 6c 75  h of zTok, inclu
361f7 64 69 6e 67 20 6e 75 6c 20 74 65 72 6d 69 6e 61  ding nul termina
361f8 74 6f 72 20 2a 2f 0a 0a 20 20 76 20 3d 20 28 66  tor */..  v = (f
361f9 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 20  ulltext_vtab *) 
361fa 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
361fb 69 7a 65 6f 66 28 66 75 6c 6c 74 65 78 74 5f 76  izeof(fulltext_v
361fc 74 61 62 29 29 3b 0a 20 20 69 66 28 20 76 3d 3d  tab));.  if( v==
361fd 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  0 ) return SQLIT
361fe 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 43 4c 45 41 52  E_NOMEM;.  CLEAR
361ff 28 76 29 3b 0a 20 20 2f 2a 20 73 71 6c 69 74 65  (v);.  /* sqlite
36200 20 77 69 6c 6c 20 69 6e 69 74 69 61 6c 69 7a 65   will initialize
36201 20 76 2d 3e 62 61 73 65 20 2a 2f 0a 20 20 76 2d   v->base */.  v-
36202 3e 64 62 20 3d 20 64 62 3b 0a 20 20 76 2d 3e 7a  >db = db;.  v->z
36203 44 62 20 3d 20 73 70 65 63 2d 3e 7a 44 62 3b 20  Db = spec->zDb; 
36204 20 20 20 20 20 20 2f 2a 20 46 72 65 65 64 20 77        /* Freed w
36205 68 65 6e 20 61 7a 43 6f 6c 75 6d 6e 20 69 73 20  hen azColumn is 
36206 66 72 65 65 64 20 2a 2f 0a 20 20 76 2d 3e 7a 4e  freed */.  v->zN
36207 61 6d 65 20 3d 20 73 70 65 63 2d 3e 7a 4e 61 6d  ame = spec->zNam
36208 65 3b 20 20 20 2f 2a 20 46 72 65 65 64 20 77 68  e;   /* Freed wh
36209 65 6e 20 61 7a 43 6f 6c 75 6d 6e 20 69 73 20 66  en azColumn is f
3620a 72 65 65 64 20 2a 2f 0a 20 20 76 2d 3e 6e 43 6f  reed */.  v->nCo
3620b 6c 75 6d 6e 20 3d 20 73 70 65 63 2d 3e 6e 43 6f  lumn = spec->nCo
3620c 6c 75 6d 6e 3b 0a 20 20 76 2d 3e 61 7a 43 6f 6e  lumn;.  v->azCon
3620d 74 65 6e 74 43 6f 6c 75 6d 6e 20 3d 20 73 70 65  tentColumn = spe
3620e 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75  c->azContentColu
3620f 6d 6e 3b 0a 20 20 73 70 65 63 2d 3e 61 7a 43 6f  mn;.  spec->azCo
36210 6e 74 65 6e 74 43 6f 6c 75 6d 6e 20 3d 20 30 3b  ntentColumn = 0;
36211 0a 20 20 76 2d 3e 61 7a 43 6f 6c 75 6d 6e 20 3d  .  v->azColumn =
36212 20 73 70 65 63 2d 3e 61 7a 43 6f 6c 75 6d 6e 3b   spec->azColumn;
36213 0a 20 20 73 70 65 63 2d 3e 61 7a 43 6f 6c 75 6d  .  spec->azColum
36214 6e 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 73 70  n = 0;..  if( sp
36215 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72 3d  ec->azTokenizer=
36216 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
36217 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
36218 20 7d 0a 0a 20 20 7a 54 6f 6b 20 3d 20 73 70 65   }..  zTok = spe
36219 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72 5b 30  c->azTokenizer[0
3621a 5d 3b 20 0a 20 20 69 66 28 20 21 7a 54 6f 6b 20  ]; .  if( !zTok 
3621b 29 7b 0a 20 20 20 20 7a 54 6f 6b 20 3d 20 22 73  ){.    zTok = "s
3621c 69 6d 70 6c 65 22 3b 0a 20 20 7d 0a 20 20 6e 54  imple";.  }.  nT
3621d 6f 6b 20 3d 20 73 74 72 6c 65 6e 28 7a 54 6f 6b  ok = strlen(zTok
3621e 29 2b 31 3b 0a 0a 20 20 6d 20 3d 20 28 73 71 6c  )+1;..  m = (sql
3621f 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
36220 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65 33 46  odule *)sqlite3F
36221 74 73 33 48 61 73 68 46 69 6e 64 28 70 48 61 73  ts3HashFind(pHas
36222 68 2c 20 7a 54 6f 6b 2c 20 6e 54 6f 6b 29 3b 0a  h, zTok, nTok);.
36223 20 20 69 66 28 20 21 6d 20 29 7b 0a 20 20 20 20    if( !m ){.    
36224 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33  *pzErr = sqlite3
36225 5f 6d 70 72 69 6e 74 66 28 22 75 6e 6b 6e 6f 77  _mprintf("unknow
36226 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 25 73 22  n tokenizer: %s"
36227 2c 20 73 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69  , spec->azTokeni
36228 7a 65 72 5b 30 5d 29 3b 0a 20 20 20 20 72 63 20  zer[0]);.    rc 
36229 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
3622a 20 20 20 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20      goto err;.  
3622b 7d 0a 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 73 70  }..  for(n=0; sp
3622c 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72 5b  ec->azTokenizer[
3622d 6e 5d 3b 20 6e 2b 2b 29 7b 7d 0a 20 20 69 66 28  n]; n++){}.  if(
3622e 20 6e 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 6d   n ){.    rc = m
3622f 2d 3e 78 43 72 65 61 74 65 28 6e 2d 31 2c 20 28  ->xCreate(n-1, (
36230 63 6f 6e 73 74 20 63 68 61 72 2a 63 6f 6e 73 74  const char*const
36231 2a 29 26 73 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e  *)&spec->azToken
36232 69 7a 65 72 5b 31 5d 2c 0a 20 20 20 20 20 20 20  izer[1],.       
36233 20 20 20 20 20 20 20 20 20 20 20 20 20 26 76 2d               &v-
36234 3e 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 20 20  >pTokenizer);.  
36235 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
36236 6d 2d 3e 78 43 72 65 61 74 65 28 30 2c 20 30 2c  m->xCreate(0, 0,
36237 20 26 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 29   &v->pTokenizer)
36238 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d  ;.  }.  if( rc!=
36239 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
3623a 20 65 72 72 3b 0a 20 20 76 2d 3e 70 54 6f 6b 65   err;.  v->pToke
3623b 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d  nizer->pModule =
3623c 20 6d 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 20   m;..  /* TODO: 
3623d 76 65 72 69 66 79 20 74 68 65 20 65 78 69 73 74  verify the exist
3623e 65 6e 63 65 20 6f 66 20 62 61 63 6b 69 6e 67 20  ence of backing 
3623f 74 61 62 6c 65 73 20 66 6f 6f 5f 63 6f 6e 74 65  tables foo_conte
36240 6e 74 2c 20 66 6f 6f 5f 74 65 72 6d 20 2a 2f 0a  nt, foo_term */.
36241 0a 20 20 73 63 68 65 6d 61 20 3d 20 66 75 6c 6c  .  schema = full
36242 74 65 78 74 53 63 68 65 6d 61 28 76 2d 3e 6e 43  textSchema(v->nC
36243 6f 6c 75 6d 6e 2c 20 28 63 6f 6e 73 74 20 63 68  olumn, (const ch
36244 61 72 2a 63 6f 6e 73 74 2a 29 76 2d 3e 61 7a 43  ar*const*)v->azC
36245 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20 20 20 20 20  olumn,.         
36246 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36247 20 73 70 65 63 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   spec->zName);. 
36248 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 64 65   rc = sqlite3_de
36249 63 6c 61 72 65 5f 76 74 61 62 28 64 62 2c 20 73  clare_vtab(db, s
3624a 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c 69 74 65  chema);.  sqlite
3624b 33 5f 66 72 65 65 28 73 63 68 65 6d 61 29 3b 0a  3_free(schema);.
3624c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
3624d 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a  _OK ) goto err;.
3624e 0a 20 20 6d 65 6d 73 65 74 28 76 2d 3e 70 46 75  .  memset(v->pFu
3624f 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73  lltextStatements
36250 2c 20 30 2c 20 73 69 7a 65 6f 66 28 76 2d 3e 70  , 0, sizeof(v->p
36251 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e  FulltextStatemen
36252 74 73 29 29 3b 0a 0a 20 20 2f 2a 20 49 6e 64 69  ts));..  /* Indi
36253 63 61 74 65 20 74 68 61 74 20 74 68 65 20 62 75  cate that the bu
36254 66 66 65 72 20 69 73 20 6e 6f 74 20 6c 69 76 65  ffer is not live
36255 2e 20 2a 2f 0a 20 20 76 2d 3e 6e 50 65 6e 64 69  . */.  v->nPendi
36256 6e 67 44 61 74 61 20 3d 20 2d 31 3b 0a 0a 20 20  ngData = -1;..  
36257 2a 70 70 56 54 61 62 20 3d 20 26 76 2d 3e 62 61  *ppVTab = &v->ba
36258 73 65 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28  se;.  FTSTRACE((
36259 22 46 54 53 33 20 43 6f 6e 6e 65 63 74 20 25 70  "FTS3 Connect %p
3625a 5c 6e 22 2c 20 76 29 29 3b 0a 0a 20 20 72 65 74  \n", v));..  ret
3625b 75 72 6e 20 72 63 3b 0a 0a 65 72 72 3a 0a 20 20  urn rc;..err:.  
3625c 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 5f 64 65  fulltext_vtab_de
3625d 73 74 72 6f 79 28 76 29 3b 0a 20 20 72 65 74 75  stroy(v);.  retu
3625e 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63  rn rc;.}..static
3625f 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 43 6f 6e   int fulltextCon
36260 6e 65 63 74 28 0a 20 20 73 71 6c 69 74 65 33 20  nect(.  sqlite3 
36261 2a 64 62 2c 0a 20 20 76 6f 69 64 20 2a 70 41 75  *db,.  void *pAu
36262 78 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 63  x,.  int argc, c
36263 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74  onst char *const
36264 2a 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65 33  *argv,.  sqlite3
36265 5f 76 74 61 62 20 2a 2a 70 70 56 54 61 62 2c 0a  _vtab **ppVTab,.
36266 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 0a 29    char **pzErr.)
36267 7b 0a 20 20 54 61 62 6c 65 53 70 65 63 20 73 70  {.  TableSpec sp
36268 65 63 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 70  ec;.  int rc = p
36269 61 72 73 65 53 70 65 63 28 26 73 70 65 63 2c 20  arseSpec(&spec, 
3626a 61 72 67 63 2c 20 61 72 67 76 2c 20 70 7a 45 72  argc, argv, pzEr
3626b 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  r);.  if( rc!=SQ
3626c 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
3626d 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 63 6f 6e   rc;..  rc = con
3626e 73 74 72 75 63 74 56 74 61 62 28 64 62 2c 20 28  structVtab(db, (
3626f 66 74 73 33 48 61 73 68 20 2a 29 70 41 75 78 2c  fts3Hash *)pAux,
36270 20 26 73 70 65 63 2c 20 70 70 56 54 61 62 2c 20   &spec, ppVTab, 
36271 70 7a 45 72 72 29 3b 0a 20 20 63 6c 65 61 72 54  pzErr);.  clearT
36272 61 62 6c 65 53 70 65 63 28 26 73 70 65 63 29 3b  ableSpec(&spec);
36273 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
36274 0a 2f 2a 20 54 68 65 20 25 5f 63 6f 6e 74 65 6e  ./* The %_conten
36275 74 20 74 61 62 6c 65 20 68 6f 6c 64 73 20 74 68  t table holds th
36276 65 20 74 65 78 74 20 6f 66 20 65 61 63 68 20 64  e text of each d
36277 6f 63 75 6d 65 6e 74 2c 20 77 69 74 68 0a 2a 2a  ocument, with.**
36278 20 74 68 65 20 64 6f 63 69 64 20 63 6f 6c 75 6d   the docid colum
36279 6e 20 65 78 70 6f 73 65 64 20 61 73 20 74 68 65  n exposed as the
3627a 20 53 51 4c 69 74 65 20 72 6f 77 69 64 20 66 6f   SQLite rowid fo
3627b 72 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a  r the table..*/.
3627c 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54  /* TODO(shess) T
3627d 68 69 73 20 63 6f 6d 6d 65 6e 74 20 6e 65 65 64  his comment need
3627e 73 20 65 6c 61 62 6f 72 61 74 69 6f 6e 20 74 6f  s elaboration to
3627f 20 6d 61 74 63 68 20 74 68 65 20 75 70 64 61 74   match the updat
36280 65 64 0a 2a 2a 20 63 6f 64 65 2e 20 20 57 6f 72  ed.** code.  Wor
36281 6b 20 69 74 20 69 6e 74 6f 20 74 68 65 20 74 6f  k it into the to
36282 70 2d 6f 66 2d 66 69 6c 65 20 63 6f 6d 6d 65 6e  p-of-file commen
36283 74 20 61 74 20 74 68 61 74 20 74 69 6d 65 2e 0a  t at that time..
36284 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75  */.static int fu
36285 6c 6c 74 65 78 74 43 72 65 61 74 65 28 73 71 6c  lltextCreate(sql
36286 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a  ite3 *db, void *
36287 70 41 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20  pAux,.          
36288 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36289 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20  int argc, const 
3628a 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a 61 72  char * const *ar
3628b 67 76 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  gv,.            
3628c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
3628d 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56  lite3_vtab **ppV
3628e 54 61 62 2c 20 63 68 61 72 20 2a 2a 70 7a 45 72  Tab, char **pzEr
3628f 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  r){.  int rc;.  
36290 54 61 62 6c 65 53 70 65 63 20 73 70 65 63 3b 0a  TableSpec spec;.
36291 20 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 73    StringBuffer s
36292 63 68 65 6d 61 3b 0a 20 20 46 54 53 54 52 41 43  chema;.  FTSTRAC
36293 45 28 28 22 46 54 53 33 20 43 72 65 61 74 65 5c  E(("FTS3 Create\
36294 6e 22 29 29 3b 0a 0a 20 20 72 63 20 3d 20 70 61  n"));..  rc = pa
36295 72 73 65 53 70 65 63 28 26 73 70 65 63 2c 20 61  rseSpec(&spec, a
36296 72 67 63 2c 20 61 72 67 76 2c 20 70 7a 45 72 72  rgc, argv, pzErr
36297 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
36298 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
36299 72 63 3b 0a 0a 20 20 69 6e 69 74 53 74 72 69 6e  rc;..  initStrin
3629a 67 42 75 66 66 65 72 28 26 73 63 68 65 6d 61 29  gBuffer(&schema)
3629b 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 63 68 65  ;.  append(&sche
3629c 6d 61 2c 20 22 43 52 45 41 54 45 20 54 41 42 4c  ma, "CREATE TABL
3629d 45 20 25 5f 63 6f 6e 74 65 6e 74 28 22 29 3b 0a  E %_content(");.
3629e 20 20 61 70 70 65 6e 64 28 26 73 63 68 65 6d 61    append(&schema
3629f 2c 20 22 20 20 64 6f 63 69 64 20 49 4e 54 45 47  , "  docid INTEG
362a0 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c 22  ER PRIMARY KEY,"
362a1 29 3b 0a 20 20 61 70 70 65 6e 64 4c 69 73 74 28  );.  appendList(
362a2 26 73 63 68 65 6d 61 2c 20 73 70 65 63 2e 6e 43  &schema, spec.nC
362a3 6f 6c 75 6d 6e 2c 20 73 70 65 63 2e 61 7a 43 6f  olumn, spec.azCo
362a4 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20  ntentColumn);.  
362a5 61 70 70 65 6e 64 28 26 73 63 68 65 6d 61 2c 20  append(&schema, 
362a6 22 29 22 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  ")");.  rc = sql
362a7 5f 65 78 65 63 28 64 62 2c 20 73 70 65 63 2e 7a  _exec(db, spec.z
362a8 44 62 2c 20 73 70 65 63 2e 7a 4e 61 6d 65 2c 20  Db, spec.zName, 
362a9 73 74 72 69 6e 67 42 75 66 66 65 72 44 61 74 61  stringBufferData
362aa 28 26 73 63 68 65 6d 61 29 29 3b 0a 20 20 73 74  (&schema));.  st
362ab 72 69 6e 67 42 75 66 66 65 72 44 65 73 74 72 6f  ringBufferDestro
362ac 79 28 26 73 63 68 65 6d 61 29 3b 0a 20 20 69 66  y(&schema);.  if
362ad 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
362ae 29 20 67 6f 74 6f 20 6f 75 74 3b 0a 0a 20 20 72  ) goto out;..  r
362af 63 20 3d 20 73 71 6c 5f 65 78 65 63 28 64 62 2c  c = sql_exec(db,
362b0 20 73 70 65 63 2e 7a 44 62 2c 20 73 70 65 63 2e   spec.zDb, spec.
362b1 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20  zName,.         
362b2 20 20 20 20 20 20 20 22 63 72 65 61 74 65 20 74         "create t
362b3 61 62 6c 65 20 25 5f 73 65 67 6d 65 6e 74 73 28  able %_segments(
362b4 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
362b5 20 20 22 20 20 62 6c 6f 63 6b 69 64 20 49 4e 54    "  blockid INT
362b6 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
362b7 2c 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,".             
362b8 20 20 20 22 20 20 62 6c 6f 63 6b 20 62 6c 6f 62     "  block blob
362b9 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
362ba 20 20 22 29 3b 22 0a 20 20 20 20 20 20 20 20 20    ");".         
362bb 20 20 20 20 20 20 20 29 3b 0a 20 20 69 66 28 20         );.  if( 
362bc 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
362bd 67 6f 74 6f 20 6f 75 74 3b 0a 0a 20 20 72 63 20  goto out;..  rc 
362be 3d 20 73 71 6c 5f 65 78 65 63 28 64 62 2c 20 73  = sql_exec(db, s
362bf 70 65 63 2e 7a 44 62 2c 20 73 70 65 63 2e 7a 4e  pec.zDb, spec.zN
362c0 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ame,.           
362c1 20 20 20 20 20 22 63 72 65 61 74 65 20 74 61 62       "create tab
362c2 6c 65 20 25 5f 73 65 67 64 69 72 28 22 0a 20 20  le %_segdir(".  
362c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 20                " 
362c4 20 6c 65 76 65 6c 20 69 6e 74 65 67 65 72 2c 22   level integer,"
362c5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
362c6 20 22 20 20 69 64 78 20 69 6e 74 65 67 65 72 2c   "  idx integer,
362c7 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
362c8 20 20 22 20 20 73 74 61 72 74 5f 62 6c 6f 63 6b    "  start_block
362c9 20 69 6e 74 65 67 65 72 2c 22 0a 20 20 20 20 20   integer,".     
362ca 20 20 20 20 20 20 20 20 20 20 20 22 20 20 6c 65             "  le
362cb 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63 6b 20 69  aves_end_block i
362cc 6e 74 65 67 65 72 2c 22 0a 20 20 20 20 20 20 20  nteger,".       
362cd 20 20 20 20 20 20 20 20 20 22 20 20 65 6e 64 5f           "  end_
362ce 62 6c 6f 63 6b 20 69 6e 74 65 67 65 72 2c 22 0a  block integer,".
362cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
362d0 22 20 20 72 6f 6f 74 20 62 6c 6f 62 2c 22 0a 20  "  root blob,". 
362d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
362d2 20 20 70 72 69 6d 61 72 79 20 6b 65 79 28 6c 65    primary key(le
362d3 76 65 6c 2c 20 69 64 78 29 22 0a 20 20 20 20 20  vel, idx)".     
362d4 20 20 20 20 20 20 20 20 20 20 20 22 29 3b 22 29             ");")
362d5 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
362d6 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 6f 75 74  TE_OK ) goto out
362d7 3b 0a 0a 20 20 72 63 20 3d 20 63 6f 6e 73 74 72  ;..  rc = constr
362d8 75 63 74 56 74 61 62 28 64 62 2c 20 28 66 74 73  uctVtab(db, (fts
362d9 33 48 61 73 68 20 2a 29 70 41 75 78 2c 20 26 73  3Hash *)pAux, &s
362da 70 65 63 2c 20 70 70 56 54 61 62 2c 20 70 7a 45  pec, ppVTab, pzE
362db 72 72 29 3b 0a 0a 6f 75 74 3a 0a 20 20 63 6c 65  rr);..out:.  cle
362dc 61 72 54 61 62 6c 65 53 70 65 63 28 26 73 70 65  arTableSpec(&spe
362dd 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  c);.  return rc;
362de 0a 7d 0a 0a 2f 2a 20 44 65 63 69 64 65 20 68 6f  .}../* Decide ho
362df 77 20 74 6f 20 68 61 6e 64 6c 65 20 61 6e 20 53  w to handle an S
362e0 51 4c 20 71 75 65 72 79 2e 20 2a 2f 0a 73 74 61  QL query. */.sta
362e1 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74  tic int fulltext
362e2 42 65 73 74 49 6e 64 65 78 28 73 71 6c 69 74 65  BestIndex(sqlite
362e3 33 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20 73  3_vtab *pVTab, s
362e4 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
362e5 6f 20 2a 70 49 6e 66 6f 29 7b 0a 20 20 66 75 6c  o *pInfo){.  ful
362e6 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20  ltext_vtab *v = 
362e7 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
362e8 29 70 56 54 61 62 3b 0a 20 20 69 6e 74 20 69 3b  )pVTab;.  int i;
362e9 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54  .  FTSTRACE(("FT
362ea 53 33 20 42 65 73 74 49 6e 64 65 78 5c 6e 22 29  S3 BestIndex\n")
362eb 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  );..  for(i=0; i
362ec 3c 70 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61  <pInfo->nConstra
362ed 69 6e 74 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 63  int; ++i){.    c
362ee 6f 6e 73 74 20 73 74 72 75 63 74 20 73 71 6c 69  onst struct sqli
362ef 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72  te3_index_constr
362f0 61 69 6e 74 20 2a 70 43 6f 6e 73 74 72 61 69 6e  aint *pConstrain
362f1 74 3b 0a 20 20 20 20 70 43 6f 6e 73 74 72 61 69  t;.    pConstrai
362f2 6e 74 20 3d 20 26 70 49 6e 66 6f 2d 3e 61 43 6f  nt = &pInfo->aCo
362f3 6e 73 74 72 61 69 6e 74 5b 69 5d 3b 0a 20 20 20  nstraint[i];.   
362f4 20 69 66 28 20 70 43 6f 6e 73 74 72 61 69 6e 74   if( pConstraint
362f5 2d 3e 75 73 61 62 6c 65 20 29 20 7b 0a 20 20 20  ->usable ) {.   
362f6 20 20 20 69 66 28 20 28 70 43 6f 6e 73 74 72 61     if( (pConstra
362f7 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 2d 31  int->iColumn==-1
362f8 20 7c 7c 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d   || pConstraint-
362f9 3e 69 43 6f 6c 75 6d 6e 3d 3d 76 2d 3e 6e 43 6f  >iColumn==v->nCo
362fa 6c 75 6d 6e 2b 31 29 20 26 26 0a 20 20 20 20 20  lumn+1) &&.     
362fb 20 20 20 20 20 70 43 6f 6e 73 74 72 61 69 6e 74       pConstraint
362fc 2d 3e 6f 70 3d 3d 53 51 4c 49 54 45 5f 49 4e 44  ->op==SQLITE_IND
362fd 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 45 51  EX_CONSTRAINT_EQ
362fe 20 29 7b 0a 20 20 20 20 20 20 20 20 70 49 6e 66   ){.        pInf
362ff 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 51 55 45 52  o->idxNum = QUER
36300 59 5f 44 4f 43 49 44 3b 20 20 20 20 20 20 2f 2a  Y_DOCID;      /*
36301 20 6c 6f 6f 6b 75 70 20 62 79 20 64 6f 63 69 64   lookup by docid
36302 20 2a 2f 0a 20 20 20 20 20 20 20 20 46 54 53 54   */.        FTST
36303 52 41 43 45 28 28 22 46 54 53 33 20 51 55 45 52  RACE(("FTS3 QUER
36304 59 5f 44 4f 43 49 44 5c 6e 22 29 29 3b 0a 20 20  Y_DOCID\n"));.  
36305 20 20 20 20 7d 20 65 6c 73 65 20 69 66 28 20 70      } else if( p
36306 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43 6f 6c  Constraint->iCol
36307 75 6d 6e 3e 3d 30 20 26 26 20 70 43 6f 6e 73 74  umn>=0 && pConst
36308 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 3c 3d  raint->iColumn<=
36309 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 26 26 0a 20 20  v->nColumn &&.  
3630a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
3630b 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 6f 70 3d 3d  Constraint->op==
3630c 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
3630d 53 54 52 41 49 4e 54 5f 4d 41 54 43 48 20 29 7b  STRAINT_MATCH ){
3630e 0a 20 20 20 20 20 20 20 20 2f 2a 20 66 75 6c 6c  .        /* full
3630f 2d 74 65 78 74 20 73 65 61 72 63 68 20 2a 2f 0a  -text search */.
36310 20 20 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 69          pInfo->i
36311 64 78 4e 75 6d 20 3d 20 51 55 45 52 59 5f 46 55  dxNum = QUERY_FU
36312 4c 4c 54 45 58 54 20 2b 20 70 43 6f 6e 73 74 72  LLTEXT + pConstr
36313 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20  aint->iColumn;. 
36314 20 20 20 20 20 20 20 46 54 53 54 52 41 43 45 28         FTSTRACE(
36315 28 22 46 54 53 33 20 51 55 45 52 59 5f 46 55 4c  ("FTS3 QUERY_FUL
36316 4c 54 45 58 54 20 25 64 5c 6e 22 2c 20 70 43 6f  LTEXT %d\n", pCo
36317 6e 73 74 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d  nstraint->iColum
36318 6e 29 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73  n));.      } els
36319 65 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20  e continue;..   
3631a 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74     pInfo->aConst
3631b 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e 61 72  raintUsage[i].ar
3631c 67 76 49 6e 64 65 78 20 3d 20 31 3b 0a 20 20 20  gvIndex = 1;.   
3631d 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74     pInfo->aConst
3631e 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e 6f 6d  raintUsage[i].om
3631f 69 74 20 3d 20 31 3b 0a 0a 20 20 20 20 20 20 2f  it = 1;..      /
36320 2a 20 41 6e 20 61 72 62 69 74 72 61 72 79 20 76  * An arbitrary v
36321 61 6c 75 65 20 66 6f 72 20 6e 6f 77 2e 0a 20 20  alue for now..  
36322 20 20 20 20 20 2a 20 54 4f 44 4f 3a 20 50 65 72       * TODO: Per
36323 68 61 70 73 20 64 6f 63 69 64 20 6d 61 74 63 68  haps docid match
36324 65 73 20 73 68 6f 75 6c 64 20 62 65 20 63 6f 6e  es should be con
36325 73 69 64 65 72 65 64 20 63 68 65 61 70 65 72 20  sidered cheaper 
36326 74 68 61 6e 0a 20 20 20 20 20 20 20 2a 20 66 75  than.       * fu
36327 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68 65 73  ll-text searches
36328 2e 20 2a 2f 0a 20 20 20 20 20 20 70 49 6e 66 6f  . */.      pInfo
36329 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74 20  ->estimatedCost 
3632a 3d 20 31 2e 30 3b 20 20 20 0a 0a 20 20 20 20 20  = 1.0;   ..     
3632b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
3632c 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70  K;.    }.  }.  p
3632d 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 51  Info->idxNum = Q
3632e 55 45 52 59 5f 47 45 4e 45 52 49 43 3b 0a 20 20  UERY_GENERIC;.  
3632f 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
36330 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
36331 66 75 6c 6c 74 65 78 74 44 69 73 63 6f 6e 6e 65  fulltextDisconne
36332 63 74 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  ct(sqlite3_vtab 
36333 2a 70 56 54 61 62 29 7b 0a 20 20 46 54 53 54 52  *pVTab){.  FTSTR
36334 41 43 45 28 28 22 46 54 53 33 20 44 69 73 63 6f  ACE(("FTS3 Disco
36335 6e 6e 65 63 74 20 25 70 5c 6e 22 2c 20 70 56 54  nnect %p\n", pVT
36336 61 62 29 29 3b 0a 20 20 66 75 6c 6c 74 65 78 74  ab));.  fulltext
36337 5f 76 74 61 62 5f 64 65 73 74 72 6f 79 28 28 66  _vtab_destroy((f
36338 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 70  ulltext_vtab *)p
36339 56 54 61 62 29 3b 0a 20 20 72 65 74 75 72 6e 20  VTab);.  return 
3633a 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74  SQLITE_OK;.}..st
3633b 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78  atic int fulltex
3633c 74 44 65 73 74 72 6f 79 28 73 71 6c 69 74 65 33  tDestroy(sqlite3
3633d 5f 76 74 61 62 20 2a 70 56 54 61 62 29 7b 0a 20  _vtab *pVTab){. 
3633e 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a   fulltext_vtab *
3633f 76 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 76 74  v = (fulltext_vt
36340 61 62 20 2a 29 70 56 54 61 62 3b 0a 20 20 69 6e  ab *)pVTab;.  in
36341 74 20 72 63 3b 0a 0a 20 20 46 54 53 54 52 41 43  t rc;..  FTSTRAC
36342 45 28 28 22 46 54 53 33 20 44 65 73 74 72 6f 79  E(("FTS3 Destroy
36343 20 25 70 5c 6e 22 2c 20 70 56 54 61 62 29 29 3b   %p\n", pVTab));
36344 0a 20 20 72 63 20 3d 20 73 71 6c 5f 65 78 65 63  .  rc = sql_exec
36345 28 76 2d 3e 64 62 2c 20 76 2d 3e 7a 44 62 2c 20  (v->db, v->zDb, 
36346 76 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20  v->zName,.      
36347 20 20 20 20 20 20 20 20 20 20 22 64 72 6f 70 20            "drop 
36348 74 61 62 6c 65 20 69 66 20 65 78 69 73 74 73 20  table if exists 
36349 25 5f 63 6f 6e 74 65 6e 74 3b 22 0a 20 20 20 20  %_content;".    
3634a 20 20 20 20 20 20 20 20 20 20 20 20 22 64 72 6f              "dro
3634b 70 20 74 61 62 6c 65 20 69 66 20 65 78 69 73 74  p table if exist
3634c 73 20 25 5f 73 65 67 6d 65 6e 74 73 3b 22 0a 20  s %_segments;". 
3634d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
3634e 64 72 6f 70 20 74 61 62 6c 65 20 69 66 20 65 78  drop table if ex
3634f 69 73 74 73 20 25 5f 73 65 67 64 69 72 3b 22 0a  ists %_segdir;".
36350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36351 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
36352 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
36353 72 63 3b 0a 0a 20 20 66 75 6c 6c 74 65 78 74 5f  rc;..  fulltext_
36354 76 74 61 62 5f 64 65 73 74 72 6f 79 28 28 66 75  vtab_destroy((fu
36355 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 70 56  lltext_vtab *)pV
36356 54 61 62 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  Tab);.  return S
36357 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61  QLITE_OK;.}..sta
36358 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74  tic int fulltext
36359 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 74 61  Open(sqlite3_vta
3635a 62 20 2a 70 56 54 61 62 2c 20 73 71 6c 69 74 65  b *pVTab, sqlite
3635b 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 2a  3_vtab_cursor **
3635c 70 70 43 75 72 73 6f 72 29 7b 0a 20 20 66 75 6c  ppCursor){.  ful
3635d 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 63 3b  ltext_cursor *c;
3635e 0a 0a 20 20 63 20 3d 20 28 66 75 6c 6c 74 65 78  ..  c = (fulltex
3635f 74 5f 63 75 72 73 6f 72 20 2a 29 20 73 71 6c 69  t_cursor *) sqli
36360 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
36361 66 28 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f  f(fulltext_curso
36362 72 29 29 3b 0a 20 20 69 66 28 20 63 20 29 7b 0a  r));.  if( c ){.
36363 20 20 20 20 6d 65 6d 73 65 74 28 63 2c 20 30 2c      memset(c, 0,
36364 20 73 69 7a 65 6f 66 28 66 75 6c 6c 74 65 78 74   sizeof(fulltext
36365 5f 63 75 72 73 6f 72 29 29 3b 0a 20 20 20 20 2f  _cursor));.    /
36366 2a 20 73 71 6c 69 74 65 20 77 69 6c 6c 20 69 6e  * sqlite will in
36367 69 74 69 61 6c 69 7a 65 20 63 2d 3e 62 61 73 65  itialize c->base
36368 20 2a 2f 0a 20 20 20 20 2a 70 70 43 75 72 73 6f   */.    *ppCurso
36369 72 20 3d 20 26 63 2d 3e 62 61 73 65 3b 0a 20 20  r = &c->base;.  
3636a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53    FTSTRACE(("FTS
3636b 33 20 4f 70 65 6e 20 25 70 3a 20 25 70 5c 6e 22  3 Open %p: %p\n"
3636c 2c 20 70 56 54 61 62 2c 20 63 29 29 3b 0a 20 20  , pVTab, c));.  
3636d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3636e 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OK;.  }else{.   
3636f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
36370 4f 4d 45 4d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20  OMEM;.  }.}../* 
36371 46 72 65 65 20 61 6c 6c 20 6f 66 20 74 68 65 20  Free all of the 
36372 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f  dynamically allo
36373 63 61 74 65 64 20 6d 65 6d 6f 72 79 20 68 65 6c  cated memory hel
36374 64 20 62 79 20 74 68 65 0a 2a 2a 20 53 6e 69 70  d by the.** Snip
36375 70 65 74 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  pet.*/.static vo
36376 69 64 20 73 6e 69 70 70 65 74 43 6c 65 61 72 28  id snippetClear(
36377 53 6e 69 70 70 65 74 20 2a 70 29 7b 0a 20 20 73  Snippet *p){.  s
36378 71 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61  qlite3_free(p->a
36379 4d 61 74 63 68 29 3b 0a 20 20 73 71 6c 69 74 65  Match);.  sqlite
3637a 33 5f 66 72 65 65 28 70 2d 3e 7a 4f 66 66 73 65  3_free(p->zOffse
3637b 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  t);.  sqlite3_fr
3637c 65 65 28 70 2d 3e 7a 53 6e 69 70 70 65 74 29 3b  ee(p->zSnippet);
3637d 0a 20 20 43 4c 45 41 52 28 70 29 3b 0a 7d 0a 0a  .  CLEAR(p);.}..
3637e 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20 73  /*.** Append a s
3637f 69 6e 67 6c 65 20 65 6e 74 72 79 20 74 6f 20 74  ingle entry to t
36380 68 65 20 70 2d 3e 61 4d 61 74 63 68 5b 5d 20 6c  he p->aMatch[] l
36381 6f 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  og..*/.static vo
36382 69 64 20 73 6e 69 70 70 65 74 41 70 70 65 6e 64  id snippetAppend
36383 4d 61 74 63 68 28 0a 20 20 53 6e 69 70 70 65 74  Match(.  Snippet
36384 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
36385 20 20 20 2f 2a 20 41 70 70 65 6e 64 20 74 68 65     /* Append the
36386 20 65 6e 74 72 79 20 74 6f 20 74 68 69 73 20 73   entry to this s
36387 6e 69 70 70 65 74 20 2a 2f 0a 20 20 69 6e 74 20  nippet */.  int 
36388 69 43 6f 6c 2c 20 69 6e 74 20 69 54 65 72 6d 2c  iCol, int iTerm,
36389 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c        /* The col
3638a 75 6d 6e 20 61 6e 64 20 71 75 65 72 79 20 74 65  umn and query te
3638b 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 6b  rm */.  int iTok
3638c 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  en,             
3638d 20 20 2f 2a 20 4d 61 74 63 68 69 6e 67 20 74 6f    /* Matching to
3638e 6b 65 6e 20 69 6e 20 64 6f 63 75 6d 65 6e 74 20  ken in document 
3638f 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 72 74 2c  */.  int iStart,
36390 20 69 6e 74 20 6e 42 79 74 65 20 20 20 20 20 2f   int nByte     /
36391 2a 20 4f 66 66 73 65 74 20 61 6e 64 20 73 69 7a  * Offset and siz
36392 65 20 6f 66 20 74 68 65 20 6d 61 74 63 68 20 2a  e of the match *
36393 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
36394 73 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61  struct snippetMa
36395 74 63 68 20 2a 70 4d 61 74 63 68 3b 0a 20 20 69  tch *pMatch;.  i
36396 66 28 20 70 2d 3e 6e 4d 61 74 63 68 2b 31 3e 3d  f( p->nMatch+1>=
36397 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20  p->nAlloc ){.   
36398 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e   p->nAlloc = p->
36399 6e 41 6c 6c 6f 63 2a 32 20 2b 20 31 30 3b 0a 20  nAlloc*2 + 10;. 
3639a 20 20 20 70 2d 3e 61 4d 61 74 63 68 20 3d 20 73     p->aMatch = s
3639b 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70  qlite3_realloc(p
3639c 2d 3e 61 4d 61 74 63 68 2c 20 70 2d 3e 6e 41 6c  ->aMatch, p->nAl
3639d 6c 6f 63 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 4d  loc*sizeof(p->aM
3639e 61 74 63 68 5b 30 5d 29 20 29 3b 0a 20 20 20 20  atch[0]) );.    
3639f 69 66 28 20 70 2d 3e 61 4d 61 74 63 68 3d 3d 30  if( p->aMatch==0
363a0 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 4d 61   ){.      p->nMa
363a1 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  tch = 0;.      p
363a2 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 30 3b 0a 20 20  ->nAlloc = 0;.  
363a3 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
363a4 7d 0a 20 20 7d 0a 20 20 69 20 3d 20 70 2d 3e 6e  }.  }.  i = p->n
363a5 4d 61 74 63 68 2b 2b 3b 0a 20 20 70 4d 61 74 63  Match++;.  pMatc
363a6 68 20 3d 20 26 70 2d 3e 61 4d 61 74 63 68 5b 69  h = &p->aMatch[i
363a7 5d 3b 0a 20 20 70 4d 61 74 63 68 2d 3e 69 43 6f  ];.  pMatch->iCo
363a8 6c 20 3d 20 69 43 6f 6c 3b 0a 20 20 70 4d 61 74  l = iCol;.  pMat
363a9 63 68 2d 3e 69 54 65 72 6d 20 3d 20 69 54 65 72  ch->iTerm = iTer
363aa 6d 3b 0a 20 20 70 4d 61 74 63 68 2d 3e 69 54 6f  m;.  pMatch->iTo
363ab 6b 65 6e 20 3d 20 69 54 6f 6b 65 6e 3b 0a 20 20  ken = iToken;.  
363ac 70 4d 61 74 63 68 2d 3e 69 53 74 61 72 74 20 3d  pMatch->iStart =
363ad 20 69 53 74 61 72 74 3b 0a 20 20 70 4d 61 74 63   iStart;.  pMatc
363ae 68 2d 3e 6e 42 79 74 65 20 3d 20 6e 42 79 74 65  h->nByte = nByte
363af 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 7a 69 6e  ;.}../*.** Sizin
363b0 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  g information fo
363b1 72 20 74 68 65 20 63 69 72 63 75 6c 61 72 20 62  r the circular b
363b2 75 66 66 65 72 20 75 73 65 64 20 69 6e 20 73 6e  uffer used in sn
363b3 69 70 70 65 74 4f 66 66 73 65 74 73 4f 66 43 6f  ippetOffsetsOfCo
363b4 6c 75 6d 6e 28 29 0a 2a 2f 0a 23 64 65 66 69 6e  lumn().*/.#defin
363b5 65 20 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 20  e FTS3_ROTOR_SZ 
363b6 20 20 28 33 32 29 0a 23 64 65 66 69 6e 65 20 46    (32).#define F
363b7 54 53 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b 20 28  TS3_ROTOR_MASK (
363b8 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 2d 31 29  FTS3_ROTOR_SZ-1)
363b9 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e  ../*.** Function
363ba 20 74 6f 20 69 74 65 72 61 74 65 20 74 68 72 6f   to iterate thro
363bb 75 67 68 20 74 68 65 20 74 6f 6b 65 6e 73 20 6f  ugh the tokens o
363bc 66 20 61 20 63 6f 6d 70 69 6c 65 64 20 65 78 70  f a compiled exp
363bd 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 45  ression..**.** E
363be 78 63 65 70 74 2c 20 73 6b 69 70 20 61 6c 6c 20  xcept, skip all 
363bf 74 6f 6b 65 6e 73 20 6f 6e 20 74 68 65 20 72 69  tokens on the ri
363c0 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66  ght-hand side of
363c1 20 61 20 4e 4f 54 20 6f 70 65 72 61 74 6f 72 2e   a NOT operator.
363c2 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
363c3 6e 20 69 73 20 75 73 65 64 20 74 6f 20 66 69 6e  n is used to fin
363c4 64 20 74 6f 6b 65 6e 73 20 61 73 20 70 61 72 74  d tokens as part
363c5 20 6f 66 20 73 6e 69 70 70 65 74 20 61 6e 64 20   of snippet and 
363c6 6f 66 66 73 65 74 0a 2a 2a 20 67 65 6e 65 72 61  offset.** genera
363c7 74 69 6f 6e 20 61 6e 64 20 77 65 20 64 6f 20 6e  tion and we do n
363c8 74 20 77 61 6e 74 20 73 6e 69 70 70 65 74 73 20  t want snippets 
363c9 61 6e 64 20 6f 66 66 73 65 74 73 20 74 6f 20 72  and offsets to r
363ca 65 70 6f 72 74 20 6d 61 74 63 68 65 73 0a 2a 2a  eport matches.**
363cb 20 66 6f 72 20 74 6f 6b 65 6e 73 20 6f 6e 20 74   for tokens on t
363cc 68 65 20 52 48 53 20 6f 66 20 61 20 4e 4f 54 2e  he RHS of a NOT.
363cd 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
363ce 74 73 33 4e 65 78 74 45 78 70 72 54 6f 6b 65 6e  ts3NextExprToken
363cf 28 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78  (Fts3Expr **ppEx
363d0 70 72 2c 20 69 6e 74 20 2a 70 69 54 6f 6b 65 6e  pr, int *piToken
363d1 29 7b 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70  ){.  Fts3Expr *p
363d2 20 3d 20 2a 70 70 45 78 70 72 3b 0a 20 20 69 6e   = *ppExpr;.  in
363d3 74 20 69 54 6f 6b 65 6e 20 3d 20 2a 70 69 54 6f  t iToken = *piTo
363d4 6b 65 6e 3b 0a 20 20 69 66 28 20 69 54 6f 6b 65  ken;.  if( iToke
363d5 6e 3c 30 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e  n<0 ){.    /* In
363d6 20 74 68 69 73 20 63 61 73 65 20 74 68 65 20 65   this case the e
363d7 78 70 72 65 73 73 69 6f 6e 20 70 20 69 73 20 74  xpression p is t
363d8 68 65 20 72 6f 6f 74 20 6f 66 20 61 6e 20 65 78  he root of an ex
363d9 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 0a 20  pression tree.. 
363da 20 20 20 2a 2a 20 4d 6f 76 65 20 74 6f 20 74 68     ** Move to th
363db 65 20 66 69 72 73 74 20 74 6f 6b 65 6e 20 69 6e  e first token in
363dc 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
363dd 74 72 65 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tree..    */.   
363de 20 77 68 69 6c 65 28 20 70 2d 3e 70 4c 65 66 74   while( p->pLeft
363df 20 29 7b 0a 20 20 20 20 20 20 70 20 3d 20 70 2d   ){.      p = p-
363e0 3e 70 4c 65 66 74 3b 0a 20 20 20 20 7d 0a 20 20  >pLeft;.    }.  
363e1 20 20 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20    iToken = 0;.  
363e2 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
363e3 74 28 70 20 26 26 20 70 2d 3e 65 54 79 70 65 3d  t(p && p->eType=
363e4 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41 53 45  =FTSQUERY_PHRASE
363e5 20 29 3b 0a 20 20 20 20 69 66 28 20 69 54 6f 6b   );.    if( iTok
363e6 65 6e 3c 28 70 2d 3e 70 50 68 72 61 73 65 2d 3e  en<(p->pPhrase->
363e7 6e 54 6f 6b 65 6e 2d 31 29 20 29 7b 0a 20 20 20  nToken-1) ){.   
363e8 20 20 20 69 54 6f 6b 65 6e 2b 2b 3b 0a 20 20 20     iToken++;.   
363e9 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 54   }else{.      iT
363ea 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20  oken = 0;.      
363eb 77 68 69 6c 65 28 20 70 2d 3e 70 50 61 72 65 6e  while( p->pParen
363ec 74 20 26 26 20 70 2d 3e 70 50 61 72 65 6e 74 2d  t && p->pParent-
363ed 3e 70 4c 65 66 74 21 3d 70 20 29 7b 0a 20 20 20  >pLeft!=p ){.   
363ee 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e       assert( p->
363ef 70 50 61 72 65 6e 74 2d 3e 70 52 69 67 68 74 3d  pParent->pRight=
363f0 3d 70 20 29 3b 0a 20 20 20 20 20 20 20 20 70 20  =p );.        p 
363f1 3d 20 70 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20  = p->pParent;.  
363f2 20 20 20 20 7d 0a 20 20 20 20 20 20 70 20 3d 20      }.      p = 
363f3 70 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20 20  p->pParent;.    
363f4 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 20    if( p ){.     
363f5 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 52     assert( p->pR
363f6 69 67 68 74 21 3d 30 20 29 3b 0a 20 20 20 20 20  ight!=0 );.     
363f7 20 20 20 70 20 3d 20 70 2d 3e 70 52 69 67 68 74     p = p->pRight
363f8 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  ;.        while(
363f9 20 70 2d 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20   p->pLeft ){.   
363fa 20 20 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4c         p = p->pL
363fb 65 66 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eft;.        }. 
363fc 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
363fd 0a 0a 20 20 2a 70 70 45 78 70 72 20 3d 20 70 3b  ..  *ppExpr = p;
363fe 0a 20 20 2a 70 69 54 6f 6b 65 6e 20 3d 20 69 54  .  *piToken = iT
363ff 6f 6b 65 6e 3b 0a 20 20 72 65 74 75 72 6e 20 70  oken;.  return p
36400 3f 31 3a 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ?1:0;.}../*.** R
36401 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68  eturn TRUE if th
36402 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64  e expression nod
36403 65 20 70 45 78 70 72 20 69 73 20 6c 6f 63 61 74  e pExpr is locat
36404 65 64 20 62 65 6e 65 61 74 68 20 74 68 65 0a 2a  ed beneath the.*
36405 2a 20 52 48 53 20 6f 66 20 61 20 4e 4f 54 20 6f  * RHS of a NOT o
36406 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74  perator..*/.stat
36407 69 63 20 69 6e 74 20 66 74 73 33 45 78 70 72 42  ic int fts3ExprB
36408 65 6e 65 61 74 68 4e 6f 74 28 46 74 73 33 45 78  eneathNot(Fts3Ex
36409 70 72 20 2a 70 29 7b 0a 20 20 46 74 73 33 45 78  pr *p){.  Fts3Ex
3640a 70 72 20 2a 70 50 61 72 65 6e 74 3b 0a 20 20 77  pr *pParent;.  w
3640b 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 70  hile( p ){.    p
3640c 50 61 72 65 6e 74 20 3d 20 70 2d 3e 70 50 61 72  Parent = p->pPar
3640d 65 6e 74 3b 0a 20 20 20 20 69 66 28 20 70 50 61  ent;.    if( pPa
3640e 72 65 6e 74 20 26 26 20 70 50 61 72 65 6e 74 2d  rent && pParent-
3640f 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  >eType==FTSQUERY
36410 5f 4e 4f 54 20 26 26 20 70 50 61 72 65 6e 74 2d  _NOT && pParent-
36411 3e 70 52 69 67 68 74 3d 3d 70 20 29 7b 0a 20 20  >pRight==p ){.  
36412 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
36413 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 50 61 72    }.    p = pPar
36414 65 6e 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ent;.  }.  retur
36415 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  n 0;.}../*.** Ad
36416 64 20 65 6e 74 72 69 65 73 20 74 6f 20 70 53 6e  d entries to pSn
36417 69 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b 5d 20  ippet->aMatch[] 
36418 66 6f 72 20 65 76 65 72 79 20 6d 61 74 63 68 20  for every match 
36419 74 68 61 74 20 6f 63 63 75 72 73 20 61 67 61 69  that occurs agai
3641a 6e 73 74 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 20  nst.** document 
3641b 7a 44 6f 63 5b 30 2e 2e 6e 44 6f 63 2d 31 5d 20  zDoc[0..nDoc-1] 
3641c 77 68 69 63 68 20 69 73 20 73 74 6f 72 65 64 20  which is stored 
3641d 69 6e 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 75 6d  in column iColum
3641e 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
3641f 64 20 73 6e 69 70 70 65 74 4f 66 66 73 65 74 73  d snippetOffsets
36420 4f 66 43 6f 6c 75 6d 6e 28 0a 20 20 66 75 6c 6c  OfColumn(.  full
36421 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 70 43 75  text_cursor *pCu
36422 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  r,         /* Th
36423 65 20 66 75 6c 6c 74 65 73 74 20 73 65 61 72 63  e fulltest searc
36424 68 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 53 6e  h cursor */.  Sn
36425 69 70 70 65 74 20 2a 70 53 6e 69 70 70 65 74 2c  ippet *pSnippet,
36426 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36427 54 68 65 20 53 6e 69 70 70 65 74 20 6f 62 6a 65  The Snippet obje
36428 63 74 20 74 6f 20 62 65 20 66 69 6c 6c 65 64 20  ct to be filled 
36429 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c  in */.  int iCol
3642a 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20  umn,            
3642b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
3642c 6f 66 20 66 75 6c 6c 74 65 78 74 20 74 61 62 6c  of fulltext tabl
3642d 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 63 6f  e column */.  co
3642e 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f 63 2c 20  nst char *zDoc, 
3642f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
36430 54 65 78 74 20 6f 66 20 74 68 65 20 66 75 6c 6c  Text of the full
36431 74 65 78 74 20 74 61 62 6c 65 20 63 6f 6c 75 6d  text table colum
36432 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 6f 63 20  n */.  int nDoc 
36433 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36434 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20        /* Length 
36435 6f 66 20 7a 44 6f 63 20 69 6e 20 62 79 74 65 73  of zDoc in bytes
36436 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 73   */.){.  const s
36437 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
36438 5f 6d 6f 64 75 6c 65 20 2a 70 54 4d 6f 64 75 6c  _module *pTModul
36439 65 3b 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e  e;  /* The token
3643a 69 7a 65 72 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20  izer module */. 
3643b 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
3643c 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 3b 20  er *pTokenizer; 
3643d 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
3643e 68 65 20 73 70 65 63 69 66 69 63 20 74 6f 6b 65  he specific toke
3643f 6e 69 7a 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74  nizer */.  sqlit
36440 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  e3_tokenizer_cur
36441 73 6f 72 20 2a 70 54 43 75 72 73 6f 72 3b 20 20  sor *pTCursor;  
36442 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a        /* Tokeniz
36443 65 72 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 66  er cursor */.  f
36444 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 70 56  ulltext_vtab *pV
36445 74 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  tab;            
36446 20 20 20 20 2f 2a 20 54 68 65 20 66 75 6c 6c 20      /* The full 
36447 74 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20  text index */.  
36448 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20  int nColumn;    
36449 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3644a 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
3644b 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
3644c 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20   index */.  int 
3644d 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20  i, j;           
3644e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3644f 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
36450 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  s */.  int rc;  
36451 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36452 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
36453 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20  eturn code */.  
36454 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6d 61 74  unsigned int mat
36455 63 68 2c 20 70 72 65 76 4d 61 74 63 68 3b 20 20  ch, prevMatch;  
36456 20 20 20 20 20 2f 2a 20 50 68 72 61 73 65 20 73       /* Phrase s
36457 65 61 72 63 68 20 62 69 74 6d 61 73 6b 73 20 2a  earch bitmasks *
36458 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
36459 7a 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20  zToken;         
3645a 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74           /* Next
3645b 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20   token from the 
3645c 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 69  tokenizer */.  i
3645d 6e 74 20 6e 54 6f 6b 65 6e 3b 20 20 20 20 20 20  nt nToken;      
3645e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3645f 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a      /* Size of z
36460 54 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  Token */.  int i
36461 42 65 67 69 6e 2c 20 69 45 6e 64 2c 20 69 50 6f  Begin, iEnd, iPo
36462 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
36463 2f 2a 20 4f 66 66 73 65 74 73 20 6f 66 20 62 65  /* Offsets of be
36464 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 20  ginning and end 
36465 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c  */..  /* The fol
36466 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 73  lowing variables
36467 20 6b 65 65 70 20 61 20 63 69 72 63 75 6c 61 72   keep a circular
36468 20 62 75 66 66 65 72 20 6f 66 20 74 68 65 20 6c   buffer of the l
36469 61 73 74 0a 20 20 2a 2a 20 66 65 77 20 74 6f 6b  ast.  ** few tok
3646a 65 6e 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  ens */.  unsigne
3646b 64 20 69 6e 74 20 69 52 6f 74 6f 72 20 3d 20 30  d int iRotor = 0
3646c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
3646d 20 49 6e 64 65 78 20 6f 66 20 63 75 72 72 65 6e   Index of curren
3646e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74  t token */.  int
3646f 20 69 52 6f 74 6f 72 42 65 67 69 6e 5b 46 54 53   iRotorBegin[FTS
36470 33 5f 52 4f 54 4f 52 5f 53 5a 5d 3b 20 20 20 20  3_ROTOR_SZ];    
36471 20 20 2f 2a 20 42 65 67 69 6e 6e 69 6e 67 20 6f    /* Beginning o
36472 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a  ffset of token *
36473 2f 0a 20 20 69 6e 74 20 69 52 6f 74 6f 72 4c 65  /.  int iRotorLe
36474 6e 5b 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 5d  n[FTS3_ROTOR_SZ]
36475 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67  ;        /* Leng
36476 74 68 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 0a  th of token */..
36477 20 20 70 56 74 61 62 20 3d 20 63 75 72 73 6f 72    pVtab = cursor
36478 5f 76 74 61 62 28 70 43 75 72 29 3b 0a 20 20 6e  _vtab(pCur);.  n
36479 43 6f 6c 75 6d 6e 20 3d 20 70 56 74 61 62 2d 3e  Column = pVtab->
3647a 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 70 54 6f 6b 65  nColumn;.  pToke
3647b 6e 69 7a 65 72 20 3d 20 70 56 74 61 62 2d 3e 70  nizer = pVtab->p
3647c 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 70 54 4d  Tokenizer;.  pTM
3647d 6f 64 75 6c 65 20 3d 20 70 54 6f 6b 65 6e 69 7a  odule = pTokeniz
3647e 65 72 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 72  er->pModule;.  r
3647f 63 20 3d 20 70 54 4d 6f 64 75 6c 65 2d 3e 78 4f  c = pTModule->xO
36480 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20  pen(pTokenizer, 
36481 7a 44 6f 63 2c 20 6e 44 6f 63 2c 20 26 70 54 43  zDoc, nDoc, &pTC
36482 75 72 73 6f 72 29 3b 0a 20 20 69 66 28 20 72 63  ursor);.  if( rc
36483 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 54 43   ) return;.  pTC
36484 75 72 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65  ursor->pTokenize
36485 72 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a  r = pTokenizer;.
36486 0a 20 20 70 72 65 76 4d 61 74 63 68 20 3d 20 30  .  prevMatch = 0
36487 3b 0a 20 20 77 68 69 6c 65 28 20 21 70 54 4d 6f  ;.  while( !pTMo
36488 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 54 43 75  dule->xNext(pTCu
36489 72 73 6f 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26  rsor, &zToken, &
3648a 6e 54 6f 6b 65 6e 2c 20 26 69 42 65 67 69 6e 2c  nToken, &iBegin,
3648b 20 26 69 45 6e 64 2c 20 26 69 50 6f 73 29 20 29   &iEnd, &iPos) )
3648c 7b 0a 20 20 20 20 46 74 73 33 45 78 70 72 20 2a  {.    Fts3Expr *
3648d 70 49 74 65 72 20 3d 20 70 43 75 72 2d 3e 70 45  pIter = pCur->pE
3648e 78 70 72 3b 0a 20 20 20 20 69 6e 74 20 69 49 74  xpr;.    int iIt
3648f 65 72 20 3d 20 2d 31 3b 0a 20 20 20 20 69 52 6f  er = -1;.    iRo
36490 74 6f 72 42 65 67 69 6e 5b 69 52 6f 74 6f 72 26  torBegin[iRotor&
36491 46 54 53 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b 5d  FTS3_ROTOR_MASK]
36492 20 3d 20 69 42 65 67 69 6e 3b 0a 20 20 20 20 69   = iBegin;.    i
36493 52 6f 74 6f 72 4c 65 6e 5b 69 52 6f 74 6f 72 26  RotorLen[iRotor&
36494 46 54 53 33 5f 52 4f 54 4f 52 5f 4d 41 53 4b 5d  FTS3_ROTOR_MASK]
36495 20 3d 20 69 45 6e 64 2d 69 42 65 67 69 6e 3b 0a   = iEnd-iBegin;.
36496 20 20 20 20 6d 61 74 63 68 20 3d 20 30 3b 0a 20      match = 0;. 
36497 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 28 46     for(i=0; i<(F
36498 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 2d 31 29 20  TS3_ROTOR_SZ-1) 
36499 26 26 20 66 74 73 33 4e 65 78 74 45 78 70 72 54  && fts3NextExprT
3649a 6f 6b 65 6e 28 26 70 49 74 65 72 2c 20 26 69 49  oken(&pIter, &iI
3649b 74 65 72 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ter); i++){.    
3649c 20 20 69 6e 74 20 6e 50 68 72 61 73 65 3b 20 20    int nPhrase;  
3649d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3649e 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74    /* Number of t
3649f 6f 6b 65 6e 73 20 69 6e 20 63 75 72 72 65 6e 74  okens in current
364a0 20 70 68 72 61 73 65 20 2a 2f 0a 20 20 20 20 20   phrase */.     
364a1 20 73 74 72 75 63 74 20 50 68 72 61 73 65 54 6f   struct PhraseTo
364a2 6b 65 6e 20 2a 70 54 6f 6b 65 6e 3b 20 20 20 20  ken *pToken;    
364a3 20 2f 2a 20 43 75 72 72 65 6e 74 20 74 6f 6b 65   /* Current toke
364a4 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 69  n */.      int i
364a5 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
364a6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f             /* Co
364a7 6c 75 6d 6e 20 69 6e 64 65 78 20 2a 2f 0a 0a 20  lumn index */.. 
364a8 20 20 20 20 20 69 66 28 20 66 74 73 33 45 78 70       if( fts3Exp
364a9 72 42 65 6e 65 61 74 68 4e 6f 74 28 70 49 74 65  rBeneathNot(pIte
364aa 72 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  r) ) continue;. 
364ab 20 20 20 20 20 6e 50 68 72 61 73 65 20 3d 20 70       nPhrase = p
364ac 49 74 65 72 2d 3e 70 50 68 72 61 73 65 2d 3e 6e  Iter->pPhrase->n
364ad 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 70 54 6f  Token;.      pTo
364ae 6b 65 6e 20 3d 20 26 70 49 74 65 72 2d 3e 70 50  ken = &pIter->pP
364af 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 49  hrase->aToken[iI
364b0 74 65 72 5d 3b 0a 20 20 20 20 20 20 69 43 6f 6c  ter];.      iCol
364b1 20 3d 20 70 49 74 65 72 2d 3e 70 50 68 72 61 73   = pIter->pPhras
364b2 65 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20  e->iColumn;.    
364b3 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 26 26    if( iCol>=0 &&
364b4 20 69 43 6f 6c 3c 6e 43 6f 6c 75 6d 6e 20 26 26   iCol<nColumn &&
364b5 20 69 43 6f 6c 21 3d 69 43 6f 6c 75 6d 6e 20 29   iCol!=iColumn )
364b6 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
364b7 20 69 66 28 20 70 54 6f 6b 65 6e 2d 3e 6e 3e 6e   if( pToken->n>n
364b8 54 6f 6b 65 6e 20 29 20 63 6f 6e 74 69 6e 75 65  Token ) continue
364b9 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 54 6f  ;.      if( !pTo
364ba 6b 65 6e 2d 3e 69 73 50 72 65 66 69 78 20 26 26  ken->isPrefix &&
364bb 20 70 54 6f 6b 65 6e 2d 3e 6e 3c 6e 54 6f 6b 65   pToken->n<nToke
364bc 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  n ) continue;.  
364bd 20 20 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b      assert( pTok
364be 65 6e 2d 3e 6e 3c 3d 6e 54 6f 6b 65 6e 20 29 3b  en->n<=nToken );
364bf 0a 20 20 20 20 20 20 69 66 28 20 6d 65 6d 63 6d  .      if( memcm
364c0 70 28 70 54 6f 6b 65 6e 2d 3e 7a 2c 20 7a 54 6f  p(pToken->z, zTo
364c1 6b 65 6e 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 20  ken, pToken->n) 
364c2 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
364c3 20 20 69 66 28 20 69 49 74 65 72 3e 30 20 26 26    if( iIter>0 &&
364c4 20 28 70 72 65 76 4d 61 74 63 68 20 26 20 28 31   (prevMatch & (1
364c5 3c 3c 69 29 29 3d 3d 30 20 29 20 63 6f 6e 74 69  <<i))==0 ) conti
364c6 6e 75 65 3b 0a 20 20 20 20 20 20 6d 61 74 63 68  nue;.      match
364c7 20 7c 3d 20 31 3c 3c 69 3b 0a 20 20 20 20 20 20   |= 1<<i;.      
364c8 69 66 28 20 69 3d 3d 28 46 54 53 33 5f 52 4f 54  if( i==(FTS3_ROT
364c9 4f 52 5f 53 5a 2d 32 29 20 7c 7c 20 6e 50 68 72  OR_SZ-2) || nPhr
364ca 61 73 65 3d 3d 69 49 74 65 72 2b 31 20 29 7b 0a  ase==iIter+1 ){.
364cb 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 6e 50          for(j=nP
364cc 68 72 61 73 65 2d 31 3b 20 6a 3e 3d 30 3b 20 6a  hrase-1; j>=0; j
364cd 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  --){.          i
364ce 6e 74 20 6b 20 3d 20 28 69 52 6f 74 6f 72 2d 6a  nt k = (iRotor-j
364cf 29 20 26 20 46 54 53 33 5f 52 4f 54 4f 52 5f 4d  ) & FTS3_ROTOR_M
364d0 41 53 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 73  ASK;.          s
364d1 6e 69 70 70 65 74 41 70 70 65 6e 64 4d 61 74 63  nippetAppendMatc
364d2 68 28 70 53 6e 69 70 70 65 74 2c 20 69 43 6f 6c  h(pSnippet, iCol
364d3 75 6d 6e 2c 20 69 2d 6a 2c 20 69 50 6f 73 2d 6a  umn, i-j, iPos-j
364d4 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
364d5 20 20 69 52 6f 74 6f 72 42 65 67 69 6e 5b 6b 5d    iRotorBegin[k]
364d6 2c 20 69 52 6f 74 6f 72 4c 65 6e 5b 6b 5d 29 3b  , iRotorLen[k]);
364d7 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
364d8 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 72 65   }.    }.    pre
364d9 76 4d 61 74 63 68 20 3d 20 6d 61 74 63 68 3c 3c  vMatch = match<<
364da 31 3b 0a 20 20 20 20 69 52 6f 74 6f 72 2b 2b 3b  1;.    iRotor++;
364db 0a 20 20 7d 0a 20 20 70 54 4d 6f 64 75 6c 65 2d  .  }.  pTModule-
364dc 3e 78 43 6c 6f 73 65 28 70 54 43 75 72 73 6f 72  >xClose(pTCursor
364dd 29 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  );  .}../*.** Re
364de 6d 6f 76 65 20 65 6e 74 72 69 65 73 20 66 72 6f  move entries fro
364df 6d 20 74 68 65 20 70 53 6e 69 70 70 65 74 20 73  m the pSnippet s
364e0 74 72 75 63 74 75 72 65 20 74 6f 20 61 63 63 6f  tructure to acco
364e1 75 6e 74 20 66 6f 72 20 74 68 65 20 4e 45 41 52  unt for the NEAR
364e2 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 20 57 68  .** operator. Wh
364e3 65 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65  en this is calle
364e4 64 2c 20 70 53 6e 69 70 70 65 74 20 63 6f 6e 74  d, pSnippet cont
364e5 61 69 6e 73 20 74 68 65 20 6c 69 73 74 20 6f 66  ains the list of
364e6 20 74 6f 6b 65 6e 20 0a 2a 2a 20 6f 66 66 73 65   token .** offse
364e7 74 73 20 70 72 6f 64 75 63 65 64 20 62 79 20 74  ts produced by t
364e8 72 65 61 74 69 6e 67 20 61 6c 6c 20 4e 45 41 52  reating all NEAR
364e9 20 6f 70 65 72 61 74 6f 72 73 20 61 73 20 41 4e   operators as AN
364ea 44 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a 2a 20  D operators..** 
364eb 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
364ec 6d 6f 76 65 73 20 61 6e 79 20 65 6e 74 72 69 65  moves any entrie
364ed 73 20 74 68 61 74 20 73 68 6f 75 6c 64 20 6e 6f  s that should no
364ee 74 20 62 65 20 70 72 65 73 65 6e 74 20 61 66 74  t be present aft
364ef 65 72 0a 2a 2a 20 61 63 63 6f 75 6e 74 69 6e 67  er.** accounting
364f0 20 66 6f 72 20 74 68 65 20 4e 45 41 52 20 72 65   for the NEAR re
364f1 73 74 72 69 63 74 69 6f 6e 2e 20 46 6f 72 20 65  striction. For e
364f2 78 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 71  xample, if the q
364f3 75 65 72 69 65 64 0a 2a 2a 20 64 6f 63 75 6d 65  ueried.** docume
364f4 6e 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  nt is:.**.**    
364f5 20 22 41 20 42 20 43 20 44 20 45 20 41 22 0a 2a   "A B C D E A".*
364f6 2a 0a 2a 2a 20 61 6e 64 20 74 68 65 20 71 75 65  *.** and the que
364f7 72 79 20 69 73 3a 0a 2a 2a 20 0a 2a 2a 20 20 20  ry is:.** .**   
364f8 20 20 41 20 4e 45 41 52 2f 30 20 45 0a 2a 2a 0a    A NEAR/0 E.**.
364f9 2a 2a 20 74 68 65 6e 20 77 68 65 6e 20 74 68 69  ** then when thi
364fa 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  s function is ca
364fb 6c 6c 65 64 20 74 68 65 20 53 6e 69 70 70 65 74  lled the Snippet
364fc 20 63 6f 6e 74 61 69 6e 73 20 74 6f 6b 65 6e 20   contains token 
364fd 6f 66 66 73 65 74 73 0a 2a 2a 20 30 2c 20 34 20  offsets.** 0, 4 
364fe 61 6e 64 20 35 2e 20 54 68 69 73 20 66 75 6e 63  and 5. This func
364ff 74 69 6f 6e 20 72 65 6d 6f 76 65 73 20 74 68 65  tion removes the
36500 20 22 30 22 20 65 6e 74 72 79 20 28 62 65 63 61   "0" entry (beca
36501 75 73 65 20 74 68 65 20 66 69 72 73 74 20 41 0a  use the first A.
36502 2a 2a 20 69 73 20 6e 6f 74 20 6e 65 61 72 20 65  ** is not near e
36503 6e 6f 75 67 68 20 74 6f 20 61 6e 20 45 29 2e 0a  nough to an E)..
36504 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20  **.** When this 
36505 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c  function is call
36506 65 64 2c 20 74 68 65 20 76 61 6c 75 65 20 70 6f  ed, the value po
36507 69 6e 74 65 64 20 74 6f 20 62 79 20 70 61 72 61  inted to by para
36508 6d 65 74 65 72 20 70 69 4c 65 66 74 20 69 73 0a  meter piLeft is.
36509 2a 2a 20 74 68 65 20 69 6e 74 65 67 65 72 20 69  ** the integer i
3650a 64 20 6f 66 20 74 68 65 20 6c 65 66 74 2d 6d 6f  d of the left-mo
3650b 73 74 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20  st token in the 
3650c 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20  expression tree 
3650d 68 65 61 64 65 64 20 62 79 0a 2a 2a 20 70 45 78  headed by.** pEx
3650e 70 72 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  pr. This functio
3650f 6e 20 69 6e 63 72 65 6d 65 6e 74 73 20 2a 70 69  n increments *pi
36510 4c 65 66 74 20 62 79 20 74 68 65 20 74 6f 74 61  Left by the tota
36511 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74 6f 6b 65  l number of toke
36512 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 65 78 70  ns.** in the exp
36513 72 65 73 73 69 6f 6e 20 74 72 65 65 20 68 65 61  ression tree hea
36514 64 65 64 20 62 79 20 70 45 78 70 72 2e 0a 2a 2a  ded by pExpr..**
36515 0a 2a 2a 20 52 65 74 75 72 6e 20 31 20 69 66 20  .** Return 1 if 
36516 61 6e 79 20 74 72 69 6d 6d 69 6e 67 20 6f 63 63  any trimming occ
36517 75 72 73 2e 20 20 52 65 74 75 72 6e 20 30 20 69  urs.  Return 0 i
36518 66 20 6e 6f 20 74 72 69 6d 6d 69 6e 67 20 69 73  f no trimming is
36519 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74   required..*/.st
3651a 61 74 69 63 20 69 6e 74 20 74 72 69 6d 53 6e 69  atic int trimSni
3651b 70 70 65 74 4f 66 66 73 65 74 73 28 0a 20 20 46  ppetOffsets(.  F
3651c 74 73 33 45 78 70 72 20 2a 70 45 78 70 72 2c 20  ts3Expr *pExpr, 
3651d 20 20 20 20 20 2f 2a 20 54 68 65 20 73 65 61 72       /* The sear
3651e 63 68 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  ch expression */
3651f 0a 20 20 53 6e 69 70 70 65 74 20 2a 70 53 6e 69  .  Snippet *pSni
36520 70 70 65 74 2c 20 20 20 20 2f 2a 20 54 68 65 20  ppet,    /* The 
36521 73 65 74 20 6f 66 20 73 6e 69 70 70 65 74 20 6f  set of snippet o
36522 66 66 73 65 74 73 20 74 6f 20 62 65 20 74 72 69  ffsets to be tri
36523 6d 6d 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  mmed */.  int *p
36524 69 4c 65 66 74 20 20 20 20 20 20 20 20 20 20 20  iLeft           
36525 2f 2a 20 49 6e 64 65 78 20 6f 66 20 6c 65 66 74  /* Index of left
36526 2d 6d 6f 73 74 20 74 6f 6b 65 6e 20 69 6e 20 70  -most token in p
36527 45 78 70 72 20 2a 2f 0a 29 7b 0a 20 20 69 66 28  Expr */.){.  if(
36528 20 70 45 78 70 72 20 29 7b 0a 20 20 20 20 69 66   pExpr ){.    if
36529 28 20 74 72 69 6d 53 6e 69 70 70 65 74 4f 66 66  ( trimSnippetOff
3652a 73 65 74 73 28 70 45 78 70 72 2d 3e 70 4c 65 66  sets(pExpr->pLef
3652b 74 2c 20 70 53 6e 69 70 70 65 74 2c 20 70 69 4c  t, pSnippet, piL
3652c 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65  eft) ){.      re
3652d 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 0a 20  turn 1;.    }.. 
3652e 20 20 20 73 77 69 74 63 68 28 20 70 45 78 70 72     switch( pExpr
3652f 2d 3e 65 54 79 70 65 20 29 7b 0a 20 20 20 20 20  ->eType ){.     
36530 20 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 50   case FTSQUERY_P
36531 48 52 41 53 45 3a 0a 20 20 20 20 20 20 20 20 2a  HRASE:.        *
36532 70 69 4c 65 66 74 20 2b 3d 20 70 45 78 70 72 2d  piLeft += pExpr-
36533 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e  >pPhrase->nToken
36534 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
36535 0a 20 20 20 20 20 20 63 61 73 65 20 46 54 53 51  .      case FTSQ
36536 55 45 52 59 5f 4e 45 41 52 3a 20 7b 0a 20 20 20  UERY_NEAR: {.   
36537 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68       /* The righ
36538 74 2d 68 61 6e 64 2d 73 69 64 65 20 6f 66 20 61  t-hand-side of a
36539 20 4e 45 41 52 20 6f 70 65 72 61 74 6f 72 20 69   NEAR operator i
3653a 73 20 61 6c 77 61 79 73 20 61 20 70 68 72 61 73  s always a phras
3653b 65 2e 20 54 68 65 0a 20 20 20 20 20 20 20 20 2a  e. The.        *
3653c 2a 20 6c 65 66 74 2d 68 61 6e 64 2d 73 69 64 65  * left-hand-side
3653d 20 69 73 20 65 69 74 68 65 72 20 61 20 70 68 72   is either a phr
3653e 61 73 65 20 6f 72 20 61 6e 20 65 78 70 72 65 73  ase or an expres
3653f 73 69 6f 6e 20 74 72 65 65 20 74 68 61 74 20 69  sion tree that i
36540 73 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 74  s .        ** it
36541 73 65 6c 66 20 68 65 61 64 65 64 20 62 79 20 61  self headed by a
36542 20 4e 45 41 52 20 6f 70 65 72 61 74 6f 72 2e 20   NEAR operator. 
36543 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e  The following in
36544 69 74 69 61 6c 69 7a 61 74 69 6f 6e 73 0a 20 20  itializations.  
36545 20 20 20 20 20 20 2a 2a 20 73 65 74 20 6c 6f 63        ** set loc
36546 61 6c 20 76 61 72 69 61 62 6c 65 20 69 4c 65 66  al variable iLef
36547 74 20 74 6f 20 74 68 65 20 74 6f 6b 65 6e 20 6e  t to the token n
36548 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6c 65 66  umber of the lef
36549 74 2d 6d 6f 73 74 0a 20 20 20 20 20 20 20 20 2a  t-most.        *
3654a 2a 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 72  * token in the r
3654b 69 67 68 74 2d 68 61 6e 64 20 70 68 72 61 73 65  ight-hand phrase
3654c 2c 20 61 6e 64 20 69 52 69 67 68 74 20 74 6f 20  , and iRight to 
3654d 74 68 65 20 72 69 67 68 74 20 6d 6f 73 74 0a 20  the right most. 
3654e 20 20 20 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 20         ** token 
3654f 69 6e 20 74 68 65 20 73 61 6d 65 20 70 68 72 61  in the same phra
36550 73 65 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  se. For example,
36551 20 69 66 20 77 65 20 68 61 64 3a 0a 20 20 20 20   if we had:.    
36552 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
36553 2a 20 20 20 20 20 3c 63 6f 6c 3e 20 4d 41 54 43  *     <col> MATC
36554 48 20 27 22 61 62 63 20 64 65 66 22 20 4e 45 41  H '"abc def" NEA
36555 52 2f 32 20 22 67 68 69 20 6a 6b 6c 22 27 0a 20  R/2 "ghi jkl"'. 
36556 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
36557 20 20 2a 2a 20 74 68 65 6e 20 69 4c 65 66 74 20    ** then iLeft 
36558 77 69 6c 6c 20 62 65 20 73 65 74 20 74 6f 20 32  will be set to 2
36559 20 28 74 6f 6b 65 6e 20 6e 75 6d 62 65 72 20 6f   (token number o
3655a 66 20 67 68 69 29 20 61 6e 64 20 6e 54 6f 6b 65  f ghi) and nToke
3655b 6e 20 77 69 6c 6c 0a 20 20 20 20 20 20 20 20 2a  n will.        *
3655c 2a 20 62 65 20 73 65 74 20 74 6f 20 34 2e 0a 20  * be set to 4.. 
3655d 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
3655e 20 20 46 74 73 33 45 78 70 72 20 2a 70 4c 65 66    Fts3Expr *pLef
3655f 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  t = pExpr->pLeft
36560 3b 0a 20 20 20 20 20 20 20 20 46 74 73 33 45 78  ;.        Fts3Ex
36561 70 72 20 2a 70 52 69 67 68 74 20 3d 20 70 45 78  pr *pRight = pEx
36562 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20  pr->pRight;.    
36563 20 20 20 20 69 6e 74 20 69 4c 65 66 74 20 3d 20      int iLeft = 
36564 2a 70 69 4c 65 66 74 3b 0a 20 20 20 20 20 20 20  *piLeft;.       
36565 20 69 6e 74 20 6e 4e 65 61 72 20 3d 20 70 45 78   int nNear = pEx
36566 70 72 2d 3e 6e 4e 65 61 72 3b 0a 20 20 20 20 20  pr->nNear;.     
36567 20 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20     int nToken = 
36568 70 52 69 67 68 74 2d 3e 70 50 68 72 61 73 65 2d  pRight->pPhrase-
36569 3e 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20  >nToken;.       
3656a 20 69 6e 74 20 6a 6a 2c 20 69 69 3b 0a 20 20 20   int jj, ii;.   
3656b 20 20 20 20 20 69 66 28 20 70 4c 65 66 74 2d 3e       if( pLeft->
3656c 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f  eType==FTSQUERY_
3656d 4e 45 41 52 20 29 7b 0a 20 20 20 20 20 20 20 20  NEAR ){.        
3656e 20 20 70 4c 65 66 74 20 3d 20 70 4c 65 66 74 2d    pLeft = pLeft-
3656f 3e 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20  >pRight;.       
36570 20 7d 0a 20 20 20 20 20 20 20 20 61 73 73 65 72   }.        asser
36571 74 28 20 70 52 69 67 68 74 2d 3e 65 54 79 70 65  t( pRight->eType
36572 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  ==FTSQUERY_PHRAS
36573 45 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  E );.        ass
36574 65 72 74 28 20 70 4c 65 66 74 2d 3e 65 54 79 70  ert( pLeft->eTyp
36575 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41  e==FTSQUERY_PHRA
36576 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20 6e 54  SE );.        nT
36577 6f 6b 65 6e 20 2b 3d 20 70 4c 65 66 74 2d 3e 70  oken += pLeft->p
36578 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 0a  Phrase->nToken;.
36579 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 69 3d  .        for(ii=
3657a 30 3b 20 69 69 3c 70 53 6e 69 70 70 65 74 2d 3e  0; ii<pSnippet->
3657b 6e 4d 61 74 63 68 3b 20 69 69 2b 2b 29 7b 0a 20  nMatch; ii++){. 
3657c 20 20 20 20 20 20 20 20 20 73 74 72 75 63 74 20           struct 
3657d 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 70 20  snippetMatch *p 
3657e 3d 20 26 70 53 6e 69 70 70 65 74 2d 3e 61 4d 61  = &pSnippet->aMa
3657f 74 63 68 5b 69 69 5d 3b 0a 20 20 20 20 20 20 20  tch[ii];.       
36580 20 20 20 69 66 28 20 70 2d 3e 69 54 65 72 6d 3d     if( p->iTerm=
36581 3d 69 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20  =iLeft ){.      
36582 20 20 20 20 20 20 69 6e 74 20 69 73 4f 6b 20 3d        int isOk =
36583 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   0;.            
36584 2f 2a 20 53 6e 69 70 70 65 74 20 69 69 20 69 73  /* Snippet ii is
36585 20 61 6e 20 6f 63 63 75 72 65 6e 63 65 20 6f 66   an occurence of
36586 20 71 75 65 72 79 20 74 65 72 6d 20 69 4c 65 66   query term iLef
36587 74 20 69 6e 20 74 68 65 20 64 6f 63 75 6d 65 6e  t in the documen
36588 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  t..            *
36589 2a 20 49 74 20 6f 63 63 75 72 73 20 61 74 20 70  * It occurs at p
3658a 6f 73 69 74 69 6f 6e 20 28 70 2d 3e 69 54 6f 6b  osition (p->iTok
3658b 65 6e 29 20 6f 66 20 74 68 65 20 64 6f 63 75 6d  en) of the docum
3658c 65 6e 74 2e 20 57 65 20 6e 6f 77 0a 20 20 20 20  ent. We now.    
3658d 20 20 20 20 20 20 20 20 2a 2a 20 73 65 61 72 63          ** searc
3658e 68 20 66 6f 72 20 61 6e 20 69 6e 73 74 61 6e 63  h for an instanc
3658f 65 20 6f 66 20 74 6f 6b 65 6e 20 28 69 4c 65 66  e of token (iLef
36590 74 2d 31 29 20 73 6f 6d 65 77 68 65 72 65 20 69  t-1) somewhere i
36591 6e 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 20  n the .         
36592 20 20 20 2a 2a 20 72 61 6e 67 65 20 28 70 2d 3e     ** range (p->
36593 69 54 6f 6b 65 6e 20 2d 20 6e 4e 65 61 72 29 2e  iToken - nNear).
36594 2e 2e 28 70 2d 3e 69 54 6f 6b 65 6e 20 2b 20 6e  ..(p->iToken + n
36595 4e 65 61 72 20 2b 20 6e 54 6f 6b 65 6e 29 20 77  Near + nToken) w
36596 69 74 68 69 6e 20 0a 20 20 20 20 20 20 20 20 20  ithin .         
36597 20 20 20 2a 2a 20 74 68 65 20 73 65 74 20 6f 66     ** the set of
36598 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20 73 74   snippetMatch st
36599 72 75 63 74 75 72 65 73 2e 20 49 66 20 6f 6e 65  ructures. If one
3659a 20 69 73 20 66 6f 75 6e 64 2c 20 70 72 6f 63 65   is found, proce
3659b 65 64 2e 20 0a 20 20 20 20 20 20 20 20 20 20 20  ed. .           
3659c 20 2a 2a 20 49 66 20 6f 6e 65 20 63 61 6e 6e 6f   ** If one canno
3659d 74 20 62 65 20 66 6f 75 6e 64 2c 20 74 68 65 6e  t be found, then
3659e 20 72 65 6d 6f 76 65 20 73 6e 69 70 70 65 74 73   remove snippets
3659f 20 69 69 2e 2e 28 69 69 2b 4e 2d 31 29 20 0a 20   ii..(ii+N-1) . 
365a0 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 72             ** fr
365a1 6f 6d 20 74 68 65 20 6d 61 74 63 68 69 6e 67 20  om the matching 
365a2 73 6e 69 70 70 65 74 73 2c 20 77 68 65 72 65 20  snippets, where 
365a3 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  N is the number 
365a4 6f 66 20 74 6f 6b 65 6e 73 20 0a 20 20 20 20 20  of tokens .     
365a5 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 70 68 72         ** in phr
365a6 61 73 65 20 70 52 69 67 68 74 2d 3e 70 50 68 72  ase pRight->pPhr
365a7 61 73 65 2e 0a 20 20 20 20 20 20 20 20 20 20 20  ase..           
365a8 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
365a9 66 6f 72 28 6a 6a 3d 30 3b 20 69 73 4f 6b 3d 3d  for(jj=0; isOk==
365aa 30 20 26 26 20 6a 6a 3c 70 53 6e 69 70 70 65 74  0 && jj<pSnippet
365ab 2d 3e 6e 4d 61 74 63 68 3b 20 6a 6a 2b 2b 29 7b  ->nMatch; jj++){
365ac 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73  .              s
365ad 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74  truct snippetMat
365ae 63 68 20 2a 70 32 20 3d 20 26 70 53 6e 69 70 70  ch *p2 = &pSnipp
365af 65 74 2d 3e 61 4d 61 74 63 68 5b 6a 6a 5d 3b 0a  et->aMatch[jj];.
365b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
365b1 28 20 70 32 2d 3e 69 54 65 72 6d 3d 3d 28 69 4c  ( p2->iTerm==(iL
365b2 65 66 74 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  eft-1) ){.      
365b3 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 32            if( p2
365b4 2d 3e 69 54 6f 6b 65 6e 3e 3d 28 70 2d 3e 69 54  ->iToken>=(p->iT
365b5 6f 6b 65 6e 2d 6e 4e 65 61 72 2d 31 29 20 0a 20  oken-nNear-1) . 
365b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
365b7 26 26 20 70 32 2d 3e 69 54 6f 6b 65 6e 3c 28 70  && p2->iToken<(p
365b8 2d 3e 69 54 6f 6b 65 6e 2b 6e 4e 65 61 72 2b 6e  ->iToken+nNear+n
365b9 54 6f 6b 65 6e 29 20 0a 20 20 20 20 20 20 20 20  Token) .        
365ba 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20          ){.     
365bb 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 4f               isO
365bc 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  k = 1;.         
365bd 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
365be 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
365bf 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
365c0 20 20 20 69 66 28 20 21 69 73 4f 6b 20 29 7b 0a     if( !isOk ){.
365c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
365c2 74 20 6b 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  t kk;.          
365c3 20 20 20 20 66 6f 72 28 6b 6b 3d 30 3b 20 6b 6b      for(kk=0; kk
365c4 3c 70 52 69 67 68 74 2d 3e 70 50 68 72 61 73 65  <pRight->pPhrase
365c5 2d 3e 6e 54 6f 6b 65 6e 3b 20 6b 6b 2b 2b 29 7b  ->nToken; kk++){
365c6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
365c7 20 70 53 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63   pSnippet->aMatc
365c8 68 5b 6b 6b 2b 69 69 5d 2e 69 54 65 72 6d 20 3d  h[kk+ii].iTerm =
365c9 20 2d 32 3b 0a 20 20 20 20 20 20 20 20 20 20 20   -2;.           
365ca 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
365cb 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
365cc 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
365cd 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
365ce 20 69 66 28 20 70 2d 3e 69 54 65 72 6d 3d 3d 28   if( p->iTerm==(
365cf 69 4c 65 66 74 2d 31 29 20 29 7b 0a 20 20 20 20  iLeft-1) ){.    
365d0 20 20 20 20 20 20 20 20 69 6e 74 20 69 73 4f 6b          int isOk
365d1 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
365d2 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 69 73 4f 6b    for(jj=0; isOk
365d3 3d 3d 30 20 26 26 20 6a 6a 3c 70 53 6e 69 70 70  ==0 && jj<pSnipp
365d4 65 74 2d 3e 6e 4d 61 74 63 68 3b 20 6a 6a 2b 2b  et->nMatch; jj++
365d5 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
365d6 20 73 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d   struct snippetM
365d7 61 74 63 68 20 2a 70 32 20 3d 20 26 70 53 6e 69  atch *p2 = &pSni
365d8 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b 6a 6a 5d  ppet->aMatch[jj]
365d9 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
365da 69 66 28 20 70 32 2d 3e 69 54 65 72 6d 3d 3d 69  if( p2->iTerm==i
365db 4c 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Left ){.        
365dc 20 20 20 20 20 20 20 20 69 66 28 20 70 32 2d 3e          if( p2->
365dd 69 54 6f 6b 65 6e 3c 3d 28 70 2d 3e 69 54 6f 6b  iToken<=(p->iTok
365de 65 6e 2b 6e 4e 65 61 72 2b 31 29 20 0a 20 20 20  en+nNear+1) .   
365df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26                &&
365e0 20 70 32 2d 3e 69 54 6f 6b 65 6e 3e 28 70 2d 3e   p2->iToken>(p->
365e1 69 54 6f 6b 65 6e 2d 6e 4e 65 61 72 2d 6e 54 6f  iToken-nNear-nTo
365e2 6b 65 6e 29 20 0a 20 20 20 20 20 20 20 20 20 20  ken) .          
365e3 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
365e4 20 20 20 20 20 20 20 20 20 20 20 69 73 4f 6b 20             isOk 
365e5 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 1;.           
365e6 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
365e7 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
365e8 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
365e9 20 69 66 28 20 21 69 73 4f 6b 20 29 7b 0a 20 20   if( !isOk ){.  
365ea 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
365eb 6b 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  kk;.            
365ec 20 20 66 6f 72 28 6b 6b 3d 30 3b 20 6b 6b 3c 70    for(kk=0; kk<p
365ed 4c 65 66 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e  Left->pPhrase->n
365ee 54 6f 6b 65 6e 3b 20 6b 6b 2b 2b 29 7b 0a 20 20  Token; kk++){.  
365ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 53                pS
365f0 6e 69 70 70 65 74 2d 3e 61 4d 61 74 63 68 5b 69  nippet->aMatch[i
365f1 69 2d 6b 6b 5d 2e 69 54 65 72 6d 20 3d 20 2d 32  i-kk].iTerm = -2
365f2 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
365f3 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }.              
365f4 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
365f5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
365f6 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
365f7 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
365f8 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
365f9 69 66 28 20 74 72 69 6d 53 6e 69 70 70 65 74 4f  if( trimSnippetO
365fa 66 66 73 65 74 73 28 70 45 78 70 72 2d 3e 70 52  ffsets(pExpr->pR
365fb 69 67 68 74 2c 20 70 53 6e 69 70 70 65 74 2c 20  ight, pSnippet, 
365fc 70 69 4c 65 66 74 29 20 29 7b 0a 20 20 20 20 20  piLeft) ){.     
365fd 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
365fe 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
365ff 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74  .}../*.** Comput
36600 65 20 61 6c 6c 20 6f 66 66 73 65 74 73 20 66 6f  e all offsets fo
36601 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f  r the current ro
36602 77 20 6f 66 20 74 68 65 20 71 75 65 72 79 2e 20  w of the query. 
36603 20 0a 2a 2a 20 49 66 20 74 68 65 20 6f 66 66 73   .** If the offs
36604 65 74 73 20 68 61 76 65 20 61 6c 72 65 61 64 79  ets have already
36605 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 2c 20   been computed, 
36606 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
36607 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74  a no-op..*/.stat
36608 69 63 20 76 6f 69 64 20 73 6e 69 70 70 65 74 41  ic void snippetA
36609 6c 6c 4f 66 66 73 65 74 73 28 66 75 6c 6c 74 65  llOffsets(fullte
3660a 78 74 5f 63 75 72 73 6f 72 20 2a 70 29 7b 0a 20  xt_cursor *p){. 
3660b 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 0a 20 20   int nColumn;.  
3660c 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 69 3b 0a  int iColumn, i;.
3660d 20 20 69 6e 74 20 69 46 69 72 73 74 2c 20 69 4c    int iFirst, iL
3660e 61 73 74 3b 0a 20 20 69 6e 74 20 69 54 65 72 6d  ast;.  int iTerm
3660f 20 3d 20 30 3b 0a 20 20 66 75 6c 6c 74 65 78 74   = 0;.  fulltext
36610 5f 76 74 61 62 20 2a 70 46 74 73 20 3d 20 63 75  _vtab *pFts = cu
36611 72 73 6f 72 5f 76 74 61 62 28 70 29 3b 0a 0a 20  rsor_vtab(p);.. 
36612 20 69 66 28 20 70 2d 3e 73 6e 69 70 70 65 74 2e   if( p->snippet.
36613 6e 4d 61 74 63 68 20 7c 7c 20 70 2d 3e 70 45 78  nMatch || p->pEx
36614 70 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  pr==0 ){.    ret
36615 75 72 6e 3b 0a 20 20 7d 0a 20 20 6e 43 6f 6c 75  urn;.  }.  nColu
36616 6d 6e 20 3d 20 70 46 74 73 2d 3e 6e 43 6f 6c 75  mn = pFts->nColu
36617 6d 6e 3b 0a 20 20 69 43 6f 6c 75 6d 6e 20 3d 20  mn;.  iColumn = 
36618 28 70 2d 3e 69 43 75 72 73 6f 72 54 79 70 65 20  (p->iCursorType 
36619 2d 20 51 55 45 52 59 5f 46 55 4c 4c 54 45 58 54  - QUERY_FULLTEXT
3661a 29 3b 0a 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e  );.  if( iColumn
3661b 3c 30 20 7c 7c 20 69 43 6f 6c 75 6d 6e 3e 3d 6e  <0 || iColumn>=n
3661c 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 2f 2a  Column ){.    /*
3661d 20 4c 6f 6f 6b 20 66 6f 72 20 6d 61 74 63 68 65   Look for matche
3661e 73 20 6f 76 65 72 20 61 6c 6c 20 63 6f 6c 75 6d  s over all colum
3661f 6e 73 20 6f 66 20 74 68 65 20 66 75 6c 6c 2d 74  ns of the full-t
36620 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20  ext index */.   
36621 20 69 46 69 72 73 74 20 3d 20 30 3b 0a 20 20 20   iFirst = 0;.   
36622 20 69 4c 61 73 74 20 3d 20 6e 43 6f 6c 75 6d 6e   iLast = nColumn
36623 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  -1;.  }else{.   
36624 20 2f 2a 20 4c 6f 6f 6b 20 66 6f 72 20 6d 61 74   /* Look for mat
36625 63 68 65 73 20 69 6e 20 74 68 65 20 69 43 6f 6c  ches in the iCol
36626 75 6d 6e 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  umn-th column of
36627 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 6c 79 20   the index only 
36628 2a 2f 0a 20 20 20 20 69 46 69 72 73 74 20 3d 20  */.    iFirst = 
36629 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 69 4c 61  iColumn;.    iLa
3662a 73 74 20 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20  st = iColumn;.  
3662b 7d 0a 20 20 66 6f 72 28 69 3d 69 46 69 72 73 74  }.  for(i=iFirst
3662c 3b 20 69 3c 3d 69 4c 61 73 74 3b 20 69 2b 2b 29  ; i<=iLast; i++)
3662d 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
3662e 20 2a 7a 44 6f 63 3b 0a 20 20 20 20 69 6e 74 20   *zDoc;.    int 
3662f 6e 44 6f 63 3b 0a 20 20 20 20 7a 44 6f 63 20 3d  nDoc;.    zDoc =
36630 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71   (const char*)sq
36631 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
36632 74 28 70 2d 3e 70 53 74 6d 74 2c 20 69 2b 31 29  t(p->pStmt, i+1)
36633 3b 0a 20 20 20 20 6e 44 6f 63 20 3d 20 73 71 6c  ;.    nDoc = sql
36634 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
36635 73 28 70 2d 3e 70 53 74 6d 74 2c 20 69 2b 31 29  s(p->pStmt, i+1)
36636 3b 0a 20 20 20 20 73 6e 69 70 70 65 74 4f 66 66  ;.    snippetOff
36637 73 65 74 73 4f 66 43 6f 6c 75 6d 6e 28 70 2c 20  setsOfColumn(p, 
36638 26 70 2d 3e 73 6e 69 70 70 65 74 2c 20 69 2c 20  &p->snippet, i, 
36639 7a 44 6f 63 2c 20 6e 44 6f 63 29 3b 0a 20 20 7d  zDoc, nDoc);.  }
3663a 0a 0a 20 20 77 68 69 6c 65 28 20 74 72 69 6d 53  ..  while( trimS
3663b 6e 69 70 70 65 74 4f 66 66 73 65 74 73 28 70 2d  nippetOffsets(p-
3663c 3e 70 45 78 70 72 2c 20 26 70 2d 3e 73 6e 69 70  >pExpr, &p->snip
3663d 70 65 74 2c 20 26 69 54 65 72 6d 29 20 29 7b 0a  pet, &iTerm) ){.
3663e 20 20 20 20 69 54 65 72 6d 20 3d 20 30 3b 0a 20      iTerm = 0;. 
3663f 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76   }.}../*.** Conv
36640 65 72 74 20 74 68 65 20 69 6e 66 6f 72 6d 61 74  ert the informat
36641 69 6f 6e 20 69 6e 20 74 68 65 20 61 4d 61 74 63  ion in the aMatc
36642 68 5b 5d 20 61 72 72 61 79 20 6f 66 20 74 68 65  h[] array of the
36643 20 73 6e 69 70 70 65 74 0a 2a 2a 20 69 6e 74 6f   snippet.** into
36644 20 74 68 65 20 73 74 72 69 6e 67 20 7a 4f 66 66   the string zOff
36645 73 65 74 5b 30 2e 2e 6e 4f 66 66 73 65 74 2d 31  set[0..nOffset-1
36646 5d 2e 20 54 68 69 73 20 73 74 72 69 6e 67 20 69  ]. This string i
36647 73 20 75 73 65 64 20 61 73 0a 2a 2a 20 74 68 65  s used as.** the
36648 20 72 65 74 75 72 6e 20 6f 66 20 74 68 65 20 53   return of the S
36649 51 4c 20 6f 66 66 73 65 74 73 28 29 20 66 75 6e  QL offsets() fun
3664a 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ction..*/.static
3664b 20 76 6f 69 64 20 73 6e 69 70 70 65 74 4f 66 66   void snippetOff
3664c 73 65 74 54 65 78 74 28 53 6e 69 70 70 65 74 20  setText(Snippet 
3664d 2a 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  *p){.  int i;.  
3664e 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 53  int cnt = 0;.  S
3664f 74 72 69 6e 67 42 75 66 66 65 72 20 73 62 3b 0a  tringBuffer sb;.
36650 20 20 63 68 61 72 20 7a 42 75 66 5b 32 30 30 5d    char zBuf[200]
36651 3b 0a 20 20 69 66 28 20 70 2d 3e 7a 4f 66 66 73  ;.  if( p->zOffs
36652 65 74 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  et ) return;.  i
36653 6e 69 74 53 74 72 69 6e 67 42 75 66 66 65 72 28  nitStringBuffer(
36654 26 73 62 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  &sb);.  for(i=0;
36655 20 69 3c 70 2d 3e 6e 4d 61 74 63 68 3b 20 69 2b   i<p->nMatch; i+
36656 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 73  +){.    struct s
36657 6e 69 70 70 65 74 4d 61 74 63 68 20 2a 70 4d 61  nippetMatch *pMa
36658 74 63 68 20 3d 20 26 70 2d 3e 61 4d 61 74 63 68  tch = &p->aMatch
36659 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 4d 61  [i];.    if( pMa
3665a 74 63 68 2d 3e 69 54 65 72 6d 3e 3d 30 20 29 7b  tch->iTerm>=0 ){
3665b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 73 6e 69  .      /* If sni
3665c 70 70 65 74 4d 61 74 63 68 2e 69 54 65 72 6d 20  ppetMatch.iTerm 
3665d 69 73 20 6c 65 73 73 20 74 68 61 6e 20 30 2c 20  is less than 0, 
3665e 74 68 65 6e 20 74 68 65 20 6d 61 74 63 68 20 77  then the match w
3665f 61 73 20 0a 20 20 20 20 20 20 2a 2a 20 64 69 73  as .      ** dis
36660 63 61 72 64 65 64 20 61 73 20 70 61 72 74 20 6f  carded as part o
36661 66 20 70 72 6f 63 65 73 73 69 6e 67 20 74 68 65  f processing the
36662 20 4e 45 41 52 20 6f 70 65 72 61 74 6f 72 20 28   NEAR operator (
36663 73 65 65 20 74 68 65 20 0a 20 20 20 20 20 20 2a  see the .      *
36664 2a 20 74 72 69 6d 53 6e 69 70 70 65 74 4f 66 66  * trimSnippetOff
36665 73 65 74 73 46 6f 72 4e 65 61 72 28 29 20 66 75  setsForNear() fu
36666 6e 63 74 69 6f 6e 20 66 6f 72 20 64 65 74 61 69  nction for detai
36667 6c 73 29 2e 20 49 67 6e 6f 72 65 20 0a 20 20 20  ls). Ignore .   
36668 20 20 20 2a 2a 20 69 74 20 69 6e 20 74 68 69 73     ** it in this
36669 20 63 61 73 65 0a 20 20 20 20 20 20 2a 2f 0a 20   case.      */. 
3666a 20 20 20 20 20 7a 42 75 66 5b 30 5d 20 3d 20 27       zBuf[0] = '
3666b 20 27 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ';.      sqlite
3666c 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
3666d 66 28 7a 42 75 66 29 2d 31 2c 20 26 7a 42 75 66  f(zBuf)-1, &zBuf
3666e 5b 63 6e 74 3e 30 5d 2c 20 22 25 64 20 25 64 20  [cnt>0], "%d %d 
3666f 25 64 20 25 64 22 2c 0a 20 20 20 20 20 20 20 20  %d %d",.        
36670 20 20 70 4d 61 74 63 68 2d 3e 69 43 6f 6c 2c 20    pMatch->iCol, 
36671 70 4d 61 74 63 68 2d 3e 69 54 65 72 6d 2c 20 70  pMatch->iTerm, p
36672 4d 61 74 63 68 2d 3e 69 53 74 61 72 74 2c 20 70  Match->iStart, p
36673 4d 61 74 63 68 2d 3e 6e 42 79 74 65 29 3b 0a 20  Match->nByte);. 
36674 20 20 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c       append(&sb,
36675 20 7a 42 75 66 29 3b 0a 20 20 20 20 20 20 63 6e   zBuf);.      cn
36676 74 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  t++;.    }.  }. 
36677 20 70 2d 3e 7a 4f 66 66 73 65 74 20 3d 20 73 74   p->zOffset = st
36678 72 69 6e 67 42 75 66 66 65 72 44 61 74 61 28 26  ringBufferData(&
36679 73 62 29 3b 0a 20 20 70 2d 3e 6e 4f 66 66 73 65  sb);.  p->nOffse
3667a 74 20 3d 20 73 74 72 69 6e 67 42 75 66 66 65 72  t = stringBuffer
3667b 4c 65 6e 67 74 68 28 26 73 62 29 3b 0a 7d 0a 0a  Length(&sb);.}..
3667c 2f 2a 0a 2a 2a 20 7a 44 6f 63 5b 30 2e 2e 6e 44  /*.** zDoc[0..nD
3667d 6f 63 2d 31 5d 20 69 73 20 70 68 72 61 73 65 20  oc-1] is phrase 
3667e 6f 66 20 74 65 78 74 2e 20 20 61 4d 61 74 63 68  of text.  aMatch
3667f 5b 30 2e 2e 6e 4d 61 74 63 68 2d 31 5d 20 61 72  [0..nMatch-1] ar
36680 65 20 61 20 73 65 74 0a 2a 2a 20 6f 66 20 6d 61  e a set.** of ma
36681 74 63 68 69 6e 67 20 77 6f 72 64 73 20 73 6f 6d  tching words som
36682 65 20 6f 66 20 77 68 69 63 68 20 6d 69 67 68 74  e of which might
36683 20 62 65 20 69 6e 20 7a 44 6f 63 2e 20 20 7a 44   be in zDoc.  zD
36684 6f 63 20 69 73 20 63 6f 6c 75 6d 6e 0a 2a 2a 20  oc is column.** 
36685 6e 75 6d 62 65 72 20 69 43 6f 6c 2e 0a 2a 2a 0a  number iCol..**.
36686 2a 2a 20 69 42 72 65 61 6b 20 69 73 20 73 75 67  ** iBreak is sug
36687 67 65 73 74 65 64 20 73 70 6f 74 20 69 6e 20 7a  gested spot in z
36688 44 6f 63 20 77 68 65 72 65 20 77 65 20 63 6f 75  Doc where we cou
36689 6c 64 20 62 65 67 69 6e 20 6f 72 20 65 6e 64 20  ld begin or end 
3668a 61 6e 0a 2a 2a 20 65 78 63 65 72 70 74 2e 20 20  an.** excerpt.  
3668b 52 65 74 75 72 6e 20 61 20 76 61 6c 75 65 20 73  Return a value s
3668c 69 6d 69 6c 61 72 20 74 6f 20 69 42 72 65 61 6b  imilar to iBreak
3668d 20 62 75 74 20 70 6f 73 73 69 62 6c 79 20 61 64   but possibly ad
3668e 6a 75 73 74 65 64 0a 2a 2a 20 74 6f 20 62 65 20  justed.** to be 
3668f 61 20 6c 69 74 74 6c 65 20 6c 65 66 74 20 6f 72  a little left or
36690 20 72 69 67 68 74 20 73 6f 20 74 68 61 74 20 74   right so that t
36691 68 65 20 62 72 65 61 6b 20 70 6f 69 6e 74 20 69  he break point i
36692 73 20 62 65 74 74 65 72 2e 0a 2a 2f 0a 73 74 61  s better..*/.sta
36693 74 69 63 20 69 6e 74 20 77 6f 72 64 42 6f 75 6e  tic int wordBoun
36694 64 61 72 79 28 0a 20 20 69 6e 74 20 69 42 72 65  dary(.  int iBre
36695 61 6b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ak,             
36696 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 75 67        /* The sug
36697 67 65 73 74 65 64 20 62 72 65 61 6b 20 70 6f 69  gested break poi
36698 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  nt */.  const ch
36699 61 72 20 2a 7a 44 6f 63 2c 20 20 20 20 20 20 20  ar *zDoc,       
3669a 20 20 20 20 20 20 2f 2a 20 44 6f 63 75 6d 65 6e        /* Documen
3669b 74 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20  t text */.  int 
3669c 6e 44 6f 63 2c 20 20 20 20 20 20 20 20 20 20 20  nDoc,           
3669d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
3669e 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
3669f 7a 44 6f 63 5b 5d 20 2a 2f 0a 20 20 73 74 72 75  zDoc[] */.  stru
366a0 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20  ct snippetMatch 
366a1 2a 61 4d 61 74 63 68 2c 20 20 2f 2a 20 4d 61 74  *aMatch,  /* Mat
366a2 63 68 69 6e 67 20 77 6f 72 64 73 20 2a 2f 0a 20  ching words */. 
366a3 20 69 6e 74 20 6e 4d 61 74 63 68 2c 20 20 20 20   int nMatch,    
366a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
366a5 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
366a6 69 65 73 20 69 6e 20 61 4d 61 74 63 68 5b 5d 20  ies in aMatch[] 
366a7 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 20 20 20  */.  int iCol   
366a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
366a9 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e     /* The column
366aa 20 6e 75 6d 62 65 72 20 66 6f 72 20 7a 44 6f 63   number for zDoc
366ab 5b 5d 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  [] */.){.  int i
366ac 3b 0a 20 20 69 66 28 20 69 42 72 65 61 6b 3c 3d  ;.  if( iBreak<=
366ad 31 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  10 ){.    return
366ae 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69 42   0;.  }.  if( iB
366af 72 65 61 6b 3e 3d 6e 44 6f 63 2d 31 30 20 29 7b  reak>=nDoc-10 ){
366b0 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 44 6f 63  .    return nDoc
366b1 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b  ;.  }.  for(i=0;
366b2 20 69 3c 6e 4d 61 74 63 68 20 26 26 20 61 4d 61   i<nMatch && aMa
366b3 74 63 68 5b 69 5d 2e 69 43 6f 6c 3c 69 43 6f 6c  tch[i].iCol<iCol
366b4 3b 20 69 2b 2b 29 7b 7d 0a 20 20 77 68 69 6c 65  ; i++){}.  while
366b5 28 20 69 3c 6e 4d 61 74 63 68 20 26 26 20 61 4d  ( i<nMatch && aM
366b6 61 74 63 68 5b 69 5d 2e 69 53 74 61 72 74 2b 61  atch[i].iStart+a
366b7 4d 61 74 63 68 5b 69 5d 2e 6e 42 79 74 65 3c 69  Match[i].nByte<i
366b8 42 72 65 61 6b 20 29 7b 20 69 2b 2b 3b 20 7d 0a  Break ){ i++; }.
366b9 20 20 69 66 28 20 69 3c 6e 4d 61 74 63 68 20 29    if( i<nMatch )
366ba 7b 0a 20 20 20 20 69 66 28 20 61 4d 61 74 63 68  {.    if( aMatch
366bb 5b 69 5d 2e 69 53 74 61 72 74 3c 69 42 72 65 61  [i].iStart<iBrea
366bc 6b 2b 31 30 20 29 7b 0a 20 20 20 20 20 20 72 65  k+10 ){.      re
366bd 74 75 72 6e 20 61 4d 61 74 63 68 5b 69 5d 2e 69  turn aMatch[i].i
366be 53 74 61 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20  Start;.    }.   
366bf 20 69 66 28 20 69 3e 30 20 26 26 20 61 4d 61 74   if( i>0 && aMat
366c0 63 68 5b 69 2d 31 5d 2e 69 53 74 61 72 74 2b 61  ch[i-1].iStart+a
366c1 4d 61 74 63 68 5b 69 2d 31 5d 2e 6e 42 79 74 65  Match[i-1].nByte
366c2 3e 3d 69 42 72 65 61 6b 20 29 7b 0a 20 20 20 20  >=iBreak ){.    
366c3 20 20 72 65 74 75 72 6e 20 61 4d 61 74 63 68 5b    return aMatch[
366c4 69 2d 31 5d 2e 69 53 74 61 72 74 3b 0a 20 20 20  i-1].iStart;.   
366c5 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 31   }.  }.  for(i=1
366c6 3b 20 69 3c 3d 31 30 3b 20 69 2b 2b 29 7b 0a 20  ; i<=10; i++){. 
366c7 20 20 20 69 66 28 20 73 61 66 65 5f 69 73 73 70     if( safe_issp
366c8 61 63 65 28 7a 44 6f 63 5b 69 42 72 65 61 6b 2d  ace(zDoc[iBreak-
366c9 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  i]) ){.      ret
366ca 75 72 6e 20 69 42 72 65 61 6b 20 2d 20 69 20 2b  urn iBreak - i +
366cb 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   1;.    }.    if
366cc 28 20 73 61 66 65 5f 69 73 73 70 61 63 65 28 7a  ( safe_isspace(z
366cd 44 6f 63 5b 69 42 72 65 61 6b 2b 69 5d 29 20 29  Doc[iBreak+i]) )
366ce 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  {.      return i
366cf 42 72 65 61 6b 20 2b 20 69 20 2b 20 31 3b 0a 20  Break + i + 1;. 
366d0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
366d1 6e 20 69 42 72 65 61 6b 3b 0a 7d 0a 0a 0a 0a 2f  n iBreak;.}..../
366d2 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c  *.** Allowed val
366d3 75 65 73 20 66 6f 72 20 53 6e 69 70 70 65 74 2e  ues for Snippet.
366d4 61 4d 61 74 63 68 5b 5d 2e 73 6e 53 74 61 74 75  aMatch[].snStatu
366d5 73 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 4e 49  s.*/.#define SNI
366d6 50 50 45 54 5f 49 47 4e 4f 52 45 20 20 30 20 20  PPET_IGNORE  0  
366d7 20 2f 2a 20 49 74 20 69 73 20 6f 6b 20 74 6f 20   /* It is ok to 
366d8 6f 6d 69 74 20 74 68 69 73 20 6d 61 74 63 68 20  omit this match 
366d9 66 72 6f 6d 20 74 68 65 20 73 6e 69 70 70 65 74  from the snippet
366da 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 4e 49 50   */.#define SNIP
366db 50 45 54 5f 44 45 53 49 52 45 44 20 31 20 20 20  PET_DESIRED 1   
366dc 2f 2a 20 57 65 20 77 61 6e 74 20 74 6f 20 69 6e  /* We want to in
366dd 63 6c 75 64 65 20 74 68 69 73 20 6d 61 74 63 68  clude this match
366de 20 69 6e 20 74 68 65 20 73 6e 69 70 70 65 74 20   in the snippet 
366df 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  */../*.** Genera
366e0 74 65 20 74 68 65 20 74 65 78 74 20 6f 66 20 61  te the text of a
366e1 20 73 6e 69 70 70 65 74 2e 0a 2a 2f 0a 73 74 61   snippet..*/.sta
366e2 74 69 63 20 76 6f 69 64 20 73 6e 69 70 70 65 74  tic void snippet
366e3 54 65 78 74 28 0a 20 20 66 75 6c 6c 74 65 78 74  Text(.  fulltext
366e4 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
366e5 2c 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f  ,   /* The curso
366e6 72 20 77 65 20 6e 65 65 64 20 74 68 65 20 73 6e  r we need the sn
366e7 69 70 70 65 74 20 66 6f 72 20 2a 2f 0a 20 20 63  ippet for */.  c
366e8 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 61 72  onst char *zStar
366e9 74 4d 61 72 6b 2c 20 20 20 20 20 2f 2a 20 4d 61  tMark,     /* Ma
366ea 72 6b 75 70 20 74 6f 20 61 70 70 65 61 72 20 62  rkup to appear b
366eb 65 66 6f 72 65 20 65 61 63 68 20 6d 61 74 63 68  efore each match
366ec 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
366ed 20 2a 7a 45 6e 64 4d 61 72 6b 2c 20 20 20 20 20   *zEndMark,     
366ee 20 20 2f 2a 20 4d 61 72 6b 75 70 20 74 6f 20 61    /* Markup to a
366ef 70 70 65 61 72 20 61 66 74 65 72 20 65 61 63 68  ppear after each
366f0 20 6d 61 74 63 68 20 2a 2f 0a 20 20 63 6f 6e 73   match */.  cons
366f1 74 20 63 68 61 72 20 2a 7a 45 6c 6c 69 70 73 69  t char *zEllipsi
366f2 73 20 20 20 20 20 20 20 2f 2a 20 45 6c 6c 69 70  s       /* Ellip
366f3 73 69 73 20 6d 61 72 6b 20 2a 2f 0a 29 7b 0a 20  sis mark */.){. 
366f4 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74 72   int i, j;.  str
366f5 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68  uct snippetMatch
366f6 20 2a 61 4d 61 74 63 68 3b 0a 20 20 69 6e 74 20   *aMatch;.  int 
366f7 6e 4d 61 74 63 68 3b 0a 20 20 69 6e 74 20 6e 44  nMatch;.  int nD
366f8 65 73 69 72 65 64 3b 0a 20 20 53 74 72 69 6e 67  esired;.  String
366f9 42 75 66 66 65 72 20 73 62 3b 0a 20 20 69 6e 74  Buffer sb;.  int
366fa 20 74 61 69 6c 43 6f 6c 3b 0a 20 20 69 6e 74 20   tailCol;.  int 
366fb 74 61 69 6c 4f 66 66 73 65 74 3b 0a 20 20 69 6e  tailOffset;.  in
366fc 74 20 69 43 6f 6c 3b 0a 20 20 69 6e 74 20 6e 44  t iCol;.  int nD
366fd 6f 63 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  oc;.  const char
366fe 20 2a 7a 44 6f 63 3b 0a 20 20 69 6e 74 20 69 53   *zDoc;.  int iS
366ff 74 61 72 74 2c 20 69 45 6e 64 3b 0a 20 20 69 6e  tart, iEnd;.  in
36700 74 20 74 61 69 6c 45 6c 6c 69 70 73 69 73 20 3d  t tailEllipsis =
36701 20 30 3b 0a 20 20 69 6e 74 20 69 4d 61 74 63 68   0;.  int iMatch
36702 3b 0a 20 20 0a 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  ..  sqlite3_
36703 66 72 65 65 28 70 43 75 72 73 6f 72 2d 3e 73 6e  free(pCursor->sn
36704 69 70 70 65 74 2e 7a 53 6e 69 70 70 65 74 29 3b  ippet.zSnippet);
36705 0a 20 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70  .  pCursor->snip
36706 70 65 74 2e 7a 53 6e 69 70 70 65 74 20 3d 20 30  pet.zSnippet = 0
36707 3b 0a 20 20 61 4d 61 74 63 68 20 3d 20 70 43 75  ;.  aMatch = pCu
36708 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 61 4d  rsor->snippet.aM
36709 61 74 63 68 3b 0a 20 20 6e 4d 61 74 63 68 20 3d  atch;.  nMatch =
3670a 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65   pCursor->snippe
3670b 74 2e 6e 4d 61 74 63 68 3b 0a 20 20 69 6e 69 74  t.nMatch;.  init
3670c 53 74 72 69 6e 67 42 75 66 66 65 72 28 26 73 62  StringBuffer(&sb
3670d 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  );..  for(i=0; i
3670e 3c 6e 4d 61 74 63 68 3b 20 69 2b 2b 29 7b 0a 20  <nMatch; i++){. 
3670f 20 20 20 61 4d 61 74 63 68 5b 69 5d 2e 73 6e 53     aMatch[i].snS
36710 74 61 74 75 73 20 3d 20 53 4e 49 50 50 45 54 5f  tatus = SNIPPET_
36711 49 47 4e 4f 52 45 3b 0a 20 20 7d 0a 20 20 6e 44  IGNORE;.  }.  nD
36712 65 73 69 72 65 64 20 3d 20 30 3b 0a 20 20 66 6f  esired = 0;.  fo
36713 72 28 69 3d 30 3b 20 69 3c 46 54 53 33 5f 52 4f  r(i=0; i<FTS3_RO
36714 54 4f 52 5f 53 5a 3b 20 69 2b 2b 29 7b 0a 20 20  TOR_SZ; i++){.  
36715 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 4d 61    for(j=0; j<nMa
36716 74 63 68 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  tch; j++){.     
36717 20 69 66 28 20 61 4d 61 74 63 68 5b 6a 5d 2e 69   if( aMatch[j].i
36718 54 65 72 6d 3d 3d 69 20 29 7b 0a 20 20 20 20 20  Term==i ){.     
36719 20 20 20 61 4d 61 74 63 68 5b 6a 5d 2e 73 6e 53     aMatch[j].snS
3671a 74 61 74 75 73 20 3d 20 53 4e 49 50 50 45 54 5f  tatus = SNIPPET_
3671b 44 45 53 49 52 45 44 3b 0a 20 20 20 20 20 20 20  DESIRED;.       
3671c 20 6e 44 65 73 69 72 65 64 2b 2b 3b 0a 20 20 20   nDesired++;.   
3671d 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3671e 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20    }.    }.  }.. 
3671f 20 69 4d 61 74 63 68 20 3d 20 30 3b 0a 20 20 74   iMatch = 0;.  t
36720 61 69 6c 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 74  ailCol = -1;.  t
36721 61 69 6c 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20  ailOffset = 0;. 
36722 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4d 61 74   for(i=0; i<nMat
36723 63 68 20 26 26 20 6e 44 65 73 69 72 65 64 3e 30  ch && nDesired>0
36724 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
36725 61 4d 61 74 63 68 5b 69 5d 2e 73 6e 53 74 61 74  aMatch[i].snStat
36726 75 73 21 3d 53 4e 49 50 50 45 54 5f 44 45 53 49  us!=SNIPPET_DESI
36727 52 45 44 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  RED ) continue;.
36728 20 20 20 20 6e 44 65 73 69 72 65 64 2d 2d 3b 0a      nDesired--;.
36729 20 20 20 20 69 43 6f 6c 20 3d 20 61 4d 61 74 63      iCol = aMatc
3672a 68 5b 69 5d 2e 69 43 6f 6c 3b 0a 20 20 20 20 7a  h[i].iCol;.    z
3672b 44 6f 63 20 3d 20 28 63 6f 6e 73 74 20 63 68 61  Doc = (const cha
3672c 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  r*)sqlite3_colum
3672d 6e 5f 74 65 78 74 28 70 43 75 72 73 6f 72 2d 3e  n_text(pCursor->
3672e 70 53 74 6d 74 2c 20 69 43 6f 6c 2b 31 29 3b 0a  pStmt, iCol+1);.
3672f 20 20 20 20 6e 44 6f 63 20 3d 20 73 71 6c 69 74      nDoc = sqlit
36730 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
36731 70 43 75 72 73 6f 72 2d 3e 70 53 74 6d 74 2c 20  pCursor->pStmt, 
36732 69 43 6f 6c 2b 31 29 3b 0a 20 20 20 20 69 53 74  iCol+1);.    iSt
36733 61 72 74 20 3d 20 61 4d 61 74 63 68 5b 69 5d 2e  art = aMatch[i].
36734 69 53 74 61 72 74 20 2d 20 34 30 3b 0a 20 20 20  iStart - 40;.   
36735 20 69 53 74 61 72 74 20 3d 20 77 6f 72 64 42 6f   iStart = wordBo
36736 75 6e 64 61 72 79 28 69 53 74 61 72 74 2c 20 7a  undary(iStart, z
36737 44 6f 63 2c 20 6e 44 6f 63 2c 20 61 4d 61 74 63  Doc, nDoc, aMatc
36738 68 2c 20 6e 4d 61 74 63 68 2c 20 69 43 6f 6c 29  h, nMatch, iCol)
36739 3b 0a 20 20 20 20 69 66 28 20 69 53 74 61 72 74  ;.    if( iStart
3673a 3c 3d 31 30 20 29 7b 0a 20 20 20 20 20 20 69 53  <=10 ){.      iS
3673b 74 61 72 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  tart = 0;.    }.
3673c 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 74 61      if( iCol==ta
3673d 69 6c 43 6f 6c 20 26 26 20 69 53 74 61 72 74 3c  ilCol && iStart<
3673e 3d 74 61 69 6c 4f 66 66 73 65 74 2b 32 30 20 29  =tailOffset+20 )
3673f 7b 0a 20 20 20 20 20 20 69 53 74 61 72 74 20 3d  {.      iStart =
36740 20 74 61 69 6c 4f 66 66 73 65 74 3b 0a 20 20 20   tailOffset;.   
36741 20 7d 0a 20 20 20 20 69 66 28 20 28 69 43 6f 6c   }.    if( (iCol
36742 21 3d 74 61 69 6c 43 6f 6c 20 26 26 20 74 61 69  !=tailCol && tai
36743 6c 43 6f 6c 3e 3d 30 29 20 7c 7c 20 69 53 74 61  lCol>=0) || iSta
36744 72 74 21 3d 74 61 69 6c 4f 66 66 73 65 74 20 29  rt!=tailOffset )
36745 7b 0a 20 20 20 20 20 20 74 72 69 6d 57 68 69 74  {.      trimWhit
36746 65 53 70 61 63 65 28 26 73 62 29 3b 0a 20 20 20  eSpace(&sb);.   
36747 20 20 20 61 70 70 65 6e 64 57 68 69 74 65 53 70     appendWhiteSp
36748 61 63 65 28 26 73 62 29 3b 0a 20 20 20 20 20 20  ace(&sb);.      
36749 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 45 6c 6c  append(&sb, zEll
3674a 69 70 73 69 73 29 3b 0a 20 20 20 20 20 20 61 70  ipsis);.      ap
3674b 70 65 6e 64 57 68 69 74 65 53 70 61 63 65 28 26  pendWhiteSpace(&
3674c 73 62 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  sb);.    }.    i
3674d 45 6e 64 20 3d 20 61 4d 61 74 63 68 5b 69 5d 2e  End = aMatch[i].
3674e 69 53 74 61 72 74 20 2b 20 61 4d 61 74 63 68 5b  iStart + aMatch[
3674f 69 5d 2e 6e 42 79 74 65 20 2b 20 34 30 3b 0a 20  i].nByte + 40;. 
36750 20 20 20 69 45 6e 64 20 3d 20 77 6f 72 64 42 6f     iEnd = wordBo
36751 75 6e 64 61 72 79 28 69 45 6e 64 2c 20 7a 44 6f  undary(iEnd, zDo
36752 63 2c 20 6e 44 6f 63 2c 20 61 4d 61 74 63 68 2c  c, nDoc, aMatch,
36753 20 6e 4d 61 74 63 68 2c 20 69 43 6f 6c 29 3b 0a   nMatch, iCol);.
36754 20 20 20 20 69 66 28 20 69 45 6e 64 3e 3d 6e 44      if( iEnd>=nD
36755 6f 63 2d 31 30 20 29 7b 0a 20 20 20 20 20 20 69  oc-10 ){.      i
36756 45 6e 64 20 3d 20 6e 44 6f 63 3b 0a 20 20 20 20  End = nDoc;.    
36757 20 20 74 61 69 6c 45 6c 6c 69 70 73 69 73 20 3d    tailEllipsis =
36758 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   0;.    }else{. 
36759 20 20 20 20 20 74 61 69 6c 45 6c 6c 69 70 73 69       tailEllipsi
3675a 73 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  s = 1;.    }.   
3675b 20 77 68 69 6c 65 28 20 69 4d 61 74 63 68 3c 6e   while( iMatch<n
3675c 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63 68 5b  Match && aMatch[
3675d 69 4d 61 74 63 68 5d 2e 69 43 6f 6c 3c 69 43 6f  iMatch].iCol<iCo
3675e 6c 20 29 7b 20 69 4d 61 74 63 68 2b 2b 3b 20 7d  l ){ iMatch++; }
3675f 0a 20 20 20 20 77 68 69 6c 65 28 20 69 53 74 61  .    while( iSta
36760 72 74 3c 69 45 6e 64 20 29 7b 0a 20 20 20 20 20  rt<iEnd ){.     
36761 20 77 68 69 6c 65 28 20 69 4d 61 74 63 68 3c 6e   while( iMatch<n
36762 4d 61 74 63 68 20 26 26 20 61 4d 61 74 63 68 5b  Match && aMatch[
36763 69 4d 61 74 63 68 5d 2e 69 53 74 61 72 74 3c 69  iMatch].iStart<i
36764 53 74 61 72 74 0a 20 20 20 20 20 20 20 20 20 20  Start.          
36765 20 20 20 26 26 20 61 4d 61 74 63 68 5b 69 4d 61     && aMatch[iMa
36766 74 63 68 5d 2e 69 43 6f 6c 3c 3d 69 43 6f 6c 20  tch].iCol<=iCol 
36767 29 7b 0a 20 20 20 20 20 20 20 20 69 4d 61 74 63  ){.        iMatc
36768 68 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  h++;.      }.   
36769 20 20 20 69 66 28 20 69 4d 61 74 63 68 3c 6e 4d     if( iMatch<nM
3676a 61 74 63 68 20 26 26 20 61 4d 61 74 63 68 5b 69  atch && aMatch[i
3676b 4d 61 74 63 68 5d 2e 69 53 74 61 72 74 3c 69 45  Match].iStart<iE
3676c 6e 64 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  nd.             
3676d 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68  && aMatch[iMatch
3676e 5d 2e 69 43 6f 6c 3d 3d 69 43 6f 6c 20 29 7b 0a  ].iCol==iCol ){.
3676f 20 20 20 20 20 20 20 20 6e 61 70 70 65 6e 64 28          nappend(
36770 26 73 62 2c 20 26 7a 44 6f 63 5b 69 53 74 61 72  &sb, &zDoc[iStar
36771 74 5d 2c 20 61 4d 61 74 63 68 5b 69 4d 61 74 63  t], aMatch[iMatc
36772 68 5d 2e 69 53 74 61 72 74 20 2d 20 69 53 74 61  h].iStart - iSta
36773 72 74 29 3b 0a 20 20 20 20 20 20 20 20 69 53 74  rt);.        iSt
36774 61 72 74 20 3d 20 61 4d 61 74 63 68 5b 69 4d 61  art = aMatch[iMa
36775 74 63 68 5d 2e 69 53 74 61 72 74 3b 0a 20 20 20  tch].iStart;.   
36776 20 20 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c       append(&sb,
36777 20 7a 53 74 61 72 74 4d 61 72 6b 29 3b 0a 20 20   zStartMark);.  
36778 20 20 20 20 20 20 6e 61 70 70 65 6e 64 28 26 73        nappend(&s
36779 62 2c 20 26 7a 44 6f 63 5b 69 53 74 61 72 74 5d  b, &zDoc[iStart]
3677a 2c 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d  , aMatch[iMatch]
3677b 2e 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 20  .nByte);.       
3677c 20 61 70 70 65 6e 64 28 26 73 62 2c 20 7a 45 6e   append(&sb, zEn
3677d 64 4d 61 72 6b 29 3b 0a 20 20 20 20 20 20 20 20  dMark);.        
3677e 69 53 74 61 72 74 20 2b 3d 20 61 4d 61 74 63 68  iStart += aMatch
3677f 5b 69 4d 61 74 63 68 5d 2e 6e 42 79 74 65 3b 0a  [iMatch].nByte;.
36780 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d 69 4d          for(j=iM
36781 61 74 63 68 2b 31 3b 20 6a 3c 6e 4d 61 74 63 68  atch+1; j<nMatch
36782 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; j++){.        
36783 20 20 69 66 28 20 61 4d 61 74 63 68 5b 6a 5d 2e    if( aMatch[j].
36784 69 54 65 72 6d 3d 3d 61 4d 61 74 63 68 5b 69 4d  iTerm==aMatch[iM
36785 61 74 63 68 5d 2e 69 54 65 72 6d 0a 20 20 20 20  atch].iTerm.    
36786 20 20 20 20 20 20 20 20 20 20 26 26 20 61 4d 61            && aMa
36787 74 63 68 5b 6a 5d 2e 73 6e 53 74 61 74 75 73 3d  tch[j].snStatus=
36788 3d 53 4e 49 50 50 45 54 5f 44 45 53 49 52 45 44  =SNIPPET_DESIRED
36789 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
3678a 6e 44 65 73 69 72 65 64 2d 2d 3b 0a 20 20 20 20  nDesired--;.    
3678b 20 20 20 20 20 20 20 20 61 4d 61 74 63 68 5b 6a          aMatch[j
3678c 5d 2e 73 6e 53 74 61 74 75 73 20 3d 20 53 4e 49  ].snStatus = SNI
3678d 50 50 45 54 5f 49 47 4e 4f 52 45 3b 0a 20 20 20  PPET_IGNORE;.   
3678e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
3678f 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a   }.      }else{.
36790 20 20 20 20 20 20 20 20 6e 61 70 70 65 6e 64 28          nappend(
36791 26 73 62 2c 20 26 7a 44 6f 63 5b 69 53 74 61 72  &sb, &zDoc[iStar
36792 74 5d 2c 20 69 45 6e 64 20 2d 20 69 53 74 61 72  t], iEnd - iStar
36793 74 29 3b 0a 20 20 20 20 20 20 20 20 69 53 74 61  t);.        iSta
36794 72 74 20 3d 20 69 45 6e 64 3b 0a 20 20 20 20 20  rt = iEnd;.     
36795 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 74 61 69   }.    }.    tai
36796 6c 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a 20 20 20  lCol = iCol;.   
36797 20 74 61 69 6c 4f 66 66 73 65 74 20 3d 20 69 45   tailOffset = iE
36798 6e 64 3b 0a 20 20 7d 0a 20 20 74 72 69 6d 57 68  nd;.  }.  trimWh
36799 69 74 65 53 70 61 63 65 28 26 73 62 29 3b 0a 20  iteSpace(&sb);. 
3679a 20 69 66 28 20 74 61 69 6c 45 6c 6c 69 70 73 69   if( tailEllipsi
3679b 73 20 29 7b 0a 20 20 20 20 61 70 70 65 6e 64 57  s ){.    appendW
3679c 68 69 74 65 53 70 61 63 65 28 26 73 62 29 3b 0a  hiteSpace(&sb);.
3679d 20 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20      append(&sb, 
3679e 7a 45 6c 6c 69 70 73 69 73 29 3b 0a 20 20 7d 0a  zEllipsis);.  }.
3679f 20 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70    pCursor->snipp
367a0 65 74 2e 7a 53 6e 69 70 70 65 74 20 3d 20 73 74  et.zSnippet = st
367a1 72 69 6e 67 42 75 66 66 65 72 44 61 74 61 28 26  ringBufferData(&
367a2 73 62 29 3b 0a 20 20 70 43 75 72 73 6f 72 2d 3e  sb);.  pCursor->
367a3 73 6e 69 70 70 65 74 2e 6e 53 6e 69 70 70 65 74  snippet.nSnippet
367a4 20 3d 20 73 74 72 69 6e 67 42 75 66 66 65 72 4c   = stringBufferL
367a5 65 6e 67 74 68 28 26 73 62 29 3b 0a 7d 0a 0a 0a  ength(&sb);.}...
367a6 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20  /*.** Close the 
367a7 63 75 72 73 6f 72 2e 20 20 46 6f 72 20 61 64 64  cursor.  For add
367a8 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
367a9 69 6f 6e 20 73 65 65 20 74 68 65 20 64 6f 63 75  ion see the docu
367aa 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 6e 20  mentation.** on 
367ab 74 68 65 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f  the xClose metho
367ac 64 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  d of the virtual
367ad 20 74 61 62 6c 65 20 69 6e 74 65 72 66 61 63 65   table interface
367ae 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
367af 66 75 6c 6c 74 65 78 74 43 6c 6f 73 65 28 73 71  fulltextClose(sq
367b0 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
367b1 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 66  r *pCursor){.  f
367b2 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a  ulltext_cursor *
367b3 63 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63 75  c = (fulltext_cu
367b4 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b  rsor *) pCursor;
367b5 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54  .  FTSTRACE(("FT
367b6 53 33 20 43 6c 6f 73 65 20 25 70 5c 6e 22 2c 20  S3 Close %p\n", 
367b7 63 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  c));.  sqlite3_f
367b8 69 6e 61 6c 69 7a 65 28 63 2d 3e 70 53 74 6d 74  inalize(c->pStmt
367b9 29 3b 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33  );.  sqlite3Fts3
367ba 45 78 70 72 46 72 65 65 28 63 2d 3e 70 45 78 70  ExprFree(c->pExp
367bb 72 29 3b 0a 20 20 73 6e 69 70 70 65 74 43 6c 65  r);.  snippetCle
367bc 61 72 28 26 63 2d 3e 73 6e 69 70 70 65 74 29 3b  ar(&c->snippet);
367bd 0a 20 20 69 66 28 20 63 2d 3e 72 65 73 75 6c 74  .  if( c->result
367be 2e 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20  .nData!=0 ){.   
367bf 20 64 6c 72 44 65 73 74 72 6f 79 28 26 63 2d 3e   dlrDestroy(&c->
367c0 72 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20 20 64  reader);.  }.  d
367c1 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
367c2 28 26 63 2d 3e 72 65 73 75 6c 74 29 3b 0a 20 20  (&c->result);.  
367c3 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63 29 3b  sqlite3_free(c);
367c4 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
367c5 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69  _OK;.}..static i
367c6 6e 74 20 66 75 6c 6c 74 65 78 74 4e 65 78 74 28  nt fulltextNext(
367c7 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
367c8 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20  sor *pCursor){. 
367c9 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72   fulltext_cursor
367ca 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f   *c = (fulltext_
367cb 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f  cursor *) pCurso
367cc 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  r;.  int rc;..  
367cd 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20  FTSTRACE(("FTS3 
367ce 4e 65 78 74 20 25 70 5c 6e 22 2c 20 70 43 75 72  Next %p\n", pCur
367cf 73 6f 72 29 29 3b 0a 20 20 73 6e 69 70 70 65 74  sor));.  snippet
367d0 43 6c 65 61 72 28 26 63 2d 3e 73 6e 69 70 70 65  Clear(&c->snippe
367d1 74 29 3b 0a 20 20 69 66 28 20 63 2d 3e 69 43 75  t);.  if( c->iCu
367d2 72 73 6f 72 54 79 70 65 20 3c 20 51 55 45 52 59  rsorType < QUERY
367d3 5f 46 55 4c 4c 54 45 58 54 20 29 7b 0a 20 20 20  _FULLTEXT ){.   
367d4 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
367d5 48 61 6e 64 6c 65 20 53 51 4c 49 54 45 5f 53 43  Handle SQLITE_SC
367d6 48 45 4d 41 20 41 4e 44 20 53 51 4c 49 54 45 5f  HEMA AND SQLITE_
367d7 42 55 53 59 2e 20 2a 2f 0a 20 20 20 20 72 63 20  BUSY. */.    rc 
367d8 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 63  = sqlite3_step(c
367d9 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 73 77  ->pStmt);.    sw
367da 69 74 63 68 28 20 72 63 20 29 7b 0a 20 20 20 20  itch( rc ){.    
367db 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 52 4f    case SQLITE_RO
367dc 57 3a 0a 20 20 20 20 20 20 20 20 63 2d 3e 65 6f  W:.        c->eo
367dd 66 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 72  f = 0;.        r
367de 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
367df 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49  .      case SQLI
367e0 54 45 5f 44 4f 4e 45 3a 0a 20 20 20 20 20 20 20  TE_DONE:.       
367e1 20 63 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20 20 20   c->eof = 1;.   
367e2 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
367e3 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 64 65 66  TE_OK;.      def
367e4 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20 63 2d  ault:.        c-
367e5 3e 65 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20  >eof = 1;.      
367e6 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
367e7 20 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 20 20 2f   }.  } else {  /
367e8 2a 20 66 75 6c 6c 2d 74 65 78 74 20 71 75 65 72  * full-text quer
367e9 79 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71  y */.    rc = sq
367ea 6c 69 74 65 33 5f 72 65 73 65 74 28 63 2d 3e 70  lite3_reset(c->p
367eb 53 74 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 72  Stmt);.    if( r
367ec 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
367ed 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 69  eturn rc;..    i
367ee 66 28 20 63 2d 3e 72 65 73 75 6c 74 2e 6e 44 61  f( c->result.nDa
367ef 74 61 3d 3d 30 20 7c 7c 20 64 6c 72 41 74 45 6e  ta==0 || dlrAtEn
367f0 64 28 26 63 2d 3e 72 65 61 64 65 72 29 20 29 7b  d(&c->reader) ){
367f1 0a 20 20 20 20 20 20 63 2d 3e 65 6f 66 20 3d 20  .      c->eof = 
367f2 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  1;.      return 
367f3 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
367f4 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
367f5 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 63 2d 3e  3_bind_int64(c->
367f6 70 53 74 6d 74 2c 20 31 2c 20 64 6c 72 44 6f 63  pStmt, 1, dlrDoc
367f7 69 64 28 26 63 2d 3e 72 65 61 64 65 72 29 29 3b  id(&c->reader));
367f8 0a 20 20 20 20 64 6c 72 53 74 65 70 28 26 63 2d  .    dlrStep(&c-
367f9 3e 72 65 61 64 65 72 29 3b 0a 20 20 20 20 69 66  >reader);.    if
367fa 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
367fb 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
367fc 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20   /* TODO(shess) 
367fd 48 61 6e 64 6c 65 20 53 51 4c 49 54 45 5f 53 43  Handle SQLITE_SC
367fe 48 45 4d 41 20 41 4e 44 20 53 51 4c 49 54 45 5f  HEMA AND SQLITE_
367ff 42 55 53 59 2e 20 2a 2f 0a 20 20 20 20 72 63 20  BUSY. */.    rc 
36800 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 63  = sqlite3_step(c
36801 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 69 66  ->pStmt);.    if
36802 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc==SQLITE_ROW
36803 20 29 7b 20 20 20 2f 2a 20 74 68 65 20 63 61 73   ){   /* the cas
36804 65 20 77 65 20 65 78 70 65 63 74 20 2a 2f 0a 20  e we expect */. 
36805 20 20 20 20 20 63 2d 3e 65 6f 66 20 3d 20 30 3b       c->eof = 0;
36806 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
36807 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20  LITE_OK;.    }. 
36808 20 20 20 2f 2a 20 61 6e 20 65 72 72 6f 72 20 6f     /* an error o
36809 63 63 75 72 72 65 64 3b 20 61 62 6f 72 74 20 2a  ccurred; abort *
3680a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3d  /.    return rc=
3680b 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3f 20 53  =SQLITE_DONE ? S
3680c 51 4c 49 54 45 5f 45 52 52 4f 52 20 3a 20 72 63  QLITE_ERROR : rc
3680d 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 20 54 4f 44  ;.  }.}.../* TOD
3680e 4f 28 73 68 65 73 73 29 20 49 66 20 77 65 20 70  O(shess) If we p
3680f 75 73 68 65 64 20 4c 65 61 66 52 65 61 64 65 72  ushed LeafReader
36810 20 74 6f 20 74 68 65 20 74 6f 70 20 6f 66 20 74   to the top of t
36811 68 65 20 66 69 6c 65 2c 20 6f 72 20 74 6f 0a 2a  he file, or to.*
36812 2a 20 61 6e 6f 74 68 65 72 20 66 69 6c 65 2c 20  * another file, 
36813 74 65 72 6d 5f 73 65 6c 65 63 74 28 29 20 63 6f  term_select() co
36814 75 6c 64 20 62 65 20 70 75 73 68 65 64 20 61 62  uld be pushed ab
36815 6f 76 65 0a 2a 2a 20 64 6f 63 4c 69 73 74 4f 66  ove.** docListOf
36816 54 65 72 6d 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  Term()..*/.stati
36817 63 20 69 6e 74 20 74 65 72 6d 53 65 6c 65 63 74  c int termSelect
36818 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
36819 76 2c 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 0a  v, int iColumn,.
3681a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3681b 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
3681c 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65   *pTerm, int nTe
3681d 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78  rm, int isPrefix
3681e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
3681f 20 20 20 20 20 20 20 20 44 6f 63 4c 69 73 74 54          DocListT
36820 79 70 65 20 69 54 79 70 65 2c 20 44 61 74 61 42  ype iType, DataB
36821 75 66 66 65 72 20 2a 6f 75 74 29 3b 0a 0a 2f 2a  uffer *out);../*
36822 20 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 44 6f   .** Return a Do
36823 63 4c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64  cList correspond
36824 69 6e 67 20 74 6f 20 74 68 65 20 70 68 72 61 73  ing to the phras
36825 65 20 2a 70 50 68 72 61 73 65 2e 0a 2a 2a 0a 2a  e *pPhrase..**.*
36826 2a 20 54 68 65 20 72 65 73 75 6c 74 69 6e 67 20  * The resulting 
36827 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69 73  DL_DOCIDS doclis
36828 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70  t is stored in p
36829 52 65 73 75 6c 74 2c 20 77 68 69 63 68 20 69 73  Result, which is
3682a 0a 2a 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 2e  .** overwritten.
3682b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
3682c 6f 63 4c 69 73 74 4f 66 50 68 72 61 73 65 28 0a  ocListOfPhrase(.
3682d 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20    fulltext_vtab 
3682e 2a 70 54 61 62 2c 20 20 20 2f 2a 20 54 68 65 20  *pTab,   /* The 
3682f 66 75 6c 6c 20 74 65 78 74 20 69 6e 64 65 78 20  full text index 
36830 2a 2f 0a 20 20 46 74 73 33 50 68 72 61 73 65 20  */.  Fts3Phrase 
36831 2a 70 50 68 72 61 73 65 2c 20 20 20 2f 2a 20 50  *pPhrase,   /* P
36832 68 72 61 73 65 20 74 6f 20 72 65 74 75 72 6e 20  hrase to return 
36833 61 20 64 6f 63 6c 69 73 74 20 63 6f 72 72 65 73  a doclist corres
36834 70 6f 6e 64 69 6e 67 20 74 6f 20 2a 2f 0a 20 20  ponding to */.  
36835 44 6f 63 4c 69 73 74 54 79 70 65 20 65 4c 69 73  DocListType eLis
36836 74 54 79 70 65 2c 20 2f 2a 20 45 69 74 68 65 72  tType, /* Either
36837 20 44 4c 5f 44 4f 43 49 44 53 20 6f 72 20 44 4c   DL_DOCIDS or DL
36838 5f 50 4f 53 49 54 49 4f 4e 53 20 2a 2f 0a 20 20  _POSITIONS */.  
36839 44 61 74 61 42 75 66 66 65 72 20 2a 70 52 65 73  DataBuffer *pRes
3683a 75 6c 74 20 20 20 20 2f 2a 20 57 72 69 74 65 20  ult    /* Write 
3683b 74 68 65 20 72 65 73 75 6c 74 20 68 65 72 65 20  the result here 
3683c 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a  */.){.  int ii;.
3683d 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
3683e 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 43 6f 6c  E_OK;.  int iCol
3683f 20 3d 20 70 50 68 72 61 73 65 2d 3e 69 43 6f 6c   = pPhrase->iCol
36840 75 6d 6e 3b 0a 20 20 44 6f 63 4c 69 73 74 54 79  umn;.  DocListTy
36841 70 65 20 65 54 79 70 65 20 3d 20 65 4c 69 73 74  pe eType = eList
36842 54 79 70 65 3b 0a 20 20 61 73 73 65 72 74 28 20  Type;.  assert( 
36843 65 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49 54 49  eType==DL_POSITI
36844 4f 4e 53 20 7c 7c 20 65 54 79 70 65 3d 3d 44 4c  ONS || eType==DL
36845 5f 44 4f 43 49 44 53 20 29 3b 0a 20 20 69 66 28  _DOCIDS );.  if(
36846 20 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e   pPhrase->nToken
36847 3e 31 20 29 7b 0a 20 20 20 20 65 54 79 70 65 20  >1 ){.    eType 
36848 3d 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 3b 0a  = DL_POSITIONS;.
36849 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63    }..  /* This c
3684a 6f 64 65 20 73 68 6f 75 6c 64 20 6e 65 76 65 72  ode should never
3684b 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68 20   be called with 
3684c 62 75 66 66 65 72 65 64 20 75 70 64 61 74 65 73  buffered updates
3684d 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  . */.  assert( p
3684e 54 61 62 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74  Tab->nPendingDat
3684f 61 3c 30 20 29 3b 0a 0a 20 20 66 6f 72 28 69 69  a<0 );..  for(ii
36850 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
36851 4b 20 26 26 20 69 69 3c 70 50 68 72 61 73 65 2d  K && ii<pPhrase-
36852 3e 6e 54 6f 6b 65 6e 3b 20 69 69 2b 2b 29 7b 0a  >nToken; ii++){.
36853 20 20 20 20 44 61 74 61 42 75 66 66 65 72 20 74      DataBuffer t
36854 6d 70 3b 0a 20 20 20 20 73 74 72 75 63 74 20 50  mp;.    struct P
36855 68 72 61 73 65 54 6f 6b 65 6e 20 2a 70 20 3d 20  hraseToken *p = 
36856 26 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e  &pPhrase->aToken
36857 5b 69 69 5d 3b 0a 20 20 20 20 72 63 20 3d 20 74  [ii];.    rc = t
36858 65 72 6d 53 65 6c 65 63 74 28 70 54 61 62 2c 20  ermSelect(pTab, 
36859 69 43 6f 6c 2c 20 70 2d 3e 7a 2c 20 70 2d 3e 6e  iCol, p->z, p->n
3685a 2c 20 70 2d 3e 69 73 50 72 65 66 69 78 2c 20 65  , p->isPrefix, e
3685b 54 79 70 65 2c 20 26 74 6d 70 29 3b 0a 20 20 20  Type, &tmp);.   
3685c 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
3685d 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  OK ){.      if( 
3685e 69 69 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ii==0 ){.       
3685f 20 2a 70 52 65 73 75 6c 74 20 3d 20 74 6d 70 3b   *pResult = tmp;
36860 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
36861 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72        DataBuffer
36862 20 72 65 73 20 3d 20 2a 70 52 65 73 75 6c 74 3b   res = *pResult;
36863 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66  .        dataBuf
36864 66 65 72 49 6e 69 74 28 70 52 65 73 75 6c 74 2c  ferInit(pResult,
36865 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   0);.        if(
36866 20 69 69 3d 3d 28 70 50 68 72 61 73 65 2d 3e 6e   ii==(pPhrase->n
36867 54 6f 6b 65 6e 2d 31 29 20 29 7b 0a 20 20 20 20  Token-1) ){.    
36868 20 20 20 20 20 20 65 54 79 70 65 20 3d 20 65 4c        eType = eL
36869 69 73 74 54 79 70 65 3b 0a 20 20 20 20 20 20 20  istType;.       
3686a 20 7d 0a 20 20 20 20 20 20 20 20 64 6f 63 4c 69   }.        docLi
3686b 73 74 50 68 72 61 73 65 4d 65 72 67 65 28 0a 20  stPhraseMerge(. 
3686c 20 20 20 20 20 20 20 20 20 72 65 73 2e 70 44 61           res.pDa
3686d 74 61 2c 20 72 65 73 2e 6e 44 61 74 61 2c 20 74  ta, res.nData, t
3686e 6d 70 2e 70 44 61 74 61 2c 20 74 6d 70 2e 6e 44  mp.pData, tmp.nD
3686f 61 74 61 2c 20 30 2c 20 30 2c 20 65 54 79 70 65  ata, 0, 0, eType
36870 2c 20 70 52 65 73 75 6c 74 0a 20 20 20 20 20 20  , pResult.      
36871 20 20 29 3b 0a 20 20 20 20 20 20 20 20 64 61 74    );.        dat
36872 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
36873 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20 64 61  res);.        da
36874 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
36875 26 74 6d 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20  &tmp);.      }. 
36876 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
36877 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
36878 45 76 61 6c 75 61 74 65 20 74 68 65 20 66 75 6c  Evaluate the ful
36879 6c 2d 74 65 78 74 20 65 78 70 72 65 73 73 69 6f  l-text expressio
3687a 6e 20 70 45 78 70 72 20 61 67 61 69 6e 73 74 20  n pExpr against 
3687b 66 74 73 33 20 74 61 62 6c 65 20 70 54 61 62 2e  fts3 table pTab.
3687c 20 57 72 69 74 65 0a 2a 2a 20 74 68 65 20 72 65   Write.** the re
3687d 73 75 6c 74 73 20 69 6e 74 6f 20 70 52 65 73 2e  sults into pRes.
3687e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65  .*/.static int e
3687f 76 61 6c 46 74 73 33 45 78 70 72 28 0a 20 20 66  valFts3Expr(.  f
36880 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 70 54  ulltext_vtab *pT
36881 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ab,           /*
36882 20 46 74 73 33 20 56 69 72 74 75 61 6c 20 74 61   Fts3 Virtual ta
36883 62 6c 65 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  ble object */.  
36884 46 74 73 33 45 78 70 72 20 2a 70 45 78 70 72 2c  Fts3Expr *pExpr,
36885 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
36886 2a 20 50 61 72 73 65 64 20 66 74 73 33 20 65 78  * Parsed fts3 ex
36887 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 44 61  pression */.  Da
36888 74 61 42 75 66 66 65 72 20 2a 70 52 65 73 20 20  taBuffer *pRes  
36889 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3688a 4f 55 54 3a 20 57 72 69 74 65 20 72 65 73 75 6c  OUT: Write resul
3688b 74 73 20 6f 66 20 74 68 65 20 65 78 70 72 65 73  ts of the expres
3688c 73 69 6f 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  sion here */.){.
3688d 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
3688e 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74  E_OK;..  /* Init
3688f 69 61 6c 69 7a 65 20 74 68 65 20 6f 75 74 70 75  ialize the outpu
36890 74 20 62 75 66 66 65 72 2e 20 49 66 20 74 68 69  t buffer. If thi
36891 73 20 69 73 20 61 6e 20 65 6d 70 74 79 20 71 75  s is an empty qu
36892 65 72 79 20 28 70 45 78 70 72 3d 3d 30 29 2c 20  ery (pExpr==0), 
36893 0a 20 20 2a 2a 20 74 68 69 73 20 69 73 20 61 6c  .  ** this is al
36894 6c 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20  l that needs to 
36895 62 65 20 64 6f 6e 65 2e 20 45 6d 70 74 79 20 71  be done. Empty q
36896 75 65 72 69 65 73 20 70 72 6f 64 75 63 65 20 65  ueries produce e
36897 6d 70 74 79 20 0a 20 20 2a 2a 20 72 65 73 75 6c  mpty .  ** resul
36898 74 20 73 65 74 73 2e 0a 20 20 2a 2f 0a 20 20 64  t sets..  */.  d
36899 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 70 52  ataBufferInit(pR
3689a 65 73 2c 20 30 29 3b 0a 0a 20 20 69 66 28 20 70  es, 0);..  if( p
3689b 45 78 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20  Expr ){.    if( 
3689c 70 45 78 70 72 2d 3e 65 54 79 70 65 3d 3d 46 54  pExpr->eType==FT
3689d 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 29 7b  SQUERY_PHRASE ){
3689e 0a 20 20 20 20 20 20 44 6f 63 4c 69 73 74 54 79  .      DocListTy
3689f 70 65 20 65 54 79 70 65 20 3d 20 44 4c 5f 44 4f  pe eType = DL_DO
368a0 43 49 44 53 3b 0a 20 20 20 20 20 20 69 66 28 20  CIDS;.      if( 
368a1 70 45 78 70 72 2d 3e 70 50 61 72 65 6e 74 20 26  pExpr->pParent &
368a2 26 20 70 45 78 70 72 2d 3e 70 50 61 72 65 6e 74  & pExpr->pParent
368a3 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52  ->eType==FTSQUER
368a4 59 5f 4e 45 41 52 20 29 7b 0a 20 20 20 20 20 20  Y_NEAR ){.      
368a5 20 20 65 54 79 70 65 20 3d 20 44 4c 5f 50 4f 53    eType = DL_POS
368a6 49 54 49 4f 4e 53 3b 0a 20 20 20 20 20 20 7d 0a  ITIONS;.      }.
368a7 20 20 20 20 20 20 72 63 20 3d 20 64 6f 63 4c 69        rc = docLi
368a8 73 74 4f 66 50 68 72 61 73 65 28 70 54 61 62 2c  stOfPhrase(pTab,
368a9 20 70 45 78 70 72 2d 3e 70 50 68 72 61 73 65 2c   pExpr->pPhrase,
368aa 20 65 54 79 70 65 2c 20 70 52 65 73 29 3b 0a 20   eType, pRes);. 
368ab 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
368ac 44 61 74 61 42 75 66 66 65 72 20 6c 68 73 3b 0a  DataBuffer lhs;.
368ad 20 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72        DataBuffer
368ae 20 72 68 73 3b 0a 0a 20 20 20 20 20 20 64 61 74   rhs;..      dat
368af 61 42 75 66 66 65 72 49 6e 69 74 28 26 72 68 73  aBufferInit(&rhs
368b0 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , 0);.      if( 
368b1 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d  SQLITE_OK==(rc =
368b2 20 65 76 61 6c 46 74 73 33 45 78 70 72 28 70 54   evalFts3Expr(pT
368b3 61 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  ab, pExpr->pLeft
368b4 2c 20 26 6c 68 73 29 29 20 0a 20 20 20 20 20 20  , &lhs)) .      
368b5 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28   && SQLITE_OK==(
368b6 72 63 20 3d 20 65 76 61 6c 46 74 73 33 45 78 70  rc = evalFts3Exp
368b7 72 28 70 54 61 62 2c 20 70 45 78 70 72 2d 3e 70  r(pTab, pExpr->p
368b8 52 69 67 68 74 2c 20 26 72 68 73 29 29 20 0a 20  Right, &rhs)) . 
368b9 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
368ba 73 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 65  switch( pExpr->e
368bb 54 79 70 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Type ){.        
368bc 20 20 63 61 73 65 20 46 54 53 51 55 45 52 59 5f    case FTSQUERY_
368bd 4e 45 41 52 3a 20 7b 0a 20 20 20 20 20 20 20 20  NEAR: {.        
368be 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a      int nToken;.
368bf 20 20 20 20 20 20 20 20 20 20 20 20 46 74 73 33              Fts3
368c0 45 78 70 72 20 2a 70 4c 65 66 74 3b 0a 20 20 20  Expr *pLeft;.   
368c1 20 20 20 20 20 20 20 20 20 44 6f 63 4c 69 73 74           DocList
368c2 54 79 70 65 20 65 54 79 70 65 20 3d 20 44 4c 5f  Type eType = DL_
368c3 44 4f 43 49 44 53 3b 0a 20 20 20 20 20 20 20 20  DOCIDS;.        
368c4 20 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70      if( pExpr->p
368c5 50 61 72 65 6e 74 20 26 26 20 70 45 78 70 72 2d  Parent && pExpr-
368c6 3e 70 50 61 72 65 6e 74 2d 3e 65 54 79 70 65 3d  >pParent->eType=
368c7 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52 20 29  =FTSQUERY_NEAR )
368c8 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
368c9 65 54 79 70 65 20 3d 20 44 4c 5f 50 4f 53 49 54  eType = DL_POSIT
368ca 49 4f 4e 53 3b 0a 20 20 20 20 20 20 20 20 20 20  IONS;.          
368cb 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
368cc 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70  pLeft = pExpr->p
368cd 4c 65 66 74 3b 0a 20 20 20 20 20 20 20 20 20 20  Left;.          
368ce 20 20 77 68 69 6c 65 28 20 70 4c 65 66 74 2d 3e    while( pLeft->
368cf 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f  eType==FTSQUERY_
368d0 4e 45 41 52 20 29 7b 20 0a 20 20 20 20 20 20 20  NEAR ){ .       
368d1 20 20 20 20 20 20 20 70 4c 65 66 74 3d 70 4c 65         pLeft=pLe
368d2 66 74 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20 20  ft->pRight;.    
368d3 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
368d4 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45        assert( pE
368d5 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 65 54 79  xpr->pRight->eTy
368d6 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52  pe==FTSQUERY_PHR
368d7 41 53 45 20 29 3b 0a 20 20 20 20 20 20 20 20 20  ASE );.         
368d8 20 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 74     assert( pLeft
368d9 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52  ->eType==FTSQUER
368da 59 5f 50 48 52 41 53 45 20 29 3b 0a 20 20 20 20  Y_PHRASE );.    
368db 20 20 20 20 20 20 20 20 6e 54 6f 6b 65 6e 20 3d          nToken =
368dc 20 70 4c 65 66 74 2d 3e 70 50 68 72 61 73 65 2d   pLeft->pPhrase-
368dd 3e 6e 54 6f 6b 65 6e 20 2b 20 70 45 78 70 72 2d  >nToken + pExpr-
368de 3e 70 52 69 67 68 74 2d 3e 70 50 68 72 61 73 65  >pRight->pPhrase
368df 2d 3e 6e 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20  ->nToken;.      
368e0 20 20 20 20 20 20 64 6f 63 4c 69 73 74 50 68 72        docListPhr
368e1 61 73 65 4d 65 72 67 65 28 6c 68 73 2e 70 44 61  aseMerge(lhs.pDa
368e2 74 61 2c 20 6c 68 73 2e 6e 44 61 74 61 2c 20 72  ta, lhs.nData, r
368e3 68 73 2e 70 44 61 74 61 2c 20 72 68 73 2e 6e 44  hs.pData, rhs.nD
368e4 61 74 61 2c 20 0a 20 20 20 20 20 20 20 20 20 20  ata, .          
368e5 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6e 4e 65        pExpr->nNe
368e6 61 72 2b 31 2c 20 6e 54 6f 6b 65 6e 2c 20 65 54  ar+1, nToken, eT
368e7 79 70 65 2c 20 70 52 65 73 0a 20 20 20 20 20 20  ype, pRes.      
368e8 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20        );.       
368e9 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
368ea 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
368eb 20 20 63 61 73 65 20 46 54 53 51 55 45 52 59 5f    case FTSQUERY_
368ec 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 20 20 20  NOT: {.         
368ed 20 20 20 64 6f 63 4c 69 73 74 45 78 63 65 70 74     docListExcept
368ee 4d 65 72 67 65 28 6c 68 73 2e 70 44 61 74 61 2c  Merge(lhs.pData,
368ef 20 6c 68 73 2e 6e 44 61 74 61 2c 20 72 68 73 2e   lhs.nData, rhs.
368f0 70 44 61 74 61 2c 20 72 68 73 2e 6e 44 61 74 61  pData, rhs.nData
368f1 2c 70 52 65 73 29 3b 0a 20 20 20 20 20 20 20 20  ,pRes);.        
368f2 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
368f3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
368f4 20 63 61 73 65 20 46 54 53 51 55 45 52 59 5f 41   case FTSQUERY_A
368f5 4e 44 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20  ND: {.          
368f6 20 20 64 6f 63 4c 69 73 74 41 6e 64 4d 65 72 67    docListAndMerg
368f7 65 28 6c 68 73 2e 70 44 61 74 61 2c 20 6c 68 73  e(lhs.pData, lhs
368f8 2e 6e 44 61 74 61 2c 20 72 68 73 2e 70 44 61 74  .nData, rhs.pDat
368f9 61 2c 20 72 68 73 2e 6e 44 61 74 61 2c 20 70 52  a, rhs.nData, pR
368fa 65 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  es);.           
368fb 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
368fc 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 63 61    }.          ca
368fd 73 65 20 46 54 53 51 55 45 52 59 5f 4f 52 3a 20  se FTSQUERY_OR: 
368fe 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 6f  {.            do
368ff 63 4c 69 73 74 4f 72 4d 65 72 67 65 28 6c 68 73  cListOrMerge(lhs
36900 2e 70 44 61 74 61 2c 20 6c 68 73 2e 6e 44 61 74  .pData, lhs.nDat
36901 61 2c 20 72 68 73 2e 70 44 61 74 61 2c 20 72 68  a, rhs.pData, rh
36902 73 2e 6e 44 61 74 61 2c 20 70 52 65 73 29 3b 0a  s.nData, pRes);.
36903 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
36904 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  k;.          }. 
36905 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
36906 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65  .      dataBuffe
36907 72 44 65 73 74 72 6f 79 28 26 6c 68 73 29 3b 0a  rDestroy(&lhs);.
36908 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72        dataBuffer
36909 44 65 73 74 72 6f 79 28 26 72 68 73 29 3b 0a 20  Destroy(&rhs);. 
3690a 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
3690b 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44  rn rc;.}../* TOD
3690c 4f 28 73 68 65 73 73 29 20 52 65 66 61 63 74 6f  O(shess) Refacto
3690d 72 20 74 68 65 20 63 6f 64 65 20 74 6f 20 72 65  r the code to re
3690e 6d 6f 76 65 20 74 68 69 73 20 66 6f 72 77 61 72  move this forwar
3690f 64 20 64 65 63 6c 2e 20 2a 2f 0a 73 74 61 74 69  d decl. */.stati
36910 63 20 69 6e 74 20 66 6c 75 73 68 50 65 6e 64 69  c int flushPendi
36911 6e 67 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74  ngTerms(fulltext
36912 5f 76 74 61 62 20 2a 76 29 3b 0a 0a 2f 2a 20 50  _vtab *v);../* P
36913 65 72 66 6f 72 6d 20 61 20 66 75 6c 6c 2d 74 65  erform a full-te
36914 78 74 20 71 75 65 72 79 20 75 73 69 6e 67 20 74  xt query using t
36915 68 65 20 73 65 61 72 63 68 20 65 78 70 72 65 73  he search expres
36916 73 69 6f 6e 20 69 6e 0a 2a 2a 20 7a 49 6e 70 75  sion in.** zInpu
36917 74 5b 30 2e 2e 6e 49 6e 70 75 74 2d 31 5d 2e 20  t[0..nInput-1]. 
36918 20 52 65 74 75 72 6e 20 61 20 6c 69 73 74 20 6f   Return a list o
36919 66 20 6d 61 74 63 68 69 6e 67 20 64 6f 63 75 6d  f matching docum
3691a 65 6e 74 73 0a 2a 2a 20 69 6e 20 70 52 65 73 75  ents.** in pResu
3691b 6c 74 2e 0a 2a 2a 0a 2a 2a 20 51 75 65 72 69 65  lt..**.** Querie
3691c 73 20 6d 75 73 74 20 6d 61 74 63 68 20 63 6f 6c  s must match col
3691d 75 6d 6e 20 69 43 6f 6c 75 6d 6e 2e 20 20 4f 72  umn iColumn.  Or
3691e 20 69 66 20 69 43 6f 6c 75 6d 6e 3e 3d 6e 43 6f   if iColumn>=nCo
3691f 6c 75 6d 6e 0a 2a 2a 20 74 68 65 79 20 61 72 65  lumn.** they are
36920 20 61 6c 6c 6f 77 65 64 20 74 6f 20 6d 61 74 63   allowed to matc
36921 68 20 61 67 61 69 6e 73 74 20 61 6e 79 20 63 6f  h against any co
36922 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lumn..*/.static 
36923 69 6e 74 20 66 75 6c 6c 74 65 78 74 51 75 65 72  int fulltextQuer
36924 79 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74  y(.  fulltext_vt
36925 61 62 20 2a 76 2c 20 20 20 20 20 20 2f 2a 20 54  ab *v,      /* T
36926 68 65 20 66 75 6c 6c 20 74 65 78 74 20 69 6e 64  he full text ind
36927 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c  ex */.  int iCol
36928 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 20 2f  umn,           /
36929 2a 20 4d 61 74 63 68 20 61 67 61 69 6e 73 74 20  * Match against 
3692a 74 68 69 73 20 63 6f 6c 75 6d 6e 20 62 79 20 64  this column by d
3692b 65 66 61 75 6c 74 20 2a 2f 0a 20 20 63 6f 6e 73  efault */.  cons
3692c 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74 2c 20  t char *zInput, 
3692d 20 20 20 2f 2a 20 54 68 65 20 71 75 65 72 79 20     /* The query 
3692e 73 74 72 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  string */.  int 
3692f 6e 49 6e 70 75 74 2c 20 20 20 20 20 20 20 20 20  nInput,         
36930 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
36931 62 79 74 65 73 20 69 6e 20 7a 49 6e 70 75 74 5b  bytes in zInput[
36932 5d 20 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65  ] */.  DataBuffe
36933 72 20 2a 70 52 65 73 75 6c 74 2c 20 20 20 2f 2a  r *pResult,   /*
36934 20 57 72 69 74 65 20 74 68 65 20 72 65 73 75 6c   Write the resul
36935 74 20 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a  t doclist here *
36936 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 2a 70  /.  Fts3Expr **p
36937 70 45 78 70 72 20 20 20 20 20 20 20 20 2f 2a 20  pExpr        /* 
36938 50 75 74 20 70 61 72 73 65 64 20 71 75 65 72 79  Put parsed query
36939 20 73 74 72 69 6e 67 20 68 65 72 65 20 2a 2f 0a   string here */.
3693a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  ){.  int rc;..  
3693b 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 49  /* TODO(shess) I
3693c 6e 73 74 65 61 64 20 6f 66 20 66 6c 75 73 68 69  nstead of flushi
3693d 6e 67 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 2c  ng pendingTerms,
3693e 20 77 65 20 63 6f 75 6c 64 20 71 75 65 72 79 20   we could query 
3693f 66 6f 72 0a 20 20 2a 2a 20 74 68 65 20 72 65 6c  for.  ** the rel
36940 65 76 61 6e 74 20 74 65 72 6d 20 61 6e 64 20 6d  evant term and m
36941 65 72 67 65 20 74 68 65 20 64 6f 63 6c 69 73 74  erge the doclist
36942 20 69 6e 74 6f 20 77 68 61 74 20 77 65 20 72 65   into what we re
36943 63 65 69 76 65 20 66 72 6f 6d 0a 20 20 2a 2a 20  ceive from.  ** 
36944 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 57  the database.  W
36945 61 69 74 20 61 6e 64 20 73 65 65 20 69 66 20 74  ait and see if t
36946 68 69 73 20 69 73 20 61 20 63 6f 6d 6d 6f 6e 20  his is a common 
36947 69 73 73 75 65 2c 20 66 69 72 73 74 2e 0a 20 20  issue, first..  
36948 2a 2a 0a 20 20 2a 2a 20 41 20 67 6f 6f 64 20 72  **.  ** A good r
36949 65 61 73 6f 6e 20 6e 6f 74 20 74 6f 20 66 6c 75  eason not to flu
3694a 73 68 20 69 73 20 74 6f 20 6e 6f 74 20 67 65 6e  sh is to not gen
3694b 65 72 61 74 65 20 75 70 64 61 74 65 2d 72 65 6c  erate update-rel
3694c 61 74 65 64 0a 20 20 2a 2a 20 65 72 72 6f 72 20  ated.  ** error 
3694d 63 6f 64 65 73 20 66 72 6f 6d 20 68 65 72 65 2e  codes from here.
3694e 0a 20 20 2a 2f 0a 0a 20 20 2f 2a 20 46 6c 75 73  .  */..  /* Flus
3694f 68 20 61 6e 79 20 62 75 66 66 65 72 65 64 20 75  h any buffered u
36950 70 64 61 74 65 73 20 62 65 66 6f 72 65 20 65 78  pdates before ex
36951 65 63 75 74 69 6e 67 20 74 68 65 20 71 75 65 72  ecuting the quer
36952 79 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 66 6c 75  y. */.  rc = flu
36953 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76  shPendingTerms(v
36954 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
36955 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65  ITE_OK ){.    re
36956 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
36957 2f 2a 20 50 61 72 73 65 20 74 68 65 20 71 75 65  /* Parse the que
36958 72 79 20 70 61 73 73 65 64 20 74 6f 20 74 68 65  ry passed to the
36959 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72 2e   MATCH operator.
3695a 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   */.  rc = sqlit
3695b 65 33 46 74 73 33 45 78 70 72 50 61 72 73 65 28  e3Fts3ExprParse(
3695c 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 0a  v->pTokenizer, .
3695d 20 20 20 20 20 20 76 2d 3e 61 7a 43 6f 6c 75 6d        v->azColum
3695e 6e 2c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 69  n, v->nColumn, i
3695f 43 6f 6c 75 6d 6e 2c 20 7a 49 6e 70 75 74 2c 20  Column, zInput, 
36960 6e 49 6e 70 75 74 2c 20 70 70 45 78 70 72 0a 20  nInput, ppExpr. 
36961 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51   );.  if( rc!=SQ
36962 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61  LITE_OK ){.    a
36963 73 73 65 72 74 28 20 30 3d 3d 28 2a 70 70 45 78  ssert( 0==(*ppEx
36964 70 72 29 20 29 3b 0a 20 20 20 20 72 65 74 75 72  pr) );.    retur
36965 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  n rc;.  }..  ret
36966 75 72 6e 20 65 76 61 6c 46 74 73 33 45 78 70 72  urn evalFts3Expr
36967 28 76 2c 20 2a 70 70 45 78 70 72 2c 20 70 52 65  (v, *ppExpr, pRe
36968 73 75 6c 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  sult);.}../*.** 
36969 54 68 69 73 20 69 73 20 74 68 65 20 78 46 69 6c  This is the xFil
3696a 74 65 72 20 69 6e 74 65 72 66 61 63 65 20 66 6f  ter interface fo
3696b 72 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  r the virtual ta
3696c 62 6c 65 2e 20 20 53 65 65 0a 2a 2a 20 74 68 65  ble.  See.** the
3696d 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 78   virtual table x
3696e 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20 64 6f  Filter method do
3696f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20  cumentation for 
36970 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e  additional.** in
36971 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  formation..**.**
36972 20 49 66 20 69 64 78 4e 75 6d 3d 3d 51 55 45 52   If idxNum==QUER
36973 59 5f 47 45 4e 45 52 49 43 20 74 68 65 6e 20 64  Y_GENERIC then d
36974 6f 20 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73  o a full table s
36975 63 61 6e 20 61 67 61 69 6e 73 74 0a 2a 2a 20 74  can against.** t
36976 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62  he %_content tab
36977 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 64 78  le..**.** If idx
36978 4e 75 6d 3d 3d 51 55 45 52 59 5f 44 4f 43 49 44  Num==QUERY_DOCID
36979 20 74 68 65 6e 20 64 6f 20 61 20 64 6f 63 69 64   then do a docid
3697a 20 6c 6f 6f 6b 75 70 20 66 6f 72 20 61 20 73 69   lookup for a si
3697b 6e 67 6c 65 20 65 6e 74 72 79 0a 2a 2a 20 69 6e  ngle entry.** in
3697c 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74   the %_content t
3697d 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69  able..**.** If i
3697e 64 78 4e 75 6d 3e 3d 51 55 45 52 59 5f 46 55 4c  dxNum>=QUERY_FUL
3697f 4c 54 45 58 54 20 74 68 65 6e 20 75 73 65 20 74  LTEXT then use t
36980 68 65 20 66 75 6c 6c 20 74 65 78 74 20 69 6e 64  he full text ind
36981 65 78 2e 20 20 54 68 65 0a 2a 2a 20 63 6f 6c 75  ex.  The.** colu
36982 6d 6e 20 6f 6e 20 74 68 65 20 6c 65 66 74 2d 68  mn on the left-h
36983 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68 65 20  and side of the 
36984 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72 20 69  MATCH operator i
36985 73 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e 75 6d 62  s column.** numb
36986 65 72 20 69 64 78 4e 75 6d 2d 51 55 45 52 59 5f  er idxNum-QUERY_
36987 46 55 4c 4c 54 45 58 54 2c 20 30 20 69 6e 64 65  FULLTEXT, 0 inde
36988 78 65 64 2e 20 20 61 72 67 76 5b 30 5d 20 69 73  xed.  argv[0] is
36989 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 0a   the right-hand.
3698a 2a 2a 20 73 69 64 65 20 6f 66 20 74 68 65 20 4d  ** side of the M
3698b 41 54 43 48 20 6f 70 65 72 61 74 6f 72 2e 0a 2a  ATCH operator..*
3698c 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
3698d 20 55 70 67 72 61 64 65 20 74 68 65 20 63 75 72   Upgrade the cur
3698e 73 6f 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69  sor initializati
3698f 6f 6e 20 61 6e 64 20 64 65 73 74 72 75 63 74 69  on and destructi
36990 6f 6e 20 74 6f 0a 2a 2a 20 61 63 63 6f 75 6e 74  on to.** account
36991 20 66 6f 72 20 66 75 6c 6c 74 65 78 74 46 69 6c   for fulltextFil
36992 74 65 72 28 29 20 62 65 69 6e 67 20 63 61 6c 6c  ter() being call
36993 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  ed multiple time
36994 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 61 6d 65  s on the.** same
36995 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 63 75   cursor.  The cu
36996 72 72 65 6e 74 20 73 6f 6c 75 74 69 6f 6e 20 69  rrent solution i
36997 73 20 76 65 72 79 20 66 72 61 67 69 6c 65 2e 20  s very fragile. 
36998 20 41 70 70 6c 79 20 66 69 78 20 74 6f 0a 2a 2a   Apply fix to.**
36999 20 66 74 73 33 20 61 73 20 61 70 70 72 6f 70 72   fts3 as appropr
3699a 69 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  iate..*/.static 
3699b 69 6e 74 20 66 75 6c 6c 74 65 78 74 46 69 6c 74  int fulltextFilt
3699c 65 72 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  er(.  sqlite3_vt
3699d 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  ab_cursor *pCurs
3699e 6f 72 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 63  or,     /* The c
3699f 75 72 73 6f 72 20 75 73 65 64 20 66 6f 72 20 74  ursor used for t
369a0 68 69 73 20 71 75 65 72 79 20 2a 2f 0a 20 20 69  his query */.  i
369a1 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e 73 74  nt idxNum, const
369a2 20 63 68 61 72 20 2a 69 64 78 53 74 72 2c 20 20   char *idxStr,  
369a3 20 2f 2a 20 57 68 69 63 68 20 69 6e 64 65 78 69   /* Which indexi
369a4 6e 67 20 73 63 68 65 6d 65 20 74 6f 20 75 73 65  ng scheme to use
369a5 20 2a 2f 0a 20 20 69 6e 74 20 61 72 67 63 2c 20   */.  int argc, 
369a6 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
369a7 61 72 67 76 20 20 20 20 2f 2a 20 41 72 67 75 6d  argv    /* Argum
369a8 65 6e 74 73 20 66 6f 72 20 74 68 65 20 69 6e 64  ents for the ind
369a9 65 78 69 6e 67 20 73 63 68 65 6d 65 20 2a 2f 0a  exing scheme */.
369aa 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75  ){.  fulltext_cu
369ab 72 73 6f 72 20 2a 63 20 3d 20 28 66 75 6c 6c 74  rsor *c = (fullt
369ac 65 78 74 5f 63 75 72 73 6f 72 20 2a 29 20 70 43  ext_cursor *) pC
369ad 75 72 73 6f 72 3b 0a 20 20 66 75 6c 6c 74 65 78  ursor;.  fulltex
369ae 74 5f 76 74 61 62 20 2a 76 20 3d 20 63 75 72 73  t_vtab *v = curs
369af 6f 72 5f 76 74 61 62 28 63 29 3b 0a 20 20 69 6e  or_vtab(c);.  in
369b0 74 20 72 63 3b 0a 0a 20 20 46 54 53 54 52 41 43  t rc;..  FTSTRAC
369b1 45 28 28 22 46 54 53 33 20 46 69 6c 74 65 72 20  E(("FTS3 Filter 
369b2 25 70 5c 6e 22 2c 70 43 75 72 73 6f 72 29 29 3b  %p\n",pCursor));
369b3 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 63 75  ..  /* If the cu
369b4 72 73 6f 72 20 68 61 73 20 61 20 73 74 61 74 65  rsor has a state
369b5 6d 65 6e 74 20 74 68 61 74 20 77 61 73 20 6e 6f  ment that was no
369b6 74 20 70 72 65 70 61 72 65 64 20 61 63 63 6f 72  t prepared accor
369b7 64 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 69 64 78  ding to.  ** idx
369b8 4e 75 6d 2c 20 63 6c 65 61 72 20 69 74 2e 20 20  Num, clear it.  
369b9 49 20 62 65 6c 69 65 76 65 20 61 6c 6c 20 63 61  I believe all ca
369ba 6c 6c 73 20 74 6f 20 66 75 6c 6c 74 65 78 74 46  lls to fulltextF
369bb 69 6c 74 65 72 20 77 69 74 68 20 61 0a 20 20 2a  ilter with a.  *
369bc 2a 20 67 69 76 65 6e 20 63 75 72 73 6f 72 20 77  * given cursor w
369bd 69 6c 6c 20 68 61 76 65 20 74 68 65 20 73 61 6d  ill have the sam
369be 65 20 69 64 78 4e 75 6d 20 2c 20 62 75 74 20 69  e idxNum , but i
369bf 6e 20 74 68 69 73 20 63 61 73 65 20 69 74 27 73  n this case it's
369c0 0a 20 20 2a 2a 20 65 61 73 79 20 74 6f 20 62 65  .  ** easy to be
369c1 20 73 61 66 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   safe..  */.  if
369c2 28 20 63 2d 3e 70 53 74 6d 74 20 26 26 20 63 2d  ( c->pStmt && c-
369c3 3e 69 43 75 72 73 6f 72 54 79 70 65 21 3d 69 64  >iCursorType!=id
369c4 78 4e 75 6d 20 29 7b 0a 20 20 20 20 73 71 6c 69  xNum ){.    sqli
369c5 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 63 2d 3e  te3_finalize(c->
369c6 70 53 74 6d 74 29 3b 0a 20 20 20 20 63 2d 3e 70  pStmt);.    c->p
369c7 53 74 6d 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 7d  Stmt = NULL;.  }
369c8 0a 0a 20 20 2f 2a 20 47 65 74 20 61 20 66 72 65  ..  /* Get a fre
369c9 73 68 20 73 74 61 74 65 6d 65 6e 74 20 61 70 70  sh statement app
369ca 72 6f 70 72 69 61 74 65 20 74 6f 20 69 64 78 4e  ropriate to idxN
369cb 75 6d 2e 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f  um. */.  /* TODO
369cc 28 73 68 65 73 73 29 3a 20 41 64 64 20 61 20 70  (shess): Add a p
369cd 72 65 70 61 72 65 64 2d 73 74 61 74 65 6d 65 6e  repared-statemen
369ce 74 20 63 61 63 68 65 20 69 6e 20 74 68 65 20 76  t cache in the v
369cf 74 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 2a  t structure..  *
369d0 2a 20 54 68 65 20 63 61 63 68 65 20 6d 75 73 74  * The cache must
369d1 20 68 61 6e 64 6c 65 20 6d 75 6c 74 69 70 6c 65   handle multiple
369d2 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 2e 20 20   open cursors.  
369d3 45 61 73 69 65 72 20 74 6f 20 63 61 63 68 65 20  Easier to cache 
369d4 74 68 65 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65  the.  ** stateme
369d5 6e 74 20 76 61 72 69 61 6e 74 73 20 61 74 20 74  nt variants at t
369d6 68 65 20 76 74 20 74 6f 20 72 65 64 75 63 65 20  he vt to reduce 
369d7 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c 6f 63 2f 66  malloc/realloc/f
369d8 72 65 65 20 68 65 72 65 2e 0a 20 20 2a 2a 20 4f  ree here..  ** O
369d9 72 20 77 65 20 63 6f 75 6c 64 20 68 61 76 65 20  r we could have 
369da 61 20 53 74 72 69 6e 67 42 75 66 66 65 72 20 76  a StringBuffer v
369db 61 72 69 61 6e 74 20 77 68 69 63 68 20 61 6c 6c  ariant which all
369dc 6f 77 65 64 20 73 74 61 63 6b 0a 20 20 2a 2a 20  owed stack.  ** 
369dd 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 66 6f 72  construction for
369de 20 73 6d 61 6c 6c 20 76 61 6c 75 65 73 2e 0a 20   small values.. 
369df 20 2a 2f 0a 20 20 69 66 28 20 21 63 2d 3e 70 53   */.  if( !c->pS
369e0 74 6d 74 20 29 7b 0a 20 20 20 20 53 74 72 69 6e  tmt ){.    Strin
369e1 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20 20 20  gBuffer sb;.    
369e2 69 6e 69 74 53 74 72 69 6e 67 42 75 66 66 65 72  initStringBuffer
369e3 28 26 73 62 29 3b 0a 20 20 20 20 61 70 70 65 6e  (&sb);.    appen
369e4 64 28 26 73 62 2c 20 22 53 45 4c 45 43 54 20 64  d(&sb, "SELECT d
369e5 6f 63 69 64 2c 20 22 29 3b 0a 20 20 20 20 61 70  ocid, ");.    ap
369e6 70 65 6e 64 4c 69 73 74 28 26 73 62 2c 20 76 2d  pendList(&sb, v-
369e7 3e 6e 43 6f 6c 75 6d 6e 2c 20 76 2d 3e 61 7a 43  >nColumn, v->azC
369e8 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20  ontentColumn);. 
369e9 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22     append(&sb, "
369ea 20 46 52 4f 4d 20 25 5f 63 6f 6e 74 65 6e 74 22   FROM %_content"
369eb 29 3b 0a 20 20 20 20 69 66 28 20 69 64 78 4e 75  );.    if( idxNu
369ec 6d 21 3d 51 55 45 52 59 5f 47 45 4e 45 52 49 43  m!=QUERY_GENERIC
369ed 20 29 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22   ) append(&sb, "
369ee 20 57 48 45 52 45 20 64 6f 63 69 64 20 3d 20 3f   WHERE docid = ?
369ef 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  ");.    rc = sql
369f0 5f 70 72 65 70 61 72 65 28 76 2d 3e 64 62 2c 20  _prepare(v->db, 
369f1 76 2d 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61 6d 65  v->zDb, v->zName
369f2 2c 20 26 63 2d 3e 70 53 74 6d 74 2c 0a 20 20 20  , &c->pStmt,.   
369f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
369f4 20 20 73 74 72 69 6e 67 42 75 66 66 65 72 44 61    stringBufferDa
369f5 74 61 28 26 73 62 29 29 3b 0a 20 20 20 20 73 74  ta(&sb));.    st
369f6 72 69 6e 67 42 75 66 66 65 72 44 65 73 74 72 6f  ringBufferDestro
369f7 79 28 26 73 62 29 3b 0a 20 20 20 20 69 66 28 20  y(&sb);.    if( 
369f8 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
369f9 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 63  return rc;.    c
369fa 2d 3e 69 43 75 72 73 6f 72 54 79 70 65 20 3d 20  ->iCursorType = 
369fb 69 64 78 4e 75 6d 3b 0a 20 20 7d 65 6c 73 65 7b  idxNum;.  }else{
369fc 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
369fd 65 74 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20 20  et(c->pStmt);.  
369fe 20 20 61 73 73 65 72 74 28 20 63 2d 3e 69 43 75    assert( c->iCu
369ff 72 73 6f 72 54 79 70 65 3d 3d 69 64 78 4e 75 6d  rsorType==idxNum
36a00 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63   );.  }..  switc
36a01 68 28 20 69 64 78 4e 75 6d 20 29 7b 0a 20 20 20  h( idxNum ){.   
36a02 20 63 61 73 65 20 51 55 45 52 59 5f 47 45 4e 45   case QUERY_GENE
36a03 52 49 43 3a 0a 20 20 20 20 20 20 62 72 65 61 6b  RIC:.      break
36a04 3b 0a 0a 20 20 20 20 63 61 73 65 20 51 55 45 52  ;..    case QUER
36a05 59 5f 44 4f 43 49 44 3a 0a 20 20 20 20 20 20 72  Y_DOCID:.      r
36a06 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  c = sqlite3_bind
36a07 5f 69 6e 74 36 34 28 63 2d 3e 70 53 74 6d 74 2c  _int64(c->pStmt,
36a08 20 31 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   1, sqlite3_valu
36a09 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30 5d 29  e_int64(argv[0])
36a0a 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
36a0b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
36a0c 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 62 72  urn rc;.      br
36a0d 65 61 6b 3b 0a 0a 20 20 20 20 64 65 66 61 75 6c  eak;..    defaul
36a0e 74 3a 20 20 20 2f 2a 20 66 75 6c 6c 2d 74 65 78  t:   /* full-tex
36a0f 74 20 73 65 61 72 63 68 20 2a 2f 0a 20 20 20 20  t search */.    
36a10 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c  {.      int iCol
36a11 20 3d 20 69 64 78 4e 75 6d 2d 51 55 45 52 59 5f   = idxNum-QUERY_
36a12 46 55 4c 4c 54 45 58 54 3b 0a 20 20 20 20 20 20  FULLTEXT;.      
36a13 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 51 75 65  const char *zQue
36a14 72 79 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  ry = (const char
36a15 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
36a16 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a  _text(argv[0]);.
36a17 20 20 20 20 20 20 61 73 73 65 72 74 28 20 69 64        assert( id
36a18 78 4e 75 6d 3c 3d 51 55 45 52 59 5f 46 55 4c 4c  xNum<=QUERY_FULL
36a19 54 45 58 54 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 29  TEXT+v->nColumn)
36a1a 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
36a1b 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 20 20 20  argc==1 );.     
36a1c 20 69 66 28 20 63 2d 3e 72 65 73 75 6c 74 2e 6e   if( c->result.n
36a1d 44 61 74 61 21 3d 30 20 29 7b 0a 20 20 20 20 20  Data!=0 ){.     
36a1e 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73 65 20     /* This case 
36a1f 68 61 70 70 65 6e 73 20 69 66 20 74 68 65 20 73  happens if the s
36a20 61 6d 65 20 63 75 72 73 6f 72 20 69 73 20 75 73  ame cursor is us
36a21 65 64 20 72 65 70 65 61 74 65 64 6c 79 2e 20 2a  ed repeatedly. *
36a22 2f 0a 20 20 20 20 20 20 20 20 64 6c 72 44 65 73  /.        dlrDes
36a23 74 72 6f 79 28 26 63 2d 3e 72 65 61 64 65 72 29  troy(&c->reader)
36a24 3b 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75  ;.        dataBu
36a25 66 66 65 72 52 65 73 65 74 28 26 63 2d 3e 72 65  fferReset(&c->re
36a26 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c  sult);.      }el
36a27 73 65 7b 0a 20 20 20 20 20 20 20 20 64 61 74 61  se{.        data
36a28 42 75 66 66 65 72 49 6e 69 74 28 26 63 2d 3e 72  BufferInit(&c->r
36a29 65 73 75 6c 74 2c 20 30 29 3b 0a 20 20 20 20 20  esult, 0);.     
36a2a 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20 66 75   }.      rc = fu
36a2b 6c 6c 74 65 78 74 51 75 65 72 79 28 76 2c 20 69  lltextQuery(v, i
36a2c 43 6f 6c 2c 20 7a 51 75 65 72 79 2c 20 2d 31 2c  Col, zQuery, -1,
36a2d 20 26 63 2d 3e 72 65 73 75 6c 74 2c 20 26 63 2d   &c->result, &c-
36a2e 3e 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 69  >pExpr);.      i
36a2f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
36a30 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
36a31 20 20 20 20 69 66 28 20 63 2d 3e 72 65 73 75 6c      if( c->resul
36a32 74 2e 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20 20  t.nData!=0 ){.  
36a33 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28 26 63        dlrInit(&c
36a34 2d 3e 72 65 61 64 65 72 2c 20 44 4c 5f 44 4f 43  ->reader, DL_DOC
36a35 49 44 53 2c 20 63 2d 3e 72 65 73 75 6c 74 2e 70  IDS, c->result.p
36a36 44 61 74 61 2c 20 63 2d 3e 72 65 73 75 6c 74 2e  Data, c->result.
36a37 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20 7d 0a  nData);.      }.
36a38 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
36a39 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
36a3a 20 66 75 6c 6c 74 65 78 74 4e 65 78 74 28 70 43   fulltextNext(pC
36a3b 75 72 73 6f 72 29 3b 0a 7d 0a 0a 2f 2a 20 54 68  ursor);.}../* Th
36a3c 69 73 20 69 73 20 74 68 65 20 78 45 6f 66 20 6d  is is the xEof m
36a3d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 76 69 72  ethod of the vir
36a3e 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65  tual table.  The
36a3f 20 53 51 4c 69 74 65 20 63 6f 72 65 0a 2a 2a 20   SQLite core.** 
36a40 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69  calls this routi
36a41 6e 65 20 74 6f 20 66 69 6e 64 20 6f 75 74 20 69  ne to find out i
36a42 66 20 69 74 20 68 61 73 20 72 65 61 63 68 65 64  f it has reached
36a43 20 74 68 65 20 65 6e 64 20 6f 66 0a 2a 2a 20 61   the end of.** a
36a44 20 71 75 65 72 79 27 73 20 72 65 73 75 6c 74 73   query's results
36a45 20 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   set..*/.static 
36a46 69 6e 74 20 66 75 6c 6c 74 65 78 74 45 6f 66 28  int fulltextEof(
36a47 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
36a48 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20  sor *pCursor){. 
36a49 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72   fulltext_cursor
36a4a 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f   *c = (fulltext_
36a4b 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f  cursor *) pCurso
36a4c 72 3b 0a 20 20 72 65 74 75 72 6e 20 63 2d 3e 65  r;.  return c->e
36a4d 6f 66 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 69  of;.}../* This i
36a4e 73 20 74 68 65 20 78 43 6f 6c 75 6d 6e 20 6d 65  s the xColumn me
36a4f 74 68 6f 64 20 6f 66 20 74 68 65 20 76 69 72 74  thod of the virt
36a50 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20  ual table.  The 
36a51 53 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65 20 63  SQLite.** core c
36a52 61 6c 6c 73 20 74 68 69 73 20 6d 65 74 68 6f 64  alls this method
36a53 20 64 75 72 69 6e 67 20 61 20 71 75 65 72 79 20   during a query 
36a54 77 68 65 6e 20 69 74 20 6e 65 65 64 73 20 74 68  when it needs th
36a55 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 61 20  e value.** of a 
36a56 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 74 68 65 20  column from the 
36a57 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20  virtual table.  
36a58 54 68 69 73 20 6d 65 74 68 6f 64 20 6e 65 65 64  This method need
36a59 73 20 74 6f 20 75 73 65 0a 2a 2a 20 6f 6e 65 20  s to use.** one 
36a5a 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72  of the sqlite3_r
36a5b 65 73 75 6c 74 5f 2a 28 29 20 72 6f 75 74 69 6e  esult_*() routin
36a5c 65 73 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  es to store the 
36a5d 72 65 71 75 65 73 74 65 64 0a 2a 2a 20 76 61 6c  requested.** val
36a5e 75 65 20 62 61 63 6b 20 69 6e 20 74 68 65 20 70  ue back in the p
36a5f 43 6f 6e 74 65 78 74 2e 0a 2a 2f 0a 73 74 61 74  Context..*/.stat
36a60 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 43  ic int fulltextC
36a61 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33 5f 76 74  olumn(sqlite3_vt
36a62 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  ab_cursor *pCurs
36a63 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  or,.            
36a64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
36a65 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
36a66 43 6f 6e 74 65 78 74 2c 20 69 6e 74 20 69 64 78  Context, int idx
36a67 43 6f 6c 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74  Col){.  fulltext
36a68 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 66 75  _cursor *c = (fu
36a69 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 29  lltext_cursor *)
36a6a 20 70 43 75 72 73 6f 72 3b 0a 20 20 66 75 6c 6c   pCursor;.  full
36a6b 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20 63  text_vtab *v = c
36a6c 75 72 73 6f 72 5f 76 74 61 62 28 63 29 3b 0a 0a  ursor_vtab(c);..
36a6d 20 20 69 66 28 20 69 64 78 43 6f 6c 3c 76 2d 3e    if( idxCol<v->
36a6e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 73  nColumn ){.    s
36a6f 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
36a70 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  al = sqlite3_col
36a71 75 6d 6e 5f 76 61 6c 75 65 28 63 2d 3e 70 53 74  umn_value(c->pSt
36a72 6d 74 2c 20 69 64 78 43 6f 6c 2b 31 29 3b 0a 20  mt, idxCol+1);. 
36a73 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
36a74 74 5f 76 61 6c 75 65 28 70 43 6f 6e 74 65 78 74  t_value(pContext
36a75 2c 20 70 56 61 6c 29 3b 0a 20 20 7d 65 6c 73 65  , pVal);.  }else
36a76 20 69 66 28 20 69 64 78 43 6f 6c 3d 3d 76 2d 3e   if( idxCol==v->
36a77 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 2f  nColumn ){.    /
36a78 2a 20 54 68 65 20 65 78 74 72 61 20 63 6f 6c 75  * The extra colu
36a79 6d 6e 20 77 68 6f 73 65 20 6e 61 6d 65 20 69 73  mn whose name is
36a7a 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
36a7b 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 20 52   table..    ** R
36a7c 65 74 75 72 6e 20 61 20 62 6c 6f 62 20 77 68 69  eturn a blob whi
36a7d 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ch is a pointer 
36a7e 74 6f 20 74 68 65 20 63 75 72 73 6f 72 0a 20 20  to the cursor.  
36a7f 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33    */.    sqlite3
36a80 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 70 43 6f  _result_blob(pCo
36a81 6e 74 65 78 74 2c 20 26 63 2c 20 73 69 7a 65 6f  ntext, &c, sizeo
36a82 66 28 63 29 2c 20 53 51 4c 49 54 45 5f 54 52 41  f(c), SQLITE_TRA
36a83 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c 73 65  NSIENT);.  }else
36a84 20 69 66 28 20 69 64 78 43 6f 6c 3d 3d 76 2d 3e   if( idxCol==v->
36a85 6e 43 6f 6c 75 6d 6e 2b 31 20 29 7b 0a 20 20 20  nColumn+1 ){.   
36a86 20 2f 2a 20 54 68 65 20 64 6f 63 69 64 20 63 6f   /* The docid co
36a87 6c 75 6d 6e 2c 20 77 68 69 63 68 20 69 73 20 61  lumn, which is a
36a88 6e 20 61 6c 69 61 73 20 66 6f 72 20 72 6f 77 69  n alias for rowi
36a89 64 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  d. */.    sqlite
36a8a 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d 20  3_value *pVal = 
36a8b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76  sqlite3_column_v
36a8c 61 6c 75 65 28 63 2d 3e 70 53 74 6d 74 2c 20 30  alue(c->pStmt, 0
36a8d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
36a8e 65 73 75 6c 74 5f 76 61 6c 75 65 28 70 43 6f 6e  esult_value(pCon
36a8f 74 65 78 74 2c 20 70 56 61 6c 29 3b 0a 20 20 7d  text, pVal);.  }
36a90 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
36a91 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20  _OK;.}../* This 
36a92 69 73 20 74 68 65 20 78 52 6f 77 69 64 20 6d 65  is the xRowid me
36a93 74 68 6f 64 2e 20 20 54 68 65 20 53 51 4c 69 74  thod.  The SQLit
36a94 65 20 63 6f 72 65 20 63 61 6c 6c 73 20 74 68 69  e core calls thi
36a95 73 20 72 6f 75 74 69 6e 65 20 74 6f 0a 2a 2a 20  s routine to.** 
36a96 72 65 74 72 69 65 76 65 20 74 68 65 20 72 6f 77  retrieve the row
36a97 69 64 20 66 6f 72 20 74 68 65 20 63 75 72 72 65  id for the curre
36a98 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 72 65  nt row of the re
36a99 73 75 6c 74 20 73 65 74 2e 20 20 66 74 73 33 0a  sult set.  fts3.
36a9a 2a 2a 20 65 78 70 6f 73 65 73 20 25 5f 63 6f 6e  ** exposes %_con
36a9b 74 65 6e 74 2e 64 6f 63 69 64 20 61 73 20 74 68  tent.docid as th
36a9c 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20  e rowid for the 
36a9d 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20  virtual table.  
36a9e 54 68 65 0a 2a 2a 20 72 6f 77 69 64 20 73 68 6f  The.** rowid sho
36a9f 75 6c 64 20 62 65 20 77 72 69 74 74 65 6e 20 74  uld be written t
36aa0 6f 20 2a 70 52 6f 77 69 64 2e 0a 2a 2f 0a 73 74  o *pRowid..*/.st
36aa1 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78  atic int fulltex
36aa2 74 52 6f 77 69 64 28 73 71 6c 69 74 65 33 5f 76  tRowid(sqlite3_v
36aa3 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  tab_cursor *pCur
36aa4 73 6f 72 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36  sor, sqlite_int6
36aa5 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20 20 66 75  4 *pRowid){.  fu
36aa6 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 63  lltext_cursor *c
36aa7 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63 75 72   = (fulltext_cur
36aa8 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a  sor *) pCursor;.
36aa9 0a 20 20 2a 70 52 6f 77 69 64 20 3d 20 73 71 6c  .  *pRowid = sql
36aaa 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36  ite3_column_int6
36aab 34 28 63 2d 3e 70 53 74 6d 74 2c 20 30 29 3b 0a  4(c->pStmt, 0);.
36aac 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
36aad 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20 61 6c  OK;.}../* Add al
36aae 6c 20 74 65 72 6d 73 20 69 6e 20 5b 7a 54 65 78  l terms in [zTex
36aaf 74 5d 20 74 6f 20 70 65 6e 64 69 6e 67 54 65 72  t] to pendingTer
36ab0 6d 73 20 74 61 62 6c 65 2e 20 20 49 66 20 5b 69  ms table.  If [i
36ab1 43 6f 6c 75 6d 6e 5d 20 3e 20 30 2c 0a 2a 2a 20  Column] > 0,.** 
36ab2 77 65 20 61 6c 73 6f 20 73 74 6f 72 65 20 70 6f  we also store po
36ab3 73 69 74 69 6f 6e 73 20 61 6e 64 20 6f 66 66 73  sitions and offs
36ab4 65 74 73 20 69 6e 20 74 68 65 20 68 61 73 68 20  ets in the hash 
36ab5 74 61 62 6c 65 20 75 73 69 6e 67 20 74 68 61 74  table using that
36ab6 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65  .** column numbe
36ab7 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
36ab8 20 62 75 69 6c 64 54 65 72 6d 73 28 66 75 6c 6c   buildTerms(full
36ab9 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71  text_vtab *v, sq
36aba 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69  lite_int64 iDoci
36abb 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
36abc 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
36abd 68 61 72 20 2a 7a 54 65 78 74 2c 20 69 6e 74 20  har *zText, int 
36abe 69 43 6f 6c 75 6d 6e 29 7b 0a 20 20 73 71 6c 69  iColumn){.  sqli
36abf 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
36ac0 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 76 2d 3e 70  Tokenizer = v->p
36ac1 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 71 6c  Tokenizer;.  sql
36ac2 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
36ac3 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a  ursor *pCursor;.
36ac4 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
36ac5 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 6e 54 6f 6b  oken;.  int nTok
36ac6 65 6e 42 79 74 65 73 3b 0a 20 20 69 6e 74 20 69  enBytes;.  int i
36ac7 53 74 61 72 74 4f 66 66 73 65 74 2c 20 69 45 6e  StartOffset, iEn
36ac8 64 4f 66 66 73 65 74 2c 20 69 50 6f 73 69 74 69  dOffset, iPositi
36ac9 6f 6e 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  on;.  int rc;.. 
36aca 20 72 63 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72   rc = pTokenizer
36acb 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e  ->pModule->xOpen
36acc 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a 54 65  (pTokenizer, zTe
36acd 78 74 2c 20 2d 31 2c 20 26 70 43 75 72 73 6f 72  xt, -1, &pCursor
36ace 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
36acf 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
36ad0 72 63 3b 0a 0a 20 20 70 43 75 72 73 6f 72 2d 3e  rc;..  pCursor->
36ad1 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f  pTokenizer = pTo
36ad2 6b 65 6e 69 7a 65 72 3b 0a 20 20 77 68 69 6c 65  kenizer;.  while
36ad3 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63  ( SQLITE_OK==(rc
36ad4 3d 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f  =pTokenizer->pMo
36ad5 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75 72  dule->xNext(pCur
36ad6 73 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  sor,.           
36ad7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ad8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ad9 20 20 20 20 20 20 20 20 26 70 54 6f 6b 65 6e 2c          &pToken,
36ada 20 26 6e 54 6f 6b 65 6e 42 79 74 65 73 2c 0a 20   &nTokenBytes,. 
36adb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36adc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36add 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ade 20 20 26 69 53 74 61 72 74 4f 66 66 73 65 74 2c    &iStartOffset,
36adf 20 26 69 45 6e 64 4f 66 66 73 65 74 2c 0a 20 20   &iEndOffset,.  
36ae0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ae1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ae2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ae3 20 26 69 50 6f 73 69 74 69 6f 6e 29 29 20 29 7b   &iPosition)) ){
36ae4 0a 20 20 20 20 44 4c 43 6f 6c 6c 65 63 74 6f 72  .    DLCollector
36ae5 20 2a 70 3b 0a 20 20 20 20 69 6e 74 20 6e 44 61   *p;.    int nDa
36ae6 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ta;             
36ae7 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
36ae8 20 64 6f 63 6c 69 73 74 20 62 65 66 6f 72 65 20   doclist before 
36ae9 6f 75 72 20 75 70 64 61 74 65 2e 20 2a 2f 0a 0a  our update. */..
36aea 20 20 20 20 2f 2a 20 50 6f 73 69 74 69 6f 6e 73      /* Positions
36aeb 20 63 61 6e 27 74 20 62 65 20 6e 65 67 61 74 69   can't be negati
36aec 76 65 3b 20 77 65 20 75 73 65 20 2d 31 20 61 73  ve; we use -1 as
36aed 20 61 20 74 65 72 6d 69 6e 61 74 6f 72 0a 20 20   a terminator.  
36aee 20 20 20 2a 20 69 6e 74 65 72 6e 61 6c 6c 79 2e     * internally.
36aef 20 20 54 6f 6b 65 6e 20 63 61 6e 27 74 20 62 65    Token can't be
36af0 20 4e 55 4c 4c 20 6f 72 20 65 6d 70 74 79 2e 20   NULL or empty. 
36af1 2a 2f 0a 20 20 20 20 69 66 28 20 69 50 6f 73 69  */.    if( iPosi
36af2 74 69 6f 6e 3c 30 20 7c 7c 20 70 54 6f 6b 65 6e  tion<0 || pToken
36af3 20 3d 3d 20 4e 55 4c 4c 20 7c 7c 20 6e 54 6f 6b   == NULL || nTok
36af4 65 6e 42 79 74 65 73 20 3d 3d 20 30 20 29 7b 0a  enBytes == 0 ){.
36af5 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
36af6 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 62  E_ERROR;.      b
36af7 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
36af8 20 70 20 3d 20 66 74 73 33 48 61 73 68 46 69 6e   p = fts3HashFin
36af9 64 28 26 76 2d 3e 70 65 6e 64 69 6e 67 54 65 72  d(&v->pendingTer
36afa 6d 73 2c 20 70 54 6f 6b 65 6e 2c 20 6e 54 6f 6b  ms, pToken, nTok
36afb 65 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 69 66  enBytes);.    if
36afc 28 20 70 3d 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20  ( p==NULL ){.   
36afd 20 20 20 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20     nData = 0;.  
36afe 20 20 20 20 70 20 3d 20 64 6c 63 4e 65 77 28 69      p = dlcNew(i
36aff 44 6f 63 69 64 2c 20 44 4c 5f 44 45 46 41 55 4c  Docid, DL_DEFAUL
36b00 54 29 3b 0a 20 20 20 20 20 20 66 74 73 33 48 61  T);.      fts3Ha
36b01 73 68 49 6e 73 65 72 74 28 26 76 2d 3e 70 65 6e  shInsert(&v->pen
36b02 64 69 6e 67 54 65 72 6d 73 2c 20 70 54 6f 6b 65  dingTerms, pToke
36b03 6e 2c 20 6e 54 6f 6b 65 6e 42 79 74 65 73 2c 20  n, nTokenBytes, 
36b04 70 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 76  p);..      /* Ov
36b05 65 72 68 65 61 64 20 66 6f 72 20 6f 75 72 20 68  erhead for our h
36b06 61 73 68 20 74 61 62 6c 65 20 65 6e 74 72 79 2c  ash table entry,
36b07 20 74 68 65 20 6b 65 79 2c 20 61 6e 64 20 74 68   the key, and th
36b08 65 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20  e value. */.    
36b09 20 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74    v->nPendingDat
36b0a 61 20 2b 3d 20 73 69 7a 65 6f 66 28 73 74 72 75  a += sizeof(stru
36b0b 63 74 20 66 74 73 33 48 61 73 68 45 6c 65 6d 29  ct fts3HashElem)
36b0c 2b 73 69 7a 65 6f 66 28 2a 70 29 2b 6e 54 6f 6b  +sizeof(*p)+nTok
36b0d 65 6e 42 79 74 65 73 3b 0a 20 20 20 20 7d 65 6c  enBytes;.    }el
36b0e 73 65 7b 0a 20 20 20 20 20 20 6e 44 61 74 61 20  se{.      nData 
36b0f 3d 20 70 2d 3e 62 2e 6e 44 61 74 61 3b 0a 20 20  = p->b.nData;.  
36b10 20 20 20 20 69 66 28 20 70 2d 3e 64 6c 77 2e 69      if( p->dlw.i
36b11 50 72 65 76 44 6f 63 69 64 21 3d 69 44 6f 63 69  PrevDocid!=iDoci
36b12 64 20 29 20 64 6c 63 4e 65 78 74 28 70 2c 20 69  d ) dlcNext(p, i
36b13 44 6f 63 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20  Docid);.    }.  
36b14 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3e 3d 30    if( iColumn>=0
36b15 20 29 7b 0a 20 20 20 20 20 20 64 6c 63 41 64 64   ){.      dlcAdd
36b16 50 6f 73 28 70 2c 20 69 43 6f 6c 75 6d 6e 2c 20  Pos(p, iColumn, 
36b17 69 50 6f 73 69 74 69 6f 6e 2c 20 69 53 74 61 72  iPosition, iStar
36b18 74 4f 66 66 73 65 74 2c 20 69 45 6e 64 4f 66 66  tOffset, iEndOff
36b19 73 65 74 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  set);.    }..   
36b1a 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 64   /* Accumulate d
36b1b 61 74 61 20 61 64 64 65 64 20 62 79 20 64 6c 63  ata added by dlc
36b1c 4e 65 77 20 6f 72 20 64 6c 63 4e 65 78 74 2c 20  New or dlcNext, 
36b1d 61 6e 64 20 64 6c 63 41 64 64 50 6f 73 2e 20 2a  and dlcAddPos. *
36b1e 2f 0a 20 20 20 20 76 2d 3e 6e 50 65 6e 64 69 6e  /.    v->nPendin
36b1f 67 44 61 74 61 20 2b 3d 20 70 2d 3e 62 2e 6e 44  gData += p->b.nD
36b20 61 74 61 2d 6e 44 61 74 61 3b 0a 20 20 7d 0a 0a  ata-nData;.  }..
36b21 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
36b22 20 43 68 65 63 6b 20 72 65 74 75 72 6e 3f 20 20   Check return?  
36b23 53 68 6f 75 6c 64 20 74 68 69 73 20 62 65 20 61  Should this be a
36b24 62 6c 65 20 74 6f 20 63 61 75 73 65 20 65 72 72  ble to cause err
36b25 6f 72 73 20 61 74 0a 20 20 2a 2a 20 74 68 69 73  ors at.  ** this
36b26 20 70 6f 69 6e 74 3f 20 20 41 63 74 75 61 6c 6c   point?  Actuall
36b27 79 2c 20 73 61 6d 65 20 71 75 65 73 74 69 6f 6e  y, same question
36b28 20 61 62 6f 75 74 20 73 71 6c 69 74 65 33 5f 66   about sqlite3_f
36b29 69 6e 61 6c 69 7a 65 28 29 2c 0a 20 20 2a 2a 20  inalize(),.  ** 
36b2a 74 68 6f 75 67 68 20 6f 6e 65 20 63 6f 75 6c 64  though one could
36b2b 20 61 72 67 75 65 20 74 68 61 74 20 66 61 69 6c   argue that fail
36b2c 75 72 65 20 74 68 65 72 65 20 6d 65 61 6e 73 20  ure there means 
36b2d 74 68 61 74 20 74 68 65 20 64 61 74 61 20 69 73  that the data is
36b2e 0a 20 20 2a 2a 20 6e 6f 74 20 64 75 72 61 62 6c  .  ** not durabl
36b2f 65 2e 20 20 2a 70 6f 6e 64 65 72 2a 0a 20 20 2a  e.  *ponder*.  *
36b30 2f 0a 20 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e  /.  pTokenizer->
36b31 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28  pModule->xClose(
36b32 70 43 75 72 73 6f 72 29 3b 0a 20 20 69 66 28 20  pCursor);.  if( 
36b33 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3d 3d 20 72  SQLITE_DONE == r
36b34 63 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  c ) return SQLIT
36b35 45 5f 4f 4b 3b 0a 20 20 72 65 74 75 72 6e 20 72  E_OK;.  return r
36b36 63 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20 64 6f 63  c;.}../* Add doc
36b37 6c 69 73 74 73 20 66 6f 72 20 61 6c 6c 20 74 65  lists for all te
36b38 72 6d 73 20 69 6e 20 5b 70 56 61 6c 75 65 73 5d  rms in [pValues]
36b39 20 74 6f 20 70 65 6e 64 69 6e 67 54 65 72 6d 73   to pendingTerms
36b3a 20 74 61 62 6c 65 2e 20 2a 2f 0a 73 74 61 74 69   table. */.stati
36b3b 63 20 69 6e 74 20 69 6e 73 65 72 74 54 65 72 6d  c int insertTerm
36b3c 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  s(fulltext_vtab 
36b3d 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  *v, sqlite_int64
36b3e 20 69 44 6f 63 69 64 2c 0a 20 20 20 20 20 20 20   iDocid,.       
36b3f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b40 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
36b41 70 56 61 6c 75 65 73 29 7b 0a 20 20 69 6e 74 20  pValues){.  int 
36b42 69 3b 0a 20 20 66 6f 72 28 69 20 3d 20 30 3b 20  i;.  for(i = 0; 
36b43 69 20 3c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 3b  i < v->nColumn ;
36b44 20 2b 2b 69 29 7b 0a 20 20 20 20 63 68 61 72 20   ++i){.    char 
36b45 2a 7a 54 65 78 74 20 3d 20 28 63 68 61 72 2a 29  *zText = (char*)
36b46 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
36b47 78 74 28 70 56 61 6c 75 65 73 5b 69 5d 29 3b 0a  xt(pValues[i]);.
36b48 20 20 20 20 69 6e 74 20 72 63 20 3d 20 62 75 69      int rc = bui
36b49 6c 64 54 65 72 6d 73 28 76 2c 20 69 44 6f 63 69  ldTerms(v, iDoci
36b4a 64 2c 20 7a 54 65 78 74 2c 20 69 29 3b 0a 20 20  d, zText, i);.  
36b4b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
36b4c 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
36b4d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
36b4e 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 41  LITE_OK;.}../* A
36b4f 64 64 20 65 6d 70 74 79 20 64 6f 63 6c 69 73 74  dd empty doclist
36b50 73 20 66 6f 72 20 61 6c 6c 20 74 65 72 6d 73 20  s for all terms 
36b51 69 6e 20 74 68 65 20 67 69 76 65 6e 20 72 6f 77  in the given row
36b52 27 73 20 63 6f 6e 74 65 6e 74 20 74 6f 0a 2a 2a  's content to.**
36b53 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 2e 0a 2a   pendingTerms..*
36b54 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 6c  /.static int del
36b55 65 74 65 54 65 72 6d 73 28 66 75 6c 6c 74 65 78  eteTerms(fulltex
36b56 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74  t_vtab *v, sqlit
36b57 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 29 7b  e_int64 iDocid){
36b58 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
36b59 70 56 61 6c 75 65 73 3b 0a 20 20 69 6e 74 20 69  pValues;.  int i
36b5a 2c 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f  , rc;..  /* TODO
36b5b 28 73 68 65 73 73 29 20 53 68 6f 75 6c 64 20 77  (shess) Should w
36b5c 65 20 61 6c 6c 6f 77 20 73 75 63 68 20 74 61 62  e allow such tab
36b5d 6c 65 73 20 61 74 20 61 6c 6c 3f 20 2a 2f 0a 20  les at all? */. 
36b5e 20 69 66 28 20 44 4c 5f 44 45 46 41 55 4c 54 3d   if( DL_DEFAULT=
36b5f 3d 44 4c 5f 44 4f 43 49 44 53 20 29 20 72 65 74  =DL_DOCIDS ) ret
36b60 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
36b61 3b 0a 0a 20 20 72 63 20 3d 20 63 6f 6e 74 65 6e  ;..  rc = conten
36b62 74 5f 73 65 6c 65 63 74 28 76 2c 20 69 44 6f 63  t_select(v, iDoc
36b63 69 64 2c 20 26 70 56 61 6c 75 65 73 29 3b 0a 20  id, &pValues);. 
36b64 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
36b65 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
36b66 0a 20 20 66 6f 72 28 69 20 3d 20 30 20 3b 20 69  .  for(i = 0 ; i
36b67 20 3c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b   < v->nColumn; +
36b68 2b 69 29 20 7b 0a 20 20 20 20 72 63 20 3d 20 62  +i) {.    rc = b
36b69 75 69 6c 64 54 65 72 6d 73 28 76 2c 20 69 44 6f  uildTerms(v, iDo
36b6a 63 69 64 2c 20 70 56 61 6c 75 65 73 5b 69 5d 2c  cid, pValues[i],
36b6b 20 2d 31 29 3b 0a 20 20 20 20 69 66 28 20 72 63   -1);.    if( rc
36b6c 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72  !=SQLITE_OK ) br
36b6d 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 66 72 65 65  eak;.  }..  free
36b6e 53 74 72 69 6e 67 41 72 72 61 79 28 76 2d 3e 6e  StringArray(v->n
36b6f 43 6f 6c 75 6d 6e 2c 20 70 56 61 6c 75 65 73 29  Column, pValues)
36b70 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
36b71 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f  E_OK;.}../* TODO
36b72 28 73 68 65 73 73 29 20 52 65 66 61 63 74 6f 72  (shess) Refactor
36b73 20 74 68 65 20 63 6f 64 65 20 74 6f 20 72 65 6d   the code to rem
36b74 6f 76 65 20 74 68 69 73 20 66 6f 72 77 61 72 64  ove this forward
36b75 20 64 65 63 6c 2e 20 2a 2f 0a 73 74 61 74 69 63   decl. */.static
36b76 20 69 6e 74 20 69 6e 69 74 50 65 6e 64 69 6e 67   int initPending
36b77 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76  Terms(fulltext_v
36b78 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69  tab *v, sqlite_i
36b79 6e 74 36 34 20 69 44 6f 63 69 64 29 3b 0a 0a 2f  nt64 iDocid);../
36b7a 2a 20 49 6e 73 65 72 74 20 61 20 72 6f 77 20 69  * Insert a row i
36b7b 6e 74 6f 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e  nto the %_conten
36b7c 74 20 74 61 62 6c 65 3b 20 73 65 74 20 2a 70 69  t table; set *pi
36b7d 44 6f 63 69 64 20 74 6f 20 62 65 20 74 68 65 20  Docid to be the 
36b7e 49 44 20 6f 66 20 74 68 65 0a 2a 2a 20 6e 65 77  ID of the.** new
36b7f 20 72 6f 77 2e 20 20 41 64 64 20 64 6f 63 6c 69   row.  Add docli
36b80 73 74 73 20 66 6f 72 20 74 65 72 6d 73 20 74 6f  sts for terms to
36b81 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 2e 0a 2a   pendingTerms..*
36b82 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 64  /.static int ind
36b83 65 78 5f 69 6e 73 65 72 74 28 66 75 6c 6c 74 65  ex_insert(fullte
36b84 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69  xt_vtab *v, sqli
36b85 74 65 33 5f 76 61 6c 75 65 20 2a 70 52 65 71 75  te3_value *pRequ
36b86 65 73 74 44 6f 63 69 64 2c 0a 20 20 20 20 20 20  estDocid,.      
36b87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36b88 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
36b89 2a 2a 70 56 61 6c 75 65 73 2c 20 73 71 6c 69 74  **pValues, sqlit
36b8a 65 5f 69 6e 74 36 34 20 2a 70 69 44 6f 63 69 64  e_int64 *piDocid
36b8b 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  ){.  int rc;..  
36b8c 72 63 20 3d 20 63 6f 6e 74 65 6e 74 5f 69 6e 73  rc = content_ins
36b8d 65 72 74 28 76 2c 20 70 52 65 71 75 65 73 74 44  ert(v, pRequestD
36b8e 6f 63 69 64 2c 20 70 56 61 6c 75 65 73 29 3b 20  ocid, pValues); 
36b8f 20 2f 2a 20 65 78 65 63 75 74 65 20 61 6e 20 53   /* execute an S
36b90 51 4c 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69  QL INSERT */.  i
36b91 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
36b92 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
36b93 20 2f 2a 20 64 6f 63 69 64 20 63 6f 6c 75 6d 6e   /* docid column
36b94 20 69 73 20 61 6e 20 61 6c 69 61 73 20 66 6f 72   is an alias for
36b95 20 72 6f 77 69 64 2e 20 2a 2f 0a 20 20 2a 70 69   rowid. */.  *pi
36b96 44 6f 63 69 64 20 3d 20 73 71 6c 69 74 65 33 5f  Docid = sqlite3_
36b97 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
36b98 64 28 76 2d 3e 64 62 29 3b 0a 20 20 72 63 20 3d  d(v->db);.  rc =
36b99 20 69 6e 69 74 50 65 6e 64 69 6e 67 54 65 72 6d   initPendingTerm
36b9a 73 28 76 2c 20 2a 70 69 44 6f 63 69 64 29 3b 0a  s(v, *piDocid);.
36b9b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
36b9c 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
36b9d 0a 0a 20 20 72 65 74 75 72 6e 20 69 6e 73 65 72  ..  return inser
36b9e 74 54 65 72 6d 73 28 76 2c 20 2a 70 69 44 6f 63  tTerms(v, *piDoc
36b9f 69 64 2c 20 70 56 61 6c 75 65 73 29 3b 0a 7d 0a  id, pValues);.}.
36ba0 0a 2f 2a 20 44 65 6c 65 74 65 20 61 20 72 6f 77  ./* Delete a row
36ba1 20 66 72 6f 6d 20 74 68 65 20 25 5f 63 6f 6e 74   from the %_cont
36ba2 65 6e 74 20 74 61 62 6c 65 3b 20 61 64 64 20 65  ent table; add e
36ba3 6d 70 74 79 20 64 6f 63 6c 69 73 74 73 20 66 6f  mpty doclists fo
36ba4 72 20 74 65 72 6d 73 0a 2a 2a 20 74 6f 20 70 65  r terms.** to pe
36ba5 6e 64 69 6e 67 54 65 72 6d 73 2e 0a 2a 2f 0a 73  ndingTerms..*/.s
36ba6 74 61 74 69 63 20 69 6e 74 20 69 6e 64 65 78 5f  tatic int index_
36ba7 64 65 6c 65 74 65 28 66 75 6c 6c 74 65 78 74 5f  delete(fulltext_
36ba8 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f  vtab *v, sqlite_
36ba9 69 6e 74 36 34 20 69 52 6f 77 29 7b 0a 20 20 69  int64 iRow){.  i
36baa 6e 74 20 72 63 20 3d 20 69 6e 69 74 50 65 6e 64  nt rc = initPend
36bab 69 6e 67 54 65 72 6d 73 28 76 2c 20 69 52 6f 77  ingTerms(v, iRow
36bac 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
36bad 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
36bae 72 63 3b 0a 0a 20 20 72 63 20 3d 20 64 65 6c 65  rc;..  rc = dele
36baf 74 65 54 65 72 6d 73 28 76 2c 20 69 52 6f 77 29  teTerms(v, iRow)
36bb0 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
36bb1 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
36bb2 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 63 6f 6e  c;..  return con
36bb3 74 65 6e 74 5f 64 65 6c 65 74 65 28 76 2c 20 69  tent_delete(v, i
36bb4 52 6f 77 29 3b 20 20 2f 2a 20 65 78 65 63 75 74  Row);  /* execut
36bb5 65 20 61 6e 20 53 51 4c 20 44 45 4c 45 54 45 20  e an SQL DELETE 
36bb6 2a 2f 0a 7d 0a 0a 2f 2a 20 55 70 64 61 74 65 20  */.}../* Update 
36bb7 61 20 72 6f 77 20 69 6e 20 74 68 65 20 25 5f 63  a row in the %_c
36bb8 6f 6e 74 65 6e 74 20 74 61 62 6c 65 3b 20 61 64  ontent table; ad
36bb9 64 20 64 65 6c 65 74 65 20 64 6f 63 6c 69 73 74  d delete doclist
36bba 73 20 74 6f 0a 2a 2a 20 70 65 6e 64 69 6e 67 54  s to.** pendingT
36bbb 65 72 6d 73 20 66 6f 72 20 6f 6c 64 20 74 65 72  erms for old ter
36bbc 6d 73 20 6e 6f 74 20 69 6e 20 74 68 65 20 6e 65  ms not in the ne
36bbd 77 20 64 61 74 61 2c 20 61 64 64 20 69 6e 73 65  w data, add inse
36bbe 72 74 20 64 6f 63 6c 69 73 74 73 0a 2a 2a 20 74  rt doclists.** t
36bbf 6f 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 66  o pendingTerms f
36bc0 6f 72 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20  or terms in the 
36bc1 6e 65 77 20 64 61 74 61 2e 0a 2a 2f 0a 73 74 61  new data..*/.sta
36bc2 74 69 63 20 69 6e 74 20 69 6e 64 65 78 5f 75 70  tic int index_up
36bc3 64 61 74 65 28 66 75 6c 6c 74 65 78 74 5f 76 74  date(fulltext_vt
36bc4 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e  ab *v, sqlite_in
36bc5 74 36 34 20 69 52 6f 77 2c 0a 20 20 20 20 20 20  t64 iRow,.      
36bc6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36bc7 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
36bc8 2a 2a 70 56 61 6c 75 65 73 29 7b 0a 20 20 69 6e  **pValues){.  in
36bc9 74 20 72 63 20 3d 20 69 6e 69 74 50 65 6e 64 69  t rc = initPendi
36bca 6e 67 54 65 72 6d 73 28 76 2c 20 69 52 6f 77 29  ngTerms(v, iRow)
36bcb 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
36bcc 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
36bcd 63 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74  c;..  /* Generat
36bce 65 20 61 6e 20 65 6d 70 74 79 20 64 6f 63 6c 69  e an empty docli
36bcf 73 74 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d  st for each term
36bd0 20 74 68 61 74 20 70 72 65 76 69 6f 75 73 6c 79   that previously
36bd1 20 61 70 70 65 61 72 65 64 20 69 6e 20 74 68 69   appeared in thi
36bd2 73 0a 20 20 20 2a 20 72 6f 77 2e 20 2a 2f 0a 20  s.   * row. */. 
36bd3 20 72 63 20 3d 20 64 65 6c 65 74 65 54 65 72 6d   rc = deleteTerm
36bd4 73 28 76 2c 20 69 52 6f 77 29 3b 0a 20 20 69 66  s(v, iRow);.  if
36bd5 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
36bd6 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
36bd7 72 63 20 3d 20 63 6f 6e 74 65 6e 74 5f 75 70 64  rc = content_upd
36bd8 61 74 65 28 76 2c 20 70 56 61 6c 75 65 73 2c 20  ate(v, pValues, 
36bd9 69 52 6f 77 29 3b 20 20 2f 2a 20 65 78 65 63 75  iRow);  /* execu
36bda 74 65 20 61 6e 20 53 51 4c 20 55 50 44 41 54 45  te an SQL UPDATE
36bdb 20 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51   */.  if( rc!=SQ
36bdc 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
36bdd 20 72 63 3b 0a 0a 20 20 2f 2a 20 4e 6f 77 20 61   rc;..  /* Now a
36bde 64 64 20 70 6f 73 69 74 69 6f 6e 73 20 66 6f 72  dd positions for
36bdf 20 74 65 72 6d 73 20 77 68 69 63 68 20 61 70 70   terms which app
36be0 65 61 72 20 69 6e 20 74 68 65 20 75 70 64 61 74  ear in the updat
36be1 65 64 20 72 6f 77 2e 20 2a 2f 0a 20 20 72 65 74  ed row. */.  ret
36be2 75 72 6e 20 69 6e 73 65 72 74 54 65 72 6d 73 28  urn insertTerms(
36be3 76 2c 20 69 52 6f 77 2c 20 70 56 61 6c 75 65 73  v, iRow, pValues
36be4 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  );.}../*********
36be5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36be6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36be7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36be8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 49  **********/./* I
36be9 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20 69 73  nteriorWriter is
36bea 20 75 73 65 64 20 74 6f 20 63 6f 6c 6c 65 63 74   used to collect
36beb 20 74 65 72 6d 73 20 61 6e 64 20 62 6c 6f 63 6b   terms and block
36bec 20 72 65 66 65 72 65 6e 63 65 73 20 69 6e 74 6f   references into
36bed 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  .** interior nod
36bee 65 73 20 69 6e 20 25 5f 73 65 67 6d 65 6e 74 73  es in %_segments
36bef 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74 61 72  .  See commentar
36bf0 79 20 61 74 20 74 6f 70 20 6f 66 20 66 69 6c 65  y at top of file
36bf1 20 66 6f 72 0a 2a 2a 20 66 6f 72 6d 61 74 2e 0a   for.** format..
36bf2 2a 2f 0a 0a 2f 2a 20 48 6f 77 20 6c 61 72 67 65  */../* How large
36bf3 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20   interior nodes 
36bf4 63 61 6e 20 67 72 6f 77 2e 20 2a 2f 0a 23 64 65  can grow. */.#de
36bf5 66 69 6e 65 20 49 4e 54 45 52 49 4f 52 5f 4d 41  fine INTERIOR_MA
36bf6 58 20 32 30 34 38 0a 0a 2f 2a 20 4d 69 6e 69 6d  X 2048../* Minim
36bf7 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72  um number of ter
36bf8 6d 73 20 70 65 72 20 69 6e 74 65 72 69 6f 72 20  ms per interior 
36bf9 6e 6f 64 65 20 28 65 78 63 65 70 74 20 74 68 65  node (except the
36bfa 20 72 6f 6f 74 29 2e 20 54 68 69 73 0a 2a 2a 20   root). This.** 
36bfb 70 72 65 76 65 6e 74 73 20 6c 61 72 67 65 20 74  prevents large t
36bfc 65 72 6d 73 20 66 72 6f 6d 20 6d 61 6b 69 6e 67  erms from making
36bfd 20 74 68 65 20 74 72 65 65 20 74 6f 6f 20 73 6b   the tree too sk
36bfe 69 6e 6e 79 20 2d 20 6d 75 73 74 20 62 65 20 3e  inny - must be >
36bff 30 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65  0.** so that the
36c00 20 74 72 65 65 20 61 6c 77 61 79 73 20 6d 61 6b   tree always mak
36c01 65 73 20 70 72 6f 67 72 65 73 73 2e 20 20 4e 6f  es progress.  No
36c02 74 65 20 74 68 61 74 20 74 68 65 20 6d 69 6e 20  te that the min 
36c03 74 72 65 65 0a 2a 2a 20 66 61 6e 6f 75 74 20 77  tree.** fanout w
36c04 69 6c 6c 20 62 65 20 49 4e 54 45 52 49 4f 52 5f  ill be INTERIOR_
36c05 4d 49 4e 5f 54 45 52 4d 53 2b 31 2e 0a 2a 2f 0a  MIN_TERMS+1..*/.
36c06 23 64 65 66 69 6e 65 20 49 4e 54 45 52 49 4f 52  #define INTERIOR
36c07 5f 4d 49 4e 5f 54 45 52 4d 53 20 37 0a 23 69 66  _MIN_TERMS 7.#if
36c08 20 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45   INTERIOR_MIN_TE
36c09 52 4d 53 3c 31 0a 23 20 65 72 72 6f 72 20 49 4e  RMS<1.# error IN
36c0a 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53  TERIOR_MIN_TERMS
36c0b 20 6d 75 73 74 20 62 65 20 67 72 65 61 74 65 72   must be greater
36c0c 20 74 68 61 6e 20 30 2e 0a 23 65 6e 64 69 66 0a   than 0..#endif.
36c0d 0a 2f 2a 20 52 4f 4f 54 5f 4d 41 58 20 63 6f 6e  ./* ROOT_MAX con
36c0e 74 72 6f 6c 73 20 68 6f 77 20 6d 75 63 68 20 64  trols how much d
36c0f 61 74 61 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ata is stored in
36c10 6c 69 6e 65 20 69 6e 20 74 68 65 20 73 65 67 6d  line in the segm
36c11 65 6e 74 0a 2a 2a 20 64 69 72 65 63 74 6f 72 79  ent.** directory
36c12 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  ..*/./* TODO(she
36c13 73 73 29 20 50 75 73 68 20 52 4f 4f 54 5f 4d 41  ss) Push ROOT_MA
36c14 58 20 64 6f 77 6e 20 74 6f 20 77 68 6f 65 76 65  X down to whoeve
36c15 72 20 69 73 20 77 72 69 74 69 6e 67 20 74 68 69  r is writing thi
36c16 6e 67 73 2e 20 20 49 74 27 73 0a 2a 2a 20 6f 6e  ngs.  It's.** on
36c17 6c 79 20 68 65 72 65 20 73 6f 20 74 68 61 74 20  ly here so that 
36c18 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 52 6f  interiorWriterRo
36c19 6f 74 49 6e 66 6f 28 29 20 61 6e 64 20 6c 65 61  otInfo() and lea
36c1a 66 57 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28  fWriterRootInfo(
36c1b 29 0a 2a 2a 20 63 61 6e 20 62 6f 74 68 20 73 65  ).** can both se
36c1c 65 20 69 74 2c 20 62 75 74 20 69 66 20 74 68 65  e it, but if the
36c1d 20 63 61 6c 6c 65 72 20 70 61 73 73 65 64 20 69   caller passed i
36c1e 74 20 69 6e 2c 20 77 65 20 77 6f 75 6c 64 6e 27  t in, we wouldn'
36c1f 74 20 65 76 65 6e 0a 2a 2a 20 6e 65 65 64 20 61  t even.** need a
36c20 20 64 65 66 69 6e 65 2e 0a 2a 2f 0a 23 64 65 66   define..*/.#def
36c21 69 6e 65 20 52 4f 4f 54 5f 4d 41 58 20 31 30 32  ine ROOT_MAX 102
36c22 34 0a 23 69 66 20 52 4f 4f 54 5f 4d 41 58 3c 56  4.#if ROOT_MAX<V
36c23 41 52 49 4e 54 5f 4d 41 58 2a 32 0a 23 20 65 72  ARINT_MAX*2.# er
36c24 72 6f 72 20 52 4f 4f 54 5f 4d 41 58 20 6d 75 73  ror ROOT_MAX mus
36c25 74 20 68 61 76 65 20 65 6e 6f 75 67 68 20 73 70  t have enough sp
36c26 61 63 65 20 66 6f 72 20 61 20 68 65 61 64 65 72  ace for a header
36c27 2e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 49 6e 74  ..#endif../* Int
36c28 65 72 69 6f 72 42 6c 6f 63 6b 20 73 74 6f 72 65  eriorBlock store
36c29 73 20 61 20 6c 69 6e 6b 65 64 2d 6c 69 73 74 20  s a linked-list 
36c2a 6f 66 20 69 6e 74 65 72 69 6f 72 20 62 6c 6f 63  of interior bloc
36c2b 6b 73 20 77 68 69 6c 65 20 61 20 6c 6f 77 65 72  ks while a lower
36c2c 0a 2a 2a 20 6c 61 79 65 72 20 69 73 20 62 65 69  .** layer is bei
36c2d 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 2e 0a  ng constructed..
36c2e 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
36c2f 74 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20  t InteriorBlock 
36c30 7b 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 74  {.  DataBuffer t
36c31 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 2f  erm;           /
36c32 2a 20 4c 65 66 74 6d 6f 73 74 20 74 65 72 6d 20  * Leftmost term 
36c33 69 6e 20 62 6c 6f 63 6b 27 73 20 73 75 62 74 72  in block's subtr
36c34 65 65 2e 20 2a 2f 0a 20 20 44 61 74 61 42 75 66  ee. */.  DataBuf
36c35 66 65 72 20 64 61 74 61 3b 20 20 20 20 20 20 20  fer data;       
36c36 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74      /* Accumulat
36c37 65 64 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  ed data for the 
36c38 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 73 74 72 75  block. */.  stru
36c39 63 74 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b  ct InteriorBlock
36c3a 20 2a 6e 65 78 74 3b 0a 7d 20 49 6e 74 65 72 69   *next;.} Interi
36c3b 6f 72 42 6c 6f 63 6b 3b 0a 0a 73 74 61 74 69 63  orBlock;..static
36c3c 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a   InteriorBlock *
36c3d 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 4e 65 77  interiorBlockNew
36c3e 28 69 6e 74 20 69 48 65 69 67 68 74 2c 20 73 71  (int iHeight, sq
36c3f 6c 69 74 65 5f 69 6e 74 36 34 20 69 43 68 69 6c  lite_int64 iChil
36c40 64 42 6c 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20  dBlock,.        
36c41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36c42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
36c43 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d  onst char *pTerm
36c44 2c 20 69 6e 74 20 6e 54 65 72 6d 29 7b 0a 20 20  , int nTerm){.  
36c45 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 62  InteriorBlock *b
36c46 6c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 6d  lock = sqlite3_m
36c47 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 49 6e 74  alloc(sizeof(Int
36c48 65 72 69 6f 72 42 6c 6f 63 6b 29 29 3b 0a 20 20  eriorBlock));.  
36c49 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41  char c[VARINT_MA
36c4a 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20  X+VARINT_MAX];. 
36c4b 20 69 6e 74 20 6e 3b 0a 0a 20 20 69 66 28 20 62   int n;..  if( b
36c4c 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 6d 65 6d 73  lock ){.    mems
36c4d 65 74 28 62 6c 6f 63 6b 2c 20 30 2c 20 73 69 7a  et(block, 0, siz
36c4e 65 6f 66 28 2a 62 6c 6f 63 6b 29 29 3b 0a 20 20  eof(*block));.  
36c4f 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74    dataBufferInit
36c50 28 26 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2c 20 30  (&block->term, 0
36c51 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65  );.    dataBuffe
36c52 72 52 65 70 6c 61 63 65 28 26 62 6c 6f 63 6b 2d  rReplace(&block-
36c53 3e 74 65 72 6d 2c 20 70 54 65 72 6d 2c 20 6e 54  >term, pTerm, nT
36c54 65 72 6d 29 3b 0a 0a 20 20 20 20 6e 20 3d 20 66  erm);..    n = f
36c55 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20  ts3PutVarint(c, 
36c56 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20 6e 20  iHeight);.    n 
36c57 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74  += fts3PutVarint
36c58 28 63 2b 6e 2c 20 69 43 68 69 6c 64 42 6c 6f 63  (c+n, iChildBloc
36c59 6b 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66  k);.    dataBuff
36c5a 65 72 49 6e 69 74 28 26 62 6c 6f 63 6b 2d 3e 64  erInit(&block->d
36c5b 61 74 61 2c 20 49 4e 54 45 52 49 4f 52 5f 4d 41  ata, INTERIOR_MA
36c5c 58 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66  X);.    dataBuff
36c5d 65 72 52 65 70 6c 61 63 65 28 26 62 6c 6f 63 6b  erReplace(&block
36c5e 2d 3e 64 61 74 61 2c 20 63 2c 20 6e 29 3b 0a 20  ->data, c, n);. 
36c5f 20 7d 0a 20 20 72 65 74 75 72 6e 20 62 6c 6f 63   }.  return bloc
36c60 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44  k;.}..#ifndef ND
36c61 45 42 55 47 0a 2f 2a 20 56 65 72 69 66 79 20 74  EBUG./* Verify t
36c62 68 61 74 20 74 68 65 20 64 61 74 61 20 69 73 20  hat the data is 
36c63 72 65 61 64 61 62 6c 65 20 61 73 20 61 6e 20 69  readable as an i
36c64 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20 2a 2f  nterior node. */
36c65 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 74  .static void int
36c66 65 72 69 6f 72 42 6c 6f 63 6b 56 61 6c 69 64 61  eriorBlockValida
36c67 74 65 28 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b  te(InteriorBlock
36c68 20 2a 70 42 6c 6f 63 6b 29 7b 0a 20 20 63 6f 6e   *pBlock){.  con
36c69 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 20 3d  st char *pData =
36c6a 20 70 42 6c 6f 63 6b 2d 3e 64 61 74 61 2e 70 44   pBlock->data.pD
36c6b 61 74 61 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61  ata;.  int nData
36c6c 20 3d 20 70 42 6c 6f 63 6b 2d 3e 64 61 74 61 2e   = pBlock->data.
36c6d 6e 44 61 74 61 3b 0a 20 20 69 6e 74 20 6e 2c 20  nData;.  int n, 
36c6e 69 44 75 6d 6d 79 3b 0a 20 20 73 71 6c 69 74 65  iDummy;.  sqlite
36c6f 5f 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69 64 3b  _int64 iBlockid;
36c70 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61 74  ..  assert( nDat
36c71 61 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  a>0 );.  assert(
36c72 20 70 44 61 74 61 21 3d 30 20 29 3b 0a 20 20 61   pData!=0 );.  a
36c73 73 73 65 72 74 28 20 70 44 61 74 61 2b 6e 44 61  ssert( pData+nDa
36c74 74 61 3e 70 44 61 74 61 20 29 3b 0a 0a 20 20 2f  ta>pData );..  /
36c75 2a 20 4d 75 73 74 20 6c 65 61 64 20 77 69 74 68  * Must lead with
36c76 20 68 65 69 67 68 74 20 6f 66 20 6e 6f 64 65 20   height of node 
36c77 61 73 20 61 20 76 61 72 69 6e 74 28 6e 29 2c 20  as a varint(n), 
36c78 6e 3e 30 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73  n>0 */.  n = fts
36c79 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 44 61  3GetVarint32(pDa
36c7a 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20  ta, &iDummy);.  
36c7b 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20  assert( n>0 );. 
36c7c 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79 3e   assert( iDummy>
36c7d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  0 );.  assert( n
36c7e 3c 6e 44 61 74 61 20 29 3b 0a 20 20 70 44 61 74  <nData );.  pDat
36c7f 61 20 2b 3d 20 6e 3b 0a 20 20 6e 44 61 74 61 20  a += n;.  nData 
36c80 2d 3d 20 6e 3b 0a 0a 20 20 2f 2a 20 4d 75 73 74  -= n;..  /* Must
36c81 20 63 6f 6e 74 61 69 6e 20 69 42 6c 6f 63 6b 69   contain iBlocki
36c82 64 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33  d. */.  n = fts3
36c83 47 65 74 56 61 72 69 6e 74 28 70 44 61 74 61 2c  GetVarint(pData,
36c84 20 26 69 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 61   &iBlockid);.  a
36c85 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20  ssert( n>0 );.  
36c86 61 73 73 65 72 74 28 20 6e 3c 3d 6e 44 61 74 61  assert( n<=nData
36c87 20 29 3b 0a 20 20 70 44 61 74 61 20 2b 3d 20 6e   );.  pData += n
36c88 3b 0a 20 20 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a  ;.  nData -= n;.
36c89 0a 20 20 2f 2a 20 5a 65 72 6f 20 6f 72 20 6d 6f  .  /* Zero or mo
36c8a 72 65 20 74 65 72 6d 73 20 6f 66 20 70 6f 73 69  re terms of posi
36c8b 74 69 76 65 20 6c 65 6e 67 74 68 20 2a 2f 0a 20  tive length */. 
36c8c 20 69 66 28 20 6e 44 61 74 61 21 3d 30 20 29 7b   if( nData!=0 ){
36c8d 0a 20 20 20 20 2f 2a 20 46 69 72 73 74 20 74 65  .    /* First te
36c8e 72 6d 20 69 73 20 6e 6f 74 20 64 65 6c 74 61 2d  rm is not delta-
36c8f 65 6e 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 20 20  encoded. */.    
36c90 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e  n = fts3GetVarin
36c91 74 33 32 28 70 44 61 74 61 2c 20 26 69 44 75 6d  t32(pData, &iDum
36c92 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  my);.    assert(
36c93 20 6e 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65   n>0 );.    asse
36c94 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b 0a  rt( iDummy>0 );.
36c95 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44      assert( n+iD
36c96 75 6d 6d 79 3e 30 29 3b 0a 20 20 20 20 61 73 73  ummy>0);.    ass
36c97 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e  ert( n+iDummy<=n
36c98 44 61 74 61 20 29 3b 0a 20 20 20 20 70 44 61 74  Data );.    pDat
36c99 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20  a += n+iDummy;. 
36c9a 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b 69 44     nData -= n+iD
36c9b 75 6d 6d 79 3b 0a 0a 20 20 20 20 2f 2a 20 46 6f  ummy;..    /* Fo
36c9c 6c 6c 6f 77 69 6e 67 20 74 65 72 6d 73 20 64 65  llowing terms de
36c9d 6c 74 61 2d 65 6e 63 6f 64 65 64 2e 20 2a 2f 0a  lta-encoded. */.
36c9e 20 20 20 20 77 68 69 6c 65 28 20 6e 44 61 74 61      while( nData
36c9f 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  !=0 ){.      /* 
36ca0 4c 65 6e 67 74 68 20 6f 66 20 73 68 61 72 65 64  Length of shared
36ca1 20 70 72 65 66 69 78 2e 20 2a 2f 0a 20 20 20 20   prefix. */.    
36ca2 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72    n = fts3GetVar
36ca3 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44  int32(pData, &iD
36ca4 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 61 73 73  ummy);.      ass
36ca5 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20  ert( n>0 );.    
36ca6 20 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79    assert( iDummy
36ca7 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  >=0 );.      ass
36ca8 65 72 74 28 20 6e 3c 6e 44 61 74 61 20 29 3b 0a  ert( n<nData );.
36ca9 20 20 20 20 20 20 70 44 61 74 61 20 2b 3d 20 6e        pData += n
36caa 3b 0a 20 20 20 20 20 20 6e 44 61 74 61 20 2d 3d  ;.      nData -=
36cab 20 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4c 65   n;..      /* Le
36cac 6e 67 74 68 20 61 6e 64 20 64 61 74 61 20 6f 66  ngth and data of
36cad 20 64 69 73 74 69 6e 63 74 20 73 75 66 66 69 78   distinct suffix
36cae 2e 20 2a 2f 0a 20 20 20 20 20 20 6e 20 3d 20 66  . */.      n = f
36caf 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
36cb0 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  Data, &iDummy);.
36cb1 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e        assert( n>
36cb2 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  0 );.      asser
36cb3 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20  t( iDummy>0 );. 
36cb4 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69       assert( n+i
36cb5 44 75 6d 6d 79 3e 30 29 3b 0a 20 20 20 20 20 20  Dummy>0);.      
36cb6 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79  assert( n+iDummy
36cb7 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 20  <=nData );.     
36cb8 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d   pData += n+iDum
36cb9 6d 79 3b 0a 20 20 20 20 20 20 6e 44 61 74 61 20  my;.      nData 
36cba 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20 20  -= n+iDummy;.   
36cbb 20 7d 0a 20 20 7d 0a 7d 0a 23 64 65 66 69 6e 65   }.  }.}.#define
36cbc 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 49 4e   ASSERT_VALID_IN
36cbd 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 78 29 20  TERIOR_BLOCK(x) 
36cbe 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 56 61 6c  interiorBlockVal
36cbf 69 64 61 74 65 28 78 29 0a 23 65 6c 73 65 0a 23  idate(x).#else.#
36cc0 64 65 66 69 6e 65 20 41 53 53 45 52 54 5f 56 41  define ASSERT_VA
36cc1 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c 4f  LID_INTERIOR_BLO
36cc2 43 4b 28 78 29 20 61 73 73 65 72 74 28 20 31 20  CK(x) assert( 1 
36cc3 29 0a 23 65 6e 64 69 66 0a 0a 74 79 70 65 64 65  ).#endif..typede
36cc4 66 20 73 74 72 75 63 74 20 49 6e 74 65 72 69 6f  f struct Interio
36cc5 72 57 72 69 74 65 72 20 7b 0a 20 20 69 6e 74 20  rWriter {.  int 
36cc6 69 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20  iHeight;        
36cc7 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 72             /* fr
36cc8 6f 6d 20 30 20 61 74 20 6c 65 61 76 65 73 2e 20  om 0 at leaves. 
36cc9 2a 2f 0a 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f  */.  InteriorBlo
36cca 63 6b 20 2a 66 69 72 73 74 2c 20 2a 6c 61 73 74  ck *first, *last
36ccb 3b 0a 20 20 73 74 72 75 63 74 20 49 6e 74 65 72  ;.  struct Inter
36ccc 69 6f 72 57 72 69 74 65 72 20 2a 70 61 72 65 6e  iorWriter *paren
36ccd 74 57 72 69 74 65 72 3b 0a 0a 20 20 44 61 74 61  tWriter;..  Data
36cce 42 75 66 66 65 72 20 74 65 72 6d 3b 20 20 20 20  Buffer term;    
36ccf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61             /* La
36cd0 73 74 20 74 65 72 6d 20 77 72 69 74 74 65 6e 20  st term written 
36cd1 74 6f 20 62 6c 6f 63 6b 20 22 6c 61 73 74 22 2e  to block "last".
36cd2 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74   */.  sqlite_int
36cd3 36 34 20 69 4f 70 65 6e 69 6e 67 43 68 69 6c 64  64 iOpeningChild
36cd4 42 6c 6f 63 6b 3b 20 2f 2a 20 46 69 72 73 74 20  Block; /* First 
36cd5 63 68 69 6c 64 20 62 6c 6f 63 6b 20 69 6e 20 62  child block in b
36cd6 6c 6f 63 6b 20 22 6c 61 73 74 22 2e 20 2a 2f 0a  lock "last". */.
36cd7 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
36cd8 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4c   sqlite_int64 iL
36cd9 61 73 74 43 68 69 6c 64 42 6c 6f 63 6b 3b 20 20  astChildBlock;  
36cda 2f 2a 20 66 6f 72 20 63 6f 6e 73 69 73 74 65 6e  /* for consisten
36cdb 63 79 20 63 68 65 63 6b 73 2e 20 2a 2f 0a 23 65  cy checks. */.#e
36cdc 6e 64 69 66 0a 7d 20 49 6e 74 65 72 69 6f 72 57  ndif.} InteriorW
36cdd 72 69 74 65 72 3b 0a 0a 2f 2a 20 49 6e 69 74 69  riter;../* Initi
36cde 61 6c 69 7a 65 20 61 6e 20 69 6e 74 65 72 69 6f  alize an interio
36cdf 72 20 6e 6f 64 65 20 77 68 65 72 65 20 70 54 65  r node where pTe
36ce0 72 6d 5b 6e 54 65 72 6d 5d 20 6d 61 72 6b 73 20  rm[nTerm] marks 
36ce1 74 68 65 20 6c 65 66 74 6d 6f 73 74 0a 2a 2a 20  the leftmost.** 
36ce2 74 65 72 6d 20 69 6e 20 74 68 65 20 74 72 65 65  term in the tree
36ce3 2e 20 20 69 43 68 69 6c 64 42 6c 6f 63 6b 20 69  .  iChildBlock i
36ce4 73 20 74 68 65 20 6c 65 66 74 6d 6f 73 74 20 63  s the leftmost c
36ce5 68 69 6c 64 20 62 6c 6f 63 6b 20 61 74 20 74 68  hild block at th
36ce6 65 0a 2a 2a 20 6e 65 78 74 20 6c 65 76 65 6c 20  e.** next level 
36ce7 64 6f 77 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a  down the tree..*
36ce8 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e  /.static void in
36ce9 74 65 72 69 6f 72 57 72 69 74 65 72 49 6e 69 74  teriorWriterInit
36cea 28 69 6e 74 20 69 48 65 69 67 68 74 2c 20 63 6f  (int iHeight, co
36ceb 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c  nst char *pTerm,
36cec 20 69 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20 20   int nTerm,.    
36ced 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36cee 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
36cef 65 5f 69 6e 74 36 34 20 69 43 68 69 6c 64 42 6c  e_int64 iChildBl
36cf0 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ock,.           
36cf1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36cf2 20 20 20 20 49 6e 74 65 72 69 6f 72 57 72 69 74      InteriorWrit
36cf3 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20  er *pWriter){.  
36cf4 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 62  InteriorBlock *b
36cf5 6c 6f 63 6b 3b 0a 20 20 61 73 73 65 72 74 28 20  lock;.  assert( 
36cf6 69 48 65 69 67 68 74 3e 30 20 29 3b 0a 20 20 43  iHeight>0 );.  C
36cf7 4c 45 41 52 28 70 57 72 69 74 65 72 29 3b 0a 0a  LEAR(pWriter);..
36cf8 20 20 70 57 72 69 74 65 72 2d 3e 69 48 65 69 67    pWriter->iHeig
36cf9 68 74 20 3d 20 69 48 65 69 67 68 74 3b 0a 20 20  ht = iHeight;.  
36cfa 70 57 72 69 74 65 72 2d 3e 69 4f 70 65 6e 69 6e  pWriter->iOpenin
36cfb 67 43 68 69 6c 64 42 6c 6f 63 6b 20 3d 20 69 43  gChildBlock = iC
36cfc 68 69 6c 64 42 6c 6f 63 6b 3b 0a 23 69 66 6e 64  hildBlock;.#ifnd
36cfd 65 66 20 4e 44 45 42 55 47 0a 20 20 70 57 72 69  ef NDEBUG.  pWri
36cfe 74 65 72 2d 3e 69 4c 61 73 74 43 68 69 6c 64 42  ter->iLastChildB
36cff 6c 6f 63 6b 20 3d 20 69 43 68 69 6c 64 42 6c 6f  lock = iChildBlo
36d00 63 6b 3b 0a 23 65 6e 64 69 66 0a 20 20 62 6c 6f  ck;.#endif.  blo
36d01 63 6b 20 3d 20 69 6e 74 65 72 69 6f 72 42 6c 6f  ck = interiorBlo
36d02 63 6b 4e 65 77 28 69 48 65 69 67 68 74 2c 20 69  ckNew(iHeight, i
36d03 43 68 69 6c 64 42 6c 6f 63 6b 2c 20 70 54 65 72  ChildBlock, pTer
36d04 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 70 57 72  m, nTerm);.  pWr
36d05 69 74 65 72 2d 3e 6c 61 73 74 20 3d 20 70 57 72  iter->last = pWr
36d06 69 74 65 72 2d 3e 66 69 72 73 74 20 3d 20 62 6c  iter->first = bl
36d07 6f 63 6b 3b 0a 20 20 41 53 53 45 52 54 5f 56 41  ock;.  ASSERT_VA
36d08 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c 4f  LID_INTERIOR_BLO
36d09 43 4b 28 70 57 72 69 74 65 72 2d 3e 6c 61 73 74  CK(pWriter->last
36d0a 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 49  );.  dataBufferI
36d0b 6e 69 74 28 26 70 57 72 69 74 65 72 2d 3e 74 65  nit(&pWriter->te
36d0c 72 6d 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 20 41 70  rm, 0);.}../* Ap
36d0d 70 65 6e 64 20 74 68 65 20 63 68 69 6c 64 20 6e  pend the child n
36d0e 6f 64 65 20 72 6f 6f 74 65 64 20 61 74 20 69 43  ode rooted at iC
36d0f 68 69 6c 64 42 6c 6f 63 6b 20 74 6f 20 74 68 65  hildBlock to the
36d10 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2c 0a   interior node,.
36d11 2a 2a 20 77 69 74 68 20 70 54 65 72 6d 5b 6e 54  ** with pTerm[nT
36d12 65 72 6d 5d 20 61 73 20 74 68 65 20 6c 65 66 74  erm] as the left
36d13 6d 6f 73 74 20 74 65 72 6d 20 69 6e 20 69 43 68  most term in iCh
36d14 69 6c 64 42 6c 6f 63 6b 27 73 20 73 75 62 74 72  ildBlock's subtr
36d15 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ee..*/.static vo
36d16 69 64 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65  id interiorWrite
36d17 72 41 70 70 65 6e 64 28 49 6e 74 65 72 69 6f 72  rAppend(Interior
36d18 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c  Writer *pWriter,
36d19 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
36d1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36d1b 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
36d1c 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 0a  erm, int nTerm,.
36d1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36d1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36d1f 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 43   sqlite_int64 iC
36d20 68 69 6c 64 42 6c 6f 63 6b 29 7b 0a 20 20 63 68  hildBlock){.  ch
36d21 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b  ar c[VARINT_MAX+
36d22 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69  VARINT_MAX];.  i
36d23 6e 74 20 6e 2c 20 6e 50 72 65 66 69 78 20 3d 20  nt n, nPrefix = 
36d24 30 3b 0a 0a 20 20 41 53 53 45 52 54 5f 56 41 4c  0;..  ASSERT_VAL
36d25 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43  ID_INTERIOR_BLOC
36d26 4b 28 70 57 72 69 74 65 72 2d 3e 6c 61 73 74 29  K(pWriter->last)
36d27 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 69 72 73  ;..  /* The firs
36d28 74 20 74 65 72 6d 20 77 72 69 74 74 65 6e 20 69  t term written i
36d29 6e 74 6f 20 61 6e 20 69 6e 74 65 72 69 6f 72 20  nto an interior 
36d2a 6e 6f 64 65 20 69 73 20 61 63 74 75 61 6c 6c 79  node is actually
36d2b 0a 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 64  .  ** associated
36d2c 20 77 69 74 68 20 74 68 65 20 73 65 63 6f 6e 64   with the second
36d2d 20 63 68 69 6c 64 20 61 64 64 65 64 20 28 74 68   child added (th
36d2e 65 20 66 69 72 73 74 20 63 68 69 6c 64 20 77 61  e first child wa
36d2f 73 20 61 64 64 65 64 0a 20 20 2a 2a 20 69 6e 20  s added.  ** in 
36d30 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 49 6e  interiorWriterIn
36d31 69 74 2c 20 6f 72 20 69 6e 20 74 68 65 20 69 66  it, or in the if
36d32 20 63 6c 61 75 73 65 20 61 74 20 74 68 65 20 62   clause at the b
36d33 6f 74 74 6f 6d 20 6f 66 20 74 68 69 73 0a 20 20  ottom of this.  
36d34 2a 2a 20 66 75 6e 63 74 69 6f 6e 29 2e 20 20 54  ** function).  T
36d35 68 61 74 20 74 65 72 6d 20 67 65 74 73 20 65 6e  hat term gets en
36d36 63 6f 64 65 64 20 73 74 72 61 69 67 68 74 20 75  coded straight u
36d37 70 2c 20 77 69 74 68 20 6e 50 72 65 66 69 78 20  p, with nPrefix 
36d38 6c 65 66 74 0a 20 20 2a 2a 20 61 74 20 30 2e 0a  left.  ** at 0..
36d39 20 20 2a 2f 0a 20 20 69 66 28 20 70 57 72 69 74    */.  if( pWrit
36d3a 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3d 3d  er->term.nData==
36d3b 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 66 74 73  0 ){.    n = fts
36d3c 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20 6e 54  3PutVarint(c, nT
36d3d 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  erm);.  }else{. 
36d3e 20 20 20 77 68 69 6c 65 28 20 6e 50 72 65 66 69     while( nPrefi
36d3f 78 3c 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2e  x<pWriter->term.
36d40 6e 44 61 74 61 20 26 26 0a 20 20 20 20 20 20 20  nData &&.       
36d41 20 20 20 20 70 54 65 72 6d 5b 6e 50 72 65 66 69      pTerm[nPrefi
36d42 78 5d 3d 3d 70 57 72 69 74 65 72 2d 3e 74 65 72  x]==pWriter->ter
36d43 6d 2e 70 44 61 74 61 5b 6e 50 72 65 66 69 78 5d  m.pData[nPrefix]
36d44 20 29 7b 0a 20 20 20 20 20 20 6e 50 72 65 66 69   ){.      nPrefi
36d45 78 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  x++;.    }..    
36d46 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e  n = fts3PutVarin
36d47 74 28 63 2c 20 6e 50 72 65 66 69 78 29 3b 0a 20  t(c, nPrefix);. 
36d48 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74 56     n += fts3PutV
36d49 61 72 69 6e 74 28 63 2b 6e 2c 20 6e 54 65 72 6d  arint(c+n, nTerm
36d4a 2d 6e 50 72 65 66 69 78 29 3b 0a 20 20 7d 0a 0a  -nPrefix);.  }..
36d4b 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
36d4c 20 70 57 72 69 74 65 72 2d 3e 69 4c 61 73 74 43   pWriter->iLastC
36d4d 68 69 6c 64 42 6c 6f 63 6b 2b 2b 3b 0a 23 65 6e  hildBlock++;.#en
36d4e 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 70 57  dif.  assert( pW
36d4f 72 69 74 65 72 2d 3e 69 4c 61 73 74 43 68 69 6c  riter->iLastChil
36d50 64 42 6c 6f 63 6b 3d 3d 69 43 68 69 6c 64 42 6c  dBlock==iChildBl
36d51 6f 63 6b 20 29 3b 0a 0a 20 20 2f 2a 20 4f 76 65  ock );..  /* Ove
36d52 72 66 6c 6f 77 20 74 6f 20 61 20 6e 65 77 20 62  rflow to a new b
36d53 6c 6f 63 6b 20 69 66 20 74 68 65 20 6e 65 77 20  lock if the new 
36d54 74 65 72 6d 20 6d 61 6b 65 73 20 74 68 65 20 63  term makes the c
36d55 75 72 72 65 6e 74 20 62 6c 6f 63 6b 0a 20 20 2a  urrent block.  *
36d56 2a 20 74 6f 6f 20 62 69 67 2c 20 61 6e 64 20 74  * too big, and t
36d57 68 65 20 63 75 72 72 65 6e 74 20 62 6c 6f 63 6b  he current block
36d58 20 61 6c 72 65 61 64 79 20 68 61 73 20 65 6e 6f   already has eno
36d59 75 67 68 20 74 65 72 6d 73 2e 0a 20 20 2a 2f 0a  ugh terms..  */.
36d5a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e 6c    if( pWriter->l
36d5b 61 73 74 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2b  ast->data.nData+
36d5c 6e 2b 6e 54 65 72 6d 2d 6e 50 72 65 66 69 78 3e  n+nTerm-nPrefix>
36d5d 49 4e 54 45 52 49 4f 52 5f 4d 41 58 20 26 26 0a  INTERIOR_MAX &&.
36d5e 20 20 20 20 20 20 69 43 68 69 6c 64 42 6c 6f 63        iChildBloc
36d5f 6b 2d 70 57 72 69 74 65 72 2d 3e 69 4f 70 65 6e  k-pWriter->iOpen
36d60 69 6e 67 43 68 69 6c 64 42 6c 6f 63 6b 3e 49 4e  ingChildBlock>IN
36d61 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53  TERIOR_MIN_TERMS
36d62 20 29 7b 0a 20 20 20 20 70 57 72 69 74 65 72 2d   ){.    pWriter-
36d63 3e 6c 61 73 74 2d 3e 6e 65 78 74 20 3d 20 69 6e  >last->next = in
36d64 74 65 72 69 6f 72 42 6c 6f 63 6b 4e 65 77 28 70  teriorBlockNew(p
36d65 57 72 69 74 65 72 2d 3e 69 48 65 69 67 68 74 2c  Writer->iHeight,
36d66 20 69 43 68 69 6c 64 42 6c 6f 63 6b 2c 0a 20 20   iChildBlock,.  
36d67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36d68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36d69 20 20 20 20 20 20 20 20 20 70 54 65 72 6d 2c 20           pTerm, 
36d6a 6e 54 65 72 6d 29 3b 0a 20 20 20 20 70 57 72 69  nTerm);.    pWri
36d6b 74 65 72 2d 3e 6c 61 73 74 20 3d 20 70 57 72 69  ter->last = pWri
36d6c 74 65 72 2d 3e 6c 61 73 74 2d 3e 6e 65 78 74 3b  ter->last->next;
36d6d 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69 4f  .    pWriter->iO
36d6e 70 65 6e 69 6e 67 43 68 69 6c 64 42 6c 6f 63 6b  peningChildBlock
36d6f 20 3d 20 69 43 68 69 6c 64 42 6c 6f 63 6b 3b 0a   = iChildBlock;.
36d70 20 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65      dataBufferRe
36d71 73 65 74 28 26 70 57 72 69 74 65 72 2d 3e 74 65  set(&pWriter->te
36d72 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  rm);.  }else{.  
36d73 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
36d74 6e 64 32 28 26 70 57 72 69 74 65 72 2d 3e 6c 61  nd2(&pWriter->la
36d75 73 74 2d 3e 64 61 74 61 2c 20 63 2c 20 6e 2c 0a  st->data, c, n,.
36d76 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36d77 20 20 20 20 20 20 70 54 65 72 6d 2b 6e 50 72 65        pTerm+nPre
36d78 66 69 78 2c 20 6e 54 65 72 6d 2d 6e 50 72 65 66  fix, nTerm-nPref
36d79 69 78 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66  ix);.    dataBuf
36d7a 66 65 72 52 65 70 6c 61 63 65 28 26 70 57 72 69  ferReplace(&pWri
36d7b 74 65 72 2d 3e 74 65 72 6d 2c 20 70 54 65 72 6d  ter->term, pTerm
36d7c 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 7d 0a 20 20  , nTerm);.  }.  
36d7d 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 49 4e 54  ASSERT_VALID_INT
36d7e 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 70 57 72 69  ERIOR_BLOCK(pWri
36d7f 74 65 72 2d 3e 6c 61 73 74 29 3b 0a 7d 0a 0a 2f  ter->last);.}../
36d80 2a 20 46 72 65 65 20 74 68 65 20 73 70 61 63 65  * Free the space
36d81 20 75 73 65 64 20 62 79 20 70 57 72 69 74 65 72   used by pWriter
36d82 2c 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20  , including the 
36d83 6c 69 6e 6b 65 64 2d 6c 69 73 74 20 6f 66 0a 2a  linked-list of.*
36d84 2a 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 73  * InteriorBlocks
36d85 2c 20 61 6e 64 20 70 61 72 65 6e 74 57 72 69 74  , and parentWrit
36d86 65 72 2c 20 69 66 20 70 72 65 73 65 6e 74 2e 0a  er, if present..
36d87 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e  */.static int in
36d88 74 65 72 69 6f 72 57 72 69 74 65 72 44 65 73 74  teriorWriterDest
36d89 72 6f 79 28 49 6e 74 65 72 69 6f 72 57 72 69 74  roy(InteriorWrit
36d8a 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20  er *pWriter){.  
36d8b 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 62  InteriorBlock *b
36d8c 6c 6f 63 6b 20 3d 20 70 57 72 69 74 65 72 2d 3e  lock = pWriter->
36d8d 66 69 72 73 74 3b 0a 0a 20 20 77 68 69 6c 65 28  first;..  while(
36d8e 20 62 6c 6f 63 6b 21 3d 4e 55 4c 4c 20 29 7b 0a   block!=NULL ){.
36d8f 20 20 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63      InteriorBloc
36d90 6b 20 2a 62 20 3d 20 62 6c 6f 63 6b 3b 0a 20 20  k *b = block;.  
36d91 20 20 62 6c 6f 63 6b 20 3d 20 62 6c 6f 63 6b 2d    block = block-
36d92 3e 6e 65 78 74 3b 0a 20 20 20 20 64 61 74 61 42  >next;.    dataB
36d93 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 62 2d  ufferDestroy(&b-
36d94 3e 74 65 72 6d 29 3b 0a 20 20 20 20 64 61 74 61  >term);.    data
36d95 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 62  BufferDestroy(&b
36d96 2d 3e 64 61 74 61 29 3b 0a 20 20 20 20 73 71 6c  ->data);.    sql
36d97 69 74 65 33 5f 66 72 65 65 28 62 29 3b 0a 20 20  ite3_free(b);.  
36d98 7d 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d  }.  if( pWriter-
36d99 3e 70 61 72 65 6e 74 57 72 69 74 65 72 21 3d 4e  >parentWriter!=N
36d9a 55 4c 4c 20 29 7b 0a 20 20 20 20 69 6e 74 65 72  ULL ){.    inter
36d9b 69 6f 72 57 72 69 74 65 72 44 65 73 74 72 6f 79  iorWriterDestroy
36d9c 28 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e 74  (pWriter->parent
36d9d 57 72 69 74 65 72 29 3b 0a 20 20 20 20 73 71 6c  Writer);.    sql
36d9e 69 74 65 33 5f 66 72 65 65 28 70 57 72 69 74 65  ite3_free(pWrite
36d9f 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 29  r->parentWriter)
36da0 3b 0a 20 20 7d 0a 20 20 64 61 74 61 42 75 66 66  ;.  }.  dataBuff
36da1 65 72 44 65 73 74 72 6f 79 28 26 70 57 72 69 74  erDestroy(&pWrit
36da2 65 72 2d 3e 74 65 72 6d 29 3b 0a 20 20 53 43 52  er->term);.  SCR
36da3 41 4d 42 4c 45 28 70 57 72 69 74 65 72 29 3b 0a  AMBLE(pWriter);.
36da4 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
36da5 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49 66 20 70 57 72  OK;.}../* If pWr
36da6 69 74 65 72 20 63 61 6e 20 66 69 74 20 65 6e 74  iter can fit ent
36da7 69 72 65 6c 79 20 69 6e 20 52 4f 4f 54 5f 4d 41  irely in ROOT_MA
36da8 58 2c 20 72 65 74 75 72 6e 20 69 74 20 61 73 20  X, return it as 
36da9 74 68 65 20 72 6f 6f 74 20 69 6e 66 6f 0a 2a 2a  the root info.**
36daa 20 64 69 72 65 63 74 6c 79 2c 20 6c 65 61 76 69   directly, leavi
36dab 6e 67 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64  ng *piEndBlockid
36dac 20 75 6e 63 68 61 6e 67 65 64 2e 20 20 4f 74 68   unchanged.  Oth
36dad 65 72 77 69 73 65 2c 20 66 6c 75 73 68 0a 2a 2a  erwise, flush.**
36dae 20 70 57 72 69 74 65 72 20 74 6f 20 25 5f 73 65   pWriter to %_se
36daf 67 6d 65 6e 74 73 2c 20 62 75 69 6c 64 69 6e 67  gments, building
36db0 20 61 20 6e 65 77 20 6c 61 79 65 72 20 6f 66 20   a new layer of 
36db1 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 2c 20  interior nodes, 
36db2 61 6e 64 0a 2a 2a 20 72 65 63 75 72 73 69 76 65  and.** recursive
36db3 6c 79 20 61 73 6b 20 66 6f 72 20 74 68 65 69 72  ly ask for their
36db4 20 72 6f 6f 74 20 69 6e 74 6f 2e 0a 2a 2f 0a 73   root into..*/.s
36db5 74 61 74 69 63 20 69 6e 74 20 69 6e 74 65 72 69  tatic int interi
36db6 6f 72 57 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f  orWriterRootInfo
36db7 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
36db8 76 2c 20 49 6e 74 65 72 69 6f 72 57 72 69 74 65  v, InteriorWrite
36db9 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20  r *pWriter,.    
36dba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36dbb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 68                ch
36dbc 61 72 20 2a 2a 70 70 52 6f 6f 74 49 6e 66 6f 2c  ar **ppRootInfo,
36dbd 20 69 6e 74 20 2a 70 6e 52 6f 6f 74 49 6e 66 6f   int *pnRootInfo
36dbe 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
36dbf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36dc0 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
36dc1 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64 29 7b   *piEndBlockid){
36dc2 0a 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b  .  InteriorBlock
36dc3 20 2a 62 6c 6f 63 6b 20 3d 20 70 57 72 69 74 65   *block = pWrite
36dc4 72 2d 3e 66 69 72 73 74 3b 0a 20 20 73 71 6c 69  r->first;.  sqli
36dc5 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69  te_int64 iBlocki
36dc6 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b  d = 0;.  int rc;
36dc7 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 61 6e  ..  /* If we can
36dc8 20 66 69 74 20 74 68 65 20 73 65 67 6d 65 6e 74   fit the segment
36dc9 20 69 6e 6c 69 6e 65 20 2a 2f 0a 20 20 69 66 28   inline */.  if(
36dca 20 62 6c 6f 63 6b 3d 3d 70 57 72 69 74 65 72 2d   block==pWriter-
36dcb 3e 6c 61 73 74 20 26 26 20 62 6c 6f 63 6b 2d 3e  >last && block->
36dcc 64 61 74 61 2e 6e 44 61 74 61 3c 52 4f 4f 54 5f  data.nData<ROOT_
36dcd 4d 41 58 20 29 7b 0a 20 20 20 20 2a 70 70 52 6f  MAX ){.    *ppRo
36dce 6f 74 49 6e 66 6f 20 3d 20 62 6c 6f 63 6b 2d 3e  otInfo = block->
36dcf 64 61 74 61 2e 70 44 61 74 61 3b 0a 20 20 20 20  data.pData;.    
36dd0 2a 70 6e 52 6f 6f 74 49 6e 66 6f 20 3d 20 62 6c  *pnRootInfo = bl
36dd1 6f 63 6b 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3b  ock->data.nData;
36dd2 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
36dd3 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  TE_OK;.  }..  /*
36dd4 20 46 6c 75 73 68 20 74 68 65 20 66 69 72 73 74   Flush the first
36dd5 20 62 6c 6f 63 6b 20 74 6f 20 25 5f 73 65 67 6d   block to %_segm
36dd6 65 6e 74 73 2c 20 61 6e 64 20 63 72 65 61 74 65  ents, and create
36dd7 20 61 20 6e 65 77 20 6c 65 76 65 6c 20 6f 66 0a   a new level of.
36dd8 20 20 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f    ** interior no
36dd9 64 65 2e 0a 20 20 2a 2f 0a 20 20 41 53 53 45 52  de..  */.  ASSER
36dda 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52  T_VALID_INTERIOR
36ddb 5f 42 4c 4f 43 4b 28 62 6c 6f 63 6b 29 3b 0a 20  _BLOCK(block);. 
36ddc 20 72 63 20 3d 20 62 6c 6f 63 6b 5f 69 6e 73 65   rc = block_inse
36ddd 72 74 28 76 2c 20 62 6c 6f 63 6b 2d 3e 64 61 74  rt(v, block->dat
36dde 61 2e 70 44 61 74 61 2c 20 62 6c 6f 63 6b 2d 3e  a.pData, block->
36ddf 64 61 74 61 2e 6e 44 61 74 61 2c 20 26 69 42 6c  data.nData, &iBl
36de0 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63  ockid);.  if( rc
36de1 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
36de2 74 75 72 6e 20 72 63 3b 0a 20 20 2a 70 69 45 6e  turn rc;.  *piEn
36de3 64 42 6c 6f 63 6b 69 64 20 3d 20 69 42 6c 6f 63  dBlockid = iBloc
36de4 6b 69 64 3b 0a 0a 20 20 70 57 72 69 74 65 72 2d  kid;..  pWriter-
36de5 3e 70 61 72 65 6e 74 57 72 69 74 65 72 20 3d 20  >parentWriter = 
36de6 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
36de7 69 7a 65 6f 66 28 2a 70 57 72 69 74 65 72 2d 3e  izeof(*pWriter->
36de8 70 61 72 65 6e 74 57 72 69 74 65 72 29 29 3b 0a  parentWriter));.
36de9 20 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72    interiorWriter
36dea 49 6e 69 74 28 70 57 72 69 74 65 72 2d 3e 69 48  Init(pWriter->iH
36deb 65 69 67 68 74 2b 31 2c 0a 20 20 20 20 20 20 20  eight+1,.       
36dec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6c                bl
36ded 6f 63 6b 2d 3e 74 65 72 6d 2e 70 44 61 74 61 2c  ock->term.pData,
36dee 20 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2e 6e 44 61   block->term.nDa
36def 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ta,.            
36df0 20 20 20 20 20 20 20 20 20 69 42 6c 6f 63 6b 69           iBlocki
36df1 64 2c 20 70 57 72 69 74 65 72 2d 3e 70 61 72 65  d, pWriter->pare
36df2 6e 74 57 72 69 74 65 72 29 3b 0a 0a 20 20 2f 2a  ntWriter);..  /*
36df3 20 46 6c 75 73 68 20 61 64 64 69 74 69 6f 6e 61   Flush additiona
36df4 6c 20 62 6c 6f 63 6b 73 20 61 6e 64 20 61 70 70  l blocks and app
36df5 65 6e 64 20 74 6f 20 74 68 65 20 68 69 67 68 65  end to the highe
36df6 72 20 69 6e 74 65 72 69 6f 72 0a 20 20 2a 2a 20  r interior.  ** 
36df7 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  node..  */.  for
36df8 28 62 6c 6f 63 6b 3d 62 6c 6f 63 6b 2d 3e 6e 65  (block=block->ne
36df9 78 74 3b 20 62 6c 6f 63 6b 21 3d 4e 55 4c 4c 3b  xt; block!=NULL;
36dfa 20 62 6c 6f 63 6b 3d 62 6c 6f 63 6b 2d 3e 6e 65   block=block->ne
36dfb 78 74 29 7b 0a 20 20 20 20 41 53 53 45 52 54 5f  xt){.    ASSERT_
36dfc 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42  VALID_INTERIOR_B
36dfd 4c 4f 43 4b 28 62 6c 6f 63 6b 29 3b 0a 20 20 20  LOCK(block);.   
36dfe 20 72 63 20 3d 20 62 6c 6f 63 6b 5f 69 6e 73 65   rc = block_inse
36dff 72 74 28 76 2c 20 62 6c 6f 63 6b 2d 3e 64 61 74  rt(v, block->dat
36e00 61 2e 70 44 61 74 61 2c 20 62 6c 6f 63 6b 2d 3e  a.pData, block->
36e01 64 61 74 61 2e 6e 44 61 74 61 2c 20 26 69 42 6c  data.nData, &iBl
36e02 6f 63 6b 69 64 29 3b 0a 20 20 20 20 69 66 28 20  ockid);.    if( 
36e03 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
36e04 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 2a  return rc;.    *
36e05 70 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 69  piEndBlockid = i
36e06 42 6c 6f 63 6b 69 64 3b 0a 0a 20 20 20 20 69 6e  Blockid;..    in
36e07 74 65 72 69 6f 72 57 72 69 74 65 72 41 70 70 65  teriorWriterAppe
36e08 6e 64 28 70 57 72 69 74 65 72 2d 3e 70 61 72 65  nd(pWriter->pare
36e09 6e 74 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20  ntWriter,.      
36e0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36e0b 20 20 20 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2e 70     block->term.p
36e0c 44 61 74 61 2c 20 62 6c 6f 63 6b 2d 3e 74 65 72  Data, block->ter
36e0d 6d 2e 6e 44 61 74 61 2c 20 69 42 6c 6f 63 6b 69  m.nData, iBlocki
36e0e 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50 61  d);.  }..  /* Pa
36e0f 72 65 6e 74 20 6e 6f 64 65 20 67 65 74 73 20 74  rent node gets t
36e10 68 65 20 63 68 61 6e 63 65 20 74 6f 20 62 65 20  he chance to be 
36e11 74 68 65 20 72 6f 6f 74 2e 20 2a 2f 0a 20 20 72  the root. */.  r
36e12 65 74 75 72 6e 20 69 6e 74 65 72 69 6f 72 57 72  eturn interiorWr
36e13 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 76 2c 20  iterRootInfo(v, 
36e14 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57  pWriter->parentW
36e15 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20  riter,.         
36e16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36e17 20 20 20 20 20 20 20 70 70 52 6f 6f 74 49 6e 66         ppRootInf
36e18 6f 2c 20 70 6e 52 6f 6f 74 49 6e 66 6f 2c 20 70  o, pnRootInfo, p
36e19 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 7d 0a  iEndBlockid);.}.
36e1a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
36e1b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36e1e 2a 2a 2f 0a 2f 2a 20 49 6e 74 65 72 69 6f 72 52  **/./* InteriorR
36e1f 65 61 64 65 72 20 69 73 20 75 73 65 64 20 74 6f  eader is used to
36e20 20 72 65 61 64 20 6f 66 66 20 74 68 65 20 64 61   read off the da
36e21 74 61 20 66 72 6f 6d 20 61 6e 20 69 6e 74 65 72  ta from an inter
36e22 69 6f 72 20 6e 6f 64 65 0a 2a 2a 20 28 73 65 65  ior node.** (see
36e23 20 63 6f 6d 6d 65 6e 74 20 61 74 20 74 6f 70 20   comment at top 
36e24 6f 66 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20  of file for the 
36e25 66 6f 72 6d 61 74 29 2e 0a 2a 2f 0a 74 79 70 65  format)..*/.type
36e26 64 65 66 20 73 74 72 75 63 74 20 49 6e 74 65 72  def struct Inter
36e27 69 6f 72 52 65 61 64 65 72 20 7b 0a 20 20 63 6f  iorReader {.  co
36e28 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 3b  nst char *pData;
36e29 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a 0a 20  .  int nData;.. 
36e2a 20 44 61 74 61 42 75 66 66 65 72 20 74 65 72 6d   DataBuffer term
36e2b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 72  ;          /* pr
36e2c 65 76 69 6f 75 73 20 74 65 72 6d 2c 20 66 6f 72  evious term, for
36e2d 20 64 65 63 6f 64 69 6e 67 20 74 65 72 6d 20 64   decoding term d
36e2e 65 6c 74 61 2e 20 2a 2f 0a 0a 20 20 73 71 6c 69  elta. */..  sqli
36e2f 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69  te_int64 iBlocki
36e30 64 3b 0a 7d 20 49 6e 74 65 72 69 6f 72 52 65 61  d;.} InteriorRea
36e31 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69  der;..static voi
36e32 64 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72  d interiorReader
36e33 44 65 73 74 72 6f 79 28 49 6e 74 65 72 69 6f 72  Destroy(Interior
36e34 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
36e35 7b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  {.  dataBufferDe
36e36 73 74 72 6f 79 28 26 70 52 65 61 64 65 72 2d 3e  stroy(&pReader->
36e37 74 65 72 6d 29 3b 0a 20 20 53 43 52 41 4d 42 4c  term);.  SCRAMBL
36e38 45 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 2f  E(pReader);.}../
36e39 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68  * TODO(shess) Th
36e3a 65 20 61 73 73 65 72 74 69 6f 6e 73 20 61 72 65  e assertions are
36e3b 20 67 72 65 61 74 2c 20 62 75 74 20 77 68 61 74   great, but what
36e3c 20 69 66 20 77 65 27 72 65 20 69 6e 20 4e 44 45   if we're in NDE
36e3d 42 55 47 0a 2a 2a 20 61 6e 64 20 74 68 65 20 62  BUG.** and the b
36e3e 6c 6f 62 20 69 73 20 65 6d 70 74 79 20 6f 72 20  lob is empty or 
36e3f 6f 74 68 65 72 77 69 73 65 20 63 6f 6e 74 61 69  otherwise contai
36e40 6e 73 20 73 75 73 70 65 63 74 20 64 61 74 61 3f  ns suspect data?
36e41 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
36e42 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 49 6e  interiorReaderIn
36e43 69 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  it(const char *p
36e44 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c  Data, int nData,
36e45 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
36e46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36e47 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a  InteriorReader *
36e48 70 52 65 61 64 65 72 29 7b 0a 20 20 69 6e 74 20  pReader){.  int 
36e49 6e 2c 20 6e 54 65 72 6d 3b 0a 0a 20 20 2f 2a 20  n, nTerm;..  /* 
36e4a 52 65 71 75 69 72 65 20 61 74 20 6c 65 61 73 74  Require at least
36e4b 20 74 68 65 20 6c 65 61 64 69 6e 67 20 66 6c 61   the leading fla
36e4c 67 20 62 79 74 65 20 2a 2f 0a 20 20 61 73 73 65  g byte */.  asse
36e4d 72 74 28 20 6e 44 61 74 61 3e 30 20 29 3b 0a 20  rt( nData>0 );. 
36e4e 20 61 73 73 65 72 74 28 20 70 44 61 74 61 5b 30   assert( pData[0
36e4f 5d 21 3d 27 5c 30 27 20 29 3b 0a 0a 20 20 43 4c  ]!='\0' );..  CL
36e50 45 41 52 28 70 52 65 61 64 65 72 29 3b 0a 0a 20  EAR(pReader);.. 
36e51 20 2f 2a 20 44 65 63 6f 64 65 20 74 68 65 20 62   /* Decode the b
36e52 61 73 65 20 62 6c 6f 63 6b 69 64 2c 20 61 6e 64  ase blockid, and
36e53 20 73 65 74 20 74 68 65 20 63 75 72 73 6f 72 20   set the cursor 
36e54 74 6f 20 74 68 65 20 66 69 72 73 74 20 74 65 72  to the first ter
36e55 6d 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33  m. */.  n = fts3
36e56 47 65 74 56 61 72 69 6e 74 28 70 44 61 74 61 2b  GetVarint(pData+
36e57 31 2c 20 26 70 52 65 61 64 65 72 2d 3e 69 42 6c  1, &pReader->iBl
36e58 6f 63 6b 69 64 29 3b 0a 20 20 61 73 73 65 72 74  ockid);.  assert
36e59 28 20 31 2b 6e 3c 3d 6e 44 61 74 61 20 29 3b 0a  ( 1+n<=nData );.
36e5a 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61    pReader->pData
36e5b 20 3d 20 70 44 61 74 61 2b 31 2b 6e 3b 0a 20 20   = pData+1+n;.  
36e5c 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d  pReader->nData =
36e5d 20 6e 44 61 74 61 2d 28 31 2b 6e 29 3b 0a 0a 20   nData-(1+n);.. 
36e5e 20 2f 2a 20 41 20 73 69 6e 67 6c 65 2d 63 68 69   /* A single-chi
36e5f 6c 64 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  ld interior node
36e60 20 28 73 75 63 68 20 61 73 20 77 68 65 6e 20 61   (such as when a
36e61 20 6c 65 61 66 20 6e 6f 64 65 20 77 61 73 20 74   leaf node was t
36e62 6f 6f 0a 20 20 2a 2a 20 6c 61 72 67 65 20 66 6f  oo.  ** large fo
36e63 72 20 74 68 65 20 73 65 67 6d 65 6e 74 20 64 69  r the segment di
36e64 72 65 63 74 6f 72 79 29 20 77 6f 6e 27 74 20 68  rectory) won't h
36e65 61 76 65 20 61 6e 79 20 74 65 72 6d 73 2e 0a 20  ave any terms.. 
36e66 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 64   ** Otherwise, d
36e67 65 63 6f 64 65 20 74 68 65 20 66 69 72 73 74 20  ecode the first 
36e68 74 65 72 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  term..  */.  if(
36e69 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 3d   pReader->nData=
36e6a 3d 30 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75  =0 ){.    dataBu
36e6b 66 66 65 72 49 6e 69 74 28 26 70 52 65 61 64 65  fferInit(&pReade
36e6c 72 2d 3e 74 65 72 6d 2c 20 30 29 3b 0a 20 20 7d  r->term, 0);.  }
36e6d 65 6c 73 65 7b 0a 20 20 20 20 6e 20 3d 20 66 74  else{.    n = ft
36e6e 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52  s3GetVarint32(pR
36e6f 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 6e  eader->pData, &n
36e70 54 65 72 6d 29 3b 0a 20 20 20 20 64 61 74 61 42  Term);.    dataB
36e71 75 66 66 65 72 49 6e 69 74 28 26 70 52 65 61 64  ufferInit(&pRead
36e72 65 72 2d 3e 74 65 72 6d 2c 20 6e 54 65 72 6d 29  er->term, nTerm)
36e73 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  ;.    dataBuffer
36e74 52 65 70 6c 61 63 65 28 26 70 52 65 61 64 65 72  Replace(&pReader
36e75 2d 3e 74 65 72 6d 2c 20 70 52 65 61 64 65 72 2d  ->term, pReader-
36e76 3e 70 44 61 74 61 2b 6e 2c 20 6e 54 65 72 6d 29  >pData+n, nTerm)
36e77 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b  ;.    assert( n+
36e78 6e 54 65 72 6d 3c 3d 70 52 65 61 64 65 72 2d 3e  nTerm<=pReader->
36e79 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 70 52 65  nData );.    pRe
36e7a 61 64 65 72 2d 3e 70 44 61 74 61 20 2b 3d 20 6e  ader->pData += n
36e7b 2b 6e 54 65 72 6d 3b 0a 20 20 20 20 70 52 65 61  +nTerm;.    pRea
36e7c 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b  der->nData -= n+
36e7d 6e 54 65 72 6d 3b 0a 20 20 7d 0a 7d 0a 0a 73 74  nTerm;.  }.}..st
36e7e 61 74 69 63 20 69 6e 74 20 69 6e 74 65 72 69 6f  atic int interio
36e7f 72 52 65 61 64 65 72 41 74 45 6e 64 28 49 6e 74  rReaderAtEnd(Int
36e80 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70 52 65  eriorReader *pRe
36e81 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ader){.  return 
36e82 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44  pReader->term.nD
36e83 61 74 61 3d 3d 30 3b 0a 7d 0a 0a 73 74 61 74 69  ata==0;.}..stati
36e84 63 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  c sqlite_int64 i
36e85 6e 74 65 72 69 6f 72 52 65 61 64 65 72 43 75 72  nteriorReaderCur
36e86 72 65 6e 74 42 6c 6f 63 6b 69 64 28 49 6e 74 65  rentBlockid(Inte
36e87 72 69 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61  riorReader *pRea
36e88 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  der){.  return p
36e89 52 65 61 64 65 72 2d 3e 69 42 6c 6f 63 6b 69 64  Reader->iBlockid
36e8a 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
36e8b 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 54 65  interiorReaderTe
36e8c 72 6d 42 79 74 65 73 28 49 6e 74 65 72 69 6f 72  rmBytes(Interior
36e8d 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
36e8e 7b 0a 20 20 61 73 73 65 72 74 28 20 21 69 6e 74  {.  assert( !int
36e8f 65 72 69 6f 72 52 65 61 64 65 72 41 74 45 6e 64  eriorReaderAtEnd
36e90 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72  (pReader) );.  r
36e91 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 74  eturn pReader->t
36e92 65 72 6d 2e 6e 44 61 74 61 3b 0a 7d 0a 73 74 61  erm.nData;.}.sta
36e93 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
36e94 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 54 65  interiorReaderTe
36e95 72 6d 28 49 6e 74 65 72 69 6f 72 52 65 61 64 65  rm(InteriorReade
36e96 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61  r *pReader){.  a
36e97 73 73 65 72 74 28 20 21 69 6e 74 65 72 69 6f 72  ssert( !interior
36e98 52 65 61 64 65 72 41 74 45 6e 64 28 70 52 65 61  ReaderAtEnd(pRea
36e99 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  der) );.  return
36e9a 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 70   pReader->term.p
36e9b 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 20 53 74 65 70  Data;.}../* Step
36e9c 20 66 6f 72 77 61 72 64 20 74 6f 20 74 68 65 20   forward to the 
36e9d 6e 65 78 74 20 74 65 72 6d 20 69 6e 20 74 68 65  next term in the
36e9e 20 6e 6f 64 65 2e 20 2a 2f 0a 73 74 61 74 69 63   node. */.static
36e9f 20 76 6f 69 64 20 69 6e 74 65 72 69 6f 72 52 65   void interiorRe
36ea0 61 64 65 72 53 74 65 70 28 49 6e 74 65 72 69 6f  aderStep(Interio
36ea1 72 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  rReader *pReader
36ea2 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 69 6e  ){.  assert( !in
36ea3 74 65 72 69 6f 72 52 65 61 64 65 72 41 74 45 6e  teriorReaderAtEn
36ea4 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 0a 20  d(pReader) );.. 
36ea5 20 2f 2a 20 49 66 20 74 68 65 20 6c 61 73 74 20   /* If the last 
36ea6 74 65 72 6d 20 68 61 73 20 62 65 65 6e 20 72 65  term has been re
36ea7 61 64 2c 20 73 69 67 6e 61 6c 20 65 6f 66 2c 20  ad, signal eof, 
36ea8 65 6c 73 65 20 63 6f 6e 73 74 72 75 63 74 20 74  else construct t
36ea9 68 65 0a 20 20 2a 2a 20 6e 65 78 74 20 74 65 72  he.  ** next ter
36eaa 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 52  m..  */.  if( pR
36eab 65 61 64 65 72 2d 3e 6e 44 61 74 61 3d 3d 30 20  eader->nData==0 
36eac 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65  ){.    dataBuffe
36ead 72 52 65 73 65 74 28 26 70 52 65 61 64 65 72 2d  rReset(&pReader-
36eae 3e 74 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b  >term);.  }else{
36eaf 0a 20 20 20 20 69 6e 74 20 6e 2c 20 6e 50 72 65  .    int n, nPre
36eb0 66 69 78 2c 20 6e 53 75 66 66 69 78 3b 0a 0a 20  fix, nSuffix;.. 
36eb1 20 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61     n = fts3GetVa
36eb2 72 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e  rint32(pReader->
36eb3 70 44 61 74 61 2c 20 26 6e 50 72 65 66 69 78 29  pData, &nPrefix)
36eb4 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47  ;.    n += fts3G
36eb5 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64  etVarint32(pRead
36eb6 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 6e 53  er->pData+n, &nS
36eb7 75 66 66 69 78 29 3b 0a 0a 20 20 20 20 2f 2a 20  uffix);..    /* 
36eb8 54 72 75 6e 63 61 74 65 20 74 68 65 20 63 75 72  Truncate the cur
36eb9 72 65 6e 74 20 74 65 72 6d 20 61 6e 64 20 61 70  rent term and ap
36eba 70 65 6e 64 20 73 75 66 66 69 78 20 64 61 74 61  pend suffix data
36ebb 2e 20 2a 2f 0a 20 20 20 20 70 52 65 61 64 65 72  . */.    pReader
36ebc 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20 3d 20 6e  ->term.nData = n
36ebd 50 72 65 66 69 78 3b 0a 20 20 20 20 64 61 74 61  Prefix;.    data
36ebe 42 75 66 66 65 72 41 70 70 65 6e 64 28 26 70 52  BufferAppend(&pR
36ebf 65 61 64 65 72 2d 3e 74 65 72 6d 2c 20 70 52 65  eader->term, pRe
36ec0 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 6e  ader->pData+n, n
36ec1 53 75 66 66 69 78 29 3b 0a 0a 20 20 20 20 61 73  Suffix);..    as
36ec2 73 65 72 74 28 20 6e 2b 6e 53 75 66 66 69 78 3c  sert( n+nSuffix<
36ec3 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20  =pReader->nData 
36ec4 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  );.    pReader->
36ec5 70 44 61 74 61 20 2b 3d 20 6e 2b 6e 53 75 66 66  pData += n+nSuff
36ec6 69 78 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d  ix;.    pReader-
36ec7 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b 6e 53 75 66  >nData -= n+nSuf
36ec8 66 69 78 3b 0a 20 20 7d 0a 20 20 70 52 65 61 64  fix;.  }.  pRead
36ec9 65 72 2d 3e 69 42 6c 6f 63 6b 69 64 2b 2b 3b 0a  er->iBlockid++;.
36eca 7d 0a 0a 2f 2a 20 43 6f 6d 70 61 72 65 20 74 68  }../* Compare th
36ecb 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d 20 74  e current term t
36ecc 6f 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 2c 20  o pTerm[nTerm], 
36ecd 72 65 74 75 72 6e 69 6e 67 20 73 74 72 63 6d 70  returning strcmp
36ece 2d 73 74 79 6c 65 0a 2a 2a 20 72 65 73 75 6c 74  -style.** result
36ecf 73 2e 20 20 49 66 20 69 73 50 72 65 66 69 78 2c  s.  If isPrefix,
36ed0 20 65 71 75 61 6c 69 74 79 20 6d 65 61 6e 73 20   equality means 
36ed1 65 71 75 61 6c 20 74 68 72 6f 75 67 68 20 6e 54  equal through nT
36ed2 65 72 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74  erm bytes..*/.st
36ed3 61 74 69 63 20 69 6e 74 20 69 6e 74 65 72 69 6f  atic int interio
36ed4 72 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 49  rReaderTermCmp(I
36ed5 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70  nteriorReader *p
36ed6 52 65 61 64 65 72 2c 0a 20 20 20 20 20 20 20 20  Reader,.        
36ed7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36ed8 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
36ed9 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20  har *pTerm, int 
36eda 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65  nTerm, int isPre
36edb 66 69 78 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68  fix){.  const ch
36edc 61 72 20 2a 70 52 65 61 64 65 72 54 65 72 6d 20  ar *pReaderTerm 
36edd 3d 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72  = interiorReader
36ede 54 65 72 6d 28 70 52 65 61 64 65 72 29 3b 0a 20  Term(pReader);. 
36edf 20 69 6e 74 20 6e 52 65 61 64 65 72 54 65 72 6d   int nReaderTerm
36ee0 20 3d 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65   = interiorReade
36ee1 72 54 65 72 6d 42 79 74 65 73 28 70 52 65 61 64  rTermBytes(pRead
36ee2 65 72 29 3b 0a 20 20 69 6e 74 20 63 2c 20 6e 20  er);.  int c, n 
36ee3 3d 20 6e 52 65 61 64 65 72 54 65 72 6d 3c 6e 54  = nReaderTerm<nT
36ee4 65 72 6d 20 3f 20 6e 52 65 61 64 65 72 54 65 72  erm ? nReaderTer
36ee5 6d 20 3a 20 6e 54 65 72 6d 3b 0a 0a 20 20 69 66  m : nTerm;..  if
36ee6 28 20 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  ( n==0 ){.    if
36ee7 28 20 6e 52 65 61 64 65 72 54 65 72 6d 3e 30 20  ( nReaderTerm>0 
36ee8 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20  ) return -1;.   
36ee9 20 69 66 28 20 6e 54 65 72 6d 3e 30 20 29 20 72   if( nTerm>0 ) r
36eea 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 72 65 74  eturn 1;.    ret
36eeb 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 63 20  urn 0;.  }..  c 
36eec 3d 20 6d 65 6d 63 6d 70 28 70 52 65 61 64 65 72  = memcmp(pReader
36eed 54 65 72 6d 2c 20 70 54 65 72 6d 2c 20 6e 29 3b  Term, pTerm, n);
36eee 0a 20 20 69 66 28 20 63 21 3d 30 20 29 20 72 65  .  if( c!=0 ) re
36eef 74 75 72 6e 20 63 3b 0a 20 20 69 66 28 20 69 73  turn c;.  if( is
36ef0 50 72 65 66 69 78 20 26 26 20 6e 3d 3d 6e 54 65  Prefix && n==nTe
36ef1 72 6d 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  rm ) return 0;. 
36ef2 20 72 65 74 75 72 6e 20 6e 52 65 61 64 65 72 54   return nReaderT
36ef3 65 72 6d 20 2d 20 6e 54 65 72 6d 3b 0a 7d 0a 0a  erm - nTerm;.}..
36ef4 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
36ef5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36ef6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36ef7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
36ef8 2a 2f 0a 2f 2a 20 4c 65 61 66 57 72 69 74 65 72  */./* LeafWriter
36ef9 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6c 6c   is used to coll
36efa 65 63 74 20 74 65 72 6d 73 20 61 6e 64 20 61 73  ect terms and as
36efb 73 6f 63 69 61 74 65 64 20 64 6f 63 6c 69 73 74  sociated doclist
36efc 20 64 61 74 61 0a 2a 2a 20 69 6e 74 6f 20 6c 65   data.** into le
36efd 61 66 20 62 6c 6f 63 6b 73 20 69 6e 20 25 5f 73  af blocks in %_s
36efe 65 67 6d 65 6e 74 73 20 28 73 65 65 20 74 6f 70  egments (see top
36eff 20 6f 66 20 66 69 6c 65 20 66 6f 72 20 66 6f 72   of file for for
36f00 6d 61 74 20 69 6e 66 6f 29 2e 0a 2a 2a 20 45 78  mat info)..** Ex
36f01 70 65 63 74 65 64 20 75 73 61 67 65 20 69 73 3a  pected usage is:
36f02 0a 2a 2a 0a 2a 2a 20 4c 65 61 66 57 72 69 74 65  .**.** LeafWrite
36f03 72 20 77 72 69 74 65 72 3b 0a 2a 2a 20 6c 65 61  r writer;.** lea
36f04 66 57 72 69 74 65 72 49 6e 69 74 28 30 2c 20 30  fWriterInit(0, 0
36f05 2c 20 26 77 72 69 74 65 72 29 3b 0a 2a 2a 20 77  , &writer);.** w
36f06 68 69 6c 65 28 20 73 6f 72 74 65 64 5f 74 65 72  hile( sorted_ter
36f07 6d 73 5f 6c 65 66 74 5f 74 6f 5f 70 72 6f 63 65  ms_left_to_proce
36f08 73 73 20 29 7b 0a 2a 2a 20 20 20 2f 2f 20 64 61  ss ){.**   // da
36f09 74 61 20 69 73 20 64 6f 63 6c 69 73 74 20 64 61  ta is doclist da
36f0a 74 61 20 66 6f 72 20 74 68 61 74 20 74 65 72 6d  ta for that term
36f0b 2e 0a 2a 2a 20 20 20 72 63 20 3d 20 6c 65 61 66  ..**   rc = leaf
36f0c 57 72 69 74 65 72 53 74 65 70 28 76 2c 20 26 77  WriterStep(v, &w
36f0d 72 69 74 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54  riter, pTerm, nT
36f0e 65 72 6d 2c 20 70 44 61 74 61 2c 20 6e 44 61 74  erm, pData, nDat
36f0f 61 29 3b 0a 2a 2a 20 20 20 69 66 28 20 72 63 21  a);.**   if( rc!
36f10 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
36f11 6f 20 65 72 72 3b 0a 2a 2a 20 7d 0a 2a 2a 20 72  o err;.** }.** r
36f12 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 46 69  c = leafWriterFi
36f13 6e 61 6c 69 7a 65 28 76 2c 20 26 77 72 69 74 65  nalize(v, &write
36f14 72 29 3b 0a 2a 2a 65 72 72 3a 0a 2a 2a 20 6c 65  r);.**err:.** le
36f15 61 66 57 72 69 74 65 72 44 65 73 74 72 6f 79 28  afWriterDestroy(
36f16 26 77 72 69 74 65 72 29 3b 0a 2a 2a 20 72 65 74  &writer);.** ret
36f17 75 72 6e 20 72 63 3b 0a 2a 2a 0a 2a 2a 20 6c 65  urn rc;.**.** le
36f18 61 66 57 72 69 74 65 72 53 74 65 70 28 29 20 6d  afWriterStep() m
36f19 61 79 20 77 72 69 74 65 20 61 20 63 6f 6c 6c 65  ay write a colle
36f1a 63 74 65 64 20 6c 65 61 66 20 6f 75 74 20 74 6f  cted leaf out to
36f1b 20 25 5f 73 65 67 6d 65 6e 74 73 2e 0a 2a 2a 20   %_segments..** 
36f1c 6c 65 61 66 57 72 69 74 65 72 46 69 6e 61 6c 69  leafWriterFinali
36f1d 7a 65 28 29 20 66 69 6e 69 73 68 65 73 20 77 72  ze() finishes wr
36f1e 69 74 69 6e 67 20 61 6e 79 20 62 75 66 66 65 72  iting any buffer
36f1f 65 64 20 64 61 74 61 20 61 6e 64 20 73 74 6f 72  ed data and stor
36f20 65 73 0a 2a 2a 20 61 20 72 6f 6f 74 20 6e 6f 64  es.** a root nod
36f21 65 20 69 6e 20 25 5f 73 65 67 64 69 72 2e 20 20  e in %_segdir.  
36f22 6c 65 61 66 57 72 69 74 65 72 44 65 73 74 72 6f  leafWriterDestro
36f23 79 28 29 20 66 72 65 65 73 20 61 6c 6c 20 62 75  y() frees all bu
36f24 66 66 65 72 73 20 61 6e 64 0a 2a 2a 20 49 6e 74  ffers and.** Int
36f25 65 72 69 6f 72 57 72 69 74 65 72 73 20 61 6c 6c  eriorWriters all
36f26 6f 63 61 74 65 64 20 61 73 20 70 61 72 74 20 6f  ocated as part o
36f27 66 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 73  f writing this s
36f28 65 67 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 4f  egment..**.** TO
36f29 44 4f 28 73 68 65 73 73 29 20 44 6f 63 75 6d 65  DO(shess) Docume
36f2a 6e 74 20 6c 65 61 66 57 72 69 74 65 72 53 74 65  nt leafWriterSte
36f2b 70 4d 65 72 67 65 28 29 2e 0a 2a 2f 0a 0a 2f 2a  pMerge()..*/../*
36f2c 20 50 75 74 20 74 65 72 6d 73 20 77 69 74 68 20   Put terms with 
36f2d 64 61 74 61 20 74 68 69 73 20 62 69 67 20 69 6e  data this big in
36f2e 20 74 68 65 69 72 20 6f 77 6e 20 62 6c 6f 63 6b   their own block
36f2f 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 54 41  . */.#define STA
36f30 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 31 30 32 34  NDALONE_MIN 1024
36f31 0a 0a 2f 2a 20 4b 65 65 70 20 6c 65 61 66 20 62  ../* Keep leaf b
36f32 6c 6f 63 6b 73 20 62 65 6c 6f 77 20 74 68 69 73  locks below this
36f33 20 73 69 7a 65 2e 20 2a 2f 0a 23 64 65 66 69 6e   size. */.#defin
36f34 65 20 4c 45 41 46 5f 4d 41 58 20 32 30 34 38 0a  e LEAF_MAX 2048.
36f35 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
36f36 4c 65 61 66 57 72 69 74 65 72 20 7b 0a 20 20 69  LeafWriter {.  i
36f37 6e 74 20 69 4c 65 76 65 6c 3b 0a 20 20 69 6e 74  nt iLevel;.  int
36f38 20 69 64 78 3b 0a 20 20 73 71 6c 69 74 65 5f 69   idx;.  sqlite_i
36f39 6e 74 36 34 20 69 53 74 61 72 74 42 6c 6f 63 6b  nt64 iStartBlock
36f3a 69 64 3b 20 20 20 20 20 2f 2a 20 6e 65 65 64 65  id;     /* neede
36f3b 64 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20  d to create the 
36f3c 72 6f 6f 74 20 69 6e 66 6f 20 2a 2f 0a 20 20 73  root info */.  s
36f3d 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64  qlite_int64 iEnd
36f3e 42 6c 6f 63 6b 69 64 3b 20 20 20 20 20 20 20 2f  Blockid;       /
36f3f 2a 20 77 68 65 6e 20 77 65 27 72 65 20 64 6f 6e  * when we're don
36f40 65 20 77 72 69 74 69 6e 67 2e 20 2a 2f 0a 0a 20  e writing. */.. 
36f41 20 44 61 74 61 42 75 66 66 65 72 20 74 65 72 6d   DataBuffer term
36f42 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
36f43 20 2f 2a 20 70 72 65 76 69 6f 75 73 20 65 6e 63   /* previous enc
36f44 6f 64 65 64 20 74 65 72 6d 20 2a 2f 0a 20 20 44  oded term */.  D
36f45 61 74 61 42 75 66 66 65 72 20 64 61 74 61 3b 20  ataBuffer data; 
36f46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
36f47 2a 20 65 6e 63 6f 64 69 6e 67 20 62 75 66 66 65  * encoding buffe
36f48 72 20 2a 2f 0a 0a 20 20 2f 2a 20 62 79 74 65 73  r */..  /* bytes
36f49 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d 20 69   of first term i
36f4a 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 6f  n the current no
36f4b 64 65 20 77 68 69 63 68 20 64 69 73 74 69 6e 67  de which disting
36f4c 75 69 73 68 65 73 20 74 68 61 74 0a 20 20 2a 2a  uishes that.  **
36f4d 20 74 65 72 6d 20 66 72 6f 6d 20 74 68 65 20 6c   term from the l
36f4e 61 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ast term of the 
36f4f 70 72 65 76 69 6f 75 73 20 6e 6f 64 65 2e 0a 20  previous node.. 
36f50 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 44   */.  int nTermD
36f51 69 73 74 69 6e 63 74 3b 0a 0a 20 20 49 6e 74 65  istinct;..  Inte
36f52 72 69 6f 72 57 72 69 74 65 72 20 70 61 72 65 6e  riorWriter paren
36f53 74 57 72 69 74 65 72 3b 20 20 20 20 2f 2a 20 69  tWriter;    /* i
36f54 66 20 77 65 20 6f 76 65 72 66 6c 6f 77 20 2a 2f  f we overflow */
36f55 0a 20 20 69 6e 74 20 68 61 73 5f 70 61 72 65 6e  .  int has_paren
36f56 74 3b 0a 7d 20 4c 65 61 66 57 72 69 74 65 72 3b  t;.} LeafWriter;
36f57 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65  ..static void le
36f58 61 66 57 72 69 74 65 72 49 6e 69 74 28 69 6e 74  afWriterInit(int
36f59 20 69 4c 65 76 65 6c 2c 20 69 6e 74 20 69 64 78   iLevel, int idx
36f5a 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57  , LeafWriter *pW
36f5b 72 69 74 65 72 29 7b 0a 20 20 43 4c 45 41 52 28  riter){.  CLEAR(
36f5c 70 57 72 69 74 65 72 29 3b 0a 20 20 70 57 72 69  pWriter);.  pWri
36f5d 74 65 72 2d 3e 69 4c 65 76 65 6c 20 3d 20 69 4c  ter->iLevel = iL
36f5e 65 76 65 6c 3b 0a 20 20 70 57 72 69 74 65 72 2d  evel;.  pWriter-
36f5f 3e 69 64 78 20 3d 20 69 64 78 3b 0a 0a 20 20 64  >idx = idx;..  d
36f60 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 70  ataBufferInit(&p
36f61 57 72 69 74 65 72 2d 3e 74 65 72 6d 2c 20 33 32  Writer->term, 32
36f62 29 3b 0a 0a 20 20 2f 2a 20 53 74 61 72 74 20 6f  );..  /* Start o
36f63 75 74 20 77 69 74 68 20 61 20 72 65 61 73 6f 6e  ut with a reason
36f64 61 62 6c 79 20 73 69 7a 65 64 20 62 6c 6f 63 6b  ably sized block
36f65 2c 20 74 68 6f 75 67 68 20 69 74 20 63 61 6e 20  , though it can 
36f66 67 72 6f 77 2e 20 2a 2f 0a 20 20 64 61 74 61 42  grow. */.  dataB
36f67 75 66 66 65 72 49 6e 69 74 28 26 70 57 72 69 74  ufferInit(&pWrit
36f68 65 72 2d 3e 64 61 74 61 2c 20 4c 45 41 46 5f 4d  er->data, LEAF_M
36f69 41 58 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  AX);.}..#ifndef 
36f6a 4e 44 45 42 55 47 0a 2f 2a 20 56 65 72 69 66 79  NDEBUG./* Verify
36f6b 20 74 68 61 74 20 74 68 65 20 64 61 74 61 20 69   that the data i
36f6c 73 20 72 65 61 64 61 62 6c 65 20 61 73 20 61 20  s readable as a 
36f6d 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a 73 74  leaf node. */.st
36f6e 61 74 69 63 20 76 6f 69 64 20 6c 65 61 66 4e 6f  atic void leafNo
36f6f 64 65 56 61 6c 69 64 61 74 65 28 63 6f 6e 73 74  deValidate(const
36f70 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e   char *pData, in
36f71 74 20 6e 44 61 74 61 29 7b 0a 20 20 69 6e 74 20  t nData){.  int 
36f72 6e 2c 20 69 44 75 6d 6d 79 3b 0a 0a 20 20 69 66  n, iDummy;..  if
36f73 28 20 6e 44 61 74 61 3d 3d 30 20 29 20 72 65 74  ( nData==0 ) ret
36f74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  urn;.  assert( n
36f75 44 61 74 61 3e 30 20 29 3b 0a 20 20 61 73 73 65  Data>0 );.  asse
36f76 72 74 28 20 70 44 61 74 61 21 3d 30 20 29 3b 0a  rt( pData!=0 );.
36f77 20 20 61 73 73 65 72 74 28 20 70 44 61 74 61 2b    assert( pData+
36f78 6e 44 61 74 61 3e 70 44 61 74 61 20 29 3b 0a 0a  nData>pData );..
36f79 20 20 2f 2a 20 4d 75 73 74 20 6c 65 61 64 20 77    /* Must lead w
36f7a 69 74 68 20 61 20 76 61 72 69 6e 74 28 30 29 20  ith a varint(0) 
36f7b 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65 74  */.  n = fts3Get
36f7c 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c 20  Varint32(pData, 
36f7d 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73 73 65  &iDummy);.  asse
36f7e 72 74 28 20 69 44 75 6d 6d 79 3d 3d 30 20 29 3b  rt( iDummy==0 );
36f7f 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29  .  assert( n>0 )
36f80 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3c 6e 44  ;.  assert( n<nD
36f81 61 74 61 20 29 3b 0a 20 20 70 44 61 74 61 20 2b  ata );.  pData +
36f82 3d 20 6e 3b 0a 20 20 6e 44 61 74 61 20 2d 3d 20  = n;.  nData -= 
36f83 6e 3b 0a 0a 20 20 2f 2a 20 4c 65 61 64 69 6e 67  n;..  /* Leading
36f84 20 74 65 72 6d 20 6c 65 6e 67 74 68 20 61 6e 64   term length and
36f85 20 64 61 74 61 20 6d 75 73 74 20 66 69 74 20 69   data must fit i
36f86 6e 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 6e  n buffer. */.  n
36f87 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
36f88 33 32 28 70 44 61 74 61 2c 20 26 69 44 75 6d 6d  32(pData, &iDumm
36f89 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e  y);.  assert( n>
36f8a 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  0 );.  assert( i
36f8b 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 61 73 73  Dummy>0 );.  ass
36f8c 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3e 30 20  ert( n+iDummy>0 
36f8d 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 2b 69  );.  assert( n+i
36f8e 44 75 6d 6d 79 3c 6e 44 61 74 61 20 29 3b 0a 20  Dummy<nData );. 
36f8f 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d   pData += n+iDum
36f90 6d 79 3b 0a 20 20 6e 44 61 74 61 20 2d 3d 20 6e  my;.  nData -= n
36f91 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20 2f 2a 20 4c  +iDummy;..  /* L
36f92 65 61 64 69 6e 67 20 74 65 72 6d 27 73 20 64 6f  eading term's do
36f93 63 6c 69 73 74 20 6c 65 6e 67 74 68 20 61 6e 64  clist length and
36f94 20 64 61 74 61 20 6d 75 73 74 20 66 69 74 2e 20   data must fit. 
36f95 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65 74  */.  n = fts3Get
36f96 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c 20  Varint32(pData, 
36f97 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73 73 65  &iDummy);.  asse
36f98 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73  rt( n>0 );.  ass
36f99 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b  ert( iDummy>0 );
36f9a 0a 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75  .  assert( n+iDu
36f9b 6d 6d 79 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  mmy>0 );.  asser
36f9c 74 28 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e 44 61  t( n+iDummy<=nDa
36f9d 74 61 20 29 3b 0a 20 20 41 53 53 45 52 54 5f 56  ta );.  ASSERT_V
36f9e 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 44 4c 5f  ALID_DOCLIST(DL_
36f9f 44 45 46 41 55 4c 54 2c 20 70 44 61 74 61 2b 6e  DEFAULT, pData+n
36fa0 2c 20 69 44 75 6d 6d 79 2c 20 4e 55 4c 4c 29 3b  , iDummy, NULL);
36fa1 0a 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44  .  pData += n+iD
36fa2 75 6d 6d 79 3b 0a 20 20 6e 44 61 74 61 20 2d 3d  ummy;.  nData -=
36fa3 20 6e 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20 2f 2a   n+iDummy;..  /*
36fa4 20 56 65 72 69 66 79 20 74 68 61 74 20 74 72 61   Verify that tra
36fa5 69 6c 69 6e 67 20 74 65 72 6d 73 20 61 6e 64 20  iling terms and 
36fa6 64 6f 63 6c 69 73 74 73 20 61 6c 73 6f 20 61 72  doclists also ar
36fa7 65 20 72 65 61 64 61 62 6c 65 2e 20 2a 2f 0a 20  e readable. */. 
36fa8 20 77 68 69 6c 65 28 20 6e 44 61 74 61 21 3d 30   while( nData!=0
36fa9 20 29 7b 0a 20 20 20 20 6e 20 3d 20 66 74 73 33   ){.    n = fts3
36faa 47 65 74 56 61 72 69 6e 74 33 32 28 70 44 61 74  GetVarint32(pDat
36fab 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20  a, &iDummy);.   
36fac 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a   assert( n>0 );.
36fad 20 20 20 20 61 73 73 65 72 74 28 20 69 44 75 6d      assert( iDum
36fae 6d 79 3e 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  my>=0 );.    ass
36faf 65 72 74 28 20 6e 3c 6e 44 61 74 61 20 29 3b 0a  ert( n<nData );.
36fb0 20 20 20 20 70 44 61 74 61 20 2b 3d 20 6e 3b 0a      pData += n;.
36fb1 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a      nData -= n;.
36fb2 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56      n = fts3GetV
36fb3 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26  arint32(pData, &
36fb4 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73  iDummy);.    ass
36fb5 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20  ert( n>0 );.    
36fb6 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79 3e 30  assert( iDummy>0
36fb7 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
36fb8 6e 2b 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20  n+iDummy>0 );.  
36fb9 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d    assert( n+iDum
36fba 6d 79 3c 6e 44 61 74 61 20 29 3b 0a 20 20 20 20  my<nData );.    
36fbb 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d  pData += n+iDumm
36fbc 79 3b 0a 20 20 20 20 6e 44 61 74 61 20 2d 3d 20  y;.    nData -= 
36fbd 6e 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20 20 20 6e  n+iDummy;..    n
36fbe 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
36fbf 33 32 28 70 44 61 74 61 2c 20 26 69 44 75 6d 6d  32(pData, &iDumm
36fc0 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  y);.    assert( 
36fc1 6e 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  n>0 );.    asser
36fc2 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20  t( iDummy>0 );. 
36fc3 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75     assert( n+iDu
36fc4 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20 61 73 73  mmy>0 );.    ass
36fc5 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e  ert( n+iDummy<=n
36fc6 44 61 74 61 20 29 3b 0a 20 20 20 20 41 53 53 45  Data );.    ASSE
36fc7 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53 54  RT_VALID_DOCLIST
36fc8 28 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70 44 61  (DL_DEFAULT, pDa
36fc9 74 61 2b 6e 2c 20 69 44 75 6d 6d 79 2c 20 4e 55  ta+n, iDummy, NU
36fca 4c 4c 29 3b 0a 20 20 20 20 70 44 61 74 61 20 2b  LL);.    pData +
36fcb 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20 20 20  = n+iDummy;.    
36fcc 6e 44 61 74 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d  nData -= n+iDumm
36fcd 79 3b 0a 20 20 7d 0a 7d 0a 23 64 65 66 69 6e 65  y;.  }.}.#define
36fce 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c 45   ASSERT_VALID_LE
36fcf 41 46 5f 4e 4f 44 45 28 70 2c 20 6e 29 20 6c 65  AF_NODE(p, n) le
36fd0 61 66 4e 6f 64 65 56 61 6c 69 64 61 74 65 28 70  afNodeValidate(p
36fd1 2c 20 6e 29 0a 23 65 6c 73 65 0a 23 64 65 66 69  , n).#else.#defi
36fd2 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f  ne ASSERT_VALID_
36fd3 4c 45 41 46 5f 4e 4f 44 45 28 70 2c 20 6e 29 20  LEAF_NODE(p, n) 
36fd4 61 73 73 65 72 74 28 20 31 20 29 0a 23 65 6e 64  assert( 1 ).#end
36fd5 69 66 0a 0a 2f 2a 20 46 6c 75 73 68 20 74 68 65  if../* Flush the
36fd6 20 63 75 72 72 65 6e 74 20 6c 65 61 66 20 6e 6f   current leaf no
36fd7 64 65 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73  de to %_segments
36fd8 2c 20 61 6e 64 20 61 64 64 69 6e 67 20 74 68 65  , and adding the
36fd9 20 72 65 73 75 6c 74 69 6e 67 0a 2a 2a 20 62 6c   resulting.** bl
36fda 6f 63 6b 69 64 20 61 6e 64 20 74 68 65 20 73 74  ockid and the st
36fdb 61 72 74 69 6e 67 20 74 65 72 6d 20 74 6f 20 74  arting term to t
36fdc 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  he interior node
36fdd 20 77 68 69 63 68 20 77 69 6c 6c 0a 2a 2a 20 63   which will.** c
36fde 6f 6e 74 61 69 6e 20 69 74 2e 0a 2a 2f 0a 73 74  ontain it..*/.st
36fdf 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57 72 69  atic int leafWri
36fe0 74 65 72 49 6e 74 65 72 6e 61 6c 46 6c 75 73 68  terInternalFlush
36fe1 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
36fe2 76 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70  v, LeafWriter *p
36fe3 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20  Writer,.        
36fe4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
36fe5 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69             int i
36fe6 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 29  Data, int nData)
36fe7 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  {.  sqlite_int64
36fe8 20 69 42 6c 6f 63 6b 69 64 20 3d 20 30 3b 0a 20   iBlockid = 0;. 
36fe9 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 53 74   const char *pSt
36fea 61 72 74 69 6e 67 54 65 72 6d 3b 0a 20 20 69 6e  artingTerm;.  in
36feb 74 20 6e 53 74 61 72 74 69 6e 67 54 65 72 6d 2c  t nStartingTerm,
36fec 20 72 63 2c 20 6e 3b 0a 0a 20 20 2f 2a 20 4d 75   rc, n;..  /* Mu
36fed 73 74 20 68 61 76 65 20 74 68 65 20 6c 65 61 64  st have the lead
36fee 69 6e 67 20 76 61 72 69 6e 74 28 30 29 20 66 6c  ing varint(0) fl
36fef 61 67 2c 20 70 6c 75 73 20 61 74 20 6c 65 61 73  ag, plus at leas
36ff0 74 20 73 6f 6d 65 0a 20 20 2a 2a 20 76 61 6c 69  t some.  ** vali
36ff1 64 2d 6c 6f 6f 6b 69 6e 67 20 64 61 74 61 2e 0a  d-looking data..
36ff2 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 6e    */.  assert( n
36ff3 44 61 74 61 3e 32 20 29 3b 0a 20 20 61 73 73 65  Data>2 );.  asse
36ff4 72 74 28 20 69 44 61 74 61 3e 3d 30 20 29 3b 0a  rt( iData>=0 );.
36ff5 20 20 61 73 73 65 72 74 28 20 69 44 61 74 61 2b    assert( iData+
36ff6 6e 44 61 74 61 3c 3d 70 57 72 69 74 65 72 2d 3e  nData<=pWriter->
36ff7 64 61 74 61 2e 6e 44 61 74 61 20 29 3b 0a 20 20  data.nData );.  
36ff8 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c 45 41  ASSERT_VALID_LEA
36ff9 46 5f 4e 4f 44 45 28 70 57 72 69 74 65 72 2d 3e  F_NODE(pWriter->
36ffa 64 61 74 61 2e 70 44 61 74 61 2b 69 44 61 74 61  data.pData+iData
36ffb 2c 20 6e 44 61 74 61 29 3b 0a 0a 20 20 72 63 20  , nData);..  rc 
36ffc 3d 20 62 6c 6f 63 6b 5f 69 6e 73 65 72 74 28 76  = block_insert(v
36ffd 2c 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e  , pWriter->data.
36ffe 70 44 61 74 61 2b 69 44 61 74 61 2c 20 6e 44 61  pData+iData, nDa
36fff 74 61 2c 20 26 69 42 6c 6f 63 6b 69 64 29 3b 0a  ta, &iBlockid);.
37000 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
37001 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
37002 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f 63  .  assert( iBloc
37003 6b 69 64 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20  kid!=0 );..  /* 
37004 52 65 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20  Reconstruct the 
37005 66 69 72 73 74 20 74 65 72 6d 20 69 6e 20 74 68  first term in th
37006 65 20 6c 65 61 66 20 66 6f 72 20 70 75 72 70 6f  e leaf for purpo
37007 73 65 73 20 6f 66 20 62 75 69 6c 64 69 6e 67 0a  ses of building.
37008 20 20 2a 2a 20 74 68 65 20 69 6e 74 65 72 69 6f    ** the interio
37009 72 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 6e  r node..  */.  n
3700a 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
3700b 33 32 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61  32(pWriter->data
3700c 2e 70 44 61 74 61 2b 69 44 61 74 61 2b 31 2c 20  .pData+iData+1, 
3700d 26 6e 53 74 61 72 74 69 6e 67 54 65 72 6d 29 3b  &nStartingTerm);
3700e 0a 20 20 70 53 74 61 72 74 69 6e 67 54 65 72 6d  .  pStartingTerm
3700f 20 3d 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61   = pWriter->data
37010 2e 70 44 61 74 61 2b 69 44 61 74 61 2b 31 2b 6e  .pData+iData+1+n
37011 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57 72 69  ;.  assert( pWri
37012 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3e  ter->data.nData>
37013 69 44 61 74 61 2b 31 2b 6e 2b 6e 53 74 61 72 74  iData+1+n+nStart
37014 69 6e 67 54 65 72 6d 20 29 3b 0a 20 20 61 73 73  ingTerm );.  ass
37015 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e 6e 54  ert( pWriter->nT
37016 65 72 6d 44 69 73 74 69 6e 63 74 3e 30 20 29 3b  ermDistinct>0 );
37017 0a 20 20 61 73 73 65 72 74 28 20 70 57 72 69 74  .  assert( pWrit
37018 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74 69 6e 63  er->nTermDistinc
37019 74 3c 3d 6e 53 74 61 72 74 69 6e 67 54 65 72 6d  t<=nStartingTerm
3701a 20 29 3b 0a 20 20 6e 53 74 61 72 74 69 6e 67 54   );.  nStartingT
3701b 65 72 6d 20 3d 20 70 57 72 69 74 65 72 2d 3e 6e  erm = pWriter->n
3701c 54 65 72 6d 44 69 73 74 69 6e 63 74 3b 0a 0a 20  TermDistinct;.. 
3701d 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e 68 61   if( pWriter->ha
3701e 73 5f 70 61 72 65 6e 74 20 29 7b 0a 20 20 20 20  s_parent ){.    
3701f 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 41 70  interiorWriterAp
37020 70 65 6e 64 28 26 70 57 72 69 74 65 72 2d 3e 70  pend(&pWriter->p
37021 61 72 65 6e 74 57 72 69 74 65 72 2c 0a 20 20 20  arentWriter,.   
37022 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37023 20 20 20 20 20 20 70 53 74 61 72 74 69 6e 67 54        pStartingT
37024 65 72 6d 2c 20 6e 53 74 61 72 74 69 6e 67 54 65  erm, nStartingTe
37025 72 6d 2c 20 69 42 6c 6f 63 6b 69 64 29 3b 0a 20  rm, iBlockid);. 
37026 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 65   }else{.    inte
37027 72 69 6f 72 57 72 69 74 65 72 49 6e 69 74 28 31  riorWriterInit(1
37028 2c 20 70 53 74 61 72 74 69 6e 67 54 65 72 6d 2c  , pStartingTerm,
37029 20 6e 53 74 61 72 74 69 6e 67 54 65 72 6d 2c 20   nStartingTerm, 
3702a 69 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20  iBlockid,.      
3702b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3702c 20 26 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e   &pWriter->paren
3702d 74 57 72 69 74 65 72 29 3b 0a 20 20 20 20 70 57  tWriter);.    pW
3702e 72 69 74 65 72 2d 3e 68 61 73 5f 70 61 72 65 6e  riter->has_paren
3702f 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  t = 1;.  }..  /*
37030 20 54 72 61 63 6b 20 74 68 65 20 73 70 61 6e 20   Track the span 
37031 6f 66 20 74 68 69 73 20 73 65 67 6d 65 6e 74 27  of this segment'
37032 73 20 6c 65 61 66 20 6e 6f 64 65 73 2e 20 2a 2f  s leaf nodes. */
37033 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e  .  if( pWriter->
37034 69 45 6e 64 42 6c 6f 63 6b 69 64 3d 3d 30 20 29  iEndBlockid==0 )
37035 7b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69  {.    pWriter->i
37036 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 70 57 72  EndBlockid = pWr
37037 69 74 65 72 2d 3e 69 53 74 61 72 74 42 6c 6f 63  iter->iStartBloc
37038 6b 69 64 20 3d 20 69 42 6c 6f 63 6b 69 64 3b 0a  kid = iBlockid;.
37039 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 57 72    }else{.    pWr
3703a 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69  iter->iEndBlocki
3703b 64 2b 2b 3b 0a 20 20 20 20 61 73 73 65 72 74 28  d++;.    assert(
3703c 20 69 42 6c 6f 63 6b 69 64 3d 3d 70 57 72 69 74   iBlockid==pWrit
3703d 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 20  er->iEndBlockid 
3703e 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  );.  }..  return
3703f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74   SQLITE_OK;.}.st
37040 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57 72 69  atic int leafWri
37041 74 65 72 46 6c 75 73 68 28 66 75 6c 6c 74 65 78  terFlush(fulltex
37042 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61 66 57  t_vtab *v, LeafW
37043 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b  riter *pWriter){
37044 0a 20 20 69 6e 74 20 72 63 20 3d 20 6c 65 61 66  .  int rc = leaf
37045 57 72 69 74 65 72 49 6e 74 65 72 6e 61 6c 46 6c  WriterInternalFl
37046 75 73 68 28 76 2c 20 70 57 72 69 74 65 72 2c 20  ush(v, pWriter, 
37047 30 2c 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61  0, pWriter->data
37048 2e 6e 44 61 74 61 29 3b 0a 20 20 69 66 28 20 72  .nData);.  if( r
37049 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
3704a 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20  eturn rc;..  /* 
3704b 52 65 2d 69 6e 69 74 69 61 6c 69 7a 65 20 74 68  Re-initialize th
3704c 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e  e output buffer.
3704d 20 2a 2f 0a 20 20 64 61 74 61 42 75 66 66 65 72   */.  dataBuffer
3704e 52 65 73 65 74 28 26 70 57 72 69 74 65 72 2d 3e  Reset(&pWriter->
3704f 64 61 74 61 29 3b 0a 0a 20 20 72 65 74 75 72 6e  data);..  return
37050 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
37051 2a 20 46 65 74 63 68 20 74 68 65 20 72 6f 6f 74  * Fetch the root
37052 20 69 6e 66 6f 20 66 6f 72 20 74 68 65 20 73 65   info for the se
37053 67 6d 65 6e 74 2e 20 20 49 66 20 74 68 65 20 65  gment.  If the e
37054 6e 74 69 72 65 20 6c 65 61 66 20 66 69 74 73 0a  ntire leaf fits.
37055 2a 2a 20 77 69 74 68 69 6e 20 52 4f 4f 54 5f 4d  ** within ROOT_M
37056 41 58 2c 20 74 68 65 6e 20 69 74 20 77 69 6c 6c  AX, then it will
37057 20 62 65 20 72 65 74 75 72 6e 65 64 20 64 69 72   be returned dir
37058 65 63 74 6c 79 2c 20 6f 74 68 65 72 77 69 73 65  ectly, otherwise
37059 20 69 74 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 66   it.** will be f
3705a 6c 75 73 68 65 64 20 61 6e 64 20 74 68 65 20 72  lushed and the r
3705b 6f 6f 74 20 69 6e 66 6f 20 77 69 6c 6c 20 62 65  oot info will be
3705c 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 74   returned from t
3705d 68 65 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e  he.** interior n
3705e 6f 64 65 2e 20 20 2a 70 69 45 6e 64 42 6c 6f 63  ode.  *piEndBloc
3705f 6b 69 64 20 69 73 20 73 65 74 20 74 6f 20 74 68  kid is set to th
37060 65 20 62 6c 6f 63 6b 69 64 20 6f 66 20 74 68 65  e blockid of the
37061 20 6c 61 73 74 0a 2a 2a 20 69 6e 74 65 72 69 6f   last.** interio
37062 72 20 6f 72 20 6c 65 61 66 20 6e 6f 64 65 20 77  r or leaf node w
37063 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 28  ritten to disk (
37064 30 20 69 66 20 6e 6f 6e 65 20 61 72 65 20 77 72  0 if none are wr
37065 69 74 74 65 6e 20 61 74 0a 2a 2a 20 61 6c 6c 29  itten at.** all)
37066 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
37067 6c 65 61 66 57 72 69 74 65 72 52 6f 6f 74 49 6e  leafWriterRootIn
37068 66 6f 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  fo(fulltext_vtab
37069 20 2a 76 2c 20 4c 65 61 66 57 72 69 74 65 72 20   *v, LeafWriter 
3706a 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20  *pWriter,.      
3706b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3706c 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 2a 70          char **p
3706d 70 52 6f 6f 74 49 6e 66 6f 2c 20 69 6e 74 20 2a  pRootInfo, int *
3706e 70 6e 52 6f 6f 74 49 6e 66 6f 2c 0a 20 20 20 20  pnRootInfo,.    
3706f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37070 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
37071 5f 69 6e 74 36 34 20 2a 70 69 45 6e 64 42 6c 6f  _int64 *piEndBlo
37072 63 6b 69 64 29 7b 0a 20 20 2f 2a 20 77 65 20 63  ckid){.  /* we c
37073 61 6e 20 66 69 74 20 74 68 65 20 73 65 67 6d 65  an fit the segme
37074 6e 74 20 65 6e 74 69 72 65 6c 79 20 69 6e 6c 69  nt entirely inli
37075 6e 65 20 2a 2f 0a 20 20 69 66 28 20 21 70 57 72  ne */.  if( !pWr
37076 69 74 65 72 2d 3e 68 61 73 5f 70 61 72 65 6e 74  iter->has_parent
37077 20 26 26 20 70 57 72 69 74 65 72 2d 3e 64 61 74   && pWriter->dat
37078 61 2e 6e 44 61 74 61 3c 52 4f 4f 54 5f 4d 41 58  a.nData<ROOT_MAX
37079 20 29 7b 0a 20 20 20 20 2a 70 70 52 6f 6f 74 49   ){.    *ppRootI
3707a 6e 66 6f 20 3d 20 70 57 72 69 74 65 72 2d 3e 64  nfo = pWriter->d
3707b 61 74 61 2e 70 44 61 74 61 3b 0a 20 20 20 20 2a  ata.pData;.    *
3707c 70 6e 52 6f 6f 74 49 6e 66 6f 20 3d 20 70 57 72  pnRootInfo = pWr
3707d 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61  iter->data.nData
3707e 3b 0a 20 20 20 20 2a 70 69 45 6e 64 42 6c 6f 63  ;.    *piEndBloc
3707f 6b 69 64 20 3d 20 30 3b 0a 20 20 20 20 72 65 74  kid = 0;.    ret
37080 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
37081 20 7d 0a 0a 20 20 2f 2a 20 46 6c 75 73 68 20 72   }..  /* Flush r
37082 65 6d 61 69 6e 69 6e 67 20 6c 65 61 66 20 64 61  emaining leaf da
37083 74 61 2e 20 2a 2f 0a 20 20 69 66 28 20 70 57 72  ta. */.  if( pWr
37084 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61  iter->data.nData
37085 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63  >0 ){.    int rc
37086 20 3d 20 6c 65 61 66 57 72 69 74 65 72 46 6c 75   = leafWriterFlu
37087 73 68 28 76 2c 20 70 57 72 69 74 65 72 29 3b 0a  sh(v, pWriter);.
37088 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
37089 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
3708a 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 20  c;.  }..  /* We 
3708b 6d 75 73 74 20 68 61 76 65 20 66 6c 75 73 68 65  must have flushe
3708c 64 20 61 20 6c 65 61 66 20 61 74 20 73 6f 6d 65  d a leaf at some
3708d 20 70 6f 69 6e 74 2e 20 2a 2f 0a 20 20 61 73 73   point. */.  ass
3708e 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e 68 61  ert( pWriter->ha
3708f 73 5f 70 61 72 65 6e 74 20 29 3b 0a 0a 20 20 2f  s_parent );..  /
37090 2a 20 54 65 6e 61 74 69 76 65 6c 79 20 73 65 74  * Tenatively set
37091 20 74 68 65 20 65 6e 64 20 6c 65 61 66 20 62 6c   the end leaf bl
37092 6f 63 6b 69 64 20 61 73 20 74 68 65 20 65 6e 64  ockid as the end
37093 20 62 6c 6f 63 6b 69 64 2e 20 20 49 66 20 74 68   blockid.  If th
37094 65 0a 20 20 2a 2a 20 69 6e 74 65 72 69 6f 72 20  e.  ** interior 
37095 6e 6f 64 65 20 63 61 6e 20 62 65 20 72 65 74 75  node can be retu
37096 72 6e 65 64 20 69 6e 6c 69 6e 65 2c 20 74 68 69  rned inline, thi
37097 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20 66 69  s will be the fi
37098 6e 61 6c 0a 20 20 2a 2a 20 62 6c 6f 63 6b 69 64  nal.  ** blockid
37099 2c 20 6f 74 68 65 72 77 69 73 65 20 69 74 20 77  , otherwise it w
3709a 69 6c 6c 20 62 65 20 6f 76 65 72 77 72 69 74 74  ill be overwritt
3709b 65 6e 20 62 79 0a 20 20 2a 2a 20 69 6e 74 65 72  en by.  ** inter
3709c 69 6f 72 57 72 69 74 65 72 52 6f 6f 74 49 6e 66  iorWriterRootInf
3709d 6f 28 29 2e 0a 20 20 2a 2f 0a 20 20 2a 70 69 45  o()..  */.  *piE
3709e 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 70 57 72 69  ndBlockid = pWri
3709f 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69 64  ter->iEndBlockid
370a0 3b 0a 0a 20 20 72 65 74 75 72 6e 20 69 6e 74 65  ;..  return inte
370a1 72 69 6f 72 57 72 69 74 65 72 52 6f 6f 74 49 6e  riorWriterRootIn
370a2 66 6f 28 76 2c 20 26 70 57 72 69 74 65 72 2d 3e  fo(v, &pWriter->
370a3 70 61 72 65 6e 74 57 72 69 74 65 72 2c 0a 20 20  parentWriter,.  
370a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
370a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 70                pp
370a6 52 6f 6f 74 49 6e 66 6f 2c 20 70 6e 52 6f 6f 74  RootInfo, pnRoot
370a7 49 6e 66 6f 2c 20 70 69 45 6e 64 42 6c 6f 63 6b  Info, piEndBlock
370a8 69 64 29 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6c 6c 65  id);.}../* Colle
370a9 63 74 20 74 68 65 20 72 6f 6f 74 49 6e 66 6f 20  ct the rootInfo 
370aa 64 61 74 61 20 61 6e 64 20 73 74 6f 72 65 20 69  data and store i
370ab 74 20 69 6e 74 6f 20 74 68 65 20 73 65 67 6d 65  t into the segme
370ac 6e 74 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a  nt directory..**
370ad 20 54 68 69 73 20 68 61 73 20 74 68 65 20 65 66   This has the ef
370ae 66 65 63 74 20 6f 66 20 66 6c 75 73 68 69 6e 67  fect of flushing
370af 20 74 68 65 20 73 65 67 6d 65 6e 74 27 73 20 6c   the segment's l
370b0 65 61 66 20 64 61 74 61 20 74 6f 0a 2a 2a 20 25  eaf data to.** %
370b1 5f 73 65 67 6d 65 6e 74 73 2c 20 61 6e 64 20 61  _segments, and a
370b2 6c 73 6f 20 66 6c 75 73 68 69 6e 67 20 61 6e 79  lso flushing any
370b3 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20   interior nodes 
370b4 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 2e 0a 2a  to %_segments..*
370b5 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61  /.static int lea
370b6 66 57 72 69 74 65 72 46 69 6e 61 6c 69 7a 65 28  fWriterFinalize(
370b7 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
370b8 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57  , LeafWriter *pW
370b9 72 69 74 65 72 29 7b 0a 20 20 73 71 6c 69 74 65  riter){.  sqlite
370ba 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f 63 6b  _int64 iEndBlock
370bb 69 64 3b 0a 20 20 63 68 61 72 20 2a 70 52 6f 6f  id;.  char *pRoo
370bc 74 49 6e 66 6f 3b 0a 20 20 69 6e 74 20 72 63 2c  tInfo;.  int rc,
370bd 20 6e 52 6f 6f 74 49 6e 66 6f 3b 0a 0a 20 20 72   nRootInfo;..  r
370be 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 52 6f  c = leafWriterRo
370bf 6f 74 49 6e 66 6f 28 76 2c 20 70 57 72 69 74 65  otInfo(v, pWrite
370c0 72 2c 20 26 70 52 6f 6f 74 49 6e 66 6f 2c 20 26  r, &pRootInfo, &
370c1 6e 52 6f 6f 74 49 6e 66 6f 2c 20 26 69 45 6e 64  nRootInfo, &iEnd
370c2 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20  Blockid);.  if( 
370c3 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
370c4 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a  return rc;..  /*
370c5 20 44 6f 6e 27 74 20 62 6f 74 68 65 72 20 73 74   Don't bother st
370c6 6f 72 69 6e 67 20 61 6e 20 65 6e 74 69 72 65 6c  oring an entirel
370c7 79 20 65 6d 70 74 79 20 73 65 67 6d 65 6e 74 2e  y empty segment.
370c8 20 2a 2f 0a 20 20 69 66 28 20 69 45 6e 64 42 6c   */.  if( iEndBl
370c9 6f 63 6b 69 64 3d 3d 30 20 26 26 20 6e 52 6f 6f  ockid==0 && nRoo
370ca 74 49 6e 66 6f 3d 3d 30 20 29 20 72 65 74 75 72  tInfo==0 ) retur
370cb 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  n SQLITE_OK;..  
370cc 72 65 74 75 72 6e 20 73 65 67 64 69 72 5f 73 65  return segdir_se
370cd 74 28 76 2c 20 70 57 72 69 74 65 72 2d 3e 69 4c  t(v, pWriter->iL
370ce 65 76 65 6c 2c 20 70 57 72 69 74 65 72 2d 3e 69  evel, pWriter->i
370cf 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  dx,.            
370d0 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72 2d          pWriter-
370d1 3e 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c 20  >iStartBlockid, 
370d2 70 57 72 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f  pWriter->iEndBlo
370d3 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20  ckid,.          
370d4 20 20 20 20 20 20 20 20 20 20 69 45 6e 64 42 6c            iEndBl
370d5 6f 63 6b 69 64 2c 20 70 52 6f 6f 74 49 6e 66 6f  ockid, pRootInfo
370d6 2c 20 6e 52 6f 6f 74 49 6e 66 6f 29 3b 0a 7d 0a  , nRootInfo);.}.
370d7 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65 61  .static void lea
370d8 66 57 72 69 74 65 72 44 65 73 74 72 6f 79 28 4c  fWriterDestroy(L
370d9 65 61 66 57 72 69 74 65 72 20 2a 70 57 72 69 74  eafWriter *pWrit
370da 65 72 29 7b 0a 20 20 69 66 28 20 70 57 72 69 74  er){.  if( pWrit
370db 65 72 2d 3e 68 61 73 5f 70 61 72 65 6e 74 20 29  er->has_parent )
370dc 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 44   interiorWriterD
370dd 65 73 74 72 6f 79 28 26 70 57 72 69 74 65 72 2d  estroy(&pWriter-
370de 3e 70 61 72 65 6e 74 57 72 69 74 65 72 29 3b 0a  >parentWriter);.
370df 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74    dataBufferDest
370e0 72 6f 79 28 26 70 57 72 69 74 65 72 2d 3e 74 65  roy(&pWriter->te
370e1 72 6d 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  rm);.  dataBuffe
370e2 72 44 65 73 74 72 6f 79 28 26 70 57 72 69 74 65  rDestroy(&pWrite
370e3 72 2d 3e 64 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 20  r->data);.}../* 
370e4 45 6e 63 6f 64 65 20 61 20 74 65 72 6d 20 69 6e  Encode a term in
370e5 74 6f 20 74 68 65 20 6c 65 61 66 57 72 69 74 65  to the leafWrite
370e6 72 2c 20 64 65 6c 74 61 2d 65 6e 63 6f 64 69 6e  r, delta-encodin
370e7 67 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65  g as appropriate
370e8 2e 0a 2a 2a 20 52 65 74 75 72 6e 73 20 74 68 65  ..** Returns the
370e9 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 6e   length of the n
370ea 65 77 20 74 65 72 6d 20 77 68 69 63 68 20 64 69  ew term which di
370eb 73 74 69 6e 67 75 69 73 68 65 73 20 69 74 20 66  stinguishes it f
370ec 72 6f 6d 20 74 68 65 0a 2a 2a 20 70 72 65 76 69  rom the.** previ
370ed 6f 75 73 20 74 65 72 6d 2c 20 77 68 69 63 68 20  ous term, which 
370ee 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73  can be used to s
370ef 65 74 20 6e 54 65 72 6d 44 69 73 74 69 6e 63 74  et nTermDistinct
370f0 20 77 68 65 6e 20 61 20 6e 6f 64 65 0a 2a 2a 20   when a node.** 
370f1 62 6f 75 6e 64 61 72 79 20 69 73 20 63 72 6f 73  boundary is cros
370f2 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  sed..*/.static i
370f3 6e 74 20 6c 65 61 66 57 72 69 74 65 72 45 6e 63  nt leafWriterEnc
370f4 6f 64 65 54 65 72 6d 28 4c 65 61 66 57 72 69 74  odeTerm(LeafWrit
370f5 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20  er *pWriter,.   
370f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
370f7 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
370f8 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20  st char *pTerm, 
370f9 69 6e 74 20 6e 54 65 72 6d 29 7b 0a 20 20 63 68  int nTerm){.  ch
370fa 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b  ar c[VARINT_MAX+
370fb 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69  VARINT_MAX];.  i
370fc 6e 74 20 6e 2c 20 6e 50 72 65 66 69 78 20 3d 20  nt n, nPrefix = 
370fd 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 54  0;..  assert( nT
370fe 65 72 6d 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65  erm>0 );.  while
370ff 28 20 6e 50 72 65 66 69 78 3c 70 57 72 69 74 65  ( nPrefix<pWrite
37100 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20 26 26  r->term.nData &&
37101 0a 20 20 20 20 20 20 20 20 20 70 54 65 72 6d 5b  .         pTerm[
37102 6e 50 72 65 66 69 78 5d 3d 3d 70 57 72 69 74 65  nPrefix]==pWrite
37103 72 2d 3e 74 65 72 6d 2e 70 44 61 74 61 5b 6e 50  r->term.pData[nP
37104 72 65 66 69 78 5d 20 29 7b 0a 20 20 20 20 6e 50  refix] ){.    nP
37105 72 65 66 69 78 2b 2b 3b 0a 20 20 20 20 2f 2a 20  refix++;.    /* 
37106 46 61 69 6c 69 6e 67 20 74 68 69 73 20 69 6d 70  Failing this imp
37107 6c 69 65 73 20 74 68 61 74 20 74 68 65 20 74 65  lies that the te
37108 72 6d 73 20 77 65 72 65 6e 27 74 20 69 6e 20 6f  rms weren't in o
37109 72 64 65 72 2e 20 2a 2f 0a 20 20 20 20 61 73 73  rder. */.    ass
3710a 65 72 74 28 20 6e 50 72 65 66 69 78 3c 6e 54 65  ert( nPrefix<nTe
3710b 72 6d 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  rm );.  }..  if(
3710c 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e   pWriter->data.n
3710d 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  Data==0 ){.    /
3710e 2a 20 45 6e 63 6f 64 65 20 74 68 65 20 6e 6f 64  * Encode the nod
3710f 65 20 68 65 61 64 65 72 20 61 6e 64 20 6c 65 61  e header and lea
37110 64 69 6e 67 20 74 65 72 6d 20 61 73 3a 0a 20 20  ding term as:.  
37111 20 20 2a 2a 20 20 76 61 72 69 6e 74 28 30 29 0a    **  varint(0).
37112 20 20 20 20 2a 2a 20 20 76 61 72 69 6e 74 28 6e      **  varint(n
37113 54 65 72 6d 29 0a 20 20 20 20 2a 2a 20 20 63 68  Term).    **  ch
37114 61 72 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 0a  ar pTerm[nTerm].
37115 20 20 20 20 2a 2f 0a 20 20 20 20 6e 20 3d 20 66      */.    n = f
37116 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20  ts3PutVarint(c, 
37117 27 5c 30 27 29 3b 0a 20 20 20 20 6e 20 2b 3d 20  '\0');.    n += 
37118 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b  fts3PutVarint(c+
37119 6e 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20 20 64  n, nTerm);.    d
3711a 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32  ataBufferAppend2
3711b 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2c  (&pWriter->data,
3711c 20 63 2c 20 6e 2c 20 70 54 65 72 6d 2c 20 6e 54   c, n, pTerm, nT
3711d 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  erm);.  }else{. 
3711e 20 20 20 2f 2a 20 44 65 6c 74 61 2d 65 6e 63 6f     /* Delta-enco
3711f 64 65 20 74 68 65 20 74 65 72 6d 20 61 73 3a 0a  de the term as:.
37120 20 20 20 20 2a 2a 20 20 76 61 72 69 6e 74 28 6e      **  varint(n
37121 50 72 65 66 69 78 29 0a 20 20 20 20 2a 2a 20 20  Prefix).    **  
37122 76 61 72 69 6e 74 28 6e 53 75 66 66 69 78 29 0a  varint(nSuffix).
37123 20 20 20 20 2a 2a 20 20 63 68 61 72 20 70 54 65      **  char pTe
37124 72 6d 53 75 66 66 69 78 5b 6e 53 75 66 66 69 78  rmSuffix[nSuffix
37125 5d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 20 3d  ].    */.    n =
37126 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
37127 2c 20 6e 50 72 65 66 69 78 29 3b 0a 20 20 20 20  , nPrefix);.    
37128 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69  n += fts3PutVari
37129 6e 74 28 63 2b 6e 2c 20 6e 54 65 72 6d 2d 6e 50  nt(c+n, nTerm-nP
3712a 72 65 66 69 78 29 3b 0a 20 20 20 20 64 61 74 61  refix);.    data
3712b 42 75 66 66 65 72 41 70 70 65 6e 64 32 28 26 70  BufferAppend2(&p
3712c 57 72 69 74 65 72 2d 3e 64 61 74 61 2c 20 63 2c  Writer->data, c,
3712d 20 6e 2c 20 70 54 65 72 6d 2b 6e 50 72 65 66 69   n, pTerm+nPrefi
3712e 78 2c 20 6e 54 65 72 6d 2d 6e 50 72 65 66 69 78  x, nTerm-nPrefix
3712f 29 3b 0a 20 20 7d 0a 20 20 64 61 74 61 42 75 66  );.  }.  dataBuf
37130 66 65 72 52 65 70 6c 61 63 65 28 26 70 57 72 69  ferReplace(&pWri
37131 74 65 72 2d 3e 74 65 72 6d 2c 20 70 54 65 72 6d  ter->term, pTerm
37132 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 72 65 74  , nTerm);..  ret
37133 75 72 6e 20 6e 50 72 65 66 69 78 2b 31 3b 0a 7d  urn nPrefix+1;.}
37134 0a 0a 2f 2a 20 55 73 65 64 20 74 6f 20 61 76 6f  ../* Used to avo
37135 69 64 20 61 20 6d 65 6d 6d 6f 76 65 20 77 68 65  id a memmove whe
37136 6e 20 61 20 6c 61 72 67 65 20 61 6d 6f 75 6e 74  n a large amount
37137 20 6f 66 20 64 6f 63 6c 69 73 74 20 64 61 74 61   of doclist data
37138 20 69 73 20 69 6e 0a 2a 2a 20 74 68 65 20 62 75   is in.** the bu
37139 66 66 65 72 2e 20 20 54 68 69 73 20 63 6f 6e 73  ffer.  This cons
3713a 74 72 75 63 74 73 20 61 20 6e 6f 64 65 20 61 6e  tructs a node an
3713b 64 20 74 65 72 6d 20 68 65 61 64 65 72 20 62 65  d term header be
3713c 66 6f 72 65 0a 2a 2a 20 69 44 6f 63 6c 69 73 74  fore.** iDoclist
3713d 44 61 74 61 20 61 6e 64 20 66 6c 75 73 68 65 73  Data and flushes
3713e 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 63   the resulting c
3713f 6f 6d 70 6c 65 74 65 20 6e 6f 64 65 20 75 73 69  omplete node usi
37140 6e 67 0a 2a 2a 20 6c 65 61 66 57 72 69 74 65 72  ng.** leafWriter
37141 49 6e 74 65 72 6e 61 6c 46 6c 75 73 68 28 29 2e  InternalFlush().
37142 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
37143 65 61 66 57 72 69 74 65 72 49 6e 6c 69 6e 65 46  eafWriterInlineF
37144 6c 75 73 68 28 66 75 6c 6c 74 65 78 74 5f 76 74  lush(fulltext_vt
37145 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74 65  ab *v, LeafWrite
37146 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20  r *pWriter,.    
37147 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37148 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
37149 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20  st char *pTerm, 
3714a 69 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20  int nTerm,.     
3714b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3714c 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
3714d 69 44 6f 63 6c 69 73 74 44 61 74 61 29 7b 0a 20  iDoclistData){. 
3714e 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d   char c[VARINT_M
3714f 41 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a  AX+VARINT_MAX];.
37150 20 20 69 6e 74 20 69 44 61 74 61 2c 20 6e 20 3d    int iData, n =
37151 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
37152 2c 20 30 29 3b 0a 20 20 6e 20 2b 3d 20 66 74 73  , 0);.  n += fts
37153 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20  3PutVarint(c+n, 
37154 6e 54 65 72 6d 29 3b 0a 0a 20 20 2f 2a 20 54 68  nTerm);..  /* Th
37155 65 72 65 20 73 68 6f 75 6c 64 20 61 6c 77 61 79  ere should alway
37156 73 20 62 65 20 72 6f 6f 6d 20 66 6f 72 20 74 68  s be room for th
37157 65 20 68 65 61 64 65 72 2e 20 20 45 76 65 6e 20  e header.  Even 
37158 69 66 20 70 54 65 72 6d 20 73 68 61 72 65 64 0a  if pTerm shared.
37159 20 20 2a 2a 20 61 20 73 75 62 73 74 61 6e 74 69    ** a substanti
3715a 61 6c 20 70 72 65 66 69 78 20 77 69 74 68 20 74  al prefix with t
3715b 68 65 20 70 72 65 76 69 6f 75 73 20 74 65 72 6d  he previous term
3715c 2c 20 74 68 65 20 65 6e 74 69 72 65 20 70 72 65  , the entire pre
3715d 66 69 78 0a 20 20 2a 2a 20 63 6f 75 6c 64 20 62  fix.  ** could b
3715e 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20 66 72  e constructed fr
3715f 6f 6d 20 65 61 72 6c 69 65 72 20 64 61 74 61 20  om earlier data 
37160 69 6e 20 74 68 65 20 64 6f 63 6c 69 73 74 2c 20  in the doclist, 
37161 73 6f 20 74 68 65 72 65 0a 20 20 2a 2a 20 73 68  so there.  ** sh
37162 6f 75 6c 64 20 62 65 20 72 6f 6f 6d 2e 0a 20 20  ould be room..  
37163 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 44 6f  */.  assert( iDo
37164 63 6c 69 73 74 44 61 74 61 3e 3d 6e 2b 6e 54 65  clistData>=n+nTe
37165 72 6d 20 29 3b 0a 0a 20 20 69 44 61 74 61 20 3d  rm );..  iData =
37166 20 69 44 6f 63 6c 69 73 74 44 61 74 61 2d 28 6e   iDoclistData-(n
37167 2b 6e 54 65 72 6d 29 3b 0a 20 20 6d 65 6d 63 70  +nTerm);.  memcp
37168 79 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e  y(pWriter->data.
37169 70 44 61 74 61 2b 69 44 61 74 61 2c 20 63 2c 20  pData+iData, c, 
3716a 6e 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 57 72  n);.  memcpy(pWr
3716b 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61  iter->data.pData
3716c 2b 69 44 61 74 61 2b 6e 2c 20 70 54 65 72 6d 2c  +iData+n, pTerm,
3716d 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 72 65 74 75   nTerm);..  retu
3716e 72 6e 20 6c 65 61 66 57 72 69 74 65 72 49 6e 74  rn leafWriterInt
3716f 65 72 6e 61 6c 46 6c 75 73 68 28 76 2c 20 70 57  ernalFlush(v, pW
37170 72 69 74 65 72 2c 20 69 44 61 74 61 2c 20 70 57  riter, iData, pW
37171 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
37172 61 2d 69 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 20  a-iData);.}../* 
37173 50 75 73 68 20 70 54 65 72 6d 5b 6e 54 65 72 6d  Push pTerm[nTerm
37174 5d 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65  ] along with the
37175 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 74 6f   doclist data to
37176 20 74 68 65 20 6c 65 61 66 20 6c 61 79 65 72 20   the leaf layer 
37177 6f 66 0a 2a 2a 20 25 5f 73 65 67 6d 65 6e 74 73  of.** %_segments
37178 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
37179 6c 65 61 66 57 72 69 74 65 72 53 74 65 70 4d 65  leafWriterStepMe
3717a 72 67 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  rge(fulltext_vta
3717b 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74 65 72  b *v, LeafWriter
3717c 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20   *pWriter,.     
3717d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3717e 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
3717f 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74  char *pTerm, int
37180 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20 20 20   nTerm,.        
37181 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37182 20 20 20 20 20 20 20 44 4c 52 65 61 64 65 72 20         DLReader 
37183 2a 70 52 65 61 64 65 72 73 2c 20 69 6e 74 20 6e  *pReaders, int n
37184 52 65 61 64 65 72 73 29 7b 0a 20 20 63 68 61 72  Readers){.  char
37185 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b 56 41   c[VARINT_MAX+VA
37186 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74  RINT_MAX];.  int
37187 20 69 54 65 72 6d 44 61 74 61 20 3d 20 70 57 72   iTermData = pWr
37188 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61  iter->data.nData
37189 2c 20 69 44 6f 63 6c 69 73 74 44 61 74 61 3b 0a  , iDoclistData;.
3718a 20 20 69 6e 74 20 69 2c 20 6e 44 61 74 61 2c 20    int i, nData, 
3718b 6e 2c 20 6e 41 63 74 75 61 6c 44 61 74 61 2c 20  n, nActualData, 
3718c 6e 41 63 74 75 61 6c 2c 20 72 63 2c 20 6e 54 65  nActual, rc, nTe
3718d 72 6d 44 69 73 74 69 6e 63 74 3b 0a 0a 20 20 41  rmDistinct;..  A
3718e 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c 45 41 46  SSERT_VALID_LEAF
3718f 5f 4e 4f 44 45 28 70 57 72 69 74 65 72 2d 3e 64  _NODE(pWriter->d
37190 61 74 61 2e 70 44 61 74 61 2c 20 70 57 72 69 74  ata.pData, pWrit
37191 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 29 3b  er->data.nData);
37192 0a 20 20 6e 54 65 72 6d 44 69 73 74 69 6e 63 74  .  nTermDistinct
37193 20 3d 20 6c 65 61 66 57 72 69 74 65 72 45 6e 63   = leafWriterEnc
37194 6f 64 65 54 65 72 6d 28 70 57 72 69 74 65 72 2c  odeTerm(pWriter,
37195 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a   pTerm, nTerm);.
37196 0a 20 20 2f 2a 20 52 65 6d 65 6d 62 65 72 20 6e  .  /* Remember n
37197 54 65 72 6d 44 69 73 74 69 6e 63 74 20 69 66 20  TermDistinct if 
37198 6f 70 65 6e 69 6e 67 20 61 20 6e 65 77 20 6e 6f  opening a new no
37199 64 65 2e 20 2a 2f 0a 20 20 69 66 28 20 69 54 65  de. */.  if( iTe
3719a 72 6d 44 61 74 61 3d 3d 30 20 29 20 70 57 72 69  rmData==0 ) pWri
3719b 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74 69 6e  ter->nTermDistin
3719c 63 74 20 3d 20 6e 54 65 72 6d 44 69 73 74 69 6e  ct = nTermDistin
3719d 63 74 3b 0a 0a 20 20 69 44 6f 63 6c 69 73 74 44  ct;..  iDoclistD
3719e 61 74 61 20 3d 20 70 57 72 69 74 65 72 2d 3e 64  ata = pWriter->d
3719f 61 74 61 2e 6e 44 61 74 61 3b 0a 0a 20 20 2f 2a  ata.nData;..  /*
371a0 20 45 73 74 69 6d 61 74 65 20 74 68 65 20 6c 65   Estimate the le
371a1 6e 67 74 68 20 6f 66 20 74 68 65 20 6d 65 72 67  ngth of the merg
371a2 65 64 20 64 6f 63 6c 69 73 74 20 73 6f 20 77 65  ed doclist so we
371a3 20 63 61 6e 20 6c 65 61 76 65 20 73 70 61 63 65   can leave space
371a4 0a 20 20 2a 2a 20 74 6f 20 65 6e 63 6f 64 65 20  .  ** to encode 
371a5 69 74 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  it..  */.  for(i
371a6 3d 30 2c 20 6e 44 61 74 61 3d 30 3b 20 69 3c 6e  =0, nData=0; i<n
371a7 52 65 61 64 65 72 73 3b 20 69 2b 2b 29 7b 0a 20  Readers; i++){. 
371a8 20 20 20 6e 44 61 74 61 20 2b 3d 20 64 6c 72 41     nData += dlrA
371a9 6c 6c 44 61 74 61 42 79 74 65 73 28 26 70 52 65  llDataBytes(&pRe
371aa 61 64 65 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 20  aders[i]);.  }. 
371ab 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72 69   n = fts3PutVari
371ac 6e 74 28 63 2c 20 6e 44 61 74 61 29 3b 0a 20 20  nt(c, nData);.  
371ad 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
371ae 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2c  (&pWriter->data,
371af 20 63 2c 20 6e 29 3b 0a 0a 20 20 64 6f 63 4c 69   c, n);..  docLi
371b0 73 74 4d 65 72 67 65 28 26 70 57 72 69 74 65 72  stMerge(&pWriter
371b1 2d 3e 64 61 74 61 2c 20 70 52 65 61 64 65 72 73  ->data, pReaders
371b2 2c 20 6e 52 65 61 64 65 72 73 29 3b 0a 20 20 41  , nReaders);.  A
371b3 53 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c  SSERT_VALID_DOCL
371b4 49 53 54 28 44 4c 5f 44 45 46 41 55 4c 54 2c 0a  IST(DL_DEFAULT,.
371b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
371b6 20 20 20 20 20 20 20 70 57 72 69 74 65 72 2d 3e         pWriter->
371b7 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63 6c  data.pData+iDocl
371b8 69 73 74 44 61 74 61 2b 6e 2c 0a 20 20 20 20 20  istData+n,.     
371b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
371ba 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e    pWriter->data.
371bb 6e 44 61 74 61 2d 69 44 6f 63 6c 69 73 74 44 61  nData-iDoclistDa
371bc 74 61 2d 6e 2c 20 4e 55 4c 4c 29 3b 0a 0a 20 20  ta-n, NULL);..  
371bd 2f 2a 20 54 68 65 20 61 63 74 75 61 6c 20 61 6d  /* The actual am
371be 6f 75 6e 74 20 6f 66 20 64 6f 63 6c 69 73 74 20  ount of doclist 
371bf 64 61 74 61 20 61 74 20 74 68 69 73 20 70 6f 69  data at this poi
371c0 6e 74 20 63 6f 75 6c 64 20 62 65 20 73 6d 61 6c  nt could be smal
371c1 6c 65 72 0a 20 20 2a 2a 20 74 68 61 6e 20 74 68  ler.  ** than th
371c2 65 20 6c 65 6e 67 74 68 20 77 65 20 65 6e 63 6f  e length we enco
371c3 64 65 64 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c  ded.  Additional
371c4 6c 79 2c 20 74 68 65 20 73 70 61 63 65 20 72 65  ly, the space re
371c5 71 75 69 72 65 64 20 74 6f 0a 20 20 2a 2a 20 65  quired to.  ** e
371c6 6e 63 6f 64 65 20 74 68 69 73 20 6c 65 6e 67 74  ncode this lengt
371c7 68 20 63 6f 75 6c 64 20 62 65 20 73 6d 61 6c 6c  h could be small
371c8 65 72 2e 20 20 46 6f 72 20 73 6d 61 6c 6c 20 64  er.  For small d
371c9 6f 63 6c 69 73 74 73 2c 20 74 68 69 73 20 69 73  oclists, this is
371ca 0a 20 20 2a 2a 20 6e 6f 74 20 61 20 62 69 67 20  .  ** not a big 
371cb 64 65 61 6c 2c 20 77 65 20 63 61 6e 20 6a 75 73  deal, we can jus
371cc 74 20 75 73 65 20 6d 65 6d 6d 6f 76 65 28 29 20  t use memmove() 
371cd 74 6f 20 61 64 6a 75 73 74 20 74 68 69 6e 67 73  to adjust things
371ce 2e 0a 20 20 2a 2f 0a 20 20 6e 41 63 74 75 61 6c  ..  */.  nActual
371cf 44 61 74 61 20 3d 20 70 57 72 69 74 65 72 2d 3e  Data = pWriter->
371d0 64 61 74 61 2e 6e 44 61 74 61 2d 28 69 44 6f 63  data.nData-(iDoc
371d1 6c 69 73 74 44 61 74 61 2b 6e 29 3b 0a 20 20 6e  listData+n);.  n
371d2 41 63 74 75 61 6c 20 3d 20 66 74 73 33 50 75 74  Actual = fts3Put
371d3 56 61 72 69 6e 74 28 63 2c 20 6e 41 63 74 75 61  Varint(c, nActua
371d4 6c 44 61 74 61 29 3b 0a 20 20 61 73 73 65 72 74  lData);.  assert
371d5 28 20 6e 41 63 74 75 61 6c 44 61 74 61 3c 3d 6e  ( nActualData<=n
371d6 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74  Data );.  assert
371d7 28 20 6e 41 63 74 75 61 6c 3c 3d 6e 20 29 3b 0a  ( nActual<=n );.
371d8 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6e 65 77  .  /* If the new
371d9 20 64 6f 63 6c 69 73 74 20 69 73 20 62 69 67 20   doclist is big 
371da 65 6e 6f 75 67 68 20 66 6f 72 20 66 6f 72 63 65  enough for force
371db 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65 20 6c 65   a standalone le
371dc 61 66 0a 20 20 2a 2a 20 6e 6f 64 65 2c 20 77 65  af.  ** node, we
371dd 20 63 61 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79   can immediately
371de 20 66 6c 75 73 68 20 69 74 20 69 6e 6c 69 6e 65   flush it inline
371df 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 74   without doing t
371e0 68 65 0a 20 20 2a 2a 20 6d 65 6d 6d 6f 76 65 28  he.  ** memmove(
371e1 29 2e 0a 20 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44  )..  */.  /* TOD
371e2 4f 28 73 68 65 73 73 29 20 54 68 69 73 20 74 65  O(shess) This te
371e3 73 74 20 6d 61 74 63 68 65 73 20 6c 65 61 66 57  st matches leafW
371e4 72 69 74 65 72 53 74 65 70 28 29 2c 20 77 68 69  riterStep(), whi
371e5 63 68 20 64 6f 65 73 20 74 68 69 73 0a 20 20 2a  ch does this.  *
371e6 2a 20 74 65 73 74 20 62 65 66 6f 72 65 20 69 74  * test before it
371e7 20 6b 6e 6f 77 73 20 74 68 65 20 63 6f 73 74 20   knows the cost 
371e8 74 6f 20 76 61 72 69 6e 74 2d 65 6e 63 6f 64 65  to varint-encode
371e9 20 74 68 65 20 74 65 72 6d 20 61 6e 64 0a 20 20   the term and.  
371ea 2a 2a 20 64 6f 63 6c 69 73 74 20 6c 65 6e 67 74  ** doclist lengt
371eb 68 73 2e 20 20 41 74 20 73 6f 6d 65 20 70 6f 69  hs.  At some poi
371ec 6e 74 2c 20 63 68 61 6e 67 65 20 74 6f 0a 20 20  nt, change to.  
371ed 2a 2a 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61  ** pWriter->data
371ee 2e 6e 44 61 74 61 2d 69 54 65 72 6d 44 61 74 61  .nData-iTermData
371ef 3e 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 2e  >STANDALONE_MIN.
371f0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 54 65 72  .  */.  if( nTer
371f1 6d 2b 6e 41 63 74 75 61 6c 44 61 74 61 3e 53 54  m+nActualData>ST
371f2 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 29 7b 0a  ANDALONE_MIN ){.
371f3 20 20 20 20 2f 2a 20 50 75 73 68 20 6c 65 61 66      /* Push leaf
371f4 20 6e 6f 64 65 20 66 72 6f 6d 20 62 65 66 6f 72   node from befor
371f5 65 20 74 68 69 73 20 74 65 72 6d 2e 20 2a 2f 0a  e this term. */.
371f6 20 20 20 20 69 66 28 20 69 54 65 72 6d 44 61 74      if( iTermDat
371f7 61 3e 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  a>0 ){.      rc 
371f8 3d 20 6c 65 61 66 57 72 69 74 65 72 49 6e 74 65  = leafWriterInte
371f9 72 6e 61 6c 46 6c 75 73 68 28 76 2c 20 70 57 72  rnalFlush(v, pWr
371fa 69 74 65 72 2c 20 30 2c 20 69 54 65 72 6d 44 61  iter, 0, iTermDa
371fb 74 61 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ta);.      if( r
371fc 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
371fd 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 20  eturn rc;..     
371fe 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72 6d 44   pWriter->nTermD
371ff 69 73 74 69 6e 63 74 20 3d 20 6e 54 65 72 6d 44  istinct = nTermD
37200 69 73 74 69 6e 63 74 3b 0a 20 20 20 20 7d 0a 0a  istinct;.    }..
37201 20 20 20 20 2f 2a 20 46 69 78 20 74 68 65 20 65      /* Fix the e
37202 6e 63 6f 64 65 64 20 64 6f 63 6c 69 73 74 20 6c  ncoded doclist l
37203 65 6e 67 74 68 2e 20 2a 2f 0a 20 20 20 20 69 44  ength. */.    iD
37204 6f 63 6c 69 73 74 44 61 74 61 20 2b 3d 20 6e 20  oclistData += n 
37205 2d 20 6e 41 63 74 75 61 6c 3b 0a 20 20 20 20 6d  - nActual;.    m
37206 65 6d 63 70 79 28 70 57 72 69 74 65 72 2d 3e 64  emcpy(pWriter->d
37207 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63 6c 69  ata.pData+iDocli
37208 73 74 44 61 74 61 2c 20 63 2c 20 6e 41 63 74 75  stData, c, nActu
37209 61 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 50 75 73  al);..    /* Pus
3720a 68 20 74 68 65 20 73 74 61 6e 64 61 6c 6f 6e 65  h the standalone
3720b 20 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a 20   leaf node. */. 
3720c 20 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74     rc = leafWrit
3720d 65 72 49 6e 6c 69 6e 65 46 6c 75 73 68 28 76 2c  erInlineFlush(v,
3720e 20 70 57 72 69 74 65 72 2c 20 70 54 65 72 6d 2c   pWriter, pTerm,
3720f 20 6e 54 65 72 6d 2c 20 69 44 6f 63 6c 69 73 74   nTerm, iDoclist
37210 44 61 74 61 29 3b 0a 20 20 20 20 69 66 28 20 72  Data);.    if( r
37211 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
37212 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 2f  eturn rc;..    /
37213 2a 20 4c 65 61 76 65 20 74 68 65 20 6e 6f 64 65  * Leave the node
37214 20 65 6d 70 74 79 2e 20 2a 2f 0a 20 20 20 20 64   empty. */.    d
37215 61 74 61 42 75 66 66 65 72 52 65 73 65 74 28 26  ataBufferReset(&
37216 70 57 72 69 74 65 72 2d 3e 64 61 74 61 29 3b 0a  pWriter->data);.
37217 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
37218 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69    }..  /* At thi
37219 73 20 70 6f 69 6e 74 2c 20 77 65 20 6b 6e 6f 77  s point, we know
3721a 20 74 68 61 74 20 74 68 65 20 64 6f 63 6c 69 73   that the doclis
3721b 74 20 77 61 73 20 73 6d 61 6c 6c 2c 20 73 6f 20  t was small, so 
3721c 64 6f 20 74 68 65 0a 20 20 2a 2a 20 6d 65 6d 6d  do the.  ** memm
3721d 6f 76 65 20 69 66 20 69 6e 64 69 63 61 74 65 64  ove if indicated
3721e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 41 63  ..  */.  if( nAc
3721f 74 75 61 6c 3c 6e 20 29 7b 0a 20 20 20 20 6d 65  tual<n ){.    me
37220 6d 6d 6f 76 65 28 70 57 72 69 74 65 72 2d 3e 64  mmove(pWriter->d
37221 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63 6c 69  ata.pData+iDocli
37222 73 74 44 61 74 61 2b 6e 41 63 74 75 61 6c 2c 0a  stData+nActual,.
37223 20 20 20 20 20 20 20 20 20 20 20 20 70 57 72 69              pWri
37224 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b  ter->data.pData+
37225 69 44 6f 63 6c 69 73 74 44 61 74 61 2b 6e 2c 0a  iDoclistData+n,.
37226 20 20 20 20 20 20 20 20 20 20 20 20 70 57 72 69              pWri
37227 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d  ter->data.nData-
37228 28 69 44 6f 63 6c 69 73 74 44 61 74 61 2b 6e 29  (iDoclistData+n)
37229 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  );.    pWriter->
3722a 64 61 74 61 2e 6e 44 61 74 61 20 2d 3d 20 6e 2d  data.nData -= n-
3722b 6e 41 63 74 75 61 6c 3b 0a 20 20 7d 0a 0a 20 20  nActual;.  }..  
3722c 2f 2a 20 52 65 70 6c 61 63 65 20 77 72 69 74 74  /* Replace writt
3722d 65 6e 20 6c 65 6e 67 74 68 20 77 69 74 68 20 61  en length with a
3722e 63 74 75 61 6c 20 6c 65 6e 67 74 68 2e 20 2a 2f  ctual length. */
3722f 0a 20 20 6d 65 6d 63 70 79 28 70 57 72 69 74 65  .  memcpy(pWrite
37230 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44  r->data.pData+iD
37231 6f 63 6c 69 73 74 44 61 74 61 2c 20 63 2c 20 6e  oclistData, c, n
37232 41 63 74 75 61 6c 29 3b 0a 0a 20 20 2f 2a 20 49  Actual);..  /* I
37233 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20 74 6f  f the node is to
37234 6f 20 6c 61 72 67 65 2c 20 62 72 65 61 6b 20 74  o large, break t
37235 68 69 6e 67 73 20 75 70 2e 20 2a 2f 0a 20 20 2f  hings up. */.  /
37236 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68  * TODO(shess) Th
37237 69 73 20 74 65 73 74 20 6d 61 74 63 68 65 73 20  is test matches 
37238 6c 65 61 66 57 72 69 74 65 72 53 74 65 70 28 29  leafWriterStep()
37239 2c 20 77 68 69 63 68 20 64 6f 65 73 20 74 68 69  , which does thi
3723a 73 0a 20 20 2a 2a 20 74 65 73 74 20 62 65 66 6f  s.  ** test befo
3723b 72 65 20 69 74 20 6b 6e 6f 77 73 20 74 68 65 20  re it knows the 
3723c 63 6f 73 74 20 74 6f 20 76 61 72 69 6e 74 2d 65  cost to varint-e
3723d 6e 63 6f 64 65 20 74 68 65 20 74 65 72 6d 20 61  ncode the term a
3723e 6e 64 0a 20 20 2a 2a 20 64 6f 63 6c 69 73 74 20  nd.  ** doclist 
3723f 6c 65 6e 67 74 68 73 2e 20 20 41 74 20 73 6f 6d  lengths.  At som
37240 65 20 70 6f 69 6e 74 2c 20 63 68 61 6e 67 65 20  e point, change 
37241 74 6f 0a 20 20 2a 2a 20 70 57 72 69 74 65 72 2d  to.  ** pWriter-
37242 3e 64 61 74 61 2e 6e 44 61 74 61 3e 4c 45 41 46  >data.nData>LEAF
37243 5f 4d 41 58 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  _MAX..  */.  if(
37244 20 69 54 65 72 6d 44 61 74 61 2b 6e 54 65 72 6d   iTermData+nTerm
37245 2b 6e 41 63 74 75 61 6c 44 61 74 61 3e 4c 45 41  +nActualData>LEA
37246 46 5f 4d 41 58 20 29 7b 0a 20 20 20 20 2f 2a 20  F_MAX ){.    /* 
37247 46 6c 75 73 68 20 6f 75 74 20 74 68 65 20 6c 65  Flush out the le
37248 61 64 69 6e 67 20 64 61 74 61 20 61 73 20 61 20  ading data as a 
37249 6e 6f 64 65 20 2a 2f 0a 20 20 20 20 72 63 20 3d  node */.    rc =
3724a 20 6c 65 61 66 57 72 69 74 65 72 49 6e 74 65 72   leafWriterInter
3724b 6e 61 6c 46 6c 75 73 68 28 76 2c 20 70 57 72 69  nalFlush(v, pWri
3724c 74 65 72 2c 20 30 2c 20 69 54 65 72 6d 44 61 74  ter, 0, iTermDat
3724d 61 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  a);.    if( rc!=
3724e 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
3724f 72 6e 20 72 63 3b 0a 0a 20 20 20 20 70 57 72 69  rn rc;..    pWri
37250 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74 69 6e  ter->nTermDistin
37251 63 74 20 3d 20 6e 54 65 72 6d 44 69 73 74 69 6e  ct = nTermDistin
37252 63 74 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 62 75  ct;..    /* Rebu
37253 69 6c 64 20 68 65 61 64 65 72 20 75 73 69 6e 67  ild header using
37254 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 65 72   the current ter
37255 6d 20 2a 2f 0a 20 20 20 20 6e 20 3d 20 66 74 73  m */.    n = fts
37256 33 50 75 74 56 61 72 69 6e 74 28 70 57 72 69 74  3PutVarint(pWrit
37257 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2c 20  er->data.pData, 
37258 30 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73  0);.    n += fts
37259 33 50 75 74 56 61 72 69 6e 74 28 70 57 72 69 74  3PutVarint(pWrit
3725a 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 6e  er->data.pData+n
3725b 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20 20 6d 65  , nTerm);.    me
3725c 6d 63 70 79 28 70 57 72 69 74 65 72 2d 3e 64 61  mcpy(pWriter->da
3725d 74 61 2e 70 44 61 74 61 2b 6e 2c 20 70 54 65 72  ta.pData+n, pTer
3725e 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20 20 6e  m, nTerm);.    n
3725f 20 2b 3d 20 6e 54 65 72 6d 3b 0a 0a 20 20 20 20   += nTerm;..    
37260 2f 2a 20 54 68 65 72 65 20 73 68 6f 75 6c 64 20  /* There should 
37261 61 6c 77 61 79 73 20 62 65 20 72 6f 6f 6d 2c 20  always be room, 
37262 62 65 63 61 75 73 65 20 74 68 65 20 70 72 65 76  because the prev
37263 69 6f 75 73 20 65 6e 63 6f 64 69 6e 67 0a 20 20  ious encoding.  
37264 20 20 2a 2a 20 69 6e 63 6c 75 64 65 64 20 61 6c    ** included al
37265 6c 20 64 61 74 61 20 6e 65 63 65 73 73 61 72 79  l data necessary
37266 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 74 68   to construct th
37267 65 20 74 65 72 6d 2e 0a 20 20 20 20 2a 2f 0a 20  e term..    */. 
37268 20 20 20 61 73 73 65 72 74 28 20 6e 3c 69 44 6f     assert( n<iDo
37269 63 6c 69 73 74 44 61 74 61 20 29 3b 0a 20 20 20  clistData );.   
3726a 20 2f 2a 20 53 6f 20 6c 6f 6e 67 20 61 73 20 53   /* So long as S
3726b 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 69 73  TANDALONE_MIN is
3726c 20 68 61 6c 66 20 6f 72 20 6c 65 73 73 20 6f 66   half or less of
3726d 20 4c 45 41 46 5f 4d 41 58 2c 20 74 68 65 0a 20   LEAF_MAX, the. 
3726e 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20     ** following 
3726f 6d 65 6d 63 70 79 28 29 20 69 73 20 73 61 66 65  memcpy() is safe
37270 20 28 61 73 20 6f 70 70 6f 73 65 64 20 74 6f 20   (as opposed to 
37271 6e 65 65 64 69 6e 67 20 61 20 6d 65 6d 6d 6f 76  needing a memmov
37272 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  e)..    */.    a
37273 73 73 65 72 74 28 20 32 2a 53 54 41 4e 44 41 4c  ssert( 2*STANDAL
37274 4f 4e 45 5f 4d 49 4e 3c 3d 4c 45 41 46 5f 4d 41  ONE_MIN<=LEAF_MA
37275 58 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  X );.    assert(
37276 20 6e 2b 70 57 72 69 74 65 72 2d 3e 64 61 74 61   n+pWriter->data
37277 2e 6e 44 61 74 61 2d 69 44 6f 63 6c 69 73 74 44  .nData-iDoclistD
37278 61 74 61 3c 69 44 6f 63 6c 69 73 74 44 61 74 61  ata<iDoclistData
37279 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70   );.    memcpy(p
3727a 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61  Writer->data.pDa
3727b 74 61 2b 6e 2c 0a 20 20 20 20 20 20 20 20 20 20  ta+n,.          
3727c 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70   pWriter->data.p
3727d 44 61 74 61 2b 69 44 6f 63 6c 69 73 74 44 61 74  Data+iDoclistDat
3727e 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 57  a,.           pW
3727f 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
37280 61 2d 69 44 6f 63 6c 69 73 74 44 61 74 61 29 3b  a-iDoclistData);
37281 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61  .    pWriter->da
37282 74 61 2e 6e 44 61 74 61 20 2d 3d 20 69 44 6f 63  ta.nData -= iDoc
37283 6c 69 73 74 44 61 74 61 2d 6e 3b 0a 20 20 7d 0a  listData-n;.  }.
37284 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c    ASSERT_VALID_L
37285 45 41 46 5f 4e 4f 44 45 28 70 57 72 69 74 65 72  EAF_NODE(pWriter
37286 2d 3e 64 61 74 61 2e 70 44 61 74 61 2c 20 70 57  ->data.pData, pW
37287 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
37288 61 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51  a);..  return SQ
37289 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 50  LITE_OK;.}../* P
3728a 75 73 68 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d  ush pTerm[nTerm]
3728b 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20   along with the 
3728c 64 6f 63 6c 69 73 74 20 64 61 74 61 20 74 6f 20  doclist data to 
3728d 74 68 65 20 6c 65 61 66 20 6c 61 79 65 72 20 6f  the leaf layer o
3728e 66 0a 2a 2a 20 25 5f 73 65 67 6d 65 6e 74 73 2e  f.** %_segments.
3728f 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .*/./* TODO(shes
37290 73 29 20 52 65 76 69 73 65 20 77 72 69 74 65 5a  s) Revise writeZ
37291 65 72 6f 53 65 67 6d 65 6e 74 28 29 20 73 6f 20  eroSegment() so 
37292 74 68 61 74 20 64 6f 63 6c 69 73 74 73 20 61 72  that doclists ar
37293 65 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74 65 64  e.** constructed
37294 20 64 69 72 65 63 74 6c 79 20 69 6e 20 70 57 72   directly in pWr
37295 69 74 65 72 2d 3e 64 61 74 61 2e 0a 2a 2f 0a 73  iter->data..*/.s
37296 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57 72  tatic int leafWr
37297 69 74 65 72 53 74 65 70 28 66 75 6c 6c 74 65 78  iterStep(fulltex
37298 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61 66 57  t_vtab *v, LeafW
37299 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a  riter *pWriter,.
3729a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3729b 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
3729c 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74  char *pTerm, int
3729d 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20 20 20   nTerm,.        
3729e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3729f 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44    const char *pD
372a0 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 29 7b  ata, int nData){
372a1 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 44 4c 52  .  int rc;.  DLR
372a2 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a 0a 20  eader reader;.. 
372a3 20 64 6c 72 49 6e 69 74 28 26 72 65 61 64 65 72   dlrInit(&reader
372a4 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70 44  , DL_DEFAULT, pD
372a5 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 20 20 72  ata, nData);.  r
372a6 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 53 74  c = leafWriterSt
372a7 65 70 4d 65 72 67 65 28 76 2c 20 70 57 72 69 74  epMerge(v, pWrit
372a8 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d  er, pTerm, nTerm
372a9 2c 20 26 72 65 61 64 65 72 2c 20 31 29 3b 0a 20  , &reader, 1);. 
372aa 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72 65 61   dlrDestroy(&rea
372ab 64 65 72 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  der);..  return 
372ac 72 63 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  rc;.}.../*******
372ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
372ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
372af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
372b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 4c 65  *********/./* Le
372b1 61 66 52 65 61 64 65 72 20 69 73 20 75 73 65 64  afReader is used
372b2 20 74 6f 20 69 74 65 72 61 74 65 20 6f 76 65 72   to iterate over
372b3 20 61 6e 20 69 6e 64 69 76 69 64 75 61 6c 20 6c   an individual l
372b4 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a 74 79 70  eaf node. */.typ
372b5 65 64 65 66 20 73 74 72 75 63 74 20 4c 65 61 66  edef struct Leaf
372b6 52 65 61 64 65 72 20 7b 0a 20 20 44 61 74 61 42  Reader {.  DataB
372b7 75 66 66 65 72 20 74 65 72 6d 3b 20 20 20 20 20  uffer term;     
372b8 20 20 20 20 20 2f 2a 20 63 6f 70 79 20 6f 66 20       /* copy of 
372b9 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20 2a 2f  current term. */
372ba 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
372bb 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 2f 2a  pData;        /*
372bc 20 64 61 74 61 20 66 6f 72 20 63 75 72 72 65 6e   data for curren
372bd 74 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 69 6e 74  t term. */.  int
372be 20 6e 44 61 74 61 3b 0a 7d 20 4c 65 61 66 52 65   nData;.} LeafRe
372bf 61 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f  ader;..static vo
372c0 69 64 20 6c 65 61 66 52 65 61 64 65 72 44 65 73  id leafReaderDes
372c1 74 72 6f 79 28 4c 65 61 66 52 65 61 64 65 72 20  troy(LeafReader 
372c2 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 64 61 74  *pReader){.  dat
372c3 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
372c4 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 29 3b 0a  pReader->term);.
372c5 20 20 53 43 52 41 4d 42 4c 45 28 70 52 65 61 64    SCRAMBLE(pRead
372c6 65 72 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69  er);.}..static i
372c7 6e 74 20 6c 65 61 66 52 65 61 64 65 72 41 74 45  nt leafReaderAtE
372c8 6e 64 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70  nd(LeafReader *p
372c9 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72  Reader){.  retur
372ca 6e 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61  n pReader->nData
372cb 3c 3d 30 3b 0a 7d 0a 0a 2f 2a 20 41 63 63 65 73  <=0;.}../* Acces
372cc 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 65  s the current te
372cd 72 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  rm. */.static in
372ce 74 20 6c 65 61 66 52 65 61 64 65 72 54 65 72 6d  t leafReaderTerm
372cf 42 79 74 65 73 28 4c 65 61 66 52 65 61 64 65 72  Bytes(LeafReader
372d0 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65   *pReader){.  re
372d1 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 74 65  turn pReader->te
372d2 72 6d 2e 6e 44 61 74 61 3b 0a 7d 0a 73 74 61 74  rm.nData;.}.stat
372d3 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c  ic const char *l
372d4 65 61 66 52 65 61 64 65 72 54 65 72 6d 28 4c 65  eafReaderTerm(Le
372d5 61 66 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  afReader *pReade
372d6 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 52  r){.  assert( pR
372d7 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74  eader->term.nDat
372d8 61 3e 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  a>0 );.  return 
372d9 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 70 44  pReader->term.pD
372da 61 74 61 3b 0a 7d 0a 0a 2f 2a 20 41 63 63 65 73  ata;.}../* Acces
372db 73 20 74 68 65 20 64 6f 63 6c 69 73 74 20 64 61  s the doclist da
372dc 74 61 20 66 6f 72 20 74 68 65 20 63 75 72 72 65  ta for the curre
372dd 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 73 74 61 74  nt term. */.stat
372de 69 63 20 69 6e 74 20 6c 65 61 66 52 65 61 64 65  ic int leafReade
372df 72 44 61 74 61 42 79 74 65 73 28 4c 65 61 66 52  rDataBytes(LeafR
372e0 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
372e1 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a 20 20  .  int nData;.  
372e2 61 73 73 65 72 74 28 20 70 52 65 61 64 65 72 2d  assert( pReader-
372e3 3e 74 65 72 6d 2e 6e 44 61 74 61 3e 30 20 29 3b  >term.nData>0 );
372e4 0a 20 20 66 74 73 33 47 65 74 56 61 72 69 6e 74  .  fts3GetVarint
372e5 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74  32(pReader->pDat
372e6 61 2c 20 26 6e 44 61 74 61 29 3b 0a 20 20 72 65  a, &nData);.  re
372e7 74 75 72 6e 20 6e 44 61 74 61 3b 0a 7d 0a 73 74  turn nData;.}.st
372e8 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
372e9 2a 6c 65 61 66 52 65 61 64 65 72 44 61 74 61 28  *leafReaderData(
372ea 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52 65 61  LeafReader *pRea
372eb 64 65 72 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6e  der){.  int n, n
372ec 44 61 74 61 3b 0a 20 20 61 73 73 65 72 74 28 20  Data;.  assert( 
372ed 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44  pReader->term.nD
372ee 61 74 61 3e 30 20 29 3b 0a 20 20 6e 20 3d 20 66  ata>0 );.  n = f
372ef 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
372f0 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26  Reader->pData, &
372f1 6e 44 61 74 61 29 3b 0a 20 20 72 65 74 75 72 6e  nData);.  return
372f2 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b   pReader->pData+
372f3 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  n;.}..static voi
372f4 64 20 6c 65 61 66 52 65 61 64 65 72 49 6e 69 74  d leafReaderInit
372f5 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61  (const char *pDa
372f6 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20  ta, int nData,. 
372f7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
372f8 20 20 20 20 20 20 20 20 20 20 4c 65 61 66 52 65            LeafRe
372f9 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
372fa 20 20 69 6e 74 20 6e 54 65 72 6d 2c 20 6e 3b 0a    int nTerm, n;.
372fb 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61 74 61  .  assert( nData
372fc 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
372fd 70 44 61 74 61 5b 30 5d 3d 3d 27 5c 30 27 20 29  pData[0]=='\0' )
372fe 3b 0a 0a 20 20 43 4c 45 41 52 28 70 52 65 61 64  ;..  CLEAR(pRead
372ff 65 72 29 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20  er);..  /* Read 
37300 74 68 65 20 66 69 72 73 74 20 74 65 72 6d 2c 20  the first term, 
37301 73 6b 69 70 70 69 6e 67 20 74 68 65 20 68 65 61  skipping the hea
37302 64 65 72 20 62 79 74 65 2e 20 2a 2f 0a 20 20 6e  der byte. */.  n
37303 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
37304 33 32 28 70 44 61 74 61 2b 31 2c 20 26 6e 54 65  32(pData+1, &nTe
37305 72 6d 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  rm);.  dataBuffe
37306 72 49 6e 69 74 28 26 70 52 65 61 64 65 72 2d 3e  rInit(&pReader->
37307 74 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20  term, nTerm);.  
37308 64 61 74 61 42 75 66 66 65 72 52 65 70 6c 61 63  dataBufferReplac
37309 65 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72 6d  e(&pReader->term
3730a 2c 20 70 44 61 74 61 2b 31 2b 6e 2c 20 6e 54 65  , pData+1+n, nTe
3730b 72 6d 29 3b 0a 0a 20 20 2f 2a 20 50 6f 73 69 74  rm);..  /* Posit
3730c 69 6f 6e 20 61 66 74 65 72 20 74 68 65 20 66 69  ion after the fi
3730d 72 73 74 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 61  rst term. */.  a
3730e 73 73 65 72 74 28 20 31 2b 6e 2b 6e 54 65 72 6d  ssert( 1+n+nTerm
3730f 3c 6e 44 61 74 61 20 29 3b 0a 20 20 70 52 65 61  <nData );.  pRea
37310 64 65 72 2d 3e 70 44 61 74 61 20 3d 20 70 44 61  der->pData = pDa
37311 74 61 2b 31 2b 6e 2b 6e 54 65 72 6d 3b 0a 20 20  ta+1+n+nTerm;.  
37312 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d  pReader->nData =
37313 20 6e 44 61 74 61 2d 31 2d 6e 2d 6e 54 65 72 6d   nData-1-n-nTerm
37314 3b 0a 7d 0a 0a 2f 2a 20 53 74 65 70 20 74 68 65  ;.}../* Step the
37315 20 72 65 61 64 65 72 20 66 6f 72 77 61 72 64 20   reader forward 
37316 74 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d  to the next term
37317 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  . */.static void
37318 20 6c 65 61 66 52 65 61 64 65 72 53 74 65 70 28   leafReaderStep(
37319 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52 65 61  LeafReader *pRea
3731a 64 65 72 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6e  der){.  int n, n
3731b 44 61 74 61 2c 20 6e 50 72 65 66 69 78 2c 20 6e  Data, nPrefix, n
3731c 53 75 66 66 69 78 3b 0a 20 20 61 73 73 65 72 74  Suffix;.  assert
3731d 28 20 21 6c 65 61 66 52 65 61 64 65 72 41 74 45  ( !leafReaderAtE
3731e 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 0a  nd(pReader) );..
3731f 20 20 2f 2a 20 53 6b 69 70 20 70 72 65 76 69 6f    /* Skip previo
37320 75 73 20 65 6e 74 72 79 27 73 20 64 61 74 61 20  us entry's data 
37321 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6e 20 3d 20  block. */.  n = 
37322 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
37323 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20  pReader->pData, 
37324 26 6e 44 61 74 61 29 3b 0a 20 20 61 73 73 65 72  &nData);.  asser
37325 74 28 20 6e 2b 6e 44 61 74 61 3c 3d 70 52 65 61  t( n+nData<=pRea
37326 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20  der->nData );.  
37327 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 20 2b  pReader->pData +
37328 3d 20 6e 2b 6e 44 61 74 61 3b 0a 20 20 70 52 65  = n+nData;.  pRe
37329 61 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d 20 6e  ader->nData -= n
3732a 2b 6e 44 61 74 61 3b 0a 0a 20 20 69 66 28 20 21  +nData;..  if( !
3732b 6c 65 61 66 52 65 61 64 65 72 41 74 45 6e 64 28  leafReaderAtEnd(
3732c 70 52 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20  pReader) ){.    
3732d 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 74 68 65  /* Construct the
3732e 20 6e 65 77 20 74 65 72 6d 20 75 73 69 6e 67 20   new term using 
3732f 61 20 70 72 65 66 69 78 20 66 72 6f 6d 20 74 68  a prefix from th
37330 65 20 6f 6c 64 20 74 65 72 6d 20 70 6c 75 73 20  e old term plus 
37331 61 0a 20 20 20 20 2a 2a 20 73 75 66 66 69 78 20  a.    ** suffix 
37332 66 72 6f 6d 20 74 68 65 20 6c 65 61 66 20 64 61  from the leaf da
37333 74 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e  ta..    */.    n
37334 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
37335 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74  32(pReader->pDat
37336 61 2c 20 26 6e 50 72 65 66 69 78 29 3b 0a 20 20  a, &nPrefix);.  
37337 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61    n += fts3GetVa
37338 72 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e  rint32(pReader->
37339 70 44 61 74 61 2b 6e 2c 20 26 6e 53 75 66 66 69  pData+n, &nSuffi
3733a 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  x);.    assert( 
3733b 6e 2b 6e 53 75 66 66 69 78 3c 70 52 65 61 64 65  n+nSuffix<pReade
3733c 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 20 20  r->nData );.    
3733d 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44  pReader->term.nD
3733e 61 74 61 20 3d 20 6e 50 72 65 66 69 78 3b 0a 20  ata = nPrefix;. 
3733f 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70     dataBufferApp
37340 65 6e 64 28 26 70 52 65 61 64 65 72 2d 3e 74 65  end(&pReader->te
37341 72 6d 2c 20 70 52 65 61 64 65 72 2d 3e 70 44 61  rm, pReader->pDa
37342 74 61 2b 6e 2c 20 6e 53 75 66 66 69 78 29 3b 0a  ta+n, nSuffix);.
37343 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 70 44  .    pReader->pD
37344 61 74 61 20 2b 3d 20 6e 2b 6e 53 75 66 66 69 78  ata += n+nSuffix
37345 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 6e  ;.    pReader->n
37346 44 61 74 61 20 2d 3d 20 6e 2b 6e 53 75 66 66 69  Data -= n+nSuffi
37347 78 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 73 74 72  x;.  }.}../* str
37348 63 6d 70 2d 73 74 79 6c 65 20 63 6f 6d 70 61 72  cmp-style compar
37349 69 73 6f 6e 20 6f 66 20 70 52 65 61 64 65 72 27  ison of pReader'
3734a 73 20 63 75 72 72 65 6e 74 20 74 65 72 6d 20 61  s current term a
3734b 67 61 69 6e 73 74 20 70 54 65 72 6d 2e 0a 2a 2a  gainst pTerm..**
3734c 20 49 66 20 69 73 50 72 65 66 69 78 2c 20 65 71   If isPrefix, eq
3734d 75 61 6c 69 74 79 20 6d 65 61 6e 73 20 65 71 75  uality means equ
3734e 61 6c 20 74 68 72 6f 75 67 68 20 6e 54 65 72 6d  al through nTerm
3734f 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69   bytes..*/.stati
37350 63 20 69 6e 74 20 6c 65 61 66 52 65 61 64 65 72  c int leafReader
37351 54 65 72 6d 43 6d 70 28 4c 65 61 66 52 65 61 64  TermCmp(LeafRead
37352 65 72 20 2a 70 52 65 61 64 65 72 2c 0a 20 20 20  er *pReader,.   
37353 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37354 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
37355 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74  char *pTerm, int
37356 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50 72   nTerm, int isPr
37357 65 66 69 78 29 7b 0a 20 20 69 6e 74 20 63 2c 20  efix){.  int c, 
37358 6e 20 3d 20 70 52 65 61 64 65 72 2d 3e 74 65 72  n = pReader->ter
37359 6d 2e 6e 44 61 74 61 3c 6e 54 65 72 6d 20 3f 20  m.nData<nTerm ? 
3735a 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44  pReader->term.nD
3735b 61 74 61 20 3a 20 6e 54 65 72 6d 3b 0a 20 20 69  ata : nTerm;.  i
3735c 66 28 20 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69  f( n==0 ){.    i
3735d 66 28 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d  f( pReader->term
3735e 2e 6e 44 61 74 61 3e 30 20 29 20 72 65 74 75 72  .nData>0 ) retur
3735f 6e 20 2d 31 3b 0a 20 20 20 20 69 66 28 6e 54 65  n -1;.    if(nTe
37360 72 6d 3e 30 20 29 20 72 65 74 75 72 6e 20 31 3b  rm>0 ) return 1;
37361 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
37362 20 7d 0a 0a 20 20 63 20 3d 20 6d 65 6d 63 6d 70   }..  c = memcmp
37363 28 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 70  (pReader->term.p
37364 44 61 74 61 2c 20 70 54 65 72 6d 2c 20 6e 29 3b  Data, pTerm, n);
37365 0a 20 20 69 66 28 20 63 21 3d 30 20 29 20 72 65  .  if( c!=0 ) re
37366 74 75 72 6e 20 63 3b 0a 20 20 69 66 28 20 69 73  turn c;.  if( is
37367 50 72 65 66 69 78 20 26 26 20 6e 3d 3d 6e 54 65  Prefix && n==nTe
37368 72 6d 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  rm ) return 0;. 
37369 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d   return pReader-
3736a 3e 74 65 72 6d 2e 6e 44 61 74 61 20 2d 20 6e 54  >term.nData - nT
3736b 65 72 6d 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a  erm;.}.../******
3736c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3736d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3736e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3736f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 4c  **********/./* L
37370 65 61 76 65 73 52 65 61 64 65 72 20 77 72 61 70  eavesReader wrap
37371 73 20 4c 65 61 66 52 65 61 64 65 72 20 74 6f 20  s LeafReader to 
37372 61 6c 6c 6f 77 20 69 74 65 72 61 74 69 6e 67 20  allow iterating 
37373 6f 76 65 72 20 74 68 65 20 65 6e 74 69 72 65 0a  over the entire.
37374 2a 2a 20 6c 65 61 66 20 6c 61 79 65 72 20 6f 66  ** leaf layer of
37375 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 74 79   the tree..*/.ty
37376 70 65 64 65 66 20 73 74 72 75 63 74 20 4c 65 61  pedef struct Lea
37377 76 65 73 52 65 61 64 65 72 20 7b 0a 20 20 69 6e  vesReader {.  in
37378 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20  t idx;          
37379 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
3737a 20 77 69 74 68 69 6e 20 74 68 65 20 73 65 67 6d   within the segm
3737b 65 6e 74 2e 20 2a 2f 0a 0a 20 20 73 71 6c 69 74  ent. */..  sqlit
3737c 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20  e3_stmt *pStmt; 
3737d 20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e       /* Statemen
3737e 74 20 77 65 27 72 65 20 73 74 72 65 61 6d 69 6e  t we're streamin
3737f 67 20 6c 65 61 76 65 73 20 66 72 6f 6d 2e 20 2a  g leaves from. *
37380 2f 0a 20 20 69 6e 74 20 65 6f 66 3b 20 20 20 20  /.  int eof;    
37381 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
37382 20 77 65 27 76 65 20 73 65 65 6e 20 53 51 4c 49   we've seen SQLI
37383 54 45 5f 44 4f 4e 45 20 66 72 6f 6d 20 70 53 74  TE_DONE from pSt
37384 6d 74 2e 20 2a 2f 0a 0a 20 20 4c 65 61 66 52 65  mt. */..  LeafRe
37385 61 64 65 72 20 6c 65 61 66 52 65 61 64 65 72 3b  ader leafReader;
37386 20 20 20 20 2f 2a 20 72 65 61 64 65 72 20 66 6f      /* reader fo
37387 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 65  r the current le
37388 61 66 2e 20 2a 2f 0a 20 20 44 61 74 61 42 75 66  af. */.  DataBuf
37389 66 65 72 20 72 6f 6f 74 44 61 74 61 3b 20 20 20  fer rootData;   
3738a 20 20 20 2f 2a 20 72 6f 6f 74 20 64 61 74 61 20     /* root data 
3738b 66 6f 72 20 69 6e 6c 69 6e 65 2e 20 2a 2f 0a 7d  for inline. */.}
3738c 20 4c 65 61 76 65 73 52 65 61 64 65 72 3b 0a 0a   LeavesReader;..
3738d 2f 2a 20 41 63 63 65 73 73 20 74 68 65 20 63 75  /* Access the cu
3738e 72 72 65 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 73  rrent term. */.s
3738f 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73  tatic int leaves
37390 52 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28  ReaderTermBytes(
37391 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52  LeavesReader *pR
37392 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74  eader){.  assert
37393 28 20 21 70 52 65 61 64 65 72 2d 3e 65 6f 66 20  ( !pReader->eof 
37394 29 3b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 66  );.  return leaf
37395 52 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28  ReaderTermBytes(
37396 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52 65  &pReader->leafRe
37397 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20  ader);.}.static 
37398 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 65 61 76  const char *leav
37399 65 73 52 65 61 64 65 72 54 65 72 6d 28 4c 65 61  esReaderTerm(Lea
3739a 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
3739b 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21  er){.  assert( !
3739c 70 52 65 61 64 65 72 2d 3e 65 6f 66 20 29 3b 0a  pReader->eof );.
3739d 20 20 72 65 74 75 72 6e 20 6c 65 61 66 52 65 61    return leafRea
3739e 64 65 72 54 65 72 6d 28 26 70 52 65 61 64 65 72  derTerm(&pReader
3739f 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 7d  ->leafReader);.}
373a0 0a 0a 2f 2a 20 41 63 63 65 73 73 20 74 68 65 20  ../* Access the 
373a1 64 6f 63 6c 69 73 74 20 64 61 74 61 20 66 6f 72  doclist data for
373a2 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 65 72   the current ter
373a3 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  m. */.static int
373a4 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 61 74   leavesReaderDat
373a5 61 42 79 74 65 73 28 4c 65 61 76 65 73 52 65 61  aBytes(LeavesRea
373a6 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
373a7 20 61 73 73 65 72 74 28 20 21 70 52 65 61 64 65   assert( !pReade
373a8 72 2d 3e 65 6f 66 20 29 3b 0a 20 20 72 65 74 75  r->eof );.  retu
373a9 72 6e 20 6c 65 61 66 52 65 61 64 65 72 44 61 74  rn leafReaderDat
373aa 61 42 79 74 65 73 28 26 70 52 65 61 64 65 72 2d  aBytes(&pReader-
373ab 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 7d 0a  >leafReader);.}.
373ac 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
373ad 72 20 2a 6c 65 61 76 65 73 52 65 61 64 65 72 44  r *leavesReaderD
373ae 61 74 61 28 4c 65 61 76 65 73 52 65 61 64 65 72  ata(LeavesReader
373af 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73   *pReader){.  as
373b0 73 65 72 74 28 20 21 70 52 65 61 64 65 72 2d 3e  sert( !pReader->
373b1 65 6f 66 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  eof );.  return 
373b2 6c 65 61 66 52 65 61 64 65 72 44 61 74 61 28 26  leafReaderData(&
373b3 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52 65 61  pReader->leafRea
373b4 64 65 72 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  der);.}..static 
373b5 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65 72  int leavesReader
373b6 41 74 45 6e 64 28 4c 65 61 76 65 73 52 65 61 64  AtEnd(LeavesRead
373b7 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
373b8 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
373b9 65 6f 66 3b 0a 7d 0a 0a 2f 2a 20 6c 6f 61 64 53  eof;.}../* loadS
373ba 65 67 6d 65 6e 74 4c 65 61 76 65 73 28 29 20 6d  egmentLeaves() m
373bb 61 79 20 6e 6f 74 20 72 65 61 64 20 61 6c 6c 20  ay not read all 
373bc 74 68 65 20 77 61 79 20 74 6f 20 53 51 4c 49 54  the way to SQLIT
373bd 45 5f 44 4f 4e 45 2c 20 74 68 75 73 0a 2a 2a 20  E_DONE, thus.** 
373be 6c 65 61 76 69 6e 67 20 74 68 65 20 73 74 61 74  leaving the stat
373bf 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 6f 70 65  ement handle ope
373c0 6e 2c 20 77 68 69 63 68 20 6c 6f 63 6b 73 20 74  n, which locks t
373c1 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20  he table..*/./* 
373c2 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68 69 73  TODO(shess) This
373c3 20 22 73 6f 6c 75 74 69 6f 6e 22 20 69 73 20 6e   "solution" is n
373c4 6f 74 20 73 61 74 69 73 66 61 63 74 6f 72 79 2e  ot satisfactory.
373c5 20 20 52 65 61 6c 6c 79 2c 20 74 68 65 72 65 0a    Really, there.
373c6 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 68 65  ** should be che
373c7 63 6b 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 20 66  ck-in function f
373c8 6f 72 20 61 6c 6c 20 73 74 61 74 65 6d 65 6e 74  or all statement
373c9 20 68 61 6e 64 6c 65 73 20 77 68 69 63 68 0a 2a   handles which.*
373ca 2a 20 61 72 72 61 6e 67 65 73 20 74 6f 20 63 61  * arranges to ca
373cb 6c 6c 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74  ll sqlite3_reset
373cc 28 29 2e 20 20 54 68 69 73 20 6d 6f 73 74 20 6c  ().  This most l
373cd 69 6b 65 6c 79 20 77 69 6c 6c 20 72 65 71 75 69  ikely will requi
373ce 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 63 61 74 69  re.** modificati
373cf 6f 6e 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 66 6c  on to control fl
373d0 6f 77 20 61 6c 6c 20 6f 76 65 72 20 74 68 65 20  ow all over the 
373d1 70 6c 61 63 65 2c 20 74 68 6f 75 67 68 2c 20 73  place, though, s
373d2 6f 20 66 6f 72 20 6e 6f 77 0a 2a 2a 20 6a 75 73  o for now.** jus
373d3 74 20 70 75 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  t punt..**.** No
373d4 74 65 20 74 68 65 20 74 68 65 20 63 75 72 72 65  te the the curre
373d5 6e 74 20 73 79 73 74 65 6d 20 61 73 73 75 6d 65  nt system assume
373d6 73 20 74 68 61 74 20 73 65 67 6d 65 6e 74 20 6d  s that segment m
373d7 65 72 67 65 73 20 77 69 6c 6c 20 72 75 6e 20 74  erges will run t
373d8 6f 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 2c  o.** completion,
373d9 20 77 68 69 63 68 20 69 73 20 77 68 79 20 74 68   which is why th
373da 69 73 20 70 61 72 74 69 63 75 6c 61 72 20 70 72  is particular pr
373db 6f 62 61 62 6c 79 20 68 61 73 6e 27 74 20 61 72  obably hasn't ar
373dc 69 73 65 6e 20 69 6e 0a 2a 2a 20 74 68 69 73 20  isen in.** this 
373dd 63 61 73 65 2e 20 20 50 72 6f 62 61 62 6c 79 20  case.  Probably 
373de 61 20 62 72 69 74 74 6c 65 20 61 73 73 75 6d 70  a brittle assump
373df 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
373e0 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65 72  int leavesReader
373e1 52 65 73 65 74 28 4c 65 61 76 65 73 52 65 61 64  Reset(LeavesRead
373e2 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
373e3 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 72  return sqlite3_r
373e4 65 73 65 74 28 70 52 65 61 64 65 72 2d 3e 70 53  eset(pReader->pS
373e5 74 6d 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  tmt);.}..static 
373e6 76 6f 69 64 20 6c 65 61 76 65 73 52 65 61 64 65  void leavesReade
373e7 72 44 65 73 74 72 6f 79 28 4c 65 61 76 65 73 52  rDestroy(LeavesR
373e8 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
373e9 0a 20 20 2f 2a 20 49 66 20 69 64 78 20 69 73 20  .  /* If idx is 
373ea 2d 31 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 77  -1, that means w
373eb 65 27 72 65 20 75 73 69 6e 67 20 61 20 6e 6f 6e  e're using a non
373ec 2d 63 61 63 68 65 64 20 73 74 61 74 65 6d 65 6e  -cached statemen
373ed 74 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20 69 6e  t.  ** handle in
373ee 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 28 29 20   the optimize() 
373ef 63 61 73 65 2c 20 73 6f 20 77 65 20 6e 65 65 64  case, so we need
373f0 20 74 6f 20 72 65 6c 65 61 73 65 20 69 74 2e 0a   to release it..
373f1 20 20 2a 2f 0a 20 20 69 66 28 20 70 52 65 61 64    */.  if( pRead
373f2 65 72 2d 3e 70 53 74 6d 74 21 3d 4e 55 4c 4c 20  er->pStmt!=NULL 
373f3 26 26 20 70 52 65 61 64 65 72 2d 3e 69 64 78 3d  && pReader->idx=
373f4 3d 2d 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  =-1 ){.    sqlit
373f5 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 65 61  e3_finalize(pRea
373f6 64 65 72 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 7d  der->pStmt);.  }
373f7 0a 20 20 6c 65 61 66 52 65 61 64 65 72 44 65 73  .  leafReaderDes
373f8 74 72 6f 79 28 26 70 52 65 61 64 65 72 2d 3e 6c  troy(&pReader->l
373f9 65 61 66 52 65 61 64 65 72 29 3b 0a 20 20 64 61  eafReader);.  da
373fa 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
373fb 26 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61  &pReader->rootDa
373fc 74 61 29 3b 0a 20 20 53 43 52 41 4d 42 4c 45 28  ta);.  SCRAMBLE(
373fd 70 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 2f 2a 20  pReader);.}../* 
373fe 49 6e 69 74 69 61 6c 69 7a 65 20 70 52 65 61 64  Initialize pRead
373ff 65 72 20 77 69 74 68 20 74 68 65 20 67 69 76 65  er with the give
37400 6e 20 72 6f 6f 74 20 64 61 74 61 20 28 69 66 20  n root data (if 
37401 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 3d 3d 30  iStartBlockid==0
37402 0a 2a 2a 20 74 68 65 20 6c 65 61 66 20 64 61 74  .** the leaf dat
37403 61 20 77 61 73 20 65 6e 74 69 72 65 6c 79 20 63  a was entirely c
37404 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20  ontained in the 
37405 72 6f 6f 74 29 2c 20 6f 72 20 66 72 6f 6d 20 74  root), or from t
37406 68 65 0a 2a 2a 20 73 74 72 65 61 6d 20 6f 66 20  he.** stream of 
37407 62 6c 6f 63 6b 73 20 62 65 74 77 65 65 6e 20 69  blocks between i
37408 53 74 61 72 74 42 6c 6f 63 6b 69 64 20 61 6e 64  StartBlockid and
37409 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 20 69 6e   iEndBlockid, in
3740a 63 6c 75 73 69 76 65 2e 0a 2a 2f 0a 73 74 61 74  clusive..*/.stat
3740b 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61  ic int leavesRea
3740c 64 65 72 49 6e 69 74 28 66 75 6c 6c 74 65 78 74  derInit(fulltext
3740d 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20 20 20 20  _vtab *v,.      
3740e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3740f 20 20 20 20 20 20 69 6e 74 20 69 64 78 2c 0a 20        int idx,. 
37410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37411 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
37412 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42 6c  e_int64 iStartBl
37413 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20  ockid,.         
37414 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37415 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
37416 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 0a 20 20 20  iEndBlockid,.   
37417 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37418 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
37419 68 61 72 20 2a 70 52 6f 6f 74 44 61 74 61 2c 20  har *pRootData, 
3741a 69 6e 74 20 6e 52 6f 6f 74 44 61 74 61 2c 0a 20  int nRootData,. 
3741b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3741c 20 20 20 20 20 20 20 20 20 20 20 4c 65 61 76 65             Leave
3741d 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  sReader *pReader
3741e 29 7b 0a 20 20 43 4c 45 41 52 28 70 52 65 61 64  ){.  CLEAR(pRead
3741f 65 72 29 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  er);.  pReader->
37420 69 64 78 20 3d 20 69 64 78 3b 0a 0a 20 20 64 61  idx = idx;..  da
37421 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 70 52  taBufferInit(&pR
37422 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61 74 61 2c  eader->rootData,
37423 20 30 29 3b 0a 20 20 69 66 28 20 69 53 74 61 72   0);.  if( iStar
37424 74 42 6c 6f 63 6b 69 64 3d 3d 30 20 29 7b 0a 20  tBlockid==0 ){. 
37425 20 20 20 2f 2a 20 45 6e 74 69 72 65 20 6c 65 61     /* Entire lea
37426 66 20 6c 65 76 65 6c 20 66 69 74 20 69 6e 20 72  f level fit in r
37427 6f 6f 74 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20  oot data. */.   
37428 20 64 61 74 61 42 75 66 66 65 72 52 65 70 6c 61   dataBufferRepla
37429 63 65 28 26 70 52 65 61 64 65 72 2d 3e 72 6f 6f  ce(&pReader->roo
3742a 74 44 61 74 61 2c 20 70 52 6f 6f 74 44 61 74 61  tData, pRootData
3742b 2c 20 6e 52 6f 6f 74 44 61 74 61 29 3b 0a 20 20  , nRootData);.  
3742c 20 20 6c 65 61 66 52 65 61 64 65 72 49 6e 69 74    leafReaderInit
3742d 28 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61  (pReader->rootDa
3742e 74 61 2e 70 44 61 74 61 2c 20 70 52 65 61 64 65  ta.pData, pReade
3742f 72 2d 3e 72 6f 6f 74 44 61 74 61 2e 6e 44 61 74  r->rootData.nDat
37430 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
37431 20 20 20 20 20 20 26 70 52 65 61 64 65 72 2d 3e        &pReader->
37432 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20 20 7d  leafReader);.  }
37433 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
37434 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 20 20 69  3_stmt *s;.    i
37435 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  nt rc = sql_get_
37436 6c 65 61 66 5f 73 74 61 74 65 6d 65 6e 74 28 76  leaf_statement(v
37437 2c 20 69 64 78 2c 20 26 73 29 3b 0a 20 20 20 20  , idx, &s);.    
37438 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
37439 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
3743a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
3743b 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31  _bind_int64(s, 1
3743c 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 29  , iStartBlockid)
3743d 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
3743e 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
3743f 20 72 63 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73   rc;..    rc = s
37440 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
37441 34 28 73 2c 20 32 2c 20 69 45 6e 64 42 6c 6f 63  4(s, 2, iEndBloc
37442 6b 69 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63  kid);.    if( rc
37443 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
37444 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 72 63  turn rc;..    rc
37445 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
37446 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  s);.    if( rc==
37447 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20  SQLITE_DONE ){. 
37448 20 20 20 20 20 70 52 65 61 64 65 72 2d 3e 65 6f       pReader->eo
37449 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74  f = 1;.      ret
3744a 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
3744b 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21     }.    if( rc!
3744c 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65  =SQLITE_ROW ) re
3744d 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 70 52  turn rc;..    pR
3744e 65 61 64 65 72 2d 3e 70 53 74 6d 74 20 3d 20 73  eader->pStmt = s
3744f 3b 0a 20 20 20 20 6c 65 61 66 52 65 61 64 65 72  ;.    leafReader
37450 49 6e 69 74 28 73 71 6c 69 74 65 33 5f 63 6f 6c  Init(sqlite3_col
37451 75 6d 6e 5f 62 6c 6f 62 28 70 52 65 61 64 65 72  umn_blob(pReader
37452 2d 3e 70 53 74 6d 74 2c 20 30 29 2c 0a 20 20 20  ->pStmt, 0),.   
37453 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37454 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
37455 79 74 65 73 28 70 52 65 61 64 65 72 2d 3e 70 53  ytes(pReader->pS
37456 74 6d 74 2c 20 30 29 2c 0a 20 20 20 20 20 20 20  tmt, 0),.       
37457 20 20 20 20 20 20 20 20 20 20 20 20 26 70 52 65              &pRe
37458 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72  ader->leafReader
37459 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
3745a 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
3745b 20 53 74 65 70 20 74 68 65 20 63 75 72 72 65 6e   Step the curren
3745c 74 20 6c 65 61 66 20 66 6f 72 77 61 72 64 20 74  t leaf forward t
3745d 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d 2e  o the next term.
3745e 20 20 49 66 20 77 65 20 72 65 61 63 68 20 74 68    If we reach th
3745f 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65 20  e.** end of the 
37460 63 75 72 72 65 6e 74 20 6c 65 61 66 2c 20 73 74  current leaf, st
37461 65 70 20 66 6f 72 77 61 72 64 20 74 6f 20 74 68  ep forward to th
37462 65 20 6e 65 78 74 20 6c 65 61 66 20 62 6c 6f 63  e next leaf bloc
37463 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  k..*/.static int
37464 20 6c 65 61 76 65 73 52 65 61 64 65 72 53 74 65   leavesReaderSte
37465 70 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  p(fulltext_vtab 
37466 2a 76 2c 20 4c 65 61 76 65 73 52 65 61 64 65 72  *v, LeavesReader
37467 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73   *pReader){.  as
37468 73 65 72 74 28 20 21 6c 65 61 76 65 73 52 65 61  sert( !leavesRea
37469 64 65 72 41 74 45 6e 64 28 70 52 65 61 64 65 72  derAtEnd(pReader
3746a 29 20 29 3b 0a 20 20 6c 65 61 66 52 65 61 64 65  ) );.  leafReade
3746b 72 53 74 65 70 28 26 70 52 65 61 64 65 72 2d 3e  rStep(&pReader->
3746c 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 0a 20 20  leafReader);..  
3746d 69 66 28 20 6c 65 61 66 52 65 61 64 65 72 41 74  if( leafReaderAt
3746e 45 6e 64 28 26 70 52 65 61 64 65 72 2d 3e 6c 65  End(&pReader->le
3746f 61 66 52 65 61 64 65 72 29 20 29 7b 0a 20 20 20  afReader) ){.   
37470 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 69 66 28   int rc;.    if(
37471 20 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61   pReader->rootDa
37472 74 61 2e 70 44 61 74 61 20 29 7b 0a 20 20 20 20  ta.pData ){.    
37473 20 20 70 52 65 61 64 65 72 2d 3e 65 6f 66 20 3d    pReader->eof =
37474 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   1;.      return
37475 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
37476 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  }.    rc = sqlit
37477 65 33 5f 73 74 65 70 28 70 52 65 61 64 65 72 2d  e3_step(pReader-
37478 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 69 66 28  >pStmt);.    if(
37479 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20   rc!=SQLITE_ROW 
3747a 29 7b 0a 20 20 20 20 20 20 70 52 65 61 64 65 72  ){.      pReader
3747b 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20  ->eof = 1;.     
3747c 20 72 65 74 75 72 6e 20 72 63 3d 3d 53 51 4c 49   return rc==SQLI
3747d 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c 49 54 45  TE_DONE ? SQLITE
3747e 5f 4f 4b 20 3a 20 72 63 3b 0a 20 20 20 20 7d 0a  _OK : rc;.    }.
3747f 20 20 20 20 6c 65 61 66 52 65 61 64 65 72 44 65      leafReaderDe
37480 73 74 72 6f 79 28 26 70 52 65 61 64 65 72 2d 3e  stroy(&pReader->
37481 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20 20 20  leafReader);.   
37482 20 6c 65 61 66 52 65 61 64 65 72 49 6e 69 74 28   leafReaderInit(
37483 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
37484 6c 6f 62 28 70 52 65 61 64 65 72 2d 3e 70 53 74  lob(pReader->pSt
37485 6d 74 2c 20 30 29 2c 0a 20 20 20 20 20 20 20 20  mt, 0),.        
37486 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
37487 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
37488 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 2c 20  pReader->pStmt, 
37489 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  0),.            
3748a 20 20 20 20 20 20 20 26 70 52 65 61 64 65 72 2d         &pReader-
3748b 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20 20  >leafReader);.  
3748c 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
3748d 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4f 72 64 65  E_OK;.}../* Orde
3748e 72 20 4c 65 61 76 65 73 52 65 61 64 65 72 73 20  r LeavesReaders 
3748f 62 79 20 74 68 65 69 72 20 74 65 72 6d 2c 20 69  by their term, i
37490 67 6e 6f 72 69 6e 67 20 69 64 78 2e 20 20 52 65  gnoring idx.  Re
37491 61 64 65 72 73 20 61 74 20 65 6f 66 0a 2a 2a 20  aders at eof.** 
37492 61 6c 77 61 79 73 20 73 6f 72 74 20 74 6f 20 74  always sort to t
37493 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69  he end..*/.stati
37494 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64  c int leavesRead
37495 65 72 54 65 72 6d 43 6d 70 28 4c 65 61 76 65 73  erTermCmp(Leaves
37496 52 65 61 64 65 72 20 2a 6c 72 31 2c 20 4c 65 61  Reader *lr1, Lea
37497 76 65 73 52 65 61 64 65 72 20 2a 6c 72 32 29 7b  vesReader *lr2){
37498 0a 20 20 69 66 28 20 6c 65 61 76 65 73 52 65 61  .  if( leavesRea
37499 64 65 72 41 74 45 6e 64 28 6c 72 31 29 20 29 7b  derAtEnd(lr1) ){
3749a 0a 20 20 20 20 69 66 28 20 6c 65 61 76 65 73 52  .    if( leavesR
3749b 65 61 64 65 72 41 74 45 6e 64 28 6c 72 32 29 20  eaderAtEnd(lr2) 
3749c 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
3749d 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20  return 1;.  }.  
3749e 69 66 28 20 6c 65 61 76 65 73 52 65 61 64 65 72  if( leavesReader
3749f 41 74 45 6e 64 28 6c 72 32 29 20 29 20 72 65 74  AtEnd(lr2) ) ret
374a0 75 72 6e 20 2d 31 3b 0a 0a 20 20 72 65 74 75 72  urn -1;..  retur
374a1 6e 20 6c 65 61 66 52 65 61 64 65 72 54 65 72 6d  n leafReaderTerm
374a2 43 6d 70 28 26 6c 72 31 2d 3e 6c 65 61 66 52 65  Cmp(&lr1->leafRe
374a3 61 64 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20  ader,.          
374a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
374a5 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72   leavesReaderTer
374a6 6d 28 6c 72 32 29 2c 20 6c 65 61 76 65 73 52 65  m(lr2), leavesRe
374a7 61 64 65 72 54 65 72 6d 42 79 74 65 73 28 6c 72  aderTermBytes(lr
374a8 32 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  2),.            
374a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
374aa 29 3b 0a 7d 0a 0a 2f 2a 20 53 69 6d 69 6c 61 72  );.}../* Similar
374ab 20 74 6f 20 6c 65 61 76 65 73 52 65 61 64 65 72   to leavesReader
374ac 54 65 72 6d 43 6d 70 28 29 2c 20 77 69 74 68 20  TermCmp(), with 
374ad 61 64 64 69 74 69 6f 6e 61 6c 20 6f 72 64 65 72  additional order
374ae 69 6e 67 20 62 79 20 69 64 78 0a 2a 2a 20 73 6f  ing by idx.** so
374af 20 74 68 61 74 20 6f 6c 64 65 72 20 73 65 67 6d   that older segm
374b0 65 6e 74 73 20 73 6f 72 74 20 62 65 66 6f 72 65  ents sort before
374b1 20 6e 65 77 65 72 20 73 65 67 6d 65 6e 74 73 2e   newer segments.
374b2 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
374b3 65 61 76 65 73 52 65 61 64 65 72 43 6d 70 28 4c  eavesReaderCmp(L
374b4 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72 31  eavesReader *lr1
374b5 2c 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  , LeavesReader *
374b6 6c 72 32 29 7b 0a 20 20 69 6e 74 20 63 20 3d 20  lr2){.  int c = 
374b7 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  leavesReaderTerm
374b8 43 6d 70 28 6c 72 31 2c 20 6c 72 32 29 3b 0a 20  Cmp(lr1, lr2);. 
374b9 20 69 66 28 20 63 21 3d 30 20 29 20 72 65 74 75   if( c!=0 ) retu
374ba 72 6e 20 63 3b 0a 20 20 72 65 74 75 72 6e 20 6c  rn c;.  return l
374bb 72 31 2d 3e 69 64 78 2d 6c 72 32 2d 3e 69 64 78  r1->idx-lr2->idx
374bc 3b 0a 7d 0a 0a 2f 2a 20 41 73 73 75 6d 65 20 74  ;.}../* Assume t
374bd 68 61 74 20 70 4c 72 5b 31 5d 2e 2e 70 4c 72 5b  hat pLr[1]..pLr[
374be 6e 4c 72 5d 20 61 72 65 20 73 6f 72 74 65 64 2e  nLr] are sorted.
374bf 20 20 42 75 62 62 6c 65 20 70 4c 72 5b 30 5d 20    Bubble pLr[0] 
374c0 69 6e 74 6f 20 69 74 73 0a 2a 2a 20 73 6f 72 74  into its.** sort
374c1 65 64 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a  ed position..*/.
374c2 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65 61 76  static void leav
374c3 65 73 52 65 61 64 65 72 52 65 6f 72 64 65 72 28  esReaderReorder(
374c4 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 4c  LeavesReader *pL
374c5 72 2c 20 69 6e 74 20 6e 4c 72 29 7b 0a 20 20 77  r, int nLr){.  w
374c6 68 69 6c 65 28 20 6e 4c 72 3e 31 20 26 26 20 6c  hile( nLr>1 && l
374c7 65 61 76 65 73 52 65 61 64 65 72 43 6d 70 28 70  eavesReaderCmp(p
374c8 4c 72 2c 20 70 4c 72 2b 31 29 3e 30 20 29 7b 0a  Lr, pLr+1)>0 ){.
374c9 20 20 20 20 4c 65 61 76 65 73 52 65 61 64 65 72      LeavesReader
374ca 20 74 6d 70 20 3d 20 70 4c 72 5b 30 5d 3b 0a 20   tmp = pLr[0];. 
374cb 20 20 20 70 4c 72 5b 30 5d 20 3d 20 70 4c 72 5b     pLr[0] = pLr[
374cc 31 5d 3b 0a 20 20 20 20 70 4c 72 5b 31 5d 20 3d  1];.    pLr[1] =
374cd 20 74 6d 70 3b 0a 20 20 20 20 6e 4c 72 2d 2d 3b   tmp;.    nLr--;
374ce 0a 20 20 20 20 70 4c 72 2b 2b 3b 0a 20 20 7d 0a  .    pLr++;.  }.
374cf 7d 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65  }../* Initialize
374d0 73 20 70 52 65 61 64 65 72 73 20 77 69 74 68 20  s pReaders with 
374d1 74 68 65 20 73 65 67 6d 65 6e 74 73 20 66 72 6f  the segments fro
374d2 6d 20 6c 65 76 65 6c 20 69 4c 65 76 65 6c 2c 20  m level iLevel, 
374d3 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 74 68 65  returning.** the
374d4 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 67 6d 65   number of segme
374d5 6e 74 73 20 69 6e 20 2a 70 69 52 65 61 64 65 72  nts in *piReader
374d6 73 2e 20 20 4c 65 61 76 65 73 20 70 52 65 61 64  s.  Leaves pRead
374d7 65 72 73 20 69 6e 20 73 6f 72 74 65 64 0a 2a 2a  ers in sorted.**
374d8 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69   order..*/.stati
374d9 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64  c int leavesRead
374da 65 72 73 49 6e 69 74 28 66 75 6c 6c 74 65 78 74  ersInit(fulltext
374db 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c  _vtab *v, int iL
374dc 65 76 65 6c 2c 0a 20 20 20 20 20 20 20 20 20 20  evel,.          
374dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
374de 20 20 20 4c 65 61 76 65 73 52 65 61 64 65 72 20     LeavesReader 
374df 2a 70 52 65 61 64 65 72 73 2c 20 69 6e 74 20 2a  *pReaders, int *
374e0 70 69 52 65 61 64 65 72 73 29 7b 0a 20 20 73 71  piReaders){.  sq
374e1 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20  lite3_stmt *s;. 
374e2 20 69 6e 74 20 69 2c 20 72 63 20 3d 20 73 71 6c   int i, rc = sql
374e3 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
374e4 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f  , SEGDIR_SELECT_
374e5 4c 45 56 45 4c 5f 53 54 4d 54 2c 20 26 73 29 3b  LEVEL_STMT, &s);
374e6 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
374e7 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
374e8 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
374e9 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c  3_bind_int(s, 1,
374ea 20 69 4c 65 76 65 6c 29 3b 0a 20 20 69 66 28 20   iLevel);.  if( 
374eb 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
374ec 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 69 20  return rc;..  i 
374ed 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 28 72  = 0;.  while( (r
374ee 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
374ef 28 73 29 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  (s))==SQLITE_ROW
374f0 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69   ){.    sqlite_i
374f1 6e 74 36 34 20 69 53 74 61 72 74 20 3d 20 73 71  nt64 iStart = sq
374f2 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
374f3 36 34 28 73 2c 20 30 29 3b 0a 20 20 20 20 73 71  64(s, 0);.    sq
374f4 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 20  lite_int64 iEnd 
374f5 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
374f6 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b 0a 20 20  _int64(s, 1);.  
374f7 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52    const char *pR
374f8 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69 74 65  ootData = sqlite
374f9 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 2c  3_column_blob(s,
374fa 20 32 29 3b 0a 20 20 20 20 69 6e 74 20 6e 52 6f   2);.    int nRo
374fb 6f 74 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33  otData = sqlite3
374fc 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 2c  _column_bytes(s,
374fd 20 32 29 3b 0a 0a 20 20 20 20 61 73 73 65 72 74   2);..    assert
374fe 28 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 20  ( i<MERGE_COUNT 
374ff 29 3b 0a 20 20 20 20 72 63 20 3d 20 6c 65 61 76  );.    rc = leav
37500 65 73 52 65 61 64 65 72 49 6e 69 74 28 76 2c 20  esReaderInit(v, 
37501 69 2c 20 69 53 74 61 72 74 2c 20 69 45 6e 64 2c  i, iStart, iEnd,
37502 20 70 52 6f 6f 74 44 61 74 61 2c 20 6e 52 6f 6f   pRootData, nRoo
37503 74 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20  tData,.         
37504 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37505 20 26 70 52 65 61 64 65 72 73 5b 69 5d 29 3b 0a   &pReaders[i]);.
37506 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
37507 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 0a  TE_OK ) break;..
37508 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 20 20 69      i++;.  }.  i
37509 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f  f( rc!=SQLITE_DO
3750a 4e 45 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28  NE ){.    while(
3750b 20 69 2d 2d 3e 30 20 29 7b 0a 20 20 20 20 20 20   i-->0 ){.      
3750c 6c 65 61 76 65 73 52 65 61 64 65 72 44 65 73 74  leavesReaderDest
3750d 72 6f 79 28 26 70 52 65 61 64 65 72 73 5b 69 5d  roy(&pReaders[i]
3750e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  );.    }.    ret
3750f 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a  urn rc;.  }..  *
37510 70 69 52 65 61 64 65 72 73 20 3d 20 69 3b 0a 0a  piReaders = i;..
37511 20 20 2f 2a 20 4c 65 61 76 65 20 6f 75 72 20 72    /* Leave our r
37512 65 73 75 6c 74 73 20 73 6f 72 74 65 64 20 62 79  esults sorted by
37513 20 74 65 72 6d 2c 20 74 68 65 6e 20 61 67 65 2e   term, then age.
37514 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69 2d 2d   */.  while( i--
37515 20 29 7b 0a 20 20 20 20 6c 65 61 76 65 73 52 65   ){.    leavesRe
37516 61 64 65 72 52 65 6f 72 64 65 72 28 70 52 65 61  aderReorder(pRea
37517 64 65 72 73 2b 69 2c 20 2a 70 69 52 65 61 64 65  ders+i, *piReade
37518 72 73 2d 69 29 3b 0a 20 20 7d 0a 20 20 72 65 74  rs-i);.  }.  ret
37519 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
3751a 0a 0a 2f 2a 20 4d 65 72 67 65 20 64 6f 63 6c 69  ../* Merge docli
3751b 73 74 73 20 66 72 6f 6d 20 70 52 65 61 64 65 72  sts from pReader
3751c 73 5b 6e 52 65 61 64 65 72 73 5d 20 69 6e 74 6f  s[nReaders] into
3751d 20 61 20 73 69 6e 67 6c 65 20 64 6f 63 6c 69 73   a single doclis
3751e 74 2c 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 77  t, which.** is w
3751f 72 69 74 74 65 6e 20 74 6f 20 70 57 72 69 74 65  ritten to pWrite
37520 72 2e 20 20 41 73 73 75 6d 65 73 20 70 52 65 61  r.  Assumes pRea
37521 64 65 72 73 20 69 73 20 6f 72 64 65 72 65 64 20  ders is ordered 
37522 6f 6c 64 65 73 74 20 74 6f 0a 2a 2a 20 6e 65 77  oldest to.** new
37523 65 73 74 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28  est..*/./* TODO(
37524 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72 20  shess) Consider 
37525 70 75 74 74 69 6e 67 20 74 68 69 73 20 69 6e 6c  putting this inl
37526 69 6e 65 20 69 6e 20 73 65 67 6d 65 6e 74 4d 65  ine in segmentMe
37527 72 67 65 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63  rge(). */.static
37528 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65   int leavesReade
37529 72 73 4d 65 72 67 65 28 66 75 6c 6c 74 65 78 74  rsMerge(fulltext
3752a 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20 20 20 20  _vtab *v,.      
3752b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3752c 20 20 20 20 20 20 20 20 4c 65 61 76 65 73 52 65          LeavesRe
3752d 61 64 65 72 20 2a 70 52 65 61 64 65 72 73 2c 20  ader *pReaders, 
3752e 69 6e 74 20 6e 52 65 61 64 65 72 73 2c 0a 20 20  int nReaders,.  
3752f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37530 20 20 20 20 20 20 20 20 20 20 20 20 4c 65 61 66              Leaf
37531 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 29  Writer *pWriter)
37532 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 64 6c 52  {.  DLReader dlR
37533 65 61 64 65 72 73 5b 4d 45 52 47 45 5f 43 4f 55  eaders[MERGE_COU
37534 4e 54 5d 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  NT];.  const cha
37535 72 20 2a 70 54 65 72 6d 20 3d 20 6c 65 61 76 65  r *pTerm = leave
37536 73 52 65 61 64 65 72 54 65 72 6d 28 70 52 65 61  sReaderTerm(pRea
37537 64 65 72 73 29 3b 0a 20 20 69 6e 74 20 69 2c 20  ders);.  int i, 
37538 6e 54 65 72 6d 20 3d 20 6c 65 61 76 65 73 52 65  nTerm = leavesRe
37539 61 64 65 72 54 65 72 6d 42 79 74 65 73 28 70 52  aderTermBytes(pR
3753a 65 61 64 65 72 73 29 3b 0a 0a 20 20 61 73 73 65  eaders);..  asse
3753b 72 74 28 20 6e 52 65 61 64 65 72 73 3c 3d 4d 45  rt( nReaders<=ME
3753c 52 47 45 5f 43 4f 55 4e 54 20 29 3b 0a 0a 20 20  RGE_COUNT );..  
3753d 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65 61 64  for(i=0; i<nRead
3753e 65 72 73 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 64  ers; i++){.    d
3753f 6c 72 49 6e 69 74 28 26 64 6c 52 65 61 64 65 72  lrInit(&dlReader
37540 73 5b 69 5d 2c 20 44 4c 5f 44 45 46 41 55 4c 54  s[i], DL_DEFAULT
37541 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 65  ,.            le
37542 61 76 65 73 52 65 61 64 65 72 44 61 74 61 28 70  avesReaderData(p
37543 52 65 61 64 65 72 73 2b 69 29 2c 0a 20 20 20 20  Readers+i),.    
37544 20 20 20 20 20 20 20 20 6c 65 61 76 65 73 52 65          leavesRe
37545 61 64 65 72 44 61 74 61 42 79 74 65 73 28 70 52  aderDataBytes(pR
37546 65 61 64 65 72 73 2b 69 29 29 3b 0a 20 20 7d 0a  eaders+i));.  }.
37547 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 66 57 72  .  return leafWr
37548 69 74 65 72 53 74 65 70 4d 65 72 67 65 28 76 2c  iterStepMerge(v,
37549 20 70 57 72 69 74 65 72 2c 20 70 54 65 72 6d 2c   pWriter, pTerm,
3754a 20 6e 54 65 72 6d 2c 20 64 6c 52 65 61 64 65 72   nTerm, dlReader
3754b 73 2c 20 6e 52 65 61 64 65 72 73 29 3b 0a 7d 0a  s, nReaders);.}.
3754c 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 20  ./* Forward ref 
3754d 64 75 65 20 74 6f 20 6d 75 74 75 61 6c 20 72 65  due to mutual re
3754e 63 75 72 73 69 6f 6e 20 77 69 74 68 20 73 65 67  cursion with seg
3754f 64 69 72 4e 65 78 74 49 6e 64 65 78 28 29 2e 20  dirNextIndex(). 
37550 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65  */.static int se
37551 67 6d 65 6e 74 4d 65 72 67 65 28 66 75 6c 6c 74  gmentMerge(fullt
37552 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74  ext_vtab *v, int
37553 20 69 4c 65 76 65 6c 29 3b 0a 0a 2f 2a 20 50 75   iLevel);../* Pu
37554 74 20 74 68 65 20 6e 65 78 74 20 61 76 61 69 6c  t the next avail
37555 61 62 6c 65 20 69 6e 64 65 78 20 61 74 20 69 4c  able index at iL
37556 65 76 65 6c 20 69 6e 74 6f 20 2a 70 69 64 78 2e  evel into *pidx.
37557 20 20 49 66 20 69 4c 65 76 65 6c 0a 2a 2a 20 61    If iLevel.** a
37558 6c 72 65 61 64 79 20 68 61 73 20 4d 45 52 47 45  lready has MERGE
37559 5f 43 4f 55 4e 54 20 73 65 67 6d 65 6e 74 73 2c  _COUNT segments,
3755a 20 74 68 65 79 20 61 72 65 20 6d 65 72 67 65 64   they are merged
3755b 20 74 6f 20 61 20 68 69 67 68 65 72 0a 2a 2a 20   to a higher.** 
3755c 6c 65 76 65 6c 20 74 6f 20 6d 61 6b 65 20 72 6f  level to make ro
3755d 6f 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  om..*/.static in
3755e 74 20 73 65 67 64 69 72 4e 65 78 74 49 6e 64 65  t segdirNextInde
3755f 78 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  x(fulltext_vtab 
37560 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20  *v, int iLevel, 
37561 69 6e 74 20 2a 70 69 64 78 29 7b 0a 20 20 69 6e  int *pidx){.  in
37562 74 20 72 63 20 3d 20 73 65 67 64 69 72 5f 6d 61  t rc = segdir_ma
37563 78 5f 69 6e 64 65 78 28 76 2c 20 69 4c 65 76 65  x_index(v, iLeve
37564 6c 2c 20 70 69 64 78 29 3b 0a 20 20 69 66 28 20  l, pidx);.  if( 
37565 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
37566 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ){              
37567 2f 2a 20 4e 6f 20 73 65 67 6d 65 6e 74 73 20 61  /* No segments a
37568 74 20 69 4c 65 76 65 6c 2e 20 2a 2f 0a 20 20 20  t iLevel. */.   
37569 20 2a 70 69 64 78 20 3d 20 30 3b 0a 20 20 7d 65   *pidx = 0;.  }e
3756a 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  lse if( rc==SQLI
3756b 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 69 66  TE_ROW ){.    if
3756c 28 20 2a 70 69 64 78 3d 3d 28 4d 45 52 47 45 5f  ( *pidx==(MERGE_
3756d 43 4f 55 4e 54 2d 31 29 20 29 7b 0a 20 20 20 20  COUNT-1) ){.    
3756e 20 20 72 63 20 3d 20 73 65 67 6d 65 6e 74 4d 65    rc = segmentMe
3756f 72 67 65 28 76 2c 20 69 4c 65 76 65 6c 29 3b 0a  rge(v, iLevel);.
37570 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
37571 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
37572 20 72 63 3b 0a 20 20 20 20 20 20 2a 70 69 64 78   rc;.      *pidx
37573 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
37574 0a 20 20 20 20 20 20 28 2a 70 69 64 78 29 2b 2b  .      (*pidx)++
37575 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
37576 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
37577 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
37578 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4d 65  ITE_OK;.}../* Me
37579 72 67 65 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20  rge MERGE_COUNT 
3757a 73 65 67 6d 65 6e 74 73 20 61 74 20 69 4c 65 76  segments at iLev
3757b 65 6c 20 69 6e 74 6f 20 61 20 6e 65 77 20 73 65  el into a new se
3757c 67 6d 65 6e 74 20 61 74 0a 2a 2a 20 69 4c 65 76  gment at.** iLev
3757d 65 6c 2b 31 2e 20 20 49 66 20 69 4c 65 76 65 6c  el+1.  If iLevel
3757e 2b 31 20 69 73 20 61 6c 72 65 61 64 79 20 66 75  +1 is already fu
3757f 6c 6c 20 6f 66 20 73 65 67 6d 65 6e 74 73 2c 20  ll of segments, 
37580 74 68 6f 73 65 20 77 69 6c 6c 20 62 65 0a 2a 2a  those will be.**
37581 20 6d 65 72 67 65 64 20 74 6f 20 6d 61 6b 65 20   merged to make 
37582 72 6f 6f 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  room..*/.static 
37583 69 6e 74 20 73 65 67 6d 65 6e 74 4d 65 72 67 65  int segmentMerge
37584 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
37585 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 29 7b 0a  v, int iLevel){.
37586 20 20 4c 65 61 66 57 72 69 74 65 72 20 77 72 69    LeafWriter wri
37587 74 65 72 3b 0a 20 20 4c 65 61 76 65 73 52 65 61  ter;.  LeavesRea
37588 64 65 72 20 6c 72 73 5b 4d 45 52 47 45 5f 43 4f  der lrs[MERGE_CO
37589 55 4e 54 5d 3b 0a 20 20 69 6e 74 20 69 2c 20 72  UNT];.  int i, r
3758a 63 2c 20 69 64 78 20 3d 20 30 3b 0a 0a 20 20 2f  c, idx = 0;..  /
3758b 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20  * Determine the 
3758c 6e 65 78 74 20 61 76 61 69 6c 61 62 6c 65 20 73  next available s
3758d 65 67 6d 65 6e 74 20 69 6e 64 65 78 20 61 74 20  egment index at 
3758e 74 68 65 20 6e 65 78 74 20 6c 65 76 65 6c 2c 0a  the next level,.
3758f 20 20 2a 2a 20 6d 65 72 67 69 6e 67 20 61 73 20    ** merging as 
37590 6e 65 63 65 73 73 61 72 79 2e 0a 20 20 2a 2f 0a  necessary..  */.
37591 20 20 72 63 20 3d 20 73 65 67 64 69 72 4e 65 78    rc = segdirNex
37592 74 49 6e 64 65 78 28 76 2c 20 69 4c 65 76 65 6c  tIndex(v, iLevel
37593 2b 31 2c 20 26 69 64 78 29 3b 0a 20 20 69 66 28  +1, &idx);.  if(
37594 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
37595 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f   return rc;..  /
37596 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68  * TODO(shess) Th
37597 69 73 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  is assumes that 
37598 77 65 27 6c 6c 20 61 6c 77 61 79 73 20 73 65 65  we'll always see
37599 20 65 78 61 63 74 6c 79 0a 20 20 2a 2a 20 4d 45   exactly.  ** ME
3759a 52 47 45 5f 43 4f 55 4e 54 20 73 65 67 6d 65 6e  RGE_COUNT segmen
3759b 74 73 20 74 6f 20 6d 65 72 67 65 20 61 74 20 61  ts to merge at a
3759c 20 67 69 76 65 6e 20 6c 65 76 65 6c 2e 20 20 54   given level.  T
3759d 68 61 74 20 77 69 6c 6c 20 62 65 0a 20 20 2a 2a  hat will be.  **
3759e 20 62 72 6f 6b 65 6e 20 69 66 20 77 65 20 61 6c   broken if we al
3759f 6c 6f 77 20 74 68 65 20 64 65 76 65 6c 6f 70 65  low the develope
375a0 72 20 74 6f 20 72 65 71 75 65 73 74 20 70 72 65  r to request pre
375a1 65 6d 70 74 69 76 65 20 6f 72 0a 20 20 2a 2a 20  emptive or.  ** 
375a2 64 65 66 65 72 72 65 64 20 6d 65 72 67 69 6e 67  deferred merging
375a3 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28  ..  */.  memset(
375a4 26 6c 72 73 2c 20 27 5c 30 27 2c 20 73 69 7a 65  &lrs, '\0', size
375a5 6f 66 28 6c 72 73 29 29 3b 0a 20 20 72 63 20 3d  of(lrs));.  rc =
375a6 20 6c 65 61 76 65 73 52 65 61 64 65 72 73 49 6e   leavesReadersIn
375a7 69 74 28 76 2c 20 69 4c 65 76 65 6c 2c 20 6c 72  it(v, iLevel, lr
375a8 73 2c 20 26 69 29 3b 0a 20 20 69 66 28 20 72 63  s, &i);.  if( rc
375a9 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
375aa 74 75 72 6e 20 72 63 3b 0a 20 20 61 73 73 65 72  turn rc;.  asser
375ab 74 28 20 69 3d 3d 4d 45 52 47 45 5f 43 4f 55 4e  t( i==MERGE_COUN
375ac 54 20 29 3b 0a 0a 20 20 6c 65 61 66 57 72 69 74  T );..  leafWrit
375ad 65 72 49 6e 69 74 28 69 4c 65 76 65 6c 2b 31 2c  erInit(iLevel+1,
375ae 20 69 64 78 2c 20 26 77 72 69 74 65 72 29 3b 0a   idx, &writer);.
375af 0a 20 20 2f 2a 20 53 69 6e 63 65 20 6c 65 61 76  .  /* Since leav
375b0 65 73 52 65 61 64 65 72 52 65 6f 72 64 65 72 28  esReaderReorder(
375b1 29 20 70 75 73 68 65 73 20 72 65 61 64 65 72 73  ) pushes readers
375b2 20 61 74 20 65 6f 66 20 74 6f 20 74 68 65 20 65   at eof to the e
375b3 6e 64 2c 0a 20 20 2a 2a 20 77 68 65 6e 20 74 68  nd,.  ** when th
375b4 65 20 66 69 72 73 74 20 72 65 61 64 65 72 20 69  e first reader i
375b5 73 20 65 6d 70 74 79 2c 20 61 6c 6c 20 77 69 6c  s empty, all wil
375b6 6c 20 62 65 20 65 6d 70 74 79 2e 0a 20 20 2a 2f  l be empty..  */
375b7 0a 20 20 77 68 69 6c 65 28 20 21 6c 65 61 76 65  .  while( !leave
375b8 73 52 65 61 64 65 72 41 74 45 6e 64 28 6c 72 73  sReaderAtEnd(lrs
375b9 29 20 29 7b 0a 20 20 20 20 2f 2a 20 46 69 67 75  ) ){.    /* Figu
375ba 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20  re out how many 
375bb 72 65 61 64 65 72 73 20 73 68 61 72 65 20 74 68  readers share th
375bc 65 69 72 20 6e 65 78 74 20 74 65 72 6d 2e 20 2a  eir next term. *
375bd 2f 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69  /.    for(i=1; i
375be 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 20 26 26 20  <MERGE_COUNT && 
375bf 21 6c 65 61 76 65 73 52 65 61 64 65 72 41 74 45  !leavesReaderAtE
375c0 6e 64 28 6c 72 73 2b 69 29 3b 20 69 2b 2b 29 7b  nd(lrs+i); i++){
375c1 0a 20 20 20 20 20 20 69 66 28 20 30 21 3d 6c 65  .      if( 0!=le
375c2 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 43 6d  avesReaderTermCm
375c3 70 28 6c 72 73 2c 20 6c 72 73 2b 69 29 20 29 20  p(lrs, lrs+i) ) 
375c4 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
375c5 20 20 72 63 20 3d 20 6c 65 61 76 65 73 52 65 61    rc = leavesRea
375c6 64 65 72 73 4d 65 72 67 65 28 76 2c 20 6c 72 73  dersMerge(v, lrs
375c7 2c 20 69 2c 20 26 77 72 69 74 65 72 29 3b 0a 20  , i, &writer);. 
375c8 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
375c9 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b  E_OK ) goto err;
375ca 0a 0a 20 20 20 20 2f 2a 20 53 74 65 70 20 66 6f  ..    /* Step fo
375cb 72 77 61 72 64 20 74 68 6f 73 65 20 74 68 61 74  rward those that
375cc 20 77 65 72 65 20 6d 65 72 67 65 64 2e 20 2a 2f   were merged. */
375cd 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d 2d 3e  .    while( i-->
375ce 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
375cf 6c 65 61 76 65 73 52 65 61 64 65 72 53 74 65 70  leavesReaderStep
375d0 28 76 2c 20 6c 72 73 2b 69 29 3b 0a 20 20 20 20  (v, lrs+i);.    
375d1 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
375d2 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a  _OK ) goto err;.
375d3 0a 20 20 20 20 20 20 2f 2a 20 52 65 6f 72 64 65  .      /* Reorde
375d4 72 20 62 79 20 74 65 72 6d 2c 20 74 68 65 6e 20  r by term, then 
375d5 62 79 20 61 67 65 2e 20 2a 2f 0a 20 20 20 20 20  by age. */.     
375d6 20 6c 65 61 76 65 73 52 65 61 64 65 72 52 65 6f   leavesReaderReo
375d7 72 64 65 72 28 6c 72 73 2b 69 2c 20 4d 45 52 47  rder(lrs+i, MERG
375d8 45 5f 43 4f 55 4e 54 2d 69 29 3b 0a 20 20 20 20  E_COUNT-i);.    
375d9 7d 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 3d 30  }.  }..  for(i=0
375da 3b 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 3b  ; i<MERGE_COUNT;
375db 20 69 2b 2b 29 7b 0a 20 20 20 20 6c 65 61 76 65   i++){.    leave
375dc 73 52 65 61 64 65 72 44 65 73 74 72 6f 79 28 26  sReaderDestroy(&
375dd 6c 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 0a 20 20  lrs[i]);.  }..  
375de 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 46  rc = leafWriterF
375df 69 6e 61 6c 69 7a 65 28 76 2c 20 26 77 72 69 74  inalize(v, &writ
375e0 65 72 29 3b 0a 20 20 6c 65 61 66 57 72 69 74 65  er);.  leafWrite
375e1 72 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72  rDestroy(&writer
375e2 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
375e3 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
375e4 72 63 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65  rc;..  /* Delete
375e5 20 74 68 65 20 6d 65 72 67 65 64 20 73 65 67 6d   the merged segm
375e6 65 6e 74 20 64 61 74 61 2e 20 2a 2f 0a 20 20 72  ent data. */.  r
375e7 65 74 75 72 6e 20 73 65 67 64 69 72 5f 64 65 6c  eturn segdir_del
375e8 65 74 65 28 76 2c 20 69 4c 65 76 65 6c 29 3b 0a  ete(v, iLevel);.
375e9 0a 20 65 72 72 3a 0a 20 20 66 6f 72 28 69 3d 30  . err:.  for(i=0
375ea 3b 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 3b  ; i<MERGE_COUNT;
375eb 20 69 2b 2b 29 7b 0a 20 20 20 20 6c 65 61 76 65   i++){.    leave
375ec 73 52 65 61 64 65 72 44 65 73 74 72 6f 79 28 26  sReaderDestroy(&
375ed 6c 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 6c  lrs[i]);.  }.  l
375ee 65 61 66 57 72 69 74 65 72 44 65 73 74 72 6f 79  eafWriterDestroy
375ef 28 26 77 72 69 74 65 72 29 3b 0a 20 20 72 65 74  (&writer);.  ret
375f0 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 41 63  urn rc;.}../* Ac
375f1 63 75 6d 75 6c 61 74 65 20 74 68 65 20 75 6e 69  cumulate the uni
375f2 6f 6e 20 6f 66 20 2a 61 63 63 20 61 6e 64 20 2a  on of *acc and *
375f3 70 44 61 74 61 20 69 6e 74 6f 20 2a 61 63 63 2e  pData into *acc.
375f4 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20   */.static void 
375f5 64 6f 63 4c 69 73 74 41 63 63 75 6d 75 6c 61 74  docListAccumulat
375f6 65 55 6e 69 6f 6e 28 44 61 74 61 42 75 66 66 65  eUnion(DataBuffe
375f7 72 20 2a 61 63 63 2c 0a 20 20 20 20 20 20 20 20  r *acc,.        
375f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
375f9 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
375fa 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e   char *pData, in
375fb 74 20 6e 44 61 74 61 29 20 7b 0a 20 20 44 61 74  t nData) {.  Dat
375fc 61 42 75 66 66 65 72 20 74 6d 70 20 3d 20 2a 61  aBuffer tmp = *a
375fd 63 63 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72  cc;.  dataBuffer
375fe 49 6e 69 74 28 61 63 63 2c 20 74 6d 70 2e 6e 44  Init(acc, tmp.nD
375ff 61 74 61 2b 6e 44 61 74 61 29 3b 0a 20 20 64 6f  ata+nData);.  do
37600 63 4c 69 73 74 55 6e 69 6f 6e 28 74 6d 70 2e 70  cListUnion(tmp.p
37601 44 61 74 61 2c 20 74 6d 70 2e 6e 44 61 74 61 2c  Data, tmp.nData,
37602 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 61   pData, nData, a
37603 63 63 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  cc);.  dataBuffe
37604 72 44 65 73 74 72 6f 79 28 26 74 6d 70 29 3b 0a  rDestroy(&tmp);.
37605 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  }../* TODO(shess
37606 29 20 49 74 20 6d 69 67 68 74 20 62 65 20 69 6e  ) It might be in
37607 74 65 72 65 73 74 69 6e 67 20 74 6f 20 65 78 70  teresting to exp
37608 6c 6f 72 65 20 64 69 66 66 65 72 65 6e 74 20 6d  lore different m
37609 65 72 67 65 0a 2a 2a 20 73 74 72 61 74 65 67 69  erge.** strategi
3760a 65 73 2c 20 68 65 72 65 2e 20 20 46 6f 72 20 69  es, here.  For i
3760b 6e 73 74 61 6e 63 65 2c 20 73 69 6e 63 65 20 74  nstance, since t
3760c 68 69 73 20 69 73 20 61 20 73 6f 72 74 65 64 20  his is a sorted 
3760d 6d 65 72 67 65 2c 20 77 65 0a 2a 2a 20 63 6f 75  merge, we.** cou
3760e 6c 64 20 65 61 73 69 6c 79 20 6d 65 72 67 65 20  ld easily merge 
3760f 6d 61 6e 79 20 64 6f 63 6c 69 73 74 73 20 69 6e  many doclists in
37610 20 70 61 72 61 6c 6c 65 6c 2e 20 20 57 69 74 68   parallel.  With
37611 20 73 6f 6d 65 0a 2a 2a 20 63 6f 6d 70 72 65 68   some.** compreh
37612 65 6e 73 69 6f 6e 20 6f 66 20 74 68 65 20 73 74  ension of the st
37613 6f 72 61 67 65 20 66 6f 72 6d 61 74 2c 20 77 65  orage format, we
37614 20 63 6f 75 6c 64 20 6d 65 72 67 65 20 61 6c 6c   could merge all
37615 20 6f 66 20 74 68 65 0a 2a 2a 20 64 6f 63 6c 69   of the.** docli
37616 73 74 73 20 77 69 74 68 69 6e 20 61 20 6c 65 61  sts within a lea
37617 66 20 6e 6f 64 65 20 64 69 72 65 63 74 6c 79 20  f node directly 
37618 66 72 6f 6d 20 74 68 65 20 6c 65 61 66 20 6e 6f  from the leaf no
37619 64 65 27 73 20 73 74 6f 72 61 67 65 2e 0a 2a 2a  de's storage..**
3761a 20 49 74 20 6d 61 79 20 62 65 20 77 6f 72 74 68   It may be worth
3761b 77 68 69 6c 65 20 74 6f 20 6d 65 72 67 65 20 73  while to merge s
3761c 6d 61 6c 6c 65 72 20 64 6f 63 6c 69 73 74 73 20  maller doclists 
3761d 62 65 66 6f 72 65 20 6c 61 72 67 65 72 0a 2a 2a  before larger.**
3761e 20 64 6f 63 6c 69 73 74 73 2c 20 73 69 6e 63 65   doclists, since
3761f 20 74 68 65 79 20 63 61 6e 20 62 65 20 74 72 61   they can be tra
37620 76 65 72 73 65 64 20 6d 6f 72 65 20 71 75 69 63  versed more quic
37621 6b 6c 79 20 2d 20 62 75 74 20 74 68 65 0a 2a 2a  kly - but the.**
37622 20 72 65 73 75 6c 74 73 20 6d 61 79 20 68 61 76   results may hav
37623 65 20 6c 65 73 73 20 6f 76 65 72 6c 61 70 2c 20  e less overlap, 
37624 6d 61 6b 69 6e 67 20 74 68 65 6d 20 6d 6f 72 65  making them more
37625 20 65 78 70 65 6e 73 69 76 65 20 69 6e 20 61 0a   expensive in a.
37626 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 77 61 79  ** different way
37627 2e 0a 2a 2f 0a 0a 2f 2a 20 53 63 61 6e 20 70 52  ..*/../* Scan pR
37628 65 61 64 65 72 20 66 6f 72 20 70 54 65 72 6d 2f  eader for pTerm/
37629 6e 54 65 72 6d 2c 20 61 6e 64 20 6d 65 72 67 65  nTerm, and merge
3762a 20 74 68 65 20 74 65 72 6d 27 73 20 64 6f 63 6c   the term's docl
3762b 69 73 74 20 6f 76 65 72 0a 2a 2a 20 2a 6f 75 74  ist over.** *out
3762c 20 28 61 6e 79 20 64 6f 63 6c 69 73 74 73 20 77   (any doclists w
3762d 69 74 68 20 64 75 70 6c 69 63 61 74 65 20 64 6f  ith duplicate do
3762e 63 69 64 73 20 6f 76 65 72 77 72 69 74 65 20 74  cids overwrite t
3762f 68 6f 73 65 20 69 6e 20 2a 6f 75 74 29 2e 0a 2a  hose in *out)..*
37630 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74  * Internal funct
37631 69 6f 6e 20 66 6f 72 20 6c 6f 61 64 53 65 67 6d  ion for loadSegm
37632 65 6e 74 4c 65 61 66 28 29 2e 0a 2a 2f 0a 73 74  entLeaf()..*/.st
37633 61 74 69 63 20 69 6e 74 20 6c 6f 61 64 53 65 67  atic int loadSeg
37634 6d 65 6e 74 4c 65 61 76 65 73 49 6e 74 28 66 75  mentLeavesInt(fu
37635 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
37636 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52  LeavesReader *pR
37637 65 61 64 65 72 2c 0a 20 20 20 20 20 20 20 20 20  eader,.         
37638 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37639 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
3763a 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54  r *pTerm, int nT
3763b 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69  erm, int isPrefi
3763c 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  x,.             
3763d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3763e 20 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 6f     DataBuffer *o
3763f 75 74 29 7b 0a 20 20 2f 2a 20 64 6f 63 6c 69 73  ut){.  /* doclis
37640 74 20 64 61 74 61 20 69 73 20 61 63 63 75 6d 75  t data is accumu
37641 6c 61 74 65 64 20 69 6e 74 6f 20 70 42 75 66 66  lated into pBuff
37642 65 72 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 68  ers similar to h
37643 6f 77 20 6f 6e 65 20 64 6f 65 73 0a 20 20 2a 2a  ow one does.  **
37644 20 69 6e 63 72 65 6d 65 6e 74 20 69 6e 20 62 69   increment in bi
37645 6e 61 72 79 20 61 72 69 74 68 6d 65 74 69 63 2e  nary arithmetic.
37646 20 20 49 66 20 69 6e 64 65 78 20 30 20 69 73 20    If index 0 is 
37647 65 6d 70 74 79 2c 20 74 68 65 20 64 61 74 61 20  empty, the data 
37648 69 73 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20 74  is.  ** stored t
37649 68 65 72 65 2e 20 20 49 66 20 74 68 65 72 65 20  here.  If there 
3764a 69 73 20 64 61 74 61 20 74 68 65 72 65 2c 20 69  is data there, i
3764b 74 20 69 73 20 6d 65 72 67 65 64 20 61 6e 64 20  t is merged and 
3764c 74 68 65 0a 20 20 2a 2a 20 72 65 73 75 6c 74 73  the.  ** results
3764d 20 63 61 72 72 69 65 64 20 69 6e 74 6f 20 70 6f   carried into po
3764e 73 69 74 69 6f 6e 20 31 2c 20 77 69 74 68 20 66  sition 1, with f
3764f 75 72 74 68 65 72 20 6d 65 72 67 65 2d 61 6e 64  urther merge-and
37650 2d 63 61 72 72 79 0a 20 20 2a 2a 20 75 6e 74 69  -carry.  ** unti
37651 6c 20 61 6e 20 65 6d 70 74 79 20 70 6f 73 69 74  l an empty posit
37652 69 6f 6e 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20  ion is found..  
37653 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72 20  */.  DataBuffer 
37654 2a 70 42 75 66 66 65 72 73 20 3d 20 4e 55 4c 4c  *pBuffers = NULL
37655 3b 0a 20 20 69 6e 74 20 6e 42 75 66 66 65 72 73  ;.  int nBuffers
37656 20 3d 20 30 2c 20 6e 4d 61 78 42 75 66 66 65 72   = 0, nMaxBuffer
37657 73 20 3d 20 30 2c 20 72 63 3b 0a 0a 20 20 61 73  s = 0, rc;..  as
37658 73 65 72 74 28 20 6e 54 65 72 6d 3e 30 20 29 3b  sert( nTerm>0 );
37659 0a 0a 20 20 66 6f 72 28 72 63 3d 53 51 4c 49 54  ..  for(rc=SQLIT
3765a 45 5f 4f 4b 3b 20 72 63 3d 3d 53 51 4c 49 54 45  E_OK; rc==SQLITE
3765b 5f 4f 4b 20 26 26 20 21 6c 65 61 76 65 73 52 65  _OK && !leavesRe
3765c 61 64 65 72 41 74 45 6e 64 28 70 52 65 61 64 65  aderAtEnd(pReade
3765d 72 29 3b 0a 20 20 20 20 20 20 72 63 3d 6c 65 61  r);.      rc=lea
3765e 76 65 73 52 65 61 64 65 72 53 74 65 70 28 76 2c  vesReaderStep(v,
3765f 20 70 52 65 61 64 65 72 29 29 7b 0a 20 20 20 20   pReader)){.    
37660 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 52  /* TODO(shess) R
37661 65 61 6c 6c 79 20 77 61 6e 74 20 6c 65 61 76 65  eally want leave
37662 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 29  sReaderTermCmp()
37663 2c 20 62 75 74 20 74 68 61 74 20 6e 61 6d 65 20  , but that name 
37664 69 73 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64  is.    ** alread
37665 79 20 74 61 6b 65 6e 20 74 6f 20 63 6f 6d 70 61  y taken to compa
37666 72 65 20 74 68 65 20 74 65 72 6d 73 20 6f 66 20  re the terms of 
37667 74 77 6f 20 4c 65 61 76 65 73 52 65 61 64 65 72  two LeavesReader
37668 73 2e 20 20 54 68 69 6e 6b 0a 20 20 20 20 2a 2a  s.  Think.    **
37669 20 6f 6e 20 61 20 62 65 74 74 65 72 20 6e 61 6d   on a better nam
3766a 65 2e 20 20 5b 4d 65 61 6e 77 68 69 6c 65 2c 20  e.  [Meanwhile, 
3766b 62 72 65 61 6b 20 65 6e 63 61 70 73 75 6c 61 74  break encapsulat
3766c 69 6f 6e 20 72 61 74 68 65 72 20 74 68 61 6e 0a  ion rather than.
3766d 20 20 20 20 2a 2a 20 75 73 65 20 61 20 63 6f 6e      ** use a con
3766e 66 75 73 69 6e 67 20 6e 61 6d 65 2e 5d 0a 20 20  fusing name.].  
3766f 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 20 3d    */.    int c =
37670 20 6c 65 61 66 52 65 61 64 65 72 54 65 72 6d 43   leafReaderTermC
37671 6d 70 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61  mp(&pReader->lea
37672 66 52 65 61 64 65 72 2c 20 70 54 65 72 6d 2c 20  fReader, pTerm, 
37673 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 29  nTerm, isPrefix)
37674 3b 0a 20 20 20 20 69 66 28 20 63 3e 30 20 29 20  ;.    if( c>0 ) 
37675 62 72 65 61 6b 3b 20 20 20 20 20 20 2f 2a 20 50  break;      /* P
37676 61 73 74 20 61 6e 79 20 70 6f 73 73 69 62 6c 65  ast any possible
37677 20 6d 61 74 63 68 65 73 2e 20 2a 2f 0a 20 20 20   matches. */.   
37678 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20   if( c==0 ){.   
37679 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
3767a 44 61 74 61 20 3d 20 6c 65 61 76 65 73 52 65 61  Data = leavesRea
3767b 64 65 72 44 61 74 61 28 70 52 65 61 64 65 72 29  derData(pReader)
3767c 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 42 75 66  ;.      int iBuf
3767d 66 65 72 2c 20 6e 44 61 74 61 20 3d 20 6c 65 61  fer, nData = lea
3767e 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79 74  vesReaderDataByt
3767f 65 73 28 70 52 65 61 64 65 72 29 3b 0a 0a 20 20  es(pReader);..  
37680 20 20 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20      /* Find the 
37681 66 69 72 73 74 20 65 6d 70 74 79 20 62 75 66 66  first empty buff
37682 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72  er. */.      for
37683 28 69 42 75 66 66 65 72 3d 30 3b 20 69 42 75 66  (iBuffer=0; iBuf
37684 66 65 72 3c 6e 42 75 66 66 65 72 73 3b 20 2b 2b  fer<nBuffers; ++
37685 69 42 75 66 66 65 72 29 7b 0a 20 20 20 20 20 20  iBuffer){.      
37686 20 20 69 66 28 20 30 3d 3d 70 42 75 66 66 65 72    if( 0==pBuffer
37687 73 5b 69 42 75 66 66 65 72 5d 2e 6e 44 61 74 61  s[iBuffer].nData
37688 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
37689 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 75 74 20  }..      /* Out 
3768a 6f 66 20 62 75 66 66 65 72 73 2c 20 61 64 64 20  of buffers, add 
3768b 61 6e 20 65 6d 70 74 79 20 6f 6e 65 2e 20 2a 2f  an empty one. */
3768c 0a 20 20 20 20 20 20 69 66 28 20 69 42 75 66 66  .      if( iBuff
3768d 65 72 3d 3d 6e 42 75 66 66 65 72 73 20 29 7b 0a  er==nBuffers ){.
3768e 20 20 20 20 20 20 20 20 69 66 28 20 6e 42 75 66          if( nBuf
3768f 66 65 72 73 3d 3d 6e 4d 61 78 42 75 66 66 65 72  fers==nMaxBuffer
37690 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 44  s ){.          D
37691 61 74 61 42 75 66 66 65 72 20 2a 70 3b 0a 20 20  ataBuffer *p;.  
37692 20 20 20 20 20 20 20 20 6e 4d 61 78 42 75 66 66          nMaxBuff
37693 65 72 73 20 2b 3d 20 32 30 3b 0a 0a 20 20 20 20  ers += 20;..    
37694 20 20 20 20 20 20 2f 2a 20 4d 61 6e 75 61 6c 20        /* Manual 
37695 72 65 61 6c 6c 6f 63 20 73 6f 20 77 65 20 63 61  realloc so we ca
37696 6e 20 68 61 6e 64 6c 65 20 4e 55 4c 4c 20 61 70  n handle NULL ap
37697 70 72 6f 70 72 69 61 74 65 6c 79 2e 20 2a 2f 0a  propriately. */.
37698 20 20 20 20 20 20 20 20 20 20 70 20 3d 20 73 71            p = sq
37699 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 4d 61  lite3_malloc(nMa
3769a 78 42 75 66 66 65 72 73 2a 73 69 7a 65 6f 66 28  xBuffers*sizeof(
3769b 2a 70 42 75 66 66 65 72 73 29 29 3b 0a 20 20 20  *pBuffers));.   
3769c 20 20 20 20 20 20 20 69 66 28 20 70 3d 3d 4e 55         if( p==NU
3769d 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  LL ){.          
3769e 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
3769f 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 20  MEM;.           
376a0 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
376a1 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 69    }..          i
376a2 66 28 20 6e 42 75 66 66 65 72 73 3e 30 20 29 7b  f( nBuffers>0 ){
376a3 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73  .            ass
376a4 65 72 74 28 70 42 75 66 66 65 72 73 21 3d 4e 55  ert(pBuffers!=NU
376a5 4c 4c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  LL);.           
376a6 20 6d 65 6d 63 70 79 28 70 2c 20 70 42 75 66 66   memcpy(p, pBuff
376a7 65 72 73 2c 20 6e 42 75 66 66 65 72 73 2a 73 69  ers, nBuffers*si
376a8 7a 65 6f 66 28 2a 70 42 75 66 66 65 72 73 29 29  zeof(*pBuffers))
376a9 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ;.            sq
376aa 6c 69 74 65 33 5f 66 72 65 65 28 70 42 75 66 66  lite3_free(pBuff
376ab 65 72 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ers);.          
376ac 7d 0a 20 20 20 20 20 20 20 20 20 20 70 42 75 66  }.          pBuf
376ad 66 65 72 73 20 3d 20 70 3b 0a 20 20 20 20 20 20  fers = p;.      
376ae 20 20 7d 0a 20 20 20 20 20 20 20 20 64 61 74 61    }.        data
376af 42 75 66 66 65 72 49 6e 69 74 28 26 28 70 42 75  BufferInit(&(pBu
376b0 66 66 65 72 73 5b 6e 42 75 66 66 65 72 73 5d 29  ffers[nBuffers])
376b1 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 6e 42  , 0);.        nB
376b2 75 66 66 65 72 73 2b 2b 3b 0a 20 20 20 20 20 20  uffers++;.      
376b3 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 41 74 20 74  }..      /* At t
376b4 68 69 73 20 70 6f 69 6e 74 2c 20 6d 75 73 74 20  his point, must 
376b5 68 61 76 65 20 61 6e 20 65 6d 70 74 79 20 61 74  have an empty at
376b6 20 69 42 75 66 66 65 72 2e 20 2a 2f 0a 20 20 20   iBuffer. */.   
376b7 20 20 20 61 73 73 65 72 74 28 69 42 75 66 66 65     assert(iBuffe
376b8 72 3c 6e 42 75 66 66 65 72 73 20 26 26 20 70 42  r<nBuffers && pB
376b9 75 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d 2e  uffers[iBuffer].
376ba 6e 44 61 74 61 3d 3d 30 29 3b 0a 0a 20 20 20 20  nData==0);..    
376bb 20 20 2f 2a 20 49 66 20 65 6d 70 74 79 20 77 61    /* If empty wa
376bc 73 20 66 69 72 73 74 20 62 75 66 66 65 72 2c 20  s first buffer, 
376bd 6e 6f 20 6e 65 65 64 20 66 6f 72 20 6d 65 72 67  no need for merg
376be 65 20 6c 6f 67 69 63 2e 20 2a 2f 0a 20 20 20 20  e logic. */.    
376bf 20 20 69 66 28 20 69 42 75 66 66 65 72 3d 3d 30    if( iBuffer==0
376c0 20 29 7b 0a 20 20 20 20 20 20 20 20 64 61 74 61   ){.        data
376c1 42 75 66 66 65 72 52 65 70 6c 61 63 65 28 26 28  BufferReplace(&(
376c2 70 42 75 66 66 65 72 73 5b 30 5d 29 2c 20 70 44  pBuffers[0]), pD
376c3 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 20 20 20  ata, nData);.   
376c4 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
376c5 20 20 2f 2a 20 70 41 63 63 20 69 73 20 74 68 65    /* pAcc is the
376c6 20 65 6d 70 74 79 20 62 75 66 66 65 72 20 74 68   empty buffer th
376c7 65 20 6d 65 72 67 65 64 20 64 61 74 61 20 77 69  e merged data wi
376c8 6c 6c 20 65 6e 64 20 75 70 20 69 6e 2e 20 2a 2f  ll end up in. */
376c9 0a 20 20 20 20 20 20 20 20 44 61 74 61 42 75 66  .        DataBuf
376ca 66 65 72 20 2a 70 41 63 63 20 3d 20 26 28 70 42  fer *pAcc = &(pB
376cb 75 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d 29  uffers[iBuffer])
376cc 3b 0a 20 20 20 20 20 20 20 20 44 61 74 61 42 75  ;.        DataBu
376cd 66 66 65 72 20 2a 70 20 3d 20 26 28 70 42 75 66  ffer *p = &(pBuf
376ce 66 65 72 73 5b 30 5d 29 3b 0a 0a 20 20 20 20 20  fers[0]);..     
376cf 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 70 6f 73     /* Handle pos
376d0 69 74 69 6f 6e 20 30 20 73 70 65 63 69 61 6c 6c  ition 0 speciall
376d1 79 20 74 6f 20 61 76 6f 69 64 20 6e 65 65 64 20  y to avoid need 
376d2 74 6f 20 70 72 69 6d 65 20 70 41 63 63 0a 20 20  to prime pAcc.  
376d3 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 70 44        ** with pD
376d4 61 74 61 2f 6e 44 61 74 61 2e 0a 20 20 20 20 20  ata/nData..     
376d5 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 64 61     */.        da
376d6 74 61 42 75 66 66 65 72 53 77 61 70 28 70 2c 20  taBufferSwap(p, 
376d7 70 41 63 63 29 3b 0a 20 20 20 20 20 20 20 20 64  pAcc);.        d
376d8 6f 63 4c 69 73 74 41 63 63 75 6d 75 6c 61 74 65  ocListAccumulate
376d9 55 6e 69 6f 6e 28 70 41 63 63 2c 20 70 44 61 74  Union(pAcc, pDat
376da 61 2c 20 6e 44 61 74 61 29 3b 0a 0a 20 20 20 20  a, nData);..    
376db 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74      /* Accumulat
376dc 65 20 72 65 6d 61 69 6e 69 6e 67 20 64 6f 63 6c  e remaining docl
376dd 69 73 74 73 20 69 6e 74 6f 20 70 41 63 63 2e 20  ists into pAcc. 
376de 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 2b  */.        for(+
376df 2b 70 3b 20 70 3c 70 41 63 63 3b 20 2b 2b 70 29  +p; p<pAcc; ++p)
376e0 7b 0a 20 20 20 20 20 20 20 20 20 20 64 6f 63 4c  {.          docL
376e1 69 73 74 41 63 63 75 6d 75 6c 61 74 65 55 6e 69  istAccumulateUni
376e2 6f 6e 28 70 41 63 63 2c 20 70 2d 3e 70 44 61 74  on(pAcc, p->pDat
376e3 61 2c 20 70 2d 3e 6e 44 61 74 61 29 3b 0a 0a 20  a, p->nData);.. 
376e4 20 20 20 20 20 20 20 20 20 2f 2a 20 64 61 74 61           /* data
376e5 42 75 66 66 65 72 52 65 73 65 74 28 29 20 63 6f  BufferReset() co
376e6 75 6c 64 20 61 6c 6c 6f 77 20 61 20 6c 61 72 67  uld allow a larg
376e7 65 20 64 6f 63 6c 69 73 74 20 74 6f 20 62 6c 6f  e doclist to blo
376e8 77 20 75 70 0a 20 20 20 20 20 20 20 20 20 20 2a  w up.          *
376e9 2a 20 6f 75 72 20 6d 65 6d 6f 72 79 20 72 65 71  * our memory req
376ea 75 69 72 65 6d 65 6e 74 73 2e 0a 20 20 20 20 20  uirements..     
376eb 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
376ec 20 20 69 66 28 20 70 2d 3e 6e 43 61 70 61 63 69    if( p->nCapaci
376ed 74 79 3c 31 30 32 34 20 29 7b 0a 20 20 20 20 20  ty<1024 ){.     
376ee 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65         dataBuffe
376ef 72 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20 20  rReset(p);.     
376f0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
376f1 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66          dataBuff
376f2 65 72 44 65 73 74 72 6f 79 28 70 29 3b 0a 20 20  erDestroy(p);.  
376f3 20 20 20 20 20 20 20 20 20 20 64 61 74 61 42 75            dataBu
376f4 66 66 65 72 49 6e 69 74 28 70 2c 20 30 29 3b 0a  fferInit(p, 0);.
376f5 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
376f6 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
376f7 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 6e    }.  }..  /* Un
376f8 69 6f 6e 20 61 6c 6c 20 74 68 65 20 64 6f 63 6c  ion all the docl
376f9 69 73 74 73 20 74 6f 67 65 74 68 65 72 20 69 6e  ists together in
376fa 74 6f 20 2a 6f 75 74 2e 20 2a 2f 0a 20 20 2f 2a  to *out. */.  /*
376fb 20 54 4f 44 4f 28 73 68 65 73 73 29 20 57 68 61   TODO(shess) Wha
376fc 74 20 69 66 20 2a 6f 75 74 20 69 73 20 62 69 67  t if *out is big
376fd 3f 20 20 53 69 67 68 2e 20 2a 2f 0a 20 20 69 66  ?  Sigh. */.  if
376fe 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
376ff 26 26 20 6e 42 75 66 66 65 72 73 3e 30 20 29 7b  && nBuffers>0 ){
37700 0a 20 20 20 20 69 6e 74 20 69 42 75 66 66 65 72  .    int iBuffer
37701 3b 0a 20 20 20 20 66 6f 72 28 69 42 75 66 66 65  ;.    for(iBuffe
37702 72 3d 30 3b 20 69 42 75 66 66 65 72 3c 6e 42 75  r=0; iBuffer<nBu
37703 66 66 65 72 73 3b 20 2b 2b 69 42 75 66 66 65 72  ffers; ++iBuffer
37704 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 42 75  ){.      if( pBu
37705 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d 2e 6e  ffers[iBuffer].n
37706 44 61 74 61 3e 30 20 29 7b 0a 20 20 20 20 20 20  Data>0 ){.      
37707 20 20 69 66 28 20 6f 75 74 2d 3e 6e 44 61 74 61    if( out->nData
37708 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
37709 20 64 61 74 61 42 75 66 66 65 72 53 77 61 70 28   dataBufferSwap(
3770a 6f 75 74 2c 20 26 28 70 42 75 66 66 65 72 73 5b  out, &(pBuffers[
3770b 69 42 75 66 66 65 72 5d 29 29 3b 0a 20 20 20 20  iBuffer]));.    
3770c 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3770d 20 20 20 20 20 64 6f 63 4c 69 73 74 41 63 63 75       docListAccu
3770e 6d 75 6c 61 74 65 55 6e 69 6f 6e 28 6f 75 74 2c  mulateUnion(out,
3770f 20 70 42 75 66 66 65 72 73 5b 69 42 75 66 66 65   pBuffers[iBuffe
37710 72 5d 2e 70 44 61 74 61 2c 0a 20 20 20 20 20 20  r].pData,.      
37711 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37712 20 20 20 20 20 20 20 20 20 20 20 70 42 75 66 66             pBuff
37713 65 72 73 5b 69 42 75 66 66 65 72 5d 2e 6e 44 61  ers[iBuffer].nDa
37714 74 61 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ta);.        }. 
37715 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
37716 0a 0a 20 20 77 68 69 6c 65 28 20 6e 42 75 66 66  ..  while( nBuff
37717 65 72 73 2d 2d 20 29 7b 0a 20 20 20 20 64 61 74  ers-- ){.    dat
37718 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
37719 28 70 42 75 66 66 65 72 73 5b 6e 42 75 66 66 65  (pBuffers[nBuffe
3771a 72 73 5d 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28  rs]));.  }.  if(
3771b 20 70 42 75 66 66 65 72 73 21 3d 4e 55 4c 4c 20   pBuffers!=NULL 
3771c 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70  ) sqlite3_free(p
3771d 42 75 66 66 65 72 73 29 3b 0a 0a 20 20 72 65 74  Buffers);..  ret
3771e 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 43 61  urn rc;.}../* Ca
3771f 6c 6c 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65  ll loadSegmentLe
37720 61 76 65 73 49 6e 74 28 29 20 77 69 74 68 20 70  avesInt() with p
37721 44 61 74 61 2f 6e 44 61 74 61 20 61 73 20 69 6e  Data/nData as in
37722 70 75 74 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  put. */.static i
37723 6e 74 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65  nt loadSegmentLe
37724 61 66 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  af(fulltext_vtab
37725 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   *v, const char 
37726 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74  *pData, int nDat
37727 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
37728 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
37729 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c  nst char *pTerm,
3772a 20 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20   int nTerm, int 
3772b 69 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20  isPrefix,.      
3772c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3772d 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72 20       DataBuffer 
3772e 2a 6f 75 74 29 7b 0a 20 20 4c 65 61 76 65 73 52  *out){.  LeavesR
3772f 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a 20 20  eader reader;.  
37730 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72  int rc;..  asser
37731 74 28 20 6e 44 61 74 61 3e 31 20 29 3b 0a 20 20  t( nData>1 );.  
37732 61 73 73 65 72 74 28 20 2a 70 44 61 74 61 3d 3d  assert( *pData==
37733 27 5c 30 27 20 29 3b 0a 20 20 72 63 20 3d 20 6c  '\0' );.  rc = l
37734 65 61 76 65 73 52 65 61 64 65 72 49 6e 69 74 28  eavesReaderInit(
37735 76 2c 20 30 2c 20 30 2c 20 30 2c 20 70 44 61 74  v, 0, 0, 0, pDat
37736 61 2c 20 6e 44 61 74 61 2c 20 26 72 65 61 64 65  a, nData, &reade
37737 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  r);.  if( rc!=SQ
37738 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
37739 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 6c 6f 61   rc;..  rc = loa
3773a 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73 49 6e  dSegmentLeavesIn
3773b 74 28 76 2c 20 26 72 65 61 64 65 72 2c 20 70 54  t(v, &reader, pT
3773c 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72  erm, nTerm, isPr
3773d 65 66 69 78 2c 20 6f 75 74 29 3b 0a 20 20 6c 65  efix, out);.  le
3773e 61 76 65 73 52 65 61 64 65 72 52 65 73 65 74 28  avesReaderReset(
3773f 26 72 65 61 64 65 72 29 3b 0a 20 20 6c 65 61 76  &reader);.  leav
37740 65 73 52 65 61 64 65 72 44 65 73 74 72 6f 79 28  esReaderDestroy(
37741 26 72 65 61 64 65 72 29 3b 0a 20 20 72 65 74 75  &reader);.  retu
37742 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 43 61 6c  rn rc;.}../* Cal
37743 6c 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61  l loadSegmentLea
37744 76 65 73 49 6e 74 28 29 20 77 69 74 68 20 74 68  vesInt() with th
37745 65 20 6c 65 61 66 20 6e 6f 64 65 73 20 66 72 6f  e leaf nodes fro
37746 6d 20 69 53 74 61 72 74 4c 65 61 66 20 74 6f 0a  m iStartLeaf to.
37747 2a 2a 20 69 45 6e 64 4c 65 61 66 20 28 69 6e 63  ** iEndLeaf (inc
37748 6c 75 73 69 76 65 29 20 61 73 20 69 6e 70 75 74  lusive) as input
37749 2c 20 61 6e 64 20 6d 65 72 67 65 20 74 68 65 20  , and merge the 
3774a 72 65 73 75 6c 74 69 6e 67 20 64 6f 63 6c 69 73  resulting doclis
3774b 74 20 69 6e 74 6f 0a 2a 2a 20 6f 75 74 2e 0a 2a  t into.** out..*
3774c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 61  /.static int loa
3774d 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73 28 66  dSegmentLeaves(f
3774e 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
3774f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
37750 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
37751 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72  lite_int64 iStar
37752 74 4c 65 61 66 2c 20 73 71 6c 69 74 65 5f 69 6e  tLeaf, sqlite_in
37753 74 36 34 20 69 45 6e 64 4c 65 61 66 2c 0a 20 20  t64 iEndLeaf,.  
37754 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37755 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
37756 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e   char *pTerm, in
37757 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50  t nTerm, int isP
37758 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20  refix,.         
37759 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3775a 20 20 20 20 44 61 74 61 42 75 66 66 65 72 20 2a      DataBuffer *
3775b 6f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  out){.  int rc;.
3775c 20 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 72    LeavesReader r
3775d 65 61 64 65 72 3b 0a 0a 20 20 61 73 73 65 72 74  eader;..  assert
3775e 28 20 69 53 74 61 72 74 4c 65 61 66 3c 3d 69 45  ( iStartLeaf<=iE
3775f 6e 64 4c 65 61 66 20 29 3b 0a 20 20 72 63 20 3d  ndLeaf );.  rc =
37760 20 6c 65 61 76 65 73 52 65 61 64 65 72 49 6e 69   leavesReaderIni
37761 74 28 76 2c 20 30 2c 20 69 53 74 61 72 74 4c 65  t(v, 0, iStartLe
37762 61 66 2c 20 69 45 6e 64 4c 65 61 66 2c 20 4e 55  af, iEndLeaf, NU
37763 4c 4c 2c 20 30 2c 20 26 72 65 61 64 65 72 29 3b  LL, 0, &reader);
37764 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
37765 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
37766 3b 0a 0a 20 20 72 63 20 3d 20 6c 6f 61 64 53 65  ;..  rc = loadSe
37767 67 6d 65 6e 74 4c 65 61 76 65 73 49 6e 74 28 76  gmentLeavesInt(v
37768 2c 20 26 72 65 61 64 65 72 2c 20 70 54 65 72 6d  , &reader, pTerm
37769 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69  , nTerm, isPrefi
3776a 78 2c 20 6f 75 74 29 3b 0a 20 20 6c 65 61 76 65  x, out);.  leave
3776b 73 52 65 61 64 65 72 52 65 73 65 74 28 26 72 65  sReaderReset(&re
3776c 61 64 65 72 29 3b 0a 20 20 6c 65 61 76 65 73 52  ader);.  leavesR
3776d 65 61 64 65 72 44 65 73 74 72 6f 79 28 26 72 65  eaderDestroy(&re
3776e 61 64 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  ader);.  return 
3776f 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 61 6b 69 6e 67  rc;.}../* Taking
37770 20 70 44 61 74 61 2f 6e 44 61 74 61 20 61 73 20   pData/nData as 
37771 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  an interior node
37772 2c 20 66 69 6e 64 20 74 68 65 20 73 65 71 75 65  , find the seque
37773 6e 63 65 20 6f 66 20 63 68 69 6c 64 0a 2a 2a 20  nce of child.** 
37774 6e 6f 64 65 73 20 77 68 69 63 68 20 63 6f 75 6c  nodes which coul
37775 64 20 69 6e 63 6c 75 64 65 20 70 54 65 72 6d 2f  d include pTerm/
37776 6e 54 65 72 6d 2f 69 73 50 72 65 66 69 78 2e 20  nTerm/isPrefix. 
37777 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 0a 2a   Note that the.*
37778 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20  * interior node 
37779 74 65 72 6d 73 20 6c 6f 67 69 63 61 6c 6c 79 20  terms logically 
3777a 63 6f 6d 65 20 62 65 74 77 65 65 6e 20 74 68 65  come between the
3777b 20 62 6c 6f 63 6b 73 2c 20 73 6f 20 74 68 65 72   blocks, so ther
3777c 65 20 69 73 0a 2a 2a 20 6f 6e 65 20 6d 6f 72 65  e is.** one more
3777d 20 62 6c 6f 63 6b 69 64 20 74 68 61 6e 20 74 68   blockid than th
3777e 65 72 65 20 61 72 65 20 74 65 72 6d 73 20 28 74  ere are terms (t
3777f 68 61 74 20 62 6c 6f 63 6b 20 63 6f 6e 74 61 69  hat block contai
37780 6e 73 20 74 65 72 6d 73 20 3e 3d 0a 2a 2a 20 74  ns terms >=.** t
37781 68 65 20 6c 61 73 74 20 69 6e 74 65 72 69 6f 72  he last interior
37782 2d 6e 6f 64 65 20 74 65 72 6d 29 2e 0a 2a 2f 0a  -node term)..*/.
37783 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54  /* TODO(shess) T
37784 68 65 20 63 61 6c 6c 69 6e 67 20 63 6f 64 65 20  he calling code 
37785 6d 61 79 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77  may already know
37786 20 74 68 61 74 20 74 68 65 20 65 6e 64 20 63 68   that the end ch
37787 69 6c 64 20 69 73 0a 2a 2a 20 6e 6f 74 20 77 6f  ild is.** not wo
37788 72 74 68 20 63 61 6c 63 75 6c 61 74 69 6e 67 2c  rth calculating,
37789 20 62 65 63 61 75 73 65 20 74 68 65 20 65 6e 64   because the end
3778a 20 6d 61 79 20 62 65 20 69 6e 20 61 20 6c 61 74   may be in a lat
3778b 65 72 20 73 69 62 6c 69 6e 67 0a 2a 2a 20 6e 6f  er sibling.** no
3778c 64 65 2e 20 20 43 6f 6e 73 69 64 65 72 20 77 68  de.  Consider wh
3778d 65 74 68 65 72 20 62 72 65 61 6b 69 6e 67 20 73  ether breaking s
3778e 79 6d 6d 65 74 72 79 20 69 73 20 77 6f 72 74 68  ymmetry is worth
3778f 77 68 69 6c 65 2e 20 20 49 20 73 75 73 70 65 63  while.  I suspec
37790 74 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74 20 77  t.** it is not w
37791 6f 72 74 68 77 68 69 6c 65 2e 0a 2a 2f 0a 73 74  orthwhile..*/.st
37792 61 74 69 63 20 76 6f 69 64 20 67 65 74 43 68 69  atic void getChi
37793 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28  ldrenContaining(
37794 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
37795 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20  a, int nData,.  
37796 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37797 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37798 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
37799 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e  m, int nTerm, in
3779a 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20 20 20  t isPrefix,.    
3779b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3779c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
3779d 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69 53 74  lite_int64 *piSt
3779e 61 72 74 43 68 69 6c 64 2c 0a 20 20 20 20 20 20  artChild,.      
3779f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
377a0 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
377a1 74 65 5f 69 6e 74 36 34 20 2a 70 69 45 6e 64 43  te_int64 *piEndC
377a2 68 69 6c 64 29 7b 0a 20 20 49 6e 74 65 72 69 6f  hild){.  Interio
377a3 72 52 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a  rReader reader;.
377a4 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61 74 61  .  assert( nData
377a5 3e 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >1 );.  assert( 
377a6 2a 70 44 61 74 61 21 3d 27 5c 30 27 20 29 3b 0a  *pData!='\0' );.
377a7 20 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72    interiorReader
377a8 49 6e 69 74 28 70 44 61 74 61 2c 20 6e 44 61 74  Init(pData, nDat
377a9 61 2c 20 26 72 65 61 64 65 72 29 3b 0a 0a 20 20  a, &reader);..  
377aa 2f 2a 20 53 63 61 6e 20 66 6f 72 20 74 68 65 20  /* Scan for the 
377ab 66 69 72 73 74 20 63 68 69 6c 64 20 77 68 69 63  first child whic
377ac 68 20 63 6f 75 6c 64 20 63 6f 6e 74 61 69 6e 20  h could contain 
377ad 70 54 65 72 6d 2f 6e 54 65 72 6d 2e 20 2a 2f 0a  pTerm/nTerm. */.
377ae 20 20 77 68 69 6c 65 28 20 21 69 6e 74 65 72 69    while( !interi
377af 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28 26 72  orReaderAtEnd(&r
377b0 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20 69 66  eader) ){.    if
377b1 28 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72  ( interiorReader
377b2 54 65 72 6d 43 6d 70 28 26 72 65 61 64 65 72 2c  TermCmp(&reader,
377b3 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 30   pTerm, nTerm, 0
377b4 29 3e 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  )>0 ) break;.   
377b5 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 53   interiorReaderS
377b6 74 65 70 28 26 72 65 61 64 65 72 29 3b 0a 20 20  tep(&reader);.  
377b7 7d 0a 20 20 2a 70 69 53 74 61 72 74 43 68 69 6c  }.  *piStartChil
377b8 64 20 3d 20 69 6e 74 65 72 69 6f 72 52 65 61 64  d = interiorRead
377b9 65 72 43 75 72 72 65 6e 74 42 6c 6f 63 6b 69 64  erCurrentBlockid
377ba 28 26 72 65 61 64 65 72 29 3b 0a 0a 20 20 2f 2a  (&reader);..  /*
377bb 20 4b 65 65 70 20 73 63 61 6e 6e 69 6e 67 20 74   Keep scanning t
377bc 6f 20 66 69 6e 64 20 61 20 74 65 72 6d 20 67 72  o find a term gr
377bd 65 61 74 65 72 20 74 68 61 6e 20 6f 75 72 20 74  eater than our t
377be 65 72 6d 2c 20 75 73 69 6e 67 20 70 72 65 66 69  erm, using prefi
377bf 78 0a 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f  x.  ** compariso
377c0 6e 20 69 66 20 69 6e 64 69 63 61 74 65 64 2e 20  n if indicated. 
377c1 20 49 66 20 69 73 50 72 65 66 69 78 20 69 73 20   If isPrefix is 
377c2 66 61 6c 73 65 2c 20 74 68 69 73 20 77 69 6c 6c  false, this will
377c3 20 62 65 20 74 68 65 0a 20 20 2a 2a 20 73 61 6d   be the.  ** sam
377c4 65 20 62 6c 6f 63 6b 69 64 20 61 73 20 74 68 65  e blockid as the
377c5 20 73 74 61 72 74 69 6e 67 20 62 6c 6f 63 6b 2e   starting block.
377c6 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 21  .  */.  while( !
377c7 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 41 74  interiorReaderAt
377c8 45 6e 64 28 26 72 65 61 64 65 72 29 20 29 7b 0a  End(&reader) ){.
377c9 20 20 20 20 69 66 28 20 69 6e 74 65 72 69 6f 72      if( interior
377ca 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 26 72  ReaderTermCmp(&r
377cb 65 61 64 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54  eader, pTerm, nT
377cc 65 72 6d 2c 20 69 73 50 72 65 66 69 78 29 3e 30  erm, isPrefix)>0
377cd 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 6e   ) break;.    in
377ce 74 65 72 69 6f 72 52 65 61 64 65 72 53 74 65 70  teriorReaderStep
377cf 28 26 72 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20  (&reader);.  }. 
377d0 20 2a 70 69 45 6e 64 43 68 69 6c 64 20 3d 20 69   *piEndChild = i
377d1 6e 74 65 72 69 6f 72 52 65 61 64 65 72 43 75 72  nteriorReaderCur
377d2 72 65 6e 74 42 6c 6f 63 6b 69 64 28 26 72 65 61  rentBlockid(&rea
377d3 64 65 72 29 3b 0a 0a 20 20 69 6e 74 65 72 69 6f  der);..  interio
377d4 72 52 65 61 64 65 72 44 65 73 74 72 6f 79 28 26  rReaderDestroy(&
377d5 72 65 61 64 65 72 29 3b 0a 0a 20 20 2f 2a 20 43  reader);..  /* C
377d6 68 69 6c 64 72 65 6e 20 6d 75 73 74 20 61 73 63  hildren must asc
377d7 65 6e 64 2c 20 61 6e 64 20 69 66 20 21 70 72 65  end, and if !pre
377d8 66 69 78 2c 20 62 6f 74 68 20 6d 75 73 74 20 62  fix, both must b
377d9 65 20 74 68 65 20 73 61 6d 65 2e 20 2a 2f 0a 20  e the same. */. 
377da 20 61 73 73 65 72 74 28 20 2a 70 69 45 6e 64 43   assert( *piEndC
377db 68 69 6c 64 3e 3d 2a 70 69 53 74 61 72 74 43 68  hild>=*piStartCh
377dc 69 6c 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ild );.  assert(
377dd 20 69 73 50 72 65 66 69 78 20 7c 7c 20 2a 70 69   isPrefix || *pi
377de 53 74 61 72 74 43 68 69 6c 64 3d 3d 2a 70 69 45  StartChild==*piE
377df 6e 64 43 68 69 6c 64 20 29 3b 0a 7d 0a 0a 2f 2a  ndChild );.}../*
377e0 20 52 65 61 64 20 62 6c 6f 63 6b 20 61 74 20 69   Read block at i
377e1 42 6c 6f 63 6b 69 64 20 61 6e 64 20 70 61 73 73  Blockid and pass
377e2 20 69 74 20 77 69 74 68 20 6f 74 68 65 72 20 70   it with other p
377e3 61 72 61 6d 73 20 74 6f 0a 2a 2a 20 67 65 74 43  arams to.** getC
377e4 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e  hildrenContainin
377e5 67 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  g()..*/.static i
377e6 6e 74 20 6c 6f 61 64 41 6e 64 47 65 74 43 68 69  nt loadAndGetChi
377e7 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28  ldrenContaining(
377e8 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  .  fulltext_vtab
377e9 20 2a 76 2c 0a 20 20 73 71 6c 69 74 65 5f 69 6e   *v,.  sqlite_in
377ea 74 36 34 20 69 42 6c 6f 63 6b 69 64 2c 0a 20 20  t64 iBlockid,.  
377eb 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
377ec 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e  m, int nTerm, in
377ed 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20 73 71  t isPrefix,.  sq
377ee 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69 53 74  lite_int64 *piSt
377ef 61 72 74 43 68 69 6c 64 2c 20 73 71 6c 69 74 65  artChild, sqlite
377f0 5f 69 6e 74 36 34 20 2a 70 69 45 6e 64 43 68 69  _int64 *piEndChi
377f1 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  ld.){.  sqlite3_
377f2 73 74 6d 74 20 2a 73 20 3d 20 4e 55 4c 4c 3b 0a  stmt *s = NULL;.
377f3 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73    int rc;..  ass
377f4 65 72 74 28 20 69 42 6c 6f 63 6b 69 64 21 3d 30  ert( iBlockid!=0
377f5 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54   );.  assert( pT
377f6 65 72 6d 21 3d 4e 55 4c 4c 20 29 3b 0a 20 20 61  erm!=NULL );.  a
377f7 73 73 65 72 74 28 20 6e 54 65 72 6d 21 3d 30 20  ssert( nTerm!=0 
377f8 29 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 4f 44  );        /* TOD
377f9 4f 28 73 68 65 73 73 29 20 57 68 79 20 6e 6f 74  O(shess) Why not
377fa 20 61 6c 6c 6f 77 20 74 68 69 73 3f 20 2a 2f 0a   allow this? */.
377fb 20 20 61 73 73 65 72 74 28 20 70 69 53 74 61 72    assert( piStar
377fc 74 43 68 69 6c 64 21 3d 4e 55 4c 4c 20 29 3b 0a  tChild!=NULL );.
377fd 20 20 61 73 73 65 72 74 28 20 70 69 45 6e 64 43    assert( piEndC
377fe 68 69 6c 64 21 3d 4e 55 4c 4c 20 29 3b 0a 0a 20  hild!=NULL );.. 
377ff 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
37800 61 74 65 6d 65 6e 74 28 76 2c 20 42 4c 4f 43 4b  atement(v, BLOCK
37801 5f 53 45 4c 45 43 54 5f 53 54 4d 54 2c 20 26 73  _SELECT_STMT, &s
37802 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
37803 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
37804 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  rc;..  rc = sqli
37805 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73  te3_bind_int64(s
37806 2c 20 31 2c 20 69 42 6c 6f 63 6b 69 64 29 3b 0a  , 1, iBlockid);.
37807 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
37808 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
37809 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
3780a 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20  _step(s);.  if( 
3780b 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
3780c 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
3780d 45 52 52 4f 52 3b 0a 20 20 69 66 28 20 72 63 21  ERROR;.  if( rc!
3780e 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65  =SQLITE_ROW ) re
3780f 74 75 72 6e 20 72 63 3b 0a 0a 20 20 67 65 74 43  turn rc;..  getC
37810 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e  hildrenContainin
37811 67 28 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  g(sqlite3_column
37812 5f 62 6c 6f 62 28 73 2c 20 30 29 2c 20 73 71 6c  _blob(s, 0), sql
37813 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
37814 73 28 73 2c 20 30 29 2c 0a 20 20 20 20 20 20 20  s(s, 0),.       
37815 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37816 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69   pTerm, nTerm, i
37817 73 50 72 65 66 69 78 2c 20 70 69 53 74 61 72 74  sPrefix, piStart
37818 43 68 69 6c 64 2c 20 70 69 45 6e 64 43 68 69 6c  Child, piEndChil
37819 64 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70  d);..  /* We exp
3781a 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77  ect only one row
3781b 2e 20 20 57 65 20 6d 75 73 74 20 65 78 65 63 75  .  We must execu
3781c 74 65 20 61 6e 6f 74 68 65 72 20 73 71 6c 69 74  te another sqlit
3781d 65 33 5f 73 74 65 70 28 29 0a 20 20 20 2a 20 74  e3_step().   * t
3781e 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 69  o complete the i
3781f 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77  teration; otherw
37820 69 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69  ise the table wi
37821 6c 6c 20 72 65 6d 61 69 6e 0a 20 20 20 2a 20 6c  ll remain.   * l
37822 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d  ocked. */.  rc =
37823 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
37824 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
37825 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20  TE_ROW ) return 
37826 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
37827 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44  if( rc!=SQLITE_D
37828 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 72 63 3b  ONE ) return rc;
37829 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
3782a 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 72 61 76  E_OK;.}../* Trav
3782b 65 72 73 65 20 74 68 65 20 74 72 65 65 20 72 65  erse the tree re
3782c 70 72 65 73 65 6e 74 65 64 20 62 79 20 70 44 61  presented by pDa
3782d 74 61 5b 6e 44 61 74 61 5d 20 6c 6f 6f 6b 69 6e  ta[nData] lookin
3782e 67 20 66 6f 72 0a 2a 2a 20 70 54 65 72 6d 5b 6e  g for.** pTerm[n
3782f 54 65 72 6d 5d 2c 20 70 6c 61 63 69 6e 67 20 69  Term], placing i
37830 74 73 20 64 6f 63 6c 69 73 74 20 69 6e 74 6f 20  ts doclist into 
37831 2a 6f 75 74 2e 20 20 54 68 69 73 20 69 73 20 69  *out.  This is i
37832 6e 74 65 72 6e 61 6c 20 74 6f 0a 2a 2a 20 6c 6f  nternal to.** lo
37833 61 64 53 65 67 6d 65 6e 74 28 29 20 74 6f 20 6d  adSegment() to m
37834 61 6b 65 20 65 72 72 6f 72 2d 68 61 6e 64 6c 69  ake error-handli
37835 6e 67 20 63 6c 65 61 6e 65 72 2e 0a 2a 2f 0a 73  ng cleaner..*/.s
37836 74 61 74 69 63 20 69 6e 74 20 6c 6f 61 64 53 65  tatic int loadSe
37837 67 6d 65 6e 74 49 6e 74 28 66 75 6c 6c 74 65 78  gmentInt(fulltex
37838 74 5f 76 74 61 62 20 2a 76 2c 20 63 6f 6e 73 74  t_vtab *v, const
37839 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e   char *pData, in
3783a 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20  t nData,.       
3783b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3783c 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
3783d 69 4c 65 61 76 65 73 45 6e 64 2c 0a 20 20 20 20  iLeavesEnd,.    
3783e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3783f 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
37840 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65   *pTerm, int nTe
37841 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78  rm, int isPrefix
37842 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
37843 20 20 20 20 20 20 20 20 20 20 20 20 44 61 74 61              Data
37844 42 75 66 66 65 72 20 2a 6f 75 74 29 7b 0a 20 20  Buffer *out){.  
37845 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 20  /* Special case 
37846 77 68 65 72 65 20 72 6f 6f 74 20 69 73 20 61 20  where root is a 
37847 6c 65 61 66 2e 20 2a 2f 0a 20 20 69 66 28 20 2a  leaf. */.  if( *
37848 70 44 61 74 61 3d 3d 27 5c 30 27 20 29 7b 0a 20  pData=='\0' ){. 
37849 20 20 20 72 65 74 75 72 6e 20 6c 6f 61 64 53 65     return loadSe
3784a 67 6d 65 6e 74 4c 65 61 66 28 76 2c 20 70 44 61  gmentLeaf(v, pDa
3784b 74 61 2c 20 6e 44 61 74 61 2c 20 70 54 65 72 6d  ta, nData, pTerm
3784c 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69  , nTerm, isPrefi
3784d 78 2c 20 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65  x, out);.  }else
3784e 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20  {.    int rc;.  
3784f 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
37850 53 74 61 72 74 43 68 69 6c 64 2c 20 69 45 6e 64  StartChild, iEnd
37851 43 68 69 6c 64 3b 0a 0a 20 20 20 20 2f 2a 20 50  Child;..    /* P
37852 72 6f 63 65 73 73 20 70 44 61 74 61 20 61 73 20  rocess pData as 
37853 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  an interior node
37854 2c 20 74 68 65 6e 20 6c 6f 6f 70 20 64 6f 77 6e  , then loop down
37855 20 74 68 65 20 74 72 65 65 0a 20 20 20 20 2a 2a   the tree.    **
37856 20 75 6e 74 69 6c 20 77 65 20 66 69 6e 64 20 74   until we find t
37857 68 65 20 73 65 74 20 6f 66 20 6c 65 61 66 20 6e  he set of leaf n
37858 6f 64 65 73 20 74 6f 20 73 63 61 6e 20 66 6f 72  odes to scan for
37859 20 74 68 65 20 74 65 72 6d 2e 0a 20 20 20 20 2a   the term..    *
3785a 2f 0a 20 20 20 20 67 65 74 43 68 69 6c 64 72 65  /.    getChildre
3785b 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 70 44 61 74  nContaining(pDat
3785c 61 2c 20 6e 44 61 74 61 2c 20 70 54 65 72 6d 2c  a, nData, pTerm,
3785d 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78   nTerm, isPrefix
3785e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
3785f 20 20 20 20 20 20 20 20 20 20 20 20 26 69 53 74              &iSt
37860 61 72 74 43 68 69 6c 64 2c 20 26 69 45 6e 64 43  artChild, &iEndC
37861 68 69 6c 64 29 3b 0a 20 20 20 20 77 68 69 6c 65  hild);.    while
37862 28 20 69 53 74 61 72 74 43 68 69 6c 64 3e 69 4c  ( iStartChild>iL
37863 65 61 76 65 73 45 6e 64 20 29 7b 0a 20 20 20 20  eavesEnd ){.    
37864 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
37865 4e 65 78 74 53 74 61 72 74 2c 20 69 4e 65 78 74  NextStart, iNext
37866 45 6e 64 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  End;.      rc = 
37867 6c 6f 61 64 41 6e 64 47 65 74 43 68 69 6c 64 72  loadAndGetChildr
37868 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 76 2c 20  enContaining(v, 
37869 69 53 74 61 72 74 43 68 69 6c 64 2c 20 70 54 65  iStartChild, pTe
3786a 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65  rm, nTerm, isPre
3786b 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20  fix,.           
3786c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3786d 20 20 20 20 20 20 20 20 20 20 20 20 20 26 69 4e               &iN
3786e 65 78 74 53 74 61 72 74 2c 20 26 69 4e 65 78 74  extStart, &iNext
3786f 45 6e 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20  End);.      if( 
37870 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
37871 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20  return rc;..    
37872 20 20 2f 2a 20 49 66 20 77 65 27 76 65 20 62 72    /* If we've br
37873 61 6e 63 68 65 64 2c 20 66 6f 6c 6c 6f 77 20 74  anched, follow t
37874 68 65 20 65 6e 64 20 62 72 61 6e 63 68 2c 20 74  he end branch, t
37875 6f 6f 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  oo. */.      if(
37876 20 69 53 74 61 72 74 43 68 69 6c 64 21 3d 69 45   iStartChild!=iE
37877 6e 64 43 68 69 6c 64 20 29 7b 0a 20 20 20 20 20  ndChild ){.     
37878 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
37879 69 44 75 6d 6d 79 3b 0a 20 20 20 20 20 20 20 20  iDummy;.        
3787a 72 63 20 3d 20 6c 6f 61 64 41 6e 64 47 65 74 43  rc = loadAndGetC
3787b 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e  hildrenContainin
3787c 67 28 76 2c 20 69 45 6e 64 43 68 69 6c 64 2c 20  g(v, iEndChild, 
3787d 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73  pTerm, nTerm, is
3787e 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20  Prefix,.        
3787f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37881 20 20 26 69 44 75 6d 6d 79 2c 20 26 69 4e 65 78    &iDummy, &iNex
37882 74 45 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 69  tEnd);.        i
37883 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
37884 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
37885 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73      }..      ass
37886 65 72 74 28 20 69 4e 65 78 74 53 74 61 72 74 3c  ert( iNextStart<
37887 3d 69 4e 65 78 74 45 6e 64 20 29 3b 0a 20 20 20  =iNextEnd );.   
37888 20 20 20 69 53 74 61 72 74 43 68 69 6c 64 20 3d     iStartChild =
37889 20 69 4e 65 78 74 53 74 61 72 74 3b 0a 20 20 20   iNextStart;.   
3788a 20 20 20 69 45 6e 64 43 68 69 6c 64 20 3d 20 69     iEndChild = i
3788b 4e 65 78 74 45 6e 64 3b 0a 20 20 20 20 7d 0a 20  NextEnd;.    }. 
3788c 20 20 20 61 73 73 65 72 74 28 20 69 53 74 61 72     assert( iStar
3788d 74 43 68 69 6c 64 3c 3d 69 4c 65 61 76 65 73 45  tChild<=iLeavesE
3788e 6e 64 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  nd );.    assert
3788f 28 20 69 45 6e 64 43 68 69 6c 64 3c 3d 69 4c 65  ( iEndChild<=iLe
37890 61 76 65 73 45 6e 64 20 29 3b 0a 0a 20 20 20 20  avesEnd );..    
37891 2f 2a 20 53 63 61 6e 20 74 68 72 6f 75 67 68 20  /* Scan through 
37892 74 68 65 20 6c 65 61 66 20 73 65 67 6d 65 6e 74  the leaf segment
37893 73 20 66 6f 72 20 64 6f 63 6c 69 73 74 73 2e 20  s for doclists. 
37894 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 6c 6f  */.    return lo
37895 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73 28  adSegmentLeaves(
37896 76 2c 20 69 53 74 61 72 74 43 68 69 6c 64 2c 20  v, iStartChild, 
37897 69 45 6e 64 43 68 69 6c 64 2c 0a 20 20 20 20 20  iEndChild,.     
37898 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37899 20 20 20 20 20 20 20 20 70 54 65 72 6d 2c 20 6e          pTerm, n
3789a 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 20  Term, isPrefix, 
3789b 6f 75 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20  out);.  }.}../* 
3789c 43 61 6c 6c 20 6c 6f 61 64 53 65 67 6d 65 6e 74  Call loadSegment
3789d 49 6e 74 28 29 20 74 6f 20 63 6f 6c 6c 65 63 74  Int() to collect
3789e 20 74 68 65 20 64 6f 63 6c 69 73 74 20 66 6f 72   the doclist for
3789f 20 70 54 65 72 6d 2f 6e 54 65 72 6d 2c 20 74 68   pTerm/nTerm, th
378a0 65 6e 0a 2a 2a 20 6d 65 72 67 65 20 69 74 73 20  en.** merge its 
378a1 64 6f 63 6c 69 73 74 20 6f 76 65 72 20 2a 6f 75  doclist over *ou
378a2 74 20 28 61 6e 79 20 64 75 70 6c 69 63 61 74 65  t (any duplicate
378a3 20 64 6f 63 6c 69 73 74 73 20 72 65 61 64 20 66   doclists read f
378a4 72 6f 6d 20 74 68 65 0a 2a 2a 20 73 65 67 6d 65  rom the.** segme
378a5 6e 74 20 72 6f 6f 74 65 64 20 61 74 20 70 44 61  nt rooted at pDa
378a6 74 61 20 77 69 6c 6c 20 6f 76 65 72 77 72 69 74  ta will overwrit
378a7 65 20 74 68 6f 73 65 20 69 6e 20 2a 6f 75 74 29  e those in *out)
378a8 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  ..*/./* TODO(she
378a9 73 73 29 20 43 6f 6e 73 69 64 65 72 20 63 68 61  ss) Consider cha
378aa 6e 67 69 6e 67 20 74 68 69 73 20 74 6f 20 64 65  nging this to de
378ab 74 65 72 6d 69 6e 65 20 74 68 65 20 64 65 70 74  termine the dept
378ac 68 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 65 61 76  h of the.** leav
378ad 65 73 20 75 73 69 6e 67 20 65 69 74 68 65 72 20  es using either 
378ae 74 68 65 20 66 69 72 73 74 20 63 68 61 72 61 63  the first charac
378af 74 65 72 73 20 6f 66 20 69 6e 74 65 72 69 6f 72  ters of interior
378b0 20 6e 6f 64 65 73 20 28 77 68 65 6e 0a 2a 2a 20   nodes (when.** 
378b1 3d 3d 31 2c 20 77 65 27 72 65 20 6f 6e 65 20 6c  ==1, we're one l
378b2 65 76 65 6c 20 61 62 6f 76 65 20 74 68 65 20 6c  evel above the l
378b3 65 61 76 65 73 29 2c 20 6f 72 20 74 68 65 20 66  eaves), or the f
378b4 69 72 73 74 20 63 68 61 72 61 63 74 65 72 20 6f  irst character o
378b5 66 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20 28 77  f.** the root (w
378b6 68 69 63 68 20 77 69 6c 6c 20 64 65 73 63 72 69  hich will descri
378b7 62 65 20 74 68 65 20 68 65 69 67 68 74 20 6f 66  be the height of
378b8 20 74 68 65 20 74 72 65 65 20 64 69 72 65 63 74   the tree direct
378b9 6c 79 29 2e 0a 2a 2a 20 45 69 74 68 65 72 20 66  ly)..** Either f
378ba 65 65 6c 73 20 73 6f 6d 65 77 68 61 74 20 74 72  eels somewhat tr
378bb 69 63 6b 79 20 74 6f 20 6d 65 2e 0a 2a 2f 0a 2f  icky to me..*/./
378bc 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68  * TODO(shess) Th
378bd 65 20 63 75 72 72 65 6e 74 20 6d 65 72 67 65 20  e current merge 
378be 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20  is likely to be 
378bf 73 6c 6f 77 20 66 6f 72 20 6c 61 72 67 65 0a 2a  slow for large.*
378c0 2a 20 64 6f 63 6c 69 73 74 73 20 28 74 68 6f 75  * doclists (thou
378c1 67 68 20 69 74 20 73 68 6f 75 6c 64 20 70 72 6f  gh it should pro
378c2 63 65 73 73 20 66 72 6f 6d 20 6e 65 77 65 73 74  cess from newest
378c3 2f 73 6d 61 6c 6c 65 73 74 20 74 6f 0a 2a 2a 20  /smallest to.** 
378c4 6f 6c 64 65 73 74 2f 6c 61 72 67 65 73 74 2c 20  oldest/largest, 
378c5 73 6f 20 69 74 20 6d 61 79 20 6e 6f 74 20 62 65  so it may not be
378c6 20 74 68 61 74 20 62 61 64 29 2e 20 20 49 74 20   that bad).  It 
378c7 6d 69 67 68 74 20 62 65 20 75 73 65 66 75 6c 20  might be useful 
378c8 74 6f 0a 2a 2a 20 6d 6f 64 69 66 79 20 74 68 69  to.** modify thi
378c9 6e 67 73 20 74 6f 20 61 6c 6c 6f 77 20 66 6f 72  ngs to allow for
378ca 20 4e 2d 77 61 79 20 6d 65 72 67 69 6e 67 2e 20   N-way merging. 
378cb 20 54 68 69 73 20 63 6f 75 6c 64 20 65 69 74 68   This could eith
378cc 65 72 20 62 65 0a 2a 2a 20 77 69 74 68 69 6e 20  er be.** within 
378cd 61 20 73 65 67 6d 65 6e 74 2c 20 77 69 74 68 20  a segment, with 
378ce 70 61 69 72 77 69 73 65 20 6d 65 72 67 65 73 20  pairwise merges 
378cf 61 63 72 6f 73 73 20 73 65 67 6d 65 6e 74 73 2c  across segments,
378d0 20 6f 72 20 61 63 72 6f 73 73 0a 2a 2a 20 61 6c   or across.** al
378d1 6c 20 73 65 67 6d 65 6e 74 73 20 61 74 20 6f 6e  l segments at on
378d2 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ce..*/.static in
378d3 74 20 6c 6f 61 64 53 65 67 6d 65 6e 74 28 66 75  t loadSegment(fu
378d4 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
378d5 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
378d6 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20  a, int nData,.  
378d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
378d8 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
378d9 34 20 69 4c 65 61 76 65 73 45 6e 64 2c 0a 20 20  4 iLeavesEnd,.  
378da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
378db 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
378dc 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72  *pTerm, int nTer
378dd 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78 2c  m, int isPrefix,
378de 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
378df 20 20 20 20 20 20 20 20 44 61 74 61 42 75 66 66          DataBuff
378e0 65 72 20 2a 6f 75 74 29 7b 0a 20 20 44 61 74 61  er *out){.  Data
378e1 42 75 66 66 65 72 20 72 65 73 75 6c 74 3b 0a 20  Buffer result;. 
378e2 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65   int rc;..  asse
378e3 72 74 28 20 6e 44 61 74 61 3e 31 20 29 3b 0a 0a  rt( nData>1 );..
378e4 20 20 2f 2a 20 54 68 69 73 20 63 6f 64 65 20 73    /* This code s
378e5 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 63  hould never be c
378e6 61 6c 6c 65 64 20 77 69 74 68 20 62 75 66 66 65  alled with buffe
378e7 72 65 64 20 75 70 64 61 74 65 73 2e 20 2a 2f 0a  red updates. */.
378e8 20 20 61 73 73 65 72 74 28 20 76 2d 3e 6e 50 65    assert( v->nPe
378e9 6e 64 69 6e 67 44 61 74 61 3c 30 20 29 3b 0a 0a  ndingData<0 );..
378ea 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74    dataBufferInit
378eb 28 26 72 65 73 75 6c 74 2c 20 30 29 3b 0a 20 20  (&result, 0);.  
378ec 72 63 20 3d 20 6c 6f 61 64 53 65 67 6d 65 6e 74  rc = loadSegment
378ed 49 6e 74 28 76 2c 20 70 44 61 74 61 2c 20 6e 44  Int(v, pData, nD
378ee 61 74 61 2c 20 69 4c 65 61 76 65 73 45 6e 64 2c  ata, iLeavesEnd,
378ef 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
378f0 20 20 20 20 20 20 20 70 54 65 72 6d 2c 20 6e 54         pTerm, nT
378f1 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 20 26  erm, isPrefix, &
378f2 72 65 73 75 6c 74 29 3b 0a 20 20 69 66 28 20 72  result);.  if( r
378f3 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
378f4 72 65 73 75 6c 74 2e 6e 44 61 74 61 3e 30 20 29  result.nData>0 )
378f5 7b 0a 20 20 20 20 69 66 28 20 6f 75 74 2d 3e 6e  {.    if( out->n
378f6 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Data==0 ){.     
378f7 20 44 61 74 61 42 75 66 66 65 72 20 74 6d 70 20   DataBuffer tmp 
378f8 3d 20 2a 6f 75 74 3b 0a 20 20 20 20 20 20 2a 6f  = *out;.      *o
378f9 75 74 20 3d 20 72 65 73 75 6c 74 3b 0a 20 20 20  ut = result;.   
378fa 20 20 20 72 65 73 75 6c 74 20 3d 20 74 6d 70 3b     result = tmp;
378fb 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
378fc 20 20 44 61 74 61 42 75 66 66 65 72 20 6d 65 72    DataBuffer mer
378fd 67 65 64 3b 0a 20 20 20 20 20 20 44 4c 52 65 61  ged;.      DLRea
378fe 64 65 72 20 72 65 61 64 65 72 73 5b 32 5d 3b 0a  der readers[2];.
378ff 0a 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28 26  .      dlrInit(&
37900 72 65 61 64 65 72 73 5b 30 5d 2c 20 44 4c 5f 44  readers[0], DL_D
37901 45 46 41 55 4c 54 2c 20 6f 75 74 2d 3e 70 44 61  EFAULT, out->pDa
37902 74 61 2c 20 6f 75 74 2d 3e 6e 44 61 74 61 29 3b  ta, out->nData);
37903 0a 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28 26  .      dlrInit(&
37904 72 65 61 64 65 72 73 5b 31 5d 2c 20 44 4c 5f 44  readers[1], DL_D
37905 45 46 41 55 4c 54 2c 20 72 65 73 75 6c 74 2e 70  EFAULT, result.p
37906 44 61 74 61 2c 20 72 65 73 75 6c 74 2e 6e 44 61  Data, result.nDa
37907 74 61 29 3b 0a 20 20 20 20 20 20 64 61 74 61 42  ta);.      dataB
37908 75 66 66 65 72 49 6e 69 74 28 26 6d 65 72 67 65  ufferInit(&merge
37909 64 2c 20 6f 75 74 2d 3e 6e 44 61 74 61 2b 72 65  d, out->nData+re
3790a 73 75 6c 74 2e 6e 44 61 74 61 29 3b 0a 20 20 20  sult.nData);.   
3790b 20 20 20 64 6f 63 4c 69 73 74 4d 65 72 67 65 28     docListMerge(
3790c 26 6d 65 72 67 65 64 2c 20 72 65 61 64 65 72 73  &merged, readers
3790d 2c 20 32 29 3b 0a 20 20 20 20 20 20 64 61 74 61  , 2);.      data
3790e 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 6f 75  BufferDestroy(ou
3790f 74 29 3b 0a 20 20 20 20 20 20 2a 6f 75 74 20 3d  t);.      *out =
37910 20 6d 65 72 67 65 64 3b 0a 20 20 20 20 20 20 64   merged;.      d
37911 6c 72 44 65 73 74 72 6f 79 28 26 72 65 61 64 65  lrDestroy(&reade
37912 72 73 5b 30 5d 29 3b 0a 20 20 20 20 20 20 64 6c  rs[0]);.      dl
37913 72 44 65 73 74 72 6f 79 28 26 72 65 61 64 65 72  rDestroy(&reader
37914 73 5b 31 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  s[1]);.    }.  }
37915 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73  .  dataBufferDes
37916 74 72 6f 79 28 26 72 65 73 75 6c 74 29 3b 0a 20  troy(&result);. 
37917 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
37918 2a 20 53 63 61 6e 20 74 68 65 20 64 61 74 61 62  * Scan the datab
37919 61 73 65 20 61 6e 64 20 6d 65 72 67 65 20 74 6f  ase and merge to
3791a 67 65 74 68 65 72 20 74 68 65 20 70 6f 73 74 69  gether the posti
3791b 6e 67 20 6c 69 73 74 73 20 66 6f 72 20 74 68 65  ng lists for the
3791c 20 74 65 72 6d 0a 2a 2a 20 69 6e 74 6f 20 2a 6f   term.** into *o
3791d 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ut..*/.static in
3791e 74 20 74 65 72 6d 53 65 6c 65 63 74 28 0a 20 20  t termSelect(.  
3791f 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
37920 2c 20 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  , .  int iColumn
37921 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ,.  const char *
37922 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d  pTerm, int nTerm
37923 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ,             /*
37924 20 54 65 72 6d 20 74 6f 20 71 75 65 72 79 20 66   Term to query f
37925 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 69 73 50 72  or */.  int isPr
37926 65 66 69 78 2c 20 20 20 20 20 20 20 20 20 20 20  efix,           
37927 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37928 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20    /* True for a 
37929 70 72 65 66 69 78 20 73 65 61 72 63 68 20 2a 2f  prefix search */
3792a 0a 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69  .  DocListType i
3792b 54 79 70 65 2c 20 0a 20 20 44 61 74 61 42 75 66  Type, .  DataBuf
3792c 66 65 72 20 2a 6f 75 74 20 20 20 20 20 20 20 20  fer *out        
3792d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3792e 20 20 20 2f 2a 20 57 72 69 74 65 20 72 65 73 75     /* Write resu
3792f 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  lts here */.){. 
37930 20 44 61 74 61 42 75 66 66 65 72 20 64 6f 63 6c   DataBuffer docl
37931 69 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  ist;.  sqlite3_s
37932 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63  tmt *s;.  int rc
37933 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65   = sql_get_state
37934 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 53  ment(v, SEGDIR_S
37935 45 4c 45 43 54 5f 41 4c 4c 5f 53 54 4d 54 2c 20  ELECT_ALL_STMT, 
37936 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &s);.  if( rc!=S
37937 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
37938 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 69 73  n rc;..  /* This
37939 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 6e 65 76   code should nev
3793a 65 72 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74  er be called wit
3793b 68 20 62 75 66 66 65 72 65 64 20 75 70 64 61 74  h buffered updat
3793c 65 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  es. */.  assert(
3793d 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61   v->nPendingData
3793e 3c 30 20 29 3b 0a 0a 20 20 64 61 74 61 42 75 66  <0 );..  dataBuf
3793f 66 65 72 49 6e 69 74 28 26 64 6f 63 6c 69 73 74  ferInit(&doclist
37940 2c 20 30 29 3b 0a 20 20 64 61 74 61 42 75 66 66  , 0);.  dataBuff
37941 65 72 49 6e 69 74 28 6f 75 74 2c 20 30 29 3b 0a  erInit(out, 0);.
37942 0a 20 20 2f 2a 20 54 72 61 76 65 72 73 65 20 74  .  /* Traverse t
37943 68 65 20 73 65 67 6d 65 6e 74 73 20 66 72 6f 6d  he segments from
37944 20 6f 6c 64 65 73 74 20 74 6f 20 6e 65 77 65 73   oldest to newes
37945 74 20 73 6f 20 74 68 61 74 20 6e 65 77 65 72 20  t so that newer 
37946 64 6f 63 6c 69 73 74 0a 20 20 2a 2a 20 65 6c 65  doclist.  ** ele
37947 6d 65 6e 74 73 20 66 6f 72 20 67 69 76 65 6e 20  ments for given 
37948 64 6f 63 69 64 73 20 6f 76 65 72 77 72 69 74 65  docids overwrite
37949 20 6f 6c 64 65 72 20 65 6c 65 6d 65 6e 74 73 2e   older elements.
3794a 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 28  .  */.  while( (
3794b 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  rc = sqlite3_ste
3794c 70 28 73 29 29 3d 3d 53 51 4c 49 54 45 5f 52 4f  p(s))==SQLITE_RO
3794d 57 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  W ){.    const c
3794e 68 61 72 20 2a 70 44 61 74 61 20 3d 20 73 71 6c  har *pData = sql
3794f 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
37950 28 73 2c 20 32 29 3b 0a 20 20 20 20 63 6f 6e 73  (s, 2);.    cons
37951 74 20 69 6e 74 20 6e 44 61 74 61 20 3d 20 73 71  t int nData = sq
37952 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
37953 65 73 28 73 2c 20 32 29 3b 0a 20 20 20 20 63 6f  es(s, 2);.    co
37954 6e 73 74 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  nst sqlite_int64
37955 20 69 4c 65 61 76 65 73 45 6e 64 20 3d 20 73 71   iLeavesEnd = sq
37956 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
37957 36 34 28 73 2c 20 31 29 3b 0a 20 20 20 20 72 63  64(s, 1);.    rc
37958 20 3d 20 6c 6f 61 64 53 65 67 6d 65 6e 74 28 76   = loadSegment(v
37959 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20  , pData, nData, 
3795a 69 4c 65 61 76 65 73 45 6e 64 2c 20 70 54 65 72  iLeavesEnd, pTer
3795b 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66  m, nTerm, isPref
3795c 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ix,.            
3795d 20 20 20 20 20 20 20 20 20 26 64 6f 63 6c 69 73           &doclis
3795e 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  t);.    if( rc!=
3795f 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
37960 20 65 72 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20   err;.  }.  if( 
37961 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
37962 29 7b 0a 20 20 20 20 69 66 28 20 64 6f 63 6c 69  ){.    if( docli
37963 73 74 2e 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20  st.nData!=0 ){. 
37964 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65       /* TODO(she
37965 73 73 29 20 54 68 65 20 6f 6c 64 20 74 65 72 6d  ss) The old term
37966 5f 73 65 6c 65 63 74 5f 61 6c 6c 28 29 20 63 6f  _select_all() co
37967 64 65 20 61 70 70 6c 69 65 64 20 74 68 65 20 63  de applied the c
37968 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 2a 2a 20 72  olumn.      ** r
37969 65 73 74 72 69 63 74 20 61 73 20 77 65 20 6d 65  estrict as we me
3796a 72 67 65 64 20 73 65 67 6d 65 6e 74 73 2c 20 6c  rged segments, l
3796b 65 61 64 69 6e 67 20 74 6f 20 73 6d 61 6c 6c 65  eading to smalle
3796c 72 20 62 75 66 66 65 72 73 2e 0a 20 20 20 20 20  r buffers..     
3796d 20 2a 2a 20 54 68 69 73 20 69 73 20 70 72 6f 62   ** This is prob
3796e 61 62 6c 79 20 77 6f 72 74 68 77 68 69 6c 65 20  ably worthwhile 
3796f 74 6f 20 62 72 69 6e 67 20 62 61 63 6b 2c 20 6f  to bring back, o
37970 6e 63 65 20 74 68 65 20 6e 65 77 20 73 74 6f 72  nce the new stor
37971 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 73 79 73  age.      ** sys
37972 74 65 6d 20 69 73 20 63 68 65 63 6b 65 64 20 69  tem is checked i
37973 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  n..      */.    
37974 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3d 3d 76    if( iColumn==v
37975 2d 3e 6e 43 6f 6c 75 6d 6e 29 20 69 43 6f 6c 75  ->nColumn) iColu
37976 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 64  mn = -1;.      d
37977 6f 63 4c 69 73 74 54 72 69 6d 28 44 4c 5f 44 45  ocListTrim(DL_DE
37978 46 41 55 4c 54 2c 20 64 6f 63 6c 69 73 74 2e 70  FAULT, doclist.p
37979 44 61 74 61 2c 20 64 6f 63 6c 69 73 74 2e 6e 44  Data, doclist.nD
3797a 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
3797b 20 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e 2c 20         iColumn, 
3797c 69 54 79 70 65 2c 20 6f 75 74 29 3b 0a 20 20 20  iType, out);.   
3797d 20 7d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49   }.    rc = SQLI
3797e 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 65 72 72  TE_OK;.  }.. err
3797f 3a 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  :.  dataBufferDe
37980 73 74 72 6f 79 28 26 64 6f 63 6c 69 73 74 29 3b  stroy(&doclist);
37981 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
37982 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
37983 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37984 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37985 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
37986 2a 2a 2f 0a 2f 2a 20 55 73 65 64 20 74 6f 20 68  **/./* Used to h
37987 6f 6c 64 20 68 61 73 68 74 61 62 6c 65 20 64 61  old hashtable da
37988 74 61 20 66 6f 72 20 73 6f 72 74 69 6e 67 2e 20  ta for sorting. 
37989 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
3798a 74 20 54 65 72 6d 44 61 74 61 20 7b 0a 20 20 63  t TermData {.  c
3798b 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d  onst char *pTerm
3798c 3b 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 0a 20  ;.  int nTerm;. 
3798d 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43   DLCollector *pC
3798e 6f 6c 6c 65 63 74 6f 72 3b 0a 7d 20 54 65 72 6d  ollector;.} Term
3798f 44 61 74 61 3b 0a 0a 2f 2a 20 4f 72 64 65 72 73  Data;../* Orders
37990 20 54 65 72 6d 44 61 74 61 20 65 6c 65 6d 65 6e   TermData elemen
37991 74 73 20 69 6e 20 73 74 72 63 6d 70 20 66 61 73  ts in strcmp fas
37992 68 69 6f 6e 20 28 20 3c 30 20 66 6f 72 20 6c 65  hion ( <0 for le
37993 73 73 2d 74 68 61 6e 2c 20 30 0a 2a 2a 20 66 6f  ss-than, 0.** fo
37994 72 20 65 71 75 61 6c 2c 20 3e 30 20 66 6f 72 20  r equal, >0 for 
37995 67 72 65 61 74 65 72 2d 74 68 61 6e 29 2e 0a 2a  greater-than)..*
37996 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74 65 72  /.static int ter
37997 6d 44 61 74 61 43 6d 70 28 63 6f 6e 73 74 20 76  mDataCmp(const v
37998 6f 69 64 20 2a 61 76 2c 20 63 6f 6e 73 74 20 76  oid *av, const v
37999 6f 69 64 20 2a 62 76 29 7b 0a 20 20 63 6f 6e 73  oid *bv){.  cons
3799a 74 20 54 65 72 6d 44 61 74 61 20 2a 61 20 3d 20  t TermData *a = 
3799b 28 63 6f 6e 73 74 20 54 65 72 6d 44 61 74 61 20  (const TermData 
3799c 2a 29 61 76 3b 0a 20 20 63 6f 6e 73 74 20 54 65  *)av;.  const Te
3799d 72 6d 44 61 74 61 20 2a 62 20 3d 20 28 63 6f 6e  rmData *b = (con
3799e 73 74 20 54 65 72 6d 44 61 74 61 20 2a 29 62 76  st TermData *)bv
3799f 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 61 2d 3e 6e  ;.  int n = a->n
379a0 54 65 72 6d 3c 62 2d 3e 6e 54 65 72 6d 20 3f 20  Term<b->nTerm ? 
379a1 61 2d 3e 6e 54 65 72 6d 20 3a 20 62 2d 3e 6e 54  a->nTerm : b->nT
379a2 65 72 6d 3b 0a 20 20 69 6e 74 20 63 20 3d 20 6d  erm;.  int c = m
379a3 65 6d 63 6d 70 28 61 2d 3e 70 54 65 72 6d 2c 20  emcmp(a->pTerm, 
379a4 62 2d 3e 70 54 65 72 6d 2c 20 6e 29 3b 0a 20 20  b->pTerm, n);.  
379a5 69 66 28 20 63 21 3d 30 20 29 20 72 65 74 75 72  if( c!=0 ) retur
379a6 6e 20 63 3b 0a 20 20 72 65 74 75 72 6e 20 61 2d  n c;.  return a-
379a7 3e 6e 54 65 72 6d 2d 62 2d 3e 6e 54 65 72 6d 3b  >nTerm-b->nTerm;
379a8 0a 7d 0a 0a 2f 2a 20 4f 72 64 65 72 20 70 54 65  .}../* Order pTe
379a9 72 6d 73 20 64 61 74 61 20 62 79 20 74 65 72 6d  rms data by term
379aa 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61 20 6e  , then write a n
379ab 65 77 20 6c 65 76 65 6c 20 30 20 73 65 67 6d 65  ew level 0 segme
379ac 6e 74 20 75 73 69 6e 67 0a 2a 2a 20 4c 65 61 66  nt using.** Leaf
379ad 57 72 69 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  Writer..*/.stati
379ae 63 20 69 6e 74 20 77 72 69 74 65 5a 65 72 6f 53  c int writeZeroS
379af 65 67 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f  egment(fulltext_
379b0 76 74 61 62 20 2a 76 2c 20 66 74 73 33 48 61 73  vtab *v, fts3Has
379b1 68 20 2a 70 54 65 72 6d 73 29 7b 0a 20 20 66 74  h *pTerms){.  ft
379b2 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 3b 0a 20  s3HashElem *e;. 
379b3 20 69 6e 74 20 69 64 78 2c 20 72 63 2c 20 69 2c   int idx, rc, i,
379b4 20 6e 3b 0a 20 20 54 65 72 6d 44 61 74 61 20 2a   n;.  TermData *
379b5 70 44 61 74 61 3b 0a 20 20 4c 65 61 66 57 72 69  pData;.  LeafWri
379b6 74 65 72 20 77 72 69 74 65 72 3b 0a 20 20 44 61  ter writer;.  Da
379b7 74 61 42 75 66 66 65 72 20 64 6c 3b 0a 0a 20 20  taBuffer dl;..  
379b8 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65  /* Determine the
379b9 20 6e 65 78 74 20 69 6e 64 65 78 20 61 74 20 6c   next index at l
379ba 65 76 65 6c 20 30 2c 20 6d 65 72 67 69 6e 67 20  evel 0, merging 
379bb 61 73 20 6e 65 63 65 73 73 61 72 79 2e 20 2a 2f  as necessary. */
379bc 0a 20 20 72 63 20 3d 20 73 65 67 64 69 72 4e 65  .  rc = segdirNe
379bd 78 74 49 6e 64 65 78 28 76 2c 20 30 2c 20 26 69  xtIndex(v, 0, &i
379be 64 78 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  dx);.  if( rc!=S
379bf 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
379c0 6e 20 72 63 3b 0a 0a 20 20 6e 20 3d 20 66 74 73  n rc;..  n = fts
379c1 33 48 61 73 68 43 6f 75 6e 74 28 70 54 65 72 6d  3HashCount(pTerm
379c2 73 29 3b 0a 20 20 70 44 61 74 61 20 3d 20 73 71  s);.  pData = sq
379c3 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 2a 73  lite3_malloc(n*s
379c4 69 7a 65 6f 66 28 54 65 72 6d 44 61 74 61 29 29  izeof(TermData))
379c5 3b 0a 0a 20 20 66 6f 72 28 69 20 3d 20 30 2c 20  ;..  for(i = 0, 
379c6 65 20 3d 20 66 74 73 33 48 61 73 68 46 69 72 73  e = fts3HashFirs
379c7 74 28 70 54 65 72 6d 73 29 3b 20 65 3b 20 69 2b  t(pTerms); e; i+
379c8 2b 2c 20 65 20 3d 20 66 74 73 33 48 61 73 68 4e  +, e = fts3HashN
379c9 65 78 74 28 65 29 29 7b 0a 20 20 20 20 61 73 73  ext(e)){.    ass
379ca 65 72 74 28 20 69 3c 6e 20 29 3b 0a 20 20 20 20  ert( i<n );.    
379cb 70 44 61 74 61 5b 69 5d 2e 70 54 65 72 6d 20 3d  pData[i].pTerm =
379cc 20 66 74 73 33 48 61 73 68 4b 65 79 28 65 29 3b   fts3HashKey(e);
379cd 0a 20 20 20 20 70 44 61 74 61 5b 69 5d 2e 6e 54  .    pData[i].nT
379ce 65 72 6d 20 3d 20 66 74 73 33 48 61 73 68 4b 65  erm = fts3HashKe
379cf 79 73 69 7a 65 28 65 29 3b 0a 20 20 20 20 70 44  ysize(e);.    pD
379d0 61 74 61 5b 69 5d 2e 70 43 6f 6c 6c 65 63 74 6f  ata[i].pCollecto
379d1 72 20 3d 20 66 74 73 33 48 61 73 68 44 61 74 61  r = fts3HashData
379d2 28 65 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  (e);.  }.  asser
379d3 74 28 20 69 3d 3d 6e 20 29 3b 0a 0a 20 20 2f 2a  t( i==n );..  /*
379d4 20 54 4f 44 4f 28 73 68 65 73 73 29 20 53 68 6f   TODO(shess) Sho
379d5 75 6c 64 20 77 65 20 61 6c 6c 6f 77 20 75 73 65  uld we allow use
379d6 72 2d 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74  r-defined collat
379d7 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 2c 0a 20  ion sequences,. 
379d8 20 2a 2a 20 68 65 72 65 3f 20 20 49 20 74 68 69   ** here?  I thi
379d9 6e 6b 20 77 65 20 6f 6e 6c 79 20 6e 65 65 64 20  nk we only need 
379da 74 68 61 74 20 6f 6e 63 65 20 77 65 20 73 75 70  that once we sup
379db 70 6f 72 74 20 70 72 65 66 69 78 20 73 65 61 72  port prefix sear
379dc 63 68 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ches..  */.  if(
379dd 20 6e 3e 31 20 29 20 71 73 6f 72 74 28 70 44 61   n>1 ) qsort(pDa
379de 74 61 2c 20 6e 2c 20 73 69 7a 65 6f 66 28 2a 70  ta, n, sizeof(*p
379df 44 61 74 61 29 2c 20 74 65 72 6d 44 61 74 61 43  Data), termDataC
379e0 6d 70 29 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28  mp);..  /* TODO(
379e1 73 68 65 73 73 29 20 52 65 66 61 63 74 6f 72 20  shess) Refactor 
379e2 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 77  so that we can w
379e3 72 69 74 65 20 64 69 72 65 63 74 6c 79 20 74 6f  rite directly to
379e4 20 74 68 65 20 73 65 67 6d 65 6e 74 0a 20 20 2a   the segment.  *
379e5 2a 20 44 61 74 61 42 75 66 66 65 72 2c 20 61 73  * DataBuffer, as
379e6 20 68 61 70 70 65 6e 73 20 66 6f 72 20 73 65 67   happens for seg
379e7 6d 65 6e 74 20 6d 65 72 67 65 73 2e 0a 20 20 2a  ment merges..  *
379e8 2f 0a 20 20 6c 65 61 66 57 72 69 74 65 72 49 6e  /.  leafWriterIn
379e9 69 74 28 30 2c 20 69 64 78 2c 20 26 77 72 69 74  it(0, idx, &writ
379ea 65 72 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  er);.  dataBuffe
379eb 72 49 6e 69 74 28 26 64 6c 2c 20 30 29 3b 0a 20  rInit(&dl, 0);. 
379ec 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69   for(i=0; i<n; i
379ed 2b 2b 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66  ++){.    dataBuf
379ee 66 65 72 52 65 73 65 74 28 26 64 6c 29 3b 0a 20  ferReset(&dl);. 
379ef 20 20 20 64 6c 63 41 64 64 44 6f 63 6c 69 73 74     dlcAddDoclist
379f0 28 70 44 61 74 61 5b 69 5d 2e 70 43 6f 6c 6c 65  (pData[i].pColle
379f1 63 74 6f 72 2c 20 26 64 6c 29 3b 0a 20 20 20 20  ctor, &dl);.    
379f2 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 53  rc = leafWriterS
379f3 74 65 70 28 76 2c 20 26 77 72 69 74 65 72 2c 0a  tep(v, &writer,.
379f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
379f5 20 20 20 20 20 20 20 20 70 44 61 74 61 5b 69 5d          pData[i]
379f6 2e 70 54 65 72 6d 2c 20 70 44 61 74 61 5b 69 5d  .pTerm, pData[i]
379f7 2e 6e 54 65 72 6d 2c 20 64 6c 2e 70 44 61 74 61  .nTerm, dl.pData
379f8 2c 20 64 6c 2e 6e 44 61 74 61 29 3b 0a 20 20 20  , dl.nData);.   
379f9 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
379fa 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 20  OK ) goto err;. 
379fb 20 7d 0a 20 20 72 63 20 3d 20 6c 65 61 66 57 72   }.  rc = leafWr
379fc 69 74 65 72 46 69 6e 61 6c 69 7a 65 28 76 2c 20  iterFinalize(v, 
379fd 26 77 72 69 74 65 72 29 3b 0a 0a 20 65 72 72 3a  &writer);.. err:
379fe 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73  .  dataBufferDes
379ff 74 72 6f 79 28 26 64 6c 29 3b 0a 20 20 73 71 6c  troy(&dl);.  sql
37a00 69 74 65 33 5f 66 72 65 65 28 70 44 61 74 61 29  ite3_free(pData)
37a01 3b 0a 20 20 6c 65 61 66 57 72 69 74 65 72 44 65  ;.  leafWriterDe
37a02 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a  stroy(&writer);.
37a03 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
37a04 2f 2a 20 49 66 20 70 65 6e 64 69 6e 67 54 65 72  /* If pendingTer
37a05 6d 73 20 68 61 73 20 64 61 74 61 2c 20 66 72 65  ms has data, fre
37a06 65 20 69 74 2e 20 2a 2f 0a 73 74 61 74 69 63 20  e it. */.static 
37a07 69 6e 74 20 63 6c 65 61 72 50 65 6e 64 69 6e 67  int clearPending
37a08 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76  Terms(fulltext_v
37a09 74 61 62 20 2a 76 29 7b 0a 20 20 69 66 28 20 76  tab *v){.  if( v
37a0a 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3e 3d  ->nPendingData>=
37a0b 30 20 29 7b 0a 20 20 20 20 66 74 73 33 48 61 73  0 ){.    fts3Has
37a0c 68 45 6c 65 6d 20 2a 65 3b 0a 20 20 20 20 66 6f  hElem *e;.    fo
37a0d 72 28 65 3d 66 74 73 33 48 61 73 68 46 69 72 73  r(e=fts3HashFirs
37a0e 74 28 26 76 2d 3e 70 65 6e 64 69 6e 67 54 65 72  t(&v->pendingTer
37a0f 6d 73 29 3b 20 65 3b 20 65 3d 66 74 73 33 48 61  ms); e; e=fts3Ha
37a10 73 68 4e 65 78 74 28 65 29 29 7b 0a 20 20 20 20  shNext(e)){.    
37a11 20 20 64 6c 63 44 65 6c 65 74 65 28 66 74 73 33    dlcDelete(fts3
37a12 48 61 73 68 44 61 74 61 28 65 29 29 3b 0a 20 20  HashData(e));.  
37a13 20 20 7d 0a 20 20 20 20 66 74 73 33 48 61 73 68    }.    fts3Hash
37a14 43 6c 65 61 72 28 26 76 2d 3e 70 65 6e 64 69 6e  Clear(&v->pendin
37a15 67 54 65 72 6d 73 29 3b 0a 20 20 20 20 76 2d 3e  gTerms);.    v->
37a16 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 3d 20 2d  nPendingData = -
37a17 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  1;.  }.  return 
37a18 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
37a19 20 49 66 20 70 65 6e 64 69 6e 67 54 65 72 6d 73   If pendingTerms
37a1a 20 68 61 73 20 64 61 74 61 2c 20 66 6c 75 73 68   has data, flush
37a1b 20 69 74 20 74 6f 20 61 20 6c 65 76 65 6c 2d 7a   it to a level-z
37a1c 65 72 6f 20 73 65 67 6d 65 6e 74 2c 20 61 6e 64  ero segment, and
37a1d 0a 2a 2a 20 66 72 65 65 20 69 74 2e 0a 2a 2f 0a  .** free it..*/.
37a1e 73 74 61 74 69 63 20 69 6e 74 20 66 6c 75 73 68  static int flush
37a1f 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 66 75 6c  PendingTerms(ful
37a20 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a  ltext_vtab *v){.
37a21 20 20 69 66 28 20 76 2d 3e 6e 50 65 6e 64 69 6e    if( v->nPendin
37a22 67 44 61 74 61 3e 3d 30 20 29 7b 0a 20 20 20 20  gData>=0 ){.    
37a23 69 6e 74 20 72 63 20 3d 20 77 72 69 74 65 5a 65  int rc = writeZe
37a24 72 6f 53 65 67 6d 65 6e 74 28 76 2c 20 26 76 2d  roSegment(v, &v-
37a25 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 29 3b 0a  >pendingTerms);.
37a26 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
37a27 54 45 5f 4f 4b 20 29 20 63 6c 65 61 72 50 65 6e  TE_OK ) clearPen
37a28 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a 20 20  dingTerms(v);.  
37a29 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
37a2a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
37a2b 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49 66 20 70 65  _OK;.}../* If pe
37a2c 6e 64 69 6e 67 54 65 72 6d 73 20 69 73 20 22 74  ndingTerms is "t
37a2d 6f 6f 20 62 69 67 22 2c 20 6f 72 20 64 6f 63 69  oo big", or doci
37a2e 64 20 69 73 20 6f 75 74 20 6f 66 20 6f 72 64 65  d is out of orde
37a2f 72 2c 20 66 6c 75 73 68 20 69 74 2e 0a 2a 2a 20  r, flush it..** 
37a30 52 65 67 61 72 64 6c 65 73 73 2c 20 62 65 20 63  Regardless, be c
37a31 65 72 74 61 69 6e 20 74 68 61 74 20 70 65 6e 64  ertain that pend
37a32 69 6e 67 54 65 72 6d 73 20 69 73 20 69 6e 69 74  ingTerms is init
37a33 69 61 6c 69 7a 65 64 20 66 6f 72 20 75 73 65 2e  ialized for use.
37a34 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
37a35 6e 69 74 50 65 6e 64 69 6e 67 54 65 72 6d 73 28  nitPendingTerms(
37a36 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
37a37 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  , sqlite_int64 i
37a38 44 6f 63 69 64 29 7b 0a 20 20 2f 2a 20 54 4f 44  Docid){.  /* TOD
37a39 4f 28 73 68 65 73 73 29 20 45 78 70 6c 6f 72 65  O(shess) Explore
37a3a 20 77 68 65 74 68 65 72 20 70 61 72 74 69 61 6c   whether partial
37a3b 6c 79 20 66 6c 75 73 68 69 6e 67 20 74 68 65 20  ly flushing the 
37a3c 62 75 66 66 65 72 20 6f 6e 0a 20 20 2a 2a 20 66  buffer on.  ** f
37a3d 6f 72 63 65 64 2d 66 6c 75 73 68 20 77 6f 75 6c  orced-flush woul
37a3e 64 20 70 72 6f 76 69 64 65 20 62 65 74 74 65 72  d provide better
37a3f 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20 20 49   performance.  I
37a40 20 73 75 73 70 65 63 74 20 74 68 61 74 20 69 66   suspect that if
37a41 0a 20 20 2a 2a 20 77 65 20 6f 72 64 65 72 65 64  .  ** we ordered
37a42 20 74 68 65 20 64 6f 63 6c 69 73 74 73 20 62 79   the doclists by
37a43 20 73 69 7a 65 20 61 6e 64 20 66 6c 75 73 68 65   size and flushe
37a44 64 20 74 68 65 20 6c 61 72 67 65 73 74 20 75 6e  d the largest un
37a45 74 69 6c 20 74 68 65 0a 20 20 2a 2a 20 62 75 66  til the.  ** buf
37a46 66 65 72 20 77 61 73 20 68 61 6c 66 20 65 6d 70  fer was half emp
37a47 74 79 2c 20 74 68 61 74 20 77 6f 75 6c 64 20 6c  ty, that would l
37a48 65 74 20 74 68 65 20 6c 65 73 73 20 66 72 65 71  et the less freq
37a49 75 65 6e 74 20 74 65 72 6d 73 0a 20 20 2a 2a 20  uent terms.  ** 
37a4a 67 65 6e 65 72 61 74 65 20 6c 6f 6e 67 65 72 20  generate longer 
37a4b 64 6f 63 6c 69 73 74 73 2e 0a 20 20 2a 2f 0a 20  doclists..  */. 
37a4c 20 69 66 28 20 69 44 6f 63 69 64 3c 3d 76 2d 3e   if( iDocid<=v->
37a4d 69 50 72 65 76 44 6f 63 69 64 20 7c 7c 20 76 2d  iPrevDocid || v-
37a4e 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3e 6b 50  >nPendingData>kP
37a4f 65 6e 64 69 6e 67 54 68 72 65 73 68 6f 6c 64 20  endingThreshold 
37a50 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20  ){.    int rc = 
37a51 66 6c 75 73 68 50 65 6e 64 69 6e 67 54 65 72 6d  flushPendingTerm
37a52 73 28 76 29 3b 0a 20 20 20 20 69 66 28 20 72 63  s(v);.    if( rc
37a53 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
37a54 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69  turn rc;.  }.  i
37a55 66 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61  f( v->nPendingDa
37a56 74 61 3c 30 20 29 7b 0a 20 20 20 20 66 74 73 33  ta<0 ){.    fts3
37a57 48 61 73 68 49 6e 69 74 28 26 76 2d 3e 70 65 6e  HashInit(&v->pen
37a58 64 69 6e 67 54 65 72 6d 73 2c 20 46 54 53 33 5f  dingTerms, FTS3_
37a59 48 41 53 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b  HASH_STRING, 1);
37a5a 0a 20 20 20 20 76 2d 3e 6e 50 65 6e 64 69 6e 67  .    v->nPending
37a5b 44 61 74 61 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Data = 0;.  }.  
37a5c 76 2d 3e 69 50 72 65 76 44 6f 63 69 64 20 3d 20  v->iPrevDocid = 
37a5d 69 44 6f 63 69 64 3b 0a 20 20 72 65 74 75 72 6e  iDocid;.  return
37a5e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
37a5f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
37a60 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 78  implements the x
37a61 55 70 64 61 74 65 20 63 61 6c 6c 62 61 63 6b 3b  Update callback;
37a62 20 69 74 20 69 73 20 74 68 65 20 74 6f 70 2d 6c   it is the top-l
37a63 65 76 65 6c 20 65 6e 74 72 79 0a 20 2a 20 70 6f  evel entry. * po
37a64 69 6e 74 20 66 6f 72 20 69 6e 73 65 72 74 69 6e  int for insertin
37a65 67 2c 20 64 65 6c 65 74 69 6e 67 20 6f 72 20 75  g, deleting or u
37a66 70 64 61 74 69 6e 67 20 61 20 72 6f 77 20 69 6e  pdating a row in
37a67 20 61 20 66 75 6c 6c 2d 74 65 78 74 20 74 61 62   a full-text tab
37a68 6c 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le. */.static in
37a69 74 20 66 75 6c 6c 74 65 78 74 55 70 64 61 74 65  t fulltextUpdate
37a6a 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
37a6b 56 74 61 62 2c 20 69 6e 74 20 6e 41 72 67 2c 20  Vtab, int nArg, 
37a6c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
37a6d 70 70 41 72 67 2c 0a 20 20 20 20 20 20 20 20 20  ppArg,.         
37a6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37a6f 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70   sqlite_int64 *p
37a70 52 6f 77 69 64 29 7b 0a 20 20 66 75 6c 6c 74 65  Rowid){.  fullte
37a71 78 74 5f 76 74 61 62 20 2a 76 20 3d 20 28 66 75  xt_vtab *v = (fu
37a72 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 20 70  lltext_vtab *) p
37a73 56 74 61 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  Vtab;.  int rc;.
37a74 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54  .  FTSTRACE(("FT
37a75 53 33 20 55 70 64 61 74 65 20 25 70 5c 6e 22 2c  S3 Update %p\n",
37a76 20 70 56 74 61 62 29 29 3b 0a 0a 20 20 69 66 28   pVtab));..  if(
37a77 20 6e 41 72 67 3c 32 20 29 7b 0a 20 20 20 20 72   nArg<2 ){.    r
37a78 63 20 3d 20 69 6e 64 65 78 5f 64 65 6c 65 74 65  c = index_delete
37a79 28 76 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  (v, sqlite3_valu
37a7a 65 5f 69 6e 74 36 34 28 70 70 41 72 67 5b 30 5d  e_int64(ppArg[0]
37a7b 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  ));.    if( rc==
37a7c 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
37a7d 20 20 20 2f 2a 20 49 66 20 77 65 20 6a 75 73 74     /* If we just
37a7e 20 64 65 6c 65 74 65 64 20 74 68 65 20 6c 61 73   deleted the las
37a7f 74 20 72 6f 77 20 69 6e 20 74 68 65 20 74 61 62  t row in the tab
37a80 6c 65 2c 20 63 6c 65 61 72 20 6f 75 74 20 74 68  le, clear out th
37a81 65 0a 20 20 20 20 20 20 2a 2a 20 69 6e 64 65 78  e.      ** index
37a82 20 64 61 74 61 2e 0a 20 20 20 20 20 20 2a 2f 0a   data..      */.
37a83 20 20 20 20 20 20 72 63 20 3d 20 63 6f 6e 74 65        rc = conte
37a84 6e 74 5f 65 78 69 73 74 73 28 76 29 3b 0a 20 20  nt_exists(v);.  
37a85 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
37a86 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20  TE_ROW ){.      
37a87 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
37a88 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
37a89 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
37a8a 45 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  E ){.        /* 
37a8b 43 6c 65 61 72 20 74 68 65 20 70 65 6e 64 69 6e  Clear the pendin
37a8c 67 20 74 65 72 6d 73 20 73 6f 20 77 65 20 64 6f  g terms so we do
37a8d 6e 27 74 20 66 6c 75 73 68 20 61 20 75 73 65 6c  n't flush a usel
37a8e 65 73 73 20 6c 65 76 65 6c 2d 30 0a 20 20 20 20  ess level-0.    
37a8f 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74 20 77      ** segment w
37a90 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74  hen the transact
37a91 69 6f 6e 20 63 6c 6f 73 65 73 2e 0a 20 20 20 20  ion closes..    
37a92 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72      */.        r
37a93 63 20 3d 20 63 6c 65 61 72 50 65 6e 64 69 6e 67  c = clearPending
37a94 54 65 72 6d 73 28 76 29 3b 0a 20 20 20 20 20 20  Terms(v);.      
37a95 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
37a96 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
37a97 20 72 63 20 3d 20 73 65 67 64 69 72 5f 64 65 6c   rc = segdir_del
37a98 65 74 65 5f 61 6c 6c 28 76 29 3b 0a 20 20 20 20  ete_all(v);.    
37a99 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
37a9a 20 20 7d 0a 20 20 7d 20 65 6c 73 65 20 69 66 28    }.  } else if(
37a9b 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
37a9c 79 70 65 28 70 70 41 72 67 5b 30 5d 29 20 21 3d  ype(ppArg[0]) !=
37a9d 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a   SQLITE_NULL ){.
37a9e 20 20 20 20 2f 2a 20 41 6e 20 75 70 64 61 74 65      /* An update
37a9f 3a 0a 20 20 20 20 20 2a 20 70 70 41 72 67 5b 30  :.     * ppArg[0
37aa0 5d 20 3d 20 6f 6c 64 20 72 6f 77 69 64 0a 20 20  ] = old rowid.  
37aa1 20 20 20 2a 20 70 70 41 72 67 5b 31 5d 20 3d 20     * ppArg[1] = 
37aa2 6e 65 77 20 72 6f 77 69 64 0a 20 20 20 20 20 2a  new rowid.     *
37aa3 20 70 70 41 72 67 5b 32 2e 2e 32 2b 76 2d 3e 6e   ppArg[2..2+v->n
37aa4 43 6f 6c 75 6d 6e 2d 31 5d 20 3d 20 76 61 6c 75  Column-1] = valu
37aa5 65 73 0a 20 20 20 20 20 2a 20 70 70 41 72 67 5b  es.     * ppArg[
37aa6 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20  2+v->nColumn] = 
37aa7 76 61 6c 75 65 20 66 6f 72 20 6d 61 67 69 63 20  value for magic 
37aa8 63 6f 6c 75 6d 6e 20 28 77 65 20 69 67 6e 6f 72  column (we ignor
37aa9 65 20 74 68 69 73 29 0a 20 20 20 20 20 2a 20 70  e this).     * p
37aaa 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d  pArg[2+v->nColum
37aab 6e 2b 31 5d 20 3d 20 76 61 6c 75 65 20 66 6f 72  n+1] = value for
37aac 20 64 6f 63 69 64 0a 20 20 20 20 20 2a 2f 0a 20   docid.     */. 
37aad 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
37aae 72 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 5f  rowid = sqlite3_
37aaf 76 61 6c 75 65 5f 69 6e 74 36 34 28 70 70 41 72  value_int64(ppAr
37ab0 67 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 73  g[0]);.    if( s
37ab1 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
37ab2 65 28 70 70 41 72 67 5b 31 5d 29 20 21 3d 20 53  e(ppArg[1]) != S
37ab3 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 7c  QLITE_INTEGER ||
37ab4 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
37ab5 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 70 70 41  _value_int64(ppA
37ab6 72 67 5b 31 5d 29 20 21 3d 20 72 6f 77 69 64 20  rg[1]) != rowid 
37ab7 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
37ab8 4c 49 54 45 5f 45 52 52 4f 52 3b 20 20 2f 2a 20  LITE_ERROR;  /* 
37ab9 77 65 20 64 6f 6e 27 74 20 61 6c 6c 6f 77 20 63  we don't allow c
37aba 68 61 6e 67 69 6e 67 20 74 68 65 20 72 6f 77 69  hanging the rowi
37abb 64 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 20 69  d */.    }else i
37abc 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f( sqlite3_value
37abd 5f 74 79 70 65 28 70 70 41 72 67 5b 32 2b 76 2d  _type(ppArg[2+v-
37abe 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 29 20 21 3d 20  >nColumn+1]) != 
37abf 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 7c  SQLITE_INTEGER |
37ac0 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  |.              
37ac1 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
37ac2 74 36 34 28 70 70 41 72 67 5b 32 2b 76 2d 3e 6e  t64(ppArg[2+v->n
37ac3 43 6f 6c 75 6d 6e 2b 31 5d 29 20 21 3d 20 72 6f  Column+1]) != ro
37ac4 77 69 64 20 29 7b 0a 20 20 20 20 20 20 72 63 20  wid ){.      rc 
37ac5 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 20  = SQLITE_ERROR; 
37ac6 20 2f 2a 20 77 65 20 64 6f 6e 27 74 20 61 6c 6c   /* we don't all
37ac7 6f 77 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20  ow changing the 
37ac8 64 6f 63 69 64 20 2a 2f 0a 20 20 20 20 7d 65 6c  docid */.    }el
37ac9 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
37aca 28 20 6e 41 72 67 3d 3d 32 2b 76 2d 3e 6e 43 6f  ( nArg==2+v->nCo
37acb 6c 75 6d 6e 2b 32 29 3b 0a 20 20 20 20 20 20 72  lumn+2);.      r
37acc 63 20 3d 20 69 6e 64 65 78 5f 75 70 64 61 74 65  c = index_update
37acd 28 76 2c 20 72 6f 77 69 64 2c 20 26 70 70 41 72  (v, rowid, &ppAr
37ace 67 5b 32 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  g[2]);.    }.  }
37acf 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 41   else {.    /* A
37ad0 6e 20 69 6e 73 65 72 74 3a 0a 20 20 20 20 20 2a  n insert:.     *
37ad1 20 70 70 41 72 67 5b 31 5d 20 3d 20 72 65 71 75   ppArg[1] = requ
37ad2 65 73 74 65 64 20 72 6f 77 69 64 0a 20 20 20 20  ested rowid.    
37ad3 20 2a 20 70 70 41 72 67 5b 32 2e 2e 32 2b 76 2d   * ppArg[2..2+v-
37ad4 3e 6e 43 6f 6c 75 6d 6e 2d 31 5d 20 3d 20 76 61  >nColumn-1] = va
37ad5 6c 75 65 73 0a 20 20 20 20 20 2a 20 70 70 41 72  lues.     * ppAr
37ad6 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20  g[2+v->nColumn] 
37ad7 3d 20 76 61 6c 75 65 20 66 6f 72 20 6d 61 67 69  = value for magi
37ad8 63 20 63 6f 6c 75 6d 6e 20 28 77 65 20 69 67 6e  c column (we ign
37ad9 6f 72 65 20 74 68 69 73 29 0a 20 20 20 20 20 2a  ore this).     *
37ada 20 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c   ppArg[2+v->nCol
37adb 75 6d 6e 2b 31 5d 20 3d 20 76 61 6c 75 65 20 66  umn+1] = value f
37adc 6f 72 20 64 6f 63 69 64 0a 20 20 20 20 20 2a 2f  or docid.     */
37add 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  .    sqlite3_val
37ade 75 65 20 2a 70 52 65 71 75 65 73 74 44 6f 63 69  ue *pRequestDoci
37adf 64 20 3d 20 70 70 41 72 67 5b 32 2b 76 2d 3e 6e  d = ppArg[2+v->n
37ae0 43 6f 6c 75 6d 6e 2b 31 5d 3b 0a 20 20 20 20 61  Column+1];.    a
37ae1 73 73 65 72 74 28 20 6e 41 72 67 3d 3d 32 2b 76  ssert( nArg==2+v
37ae2 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 29 3b 0a 20 20  ->nColumn+2);.  
37ae3 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c    if( SQLITE_NUL
37ae4 4c 20 21 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  L != sqlite3_val
37ae5 75 65 5f 74 79 70 65 28 70 52 65 71 75 65 73 74  ue_type(pRequest
37ae6 44 6f 63 69 64 29 20 26 26 0a 20 20 20 20 20 20  Docid) &&.      
37ae7 20 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 21 3d    SQLITE_NULL !=
37ae8 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
37ae9 79 70 65 28 70 70 41 72 67 5b 31 5d 29 20 29 7b  ype(ppArg[1]) ){
37aea 0a 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73  .      /* TODO(s
37aeb 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72 20 61  hess) Consider a
37aec 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 74 6f 20  llowing this to 
37aed 77 6f 72 6b 20 69 66 20 74 68 65 20 76 61 6c 75  work if the valu
37aee 65 73 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20  es are.      ** 
37aef 69 64 65 6e 74 69 63 61 6c 2e 20 20 49 27 6d 20  identical.  I'm 
37af0 69 6e 63 6c 69 6e 65 64 20 74 6f 20 64 69 73 63  inclined to disc
37af1 6f 75 72 61 67 65 20 74 68 61 74 20 75 73 61 67  ourage that usag
37af2 65 2c 20 74 68 6f 75 67 68 2c 0a 20 20 20 20 20  e, though,.     
37af3 20 2a 2a 20 67 69 76 65 6e 20 74 68 61 74 20 62   ** given that b
37af4 6f 74 68 20 72 6f 77 69 64 20 61 6e 64 20 64 6f  oth rowid and do
37af5 63 69 64 20 61 72 65 20 73 70 65 63 69 61 6c 20  cid are special 
37af6 63 6f 6c 75 6d 6e 73 2e 20 20 42 65 74 74 65 72  columns.  Better
37af7 0a 20 20 20 20 20 20 2a 2a 20 77 6f 75 6c 64 20  .      ** would 
37af8 62 65 20 74 6f 20 64 65 66 69 6e 65 20 6f 6e 65  be to define one
37af9 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20 61 73   or the other as
37afa 20 74 68 65 20 64 65 66 61 75 6c 74 20 77 69 6e   the default win
37afb 6e 65 72 2c 0a 20 20 20 20 20 20 2a 2a 20 62 75  ner,.      ** bu
37afc 74 20 73 68 6f 75 6c 64 20 69 74 20 62 65 20 66  t should it be f
37afd 74 73 33 2d 63 65 6e 74 72 69 63 20 28 64 6f 63  ts3-centric (doc
37afe 69 64 29 20 6f 72 20 53 51 4c 69 74 65 2d 63 65  id) or SQLite-ce
37aff 6e 74 72 69 63 0a 20 20 20 20 20 20 2a 2a 20 28  ntric.      ** (
37b00 72 6f 77 69 64 29 3f 0a 20 20 20 20 20 20 2a 2f  rowid)?.      */
37b01 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
37b02 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 65  TE_ERROR;.    }e
37b03 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 53  lse{.      if( S
37b04 51 4c 49 54 45 5f 4e 55 4c 4c 20 3d 3d 20 73 71  QLITE_NULL == sq
37b05 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
37b06 28 70 52 65 71 75 65 73 74 44 6f 63 69 64 29 20  (pRequestDocid) 
37b07 29 7b 0a 20 20 20 20 20 20 20 20 70 52 65 71 75  ){.        pRequ
37b08 65 73 74 44 6f 63 69 64 20 3d 20 70 70 41 72 67  estDocid = ppArg
37b09 5b 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  [1];.      }.   
37b0a 20 20 20 72 63 20 3d 20 69 6e 64 65 78 5f 69 6e     rc = index_in
37b0b 73 65 72 74 28 76 2c 20 70 52 65 71 75 65 73 74  sert(v, pRequest
37b0c 44 6f 63 69 64 2c 20 26 70 70 41 72 67 5b 32 5d  Docid, &ppArg[2]
37b0d 2c 20 70 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d  , pRowid);.    }
37b0e 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
37b0f 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  c;.}..static int
37b10 20 66 75 6c 6c 74 65 78 74 53 79 6e 63 28 73 71   fulltextSync(sq
37b11 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
37b12 62 29 7b 0a 20 20 46 54 53 54 52 41 43 45 28 28  b){.  FTSTRACE((
37b13 22 46 54 53 33 20 78 53 79 6e 63 28 29 5c 6e 22  "FTS3 xSync()\n"
37b14 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 66 6c 75  ));.  return flu
37b15 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 28  shPendingTerms((
37b16 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29  fulltext_vtab *)
37b17 70 56 74 61 62 29 3b 0a 7d 0a 0a 73 74 61 74 69  pVtab);.}..stati
37b18 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 42 65  c int fulltextBe
37b19 67 69 6e 28 73 71 6c 69 74 65 33 5f 76 74 61 62  gin(sqlite3_vtab
37b1a 20 2a 70 56 74 61 62 29 7b 0a 20 20 66 75 6c 6c   *pVtab){.  full
37b1b 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20 28  text_vtab *v = (
37b1c 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29  fulltext_vtab *)
37b1d 20 70 56 74 61 62 3b 0a 20 20 46 54 53 54 52 41   pVtab;.  FTSTRA
37b1e 43 45 28 28 22 46 54 53 33 20 78 42 65 67 69 6e  CE(("FTS3 xBegin
37b1f 28 29 5c 6e 22 29 29 3b 0a 0a 20 20 2f 2a 20 41  ()\n"));..  /* A
37b20 6e 79 20 62 75 66 66 65 72 65 64 20 75 70 64 61  ny buffered upda
37b21 74 65 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20  tes should have 
37b22 62 65 65 6e 20 63 6c 65 61 72 65 64 20 62 79 20  been cleared by 
37b23 74 68 65 20 70 72 65 76 69 6f 75 73 0a 20 20 2a  the previous.  *
37b24 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20  * transaction.. 
37b25 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 76 2d   */.  assert( v-
37b26 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c 30 20  >nPendingData<0 
37b27 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 6c 65 61  );.  return clea
37b28 72 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76 29  rPendingTerms(v)
37b29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
37b2a 66 75 6c 6c 74 65 78 74 43 6f 6d 6d 69 74 28 73  fulltextCommit(s
37b2b 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
37b2c 61 62 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f  ab){.  fulltext_
37b2d 76 74 61 62 20 2a 76 20 3d 20 28 66 75 6c 6c 74  vtab *v = (fullt
37b2e 65 78 74 5f 76 74 61 62 20 2a 29 20 70 56 74 61  ext_vtab *) pVta
37b2f 62 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  b;.  FTSTRACE(("
37b30 46 54 53 33 20 78 43 6f 6d 6d 69 74 28 29 5c 6e  FTS3 xCommit()\n
37b31 22 29 29 3b 0a 0a 20 20 2f 2a 20 42 75 66 66 65  "));..  /* Buffe
37b32 72 65 64 20 75 70 64 61 74 65 73 20 73 68 6f 75  red updates shou
37b33 6c 64 20 68 61 76 65 20 62 65 65 6e 20 63 6c 65  ld have been cle
37b34 61 72 65 64 20 62 79 20 66 75 6c 6c 74 65 78 74  ared by fulltext
37b35 53 79 6e 63 28 29 2e 20 2a 2f 0a 20 20 61 73 73  Sync(). */.  ass
37b36 65 72 74 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67  ert( v->nPending
37b37 44 61 74 61 3c 30 20 29 3b 0a 20 20 72 65 74 75  Data<0 );.  retu
37b38 72 6e 20 63 6c 65 61 72 50 65 6e 64 69 6e 67 54  rn clearPendingT
37b39 65 72 6d 73 28 76 29 3b 0a 7d 0a 0a 73 74 61 74  erms(v);.}..stat
37b3a 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 52  ic int fulltextR
37b3b 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 74 65 33 5f  ollback(sqlite3_
37b3c 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20  vtab *pVtab){.  
37b3d 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20  FTSTRACE(("FTS3 
37b3e 78 52 6f 6c 6c 62 61 63 6b 28 29 5c 6e 22 29 29  xRollback()\n"))
37b3f 3b 0a 20 20 72 65 74 75 72 6e 20 63 6c 65 61 72  ;.  return clear
37b40 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 28 66 75  PendingTerms((fu
37b41 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 70 56  lltext_vtab *)pV
37b42 74 61 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  tab);.}../*.** I
37b43 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
37b44 20 74 68 65 20 73 6e 69 70 70 65 74 28 29 20 66   the snippet() f
37b45 75 6e 63 74 69 6f 6e 20 66 6f 72 20 46 54 53 33  unction for FTS3
37b46 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
37b47 73 6e 69 70 70 65 74 46 75 6e 63 28 0a 20 20 73  snippetFunc(.  s
37b48 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
37b49 70 43 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  pContext,.  int 
37b4a 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
37b4b 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
37b4c 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f    fulltext_curso
37b4d 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 69 66  r *pCursor;.  if
37b4e 28 20 61 72 67 63 3c 31 20 29 20 72 65 74 75 72  ( argc<1 ) retur
37b4f 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  n;.  if( sqlite3
37b50 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
37b51 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f  [0])!=SQLITE_BLO
37b52 42 20 7c 7c 0a 20 20 20 20 20 20 73 71 6c 69 74  B ||.      sqlit
37b53 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
37b54 72 67 76 5b 30 5d 29 21 3d 73 69 7a 65 6f 66 28  rgv[0])!=sizeof(
37b55 70 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20  pCursor) ){.    
37b56 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
37b57 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22  rror(pContext, "
37b58 69 6c 6c 65 67 61 6c 20 66 69 72 73 74 20 61 72  illegal first ar
37b59 67 75 6d 65 6e 74 20 74 6f 20 68 74 6d 6c 5f 73  gument to html_s
37b5a 6e 69 70 70 65 74 22 2c 2d 31 29 3b 0a 20 20 7d  nippet",-1);.  }
37b5b 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20  else{.    const 
37b5c 63 68 61 72 20 2a 7a 53 74 61 72 74 20 3d 20 22  char *zStart = "
37b5d 3c 62 3e 22 3b 0a 20 20 20 20 63 6f 6e 73 74 20  <b>";.    const 
37b5e 63 68 61 72 20 2a 7a 45 6e 64 20 3d 20 22 3c 2f  char *zEnd = "</
37b5f 62 3e 22 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  b>";.    const c
37b60 68 61 72 20 2a 7a 45 6c 6c 69 70 73 69 73 20 3d  har *zEllipsis =
37b61 20 22 3c 62 3e 2e 2e 2e 3c 2f 62 3e 22 3b 0a 20   "<b>...</b>";. 
37b62 20 20 20 6d 65 6d 63 70 79 28 26 70 43 75 72 73     memcpy(&pCurs
37b63 6f 72 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  or, sqlite3_valu
37b64 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 2c  e_blob(argv[0]),
37b65 20 73 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29   sizeof(pCursor)
37b66 29 3b 0a 20 20 20 20 69 66 28 20 61 72 67 63 3e  );.    if( argc>
37b67 3d 32 20 29 7b 0a 20 20 20 20 20 20 7a 53 74 61  =2 ){.      zSta
37b68 72 74 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  rt = (const char
37b69 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
37b6a 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20  text(argv[1]);. 
37b6b 20 20 20 20 20 69 66 28 20 61 72 67 63 3e 3d 33       if( argc>=3
37b6c 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 45 6e 64   ){.        zEnd
37b6d 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29   = (const char*)
37b6e 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
37b6f 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20  xt(argv[2]);.   
37b70 20 20 20 20 20 69 66 28 20 61 72 67 63 3e 3d 34       if( argc>=4
37b71 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 45   ){.          zE
37b72 6c 6c 69 70 73 69 73 20 3d 20 28 63 6f 6e 73 74  llipsis = (const
37b73 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76   char*)sqlite3_v
37b74 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 33  alue_text(argv[3
37b75 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ]);.        }.  
37b76 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
37b77 73 6e 69 70 70 65 74 41 6c 6c 4f 66 66 73 65 74  snippetAllOffset
37b78 73 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  s(pCursor);.    
37b79 73 6e 69 70 70 65 74 54 65 78 74 28 70 43 75 72  snippetText(pCur
37b7a 73 6f 72 2c 20 7a 53 74 61 72 74 2c 20 7a 45 6e  sor, zStart, zEn
37b7b 64 2c 20 7a 45 6c 6c 69 70 73 69 73 29 3b 0a 20  d, zEllipsis);. 
37b7c 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
37b7d 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78 74 2c  t_text(pContext,
37b7e 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65   pCursor->snippe
37b7f 74 2e 7a 53 6e 69 70 70 65 74 2c 0a 20 20 20 20  t.zSnippet,.    
37b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37b81 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69      pCursor->sni
37b82 70 70 65 74 2e 6e 53 6e 69 70 70 65 74 2c 20 53  ppet.nSnippet, S
37b83 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
37b84 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c   }.}../*.** Impl
37b85 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
37b86 65 20 6f 66 66 73 65 74 73 28 29 20 66 75 6e 63  e offsets() func
37b87 74 69 6f 6e 20 66 6f 72 20 46 54 53 33 0a 2a 2f  tion for FTS3.*/
37b88 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6e 69  .static void sni
37b89 70 70 65 74 4f 66 66 73 65 74 73 46 75 6e 63 28  ppetOffsetsFunc(
37b8a 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
37b8b 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20  xt *pContext,.  
37b8c 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
37b8d 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
37b8e 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63  .){.  fulltext_c
37b8f 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a  ursor *pCursor;.
37b90 20 20 69 66 28 20 61 72 67 63 3c 31 20 29 20 72    if( argc<1 ) r
37b91 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c  eturn;.  if( sql
37b92 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
37b93 61 72 67 76 5b 30 5d 29 21 3d 53 51 4c 49 54 45  argv[0])!=SQLITE
37b94 5f 42 4c 4f 42 20 7c 7c 0a 20 20 20 20 20 20 73  _BLOB ||.      s
37b95 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
37b96 65 73 28 61 72 67 76 5b 30 5d 29 21 3d 73 69 7a  es(argv[0])!=siz
37b97 65 6f 66 28 70 43 75 72 73 6f 72 29 20 29 7b 0a  eof(pCursor) ){.
37b98 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
37b99 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65 78  lt_error(pContex
37b9a 74 2c 20 22 69 6c 6c 65 67 61 6c 20 66 69 72 73  t, "illegal firs
37b9b 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 6f 66  t argument to of
37b9c 66 73 65 74 73 22 2c 2d 31 29 3b 0a 20 20 7d 65  fsets",-1);.  }e
37b9d 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28  lse{.    memcpy(
37b9e 26 70 43 75 72 73 6f 72 2c 20 73 71 6c 69 74 65  &pCursor, sqlite
37b9f 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67  3_value_blob(arg
37ba0 76 5b 30 5d 29 2c 20 73 69 7a 65 6f 66 28 70 43  v[0]), sizeof(pC
37ba1 75 72 73 6f 72 29 29 3b 0a 20 20 20 20 73 6e 69  ursor));.    sni
37ba2 70 70 65 74 41 6c 6c 4f 66 66 73 65 74 73 28 70  ppetAllOffsets(p
37ba3 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 73 6e 69  Cursor);.    sni
37ba4 70 70 65 74 4f 66 66 73 65 74 54 65 78 74 28 26  ppetOffsetText(&
37ba5 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74  pCursor->snippet
37ba6 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
37ba7 65 73 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74  esult_text(pCont
37ba8 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ext,.           
37ba9 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 75               pCu
37baa 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 7a 4f  rsor->snippet.zO
37bab 66 66 73 65 74 2c 20 70 43 75 72 73 6f 72 2d 3e  ffset, pCursor->
37bac 73 6e 69 70 70 65 74 2e 6e 4f 66 66 73 65 74 2c  snippet.nOffset,
37bad 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
37bae 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
37baf 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a  STATIC);.  }.}..
37bb0 2f 2a 20 4f 70 74 4c 65 61 76 65 73 52 65 61 64  /* OptLeavesRead
37bb1 65 72 20 69 73 20 6e 65 61 72 6c 79 20 69 64 65  er is nearly ide
37bb2 6e 74 69 63 61 6c 20 74 6f 20 4c 65 61 76 65 73  ntical to Leaves
37bb3 52 65 61 64 65 72 2c 20 65 78 63 65 70 74 20 74  Reader, except t
37bb4 68 61 74 0a 2a 2a 20 77 68 65 72 65 20 4c 65 61  hat.** where Lea
37bb5 76 65 73 52 65 61 64 65 72 20 69 73 20 67 65 61  vesReader is gea
37bb6 72 65 64 20 74 6f 77 61 72 64 73 20 74 68 65 20  red towards the 
37bb7 6d 65 72 67 69 6e 67 20 6f 66 20 63 6f 6d 70 6c  merging of compl
37bb8 65 74 65 0a 2a 2a 20 73 65 67 6d 65 6e 74 20 6c  ete.** segment l
37bb9 65 76 65 6c 73 20 28 77 69 74 68 20 65 78 61 63  evels (with exac
37bba 74 6c 79 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20  tly MERGE_COUNT 
37bbb 73 65 67 6d 65 6e 74 73 29 2c 20 4f 70 74 4c 65  segments), OptLe
37bbc 61 76 65 73 52 65 61 64 65 72 0a 2a 2a 20 69 73  avesReader.** is
37bbd 20 67 65 61 72 65 64 20 74 6f 77 61 72 64 73 20   geared towards 
37bbe 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
37bbf 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 28 29  f the optimize()
37bc0 20 66 75 6e 63 74 69 6f 6e 2c 20 61 6e 64 0a 2a   function, and.*
37bc1 2a 20 63 61 6e 20 6d 65 72 67 65 20 61 6c 6c 20  * can merge all 
37bc2 73 65 67 6d 65 6e 74 73 20 73 69 6d 75 6c 74 61  segments simulta
37bc3 6e 65 6f 75 73 6c 79 2e 20 20 54 68 69 73 20 76  neously.  This v
37bc4 65 72 73 69 6f 6e 20 6d 61 79 20 62 65 0a 2a 2a  ersion may be.**
37bc5 20 73 6f 6d 65 77 68 61 74 20 6c 65 73 73 20 65   somewhat less e
37bc6 66 66 69 63 69 65 6e 74 20 74 68 61 6e 20 4c 65  fficient than Le
37bc7 61 76 65 73 52 65 61 64 65 72 20 62 65 63 61 75  avesReader becau
37bc8 73 65 20 69 74 20 6d 65 72 67 65 73 20 69 6e 74  se it merges int
37bc9 6f 20 61 6e 0a 2a 2a 20 61 63 63 75 6d 75 6c 61  o an.** accumula
37bca 74 6f 72 20 72 61 74 68 65 72 20 74 68 61 6e 20  tor rather than 
37bcb 64 6f 69 6e 67 20 61 6e 20 4e 2d 77 61 79 20 6d  doing an N-way m
37bcc 65 72 67 65 2c 20 62 75 74 20 73 69 6e 63 65 20  erge, but since 
37bcd 73 65 67 6d 65 6e 74 0a 2a 2a 20 73 69 7a 65 20  segment.** size 
37bce 67 72 6f 77 73 20 65 78 70 6f 6e 65 6e 74 69 61  grows exponentia
37bcf 6c 6c 79 20 28 73 6f 20 73 65 67 6d 65 6e 74 20  lly (so segment 
37bd0 63 6f 75 6e 74 20 6c 6f 67 72 69 74 68 6d 69 63  count logrithmic
37bd1 61 6c 6c 79 29 20 74 68 69 73 20 69 73 0a 2a 2a  ally) this is.**
37bd2 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 20 61 6e   probably not an
37bd3 20 69 6d 6d 65 64 69 61 74 65 20 70 72 6f 62 6c   immediate probl
37bd4 65 6d 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73  em..*/./* TODO(s
37bd5 68 65 73 73 29 3a 20 50 72 6f 76 65 20 74 68 61  hess): Prove tha
37bd6 74 20 61 73 73 65 72 74 69 6f 6e 2c 20 6f 72 20  t assertion, or 
37bd7 65 78 74 65 6e 64 20 74 68 65 20 6d 65 72 67 65  extend the merge
37bd8 20 63 6f 64 65 20 74 6f 0a 2a 2a 20 6d 65 72 67   code to.** merg
37bd9 65 20 74 72 65 65 20 66 61 73 68 69 6f 6e 20 28  e tree fashion (
37bda 6c 69 6b 65 20 74 68 65 20 70 72 65 66 69 78 2d  like the prefix-
37bdb 73 65 61 72 63 68 69 6e 67 20 63 6f 64 65 20 64  searching code d
37bdc 6f 65 73 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f  oes)..*/./* TODO
37bdd 28 73 68 65 73 73 29 3a 20 4f 70 74 4c 65 61 76  (shess): OptLeav
37bde 65 73 52 65 61 64 65 72 20 61 6e 64 20 4c 65 61  esReader and Lea
37bdf 76 65 73 52 65 61 64 65 72 20 63 6f 75 6c 64 20  vesReader could 
37be0 70 72 6f 62 61 62 6c 79 20 62 65 0a 2a 2a 20 6d  probably be.** m
37be1 65 72 67 65 64 20 77 69 74 68 20 6c 69 74 74 6c  erged with littl
37be2 65 20 6f 72 20 6e 6f 20 6c 6f 73 73 20 6f 66 20  e or no loss of 
37be3 70 65 72 66 6f 72 6d 61 6e 63 65 20 66 6f 72 20  performance for 
37be4 4c 65 61 76 65 73 52 65 61 64 65 72 2e 20 20 54  LeavesReader.  T
37be5 68 65 0a 2a 2a 20 6d 65 72 67 65 64 20 63 6f 64  he.** merged cod
37be6 65 20 77 6f 75 6c 64 20 6e 65 65 64 20 74 6f 20  e would need to 
37be7 68 61 6e 64 6c 65 20 3e 4d 45 52 47 45 5f 43 4f  handle >MERGE_CO
37be8 55 4e 54 20 73 65 67 6d 65 6e 74 73 2c 20 61 6e  UNT segments, an
37be9 64 20 77 6f 75 6c 64 0a 2a 2a 20 61 6c 73 6f 20  d would.** also 
37bea 6e 65 65 64 20 74 6f 20 62 65 20 61 62 6c 65 20  need to be able 
37beb 74 6f 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 6f 70  to optionally op
37bec 74 69 6d 69 7a 65 20 61 77 61 79 20 64 65 6c 65  timize away dele
37bed 74 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  tes..*/.typedef 
37bee 73 74 72 75 63 74 20 4f 70 74 4c 65 61 76 65 73  struct OptLeaves
37bef 52 65 61 64 65 72 20 7b 0a 20 20 2f 2a 20 53 65  Reader {.  /* Se
37bf0 67 6d 65 6e 74 20 6e 75 6d 62 65 72 2c 20 74 6f  gment number, to
37bf1 20 6f 72 64 65 72 20 72 65 61 64 65 72 73 20 62   order readers b
37bf2 79 20 61 67 65 2e 20 2a 2f 0a 20 20 69 6e 74 20  y age. */.  int 
37bf3 73 65 67 6d 65 6e 74 3b 0a 20 20 4c 65 61 76 65  segment;.  Leave
37bf4 73 52 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a  sReader reader;.
37bf5 7d 20 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65  } OptLeavesReade
37bf6 72 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  r;..static int o
37bf7 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 41 74  ptLeavesReaderAt
37bf8 45 6e 64 28 4f 70 74 4c 65 61 76 65 73 52 65 61  End(OptLeavesRea
37bf9 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
37bfa 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73 52 65   return leavesRe
37bfb 61 64 65 72 41 74 45 6e 64 28 26 70 52 65 61 64  aderAtEnd(&pRead
37bfc 65 72 2d 3e 72 65 61 64 65 72 29 3b 0a 7d 0a 73  er->reader);.}.s
37bfd 74 61 74 69 63 20 69 6e 74 20 6f 70 74 4c 65 61  tatic int optLea
37bfe 76 65 73 52 65 61 64 65 72 54 65 72 6d 42 79 74  vesReaderTermByt
37bff 65 73 28 4f 70 74 4c 65 61 76 65 73 52 65 61 64  es(OptLeavesRead
37c00 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
37c01 72 65 74 75 72 6e 20 6c 65 61 76 65 73 52 65 61  return leavesRea
37c02 64 65 72 54 65 72 6d 42 79 74 65 73 28 26 70 52  derTermBytes(&pR
37c03 65 61 64 65 72 2d 3e 72 65 61 64 65 72 29 3b 0a  eader->reader);.
37c04 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  }.static const c
37c05 68 61 72 20 2a 6f 70 74 4c 65 61 76 65 73 52 65  har *optLeavesRe
37c06 61 64 65 72 44 61 74 61 28 4f 70 74 4c 65 61 76  aderData(OptLeav
37c07 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  esReader *pReade
37c08 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61  r){.  return lea
37c09 76 65 73 52 65 61 64 65 72 44 61 74 61 28 26 70  vesReaderData(&p
37c0a 52 65 61 64 65 72 2d 3e 72 65 61 64 65 72 29 3b  Reader->reader);
37c0b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70  .}.static int op
37c0c 74 4c 65 61 76 65 73 52 65 61 64 65 72 44 61 74  tLeavesReaderDat
37c0d 61 42 79 74 65 73 28 4f 70 74 4c 65 61 76 65 73  aBytes(OptLeaves
37c0e 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
37c0f 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 76 65  {.  return leave
37c10 73 52 65 61 64 65 72 44 61 74 61 42 79 74 65 73  sReaderDataBytes
37c11 28 26 70 52 65 61 64 65 72 2d 3e 72 65 61 64 65  (&pReader->reade
37c12 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e  r);.}.static con
37c13 73 74 20 63 68 61 72 20 2a 6f 70 74 4c 65 61 76  st char *optLeav
37c14 65 73 52 65 61 64 65 72 54 65 72 6d 28 4f 70 74  esReaderTerm(Opt
37c15 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52  LeavesReader *pR
37c16 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  eader){.  return
37c17 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72   leavesReaderTer
37c18 6d 28 26 70 52 65 61 64 65 72 2d 3e 72 65 61 64  m(&pReader->read
37c19 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  er);.}.static in
37c1a 74 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65  t optLeavesReade
37c1b 72 53 74 65 70 28 66 75 6c 6c 74 65 78 74 5f 76  rStep(fulltext_v
37c1c 74 61 62 20 2a 76 2c 20 4f 70 74 4c 65 61 76 65  tab *v, OptLeave
37c1d 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  sReader *pReader
37c1e 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 76  ){.  return leav
37c1f 65 73 52 65 61 64 65 72 53 74 65 70 28 76 2c 20  esReaderStep(v, 
37c20 26 70 52 65 61 64 65 72 2d 3e 72 65 61 64 65 72  &pReader->reader
37c21 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  );.}.static int 
37c22 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 54  optLeavesReaderT
37c23 65 72 6d 43 6d 70 28 4f 70 74 4c 65 61 76 65 73  ermCmp(OptLeaves
37c24 52 65 61 64 65 72 20 2a 6c 72 31 2c 20 4f 70 74  Reader *lr1, Opt
37c25 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72  LeavesReader *lr
37c26 32 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61  2){.  return lea
37c27 76 65 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70  vesReaderTermCmp
37c28 28 26 6c 72 31 2d 3e 72 65 61 64 65 72 2c 20 26  (&lr1->reader, &
37c29 6c 72 32 2d 3e 72 65 61 64 65 72 29 3b 0a 7d 0a  lr2->reader);.}.
37c2a 2f 2a 20 4f 72 64 65 72 20 62 79 20 74 65 72 6d  /* Order by term
37c2b 20 61 73 63 65 6e 64 69 6e 67 2c 20 73 65 67 6d   ascending, segm
37c2c 65 6e 74 20 61 73 63 65 6e 64 69 6e 67 20 28 6f  ent ascending (o
37c2d 6c 64 65 73 74 20 74 6f 20 6e 65 77 65 73 74 29  ldest to newest)
37c2e 2c 20 77 69 74 68 0a 2a 2a 20 65 78 68 61 75 73  , with.** exhaus
37c2f 74 65 64 20 72 65 61 64 65 72 73 20 74 6f 20 74  ted readers to t
37c30 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69  he end..*/.stati
37c31 63 20 69 6e 74 20 6f 70 74 4c 65 61 76 65 73 52  c int optLeavesR
37c32 65 61 64 65 72 43 6d 70 28 4f 70 74 4c 65 61 76  eaderCmp(OptLeav
37c33 65 73 52 65 61 64 65 72 20 2a 6c 72 31 2c 20 4f  esReader *lr1, O
37c34 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  ptLeavesReader *
37c35 6c 72 32 29 7b 0a 20 20 69 6e 74 20 63 20 3d 20  lr2){.  int c = 
37c36 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 54  optLeavesReaderT
37c37 65 72 6d 43 6d 70 28 6c 72 31 2c 20 6c 72 32 29  ermCmp(lr1, lr2)
37c38 3b 0a 20 20 69 66 28 20 63 21 3d 30 20 29 20 72  ;.  if( c!=0 ) r
37c39 65 74 75 72 6e 20 63 3b 0a 20 20 72 65 74 75 72  eturn c;.  retur
37c3a 6e 20 6c 72 31 2d 3e 73 65 67 6d 65 6e 74 2d 6c  n lr1->segment-l
37c3b 72 32 2d 3e 73 65 67 6d 65 6e 74 3b 0a 7d 0a 2f  r2->segment;.}./
37c3c 2a 20 42 75 62 62 6c 65 20 70 4c 72 5b 30 5d 20  * Bubble pLr[0] 
37c3d 74 6f 20 61 70 70 72 6f 70 72 69 61 74 65 20 70  to appropriate p
37c3e 6c 61 63 65 20 69 6e 20 70 4c 72 5b 31 2e 2e 6e  lace in pLr[1..n
37c3f 4c 72 2d 31 5d 2e 20 20 41 73 73 75 6d 65 73 20  Lr-1].  Assumes 
37c40 74 68 61 74 0a 2a 2a 20 70 4c 72 5b 31 2e 2e 6e  that.** pLr[1..n
37c41 4c 72 2d 31 5d 20 69 73 20 61 6c 72 65 61 64 79  Lr-1] is already
37c42 20 73 6f 72 74 65 64 2e 0a 2a 2f 0a 73 74 61 74   sorted..*/.stat
37c43 69 63 20 76 6f 69 64 20 6f 70 74 4c 65 61 76 65  ic void optLeave
37c44 73 52 65 61 64 65 72 52 65 6f 72 64 65 72 28 4f  sReaderReorder(O
37c45 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  ptLeavesReader *
37c46 70 4c 72 2c 20 69 6e 74 20 6e 4c 72 29 7b 0a 20  pLr, int nLr){. 
37c47 20 77 68 69 6c 65 28 20 6e 4c 72 3e 31 20 26 26   while( nLr>1 &&
37c48 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
37c49 43 6d 70 28 70 4c 72 2c 20 70 4c 72 2b 31 29 3e  Cmp(pLr, pLr+1)>
37c4a 30 20 29 7b 0a 20 20 20 20 4f 70 74 4c 65 61 76  0 ){.    OptLeav
37c4b 65 73 52 65 61 64 65 72 20 74 6d 70 20 3d 20 70  esReader tmp = p
37c4c 4c 72 5b 30 5d 3b 0a 20 20 20 20 70 4c 72 5b 30  Lr[0];.    pLr[0
37c4d 5d 20 3d 20 70 4c 72 5b 31 5d 3b 0a 20 20 20 20  ] = pLr[1];.    
37c4e 70 4c 72 5b 31 5d 20 3d 20 74 6d 70 3b 0a 20 20  pLr[1] = tmp;.  
37c4f 20 20 6e 4c 72 2d 2d 3b 0a 20 20 20 20 70 4c 72    nLr--;.    pLr
37c50 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 6f 70  ++;.  }.}../* op
37c51 74 69 6d 69 7a 65 28 29 20 68 65 6c 70 65 72 20  timize() helper 
37c52 66 75 6e 63 74 69 6f 6e 2e 20 20 50 75 74 20 74  function.  Put t
37c53 68 65 20 72 65 61 64 65 72 73 20 69 6e 20 6f 72  he readers in or
37c54 64 65 72 20 61 6e 64 20 69 74 65 72 61 74 65 0a  der and iterate.
37c55 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 6d 2c  ** through them,
37c56 20 6d 65 72 67 69 6e 67 20 64 6f 63 6c 69 73 74   merging doclist
37c57 73 20 66 6f 72 20 6d 61 74 63 68 69 6e 67 20 74  s for matching t
37c58 65 72 6d 73 20 69 6e 74 6f 20 70 57 72 69 74 65  erms into pWrite
37c59 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 73 20 53 51  r..** Returns SQ
37c5a 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
37c5b 73 73 2c 20 6f 72 20 74 68 65 20 53 51 4c 69 74  ss, or the SQLit
37c5c 65 20 65 72 72 6f 72 20 63 6f 64 65 20 77 68 69  e error code whi
37c5d 63 68 0a 2a 2a 20 70 72 65 76 65 6e 74 65 64 20  ch.** prevented 
37c5e 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 74  success..*/.stat
37c5f 69 63 20 69 6e 74 20 6f 70 74 69 6d 69 7a 65 49  ic int optimizeI
37c60 6e 74 65 72 6e 61 6c 28 66 75 6c 6c 74 65 78 74  nternal(fulltext
37c61 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20 20 20 20  _vtab *v,.      
37c62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37c63 20 20 20 20 20 20 4f 70 74 4c 65 61 76 65 73 52        OptLeavesR
37c64 65 61 64 65 72 20 2a 72 65 61 64 65 72 73 2c 20  eader *readers, 
37c65 69 6e 74 20 6e 52 65 61 64 65 72 73 2c 0a 20 20  int nReaders,.  
37c66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37c67 20 20 20 20 20 20 20 20 20 20 4c 65 61 66 57 72            LeafWr
37c68 69 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a  iter *pWriter){.
37c69 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20 53 51    int i, rc = SQ
37c6a 4c 49 54 45 5f 4f 4b 3b 0a 20 20 44 61 74 61 42  LITE_OK;.  DataB
37c6b 75 66 66 65 72 20 64 6f 63 6c 69 73 74 2c 20 6d  uffer doclist, m
37c6c 65 72 67 65 64 2c 20 74 6d 70 3b 0a 0a 20 20 2f  erged, tmp;..  /
37c6d 2a 20 4f 72 64 65 72 20 74 68 65 20 72 65 61 64  * Order the read
37c6e 65 72 73 2e 20 2a 2f 0a 20 20 69 20 3d 20 6e 52  ers. */.  i = nR
37c6f 65 61 64 65 72 73 3b 0a 20 20 77 68 69 6c 65 28  eaders;.  while(
37c70 20 69 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20   i-- > 0 ){.    
37c71 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 52  optLeavesReaderR
37c72 65 6f 72 64 65 72 28 26 72 65 61 64 65 72 73 5b  eorder(&readers[
37c73 69 5d 2c 20 6e 52 65 61 64 65 72 73 2d 69 29 3b  i], nReaders-i);
37c74 0a 20 20 7d 0a 0a 20 20 64 61 74 61 42 75 66 66  .  }..  dataBuff
37c75 65 72 49 6e 69 74 28 26 64 6f 63 6c 69 73 74 2c  erInit(&doclist,
37c76 20 4c 45 41 46 5f 4d 41 58 29 3b 0a 20 20 64 61   LEAF_MAX);.  da
37c77 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 6d 65  taBufferInit(&me
37c78 72 67 65 64 2c 20 4c 45 41 46 5f 4d 41 58 29 3b  rged, LEAF_MAX);
37c79 0a 0a 20 20 2f 2a 20 45 78 68 61 75 73 74 65 64  ..  /* Exhausted
37c7a 20 72 65 61 64 65 72 73 20 62 75 62 62 6c 65 20   readers bubble 
37c7b 74 6f 20 74 68 65 20 65 6e 64 2c 20 73 6f 20 77  to the end, so w
37c7c 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 72 65  hen the first re
37c7d 61 64 65 72 20 69 73 0a 20 20 2a 2a 20 61 74 20  ader is.  ** at 
37c7e 65 6f 66 2c 20 61 6c 6c 20 61 72 65 20 61 74 20  eof, all are at 
37c7f 65 6f 66 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c  eof..  */.  whil
37c80 65 28 20 21 6f 70 74 4c 65 61 76 65 73 52 65 61  e( !optLeavesRea
37c81 64 65 72 41 74 45 6e 64 28 26 72 65 61 64 65 72  derAtEnd(&reader
37c82 73 5b 30 5d 29 20 29 7b 0a 0a 20 20 20 20 2f 2a  s[0]) ){..    /*
37c83 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20   Figure out how 
37c84 6d 61 6e 79 20 72 65 61 64 65 72 73 20 73 68 61  many readers sha
37c85 72 65 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d  re the next term
37c86 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 31  . */.    for(i=1
37c87 3b 20 69 3c 6e 52 65 61 64 65 72 73 20 26 26 20  ; i<nReaders && 
37c88 21 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72  !optLeavesReader
37c89 41 74 45 6e 64 28 26 72 65 61 64 65 72 73 5b 69  AtEnd(&readers[i
37c8a 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ]); i++){.      
37c8b 69 66 28 20 30 21 3d 6f 70 74 4c 65 61 76 65 73  if( 0!=optLeaves
37c8c 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 26 72  ReaderTermCmp(&r
37c8d 65 61 64 65 72 73 5b 30 5d 2c 20 26 72 65 61 64  eaders[0], &read
37c8e 65 72 73 5b 69 5d 29 20 29 20 62 72 65 61 6b 3b  ers[i]) ) break;
37c8f 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53  .    }..    /* S
37c90 70 65 63 69 61 6c 2d 63 61 73 65 20 66 6f 72 20  pecial-case for 
37c91 6e 6f 20 6d 65 72 67 65 2e 20 2a 2f 0a 20 20 20  no merge. */.   
37c92 20 69 66 28 20 69 3d 3d 31 20 29 7b 0a 20 20 20   if( i==1 ){.   
37c93 20 20 20 2f 2a 20 54 72 69 6d 20 64 65 6c 65 74     /* Trim delet
37c94 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 64 6f  ions from the do
37c95 63 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20  clist. */.      
37c96 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74 28  dataBufferReset(
37c97 26 6d 65 72 67 65 64 29 3b 0a 20 20 20 20 20 20  &merged);.      
37c98 64 6f 63 4c 69 73 74 54 72 69 6d 28 44 4c 5f 44  docListTrim(DL_D
37c99 45 46 41 55 4c 54 2c 0a 20 20 20 20 20 20 20 20  EFAULT,.        
37c9a 20 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65 61            optLea
37c9b 76 65 73 52 65 61 64 65 72 44 61 74 61 28 26 72  vesReaderData(&r
37c9c 65 61 64 65 72 73 5b 30 5d 29 2c 0a 20 20 20 20  eaders[0]),.    
37c9d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 70                op
37c9e 74 4c 65 61 76 65 73 52 65 61 64 65 72 44 61 74  tLeavesReaderDat
37c9f 61 42 79 74 65 73 28 26 72 65 61 64 65 72 73 5b  aBytes(&readers[
37ca0 30 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  0]),.           
37ca1 20 20 20 20 20 20 20 2d 31 2c 20 44 4c 5f 44 45         -1, DL_DE
37ca2 46 41 55 4c 54 2c 20 26 6d 65 72 67 65 64 29 3b  FAULT, &merged);
37ca3 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
37ca4 20 20 44 4c 52 65 61 64 65 72 20 64 6c 52 65 61    DLReader dlRea
37ca5 64 65 72 73 5b 4d 45 52 47 45 5f 43 4f 55 4e 54  ders[MERGE_COUNT
37ca6 5d 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 52 65  ];.      int iRe
37ca7 61 64 65 72 2c 20 6e 52 65 61 64 65 72 73 3b 0a  ader, nReaders;.
37ca8 0a 20 20 20 20 20 20 2f 2a 20 50 72 69 6d 65 20  .      /* Prime 
37ca9 74 68 65 20 70 69 70 65 6c 69 6e 65 20 77 69 74  the pipeline wit
37caa 68 20 74 68 65 20 66 69 72 73 74 20 72 65 61 64  h the first read
37cab 65 72 27 73 20 64 6f 63 6c 69 73 74 2e 20 20 41  er's doclist.  A
37cac 66 74 65 72 0a 20 20 20 20 20 20 2a 2a 20 6f 6e  fter.      ** on
37cad 65 20 70 61 73 73 20 69 6e 64 65 78 20 30 20 77  e pass index 0 w
37cae 69 6c 6c 20 72 65 66 65 72 65 6e 63 65 20 74 68  ill reference th
37caf 65 20 61 63 63 75 6d 75 6c 61 74 65 64 20 64 6f  e accumulated do
37cb0 63 6c 69 73 74 2e 0a 20 20 20 20 20 20 2a 2f 0a  clist..      */.
37cb1 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28 26 64        dlrInit(&d
37cb2 6c 52 65 61 64 65 72 73 5b 30 5d 2c 20 44 4c 5f  lReaders[0], DL_
37cb3 44 45 46 41 55 4c 54 2c 0a 20 20 20 20 20 20 20  DEFAULT,.       
37cb4 20 20 20 20 20 20 20 6f 70 74 4c 65 61 76 65 73         optLeaves
37cb5 52 65 61 64 65 72 44 61 74 61 28 26 72 65 61 64  ReaderData(&read
37cb6 65 72 73 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20  ers[0]),.       
37cb7 20 20 20 20 20 20 20 6f 70 74 4c 65 61 76 65 73         optLeaves
37cb8 52 65 61 64 65 72 44 61 74 61 42 79 74 65 73 28  ReaderDataBytes(
37cb9 26 72 65 61 64 65 72 73 5b 30 5d 29 29 3b 0a 20  &readers[0]));. 
37cba 20 20 20 20 20 69 52 65 61 64 65 72 20 3d 20 31       iReader = 1
37cbb 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
37cbc 20 69 52 65 61 64 65 72 3c 69 20 29 3b 20 20 2f   iReader<i );  /
37cbd 2a 20 4d 75 73 74 20 65 78 65 63 75 74 65 20 74  * Must execute t
37cbe 68 65 20 6c 6f 6f 70 20 61 74 20 6c 65 61 73 74  he loop at least
37cbf 20 6f 6e 63 65 2e 20 2a 2f 0a 20 20 20 20 20 20   once. */.      
37cc0 77 68 69 6c 65 28 20 69 52 65 61 64 65 72 3c 69  while( iReader<i
37cc1 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4d   ){.        /* M
37cc2 65 72 67 65 20 31 36 20 69 6e 70 75 74 73 20 70  erge 16 inputs p
37cc3 65 72 20 70 61 73 73 2e 20 2a 2f 0a 20 20 20 20  er pass. */.    
37cc4 20 20 20 20 66 6f 72 28 20 6e 52 65 61 64 65 72      for( nReader
37cc5 73 3d 31 3b 20 69 52 65 61 64 65 72 3c 69 20 26  s=1; iReader<i &
37cc6 26 20 6e 52 65 61 64 65 72 73 3c 4d 45 52 47 45  & nReaders<MERGE
37cc7 5f 43 4f 55 4e 54 3b 0a 20 20 20 20 20 20 20 20  _COUNT;.        
37cc8 20 20 20 20 20 69 52 65 61 64 65 72 2b 2b 2c 20       iReader++, 
37cc9 6e 52 65 61 64 65 72 73 2b 2b 20 29 7b 0a 20 20  nReaders++ ){.  
37cca 20 20 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28          dlrInit(
37ccb 26 64 6c 52 65 61 64 65 72 73 5b 6e 52 65 61 64  &dlReaders[nRead
37ccc 65 72 73 5d 2c 20 44 4c 5f 44 45 46 41 55 4c 54  ers], DL_DEFAULT
37ccd 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
37cce 20 20 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61      optLeavesRea
37ccf 64 65 72 44 61 74 61 28 26 72 65 61 64 65 72 73  derData(&readers
37cd0 5b 69 52 65 61 64 65 72 5d 29 2c 0a 20 20 20 20  [iReader]),.    
37cd1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 70                op
37cd2 74 4c 65 61 76 65 73 52 65 61 64 65 72 44 61 74  tLeavesReaderDat
37cd3 61 42 79 74 65 73 28 26 72 65 61 64 65 72 73 5b  aBytes(&readers[
37cd4 69 52 65 61 64 65 72 5d 29 29 3b 0a 20 20 20 20  iReader]));.    
37cd5 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f      }..        /
37cd6 2a 20 4d 65 72 67 65 20 64 6f 63 6c 69 73 74 73  * Merge doclists
37cd7 20 61 6e 64 20 73 77 61 70 20 72 65 73 75 6c 74   and swap result
37cd8 20 69 6e 74 6f 20 61 63 63 75 6d 75 6c 61 74 6f   into accumulato
37cd9 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 64 61  r. */.        da
37cda 74 61 42 75 66 66 65 72 52 65 73 65 74 28 26 6d  taBufferReset(&m
37cdb 65 72 67 65 64 29 3b 0a 20 20 20 20 20 20 20 20  erged);.        
37cdc 64 6f 63 4c 69 73 74 4d 65 72 67 65 28 26 6d 65  docListMerge(&me
37cdd 72 67 65 64 2c 20 64 6c 52 65 61 64 65 72 73 2c  rged, dlReaders,
37cde 20 6e 52 65 61 64 65 72 73 29 3b 0a 20 20 20 20   nReaders);.    
37cdf 20 20 20 20 74 6d 70 20 3d 20 6d 65 72 67 65 64      tmp = merged
37ce0 3b 0a 20 20 20 20 20 20 20 20 6d 65 72 67 65 64  ;.        merged
37ce1 20 3d 20 64 6f 63 6c 69 73 74 3b 0a 20 20 20 20   = doclist;.    
37ce2 20 20 20 20 64 6f 63 6c 69 73 74 20 3d 20 74 6d      doclist = tm
37ce3 70 3b 0a 0a 20 20 20 20 20 20 20 20 77 68 69 6c  p;..        whil
37ce4 65 28 20 6e 52 65 61 64 65 72 73 2d 2d 20 3e 20  e( nReaders-- > 
37ce5 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64  0 ){.          d
37ce6 6c 72 44 65 73 74 72 6f 79 28 26 64 6c 52 65 61  lrDestroy(&dlRea
37ce7 64 65 72 73 5b 6e 52 65 61 64 65 72 73 5d 29 3b  ders[nReaders]);
37ce8 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  .        }..    
37ce9 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74      /* Accumulat
37cea 65 64 20 64 6f 63 6c 69 73 74 20 74 6f 20 72 65  ed doclist to re
37ceb 61 64 65 72 20 30 20 66 6f 72 20 6e 65 78 74 20  ader 0 for next 
37cec 70 61 73 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20  pass. */.       
37ced 20 64 6c 72 49 6e 69 74 28 26 64 6c 52 65 61 64   dlrInit(&dlRead
37cee 65 72 73 5b 30 5d 2c 20 44 4c 5f 44 45 46 41 55  ers[0], DL_DEFAU
37cef 4c 54 2c 20 64 6f 63 6c 69 73 74 2e 70 44 61 74  LT, doclist.pDat
37cf0 61 2c 20 64 6f 63 6c 69 73 74 2e 6e 44 61 74 61  a, doclist.nData
37cf1 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  );.      }..    
37cf2 20 20 2f 2a 20 44 65 73 74 72 6f 79 20 72 65 61    /* Destroy rea
37cf3 64 65 72 20 74 68 61 74 20 77 61 73 20 6c 65 66  der that was lef
37cf4 74 20 69 6e 20 74 68 65 20 70 69 70 65 6c 69 6e  t in the pipelin
37cf5 65 2e 20 2a 2f 0a 20 20 20 20 20 20 64 6c 72 44  e. */.      dlrD
37cf6 65 73 74 72 6f 79 28 26 64 6c 52 65 61 64 65 72  estroy(&dlReader
37cf7 73 5b 30 5d 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  s[0]);..      /*
37cf8 20 54 72 69 6d 20 64 65 6c 65 74 69 6f 6e 73 20   Trim deletions 
37cf9 66 72 6f 6d 20 74 68 65 20 64 6f 63 6c 69 73 74  from the doclist
37cfa 2e 20 2a 2f 0a 20 20 20 20 20 20 64 61 74 61 42  . */.      dataB
37cfb 75 66 66 65 72 52 65 73 65 74 28 26 6d 65 72 67  ufferReset(&merg
37cfc 65 64 29 3b 0a 20 20 20 20 20 20 64 6f 63 4c 69  ed);.      docLi
37cfd 73 74 54 72 69 6d 28 44 4c 5f 44 45 46 41 55 4c  stTrim(DL_DEFAUL
37cfe 54 2c 20 64 6f 63 6c 69 73 74 2e 70 44 61 74 61  T, doclist.pData
37cff 2c 20 64 6f 63 6c 69 73 74 2e 6e 44 61 74 61 2c  , doclist.nData,
37d00 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
37d01 20 20 20 2d 31 2c 20 44 4c 5f 44 45 46 41 55 4c     -1, DL_DEFAUL
37d02 54 2c 20 26 6d 65 72 67 65 64 29 3b 0a 20 20 20  T, &merged);.   
37d03 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20   }..    /* Only 
37d04 70 61 73 73 20 64 6f 63 6c 69 73 74 73 20 77 69  pass doclists wi
37d05 74 68 20 68 69 74 73 20 28 73 6b 69 70 20 69 66  th hits (skip if
37d06 20 61 6c 6c 20 68 69 74 73 20 64 65 6c 65 74 65   all hits delete
37d07 64 29 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 6d  d). */.    if( m
37d08 65 72 67 65 64 2e 6e 44 61 74 61 3e 30 20 29 7b  erged.nData>0 ){
37d09 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 65 61 66  .      rc = leaf
37d0a 57 72 69 74 65 72 53 74 65 70 28 76 2c 20 70 57  WriterStep(v, pW
37d0b 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20  riter,.         
37d0c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37d0d 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
37d0e 54 65 72 6d 28 26 72 65 61 64 65 72 73 5b 30 5d  Term(&readers[0]
37d0f 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
37d10 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 70 74               opt
37d11 4c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  LeavesReaderTerm
37d12 42 79 74 65 73 28 26 72 65 61 64 65 72 73 5b 30  Bytes(&readers[0
37d13 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ]),.            
37d14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65                me
37d15 72 67 65 64 2e 70 44 61 74 61 2c 20 6d 65 72 67  rged.pData, merg
37d16 65 64 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20 20  ed.nData);.     
37d17 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
37d18 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 20  OK ) goto err;. 
37d19 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 74 65     }..    /* Ste
37d1a 70 20 6d 65 72 67 65 64 20 72 65 61 64 65 72 73  p merged readers
37d1b 20 74 6f 20 6e 65 78 74 20 74 65 72 6d 20 61 6e   to next term an
37d1c 64 20 72 65 6f 72 64 65 72 2e 20 2a 2f 0a 20 20  d reorder. */.  
37d1d 20 20 77 68 69 6c 65 28 20 69 2d 2d 20 3e 20 30    while( i-- > 0
37d1e 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6f   ){.      rc = o
37d1f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 53 74  ptLeavesReaderSt
37d20 65 70 28 76 2c 20 26 72 65 61 64 65 72 73 5b 69  ep(v, &readers[i
37d21 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  ]);.      if( rc
37d22 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
37d23 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 20 20 6f  to err;..      o
37d24 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 52 65  ptLeavesReaderRe
37d25 6f 72 64 65 72 28 26 72 65 61 64 65 72 73 5b 69  order(&readers[i
37d26 5d 2c 20 6e 52 65 61 64 65 72 73 2d 69 29 3b 0a  ], nReaders-i);.
37d27 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 65 72 72 3a      }.  }.. err:
37d28 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73  .  dataBufferDes
37d29 74 72 6f 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a  troy(&doclist);.
37d2a 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74    dataBufferDest
37d2b 72 6f 79 28 26 6d 65 72 67 65 64 29 3b 0a 20 20  roy(&merged);.  
37d2c 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
37d2d 20 49 6d 70 6c 65 6d 65 6e 74 20 6f 70 74 69 6d   Implement optim
37d2e 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  ize() function f
37d2f 6f 72 20 46 54 53 33 2e 20 20 6f 70 74 69 6d 69  or FTS3.  optimi
37d30 7a 65 28 74 29 20 6d 65 72 67 65 73 20 61 6c 6c  ze(t) merges all
37d31 0a 2a 2a 20 73 65 67 6d 65 6e 74 73 20 69 6e 20  .** segments in 
37d32 74 68 65 20 66 74 73 20 69 6e 64 65 78 20 69 6e  the fts index in
37d33 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 65 67 6d  to a single segm
37d34 65 6e 74 2e 20 20 27 74 27 20 69 73 20 74 68 65  ent.  't' is the
37d35 20 6d 61 67 69 63 0a 2a 2a 20 74 61 62 6c 65 2d   magic.** table-
37d36 6e 61 6d 65 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f  named column..*/
37d37 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 70 74  .static void opt
37d38 69 6d 69 7a 65 46 75 6e 63 28 73 71 6c 69 74 65  imizeFunc(sqlite
37d39 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74  3_context *pCont
37d3a 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ext,.           
37d3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
37d3c 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
37d3d 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20  value **argv){. 
37d3e 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72   fulltext_cursor
37d3f 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28   *pCursor;.  if(
37d40 20 61 72 67 63 3e 31 20 29 7b 0a 20 20 20 20 73   argc>1 ){.    s
37d41 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
37d42 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 65  ror(pContext, "e
37d43 78 63 65 73 73 20 61 72 67 75 6d 65 6e 74 73 20  xcess arguments 
37d44 74 6f 20 6f 70 74 69 6d 69 7a 65 28 29 22 2c 2d  to optimize()",-
37d45 31 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  1);.  }else if( 
37d46 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
37d47 70 65 28 61 72 67 76 5b 30 5d 29 21 3d 53 51 4c  pe(argv[0])!=SQL
37d48 49 54 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20 20 20  ITE_BLOB ||.    
37d49 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
37d4a 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
37d4b 5b 30 5d 29 21 3d 73 69 7a 65 6f 66 28 70 43 75  [0])!=sizeof(pCu
37d4c 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 73 71 6c  rsor) ){.    sql
37d4d 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
37d4e 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 69 6c 6c  r(pContext, "ill
37d4f 65 67 61 6c 20 66 69 72 73 74 20 61 72 67 75 6d  egal first argum
37d50 65 6e 74 20 74 6f 20 6f 70 74 69 6d 69 7a 65 22  ent to optimize"
37d51 2c 2d 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ,-1);.  }else{. 
37d52 20 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62     fulltext_vtab
37d53 20 2a 76 3b 0a 20 20 20 20 69 6e 74 20 69 2c 20   *v;.    int i, 
37d54 72 63 2c 20 69 4d 61 78 4c 65 76 65 6c 3b 0a 20  rc, iMaxLevel;. 
37d55 20 20 20 4f 70 74 4c 65 61 76 65 73 52 65 61 64     OptLeavesRead
37d56 65 72 20 2a 72 65 61 64 65 72 73 3b 0a 20 20 20  er *readers;.   
37d57 20 69 6e 74 20 6e 52 65 61 64 65 72 73 3b 0a 20   int nReaders;. 
37d58 20 20 20 4c 65 61 66 57 72 69 74 65 72 20 77 72     LeafWriter wr
37d59 69 74 65 72 3b 0a 20 20 20 20 73 71 6c 69 74 65  iter;.    sqlite
37d5a 33 5f 73 74 6d 74 20 2a 73 3b 0a 0a 20 20 20 20  3_stmt *s;..    
37d5b 6d 65 6d 63 70 79 28 26 70 43 75 72 73 6f 72 2c  memcpy(&pCursor,
37d5c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
37d5d 6c 6f 62 28 61 72 67 76 5b 30 5d 29 2c 20 73 69  lob(argv[0]), si
37d5e 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 29 3b 0a  zeof(pCursor));.
37d5f 20 20 20 20 76 20 3d 20 63 75 72 73 6f 72 5f 76      v = cursor_v
37d60 74 61 62 28 70 43 75 72 73 6f 72 29 3b 0a 0a 20  tab(pCursor);.. 
37d61 20 20 20 2f 2a 20 46 6c 75 73 68 20 61 6e 79 20     /* Flush any 
37d62 62 75 66 66 65 72 65 64 20 75 70 64 61 74 65 73  buffered updates
37d63 20 62 65 66 6f 72 65 20 6f 70 74 69 6d 69 7a 69   before optimizi
37d64 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  ng. */.    rc = 
37d65 66 6c 75 73 68 50 65 6e 64 69 6e 67 54 65 72 6d  flushPendingTerm
37d66 73 28 76 29 3b 0a 20 20 20 20 69 66 28 20 72 63  s(v);.    if( rc
37d67 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
37d68 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 72 63 20  to err;..    rc 
37d69 3d 20 73 65 67 64 69 72 5f 63 6f 75 6e 74 28 76  = segdir_count(v
37d6a 2c 20 26 6e 52 65 61 64 65 72 73 2c 20 26 69 4d  , &nReaders, &iM
37d6b 61 78 4c 65 76 65 6c 29 3b 0a 20 20 20 20 69 66  axLevel);.    if
37d6c 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
37d6d 29 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20 20 20  ) goto err;.    
37d6e 69 66 28 20 6e 52 65 61 64 65 72 73 3d 3d 30 20  if( nReaders==0 
37d6f 7c 7c 20 6e 52 65 61 64 65 72 73 3d 3d 31 20 29  || nReaders==1 )
37d70 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
37d71 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e  result_text(pCon
37d72 74 65 78 74 2c 20 22 49 6e 64 65 78 20 61 6c 72  text, "Index alr
37d73 65 61 64 79 20 6f 70 74 69 6d 61 6c 22 2c 20 2d  eady optimal", -
37d74 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1,.             
37d75 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
37d76 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
37d77 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
37d78 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 5f 67  ..    rc = sql_g
37d79 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
37d7a 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 41 4c  SEGDIR_SELECT_AL
37d7b 4c 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 20  L_STMT, &s);.   
37d7c 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
37d7d 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a  OK ) goto err;..
37d7e 20 20 20 20 72 65 61 64 65 72 73 20 3d 20 73 71      readers = sq
37d7f 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 52 65  lite3_malloc(nRe
37d80 61 64 65 72 73 2a 73 69 7a 65 6f 66 28 72 65 61  aders*sizeof(rea
37d81 64 65 72 73 5b 30 5d 29 29 3b 0a 20 20 20 20 69  ders[0]));.    i
37d82 66 28 20 72 65 61 64 65 72 73 3d 3d 4e 55 4c 4c  f( readers==NULL
37d83 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20   ) goto err;..  
37d84 20 20 2f 2a 20 4e 6f 74 65 20 74 68 61 74 20 74    /* Note that t
37d85 68 65 72 65 20 77 69 6c 6c 20 61 6c 72 65 61 64  here will alread
37d86 79 20 62 65 20 61 20 73 65 67 6d 65 6e 74 20 61  y be a segment a
37d87 74 20 74 68 69 73 20 70 6f 73 69 74 69 6f 6e 0a  t this position.
37d88 20 20 20 20 2a 2a 20 75 6e 74 69 6c 20 77 65 20      ** until we 
37d89 63 61 6c 6c 20 73 65 67 64 69 72 5f 64 65 6c 65  call segdir_dele
37d8a 74 65 28 29 20 6f 6e 20 69 4d 61 78 4c 65 76 65  te() on iMaxLeve
37d8b 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6c 65  l..    */.    le
37d8c 61 66 57 72 69 74 65 72 49 6e 69 74 28 69 4d 61  afWriterInit(iMa
37d8d 78 4c 65 76 65 6c 2c 20 30 2c 20 26 77 72 69 74  xLevel, 0, &writ
37d8e 65 72 29 3b 0a 0a 20 20 20 20 69 20 3d 20 30 3b  er);..    i = 0;
37d8f 0a 20 20 20 20 77 68 69 6c 65 28 20 28 72 63 20  .    while( (rc 
37d90 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73  = sqlite3_step(s
37d91 29 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  ))==SQLITE_ROW )
37d92 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69  {.      sqlite_i
37d93 6e 74 36 34 20 69 53 74 61 72 74 20 3d 20 73 71  nt64 iStart = sq
37d94 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
37d95 36 34 28 73 2c 20 30 29 3b 0a 20 20 20 20 20 20  64(s, 0);.      
37d96 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e  sqlite_int64 iEn
37d97 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  d = sqlite3_colu
37d98 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b 0a  mn_int64(s, 1);.
37d99 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
37d9a 20 2a 70 52 6f 6f 74 44 61 74 61 20 3d 20 73 71   *pRootData = sq
37d9b 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
37d9c 62 28 73 2c 20 32 29 3b 0a 20 20 20 20 20 20 69  b(s, 2);.      i
37d9d 6e 74 20 6e 52 6f 6f 74 44 61 74 61 20 3d 20 73  nt nRootData = s
37d9e 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
37d9f 74 65 73 28 73 2c 20 32 29 3b 0a 0a 20 20 20 20  tes(s, 2);..    
37da0 20 20 61 73 73 65 72 74 28 20 69 3c 6e 52 65 61    assert( i<nRea
37da1 64 65 72 73 20 29 3b 0a 20 20 20 20 20 20 72 63  ders );.      rc
37da2 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 49   = leavesReaderI
37da3 6e 69 74 28 76 2c 20 2d 31 2c 20 69 53 74 61 72  nit(v, -1, iStar
37da4 74 2c 20 69 45 6e 64 2c 20 70 52 6f 6f 74 44 61  t, iEnd, pRootDa
37da5 74 61 2c 20 6e 52 6f 6f 74 44 61 74 61 2c 0a 20  ta, nRootData,. 
37da6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37da7 20 20 20 20 20 20 20 20 20 20 20 26 72 65 61 64             &read
37da8 65 72 73 5b 69 5d 2e 72 65 61 64 65 72 29 3b 0a  ers[i].reader);.
37da9 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
37daa 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b  LITE_OK ) break;
37dab 0a 0a 20 20 20 20 20 20 72 65 61 64 65 72 73 5b  ..      readers[
37dac 69 5d 2e 73 65 67 6d 65 6e 74 20 3d 20 69 3b 0a  i].segment = i;.
37dad 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 7d        i++;.    }
37dae 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 6d  ..    /* If we m
37daf 61 6e 61 67 65 64 20 74 6f 20 73 75 63 63 65 73  anaged to succes
37db0 73 66 75 6c 6c 79 20 72 65 61 64 20 74 68 65 6d  sfully read them
37db1 20 61 6c 6c 2c 20 6f 70 74 69 6d 69 7a 65 20 74   all, optimize t
37db2 68 65 6d 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  hem. */.    if( 
37db3 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
37db4 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
37db5 20 69 3d 3d 6e 52 65 61 64 65 72 73 20 29 3b 0a   i==nReaders );.
37db6 20 20 20 20 20 20 72 63 20 3d 20 6f 70 74 69 6d        rc = optim
37db7 69 7a 65 49 6e 74 65 72 6e 61 6c 28 76 2c 20 72  izeInternal(v, r
37db8 65 61 64 65 72 73 2c 20 6e 52 65 61 64 65 72 73  eaders, nReaders
37db9 2c 20 26 77 72 69 74 65 72 29 3b 0a 20 20 20 20  , &writer);.    
37dba 7d 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d  }..    while( i-
37dbb 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20 20 20 6c  - > 0 ){.      l
37dbc 65 61 76 65 73 52 65 61 64 65 72 44 65 73 74 72  eavesReaderDestr
37dbd 6f 79 28 26 72 65 61 64 65 72 73 5b 69 5d 2e 72  oy(&readers[i].r
37dbe 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20  eader);.    }.  
37dbf 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 72    sqlite3_free(r
37dc0 65 61 64 65 72 73 29 3b 0a 0a 20 20 20 20 2f 2a  eaders);..    /*
37dc1 20 49 66 20 77 65 27 76 65 20 73 75 63 63 65 73   If we've succes
37dc2 73 66 75 6c 6c 79 20 67 6f 74 74 65 6e 20 74 6f  sfully gotten to
37dc3 20 68 65 72 65 2c 20 64 65 6c 65 74 65 20 74 68   here, delete th
37dc4 65 20 6f 6c 64 20 73 65 67 6d 65 6e 74 73 0a 20  e old segments. 
37dc5 20 20 20 2a 2a 20 61 6e 64 20 66 6c 75 73 68 20     ** and flush 
37dc6 74 68 65 20 69 6e 74 65 72 69 6f 72 20 73 74 72  the interior str
37dc7 75 63 74 75 72 65 20 6f 66 20 74 68 65 20 6e 65  ucture of the ne
37dc8 77 20 73 65 67 6d 65 6e 74 2e 0a 20 20 20 20 2a  w segment..    *
37dc9 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  /.    if( rc==SQ
37dca 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
37dcb 20 66 6f 72 28 20 69 3d 30 3b 20 69 3c 3d 69 4d   for( i=0; i<=iM
37dcc 61 78 4c 65 76 65 6c 3b 20 69 2b 2b 20 29 7b 0a  axLevel; i++ ){.
37dcd 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 65 67          rc = seg
37dce 64 69 72 5f 64 65 6c 65 74 65 28 76 2c 20 69 29  dir_delete(v, i)
37dcf 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  ;.        if( rc
37dd0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72  !=SQLITE_OK ) br
37dd1 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  eak;.      }..  
37dd2 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
37dd3 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 6c 65 61  TE_OK ) rc = lea
37dd4 66 57 72 69 74 65 72 46 69 6e 61 6c 69 7a 65 28  fWriterFinalize(
37dd5 76 2c 20 26 77 72 69 74 65 72 29 3b 0a 20 20 20  v, &writer);.   
37dd6 20 7d 0a 0a 20 20 20 20 6c 65 61 66 57 72 69 74   }..    leafWrit
37dd7 65 72 44 65 73 74 72 6f 79 28 26 77 72 69 74 65  erDestroy(&write
37dd8 72 29 3b 0a 0a 20 20 20 20 69 66 28 20 72 63 21  r);..    if( rc!
37dd9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
37dda 6f 20 65 72 72 3b 0a 0a 20 20 20 20 73 71 6c 69  o err;..    sqli
37ddb 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
37ddc 70 43 6f 6e 74 65 78 74 2c 20 22 49 6e 64 65 78  pContext, "Index
37ddd 20 6f 70 74 69 6d 69 7a 65 64 22 2c 20 2d 31 2c   optimized", -1,
37dde 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
37ddf 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 0a 20 20  .    return;..  
37de0 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
37de1 3a 20 45 72 72 6f 72 2d 68 61 6e 64 6c 69 6e 67  : Error-handling
37de2 20 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6d 70   needs to be imp
37de3 72 6f 76 65 64 20 61 6c 6f 6e 67 20 74 68 65 0a  roved along the.
37de4 20 20 20 20 2a 2a 20 6c 69 6e 65 73 20 6f 66 20      ** lines of 
37de5 74 68 65 20 64 75 6d 70 5f 20 66 75 6e 63 74 69  the dump_ functi
37de6 6f 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20 65 72 72  ons..    */. err
37de7 3a 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 63 68  :.    {.      ch
37de8 61 72 20 62 75 66 5b 35 31 32 5d 3b 0a 20 20 20  ar buf[512];.   
37de9 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
37dea 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66 29 2c  ntf(sizeof(buf),
37deb 20 62 75 66 2c 20 22 45 72 72 6f 72 20 69 6e 20   buf, "Error in 
37dec 6f 70 74 69 6d 69 7a 65 3a 20 25 73 22 2c 0a 20  optimize: %s",. 
37ded 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37dee 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 65 72        sqlite3_er
37def 72 6d 73 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e  rmsg(sqlite3_con
37df0 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 70  text_db_handle(p
37df1 43 6f 6e 74 65 78 74 29 29 29 3b 0a 20 20 20 20  Context)));.    
37df2 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
37df3 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c  _error(pContext,
37df4 20 62 75 66 2c 20 2d 31 29 3b 0a 20 20 20 20 7d   buf, -1);.    }
37df5 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65 66 20 53  .  }.}..#ifdef S
37df6 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 20 47 65  QLITE_TEST./* Ge
37df7 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20  nerate an error 
37df8 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3c 70 72  of the form "<pr
37df9 65 66 69 78 3e 3a 20 3c 6d 73 67 3e 22 2e 20 20  efix>: <msg>".  
37dfa 49 66 20 6d 73 67 20 69 73 20 4e 55 4c 4c 2c 0a  If msg is NULL,.
37dfb 2a 2a 20 70 75 6c 6c 20 74 68 65 20 65 72 72 6f  ** pull the erro
37dfc 72 20 66 72 6f 6d 20 74 68 65 20 63 6f 6e 74 65  r from the conte
37dfd 78 74 27 73 20 64 62 20 68 61 6e 64 6c 65 2e 0a  xt's db handle..
37dfe 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67  */.static void g
37dff 65 6e 65 72 61 74 65 45 72 72 6f 72 28 73 71 6c  enerateError(sql
37e00 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43  ite3_context *pC
37e01 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20  ontext,.        
37e02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37e03 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 72    const char *pr
37e04 65 66 69 78 2c 20 63 6f 6e 73 74 20 63 68 61 72  efix, const char
37e05 20 2a 6d 73 67 29 7b 0a 20 20 63 68 61 72 20 62   *msg){.  char b
37e06 75 66 5b 35 31 32 5d 3b 0a 20 20 69 66 28 20 6d  uf[512];.  if( m
37e07 73 67 3d 3d 4e 55 4c 4c 20 29 20 6d 73 67 20 3d  sg==NULL ) msg =
37e08 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28   sqlite3_errmsg(
37e09 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
37e0a 64 62 5f 68 61 6e 64 6c 65 28 70 43 6f 6e 74 65  db_handle(pConte
37e0b 78 74 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  xt));.  sqlite3_
37e0c 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
37e0d 62 75 66 29 2c 20 62 75 66 2c 20 22 25 73 3a 20  buf), buf, "%s: 
37e0e 25 73 22 2c 20 70 72 65 66 69 78 2c 20 6d 73 67  %s", prefix, msg
37e0f 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  );.  sqlite3_res
37e10 75 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65  ult_error(pConte
37e11 78 74 2c 20 62 75 66 2c 20 2d 31 29 3b 0a 7d 0a  xt, buf, -1);.}.
37e12 0a 2f 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74  ./* Helper funct
37e13 69 6f 6e 20 74 6f 20 63 6f 6c 6c 65 63 74 20 74  ion to collect t
37e14 68 65 20 73 65 74 20 6f 66 20 74 65 72 6d 73 20  he set of terms 
37e15 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74 20 69  in the segment i
37e16 6e 74 6f 0a 2a 2a 20 70 54 65 72 6d 73 2e 20 20  nto.** pTerms.  
37e17 54 68 65 20 73 65 67 6d 65 6e 74 20 69 73 20 64  The segment is d
37e18 65 66 69 6e 65 64 20 62 79 20 74 68 65 20 6c 65  efined by the le
37e19 61 66 20 6e 6f 64 65 73 20 62 65 74 77 65 65 6e  af nodes between
37e1a 0a 2a 2a 20 69 53 74 61 72 74 42 6c 6f 63 6b 69  .** iStartBlocki
37e1b 64 20 61 6e 64 20 69 45 6e 64 42 6c 6f 63 6b 69  d and iEndBlocki
37e1c 64 2c 20 69 6e 63 6c 75 73 69 76 65 2c 20 6f 72  d, inclusive, or
37e1d 20 62 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 73   by the contents
37e1e 20 6f 66 0a 2a 2a 20 70 52 6f 6f 74 44 61 74 61   of.** pRootData
37e1f 20 69 66 20 69 53 74 61 72 74 42 6c 6f 63 6b 69   if iStartBlocki
37e20 64 20 69 73 20 30 20 28 69 6e 20 77 68 69 63 68  d is 0 (in which
37e21 20 63 61 73 65 20 74 68 65 20 65 6e 74 69 72 65   case the entire
37e22 20 73 65 67 6d 65 6e 74 0a 2a 2a 20 66 69 74 20   segment.** fit 
37e23 69 6e 20 61 20 6c 65 61 66 29 2e 0a 2a 2f 0a 73  in a leaf)..*/.s
37e24 74 61 74 69 63 20 69 6e 74 20 63 6f 6c 6c 65 63  tatic int collec
37e25 74 53 65 67 6d 65 6e 74 54 65 72 6d 73 28 66 75  tSegmentTerms(fu
37e26 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
37e27 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 2c  sqlite3_stmt *s,
37e28 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
37e29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37e2a 66 74 73 33 48 61 73 68 20 2a 70 54 65 72 6d 73  fts3Hash *pTerms
37e2b 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  ){.  const sqlit
37e2c 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42 6c  e_int64 iStartBl
37e2d 6f 63 6b 69 64 20 3d 20 73 71 6c 69 74 65 33 5f  ockid = sqlite3_
37e2e 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20  column_int64(s, 
37e2f 30 29 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  0);.  const sqli
37e30 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f  te_int64 iEndBlo
37e31 63 6b 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 63  ckid = sqlite3_c
37e32 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31  olumn_int64(s, 1
37e33 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  );.  const char 
37e34 2a 70 52 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c  *pRootData = sql
37e35 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
37e36 28 73 2c 20 32 29 3b 0a 20 20 63 6f 6e 73 74 20  (s, 2);.  const 
37e37 69 6e 74 20 6e 52 6f 6f 74 44 61 74 61 20 3d 20  int nRootData = 
37e38 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
37e39 79 74 65 73 28 73 2c 20 32 29 3b 0a 20 20 4c 65  ytes(s, 2);.  Le
37e3a 61 76 65 73 52 65 61 64 65 72 20 72 65 61 64 65  avesReader reade
37e3b 72 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 6c 65  r;.  int rc = le
37e3c 61 76 65 73 52 65 61 64 65 72 49 6e 69 74 28 76  avesReaderInit(v
37e3d 2c 20 30 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b  , 0, iStartBlock
37e3e 69 64 2c 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c  id, iEndBlockid,
37e3f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
37e40 20 20 20 20 20 20 20 20 20 20 20 20 20 70 52 6f               pRo
37e41 6f 74 44 61 74 61 2c 20 6e 52 6f 6f 74 44 61 74  otData, nRootDat
37e42 61 2c 20 26 72 65 61 64 65 72 29 3b 0a 20 20 69  a, &reader);.  i
37e43 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
37e44 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
37e45 20 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49   while( rc==SQLI
37e46 54 45 5f 4f 4b 20 26 26 20 21 6c 65 61 76 65 73  TE_OK && !leaves
37e47 52 65 61 64 65 72 41 74 45 6e 64 28 26 72 65 61  ReaderAtEnd(&rea
37e48 64 65 72 29 20 29 7b 0a 20 20 20 20 63 6f 6e 73  der) ){.    cons
37e49 74 20 63 68 61 72 20 2a 70 54 65 72 6d 20 3d 20  t char *pTerm = 
37e4a 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  leavesReaderTerm
37e4b 28 26 72 65 61 64 65 72 29 3b 0a 20 20 20 20 63  (&reader);.    c
37e4c 6f 6e 73 74 20 69 6e 74 20 6e 54 65 72 6d 20 3d  onst int nTerm =
37e4d 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72   leavesReaderTer
37e4e 6d 42 79 74 65 73 28 26 72 65 61 64 65 72 29 3b  mBytes(&reader);
37e4f 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64 56 61  .    void *oldVa
37e50 6c 75 65 20 3d 20 73 71 6c 69 74 65 33 46 74 73  lue = sqlite3Fts
37e51 33 48 61 73 68 46 69 6e 64 28 70 54 65 72 6d 73  3HashFind(pTerms
37e52 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b  , pTerm, nTerm);
37e53 0a 20 20 20 20 76 6f 69 64 20 2a 6e 65 77 56 61  .    void *newVa
37e54 6c 75 65 20 3d 20 28 76 6f 69 64 20 2a 29 28 28  lue = (void *)((
37e55 63 68 61 72 20 2a 29 6f 6c 64 56 61 6c 75 65 2b  char *)oldValue+
37e56 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 72 6f 6d  1);..    /* From
37e57 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 62 65 66   the comment bef
37e58 6f 72 65 20 73 71 6c 69 74 65 33 46 74 73 33 48  ore sqlite3Fts3H
37e59 61 73 68 49 6e 73 65 72 74 20 69 6e 20 66 74 73  ashInsert in fts
37e5a 33 5f 68 61 73 68 2e 63 2c 0a 20 20 20 20 2a 2a  3_hash.c,.    **
37e5b 20 74 68 65 20 64 61 74 61 20 76 61 6c 75 65 20   the data value 
37e5c 70 61 73 73 65 64 20 69 73 20 72 65 74 75 72 6e  passed is return
37e5d 65 64 20 69 6e 20 63 61 73 65 20 6f 66 20 6d 61  ed in case of ma
37e5e 6c 6c 6f 63 20 66 61 69 6c 75 72 65 2e 0a 20 20  lloc failure..  
37e5f 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 65 77    */.    if( new
37e60 56 61 6c 75 65 3d 3d 73 71 6c 69 74 65 33 46 74  Value==sqlite3Ft
37e61 73 33 48 61 73 68 49 6e 73 65 72 74 28 70 54 65  s3HashInsert(pTe
37e62 72 6d 73 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72  rms, pTerm, nTer
37e63 6d 2c 20 6e 65 77 56 61 6c 75 65 29 20 29 7b 0a  m, newValue) ){.
37e64 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
37e65 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c  E_NOMEM;.    }el
37e66 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c  se{.      rc = l
37e67 65 61 76 65 73 52 65 61 64 65 72 53 74 65 70 28  eavesReaderStep(
37e68 76 2c 20 26 72 65 61 64 65 72 29 3b 0a 20 20 20  v, &reader);.   
37e69 20 7d 0a 20 20 7d 0a 0a 20 20 6c 65 61 76 65 73   }.  }..  leaves
37e6a 52 65 61 64 65 72 44 65 73 74 72 6f 79 28 26 72  ReaderDestroy(&r
37e6b 65 61 64 65 72 29 3b 0a 20 20 72 65 74 75 72 6e  eader);.  return
37e6c 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 48 65 6c 70 65   rc;.}../* Helpe
37e6d 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 75  r function to bu
37e6e 69 6c 64 20 74 68 65 20 72 65 73 75 6c 74 20 73  ild the result s
37e6f 74 72 69 6e 67 20 66 6f 72 20 64 75 6d 70 5f 74  tring for dump_t
37e70 65 72 6d 73 28 29 2e 20 2a 2f 0a 73 74 61 74 69  erms(). */.stati
37e71 63 20 69 6e 74 20 67 65 6e 65 72 61 74 65 54 65  c int generateTe
37e72 72 6d 73 52 65 73 75 6c 74 28 73 71 6c 69 74 65  rmsResult(sqlite
37e73 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74  3_context *pCont
37e74 65 78 74 2c 20 66 74 73 33 48 61 73 68 20 2a 70  ext, fts3Hash *p
37e75 54 65 72 6d 73 29 7b 0a 20 20 69 6e 74 20 69 54  Terms){.  int iT
37e76 65 72 6d 2c 20 6e 54 65 72 6d 73 2c 20 6e 52 65  erm, nTerms, nRe
37e77 73 75 6c 74 42 79 74 65 73 2c 20 69 42 79 74 65  sultBytes, iByte
37e78 3b 0a 20 20 63 68 61 72 20 2a 72 65 73 75 6c 74  ;.  char *result
37e79 3b 0a 20 20 54 65 72 6d 44 61 74 61 20 2a 70 44  ;.  TermData *pD
37e7a 61 74 61 3b 0a 20 20 66 74 73 33 48 61 73 68 45  ata;.  fts3HashE
37e7b 6c 65 6d 20 2a 65 3b 0a 0a 20 20 2f 2a 20 49 74  lem *e;..  /* It
37e7c 65 72 61 74 65 20 70 54 65 72 6d 73 20 74 6f 20  erate pTerms to 
37e7d 67 65 6e 65 72 61 74 65 20 61 6e 20 61 72 72 61  generate an arra
37e7e 79 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 70 44  y of terms in pD
37e7f 61 74 61 20 66 6f 72 0a 20 20 2a 2a 20 73 6f 72  ata for.  ** sor
37e80 74 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 6e 54 65  ting..  */.  nTe
37e81 72 6d 73 20 3d 20 66 74 73 33 48 61 73 68 43 6f  rms = fts3HashCo
37e82 75 6e 74 28 70 54 65 72 6d 73 29 3b 0a 20 20 61  unt(pTerms);.  a
37e83 73 73 65 72 74 28 20 6e 54 65 72 6d 73 3e 30 20  ssert( nTerms>0 
37e84 29 3b 0a 20 20 70 44 61 74 61 20 3d 20 73 71 6c  );.  pData = sql
37e85 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 54 65 72  ite3_malloc(nTer
37e86 6d 73 2a 73 69 7a 65 6f 66 28 54 65 72 6d 44 61  ms*sizeof(TermDa
37e87 74 61 29 29 3b 0a 20 20 69 66 28 20 70 44 61 74  ta));.  if( pDat
37e88 61 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e  a==NULL ) return
37e89 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a   SQLITE_NOMEM;..
37e8a 20 20 6e 52 65 73 75 6c 74 42 79 74 65 73 20 3d    nResultBytes =
37e8b 20 30 3b 0a 20 20 66 6f 72 28 69 54 65 72 6d 20   0;.  for(iTerm 
37e8c 3d 20 30 2c 20 65 20 3d 20 66 74 73 33 48 61 73  = 0, e = fts3Has
37e8d 68 46 69 72 73 74 28 70 54 65 72 6d 73 29 3b 20  hFirst(pTerms); 
37e8e 65 3b 20 69 54 65 72 6d 2b 2b 2c 20 65 20 3d 20  e; iTerm++, e = 
37e8f 66 74 73 33 48 61 73 68 4e 65 78 74 28 65 29 29  fts3HashNext(e))
37e90 7b 0a 20 20 20 20 6e 52 65 73 75 6c 74 42 79 74  {.    nResultByt
37e91 65 73 20 2b 3d 20 66 74 73 33 48 61 73 68 4b 65  es += fts3HashKe
37e92 79 73 69 7a 65 28 65 29 2b 31 3b 20 20 20 2f 2a  ysize(e)+1;   /*
37e93 20 54 65 72 6d 20 70 6c 75 73 20 74 72 61 69 6c   Term plus trail
37e94 69 6e 67 20 73 70 61 63 65 20 2a 2f 0a 20 20 20  ing space */.   
37e95 20 61 73 73 65 72 74 28 20 69 54 65 72 6d 3c 6e   assert( iTerm<n
37e96 54 65 72 6d 73 20 29 3b 0a 20 20 20 20 70 44 61  Terms );.    pDa
37e97 74 61 5b 69 54 65 72 6d 5d 2e 70 54 65 72 6d 20  ta[iTerm].pTerm 
37e98 3d 20 66 74 73 33 48 61 73 68 4b 65 79 28 65 29  = fts3HashKey(e)
37e99 3b 0a 20 20 20 20 70 44 61 74 61 5b 69 54 65 72  ;.    pData[iTer
37e9a 6d 5d 2e 6e 54 65 72 6d 20 3d 20 66 74 73 33 48  m].nTerm = fts3H
37e9b 61 73 68 4b 65 79 73 69 7a 65 28 65 29 3b 0a 20  ashKeysize(e);. 
37e9c 20 20 20 70 44 61 74 61 5b 69 54 65 72 6d 5d 2e     pData[iTerm].
37e9d 70 43 6f 6c 6c 65 63 74 6f 72 20 3d 20 66 74 73  pCollector = fts
37e9e 33 48 61 73 68 44 61 74 61 28 65 29 3b 20 20 2f  3HashData(e);  /
37e9f 2a 20 75 6e 75 73 65 64 20 2a 2f 0a 20 20 7d 0a  * unused */.  }.
37ea0 20 20 61 73 73 65 72 74 28 20 69 54 65 72 6d 3d    assert( iTerm=
37ea1 3d 6e 54 65 72 6d 73 20 29 3b 0a 0a 20 20 61 73  =nTerms );..  as
37ea2 73 65 72 74 28 20 6e 52 65 73 75 6c 74 42 79 74  sert( nResultByt
37ea3 65 73 3e 30 20 29 3b 20 20 20 2f 2a 20 6e 54 65  es>0 );   /* nTe
37ea4 72 6d 73 3e 30 2c 20 6e 52 65 73 75 6c 74 73 42  rms>0, nResultsB
37ea5 79 74 65 73 20 6d 75 73 74 20 62 65 2c 20 74 6f  ytes must be, to
37ea6 6f 2e 20 2a 2f 0a 20 20 72 65 73 75 6c 74 20 3d  o. */.  result =
37ea7 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
37ea8 6e 52 65 73 75 6c 74 42 79 74 65 73 29 3b 0a 20  nResultBytes);. 
37ea9 20 69 66 28 20 72 65 73 75 6c 74 3d 3d 4e 55 4c   if( result==NUL
37eaa 4c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  L ){.    sqlite3
37eab 5f 66 72 65 65 28 70 44 61 74 61 29 3b 0a 20 20  _free(pData);.  
37eac 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
37ead 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66  NOMEM;.  }..  if
37eae 28 20 6e 54 65 72 6d 73 3e 31 20 29 20 71 73 6f  ( nTerms>1 ) qso
37eaf 72 74 28 70 44 61 74 61 2c 20 6e 54 65 72 6d 73  rt(pData, nTerms
37eb0 2c 20 73 69 7a 65 6f 66 28 2a 70 44 61 74 61 29  , sizeof(*pData)
37eb1 2c 20 74 65 72 6d 44 61 74 61 43 6d 70 29 3b 0a  , termDataCmp);.
37eb2 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 74  .  /* Read the t
37eb3 65 72 6d 73 20 69 6e 20 6f 72 64 65 72 20 74 6f  erms in order to
37eb4 20 62 75 69 6c 64 20 74 68 65 20 72 65 73 75 6c   build the resul
37eb5 74 2e 20 2a 2f 0a 20 20 69 42 79 74 65 20 3d 20  t. */.  iByte = 
37eb6 30 3b 0a 20 20 66 6f 72 28 69 54 65 72 6d 3d 30  0;.  for(iTerm=0
37eb7 3b 20 69 54 65 72 6d 3c 6e 54 65 72 6d 73 3b 20  ; iTerm<nTerms; 
37eb8 2b 2b 69 54 65 72 6d 29 7b 0a 20 20 20 20 6d 65  ++iTerm){.    me
37eb9 6d 63 70 79 28 72 65 73 75 6c 74 2b 69 42 79 74  mcpy(result+iByt
37eba 65 2c 20 70 44 61 74 61 5b 69 54 65 72 6d 5d 2e  e, pData[iTerm].
37ebb 70 54 65 72 6d 2c 20 70 44 61 74 61 5b 69 54 65  pTerm, pData[iTe
37ebc 72 6d 5d 2e 6e 54 65 72 6d 29 3b 0a 20 20 20 20  rm].nTerm);.    
37ebd 69 42 79 74 65 20 2b 3d 20 70 44 61 74 61 5b 69  iByte += pData[i
37ebe 54 65 72 6d 5d 2e 6e 54 65 72 6d 3b 0a 20 20 20  Term].nTerm;.   
37ebf 20 72 65 73 75 6c 74 5b 69 42 79 74 65 2b 2b 5d   result[iByte++]
37ec0 20 3d 20 27 20 27 3b 0a 20 20 7d 0a 20 20 61 73   = ' ';.  }.  as
37ec1 73 65 72 74 28 20 69 42 79 74 65 3d 3d 6e 52 65  sert( iByte==nRe
37ec2 73 75 6c 74 42 79 74 65 73 20 29 3b 0a 20 20 61  sultBytes );.  a
37ec3 73 73 65 72 74 28 20 72 65 73 75 6c 74 5b 6e 52  ssert( result[nR
37ec4 65 73 75 6c 74 42 79 74 65 73 2d 31 5d 3d 3d 27  esultBytes-1]=='
37ec5 20 27 20 29 3b 0a 20 20 72 65 73 75 6c 74 5b 6e   ' );.  result[n
37ec6 52 65 73 75 6c 74 42 79 74 65 73 2d 31 5d 20 3d  ResultBytes-1] =
37ec7 20 27 5c 30 27 3b 0a 0a 20 20 2f 2a 20 50 61 73   '\0';..  /* Pas
37ec8 73 65 73 20 61 77 61 79 20 6f 77 6e 65 72 73 68  ses away ownersh
37ec9 69 70 20 6f 66 20 72 65 73 75 6c 74 2e 20 2a 2f  ip of result. */
37eca 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
37ecb 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78 74 2c  t_text(pContext,
37ecc 20 72 65 73 75 6c 74 2c 20 6e 52 65 73 75 6c 74   result, nResult
37ecd 42 79 74 65 73 2d 31 2c 20 73 71 6c 69 74 65 33  Bytes-1, sqlite3
37ece 5f 66 72 65 65 29 3b 0a 20 20 73 71 6c 69 74 65  _free);.  sqlite
37ecf 33 5f 66 72 65 65 28 70 44 61 74 61 29 3b 0a 20  3_free(pData);. 
37ed0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
37ed1 4b 3b 0a 7d 0a 0a 2f 2a 20 49 6d 70 6c 65 6d 65  K;.}../* Impleme
37ed2 6e 74 73 20 64 75 6d 70 5f 74 65 72 6d 73 28 29  nts dump_terms()
37ed3 20 66 6f 72 20 75 73 65 20 69 6e 20 69 6e 73 70   for use in insp
37ed4 65 63 74 69 6e 67 20 74 68 65 20 66 74 73 33 20  ecting the fts3 
37ed5 69 6e 64 65 78 20 66 72 6f 6d 0a 2a 2a 20 74 65  index from.** te
37ed6 73 74 73 2e 20 20 54 45 58 54 20 72 65 73 75 6c  sts.  TEXT resul
37ed7 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  t containing the
37ed8 20 6f 72 64 65 72 65 64 20 6c 69 73 74 20 6f 66   ordered list of
37ed9 20 74 65 72 6d 73 20 6a 6f 69 6e 65 64 20 62 79   terms joined by
37eda 0a 2a 2a 20 73 70 61 63 65 73 2e 20 20 64 75 6d  .** spaces.  dum
37edb 70 5f 74 65 72 6d 73 28 74 2c 20 6c 65 76 65 6c  p_terms(t, level
37edc 2c 20 69 64 78 29 20 64 75 6d 70 73 20 74 68 65  , idx) dumps the
37edd 20 74 65 72 6d 73 20 66 6f 72 20 74 68 65 20 73   terms for the s
37ede 65 67 6d 65 6e 74 0a 2a 2a 20 73 70 65 63 69 66  egment.** specif
37edf 69 65 64 20 62 79 20 6c 65 76 65 6c 2c 20 69 64  ied by level, id
37ee0 78 20 28 69 6e 20 25 5f 73 65 67 64 69 72 29 2c  x (in %_segdir),
37ee1 20 77 68 69 6c 65 20 64 75 6d 70 5f 74 65 72 6d   while dump_term
37ee2 73 28 74 29 20 64 75 6d 70 73 0a 2a 2a 20 61 6c  s(t) dumps.** al
37ee3 6c 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 69  l terms in the i
37ee4 6e 64 65 78 2e 20 20 49 6e 20 62 6f 74 68 20 63  ndex.  In both c
37ee5 61 73 65 73 20 74 20 69 73 20 74 68 65 20 66 74  ases t is the ft
37ee6 73 20 74 61 62 6c 65 27 73 20 6d 61 67 69 63 0a  s table's magic.
37ee7 2a 2a 20 74 61 62 6c 65 2d 6e 61 6d 65 64 20 63  ** table-named c
37ee8 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  olumn..*/.static
37ee9 20 76 6f 69 64 20 64 75 6d 70 54 65 72 6d 73 46   void dumpTermsF
37eea 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
37eeb 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74  ontext *pContext
37eec 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71  ,.  int argc, sq
37eed 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
37eee 67 76 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74  gv.){.  fulltext
37eef 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72  _cursor *pCursor
37ef0 3b 0a 20 20 69 66 28 20 61 72 67 63 21 3d 33 20  ;.  if( argc!=3 
37ef1 26 26 20 61 72 67 63 21 3d 31 20 29 7b 0a 20 20  && argc!=1 ){.  
37ef2 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28    generateError(
37ef3 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f  pContext, "dump_
37ef4 74 65 72 6d 73 22 2c 20 22 69 6e 63 6f 72 72 65  terms", "incorre
37ef5 63 74 20 61 72 67 75 6d 65 6e 74 73 22 29 3b 0a  ct arguments");.
37ef6 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69    }else if( sqli
37ef7 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61  te3_value_type(a
37ef8 72 67 76 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f  rgv[0])!=SQLITE_
37ef9 42 4c 4f 42 20 7c 7c 0a 20 20 20 20 20 20 20 20  BLOB ||.        
37efa 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75      sqlite3_valu
37efb 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29  e_bytes(argv[0])
37efc 21 3d 73 69 7a 65 6f 66 28 70 43 75 72 73 6f 72  !=sizeof(pCursor
37efd 29 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61 74  ) ){.    generat
37efe 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c  eError(pContext,
37eff 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20 22   "dump_terms", "
37f00 69 6c 6c 65 67 61 6c 20 66 69 72 73 74 20 61 72  illegal first ar
37f01 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 65 6c 73  gument");.  }els
37f02 65 7b 0a 20 20 20 20 66 75 6c 6c 74 65 78 74 5f  e{.    fulltext_
37f03 76 74 61 62 20 2a 76 3b 0a 20 20 20 20 66 74 73  vtab *v;.    fts
37f04 33 48 61 73 68 20 74 65 72 6d 73 3b 0a 20 20 20  3Hash terms;.   
37f05 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73   sqlite3_stmt *s
37f06 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74   = NULL;.    int
37f07 20 72 63 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79   rc;..    memcpy
37f08 28 26 70 43 75 72 73 6f 72 2c 20 73 71 6c 69 74  (&pCursor, sqlit
37f09 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72  e3_value_blob(ar
37f0a 67 76 5b 30 5d 29 2c 20 73 69 7a 65 6f 66 28 70  gv[0]), sizeof(p
37f0b 43 75 72 73 6f 72 29 29 3b 0a 20 20 20 20 76 20  Cursor));.    v 
37f0c 3d 20 63 75 72 73 6f 72 5f 76 74 61 62 28 70 43  = cursor_vtab(pC
37f0d 75 72 73 6f 72 29 3b 0a 0a 20 20 20 20 2f 2a 20  ursor);..    /* 
37f0e 49 66 20 70 61 73 73 65 64 20 6f 6e 6c 79 20 74  If passed only t
37f0f 68 65 20 63 75 72 73 6f 72 20 63 6f 6c 75 6d 6e  he cursor column
37f10 2c 20 67 65 74 20 61 6c 6c 20 73 65 67 6d 65 6e  , get all segmen
37f11 74 73 2e 20 20 4f 74 68 65 72 77 69 73 65 0a 20  ts.  Otherwise. 
37f12 20 20 20 2a 2a 20 67 65 74 20 74 68 65 20 73 65     ** get the se
37f13 67 6d 65 6e 74 20 64 65 73 63 72 69 62 65 64 20  gment described 
37f14 62 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  by the following
37f15 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e 0a   two arguments..
37f16 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 61      */.    if( a
37f17 72 67 63 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  rgc==1 ){.      
37f18 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61  rc = sql_get_sta
37f19 74 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52  tement(v, SEGDIR
37f1a 5f 53 45 4c 45 43 54 5f 41 4c 4c 5f 53 54 4d 54  _SELECT_ALL_STMT
37f1b 2c 20 26 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65  , &s);.    }else
37f1c 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
37f1d 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
37f1e 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f  , SEGDIR_SELECT_
37f1f 53 45 47 4d 45 4e 54 5f 53 54 4d 54 2c 20 26 73  SEGMENT_STMT, &s
37f20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
37f21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
37f22 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
37f23 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31  e3_bind_int(s, 1
37f24 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  , sqlite3_value_
37f25 69 6e 74 28 61 72 67 76 5b 31 5d 29 29 3b 0a 20  int(argv[1]));. 
37f26 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
37f27 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
37f28 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
37f29 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 32  e3_bind_int(s, 2
37f2a 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  , sqlite3_value_
37f2b 69 6e 74 28 61 72 67 76 5b 32 5d 29 29 3b 0a 20  int(argv[2]));. 
37f2c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
37f2d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
37f2e 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
37f2f 0a 20 20 20 20 20 20 67 65 6e 65 72 61 74 65 45  .      generateE
37f30 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22  rror(pContext, "
37f31 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20 4e 55 4c  dump_terms", NUL
37f32 4c 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  L);.      return
37f33 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
37f34 43 6f 6c 6c 65 63 74 20 74 68 65 20 74 65 72 6d  Collect the term
37f35 73 20 66 6f 72 20 65 61 63 68 20 73 65 67 6d 65  s for each segme
37f36 6e 74 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  nt. */.    sqlit
37f37 65 33 46 74 73 33 48 61 73 68 49 6e 69 74 28 26  e3Fts3HashInit(&
37f38 74 65 72 6d 73 2c 20 46 54 53 33 5f 48 41 53 48  terms, FTS3_HASH
37f39 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a 20 20 20  _STRING, 1);.   
37f3a 20 77 68 69 6c 65 28 20 28 72 63 20 3d 20 73 71   while( (rc = sq
37f3b 6c 69 74 65 33 5f 73 74 65 70 28 73 29 29 3d 3d  lite3_step(s))==
37f3c 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
37f3d 20 20 20 20 72 63 20 3d 20 63 6f 6c 6c 65 63 74      rc = collect
37f3e 53 65 67 6d 65 6e 74 54 65 72 6d 73 28 76 2c 20  SegmentTerms(v, 
37f3f 73 2c 20 26 74 65 72 6d 73 29 3b 0a 20 20 20 20  s, &terms);.    
37f40 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
37f41 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  _OK ) break;.   
37f42 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 21 3d   }..    if( rc!=
37f43 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20  SQLITE_DONE ){. 
37f44 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
37f45 65 74 28 73 29 3b 0a 20 20 20 20 20 20 67 65 6e  et(s);.      gen
37f46 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74  erateError(pCont
37f47 65 78 74 2c 20 22 64 75 6d 70 5f 74 65 72 6d 73  ext, "dump_terms
37f48 22 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 7d 65  ", NULL);.    }e
37f49 6c 73 65 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74  lse{.      const
37f4a 20 69 6e 74 20 6e 54 65 72 6d 73 20 3d 20 66 74   int nTerms = ft
37f4b 73 33 48 61 73 68 43 6f 75 6e 74 28 26 74 65 72  s3HashCount(&ter
37f4c 6d 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e  ms);.      if( n
37f4d 54 65 72 6d 73 3e 30 20 29 7b 0a 20 20 20 20 20  Terms>0 ){.     
37f4e 20 20 20 72 63 20 3d 20 67 65 6e 65 72 61 74 65     rc = generate
37f4f 54 65 72 6d 73 52 65 73 75 6c 74 28 70 43 6f 6e  TermsResult(pCon
37f50 74 65 78 74 2c 20 26 74 65 72 6d 73 29 3b 0a 20  text, &terms);. 
37f51 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
37f52 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20  QLITE_NOMEM ){. 
37f53 20 20 20 20 20 20 20 20 20 67 65 6e 65 72 61 74           generat
37f54 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c  eError(pContext,
37f55 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20 22   "dump_terms", "
37f56 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b  out of memory");
37f57 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
37f58 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
37f59 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
37f5a 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
37f5b 20 20 20 7d 65 6c 73 65 20 69 66 28 20 61 72 67     }else if( arg
37f5c 63 3d 3d 33 20 29 7b 0a 20 20 20 20 20 20 20 20  c==3 ){.        
37f5d 2f 2a 20 54 68 65 20 73 70 65 63 69 66 69 63 20  /* The specific 
37f5e 73 65 67 6d 65 6e 74 20 61 73 6b 65 64 20 66 6f  segment asked fo
37f5f 72 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 66  r could not be f
37f60 6f 75 6e 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20  ound. */.       
37f61 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70   generateError(p
37f62 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 74  Context, "dump_t
37f63 65 72 6d 73 22 2c 20 22 73 65 67 6d 65 6e 74 20  erms", "segment 
37f64 6e 6f 74 20 66 6f 75 6e 64 22 29 3b 0a 20 20 20  not found");.   
37f65 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
37f66 20 20 2f 2a 20 4e 6f 20 73 65 67 6d 65 6e 74 73    /* No segments
37f67 20 66 6f 75 6e 64 2e 20 2a 2f 0a 20 20 20 20 20   found. */.     
37f68 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73     /* TODO(shess
37f69 29 3a 20 49 74 20 73 68 6f 75 6c 64 20 62 65 20  ): It should be 
37f6a 69 6d 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 65  impossible to re
37f6b 61 63 68 20 74 68 69 73 2e 20 20 54 68 69 73 0a  ach this.  This.
37f6c 20 20 20 20 20 20 20 20 2a 2a 20 63 61 73 65 20          ** case 
37f6d 63 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20  can only happen 
37f6e 66 6f 72 20 61 6e 20 65 6d 70 74 79 20 74 61 62  for an empty tab
37f6f 6c 65 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73  le, in which cas
37f70 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 53 51 4c  e.        ** SQL
37f71 69 74 65 20 68 61 73 20 6e 6f 20 72 6f 77 73 20  ite has no rows 
37f72 74 6f 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e  to call this fun
37f73 63 74 69 6f 6e 20 6f 6e 2e 0a 20 20 20 20 20 20  ction on..      
37f74 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c    */.        sql
37f75 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c  ite3_result_null
37f76 28 70 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  (pContext);.    
37f77 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71    }.    }.    sq
37f78 6c 69 74 65 33 46 74 73 33 48 61 73 68 43 6c 65  lite3Fts3HashCle
37f79 61 72 28 26 74 65 72 6d 73 29 3b 0a 20 20 7d 0a  ar(&terms);.  }.
37f7a 7d 0a 0a 2f 2a 20 45 78 70 61 6e 64 20 74 68 65  }../* Expand the
37f7b 20 44 4c 5f 44 45 46 41 55 4c 54 20 64 6f 63 6c   DL_DEFAULT docl
37f7c 69 73 74 20 69 6e 20 70 44 61 74 61 20 69 6e 74  ist in pData int
37f7d 6f 20 61 20 74 65 78 74 20 72 65 73 75 6c 74 20  o a text result 
37f7e 69 6e 0a 2a 2a 20 70 43 6f 6e 74 65 78 74 2e 0a  in.** pContext..
37f7f 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
37f80 72 65 61 74 65 44 6f 63 6c 69 73 74 52 65 73 75  reateDoclistResu
37f81 6c 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  lt(sqlite3_conte
37f82 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20  xt *pContext,.  
37f83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37f84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
37f85 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c  nst char *pData,
37f86 20 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20 20 44   int nData){.  D
37f87 61 74 61 42 75 66 66 65 72 20 64 75 6d 70 3b 0a  ataBuffer dump;.
37f88 20 20 44 4c 52 65 61 64 65 72 20 64 6c 52 65 61    DLReader dlRea
37f89 64 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  der;..  assert( 
37f8a 70 44 61 74 61 21 3d 4e 55 4c 4c 20 26 26 20 6e  pData!=NULL && n
37f8b 44 61 74 61 3e 30 20 29 3b 0a 0a 20 20 64 61 74  Data>0 );..  dat
37f8c 61 42 75 66 66 65 72 49 6e 69 74 28 26 64 75 6d  aBufferInit(&dum
37f8d 70 2c 20 30 29 3b 0a 20 20 64 6c 72 49 6e 69 74  p, 0);.  dlrInit
37f8e 28 26 64 6c 52 65 61 64 65 72 2c 20 44 4c 5f 44  (&dlReader, DL_D
37f8f 45 46 41 55 4c 54 2c 20 70 44 61 74 61 2c 20 6e  EFAULT, pData, n
37f90 44 61 74 61 29 3b 0a 20 20 66 6f 72 28 20 3b 20  Data);.  for( ; 
37f91 21 64 6c 72 41 74 45 6e 64 28 26 64 6c 52 65 61  !dlrAtEnd(&dlRea
37f92 64 65 72 29 3b 20 64 6c 72 53 74 65 70 28 26 64  der); dlrStep(&d
37f93 6c 52 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20  lReader) ){.    
37f94 63 68 61 72 20 62 75 66 5b 32 35 36 5d 3b 0a 20  char buf[256];. 
37f95 20 20 20 50 4c 52 65 61 64 65 72 20 70 6c 52 65     PLReader plRe
37f96 61 64 65 72 3b 0a 0a 20 20 20 20 70 6c 72 49 6e  ader;..    plrIn
37f97 69 74 28 26 70 6c 52 65 61 64 65 72 2c 20 26 64  it(&plReader, &d
37f98 6c 52 65 61 64 65 72 29 3b 0a 20 20 20 20 69 66  lReader);.    if
37f99 28 20 44 4c 5f 44 45 46 41 55 4c 54 3d 3d 44 4c  ( DL_DEFAULT==DL
37f9a 5f 44 4f 43 49 44 53 20 7c 7c 20 70 6c 72 41 74  _DOCIDS || plrAt
37f9b 45 6e 64 28 26 70 6c 52 65 61 64 65 72 29 20 29  End(&plReader) )
37f9c 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
37f9d 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
37f9e 62 75 66 29 2c 20 62 75 66 2c 20 22 5b 25 6c 6c  buf), buf, "[%ll
37f9f 64 5d 20 22 2c 20 64 6c 72 44 6f 63 69 64 28 26  d] ", dlrDocid(&
37fa0 64 6c 52 65 61 64 65 72 29 29 3b 0a 20 20 20 20  dlReader));.    
37fa1 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
37fa2 6e 64 28 26 64 75 6d 70 2c 20 62 75 66 2c 20 73  nd(&dump, buf, s
37fa3 74 72 6c 65 6e 28 62 75 66 29 29 3b 0a 20 20 20  trlen(buf));.   
37fa4 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e   }else{.      in
37fa5 74 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 6c 72 43  t iColumn = plrC
37fa6 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65 72 29  olumn(&plReader)
37fa7 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ;..      sqlite3
37fa8 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66  _snprintf(sizeof
37fa9 28 62 75 66 29 2c 20 62 75 66 2c 20 22 5b 25 6c  (buf), buf, "[%l
37faa 6c 64 20 25 64 5b 22 2c 0a 20 20 20 20 20 20 20  ld %d[",.       
37fab 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37fac 64 6c 72 44 6f 63 69 64 28 26 64 6c 52 65 61 64  dlrDocid(&dlRead
37fad 65 72 29 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 20  er), iColumn);. 
37fae 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41       dataBufferA
37faf 70 70 65 6e 64 28 26 64 75 6d 70 2c 20 62 75 66  ppend(&dump, buf
37fb0 2c 20 73 74 72 6c 65 6e 28 62 75 66 29 29 3b 0a  , strlen(buf));.
37fb1 0a 20 20 20 20 20 20 66 6f 72 28 20 3b 20 21 70  .      for( ; !p
37fb2 6c 72 41 74 45 6e 64 28 26 70 6c 52 65 61 64 65  lrAtEnd(&plReade
37fb3 72 29 3b 20 70 6c 72 53 74 65 70 28 26 70 6c 52  r); plrStep(&plR
37fb4 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20 20 20  eader) ){.      
37fb5 20 20 69 66 28 20 70 6c 72 43 6f 6c 75 6d 6e 28    if( plrColumn(
37fb6 26 70 6c 52 65 61 64 65 72 29 21 3d 69 43 6f 6c  &plReader)!=iCol
37fb7 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20  umn ){.         
37fb8 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 6c 72 43 6f   iColumn = plrCo
37fb9 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65 72 29 3b  lumn(&plReader);
37fba 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
37fbb 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65  e3_snprintf(size
37fbc 6f 66 28 62 75 66 29 2c 20 62 75 66 2c 20 22 5d  of(buf), buf, "]
37fbd 20 25 64 5b 22 2c 20 69 43 6f 6c 75 6d 6e 29 3b   %d[", iColumn);
37fbe 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
37fbf 74 28 20 64 75 6d 70 2e 6e 44 61 74 61 3e 30 20  t( dump.nData>0 
37fc0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 64 75 6d  );.          dum
37fc1 70 2e 6e 44 61 74 61 2d 2d 3b 20 20 20 20 20 20  p.nData--;      
37fc2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
37fc3 2a 20 4f 76 65 72 77 72 69 74 65 20 74 72 61 69  * Overwrite trai
37fc4 6c 69 6e 67 20 73 70 61 63 65 2e 20 2a 2f 0a 20  ling space. */. 
37fc5 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
37fc6 20 64 75 6d 70 2e 70 44 61 74 61 5b 64 75 6d 70   dump.pData[dump
37fc7 2e 6e 44 61 74 61 5d 3d 3d 27 20 27 29 3b 0a 20  .nData]==' ');. 
37fc8 20 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66           dataBuf
37fc9 66 65 72 41 70 70 65 6e 64 28 26 64 75 6d 70 2c  ferAppend(&dump,
37fca 20 62 75 66 2c 20 73 74 72 6c 65 6e 28 62 75 66   buf, strlen(buf
37fcb 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ));.        }.  
37fcc 20 20 20 20 20 20 69 66 28 20 44 4c 5f 44 45 46        if( DL_DEF
37fcd 41 55 4c 54 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f  AULT==DL_POSITIO
37fce 4e 53 5f 4f 46 46 53 45 54 53 20 29 7b 0a 20 20  NS_OFFSETS ){.  
37fcf 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
37fd0 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
37fd1 62 75 66 29 2c 20 62 75 66 2c 20 22 25 64 2c 25  buf), buf, "%d,%
37fd2 64 2c 25 64 20 22 2c 0a 20 20 20 20 20 20 20 20  d,%d ",.        
37fd3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37fd4 20 20 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26     plrPosition(&
37fd5 70 6c 52 65 61 64 65 72 29 2c 0a 20 20 20 20 20  plReader),.     
37fd6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37fd7 20 20 20 20 20 20 70 6c 72 53 74 61 72 74 4f 66        plrStartOf
37fd8 66 73 65 74 28 26 70 6c 52 65 61 64 65 72 29 2c  fset(&plReader),
37fd9 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 28 26 70   plrEndOffset(&p
37fda 6c 52 65 61 64 65 72 29 29 3b 0a 20 20 20 20 20  lReader));.     
37fdb 20 20 20 7d 65 6c 73 65 20 69 66 28 20 44 4c 5f     }else if( DL_
37fdc 44 45 46 41 55 4c 54 3d 3d 44 4c 5f 50 4f 53 49  DEFAULT==DL_POSI
37fdd 54 49 4f 4e 53 20 29 7b 0a 20 20 20 20 20 20 20  TIONS ){.       
37fde 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
37fdf 6e 74 66 28 73 69 7a 65 6f 66 28 62 75 66 29 2c  ntf(sizeof(buf),
37fe0 20 62 75 66 2c 20 22 25 64 20 22 2c 20 70 6c 72   buf, "%d ", plr
37fe1 50 6f 73 69 74 69 6f 6e 28 26 70 6c 52 65 61 64  Position(&plRead
37fe2 65 72 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 65  er));.        }e
37fe3 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61  lse{.          a
37fe4 73 73 65 72 74 28 20 4e 55 4c 4c 3d 3d 22 55 6e  ssert( NULL=="Un
37fe5 68 61 6e 64 6c 65 64 20 44 4c 5f 44 45 46 41 55  handled DL_DEFAU
37fe6 4c 54 20 76 61 6c 75 65 22 29 3b 0a 20 20 20 20  LT value");.    
37fe7 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 61      }.        da
37fe8 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 26  taBufferAppend(&
37fe9 64 75 6d 70 2c 20 62 75 66 2c 20 73 74 72 6c 65  dump, buf, strle
37fea 6e 28 62 75 66 29 29 3b 0a 20 20 20 20 20 20 7d  n(buf));.      }
37feb 0a 20 20 20 20 20 20 70 6c 72 44 65 73 74 72 6f  .      plrDestro
37fec 79 28 26 70 6c 52 65 61 64 65 72 29 3b 0a 0a 20  y(&plReader);.. 
37fed 20 20 20 20 20 61 73 73 65 72 74 28 20 64 75 6d       assert( dum
37fee 70 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 20  p.nData>0 );.   
37fef 20 20 20 64 75 6d 70 2e 6e 44 61 74 61 2d 2d 3b     dump.nData--;
37ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
37ff1 20 20 20 20 20 2f 2a 20 4f 76 65 72 77 72 69 74       /* Overwrit
37ff2 65 20 74 72 61 69 6c 69 6e 67 20 73 70 61 63 65  e trailing space
37ff3 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72  . */.      asser
37ff4 74 28 20 64 75 6d 70 2e 70 44 61 74 61 5b 64 75  t( dump.pData[du
37ff5 6d 70 2e 6e 44 61 74 61 5d 3d 3d 27 20 27 29 3b  mp.nData]==' ');
37ff6 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65  .      dataBuffe
37ff7 72 41 70 70 65 6e 64 28 26 64 75 6d 70 2c 20 22  rAppend(&dump, "
37ff8 5d 5d 20 22 2c 20 33 29 3b 0a 20 20 20 20 7d 0a  ]] ", 3);.    }.
37ff9 20 20 7d 0a 20 20 64 6c 72 44 65 73 74 72 6f 79    }.  dlrDestroy
37ffa 28 26 64 6c 52 65 61 64 65 72 29 3b 0a 0a 20 20  (&dlReader);..  
37ffb 61 73 73 65 72 74 28 20 64 75 6d 70 2e 6e 44 61  assert( dump.nDa
37ffc 74 61 3e 30 20 29 3b 0a 20 20 64 75 6d 70 2e 6e  ta>0 );.  dump.n
37ffd 44 61 74 61 2d 2d 3b 20 20 20 20 20 20 20 20 20  Data--;         
37ffe 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
37fff 76 65 72 77 72 69 74 65 20 74 72 61 69 6c 69 6e  verwrite trailin
38000 67 20 73 70 61 63 65 2e 20 2a 2f 0a 20 20 61 73  g space. */.  as
38001 73 65 72 74 28 20 64 75 6d 70 2e 70 44 61 74 61  sert( dump.pData
38002 5b 64 75 6d 70 2e 6e 44 61 74 61 5d 3d 3d 27 20  [dump.nData]==' 
38003 27 29 3b 0a 20 20 64 75 6d 70 2e 70 44 61 74 61  ');.  dump.pData
38004 5b 64 75 6d 70 2e 6e 44 61 74 61 5d 20 3d 20 27  [dump.nData] = '
38005 5c 30 27 3b 0a 20 20 61 73 73 65 72 74 28 20 64  \0';.  assert( d
38006 75 6d 70 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 0a  ump.nData>0 );..
38007 20 20 2f 2a 20 50 61 73 73 65 73 20 6f 77 6e 65    /* Passes owne
38008 72 73 68 69 70 20 6f 66 20 64 75 6d 70 27 73 20  rship of dump's 
38009 62 75 66 66 65 72 20 74 6f 20 70 43 6f 6e 74 65  buffer to pConte
3800a 78 74 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  xt. */.  sqlite3
3800b 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 6f  _result_text(pCo
3800c 6e 74 65 78 74 2c 20 64 75 6d 70 2e 70 44 61 74  ntext, dump.pDat
3800d 61 2c 20 64 75 6d 70 2e 6e 44 61 74 61 2c 20 73  a, dump.nData, s
3800e 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20  qlite3_free);.  
3800f 64 75 6d 70 2e 70 44 61 74 61 20 3d 20 4e 55 4c  dump.pData = NUL
38010 4c 3b 0a 20 20 64 75 6d 70 2e 6e 44 61 74 61 20  L;.  dump.nData 
38011 3d 20 64 75 6d 70 2e 6e 43 61 70 61 63 69 74 79  = dump.nCapacity
38012 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20 49 6d 70 6c   = 0;.}../* Impl
38013 65 6d 65 6e 74 73 20 64 75 6d 70 5f 64 6f 63 6c  ements dump_docl
38014 69 73 74 28 29 20 66 6f 72 20 75 73 65 20 69 6e  ist() for use in
38015 20 69 6e 73 70 65 63 74 69 6e 67 20 74 68 65 20   inspecting the 
38016 66 74 73 33 20 69 6e 64 65 78 20 66 72 6f 6d 0a  fts3 index from.
38017 2a 2a 20 74 65 73 74 73 2e 20 20 54 45 58 54 20  ** tests.  TEXT 
38018 72 65 73 75 6c 74 20 63 6f 6e 74 61 69 6e 69 6e  result containin
38019 67 20 61 20 73 74 72 69 6e 67 20 72 65 70 72 65  g a string repre
3801a 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  sentation of the
3801b 0a 2a 2a 20 64 6f 63 6c 69 73 74 20 66 6f 72 20  .** doclist for 
3801c 74 68 65 20 69 6e 64 69 63 61 74 65 64 20 74 65  the indicated te
3801d 72 6d 2e 20 20 64 75 6d 70 5f 64 6f 63 6c 69 73  rm.  dump_doclis
3801e 74 28 74 2c 20 74 65 72 6d 2c 20 6c 65 76 65 6c  t(t, term, level
3801f 2c 20 69 64 78 29 0a 2a 2a 20 64 75 6d 70 73 20  , idx).** dumps 
38020 74 68 65 20 64 6f 63 6c 69 73 74 20 66 6f 72 20  the doclist for 
38021 74 65 72 6d 20 66 72 6f 6d 20 74 68 65 20 73 65  term from the se
38022 67 6d 65 6e 74 20 73 70 65 63 69 66 69 65 64 20  gment specified 
38023 62 79 20 6c 65 76 65 6c 2c 20 69 64 78 0a 2a 2a  by level, idx.**
38024 20 28 69 6e 20 25 5f 73 65 67 64 69 72 29 2c 20   (in %_segdir), 
38025 77 68 69 6c 65 20 64 75 6d 70 5f 64 6f 63 6c 69  while dump_docli
38026 73 74 28 74 2c 20 74 65 72 6d 29 20 64 75 6d 70  st(t, term) dump
38027 73 20 74 68 65 20 6c 6f 67 69 63 61 6c 0a 2a 2a  s the logical.**
38028 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 74 68 65   doclist for the
38029 20 74 65 72 6d 20 61 63 72 6f 73 73 20 61 6c 6c   term across all
3802a 20 73 65 67 6d 65 6e 74 73 2e 20 20 54 68 65 20   segments.  The 
3802b 70 65 72 2d 73 65 67 6d 65 6e 74 20 64 6f 63 6c  per-segment docl
3802c 69 73 74 0a 2a 2a 20 63 61 6e 20 63 6f 6e 74 61  ist.** can conta
3802d 69 6e 20 64 65 6c 65 74 69 6f 6e 73 2c 20 77 68  in deletions, wh
3802e 69 6c 65 20 74 68 65 20 66 75 6c 6c 2d 69 6e 64  ile the full-ind
3802f 65 78 20 64 6f 63 6c 69 73 74 20 77 69 6c 6c 20  ex doclist will 
38030 6e 6f 74 0a 2a 2a 20 28 64 65 6c 65 74 69 6f 6e  not.** (deletion
38031 73 20 61 72 65 20 6f 6d 69 74 74 65 64 29 2e 0a  s are omitted)..
38032 2a 2a 0a 2a 2a 20 52 65 73 75 6c 74 20 66 6f 72  **.** Result for
38033 6d 61 74 73 20 64 69 66 66 65 72 20 77 69 74 68  mats differ with
38034 20 74 68 65 20 73 65 74 74 69 6e 67 20 6f 66 20   the setting of 
38035 44 4c 5f 44 45 46 41 55 4c 54 53 2e 20 20 45 78  DL_DEFAULTS.  Ex
38036 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 44 4c  amples:.**.** DL
38037 5f 44 4f 43 49 44 53 3a 20 5b 31 5d 20 5b 33 5d  _DOCIDS: [1] [3]
38038 20 5b 37 5d 0a 2a 2a 20 44 4c 5f 50 4f 53 49 54   [7].** DL_POSIT
38039 49 4f 4e 53 3a 20 5b 31 20 30 5b 30 20 34 5d 20  IONS: [1 0[0 4] 
3803a 31 5b 31 37 5d 5d 20 5b 33 20 31 5b 35 5d 5d 0a  1[17]] [3 1[5]].
3803b 2a 2a 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f  ** DL_POSITIONS_
3803c 4f 46 46 53 45 54 53 3a 20 5b 31 20 30 5b 30 2c  OFFSETS: [1 0[0,
3803d 30 2c 33 20 34 2c 32 33 2c 32 36 5d 20 31 5b 31  0,3 4,23,26] 1[1
3803e 37 2c 31 30 32 2c 31 30 35 5d 5d 20 5b 33 20 31  7,102,105]] [3 1
3803f 5b 35 2c 32 30 2c 32 33 5d 5d 0a 2a 2a 0a 2a 2a  [5,20,23]].**.**
38040 20 49 6e 20 65 61 63 68 20 63 61 73 65 20 74 68   In each case th
38041 65 20 6e 75 6d 62 65 72 20 61 66 74 65 72 20 74  e number after t
38042 68 65 20 6f 75 74 65 72 20 27 5b 27 20 69 73 20  he outer '[' is 
38043 74 68 65 20 64 6f 63 69 64 2e 20 20 49 6e 20 74  the docid.  In t
38044 68 65 0a 2a 2a 20 6c 61 74 74 65 72 20 74 77 6f  he.** latter two
38045 20 63 61 73 65 73 2c 20 74 68 65 20 6e 75 6d 62   cases, the numb
38046 65 72 20 62 65 66 6f 72 65 20 74 68 65 20 69 6e  er before the in
38047 6e 65 72 20 27 5b 27 20 69 73 20 74 68 65 20 63  ner '[' is the c
38048 6f 6c 75 6d 6e 0a 2a 2a 20 61 73 73 6f 63 69 61  olumn.** associa
38049 74 65 64 20 77 69 74 68 20 74 68 65 20 76 61 6c  ted with the val
3804a 75 65 73 20 77 69 74 68 69 6e 2e 20 20 46 6f 72  ues within.  For
3804b 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 74 68   DL_POSITIONS th
3804c 65 20 6e 75 6d 62 65 72 73 0a 2a 2a 20 77 69 74  e numbers.** wit
3804d 68 69 6e 20 61 72 65 20 74 68 65 20 70 6f 73 69  hin are the posi
3804e 74 69 6f 6e 73 2c 20 66 6f 72 20 44 4c 5f 50 4f  tions, for DL_PO
3804f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20  SITIONS_OFFSETS 
38050 74 68 65 79 20 61 72 65 20 74 68 65 0a 2a 2a 20  they are the.** 
38051 70 6f 73 69 74 69 6f 6e 2c 20 74 68 65 20 73 74  position, the st
38052 61 72 74 20 6f 66 66 73 65 74 2c 20 61 6e 64 20  art offset, and 
38053 74 68 65 20 65 6e 64 20 6f 66 66 73 65 74 2e 0a  the end offset..
38054 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  */.static void d
38055 75 6d 70 44 6f 63 6c 69 73 74 46 75 6e 63 28 0a  umpDoclistFunc(.
38056 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
38057 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20 69  t *pContext,.  i
38058 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33  nt argc, sqlite3
38059 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
3805a 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73  .  fulltext_curs
3805b 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 69  or *pCursor;.  i
3805c 66 28 20 61 72 67 63 21 3d 32 20 26 26 20 61 72  f( argc!=2 && ar
3805d 67 63 21 3d 34 20 29 7b 0a 20 20 20 20 67 65 6e  gc!=4 ){.    gen
3805e 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74  erateError(pCont
3805f 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69  ext, "dump_docli
38060 73 74 22 2c 20 22 69 6e 63 6f 72 72 65 63 74 20  st", "incorrect 
38061 61 72 67 75 6d 65 6e 74 73 22 29 3b 0a 20 20 7d  arguments");.  }
38062 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33  else if( sqlite3
38063 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
38064 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f  [0])!=SQLITE_BLO
38065 42 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20  B ||.           
38066 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
38067 79 74 65 73 28 61 72 67 76 5b 30 5d 29 21 3d 73  ytes(argv[0])!=s
38068 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 20 29  izeof(pCursor) )
38069 7b 0a 20 20 20 20 67 65 6e 65 72 61 74 65 45 72  {.    generateEr
3806a 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64  ror(pContext, "d
3806b 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20 22 69  ump_doclist", "i
3806c 6c 6c 65 67 61 6c 20 66 69 72 73 74 20 61 72 67  llegal first arg
3806d 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 65 6c 73 65  ument");.  }else
3806e 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
3806f 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29  ue_text(argv[1])
38070 3d 3d 4e 55 4c 4c 20 7c 7c 0a 20 20 20 20 20 20  ==NULL ||.      
38071 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61        sqlite3_va
38072 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d  lue_text(argv[1]
38073 29 5b 30 5d 3d 3d 27 5c 30 27 20 29 7b 0a 20 20  )[0]=='\0' ){.  
38074 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28    generateError(
38075 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f  pContext, "dump_
38076 64 6f 63 6c 69 73 74 22 2c 20 22 65 6d 70 74 79  doclist", "empty
38077 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
38078 22 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ");.  }else{.   
38079 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65   const char *pTe
3807a 72 6d 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  rm = (const char
3807b 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
3807c 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a  _text(argv[1]);.
3807d 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 54      const int nT
3807e 65 72 6d 20 3d 20 73 74 72 6c 65 6e 28 70 54 65  erm = strlen(pTe
3807f 72 6d 29 3b 0a 20 20 20 20 66 75 6c 6c 74 65 78  rm);.    fulltex
38080 74 5f 76 74 61 62 20 2a 76 3b 0a 20 20 20 20 69  t_vtab *v;.    i
38081 6e 74 20 72 63 3b 0a 20 20 20 20 44 61 74 61 42  nt rc;.    DataB
38082 75 66 66 65 72 20 64 6f 63 6c 69 73 74 3b 0a 0a  uffer doclist;..
38083 20 20 20 20 6d 65 6d 63 70 79 28 26 70 43 75 72      memcpy(&pCur
38084 73 6f 72 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c  sor, sqlite3_val
38085 75 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29  ue_blob(argv[0])
38086 2c 20 73 69 7a 65 6f 66 28 70 43 75 72 73 6f 72  , sizeof(pCursor
38087 29 29 3b 0a 20 20 20 20 76 20 3d 20 63 75 72 73  ));.    v = curs
38088 6f 72 5f 76 74 61 62 28 70 43 75 72 73 6f 72 29  or_vtab(pCursor)
38089 3b 0a 0a 20 20 20 20 64 61 74 61 42 75 66 66 65  ;..    dataBuffe
3808a 72 49 6e 69 74 28 26 64 6f 63 6c 69 73 74 2c 20  rInit(&doclist, 
3808b 30 29 3b 0a 0a 20 20 20 20 2f 2a 20 74 65 72 6d  0);..    /* term
3808c 53 65 6c 65 63 74 28 29 20 79 69 65 6c 64 73 20  Select() yields 
3808d 74 68 65 20 73 61 6d 65 20 6c 6f 67 69 63 61 6c  the same logical
3808e 20 64 6f 63 6c 69 73 74 20 74 68 61 74 20 71 75   doclist that qu
3808f 65 72 69 65 73 20 61 72 65 0a 20 20 20 20 2a 2a  eries are.    **
38090 20 72 75 6e 20 61 67 61 69 6e 73 74 2e 0a 20 20   run against..  
38091 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 61 72 67    */.    if( arg
38092 63 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 72 63  c==2 ){.      rc
38093 20 3d 20 74 65 72 6d 53 65 6c 65 63 74 28 76 2c   = termSelect(v,
38094 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 70 54 65   v->nColumn, pTe
38095 72 6d 2c 20 6e 54 65 72 6d 2c 20 30 2c 20 44 4c  rm, nTerm, 0, DL
38096 5f 44 45 46 41 55 4c 54 2c 20 26 64 6f 63 6c 69  _DEFAULT, &docli
38097 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  st);.    }else{.
38098 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74        sqlite3_st
38099 6d 74 20 2a 73 20 3d 20 4e 55 4c 4c 3b 0a 0a 20  mt *s = NULL;.. 
3809a 20 20 20 20 20 2f 2a 20 47 65 74 20 6f 75 72 20       /* Get our 
3809b 73 70 65 63 69 66 69 63 20 73 65 67 6d 65 6e 74  specific segment
3809c 27 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20  's information. 
3809d 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  */.      rc = sq
3809e 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28  l_get_statement(
3809f 76 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54  v, SEGDIR_SELECT
380a0 5f 53 45 47 4d 45 4e 54 5f 53 54 4d 54 2c 20 26  _SEGMENT_STMT, &
380a1 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  s);.      if( rc
380a2 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
380a3 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
380a4 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20  te3_bind_int(s, 
380a5 31 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  1, sqlite3_value
380a6 5f 69 6e 74 28 61 72 67 76 5b 32 5d 29 29 3b 0a  _int(argv[2]));.
380a7 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
380a8 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
380a9 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
380aa 74 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20  te3_bind_int(s, 
380ab 32 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  2, sqlite3_value
380ac 5f 69 6e 74 28 61 72 67 76 5b 33 5d 29 29 3b 0a  _int(argv[3]));.
380ad 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
380ae 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  }..      if( rc=
380af 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
380b0 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
380b1 65 33 5f 73 74 65 70 28 73 29 3b 0a 0a 20 20 20  e3_step(s);..   
380b2 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
380b3 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20  ITE_DONE ){.    
380b4 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72        dataBuffer
380b5 44 65 73 74 72 6f 79 28 26 64 6f 63 6c 69 73 74  Destroy(&doclist
380b6 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 65 6e  );.          gen
380b7 65 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74  erateError(pCont
380b8 65 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69  ext, "dump_docli
380b9 73 74 22 2c 20 22 73 65 67 6d 65 6e 74 20 6e 6f  st", "segment no
380ba 74 20 66 6f 75 6e 64 22 29 3b 0a 20 20 20 20 20  t found");.     
380bb 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
380bc 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
380bd 2f 2a 20 46 6f 75 6e 64 20 61 20 73 65 67 6d 65  /* Found a segme
380be 6e 74 2c 20 6c 6f 61 64 20 69 74 20 69 6e 74 6f  nt, load it into
380bf 20 64 6f 63 6c 69 73 74 2e 20 2a 2f 0a 20 20 20   doclist. */.   
380c0 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
380c1 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
380c2 20 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74       const sqlit
380c3 65 5f 69 6e 74 36 34 20 69 4c 65 61 76 65 73 45  e_int64 iLeavesE
380c4 6e 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  nd = sqlite3_col
380c5 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b  umn_int64(s, 1);
380c6 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74  .          const
380c7 20 63 68 61 72 20 2a 70 44 61 74 61 20 3d 20 73   char *pData = s
380c8 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c  qlite3_column_bl
380c9 6f 62 28 73 2c 20 32 29 3b 0a 20 20 20 20 20 20  ob(s, 2);.      
380ca 20 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 44      const int nD
380cb 61 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  ata = sqlite3_co
380cc 6c 75 6d 6e 5f 62 79 74 65 73 28 73 2c 20 32 29  lumn_bytes(s, 2)
380cd 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;..          /* 
380ce 6c 6f 61 64 53 65 67 6d 65 6e 74 28 29 20 69 73  loadSegment() is
380cf 20 75 73 65 64 20 62 79 20 74 65 72 6d 53 65 6c   used by termSel
380d0 65 63 74 28 29 20 74 6f 20 6c 6f 61 64 20 65 61  ect() to load ea
380d1 63 68 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  ch.          ** 
380d2 73 65 67 6d 65 6e 74 27 73 20 64 61 74 61 2e 0a  segment's data..
380d3 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20            */.   
380d4 20 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 61 64         rc = load
380d5 53 65 67 6d 65 6e 74 28 76 2c 20 70 44 61 74 61  Segment(v, pData
380d6 2c 20 6e 44 61 74 61 2c 20 69 4c 65 61 76 65 73  , nData, iLeaves
380d7 45 6e 64 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72  End, pTerm, nTer
380d8 6d 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  m, 0,.          
380d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
380da 20 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20   &doclist);.    
380db 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
380dc 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
380dd 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
380de 74 65 33 5f 73 74 65 70 28 73 29 3b 0a 0a 20 20  te3_step(s);..  
380df 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 6f            /* Sho
380e0 75 6c 64 20 6e 6f 74 20 68 61 76 65 20 6d 6f 72  uld not have mor
380e1 65 20 74 68 61 6e 20 6f 6e 65 20 6d 61 74 63 68  e than one match
380e2 69 6e 67 20 73 65 67 6d 65 6e 74 2e 20 2a 2f 0a  ing segment. */.
380e3 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
380e4 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc!=SQLITE_DONE 
380e5 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
380e6 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 73   sqlite3_reset(s
380e7 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
380e8 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72   dataBufferDestr
380e9 6f 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20  oy(&doclist);.  
380ea 20 20 20 20 20 20 20 20 20 20 20 20 67 65 6e 65              gene
380eb 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65  rateError(pConte
380ec 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73  xt, "dump_doclis
380ed 74 22 2c 20 22 69 6e 76 61 6c 69 64 20 73 65 67  t", "invalid seg
380ee 64 69 72 22 29 3b 0a 20 20 20 20 20 20 20 20 20  dir");.         
380ef 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20       return;.   
380f0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
380f1 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
380f2 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20  TE_OK;.         
380f3 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
380f4 20 20 20 7d 0a 0a 20 20 20 20 20 20 73 71 6c 69     }..      sqli
380f5 74 65 33 5f 72 65 73 65 74 28 73 29 3b 0a 20 20  te3_reset(s);.  
380f6 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 3d    }..    if( rc=
380f7 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
380f8 20 20 20 20 69 66 28 20 64 6f 63 6c 69 73 74 2e      if( doclist.
380f9 6e 44 61 74 61 3e 30 20 29 7b 0a 20 20 20 20 20  nData>0 ){.     
380fa 20 20 20 63 72 65 61 74 65 44 6f 63 6c 69 73 74     createDoclist
380fb 52 65 73 75 6c 74 28 70 43 6f 6e 74 65 78 74 2c  Result(pContext,
380fc 20 64 6f 63 6c 69 73 74 2e 70 44 61 74 61 2c 20   doclist.pData, 
380fd 64 6f 63 6c 69 73 74 2e 6e 44 61 74 61 29 3b 0a  doclist.nData);.
380fe 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
380ff 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65       /* TODO(she
38100 73 73 29 3a 20 54 68 69 73 20 63 61 6e 20 68 61  ss): This can ha
38101 70 70 65 6e 20 69 66 20 74 68 65 20 74 65 72 6d  ppen if the term
38102 20 69 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 2c   is not present,
38103 20 6f 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 69   or.        ** i
38104 66 20 61 6c 6c 20 69 6e 73 74 61 6e 63 65 73 20  f all instances 
38105 6f 66 20 74 68 65 20 74 65 72 6d 20 68 61 76 65  of the term have
38106 20 62 65 65 6e 20 64 65 6c 65 74 65 64 20 61 6e   been deleted an
38107 64 20 74 68 69 73 20 69 73 0a 20 20 20 20 20 20  d this is.      
38108 20 20 2a 2a 20 61 6e 20 61 6c 6c 2d 69 6e 64 65    ** an all-inde
38109 78 20 64 75 6d 70 2e 20 20 49 74 20 6d 61 79 20  x dump.  It may 
3810a 62 65 20 69 6e 74 65 72 65 73 74 69 6e 67 20 74  be interesting t
3810b 6f 20 64 69 73 74 69 6e 67 75 69 73 68 0a 20 20  o distinguish.  
3810c 20 20 20 20 20 20 2a 2a 20 74 68 65 73 65 20 63        ** these c
3810d 61 73 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f  ases..        */
3810e 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
3810f 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 6f  _result_text(pCo
38110 6e 74 65 78 74 2c 20 22 22 2c 20 30 2c 20 53 51  ntext, "", 0, SQ
38111 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
38112 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20      }.    }else 
38113 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  if( rc==SQLITE_N
38114 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 2f 2a  OMEM ){.      /*
38115 20 48 61 6e 64 6c 65 20 6f 75 74 2d 6f 66 2d 6d   Handle out-of-m
38116 65 6d 6f 72 79 20 63 61 73 65 73 20 73 70 65 63  emory cases spec
38117 69 61 6c 6c 79 20 62 65 63 61 75 73 65 20 69 66  ially because if
38118 20 74 68 65 79 20 61 72 65 0a 20 20 20 20 20 20   they are.      
38119 2a 2a 20 67 65 6e 65 72 61 74 65 64 20 69 6e 20  ** generated in 
3811a 66 74 73 33 20 63 6f 64 65 20 74 68 65 79 20 6d  fts3 code they m
3811b 61 79 20 6e 6f 74 20 62 65 20 72 65 66 6c 65 63  ay not be reflec
3811c 74 65 64 20 69 6e 20 74 68 65 20 64 62 0a 20 20  ted in the db.  
3811d 20 20 20 20 2a 2a 20 68 61 6e 64 6c 65 2e 0a 20      ** handle.. 
3811e 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 2f 2a       */.      /*
3811f 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20 48 61   TODO(shess): Ha
38120 6e 64 6c 65 20 74 68 69 73 20 6d 6f 72 65 20 63  ndle this more c
38121 6f 6d 70 72 65 68 65 6e 73 69 76 65 6c 79 2e 0a  omprehensively..
38122 20 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33        ** sqlite3
38123 45 72 72 53 74 72 28 29 20 68 61 73 20 77 68 61  ErrStr() has wha
38124 74 20 49 20 6e 65 65 64 2c 20 62 75 74 20 69 73  t I need, but is
38125 20 69 6e 74 65 72 6e 61 6c 2e 0a 20 20 20 20 20   internal..     
38126 20 2a 2f 0a 20 20 20 20 20 20 67 65 6e 65 72 61   */.      genera
38127 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74  teError(pContext
38128 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22  , "dump_doclist"
38129 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79  , "out of memory
3812a 22 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ");.    }else{. 
3812b 20 20 20 20 20 67 65 6e 65 72 61 74 65 45 72 72       generateErr
3812c 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75  or(pContext, "du
3812d 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20 4e 55 4c  mp_doclist", NUL
3812e 4c 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 64  L);.    }..    d
3812f 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79  ataBufferDestroy
38130 28 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20 7d 0a  (&doclist);.  }.
38131 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
38132 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70  This routine imp
38133 6c 65 6d 65 6e 74 73 20 74 68 65 20 78 46 69 6e  lements the xFin
38134 64 46 75 6e 63 74 69 6f 6e 20 6d 65 74 68 6f 64  dFunction method
38135 20 66 6f 72 20 74 68 65 20 46 54 53 33 0a 2a 2a   for the FTS3.**
38136 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a   virtual table..
38137 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75  */.static int fu
38138 6c 6c 74 65 78 74 46 69 6e 64 46 75 6e 63 74 69  lltextFindFuncti
38139 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  on(.  sqlite3_vt
3813a 61 62 20 2a 70 56 74 61 62 2c 0a 20 20 69 6e 74  ab *pVtab,.  int
3813b 20 6e 41 72 67 2c 0a 20 20 63 6f 6e 73 74 20 63   nArg,.  const c
3813c 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 76 6f  har *zName,.  vo
3813d 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 28 73 71  id (**pxFunc)(sq
3813e 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69  lite3_context*,i
3813f 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt,sqlite3_value
38140 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 2a 2a 70 70  **),.  void **pp
38141 41 72 67 0a 29 7b 0a 20 20 69 66 28 20 73 74 72  Arg.){.  if( str
38142 63 6d 70 28 7a 4e 61 6d 65 2c 22 73 6e 69 70 70  cmp(zName,"snipp
38143 65 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2a  et")==0 ){.    *
38144 70 78 46 75 6e 63 20 3d 20 73 6e 69 70 70 65 74  pxFunc = snippet
38145 46 75 6e 63 3b 0a 20 20 20 20 72 65 74 75 72 6e  Func;.    return
38146 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   1;.  }else if( 
38147 73 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 22 6f 66  strcmp(zName,"of
38148 66 73 65 74 73 22 29 3d 3d 30 20 29 7b 0a 20 20  fsets")==0 ){.  
38149 20 20 2a 70 78 46 75 6e 63 20 3d 20 73 6e 69 70    *pxFunc = snip
3814a 70 65 74 4f 66 66 73 65 74 73 46 75 6e 63 3b 0a  petOffsetsFunc;.
3814b 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
3814c 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70  }else if( strcmp
3814d 28 7a 4e 61 6d 65 2c 22 6f 70 74 69 6d 69 7a 65  (zName,"optimize
3814e 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 78  ")==0 ){.    *px
3814f 46 75 6e 63 20 3d 20 6f 70 74 69 6d 69 7a 65 46  Func = optimizeF
38150 75 6e 63 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  unc;.    return 
38151 31 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  1;.#ifdef SQLITE
38152 5f 54 45 53 54 0a 20 20 20 20 2f 2a 20 4e 4f 54  _TEST.    /* NOT
38153 45 28 73 68 65 73 73 29 3a 20 54 68 65 73 65 20  E(shess): These 
38154 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 70 72  functions are pr
38155 65 73 65 6e 74 20 6f 6e 6c 79 20 66 6f 72 20 74  esent only for t
38156 65 73 74 69 6e 67 0a 20 20 20 20 2a 2a 20 70 75  esting.    ** pu
38157 72 70 6f 73 65 73 2e 20 20 4e 6f 20 70 61 72 74  rposes.  No part
38158 69 63 75 6c 61 72 20 65 66 66 6f 72 74 20 69 73  icular effort is
38159 20 6d 61 64 65 20 74 6f 20 6f 70 74 69 6d 69 7a   made to optimiz
3815a 65 20 74 68 65 69 72 0a 20 20 20 20 2a 2a 20 65  e their.    ** e
3815b 78 65 63 75 74 69 6f 6e 20 6f 72 20 68 6f 77 20  xecution or how 
3815c 74 68 65 79 20 62 75 69 6c 64 20 74 68 65 69 72  they build their
3815d 20 72 65 73 75 6c 74 73 2e 0a 20 20 20 20 2a 2f   results..    */
3815e 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72  .  }else if( str
3815f 63 6d 70 28 7a 4e 61 6d 65 2c 22 64 75 6d 70 5f  cmp(zName,"dump_
38160 74 65 72 6d 73 22 29 3d 3d 30 20 29 7b 0a 20 20  terms")==0 ){.  
38161 20 20 2f 2a 20 66 70 72 69 6e 74 66 28 73 74 64    /* fprintf(std
38162 65 72 72 2c 20 22 46 6f 75 6e 64 20 64 75 6d 70  err, "Found dump
38163 5f 74 65 72 6d 73 5c 6e 22 29 3b 20 2a 2f 0a 20  _terms\n"); */. 
38164 20 20 20 2a 70 78 46 75 6e 63 20 3d 20 64 75 6d     *pxFunc = dum
38165 70 54 65 72 6d 73 46 75 6e 63 3b 0a 20 20 20 20  pTermsFunc;.    
38166 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73  return 1;.  }els
38167 65 20 69 66 28 20 73 74 72 63 6d 70 28 7a 4e 61  e if( strcmp(zNa
38168 6d 65 2c 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74  me,"dump_doclist
38169 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  ")==0 ){.    /* 
3816a 66 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20  fprintf(stderr, 
3816b 22 46 6f 75 6e 64 20 64 75 6d 70 5f 64 6f 63 6c  "Found dump_docl
3816c 69 73 74 5c 6e 22 29 3b 20 2a 2f 0a 20 20 20 20  ist\n"); */.    
3816d 2a 70 78 46 75 6e 63 20 3d 20 64 75 6d 70 44 6f  *pxFunc = dumpDo
3816e 63 6c 69 73 74 46 75 6e 63 3b 0a 20 20 20 20 72  clistFunc;.    r
3816f 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a  eturn 1;.#endif.
38170 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
38171 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6e 61 6d 65 20  }../*.** Rename 
38172 61 6e 20 66 74 73 33 20 74 61 62 6c 65 2e 0a 2a  an fts3 table..*
38173 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c  /.static int ful
38174 6c 74 65 78 74 52 65 6e 61 6d 65 28 0a 20 20 73  ltextRename(.  s
38175 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
38176 61 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  ab,.  const char
38177 20 2a 7a 4e 61 6d 65 0a 29 7b 0a 20 20 66 75 6c   *zName.){.  ful
38178 6c 74 65 78 74 5f 76 74 61 62 20 2a 70 20 3d 20  ltext_vtab *p = 
38179 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
3817a 29 70 56 74 61 62 3b 0a 20 20 69 6e 74 20 72 63  )pVtab;.  int rc
3817b 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
3817c 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20  .  char *zSql = 
3817d 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
3817e 0a 20 20 20 20 22 41 4c 54 45 52 20 54 41 42 4c  .    "ALTER TABL
3817f 45 20 25 51 2e 27 25 71 5f 63 6f 6e 74 65 6e 74  E %Q.'%q_content
38180 27 20 20 52 45 4e 41 4d 45 20 54 4f 20 27 25 71  '  RENAME TO '%q
38181 5f 63 6f 6e 74 65 6e 74 27 3b 22 0a 20 20 20 20  _content';".    
38182 22 41 4c 54 45 52 20 54 41 42 4c 45 20 25 51 2e  "ALTER TABLE %Q.
38183 27 25 71 5f 73 65 67 6d 65 6e 74 73 27 20 52 45  '%q_segments' RE
38184 4e 41 4d 45 20 54 4f 20 27 25 71 5f 73 65 67 6d  NAME TO '%q_segm
38185 65 6e 74 73 27 3b 22 0a 20 20 20 20 22 41 4c 54  ents';".    "ALT
38186 45 52 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f  ER TABLE %Q.'%q_
38187 73 65 67 64 69 72 27 20 20 20 52 45 4e 41 4d 45  segdir'   RENAME
38188 20 54 4f 20 27 25 71 5f 73 65 67 64 69 72 27 3b   TO '%q_segdir';
38189 22 0a 20 20 20 20 2c 20 70 2d 3e 7a 44 62 2c 20  ".    , p->zDb, 
3818a 70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 20  p->zName, zName 
3818b 0a 20 20 20 20 2c 20 70 2d 3e 7a 44 62 2c 20 70  .    , p->zDb, p
3818c 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 20 0a  ->zName, zName .
3818d 20 20 20 20 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d      , p->zDb, p-
3818e 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 0a 20 20  >zName, zName.  
3818f 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 20 29 7b  );.  if( zSql ){
38190 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
38191 33 5f 65 78 65 63 28 70 2d 3e 64 62 2c 20 7a 53  3_exec(p->db, zS
38192 71 6c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20  ql, 0, 0, 0);.  
38193 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
38194 53 71 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  Sql);.  }.  retu
38195 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63  rn rc;.}..static
38196 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
38197 6f 64 75 6c 65 20 66 74 73 33 4d 6f 64 75 6c 65  odule fts3Module
38198 20 3d 20 7b 0a 20 20 2f 2a 20 69 56 65 72 73 69   = {.  /* iVersi
38199 6f 6e 20 20 20 20 20 20 2a 2f 20 30 2c 0a 20 20  on      */ 0,.  
3819a 2f 2a 20 78 43 72 65 61 74 65 20 20 20 20 20 20  /* xCreate      
3819b 20 2a 2f 20 66 75 6c 6c 74 65 78 74 43 72 65 61   */ fulltextCrea
3819c 74 65 2c 0a 20 20 2f 2a 20 78 43 6f 6e 6e 65 63  te,.  /* xConnec
3819d 74 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65  t      */ fullte
3819e 78 74 43 6f 6e 6e 65 63 74 2c 0a 20 20 2f 2a 20  xtConnect,.  /* 
3819f 78 42 65 73 74 49 6e 64 65 78 20 20 20 20 2a 2f  xBestIndex    */
381a0 20 66 75 6c 6c 74 65 78 74 42 65 73 74 49 6e 64   fulltextBestInd
381a1 65 78 2c 0a 20 20 2f 2a 20 78 44 69 73 63 6f 6e  ex,.  /* xDiscon
381a2 6e 65 63 74 20 20 20 2a 2f 20 66 75 6c 6c 74 65  nect   */ fullte
381a3 78 74 44 69 73 63 6f 6e 6e 65 63 74 2c 0a 20 20  xtDisconnect,.  
381a4 2f 2a 20 78 44 65 73 74 72 6f 79 20 20 20 20 20  /* xDestroy     
381a5 20 2a 2f 20 66 75 6c 6c 74 65 78 74 44 65 73 74   */ fulltextDest
381a6 72 6f 79 2c 0a 20 20 2f 2a 20 78 4f 70 65 6e 20  roy,.  /* xOpen 
381a7 20 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74          */ fullt
381a8 65 78 74 4f 70 65 6e 2c 0a 20 20 2f 2a 20 78 43  extOpen,.  /* xC
381a9 6c 6f 73 65 20 20 20 20 20 20 20 20 2a 2f 20 66  lose        */ f
381aa 75 6c 6c 74 65 78 74 43 6c 6f 73 65 2c 0a 20 20  ulltextClose,.  
381ab 2f 2a 20 78 46 69 6c 74 65 72 20 20 20 20 20 20  /* xFilter      
381ac 20 2a 2f 20 66 75 6c 6c 74 65 78 74 46 69 6c 74   */ fulltextFilt
381ad 65 72 2c 0a 20 20 2f 2a 20 78 4e 65 78 74 20 20  er,.  /* xNext  
381ae 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65         */ fullte
381af 78 74 4e 65 78 74 2c 0a 20 20 2f 2a 20 78 45 6f  xtNext,.  /* xEo
381b0 66 20 20 20 20 20 20 20 20 20 20 2a 2f 20 66 75  f          */ fu
381b1 6c 6c 74 65 78 74 45 6f 66 2c 0a 20 20 2f 2a 20  lltextEof,.  /* 
381b2 78 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20 2a 2f  xColumn       */
381b3 20 66 75 6c 6c 74 65 78 74 43 6f 6c 75 6d 6e 2c   fulltextColumn,
381b4 0a 20 20 2f 2a 20 78 52 6f 77 69 64 20 20 20 20  .  /* xRowid    
381b5 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 52      */ fulltextR
381b6 6f 77 69 64 2c 0a 20 20 2f 2a 20 78 55 70 64 61  owid,.  /* xUpda
381b7 74 65 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c  te       */ full
381b8 74 65 78 74 55 70 64 61 74 65 2c 0a 20 20 2f 2a  textUpdate,.  /*
381b9 20 78 42 65 67 69 6e 20 20 20 20 20 20 20 20 2a   xBegin        *
381ba 2f 20 66 75 6c 6c 74 65 78 74 42 65 67 69 6e 2c  / fulltextBegin,
381bb 0a 20 20 2f 2a 20 78 53 79 6e 63 20 20 20 20 20  .  /* xSync     
381bc 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 53      */ fulltextS
381bd 79 6e 63 2c 0a 20 20 2f 2a 20 78 43 6f 6d 6d 69  ync,.  /* xCommi
381be 74 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74  t       */ fullt
381bf 65 78 74 43 6f 6d 6d 69 74 2c 0a 20 20 2f 2a 20  extCommit,.  /* 
381c0 78 52 6f 6c 6c 62 61 63 6b 20 20 20 20 20 2a 2f  xRollback     */
381c1 20 66 75 6c 6c 74 65 78 74 52 6f 6c 6c 62 61 63   fulltextRollbac
381c2 6b 2c 0a 20 20 2f 2a 20 78 46 69 6e 64 46 75 6e  k,.  /* xFindFun
381c3 63 74 69 6f 6e 20 2a 2f 20 66 75 6c 6c 74 65 78  ction */ fulltex
381c4 74 46 69 6e 64 46 75 6e 63 74 69 6f 6e 2c 0a 20  tFindFunction,. 
381c5 20 2f 2a 20 78 52 65 6e 61 6d 65 20 2a 2f 20 20   /* xRename */  
381c6 20 20 20 20 20 66 75 6c 6c 74 65 78 74 52 65 6e       fulltextRen
381c7 61 6d 65 2c 0a 7d 3b 0a 0a 73 74 61 74 69 63 20  ame,.};..static 
381c8 76 6f 69 64 20 68 61 73 68 44 65 73 74 72 6f 79  void hashDestroy
381c9 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 66 74 73  (void *p){.  fts
381ca 33 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 28  3Hash *pHash = (
381cb 66 74 73 33 48 61 73 68 20 2a 29 70 3b 0a 20 20  fts3Hash *)p;.  
381cc 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 43  sqlite3Fts3HashC
381cd 6c 65 61 72 28 70 48 61 73 68 29 3b 0a 20 20 73  lear(pHash);.  s
381ce 71 6c 69 74 65 33 5f 66 72 65 65 28 70 48 61 73  qlite3_free(pHas
381cf 68 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  h);.}../*.** The
381d0 20 66 74 73 33 20 62 75 69 6c 74 2d 69 6e 20 74   fts3 built-in t
381d1 6f 6b 65 6e 69 7a 65 72 73 20 2d 20 22 73 69 6d  okenizers - "sim
381d2 70 6c 65 22 20 61 6e 64 20 22 70 6f 72 74 65 72  ple" and "porter
381d3 22 20 2d 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e  " - are implemen
381d4 74 65 64 0a 2a 2a 20 69 6e 20 66 69 6c 65 73 20  ted.** in files 
381d5 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 31 2e  fts3_tokenizer1.
381d6 63 20 61 6e 64 20 66 74 73 33 5f 70 6f 72 74 65  c and fts3_porte
381d7 72 2e 63 20 72 65 73 70 65 63 74 69 76 65 6c 79  r.c respectively
381d8 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  . The following.
381d9 2a 2a 20 74 77 6f 20 66 6f 72 77 61 72 64 20 64  ** two forward d
381da 65 63 6c 61 72 61 74 69 6f 6e 73 20 61 72 65 20  eclarations are 
381db 66 6f 72 20 66 75 6e 63 74 69 6f 6e 73 20 64 65  for functions de
381dc 63 6c 61 72 65 64 20 69 6e 20 74 68 65 73 65 20  clared in these 
381dd 66 69 6c 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f  files.** used to
381de 20 72 65 74 72 69 65 76 65 20 74 68 65 20 72 65   retrieve the re
381df 73 70 65 63 74 69 76 65 20 69 6d 70 6c 65 6d 65  spective impleme
381e0 6e 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20  ntations..**.** 
381e1 43 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 46  Calling sqlite3F
381e2 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a  ts3SimpleTokeniz
381e3 65 72 4d 6f 64 75 6c 65 28 29 20 73 65 74 73 20  erModule() sets 
381e4 74 68 65 20 76 61 6c 75 65 20 70 6f 69 6e 74 65  the value pointe
381e5 64 0a 2a 2a 20 74 6f 20 62 79 20 74 68 65 20 61  d.** to by the a
381e6 72 67 75 6d 65 6e 74 20 74 6f 20 70 6f 69 6e 74  rgument to point
381e7 20 61 20 74 68 65 20 22 73 69 6d 70 6c 65 22 20   a the "simple" 
381e8 74 6f 6b 65 6e 69 7a 65 72 20 69 6d 70 6c 65 6d  tokenizer implem
381e9 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 46 75 6e  entation..** Fun
381ea 63 74 69 6f 6e 20 2e 2e 2e 50 6f 72 74 65 72 54  ction ...PorterT
381eb 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 29  okenizerModule()
381ec 20 73 65 74 73 20 2a 70 4d 6f 64 75 6c 65 20 74   sets *pModule t
381ed 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a  o point to the.*
381ee 2a 20 70 6f 72 74 65 72 20 74 6f 6b 65 6e 69 7a  * porter tokeniz
381ef 65 72 2f 73 74 65 6d 6d 65 72 20 69 6d 70 6c 65  er/stemmer imple
381f0 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  mentation..*/.SQ
381f1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
381f2 64 20 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d  d sqlite3Fts3Sim
381f3 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75  pleTokenizerModu
381f4 6c 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  le(sqlite3_token
381f5 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73  izer_module cons
381f6 74 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 53 51  t**ppModule);.SQ
381f7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
381f8 64 20 73 71 6c 69 74 65 33 46 74 73 33 50 6f 72  d sqlite3Fts3Por
381f9 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75  terTokenizerModu
381fa 6c 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  le(sqlite3_token
381fb 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73  izer_module cons
381fc 74 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 53 51  t**ppModule);.SQ
381fd 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
381fe 64 20 73 71 6c 69 74 65 33 46 74 73 33 49 63 75  d sqlite3Fts3Icu
381ff 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28  TokenizerModule(
38200 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
38201 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a  r_module const**
38202 70 70 4d 6f 64 75 6c 65 29 3b 0a 0a 53 51 4c 49  ppModule);..SQLI
38203 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
38204 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 48 61  qlite3Fts3InitHa
38205 73 68 54 61 62 6c 65 28 73 71 6c 69 74 65 33 20  shTable(sqlite3 
38206 2a 2c 20 66 74 73 33 48 61 73 68 20 2a 2c 20 63  *, fts3Hash *, c
38207 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 0a 2f  onst char *);../
38208 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 73 65 20  *.** Initialise 
38209 74 68 65 20 66 74 73 33 20 65 78 74 65 6e 73 69  the fts3 extensi
3820a 6f 6e 2e 20 49 66 20 74 68 69 73 20 65 78 74 65  on. If this exte
3820b 6e 73 69 6f 6e 20 69 73 20 62 75 69 6c 74 20 61  nsion is built a
3820c 73 20 70 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65  s part.** of the
3820d 20 73 71 6c 69 74 65 20 6c 69 62 72 61 72 79 2c   sqlite library,
3820e 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   then this funct
3820f 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 69  ion is called di
38210 72 65 63 74 6c 79 20 62 79 0a 2a 2a 20 53 51 4c  rectly by.** SQL
38211 69 74 65 2e 20 49 66 20 66 74 73 33 20 69 73 20  ite. If fts3 is 
38212 62 75 69 6c 74 20 61 73 20 61 20 64 79 6e 61 6d  built as a dynam
38213 69 63 61 6c 6c 79 20 6c 6f 61 64 61 62 6c 65 20  ically loadable 
38214 65 78 74 65 6e 73 69 6f 6e 2c 20 74 68 69 73 0a  extension, this.
38215 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  ** function is c
38216 61 6c 6c 65 64 20 62 79 20 74 68 65 20 73 71 6c  alled by the sql
38217 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69  ite3_extension_i
38218 6e 69 74 28 29 20 65 6e 74 72 79 20 70 6f 69 6e  nit() entry poin
38219 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
3821a 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
3821b 46 74 73 33 49 6e 69 74 28 73 71 6c 69 74 65 33  Fts3Init(sqlite3
3821c 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 72 63 20   *db){.  int rc 
3821d 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 66  = SQLITE_OK;.  f
3821e 74 73 33 48 61 73 68 20 2a 70 48 61 73 68 20 3d  ts3Hash *pHash =
3821f 20 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69   0;.  const sqli
38220 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
38221 64 75 6c 65 20 2a 70 53 69 6d 70 6c 65 20 3d 20  dule *pSimple = 
38222 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  0;.  const sqlit
38223 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
38224 75 6c 65 20 2a 70 50 6f 72 74 65 72 20 3d 20 30  ule *pPorter = 0
38225 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ;.  const sqlite
38226 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
38227 6c 65 20 2a 70 49 63 75 20 3d 20 30 3b 0a 0a 20  le *pIcu = 0;.. 
38228 20 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d 70   sqlite3Fts3Simp
38229 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  leTokenizerModul
3822a 65 28 26 70 53 69 6d 70 6c 65 29 3b 0a 20 20 73  e(&pSimple);.  s
3822b 71 6c 69 74 65 33 46 74 73 33 50 6f 72 74 65 72  qlite3Fts3Porter
3822c 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28  TokenizerModule(
3822d 26 70 50 6f 72 74 65 72 29 3b 0a 23 69 66 64 65  &pPorter);.#ifde
3822e 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
3822f 49 43 55 0a 20 20 73 71 6c 69 74 65 33 46 74 73  ICU.  sqlite3Fts
38230 33 49 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  3IcuTokenizerMod
38231 75 6c 65 28 26 70 49 63 75 29 3b 0a 23 65 6e 64  ule(&pIcu);.#end
38232 69 66 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  if..  /* Allocat
38233 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 73 65  e and initialise
38234 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c 65 20   the hash-table 
38235 75 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 6f  used to store to
38236 6b 65 6e 69 7a 65 72 73 2e 20 2a 2f 0a 20 20 70  kenizers. */.  p
38237 48 61 73 68 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Hash = sqlite3_m
38238 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 66 74 73  alloc(sizeof(fts
38239 33 48 61 73 68 29 29 3b 0a 20 20 69 66 28 20 21  3Hash));.  if( !
3823a 70 48 61 73 68 20 29 7b 0a 20 20 20 20 72 63 20  pHash ){.    rc 
3823b 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
3823c 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
3823d 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 69 74  ite3Fts3HashInit
3823e 28 70 48 61 73 68 2c 20 46 54 53 33 5f 48 41 53  (pHash, FTS3_HAS
3823f 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a 20 20  H_STRING, 1);.  
38240 7d 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65  }..  /* Load the
38241 20 62 75 69 6c 74 2d 69 6e 20 74 6f 6b 65 6e 69   built-in tokeni
38242 7a 65 72 73 20 69 6e 74 6f 20 74 68 65 20 68 61  zers into the ha
38243 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66  sh table */.  if
38244 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
38245 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  ){.    if( sqlit
38246 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74  e3Fts3HashInsert
38247 28 70 48 61 73 68 2c 20 22 73 69 6d 70 6c 65 22  (pHash, "simple"
38248 2c 20 37 2c 20 28 76 6f 69 64 20 2a 29 70 53 69  , 7, (void *)pSi
38249 6d 70 6c 65 29 0a 20 20 20 20 20 7c 7c 20 73 71  mple).     || sq
3824a 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73  lite3Fts3HashIns
3824b 65 72 74 28 70 48 61 73 68 2c 20 22 70 6f 72 74  ert(pHash, "port
3824c 65 72 22 2c 20 37 2c 20 28 76 6f 69 64 20 2a 29  er", 7, (void *)
3824d 70 50 6f 72 74 65 72 29 20 0a 20 20 20 20 20 7c  pPorter) .     |
3824e 7c 20 28 70 49 63 75 20 26 26 20 73 71 6c 69 74  | (pIcu && sqlit
3824f 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74  e3Fts3HashInsert
38250 28 70 48 61 73 68 2c 20 22 69 63 75 22 2c 20 34  (pHash, "icu", 4
38251 2c 20 28 76 6f 69 64 20 2a 29 70 49 63 75 29 29  , (void *)pIcu))
38252 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 63  .    ){.      rc
38253 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
38254 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 23 69 66 64  .    }.  }..#ifd
38255 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
38256 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72   sqlite3Fts3Expr
38257 49 6e 69 74 54 65 73 74 49 6e 74 65 72 66 61 63  InitTestInterfac
38258 65 28 64 62 29 3b 0a 23 65 6e 64 69 66 0a 0a 20  e(db);.#endif.. 
38259 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 76   /* Create the v
3825a 69 72 74 75 61 6c 20 74 61 62 6c 65 20 77 72 61  irtual table wra
3825b 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68 65 20  pper around the 
3825c 68 61 73 68 2d 74 61 62 6c 65 20 61 6e 64 20 6f  hash-table and o
3825d 76 65 72 6c 6f 61 64 20 0a 20 20 2a 2a 20 74 68  verload .  ** th
3825e 65 20 74 77 6f 20 73 63 61 6c 61 72 20 66 75 6e  e two scalar fun
3825f 63 74 69 6f 6e 73 2e 20 49 66 20 74 68 69 73 20  ctions. If this 
38260 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72  is successful, r
38261 65 67 69 73 74 65 72 20 74 68 65 0a 20 20 2a 2a  egister the.  **
38262 20 6d 6f 64 75 6c 65 20 77 69 74 68 20 73 71 6c   module with sql
38263 69 74 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ite..  */.  if( 
38264 53 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 0a 20  SQLITE_OK==rc . 
38265 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d    && SQLITE_OK==
38266 28 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73  (rc = sqlite3Fts
38267 33 49 6e 69 74 48 61 73 68 54 61 62 6c 65 28 64  3InitHashTable(d
38268 62 2c 20 70 48 61 73 68 2c 20 22 66 74 73 33 5f  b, pHash, "fts3_
38269 74 6f 6b 65 6e 69 7a 65 72 22 29 29 0a 20 20 20  tokenizer")).   
3826a 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72  && SQLITE_OK==(r
3826b 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65 72  c = sqlite3_over
3826c 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62  load_function(db
3826d 2c 20 22 73 6e 69 70 70 65 74 22 2c 20 2d 31 29  , "snippet", -1)
3826e 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f  ).   && SQLITE_O
3826f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33  K==(rc = sqlite3
38270 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69  _overload_functi
38271 6f 6e 28 64 62 2c 20 22 6f 66 66 73 65 74 73 22  on(db, "offsets"
38272 2c 20 2d 31 29 29 0a 20 20 20 26 26 20 53 51 4c  , -1)).   && SQL
38273 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71  ITE_OK==(rc = sq
38274 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66  lite3_overload_f
38275 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 6f 70 74  unction(db, "opt
38276 69 6d 69 7a 65 22 2c 20 2d 31 29 29 0a 23 69 66  imize", -1)).#if
38277 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
38278 20 20 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d     && SQLITE_OK=
38279 3d 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f  =(rc = sqlite3_o
3827a 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e  verload_function
3827b 28 64 62 2c 20 22 64 75 6d 70 5f 74 65 72 6d 73  (db, "dump_terms
3827c 22 2c 20 2d 31 29 29 0a 20 20 20 26 26 20 53 51  ", -1)).   && SQ
3827d 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73  LITE_OK==(rc = s
3827e 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64 5f  qlite3_overload_
3827f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 64 75  function(db, "du
38280 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20 2d 31 29  mp_doclist", -1)
38281 29 0a 23 65 6e 64 69 66 0a 20 20 29 7b 0a 20 20  ).#endif.  ){.  
38282 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
38283 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76  _create_module_v
38284 32 28 0a 20 20 20 20 20 20 20 20 64 62 2c 20 22  2(.        db, "
38285 66 74 73 33 22 2c 20 26 66 74 73 33 4d 6f 64 75  fts3", &fts3Modu
38286 6c 65 2c 20 28 76 6f 69 64 20 2a 29 70 48 61 73  le, (void *)pHas
38287 68 2c 20 68 61 73 68 44 65 73 74 72 6f 79 0a 20  h, hashDestroy. 
38288 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20     );.  }..  /* 
38289 41 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63 63  An error has occ
3828a 75 72 72 65 64 2e 20 44 65 6c 65 74 65 20 74 68  urred. Delete th
3828b 65 20 68 61 73 68 20 74 61 62 6c 65 20 61 6e 64  e hash table and
3828c 20 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f   return the erro
3828d 72 20 63 6f 64 65 2e 20 2a 2f 0a 20 20 61 73 73  r code. */.  ass
3828e 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45 5f  ert( rc!=SQLITE_
3828f 4f 4b 20 29 3b 0a 20 20 69 66 28 20 70 48 61 73  OK );.  if( pHas
38290 68 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  h ){.    sqlite3
38291 46 74 73 33 48 61 73 68 43 6c 65 61 72 28 70 48  Fts3HashClear(pH
38292 61 73 68 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ash);.    sqlite
38293 33 5f 66 72 65 65 28 70 48 61 73 68 29 3b 0a 20  3_free(pHash);. 
38294 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
38295 7d 0a 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 43  }..#if !SQLITE_C
38296 4f 52 45 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ORE.SQLITE_API i
38297 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e  nt sqlite3_exten
38298 73 69 6f 6e 5f 69 6e 69 74 28 0a 20 20 73 71 6c  sion_init(.  sql
38299 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 63 68 61  ite3 *db, .  cha
3829a 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c 0a 20 20  r **pzErrMsg,.  
3829b 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70  const sqlite3_ap
3829c 69 5f 72 6f 75 74 69 6e 65 73 20 2a 70 41 70 69  i_routines *pApi
3829d 0a 29 7b 0a 20 20 53 51 4c 49 54 45 5f 45 58 54  .){.  SQLITE_EXT
3829e 45 4e 53 49 4f 4e 5f 49 4e 49 54 32 28 70 41 70  ENSION_INIT2(pAp
3829f 69 29 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  i).  return sqli
382a0 74 65 33 46 74 73 33 49 6e 69 74 28 64 62 29 3b  te3Fts3Init(db);
382a1 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69  .}.#endif..#endi
382a2 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
382a3 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65  LITE_CORE) || de
382a4 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
382a5 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a  BLE_FTS3) */../*
382a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
382a7 64 20 6f 66 20 66 74 73 33 2e 63 20 2a 2a 2a 2a  d of fts3.c ****
382a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
382a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
382aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
382ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
382ac 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 65 78  gin file fts3_ex
382ad 70 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  pr.c ***********
382ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
382af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
382b0 0a 2a 2a 20 32 30 30 38 20 4e 6f 76 20 32 38 0a  .** 2008 Nov 28.
382b1 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
382b2 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
382b3 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
382b4 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
382b5 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
382b6 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
382b7 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
382b8 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
382b9 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
382ba 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
382bb 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
382bc 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
382bd 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
382be 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
382bf 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
382c0 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
382c1 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
382c2 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
382c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
382c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
382c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
382c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
382c7 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d  ***.**.** This m
382c8 6f 64 75 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63  odule contains c
382c9 6f 64 65 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  ode that impleme
382ca 6e 74 73 20 61 20 70 61 72 73 65 72 20 66 6f 72  nts a parser for
382cb 20 66 74 73 33 20 71 75 65 72 79 20 73 74 72 69   fts3 query stri
382cc 6e 67 73 0a 2a 2a 20 28 74 68 65 20 72 69 67 68  ngs.** (the righ
382cd 74 2d 68 61 6e 64 20 61 72 67 75 6d 65 6e 74 20  t-hand argument 
382ce 74 6f 20 74 68 65 20 4d 41 54 43 48 20 6f 70 65  to the MATCH ope
382cf 72 61 74 6f 72 29 2e 20 42 65 63 61 75 73 65 20  rator). Because 
382d0 74 68 65 20 73 75 70 70 6f 72 74 65 64 20 0a 2a  the supported .*
382d1 2a 20 73 79 6e 74 61 78 20 69 73 20 72 65 6c 61  * syntax is rela
382d2 74 69 76 65 6c 79 20 73 69 6d 70 6c 65 2c 20 74  tively simple, t
382d3 68 65 20 77 68 6f 6c 65 20 74 6f 6b 65 6e 69 7a  he whole tokeniz
382d4 65 72 2f 70 61 72 73 65 72 20 73 79 73 74 65 6d  er/parser system
382d5 20 69 73 0a 2a 2a 20 68 61 6e 64 2d 63 6f 64 65   is.** hand-code
382d6 64 2e 20 54 68 65 20 70 75 62 6c 69 63 20 69 6e  d. The public in
382d7 74 65 72 66 61 63 65 20 74 6f 20 74 68 69 73 20  terface to this 
382d8 6d 6f 64 75 6c 65 20 69 73 20 64 65 63 6c 61 72  module is declar
382d9 65 64 20 69 6e 20 73 6f 75 72 63 65 0a 2a 2a 20  ed in source.** 
382da 63 6f 64 65 20 66 69 6c 65 20 22 66 74 73 33 5f  code file "fts3_
382db 65 78 70 72 2e 68 22 2e 0a 2a 2f 0a 23 69 66 20  expr.h"..*/.#if 
382dc 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
382dd 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64  CORE) || defined
382de 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46  (SQLITE_ENABLE_F
382df 54 53 33 29 0a 0a 2f 2a 0a 2a 2a 20 42 79 20 64  TS3)../*.** By d
382e0 65 66 61 75 6c 74 2c 20 74 68 69 73 20 6d 6f 64  efault, this mod
382e1 75 6c 65 20 70 61 72 73 65 73 20 74 68 65 20 6c  ule parses the l
382e2 65 67 61 63 79 20 73 79 6e 74 61 78 20 74 68 61  egacy syntax tha
382e3 74 20 68 61 73 20 62 65 65 6e 20 0a 2a 2a 20 74  t has been .** t
382e4 72 61 64 69 74 69 6f 6e 61 6c 6c 79 20 75 73 65  raditionally use
382e5 64 20 62 79 20 66 74 73 33 2e 20 4f 72 2c 20 69  d by fts3. Or, i
382e6 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
382e7 46 54 53 33 5f 50 41 52 45 4e 54 48 45 53 49 53  FTS3_PARENTHESIS
382e8 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65 64 2c 20  .** is defined, 
382e9 74 68 65 6e 20 69 74 20 75 73 65 73 20 74 68 65  then it uses the
382ea 20 6e 65 77 20 73 79 6e 74 61 78 2e 20 54 68 65   new syntax. The
382eb 20 64 69 66 66 65 72 65 6e 63 65 73 20 62 65 74   differences bet
382ec 77 65 65 6e 0a 2a 2a 20 74 68 65 20 6e 65 77 20  ween.** the new 
382ed 61 6e 64 20 74 68 65 20 6f 6c 64 20 73 79 6e 74  and the old synt
382ee 61 78 65 73 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20  axes are:.**.** 
382ef 20 61 29 20 54 68 65 20 6e 65 77 20 73 79 6e 74   a) The new synt
382f0 61 78 20 73 75 70 70 6f 72 74 73 20 70 61 72 65  ax supports pare
382f1 6e 74 68 65 73 69 73 2e 20 54 68 65 20 6f 6c 64  nthesis. The old
382f2 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a   does not..**.**
382f3 20 20 62 29 20 54 68 65 20 6e 65 77 20 73 79 6e    b) The new syn
382f4 74 61 78 20 73 75 70 70 6f 72 74 73 20 74 68 65  tax supports the
382f5 20 41 4e 44 20 61 6e 64 20 4e 4f 54 20 6f 70 65   AND and NOT ope
382f6 72 61 74 6f 72 73 2e 20 54 68 65 20 6f 6c 64 20  rators. The old 
382f7 64 6f 65 73 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20  does not..**.** 
382f8 20 63 29 20 54 68 65 20 6f 6c 64 20 73 79 6e 74   c) The old synt
382f9 61 78 20 73 75 70 70 6f 72 74 73 20 74 68 65 20  ax supports the 
382fa 22 2d 22 20 74 6f 6b 65 6e 20 71 75 61 6c 69 66  "-" token qualif
382fb 69 65 72 2e 20 54 68 69 73 20 69 73 20 6e 6f 74  ier. This is not
382fc 20 0a 2a 2a 20 20 20 20 20 73 75 70 70 6f 72 74   .**     support
382fd 65 64 20 62 79 20 74 68 65 20 6e 65 77 20 73 79  ed by the new sy
382fe 6e 74 61 78 20 28 69 74 20 69 73 20 72 65 70 6c  ntax (it is repl
382ff 61 63 65 64 20 62 79 20 74 68 65 20 4e 4f 54 20  aced by the NOT 
38300 6f 70 65 72 61 74 6f 72 29 2e 0a 2a 2a 0a 2a 2a  operator)..**.**
38301 20 20 64 29 20 57 68 65 6e 20 75 73 69 6e 67 20    d) When using 
38302 74 68 65 20 6f 6c 64 20 73 79 6e 74 61 78 2c 20  the old syntax, 
38303 74 68 65 20 4f 52 20 6f 70 65 72 61 74 6f 72 20  the OR operator 
38304 68 61 73 20 61 20 67 72 65 61 74 65 72 20 70 72  has a greater pr
38305 65 63 65 64 65 6e 63 65 0a 2a 2a 20 20 20 20 20  ecedence.**     
38306 74 68 61 6e 20 61 6e 20 69 6d 70 6c 69 63 69 74  than an implicit
38307 20 41 4e 44 2e 20 57 68 65 6e 20 75 73 69 6e 67   AND. When using
38308 20 74 68 65 20 6e 65 77 2c 20 62 6f 74 68 20 69   the new, both i
38309 6d 70 6c 69 63 69 74 79 20 61 6e 64 20 65 78 70  mplicity and exp
3830a 6c 69 63 69 74 0a 2a 2a 20 20 20 20 20 41 4e 44  licit.**     AND
3830b 20 6f 70 65 72 61 74 6f 72 73 20 68 61 76 65 20   operators have 
3830c 61 20 68 69 67 68 65 72 20 70 72 65 63 65 64 65  a higher precede
3830d 6e 63 65 20 74 68 61 6e 20 4f 52 2e 0a 2a 2a 0a  nce than OR..**.
3830e 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65 64 20 77  ** If compiled w
3830f 69 74 68 20 53 51 4c 49 54 45 5f 54 45 53 54 20  ith SQLITE_TEST 
38310 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68  defined, then th
38311 69 73 20 6d 6f 64 75 6c 65 20 65 78 70 6f 72 74  is module export
38312 73 20 74 68 65 0a 2a 2a 20 73 79 6d 62 6f 6c 20  s the.** symbol 
38313 22 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 74 73  "int sqlite3_fts
38314 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68  3_enable_parenth
38315 65 73 65 73 22 2e 20 53 65 74 74 69 6e 67 20 74  eses". Setting t
38316 68 69 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20  his variable.** 
38317 74 6f 20 7a 65 72 6f 20 63 61 75 73 65 73 20 74  to zero causes t
38318 68 65 20 6d 6f 64 75 6c 65 20 74 6f 20 75 73 65  he module to use
38319 20 74 68 65 20 6f 6c 64 20 73 79 6e 74 61 78 2e   the old syntax.
3831a 20 49 66 20 69 74 20 69 73 20 73 65 74 20 74 6f   If it is set to
3831b 20 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68   .** non-zero th
3831c 65 20 6e 65 77 20 73 79 6e 74 61 78 20 69 73 20  e new syntax is 
3831d 61 63 74 69 76 61 74 65 64 2e 20 54 68 69 73 20  activated. This 
3831e 69 73 20 73 6f 20 62 6f 74 68 20 73 79 6e 74 61  is so both synta
3831f 78 65 73 20 63 61 6e 0a 2a 2a 20 62 65 20 74 65  xes can.** be te
38320 73 74 65 64 20 75 73 69 6e 67 20 61 20 73 69 6e  sted using a sin
38321 67 6c 65 20 62 75 69 6c 64 20 6f 66 20 74 65 73  gle build of tes
38322 74 66 69 78 74 75 72 65 2e 0a 2a 2f 0a 23 69 66  tfixture..*/.#if
38323 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
38324 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
38325 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62  qlite3_fts3_enab
38326 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73 20 3d  le_parentheses =
38327 20 30 3b 0a 23 65 6c 73 65 0a 23 20 69 66 64 65   0;.#else.# ifde
38328 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
38329 46 54 53 33 5f 50 41 52 45 4e 54 48 45 53 49 53  FTS3_PARENTHESIS
3832a 20 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69   .#  define sqli
3832b 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f  te3_fts3_enable_
3832c 70 61 72 65 6e 74 68 65 73 65 73 20 31 0a 23 20  parentheses 1.# 
3832d 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 73  else.#  define s
3832e 71 6c 69 74 65 33 5f 66 74 73 33 5f 65 6e 61 62  qlite3_fts3_enab
3832f 6c 65 5f 70 61 72 65 6e 74 68 65 73 65 73 20 30  le_parentheses 0
38330 0a 23 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a  .# endif.#endif.
38331 0a 2f 2a 0a 2a 2a 20 44 65 66 61 75 6c 74 20 73  ./*.** Default s
38332 70 61 6e 20 66 6f 72 20 4e 45 41 52 20 6f 70 65  pan for NEAR ope
38333 72 61 74 6f 72 73 2e 0a 2a 2f 0a 23 64 65 66 69  rators..*/.#defi
38334 6e 65 20 53 51 4c 49 54 45 5f 46 54 53 33 5f 44  ne SQLITE_FTS3_D
38335 45 46 41 55 4c 54 5f 4e 45 41 52 5f 50 41 52 41  EFAULT_NEAR_PARA
38336 4d 20 31 30 0a 0a 0a 74 79 70 65 64 65 66 20 73  M 10...typedef s
38337 74 72 75 63 74 20 50 61 72 73 65 43 6f 6e 74 65  truct ParseConte
38338 78 74 20 50 61 72 73 65 43 6f 6e 74 65 78 74 3b  xt ParseContext;
38339 0a 73 74 72 75 63 74 20 50 61 72 73 65 43 6f 6e  .struct ParseCon
3833a 74 65 78 74 20 7b 0a 20 20 73 71 6c 69 74 65 33  text {.  sqlite3
3833b 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b  _tokenizer *pTok
3833c 65 6e 69 7a 65 72 3b 20 20 20 20 20 20 2f 2a 20  enizer;      /* 
3833d 54 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64 75 6c 65  Tokenizer module
3833e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
3833f 20 2a 2a 61 7a 43 6f 6c 3b 20 20 20 20 20 20 20   **azCol;       
38340 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 72            /* Arr
38341 61 79 20 6f 66 20 63 6f 6c 75 6d 6e 20 6e 61 6d  ay of column nam
38342 65 73 20 66 6f 72 20 66 74 73 33 20 74 61 62 6c  es for fts3 tabl
38343 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b  e */.  int nCol;
38344 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38345 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
38346 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
38347 69 6e 20 61 7a 43 6f 6c 5b 5d 20 2a 2f 0a 20 20  in azCol[] */.  
38348 69 6e 74 20 69 44 65 66 61 75 6c 74 43 6f 6c 3b  int iDefaultCol;
38349 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3834a 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 63      /* Default c
3834b 6f 6c 75 6d 6e 20 74 6f 20 71 75 65 72 79 20 2a  olumn to query *
3834c 2f 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  /.  sqlite3_cont
3834d 65 78 74 20 2a 70 43 74 78 3b 20 20 20 20 20 20  ext *pCtx;      
3834e 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65          /* Write
3834f 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68   error message h
38350 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 65  ere */.  int nNe
38351 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  st;             
38352 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38353 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64  Number of nested
38354 20 62 72 61 63 6b 65 74 73 20 2a 2f 0a 7d 3b 0a   brackets */.};.
38355 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
38356 74 69 6f 6e 20 69 73 20 65 71 75 69 76 61 6c 65  tion is equivale
38357 6e 74 20 74 6f 20 74 68 65 20 73 74 61 6e 64 61  nt to the standa
38358 72 64 20 69 73 73 70 61 63 65 28 29 20 66 75 6e  rd isspace() fun
38359 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 54 68  ction. .**.** Th
3835a 65 20 73 74 61 6e 64 61 72 64 20 69 73 73 70 61  e standard isspa
3835b 63 65 28 29 20 63 61 6e 20 62 65 20 61 77 6b 77  ce() can be awkw
3835c 61 72 64 20 74 6f 20 75 73 65 20 73 61 66 65 6c  ard to use safel
3835d 79 2c 20 62 65 63 61 75 73 65 20 61 6c 74 68 6f  y, because altho
3835e 75 67 68 20 69 74 0a 2a 2a 20 69 73 20 64 65 66  ugh it.** is def
3835f 69 6e 65 64 20 74 6f 20 61 63 63 65 70 74 20 61  ined to accept a
38360 6e 20 61 72 67 75 6d 65 6e 74 20 6f 66 20 74 79  n argument of ty
38361 70 65 20 69 6e 74 2c 20 69 74 73 20 62 65 68 61  pe int, its beha
38362 76 69 6f 75 72 20 77 68 65 6e 20 70 61 73 73 65  viour when passe
38363 64 0a 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 20  d.** an integer 
38364 74 68 61 74 20 66 61 6c 6c 73 20 6f 75 74 73 69  that falls outsi
38365 64 65 20 6f 66 20 74 68 65 20 72 61 6e 67 65 20  de of the range 
38366 6f 66 20 74 68 65 20 75 6e 73 69 67 6e 65 64 20  of the unsigned 
38367 63 68 61 72 20 74 79 70 65 0a 2a 2a 20 69 73 20  char type.** is 
38368 75 6e 64 65 66 69 6e 65 64 20 28 61 6e 64 20 73  undefined (and s
38369 6f 6d 65 74 69 6d 65 73 2c 20 22 75 6e 64 65 66  ometimes, "undef
3836a 69 6e 65 64 22 20 6d 65 61 6e 73 20 73 65 67 66  ined" means segf
3836b 61 75 6c 74 29 2e 20 54 68 69 73 20 77 72 61 70  ault). This wrap
3836c 70 65 72 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65  per.** is define
3836d 64 20 74 6f 20 61 63 63 65 70 74 20 61 6e 20 61  d to accept an a
3836e 72 67 75 6d 65 6e 74 20 6f 66 20 74 79 70 65 20  rgument of type 
3836f 63 68 61 72 2c 20 61 6e 64 20 61 6c 77 61 79 73  char, and always
38370 20 72 65 74 75 72 6e 73 20 30 20 66 6f 72 0a 2a   returns 0 for.*
38371 2a 20 61 6e 79 20 76 61 6c 75 65 73 20 74 68 61  * any values tha
38372 74 20 66 61 6c 6c 20 6f 75 74 73 69 64 65 20 6f  t fall outside o
38373 66 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 74  f the range of t
38374 68 65 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  he unsigned char
38375 20 74 79 70 65 20 28 69 2e 65 2e 0a 2a 2a 20 6e   type (i.e..** n
38376 65 67 61 74 69 76 65 20 76 61 6c 75 65 73 29 2e  egative values).
38377 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
38378 74 73 33 69 73 73 70 61 63 65 28 63 68 61 72 20  ts3isspace(char 
38379 63 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 63 26  c){.  return (c&
3837a 30 78 38 30 29 3d 3d 30 20 3f 20 69 73 73 70 61  0x80)==0 ? isspa
3837b 63 65 28 63 29 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a  ce(c) : 0;.}../*
3837c 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65 20  .** Extract the 
3837d 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20  next token from 
3837e 62 75 66 66 65 72 20 7a 20 28 6c 65 6e 67 74 68  buffer z (length
3837f 20 6e 29 20 75 73 69 6e 67 20 74 68 65 20 74 6f   n) using the to
38380 6b 65 6e 69 7a 65 72 0a 2a 2a 20 61 6e 64 20 6f  kenizer.** and o
38381 74 68 65 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ther information
38382 20 28 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 65   (column names e
38383 74 63 2e 29 20 69 6e 20 70 50 61 72 73 65 2e 20  tc.) in pParse. 
38384 43 72 65 61 74 65 20 61 6e 20 46 74 73 33 45 78  Create an Fts3Ex
38385 70 72 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20  pr.** structure 
38386 6f 66 20 74 79 70 65 20 46 54 53 51 55 45 52 59  of type FTSQUERY
38387 5f 50 48 52 41 53 45 20 63 6f 6e 74 61 69 6e 69  _PHRASE containi
38388 6e 67 20 61 20 70 68 72 61 73 65 20 63 6f 6e 73  ng a phrase cons
38389 69 73 74 69 6e 67 20 6f 66 20 74 68 69 73 0a 2a  isting of this.*
3838a 2a 20 73 69 6e 67 6c 65 20 74 6f 6b 65 6e 20 61  * single token a
3838b 6e 64 20 73 65 74 20 2a 70 70 45 78 70 72 20 74  nd set *ppExpr t
3838c 6f 20 70 6f 69 6e 74 20 74 6f 20 69 74 2e 20 49  o point to it. I
3838d 66 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  f the end of the
3838e 20 62 75 66 66 65 72 20 69 73 0a 2a 2a 20 72 65   buffer is.** re
3838f 61 63 68 65 64 20 62 65 66 6f 72 65 20 61 20 74  ached before a t
38390 6f 6b 65 6e 20 69 73 20 66 6f 75 6e 64 2c 20 73  oken is found, s
38391 65 74 20 2a 70 70 45 78 70 72 20 74 6f 20 7a 65  et *ppExpr to ze
38392 72 6f 2e 20 49 74 20 69 73 20 74 68 65 0a 2a 2a  ro. It is the.**
38393 20 72 65 73 70 6f 6e 73 69 62 69 6c 69 74 79 20   responsibility 
38394 6f 66 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f  of the caller to
38395 20 65 76 65 6e 74 75 61 6c 6c 79 20 64 65 61 6c   eventually deal
38396 6c 6f 63 61 74 65 20 74 68 65 20 61 6c 6c 6f 63  locate the alloc
38397 61 74 65 64 20 0a 2a 2a 20 46 74 73 33 45 78 70  ated .** Fts3Exp
38398 72 20 73 74 72 75 63 74 75 72 65 20 28 69 66 20  r structure (if 
38399 61 6e 79 29 20 62 79 20 70 61 73 73 69 6e 67 20  any) by passing 
3839a 69 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 66 72  it to sqlite3_fr
3839b 65 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75  ee()..**.** Retu
3839c 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66 20  rn SQLITE_OK if 
3839d 73 75 63 63 65 73 73 66 75 6c 2c 20 6f 72 20 53  successful, or S
3839e 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 61  QLITE_NOMEM if a
3839f 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
383a0 6f 6e 0a 2a 2a 20 66 61 69 6c 73 2e 0a 2a 2f 0a  on.** fails..*/.
383a1 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 4e 65  static int getNe
383a2 78 74 54 6f 6b 65 6e 28 0a 20 20 50 61 72 73 65  xtToken(.  Parse
383a3 43 6f 6e 74 65 78 74 20 2a 70 50 61 72 73 65 2c  Context *pParse,
383a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
383a5 20 20 20 2f 2a 20 66 74 73 33 20 71 75 65 72 79     /* fts3 query
383a6 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a   parse context *
383a7 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20  /.  int iCol,   
383a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
383a9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
383aa 61 6c 75 65 20 66 6f 72 20 46 74 73 33 50 68 72  alue for Fts3Phr
383ab 61 73 65 2e 69 43 6f 6c 75 6d 6e 20 2a 2f 0a 20  ase.iColumn */. 
383ac 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
383ad 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20  int n,          
383ae 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75           /* Inpu
383af 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 46 74  t string */.  Ft
383b0 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70 72 2c  s3Expr **ppExpr,
383b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
383b2 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 65 78        /* OUT: ex
383b3 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e  pression */.  in
383b4 74 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 20 20  t *pnConsumed   
383b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
383b6 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75        /* OUT: Nu
383b7 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 63 6f  mber of bytes co
383b8 6e 73 75 6d 65 64 20 2a 2f 0a 29 7b 0a 20 20 73  nsumed */.){.  s
383b9 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
383ba 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70   *pTokenizer = p
383bb 50 61 72 73 65 2d 3e 70 54 6f 6b 65 6e 69 7a 65  Parse->pTokenize
383bc 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  r;.  sqlite3_tok
383bd 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f  enizer_module co
383be 6e 73 74 20 2a 70 4d 6f 64 75 6c 65 20 3d 20 70  nst *pModule = p
383bf 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75  Tokenizer->pModu
383c0 6c 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  le;.  int rc;.  
383c1 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
383c2 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  r_cursor *pCurso
383c3 72 3b 0a 20 20 46 74 73 33 45 78 70 72 20 2a 70  r;.  Fts3Expr *p
383c4 52 65 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e  Ret = 0;.  int n
383c5 43 6f 6e 73 75 6d 65 64 20 3d 20 30 3b 0a 0a 20  Consumed = 0;.. 
383c6 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78   rc = pModule->x
383c7 4f 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c  Open(pTokenizer,
383c8 20 7a 2c 20 6e 2c 20 26 70 43 75 72 73 6f 72 29   z, n, &pCursor)
383c9 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
383ca 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 63 6f 6e  TE_OK ){.    con
383cb 73 74 20 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b  st char *zToken;
383cc 0a 20 20 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 2c  .    int nToken,
383cd 20 69 53 74 61 72 74 2c 20 69 45 6e 64 2c 20 69   iStart, iEnd, i
383ce 50 6f 73 69 74 69 6f 6e 3b 0a 20 20 20 20 69 6e  Position;.    in
383cf 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20  t nByte;        
383d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
383d1 20 20 20 20 20 20 20 2f 2a 20 74 6f 74 61 6c 20         /* total 
383d2 73 70 61 63 65 20 74 6f 20 61 6c 6c 6f 63 61 74  space to allocat
383d3 65 20 2a 2f 0a 0a 20 20 20 20 70 43 75 72 73 6f  e */..    pCurso
383d4 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20  r->pTokenizer = 
383d5 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 20 20  pTokenizer;.    
383d6 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 4e  rc = pModule->xN
383d7 65 78 74 28 70 43 75 72 73 6f 72 2c 20 26 7a 54  ext(pCursor, &zT
383d8 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c 20 26  oken, &nToken, &
383d9 69 53 74 61 72 74 2c 20 26 69 45 6e 64 2c 20 26  iStart, &iEnd, &
383da 69 50 6f 73 69 74 69 6f 6e 29 3b 0a 0a 20 20 20  iPosition);..   
383db 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
383dc 4f 4b 20 29 7b 0a 20 20 20 20 20 20 6e 42 79 74  OK ){.      nByt
383dd 65 20 3d 20 73 69 7a 65 6f 66 28 46 74 73 33 45  e = sizeof(Fts3E
383de 78 70 72 29 20 2b 20 73 69 7a 65 6f 66 28 46 74  xpr) + sizeof(Ft
383df 73 33 50 68 72 61 73 65 29 20 2b 20 6e 54 6f 6b  s3Phrase) + nTok
383e0 65 6e 3b 0a 20 20 20 20 20 20 70 52 65 74 20 3d  en;.      pRet =
383e1 20 28 46 74 73 33 45 78 70 72 20 2a 29 73 71 6c   (Fts3Expr *)sql
383e2 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74  ite3_malloc(nByt
383e3 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70  e);.      if( !p
383e4 52 65 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72  Ret ){.        r
383e5 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
383e6 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
383e7 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 52         memset(pR
383e8 65 74 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20  et, 0, nByte);. 
383e9 20 20 20 20 20 20 20 70 52 65 74 2d 3e 65 54 79         pRet->eTy
383ea 70 65 20 3d 20 46 54 53 51 55 45 52 59 5f 50 48  pe = FTSQUERY_PH
383eb 52 41 53 45 3b 0a 20 20 20 20 20 20 20 20 70 52  RASE;.        pR
383ec 65 74 2d 3e 70 50 68 72 61 73 65 20 3d 20 28 46  et->pPhrase = (F
383ed 74 73 33 50 68 72 61 73 65 20 2a 29 26 70 52 65  ts3Phrase *)&pRe
383ee 74 5b 31 5d 3b 0a 20 20 20 20 20 20 20 20 70 52  t[1];.        pR
383ef 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f  et->pPhrase->nTo
383f0 6b 65 6e 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ken = 1;.       
383f1 20 70 52 65 74 2d 3e 70 50 68 72 61 73 65 2d 3e   pRet->pPhrase->
383f2 69 43 6f 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a  iColumn = iCol;.
383f3 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50          pRet->pP
383f4 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d  hrase->aToken[0]
383f5 2e 6e 20 3d 20 6e 54 6f 6b 65 6e 3b 0a 20 20 20  .n = nToken;.   
383f6 20 20 20 20 20 70 52 65 74 2d 3e 70 50 68 72 61       pRet->pPhra
383f7 73 65 2d 3e 61 54 6f 6b 65 6e 5b 30 5d 2e 7a 20  se->aToken[0].z 
383f8 3d 20 28 63 68 61 72 20 2a 29 26 70 52 65 74 2d  = (char *)&pRet-
383f9 3e 70 50 68 72 61 73 65 5b 31 5d 3b 0a 20 20 20  >pPhrase[1];.   
383fa 20 20 20 20 20 6d 65 6d 63 70 79 28 70 52 65 74       memcpy(pRet
383fb 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65  ->pPhrase->aToke
383fc 6e 5b 30 5d 2e 7a 2c 20 7a 54 6f 6b 65 6e 2c 20  n[0].z, zToken, 
383fd 6e 54 6f 6b 65 6e 29 3b 0a 0a 20 20 20 20 20 20  nToken);..      
383fe 20 20 69 66 28 20 69 45 6e 64 3c 6e 20 26 26 20    if( iEnd<n && 
383ff 7a 5b 69 45 6e 64 5d 3d 3d 27 2a 27 20 29 7b 0a  z[iEnd]=='*' ){.
38400 20 20 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e            pRet->
38401 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b  pPhrase->aToken[
38402 30 5d 2e 69 73 50 72 65 66 69 78 20 3d 20 31 3b  0].isPrefix = 1;
38403 0a 20 20 20 20 20 20 20 20 20 20 69 45 6e 64 2b  .          iEnd+
38404 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  +;.        }.   
38405 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65       if( !sqlite
38406 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61  3_fts3_enable_pa
38407 72 65 6e 74 68 65 73 65 73 20 26 26 20 69 53 74  rentheses && iSt
38408 61 72 74 3e 30 20 26 26 20 7a 5b 69 53 74 61 72  art>0 && z[iStar
38409 74 2d 31 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20  t-1]=='-' ){.   
3840a 20 20 20 20 20 20 20 70 52 65 74 2d 3e 70 50 68         pRet->pPh
3840b 72 61 73 65 2d 3e 69 73 4e 6f 74 20 3d 20 31 3b  rase->isNot = 1;
3840c 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
3840d 20 7d 0a 20 20 20 20 20 20 6e 43 6f 6e 73 75 6d   }.      nConsum
3840e 65 64 20 3d 20 69 45 6e 64 3b 0a 20 20 20 20 7d  ed = iEnd;.    }
3840f 0a 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78  ..    pModule->x
38410 43 6c 6f 73 65 28 70 43 75 72 73 6f 72 29 3b 0a  Close(pCursor);.
38411 20 20 7d 0a 20 20 0a 20 20 2a 70 6e 43 6f 6e 73    }.  .  *pnCons
38412 75 6d 65 64 20 3d 20 6e 43 6f 6e 73 75 6d 65 64  umed = nConsumed
38413 3b 0a 20 20 2a 70 70 45 78 70 72 20 3d 20 70 52  ;.  *ppExpr = pR
38414 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  et;.  return rc;
38415 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 6c 61 72  .}.../*.** Enlar
38416 67 65 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ge a memory allo
38417 63 61 74 69 6f 6e 2e 20 20 49 66 20 61 6e 20 6f  cation.  If an o
38418 75 74 2d 6f 66 2d 6d 65 6d 6f 72 79 20 61 6c 6c  ut-of-memory all
38419 6f 63 61 74 69 6f 6e 20 6f 63 63 75 72 73 2c 0a  ocation occurs,.
3841a 2a 2a 20 74 68 65 6e 20 66 72 65 65 20 74 68 65  ** then free the
3841b 20 6f 6c 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e   old allocation.
3841c 0a 2a 2f 0a 76 6f 69 64 20 2a 66 74 73 33 52 65  .*/.void *fts3Re
3841d 61 6c 6c 6f 63 4f 72 46 72 65 65 28 76 6f 69 64  allocOrFree(void
3841e 20 2a 70 4f 72 69 67 2c 20 69 6e 74 20 6e 4e 65   *pOrig, int nNe
3841f 77 29 7b 0a 20 20 76 6f 69 64 20 2a 70 52 65 74  w){.  void *pRet
38420 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
38421 6f 63 28 70 4f 72 69 67 2c 20 6e 4e 65 77 29 3b  oc(pOrig, nNew);
38422 0a 20 20 69 66 28 20 21 70 52 65 74 20 29 7b 0a  .  if( !pRet ){.
38423 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
38424 28 70 4f 72 69 67 29 3b 0a 20 20 7d 0a 20 20 72  (pOrig);.  }.  r
38425 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a 2f  eturn pRet;.}../
38426 2a 0a 2a 2a 20 42 75 66 66 65 72 20 7a 49 6e 70  *.** Buffer zInp
38427 75 74 2c 20 6c 65 6e 67 74 68 20 6e 49 6e 70 75  ut, length nInpu
38428 74 2c 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  t, contains the 
38429 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 20 71 75  contents of a qu
3842a 6f 74 65 64 20 73 74 72 69 6e 67 0a 2a 2a 20 74  oted string.** t
3842b 68 61 74 20 61 70 70 65 61 72 65 64 20 61 73 20  hat appeared as 
3842c 70 61 72 74 20 6f 66 20 61 6e 20 66 74 73 33 20  part of an fts3 
3842d 71 75 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e  query expression
3842e 2e 20 4e 65 69 74 68 65 72 20 71 75 6f 74 65 20  . Neither quote 
3842f 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 69 73 20  character.** is 
38430 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20  included in the 
38431 62 75 66 66 65 72 2e 20 54 68 69 73 20 66 75 6e  buffer. This fun
38432 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73 20 74  ction attempts t
38433 6f 20 74 6f 6b 65 6e 69 7a 65 20 74 68 65 20 65  o tokenize the e
38434 6e 74 69 72 65 0a 2a 2a 20 69 6e 70 75 74 20 62  ntire.** input b
38435 75 66 66 65 72 20 61 6e 64 20 63 72 65 61 74 65  uffer and create
38436 20 61 6e 20 46 74 73 33 45 78 70 72 20 73 74 72   an Fts3Expr str
38437 75 63 74 75 72 65 20 6f 66 20 74 79 70 65 20 46  ucture of type F
38438 54 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 0a  TSQUERY_PHRASE .
38439 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ** containing th
3843a 65 20 72 65 73 75 6c 74 73 2e 0a 2a 2a 0a 2a 2a  e results..**.**
3843b 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
3843c 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74  SQLITE_OK is ret
3843d 75 72 6e 65 64 20 61 6e 64 20 2a 70 70 45 78 70  urned and *ppExp
3843e 72 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61  r set to point a
3843f 74 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74  t the.** allocat
38440 65 64 20 46 74 73 33 45 78 70 72 20 73 74 72 75  ed Fts3Expr stru
38441 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73 65  cture. Otherwise
38442 2c 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f  , either SQLITE_
38443 4e 4f 4d 45 4d 20 28 6f 75 74 20 6f 66 20 6d 65  NOMEM (out of me
38444 6d 6f 72 79 0a 2a 2a 20 65 72 72 6f 72 29 20 6f  mory.** error) o
38445 72 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 28  r SQLITE_ERROR (
38446 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 65 72 72  tokenization err
38447 6f 72 29 20 69 73 20 72 65 74 75 72 6e 65 64 20  or) is returned 
38448 61 6e 64 20 2a 70 70 45 78 70 72 20 73 65 74 0a  and *ppExpr set.
38449 2a 2a 20 74 6f 20 30 2e 0a 2a 2f 0a 73 74 61 74  ** to 0..*/.stat
3844a 69 63 20 69 6e 74 20 67 65 74 4e 65 78 74 53 74  ic int getNextSt
3844b 72 69 6e 67 28 0a 20 20 50 61 72 73 65 43 6f 6e  ring(.  ParseCon
3844c 74 65 78 74 20 2a 70 50 61 72 73 65 2c 20 20 20  text *pParse,   
3844d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3844e 2f 2a 20 66 74 73 33 20 71 75 65 72 79 20 70 61  /* fts3 query pa
3844f 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  rse context */. 
38450 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e   const char *zIn
38451 70 75 74 2c 20 69 6e 74 20 6e 49 6e 70 75 74 2c  put, int nInput,
38452 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75           /* Inpu
38453 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 46 74  t string */.  Ft
38454 73 33 45 78 70 72 20 2a 2a 70 70 45 78 70 72 20  s3Expr **ppExpr 
38455 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38456 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 65 78        /* OUT: ex
38457 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20  pression */.){. 
38458 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
38459 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d  er *pTokenizer =
3845a 20 70 50 61 72 73 65 2d 3e 70 54 6f 6b 65 6e 69   pParse->pTokeni
3845b 7a 65 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74  zer;.  sqlite3_t
3845c 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
3845d 63 6f 6e 73 74 20 2a 70 4d 6f 64 75 6c 65 20 3d  const *pModule =
3845e 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f   pTokenizer->pMo
3845f 64 75 6c 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  dule;.  int rc;.
38460 20 20 46 74 73 33 45 78 70 72 20 2a 70 20 3d 20    Fts3Expr *p = 
38461 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  0;.  sqlite3_tok
38462 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70  enizer_cursor *p
38463 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 63 68  Cursor = 0;.  ch
38464 61 72 20 2a 7a 54 65 6d 70 20 3d 20 30 3b 0a 20  ar *zTemp = 0;. 
38465 20 69 6e 74 20 6e 54 65 6d 70 20 3d 20 30 3b 0a   int nTemp = 0;.
38466 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d  .  rc = pModule-
38467 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65  >xOpen(pTokenize
38468 72 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e 70 75  r, zInput, nInpu
38469 74 2c 20 26 70 43 75 72 73 6f 72 29 3b 0a 20 20  t, &pCursor);.  
3846a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3846b 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 69 3b  K ){.    int ii;
3846c 0a 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 70 54  .    pCursor->pT
3846d 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65  okenizer = pToke
3846e 6e 69 7a 65 72 3b 0a 20 20 20 20 66 6f 72 28 69  nizer;.    for(i
3846f 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  i=0; rc==SQLITE_
38470 4f 4b 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20  OK; ii++){.     
38471 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f   const char *zTo
38472 6b 65 6e 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  ken;.      int n
38473 54 6f 6b 65 6e 2c 20 69 42 65 67 69 6e 2c 20 69  Token, iBegin, i
38474 45 6e 64 2c 20 69 50 6f 73 3b 0a 20 20 20 20 20  End, iPos;.     
38475 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78   rc = pModule->x
38476 4e 65 78 74 28 70 43 75 72 73 6f 72 2c 20 26 7a  Next(pCursor, &z
38477 54 6f 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c 20  Token, &nToken, 
38478 26 69 42 65 67 69 6e 2c 20 26 69 45 6e 64 2c 20  &iBegin, &iEnd, 
38479 26 69 50 6f 73 29 3b 0a 20 20 20 20 20 20 69 66  &iPos);.      if
3847a 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
3847b 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e  ){.        int n
3847c 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 46 74  Byte = sizeof(Ft
3847d 73 33 45 78 70 72 29 20 2b 20 73 69 7a 65 6f 66  s3Expr) + sizeof
3847e 28 46 74 73 33 50 68 72 61 73 65 29 3b 0a 20 20  (Fts3Phrase);.  
3847f 20 20 20 20 20 20 70 20 3d 20 66 74 73 33 52 65        p = fts3Re
38480 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 2c 20 6e  allocOrFree(p, n
38481 42 79 74 65 2b 69 69 2a 73 69 7a 65 6f 66 28 73  Byte+ii*sizeof(s
38482 74 72 75 63 74 20 50 68 72 61 73 65 54 6f 6b 65  truct PhraseToke
38483 6e 29 29 3b 0a 20 20 20 20 20 20 20 20 7a 54 65  n));.        zTe
38484 6d 70 20 3d 20 66 74 73 33 52 65 61 6c 6c 6f 63  mp = fts3Realloc
38485 4f 72 46 72 65 65 28 7a 54 65 6d 70 2c 20 6e 54  OrFree(zTemp, nT
38486 65 6d 70 20 2b 20 6e 54 6f 6b 65 6e 29 3b 0a 20  emp + nToken);. 
38487 20 20 20 20 20 20 20 69 66 28 20 21 70 20 7c 7c         if( !p ||
38488 20 21 7a 54 65 6d 70 20 29 7b 0a 20 20 20 20 20   !zTemp ){.     
38489 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d       goto no_mem
3848a 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
3848b 20 20 20 20 69 66 28 20 69 69 3d 3d 30 20 29 7b      if( ii==0 ){
3848c 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 73 65  .          memse
3848d 74 28 70 2c 20 30 2c 20 6e 42 79 74 65 29 3b 0a  t(p, 0, nByte);.
3848e 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50 68            p->pPh
3848f 72 61 73 65 20 3d 20 28 46 74 73 33 50 68 72 61  rase = (Fts3Phra
38490 73 65 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20  se *)&p[1];.    
38491 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 2d      }.        p-
38492 3e 70 50 68 72 61 73 65 20 3d 20 28 46 74 73 33  >pPhrase = (Fts3
38493 50 68 72 61 73 65 20 2a 29 26 70 5b 31 5d 3b 0a  Phrase *)&p[1];.
38494 20 20 20 20 20 20 20 20 70 2d 3e 70 50 68 72 61          p->pPhra
38495 73 65 2d 3e 6e 54 6f 6b 65 6e 20 3d 20 69 69 2b  se->nToken = ii+
38496 31 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 70 50  1;.        p->pP
38497 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 69 69  hrase->aToken[ii
38498 5d 2e 6e 20 3d 20 6e 54 6f 6b 65 6e 3b 0a 20 20  ].n = nToken;.  
38499 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 54        memcpy(&zT
3849a 65 6d 70 5b 6e 54 65 6d 70 5d 2c 20 7a 54 6f 6b  emp[nTemp], zTok
3849b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20  en, nToken);.   
3849c 20 20 20 20 20 6e 54 65 6d 70 20 2b 3d 20 6e 54       nTemp += nT
3849d 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 69 66  oken;.        if
3849e 28 20 69 45 6e 64 3c 6e 49 6e 70 75 74 20 26 26  ( iEnd<nInput &&
3849f 20 7a 49 6e 70 75 74 5b 69 45 6e 64 5d 3d 3d 27   zInput[iEnd]=='
384a0 2a 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  *' ){.          
384a1 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b  p->pPhrase->aTok
384a2 65 6e 5b 69 69 5d 2e 69 73 50 72 65 66 69 78 20  en[ii].isPrefix 
384a3 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  = 1;.        }el
384a4 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d  se{.          p-
384a5 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e  >pPhrase->aToken
384a6 5b 69 69 5d 2e 69 73 50 72 65 66 69 78 20 3d 20  [ii].isPrefix = 
384a7 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
384a8 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
384a9 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28  pModule->xClose(
384aa 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 70 43  pCursor);.    pC
384ab 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20 7d 0a 0a  ursor = 0;.  }..
384ac 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
384ad 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 69 6e 74  _DONE ){.    int
384ae 20 6a 6a 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a   jj;.    char *z
384af 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20 6e 4e 65  New;.    int nNe
384b0 77 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 6e  w = 0;.    int n
384b1 42 79 74 65 20 3d 20 73 69 7a 65 6f 66 28 46 74  Byte = sizeof(Ft
384b2 73 33 45 78 70 72 29 20 2b 20 73 69 7a 65 6f 66  s3Expr) + sizeof
384b3 28 46 74 73 33 50 68 72 61 73 65 29 3b 0a 20 20  (Fts3Phrase);.  
384b4 20 20 6e 42 79 74 65 20 2b 3d 20 28 70 3f 28 70    nByte += (p?(p
384b5 2d 3e 70 50 68 72 61 73 65 2d 3e 6e 54 6f 6b 65  ->pPhrase->nToke
384b6 6e 2d 31 29 3a 30 29 20 2a 20 73 69 7a 65 6f 66  n-1):0) * sizeof
384b7 28 73 74 72 75 63 74 20 50 68 72 61 73 65 54 6f  (struct PhraseTo
384b8 6b 65 6e 29 3b 0a 20 20 20 20 70 20 3d 20 66 74  ken);.    p = ft
384b9 73 33 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28  s3ReallocOrFree(
384ba 70 2c 20 6e 42 79 74 65 20 2b 20 6e 54 65 6d 70  p, nByte + nTemp
384bb 29 3b 0a 20 20 20 20 69 66 28 20 21 70 20 29 7b  );.    if( !p ){
384bc 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d  .      goto no_m
384bd 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  em;.    }.    if
384be 28 20 7a 54 65 6d 70 20 29 7b 0a 20 20 20 20 20  ( zTemp ){.     
384bf 20 7a 4e 65 77 20 3d 20 26 28 28 28 63 68 61 72   zNew = &(((char
384c0 20 2a 29 70 29 5b 6e 42 79 74 65 5d 29 3b 0a 20   *)p)[nByte]);. 
384c1 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77       memcpy(zNew
384c2 2c 20 7a 54 65 6d 70 2c 20 6e 54 65 6d 70 29 3b  , zTemp, nTemp);
384c3 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
384c4 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20 6e    memset(p, 0, n
384c5 42 79 74 65 2b 6e 54 65 6d 70 29 3b 0a 20 20 20  Byte+nTemp);.   
384c6 20 7d 0a 20 20 20 20 70 2d 3e 70 50 68 72 61 73   }.    p->pPhras
384c7 65 20 3d 20 28 46 74 73 33 50 68 72 61 73 65 20  e = (Fts3Phrase 
384c8 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20 66 6f 72  *)&p[1];.    for
384c9 28 6a 6a 3d 30 3b 20 6a 6a 3c 70 2d 3e 70 50 68  (jj=0; jj<p->pPh
384ca 72 61 73 65 2d 3e 6e 54 6f 6b 65 6e 3b 20 6a 6a  rase->nToken; jj
384cb 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 50  ++){.      p->pP
384cc 68 72 61 73 65 2d 3e 61 54 6f 6b 65 6e 5b 6a 6a  hrase->aToken[jj
384cd 5d 2e 7a 20 3d 20 26 7a 4e 65 77 5b 6e 4e 65 77  ].z = &zNew[nNew
384ce 5d 3b 0a 20 20 20 20 20 20 6e 4e 65 77 20 2b 3d  ];.      nNew +=
384cf 20 70 2d 3e 70 50 68 72 61 73 65 2d 3e 61 54 6f   p->pPhrase->aTo
384d0 6b 65 6e 5b 6a 6a 5d 2e 6e 3b 0a 20 20 20 20 7d  ken[jj].n;.    }
384d1 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
384d2 65 28 7a 54 65 6d 70 29 3b 0a 20 20 20 20 70 2d  e(zTemp);.    p-
384d3 3e 65 54 79 70 65 20 3d 20 46 54 53 51 55 45 52  >eType = FTSQUER
384d4 59 5f 50 48 52 41 53 45 3b 0a 20 20 20 20 70 2d  Y_PHRASE;.    p-
384d5 3e 70 50 68 72 61 73 65 2d 3e 69 43 6f 6c 75 6d  >pPhrase->iColum
384d6 6e 20 3d 20 70 50 61 72 73 65 2d 3e 69 44 65 66  n = pParse->iDef
384d7 61 75 6c 74 43 6f 6c 3b 0a 20 20 20 20 72 63 20  aultCol;.    rc 
384d8 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  = SQLITE_OK;.  }
384d9 0a 0a 20 20 2a 70 70 45 78 70 72 20 3d 20 70 3b  ..  *ppExpr = p;
384da 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 6e 6f  .  return rc;.no
384db 5f 6d 65 6d 3a 0a 0a 20 20 69 66 28 20 70 43 75  _mem:..  if( pCu
384dc 72 73 6f 72 20 29 7b 0a 20 20 20 20 70 4d 6f 64  rsor ){.    pMod
384dd 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43 75 72  ule->xClose(pCur
384de 73 6f 72 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  sor);.  }.  sqli
384df 74 65 33 5f 66 72 65 65 28 7a 54 65 6d 70 29 3b  te3_free(zTemp);
384e0 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
384e1 70 29 3b 0a 20 20 2a 70 70 45 78 70 72 20 3d 20  p);.  *ppExpr = 
384e2 30 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  0;.  return SQLI
384e3 54 45 5f 4e 4f 4d 45 4d 3b 0a 7d 0a 0a 2f 2a 0a  TE_NOMEM;.}../*.
384e4 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 67 65 74 4e  ** Function getN
384e5 65 78 74 4e 6f 64 65 28 29 2c 20 77 68 69 63 68  extNode(), which
384e6 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 66 74   is called by ft
384e7 73 33 45 78 70 72 50 61 72 73 65 28 29 2c 20 6d  s3ExprParse(), m
384e8 61 79 20 69 74 73 65 6c 66 0a 2a 2a 20 63 61 6c  ay itself.** cal
384e9 6c 20 66 74 73 33 45 78 70 72 50 61 72 73 65 28  l fts3ExprParse(
384ea 29 2e 20 53 6f 20 74 68 69 73 20 66 6f 72 77 61  ). So this forwa
384eb 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 69  rd declaration i
384ec 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73  s required..*/.s
384ed 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 45 78  tatic int fts3Ex
384ee 70 72 50 61 72 73 65 28 50 61 72 73 65 43 6f 6e  prParse(ParseCon
384ef 74 65 78 74 20 2a 2c 20 63 6f 6e 73 74 20 63 68  text *, const ch
384f0 61 72 20 2a 2c 20 69 6e 74 2c 20 46 74 73 33 45  ar *, int, Fts3E
384f1 78 70 72 20 2a 2a 2c 20 69 6e 74 20 2a 29 3b 0a  xpr **, int *);.
384f2 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6f 75 74 70 75  ./*.** The outpu
384f3 74 20 76 61 72 69 61 62 6c 65 20 2a 70 70 45 78  t variable *ppEx
384f4 70 72 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20  pr is populated 
384f5 77 69 74 68 20 61 6e 20 61 6c 6c 6f 63 61 74 65  with an allocate
384f6 64 20 46 74 73 33 45 78 70 72 20 0a 2a 2a 20 73  d Fts3Expr .** s
384f7 74 72 75 63 74 75 72 65 2c 20 6f 72 20 73 65 74  tructure, or set
384f8 20 74 6f 20 30 20 69 66 20 74 68 65 20 65 6e 64   to 0 if the end
384f9 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 62 75   of the input bu
384fa 66 66 65 72 20 69 73 20 72 65 61 63 68 65 64 2e  ffer is reached.
384fb 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 73 20 61  .**.** Returns a
384fc 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
384fd 6f 64 65 2e 20 53 51 4c 49 54 45 5f 4f 4b 20 69  ode. SQLITE_OK i
384fe 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f 72  f everything wor
384ff 6b 73 2c 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  ks, SQLITE_NOMEM
38500 0a 2a 2a 20 69 66 20 61 20 6d 61 6c 6c 6f 63 20  .** if a malloc 
38501 66 61 69 6c 75 72 65 20 6f 63 63 75 72 73 2c 20  failure occurs, 
38502 6f 72 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  or SQLITE_ERROR 
38503 69 66 20 61 20 70 61 72 73 65 20 65 72 72 6f 72  if a parse error
38504 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e   is encountered.
38505 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 45 52  .** If SQLITE_ER
38506 52 4f 52 20 69 73 20 72 65 74 75 72 6e 65 64 2c  ROR is returned,
38507 20 70 43 6f 6e 74 65 78 74 20 69 73 20 70 6f 70   pContext is pop
38508 75 6c 61 74 65 64 20 77 69 74 68 20 61 6e 20 65  ulated with an e
38509 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f  rror message..*/
3850a 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 4e  .static int getN
3850b 65 78 74 4e 6f 64 65 28 0a 20 20 50 61 72 73 65  extNode(.  Parse
3850c 43 6f 6e 74 65 78 74 20 2a 70 50 61 72 73 65 2c  Context *pParse,
3850d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3850e 20 20 20 2f 2a 20 66 74 73 33 20 71 75 65 72 79     /* fts3 query
3850f 20 70 61 72 73 65 20 63 6f 6e 74 65 78 74 20 2a   parse context *
38510 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
38511 7a 2c 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20  z, int n,       
38512 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
38513 6e 70 75 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20  nput string */. 
38514 20 46 74 73 33 45 78 70 72 20 2a 2a 70 70 45 78   Fts3Expr **ppEx
38515 70 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  pr,             
38516 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
38517 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
38518 20 69 6e 74 20 2a 70 6e 43 6f 6e 73 75 6d 65 64   int *pnConsumed
38519 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3851a 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
3851b 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
3851c 20 63 6f 6e 73 75 6d 65 64 20 2a 2f 0a 29 7b 0a   consumed */.){.
3851d 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
3851e 74 72 75 63 74 20 46 74 73 33 4b 65 79 77 6f 72  truct Fts3Keywor
3851f 64 20 7b 0a 20 20 20 20 63 68 61 72 20 7a 5b 34  d {.    char z[4
38520 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ];              
38521 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
38522 20 4b 65 79 77 6f 72 64 20 74 65 78 74 20 2a 2f   Keyword text */
38523 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68  .    unsigned ch
38524 61 72 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20  ar n;           
38525 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65             /* Le
38526 6e 67 74 68 20 6f 66 20 74 68 65 20 6b 65 79 77  ngth of the keyw
38527 6f 72 64 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67  ord */.    unsig
38528 6e 65 64 20 63 68 61 72 20 70 61 72 65 6e 4f 6e  ned char parenOn
38529 6c 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ly;             
3852a 20 2f 2a 20 4f 6e 6c 79 20 76 61 6c 69 64 20 69   /* Only valid i
3852b 6e 20 70 61 72 65 6e 20 6d 6f 64 65 20 2a 2f 0a  n paren mode */.
3852c 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
3852d 72 20 65 54 79 70 65 3b 20 20 20 20 20 20 20 20  r eType;        
3852e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79            /* Key
3852f 77 6f 72 64 20 63 6f 64 65 20 2a 2f 0a 20 20 7d  word code */.  }
38530 20 61 4b 65 79 77 6f 72 64 5b 5d 20 3d 20 7b 0a   aKeyword[] = {.
38531 20 20 20 20 7b 20 22 4f 52 22 20 2c 20 20 32 2c      { "OR" ,  2,
38532 20 30 2c 20 46 54 53 51 55 45 52 59 5f 4f 52 20   0, FTSQUERY_OR 
38533 20 20 7d 2c 0a 20 20 20 20 7b 20 22 41 4e 44 22    },.    { "AND"
38534 2c 20 20 33 2c 20 31 2c 20 46 54 53 51 55 45 52  ,  3, 1, FTSQUER
38535 59 5f 41 4e 44 20 20 7d 2c 0a 20 20 20 20 7b 20  Y_AND  },.    { 
38536 22 4e 4f 54 22 2c 20 20 33 2c 20 31 2c 20 46 54  "NOT",  3, 1, FT
38537 53 51 55 45 52 59 5f 4e 4f 54 20 20 7d 2c 0a 20  SQUERY_NOT  },. 
38538 20 20 20 7b 20 22 4e 45 41 52 22 2c 20 34 2c 20     { "NEAR", 4, 
38539 30 2c 20 46 54 53 51 55 45 52 59 5f 4e 45 41 52  0, FTSQUERY_NEAR
3853a 20 7d 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 69   }.  };.  int ii
3853b 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20  ;.  int iCol;.  
3853c 69 6e 74 20 69 43 6f 6c 4c 65 6e 3b 0a 20 20 69  int iColLen;.  i
3853d 6e 74 20 72 63 3b 0a 20 20 46 74 73 33 45 78 70  nt rc;.  Fts3Exp
3853e 72 20 2a 70 52 65 74 20 3d 20 30 3b 0a 0a 20 20  r *pRet = 0;..  
3853f 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70  const char *zInp
38540 75 74 20 3d 20 7a 3b 0a 20 20 69 6e 74 20 6e 49  ut = z;.  int nI
38541 6e 70 75 74 20 3d 20 6e 3b 0a 0a 20 20 2f 2a 20  nput = n;..  /* 
38542 53 6b 69 70 20 6f 76 65 72 20 61 6e 79 20 77 68  Skip over any wh
38543 69 74 65 73 70 61 63 65 20 62 65 66 6f 72 65 20  itespace before 
38544 63 68 65 63 6b 69 6e 67 20 66 6f 72 20 61 20 6b  checking for a k
38545 65 79 77 6f 72 64 2c 20 61 6e 20 6f 70 65 6e 20  eyword, an open 
38546 6f 72 0a 20 20 2a 2a 20 63 6c 6f 73 65 20 62 72  or.  ** close br
38547 61 63 6b 65 74 2c 20 6f 72 20 61 20 71 75 6f 74  acket, or a quot
38548 65 64 20 73 74 72 69 6e 67 2e 20 0a 20 20 2a 2f  ed string. .  */
38549 0a 20 20 77 68 69 6c 65 28 20 6e 49 6e 70 75 74  .  while( nInput
3854a 3e 30 20 26 26 20 66 74 73 33 69 73 73 70 61 63  >0 && fts3isspac
3854b 65 28 2a 7a 49 6e 70 75 74 29 20 29 7b 0a 20 20  e(*zInput) ){.  
3854c 20 20 6e 49 6e 70 75 74 2d 2d 3b 0a 20 20 20 20    nInput--;.    
3854d 7a 49 6e 70 75 74 2b 2b 3b 0a 20 20 7d 0a 20 20  zInput++;.  }.  
3854e 69 66 28 20 6e 49 6e 70 75 74 3d 3d 30 20 29 7b  if( nInput==0 ){
3854f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
38550 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 0a 20 20  TE_DONE;.  }..  
38551 2f 2a 20 53 65 65 20 69 66 20 77 65 20 61 72 65  /* See if we are
38552 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20   dealing with a 
38553 6b 65 79 77 6f 72 64 2e 20 2a 2f 0a 20 20 66 6f  keyword. */.  fo
38554 72 28 69 69 3d 30 3b 20 69 69 3c 28 69 6e 74 29  r(ii=0; ii<(int)
38555 28 73 69 7a 65 6f 66 28 61 4b 65 79 77 6f 72 64  (sizeof(aKeyword
38556 29 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20  )/sizeof(struct 
38557 46 74 73 33 4b 65 79 77 6f 72 64 29 29 3b 20 69  Fts3Keyword)); i
38558 69 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  i++){.    const 
38559 73 74 72 75 63 74 20 46 74 73 33 4b 65 79 77 6f  struct Fts3Keywo
3855a 72 64 20 2a 70 4b 65 79 20 3d 20 26 61 4b 65 79  rd *pKey = &aKey
3855b 77 6f 72 64 5b 69 69 5d 3b 0a 0a 20 20 20 20 69  word[ii];..    i
3855c 66 28 20 28 70 4b 65 79 2d 3e 70 61 72 65 6e 4f  f( (pKey->parenO
3855d 6e 6c 79 20 26 20 7e 73 71 6c 69 74 65 33 5f 66  nly & ~sqlite3_f
3855e 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e  ts3_enable_paren
3855f 74 68 65 73 65 73 29 21 3d 30 20 29 7b 0a 20 20  theses)!=0 ){.  
38560 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20      continue;.  
38561 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 6e 49 6e    }..    if( nIn
38562 70 75 74 3e 3d 70 4b 65 79 2d 3e 6e 20 26 26 20  put>=pKey->n && 
38563 30 3d 3d 6d 65 6d 63 6d 70 28 7a 49 6e 70 75 74  0==memcmp(zInput
38564 2c 20 70 4b 65 79 2d 3e 7a 2c 20 70 4b 65 79 2d  , pKey->z, pKey-
38565 3e 6e 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  >n) ){.      int
38566 20 6e 4e 65 61 72 20 3d 20 53 51 4c 49 54 45 5f   nNear = SQLITE_
38567 46 54 53 33 5f 44 45 46 41 55 4c 54 5f 4e 45 41  FTS3_DEFAULT_NEA
38568 52 5f 50 41 52 41 4d 3b 0a 20 20 20 20 20 20 69  R_PARAM;.      i
38569 6e 74 20 6e 4b 65 79 20 3d 20 70 4b 65 79 2d 3e  nt nKey = pKey->
3856a 6e 3b 0a 20 20 20 20 20 20 63 68 61 72 20 63 4e  n;.      char cN
3856b 65 78 74 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49  ext;..      /* I
3856c 66 20 74 68 69 73 20 69 73 20 61 20 22 4e 45 41  f this is a "NEA
3856d 52 22 20 6b 65 79 77 6f 72 64 2c 20 63 68 65 63  R" keyword, chec
3856e 6b 20 66 6f 72 20 61 6e 20 65 78 70 6c 69 63 69  k for an explici
3856f 74 20 6e 65 61 72 6e 65 73 73 2e 20 2a 2f 0a 20  t nearness. */. 
38570 20 20 20 20 20 69 66 28 20 70 4b 65 79 2d 3e 65       if( pKey->e
38571 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e  Type==FTSQUERY_N
38572 45 41 52 20 29 7b 0a 20 20 20 20 20 20 20 20 61  EAR ){.        a
38573 73 73 65 72 74 28 20 6e 4b 65 79 3d 3d 34 20 29  ssert( nKey==4 )
38574 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 49  ;.        if( zI
38575 6e 70 75 74 5b 34 5d 3d 3d 27 2f 27 20 26 26 20  nput[4]=='/' && 
38576 7a 49 6e 70 75 74 5b 35 5d 3e 3d 27 30 27 20 26  zInput[5]>='0' &
38577 26 20 7a 49 6e 70 75 74 5b 35 5d 3c 3d 27 39 27  & zInput[5]<='9'
38578 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 4e   ){.          nN
38579 65 61 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ear = 0;.       
3857a 20 20 20 66 6f 72 28 6e 4b 65 79 3d 35 3b 20 7a     for(nKey=5; z
3857b 49 6e 70 75 74 5b 6e 4b 65 79 5d 3e 3d 27 30 27  Input[nKey]>='0'
3857c 20 26 26 20 7a 49 6e 70 75 74 5b 6e 4b 65 79 5d   && zInput[nKey]
3857d 3c 3d 27 39 27 3b 20 6e 4b 65 79 2b 2b 29 7b 0a  <='9'; nKey++){.
3857e 20 20 20 20 20 20 20 20 20 20 20 20 6e 4e 65 61              nNea
3857f 72 20 3d 20 6e 4e 65 61 72 20 2a 20 31 30 20 2b  r = nNear * 10 +
38580 20 28 7a 49 6e 70 75 74 5b 6e 4b 65 79 5d 20 2d   (zInput[nKey] -
38581 20 27 30 27 29 3b 0a 20 20 20 20 20 20 20 20 20   '0');.         
38582 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
38583 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 41     }..      /* A
38584 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 69  t this point thi
38585 73 20 69 73 20 70 72 6f 62 61 62 6c 79 20 61 20  s is probably a 
38586 6b 65 79 77 6f 72 64 2e 20 42 75 74 20 66 6f 72  keyword. But for
38587 20 74 68 61 74 20 74 6f 20 62 65 20 74 72 75 65   that to be true
38588 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e  ,.      ** the n
38589 65 78 74 20 62 79 74 65 20 6d 75 73 74 20 63 6f  ext byte must co
3858a 6e 74 61 69 6e 20 65 69 74 68 65 72 20 77 68 69  ntain either whi
3858b 74 65 73 70 61 63 65 2c 20 61 6e 20 6f 70 65 6e  tespace, an open
3858c 20 6f 72 20 63 6c 6f 73 65 0a 20 20 20 20 20 20   or close.      
3858d 2a 2a 20 70 61 72 65 6e 74 68 65 73 69 73 2c 20  ** parenthesis, 
3858e 61 20 71 75 6f 74 65 20 63 68 61 72 61 63 74 65  a quote characte
3858f 72 2c 20 6f 72 20 45 4f 46 2e 20 0a 20 20 20 20  r, or EOF. .    
38590 20 20 2a 2f 0a 20 20 20 20 20 20 63 4e 65 78 74    */.      cNext
38591 20 3d 20 7a 49 6e 70 75 74 5b 6e 4b 65 79 5d 3b   = zInput[nKey];
38592 0a 20 20 20 20 20 20 69 66 28 20 66 74 73 33 69  .      if( fts3i
38593 73 73 70 61 63 65 28 63 4e 65 78 74 29 20 0a 20  sspace(cNext) . 
38594 20 20 20 20 20 20 7c 7c 20 63 4e 65 78 74 3d 3d        || cNext==
38595 27 22 27 20 7c 7c 20 63 4e 65 78 74 3d 3d 27 28  '"' || cNext=='(
38596 27 20 7c 7c 20 63 4e 65 78 74 3d 3d 27 29 27 20  ' || cNext==')' 
38597 7c 7c 20 63 4e 65 78 74 3d 3d 30 0a 20 20 20 20  || cNext==0.    
38598 20 20 29 7b 0a 20 20 20 20 20 20 20 20 70 52 65    ){.        pRe
38599 74 20 3d 20 28 46 74 73 33 45 78 70 72 20 2a 29  t = (Fts3Expr *)
3859a 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
3859b 69 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29 29  izeof(Fts3Expr))
3859c 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74  ;.        memset
3859d 28 70 52 65 74 2c 20 30 2c 20 73 69 7a 65 6f 66  (pRet, 0, sizeof
3859e 28 46 74 73 33 45 78 70 72 29 29 3b 0a 20 20 20  (Fts3Expr));.   
3859f 20 20 20 20 20 70 52 65 74 2d 3e 65 54 79 70 65       pRet->eType
385a0 20 3d 20 70 4b 65 79 2d 3e 65 54 79 70 65 3b 0a   = pKey->eType;.
385a1 20 20 20 20 20 20 20 20 70 52 65 74 2d 3e 6e 4e          pRet->nN
385a2 65 61 72 20 3d 20 6e 4e 65 61 72 3b 0a 20 20 20  ear = nNear;.   
385a3 20 20 20 20 20 2a 70 70 45 78 70 72 20 3d 20 70       *ppExpr = p
385a4 52 65 74 3b 0a 20 20 20 20 20 20 20 20 2a 70 6e  Ret;.        *pn
385a5 43 6f 6e 73 75 6d 65 64 20 3d 20 28 7a 49 6e 70  Consumed = (zInp
385a6 75 74 20 2d 20 7a 29 20 2b 20 6e 4b 65 79 3b 0a  ut - z) + nKey;.
385a7 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
385a8 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20  QLITE_OK;.      
385a9 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 54 75 72 6e  }..      /* Turn
385aa 73 20 6f 75 74 20 74 68 61 74 20 77 61 73 6e 27  s out that wasn'
385ab 74 20 61 20 6b 65 79 77 6f 72 64 20 61 66 74 65  t a keyword afte
385ac 72 20 61 6c 6c 2e 20 54 68 69 73 20 68 61 70 70  r all. This happ
385ad 65 6e 73 20 69 66 20 74 68 65 0a 20 20 20 20 20  ens if the.     
385ae 20 2a 2a 20 75 73 65 72 20 68 61 73 20 73 75 70   ** user has sup
385af 70 6c 69 65 64 20 61 20 74 6f 6b 65 6e 20 73 75  plied a token su
385b0 63 68 20 61 73 20 22 4f 52 61 63 6c 65 22 2e 20  ch as "ORacle". 
385b1 43 6f 6e 74 69 6e 75 65 2e 0a 20 20 20 20 20 20  Continue..      
385b2 2a 2f 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  */.    }.  }..  
385b3 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 61 6e 20  /* Check for an 
385b4 6f 70 65 6e 20 62 72 61 63 6b 65 74 2e 20 2a 2f  open bracket. */
385b5 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 66  .  if( sqlite3_f
385b6 74 73 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e  ts3_enable_paren
385b7 74 68 65 73 65 73 20 29 7b 0a 20 20 20 20 69 66  theses ){.    if
385b8 28 20 2a 7a 49 6e 70 75 74 3d 3d 27 28 27 20 29  ( *zInput=='(' )
385b9 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 43 6f 6e  {.      int nCon
385ba 73 75 6d 65 64 3b 0a 20 20 20 20 20 20 69 6e 74  sumed;.      int
385bb 20 72 63 3b 0a 20 20 20 20 20 20 70 50 61 72 73   rc;.      pPars
385bc 65 2d 3e 6e 4e 65 73 74 2b 2b 3b 0a 20 20 20 20  e->nNest++;.    
385bd 20 20 72 63 20 3d 20 66 74 73 33 45 78 70 72 50    rc = fts3ExprP
385be 61 72 73 65 28 70 50 61 72 73 65 2c 20 26 7a 49  arse(pParse, &zI
385bf 6e 70 75 74 5b 31 5d 2c 20 6e 49 6e 70 75 74 2d  nput[1], nInput-
385c0 31 2c 20 70 70 45 78 70 72 2c 20 26 6e 43 6f 6e  1, ppExpr, &nCon
385c1 73 75 6d 65 64 29 3b 0a 20 20 20 20 20 20 69 66  sumed);.      if
385c2 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
385c3 26 26 20 21 2a 70 70 45 78 70 72 20 29 7b 0a 20  && !*ppExpr ){. 
385c4 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
385c5 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 20 20 7d  TE_DONE;.      }
385c6 0a 20 20 20 20 20 20 2a 70 6e 43 6f 6e 73 75 6d  .      *pnConsum
385c7 65 64 20 3d 20 28 7a 49 6e 70 75 74 20 2d 20 7a  ed = (zInput - z
385c8 29 20 2b 20 31 20 2b 20 6e 43 6f 6e 73 75 6d 65  ) + 1 + nConsume
385c9 64 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  d;.      return 
385ca 72 63 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20  rc;.    }.  .   
385cb 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 61 20   /* Check for a 
385cc 63 6c 6f 73 65 20 62 72 61 63 6b 65 74 2e 20 2a  close bracket. *
385cd 2f 0a 20 20 20 20 69 66 28 20 2a 7a 49 6e 70 75  /.    if( *zInpu
385ce 74 3d 3d 27 29 27 20 29 7b 0a 20 20 20 20 20 20  t==')' ){.      
385cf 70 50 61 72 73 65 2d 3e 6e 4e 65 73 74 2d 2d 3b  pParse->nNest--;
385d0 0a 20 20 20 20 20 20 2a 70 6e 43 6f 6e 73 75 6d  .      *pnConsum
385d1 65 64 20 3d 20 28 7a 49 6e 70 75 74 20 2d 20 7a  ed = (zInput - z
385d2 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 72 65 74  ) + 1;.      ret
385d3 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  urn SQLITE_DONE;
385d4 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
385d5 20 53 65 65 20 69 66 20 77 65 20 61 72 65 20 64   See if we are d
385d6 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20 71 75  ealing with a qu
385d7 6f 74 65 64 20 70 68 72 61 73 65 2e 20 49 66 20  oted phrase. If 
385d8 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73 65  this is the case
385d9 2c 20 74 68 65 6e 0a 20 20 2a 2a 20 73 65 61 72  , then.  ** sear
385da 63 68 20 66 6f 72 20 74 68 65 20 63 6c 6f 73 69  ch for the closi
385db 6e 67 20 71 75 6f 74 65 20 61 6e 64 20 70 61 73  ng quote and pas
385dc 73 20 74 68 65 20 77 68 6f 6c 65 20 73 74 72 69  s the whole stri
385dd 6e 67 20 74 6f 20 67 65 74 4e 65 78 74 53 74 72  ng to getNextStr
385de 69 6e 67 28 29 0a 20 20 2a 2a 20 66 6f 72 20 70  ing().  ** for p
385df 72 6f 63 65 73 73 69 6e 67 2e 20 54 68 69 73 20  rocessing. This 
385e0 69 73 20 65 61 73 79 20 74 6f 20 64 6f 2c 20 61  is easy to do, a
385e1 73 20 66 74 73 33 20 68 61 73 20 6e 6f 20 73 79  s fts3 has no sy
385e2 6e 74 61 78 20 66 6f 72 20 65 73 63 61 70 69 6e  ntax for escapin
385e3 67 0a 20 20 2a 2a 20 61 20 71 75 6f 74 65 20 63  g.  ** a quote c
385e4 68 61 72 61 63 74 65 72 20 65 6d 62 65 64 64 65  haracter embedde
385e5 64 20 69 6e 20 61 20 73 74 72 69 6e 67 2e 0a 20  d in a string.. 
385e6 20 2a 2f 0a 20 20 69 66 28 20 2a 7a 49 6e 70 75   */.  if( *zInpu
385e7 74 3d 3d 27 22 27 20 29 7b 0a 20 20 20 20 66 6f  t=='"' ){.    fo
385e8 72 28 69 69 3d 31 3b 20 69 69 3c 6e 49 6e 70 75  r(ii=1; ii<nInpu
385e9 74 20 26 26 20 7a 49 6e 70 75 74 5b 69 69 5d 21  t && zInput[ii]!
385ea 3d 27 22 27 3b 20 69 69 2b 2b 29 3b 0a 20 20 20  ='"'; ii++);.   
385eb 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 3d 20 28   *pnConsumed = (
385ec 7a 49 6e 70 75 74 20 2d 20 7a 29 20 2b 20 69 69  zInput - z) + ii
385ed 20 2b 20 31 3b 0a 20 20 20 20 69 66 28 20 69 69   + 1;.    if( ii
385ee 3d 3d 6e 49 6e 70 75 74 20 29 7b 0a 20 20 20 20  ==nInput ){.    
385ef 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
385f0 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20  ERROR;.    }.   
385f1 20 72 65 74 75 72 6e 20 67 65 74 4e 65 78 74 53   return getNextS
385f2 74 72 69 6e 67 28 70 50 61 72 73 65 2c 20 26 7a  tring(pParse, &z
385f3 49 6e 70 75 74 5b 31 5d 2c 20 69 69 2d 31 2c 20  Input[1], ii-1, 
385f4 70 70 45 78 70 72 29 3b 0a 20 20 7d 0a 0a 0a 20  ppExpr);.  }... 
385f5 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20 66   /* If control f
385f6 6c 6f 77 73 20 74 6f 20 74 68 69 73 20 70 6f 69  lows to this poi
385f7 6e 74 2c 20 74 68 69 73 20 6d 75 73 74 20 62 65  nt, this must be
385f8 20 61 20 72 65 67 75 6c 61 72 20 74 6f 6b 65 6e   a regular token
385f9 2c 20 6f 72 20 0a 20 20 2a 2a 20 74 68 65 20 65  , or .  ** the e
385fa 6e 64 20 6f 66 20 74 68 65 20 69 6e 70 75 74 2e  nd of the input.
385fb 20 52 65 61 64 20 61 20 72 65 67 75 6c 61 72 20   Read a regular 
385fc 74 6f 6b 65 6e 20 75 73 69 6e 67 20 74 68 65 20  token using the 
385fd 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
385fe 72 0a 20 20 2a 2a 20 69 6e 74 65 72 66 61 63 65  r.  ** interface
385ff 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20 73  . Before doing s
38600 6f 2c 20 66 69 67 75 72 65 20 6f 75 74 20 69 66  o, figure out if
38601 20 74 68 65 72 65 20 69 73 20 61 6e 20 65 78 70   there is an exp
38602 6c 69 63 69 74 0a 20 20 2a 2a 20 63 6f 6c 75 6d  licit.  ** colum
38603 6e 20 73 70 65 63 69 66 69 65 72 20 66 6f 72 20  n specifier for 
38604 74 68 65 20 74 6f 6b 65 6e 2e 20 0a 20 20 2a 2a  the token. .  **
38605 0a 20 20 2a 2a 20 54 4f 44 4f 3a 20 53 74 72 61  .  ** TODO: Stra
38606 6e 67 65 6c 79 2c 20 69 74 20 69 73 20 6e 6f 74  ngely, it is not
38607 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 61 73 73   possible to ass
38608 6f 63 69 61 74 65 20 61 20 63 6f 6c 75 6d 6e 20  ociate a column 
38609 73 70 65 63 69 66 69 65 72 0a 20 20 2a 2a 20 77  specifier.  ** w
3860a 69 74 68 20 61 20 71 75 6f 74 65 64 20 70 68 72  ith a quoted phr
3860b 61 73 65 2c 20 6f 6e 6c 79 20 77 69 74 68 20 61  ase, only with a
3860c 20 73 69 6e 67 6c 65 20 74 6f 6b 65 6e 2e 20 4e   single token. N
3860d 6f 74 20 73 75 72 65 20 69 66 20 74 68 69 73 20  ot sure if this 
3860e 77 61 73 0a 20 20 2a 2a 20 61 6e 20 69 6d 70 6c  was.  ** an impl
3860f 65 6d 65 6e 74 61 74 69 6f 6e 20 61 72 74 69 66  ementation artif
38610 61 63 74 20 6f 72 20 61 6e 20 69 6e 74 65 6e 74  act or an intent
38611 69 6f 6e 61 6c 20 64 65 63 69 73 69 6f 6e 20 77  ional decision w
38612 68 65 6e 20 66 74 73 33 20 77 61 73 0a 20 20 2a  hen fts3 was.  *
38613 2a 20 66 69 72 73 74 20 69 6d 70 6c 65 6d 65 6e  * first implemen
38614 74 65 64 2e 20 57 68 69 63 68 65 76 65 72 20 69  ted. Whichever i
38615 74 20 77 61 73 2c 20 74 68 69 73 20 6d 6f 64 75  t was, this modu
38616 6c 65 20 64 75 70 6c 69 63 61 74 65 73 20 74 68  le duplicates th
38617 65 20 0a 20 20 2a 2a 20 6c 69 6d 69 74 61 74 69  e .  ** limitati
38618 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 69 43 6f 6c 20  on..  */.  iCol 
38619 3d 20 70 50 61 72 73 65 2d 3e 69 44 65 66 61 75  = pParse->iDefau
3861a 6c 74 43 6f 6c 3b 0a 20 20 69 43 6f 6c 4c 65 6e  ltCol;.  iColLen
3861b 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 69 3d 30   = 0;.  for(ii=0
3861c 3b 20 69 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f  ; ii<pParse->nCo
3861d 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 63 6f  l; ii++){.    co
3861e 6e 73 74 20 63 68 61 72 20 2a 7a 53 74 72 20 3d  nst char *zStr =
3861f 20 70 50 61 72 73 65 2d 3e 61 7a 43 6f 6c 5b 69   pParse->azCol[i
38620 69 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 53 74 72  i];.    int nStr
38621 20 3d 20 73 74 72 6c 65 6e 28 7a 53 74 72 29 3b   = strlen(zStr);
38622 0a 20 20 20 20 69 66 28 20 6e 49 6e 70 75 74 3e  .    if( nInput>
38623 6e 53 74 72 20 26 26 20 7a 49 6e 70 75 74 5b 6e  nStr && zInput[n
38624 53 74 72 5d 3d 3d 27 3a 27 20 0a 20 20 20 20 20  Str]==':' .     
38625 26 26 20 73 71 6c 69 74 65 33 5f 73 74 72 6e 69  && sqlite3_strni
38626 63 6d 70 28 7a 53 74 72 2c 20 7a 49 6e 70 75 74  cmp(zStr, zInput
38627 2c 20 6e 53 74 72 29 3d 3d 30 20 0a 20 20 20 20  , nStr)==0 .    
38628 29 7b 0a 20 20 20 20 20 20 69 43 6f 6c 20 3d 20  ){.      iCol = 
38629 69 69 3b 0a 20 20 20 20 20 20 69 43 6f 6c 4c 65  ii;.      iColLe
3862a 6e 20 3d 20 28 28 7a 49 6e 70 75 74 20 2d 20 7a  n = ((zInput - z
3862b 29 20 2b 20 6e 53 74 72 20 2b 20 31 29 3b 0a 20  ) + nStr + 1);. 
3862c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
3862d 7d 0a 20 20 7d 0a 20 20 72 63 20 3d 20 67 65 74  }.  }.  rc = get
3862e 4e 65 78 74 54 6f 6b 65 6e 28 70 50 61 72 73 65  NextToken(pParse
3862f 2c 20 69 43 6f 6c 2c 20 26 7a 5b 69 43 6f 6c 4c  , iCol, &z[iColL
38630 65 6e 5d 2c 20 6e 2d 69 43 6f 6c 4c 65 6e 2c 20  en], n-iColLen, 
38631 70 70 45 78 70 72 2c 20 70 6e 43 6f 6e 73 75 6d  ppExpr, pnConsum
38632 65 64 29 3b 0a 20 20 2a 70 6e 43 6f 6e 73 75 6d  ed);.  *pnConsum
38633 65 64 20 2b 3d 20 69 43 6f 6c 4c 65 6e 3b 0a 20  ed += iColLen;. 
38634 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
38635 2a 0a 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e  *.** The argumen
38636 74 20 69 73 20 61 6e 20 46 74 73 33 45 78 70 72  t is an Fts3Expr
38637 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 61   structure for a
38638 20 62 69 6e 61 72 79 20 6f 70 65 72 61 74 6f 72   binary operator
38639 20 28 61 6e 79 20 74 79 70 65 0a 2a 2a 20 65 78   (any type.** ex
3863a 63 65 70 74 20 61 6e 20 46 54 53 51 55 45 52 59  cept an FTSQUERY
3863b 5f 50 48 52 41 53 45 29 2e 20 52 65 74 75 72 6e  _PHRASE). Return
3863c 20 61 6e 20 69 6e 74 65 67 65 72 20 76 61 6c 75   an integer valu
3863d 65 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74  e representing t
3863e 68 65 0a 2a 2a 20 70 72 65 63 65 64 65 6e 63 65  he.** precedence
3863f 20 6f 66 20 74 68 65 20 6f 70 65 72 61 74 6f 72   of the operator
38640 2e 20 4c 6f 77 65 72 20 76 61 6c 75 65 73 20 68  . Lower values h
38641 61 76 65 20 61 20 68 69 67 68 65 72 20 70 72 65  ave a higher pre
38642 63 65 64 65 6e 63 65 20 28 69 2e 65 2e 0a 2a 2a  cedence (i.e..**
38643 20 67 72 6f 75 70 20 6d 6f 72 65 20 74 69 67 68   group more tigh
38644 74 6c 79 29 2e 20 46 6f 72 20 65 78 61 6d 70 6c  tly). For exampl
38645 65 2c 20 69 6e 20 74 68 65 20 43 20 6c 61 6e 67  e, in the C lang
38646 75 61 67 65 2c 20 74 68 65 20 3d 3d 20 6f 70 65  uage, the == ope
38647 72 61 74 6f 72 0a 2a 2a 20 67 72 6f 75 70 73 20  rator.** groups 
38648 6d 6f 72 65 20 74 69 67 68 74 6c 79 20 74 68 61  more tightly tha
38649 6e 20 7c 7c 2c 20 61 6e 64 20 77 6f 75 6c 64 20  n ||, and would 
3864a 74 68 65 72 65 66 6f 72 65 20 68 61 76 65 20 61  therefore have a
3864b 20 68 69 67 68 65 72 20 70 72 65 63 65 64 65 6e   higher preceden
3864c 63 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 75  ce..**.** When u
3864d 73 69 6e 67 20 74 68 65 20 6e 65 77 20 66 74 73  sing the new fts
3864e 33 20 71 75 65 72 79 20 73 79 6e 74 61 78 20 28  3 query syntax (
3864f 77 68 65 6e 20 53 51 4c 49 54 45 5f 45 4e 41 42  when SQLITE_ENAB
38650 4c 45 5f 46 54 53 33 5f 50 41 52 45 4e 54 48 45  LE_FTS3_PARENTHE
38651 53 49 53 0a 2a 2a 20 69 73 20 64 65 66 69 6e 65  SIS.** is define
38652 64 29 2c 20 74 68 65 20 6f 72 64 65 72 20 6f 66  d), the order of
38653 20 74 68 65 20 6f 70 65 72 61 74 6f 72 73 20 69   the operators i
38654 6e 20 70 72 65 63 65 64 65 6e 63 65 20 66 72 6f  n precedence fro
38655 6d 20 68 69 67 68 65 73 74 20 74 6f 0a 2a 2a 20  m highest to.** 
38656 6c 6f 77 65 73 74 20 69 73 3a 0a 2a 2a 0a 2a 2a  lowest is:.**.**
38657 20 20 20 4e 45 41 52 0a 2a 2a 20 20 20 4e 4f 54     NEAR.**   NOT
38658 0a 2a 2a 20 20 20 41 4e 44 20 28 69 6e 63 6c 75  .**   AND (inclu
38659 64 69 6e 67 20 69 6d 70 6c 69 63 69 74 20 41 4e  ding implicit AN
3865a 44 73 29 0a 2a 2a 20 20 20 4f 52 0a 2a 2a 0a 2a  Ds).**   OR.**.*
3865b 2a 20 4e 6f 74 65 20 74 68 61 74 20 77 68 65 6e  * Note that when
3865c 20 75 73 69 6e 67 20 74 68 65 20 6f 6c 64 20 71   using the old q
3865d 75 65 72 79 20 73 79 6e 74 61 78 2c 20 74 68 65  uery syntax, the
3865e 20 4f 52 20 6f 70 65 72 61 74 6f 72 20 68 61 73   OR operator has
3865f 20 61 20 68 69 67 68 65 72 0a 2a 2a 20 70 72 65   a higher.** pre
38660 63 65 64 65 6e 63 65 20 74 68 61 6e 20 74 68 65  cedence than the
38661 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 0a 2a   AND operator..*
38662 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 50  /.static int opP
38663 72 65 63 65 64 65 6e 63 65 28 46 74 73 33 45 78  recedence(Fts3Ex
38664 70 72 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  pr *p){.  assert
38665 28 20 70 2d 3e 65 54 79 70 65 21 3d 46 54 53 51  ( p->eType!=FTSQ
38666 55 45 52 59 5f 50 48 52 41 53 45 20 29 3b 0a 20  UERY_PHRASE );. 
38667 20 69 66 28 20 73 71 6c 69 74 65 33 5f 66 74 73   if( sqlite3_fts
38668 33 5f 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68  3_enable_parenth
38669 65 73 65 73 20 29 7b 0a 20 20 20 20 72 65 74 75  eses ){.    retu
3866a 72 6e 20 70 2d 3e 65 54 79 70 65 3b 0a 20 20 7d  rn p->eType;.  }
3866b 65 6c 73 65 20 69 66 28 20 70 2d 3e 65 54 79 70  else if( p->eTyp
3866c 65 3d 3d 46 54 53 51 55 45 52 59 5f 4e 45 41 52  e==FTSQUERY_NEAR
3866d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31   ){.    return 1
3866e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d  ;.  }else if( p-
3866f 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  >eType==FTSQUERY
38670 5f 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OR ){.    retur
38671 6e 20 32 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  n 2;.  }.  asser
38672 74 28 20 70 2d 3e 65 54 79 70 65 3d 3d 46 54 53  t( p->eType==FTS
38673 51 55 45 52 59 5f 41 4e 44 20 29 3b 0a 20 20 72  QUERY_AND );.  r
38674 65 74 75 72 6e 20 33 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 3;.}../*.*
38675 2a 20 41 72 67 75 6d 65 6e 74 20 70 70 48 65 61  * Argument ppHea
38676 64 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69  d contains a poi
38677 6e 74 65 72 20 74 6f 20 74 68 65 20 63 75 72 72  nter to the curr
38678 65 6e 74 20 68 65 61 64 20 6f 66 20 61 20 71 75  ent head of a qu
38679 65 72 79 20 0a 2a 2a 20 65 78 70 72 65 73 73 69  ery .** expressi
3867a 6f 6e 20 74 72 65 65 20 62 65 69 6e 67 20 70 61  on tree being pa
3867b 72 73 65 64 2e 20 70 50 72 65 76 20 69 73 20 74  rsed. pPrev is t
3867c 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f  he expression no
3867d 64 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  de most recently
3867e 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 69 6e 74  .** inserted int
3867f 6f 20 74 68 65 20 74 72 65 65 2e 20 54 68 69 73  o the tree. This
38680 20 66 75 6e 63 74 69 6f 6e 20 61 64 64 73 20 70   function adds p
38681 4e 65 77 2c 20 77 68 69 63 68 20 69 73 20 61 6c  New, which is al
38682 77 61 79 73 20 61 20 62 69 6e 61 72 79 0a 2a 2a  ways a binary.**
38683 20 6f 70 65 72 61 74 6f 72 20 6e 6f 64 65 2c 20   operator node, 
38684 69 6e 74 6f 20 74 68 65 20 65 78 70 72 65 73 73  into the express
38685 69 6f 6e 20 74 72 65 65 20 62 61 73 65 64 20 6f  ion tree based o
38686 6e 20 74 68 65 20 72 65 6c 61 74 69 76 65 20 70  n the relative p
38687 72 65 63 65 64 65 6e 63 65 0a 2a 2a 20 6f 66 20  recedence.** of 
38688 70 4e 65 77 20 61 6e 64 20 74 68 65 20 65 78 69  pNew and the exi
38689 73 74 69 6e 67 20 6e 6f 64 65 73 20 6f 66 20 74  sting nodes of t
3868a 68 65 20 74 72 65 65 2e 20 54 68 69 73 20 6d 61  he tree. This ma
3868b 79 20 72 65 73 75 6c 74 20 69 6e 20 74 68 65 20  y result in the 
3868c 68 65 61 64 0a 2a 2a 20 6f 66 20 74 68 65 20 74  head.** of the t
3868d 72 65 65 20 63 68 61 6e 67 69 6e 67 2c 20 69 6e  ree changing, in
3868e 20 77 68 69 63 68 20 63 61 73 65 20 2a 70 70 48   which case *ppH
3868f 65 61 64 20 69 73 20 73 65 74 20 74 6f 20 74 68  ead is set to th
38690 65 20 6e 65 77 20 72 6f 6f 74 20 6e 6f 64 65 2e  e new root node.
38691 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
38692 69 6e 73 65 72 74 42 69 6e 61 72 79 4f 70 65 72  insertBinaryOper
38693 61 74 6f 72 28 0a 20 20 46 74 73 33 45 78 70 72  ator(.  Fts3Expr
38694 20 2a 2a 70 70 48 65 61 64 2c 20 20 20 20 20 20   **ppHead,      
38695 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74   /* Pointer to t
38696 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20  he root node of 
38697 61 20 74 72 65 65 20 2a 2f 0a 20 20 46 74 73 33  a tree */.  Fts3
38698 45 78 70 72 20 2a 70 50 72 65 76 2c 20 20 20 20  Expr *pPrev,    
38699 20 20 20 20 20 2f 2a 20 4e 6f 64 65 20 6d 6f 73       /* Node mos
3869a 74 20 72 65 63 65 6e 74 6c 79 20 69 6e 73 65 72  t recently inser
3869b 74 65 64 20 69 6e 74 6f 20 74 68 65 20 74 72 65  ted into the tre
3869c 65 20 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20  e */.  Fts3Expr 
3869d 2a 70 4e 65 77 20 20 20 20 20 20 20 20 20 20 20  *pNew           
3869e 2f 2a 20 4e 65 77 20 62 69 6e 61 72 79 20 6e 6f  /* New binary no
3869f 64 65 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74  de to insert int
386a0 6f 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65  o expression tre
386a1 65 20 2a 2f 0a 29 7b 0a 20 20 46 74 73 33 45 78  e */.){.  Fts3Ex
386a2 70 72 20 2a 70 53 70 6c 69 74 20 3d 20 70 50 72  pr *pSplit = pPr
386a3 65 76 3b 0a 20 20 77 68 69 6c 65 28 20 70 53 70  ev;.  while( pSp
386a4 6c 69 74 2d 3e 70 50 61 72 65 6e 74 20 26 26 20  lit->pParent && 
386a5 6f 70 50 72 65 63 65 64 65 6e 63 65 28 70 53 70  opPrecedence(pSp
386a6 6c 69 74 2d 3e 70 50 61 72 65 6e 74 29 3c 3d 6f  lit->pParent)<=o
386a7 70 50 72 65 63 65 64 65 6e 63 65 28 70 4e 65 77  pPrecedence(pNew
386a8 29 20 29 7b 0a 20 20 20 20 70 53 70 6c 69 74 20  ) ){.    pSplit 
386a9 3d 20 70 53 70 6c 69 74 2d 3e 70 50 61 72 65 6e  = pSplit->pParen
386aa 74 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 53  t;.  }..  if( pS
386ab 70 6c 69 74 2d 3e 70 50 61 72 65 6e 74 20 29 7b  plit->pParent ){
386ac 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 53 70  .    assert( pSp
386ad 6c 69 74 2d 3e 70 50 61 72 65 6e 74 2d 3e 70 52  lit->pParent->pR
386ae 69 67 68 74 3d 3d 70 53 70 6c 69 74 20 29 3b 0a  ight==pSplit );.
386af 20 20 20 20 70 53 70 6c 69 74 2d 3e 70 50 61 72      pSplit->pPar
386b0 65 6e 74 2d 3e 70 52 69 67 68 74 20 3d 20 70 4e  ent->pRight = pN
386b1 65 77 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 70 50  ew;.    pNew->pP
386b2 61 72 65 6e 74 20 3d 20 70 53 70 6c 69 74 2d 3e  arent = pSplit->
386b3 70 50 61 72 65 6e 74 3b 0a 20 20 7d 65 6c 73 65  pParent;.  }else
386b4 7b 0a 20 20 20 20 2a 70 70 48 65 61 64 20 3d 20  {.    *ppHead = 
386b5 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 70 4e 65 77  pNew;.  }.  pNew
386b6 2d 3e 70 4c 65 66 74 20 3d 20 70 53 70 6c 69 74  ->pLeft = pSplit
386b7 3b 0a 20 20 70 53 70 6c 69 74 2d 3e 70 50 61 72  ;.  pSplit->pPar
386b8 65 6e 74 20 3d 20 70 4e 65 77 3b 0a 7d 0a 0a 2f  ent = pNew;.}../
386b9 2a 0a 2a 2a 20 50 61 72 73 65 20 74 68 65 20 66  *.** Parse the f
386ba 74 73 33 20 71 75 65 72 79 20 65 78 70 72 65 73  ts3 query expres
386bb 73 69 6f 6e 20 66 6f 75 6e 64 20 69 6e 20 62 75  sion found in bu
386bc 66 66 65 72 20 7a 2c 20 6c 65 6e 67 74 68 20 6e  ffer z, length n
386bd 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a  . This function.
386be 2a 2a 20 72 65 74 75 72 6e 73 20 65 69 74 68 65  ** returns eithe
386bf 72 20 77 68 65 6e 20 74 68 65 20 65 6e 64 20 6f  r when the end o
386c0 66 20 74 68 65 20 62 75 66 66 65 72 20 69 73 20  f the buffer is 
386c1 72 65 61 63 68 65 64 20 6f 72 20 61 6e 20 75 6e  reached or an un
386c2 6d 61 74 63 68 65 64 20 0a 2a 2a 20 63 6c 6f 73  matched .** clos
386c3 69 6e 67 20 62 72 61 63 6b 65 74 20 2d 20 27 29  ing bracket - ')
386c4 27 20 2d 20 69 73 20 65 6e 63 6f 75 6e 74 65 72  ' - is encounter
386c5 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 75 63  ed..**.** If suc
386c6 63 65 73 73 66 75 6c 2c 20 53 51 4c 49 54 45 5f  cessful, SQLITE_
386c7 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20  OK is returned, 
386c8 2a 70 70 45 78 70 72 20 69 73 20 73 65 74 20 74  *ppExpr is set t
386c9 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a  o point to the.*
386ca 2a 20 70 61 72 73 65 64 20 66 6f 72 6d 20 6f 66  * parsed form of
386cb 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
386cc 61 6e 64 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20  and *pnConsumed 
386cd 69 73 20 73 65 74 20 74 6f 20 74 68 65 20 6e 75  is set to the nu
386ce 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 79 74 65 73  mber of.** bytes
386cf 20 72 65 61 64 20 66 72 6f 6d 20 62 75 66 66 65   read from buffe
386d0 72 20 7a 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  r z. Otherwise, 
386d1 2a 70 70 45 78 70 72 20 69 73 20 73 65 74 20 74  *ppExpr is set t
386d2 6f 20 30 20 61 6e 64 20 53 51 4c 49 54 45 5f 4e  o 0 and SQLITE_N
386d3 4f 4d 45 4d 0a 2a 2a 20 28 6f 75 74 20 6f 66 20  OMEM.** (out of 
386d4 6d 65 6d 6f 72 79 20 65 72 72 6f 72 29 20 6f 72  memory error) or
386d5 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 28 70   SQLITE_ERROR (p
386d6 61 72 73 65 20 65 72 72 6f 72 29 20 69 73 20 72  arse error) is r
386d7 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74  eturned..*/.stat
386d8 69 63 20 69 6e 74 20 66 74 73 33 45 78 70 72 50  ic int fts3ExprP
386d9 61 72 73 65 28 0a 20 20 50 61 72 73 65 43 6f 6e  arse(.  ParseCon
386da 74 65 78 74 20 2a 70 50 61 72 73 65 2c 20 20 20  text *pParse,   
386db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
386dc 2f 2a 20 66 74 73 33 20 71 75 65 72 79 20 70 61  /* fts3 query pa
386dd 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  rse context */. 
386de 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20   const char *z, 
386df 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20 20 20  int n,          
386e0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74           /* Text
386e1 20 6f 66 20 4d 41 54 43 48 20 71 75 65 72 79 20   of MATCH query 
386e2 2a 2f 0a 20 20 46 74 73 33 45 78 70 72 20 2a 2a  */.  Fts3Expr **
386e3 70 70 45 78 70 72 2c 20 20 20 20 20 20 20 20 20  ppExpr,         
386e4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
386e5 4f 55 54 3a 20 50 61 72 73 65 64 20 71 75 65 72  OUT: Parsed quer
386e6 79 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  y structure */. 
386e7 20 69 6e 74 20 2a 70 6e 43 6f 6e 73 75 6d 65 64   int *pnConsumed
386e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
386e9 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
386ea 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
386eb 20 63 6f 6e 73 75 6d 65 64 20 2a 2f 0a 29 7b 0a   consumed */.){.
386ec 20 20 46 74 73 33 45 78 70 72 20 2a 70 52 65 74    Fts3Expr *pRet
386ed 20 3d 20 30 3b 0a 20 20 46 74 73 33 45 78 70 72   = 0;.  Fts3Expr
386ee 20 2a 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 46   *pPrev = 0;.  F
386ef 74 73 33 45 78 70 72 20 2a 70 4e 6f 74 42 72 61  ts3Expr *pNotBra
386f0 6e 63 68 20 3d 20 30 3b 20 20 20 20 20 20 20 20  nch = 0;        
386f1 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 75         /* Only u
386f2 73 65 64 20 69 6e 20 6c 65 67 61 63 79 20 70 61  sed in legacy pa
386f3 72 73 65 20 6d 6f 64 65 20 2a 2f 0a 20 20 69 6e  rse mode */.  in
386f4 74 20 6e 49 6e 20 3d 20 6e 3b 0a 20 20 63 6f 6e  t nIn = n;.  con
386f5 73 74 20 63 68 61 72 20 2a 7a 49 6e 20 3d 20 7a  st char *zIn = z
386f6 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
386f7 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 73  ITE_OK;.  int is
386f8 52 65 71 75 69 72 65 50 68 72 61 73 65 20 3d 20  RequirePhrase = 
386f9 31 3b 0a 0a 20 20 77 68 69 6c 65 28 20 72 63 3d  1;..  while( rc=
386fa 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
386fb 20 20 46 74 73 33 45 78 70 72 20 2a 70 20 3d 20    Fts3Expr *p = 
386fc 30 3b 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65  0;.    int nByte
386fd 20 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 67   = 0;.    rc = g
386fe 65 74 4e 65 78 74 4e 6f 64 65 28 70 50 61 72 73  etNextNode(pPars
386ff 65 2c 20 7a 49 6e 2c 20 6e 49 6e 2c 20 26 70 2c  e, zIn, nIn, &p,
38700 20 26 6e 42 79 74 65 29 3b 0a 20 20 20 20 69 66   &nByte);.    if
38701 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
38702 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 73 50  ){.      int isP
38703 68 72 61 73 65 3b 0a 0a 20 20 20 20 20 20 69 66  hrase;..      if
38704 28 20 21 73 71 6c 69 74 65 33 5f 66 74 73 33 5f  ( !sqlite3_fts3_
38705 65 6e 61 62 6c 65 5f 70 61 72 65 6e 74 68 65 73  enable_parenthes
38706 65 73 20 0a 20 20 20 20 20 20 20 26 26 20 70 2d  es .       && p-
38707 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45 52 59  >eType==FTSQUERY
38708 5f 50 48 52 41 53 45 20 26 26 20 70 2d 3e 70 50  _PHRASE && p->pP
38709 68 72 61 73 65 2d 3e 69 73 4e 6f 74 20 0a 20 20  hrase->isNot .  
3870a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 2f      ){.        /
3870b 2a 20 43 72 65 61 74 65 20 61 6e 20 69 6d 70 6c  * Create an impl
3870c 69 63 69 74 20 4e 4f 54 20 6f 70 65 72 61 74 6f  icit NOT operato
3870d 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 46 74  r. */.        Ft
3870e 73 33 45 78 70 72 20 2a 70 4e 6f 74 20 3d 20 73  s3Expr *pNot = s
3870f 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
38710 7a 65 6f 66 28 46 74 73 33 45 78 70 72 29 29 3b  zeof(Fts3Expr));
38711 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 70 4e  .        if( !pN
38712 6f 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ot ){.          
38713 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 46  sqlite3Fts3ExprF
38714 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20  ree(p);.        
38715 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
38716 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 67  MEM;.          g
38717 6f 74 6f 20 65 78 70 72 70 61 72 73 65 5f 6f 75  oto exprparse_ou
38718 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  t;.        }.   
38719 20 20 20 20 20 6d 65 6d 73 65 74 28 70 4e 6f 74       memset(pNot
3871a 2c 20 30 2c 20 73 69 7a 65 6f 66 28 46 74 73 33  , 0, sizeof(Fts3
3871b 45 78 70 72 29 29 3b 0a 20 20 20 20 20 20 20 20  Expr));.        
3871c 70 4e 6f 74 2d 3e 65 54 79 70 65 20 3d 20 46 54  pNot->eType = FT
3871d 53 51 55 45 52 59 5f 4e 4f 54 3b 0a 20 20 20 20  SQUERY_NOT;.    
3871e 20 20 20 20 70 4e 6f 74 2d 3e 70 52 69 67 68 74      pNot->pRight
3871f 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 69 66   = p;.        if
38720 28 20 70 4e 6f 74 42 72 61 6e 63 68 20 29 7b 0a  ( pNotBranch ){.
38721 20 20 20 20 20 20 20 20 20 20 70 4e 6f 74 2d 3e            pNot->
38722 70 4c 65 66 74 20 3d 20 70 4e 6f 74 42 72 61 6e  pLeft = pNotBran
38723 63 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ch;.        }.  
38724 20 20 20 20 20 20 70 4e 6f 74 42 72 61 6e 63 68        pNotBranch
38725 20 3d 20 70 4e 6f 74 3b 0a 20 20 20 20 20 20 20   = pNot;.       
38726 20 70 20 3d 20 70 50 72 65 76 3b 0a 20 20 20 20   p = pPrev;.    
38727 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
38728 20 69 6e 74 20 65 54 79 70 65 20 3d 20 70 2d 3e   int eType = p->
38729 65 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 61  eType;.        a
3872a 73 73 65 72 74 28 20 65 54 79 70 65 21 3d 46 54  ssert( eType!=FT
3872b 53 51 55 45 52 59 5f 50 48 52 41 53 45 20 7c 7c  SQUERY_PHRASE ||
3872c 20 21 70 2d 3e 70 50 68 72 61 73 65 2d 3e 69 73   !p->pPhrase->is
3872d 4e 6f 74 20 29 3b 0a 20 20 20 20 20 20 20 20 69  Not );.        i
3872e 73 50 68 72 61 73 65 20 3d 20 28 65 54 79 70 65  sPhrase = (eType
3872f 3d 3d 46 54 53 51 55 45 52 59 5f 50 48 52 41 53  ==FTSQUERY_PHRAS
38730 45 20 7c 7c 20 70 2d 3e 70 4c 65 66 74 29 3b 0a  E || p->pLeft);.
38731 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  .        /* The 
38732 69 73 52 65 71 75 69 72 65 50 68 72 61 73 65 20  isRequirePhrase 
38733 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20  variable is set 
38734 74 6f 20 74 72 75 65 20 69 66 20 61 20 70 68 72  to true if a phr
38735 61 73 65 20 6f 72 0a 20 20 20 20 20 20 20 20 2a  ase or.        *
38736 2a 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  * an expression 
38737 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 70 61 72  contained in par
38738 65 6e 74 68 65 73 69 73 20 69 73 20 72 65 71 75  enthesis is requ
38739 69 72 65 64 2e 20 49 66 20 61 0a 20 20 20 20 20  ired. If a.     
3873a 20 20 20 2a 2a 20 62 69 6e 61 72 79 20 6f 70 65     ** binary ope
3873b 72 61 74 6f 72 20 28 41 4e 44 2c 20 4f 52 2c 20  rator (AND, OR, 
3873c 4e 4f 54 20 6f 72 20 4e 45 41 52 29 20 69 73 20  NOT or NEAR) is 
3873d 65 6e 63 6f 75 6e 74 65 64 20 77 68 65 6e 0a 20  encounted when. 
3873e 20 20 20 20 20 20 20 2a 2a 20 69 73 52 65 71 75         ** isRequ
3873f 69 72 65 50 68 72 61 73 65 20 69 73 20 73 65 74  irePhrase is set
38740 2c 20 74 68 69 73 20 69 73 20 61 20 73 79 6e 74  , this is a synt
38741 61 78 20 65 72 72 6f 72 2e 0a 20 20 20 20 20 20  ax error..      
38742 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28    */.        if(
38743 20 21 69 73 50 68 72 61 73 65 20 26 26 20 69 73   !isPhrase && is
38744 52 65 71 75 69 72 65 50 68 72 61 73 65 20 29 7b  RequirePhrase ){
38745 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
38746 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28 70  e3Fts3ExprFree(p
38747 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  );.          rc 
38748 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
38749 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 65            goto e
3874a 78 70 72 70 61 72 73 65 5f 6f 75 74 3b 0a 20 20  xprparse_out;.  
3874b 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20        }.  .     
3874c 20 20 20 69 66 28 20 69 73 50 68 72 61 73 65 20     if( isPhrase 
3874d 26 26 20 21 69 73 52 65 71 75 69 72 65 50 68 72  && !isRequirePhr
3874e 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ase ){.         
3874f 20 2f 2a 20 49 6e 73 65 72 74 20 61 6e 20 69 6d   /* Insert an im
38750 70 6c 69 63 69 74 20 41 4e 44 20 6f 70 65 72 61  plicit AND opera
38751 74 6f 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  tor. */.        
38752 20 20 46 74 73 33 45 78 70 72 20 2a 70 41 6e 64    Fts3Expr *pAnd
38753 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  ;.          asse
38754 72 74 28 20 70 52 65 74 20 26 26 20 70 50 72 65  rt( pRet && pPre
38755 76 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70  v );.          p
38756 41 6e 64 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  And = sqlite3_ma
38757 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 46 74 73 33  lloc(sizeof(Fts3
38758 45 78 70 72 29 29 3b 0a 20 20 20 20 20 20 20 20  Expr));.        
38759 20 20 69 66 28 20 21 70 41 6e 64 20 29 7b 0a 20    if( !pAnd ){. 
3875a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
3875b 65 33 46 74 73 33 45 78 70 72 46 72 65 65 28 70  e3Fts3ExprFree(p
3875c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  );.            r
3875d 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
3875e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f  ;.            go
3875f 74 6f 20 65 78 70 72 70 61 72 73 65 5f 6f 75 74  to exprparse_out
38760 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
38761 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70          memset(p
38762 41 6e 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 46  And, 0, sizeof(F
38763 74 73 33 45 78 70 72 29 29 3b 0a 20 20 20 20 20  ts3Expr));.     
38764 20 20 20 20 20 70 41 6e 64 2d 3e 65 54 79 70 65       pAnd->eType
38765 20 3d 20 46 54 53 51 55 45 52 59 5f 41 4e 44 3b   = FTSQUERY_AND;
38766 0a 20 20 20 20 20 20 20 20 20 20 69 6e 73 65 72  .          inser
38767 74 42 69 6e 61 72 79 4f 70 65 72 61 74 6f 72 28  tBinaryOperator(
38768 26 70 52 65 74 2c 20 70 50 72 65 76 2c 20 70 41  &pRet, pPrev, pA
38769 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70  nd);.          p
3876a 50 72 65 76 20 3d 20 70 41 6e 64 3b 0a 20 20 20  Prev = pAnd;.   
3876b 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
3876c 2f 2a 20 54 68 69 73 20 74 65 73 74 20 63 61 74  /* This test cat
3876d 63 68 65 73 20 61 74 74 65 6d 70 74 73 20 74 6f  ches attempts to
3876e 20 6d 61 6b 65 20 65 69 74 68 65 72 20 6f 70 65   make either ope
3876f 72 61 6e 64 20 6f 66 20 61 20 4e 45 41 52 0a 20  rand of a NEAR. 
38770 20 20 20 20 20 20 20 2a 2a 20 6f 70 65 72 61 74         ** operat
38771 6f 72 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68  or something oth
38772 65 72 20 74 68 61 6e 20 61 20 70 68 72 61 73 65  er than a phrase
38773 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 65  . For example, e
38774 69 74 68 65 72 20 6f 66 0a 20 20 20 20 20 20 20  ither of.       
38775 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   ** the followin
38776 67 3a 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20  g:.        **.  
38777 20 20 20 20 20 20 2a 2a 20 20 20 20 28 62 72 61        **    (bra
38778 63 6b 65 74 65 64 20 65 78 70 72 65 73 73 69 6f  cketed expressio
38779 6e 29 20 4e 45 41 52 20 70 68 72 61 73 65 0a 20  n) NEAR phrase. 
3877a 20 20 20 20 20 20 20 2a 2a 20 20 20 20 70 68 72         **    phr
3877b 61 73 65 20 4e 45 41 52 20 28 62 72 61 63 6b 65  ase NEAR (bracke
3877c 74 65 64 20 65 78 70 72 65 73 73 69 6f 6e 29 0a  ted expression).
3877d 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
3877e 20 20 20 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20     ** Return an 
3877f 65 72 72 6f 72 20 69 6e 20 65 69 74 68 65 72 20  error in either 
38780 63 61 73 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f  case..        */
38781 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 50 72  .        if( pPr
38782 65 76 20 26 26 20 28 0a 20 20 20 20 20 20 20 20  ev && (.        
38783 20 20 20 20 28 65 54 79 70 65 3d 3d 46 54 53 51      (eType==FTSQ
38784 55 45 52 59 5f 4e 45 41 52 20 26 26 20 21 69 73  UERY_NEAR && !is
38785 50 68 72 61 73 65 20 26 26 20 70 50 72 65 76 2d  Phrase && pPrev-
38786 3e 65 54 79 70 65 21 3d 46 54 53 51 55 45 52 59  >eType!=FTSQUERY
38787 5f 50 48 52 41 53 45 29 0a 20 20 20 20 20 20 20  _PHRASE).       
38788 20 20 7c 7c 20 28 65 54 79 70 65 21 3d 46 54 53    || (eType!=FTS
38789 51 55 45 52 59 5f 50 48 52 41 53 45 20 26 26 20  QUERY_PHRASE && 
3878a 69 73 50 68 72 61 73 65 20 26 26 20 70 50 72 65  isPhrase && pPre
3878b 76 2d 3e 65 54 79 70 65 3d 3d 46 54 53 51 55 45  v->eType==FTSQUE
3878c 52 59 5f 4e 45 41 52 29 0a 20 20 20 20 20 20 20  RY_NEAR).       
3878d 20 29 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73   )){.          s
3878e 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 46 72  qlite3Fts3ExprFr
3878f 65 65 28 70 29 3b 0a 20 20 20 20 20 20 20 20 20  ee(p);.         
38790 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
38791 4f 52 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  OR;.          go
38792 74 6f 20 65 78 70 72 70 61 72 73 65 5f 6f 75 74  to exprparse_out
38793 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 0a 20  ;.        }.  . 
38794 20 20 20 20 20 20 20 69 66 28 20 69 73 50 68 72         if( isPhr
38795 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ase ){.         
38796 20 69 66 28 20 70 52 65 74 20 29 7b 0a 20 20 20   if( pRet ){.   
38797 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
38798 20 70 50 72 65 76 20 26 26 20 70 50 72 65 76 2d   pPrev && pPrev-
38799 3e 70 4c 65 66 74 20 26 26 20 70 50 72 65 76 2d  >pLeft && pPrev-
3879a 3e 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20 20  >pRight==0 );.  
3879b 20 20 20 20 20 20 20 20 20 20 70 50 72 65 76 2d            pPrev-
3879c 3e 70 52 69 67 68 74 20 3d 20 70 3b 0a 20 20 20  >pRight = p;.   
3879d 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50 61 72           p->pPar
3879e 65 6e 74 20 3d 20 70 50 72 65 76 3b 0a 20 20 20  ent = pPrev;.   
3879f 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
387a0 20 20 20 20 20 20 20 20 20 20 70 52 65 74 20 3d            pRet =
387a1 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a   p;.          }.
387a2 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
387a3 20 20 20 20 20 20 20 20 20 69 6e 73 65 72 74 42           insertB
387a4 69 6e 61 72 79 4f 70 65 72 61 74 6f 72 28 26 70  inaryOperator(&p
387a5 52 65 74 2c 20 70 50 72 65 76 2c 20 70 29 3b 0a  Ret, pPrev, p);.
387a6 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
387a7 20 20 69 73 52 65 71 75 69 72 65 50 68 72 61 73    isRequirePhras
387a8 65 20 3d 20 21 69 73 50 68 72 61 73 65 3b 0a 20  e = !isPhrase;. 
387a9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73       }.      ass
387aa 65 72 74 28 20 6e 42 79 74 65 3e 30 20 29 3b 0a  ert( nByte>0 );.
387ab 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
387ac 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
387ad 7c 7c 20 28 6e 42 79 74 65 3e 30 20 26 26 20 6e  || (nByte>0 && n
387ae 42 79 74 65 3c 3d 6e 49 6e 29 20 29 3b 0a 20 20  Byte<=nIn) );.  
387af 20 20 6e 49 6e 20 2d 3d 20 6e 42 79 74 65 3b 0a    nIn -= nByte;.
387b0 20 20 20 20 7a 49 6e 20 2b 3d 20 6e 42 79 74 65      zIn += nByte
387b1 3b 0a 20 20 20 20 70 50 72 65 76 20 3d 20 70 3b  ;.    pPrev = p;
387b2 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d  .  }..  if( rc==
387b3 53 51 4c 49 54 45 5f 44 4f 4e 45 20 26 26 20 70  SQLITE_DONE && p
387b4 52 65 74 20 26 26 20 69 73 52 65 71 75 69 72 65  Ret && isRequire
387b5 50 68 72 61 73 65 20 29 7b 0a 20 20 20 20 72 63  Phrase ){.    rc
387b6 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
387b7 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d  .  }..  if( rc==
387b8 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20  SQLITE_DONE ){. 
387b9 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
387ba 4b 3b 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69  K;.    if( !sqli
387bb 74 65 33 5f 66 74 73 33 5f 65 6e 61 62 6c 65 5f  te3_fts3_enable_
387bc 70 61 72 65 6e 74 68 65 73 65 73 20 26 26 20 70  parentheses && p
387bd 4e 6f 74 42 72 61 6e 63 68 20 29 7b 0a 20 20 20  NotBranch ){.   
387be 20 20 20 69 66 28 20 21 70 52 65 74 20 29 7b 0a     if( !pRet ){.
387bf 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c          rc = SQL
387c0 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
387c1 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
387c2 46 74 73 33 45 78 70 72 20 2a 70 49 74 65 72 20  Fts3Expr *pIter 
387c3 3d 20 70 4e 6f 74 42 72 61 6e 63 68 3b 0a 20 20  = pNotBranch;.  
387c4 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 49 74        while( pIt
387c5 65 72 2d 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20  er->pLeft ){.   
387c6 20 20 20 20 20 20 20 70 49 74 65 72 20 3d 20 70         pIter = p
387c7 49 74 65 72 2d 3e 70 4c 65 66 74 3b 0a 20 20 20  Iter->pLeft;.   
387c8 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70       }.        p
387c9 49 74 65 72 2d 3e 70 4c 65 66 74 20 3d 20 70 52  Iter->pLeft = pR
387ca 65 74 3b 0a 20 20 20 20 20 20 20 20 70 52 65 74  et;.        pRet
387cb 20 3d 20 70 4e 6f 74 42 72 61 6e 63 68 3b 0a 20   = pNotBranch;. 
387cc 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
387cd 0a 20 20 2a 70 6e 43 6f 6e 73 75 6d 65 64 20 3d  .  *pnConsumed =
387ce 20 6e 20 2d 20 6e 49 6e 3b 0a 0a 65 78 70 72 70   n - nIn;..exprp
387cf 61 72 73 65 5f 6f 75 74 3a 0a 20 20 69 66 28 20  arse_out:.  if( 
387d0 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
387d1 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33  .    sqlite3Fts3
387d2 45 78 70 72 46 72 65 65 28 70 52 65 74 29 3b 0a  ExprFree(pRet);.
387d3 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 45      sqlite3Fts3E
387d4 78 70 72 46 72 65 65 28 70 4e 6f 74 42 72 61 6e  xprFree(pNotBran
387d5 63 68 29 3b 0a 20 20 20 20 70 52 65 74 20 3d 20  ch);.    pRet = 
387d6 30 3b 0a 20 20 7d 0a 20 20 2a 70 70 45 78 70 72  0;.  }.  *ppExpr
387d7 20 3d 20 70 52 65 74 3b 0a 20 20 72 65 74 75 72   = pRet;.  retur
387d8 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  n rc;.}../*.** P
387d9 61 72 61 6d 65 74 65 72 73 20 7a 20 61 6e 64 20  arameters z and 
387da 6e 20 63 6f 6e 74 61 69 6e 20 61 20 70 6f 69 6e  n contain a poin
387db 74 65 72 20 74 6f 20 61 6e 64 20 6c 65 6e 67 74  ter to and lengt
387dc 68 20 6f 66 20 61 20 62 75 66 66 65 72 20 63 6f  h of a buffer co
387dd 6e 74 61 69 6e 69 6e 67 0a 2a 2a 20 61 6e 20 66  ntaining.** an f
387de 74 73 33 20 71 75 65 72 79 20 65 78 70 72 65 73  ts3 query expres
387df 73 69 6f 6e 2c 20 72 65 73 70 65 63 74 69 76 65  sion, respective
387e0 6c 79 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f  ly. This functio
387e1 6e 20 61 74 74 65 6d 70 74 73 20 74 6f 20 70 61  n attempts to pa
387e2 72 73 65 20 74 68 65 0a 2a 2a 20 71 75 65 72 79  rse the.** query
387e3 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20   expression and 
387e4 63 72 65 61 74 65 20 61 20 74 72 65 65 20 6f 66  create a tree of
387e5 20 46 74 73 33 45 78 70 72 20 73 74 72 75 63 74   Fts3Expr struct
387e6 75 72 65 73 20 72 65 70 72 65 73 65 6e 74 69 6e  ures representin
387e7 67 20 74 68 65 0a 2a 2a 20 70 61 72 73 65 64 20  g the.** parsed 
387e8 65 78 70 72 65 73 73 69 6f 6e 2e 20 49 66 20 73  expression. If s
387e9 75 63 63 65 73 73 66 75 6c 2c 20 2a 70 70 45 78  uccessful, *ppEx
387ea 70 72 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69  pr is set to poi
387eb 6e 74 20 74 6f 20 74 68 65 20 68 65 61 64 0a 2a  nt to the head.*
387ec 2a 20 6f 66 20 74 68 65 20 70 61 72 73 65 64 20  * of the parsed 
387ed 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20  expression tree 
387ee 61 6e 64 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73  and SQLITE_OK is
387ef 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 61 6e   returned. If an
387f0 20 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73   error.** occurs
387f1 2c 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f  , either SQLITE_
387f2 4e 4f 4d 45 4d 20 28 6f 75 74 2d 6f 66 2d 6d 65  NOMEM (out-of-me
387f3 6d 6f 72 79 20 65 72 72 6f 72 29 20 6f 72 20 53  mory error) or S
387f4 51 4c 49 54 45 5f 45 52 52 4f 52 20 28 70 61 72  QLITE_ERROR (par
387f5 73 65 0a 2a 2a 20 65 72 72 6f 72 29 20 69 73 20  se.** error) is 
387f6 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a 70 70  returned and *pp
387f7 45 78 70 72 20 69 73 20 73 65 74 20 74 6f 20 30  Expr is set to 0
387f8 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 61 6d  ..**.** If param
387f9 65 74 65 72 20 6e 20 69 73 20 61 20 6e 65 67 61  eter n is a nega
387fa 74 69 76 65 20 6e 75 6d 62 65 72 2c 20 74 68 65  tive number, the
387fb 6e 20 7a 20 69 73 20 61 73 73 75 6d 65 64 20 74  n z is assumed t
387fc 6f 20 70 6f 69 6e 74 20 74 6f 20 61 0a 2a 2a 20  o point to a.** 
387fd 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 73  nul-terminated s
387fe 74 72 69 6e 67 20 61 6e 64 20 74 68 65 20 6c 65  tring and the le
387ff 6e 67 74 68 20 69 73 20 64 65 74 65 72 6d 69 6e  ngth is determin
38800 65 64 20 75 73 69 6e 67 20 73 74 72 6c 65 6e 28  ed using strlen(
38801 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72  )..**.** The fir
38802 73 74 20 70 61 72 61 6d 65 74 65 72 2c 20 70 54  st parameter, pT
38803 6f 6b 65 6e 69 7a 65 72 2c 20 69 73 20 70 61 73  okenizer, is pas
38804 73 65 64 20 74 68 65 20 66 74 73 33 20 74 6f 6b  sed the fts3 tok
38805 65 6e 69 7a 65 72 20 6d 6f 64 75 6c 65 20 74 6f  enizer module to
38806 0a 2a 2a 20 75 73 65 20 74 6f 20 6e 6f 72 6d 61  .** use to norma
38807 6c 69 7a 65 20 71 75 65 72 79 20 74 6f 6b 65 6e  lize query token
38808 73 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67 20  s while parsing 
38809 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 20  the expression. 
3880a 54 68 65 20 61 7a 43 6f 6c 5b 5d 0a 2a 2a 20 61  The azCol[].** a
3880b 72 72 61 79 2c 20 77 68 69 63 68 20 69 73 20 61  rray, which is a
3880c 73 73 75 6d 65 64 20 74 6f 20 63 6f 6e 74 61 69  ssumed to contai
3880d 6e 20 6e 43 6f 6c 20 65 6e 74 72 69 65 73 2c 20  n nCol entries, 
3880e 73 68 6f 75 6c 64 20 63 6f 6e 74 61 69 6e 20 74  should contain t
3880f 68 65 20 6e 61 6d 65 73 0a 2a 2a 20 6f 66 20 65  he names.** of e
38810 61 63 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68  ach column in th
38811 65 20 74 61 72 67 65 74 20 66 74 73 33 20 74 61  e target fts3 ta
38812 62 6c 65 2c 20 69 6e 20 6f 72 64 65 72 20 66 72  ble, in order fr
38813 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 68 74  om left to right
38814 2e 20 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d  . .** Column nam
38815 65 73 20 6d 75 73 74 20 62 65 20 6e 75 6c 2d 74  es must be nul-t
38816 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67  erminated string
38817 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 44 65  s..**.** The iDe
38818 66 61 75 6c 74 43 6f 6c 20 70 61 72 61 6d 65 74  faultCol paramet
38819 65 72 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73  er should be pas
3881a 73 65 64 20 74 68 65 20 69 6e 64 65 78 20 6f 66  sed the index of
3881b 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d   the table colum
3881c 6e 0a 2a 2a 20 74 68 61 74 20 61 70 70 65 61 72  n.** that appear
3881d 73 20 6f 6e 20 74 68 65 20 6c 65 66 74 2d 68 61  s on the left-ha
3881e 6e 64 2d 73 69 64 65 20 6f 66 20 74 68 65 20 4d  nd-side of the M
3881f 41 54 43 48 20 6f 70 65 72 61 74 6f 72 20 28 74  ATCH operator (t
38820 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 63 6f  he default.** co
38821 6c 75 6d 6e 20 74 6f 20 6d 61 74 63 68 20 61 67  lumn to match ag
38822 61 69 6e 73 74 20 66 6f 72 20 74 6f 6b 65 6e 73  ainst for tokens
38823 20 66 6f 72 20 77 68 69 63 68 20 61 20 63 6f 6c   for which a col
38824 75 6d 6e 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20  umn name is not 
38825 65 78 70 6c 69 63 69 74 6c 79 0a 2a 2a 20 73 70  explicitly.** sp
38826 65 63 69 66 69 65 64 20 61 73 20 70 61 72 74 20  ecified as part 
38827 6f 66 20 74 68 65 20 71 75 65 72 79 20 73 74 72  of the query str
38828 69 6e 67 29 2c 20 6f 72 20 2d 31 20 69 66 20 74  ing), or -1 if t
38829 6f 6b 65 6e 73 20 6d 61 79 20 62 79 20 64 65 66  okens may by def
3882a 61 75 6c 74 0a 2a 2a 20 6d 61 74 63 68 20 61 6e  ault.** match an
3882b 79 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 2e 0a  y table column..
3882c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
3882d 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73  E int sqlite3Fts
3882e 33 45 78 70 72 50 61 72 73 65 28 0a 20 20 73 71  3ExprParse(.  sq
3882f 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
38830 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20  *pTokenizer,    
38831 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 6d    /* Tokenizer m
38832 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20  odule */.  char 
38833 2a 2a 61 7a 43 6f 6c 2c 20 20 20 20 20 20 20 20  **azCol,        
38834 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
38835 2a 20 41 72 72 61 79 20 6f 66 20 63 6f 6c 75 6d  * Array of colum
38836 6e 20 6e 61 6d 65 73 20 66 6f 72 20 66 74 73 33  n names for fts3
38837 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
38838 6e 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20  nCol,           
38839 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3883a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
3883b 72 69 65 73 20 69 6e 20 61 7a 43 6f 6c 5b 5d 20  ries in azCol[] 
3883c 2a 2f 0a 20 20 69 6e 74 20 69 44 65 66 61 75 6c  */.  int iDefaul
3883d 74 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20  tCol,           
3883e 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61           /* Defa
3883f 75 6c 74 20 63 6f 6c 75 6d 6e 20 74 6f 20 71 75  ult column to qu
38840 65 72 79 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ery */.  const c
38841 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 20  har *z, int n,  
38842 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38843 54 65 78 74 20 6f 66 20 4d 41 54 43 48 20 71 75  Text of MATCH qu
38844 65 72 79 20 2a 2f 0a 20 20 46 74 73 33 45 78 70  ery */.  Fts3Exp
38845 72 20 2a 2a 70 70 45 78 70 72 20 20 20 20 20 20  r **ppExpr      
38846 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38847 4f 55 54 3a 20 50 61 72 73 65 64 20 71 75 65 72  OUT: Parsed quer
38848 79 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 29  y structure */.)
38849 7b 0a 20 20 69 6e 74 20 6e 50 61 72 73 65 64 3b  {.  int nParsed;
3884a 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 50 61 72  .  int rc;.  Par
3884b 73 65 43 6f 6e 74 65 78 74 20 73 50 61 72 73 65  seContext sParse
3884c 3b 0a 20 20 73 50 61 72 73 65 2e 70 54 6f 6b 65  ;.  sParse.pToke
3884d 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65 6e 69 7a  nizer = pTokeniz
3884e 65 72 3b 0a 20 20 73 50 61 72 73 65 2e 61 7a 43  er;.  sParse.azC
3884f 6f 6c 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  ol = (const char
38850 20 2a 2a 29 61 7a 43 6f 6c 3b 0a 20 20 73 50 61   **)azCol;.  sPa
38851 72 73 65 2e 6e 43 6f 6c 20 3d 20 6e 43 6f 6c 3b  rse.nCol = nCol;
38852 0a 20 20 73 50 61 72 73 65 2e 69 44 65 66 61 75  .  sParse.iDefau
38853 6c 74 43 6f 6c 20 3d 20 69 44 65 66 61 75 6c 74  ltCol = iDefault
38854 43 6f 6c 3b 0a 20 20 73 50 61 72 73 65 2e 6e 4e  Col;.  sParse.nN
38855 65 73 74 20 3d 20 30 3b 0a 20 20 69 66 28 20 7a  est = 0;.  if( z
38856 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 70 45 78  ==0 ){.    *ppEx
38857 70 72 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75  pr = 0;.    retu
38858 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
38859 7d 0a 20 20 69 66 28 20 6e 3c 30 20 29 7b 0a 20  }.  if( n<0 ){. 
3885a 20 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 29     n = strlen(z)
3885b 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 66 74 73  ;.  }.  rc = fts
3885c 33 45 78 70 72 50 61 72 73 65 28 26 73 50 61 72  3ExprParse(&sPar
3885d 73 65 2c 20 7a 2c 20 6e 2c 20 70 70 45 78 70 72  se, z, n, ppExpr
3885e 2c 20 26 6e 50 61 72 73 65 64 29 3b 0a 0a 20 20  , &nParsed);..  
3885f 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 6d 69 73  /* Check for mis
38860 6d 61 74 63 68 65 64 20 70 61 72 65 6e 74 68 65  matched parenthe
38861 73 69 73 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d  sis */.  if( rc=
38862 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 73 50  =SQLITE_OK && sP
38863 61 72 73 65 2e 6e 4e 65 73 74 20 29 7b 0a 20 20  arse.nNest ){.  
38864 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
38865 52 4f 52 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ROR;.    sqlite3
38866 46 74 73 33 45 78 70 72 46 72 65 65 28 2a 70 70  Fts3ExprFree(*pp
38867 45 78 70 72 29 3b 0a 20 20 20 20 2a 70 70 45 78  Expr);.    *ppEx
38868 70 72 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 72  pr = 0;.  }..  r
38869 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
3886a 2a 2a 20 46 72 65 65 20 61 20 70 61 72 73 65 64  ** Free a parsed
3886b 20 66 74 73 33 20 71 75 65 72 79 20 65 78 70 72   fts3 query expr
3886c 65 73 73 69 6f 6e 20 61 6c 6c 6f 63 61 74 65 64  ession allocated
3886d 20 62 79 20 73 71 6c 69 74 65 33 46 74 73 33 45   by sqlite3Fts3E
3886e 78 70 72 50 61 72 73 65 28 29 2e 0a 2a 2f 0a 53  xprParse()..*/.S
3886f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
38870 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 45 78  id sqlite3Fts3Ex
38871 70 72 46 72 65 65 28 46 74 73 33 45 78 70 72 20  prFree(Fts3Expr 
38872 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a  *p){.  if( p ){.
38873 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 45      sqlite3Fts3E
38874 78 70 72 46 72 65 65 28 70 2d 3e 70 4c 65 66 74  xprFree(p->pLeft
38875 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 46 74  );.    sqlite3Ft
38876 73 33 45 78 70 72 46 72 65 65 28 70 2d 3e 70 52  s3ExprFree(p->pR
38877 69 67 68 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  ight);.    sqlit
38878 65 33 5f 66 72 65 65 28 70 29 3b 0a 20 20 7d 0a  e3_free(p);.  }.
38879 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
3887a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3887b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3887c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3887d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3887e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
3887f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38881 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38882 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
38883 2a 20 45 76 65 72 79 74 68 69 6e 67 20 61 66 74  * Everything aft
38884 65 72 20 74 68 69 73 20 70 6f 69 6e 74 20 69 73  er this point is
38885 20 6a 75 73 74 20 74 65 73 74 20 63 6f 64 65 2e   just test code.
38886 0a 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  .*/..#ifdef SQLI
38887 54 45 5f 54 45 53 54 0a 0a 0a 2f 2a 0a 2a 2a 20  TE_TEST.../*.** 
38888 46 75 6e 63 74 69 6f 6e 20 74 6f 20 71 75 65 72  Function to quer
38889 79 20 74 68 65 20 68 61 73 68 2d 74 61 62 6c 65  y the hash-table
3888a 20 6f 66 20 74 6f 6b 65 6e 69 7a 65 72 73 20 28   of tokenizers (
3888b 73 65 65 20 52 45 41 44 4d 45 2e 74 6f 6b 65 6e  see README.token
3888c 69 7a 65 72 73 29 2e 0a 2a 2f 0a 73 74 61 74 69  izers)..*/.stati
3888d 63 20 69 6e 74 20 71 75 65 72 79 54 65 73 74 54  c int queryTestT
3888e 6f 6b 65 6e 69 7a 65 72 28 0a 20 20 73 71 6c 69  okenizer(.  sqli
3888f 74 65 33 20 2a 64 62 2c 20 0a 20 20 63 6f 6e 73  te3 *db, .  cons
38890 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20  t char *zName,  
38891 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
38892 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
38893 65 20 2a 2a 70 70 0a 29 7b 0a 20 20 69 6e 74 20  e **pp.){.  int 
38894 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  rc;.  sqlite3_st
38895 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 63 6f 6e  mt *pStmt;.  con
38896 73 74 20 63 68 61 72 20 7a 53 71 6c 5b 5d 20 3d  st char zSql[] =
38897 20 22 53 45 4c 45 43 54 20 66 74 73 33 5f 74 6f   "SELECT fts3_to
38898 6b 65 6e 69 7a 65 72 28 3f 29 22 3b 0a 0a 20 20  kenizer(?)";..  
38899 2a 70 70 20 3d 20 30 3b 0a 20 20 72 63 20 3d 20  *pp = 0;.  rc = 
3889a 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
3889b 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c  v2(db, zSql, -1,
3889c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 69   &pStmt, 0);.  i
3889d 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
3889e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
3889f 63 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  c;.  }..  sqlite
388a0 33 5f 62 69 6e 64 5f 74 65 78 74 28 70 53 74 6d  3_bind_text(pStm
388a1 74 2c 20 31 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c  t, 1, zName, -1,
388a2 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
388a3 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 52 4f  .  if( SQLITE_RO
388a4 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65 70 28  W==sqlite3_step(
388a5 70 53 74 6d 74 29 20 29 7b 0a 20 20 20 20 69 66  pStmt) ){.    if
388a6 28 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  ( sqlite3_column
388a7 5f 74 79 70 65 28 70 53 74 6d 74 2c 20 30 29 3d  _type(pStmt, 0)=
388a8 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a  =SQLITE_BLOB ){.
388a9 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 70 2c        memcpy(pp,
388aa 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
388ab 62 6c 6f 62 28 70 53 74 6d 74 2c 20 30 29 2c 20  blob(pStmt, 0), 
388ac 73 69 7a 65 6f 66 28 2a 70 70 29 29 3b 0a 20 20  sizeof(*pp));.  
388ad 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72    }.  }..  retur
388ae 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69  n sqlite3_finali
388af 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a  ze(pStmt);.}../*
388b0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
388b1 6e 20 69 73 20 70 61 72 74 20 6f 66 20 74 68 65  n is part of the
388b2 20 74 65 73 74 20 69 6e 74 65 72 66 61 63 65 20   test interface 
388b3 66 6f 72 20 74 68 65 20 71 75 65 72 79 20 70 61  for the query pa
388b4 72 73 65 72 2e 20 49 74 0a 2a 2a 20 77 72 69 74  rser. It.** writ
388b5 65 73 20 61 20 74 65 78 74 20 72 65 70 72 65 73  es a text repres
388b6 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
388b7 71 75 65 72 79 20 65 78 70 72 65 73 73 69 6f 6e  query expression
388b8 20 70 45 78 70 72 20 69 6e 74 6f 20 74 68 65 0a   pExpr into the.
388b9 2a 2a 20 62 75 66 66 65 72 20 70 6f 69 6e 74 65  ** buffer pointe
388ba 64 20 74 6f 20 62 79 20 61 72 67 75 6d 65 6e 74  d to by argument
388bb 20 7a 42 75 66 2e 20 49 74 20 69 73 20 61 73 73   zBuf. It is ass
388bc 75 6d 65 64 20 74 68 61 74 20 7a 42 75 66 20 69  umed that zBuf i
388bd 73 20 6c 61 72 67 65 20 0a 2a 2a 20 65 6e 6f 75  s large .** enou
388be 67 68 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  gh to store the 
388bf 72 65 71 75 69 72 65 64 20 74 65 78 74 20 72 65  required text re
388c0 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2f  presentation..*/
388c1 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65 78 70  .static void exp
388c2 72 54 6f 53 74 72 69 6e 67 28 46 74 73 33 45 78  rToString(Fts3Ex
388c3 70 72 20 2a 70 45 78 70 72 2c 20 63 68 61 72 20  pr *pExpr, char 
388c4 2a 7a 42 75 66 29 7b 0a 20 20 73 77 69 74 63 68  *zBuf){.  switch
388c5 28 20 70 45 78 70 72 2d 3e 65 54 79 70 65 20 29  ( pExpr->eType )
388c6 7b 0a 20 20 20 20 63 61 73 65 20 46 54 53 51 55  {.    case FTSQU
388c7 45 52 59 5f 50 48 52 41 53 45 3a 20 7b 0a 20 20  ERY_PHRASE: {.  
388c8 20 20 20 20 46 74 73 33 50 68 72 61 73 65 20 2a      Fts3Phrase *
388c9 70 50 68 72 61 73 65 20 3d 20 70 45 78 70 72 2d  pPhrase = pExpr-
388ca 3e 70 50 68 72 61 73 65 3b 0a 20 20 20 20 20 20  >pPhrase;.      
388cb 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 7a 42 75  int i;.      zBu
388cc 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75  f += sprintf(zBu
388cd 66 2c 20 22 50 48 52 41 53 45 20 25 64 20 25 64  f, "PHRASE %d %d
388ce 22 2c 20 70 50 68 72 61 73 65 2d 3e 69 43 6f 6c  ", pPhrase->iCol
388cf 75 6d 6e 2c 20 70 50 68 72 61 73 65 2d 3e 69 73  umn, pPhrase->is
388d0 4e 6f 74 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  Not);.      for(
388d1 69 3d 30 3b 20 69 3c 70 50 68 72 61 73 65 2d 3e  i=0; i<pPhrase->
388d2 6e 54 6f 6b 65 6e 3b 20 69 2b 2b 29 7b 0a 20 20  nToken; i++){.  
388d3 20 20 20 20 20 20 7a 42 75 66 20 2b 3d 20 73 70        zBuf += sp
388d4 72 69 6e 74 66 28 7a 42 75 66 2c 22 20 25 2e 2a  rintf(zBuf," %.*
388d5 73 22 2c 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b  s",pPhrase->aTok
388d6 65 6e 5b 69 5d 2e 6e 2c 70 50 68 72 61 73 65 2d  en[i].n,pPhrase-
388d7 3e 61 54 6f 6b 65 6e 5b 69 5d 2e 7a 29 3b 0a 20  >aToken[i].z);. 
388d8 20 20 20 20 20 20 20 7a 42 75 66 20 2b 3d 20 73         zBuf += s
388d9 70 72 69 6e 74 66 28 7a 42 75 66 2c 22 25 73 22  printf(zBuf,"%s"
388da 2c 20 28 70 50 68 72 61 73 65 2d 3e 61 54 6f 6b  , (pPhrase->aTok
388db 65 6e 5b 69 5d 2e 69 73 50 72 65 66 69 78 3f 22  en[i].isPrefix?"
388dc 2b 22 3a 22 22 29 29 3b 0a 20 20 20 20 20 20 7d  +":""));.      }
388dd 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
388de 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 46     }..    case F
388df 54 53 51 55 45 52 59 5f 4e 45 41 52 3a 0a 20 20  TSQUERY_NEAR:.  
388e0 20 20 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69      zBuf += spri
388e1 6e 74 66 28 7a 42 75 66 2c 20 22 4e 45 41 52 2f  ntf(zBuf, "NEAR/
388e2 25 64 20 22 2c 20 70 45 78 70 72 2d 3e 6e 4e 65  %d ", pExpr->nNe
388e3 61 72 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ar);.      break
388e4 3b 0a 20 20 20 20 63 61 73 65 20 46 54 53 51 55  ;.    case FTSQU
388e5 45 52 59 5f 4e 4f 54 3a 0a 20 20 20 20 20 20 7a  ERY_NOT:.      z
388e6 42 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a  Buf += sprintf(z
388e7 42 75 66 2c 20 22 4e 4f 54 20 22 29 3b 0a 20 20  Buf, "NOT ");.  
388e8 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
388e9 61 73 65 20 46 54 53 51 55 45 52 59 5f 41 4e 44  ase FTSQUERY_AND
388ea 3a 0a 20 20 20 20 20 20 7a 42 75 66 20 2b 3d 20  :.      zBuf += 
388eb 73 70 72 69 6e 74 66 28 7a 42 75 66 2c 20 22 41  sprintf(zBuf, "A
388ec 4e 44 20 22 29 3b 0a 20 20 20 20 20 20 62 72 65  ND ");.      bre
388ed 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 46 54 53  ak;.    case FTS
388ee 51 55 45 52 59 5f 4f 52 3a 0a 20 20 20 20 20 20  QUERY_OR:.      
388ef 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74 66 28  zBuf += sprintf(
388f0 7a 42 75 66 2c 20 22 4f 52 20 22 29 3b 0a 20 20  zBuf, "OR ");.  
388f1 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a      break;.  }..
388f2 20 20 7a 42 75 66 20 2b 3d 20 73 70 72 69 6e 74    zBuf += sprint
388f3 66 28 7a 42 75 66 2c 20 22 7b 22 29 3b 0a 20 20  f(zBuf, "{");.  
388f4 65 78 70 72 54 6f 53 74 72 69 6e 67 28 70 45 78  exprToString(pEx
388f5 70 72 2d 3e 70 4c 65 66 74 2c 20 7a 42 75 66 29  pr->pLeft, zBuf)
388f6 3b 0a 20 20 7a 42 75 66 20 2b 3d 20 73 74 72 6c  ;.  zBuf += strl
388f7 65 6e 28 7a 42 75 66 29 3b 0a 20 20 7a 42 75 66  en(zBuf);.  zBuf
388f8 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66   += sprintf(zBuf
388f9 2c 20 22 7d 20 22 29 3b 0a 0a 20 20 7a 42 75 66  , "} ");..  zBuf
388fa 20 2b 3d 20 73 70 72 69 6e 74 66 28 7a 42 75 66   += sprintf(zBuf
388fb 2c 20 22 7b 22 29 3b 0a 20 20 65 78 70 72 54 6f  , "{");.  exprTo
388fc 53 74 72 69 6e 67 28 70 45 78 70 72 2d 3e 70 52  String(pExpr->pR
388fd 69 67 68 74 2c 20 7a 42 75 66 29 3b 0a 20 20 7a  ight, zBuf);.  z
388fe 42 75 66 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 42  Buf += strlen(zB
388ff 75 66 29 3b 0a 20 20 7a 42 75 66 20 2b 3d 20 73  uf);.  zBuf += s
38900 70 72 69 6e 74 66 28 7a 42 75 66 2c 20 22 7d 22  printf(zBuf, "}"
38901 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  );.}../*.** This
38902 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e   is the implemen
38903 74 61 74 69 6f 6e 20 6f 66 20 61 20 73 63 61 6c  tation of a scal
38904 61 72 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  ar SQL function 
38905 75 73 65 64 20 74 6f 20 74 65 73 74 20 74 68 65  used to test the
38906 20 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20   .** expression 
38907 70 61 72 73 65 72 2e 20 49 74 20 73 68 6f 75 6c  parser. It shoul
38908 64 20 62 65 20 63 61 6c 6c 65 64 20 61 73 20 66  d be called as f
38909 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ollows:.**.**   
3890a 66 74 73 33 5f 65 78 70 72 74 65 73 74 28 3c 74  fts3_exprtest(<t
3890b 6f 6b 65 6e 69 7a 65 72 3e 2c 20 3c 65 78 70 72  okenizer>, <expr
3890c 3e 2c 20 3c 63 6f 6c 75 6d 6e 20 31 3e 2c 20 2e  >, <column 1>, .
3890d 2e 2e 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ..);.**.** The f
3890e 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 3c  irst argument, <
3890f 74 6f 6b 65 6e 69 7a 65 72 3e 2c 20 69 73 20 74  tokenizer>, is t
38910 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66  he name of the f
38911 74 73 33 20 74 6f 6b 65 6e 69 7a 65 72 20 75 73  ts3 tokenizer us
38912 65 64 0a 2a 2a 20 74 6f 20 70 61 72 73 65 20 74  ed.** to parse t
38913 68 65 20 71 75 65 72 79 20 65 78 70 72 65 73 73  he query express
38914 69 6f 6e 20 28 73 65 65 20 52 45 41 44 4d 45 2e  ion (see README.
38915 74 6f 6b 65 6e 69 7a 65 72 73 29 2e 20 54 68 65  tokenizers). The
38916 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
38917 0a 2a 2a 20 69 73 20 74 68 65 20 71 75 65 72 79  .** is the query
38918 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 70   expression to p
38919 61 72 73 65 2e 20 45 61 63 68 20 73 75 62 73 65  arse. Each subse
3891a 71 75 65 6e 74 20 61 72 67 75 6d 65 6e 74 20 69  quent argument i
3891b 73 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66  s the name.** of
3891c 20 61 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65   a column of the
3891d 20 66 74 73 33 20 74 61 62 6c 65 20 74 68 61 74   fts3 table that
3891e 20 74 68 65 20 71 75 65 72 79 20 65 78 70 72 65   the query expre
3891f 73 73 69 6f 6e 20 6d 61 79 20 72 65 66 65 72 20  ssion may refer 
38920 74 6f 2e 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70  to..** For examp
38921 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45  le:.**.**   SELE
38922 43 54 20 66 74 73 33 5f 65 78 70 72 74 65 73 74  CT fts3_exprtest
38923 28 27 73 69 6d 70 6c 65 27 2c 20 27 42 69 6c 6c  ('simple', 'Bill
38924 20 63 6f 6c 32 3a 42 6c 6f 67 67 73 27 2c 20 27   col2:Bloggs', '
38925 63 6f 6c 31 27 2c 20 27 63 6f 6c 32 27 29 3b 0a  col1', 'col2');.
38926 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
38927 74 73 33 45 78 70 72 54 65 73 74 28 0a 20 20 73  ts3ExprTest(.  s
38928 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
38929 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
3892a 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
3892b 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
3892c 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
3892d 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 20  er_module const 
3892e 2a 70 4d 6f 64 75 6c 65 20 3d 20 30 3b 0a 20 20  *pModule = 0;.  
3892f 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
38930 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20  r *pTokenizer = 
38931 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 63  0;.  int rc;.  c
38932 68 61 72 20 2a 2a 61 7a 43 6f 6c 20 3d 20 30 3b  har **azCol = 0;
38933 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
38934 45 78 70 72 3b 0a 20 20 69 6e 74 20 6e 45 78 70  Expr;.  int nExp
38935 72 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20  r;.  int nCol;. 
38936 20 69 6e 74 20 69 69 3b 0a 20 20 46 74 73 33 45   int ii;.  Fts3E
38937 78 70 72 20 2a 70 45 78 70 72 3b 0a 20 20 73 71  xpr *pExpr;.  sq
38938 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69  lite3 *db = sqli
38939 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
3893a 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a  andle(context);.
3893b 0a 20 20 69 66 28 20 61 72 67 63 3c 33 20 29 7b  .  if( argc<3 ){
3893c 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
3893d 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78  ult_error(contex
3893e 74 2c 20 0a 20 20 20 20 20 20 20 20 22 55 73 61  t, .        "Usa
3893f 67 65 3a 20 66 74 73 33 5f 65 78 70 72 74 65 73  ge: fts3_exprtes
38940 74 28 74 6f 6b 65 6e 69 7a 65 72 2c 20 65 78 70  t(tokenizer, exp
38941 72 2c 20 63 6f 6c 31 2c 20 2e 2e 2e 22 2c 20 2d  r, col1, ...", -
38942 31 0a 20 20 20 20 29 3b 0a 20 20 20 20 72 65 74  1.    );.    ret
38943 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d  urn;.  }..  rc =
38944 20 71 75 65 72 79 54 65 73 74 54 6f 6b 65 6e 69   queryTestTokeni
38945 7a 65 72 28 64 62 2c 0a 20 20 20 20 20 20 20 20  zer(db,.        
38946 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38947 20 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29    (const char *)
38948 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
38949 78 74 28 61 72 67 76 5b 30 5d 29 2c 20 26 70 4d  xt(argv[0]), &pM
3894a 6f 64 75 6c 65 29 3b 0a 20 20 69 66 28 20 72 63  odule);.  if( rc
3894b 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
3894c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  {.    sqlite3_re
3894d 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d  sult_error_nomem
3894e 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 67  (context);.    g
3894f 6f 74 6f 20 65 78 70 72 74 65 73 74 5f 6f 75 74  oto exprtest_out
38950 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70  ;.  }else if( !p
38951 4d 6f 64 75 6c 65 20 29 7b 0a 20 20 20 20 73 71  Module ){.    sq
38952 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
38953 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 4e 6f 20  or(context, "No 
38954 73 75 63 68 20 74 6f 6b 65 6e 69 7a 65 72 20 6d  such tokenizer m
38955 6f 64 75 6c 65 22 2c 20 2d 31 29 3b 0a 20 20 20  odule", -1);.   
38956 20 67 6f 74 6f 20 65 78 70 72 74 65 73 74 5f 6f   goto exprtest_o
38957 75 74 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20  ut;.  }..  rc = 
38958 70 4d 6f 64 75 6c 65 2d 3e 78 43 72 65 61 74 65  pModule->xCreate
38959 28 30 2c 20 30 2c 20 26 70 54 6f 6b 65 6e 69 7a  (0, 0, &pTokeniz
3895a 65 72 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  er);.  assert( r
3895b 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  c==SQLITE_NOMEM 
3895c 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  || rc==SQLITE_OK
3895d 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   );.  if( rc==SQ
3895e 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  LITE_NOMEM ){.  
3895f 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
38960 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e  _error_nomem(con
38961 74 65 78 74 29 3b 0a 20 20 20 20 67 6f 74 6f 20  text);.    goto 
38962 65 78 70 72 74 65 73 74 5f 6f 75 74 3b 0a 20 20  exprtest_out;.  
38963 7d 0a 20 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e  }.  pTokenizer->
38964 70 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64 75 6c  pModule = pModul
38965 65 3b 0a 0a 20 20 7a 45 78 70 72 20 3d 20 28 63  e;..  zExpr = (c
38966 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69  onst char *)sqli
38967 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
38968 72 67 76 5b 31 5d 29 3b 0a 20 20 6e 45 78 70 72  rgv[1]);.  nExpr
38969 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
3896a 5f 62 79 74 65 73 28 61 72 67 76 5b 31 5d 29 3b  _bytes(argv[1]);
3896b 0a 20 20 6e 43 6f 6c 20 3d 20 61 72 67 63 2d 32  .  nCol = argc-2
3896c 3b 0a 20 20 61 7a 43 6f 6c 20 3d 20 28 63 68 61  ;.  azCol = (cha
3896d 72 20 2a 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  r **)sqlite3_mal
3896e 6c 6f 63 28 6e 43 6f 6c 2a 73 69 7a 65 6f 66 28  loc(nCol*sizeof(
3896f 63 68 61 72 20 2a 29 29 3b 0a 20 20 69 66 28 20  char *));.  if( 
38970 21 61 7a 43 6f 6c 20 29 7b 0a 20 20 20 20 73 71  !azCol ){.    sq
38971 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
38972 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74  or_nomem(context
38973 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 70 72  );.    goto expr
38974 74 65 73 74 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20  test_out;.  }.  
38975 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 6f  for(ii=0; ii<nCo
38976 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 61 7a  l; ii++){.    az
38977 43 6f 6c 5b 69 69 5d 20 3d 20 28 63 68 61 72 20  Col[ii] = (char 
38978 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
38979 74 65 78 74 28 61 72 67 76 5b 69 69 2b 32 5d 29  text(argv[ii+2])
3897a 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71  ;.  }..  rc = sq
3897b 6c 69 74 65 33 46 74 73 33 45 78 70 72 50 61 72  lite3Fts3ExprPar
3897c 73 65 28 0a 20 20 20 20 20 20 70 54 6f 6b 65 6e  se(.      pToken
3897d 69 7a 65 72 2c 20 61 7a 43 6f 6c 2c 20 6e 43 6f  izer, azCol, nCo
3897e 6c 2c 20 6e 43 6f 6c 2c 20 7a 45 78 70 72 2c 20  l, nCol, zExpr, 
3897f 6e 45 78 70 72 2c 20 26 70 45 78 70 72 0a 20 20  nExpr, &pExpr.  
38980 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
38981 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20  ITE_NOMEM ){.   
38982 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
38983 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74  error_nomem(cont
38984 65 78 74 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  ext);.    goto e
38985 78 70 72 74 65 73 74 5f 6f 75 74 3b 0a 20 20 7d  xprtest_out;.  }
38986 65 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c  else if( rc==SQL
38987 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 63 68  ITE_OK ){.    ch
38988 61 72 20 7a 42 75 66 5b 34 30 39 36 5d 3b 0a 20  ar zBuf[4096];. 
38989 20 20 20 65 78 70 72 54 6f 53 74 72 69 6e 67 28     exprToString(
3898a 70 45 78 70 72 2c 20 7a 42 75 66 29 3b 0a 20 20  pExpr, zBuf);.  
3898b 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
3898c 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a  _text(context, z
3898d 42 75 66 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  Buf, -1, SQLITE_
3898e 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20  TRANSIENT);.    
3898f 73 71 6c 69 74 65 33 46 74 73 33 45 78 70 72 46  sqlite3Fts3ExprF
38990 72 65 65 28 70 45 78 70 72 29 3b 0a 20 20 7d 65  ree(pExpr);.  }e
38991 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
38992 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
38993 6e 74 65 78 74 2c 20 22 45 72 72 6f 72 20 70 61  ntext, "Error pa
38994 72 73 69 6e 67 20 65 78 70 72 65 73 73 69 6f 6e  rsing expression
38995 22 2c 20 2d 31 29 3b 0a 20 20 7d 0a 0a 65 78 70  ", -1);.  }..exp
38996 72 74 65 73 74 5f 6f 75 74 3a 0a 20 20 69 66 28  rtest_out:.  if(
38997 20 70 4d 6f 64 75 6c 65 20 26 26 20 70 54 6f 6b   pModule && pTok
38998 65 6e 69 7a 65 72 20 29 7b 0a 20 20 20 20 72 63  enizer ){.    rc
38999 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 44 65 73   = pModule->xDes
3899a 74 72 6f 79 28 70 54 6f 6b 65 6e 69 7a 65 72 29  troy(pTokenizer)
3899b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
3899c 66 72 65 65 28 61 7a 43 6f 6c 29 3b 0a 7d 0a 0a  free(azCol);.}..
3899d 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 74  /*.** Register t
3899e 68 65 20 71 75 65 72 79 20 65 78 70 72 65 73 73  he query express
3899f 69 6f 6e 20 70 61 72 73 65 72 20 74 65 73 74 20  ion parser test 
389a0 66 75 6e 63 74 69 6f 6e 20 66 74 73 33 5f 65 78  function fts3_ex
389a1 70 72 74 65 73 74 28 29 20 0a 2a 2a 20 77 69 74  prtest() .** wit
389a2 68 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  h database conne
389a3 63 74 69 6f 6e 20 64 62 2e 20 0a 2a 2f 0a 53 51  ction db. .*/.SQ
389a4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
389a5 64 20 73 71 6c 69 74 65 33 46 74 73 33 45 78 70  d sqlite3Fts3Exp
389a6 72 49 6e 69 74 54 65 73 74 49 6e 74 65 72 66 61  rInitTestInterfa
389a7 63 65 28 73 71 6c 69 74 65 33 2a 20 64 62 29 7b  ce(sqlite3* db){
389a8 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  .  sqlite3_creat
389a9 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20 20 20  e_function(.    
389aa 20 20 64 62 2c 20 22 66 74 73 33 5f 65 78 70 72    db, "fts3_expr
389ab 74 65 73 74 22 2c 20 2d 31 2c 20 53 51 4c 49 54  test", -1, SQLIT
389ac 45 5f 55 54 46 38 2c 20 30 2c 20 66 74 73 33 45  E_UTF8, 0, fts3E
389ad 78 70 72 54 65 73 74 2c 20 30 2c 20 30 0a 20 20  xprTest, 0, 0.  
389ae 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 0a 23 65 6e  );.}..#endif.#en
389af 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28  dif /* !defined(
389b0 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
389b1 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
389b2 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a  NABLE_FTS3) */..
389b3 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
389b4 45 6e 64 20 6f 66 20 66 74 73 33 5f 65 78 70 72  End of fts3_expr
389b5 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
389b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
389b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
389b8 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
389b9 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73 33 5f  Begin file fts3_
389ba 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  hash.c *********
389bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
389bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
389bd 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
389be 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68  mber 22.**.** Th
389bf 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
389c0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
389c1 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
389c2 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
389c3 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
389c4 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
389c5 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
389c6 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
389c7 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
389c8 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
389c9 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
389ca 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
389cb 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
389cc 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
389cd 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
389ce 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
389cf 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
389d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
389d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
389d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
389d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
389d4 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ******.** This i
389d5 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61  s the implementa
389d6 74 69 6f 6e 20 6f 66 20 67 65 6e 65 72 69 63 20  tion of generic 
389d7 68 61 73 68 2d 74 61 62 6c 65 73 20 75 73 65 64  hash-tables used
389d8 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 57   in SQLite..** W
389d9 65 27 76 65 20 6d 6f 64 69 66 69 65 64 20 69 74  e've modified it
389da 20 73 6c 69 67 68 74 6c 79 20 74 6f 20 73 65 72   slightly to ser
389db 76 65 20 61 73 20 61 20 73 74 61 6e 64 61 6c 6f  ve as a standalo
389dc 6e 65 20 68 61 73 68 20 74 61 62 6c 65 0a 2a 2a  ne hash table.**
389dd 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
389de 66 6f 72 20 74 68 65 20 66 75 6c 6c 2d 74 65 78  for the full-tex
389df 74 20 69 6e 64 65 78 69 6e 67 20 6d 6f 64 75 6c  t indexing modul
389e0 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  e..*/../*.** The
389e1 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69   code in this fi
389e2 6c 65 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69  le is only compi
389e3 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20  led if:.**.**   
389e4 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64    * The FTS3 mod
389e5 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75 69  ule is being bui
389e6 6c 74 20 61 73 20 61 6e 20 65 78 74 65 6e 73 69  lt as an extensi
389e7 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 28 69 6e 20  on.**       (in 
389e8 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54  which case SQLIT
389e9 45 5f 43 4f 52 45 20 69 73 20 6e 6f 74 20 64 65  E_CORE is not de
389ea 66 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a  fined), or.**.**
389eb 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33 20       * The FTS3 
389ec 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20  module is being 
389ed 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 63  built into the c
389ee 6f 72 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20  ore of.**       
389ef 53 51 4c 69 74 65 20 28 69 6e 20 77 68 69 63 68  SQLite (in which
389f0 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45 4e 41   case SQLITE_ENA
389f1 42 4c 45 5f 46 54 53 33 20 69 73 20 64 65 66 69  BLE_FTS3 is defi
389f2 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65  ned)..*/.#if !de
389f3 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
389f4 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
389f5 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
389f6 29 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f  )..../*.** Mallo
389f7 63 20 61 6e 64 20 46 72 65 65 20 66 75 6e 63 74  c and Free funct
389f8 69 6f 6e 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ions.*/.static v
389f9 6f 69 64 20 2a 66 74 73 33 48 61 73 68 4d 61 6c  oid *fts3HashMal
389fa 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f  loc(int n){.  vo
389fb 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 5f  id *p = sqlite3_
389fc 6d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 69 66 28  malloc(n);.  if(
389fd 20 70 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74   p ){.    memset
389fe 28 70 2c 20 30 2c 20 6e 29 3b 0a 20 20 7d 0a 20  (p, 0, n);.  }. 
389ff 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 73 74 61   return p;.}.sta
38a00 74 69 63 20 76 6f 69 64 20 66 74 73 33 48 61 73  tic void fts3Has
38a01 68 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a  hFree(void *p){.
38a02 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
38a03 29 3b 0a 7d 0a 0a 2f 2a 20 54 75 72 6e 20 62 75  );.}../* Turn bu
38a04 6c 6b 20 6d 65 6d 6f 72 79 20 69 6e 74 6f 20 61  lk memory into a
38a05 20 68 61 73 68 20 74 61 62 6c 65 20 6f 62 6a 65   hash table obje
38a06 63 74 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69  ct by initializi
38a07 6e 67 20 74 68 65 0a 2a 2a 20 66 69 65 6c 64 73  ng the.** fields
38a08 20 6f 66 20 74 68 65 20 48 61 73 68 20 73 74 72   of the Hash str
38a09 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 22 70  ucture..**.** "p
38a0a 4e 65 77 22 20 69 73 20 61 20 70 6f 69 6e 74 65  New" is a pointe
38a0b 72 20 74 6f 20 74 68 65 20 68 61 73 68 20 74 61  r to the hash ta
38a0c 62 6c 65 20 74 68 61 74 20 69 73 20 74 6f 20 62  ble that is to b
38a0d 65 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a  e initialized..*
38a0e 2a 20 6b 65 79 43 6c 61 73 73 20 69 73 20 6f 6e  * keyClass is on
38a0f 65 20 6f 66 20 74 68 65 20 63 6f 6e 73 74 61 6e  e of the constan
38a10 74 73 20 0a 2a 2a 20 46 54 53 33 5f 48 41 53 48  ts .** FTS3_HASH
38a11 5f 42 49 4e 41 52 59 20 6f 72 20 46 54 53 33 5f  _BINARY or FTS3_
38a12 48 41 53 48 5f 53 54 52 49 4e 47 2e 20 20 54 68  HASH_STRING.  Th
38a13 65 20 76 61 6c 75 65 20 6f 66 20 6b 65 79 43 6c  e value of keyCl
38a14 61 73 73 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e  ass .** determin
38a15 65 73 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20  es what kind of 
38a16 6b 65 79 20 74 68 65 20 68 61 73 68 20 74 61 62  key the hash tab
38a17 6c 65 20 77 69 6c 6c 20 75 73 65 2e 20 20 22 63  le will use.  "c
38a18 6f 70 79 4b 65 79 22 20 69 73 0a 2a 2a 20 74 72  opyKey" is.** tr
38a19 75 65 20 69 66 20 74 68 65 20 68 61 73 68 20 74  ue if the hash t
38a1a 61 62 6c 65 20 73 68 6f 75 6c 64 20 6d 61 6b 65  able should make
38a1b 20 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65   its own private
38a1c 20 63 6f 70 79 20 6f 66 20 6b 65 79 73 20 61 6e   copy of keys an
38a1d 64 0a 2a 2a 20 66 61 6c 73 65 20 69 66 20 69 74  d.** false if it
38a1e 20 73 68 6f 75 6c 64 20 6a 75 73 74 20 75 73 65   should just use
38a1f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 70 6f   the supplied po
38a20 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  inter..*/.SQLITE
38a21 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
38a22 6c 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 69  lite3Fts3HashIni
38a23 74 28 66 74 73 33 48 61 73 68 20 2a 70 4e 65 77  t(fts3Hash *pNew
38a24 2c 20 69 6e 74 20 6b 65 79 43 6c 61 73 73 2c 20  , int keyClass, 
38a25 69 6e 74 20 63 6f 70 79 4b 65 79 29 7b 0a 20 20  int copyKey){.  
38a26 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30 20  assert( pNew!=0 
38a27 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6b 65 79  );.  assert( key
38a28 43 6c 61 73 73 3e 3d 46 54 53 33 5f 48 41 53 48  Class>=FTS3_HASH
38a29 5f 53 54 52 49 4e 47 20 26 26 20 6b 65 79 43 6c  _STRING && keyCl
38a2a 61 73 73 3c 3d 46 54 53 33 5f 48 41 53 48 5f 42  ass<=FTS3_HASH_B
38a2b 49 4e 41 52 59 20 29 3b 0a 20 20 70 4e 65 77 2d  INARY );.  pNew-
38a2c 3e 6b 65 79 43 6c 61 73 73 20 3d 20 6b 65 79 43  >keyClass = keyC
38a2d 6c 61 73 73 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f  lass;.  pNew->co
38a2e 70 79 4b 65 79 20 3d 20 63 6f 70 79 4b 65 79 3b  pyKey = copyKey;
38a2f 0a 20 20 70 4e 65 77 2d 3e 66 69 72 73 74 20 3d  .  pNew->first =
38a30 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 75 6e   0;.  pNew->coun
38a31 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 68  t = 0;.  pNew->h
38a32 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 70 4e 65  tsize = 0;.  pNe
38a33 77 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  w->ht = 0;.}../*
38a34 20 52 65 6d 6f 76 65 20 61 6c 6c 20 65 6e 74 72   Remove all entr
38a35 69 65 73 20 66 72 6f 6d 20 61 20 68 61 73 68 20  ies from a hash 
38a36 74 61 62 6c 65 2e 20 20 52 65 63 6c 61 69 6d 20  table.  Reclaim 
38a37 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 43  all memory..** C
38a38 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  all this routine
38a39 20 74 6f 20 64 65 6c 65 74 65 20 61 20 68 61 73   to delete a has
38a3a 68 20 74 61 62 6c 65 20 6f 72 20 74 6f 20 72 65  h table or to re
38a3b 73 65 74 20 61 20 68 61 73 68 20 74 61 62 6c 65  set a hash table
38a3c 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6d 70 74 79  .** to the empty
38a3d 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54   state..*/.SQLIT
38a3e 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
38a3f 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 43 6c  qlite3Fts3HashCl
38a40 65 61 72 28 66 74 73 33 48 61 73 68 20 2a 70 48  ear(fts3Hash *pH
38a41 29 7b 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65  ){.  fts3HashEle
38a42 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 20  m *elem;        
38a43 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20   /* For looping 
38a44 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e 74  over all element
38a45 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a  s of the table *
38a46 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 48 21  /..  assert( pH!
38a47 3d 30 20 29 3b 0a 20 20 65 6c 65 6d 20 3d 20 70  =0 );.  elem = p
38a48 48 2d 3e 66 69 72 73 74 3b 0a 20 20 70 48 2d 3e  H->first;.  pH->
38a49 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 66 74 73  first = 0;.  fts
38a4a 33 48 61 73 68 46 72 65 65 28 70 48 2d 3e 68 74  3HashFree(pH->ht
38a4b 29 3b 0a 20 20 70 48 2d 3e 68 74 20 3d 20 30 3b  );.  pH->ht = 0;
38a4c 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65 20 3d 20  .  pH->htsize = 
38a4d 30 3b 0a 20 20 77 68 69 6c 65 28 20 65 6c 65 6d  0;.  while( elem
38a4e 20 29 7b 0a 20 20 20 20 66 74 73 33 48 61 73 68   ){.    fts3Hash
38a4f 45 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65 6d 20  Elem *next_elem 
38a50 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20  = elem->next;.  
38a51 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b 65    if( pH->copyKe
38a52 79 20 26 26 20 65 6c 65 6d 2d 3e 70 4b 65 79 20  y && elem->pKey 
38a53 29 7b 0a 20 20 20 20 20 20 66 74 73 33 48 61 73  ){.      fts3Has
38a54 68 46 72 65 65 28 65 6c 65 6d 2d 3e 70 4b 65 79  hFree(elem->pKey
38a55 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 74 73  );.    }.    fts
38a56 33 48 61 73 68 46 72 65 65 28 65 6c 65 6d 29 3b  3HashFree(elem);
38a57 0a 20 20 20 20 65 6c 65 6d 20 3d 20 6e 65 78 74  .    elem = next
38a58 5f 65 6c 65 6d 3b 0a 20 20 7d 0a 20 20 70 48 2d  _elem;.  }.  pH-
38a59 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 7d 0a 0a 2f  >count = 0;.}../
38a5a 2a 0a 2a 2a 20 48 61 73 68 20 61 6e 64 20 63 6f  *.** Hash and co
38a5b 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f  mparison functio
38a5c 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 6f 64 65  ns when the mode
38a5d 20 69 73 20 46 54 53 33 5f 48 41 53 48 5f 53 54   is FTS3_HASH_ST
38a5e 52 49 4e 47 0a 2a 2f 0a 73 74 61 74 69 63 20 69  RING.*/.static i
38a5f 6e 74 20 66 74 73 33 53 74 72 48 61 73 68 28 63  nt fts3StrHash(c
38a60 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c  onst void *pKey,
38a61 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 63 6f   int nKey){.  co
38a62 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 63  nst char *z = (c
38a63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65 79  onst char *)pKey
38a64 3b 0a 20 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20  ;.  int h = 0;. 
38a65 20 69 66 28 20 6e 4b 65 79 3c 3d 30 20 29 20 6e   if( nKey<=0 ) n
38a66 4b 65 79 20 3d 20 28 69 6e 74 29 20 73 74 72 6c  Key = (int) strl
38a67 65 6e 28 7a 29 3b 0a 20 20 77 68 69 6c 65 28 20  en(z);.  while( 
38a68 6e 4b 65 79 20 3e 20 30 20 20 29 7b 0a 20 20 20  nKey > 0  ){.   
38a69 20 68 20 3d 20 28 68 3c 3c 33 29 20 5e 20 68 20   h = (h<<3) ^ h 
38a6a 5e 20 2a 7a 2b 2b 3b 0a 20 20 20 20 6e 4b 65 79  ^ *z++;.    nKey
38a6b 2d 2d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  --;.  }.  return
38a6c 20 68 20 26 20 30 78 37 66 66 66 66 66 66 66 3b   h & 0x7fffffff;
38a6d 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74  .}.static int ft
38a6e 73 33 53 74 72 43 6f 6d 70 61 72 65 28 63 6f 6e  s3StrCompare(con
38a6f 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 31 2c 20  st void *pKey1, 
38a70 69 6e 74 20 6e 31 2c 20 63 6f 6e 73 74 20 76 6f  int n1, const vo
38a71 69 64 20 2a 70 4b 65 79 32 2c 20 69 6e 74 20 6e  id *pKey2, int n
38a72 32 29 7b 0a 20 20 69 66 28 20 6e 31 21 3d 6e 32  2){.  if( n1!=n2
38a73 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72   ) return 1;.  r
38a74 65 74 75 72 6e 20 73 74 72 6e 63 6d 70 28 28 63  eturn strncmp((c
38a75 6f 6e 73 74 20 63 68 61 72 2a 29 70 4b 65 79 31  onst char*)pKey1
38a76 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 4b  ,(const char*)pK
38a77 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ey2,n1);.}../*.*
38a78 2a 20 48 61 73 68 20 61 6e 64 20 63 6f 6d 70 61  * Hash and compa
38a79 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20  rison functions 
38a7a 77 68 65 6e 20 74 68 65 20 6d 6f 64 65 20 69 73  when the mode is
38a7b 20 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52   FTS3_HASH_BINAR
38a7c 59 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  Y.*/.static int 
38a7d 66 74 73 33 42 69 6e 48 61 73 68 28 63 6f 6e 73  fts3BinHash(cons
38a7e 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e  t void *pKey, in
38a7f 74 20 6e 4b 65 79 29 7b 0a 20 20 69 6e 74 20 68  t nKey){.  int h
38a80 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 63 68   = 0;.  const ch
38a81 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 63  ar *z = (const c
38a82 68 61 72 20 2a 29 70 4b 65 79 3b 0a 20 20 77 68  har *)pKey;.  wh
38a83 69 6c 65 28 20 6e 4b 65 79 2d 2d 20 3e 20 30 20  ile( nKey-- > 0 
38a84 29 7b 0a 20 20 20 20 68 20 3d 20 28 68 3c 3c 33  ){.    h = (h<<3
38a85 29 20 5e 20 68 20 5e 20 2a 28 7a 2b 2b 29 3b 0a  ) ^ h ^ *(z++);.
38a86 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 20 26    }.  return h &
38a87 20 30 78 37 66 66 66 66 66 66 66 3b 0a 7d 0a 73   0x7fffffff;.}.s
38a88 74 61 74 69 63 20 69 6e 74 20 66 74 73 33 42 69  tatic int fts3Bi
38a89 6e 43 6f 6d 70 61 72 65 28 63 6f 6e 73 74 20 76  nCompare(const v
38a8a 6f 69 64 20 2a 70 4b 65 79 31 2c 20 69 6e 74 20  oid *pKey1, int 
38a8b 6e 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  n1, const void *
38a8c 70 4b 65 79 32 2c 20 69 6e 74 20 6e 32 29 7b 0a  pKey2, int n2){.
38a8d 20 20 69 66 28 20 6e 31 21 3d 6e 32 20 29 20 72    if( n1!=n2 ) r
38a8e 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 74 75 72  eturn 1;.  retur
38a8f 6e 20 6d 65 6d 63 6d 70 28 70 4b 65 79 31 2c 70  n memcmp(pKey1,p
38a90 4b 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a 2f 2a 0a  Key2,n1);.}../*.
38a91 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
38a92 74 65 72 20 74 6f 20 74 68 65 20 61 70 70 72 6f  ter to the appro
38a93 70 72 69 61 74 65 20 68 61 73 68 20 66 75 6e 63  priate hash func
38a94 74 69 6f 6e 20 67 69 76 65 6e 20 74 68 65 20 6b  tion given the k
38a95 65 79 20 63 6c 61 73 73 2e 0a 2a 2a 0a 2a 2a 20  ey class..**.** 
38a96 54 68 65 20 43 20 73 79 6e 74 61 78 20 69 6e 20  The C syntax in 
38a97 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65  this function de
38a98 66 69 6e 69 74 69 6f 6e 20 6d 61 79 20 62 65 20  finition may be 
38a99 75 6e 66 61 6d 69 6c 61 72 20 74 6f 20 73 6f 6d  unfamilar to som
38a9a 65 20 0a 2a 2a 20 70 72 6f 67 72 61 6d 6d 65 72  e .** programmer
38a9b 73 2c 20 73 6f 20 77 65 20 70 72 6f 76 69 64 65  s, so we provide
38a9c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61   the following a
38a9d 64 64 69 74 69 6f 6e 61 6c 20 65 78 70 6c 61 6e  dditional explan
38a9e 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 65  ation:.**.** The
38a9f 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75 6e   name of the fun
38aa0 63 74 69 6f 6e 20 69 73 20 22 66 74 73 48 61 73  ction is "ftsHas
38aa1 68 46 75 6e 63 74 69 6f 6e 22 2e 20 20 54 68 65  hFunction".  The
38aa2 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20   function takes 
38aa3 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 70 61 72 61  a.** single para
38aa4 6d 65 74 65 72 20 22 6b 65 79 43 6c 61 73 73 22  meter "keyClass"
38aa5 2e 20 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  .  The return va
38aa6 6c 75 65 20 6f 66 20 66 74 73 48 61 73 68 46 75  lue of ftsHashFu
38aa7 6e 63 74 69 6f 6e 28 29 0a 2a 2a 20 69 73 20 61  nction().** is a
38aa8 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 6f 74   pointer to anot
38aa9 68 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53  her function.  S
38aaa 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74 68 65  pecifically, the
38aab 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a   return value.**
38aac 20 6f 66 20 66 74 73 48 61 73 68 46 75 6e 63 74   of ftsHashFunct
38aad 69 6f 6e 28 29 20 69 73 20 61 20 70 6f 69 6e 74  ion() is a point
38aae 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e  er to a function
38aaf 20 74 68 61 74 20 74 61 6b 65 73 20 74 77 6f 20   that takes two 
38ab0 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 77 69  parameters.** wi
38ab1 74 68 20 74 79 70 65 73 20 22 63 6f 6e 73 74 20  th types "const 
38ab2 76 6f 69 64 2a 22 20 61 6e 64 20 22 69 6e 74 22  void*" and "int"
38ab3 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e 20   and returns an 
38ab4 22 69 6e 74 22 2e 0a 2a 2f 0a 73 74 61 74 69 63  "int"..*/.static
38ab5 20 69 6e 74 20 28 2a 66 74 73 48 61 73 68 46 75   int (*ftsHashFu
38ab6 6e 63 74 69 6f 6e 28 69 6e 74 20 6b 65 79 43 6c  nction(int keyCl
38ab7 61 73 73 29 29 28 63 6f 6e 73 74 20 76 6f 69 64  ass))(const void
38ab8 2a 2c 69 6e 74 29 7b 0a 20 20 69 66 28 20 6b 65  *,int){.  if( ke
38ab9 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f 48 41 53  yClass==FTS3_HAS
38aba 48 5f 53 54 52 49 4e 47 20 29 7b 0a 20 20 20 20  H_STRING ){.    
38abb 72 65 74 75 72 6e 20 26 66 74 73 33 53 74 72 48  return &fts3StrH
38abc 61 73 68 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ash;.  }else{.  
38abd 20 20 61 73 73 65 72 74 28 20 6b 65 79 43 6c 61    assert( keyCla
38abe 73 73 3d 3d 46 54 53 33 5f 48 41 53 48 5f 42 49  ss==FTS3_HASH_BI
38abf 4e 41 52 59 20 29 3b 0a 20 20 20 20 72 65 74 75  NARY );.    retu
38ac0 72 6e 20 26 66 74 73 33 42 69 6e 48 61 73 68 3b  rn &fts3BinHash;
38ac1 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
38ac2 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
38ac3 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  o the appropriat
38ac4 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 20  e hash function 
38ac5 67 69 76 65 6e 20 74 68 65 20 6b 65 79 20 63 6c  given the key cl
38ac6 61 73 73 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 68  ass..**.** For h
38ac7 65 6c 70 20 69 6e 20 69 6e 74 65 72 70 72 65 74  elp in interpret
38ac8 65 64 20 74 68 65 20 6f 62 73 63 75 72 65 20 43  ed the obscure C
38ac9 20 63 6f 64 65 20 69 6e 20 74 68 65 20 66 75 6e   code in the fun
38aca 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e  ction definition
38acb 2c 0a 2a 2a 20 73 65 65 20 74 68 65 20 68 65 61  ,.** see the hea
38acc 64 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74  der comment on t
38acd 68 65 20 70 72 65 76 69 6f 75 73 20 66 75 6e 63  he previous func
38ace 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
38acf 69 6e 74 20 28 2a 66 74 73 43 6f 6d 70 61 72 65  int (*ftsCompare
38ad0 46 75 6e 63 74 69 6f 6e 28 69 6e 74 20 6b 65 79  Function(int key
38ad1 43 6c 61 73 73 29 29 28 63 6f 6e 73 74 20 76 6f  Class))(const vo
38ad2 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
38ad3 69 64 2a 2c 69 6e 74 29 7b 0a 20 20 69 66 28 20  id*,int){.  if( 
38ad4 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f 48  keyClass==FTS3_H
38ad5 41 53 48 5f 53 54 52 49 4e 47 20 29 7b 0a 20 20  ASH_STRING ){.  
38ad6 20 20 72 65 74 75 72 6e 20 26 66 74 73 33 53 74    return &fts3St
38ad7 72 43 6f 6d 70 61 72 65 3b 0a 20 20 7d 65 6c 73  rCompare;.  }els
38ad8 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6b  e{.    assert( k
38ad9 65 79 43 6c 61 73 73 3d 3d 46 54 53 33 5f 48 41  eyClass==FTS3_HA
38ada 53 48 5f 42 49 4e 41 52 59 20 29 3b 0a 20 20 20  SH_BINARY );.   
38adb 20 72 65 74 75 72 6e 20 26 66 74 73 33 42 69 6e   return &fts3Bin
38adc 43 6f 6d 70 61 72 65 3b 0a 20 20 7d 0a 7d 0a 0a  Compare;.  }.}..
38add 2f 2a 20 4c 69 6e 6b 20 61 6e 20 65 6c 65 6d 65  /* Link an eleme
38ade 6e 74 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68  nt into the hash
38adf 20 74 61 62 6c 65 0a 2a 2f 0a 73 74 61 74 69 63   table.*/.static
38ae0 20 76 6f 69 64 20 66 74 73 33 48 61 73 68 49 6e   void fts3HashIn
38ae1 73 65 72 74 45 6c 65 6d 65 6e 74 28 0a 20 20 66  sertElement(.  f
38ae2 74 73 33 48 61 73 68 20 2a 70 48 2c 20 20 20 20  ts3Hash *pH,    
38ae3 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63          /* The c
38ae4 6f 6d 70 6c 65 74 65 20 68 61 73 68 20 74 61 62  omplete hash tab
38ae5 6c 65 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 5f  le */.  struct _
38ae6 66 74 73 33 68 74 20 2a 70 45 6e 74 72 79 2c 20  fts3ht *pEntry, 
38ae7 20 2f 2a 20 54 68 65 20 65 6e 74 72 79 20 69 6e   /* The entry in
38ae8 74 6f 20 77 68 69 63 68 20 70 4e 65 77 20 69 73  to which pNew is
38ae9 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 66   inserted */.  f
38aea 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70 4e 65  ts3HashElem *pNe
38aeb 77 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65  w       /* The e
38aec 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 69 6e 73  lement to be ins
38aed 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 66 74  erted */.){.  ft
38aee 73 33 48 61 73 68 45 6c 65 6d 20 2a 70 48 65 61  s3HashElem *pHea
38aef 64 3b 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20  d;     /* First 
38af0 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20  element already 
38af1 69 6e 20 70 45 6e 74 72 79 20 2a 2f 0a 20 20 70  in pEntry */.  p
38af2 48 65 61 64 20 3d 20 70 45 6e 74 72 79 2d 3e 63  Head = pEntry->c
38af3 68 61 69 6e 3b 0a 20 20 69 66 28 20 70 48 65 61  hain;.  if( pHea
38af4 64 20 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e  d ){.    pNew->n
38af5 65 78 74 20 3d 20 70 48 65 61 64 3b 0a 20 20 20  ext = pHead;.   
38af6 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 70 48   pNew->prev = pH
38af7 65 61 64 2d 3e 70 72 65 76 3b 0a 20 20 20 20 69  ead->prev;.    i
38af8 66 28 20 70 48 65 61 64 2d 3e 70 72 65 76 20 29  f( pHead->prev )
38af9 7b 20 70 48 65 61 64 2d 3e 70 72 65 76 2d 3e 6e  { pHead->prev->n
38afa 65 78 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20  ext = pNew; }.  
38afb 20 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20    else          
38afc 20 20 20 7b 20 70 48 2d 3e 66 69 72 73 74 20 3d     { pH->first =
38afd 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20 70 48 65   pNew; }.    pHe
38afe 61 64 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b  ad->prev = pNew;
38aff 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e  .  }else{.    pN
38b00 65 77 2d 3e 6e 65 78 74 20 3d 20 70 48 2d 3e 66  ew->next = pH->f
38b01 69 72 73 74 3b 0a 20 20 20 20 69 66 28 20 70 48  irst;.    if( pH
38b02 2d 3e 66 69 72 73 74 20 29 7b 20 70 48 2d 3e 66  ->first ){ pH->f
38b03 69 72 73 74 2d 3e 70 72 65 76 20 3d 20 70 4e 65  irst->prev = pNe
38b04 77 3b 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70  w; }.    pNew->p
38b05 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 70 48 2d  rev = 0;.    pH-
38b06 3e 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20  >first = pNew;. 
38b07 20 7d 0a 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75   }.  pEntry->cou
38b08 6e 74 2b 2b 3b 0a 20 20 70 45 6e 74 72 79 2d 3e  nt++;.  pEntry->
38b09 63 68 61 69 6e 20 3d 20 70 4e 65 77 3b 0a 7d 0a  chain = pNew;.}.
38b0a 0a 0a 2f 2a 20 52 65 73 69 7a 65 20 74 68 65 20  ../* Resize the 
38b0b 68 61 73 68 20 74 61 62 6c 65 20 73 6f 20 74 68  hash table so th
38b0c 61 74 20 69 74 20 63 61 6e 74 61 69 6e 73 20 22  at it cantains "
38b0d 6e 65 77 5f 73 69 7a 65 22 20 62 75 63 6b 65 74  new_size" bucket
38b0e 73 2e 0a 2a 2a 20 22 6e 65 77 5f 73 69 7a 65 22  s..** "new_size"
38b0f 20 6d 75 73 74 20 62 65 20 61 20 70 6f 77 65 72   must be a power
38b10 20 6f 66 20 32 2e 20 20 54 68 65 20 68 61 73 68   of 2.  The hash
38b11 20 74 61 62 6c 65 20 6d 69 67 68 74 20 66 61 69   table might fai
38b12 6c 20 0a 2a 2a 20 74 6f 20 72 65 73 69 7a 65 20  l .** to resize 
38b13 69 66 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28  if sqliteMalloc(
38b14 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74  ) fails..*/.stat
38b15 69 63 20 76 6f 69 64 20 66 74 73 33 52 65 68 61  ic void fts3Reha
38b16 73 68 28 66 74 73 33 48 61 73 68 20 2a 70 48 2c  sh(fts3Hash *pH,
38b17 20 69 6e 74 20 6e 65 77 5f 73 69 7a 65 29 7b 0a   int new_size){.
38b18 20 20 73 74 72 75 63 74 20 5f 66 74 73 33 68 74    struct _fts3ht
38b19 20 2a 6e 65 77 5f 68 74 3b 20 20 20 20 20 20 20   *new_ht;       
38b1a 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20 68 61     /* The new ha
38b1b 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 66 74  sh table */.  ft
38b1c 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d  s3HashElem *elem
38b1d 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d 3b 20 20 2f  , *next_elem;  /
38b1e 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76  * For looping ov
38b1f 65 72 20 65 78 69 73 74 69 6e 67 20 65 6c 65 6d  er existing elem
38b20 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  ents */.  int (*
38b21 78 48 61 73 68 29 28 63 6f 6e 73 74 20 76 6f 69  xHash)(const voi
38b22 64 2a 2c 69 6e 74 29 3b 20 20 20 2f 2a 20 54 68  d*,int);   /* Th
38b23 65 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 20  e hash function 
38b24 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 28 6e  */..  assert( (n
38b25 65 77 5f 73 69 7a 65 20 26 20 28 6e 65 77 5f 73  ew_size & (new_s
38b26 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20  ize-1))==0 );.  
38b27 6e 65 77 5f 68 74 20 3d 20 28 73 74 72 75 63 74  new_ht = (struct
38b28 20 5f 66 74 73 33 68 74 20 2a 29 66 74 73 33 48   _fts3ht *)fts3H
38b29 61 73 68 4d 61 6c 6c 6f 63 28 20 6e 65 77 5f 73  ashMalloc( new_s
38b2a 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74 72 75 63  ize*sizeof(struc
38b2b 74 20 5f 66 74 73 33 68 74 29 20 29 3b 0a 20 20  t _fts3ht) );.  
38b2c 69 66 28 20 6e 65 77 5f 68 74 3d 3d 30 20 29 20  if( new_ht==0 ) 
38b2d 72 65 74 75 72 6e 3b 0a 20 20 66 74 73 33 48 61  return;.  fts3Ha
38b2e 73 68 46 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a  shFree(pH->ht);.
38b2f 20 20 70 48 2d 3e 68 74 20 3d 20 6e 65 77 5f 68    pH->ht = new_h
38b30 74 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65 20  t;.  pH->htsize 
38b31 3d 20 6e 65 77 5f 73 69 7a 65 3b 0a 20 20 78 48  = new_size;.  xH
38b32 61 73 68 20 3d 20 66 74 73 48 61 73 68 46 75 6e  ash = ftsHashFun
38b33 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61  ction(pH->keyCla
38b34 73 73 29 3b 0a 20 20 66 6f 72 28 65 6c 65 6d 3d  ss);.  for(elem=
38b35 70 48 2d 3e 66 69 72 73 74 2c 20 70 48 2d 3e 66  pH->first, pH->f
38b36 69 72 73 74 3d 30 3b 20 65 6c 65 6d 3b 20 65 6c  irst=0; elem; el
38b37 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 29 7b  em = next_elem){
38b38 0a 20 20 20 20 69 6e 74 20 68 20 3d 20 28 2a 78  .    int h = (*x
38b39 48 61 73 68 29 28 65 6c 65 6d 2d 3e 70 4b 65 79  Hash)(elem->pKey
38b3a 2c 20 65 6c 65 6d 2d 3e 6e 4b 65 79 29 20 26 20  , elem->nKey) & 
38b3b 28 6e 65 77 5f 73 69 7a 65 2d 31 29 3b 0a 20 20  (new_size-1);.  
38b3c 20 20 6e 65 78 74 5f 65 6c 65 6d 20 3d 20 65 6c    next_elem = el
38b3d 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20 20 66 74  em->next;.    ft
38b3e 73 33 48 61 73 68 49 6e 73 65 72 74 45 6c 65 6d  s3HashInsertElem
38b3f 65 6e 74 28 70 48 2c 20 26 6e 65 77 5f 68 74 5b  ent(pH, &new_ht[
38b40 68 5d 2c 20 65 6c 65 6d 29 3b 0a 20 20 7d 0a 7d  h], elem);.  }.}
38b41 0a 0a 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69  ../* This functi
38b42 6f 6e 20 28 66 6f 72 20 69 6e 74 65 72 6e 61 6c  on (for internal
38b43 20 75 73 65 20 6f 6e 6c 79 29 20 6c 6f 63 61 74   use only) locat
38b44 65 73 20 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e  es an element in
38b45 20 61 6e 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c   an.** hash tabl
38b46 65 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 74  e that matches t
38b47 68 65 20 67 69 76 65 6e 20 6b 65 79 2e 20 20 54  he given key.  T
38b48 68 65 20 68 61 73 68 20 66 6f 72 20 74 68 69 73  he hash for this
38b49 20 6b 65 79 20 68 61 73 0a 2a 2a 20 61 6c 72 65   key has.** alre
38b4a 61 64 79 20 62 65 65 6e 20 63 6f 6d 70 75 74 65  ady been compute
38b4b 64 20 61 6e 64 20 69 73 20 70 61 73 73 65 64 20  d and is passed 
38b4c 61 73 20 74 68 65 20 34 74 68 20 70 61 72 61 6d  as the 4th param
38b4d 65 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  eter..*/.static 
38b4e 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 66 74  fts3HashElem *ft
38b4f 73 33 46 69 6e 64 45 6c 65 6d 65 6e 74 42 79 48  s3FindElementByH
38b50 61 73 68 28 0a 20 20 63 6f 6e 73 74 20 66 74 73  ash(.  const fts
38b51 33 48 61 73 68 20 2a 70 48 2c 20 2f 2a 20 54 68  3Hash *pH, /* Th
38b52 65 20 70 48 20 74 6f 20 62 65 20 73 65 61 72 63  e pH to be searc
38b53 68 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  hed */.  const v
38b54 6f 69 64 20 2a 70 4b 65 79 2c 20 20 20 2f 2a 20  oid *pKey,   /* 
38b55 54 68 65 20 6b 65 79 20 77 65 20 61 72 65 20 73  The key we are s
38b56 65 61 72 63 68 69 6e 67 20 66 6f 72 20 2a 2f 0a  earching for */.
38b57 20 20 69 6e 74 20 6e 4b 65 79 2c 0a 20 20 69 6e    int nKey,.  in
38b58 74 20 68 20 20 20 20 20 20 20 20 20 20 20 20 20  t h             
38b59 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66 6f    /* The hash fo
38b5a 72 20 74 68 69 73 20 6b 65 79 2e 20 2a 2f 0a 29  r this key. */.)
38b5b 7b 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65 6d  {.  fts3HashElem
38b5c 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 20 20   *elem;         
38b5d 20 20 20 2f 2a 20 55 73 65 64 20 74 6f 20 6c 6f     /* Used to lo
38b5e 6f 70 20 74 68 72 75 20 74 68 65 20 65 6c 65 6d  op thru the elem
38b5f 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e  ent list */.  in
38b60 74 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20  t count;        
38b61 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38b62 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e  Number of elemen
38b63 74 73 20 6c 65 66 74 20 74 6f 20 74 65 73 74 20  ts left to test 
38b64 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6d 70  */.  int (*xComp
38b65 61 72 65 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a  are)(const void*
38b66 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
38b67 2c 69 6e 74 29 3b 20 20 2f 2a 20 63 6f 6d 70 61  ,int);  /* compa
38b68 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 2a  rison function *
38b69 2f 0a 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 20  /..  if( pH->ht 
38b6a 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 5f 66  ){.    struct _f
38b6b 74 73 33 68 74 20 2a 70 45 6e 74 72 79 20 3d 20  ts3ht *pEntry = 
38b6c 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 20 20  &pH->ht[h];.    
38b6d 65 6c 65 6d 20 3d 20 70 45 6e 74 72 79 2d 3e 63  elem = pEntry->c
38b6e 68 61 69 6e 3b 0a 20 20 20 20 63 6f 75 6e 74 20  hain;.    count 
38b6f 3d 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 3b  = pEntry->count;
38b70 0a 20 20 20 20 78 43 6f 6d 70 61 72 65 20 3d 20  .    xCompare = 
38b71 66 74 73 43 6f 6d 70 61 72 65 46 75 6e 63 74 69  ftsCompareFuncti
38b72 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29  on(pH->keyClass)
38b73 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 63 6f 75  ;.    while( cou
38b74 6e 74 2d 2d 20 26 26 20 65 6c 65 6d 20 29 7b 0a  nt-- && elem ){.
38b75 20 20 20 20 20 20 69 66 28 20 28 2a 78 43 6f 6d        if( (*xCom
38b76 70 61 72 65 29 28 65 6c 65 6d 2d 3e 70 4b 65 79  pare)(elem->pKey
38b77 2c 65 6c 65 6d 2d 3e 6e 4b 65 79 2c 70 4b 65 79  ,elem->nKey,pKey
38b78 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b 20 0a 20 20  ,nKey)==0 ){ .  
38b79 20 20 20 20 20 20 72 65 74 75 72 6e 20 65 6c 65        return ele
38b7a 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  m;.      }.     
38b7b 20 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65   elem = elem->ne
38b7c 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  xt;.    }.  }.  
38b7d 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20  return 0;.}../* 
38b7e 52 65 6d 6f 76 65 20 61 20 73 69 6e 67 6c 65 20  Remove a single 
38b7f 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20 68  entry from the h
38b80 61 73 68 20 74 61 62 6c 65 20 67 69 76 65 6e 20  ash table given 
38b81 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 61  a pointer to tha
38b82 74 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 61 6e 64  t.** element and
38b83 20 61 20 68 61 73 68 20 6f 6e 20 74 68 65 20 65   a hash on the e
38b84 6c 65 6d 65 6e 74 27 73 20 6b 65 79 2e 0a 2a 2f  lement's key..*/
38b85 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 74 73  .static void fts
38b86 33 52 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 42 79  3RemoveElementBy
38b87 48 61 73 68 28 0a 20 20 66 74 73 33 48 61 73 68  Hash(.  fts3Hash
38b88 20 2a 70 48 2c 20 20 20 20 20 20 20 20 20 2f 2a   *pH,         /*
38b89 20 54 68 65 20 70 48 20 63 6f 6e 74 61 69 6e 69   The pH containi
38b8a 6e 67 20 22 65 6c 65 6d 22 20 2a 2f 0a 20 20 66  ng "elem" */.  f
38b8b 74 73 33 48 61 73 68 45 6c 65 6d 2a 20 65 6c 65  ts3HashElem* ele
38b8c 6d 2c 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d  m,   /* The elem
38b8d 65 6e 74 20 74 6f 20 62 65 20 72 65 6d 6f 76 65  ent to be remove
38b8e 64 20 66 72 6f 6d 20 74 68 65 20 70 48 20 2a 2f  d from the pH */
38b8f 0a 20 20 69 6e 74 20 68 20 20 20 20 20 20 20 20  .  int h        
38b90 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68           /* Hash
38b91 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 65   value for the e
38b92 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 73  lement */.){.  s
38b93 74 72 75 63 74 20 5f 66 74 73 33 68 74 20 2a 70  truct _fts3ht *p
38b94 45 6e 74 72 79 3b 0a 20 20 69 66 28 20 65 6c 65  Entry;.  if( ele
38b95 6d 2d 3e 70 72 65 76 20 29 7b 0a 20 20 20 20 65  m->prev ){.    e
38b96 6c 65 6d 2d 3e 70 72 65 76 2d 3e 6e 65 78 74 20  lem->prev->next 
38b97 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 20 0a 20  = elem->next; . 
38b98 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 48 2d 3e   }else{.    pH->
38b99 66 69 72 73 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65  first = elem->ne
38b9a 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 65 6c  xt;.  }.  if( el
38b9b 65 6d 2d 3e 6e 65 78 74 20 29 7b 0a 20 20 20 20  em->next ){.    
38b9c 65 6c 65 6d 2d 3e 6e 65 78 74 2d 3e 70 72 65 76  elem->next->prev
38b9d 20 3d 20 65 6c 65 6d 2d 3e 70 72 65 76 3b 0a 20   = elem->prev;. 
38b9e 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d 20 26 70   }.  pEntry = &p
38b9f 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 69 66 28 20  H->ht[h];.  if( 
38ba0 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3d 3d 65  pEntry->chain==e
38ba1 6c 65 6d 20 29 7b 0a 20 20 20 20 70 45 6e 74 72  lem ){.    pEntr
38ba2 79 2d 3e 63 68 61 69 6e 20 3d 20 65 6c 65 6d 2d  y->chain = elem-
38ba3 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 70 45 6e  >next;.  }.  pEn
38ba4 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20 20  try->count--;.  
38ba5 69 66 28 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e  if( pEntry->coun
38ba6 74 3c 3d 30 20 29 7b 0a 20 20 20 20 70 45 6e 74  t<=0 ){.    pEnt
38ba7 72 79 2d 3e 63 68 61 69 6e 20 3d 20 30 3b 0a 20  ry->chain = 0;. 
38ba8 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 70   }.  if( pH->cop
38ba9 79 4b 65 79 20 26 26 20 65 6c 65 6d 2d 3e 70 4b  yKey && elem->pK
38baa 65 79 20 29 7b 0a 20 20 20 20 66 74 73 33 48 61  ey ){.    fts3Ha
38bab 73 68 46 72 65 65 28 65 6c 65 6d 2d 3e 70 4b 65  shFree(elem->pKe
38bac 79 29 3b 0a 20 20 7d 0a 20 20 66 74 73 33 48 61  y);.  }.  fts3Ha
38bad 73 68 46 72 65 65 28 20 65 6c 65 6d 20 29 3b 0a  shFree( elem );.
38bae 20 20 70 48 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a 20    pH->count--;. 
38baf 20 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74 3c 3d   if( pH->count<=
38bb0 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
38bb1 20 70 48 2d 3e 66 69 72 73 74 3d 3d 30 20 29 3b   pH->first==0 );
38bb2 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 48 2d  .    assert( pH-
38bb3 3e 63 6f 75 6e 74 3d 3d 30 20 29 3b 0a 20 20 20  >count==0 );.   
38bb4 20 66 74 73 33 48 61 73 68 43 6c 65 61 72 28 70   fts3HashClear(p
38bb5 48 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 74  H);.  }.}../* At
38bb6 74 65 6d 70 74 20 74 6f 20 6c 6f 63 61 74 65 20  tempt to locate 
38bb7 61 6e 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68  an element of th
38bb8 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48 20  e hash table pH 
38bb9 77 69 74 68 20 61 20 6b 65 79 0a 2a 2a 20 74 68  with a key.** th
38bba 61 74 20 6d 61 74 63 68 65 73 20 70 4b 65 79 2c  at matches pKey,
38bbb 6e 4b 65 79 2e 20 20 52 65 74 75 72 6e 20 74 68  nKey.  Return th
38bbc 65 20 64 61 74 61 20 66 6f 72 20 74 68 69 73 20  e data for this 
38bbd 65 6c 65 6d 65 6e 74 20 69 66 20 69 74 20 69 73  element if it is
38bbe 0a 2a 2a 20 66 6f 75 6e 64 2c 20 6f 72 20 4e 55  .** found, or NU
38bbf 4c 4c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e  LL if there is n
38bc0 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a 53 51 4c 49  o match..*/.SQLI
38bc1 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
38bc2 2a 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68  *sqlite3Fts3Hash
38bc3 46 69 6e 64 28 63 6f 6e 73 74 20 66 74 73 33 48  Find(const fts3H
38bc4 61 73 68 20 2a 70 48 2c 20 63 6f 6e 73 74 20 76  ash *pH, const v
38bc5 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e  oid *pKey, int n
38bc6 4b 65 79 29 7b 0a 20 20 69 6e 74 20 68 3b 20 20  Key){.  int h;  
38bc7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
38bc8 2a 20 41 20 68 61 73 68 20 6f 6e 20 6b 65 79 20  * A hash on key 
38bc9 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65  */.  fts3HashEle
38bca 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 2f 2a 20 54  m *elem;    /* T
38bcb 68 65 20 65 6c 65 6d 65 6e 74 20 74 68 61 74 20  he element that 
38bcc 6d 61 74 63 68 65 73 20 6b 65 79 20 2a 2f 0a 20  matches key */. 
38bcd 20 69 6e 74 20 28 2a 78 48 61 73 68 29 28 63 6f   int (*xHash)(co
38bce 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20  nst void*,int); 
38bcf 20 2f 2a 20 54 68 65 20 68 61 73 68 20 66 75 6e   /* The hash fun
38bd0 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20  ction */..  if( 
38bd1 70 48 3d 3d 30 20 7c 7c 20 70 48 2d 3e 68 74 3d  pH==0 || pH->ht=
38bd2 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
38bd3 20 78 48 61 73 68 20 3d 20 66 74 73 48 61 73 68   xHash = ftsHash
38bd4 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e 6b 65 79  Function(pH->key
38bd5 43 6c 61 73 73 29 3b 0a 20 20 61 73 73 65 72 74  Class);.  assert
38bd6 28 20 78 48 61 73 68 21 3d 30 20 29 3b 0a 20 20  ( xHash!=0 );.  
38bd7 68 20 3d 20 28 2a 78 48 61 73 68 29 28 70 4b 65  h = (*xHash)(pKe
38bd8 79 2c 6e 4b 65 79 29 3b 0a 20 20 61 73 73 65 72  y,nKey);.  asser
38bd9 74 28 20 28 70 48 2d 3e 68 74 73 69 7a 65 20 26  t( (pH->htsize &
38bda 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29   (pH->htsize-1))
38bdb 3d 3d 30 20 29 3b 0a 20 20 65 6c 65 6d 20 3d 20  ==0 );.  elem = 
38bdc 66 74 73 33 46 69 6e 64 45 6c 65 6d 65 6e 74 42  fts3FindElementB
38bdd 79 48 61 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b  yHash(pH,pKey,nK
38bde 65 79 2c 20 68 20 26 20 28 70 48 2d 3e 68 74 73  ey, h & (pH->hts
38bdf 69 7a 65 2d 31 29 29 3b 0a 20 20 72 65 74 75 72  ize-1));.  retur
38be0 6e 20 65 6c 65 6d 20 3f 20 65 6c 65 6d 2d 3e 64  n elem ? elem->d
38be1 61 74 61 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 20 49  ata : 0;.}../* I
38be2 6e 73 65 72 74 20 61 6e 20 65 6c 65 6d 65 6e 74  nsert an element
38be3 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74   into the hash t
38be4 61 62 6c 65 20 70 48 2e 20 20 54 68 65 20 6b 65  able pH.  The ke
38be5 79 20 69 73 20 70 4b 65 79 2c 6e 4b 65 79 0a 2a  y is pKey,nKey.*
38be6 2a 20 61 6e 64 20 74 68 65 20 64 61 74 61 20 69  * and the data i
38be7 73 20 22 64 61 74 61 22 2e 0a 2a 2a 0a 2a 2a 20  s "data"..**.** 
38be8 49 66 20 6e 6f 20 65 6c 65 6d 65 6e 74 20 65 78  If no element ex
38be9 69 73 74 73 20 77 69 74 68 20 61 20 6d 61 74 63  ists with a matc
38bea 68 69 6e 67 20 6b 65 79 2c 20 74 68 65 6e 20 61  hing key, then a
38beb 20 6e 65 77 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20   new.** element 
38bec 69 73 20 63 72 65 61 74 65 64 2e 20 20 41 20 63  is created.  A c
38bed 6f 70 79 20 6f 66 20 74 68 65 20 6b 65 79 20 69  opy of the key i
38bee 73 20 6d 61 64 65 20 69 66 20 74 68 65 20 63 6f  s made if the co
38bef 70 79 4b 65 79 0a 2a 2a 20 66 6c 61 67 20 69 73  pyKey.** flag is
38bf0 20 73 65 74 2e 20 20 4e 55 4c 4c 20 69 73 20 72   set.  NULL is r
38bf1 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  eturned..**.** I
38bf2 66 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d 65 6e  f another elemen
38bf3 74 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  t already exists
38bf4 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6b   with the same k
38bf5 65 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20  ey, then the.** 
38bf6 6e 65 77 20 64 61 74 61 20 72 65 70 6c 61 63 65  new data replace
38bf7 73 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 61  s the old data a
38bf8 6e 64 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20  nd the old data 
38bf9 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20  is returned..** 
38bfa 54 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 63  The key is not c
38bfb 6f 70 69 65 64 20 69 6e 20 74 68 69 73 20 69 6e  opied in this in
38bfc 73 74 61 6e 63 65 2e 20 20 49 66 20 61 20 6d 61  stance.  If a ma
38bfd 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68 65 6e  lloc fails, then
38bfe 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61  .** the new data
38bff 20 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64   is returned and
38c00 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   the hash table 
38c01 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a  is unchanged..**
38c02 0a 2a 2a 20 49 66 20 74 68 65 20 22 64 61 74 61  .** If the "data
38c03 22 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  " parameter to t
38c04 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
38c05 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a  NULL, then the.*
38c06 2a 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72 65 73  * element corres
38c07 70 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65 79 22  ponding to "key"
38c08 20 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d   is removed from
38c09 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e   the hash table.
38c0a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
38c0b 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
38c0c 46 74 73 33 48 61 73 68 49 6e 73 65 72 74 28 0a  Fts3HashInsert(.
38c0d 20 20 66 74 73 33 48 61 73 68 20 2a 70 48 2c 20    fts3Hash *pH, 
38c0e 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 68 61         /* The ha
38c0f 73 68 20 74 61 62 6c 65 20 74 6f 20 69 6e 73 65  sh table to inse
38c10 72 74 20 69 6e 74 6f 20 2a 2f 0a 20 20 63 6f 6e  rt into */.  con
38c11 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 20  st void *pKey,  
38c12 20 20 2f 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a    /* The key */.
38c13 20 20 69 6e 74 20 6e 4b 65 79 2c 20 20 20 20 20    int nKey,     
38c14 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
38c15 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65   of bytes in the
38c16 20 6b 65 79 20 2a 2f 0a 20 20 76 6f 69 64 20 2a   key */.  void *
38c17 64 61 74 61 20 20 20 20 20 20 20 20 20 20 20 2f  data           /
38c18 2a 20 54 68 65 20 64 61 74 61 20 2a 2f 0a 29 7b  * The data */.){
38c19 0a 20 20 69 6e 74 20 68 72 61 77 3b 20 20 20 20  .  int hraw;    
38c1a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
38c1b 52 61 77 20 68 61 73 68 20 76 61 6c 75 65 20 6f  Raw hash value o
38c1c 66 20 74 68 65 20 6b 65 79 20 2a 2f 0a 20 20 69  f the key */.  i
38c1d 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20 20 20  nt h;           
38c1e 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20           /* the 
38c1f 68 61 73 68 20 6f 66 20 74 68 65 20 6b 65 79 20  hash of the key 
38c20 6d 6f 64 75 6c 6f 20 68 61 73 68 20 74 61 62 6c  modulo hash tabl
38c21 65 20 73 69 7a 65 20 2a 2f 0a 20 20 66 74 73 33  e size */.  fts3
38c22 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20  HashElem *elem; 
38c23 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
38c24 20 6c 6f 6f 70 20 74 68 72 75 20 74 68 65 20 65   loop thru the e
38c25 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20  lement list */. 
38c26 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 6e   fts3HashElem *n
38c27 65 77 5f 65 6c 65 6d 3b 20 20 20 2f 2a 20 4e 65  ew_elem;   /* Ne
38c28 77 20 65 6c 65 6d 65 6e 74 20 61 64 64 65 64 20  w element added 
38c29 74 6f 20 74 68 65 20 70 48 20 2a 2f 0a 20 20 69  to the pH */.  i
38c2a 6e 74 20 28 2a 78 48 61 73 68 29 28 63 6f 6e 73  nt (*xHash)(cons
38c2b 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20 20 2f  t void*,int);  /
38c2c 2a 20 54 68 65 20 68 61 73 68 20 66 75 6e 63 74  * The hash funct
38c2d 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ion */..  assert
38c2e 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 78 48 61  ( pH!=0 );.  xHa
38c2f 73 68 20 3d 20 66 74 73 48 61 73 68 46 75 6e 63  sh = ftsHashFunc
38c30 74 69 6f 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73  tion(pH->keyClas
38c31 73 29 3b 0a 20 20 61 73 73 65 72 74 28 20 78 48  s);.  assert( xH
38c32 61 73 68 21 3d 30 20 29 3b 0a 20 20 68 72 61 77  ash!=0 );.  hraw
38c33 20 3d 20 28 2a 78 48 61 73 68 29 28 70 4b 65 79   = (*xHash)(pKey
38c34 2c 20 6e 4b 65 79 29 3b 0a 20 20 61 73 73 65 72  , nKey);.  asser
38c35 74 28 20 28 70 48 2d 3e 68 74 73 69 7a 65 20 26  t( (pH->htsize &
38c36 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29   (pH->htsize-1))
38c37 3d 3d 30 20 29 3b 0a 20 20 68 20 3d 20 68 72 61  ==0 );.  h = hra
38c38 77 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d  w & (pH->htsize-
38c39 31 29 3b 0a 20 20 65 6c 65 6d 20 3d 20 66 74 73  1);.  elem = fts
38c3a 33 46 69 6e 64 45 6c 65 6d 65 6e 74 42 79 48 61  3FindElementByHa
38c3b 73 68 28 70 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c  sh(pH,pKey,nKey,
38c3c 68 29 3b 0a 20 20 69 66 28 20 65 6c 65 6d 20 29  h);.  if( elem )
38c3d 7b 0a 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64 5f  {.    void *old_
38c3e 64 61 74 61 20 3d 20 65 6c 65 6d 2d 3e 64 61 74  data = elem->dat
38c3f 61 3b 0a 20 20 20 20 69 66 28 20 64 61 74 61 3d  a;.    if( data=
38c40 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 74 73 33  =0 ){.      fts3
38c41 52 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 42 79 48  RemoveElementByH
38c42 61 73 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a  ash(pH,elem,h);.
38c43 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
38c44 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 64 61   elem->data = da
38c45 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  ta;.    }.    re
38c46 74 75 72 6e 20 6f 6c 64 5f 64 61 74 61 3b 0a 20  turn old_data;. 
38c47 20 7d 0a 20 20 69 66 28 20 64 61 74 61 3d 3d 30   }.  if( data==0
38c48 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69   ) return 0;.  i
38c49 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 3d 3d 30  f( pH->htsize==0
38c4a 20 29 7b 0a 20 20 20 20 66 74 73 33 52 65 68 61   ){.    fts3Reha
38c4b 73 68 28 70 48 2c 38 29 3b 0a 20 20 20 20 69 66  sh(pH,8);.    if
38c4c 28 20 70 48 2d 3e 68 74 73 69 7a 65 3d 3d 30 20  ( pH->htsize==0 
38c4d 29 7b 0a 20 20 20 20 20 20 70 48 2d 3e 63 6f 75  ){.      pH->cou
38c4e 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65  nt = 0;.      re
38c4f 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 20 20 7d  turn data;.    }
38c50 0a 20 20 7d 0a 20 20 6e 65 77 5f 65 6c 65 6d 20  .  }.  new_elem 
38c51 3d 20 28 66 74 73 33 48 61 73 68 45 6c 65 6d 2a  = (fts3HashElem*
38c52 29 66 74 73 33 48 61 73 68 4d 61 6c 6c 6f 63 28  )fts3HashMalloc(
38c53 20 73 69 7a 65 6f 66 28 66 74 73 33 48 61 73 68   sizeof(fts3Hash
38c54 45 6c 65 6d 29 20 29 3b 0a 20 20 69 66 28 20 6e  Elem) );.  if( n
38c55 65 77 5f 65 6c 65 6d 3d 3d 30 20 29 20 72 65 74  ew_elem==0 ) ret
38c56 75 72 6e 20 64 61 74 61 3b 0a 20 20 69 66 28 20  urn data;.  if( 
38c57 70 48 2d 3e 63 6f 70 79 4b 65 79 20 26 26 20 70  pH->copyKey && p
38c58 4b 65 79 21 3d 30 20 29 7b 0a 20 20 20 20 6e 65  Key!=0 ){.    ne
38c59 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 66  w_elem->pKey = f
38c5a 74 73 33 48 61 73 68 4d 61 6c 6c 6f 63 28 20 6e  ts3HashMalloc( n
38c5b 4b 65 79 20 29 3b 0a 20 20 20 20 69 66 28 20 6e  Key );.    if( n
38c5c 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 3d 3d 30  ew_elem->pKey==0
38c5d 20 29 7b 0a 20 20 20 20 20 20 66 74 73 33 48 61   ){.      fts3Ha
38c5e 73 68 46 72 65 65 28 6e 65 77 5f 65 6c 65 6d 29  shFree(new_elem)
38c5f 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 64  ;.      return d
38c60 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d  ata;.    }.    m
38c61 65 6d 63 70 79 28 28 76 6f 69 64 2a 29 6e 65 77  emcpy((void*)new
38c62 5f 65 6c 65 6d 2d 3e 70 4b 65 79 2c 20 70 4b 65  _elem->pKey, pKe
38c63 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 7d 65 6c 73  y, nKey);.  }els
38c64 65 7b 0a 20 20 20 20 6e 65 77 5f 65 6c 65 6d 2d  e{.    new_elem-
38c65 3e 70 4b 65 79 20 3d 20 28 76 6f 69 64 2a 29 70  >pKey = (void*)p
38c66 4b 65 79 3b 0a 20 20 7d 0a 20 20 6e 65 77 5f 65  Key;.  }.  new_e
38c67 6c 65 6d 2d 3e 6e 4b 65 79 20 3d 20 6e 4b 65 79  lem->nKey = nKey
38c68 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2b 2b 3b  ;.  pH->count++;
38c69 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 75 6e 74  .  if( pH->count
38c6a 20 3e 20 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b   > pH->htsize ){
38c6b 0a 20 20 20 20 66 74 73 33 52 65 68 61 73 68 28  .    fts3Rehash(
38c6c 70 48 2c 70 48 2d 3e 68 74 73 69 7a 65 2a 32 29  pH,pH->htsize*2)
38c6d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
38c6e 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29 3b 0a  pH->htsize>0 );.
38c6f 20 20 61 73 73 65 72 74 28 20 28 70 48 2d 3e 68    assert( (pH->h
38c70 74 73 69 7a 65 20 26 20 28 70 48 2d 3e 68 74 73  tsize & (pH->hts
38c71 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a 20 20  ize-1))==0 );.  
38c72 68 20 3d 20 68 72 61 77 20 26 20 28 70 48 2d 3e  h = hraw & (pH->
38c73 68 74 73 69 7a 65 2d 31 29 3b 0a 20 20 66 74 73  htsize-1);.  fts
38c74 33 48 61 73 68 49 6e 73 65 72 74 45 6c 65 6d 65  3HashInsertEleme
38c75 6e 74 28 70 48 2c 20 26 70 48 2d 3e 68 74 5b 68  nt(pH, &pH->ht[h
38c76 5d 2c 20 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20  ], new_elem);.  
38c77 6e 65 77 5f 65 6c 65 6d 2d 3e 64 61 74 61 20 3d  new_elem->data =
38c78 20 64 61 74 61 3b 0a 20 20 72 65 74 75 72 6e 20   data;.  return 
38c79 30 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  0;.}..#endif /* 
38c7a 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
38c7b 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64  CORE) || defined
38c7c 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46  (SQLITE_ENABLE_F
38c7d 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  TS3) */../******
38c7e 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
38c7f 66 74 73 33 5f 68 61 73 68 2e 63 20 2a 2a 2a 2a  fts3_hash.c ****
38c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38c81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38c82 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
38c83 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
38c84 69 6c 65 20 66 74 73 33 5f 70 6f 72 74 65 72 2e  ile fts3_porter.
38c85 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
38c86 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38c87 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
38c88 30 30 36 20 53 65 70 74 65 6d 62 65 72 20 33 30  006 September 30
38c89 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
38c8a 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
38c8b 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
38c8c 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
38c8d 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
38c8e 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
38c8f 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
38c90 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
38c91 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
38c92 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
38c93 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
38c94 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
38c95 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
38c96 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
38c97 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
38c98 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
38c99 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
38c9a 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
38c9b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38c9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38c9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
38c9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
38c9f 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
38ca0 6e 20 6f 66 20 74 68 65 20 66 75 6c 6c 2d 74 65  n of the full-te
38ca1 78 74 2d 73 65 61 72 63 68 20 74 6f 6b 65 6e 69  xt-search tokeni
38ca2 7a 65 72 20 74 68 61 74 20 69 6d 70 6c 65 6d 65  zer that impleme
38ca3 6e 74 73 0a 2a 2a 20 61 20 50 6f 72 74 65 72 20  nts.** a Porter 
38ca4 73 74 65 6d 6d 65 72 2e 0a 2a 2f 0a 0a 2f 2a 0a  stemmer..*/../*.
38ca5 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74  ** The code in t
38ca6 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79  his file is only
38ca7 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a   compiled if:.**
38ca8 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54  .**     * The FT
38ca9 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69  S3 module is bei
38caa 6e 67 20 62 75 69 6c 74 20 61 73 20 61 6e 20 65  ng built as an e
38cab 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20  xtension.**     
38cac 20 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65    (in which case
38cad 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20   SQLITE_CORE is 
38cae 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72  not defined), or
38caf 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65  .**.**     * The
38cb0 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20   FTS3 module is 
38cb1 62 65 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f  being built into
38cb2 20 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20   the core of.** 
38cb3 20 20 20 20 20 20 53 51 4c 69 74 65 20 28 69 6e        SQLite (in
38cb4 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 49   which case SQLI
38cb5 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 69  TE_ENABLE_FTS3 i
38cb6 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23  s defined)..*/.#
38cb7 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
38cb8 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66 69  TE_CORE) || defi
38cb9 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
38cba 45 5f 46 54 53 33 29 0a 0a 0a 0a 0a 2f 2a 0a 2a  E_FTS3)...../*.*
38cbb 2a 20 43 6c 61 73 73 20 64 65 72 69 76 65 64 20  * Class derived 
38cbc 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 74 6f 6b  from sqlite3_tok
38cbd 65 6e 69 7a 65 72 0a 2a 2f 0a 74 79 70 65 64 65  enizer.*/.typede
38cbe 66 20 73 74 72 75 63 74 20 70 6f 72 74 65 72 5f  f struct porter_
38cbf 74 6f 6b 65 6e 69 7a 65 72 20 7b 0a 20 20 73 71  tokenizer {.  sq
38cc0 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
38cc1 62 61 73 65 3b 20 20 20 20 20 20 2f 2a 20 42 61  base;      /* Ba
38cc2 73 65 20 63 6c 61 73 73 20 2a 2f 0a 7d 20 70 6f  se class */.} po
38cc3 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 3b 0a  rter_tokenizer;.
38cc4 0a 2f 2a 0a 2a 2a 20 43 6c 61 73 73 20 64 65 72  ./*.** Class der
38cc5 69 76 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 33  ived from sqlit3
38cc6 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
38cc7 72 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  r.*/.typedef str
38cc8 75 63 74 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e  uct porter_token
38cc9 69 7a 65 72 5f 63 75 72 73 6f 72 20 7b 0a 20 20  izer_cursor {.  
38cca 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
38ccb 72 5f 63 75 72 73 6f 72 20 62 61 73 65 3b 0a 20  r_cursor base;. 
38ccc 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e   const char *zIn
38ccd 70 75 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  put;          /*
38cce 20 69 6e 70 75 74 20 77 65 20 61 72 65 20 74 6f   input we are to
38ccf 6b 65 6e 69 7a 69 6e 67 20 2a 2f 0a 20 20 69 6e  kenizing */.  in
38cd0 74 20 6e 49 6e 70 75 74 3b 20 20 20 20 20 20 20  t nInput;       
38cd1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 69             /* si
38cd2 7a 65 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20  ze of the input 
38cd3 2a 2f 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74  */.  int iOffset
38cd4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
38cd5 20 20 2f 2a 20 63 75 72 72 65 6e 74 20 70 6f 73    /* current pos
38cd6 69 74 69 6f 6e 20 69 6e 20 7a 49 6e 70 75 74 20  ition in zInput 
38cd7 2a 2f 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e 3b  */.  int iToken;
38cd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
38cd9 20 20 2f 2a 20 69 6e 64 65 78 20 6f 66 20 6e 65    /* index of ne
38cda 78 74 20 74 6f 6b 65 6e 20 74 6f 20 62 65 20 72  xt token to be r
38cdb 65 74 75 72 6e 65 64 20 2a 2f 0a 20 20 63 68 61  eturned */.  cha
38cdc 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20 20 20 20  r *zToken;      
38cdd 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74 6f            /* sto
38cde 72 61 67 65 20 66 6f 72 20 63 75 72 72 65 6e 74  rage for current
38cdf 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20   token */.  int 
38ce0 6e 41 6c 6c 6f 63 61 74 65 64 3b 20 20 20 20 20  nAllocated;     
38ce1 20 20 20 20 20 20 20 20 20 2f 2a 20 73 70 61 63           /* spac
38ce2 65 20 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 7a  e allocated to z
38ce3 54 6f 6b 65 6e 20 62 75 66 66 65 72 20 2a 2f 0a  Token buffer */.
38ce4 7d 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a  } porter_tokeniz
38ce5 65 72 5f 63 75 72 73 6f 72 3b 0a 0a 0a 2f 2a 20  er_cursor;.../* 
38ce6 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74  Forward declarat
38ce7 69 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f  ion */.static co
38ce8 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  nst sqlite3_toke
38ce9 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 70 6f 72  nizer_module por
38cea 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75  terTokenizerModu
38ceb 6c 65 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61  le;.../*.** Crea
38cec 74 65 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a  te a new tokeniz
38ced 65 72 20 69 6e 73 74 61 6e 63 65 2e 0a 2a 2f 0a  er instance..*/.
38cee 73 74 61 74 69 63 20 69 6e 74 20 70 6f 72 74 65  static int porte
38cef 72 43 72 65 61 74 65 28 0a 20 20 69 6e 74 20 61  rCreate(.  int a
38cf0 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  rgc, const char 
38cf1 2a 20 63 6f 6e 73 74 20 2a 61 72 67 76 2c 0a 20  * const *argv,. 
38cf2 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
38cf3 65 72 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72  er **ppTokenizer
38cf4 0a 29 7b 0a 20 20 70 6f 72 74 65 72 5f 74 6f 6b  .){.  porter_tok
38cf5 65 6e 69 7a 65 72 20 2a 74 3b 0a 20 20 74 20 3d  enizer *t;.  t =
38cf6 20 28 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a   (porter_tokeniz
38cf7 65 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61  er *) sqlite3_ma
38cf8 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 74 29 29  lloc(sizeof(*t))
38cf9 3b 0a 20 20 69 66 28 20 74 3d 3d 4e 55 4c 4c 20  ;.  if( t==NULL 
38cfa 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
38cfb 4e 4f 4d 45 4d 3b 0a 20 20 6d 65 6d 73 65 74 28  NOMEM;.  memset(
38cfc 74 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 74 29  t, 0, sizeof(*t)
38cfd 29 3b 0a 20 20 2a 70 70 54 6f 6b 65 6e 69 7a 65  );.  *ppTokenize
38cfe 72 20 3d 20 26 74 2d 3e 62 61 73 65 3b 0a 20 20  r = &t->base;.  
38cff 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
38d00 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72  ;.}../*.** Destr
38d01 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a  oy a tokenizer.*
38d02 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f 72  /.static int por
38d03 74 65 72 44 65 73 74 72 6f 79 28 73 71 6c 69 74  terDestroy(sqlit
38d04 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
38d05 6f 6b 65 6e 69 7a 65 72 29 7b 0a 20 20 73 71 6c  okenizer){.  sql
38d06 69 74 65 33 5f 66 72 65 65 28 70 54 6f 6b 65 6e  ite3_free(pToken
38d07 69 7a 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  izer);.  return 
38d08 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
38d09 0a 2a 2a 20 50 72 65 70 61 72 65 20 74 6f 20 62  .** Prepare to b
38d0a 65 67 69 6e 20 74 6f 6b 65 6e 69 7a 69 6e 67 20  egin tokenizing 
38d0b 61 20 70 61 72 74 69 63 75 6c 61 72 20 73 74 72  a particular str
38d0c 69 6e 67 2e 20 20 54 68 65 20 69 6e 70 75 74 0a  ing.  The input.
38d0d 2a 2a 20 73 74 72 69 6e 67 20 74 6f 20 62 65 20  ** string to be 
38d0e 74 6f 6b 65 6e 69 7a 65 64 20 69 73 20 7a 49 6e  tokenized is zIn
38d0f 70 75 74 5b 30 2e 2e 6e 49 6e 70 75 74 2d 31 5d  put[0..nInput-1]
38d10 2e 20 20 41 20 63 75 72 73 6f 72 0a 2a 2a 20 75  .  A cursor.** u
38d11 73 65 64 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74  sed to increment
38d12 61 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65 20 74 68  ally tokenize th
38d13 69 73 20 73 74 72 69 6e 67 20 69 73 20 72 65 74  is string is ret
38d14 75 72 6e 65 64 20 69 6e 20 0a 2a 2a 20 2a 70 70  urned in .** *pp
38d15 43 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69  Cursor..*/.stati
38d16 63 20 69 6e 74 20 70 6f 72 74 65 72 4f 70 65 6e  c int porterOpen
38d17 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  (.  sqlite3_toke
38d18 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65  nizer *pTokenize
38d19 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  r,         /* Th
38d1a 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20  e tokenizer */. 
38d1b 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e   const char *zIn
38d1c 70 75 74 2c 20 69 6e 74 20 6e 49 6e 70 75 74 2c  put, int nInput,
38d1d 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69 6e          /* Strin
38d1e 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a 65  g to be tokenize
38d1f 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 74  d */.  sqlite3_t
38d20 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
38d21 2a 2a 70 70 43 75 72 73 6f 72 20 20 20 20 2f 2a  **ppCursor    /*
38d22 20 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a 61 74 69   OUT: Tokenizati
38d23 6f 6e 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a  on cursor */.){.
38d24 20 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a    porter_tokeniz
38d25 65 72 5f 63 75 72 73 6f 72 20 2a 63 3b 0a 0a 20  er_cursor *c;.. 
38d26 20 63 20 3d 20 28 70 6f 72 74 65 72 5f 74 6f 6b   c = (porter_tok
38d27 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 29  enizer_cursor *)
38d28 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
38d29 73 69 7a 65 6f 66 28 2a 63 29 29 3b 0a 20 20 69  sizeof(*c));.  i
38d2a 66 28 20 63 3d 3d 4e 55 4c 4c 20 29 20 72 65 74  f( c==NULL ) ret
38d2b 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
38d2c 3b 0a 0a 20 20 63 2d 3e 7a 49 6e 70 75 74 20 3d  ;..  c->zInput =
38d2d 20 7a 49 6e 70 75 74 3b 0a 20 20 69 66 28 20 7a   zInput;.  if( z
38d2e 49 6e 70 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20  Input==0 ){.    
38d2f 63 2d 3e 6e 49 6e 70 75 74 20 3d 20 30 3b 0a 20  c->nInput = 0;. 
38d30 20 7d 65 6c 73 65 20 69 66 28 20 6e 49 6e 70 75   }else if( nInpu
38d31 74 3c 30 20 29 7b 0a 20 20 20 20 63 2d 3e 6e 49  t<0 ){.    c->nI
38d32 6e 70 75 74 20 3d 20 28 69 6e 74 29 73 74 72 6c  nput = (int)strl
38d33 65 6e 28 7a 49 6e 70 75 74 29 3b 0a 20 20 7d 65  en(zInput);.  }e
38d34 6c 73 65 7b 0a 20 20 20 20 63 2d 3e 6e 49 6e 70  lse{.    c->nInp
38d35 75 74 20 3d 20 6e 49 6e 70 75 74 3b 0a 20 20 7d  ut = nInput;.  }
38d36 0a 20 20 63 2d 3e 69 4f 66 66 73 65 74 20 3d 20  .  c->iOffset = 
38d37 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0;              
38d38 20 20 20 2f 2a 20 73 74 61 72 74 20 74 6f 6b 65     /* start toke
38d39 6e 69 7a 69 6e 67 20 61 74 20 74 68 65 20 62 65  nizing at the be
38d3a 67 69 6e 6e 69 6e 67 20 2a 2f 0a 20 20 63 2d 3e  ginning */.  c->
38d3b 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 63 2d  iToken = 0;.  c-
38d3c 3e 7a 54 6f 6b 65 6e 20 3d 20 4e 55 4c 4c 3b 20  >zToken = NULL; 
38d3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
38d3e 20 6e 6f 20 73 70 61 63 65 20 61 6c 6c 6f 63 61   no space alloca
38d3f 74 65 64 2c 20 79 65 74 2e 20 2a 2f 0a 20 20 63  ted, yet. */.  c
38d40 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64 20 3d 20 30  ->nAllocated = 0
38d41 3b 0a 0a 20 20 2a 70 70 43 75 72 73 6f 72 20 3d  ;..  *ppCursor =
38d42 20 26 63 2d 3e 62 61 73 65 3b 0a 20 20 72 65 74   &c->base;.  ret
38d43 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
38d44 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20  ../*.** Close a 
38d45 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72  tokenization cur
38d46 73 6f 72 20 70 72 65 76 69 6f 75 73 6c 79 20 6f  sor previously o
38d47 70 65 6e 65 64 20 62 79 20 61 20 63 61 6c 6c 20  pened by a call 
38d48 74 6f 0a 2a 2a 20 70 6f 72 74 65 72 4f 70 65 6e  to.** porterOpen
38d49 28 29 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 61  () above..*/.sta
38d4a 74 69 63 20 69 6e 74 20 70 6f 72 74 65 72 43 6c  tic int porterCl
38d4b 6f 73 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65  ose(sqlite3_toke
38d4c 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43  nizer_cursor *pC
38d4d 75 72 73 6f 72 29 7b 0a 20 20 70 6f 72 74 65 72  ursor){.  porter
38d4e 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
38d4f 72 20 2a 63 20 3d 20 28 70 6f 72 74 65 72 5f 74  r *c = (porter_t
38d50 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
38d51 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 73 71  *) pCursor;.  sq
38d52 6c 69 74 65 33 5f 66 72 65 65 28 63 2d 3e 7a 54  lite3_free(c->zT
38d53 6f 6b 65 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33  oken);.  sqlite3
38d54 5f 66 72 65 65 28 63 29 3b 0a 20 20 72 65 74 75  _free(c);.  retu
38d55 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
38d56 2f 2a 0a 2a 2a 20 56 6f 77 65 6c 20 6f 72 20 63  /*.** Vowel or c
38d57 6f 6e 73 6f 6e 61 6e 74 0a 2a 2f 0a 73 74 61 74  onsonant.*/.stat
38d58 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 63 54  ic const char cT
38d59 79 70 65 5b 5d 20 3d 20 7b 0a 20 20 20 30 2c 20  ype[] = {.   0, 
38d5a 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31  1, 1, 1, 0, 1, 1
38d5b 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 0, 1, 1, 1,
38d5c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20   1, 1, 0, 1, 1, 
38d5d 31 2c 20 31 2c 20 31 2c 20 30 2c 0a 20 20 20 31  1, 1, 1, 0,.   1
38d5e 2c 20 31 2c 20 31 2c 20 32 2c 20 31 0a 7d 3b 0a  , 1, 1, 2, 1.};.
38d5f 0a 2f 2a 0a 2a 2a 20 69 73 43 6f 6e 73 6f 6e 61  ./*.** isConsona
38d60 6e 74 28 29 20 61 6e 64 20 69 73 56 6f 77 65 6c  nt() and isVowel
38d61 28 29 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20  () determine if 
38d62 74 68 65 69 72 20 66 69 72 73 74 20 63 68 61 72  their first char
38d63 61 63 74 65 72 20 69 6e 0a 2a 2a 20 74 68 65 20  acter in.** the 
38d64 73 74 72 69 6e 67 20 74 68 65 79 20 70 6f 69 6e  string they poin
38d65 74 20 74 6f 20 69 73 20 61 20 63 6f 6e 73 6f 6e  t to is a conson
38d66 61 6e 74 20 6f 72 20 61 20 76 6f 77 65 6c 2c 20  ant or a vowel, 
38d67 61 63 63 6f 72 64 69 6e 67 0a 2a 2a 20 74 6f 20  according.** to 
38d68 50 6f 72 74 65 72 20 72 75 6c 73 2e 20 20 0a 2a  Porter ruls.  .*
38d69 2a 0a 2a 2a 20 41 20 63 6f 6e 73 6f 6e 61 74 65  *.** A consonate
38d6a 20 69 73 20 61 6e 79 20 6c 65 74 74 65 72 20 6f   is any letter o
38d6b 74 68 65 72 20 74 68 61 6e 20 27 61 27 2c 20 27  ther than 'a', '
38d6c 65 27 2c 20 27 69 27 2c 20 27 6f 27 2c 20 6f 72  e', 'i', 'o', or
38d6d 20 27 75 27 2e 0a 2a 2a 20 27 59 27 20 69 73 20   'u'..** 'Y' is 
38d6e 61 20 63 6f 6e 73 6f 6e 61 6e 74 20 75 6e 6c 65  a consonant unle
38d6f 73 73 20 69 74 20 66 6f 6c 6c 6f 77 73 20 61 6e  ss it follows an
38d70 6f 74 68 65 72 20 63 6f 6e 73 6f 6e 61 6e 74 2c  other consonant,
38d71 0a 2a 2a 20 69 6e 20 77 68 69 63 68 20 63 61 73  .** in which cas
38d72 65 20 69 74 20 69 73 20 61 20 76 6f 77 65 6c 2e  e it is a vowel.
38d73 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 73 65 20  .**.** In these 
38d74 72 6f 75 74 69 6e 65 2c 20 74 68 65 20 6c 65 74  routine, the let
38d75 74 65 72 73 20 61 72 65 20 69 6e 20 72 65 76 65  ters are in reve
38d76 72 73 65 20 6f 72 64 65 72 2e 20 20 53 6f 20 74  rse order.  So t
38d77 68 65 20 27 79 27 20 72 75 6c 65 0a 2a 2a 20 69  he 'y' rule.** i
38d78 73 20 74 68 61 74 20 27 79 27 20 69 73 20 61 20  s that 'y' is a 
38d79 63 6f 6e 73 6f 6e 61 6e 74 20 75 6e 6c 65 73 73  consonant unless
38d7a 20 69 74 20 69 73 20 66 6f 6c 6c 6f 77 65 64 20   it is followed 
38d7b 62 79 20 61 6e 6f 74 68 65 72 0a 2a 2a 20 63 6f  by another.** co
38d7c 6e 73 6f 6e 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74  nsonent..*/.stat
38d7d 69 63 20 69 6e 74 20 69 73 56 6f 77 65 6c 28 63  ic int isVowel(c
38d7e 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 73 74 61  onst char*);.sta
38d7f 74 69 63 20 69 6e 74 20 69 73 43 6f 6e 73 6f 6e  tic int isConson
38d80 61 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ant(const char *
38d81 7a 29 7b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 63  z){.  int j;.  c
38d82 68 61 72 20 78 20 3d 20 2a 7a 3b 0a 20 20 69 66  har x = *z;.  if
38d83 28 20 78 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( x==0 ) return 
38d84 30 3b 0a 20 20 61 73 73 65 72 74 28 20 78 3e 3d  0;.  assert( x>=
38d85 27 61 27 20 26 26 20 78 3c 3d 27 7a 27 20 29 3b  'a' && x<='z' );
38d86 0a 20 20 6a 20 3d 20 63 54 79 70 65 5b 78 2d 27  .  j = cType[x-'
38d87 61 27 5d 3b 0a 20 20 69 66 28 20 6a 3c 32 20 29  a'];.  if( j<2 )
38d88 20 72 65 74 75 72 6e 20 6a 3b 0a 20 20 72 65 74   return j;.  ret
38d89 75 72 6e 20 7a 5b 31 5d 3d 3d 30 20 7c 7c 20 69  urn z[1]==0 || i
38d8a 73 56 6f 77 65 6c 28 7a 20 2b 20 31 29 3b 0a 7d  sVowel(z + 1);.}
38d8b 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 56 6f  .static int isVo
38d8c 77 65 6c 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  wel(const char *
38d8d 7a 29 7b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 63  z){.  int j;.  c
38d8e 68 61 72 20 78 20 3d 20 2a 7a 3b 0a 20 20 69 66  har x = *z;.  if
38d8f 28 20 78 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( x==0 ) return 
38d90 30 3b 0a 20 20 61 73 73 65 72 74 28 20 78 3e 3d  0;.  assert( x>=
38d91 27 61 27 20 26 26 20 78 3c 3d 27 7a 27 20 29 3b  'a' && x<='z' );
38d92 0a 20 20 6a 20 3d 20 63 54 79 70 65 5b 78 2d 27  .  j = cType[x-'
38d93 61 27 5d 3b 0a 20 20 69 66 28 20 6a 3c 32 20 29  a'];.  if( j<2 )
38d94 20 72 65 74 75 72 6e 20 31 2d 6a 3b 0a 20 20 72   return 1-j;.  r
38d95 65 74 75 72 6e 20 69 73 43 6f 6e 73 6f 6e 61 6e  eturn isConsonan
38d96 74 28 7a 20 2b 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a  t(z + 1);.}../*.
38d97 2a 2a 20 4c 65 74 20 61 6e 79 20 73 65 71 75 65  ** Let any seque
38d98 6e 63 65 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f  nce of one or mo
38d99 72 65 20 76 6f 77 65 6c 73 20 62 65 20 72 65 70  re vowels be rep
38d9a 72 65 73 65 6e 74 65 64 20 62 79 20 56 20 61 6e  resented by V an
38d9b 64 20 6c 65 74 0a 2a 2a 20 43 20 62 65 20 73 65  d let.** C be se
38d9c 71 75 65 6e 63 65 20 6f 66 20 6f 6e 65 20 6f 72  quence of one or
38d9d 20 6d 6f 72 65 20 63 6f 6e 73 6f 6e 61 6e 74 73   more consonants
38d9e 2e 20 20 54 68 65 6e 20 65 76 65 72 79 20 77 6f  .  Then every wo
38d9f 72 64 20 63 61 6e 20 62 65 0a 2a 2a 20 72 65 70  rd can be.** rep
38da0 72 65 73 65 6e 74 65 64 20 61 73 3a 0a 2a 2a 0a  resented as:.**.
38da1 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 43 5d  **           [C]
38da2 20 28 56 43 29 7b 6d 7d 20 5b 56 5d 0a 2a 2a 0a   (VC){m} [V].**.
38da3 2a 2a 20 49 6e 20 70 72 6f 73 65 3a 20 20 41 20  ** In prose:  A 
38da4 77 6f 72 64 20 69 73 20 61 6e 20 6f 70 74 69 6f  word is an optio
38da5 6e 61 6c 20 63 6f 6e 73 6f 6e 61 6e 74 20 66 6f  nal consonant fo
38da6 6c 6c 6f 77 65 64 20 62 79 20 7a 65 72 6f 20 6f  llowed by zero o
38da7 72 0a 2a 2a 20 76 6f 77 65 6c 2d 63 6f 6e 73 6f  r.** vowel-conso
38da8 6e 61 6e 74 20 70 61 69 72 73 20 66 6f 6c 6c 6f  nant pairs follo
38da9 77 65 64 20 62 79 20 61 6e 20 6f 70 74 69 6f 6e  wed by an option
38daa 61 6c 20 76 6f 77 65 6c 2e 20 20 22 6d 22 20 69  al vowel.  "m" i
38dab 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20  s the.** number 
38dac 6f 66 20 76 6f 77 65 6c 20 63 6f 6e 73 6f 6e 61  of vowel consona
38dad 6e 74 20 70 61 69 72 73 2e 20 20 54 68 69 73 20  nt pairs.  This 
38dae 72 6f 75 74 69 6e 65 20 63 6f 6d 70 75 74 65 73  routine computes
38daf 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66   the value.** of
38db0 20 6d 20 66 6f 72 20 74 68 65 20 66 69 72 73 74   m for the first
38db1 20 69 20 62 79 74 65 73 20 6f 66 20 61 20 77 6f   i bytes of a wo
38db2 72 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  rd..**.** Return
38db3 20 74 72 75 65 20 69 66 20 74 68 65 20 6d 2d 76   true if the m-v
38db4 61 6c 75 65 20 66 6f 72 20 7a 20 69 73 20 31 20  alue for z is 1 
38db5 6f 72 20 6d 6f 72 65 2e 20 20 49 6e 20 6f 74 68  or more.  In oth
38db6 65 72 20 77 6f 72 64 73 2c 0a 2a 2a 20 72 65 74  er words,.** ret
38db7 75 72 6e 20 74 72 75 65 20 69 66 20 7a 20 63 6f  urn true if z co
38db8 6e 74 61 69 6e 73 20 61 74 20 6c 65 61 73 74 20  ntains at least 
38db9 6f 6e 65 20 76 6f 77 65 6c 20 74 68 61 74 20 69  one vowel that i
38dba 73 20 66 6f 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79  s followed.** by
38dbb 20 61 20 63 6f 6e 73 6f 6e 61 6e 74 2e 0a 2a 2a   a consonant..**
38dbc 0a 2a 2a 20 49 6e 20 74 68 69 73 20 72 6f 75 74  .** In this rout
38dbd 69 6e 65 20 7a 5b 5d 20 69 73 20 69 6e 20 72 65  ine z[] is in re
38dbe 76 65 72 73 65 20 6f 72 64 65 72 2e 20 20 53 6f  verse order.  So
38dbf 20 77 65 20 61 72 65 20 72 65 61 6c 6c 79 20 6c   we are really l
38dc0 6f 6f 6b 69 6e 67 0a 2a 2a 20 66 6f 72 20 61 6e  ooking.** for an
38dc1 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 6f 66 20   instance of of 
38dc2 61 20 63 6f 6e 73 6f 6e 61 6e 74 20 66 6f 6c 6c  a consonant foll
38dc3 6f 77 65 64 20 62 79 20 61 20 76 6f 77 65 6c 2e  owed by a vowel.
38dc4 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
38dc5 5f 67 74 5f 30 28 63 6f 6e 73 74 20 63 68 61 72  _gt_0(const char
38dc6 20 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28 20 69   *z){.  while( i
38dc7 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b  sVowel(z) ){ z++
38dc8 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20  ; }.  if( *z==0 
38dc9 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68  ) return 0;.  wh
38dca 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74  ile( isConsonant
38dcb 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20  (z) ){ z++; }.  
38dcc 72 65 74 75 72 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a  return *z!=0;.}.
38dcd 0a 2f 2a 20 4c 69 6b 65 20 6d 67 74 30 20 61 62  ./* Like mgt0 ab
38dce 6f 76 65 20 65 78 63 65 70 74 20 77 65 20 61 72  ove except we ar
38dcf 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20  e looking for a 
38dd0 76 61 6c 75 65 20 6f 66 20 6d 20 77 68 69 63 68  value of m which
38dd1 20 69 73 0a 2a 2a 20 65 78 61 63 74 6c 79 20 31   is.** exactly 1
38dd2 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
38dd3 5f 65 71 5f 31 28 63 6f 6e 73 74 20 63 68 61 72  _eq_1(const char
38dd4 20 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28 20 69   *z){.  while( i
38dd5 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b  sVowel(z) ){ z++
38dd6 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20  ; }.  if( *z==0 
38dd7 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68  ) return 0;.  wh
38dd8 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74  ile( isConsonant
38dd9 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20  (z) ){ z++; }.  
38dda 69 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75  if( *z==0 ) retu
38ddb 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69  rn 0;.  while( i
38ddc 73 56 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b  sVowel(z) ){ z++
38ddd 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20  ; }.  if( *z==0 
38dde 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 77 68  ) return 1;.  wh
38ddf 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74  ile( isConsonant
38de0 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20  (z) ){ z++; }.  
38de1 72 65 74 75 72 6e 20 2a 7a 3d 3d 30 3b 0a 7d 0a  return *z==0;.}.
38de2 0a 2f 2a 20 4c 69 6b 65 20 6d 67 74 30 20 61 62  ./* Like mgt0 ab
38de3 6f 76 65 20 65 78 63 65 70 74 20 77 65 20 61 72  ove except we ar
38de4 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20  e looking for a 
38de5 76 61 6c 75 65 20 6f 66 20 6d 3e 31 20 69 6e 73  value of m>1 ins
38de6 74 65 61 64 0a 2a 2a 20 6f 72 20 6d 3e 30 0a 2a  tead.** or m>0.*
38de7 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 5f 67  /.static int m_g
38de8 74 5f 31 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  t_1(const char *
38de9 7a 29 7b 0a 20 20 77 68 69 6c 65 28 20 69 73 56  z){.  while( isV
38dea 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20  owel(z) ){ z++; 
38deb 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20  }.  if( *z==0 ) 
38dec 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c  return 0;.  whil
38ded 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a  e( isConsonant(z
38dee 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66  ) ){ z++; }.  if
38def 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e  ( *z==0 ) return
38df0 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 56   0;.  while( isV
38df1 6f 77 65 6c 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20  owel(z) ){ z++; 
38df2 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30 20 29 20  }.  if( *z==0 ) 
38df3 72 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c  return 0;.  whil
38df4 65 28 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a  e( isConsonant(z
38df5 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 72 65  ) ){ z++; }.  re
38df6 74 75 72 6e 20 2a 7a 21 3d 30 3b 0a 7d 0a 0a 2f  turn *z!=0;.}../
38df7 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45  *.** Return TRUE
38df8 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 76   if there is a v
38df9 6f 77 65 6c 20 61 6e 79 77 68 65 72 65 20 77 69  owel anywhere wi
38dfa 74 68 69 6e 20 7a 5b 30 2e 2e 6e 2d 31 5d 0a 2a  thin z[0..n-1].*
38dfb 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68 61 73  /.static int has
38dfc 56 6f 77 65 6c 28 63 6f 6e 73 74 20 63 68 61 72  Vowel(const char
38dfd 20 2a 7a 29 7b 0a 20 20 77 68 69 6c 65 28 20 69   *z){.  while( i
38dfe 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b  sConsonant(z) ){
38dff 20 7a 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e   z++; }.  return
38e00 20 2a 7a 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   *z!=0;.}../*.**
38e01 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
38e02 74 68 65 20 77 6f 72 64 20 65 6e 64 73 20 69 6e  the word ends in
38e03 20 61 20 64 6f 75 62 6c 65 20 63 6f 6e 73 6f 6e   a double conson
38e04 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74  ant..**.** The t
38e05 65 78 74 20 69 73 20 72 65 76 65 72 73 65 64 20  ext is reversed 
38e06 68 65 72 65 2e 20 53 6f 20 77 65 20 61 72 65 20  here. So we are 
38e07 72 65 61 6c 6c 79 20 6c 6f 6f 6b 69 6e 67 20 61  really looking a
38e08 74 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 74  t.** the first t
38e09 77 6f 20 63 68 61 72 61 63 74 65 72 73 20 6f 66  wo characters of
38e0a 20 7a 5b 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   z[]..*/.static 
38e0b 69 6e 74 20 64 6f 75 62 6c 65 43 6f 6e 73 6f 6e  int doubleConson
38e0c 61 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  ant(const char *
38e0d 7a 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 73 43  z){.  return isC
38e0e 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 26 26 20 7a  onsonant(z) && z
38e0f 5b 30 5d 3d 3d 7a 5b 31 5d 20 26 26 20 69 73 43  [0]==z[1] && isC
38e10 6f 6e 73 6f 6e 61 6e 74 28 7a 2b 31 29 3b 0a 7d  onsonant(z+1);.}
38e11 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
38e12 52 55 45 20 69 66 20 74 68 65 20 77 6f 72 64 20  RUE if the word 
38e13 65 6e 64 73 20 77 69 74 68 20 74 68 72 65 65 20  ends with three 
38e14 6c 65 74 74 65 72 73 20 77 68 69 63 68 0a 2a 2a  letters which.**
38e15 20 61 72 65 20 63 6f 6e 73 6f 6e 61 6e 74 2d 76   are consonant-v
38e16 6f 77 65 6c 2d 63 6f 6e 73 6f 6e 65 6e 74 20 61  owel-consonent a
38e17 6e 64 20 77 68 65 72 65 20 74 68 65 20 66 69 6e  nd where the fin
38e18 61 6c 20 63 6f 6e 73 6f 6e 61 6e 74 0a 2a 2a 20  al consonant.** 
38e19 69 73 20 6e 6f 74 20 27 77 27 2c 20 27 78 27 2c  is not 'w', 'x',
38e1a 20 6f 72 20 27 79 27 2e 0a 2a 2a 0a 2a 2a 20 54   or 'y'..**.** T
38e1b 68 65 20 77 6f 72 64 20 69 73 20 72 65 76 65 72  he word is rever
38e1c 73 65 64 20 68 65 72 65 2e 20 20 53 6f 20 77 65  sed here.  So we
38e1d 20 61 72 65 20 72 65 61 6c 6c 79 20 63 68 65 63   are really chec
38e1e 6b 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 72 73  king the.** firs
38e1f 74 20 74 68 72 65 65 20 6c 65 74 74 65 72 73 20  t three letters 
38e20 61 6e 64 20 74 68 65 20 66 69 72 73 74 20 6f 6e  and the first on
38e21 65 20 63 61 6e 6e 6f 74 20 62 65 20 69 6e 20 5b  e cannot be in [
38e22 77 78 79 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  wxy]..*/.static 
38e23 69 6e 74 20 73 74 61 72 5f 6f 68 28 63 6f 6e 73  int star_oh(cons
38e24 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 72 65  t char *z){.  re
38e25 74 75 72 6e 0a 20 20 20 20 7a 5b 30 5d 21 3d 30  turn.    z[0]!=0
38e26 20 26 26 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28   && isConsonant(
38e27 7a 29 20 26 26 0a 20 20 20 20 7a 5b 30 5d 21 3d  z) &&.    z[0]!=
38e28 27 77 27 20 26 26 20 7a 5b 30 5d 21 3d 27 78 27  'w' && z[0]!='x'
38e29 20 26 26 20 7a 5b 30 5d 21 3d 27 79 27 20 26 26   && z[0]!='y' &&
38e2a 0a 20 20 20 20 7a 5b 31 5d 21 3d 30 20 26 26 20  .    z[1]!=0 && 
38e2b 69 73 56 6f 77 65 6c 28 7a 2b 31 29 20 26 26 0a  isVowel(z+1) &&.
38e2c 20 20 20 20 7a 5b 32 5d 21 3d 30 20 26 26 20 69      z[2]!=0 && i
38e2d 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 2b 32 29 3b  sConsonant(z+2);
38e2e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  .}../*.** If the
38e2f 20 77 6f 72 64 20 65 6e 64 73 20 77 69 74 68 20   word ends with 
38e30 7a 46 72 6f 6d 20 61 6e 64 20 78 43 6f 6e 64 28  zFrom and xCond(
38e31 29 20 69 73 20 74 72 75 65 20 66 6f 72 20 74 68  ) is true for th
38e32 65 20 73 74 65 6d 0a 2a 2a 20 6f 66 20 74 68 65  e stem.** of the
38e33 20 77 6f 72 64 20 74 68 61 74 20 70 72 65 63 65   word that prece
38e34 65 64 73 20 74 68 65 20 7a 46 72 6f 6d 20 65 6e  eds the zFrom en
38e35 64 69 6e 67 2c 20 74 68 65 6e 20 63 68 61 6e 67  ding, then chang
38e36 65 20 74 68 65 20 0a 2a 2a 20 65 6e 64 69 6e 67  e the .** ending
38e37 20 74 6f 20 7a 54 6f 2e 0a 2a 2a 0a 2a 2a 20 54   to zTo..**.** T
38e38 68 65 20 69 6e 70 75 74 20 77 6f 72 64 20 2a 70  he input word *p
38e39 7a 20 61 6e 64 20 7a 46 72 6f 6d 20 61 72 65 20  z and zFrom are 
38e3a 62 6f 74 68 20 69 6e 20 72 65 76 65 72 73 65 20  both in reverse 
38e3b 6f 72 64 65 72 2e 20 20 7a 54 6f 0a 2a 2a 20 69  order.  zTo.** i
38e3c 73 20 69 6e 20 6e 6f 72 6d 61 6c 20 6f 72 64 65  s in normal orde
38e3d 72 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  r. .**.** Return
38e3e 20 54 52 55 45 20 69 66 20 7a 46 72 6f 6d 20 6d   TRUE if zFrom m
38e3f 61 74 63 68 65 73 2e 20 20 52 65 74 75 72 6e 20  atches.  Return 
38e40 46 41 4c 53 45 20 69 66 20 7a 46 72 6f 6d 20 64  FALSE if zFrom d
38e41 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 61 74 63 68  oes not.** match
38e42 2e 20 20 4e 6f 74 20 74 68 61 74 20 54 52 55 45  .  Not that TRUE
38e43 20 69 73 20 72 65 74 75 72 6e 65 64 20 65 76 65   is returned eve
38e44 6e 20 69 66 20 78 43 6f 6e 64 28 29 20 66 61 69  n if xCond() fai
38e45 6c 73 20 61 6e 64 0a 2a 2a 20 6e 6f 20 73 75 62  ls and.** no sub
38e46 73 74 69 74 75 74 69 6f 6e 20 6f 63 63 75 72 73  stitution occurs
38e47 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
38e48 73 74 65 6d 28 0a 20 20 63 68 61 72 20 2a 2a 70  stem(.  char **p
38e49 7a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  z,             /
38e4a 2a 20 54 68 65 20 77 6f 72 64 20 62 65 69 6e 67  * The word being
38e4b 20 73 74 65 6d 6d 65 64 20 28 52 65 76 65 72 73   stemmed (Revers
38e4c 65 64 29 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ed) */.  const c
38e4d 68 61 72 20 2a 7a 46 72 6f 6d 2c 20 20 20 20 20  har *zFrom,     
38e4e 2f 2a 20 49 66 20 74 68 65 20 65 6e 64 69 6e 67  /* If the ending
38e4f 20 6d 61 74 63 68 65 73 20 74 68 69 73 2e 2e 2e   matches this...
38e50 20 28 52 65 76 65 72 73 65 64 29 20 2a 2f 0a 20   (Reversed) */. 
38e51 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f   const char *zTo
38e52 2c 20 20 20 20 20 20 20 2f 2a 20 2e 2e 2e 20 63  ,       /* ... c
38e53 68 61 6e 67 65 20 74 68 65 20 65 6e 64 69 6e 67  hange the ending
38e54 20 74 6f 20 74 68 69 73 20 28 6e 6f 74 20 72 65   to this (not re
38e55 76 65 72 73 65 64 29 20 2a 2f 0a 20 20 69 6e 74  versed) */.  int
38e56 20 28 2a 78 43 6f 6e 64 29 28 63 6f 6e 73 74 20   (*xCond)(const 
38e57 63 68 61 72 2a 29 20 20 20 2f 2a 20 43 6f 6e 64  char*)   /* Cond
38e58 69 74 69 6f 6e 20 74 68 61 74 20 6d 75 73 74 20  ition that must 
38e59 62 65 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20 20  be true */.){.  
38e5a 63 68 61 72 20 2a 7a 20 3d 20 2a 70 7a 3b 0a 20  char *z = *pz;. 
38e5b 20 77 68 69 6c 65 28 20 2a 7a 46 72 6f 6d 20 26   while( *zFrom &
38e5c 26 20 2a 7a 46 72 6f 6d 3d 3d 2a 7a 20 29 7b 20  & *zFrom==*z ){ 
38e5d 7a 2b 2b 3b 20 7a 46 72 6f 6d 2b 2b 3b 20 7d 0a  z++; zFrom++; }.
38e5e 20 20 69 66 28 20 2a 7a 46 72 6f 6d 21 3d 30 20    if( *zFrom!=0 
38e5f 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66  ) return 0;.  if
38e60 28 20 78 43 6f 6e 64 20 26 26 20 21 78 43 6f 6e  ( xCond && !xCon
38e61 64 28 7a 29 20 29 20 72 65 74 75 72 6e 20 31 3b  d(z) ) return 1;
38e62 0a 20 20 77 68 69 6c 65 28 20 2a 7a 54 6f 20 29  .  while( *zTo )
38e63 7b 0a 20 20 20 20 2a 28 2d 2d 7a 29 20 3d 20 2a  {.    *(--z) = *
38e64 28 7a 54 6f 2b 2b 29 3b 0a 20 20 7d 0a 20 20 2a  (zTo++);.  }.  *
38e65 70 7a 20 3d 20 7a 3b 0a 20 20 72 65 74 75 72 6e  pz = z;.  return
38e66 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69   1;.}../*.** Thi
38e67 73 20 69 73 20 74 68 65 20 66 61 6c 6c 62 61 63  s is the fallbac
38e68 6b 20 73 74 65 6d 6d 65 72 20 75 73 65 64 20 77  k stemmer used w
38e69 68 65 6e 20 74 68 65 20 70 6f 72 74 65 72 20 73  hen the porter s
38e6a 74 65 6d 6d 65 72 20 69 73 0a 2a 2a 20 69 6e 61  temmer is.** ina
38e6b 70 70 72 6f 70 72 69 61 74 65 2e 20 20 54 68 65  ppropriate.  The
38e6c 20 69 6e 70 75 74 20 77 6f 72 64 20 69 73 20 63   input word is c
38e6d 6f 70 69 65 64 20 69 6e 74 6f 20 74 68 65 20 6f  opied into the o
38e6e 75 74 70 75 74 20 77 69 74 68 0a 2a 2a 20 55 53  utput with.** US
38e6f 2d 41 53 43 49 49 20 63 61 73 65 20 66 6f 6c 64  -ASCII case fold
38e70 69 6e 67 2e 20 20 49 66 20 74 68 65 20 69 6e 70  ing.  If the inp
38e71 75 74 20 77 6f 72 64 20 69 73 20 74 6f 6f 20 6c  ut word is too l
38e72 6f 6e 67 20 28 6d 6f 72 65 0a 2a 2a 20 74 68 61  ong (more.** tha
38e73 6e 20 32 30 20 62 79 74 65 73 20 69 66 20 69 74  n 20 bytes if it
38e74 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20 64 69 67   contains no dig
38e75 69 74 73 20 6f 72 20 6d 6f 72 65 20 74 68 61 6e  its or more than
38e76 20 36 20 62 79 74 65 73 20 69 66 0a 2a 2a 20 69   6 bytes if.** i
38e77 74 20 63 6f 6e 74 61 69 6e 73 20 64 69 67 69 74  t contains digit
38e78 73 29 20 74 68 65 6e 20 77 6f 72 64 20 69 73 20  s) then word is 
38e79 74 72 75 6e 63 61 74 65 64 20 74 6f 20 32 30 20  truncated to 20 
38e7a 6f 72 20 36 20 62 79 74 65 73 0a 2a 2a 20 62 79  or 6 bytes.** by
38e7b 20 74 61 6b 69 6e 67 20 31 30 20 6f 72 20 33 20   taking 10 or 3 
38e7c 62 79 74 65 73 20 66 72 6f 6d 20 74 68 65 20 62  bytes from the b
38e7d 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64  eginning and end
38e7e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
38e7f 20 63 6f 70 79 5f 73 74 65 6d 6d 65 72 28 63 6f   copy_stemmer(co
38e80 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c 20 69  nst char *zIn, i
38e81 6e 74 20 6e 49 6e 2c 20 63 68 61 72 20 2a 7a 4f  nt nIn, char *zO
38e82 75 74 2c 20 69 6e 74 20 2a 70 6e 4f 75 74 29 7b  ut, int *pnOut){
38e83 0a 20 20 69 6e 74 20 69 2c 20 6d 78 2c 20 6a 3b  .  int i, mx, j;
38e84 0a 20 20 69 6e 74 20 68 61 73 44 69 67 69 74 20  .  int hasDigit 
38e85 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  = 0;.  for(i=0; 
38e86 69 3c 6e 49 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  i<nIn; i++){.   
38e87 20 69 6e 74 20 63 20 3d 20 7a 49 6e 5b 69 5d 3b   int c = zIn[i];
38e88 0a 20 20 20 20 69 66 28 20 63 3e 3d 27 41 27 20  .    if( c>='A' 
38e89 26 26 20 63 3c 3d 27 5a 27 20 29 7b 0a 20 20 20  && c<='Z' ){.   
38e8a 20 20 20 7a 4f 75 74 5b 69 5d 20 3d 20 63 20 2d     zOut[i] = c -
38e8b 20 27 41 27 20 2b 20 27 61 27 3b 0a 20 20 20 20   'A' + 'a';.    
38e8c 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28  }else{.      if(
38e8d 20 63 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39   c>='0' && c<='9
38e8e 27 20 29 20 68 61 73 44 69 67 69 74 20 3d 20 31  ' ) hasDigit = 1
38e8f 3b 0a 20 20 20 20 20 20 7a 4f 75 74 5b 69 5d 20  ;.      zOut[i] 
38e90 3d 20 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = c;.    }.  }. 
38e91 20 6d 78 20 3d 20 68 61 73 44 69 67 69 74 20 3f   mx = hasDigit ?
38e92 20 33 20 3a 20 31 30 3b 0a 20 20 69 66 28 20 6e   3 : 10;.  if( n
38e93 49 6e 3e 6d 78 2a 32 20 29 7b 0a 20 20 20 20 66  In>mx*2 ){.    f
38e94 6f 72 28 6a 3d 6d 78 2c 20 69 3d 6e 49 6e 2d 6d  or(j=mx, i=nIn-m
38e95 78 3b 20 69 3c 6e 49 6e 3b 20 69 2b 2b 2c 20 6a  x; i<nIn; i++, j
38e96 2b 2b 29 7b 0a 20 20 20 20 20 20 7a 4f 75 74 5b  ++){.      zOut[
38e97 6a 5d 20 3d 20 7a 4f 75 74 5b 69 5d 3b 0a 20 20  j] = zOut[i];.  
38e98 20 20 7d 0a 20 20 20 20 69 20 3d 20 6a 3b 0a 20    }.    i = j;. 
38e99 20 7d 0a 20 20 7a 4f 75 74 5b 69 5d 20 3d 20 30   }.  zOut[i] = 0
38e9a 3b 0a 20 20 2a 70 6e 4f 75 74 20 3d 20 69 3b 0a  ;.  *pnOut = i;.
38e9b 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74 65 6d 20 74  }.../*.** Stem t
38e9c 68 65 20 69 6e 70 75 74 20 77 6f 72 64 20 7a 49  he input word zI
38e9d 6e 5b 30 2e 2e 6e 49 6e 2d 31 5d 2e 20 20 53 74  n[0..nIn-1].  St
38e9e 6f 72 65 20 74 68 65 20 6f 75 74 70 75 74 20 69  ore the output i
38e9f 6e 20 7a 4f 75 74 2e 0a 2a 2a 20 7a 4f 75 74 20  n zOut..** zOut 
38ea0 69 73 20 61 74 20 6c 65 61 73 74 20 62 69 67 20  is at least big 
38ea1 65 6e 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 6e  enough to hold n
38ea2 49 6e 20 62 79 74 65 73 2e 20 20 57 72 69 74 65  In bytes.  Write
38ea3 20 74 68 65 20 61 63 74 75 61 6c 0a 2a 2a 20 73   the actual.** s
38ea4 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74 70 75  ize of the outpu
38ea5 74 20 77 6f 72 64 20 28 65 78 63 6c 75 73 69 76  t word (exclusiv
38ea6 65 20 6f 66 20 74 68 65 20 27 5c 30 27 20 74 65  e of the '\0' te
38ea7 72 6d 69 6e 61 74 6f 72 29 20 69 6e 74 6f 20 2a  rminator) into *
38ea8 70 6e 4f 75 74 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79  pnOut..**.** Any
38ea9 20 75 70 70 65 72 2d 63 61 73 65 20 63 68 61 72   upper-case char
38eaa 61 63 74 65 72 73 20 69 6e 20 74 68 65 20 55 53  acters in the US
38eab 2d 41 53 43 49 49 20 63 68 61 72 61 63 74 65 72  -ASCII character
38eac 20 73 65 74 20 28 5b 41 2d 5a 5d 29 0a 2a 2a 20   set ([A-Z]).** 
38ead 61 72 65 20 63 6f 6e 76 65 72 74 65 64 20 74 6f  are converted to
38eae 20 6c 6f 77 65 72 20 63 61 73 65 2e 20 20 55 70   lower case.  Up
38eaf 70 65 72 2d 63 61 73 65 20 55 54 46 20 63 68 61  per-case UTF cha
38eb0 72 61 63 74 65 72 73 20 61 72 65 0a 2a 2a 20 75  racters are.** u
38eb1 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20  nchanged..**.** 
38eb2 57 6f 72 64 73 20 74 68 61 74 20 61 72 65 20 6c  Words that are l
38eb3 6f 6e 67 65 72 20 74 68 61 6e 20 61 62 6f 75 74  onger than about
38eb4 20 32 30 20 62 79 74 65 73 20 61 72 65 20 73 74   20 bytes are st
38eb5 65 6d 6d 65 64 20 62 79 20 72 65 74 61 69 6e 69  emmed by retaini
38eb6 6e 67 0a 2a 2a 20 61 20 66 65 77 20 62 79 74 65  ng.** a few byte
38eb7 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e  s from the begin
38eb8 6e 69 6e 67 20 61 6e 64 20 74 68 65 20 65 6e 64  ning and the end
38eb9 20 6f 66 20 74 68 65 20 77 6f 72 64 2e 20 20 49   of the word.  I
38eba 66 20 74 68 65 0a 2a 2a 20 77 6f 72 64 20 63 6f  f the.** word co
38ebb 6e 74 61 69 6e 73 20 64 69 67 69 74 73 2c 20 33  ntains digits, 3
38ebc 20 62 79 74 65 73 20 61 72 65 20 74 61 6b 65 6e   bytes are taken
38ebd 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e   from the beginn
38ebe 69 6e 67 20 61 6e 64 0a 2a 2a 20 33 20 62 79 74  ing and.** 3 byt
38ebf 65 73 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 2e  es from the end.
38ec0 20 20 46 6f 72 20 6c 6f 6e 67 20 77 6f 72 64 73    For long words
38ec1 20 77 69 74 68 6f 75 74 20 64 69 67 69 74 73 2c   without digits,
38ec2 20 31 30 20 62 79 74 65 73 0a 2a 2a 20 61 72 65   10 bytes.** are
38ec3 20 74 61 6b 65 6e 20 66 72 6f 6d 20 65 61 63 68   taken from each
38ec4 20 65 6e 64 2e 20 20 55 53 2d 41 53 43 49 49 20   end.  US-ASCII 
38ec5 63 61 73 65 20 66 6f 6c 64 69 6e 67 20 73 74 69  case folding sti
38ec6 6c 6c 20 61 70 70 6c 69 65 73 2e 0a 2a 2a 20 0a  ll applies..** .
38ec7 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74 20  ** If the input 
38ec8 77 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20 6e 6f  word contains no
38ec9 74 20 64 69 67 69 74 73 20 62 75 74 20 64 6f 65  t digits but doe
38eca 73 20 63 68 61 72 61 63 74 65 72 73 20 6e 6f 74  s characters not
38ecb 20 0a 2a 2a 20 69 6e 20 5b 61 2d 7a 41 2d 5a 5d   .** in [a-zA-Z]
38ecc 20 74 68 65 6e 20 6e 6f 20 73 74 65 6d 6d 69 6e   then no stemmin
38ecd 67 20 69 73 20 61 74 74 65 6d 70 74 65 64 20 61  g is attempted a
38ece 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  nd this routine 
38ecf 6a 75 73 74 20 0a 2a 2a 20 63 6f 70 69 65 73 20  just .** copies 
38ed0 74 68 65 20 69 6e 70 75 74 20 69 6e 74 6f 20 74  the input into t
38ed1 68 65 20 69 6e 70 75 74 20 69 6e 74 6f 20 74 68  he input into th
38ed2 65 20 6f 75 74 70 75 74 20 77 69 74 68 20 55 53  e output with US
38ed3 2d 41 53 43 49 49 0a 2a 2a 20 63 61 73 65 20 66  -ASCII.** case f
38ed4 6f 6c 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 74  olding..**.** St
38ed5 65 6d 6d 69 6e 67 20 6e 65 76 65 72 20 69 6e 63  emming never inc
38ed6 72 65 61 73 65 73 20 74 68 65 20 6c 65 6e 67 74  reases the lengt
38ed7 68 20 6f 66 20 74 68 65 20 77 6f 72 64 2e 20 20  h of the word.  
38ed8 53 6f 20 74 68 65 72 65 20 69 73 0a 2a 2a 20 6e  So there is.** n
38ed9 6f 20 63 68 61 6e 63 65 20 6f 66 20 6f 76 65 72  o chance of over
38eda 66 6c 6f 77 69 6e 67 20 74 68 65 20 7a 4f 75 74  flowing the zOut
38edb 20 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74   buffer..*/.stat
38edc 69 63 20 76 6f 69 64 20 70 6f 72 74 65 72 5f 73  ic void porter_s
38edd 74 65 6d 6d 65 72 28 63 6f 6e 73 74 20 63 68 61  temmer(const cha
38ede 72 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 49 6e 2c  r *zIn, int nIn,
38edf 20 63 68 61 72 20 2a 7a 4f 75 74 2c 20 69 6e 74   char *zOut, int
38ee0 20 2a 70 6e 4f 75 74 29 7b 0a 20 20 69 6e 74 20   *pnOut){.  int 
38ee1 69 2c 20 6a 2c 20 63 3b 0a 20 20 63 68 61 72 20  i, j, c;.  char 
38ee2 7a 52 65 76 65 72 73 65 5b 32 38 5d 3b 0a 20 20  zReverse[28];.  
38ee3 63 68 61 72 20 2a 7a 2c 20 2a 7a 32 3b 0a 20 20  char *z, *z2;.  
38ee4 69 66 28 20 6e 49 6e 3c 33 20 7c 7c 20 6e 49 6e  if( nIn<3 || nIn
38ee5 3e 3d 73 69 7a 65 6f 66 28 7a 52 65 76 65 72 73  >=sizeof(zRevers
38ee6 65 29 2d 37 20 29 7b 0a 20 20 20 20 2f 2a 20 54  e)-7 ){.    /* T
38ee7 68 65 20 77 6f 72 64 20 69 73 20 74 6f 6f 20 62  he word is too b
38ee8 69 67 20 6f 72 20 74 6f 6f 20 73 6d 61 6c 6c 20  ig or too small 
38ee9 66 6f 72 20 74 68 65 20 70 6f 72 74 65 72 20 73  for the porter s
38eea 74 65 6d 6d 65 72 2e 0a 20 20 20 20 2a 2a 20 46  temmer..    ** F
38eeb 61 6c 6c 62 61 63 6b 20 74 6f 20 74 68 65 20 63  allback to the c
38eec 6f 70 79 20 73 74 65 6d 6d 65 72 20 2a 2f 0a 20  opy stemmer */. 
38eed 20 20 20 63 6f 70 79 5f 73 74 65 6d 6d 65 72 28     copy_stemmer(
38eee 7a 49 6e 2c 20 6e 49 6e 2c 20 7a 4f 75 74 2c 20  zIn, nIn, zOut, 
38eef 70 6e 4f 75 74 29 3b 0a 20 20 20 20 72 65 74 75  pnOut);.    retu
38ef0 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  rn;.  }.  for(i=
38ef1 30 2c 20 6a 3d 73 69 7a 65 6f 66 28 7a 52 65 76  0, j=sizeof(zRev
38ef2 65 72 73 65 29 2d 36 3b 20 69 3c 6e 49 6e 3b 20  erse)-6; i<nIn; 
38ef3 69 2b 2b 2c 20 6a 2d 2d 29 7b 0a 20 20 20 20 63  i++, j--){.    c
38ef4 20 3d 20 7a 49 6e 5b 69 5d 3b 0a 20 20 20 20 69   = zIn[i];.    i
38ef5 66 28 20 63 3e 3d 27 41 27 20 26 26 20 63 3c 3d  f( c>='A' && c<=
38ef6 27 5a 27 20 29 7b 0a 20 20 20 20 20 20 7a 52 65  'Z' ){.      zRe
38ef7 76 65 72 73 65 5b 6a 5d 20 3d 20 63 20 2b 20 27  verse[j] = c + '
38ef8 61 27 20 2d 20 27 41 27 3b 0a 20 20 20 20 7d 65  a' - 'A';.    }e
38ef9 6c 73 65 20 69 66 28 20 63 3e 3d 27 61 27 20 26  lse if( c>='a' &
38efa 26 20 63 3c 3d 27 7a 27 20 29 7b 0a 20 20 20 20  & c<='z' ){.    
38efb 20 20 7a 52 65 76 65 72 73 65 5b 6a 5d 20 3d 20    zReverse[j] = 
38efc 63 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  c;.    }else{.  
38efd 20 20 20 20 2f 2a 20 54 68 65 20 75 73 65 20 6f      /* The use o
38efe 66 20 61 20 63 68 61 72 61 63 74 65 72 20 6e 6f  f a character no
38eff 74 20 69 6e 20 5b 61 2d 7a 41 2d 5a 5d 20 6d 65  t in [a-zA-Z] me
38f00 61 6e 73 20 74 68 61 74 20 77 65 20 66 61 6c 6c  ans that we fall
38f01 62 61 63 6b 0a 20 20 20 20 20 20 2a 2a 20 74 6f  back.      ** to
38f02 20 74 68 65 20 63 6f 70 79 20 73 74 65 6d 6d 65   the copy stemme
38f03 72 20 2a 2f 0a 20 20 20 20 20 20 63 6f 70 79 5f  r */.      copy_
38f04 73 74 65 6d 6d 65 72 28 7a 49 6e 2c 20 6e 49 6e  stemmer(zIn, nIn
38f05 2c 20 7a 4f 75 74 2c 20 70 6e 4f 75 74 29 3b 0a  , zOut, pnOut);.
38f06 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
38f07 20 20 7d 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74    }.  }.  memset
38f08 28 26 7a 52 65 76 65 72 73 65 5b 73 69 7a 65 6f  (&zReverse[sizeo
38f09 66 28 7a 52 65 76 65 72 73 65 29 2d 35 5d 2c 20  f(zReverse)-5], 
38f0a 30 2c 20 35 29 3b 0a 20 20 7a 20 3d 20 26 7a 52  0, 5);.  z = &zR
38f0b 65 76 65 72 73 65 5b 6a 2b 31 5d 3b 0a 0a 0a 20  everse[j+1];... 
38f0c 20 2f 2a 20 53 74 65 70 20 31 61 20 2a 2f 0a 20   /* Step 1a */. 
38f0d 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 73 27 20 29   if( z[0]=='s' )
38f0e 7b 0a 20 20 20 20 69 66 28 0a 20 20 20 20 20 21  {.    if(.     !
38f0f 73 74 65 6d 28 26 7a 2c 20 22 73 65 73 73 22 2c  stem(&z, "sess",
38f10 20 22 73 73 22 2c 20 30 29 20 26 26 0a 20 20 20   "ss", 0) &&.   
38f11 20 20 21 73 74 65 6d 28 26 7a 2c 20 22 73 65 69    !stem(&z, "sei
38f12 22 2c 20 22 69 22 2c 20 30 29 20 20 26 26 0a 20  ", "i", 0)  &&. 
38f13 20 20 20 20 21 73 74 65 6d 28 26 7a 2c 20 22 73      !stem(&z, "s
38f14 73 22 2c 20 22 73 73 22 2c 20 30 29 0a 20 20 20  s", "ss", 0).   
38f15 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20   ){.      z++;. 
38f16 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53     }.  }..  /* S
38f17 74 65 70 20 31 62 20 2a 2f 20 20 0a 20 20 7a 32  tep 1b */  .  z2
38f18 20 3d 20 7a 3b 0a 20 20 69 66 28 20 73 74 65 6d   = z;.  if( stem
38f19 28 26 7a 2c 20 22 64 65 65 22 2c 20 22 65 65 22  (&z, "dee", "ee"
38f1a 2c 20 6d 5f 67 74 5f 30 29 20 29 7b 0a 20 20 20  , m_gt_0) ){.   
38f1b 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20   /* Do nothing. 
38f1c 20 54 68 65 20 77 6f 72 6b 20 77 61 73 20 61 6c   The work was al
38f1d 6c 20 69 6e 20 74 68 65 20 74 65 73 74 20 2a 2f  l in the test */
38f1e 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 0a 20 20  .  }else if( .  
38f1f 20 20 20 28 73 74 65 6d 28 26 7a 2c 20 22 67 6e     (stem(&z, "gn
38f20 69 22 2c 20 22 22 2c 20 68 61 73 56 6f 77 65 6c  i", "", hasVowel
38f21 29 20 7c 7c 20 73 74 65 6d 28 26 7a 2c 20 22 64  ) || stem(&z, "d
38f22 65 22 2c 20 22 22 2c 20 68 61 73 56 6f 77 65 6c  e", "", hasVowel
38f23 29 29 0a 20 20 20 20 20 20 26 26 20 7a 21 3d 7a  )).      && z!=z
38f24 32 0a 20 20 29 7b 0a 20 20 20 20 20 69 66 28 20  2.  ){.     if( 
38f25 73 74 65 6d 28 26 7a 2c 20 22 74 61 22 2c 20 22  stem(&z, "ta", "
38f26 61 74 65 22 2c 20 30 29 20 7c 7c 0a 20 20 20 20  ate", 0) ||.    
38f27 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c       stem(&z, "l
38f28 62 22 2c 20 22 62 6c 65 22 2c 20 30 29 20 7c 7c  b", "ble", 0) ||
38f29 0a 20 20 20 20 20 20 20 20 20 73 74 65 6d 28 26  .         stem(&
38f2a 7a 2c 20 22 7a 69 22 2c 20 22 69 7a 65 22 2c 20  z, "zi", "ize", 
38f2b 30 29 20 29 7b 0a 20 20 20 20 20 20 20 2f 2a 20  0) ){.       /* 
38f2c 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20 54 68 65  Do nothing.  The
38f2d 20 77 6f 72 6b 20 77 61 73 20 61 6c 6c 20 69 6e   work was all in
38f2e 20 74 68 65 20 74 65 73 74 20 2a 2f 0a 20 20 20   the test */.   
38f2f 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6f 75 62    }else if( doub
38f30 6c 65 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 26  leConsonant(z) &
38f31 26 20 28 2a 7a 21 3d 27 6c 27 20 26 26 20 2a 7a  & (*z!='l' && *z
38f32 21 3d 27 73 27 20 26 26 20 2a 7a 21 3d 27 7a 27  !='s' && *z!='z'
38f33 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 2b 2b 3b  ) ){.       z++;
38f34 0a 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  .     }else if( 
38f35 6d 5f 65 71 5f 31 28 7a 29 20 26 26 20 73 74 61  m_eq_1(z) && sta
38f36 72 5f 6f 68 28 7a 29 20 29 7b 0a 20 20 20 20 20  r_oh(z) ){.     
38f37 20 20 2a 28 2d 2d 7a 29 20 3d 20 27 65 27 3b 0a    *(--z) = 'e';.
38f38 20 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a       }.  }..  /*
38f39 20 53 74 65 70 20 31 63 20 2a 2f 0a 20 20 69 66   Step 1c */.  if
38f3a 28 20 7a 5b 30 5d 3d 3d 27 79 27 20 26 26 20 68  ( z[0]=='y' && h
38f3b 61 73 56 6f 77 65 6c 28 7a 2b 31 29 20 29 7b 0a  asVowel(z+1) ){.
38f3c 20 20 20 20 7a 5b 30 5d 20 3d 20 27 69 27 3b 0a      z[0] = 'i';.
38f3d 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 32    }..  /* Step 2
38f3e 20 2a 2f 0a 20 20 73 77 69 74 63 68 28 20 7a 5b   */.  switch( z[
38f3f 31 5d 20 29 7b 0a 20 20 20 63 61 73 65 20 27 61  1] ){.   case 'a
38f40 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ':.     stem(&z,
38f41 20 22 6c 61 6e 6f 69 74 61 22 2c 20 22 61 74 65   "lanoita", "ate
38f42 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20  ", m_gt_0) ||.  
38f43 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6c 61 6e     stem(&z, "lan
38f44 6f 69 74 22 2c 20 22 74 69 6f 6e 22 2c 20 6d 5f  oit", "tion", m_
38f45 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61  gt_0);.     brea
38f46 6b 3b 0a 20 20 20 63 61 73 65 20 27 63 27 3a 0a  k;.   case 'c':.
38f47 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
38f48 63 6e 65 22 2c 20 22 65 6e 63 65 22 2c 20 6d 5f  cne", "ence", m_
38f49 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_0) ||.     st
38f4a 65 6d 28 26 7a 2c 20 22 69 63 6e 61 22 2c 20 22  em(&z, "icna", "
38f4b 61 6e 63 65 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a  ance", m_gt_0);.
38f4c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
38f4d 61 73 65 20 27 65 27 3a 0a 20 20 20 20 20 73 74  ase 'e':.     st
38f4e 65 6d 28 26 7a 2c 20 22 72 65 7a 69 22 2c 20 22  em(&z, "rezi", "
38f4f 69 7a 65 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20  ize", m_gt_0);. 
38f50 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61      break;.   ca
38f51 73 65 20 27 67 27 3a 0a 20 20 20 20 20 73 74 65  se 'g':.     ste
38f52 6d 28 26 7a 2c 20 22 69 67 6f 6c 22 2c 20 22 6c  m(&z, "igol", "l
38f53 6f 67 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20  og", m_gt_0);.  
38f54 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73     break;.   cas
38f55 65 20 27 6c 27 3a 0a 20 20 20 20 20 73 74 65 6d  e 'l':.     stem
38f56 28 26 7a 2c 20 22 69 6c 62 22 2c 20 22 62 6c 65  (&z, "ilb", "ble
38f57 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20  ", m_gt_0) ||.  
38f58 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 6c 6c     stem(&z, "ill
38f59 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74 5f 30  a", "al", m_gt_0
38f5a 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
38f5b 7a 2c 20 22 69 6c 74 6e 65 22 2c 20 22 65 6e 74  z, "iltne", "ent
38f5c 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20  ", m_gt_0) ||.  
38f5d 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 6c 65     stem(&z, "ile
38f5e 22 2c 20 22 65 22 2c 20 6d 5f 67 74 5f 30 29 20  ", "e", m_gt_0) 
38f5f 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ||.     stem(&z,
38f60 20 22 69 6c 73 75 6f 22 2c 20 22 6f 75 73 22 2c   "ilsuo", "ous",
38f61 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62   m_gt_0);.     b
38f62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 6f  reak;.   case 'o
38f63 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ':.     stem(&z,
38f64 20 22 6e 6f 69 74 61 7a 69 22 2c 20 22 69 7a 65   "noitazi", "ize
38f65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20  ", m_gt_0) ||.  
38f66 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6e 6f 69     stem(&z, "noi
38f67 74 61 22 2c 20 22 61 74 65 22 2c 20 6d 5f 67 74  ta", "ate", m_gt
38f68 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
38f69 28 26 7a 2c 20 22 72 6f 74 61 22 2c 20 22 61 74  (&z, "rota", "at
38f6a 65 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20  e", m_gt_0);.   
38f6b 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
38f6c 20 27 73 27 3a 0a 20 20 20 20 20 73 74 65 6d 28   's':.     stem(
38f6d 26 7a 2c 20 22 6d 73 69 6c 61 22 2c 20 22 61 6c  &z, "msila", "al
38f6e 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20  ", m_gt_0) ||.  
38f6f 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 73 73 65     stem(&z, "sse
38f70 6e 65 76 69 22 2c 20 22 69 76 65 22 2c 20 6d 5f  nevi", "ive", m_
38f71 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74  gt_0) ||.     st
38f72 65 6d 28 26 7a 2c 20 22 73 73 65 6e 6c 75 66 22  em(&z, "ssenluf"
38f73 2c 20 22 66 75 6c 22 2c 20 6d 5f 67 74 5f 30 29  , "ful", m_gt_0)
38f74 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a   ||.     stem(&z
38f75 2c 20 22 73 73 65 6e 73 75 6f 22 2c 20 22 6f 75  , "ssensuo", "ou
38f76 73 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20  s", m_gt_0);.   
38f77 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
38f78 20 27 74 27 3a 0a 20 20 20 20 20 73 74 65 6d 28   't':.     stem(
38f79 26 7a 2c 20 22 69 74 69 6c 61 22 2c 20 22 61 6c  &z, "itila", "al
38f7a 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20  ", m_gt_0) ||.  
38f7b 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 74 69     stem(&z, "iti
38f7c 76 69 22 2c 20 22 69 76 65 22 2c 20 6d 5f 67 74  vi", "ive", m_gt
38f7d 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
38f7e 28 26 7a 2c 20 22 69 74 69 6c 69 62 22 2c 20 22  (&z, "itilib", "
38f7f 62 6c 65 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20  ble", m_gt_0);. 
38f80 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a      break;.  }..
38f81 20 20 2f 2a 20 53 74 65 70 20 33 20 2a 2f 0a 20    /* Step 3 */. 
38f82 20 73 77 69 74 63 68 28 20 7a 5b 30 5d 20 29 7b   switch( z[0] ){
38f83 0a 20 20 20 63 61 73 65 20 27 65 27 3a 0a 20 20  .   case 'e':.  
38f84 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 65 74 61     stem(&z, "eta
38f85 63 69 22 2c 20 22 69 63 22 2c 20 6d 5f 67 74 5f  ci", "ic", m_gt_
38f86 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28  0) ||.     stem(
38f87 26 7a 2c 20 22 65 76 69 74 61 22 2c 20 22 22 2c  &z, "evita", "",
38f88 20 6d 5f 67 74 5f 30 29 20 20 20 7c 7c 0a 20 20   m_gt_0)   ||.  
38f89 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 65 7a 69     stem(&z, "ezi
38f8a 6c 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74 5f  la", "al", m_gt_
38f8b 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a  0);.     break;.
38f8c 20 20 20 63 61 73 65 20 27 69 27 3a 0a 20 20 20     case 'i':.   
38f8d 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 74 69 63    stem(&z, "itic
38f8e 69 22 2c 20 22 69 63 22 2c 20 6d 5f 67 74 5f 30  i", "ic", m_gt_0
38f8f 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  );.     break;. 
38f90 20 20 63 61 73 65 20 27 6c 27 3a 0a 20 20 20 20    case 'l':.    
38f91 20 73 74 65 6d 28 26 7a 2c 20 22 6c 61 63 69 22   stem(&z, "laci"
38f92 2c 20 22 69 63 22 2c 20 6d 5f 67 74 5f 30 29 20  , "ic", m_gt_0) 
38f93 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c  ||.     stem(&z,
38f94 20 22 6c 75 66 22 2c 20 22 22 2c 20 6d 5f 67 74   "luf", "", m_gt
38f95 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b  _0);.     break;
38f96 0a 20 20 20 63 61 73 65 20 27 73 27 3a 0a 20 20  .   case 's':.  
38f97 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 73 73 65     stem(&z, "sse
38f98 6e 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 30 29 3b  n", "", m_gt_0);
38f99 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d  .     break;.  }
38f9a 0a 0a 20 20 2f 2a 20 53 74 65 70 20 34 20 2a 2f  ..  /* Step 4 */
38f9b 0a 20 20 73 77 69 74 63 68 28 20 7a 5b 31 5d 20  .  switch( z[1] 
38f9c 29 7b 0a 20 20 20 63 61 73 65 20 27 61 27 3a 0a  ){.   case 'a':.
38f9d 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27       if( z[0]=='
38f9e 6c 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 32  l' && m_gt_1(z+2
38f9f 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d  ) ){.       z +=
38fa0 20 32 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20   2;.     }.     
38fa1 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
38fa2 63 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30  c':.     if( z[0
38fa3 5d 3d 3d 27 65 27 20 26 26 20 7a 5b 32 5d 3d 3d  ]=='e' && z[2]==
38fa4 27 6e 27 20 26 26 20 28 7a 5b 33 5d 3d 3d 27 61  'n' && (z[3]=='a
38fa5 27 20 7c 7c 20 7a 5b 33 5d 3d 3d 27 65 27 29 20  ' || z[3]=='e') 
38fa6 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 34 29 20   && m_gt_1(z+4) 
38fa7 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20   ){.       z += 
38fa8 34 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62  4;.     }.     b
38fa9 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 65  reak;.   case 'e
38faa 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d  ':.     if( z[0]
38fab 3d 3d 27 72 27 20 26 26 20 6d 5f 67 74 5f 31 28  =='r' && m_gt_1(
38fac 7a 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 20 7a  z+2) ){.       z
38fad 20 2b 3d 20 32 3b 0a 20 20 20 20 20 7d 0a 20 20   += 2;.     }.  
38fae 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73     break;.   cas
38faf 65 20 27 69 27 3a 0a 20 20 20 20 20 69 66 28 20  e 'i':.     if( 
38fb0 7a 5b 30 5d 3d 3d 27 63 27 20 26 26 20 6d 5f 67  z[0]=='c' && m_g
38fb1 74 5f 31 28 7a 2b 32 29 20 29 7b 0a 20 20 20 20  t_1(z+2) ){.    
38fb2 20 20 20 7a 20 2b 3d 20 32 3b 0a 20 20 20 20 20     z += 2;.     
38fb3 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  }.     break;.  
38fb4 20 63 61 73 65 20 27 6c 27 3a 0a 20 20 20 20 20   case 'l':.     
38fb5 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20 26 26  if( z[0]=='e' &&
38fb6 20 7a 5b 32 5d 3d 3d 27 62 27 20 26 26 20 28 7a   z[2]=='b' && (z
38fb7 5b 33 5d 3d 3d 27 61 27 20 7c 7c 20 7a 5b 33 5d  [3]=='a' || z[3]
38fb8 3d 3d 27 69 27 29 20 26 26 20 6d 5f 67 74 5f 31  =='i') && m_gt_1
38fb9 28 7a 2b 34 29 20 29 7b 0a 20 20 20 20 20 20 20  (z+4) ){.       
38fba 7a 20 2b 3d 20 34 3b 0a 20 20 20 20 20 7d 0a 20  z += 4;.     }. 
38fbb 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61      break;.   ca
38fbc 73 65 20 27 6e 27 3a 0a 20 20 20 20 20 69 66 28  se 'n':.     if(
38fbd 20 7a 5b 30 5d 3d 3d 27 74 27 20 29 7b 0a 20 20   z[0]=='t' ){.  
38fbe 20 20 20 20 20 69 66 28 20 7a 5b 32 5d 3d 3d 27       if( z[2]=='
38fbf 61 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20 69  a' ){.         i
38fc0 66 28 20 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29  f( m_gt_1(z+3) )
38fc1 7b 0a 20 20 20 20 20 20 20 20 20 20 20 7a 20 2b  {.           z +
38fc2 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20 7d 0a  = 3;.         }.
38fc3 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
38fc4 20 7a 5b 32 5d 3d 3d 27 65 27 20 29 7b 0a 20 20   z[2]=='e' ){.  
38fc5 20 20 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20         stem(&z, 
38fc6 22 74 6e 65 6d 65 22 2c 20 22 22 2c 20 6d 5f 67  "tneme", "", m_g
38fc7 74 5f 31 29 20 7c 7c 0a 20 20 20 20 20 20 20 20  t_1) ||.        
38fc8 20 73 74 65 6d 28 26 7a 2c 20 22 74 6e 65 6d 22   stem(&z, "tnem"
38fc9 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 20 7c 7c  , "", m_gt_1) ||
38fca 0a 20 20 20 20 20 20 20 20 20 73 74 65 6d 28 26  .         stem(&
38fcb 7a 2c 20 22 74 6e 65 22 2c 20 22 22 2c 20 6d 5f  z, "tne", "", m_
38fcc 67 74 5f 31 29 3b 0a 20 20 20 20 20 20 20 7d 0a  gt_1);.       }.
38fcd 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61       }.     brea
38fce 6b 3b 0a 20 20 20 63 61 73 65 20 27 6f 27 3a 0a  k;.   case 'o':.
38fcf 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27       if( z[0]=='
38fd0 75 27 20 29 7b 0a 20 20 20 20 20 20 20 69 66 28  u' ){.       if(
38fd1 20 6d 5f 67 74 5f 31 28 7a 2b 32 29 20 29 7b 0a   m_gt_1(z+2) ){.
38fd2 20 20 20 20 20 20 20 20 20 7a 20 2b 3d 20 32 3b           z += 2;
38fd3 0a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 7d  .       }.     }
38fd4 65 6c 73 65 20 69 66 28 20 7a 5b 33 5d 3d 3d 27  else if( z[3]=='
38fd5 73 27 20 7c 7c 20 7a 5b 33 5d 3d 3d 27 74 27 20  s' || z[3]=='t' 
38fd6 29 7b 0a 20 20 20 20 20 20 20 73 74 65 6d 28 26  ){.       stem(&
38fd7 7a 2c 20 22 6e 6f 69 22 2c 20 22 22 2c 20 6d 5f  z, "noi", "", m_
38fd8 67 74 5f 31 29 3b 0a 20 20 20 20 20 7d 0a 20 20  gt_1);.     }.  
38fd9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73     break;.   cas
38fda 65 20 27 73 27 3a 0a 20 20 20 20 20 69 66 28 20  e 's':.     if( 
38fdb 7a 5b 30 5d 3d 3d 27 6d 27 20 26 26 20 7a 5b 32  z[0]=='m' && z[2
38fdc 5d 3d 3d 27 69 27 20 26 26 20 6d 5f 67 74 5f 31  ]=='i' && m_gt_1
38fdd 28 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20 20 20  (z+3) ){.       
38fde 7a 20 2b 3d 20 33 3b 0a 20 20 20 20 20 7d 0a 20  z += 3;.     }. 
38fdf 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61      break;.   ca
38fe0 73 65 20 27 74 27 3a 0a 20 20 20 20 20 73 74 65  se 't':.     ste
38fe1 6d 28 26 7a 2c 20 22 65 74 61 22 2c 20 22 22 2c  m(&z, "eta", "",
38fe2 20 6d 5f 67 74 5f 31 29 20 7c 7c 0a 20 20 20 20   m_gt_1) ||.    
38fe3 20 73 74 65 6d 28 26 7a 2c 20 22 69 74 69 22 2c   stem(&z, "iti",
38fe4 20 22 22 2c 20 6d 5f 67 74 5f 31 29 3b 0a 20 20   "", m_gt_1);.  
38fe5 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73     break;.   cas
38fe6 65 20 27 75 27 3a 0a 20 20 20 20 20 69 66 28 20  e 'u':.     if( 
38fe7 7a 5b 30 5d 3d 3d 27 73 27 20 26 26 20 7a 5b 32  z[0]=='s' && z[2
38fe8 5d 3d 3d 27 6f 27 20 26 26 20 6d 5f 67 74 5f 31  ]=='o' && m_gt_1
38fe9 28 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20 20 20  (z+3) ){.       
38fea 7a 20 2b 3d 20 33 3b 0a 20 20 20 20 20 7d 0a 20  z += 3;.     }. 
38feb 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61      break;.   ca
38fec 73 65 20 27 76 27 3a 0a 20 20 20 63 61 73 65 20  se 'v':.   case 
38fed 27 7a 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b  'z':.     if( z[
38fee 30 5d 3d 3d 27 65 27 20 26 26 20 7a 5b 32 5d 3d  0]=='e' && z[2]=
38fef 3d 27 69 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a  ='i' && m_gt_1(z
38ff0 2b 33 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20  +3) ){.       z 
38ff1 2b 3d 20 33 3b 0a 20 20 20 20 20 7d 0a 20 20 20  += 3;.     }.   
38ff2 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20    break;.  }..  
38ff3 2f 2a 20 53 74 65 70 20 35 61 20 2a 2f 0a 20 20  /* Step 5a */.  
38ff4 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20 29 7b  if( z[0]=='e' ){
38ff5 0a 20 20 20 20 69 66 28 20 6d 5f 67 74 5f 31 28  .    if( m_gt_1(
38ff6 7a 2b 31 29 20 29 7b 0a 20 20 20 20 20 20 7a 2b  z+1) ){.      z+
38ff7 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  +;.    }else if(
38ff8 20 6d 5f 65 71 5f 31 28 7a 2b 31 29 20 26 26 20   m_eq_1(z+1) && 
38ff9 21 73 74 61 72 5f 6f 68 28 7a 2b 31 29 20 29 7b  !star_oh(z+1) ){
38ffa 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20  .      z++;.    
38ffb 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70  }.  }..  /* Step
38ffc 20 35 62 20 2a 2f 0a 20 20 69 66 28 20 6d 5f 67   5b */.  if( m_g
38ffd 74 5f 31 28 7a 29 20 26 26 20 7a 5b 30 5d 3d 3d  t_1(z) && z[0]==
38ffe 27 6c 27 20 26 26 20 7a 5b 31 5d 3d 3d 27 6c 27  'l' && z[1]=='l'
38fff 20 29 7b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d   ){.    z++;.  }
39000 0a 0a 20 20 2f 2a 20 7a 5b 5d 20 69 73 20 6e 6f  ..  /* z[] is no
39001 77 20 74 68 65 20 73 74 65 6d 6d 65 64 20 77 6f  w the stemmed wo
39002 72 64 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72  rd in reverse or
39003 64 65 72 2e 20 20 46 6c 69 70 20 69 74 20 62 61  der.  Flip it ba
39004 63 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 20 69  ck.  ** around i
39005 6e 74 6f 20 66 6f 72 77 61 72 64 20 6f 72 64 65  nto forward orde
39006 72 20 61 6e 64 20 72 65 74 75 72 6e 2e 0a 20 20  r and return..  
39007 2a 2f 0a 20 20 2a 70 6e 4f 75 74 20 3d 20 69 20  */.  *pnOut = i 
39008 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 7a  = strlen(z);.  z
39009 4f 75 74 5b 69 5d 20 3d 20 30 3b 0a 20 20 77 68  Out[i] = 0;.  wh
3900a 69 6c 65 28 20 2a 7a 20 29 7b 0a 20 20 20 20 7a  ile( *z ){.    z
3900b 4f 75 74 5b 2d 2d 69 5d 20 3d 20 2a 28 7a 2b 2b  Out[--i] = *(z++
3900c 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
3900d 43 68 61 72 61 63 74 65 72 73 20 74 68 61 74 20  Characters that 
3900e 63 61 6e 20 62 65 20 70 61 72 74 20 6f 66 20 61  can be part of a
3900f 20 74 6f 6b 65 6e 2e 20 20 57 65 20 61 73 73 75   token.  We assu
39010 6d 65 20 61 6e 79 20 63 68 61 72 61 63 74 65 72  me any character
39011 0a 2a 2a 20 77 68 6f 73 65 20 76 61 6c 75 65 20  .** whose value 
39012 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  is greater than 
39013 30 78 38 30 20 28 61 6e 79 20 55 54 46 20 63 68  0x80 (any UTF ch
39014 61 72 61 63 74 65 72 29 20 63 61 6e 20 62 65 0a  aracter) can be.
39015 2a 2a 20 70 61 72 74 20 6f 66 20 61 20 74 6f 6b  ** part of a tok
39016 65 6e 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f  en.  In other wo
39017 72 64 73 2c 20 64 65 6c 69 6d 69 74 65 72 73 20  rds, delimiters 
39018 61 6c 6c 20 6d 75 73 74 20 68 61 76 65 0a 2a 2a  all must have.**
39019 20 76 61 6c 75 65 73 20 6f 66 20 30 78 37 66 20   values of 0x7f 
3901a 6f 72 20 6c 6f 77 65 72 2e 0a 2a 2f 0a 73 74 61  or lower..*/.sta
3901b 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 70  tic const char p
3901c 6f 72 74 65 72 49 64 43 68 61 72 5b 5d 20 3d 20  orterIdChar[] = 
3901d 7b 0a 2f 2a 20 78 30 20 78 31 20 78 32 20 78 33  {./* x0 x1 x2 x3
3901e 20 78 34 20 78 35 20 78 36 20 78 37 20 78 38 20   x4 x5 x6 x7 x8 
3901f 78 39 20 78 41 20 78 42 20 78 43 20 78 44 20 78  x9 xA xB xC xD x
39020 45 20 78 46 20 2a 2f 0a 20 20 20 20 31 2c 20 31  E xF */.    1, 1
39021 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
39022 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20   1, 1, 1, 0, 0, 
39023 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
39024 33 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20  3x */.    0, 1, 
39025 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
39026 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
39027 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 34 78   1, 1, 1,  /* 4x
39028 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c   */.    1, 1, 1,
39029 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
3902a 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30  1, 1, 1, 0, 0, 0
3902b 2c 20 30 2c 20 31 2c 20 20 2f 2a 20 35 78 20 2a  , 0, 1,  /* 5x *
3902c 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31  /.    0, 1, 1, 1
3902d 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
3902e 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
3902f 31 2c 20 31 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a  1, 1,  /* 6x */.
39030 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20      1, 1, 1, 1, 
39031 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
39032 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 0, 0, 0, 0,
39033 20 30 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a 7d 3b   0,  /* 7x */.};
39034 0a 23 64 65 66 69 6e 65 20 69 73 44 65 6c 69 6d  .#define isDelim
39035 28 43 29 20 28 28 28 63 68 3d 43 29 26 30 78 38  (C) (((ch=C)&0x8
39036 30 29 3d 3d 30 20 26 26 20 28 63 68 3c 30 78 33  0)==0 && (ch<0x3
39037 30 20 7c 7c 20 21 70 6f 72 74 65 72 49 64 43 68  0 || !porterIdCh
39038 61 72 5b 63 68 2d 30 78 33 30 5d 29 29 0a 0a 2f  ar[ch-0x30]))../
39039 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68 65  *.** Extract the
3903a 20 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d   next token from
3903b 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20   a tokenization 
3903c 63 75 72 73 6f 72 2e 20 20 54 68 65 20 63 75 72  cursor.  The cur
3903d 73 6f 72 20 6d 75 73 74 0a 2a 2a 20 68 61 76 65  sor must.** have
3903e 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 79 20   been opened by 
3903f 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f 20  a prior call to 
39040 70 6f 72 74 65 72 4f 70 65 6e 28 29 2e 0a 2a 2f  porterOpen()..*/
39041 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6f 72 74  .static int port
39042 65 72 4e 65 78 74 28 0a 20 20 73 71 6c 69 74 65  erNext(.  sqlite
39043 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
39044 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 20 2f 2a  or *pCursor,  /*
39045 20 43 75 72 73 6f 72 20 72 65 74 75 72 6e 65 64   Cursor returned
39046 20 62 79 20 70 6f 72 74 65 72 4f 70 65 6e 20 2a   by porterOpen *
39047 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
39048 2a 70 7a 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20  *pzToken,       
39049 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
3904a 2a 70 7a 54 6f 6b 65 6e 20 69 73 20 74 68 65 20  *pzToken is the 
3904b 74 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0a 20 20  token text */.  
3904c 69 6e 74 20 2a 70 6e 42 79 74 65 73 2c 20 20 20  int *pnBytes,   
3904d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3904e 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62      /* OUT: Numb
3904f 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74  er of bytes in t
39050 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  oken */.  int *p
39051 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 20 20  iStartOffset,   
39052 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
39053 20 4f 55 54 3a 20 53 74 61 72 74 69 6e 67 20 6f   OUT: Starting o
39054 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a  ffset of token *
39055 2f 0a 20 20 69 6e 74 20 2a 70 69 45 6e 64 4f 66  /.  int *piEndOf
39056 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 20  fset,           
39057 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
39058 45 6e 64 69 6e 67 20 6f 66 66 73 65 74 20 6f 66  Ending offset of
39059 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20   token */.  int 
3905a 2a 70 69 50 6f 73 69 74 69 6f 6e 20 20 20 20 20  *piPosition     
3905b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3905c 2f 2a 20 4f 55 54 3a 20 50 6f 73 69 74 69 6f 6e  /* OUT: Position
3905d 20 69 6e 74 65 67 65 72 20 6f 66 20 74 6f 6b 65   integer of toke
3905e 6e 20 2a 2f 0a 29 7b 0a 20 20 70 6f 72 74 65 72  n */.){.  porter
3905f 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
39060 72 20 2a 63 20 3d 20 28 70 6f 72 74 65 72 5f 74  r *c = (porter_t
39061 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
39062 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 63 6f  *) pCursor;.  co
39063 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 63 2d  nst char *z = c-
39064 3e 7a 49 6e 70 75 74 3b 0a 0a 20 20 77 68 69 6c  >zInput;..  whil
39065 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d  e( c->iOffset<c-
39066 3e 6e 49 6e 70 75 74 20 29 7b 0a 20 20 20 20 69  >nInput ){.    i
39067 6e 74 20 69 53 74 61 72 74 4f 66 66 73 65 74 2c  nt iStartOffset,
39068 20 63 68 3b 0a 0a 20 20 20 20 2f 2a 20 53 63 61   ch;..    /* Sca
39069 6e 20 70 61 73 74 20 64 65 6c 69 6d 69 74 65 72  n past delimiter
3906a 20 63 68 61 72 61 63 74 65 72 73 20 2a 2f 0a 20   characters */. 
3906b 20 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66     while( c->iOf
3906c 66 73 65 74 3c 63 2d 3e 6e 49 6e 70 75 74 20 26  fset<c->nInput &
3906d 26 20 69 73 44 65 6c 69 6d 28 7a 5b 63 2d 3e 69  & isDelim(z[c->i
3906e 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20 20 20 20  Offset]) ){.    
3906f 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b 0a    c->iOffset++;.
39070 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f      }..    /* Co
39071 75 6e 74 20 6e 6f 6e 2d 64 65 6c 69 6d 69 74 65  unt non-delimite
39072 72 20 63 68 61 72 61 63 74 65 72 73 2e 20 2a 2f  r characters. */
39073 0a 20 20 20 20 69 53 74 61 72 74 4f 66 66 73 65  .    iStartOffse
39074 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74 3b 0a  t = c->iOffset;.
39075 20 20 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f      while( c->iO
39076 66 66 73 65 74 3c 63 2d 3e 6e 49 6e 70 75 74 20  ffset<c->nInput 
39077 26 26 20 21 69 73 44 65 6c 69 6d 28 7a 5b 63 2d  && !isDelim(z[c-
39078 3e 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20 20  >iOffset]) ){.  
39079 20 20 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b 2b      c->iOffset++
3907a 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
3907b 20 63 2d 3e 69 4f 66 66 73 65 74 3e 69 53 74 61   c->iOffset>iSta
3907c 72 74 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20  rtOffset ){.    
3907d 20 20 69 6e 74 20 6e 20 3d 20 63 2d 3e 69 4f 66    int n = c->iOf
3907e 66 73 65 74 2d 69 53 74 61 72 74 4f 66 66 73 65  fset-iStartOffse
3907f 74 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 3e 63  t;.      if( n>c
39080 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64 20 29 7b 0a  ->nAllocated ){.
39081 20 20 20 20 20 20 20 20 63 2d 3e 6e 41 6c 6c 6f          c->nAllo
39082 63 61 74 65 64 20 3d 20 6e 2b 32 30 3b 0a 20 20  cated = n+20;.  
39083 20 20 20 20 20 20 63 2d 3e 7a 54 6f 6b 65 6e 20        c->zToken 
39084 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  = sqlite3_reallo
39085 63 28 63 2d 3e 7a 54 6f 6b 65 6e 2c 20 63 2d 3e  c(c->zToken, c->
39086 6e 41 6c 6c 6f 63 61 74 65 64 29 3b 0a 20 20 20  nAllocated);.   
39087 20 20 20 20 20 69 66 28 20 63 2d 3e 7a 54 6f 6b       if( c->zTok
39088 65 6e 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72  en==NULL ) retur
39089 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
3908a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 6f        }.      po
3908b 72 74 65 72 5f 73 74 65 6d 6d 65 72 28 26 7a 5b  rter_stemmer(&z[
3908c 69 53 74 61 72 74 4f 66 66 73 65 74 5d 2c 20 6e  iStartOffset], n
3908d 2c 20 63 2d 3e 7a 54 6f 6b 65 6e 2c 20 70 6e 42  , c->zToken, pnB
3908e 79 74 65 73 29 3b 0a 20 20 20 20 20 20 2a 70 7a  ytes);.      *pz
3908f 54 6f 6b 65 6e 20 3d 20 63 2d 3e 7a 54 6f 6b 65  Token = c->zToke
39090 6e 3b 0a 20 20 20 20 20 20 2a 70 69 53 74 61 72  n;.      *piStar
39091 74 4f 66 66 73 65 74 20 3d 20 69 53 74 61 72 74  tOffset = iStart
39092 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 2a 70  Offset;.      *p
39093 69 45 6e 64 4f 66 66 73 65 74 20 3d 20 63 2d 3e  iEndOffset = c->
39094 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 2a  iOffset;.      *
39095 70 69 50 6f 73 69 74 69 6f 6e 20 3d 20 63 2d 3e  piPosition = c->
39096 69 54 6f 6b 65 6e 2b 2b 3b 0a 20 20 20 20 20 20  iToken++;.      
39097 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
39098 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
39099 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45  turn SQLITE_DONE
3909a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  ;.}../*.** The s
3909b 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 74  et of routines t
3909c 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68  hat implement th
3909d 65 20 70 6f 72 74 65 72 2d 73 74 65 6d 6d 65 72  e porter-stemmer
3909e 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74   tokenizer.*/.st
3909f 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74  atic const sqlit
390a0 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
390a1 75 6c 65 20 70 6f 72 74 65 72 54 6f 6b 65 6e 69  ule porterTokeni
390a2 7a 65 72 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20  zerModule = {.  
390a3 30 2c 0a 20 20 70 6f 72 74 65 72 43 72 65 61 74  0,.  porterCreat
390a4 65 2c 0a 20 20 70 6f 72 74 65 72 44 65 73 74 72  e,.  porterDestr
390a5 6f 79 2c 0a 20 20 70 6f 72 74 65 72 4f 70 65 6e  oy,.  porterOpen
390a6 2c 0a 20 20 70 6f 72 74 65 72 43 6c 6f 73 65 2c  ,.  porterClose,
390a7 0a 20 20 70 6f 72 74 65 72 4e 65 78 74 2c 0a 7d  .  porterNext,.}
390a8 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74  ;../*.** Allocat
390a9 65 20 61 20 6e 65 77 20 70 6f 72 74 65 72 20 74  e a new porter t
390aa 6f 6b 65 6e 69 7a 65 72 2e 20 20 52 65 74 75 72  okenizer.  Retur
390ab 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
390ac 68 65 20 6e 65 77 0a 2a 2a 20 74 6f 6b 65 6e 69  he new.** tokeni
390ad 7a 65 72 20 69 6e 20 2a 70 70 4d 6f 64 75 6c 65  zer in *ppModule
390ae 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
390af 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
390b0 74 73 33 50 6f 72 74 65 72 54 6f 6b 65 6e 69 7a  ts3PorterTokeniz
390b1 65 72 4d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69  erModule(.  sqli
390b2 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
390b3 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f  dule const**ppMo
390b4 64 75 6c 65 0a 29 7b 0a 20 20 2a 70 70 4d 6f 64  dule.){.  *ppMod
390b5 75 6c 65 20 3d 20 26 70 6f 72 74 65 72 54 6f 6b  ule = &porterTok
390b6 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a 7d 0a  enizerModule;.}.
390b7 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
390b8 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29  ned(SQLITE_CORE)
390b9 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
390ba 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20  TE_ENABLE_FTS3) 
390bb 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
390bc 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f  *** End of fts3_
390bd 70 6f 72 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a  porter.c *******
390be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
390bf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
390c0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
390c1 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66  *** Begin file f
390c2 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 63 20  ts3_tokenizer.c 
390c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
390c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
390c5 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4a  **/./*.** 2007 J
390c6 75 6e 65 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65  une 22.**.** The
390c7 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
390c8 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
390c9 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
390ca 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
390cb 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
390cc 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
390cd 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
390ce 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
390cf 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
390d0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
390d1 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
390d2 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
390d3 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
390d4 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
390d5 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
390d6 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
390d7 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
390d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
390d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
390da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
390db 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
390dc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
390dd 20 54 68 69 73 20 69 73 20 70 61 72 74 20 6f 66   This is part of
390de 20 61 6e 20 53 51 4c 69 74 65 20 6d 6f 64 75 6c   an SQLite modul
390df 65 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 66  e implementing f
390e0 75 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68 2e  ull-text search.
390e1 0a 2a 2a 20 54 68 69 73 20 70 61 72 74 69 63 75  .** This particu
390e2 6c 61 72 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65  lar file impleme
390e3 6e 74 73 20 74 68 65 20 67 65 6e 65 72 69 63 20  nts the generic 
390e4 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 74 65 72 66  tokenizer interf
390e5 61 63 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54  ace..*/../*.** T
390e6 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20  he code in this 
390e7 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63 6f 6d  file is only com
390e8 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20  piled if:.**.** 
390e9 20 20 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d      * The FTS3 m
390ea 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62  odule is being b
390eb 75 69 6c 74 20 61 73 20 61 6e 20 65 78 74 65 6e  uilt as an exten
390ec 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 28 69  sion.**       (i
390ed 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c  n which case SQL
390ee 49 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f 74 20  ITE_CORE is not 
390ef 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a  defined), or.**.
390f0 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53  **     * The FTS
390f1 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e  3 module is bein
390f2 67 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65  g built into the
390f3 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20 20 20   core of.**     
390f4 20 20 53 51 4c 69 74 65 20 28 69 6e 20 77 68 69    SQLite (in whi
390f5 63 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45  ch case SQLITE_E
390f6 4e 41 42 4c 45 5f 46 54 53 33 20 69 73 20 64 65  NABLE_FTS3 is de
390f7 66 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66 20 21  fined)..*/.#if !
390f8 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43  defined(SQLITE_C
390f9 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  ORE) || defined(
390fa 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54  SQLITE_ENABLE_FT
390fb 53 33 29 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  S3)..#ifndef SQL
390fc 49 54 45 5f 43 4f 52 45 0a 20 20 53 51 4c 49 54  ITE_CORE.  SQLIT
390fd 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49 54  E_EXTENSION_INIT
390fe 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  1.#endif.../*.**
390ff 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
39100 6f 66 20 74 68 65 20 53 51 4c 20 73 63 61 6c 61  of the SQL scala
39101 72 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61  r function for a
39102 63 63 65 73 73 69 6e 67 20 74 68 65 20 75 6e 64  ccessing the und
39103 65 72 6c 79 69 6e 67 20 0a 2a 2a 20 68 61 73 68  erlying .** hash
39104 20 74 61 62 6c 65 2e 20 54 68 69 73 20 66 75 6e   table. This fun
39105 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 63 61 6c  ction may be cal
39106 6c 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  led as follows:.
39107 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c  **.**   SELECT <
39108 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c  function-name>(<
39109 6b 65 79 2d 6e 61 6d 65 3e 29 3b 0a 2a 2a 20 20  key-name>);.**  
3910a 20 53 45 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f   SELECT <functio
3910b 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d  n-name>(<key-nam
3910c 65 3e 2c 20 3c 70 6f 69 6e 74 65 72 3e 29 3b 0a  e>, <pointer>);.
3910d 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 3c 66 75 6e  **.** where <fun
3910e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20 74  ction-name> is t
3910f 68 65 20 6e 61 6d 65 20 70 61 73 73 65 64 20 61  he name passed a
39110 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
39111 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20  ument.** to the 
39112 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 48  sqlite3Fts3InitH
39113 61 73 68 54 61 62 6c 65 28 29 20 66 75 6e 63 74  ashTable() funct
39114 69 6f 6e 20 28 65 2e 67 2e 20 27 66 74 73 33 5f  ion (e.g. 'fts3_
39115 74 6f 6b 65 6e 69 7a 65 72 27 29 2e 0a 2a 2a 0a  tokenizer')..**.
39116 2a 2a 20 49 66 20 74 68 65 20 3c 70 6f 69 6e 74  ** If the <point
39117 65 72 3e 20 61 72 67 75 6d 65 6e 74 20 69 73 20  er> argument is 
39118 73 70 65 63 69 66 69 65 64 2c 20 69 74 20 6d 75  specified, it mu
39119 73 74 20 62 65 20 61 20 62 6c 6f 62 20 76 61 6c  st be a blob val
3911a 75 65 0a 2a 2a 20 63 6f 6e 74 61 69 6e 69 6e 67  ue.** containing
3911b 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 62 65   a pointer to be
3911c 20 73 74 6f 72 65 64 20 61 73 20 74 68 65 20 68   stored as the h
3911d 61 73 68 20 64 61 74 61 20 63 6f 72 72 65 73 70  ash data corresp
3911e 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20 74 68 65  onding.** to the
3911f 20 73 74 72 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d   string <key-nam
39120 65 3e 2e 20 49 66 20 3c 70 6f 69 6e 74 65 72 3e  e>. If <pointer>
39121 20 69 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65   is not specifie
39122 64 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 73  d, then.** the s
39123 74 72 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d 65 3e  tring <key-name>
39124 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20 65 78   must already ex
39125 69 73 74 20 69 6e 20 74 68 65 20 68 61 73 20 74  ist in the has t
39126 61 62 6c 65 2e 20 4f 74 68 65 72 77 69 73 65 2c  able. Otherwise,
39127 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 69 73 20  .** an error is 
39128 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
39129 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  Whether or not t
3912a 68 65 20 3c 70 6f 69 6e 74 65 72 3e 20 61 72 67  he <pointer> arg
3912b 75 6d 65 6e 74 20 69 73 20 73 70 65 63 69 66 69  ument is specifi
3912c 65 64 2c 20 74 68 65 20 76 61 6c 75 65 20 72 65  ed, the value re
3912d 74 75 72 6e 65 64 0a 2a 2a 20 69 73 20 61 20 62  turned.** is a b
3912e 6c 6f 62 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  lob containing t
3912f 68 65 20 70 6f 69 6e 74 65 72 20 73 74 6f 72 65  he pointer store
39130 64 20 61 73 20 74 68 65 20 68 61 73 68 20 64 61  d as the hash da
39131 74 61 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ta corresponding
39132 0a 2a 2a 20 74 6f 20 73 74 72 69 6e 67 20 3c 6b  .** to string <k
39133 65 79 2d 6e 61 6d 65 3e 20 28 61 66 74 65 72 20  ey-name> (after 
39134 74 68 65 20 68 61 73 68 2d 74 61 62 6c 65 20 69  the hash-table i
39135 73 20 75 70 64 61 74 65 64 2c 20 69 66 20 61 70  s updated, if ap
39136 70 6c 69 63 61 62 6c 65 29 2e 0a 2a 2f 0a 73 74  plicable)..*/.st
39137 61 74 69 63 20 76 6f 69 64 20 73 63 61 6c 61 72  atic void scalar
39138 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
39139 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
3913a 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
3913b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
3913c 61 72 67 76 0a 29 7b 0a 20 20 66 74 73 33 48 61  argv.){.  fts3Ha
3913d 73 68 20 2a 70 48 61 73 68 3b 0a 20 20 76 6f 69  sh *pHash;.  voi
3913e 64 20 2a 70 50 74 72 20 3d 20 30 3b 0a 20 20 63  d *pPtr = 0;.  c
3913f 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
39140 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69 6e 74  ar *zName;.  int
39141 20 6e 4e 61 6d 65 3b 0a 0a 20 20 61 73 73 65 72   nName;..  asser
39142 74 28 20 61 72 67 63 3d 3d 31 20 7c 7c 20 61 72  t( argc==1 || ar
39143 67 63 3d 3d 32 20 29 3b 0a 0a 20 20 70 48 61 73  gc==2 );..  pHas
39144 68 20 3d 20 28 66 74 73 33 48 61 73 68 20 2a 29  h = (fts3Hash *)
39145 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
39146 61 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 7a  a(context);..  z
39147 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f 76  Name = sqlite3_v
39148 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
39149 5d 29 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 71  ]);.  nName = sq
3914a 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
3914b 73 28 61 72 67 76 5b 30 5d 29 2b 31 3b 0a 0a 20  s(argv[0])+1;.. 
3914c 20 69 66 28 20 61 72 67 63 3d 3d 32 20 29 7b 0a   if( argc==2 ){.
3914d 20 20 20 20 76 6f 69 64 20 2a 70 4f 6c 64 3b 0a      void *pOld;.
3914e 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c 69      int n = sqli
3914f 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
39150 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 69 66  argv[1]);.    if
39151 28 20 6e 21 3d 73 69 7a 65 6f 66 28 70 50 74 72  ( n!=sizeof(pPtr
39152 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ) ){.      sqlit
39153 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
39154 63 6f 6e 74 65 78 74 2c 20 22 61 72 67 75 6d 65  context, "argume
39155 6e 74 20 74 79 70 65 20 6d 69 73 6d 61 74 63 68  nt type mismatch
39156 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 72 65  ", -1);.      re
39157 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20  turn;.    }.    
39158 70 50 74 72 20 3d 20 2a 28 76 6f 69 64 20 2a 2a  pPtr = *(void **
39159 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62  )sqlite3_value_b
3915a 6c 6f 62 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  lob(argv[1]);.  
3915b 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33    pOld = sqlite3
3915c 46 74 73 33 48 61 73 68 49 6e 73 65 72 74 28 70  Fts3HashInsert(p
3915d 48 61 73 68 2c 20 28 76 6f 69 64 20 2a 29 7a 4e  Hash, (void *)zN
3915e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70 50 74 72  ame, nName, pPtr
3915f 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 3d  );.    if( pOld=
39160 3d 70 50 74 72 20 29 7b 0a 20 20 20 20 20 20 73  =pPtr ){.      s
39161 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
39162 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 6f 75  ror(context, "ou
39163 74 20 6f 66 20 6d 65 6d 6f 72 79 22 2c 20 2d 31  t of memory", -1
39164 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  );.      return;
39165 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
39166 20 20 20 20 70 50 74 72 20 3d 20 73 71 6c 69 74      pPtr = sqlit
39167 65 33 46 74 73 33 48 61 73 68 46 69 6e 64 28 70  e3Fts3HashFind(p
39168 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61  Hash, zName, nNa
39169 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 21 70 50  me);.    if( !pP
3916a 74 72 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72  tr ){.      char
3916b 20 2a 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33   *zErr = sqlite3
3916c 5f 6d 70 72 69 6e 74 66 28 22 75 6e 6b 6e 6f 77  _mprintf("unknow
3916d 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 25 73 22  n tokenizer: %s"
3916e 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  , zName);.      
3916f 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
39170 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45  rror(context, zE
39171 72 72 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 73  rr, -1);.      s
39172 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72 72  qlite3_free(zErr
39173 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  );.      return;
39174 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71  .    }.  }..  sq
39175 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f  lite3_result_blo
39176 62 28 63 6f 6e 74 65 78 74 2c 20 28 76 6f 69 64  b(context, (void
39177 20 2a 29 26 70 50 74 72 2c 20 73 69 7a 65 6f 66   *)&pPtr, sizeof
39178 28 70 50 74 72 29 2c 20 53 51 4c 49 54 45 5f 54  (pPtr), SQLITE_T
39179 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a 0a 23 69  RANSIENT);.}..#i
3917a 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
3917b 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  .../*.** Impleme
3917c 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 73 70 65  ntation of a spe
3917d 63 69 61 6c 20 53 51 4c 20 73 63 61 6c 61 72 20  cial SQL scalar 
3917e 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 74 65 73  function for tes
3917f 74 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72 73 20  ting tokenizers 
39180 0a 2a 2a 20 64 65 73 69 67 6e 65 64 20 74 6f 20  .** designed to 
39181 62 65 20 75 73 65 64 20 69 6e 20 63 6f 6e 63 65  be used in conce
39182 72 74 20 77 69 74 68 20 74 68 65 20 54 63 6c 20  rt with the Tcl 
39183 74 65 73 74 69 6e 67 20 66 72 61 6d 65 77 6f 72  testing framewor
39184 6b 2e 20 54 68 69 73 0a 2a 2a 20 66 75 6e 63 74  k. This.** funct
39185 69 6f 6e 20 6d 75 73 74 20 62 65 20 63 61 6c 6c  ion must be call
39186 65 64 20 77 69 74 68 20 74 77 6f 20 61 72 67 75  ed with two argu
39187 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 53  ments:.**.**   S
39188 45 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f 6e 2d  ELECT <function-
39189 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d 65 3e  name>(<key-name>
3918a 2c 20 3c 69 6e 70 75 74 2d 73 74 72 69 6e 67 3e  , <input-string>
3918b 29 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c  );.**   SELECT <
3918c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c  function-name>(<
3918d 6b 65 79 2d 6e 61 6d 65 3e 2c 20 3c 70 6f 69 6e  key-name>, <poin
3918e 74 65 72 3e 29 3b 0a 2a 2a 0a 2a 2a 20 77 68 65  ter>);.**.** whe
3918f 72 65 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d  re <function-nam
39190 65 3e 20 69 73 20 74 68 65 20 6e 61 6d 65 20 70  e> is the name p
39191 61 73 73 65 64 20 61 73 20 74 68 65 20 73 65 63  assed as the sec
39192 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20  ond argument.** 
39193 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 46 74  to the sqlite3Ft
39194 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c 65 28  s3InitHashTable(
39195 29 20 66 75 6e 63 74 69 6f 6e 20 28 65 2e 67 2e  ) function (e.g.
39196 20 27 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72   'fts3_tokenizer
39197 27 29 0a 2a 2a 20 63 6f 6e 63 61 74 65 6e 61 74  ').** concatenat
39198 65 64 20 77 69 74 68 20 74 68 65 20 73 74 72 69  ed with the stri
39199 6e 67 20 27 5f 74 65 73 74 27 20 28 65 2e 67 2e  ng '_test' (e.g.
3919a 20 27 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72   'fts3_tokenizer
3919b 5f 74 65 73 74 27 29 2e 0a 2a 2a 0a 2a 2a 20 54  _test')..**.** T
3919c 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
3919d 69 73 20 61 20 73 74 72 69 6e 67 20 74 68 61 74  is a string that
3919e 20 6d 61 79 20 62 65 20 69 6e 74 65 72 70 72 65   may be interpre
3919f 74 65 64 20 61 73 20 61 20 54 63 6c 0a 2a 2a 20  ted as a Tcl.** 
391a0 6c 69 73 74 2e 20 46 6f 72 20 65 61 63 68 20 74  list. For each t
391a1 6f 6b 65 6e 20 69 6e 20 74 68 65 20 3c 69 6e 70  oken in the <inp
391a2 75 74 2d 73 74 72 69 6e 67 3e 2c 20 74 68 72 65  ut-string>, thre
391a3 65 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 0a 2a  e elements are.*
391a4 2a 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 72  * added to the r
391a5 65 74 75 72 6e 65 64 20 6c 69 73 74 2e 20 54 68  eturned list. Th
391a6 65 20 66 69 72 73 74 20 69 73 20 74 68 65 20 74  e first is the t
391a7 6f 6b 65 6e 20 70 6f 73 69 74 69 6f 6e 2c 20 74  oken position, t
391a8 68 65 20 0a 2a 2a 20 73 65 63 6f 6e 64 20 69 73  he .** second is
391a9 20 74 68 65 20 74 6f 6b 65 6e 20 74 65 78 74 20   the token text 
391aa 28 66 6f 6c 64 65 64 2c 20 73 74 65 6d 6d 65 64  (folded, stemmed
391ab 2c 20 65 74 63 2e 29 20 61 6e 64 20 74 68 65 20  , etc.) and the 
391ac 74 68 69 72 64 20 69 73 20 74 68 65 0a 2a 2a 20  third is the.** 
391ad 73 75 62 73 74 72 69 6e 67 20 6f 66 20 3c 69 6e  substring of <in
391ae 70 75 74 2d 73 74 72 69 6e 67 3e 20 61 73 73 6f  put-string> asso
391af 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
391b0 74 6f 6b 65 6e 2e 20 46 6f 72 20 65 78 61 6d 70  token. For examp
391b1 6c 65 2c 20 0a 2a 2a 20 75 73 69 6e 67 20 74 68  le, .** using th
391b2 65 20 62 75 69 6c 74 2d 69 6e 20 22 73 69 6d 70  e built-in "simp
391b3 6c 65 22 20 74 6f 6b 65 6e 69 7a 65 72 3a 0a 2a  le" tokenizer:.*
391b4 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 66 74  *.**   SELECT ft
391b5 73 5f 74 6f 6b 65 6e 69 7a 65 72 5f 74 65 73 74  s_tokenizer_test
391b6 28 27 73 69 6d 70 6c 65 27 2c 20 27 49 20 64 6f  ('simple', 'I do
391b7 6e 27 74 20 73 65 65 20 68 6f 77 27 29 3b 0a 2a  n't see how');.*
391b8 2a 0a 2a 2a 20 77 69 6c 6c 20 72 65 74 75 72 6e  *.** will return
391b9 20 74 68 65 20 73 74 72 69 6e 67 3a 0a 2a 2a 0a   the string:.**.
391ba 2a 2a 20 20 20 22 7b 30 20 69 20 49 20 31 20 64  **   "{0 i I 1 d
391bb 6f 6e 74 20 64 6f 6e 27 74 20 32 20 73 65 65 20  ont don't 2 see 
391bc 73 65 65 20 33 20 68 6f 77 20 68 6f 77 7d 22 0a  see 3 how how}".
391bd 2a 2a 20 20 20 0a 2a 2f 0a 73 74 61 74 69 63 20  **   .*/.static 
391be 76 6f 69 64 20 74 65 73 74 46 75 6e 63 28 0a 20  void testFunc(. 
391bf 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
391c0 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
391c1 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
391c2 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
391c3 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70 48 61  .  fts3Hash *pHa
391c4 73 68 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  sh;.  sqlite3_to
391c5 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a  kenizer_module *
391c6 70 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  p;.  sqlite3_tok
391c7 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a  enizer *pTokeniz
391c8 65 72 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  er = 0;.  sqlite
391c9 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
391ca 6f 72 20 2a 70 43 73 72 20 3d 20 30 3b 0a 0a 20  or *pCsr = 0;.. 
391cb 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45 72   const char *zEr
391cc 72 20 3d 20 30 3b 0a 0a 20 20 63 6f 6e 73 74 20  r = 0;..  const 
391cd 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69  char *zName;.  i
391ce 6e 74 20 6e 4e 61 6d 65 3b 0a 20 20 63 6f 6e 73  nt nName;.  cons
391cf 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74 3b 0a  t char *zInput;.
391d0 20 20 69 6e 74 20 6e 49 6e 70 75 74 3b 0a 0a 20    int nInput;.. 
391d1 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41 72   const char *zAr
391d2 67 20 3d 20 30 3b 0a 0a 20 20 63 6f 6e 73 74 20  g = 0;..  const 
391d3 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 0a 20 20  char *zToken;.  
391d4 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20 69 6e  int nToken;.  in
391d5 74 20 69 53 74 61 72 74 3b 0a 20 20 69 6e 74 20  t iStart;.  int 
391d6 69 45 6e 64 3b 0a 20 20 69 6e 74 20 69 50 6f 73  iEnd;.  int iPos
391d7 3b 0a 0a 20 20 54 63 6c 5f 4f 62 6a 20 2a 70 52  ;..  Tcl_Obj *pR
391d8 65 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 61  et;..  assert( a
391d9 72 67 63 3d 3d 32 20 7c 7c 20 61 72 67 63 3d 3d  rgc==2 || argc==
391da 33 20 29 3b 0a 0a 20 20 6e 4e 61 6d 65 20 3d 20  3 );..  nName = 
391db 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
391dc 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  tes(argv[0]);.  
391dd 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20 63  zName = (const c
391de 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76 61  har *)sqlite3_va
391df 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
391e0 29 3b 0a 20 20 6e 49 6e 70 75 74 20 3d 20 73 71  );.  nInput = sq
391e1 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
391e2 73 28 61 72 67 76 5b 61 72 67 63 2d 31 5d 29 3b  s(argv[argc-1]);
391e3 0a 20 20 7a 49 6e 70 75 74 20 3d 20 28 63 6f 6e  .  zInput = (con
391e4 73 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65  st char *)sqlite
391e5 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
391e6 76 5b 61 72 67 63 2d 31 5d 29 3b 0a 0a 20 20 69  v[argc-1]);..  i
391e7 66 28 20 61 72 67 63 3d 3d 33 20 29 7b 0a 20 20  f( argc==3 ){.  
391e8 20 20 7a 41 72 67 20 3d 20 28 63 6f 6e 73 74 20    zArg = (const 
391e9 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76  char *)sqlite3_v
391ea 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31  alue_text(argv[1
391eb 5d 29 3b 0a 20 20 7d 0a 0a 20 20 70 48 61 73 68  ]);.  }..  pHash
391ec 20 3d 20 28 66 74 73 33 48 61 73 68 20 2a 29 73   = (fts3Hash *)s
391ed 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
391ee 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 70 20 3d  (context);.  p =
391ef 20 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69   (sqlite3_tokeni
391f0 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 29 73 71 6c  zer_module *)sql
391f1 69 74 65 33 46 74 73 33 48 61 73 68 46 69 6e 64  ite3Fts3HashFind
391f2 28 70 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e  (pHash, zName, n
391f3 4e 61 6d 65 2b 31 29 3b 0a 0a 20 20 69 66 28 20  Name+1);..  if( 
391f4 21 70 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  !p ){.    char *
391f5 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d  zErr = sqlite3_m
391f6 70 72 69 6e 74 66 28 22 75 6e 6b 6e 6f 77 6e 20  printf("unknown 
391f7 74 6f 6b 65 6e 69 7a 65 72 3a 20 25 73 22 2c 20  tokenizer: %s", 
391f8 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c 69  zName);.    sqli
391f9 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
391fa 28 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20  (context, zErr, 
391fb 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  -1);.    sqlite3
391fc 5f 66 72 65 65 28 7a 45 72 72 29 3b 0a 20 20 20  _free(zErr);.   
391fd 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
391fe 70 52 65 74 20 3d 20 54 63 6c 5f 4e 65 77 4f 62  pRet = Tcl_NewOb
391ff 6a 28 29 3b 0a 20 20 54 63 6c 5f 49 6e 63 72 52  j();.  Tcl_IncrR
39200 65 66 43 6f 75 6e 74 28 70 52 65 74 29 3b 0a 0a  efCount(pRet);..
39201 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
39202 3d 70 2d 3e 78 43 72 65 61 74 65 28 7a 41 72 67  =p->xCreate(zArg
39203 20 3f 20 31 20 3a 20 30 2c 20 26 7a 41 72 67 2c   ? 1 : 0, &zArg,
39204 20 26 70 54 6f 6b 65 6e 69 7a 65 72 29 20 29 7b   &pTokenizer) ){
39205 0a 20 20 20 20 7a 45 72 72 20 3d 20 22 65 72 72  .    zErr = "err
39206 6f 72 20 69 6e 20 78 43 72 65 61 74 65 28 29 22  or in xCreate()"
39207 3b 0a 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73  ;.    goto finis
39208 68 3b 0a 20 20 7d 0a 20 20 70 54 6f 6b 65 6e 69  h;.  }.  pTokeni
39209 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 20 3d 20 70  zer->pModule = p
3920a 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ;.  if( SQLITE_O
3920b 4b 21 3d 70 2d 3e 78 4f 70 65 6e 28 70 54 6f 6b  K!=p->xOpen(pTok
3920c 65 6e 69 7a 65 72 2c 20 7a 49 6e 70 75 74 2c 20  enizer, zInput, 
3920d 6e 49 6e 70 75 74 2c 20 26 70 43 73 72 29 20 29  nInput, &pCsr) )
3920e 7b 0a 20 20 20 20 7a 45 72 72 20 3d 20 22 65 72  {.    zErr = "er
3920f 72 6f 72 20 69 6e 20 78 4f 70 65 6e 28 29 22 3b  ror in xOpen()";
39210 0a 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68  .    goto finish
39211 3b 0a 20 20 7d 0a 20 20 70 43 73 72 2d 3e 70 54  ;.  }.  pCsr->pT
39212 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65  okenizer = pToke
39213 6e 69 7a 65 72 3b 0a 0a 20 20 77 68 69 6c 65 28  nizer;..  while(
39214 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 70 2d 3e 78   SQLITE_OK==p->x
39215 4e 65 78 74 28 70 43 73 72 2c 20 26 7a 54 6f 6b  Next(pCsr, &zTok
39216 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c 20 26 69 53  en, &nToken, &iS
39217 74 61 72 74 2c 20 26 69 45 6e 64 2c 20 26 69 50  tart, &iEnd, &iP
39218 6f 73 29 20 29 7b 0a 20 20 20 20 54 63 6c 5f 4c  os) ){.    Tcl_L
39219 69 73 74 4f 62 6a 41 70 70 65 6e 64 45 6c 65 6d  istObjAppendElem
3921a 65 6e 74 28 30 2c 20 70 52 65 74 2c 20 54 63 6c  ent(0, pRet, Tcl
3921b 5f 4e 65 77 49 6e 74 4f 62 6a 28 69 50 6f 73 29  _NewIntObj(iPos)
3921c 29 3b 0a 20 20 20 20 54 63 6c 5f 4c 69 73 74 4f  );.    Tcl_ListO
3921d 62 6a 41 70 70 65 6e 64 45 6c 65 6d 65 6e 74 28  bjAppendElement(
3921e 30 2c 20 70 52 65 74 2c 20 54 63 6c 5f 4e 65 77  0, pRet, Tcl_New
3921f 53 74 72 69 6e 67 4f 62 6a 28 7a 54 6f 6b 65 6e  StringObj(zToken
39220 2c 20 6e 54 6f 6b 65 6e 29 29 3b 0a 20 20 20 20  , nToken));.    
39221 7a 54 6f 6b 65 6e 20 3d 20 26 7a 49 6e 70 75 74  zToken = &zInput
39222 5b 69 53 74 61 72 74 5d 3b 0a 20 20 20 20 6e 54  [iStart];.    nT
39223 6f 6b 65 6e 20 3d 20 69 45 6e 64 2d 69 53 74 61  oken = iEnd-iSta
39224 72 74 3b 0a 20 20 20 20 54 63 6c 5f 4c 69 73 74  rt;.    Tcl_List
39225 4f 62 6a 41 70 70 65 6e 64 45 6c 65 6d 65 6e 74  ObjAppendElement
39226 28 30 2c 20 70 52 65 74 2c 20 54 63 6c 5f 4e 65  (0, pRet, Tcl_Ne
39227 77 53 74 72 69 6e 67 4f 62 6a 28 7a 54 6f 6b 65  wStringObj(zToke
39228 6e 2c 20 6e 54 6f 6b 65 6e 29 29 3b 0a 20 20 7d  n, nToken));.  }
39229 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ..  if( SQLITE_O
3922a 4b 21 3d 70 2d 3e 78 43 6c 6f 73 65 28 70 43 73  K!=p->xClose(pCs
3922b 72 29 20 29 7b 0a 20 20 20 20 7a 45 72 72 20 3d  r) ){.    zErr =
3922c 20 22 65 72 72 6f 72 20 69 6e 20 78 43 6c 6f 73   "error in xClos
3922d 65 28 29 22 3b 0a 20 20 20 20 67 6f 74 6f 20 66  e()";.    goto f
3922e 69 6e 69 73 68 3b 0a 20 20 7d 0a 20 20 69 66 28  inish;.  }.  if(
3922f 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78   SQLITE_OK!=p->x
39230 44 65 73 74 72 6f 79 28 70 54 6f 6b 65 6e 69 7a  Destroy(pTokeniz
39231 65 72 29 20 29 7b 0a 20 20 20 20 7a 45 72 72 20  er) ){.    zErr 
39232 3d 20 22 65 72 72 6f 72 20 69 6e 20 78 44 65 73  = "error in xDes
39233 74 72 6f 79 28 29 22 3b 0a 20 20 20 20 67 6f 74  troy()";.    got
39234 6f 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 0a 66  o finish;.  }..f
39235 69 6e 69 73 68 3a 0a 20 20 69 66 28 20 7a 45 72  inish:.  if( zEr
39236 72 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  r ){.    sqlite3
39237 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
39238 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29  ntext, zErr, -1)
39239 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
3923a 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
3923b 78 74 28 63 6f 6e 74 65 78 74 2c 20 54 63 6c 5f  xt(context, Tcl_
3923c 47 65 74 53 74 72 69 6e 67 28 70 52 65 74 29 2c  GetString(pRet),
3923d 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e   -1, SQLITE_TRAN
3923e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 20 20 54 63  SIENT);.  }.  Tc
3923f 6c 5f 44 65 63 72 52 65 66 43 6f 75 6e 74 28 70  l_DecrRefCount(p
39240 52 65 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 0a  Ret);.}..static.
39241 69 6e 74 20 72 65 67 69 73 74 65 72 54 6f 6b 65  int registerToke
39242 6e 69 7a 65 72 28 0a 20 20 73 71 6c 69 74 65 33  nizer(.  sqlite3
39243 20 2a 64 62 2c 20 0a 20 20 63 68 61 72 20 2a 7a   *db, .  char *z
39244 4e 61 6d 65 2c 20 0a 20 20 63 6f 6e 73 74 20 73  Name, .  const s
39245 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
39246 5f 6d 6f 64 75 6c 65 20 2a 70 0a 29 7b 0a 20 20  _module *p.){.  
39247 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
39248 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20  3_stmt *pStmt;. 
39249 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 53 71 6c   const char zSql
3924a 5b 5d 20 3d 20 22 53 45 4c 45 43 54 20 66 74 73  [] = "SELECT fts
3924b 33 5f 74 6f 6b 65 6e 69 7a 65 72 28 3f 2c 20 3f  3_tokenizer(?, ?
3924c 29 22 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  )";..  rc = sqli
3924d 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64  te3_prepare_v2(d
3924e 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53  b, zSql, -1, &pS
3924f 74 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 72  tmt, 0);.  if( r
39250 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
39251 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
39252 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 62 69   }..  sqlite3_bi
39253 6e 64 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 31  nd_text(pStmt, 1
39254 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 4c  , zName, -1, SQL
39255 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 73  ITE_STATIC);.  s
39256 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
39257 28 70 53 74 6d 74 2c 20 32 2c 20 26 70 2c 20 73  (pStmt, 2, &p, s
39258 69 7a 65 6f 66 28 70 29 2c 20 53 51 4c 49 54 45  izeof(p), SQLITE
39259 5f 53 54 41 54 49 43 29 3b 0a 20 20 73 71 6c 69  _STATIC);.  sqli
3925a 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 3b  te3_step(pStmt);
3925b 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  ..  return sqlit
3925c 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d  e3_finalize(pStm
3925d 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 0a 69 6e  t);.}..static.in
3925e 74 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72  t queryTokenizer
3925f 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
39260 20 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c   .  char *zName,
39261 20 20 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74    .  const sqlit
39262 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
39263 75 6c 65 20 2a 2a 70 70 0a 29 7b 0a 20 20 69 6e  ule **pp.){.  in
39264 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f  t rc;.  sqlite3_
39265 73 74 6d 74 20 2a 70 53 74 6d 74 3b 0a 20 20 63  stmt *pStmt;.  c
39266 6f 6e 73 74 20 63 68 61 72 20 7a 53 71 6c 5b 5d  onst char zSql[]
39267 20 3d 20 22 53 45 4c 45 43 54 20 66 74 73 33 5f   = "SELECT fts3_
39268 74 6f 6b 65 6e 69 7a 65 72 28 3f 29 22 3b 0a 0a  tokenizer(?)";..
39269 20 20 2a 70 70 20 3d 20 30 3b 0a 20 20 72 63 20    *pp = 0;.  rc 
3926a 3d 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  = sqlite3_prepar
3926b 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d  e_v2(db, zSql, -
3926c 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a 20  1, &pStmt, 0);. 
3926d 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3926e 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
3926f 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69   rc;.  }..  sqli
39270 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 70 53  te3_bind_text(pS
39271 74 6d 74 2c 20 31 2c 20 7a 4e 61 6d 65 2c 20 2d  tmt, 1, zName, -
39272 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  1, SQLITE_STATIC
39273 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  );.  if( SQLITE_
39274 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65  ROW==sqlite3_ste
39275 70 28 70 53 74 6d 74 29 20 29 7b 0a 20 20 20 20  p(pStmt) ){.    
39276 69 66 28 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  if( sqlite3_colu
39277 6d 6e 5f 74 79 70 65 28 70 53 74 6d 74 2c 20 30  mn_type(pStmt, 0
39278 29 3d 3d 53 51 4c 49 54 45 5f 42 4c 4f 42 20 29  )==SQLITE_BLOB )
39279 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70  {.      memcpy(p
3927a 70 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  p, sqlite3_colum
3927b 6e 5f 62 6c 6f 62 28 70 53 74 6d 74 2c 20 30 29  n_blob(pStmt, 0)
3927c 2c 20 73 69 7a 65 6f 66 28 2a 70 70 29 29 3b 0a  , sizeof(*pp));.
3927d 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74      }.  }..  ret
3927e 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  urn sqlite3_fina
3927f 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a  lize(pStmt);.}..
39280 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
39281 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73 33 53  oid sqlite3Fts3S
39282 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f  impleTokenizerMo
39283 64 75 6c 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b  dule(sqlite3_tok
39284 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f  enizer_module co
39285 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a  nst**ppModule);.
39286 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
39287 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 63 61  ation of the sca
39288 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 66 74 73  lar function fts
39289 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e 74 65  3_tokenizer_inte
3928a 72 6e 61 6c 5f 74 65 73 74 28 29 2e 0a 2a 2a 20  rnal_test()..** 
3928b 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
3928c 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
3928d 67 20 6f 6e 6c 79 2c 20 69 74 20 69 73 20 6e 6f  g only, it is no
3928e 74 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68  t included in th
3928f 65 0a 2a 2a 20 62 75 69 6c 64 20 75 6e 6c 65 73  e.** build unles
39290 73 20 53 51 4c 49 54 45 5f 54 45 53 54 20 69 73  s SQLITE_TEST is
39291 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20   defined..**.** 
39292 54 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 74  The purpose of t
39293 68 69 73 20 69 73 20 74 6f 20 74 65 73 74 20 74  his is to test t
39294 68 61 74 20 74 68 65 20 66 74 73 33 5f 74 6f 6b  hat the fts3_tok
39295 65 6e 69 7a 65 72 28 29 20 66 75 6e 63 74 69 6f  enizer() functio
39296 6e 0a 2a 2a 20 63 61 6e 20 62 65 20 75 73 65 64  n.** can be used
39297 20 61 73 20 64 65 73 69 67 6e 65 64 20 62 79 20   as designed by 
39298 74 68 65 20 43 2d 63 6f 64 65 20 69 6e 20 74 68  the C-code in th
39299 65 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a 65 72  e queryTokenizer
3929a 20 61 6e 64 0a 2a 2a 20 72 65 67 69 73 74 65 72   and.** register
3929b 54 6f 6b 65 6e 69 7a 65 72 28 29 20 66 75 6e 63  Tokenizer() func
3929c 74 69 6f 6e 73 20 61 62 6f 76 65 2e 20 54 68 65  tions above. The
3929d 73 65 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73  se two functions
3929e 20 61 72 65 20 72 65 70 65 61 74 65 64 0a 2a 2a   are repeated.**
3929f 20 69 6e 20 74 68 65 20 52 45 41 44 4d 45 2e 74   in the README.t
392a0 6f 6b 65 6e 69 7a 65 72 20 66 69 6c 65 20 61 73  okenizer file as
392a1 20 61 6e 20 65 78 61 6d 70 6c 65 2c 20 73 6f 20   an example, so 
392a2 69 74 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20  it is important 
392a3 74 6f 0a 2a 2a 20 74 65 73 74 20 74 68 65 6d 2e  to.** test them.
392a4 0a 2a 2a 0a 2a 2a 20 54 6f 20 72 75 6e 20 74 68  .**.** To run th
392a5 65 20 74 65 73 74 73 2c 20 65 76 61 6c 75 61 74  e tests, evaluat
392a6 65 20 74 68 65 20 66 74 73 33 5f 74 6f 6b 65 6e  e the fts3_token
392a7 69 7a 65 72 5f 69 6e 74 65 72 6e 61 6c 5f 74 65  izer_internal_te
392a8 73 74 28 29 20 73 63 61 6c 61 72 0a 2a 2a 20 66  st() scalar.** f
392a9 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 6e 6f 20  unction with no 
392aa 61 72 67 75 6d 65 6e 74 73 2e 20 41 6e 20 61 73  arguments. An as
392ab 73 65 72 74 28 29 20 77 69 6c 6c 20 66 61 69 6c  sert() will fail
392ac 20 69 66 20 61 20 70 72 6f 62 6c 65 6d 20 69 73   if a problem is
392ad 0a 2a 2a 20 64 65 74 65 63 74 65 64 2e 20 69 2e  .** detected. i.
392ae 65 2e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45  e.:.**.**     SE
392af 4c 45 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e 69  LECT fts3_tokeni
392b0 7a 65 72 5f 69 6e 74 65 72 6e 61 6c 5f 74 65 73  zer_internal_tes
392b1 74 28 29 3b 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69  t();.**.*/.stati
392b2 63 20 76 6f 69 64 20 69 6e 74 54 65 73 74 46 75  c void intTestFu
392b3 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
392b4 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
392b5 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
392b6 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
392b7 67 76 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  gv.){.  int rc;.
392b8 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
392b9 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
392ba 20 2a 70 31 3b 0a 20 20 63 6f 6e 73 74 20 73 71   *p1;.  const sq
392bb 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
392bc 6d 6f 64 75 6c 65 20 2a 70 32 3b 0a 20 20 73 71  module *p2;.  sq
392bd 6c 69 74 65 33 20 2a 64 62 20 3d 20 28 73 71 6c  lite3 *db = (sql
392be 69 74 65 33 20 2a 29 73 71 6c 69 74 65 33 5f 75  ite3 *)sqlite3_u
392bf 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74  ser_data(context
392c0 29 3b 0a 0a 20 20 2f 2a 20 54 65 73 74 20 74 68  );..  /* Test th
392c1 65 20 71 75 65 72 79 20 66 75 6e 63 74 69 6f 6e  e query function
392c2 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 46 74 73   */.  sqlite3Fts
392c3 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72  3SimpleTokenizer
392c4 4d 6f 64 75 6c 65 28 26 70 31 29 3b 0a 20 20 72  Module(&p1);.  r
392c5 63 20 3d 20 71 75 65 72 79 54 6f 6b 65 6e 69 7a  c = queryTokeniz
392c6 65 72 28 64 62 2c 20 22 73 69 6d 70 6c 65 22 2c  er(db, "simple",
392c7 20 26 70 32 29 3b 0a 20 20 61 73 73 65 72 74 28   &p2);.  assert(
392c8 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
392c9 3b 0a 20 20 61 73 73 65 72 74 28 20 70 31 3d 3d  ;.  assert( p1==
392ca 70 32 20 29 3b 0a 20 20 72 63 20 3d 20 71 75 65  p2 );.  rc = que
392cb 72 79 54 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20  ryTokenizer(db, 
392cc 22 6e 6f 73 75 63 68 74 6f 6b 65 6e 69 7a 65 72  "nosuchtokenizer
392cd 22 2c 20 26 70 32 29 3b 0a 20 20 61 73 73 65 72  ", &p2);.  asser
392ce 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 52  t( rc==SQLITE_ER
392cf 52 4f 52 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ROR );.  assert(
392d0 20 70 32 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65   p2==0 );.  asse
392d1 72 74 28 20 30 3d 3d 73 74 72 63 6d 70 28 73 71  rt( 0==strcmp(sq
392d2 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29  lite3_errmsg(db)
392d3 2c 20 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e  , "unknown token
392d4 69 7a 65 72 3a 20 6e 6f 73 75 63 68 74 6f 6b 65  izer: nosuchtoke
392d5 6e 69 7a 65 72 22 29 20 29 3b 0a 0a 20 20 2f 2a  nizer") );..  /*
392d6 20 54 65 73 74 20 74 68 65 20 73 74 6f 72 61 67   Test the storag
392d7 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  e function */.  
392d8 72 63 20 3d 20 72 65 67 69 73 74 65 72 54 6f 6b  rc = registerTok
392d9 65 6e 69 7a 65 72 28 64 62 2c 20 22 6e 6f 73 75  enizer(db, "nosu
392da 63 68 74 6f 6b 65 6e 69 7a 65 72 22 2c 20 70 31  chtokenizer", p1
392db 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d  );.  assert( rc=
392dc 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20  =SQLITE_OK );.  
392dd 72 63 20 3d 20 71 75 65 72 79 54 6f 6b 65 6e 69  rc = queryTokeni
392de 7a 65 72 28 64 62 2c 20 22 6e 6f 73 75 63 68 74  zer(db, "nosucht
392df 6f 6b 65 6e 69 7a 65 72 22 2c 20 26 70 32 29 3b  okenizer", &p2);
392e0 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
392e1 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 61 73  QLITE_OK );.  as
392e2 73 65 72 74 28 20 70 32 3d 3d 70 31 20 29 3b 0a  sert( p2==p1 );.
392e3 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
392e4 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
392e5 22 6f 6b 22 2c 20 2d 31 2c 20 53 51 4c 49 54 45  "ok", -1, SQLITE
392e6 5f 53 54 41 54 49 43 29 3b 0a 7d 0a 0a 23 65 6e  _STATIC);.}..#en
392e7 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 75  dif../*.** Set u
392e8 70 20 53 51 4c 20 6f 62 6a 65 63 74 73 20 69 6e  p SQL objects in
392e9 20 64 61 74 61 62 61 73 65 20 64 62 20 75 73 65   database db use
392ea 64 20 74 6f 20 61 63 63 65 73 73 20 74 68 65 20  d to access the 
392eb 63 6f 6e 74 65 6e 74 73 20 6f 66 0a 2a 2a 20 74  contents of.** t
392ec 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 6f  he hash table po
392ed 69 6e 74 65 64 20 74 6f 20 62 79 20 61 72 67 75  inted to by argu
392ee 6d 65 6e 74 20 70 48 61 73 68 2e 20 54 68 65 20  ment pHash. The 
392ef 68 61 73 68 20 74 61 62 6c 65 20 6d 75 73 74 0a  hash table must.
392f0 2a 2a 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69  ** been initiali
392f1 73 65 64 20 74 6f 20 75 73 65 20 73 74 72 69 6e  sed to use strin
392f2 67 20 6b 65 79 73 2c 20 61 6e 64 20 74 6f 20 74  g keys, and to t
392f3 61 6b 65 20 61 20 70 72 69 76 61 74 65 20 63 6f  ake a private co
392f4 70 79 20 0a 2a 2a 20 6f 66 20 74 68 65 20 6b 65  py .** of the ke
392f5 79 20 77 68 65 6e 20 61 20 76 61 6c 75 65 20 69  y when a value i
392f6 73 20 69 6e 73 65 72 74 65 64 2e 20 69 2e 65 2e  s inserted. i.e.
392f7 20 62 79 20 61 20 63 61 6c 6c 20 73 69 6d 69 6c   by a call simil
392f8 61 72 20 74 6f 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ar to:.**.**    
392f9 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49  sqlite3Fts3HashI
392fa 6e 69 74 28 70 48 61 73 68 2c 20 46 54 53 33 5f  nit(pHash, FTS3_
392fb 48 41 53 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b  HASH_STRING, 1);
392fc 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  .**.** This func
392fd 74 69 6f 6e 20 61 64 64 73 20 61 20 73 63 61 6c  tion adds a scal
392fe 61 72 20 66 75 6e 63 74 69 6f 6e 20 28 73 65 65  ar function (see
392ff 20 68 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 20   header comment 
39300 61 62 6f 76 65 0a 2a 2a 20 73 63 61 6c 61 72 46  above.** scalarF
39301 75 6e 63 28 29 20 69 6e 20 74 68 69 73 20 66 69  unc() in this fi
39302 6c 65 20 66 6f 72 20 64 65 74 61 69 6c 73 29 20  le for details) 
39303 61 6e 64 2c 20 69 66 20 45 4e 41 42 4c 45 5f 54  and, if ENABLE_T
39304 41 42 4c 45 20 69 73 0a 2a 2a 20 64 65 66 69 6e  ABLE is.** defin
39305 65 64 20 61 74 20 63 6f 6d 70 69 6c 61 74 69 6f  ed at compilatio
39306 6e 20 74 69 6d 65 2c 20 61 20 74 65 6d 70 6f 72  n time, a tempor
39307 61 72 79 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ary virtual tabl
39308 65 20 28 73 65 65 20 68 65 61 64 65 72 20 0a 2a  e (see header .*
39309 2a 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 20  * comment above 
3930a 73 74 72 75 63 74 20 48 61 73 68 54 61 62 6c 65  struct HashTable
3930b 56 74 61 62 29 20 74 6f 20 74 68 65 20 64 61 74  Vtab) to the dat
3930c 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 42 6f  abase schema. Bo
3930d 74 68 20 0a 2a 2a 20 70 72 6f 76 69 64 65 20 72  th .** provide r
3930e 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73 73  ead/write access
3930f 20 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 73   to the contents
39310 20 6f 66 20 2a 70 48 61 73 68 2e 0a 2a 2a 0a 2a   of *pHash..**.*
39311 2a 20 54 68 65 20 74 68 69 72 64 20 61 72 67 75  * The third argu
39312 6d 65 6e 74 20 74 6f 20 74 68 69 73 20 66 75 6e  ment to this fun
39313 63 74 69 6f 6e 2c 20 7a 4e 61 6d 65 2c 20 69 73  ction, zName, is
39314 20 75 73 65 64 20 61 73 20 74 68 65 20 6e 61 6d   used as the nam
39315 65 0a 2a 2a 20 6f 66 20 62 6f 74 68 20 74 68 65  e.** of both the
39316 20 73 63 61 6c 61 72 20 61 6e 64 2c 20 69 66 20   scalar and, if 
39317 63 72 65 61 74 65 64 2c 20 74 68 65 20 76 69 72  created, the vir
39318 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53  tual table..*/.S
39319 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
3931a 74 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  t sqlite3Fts3Ini
3931b 74 48 61 73 68 54 61 62 6c 65 28 0a 20 20 73 71  tHashTable(.  sq
3931c 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 66 74  lite3 *db, .  ft
3931d 73 33 48 61 73 68 20 2a 70 48 61 73 68 2c 20 0a  s3Hash *pHash, .
3931e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
3931f 61 6d 65 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20  ame.){.  int rc 
39320 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 76  = SQLITE_OK;.  v
39321 6f 69 64 20 2a 70 20 3d 20 28 76 6f 69 64 20 2a  oid *p = (void *
39322 29 70 48 61 73 68 3b 0a 20 20 63 6f 6e 73 74 20  )pHash;.  const 
39323 69 6e 74 20 61 6e 79 20 3d 20 53 51 4c 49 54 45  int any = SQLITE
39324 5f 41 4e 59 3b 0a 20 20 63 68 61 72 20 2a 7a 54  _ANY;.  char *zT
39325 65 73 74 20 3d 20 30 3b 0a 20 20 63 68 61 72 20  est = 0;.  char 
39326 2a 7a 54 65 73 74 32 20 3d 20 30 3b 0a 0a 23 69  *zTest2 = 0;..#i
39327 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
39328 0a 20 20 76 6f 69 64 20 2a 70 64 62 20 3d 20 28  .  void *pdb = (
39329 76 6f 69 64 20 2a 29 64 62 3b 0a 20 20 7a 54 65  void *)db;.  zTe
3932a 73 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  st = sqlite3_mpr
3932b 69 6e 74 66 28 22 25 73 5f 74 65 73 74 22 2c 20  intf("%s_test", 
3932c 7a 4e 61 6d 65 29 3b 0a 20 20 7a 54 65 73 74 32  zName);.  zTest2
3932d 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
3932e 74 66 28 22 25 73 5f 69 6e 74 65 72 6e 61 6c 5f  tf("%s_internal_
3932f 74 65 73 74 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20  test", zName);. 
39330 20 69 66 28 20 21 7a 54 65 73 74 20 7c 7c 20 21   if( !zTest || !
39331 7a 54 65 73 74 32 20 29 7b 0a 20 20 20 20 72 63  zTest2 ){.    rc
39332 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
39333 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69  .  }.#endif..  i
39334 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
39335 0a 20 20 20 7c 7c 20 28 72 63 20 3d 20 73 71 6c  .   || (rc = sql
39336 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
39337 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  tion(db, zName, 
39338 31 2c 20 61 6e 79 2c 20 70 2c 20 73 63 61 6c 61  1, any, p, scala
39339 72 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a 20 20  rFunc, 0, 0)).  
3933a 20 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69 74 65   || (rc = sqlite
3933b 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
3933c 6e 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 32 2c 20  n(db, zName, 2, 
3933d 61 6e 79 2c 20 70 2c 20 73 63 61 6c 61 72 46 75  any, p, scalarFu
3933e 6e 63 2c 20 30 2c 20 30 29 29 0a 23 69 66 64 65  nc, 0, 0)).#ifde
3933f 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
39340 20 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69 74 65   || (rc = sqlite
39341 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
39342 6e 28 64 62 2c 20 7a 54 65 73 74 2c 20 32 2c 20  n(db, zTest, 2, 
39343 61 6e 79 2c 20 70 2c 20 74 65 73 74 46 75 6e 63  any, p, testFunc
39344 2c 20 30 2c 20 30 29 29 0a 20 20 20 7c 7c 20 28  , 0, 0)).   || (
39345 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65  rc = sqlite3_cre
39346 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c  ate_function(db,
39347 20 7a 54 65 73 74 2c 20 33 2c 20 61 6e 79 2c 20   zTest, 3, any, 
39348 70 2c 20 74 65 73 74 46 75 6e 63 2c 20 30 2c 20  p, testFunc, 0, 
39349 30 29 29 0a 20 20 20 7c 7c 20 28 72 63 20 3d 20  0)).   || (rc = 
3934a 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
3934b 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 54 65 73  unction(db, zTes
3934c 74 32 2c 20 30 2c 20 61 6e 79 2c 20 70 64 62 2c  t2, 0, any, pdb,
3934d 20 69 6e 74 54 65 73 74 46 75 6e 63 2c 20 30 2c   intTestFunc, 0,
3934e 20 30 29 29 0a 23 65 6e 64 69 66 0a 20 20 29 3b   0)).#endif.  );
3934f 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
39350 28 7a 54 65 73 74 29 3b 0a 20 20 73 71 6c 69 74  (zTest);.  sqlit
39351 65 33 5f 66 72 65 65 28 7a 54 65 73 74 32 29 3b  e3_free(zTest2);
39352 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
39353 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
39354 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29  ned(SQLITE_CORE)
39355 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
39356 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20  TE_ENABLE_FTS3) 
39357 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
39358 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f  *** End of fts3_
39359 74 6f 6b 65 6e 69 7a 65 72 2e 63 20 2a 2a 2a 2a  tokenizer.c ****
3935a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3935b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3935c 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
3935d 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66  *** Begin file f
3935e 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 31 2e 63  ts3_tokenizer1.c
3935f 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
39360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39361 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4f  **/./*.** 2006 O
39362 63 74 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ct 10.**.** The 
39363 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
39364 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
39365 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
39366 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
39367 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
39368 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
39369 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
3936a 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
3936b 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
3936c 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
3936d 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
3936e 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
3936f 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
39370 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
39371 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
39372 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
39373 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
39374 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39375 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39376 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39377 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39378 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
39379 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
3937a 66 20 74 68 65 20 22 73 69 6d 70 6c 65 22 20 66  f the "simple" f
3937b 75 6c 6c 2d 74 65 78 74 2d 73 65 61 72 63 68 20  ull-text-search 
3937c 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2f 0a 0a 2f  tokenizer..*/../
3937d 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e  *.** The code in
3937e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e   this file is on
3937f 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a  ly compiled if:.
39380 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20  **.**     * The 
39381 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62  FTS3 module is b
39382 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61 6e  eing built as an
39383 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20   extension.**   
39384 20 20 20 20 28 69 6e 20 77 68 69 63 68 20 63 61      (in which ca
39385 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 69  se SQLITE_CORE i
39386 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20  s not defined), 
39387 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54  or.**.**     * T
39388 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69  he FTS3 module i
39389 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69 6e  s being built in
3938a 74 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a  to the core of.*
3938b 2a 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 28  *       SQLite (
3938c 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53 51  in which case SQ
3938d 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
3938e 20 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f   is defined)..*/
3938f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
39390 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65  LITE_CORE) || de
39391 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
39392 42 4c 45 5f 46 54 53 33 29 0a 0a 0a 0a 0a 74 79  BLE_FTS3).....ty
39393 70 65 64 65 66 20 73 74 72 75 63 74 20 73 69 6d  pedef struct sim
39394 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 7b 0a  ple_tokenizer {.
39395 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
39396 7a 65 72 20 62 61 73 65 3b 0a 20 20 63 68 61 72  zer base;.  char
39397 20 64 65 6c 69 6d 5b 31 32 38 5d 3b 20 20 20 20   delim[128];    
39398 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67           /* flag
39399 20 41 53 43 49 49 20 64 65 6c 69 6d 69 74 65 72   ASCII delimiter
3939a 73 20 2a 2f 0a 7d 20 73 69 6d 70 6c 65 5f 74 6f  s */.} simple_to
3939b 6b 65 6e 69 7a 65 72 3b 0a 0a 74 79 70 65 64 65  kenizer;..typede
3939c 66 20 73 74 72 75 63 74 20 73 69 6d 70 6c 65 5f  f struct simple_
3939d 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
3939e 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b   {.  sqlite3_tok
3939f 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 62 61  enizer_cursor ba
393a0 73 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  se;.  const char
393a1 20 2a 70 49 6e 70 75 74 3b 20 20 20 20 20 20 20   *pInput;       
393a2 20 20 20 2f 2a 20 69 6e 70 75 74 20 77 65 20 61     /* input we a
393a3 72 65 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 2a 2f  re tokenizing */
393a4 0a 20 20 69 6e 74 20 6e 42 79 74 65 73 3b 20 20  .  int nBytes;  
393a5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
393a6 2f 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 69  /* size of the i
393a7 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 69 4f  nput */.  int iO
393a8 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20  ffset;          
393a9 20 20 20 20 20 20 20 2f 2a 20 63 75 72 72 65 6e         /* curren
393aa 74 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 70 49  t position in pI
393ab 6e 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20 69 54  nput */.  int iT
393ac 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20  oken;           
393ad 20 20 20 20 20 20 20 2f 2a 20 69 6e 64 65 78 20         /* index 
393ae 6f 66 20 6e 65 78 74 20 74 6f 6b 65 6e 20 74 6f  of next token to
393af 20 62 65 20 72 65 74 75 72 6e 65 64 20 2a 2f 0a   be returned */.
393b0 20 20 63 68 61 72 20 2a 70 54 6f 6b 65 6e 3b 20    char *pToken; 
393b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
393b2 2a 20 73 74 6f 72 61 67 65 20 66 6f 72 20 63 75  * storage for cu
393b3 72 72 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20  rrent token */. 
393b4 20 69 6e 74 20 6e 54 6f 6b 65 6e 41 6c 6c 6f 63   int nTokenAlloc
393b5 61 74 65 64 3b 20 20 20 20 20 20 20 20 20 2f 2a  ated;         /*
393b6 20 73 70 61 63 65 20 61 6c 6c 6f 63 61 74 65 64   space allocated
393b7 20 74 6f 20 7a 54 6f 6b 65 6e 20 62 75 66 66 65   to zToken buffe
393b8 72 20 2a 2f 0a 7d 20 73 69 6d 70 6c 65 5f 74 6f  r */.} simple_to
393b9 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 3b 0a  kenizer_cursor;.
393ba 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63  ../* Forward dec
393bb 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74 61 74  laration */.stat
393bc 69 63 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  ic const sqlite3
393bd 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
393be 65 20 73 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65  e simpleTokenize
393bf 72 4d 6f 64 75 6c 65 3b 0a 0a 73 74 61 74 69 63  rModule;..static
393c0 20 69 6e 74 20 73 69 6d 70 6c 65 44 65 6c 69 6d   int simpleDelim
393c1 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65  (simple_tokenize
393c2 72 20 2a 74 2c 20 75 6e 73 69 67 6e 65 64 20 63  r *t, unsigned c
393c3 68 61 72 20 63 29 7b 0a 20 20 72 65 74 75 72 6e  har c){.  return
393c4 20 63 3c 30 78 38 30 20 26 26 20 74 2d 3e 64 65   c<0x80 && t->de
393c5 6c 69 6d 5b 63 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  lim[c];.}../*.**
393c6 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 74 6f   Create a new to
393c7 6b 65 6e 69 7a 65 72 20 69 6e 73 74 61 6e 63 65  kenizer instance
393c8 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
393c9 73 69 6d 70 6c 65 43 72 65 61 74 65 28 0a 20 20  simpleCreate(.  
393ca 69 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20  int argc, const 
393cb 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 2a 61 72  char * const *ar
393cc 67 76 2c 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  gv,.  sqlite3_to
393cd 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54 6f 6b 65  kenizer **ppToke
393ce 6e 69 7a 65 72 0a 29 7b 0a 20 20 73 69 6d 70 6c  nizer.){.  simpl
393cf 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 74 3b 0a  e_tokenizer *t;.
393d0 0a 20 20 74 20 3d 20 28 73 69 6d 70 6c 65 5f 74  .  t = (simple_t
393d1 6f 6b 65 6e 69 7a 65 72 20 2a 29 20 73 71 6c 69  okenizer *) sqli
393d2 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f  te3_malloc(sizeo
393d3 66 28 2a 74 29 29 3b 0a 20 20 69 66 28 20 74 3d  f(*t));.  if( t=
393d4 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20 53  =NULL ) return S
393d5 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 6d  QLITE_NOMEM;.  m
393d6 65 6d 73 65 74 28 74 2c 20 30 2c 20 73 69 7a 65  emset(t, 0, size
393d7 6f 66 28 2a 74 29 29 3b 0a 0a 20 20 2f 2a 20 54  of(*t));..  /* T
393d8 4f 44 4f 28 73 68 65 73 73 29 20 44 65 6c 69 6d  ODO(shess) Delim
393d9 69 74 65 72 73 20 6e 65 65 64 20 74 6f 20 72 65  iters need to re
393da 6d 61 69 6e 20 74 68 65 20 73 61 6d 65 20 66 72  main the same fr
393db 6f 6d 20 72 75 6e 20 74 6f 20 72 75 6e 2c 0a 20  om run to run,. 
393dc 20 2a 2a 20 65 6c 73 65 20 77 65 20 6e 65 65 64   ** else we need
393dd 20 74 6f 20 72 65 69 6e 64 65 78 2e 20 20 4f 6e   to reindex.  On
393de 65 20 73 6f 6c 75 74 69 6f 6e 20 77 6f 75 6c 64  e solution would
393df 20 62 65 20 61 20 6d 65 74 61 2d 74 61 62 6c 65   be a meta-table
393e0 20 74 6f 0a 20 20 2a 2a 20 74 72 61 63 6b 20 73   to.  ** track s
393e1 75 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  uch information 
393e2 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2c  in the database,
393e3 20 74 68 65 6e 20 77 65 27 64 20 6f 6e 6c 79 20   then we'd only 
393e4 77 61 6e 74 20 74 68 69 73 0a 20 20 2a 2a 20 69  want this.  ** i
393e5 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 74 68  nformation on th
393e6 65 20 69 6e 69 74 69 61 6c 20 63 72 65 61 74 65  e initial create
393e7 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61 72 67  ..  */.  if( arg
393e8 63 3e 31 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  c>1 ){.    int i
393e9 2c 20 6e 20 3d 20 73 74 72 6c 65 6e 28 61 72 67  , n = strlen(arg
393ea 76 5b 31 5d 29 3b 0a 20 20 20 20 66 6f 72 28 69  v[1]);.    for(i
393eb 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20  =0; i<n; i++){. 
393ec 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68       unsigned ch
393ed 61 72 20 63 68 20 3d 20 61 72 67 76 5b 31 5d 5b  ar ch = argv[1][
393ee 69 5d 3b 0a 20 20 20 20 20 20 2f 2a 20 57 65 20  i];.      /* We 
393ef 65 78 70 6c 69 63 69 74 6c 79 20 64 6f 6e 27 74  explicitly don't
393f0 20 73 75 70 70 6f 72 74 20 55 54 46 2d 38 20 64   support UTF-8 d
393f1 65 6c 69 6d 69 74 65 72 73 20 66 6f 72 20 6e 6f  elimiters for no
393f2 77 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  w. */.      if( 
393f3 63 68 3e 3d 30 78 38 30 20 29 7b 0a 20 20 20 20  ch>=0x80 ){.    
393f4 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
393f5 28 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  (t);.        ret
393f6 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
393f7 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
393f8 74 2d 3e 64 65 6c 69 6d 5b 63 68 5d 20 3d 20 31  t->delim[ch] = 1
393f9 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c 73 65  ;.    }.  } else
393fa 20 7b 0a 20 20 20 20 2f 2a 20 4d 61 72 6b 20 6e   {.    /* Mark n
393fb 6f 6e 2d 61 6c 70 68 61 6e 75 6d 65 72 69 63 20  on-alphanumeric 
393fc 41 53 43 49 49 20 63 68 61 72 61 63 74 65 72 73  ASCII characters
393fd 20 61 73 20 64 65 6c 69 6d 69 74 65 72 73 20 2a   as delimiters *
393fe 2f 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  /.    int i;.   
393ff 20 66 6f 72 28 69 3d 31 3b 20 69 3c 30 78 38 30   for(i=1; i<0x80
39400 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 74 2d  ; i++){.      t-
39401 3e 64 65 6c 69 6d 5b 69 5d 20 3d 20 21 69 73 61  >delim[i] = !isa
39402 6c 6e 75 6d 28 69 29 3b 0a 20 20 20 20 7d 0a 20  lnum(i);.    }. 
39403 20 7d 0a 0a 20 20 2a 70 70 54 6f 6b 65 6e 69 7a   }..  *ppTokeniz
39404 65 72 20 3d 20 26 74 2d 3e 62 61 73 65 3b 0a 20  er = &t->base;. 
39405 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
39406 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 74  K;.}../*.** Dest
39407 72 6f 79 20 61 20 74 6f 6b 65 6e 69 7a 65 72 0a  roy a tokenizer.
39408 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69  */.static int si
39409 6d 70 6c 65 44 65 73 74 72 6f 79 28 73 71 6c 69  mpleDestroy(sqli
3940a 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
3940b 54 6f 6b 65 6e 69 7a 65 72 29 7b 0a 20 20 73 71  Tokenizer){.  sq
3940c 6c 69 74 65 33 5f 66 72 65 65 28 70 54 6f 6b 65  lite3_free(pToke
3940d 6e 69 7a 65 72 29 3b 0a 20 20 72 65 74 75 72 6e  nizer);.  return
3940e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
3940f 2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 74 6f 20  *.** Prepare to 
39410 62 65 67 69 6e 20 74 6f 6b 65 6e 69 7a 69 6e 67  begin tokenizing
39411 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 73 74   a particular st
39412 72 69 6e 67 2e 20 20 54 68 65 20 69 6e 70 75 74  ring.  The input
39413 0a 2a 2a 20 73 74 72 69 6e 67 20 74 6f 20 62 65  .** string to be
39414 20 74 6f 6b 65 6e 69 7a 65 64 20 69 73 20 70 49   tokenized is pI
39415 6e 70 75 74 5b 30 2e 2e 6e 42 79 74 65 73 2d 31  nput[0..nBytes-1
39416 5d 2e 20 20 41 20 63 75 72 73 6f 72 0a 2a 2a 20  ].  A cursor.** 
39417 75 73 65 64 20 74 6f 20 69 6e 63 72 65 6d 65 6e  used to incremen
39418 74 61 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65 20 74  tally tokenize t
39419 68 69 73 20 73 74 72 69 6e 67 20 69 73 20 72 65  his string is re
3941a 74 75 72 6e 65 64 20 69 6e 20 0a 2a 2a 20 2a 70  turned in .** *p
3941b 70 43 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74  pCursor..*/.stat
3941c 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 4f 70 65  ic int simpleOpe
3941d 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  n(.  sqlite3_tok
3941e 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a  enizer *pTokeniz
3941f 65 72 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  er,         /* T
39420 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a  he tokenizer */.
39421 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 49    const char *pI
39422 6e 70 75 74 2c 20 69 6e 74 20 6e 42 79 74 65 73  nput, int nBytes
39423 2c 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69  ,        /* Stri
39424 6e 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a  ng to be tokeniz
39425 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ed */.  sqlite3_
39426 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
39427 20 2a 2a 70 70 43 75 72 73 6f 72 20 20 20 20 2f   **ppCursor    /
39428 2a 20 4f 55 54 3a 20 54 6f 6b 65 6e 69 7a 61 74  * OUT: Tokenizat
39429 69 6f 6e 20 63 75 72 73 6f 72 20 2a 2f 0a 29 7b  ion cursor */.){
3942a 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69  .  simple_tokeni
3942b 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 3b 0a 0a  zer_cursor *c;..
3942c 20 20 63 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f    c = (simple_to
3942d 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
3942e 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  ) sqlite3_malloc
3942f 28 73 69 7a 65 6f 66 28 2a 63 29 29 3b 0a 20 20  (sizeof(*c));.  
39430 69 66 28 20 63 3d 3d 4e 55 4c 4c 20 29 20 72 65  if( c==NULL ) re
39431 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
39432 4d 3b 0a 0a 20 20 63 2d 3e 70 49 6e 70 75 74 20  M;..  c->pInput 
39433 3d 20 70 49 6e 70 75 74 3b 0a 20 20 69 66 28 20  = pInput;.  if( 
39434 70 49 6e 70 75 74 3d 3d 30 20 29 7b 0a 20 20 20  pInput==0 ){.   
39435 20 63 2d 3e 6e 42 79 74 65 73 20 3d 20 30 3b 0a   c->nBytes = 0;.
39436 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 42 79 74    }else if( nByt
39437 65 73 3c 30 20 29 7b 0a 20 20 20 20 63 2d 3e 6e  es<0 ){.    c->n
39438 42 79 74 65 73 20 3d 20 28 69 6e 74 29 73 74 72  Bytes = (int)str
39439 6c 65 6e 28 70 49 6e 70 75 74 29 3b 0a 20 20 7d  len(pInput);.  }
3943a 65 6c 73 65 7b 0a 20 20 20 20 63 2d 3e 6e 42 79  else{.    c->nBy
3943b 74 65 73 20 3d 20 6e 42 79 74 65 73 3b 0a 20 20  tes = nBytes;.  
3943c 7d 0a 20 20 63 2d 3e 69 4f 66 66 73 65 74 20 3d  }.  c->iOffset =
3943d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
3943e 20 20 20 20 2f 2a 20 73 74 61 72 74 20 74 6f 6b      /* start tok
3943f 65 6e 69 7a 69 6e 67 20 61 74 20 74 68 65 20 62  enizing at the b
39440 65 67 69 6e 6e 69 6e 67 20 2a 2f 0a 20 20 63 2d  eginning */.  c-
39441 3e 69 54 6f 6b 65 6e 20 3d 20 30 3b 0a 20 20 63  >iToken = 0;.  c
39442 2d 3e 70 54 6f 6b 65 6e 20 3d 20 4e 55 4c 4c 3b  ->pToken = NULL;
39443 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
39444 2a 20 6e 6f 20 73 70 61 63 65 20 61 6c 6c 6f 63  * no space alloc
39445 61 74 65 64 2c 20 79 65 74 2e 20 2a 2f 0a 20 20  ated, yet. */.  
39446 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61 74  c->nTokenAllocat
39447 65 64 20 3d 20 30 3b 0a 0a 20 20 2a 70 70 43 75  ed = 0;..  *ppCu
39448 72 73 6f 72 20 3d 20 26 63 2d 3e 62 61 73 65 3b  rsor = &c->base;
39449 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
3944a 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  _OK;.}../*.** Cl
3944b 6f 73 65 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69  ose a tokenizati
3944c 6f 6e 20 63 75 72 73 6f 72 20 70 72 65 76 69 6f  on cursor previo
3944d 75 73 6c 79 20 6f 70 65 6e 65 64 20 62 79 20 61  usly opened by a
3944e 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73 69 6d 70   call to.** simp
3944f 6c 65 4f 70 65 6e 28 29 20 61 62 6f 76 65 2e 0a  leOpen() above..
39450 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69  */.static int si
39451 6d 70 6c 65 43 6c 6f 73 65 28 73 71 6c 69 74 65  mpleClose(sqlite
39452 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
39453 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20  or *pCursor){.  
39454 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72  simple_tokenizer
39455 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 73 69  _cursor *c = (si
39456 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  mple_tokenizer_c
39457 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72  ursor *) pCursor
39458 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
39459 28 63 2d 3e 70 54 6f 6b 65 6e 29 3b 0a 20 20 73  (c->pToken);.  s
3945a 71 6c 69 74 65 33 5f 66 72 65 65 28 63 29 3b 0a  qlite3_free(c);.
3945b 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3945c 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74  OK;.}../*.** Ext
3945d 72 61 63 74 20 74 68 65 20 6e 65 78 74 20 74 6f  ract the next to
3945e 6b 65 6e 20 66 72 6f 6d 20 61 20 74 6f 6b 65 6e  ken from a token
3945f 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72 2e 20  ization cursor. 
39460 20 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73 74   The cursor must
39461 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 6f 70  .** have been op
39462 65 6e 65 64 20 62 79 20 61 20 70 72 69 6f 72 20  ened by a prior 
39463 63 61 6c 6c 20 74 6f 20 73 69 6d 70 6c 65 4f 70  call to simpleOp
39464 65 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  en()..*/.static 
39465 69 6e 74 20 73 69 6d 70 6c 65 4e 65 78 74 28 0a  int simpleNext(.
39466 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
39467 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  zer_cursor *pCur
39468 73 6f 72 2c 20 20 2f 2a 20 43 75 72 73 6f 72 20  sor,  /* Cursor 
39469 72 65 74 75 72 6e 65 64 20 62 79 20 73 69 6d 70  returned by simp
3946a 6c 65 4f 70 65 6e 20 2a 2f 0a 20 20 63 6f 6e 73  leOpen */.  cons
3946b 74 20 63 68 61 72 20 2a 2a 70 70 54 6f 6b 65 6e  t char **ppToken
3946c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3946d 2f 2a 20 4f 55 54 3a 20 2a 70 70 54 6f 6b 65 6e  /* OUT: *ppToken
3946e 20 69 73 20 74 68 65 20 74 6f 6b 65 6e 20 74 65   is the token te
3946f 78 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 42  xt */.  int *pnB
39470 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20  ytes,           
39471 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
39472 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79  UT: Number of by
39473 74 65 73 20 69 6e 20 74 6f 6b 65 6e 20 2a 2f 0a  tes in token */.
39474 20 20 69 6e 74 20 2a 70 69 53 74 61 72 74 4f 66    int *piStartOf
39475 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 20  fset,           
39476 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 74        /* OUT: St
39477 61 72 74 69 6e 67 20 6f 66 66 73 65 74 20 6f 66  arting offset of
39478 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20   token */.  int 
39479 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c 20 20 20  *piEndOffset,   
3947a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3947b 2f 2a 20 4f 55 54 3a 20 45 6e 64 69 6e 67 20 6f  /* OUT: Ending o
3947c 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a  ffset of token *
3947d 2f 0a 20 20 69 6e 74 20 2a 70 69 50 6f 73 69 74  /.  int *piPosit
3947e 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  ion             
3947f 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20          /* OUT: 
39480 50 6f 73 69 74 69 6f 6e 20 69 6e 74 65 67 65 72  Position integer
39481 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a   of token */.){.
39482 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a    simple_tokeniz
39483 65 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28  er_cursor *c = (
39484 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72  simple_tokenizer
39485 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73  _cursor *) pCurs
39486 6f 72 3b 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b  or;.  simple_tok
39487 65 6e 69 7a 65 72 20 2a 74 20 3d 20 28 73 69 6d  enizer *t = (sim
39488 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 29  ple_tokenizer *)
39489 20 70 43 75 72 73 6f 72 2d 3e 70 54 6f 6b 65 6e   pCursor->pToken
3948a 69 7a 65 72 3b 0a 20 20 75 6e 73 69 67 6e 65 64  izer;.  unsigned
3948b 20 63 68 61 72 20 2a 70 20 3d 20 28 75 6e 73 69   char *p = (unsi
3948c 67 6e 65 64 20 63 68 61 72 20 2a 29 63 2d 3e 70  gned char *)c->p
3948d 49 6e 70 75 74 3b 0a 0a 20 20 77 68 69 6c 65 28  Input;..  while(
3948e 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d 3e 6e   c->iOffset<c->n
3948f 42 79 74 65 73 20 29 7b 0a 20 20 20 20 69 6e 74  Bytes ){.    int
39490 20 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a 0a   iStartOffset;..
39491 20 20 20 20 2f 2a 20 53 63 61 6e 20 70 61 73 74      /* Scan past
39492 20 64 65 6c 69 6d 69 74 65 72 20 63 68 61 72 61   delimiter chara
39493 63 74 65 72 73 20 2a 2f 0a 20 20 20 20 77 68 69  cters */.    whi
39494 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63  le( c->iOffset<c
39495 2d 3e 6e 42 79 74 65 73 20 26 26 20 73 69 6d 70  ->nBytes && simp
39496 6c 65 44 65 6c 69 6d 28 74 2c 20 70 5b 63 2d 3e  leDelim(t, p[c->
39497 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20 20 20  iOffset]) ){.   
39498 20 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b 2b 3b     c->iOffset++;
39499 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43  .    }..    /* C
3949a 6f 75 6e 74 20 6e 6f 6e 2d 64 65 6c 69 6d 69 74  ount non-delimit
3949b 65 72 20 63 68 61 72 61 63 74 65 72 73 2e 20 2a  er characters. *
3949c 2f 0a 20 20 20 20 69 53 74 61 72 74 4f 66 66 73  /.    iStartOffs
3949d 65 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74 3b  et = c->iOffset;
3949e 0a 20 20 20 20 77 68 69 6c 65 28 20 63 2d 3e 69  .    while( c->i
3949f 4f 66 66 73 65 74 3c 63 2d 3e 6e 42 79 74 65 73  Offset<c->nBytes
394a0 20 26 26 20 21 73 69 6d 70 6c 65 44 65 6c 69 6d   && !simpleDelim
394a1 28 74 2c 20 70 5b 63 2d 3e 69 4f 66 66 73 65 74  (t, p[c->iOffset
394a2 5d 29 20 29 7b 0a 20 20 20 20 20 20 63 2d 3e 69  ]) ){.      c->i
394a3 4f 66 66 73 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a  Offset++;.    }.
394a4 0a 20 20 20 20 69 66 28 20 63 2d 3e 69 4f 66 66  .    if( c->iOff
394a5 73 65 74 3e 69 53 74 61 72 74 4f 66 66 73 65 74  set>iStartOffset
394a6 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 2c   ){.      int i,
394a7 20 6e 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74 2d   n = c->iOffset-
394a8 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a 20 20  iStartOffset;.  
394a9 20 20 20 20 69 66 28 20 6e 3e 63 2d 3e 6e 54 6f      if( n>c->nTo
394aa 6b 65 6e 41 6c 6c 6f 63 61 74 65 64 20 29 7b 0a  kenAllocated ){.
394ab 20 20 20 20 20 20 20 20 63 2d 3e 6e 54 6f 6b 65          c->nToke
394ac 6e 41 6c 6c 6f 63 61 74 65 64 20 3d 20 6e 2b 32  nAllocated = n+2
394ad 30 3b 0a 20 20 20 20 20 20 20 20 63 2d 3e 70 54  0;.        c->pT
394ae 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f 72  oken = sqlite3_r
394af 65 61 6c 6c 6f 63 28 63 2d 3e 70 54 6f 6b 65 6e  ealloc(c->pToken
394b0 2c 20 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63  , c->nTokenAlloc
394b1 61 74 65 64 29 3b 0a 20 20 20 20 20 20 20 20 69  ated);.        i
394b2 66 28 20 63 2d 3e 70 54 6f 6b 65 6e 3d 3d 4e 55  f( c->pToken==NU
394b3 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  LL ) return SQLI
394b4 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
394b5 7d 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  }.      for(i=0;
394b6 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20   i<n; i++){.    
394b7 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73      /* TODO(shes
394b8 73 29 20 54 68 69 73 20 6e 65 65 64 73 20 65 78  s) This needs ex
394b9 70 61 6e 73 69 6f 6e 20 74 6f 20 68 61 6e 64 6c  pansion to handl
394ba 65 20 55 54 46 2d 38 0a 20 20 20 20 20 20 20 20  e UTF-8.        
394bb 2a 2a 20 63 61 73 65 2d 69 6e 73 65 6e 73 69 74  ** case-insensit
394bc 69 76 69 74 79 2e 0a 20 20 20 20 20 20 20 20 2a  ivity..        *
394bd 2f 0a 20 20 20 20 20 20 20 20 75 6e 73 69 67 6e  /.        unsign
394be 65 64 20 63 68 61 72 20 63 68 20 3d 20 70 5b 69  ed char ch = p[i
394bf 53 74 61 72 74 4f 66 66 73 65 74 2b 69 5d 3b 0a  StartOffset+i];.
394c0 20 20 20 20 20 20 20 20 63 2d 3e 70 54 6f 6b 65          c->pToke
394c1 6e 5b 69 5d 20 3d 20 63 68 3c 30 78 38 30 20 3f  n[i] = ch<0x80 ?
394c2 20 74 6f 6c 6f 77 65 72 28 63 68 29 20 3a 20 63   tolower(ch) : c
394c3 68 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  h;.      }.     
394c4 20 2a 70 70 54 6f 6b 65 6e 20 3d 20 63 2d 3e 70   *ppToken = c->p
394c5 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 2a 70 6e  Token;.      *pn
394c6 42 79 74 65 73 20 3d 20 6e 3b 0a 20 20 20 20 20  Bytes = n;.     
394c7 20 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74 20   *piStartOffset 
394c8 3d 20 69 53 74 61 72 74 4f 66 66 73 65 74 3b 0a  = iStartOffset;.
394c9 20 20 20 20 20 20 2a 70 69 45 6e 64 4f 66 66 73        *piEndOffs
394ca 65 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74 3b  et = c->iOffset;
394cb 0a 20 20 20 20 20 20 2a 70 69 50 6f 73 69 74 69  .      *piPositi
394cc 6f 6e 20 3d 20 63 2d 3e 69 54 6f 6b 65 6e 2b 2b  on = c->iToken++
394cd 3b 0a 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ;..      return 
394ce 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
394cf 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
394d0 4c 49 54 45 5f 44 4f 4e 45 3b 0a 7d 0a 0a 2f 2a  LITE_DONE;.}../*
394d1 0a 2a 2a 20 54 68 65 20 73 65 74 20 6f 66 20 72  .** The set of r
394d2 6f 75 74 69 6e 65 73 20 74 68 61 74 20 69 6d 70  outines that imp
394d3 6c 65 6d 65 6e 74 20 74 68 65 20 73 69 6d 70 6c  lement the simpl
394d4 65 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73  e tokenizer.*/.s
394d5 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
394d6 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
394d7 64 75 6c 65 20 73 69 6d 70 6c 65 54 6f 6b 65 6e  dule simpleToken
394d8 69 7a 65 72 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20  izerModule = {. 
394d9 20 30 2c 0a 20 20 73 69 6d 70 6c 65 43 72 65 61   0,.  simpleCrea
394da 74 65 2c 0a 20 20 73 69 6d 70 6c 65 44 65 73 74  te,.  simpleDest
394db 72 6f 79 2c 0a 20 20 73 69 6d 70 6c 65 4f 70 65  roy,.  simpleOpe
394dc 6e 2c 0a 20 20 73 69 6d 70 6c 65 43 6c 6f 73 65  n,.  simpleClose
394dd 2c 0a 20 20 73 69 6d 70 6c 65 4e 65 78 74 2c 0a  ,.  simpleNext,.
394de 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  };../*.** Alloca
394df 74 65 20 61 20 6e 65 77 20 73 69 6d 70 6c 65 20  te a new simple 
394e0 74 6f 6b 65 6e 69 7a 65 72 2e 20 20 52 65 74 75  tokenizer.  Retu
394e1 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
394e2 74 68 65 20 6e 65 77 0a 2a 2a 20 74 6f 6b 65 6e  the new.** token
394e3 69 7a 65 72 20 69 6e 20 2a 70 70 4d 6f 64 75 6c  izer in *ppModul
394e4 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  e.*/.SQLITE_PRIV
394e5 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
394e6 46 74 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69  Fts3SimpleTokeni
394e7 7a 65 72 4d 6f 64 75 6c 65 28 0a 20 20 73 71 6c  zerModule(.  sql
394e8 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
394e9 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70 4d  odule const**ppM
394ea 6f 64 75 6c 65 0a 29 7b 0a 20 20 2a 70 70 4d 6f  odule.){.  *ppMo
394eb 64 75 6c 65 20 3d 20 26 73 69 6d 70 6c 65 54 6f  dule = &simpleTo
394ec 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a 7d  kenizerModule;.}
394ed 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  ..#endif /* !def
394ee 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45  ined(SQLITE_CORE
394ef 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
394f0 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29  ITE_ENABLE_FTS3)
394f1 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
394f2 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33  **** End of fts3
394f3 5f 74 6f 6b 65 6e 69 7a 65 72 31 2e 63 20 2a 2a  _tokenizer1.c **
394f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
394f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
394f6 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
394f7 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
394f8 72 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  rtree.c ********
394f9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
394fa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
394fb 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20  ***/./*.** 2001 
394fc 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a  September 15.**.
394fd 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
394fe 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
394ff 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
39500 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
39501 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
39502 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
39503 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
39504 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
39505 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
39506 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
39507 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
39508 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
39509 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
3950a 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
3950b 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
3950c 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
3950d 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
3950e 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
3950f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39510 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39511 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
39512 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
39513 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
39514 73 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65  s code for imple
39515 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68  mentations of th
39516 65 20 72 2d 74 72 65 65 20 61 6e 64 20 72 2a 2d  e r-tree and r*-
39517 74 72 65 65 0a 2a 2a 20 61 6c 67 6f 72 69 74 68  tree.** algorith
39518 6d 73 20 70 61 63 6b 61 67 65 64 20 61 73 20 61  ms packaged as a
39519 6e 20 53 51 4c 69 74 65 20 76 69 72 74 75 61 6c  n SQLite virtual
3951a 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a   table module..*
3951b 2a 0a 2a 2a 20 24 49 64 3a 20 72 74 72 65 65 2e  *.** $Id: rtree.
3951c 63 2c 76 20 31 2e 31 34 20 32 30 30 39 2f 30 38  c,v 1.14 2009/08
3951d 2f 30 36 20 31 38 3a 33 36 3a 34 37 20 64 61 6e  /06 18:36:47 dan
3951e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a  ielk1977 Exp $.*
3951f 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
39520 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
39521 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
39522 4e 41 42 4c 45 5f 52 54 52 45 45 29 0a 0a 2f 2a  NABLE_RTREE)../*
39523 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
39524 6e 74 61 69 6e 73 20 61 6e 20 69 6d 70 6c 65 6d  ntains an implem
39525 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 63 6f  entation of a co
39526 75 70 6c 65 20 6f 66 20 64 69 66 66 65 72 65 6e  uple of differen
39527 74 20 76 61 72 69 61 6e 74 73 0a 2a 2a 20 6f 66  t variants.** of
39528 20 74 68 65 20 72 2d 74 72 65 65 20 61 6c 67 6f   the r-tree algo
39529 72 69 74 68 6d 2e 20 53 65 65 20 74 68 65 20 52  rithm. See the R
3952a 45 41 44 4d 45 20 66 69 6c 65 20 66 6f 72 20 66  EADME file for f
3952b 75 72 74 68 65 72 20 64 65 74 61 69 6c 73 2e 20  urther details. 
3952c 54 68 65 20 0a 2a 2a 20 73 61 6d 65 20 64 61 74  The .** same dat
3952d 61 2d 73 74 72 75 63 74 75 72 65 20 69 73 20 75  a-structure is u
3952e 73 65 64 20 66 6f 72 20 61 6c 6c 2c 20 62 75 74  sed for all, but
3952f 20 74 68 65 20 61 6c 67 6f 72 69 74 68 6d 73 20   the algorithms 
39530 66 6f 72 20 69 6e 73 65 72 74 20 61 6e 64 0a 2a  for insert and.*
39531 2a 20 64 65 6c 65 74 65 20 6f 70 65 72 61 74 69  * delete operati
39532 6f 6e 73 20 76 61 72 79 2e 20 54 68 65 20 76 61  ons vary. The va
39533 72 69 61 6e 74 73 20 75 73 65 64 20 61 72 65 20  riants used are 
39534 73 65 6c 65 63 74 65 64 20 61 74 20 63 6f 6d 70  selected at comp
39535 69 6c 65 20 74 69 6d 65 20 0a 2a 2a 20 62 79 20  ile time .** by 
39536 64 65 66 69 6e 69 6e 67 20 74 68 65 20 66 6f 6c  defining the fol
39537 6c 6f 77 69 6e 67 20 73 79 6d 62 6f 6c 73 3a 0a  lowing symbols:.
39538 2a 2f 0a 0a 2f 2a 20 45 69 74 68 65 72 2c 20 62  */../* Either, b
39539 6f 74 68 20 6f 72 20 6e 6f 6e 65 20 6f 66 20 74  oth or none of t
3953a 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 79  he following may
3953b 20 62 65 20 73 65 74 20 74 6f 20 61 63 74 69 76   be set to activ
3953c 61 74 65 20 0a 2a 2a 20 72 2a 74 72 65 65 20 76  ate .** r*tree v
3953d 61 72 69 61 6e 74 20 61 6c 67 6f 72 69 74 68 6d  ariant algorithm
3953e 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 56 41  s..*/.#define VA
3953f 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f  RIANT_RSTARTREE_
39540 43 48 4f 4f 53 45 53 55 42 54 52 45 45 20 30 0a  CHOOSESUBTREE 0.
39541 23 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54 5f  #define VARIANT_
39542 52 53 54 41 52 54 52 45 45 5f 52 45 49 4e 53 45  RSTARTREE_REINSE
39543 52 54 20 20 20 20 20 20 31 0a 0a 2f 2a 20 0a 2a  RT      1../* .*
39544 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 6f 66  * Exactly one of
39545 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d   the following m
39546 75 73 74 20 62 65 20 73 65 74 20 74 6f 20 31 2e  ust be set to 1.
39547 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 56 41 52 49  .*/.#define VARI
39548 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55 41 44  ANT_GUTTMAN_QUAD
39549 52 41 54 49 43 5f 53 50 4c 49 54 20 30 0a 23 64  RATIC_SPLIT 0.#d
3954a 65 66 69 6e 65 20 56 41 52 49 41 4e 54 5f 47 55  efine VARIANT_GU
3954b 54 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53 50 4c  TTMAN_LINEAR_SPL
3954c 49 54 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20  IT    0.#define 
3954d 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45  VARIANT_RSTARTRE
3954e 45 5f 53 50 4c 49 54 20 20 20 20 20 20 20 20 20  E_SPLIT         
3954f 31 0a 0a 23 64 65 66 69 6e 65 20 56 41 52 49 41  1..#define VARIA
39550 4e 54 5f 47 55 54 54 4d 41 4e 5f 53 50 4c 49 54  NT_GUTTMAN_SPLIT
39551 20 5c 0a 20 20 20 20 20 20 20 20 28 56 41 52 49   \.        (VARI
39552 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e 45  ANT_GUTTMAN_LINE
39553 41 52 5f 53 50 4c 49 54 7c 7c 56 41 52 49 41 4e  AR_SPLIT||VARIAN
39554 54 5f 47 55 54 54 4d 41 4e 5f 51 55 41 44 52 41  T_GUTTMAN_QUADRA
39555 54 49 43 5f 53 50 4c 49 54 29 0a 0a 23 69 66 20  TIC_SPLIT)..#if 
39556 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f  VARIANT_GUTTMAN_
39557 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49 54 0a  QUADRATIC_SPLIT.
39558 20 20 23 64 65 66 69 6e 65 20 50 69 63 6b 4e 65    #define PickNe
39559 78 74 20 51 75 61 64 72 61 74 69 63 50 69 63 6b  xt QuadraticPick
3955a 4e 65 78 74 0a 20 20 23 64 65 66 69 6e 65 20 50  Next.  #define P
3955b 69 63 6b 53 65 65 64 73 20 51 75 61 64 72 61 74  ickSeeds Quadrat
3955c 69 63 50 69 63 6b 53 65 65 64 73 0a 20 20 23 64  icPickSeeds.  #d
3955d 65 66 69 6e 65 20 41 73 73 69 67 6e 43 65 6c 6c  efine AssignCell
3955e 73 20 73 70 6c 69 74 4e 6f 64 65 47 75 74 74 6d  s splitNodeGuttm
3955f 61 6e 0a 23 65 6e 64 69 66 0a 23 69 66 20 56 41  an.#endif.#if VA
39560 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49  RIANT_GUTTMAN_LI
39561 4e 45 41 52 5f 53 50 4c 49 54 0a 20 20 23 64 65  NEAR_SPLIT.  #de
39562 66 69 6e 65 20 50 69 63 6b 4e 65 78 74 20 4c 69  fine PickNext Li
39563 6e 65 61 72 50 69 63 6b 4e 65 78 74 0a 20 20 23  nearPickNext.  #
39564 64 65 66 69 6e 65 20 50 69 63 6b 53 65 65 64 73  define PickSeeds
39565 20 4c 69 6e 65 61 72 50 69 63 6b 53 65 65 64 73   LinearPickSeeds
39566 0a 20 20 23 64 65 66 69 6e 65 20 41 73 73 69 67  .  #define Assig
39567 6e 43 65 6c 6c 73 20 73 70 6c 69 74 4e 6f 64 65  nCells splitNode
39568 47 75 74 74 6d 61 6e 0a 23 65 6e 64 69 66 0a 23  Guttman.#endif.#
39569 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52  if VARIANT_RSTAR
3956a 54 52 45 45 5f 53 50 4c 49 54 0a 20 20 23 64 65  TREE_SPLIT.  #de
3956b 66 69 6e 65 20 41 73 73 69 67 6e 43 65 6c 6c 73  fine AssignCells
3956c 20 73 70 6c 69 74 4e 6f 64 65 53 74 61 72 74 72   splitNodeStartr
3956d 65 65 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e  ee.#endif...#ifn
3956e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a  def SQLITE_CORE.
3956f 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49    SQLITE_EXTENSI
39570 4f 4e 5f 49 4e 49 54 31 0a 23 65 6c 73 65 0a 23  ON_INIT1.#else.#
39571 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20  endif...#ifndef 
39572 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54  SQLITE_AMALGAMAT
39573 49 4f 4e 0a 74 79 70 65 64 65 66 20 73 71 6c 69  ION.typedef sqli
39574 74 65 33 5f 69 6e 74 36 34 20 69 36 34 3b 0a 74  te3_int64 i64;.t
39575 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20  ypedef unsigned 
39576 63 68 61 72 20 75 38 3b 0a 74 79 70 65 64 65 66  char u8;.typedef
39577 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 75 33   unsigned int u3
39578 32 3b 0a 23 65 6e 64 69 66 0a 0a 74 79 70 65 64  2;.#endif..typed
39579 65 66 20 73 74 72 75 63 74 20 52 74 72 65 65 20  ef struct Rtree 
3957a 52 74 72 65 65 3b 0a 74 79 70 65 64 65 66 20 73  Rtree;.typedef s
3957b 74 72 75 63 74 20 52 74 72 65 65 43 75 72 73 6f  truct RtreeCurso
3957c 72 20 52 74 72 65 65 43 75 72 73 6f 72 3b 0a 74  r RtreeCursor;.t
3957d 79 70 65 64 65 66 20 73 74 72 75 63 74 20 52 74  ypedef struct Rt
3957e 72 65 65 4e 6f 64 65 20 52 74 72 65 65 4e 6f 64  reeNode RtreeNod
3957f 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  e;.typedef struc
39580 74 20 52 74 72 65 65 43 65 6c 6c 20 52 74 72 65  t RtreeCell Rtre
39581 65 43 65 6c 6c 3b 0a 74 79 70 65 64 65 66 20 73  eCell;.typedef s
39582 74 72 75 63 74 20 52 74 72 65 65 43 6f 6e 73 74  truct RtreeConst
39583 72 61 69 6e 74 20 52 74 72 65 65 43 6f 6e 73 74  raint RtreeConst
39584 72 61 69 6e 74 3b 0a 74 79 70 65 64 65 66 20 75  raint;.typedef u
39585 6e 69 6f 6e 20 52 74 72 65 65 43 6f 6f 72 64 20  nion RtreeCoord 
39586 52 74 72 65 65 43 6f 6f 72 64 3b 0a 0a 2f 2a 20  RtreeCoord;../* 
39587 54 68 65 20 72 74 72 65 65 20 6d 61 79 20 68 61  The rtree may ha
39588 76 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64  ve between 1 and
39589 20 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e   RTREE_MAX_DIMEN
3958a 53 49 4f 4e 53 20 64 69 6d 65 6e 73 69 6f 6e 73  SIONS dimensions
3958b 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 52 54 52  . */.#define RTR
3958c 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e  EE_MAX_DIMENSION
3958d 53 20 35 0a 0a 2f 2a 20 53 69 7a 65 20 6f 66 20  S 5../* Size of 
3958e 68 61 73 68 20 74 61 62 6c 65 20 52 74 72 65 65  hash table Rtree
3958f 2e 61 48 61 73 68 2e 20 54 68 69 73 20 68 61 73  .aHash. This has
39590 68 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 65  h table is not e
39591 78 70 65 63 74 65 64 20 74 6f 0a 2a 2a 20 65 76  xpected to.** ev
39592 65 72 20 63 6f 6e 74 61 69 6e 20 76 65 72 79 20  er contain very 
39593 6d 61 6e 79 20 65 6e 74 72 69 65 73 2c 20 73 6f  many entries, so
39594 20 61 20 66 69 78 65 64 20 6e 75 6d 62 65 72 20   a fixed number 
39595 6f 66 20 62 75 63 6b 65 74 73 20 69 73 20 0a 2a  of buckets is .*
39596 2a 20 75 73 65 64 2e 0a 2a 2f 0a 23 64 65 66 69  * used..*/.#defi
39597 6e 65 20 48 41 53 48 53 49 5a 45 20 31 32 38 0a  ne HASHSIZE 128.
39598 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 72 74 72 65 65  ./* .** An rtree
39599 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20 6f   virtual-table o
3959a 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 72 75 63 74  bject..*/.struct
3959b 20 52 74 72 65 65 20 7b 0a 20 20 73 71 6c 69 74   Rtree {.  sqlit
3959c 65 33 5f 76 74 61 62 20 62 61 73 65 3b 0a 20 20  e3_vtab base;.  
3959d 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
3959e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48              /* H
3959f 6f 73 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ost database con
395a0 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74  nection */.  int
395a1 20 69 4e 6f 64 65 53 69 7a 65 3b 20 20 20 20 20   iNodeSize;     
395a2 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
395a3 20 69 6e 20 62 79 74 65 73 20 6f 66 20 65 61 63   in bytes of eac
395a4 68 20 6e 6f 64 65 20 69 6e 20 74 68 65 20 6e 6f  h node in the no
395a5 64 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  de table */.  in
395a6 74 20 6e 44 69 6d 3b 20 20 20 20 20 20 20 20 20  t nDim;         
395a7 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
395a8 62 65 72 20 6f 66 20 64 69 6d 65 6e 73 69 6f 6e  ber of dimension
395a9 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65  s */.  int nByte
395aa 73 50 65 72 43 65 6c 6c 3b 20 20 20 20 20 20 20  sPerCell;       
395ab 20 20 20 2f 2a 20 42 79 74 65 73 20 63 6f 6e 73     /* Bytes cons
395ac 75 6d 65 64 20 70 65 72 20 63 65 6c 6c 20 2a 2f  umed per cell */
395ad 0a 20 20 69 6e 74 20 69 44 65 70 74 68 3b 20 20  .  int iDepth;  
395ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
395af 2a 20 43 75 72 72 65 6e 74 20 64 65 70 74 68 20  * Current depth 
395b0 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20 73 74  of the r-tree st
395b1 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 63 68 61  ructure */.  cha
395b2 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 20 20  r *zDb;         
395b3 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
395b4 20 6f 66 20 64 61 74 61 62 61 73 65 20 63 6f 6e   of database con
395b5 74 61 69 6e 69 6e 67 20 72 2d 74 72 65 65 20 74  taining r-tree t
395b6 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  able */.  char *
395b7 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  zName;          
395b8 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
395b9 20 72 2d 74 72 65 65 20 74 61 62 6c 65 20 2a 2f   r-tree table */
395ba 20 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 61   .  RtreeNode *a
395bb 48 61 73 68 5b 48 41 53 48 53 49 5a 45 5d 3b 20  Hash[HASHSIZE]; 
395bc 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 6f 66  /* Hash table of
395bd 20 69 6e 2d 6d 65 6d 6f 72 79 20 6e 6f 64 65 73   in-memory nodes
395be 2e 20 2a 2f 20 0a 20 20 69 6e 74 20 6e 42 75 73  . */ .  int nBus
395bf 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
395c0 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e      /* Current n
395c1 75 6d 62 65 72 20 6f 66 20 75 73 65 72 73 20 6f  umber of users o
395c2 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
395c3 20 2a 2f 0a 0a 20 20 2f 2a 20 4c 69 73 74 20 6f   */..  /* List o
395c4 66 20 6e 6f 64 65 73 20 72 65 6d 6f 76 65 64 20  f nodes removed 
395c5 64 75 72 69 6e 67 20 61 20 43 6f 6e 64 65 6e 73  during a Condens
395c6 65 54 72 65 65 20 6f 70 65 72 61 74 69 6f 6e 2e  eTree operation.
395c7 20 4c 69 73 74 20 69 73 0a 20 20 2a 2a 20 6c 69   List is.  ** li
395c8 6e 6b 65 64 20 74 6f 67 65 74 68 65 72 20 76 69  nked together vi
395c9 61 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6e 6f  a the pointer no
395ca 72 6d 61 6c 6c 79 20 75 73 65 64 20 66 6f 72 20  rmally used for 
395cb 68 61 73 68 20 63 68 61 69 6e 73 20 2d 0a 20 20  hash chains -.  
395cc 2a 2a 20 52 74 72 65 65 4e 6f 64 65 2e 70 4e 65  ** RtreeNode.pNe
395cd 78 74 2e 20 52 74 72 65 65 4e 6f 64 65 2e 69 4e  xt. RtreeNode.iN
395ce 6f 64 65 20 73 74 6f 72 65 73 20 74 68 65 20 64  ode stores the d
395cf 65 70 74 68 20 6f 66 20 74 68 65 20 73 75 62 2d  epth of the sub-
395d0 74 72 65 65 20 0a 20 20 2a 2a 20 68 65 61 64 65  tree .  ** heade
395d1 64 20 62 79 20 74 68 65 20 6e 6f 64 65 20 28 6c  d by the node (l
395d2 65 61 66 20 6e 6f 64 65 73 20 68 61 76 65 20 52  eaf nodes have R
395d3 74 72 65 65 4e 6f 64 65 2e 69 4e 6f 64 65 3d 3d  treeNode.iNode==
395d4 30 29 2e 0a 20 20 2a 2f 0a 20 20 52 74 72 65 65  0)..  */.  Rtree
395d5 4e 6f 64 65 20 2a 70 44 65 6c 65 74 65 64 3b 0a  Node *pDeleted;.
395d6 20 20 69 6e 74 20 69 52 65 69 6e 73 65 72 74 48    int iReinsertH
395d7 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 2f 2a  eight;        /*
395d8 20 48 65 69 67 68 74 20 6f 66 20 73 75 62 2d 74   Height of sub-t
395d9 72 65 65 73 20 52 65 69 6e 73 65 72 74 28 29 20  rees Reinsert() 
395da 68 61 73 20 72 75 6e 20 6f 6e 20 2a 2f 0a 0a 20  has run on */.. 
395db 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 73 20 74   /* Statements t
395dc 6f 20 72 65 61 64 2f 77 72 69 74 65 2f 64 65 6c  o read/write/del
395dd 65 74 65 20 61 20 72 65 63 6f 72 64 20 66 72 6f  ete a record fro
395de 6d 20 78 78 78 5f 6e 6f 64 65 20 2a 2f 0a 20 20  m xxx_node */.  
395df 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 52  sqlite3_stmt *pR
395e0 65 61 64 4e 6f 64 65 3b 0a 20 20 73 71 6c 69 74  eadNode;.  sqlit
395e1 65 33 5f 73 74 6d 74 20 2a 70 57 72 69 74 65 4e  e3_stmt *pWriteN
395e2 6f 64 65 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  ode;.  sqlite3_s
395e3 74 6d 74 20 2a 70 44 65 6c 65 74 65 4e 6f 64 65  tmt *pDeleteNode
395e4 3b 0a 0a 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e  ;..  /* Statemen
395e5 74 73 20 74 6f 20 72 65 61 64 2f 77 72 69 74 65  ts to read/write
395e6 2f 64 65 6c 65 74 65 20 61 20 72 65 63 6f 72 64  /delete a record
395e7 20 66 72 6f 6d 20 78 78 78 5f 72 6f 77 69 64 20   from xxx_rowid 
395e8 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
395e9 74 20 2a 70 52 65 61 64 52 6f 77 69 64 3b 0a 20  t *pReadRowid;. 
395ea 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
395eb 57 72 69 74 65 52 6f 77 69 64 3b 0a 20 20 73 71  WriteRowid;.  sq
395ec 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44 65 6c  lite3_stmt *pDel
395ed 65 74 65 52 6f 77 69 64 3b 0a 0a 20 20 2f 2a 20  eteRowid;..  /* 
395ee 53 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 72 65  Statements to re
395ef 61 64 2f 77 72 69 74 65 2f 64 65 6c 65 74 65 20  ad/write/delete 
395f0 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 78 78  a record from xx
395f1 78 5f 70 61 72 65 6e 74 20 2a 2f 0a 20 20 73 71  x_parent */.  sq
395f2 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 52 65 61  lite3_stmt *pRea
395f3 64 50 61 72 65 6e 74 3b 0a 20 20 73 71 6c 69 74  dParent;.  sqlit
395f4 65 33 5f 73 74 6d 74 20 2a 70 57 72 69 74 65 50  e3_stmt *pWriteP
395f5 61 72 65 6e 74 3b 0a 20 20 73 71 6c 69 74 65 33  arent;.  sqlite3
395f6 5f 73 74 6d 74 20 2a 70 44 65 6c 65 74 65 50 61  _stmt *pDeletePa
395f7 72 65 6e 74 3b 0a 0a 20 20 69 6e 74 20 65 43 6f  rent;..  int eCo
395f8 6f 72 64 54 79 70 65 3b 0a 7d 3b 0a 0a 2f 2a 20  ordType;.};../* 
395f9 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73 20  Possible values 
395fa 66 6f 72 20 65 43 6f 6f 72 64 54 79 70 65 3a 20  for eCoordType: 
395fb 2a 2f 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45  */.#define RTREE
395fc 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32 20 30 0a  _COORD_REAL32 0.
395fd 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f 43 4f  #define RTREE_CO
395fe 4f 52 44 5f 49 4e 54 33 32 20 20 31 0a 0a 2f 2a  ORD_INT32  1../*
395ff 0a 2a 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20  .** The minimum 
39600 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20  number of cells 
39601 61 6c 6c 6f 77 65 64 20 66 6f 72 20 61 20 6e 6f  allowed for a no
39602 64 65 20 69 73 20 61 20 74 68 69 72 64 20 6f 66  de is a third of
39603 20 74 68 65 20 0a 2a 2a 20 6d 61 78 69 6d 75 6d   the .** maximum
39604 2e 20 49 6e 20 47 75 74 6d 61 6e 27 73 20 6e 6f  . In Gutman's no
39605 74 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20  tation:.**.**   
39606 20 20 6d 20 3d 20 4d 2f 33 0a 2a 2a 0a 2a 2a 20    m = M/3.**.** 
39607 49 66 20 61 6e 20 52 2a 2d 74 72 65 65 20 22 52  If an R*-tree "R
39608 65 69 6e 73 65 72 74 22 20 6f 70 65 72 61 74 69  einsert" operati
39609 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64 2c 20  on is required, 
3960a 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72 20  the same number 
3960b 6f 66 0a 2a 2a 20 63 65 6c 6c 73 20 61 72 65 20  of.** cells are 
3960c 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68 65  removed from the
3960d 20 6f 76 65 72 66 75 6c 6c 20 6e 6f 64 65 20 61   overfull node a
3960e 6e 64 20 72 65 69 6e 73 65 72 74 65 64 20 69 6e  nd reinserted in
3960f 74 6f 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a  to the tree..*/.
39610 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f 4d 49  #define RTREE_MI
39611 4e 43 45 4c 4c 53 28 70 29 20 28 28 28 28 70 29  NCELLS(p) ((((p)
39612 2d 3e 69 4e 6f 64 65 53 69 7a 65 2d 34 29 2f 28  ->iNodeSize-4)/(
39613 70 29 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c  p)->nBytesPerCel
39614 6c 29 2f 33 29 0a 23 64 65 66 69 6e 65 20 52 54  l)/3).#define RT
39615 52 45 45 5f 52 45 49 4e 53 45 52 54 28 70 29 20  REE_REINSERT(p) 
39616 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70  RTREE_MINCELLS(p
39617 29 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f  ).#define RTREE_
39618 4d 41 58 43 45 4c 4c 53 20 35 31 0a 0a 2f 2a 20  MAXCELLS 51../* 
39619 0a 2a 2a 20 41 6e 20 72 74 72 65 65 20 63 75 72  .** An rtree cur
3961a 73 6f 72 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73  sor object..*/.s
3961b 74 72 75 63 74 20 52 74 72 65 65 43 75 72 73 6f  truct RtreeCurso
3961c 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  r {.  sqlite3_vt
3961d 61 62 5f 63 75 72 73 6f 72 20 62 61 73 65 3b 0a  ab_cursor base;.
3961e 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
3961f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  de;             
39620 20 20 20 20 2f 2a 20 4e 6f 64 65 20 63 75 72 73      /* Node curs
39621 6f 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20  or is currently 
39622 70 6f 69 6e 74 69 6e 67 20 61 74 20 2a 2f 0a 20  pointing at */. 
39623 20 69 6e 74 20 69 43 65 6c 6c 3b 20 20 20 20 20   int iCell;     
39624 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39625 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63     /* Index of c
39626 75 72 72 65 6e 74 20 63 65 6c 6c 20 69 6e 20 70  urrent cell in p
39627 4e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69 53  Node */.  int iS
39628 74 72 61 74 65 67 79 3b 20 20 20 20 20 20 20 20  trategy;        
39629 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
3962a 6f 70 79 20 6f 66 20 69 64 78 4e 75 6d 20 73 65  opy of idxNum se
3962b 61 72 63 68 20 70 61 72 61 6d 65 74 65 72 20 2a  arch parameter *
3962c 2f 0a 20 20 69 6e 74 20 6e 43 6f 6e 73 74 72 61  /.  int nConstra
3962d 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  int;            
3962e 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
3962f 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 43  of entries in aC
39630 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 52  onstraint */.  R
39631 74 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 2a  treeConstraint *
39632 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20  aConstraint;    
39633 20 2f 2a 20 53 65 61 72 63 68 20 63 6f 6e 73 74   /* Search const
39634 72 61 69 6e 74 73 2e 20 2a 2f 0a 7d 3b 0a 0a 75  raints. */.};..u
39635 6e 69 6f 6e 20 52 74 72 65 65 43 6f 6f 72 64 20  nion RtreeCoord 
39636 7b 0a 20 20 66 6c 6f 61 74 20 66 3b 0a 20 20 69  {.  float f;.  i
39637 6e 74 20 69 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  nt i;.};../*.** 
39638 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20  The argument is 
39639 61 6e 20 52 74 72 65 65 43 6f 6f 72 64 2e 20 52  an RtreeCoord. R
3963a 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20  eturn the value 
3963b 73 74 6f 72 65 64 20 77 69 74 68 69 6e 20 74 68  stored within th
3963c 65 20 52 74 72 65 65 43 6f 6f 72 64 0a 2a 2a 20  e RtreeCoord.** 
3963d 66 6f 72 6d 61 74 74 65 64 20 61 73 20 61 20 64  formatted as a d
3963e 6f 75 62 6c 65 2e 20 54 68 69 73 20 6d 61 63 72  ouble. This macr
3963f 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74 20 6c  o assumes that l
39640 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 20 70 52  ocal variable pR
39641 74 72 65 65 20 70 6f 69 6e 74 73 0a 2a 2a 20 74  tree points.** t
39642 6f 20 74 68 65 20 52 74 72 65 65 20 73 74 72 75  o the Rtree stru
39643 63 74 75 72 65 20 61 73 73 6f 63 69 61 74 65 64  cture associated
39644 20 77 69 74 68 20 74 68 65 20 52 74 72 65 65 43   with the RtreeC
39645 6f 6f 72 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65  oord..*/.#define
39646 20 44 43 4f 4f 52 44 28 63 6f 6f 72 64 29 20 28   DCOORD(coord) (
39647 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39648 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 28             \.  (
39649 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79  pRtree->eCoordTy
3964a 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f  pe==RTREE_COORD_
3964b 52 45 41 4c 33 32 29 20 3f 20 20 20 20 20 20 5c  REAL32) ?      \
3964c 0a 20 20 20 20 28 28 64 6f 75 62 6c 65 29 63 6f  .    ((double)co
3964d 6f 72 64 2e 66 29 20 3a 20 20 20 20 20 20 20 20  ord.f) :        
3964e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3964f 20 20 20 5c 0a 20 20 20 20 28 28 64 6f 75 62 6c     \.    ((doubl
39650 65 29 63 6f 6f 72 64 2e 69 29 20 20 20 20 20 20  e)coord.i)      
39651 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39652 20 20 20 20 20 20 20 5c 0a 29 0a 0a 2f 2a 0a 2a         \.)../*.*
39653 2a 20 41 20 73 65 61 72 63 68 20 63 6f 6e 73 74  * A search const
39654 72 61 69 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74  raint..*/.struct
39655 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69 6e 74   RtreeConstraint
39656 20 7b 0a 20 20 69 6e 74 20 69 43 6f 6f 72 64 3b   {.  int iCoord;
39657 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39658 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
39659 6f 66 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 63  of constrained c
3965a 6f 6f 72 64 69 6e 61 74 65 20 2a 2f 0a 20 20 69  oordinate */.  i
3965b 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20 20  nt op;          
3965c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3965d 20 2f 2a 20 43 6f 6e 73 74 72 61 69 6e 69 6e 67   /* Constraining
3965e 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20   operation */.  
3965f 64 6f 75 62 6c 65 20 72 56 61 6c 75 65 3b 20 20  double rValue;  
39660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39661 20 20 2f 2a 20 43 6f 6e 73 74 72 61 69 6e 74 20    /* Constraint 
39662 76 61 6c 75 65 2e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  value. */.};../*
39663 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73   Possible values
39664 20 66 6f 72 20 52 74 72 65 65 43 6f 6e 73 74 72   for RtreeConstr
39665 61 69 6e 74 2e 6f 70 20 2a 2f 0a 23 64 65 66 69  aint.op */.#defi
39666 6e 65 20 52 54 52 45 45 5f 45 51 20 30 78 34 31  ne RTREE_EQ 0x41
39667 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f 4c  .#define RTREE_L
39668 45 20 30 78 34 32 0a 23 64 65 66 69 6e 65 20 52  E 0x42.#define R
39669 54 52 45 45 5f 4c 54 20 30 78 34 33 0a 23 64 65  TREE_LT 0x43.#de
3966a 66 69 6e 65 20 52 54 52 45 45 5f 47 45 20 30 78  fine RTREE_GE 0x
3966b 34 34 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45  44.#define RTREE
3966c 5f 47 54 20 30 78 34 35 0a 0a 2f 2a 20 0a 2a 2a  _GT 0x45../* .**
3966d 20 41 6e 20 72 74 72 65 65 20 73 74 72 75 63 74   An rtree struct
3966e 75 72 65 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  ure node..**.** 
3966f 44 61 74 61 20 66 6f 72 6d 61 74 20 28 52 74 72  Data format (Rtr
39670 65 65 4e 6f 64 65 2e 7a 44 61 74 61 29 3a 0a 2a  eeNode.zData):.*
39671 2a 0a 2a 2a 20 20 20 31 2e 20 49 66 20 74 68 65  *.**   1. If the
39672 20 6e 6f 64 65 20 69 73 20 74 68 65 20 72 6f 6f   node is the roo
39673 74 20 6e 6f 64 65 20 28 6e 6f 64 65 20 31 29 2c  t node (node 1),
39674 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 74 20   then the first 
39675 32 20 62 79 74 65 73 0a 2a 2a 20 20 20 20 20 20  2 bytes.**      
39676 6f 66 20 74 68 65 20 6e 6f 64 65 20 63 6f 6e 74  of the node cont
39677 61 69 6e 20 74 68 65 20 74 72 65 65 20 64 65 70  ain the tree dep
39678 74 68 20 61 73 20 61 20 62 69 67 2d 65 6e 64 69  th as a big-endi
39679 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 20  an integer..**  
3967a 20 20 20 20 46 6f 72 20 6e 6f 6e 2d 72 6f 6f 74      For non-root
3967b 20 6e 6f 64 65 73 2c 20 74 68 65 20 66 69 72 73   nodes, the firs
3967c 74 20 32 20 62 79 74 65 73 20 61 72 65 20 6c 65  t 2 bytes are le
3967d 66 74 20 75 6e 75 73 65 64 2e 0a 2a 2a 0a 2a 2a  ft unused..**.**
3967e 20 20 20 32 2e 20 54 68 65 20 6e 65 78 74 20 32     2. The next 2
3967f 20 62 79 74 65 73 20 63 6f 6e 74 61 69 6e 20 74   bytes contain t
39680 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74  he number of ent
39681 72 69 65 73 20 63 75 72 72 65 6e 74 6c 79 20 0a  ries currently .
39682 2a 2a 20 20 20 20 20 20 73 74 6f 72 65 64 20 69  **      stored i
39683 6e 20 74 68 65 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a  n the node..**.*
39684 2a 20 20 20 33 2e 20 54 68 65 20 72 65 6d 61 69  *   3. The remai
39685 6e 64 65 72 20 6f 66 20 74 68 65 20 6e 6f 64 65  nder of the node
39686 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e 6f   contains the no
39687 64 65 20 65 6e 74 72 69 65 73 2e 20 45 61 63 68  de entries. Each
39688 20 65 6e 74 72 79 0a 2a 2a 20 20 20 20 20 20 63   entry.**      c
39689 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73 69 6e  onsists of a sin
3968a 67 6c 65 20 38 2d 62 79 74 65 20 69 6e 74 65 67  gle 8-byte integ
3968b 65 72 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61  er followed by a
3968c 6e 20 65 76 65 6e 20 6e 75 6d 62 65 72 0a 2a 2a  n even number.**
3968d 20 20 20 20 20 20 6f 66 20 34 2d 62 79 74 65 20        of 4-byte 
3968e 63 6f 6f 72 64 69 6e 61 74 65 73 2e 20 46 6f 72  coordinates. For
3968f 20 6c 65 61 66 20 6e 6f 64 65 73 20 74 68 65 20   leaf nodes the 
39690 69 6e 74 65 67 65 72 20 69 73 20 74 68 65 20 72  integer is the r
39691 6f 77 69 64 0a 2a 2a 20 20 20 20 20 20 6f 66 20  owid.**      of 
39692 61 20 72 65 63 6f 72 64 2e 20 46 6f 72 20 69 6e  a record. For in
39693 74 65 72 6e 61 6c 20 6e 6f 64 65 73 20 69 74 20  ternal nodes it 
39694 69 73 20 74 68 65 20 6e 6f 64 65 20 6e 75 6d 62  is the node numb
39695 65 72 20 6f 66 20 61 0a 2a 2a 20 20 20 20 20 20  er of a.**      
39696 63 68 69 6c 64 20 70 61 67 65 2e 0a 2a 2f 0a 73  child page..*/.s
39697 74 72 75 63 74 20 52 74 72 65 65 4e 6f 64 65 20  truct RtreeNode 
39698 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  {.  RtreeNode *p
39699 50 61 72 65 6e 74 3b 20 20 20 20 20 20 20 20 20  Parent;         
3969a 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74 20        /* Parent 
3969b 6e 6f 64 65 20 2a 2f 0a 20 20 69 36 34 20 69 4e  node */.  i64 iN
3969c 6f 64 65 3b 0a 20 20 69 6e 74 20 6e 52 65 66 3b  ode;.  int nRef;
3969d 0a 20 20 69 6e 74 20 69 73 44 69 72 74 79 3b 0a  .  int isDirty;.
3969e 20 20 75 38 20 2a 7a 44 61 74 61 3b 0a 20 20 52    u8 *zData;.  R
3969f 74 72 65 65 4e 6f 64 65 20 2a 70 4e 65 78 74 3b  treeNode *pNext;
396a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
396a1 20 2f 2a 20 4e 65 78 74 20 6e 6f 64 65 20 69 6e   /* Next node in
396a2 20 74 68 69 73 20 68 61 73 68 20 63 68 61 69 6e   this hash chain
396a3 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20 4e   */.};.#define N
396a4 43 45 4c 4c 28 70 4e 6f 64 65 29 20 72 65 61 64  CELL(pNode) read
396a5 49 6e 74 31 36 28 26 28 70 4e 6f 64 65 29 2d 3e  Int16(&(pNode)->
396a6 7a 44 61 74 61 5b 32 5d 29 0a 0a 2f 2a 20 0a 2a  zData[2])../* .*
396a7 2a 20 53 74 72 75 63 74 75 72 65 20 74 6f 20 73  * Structure to s
396a8 74 6f 72 65 20 61 20 64 65 73 65 72 69 61 6c 69  tore a deseriali
396a9 7a 65 64 20 72 74 72 65 65 20 72 65 63 6f 72 64  zed rtree record
396aa 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74 72 65  ..*/.struct Rtre
396ab 65 43 65 6c 6c 20 7b 0a 20 20 69 36 34 20 69 52  eCell {.  i64 iR
396ac 6f 77 69 64 3b 0a 20 20 52 74 72 65 65 43 6f 6f  owid;.  RtreeCoo
396ad 72 64 20 61 43 6f 6f 72 64 5b 52 54 52 45 45 5f  rd aCoord[RTREE_
396ae 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a 32  MAX_DIMENSIONS*2
396af 5d 3b 0a 7d 3b 0a 0a 23 69 66 6e 64 65 66 20 4d  ];.};..#ifndef M
396b0 41 58 0a 23 20 64 65 66 69 6e 65 20 4d 41 58 28  AX.# define MAX(
396b1 78 2c 79 29 20 28 28 78 29 20 3c 20 28 79 29 20  x,y) ((x) < (y) 
396b2 3f 20 28 79 29 20 3a 20 28 78 29 29 0a 23 65 6e  ? (y) : (x)).#en
396b3 64 69 66 0a 23 69 66 6e 64 65 66 20 4d 49 4e 0a  dif.#ifndef MIN.
396b4 23 20 64 65 66 69 6e 65 20 4d 49 4e 28 78 2c 79  # define MIN(x,y
396b5 29 20 28 28 78 29 20 3e 20 28 79 29 20 3f 20 28  ) ((x) > (y) ? (
396b6 79 29 20 3a 20 28 78 29 29 0a 23 65 6e 64 69 66  y) : (x)).#endif
396b7 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e  ../*.** Function
396b8 73 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a 65  s to deserialize
396b9 20 61 20 31 36 20 62 69 74 20 69 6e 74 65 67 65   a 16 bit intege
396ba 72 2c 20 33 32 20 62 69 74 20 72 65 61 6c 20 6e  r, 32 bit real n
396bb 75 6d 62 65 72 20 61 6e 64 0a 2a 2a 20 36 34 20  umber and.** 64 
396bc 62 69 74 20 69 6e 74 65 67 65 72 2e 20 54 68 65  bit integer. The
396bd 20 64 65 73 65 72 69 61 6c 69 7a 65 64 20 76 61   deserialized va
396be 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  lue is returned.
396bf 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
396c0 65 61 64 49 6e 74 31 36 28 75 38 20 2a 70 29 7b  eadInt16(u8 *p){
396c1 0a 20 20 72 65 74 75 72 6e 20 28 70 5b 30 5d 3c  .  return (p[0]<
396c2 3c 38 29 20 2b 20 70 5b 31 5d 3b 0a 7d 0a 73 74  <8) + p[1];.}.st
396c3 61 74 69 63 20 76 6f 69 64 20 72 65 61 64 43 6f  atic void readCo
396c4 6f 72 64 28 75 38 20 2a 70 2c 20 52 74 72 65 65  ord(u8 *p, Rtree
396c5 43 6f 6f 72 64 20 2a 70 43 6f 6f 72 64 29 7b 0a  Coord *pCoord){.
396c6 20 20 75 33 32 20 69 20 3d 20 28 0a 20 20 20 20    u32 i = (.    
396c7 28 28 28 75 33 32 29 70 5b 30 5d 29 20 3c 3c 20  (((u32)p[0]) << 
396c8 32 34 29 20 2b 20 0a 20 20 20 20 28 28 28 75 33  24) + .    (((u3
396c9 32 29 70 5b 31 5d 29 20 3c 3c 20 31 36 29 20 2b  2)p[1]) << 16) +
396ca 20 0a 20 20 20 20 28 28 28 75 33 32 29 70 5b 32   .    (((u32)p[2
396cb 5d 29 20 3c 3c 20 20 38 29 20 2b 20 0a 20 20 20  ]) <<  8) + .   
396cc 20 28 28 28 75 33 32 29 70 5b 33 5d 29 20 3c 3c   (((u32)p[3]) <<
396cd 20 20 30 29 0a 20 20 29 3b 0a 20 20 2a 28 75 33    0).  );.  *(u3
396ce 32 20 2a 29 70 43 6f 6f 72 64 20 3d 20 69 3b 0a  2 *)pCoord = i;.
396cf 7d 0a 73 74 61 74 69 63 20 69 36 34 20 72 65 61  }.static i64 rea
396d0 64 49 6e 74 36 34 28 75 38 20 2a 70 29 7b 0a 20  dInt64(u8 *p){. 
396d1 20 72 65 74 75 72 6e 20 28 0a 20 20 20 20 28 28   return (.    ((
396d2 28 69 36 34 29 70 5b 30 5d 29 20 3c 3c 20 35 36  (i64)p[0]) << 56
396d3 29 20 2b 20 0a 20 20 20 20 28 28 28 69 36 34 29  ) + .    (((i64)
396d4 70 5b 31 5d 29 20 3c 3c 20 34 38 29 20 2b 20 0a  p[1]) << 48) + .
396d5 20 20 20 20 28 28 28 69 36 34 29 70 5b 32 5d 29      (((i64)p[2])
396d6 20 3c 3c 20 34 30 29 20 2b 20 0a 20 20 20 20 28   << 40) + .    (
396d7 28 28 69 36 34 29 70 5b 33 5d 29 20 3c 3c 20 33  ((i64)p[3]) << 3
396d8 32 29 20 2b 20 0a 20 20 20 20 28 28 28 69 36 34  2) + .    (((i64
396d9 29 70 5b 34 5d 29 20 3c 3c 20 32 34 29 20 2b 20  )p[4]) << 24) + 
396da 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 35 5d  .    (((i64)p[5]
396db 29 20 3c 3c 20 31 36 29 20 2b 20 0a 20 20 20 20  ) << 16) + .    
396dc 28 28 28 69 36 34 29 70 5b 36 5d 29 20 3c 3c 20  (((i64)p[6]) << 
396dd 20 38 29 20 2b 20 0a 20 20 20 20 28 28 28 69 36   8) + .    (((i6
396de 34 29 70 5b 37 5d 29 20 3c 3c 20 20 30 29 0a 20  4)p[7]) <<  0). 
396df 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e   );.}../*.** Fun
396e0 63 74 69 6f 6e 73 20 74 6f 20 73 65 72 69 61 6c  ctions to serial
396e1 69 7a 65 20 61 20 31 36 20 62 69 74 20 69 6e 74  ize a 16 bit int
396e2 65 67 65 72 2c 20 33 32 20 62 69 74 20 72 65 61  eger, 32 bit rea
396e3 6c 20 6e 75 6d 62 65 72 20 61 6e 64 0a 2a 2a 20  l number and.** 
396e4 36 34 20 62 69 74 20 69 6e 74 65 67 65 72 2e 20  64 bit integer. 
396e5 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  The value return
396e6 65 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ed is the number
396e7 20 6f 66 20 62 79 74 65 73 20 77 72 69 74 74 65   of bytes writte
396e8 6e 0a 2a 2a 20 74 6f 20 74 68 65 20 61 72 67 75  n.** to the argu
396e9 6d 65 6e 74 20 62 75 66 66 65 72 20 28 61 6c 77  ment buffer (alw
396ea 61 79 73 20 32 2c 20 34 20 61 6e 64 20 38 20 72  ays 2, 4 and 8 r
396eb 65 73 70 65 63 74 69 76 65 6c 79 29 2e 0a 2a 2f  espectively)..*/
396ec 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72 69 74  .static int writ
396ed 65 49 6e 74 31 36 28 75 38 20 2a 70 2c 20 69 6e  eInt16(u8 *p, in
396ee 74 20 69 29 7b 0a 20 20 70 5b 30 5d 20 3d 20 28  t i){.  p[0] = (
396ef 69 3e 3e 20 38 29 26 30 78 46 46 3b 0a 20 20 70  i>> 8)&0xFF;.  p
396f0 5b 31 5d 20 3d 20 28 69 3e 3e 20 30 29 26 30 78  [1] = (i>> 0)&0x
396f1 46 46 3b 0a 20 20 72 65 74 75 72 6e 20 32 3b 0a  FF;.  return 2;.
396f2 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72 69  }.static int wri
396f3 74 65 43 6f 6f 72 64 28 75 38 20 2a 70 2c 20 52  teCoord(u8 *p, R
396f4 74 72 65 65 43 6f 6f 72 64 20 2a 70 43 6f 6f 72  treeCoord *pCoor
396f5 64 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20 20 61  d){.  u32 i;.  a
396f6 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 52 74  ssert( sizeof(Rt
396f7 72 65 65 43 6f 6f 72 64 29 3d 3d 34 20 29 3b 0a  reeCoord)==4 );.
396f8 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66    assert( sizeof
396f9 28 75 33 32 29 3d 3d 34 20 29 3b 0a 20 20 69 20  (u32)==4 );.  i 
396fa 3d 20 2a 28 75 33 32 20 2a 29 70 43 6f 6f 72 64  = *(u32 *)pCoord
396fb 3b 0a 20 20 70 5b 30 5d 20 3d 20 28 69 3e 3e 32  ;.  p[0] = (i>>2
396fc 34 29 26 30 78 46 46 3b 0a 20 20 70 5b 31 5d 20  4)&0xFF;.  p[1] 
396fd 3d 20 28 69 3e 3e 31 36 29 26 30 78 46 46 3b 0a  = (i>>16)&0xFF;.
396fe 20 20 70 5b 32 5d 20 3d 20 28 69 3e 3e 20 38 29    p[2] = (i>> 8)
396ff 26 30 78 46 46 3b 0a 20 20 70 5b 33 5d 20 3d 20  &0xFF;.  p[3] = 
39700 28 69 3e 3e 20 30 29 26 30 78 46 46 3b 0a 20 20  (i>> 0)&0xFF;.  
39701 72 65 74 75 72 6e 20 34 3b 0a 7d 0a 73 74 61 74  return 4;.}.stat
39702 69 63 20 69 6e 74 20 77 72 69 74 65 49 6e 74 36  ic int writeInt6
39703 34 28 75 38 20 2a 70 2c 20 69 36 34 20 69 29 7b  4(u8 *p, i64 i){
39704 0a 20 20 70 5b 30 5d 20 3d 20 28 69 3e 3e 35 36  .  p[0] = (i>>56
39705 29 26 30 78 46 46 3b 0a 20 20 70 5b 31 5d 20 3d  )&0xFF;.  p[1] =
39706 20 28 69 3e 3e 34 38 29 26 30 78 46 46 3b 0a 20   (i>>48)&0xFF;. 
39707 20 70 5b 32 5d 20 3d 20 28 69 3e 3e 34 30 29 26   p[2] = (i>>40)&
39708 30 78 46 46 3b 0a 20 20 70 5b 33 5d 20 3d 20 28  0xFF;.  p[3] = (
39709 69 3e 3e 33 32 29 26 30 78 46 46 3b 0a 20 20 70  i>>32)&0xFF;.  p
3970a 5b 34 5d 20 3d 20 28 69 3e 3e 32 34 29 26 30 78  [4] = (i>>24)&0x
3970b 46 46 3b 0a 20 20 70 5b 35 5d 20 3d 20 28 69 3e  FF;.  p[5] = (i>
3970c 3e 31 36 29 26 30 78 46 46 3b 0a 20 20 70 5b 36  >16)&0xFF;.  p[6
3970d 5d 20 3d 20 28 69 3e 3e 20 38 29 26 30 78 46 46  ] = (i>> 8)&0xFF
3970e 3b 0a 20 20 70 5b 37 5d 20 3d 20 28 69 3e 3e 20  ;.  p[7] = (i>> 
3970f 30 29 26 30 78 46 46 3b 0a 20 20 72 65 74 75 72  0)&0xFF;.  retur
39710 6e 20 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  n 8;.}../*.** In
39711 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66 65  crement the refe
39712 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66 20 6e  rence count of n
39713 6f 64 65 20 70 2e 0a 2a 2f 0a 73 74 61 74 69 63  ode p..*/.static
39714 20 76 6f 69 64 20 6e 6f 64 65 52 65 66 65 72 65   void nodeRefere
39715 6e 63 65 28 52 74 72 65 65 4e 6f 64 65 20 2a 70  nce(RtreeNode *p
39716 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20  ){.  if( p ){.  
39717 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 7d    p->nRef++;.  }
39718 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  .}../*.** Clear 
39719 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 6e  the content of n
3971a 6f 64 65 20 70 20 28 73 65 74 20 61 6c 6c 20 62  ode p (set all b
3971b 79 74 65 73 20 74 6f 20 30 78 30 30 29 2e 0a 2a  ytes to 0x00)..*
3971c 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f  /.static void no
3971d 64 65 5a 65 72 6f 28 52 74 72 65 65 20 2a 70 52  deZero(Rtree *pR
3971e 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20  tree, RtreeNode 
3971f 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a  *p){.  if( p ){.
39720 20 20 20 20 6d 65 6d 73 65 74 28 26 70 2d 3e 7a      memset(&p->z
39721 44 61 74 61 5b 32 5d 2c 20 30 2c 20 70 52 74 72  Data[2], 0, pRtr
39722 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 2d 32 29  ee->iNodeSize-2)
39723 3b 0a 20 20 20 20 70 2d 3e 69 73 44 69 72 74 79  ;.    p->isDirty
39724 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a   = 1;.  }.}../*.
39725 2a 2a 20 47 69 76 65 6e 20 61 20 6e 6f 64 65 20  ** Given a node 
39726 6e 75 6d 62 65 72 20 69 4e 6f 64 65 2c 20 72 65  number iNode, re
39727 74 75 72 6e 20 74 68 65 20 63 6f 72 72 65 73 70  turn the corresp
39728 6f 6e 64 69 6e 67 20 6b 65 79 20 74 6f 20 75 73  onding key to us
39729 65 0a 2a 2a 20 69 6e 20 74 68 65 20 52 74 72 65  e.** in the Rtre
3972a 65 2e 61 48 61 73 68 20 74 61 62 6c 65 2e 0a 2a  e.aHash table..*
3972b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 64  /.static int nod
3972c 65 48 61 73 68 28 69 36 34 20 69 4e 6f 64 65 29  eHash(i64 iNode)
3972d 7b 0a 20 20 72 65 74 75 72 6e 20 28 0a 20 20 20  {.  return (.   
3972e 20 28 69 4e 6f 64 65 3e 3e 35 36 29 20 5e 20 28   (iNode>>56) ^ (
3972f 69 4e 6f 64 65 3e 3e 34 38 29 20 5e 20 28 69 4e  iNode>>48) ^ (iN
39730 6f 64 65 3e 3e 34 30 29 20 5e 20 28 69 4e 6f 64  ode>>40) ^ (iNod
39731 65 3e 3e 33 32 29 20 5e 20 0a 20 20 20 20 28 69  e>>32) ^ .    (i
39732 4e 6f 64 65 3e 3e 32 34 29 20 5e 20 28 69 4e 6f  Node>>24) ^ (iNo
39733 64 65 3e 3e 31 36 29 20 5e 20 28 69 4e 6f 64 65  de>>16) ^ (iNode
39734 3e 3e 20 38 29 20 5e 20 28 69 4e 6f 64 65 3e 3e  >> 8) ^ (iNode>>
39735 20 30 29 0a 20 20 29 20 25 20 48 41 53 48 53 49   0).  ) % HASHSI
39736 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 61  ZE;.}../*.** Sea
39737 72 63 68 20 74 68 65 20 6e 6f 64 65 20 68 61 73  rch the node has
39738 68 20 74 61 62 6c 65 20 66 6f 72 20 6e 6f 64 65  h table for node
39739 20 69 4e 6f 64 65 2e 20 49 66 20 66 6f 75 6e 64   iNode. If found
3973a 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  , return a point
3973b 65 72 0a 2a 2a 20 74 6f 20 69 74 2e 20 4f 74 68  er.** to it. Oth
3973c 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 20 30  erwise, return 0
3973d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52 74 72 65  ..*/.static Rtre
3973e 65 4e 6f 64 65 20 2a 6e 6f 64 65 48 61 73 68 4c  eNode *nodeHashL
3973f 6f 6f 6b 75 70 28 52 74 72 65 65 20 2a 70 52 74  ookup(Rtree *pRt
39740 72 65 65 2c 20 69 36 34 20 69 4e 6f 64 65 29 7b  ree, i64 iNode){
39741 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 3b  .  RtreeNode *p;
39742 0a 20 20 61 73 73 65 72 74 28 20 69 4e 6f 64 65  .  assert( iNode
39743 21 3d 30 20 29 3b 0a 20 20 66 6f 72 28 70 3d 70  !=0 );.  for(p=p
39744 52 74 72 65 65 2d 3e 61 48 61 73 68 5b 6e 6f 64  Rtree->aHash[nod
39745 65 48 61 73 68 28 69 4e 6f 64 65 29 5d 3b 20 70  eHash(iNode)]; p
39746 20 26 26 20 70 2d 3e 69 4e 6f 64 65 21 3d 69 4e   && p->iNode!=iN
39747 6f 64 65 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29  ode; p=p->pNext)
39748 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  ;.  return p;.}.
39749 0a 2f 2a 0a 2a 2a 20 41 64 64 20 6e 6f 64 65 20  ./*.** Add node 
3974a 70 4e 6f 64 65 20 74 6f 20 74 68 65 20 6e 6f 64  pNode to the nod
3974b 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f  e hash table..*/
3974c 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f 64  .static void nod
3974d 65 48 61 73 68 49 6e 73 65 72 74 28 52 74 72 65  eHashInsert(Rtre
3974e 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65  e *pRtree, Rtree
3974f 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20  Node *pNode){.  
39750 69 66 28 20 70 4e 6f 64 65 20 29 7b 0a 20 20 20  if( pNode ){.   
39751 20 69 6e 74 20 69 48 61 73 68 3b 0a 20 20 20 20   int iHash;.    
39752 61 73 73 65 72 74 28 20 70 4e 6f 64 65 2d 3e 70  assert( pNode->p
39753 4e 65 78 74 3d 3d 30 20 29 3b 0a 20 20 20 20 69  Next==0 );.    i
39754 48 61 73 68 20 3d 20 6e 6f 64 65 48 61 73 68 28  Hash = nodeHash(
39755 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20  pNode->iNode);. 
39756 20 20 20 70 4e 6f 64 65 2d 3e 70 4e 65 78 74 20     pNode->pNext 
39757 3d 20 70 52 74 72 65 65 2d 3e 61 48 61 73 68 5b  = pRtree->aHash[
39758 69 48 61 73 68 5d 3b 0a 20 20 20 20 70 52 74 72  iHash];.    pRtr
39759 65 65 2d 3e 61 48 61 73 68 5b 69 48 61 73 68 5d  ee->aHash[iHash]
3975a 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 7d 0a 7d 0a   = pNode;.  }.}.
3975b 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 6e 6f  ./*.** Remove no
3975c 64 65 20 70 4e 6f 64 65 20 66 72 6f 6d 20 74 68  de pNode from th
3975d 65 20 6e 6f 64 65 20 68 61 73 68 20 74 61 62 6c  e node hash tabl
3975e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
3975f 64 20 6e 6f 64 65 48 61 73 68 44 65 6c 65 74 65  d nodeHashDelete
39760 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20  (Rtree *pRtree, 
39761 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
39762 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  ){.  RtreeNode *
39763 2a 70 70 3b 0a 20 20 69 66 28 20 70 4e 6f 64 65  *pp;.  if( pNode
39764 2d 3e 69 4e 6f 64 65 21 3d 30 20 29 7b 0a 20 20  ->iNode!=0 ){.  
39765 20 20 70 70 20 3d 20 26 70 52 74 72 65 65 2d 3e    pp = &pRtree->
39766 61 48 61 73 68 5b 6e 6f 64 65 48 61 73 68 28 70  aHash[nodeHash(p
39767 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 5d 3b 0a 20  Node->iNode)];. 
39768 20 20 20 66 6f 72 28 20 3b 20 28 2a 70 70 29 21     for( ; (*pp)!
39769 3d 70 4e 6f 64 65 3b 20 70 70 20 3d 20 26 28 2a  =pNode; pp = &(*
3976a 70 70 29 2d 3e 70 4e 65 78 74 29 7b 20 61 73 73  pp)->pNext){ ass
3976b 65 72 74 28 2a 70 70 29 3b 20 7d 0a 20 20 20 20  ert(*pp); }.    
3976c 2a 70 70 20 3d 20 70 4e 6f 64 65 2d 3e 70 4e 65  *pp = pNode->pNe
3976d 78 74 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 70  xt;.    pNode->p
3976e 4e 65 78 74 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a  Next = 0;.  }.}.
3976f 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  ./*.** Allocate 
39770 61 6e 64 20 72 65 74 75 72 6e 20 6e 65 77 20 72  and return new r
39771 2d 74 72 65 65 20 6e 6f 64 65 2e 20 49 6e 69 74  -tree node. Init
39772 69 61 6c 6c 79 2c 20 28 52 74 72 65 65 4e 6f 64  ially, (RtreeNod
39773 65 2e 69 4e 6f 64 65 3d 3d 30 29 2c 0a 2a 2a 20  e.iNode==0),.** 
39774 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20  indicating that 
39775 6e 6f 64 65 20 68 61 73 20 6e 6f 74 20 79 65 74  node has not yet
39776 20 62 65 65 6e 20 61 73 73 69 67 6e 65 64 20 61   been assigned a
39777 20 6e 6f 64 65 20 6e 75 6d 62 65 72 2e 20 49 74   node number. It
39778 20 69 73 0a 2a 2a 20 61 73 73 69 67 6e 65 64 20   is.** assigned 
39779 61 20 6e 6f 64 65 20 6e 75 6d 62 65 72 20 77 68  a node number wh
3977a 65 6e 20 6e 6f 64 65 57 72 69 74 65 28 29 20 69  en nodeWrite() i
3977b 73 20 63 61 6c 6c 65 64 20 74 6f 20 77 72 69 74  s called to writ
3977c 65 20 74 68 65 0a 2a 2a 20 6e 6f 64 65 20 63 6f  e the.** node co
3977d 6e 74 65 6e 74 73 20 6f 75 74 20 74 6f 20 74 68  ntents out to th
3977e 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 73  e database..*/.s
3977f 74 61 74 69 63 20 52 74 72 65 65 4e 6f 64 65 20  tatic RtreeNode 
39780 2a 6e 6f 64 65 4e 65 77 28 52 74 72 65 65 20 2a  *nodeNew(Rtree *
39781 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64  pRtree, RtreeNod
39782 65 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e 74 20  e *pParent, int 
39783 7a 65 72 6f 29 7b 0a 20 20 52 74 72 65 65 4e 6f  zero){.  RtreeNo
39784 64 65 20 2a 70 4e 6f 64 65 3b 0a 20 20 70 4e 6f  de *pNode;.  pNo
39785 64 65 20 3d 20 28 52 74 72 65 65 4e 6f 64 65 20  de = (RtreeNode 
39786 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
39787 28 73 69 7a 65 6f 66 28 52 74 72 65 65 4e 6f 64  (sizeof(RtreeNod
39788 65 29 20 2b 20 70 52 74 72 65 65 2d 3e 69 4e 6f  e) + pRtree->iNo
39789 64 65 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 70  deSize);.  if( p
3978a 4e 6f 64 65 20 29 7b 0a 20 20 20 20 6d 65 6d 73  Node ){.    mems
3978b 65 74 28 70 4e 6f 64 65 2c 20 30 2c 20 73 69 7a  et(pNode, 0, siz
3978c 65 6f 66 28 52 74 72 65 65 4e 6f 64 65 29 20 2b  eof(RtreeNode) +
3978d 20 28 7a 65 72 6f 3f 70 52 74 72 65 65 2d 3e 69   (zero?pRtree->i
3978e 4e 6f 64 65 53 69 7a 65 3a 30 29 29 3b 0a 20 20  NodeSize:0));.  
3978f 20 20 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 20 3d    pNode->zData =
39790 20 28 75 38 20 2a 29 26 70 4e 6f 64 65 5b 31 5d   (u8 *)&pNode[1]
39791 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 6e 52 65  ;.    pNode->nRe
39792 66 20 3d 20 31 3b 0a 20 20 20 20 70 4e 6f 64 65  f = 1;.    pNode
39793 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50 61 72  ->pParent = pPar
39794 65 6e 74 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e  ent;.    pNode->
39795 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 20 20 20  isDirty = 1;.   
39796 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28 70   nodeReference(p
39797 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 20 20 72  Parent);.  }.  r
39798 65 74 75 72 6e 20 70 4e 6f 64 65 3b 0a 7d 0a 0a  eturn pNode;.}..
39799 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 61 20 72  /*.** Obtain a r
3979a 65 66 65 72 65 6e 63 65 20 74 6f 20 61 6e 20 72  eference to an r
3979b 2d 74 72 65 65 20 6e 6f 64 65 2e 0a 2a 2f 0a 73  -tree node..*/.s
3979c 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64 65 41 63  tatic int.nodeAc
3979d 71 75 69 72 65 28 0a 20 20 52 74 72 65 65 20 2a  quire(.  Rtree *
3979e 70 52 74 72 65 65 2c 20 20 20 20 20 20 20 20 20  pRtree,         
3979f 20 20 20 20 2f 2a 20 52 2d 74 72 65 65 20 73 74      /* R-tree st
397a0 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 36 34  ructure */.  i64
397a1 20 69 4e 6f 64 65 2c 20 20 20 20 20 20 20 20 20   iNode,         
397a2 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 64 65 20          /* Node 
397a3 6e 75 6d 62 65 72 20 74 6f 20 6c 6f 61 64 20 2a  number to load *
397a4 2f 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  /.  RtreeNode *p
397a5 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20 2f  Parent,        /
397a6 2a 20 45 69 74 68 65 72 20 74 68 65 20 70 61 72  * Either the par
397a7 65 6e 74 20 6e 6f 64 65 20 6f 72 20 4e 55 4c 4c  ent node or NULL
397a8 20 2a 2f 0a 20 20 52 74 72 65 65 4e 6f 64 65 20   */.  RtreeNode 
397a9 2a 2a 70 70 4e 6f 64 65 20 20 20 20 20 20 20 20  **ppNode        
397aa 20 2f 2a 20 4f 55 54 3a 20 41 63 71 75 69 72 65   /* OUT: Acquire
397ab 64 20 6e 6f 64 65 20 2a 2f 0a 29 7b 0a 20 20 69  d node */.){.  i
397ac 6e 74 20 72 63 3b 0a 20 20 52 74 72 65 65 4e 6f  nt rc;.  RtreeNo
397ad 64 65 20 2a 70 4e 6f 64 65 3b 0a 0a 20 20 2f 2a  de *pNode;..  /*
397ae 20 43 68 65 63 6b 20 69 66 20 74 68 65 20 72 65   Check if the re
397af 71 75 65 73 74 65 64 20 6e 6f 64 65 20 69 73 20  quested node is 
397b0 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 68  already in the h
397b1 61 73 68 20 74 61 62 6c 65 2e 20 49 66 20 73 6f  ash table. If so
397b2 2c 0a 20 20 2a 2a 20 69 6e 63 72 65 61 73 65 20  ,.  ** increase 
397b3 69 74 73 20 72 65 66 65 72 65 6e 63 65 20 63 6f  its reference co
397b4 75 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 20 69  unt and return i
397b5 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70  t..  */.  if( (p
397b6 4e 6f 64 65 20 3d 20 6e 6f 64 65 48 61 73 68 4c  Node = nodeHashL
397b7 6f 6f 6b 75 70 28 70 52 74 72 65 65 2c 20 69 4e  ookup(pRtree, iN
397b8 6f 64 65 29 29 20 29 7b 0a 20 20 20 20 61 73 73  ode)) ){.    ass
397b9 65 72 74 28 20 21 70 50 61 72 65 6e 74 20 7c 7c  ert( !pParent ||
397ba 20 21 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74   !pNode->pParent
397bb 20 7c 7c 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65   || pNode->pPare
397bc 6e 74 3d 3d 70 50 61 72 65 6e 74 20 29 3b 0a 20  nt==pParent );. 
397bd 20 20 20 69 66 28 20 70 50 61 72 65 6e 74 20 26     if( pParent &
397be 26 20 21 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e  & !pNode->pParen
397bf 74 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64 65 52  t ){.      nodeR
397c0 65 66 65 72 65 6e 63 65 28 70 50 61 72 65 6e 74  eference(pParent
397c1 29 3b 0a 20 20 20 20 20 20 70 4e 6f 64 65 2d 3e  );.      pNode->
397c2 70 50 61 72 65 6e 74 20 3d 20 70 50 61 72 65 6e  pParent = pParen
397c3 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 6f  t;.    }.    pNo
397c4 64 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20  de->nRef++;.    
397c5 2a 70 70 4e 6f 64 65 20 3d 20 70 4e 6f 64 65 3b  *ppNode = pNode;
397c6 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
397c7 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 70 4e  TE_OK;.  }..  pN
397c8 6f 64 65 20 3d 20 28 52 74 72 65 65 4e 6f 64 65  ode = (RtreeNode
397c9 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   *)sqlite3_mallo
397ca 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65 4e 6f  c(sizeof(RtreeNo
397cb 64 65 29 20 2b 20 70 52 74 72 65 65 2d 3e 69 4e  de) + pRtree->iN
397cc 6f 64 65 53 69 7a 65 29 3b 0a 20 20 69 66 28 20  odeSize);.  if( 
397cd 21 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20 2a 70  !pNode ){.    *p
397ce 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20 72  pNode = 0;.    r
397cf 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
397d0 45 4d 3b 0a 20 20 7d 0a 20 20 70 4e 6f 64 65 2d  EM;.  }.  pNode-
397d1 3e 70 50 61 72 65 6e 74 20 3d 20 70 50 61 72 65  >pParent = pPare
397d2 6e 74 3b 0a 20 20 70 4e 6f 64 65 2d 3e 7a 44 61  nt;.  pNode->zDa
397d3 74 61 20 3d 20 28 75 38 20 2a 29 26 70 4e 6f 64  ta = (u8 *)&pNod
397d4 65 5b 31 5d 3b 0a 20 20 70 4e 6f 64 65 2d 3e 6e  e[1];.  pNode->n
397d5 52 65 66 20 3d 20 31 3b 0a 20 20 70 4e 6f 64 65  Ref = 1;.  pNode
397d6 2d 3e 69 4e 6f 64 65 20 3d 20 69 4e 6f 64 65 3b  ->iNode = iNode;
397d7 0a 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74  .  pNode->isDirt
397d8 79 20 3d 20 30 3b 0a 20 20 70 4e 6f 64 65 2d 3e  y = 0;.  pNode->
397d9 70 4e 65 78 74 20 3d 20 30 3b 0a 0a 20 20 73 71  pNext = 0;..  sq
397da 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
397db 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f  (pRtree->pReadNo
397dc 64 65 2c 20 31 2c 20 69 4e 6f 64 65 29 3b 0a 20  de, 1, iNode);. 
397dd 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74   rc = sqlite3_st
397de 65 70 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64  ep(pRtree->pRead
397df 4e 6f 64 65 29 3b 0a 20 20 69 66 28 20 72 63 3d  Node);.  if( rc=
397e0 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
397e1 20 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 42 6c     const u8 *zBl
397e2 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ob = sqlite3_col
397e3 75 6d 6e 5f 62 6c 6f 62 28 70 52 74 72 65 65 2d  umn_blob(pRtree-
397e4 3e 70 52 65 61 64 4e 6f 64 65 2c 20 30 29 3b 0a  >pReadNode, 0);.
397e5 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 6f 64 65      memcpy(pNode
397e6 2d 3e 7a 44 61 74 61 2c 20 7a 42 6c 6f 62 2c 20  ->zData, zBlob, 
397e7 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a  pRtree->iNodeSiz
397e8 65 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65 66 65  e);.    nodeRefe
397e9 72 65 6e 63 65 28 70 50 61 72 65 6e 74 29 3b 0a  rence(pParent);.
397ea 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
397eb 69 74 65 33 5f 66 72 65 65 28 70 4e 6f 64 65 29  ite3_free(pNode)
397ec 3b 0a 20 20 20 20 70 4e 6f 64 65 20 3d 20 30 3b  ;.    pNode = 0;
397ed 0a 20 20 7d 0a 0a 20 20 2a 70 70 4e 6f 64 65 20  .  }..  *ppNode 
397ee 3d 20 70 4e 6f 64 65 3b 0a 20 20 72 63 20 3d 20  = pNode;.  rc = 
397ef 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52  sqlite3_reset(pR
397f0 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65 29  tree->pReadNode)
397f1 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
397f2 49 54 45 5f 4f 4b 20 26 26 20 69 4e 6f 64 65 3d  ITE_OK && iNode=
397f3 3d 31 20 29 7b 0a 20 20 20 20 70 52 74 72 65 65  =1 ){.    pRtree
397f4 2d 3e 69 44 65 70 74 68 20 3d 20 72 65 61 64 49  ->iDepth = readI
397f5 6e 74 31 36 28 70 4e 6f 64 65 2d 3e 7a 44 61 74  nt16(pNode->zDat
397f6 61 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  a);.  }..  asser
397f7 74 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  t( (rc==SQLITE_O
397f8 4b 20 26 26 20 70 4e 6f 64 65 29 20 7c 7c 20 28  K && pNode) || (
397f9 70 4e 6f 64 65 3d 3d 30 20 26 26 20 72 63 21 3d  pNode==0 && rc!=
397fa 53 51 4c 49 54 45 5f 4f 4b 29 20 29 3b 0a 20 20  SQLITE_OK) );.  
397fb 6e 6f 64 65 48 61 73 68 49 6e 73 65 72 74 28 70  nodeHashInsert(p
397fc 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 0a  Rtree, pNode);..
397fd 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
397fe 2f 2a 0a 2a 2a 20 4f 76 65 72 77 72 69 74 65 20  /*.** Overwrite 
397ff 63 65 6c 6c 20 69 43 65 6c 6c 20 6f 66 20 6e 6f  cell iCell of no
39800 64 65 20 70 4e 6f 64 65 20 77 69 74 68 20 74 68  de pNode with th
39801 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 43  e contents of pC
39802 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ell..*/.static v
39803 6f 69 64 20 6e 6f 64 65 4f 76 65 72 77 72 69 74  oid nodeOverwrit
39804 65 43 65 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a  eCell(.  Rtree *
39805 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65  pRtree, .  Rtree
39806 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 20 0a 20  Node *pNode,  . 
39807 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c   RtreeCell *pCel
39808 6c 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c 6c 0a  l, .  int iCell.
39809 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 75  ){.  int ii;.  u
3980a 38 20 2a 70 20 3d 20 26 70 4e 6f 64 65 2d 3e 7a  8 *p = &pNode->z
3980b 44 61 74 61 5b 34 20 2b 20 70 52 74 72 65 65 2d  Data[4 + pRtree-
3980c 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 69  >nBytesPerCell*i
3980d 43 65 6c 6c 5d 3b 0a 20 20 70 20 2b 3d 20 77 72  Cell];.  p += wr
3980e 69 74 65 49 6e 74 36 34 28 70 2c 20 70 43 65 6c  iteInt64(p, pCel
3980f 6c 2d 3e 69 52 6f 77 69 64 29 3b 0a 20 20 66 6f  l->iRowid);.  fo
39810 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72  r(ii=0; ii<(pRtr
39811 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b  ee->nDim*2); ii+
39812 2b 29 7b 0a 20 20 20 20 70 20 2b 3d 20 77 72 69  +){.    p += wri
39813 74 65 43 6f 6f 72 64 28 70 2c 20 26 70 43 65 6c  teCoord(p, &pCel
39814 6c 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 29 3b 0a  l->aCoord[ii]);.
39815 20 20 7d 0a 20 20 70 4e 6f 64 65 2d 3e 69 73 44    }.  pNode->isD
39816 69 72 74 79 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a  irty = 1;.}../*.
39817 2a 2a 20 52 65 6d 6f 76 65 20 63 65 6c 6c 20 74  ** Remove cell t
39818 68 65 20 63 65 6c 6c 20 77 69 74 68 20 69 6e 64  he cell with ind
39819 65 78 20 69 43 65 6c 6c 20 66 72 6f 6d 20 6e 6f  ex iCell from no
3981a 64 65 20 70 4e 6f 64 65 2e 0a 2a 2f 0a 73 74 61  de pNode..*/.sta
3981b 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 44 65 6c  tic void nodeDel
3981c 65 74 65 43 65 6c 6c 28 52 74 72 65 65 20 2a 70  eteCell(Rtree *p
3981d 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
3981e 20 2a 70 4e 6f 64 65 2c 20 69 6e 74 20 69 43 65   *pNode, int iCe
3981f 6c 6c 29 7b 0a 20 20 75 38 20 2a 70 44 73 74 20  ll){.  u8 *pDst 
39820 3d 20 26 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b  = &pNode->zData[
39821 34 20 2b 20 70 52 74 72 65 65 2d 3e 6e 42 79 74  4 + pRtree->nByt
39822 65 73 50 65 72 43 65 6c 6c 2a 69 43 65 6c 6c 5d  esPerCell*iCell]
39823 3b 0a 20 20 75 38 20 2a 70 53 72 63 20 3d 20 26  ;.  u8 *pSrc = &
39824 70 44 73 74 5b 70 52 74 72 65 65 2d 3e 6e 42 79  pDst[pRtree->nBy
39825 74 65 73 50 65 72 43 65 6c 6c 5d 3b 0a 20 20 69  tesPerCell];.  i
39826 6e 74 20 6e 42 79 74 65 20 3d 20 28 4e 43 45 4c  nt nByte = (NCEL
39827 4c 28 70 4e 6f 64 65 29 20 2d 20 69 43 65 6c 6c  L(pNode) - iCell
39828 20 2d 20 31 29 20 2a 20 70 52 74 72 65 65 2d 3e   - 1) * pRtree->
39829 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 3b 0a 20  nBytesPerCell;. 
3982a 20 6d 65 6d 6d 6f 76 65 28 70 44 73 74 2c 20 70   memmove(pDst, p
3982b 53 72 63 2c 20 6e 42 79 74 65 29 3b 0a 20 20 77  Src, nByte);.  w
3982c 72 69 74 65 49 6e 74 31 36 28 26 70 4e 6f 64 65  riteInt16(&pNode
3982d 2d 3e 7a 44 61 74 61 5b 32 5d 2c 20 4e 43 45 4c  ->zData[2], NCEL
3982e 4c 28 70 4e 6f 64 65 29 2d 31 29 3b 0a 20 20 70  L(pNode)-1);.  p
3982f 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20  Node->isDirty = 
39830 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65  1;.}../*.** Inse
39831 72 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  rt the contents 
39832 6f 66 20 63 65 6c 6c 20 70 43 65 6c 6c 20 69 6e  of cell pCell in
39833 74 6f 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 49  to node pNode. I
39834 66 20 74 68 65 20 69 6e 73 65 72 74 0a 2a 2a 20  f the insert.** 
39835 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72  is successful, r
39836 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
39837 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  .**.** If there 
39838 69 73 20 6e 6f 74 20 65 6e 6f 75 67 68 20 66 72  is not enough fr
39839 65 65 20 73 70 61 63 65 20 69 6e 20 70 4e 6f 64  ee space in pNod
3983a 65 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  e, return SQLITE
3983b 5f 46 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63  _FULL..*/.static
3983c 20 69 6e 74 0a 6e 6f 64 65 49 6e 73 65 72 74 43   int.nodeInsertC
3983d 65 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a 70 52  ell(.  Rtree *pR
3983e 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f  tree, .  RtreeNo
3983f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 52 74  de *pNode, .  Rt
39840 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 20 0a  reeCell *pCell .
39841 29 7b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20  ){.  int nCell; 
39842 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
39843 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75     /* Current nu
39844 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e  mber of cells in
39845 20 70 4e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20   pNode */.  int 
39846 6e 4d 61 78 43 65 6c 6c 3b 20 20 20 20 20 20 20  nMaxCell;       
39847 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
39848 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63  imum number of c
39849 65 6c 6c 73 20 66 6f 72 20 70 4e 6f 64 65 20 2a  ells for pNode *
3984a 2f 0a 0a 20 20 6e 4d 61 78 43 65 6c 6c 20 3d 20  /..  nMaxCell = 
3984b 28 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69  (pRtree->iNodeSi
3984c 7a 65 2d 34 29 2f 70 52 74 72 65 65 2d 3e 6e 42  ze-4)/pRtree->nB
3984d 79 74 65 73 50 65 72 43 65 6c 6c 3b 0a 20 20 6e  ytesPerCell;.  n
3984e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f  Cell = NCELL(pNo
3984f 64 65 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 6e  de);..  assert(n
39850 43 65 6c 6c 3c 3d 6e 4d 61 78 43 65 6c 6c 29 3b  Cell<=nMaxCell);
39851 0a 0a 20 20 69 66 28 20 6e 43 65 6c 6c 3c 6e 4d  ..  if( nCell<nM
39852 61 78 43 65 6c 6c 20 29 7b 0a 20 20 20 20 6e 6f  axCell ){.    no
39853 64 65 4f 76 65 72 77 72 69 74 65 43 65 6c 6c 28  deOverwriteCell(
39854 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70  pRtree, pNode, p
39855 43 65 6c 6c 2c 20 6e 43 65 6c 6c 29 3b 0a 20 20  Cell, nCell);.  
39856 20 20 77 72 69 74 65 49 6e 74 31 36 28 26 70 4e    writeInt16(&pN
39857 6f 64 65 2d 3e 7a 44 61 74 61 5b 32 5d 2c 20 6e  ode->zData[2], n
39858 43 65 6c 6c 2b 31 29 3b 0a 20 20 20 20 70 4e 6f  Cell+1);.    pNo
39859 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b  de->isDirty = 1;
3985a 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 28  .  }..  return (
3985b 6e 43 65 6c 6c 3d 3d 6e 4d 61 78 43 65 6c 6c 29  nCell==nMaxCell)
3985c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  ;.}../*.** If th
3985d 65 20 6e 6f 64 65 20 69 73 20 64 69 72 74 79 2c  e node is dirty,
3985e 20 77 72 69 74 65 20 69 74 20 6f 75 74 20 74 6f   write it out to
3985f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a   the database..*
39860 2f 0a 73 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64  /.static int.nod
39861 65 57 72 69 74 65 28 52 74 72 65 65 20 2a 70 52  eWrite(Rtree *pR
39862 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20  tree, RtreeNode 
39863 2a 70 4e 6f 64 65 29 7b 0a 20 20 69 6e 74 20 72  *pNode){.  int r
39864 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
39865 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 73 44 69   if( pNode->isDi
39866 72 74 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  rty ){.    sqlit
39867 65 33 5f 73 74 6d 74 20 2a 70 20 3d 20 70 52 74  e3_stmt *p = pRt
39868 72 65 65 2d 3e 70 57 72 69 74 65 4e 6f 64 65 3b  ree->pWriteNode;
39869 0a 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e  .    if( pNode->
3986a 69 4e 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 73  iNode ){.      s
3986b 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
3986c 34 28 70 2c 20 31 2c 20 70 4e 6f 64 65 2d 3e 69  4(p, 1, pNode->i
3986d 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Node);.    }else
3986e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
3986f 62 69 6e 64 5f 6e 75 6c 6c 28 70 2c 20 31 29 3b  bind_null(p, 1);
39870 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
39871 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 70 2c 20  e3_bind_blob(p, 
39872 32 2c 20 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 2c  2, pNode->zData,
39873 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69   pRtree->iNodeSi
39874 7a 65 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  ze, SQLITE_STATI
39875 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  C);.    sqlite3_
39876 73 74 65 70 28 70 29 3b 0a 20 20 20 20 70 4e 6f  step(p);.    pNo
39877 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20 30 3b  de->isDirty = 0;
39878 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
39879 33 5f 72 65 73 65 74 28 70 29 3b 0a 20 20 20 20  3_reset(p);.    
3987a 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  if( pNode->iNode
3987b 3d 3d 30 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  ==0 && rc==SQLIT
3987c 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 4e  E_OK ){.      pN
3987d 6f 64 65 2d 3e 69 4e 6f 64 65 20 3d 20 73 71 6c  ode->iNode = sql
3987e 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74  ite3_last_insert
3987f 5f 72 6f 77 69 64 28 70 52 74 72 65 65 2d 3e 64  _rowid(pRtree->d
39880 62 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 48 61  b);.      nodeHa
39881 73 68 49 6e 73 65 72 74 28 70 52 74 72 65 65 2c  shInsert(pRtree,
39882 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20   pNode);.    }. 
39883 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
39884 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65  }../*.** Release
39885 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20   a reference to 
39886 61 20 6e 6f 64 65 2e 20 49 66 20 74 68 65 20 6e  a node. If the n
39887 6f 64 65 20 69 73 20 64 69 72 74 79 20 61 6e 64  ode is dirty and
39888 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 0a 2a   the reference.*
39889 2a 20 63 6f 75 6e 74 20 64 72 6f 70 73 20 74 6f  * count drops to
3988a 20 7a 65 72 6f 2c 20 74 68 65 20 6e 6f 64 65 20   zero, the node 
3988b 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20  data is written 
3988c 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  to the database.
3988d 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 0a 6e  .*/.static int.n
3988e 6f 64 65 52 65 6c 65 61 73 65 28 52 74 72 65 65  odeRelease(Rtree
3988f 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e   *pRtree, RtreeN
39890 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 69  ode *pNode){.  i
39891 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
39892 4b 3b 0a 20 20 69 66 28 20 70 4e 6f 64 65 20 29  K;.  if( pNode )
39893 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e  {.    assert( pN
39894 6f 64 65 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  ode->nRef>0 );. 
39895 20 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 2d 2d     pNode->nRef--
39896 3b 0a 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d  ;.    if( pNode-
39897 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20  >nRef==0 ){.    
39898 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f    if( pNode->iNo
39899 64 65 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20  de==1 ){.       
3989a 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 20   pRtree->iDepth 
3989b 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  = -1;.      }.  
3989c 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 70      if( pNode->p
3989d 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 20 20  Parent ){.      
3989e 20 20 72 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61    rc = nodeRelea
3989f 73 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  se(pRtree, pNode
398a0 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20  ->pParent);.    
398a1 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63    }.      if( rc
398a2 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
398a3 20 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64 65         rc = node
398a4 57 72 69 74 65 28 70 52 74 72 65 65 2c 20 70 4e  Write(pRtree, pN
398a5 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ode);.      }.  
398a6 20 20 20 20 6e 6f 64 65 48 61 73 68 44 65 6c 65      nodeHashDele
398a7 74 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  te(pRtree, pNode
398a8 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
398a9 5f 66 72 65 65 28 70 4e 6f 64 65 29 3b 0a 20 20  _free(pNode);.  
398aa 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
398ab 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
398ac 74 75 72 6e 20 74 68 65 20 36 34 2d 62 69 74 20  turn the 64-bit 
398ad 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 61 73  integer value as
398ae 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 63 65  sociated with ce
398af 6c 6c 20 69 43 65 6c 6c 20 6f 66 0a 2a 2a 20 6e  ll iCell of.** n
398b0 6f 64 65 20 70 4e 6f 64 65 2e 20 49 66 20 70 4e  ode pNode. If pN
398b1 6f 64 65 20 69 73 20 61 20 6c 65 61 66 20 6e 6f  ode is a leaf no
398b2 64 65 2c 20 74 68 69 73 20 69 73 20 61 20 72 6f  de, this is a ro
398b3 77 69 64 2e 20 49 66 20 69 74 20 69 73 0a 2a 2a  wid. If it is.**
398b4 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64   an internal nod
398b5 65 2c 20 74 68 65 6e 20 74 68 65 20 36 34 2d 62  e, then the 64-b
398b6 69 74 20 69 6e 74 65 67 65 72 20 69 73 20 61 20  it integer is a 
398b7 63 68 69 6c 64 20 70 61 67 65 20 6e 75 6d 62 65  child page numbe
398b8 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 34  r..*/.static i64
398b9 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 0a 20   nodeGetRowid(. 
398ba 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20   Rtree *pRtree, 
398bb 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  .  RtreeNode *pN
398bc 6f 64 65 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c  ode, .  int iCel
398bd 6c 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69  l.){.  assert( i
398be 43 65 6c 6c 3c 4e 43 45 4c 4c 28 70 4e 6f 64 65  Cell<NCELL(pNode
398bf 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65  ) );.  return re
398c0 61 64 49 6e 74 36 34 28 26 70 4e 6f 64 65 2d 3e  adInt64(&pNode->
398c1 7a 44 61 74 61 5b 34 20 2b 20 70 52 74 72 65 65  zData[4 + pRtree
398c2 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 2a  ->nBytesPerCell*
398c3 69 43 65 6c 6c 5d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  iCell]);.}../*.*
398c4 2a 20 52 65 74 75 72 6e 20 63 6f 6f 72 64 69 6e  * Return coordin
398c5 61 74 65 20 69 43 6f 6f 72 64 20 66 72 6f 6d 20  ate iCoord from 
398c6 63 65 6c 6c 20 69 43 65 6c 6c 20 69 6e 20 6e 6f  cell iCell in no
398c7 64 65 20 70 4e 6f 64 65 2e 0a 2a 2f 0a 73 74 61  de pNode..*/.sta
398c8 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 47 65 74  tic void nodeGet
398c9 43 6f 6f 72 64 28 0a 20 20 52 74 72 65 65 20 2a  Coord(.  Rtree *
398ca 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65  pRtree, .  Rtree
398cb 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20  Node *pNode, .  
398cc 69 6e 74 20 69 43 65 6c 6c 2c 0a 20 20 69 6e 74  int iCell,.  int
398cd 20 69 43 6f 6f 72 64 2c 0a 20 20 52 74 72 65 65   iCoord,.  Rtree
398ce 43 6f 6f 72 64 20 2a 70 43 6f 6f 72 64 20 20 20  Coord *pCoord   
398cf 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65          /* Space
398d0 20 74 6f 20 77 72 69 74 65 20 72 65 73 75 6c 74   to write result
398d1 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 72 65 61 64   to */.){.  read
398d2 43 6f 6f 72 64 28 26 70 4e 6f 64 65 2d 3e 7a 44  Coord(&pNode->zD
398d3 61 74 61 5b 31 32 20 2b 20 70 52 74 72 65 65 2d  ata[12 + pRtree-
398d4 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 69  >nBytesPerCell*i
398d5 43 65 6c 6c 20 2b 20 34 2a 69 43 6f 6f 72 64 5d  Cell + 4*iCoord]
398d6 2c 20 70 43 6f 6f 72 64 29 3b 0a 7d 0a 0a 2f 2a  , pCoord);.}../*
398d7 0a 2a 2a 20 44 65 73 65 72 69 61 6c 69 7a 65 20  .** Deserialize 
398d8 63 65 6c 6c 20 69 43 65 6c 6c 20 6f 66 20 6e 6f  cell iCell of no
398d9 64 65 20 70 4e 6f 64 65 2e 20 50 6f 70 75 6c 61  de pNode. Popula
398da 74 65 20 74 68 65 20 73 74 72 75 63 74 75 72 65  te the structure
398db 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62   pointed.** to b
398dc 79 20 70 43 65 6c 6c 20 77 69 74 68 20 74 68 65  y pCell with the
398dd 20 72 65 73 75 6c 74 73 2e 0a 2a 2f 0a 73 74 61   results..*/.sta
398de 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 47 65 74  tic void nodeGet
398df 43 65 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a 70  Cell(.  Rtree *p
398e0 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e  Rtree, .  RtreeN
398e1 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69  ode *pNode, .  i
398e2 6e 74 20 69 43 65 6c 6c 2c 0a 20 20 52 74 72 65  nt iCell,.  Rtre
398e3 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 0a 29 7b 0a  eCell *pCell.){.
398e4 20 20 69 6e 74 20 69 69 3b 0a 20 20 70 43 65 6c    int ii;.  pCel
398e5 6c 2d 3e 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65  l->iRowid = node
398e6 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c  GetRowid(pRtree,
398e7 20 70 4e 6f 64 65 2c 20 69 43 65 6c 6c 29 3b 0a   pNode, iCell);.
398e8 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70    for(ii=0; ii<p
398e9 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 3b 20 69  Rtree->nDim*2; i
398ea 69 2b 2b 29 7b 0a 20 20 20 20 6e 6f 64 65 47 65  i++){.    nodeGe
398eb 74 43 6f 6f 72 64 28 70 52 74 72 65 65 2c 20 70  tCoord(pRtree, p
398ec 4e 6f 64 65 2c 20 69 43 65 6c 6c 2c 20 69 69 2c  Node, iCell, ii,
398ed 20 26 70 43 65 6c 6c 2d 3e 61 43 6f 6f 72 64 5b   &pCell->aCoord[
398ee 69 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a  ii]);.  }.}.../*
398ef 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   Forward declara
398f0 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 66 75 6e  tion for the fun
398f1 63 74 69 6f 6e 20 74 68 61 74 20 64 6f 65 73 20  ction that does 
398f2 74 68 65 20 77 6f 72 6b 20 6f 66 0a 2a 2a 20 74  the work of.** t
398f3 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
398f4 20 6d 6f 64 75 6c 65 20 78 43 72 65 61 74 65 28   module xCreate(
398f5 29 20 61 6e 64 20 78 43 6f 6e 6e 65 63 74 28 29  ) and xConnect()
398f6 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 73 74 61   methods..*/.sta
398f7 74 69 63 20 69 6e 74 20 72 74 72 65 65 49 6e 69  tic int rtreeIni
398f8 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 2c 20  t(.  sqlite3 *, 
398f9 76 6f 69 64 20 2a 2c 20 69 6e 74 2c 20 63 6f 6e  void *, int, con
398fa 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 2c  st char *const*,
398fb 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a   sqlite3_vtab **
398fc 2c 20 63 68 61 72 20 2a 2a 2c 20 69 6e 74 0a 29  , char **, int.)
398fd 3b 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20  ;../* .** Rtree 
398fe 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
398ff 64 75 6c 65 20 78 43 72 65 61 74 65 20 6d 65 74  dule xCreate met
39900 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  hod..*/.static i
39901 6e 74 20 72 74 72 65 65 43 72 65 61 74 65 28 0a  nt rtreeCreate(.
39902 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20    sqlite3 *db,. 
39903 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 69   void *pAux,.  i
39904 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63  nt argc, const c
39905 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c  har *const*argv,
39906 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
39907 2a 2a 70 70 56 74 61 62 2c 0a 20 20 63 68 61 72  **ppVtab,.  char
39908 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20 20 72 65   **pzErr.){.  re
39909 74 75 72 6e 20 72 74 72 65 65 49 6e 69 74 28 64  turn rtreeInit(d
3990a 62 2c 20 70 41 75 78 2c 20 61 72 67 63 2c 20 61  b, pAux, argc, a
3990b 72 67 76 2c 20 70 70 56 74 61 62 2c 20 70 7a 45  rgv, ppVtab, pzE
3990c 72 72 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a  rr, 1);.}../* .*
3990d 2a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20  * Rtree virtual 
3990e 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 43 6f  table module xCo
3990f 6e 6e 65 63 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f  nnect method..*/
39910 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65  .static int rtre
39911 65 43 6f 6e 6e 65 63 74 28 0a 20 20 73 71 6c 69  eConnect(.  sqli
39912 74 65 33 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20  te3 *db,.  void 
39913 2a 70 41 75 78 2c 0a 20 20 69 6e 74 20 61 72 67  *pAux,.  int arg
39914 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  c, const char *c
39915 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 73 71 6c  onst*argv,.  sql
39916 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 74  ite3_vtab **ppVt
39917 61 62 2c 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45  ab,.  char **pzE
39918 72 72 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 72  rr.){.  return r
39919 74 72 65 65 49 6e 69 74 28 64 62 2c 20 70 41 75  treeInit(db, pAu
3991a 78 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 70  x, argc, argv, p
3991b 70 56 74 61 62 2c 20 70 7a 45 72 72 2c 20 30 29  pVtab, pzErr, 0)
3991c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65  ;.}../*.** Incre
3991d 6d 65 6e 74 20 74 68 65 20 72 2d 74 72 65 65 20  ment the r-tree 
3991e 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e  reference count.
3991f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
39920 72 74 72 65 65 52 65 66 65 72 65 6e 63 65 28 52  rtreeReference(R
39921 74 72 65 65 20 2a 70 52 74 72 65 65 29 7b 0a 20  tree *pRtree){. 
39922 20 70 52 74 72 65 65 2d 3e 6e 42 75 73 79 2b 2b   pRtree->nBusy++
39923 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65  ;.}../*.** Decre
39924 6d 65 6e 74 20 74 68 65 20 72 2d 74 72 65 65 20  ment the r-tree 
39925 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e  reference count.
39926 20 57 68 65 6e 20 74 68 65 20 72 65 66 65 72 65   When the refere
39927 6e 63 65 20 63 6f 75 6e 74 20 72 65 61 63 68 65  nce count reache
39928 73 0a 2a 2a 20 7a 65 72 6f 20 74 68 65 20 73 74  s.** zero the st
39929 72 75 63 74 75 72 65 20 69 73 20 64 65 6c 65 74  ructure is delet
3992a 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
3992b 69 64 20 72 74 72 65 65 52 65 6c 65 61 73 65 28  id rtreeRelease(
3992c 52 74 72 65 65 20 2a 70 52 74 72 65 65 29 7b 0a  Rtree *pRtree){.
3992d 20 20 70 52 74 72 65 65 2d 3e 6e 42 75 73 79 2d    pRtree->nBusy-
3992e 2d 3b 0a 20 20 69 66 28 20 70 52 74 72 65 65 2d  -;.  if( pRtree-
3992f 3e 6e 42 75 73 79 3d 3d 30 20 29 7b 0a 20 20 20  >nBusy==0 ){.   
39930 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
39931 65 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e  e(pRtree->pReadN
39932 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ode);.    sqlite
39933 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65  3_finalize(pRtre
39934 65 2d 3e 70 57 72 69 74 65 4e 6f 64 65 29 3b 0a  e->pWriteNode);.
39935 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61      sqlite3_fina
39936 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70 44 65  lize(pRtree->pDe
39937 6c 65 74 65 4e 6f 64 65 29 3b 0a 20 20 20 20 73  leteNode);.    s
39938 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
39939 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77  pRtree->pReadRow
3993a 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  id);.    sqlite3
3993b 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65  _finalize(pRtree
3993c 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29 3b 0a  ->pWriteRowid);.
3993d 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61      sqlite3_fina
3993e 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70 44 65  lize(pRtree->pDe
3993f 6c 65 74 65 52 6f 77 69 64 29 3b 0a 20 20 20 20  leteRowid);.    
39940 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
39941 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 50 61  (pRtree->pReadPa
39942 72 65 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  rent);.    sqlit
39943 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72  e3_finalize(pRtr
39944 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65 6e 74  ee->pWriteParent
39945 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
39946 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e  inalize(pRtree->
39947 70 44 65 6c 65 74 65 50 61 72 65 6e 74 29 3b 0a  pDeleteParent);.
39948 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
39949 28 70 52 74 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a  (pRtree);.  }.}.
3994a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69  ./* .** Rtree vi
3994b 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
3994c 6c 65 20 78 44 69 73 63 6f 6e 6e 65 63 74 20 6d  le xDisconnect m
3994d 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ethod..*/.static
3994e 20 69 6e 74 20 72 74 72 65 65 44 69 73 63 6f 6e   int rtreeDiscon
3994f 6e 65 63 74 28 73 71 6c 69 74 65 33 5f 76 74 61  nect(sqlite3_vta
39950 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 72 74 72  b *pVtab){.  rtr
39951 65 65 52 65 6c 65 61 73 65 28 28 52 74 72 65 65  eeRelease((Rtree
39952 20 2a 29 70 56 74 61 62 29 3b 0a 20 20 72 65 74   *)pVtab);.  ret
39953 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
39954 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76  ../* .** Rtree v
39955 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
39956 75 6c 65 20 78 44 65 73 74 72 6f 79 20 6d 65 74  ule xDestroy met
39957 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  hod..*/.static i
39958 6e 74 20 72 74 72 65 65 44 65 73 74 72 6f 79 28  nt rtreeDestroy(
39959 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
3995a 74 61 62 29 7b 0a 20 20 52 74 72 65 65 20 2a 70  tab){.  Rtree *p
3995b 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a  Rtree = (Rtree *
3995c 29 70 56 74 61 62 3b 0a 20 20 69 6e 74 20 72 63  )pVtab;.  int rc
3995d 3b 0a 20 20 63 68 61 72 20 2a 7a 43 72 65 61 74  ;.  char *zCreat
3995e 65 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  e = sqlite3_mpri
3995f 6e 74 66 28 0a 20 20 20 20 22 44 52 4f 50 20 54  ntf(.    "DROP T
39960 41 42 4c 45 20 27 25 71 27 2e 27 25 71 5f 6e 6f  ABLE '%q'.'%q_no
39961 64 65 27 3b 22 0a 20 20 20 20 22 44 52 4f 50 20  de';".    "DROP 
39962 54 41 42 4c 45 20 27 25 71 27 2e 27 25 71 5f 72  TABLE '%q'.'%q_r
39963 6f 77 69 64 27 3b 22 0a 20 20 20 20 22 44 52 4f  owid';".    "DRO
39964 50 20 54 41 42 4c 45 20 27 25 71 27 2e 27 25 71  P TABLE '%q'.'%q
39965 5f 70 61 72 65 6e 74 27 3b 22 2c 0a 20 20 20 20  _parent';",.    
39966 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74  pRtree->zDb, pRt
39967 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 0a 20 20 20  ree->zName, .   
39968 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52   pRtree->zDb, pR
39969 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20  tree->zName,.   
3996a 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52   pRtree->zDb, pR
3996b 74 72 65 65 2d 3e 7a 4e 61 6d 65 0a 20 20 29 3b  tree->zName.  );
3996c 0a 20 20 69 66 28 20 21 7a 43 72 65 61 74 65 20  .  if( !zCreate 
3996d 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
3996e 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73  TE_NOMEM;.  }els
3996f 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  e{.    rc = sqli
39970 74 65 33 5f 65 78 65 63 28 70 52 74 72 65 65 2d  te3_exec(pRtree-
39971 3e 64 62 2c 20 7a 43 72 65 61 74 65 2c 20 30 2c  >db, zCreate, 0,
39972 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
39973 74 65 33 5f 66 72 65 65 28 7a 43 72 65 61 74 65  te3_free(zCreate
39974 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  );.  }.  if( rc=
39975 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
39976 20 20 72 74 72 65 65 52 65 6c 65 61 73 65 28 70    rtreeRelease(p
39977 52 74 72 65 65 29 3b 0a 20 20 7d 0a 0a 20 20 72  Rtree);.  }..  r
39978 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
39979 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61  .** Rtree virtua
3997a 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
3997b 4f 70 65 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a  Open method..*/.
3997c 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65  static int rtree
3997d 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 74 61  Open(sqlite3_vta
3997e 62 20 2a 70 56 54 61 62 2c 20 73 71 6c 69 74 65  b *pVTab, sqlite
3997f 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 2a  3_vtab_cursor **
39980 70 70 43 75 72 73 6f 72 29 7b 0a 20 20 69 6e 74  ppCursor){.  int
39981 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
39982 45 4d 3b 0a 20 20 52 74 72 65 65 43 75 72 73 6f  EM;.  RtreeCurso
39983 72 20 2a 70 43 73 72 3b 0a 0a 20 20 70 43 73 72  r *pCsr;..  pCsr
39984 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72 20   = (RtreeCursor 
39985 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
39986 28 73 69 7a 65 6f 66 28 52 74 72 65 65 43 75 72  (sizeof(RtreeCur
39987 73 6f 72 29 29 3b 0a 20 20 69 66 28 20 70 43 73  sor));.  if( pCs
39988 72 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28  r ){.    memset(
39989 70 43 73 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28  pCsr, 0, sizeof(
3998a 52 74 72 65 65 43 75 72 73 6f 72 29 29 3b 0a 20  RtreeCursor));. 
3998b 20 20 20 70 43 73 72 2d 3e 62 61 73 65 2e 70 56     pCsr->base.pV
3998c 74 61 62 20 3d 20 70 56 54 61 62 3b 0a 20 20 20  tab = pVTab;.   
3998d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
3998e 0a 20 20 7d 0a 20 20 2a 70 70 43 75 72 73 6f 72  .  }.  *ppCursor
3998f 20 3d 20 28 73 71 6c 69 74 65 33 5f 76 74 61 62   = (sqlite3_vtab
39990 5f 63 75 72 73 6f 72 20 2a 29 70 43 73 72 3b 0a  _cursor *)pCsr;.
39991 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
39992 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69  ./* .** Rtree vi
39993 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
39994 6c 65 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64  le xClose method
39995 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
39996 72 74 72 65 65 43 6c 6f 73 65 28 73 71 6c 69 74  rtreeClose(sqlit
39997 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a  e3_vtab_cursor *
39998 63 75 72 29 7b 0a 20 20 52 74 72 65 65 20 2a 70  cur){.  Rtree *p
39999 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a  Rtree = (Rtree *
3999a 29 28 63 75 72 2d 3e 70 56 74 61 62 29 3b 0a 20  )(cur->pVtab);. 
3999b 20 69 6e 74 20 72 63 3b 0a 20 20 52 74 72 65 65   int rc;.  Rtree
3999c 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28  Cursor *pCsr = (
3999d 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29 63 75  RtreeCursor *)cu
3999e 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  r;.  sqlite3_fre
3999f 65 28 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61  e(pCsr->aConstra
399a0 69 6e 74 29 3b 0a 20 20 72 63 20 3d 20 6e 6f 64  int);.  rc = nod
399a1 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
399a2 20 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20   pCsr->pNode);. 
399a3 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
399a4 73 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  sr);.  return rc
399a5 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 74 72 65 65  ;.}../*.** Rtree
399a6 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
399a7 6f 64 75 6c 65 20 78 45 6f 66 20 6d 65 74 68 6f  odule xEof metho
399a8 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  d..**.** Return 
399a9 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68 65 20  non-zero if the 
399aa 63 75 72 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20  cursor does not 
399ab 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 20  currently point 
399ac 74 6f 20 61 20 76 61 6c 69 64 20 0a 2a 2a 20 72  to a valid .** r
399ad 65 63 6f 72 64 20 28 69 2e 65 20 69 66 20 74 68  ecord (i.e if th
399ae 65 20 73 63 61 6e 20 68 61 73 20 66 69 6e 69 73  e scan has finis
399af 68 65 64 29 2c 20 6f 72 20 7a 65 72 6f 20 6f 74  hed), or zero ot
399b0 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74  herwise..*/.stat
399b1 69 63 20 69 6e 74 20 72 74 72 65 65 45 6f 66 28  ic int rtreeEof(
399b2 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
399b3 73 6f 72 20 2a 63 75 72 29 7b 0a 20 20 52 74 72  sor *cur){.  Rtr
399b4 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d  eeCursor *pCsr =
399b5 20 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29   (RtreeCursor *)
399b6 63 75 72 3b 0a 20 20 72 65 74 75 72 6e 20 28 70  cur;.  return (p
399b7 43 73 72 2d 3e 70 4e 6f 64 65 3d 3d 30 29 3b 0a  Csr->pNode==0);.
399b8 7d 0a 0a 2f 2a 20 0a 2a 2a 20 43 75 72 73 6f 72  }../* .** Cursor
399b9 20 70 43 75 72 73 6f 72 20 63 75 72 72 65 6e 74   pCursor current
399ba 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 63  ly points to a c
399bb 65 6c 6c 20 69 6e 20 61 20 6e 6f 6e 2d 6c 65 61  ell in a non-lea
399bc 66 20 70 61 67 65 2e 0a 2a 2a 20 52 65 74 75 72  f page..** Retur
399bd 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 73 75  n true if the su
399be 62 2d 74 72 65 65 20 68 65 61 64 65 64 20 62 79  b-tree headed by
399bf 20 74 68 65 20 63 65 6c 6c 20 69 73 20 66 69 6c   the cell is fil
399c0 74 65 72 65 64 0a 2a 2a 20 28 65 78 63 6c 75 64  tered.** (exclud
399c1 65 64 29 20 62 79 20 74 68 65 20 63 6f 6e 73 74  ed) by the const
399c2 72 61 69 6e 74 73 20 69 6e 20 74 68 65 20 70 43  raints in the pC
399c3 75 72 73 6f 72 2d 3e 61 43 6f 6e 73 74 72 61 69  ursor->aConstrai
399c4 6e 74 5b 5d 20 0a 2a 2a 20 61 72 72 61 79 2c 20  nt[] .** array, 
399c5 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69  or false otherwi
399c6 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  se..*/.static in
399c7 74 20 74 65 73 74 52 74 72 65 65 43 65 6c 6c 28  t testRtreeCell(
399c8 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52  Rtree *pRtree, R
399c9 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 75 72  treeCursor *pCur
399ca 73 6f 72 29 7b 0a 20 20 52 74 72 65 65 43 65 6c  sor){.  RtreeCel
399cb 6c 20 63 65 6c 6c 3b 0a 20 20 69 6e 74 20 69 69  l cell;.  int ii
399cc 3b 0a 20 20 69 6e 74 20 62 52 65 73 20 3d 20 30  ;.  int bRes = 0
399cd 3b 0a 0a 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c  ;..  nodeGetCell
399ce 28 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72  (pRtree, pCursor
399cf 2d 3e 70 4e 6f 64 65 2c 20 70 43 75 72 73 6f 72  ->pNode, pCursor
399d0 2d 3e 69 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b  ->iCell, &cell);
399d1 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 62 52 65  .  for(ii=0; bRe
399d2 73 3d 3d 30 20 26 26 20 69 69 3c 70 43 75 72 73  s==0 && ii<pCurs
399d3 6f 72 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  or->nConstraint;
399d4 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65   ii++){.    Rtre
399d5 65 43 6f 6e 73 74 72 61 69 6e 74 20 2a 70 20 3d  eConstraint *p =
399d6 20 26 70 43 75 72 73 6f 72 2d 3e 61 43 6f 6e 73   &pCursor->aCons
399d7 74 72 61 69 6e 74 5b 69 69 5d 3b 0a 20 20 20 20  traint[ii];.    
399d8 64 6f 75 62 6c 65 20 63 65 6c 6c 5f 6d 69 6e 20  double cell_min 
399d9 3d 20 44 43 4f 4f 52 44 28 63 65 6c 6c 2e 61 43  = DCOORD(cell.aC
399da 6f 6f 72 64 5b 28 70 2d 3e 69 43 6f 6f 72 64 3e  oord[(p->iCoord>
399db 3e 31 29 2a 32 5d 29 3b 0a 20 20 20 20 64 6f 75  >1)*2]);.    dou
399dc 62 6c 65 20 63 65 6c 6c 5f 6d 61 78 20 3d 20 44  ble cell_max = D
399dd 43 4f 4f 52 44 28 63 65 6c 6c 2e 61 43 6f 6f 72  COORD(cell.aCoor
399de 64 5b 28 70 2d 3e 69 43 6f 6f 72 64 3e 3e 31 29  d[(p->iCoord>>1)
399df 2a 32 2b 31 5d 29 3b 0a 0a 20 20 20 20 61 73 73  *2+1]);..    ass
399e0 65 72 74 28 70 2d 3e 6f 70 3d 3d 52 54 52 45 45  ert(p->op==RTREE
399e1 5f 4c 45 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54  _LE || p->op==RT
399e2 52 45 45 5f 4c 54 20 7c 7c 20 70 2d 3e 6f 70 3d  REE_LT || p->op=
399e3 3d 52 54 52 45 45 5f 47 45 20 0a 20 20 20 20 20  =RTREE_GE .     
399e4 20 20 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52     || p->op==RTR
399e5 45 45 5f 47 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d  EE_GT || p->op==
399e6 52 54 52 45 45 5f 45 51 0a 20 20 20 20 29 3b 0a  RTREE_EQ.    );.
399e7 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 2d 3e  .    switch( p->
399e8 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65  op ){.      case
399e9 20 52 54 52 45 45 5f 4c 45 3a 20 63 61 73 65 20   RTREE_LE: case 
399ea 52 54 52 45 45 5f 4c 54 3a 20 62 52 65 73 20 3d  RTREE_LT: bRes =
399eb 20 70 2d 3e 72 56 61 6c 75 65 3c 63 65 6c 6c 5f   p->rValue<cell_
399ec 6d 69 6e 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20  min; break;.    
399ed 20 20 63 61 73 65 20 52 54 52 45 45 5f 47 45 3a    case RTREE_GE:
399ee 20 63 61 73 65 20 52 54 52 45 45 5f 47 54 3a 20   case RTREE_GT: 
399ef 62 52 65 73 20 3d 20 70 2d 3e 72 56 61 6c 75 65  bRes = p->rValue
399f0 3e 63 65 6c 6c 5f 6d 61 78 3b 20 62 72 65 61 6b  >cell_max; break
399f1 3b 0a 20 20 20 20 20 20 63 61 73 65 20 52 54 52  ;.      case RTR
399f2 45 45 5f 45 51 3a 20 0a 20 20 20 20 20 20 20 20  EE_EQ: .        
399f3 62 52 65 73 20 3d 20 28 70 2d 3e 72 56 61 6c 75  bRes = (p->rValu
399f4 65 3e 63 65 6c 6c 5f 6d 61 78 20 7c 7c 20 70 2d  e>cell_max || p-
399f5 3e 72 56 61 6c 75 65 3c 63 65 6c 6c 5f 6d 69 6e  >rValue<cell_min
399f6 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
399f7 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  ;.    }.  }..  r
399f8 65 74 75 72 6e 20 62 52 65 73 3b 0a 7d 0a 0a 2f  eturn bRes;.}../
399f9 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  * .** Return tru
399fa 65 20 69 66 20 74 68 65 20 63 65 6c 6c 20 74 68  e if the cell th
399fb 61 74 20 63 75 72 73 6f 72 20 70 43 75 72 73 6f  at cursor pCurso
399fc 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  r currently poin
399fd 74 73 20 74 6f 0a 2a 2a 20 77 6f 75 6c 64 20 62  ts to.** would b
399fe 65 20 66 69 6c 74 65 72 65 64 20 28 65 78 63 6c  e filtered (excl
399ff 75 64 65 64 29 20 62 79 20 74 68 65 20 63 6f 6e  uded) by the con
39a00 73 74 72 61 69 6e 74 73 20 69 6e 20 74 68 65 20  straints in the 
39a01 0a 2a 2a 20 70 43 75 72 73 6f 72 2d 3e 61 43 6f  .** pCursor->aCo
39a02 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79  nstraint[] array
39a03 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72  , or false other
39a04 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  wise..**.** This
39a05 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65   function assume
39a06 73 20 74 68 61 74 20 74 68 65 20 63 65 6c 6c 20  s that the cell 
39a07 69 73 20 70 61 72 74 20 6f 66 20 61 20 6c 65 61  is part of a lea
39a08 66 20 6e 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69  f node..*/.stati
39a09 63 20 69 6e 74 20 74 65 73 74 52 74 72 65 65 45  c int testRtreeE
39a0a 6e 74 72 79 28 52 74 72 65 65 20 2a 70 52 74 72  ntry(Rtree *pRtr
39a0b 65 65 2c 20 52 74 72 65 65 43 75 72 73 6f 72 20  ee, RtreeCursor 
39a0c 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 52 74 72  *pCursor){.  Rtr
39a0d 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 69  eeCell cell;.  i
39a0e 6e 74 20 69 69 3b 0a 0a 20 20 6e 6f 64 65 47 65  nt ii;..  nodeGe
39a0f 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 43  tCell(pRtree, pC
39a10 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20 70 43  ursor->pNode, pC
39a11 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 2c 20 26 63  ursor->iCell, &c
39a12 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30  ell);.  for(ii=0
39a13 3b 20 69 69 3c 70 43 75 72 73 6f 72 2d 3e 6e 43  ; ii<pCursor->nC
39a14 6f 6e 73 74 72 61 69 6e 74 3b 20 69 69 2b 2b 29  onstraint; ii++)
39a15 7b 0a 20 20 20 20 52 74 72 65 65 43 6f 6e 73 74  {.    RtreeConst
39a16 72 61 69 6e 74 20 2a 70 20 3d 20 26 70 43 75 72  raint *p = &pCur
39a17 73 6f 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  sor->aConstraint
39a18 5b 69 69 5d 3b 0a 20 20 20 20 64 6f 75 62 6c 65  [ii];.    double
39a19 20 63 6f 6f 72 64 20 3d 20 44 43 4f 4f 52 44 28   coord = DCOORD(
39a1a 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 70 2d 3e 69  cell.aCoord[p->i
39a1b 43 6f 6f 72 64 5d 29 3b 0a 20 20 20 20 69 6e 74  Coord]);.    int
39a1c 20 72 65 73 3b 0a 20 20 20 20 61 73 73 65 72 74   res;.    assert
39a1d 28 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 4c 45  (p->op==RTREE_LE
39a1e 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45   || p->op==RTREE
39a1f 5f 4c 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54  _LT || p->op==RT
39a20 52 45 45 5f 47 45 20 0a 20 20 20 20 20 20 20 20  REE_GE .        
39a21 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f  || p->op==RTREE_
39a22 47 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52  GT || p->op==RTR
39a23 45 45 5f 45 51 0a 20 20 20 20 29 3b 0a 20 20 20  EE_EQ.    );.   
39a24 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29   switch( p->op )
39a25 7b 0a 20 20 20 20 20 20 63 61 73 65 20 52 54 52  {.      case RTR
39a26 45 45 5f 4c 45 3a 20 72 65 73 20 3d 20 28 63 6f  EE_LE: res = (co
39a27 6f 72 64 3c 3d 70 2d 3e 72 56 61 6c 75 65 29 3b  ord<=p->rValue);
39a28 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
39a29 73 65 20 52 54 52 45 45 5f 4c 54 3a 20 72 65 73  se RTREE_LT: res
39a2a 20 3d 20 28 63 6f 6f 72 64 3c 70 2d 3e 72 56 61   = (coord<p->rVa
39a2b 6c 75 65 29 3b 20 20 62 72 65 61 6b 3b 0a 20 20  lue);  break;.  
39a2c 20 20 20 20 63 61 73 65 20 52 54 52 45 45 5f 47      case RTREE_G
39a2d 45 3a 20 72 65 73 20 3d 20 28 63 6f 6f 72 64 3e  E: res = (coord>
39a2e 3d 70 2d 3e 72 56 61 6c 75 65 29 3b 20 62 72 65  =p->rValue); bre
39a2f 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 52  ak;.      case R
39a30 54 52 45 45 5f 47 54 3a 20 72 65 73 20 3d 20 28  TREE_GT: res = (
39a31 63 6f 6f 72 64 3e 70 2d 3e 72 56 61 6c 75 65 29  coord>p->rValue)
39a32 3b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  ;  break;.      
39a33 63 61 73 65 20 52 54 52 45 45 5f 45 51 3a 20 72  case RTREE_EQ: r
39a34 65 73 20 3d 20 28 63 6f 6f 72 64 3d 3d 70 2d 3e  es = (coord==p->
39a35 72 56 61 6c 75 65 29 3b 20 62 72 65 61 6b 3b 0a  rValue); break;.
39a36 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 21      }..    if( !
39a37 72 65 73 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  res ) return 1;.
39a38 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 30 3b    }..  return 0;
39a39 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 75 72 73 6f 72  .}../*.** Cursor
39a3a 20 70 43 75 72 73 6f 72 20 63 75 72 72 65 6e 74   pCursor current
39a3b 6c 79 20 70 6f 69 6e 74 73 20 61 74 20 61 20 6e  ly points at a n
39a3c 6f 64 65 20 74 68 61 74 20 68 65 61 64 73 20 61  ode that heads a
39a3d 20 73 75 62 2d 74 72 65 65 20 6f 66 0a 2a 2a 20   sub-tree of.** 
39a3e 68 65 69 67 68 74 20 69 48 65 69 67 68 74 20 28  height iHeight (
39a3f 69 66 20 69 48 65 69 67 68 74 3d 3d 30 2c 20 74  if iHeight==0, t
39a40 68 65 6e 20 74 68 65 20 6e 6f 64 65 20 69 73 20  hen the node is 
39a41 61 20 6c 65 61 66 29 2e 20 44 65 73 63 65 6e 64  a leaf). Descend
39a42 0a 2a 2a 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  .** to point to 
39a43 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63 65  the left-most ce
39a44 6c 6c 20 6f 66 20 74 68 65 20 73 75 62 2d 74 72  ll of the sub-tr
39a45 65 65 20 74 68 61 74 20 6d 61 74 63 68 65 73 20  ee that matches 
39a46 74 68 65 20 0a 2a 2a 20 63 6f 6e 66 69 67 75 72  the .** configur
39a47 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a  ed constraints..
39a48 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65  */.static int de
39a49 73 63 65 6e 64 54 6f 43 65 6c 6c 28 0a 20 20 52  scendToCell(.  R
39a4a 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
39a4b 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43   RtreeCursor *pC
39a4c 75 72 73 6f 72 2c 20 0a 20 20 69 6e 74 20 69 48  ursor, .  int iH
39a4d 65 69 67 68 74 2c 0a 20 20 69 6e 74 20 2a 70 45  eight,.  int *pE
39a4e 6f 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20  of              
39a4f 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65 74 20 74     /* OUT: Set t
39a50 6f 20 74 72 75 65 20 69 66 20 63 61 6e 6e 6f 74  o true if cannot
39a51 20 64 65 73 63 65 6e 64 20 2a 2f 0a 29 7b 0a 20   descend */.){. 
39a52 20 69 6e 74 20 69 73 45 6f 66 3b 0a 20 20 69 6e   int isEof;.  in
39a53 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 69 3b 0a  t rc;.  int ii;.
39a54 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43 68    RtreeNode *pCh
39a55 69 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69  ild;.  sqlite3_i
39a56 6e 74 36 34 20 69 52 6f 77 69 64 3b 0a 0a 20 20  nt64 iRowid;..  
39a57 52 74 72 65 65 4e 6f 64 65 20 2a 70 53 61 76 65  RtreeNode *pSave
39a58 64 4e 6f 64 65 20 3d 20 70 43 75 72 73 6f 72 2d  dNode = pCursor-
39a59 3e 70 4e 6f 64 65 3b 0a 20 20 69 6e 74 20 69 53  >pNode;.  int iS
39a5a 61 76 65 64 43 65 6c 6c 20 3d 20 70 43 75 72 73  avedCell = pCurs
39a5b 6f 72 2d 3e 69 43 65 6c 6c 3b 0a 0a 20 20 61 73  or->iCell;..  as
39a5c 73 65 72 74 28 20 69 48 65 69 67 68 74 3e 3d 30  sert( iHeight>=0
39a5d 20 29 3b 0a 0a 20 20 69 66 28 20 69 48 65 69 67   );..  if( iHeig
39a5e 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 73 45  ht==0 ){.    isE
39a5f 6f 66 20 3d 20 74 65 73 74 52 74 72 65 65 45 6e  of = testRtreeEn
39a60 74 72 79 28 70 52 74 72 65 65 2c 20 70 43 75 72  try(pRtree, pCur
39a61 73 6f 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  sor);.  }else{. 
39a62 20 20 20 69 73 45 6f 66 20 3d 20 74 65 73 74 52     isEof = testR
39a63 74 72 65 65 43 65 6c 6c 28 70 52 74 72 65 65 2c  treeCell(pRtree,
39a64 20 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 20   pCursor);.  }. 
39a65 20 69 66 28 20 69 73 45 6f 66 20 7c 7c 20 69 48   if( isEof || iH
39a66 65 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20  eight==0 ){.    
39a67 2a 70 45 6f 66 20 3d 20 69 73 45 6f 66 3b 0a 20  *pEof = isEof;. 
39a68 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
39a69 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 52 6f 77  _OK;.  }..  iRow
39a6a 69 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69  id = nodeGetRowi
39a6b 64 28 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f  d(pRtree, pCurso
39a6c 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 75 72 73 6f  r->pNode, pCurso
39a6d 72 2d 3e 69 43 65 6c 6c 29 3b 0a 20 20 72 63 20  r->iCell);.  rc 
39a6e 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52  = nodeAcquire(pR
39a6f 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20 70 43  tree, iRowid, pC
39a70 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20 26 70  ursor->pNode, &p
39a71 43 68 69 6c 64 29 3b 0a 20 20 69 66 28 20 72 63  Child);.  if( rc
39a72 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
39a73 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
39a74 7d 0a 0a 20 20 6e 6f 64 65 52 65 6c 65 61 73 65  }..  nodeRelease
39a75 28 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72  (pRtree, pCursor
39a76 2d 3e 70 4e 6f 64 65 29 3b 0a 20 20 70 43 75 72  ->pNode);.  pCur
39a77 73 6f 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 43 68  sor->pNode = pCh
39a78 69 6c 64 3b 0a 20 20 69 73 45 6f 66 20 3d 20 31  ild;.  isEof = 1
39a79 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 73  ;.  for(ii=0; is
39a7a 45 6f 66 20 26 26 20 69 69 3c 4e 43 45 4c 4c 28  Eof && ii<NCELL(
39a7b 70 43 68 69 6c 64 29 3b 20 69 69 2b 2b 29 7b 0a  pChild); ii++){.
39a7c 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 69 43 65      pCursor->iCe
39a7d 6c 6c 20 3d 20 69 69 3b 0a 20 20 20 20 72 63 20  ll = ii;.    rc 
39a7e 3d 20 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c 28  = descendToCell(
39a7f 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72 2c  pRtree, pCursor,
39a80 20 69 48 65 69 67 68 74 2d 31 2c 20 26 69 73 45   iHeight-1, &isE
39a81 6f 66 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  of);.    if( rc!
39a82 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
39a83 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
39a84 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
39a85 69 73 45 6f 66 20 29 7b 0a 20 20 20 20 61 73 73  isEof ){.    ass
39a86 65 72 74 28 20 70 43 75 72 73 6f 72 2d 3e 70 4e  ert( pCursor->pN
39a87 6f 64 65 3d 3d 70 43 68 69 6c 64 20 29 3b 0a 20  ode==pChild );. 
39a88 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65     nodeReference
39a89 28 70 53 61 76 65 64 4e 6f 64 65 29 3b 0a 20 20  (pSavedNode);.  
39a8a 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52    nodeRelease(pR
39a8b 74 72 65 65 2c 20 70 43 68 69 6c 64 29 3b 0a 20  tree, pChild);. 
39a8c 20 20 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64     pCursor->pNod
39a8d 65 20 3d 20 70 53 61 76 65 64 4e 6f 64 65 3b 0a  e = pSavedNode;.
39a8e 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 69 43 65      pCursor->iCe
39a8f 6c 6c 20 3d 20 69 53 61 76 65 64 43 65 6c 6c 3b  ll = iSavedCell;
39a90 0a 20 20 7d 0a 0a 20 20 2a 70 45 6f 66 20 3d 20  .  }..  *pEof = 
39a91 69 73 45 6f 66 3b 0a 20 20 72 65 74 75 72 6e 20  isEof;.  return 
39a92 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
39a93 0a 2a 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 63  .** One of the c
39a94 65 6c 6c 73 20 69 6e 20 6e 6f 64 65 20 70 4e 6f  ells in node pNo
39a95 64 65 20 69 73 20 67 75 61 72 61 6e 74 65 65 64  de is guaranteed
39a96 20 74 6f 20 68 61 76 65 20 61 20 36 34 2d 62 69   to have a 64-bi
39a97 74 20 0a 2a 2a 20 69 6e 74 65 67 65 72 20 76 61  t .** integer va
39a98 6c 75 65 20 65 71 75 61 6c 20 74 6f 20 69 52 6f  lue equal to iRo
39a99 77 69 64 2e 20 52 65 74 75 72 6e 20 74 68 65 20  wid. Return the 
39a9a 69 6e 64 65 78 20 6f 66 20 74 68 69 73 20 63 65  index of this ce
39a9b 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ll..*/.static in
39a9c 74 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64 65 78  t nodeRowidIndex
39a9d 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20  (Rtree *pRtree, 
39a9e 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
39a9f 2c 20 69 36 34 20 69 52 6f 77 69 64 29 7b 0a 20  , i64 iRowid){. 
39aa0 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28 69   int ii;.  for(i
39aa1 69 3d 30 3b 20 6e 6f 64 65 47 65 74 52 6f 77 69  i=0; nodeGetRowi
39aa2 64 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c  d(pRtree, pNode,
39aa3 20 69 69 29 21 3d 69 52 6f 77 69 64 3b 20 69 69   ii)!=iRowid; ii
39aa4 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ++){.    assert(
39aa5 20 69 69 3c 28 4e 43 45 4c 4c 28 70 4e 6f 64 65   ii<(NCELL(pNode
39aa6 29 2d 31 29 20 29 3b 0a 20 20 7d 0a 20 20 72 65  )-1) );.  }.  re
39aa7 74 75 72 6e 20 69 69 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn ii;.}../*.*
39aa8 2a 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64  * Return the ind
39aa9 65 78 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63  ex of the cell c
39aaa 6f 6e 74 61 69 6e 69 6e 67 20 61 20 70 6f 69 6e  ontaining a poin
39aab 74 65 72 20 74 6f 20 6e 6f 64 65 20 70 4e 6f 64  ter to node pNod
39aac 65 0a 2a 2a 20 69 6e 20 69 74 73 20 70 61 72 65  e.** in its pare
39aad 6e 74 2e 20 49 66 20 70 4e 6f 64 65 20 69 73 20  nt. If pNode is 
39aae 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 2c 20 72  the root node, r
39aaf 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f 0a 73 74 61  eturn -1..*/.sta
39ab0 74 69 63 20 69 6e 74 20 6e 6f 64 65 50 61 72 65  tic int nodePare
39ab1 6e 74 49 6e 64 65 78 28 52 74 72 65 65 20 2a 70  ntIndex(Rtree *p
39ab2 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
39ab3 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 52 74 72 65   *pNode){.  Rtre
39ab4 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 20 3d  eNode *pParent =
39ab5 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b   pNode->pParent;
39ab6 0a 20 20 69 66 28 20 70 50 61 72 65 6e 74 20 29  .  if( pParent )
39ab7 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 6f 64  {.    return nod
39ab8 65 52 6f 77 69 64 49 6e 64 65 78 28 70 52 74 72  eRowidIndex(pRtr
39ab9 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 70 4e 6f  ee, pParent, pNo
39aba 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 7d 0a  de->iNode);.  }.
39abb 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 7d 0a 0a    return -1;.}..
39abc 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72  /* .** Rtree vir
39abd 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
39abe 65 20 78 4e 65 78 74 20 6d 65 74 68 6f 64 2e 0a  e xNext method..
39abf 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74  */.static int rt
39ac0 72 65 65 4e 65 78 74 28 73 71 6c 69 74 65 33 5f  reeNext(sqlite3_
39ac1 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74  vtab_cursor *pVt
39ac2 61 62 43 75 72 73 6f 72 29 7b 0a 20 20 52 74 72  abCursor){.  Rtr
39ac3 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74  ee *pRtree = (Rt
39ac4 72 65 65 20 2a 29 28 70 56 74 61 62 43 75 72 73  ree *)(pVtabCurs
39ac5 6f 72 2d 3e 70 56 74 61 62 29 3b 0a 20 20 52 74  or->pVtab);.  Rt
39ac6 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 20  reeCursor *pCsr 
39ac7 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a  = (RtreeCursor *
39ac8 29 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20  )pVtabCursor;.  
39ac9 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
39aca 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 43 73 72 2d  OK;..  if( pCsr-
39acb 3e 69 53 74 72 61 74 65 67 79 3d 3d 31 20 29 7b  >iStrategy==1 ){
39acc 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 22 73 63  .    /* This "sc
39acd 61 6e 22 20 69 73 20 61 20 64 69 72 65 63 74 20  an" is a direct 
39ace 6c 6f 6f 6b 75 70 20 62 79 20 72 6f 77 69 64 2e  lookup by rowid.
39acf 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 78   There is no nex
39ad0 74 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 20 20  t entry. */.    
39ad1 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72  nodeRelease(pRtr
39ad2 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65 29  ee, pCsr->pNode)
39ad3 3b 0a 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64  ;.    pCsr->pNod
39ad4 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 65 6c  e = 0;.  }..  el
39ad5 73 65 20 69 66 28 20 70 43 73 72 2d 3e 70 4e 6f  se if( pCsr->pNo
39ad6 64 65 20 29 7b 0a 20 20 20 20 2f 2a 20 4d 6f 76  de ){.    /* Mov
39ad7 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 65 6e  e to the next en
39ad8 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 65 73  try that matches
39ad9 20 74 68 65 20 63 6f 6e 66 69 67 75 72 65 64 20   the configured 
39ada 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 2a 2f 0a  constraints. */.
39adb 20 20 20 20 69 6e 74 20 69 48 65 69 67 68 74 20      int iHeight 
39adc 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  = 0;.    while( 
39add 70 43 73 72 2d 3e 70 4e 6f 64 65 20 29 7b 0a 20  pCsr->pNode ){. 
39ade 20 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a       RtreeNode *
39adf 70 4e 6f 64 65 20 3d 20 70 43 73 72 2d 3e 70 4e  pNode = pCsr->pN
39ae0 6f 64 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  ode;.      int n
39ae1 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f  Cell = NCELL(pNo
39ae2 64 65 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 70  de);.      for(p
39ae3 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b 3b 20 70 43  Csr->iCell++; pC
39ae4 73 72 2d 3e 69 43 65 6c 6c 3c 6e 43 65 6c 6c 3b  sr->iCell<nCell;
39ae5 20 70 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b 29 7b   pCsr->iCell++){
39ae6 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 73 45  .        int isE
39ae7 6f 66 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  of;.        rc =
39ae8 20 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c 28 70   descendToCell(p
39ae9 52 74 72 65 65 2c 20 70 43 73 72 2c 20 69 48 65  Rtree, pCsr, iHe
39aea 69 67 68 74 2c 20 26 69 73 45 6f 66 29 3b 0a 20  ight, &isEof);. 
39aeb 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
39aec 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 69 73 45  QLITE_OK || !isE
39aed 6f 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  of ){.          
39aee 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
39aef 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
39af0 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d     pCsr->pNode =
39af1 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b   pNode->pParent;
39af2 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 69 43 65  .      pCsr->iCe
39af3 6c 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49  ll = nodeParentI
39af4 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70 4e 6f  ndex(pRtree, pNo
39af5 64 65 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52  de);.      nodeR
39af6 65 66 65 72 65 6e 63 65 28 70 43 73 72 2d 3e 70  eference(pCsr->p
39af7 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 6e 6f 64  Node);.      nod
39af8 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
39af9 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 69   pNode);.      i
39afa 48 65 69 67 68 74 2b 2b 3b 0a 20 20 20 20 7d 0a  Height++;.    }.
39afb 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
39afc 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65  ;.}../* .** Rtre
39afd 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
39afe 6d 6f 64 75 6c 65 20 78 52 6f 77 69 64 20 6d 65  module xRowid me
39aff 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  thod..*/.static 
39b00 69 6e 74 20 72 74 72 65 65 52 6f 77 69 64 28 73  int rtreeRowid(s
39b01 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
39b02 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 2c  or *pVtabCursor,
39b03 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70   sqlite_int64 *p
39b04 52 6f 77 69 64 29 7b 0a 20 20 52 74 72 65 65 20  Rowid){.  Rtree 
39b05 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65  *pRtree = (Rtree
39b06 20 2a 29 70 56 74 61 62 43 75 72 73 6f 72 2d 3e   *)pVtabCursor->
39b07 70 56 74 61 62 3b 0a 20 20 52 74 72 65 65 43 75  pVtab;.  RtreeCu
39b08 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 52 74  rsor *pCsr = (Rt
39b09 72 65 65 43 75 72 73 6f 72 20 2a 29 70 56 74 61  reeCursor *)pVta
39b0a 62 43 75 72 73 6f 72 3b 0a 0a 20 20 61 73 73 65  bCursor;..  asse
39b0b 72 74 28 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b  rt(pCsr->pNode);
39b0c 0a 20 20 2a 70 52 6f 77 69 64 20 3d 20 6e 6f 64  .  *pRowid = nod
39b0d 65 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65  eGetRowid(pRtree
39b0e 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65 2c 20 70  , pCsr->pNode, p
39b0f 43 73 72 2d 3e 69 43 65 6c 6c 29 3b 0a 0a 20 20  Csr->iCell);..  
39b10 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
39b11 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65  ;.}../* .** Rtre
39b12 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
39b13 6d 6f 64 75 6c 65 20 78 43 6f 6c 75 6d 6e 20 6d  module xColumn m
39b14 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ethod..*/.static
39b15 20 69 6e 74 20 72 74 72 65 65 43 6f 6c 75 6d 6e   int rtreeColumn
39b16 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
39b17 72 73 6f 72 20 2a 63 75 72 2c 20 73 71 6c 69 74  rsor *cur, sqlit
39b18 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 74 78 2c  e3_context *ctx,
39b19 20 69 6e 74 20 69 29 7b 0a 20 20 52 74 72 65 65   int i){.  Rtree
39b1a 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65   *pRtree = (Rtre
39b1b 65 20 2a 29 63 75 72 2d 3e 70 56 74 61 62 3b 0a  e *)cur->pVtab;.
39b1c 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70    RtreeCursor *p
39b1d 43 73 72 20 3d 20 28 52 74 72 65 65 43 75 72 73  Csr = (RtreeCurs
39b1e 6f 72 20 2a 29 63 75 72 3b 0a 0a 20 20 69 66 28  or *)cur;..  if(
39b1f 20 69 3d 3d 30 20 29 7b 0a 20 20 20 20 69 36 34   i==0 ){.    i64
39b20 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65   iRowid = nodeGe
39b21 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70  tRowid(pRtree, p
39b22 43 73 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 73 72  Csr->pNode, pCsr
39b23 2d 3e 69 43 65 6c 6c 29 3b 0a 20 20 20 20 73 71  ->iCell);.    sq
39b24 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
39b25 36 34 28 63 74 78 2c 20 69 52 6f 77 69 64 29 3b  64(ctx, iRowid);
39b26 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 52 74  .  }else{.    Rt
39b27 72 65 65 43 6f 6f 72 64 20 63 3b 0a 20 20 20 20  reeCoord c;.    
39b28 6e 6f 64 65 47 65 74 43 6f 6f 72 64 28 70 52 74  nodeGetCoord(pRt
39b29 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65  ree, pCsr->pNode
39b2a 2c 20 70 43 73 72 2d 3e 69 43 65 6c 6c 2c 20 69  , pCsr->iCell, i
39b2b 2d 31 2c 20 26 63 29 3b 0a 20 20 20 20 69 66 28  -1, &c);.    if(
39b2c 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54   pRtree->eCoordT
39b2d 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52 44  ype==RTREE_COORD
39b2e 5f 52 45 41 4c 33 32 20 29 7b 0a 20 20 20 20 20  _REAL32 ){.     
39b2f 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
39b30 64 6f 75 62 6c 65 28 63 74 78 2c 20 63 2e 66 29  double(ctx, c.f)
39b31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
39b32 20 20 20 61 73 73 65 72 74 28 20 70 52 74 72 65     assert( pRtre
39b33 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52  e->eCoordType==R
39b34 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32  TREE_COORD_INT32
39b35 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
39b36 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 74 78  3_result_int(ctx
39b37 2c 20 63 2e 69 29 3b 0a 20 20 20 20 7d 0a 20 20  , c.i);.    }.  
39b38 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  }..  return SQLI
39b39 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  TE_OK;.}../* .**
39b3a 20 55 73 65 20 6e 6f 64 65 41 63 71 75 69 72 65   Use nodeAcquire
39b3b 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65  () to obtain the
39b3c 20 6c 65 61 66 20 6e 6f 64 65 20 63 6f 6e 74 61   leaf node conta
39b3d 69 6e 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64  ining the record
39b3e 20 77 69 74 68 20 0a 2a 2a 20 72 6f 77 69 64 20   with .** rowid 
39b3f 69 52 6f 77 69 64 2e 20 49 66 20 73 75 63 63 65  iRowid. If succe
39b40 73 73 66 75 6c 2c 20 73 65 74 20 2a 70 70 4c 65  ssful, set *ppLe
39b41 61 66 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  af to point to t
39b42 68 65 20 6e 6f 64 65 20 61 6e 64 0a 2a 2a 20 72  he node and.** r
39b43 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
39b44 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
39b45 73 75 63 68 20 72 65 63 6f 72 64 20 69 6e 20 74  such record in t
39b46 68 65 20 74 61 62 6c 65 2c 20 73 65 74 0a 2a 2a  he table, set.**
39b47 20 2a 70 70 4c 65 61 66 20 74 6f 20 30 20 61 6e   *ppLeaf to 0 an
39b48 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  d return SQLITE_
39b49 4f 4b 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  OK. If an error 
39b4a 6f 63 63 75 72 73 2c 20 73 65 74 20 2a 70 70 4c  occurs, set *ppL
39b4b 65 61 66 0a 2a 2a 20 74 6f 20 7a 65 72 6f 20 61  eaf.** to zero a
39b4c 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 53 51 4c  nd return an SQL
39b4d 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a  ite error code..
39b4e 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69  */.static int fi
39b4f 6e 64 4c 65 61 66 4e 6f 64 65 28 52 74 72 65 65  ndLeafNode(Rtree
39b50 20 2a 70 52 74 72 65 65 2c 20 69 36 34 20 69 52   *pRtree, i64 iR
39b51 6f 77 69 64 2c 20 52 74 72 65 65 4e 6f 64 65 20  owid, RtreeNode 
39b52 2a 2a 70 70 4c 65 61 66 29 7b 0a 20 20 69 6e 74  **ppLeaf){.  int
39b53 20 72 63 3b 0a 20 20 2a 70 70 4c 65 61 66 20 3d   rc;.  *ppLeaf =
39b54 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69   0;.  sqlite3_bi
39b55 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d  nd_int64(pRtree-
39b56 3e 70 52 65 61 64 52 6f 77 69 64 2c 20 31 2c 20  >pReadRowid, 1, 
39b57 69 52 6f 77 69 64 29 3b 0a 20 20 69 66 28 20 73  iRowid);.  if( s
39b58 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72  qlite3_step(pRtr
39b59 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3d  ee->pReadRowid)=
39b5a 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
39b5b 20 20 20 69 36 34 20 69 4e 6f 64 65 20 3d 20 73     i64 iNode = s
39b5c 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
39b5d 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52 65 61  t64(pRtree->pRea
39b5e 64 52 6f 77 69 64 2c 20 30 29 3b 0a 20 20 20 20  dRowid, 0);.    
39b5f 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65  rc = nodeAcquire
39b60 28 70 52 74 72 65 65 2c 20 69 4e 6f 64 65 2c 20  (pRtree, iNode, 
39b61 30 2c 20 70 70 4c 65 61 66 29 3b 0a 20 20 20 20  0, ppLeaf);.    
39b62 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52  sqlite3_reset(pR
39b63 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64  tree->pReadRowid
39b64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
39b65 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
39b66 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64  et(pRtree->pRead
39b67 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 20 20 72 65  Rowid);.  }.  re
39b68 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 20  turn rc;.}.../* 
39b69 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61  .** Rtree virtua
39b6a 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
39b6b 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 2e 0a 2a  Filter method..*
39b6c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72  /.static int rtr
39b6d 65 65 46 69 6c 74 65 72 28 0a 20 20 73 71 6c 69  eeFilter(.  sqli
39b6e 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
39b6f 2a 70 56 74 61 62 43 75 72 73 6f 72 2c 20 0a 20  *pVtabCursor, . 
39b70 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e   int idxNum, con
39b71 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 72 2c  st char *idxStr,
39b72 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  .  int argc, sql
39b73 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
39b74 76 0a 29 7b 0a 20 20 52 74 72 65 65 20 2a 70 52  v.){.  Rtree *pR
39b75 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29  tree = (Rtree *)
39b76 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74  pVtabCursor->pVt
39b77 61 62 3b 0a 20 20 52 74 72 65 65 43 75 72 73 6f  ab;.  RtreeCurso
39b78 72 20 2a 70 43 73 72 20 3d 20 28 52 74 72 65 65  r *pCsr = (Rtree
39b79 43 75 72 73 6f 72 20 2a 29 70 56 74 61 62 43 75  Cursor *)pVtabCu
39b7a 72 73 6f 72 3b 0a 0a 20 20 52 74 72 65 65 4e 6f  rsor;..  RtreeNo
39b7b 64 65 20 2a 70 52 6f 6f 74 20 3d 20 30 3b 0a 20  de *pRoot = 0;. 
39b7c 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 72   int ii;.  int r
39b7d 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
39b7e 20 20 72 74 72 65 65 52 65 66 65 72 65 6e 63 65    rtreeReference
39b7f 28 70 52 74 72 65 65 29 3b 0a 0a 20 20 73 71 6c  (pRtree);..  sql
39b80 69 74 65 33 5f 66 72 65 65 28 70 43 73 72 2d 3e  ite3_free(pCsr->
39b81 61 43 6f 6e 73 74 72 61 69 6e 74 29 3b 0a 20 20  aConstraint);.  
39b82 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  pCsr->aConstrain
39b83 74 20 3d 20 30 3b 0a 20 20 70 43 73 72 2d 3e 69  t = 0;.  pCsr->i
39b84 53 74 72 61 74 65 67 79 20 3d 20 69 64 78 4e 75  Strategy = idxNu
39b85 6d 3b 0a 0a 20 20 69 66 28 20 69 64 78 4e 75 6d  m;..  if( idxNum
39b86 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 53 70  ==1 ){.    /* Sp
39b87 65 63 69 61 6c 20 63 61 73 65 20 2d 20 6c 6f 6f  ecial case - loo
39b88 6b 75 70 20 62 79 20 72 6f 77 69 64 2e 20 2a 2f  kup by rowid. */
39b89 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  .    RtreeNode *
39b8a 70 4c 65 61 66 3b 20 20 20 20 20 20 20 20 2f 2a  pLeaf;        /*
39b8b 20 4c 65 61 66 20 6f 6e 20 77 68 69 63 68 20 74   Leaf on which t
39b8c 68 65 20 72 65 71 75 69 72 65 64 20 63 65 6c 6c  he required cell
39b8d 20 72 65 73 69 64 65 73 20 2a 2f 0a 20 20 20 20   resides */.    
39b8e 69 36 34 20 69 52 6f 77 69 64 20 3d 20 73 71 6c  i64 iRowid = sql
39b8f 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
39b90 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 72  (argv[0]);.    r
39b91 63 20 3d 20 66 69 6e 64 4c 65 61 66 4e 6f 64 65  c = findLeafNode
39b92 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64 2c  (pRtree, iRowid,
39b93 20 26 70 4c 65 61 66 29 3b 0a 20 20 20 20 70 43   &pLeaf);.    pC
39b94 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 4c 65 61  sr->pNode = pLea
39b95 66 3b 20 0a 20 20 20 20 69 66 28 20 70 4c 65 61  f; .    if( pLea
39b96 66 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f  f && rc==SQLITE_
39b97 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 73 72  OK ){.      pCsr
39b98 2d 3e 69 43 65 6c 6c 20 3d 20 6e 6f 64 65 52 6f  ->iCell = nodeRo
39b99 77 69 64 49 6e 64 65 78 28 70 52 74 72 65 65 2c  widIndex(pRtree,
39b9a 20 70 4c 65 61 66 2c 20 69 52 6f 77 69 64 29 3b   pLeaf, iRowid);
39b9b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
39b9c 20 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20 63 61      /* Normal ca
39b9d 73 65 20 2d 20 72 2d 74 72 65 65 20 73 63 61 6e  se - r-tree scan
39b9e 2e 20 53 65 74 20 75 70 20 74 68 65 20 52 74 72  . Set up the Rtr
39b9f 65 65 43 75 72 73 6f 72 2e 61 43 6f 6e 73 74 72  eeCursor.aConstr
39ba0 61 69 6e 74 20 61 72 72 61 79 20 0a 20 20 20 20  aint array .    
39ba1 2a 2a 20 77 69 74 68 20 74 68 65 20 63 6f 6e 66  ** with the conf
39ba2 69 67 75 72 65 64 20 63 6f 6e 73 74 72 61 69 6e  igured constrain
39ba3 74 73 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ts. .    */.    
39ba4 69 66 28 20 61 72 67 63 3e 30 20 29 7b 0a 20 20  if( argc>0 ){.  
39ba5 20 20 20 20 70 43 73 72 2d 3e 61 43 6f 6e 73 74      pCsr->aConst
39ba6 72 61 69 6e 74 20 3d 20 73 71 6c 69 74 65 33 5f  raint = sqlite3_
39ba7 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74  malloc(sizeof(Rt
39ba8 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 29 2a 61  reeConstraint)*a
39ba9 72 67 63 29 3b 0a 20 20 20 20 20 20 70 43 73 72  rgc);.      pCsr
39baa 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20  ->nConstraint = 
39bab 61 72 67 63 3b 0a 20 20 20 20 20 20 69 66 28 20  argc;.      if( 
39bac 21 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69  !pCsr->aConstrai
39bad 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  nt ){.        rc
39bae 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
39baf 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
39bb0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 69        assert( (i
39bb1 64 78 53 74 72 3d 3d 30 20 26 26 20 61 72 67 63  dxStr==0 && argc
39bb2 3d 3d 30 29 20 7c 7c 20 73 74 72 6c 65 6e 28 69  ==0) || strlen(i
39bb3 64 78 53 74 72 29 3d 3d 61 72 67 63 2a 32 20 29  dxStr)==argc*2 )
39bb4 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 69  ;.        for(ii
39bb5 3d 30 3b 20 69 69 3c 61 72 67 63 3b 20 69 69 2b  =0; ii<argc; ii+
39bb6 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 52 74  +){.          Rt
39bb7 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 2a 70  reeConstraint *p
39bb8 20 3d 20 26 70 43 73 72 2d 3e 61 43 6f 6e 73 74   = &pCsr->aConst
39bb9 72 61 69 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 20  raint[ii];.     
39bba 20 20 20 20 20 70 2d 3e 6f 70 20 3d 20 69 64 78       p->op = idx
39bbb 53 74 72 5b 69 69 2a 32 5d 3b 0a 20 20 20 20 20  Str[ii*2];.     
39bbc 20 20 20 20 20 70 2d 3e 69 43 6f 6f 72 64 20 3d       p->iCoord =
39bbd 20 69 64 78 53 74 72 5b 69 69 2a 32 2b 31 5d 2d   idxStr[ii*2+1]-
39bbe 27 61 27 3b 0a 20 20 20 20 20 20 20 20 20 20 70  'a';.          p
39bbf 2d 3e 72 56 61 6c 75 65 20 3d 20 73 71 6c 69 74  ->rValue = sqlit
39bc0 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28  e3_value_double(
39bc1 61 72 67 76 5b 69 69 5d 29 3b 0a 20 20 20 20 20  argv[ii]);.     
39bc2 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
39bc3 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 72 63   }.  .    if( rc
39bc4 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
39bc5 20 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65       pCsr->pNode
39bc6 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d   = 0;.      rc =
39bc7 20 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52 74   nodeAcquire(pRt
39bc8 72 65 65 2c 20 31 2c 20 30 2c 20 26 70 52 6f 6f  ree, 1, 0, &pRoo
39bc9 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  t);.    }.    if
39bca 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
39bcb 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 73 45  ){.      int isE
39bcc 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 6e  of = 1;.      in
39bcd 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28  t nCell = NCELL(
39bce 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 70 43  pRoot);.      pC
39bcf 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 52 6f 6f  sr->pNode = pRoo
39bd0 74 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 43 73  t;.      for(pCs
39bd1 72 2d 3e 69 43 65 6c 6c 3d 30 3b 20 72 63 3d 3d  r->iCell=0; rc==
39bd2 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 43 73  SQLITE_OK && pCs
39bd3 72 2d 3e 69 43 65 6c 6c 3c 6e 43 65 6c 6c 3b 20  r->iCell<nCell; 
39bd4 70 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b 29 7b 0a  pCsr->iCell++){.
39bd5 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
39bd6 70 43 73 72 2d 3e 70 4e 6f 64 65 3d 3d 70 52 6f  pCsr->pNode==pRo
39bd7 6f 74 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63  ot );.        rc
39bd8 20 3d 20 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c   = descendToCell
39bd9 28 70 52 74 72 65 65 2c 20 70 43 73 72 2c 20 70  (pRtree, pCsr, p
39bda 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2c 20 26  Rtree->iDepth, &
39bdb 69 73 45 6f 66 29 3b 0a 20 20 20 20 20 20 20 20  isEof);.        
39bdc 69 66 28 20 21 69 73 45 6f 66 20 29 7b 0a 20 20  if( !isEof ){.  
39bdd 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
39bde 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
39bdf 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
39be0 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 73 45 6f  QLITE_OK && isEo
39be1 66 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  f ){.        ass
39be2 65 72 74 28 20 70 43 73 72 2d 3e 70 4e 6f 64 65  ert( pCsr->pNode
39be3 3d 3d 70 52 6f 6f 74 20 29 3b 0a 20 20 20 20 20  ==pRoot );.     
39be4 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70     nodeRelease(p
39be5 52 74 72 65 65 2c 20 70 52 6f 6f 74 29 3b 0a 20  Rtree, pRoot);. 
39be6 20 20 20 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f         pCsr->pNo
39be7 64 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  de = 0;.      }.
39be8 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
39be9 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21  !=SQLITE_OK || !
39bea 70 43 73 72 2d 3e 70 4e 6f 64 65 20 7c 7c 20 70  pCsr->pNode || p
39beb 43 73 72 2d 3e 69 43 65 6c 6c 3c 4e 43 45 4c 4c  Csr->iCell<NCELL
39bec 28 70 43 73 72 2d 3e 70 4e 6f 64 65 29 20 29 3b  (pCsr->pNode) );
39bed 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 74  .    }.  }..  rt
39bee 72 65 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  reeRelease(pRtre
39bef 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  e);.  return rc;
39bf0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 74 72 65 65 20  .}../*.** Rtree 
39bf1 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
39bf2 64 75 6c 65 20 78 42 65 73 74 49 6e 64 65 78 20  dule xBestIndex 
39bf3 6d 65 74 68 6f 64 2e 20 54 68 65 72 65 20 61 72  method. There ar
39bf4 65 20 74 68 72 65 65 0a 2a 2a 20 74 61 62 6c 65  e three.** table
39bf5 20 73 63 61 6e 20 73 74 72 61 74 65 67 69 65 73   scan strategies
39bf6 20 74 6f 20 63 68 6f 6f 73 65 20 66 72 6f 6d 20   to choose from 
39bf7 28 69 6e 20 6f 72 64 65 72 20 66 72 6f 6d 20 6d  (in order from m
39bf8 6f 73 74 20 74 6f 20 0a 2a 2a 20 6c 65 61 73 74  ost to .** least
39bf9 20 64 65 73 69 72 61 62 6c 65 29 3a 0a 2a 2a 0a   desirable):.**.
39bfa 2a 2a 20 20 20 69 64 78 4e 75 6d 20 20 20 20 20  **   idxNum     
39bfb 69 64 78 53 74 72 20 20 20 20 20 20 20 20 53 74  idxStr        St
39bfc 72 61 74 65 67 79 0a 2a 2a 20 20 20 2d 2d 2d 2d  rategy.**   ----
39bfd 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39bfe 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39bff 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20  ------------.** 
39c00 20 20 20 20 31 20 20 20 20 20 20 20 20 55 6e 75      1        Unu
39c01 73 65 64 20 20 20 20 20 20 20 20 44 69 72 65 63  sed        Direc
39c02 74 20 6c 6f 6f 6b 75 70 20 62 79 20 72 6f 77 69  t lookup by rowi
39c03 64 2e 0a 2a 2a 20 20 20 20 20 32 20 20 20 20 20  d..**     2     
39c04 20 20 20 53 65 65 20 62 65 6c 6f 77 20 20 20 20     See below    
39c05 20 52 2d 74 72 65 65 20 71 75 65 72 79 2e 0a 2a   R-tree query..*
39c06 2a 20 20 20 20 20 33 20 20 20 20 20 20 20 20 55  *     3        U
39c07 6e 75 73 65 64 20 20 20 20 20 20 20 20 46 75 6c  nused        Ful
39c08 6c 20 74 61 62 6c 65 20 73 63 61 6e 2e 0a 2a 2a  l table scan..**
39c09 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
39c0a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39c0b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39c0c 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 49 66 20 73 74 72  ---.**.** If str
39c0d 61 74 65 67 79 20 31 20 6f 72 20 33 20 69 73 20  ategy 1 or 3 is 
39c0e 75 73 65 64 2c 20 74 68 65 6e 20 69 64 78 53 74  used, then idxSt
39c0f 72 20 69 73 20 6e 6f 74 20 6d 65 61 6e 69 6e 67  r is not meaning
39c10 66 75 6c 2e 20 49 66 20 73 74 72 61 74 65 67 79  ful. If strategy
39c11 0a 2a 2a 20 32 20 69 73 20 75 73 65 64 2c 20 69  .** 2 is used, i
39c12 64 78 53 74 72 20 69 73 20 66 6f 72 6d 61 74 74  dxStr is formatt
39c13 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 32 20  ed to contain 2 
39c14 62 79 74 65 73 20 66 6f 72 20 65 61 63 68 20 0a  bytes for each .
39c15 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 75 73  ** constraint us
39c16 65 64 2e 20 54 68 65 20 66 69 72 73 74 20 74 77  ed. The first tw
39c17 6f 20 62 79 74 65 73 20 6f 66 20 69 64 78 53 74  o bytes of idxSt
39c18 72 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20  r correspond to 
39c19 0a 2a 2a 20 74 68 65 20 63 6f 6e 73 74 72 61 69  .** the constrai
39c1a 6e 74 20 69 6e 20 73 71 6c 69 74 65 33 5f 69 6e  nt in sqlite3_in
39c1b 64 65 78 5f 69 6e 66 6f 2e 61 43 6f 6e 73 74 72  dex_info.aConstr
39c1c 61 69 6e 74 55 73 61 67 65 5b 5d 20 77 69 74 68  aintUsage[] with
39c1d 0a 2a 2a 20 28 61 72 67 76 49 6e 64 65 78 3d 3d  .** (argvIndex==
39c1e 31 29 20 65 74 63 2e 0a 2a 2a 0a 2a 2a 20 54 68  1) etc..**.** Th
39c1f 65 20 66 69 72 73 74 20 6f 66 20 65 61 63 68 20  e first of each 
39c20 70 61 69 72 20 6f 66 20 62 79 74 65 73 20 69 6e  pair of bytes in
39c21 20 69 64 78 53 74 72 20 69 64 65 6e 74 69 66 69   idxStr identifi
39c22 65 73 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  es the constrain
39c23 74 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 61 73  t.** operator as
39c24 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
39c25 20 20 4f 70 65 72 61 74 6f 72 20 20 20 20 42 79    Operator    By
39c26 74 65 20 56 61 6c 75 65 0a 2a 2a 20 20 20 2d 2d  te Value.**   --
39c27 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
39c28 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 3d 20 20  ----.**      =  
39c29 20 20 20 20 20 20 30 78 34 31 20 28 27 41 27 29        0x41 ('A')
39c2a 0a 2a 2a 20 20 20 20 20 3c 3d 20 20 20 20 20 20  .**     <=      
39c2b 20 20 30 78 34 32 20 28 27 42 27 29 0a 2a 2a 20    0x42 ('B').** 
39c2c 20 20 20 20 20 3c 20 20 20 20 20 20 20 20 30 78       <        0x
39c2d 34 33 20 28 27 43 27 29 0a 2a 2a 20 20 20 20 20  43 ('C').**     
39c2e 3e 3d 20 20 20 20 20 20 20 20 30 78 34 34 20 28  >=        0x44 (
39c2f 27 44 27 29 0a 2a 2a 20 20 20 20 20 20 3e 20 20  'D').**      >  
39c30 20 20 20 20 20 20 30 78 34 35 20 28 27 45 27 29        0x45 ('E')
39c31 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .**   ----------
39c32 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a  ------------.**.
39c33 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 6f 66  ** The second of
39c34 20 65 61 63 68 20 70 61 69 72 20 6f 66 20 62 79   each pair of by
39c35 74 65 73 20 69 64 65 6e 74 69 66 69 65 73 20 74  tes identifies t
39c36 68 65 20 63 6f 6f 72 64 69 6e 61 74 65 20 63 6f  he coordinate co
39c37 6c 75 6d 6e 0a 2a 2a 20 74 6f 20 77 68 69 63 68  lumn.** to which
39c38 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20   the constraint 
39c39 61 70 70 6c 69 65 73 2e 20 54 68 65 20 6c 65 66  applies. The lef
39c3a 74 6d 6f 73 74 20 63 6f 6f 72 64 69 6e 61 74 65  tmost coordinate
39c3b 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 27 61   column.** is 'a
39c3c 27 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 66 72  ', the second fr
39c3d 6f 6d 20 74 68 65 20 6c 65 66 74 20 27 62 27 20  om the left 'b' 
39c3e 65 74 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  etc..*/.static i
39c3f 6e 74 20 72 74 72 65 65 42 65 73 74 49 6e 64 65  nt rtreeBestInde
39c40 78 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  x(sqlite3_vtab *
39c41 74 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64  tab, sqlite3_ind
39c42 65 78 5f 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66  ex_info *pIdxInf
39c43 6f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  o){.  int rc = S
39c44 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
39c45 69 69 2c 20 63 43 6f 6c 3b 0a 0a 20 20 69 6e 74  ii, cCol;..  int
39c46 20 69 49 64 78 20 3d 20 30 3b 0a 20 20 63 68 61   iIdx = 0;.  cha
39c47 72 20 7a 49 64 78 53 74 72 5b 52 54 52 45 45 5f  r zIdxStr[RTREE_
39c48 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a 38  MAX_DIMENSIONS*8
39c49 2b 31 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 49  +1];.  memset(zI
39c4a 64 78 53 74 72 2c 20 30 2c 20 73 69 7a 65 6f 66  dxStr, 0, sizeof
39c4b 28 7a 49 64 78 53 74 72 29 29 3b 0a 0a 20 20 61  (zIdxStr));..  a
39c4c 73 73 65 72 74 28 20 70 49 64 78 49 6e 66 6f 2d  ssert( pIdxInfo-
39c4d 3e 69 64 78 53 74 72 3d 3d 30 20 29 3b 0a 20 20  >idxStr==0 );.  
39c4e 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 49 64  for(ii=0; ii<pId
39c4f 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69  xInfo->nConstrai
39c50 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 73  nt; ii++){.    s
39c51 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
39c52 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a  dex_constraint *
39c53 70 20 3d 20 26 70 49 64 78 49 6e 66 6f 2d 3e 61  p = &pIdxInfo->a
39c54 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69 5d 3b 0a  Constraint[ii];.
39c55 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 73 61 62  .    if( p->usab
39c56 6c 65 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e  le && p->iColumn
39c57 3d 3d 30 20 26 26 20 70 2d 3e 6f 70 3d 3d 53 51  ==0 && p->op==SQ
39c58 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
39c59 52 41 49 4e 54 5f 45 51 20 29 7b 0a 20 20 20 20  RAINT_EQ ){.    
39c5a 20 20 2f 2a 20 57 65 20 68 61 76 65 20 61 6e 20    /* We have an 
39c5b 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
39c5c 69 6e 74 20 6f 6e 20 74 68 65 20 72 6f 77 69 64  int on the rowid
39c5d 2e 20 55 73 65 20 73 74 72 61 74 65 67 79 20 31  . Use strategy 1
39c5e 2e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a  . */.      int j
39c5f 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d  j;.      for(jj=
39c60 30 3b 20 6a 6a 3c 69 69 3b 20 6a 6a 2b 2b 29 7b  0; jj<ii; jj++){
39c61 0a 20 20 20 20 20 20 20 20 70 49 64 78 49 6e 66  .        pIdxInf
39c62 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  o->aConstraintUs
39c63 61 67 65 5b 6a 6a 5d 2e 61 72 67 76 49 6e 64 65  age[jj].argvInde
39c64 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70  x = 0;.        p
39c65 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72  IdxInfo->aConstr
39c66 61 69 6e 74 55 73 61 67 65 5b 6a 6a 5d 2e 6f 6d  aintUsage[jj].om
39c67 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  it = 0;.      }.
39c68 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e        pIdxInfo->
39c69 69 64 78 4e 75 6d 20 3d 20 31 3b 0a 20 20 20 20  idxNum = 1;.    
39c6a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e    pIdxInfo->aCon
39c6b 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 69 5d  straintUsage[ii]
39c6c 2e 61 72 67 76 49 6e 64 65 78 20 3d 20 31 3b 0a  .argvIndex = 1;.
39c6d 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e        pIdxInfo->
39c6e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
39c6f 5b 6a 6a 5d 2e 6f 6d 69 74 20 3d 20 31 3b 0a 0a  [jj].omit = 1;..
39c70 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73 74        /* This st
39c71 72 61 74 65 67 79 20 69 6e 76 6f 6c 76 65 73 20  rategy involves 
39c72 61 20 74 77 6f 20 72 6f 77 69 64 20 6c 6f 6f 6b  a two rowid look
39c73 75 70 73 20 6f 6e 20 61 6e 20 42 2d 54 72 65 65  ups on an B-Tree
39c74 20 73 74 72 75 63 74 75 72 65 73 0a 20 20 20 20   structures.    
39c75 20 20 2a 2a 20 61 6e 64 20 74 68 65 6e 20 61 20    ** and then a 
39c76 6c 69 6e 65 61 72 20 73 65 61 72 63 68 20 6f 66  linear search of
39c77 20 61 6e 20 52 2d 54 72 65 65 20 6e 6f 64 65 2e   an R-Tree node.
39c78 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20   This should be 
39c79 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 69 64  .      ** consid
39c7a 65 72 65 64 20 61 6c 6d 6f 73 74 20 61 73 20 71  ered almost as q
39c7b 75 69 63 6b 20 61 73 20 61 20 64 69 72 65 63 74  uick as a direct
39c7c 20 72 6f 77 69 64 20 6c 6f 6f 6b 75 70 20 28 66   rowid lookup (f
39c7d 6f 72 20 77 68 69 63 68 20 0a 20 20 20 20 20 20  or which .      
39c7e 2a 2a 20 73 71 6c 69 74 65 20 75 73 65 73 20 61  ** sqlite uses a
39c7f 6e 20 69 6e 74 65 72 6e 61 6c 20 63 6f 73 74 20  n internal cost 
39c80 6f 66 20 30 2e 30 29 2e 0a 20 20 20 20 20 20 2a  of 0.0)..      *
39c81 2f 20 0a 20 20 20 20 20 20 70 49 64 78 49 6e 66  / .      pIdxInf
39c82 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74  o->estimatedCost
39c83 20 3d 20 31 30 2e 30 3b 0a 20 20 20 20 20 20 72   = 10.0;.      r
39c84 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
39c85 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
39c86 70 2d 3e 75 73 61 62 6c 65 20 26 26 20 70 2d 3e  p->usable && p->
39c87 69 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20  iColumn>0 ){.   
39c88 20 20 20 75 38 20 6f 70 20 3d 20 30 3b 0a 20 20     u8 op = 0;.  
39c89 20 20 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f      switch( p->o
39c8a 70 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73  p ){.        cas
39c8b 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  e SQLITE_INDEX_C
39c8c 4f 4e 53 54 52 41 49 4e 54 5f 45 51 3a 20 6f 70  ONSTRAINT_EQ: op
39c8d 20 3d 20 52 54 52 45 45 5f 45 51 3b 20 62 72 65   = RTREE_EQ; bre
39c8e 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
39c8f 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f   SQLITE_INDEX_CO
39c90 4e 53 54 52 41 49 4e 54 5f 47 54 3a 20 6f 70 20  NSTRAINT_GT: op 
39c91 3d 20 52 54 52 45 45 5f 47 54 3b 20 62 72 65 61  = RTREE_GT; brea
39c92 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
39c93 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
39c94 53 54 52 41 49 4e 54 5f 4c 45 3a 20 6f 70 20 3d  STRAINT_LE: op =
39c95 20 52 54 52 45 45 5f 4c 45 3b 20 62 72 65 61 6b   RTREE_LE; break
39c96 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 53  ;.        case S
39c97 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
39c98 54 52 41 49 4e 54 5f 4c 54 3a 20 6f 70 20 3d 20  TRAINT_LT: op = 
39c99 52 54 52 45 45 5f 4c 54 3b 20 62 72 65 61 6b 3b  RTREE_LT; break;
39c9a 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 53 51  .        case SQ
39c9b 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
39c9c 52 41 49 4e 54 5f 47 45 3a 20 6f 70 20 3d 20 52  RAINT_GE: op = R
39c9d 54 52 45 45 5f 47 45 3b 20 62 72 65 61 6b 3b 0a  TREE_GE; break;.
39c9e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
39c9f 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20 20 20  ( op ){.        
39ca0 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69  /* Make sure thi
39ca1 73 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6e  s particular con
39ca2 73 74 72 61 69 6e 74 20 68 61 73 20 6e 6f 74 20  straint has not 
39ca3 62 65 65 6e 20 75 73 65 64 20 62 65 66 6f 72 65  been used before
39ca4 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 66 20  ..        ** If 
39ca5 69 74 20 68 61 73 20 62 65 65 6e 20 75 73 65 64  it has been used
39ca6 20 62 65 66 6f 72 65 2c 20 69 67 6e 6f 72 65 20   before, ignore 
39ca7 69 74 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  it..        **. 
39ca8 20 20 20 20 20 20 20 2a 2a 20 41 20 3c 3d 20 6f         ** A <= o
39ca9 72 20 3c 20 63 61 6e 20 62 65 20 75 73 65 64 20  r < can be used 
39caa 69 66 20 74 68 65 72 65 20 69 73 20 61 20 70 72  if there is a pr
39cab 69 6f 72 20 3e 3d 20 6f 72 20 3e 2e 0a 20 20 20  ior >= or >..   
39cac 20 20 20 20 20 2a 2a 20 41 20 3e 3d 20 6f 72 20       ** A >= or 
39cad 3e 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 66  > can be used if
39cae 20 74 68 65 72 65 20 69 73 20 61 20 70 72 69 6f   there is a prio
39caf 72 20 3c 20 6f 72 20 3c 3d 2e 0a 20 20 20 20 20  r < or <=..     
39cb0 20 20 20 2a 2a 20 41 20 3c 3d 20 6f 72 20 3c 20     ** A <= or < 
39cb1 69 73 20 64 69 73 71 75 61 6c 69 66 69 65 64 20  is disqualified 
39cb2 69 66 20 74 68 65 72 65 20 69 73 20 61 20 70 72  if there is a pr
39cb3 69 6f 72 20 3c 3d 2c 20 3c 2c 20 6f 72 20 3d 3d  ior <=, <, or ==
39cb4 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 20 3e  ..        ** A >
39cb5 3d 20 6f 72 20 3e 20 69 73 20 64 69 73 71 75 61  = or > is disqua
39cb6 6c 69 66 69 65 64 20 69 66 20 74 68 65 72 65 20  lified if there 
39cb7 69 73 20 61 20 70 72 69 6f 72 20 3e 3d 2c 20 3e  is a prior >=, >
39cb8 2c 20 6f 72 20 3d 3d 2e 0a 20 20 20 20 20 20 20  , or ==..       
39cb9 20 2a 2a 20 41 20 3d 3d 20 69 73 20 64 69 73 71   ** A == is disq
39cba 75 61 6c 69 66 65 64 20 69 66 20 74 68 65 72 65  ualifed if there
39cbb 20 69 73 20 61 6e 79 20 70 72 69 6f 72 20 63 6f   is any prior co
39cbc 6e 73 74 72 61 69 6e 74 2e 0a 20 20 20 20 20 20  nstraint..      
39cbd 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74    */.        int
39cbe 20 6a 2c 20 6f 70 6d 73 6b 3b 0a 20 20 20 20 20   j, opmsk;.     
39cbf 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
39cc0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f  unsigned char co
39cc1 6d 70 61 74 69 62 6c 65 5b 5d 20 3d 20 7b 20 30  mpatible[] = { 0
39cc2 2c 20 30 2c 20 31 2c 20 31 2c 20 32 2c 20 32 20  , 0, 1, 1, 2, 2 
39cc3 7d 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  };.        asser
39cc4 74 28 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54  t( compatible[RT
39cc5 52 45 45 5f 45 51 20 26 20 37 5d 3d 3d 30 20 29  REE_EQ & 7]==0 )
39cc6 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
39cc7 28 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52  ( compatible[RTR
39cc8 45 45 5f 4c 54 20 26 20 37 5d 3d 3d 31 20 29 3b  EE_LT & 7]==1 );
39cc9 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
39cca 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52 45   compatible[RTRE
39ccb 45 5f 4c 45 20 26 20 37 5d 3d 3d 31 20 29 3b 0a  E_LE & 7]==1 );.
39ccc 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
39ccd 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52 45 45  compatible[RTREE
39cce 5f 47 54 20 26 20 37 5d 3d 3d 32 20 29 3b 0a 20  _GT & 7]==2 );. 
39ccf 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 63         assert( c
39cd0 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52 45 45 5f  ompatible[RTREE_
39cd1 47 45 20 26 20 37 5d 3d 3d 32 20 29 3b 0a 20 20  GE & 7]==2 );.  
39cd2 20 20 20 20 20 20 63 43 6f 6c 20 3d 20 70 2d 3e        cCol = p->
39cd3 69 43 6f 6c 75 6d 6e 20 2d 20 31 20 2b 20 27 61  iColumn - 1 + 'a
39cd4 27 3b 0a 20 20 20 20 20 20 20 20 6f 70 6d 73 6b  ';.        opmsk
39cd5 20 3d 20 63 6f 6d 70 61 74 69 62 6c 65 5b 6f 70   = compatible[op
39cd6 20 26 20 37 5d 3b 0a 20 20 20 20 20 20 20 20 66   & 7];.        f
39cd7 6f 72 28 6a 3d 30 3b 20 6a 3c 69 49 64 78 3b 20  or(j=0; j<iIdx; 
39cd8 6a 2b 3d 32 29 7b 0a 20 20 20 20 20 20 20 20 20  j+=2){.         
39cd9 20 69 66 28 20 7a 49 64 78 53 74 72 5b 6a 2b 31   if( zIdxStr[j+1
39cda 5d 3d 3d 63 43 6f 6c 20 26 26 20 28 63 6f 6d 70  ]==cCol && (comp
39cdb 61 74 69 62 6c 65 5b 7a 49 64 78 53 74 72 5b 6a  atible[zIdxStr[j
39cdc 5d 20 26 20 37 5d 20 26 20 6f 70 6d 73 6b 29 21  ] & 7] & opmsk)!
39cdd 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
39cde 20 20 6f 70 20 3d 20 30 3b 0a 20 20 20 20 20 20    op = 0;.      
39cdf 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
39ce0 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
39ce1 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
39ce2 20 69 66 28 20 6f 70 20 29 7b 0a 20 20 20 20 20   if( op ){.     
39ce3 20 20 20 61 73 73 65 72 74 28 20 69 49 64 78 3c     assert( iIdx<
39ce4 73 69 7a 65 6f 66 28 7a 49 64 78 53 74 72 29 2d  sizeof(zIdxStr)-
39ce5 31 20 29 3b 0a 20 20 20 20 20 20 20 20 7a 49 64  1 );.        zId
39ce6 78 53 74 72 5b 69 49 64 78 2b 2b 5d 20 3d 20 6f  xStr[iIdx++] = o
39ce7 70 3b 0a 20 20 20 20 20 20 20 20 7a 49 64 78 53  p;.        zIdxS
39ce8 74 72 5b 69 49 64 78 2b 2b 5d 20 3d 20 63 43 6f  tr[iIdx++] = cCo
39ce9 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 64 78 49  l;.        pIdxI
39cea 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  nfo->aConstraint
39ceb 55 73 61 67 65 5b 69 69 5d 2e 61 72 67 76 49 6e  Usage[ii].argvIn
39cec 64 65 78 20 3d 20 28 69 49 64 78 2f 32 29 3b 0a  dex = (iIdx/2);.
39ced 20 20 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f          pIdxInfo
39cee 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61  ->aConstraintUsa
39cef 67 65 5b 69 69 5d 2e 6f 6d 69 74 20 3d 20 31 3b  ge[ii].omit = 1;
39cf0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
39cf1 20 7d 0a 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e   }..  pIdxInfo->
39cf2 69 64 78 4e 75 6d 20 3d 20 32 3b 0a 20 20 70 49  idxNum = 2;.  pI
39cf3 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72  dxInfo->needToFr
39cf4 65 65 49 64 78 53 74 72 20 3d 20 31 3b 0a 20 20  eeIdxStr = 1;.  
39cf5 69 66 28 20 69 49 64 78 3e 30 20 26 26 20 30 3d  if( iIdx>0 && 0=
39cf6 3d 28 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78 53  =(pIdxInfo->idxS
39cf7 74 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  tr = sqlite3_mpr
39cf8 69 6e 74 66 28 22 25 73 22 2c 20 7a 49 64 78 53  intf("%s", zIdxS
39cf9 74 72 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75  tr)) ){.    retu
39cfa 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
39cfb 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69  .  }.  assert( i
39cfc 49 64 78 3e 3d 30 20 29 3b 0a 20 20 70 49 64 78  Idx>=0 );.  pIdx
39cfd 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43  Info->estimatedC
39cfe 6f 73 74 20 3d 20 28 32 30 30 30 30 30 30 2e 30  ost = (2000000.0
39cff 20 2f 20 28 64 6f 75 62 6c 65 29 28 69 49 64 78   / (double)(iIdx
39d00 20 2b 20 31 29 29 3b 0a 20 20 72 65 74 75 72 6e   + 1));.  return
39d01 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
39d02 74 75 72 6e 20 74 68 65 20 4e 2d 64 69 6d 65 6e  turn the N-dimen
39d03 73 69 6f 6e 61 6c 20 76 6f 6c 75 6d 6e 20 6f 66  sional volumn of
39d04 20 74 68 65 20 63 65 6c 6c 20 73 74 6f 72 65 64   the cell stored
39d05 20 69 6e 20 2a 70 2e 0a 2a 2f 0a 73 74 61 74 69   in *p..*/.stati
39d06 63 20 66 6c 6f 61 74 20 63 65 6c 6c 41 72 65 61  c float cellArea
39d07 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20  (Rtree *pRtree, 
39d08 52 74 72 65 65 43 65 6c 6c 20 2a 70 29 7b 0a 20  RtreeCell *p){. 
39d09 20 66 6c 6f 61 74 20 61 72 65 61 20 3d 20 31 2e   float area = 1.
39d0a 30 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 66  0;.  int ii;.  f
39d0b 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74  or(ii=0; ii<(pRt
39d0c 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69  ree->nDim*2); ii
39d0d 2b 3d 32 29 7b 0a 20 20 20 20 61 72 65 61 20 3d  +=2){.    area =
39d0e 20 61 72 65 61 20 2a 20 28 44 43 4f 4f 52 44 28   area * (DCOORD(
39d0f 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 29  p->aCoord[ii+1])
39d10 20 2d 20 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f   - DCOORD(p->aCo
39d11 6f 72 64 5b 69 69 5d 29 29 3b 0a 20 20 7d 0a 20  ord[ii]));.  }. 
39d12 20 72 65 74 75 72 6e 20 61 72 65 61 3b 0a 7d 0a   return area;.}.
39d13 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
39d14 65 20 6d 61 72 67 69 6e 20 6c 65 6e 67 74 68 20  e margin length 
39d15 6f 66 20 63 65 6c 6c 20 70 2e 20 54 68 65 20 6d  of cell p. The m
39d16 61 72 67 69 6e 20 6c 65 6e 67 74 68 20 69 73 20  argin length is 
39d17 74 68 65 20 73 75 6d 0a 2a 2a 20 6f 66 20 74 68  the sum.** of th
39d18 65 20 6f 62 6a 65 63 74 73 20 73 69 7a 65 20 69  e objects size i
39d19 6e 20 65 61 63 68 20 64 69 6d 65 6e 73 69 6f 6e  n each dimension
39d1a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 66 6c 6f 61  ..*/.static floa
39d1b 74 20 63 65 6c 6c 4d 61 72 67 69 6e 28 52 74 72  t cellMargin(Rtr
39d1c 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
39d1d 65 43 65 6c 6c 20 2a 70 29 7b 0a 20 20 66 6c 6f  eCell *p){.  flo
39d1e 61 74 20 6d 61 72 67 69 6e 20 3d 20 30 2e 30 3b  at margin = 0.0;
39d1f 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72  .  int ii;.  for
39d20 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65  (ii=0; ii<(pRtre
39d21 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d  e->nDim*2); ii+=
39d22 32 29 7b 0a 20 20 20 20 6d 61 72 67 69 6e 20 2b  2){.    margin +
39d23 3d 20 28 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f  = (DCOORD(p->aCo
39d24 6f 72 64 5b 69 69 2b 31 5d 29 20 2d 20 44 43 4f  ord[ii+1]) - DCO
39d25 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69  ORD(p->aCoord[ii
39d26 5d 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ]));.  }.  retur
39d27 6e 20 6d 61 72 67 69 6e 3b 0a 7d 0a 0a 2f 2a 0a  n margin;.}../*.
39d28 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 75 6e 69  ** Store the uni
39d29 6f 6e 20 6f 66 20 63 65 6c 6c 73 20 70 31 20 61  on of cells p1 a
39d2a 6e 64 20 70 32 20 69 6e 20 70 31 2e 0a 2a 2f 0a  nd p2 in p1..*/.
39d2b 73 74 61 74 69 63 20 76 6f 69 64 20 63 65 6c 6c  static void cell
39d2c 55 6e 69 6f 6e 28 52 74 72 65 65 20 2a 70 52 74  Union(Rtree *pRt
39d2d 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a  ree, RtreeCell *
39d2e 70 31 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70  p1, RtreeCell *p
39d2f 32 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20  2){.  int ii;.  
39d30 69 66 28 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f  if( pRtree->eCoo
39d31 72 64 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f  rdType==RTREE_CO
39d32 4f 52 44 5f 52 45 41 4c 33 32 20 29 7b 0a 20 20  ORD_REAL32 ){.  
39d33 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28    for(ii=0; ii<(
39d34 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b  pRtree->nDim*2);
39d35 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 70   ii+=2){.      p
39d36 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 20  1->aCoord[ii].f 
39d37 3d 20 4d 49 4e 28 70 31 2d 3e 61 43 6f 6f 72 64  = MIN(p1->aCoord
39d38 5b 69 69 5d 2e 66 2c 20 70 32 2d 3e 61 43 6f 6f  [ii].f, p2->aCoo
39d39 72 64 5b 69 69 5d 2e 66 29 3b 0a 20 20 20 20 20  rd[ii].f);.     
39d3a 20 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31   p1->aCoord[ii+1
39d3b 5d 2e 66 20 3d 20 4d 41 58 28 70 31 2d 3e 61 43  ].f = MAX(p1->aC
39d3c 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 2c 20 70 32  oord[ii+1].f, p2
39d3d 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66  ->aCoord[ii+1].f
39d3e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
39d3f 7b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20  {.    for(ii=0; 
39d40 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d  ii<(pRtree->nDim
39d41 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20  *2); ii+=2){.   
39d42 20 20 20 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69     p1->aCoord[ii
39d43 5d 2e 69 20 3d 20 4d 49 4e 28 70 31 2d 3e 61 43  ].i = MIN(p1->aC
39d44 6f 6f 72 64 5b 69 69 5d 2e 69 2c 20 70 32 2d 3e  oord[ii].i, p2->
39d45 61 43 6f 6f 72 64 5b 69 69 5d 2e 69 29 3b 0a 20  aCoord[ii].i);. 
39d46 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f 72 64 5b       p1->aCoord[
39d47 69 69 2b 31 5d 2e 69 20 3d 20 4d 41 58 28 70 31  ii+1].i = MAX(p1
39d48 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69  ->aCoord[ii+1].i
39d49 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b  , p2->aCoord[ii+
39d4a 31 5d 2e 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  1].i);.    }.  }
39d4b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
39d4c 20 74 72 75 65 20 69 66 20 74 68 65 20 61 72 65   true if the are
39d4d 61 20 63 6f 76 65 72 65 64 20 62 79 20 70 32 20  a covered by p2 
39d4e 69 73 20 61 20 73 75 62 73 65 74 20 6f 66 20 74  is a subset of t
39d4f 68 65 20 61 72 65 61 20 63 6f 76 65 72 65 64 0a  he area covered.
39d50 2a 2a 20 62 79 20 70 31 2e 20 46 61 6c 73 65 20  ** by p1. False 
39d51 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74  otherwise..*/.st
39d52 61 74 69 63 20 69 6e 74 20 63 65 6c 6c 43 6f 6e  atic int cellCon
39d53 74 61 69 6e 73 28 52 74 72 65 65 20 2a 70 52 74  tains(Rtree *pRt
39d54 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a  ree, RtreeCell *
39d55 70 31 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70  p1, RtreeCell *p
39d56 32 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20  2){.  int ii;.  
39d57 69 6e 74 20 69 73 49 6e 74 20 3d 20 28 70 52 74  int isInt = (pRt
39d58 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d  ree->eCoordType=
39d59 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54  =RTREE_COORD_INT
39d5a 33 32 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b  32);.  for(ii=0;
39d5b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69   ii<(pRtree->nDi
39d5c 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20  m*2); ii+=2){.  
39d5d 20 20 52 74 72 65 65 43 6f 6f 72 64 20 2a 61 31    RtreeCoord *a1
39d5e 20 3d 20 26 70 31 2d 3e 61 43 6f 6f 72 64 5b 69   = &p1->aCoord[i
39d5f 69 5d 3b 0a 20 20 20 20 52 74 72 65 65 43 6f 6f  i];.    RtreeCoo
39d60 72 64 20 2a 61 32 20 3d 20 26 70 32 2d 3e 61 43  rd *a2 = &p2->aC
39d61 6f 6f 72 64 5b 69 69 5d 3b 0a 20 20 20 20 69 66  oord[ii];.    if
39d62 28 20 28 21 69 73 49 6e 74 20 26 26 20 28 61 32  ( (!isInt && (a2
39d63 5b 30 5d 2e 66 3c 61 31 5b 30 5d 2e 66 20 7c 7c  [0].f<a1[0].f ||
39d64 20 61 32 5b 31 5d 2e 66 3e 61 31 5b 31 5d 2e 66   a2[1].f>a1[1].f
39d65 29 29 20 0a 20 20 20 20 20 7c 7c 20 28 20 69 73  )) .     || ( is
39d66 49 6e 74 20 26 26 20 28 61 32 5b 30 5d 2e 69 3c  Int && (a2[0].i<
39d67 61 31 5b 30 5d 2e 69 20 7c 7c 20 61 32 5b 31 5d  a1[0].i || a2[1]
39d68 2e 69 3e 61 31 5b 31 5d 2e 69 29 29 20 0a 20 20  .i>a1[1].i)) .  
39d69 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72    ){.      retur
39d6a 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 0;.    }.  }. 
39d6b 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
39d6c 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61  .** Return the a
39d6d 6d 6f 75 6e 74 20 63 65 6c 6c 20 70 20 77 6f 75  mount cell p wou
39d6e 6c 64 20 67 72 6f 77 20 62 79 20 69 66 20 69 74  ld grow by if it
39d6f 20 77 65 72 65 20 75 6e 69 6f 6e 65 64 20 77 69   were unioned wi
39d70 74 68 20 70 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61  th pCell..*/.sta
39d71 74 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c 47 72  tic float cellGr
39d72 6f 77 74 68 28 52 74 72 65 65 20 2a 70 52 74 72  owth(Rtree *pRtr
39d73 65 65 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70  ee, RtreeCell *p
39d74 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65  , RtreeCell *pCe
39d75 6c 6c 29 7b 0a 20 20 66 6c 6f 61 74 20 61 72 65  ll){.  float are
39d76 61 3b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 63  a;.  RtreeCell c
39d77 65 6c 6c 3b 0a 20 20 6d 65 6d 63 70 79 28 26 63  ell;.  memcpy(&c
39d78 65 6c 6c 2c 20 70 2c 20 73 69 7a 65 6f 66 28 52  ell, p, sizeof(R
39d79 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 61 72  treeCell));.  ar
39d7a 65 61 20 3d 20 63 65 6c 6c 41 72 65 61 28 70 52  ea = cellArea(pR
39d7b 74 72 65 65 2c 20 26 63 65 6c 6c 29 3b 0a 20 20  tree, &cell);.  
39d7c 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65  cellUnion(pRtree
39d7d 2c 20 26 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 3b  , &cell, pCell);
39d7e 0a 20 20 72 65 74 75 72 6e 20 28 63 65 6c 6c 41  .  return (cellA
39d7f 72 65 61 28 70 52 74 72 65 65 2c 20 26 63 65 6c  rea(pRtree, &cel
39d80 6c 29 2d 61 72 65 61 29 3b 0a 7d 0a 0a 23 69 66  l)-area);.}..#if
39d81 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52   VARIANT_RSTARTR
39d82 45 45 5f 43 48 4f 4f 53 45 53 55 42 54 52 45 45  EE_CHOOSESUBTREE
39d83 20 7c 7c 20 56 41 52 49 41 4e 54 5f 52 53 54 41   || VARIANT_RSTA
39d84 52 54 52 45 45 5f 53 50 4c 49 54 0a 73 74 61 74  RTREE_SPLIT.stat
39d85 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c 4f 76 65  ic float cellOve
39d86 72 6c 61 70 28 0a 20 20 52 74 72 65 65 20 2a 70  rlap(.  Rtree *p
39d87 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 43  Rtree, .  RtreeC
39d88 65 6c 6c 20 2a 70 2c 20 0a 20 20 52 74 72 65 65  ell *p, .  Rtree
39d89 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20  Cell *aCell, .  
39d8a 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e  int nCell, .  in
39d8b 74 20 69 45 78 63 6c 75 64 65 0a 29 7b 0a 20 20  t iExclude.){.  
39d8c 69 6e 74 20 69 69 3b 0a 20 20 66 6c 6f 61 74 20  int ii;.  float 
39d8d 6f 76 65 72 6c 61 70 20 3d 20 30 2e 30 3b 0a 20  overlap = 0.0;. 
39d8e 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43   for(ii=0; ii<nC
39d8f 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  ell; ii++){.    
39d90 69 66 28 20 69 69 21 3d 69 45 78 63 6c 75 64 65  if( ii!=iExclude
39d91 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 6a   ){.      int jj
39d92 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 6f 20  ;.      float o 
39d93 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20 66 6f 72  = 1.0;.      for
39d94 28 6a 6a 3d 30 3b 20 6a 6a 3c 28 70 52 74 72 65  (jj=0; jj<(pRtre
39d95 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 6a 6a 2b 3d  e->nDim*2); jj+=
39d96 32 29 7b 0a 20 20 20 20 20 20 20 20 64 6f 75 62  2){.        doub
39d97 6c 65 20 78 31 3b 0a 20 20 20 20 20 20 20 20 64  le x1;.        d
39d98 6f 75 62 6c 65 20 78 32 3b 0a 0a 20 20 20 20 20  ouble x2;..     
39d99 20 20 20 78 31 20 3d 20 4d 41 58 28 44 43 4f 4f     x1 = MAX(DCOO
39d9a 52 44 28 70 2d 3e 61 43 6f 6f 72 64 5b 6a 6a 5d  RD(p->aCoord[jj]
39d9b 29 2c 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b  ), DCOORD(aCell[
39d9c 69 69 5d 2e 61 43 6f 6f 72 64 5b 6a 6a 5d 29 29  ii].aCoord[jj]))
39d9d 3b 0a 20 20 20 20 20 20 20 20 78 32 20 3d 20 4d  ;.        x2 = M
39d9e 49 4e 28 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f  IN(DCOORD(p->aCo
39d9f 6f 72 64 5b 6a 6a 2b 31 5d 29 2c 20 44 43 4f 4f  ord[jj+1]), DCOO
39da0 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f  RD(aCell[ii].aCo
39da1 6f 72 64 5b 6a 6a 2b 31 5d 29 29 3b 0a 0a 20 20  ord[jj+1]));..  
39da2 20 20 20 20 20 20 69 66 28 20 78 32 3c 78 31 20        if( x2<x1 
39da3 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6f 20 3d  ){.          o =
39da4 20 30 2e 30 3b 0a 20 20 20 20 20 20 20 20 20 20   0.0;.          
39da5 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
39da6 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
39da7 6f 20 3d 20 6f 20 2a 20 28 78 32 2d 78 31 29 3b  o = o * (x2-x1);
39da8 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
39da9 20 7d 0a 20 20 20 20 20 20 6f 76 65 72 6c 61 70   }.      overlap
39daa 20 2b 3d 20 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d   += o;.    }.  }
39dab 0a 20 20 72 65 74 75 72 6e 20 6f 76 65 72 6c 61  .  return overla
39dac 70 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  p;.}.#endif..#if
39dad 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52   VARIANT_RSTARTR
39dae 45 45 5f 43 48 4f 4f 53 45 53 55 42 54 52 45 45  EE_CHOOSESUBTREE
39daf 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20 63 65  .static float ce
39db0 6c 6c 4f 76 65 72 6c 61 70 45 6e 6c 61 72 67 65  llOverlapEnlarge
39db1 6d 65 6e 74 28 0a 20 20 52 74 72 65 65 20 2a 70  ment(.  Rtree *p
39db2 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 43  Rtree, .  RtreeC
39db3 65 6c 6c 20 2a 70 2c 20 0a 20 20 52 74 72 65 65  ell *p, .  Rtree
39db4 43 65 6c 6c 20 2a 70 49 6e 73 65 72 74 2c 20 0a  Cell *pInsert, .
39db5 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65    RtreeCell *aCe
39db6 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  ll, .  int nCell
39db7 2c 20 0a 20 20 69 6e 74 20 69 45 78 63 6c 75 64  , .  int iExclud
39db8 65 0a 29 7b 0a 20 20 66 6c 6f 61 74 20 62 65 66  e.){.  float bef
39db9 6f 72 65 3b 0a 20 20 66 6c 6f 61 74 20 61 66 74  ore;.  float aft
39dba 65 72 3b 0a 20 20 62 65 66 6f 72 65 20 3d 20 63  er;.  before = c
39dbb 65 6c 6c 4f 76 65 72 6c 61 70 28 70 52 74 72 65  ellOverlap(pRtre
39dbc 65 2c 20 70 2c 20 61 43 65 6c 6c 2c 20 6e 43 65  e, p, aCell, nCe
39dbd 6c 6c 2c 20 69 45 78 63 6c 75 64 65 29 3b 0a 20  ll, iExclude);. 
39dbe 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65   cellUnion(pRtre
39dbf 65 2c 20 70 2c 20 70 49 6e 73 65 72 74 29 3b 0a  e, p, pInsert);.
39dc0 20 20 61 66 74 65 72 20 3d 20 63 65 6c 6c 4f 76    after = cellOv
39dc1 65 72 6c 61 70 28 70 52 74 72 65 65 2c 20 70 2c  erlap(pRtree, p,
39dc2 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 69   aCell, nCell, i
39dc3 45 78 63 6c 75 64 65 29 3b 0a 20 20 72 65 74 75  Exclude);.  retu
39dc4 72 6e 20 61 66 74 65 72 2d 62 65 66 6f 72 65 3b  rn after-before;
39dc5 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  .}.#endif.../*.*
39dc6 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
39dc7 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 43  implements the C
39dc8 68 6f 6f 73 65 4c 65 61 66 20 61 6c 67 6f 72 69  hooseLeaf algori
39dc9 74 68 6d 20 66 72 6f 6d 20 47 75 74 6d 61 6e 5b  thm from Gutman[
39dca 38 34 5d 2e 0a 2a 2a 20 43 68 6f 6f 73 65 53 75  84]..** ChooseSu
39dcb 62 54 72 65 65 20 69 6e 20 72 2a 74 72 65 65 20  bTree in r*tree 
39dcc 74 65 72 6d 69 6e 6f 6c 6f 67 79 2e 0a 2a 2f 0a  terminology..*/.
39dcd 73 74 61 74 69 63 20 69 6e 74 20 43 68 6f 6f 73  static int Choos
39dce 65 4c 65 61 66 28 0a 20 20 52 74 72 65 65 20 2a  eLeaf(.  Rtree *
39dcf 70 52 74 72 65 65 2c 20 20 20 20 20 20 20 20 20  pRtree,         
39dd0 20 20 20 20 20 20 2f 2a 20 52 74 72 65 65 20 74        /* Rtree t
39dd1 61 62 6c 65 20 2a 2f 0a 20 20 52 74 72 65 65 43  able */.  RtreeC
39dd2 65 6c 6c 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20  ell *pCell,     
39dd3 20 20 20 20 20 20 20 2f 2a 20 43 65 6c 6c 20 74         /* Cell t
39dd4 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 72 74  o insert into rt
39dd5 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 48 65  ree */.  int iHe
39dd6 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20  ight,           
39dd7 20 20 20 20 20 20 2f 2a 20 48 65 69 67 68 74 20        /* Height 
39dd8 6f 66 20 73 75 62 2d 74 72 65 65 20 72 6f 6f 74  of sub-tree root
39dd9 65 64 20 61 74 20 70 43 65 6c 6c 20 2a 2f 0a 20  ed at pCell */. 
39dda 20 52 74 72 65 65 4e 6f 64 65 20 2a 2a 70 70 4c   RtreeNode **ppL
39ddb 65 61 66 20 20 20 20 20 20 20 20 20 20 20 2f 2a  eaf           /*
39ddc 20 4f 55 54 3a 20 53 65 6c 65 63 74 65 64 20 6c   OUT: Selected l
39ddd 65 61 66 20 70 61 67 65 20 2a 2f 0a 29 7b 0a 20  eaf page */.){. 
39dde 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69   int rc;.  int i
39ddf 69 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  i;.  RtreeNode *
39de0 70 4e 6f 64 65 3b 0a 20 20 72 63 20 3d 20 6e 6f  pNode;.  rc = no
39de1 64 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65  deAcquire(pRtree
39de2 2c 20 31 2c 20 30 2c 20 26 70 4e 6f 64 65 29 3b  , 1, 0, &pNode);
39de3 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 72 63  ..  for(ii=0; rc
39de4 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
39de5 69 3c 28 70 52 74 72 65 65 2d 3e 69 44 65 70 74  i<(pRtree->iDept
39de6 68 2d 69 48 65 69 67 68 74 29 3b 20 69 69 2b 2b  h-iHeight); ii++
39de7 29 7b 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c  ){.    int iCell
39de8 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e  ;.    sqlite3_in
39de9 74 36 34 20 69 42 65 73 74 3b 0a 0a 20 20 20 20  t64 iBest;..    
39dea 66 6c 6f 61 74 20 66 4d 69 6e 47 72 6f 77 74 68  float fMinGrowth
39deb 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 4d 69 6e  ;.    float fMin
39dec 41 72 65 61 3b 0a 20 20 20 20 66 6c 6f 61 74 20  Area;.    float 
39ded 66 4d 69 6e 4f 76 65 72 6c 61 70 3b 0a 0a 20 20  fMinOverlap;..  
39dee 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43    int nCell = NC
39def 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20  ELL(pNode);.    
39df0 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a  RtreeCell cell;.
39df1 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70      RtreeNode *p
39df2 43 68 69 6c 64 3b 0a 0a 20 20 20 20 52 74 72 65  Child;..    Rtre
39df3 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 20 3d 20 30  eCell *aCell = 0
39df4 3b 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52  ;..#if VARIANT_R
39df5 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45 53  STARTREE_CHOOSES
39df6 55 42 54 52 45 45 0a 20 20 20 20 69 66 28 20 69  UBTREE.    if( i
39df7 69 3d 3d 28 70 52 74 72 65 65 2d 3e 69 44 65 70  i==(pRtree->iDep
39df8 74 68 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 69  th-1) ){.      i
39df9 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20 61 43 65  nt jj;.      aCe
39dfa 6c 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ll = sqlite3_mal
39dfb 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65  loc(sizeof(Rtree
39dfc 43 65 6c 6c 29 2a 6e 43 65 6c 6c 29 3b 0a 20 20  Cell)*nCell);.  
39dfd 20 20 20 20 69 66 28 20 21 61 43 65 6c 6c 20 29      if( !aCell )
39dfe 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  {.        rc = S
39dff 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
39e00 20 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65       nodeRelease
39e01 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b  (pRtree, pNode);
39e02 0a 20 20 20 20 20 20 20 20 70 4e 6f 64 65 20 3d  .        pNode =
39e03 20 30 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74   0;.        cont
39e04 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  inue;.      }.  
39e05 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a      for(jj=0; jj
39e06 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0a 20  <nCell; jj++){. 
39e07 20 20 20 20 20 20 20 6e 6f 64 65 47 65 74 43 65         nodeGetCe
39e08 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
39e09 2c 20 6a 6a 2c 20 26 61 43 65 6c 6c 5b 6a 6a 5d  , jj, &aCell[jj]
39e0a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
39e0b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20  .#endif..    /* 
39e0c 53 65 6c 65 63 74 20 74 68 65 20 63 68 69 6c 64  Select the child
39e0d 20 6e 6f 64 65 20 77 68 69 63 68 20 77 69 6c 6c   node which will
39e0e 20 62 65 20 65 6e 6c 61 72 67 65 64 20 74 68 65   be enlarged the
39e0f 20 6c 65 61 73 74 20 69 66 20 70 43 65 6c 6c 0a   least if pCell.
39e10 20 20 20 20 2a 2a 20 69 73 20 69 6e 73 65 72 74      ** is insert
39e11 65 64 20 69 6e 74 6f 20 69 74 2e 20 52 65 73 6f  ed into it. Reso
39e12 6c 76 65 20 74 69 65 73 20 62 79 20 63 68 6f 6f  lve ties by choo
39e13 73 69 6e 67 20 74 68 65 20 65 6e 74 72 79 20 77  sing the entry w
39e14 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 73  ith.    ** the s
39e15 6d 61 6c 6c 65 73 74 20 61 72 65 61 2e 0a 20 20  mallest area..  
39e16 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 43 65    */.    for(iCe
39e17 6c 6c 3d 30 3b 20 69 43 65 6c 6c 3c 6e 43 65 6c  ll=0; iCell<nCel
39e18 6c 3b 20 69 43 65 6c 6c 2b 2b 29 7b 0a 20 20 20  l; iCell++){.   
39e19 20 20 20 66 6c 6f 61 74 20 67 72 6f 77 74 68 3b     float growth;
39e1a 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 61 72 65  .      float are
39e1b 61 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 6f  a;.      float o
39e1c 76 65 72 6c 61 70 20 3d 20 30 2e 30 3b 0a 20 20  verlap = 0.0;.  
39e1d 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28      nodeGetCell(
39e1e 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69  pRtree, pNode, i
39e1f 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b 0a 20 20  Cell, &cell);.  
39e20 20 20 20 20 67 72 6f 77 74 68 20 3d 20 63 65 6c      growth = cel
39e21 6c 47 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20  lGrowth(pRtree, 
39e22 26 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 3b 0a 20  &cell, pCell);. 
39e23 20 20 20 20 20 61 72 65 61 20 3d 20 63 65 6c 6c       area = cell
39e24 41 72 65 61 28 70 52 74 72 65 65 2c 20 26 63 65  Area(pRtree, &ce
39e25 6c 6c 29 3b 0a 23 69 66 20 56 41 52 49 41 4e 54  ll);.#if VARIANT
39e26 5f 52 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53  _RSTARTREE_CHOOS
39e27 45 53 55 42 54 52 45 45 0a 20 20 20 20 20 20 69  ESUBTREE.      i
39e28 66 28 20 69 69 3d 3d 28 70 52 74 72 65 65 2d 3e  f( ii==(pRtree->
39e29 69 44 65 70 74 68 2d 31 29 20 29 7b 0a 20 20 20  iDepth-1) ){.   
39e2a 20 20 20 20 20 6f 76 65 72 6c 61 70 20 3d 20 63       overlap = c
39e2b 65 6c 6c 4f 76 65 72 6c 61 70 45 6e 6c 61 72 67  ellOverlapEnlarg
39e2c 65 6d 65 6e 74 28 70 52 74 72 65 65 2c 26 63 65  ement(pRtree,&ce
39e2d 6c 6c 2c 70 43 65 6c 6c 2c 61 43 65 6c 6c 2c 6e  ll,pCell,aCell,n
39e2e 43 65 6c 6c 2c 69 43 65 6c 6c 29 3b 0a 20 20 20  Cell,iCell);.   
39e2f 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
39e30 20 20 69 66 28 20 28 69 43 65 6c 6c 3d 3d 30 29    if( (iCell==0)
39e31 20 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f 76 65   .       || (ove
39e32 72 6c 61 70 3c 66 4d 69 6e 4f 76 65 72 6c 61 70  rlap<fMinOverlap
39e33 29 20 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f 76  ) .       || (ov
39e34 65 72 6c 61 70 3d 3d 66 4d 69 6e 4f 76 65 72 6c  erlap==fMinOverl
39e35 61 70 20 26 26 20 67 72 6f 77 74 68 3c 66 4d 69  ap && growth<fMi
39e36 6e 47 72 6f 77 74 68 29 0a 20 20 20 20 20 20 20  nGrowth).       
39e37 7c 7c 20 28 6f 76 65 72 6c 61 70 3d 3d 66 4d 69  || (overlap==fMi
39e38 6e 4f 76 65 72 6c 61 70 20 26 26 20 67 72 6f 77  nOverlap && grow
39e39 74 68 3d 3d 66 4d 69 6e 47 72 6f 77 74 68 20 26  th==fMinGrowth &
39e3a 26 20 61 72 65 61 3c 66 4d 69 6e 41 72 65 61 29  & area<fMinArea)
39e3b 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
39e3c 20 20 66 4d 69 6e 4f 76 65 72 6c 61 70 20 3d 20    fMinOverlap = 
39e3d 6f 76 65 72 6c 61 70 3b 0a 20 20 20 20 20 20 20  overlap;.       
39e3e 20 66 4d 69 6e 47 72 6f 77 74 68 20 3d 20 67 72   fMinGrowth = gr
39e3f 6f 77 74 68 3b 0a 20 20 20 20 20 20 20 20 66 4d  owth;.        fM
39e40 69 6e 41 72 65 61 20 3d 20 61 72 65 61 3b 0a 20  inArea = area;. 
39e41 20 20 20 20 20 20 20 69 42 65 73 74 20 3d 20 63         iBest = c
39e42 65 6c 6c 2e 69 52 6f 77 69 64 3b 0a 20 20 20 20  ell.iRowid;.    
39e43 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73    }.    }..    s
39e44 71 6c 69 74 65 33 5f 66 72 65 65 28 61 43 65 6c  qlite3_free(aCel
39e45 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64  l);.    rc = nod
39e46 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65 2c  eAcquire(pRtree,
39e47 20 69 42 65 73 74 2c 20 70 4e 6f 64 65 2c 20 26   iBest, pNode, &
39e48 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 6e 6f 64  pChild);.    nod
39e49 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
39e4a 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 70 4e 6f   pNode);.    pNo
39e4b 64 65 20 3d 20 70 43 68 69 6c 64 3b 0a 20 20 7d  de = pChild;.  }
39e4c 0a 0a 20 20 2a 70 70 4c 65 61 66 20 3d 20 70 4e  ..  *ppLeaf = pN
39e4d 6f 64 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ode;.  return rc
39e4e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 65 6c  ;.}../*.** A cel
39e4f 6c 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20  l with the same 
39e50 63 6f 6e 74 65 6e 74 20 61 73 20 70 43 65 6c 6c  content as pCell
39e51 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 69   has just been i
39e52 6e 73 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20  nserted into.** 
39e53 74 68 65 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20  the node pNode. 
39e54 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 75 70  This function up
39e55 64 61 74 65 73 20 74 68 65 20 62 6f 75 6e 64 69  dates the boundi
39e56 6e 67 20 62 6f 78 20 63 65 6c 6c 73 20 69 6e 0a  ng box cells in.
39e57 2a 2a 20 61 6c 6c 20 61 6e 63 65 73 74 6f 72 20  ** all ancestor 
39e58 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61  elements..*/.sta
39e59 74 69 63 20 76 6f 69 64 20 41 64 6a 75 73 74 54  tic void AdjustT
39e5a 72 65 65 28 0a 20 20 52 74 72 65 65 20 2a 70 52  ree(.  Rtree *pR
39e5b 74 72 65 65 2c 20 20 20 20 20 20 20 20 20 20 20  tree,           
39e5c 20 20 20 20 20 20 20 20 20 2f 2a 20 52 74 72 65           /* Rtre
39e5d 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 52 74 72  e table */.  Rtr
39e5e 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 20  eeNode *pNode,  
39e5f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
39e60 2a 20 41 64 6a 75 73 74 20 61 6e 63 65 73 74 72  * Adjust ancestr
39e61 79 20 6f 66 20 74 68 69 73 20 6e 6f 64 65 2e 20  y of this node. 
39e62 2a 2f 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  */.  RtreeCell *
39e63 70 43 65 6c 6c 20 20 20 20 20 20 20 20 20 20 20  pCell           
39e64 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63         /* This c
39e65 65 6c 6c 20 77 61 73 20 6a 75 73 74 20 69 6e 73  ell was just ins
39e66 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 52 74  erted */.){.  Rt
39e67 72 65 65 4e 6f 64 65 20 2a 70 20 3d 20 70 4e 6f  reeNode *p = pNo
39e68 64 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e  de;.  while( p->
39e69 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 52  pParent ){.    R
39e6a 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20  treeCell cell;. 
39e6b 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50     RtreeNode *pP
39e6c 61 72 65 6e 74 20 3d 20 70 2d 3e 70 50 61 72 65  arent = p->pPare
39e6d 6e 74 3b 0a 20 20 20 20 69 6e 74 20 69 43 65 6c  nt;.    int iCel
39e6e 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e  l = nodeParentIn
39e6f 64 65 78 28 70 52 74 72 65 65 2c 20 70 29 3b 0a  dex(pRtree, p);.
39e70 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c  .    nodeGetCell
39e71 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74  (pRtree, pParent
39e72 2c 20 69 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b  , iCell, &cell);
39e73 0a 20 20 20 20 69 66 28 20 21 63 65 6c 6c 43 6f  .    if( !cellCo
39e74 6e 74 61 69 6e 73 28 70 52 74 72 65 65 2c 20 26  ntains(pRtree, &
39e75 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 20 29 7b 0a  cell, pCell) ){.
39e76 20 20 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28        cellUnion(
39e77 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70  pRtree, &cell, p
39e78 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 6e 6f 64  Cell);.      nod
39e79 65 4f 76 65 72 77 72 69 74 65 43 65 6c 6c 28 70  eOverwriteCell(p
39e7a 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c 20  Rtree, pParent, 
39e7b 26 63 65 6c 6c 2c 20 69 43 65 6c 6c 29 3b 0a 20  &cell, iCell);. 
39e7c 20 20 20 7d 0a 20 0a 20 20 20 20 70 20 3d 20 70     }. .    p = p
39e7d 50 61 72 65 6e 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f  Parent;.  }.}../
39e7e 2a 0a 2a 2a 20 57 72 69 74 65 20 6d 61 70 70 69  *.** Write mappi
39e7f 6e 67 20 28 69 52 6f 77 69 64 2d 3e 69 4e 6f 64  ng (iRowid->iNod
39e80 65 29 20 74 6f 20 74 68 65 20 3c 72 74 72 65 65  e) to the <rtree
39e81 3e 5f 72 6f 77 69 64 20 74 61 62 6c 65 2e 0a 2a  >_rowid table..*
39e82 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 6f 77  /.static int row
39e83 69 64 57 72 69 74 65 28 52 74 72 65 65 20 2a 70  idWrite(Rtree *p
39e84 52 74 72 65 65 2c 20 73 71 6c 69 74 65 33 5f 69  Rtree, sqlite3_i
39e85 6e 74 36 34 20 69 52 6f 77 69 64 2c 20 73 71 6c  nt64 iRowid, sql
39e86 69 74 65 33 5f 69 6e 74 36 34 20 69 4e 6f 64 65  ite3_int64 iNode
39e87 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  ){.  sqlite3_bin
39e88 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e  d_int64(pRtree->
39e89 70 57 72 69 74 65 52 6f 77 69 64 2c 20 31 2c 20  pWriteRowid, 1, 
39e8a 69 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c 69 74  iRowid);.  sqlit
39e8b 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52  e3_bind_int64(pR
39e8c 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69  tree->pWriteRowi
39e8d 64 2c 20 32 2c 20 69 4e 6f 64 65 29 3b 0a 20 20  d, 2, iNode);.  
39e8e 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74  sqlite3_step(pRt
39e8f 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64  ree->pWriteRowid
39e90 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  );.  return sqli
39e91 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65  te3_reset(pRtree
39e92 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29 3b 0a  ->pWriteRowid);.
39e93 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d  }../*.** Write m
39e94 61 70 70 69 6e 67 20 28 69 4e 6f 64 65 2d 3e 69  apping (iNode->i
39e95 50 61 72 29 20 74 6f 20 74 68 65 20 3c 72 74 72  Par) to the <rtr
39e96 65 65 3e 5f 70 61 72 65 6e 74 20 74 61 62 6c 65  ee>_parent table
39e97 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
39e98 70 61 72 65 6e 74 57 72 69 74 65 28 52 74 72 65  parentWrite(Rtre
39e99 65 20 2a 70 52 74 72 65 65 2c 20 73 71 6c 69 74  e *pRtree, sqlit
39e9a 65 33 5f 69 6e 74 36 34 20 69 4e 6f 64 65 2c 20  e3_int64 iNode, 
39e9b 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 50  sqlite3_int64 iP
39e9c 61 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62  ar){.  sqlite3_b
39e9d 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65  ind_int64(pRtree
39e9e 2d 3e 70 57 72 69 74 65 50 61 72 65 6e 74 2c 20  ->pWriteParent, 
39e9f 31 2c 20 69 4e 6f 64 65 29 3b 0a 20 20 73 71 6c  1, iNode);.  sql
39ea0 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
39ea1 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50 61  pRtree->pWritePa
39ea2 72 65 6e 74 2c 20 32 2c 20 69 50 61 72 29 3b 0a  rent, 2, iPar);.
39ea3 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70    sqlite3_step(p
39ea4 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72  Rtree->pWritePar
39ea5 65 6e 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 73  ent);.  return s
39ea6 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74  qlite3_reset(pRt
39ea7 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65 6e  ree->pWriteParen
39ea8 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  t);.}..static in
39ea9 74 20 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c  t rtreeInsertCel
39eaa 6c 28 52 74 72 65 65 20 2a 2c 20 52 74 72 65 65  l(Rtree *, Rtree
39eab 4e 6f 64 65 20 2a 2c 20 52 74 72 65 65 43 65 6c  Node *, RtreeCel
39eac 6c 20 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69 66 20  l *, int);..#if 
39ead 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f  VARIANT_GUTTMAN_
39eae 4c 49 4e 45 41 52 5f 53 50 4c 49 54 0a 2f 2a 0a  LINEAR_SPLIT./*.
39eaf 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
39eb0 6e 20 6f 66 20 74 68 65 20 6c 69 6e 65 61 72 20  n of the linear 
39eb1 76 61 72 69 61 6e 74 20 6f 66 20 74 68 65 20 50  variant of the P
39eb2 69 63 6b 4e 65 78 74 28 29 20 66 75 6e 63 74 69  ickNext() functi
39eb3 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d  on from.** Guttm
39eb4 61 6e 5b 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69  an[84]..*/.stati
39eb5 63 20 52 74 72 65 65 43 65 6c 6c 20 2a 4c 69 6e  c RtreeCell *Lin
39eb6 65 61 72 50 69 63 6b 4e 65 78 74 28 0a 20 20 52  earPickNext(.  R
39eb7 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20  tree *pRtree,.  
39eb8 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c  RtreeCell *aCell
39eb9 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20  , .  int nCell, 
39eba 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 4c  .  RtreeCell *pL
39ebb 65 66 74 42 6f 78 2c 20 0a 20 20 52 74 72 65 65  eftBox, .  Rtree
39ebc 43 65 6c 6c 20 2a 70 52 69 67 68 74 42 6f 78 2c  Cell *pRightBox,
39ebd 0a 20 20 69 6e 74 20 2a 61 69 55 73 65 64 0a 29  .  int *aiUsed.)
39ebe 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f  {.  int ii;.  fo
39ebf 72 28 69 69 3d 30 3b 20 61 69 55 73 65 64 5b 69  r(ii=0; aiUsed[i
39ec0 69 5d 3b 20 69 69 2b 2b 29 3b 0a 20 20 61 69 55  i]; ii++);.  aiU
39ec1 73 65 64 5b 69 69 5d 20 3d 20 31 3b 0a 20 20 72  sed[ii] = 1;.  r
39ec2 65 74 75 72 6e 20 26 61 43 65 6c 6c 5b 69 69 5d  eturn &aCell[ii]
39ec3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
39ec4 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
39ec5 20 6c 69 6e 65 61 72 20 76 61 72 69 61 6e 74 20   linear variant 
39ec6 6f 66 20 74 68 65 20 50 69 63 6b 53 65 65 64 73  of the PickSeeds
39ec7 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d  () function from
39ec8 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e  .** Guttman[84].
39ec9 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
39eca 4c 69 6e 65 61 72 50 69 63 6b 53 65 65 64 73 28  LinearPickSeeds(
39ecb 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
39ecc 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61  ,.  RtreeCell *a
39ecd 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65  Cell, .  int nCe
39ece 6c 6c 2c 20 0a 20 20 69 6e 74 20 2a 70 69 4c 65  ll, .  int *piLe
39ecf 66 74 53 65 65 64 2c 20 0a 20 20 69 6e 74 20 2a  ftSeed, .  int *
39ed0 70 69 52 69 67 68 74 53 65 65 64 0a 29 7b 0a 20  piRightSeed.){. 
39ed1 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 4c   int i;.  int iL
39ed2 65 66 74 53 65 65 64 20 3d 20 30 3b 0a 20 20 69  eftSeed = 0;.  i
39ed3 6e 74 20 69 52 69 67 68 74 53 65 65 64 20 3d 20  nt iRightSeed = 
39ed4 31 3b 0a 20 20 66 6c 6f 61 74 20 6d 61 78 4e 6f  1;.  float maxNo
39ed5 72 6d 61 6c 49 6e 6e 65 72 57 69 64 74 68 20 3d  rmalInnerWidth =
39ed6 20 30 2e 30 3b 0a 0a 20 20 2f 2a 20 50 69 63 6b   0.0;..  /* Pick
39ed7 20 74 77 6f 20 22 73 65 65 64 22 20 63 65 6c 6c   two "seed" cell
39ed8 73 20 66 72 6f 6d 20 74 68 65 20 61 72 72 61 79  s from the array
39ed9 20 6f 66 20 63 65 6c 6c 73 2e 20 54 68 65 20 61   of cells. The a
39eda 6c 67 6f 72 69 74 68 6d 20 75 73 65 64 0a 20 20  lgorithm used.  
39edb 2a 2a 20 68 65 72 65 20 69 73 20 74 68 65 20 4c  ** here is the L
39edc 69 6e 65 61 72 50 69 63 6b 53 65 65 64 73 20 61  inearPickSeeds a
39edd 6c 67 6f 72 69 74 68 6d 20 66 72 6f 6d 20 47 75  lgorithm from Gu
39ede 74 6d 61 6e 5b 31 39 38 34 5d 2e 20 54 68 65 20  tman[1984]. The 
39edf 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 20 6f 66  .  ** indices of
39ee0 20 74 68 65 20 74 77 6f 20 73 65 65 64 20 63 65   the two seed ce
39ee1 6c 6c 73 20 69 6e 20 74 68 65 20 61 72 72 61 79  lls in the array
39ee2 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 6c   are stored in l
39ee3 6f 63 61 6c 0a 20 20 2a 2a 20 76 61 72 69 61 62  ocal.  ** variab
39ee4 6c 65 73 20 69 4c 65 66 74 53 65 65 6b 20 61 6e  les iLeftSeek an
39ee5 64 20 69 52 69 67 68 74 53 65 65 64 2e 0a 20 20  d iRightSeed..  
39ee6 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  */.  for(i=0; i<
39ee7 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 2b  pRtree->nDim; i+
39ee8 2b 29 7b 0a 20 20 20 20 66 6c 6f 61 74 20 78 31  +){.    float x1
39ee9 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b   = DCOORD(aCell[
39eea 30 5d 2e 61 43 6f 6f 72 64 5b 69 2a 32 5d 29 3b  0].aCoord[i*2]);
39eeb 0a 20 20 20 20 66 6c 6f 61 74 20 78 32 20 3d 20  .    float x2 = 
39eec 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 30 5d 2e  DCOORD(aCell[0].
39eed 61 43 6f 6f 72 64 5b 69 2a 32 2b 31 5d 29 3b 0a  aCoord[i*2+1]);.
39eee 20 20 20 20 66 6c 6f 61 74 20 78 33 20 3d 20 78      float x3 = x
39eef 31 3b 0a 20 20 20 20 66 6c 6f 61 74 20 78 34 20  1;.    float x4 
39ef0 3d 20 78 32 3b 0a 20 20 20 20 69 6e 74 20 6a 6a  = x2;.    int jj
39ef1 3b 0a 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c  ;..    int iCell
39ef2 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e  Left = 0;.    in
39ef3 74 20 69 43 65 6c 6c 52 69 67 68 74 20 3d 20 30  t iCellRight = 0
39ef4 3b 0a 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 31 3b  ;..    for(jj=1;
39ef5 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29   jj<nCell; jj++)
39ef6 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 6c 65  {.      float le
39ef7 66 74 20 3d 20 44 43 4f 4f 52 44 28 61 43 65 6c  ft = DCOORD(aCel
39ef8 6c 5b 6a 6a 5d 2e 61 43 6f 6f 72 64 5b 69 2a 32  l[jj].aCoord[i*2
39ef9 5d 29 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20  ]);.      float 
39efa 72 69 67 68 74 20 3d 20 44 43 4f 4f 52 44 28 61  right = DCOORD(a
39efb 43 65 6c 6c 5b 6a 6a 5d 2e 61 43 6f 6f 72 64 5b  Cell[jj].aCoord[
39efc 69 2a 32 2b 31 5d 29 3b 0a 0a 20 20 20 20 20 20  i*2+1]);..      
39efd 69 66 28 20 6c 65 66 74 3c 78 31 20 29 20 78 31  if( left<x1 ) x1
39efe 20 3d 20 6c 65 66 74 3b 0a 20 20 20 20 20 20 69   = left;.      i
39eff 66 28 20 72 69 67 68 74 3e 78 34 20 29 20 78 34  f( right>x4 ) x4
39f00 20 3d 20 72 69 67 68 74 3b 0a 20 20 20 20 20 20   = right;.      
39f01 69 66 28 20 6c 65 66 74 3e 78 33 20 29 7b 0a 20  if( left>x3 ){. 
39f02 20 20 20 20 20 20 20 78 33 20 3d 20 6c 65 66 74         x3 = left
39f03 3b 0a 20 20 20 20 20 20 20 20 69 43 65 6c 6c 52  ;.        iCellR
39f04 69 67 68 74 20 3d 20 6a 6a 3b 0a 20 20 20 20 20  ight = jj;.     
39f05 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 69 67   }.      if( rig
39f06 68 74 3c 78 32 20 29 7b 0a 20 20 20 20 20 20 20  ht<x2 ){.       
39f07 20 78 32 20 3d 20 72 69 67 68 74 3b 0a 20 20 20   x2 = right;.   
39f08 20 20 20 20 20 69 43 65 6c 6c 4c 65 66 74 20 3d       iCellLeft =
39f09 20 6a 6a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   jj;.      }.   
39f0a 20 7d 0a 0a 20 20 20 20 69 66 28 20 78 34 21 3d   }..    if( x4!=
39f0b 78 31 20 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61  x1 ){.      floa
39f0c 74 20 6e 6f 72 6d 61 6c 77 69 64 74 68 20 3d 20  t normalwidth = 
39f0d 28 78 33 20 2d 20 78 32 29 20 2f 20 28 78 34 20  (x3 - x2) / (x4 
39f0e 2d 20 78 31 29 3b 0a 20 20 20 20 20 20 69 66 28  - x1);.      if(
39f0f 20 6e 6f 72 6d 61 6c 77 69 64 74 68 3e 6d 61 78   normalwidth>max
39f10 4e 6f 72 6d 61 6c 49 6e 6e 65 72 57 69 64 74 68  NormalInnerWidth
39f11 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4c 65 66   ){.        iLef
39f12 74 53 65 65 64 20 3d 20 69 43 65 6c 6c 4c 65 66  tSeed = iCellLef
39f13 74 3b 0a 20 20 20 20 20 20 20 20 69 52 69 67 68  t;.        iRigh
39f14 74 53 65 65 64 20 3d 20 69 43 65 6c 6c 52 69 67  tSeed = iCellRig
39f15 68 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ht;.      }.    
39f16 7d 0a 20 20 7d 0a 0a 20 20 2a 70 69 4c 65 66 74  }.  }..  *piLeft
39f17 53 65 65 64 20 3d 20 69 4c 65 66 74 53 65 65 64  Seed = iLeftSeed
39f18 3b 0a 20 20 2a 70 69 52 69 67 68 74 53 65 65 64  ;.  *piRightSeed
39f19 20 3d 20 69 52 69 67 68 74 53 65 65 64 3b 0a 7d   = iRightSeed;.}
39f1a 0a 23 65 6e 64 69 66 20 2f 2a 20 56 41 52 49 41  .#endif /* VARIA
39f1b 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e 45 41  NT_GUTTMAN_LINEA
39f1c 52 5f 53 50 4c 49 54 20 2a 2f 0a 0a 23 69 66 20  R_SPLIT */..#if 
39f1d 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f  VARIANT_GUTTMAN_
39f1e 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49 54 0a  QUADRATIC_SPLIT.
39f1f 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
39f20 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 61 64  tion of the quad
39f21 72 61 74 69 63 20 76 61 72 69 61 6e 74 20 6f 66  ratic variant of
39f22 20 74 68 65 20 50 69 63 6b 4e 65 78 74 28 29 20   the PickNext() 
39f23 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a  function from.**
39f24 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f   Guttman[84]..*/
39f25 0a 73 74 61 74 69 63 20 52 74 72 65 65 43 65 6c  .static RtreeCel
39f26 6c 20 2a 51 75 61 64 72 61 74 69 63 50 69 63 6b  l *QuadraticPick
39f27 4e 65 78 74 28 0a 20 20 52 74 72 65 65 20 2a 70  Next(.  Rtree *p
39f28 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65 43 65  Rtree,.  RtreeCe
39f29 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e  ll *aCell, .  in
39f2a 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 52 74 72 65  t nCell, .  Rtre
39f2b 65 43 65 6c 6c 20 2a 70 4c 65 66 74 42 6f 78 2c  eCell *pLeftBox,
39f2c 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70   .  RtreeCell *p
39f2d 52 69 67 68 74 42 6f 78 2c 0a 20 20 69 6e 74 20  RightBox,.  int 
39f2e 2a 61 69 55 73 65 64 0a 29 7b 0a 20 20 23 64 65  *aiUsed.){.  #de
39f2f 66 69 6e 65 20 46 41 42 53 28 61 29 20 28 28 61  fine FABS(a) ((a
39f30 29 3c 30 2e 30 3f 2d 31 2e 30 2a 28 61 29 3a 28  )<0.0?-1.0*(a):(
39f31 61 29 29 0a 0a 20 20 69 6e 74 20 69 53 65 6c 65  a))..  int iSele
39f32 63 74 20 3d 20 2d 31 3b 0a 20 20 66 6c 6f 61 74  ct = -1;.  float
39f33 20 66 44 69 66 66 3b 0a 20 20 69 6e 74 20 69 69   fDiff;.  int ii
39f34 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ;.  for(ii=0; ii
39f35 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20  <nCell; ii++){. 
39f36 20 20 20 69 66 28 20 61 69 55 73 65 64 5b 69 69     if( aiUsed[ii
39f37 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 6c  ]==0 ){.      fl
39f38 6f 61 74 20 6c 65 66 74 20 3d 20 63 65 6c 6c 47  oat left = cellG
39f39 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20 70 4c  rowth(pRtree, pL
39f3a 65 66 74 42 6f 78 2c 20 26 61 43 65 6c 6c 5b 69  eftBox, &aCell[i
39f3b 69 5d 29 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74  i]);.      float
39f3c 20 72 69 67 68 74 20 3d 20 63 65 6c 6c 47 72 6f   right = cellGro
39f3d 77 74 68 28 70 52 74 72 65 65 2c 20 70 4c 65 66  wth(pRtree, pLef
39f3e 74 42 6f 78 2c 20 26 61 43 65 6c 6c 5b 69 69 5d  tBox, &aCell[ii]
39f3f 29 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 64  );.      float d
39f40 69 66 66 20 3d 20 46 41 42 53 28 72 69 67 68 74  iff = FABS(right
39f41 2d 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 69 66  -left);.      if
39f42 28 20 69 53 65 6c 65 63 74 3c 30 20 7c 7c 20 64  ( iSelect<0 || d
39f43 69 66 66 3e 66 44 69 66 66 20 29 7b 0a 20 20 20  iff>fDiff ){.   
39f44 20 20 20 20 20 66 44 69 66 66 20 3d 20 64 69 66       fDiff = dif
39f45 66 3b 0a 20 20 20 20 20 20 20 20 69 53 65 6c 65  f;.        iSele
39f46 63 74 20 3d 20 69 69 3b 0a 20 20 20 20 20 20 7d  ct = ii;.      }
39f47 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 69 55  .    }.  }.  aiU
39f48 73 65 64 5b 69 53 65 6c 65 63 74 5d 20 3d 20 31  sed[iSelect] = 1
39f49 3b 0a 20 20 72 65 74 75 72 6e 20 26 61 43 65 6c  ;.  return &aCel
39f4a 6c 5b 69 53 65 6c 65 63 74 5d 3b 0a 7d 0a 0a 2f  l[iSelect];.}../
39f4b 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
39f4c 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 61 64 72  ion of the quadr
39f4d 61 74 69 63 20 76 61 72 69 61 6e 74 20 6f 66 20  atic variant of 
39f4e 74 68 65 20 50 69 63 6b 53 65 65 64 73 28 29 20  the PickSeeds() 
39f4f 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a  function from.**
39f50 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f   Guttman[84]..*/
39f51 0a 73 74 61 74 69 63 20 76 6f 69 64 20 51 75 61  .static void Qua
39f52 64 72 61 74 69 63 50 69 63 6b 53 65 65 64 73 28  draticPickSeeds(
39f53 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
39f54 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61  ,.  RtreeCell *a
39f55 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65  Cell, .  int nCe
39f56 6c 6c 2c 20 0a 20 20 69 6e 74 20 2a 70 69 4c 65  ll, .  int *piLe
39f57 66 74 53 65 65 64 2c 20 0a 20 20 69 6e 74 20 2a  ftSeed, .  int *
39f58 70 69 52 69 67 68 74 53 65 65 64 0a 29 7b 0a 20  piRightSeed.){. 
39f59 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 6a   int ii;.  int j
39f5a 6a 3b 0a 0a 20 20 69 6e 74 20 69 4c 65 66 74 53  j;..  int iLeftS
39f5b 65 65 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69  eed = 0;.  int i
39f5c 52 69 67 68 74 53 65 65 64 20 3d 20 31 3b 0a 20  RightSeed = 1;. 
39f5d 20 66 6c 6f 61 74 20 66 57 61 73 74 65 20 3d 20   float fWaste = 
39f5e 30 2e 30 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30  0.0;..  for(ii=0
39f5f 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b  ; ii<nCell; ii++
39f60 29 7b 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 69 69  ){.    for(jj=ii
39f61 2b 31 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a  +1; jj<nCell; jj
39f62 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74  ++){.      float
39f63 20 72 69 67 68 74 20 3d 20 63 65 6c 6c 41 72 65   right = cellAre
39f64 61 28 70 52 74 72 65 65 2c 20 26 61 43 65 6c 6c  a(pRtree, &aCell
39f65 5b 6a 6a 5d 29 3b 0a 20 20 20 20 20 20 66 6c 6f  [jj]);.      flo
39f66 61 74 20 67 72 6f 77 74 68 20 3d 20 63 65 6c 6c  at growth = cell
39f67 47 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20 26  Growth(pRtree, &
39f68 61 43 65 6c 6c 5b 69 69 5d 2c 20 26 61 43 65 6c  aCell[ii], &aCel
39f69 6c 5b 6a 6a 5d 29 3b 0a 20 20 20 20 20 20 66 6c  l[jj]);.      fl
39f6a 6f 61 74 20 77 61 73 74 65 20 3d 20 67 72 6f 77  oat waste = grow
39f6b 74 68 20 2d 20 72 69 67 68 74 3b 0a 0a 20 20 20  th - right;..   
39f6c 20 20 20 69 66 28 20 77 61 73 74 65 3e 66 57 61     if( waste>fWa
39f6d 73 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ste ){.        i
39f6e 4c 65 66 74 53 65 65 64 20 3d 20 69 69 3b 0a 20  LeftSeed = ii;. 
39f6f 20 20 20 20 20 20 20 69 52 69 67 68 74 53 65 65         iRightSee
39f70 64 20 3d 20 6a 6a 3b 0a 20 20 20 20 20 20 20 20  d = jj;.        
39f71 66 57 61 73 74 65 20 3d 20 77 61 73 74 65 3b 0a  fWaste = waste;.
39f72 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
39f73 7d 0a 0a 20 20 2a 70 69 4c 65 66 74 53 65 65 64  }..  *piLeftSeed
39f74 20 3d 20 69 4c 65 66 74 53 65 65 64 3b 0a 20 20   = iLeftSeed;.  
39f75 2a 70 69 52 69 67 68 74 53 65 65 64 20 3d 20 69  *piRightSeed = i
39f76 52 69 67 68 74 53 65 65 64 3b 0a 7d 0a 23 65 6e  RightSeed;.}.#en
39f77 64 69 66 20 2f 2a 20 56 41 52 49 41 4e 54 5f 47  dif /* VARIANT_G
39f78 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54 49 43  UTTMAN_QUADRATIC
39f79 5f 53 50 4c 49 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  _SPLIT */../*.**
39f7a 20 41 72 67 75 6d 65 6e 74 73 20 61 49 64 78 2c   Arguments aIdx,
39f7b 20 61 44 69 73 74 61 6e 63 65 20 61 6e 64 20 61   aDistance and a
39f7c 53 70 61 72 65 20 61 6c 6c 20 70 6f 69 6e 74 20  Spare all point 
39f7d 74 6f 20 61 72 72 61 79 73 20 6f 66 20 73 69 7a  to arrays of siz
39f7e 65 0a 2a 2a 20 6e 49 64 78 2e 20 54 68 65 20 61  e.** nIdx. The a
39f7f 49 64 78 20 61 72 72 61 79 20 63 6f 6e 74 61 69  Idx array contai
39f80 6e 73 20 74 68 65 20 73 65 74 20 6f 66 20 69 6e  ns the set of in
39f81 74 65 67 65 72 73 20 66 72 6f 6d 20 30 20 74 6f  tegers from 0 to
39f82 20 0a 2a 2a 20 28 6e 49 64 78 2d 31 29 20 69 6e   .** (nIdx-1) in
39f83 20 6e 6f 20 70 61 72 74 69 63 75 6c 61 72 20 6f   no particular o
39f84 72 64 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74  rder. This funct
39f85 69 6f 6e 20 73 6f 72 74 73 20 74 68 65 20 76 61  ion sorts the va
39f86 6c 75 65 73 0a 2a 2a 20 69 6e 20 61 49 64 78 20  lues.** in aIdx 
39f87 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65  according to the
39f88 20 69 6e 64 65 78 65 64 20 76 61 6c 75 65 73 20   indexed values 
39f89 69 6e 20 61 44 69 73 74 61 6e 63 65 2e 20 46 6f  in aDistance. Fo
39f8a 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 61 73  r.** example, as
39f8b 73 75 6d 69 6e 67 20 74 68 65 20 69 6e 70 75 74  suming the input
39f8c 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 49 64 78 20  s:.**.**   aIdx 
39f8d 20 20 20 20 20 3d 20 7b 20 30 2c 20 20 20 31 2c       = { 0,   1,
39f8e 20 20 20 32 2c 20 20 20 33 20 7d 0a 2a 2a 20 20     2,   3 }.**  
39f8f 20 61 44 69 73 74 61 6e 63 65 20 3d 20 7b 20 35   aDistance = { 5
39f90 2e 30 2c 20 32 2e 30 2c 20 37 2e 30 2c 20 36 2e  .0, 2.0, 7.0, 6.
39f91 30 20 7d 0a 2a 2a 0a 2a 2a 20 74 68 69 73 20 66  0 }.**.** this f
39f92 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74 68 65  unction sets the
39f93 20 61 49 64 78 20 61 72 72 61 79 20 74 6f 20 63   aIdx array to c
39f94 6f 6e 74 61 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20  ontain:.**.**   
39f95 61 49 64 78 20 20 20 20 20 20 3d 20 7b 20 30 2c  aIdx      = { 0,
39f96 20 20 20 31 2c 20 20 20 32 2c 20 20 20 33 20 7d     1,   2,   3 }
39f97 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 53 70 61 72  .**.** The aSpar
39f98 65 20 61 72 72 61 79 20 69 73 20 75 73 65 64 20  e array is used 
39f99 61 73 20 74 65 6d 70 6f 72 61 72 79 20 77 6f 72  as temporary wor
39f9a 6b 69 6e 67 20 73 70 61 63 65 20 62 79 20 74 68  king space by th
39f9b 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 61 6c 67  e.** sorting alg
39f9c 6f 72 69 74 68 6d 2e 0a 2a 2f 0a 73 74 61 74 69  orithm..*/.stati
39f9d 63 20 76 6f 69 64 20 53 6f 72 74 42 79 44 69 73  c void SortByDis
39f9e 74 61 6e 63 65 28 0a 20 20 69 6e 74 20 2a 61 49  tance(.  int *aI
39f9f 64 78 2c 20 0a 20 20 69 6e 74 20 6e 49 64 78 2c  dx, .  int nIdx,
39fa0 20 0a 20 20 66 6c 6f 61 74 20 2a 61 44 69 73 74   .  float *aDist
39fa1 61 6e 63 65 2c 20 0a 20 20 69 6e 74 20 2a 61 53  ance, .  int *aS
39fa2 70 61 72 65 0a 29 7b 0a 20 20 69 66 28 20 6e 49  pare.){.  if( nI
39fa3 64 78 3e 31 20 29 7b 0a 20 20 20 20 69 6e 74 20  dx>1 ){.    int 
39fa4 69 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20 20 69  iLeft = 0;.    i
39fa5 6e 74 20 69 52 69 67 68 74 20 3d 20 30 3b 0a 0a  nt iRight = 0;..
39fa6 20 20 20 20 69 6e 74 20 6e 4c 65 66 74 20 3d 20      int nLeft = 
39fa7 6e 49 64 78 2f 32 3b 0a 20 20 20 20 69 6e 74 20  nIdx/2;.    int 
39fa8 6e 52 69 67 68 74 20 3d 20 6e 49 64 78 2d 6e 4c  nRight = nIdx-nL
39fa9 65 66 74 3b 0a 20 20 20 20 69 6e 74 20 2a 61 4c  eft;.    int *aL
39faa 65 66 74 20 3d 20 61 49 64 78 3b 0a 20 20 20 20  eft = aIdx;.    
39fab 69 6e 74 20 2a 61 52 69 67 68 74 20 3d 20 26 61  int *aRight = &a
39fac 49 64 78 5b 6e 4c 65 66 74 5d 3b 0a 0a 20 20 20  Idx[nLeft];..   
39fad 20 53 6f 72 74 42 79 44 69 73 74 61 6e 63 65 28   SortByDistance(
39fae 61 4c 65 66 74 2c 20 6e 4c 65 66 74 2c 20 61 44  aLeft, nLeft, aD
39faf 69 73 74 61 6e 63 65 2c 20 61 53 70 61 72 65 29  istance, aSpare)
39fb0 3b 0a 20 20 20 20 53 6f 72 74 42 79 44 69 73 74  ;.    SortByDist
39fb1 61 6e 63 65 28 61 52 69 67 68 74 2c 20 6e 52 69  ance(aRight, nRi
39fb2 67 68 74 2c 20 61 44 69 73 74 61 6e 63 65 2c 20  ght, aDistance, 
39fb3 61 53 70 61 72 65 29 3b 0a 0a 20 20 20 20 6d 65  aSpare);..    me
39fb4 6d 63 70 79 28 61 53 70 61 72 65 2c 20 61 4c 65  mcpy(aSpare, aLe
39fb5 66 74 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a  ft, sizeof(int)*
39fb6 6e 4c 65 66 74 29 3b 0a 20 20 20 20 61 4c 65 66  nLeft);.    aLef
39fb7 74 20 3d 20 61 53 70 61 72 65 3b 0a 0a 20 20 20  t = aSpare;..   
39fb8 20 77 68 69 6c 65 28 20 69 4c 65 66 74 3c 6e 4c   while( iLeft<nL
39fb9 65 66 74 20 7c 7c 20 69 52 69 67 68 74 3c 6e 52  eft || iRight<nR
39fba 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 69 66  ight ){.      if
39fbb 28 20 69 4c 65 66 74 3d 3d 6e 4c 65 66 74 20 29  ( iLeft==nLeft )
39fbc 7b 0a 20 20 20 20 20 20 20 20 61 49 64 78 5b 69  {.        aIdx[i
39fbd 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61  Left+iRight] = a
39fbe 52 69 67 68 74 5b 69 52 69 67 68 74 5d 3b 0a 20  Right[iRight];. 
39fbf 20 20 20 20 20 20 20 69 52 69 67 68 74 2b 2b 3b         iRight++;
39fc0 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
39fc1 20 69 52 69 67 68 74 3d 3d 6e 52 69 67 68 74 20   iRight==nRight 
39fc2 29 7b 0a 20 20 20 20 20 20 20 20 61 49 64 78 5b  ){.        aIdx[
39fc3 69 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d 20  iLeft+iRight] = 
39fc4 61 4c 65 66 74 5b 69 4c 65 66 74 5d 3b 0a 20 20  aLeft[iLeft];.  
39fc5 20 20 20 20 20 20 69 4c 65 66 74 2b 2b 3b 0a 20        iLeft++;. 
39fc6 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
39fc7 20 20 20 20 66 6c 6f 61 74 20 66 4c 65 66 74 20      float fLeft 
39fc8 3d 20 61 44 69 73 74 61 6e 63 65 5b 61 4c 65 66  = aDistance[aLef
39fc9 74 5b 69 4c 65 66 74 5d 5d 3b 0a 20 20 20 20 20  t[iLeft]];.     
39fca 20 20 20 66 6c 6f 61 74 20 66 52 69 67 68 74 20     float fRight 
39fcb 3d 20 61 44 69 73 74 61 6e 63 65 5b 61 52 69 67  = aDistance[aRig
39fcc 68 74 5b 69 52 69 67 68 74 5d 5d 3b 0a 20 20 20  ht[iRight]];.   
39fcd 20 20 20 20 20 69 66 28 20 66 4c 65 66 74 3c 66       if( fLeft<f
39fce 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 20  Right ){.       
39fcf 20 20 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52     aIdx[iLeft+iR
39fd0 69 67 68 74 5d 20 3d 20 61 4c 65 66 74 5b 69 4c  ight] = aLeft[iL
39fd1 65 66 74 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  eft];.          
39fd2 69 4c 65 66 74 2b 2b 3b 0a 20 20 20 20 20 20 20  iLeft++;.       
39fd3 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
39fd4 20 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69    aIdx[iLeft+iRi
39fd5 67 68 74 5d 20 3d 20 61 52 69 67 68 74 5b 69 52  ght] = aRight[iR
39fd6 69 67 68 74 5d 3b 0a 20 20 20 20 20 20 20 20 20  ight];.         
39fd7 20 69 52 69 67 68 74 2b 2b 3b 0a 20 20 20 20 20   iRight++;.     
39fd8 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
39fd9 20 7d 0a 0a 23 69 66 20 30 0a 20 20 20 20 2f 2a   }..#if 0.    /*
39fda 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20   Check that the 
39fdb 73 6f 72 74 20 77 6f 72 6b 65 64 20 2a 2f 0a 20  sort worked */. 
39fdc 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a     {.      int j
39fdd 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d  j;.      for(jj=
39fde 31 3b 20 6a 6a 3c 6e 49 64 78 3b 20 6a 6a 2b 2b  1; jj<nIdx; jj++
39fdf 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74  ){.        float
39fe0 20 6c 65 66 74 20 3d 20 61 44 69 73 74 61 6e 63   left = aDistanc
39fe1 65 5b 61 49 64 78 5b 6a 6a 2d 31 5d 5d 3b 0a 20  e[aIdx[jj-1]];. 
39fe2 20 20 20 20 20 20 20 66 6c 6f 61 74 20 72 69 67         float rig
39fe3 68 74 20 3d 20 61 44 69 73 74 61 6e 63 65 5b 61  ht = aDistance[a
39fe4 49 64 78 5b 6a 6a 5d 5d 3b 0a 20 20 20 20 20 20  Idx[jj]];.      
39fe5 20 20 61 73 73 65 72 74 28 20 6c 65 66 74 3c 3d    assert( left<=
39fe6 72 69 67 68 74 20 29 3b 0a 20 20 20 20 20 20 7d  right );.      }
39fe7 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
39fe8 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d  }.}../*.** Argum
39fe9 65 6e 74 73 20 61 49 64 78 2c 20 61 43 65 6c 6c  ents aIdx, aCell
39fea 20 61 6e 64 20 61 53 70 61 72 65 20 61 6c 6c 20   and aSpare all 
39feb 70 6f 69 6e 74 20 74 6f 20 61 72 72 61 79 73 20  point to arrays 
39fec 6f 66 20 73 69 7a 65 0a 2a 2a 20 6e 49 64 78 2e  of size.** nIdx.
39fed 20 54 68 65 20 61 49 64 78 20 61 72 72 61 79 20   The aIdx array 
39fee 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 65 74  contains the set
39fef 20 6f 66 20 69 6e 74 65 67 65 72 73 20 66 72 6f   of integers fro
39ff0 6d 20 30 20 74 6f 20 0a 2a 2a 20 28 6e 49 64 78  m 0 to .** (nIdx
39ff1 2d 31 29 20 69 6e 20 6e 6f 20 70 61 72 74 69 63  -1) in no partic
39ff2 75 6c 61 72 20 6f 72 64 65 72 2e 20 54 68 69 73  ular order. This
39ff3 20 66 75 6e 63 74 69 6f 6e 20 73 6f 72 74 73 20   function sorts 
39ff4 74 68 65 20 76 61 6c 75 65 73 0a 2a 2a 20 69 6e  the values.** in
39ff5 20 61 49 64 78 20 61 63 63 6f 72 64 69 6e 67 20   aIdx according 
39ff6 74 6f 20 64 69 6d 65 6e 73 69 6f 6e 20 69 44 69  to dimension iDi
39ff7 6d 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 69  m of the cells i
39ff8 6e 20 61 43 65 6c 6c 2e 20 54 68 65 0a 2a 2a 20  n aCell. The.** 
39ff9 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66  minimum value of
39ffa 20 64 69 6d 65 6e 73 69 6f 6e 20 69 44 69 6d 20   dimension iDim 
39ffb 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 66 69  is considered fi
39ffc 72 73 74 2c 20 74 68 65 0a 2a 2a 20 6d 61 78 69  rst, the.** maxi
39ffd 6d 75 6d 20 75 73 65 64 20 74 6f 20 62 72 65 61  mum used to brea
39ffe 6b 20 74 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  k ties..**.** Th
39fff 65 20 61 53 70 61 72 65 20 61 72 72 61 79 20 69  e aSpare array i
3a000 73 20 75 73 65 64 20 61 73 20 74 65 6d 70 6f 72  s used as tempor
3a001 61 72 79 20 77 6f 72 6b 69 6e 67 20 73 70 61 63  ary working spac
3a002 65 20 62 79 20 74 68 65 0a 2a 2a 20 73 6f 72 74  e by the.** sort
3a003 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 2a  ing algorithm..*
3a004 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 53 6f  /.static void So
3a005 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e 28 0a 20  rtByDimension(. 
3a006 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a   Rtree *pRtree,.
3a007 20 20 69 6e 74 20 2a 61 49 64 78 2c 20 0a 20 20    int *aIdx, .  
3a008 69 6e 74 20 6e 49 64 78 2c 20 0a 20 20 69 6e 74  int nIdx, .  int
3a009 20 69 44 69 6d 2c 20 0a 20 20 52 74 72 65 65 43   iDim, .  RtreeC
3a00a 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69  ell *aCell, .  i
3a00b 6e 74 20 2a 61 53 70 61 72 65 0a 29 7b 0a 20 20  nt *aSpare.){.  
3a00c 69 66 28 20 6e 49 64 78 3e 31 20 29 7b 0a 0a 20  if( nIdx>1 ){.. 
3a00d 20 20 20 69 6e 74 20 69 4c 65 66 74 20 3d 20 30     int iLeft = 0
3a00e 3b 0a 20 20 20 20 69 6e 74 20 69 52 69 67 68 74  ;.    int iRight
3a00f 20 3d 20 30 3b 0a 0a 20 20 20 20 69 6e 74 20 6e   = 0;..    int n
3a010 4c 65 66 74 20 3d 20 6e 49 64 78 2f 32 3b 0a 20  Left = nIdx/2;. 
3a011 20 20 20 69 6e 74 20 6e 52 69 67 68 74 20 3d 20     int nRight = 
3a012 6e 49 64 78 2d 6e 4c 65 66 74 3b 0a 20 20 20 20  nIdx-nLeft;.    
3a013 69 6e 74 20 2a 61 4c 65 66 74 20 3d 20 61 49 64  int *aLeft = aId
3a014 78 3b 0a 20 20 20 20 69 6e 74 20 2a 61 52 69 67  x;.    int *aRig
3a015 68 74 20 3d 20 26 61 49 64 78 5b 6e 4c 65 66 74  ht = &aIdx[nLeft
3a016 5d 3b 0a 0a 20 20 20 20 53 6f 72 74 42 79 44 69  ];..    SortByDi
3a017 6d 65 6e 73 69 6f 6e 28 70 52 74 72 65 65 2c 20  mension(pRtree, 
3a018 61 4c 65 66 74 2c 20 6e 4c 65 66 74 2c 20 69 44  aLeft, nLeft, iD
3a019 69 6d 2c 20 61 43 65 6c 6c 2c 20 61 53 70 61 72  im, aCell, aSpar
3a01a 65 29 3b 0a 20 20 20 20 53 6f 72 74 42 79 44 69  e);.    SortByDi
3a01b 6d 65 6e 73 69 6f 6e 28 70 52 74 72 65 65 2c 20  mension(pRtree, 
3a01c 61 52 69 67 68 74 2c 20 6e 52 69 67 68 74 2c 20  aRight, nRight, 
3a01d 69 44 69 6d 2c 20 61 43 65 6c 6c 2c 20 61 53 70  iDim, aCell, aSp
3a01e 61 72 65 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70  are);..    memcp
3a01f 79 28 61 53 70 61 72 65 2c 20 61 4c 65 66 74 2c  y(aSpare, aLeft,
3a020 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 4c 65   sizeof(int)*nLe
3a021 66 74 29 3b 0a 20 20 20 20 61 4c 65 66 74 20 3d  ft);.    aLeft =
3a022 20 61 53 70 61 72 65 3b 0a 20 20 20 20 77 68 69   aSpare;.    whi
3a023 6c 65 28 20 69 4c 65 66 74 3c 6e 4c 65 66 74 20  le( iLeft<nLeft 
3a024 7c 7c 20 69 52 69 67 68 74 3c 6e 52 69 67 68 74  || iRight<nRight
3a025 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65   ){.      double
3a026 20 78 6c 65 66 74 31 20 3d 20 44 43 4f 4f 52 44   xleft1 = DCOORD
3a027 28 61 43 65 6c 6c 5b 61 4c 65 66 74 5b 69 4c 65  (aCell[aLeft[iLe
3a028 66 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d  ft]].aCoord[iDim
3a029 2a 32 5d 29 3b 0a 20 20 20 20 20 20 64 6f 75 62  *2]);.      doub
3a02a 6c 65 20 78 6c 65 66 74 32 20 3d 20 44 43 4f 4f  le xleft2 = DCOO
3a02b 52 44 28 61 43 65 6c 6c 5b 61 4c 65 66 74 5b 69  RD(aCell[aLeft[i
3a02c 4c 65 66 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44  Left]].aCoord[iD
3a02d 69 6d 2a 32 2b 31 5d 29 3b 0a 20 20 20 20 20 20  im*2+1]);.      
3a02e 64 6f 75 62 6c 65 20 78 72 69 67 68 74 31 20 3d  double xright1 =
3a02f 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 61 52   DCOORD(aCell[aR
3a030 69 67 68 74 5b 69 52 69 67 68 74 5d 5d 2e 61 43  ight[iRight]].aC
3a031 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20  oord[iDim*2]);. 
3a032 20 20 20 20 20 64 6f 75 62 6c 65 20 78 72 69 67       double xrig
3a033 68 74 32 20 3d 20 44 43 4f 4f 52 44 28 61 43 65  ht2 = DCOORD(aCe
3a034 6c 6c 5b 61 52 69 67 68 74 5b 69 52 69 67 68 74  ll[aRight[iRight
3a035 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32  ]].aCoord[iDim*2
3a036 2b 31 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  +1]);.      if( 
3a037 28 69 4c 65 66 74 21 3d 6e 4c 65 66 74 29 20 26  (iLeft!=nLeft) &
3a038 26 20 28 28 69 52 69 67 68 74 3d 3d 6e 52 69 67  & ((iRight==nRig
3a039 68 74 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 78  ht).       || (x
3a03a 6c 65 66 74 31 3c 78 72 69 67 68 74 31 29 0a 20  left1<xright1). 
3a03b 20 20 20 20 20 20 7c 7c 20 28 78 6c 65 66 74 31        || (xleft1
3a03c 3d 3d 78 72 69 67 68 74 31 20 26 26 20 78 6c 65  ==xright1 && xle
3a03d 66 74 32 3c 78 72 69 67 68 74 32 29 0a 20 20 20  ft2<xright2).   
3a03e 20 20 20 29 29 7b 0a 20 20 20 20 20 20 20 20 61     )){.        a
3a03f 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67 68 74  Idx[iLeft+iRight
3a040 5d 20 3d 20 61 4c 65 66 74 5b 69 4c 65 66 74 5d  ] = aLeft[iLeft]
3a041 3b 0a 20 20 20 20 20 20 20 20 69 4c 65 66 74 2b  ;.        iLeft+
3a042 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  +;.      }else{.
3a043 20 20 20 20 20 20 20 20 61 49 64 78 5b 69 4c 65          aIdx[iLe
3a044 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61 52 69  ft+iRight] = aRi
3a045 67 68 74 5b 69 52 69 67 68 74 5d 3b 0a 20 20 20  ght[iRight];.   
3a046 20 20 20 20 20 69 52 69 67 68 74 2b 2b 3b 0a 20       iRight++;. 
3a047 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69       }.    }..#i
3a048 66 20 30 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b  f 0.    /* Check
3a049 20 74 68 61 74 20 74 68 65 20 73 6f 72 74 20 77   that the sort w
3a04a 6f 72 6b 65 64 20 2a 2f 0a 20 20 20 20 7b 0a 20  orked */.    {. 
3a04b 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20       int jj;.   
3a04c 20 20 20 66 6f 72 28 6a 6a 3d 31 3b 20 6a 6a 3c     for(jj=1; jj<
3a04d 6e 49 64 78 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20  nIdx; jj++){.   
3a04e 20 20 20 20 20 66 6c 6f 61 74 20 78 6c 65 66 74       float xleft
3a04f 31 20 3d 20 61 43 65 6c 6c 5b 61 49 64 78 5b 6a  1 = aCell[aIdx[j
3a050 6a 2d 31 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69  j-1]].aCoord[iDi
3a051 6d 2a 32 5d 3b 0a 20 20 20 20 20 20 20 20 66 6c  m*2];.        fl
3a052 6f 61 74 20 78 6c 65 66 74 32 20 3d 20 61 43 65  oat xleft2 = aCe
3a053 6c 6c 5b 61 49 64 78 5b 6a 6a 2d 31 5d 5d 2e 61  ll[aIdx[jj-1]].a
3a054 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 3b  Coord[iDim*2+1];
3a055 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20 78  .        float x
3a056 72 69 67 68 74 31 20 3d 20 61 43 65 6c 6c 5b 61  right1 = aCell[a
3a057 49 64 78 5b 6a 6a 5d 5d 2e 61 43 6f 6f 72 64 5b  Idx[jj]].aCoord[
3a058 69 44 69 6d 2a 32 5d 3b 0a 20 20 20 20 20 20 20  iDim*2];.       
3a059 20 66 6c 6f 61 74 20 78 72 69 67 68 74 32 20 3d   float xright2 =
3a05a 20 61 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a 5d 5d   aCell[aIdx[jj]]
3a05b 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31  .aCoord[iDim*2+1
3a05c 5d 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ];.        asser
3a05d 74 28 20 78 6c 65 66 74 31 3c 3d 78 72 69 67 68  t( xleft1<=xrigh
3a05e 74 31 20 26 26 20 28 78 6c 65 66 74 31 3c 78 72  t1 && (xleft1<xr
3a05f 69 67 68 74 31 20 7c 7c 20 78 6c 65 66 74 32 3c  ight1 || xleft2<
3a060 3d 78 72 69 67 68 74 32 29 20 29 3b 0a 20 20 20  =xright2) );.   
3a061 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69     }.    }.#endi
3a062 66 0a 20 20 7d 0a 7d 0a 0a 23 69 66 20 56 41 52  f.  }.}..#if VAR
3a063 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 53  IANT_RSTARTREE_S
3a064 50 4c 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  PLIT./*.** Imple
3a065 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
3a066 20 52 2a 2d 74 72 65 65 20 76 61 72 69 61 6e 74   R*-tree variant
3a067 20 6f 66 20 53 70 6c 69 74 4e 6f 64 65 20 66 72   of SplitNode fr
3a068 6f 6d 20 42 65 63 6b 6d 61 6e 5b 31 39 39 30 5d  om Beckman[1990]
3a069 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3a06a 73 70 6c 69 74 4e 6f 64 65 53 74 61 72 74 72 65  splitNodeStartre
3a06b 65 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  e(.  Rtree *pRtr
3a06c 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  ee,.  RtreeCell 
3a06d 2a 61 43 65 6c 6c 2c 0a 20 20 69 6e 74 20 6e 43  *aCell,.  int nC
3a06e 65 6c 6c 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65  ell,.  RtreeNode
3a06f 20 2a 70 4c 65 66 74 2c 0a 20 20 52 74 72 65 65   *pLeft,.  Rtree
3a070 4e 6f 64 65 20 2a 70 52 69 67 68 74 2c 0a 20 20  Node *pRight,.  
3a071 52 74 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f 78  RtreeCell *pBbox
3a072 4c 65 66 74 2c 0a 20 20 52 74 72 65 65 43 65 6c  Left,.  RtreeCel
3a073 6c 20 2a 70 42 62 6f 78 52 69 67 68 74 0a 29 7b  l *pBboxRight.){
3a074 0a 20 20 69 6e 74 20 2a 2a 61 61 53 6f 72 74 65  .  int **aaSorte
3a075 64 3b 0a 20 20 69 6e 74 20 2a 61 53 70 61 72 65  d;.  int *aSpare
3a076 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 0a 20 20 69  ;.  int ii;..  i
3a077 6e 74 20 69 42 65 73 74 44 69 6d 3b 0a 20 20 69  nt iBestDim;.  i
3a078 6e 74 20 69 42 65 73 74 53 70 6c 69 74 3b 0a 20  nt iBestSplit;. 
3a079 20 66 6c 6f 61 74 20 66 42 65 73 74 4d 61 72 67   float fBestMarg
3a07a 69 6e 3b 0a 0a 20 20 69 6e 74 20 6e 42 79 74 65  in;..  int nByte
3a07b 20 3d 20 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d   = (pRtree->nDim
3a07c 2b 31 29 2a 28 73 69 7a 65 6f 66 28 69 6e 74 2a  +1)*(sizeof(int*
3a07d 29 2b 6e 43 65 6c 6c 2a 73 69 7a 65 6f 66 28 69  )+nCell*sizeof(i
3a07e 6e 74 29 29 3b 0a 0a 20 20 61 61 53 6f 72 74 65  nt));..  aaSorte
3a07f 64 20 3d 20 28 69 6e 74 20 2a 2a 29 73 71 6c 69  d = (int **)sqli
3a080 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65  te3_malloc(nByte
3a081 29 3b 0a 20 20 69 66 28 20 21 61 61 53 6f 72 74  );.  if( !aaSort
3a082 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ed ){.    return
3a083 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
3a084 20 7d 0a 0a 20 20 61 53 70 61 72 65 20 3d 20 26   }..  aSpare = &
3a085 28 28 69 6e 74 20 2a 29 26 61 61 53 6f 72 74 65  ((int *)&aaSorte
3a086 64 5b 70 52 74 72 65 65 2d 3e 6e 44 69 6d 5d 29  d[pRtree->nDim])
3a087 5b 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 6e 43  [pRtree->nDim*nC
3a088 65 6c 6c 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 61  ell];.  memset(a
3a089 61 53 6f 72 74 65 64 2c 20 30 2c 20 6e 42 79 74  aSorted, 0, nByt
3a08a 65 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  e);.  for(ii=0; 
3a08b 69 69 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b  ii<pRtree->nDim;
3a08c 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20   ii++){.    int 
3a08d 6a 6a 3b 0a 20 20 20 20 61 61 53 6f 72 74 65 64  jj;.    aaSorted
3a08e 5b 69 69 5d 20 3d 20 26 28 28 69 6e 74 20 2a 29  [ii] = &((int *)
3a08f 26 61 61 53 6f 72 74 65 64 5b 70 52 74 72 65 65  &aaSorted[pRtree
3a090 2d 3e 6e 44 69 6d 5d 29 5b 69 69 2a 6e 43 65 6c  ->nDim])[ii*nCel
3a091 6c 5d 3b 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 30  l];.    for(jj=0
3a092 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b  ; jj<nCell; jj++
3a093 29 7b 0a 20 20 20 20 20 20 61 61 53 6f 72 74 65  ){.      aaSorte
3a094 64 5b 69 69 5d 5b 6a 6a 5d 20 3d 20 6a 6a 3b 0a  d[ii][jj] = jj;.
3a095 20 20 20 20 7d 0a 20 20 20 20 53 6f 72 74 42 79      }.    SortBy
3a096 44 69 6d 65 6e 73 69 6f 6e 28 70 52 74 72 65 65  Dimension(pRtree
3a097 2c 20 61 61 53 6f 72 74 65 64 5b 69 69 5d 2c 20  , aaSorted[ii], 
3a098 6e 43 65 6c 6c 2c 20 69 69 2c 20 61 43 65 6c 6c  nCell, ii, aCell
3a099 2c 20 61 53 70 61 72 65 29 3b 0a 20 20 7d 0a 0a  , aSpare);.  }..
3a09a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70    for(ii=0; ii<p
3a09b 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 69 2b  Rtree->nDim; ii+
3a09c 2b 29 7b 0a 20 20 20 20 66 6c 6f 61 74 20 6d 61  +){.    float ma
3a09d 72 67 69 6e 20 3d 20 30 2e 30 3b 0a 20 20 20 20  rgin = 0.0;.    
3a09e 66 6c 6f 61 74 20 66 42 65 73 74 4f 76 65 72 6c  float fBestOverl
3a09f 61 70 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 42  ap;.    float fB
3a0a0 65 73 74 41 72 65 61 3b 0a 20 20 20 20 69 6e 74  estArea;.    int
3a0a1 20 69 42 65 73 74 4c 65 66 74 3b 0a 20 20 20 20   iBestLeft;.    
3a0a2 69 6e 74 20 6e 4c 65 66 74 3b 0a 0a 20 20 20 20  int nLeft;..    
3a0a3 66 6f 72 28 0a 20 20 20 20 20 20 6e 4c 65 66 74  for(.      nLeft
3a0a4 3d 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28  =RTREE_MINCELLS(
3a0a5 70 52 74 72 65 65 29 3b 20 0a 20 20 20 20 20 20  pRtree); .      
3a0a6 6e 4c 65 66 74 3c 3d 28 6e 43 65 6c 6c 2d 52 54  nLeft<=(nCell-RT
3a0a7 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74  REE_MINCELLS(pRt
3a0a8 72 65 65 29 29 3b 20 0a 20 20 20 20 20 20 6e 4c  ree)); .      nL
3a0a9 65 66 74 2b 2b 0a 20 20 20 20 29 7b 0a 20 20 20  eft++.    ){.   
3a0aa 20 20 20 52 74 72 65 65 43 65 6c 6c 20 6c 65 66     RtreeCell lef
3a0ab 74 3b 0a 20 20 20 20 20 20 52 74 72 65 65 43 65  t;.      RtreeCe
3a0ac 6c 6c 20 72 69 67 68 74 3b 0a 20 20 20 20 20 20  ll right;.      
3a0ad 69 6e 74 20 6b 6b 3b 0a 20 20 20 20 20 20 66 6c  int kk;.      fl
3a0ae 6f 61 74 20 6f 76 65 72 6c 61 70 3b 0a 20 20 20  oat overlap;.   
3a0af 20 20 20 66 6c 6f 61 74 20 61 72 65 61 3b 0a 0a     float area;..
3a0b0 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 6c 65        memcpy(&le
3a0b1 66 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72  ft, &aCell[aaSor
3a0b2 74 65 64 5b 69 69 5d 5b 30 5d 5d 2c 20 73 69 7a  ted[ii][0]], siz
3a0b3 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b  eof(RtreeCell));
3a0b4 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 72  .      memcpy(&r
3a0b5 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53  ight, &aCell[aaS
3a0b6 6f 72 74 65 64 5b 69 69 5d 5b 6e 43 65 6c 6c 2d  orted[ii][nCell-
3a0b7 31 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65  1]], sizeof(Rtre
3a0b8 65 43 65 6c 6c 29 29 3b 0a 20 20 20 20 20 20 66  eCell));.      f
3a0b9 6f 72 28 6b 6b 3d 31 3b 20 6b 6b 3c 28 6e 43 65  or(kk=1; kk<(nCe
3a0ba 6c 6c 2d 31 29 3b 20 6b 6b 2b 2b 29 7b 0a 20 20  ll-1); kk++){.  
3a0bb 20 20 20 20 20 20 69 66 28 20 6b 6b 3c 6e 4c 65        if( kk<nLe
3a0bc 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ft ){.          
3a0bd 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65  cellUnion(pRtree
3a0be 2c 20 26 6c 65 66 74 2c 20 26 61 43 65 6c 6c 5b  , &left, &aCell[
3a0bf 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b 6b 6b 5d  aaSorted[ii][kk]
3a0c0 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ]);.        }els
3a0c1 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63 65 6c  e{.          cel
3a0c2 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 26  lUnion(pRtree, &
3a0c3 72 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 61 61  right, &aCell[aa
3a0c4 53 6f 72 74 65 64 5b 69 69 5d 5b 6b 6b 5d 5d 29  Sorted[ii][kk]])
3a0c5 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
3a0c6 20 20 7d 0a 20 20 20 20 20 20 6d 61 72 67 69 6e    }.      margin
3a0c7 20 2b 3d 20 63 65 6c 6c 4d 61 72 67 69 6e 28 70   += cellMargin(p
3a0c8 52 74 72 65 65 2c 20 26 6c 65 66 74 29 3b 0a 20  Rtree, &left);. 
3a0c9 20 20 20 20 20 6d 61 72 67 69 6e 20 2b 3d 20 63       margin += c
3a0ca 65 6c 6c 4d 61 72 67 69 6e 28 70 52 74 72 65 65  ellMargin(pRtree
3a0cb 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20  , &right);.     
3a0cc 20 6f 76 65 72 6c 61 70 20 3d 20 63 65 6c 6c 4f   overlap = cellO
3a0cd 76 65 72 6c 61 70 28 70 52 74 72 65 65 2c 20 26  verlap(pRtree, &
3a0ce 6c 65 66 74 2c 20 26 72 69 67 68 74 2c 20 31 2c  left, &right, 1,
3a0cf 20 2d 31 29 3b 0a 20 20 20 20 20 20 61 72 65 61   -1);.      area
3a0d0 20 3d 20 63 65 6c 6c 41 72 65 61 28 70 52 74 72   = cellArea(pRtr
3a0d1 65 65 2c 20 26 6c 65 66 74 29 20 2b 20 63 65 6c  ee, &left) + cel
3a0d2 6c 41 72 65 61 28 70 52 74 72 65 65 2c 20 26 72  lArea(pRtree, &r
3a0d3 69 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66 28  ight);.      if(
3a0d4 20 28 6e 4c 65 66 74 3d 3d 52 54 52 45 45 5f 4d   (nLeft==RTREE_M
3a0d5 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65 29 29  INCELLS(pRtree))
3a0d6 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f 76 65 72  .       || (over
3a0d7 6c 61 70 3c 66 42 65 73 74 4f 76 65 72 6c 61 70  lap<fBestOverlap
3a0d8 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f 76 65  ).       || (ove
3a0d9 72 6c 61 70 3d 3d 66 42 65 73 74 4f 76 65 72 6c  rlap==fBestOverl
3a0da 61 70 20 26 26 20 61 72 65 61 3c 66 42 65 73 74  ap && area<fBest
3a0db 41 72 65 61 29 0a 20 20 20 20 20 20 29 7b 0a 20  Area).      ){. 
3a0dc 20 20 20 20 20 20 20 69 42 65 73 74 4c 65 66 74         iBestLeft
3a0dd 20 3d 20 6e 4c 65 66 74 3b 0a 20 20 20 20 20 20   = nLeft;.      
3a0de 20 20 66 42 65 73 74 4f 76 65 72 6c 61 70 20 3d    fBestOverlap =
3a0df 20 6f 76 65 72 6c 61 70 3b 0a 20 20 20 20 20 20   overlap;.      
3a0e0 20 20 66 42 65 73 74 41 72 65 61 20 3d 20 61 72    fBestArea = ar
3a0e1 65 61 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ea;.      }.    
3a0e2 7d 0a 0a 20 20 20 20 69 66 28 20 69 69 3d 3d 30  }..    if( ii==0
3a0e3 20 7c 7c 20 6d 61 72 67 69 6e 3c 66 42 65 73 74   || margin<fBest
3a0e4 4d 61 72 67 69 6e 20 29 7b 0a 20 20 20 20 20 20  Margin ){.      
3a0e5 69 42 65 73 74 44 69 6d 20 3d 20 69 69 3b 0a 20  iBestDim = ii;. 
3a0e6 20 20 20 20 20 66 42 65 73 74 4d 61 72 67 69 6e       fBestMargin
3a0e7 20 3d 20 6d 61 72 67 69 6e 3b 0a 20 20 20 20 20   = margin;.     
3a0e8 20 69 42 65 73 74 53 70 6c 69 74 20 3d 20 69 42   iBestSplit = iB
3a0e9 65 73 74 4c 65 66 74 3b 0a 20 20 20 20 7d 0a 20  estLeft;.    }. 
3a0ea 20 7d 0a 0a 20 20 6d 65 6d 63 70 79 28 70 42 62   }..  memcpy(pBb
3a0eb 6f 78 4c 65 66 74 2c 20 26 61 43 65 6c 6c 5b 61  oxLeft, &aCell[a
3a0ec 61 53 6f 72 74 65 64 5b 69 42 65 73 74 44 69 6d  aSorted[iBestDim
3a0ed 5d 5b 30 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74  ][0]], sizeof(Rt
3a0ee 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 6d 65 6d  reeCell));.  mem
3a0ef 63 70 79 28 70 42 62 6f 78 52 69 67 68 74 2c 20  cpy(pBboxRight, 
3a0f0 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b  &aCell[aaSorted[
3a0f1 69 42 65 73 74 44 69 6d 5d 5b 69 42 65 73 74 53  iBestDim][iBestS
3a0f2 70 6c 69 74 5d 5d 2c 20 73 69 7a 65 6f 66 28 52  plit]], sizeof(R
3a0f3 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 66 6f  treeCell));.  fo
3a0f4 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c  r(ii=0; ii<nCell
3a0f5 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72  ; ii++){.    Rtr
3a0f6 65 65 4e 6f 64 65 20 2a 70 54 61 72 67 65 74 20  eeNode *pTarget 
3a0f7 3d 20 28 69 69 3c 69 42 65 73 74 53 70 6c 69 74  = (ii<iBestSplit
3a0f8 29 3f 70 4c 65 66 74 3a 70 52 69 67 68 74 3b 0a  )?pLeft:pRight;.
3a0f9 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70      RtreeCell *p
3a0fa 42 62 6f 78 20 3d 20 28 69 69 3c 69 42 65 73 74  Bbox = (ii<iBest
3a0fb 53 70 6c 69 74 29 3f 70 42 62 6f 78 4c 65 66 74  Split)?pBboxLeft
3a0fc 3a 70 42 62 6f 78 52 69 67 68 74 3b 0a 20 20 20  :pBboxRight;.   
3a0fd 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c   RtreeCell *pCel
3a0fe 6c 20 3d 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72  l = &aCell[aaSor
3a0ff 74 65 64 5b 69 42 65 73 74 44 69 6d 5d 5b 69 69  ted[iBestDim][ii
3a100 5d 5d 3b 0a 20 20 20 20 6e 6f 64 65 49 6e 73 65  ]];.    nodeInse
3a101 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  rtCell(pRtree, p
3a102 54 61 72 67 65 74 2c 20 70 43 65 6c 6c 29 3b 0a  Target, pCell);.
3a103 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52      cellUnion(pR
3a104 74 72 65 65 2c 20 70 42 62 6f 78 2c 20 70 43 65  tree, pBbox, pCe
3a105 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69  ll);.  }..  sqli
3a106 74 65 33 5f 66 72 65 65 28 61 61 53 6f 72 74 65  te3_free(aaSorte
3a107 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  d);.  return SQL
3a108 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66  ITE_OK;.}.#endif
3a109 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47 55  ..#if VARIANT_GU
3a10a 54 54 4d 41 4e 5f 53 50 4c 49 54 0a 2f 2a 0a 2a  TTMAN_SPLIT./*.*
3a10b 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
3a10c 20 6f 66 20 74 68 65 20 72 65 67 75 6c 61 72 20   of the regular 
3a10d 52 2d 74 72 65 65 20 53 70 6c 69 74 4e 6f 64 65  R-tree SplitNode
3a10e 20 66 72 6f 6d 20 47 75 74 74 6d 61 6e 5b 31 39   from Guttman[19
3a10f 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  84]..*/.static i
3a110 6e 74 20 73 70 6c 69 74 4e 6f 64 65 47 75 74 74  nt splitNodeGutt
3a111 6d 61 6e 28 0a 20 20 52 74 72 65 65 20 2a 70 52  man(.  Rtree *pR
3a112 74 72 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c  tree,.  RtreeCel
3a113 6c 20 2a 61 43 65 6c 6c 2c 0a 20 20 69 6e 74 20  l *aCell,.  int 
3a114 6e 43 65 6c 6c 2c 0a 20 20 52 74 72 65 65 4e 6f  nCell,.  RtreeNo
3a115 64 65 20 2a 70 4c 65 66 74 2c 0a 20 20 52 74 72  de *pLeft,.  Rtr
3a116 65 65 4e 6f 64 65 20 2a 70 52 69 67 68 74 2c 0a  eeNode *pRight,.
3a117 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 42 62    RtreeCell *pBb
3a118 6f 78 4c 65 66 74 2c 0a 20 20 52 74 72 65 65 43  oxLeft,.  RtreeC
3a119 65 6c 6c 20 2a 70 42 62 6f 78 52 69 67 68 74 0a  ell *pBboxRight.
3a11a 29 7b 0a 20 20 69 6e 74 20 69 4c 65 66 74 53 65  ){.  int iLeftSe
3a11b 65 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 52  ed = 0;.  int iR
3a11c 69 67 68 74 53 65 65 64 20 3d 20 31 3b 0a 20 20  ightSeed = 1;.  
3a11d 69 6e 74 20 2a 61 69 55 73 65 64 3b 0a 20 20 69  int *aiUsed;.  i
3a11e 6e 74 20 69 3b 0a 0a 20 20 61 69 55 73 65 64 20  nt i;..  aiUsed 
3a11f 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
3a120 28 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 65  (sizeof(int)*nCe
3a121 6c 6c 29 3b 0a 20 20 69 66 28 20 21 61 69 55 73  ll);.  if( !aiUs
3a122 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ed ){.    return
3a123 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
3a124 20 7d 0a 20 20 6d 65 6d 73 65 74 28 61 69 55 73   }.  memset(aiUs
3a125 65 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 69 6e  ed, 0, sizeof(in
3a126 74 29 2a 6e 43 65 6c 6c 29 3b 0a 0a 20 20 50 69  t)*nCell);..  Pi
3a127 63 6b 53 65 65 64 73 28 70 52 74 72 65 65 2c 20  ckSeeds(pRtree, 
3a128 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 26 69  aCell, nCell, &i
3a129 4c 65 66 74 53 65 65 64 2c 20 26 69 52 69 67 68  LeftSeed, &iRigh
3a12a 74 53 65 65 64 29 3b 0a 0a 20 20 6d 65 6d 63 70  tSeed);..  memcp
3a12b 79 28 70 42 62 6f 78 4c 65 66 74 2c 20 26 61 43  y(pBboxLeft, &aC
3a12c 65 6c 6c 5b 69 4c 65 66 74 53 65 65 64 5d 2c 20  ell[iLeftSeed], 
3a12d 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c  sizeof(RtreeCell
3a12e 29 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 42 62  ));.  memcpy(pBb
3a12f 6f 78 52 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b  oxRight, &aCell[
3a130 69 52 69 67 68 74 53 65 65 64 5d 2c 20 73 69 7a  iRightSeed], siz
3a131 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b  eof(RtreeCell));
3a132 0a 20 20 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c  .  nodeInsertCel
3a133 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 2c  l(pRtree, pLeft,
3a134 20 26 61 43 65 6c 6c 5b 69 4c 65 66 74 53 65 65   &aCell[iLeftSee
3a135 64 5d 29 3b 0a 20 20 6e 6f 64 65 49 6e 73 65 72  d]);.  nodeInser
3a136 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 52  tCell(pRtree, pR
3a137 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 69 52 69  ight, &aCell[iRi
3a138 67 68 74 53 65 65 64 5d 29 3b 0a 20 20 61 69 55  ghtSeed]);.  aiU
3a139 73 65 64 5b 69 4c 65 66 74 53 65 65 64 5d 20 3d  sed[iLeftSeed] =
3a13a 20 31 3b 0a 20 20 61 69 55 73 65 64 5b 69 52 69   1;.  aiUsed[iRi
3a13b 67 68 74 53 65 65 64 5d 20 3d 20 31 3b 0a 0a 20  ghtSeed] = 1;.. 
3a13c 20 66 6f 72 28 69 3d 6e 43 65 6c 6c 2d 32 3b 20   for(i=nCell-2; 
3a13d 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 52  i>0; i--){.    R
3a13e 74 72 65 65 43 65 6c 6c 20 2a 70 4e 65 78 74 3b  treeCell *pNext;
3a13f 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 50 69 63  .    pNext = Pic
3a140 6b 4e 65 78 74 28 70 52 74 72 65 65 2c 20 61 43  kNext(pRtree, aC
3a141 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 70 42 62 6f  ell, nCell, pBbo
3a142 78 4c 65 66 74 2c 20 70 42 62 6f 78 52 69 67 68  xLeft, pBboxRigh
3a143 74 2c 20 61 69 55 73 65 64 29 3b 0a 20 20 20 20  t, aiUsed);.    
3a144 66 6c 6f 61 74 20 64 69 66 66 20 3d 20 20 0a 20  float diff =  . 
3a145 20 20 20 20 20 63 65 6c 6c 47 72 6f 77 74 68 28       cellGrowth(
3a146 70 52 74 72 65 65 2c 20 70 42 62 6f 78 4c 65 66  pRtree, pBboxLef
3a147 74 2c 20 70 4e 65 78 74 29 20 2d 20 0a 20 20 20  t, pNext) - .   
3a148 20 20 20 63 65 6c 6c 47 72 6f 77 74 68 28 70 52     cellGrowth(pR
3a149 74 72 65 65 2c 20 70 42 62 6f 78 52 69 67 68 74  tree, pBboxRight
3a14a 2c 20 70 4e 65 78 74 29 0a 20 20 20 20 3b 0a 20  , pNext).    ;. 
3a14b 20 20 20 69 66 28 20 28 52 54 52 45 45 5f 4d 49     if( (RTREE_MI
3a14c 4e 43 45 4c 4c 53 28 70 52 74 72 65 65 29 2d 4e  NCELLS(pRtree)-N
3a14d 43 45 4c 4c 28 70 52 69 67 68 74 29 3d 3d 69 29  CELL(pRight)==i)
3a14e 0a 20 20 20 20 20 7c 7c 20 28 64 69 66 66 3e 30  .     || (diff>0
3a14f 2e 30 20 26 26 20 28 52 54 52 45 45 5f 4d 49 4e  .0 && (RTREE_MIN
3a150 43 45 4c 4c 53 28 70 52 74 72 65 65 29 2d 4e 43  CELLS(pRtree)-NC
3a151 45 4c 4c 28 70 4c 65 66 74 29 21 3d 69 29 29 0a  ELL(pLeft)!=i)).
3a152 20 20 20 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64      ){.      nod
3a153 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72  eInsertCell(pRtr
3a154 65 65 2c 20 70 52 69 67 68 74 2c 20 70 4e 65 78  ee, pRight, pNex
3a155 74 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c 55 6e  t);.      cellUn
3a156 69 6f 6e 28 70 52 74 72 65 65 2c 20 70 42 62 6f  ion(pRtree, pBbo
3a157 78 52 69 67 68 74 2c 20 70 4e 65 78 74 29 3b 0a  xRight, pNext);.
3a158 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3a159 20 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28   nodeInsertCell(
3a15a 70 52 74 72 65 65 2c 20 70 4c 65 66 74 2c 20 70  pRtree, pLeft, p
3a15b 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 63 65 6c  Next);.      cel
3a15c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 70  lUnion(pRtree, p
3a15d 42 62 6f 78 4c 65 66 74 2c 20 70 4e 65 78 74 29  BboxLeft, pNext)
3a15e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73  ;.    }.  }..  s
3a15f 71 6c 69 74 65 33 5f 66 72 65 65 28 61 69 55 73  qlite3_free(aiUs
3a160 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ed);.  return SQ
3a161 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69  LITE_OK;.}.#endi
3a162 66 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 75 70  f..static int up
3a163 64 61 74 65 4d 61 70 70 69 6e 67 28 0a 20 20 52  dateMapping(.  R
3a164 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
3a165 20 69 36 34 20 69 52 6f 77 69 64 2c 20 0a 20 20   i64 iRowid, .  
3a166 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
3a167 2c 20 0a 20 20 69 6e 74 20 69 48 65 69 67 68 74  , .  int iHeight
3a168 0a 29 7b 0a 20 20 69 6e 74 20 28 2a 78 53 65 74  .){.  int (*xSet
3a169 4d 61 70 70 69 6e 67 29 28 52 74 72 65 65 20 2a  Mapping)(Rtree *
3a16a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c  , sqlite3_int64,
3a16b 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b   sqlite3_int64);
3a16c 0a 20 20 78 53 65 74 4d 61 70 70 69 6e 67 20 3d  .  xSetMapping =
3a16d 20 28 28 69 48 65 69 67 68 74 3d 3d 30 29 3f 72   ((iHeight==0)?r
3a16e 6f 77 69 64 57 72 69 74 65 3a 70 61 72 65 6e 74  owidWrite:parent
3a16f 57 72 69 74 65 29 3b 0a 20 20 69 66 28 20 69 48  Write);.  if( iH
3a170 65 69 67 68 74 3e 30 20 29 7b 0a 20 20 20 20 52  eight>0 ){.    R
3a171 74 72 65 65 4e 6f 64 65 20 2a 70 43 68 69 6c 64  treeNode *pChild
3a172 20 3d 20 6e 6f 64 65 48 61 73 68 4c 6f 6f 6b 75   = nodeHashLooku
3a173 70 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64  p(pRtree, iRowid
3a174 29 3b 0a 20 20 20 20 69 66 28 20 70 43 68 69 6c  );.    if( pChil
3a175 64 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64 65 52  d ){.      nodeR
3a176 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70  elease(pRtree, p
3a177 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e 74 29 3b  Child->pParent);
3a178 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 66 65 72  .      nodeRefer
3a179 65 6e 63 65 28 70 4e 6f 64 65 29 3b 0a 20 20 20  ence(pNode);.   
3a17a 20 20 20 70 43 68 69 6c 64 2d 3e 70 50 61 72 65     pChild->pPare
3a17b 6e 74 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 20 20  nt = pNode;.    
3a17c 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 78  }.  }.  return x
3a17d 53 65 74 4d 61 70 70 69 6e 67 28 70 52 74 72 65  SetMapping(pRtre
3a17e 65 2c 20 69 52 6f 77 69 64 2c 20 70 4e 6f 64 65  e, iRowid, pNode
3a17f 2d 3e 69 4e 6f 64 65 29 3b 0a 7d 0a 0a 73 74 61  ->iNode);.}..sta
3a180 74 69 63 20 69 6e 74 20 53 70 6c 69 74 4e 6f 64  tic int SplitNod
3a181 65 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  e(.  Rtree *pRtr
3a182 65 65 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  ee,.  RtreeNode 
3a183 2a 70 4e 6f 64 65 2c 0a 20 20 52 74 72 65 65 43  *pNode,.  RtreeC
3a184 65 6c 6c 20 2a 70 43 65 6c 6c 2c 0a 20 20 69 6e  ell *pCell,.  in
3a185 74 20 69 48 65 69 67 68 74 0a 29 7b 0a 20 20 69  t iHeight.){.  i
3a186 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 65 77 43  nt i;.  int newC
3a187 65 6c 6c 49 73 52 69 67 68 74 20 3d 20 30 3b 0a  ellIsRight = 0;.
3a188 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
3a189 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 43 65  TE_OK;.  int nCe
3a18a 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65  ll = NCELL(pNode
3a18b 29 3b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  );.  RtreeCell *
3a18c 61 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 2a 61 69  aCell;.  int *ai
3a18d 55 73 65 64 3b 0a 0a 20 20 52 74 72 65 65 4e 6f  Used;..  RtreeNo
3a18e 64 65 20 2a 70 4c 65 66 74 20 3d 20 30 3b 0a 20  de *pLeft = 0;. 
3a18f 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 52 69 67   RtreeNode *pRig
3a190 68 74 20 3d 20 30 3b 0a 0a 20 20 52 74 72 65 65  ht = 0;..  Rtree
3a191 43 65 6c 6c 20 6c 65 66 74 62 62 6f 78 3b 0a 20  Cell leftbbox;. 
3a192 20 52 74 72 65 65 43 65 6c 6c 20 72 69 67 68 74   RtreeCell right
3a193 62 62 6f 78 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  bbox;..  /* Allo
3a194 63 61 74 65 20 61 6e 20 61 72 72 61 79 20 61 6e  cate an array an
3a195 64 20 70 6f 70 75 6c 61 74 65 20 69 74 20 77 69  d populate it wi
3a196 74 68 20 61 20 63 6f 70 79 20 6f 66 20 70 43 65  th a copy of pCe
3a197 6c 6c 20 61 6e 64 20 0a 20 20 2a 2a 20 61 6c 6c  ll and .  ** all
3a198 20 63 65 6c 6c 73 20 66 72 6f 6d 20 6e 6f 64 65   cells from node
3a199 20 70 4c 65 66 74 2e 20 54 68 65 6e 20 7a 65 72   pLeft. Then zer
3a19a 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 6e  o the original n
3a19b 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 61 43 65 6c  ode..  */.  aCel
3a19c 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  l = sqlite3_mall
3a19d 6f 63 28 28 73 69 7a 65 6f 66 28 52 74 72 65 65  oc((sizeof(Rtree
3a19e 43 65 6c 6c 29 2b 73 69 7a 65 6f 66 28 69 6e 74  Cell)+sizeof(int
3a19f 29 29 2a 28 6e 43 65 6c 6c 2b 31 29 29 3b 0a 20  ))*(nCell+1));. 
3a1a0 20 69 66 28 20 21 61 43 65 6c 6c 20 29 7b 0a 20   if( !aCell ){. 
3a1a1 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
3a1a2 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20 73  OMEM;.    goto s
3a1a3 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a 20 20  plitnode_out;.  
3a1a4 7d 0a 20 20 61 69 55 73 65 64 20 3d 20 28 69 6e  }.  aiUsed = (in
3a1a5 74 20 2a 29 26 61 43 65 6c 6c 5b 6e 43 65 6c 6c  t *)&aCell[nCell
3a1a6 2b 31 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 61 69  +1];.  memset(ai
3a1a7 55 73 65 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Used, 0, sizeof(
3a1a8 69 6e 74 29 2a 28 6e 43 65 6c 6c 2b 31 29 29 3b  int)*(nCell+1));
3a1a9 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43  .  for(i=0; i<nC
3a1aa 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e  ell; i++){.    n
3a1ab 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65  odeGetCell(pRtre
3a1ac 65 2c 20 70 4e 6f 64 65 2c 20 69 2c 20 26 61 43  e, pNode, i, &aC
3a1ad 65 6c 6c 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 6e  ell[i]);.  }.  n
3a1ae 6f 64 65 5a 65 72 6f 28 70 52 74 72 65 65 2c 20  odeZero(pRtree, 
3a1af 70 4e 6f 64 65 29 3b 0a 20 20 6d 65 6d 63 70 79  pNode);.  memcpy
3a1b0 28 26 61 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c 20  (&aCell[nCell], 
3a1b1 70 43 65 6c 6c 2c 20 73 69 7a 65 6f 66 28 52 74  pCell, sizeof(Rt
3a1b2 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 6e 43 65  reeCell));.  nCe
3a1b3 6c 6c 2b 2b 3b 0a 0a 20 20 69 66 28 20 70 4e 6f  ll++;..  if( pNo
3a1b4 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a  de->iNode==1 ){.
3a1b5 20 20 20 20 70 52 69 67 68 74 20 3d 20 6e 6f 64      pRight = nod
3a1b6 65 4e 65 77 28 70 52 74 72 65 65 2c 20 70 4e 6f  eNew(pRtree, pNo
3a1b7 64 65 2c 20 31 29 3b 0a 20 20 20 20 70 4c 65 66  de, 1);.    pLef
3a1b8 74 20 3d 20 6e 6f 64 65 4e 65 77 28 70 52 74 72  t = nodeNew(pRtr
3a1b9 65 65 2c 20 70 4e 6f 64 65 2c 20 31 29 3b 0a 20  ee, pNode, 1);. 
3a1ba 20 20 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74     pRtree->iDept
3a1bb 68 2b 2b 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e  h++;.    pNode->
3a1bc 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 20 20 20  isDirty = 1;.   
3a1bd 20 77 72 69 74 65 49 6e 74 31 36 28 70 4e 6f 64   writeInt16(pNod
3a1be 65 2d 3e 7a 44 61 74 61 2c 20 70 52 74 72 65 65  e->zData, pRtree
3a1bf 2d 3e 69 44 65 70 74 68 29 3b 0a 20 20 7d 65 6c  ->iDepth);.  }el
3a1c0 73 65 7b 0a 20 20 20 20 70 4c 65 66 74 20 3d 20  se{.    pLeft = 
3a1c1 70 4e 6f 64 65 3b 0a 20 20 20 20 70 52 69 67 68  pNode;.    pRigh
3a1c2 74 20 3d 20 6e 6f 64 65 4e 65 77 28 70 52 74 72  t = nodeNew(pRtr
3a1c3 65 65 2c 20 70 4c 65 66 74 2d 3e 70 50 61 72 65  ee, pLeft->pPare
3a1c4 6e 74 2c 20 31 29 3b 0a 20 20 20 20 6e 6f 64 65  nt, 1);.    node
3a1c5 52 65 66 65 72 65 6e 63 65 28 70 4c 65 66 74 29  Reference(pLeft)
3a1c6 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70 4c  ;.  }..  if( !pL
3a1c7 65 66 74 20 7c 7c 20 21 70 52 69 67 68 74 20 29  eft || !pRight )
3a1c8 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
3a1c9 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74  E_NOMEM;.    got
3a1ca 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b  o splitnode_out;
3a1cb 0a 20 20 7d 0a 0a 20 20 6d 65 6d 73 65 74 28 70  .  }..  memset(p
3a1cc 4c 65 66 74 2d 3e 7a 44 61 74 61 2c 20 30 2c 20  Left->zData, 0, 
3a1cd 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a  pRtree->iNodeSiz
3a1ce 65 29 3b 0a 20 20 6d 65 6d 73 65 74 28 70 52 69  e);.  memset(pRi
3a1cf 67 68 74 2d 3e 7a 44 61 74 61 2c 20 30 2c 20 70  ght->zData, 0, p
3a1d0 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65  Rtree->iNodeSize
3a1d1 29 3b 0a 0a 20 20 72 63 20 3d 20 41 73 73 69 67  );..  rc = Assig
3a1d2 6e 43 65 6c 6c 73 28 70 52 74 72 65 65 2c 20 61  nCells(pRtree, a
3a1d3 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 70 4c 65  Cell, nCell, pLe
3a1d4 66 74 2c 20 70 52 69 67 68 74 2c 20 26 6c 65 66  ft, pRight, &lef
3a1d5 74 62 62 6f 78 2c 20 26 72 69 67 68 74 62 62 6f  tbbox, &rightbbo
3a1d6 78 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  x);.  if( rc!=SQ
3a1d7 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67  LITE_OK ){.    g
3a1d8 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75  oto splitnode_ou
3a1d9 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73  t;.  }..  /* Ens
3a1da 75 72 65 20 62 6f 74 68 20 63 68 69 6c 64 20 6e  ure both child n
3a1db 6f 64 65 73 20 68 61 76 65 20 6e 6f 64 65 20 6e  odes have node n
3a1dc 75 6d 62 65 72 73 20 61 73 73 69 67 6e 65 64 20  umbers assigned 
3a1dd 74 6f 20 74 68 65 6d 2e 20 2a 2f 0a 20 20 69 66  to them. */.  if
3a1de 28 20 28 30 3d 3d 70 52 69 67 68 74 2d 3e 69 4e  ( (0==pRight->iN
3a1df 6f 64 65 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b  ode && SQLITE_OK
3a1e0 21 3d 28 72 63 20 3d 20 6e 6f 64 65 57 72 69 74  !=(rc = nodeWrit
3a1e1 65 28 70 52 74 72 65 65 2c 20 70 52 69 67 68 74  e(pRtree, pRight
3a1e2 29 29 29 0a 20 20 20 7c 7c 20 28 30 3d 3d 70 4c  ))).   || (0==pL
3a1e3 65 66 74 2d 3e 69 4e 6f 64 65 20 26 26 20 53 51  eft->iNode && SQ
3a1e4 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 6e  LITE_OK!=(rc = n
3a1e5 6f 64 65 57 72 69 74 65 28 70 52 74 72 65 65 2c  odeWrite(pRtree,
3a1e6 20 70 4c 65 66 74 29 29 29 0a 20 20 29 7b 0a 20   pLeft))).  ){. 
3a1e7 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64     goto splitnod
3a1e8 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 72 69  e_out;.  }..  ri
3a1e9 67 68 74 62 62 6f 78 2e 69 52 6f 77 69 64 20 3d  ghtbbox.iRowid =
3a1ea 20 70 52 69 67 68 74 2d 3e 69 4e 6f 64 65 3b 0a   pRight->iNode;.
3a1eb 20 20 6c 65 66 74 62 62 6f 78 2e 69 52 6f 77 69    leftbbox.iRowi
3a1ec 64 20 3d 20 70 4c 65 66 74 2d 3e 69 4e 6f 64 65  d = pLeft->iNode
3a1ed 3b 0a 0a 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e  ;..  if( pNode->
3a1ee 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20 20 20  iNode==1 ){.    
3a1ef 72 63 20 3d 20 72 74 72 65 65 49 6e 73 65 72 74  rc = rtreeInsert
3a1f0 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4c 65  Cell(pRtree, pLe
3a1f1 66 74 2d 3e 70 50 61 72 65 6e 74 2c 20 26 6c 65  ft->pParent, &le
3a1f2 66 74 62 62 6f 78 2c 20 69 48 65 69 67 68 74 2b  ftbbox, iHeight+
3a1f3 31 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  1);.    if( rc!=
3a1f4 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
3a1f5 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64     goto splitnod
3a1f6 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  e_out;.    }.  }
3a1f7 65 6c 73 65 7b 0a 20 20 20 20 52 74 72 65 65 4e  else{.    RtreeN
3a1f8 6f 64 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70  ode *pParent = p
3a1f9 4c 65 66 74 2d 3e 70 50 61 72 65 6e 74 3b 0a 20  Left->pParent;. 
3a1fa 20 20 20 69 6e 74 20 69 43 65 6c 6c 20 3d 20 6e     int iCell = n
3a1fb 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78 28 70  odeParentIndex(p
3a1fc 52 74 72 65 65 2c 20 70 4c 65 66 74 29 3b 0a 20  Rtree, pLeft);. 
3a1fd 20 20 20 6e 6f 64 65 4f 76 65 72 77 72 69 74 65     nodeOverwrite
3a1fe 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 50 61  Cell(pRtree, pPa
3a1ff 72 65 6e 74 2c 20 26 6c 65 66 74 62 62 6f 78 2c  rent, &leftbbox,
3a200 20 69 43 65 6c 6c 29 3b 0a 20 20 20 20 41 64 6a   iCell);.    Adj
3a201 75 73 74 54 72 65 65 28 70 52 74 72 65 65 2c 20  ustTree(pRtree, 
3a202 70 50 61 72 65 6e 74 2c 20 26 6c 65 66 74 62 62  pParent, &leftbb
3a203 6f 78 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 28  ox);.  }.  if( (
3a204 72 63 20 3d 20 72 74 72 65 65 49 6e 73 65 72 74  rc = rtreeInsert
3a205 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 52 69  Cell(pRtree, pRi
3a206 67 68 74 2d 3e 70 50 61 72 65 6e 74 2c 20 26 72  ght->pParent, &r
3a207 69 67 68 74 62 62 6f 78 2c 20 69 48 65 69 67 68  ightbbox, iHeigh
3a208 74 2b 31 29 29 20 29 7b 0a 20 20 20 20 67 6f 74  t+1)) ){.    got
3a209 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b  o splitnode_out;
3a20a 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 3b  .  }..  for(i=0;
3a20b 20 69 3c 4e 43 45 4c 4c 28 70 52 69 67 68 74 29   i<NCELL(pRight)
3a20c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 36 34 20  ; i++){.    i64 
3a20d 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65 74  iRowid = nodeGet
3a20e 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70 52  Rowid(pRtree, pR
3a20f 69 67 68 74 2c 20 69 29 3b 0a 20 20 20 20 72 63  ight, i);.    rc
3a210 20 3d 20 75 70 64 61 74 65 4d 61 70 70 69 6e 67   = updateMapping
3a211 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64 2c  (pRtree, iRowid,
3a212 20 70 52 69 67 68 74 2c 20 69 48 65 69 67 68 74   pRight, iHeight
3a213 29 3b 0a 20 20 20 20 69 66 28 20 69 52 6f 77 69  );.    if( iRowi
3a214 64 3d 3d 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64  d==pCell->iRowid
3a215 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 43 65 6c   ){.      newCel
3a216 6c 49 73 52 69 67 68 74 20 3d 20 31 3b 0a 20 20  lIsRight = 1;.  
3a217 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d    }.    if( rc!=
3a218 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
3a219 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64     goto splitnod
3a21a 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  e_out;.    }.  }
3a21b 0a 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e  .  if( pNode->iN
3a21c 6f 64 65 3d 3d 31 20 29 7b 0a 20 20 20 20 66 6f  ode==1 ){.    fo
3a21d 72 28 69 3d 30 3b 20 69 3c 4e 43 45 4c 4c 28 70  r(i=0; i<NCELL(p
3a21e 4c 65 66 74 29 3b 20 69 2b 2b 29 7b 0a 20 20 20  Left); i++){.   
3a21f 20 20 20 69 36 34 20 69 52 6f 77 69 64 20 3d 20     i64 iRowid = 
3a220 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70 52 74  nodeGetRowid(pRt
3a221 72 65 65 2c 20 70 4c 65 66 74 2c 20 69 29 3b 0a  ree, pLeft, i);.
3a222 20 20 20 20 20 20 72 63 20 3d 20 75 70 64 61 74        rc = updat
3a223 65 4d 61 70 70 69 6e 67 28 70 52 74 72 65 65 2c  eMapping(pRtree,
3a224 20 69 52 6f 77 69 64 2c 20 70 4c 65 66 74 2c 20   iRowid, pLeft, 
3a225 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20 20 20  iHeight);.      
3a226 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
3a227 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74  K ){.        got
3a228 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b  o splitnode_out;
3a229 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
3a22a 20 7d 65 6c 73 65 20 69 66 28 20 6e 65 77 43 65   }else if( newCe
3a22b 6c 6c 49 73 52 69 67 68 74 3d 3d 30 20 29 7b 0a  llIsRight==0 ){.
3a22c 20 20 20 20 72 63 20 3d 20 75 70 64 61 74 65 4d      rc = updateM
3a22d 61 70 70 69 6e 67 28 70 52 74 72 65 65 2c 20 70  apping(pRtree, p
3a22e 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 2c 20 70 4c  Cell->iRowid, pL
3a22f 65 66 74 2c 20 69 48 65 69 67 68 74 29 3b 0a 20  eft, iHeight);. 
3a230 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   }..  if( rc==SQ
3a231 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
3a232 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28  c = nodeRelease(
3a233 70 52 74 72 65 65 2c 20 70 52 69 67 68 74 29 3b  pRtree, pRight);
3a234 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20 30 3b  .    pRight = 0;
3a235 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
3a236 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
3a237 72 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65  rc = nodeRelease
3a238 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 29 3b  (pRtree, pLeft);
3a239 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 30 3b 0a  .    pLeft = 0;.
3a23a 20 20 7d 0a 0a 73 70 6c 69 74 6e 6f 64 65 5f 6f    }..splitnode_o
3a23b 75 74 3a 0a 20 20 6e 6f 64 65 52 65 6c 65 61 73  ut:.  nodeReleas
3a23c 65 28 70 52 74 72 65 65 2c 20 70 52 69 67 68 74  e(pRtree, pRight
3a23d 29 3b 0a 20 20 6e 6f 64 65 52 65 6c 65 61 73 65  );.  nodeRelease
3a23e 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 29 3b  (pRtree, pLeft);
3a23f 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
3a240 61 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e  aCell);.  return
3a241 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69   rc;.}..static i
3a242 6e 74 20 66 69 78 4c 65 61 66 50 61 72 65 6e 74  nt fixLeafParent
3a243 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20  (Rtree *pRtree, 
3a244 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65 61 66  RtreeNode *pLeaf
3a245 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
3a246 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70  LITE_OK;.  if( p
3a247 4c 65 61 66 2d 3e 69 4e 6f 64 65 21 3d 31 20 26  Leaf->iNode!=1 &
3a248 26 20 70 4c 65 61 66 2d 3e 70 50 61 72 65 6e 74  & pLeaf->pParent
3a249 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
3a24a 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52  e3_bind_int64(pR
3a24b 74 72 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e  tree->pReadParen
3a24c 74 2c 20 31 2c 20 70 4c 65 61 66 2d 3e 69 4e 6f  t, 1, pLeaf->iNo
3a24d 64 65 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  de);.    if( sql
3a24e 69 74 65 33 5f 73 74 65 70 28 70 52 74 72 65 65  ite3_step(pRtree
3a24f 2d 3e 70 52 65 61 64 50 61 72 65 6e 74 29 3d 3d  ->pReadParent)==
3a250 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
3a251 20 20 20 20 69 36 34 20 69 4e 6f 64 65 20 3d 20      i64 iNode = 
3a252 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
3a253 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52 65  nt64(pRtree->pRe
3a254 61 64 50 61 72 65 6e 74 2c 20 30 29 3b 0a 20 20  adParent, 0);.  
3a255 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71      rc = nodeAcq
3a256 75 69 72 65 28 70 52 74 72 65 65 2c 20 69 4e 6f  uire(pRtree, iNo
3a257 64 65 2c 20 30 2c 20 26 70 4c 65 61 66 2d 3e 70  de, 0, &pLeaf->p
3a258 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d 65 6c  Parent);.    }el
3a259 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  se{.      rc = S
3a25a 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
3a25b 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72   }.    sqlite3_r
3a25c 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65  eset(pRtree->pRe
3a25d 61 64 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 69  adParent);.    i
3a25e 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
3a25f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66   ){.      rc = f
3a260 69 78 4c 65 61 66 50 61 72 65 6e 74 28 70 52 74  ixLeafParent(pRt
3a261 72 65 65 2c 20 70 4c 65 61 66 2d 3e 70 50 61 72  ree, pLeaf->pPar
3a262 65 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ent);.    }.  }.
3a263 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
3a264 73 74 61 74 69 63 20 69 6e 74 20 64 65 6c 65 74  static int delet
3a265 65 43 65 6c 6c 28 52 74 72 65 65 20 2a 2c 20 52  eCell(Rtree *, R
3a266 74 72 65 65 4e 6f 64 65 20 2a 2c 20 69 6e 74 2c  treeNode *, int,
3a267 20 69 6e 74 29 3b 0a 0a 73 74 61 74 69 63 20 69   int);..static i
3a268 6e 74 20 72 65 6d 6f 76 65 4e 6f 64 65 28 52 74  nt removeNode(Rt
3a269 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
3a26a 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 69  eeNode *pNode, i
3a26b 6e 74 20 69 48 65 69 67 68 74 29 7b 0a 20 20 69  nt iHeight){.  i
3a26c 6e 74 20 72 63 3b 0a 20 20 52 74 72 65 65 4e 6f  nt rc;.  RtreeNo
3a26d 64 65 20 2a 70 50 61 72 65 6e 74 3b 0a 20 20 69  de *pParent;.  i
3a26e 6e 74 20 69 43 65 6c 6c 3b 0a 0a 20 20 61 73 73  nt iCell;..  ass
3a26f 65 72 74 28 20 70 4e 6f 64 65 2d 3e 6e 52 65 66  ert( pNode->nRef
3a270 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 52 65 6d  ==1 );..  /* Rem
3a271 6f 76 65 20 74 68 65 20 65 6e 74 72 79 20 69 6e  ove the entry in
3a272 20 74 68 65 20 70 61 72 65 6e 74 20 63 65 6c 6c   the parent cell
3a273 2e 20 2a 2f 0a 20 20 69 43 65 6c 6c 20 3d 20 6e  . */.  iCell = n
3a274 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78 28 70  odeParentIndex(p
3a275 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20  Rtree, pNode);. 
3a276 20 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65   pParent = pNode
3a277 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 70 4e 6f  ->pParent;.  pNo
3a278 64 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 30 3b  de->pParent = 0;
3a279 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b  .  if( SQLITE_OK
3a27a 21 3d 28 72 63 20 3d 20 64 65 6c 65 74 65 43 65  !=(rc = deleteCe
3a27b 6c 6c 28 70 52 74 72 65 65 2c 20 70 50 61 72 65  ll(pRtree, pPare
3a27c 6e 74 2c 20 69 43 65 6c 6c 2c 20 69 48 65 69 67  nt, iCell, iHeig
3a27d 68 74 2b 31 29 29 20 0a 20 20 20 7c 7c 20 53 51  ht+1)) .   || SQ
3a27e 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 6e  LITE_OK!=(rc = n
3a27f 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
3a280 65 2c 20 70 50 61 72 65 6e 74 29 29 0a 20 20 29  e, pParent)).  )
3a281 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
3a282 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76  .  }..  /* Remov
3a283 65 20 74 68 65 20 78 78 78 5f 6e 6f 64 65 20 65  e the xxx_node e
3a284 6e 74 72 79 2e 20 2a 2f 0a 20 20 73 71 6c 69 74  ntry. */.  sqlit
3a285 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52  e3_bind_int64(pR
3a286 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 4e 6f 64  tree->pDeleteNod
3a287 65 2c 20 31 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f  e, 1, pNode->iNo
3a288 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  de);.  sqlite3_s
3a289 74 65 70 28 70 52 74 72 65 65 2d 3e 70 44 65 6c  tep(pRtree->pDel
3a28a 65 74 65 4e 6f 64 65 29 3b 0a 20 20 69 66 28 20  eteNode);.  if( 
3a28b 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
3a28c 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
3a28d 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 4e 6f  Rtree->pDeleteNo
3a28e 64 65 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75  de)) ){.    retu
3a28f 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  rn rc;.  }..  /*
3a290 20 52 65 6d 6f 76 65 20 74 68 65 20 78 78 78 5f   Remove the xxx_
3a291 70 61 72 65 6e 74 20 65 6e 74 72 79 2e 20 2a 2f  parent entry. */
3a292 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  .  sqlite3_bind_
3a293 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 44  int64(pRtree->pD
3a294 65 6c 65 74 65 50 61 72 65 6e 74 2c 20 31 2c 20  eleteParent, 1, 
3a295 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20  pNode->iNode);. 
3a296 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52   sqlite3_step(pR
3a297 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61 72  tree->pDeletePar
3a298 65 6e 74 29 3b 0a 20 20 69 66 28 20 53 51 4c 49  ent);.  if( SQLI
3a299 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c  TE_OK!=(rc = sql
3a29a 69 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65  ite3_reset(pRtre
3a29b 65 2d 3e 70 44 65 6c 65 74 65 50 61 72 65 6e 74  e->pDeleteParent
3a29c 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  )) ){.    return
3a29d 20 72 63 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a   rc;.  }.  .  /*
3a29e 20 52 65 6d 6f 76 65 20 74 68 65 20 6e 6f 64 65   Remove the node
3a29f 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d 65 6d   from the in-mem
3a2a0 6f 72 79 20 68 61 73 68 20 74 61 62 6c 65 20 61  ory hash table a
3a2a1 6e 64 20 6c 69 6e 6b 20 69 74 20 69 6e 74 6f 0a  nd link it into.
3a2a2 20 20 2a 2a 20 74 68 65 20 52 74 72 65 65 2e 70    ** the Rtree.p
3a2a3 44 65 6c 65 74 65 64 20 6c 69 73 74 2e 20 49 74  Deleted list. It
3a2a4 73 20 63 6f 6e 74 65 6e 74 73 20 77 69 6c 6c 20  s contents will 
3a2a5 62 65 20 72 65 2d 69 6e 73 65 72 74 65 64 20 6c  be re-inserted l
3a2a6 61 74 65 72 20 6f 6e 2e 0a 20 20 2a 2f 0a 20 20  ater on..  */.  
3a2a7 6e 6f 64 65 48 61 73 68 44 65 6c 65 74 65 28 70  nodeHashDelete(p
3a2a8 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20  Rtree, pNode);. 
3a2a9 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 3d 20   pNode->iNode = 
3a2aa 69 48 65 69 67 68 74 3b 0a 20 20 70 4e 6f 64 65  iHeight;.  pNode
3a2ab 2d 3e 70 4e 65 78 74 20 3d 20 70 52 74 72 65 65  ->pNext = pRtree
3a2ac 2d 3e 70 44 65 6c 65 74 65 64 3b 0a 20 20 70 4e  ->pDeleted;.  pN
3a2ad 6f 64 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 70  ode->nRef++;.  p
3a2ae 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 64 20  Rtree->pDeleted 
3a2af 3d 20 70 4e 6f 64 65 3b 0a 0a 20 20 72 65 74 75  = pNode;..  retu
3a2b0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
3a2b1 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66 69 78  .static void fix
3a2b2 42 6f 75 6e 64 69 6e 67 42 6f 78 28 52 74 72 65  BoundingBox(Rtre
3a2b3 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65  e *pRtree, Rtree
3a2b4 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20  Node *pNode){.  
3a2b5 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61 72 65  RtreeNode *pPare
3a2b6 6e 74 20 3d 20 70 4e 6f 64 65 2d 3e 70 50 61 72  nt = pNode->pPar
3a2b7 65 6e 74 3b 0a 20 20 69 66 28 20 70 50 61 72 65  ent;.  if( pPare
3a2b8 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 69  nt ){.    int ii
3a2b9 3b 20 0a 20 20 20 20 69 6e 74 20 6e 43 65 6c 6c  ; .    int nCell
3a2ba 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 3b   = NCELL(pNode);
3a2bb 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 62  .    RtreeCell b
3a2bc 6f 78 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ox;             
3a2bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3a2be 2a 20 42 6f 75 6e 64 69 6e 67 20 62 6f 78 20 66  * Bounding box f
3a2bf 6f 72 20 70 4e 6f 64 65 20 2a 2f 0a 20 20 20 20  or pNode */.    
3a2c0 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72  nodeGetCell(pRtr
3a2c1 65 65 2c 20 70 4e 6f 64 65 2c 20 30 2c 20 26 62  ee, pNode, 0, &b
3a2c2 6f 78 29 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d  ox);.    for(ii=
3a2c3 31 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b  1; ii<nCell; ii+
3a2c4 2b 29 7b 0a 20 20 20 20 20 20 52 74 72 65 65 43  +){.      RtreeC
3a2c5 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20 20 20 20  ell cell;.      
3a2c6 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72  nodeGetCell(pRtr
3a2c7 65 65 2c 20 70 4e 6f 64 65 2c 20 69 69 2c 20 26  ee, pNode, ii, &
3a2c8 63 65 6c 6c 29 3b 0a 20 20 20 20 20 20 63 65 6c  cell);.      cel
3a2c9 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 26  lUnion(pRtree, &
3a2ca 62 6f 78 2c 20 26 63 65 6c 6c 29 3b 0a 20 20 20  box, &cell);.   
3a2cb 20 7d 0a 20 20 20 20 62 6f 78 2e 69 52 6f 77 69   }.    box.iRowi
3a2cc 64 20 3d 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  d = pNode->iNode
3a2cd 3b 0a 20 20 20 20 69 69 20 3d 20 6e 6f 64 65 50  ;.    ii = nodeP
3a2ce 61 72 65 6e 74 49 6e 64 65 78 28 70 52 74 72 65  arentIndex(pRtre
3a2cf 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 6e  e, pNode);.    n
3a2d0 6f 64 65 4f 76 65 72 77 72 69 74 65 43 65 6c 6c  odeOverwriteCell
3a2d1 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74  (pRtree, pParent
3a2d2 2c 20 26 62 6f 78 2c 20 69 69 29 3b 0a 20 20 20  , &box, ii);.   
3a2d3 20 66 69 78 42 6f 75 6e 64 69 6e 67 42 6f 78 28   fixBoundingBox(
3a2d4 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 29  pRtree, pParent)
3a2d5 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  ;.  }.}../*.** D
3a2d6 65 6c 65 74 65 20 74 68 65 20 63 65 6c 6c 20 61  elete the cell a
3a2d7 74 20 69 6e 64 65 78 20 69 43 65 6c 6c 20 6f 66  t index iCell of
3a2d8 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 41 66 74   node pNode. Aft
3a2d9 65 72 20 72 65 6d 6f 76 69 6e 67 20 74 68 65 0a  er removing the.
3a2da 2a 2a 20 63 65 6c 6c 2c 20 61 64 6a 75 73 74 20  ** cell, adjust 
3a2db 74 68 65 20 72 2d 74 72 65 65 20 64 61 74 61 20  the r-tree data 
3a2dc 73 74 72 75 63 74 75 72 65 20 69 66 20 72 65 71  structure if req
3a2dd 75 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  uired..*/.static
3a2de 20 69 6e 74 20 64 65 6c 65 74 65 43 65 6c 6c 28   int deleteCell(
3a2df 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52  Rtree *pRtree, R
3a2e0 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c  treeNode *pNode,
3a2e1 20 69 6e 74 20 69 43 65 6c 6c 2c 20 69 6e 74 20   int iCell, int 
3a2e2 69 48 65 69 67 68 74 29 7b 0a 20 20 69 6e 74 20  iHeight){.  int 
3a2e3 72 63 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54  rc;..  if( SQLIT
3a2e4 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 66 69 78 4c  E_OK!=(rc = fixL
3a2e5 65 61 66 50 61 72 65 6e 74 28 70 52 74 72 65 65  eafParent(pRtree
3a2e6 2c 20 70 4e 6f 64 65 29 29 20 29 7b 0a 20 20 20  , pNode)) ){.   
3a2e7 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
3a2e8 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65  .  /* Remove the
3a2e9 20 63 65 6c 6c 20 66 72 6f 6d 20 74 68 65 20 6e   cell from the n
3a2ea 6f 64 65 2e 20 54 68 69 73 20 63 61 6c 6c 20 6a  ode. This call j
3a2eb 75 73 74 20 6d 6f 76 65 73 20 62 79 74 65 73 20  ust moves bytes 
3a2ec 61 72 6f 75 6e 64 0a 20 20 2a 2a 20 74 68 65 20  around.  ** the 
3a2ed 69 6e 2d 6d 65 6d 6f 72 79 20 6e 6f 64 65 20 69  in-memory node i
3a2ee 6d 61 67 65 2c 20 73 6f 20 69 74 20 63 61 6e 6e  mage, so it cann
3a2ef 6f 74 20 66 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20  ot fail..  */.  
3a2f0 6e 6f 64 65 44 65 6c 65 74 65 43 65 6c 6c 28 70  nodeDeleteCell(p
3a2f1 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 43  Rtree, pNode, iC
3a2f2 65 6c 6c 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  ell);..  /* If t
3a2f3 68 65 20 6e 6f 64 65 20 69 73 20 6e 6f 74 20 74  he node is not t
3a2f4 68 65 20 74 72 65 65 20 72 6f 6f 74 20 61 6e 64  he tree root and
3a2f5 20 6e 6f 77 20 68 61 73 20 6c 65 73 73 20 74 68   now has less th
3a2f6 61 6e 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a 20  an the minimum. 
3a2f7 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 65   ** number of ce
3a2f8 6c 6c 73 2c 20 72 65 6d 6f 76 65 20 69 74 20 66  lls, remove it f
3a2f9 72 6f 6d 20 74 68 65 20 74 72 65 65 2e 20 4f 74  rom the tree. Ot
3a2fa 68 65 72 77 69 73 65 2c 20 75 70 64 61 74 65 20  herwise, update 
3a2fb 74 68 65 0a 20 20 2a 2a 20 63 65 6c 6c 20 69 6e  the.  ** cell in
3a2fc 20 74 68 65 20 70 61 72 65 6e 74 20 6e 6f 64 65   the parent node
3a2fd 20 73 6f 20 74 68 61 74 20 69 74 20 74 69 67 68   so that it tigh
3a2fe 74 6c 79 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  tly contains the
3a2ff 20 75 70 64 61 74 65 64 0a 20 20 2a 2a 20 6e 6f   updated.  ** no
3a300 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  de..  */.  if( p
3a301 4e 6f 64 65 2d 3e 69 4e 6f 64 65 21 3d 31 20 29  Node->iNode!=1 )
3a302 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20  {.    RtreeNode 
3a303 2a 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f 64 65  *pParent = pNode
3a304 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20 20 69  ->pParent;.    i
3a305 66 28 20 28 70 50 61 72 65 6e 74 2d 3e 69 4e 6f  f( (pParent->iNo
3a306 64 65 21 3d 31 20 7c 7c 20 4e 43 45 4c 4c 28 70  de!=1 || NCELL(p
3a307 50 61 72 65 6e 74 29 21 3d 31 29 20 0a 20 20 20  Parent)!=1) .   
3a308 20 20 26 26 20 28 4e 43 45 4c 4c 28 70 4e 6f 64    && (NCELL(pNod
3a309 65 29 3c 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c  e)<RTREE_MINCELL
3a30a 53 28 70 52 74 72 65 65 29 29 0a 20 20 20 20 29  S(pRtree)).    )
3a30b 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 65 6d  {.      rc = rem
3a30c 6f 76 65 4e 6f 64 65 28 70 52 74 72 65 65 2c 20  oveNode(pRtree, 
3a30d 70 4e 6f 64 65 2c 20 69 48 65 69 67 68 74 29 3b  pNode, iHeight);
3a30e 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
3a30f 20 20 66 69 78 42 6f 75 6e 64 69 6e 67 42 6f 78    fixBoundingBox
3a310 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b  (pRtree, pNode);
3a311 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65  .    }.  }..  re
3a312 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74  turn rc;.}..stat
3a313 69 63 20 69 6e 74 20 52 65 69 6e 73 65 72 74 28  ic int Reinsert(
3a314 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
3a315 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  , .  RtreeNode *
3a316 70 4e 6f 64 65 2c 20 0a 20 20 52 74 72 65 65 43  pNode, .  RtreeC
3a317 65 6c 6c 20 2a 70 43 65 6c 6c 2c 20 0a 20 20 69  ell *pCell, .  i
3a318 6e 74 20 69 48 65 69 67 68 74 0a 29 7b 0a 20 20  nt iHeight.){.  
3a319 69 6e 74 20 2a 61 4f 72 64 65 72 3b 0a 20 20 69  int *aOrder;.  i
3a31a 6e 74 20 2a 61 53 70 61 72 65 3b 0a 20 20 52 74  nt *aSpare;.  Rt
3a31b 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 3b 0a  reeCell *aCell;.
3a31c 20 20 66 6c 6f 61 74 20 2a 61 44 69 73 74 61 6e    float *aDistan
3a31d 63 65 3b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b  ce;.  int nCell;
3a31e 0a 20 20 66 6c 6f 61 74 20 61 43 65 6e 74 65 72  .  float aCenter
3a31f 43 6f 6f 72 64 5b 52 54 52 45 45 5f 4d 41 58 5f  Coord[RTREE_MAX_
3a320 44 49 4d 45 4e 53 49 4f 4e 53 5d 3b 0a 20 20 69  DIMENSIONS];.  i
3a321 6e 74 20 69 44 69 6d 3b 0a 20 20 69 6e 74 20 69  nt iDim;.  int i
3a322 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  i;.  int rc = SQ
3a323 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 6d 65 6d 73  LITE_OK;..  mems
3a324 65 74 28 61 43 65 6e 74 65 72 43 6f 6f 72 64 2c  et(aCenterCoord,
3a325 20 30 2c 20 73 69 7a 65 6f 66 28 66 6c 6f 61 74   0, sizeof(float
3a326 29 2a 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45  )*RTREE_MAX_DIME
3a327 4e 53 49 4f 4e 53 29 3b 0a 0a 20 20 6e 43 65 6c  NSIONS);..  nCel
3a328 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29  l = NCELL(pNode)
3a329 2b 31 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61  +1;..  /* Alloca
3a32a 74 65 20 74 68 65 20 62 75 66 66 65 72 73 20 75  te the buffers u
3a32b 73 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 72  sed by this oper
3a32c 61 74 69 6f 6e 2e 20 54 68 65 20 61 6c 6c 6f 63  ation. The alloc
3a32d 61 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 72 65  ation is.  ** re
3a32e 6c 69 6e 71 75 69 73 68 65 64 20 62 65 66 6f 72  linquished befor
3a32f 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  e this function 
3a330 72 65 74 75 72 6e 73 2e 0a 20 20 2a 2f 0a 20 20  returns..  */.  
3a331 61 43 65 6c 6c 20 3d 20 28 52 74 72 65 65 43 65  aCell = (RtreeCe
3a332 6c 6c 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  ll *)sqlite3_mal
3a333 6c 6f 63 28 6e 43 65 6c 6c 20 2a 20 28 0a 20 20  loc(nCell * (.  
3a334 20 20 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65    sizeof(RtreeCe
3a335 6c 6c 29 20 2b 20 20 20 20 20 20 20 20 20 2f 2a  ll) +         /*
3a336 20 61 43 65 6c 6c 20 61 72 72 61 79 20 2a 2f 0a   aCell array */.
3a337 20 20 20 20 73 69 7a 65 6f 66 28 69 6e 74 29 20      sizeof(int) 
3a338 20 20 20 20 20 20 2b 20 20 20 20 20 20 20 20 20        +         
3a339 2f 2a 20 61 4f 72 64 65 72 20 61 72 72 61 79 20  /* aOrder array 
3a33a 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 69 6e  */.    sizeof(in
3a33b 74 29 20 20 20 20 20 20 20 2b 20 20 20 20 20 20  t)       +      
3a33c 20 20 20 2f 2a 20 61 53 70 61 72 65 20 61 72 72     /* aSpare arr
3a33d 61 79 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66  ay */.    sizeof
3a33e 28 66 6c 6f 61 74 29 20 20 20 20 20 20 20 20 20  (float)         
3a33f 20 20 20 20 20 20 2f 2a 20 61 44 69 73 74 61 6e        /* aDistan
3a340 63 65 20 61 72 72 61 79 20 2a 2f 0a 20 20 29 29  ce array */.  ))
3a341 3b 0a 20 20 69 66 28 20 21 61 43 65 6c 6c 20 29  ;.  if( !aCell )
3a342 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
3a343 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
3a344 20 61 4f 72 64 65 72 20 20 20 20 3d 20 28 69 6e   aOrder    = (in
3a345 74 20 2a 29 26 61 43 65 6c 6c 5b 6e 43 65 6c 6c  t *)&aCell[nCell
3a346 5d 3b 0a 20 20 61 53 70 61 72 65 20 20 20 20 3d  ];.  aSpare    =
3a347 20 28 69 6e 74 20 2a 29 26 61 4f 72 64 65 72 5b   (int *)&aOrder[
3a348 6e 43 65 6c 6c 5d 3b 0a 20 20 61 44 69 73 74 61  nCell];.  aDista
3a349 6e 63 65 20 3d 20 28 66 6c 6f 61 74 20 2a 29 26  nce = (float *)&
3a34a 61 53 70 61 72 65 5b 6e 43 65 6c 6c 5d 3b 0a 0a  aSpare[nCell];..
3a34b 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e    for(ii=0; ii<n
3a34c 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  Cell; ii++){.   
3a34d 20 69 66 28 20 69 69 3d 3d 28 6e 43 65 6c 6c 2d   if( ii==(nCell-
3a34e 31 29 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63  1) ){.      memc
3a34f 70 79 28 26 61 43 65 6c 6c 5b 69 69 5d 2c 20 70  py(&aCell[ii], p
3a350 43 65 6c 6c 2c 20 73 69 7a 65 6f 66 28 52 74 72  Cell, sizeof(Rtr
3a351 65 65 43 65 6c 6c 29 29 3b 0a 20 20 20 20 7d 65  eeCell));.    }e
3a352 6c 73 65 7b 0a 20 20 20 20 20 20 6e 6f 64 65 47  lse{.      nodeG
3a353 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  etCell(pRtree, p
3a354 4e 6f 64 65 2c 20 69 69 2c 20 26 61 43 65 6c 6c  Node, ii, &aCell
3a355 5b 69 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20  [ii]);.    }.   
3a356 20 61 4f 72 64 65 72 5b 69 69 5d 20 3d 20 69 69   aOrder[ii] = ii
3a357 3b 0a 20 20 20 20 66 6f 72 28 69 44 69 6d 3d 30  ;.    for(iDim=0
3a358 3b 20 69 44 69 6d 3c 70 52 74 72 65 65 2d 3e 6e  ; iDim<pRtree->n
3a359 44 69 6d 3b 20 69 44 69 6d 2b 2b 29 7b 0a 20 20  Dim; iDim++){.  
3a35a 20 20 20 20 61 43 65 6e 74 65 72 43 6f 6f 72 64      aCenterCoord
3a35b 5b 69 44 69 6d 5d 20 2b 3d 20 44 43 4f 4f 52 44  [iDim] += DCOORD
3a35c 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72  (aCell[ii].aCoor
3a35d 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20 20 20 20  d[iDim*2]);.    
3a35e 20 20 61 43 65 6e 74 65 72 43 6f 6f 72 64 5b 69    aCenterCoord[i
3a35f 44 69 6d 5d 20 2b 3d 20 44 43 4f 4f 52 44 28 61  Dim] += DCOORD(a
3a360 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72 64 5b  Cell[ii].aCoord[
3a361 69 44 69 6d 2a 32 2b 31 5d 29 3b 0a 20 20 20 20  iDim*2+1]);.    
3a362 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 44 69 6d  }.  }.  for(iDim
3a363 3d 30 3b 20 69 44 69 6d 3c 70 52 74 72 65 65 2d  =0; iDim<pRtree-
3a364 3e 6e 44 69 6d 3b 20 69 44 69 6d 2b 2b 29 7b 0a  >nDim; iDim++){.
3a365 20 20 20 20 61 43 65 6e 74 65 72 43 6f 6f 72 64      aCenterCoord
3a366 5b 69 44 69 6d 5d 20 3d 20 61 43 65 6e 74 65 72  [iDim] = aCenter
3a367 43 6f 6f 72 64 5b 69 44 69 6d 5d 2f 28 28 66 6c  Coord[iDim]/((fl
3a368 6f 61 74 29 6e 43 65 6c 6c 2a 32 2e 30 29 3b 0a  oat)nCell*2.0);.
3a369 20 20 7d 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b    }..  for(ii=0;
3a36a 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29   ii<nCell; ii++)
3a36b 7b 0a 20 20 20 20 61 44 69 73 74 61 6e 63 65 5b  {.    aDistance[
3a36c 69 69 5d 20 3d 20 30 2e 30 3b 0a 20 20 20 20 66  ii] = 0.0;.    f
3a36d 6f 72 28 69 44 69 6d 3d 30 3b 20 69 44 69 6d 3c  or(iDim=0; iDim<
3a36e 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 44  pRtree->nDim; iD
3a36f 69 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6c 6f  im++){.      flo
3a370 61 74 20 63 6f 6f 72 64 20 3d 20 44 43 4f 4f 52  at coord = DCOOR
3a371 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f  D(aCell[ii].aCoo
3a372 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 29 20 2d 20  rd[iDim*2+1]) - 
3a373 0a 20 20 20 20 20 20 20 20 20 20 44 43 4f 4f 52  .          DCOOR
3a374 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f  D(aCell[ii].aCoo
3a375 72 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20 20 20  rd[iDim*2]);.   
3a376 20 20 20 61 44 69 73 74 61 6e 63 65 5b 69 69 5d     aDistance[ii]
3a377 20 2b 3d 20 28 63 6f 6f 72 64 2d 61 43 65 6e 74   += (coord-aCent
3a378 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 29 2a 28  erCoord[iDim])*(
3a379 63 6f 6f 72 64 2d 61 43 65 6e 74 65 72 43 6f 6f  coord-aCenterCoo
3a37a 72 64 5b 69 44 69 6d 5d 29 3b 0a 20 20 20 20 7d  rd[iDim]);.    }
3a37b 0a 20 20 7d 0a 0a 20 20 53 6f 72 74 42 79 44 69  .  }..  SortByDi
3a37c 73 74 61 6e 63 65 28 61 4f 72 64 65 72 2c 20 6e  stance(aOrder, n
3a37d 43 65 6c 6c 2c 20 61 44 69 73 74 61 6e 63 65 2c  Cell, aDistance,
3a37e 20 61 53 70 61 72 65 29 3b 0a 20 20 6e 6f 64 65   aSpare);.  node
3a37f 5a 65 72 6f 28 70 52 74 72 65 65 2c 20 70 4e 6f  Zero(pRtree, pNo
3a380 64 65 29 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30  de);..  for(ii=0
3a381 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ; rc==SQLITE_OK 
3a382 26 26 20 69 69 3c 28 6e 43 65 6c 6c 2d 28 52 54  && ii<(nCell-(RT
3a383 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74  REE_MINCELLS(pRt
3a384 72 65 65 29 2b 31 29 29 3b 20 69 69 2b 2b 29 7b  ree)+1)); ii++){
3a385 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  .    RtreeCell *
3a386 70 20 3d 20 26 61 43 65 6c 6c 5b 61 4f 72 64 65  p = &aCell[aOrde
3a387 72 5b 69 69 5d 5d 3b 0a 20 20 20 20 6e 6f 64 65  r[ii]];.    node
3a388 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72 65  InsertCell(pRtre
3a389 65 2c 20 70 4e 6f 64 65 2c 20 70 29 3b 0a 20 20  e, pNode, p);.  
3a38a 20 20 69 66 28 20 70 2d 3e 69 52 6f 77 69 64 3d    if( p->iRowid=
3a38b 3d 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 20 29  =pCell->iRowid )
3a38c 7b 0a 20 20 20 20 20 20 69 66 28 20 69 48 65 69  {.      if( iHei
3a38d 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ght==0 ){.      
3a38e 20 20 72 63 20 3d 20 72 6f 77 69 64 57 72 69 74    rc = rowidWrit
3a38f 65 28 70 52 74 72 65 65 2c 20 70 2d 3e 69 52 6f  e(pRtree, p->iRo
3a390 77 69 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64  wid, pNode->iNod
3a391 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  e);.      }else{
3a392 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 61  .        rc = pa
3a393 72 65 6e 74 57 72 69 74 65 28 70 52 74 72 65 65  rentWrite(pRtree
3a394 2c 20 70 2d 3e 69 52 6f 77 69 64 2c 20 70 4e 6f  , p->iRowid, pNo
3a395 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20  de->iNode);.    
3a396 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
3a397 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3a398 4b 20 29 7b 0a 20 20 20 20 66 69 78 42 6f 75 6e  K ){.    fixBoun
3a399 64 69 6e 67 42 6f 78 28 70 52 74 72 65 65 2c 20  dingBox(pRtree, 
3a39a 70 4e 6f 64 65 29 3b 0a 20 20 7d 0a 20 20 66 6f  pNode);.  }.  fo
3a39b 72 28 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  r(; rc==SQLITE_O
3a39c 4b 20 26 26 20 69 69 3c 6e 43 65 6c 6c 3b 20 69  K && ii<nCell; i
3a39d 69 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20 46 69 6e  i++){.    /* Fin
3a39e 64 20 61 20 6e 6f 64 65 20 74 6f 20 73 74 6f 72  d a node to stor
3a39f 65 20 74 68 69 73 20 63 65 6c 6c 20 69 6e 2e 20  e this cell in. 
3a3a0 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 63 75 72  pNode->iNode cur
3a3a1 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 0a  rently contains.
3a3a2 20 20 20 20 2a 2a 20 74 68 65 20 68 65 69 67 68      ** the heigh
3a3a3 74 20 6f 66 20 74 68 65 20 73 75 62 2d 74 72 65  t of the sub-tre
3a3a4 65 20 68 65 61 64 65 64 20 62 79 20 74 68 65 20  e headed by the 
3a3a5 63 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  cell..    */.   
3a3a6 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 49 6e 73   RtreeNode *pIns
3a3a7 65 72 74 3b 0a 20 20 20 20 52 74 72 65 65 43 65  ert;.    RtreeCe
3a3a8 6c 6c 20 2a 70 20 3d 20 26 61 43 65 6c 6c 5b 61  ll *p = &aCell[a
3a3a9 4f 72 64 65 72 5b 69 69 5d 5d 3b 0a 20 20 20 20  Order[ii]];.    
3a3aa 72 63 20 3d 20 43 68 6f 6f 73 65 4c 65 61 66 28  rc = ChooseLeaf(
3a3ab 70 52 74 72 65 65 2c 20 70 2c 20 69 48 65 69 67  pRtree, p, iHeig
3a3ac 68 74 2c 20 26 70 49 6e 73 65 72 74 29 3b 0a 20  ht, &pInsert);. 
3a3ad 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
3a3ae 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e  E_OK ){.      in
3a3af 74 20 72 63 32 3b 0a 20 20 20 20 20 20 72 63 20  t rc2;.      rc 
3a3b0 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c  = rtreeInsertCel
3a3b1 6c 28 70 52 74 72 65 65 2c 20 70 49 6e 73 65 72  l(pRtree, pInser
3a3b2 74 2c 20 70 2c 20 69 48 65 69 67 68 74 29 3b 0a  t, p, iHeight);.
3a3b3 20 20 20 20 20 20 72 63 32 20 3d 20 6e 6f 64 65        rc2 = node
3a3b4 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
3a3b5 70 49 6e 73 65 72 74 29 3b 0a 20 20 20 20 20 20  pInsert);.      
3a3b6 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
3a3b7 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  K ){.        rc 
3a3b8 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20  = rc2;.      }. 
3a3b9 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71 6c 69     }.  }..  sqli
3a3ba 74 65 33 5f 66 72 65 65 28 61 43 65 6c 6c 29 3b  te3_free(aCell);
3a3bb 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
3a3bc 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 63 65  ./*.** Insert ce
3a3bd 6c 6c 20 70 43 65 6c 6c 20 69 6e 74 6f 20 6e 6f  ll pCell into no
3a3be 64 65 20 70 4e 6f 64 65 2e 20 4e 6f 64 65 20 70  de pNode. Node p
3a3bf 4e 6f 64 65 20 69 73 20 74 68 65 20 68 65 61 64  Node is the head
3a3c0 20 6f 66 20 61 20 0a 2a 2a 20 73 75 62 74 72 65   of a .** subtre
3a3c1 65 20 69 48 65 69 67 68 74 20 68 69 67 68 20 28  e iHeight high (
3a3c2 6c 65 61 66 20 6e 6f 64 65 73 20 68 61 76 65 20  leaf nodes have 
3a3c3 69 48 65 69 67 68 74 3d 3d 30 29 2e 0a 2a 2f 0a  iHeight==0)..*/.
3a3c4 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65  static int rtree
3a3c5 49 6e 73 65 72 74 43 65 6c 6c 28 0a 20 20 52 74  InsertCell(.  Rt
3a3c6 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20 52  ree *pRtree,.  R
3a3c7 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c  treeNode *pNode,
3a3c8 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43  .  RtreeCell *pC
3a3c9 65 6c 6c 2c 0a 20 20 69 6e 74 20 69 48 65 69 67  ell,.  int iHeig
3a3ca 68 74 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ht.){.  int rc =
3a3cb 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
3a3cc 28 20 69 48 65 69 67 68 74 3e 30 20 29 7b 0a 20  ( iHeight>0 ){. 
3a3cd 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43     RtreeNode *pC
3a3ce 68 69 6c 64 20 3d 20 6e 6f 64 65 48 61 73 68 4c  hild = nodeHashL
3a3cf 6f 6f 6b 75 70 28 70 52 74 72 65 65 2c 20 70 43  ookup(pRtree, pC
3a3d0 65 6c 6c 2d 3e 69 52 6f 77 69 64 29 3b 0a 20 20  ell->iRowid);.  
3a3d1 20 20 69 66 28 20 70 43 68 69 6c 64 20 29 7b 0a    if( pChild ){.
3a3d2 20 20 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73        nodeReleas
3a3d3 65 28 70 52 74 72 65 65 2c 20 70 43 68 69 6c 64  e(pRtree, pChild
3a3d4 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20  ->pParent);.    
3a3d5 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28    nodeReference(
3a3d6 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 70 43  pNode);.      pC
3a3d7 68 69 6c 64 2d 3e 70 50 61 72 65 6e 74 20 3d 20  hild->pParent = 
3a3d8 70 4e 6f 64 65 3b 0a 20 20 20 20 7d 0a 20 20 7d  pNode;.    }.  }
3a3d9 0a 20 20 69 66 28 20 6e 6f 64 65 49 6e 73 65 72  .  if( nodeInser
3a3da 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e  tCell(pRtree, pN
3a3db 6f 64 65 2c 20 70 43 65 6c 6c 29 20 29 7b 0a 23  ode, pCell) ){.#
3a3dc 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52  if VARIANT_RSTAR
3a3dd 54 52 45 45 5f 52 45 49 4e 53 45 52 54 0a 20 20  TREE_REINSERT.  
3a3de 20 20 69 66 28 20 69 48 65 69 67 68 74 3c 3d 70    if( iHeight<=p
3a3df 52 74 72 65 65 2d 3e 69 52 65 69 6e 73 65 72 74  Rtree->iReinsert
3a3e0 48 65 69 67 68 74 20 7c 7c 20 70 4e 6f 64 65 2d  Height || pNode-
3a3e1 3e 69 4e 6f 64 65 3d 3d 31 29 7b 0a 20 20 20 20  >iNode==1){.    
3a3e2 20 20 72 63 20 3d 20 53 70 6c 69 74 4e 6f 64 65    rc = SplitNode
3a3e3 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20  (pRtree, pNode, 
3a3e4 70 43 65 6c 6c 2c 20 69 48 65 69 67 68 74 29 3b  pCell, iHeight);
3a3e5 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
3a3e6 20 20 70 52 74 72 65 65 2d 3e 69 52 65 69 6e 73    pRtree->iReins
3a3e7 65 72 74 48 65 69 67 68 74 20 3d 20 69 48 65 69  ertHeight = iHei
3a3e8 67 68 74 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  ght;.      rc = 
3a3e9 52 65 69 6e 73 65 72 74 28 70 52 74 72 65 65 2c  Reinsert(pRtree,
3a3ea 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 2c 20 69   pNode, pCell, i
3a3eb 48 65 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 23  Height);.    }.#
3a3ec 65 6c 73 65 0a 20 20 20 20 72 63 20 3d 20 53 70  else.    rc = Sp
3a3ed 6c 69 74 4e 6f 64 65 28 70 52 74 72 65 65 2c 20  litNode(pRtree, 
3a3ee 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 2c 20 69 48  pNode, pCell, iH
3a3ef 65 69 67 68 74 29 3b 0a 23 65 6e 64 69 66 0a 20  eight);.#endif. 
3a3f0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 41 64 6a 75   }else{.    Adju
3a3f1 73 74 54 72 65 65 28 70 52 74 72 65 65 2c 20 70  stTree(pRtree, p
3a3f2 4e 6f 64 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20  Node, pCell);.  
3a3f3 20 20 69 66 28 20 69 48 65 69 67 68 74 3d 3d 30    if( iHeight==0
3a3f4 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 72   ){.      rc = r
3a3f5 6f 77 69 64 57 72 69 74 65 28 70 52 74 72 65 65  owidWrite(pRtree
3a3f6 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 2c  , pCell->iRowid,
3a3f7 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a   pNode->iNode);.
3a3f8 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3a3f9 20 72 63 20 3d 20 70 61 72 65 6e 74 57 72 69 74   rc = parentWrit
3a3fa 65 28 70 52 74 72 65 65 2c 20 70 43 65 6c 6c 2d  e(pRtree, pCell-
3a3fb 3e 69 52 6f 77 69 64 2c 20 70 4e 6f 64 65 2d 3e  >iRowid, pNode->
3a3fc 69 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20 20  iNode);.    }.  
3a3fd 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
3a3fe 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 69  ..static int rei
3a3ff 6e 73 65 72 74 4e 6f 64 65 43 6f 6e 74 65 6e 74  nsertNodeContent
3a400 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20  (Rtree *pRtree, 
3a401 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
3a402 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 69  ){.  int ii;.  i
3a403 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
3a404 4b 3b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d  K;.  int nCell =
3a405 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 0a   NCELL(pNode);..
3a406 20 20 66 6f 72 28 69 69 3d 30 3b 20 72 63 3d 3d    for(ii=0; rc==
3a407 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 69 3c  SQLITE_OK && ii<
3a408 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20  nCell; ii++){.  
3a409 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 49 6e    RtreeNode *pIn
3a40a 73 65 72 74 3b 0a 20 20 20 20 52 74 72 65 65 43  sert;.    RtreeC
3a40b 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20 20 6e 6f  ell cell;.    no
3a40c 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65  deGetCell(pRtree
3a40d 2c 20 70 4e 6f 64 65 2c 20 69 69 2c 20 26 63 65  , pNode, ii, &ce
3a40e 6c 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e  ll);..    /* Fin
3a40f 64 20 61 20 6e 6f 64 65 20 74 6f 20 73 74 6f 72  d a node to stor
3a410 65 20 74 68 69 73 20 63 65 6c 6c 20 69 6e 2e 20  e this cell in. 
3a411 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 63 75 72  pNode->iNode cur
3a412 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e 73 0a  rently contains.
3a413 20 20 20 20 2a 2a 20 74 68 65 20 68 65 69 67 68      ** the heigh
3a414 74 20 6f 66 20 74 68 65 20 73 75 62 2d 74 72 65  t of the sub-tre
3a415 65 20 68 65 61 64 65 64 20 62 79 20 74 68 65 20  e headed by the 
3a416 63 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  cell..    */.   
3a417 20 72 63 20 3d 20 43 68 6f 6f 73 65 4c 65 61 66   rc = ChooseLeaf
3a418 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20  (pRtree, &cell, 
3a419 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 2c 20 26 70  pNode->iNode, &p
3a41a 49 6e 73 65 72 74 29 3b 0a 20 20 20 20 69 66 28  Insert);.    if(
3a41b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
3a41c 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32 3b  {.      int rc2;
3a41d 0a 20 20 20 20 20 20 72 63 20 3d 20 72 74 72 65  .      rc = rtre
3a41e 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74 72  eInsertCell(pRtr
3a41f 65 65 2c 20 70 49 6e 73 65 72 74 2c 20 26 63 65  ee, pInsert, &ce
3a420 6c 6c 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  ll, pNode->iNode
3a421 29 3b 0a 20 20 20 20 20 20 72 63 32 20 3d 20 6e  );.      rc2 = n
3a422 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
3a423 65 2c 20 70 49 6e 73 65 72 74 29 3b 0a 20 20 20  e, pInsert);.   
3a424 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
3a425 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
3a426 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20  rc = rc2;.      
3a427 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
3a428 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
3a429 2a 20 53 65 6c 65 63 74 20 61 20 63 75 72 72 65  * Select a curre
3a42a 6e 74 6c 79 20 75 6e 75 73 65 64 20 72 6f 77 69  ntly unused rowi
3a42b 64 20 66 6f 72 20 61 20 6e 65 77 20 72 2d 74 72  d for a new r-tr
3a42c 65 65 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a 73 74  ee record..*/.st
3a42d 61 74 69 63 20 69 6e 74 20 6e 65 77 52 6f 77 69  atic int newRowi
3a42e 64 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  d(Rtree *pRtree,
3a42f 20 69 36 34 20 2a 70 69 52 6f 77 69 64 29 7b 0a   i64 *piRowid){.
3a430 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69    int rc;.  sqli
3a431 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 52  te3_bind_null(pR
3a432 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69  tree->pWriteRowi
3a433 64 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74 65 33  d, 1);.  sqlite3
3a434 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 52 74 72 65  _bind_null(pRtre
3a435 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 2c 20  e->pWriteRowid, 
3a436 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  2);.  sqlite3_st
3a437 65 70 28 70 52 74 72 65 65 2d 3e 70 57 72 69 74  ep(pRtree->pWrit
3a438 65 52 6f 77 69 64 29 3b 0a 20 20 72 63 20 3d 20  eRowid);.  rc = 
3a439 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52  sqlite3_reset(pR
3a43a 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69  tree->pWriteRowi
3a43b 64 29 3b 0a 20 20 2a 70 69 52 6f 77 69 64 20 3d  d);.  *piRowid =
3a43c 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e   sqlite3_last_in
3a43d 73 65 72 74 5f 72 6f 77 69 64 28 70 52 74 72 65  sert_rowid(pRtre
3a43e 65 2d 3e 64 62 29 3b 0a 20 20 72 65 74 75 72 6e  e->db);.  return
3a43f 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20   rc;.}..#ifndef 
3a440 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20 69 6e  NDEBUG.static in
3a441 74 20 68 61 73 68 49 73 45 6d 70 74 79 28 52 74  t hashIsEmpty(Rt
3a442 72 65 65 20 2a 70 52 74 72 65 65 29 7b 0a 20 20  ree *pRtree){.  
3a443 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28 69 69  int ii;.  for(ii
3a444 3d 30 3b 20 69 69 3c 48 41 53 48 53 49 5a 45 3b  =0; ii<HASHSIZE;
3a445 20 69 69 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65   ii++){.    asse
3a446 72 74 28 20 21 70 52 74 72 65 65 2d 3e 61 48 61  rt( !pRtree->aHa
3a447 73 68 5b 69 69 5d 20 29 3b 0a 20 20 7d 0a 20 20  sh[ii] );.  }.  
3a448 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64  return 1;.}.#end
3a449 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 78 55  if../*.** The xU
3a44a 70 64 61 74 65 20 6d 65 74 68 6f 64 20 66 6f 72  pdate method for
3a44b 20 72 74 72 65 65 20 6d 6f 64 75 6c 65 20 76 69   rtree module vi
3a44c 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2f  rtual tables..*/
3a44d 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65  .static int rtre
3a44e 65 55 70 64 61 74 65 28 0a 20 20 73 71 6c 69 74  eUpdate(.  sqlit
3a44f 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 2c 20  e3_vtab *pVtab, 
3a450 0a 20 20 69 6e 74 20 6e 44 61 74 61 2c 20 0a 20  .  int nData, . 
3a451 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
3a452 2a 61 7a 44 61 74 61 2c 20 0a 20 20 73 71 6c 69  *azData, .  sqli
3a453 74 65 5f 69 6e 74 36 34 20 2a 70 52 6f 77 69 64  te_int64 *pRowid
3a454 0a 29 7b 0a 20 20 52 74 72 65 65 20 2a 70 52 74  .){.  Rtree *pRt
3a455 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29 70  ree = (Rtree *)p
3a456 56 74 61 62 3b 0a 20 20 69 6e 74 20 72 63 20 3d  Vtab;.  int rc =
3a457 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 72   SQLITE_OK;..  r
3a458 74 72 65 65 52 65 66 65 72 65 6e 63 65 28 70 52  treeReference(pR
3a459 74 72 65 65 29 3b 0a 0a 20 20 61 73 73 65 72 74  tree);..  assert
3a45a 28 6e 44 61 74 61 3e 3d 31 29 3b 0a 20 20 61 73  (nData>=1);.  as
3a45b 73 65 72 74 28 68 61 73 68 49 73 45 6d 70 74 79  sert(hashIsEmpty
3a45c 28 70 52 74 72 65 65 29 29 3b 0a 0a 20 20 2f 2a  (pRtree));..  /*
3a45d 20 49 66 20 61 7a 44 61 74 61 5b 30 5d 20 69 73   If azData[0] is
3a45e 20 6e 6f 74 20 61 6e 20 53 51 4c 20 4e 55 4c 4c   not an SQL NULL
3a45f 20 76 61 6c 75 65 2c 20 69 74 20 69 73 20 74 68   value, it is th
3a460 65 20 72 6f 77 69 64 20 6f 66 20 61 0a 20 20 2a  e rowid of a.  *
3a461 2a 20 72 65 63 6f 72 64 20 74 6f 20 64 65 6c 65  * record to dele
3a462 74 65 20 66 72 6f 6d 20 74 68 65 20 72 2d 74 72  te from the r-tr
3a463 65 65 20 74 61 62 6c 65 2e 20 54 68 65 20 66 6f  ee table. The fo
3a464 6c 6c 6f 77 69 6e 67 20 62 6c 6f 63 6b 20 64 6f  llowing block do
3a465 65 73 0a 20 20 2a 2a 20 6a 75 73 74 20 74 68 61  es.  ** just tha
3a466 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  t..  */.  if( sq
3a467 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
3a468 28 61 7a 44 61 74 61 5b 30 5d 29 21 3d 53 51 4c  (azData[0])!=SQL
3a469 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  ITE_NULL ){.    
3a46a 69 36 34 20 69 44 65 6c 65 74 65 3b 20 20 20 20  i64 iDelete;    
3a46b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
3a46c 68 65 20 72 6f 77 69 64 20 74 6f 20 64 65 6c 65  he rowid to dele
3a46d 74 65 20 2a 2f 0a 20 20 20 20 52 74 72 65 65 4e  te */.    RtreeN
3a46e 6f 64 65 20 2a 70 4c 65 61 66 3b 20 20 20 20 20  ode *pLeaf;     
3a46f 20 20 20 20 20 20 2f 2a 20 4c 65 61 66 20 6e 6f        /* Leaf no
3a470 64 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 72 65  de containing re
3a471 63 6f 72 64 20 69 44 65 6c 65 74 65 20 2a 2f 0a  cord iDelete */.
3a472 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 3b 20 20      int iCell;  
3a473 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a474 2f 2a 20 49 6e 64 65 78 20 6f 66 20 69 44 65 6c  /* Index of iDel
3a475 65 74 65 20 63 65 6c 6c 20 69 6e 20 70 4c 65 61  ete cell in pLea
3a476 66 20 2a 2f 0a 20 20 20 20 52 74 72 65 65 4e 6f  f */.    RtreeNo
3a477 64 65 20 2a 70 52 6f 6f 74 3b 0a 0a 20 20 20 20  de *pRoot;..    
3a478 2f 2a 20 4f 62 74 61 69 6e 20 61 20 72 65 66 65  /* Obtain a refe
3a479 72 65 6e 63 65 20 74 6f 20 74 68 65 20 72 6f 6f  rence to the roo
3a47a 74 20 6e 6f 64 65 20 74 6f 20 69 6e 69 74 69 61  t node to initia
3a47b 6c 69 73 65 20 52 74 72 65 65 2e 69 44 65 70 74  lise Rtree.iDept
3a47c 68 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 6e 6f  h */.    rc = no
3a47d 64 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65  deAcquire(pRtree
3a47e 2c 20 31 2c 20 30 2c 20 26 70 52 6f 6f 74 29 3b  , 1, 0, &pRoot);
3a47f 0a 0a 20 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20  ..    /* Obtain 
3a480 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 74  a reference to t
3a481 68 65 20 6c 65 61 66 20 6e 6f 64 65 20 74 68 61  he leaf node tha
3a482 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 65  t contains the e
3a483 6e 74 72 79 20 0a 20 20 20 20 2a 2a 20 61 62 6f  ntry .    ** abo
3a484 75 74 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64  ut to be deleted
3a485 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  . .    */.    if
3a486 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
3a487 29 7b 0a 20 20 20 20 20 20 69 44 65 6c 65 74 65  ){.      iDelete
3a488 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
3a489 5f 69 6e 74 36 34 28 61 7a 44 61 74 61 5b 30 5d  _int64(azData[0]
3a48a 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 69  );.      rc = fi
3a48b 6e 64 4c 65 61 66 4e 6f 64 65 28 70 52 74 72 65  ndLeafNode(pRtre
3a48c 65 2c 20 69 44 65 6c 65 74 65 2c 20 26 70 4c 65  e, iDelete, &pLe
3a48d 61 66 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  af);.    }..    
3a48e 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 63 65  /* Delete the ce
3a48f 6c 6c 20 69 6e 20 71 75 65 73 74 69 6f 6e 20 66  ll in question f
3a490 72 6f 6d 20 74 68 65 20 6c 65 61 66 20 6e 6f 64  rom the leaf nod
3a491 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63  e. */.    if( rc
3a492 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
3a493 20 20 20 20 20 69 6e 74 20 72 63 32 3b 0a 20 20       int rc2;.  
3a494 20 20 20 20 69 43 65 6c 6c 20 3d 20 6e 6f 64 65      iCell = node
3a495 52 6f 77 69 64 49 6e 64 65 78 28 70 52 74 72 65  RowidIndex(pRtre
3a496 65 2c 20 70 4c 65 61 66 2c 20 69 44 65 6c 65 74  e, pLeaf, iDelet
3a497 65 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 64  e);.      rc = d
3a498 65 6c 65 74 65 43 65 6c 6c 28 70 52 74 72 65 65  eleteCell(pRtree
3a499 2c 20 70 4c 65 61 66 2c 20 69 43 65 6c 6c 2c 20  , pLeaf, iCell, 
3a49a 30 29 3b 0a 20 20 20 20 20 20 72 63 32 20 3d 20  0);.      rc2 = 
3a49b 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72  nodeRelease(pRtr
3a49c 65 65 2c 20 70 4c 65 61 66 29 3b 0a 20 20 20 20  ee, pLeaf);.    
3a49d 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
3a49e 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
3a49f 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 7d  c = rc2;.      }
3a4a0 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44  .    }..    /* D
3a4a1 65 6c 65 74 65 20 74 68 65 20 63 6f 72 72 65 73  elete the corres
3a4a2 70 6f 6e 64 69 6e 67 20 65 6e 74 72 79 20 69 6e  ponding entry in
3a4a3 20 74 68 65 20 3c 72 74 72 65 65 3e 5f 72 6f 77   the <rtree>_row
3a4a4 69 64 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20  id table. */.   
3a4a5 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
3a4a6 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  OK ){.      sqli
3a4a7 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
3a4a8 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 52 6f  Rtree->pDeleteRo
3a4a9 77 69 64 2c 20 31 2c 20 69 44 65 6c 65 74 65 29  wid, 1, iDelete)
3a4aa 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
3a4ab 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70 44 65  step(pRtree->pDe
3a4ac 6c 65 74 65 52 6f 77 69 64 29 3b 0a 20 20 20 20  leteRowid);.    
3a4ad 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72    rc = sqlite3_r
3a4ae 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70 44 65  eset(pRtree->pDe
3a4af 6c 65 74 65 52 6f 77 69 64 29 3b 0a 20 20 20 20  leteRowid);.    
3a4b0 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20  }..    /* Check 
3a4b1 69 66 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65  if the root node
3a4b2 20 6e 6f 77 20 68 61 73 20 65 78 61 63 74 6c 79   now has exactly
3a4b3 20 6f 6e 65 20 63 68 69 6c 64 2e 20 49 66 20 73   one child. If s
3a4b4 6f 2c 20 72 65 6d 6f 76 65 0a 20 20 20 20 2a 2a  o, remove.    **
3a4b5 20 69 74 2c 20 73 63 68 65 64 75 6c 65 20 74 68   it, schedule th
3a4b6 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  e contents of th
3a4b7 65 20 63 68 69 6c 64 20 66 6f 72 20 72 65 69 6e  e child for rein
3a4b8 73 65 72 74 69 6f 6e 20 61 6e 64 20 0a 20 20 20  sertion and .   
3a4b9 20 2a 2a 20 72 65 64 75 63 65 20 74 68 65 20 74   ** reduce the t
3a4ba 72 65 65 20 68 65 69 67 68 74 20 62 79 20 6f 6e  ree height by on
3a4bb 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  e..    **.    **
3a4bc 20 54 68 69 73 20 69 73 20 65 71 75 69 76 61 6c   This is equival
3a4bd 65 6e 74 20 74 6f 20 63 6f 70 79 69 6e 67 20 74  ent to copying t
3a4be 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
3a4bf 68 65 20 63 68 69 6c 64 20 69 6e 74 6f 0a 20 20  he child into.  
3a4c0 20 20 2a 2a 20 74 68 65 20 72 6f 6f 74 20 6e 6f    ** the root no
3a4c1 64 65 20 28 74 68 65 20 6f 70 65 72 61 74 69 6f  de (the operatio
3a4c2 6e 20 74 68 61 74 20 47 75 74 6d 61 6e 27 73 20  n that Gutman's 
3a4c3 70 61 70 65 72 20 73 61 79 73 20 74 6f 20 70 65  paper says to pe
3a4c4 72 66 6f 72 6d 20 0a 20 20 20 20 2a 2a 20 69 6e  rform .    ** in
3a4c5 20 74 68 69 73 20 73 63 65 6e 61 72 69 6f 29 2e   this scenario).
3a4c6 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
3a4c7 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
3a4c8 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 3e   pRtree->iDepth>
3a4c9 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72  0 ){.      if( r
3a4ca 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
3a4cb 4e 43 45 4c 4c 28 70 52 6f 6f 74 29 3d 3d 31 20  NCELL(pRoot)==1 
3a4cc 29 7b 0a 20 20 20 20 20 20 20 20 52 74 72 65 65  ){.        Rtree
3a4cd 4e 6f 64 65 20 2a 70 43 68 69 6c 64 3b 0a 20 20  Node *pChild;.  
3a4ce 20 20 20 20 20 20 69 36 34 20 69 43 68 69 6c 64        i64 iChild
3a4cf 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28   = nodeGetRowid(
3a4d0 70 52 74 72 65 65 2c 20 70 52 6f 6f 74 2c 20 30  pRtree, pRoot, 0
3a4d1 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
3a4d2 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52 74 72  nodeAcquire(pRtr
3a4d3 65 65 2c 20 69 43 68 69 6c 64 2c 20 70 52 6f 6f  ee, iChild, pRoo
3a4d4 74 2c 20 26 70 43 68 69 6c 64 29 3b 0a 20 20 20  t, &pChild);.   
3a4d5 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
3a4d6 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
3a4d7 20 20 20 20 72 63 20 3d 20 72 65 6d 6f 76 65 4e      rc = removeN
3a4d8 6f 64 65 28 70 52 74 72 65 65 2c 20 70 43 68 69  ode(pRtree, pChi
3a4d9 6c 64 2c 20 70 52 74 72 65 65 2d 3e 69 44 65 70  ld, pRtree->iDep
3a4da 74 68 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 7d  th-1);.        }
3a4db 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
3a4dc 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
3a4dd 20 20 20 20 20 20 20 20 70 52 74 72 65 65 2d 3e          pRtree->
3a4de 69 44 65 70 74 68 2d 2d 3b 0a 20 20 20 20 20 20  iDepth--;.      
3a4df 20 20 20 20 77 72 69 74 65 49 6e 74 31 36 28 70      writeInt16(p
3a4e0 52 6f 6f 74 2d 3e 7a 44 61 74 61 2c 20 70 52 74  Root->zData, pRt
3a4e1 72 65 65 2d 3e 69 44 65 70 74 68 29 3b 0a 20 20  ree->iDepth);.  
3a4e2 20 20 20 20 20 20 20 20 70 52 6f 6f 74 2d 3e 69          pRoot->i
3a4e3 73 44 69 72 74 79 20 3d 20 31 3b 0a 20 20 20 20  sDirty = 1;.    
3a4e4 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
3a4e5 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 2d 69    }..    /* Re-i
3a4e6 6e 73 65 72 74 20 74 68 65 20 63 6f 6e 74 65 6e  nsert the conten
3a4e7 74 73 20 6f 66 20 61 6e 79 20 75 6e 64 65 72 66  ts of any underf
3a4e8 75 6c 6c 20 6e 6f 64 65 73 20 72 65 6d 6f 76 65  ull nodes remove
3a4e9 64 20 66 72 6f 6d 20 74 68 65 20 74 72 65 65 2e  d from the tree.
3a4ea 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70 4c 65 61   */.    for(pLea
3a4eb 66 3d 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74  f=pRtree->pDelet
3a4ec 65 64 3b 20 70 4c 65 61 66 3b 20 70 4c 65 61 66  ed; pLeaf; pLeaf
3a4ed 3d 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65  =pRtree->pDelete
3a4ee 64 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63  d){.      if( rc
3a4ef 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
3a4f0 20 20 20 20 20 20 20 72 63 20 3d 20 72 65 69 6e         rc = rein
3a4f1 73 65 72 74 4e 6f 64 65 43 6f 6e 74 65 6e 74 28  sertNodeContent(
3a4f2 70 52 74 72 65 65 2c 20 70 4c 65 61 66 29 3b 0a  pRtree, pLeaf);.
3a4f3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 52        }.      pR
3a4f4 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 64 20 3d  tree->pDeleted =
3a4f5 20 70 4c 65 61 66 2d 3e 70 4e 65 78 74 3b 0a 20   pLeaf->pNext;. 
3a4f6 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
3a4f7 65 28 70 4c 65 61 66 29 3b 0a 20 20 20 20 7d 0a  e(pLeaf);.    }.
3a4f8 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65 20  .    /* Release 
3a4f9 74 68 65 20 72 65 66 65 72 65 6e 63 65 20 74 6f  the reference to
3a4fa 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 2e 20   the root node. 
3a4fb 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  */.    if( rc==S
3a4fc 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
3a4fd 20 20 72 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61    rc = nodeRelea
3a4fe 73 65 28 70 52 74 72 65 65 2c 20 70 52 6f 6f 74  se(pRtree, pRoot
3a4ff 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
3a500 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28      nodeRelease(
3a501 70 52 74 72 65 65 2c 20 70 52 6f 6f 74 29 3b 0a  pRtree, pRoot);.
3a502 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
3a503 49 66 20 74 68 65 20 61 7a 44 61 74 61 5b 5d 20  If the azData[] 
3a504 61 72 72 61 79 20 63 6f 6e 74 61 69 6e 73 20 6d  array contains m
3a505 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 65 6c 65  ore than one ele
3a506 6d 65 6e 74 2c 20 65 6c 65 6d 65 6e 74 73 0a 20  ment, elements. 
3a507 20 2a 2a 20 28 61 7a 44 61 74 61 5b 32 5d 2e 2e   ** (azData[2]..
3a508 61 7a 44 61 74 61 5b 61 72 67 63 2d 31 5d 29 20  azData[argc-1]) 
3a509 63 6f 6e 74 61 69 6e 20 61 20 6e 65 77 20 72 65  contain a new re
3a50a 63 6f 72 64 20 74 6f 20 69 6e 73 65 72 74 20 69  cord to insert i
3a50b 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20 72 2d 74  nto.  ** the r-t
3a50c 72 65 65 20 73 74 72 75 63 74 75 72 65 2e 0a 20  ree structure.. 
3a50d 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
3a50e 4c 49 54 45 5f 4f 4b 20 26 26 20 6e 44 61 74 61  LITE_OK && nData
3a50f 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 73  >1 ){.    /* Ins
3a510 65 72 74 20 61 20 6e 65 77 20 72 65 63 6f 72 64  ert a new record
3a511 20 69 6e 74 6f 20 74 68 65 20 72 2d 74 72 65 65   into the r-tree
3a512 20 2a 2f 0a 20 20 20 20 52 74 72 65 65 43 65 6c   */.    RtreeCel
3a513 6c 20 63 65 6c 6c 3b 0a 20 20 20 20 69 6e 74 20  l cell;.    int 
3a514 69 69 3b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64  ii;.    RtreeNod
3a515 65 20 2a 70 4c 65 61 66 3b 0a 0a 20 20 20 20 2f  e *pLeaf;..    /
3a516 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 63  * Populate the c
3a517 65 6c 6c 2e 61 43 6f 6f 72 64 5b 5d 20 61 72 72  ell.aCoord[] arr
3a518 61 79 2e 20 54 68 65 20 66 69 72 73 74 20 63 6f  ay. The first co
3a519 6f 72 64 69 6e 61 74 65 20 69 73 20 61 7a 44 61  ordinate is azDa
3a51a 74 61 5b 33 5d 2e 20 2a 2f 0a 20 20 20 20 61 73  ta[3]. */.    as
3a51b 73 65 72 74 28 20 6e 44 61 74 61 3d 3d 28 70 52  sert( nData==(pR
3a51c 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 20 2b 20 33  tree->nDim*2 + 3
3a51d 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 52 74  ) );.    if( pRt
3a51e 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d  ree->eCoordType=
3a51f 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41  =RTREE_COORD_REA
3a520 4c 33 32 20 29 7b 0a 20 20 20 20 20 20 66 6f 72  L32 ){.      for
3a521 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65  (ii=0; ii<(pRtre
3a522 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d  e->nDim*2); ii+=
3a523 32 29 7b 0a 20 20 20 20 20 20 20 20 63 65 6c 6c  2){.        cell
3a524 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 20 3d 20  .aCoord[ii].f = 
3a525 28 66 6c 6f 61 74 29 73 71 6c 69 74 65 33 5f 76  (float)sqlite3_v
3a526 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61 7a 44 61  alue_double(azDa
3a527 74 61 5b 69 69 2b 33 5d 29 3b 0a 20 20 20 20 20  ta[ii+3]);.     
3a528 20 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69     cell.aCoord[i
3a529 69 2b 31 5d 2e 66 20 3d 20 28 66 6c 6f 61 74 29  i+1].f = (float)
3a52a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f  sqlite3_value_do
3a52b 75 62 6c 65 28 61 7a 44 61 74 61 5b 69 69 2b 34  uble(azData[ii+4
3a52c 5d 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ]);.        if( 
3a52d 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 5d 2e  cell.aCoord[ii].
3a52e 66 3e 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69  f>cell.aCoord[ii
3a52f 2b 31 5d 2e 66 20 29 7b 0a 20 20 20 20 20 20 20  +1].f ){.       
3a530 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43     rc = SQLITE_C
3a531 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 20  ONSTRAINT;.     
3a532 20 20 20 20 20 67 6f 74 6f 20 63 6f 6e 73 74 72       goto constr
3a533 61 69 6e 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  aint;.        }.
3a534 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
3a535 65 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 69 3d  e{.      for(ii=
3a536 30 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e  0; ii<(pRtree->n
3a537 44 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a  Dim*2); ii+=2){.
3a538 20 20 20 20 20 20 20 20 63 65 6c 6c 2e 61 43 6f          cell.aCo
3a539 6f 72 64 5b 69 69 5d 2e 69 20 3d 20 73 71 6c 69  ord[ii].i = sqli
3a53a 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 7a  te3_value_int(az
3a53b 44 61 74 61 5b 69 69 2b 33 5d 29 3b 0a 20 20 20  Data[ii+3]);.   
3a53c 20 20 20 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64       cell.aCoord
3a53d 5b 69 69 2b 31 5d 2e 69 20 3d 20 73 71 6c 69 74  [ii+1].i = sqlit
3a53e 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61 7a 44  e3_value_int(azD
3a53f 61 74 61 5b 69 69 2b 34 5d 29 3b 0a 20 20 20 20  ata[ii+4]);.    
3a540 20 20 20 20 69 66 28 20 63 65 6c 6c 2e 61 43 6f      if( cell.aCo
3a541 6f 72 64 5b 69 69 5d 2e 69 3e 63 65 6c 6c 2e 61  ord[ii].i>cell.a
3a542 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69 20 29 7b  Coord[ii+1].i ){
3a543 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
3a544 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
3a545 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  T;.          got
3a546 6f 20 63 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20  o constraint;.  
3a547 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
3a548 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 46 69      }..    /* Fi
3a549 67 75 72 65 20 6f 75 74 20 74 68 65 20 72 6f 77  gure out the row
3a54a 69 64 20 6f 66 20 74 68 65 20 6e 65 77 20 72 6f  id of the new ro
3a54b 77 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 73 71  w. */.    if( sq
3a54c 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
3a54d 28 61 7a 44 61 74 61 5b 32 5d 29 3d 3d 53 51 4c  (azData[2])==SQL
3a54e 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  ITE_NULL ){.    
3a54f 20 20 72 63 20 3d 20 6e 65 77 52 6f 77 69 64 28    rc = newRowid(
3a550 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2e 69 52  pRtree, &cell.iR
3a551 6f 77 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65  owid);.    }else
3a552 7b 0a 20 20 20 20 20 20 63 65 6c 6c 2e 69 52 6f  {.      cell.iRo
3a553 77 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  wid = sqlite3_va
3a554 6c 75 65 5f 69 6e 74 36 34 28 61 7a 44 61 74 61  lue_int64(azData
3a555 5b 32 5d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  [2]);.      sqli
3a556 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70  te3_bind_int64(p
3a557 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69  Rtree->pReadRowi
3a558 64 2c 20 31 2c 20 63 65 6c 6c 2e 69 52 6f 77 69  d, 1, cell.iRowi
3a559 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 53 51  d);.      if( SQ
3a55a 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65  LITE_ROW==sqlite
3a55b 33 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70  3_step(pRtree->p
3a55c 52 65 61 64 52 6f 77 69 64 29 20 29 7b 0a 20 20  ReadRowid) ){.  
3a55d 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
3a55e 73 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65 61  set(pRtree->pRea
3a55f 64 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 20  dRowid);.       
3a560 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e   rc = SQLITE_CON
3a561 53 54 52 41 49 4e 54 3b 0a 20 20 20 20 20 20 20  STRAINT;.       
3a562 20 67 6f 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74   goto constraint
3a563 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
3a564 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
3a565 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64  et(pRtree->pRead
3a566 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 0a 20  Rowid);.    }.. 
3a567 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
3a568 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63  E_OK ){.      rc
3a569 20 3d 20 43 68 6f 6f 73 65 4c 65 61 66 28 70 52   = ChooseLeaf(pR
3a56a 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20 30 2c 20  tree, &cell, 0, 
3a56b 26 70 4c 65 61 66 29 3b 0a 20 20 20 20 7d 0a 20  &pLeaf);.    }. 
3a56c 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
3a56d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e  E_OK ){.      in
3a56e 74 20 72 63 32 3b 0a 20 20 20 20 20 20 70 52 74  t rc2;.      pRt
3a56f 72 65 65 2d 3e 69 52 65 69 6e 73 65 72 74 48 65  ree->iReinsertHe
3a570 69 67 68 74 20 3d 20 2d 31 3b 0a 20 20 20 20 20  ight = -1;.     
3a571 20 72 63 20 3d 20 72 74 72 65 65 49 6e 73 65 72   rc = rtreeInser
3a572 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4c  tCell(pRtree, pL
3a573 65 61 66 2c 20 26 63 65 6c 6c 2c 20 30 29 3b 0a  eaf, &cell, 0);.
3a574 20 20 20 20 20 20 72 63 32 20 3d 20 6e 6f 64 65        rc2 = node
3a575 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20  Release(pRtree, 
3a576 70 4c 65 61 66 29 3b 0a 20 20 20 20 20 20 69 66  pLeaf);.      if
3a577 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
3a578 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
3a579 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rc2;.      }.   
3a57a 20 7d 0a 20 20 7d 0a 0a 63 6f 6e 73 74 72 61 69   }.  }..constrai
3a57b 6e 74 3a 0a 20 20 72 74 72 65 65 52 65 6c 65 61  nt:.  rtreeRelea
3a57c 73 65 28 70 52 74 72 65 65 29 3b 0a 20 20 72 65  se(pRtree);.  re
3a57d 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
3a57e 2a 20 54 68 65 20 78 52 65 6e 61 6d 65 20 6d 65  * The xRename me
3a57f 74 68 6f 64 20 66 6f 72 20 72 74 72 65 65 20 6d  thod for rtree m
3a580 6f 64 75 6c 65 20 76 69 72 74 75 61 6c 20 74 61  odule virtual ta
3a581 62 6c 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  bles..*/.static 
3a582 69 6e 74 20 72 74 72 65 65 52 65 6e 61 6d 65 28  int rtreeRename(
3a583 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
3a584 74 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  tab, const char 
3a585 2a 7a 4e 65 77 4e 61 6d 65 29 7b 0a 20 20 52 74  *zNewName){.  Rt
3a586 72 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52  ree *pRtree = (R
3a587 74 72 65 65 20 2a 29 70 56 74 61 62 3b 0a 20 20  tree *)pVtab;.  
3a588 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
3a589 4e 4f 4d 45 4d 3b 0a 20 20 63 68 61 72 20 2a 7a  NOMEM;.  char *z
3a58a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70  Sql = sqlite3_mp
3a58b 72 69 6e 74 66 28 0a 20 20 20 20 22 41 4c 54 45  rintf(.    "ALTE
3a58c 52 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 6e  R TABLE %Q.'%q_n
3a58d 6f 64 65 27 20 20 20 52 45 4e 41 4d 45 20 54 4f  ode'   RENAME TO
3a58e 20 5c 22 25 77 5f 6e 6f 64 65 5c 22 3b 22 0a 20   \"%w_node\";". 
3a58f 20 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45 20     "ALTER TABLE 
3a590 25 51 2e 27 25 71 5f 70 61 72 65 6e 74 27 20 52  %Q.'%q_parent' R
3a591 45 4e 41 4d 45 20 54 4f 20 5c 22 25 77 5f 70 61  ENAME TO \"%w_pa
3a592 72 65 6e 74 5c 22 3b 22 0a 20 20 20 20 22 41 4c  rent\";".    "AL
3a593 54 45 52 20 54 41 42 4c 45 20 25 51 2e 27 25 71  TER TABLE %Q.'%q
3a594 5f 72 6f 77 69 64 27 20 20 52 45 4e 41 4d 45 20  _rowid'  RENAME 
3a595 54 4f 20 5c 22 25 77 5f 72 6f 77 69 64 5c 22 3b  TO \"%w_rowid\";
3a596 22 0a 20 20 20 20 2c 20 70 52 74 72 65 65 2d 3e  ".    , pRtree->
3a597 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e 7a 4e 61  zDb, pRtree->zNa
3a598 6d 65 2c 20 7a 4e 65 77 4e 61 6d 65 20 0a 20 20  me, zNewName .  
3a599 20 20 2c 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c    , pRtree->zDb,
3a59a 20 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20   pRtree->zName, 
3a59b 7a 4e 65 77 4e 61 6d 65 20 0a 20 20 20 20 2c 20  zNewName .    , 
3a59c 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74  pRtree->zDb, pRt
3a59d 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 65 77  ree->zName, zNew
3a59e 4e 61 6d 65 0a 20 20 29 3b 0a 20 20 69 66 28 20  Name.  );.  if( 
3a59f 7a 53 71 6c 20 29 7b 0a 20 20 20 20 72 63 20 3d  zSql ){.    rc =
3a5a0 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 70 52   sqlite3_exec(pR
3a5a1 74 72 65 65 2d 3e 64 62 2c 20 7a 53 71 6c 2c 20  tree->db, zSql, 
3a5a2 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71  0, 0, 0);.    sq
3a5a3 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71 6c 29  lite3_free(zSql)
3a5a4 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
3a5a5 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 73 71 6c  c;.}..static sql
3a5a6 69 74 65 33 5f 6d 6f 64 75 6c 65 20 72 74 72 65  ite3_module rtre
3a5a7 65 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20 30 2c  eModule = {.  0,
3a5a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5a9 20 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72           /* iVer
3a5aa 73 69 6f 6e 20 2a 2f 0a 20 20 72 74 72 65 65 43  sion */.  rtreeC
3a5ab 72 65 61 74 65 2c 20 20 20 20 20 20 20 20 20 20  reate,          
3a5ac 20 20 20 20 20 20 2f 2a 20 78 43 72 65 61 74 65        /* xCreate
3a5ad 20 2d 20 63 72 65 61 74 65 20 61 20 74 61 62 6c   - create a tabl
3a5ae 65 20 2a 2f 0a 20 20 72 74 72 65 65 43 6f 6e 6e  e */.  rtreeConn
3a5af 65 63 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  ect,            
3a5b0 20 20 20 2f 2a 20 78 43 6f 6e 6e 65 63 74 20 2d     /* xConnect -
3a5b1 20 63 6f 6e 6e 65 63 74 20 74 6f 20 61 6e 20 65   connect to an e
3a5b2 78 69 73 74 69 6e 67 20 74 61 62 6c 65 20 2a 2f  xisting table */
3a5b3 0a 20 20 72 74 72 65 65 42 65 73 74 49 6e 64 65  .  rtreeBestInde
3a5b4 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  x,             /
3a5b5 2a 20 78 42 65 73 74 49 6e 64 65 78 20 2d 20 44  * xBestIndex - D
3a5b6 65 74 65 72 6d 69 6e 65 20 73 65 61 72 63 68 20  etermine search 
3a5b7 73 74 72 61 74 65 67 79 20 2a 2f 0a 20 20 72 74  strategy */.  rt
3a5b8 72 65 65 44 69 73 63 6f 6e 6e 65 63 74 2c 20 20  reeDisconnect,  
3a5b9 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 69            /* xDi
3a5ba 73 63 6f 6e 6e 65 63 74 20 2d 20 44 69 73 63 6f  sconnect - Disco
3a5bb 6e 6e 65 63 74 20 66 72 6f 6d 20 61 20 74 61 62  nnect from a tab
3a5bc 6c 65 20 2a 2f 0a 20 20 72 74 72 65 65 44 65 73  le */.  rtreeDes
3a5bd 74 72 6f 79 2c 20 20 20 20 20 20 20 20 20 20 20  troy,           
3a5be 20 20 20 20 2f 2a 20 78 44 65 73 74 72 6f 79 20      /* xDestroy 
3a5bf 2d 20 44 72 6f 70 20 61 20 74 61 62 6c 65 20 2a  - Drop a table *
3a5c0 2f 0a 20 20 72 74 72 65 65 4f 70 65 6e 2c 20 20  /.  rtreeOpen,  
3a5c1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5c2 2f 2a 20 78 4f 70 65 6e 20 2d 20 6f 70 65 6e 20  /* xOpen - open 
3a5c3 61 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20 72 74  a cursor */.  rt
3a5c4 72 65 65 43 6c 6f 73 65 2c 20 20 20 20 20 20 20  reeClose,       
3a5c5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6c            /* xCl
3a5c6 6f 73 65 20 2d 20 63 6c 6f 73 65 20 61 20 63 75  ose - close a cu
3a5c7 72 73 6f 72 20 2a 2f 0a 20 20 72 74 72 65 65 46  rsor */.  rtreeF
3a5c8 69 6c 74 65 72 2c 20 20 20 20 20 20 20 20 20 20  ilter,          
3a5c9 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 74 65 72        /* xFilter
3a5ca 20 2d 20 63 6f 6e 66 69 67 75 72 65 20 73 63 61   - configure sca
3a5cb 6e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a 2f  n constraints */
3a5cc 0a 20 20 72 74 72 65 65 4e 65 78 74 2c 20 20 20  .  rtreeNext,   
3a5cd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3a5ce 2a 20 78 4e 65 78 74 20 2d 20 61 64 76 61 6e 63  * xNext - advanc
3a5cf 65 20 61 20 63 75 72 73 6f 72 20 2a 2f 0a 20 20  e a cursor */.  
3a5d0 72 74 72 65 65 45 6f 66 2c 20 20 20 20 20 20 20  rtreeEof,       
3a5d1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
3a5d2 45 6f 66 20 2a 2f 0a 20 20 72 74 72 65 65 43 6f  Eof */.  rtreeCo
3a5d3 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20 20  lumn,           
3a5d4 20 20 20 20 20 2f 2a 20 78 43 6f 6c 75 6d 6e 20       /* xColumn 
3a5d5 2d 20 72 65 61 64 20 64 61 74 61 20 2a 2f 0a 20  - read data */. 
3a5d6 20 72 74 72 65 65 52 6f 77 69 64 2c 20 20 20 20   rtreeRowid,    
3a5d7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a5d8 78 52 6f 77 69 64 20 2d 20 72 65 61 64 20 64 61  xRowid - read da
3a5d9 74 61 20 2a 2f 0a 20 20 72 74 72 65 65 55 70 64  ta */.  rtreeUpd
3a5da 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ate,            
3a5db 20 20 20 20 2f 2a 20 78 55 70 64 61 74 65 20 2d      /* xUpdate -
3a5dc 20 77 72 69 74 65 20 64 61 74 61 20 2a 2f 0a 20   write data */. 
3a5dd 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
3a5de 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a5df 78 42 65 67 69 6e 20 2d 20 62 65 67 69 6e 20 74  xBegin - begin t
3a5e0 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20  ransaction */.  
3a5e1 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
3a5e2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
3a5e3 53 79 6e 63 20 2d 20 73 79 6e 63 20 74 72 61 6e  Sync - sync tran
3a5e4 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20  saction */.  0, 
3a5e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5e6 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 6f 6d           /* xCom
3a5e7 6d 69 74 20 2d 20 63 6f 6d 6d 69 74 20 74 72 61  mit - commit tra
3a5e8 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c  nsaction */.  0,
3a5e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a5ea 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 52 6f            /* xRo
3a5eb 6c 6c 62 61 63 6b 20 2d 20 72 6f 6c 6c 62 61 63  llback - rollbac
3a5ec 6b 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f  k transaction */
3a5ed 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20  .  0,           
3a5ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3a5ef 2a 20 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e 20  * xFindFunction 
3a5f0 2d 20 66 75 6e 63 74 69 6f 6e 20 6f 76 65 72 6c  - function overl
3a5f1 6f 61 64 69 6e 67 20 2a 2f 0a 20 20 72 74 72 65  oading */.  rtre
3a5f2 65 52 65 6e 61 6d 65 20 20 20 20 20 20 20 20 20  eRename         
3a5f3 20 20 20 20 20 20 20 20 2f 2a 20 78 52 65 6e 61          /* xRena
3a5f4 6d 65 20 2d 20 72 65 6e 61 6d 65 20 74 68 65 20  me - rename the 
3a5f5 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 73 74 61  table */.};..sta
3a5f6 74 69 63 20 69 6e 74 20 72 74 72 65 65 53 71 6c  tic int rtreeSql
3a5f7 49 6e 69 74 28 0a 20 20 52 74 72 65 65 20 2a 70  Init(.  Rtree *p
3a5f8 52 74 72 65 65 2c 20 0a 20 20 73 71 6c 69 74 65  Rtree, .  sqlite
3a5f9 33 20 2a 64 62 2c 20 0a 20 20 63 6f 6e 73 74 20  3 *db, .  const 
3a5fa 63 68 61 72 20 2a 7a 44 62 2c 20 0a 20 20 63 6f  char *zDb, .  co
3a5fb 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 65 66 69  nst char *zPrefi
3a5fc 78 2c 20 0a 20 20 69 6e 74 20 69 73 43 72 65 61  x, .  int isCrea
3a5fd 74 65 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  te.){.  int rc =
3a5fe 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 23   SQLITE_OK;..  #
3a5ff 64 65 66 69 6e 65 20 4e 5f 53 54 41 54 45 4d 45  define N_STATEME
3a600 4e 54 20 39 0a 20 20 73 74 61 74 69 63 20 63 6f  NT 9.  static co
3a601 6e 73 74 20 63 68 61 72 20 2a 61 7a 53 71 6c 5b  nst char *azSql[
3a602 4e 5f 53 54 41 54 45 4d 45 4e 54 5d 20 3d 20 7b  N_STATEMENT] = {
3a603 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 61 6e 64  .    /* Read and
3a604 20 77 72 69 74 65 20 74 68 65 20 78 78 78 5f 6e   write the xxx_n
3a605 6f 64 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  ode table */.   
3a606 20 22 53 45 4c 45 43 54 20 64 61 74 61 20 46 52   "SELECT data FR
3a607 4f 4d 20 27 25 71 27 2e 27 25 71 5f 6e 6f 64 65  OM '%q'.'%q_node
3a608 27 20 57 48 45 52 45 20 6e 6f 64 65 6e 6f 20 3d  ' WHERE nodeno =
3a609 20 3a 31 22 2c 0a 20 20 20 20 22 49 4e 53 45 52   :1",.    "INSER
3a60a 54 20 4f 52 20 52 45 50 4c 41 43 45 20 49 4e 54  T OR REPLACE INT
3a60b 4f 20 27 25 71 27 2e 27 25 71 5f 6e 6f 64 65 27  O '%q'.'%q_node'
3a60c 20 56 41 4c 55 45 53 28 3a 31 2c 20 3a 32 29 22   VALUES(:1, :2)"
3a60d 2c 0a 20 20 20 20 22 44 45 4c 45 54 45 20 46 52  ,.    "DELETE FR
3a60e 4f 4d 20 27 25 71 27 2e 27 25 71 5f 6e 6f 64 65  OM '%q'.'%q_node
3a60f 27 20 57 48 45 52 45 20 6e 6f 64 65 6e 6f 20 3d  ' WHERE nodeno =
3a610 20 3a 31 22 2c 0a 0a 20 20 20 20 2f 2a 20 52 65   :1",..    /* Re
3a611 61 64 20 61 6e 64 20 77 72 69 74 65 20 74 68 65  ad and write the
3a612 20 78 78 78 5f 72 6f 77 69 64 20 74 61 62 6c 65   xxx_rowid table
3a613 20 2a 2f 0a 20 20 20 20 22 53 45 4c 45 43 54 20   */.    "SELECT 
3a614 6e 6f 64 65 6e 6f 20 46 52 4f 4d 20 27 25 71 27  nodeno FROM '%q'
3a615 2e 27 25 71 5f 72 6f 77 69 64 27 20 57 48 45 52  .'%q_rowid' WHER
3a616 45 20 72 6f 77 69 64 20 3d 20 3a 31 22 2c 0a 20  E rowid = :1",. 
3a617 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20 52 45     "INSERT OR RE
3a618 50 4c 41 43 45 20 49 4e 54 4f 20 27 25 71 27 2e  PLACE INTO '%q'.
3a619 27 25 71 5f 72 6f 77 69 64 27 20 56 41 4c 55 45  '%q_rowid' VALUE
3a61a 53 28 3a 31 2c 20 3a 32 29 22 2c 0a 20 20 20 20  S(:1, :2)",.    
3a61b 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 27 25 71  "DELETE FROM '%q
3a61c 27 2e 27 25 71 5f 72 6f 77 69 64 27 20 57 48 45  '.'%q_rowid' WHE
3a61d 52 45 20 72 6f 77 69 64 20 3d 20 3a 31 22 2c 0a  RE rowid = :1",.
3a61e 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 61 6e 64  .    /* Read and
3a61f 20 77 72 69 74 65 20 74 68 65 20 78 78 78 5f 70   write the xxx_p
3a620 61 72 65 6e 74 20 74 61 62 6c 65 20 2a 2f 0a 20  arent table */. 
3a621 20 20 20 22 53 45 4c 45 43 54 20 70 61 72 65 6e     "SELECT paren
3a622 74 6e 6f 64 65 20 46 52 4f 4d 20 27 25 71 27 2e  tnode FROM '%q'.
3a623 27 25 71 5f 70 61 72 65 6e 74 27 20 57 48 45 52  '%q_parent' WHER
3a624 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c 0a  E nodeno = :1",.
3a625 20 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20 52      "INSERT OR R
3a626 45 50 4c 41 43 45 20 49 4e 54 4f 20 27 25 71 27  EPLACE INTO '%q'
3a627 2e 27 25 71 5f 70 61 72 65 6e 74 27 20 56 41 4c  .'%q_parent' VAL
3a628 55 45 53 28 3a 31 2c 20 3a 32 29 22 2c 0a 20 20  UES(:1, :2)",.  
3a629 20 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 27    "DELETE FROM '
3a62a 25 71 27 2e 27 25 71 5f 70 61 72 65 6e 74 27 20  %q'.'%q_parent' 
3a62b 57 48 45 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a  WHERE nodeno = :
3a62c 31 22 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65  1".  };.  sqlite
3a62d 33 5f 73 74 6d 74 20 2a 2a 61 70 70 53 74 6d 74  3_stmt **appStmt
3a62e 5b 4e 5f 53 54 41 54 45 4d 45 4e 54 5d 3b 0a 20  [N_STATEMENT];. 
3a62f 20 69 6e 74 20 69 3b 0a 0a 20 20 70 52 74 72 65   int i;..  pRtre
3a630 65 2d 3e 64 62 20 3d 20 64 62 3b 0a 0a 20 20 69  e->db = db;..  i
3a631 66 28 20 69 73 43 72 65 61 74 65 20 29 7b 0a 20  f( isCreate ){. 
3a632 20 20 20 63 68 61 72 20 2a 7a 43 72 65 61 74 65     char *zCreate
3a633 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
3a634 74 66 28 0a 22 43 52 45 41 54 45 20 54 41 42 4c  tf(."CREATE TABL
3a635 45 20 5c 22 25 77 5c 22 2e 5c 22 25 77 5f 6e 6f  E \"%w\".\"%w_no
3a636 64 65 5c 22 28 6e 6f 64 65 6e 6f 20 49 4e 54 45  de\"(nodeno INTE
3a637 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 2c  GER PRIMARY KEY,
3a638 20 64 61 74 61 20 42 4c 4f 42 29 3b 22 0a 22 43   data BLOB);"."C
3a639 52 45 41 54 45 20 54 41 42 4c 45 20 5c 22 25 77  REATE TABLE \"%w
3a63a 5c 22 2e 5c 22 25 77 5f 72 6f 77 69 64 5c 22 28  \".\"%w_rowid\"(
3a63b 72 6f 77 69 64 20 49 4e 54 45 47 45 52 20 50 52  rowid INTEGER PR
3a63c 49 4d 41 52 59 20 4b 45 59 2c 20 6e 6f 64 65 6e  IMARY KEY, noden
3a63d 6f 20 49 4e 54 45 47 45 52 29 3b 22 0a 22 43 52  o INTEGER);"."CR
3a63e 45 41 54 45 20 54 41 42 4c 45 20 5c 22 25 77 5c  EATE TABLE \"%w\
3a63f 22 2e 5c 22 25 77 5f 70 61 72 65 6e 74 5c 22 28  ".\"%w_parent\"(
3a640 6e 6f 64 65 6e 6f 20 49 4e 54 45 47 45 52 20 50  nodeno INTEGER P
3a641 52 49 4d 41 52 59 20 4b 45 59 2c 20 70 61 72 65  RIMARY KEY, pare
3a642 6e 74 6e 6f 64 65 20 49 4e 54 45 47 45 52 29 3b  ntnode INTEGER);
3a643 22 0a 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 27  "."INSERT INTO '
3a644 25 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20 56 41  %q'.'%q_node' VA
3a645 4c 55 45 53 28 31 2c 20 7a 65 72 6f 62 6c 6f 62  LUES(1, zeroblob
3a646 28 25 64 29 29 22 2c 0a 20 20 20 20 20 20 7a 44  (%d))",.      zD
3a647 62 2c 20 7a 50 72 65 66 69 78 2c 20 7a 44 62 2c  b, zPrefix, zDb,
3a648 20 7a 50 72 65 66 69 78 2c 20 7a 44 62 2c 20 7a   zPrefix, zDb, z
3a649 50 72 65 66 69 78 2c 20 7a 44 62 2c 20 7a 50 72  Prefix, zDb, zPr
3a64a 65 66 69 78 2c 20 70 52 74 72 65 65 2d 3e 69 4e  efix, pRtree->iN
3a64b 6f 64 65 53 69 7a 65 0a 20 20 20 20 29 3b 0a 20  odeSize.    );. 
3a64c 20 20 20 69 66 28 20 21 7a 43 72 65 61 74 65 20     if( !zCreate 
3a64d 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
3a64e 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
3a64f 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c    }.    rc = sql
3a650 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 43  ite3_exec(db, zC
3a651 72 65 61 74 65 2c 20 30 2c 20 30 2c 20 30 29 3b  reate, 0, 0, 0);
3a652 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
3a653 65 28 7a 43 72 65 61 74 65 29 3b 0a 20 20 20 20  e(zCreate);.    
3a654 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
3a655 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  K ){.      retur
3a656 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  n rc;.    }.  }.
3a657 0a 20 20 61 70 70 53 74 6d 74 5b 30 5d 20 3d 20  .  appStmt[0] = 
3a658 26 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f  &pRtree->pReadNo
3a659 64 65 3b 0a 20 20 61 70 70 53 74 6d 74 5b 31 5d  de;.  appStmt[1]
3a65a 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 57 72 69   = &pRtree->pWri
3a65b 74 65 4e 6f 64 65 3b 0a 20 20 61 70 70 53 74 6d  teNode;.  appStm
3a65c 74 5b 32 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e  t[2] = &pRtree->
3a65d 70 44 65 6c 65 74 65 4e 6f 64 65 3b 0a 20 20 61  pDeleteNode;.  a
3a65e 70 70 53 74 6d 74 5b 33 5d 20 3d 20 26 70 52 74  ppStmt[3] = &pRt
3a65f 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 3b  ree->pReadRowid;
3a660 0a 20 20 61 70 70 53 74 6d 74 5b 34 5d 20 3d 20  .  appStmt[4] = 
3a661 26 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52  &pRtree->pWriteR
3a662 6f 77 69 64 3b 0a 20 20 61 70 70 53 74 6d 74 5b  owid;.  appStmt[
3a663 35 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e 70 44  5] = &pRtree->pD
3a664 65 6c 65 74 65 52 6f 77 69 64 3b 0a 20 20 61 70  eleteRowid;.  ap
3a665 70 53 74 6d 74 5b 36 5d 20 3d 20 26 70 52 74 72  pStmt[6] = &pRtr
3a666 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e 74 3b  ee->pReadParent;
3a667 0a 20 20 61 70 70 53 74 6d 74 5b 37 5d 20 3d 20  .  appStmt[7] = 
3a668 26 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50  &pRtree->pWriteP
3a669 61 72 65 6e 74 3b 0a 20 20 61 70 70 53 74 6d 74  arent;.  appStmt
3a66a 5b 38 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e 70  [8] = &pRtree->p
3a66b 44 65 6c 65 74 65 50 61 72 65 6e 74 3b 0a 0a 20  DeleteParent;.. 
3a66c 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 53 54   for(i=0; i<N_ST
3a66d 41 54 45 4d 45 4e 54 20 26 26 20 72 63 3d 3d 53  ATEMENT && rc==S
3a66e 51 4c 49 54 45 5f 4f 4b 3b 20 69 2b 2b 29 7b 0a  QLITE_OK; i++){.
3a66f 20 20 20 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d      char *zSql =
3a670 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
3a671 28 61 7a 53 71 6c 5b 69 5d 2c 20 7a 44 62 2c 20  (azSql[i], zDb, 
3a672 7a 50 72 65 66 69 78 29 3b 0a 20 20 20 20 69 66  zPrefix);.    if
3a673 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20 20 20 20  ( zSql ){.      
3a674 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65  rc = sqlite3_pre
3a675 70 61 72 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c  pare_v2(db, zSql
3a676 2c 20 2d 31 2c 20 61 70 70 53 74 6d 74 5b 69 5d  , -1, appStmt[i]
3a677 2c 20 30 29 3b 20 0a 20 20 20 20 7d 65 6c 73 65  , 0); .    }else
3a678 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
3a679 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
3a67a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
3a67b 65 28 7a 53 71 6c 29 3b 0a 20 20 7d 0a 0a 20 20  e(zSql);.  }..  
3a67c 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
3a67d 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
3a67e 20 71 75 65 72 69 65 73 20 64 61 74 61 62 61 73   queries databas
3a67f 65 20 68 61 6e 64 6c 65 20 64 62 20 66 6f 72 20  e handle db for 
3a680 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 75 73  the page-size us
3a681 65 64 20 62 79 0a 2a 2a 20 64 61 74 61 62 61 73  ed by.** databas
3a682 65 20 7a 44 62 2e 20 49 66 20 73 75 63 63 65 73  e zDb. If succes
3a683 73 66 75 6c 2c 20 74 68 65 20 70 61 67 65 2d 73  sful, the page-s
3a684 69 7a 65 20 69 6e 20 62 79 74 65 73 20 69 73 20  ize in bytes is 
3a685 77 72 69 74 74 65 6e 20 74 6f 0a 2a 2a 20 2a 70  written to.** *p
3a686 69 50 61 67 65 53 69 7a 65 20 61 6e 64 20 53 51  iPageSize and SQ
3a687 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64  LITE_OK returned
3a688 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 61 6e 64  . Otherwise, and
3a689 20 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72   an SQLite error
3a68a 20 0a 2a 2a 20 63 6f 64 65 20 69 73 20 72 65 74   .** code is ret
3a68b 75 72 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  urned..*/.static
3a68c 20 69 6e 74 20 67 65 74 50 61 67 65 53 69 7a 65   int getPageSize
3a68d 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
3a68e 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 2c 20 69  nst char *zDb, i
3a68f 6e 74 20 2a 70 69 50 61 67 65 53 69 7a 65 29 7b  nt *piPageSize){
3a690 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
3a691 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 63 68 61 72  TE_NOMEM;.  char
3a692 20 2a 7a 53 71 6c 3b 0a 20 20 73 71 6c 69 74 65   *zSql;.  sqlite
3a693 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 20 3d 20  3_stmt *pStmt = 
3a694 30 3b 0a 0a 20 20 7a 53 71 6c 20 3d 20 73 71 6c  0;..  zSql = sql
3a695 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 50 52  ite3_mprintf("PR
3a696 41 47 4d 41 20 25 51 2e 70 61 67 65 5f 73 69 7a  AGMA %Q.page_siz
3a697 65 22 2c 20 7a 44 62 29 3b 0a 20 20 69 66 28 20  e", zDb);.  if( 
3a698 21 7a 53 71 6c 20 29 7b 0a 20 20 20 20 72 65 74  !zSql ){.    ret
3a699 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
3a69a 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71  ;.  }..  rc = sq
3a69b 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
3a69c 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26  (db, zSql, -1, &
3a69d 70 53 74 6d 74 2c 20 30 29 3b 0a 20 20 73 71 6c  pStmt, 0);.  sql
3a69e 69 74 65 33 5f 66 72 65 65 28 7a 53 71 6c 29 3b  ite3_free(zSql);
3a69f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
3a6a0 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
3a6a1 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 69 66  rn rc;.  }..  if
3a6a2 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71  ( SQLITE_ROW==sq
3a6a3 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74  lite3_step(pStmt
3a6a4 29 20 29 7b 0a 20 20 20 20 2a 70 69 50 61 67 65  ) ){.    *piPage
3a6a5 53 69 7a 65 20 3d 20 73 71 6c 69 74 65 33 5f 63  Size = sqlite3_c
3a6a6 6f 6c 75 6d 6e 5f 69 6e 74 28 70 53 74 6d 74 2c  olumn_int(pStmt,
3a6a7 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   0);.  }.  retur
3a6a8 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69  n sqlite3_finali
3a6a9 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a  ze(pStmt);.}../*
3a6aa 20 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69   .** This functi
3a6ab 6f 6e 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d  on is the implem
3a6ac 65 6e 74 61 74 69 6f 6e 20 6f 66 20 62 6f 74 68  entation of both
3a6ad 20 74 68 65 20 78 43 6f 6e 6e 65 63 74 20 61 6e   the xConnect an
3a6ae 64 20 78 43 72 65 61 74 65 0a 2a 2a 20 6d 65 74  d xCreate.** met
3a6af 68 6f 64 73 20 6f 66 20 74 68 65 20 72 2d 74 72  hods of the r-tr
3a6b0 65 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  ee virtual table
3a6b1 2e 0a 2a 2a 0a 2a 2a 20 20 20 61 72 67 76 5b 30  ..**.**   argv[0
3a6b2 5d 20 20 20 2d 3e 20 6d 6f 64 75 6c 65 20 6e 61  ]   -> module na
3a6b3 6d 65 0a 2a 2a 20 20 20 61 72 67 76 5b 31 5d 20  me.**   argv[1] 
3a6b4 20 20 2d 3e 20 64 61 74 61 62 61 73 65 20 6e 61    -> database na
3a6b5 6d 65 0a 2a 2a 20 20 20 61 72 67 76 5b 32 5d 20  me.**   argv[2] 
3a6b6 20 20 2d 3e 20 74 61 62 6c 65 20 6e 61 6d 65 0a    -> table name.
3a6b7 2a 2a 20 20 20 61 72 67 76 5b 2e 2e 2e 5d 20 2d  **   argv[...] -
3a6b8 3e 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 2e  > column names..
3a6b9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
3a6ba 72 74 72 65 65 49 6e 69 74 28 0a 20 20 73 71 6c  rtreeInit(.  sql
3a6bb 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
3a6bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a6bd 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
3a6be 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 76 6f 69  nection */.  voi
3a6bf 64 20 2a 70 41 75 78 2c 20 20 20 20 20 20 20 20  d *pAux,        
3a6c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a6c1 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 52   /* One of the R
3a6c2 54 52 45 45 5f 43 4f 4f 52 44 5f 2a 20 63 6f 6e  TREE_COORD_* con
3a6c3 73 74 61 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20  stants */.  int 
3a6c4 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72  argc, const char
3a6c5 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 20 20 20   *const*argv,   
3a6c6 2f 2a 20 50 61 72 61 6d 65 74 65 72 73 20 74 6f  /* Parameters to
3a6c7 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
3a6c8 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 73 71 6c  atement */.  sql
3a6c9 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 74  ite3_vtab **ppVt
3a6ca 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ab,             
3a6cb 20 2f 2a 20 4f 55 54 3a 20 4e 65 77 20 76 69 72   /* OUT: New vir
3a6cc 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
3a6cd 63 68 61 72 20 2a 2a 70 7a 45 72 72 2c 20 20 20  char **pzErr,   
3a6ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a6cf 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 72 72 6f      /* OUT: Erro
3a6d0 72 20 6d 65 73 73 61 67 65 2c 20 69 66 20 61 6e  r message, if an
3a6d1 79 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43 72 65  y */.  int isCre
3a6d2 61 74 65 20 20 20 20 20 20 20 20 20 20 20 20 20  ate             
3a6d3 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
3a6d4 75 65 20 66 6f 72 20 78 43 72 65 61 74 65 2c 20  ue for xCreate, 
3a6d5 66 61 6c 73 65 20 66 6f 72 20 78 43 6f 6e 6e 65  false for xConne
3a6d6 63 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72  ct */.){.  int r
3a6d7 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
3a6d8 20 69 6e 74 20 69 50 61 67 65 53 69 7a 65 20 3d   int iPageSize =
3a6d9 20 30 3b 0a 20 20 52 74 72 65 65 20 2a 70 52 74   0;.  Rtree *pRt
3a6da 72 65 65 3b 0a 20 20 69 6e 74 20 6e 44 62 3b 20  ree;.  int nDb; 
3a6db 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3a6dc 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 69 6e 67  Length of string
3a6dd 20 61 72 67 76 5b 31 5d 20 2a 2f 0a 20 20 69 6e   argv[1] */.  in
3a6de 74 20 6e 4e 61 6d 65 3b 20 20 20 20 20 20 20 20  t nName;        
3a6df 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
3a6e0 20 73 74 72 69 6e 67 20 61 72 67 76 5b 32 5d 20   string argv[2] 
3a6e1 2a 2f 0a 20 20 69 6e 74 20 65 43 6f 6f 72 64 54  */.  int eCoordT
3a6e2 79 70 65 20 3d 20 28 69 6e 74 29 70 41 75 78 3b  ype = (int)pAux;
3a6e3 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
3a6e4 61 45 72 72 4d 73 67 5b 5d 20 3d 20 7b 0a 20 20  aErrMsg[] = {.  
3a6e5 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
3a6e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a6e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a6e8 20 20 20 20 20 20 20 20 2f 2a 20 30 20 2a 2f 0a          /* 0 */.
3a6e9 20 20 20 20 22 57 72 6f 6e 67 20 6e 75 6d 62 65      "Wrong numbe
3a6ea 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66 6f 72  r of columns for
3a6eb 20 61 6e 20 72 74 72 65 65 20 74 61 62 6c 65 22   an rtree table"
3a6ec 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 2a  ,         /* 1 *
3a6ed 2f 0a 20 20 20 20 22 54 6f 6f 20 66 65 77 20 63  /.    "Too few c
3a6ee 6f 6c 75 6d 6e 73 20 66 6f 72 20 61 6e 20 72 74  olumns for an rt
3a6ef 72 65 65 20 74 61 62 6c 65 22 2c 20 20 20 20 20  ree table",     
3a6f0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32              /* 2
3a6f1 20 2a 2f 0a 20 20 20 20 22 54 6f 6f 20 6d 61 6e   */.    "Too man
3a6f2 79 20 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 61 6e  y columns for an
3a6f3 20 72 74 72 65 65 20 74 61 62 6c 65 22 20 20 20   rtree table"   
3a6f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3a6f5 20 33 20 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 69 6e   3 */.  };..  in
3a6f6 74 20 69 45 72 72 20 3d 20 28 61 72 67 63 3c 36  t iErr = (argc<6
3a6f7 29 20 3f 20 32 20 3a 20 61 72 67 63 3e 28 52 54  ) ? 2 : argc>(RT
3a6f8 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f  REE_MAX_DIMENSIO
3a6f9 4e 53 2a 32 2b 34 29 20 3f 20 33 20 3a 20 61 72  NS*2+4) ? 3 : ar
3a6fa 67 63 25 32 3b 0a 20 20 69 66 28 20 61 45 72 72  gc%2;.  if( aErr
3a6fb 4d 73 67 5b 69 45 72 72 5d 20 29 7b 0a 20 20 20  Msg[iErr] ){.   
3a6fc 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65   *pzErr = sqlite
3a6fd 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 20  3_mprintf("%s", 
3a6fe 61 45 72 72 4d 73 67 5b 69 45 72 72 5d 29 3b 0a  aErrMsg[iErr]);.
3a6ff 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
3a700 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20  E_ERROR;.  }..  
3a701 72 63 20 3d 20 67 65 74 50 61 67 65 53 69 7a 65  rc = getPageSize
3a702 28 64 62 2c 20 61 72 67 76 5b 31 5d 2c 20 26 69  (db, argv[1], &i
3a703 50 61 67 65 53 69 7a 65 29 3b 0a 20 20 69 66 28  PageSize);.  if(
3a704 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
3a705 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
3a706 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  .  }..  /* Alloc
3a707 61 74 65 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ate the sqlite3_
3a708 76 74 61 62 20 73 74 72 75 63 74 75 72 65 20 2a  vtab structure *
3a709 2f 0a 20 20 6e 44 62 20 3d 20 73 74 72 6c 65 6e  /.  nDb = strlen
3a70a 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 6e 4e 61  (argv[1]);.  nNa
3a70b 6d 65 20 3d 20 73 74 72 6c 65 6e 28 61 72 67 76  me = strlen(argv
3a70c 5b 32 5d 29 3b 0a 20 20 70 52 74 72 65 65 20 3d  [2]);.  pRtree =
3a70d 20 28 52 74 72 65 65 20 2a 29 73 71 6c 69 74 65   (Rtree *)sqlite
3a70e 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
3a70f 52 74 72 65 65 29 2b 6e 44 62 2b 6e 4e 61 6d 65  Rtree)+nDb+nName
3a710 2b 32 29 3b 0a 20 20 69 66 28 20 21 70 52 74 72  +2);.  if( !pRtr
3a711 65 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ee ){.    return
3a712 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
3a713 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 52 74 72   }.  memset(pRtr
3a714 65 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 52 74  ee, 0, sizeof(Rt
3a715 72 65 65 29 2b 6e 44 62 2b 6e 4e 61 6d 65 2b 32  ree)+nDb+nName+2
3a716 29 3b 0a 20 20 70 52 74 72 65 65 2d 3e 6e 42 75  );.  pRtree->nBu
3a717 73 79 20 3d 20 31 3b 0a 20 20 70 52 74 72 65 65  sy = 1;.  pRtree
3a718 2d 3e 62 61 73 65 2e 70 4d 6f 64 75 6c 65 20 3d  ->base.pModule =
3a719 20 26 72 74 72 65 65 4d 6f 64 75 6c 65 3b 0a 20   &rtreeModule;. 
3a71a 20 70 52 74 72 65 65 2d 3e 7a 44 62 20 3d 20 28   pRtree->zDb = (
3a71b 63 68 61 72 20 2a 29 26 70 52 74 72 65 65 5b 31  char *)&pRtree[1
3a71c 5d 3b 0a 20 20 70 52 74 72 65 65 2d 3e 7a 4e 61  ];.  pRtree->zNa
3a71d 6d 65 20 3d 20 26 70 52 74 72 65 65 2d 3e 7a 44  me = &pRtree->zD
3a71e 62 5b 6e 44 62 2b 31 5d 3b 0a 20 20 70 52 74 72  b[nDb+1];.  pRtr
3a71f 65 65 2d 3e 6e 44 69 6d 20 3d 20 28 61 72 67 63  ee->nDim = (argc
3a720 2d 34 29 2f 32 3b 0a 20 20 70 52 74 72 65 65 2d  -4)/2;.  pRtree-
3a721 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 20 3d  >nBytesPerCell =
3a722 20 38 20 2b 20 70 52 74 72 65 65 2d 3e 6e 44 69   8 + pRtree->nDi
3a723 6d 2a 34 2a 32 3b 0a 20 20 70 52 74 72 65 65 2d  m*4*2;.  pRtree-
3a724 3e 65 43 6f 6f 72 64 54 79 70 65 20 3d 20 65 43  >eCoordType = eC
3a725 6f 6f 72 64 54 79 70 65 3b 0a 20 20 6d 65 6d 63  oordType;.  memc
3a726 70 79 28 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20  py(pRtree->zDb, 
3a727 61 72 67 76 5b 31 5d 2c 20 6e 44 62 29 3b 0a 20  argv[1], nDb);. 
3a728 20 6d 65 6d 63 70 79 28 70 52 74 72 65 65 2d 3e   memcpy(pRtree->
3a729 7a 4e 61 6d 65 2c 20 61 72 67 76 5b 32 5d 2c 20  zName, argv[2], 
3a72a 6e 4e 61 6d 65 29 3b 0a 0a 20 20 2f 2a 20 46 69  nName);..  /* Fi
3a72b 67 75 72 65 20 6f 75 74 20 74 68 65 20 6e 6f 64  gure out the nod
3a72c 65 20 73 69 7a 65 20 74 6f 20 75 73 65 2e 20 42  e size to use. B
3a72d 79 20 64 65 66 61 75 6c 74 2c 20 75 73 65 20 36  y default, use 6
3a72e 34 20 62 79 74 65 73 20 6c 65 73 73 20 74 68 61  4 bytes less tha
3a72f 6e 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62  n.  ** the datab
3a730 61 73 65 20 70 61 67 65 2d 73 69 7a 65 2e 20 54  ase page-size. T
3a731 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74  his ensures that
3a732 20 65 61 63 68 20 6e 6f 64 65 20 69 73 20 73 74   each node is st
3a733 6f 72 65 64 20 6f 6e 0a 20 20 2a 2a 20 61 20 73  ored on.  ** a s
3a734 69 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 70  ingle database p
3a735 61 67 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  age..  **.  ** I
3a736 66 20 74 68 65 20 64 61 74 61 62 61 73 64 20 70  f the databasd p
3a737 61 67 65 2d 73 69 7a 65 20 69 73 20 73 6f 20 6c  age-size is so l
3a738 61 72 67 65 20 74 68 61 74 20 6d 6f 72 65 20 74  arge that more t
3a739 68 61 6e 20 52 54 52 45 45 5f 4d 41 58 43 45 4c  han RTREE_MAXCEL
3a73a 4c 53 0a 20 20 2a 2a 20 65 6e 74 72 69 65 73 20  LS.  ** entries 
3a73b 77 6f 75 6c 64 20 66 69 74 20 69 6e 20 61 20 73  would fit in a s
3a73c 69 6e 67 6c 65 20 6e 6f 64 65 2c 20 75 73 65 20  ingle node, use 
3a73d 61 20 73 6d 61 6c 6c 65 72 20 6e 6f 64 65 2d 73  a smaller node-s
3a73e 69 7a 65 2e 0a 20 20 2a 2f 0a 20 20 70 52 74 72  ize..  */.  pRtr
3a73f 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 20 3d 20  ee->iNodeSize = 
3a740 69 50 61 67 65 53 69 7a 65 2d 36 34 3b 0a 20 20  iPageSize-64;.  
3a741 69 66 28 20 28 34 2b 70 52 74 72 65 65 2d 3e 6e  if( (4+pRtree->n
3a742 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 52 54 52  BytesPerCell*RTR
3a743 45 45 5f 4d 41 58 43 45 4c 4c 53 29 3c 70 52 74  EE_MAXCELLS)<pRt
3a744 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 20 29  ree->iNodeSize )
3a745 7b 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 69 4e  {.    pRtree->iN
3a746 6f 64 65 53 69 7a 65 20 3d 20 34 2b 70 52 74 72  odeSize = 4+pRtr
3a747 65 65 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c  ee->nBytesPerCel
3a748 6c 2a 52 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53  l*RTREE_MAXCELLS
3a749 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61  ;.  }..  /* Crea
3a74a 74 65 2f 43 6f 6e 6e 65 63 74 20 74 6f 20 74 68  te/Connect to th
3a74b 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 72 65 6c  e underlying rel
3a74c 61 74 69 6f 6e 61 6c 20 64 61 74 61 62 61 73 65  ational database
3a74d 20 73 63 68 65 6d 61 2e 20 49 66 0a 20 20 2a 2a   schema. If.  **
3a74e 20 74 68 61 74 20 69 73 20 73 75 63 63 65 73 73   that is success
3a74f 66 75 6c 2c 20 63 61 6c 6c 20 73 71 6c 69 74 65  ful, call sqlite
3a750 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28 29  3_declare_vtab()
3a751 20 74 6f 20 63 6f 6e 66 69 67 75 72 65 0a 20 20   to configure.  
3a752 2a 2a 20 74 68 65 20 72 2d 74 72 65 65 20 74 61  ** the r-tree ta
3a753 62 6c 65 20 73 63 68 65 6d 61 2e 0a 20 20 2a 2f  ble schema..  */
3a754 0a 20 20 69 66 28 20 28 72 63 20 3d 20 72 74 72  .  if( (rc = rtr
3a755 65 65 53 71 6c 49 6e 69 74 28 70 52 74 72 65 65  eeSqlInit(pRtree
3a756 2c 20 64 62 2c 20 61 72 67 76 5b 31 5d 2c 20 61  , db, argv[1], a
3a757 72 67 76 5b 32 5d 2c 20 69 73 43 72 65 61 74 65  rgv[2], isCreate
3a758 29 29 20 29 7b 0a 20 20 20 20 2a 70 7a 45 72 72  )) ){.    *pzErr
3a759 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
3a75a 74 66 28 22 25 73 22 2c 20 73 71 6c 69 74 65 33  tf("%s", sqlite3
3a75b 5f 65 72 72 6d 73 67 28 64 62 29 29 3b 0a 20 20  _errmsg(db));.  
3a75c 7d 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20  }else{.    char 
3a75d 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f  *zSql = sqlite3_
3a75e 6d 70 72 69 6e 74 66 28 22 43 52 45 41 54 45 20  mprintf("CREATE 
3a75f 54 41 42 4c 45 20 78 28 25 73 22 2c 20 61 72 67  TABLE x(%s", arg
3a760 76 5b 33 5d 29 3b 0a 20 20 20 20 63 68 61 72 20  v[3]);.    char 
3a761 2a 7a 54 6d 70 3b 0a 20 20 20 20 69 6e 74 20 69  *zTmp;.    int i
3a762 69 3b 0a 20 20 20 20 66 6f 72 28 69 69 3d 34 3b  i;.    for(ii=4;
3a763 20 7a 53 71 6c 20 26 26 20 69 69 3c 61 72 67 63   zSql && ii<argc
3a764 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20 7a  ; ii++){.      z
3a765 54 6d 70 20 3d 20 7a 53 71 6c 3b 0a 20 20 20 20  Tmp = zSql;.    
3a766 20 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33    zSql = sqlite3
3a767 5f 6d 70 72 69 6e 74 66 28 22 25 73 2c 20 25 73  _mprintf("%s, %s
3a768 22 2c 20 7a 54 6d 70 2c 20 61 72 67 76 5b 69 69  ", zTmp, argv[ii
3a769 5d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ]);.      sqlite
3a76a 33 5f 66 72 65 65 28 7a 54 6d 70 29 3b 0a 20 20  3_free(zTmp);.  
3a76b 20 20 7d 0a 20 20 20 20 69 66 28 20 7a 53 71 6c    }.    if( zSql
3a76c 20 29 7b 0a 20 20 20 20 20 20 7a 54 6d 70 20 3d   ){.      zTmp =
3a76d 20 7a 53 71 6c 3b 0a 20 20 20 20 20 20 7a 53 71   zSql;.      zSq
3a76e 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  l = sqlite3_mpri
3a76f 6e 74 66 28 22 25 73 29 3b 22 2c 20 7a 54 6d 70  ntf("%s);", zTmp
3a770 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
3a771 5f 66 72 65 65 28 7a 54 6d 70 29 3b 0a 20 20 20  _free(zTmp);.   
3a772 20 7d 0a 20 20 20 20 69 66 28 20 21 7a 53 71 6c   }.    if( !zSql
3a773 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
3a774 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
3a775 20 7d 65 6c 73 65 20 69 66 28 20 53 51 4c 49 54   }else if( SQLIT
3a776 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK!=(rc = sqli
3a777 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62  te3_declare_vtab
3a778 28 64 62 2c 20 7a 53 71 6c 29 29 20 29 7b 0a 20  (db, zSql)) ){. 
3a779 20 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71       *pzErr = sq
3a77a 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25  lite3_mprintf("%
3a77b 73 22 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  s", sqlite3_errm
3a77c 73 67 28 64 62 29 29 3b 0a 20 20 20 20 7d 0a 20  sg(db));.    }. 
3a77d 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
3a77e 7a 53 71 6c 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  zSql);.  }..  if
3a77f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
3a780 29 7b 0a 20 20 20 20 2a 70 70 56 74 61 62 20 3d  ){.    *ppVtab =
3a781 20 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a   (sqlite3_vtab *
3a782 29 70 52 74 72 65 65 3b 0a 20 20 7d 65 6c 73 65  )pRtree;.  }else
3a783 7b 0a 20 20 20 20 72 74 72 65 65 52 65 6c 65 61  {.    rtreeRelea
3a784 73 65 28 70 52 74 72 65 65 29 3b 0a 20 20 7d 0a  se(pRtree);.  }.
3a785 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
3a786 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74  ./*.** Implement
3a787 61 74 69 6f 6e 20 6f 66 20 61 20 73 63 61 6c 61  ation of a scala
3a788 72 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20  r function that 
3a789 64 65 63 6f 64 65 73 20 72 2d 74 72 65 65 20 6e  decodes r-tree n
3a78a 6f 64 65 73 20 74 6f 0a 2a 2a 20 68 75 6d 61 6e  odes to.** human
3a78b 20 72 65 61 64 61 62 6c 65 20 73 74 72 69 6e 67   readable string
3a78c 73 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 75  s. This can be u
3a78d 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e  sed for debuggin
3a78e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 2e 0a  g and analysis..
3a78f 2a 2a 0a 2a 2a 20 54 68 65 20 73 63 61 6c 61 72  **.** The scalar
3a790 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73 20   function takes 
3a791 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2c 20 61  two arguments, a
3a792 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 63 6f   blob of data co
3a793 6e 74 61 69 6e 69 6e 67 0a 2a 2a 20 61 6e 20 72  ntaining.** an r
3a794 2d 74 72 65 65 20 6e 6f 64 65 2c 20 61 6e 64 20  -tree node, and 
3a795 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64 69  the number of di
3a796 6d 65 6e 73 69 6f 6e 73 20 74 68 65 20 72 2d 74  mensions the r-t
3a797 72 65 65 20 69 6e 64 65 78 65 73 2e 0a 2a 2a 20  ree indexes..** 
3a798 46 6f 72 20 61 20 74 77 6f 2d 64 69 6d 65 6e 73  For a two-dimens
3a799 69 6f 6e 61 6c 20 72 2d 74 72 65 65 20 73 74 72  ional r-tree str
3a79a 75 63 74 75 72 65 20 63 61 6c 6c 65 64 20 22 72  ucture called "r
3a79b 74 22 2c 20 74 6f 20 64 65 73 65 72 69 61 6c 69  t", to deseriali
3a79c 7a 65 0a 2a 2a 20 61 6c 6c 20 6e 6f 64 65 73 2c  ze.** all nodes,
3a79d 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6c 69 6b   a statement lik
3a79e 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43  e:.**.**   SELEC
3a79f 54 20 72 74 72 65 65 6e 6f 64 65 28 32 2c 20 64  T rtreenode(2, d
3a7a0 61 74 61 29 20 46 52 4f 4d 20 72 74 5f 6e 6f 64  ata) FROM rt_nod
3a7a1 65 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 75 6d  e;.**.** The hum
3a7a2 61 6e 20 72 65 61 64 61 62 6c 65 20 73 74 72 69  an readable stri
3a7a3 6e 67 20 74 61 6b 65 73 20 74 68 65 20 66 6f 72  ng takes the for
3a7a4 6d 20 6f 66 20 61 20 54 63 6c 20 6c 69 73 74 20  m of a Tcl list 
3a7a5 77 69 74 68 20 6f 6e 65 0a 2a 2a 20 65 6e 74 72  with one.** entr
3a7a6 79 20 66 6f 72 20 65 61 63 68 20 63 65 6c 6c 20  y for each cell 
3a7a7 69 6e 20 74 68 65 20 72 2d 74 72 65 65 20 6e 6f  in the r-tree no
3a7a8 64 65 2e 20 45 61 63 68 20 65 6e 74 72 79 20 69  de. Each entry i
3a7a9 73 20 69 74 73 65 6c 66 20 61 0a 2a 2a 20 6c 69  s itself a.** li
3a7aa 73 74 2c 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  st, containing t
3a7ab 68 65 20 38 2d 62 79 74 65 20 72 6f 77 69 64 2f  he 8-byte rowid/
3a7ac 70 61 67 65 6e 6f 20 66 6f 6c 6c 6f 77 65 64 20  pageno followed 
3a7ad 62 79 20 74 68 65 20 0a 2a 2a 20 3c 6e 75 6d 2d  by the .** <num-
3a7ae 64 69 6d 65 6e 73 69 6f 6e 3e 2a 32 20 63 6f 6f  dimension>*2 coo
3a7af 72 64 69 6e 61 74 65 73 2e 0a 2a 2f 0a 73 74 61  rdinates..*/.sta
3a7b0 74 69 63 20 76 6f 69 64 20 72 74 72 65 65 6e 6f  tic void rtreeno
3a7b1 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  de(sqlite3_conte
3a7b2 78 74 20 2a 63 74 78 2c 20 69 6e 74 20 6e 41 72  xt *ctx, int nAr
3a7b3 67 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  g, sqlite3_value
3a7b4 20 2a 2a 61 70 41 72 67 29 7b 0a 20 20 63 68 61   **apArg){.  cha
3a7b5 72 20 2a 7a 54 65 78 74 20 3d 20 30 3b 0a 20 20  r *zText = 0;.  
3a7b6 52 74 72 65 65 4e 6f 64 65 20 6e 6f 64 65 3b 0a  RtreeNode node;.
3a7b7 20 20 52 74 72 65 65 20 74 72 65 65 3b 0a 20 20    Rtree tree;.  
3a7b8 69 6e 74 20 69 69 3b 0a 0a 20 20 6d 65 6d 73 65  int ii;..  memse
3a7b9 74 28 26 6e 6f 64 65 2c 20 30 2c 20 73 69 7a 65  t(&node, 0, size
3a7ba 6f 66 28 52 74 72 65 65 4e 6f 64 65 29 29 3b 0a  of(RtreeNode));.
3a7bb 20 20 6d 65 6d 73 65 74 28 26 74 72 65 65 2c 20    memset(&tree, 
3a7bc 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65 29  0, sizeof(Rtree)
3a7bd 29 3b 0a 20 20 74 72 65 65 2e 6e 44 69 6d 20 3d  );.  tree.nDim =
3a7be 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
3a7bf 6e 74 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20 20  nt(apArg[0]);.  
3a7c0 74 72 65 65 2e 6e 42 79 74 65 73 50 65 72 43 65  tree.nBytesPerCe
3a7c1 6c 6c 20 3d 20 38 20 2b 20 38 20 2a 20 74 72 65  ll = 8 + 8 * tre
3a7c2 65 2e 6e 44 69 6d 3b 0a 20 20 6e 6f 64 65 2e 7a  e.nDim;.  node.z
3a7c3 44 61 74 61 20 3d 20 28 75 38 20 2a 29 73 71 6c  Data = (u8 *)sql
3a7c4 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28  ite3_value_blob(
3a7c5 61 70 41 72 67 5b 31 5d 29 3b 0a 0a 20 20 66 6f  apArg[1]);..  fo
3a7c6 72 28 69 69 3d 30 3b 20 69 69 3c 4e 43 45 4c 4c  r(ii=0; ii<NCELL
3a7c7 28 26 6e 6f 64 65 29 3b 20 69 69 2b 2b 29 7b 0a  (&node); ii++){.
3a7c8 20 20 20 20 63 68 61 72 20 7a 43 65 6c 6c 5b 35      char zCell[5
3a7c9 31 32 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 43 65  12];.    int nCe
3a7ca 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 52 74 72 65  ll = 0;.    Rtre
3a7cb 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20 20  eCell cell;.    
3a7cc 69 6e 74 20 6a 6a 3b 0a 0a 20 20 20 20 6e 6f 64  int jj;..    nod
3a7cd 65 47 65 74 43 65 6c 6c 28 26 74 72 65 65 2c 20  eGetCell(&tree, 
3a7ce 26 6e 6f 64 65 2c 20 69 69 2c 20 26 63 65 6c 6c  &node, ii, &cell
3a7cf 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  );.    sqlite3_s
3a7d0 6e 70 72 69 6e 74 66 28 35 31 32 2d 6e 43 65 6c  nprintf(512-nCel
3a7d1 6c 2c 26 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 2c  l,&zCell[nCell],
3a7d2 22 25 64 22 2c 20 63 65 6c 6c 2e 69 52 6f 77 69  "%d", cell.iRowi
3a7d3 64 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d 20  d);.    nCell = 
3a7d4 73 74 72 6c 65 6e 28 7a 43 65 6c 6c 29 3b 0a 20  strlen(zCell);. 
3a7d5 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a 3c     for(jj=0; jj<
3a7d6 74 72 65 65 2e 6e 44 69 6d 2a 32 3b 20 6a 6a 2b  tree.nDim*2; jj+
3a7d7 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
3a7d8 33 5f 73 6e 70 72 69 6e 74 66 28 35 31 32 2d 6e  3_snprintf(512-n
3a7d9 43 65 6c 6c 2c 26 7a 43 65 6c 6c 5b 6e 43 65 6c  Cell,&zCell[nCel
3a7da 6c 5d 2c 22 20 25 66 22 2c 28 64 6f 75 62 6c 65  l]," %f",(double
3a7db 29 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 6a 6a 5d  )cell.aCoord[jj]
3a7dc 2e 66 29 3b 0a 20 20 20 20 20 20 6e 43 65 6c 6c  .f);.      nCell
3a7dd 20 3d 20 73 74 72 6c 65 6e 28 7a 43 65 6c 6c 29   = strlen(zCell)
3a7de 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
3a7df 20 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20   zText ){.      
3a7e0 63 68 61 72 20 2a 7a 54 65 78 74 4e 65 77 20 3d  char *zTextNew =
3a7e1 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
3a7e2 28 22 25 73 20 7b 25 73 7d 22 2c 20 7a 54 65 78  ("%s {%s}", zTex
3a7e3 74 2c 20 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 20  t, zCell);.     
3a7e4 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 54   sqlite3_free(zT
3a7e5 65 78 74 29 3b 0a 20 20 20 20 20 20 7a 54 65 78  ext);.      zTex
3a7e6 74 20 3d 20 7a 54 65 78 74 4e 65 77 3b 0a 20 20  t = zTextNew;.  
3a7e7 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a    }else{.      z
3a7e8 54 65 78 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d  Text = sqlite3_m
3a7e9 70 72 69 6e 74 66 28 22 7b 25 73 7d 22 2c 20 7a  printf("{%s}", z
3a7ea 43 65 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Cell);.    }.  }
3a7eb 0a 20 20 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  .  .  sqlite3_re
3a7ec 73 75 6c 74 5f 74 65 78 74 28 63 74 78 2c 20 7a  sult_text(ctx, z
3a7ed 54 65 78 74 2c 20 2d 31 2c 20 73 71 6c 69 74 65  Text, -1, sqlite
3a7ee 33 5f 66 72 65 65 29 3b 0a 7d 0a 0a 73 74 61 74  3_free);.}..stat
3a7ef 69 63 20 76 6f 69 64 20 72 74 72 65 65 64 65 70  ic void rtreedep
3a7f0 74 68 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  th(sqlite3_conte
3a7f1 78 74 20 2a 63 74 78 2c 20 69 6e 74 20 6e 41 72  xt *ctx, int nAr
3a7f2 67 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  g, sqlite3_value
3a7f3 20 2a 2a 61 70 41 72 67 29 7b 0a 20 20 69 66 28   **apArg){.  if(
3a7f4 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
3a7f5 79 70 65 28 61 70 41 72 67 5b 30 5d 29 21 3d 53  ype(apArg[0])!=S
3a7f6 51 4c 49 54 45 5f 42 4c 4f 42 20 0a 20 20 20 7c  QLITE_BLOB .   |
3a7f7 7c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  | sqlite3_value_
3a7f8 62 79 74 65 73 28 61 70 41 72 67 5b 30 5d 29 3c  bytes(apArg[0])<
3a7f9 32 0a 20 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  2.  ){.    sqlit
3a7fa 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
3a7fb 63 74 78 2c 20 22 49 6e 76 61 6c 69 64 20 61 72  ctx, "Invalid ar
3a7fc 67 75 6d 65 6e 74 20 74 6f 20 72 74 72 65 65 64  gument to rtreed
3a7fd 65 70 74 68 28 29 22 2c 20 2d 31 29 3b 20 0a 20  epth()", -1); . 
3a7fe 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 38 20 2a   }else{.    u8 *
3a7ff 7a 42 6c 6f 62 20 3d 20 28 75 38 20 2a 29 73 71  zBlob = (u8 *)sq
3a800 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
3a801 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20 20 20 20  (apArg[0]);.    
3a802 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
3a803 6e 74 28 63 74 78 2c 20 72 65 61 64 49 6e 74 31  nt(ctx, readInt1
3a804 36 28 7a 42 6c 6f 62 29 29 3b 0a 20 20 7d 0a 7d  6(zBlob));.  }.}
3a805 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ../*.** Register
3a806 20 74 68 65 20 72 2d 74 72 65 65 20 6d 6f 64 75   the r-tree modu
3a807 6c 65 20 77 69 74 68 20 64 61 74 61 62 61 73 65  le with database
3a808 20 68 61 6e 64 6c 65 20 64 62 2e 20 54 68 69 73   handle db. This
3a809 20 63 72 65 61 74 65 73 20 74 68 65 0a 2a 2a 20   creates the.** 
3a80a 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
3a80b 64 75 6c 65 20 22 72 74 72 65 65 22 20 61 6e 64  dule "rtree" and
3a80c 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 2f 61   the debugging/a
3a80d 6e 61 6c 79 73 69 73 20 73 63 61 6c 61 72 20 0a  nalysis scalar .
3a80e 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 22 72 74 72  ** function "rtr
3a80f 65 65 6e 6f 64 65 22 2e 0a 2a 2f 0a 53 51 4c 49  eenode"..*/.SQLI
3a810 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
3a811 71 6c 69 74 65 33 52 74 72 65 65 49 6e 69 74 28  qlite3RtreeInit(
3a812 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20  sqlite3 *db){.  
3a813 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
3a814 4f 4b 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53  OK;..  if( rc==S
3a815 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
3a816 69 6e 74 20 75 74 66 38 20 3d 20 53 51 4c 49 54  int utf8 = SQLIT
3a817 45 5f 55 54 46 38 3b 0a 20 20 20 20 72 63 20 3d  E_UTF8;.    rc =
3a818 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
3a819 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 72 74  function(db, "rt
3a81a 72 65 65 6e 6f 64 65 22 2c 20 32 2c 20 75 74 66  reenode", 2, utf
3a81b 38 2c 20 30 2c 20 72 74 72 65 65 6e 6f 64 65 2c  8, 0, rtreenode,
3a81c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69 66   0, 0);.  }.  if
3a81d 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
3a81e 29 7b 0a 20 20 20 20 69 6e 74 20 75 74 66 38 20  ){.    int utf8 
3a81f 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20  = SQLITE_UTF8;. 
3a820 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
3a821 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
3a822 64 62 2c 20 22 72 74 72 65 65 64 65 70 74 68 22  db, "rtreedepth"
3a823 2c 20 31 2c 20 75 74 66 38 2c 20 30 2c 72 74 72  , 1, utf8, 0,rtr
3a824 65 65 64 65 70 74 68 2c 20 30 2c 20 30 29 3b 0a  eedepth, 0, 0);.
3a825 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
3a826 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 76  LITE_OK ){.    v
3a827 6f 69 64 20 2a 63 20 3d 20 28 76 6f 69 64 20 2a  oid *c = (void *
3a828 29 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45 41  )RTREE_COORD_REA
3a829 4c 33 32 3b 0a 20 20 20 20 72 63 20 3d 20 73 71  L32;.    rc = sq
3a82a 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64  lite3_create_mod
3a82b 75 6c 65 5f 76 32 28 64 62 2c 20 22 72 74 72 65  ule_v2(db, "rtre
3a82c 65 22 2c 20 26 72 74 72 65 65 4d 6f 64 75 6c 65  e", &rtreeModule
3a82d 2c 20 63 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69  , c, 0);.  }.  i
3a82e 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
3a82f 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 63 20   ){.    void *c 
3a830 3d 20 28 76 6f 69 64 20 2a 29 52 54 52 45 45 5f  = (void *)RTREE_
3a831 43 4f 4f 52 44 5f 49 4e 54 33 32 3b 0a 20 20 20  COORD_INT32;.   
3a832 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72   rc = sqlite3_cr
3a833 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 64  eate_module_v2(d
3a834 62 2c 20 22 72 74 72 65 65 5f 69 33 32 22 2c 20  b, "rtree_i32", 
3a835 26 72 74 72 65 65 4d 6f 64 75 6c 65 2c 20 63 2c  &rtreeModule, c,
3a836 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75   0);.  }..  retu
3a837 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 53  rn rc;.}..#if !S
3a838 51 4c 49 54 45 5f 43 4f 52 45 0a 53 51 4c 49 54  QLITE_CORE.SQLIT
3a839 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3a83a 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74  3_extension_init
3a83b 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
3a83c 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 4d  .  char **pzErrM
3a83d 73 67 2c 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  sg,.  const sqli
3a83e 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
3a83f 20 2a 70 41 70 69 0a 29 7b 0a 20 20 53 51 4c 49   *pApi.){.  SQLI
3a840 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e 49  TE_EXTENSION_INI
3a841 54 32 28 70 41 70 69 29 0a 20 20 72 65 74 75 72  T2(pApi).  retur
3a842 6e 20 73 71 6c 69 74 65 33 52 74 72 65 65 49 6e  n sqlite3RtreeIn
3a843 69 74 28 64 62 29 3b 0a 7d 0a 23 65 6e 64 69 66  it(db);.}.#endif
3a844 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a  ..#endif../*****
3a845 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
3a846 20 72 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a   rtree.c *******
3a847 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a848 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a849 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
3a84a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
3a84b 66 69 6c 65 20 69 63 75 2e 63 20 2a 2a 2a 2a 2a  file icu.c *****
3a84c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a84d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a84e 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
3a84f 32 30 30 37 20 4d 61 79 20 36 0a 2a 2a 0a 2a 2a  2007 May 6.**.**
3a850 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
3a851 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
3a852 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
3a853 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
3a854 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
3a855 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
3a856 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
3a857 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
3a858 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
3a859 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
3a85a 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
3a85b 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
3a85c 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
3a85d 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
3a85e 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
3a85f 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
3a860 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
3a861 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a862 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a863 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a864 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3a865 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49 64  *********.** $Id
3a866 3a 20 69 63 75 2e 63 2c 76 20 31 2e 37 20 32 30  : icu.c,v 1.7 20
3a867 30 37 2f 31 32 2f 31 33 20 32 31 3a 35 34 3a 31  07/12/13 21:54:1
3a868 31 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a 2a  1 drh Exp $.**.*
3a869 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c  * This file impl
3a86a 65 6d 65 6e 74 73 20 61 6e 20 69 6e 74 65 67 72  ements an integr
3a86b 61 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 74 68  ation between th
3a86c 65 20 49 43 55 20 6c 69 62 72 61 72 79 20 0a 2a  e ICU library .*
3a86d 2a 20 28 22 49 6e 74 65 72 6e 61 74 69 6f 6e 61  * ("Internationa
3a86e 6c 20 43 6f 6d 70 6f 6e 65 6e 74 73 20 66 6f 72  l Components for
3a86f 20 55 6e 69 63 6f 64 65 22 2c 20 61 6e 20 6f 70   Unicode", an op
3a870 65 6e 2d 73 6f 75 72 63 65 20 6c 69 62 72 61 72  en-source librar
3a871 79 20 0a 2a 2a 20 66 6f 72 20 68 61 6e 64 6c 69  y .** for handli
3a872 6e 67 20 75 6e 69 63 6f 64 65 20 64 61 74 61 29  ng unicode data)
3a873 20 61 6e 64 20 53 51 4c 69 74 65 2e 20 54 68 65   and SQLite. The
3a874 20 69 6e 74 65 67 72 61 74 69 6f 6e 20 75 73 65   integration use
3a875 73 20 0a 2a 2a 20 49 43 55 20 74 6f 20 70 72 6f  s .** ICU to pro
3a876 76 69 64 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69  vide the followi
3a877 6e 67 20 74 6f 20 53 51 4c 69 74 65 3a 0a 2a 2a  ng to SQLite:.**
3a878 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6d 70 6c 65  .**   * An imple
3a879 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
3a87a 20 53 51 4c 20 72 65 67 65 78 70 28 29 20 66 75   SQL regexp() fu
3a87b 6e 63 74 69 6f 6e 20 28 61 6e 64 20 68 65 6e 63  nction (and henc
3a87c 65 20 52 45 47 45 58 50 0a 2a 2a 20 20 20 20 20  e REGEXP.**     
3a87d 6f 70 65 72 61 74 6f 72 29 20 75 73 69 6e 67 20  operator) using 
3a87e 74 68 65 20 49 43 55 20 75 72 65 67 65 78 5f 58  the ICU uregex_X
3a87f 58 28 29 20 41 50 49 73 2e 0a 2a 2a 0a 2a 2a 20  X() APIs..**.** 
3a880 20 20 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69    * Implementati
3a881 6f 6e 73 20 6f 66 20 74 68 65 20 53 51 4c 20 73  ons of the SQL s
3a882 63 61 6c 61 72 20 75 70 70 65 72 28 29 20 61 6e  calar upper() an
3a883 64 20 6c 6f 77 65 72 28 29 20 66 75 6e 63 74 69  d lower() functi
3a884 6f 6e 73 0a 2a 2a 20 20 20 20 20 66 6f 72 20 63  ons.**     for c
3a885 61 73 65 20 6d 61 70 70 69 6e 67 2e 0a 2a 2a 0a  ase mapping..**.
3a886 2a 2a 20 20 20 2a 20 49 6e 74 65 67 72 61 74 69  **   * Integrati
3a887 6f 6e 20 6f 66 20 49 43 55 20 61 6e 64 20 53 51  on of ICU and SQ
3a888 4c 69 74 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  Lite collation s
3a889 65 71 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 20  eqences..**.**  
3a88a 20 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61   * An implementa
3a88b 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c 49 4b 45  tion of the LIKE
3a88c 20 6f 70 65 72 61 74 6f 72 20 74 68 61 74 20 75   operator that u
3a88d 73 65 73 20 49 43 55 20 74 6f 20 0a 2a 2a 20 20  ses ICU to .**  
3a88e 20 20 20 70 72 6f 76 69 64 65 20 63 61 73 65 2d     provide case-
3a88f 69 6e 64 65 70 65 6e 64 65 6e 74 20 6d 61 74 63  independent matc
3a890 68 69 6e 67 2e 0a 2a 2f 0a 0a 23 69 66 20 21 64  hing..*/..#if !d
3a891 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
3a892 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
3a893 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55  QLITE_ENABLE_ICU
3a894 29 0a 0a 2f 2a 20 49 6e 63 6c 75 64 65 20 49 43  )../* Include IC
3a895 55 20 68 65 61 64 65 72 73 20 2a 2f 0a 23 69 6e  U headers */.#in
3a896 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75  clude <unicode/u
3a897 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 64  types.h>.#includ
3a898 65 20 3c 75 6e 69 63 6f 64 65 2f 75 72 65 67 65  e <unicode/urege
3a899 78 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75  x.h>.#include <u
3a89a 6e 69 63 6f 64 65 2f 75 73 74 72 69 6e 67 2e 68  nicode/ustring.h
3a89b 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63  >.#include <unic
3a89c 6f 64 65 2f 75 63 6f 6c 2e 68 3e 0a 0a 0a 23 69  ode/ucol.h>...#i
3a89d 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52  fndef SQLITE_COR
3a89e 45 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e  E.  SQLITE_EXTEN
3a89f 53 49 4f 4e 5f 49 4e 49 54 31 0a 23 65 6c 73 65  SION_INIT1.#else
3a8a0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d  .#endif../*.** M
3a8a1 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 28 69  aximum length (i
3a8a2 6e 20 62 79 74 65 73 29 20 6f 66 20 74 68 65 20  n bytes) of the 
3a8a3 70 61 74 74 65 72 6e 20 69 6e 20 61 20 4c 49 4b  pattern in a LIK
3a8a4 45 20 6f 72 20 47 4c 4f 42 0a 2a 2a 20 6f 70 65  E or GLOB.** ope
3a8a5 72 61 74 6f 72 2e 0a 2a 2f 0a 23 69 66 6e 64 65  rator..*/.#ifnde
3a8a6 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b  f SQLITE_MAX_LIK
3a8a7 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48  E_PATTERN_LENGTH
3a8a8 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
3a8a9 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45 52  _MAX_LIKE_PATTER
3a8aa 4e 5f 4c 45 4e 47 54 48 20 35 30 30 30 30 0a 23  N_LENGTH 50000.#
3a8ab 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 56 65 72  endif../*.** Ver
3a8ac 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 5f  sion of sqlite3_
3a8ad 66 72 65 65 28 29 20 74 68 61 74 20 69 73 20 61  free() that is a
3a8ae 6c 77 61 79 73 20 61 20 66 75 6e 63 74 69 6f 6e  lways a function
3a8af 2c 20 6e 65 76 65 72 20 61 20 6d 61 63 72 6f 2e  , never a macro.
3a8b0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3a8b1 78 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a  xFree(void *p){.
3a8b2 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
3a8b3 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  );.}../*.** Comp
3a8b4 61 72 65 20 74 77 6f 20 55 54 46 2d 38 20 73 74  are two UTF-8 st
3a8b5 72 69 6e 67 73 20 66 6f 72 20 65 71 75 61 6c 69  rings for equali
3a8b6 74 79 20 77 68 65 72 65 20 74 68 65 20 66 69 72  ty where the fir
3a8b7 73 74 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a 20  st string is.** 
3a8b8 61 20 22 4c 49 4b 45 22 20 65 78 70 72 65 73 73  a "LIKE" express
3a8b9 69 6f 6e 2e 20 52 65 74 75 72 6e 20 74 72 75 65  ion. Return true
3a8ba 20 28 31 29 20 69 66 20 74 68 65 79 20 61 72 65   (1) if they are
3a8bb 20 74 68 65 20 73 61 6d 65 20 61 6e 64 20 0a 2a   the same and .*
3a8bc 2a 20 66 61 6c 73 65 20 28 30 29 20 69 66 20 74  * false (0) if t
3a8bd 68 65 79 20 61 72 65 20 64 69 66 66 65 72 65 6e  hey are differen
3a8be 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
3a8bf 20 69 63 75 4c 69 6b 65 43 6f 6d 70 61 72 65 28   icuLikeCompare(
3a8c0 0a 20 20 63 6f 6e 73 74 20 75 69 6e 74 38 5f 74  .  const uint8_t
3a8c1 20 2a 7a 50 61 74 74 65 72 6e 2c 20 20 20 2f 2a   *zPattern,   /*
3a8c2 20 4c 49 4b 45 20 70 61 74 74 65 72 6e 20 2a 2f   LIKE pattern */
3a8c3 0a 20 20 63 6f 6e 73 74 20 75 69 6e 74 38 5f 74  .  const uint8_t
3a8c4 20 2a 7a 53 74 72 69 6e 67 2c 20 20 20 20 2f 2a   *zString,    /*
3a8c5 20 54 68 65 20 55 54 46 2d 38 20 73 74 72 69 6e   The UTF-8 strin
3a8c6 67 20 74 6f 20 63 6f 6d 70 61 72 65 20 61 67 61  g to compare aga
3a8c7 69 6e 73 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  inst */.  const 
3a8c8 55 43 68 61 72 33 32 20 75 45 73 63 20 20 20 20  UChar32 uEsc    
3a8c9 20 20 20 20 20 2f 2a 20 54 68 65 20 65 73 63 61       /* The esca
3a8ca 70 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a  pe character */.
3a8cb 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ){.  static cons
3a8cc 74 20 69 6e 74 20 4d 41 54 43 48 5f 4f 4e 45 20  t int MATCH_ONE 
3a8cd 3d 20 28 55 43 68 61 72 33 32 29 27 5f 27 3b 0a  = (UChar32)'_';.
3a8ce 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 69    static const i
3a8cf 6e 74 20 4d 41 54 43 48 5f 41 4c 4c 20 3d 20 28  nt MATCH_ALL = (
3a8d0 55 43 68 61 72 33 32 29 27 25 27 3b 0a 0a 20 20  UChar32)'%';..  
3a8d1 69 6e 74 20 69 50 61 74 74 65 72 6e 20 3d 20 30  int iPattern = 0
3a8d2 3b 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65  ;       /* Curre
3a8d3 6e 74 20 62 79 74 65 20 69 6e 64 65 78 20 69 6e  nt byte index in
3a8d4 20 7a 50 61 74 74 65 72 6e 20 2a 2f 0a 20 20 69   zPattern */.  i
3a8d5 6e 74 20 69 53 74 72 69 6e 67 20 3d 20 30 3b 20  nt iString = 0; 
3a8d6 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e         /* Curren
3a8d7 74 20 62 79 74 65 20 69 6e 64 65 78 20 69 6e 20  t byte index in 
3a8d8 7a 53 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 69 6e  zString */..  in
3a8d9 74 20 70 72 65 76 45 73 63 61 70 65 20 3d 20 30  t prevEscape = 0
3a8da 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  ;     /* True if
3a8db 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63 68   the previous ch
3a8dc 61 72 61 63 74 65 72 20 77 61 73 20 75 45 73 63  aracter was uEsc
3a8dd 20 2a 2f 0a 0a 20 20 77 68 69 6c 65 28 20 7a 50   */..  while( zP
3a8de 61 74 74 65 72 6e 5b 69 50 61 74 74 65 72 6e 5d  attern[iPattern]
3a8df 21 3d 30 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 52  !=0 ){..    /* R
3a8e0 65 61 64 20 28 61 6e 64 20 63 6f 6e 73 75 6d 65  ead (and consume
3a8e1 29 20 74 68 65 20 6e 65 78 74 20 63 68 61 72 61  ) the next chara
3a8e2 63 74 65 72 20 66 72 6f 6d 20 74 68 65 20 69 6e  cter from the in
3a8e3 70 75 74 20 70 61 74 74 65 72 6e 2e 20 2a 2f 0a  put pattern. */.
3a8e4 20 20 20 20 55 43 68 61 72 33 32 20 75 50 61 74      UChar32 uPat
3a8e5 74 65 72 6e 3b 0a 20 20 20 20 55 38 5f 4e 45 58  tern;.    U8_NEX
3a8e6 54 5f 55 4e 53 41 46 45 28 7a 50 61 74 74 65 72  T_UNSAFE(zPatter
3a8e7 6e 2c 20 69 50 61 74 74 65 72 6e 2c 20 75 50 61  n, iPattern, uPa
3a8e8 74 74 65 72 6e 29 3b 0a 20 20 20 20 61 73 73 65  ttern);.    asse
3a8e9 72 74 28 75 50 61 74 74 65 72 6e 21 3d 30 29 3b  rt(uPattern!=0);
3a8ea 0a 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20 61  ..    /* There a
3a8eb 72 65 20 6e 6f 77 20 34 20 70 6f 73 73 69 62 69  re now 4 possibi
3a8ec 6c 69 74 69 65 73 3a 0a 20 20 20 20 2a 2a 0a 20  lities:.    **. 
3a8ed 20 20 20 2a 2a 20 20 20 20 20 31 2e 20 75 50 61     **     1. uPa
3a8ee 74 74 65 72 6e 20 69 73 20 61 6e 20 75 6e 65 73  ttern is an unes
3a8ef 63 61 70 65 64 20 6d 61 74 63 68 2d 61 6c 6c 20  caped match-all 
3a8f0 63 68 61 72 61 63 74 65 72 20 22 25 22 2c 0a 20  character "%",. 
3a8f1 20 20 20 2a 2a 20 20 20 20 20 32 2e 20 75 50 61     **     2. uPa
3a8f2 74 74 65 72 6e 20 69 73 20 61 6e 20 75 6e 65 73  ttern is an unes
3a8f3 63 61 70 65 64 20 6d 61 74 63 68 2d 6f 6e 65 20  caped match-one 
3a8f4 63 68 61 72 61 63 74 65 72 20 22 5f 22 2c 0a 20  character "_",. 
3a8f5 20 20 20 2a 2a 20 20 20 20 20 33 2e 20 75 50 61     **     3. uPa
3a8f6 74 74 65 72 6e 20 69 73 20 61 6e 20 75 6e 65 73  ttern is an unes
3a8f7 63 61 70 65 64 20 65 73 63 61 70 65 20 63 68 61  caped escape cha
3a8f8 72 61 63 74 65 72 2c 20 6f 72 0a 20 20 20 20 2a  racter, or.    *
3a8f9 2a 20 20 20 20 20 34 2e 20 75 50 61 74 74 65 72  *     4. uPatter
3a8fa 6e 20 69 73 20 74 6f 20 62 65 20 68 61 6e 64 6c  n is to be handl
3a8fb 65 64 20 61 73 20 61 6e 20 6f 72 64 69 6e 61 72  ed as an ordinar
3a8fc 79 20 63 68 61 72 61 63 74 65 72 0a 20 20 20 20  y character.    
3a8fd 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 72 65 76  */.    if( !prev
3a8fe 45 73 63 61 70 65 20 26 26 20 75 50 61 74 74 65  Escape && uPatte
3a8ff 72 6e 3d 3d 4d 41 54 43 48 5f 41 4c 4c 20 29 7b  rn==MATCH_ALL ){
3a900 0a 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 31  .      /* Case 1
3a901 2e 20 2a 2f 0a 20 20 20 20 20 20 75 69 6e 74 38  . */.      uint8
3a902 5f 74 20 63 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  _t c;..      /* 
3a903 53 6b 69 70 20 61 6e 79 20 4d 41 54 43 48 5f 41  Skip any MATCH_A
3a904 4c 4c 20 6f 72 20 4d 41 54 43 48 5f 4f 4e 45 20  LL or MATCH_ONE 
3a905 63 68 61 72 61 63 74 65 72 73 20 74 68 61 74 20  characters that 
3a906 66 6f 6c 6c 6f 77 20 61 0a 20 20 20 20 20 20 2a  follow a.      *
3a907 2a 20 4d 41 54 43 48 5f 41 4c 4c 2e 20 46 6f 72  * MATCH_ALL. For
3a908 20 65 61 63 68 20 4d 41 54 43 48 5f 4f 4e 45 2c   each MATCH_ONE,
3a909 20 73 6b 69 70 20 6f 6e 65 20 63 68 61 72 61 63   skip one charac
3a90a 74 65 72 20 69 6e 20 74 68 65 20 0a 20 20 20 20  ter in the .    
3a90b 20 20 2a 2a 20 74 65 73 74 20 73 74 72 69 6e 67    ** test string
3a90c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
3a90d 20 77 68 69 6c 65 28 20 28 63 3d 7a 50 61 74 74   while( (c=zPatt
3a90e 65 72 6e 5b 69 50 61 74 74 65 72 6e 5d 29 20 3d  ern[iPattern]) =
3a90f 3d 20 4d 41 54 43 48 5f 41 4c 4c 20 7c 7c 20 63  = MATCH_ALL || c
3a910 20 3d 3d 20 4d 41 54 43 48 5f 4f 4e 45 20 29 7b   == MATCH_ONE ){
3a911 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d 3d  .        if( c==
3a912 4d 41 54 43 48 5f 4f 4e 45 20 29 7b 0a 20 20 20  MATCH_ONE ){.   
3a913 20 20 20 20 20 20 20 69 66 28 20 7a 53 74 72 69         if( zStri
3a914 6e 67 5b 69 53 74 72 69 6e 67 5d 3d 3d 30 20 29  ng[iString]==0 )
3a915 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
3a916 20 20 20 20 20 55 38 5f 46 57 44 5f 31 5f 55 4e       U8_FWD_1_UN
3a917 53 41 46 45 28 7a 53 74 72 69 6e 67 2c 20 69 53  SAFE(zString, iS
3a918 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 20  tring);.        
3a919 7d 0a 20 20 20 20 20 20 20 20 69 50 61 74 74 65  }.        iPatte
3a91a 72 6e 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  rn++;.      }.. 
3a91b 20 20 20 20 20 69 66 28 20 7a 50 61 74 74 65 72       if( zPatter
3a91c 6e 5b 69 50 61 74 74 65 72 6e 5d 3d 3d 30 20 29  n[iPattern]==0 )
3a91d 20 72 65 74 75 72 6e 20 31 3b 0a 0a 20 20 20 20   return 1;..    
3a91e 20 20 77 68 69 6c 65 28 20 7a 53 74 72 69 6e 67    while( zString
3a91f 5b 69 53 74 72 69 6e 67 5d 20 29 7b 0a 20 20 20  [iString] ){.   
3a920 20 20 20 20 20 69 66 28 20 69 63 75 4c 69 6b 65       if( icuLike
3a921 43 6f 6d 70 61 72 65 28 26 7a 50 61 74 74 65 72  Compare(&zPatter
3a922 6e 5b 69 50 61 74 74 65 72 6e 5d 2c 20 26 7a 53  n[iPattern], &zS
3a923 74 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d 2c 20  tring[iString], 
3a924 75 45 73 63 29 20 29 7b 0a 20 20 20 20 20 20 20  uEsc) ){.       
3a925 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20     return 1;.   
3a926 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 55       }.        U
3a927 38 5f 46 57 44 5f 31 5f 55 4e 53 41 46 45 28 7a  8_FWD_1_UNSAFE(z
3a928 53 74 72 69 6e 67 2c 20 69 53 74 72 69 6e 67 29  String, iString)
3a929 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
3a92a 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 20 20 7d  return 0;..    }
3a92b 65 6c 73 65 20 69 66 28 20 21 70 72 65 76 45 73  else if( !prevEs
3a92c 63 61 70 65 20 26 26 20 75 50 61 74 74 65 72 6e  cape && uPattern
3a92d 3d 3d 4d 41 54 43 48 5f 4f 4e 45 20 29 7b 0a 20  ==MATCH_ONE ){. 
3a92e 20 20 20 20 20 2f 2a 20 43 61 73 65 20 32 2e 20       /* Case 2. 
3a92f 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 7a 53 74  */.      if( zSt
3a930 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d 3d 3d 30  ring[iString]==0
3a931 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
3a932 20 20 20 55 38 5f 46 57 44 5f 31 5f 55 4e 53 41     U8_FWD_1_UNSA
3a933 46 45 28 7a 53 74 72 69 6e 67 2c 20 69 53 74 72  FE(zString, iStr
3a934 69 6e 67 29 3b 0a 0a 20 20 20 20 7d 65 6c 73 65  ing);..    }else
3a935 20 69 66 28 20 21 70 72 65 76 45 73 63 61 70 65   if( !prevEscape
3a936 20 26 26 20 75 50 61 74 74 65 72 6e 3d 3d 75 45   && uPattern==uE
3a937 73 63 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61  sc){.      /* Ca
3a938 73 65 20 33 2e 20 2a 2f 0a 20 20 20 20 20 20 70  se 3. */.      p
3a939 72 65 76 45 73 63 61 70 65 20 3d 20 31 3b 0a 0a  revEscape = 1;..
3a93a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
3a93b 20 2f 2a 20 43 61 73 65 20 34 2e 20 2a 2f 0a 20   /* Case 4. */. 
3a93c 20 20 20 20 20 55 43 68 61 72 33 32 20 75 53 74       UChar32 uSt
3a93d 72 69 6e 67 3b 0a 20 20 20 20 20 20 55 38 5f 4e  ring;.      U8_N
3a93e 45 58 54 5f 55 4e 53 41 46 45 28 7a 53 74 72 69  EXT_UNSAFE(zStri
3a93f 6e 67 2c 20 69 53 74 72 69 6e 67 2c 20 75 53 74  ng, iString, uSt
3a940 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 75 53 74  ring);.      uSt
3a941 72 69 6e 67 20 3d 20 75 5f 66 6f 6c 64 43 61 73  ring = u_foldCas
3a942 65 28 75 53 74 72 69 6e 67 2c 20 55 5f 46 4f 4c  e(uString, U_FOL
3a943 44 5f 43 41 53 45 5f 44 45 46 41 55 4c 54 29 3b  D_CASE_DEFAULT);
3a944 0a 20 20 20 20 20 20 75 50 61 74 74 65 72 6e 20  .      uPattern 
3a945 3d 20 75 5f 66 6f 6c 64 43 61 73 65 28 75 50 61  = u_foldCase(uPa
3a946 74 74 65 72 6e 2c 20 55 5f 46 4f 4c 44 5f 43 41  ttern, U_FOLD_CA
3a947 53 45 5f 44 45 46 41 55 4c 54 29 3b 0a 20 20 20  SE_DEFAULT);.   
3a948 20 20 20 69 66 28 20 75 53 74 72 69 6e 67 21 3d     if( uString!=
3a949 75 50 61 74 74 65 72 6e 20 29 7b 0a 20 20 20 20  uPattern ){.    
3a94a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
3a94b 20 20 20 20 7d 0a 20 20 20 20 20 20 70 72 65 76      }.      prev
3a94c 45 73 63 61 70 65 20 3d 20 30 3b 0a 20 20 20 20  Escape = 0;.    
3a94d 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  }.  }..  return 
3a94e 7a 53 74 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d  zString[iString]
3a94f 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d  ==0;.}../*.** Im
3a950 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
3a951 74 68 65 20 6c 69 6b 65 28 29 20 53 51 4c 20 66  the like() SQL f
3a952 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 66  unction.  This f
3a953 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e  unction implemen
3a954 74 73 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64 2d  ts.** the build-
3a955 69 6e 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72  in LIKE operator
3a956 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67  .  The first arg
3a957 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 75 6e  ument to the fun
3a958 63 74 69 6f 6e 20 69 73 20 74 68 65 0a 2a 2a 20  ction is the.** 
3a959 70 61 74 74 65 72 6e 20 61 6e 64 20 74 68 65 20  pattern and the 
3a95a 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
3a95b 69 73 20 74 68 65 20 73 74 72 69 6e 67 2e 20 20  is the string.  
3a95c 53 6f 2c 20 74 68 65 20 53 51 4c 20 73 74 61 74  So, the SQL stat
3a95d 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ements:.**.**   
3a95e 20 20 20 20 41 20 4c 49 4b 45 20 42 0a 2a 2a 0a      A LIKE B.**.
3a95f 2a 2a 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65  ** is implemente
3a960 64 20 61 73 20 6c 69 6b 65 28 42 2c 20 41 29 2e  d as like(B, A).
3a961 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6e 20   If there is an 
3a962 65 73 63 61 70 65 20 63 68 61 72 61 63 74 65 72  escape character
3a963 20 45 2c 20 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20   E, .**.**      
3a964 20 41 20 4c 49 4b 45 20 42 20 45 53 43 41 50 45   A LIKE B ESCAPE
3a965 20 45 0a 2a 2a 0a 2a 2a 20 69 73 20 6d 61 70 70   E.**.** is mapp
3a966 65 64 20 74 6f 20 6c 69 6b 65 28 42 2c 20 41 2c  ed to like(B, A,
3a967 20 45 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76   E)..*/.static v
3a968 6f 69 64 20 69 63 75 4c 69 6b 65 46 75 6e 63 28  oid icuLikeFunc(
3a969 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
3a96a 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20  xt *context, .  
3a96b 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73 71 6c  int argc, .  sql
3a96c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
3a96d 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73  v.){.  const uns
3a96e 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 41 20 3d  igned char *zA =
3a96f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
3a970 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  ext(argv[0]);.  
3a971 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
3a972 68 61 72 20 2a 7a 42 20 3d 20 73 71 6c 69 74 65  har *zB = sqlite
3a973 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
3a974 76 5b 31 5d 29 3b 0a 20 20 55 43 68 61 72 33 32  v[1]);.  UChar32
3a975 20 75 45 73 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a   uEsc = 0;..  /*
3a976 20 4c 69 6d 69 74 20 74 68 65 20 6c 65 6e 67 74   Limit the lengt
3a977 68 20 6f 66 20 74 68 65 20 4c 49 4b 45 20 6f 72  h of the LIKE or
3a978 20 47 4c 4f 42 20 70 61 74 74 65 72 6e 20 74 6f   GLOB pattern to
3a979 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0a   avoid problems.
3a97a 20 20 2a 2a 20 6f 66 20 64 65 65 70 20 72 65 63    ** of deep rec
3a97b 75 72 73 69 6f 6e 20 61 6e 64 20 4e 2a 4e 20 62  ursion and N*N b
3a97c 65 68 61 76 69 6f 72 20 69 6e 20 70 61 74 74 65  ehavior in patte
3a97d 72 6e 43 6f 6d 70 61 72 65 28 29 2e 0a 20 20 2a  rnCompare()..  *
3a97e 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  /.  if( sqlite3_
3a97f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
3a980 5b 30 5d 29 3e 53 51 4c 49 54 45 5f 4d 41 58 5f  [0])>SQLITE_MAX_
3a981 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e  LIKE_PATTERN_LEN
3a982 47 54 48 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  GTH ){.    sqlit
3a983 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
3a984 63 6f 6e 74 65 78 74 2c 20 22 4c 49 4b 45 20 6f  context, "LIKE o
3a985 72 20 47 4c 4f 42 20 70 61 74 74 65 72 6e 20 74  r GLOB pattern t
3a986 6f 6f 20 63 6f 6d 70 6c 65 78 22 2c 20 2d 31 29  oo complex", -1)
3a987 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
3a988 7d 0a 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d  }...  if( argc==
3a989 33 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  3 ){.    /* The 
3a98a 65 73 63 61 70 65 20 63 68 61 72 61 63 74 65 72  escape character
3a98b 20 73 74 72 69 6e 67 20 6d 75 73 74 20 63 6f 6e   string must con
3a98c 73 69 73 74 20 6f 66 20 61 20 73 69 6e 67 6c 65  sist of a single
3a98d 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72   UTF-8 character
3a98e 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69  ..    ** Otherwi
3a98f 73 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72  se, return an er
3a990 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ror..    */.    
3a991 69 6e 74 20 6e 45 3d 20 73 71 6c 69 74 65 33 5f  int nE= sqlite3_
3a992 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
3a993 5b 32 5d 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20  [2]);.    const 
3a994 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
3a995 45 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  E = sqlite3_valu
3a996 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29 3b  e_text(argv[2]);
3a997 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a  .    int i = 0;.
3a998 20 20 20 20 69 66 28 20 7a 45 3d 3d 30 20 29 20      if( zE==0 ) 
3a999 72 65 74 75 72 6e 3b 0a 20 20 20 20 55 38 5f 4e  return;.    U8_N
3a99a 45 58 54 28 7a 45 2c 20 69 2c 20 6e 45 2c 20 75  EXT(zE, i, nE, u
3a99b 45 73 63 29 3b 0a 20 20 20 20 69 66 28 20 69 21  Esc);.    if( i!
3a99c 3d 6e 45 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =nE){.      sqli
3a99d 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
3a99e 28 63 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20  (context, .     
3a99f 20 20 20 20 20 22 45 53 43 41 50 45 20 65 78 70       "ESCAPE exp
3a9a0 72 65 73 73 69 6f 6e 20 6d 75 73 74 20 62 65 20  ression must be 
3a9a1 61 20 73 69 6e 67 6c 65 20 63 68 61 72 61 63 74  a single charact
3a9a2 65 72 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20  er", -1);.      
3a9a3 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
3a9a4 7d 0a 0a 20 20 69 66 28 20 7a 41 20 26 26 20 7a  }..  if( zA && z
3a9a5 42 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  B ){.    sqlite3
3a9a6 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74  _result_int(cont
3a9a7 65 78 74 2c 20 69 63 75 4c 69 6b 65 43 6f 6d 70  ext, icuLikeComp
3a9a8 61 72 65 28 7a 41 2c 20 7a 42 2c 20 75 45 73 63  are(zA, zB, uEsc
3a9a9 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ));.  }.}../*.**
3a9aa 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
3a9ab 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e  s called when an
3a9ac 20 49 43 55 20 66 75 6e 63 74 69 6f 6e 20 63 61   ICU function ca
3a9ad 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e  lled from within
3a9ae 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  .** the implemen
3a9af 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c  tation of an SQL
3a9b0 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
3a9b1 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f   returns an erro
3a9b2 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 63 61  r..**.** The sca
3a9b3 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6e  lar function con
3a9b4 74 65 78 74 20 70 61 73 73 65 64 20 61 73 20 74  text passed as t
3a9b5 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
3a9b6 74 20 69 73 20 0a 2a 2a 20 6c 6f 61 64 65 64 20  t is .** loaded 
3a9b7 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d 65  with an error me
3a9b8 73 73 61 67 65 20 62 61 73 65 64 20 6f 6e 20 74  ssage based on t
3a9b9 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f  he following two
3a9ba 20 61 72 67 73 2e 0a 2a 2f 0a 73 74 61 74 69 63   args..*/.static
3a9bb 20 76 6f 69 64 20 69 63 75 46 75 6e 63 74 69 6f   void icuFunctio
3a9bc 6e 45 72 72 6f 72 28 0a 20 20 73 71 6c 69 74 65  nError(.  sqlite
3a9bd 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c  3_context *pCtx,
3a9be 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65         /* SQLite
3a9bf 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
3a9c0 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63 6f   context */.  co
3a9c1 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c  nst char *zName,
3a9c2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
3a9c3 6d 65 20 6f 66 20 49 43 55 20 66 75 6e 63 74 69  me of ICU functi
3a9c4 6f 6e 20 74 68 61 74 20 66 61 69 6c 65 64 20 2a  on that failed *
3a9c5 2f 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20 65  /.  UErrorCode e
3a9c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3a9c7 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20 72   /* Error code r
3a9c8 65 74 75 72 6e 65 64 20 62 79 20 49 43 55 20 66  eturned by ICU f
3a9c9 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20  unction */.){.  
3a9ca 63 68 61 72 20 7a 42 75 66 5b 31 32 38 5d 3b 0a  char zBuf[128];.
3a9cb 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
3a9cc 74 66 28 31 32 38 2c 20 7a 42 75 66 2c 20 22 49  tf(128, zBuf, "I
3a9cd 43 55 20 65 72 72 6f 72 3a 20 25 73 28 29 3a 20  CU error: %s(): 
3a9ce 25 73 22 2c 20 7a 4e 61 6d 65 2c 20 75 5f 65 72  %s", zName, u_er
3a9cf 72 6f 72 4e 61 6d 65 28 65 29 29 3b 0a 20 20 7a  rorName(e));.  z
3a9d0 42 75 66 5b 31 32 37 5d 20 3d 20 27 5c 30 27 3b  Buf[127] = '\0';
3a9d1 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
3a9d2 74 5f 65 72 72 6f 72 28 70 43 74 78 2c 20 7a 42  t_error(pCtx, zB
3a9d3 75 66 2c 20 2d 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  uf, -1);.}../*.*
3a9d4 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 64 65  * Function to de
3a9d5 6c 65 74 65 20 63 6f 6d 70 69 6c 65 64 20 72 65  lete compiled re
3a9d6 67 65 78 70 20 6f 62 6a 65 63 74 73 2e 20 52 65  gexp objects. Re
3a9d7 67 69 73 74 65 72 65 64 20 61 73 0a 2a 2a 20 61  gistered as.** a
3a9d8 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63   destructor func
3a9d9 74 69 6f 6e 20 77 69 74 68 20 73 71 6c 69 74 65  tion with sqlite
3a9da 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 2e  3_set_auxdata().
3a9db 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3a9dc 69 63 75 52 65 67 65 78 70 44 65 6c 65 74 65 28  icuRegexpDelete(
3a9dd 76 6f 69 64 20 2a 70 29 7b 0a 20 20 55 52 65 67  void *p){.  UReg
3a9de 75 6c 61 72 45 78 70 72 65 73 73 69 6f 6e 20 2a  ularExpression *
3a9df 70 45 78 70 72 20 3d 20 28 55 52 65 67 75 6c 61  pExpr = (URegula
3a9e0 72 45 78 70 72 65 73 73 69 6f 6e 20 2a 29 70 3b  rExpression *)p;
3a9e1 0a 20 20 75 72 65 67 65 78 5f 63 6c 6f 73 65 28  .  uregex_close(
3a9e2 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pExpr);.}../*.**
3a9e3 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
3a9e4 6f 66 20 53 51 4c 69 74 65 20 52 45 47 45 58 50  of SQLite REGEXP
3a9e5 20 6f 70 65 72 61 74 6f 72 2e 20 54 68 69 73 20   operator. This 
3a9e6 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20  scalar function 
3a9e7 74 61 6b 65 73 0a 2a 2a 20 74 77 6f 20 61 72 67  takes.** two arg
3a9e8 75 6d 65 6e 74 73 2e 20 54 68 65 20 66 69 72 73  uments. The firs
3a9e9 74 20 69 73 20 61 20 72 65 67 75 6c 61 72 20 65  t is a regular e
3a9ea 78 70 72 65 73 73 69 6f 6e 20 70 61 74 74 65 72  xpression patter
3a9eb 6e 20 74 6f 20 63 6f 6d 70 69 6c 65 0a 2a 2a 20  n to compile.** 
3a9ec 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 61 20  the second is a 
3a9ed 73 74 72 69 6e 67 20 74 6f 20 6d 61 74 63 68 20  string to match 
3a9ee 61 67 61 69 6e 73 74 20 74 68 61 74 20 70 61 74  against that pat
3a9ef 74 65 72 6e 2e 20 49 66 20 65 69 74 68 65 72 20  tern. If either 
3a9f0 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73 20  .** argument is 
3a9f1 61 6e 20 53 51 4c 20 4e 55 4c 4c 2c 20 74 68 65  an SQL NULL, the
3a9f2 6e 20 4e 55 4c 4c 20 49 73 20 72 65 74 75 72 6e  n NULL Is return
3a9f3 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 74  ed. Otherwise, t
3a9f4 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73 20  he result.** is 
3a9f5 31 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 20  1 if the string 
3a9f6 6d 61 74 63 68 65 73 20 74 68 65 20 70 61 74 74  matches the patt
3a9f7 65 72 6e 2c 20 6f 72 20 30 20 6f 74 68 65 72 77  ern, or 0 otherw
3a9f8 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  ise..**.** SQLit
3a9f9 65 20 6d 61 70 73 20 74 68 65 20 72 65 67 65 78  e maps the regex
3a9fa 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  p() function to 
3a9fb 74 68 65 20 72 65 67 65 78 70 28 29 20 6f 70 65  the regexp() ope
3a9fc 72 61 74 6f 72 20 73 75 63 68 0a 2a 2a 20 74 68  rator such.** th
3a9fd 61 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  at the following
3a9fe 20 74 77 6f 20 61 72 65 20 65 71 75 69 76 61 6c   two are equival
3a9ff 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 7a  ent:.**.**     z
3aa00 53 74 72 69 6e 67 20 52 45 47 45 58 50 20 7a 50  String REGEXP zP
3aa01 61 74 74 65 72 6e 0a 2a 2a 20 20 20 20 20 72 65  attern.**     re
3aa02 67 65 78 70 28 7a 50 61 74 74 65 72 6e 2c 20 7a  gexp(zPattern, z
3aa03 53 74 72 69 6e 67 29 0a 2a 2a 0a 2a 2a 20 55 73  String).**.** Us
3aa04 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  es the following
3aa05 20 49 43 55 20 72 65 67 65 78 70 20 41 50 49 73   ICU regexp APIs
3aa06 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 75 72 65 67  :.**.**     ureg
3aa07 65 78 5f 6f 70 65 6e 28 29 0a 2a 2a 20 20 20 20  ex_open().**    
3aa08 20 75 72 65 67 65 78 5f 6d 61 74 63 68 65 73 28   uregex_matches(
3aa09 29 0a 2a 2a 20 20 20 20 20 75 72 65 67 65 78 5f  ).**     uregex_
3aa0a 63 6c 6f 73 65 28 29 0a 2a 2f 0a 73 74 61 74 69  close().*/.stati
3aa0b 63 20 76 6f 69 64 20 69 63 75 52 65 67 65 78 70  c void icuRegexp
3aa0c 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e  Func(sqlite3_con
3aa0d 74 65 78 74 20 2a 70 2c 20 69 6e 74 20 6e 41 72  text *p, int nAr
3aa0e 67 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  g, sqlite3_value
3aa0f 20 2a 2a 61 70 41 72 67 29 7b 0a 20 20 55 45 72   **apArg){.  UEr
3aa10 72 6f 72 43 6f 64 65 20 73 74 61 74 75 73 20 3d  rorCode status =
3aa11 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a 20   U_ZERO_ERROR;. 
3aa12 20 55 52 65 67 75 6c 61 72 45 78 70 72 65 73 73   URegularExpress
3aa13 69 6f 6e 20 2a 70 45 78 70 72 3b 0a 20 20 55 42  ion *pExpr;.  UB
3aa14 6f 6f 6c 20 72 65 73 3b 0a 20 20 63 6f 6e 73 74  ool res;.  const
3aa15 20 55 43 68 61 72 20 2a 7a 53 74 72 69 6e 67 20   UChar *zString 
3aa16 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
3aa17 74 65 78 74 31 36 28 61 70 41 72 67 5b 31 5d 29  text16(apArg[1])
3aa18 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6c  ;..  /* If the l
3aa19 65 66 74 20 68 61 6e 64 20 73 69 64 65 20 6f 66  eft hand side of
3aa1a 20 74 68 65 20 72 65 67 65 78 70 20 6f 70 65 72   the regexp oper
3aa1b 61 74 6f 72 20 69 73 20 4e 55 4c 4c 2c 20 0a 20  ator is NULL, . 
3aa1c 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 72 65 73   ** then the res
3aa1d 75 6c 74 20 69 73 20 61 6c 73 6f 20 4e 55 4c 4c  ult is also NULL
3aa1e 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 7a  . .  */.  if( !z
3aa1f 53 74 72 69 6e 67 20 29 7b 0a 20 20 20 20 72 65  String ){.    re
3aa20 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70 45 78  turn;.  }..  pEx
3aa21 70 72 20 3d 20 73 71 6c 69 74 65 33 5f 67 65 74  pr = sqlite3_get
3aa22 5f 61 75 78 64 61 74 61 28 70 2c 20 30 29 3b 0a  _auxdata(p, 0);.
3aa23 20 20 69 66 28 20 21 70 45 78 70 72 20 29 7b 0a    if( !pExpr ){.
3aa24 20 20 20 20 63 6f 6e 73 74 20 55 43 68 61 72 20      const UChar 
3aa25 2a 7a 50 61 74 74 65 72 6e 20 3d 20 73 71 6c 69  *zPattern = sqli
3aa26 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
3aa27 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20 20 20 20  (apArg[0]);.    
3aa28 69 66 28 20 21 7a 50 61 74 74 65 72 6e 20 29 7b  if( !zPattern ){
3aa29 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
3aa2a 20 20 20 7d 0a 20 20 20 20 70 45 78 70 72 20 3d     }.    pExpr =
3aa2b 20 75 72 65 67 65 78 5f 6f 70 65 6e 28 7a 50 61   uregex_open(zPa
3aa2c 74 74 65 72 6e 2c 20 2d 31 2c 20 30 2c 20 30 2c  ttern, -1, 0, 0,
3aa2d 20 26 73 74 61 74 75 73 29 3b 0a 0a 20 20 20 20   &status);..    
3aa2e 69 66 28 20 55 5f 53 55 43 43 45 53 53 28 73 74  if( U_SUCCESS(st
3aa2f 61 74 75 73 29 20 29 7b 0a 20 20 20 20 20 20 73  atus) ){.      s
3aa30 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61  qlite3_set_auxda
3aa31 74 61 28 70 2c 20 30 2c 20 70 45 78 70 72 2c 20  ta(p, 0, pExpr, 
3aa32 69 63 75 52 65 67 65 78 70 44 65 6c 65 74 65 29  icuRegexpDelete)
3aa33 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
3aa34 20 20 20 61 73 73 65 72 74 28 21 70 45 78 70 72     assert(!pExpr
3aa35 29 3b 0a 20 20 20 20 20 20 69 63 75 46 75 6e 63  );.      icuFunc
3aa36 74 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 72  tionError(p, "ur
3aa37 65 67 65 78 5f 6f 70 65 6e 22 2c 20 73 74 61 74  egex_open", stat
3aa38 75 73 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  us);.      retur
3aa39 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  n;.    }.  }..  
3aa3a 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68 65  /* Configure the
3aa3b 20 74 65 78 74 20 74 68 61 74 20 74 68 65 20 72   text that the r
3aa3c 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f  egular expressio
3aa3d 6e 20 6f 70 65 72 61 74 65 73 20 6f 6e 2e 20 2a  n operates on. *
3aa3e 2f 0a 20 20 75 72 65 67 65 78 5f 73 65 74 54 65  /.  uregex_setTe
3aa3f 78 74 28 70 45 78 70 72 2c 20 7a 53 74 72 69 6e  xt(pExpr, zStrin
3aa40 67 2c 20 2d 31 2c 20 26 73 74 61 74 75 73 29 3b  g, -1, &status);
3aa41 0a 20 20 69 66 28 20 21 55 5f 53 55 43 43 45 53  .  if( !U_SUCCES
3aa42 53 28 73 74 61 74 75 73 29 20 29 7b 0a 20 20 20  S(status) ){.   
3aa43 20 69 63 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f   icuFunctionErro
3aa44 72 28 70 2c 20 22 75 72 65 67 65 78 5f 73 65 74  r(p, "uregex_set
3aa45 54 65 78 74 22 2c 20 73 74 61 74 75 73 29 3b 0a  Text", status);.
3aa46 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
3aa47 0a 20 20 2f 2a 20 41 74 74 65 6d 70 74 20 74 68  .  /* Attempt th
3aa48 65 20 6d 61 74 63 68 20 2a 2f 0a 20 20 72 65 73  e match */.  res
3aa49 20 3d 20 75 72 65 67 65 78 5f 6d 61 74 63 68 65   = uregex_matche
3aa4a 73 28 70 45 78 70 72 2c 20 30 2c 20 26 73 74 61  s(pExpr, 0, &sta
3aa4b 74 75 73 29 3b 0a 20 20 69 66 28 20 21 55 5f 53  tus);.  if( !U_S
3aa4c 55 43 43 45 53 53 28 73 74 61 74 75 73 29 20 29  UCCESS(status) )
3aa4d 7b 0a 20 20 20 20 69 63 75 46 75 6e 63 74 69 6f  {.    icuFunctio
3aa4e 6e 45 72 72 6f 72 28 70 2c 20 22 75 72 65 67 65  nError(p, "urege
3aa4f 78 5f 6d 61 74 63 68 65 73 22 2c 20 73 74 61 74  x_matches", stat
3aa50 75 73 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  us);.    return;
3aa51 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 74  .  }..  /* Set t
3aa52 68 65 20 74 65 78 74 20 74 68 61 74 20 74 68 65  he text that the
3aa53 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 73 73   regular express
3aa54 69 6f 6e 20 6f 70 65 72 61 74 65 73 20 6f 6e 20  ion operates on 
3aa55 74 6f 20 61 20 4e 55 4c 4c 0a 20 20 2a 2a 20 70  to a NULL.  ** p
3aa56 6f 69 6e 74 65 72 2e 20 54 68 69 73 20 69 73 20  ointer. This is 
3aa57 6e 6f 74 20 72 65 61 6c 6c 79 20 6e 65 63 65 73  not really neces
3aa58 73 61 72 79 2c 20 62 75 74 20 69 74 20 69 73 20  sary, but it is 
3aa59 74 69 64 69 65 72 20 74 68 61 6e 20 0a 20 20 2a  tidier than .  *
3aa5a 2a 20 6c 65 61 76 69 6e 67 20 74 68 65 20 72 65  * leaving the re
3aa5b 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f 6e  gular expression
3aa5c 20 6f 62 6a 65 63 74 20 63 6f 6e 66 69 67 75 72   object configur
3aa5d 65 64 20 77 69 74 68 20 61 6e 20 69 6e 76 61 6c  ed with an inval
3aa5e 69 64 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 72 20  id.  ** pointer 
3aa5f 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74  after this funct
3aa60 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 20 20 2a  ion returns..  *
3aa61 2f 0a 20 20 75 72 65 67 65 78 5f 73 65 74 54 65  /.  uregex_setTe
3aa62 78 74 28 70 45 78 70 72 2c 20 30 2c 20 30 2c 20  xt(pExpr, 0, 0, 
3aa63 26 73 74 61 74 75 73 29 3b 0a 0a 20 20 2f 2a 20  &status);..  /* 
3aa64 52 65 74 75 72 6e 20 31 20 6f 72 20 30 2e 20 2a  Return 1 or 0. *
3aa65 2f 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  /.  sqlite3_resu
3aa66 6c 74 5f 69 6e 74 28 70 2c 20 72 65 73 20 3f 20  lt_int(p, res ? 
3aa67 31 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  1 : 0);.}../*.**
3aa68 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   Implementations
3aa69 20 6f 66 20 73 63 61 6c 61 72 20 66 75 6e 63 74   of scalar funct
3aa6a 69 6f 6e 73 20 66 6f 72 20 63 61 73 65 20 6d 61  ions for case ma
3aa6b 70 70 69 6e 67 20 2d 20 75 70 70 65 72 28 29 20  pping - upper() 
3aa6c 61 6e 64 20 0a 2a 2a 20 6c 6f 77 65 72 28 29 2e  and .** lower().
3aa6d 20 46 75 6e 63 74 69 6f 6e 20 75 70 70 65 72 28   Function upper(
3aa6e 29 20 63 6f 6e 76 65 72 74 73 20 69 74 73 20 69  ) converts its i
3aa6f 6e 70 75 74 20 74 6f 20 75 70 70 65 72 2d 63 61  nput to upper-ca
3aa70 73 65 20 28 41 42 43 29 2e 0a 2a 2a 20 46 75 6e  se (ABC)..** Fun
3aa71 63 74 69 6f 6e 20 6c 6f 77 65 72 28 29 20 63 6f  ction lower() co
3aa72 6e 76 65 72 74 73 20 74 6f 20 6c 6f 77 65 72 2d  nverts to lower-
3aa73 63 61 73 65 20 28 61 62 63 29 2e 0a 2a 2a 0a 2a  case (abc)..**.*
3aa74 2a 20 49 43 55 20 70 72 6f 76 69 64 65 73 20 74  * ICU provides t
3aa75 77 6f 20 74 79 70 65 73 20 6f 66 20 63 61 73 65  wo types of case
3aa76 20 6d 61 70 70 69 6e 67 2c 20 22 67 65 6e 65 72   mapping, "gener
3aa77 61 6c 22 20 63 61 73 65 20 6d 61 70 70 69 6e 67  al" case mapping
3aa78 20 61 6e 64 0a 2a 2a 20 22 6c 61 6e 67 75 61 67   and.** "languag
3aa79 65 20 73 70 65 63 69 66 69 63 22 2e 20 52 65 66  e specific". Ref
3aa7a 65 72 20 74 6f 20 49 43 55 20 64 6f 63 75 6d 65  er to ICU docume
3aa7b 6e 74 61 74 69 6f 6e 20 66 6f 72 20 74 68 65 20  ntation for the 
3aa7c 64 69 66 66 65 72 65 6e 63 65 73 0a 2a 2a 20 62  differences.** b
3aa7d 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f 2e 0a  etween the two..
3aa7e 2a 2a 0a 2a 2a 20 54 6f 20 75 74 69 6c 69 73 65  **.** To utilise
3aa7f 20 22 67 65 6e 65 72 61 6c 22 20 63 61 73 65 20   "general" case 
3aa80 6d 61 70 70 69 6e 67 2c 20 74 68 65 20 75 70 70  mapping, the upp
3aa81 65 72 28 29 20 6f 72 20 6c 6f 77 65 72 28 29 20  er() or lower() 
3aa82 73 63 61 6c 61 72 20 0a 2a 2a 20 66 75 6e 63 74  scalar .** funct
3aa83 69 6f 6e 73 20 61 72 65 20 69 6e 76 6f 6b 65 64  ions are invoked
3aa84 20 77 69 74 68 20 6f 6e 65 20 61 72 67 75 6d 65   with one argume
3aa85 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 75 70  nt:.**.**     up
3aa86 70 65 72 28 27 41 42 43 27 29 20 2d 3e 20 27 61  per('ABC') -> 'a
3aa87 62 63 27 0a 2a 2a 20 20 20 20 20 6c 6f 77 65 72  bc'.**     lower
3aa88 28 27 61 62 63 27 29 20 2d 3e 20 27 41 42 43 27  ('abc') -> 'ABC'
3aa89 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 63 63 65 73 73  .**.** To access
3aa8a 20 49 43 55 20 22 6c 61 6e 67 75 61 67 65 20 73   ICU "language s
3aa8b 70 65 63 69 66 69 63 22 20 63 61 73 65 20 6d 61  pecific" case ma
3aa8c 70 70 69 6e 67 2c 20 75 70 70 65 72 28 29 20 6f  pping, upper() o
3aa8d 72 20 6c 6f 77 65 72 28 29 0a 2a 2a 20 73 68 6f  r lower().** sho
3aa8e 75 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 20 77  uld be invoked w
3aa8f 69 74 68 20 74 77 6f 20 61 72 67 75 6d 65 6e 74  ith two argument
3aa90 73 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72  s. The second ar
3aa91 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 61  gument is the na
3aa92 6d 65 0a 2a 2a 20 6f 66 20 74 68 65 20 6c 6f 63  me.** of the loc
3aa93 61 6c 65 20 74 6f 20 75 73 65 2e 20 50 61 73 73  ale to use. Pass
3aa94 69 6e 67 20 61 6e 20 65 6d 70 74 79 20 73 74 72  ing an empty str
3aa95 69 6e 67 20 28 22 22 29 20 6f 72 20 53 51 4c 20  ing ("") or SQL 
3aa96 4e 55 4c 4c 20 76 61 6c 75 65 0a 2a 2a 20 61 73  NULL value.** as
3aa97 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
3aa98 6d 65 6e 74 20 69 73 20 74 68 65 20 73 61 6d 65  ment is the same
3aa99 20 61 73 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65   as invoking the
3aa9a 20 31 20 61 72 67 75 6d 65 6e 74 20 76 65 72 73   1 argument vers
3aa9b 69 6f 6e 0a 2a 2a 20 6f 66 20 75 70 70 65 72 28  ion.** of upper(
3aa9c 29 20 6f 72 20 6c 6f 77 65 72 28 29 2e 0a 2a 2a  ) or lower()..**
3aa9d 0a 2a 2a 20 20 20 20 20 6c 6f 77 65 72 28 27 49  .**     lower('I
3aa9e 27 2c 20 27 65 6e 5f 75 73 27 29 20 2d 3e 20 27  ', 'en_us') -> '
3aa9f 69 27 0a 2a 2a 20 20 20 20 20 6c 6f 77 65 72 28  i'.**     lower(
3aaa0 27 49 27 2c 20 27 74 72 5f 74 72 27 29 20 2d 3e  'I', 'tr_tr') ->
3aaa1 20 27 c4 b1 27 20 28 73 6d 61 6c 6c 20 64 6f 74   '..' (small dot
3aaa2 6c 65 73 73 20 69 29 0a 2a 2a 0a 2a 2a 20 68 74  less i).**.** ht
3aaa3 74 70 3a 2f 2f 77 77 77 2e 69 63 75 2d 70 72 6f  tp://www.icu-pro
3aaa4 6a 65 63 74 2e 6f 72 67 2f 75 73 65 72 67 75 69  ject.org/usergui
3aaa5 64 65 2f 70 6f 73 69 78 2e 68 74 6d 6c 23 63 61  de/posix.html#ca
3aaa6 73 65 5f 6d 61 70 70 69 6e 67 73 0a 2a 2f 0a 73  se_mappings.*/.s
3aaa7 74 61 74 69 63 20 76 6f 69 64 20 69 63 75 43 61  tatic void icuCa
3aaa8 73 65 46 75 6e 63 31 36 28 73 71 6c 69 74 65 33  seFunc16(sqlite3
3aaa9 5f 63 6f 6e 74 65 78 74 20 2a 70 2c 20 69 6e 74  _context *p, int
3aaaa 20 6e 41 72 67 2c 20 73 71 6c 69 74 65 33 5f 76   nArg, sqlite3_v
3aaab 61 6c 75 65 20 2a 2a 61 70 41 72 67 29 7b 0a 20  alue **apArg){. 
3aaac 20 63 6f 6e 73 74 20 55 43 68 61 72 20 2a 7a 49   const UChar *zI
3aaad 6e 70 75 74 3b 0a 20 20 55 43 68 61 72 20 2a 7a  nput;.  UChar *z
3aaae 4f 75 74 70 75 74 3b 0a 20 20 69 6e 74 20 6e 49  Output;.  int nI
3aaaf 6e 70 75 74 3b 0a 20 20 69 6e 74 20 6e 4f 75 74  nput;.  int nOut
3aab0 70 75 74 3b 0a 0a 20 20 55 45 72 72 6f 72 43 6f  put;..  UErrorCo
3aab1 64 65 20 73 74 61 74 75 73 20 3d 20 55 5f 5a 45  de status = U_ZE
3aab2 52 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 6f 6e 73  RO_ERROR;.  cons
3aab3 74 20 63 68 61 72 20 2a 7a 4c 6f 63 61 6c 65 20  t char *zLocale 
3aab4 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 6e  = 0;..  assert(n
3aab5 41 72 67 3d 3d 31 20 7c 7c 20 6e 41 72 67 3d 3d  Arg==1 || nArg==
3aab6 32 29 3b 0a 20 20 69 66 28 20 6e 41 72 67 3d 3d  2);.  if( nArg==
3aab7 32 20 29 7b 0a 20 20 20 20 7a 4c 6f 63 61 6c 65  2 ){.    zLocale
3aab8 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
3aab9 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
3aaba 65 78 74 28 61 70 41 72 67 5b 31 5d 29 3b 0a 20  ext(apArg[1]);. 
3aabb 20 7d 0a 0a 20 20 7a 49 6e 70 75 74 20 3d 20 73   }..  zInput = s
3aabc 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
3aabd 74 31 36 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20  t16(apArg[0]);. 
3aabe 20 69 66 28 20 21 7a 49 6e 70 75 74 20 29 7b 0a   if( !zInput ){.
3aabf 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
3aac0 20 20 6e 49 6e 70 75 74 20 3d 20 73 71 6c 69 74    nInput = sqlit
3aac1 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31 36  e3_value_bytes16
3aac2 28 61 70 41 72 67 5b 30 5d 29 3b 0a 0a 20 20 6e  (apArg[0]);..  n
3aac3 4f 75 74 70 75 74 20 3d 20 6e 49 6e 70 75 74 20  Output = nInput 
3aac4 2a 20 32 20 2b 20 32 3b 0a 20 20 7a 4f 75 74 70  * 2 + 2;.  zOutp
3aac5 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ut = sqlite3_mal
3aac6 6c 6f 63 28 6e 4f 75 74 70 75 74 29 3b 0a 20 20  loc(nOutput);.  
3aac7 69 66 28 20 21 7a 4f 75 74 70 75 74 20 29 7b 0a  if( !zOutput ){.
3aac8 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
3aac9 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 75  .  if( sqlite3_u
3aaca 73 65 72 5f 64 61 74 61 28 70 29 20 29 7b 0a 20  ser_data(p) ){. 
3aacb 20 20 20 75 5f 73 74 72 54 6f 55 70 70 65 72 28     u_strToUpper(
3aacc 7a 4f 75 74 70 75 74 2c 20 6e 4f 75 74 70 75 74  zOutput, nOutput
3aacd 2f 32 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e 70  /2, zInput, nInp
3aace 75 74 2f 32 2c 20 7a 4c 6f 63 61 6c 65 2c 20 26  ut/2, zLocale, &
3aacf 73 74 61 74 75 73 29 3b 0a 20 20 7d 65 6c 73 65  status);.  }else
3aad0 7b 0a 20 20 20 20 75 5f 73 74 72 54 6f 4c 6f 77  {.    u_strToLow
3aad1 65 72 28 7a 4f 75 74 70 75 74 2c 20 6e 4f 75 74  er(zOutput, nOut
3aad2 70 75 74 2f 32 2c 20 7a 49 6e 70 75 74 2c 20 6e  put/2, zInput, n
3aad3 49 6e 70 75 74 2f 32 2c 20 7a 4c 6f 63 61 6c 65  Input/2, zLocale
3aad4 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20 7d 0a  , &status);.  }.
3aad5 0a 20 20 69 66 28 20 21 55 5f 53 55 43 43 45 53  .  if( !U_SUCCES
3aad6 53 28 73 74 61 74 75 73 29 20 29 7b 0a 20 20 20  S(status) ){.   
3aad7 20 69 63 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f   icuFunctionErro
3aad8 72 28 70 2c 20 22 75 5f 73 74 72 54 6f 4c 6f 77  r(p, "u_strToLow
3aad9 65 72 28 29 2f 75 5f 73 74 72 54 6f 55 70 70 65  er()/u_strToUppe
3aada 72 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20  r", status);.   
3aadb 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
3aadc 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74  sqlite3_result_t
3aadd 65 78 74 31 36 28 70 2c 20 7a 4f 75 74 70 75 74  ext16(p, zOutput
3aade 2c 20 2d 31 2c 20 78 46 72 65 65 29 3b 0a 7d 0a  , -1, xFree);.}.
3aadf 0a 2f 2a 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e  ./*.** Collation
3aae0 20 73 65 71 75 65 6e 63 65 20 64 65 73 74 72 75   sequence destru
3aae1 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 2e 20 54  ctor function. T
3aae2 68 65 20 70 43 74 78 20 61 72 67 75 6d 65 6e 74  he pCtx argument
3aae3 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 61 20   points to.** a 
3aae4 55 43 6f 6c 6c 61 74 6f 72 20 73 74 72 75 63 74  UCollator struct
3aae5 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61  ure previously a
3aae6 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 75  llocated using u
3aae7 63 6f 6c 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 73  col_open()..*/.s
3aae8 74 61 74 69 63 20 76 6f 69 64 20 69 63 75 43 6f  tatic void icuCo
3aae9 6c 6c 61 74 69 6f 6e 44 65 6c 28 76 6f 69 64 20  llationDel(void 
3aaea 2a 70 43 74 78 29 7b 0a 20 20 55 43 6f 6c 6c 61  *pCtx){.  UColla
3aaeb 74 6f 72 20 2a 70 20 3d 20 28 55 43 6f 6c 6c 61  tor *p = (UColla
3aaec 74 6f 72 20 2a 29 70 43 74 78 3b 0a 20 20 75 63  tor *)pCtx;.  uc
3aaed 6f 6c 5f 63 6c 6f 73 65 28 70 29 3b 0a 7d 0a 0a  ol_close(p);.}..
3aaee 2f 2a 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20  /*.** Collation 
3aaef 73 65 71 75 65 6e 63 65 20 63 6f 6d 70 61 72 69  sequence compari
3aaf0 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2e 20 54 68  son function. Th
3aaf1 65 20 70 43 74 78 20 61 72 67 75 6d 65 6e 74 20  e pCtx argument 
3aaf2 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 61 20 55  points to.** a U
3aaf3 43 6f 6c 6c 61 74 6f 72 20 73 74 72 75 63 74 75  Collator structu
3aaf4 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c  re previously al
3aaf5 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 75 63  located using uc
3aaf6 6f 6c 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74  ol_open()..*/.st
3aaf7 61 74 69 63 20 69 6e 74 20 69 63 75 43 6f 6c 6c  atic int icuColl
3aaf8 61 74 69 6f 6e 43 6f 6c 6c 28 0a 20 20 76 6f 69  ationColl(.  voi
3aaf9 64 20 2a 70 43 74 78 2c 0a 20 20 69 6e 74 20 6e  d *pCtx,.  int n
3aafa 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 76 6f  Left,.  const vo
3aafb 69 64 20 2a 7a 4c 65 66 74 2c 0a 20 20 69 6e 74  id *zLeft,.  int
3aafc 20 6e 52 69 67 68 74 2c 0a 20 20 63 6f 6e 73 74   nRight,.  const
3aafd 20 76 6f 69 64 20 2a 7a 52 69 67 68 74 0a 29 7b   void *zRight.){
3aafe 0a 20 20 55 43 6f 6c 6c 61 74 69 6f 6e 52 65 73  .  UCollationRes
3aaff 75 6c 74 20 72 65 73 3b 0a 20 20 55 43 6f 6c 6c  ult res;.  UColl
3ab00 61 74 6f 72 20 2a 70 20 3d 20 28 55 43 6f 6c 6c  ator *p = (UColl
3ab01 61 74 6f 72 20 2a 29 70 43 74 78 3b 0a 20 20 72  ator *)pCtx;.  r
3ab02 65 73 20 3d 20 75 63 6f 6c 5f 73 74 72 63 6f 6c  es = ucol_strcol
3ab03 6c 28 70 2c 20 28 55 43 68 61 72 20 2a 29 7a 4c  l(p, (UChar *)zL
3ab04 65 66 74 2c 20 6e 4c 65 66 74 2f 32 2c 20 28 55  eft, nLeft/2, (U
3ab05 43 68 61 72 20 2a 29 7a 52 69 67 68 74 2c 20 6e  Char *)zRight, n
3ab06 52 69 67 68 74 2f 32 29 3b 0a 20 20 73 77 69 74  Right/2);.  swit
3ab07 63 68 28 20 72 65 73 20 29 7b 0a 20 20 20 20 63  ch( res ){.    c
3ab08 61 73 65 20 55 43 4f 4c 5f 4c 45 53 53 3a 20 20  ase UCOL_LESS:  
3ab09 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20    return -1;.   
3ab0a 20 63 61 73 65 20 55 43 4f 4c 5f 47 52 45 41 54   case UCOL_GREAT
3ab0b 45 52 3a 20 72 65 74 75 72 6e 20 2b 31 3b 0a 20  ER: return +1;. 
3ab0c 20 20 20 63 61 73 65 20 55 43 4f 4c 5f 45 51 55     case UCOL_EQU
3ab0d 41 4c 3a 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  AL:   return 0;.
3ab0e 20 20 7d 0a 20 20 61 73 73 65 72 74 28 21 22 55    }.  assert(!"U
3ab0f 6e 65 78 70 65 63 74 65 64 20 72 65 74 75 72 6e  nexpected return
3ab10 20 76 61 6c 75 65 20 66 72 6f 6d 20 75 63 6f 6c   value from ucol
3ab11 5f 73 74 72 63 6f 6c 6c 28 29 22 29 3b 0a 20 20  _strcoll()");.  
3ab12 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
3ab13 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
3ab14 6e 20 6f 66 20 74 68 65 20 73 63 61 6c 61 72 20  n of the scalar 
3ab15 66 75 6e 63 74 69 6f 6e 20 69 63 75 5f 6c 6f 61  function icu_loa
3ab16 64 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 2e 0a 2a  d_collation()..*
3ab17 2a 0a 2a 2a 20 54 68 69 73 20 73 63 61 6c 61 72  *.** This scalar
3ab18 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
3ab19 64 20 74 6f 20 61 64 64 20 49 43 55 20 63 6f 6c  d to add ICU col
3ab1a 6c 61 74 69 6f 6e 20 62 61 73 65 64 20 63 6f 6c  lation based col
3ab1b 6c 61 74 69 6f 6e 20 0a 2a 2a 20 74 79 70 65 73  lation .** types
3ab1c 20 74 6f 20 61 6e 20 53 51 4c 69 74 65 20 64 61   to an SQLite da
3ab1d 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3ab1e 6e 2e 20 49 74 20 69 73 20 69 6e 74 65 6e 64 65  n. It is intende
3ab1f 64 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 0a 2a  d to be called.*
3ab20 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  * as follows:.**
3ab21 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 69  .**     SELECT i
3ab22 63 75 5f 6c 6f 61 64 5f 63 6f 6c 6c 61 74 69 6f  cu_load_collatio
3ab23 6e 28 3c 6c 6f 63 61 6c 65 3e 2c 20 3c 63 6f 6c  n(<locale>, <col
3ab24 6c 61 74 69 6f 6e 2d 6e 61 6d 65 3e 29 3b 0a 2a  lation-name>);.*
3ab25 2a 0a 2a 2a 20 57 68 65 72 65 20 3c 6c 6f 63 61  *.** Where <loca
3ab26 6c 65 3e 20 69 73 20 61 20 73 74 72 69 6e 67 20  le> is a string 
3ab27 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6e 20 49 43  containing an IC
3ab28 55 20 6c 6f 63 61 6c 65 20 69 64 65 6e 74 69 66  U locale identif
3ab29 69 65 72 20 28 69 2e 65 2e 0a 2a 2a 20 22 65 6e  ier (i.e..** "en
3ab2a 5f 41 55 22 2c 20 22 74 72 5f 54 52 22 20 65 74  _AU", "tr_TR" et
3ab2b 63 2e 29 20 61 6e 64 20 3c 63 6f 6c 6c 61 74 69  c.) and <collati
3ab2c 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20 74 68 65 20  on-name> is the 
3ab2d 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63  name of the.** c
3ab2e 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
3ab2f 65 20 74 6f 20 63 72 65 61 74 65 2e 0a 2a 2f 0a  e to create..*/.
3ab30 73 74 61 74 69 63 20 76 6f 69 64 20 69 63 75 4c  static void icuL
3ab31 6f 61 64 43 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20  oadCollation(.  
3ab32 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
3ab33 2a 70 2c 20 0a 20 20 69 6e 74 20 6e 41 72 67 2c  *p, .  int nArg,
3ab34 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   .  sqlite3_valu
3ab35 65 20 2a 2a 61 70 41 72 67 0a 29 7b 0a 20 20 73  e **apArg.){.  s
3ab36 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 28 73 71  qlite3 *db = (sq
3ab37 6c 69 74 65 33 20 2a 29 73 71 6c 69 74 65 33 5f  lite3 *)sqlite3_
3ab38 75 73 65 72 5f 64 61 74 61 28 70 29 3b 0a 20 20  user_data(p);.  
3ab39 55 45 72 72 6f 72 43 6f 64 65 20 73 74 61 74 75  UErrorCode statu
3ab3a 73 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52  s = U_ZERO_ERROR
3ab3b 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
3ab3c 7a 4c 6f 63 61 6c 65 3b 20 20 20 20 20 20 2f 2a  zLocale;      /*
3ab3d 20 4c 6f 63 61 6c 65 20 69 64 65 6e 74 69 66 69   Locale identifi
3ab3e 65 72 20 2d 20 28 65 67 2e 20 22 6a 70 5f 4a 50  er - (eg. "jp_JP
3ab3f 22 29 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ") */.  const ch
3ab40 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20  ar *zName;      
3ab41 20 20 2f 2a 20 53 51 4c 20 43 6f 6c 6c 61 74 69    /* SQL Collati
3ab42 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65  on sequence name
3ab43 20 28 65 67 2e 20 22 6a 61 70 61 6e 65 73 65 22   (eg. "japanese"
3ab44 29 20 2a 2f 0a 20 20 55 43 6f 6c 6c 61 74 6f 72  ) */.  UCollator
3ab45 20 2a 70 55 43 6f 6c 6c 61 74 6f 72 3b 20 20 20   *pUCollator;   
3ab46 20 2f 2a 20 49 43 55 20 6c 69 62 72 61 72 79 20   /* ICU library 
3ab47 63 6f 6c 6c 61 74 69 6f 6e 20 6f 62 6a 65 63 74  collation object
3ab48 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20   */.  int rc;   
3ab49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ab4a 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 66  /* Return code f
3ab4b 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 72 65 61  rom sqlite3_crea
3ab4c 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 78 28 29  te_collation_x()
3ab4d 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 6e 41   */..  assert(nA
3ab4e 72 67 3d 3d 32 29 3b 0a 20 20 7a 4c 6f 63 61 6c  rg==2);.  zLocal
3ab4f 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  e = (const char 
3ab50 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
3ab51 74 65 78 74 28 61 70 41 72 67 5b 30 5d 29 3b 0a  text(apArg[0]);.
3ab52 20 20 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74    zName = (const
3ab53 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f   char *)sqlite3_
3ab54 76 61 6c 75 65 5f 74 65 78 74 28 61 70 41 72 67  value_text(apArg
3ab55 5b 31 5d 29 3b 0a 0a 20 20 69 66 28 20 21 7a 4c  [1]);..  if( !zL
3ab56 6f 63 61 6c 65 20 7c 7c 20 21 7a 4e 61 6d 65 20  ocale || !zName 
3ab57 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  ){.    return;. 
3ab58 20 7d 0a 0a 20 20 70 55 43 6f 6c 6c 61 74 6f 72   }..  pUCollator
3ab59 20 3d 20 75 63 6f 6c 5f 6f 70 65 6e 28 7a 4c 6f   = ucol_open(zLo
3ab5a 63 61 6c 65 2c 20 26 73 74 61 74 75 73 29 3b 0a  cale, &status);.
3ab5b 20 20 69 66 28 20 21 55 5f 53 55 43 43 45 53 53    if( !U_SUCCESS
3ab5c 28 73 74 61 74 75 73 29 20 29 7b 0a 20 20 20 20  (status) ){.    
3ab5d 69 63 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f 72  icuFunctionError
3ab5e 28 70 2c 20 22 75 63 6f 6c 5f 6f 70 65 6e 22 2c  (p, "ucol_open",
3ab5f 20 73 74 61 74 75 73 29 3b 0a 20 20 20 20 72 65   status);.    re
3ab60 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65  turn;.  }.  asse
3ab61 72 74 28 70 29 3b 0a 0a 20 20 72 63 20 3d 20 73  rt(p);..  rc = s
3ab62 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
3ab63 6c 6c 61 74 69 6f 6e 5f 76 32 28 64 62 2c 20 7a  llation_v2(db, z
3ab64 4e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 55 54 46  Name, SQLITE_UTF
3ab65 31 36 2c 20 28 76 6f 69 64 20 2a 29 70 55 43 6f  16, (void *)pUCo
3ab66 6c 6c 61 74 6f 72 2c 20 0a 20 20 20 20 20 20 69  llator, .      i
3ab67 63 75 43 6f 6c 6c 61 74 69 6f 6e 43 6f 6c 6c 2c  cuCollationColl,
3ab68 20 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 44 65 6c   icuCollationDel
3ab69 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  .  );.  if( rc!=
3ab6a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
3ab6b 20 75 63 6f 6c 5f 63 6c 6f 73 65 28 70 55 43 6f   ucol_close(pUCo
3ab6c 6c 6c 61 74 6f 72 29 3b 0a 20 20 20 20 73 71 6c  llator);.    sql
3ab6d 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
3ab6e 72 28 70 2c 20 22 45 72 72 6f 72 20 72 65 67 69  r(p, "Error regi
3ab6f 73 74 65 72 69 6e 67 20 63 6f 6c 6c 61 74 69 6f  stering collatio
3ab70 6e 20 66 75 6e 63 74 69 6f 6e 22 2c 20 2d 31 29  n function", -1)
3ab71 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
3ab72 65 67 69 73 74 65 72 20 74 68 65 20 49 43 55 20  egister the ICU 
3ab73 65 78 74 65 6e 73 69 6f 6e 20 66 75 6e 63 74 69  extension functi
3ab74 6f 6e 73 20 77 69 74 68 20 64 61 74 61 62 61 73  ons with databas
3ab75 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  e db..*/.SQLITE_
3ab76 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
3ab77 74 65 33 49 63 75 49 6e 69 74 28 73 71 6c 69 74  te3IcuInit(sqlit
3ab78 65 33 20 2a 64 62 29 7b 0a 20 20 73 74 72 75 63  e3 *db){.  struc
3ab79 74 20 49 63 75 53 63 61 6c 61 72 20 7b 0a 20 20  t IcuScalar {.  
3ab7a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
3ab7b 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ame;            
3ab7c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
3ab7d 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a  unction name */.
3ab7e 20 20 20 20 69 6e 74 20 6e 41 72 67 3b 20 20 20      int nArg;   
3ab7f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ab80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3ab81 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   Number of argum
3ab82 65 6e 74 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ents */.    int 
3ab83 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  enc;            
3ab84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ab85 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6d 61 6c        /* Optimal
3ab86 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a   text encoding *
3ab87 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 43 6f 6e  /.    void *pCon
3ab88 74 65 78 74 3b 20 20 20 20 20 20 20 20 20 20 20  text;           
3ab89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ab8a 2f 2a 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f  /* sqlite3_user_
3ab8b 64 61 74 61 28 29 20 63 6f 6e 74 65 78 74 20 2a  data() context *
3ab8c 2f 0a 20 20 20 20 76 6f 69 64 20 28 2a 78 46 75  /.    void (*xFu
3ab8d 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nc)(sqlite3_cont
3ab8e 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
3ab8f 5f 76 61 6c 75 65 2a 2a 29 3b 0a 20 20 7d 20 73  _value**);.  } s
3ab90 63 61 6c 61 72 73 5b 5d 20 3d 20 7b 0a 20 20 20  calars[] = {.   
3ab91 20 7b 22 72 65 67 65 78 70 22 2c 2d 31 2c 20 53   {"regexp",-1, S
3ab92 51 4c 49 54 45 5f 41 4e 59 2c 20 20 20 20 20 20  QLITE_ANY,      
3ab93 20 20 20 20 30 2c 20 69 63 75 52 65 67 65 78 70      0, icuRegexp
3ab94 46 75 6e 63 7d 2c 0a 0a 20 20 20 20 7b 22 6c 6f  Func},..    {"lo
3ab95 77 65 72 22 2c 20 20 31 2c 20 53 51 4c 49 54 45  wer",  1, SQLITE
3ab96 5f 55 54 46 31 36 2c 20 20 20 20 20 20 20 20 30  _UTF16,        0
3ab97 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d  , icuCaseFunc16}
3ab98 2c 0a 20 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20  ,.    {"lower", 
3ab99 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36   2, SQLITE_UTF16
3ab9a 2c 20 20 20 20 20 20 20 20 30 2c 20 69 63 75 43  ,        0, icuC
3ab9b 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20  aseFunc16},.    
3ab9c 7b 22 75 70 70 65 72 22 2c 20 20 31 2c 20 53 51  {"upper",  1, SQ
3ab9d 4c 49 54 45 5f 55 54 46 31 36 2c 20 28 76 6f 69  LITE_UTF16, (voi
3ab9e 64 2a 29 31 2c 20 69 63 75 43 61 73 65 46 75 6e  d*)1, icuCaseFun
3ab9f 63 31 36 7d 2c 0a 20 20 20 20 7b 22 75 70 70 65  c16},.    {"uppe
3aba0 72 22 2c 20 20 32 2c 20 53 51 4c 49 54 45 5f 55  r",  2, SQLITE_U
3aba1 54 46 31 36 2c 20 28 76 6f 69 64 2a 29 31 2c 20  TF16, (void*)1, 
3aba2 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a  icuCaseFunc16},.
3aba3 0a 20 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20 20  .    {"lower",  
3aba4 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  1, SQLITE_UTF8, 
3aba5 20 20 20 20 20 20 20 20 30 2c 20 69 63 75 43 61          0, icuCa
3aba6 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20 7b  seFunc16},.    {
3aba7 22 6c 6f 77 65 72 22 2c 20 20 32 2c 20 53 51 4c  "lower",  2, SQL
3aba8 49 54 45 5f 55 54 46 38 2c 20 20 20 20 20 20 20  ITE_UTF8,       
3aba9 20 20 30 2c 20 69 63 75 43 61 73 65 46 75 6e 63    0, icuCaseFunc
3abaa 31 36 7d 2c 0a 20 20 20 20 7b 22 75 70 70 65 72  16},.    {"upper
3abab 22 2c 20 20 31 2c 20 53 51 4c 49 54 45 5f 55 54  ",  1, SQLITE_UT
3abac 46 38 2c 20 20 28 76 6f 69 64 2a 29 31 2c 20 69  F8,  (void*)1, i
3abad 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20  cuCaseFunc16},. 
3abae 20 20 20 7b 22 75 70 70 65 72 22 2c 20 20 32 2c     {"upper",  2,
3abaf 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 28   SQLITE_UTF8,  (
3abb0 76 6f 69 64 2a 29 31 2c 20 69 63 75 43 61 73 65  void*)1, icuCase
3abb1 46 75 6e 63 31 36 7d 2c 0a 0a 20 20 20 20 7b 22  Func16},..    {"
3abb2 6c 69 6b 65 22 2c 20 20 20 32 2c 20 53 51 4c 49  like",   2, SQLI
3abb3 54 45 5f 55 54 46 38 2c 20 20 20 20 20 20 20 20  TE_UTF8,        
3abb4 20 30 2c 20 69 63 75 4c 69 6b 65 46 75 6e 63 7d   0, icuLikeFunc}
3abb5 2c 0a 20 20 20 20 7b 22 6c 69 6b 65 22 2c 20 20  ,.    {"like",  
3abb6 20 33 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   3, SQLITE_UTF8,
3abb7 20 20 20 20 20 20 20 20 20 30 2c 20 69 63 75 4c           0, icuL
3abb8 69 6b 65 46 75 6e 63 7d 2c 0a 0a 20 20 20 20 7b  ikeFunc},..    {
3abb9 22 69 63 75 5f 6c 6f 61 64 5f 63 6f 6c 6c 61 74  "icu_load_collat
3abba 69 6f 6e 22 2c 20 20 32 2c 20 53 51 4c 49 54 45  ion",  2, SQLITE
3abbb 5f 55 54 46 38 2c 20 28 76 6f 69 64 2a 29 64 62  _UTF8, (void*)db
3abbc 2c 20 69 63 75 4c 6f 61 64 43 6f 6c 6c 61 74 69  , icuLoadCollati
3abbd 6f 6e 7d 2c 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74  on},.  };..  int
3abbe 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
3abbf 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 66 6f 72  .  int i;..  for
3abc0 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45  (i=0; rc==SQLITE
3abc1 5f 4f 4b 20 26 26 20 69 3c 28 73 69 7a 65 6f 66  _OK && i<(sizeof
3abc2 28 73 63 61 6c 61 72 73 29 2f 73 69 7a 65 6f 66  (scalars)/sizeof
3abc3 28 73 74 72 75 63 74 20 49 63 75 53 63 61 6c 61  (struct IcuScala
3abc4 72 29 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  r)); i++){.    s
3abc5 74 72 75 63 74 20 49 63 75 53 63 61 6c 61 72 20  truct IcuScalar 
3abc6 2a 70 20 3d 20 26 73 63 61 6c 61 72 73 5b 69 5d  *p = &scalars[i]
3abc7 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
3abc8 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
3abc9 6f 6e 28 0a 20 20 20 20 20 20 20 20 64 62 2c 20  on(.        db, 
3abca 70 2d 3e 7a 4e 61 6d 65 2c 20 70 2d 3e 6e 41 72  p->zName, p->nAr
3abcb 67 2c 20 70 2d 3e 65 6e 63 2c 20 70 2d 3e 70 43  g, p->enc, p->pC
3abcc 6f 6e 74 65 78 74 2c 20 70 2d 3e 78 46 75 6e 63  ontext, p->xFunc
3abcd 2c 20 30 2c 20 30 0a 20 20 20 20 29 3b 0a 20 20  , 0, 0.    );.  
3abce 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
3abcf 7d 0a 0a 23 69 66 20 21 53 51 4c 49 54 45 5f 43  }..#if !SQLITE_C
3abd0 4f 52 45 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ORE.SQLITE_API i
3abd1 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e  nt sqlite3_exten
3abd2 73 69 6f 6e 5f 69 6e 69 74 28 0a 20 20 73 71 6c  sion_init(.  sql
3abd3 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 63 68 61  ite3 *db, .  cha
3abd4 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c 0a 20 20  r **pzErrMsg,.  
3abd5 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70  const sqlite3_ap
3abd6 69 5f 72 6f 75 74 69 6e 65 73 20 2a 70 41 70 69  i_routines *pApi
3abd7 0a 29 7b 0a 20 20 53 51 4c 49 54 45 5f 45 58 54  .){.  SQLITE_EXT
3abd8 45 4e 53 49 4f 4e 5f 49 4e 49 54 32 28 70 41 70  ENSION_INIT2(pAp
3abd9 69 29 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  i).  return sqli
3abda 74 65 33 49 63 75 49 6e 69 74 28 64 62 29 3b 0a  te3IcuInit(db);.
3abdb 7d 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66  }.#endif..#endif
3abdc 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
3abdd 2a 20 45 6e 64 20 6f 66 20 69 63 75 2e 63 20 2a  * End of icu.c *
3abde 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3abdf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3abe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3abe1 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
3abe2 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73  * Begin file fts
3abe3 33 5f 69 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  3_icu.c ********
3abe4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3abe5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3abe6 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4a 75 6e  /./*.** 2007 Jun
3abe7 65 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  e 22.**.** The a
3abe8 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
3abe9 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
3abea 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
3abeb 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
3abec 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
3abed 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
3abee 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
3abef 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
3abf0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
3abf1 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
3abf2 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
3abf3 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
3abf4 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
3abf5 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
3abf6 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
3abf7 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
3abf8 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
3abf9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3abfa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3abfb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3abfc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3abfd 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ***.** This file
3abfe 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 74 6f   implements a to
3abff 6b 65 6e 69 7a 65 72 20 66 6f 72 20 66 74 73 33  kenizer for fts3
3ac00 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 49 43   based on the IC
3ac01 55 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 20 0a 2a  U library..** .*
3ac02 2a 20 24 49 64 3a 20 66 74 73 33 5f 69 63 75 2e  * $Id: fts3_icu.
3ac03 63 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 39 2f  c,v 1.3 2008/09/
3ac04 30 31 20 31 38 3a 33 34 3a 32 30 20 64 61 6e 69  01 18:34:20 dani
3ac05 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f  elk1977 Exp $.*/
3ac06 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  ..#if !defined(S
3ac07 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64  QLITE_CORE) || d
3ac08 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
3ac09 41 42 4c 45 5f 46 54 53 33 29 0a 23 69 66 64 65  ABLE_FTS3).#ifde
3ac0a 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
3ac0b 49 43 55 0a 0a 0a 23 69 6e 63 6c 75 64 65 20 3c  ICU...#include <
3ac0c 75 6e 69 63 6f 64 65 2f 75 62 72 6b 2e 68 3e 0a  unicode/ubrk.h>.
3ac0d 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64  #include <unicod
3ac0e 65 2f 75 74 66 31 36 2e 68 3e 0a 0a 74 79 70 65  e/utf16.h>..type
3ac0f 64 65 66 20 73 74 72 75 63 74 20 49 63 75 54 6f  def struct IcuTo
3ac10 6b 65 6e 69 7a 65 72 20 49 63 75 54 6f 6b 65 6e  kenizer IcuToken
3ac11 69 7a 65 72 3b 0a 74 79 70 65 64 65 66 20 73 74  izer;.typedef st
3ac12 72 75 63 74 20 49 63 75 43 75 72 73 6f 72 20 49  ruct IcuCursor I
3ac13 63 75 43 75 72 73 6f 72 3b 0a 0a 73 74 72 75 63  cuCursor;..struc
3ac14 74 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 7b  t IcuTokenizer {
3ac15 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
3ac16 69 7a 65 72 20 62 61 73 65 3b 0a 20 20 63 68 61  izer base;.  cha
3ac17 72 20 2a 7a 4c 6f 63 61 6c 65 3b 0a 7d 3b 0a 0a  r *zLocale;.};..
3ac18 73 74 72 75 63 74 20 49 63 75 43 75 72 73 6f 72  struct IcuCursor
3ac19 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b   {.  sqlite3_tok
3ac1a 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 62 61  enizer_cursor ba
3ac1b 73 65 3b 0a 0a 20 20 55 42 72 65 61 6b 49 74 65  se;..  UBreakIte
3ac1c 72 61 74 6f 72 20 2a 70 49 74 65 72 3b 20 20 20  rator *pIter;   
3ac1d 20 20 20 2f 2a 20 49 43 55 20 62 72 65 61 6b 2d     /* ICU break-
3ac1e 69 74 65 72 61 74 6f 72 20 6f 62 6a 65 63 74 20  iterator object 
3ac1f 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 72 3b 20  */.  int nChar; 
3ac20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ac21 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 55 43   /* Number of UC
3ac22 68 61 72 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20  har elements in 
3ac23 70 49 6e 70 75 74 20 2a 2f 0a 20 20 55 43 68 61  pInput */.  UCha
3ac24 72 20 2a 61 43 68 61 72 3b 20 20 20 20 20 20 20  r *aChar;       
3ac25 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20          /* Copy 
3ac26 6f 66 20 69 6e 70 75 74 20 75 73 69 6e 67 20 75  of input using u
3ac27 74 66 2d 31 36 20 65 6e 63 6f 64 69 6e 67 20 2a  tf-16 encoding *
3ac28 2f 0a 20 20 69 6e 74 20 2a 61 4f 66 66 73 65 74  /.  int *aOffset
3ac29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3ac2a 2f 2a 20 4f 66 66 73 65 74 73 20 6f 66 20 65 61  /* Offsets of ea
3ac2b 63 68 20 63 68 61 72 61 63 74 65 72 20 69 6e 20  ch character in 
3ac2c 75 74 66 2d 38 20 69 6e 70 75 74 20 2a 2f 0a 0a  utf-8 input */..
3ac2d 20 20 69 6e 74 20 6e 42 75 66 66 65 72 3b 0a 20    int nBuffer;. 
3ac2e 20 63 68 61 72 20 2a 7a 42 75 66 66 65 72 3b 0a   char *zBuffer;.
3ac2f 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e 3b 0a 7d  .  int iToken;.}
3ac30 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  ;../*.** Create 
3ac31 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65 72 20  a new tokenizer 
3ac32 69 6e 73 74 61 6e 63 65 2e 0a 2a 2f 0a 73 74 61  instance..*/.sta
3ac33 74 69 63 20 69 6e 74 20 69 63 75 43 72 65 61 74  tic int icuCreat
3ac34 65 28 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 20  e(.  int argc,  
3ac35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ac36 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
3ac37 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
3ac38 6e 20 61 72 67 76 5b 5d 20 2a 2f 0a 20 20 63 6f  n argv[] */.  co
3ac39 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74  nst char * const
3ac3a 20 2a 61 72 67 76 2c 20 20 20 20 20 20 20 20 20   *argv,         
3ac3b 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72 20     /* Tokenizer 
3ac3c 63 72 65 61 74 69 6f 6e 20 61 72 67 75 6d 65 6e  creation argumen
3ac3d 74 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  ts */.  sqlite3_
3ac3e 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54 6f  tokenizer **ppTo
3ac3f 6b 65 6e 69 7a 65 72 20 20 20 20 20 20 2f 2a 20  kenizer      /* 
3ac40 4f 55 54 3a 20 43 72 65 61 74 65 64 20 74 6f 6b  OUT: Created tok
3ac41 65 6e 69 7a 65 72 20 2a 2f 0a 29 7b 0a 20 20 49  enizer */.){.  I
3ac42 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 70 3b 0a  cuTokenizer *p;.
3ac43 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 0a 20 20    int n = 0;..  
3ac44 69 66 28 20 61 72 67 63 3e 30 20 29 7b 0a 20 20  if( argc>0 ){.  
3ac45 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28 61 72 67    n = strlen(arg
3ac46 76 5b 30 5d 29 2b 31 3b 0a 20 20 7d 0a 20 20 70  v[0])+1;.  }.  p
3ac47 20 3d 20 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72   = (IcuTokenizer
3ac48 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   *)sqlite3_mallo
3ac49 63 28 73 69 7a 65 6f 66 28 49 63 75 54 6f 6b 65  c(sizeof(IcuToke
3ac4a 6e 69 7a 65 72 29 2b 6e 29 3b 0a 20 20 69 66 28  nizer)+n);.  if(
3ac4b 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75 72   !p ){.    retur
3ac4c 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
3ac4d 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20    }.  memset(p, 
3ac4e 30 2c 20 73 69 7a 65 6f 66 28 49 63 75 54 6f 6b  0, sizeof(IcuTok
3ac4f 65 6e 69 7a 65 72 29 29 3b 0a 0a 20 20 69 66 28  enizer));..  if(
3ac50 20 6e 20 29 7b 0a 20 20 20 20 70 2d 3e 7a 4c 6f   n ){.    p->zLo
3ac51 63 61 6c 65 20 3d 20 28 63 68 61 72 20 2a 29 26  cale = (char *)&
3ac52 70 5b 31 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79  p[1];.    memcpy
3ac53 28 70 2d 3e 7a 4c 6f 63 61 6c 65 2c 20 61 72 67  (p->zLocale, arg
3ac54 76 5b 30 5d 2c 20 6e 29 3b 0a 20 20 7d 0a 0a 20  v[0], n);.  }.. 
3ac55 20 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20   *ppTokenizer = 
3ac56 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a  (sqlite3_tokeniz
3ac57 65 72 20 2a 29 70 3b 0a 0a 20 20 72 65 74 75 72  er *)p;..  retur
3ac58 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
3ac59 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20  /*.** Destroy a 
3ac5a 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61  tokenizer.*/.sta
3ac5b 74 69 63 20 69 6e 74 20 69 63 75 44 65 73 74 72  tic int icuDestr
3ac5c 6f 79 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  oy(sqlite3_token
3ac5d 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72  izer *pTokenizer
3ac5e 29 7b 0a 20 20 49 63 75 54 6f 6b 65 6e 69 7a 65  ){.  IcuTokenize
3ac5f 72 20 2a 70 20 3d 20 28 49 63 75 54 6f 6b 65 6e  r *p = (IcuToken
3ac60 69 7a 65 72 20 2a 29 70 54 6f 6b 65 6e 69 7a 65  izer *)pTokenize
3ac61 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  r;.  sqlite3_fre
3ac62 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  e(p);.  return S
3ac63 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
3ac64 2a 2a 20 50 72 65 70 61 72 65 20 74 6f 20 62 65  ** Prepare to be
3ac65 67 69 6e 20 74 6f 6b 65 6e 69 7a 69 6e 67 20 61  gin tokenizing a
3ac66 20 70 61 72 74 69 63 75 6c 61 72 20 73 74 72 69   particular stri
3ac67 6e 67 2e 20 20 54 68 65 20 69 6e 70 75 74 0a 2a  ng.  The input.*
3ac68 2a 20 73 74 72 69 6e 67 20 74 6f 20 62 65 20 74  * string to be t
3ac69 6f 6b 65 6e 69 7a 65 64 20 69 73 20 70 49 6e 70  okenized is pInp
3ac6a 75 74 5b 30 2e 2e 6e 42 79 74 65 73 2d 31 5d 2e  ut[0..nBytes-1].
3ac6b 20 20 41 20 63 75 72 73 6f 72 0a 2a 2a 20 75 73    A cursor.** us
3ac6c 65 64 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74 61  ed to incrementa
3ac6d 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65 20 74 68 69  lly tokenize thi
3ac6e 73 20 73 74 72 69 6e 67 20 69 73 20 72 65 74 75  s string is retu
3ac6f 72 6e 65 64 20 69 6e 20 0a 2a 2a 20 2a 70 70 43  rned in .** *ppC
3ac70 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  ursor..*/.static
3ac71 20 69 6e 74 20 69 63 75 4f 70 65 6e 28 0a 20 20   int icuOpen(.  
3ac72 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
3ac73 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20  r *pTokenizer,  
3ac74 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f         /* The to
3ac75 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 63 6f 6e  kenizer */.  con
3ac76 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74 2c  st char *zInput,
3ac77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ac78 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73 74 72      /* Input str
3ac79 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e  ing */.  int nIn
3ac7a 70 75 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  put,            
3ac7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ac7c 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 49 6e  /* Length of zIn
3ac7d 70 75 74 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a  put in bytes */.
3ac7e 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
3ac7f 7a 65 72 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43  zer_cursor **ppC
3ac80 75 72 73 6f 72 20 20 20 20 2f 2a 20 4f 55 54 3a  ursor    /* OUT:
3ac81 20 54 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75   Tokenization cu
3ac82 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 49 63 75  rsor */.){.  Icu
3ac83 54 6f 6b 65 6e 69 7a 65 72 20 2a 70 20 3d 20 28  Tokenizer *p = (
3ac84 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 29 70  IcuTokenizer *)p
3ac85 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 49 63 75  Tokenizer;.  Icu
3ac86 43 75 72 73 6f 72 20 2a 70 43 73 72 3b 0a 0a 20  Cursor *pCsr;.. 
3ac87 20 63 6f 6e 73 74 20 69 6e 74 33 32 5f 74 20 6f   const int32_t o
3ac88 70 74 20 3d 20 55 5f 46 4f 4c 44 5f 43 41 53 45  pt = U_FOLD_CASE
3ac89 5f 44 45 46 41 55 4c 54 3b 0a 20 20 55 45 72 72  _DEFAULT;.  UErr
3ac8a 6f 72 43 6f 64 65 20 73 74 61 74 75 73 20 3d 20  orCode status = 
3ac8b 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a 20 20  U_ZERO_ERROR;.  
3ac8c 69 6e 74 20 6e 43 68 61 72 3b 0a 0a 20 20 55 43  int nChar;..  UC
3ac8d 68 61 72 33 32 20 63 3b 0a 20 20 69 6e 74 20 69  har32 c;.  int i
3ac8e 49 6e 70 75 74 20 3d 20 30 3b 0a 20 20 69 6e 74  Input = 0;.  int
3ac8f 20 69 4f 75 74 20 3d 20 30 3b 0a 0a 20 20 2a 70   iOut = 0;..  *p
3ac90 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 0a 20 20  pCursor = 0;..  
3ac91 69 66 28 20 6e 49 6e 70 75 74 3c 30 20 29 7b 0a  if( nInput<0 ){.
3ac92 20 20 20 20 6e 49 6e 70 75 74 20 3d 20 73 74 72      nInput = str
3ac93 6c 65 6e 28 7a 49 6e 70 75 74 29 3b 0a 20 20 7d  len(zInput);.  }
3ac94 0a 20 20 6e 43 68 61 72 20 3d 20 6e 49 6e 70 75  .  nChar = nInpu
3ac95 74 2b 31 3b 0a 20 20 70 43 73 72 20 3d 20 28 49  t+1;.  pCsr = (I
3ac96 63 75 43 75 72 73 6f 72 20 2a 29 73 71 6c 69 74  cuCursor *)sqlit
3ac97 65 33 5f 6d 61 6c 6c 6f 63 28 0a 20 20 20 20 20  e3_malloc(.     
3ac98 20 73 69 7a 65 6f 66 28 49 63 75 43 75 72 73 6f   sizeof(IcuCurso
3ac99 72 29 20 2b 20 20 20 20 20 20 20 20 20 20 20 20  r) +            
3ac9a 20 20 20 20 2f 2a 20 49 63 75 43 75 72 73 6f 72      /* IcuCursor
3ac9b 20 2a 2f 0a 20 20 20 20 20 20 6e 43 68 61 72 20   */.      nChar 
3ac9c 2a 20 73 69 7a 65 6f 66 28 55 43 68 61 72 29 20  * sizeof(UChar) 
3ac9d 2b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  +            /* 
3ac9e 49 63 75 43 75 72 73 6f 72 2e 61 43 68 61 72 5b  IcuCursor.aChar[
3ac9f 5d 20 2a 2f 0a 20 20 20 20 20 20 28 6e 43 68 61  ] */.      (nCha
3aca0 72 2b 31 29 20 2a 20 73 69 7a 65 6f 66 28 69 6e  r+1) * sizeof(in
3aca1 74 29 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t)            /*
3aca2 20 49 63 75 43 75 72 73 6f 72 2e 61 4f 66 66 73   IcuCursor.aOffs
3aca3 65 74 5b 5d 20 2a 2f 0a 20 20 29 3b 0a 20 20 69  et[] */.  );.  i
3aca4 66 28 20 21 70 43 73 72 20 29 7b 0a 20 20 20 20  f( !pCsr ){.    
3aca5 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
3aca6 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65  MEM;.  }.  memse
3aca7 74 28 70 43 73 72 2c 20 30 2c 20 73 69 7a 65 6f  t(pCsr, 0, sizeo
3aca8 66 28 49 63 75 43 75 72 73 6f 72 29 29 3b 0a 20  f(IcuCursor));. 
3aca9 20 70 43 73 72 2d 3e 61 43 68 61 72 20 3d 20 28   pCsr->aChar = (
3acaa 55 43 68 61 72 20 2a 29 26 70 43 73 72 5b 31 5d  UChar *)&pCsr[1]
3acab 3b 0a 20 20 70 43 73 72 2d 3e 61 4f 66 66 73 65  ;.  pCsr->aOffse
3acac 74 20 3d 20 28 69 6e 74 20 2a 29 26 70 43 73 72  t = (int *)&pCsr
3acad 2d 3e 61 43 68 61 72 5b 6e 43 68 61 72 5d 3b 0a  ->aChar[nChar];.
3acae 0a 20 20 70 43 73 72 2d 3e 61 4f 66 66 73 65 74  .  pCsr->aOffset
3acaf 5b 69 4f 75 74 5d 20 3d 20 69 49 6e 70 75 74 3b  [iOut] = iInput;
3acb0 0a 20 20 55 38 5f 4e 45 58 54 28 7a 49 6e 70 75  .  U8_NEXT(zInpu
3acb1 74 2c 20 69 49 6e 70 75 74 2c 20 6e 49 6e 70 75  t, iInput, nInpu
3acb2 74 2c 20 63 29 3b 20 0a 20 20 77 68 69 6c 65 28  t, c); .  while(
3acb3 20 63 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20   c>0 ){.    int 
3acb4 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20 20  isError = 0;.   
3acb5 20 63 20 3d 20 75 5f 66 6f 6c 64 43 61 73 65 28   c = u_foldCase(
3acb6 63 2c 20 6f 70 74 29 3b 0a 20 20 20 20 55 31 36  c, opt);.    U16
3acb7 5f 41 50 50 45 4e 44 28 70 43 73 72 2d 3e 61 43  _APPEND(pCsr->aC
3acb8 68 61 72 2c 20 69 4f 75 74 2c 20 6e 43 68 61 72  har, iOut, nChar
3acb9 2c 20 63 2c 20 69 73 45 72 72 6f 72 29 3b 0a 20  , c, isError);. 
3acba 20 20 20 69 66 28 20 69 73 45 72 72 6f 72 20 29     if( isError )
3acbb 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
3acbc 66 72 65 65 28 70 43 73 72 29 3b 0a 20 20 20 20  free(pCsr);.    
3acbd 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
3acbe 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20  ERROR;.    }.   
3acbf 20 70 43 73 72 2d 3e 61 4f 66 66 73 65 74 5b 69   pCsr->aOffset[i
3acc0 4f 75 74 5d 20 3d 20 69 49 6e 70 75 74 3b 0a 0a  Out] = iInput;..
3acc1 20 20 20 20 69 66 28 20 69 49 6e 70 75 74 3c 6e      if( iInput<n
3acc2 49 6e 70 75 74 20 29 7b 0a 20 20 20 20 20 20 55  Input ){.      U
3acc3 38 5f 4e 45 58 54 28 7a 49 6e 70 75 74 2c 20 69  8_NEXT(zInput, i
3acc4 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20 63  Input, nInput, c
3acc5 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
3acc6 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 20 20 7d      c = 0;.    }
3acc7 0a 20 20 7d 0a 0a 20 20 70 43 73 72 2d 3e 70 49  .  }..  pCsr->pI
3acc8 74 65 72 20 3d 20 75 62 72 6b 5f 6f 70 65 6e 28  ter = ubrk_open(
3acc9 55 42 52 4b 5f 57 4f 52 44 2c 20 70 2d 3e 7a 4c  UBRK_WORD, p->zL
3acca 6f 63 61 6c 65 2c 20 70 43 73 72 2d 3e 61 43 68  ocale, pCsr->aCh
3accb 61 72 2c 20 69 4f 75 74 2c 20 26 73 74 61 74 75  ar, iOut, &statu
3accc 73 29 3b 0a 20 20 69 66 28 20 21 55 5f 53 55 43  s);.  if( !U_SUC
3accd 43 45 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a  CESS(status) ){.
3acce 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
3accf 28 70 43 73 72 29 3b 0a 20 20 20 20 72 65 74 75  (pCsr);.    retu
3acd0 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
3acd1 0a 20 20 7d 0a 20 20 70 43 73 72 2d 3e 6e 43 68  .  }.  pCsr->nCh
3acd2 61 72 20 3d 20 69 4f 75 74 3b 0a 0a 20 20 75 62  ar = iOut;..  ub
3acd3 72 6b 5f 66 69 72 73 74 28 70 43 73 72 2d 3e 70  rk_first(pCsr->p
3acd4 49 74 65 72 29 3b 0a 20 20 2a 70 70 43 75 72 73  Iter);.  *ppCurs
3acd5 6f 72 20 3d 20 28 73 71 6c 69 74 65 33 5f 74 6f  or = (sqlite3_to
3acd6 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
3acd7 29 70 43 73 72 3b 0a 20 20 72 65 74 75 72 6e 20  )pCsr;.  return 
3acd8 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
3acd9 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 74 6f 6b 65  .** Close a toke
3acda 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72 20  nization cursor 
3acdb 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e 65  previously opene
3acdc 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 69  d by a call to i
3acdd 63 75 4f 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74 61  cuOpen()..*/.sta
3acde 74 69 63 20 69 6e 74 20 69 63 75 43 6c 6f 73 65  tic int icuClose
3acdf 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a  (sqlite3_tokeniz
3ace0 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  er_cursor *pCurs
3ace1 6f 72 29 7b 0a 20 20 49 63 75 43 75 72 73 6f 72  or){.  IcuCursor
3ace2 20 2a 70 43 73 72 20 3d 20 28 49 63 75 43 75 72   *pCsr = (IcuCur
3ace3 73 6f 72 20 2a 29 70 43 75 72 73 6f 72 3b 0a 20  sor *)pCursor;. 
3ace4 20 75 62 72 6b 5f 63 6c 6f 73 65 28 70 43 73 72   ubrk_close(pCsr
3ace5 2d 3e 70 49 74 65 72 29 3b 0a 20 20 73 71 6c 69  ->pIter);.  sqli
3ace6 74 65 33 5f 66 72 65 65 28 70 43 73 72 2d 3e 7a  te3_free(pCsr->z
3ace7 42 75 66 66 65 72 29 3b 0a 20 20 73 71 6c 69 74  Buffer);.  sqlit
3ace8 65 33 5f 66 72 65 65 28 70 43 73 72 29 3b 0a 20  e3_free(pCsr);. 
3ace9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
3acea 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72  K;.}../*.** Extr
3aceb 61 63 74 20 74 68 65 20 6e 65 78 74 20 74 6f 6b  act the next tok
3acec 65 6e 20 66 72 6f 6d 20 61 20 74 6f 6b 65 6e 69  en from a tokeni
3aced 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72 2e 0a 2a  zation cursor..*
3acee 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 63 75  /.static int icu
3acef 4e 65 78 74 28 0a 20 20 73 71 6c 69 74 65 33 5f  Next(.  sqlite3_
3acf0 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
3acf1 20 2a 70 43 75 72 73 6f 72 2c 20 20 2f 2a 20 43   *pCursor,  /* C
3acf2 75 72 73 6f 72 20 72 65 74 75 72 6e 65 64 20 62  ursor returned b
3acf3 79 20 73 69 6d 70 6c 65 4f 70 65 6e 20 2a 2f 0a  y simpleOpen */.
3acf4 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70    const char **p
3acf5 70 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20 20 20  pToken,         
3acf6 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 2a 70        /* OUT: *p
3acf7 70 54 6f 6b 65 6e 20 69 73 20 74 68 65 20 74 6f  pToken is the to
3acf8 6b 65 6e 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e  ken text */.  in
3acf9 74 20 2a 70 6e 42 79 74 65 73 2c 20 20 20 20 20  t *pnBytes,     
3acfa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3acfb 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72    /* OUT: Number
3acfc 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 6f 6b   of bytes in tok
3acfd 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 53  en */.  int *piS
3acfe 74 61 72 74 4f 66 66 73 65 74 2c 20 20 20 20 20  tartOffset,     
3acff 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
3ad00 55 54 3a 20 53 74 61 72 74 69 6e 67 20 6f 66 66  UT: Starting off
3ad01 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a  set of token */.
3ad02 20 20 69 6e 74 20 2a 70 69 45 6e 64 4f 66 66 73    int *piEndOffs
3ad03 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  et,             
3ad04 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e        /* OUT: En
3ad05 64 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20 74  ding offset of t
3ad06 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70  oken */.  int *p
3ad07 69 50 6f 73 69 74 69 6f 6e 20 20 20 20 20 20 20  iPosition       
3ad08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3ad09 20 4f 55 54 3a 20 50 6f 73 69 74 69 6f 6e 20 69   OUT: Position i
3ad0a 6e 74 65 67 65 72 20 6f 66 20 74 6f 6b 65 6e 20  nteger of token 
3ad0b 2a 2f 0a 29 7b 0a 20 20 49 63 75 43 75 72 73 6f  */.){.  IcuCurso
3ad0c 72 20 2a 70 43 73 72 20 3d 20 28 49 63 75 43 75  r *pCsr = (IcuCu
3ad0d 72 73 6f 72 20 2a 29 70 43 75 72 73 6f 72 3b 0a  rsor *)pCursor;.
3ad0e 0a 20 20 69 6e 74 20 69 53 74 61 72 74 20 3d 20  .  int iStart = 
3ad0f 30 3b 0a 20 20 69 6e 74 20 69 45 6e 64 20 3d 20  0;.  int iEnd = 
3ad10 30 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20 3d  0;.  int nByte =
3ad11 20 30 3b 0a 0a 20 20 77 68 69 6c 65 28 20 69 53   0;..  while( iS
3ad12 74 61 72 74 3d 3d 69 45 6e 64 20 29 7b 0a 20 20  tart==iEnd ){.  
3ad13 20 20 55 43 68 61 72 33 32 20 63 3b 0a 0a 20 20    UChar32 c;..  
3ad14 20 20 69 53 74 61 72 74 20 3d 20 75 62 72 6b 5f    iStart = ubrk_
3ad15 63 75 72 72 65 6e 74 28 70 43 73 72 2d 3e 70 49  current(pCsr->pI
3ad16 74 65 72 29 3b 0a 20 20 20 20 69 45 6e 64 20 3d  ter);.    iEnd =
3ad17 20 75 62 72 6b 5f 6e 65 78 74 28 70 43 73 72 2d   ubrk_next(pCsr-
3ad18 3e 70 49 74 65 72 29 3b 0a 20 20 20 20 69 66 28  >pIter);.    if(
3ad19 20 69 45 6e 64 3d 3d 55 42 52 4b 5f 44 4f 4e 45   iEnd==UBRK_DONE
3ad1a 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
3ad1b 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20   SQLITE_DONE;.  
3ad1c 20 20 7d 0a 0a 20 20 20 20 77 68 69 6c 65 28 20    }..    while( 
3ad1d 69 53 74 61 72 74 3c 69 45 6e 64 20 29 7b 0a 20  iStart<iEnd ){. 
3ad1e 20 20 20 20 20 69 6e 74 20 69 57 68 69 74 65 20       int iWhite 
3ad1f 3d 20 69 53 74 61 72 74 3b 0a 20 20 20 20 20 20  = iStart;.      
3ad20 55 38 5f 4e 45 58 54 28 70 43 73 72 2d 3e 61 43  U8_NEXT(pCsr->aC
3ad21 68 61 72 2c 20 69 57 68 69 74 65 2c 20 70 43 73  har, iWhite, pCs
3ad22 72 2d 3e 6e 43 68 61 72 2c 20 63 29 3b 0a 20 20  r->nChar, c);.  
3ad23 20 20 20 20 69 66 28 20 75 5f 69 73 73 70 61 63      if( u_isspac
3ad24 65 28 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20  e(c) ){.        
3ad25 69 53 74 61 72 74 20 3d 20 69 57 68 69 74 65 3b  iStart = iWhite;
3ad26 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
3ad27 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
3ad28 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61     }.    }.    a
3ad29 73 73 65 72 74 28 69 53 74 61 72 74 3c 3d 69 45  ssert(iStart<=iE
3ad2a 6e 64 29 3b 0a 20 20 7d 0a 0a 20 20 64 6f 20 7b  nd);.  }..  do {
3ad2b 0a 20 20 20 20 55 45 72 72 6f 72 43 6f 64 65 20  .    UErrorCode 
3ad2c 73 74 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f 5f  status = U_ZERO_
3ad2d 45 52 52 4f 52 3b 0a 20 20 20 20 69 66 28 20 6e  ERROR;.    if( n
3ad2e 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20 63 68  Byte ){.      ch
3ad2f 61 72 20 2a 7a 4e 65 77 20 3d 20 73 71 6c 69 74  ar *zNew = sqlit
3ad30 65 33 5f 72 65 61 6c 6c 6f 63 28 70 43 73 72 2d  e3_realloc(pCsr-
3ad31 3e 7a 42 75 66 66 65 72 2c 20 6e 42 79 74 65 29  >zBuffer, nByte)
3ad32 3b 0a 20 20 20 20 20 20 69 66 28 20 21 7a 4e 65  ;.      if( !zNe
3ad33 77 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  w ){.        ret
3ad34 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
3ad35 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
3ad36 70 43 73 72 2d 3e 7a 42 75 66 66 65 72 20 3d 20  pCsr->zBuffer = 
3ad37 7a 4e 65 77 3b 0a 20 20 20 20 20 20 70 43 73 72  zNew;.      pCsr
3ad38 2d 3e 6e 42 75 66 66 65 72 20 3d 20 6e 42 79 74  ->nBuffer = nByt
3ad39 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 75 5f  e;.    }..    u_
3ad3a 73 74 72 54 6f 55 54 46 38 28 0a 20 20 20 20 20  strToUTF8(.     
3ad3b 20 20 20 70 43 73 72 2d 3e 7a 42 75 66 66 65 72     pCsr->zBuffer
3ad3c 2c 20 70 43 73 72 2d 3e 6e 42 75 66 66 65 72 2c  , pCsr->nBuffer,
3ad3d 20 26 6e 42 79 74 65 2c 20 20 20 20 2f 2a 20 4f   &nByte,    /* O
3ad3e 75 74 70 75 74 20 76 61 72 73 20 2a 2f 0a 20 20  utput vars */.  
3ad3f 20 20 20 20 20 20 26 70 43 73 72 2d 3e 61 43 68        &pCsr->aCh
3ad40 61 72 5b 69 53 74 61 72 74 5d 2c 20 69 45 6e 64  ar[iStart], iEnd
3ad41 2d 69 53 74 61 72 74 2c 20 20 20 20 20 20 20 2f  -iStart,       /
3ad42 2a 20 49 6e 70 75 74 20 76 61 72 73 20 2a 2f 0a  * Input vars */.
3ad43 20 20 20 20 20 20 20 20 26 73 74 61 74 75 73 20          &status 
3ad44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ad45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ad46 20 2f 2a 20 4f 75 74 70 75 74 20 73 75 63 63 65   /* Output succe
3ad47 73 73 2f 66 61 69 6c 75 72 65 20 2a 2f 0a 20 20  ss/failure */.  
3ad48 20 20 29 3b 0a 20 20 7d 20 77 68 69 6c 65 28 20    );.  } while( 
3ad49 6e 42 79 74 65 3e 70 43 73 72 2d 3e 6e 42 75 66  nByte>pCsr->nBuf
3ad4a 66 65 72 20 29 3b 0a 0a 20 20 2a 70 70 54 6f 6b  fer );..  *ppTok
3ad4b 65 6e 20 3d 20 70 43 73 72 2d 3e 7a 42 75 66 66  en = pCsr->zBuff
3ad4c 65 72 3b 0a 20 20 2a 70 6e 42 79 74 65 73 20 3d  er;.  *pnBytes =
3ad4d 20 6e 42 79 74 65 3b 0a 20 20 2a 70 69 53 74 61   nByte;.  *piSta
3ad4e 72 74 4f 66 66 73 65 74 20 3d 20 70 43 73 72 2d  rtOffset = pCsr-
3ad4f 3e 61 4f 66 66 73 65 74 5b 69 53 74 61 72 74 5d  >aOffset[iStart]
3ad50 3b 0a 20 20 2a 70 69 45 6e 64 4f 66 66 73 65 74  ;.  *piEndOffset
3ad51 20 3d 20 70 43 73 72 2d 3e 61 4f 66 66 73 65 74   = pCsr->aOffset
3ad52 5b 69 45 6e 64 5d 3b 0a 20 20 2a 70 69 50 6f 73  [iEnd];.  *piPos
3ad53 69 74 69 6f 6e 20 3d 20 70 43 73 72 2d 3e 69 54  ition = pCsr->iT
3ad54 6f 6b 65 6e 2b 2b 3b 0a 0a 20 20 72 65 74 75 72  oken++;..  retur
3ad55 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
3ad56 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 74 20 6f 66  /*.** The set of
3ad57 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 69   routines that i
3ad58 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 73 69 6d  mplement the sim
3ad59 70 6c 65 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f  ple tokenizer.*/
3ad5a 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71  .static const sq
3ad5b 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
3ad5c 6d 6f 64 75 6c 65 20 69 63 75 54 6f 6b 65 6e 69  module icuTokeni
3ad5d 7a 65 72 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20 20  zerModule = {.  
3ad5e 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
3ad5f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
3ad60 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 69 63  iVersion */.  ic
3ad61 75 43 72 65 61 74 65 2c 20 20 20 20 20 20 20 20  uCreate,        
3ad62 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43             /* xC
3ad63 72 65 61 74 65 20 20 2a 2f 0a 20 20 69 63 75 44  reate  */.  icuD
3ad64 65 73 74 72 6f 79 2c 20 20 20 20 20 20 20 20 20  estroy,         
3ad65 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 72 65           /* xCre
3ad66 61 74 65 20 20 2a 2f 0a 20 20 69 63 75 4f 70 65  ate  */.  icuOpe
3ad67 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
3ad68 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20         /* xOpen 
3ad69 20 20 20 2a 2f 0a 20 20 69 63 75 43 6c 6f 73 65     */.  icuClose
3ad6a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
3ad6b 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 20       /* xClose  
3ad6c 20 2a 2f 0a 20 20 69 63 75 4e 65 78 74 2c 20 20   */.  icuNext,  
3ad6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3ad6e 20 20 20 2f 2a 20 78 4e 65 78 74 20 20 20 20 2a     /* xNext    *
3ad6f 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  /.};../*.** Set 
3ad70 2a 70 70 4d 6f 64 75 6c 65 20 74 6f 20 70 6f 69  *ppModule to poi
3ad71 6e 74 20 61 74 20 74 68 65 20 69 6d 70 6c 65 6d  nt at the implem
3ad72 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
3ad73 49 43 55 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a  ICU tokenizer..*
3ad74 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
3ad75 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74 73   void sqlite3Fts
3ad76 33 49 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  3IcuTokenizerMod
3ad77 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 74  ule(.  sqlite3_t
3ad78 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
3ad79 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 0a  const**ppModule.
3ad7a 29 7b 0a 20 20 2a 70 70 4d 6f 64 75 6c 65 20 3d  ){.  *ppModule =
3ad7b 20 26 69 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f   &icuTokenizerMo
3ad7c 64 75 6c 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  dule;.}..#endif 
3ad7d 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  /* defined(SQLIT
3ad7e 45 5f 45 4e 41 42 4c 45 5f 49 43 55 29 20 2a 2f  E_ENABLE_ICU) */
3ad7f 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
3ad80 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29  ned(SQLITE_CORE)
3ad81 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
3ad82 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20  TE_ENABLE_FTS3) 
3ad83 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
3ad84 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f  *** End of fts3_
3ad85 69 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  icu.c **********
3ad86 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ad87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3ad88 2a 2a 2f 0a                                      **/.